1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* 28 * SCSI SCSA-compliant and not-so-DDI-compliant Tape Driver 29 */ 30 31 #if defined(lint) && !defined(DEBUG) 32 #define DEBUG 1 33 #endif 34 35 #include <sys/modctl.h> 36 #include <sys/scsi/scsi.h> 37 #include <sys/mtio.h> 38 #include <sys/scsi/targets/stdef.h> 39 #include <sys/file.h> 40 #include <sys/kstat.h> 41 #include <sys/ddidmareq.h> 42 #include <sys/ddi.h> 43 #include <sys/sunddi.h> 44 #include <sys/byteorder.h> 45 46 #define IOSP KSTAT_IO_PTR(un->un_stats) 47 /* 48 * stats maintained only for reads/writes as commands 49 * like rewind etc skew the wait/busy times 50 */ 51 #define IS_RW(bp) ((bp)->b_bcount > 0) 52 #define ST_DO_KSTATS(bp, kstat_function) \ 53 if ((bp != un->un_sbufp) && un->un_stats && IS_RW(bp)) { \ 54 kstat_function(IOSP); \ 55 } 56 57 #define ST_DO_ERRSTATS(un, x) \ 58 if (un->un_errstats) { \ 59 struct st_errstats *stp; \ 60 stp = (struct st_errstats *)un->un_errstats->ks_data; \ 61 stp->x.value.ul++; \ 62 } 63 64 #define FILL_SCSI1_LUN(devp, pkt) \ 65 if ((devp)->sd_inq->inq_ansi == 0x1) { \ 66 int _lun; \ 67 _lun = ddi_prop_get_int(DDI_DEV_T_ANY, (devp)->sd_dev, \ 68 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_LUN, 0); \ 69 if (_lun > 0) { \ 70 ((union scsi_cdb *)(pkt)->pkt_cdbp)->scc_lun = \ 71 _lun; \ 72 } \ 73 } 74 75 /* 76 * get an available contig mem header, cp. 77 * when big_enough is true, we will return NULL, if no big enough 78 * contig mem is found. 79 * when big_enough is false, we will try to find cp containing big 80 * enough contig mem. if not found, we will ruturn the last cp available. 81 * 82 * used by st_get_contig_mem() 83 */ 84 #define ST_GET_CONTIG_MEM_HEAD(un, cp, len, big_enough) { \ 85 struct contig_mem *tmp_cp = NULL; \ 86 for ((cp) = (un)->un_contig_mem; \ 87 (cp) != NULL; \ 88 tmp_cp = (cp), (cp) = (cp)->cm_next) { \ 89 if (((cp)->cm_len >= (len)) || \ 90 (!(big_enough) && ((cp)->cm_next == NULL))) { \ 91 if (tmp_cp == NULL) { \ 92 (un)->un_contig_mem = (cp)->cm_next; \ 93 } else { \ 94 tmp_cp->cm_next = (cp)->cm_next; \ 95 } \ 96 (cp)->cm_next = NULL; \ 97 (un)->un_contig_mem_available_num--; \ 98 break; \ 99 } \ 100 } \ 101 } 102 103 #define ST_NUM_MEMBERS(array) (sizeof (array) / sizeof (array[0])) 104 #define COPY_POS(dest, source) bcopy(source, dest, sizeof (tapepos_t)) 105 #define ISALNUM(byte) \ 106 (((byte) >= 'a' && (byte) <= 'z') || \ 107 ((byte) >= 'A' && (byte) <= 'Z') || \ 108 ((byte) >= '0' && (byte) <= '9')) 109 110 #define ONE_K 1024 111 112 #define MAX_SPACE_CNT(cnt) if (cnt >= 0) { \ 113 if (cnt > MIN(SP_CNT_MASK, INT32_MAX)) \ 114 return (EINVAL); \ 115 } else { \ 116 if (-(cnt) > MIN(SP_CNT_MASK, INT32_MAX)) \ 117 return (EINVAL); \ 118 } \ 119 120 /* 121 * Global External Data Definitions 122 */ 123 extern struct scsi_key_strings scsi_cmds[]; 124 extern uchar_t scsi_cdb_size[]; 125 126 /* 127 * Local Static Data 128 */ 129 static void *st_state; 130 static char *const st_label = "st"; 131 static volatile int st_recov_sz = sizeof (recov_info); 132 static const char mp_misconf[] = { 133 "St Tape is misconfigured, MPxIO enabled and " 134 "tape-command-recovery-disable set in st.conf\n" 135 }; 136 137 #ifdef __x86 138 /* 139 * We need to use below DMA attr to alloc physically contiguous 140 * memory to do I/O in big block size 141 */ 142 static ddi_dma_attr_t st_contig_mem_dma_attr = { 143 DMA_ATTR_V0, /* version number */ 144 0x0, /* lowest usable address */ 145 0xFFFFFFFFull, /* high DMA address range */ 146 0xFFFFFFFFull, /* DMA counter register */ 147 1, /* DMA address alignment */ 148 1, /* DMA burstsizes */ 149 1, /* min effective DMA size */ 150 0xFFFFFFFFull, /* max DMA xfer size */ 151 0xFFFFFFFFull, /* segment boundary */ 152 1, /* s/g list length */ 153 1, /* granularity of device */ 154 0 /* DMA transfer flags */ 155 }; 156 157 static ddi_device_acc_attr_t st_acc_attr = { 158 DDI_DEVICE_ATTR_V0, 159 DDI_NEVERSWAP_ACC, 160 DDI_STRICTORDER_ACC 161 }; 162 163 /* set limitation for the number of contig_mem */ 164 static int st_max_contig_mem_num = ST_MAX_CONTIG_MEM_NUM; 165 #endif 166 167 /* 168 * Tunable parameters 169 * 170 * DISCLAIMER 171 * ---------- 172 * These parameters are intended for use only in system testing; if you use 173 * them in production systems, you do so at your own risk. Altering any 174 * variable not listed below may cause unpredictable system behavior. 175 * 176 * st_check_media_time 177 * 178 * Three second state check 179 * 180 * st_allow_large_xfer 181 * 182 * Gated with ST_NO_RECSIZE_LIMIT 183 * 184 * 0 - Transfers larger than 64KB will not be allowed 185 * regardless of the setting of ST_NO_RECSIZE_LIMIT 186 * 1 - Transfers larger than 64KB will be allowed 187 * if ST_NO_RECSIZE_LIMIT is TRUE for the drive 188 * 189 * st_report_soft_errors_on_close 190 * 191 * Gated with ST_SOFT_ERROR_REPORTING 192 * 193 * 0 - Errors will not be reported on close regardless 194 * of the setting of ST_SOFT_ERROR_REPORTING 195 * 196 * 1 - Errors will be reported on close if 197 * ST_SOFT_ERROR_REPORTING is TRUE for the drive 198 */ 199 static int st_selection_retry_count = ST_SEL_RETRY_COUNT; 200 static int st_retry_count = ST_RETRY_COUNT; 201 202 static int st_io_time = ST_IO_TIME; 203 static int st_long_timeout_x = ST_LONG_TIMEOUT_X; 204 205 static int st_space_time = ST_SPACE_TIME; 206 static int st_long_space_time_x = ST_LONG_SPACE_TIME_X; 207 208 static int st_error_level = SCSI_ERR_RETRYABLE; 209 static int st_check_media_time = 3000000; /* 3 Second State Check */ 210 211 static int st_max_throttle = ST_MAX_THROTTLE; 212 213 static clock_t st_wait_cmds_complete = ST_WAIT_CMDS_COMPLETE; 214 215 static int st_allow_large_xfer = 1; 216 static int st_report_soft_errors_on_close = 1; 217 218 /* 219 * End of tunable parameters list 220 */ 221 222 223 224 /* 225 * Asynchronous I/O and persistent errors, refer to PSARC/1995/228 226 * 227 * Asynchronous I/O's main offering is that it is a non-blocking way to do 228 * reads and writes. The driver will queue up all the requests it gets and 229 * have them ready to transport to the HBA. Unfortunately, we cannot always 230 * just ship the I/O requests to the HBA, as there errors and exceptions 231 * that may happen when we don't want the HBA to continue. Therein comes 232 * the flush-on-errors capability. If the HBA supports it, then st will 233 * send in st_max_throttle I/O requests at the same time. 234 * 235 * Persistent errors : This was also reasonably simple. In the interrupt 236 * routines, if there was an error or exception (FM, LEOT, media error, 237 * transport error), the persistent error bits are set and shuts everything 238 * down, but setting the throttle to zero. If we hit and exception in the 239 * HBA, and flush-on-errors were set, we wait for all outstanding I/O's to 240 * come back (with CMD_ABORTED), then flush all bp's in the wait queue with 241 * the appropriate error, and this will preserve order. Of course, depending 242 * on the exception we have to show a zero read or write before we show 243 * errors back to the application. 244 */ 245 246 extern const int st_ndrivetypes; /* defined in st_conf.c */ 247 extern const struct st_drivetype st_drivetypes[]; 248 extern const char st_conf_version[]; 249 250 #ifdef STDEBUG 251 static int st_soft_error_report_debug = 0; 252 volatile int st_debug = 0; 253 static volatile dev_info_t *st_lastdev; 254 static kmutex_t st_debug_mutex; 255 #endif 256 257 #define ST_MT02_NAME "Emulex MT02 QIC-11/24 " 258 259 static const struct vid_drivetype { 260 char *vid; 261 char type; 262 } st_vid_dt[] = { 263 {"LTO-CVE ", MT_LTO}, 264 {"QUANTUM ", MT_ISDLT}, 265 {"SONY ", MT_ISAIT}, 266 {"STK ", MT_ISSTK9840} 267 }; 268 269 static const struct driver_minor_data { 270 char *name; 271 int minor; 272 } st_minor_data[] = { 273 /* 274 * The top 4 entries are for the default densities, 275 * don't alter their position. 276 */ 277 {"", 0}, 278 {"n", MT_NOREWIND}, 279 {"b", MT_BSD}, 280 {"bn", MT_NOREWIND | MT_BSD}, 281 {"l", MT_DENSITY1}, 282 {"m", MT_DENSITY2}, 283 {"h", MT_DENSITY3}, 284 {"c", MT_DENSITY4}, 285 {"u", MT_DENSITY4}, 286 {"ln", MT_DENSITY1 | MT_NOREWIND}, 287 {"mn", MT_DENSITY2 | MT_NOREWIND}, 288 {"hn", MT_DENSITY3 | MT_NOREWIND}, 289 {"cn", MT_DENSITY4 | MT_NOREWIND}, 290 {"un", MT_DENSITY4 | MT_NOREWIND}, 291 {"lb", MT_DENSITY1 | MT_BSD}, 292 {"mb", MT_DENSITY2 | MT_BSD}, 293 {"hb", MT_DENSITY3 | MT_BSD}, 294 {"cb", MT_DENSITY4 | MT_BSD}, 295 {"ub", MT_DENSITY4 | MT_BSD}, 296 {"lbn", MT_DENSITY1 | MT_NOREWIND | MT_BSD}, 297 {"mbn", MT_DENSITY2 | MT_NOREWIND | MT_BSD}, 298 {"hbn", MT_DENSITY3 | MT_NOREWIND | MT_BSD}, 299 {"cbn", MT_DENSITY4 | MT_NOREWIND | MT_BSD}, 300 {"ubn", MT_DENSITY4 | MT_NOREWIND | MT_BSD} 301 }; 302 303 /* strings used in many debug and warning messages */ 304 static const char wr_str[] = "write"; 305 static const char rd_str[] = "read"; 306 static const char wrg_str[] = "writing"; 307 static const char rdg_str[] = "reading"; 308 static const char *space_strs[] = { 309 "records", 310 "filemarks", 311 "sequential filemarks", 312 "eod", 313 "setmarks", 314 "sequential setmarks", 315 "Reserved", 316 "Reserved" 317 }; 318 static const char *load_strs[] = { 319 "unload", /* LD_UNLOAD 0 */ 320 "load", /* LD_LOAD 1 */ 321 "retension", /* LD_RETEN 2 */ 322 "load reten", /* LD_LOAD | LD_RETEN 3 */ 323 "eod", /* LD_EOT 4 */ 324 "load EOD", /* LD_LOAD | LD_EOT 5 */ 325 "reten EOD", /* LD_RETEN | LD_EOT 6 */ 326 "load reten EOD" /* LD_LOAD|LD_RETEN|LD_EOT 7 */ 327 "hold", /* LD_HOLD 8 */ 328 "load and hold" /* LD_LOAD | LD_HOLD 9 */ 329 }; 330 331 static const char *errstatenames[] = { 332 "COMMAND_DONE", 333 "COMMAND_DONE_ERROR", 334 "COMMAND_DONE_ERROR_RECOVERED", 335 "QUE_COMMAND", 336 "QUE_BUSY_COMMAND", 337 "QUE_SENSE", 338 "JUST_RETURN", 339 "COMMAND_DONE_EACCES", 340 "QUE_LAST_COMMAND", 341 "COMMAND_TIMEOUT", 342 "PATH_FAILED", 343 "DEVICE_RESET", 344 "DEVICE_TAMPER", 345 "ATTEMPT_RETRY" 346 }; 347 348 const char *bogusID = "Unknown Media ID"; 349 350 /* default density offsets in the table above */ 351 #define DEF_BLANK 0 352 #define DEF_NOREWIND 1 353 #define DEF_BSD 2 354 #define DEF_BSD_NR 3 355 356 /* Sense Key, ASC/ASCQ for which tape ejection is needed */ 357 358 static struct tape_failure_code { 359 uchar_t key; 360 uchar_t add_code; 361 uchar_t qual_code; 362 } st_tape_failure_code[] = { 363 { KEY_HARDWARE_ERROR, 0x15, 0x01}, 364 { KEY_HARDWARE_ERROR, 0x44, 0x00}, 365 { KEY_HARDWARE_ERROR, 0x53, 0x00}, 366 { KEY_HARDWARE_ERROR, 0x53, 0x01}, 367 { KEY_NOT_READY, 0x53, 0x00}, 368 { 0xff} 369 }; 370 371 /* clean bit position and mask */ 372 373 static struct cln_bit_position { 374 ushort_t cln_bit_byte; 375 uchar_t cln_bit_mask; 376 } st_cln_bit_position[] = { 377 { 21, 0x08}, 378 { 70, 0xc0}, 379 { 18, 0x81} /* 80 bit indicates in bit mode, 1 bit clean light is on */ 380 }; 381 382 /* 383 * architecture dependent allocation restrictions. For x86, we'll set 384 * dma_attr_addr_hi to st_max_phys_addr and dma_attr_sgllen to 385 * st_sgl_size during _init(). 386 */ 387 #if defined(__sparc) 388 static ddi_dma_attr_t st_alloc_attr = { 389 DMA_ATTR_V0, /* version number */ 390 0x0, /* lowest usable address */ 391 0xFFFFFFFFull, /* high DMA address range */ 392 0xFFFFFFFFull, /* DMA counter register */ 393 1, /* DMA address alignment */ 394 1, /* DMA burstsizes */ 395 1, /* min effective DMA size */ 396 0xFFFFFFFFull, /* max DMA xfer size */ 397 0xFFFFFFFFull, /* segment boundary */ 398 1, /* s/g list length */ 399 512, /* granularity of device */ 400 0 /* DMA transfer flags */ 401 }; 402 #elif defined(__x86) 403 static ddi_dma_attr_t st_alloc_attr = { 404 DMA_ATTR_V0, /* version number */ 405 0x0, /* lowest usable address */ 406 0x0, /* high DMA address range [set in _init()] */ 407 0xFFFFull, /* DMA counter register */ 408 512, /* DMA address alignment */ 409 1, /* DMA burstsizes */ 410 1, /* min effective DMA size */ 411 0xFFFFFFFFull, /* max DMA xfer size */ 412 0xFFFFFFFFull, /* segment boundary */ 413 0, /* s/g list length */ 414 512, /* granularity of device [set in _init()] */ 415 0 /* DMA transfer flags */ 416 }; 417 uint64_t st_max_phys_addr = 0xFFFFFFFFull; 418 int st_sgl_size = 0xF; 419 420 #endif 421 422 /* 423 * Configuration Data: 424 * 425 * Device driver ops vector 426 */ 427 static int st_aread(dev_t dev, struct aio_req *aio, cred_t *cred_p); 428 static int st_awrite(dev_t dev, struct aio_req *aio, cred_t *cred_p); 429 static int st_read(dev_t dev, struct uio *uio_p, cred_t *cred_p); 430 static int st_write(dev_t dev, struct uio *uio_p, cred_t *cred_p); 431 static int st_open(dev_t *devp, int flag, int otyp, cred_t *cred_p); 432 static int st_close(dev_t dev, int flag, int otyp, cred_t *cred_p); 433 static int st_strategy(struct buf *bp); 434 static int st_queued_strategy(buf_t *bp); 435 static int st_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, 436 cred_t *cred_p, int *rval_p); 437 extern int nulldev(), nodev(); 438 439 static struct cb_ops st_cb_ops = { 440 st_open, /* open */ 441 st_close, /* close */ 442 st_queued_strategy, /* strategy Not Block device but async checks */ 443 nodev, /* print */ 444 nodev, /* dump */ 445 st_read, /* read */ 446 st_write, /* write */ 447 st_ioctl, /* ioctl */ 448 nodev, /* devmap */ 449 nodev, /* mmap */ 450 nodev, /* segmap */ 451 nochpoll, /* poll */ 452 ddi_prop_op, /* cb_prop_op */ 453 0, /* streamtab */ 454 D_64BIT | D_MP | D_NEW | D_HOTPLUG | 455 D_OPEN_RETURNS_EINTR, /* cb_flag */ 456 CB_REV, /* cb_rev */ 457 st_aread, /* async I/O read entry point */ 458 st_awrite /* async I/O write entry point */ 459 460 }; 461 462 static int st_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, 463 void **result); 464 static int st_probe(dev_info_t *dev); 465 static int st_attach(dev_info_t *dev, ddi_attach_cmd_t cmd); 466 static int st_detach(dev_info_t *dev, ddi_detach_cmd_t cmd); 467 468 static struct dev_ops st_ops = { 469 DEVO_REV, /* devo_rev, */ 470 0, /* refcnt */ 471 st_info, /* info */ 472 nulldev, /* identify */ 473 st_probe, /* probe */ 474 st_attach, /* attach */ 475 st_detach, /* detach */ 476 nodev, /* reset */ 477 &st_cb_ops, /* driver operations */ 478 (struct bus_ops *)0, /* bus operations */ 479 nulldev, /* power */ 480 ddi_quiesce_not_needed, /* devo_quiesce */ 481 }; 482 483 /* 484 * Local Function Declarations 485 */ 486 static char *st_print_scsi_cmd(char cmd); 487 static void st_print_cdb(dev_info_t *dip, char *label, uint_t level, 488 char *title, char *cdb); 489 static void st_clean_print(dev_info_t *dev, char *label, uint_t level, 490 char *title, char *data, int len); 491 static int st_doattach(struct scsi_device *devp, int (*canwait)()); 492 static void st_known_tape_type(struct scsi_tape *un); 493 static int st_get_conf_from_st_dot_conf(struct scsi_tape *, char *, 494 struct st_drivetype *); 495 static int st_get_conf_from_st_conf_dot_c(struct scsi_tape *, char *, 496 struct st_drivetype *); 497 static int st_get_conf_from_tape_drive(struct scsi_tape *, char *, 498 struct st_drivetype *); 499 static int st_get_densities_from_tape_drive(struct scsi_tape *, 500 struct st_drivetype *); 501 static int st_get_timeout_values_from_tape_drive(struct scsi_tape *, 502 struct st_drivetype *); 503 static int st_get_timeouts_value(struct scsi_tape *, uchar_t, ushort_t *, 504 ushort_t); 505 static int st_get_default_conf(struct scsi_tape *, char *, 506 struct st_drivetype *); 507 static int st_rw(dev_t dev, struct uio *uio, int flag); 508 static int st_arw(dev_t dev, struct aio_req *aio, int flag); 509 static int st_find_eod(struct scsi_tape *un); 510 static int st_check_density_or_wfm(dev_t dev, int wfm, int mode, int stepflag); 511 static int st_uscsi_cmd(struct scsi_tape *un, struct uscsi_cmd *, int flag); 512 static int st_mtioctop(struct scsi_tape *un, intptr_t arg, int flag); 513 static int st_mtiocltop(struct scsi_tape *un, intptr_t arg, int flag); 514 static int st_do_mtioctop(struct scsi_tape *un, struct mtlop *mtop); 515 static void st_start(struct scsi_tape *un); 516 static int st_handle_start_busy(struct scsi_tape *un, struct buf *bp, 517 clock_t timeout_interval, int queued); 518 static int st_handle_intr_busy(struct scsi_tape *un, struct buf *bp, 519 clock_t timeout_interval); 520 static int st_handle_intr_retry_lcmd(struct scsi_tape *un, struct buf *bp); 521 static void st_done_and_mutex_exit(struct scsi_tape *un, struct buf *bp); 522 static void st_init(struct scsi_tape *un); 523 static void st_make_cmd(struct scsi_tape *un, struct buf *bp, 524 int (*func)(caddr_t)); 525 static void st_make_uscsi_cmd(struct scsi_tape *, struct uscsi_cmd *, 526 struct buf *bp, int (*func)(caddr_t)); 527 static void st_intr(struct scsi_pkt *pkt); 528 static void st_set_state(struct scsi_tape *un, buf_t *bp); 529 static void st_test_append(struct buf *bp); 530 static int st_runout(caddr_t); 531 static int st_cmd(struct scsi_tape *un, int com, int64_t count, int wait); 532 static int st_setup_cmd(struct scsi_tape *un, buf_t *bp, int com, 533 int64_t count); 534 static int st_set_compression(struct scsi_tape *un); 535 static int st_write_fm(dev_t dev, int wfm); 536 static int st_determine_generic(struct scsi_tape *un); 537 static int st_determine_density(struct scsi_tape *un, int rw); 538 static int st_get_density(struct scsi_tape *un); 539 static int st_set_density(struct scsi_tape *un); 540 static int st_loadtape(struct scsi_tape *un); 541 static int st_modesense(struct scsi_tape *un); 542 static int st_modeselect(struct scsi_tape *un); 543 static errstate st_handle_incomplete(struct scsi_tape *un, struct buf *bp); 544 static int st_wrongtapetype(struct scsi_tape *un); 545 static errstate st_check_error(struct scsi_tape *un, struct scsi_pkt *pkt); 546 static errstate st_handle_sense(struct scsi_tape *un, struct buf *bp, 547 tapepos_t *); 548 static errstate st_handle_autosense(struct scsi_tape *un, struct buf *bp, 549 tapepos_t *); 550 static int st_get_error_entry(struct scsi_tape *un, intptr_t arg, int flag); 551 static void st_update_error_stack(struct scsi_tape *un, struct scsi_pkt *pkt, 552 struct scsi_arq_status *cmd); 553 static void st_empty_error_stack(struct scsi_tape *un); 554 static errstate st_decode_sense(struct scsi_tape *un, struct buf *bp, int amt, 555 struct scsi_arq_status *, tapepos_t *); 556 static int st_report_soft_errors(dev_t dev, int flag); 557 static void st_delayed_cv_broadcast(void *arg); 558 static int st_check_media(dev_t dev, enum mtio_state state); 559 static int st_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp); 560 static void st_intr_restart(void *arg); 561 static void st_start_restart(void *arg); 562 static int st_gen_mode_sense(struct scsi_tape *un, ubufunc_t ubf, int page, 563 struct seq_mode *page_data, int page_size); 564 static int st_change_block_size(struct scsi_tape *un, uint32_t nblksz); 565 static int st_gen_mode_select(struct scsi_tape *un, ubufunc_t ubf, 566 struct seq_mode *page_data, int page_size); 567 static int st_read_block_limits(struct scsi_tape *un, 568 struct read_blklim *read_blk); 569 static int st_report_density_support(struct scsi_tape *un, 570 uchar_t *density_data, size_t buflen); 571 static int st_report_supported_operation(struct scsi_tape *un, 572 uchar_t *oper_data, uchar_t option_code, ushort_t service_action); 573 static int st_tape_init(struct scsi_tape *un); 574 static void st_flush(struct scsi_tape *un); 575 static void st_set_pe_errno(struct scsi_tape *un); 576 static void st_hba_unflush(struct scsi_tape *un); 577 static void st_turn_pe_on(struct scsi_tape *un); 578 static void st_turn_pe_off(struct scsi_tape *un); 579 static void st_set_pe_flag(struct scsi_tape *un); 580 static void st_clear_pe(struct scsi_tape *un); 581 static void st_wait_for_io(struct scsi_tape *un); 582 static int st_set_devconfig_page(struct scsi_tape *un, int compression_on); 583 static int st_set_datacomp_page(struct scsi_tape *un, int compression_on); 584 static int st_reserve_release(struct scsi_tape *un, int command, ubufunc_t ubf); 585 static int st_check_cdb_for_need_to_reserve(struct scsi_tape *un, uchar_t *cdb); 586 static int st_check_cmd_for_need_to_reserve(struct scsi_tape *un, uchar_t cmd, 587 int count); 588 static int st_take_ownership(struct scsi_tape *un, ubufunc_t ubf); 589 static int st_check_asc_ascq(struct scsi_tape *un); 590 static int st_check_clean_bit(struct scsi_tape *un); 591 static int st_check_alert_flags(struct scsi_tape *un); 592 static int st_check_sequential_clean_bit(struct scsi_tape *un); 593 static int st_check_sense_clean_bit(struct scsi_tape *un); 594 static int st_clear_unit_attentions(dev_t dev_instance, int max_trys); 595 static void st_calculate_timeouts(struct scsi_tape *un); 596 static writablity st_is_drive_worm(struct scsi_tape *un); 597 static int st_read_attributes(struct scsi_tape *un, uint16_t attribute, 598 void *buf, size_t size, ubufunc_t bufunc); 599 static int st_get_special_inquiry(struct scsi_tape *un, uchar_t size, 600 caddr_t dest, uchar_t page); 601 static int st_update_block_pos(struct scsi_tape *un, bufunc_t bf, 602 int post_space); 603 static int st_interpret_read_pos(struct scsi_tape const *un, tapepos_t *dest, 604 read_p_types type, size_t data_sz, const caddr_t responce, int post_space); 605 static int st_get_read_pos(struct scsi_tape *un, buf_t *bp); 606 static int st_logical_block_locate(struct scsi_tape *un, ubufunc_t ubf, 607 tapepos_t *pos, uint64_t lblk, uchar_t partition); 608 static int st_mtfsf_ioctl(struct scsi_tape *un, int64_t files); 609 static int st_mtfsr_ioctl(struct scsi_tape *un, int64_t count); 610 static int st_mtbsf_ioctl(struct scsi_tape *un, int64_t files); 611 static int st_mtnbsf_ioctl(struct scsi_tape *un, int64_t count); 612 static int st_mtbsr_ioctl(struct scsi_tape *un, int64_t num); 613 static int st_mtfsfm_ioctl(struct scsi_tape *un, int64_t cnt); 614 static int st_mtbsfm_ioctl(struct scsi_tape *un, int64_t cnt); 615 static int st_backward_space_files(struct scsi_tape *un, int64_t count, 616 int infront); 617 static int st_forward_space_files(struct scsi_tape *un, int64_t files); 618 static int st_scenic_route_to_begining_of_file(struct scsi_tape *un, 619 int32_t fileno); 620 static int st_space_to_begining_of_file(struct scsi_tape *un); 621 static int st_space_records(struct scsi_tape *un, int64_t records); 622 static int st_get_media_identification(struct scsi_tape *un, ubufunc_t bufunc); 623 static errstate st_command_recovery(struct scsi_tape *un, struct scsi_pkt *pkt, 624 errstate onentry); 625 static void st_recover(void *arg); 626 static void st_recov_cb(struct scsi_pkt *pkt); 627 static int st_rcmd(struct scsi_tape *un, int com, int64_t count, int wait); 628 static int st_uscsi_rcmd(struct scsi_tape *un, struct uscsi_cmd *ucmd, 629 int flag); 630 static void st_add_recovery_info_to_pkt(struct scsi_tape *un, buf_t *bp, 631 struct scsi_pkt *cmd); 632 static int st_check_mode_for_change(struct scsi_tape *un, ubufunc_t ubf); 633 static int st_test_path_to_device(struct scsi_tape *un); 634 static int st_recovery_read_pos(struct scsi_tape *un, read_p_types type, 635 read_pos_data_t *raw); 636 static int st_recovery_get_position(struct scsi_tape *un, tapepos_t *read, 637 read_pos_data_t *raw); 638 static int st_compare_expected_position(struct scsi_tape *un, st_err_info *ei, 639 cmd_attribute const * cmd_att, tapepos_t *read); 640 static errstate st_recover_reissue_pkt(struct scsi_tape *us, 641 struct scsi_pkt *pkt); 642 static int st_transport(struct scsi_tape *un, struct scsi_pkt *pkt); 643 static buf_t *st_remove_from_queue(buf_t **head, buf_t **tail, buf_t *bp); 644 static void st_add_to_queue(buf_t **head, buf_t **tail, buf_t *end, buf_t *bp); 645 static int st_reset(struct scsi_tape *un, int reset_type); 646 static void st_reset_notification(caddr_t arg); 647 static const cmd_attribute *st_lookup_cmd_attribute(unsigned char cmd); 648 649 static int st_set_target_TLR_mode(struct scsi_tape *un, ubufunc_t ubf); 650 static int st_make_sure_mode_data_is_correct(struct scsi_tape *un, 651 ubufunc_t ubf); 652 653 #ifdef __x86 654 /* 655 * routines for I/O in big block size 656 */ 657 static void st_release_contig_mem(struct scsi_tape *un, struct contig_mem *cp); 658 static struct contig_mem *st_get_contig_mem(struct scsi_tape *un, size_t len, 659 int alloc_flags); 660 static int st_bigblk_xfer_done(struct buf *bp); 661 static struct buf *st_get_bigblk_bp(struct buf *bp); 662 #endif 663 static void st_print_position(dev_info_t *dev, char *label, uint_t level, 664 const char *comment, tapepos_t *pos); 665 666 /* 667 * error statistics create/update functions 668 */ 669 static int st_create_errstats(struct scsi_tape *, int); 670 static int st_validate_tapemarks(struct scsi_tape *un, ubufunc_t ubf, 671 tapepos_t *pos); 672 673 #ifdef STDEBUG 674 static void st_debug_cmds(struct scsi_tape *un, int com, int count, int wait); 675 #endif /* STDEBUG */ 676 static char *st_dev_name(dev_t dev); 677 678 #if !defined(lint) 679 _NOTE(SCHEME_PROTECTS_DATA("unique per pkt", 680 scsi_pkt buf uio scsi_cdb uscsi_cmd)) 681 _NOTE(SCHEME_PROTECTS_DATA("unique per pkt", scsi_extended_sense scsi_status)) 682 _NOTE(SCHEME_PROTECTS_DATA("unique per pkt", recov_info)) 683 _NOTE(SCHEME_PROTECTS_DATA("stable data", scsi_device)) 684 _NOTE(DATA_READABLE_WITHOUT_LOCK(st_drivetype scsi_address)) 685 #endif 686 687 /* 688 * autoconfiguration routines. 689 */ 690 char _depends_on[] = "misc/scsi"; 691 692 static struct modldrv modldrv = { 693 &mod_driverops, /* Type of module. This one is a driver */ 694 "SCSI tape Driver", /* Name of the module. */ 695 &st_ops /* driver ops */ 696 }; 697 698 static struct modlinkage modlinkage = { 699 MODREV_1, &modldrv, NULL 700 }; 701 702 /* 703 * Notes on Post Reset Behavior in the tape driver: 704 * 705 * When the tape drive is opened, the driver attempts to make sure that 706 * the tape head is positioned exactly where it was left when it was last 707 * closed provided the medium is not changed. If the tape drive is 708 * opened in O_NDELAY mode, the repositioning (if necessary for any loss 709 * of position due to reset) will happen when the first tape operation or 710 * I/O occurs. The repositioning (if required) may not be possible under 711 * certain situations such as when the device firmware not able to report 712 * the medium change in the REQUEST SENSE data because of a reset or a 713 * misbehaving bus not allowing the reposition to happen. In such 714 * extraordinary situations, where the driver fails to position the head 715 * at its original position, it will fail the open the first time, to 716 * save the applications from overwriting the data. All further attempts 717 * to open the tape device will result in the driver attempting to load 718 * the tape at BOT (beginning of tape). Also a warning message to 719 * indicate that further attempts to open the tape device may result in 720 * the tape being loaded at BOT will be printed on the console. If the 721 * tape device is opened in O_NDELAY mode, failure to restore the 722 * original tape head position, will result in the failure of the first 723 * tape operation or I/O, Further, the driver will invalidate its 724 * internal tape position which will necessitate the applications to 725 * validate the position by using either a tape positioning ioctl (such 726 * as MTREW) or closing and reopening the tape device. 727 * 728 */ 729 730 int 731 _init(void) 732 { 733 int e; 734 735 if (((e = ddi_soft_state_init(&st_state, 736 sizeof (struct scsi_tape), ST_MAXUNIT)) != 0)) { 737 return (e); 738 } 739 740 if ((e = mod_install(&modlinkage)) != 0) { 741 ddi_soft_state_fini(&st_state); 742 } else { 743 #ifdef STDEBUG 744 mutex_init(&st_debug_mutex, NULL, MUTEX_DRIVER, NULL); 745 #endif 746 747 #if defined(__x86) 748 /* set the max physical address for iob allocs on x86 */ 749 st_alloc_attr.dma_attr_addr_hi = st_max_phys_addr; 750 751 /* 752 * set the sgllen for iob allocs on x86. If this is set less 753 * than the number of pages the buffer will take 754 * (taking into account alignment), it would force the 755 * allocator to try and allocate contiguous pages. 756 */ 757 st_alloc_attr.dma_attr_sgllen = st_sgl_size; 758 #endif 759 } 760 761 return (e); 762 } 763 764 int 765 _fini(void) 766 { 767 int e; 768 769 if ((e = mod_remove(&modlinkage)) != 0) { 770 return (e); 771 } 772 773 #ifdef STDEBUG 774 mutex_destroy(&st_debug_mutex); 775 #endif 776 777 ddi_soft_state_fini(&st_state); 778 779 return (e); 780 } 781 782 int 783 _info(struct modinfo *modinfop) 784 { 785 return (mod_info(&modlinkage, modinfop)); 786 } 787 788 789 static int 790 st_probe(dev_info_t *devi) 791 { 792 int instance; 793 struct scsi_device *devp; 794 int rval; 795 796 #if !defined(__sparc) 797 char *tape_prop; 798 int tape_prop_len; 799 #endif 800 801 ST_ENTR(devi, st_probe); 802 803 /* If self identifying device */ 804 if (ddi_dev_is_sid(devi) == DDI_SUCCESS) { 805 return (DDI_PROBE_DONTCARE); 806 } 807 808 #if !defined(__sparc) 809 /* 810 * Since some x86 HBAs have devnodes that look like SCSI as 811 * far as we can tell but aren't really SCSI (DADK, like mlx) 812 * we check for the presence of the "tape" property. 813 */ 814 if (ddi_prop_op(DDI_DEV_T_NONE, devi, PROP_LEN_AND_VAL_ALLOC, 815 DDI_PROP_CANSLEEP, "tape", 816 (caddr_t)&tape_prop, &tape_prop_len) != DDI_PROP_SUCCESS) { 817 return (DDI_PROBE_FAILURE); 818 } 819 if (strncmp(tape_prop, "sctp", tape_prop_len) != 0) { 820 kmem_free(tape_prop, tape_prop_len); 821 return (DDI_PROBE_FAILURE); 822 } 823 kmem_free(tape_prop, tape_prop_len); 824 #endif 825 826 devp = ddi_get_driver_private(devi); 827 instance = ddi_get_instance(devi); 828 829 if (ddi_get_soft_state(st_state, instance) != NULL) { 830 return (DDI_PROBE_PARTIAL); 831 } 832 833 834 /* 835 * Turn around and call probe routine to see whether 836 * we actually have a tape at this SCSI nexus. 837 */ 838 if (scsi_probe(devp, NULL_FUNC) == SCSIPROBE_EXISTS) { 839 840 /* 841 * In checking the whole inq_dtype byte we are looking at both 842 * the Peripheral Qualifier and the Peripheral Device Type. 843 * For this driver we are only interested in sequential devices 844 * that are connected or capable if connecting to this logical 845 * unit. 846 */ 847 if (devp->sd_inq->inq_dtype == 848 (DTYPE_SEQUENTIAL | DPQ_POSSIBLE)) { 849 ST_DEBUG6(devi, st_label, SCSI_DEBUG, 850 "probe exists\n"); 851 rval = DDI_PROBE_SUCCESS; 852 } else { 853 rval = DDI_PROBE_FAILURE; 854 } 855 } else { 856 ST_DEBUG6(devi, st_label, SCSI_DEBUG, 857 "probe failure: nothing there\n"); 858 rval = DDI_PROBE_FAILURE; 859 } 860 scsi_unprobe(devp); 861 return (rval); 862 } 863 864 static int 865 st_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 866 { 867 int instance; 868 int wide; 869 int dev_instance; 870 int ret_status; 871 struct scsi_device *devp; 872 int node_ix; 873 struct scsi_tape *un; 874 875 ST_ENTR(devi, st_attach); 876 877 devp = ddi_get_driver_private(devi); 878 instance = ddi_get_instance(devi); 879 880 switch (cmd) { 881 case DDI_ATTACH: 882 if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 883 "tape-command-recovery-disable", 0) != 0) { 884 st_recov_sz = sizeof (pkt_info); 885 } 886 if (st_doattach(devp, SLEEP_FUNC) == DDI_FAILURE) { 887 return (DDI_FAILURE); 888 } 889 break; 890 case DDI_RESUME: 891 /* 892 * Suspend/Resume 893 * 894 * When the driver suspended, there might be 895 * outstanding cmds and therefore we need to 896 * reset the suspended flag and resume the scsi 897 * watch thread and restart commands and timeouts 898 */ 899 900 if (!(un = ddi_get_soft_state(st_state, instance))) { 901 return (DDI_FAILURE); 902 } 903 dev_instance = ((un->un_dev == 0) ? MTMINOR(instance) : 904 un->un_dev); 905 906 mutex_enter(ST_MUTEX); 907 908 un->un_throttle = un->un_max_throttle; 909 un->un_tids_at_suspend = 0; 910 un->un_pwr_mgmt = ST_PWR_NORMAL; 911 912 if (un->un_swr_token) { 913 scsi_watch_resume(un->un_swr_token); 914 } 915 916 /* 917 * Restart timeouts 918 */ 919 if ((un->un_tids_at_suspend & ST_DELAY_TID) != 0) { 920 mutex_exit(ST_MUTEX); 921 un->un_delay_tid = timeout( 922 st_delayed_cv_broadcast, un, 923 drv_usectohz((clock_t) 924 MEDIA_ACCESS_DELAY)); 925 mutex_enter(ST_MUTEX); 926 } 927 928 if (un->un_tids_at_suspend & ST_HIB_TID) { 929 mutex_exit(ST_MUTEX); 930 un->un_hib_tid = timeout(st_intr_restart, un, 931 ST_STATUS_BUSY_TIMEOUT); 932 mutex_enter(ST_MUTEX); 933 } 934 935 ret_status = st_clear_unit_attentions(dev_instance, 5); 936 937 /* 938 * now check if we need to restore the tape position 939 */ 940 if ((un->un_suspend_pos.pmode != invalid) && 941 ((un->un_suspend_pos.fileno > 0) || 942 (un->un_suspend_pos.blkno > 0)) || 943 (un->un_suspend_pos.lgclblkno > 0)) { 944 if (ret_status != 0) { 945 /* 946 * tape didn't get good TUR 947 * just print out error messages 948 */ 949 scsi_log(ST_DEVINFO, st_label, CE_WARN, 950 "st_attach-RESUME: tape failure " 951 " tape position will be lost"); 952 } else { 953 /* this prints errors */ 954 (void) st_validate_tapemarks(un, 955 st_uscsi_cmd, &un->un_suspend_pos); 956 } 957 /* 958 * there are no retries, if there is an error 959 * we don't know if the tape has changed 960 */ 961 un->un_suspend_pos.pmode = invalid; 962 } 963 964 /* now we are ready to start up any queued I/Os */ 965 if (un->un_ncmds || un->un_quef) { 966 st_start(un); 967 } 968 969 cv_broadcast(&un->un_suspend_cv); 970 mutex_exit(ST_MUTEX); 971 return (DDI_SUCCESS); 972 973 default: 974 return (DDI_FAILURE); 975 } 976 977 un = ddi_get_soft_state(st_state, instance); 978 979 ST_DEBUG(devi, st_label, SCSI_DEBUG, 980 "st_attach: instance=%x\n", instance); 981 982 /* 983 * Add a zero-length attribute to tell the world we support 984 * kernel ioctls (for layered drivers) 985 */ 986 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP, 987 DDI_KERNEL_IOCTL, NULL, 0); 988 989 ddi_report_dev((dev_info_t *)devi); 990 991 /* 992 * If it's a SCSI-2 tape drive which supports wide, 993 * tell the host adapter to use wide. 994 */ 995 wide = ((devp->sd_inq->inq_rdf == RDF_SCSI2) && 996 (devp->sd_inq->inq_wbus16 || devp->sd_inq->inq_wbus32)) ? 1 : 0; 997 998 if (scsi_ifsetcap(ROUTE, "wide-xfer", wide, 1) == 1) { 999 ST_DEBUG(devi, st_label, SCSI_DEBUG, 1000 "Wide Transfer %s\n", wide ? "enabled" : "disabled"); 1001 } 1002 1003 /* 1004 * enable autorequest sense; keep the rq packet around in case 1005 * the autorequest sense fails because of a busy condition 1006 * do a getcap first in case the capability is not variable 1007 */ 1008 if (scsi_ifgetcap(ROUTE, "auto-rqsense", 1) == 1) { 1009 un->un_arq_enabled = 1; 1010 } else { 1011 un->un_arq_enabled = 1012 ((scsi_ifsetcap(ROUTE, "auto-rqsense", 1, 1) == 1) ? 1 : 0); 1013 } 1014 1015 ST_DEBUG(devi, st_label, SCSI_DEBUG, "auto request sense %s\n", 1016 (un->un_arq_enabled ? "enabled" : "disabled")); 1017 1018 un->un_untagged_qing = 1019 (scsi_ifgetcap(ROUTE, "untagged-qing", 0) == 1); 1020 1021 /* 1022 * XXX - This is just for 2.6. to tell users that write buffering 1023 * has gone away. 1024 */ 1025 if (un->un_arq_enabled && un->un_untagged_qing) { 1026 if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 1027 "tape-driver-buffering", 0) != 0) { 1028 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 1029 "Write Data Buffering has been depricated. Your " 1030 "applications should continue to work normally.\n" 1031 " But, they should ported to use Asynchronous " 1032 " I/O\n" 1033 " For more information, read about " 1034 " tape-driver-buffering " 1035 "property in the st(7d) man page\n"); 1036 } 1037 } 1038 1039 un->un_max_throttle = un->un_throttle = un->un_last_throttle = 1; 1040 un->un_flush_on_errors = 0; 1041 un->un_mkr_pkt = (struct scsi_pkt *)NULL; 1042 1043 ST_DEBUG(devi, st_label, SCSI_DEBUG, 1044 "throttle=%x, max_throttle = %x\n", 1045 un->un_throttle, un->un_max_throttle); 1046 1047 /* initialize persistent errors to nil */ 1048 un->un_persistence = 0; 1049 un->un_persist_errors = 0; 1050 1051 /* 1052 * Get dma-max from HBA driver. If it is not defined, use 64k 1053 */ 1054 un->un_maxdma = scsi_ifgetcap(&devp->sd_address, "dma-max", 1); 1055 if (un->un_maxdma == -1) { 1056 ST_DEBUG(devi, st_label, SCSI_DEBUG, 1057 "Received a value that looked like -1. Using 64k maxdma"); 1058 un->un_maxdma = (64 * ONE_K); 1059 } 1060 1061 #ifdef __x86 1062 /* 1063 * for x86, the device may be able to DMA more than the system will 1064 * allow under some circumstances. We need account for both the HBA's 1065 * and system's contraints. 1066 * 1067 * Get the maximum DMA under worse case conditions. e.g. looking at the 1068 * device constraints, the max copy buffer size, and the worse case 1069 * fragmentation. NOTE: this may differ from dma-max since dma-max 1070 * doesn't take the worse case framentation into account. 1071 * 1072 * e.g. a device may be able to DMA 16MBytes, but can only DMA 1MByte 1073 * if none of the pages are contiguous. Keeping track of both of these 1074 * values allows us to support larger tape block sizes on some devices. 1075 */ 1076 un->un_maxdma_arch = scsi_ifgetcap(&devp->sd_address, "dma-max-arch", 1077 1); 1078 1079 /* 1080 * If the dma-max-arch capability is not implemented, or the value 1081 * comes back higher than what was reported in dma-max, use dma-max. 1082 */ 1083 if ((un->un_maxdma_arch == -1) || 1084 ((uint_t)un->un_maxdma < (uint_t)un->un_maxdma_arch)) { 1085 un->un_maxdma_arch = un->un_maxdma; 1086 } 1087 #endif 1088 1089 /* 1090 * Get the max allowable cdb size 1091 */ 1092 un->un_max_cdb_sz = 1093 scsi_ifgetcap(&devp->sd_address, "max-cdb-length", 1); 1094 if (un->un_max_cdb_sz < CDB_GROUP0) { 1095 ST_DEBUG(devi, st_label, SCSI_DEBUG, 1096 "HBA reported max-cdb-length as %d\n", un->un_max_cdb_sz); 1097 un->un_max_cdb_sz = CDB_GROUP4; /* optimistic default */ 1098 } 1099 1100 if (strcmp(ddi_driver_name(ddi_get_parent(ST_DEVINFO)), "scsi_vhci")) { 1101 un->un_multipath = 0; 1102 } else { 1103 un->un_multipath = 1; 1104 } 1105 1106 un->un_maxbsize = MAXBSIZE_UNKNOWN; 1107 1108 un->un_mediastate = MTIO_NONE; 1109 un->un_HeadClean = TAPE_ALERT_SUPPORT_UNKNOWN; 1110 1111 /* 1112 * initialize kstats 1113 */ 1114 un->un_stats = kstat_create("st", instance, NULL, "tape", 1115 KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT); 1116 if (un->un_stats) { 1117 un->un_stats->ks_lock = ST_MUTEX; 1118 kstat_install(un->un_stats); 1119 } 1120 (void) st_create_errstats(un, instance); 1121 1122 /* 1123 * find the drive type for this target 1124 */ 1125 mutex_enter(ST_MUTEX); 1126 un->un_dev = MTMINOR(instance); 1127 st_known_tape_type(un); 1128 un->un_dev = 0; 1129 mutex_exit(ST_MUTEX); 1130 1131 for (node_ix = 0; node_ix < ST_NUM_MEMBERS(st_minor_data); node_ix++) { 1132 int minor; 1133 char *name; 1134 1135 name = st_minor_data[node_ix].name; 1136 minor = st_minor_data[node_ix].minor; 1137 1138 /* 1139 * For default devices set the density to the 1140 * preferred default density for this device. 1141 */ 1142 if (node_ix <= DEF_BSD_NR) { 1143 minor |= un->un_dp->default_density; 1144 } 1145 minor |= MTMINOR(instance); 1146 1147 if (ddi_create_minor_node(devi, name, S_IFCHR, minor, 1148 DDI_NT_TAPE, NULL) == DDI_SUCCESS) { 1149 continue; 1150 } 1151 1152 ddi_remove_minor_node(devi, NULL); 1153 1154 (void) scsi_reset_notify(ROUTE, SCSI_RESET_CANCEL, 1155 st_reset_notification, (caddr_t)un); 1156 cv_destroy(&un->un_clscv); 1157 cv_destroy(&un->un_sbuf_cv); 1158 cv_destroy(&un->un_queue_cv); 1159 cv_destroy(&un->un_state_cv); 1160 #ifdef __x86 1161 cv_destroy(&un->un_contig_mem_cv); 1162 #endif 1163 cv_destroy(&un->un_suspend_cv); 1164 cv_destroy(&un->un_tape_busy_cv); 1165 cv_destroy(&un->un_recov_buf_cv); 1166 if (un->un_recov_taskq) { 1167 ddi_taskq_destroy(un->un_recov_taskq); 1168 } 1169 if (un->un_sbufp) { 1170 freerbuf(un->un_sbufp); 1171 } 1172 if (un->un_recov_buf) { 1173 freerbuf(un->un_recov_buf); 1174 } 1175 if (un->un_uscsi_rqs_buf) { 1176 kmem_free(un->un_uscsi_rqs_buf, SENSE_LENGTH); 1177 } 1178 if (un->un_mspl) { 1179 i_ddi_mem_free((caddr_t)un->un_mspl, NULL); 1180 } 1181 if (un->un_dp_size) { 1182 kmem_free(un->un_dp, un->un_dp_size); 1183 } 1184 if (un->un_state) { 1185 kstat_delete(un->un_stats); 1186 } 1187 if (un->un_errstats) { 1188 kstat_delete(un->un_errstats); 1189 } 1190 1191 scsi_destroy_pkt(un->un_rqs); 1192 scsi_free_consistent_buf(un->un_rqs_bp); 1193 ddi_soft_state_free(st_state, instance); 1194 devp->sd_private = NULL; 1195 devp->sd_sense = NULL; 1196 1197 ddi_prop_remove_all(devi); 1198 return (DDI_FAILURE); 1199 } 1200 1201 return (DDI_SUCCESS); 1202 } 1203 1204 /* 1205 * st_detach: 1206 * 1207 * we allow a detach if and only if: 1208 * - no tape is currently inserted 1209 * - tape position is at BOT or unknown 1210 * (if it is not at BOT then a no rewind 1211 * device was opened and we have to preserve state) 1212 * - it must be in a closed state : no timeouts or scsi_watch requests 1213 * will exist if it is closed, so we don't need to check for 1214 * them here. 1215 */ 1216 /*ARGSUSED*/ 1217 static int 1218 st_detach(dev_info_t *devi, ddi_detach_cmd_t cmd) 1219 { 1220 int instance; 1221 int result; 1222 struct scsi_device *devp; 1223 struct scsi_tape *un; 1224 clock_t wait_cmds_complete; 1225 1226 ST_ENTR(devi, st_detach); 1227 1228 instance = ddi_get_instance(devi); 1229 1230 if (!(un = ddi_get_soft_state(st_state, instance))) { 1231 return (DDI_FAILURE); 1232 } 1233 1234 mutex_enter(ST_MUTEX); 1235 1236 /* 1237 * Clear error entry stack 1238 */ 1239 st_empty_error_stack(un); 1240 1241 mutex_exit(ST_MUTEX); 1242 1243 switch (cmd) { 1244 1245 case DDI_DETACH: 1246 /* 1247 * Undo what we did in st_attach & st_doattach, 1248 * freeing resources and removing things we installed. 1249 * The system framework guarantees we are not active 1250 * with this devinfo node in any other entry points at 1251 * this time. 1252 */ 1253 1254 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 1255 "st_detach: instance=%x, un=%p\n", instance, 1256 (void *)un); 1257 1258 if (((un->un_dp->options & ST_UNLOADABLE) == 0) || 1259 ((un->un_rsvd_status & ST_APPLICATION_RESERVATIONS) != 0) || 1260 (un->un_ncmds != 0) || (un->un_quef != NULL) || 1261 (un->un_state != ST_STATE_CLOSED)) { 1262 /* 1263 * we cannot unload some targets because the 1264 * inquiry returns junk unless immediately 1265 * after a reset 1266 */ 1267 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 1268 "cannot unload instance %x\n", instance); 1269 un->un_unit_attention_flags |= 4; 1270 return (DDI_FAILURE); 1271 } 1272 1273 /* 1274 * if the tape has been removed then we may unload; 1275 * do a test unit ready and if it returns NOT READY 1276 * then we assume that it is safe to unload. 1277 * as a side effect, pmode may be set to invalid if the 1278 * the test unit ready fails; 1279 * also un_state may be set to non-closed, so reset it 1280 */ 1281 if ((un->un_dev) && /* Been opened since attach */ 1282 ((un->un_pos.pmode == legacy) && 1283 (un->un_pos.fileno > 0) || /* Known position not rewound */ 1284 (un->un_pos.blkno != 0)) || /* Or within first file */ 1285 ((un->un_pos.pmode == logical) && 1286 (un->un_pos.lgclblkno > 0))) { 1287 mutex_enter(ST_MUTEX); 1288 /* 1289 * Send Test Unit Ready in the hopes that if 1290 * the drive is not in the state we think it is. 1291 * And the state will be changed so it can be detached. 1292 * If the command fails to reach the device and 1293 * the drive was not rewound or unloaded we want 1294 * to fail the detach till a user command fails 1295 * where after the detach will succead. 1296 */ 1297 result = st_cmd(un, SCMD_TEST_UNIT_READY, 0, SYNC_CMD); 1298 /* 1299 * After TUR un_state may be set to non-closed, 1300 * so reset it back. 1301 */ 1302 un->un_state = ST_STATE_CLOSED; 1303 mutex_exit(ST_MUTEX); 1304 } 1305 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 1306 "un_status=%x, fileno=%x, blkno=%x\n", 1307 un->un_status, un->un_pos.fileno, un->un_pos.blkno); 1308 1309 /* 1310 * check again: 1311 * if we are not at BOT then it is not safe to unload 1312 */ 1313 if ((un->un_dev) && /* Been opened since attach */ 1314 (result != EACCES) && /* drive is use by somebody */ 1315 ((((un->un_pos.pmode == legacy) && 1316 (un->un_pos.fileno > 0) || /* Known position not rewound */ 1317 (un->un_pos.blkno != 0)) || /* Or within first file */ 1318 ((un->un_pos.pmode == logical) && 1319 (un->un_pos.lgclblkno > 0))) && 1320 ((un->un_state == ST_STATE_CLOSED) && 1321 (un->un_laststate == ST_STATE_CLOSING)))) { 1322 1323 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 1324 "cannot detach: pmode=%d fileno=0x%x, blkno=0x%x" 1325 " lgclblkno=0x%"PRIx64"\n", un->un_pos.pmode, 1326 un->un_pos.fileno, un->un_pos.blkno, 1327 un->un_pos.lgclblkno); 1328 un->un_unit_attention_flags |= 4; 1329 return (DDI_FAILURE); 1330 } 1331 1332 /* 1333 * Just To make sure that we have released the 1334 * tape unit . 1335 */ 1336 if (un->un_dev && (un->un_rsvd_status & ST_RESERVE) && 1337 !DEVI_IS_DEVICE_REMOVED(devi)) { 1338 mutex_enter(ST_MUTEX); 1339 (void) st_reserve_release(un, ST_RELEASE, st_uscsi_cmd); 1340 mutex_exit(ST_MUTEX); 1341 } 1342 1343 /* 1344 * now remove other data structures allocated in st_doattach() 1345 */ 1346 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 1347 "destroying/freeing\n"); 1348 1349 (void) scsi_reset_notify(ROUTE, SCSI_RESET_CANCEL, 1350 st_reset_notification, (caddr_t)un); 1351 cv_destroy(&un->un_clscv); 1352 cv_destroy(&un->un_sbuf_cv); 1353 cv_destroy(&un->un_queue_cv); 1354 cv_destroy(&un->un_suspend_cv); 1355 cv_destroy(&un->un_tape_busy_cv); 1356 cv_destroy(&un->un_recov_buf_cv); 1357 1358 if (un->un_recov_taskq) { 1359 ddi_taskq_destroy(un->un_recov_taskq); 1360 } 1361 1362 if (un->un_hib_tid) { 1363 (void) untimeout(un->un_hib_tid); 1364 un->un_hib_tid = 0; 1365 } 1366 1367 if (un->un_delay_tid) { 1368 (void) untimeout(un->un_delay_tid); 1369 un->un_delay_tid = 0; 1370 } 1371 cv_destroy(&un->un_state_cv); 1372 1373 #ifdef __x86 1374 cv_destroy(&un->un_contig_mem_cv); 1375 1376 if (un->un_contig_mem_hdl != NULL) { 1377 ddi_dma_free_handle(&un->un_contig_mem_hdl); 1378 } 1379 #endif 1380 if (un->un_sbufp) { 1381 freerbuf(un->un_sbufp); 1382 } 1383 if (un->un_recov_buf) { 1384 freerbuf(un->un_recov_buf); 1385 } 1386 if (un->un_uscsi_rqs_buf) { 1387 kmem_free(un->un_uscsi_rqs_buf, SENSE_LENGTH); 1388 } 1389 if (un->un_mspl) { 1390 i_ddi_mem_free((caddr_t)un->un_mspl, NULL); 1391 } 1392 if (un->un_rqs) { 1393 scsi_destroy_pkt(un->un_rqs); 1394 scsi_free_consistent_buf(un->un_rqs_bp); 1395 } 1396 if (un->un_mkr_pkt) { 1397 scsi_destroy_pkt(un->un_mkr_pkt); 1398 } 1399 if (un->un_arq_enabled) { 1400 (void) scsi_ifsetcap(ROUTE, "auto-rqsense", 0, 1); 1401 } 1402 if (un->un_dp_size) { 1403 kmem_free(un->un_dp, un->un_dp_size); 1404 } 1405 if (un->un_stats) { 1406 kstat_delete(un->un_stats); 1407 un->un_stats = (kstat_t *)0; 1408 } 1409 if (un->un_errstats) { 1410 kstat_delete(un->un_errstats); 1411 un->un_errstats = (kstat_t *)0; 1412 } 1413 if (un->un_media_id_len) { 1414 kmem_free(un->un_media_id, un->un_media_id_len); 1415 } 1416 devp = ST_SCSI_DEVP; 1417 ddi_soft_state_free(st_state, instance); 1418 devp->sd_private = NULL; 1419 devp->sd_sense = NULL; 1420 scsi_unprobe(devp); 1421 ddi_prop_remove_all(devi); 1422 ddi_remove_minor_node(devi, NULL); 1423 ST_DEBUG(0, st_label, SCSI_DEBUG, "st_detach done\n"); 1424 return (DDI_SUCCESS); 1425 1426 case DDI_SUSPEND: 1427 1428 /* 1429 * Suspend/Resume 1430 * 1431 * To process DDI_SUSPEND, we must do the following: 1432 * 1433 * - check ddi_removing_power to see if power will be turned 1434 * off. if so, return DDI_FAILURE 1435 * - check if we are already suspended, 1436 * if so, return DDI_FAILURE 1437 * - check if device state is CLOSED, 1438 * if not, return DDI_FAILURE. 1439 * - wait until outstanding operations complete 1440 * - save tape state 1441 * - block new operations 1442 * - cancel pending timeouts 1443 * 1444 */ 1445 1446 if (ddi_removing_power(devi)) { 1447 return (DDI_FAILURE); 1448 } 1449 1450 if (un->un_dev == 0) 1451 un->un_dev = MTMINOR(instance); 1452 1453 mutex_enter(ST_MUTEX); 1454 1455 /* 1456 * Shouldn't already be suspended, if so return failure 1457 */ 1458 if (un->un_pwr_mgmt == ST_PWR_SUSPENDED) { 1459 mutex_exit(ST_MUTEX); 1460 return (DDI_FAILURE); 1461 } 1462 if (un->un_state != ST_STATE_CLOSED) { 1463 mutex_exit(ST_MUTEX); 1464 return (DDI_FAILURE); 1465 } 1466 1467 /* 1468 * Wait for all outstanding I/O's to complete 1469 * 1470 * we wait on both ncmds and the wait queue for times 1471 * when we are flushing after persistent errors are 1472 * flagged, which is when ncmds can be 0, and the 1473 * queue can still have I/O's. This way we preserve 1474 * order of biodone's. 1475 */ 1476 wait_cmds_complete = ddi_get_lbolt(); 1477 wait_cmds_complete += 1478 st_wait_cmds_complete * drv_usectohz(1000000); 1479 while (un->un_ncmds || un->un_quef || 1480 (un->un_state == ST_STATE_RESOURCE_WAIT)) { 1481 1482 if (cv_timedwait(&un->un_tape_busy_cv, ST_MUTEX, 1483 wait_cmds_complete) == -1) { 1484 /* 1485 * Time expired then cancel the command 1486 */ 1487 if (st_reset(un, RESET_LUN) == 0) { 1488 if (un->un_last_throttle) { 1489 un->un_throttle = 1490 un->un_last_throttle; 1491 } 1492 mutex_exit(ST_MUTEX); 1493 return (DDI_FAILURE); 1494 } else { 1495 break; 1496 } 1497 } 1498 } 1499 1500 /* 1501 * DDI_SUSPEND says that the system "may" power down, we 1502 * remember the file and block number before rewinding. 1503 * we also need to save state before issuing 1504 * any WRITE_FILE_MARK command. 1505 */ 1506 (void) st_update_block_pos(un, st_cmd, 0); 1507 COPY_POS(&un->un_suspend_pos, &un->un_pos); 1508 1509 1510 /* 1511 * Issue a zero write file fmk command to tell the drive to 1512 * flush any buffered tape marks 1513 */ 1514 (void) st_cmd(un, SCMD_WRITE_FILE_MARK, 0, SYNC_CMD); 1515 1516 /* 1517 * Because not all tape drives correctly implement buffer 1518 * flushing with the zero write file fmk command, issue a 1519 * synchronous rewind command to force data flushing. 1520 * st_validate_tapemarks() will do a rewind during DDI_RESUME 1521 * anyway. 1522 */ 1523 (void) st_cmd(un, SCMD_REWIND, 0, SYNC_CMD); 1524 1525 /* stop any new operations */ 1526 un->un_pwr_mgmt = ST_PWR_SUSPENDED; 1527 un->un_throttle = 0; 1528 1529 /* 1530 * cancel any outstanding timeouts 1531 */ 1532 if (un->un_delay_tid) { 1533 timeout_id_t temp_id = un->un_delay_tid; 1534 un->un_delay_tid = 0; 1535 un->un_tids_at_suspend |= ST_DELAY_TID; 1536 mutex_exit(ST_MUTEX); 1537 (void) untimeout(temp_id); 1538 mutex_enter(ST_MUTEX); 1539 } 1540 1541 if (un->un_hib_tid) { 1542 timeout_id_t temp_id = un->un_hib_tid; 1543 un->un_hib_tid = 0; 1544 un->un_tids_at_suspend |= ST_HIB_TID; 1545 mutex_exit(ST_MUTEX); 1546 (void) untimeout(temp_id); 1547 mutex_enter(ST_MUTEX); 1548 } 1549 1550 /* 1551 * Suspend the scsi_watch_thread 1552 */ 1553 if (un->un_swr_token) { 1554 opaque_t temp_token = un->un_swr_token; 1555 mutex_exit(ST_MUTEX); 1556 scsi_watch_suspend(temp_token); 1557 } else { 1558 mutex_exit(ST_MUTEX); 1559 } 1560 1561 return (DDI_SUCCESS); 1562 1563 default: 1564 ST_DEBUG(0, st_label, SCSI_DEBUG, "st_detach failed\n"); 1565 return (DDI_FAILURE); 1566 } 1567 } 1568 1569 1570 /* ARGSUSED */ 1571 static int 1572 st_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 1573 { 1574 dev_t dev; 1575 struct scsi_tape *un; 1576 int instance, error; 1577 1578 ST_ENTR(dip, st_info); 1579 1580 switch (infocmd) { 1581 case DDI_INFO_DEVT2DEVINFO: 1582 dev = (dev_t)arg; 1583 instance = MTUNIT(dev); 1584 if ((un = ddi_get_soft_state(st_state, instance)) == NULL) 1585 return (DDI_FAILURE); 1586 *result = (void *) ST_DEVINFO; 1587 error = DDI_SUCCESS; 1588 break; 1589 case DDI_INFO_DEVT2INSTANCE: 1590 dev = (dev_t)arg; 1591 instance = MTUNIT(dev); 1592 *result = (void *)(uintptr_t)instance; 1593 error = DDI_SUCCESS; 1594 break; 1595 default: 1596 error = DDI_FAILURE; 1597 } 1598 return (error); 1599 } 1600 1601 static int 1602 st_doattach(struct scsi_device *devp, int (*canwait)()) 1603 { 1604 struct scsi_tape *un = NULL; 1605 recov_info *ri; 1606 int km_flags = (canwait != NULL_FUNC) ? KM_SLEEP : KM_NOSLEEP; 1607 int instance; 1608 size_t rlen; 1609 1610 ST_FUNC(devp->sd_dev, st_doattach); 1611 /* 1612 * Call the routine scsi_probe to do some of the dirty work. 1613 * If the INQUIRY command succeeds, the field sd_inq in the 1614 * device structure will be filled in. 1615 */ 1616 ST_DEBUG(devp->sd_dev, st_label, SCSI_DEBUG, 1617 "st_doattach(): probing\n"); 1618 1619 if (scsi_probe(devp, canwait) == SCSIPROBE_EXISTS) { 1620 1621 /* 1622 * In checking the whole inq_dtype byte we are looking at both 1623 * the Peripheral Qualifier and the Peripheral Device Type. 1624 * For this driver we are only interested in sequential devices 1625 * that are connected or capable if connecting to this logical 1626 * unit. 1627 */ 1628 if (devp->sd_inq->inq_dtype == 1629 (DTYPE_SEQUENTIAL | DPQ_POSSIBLE)) { 1630 ST_DEBUG(devp->sd_dev, st_label, SCSI_DEBUG, 1631 "probe exists\n"); 1632 } else { 1633 /* Something there but not a tape device */ 1634 scsi_unprobe(devp); 1635 return (DDI_FAILURE); 1636 } 1637 } else { 1638 /* Nothing there */ 1639 ST_DEBUG(devp->sd_dev, st_label, SCSI_DEBUG, 1640 "probe failure: nothing there\n"); 1641 scsi_unprobe(devp); 1642 return (DDI_FAILURE); 1643 } 1644 1645 1646 /* 1647 * The actual unit is present. 1648 * Now is the time to fill in the rest of our info.. 1649 */ 1650 instance = ddi_get_instance(devp->sd_dev); 1651 1652 if (ddi_soft_state_zalloc(st_state, instance) != DDI_SUCCESS) { 1653 goto error; 1654 } 1655 un = ddi_get_soft_state(st_state, instance); 1656 1657 ASSERT(un != NULL); 1658 1659 un->un_rqs_bp = scsi_alloc_consistent_buf(&devp->sd_address, NULL, 1660 MAX_SENSE_LENGTH, B_READ, canwait, NULL); 1661 if (un->un_rqs_bp == NULL) { 1662 goto error; 1663 } 1664 un->un_rqs = scsi_init_pkt(&devp->sd_address, NULL, un->un_rqs_bp, 1665 CDB_GROUP0, 1, st_recov_sz, PKT_CONSISTENT, canwait, NULL); 1666 if (!un->un_rqs) { 1667 goto error; 1668 } 1669 ASSERT(un->un_rqs->pkt_resid == 0); 1670 devp->sd_sense = 1671 (struct scsi_extended_sense *)un->un_rqs_bp->b_un.b_addr; 1672 ASSERT(geterror(un->un_rqs_bp) == NULL); 1673 1674 (void) scsi_setup_cdb((union scsi_cdb *)un->un_rqs->pkt_cdbp, 1675 SCMD_REQUEST_SENSE, 0, MAX_SENSE_LENGTH, 0); 1676 FILL_SCSI1_LUN(devp, un->un_rqs); 1677 un->un_rqs->pkt_flags |= (FLAG_SENSING | FLAG_HEAD | FLAG_NODISCON); 1678 un->un_rqs->pkt_time = st_io_time; 1679 un->un_rqs->pkt_comp = st_intr; 1680 ri = (recov_info *)un->un_rqs->pkt_private; 1681 if (st_recov_sz == sizeof (recov_info)) { 1682 ri->privatelen = sizeof (recov_info); 1683 } else { 1684 ri->privatelen = sizeof (pkt_info); 1685 } 1686 1687 un->un_sbufp = getrbuf(km_flags); 1688 un->un_recov_buf = getrbuf(km_flags); 1689 1690 un->un_uscsi_rqs_buf = kmem_alloc(SENSE_LENGTH, KM_SLEEP); 1691 1692 /* 1693 * use i_ddi_mem_alloc() for now until we have an interface to allocate 1694 * memory for DMA which doesn't require a DMA handle. ddi_iopb_alloc() 1695 * is obsolete and we want more flexibility in controlling the DMA 1696 * address constraints. 1697 */ 1698 (void) i_ddi_mem_alloc(devp->sd_dev, &st_alloc_attr, 1699 sizeof (struct seq_mode), ((km_flags == KM_SLEEP) ? 1 : 0), 0, 1700 NULL, (caddr_t *)&un->un_mspl, &rlen, NULL); 1701 1702 (void) i_ddi_mem_alloc(devp->sd_dev, &st_alloc_attr, 1703 sizeof (read_pos_data_t), ((km_flags == KM_SLEEP) ? 1 : 0), 0, 1704 NULL, (caddr_t *)&un->un_read_pos_data, &rlen, NULL); 1705 1706 if (!un->un_sbufp || !un->un_mspl || !un->un_read_pos_data) { 1707 ST_DEBUG6(devp->sd_dev, st_label, SCSI_DEBUG, 1708 "probe partial failure: no space\n"); 1709 goto error; 1710 } 1711 1712 bzero(un->un_mspl, sizeof (struct seq_mode)); 1713 1714 cv_init(&un->un_sbuf_cv, NULL, CV_DRIVER, NULL); 1715 cv_init(&un->un_queue_cv, NULL, CV_DRIVER, NULL); 1716 cv_init(&un->un_clscv, NULL, CV_DRIVER, NULL); 1717 cv_init(&un->un_state_cv, NULL, CV_DRIVER, NULL); 1718 #ifdef __x86 1719 cv_init(&un->un_contig_mem_cv, NULL, CV_DRIVER, NULL); 1720 #endif 1721 1722 /* Initialize power managemnet condition variable */ 1723 cv_init(&un->un_suspend_cv, NULL, CV_DRIVER, NULL); 1724 cv_init(&un->un_tape_busy_cv, NULL, CV_DRIVER, NULL); 1725 cv_init(&un->un_recov_buf_cv, NULL, CV_DRIVER, NULL); 1726 1727 un->un_recov_taskq = ddi_taskq_create(devp->sd_dev, 1728 "un_recov_taskq", 1, TASKQ_DEFAULTPRI, km_flags); 1729 1730 ASSERT(un->un_recov_taskq != NULL); 1731 1732 un->un_pos.pmode = invalid; 1733 un->un_sd = devp; 1734 un->un_swr_token = (opaque_t)NULL; 1735 un->un_comp_page = ST_DEV_DATACOMP_PAGE | ST_DEV_CONFIG_PAGE; 1736 un->un_wormable = st_is_drive_worm; 1737 un->un_media_id_method = st_get_media_identification; 1738 /* 1739 * setting long a initial as it contains logical file info. 1740 * support for long format is mandatory but many drive don't do it. 1741 */ 1742 un->un_read_pos_type = LONG_POS; 1743 1744 un->un_suspend_pos.pmode = invalid; 1745 1746 st_add_recovery_info_to_pkt(un, un->un_rqs_bp, un->un_rqs); 1747 1748 #ifdef __x86 1749 if (ddi_dma_alloc_handle(ST_DEVINFO, &st_contig_mem_dma_attr, 1750 DDI_DMA_SLEEP, NULL, &un->un_contig_mem_hdl) != DDI_SUCCESS) { 1751 ST_DEBUG6(devp->sd_dev, st_label, SCSI_DEBUG, 1752 "allocation of contiguous memory dma handle failed!"); 1753 un->un_contig_mem_hdl = NULL; 1754 goto error; 1755 } 1756 #endif 1757 1758 /* 1759 * Since this driver manages devices with "remote" hardware, 1760 * i.e. the devices themselves have no "reg" properties, 1761 * the SUSPEND/RESUME commands in detach/attach will not be 1762 * called by the power management framework unless we request 1763 * it by creating a "pm-hardware-state" property and setting it 1764 * to value "needs-suspend-resume". 1765 */ 1766 if (ddi_prop_update_string(DDI_DEV_T_NONE, devp->sd_dev, 1767 "pm-hardware-state", "needs-suspend-resume") != 1768 DDI_PROP_SUCCESS) { 1769 1770 ST_DEBUG(devp->sd_dev, st_label, SCSI_DEBUG, 1771 "ddi_prop_update(\"pm-hardware-state\") failed\n"); 1772 goto error; 1773 } 1774 1775 if (ddi_prop_create(DDI_DEV_T_NONE, devp->sd_dev, DDI_PROP_CANSLEEP, 1776 "no-involuntary-power-cycles", NULL, 0) != DDI_PROP_SUCCESS) { 1777 1778 ST_DEBUG(devp->sd_dev, st_label, SCSI_DEBUG, 1779 "ddi_prop_create(\"no-involuntary-power-cycles\") " 1780 "failed\n"); 1781 goto error; 1782 } 1783 1784 (void) scsi_reset_notify(ROUTE, SCSI_RESET_NOTIFY, 1785 st_reset_notification, (caddr_t)un); 1786 1787 ST_DEBUG6(devp->sd_dev, st_label, SCSI_DEBUG, "attach success\n"); 1788 return (DDI_SUCCESS); 1789 1790 error: 1791 devp->sd_sense = NULL; 1792 1793 ddi_remove_minor_node(devp->sd_dev, NULL); 1794 if (un) { 1795 if (un->un_mspl) { 1796 i_ddi_mem_free((caddr_t)un->un_mspl, NULL); 1797 } 1798 if (un->un_read_pos_data) { 1799 i_ddi_mem_free((caddr_t)un->un_read_pos_data, 0); 1800 } 1801 if (un->un_sbufp) { 1802 freerbuf(un->un_sbufp); 1803 } 1804 if (un->un_recov_buf) { 1805 freerbuf(un->un_recov_buf); 1806 } 1807 if (un->un_uscsi_rqs_buf) { 1808 kmem_free(un->un_uscsi_rqs_buf, SENSE_LENGTH); 1809 } 1810 #ifdef __x86 1811 if (un->un_contig_mem_hdl != NULL) { 1812 ddi_dma_free_handle(&un->un_contig_mem_hdl); 1813 } 1814 #endif 1815 if (un->un_rqs) { 1816 scsi_destroy_pkt(un->un_rqs); 1817 } 1818 1819 if (un->un_rqs_bp) { 1820 scsi_free_consistent_buf(un->un_rqs_bp); 1821 } 1822 1823 ddi_soft_state_free(st_state, instance); 1824 devp->sd_private = NULL; 1825 } 1826 1827 if (devp->sd_inq) { 1828 scsi_unprobe(devp); 1829 } 1830 return (DDI_FAILURE); 1831 } 1832 1833 typedef int 1834 (*cfg_functp)(struct scsi_tape *, char *vidpid, struct st_drivetype *); 1835 1836 static cfg_functp config_functs[] = { 1837 st_get_conf_from_st_dot_conf, 1838 st_get_conf_from_st_conf_dot_c, 1839 st_get_conf_from_tape_drive, 1840 st_get_default_conf 1841 }; 1842 1843 1844 /* 1845 * determine tape type, using tape-config-list or built-in table or 1846 * use a generic tape config entry 1847 */ 1848 static void 1849 st_known_tape_type(struct scsi_tape *un) 1850 { 1851 struct st_drivetype *dp; 1852 cfg_functp *config_funct; 1853 uchar_t reserved; 1854 1855 ST_FUNC(ST_DEVINFO, st_known_tape_type); 1856 1857 reserved = (un->un_rsvd_status & ST_RESERVE) ? ST_RESERVE 1858 : ST_RELEASE; 1859 1860 /* 1861 * XXX: Emulex MT-02 (and emulators) predates SCSI-1 and has 1862 * no vid & pid inquiry data. So, we provide one. 1863 */ 1864 if (ST_INQUIRY->inq_len == 0 || 1865 (bcmp("\0\0\0\0\0\0\0\0", ST_INQUIRY->inq_vid, 8) == 0)) { 1866 (void) strcpy((char *)ST_INQUIRY->inq_vid, ST_MT02_NAME); 1867 } 1868 1869 if (un->un_dp_size == 0) { 1870 un->un_dp_size = sizeof (struct st_drivetype); 1871 dp = kmem_zalloc((size_t)un->un_dp_size, KM_SLEEP); 1872 un->un_dp = dp; 1873 } else { 1874 dp = un->un_dp; 1875 } 1876 1877 un->un_dp->non_motion_timeout = st_io_time; 1878 /* 1879 * Loop through the configuration methods till one works. 1880 */ 1881 for (config_funct = &config_functs[0]; ; config_funct++) { 1882 if ((*config_funct)(un, ST_INQUIRY->inq_vid, dp)) { 1883 break; 1884 } 1885 } 1886 1887 /* 1888 * If we didn't just make up this configuration and 1889 * all the density codes are the same.. 1890 * Set Auto Density over ride. 1891 */ 1892 if (*config_funct != st_get_default_conf) { 1893 /* 1894 * If this device is one that is configured and all 1895 * densities are the same, This saves doing gets and set 1896 * that yield nothing. 1897 */ 1898 if ((dp->densities[0]) == (dp->densities[1]) && 1899 (dp->densities[0]) == (dp->densities[2]) && 1900 (dp->densities[0]) == (dp->densities[3])) { 1901 1902 dp->options |= ST_AUTODEN_OVERRIDE; 1903 } 1904 } 1905 1906 1907 /* 1908 * Store tape drive characteristics. 1909 */ 1910 un->un_status = 0; 1911 un->un_attached = 1; 1912 un->un_init_options = dp->options; 1913 1914 /* setup operation time-outs based on options */ 1915 st_calculate_timeouts(un); 1916 1917 /* TLR support */ 1918 if (un->un_dp->type != ST_TYPE_INVALID) { 1919 int result; 1920 1921 /* try and enable TLR */ 1922 un->un_tlr_flag = TLR_SAS_ONE_DEVICE; 1923 result = st_set_target_TLR_mode(un, st_uscsi_cmd); 1924 if (result == EACCES) { 1925 /* 1926 * From attach command failed. 1927 * Set dp type so is run again on open. 1928 */ 1929 un->un_dp->type = ST_TYPE_INVALID; 1930 un->un_tlr_flag = TLR_NOT_KNOWN; 1931 } else if (result == 0) { 1932 if (scsi_ifgetcap(&un->un_sd->sd_address, 1933 "tran-layer-retries", 1) == -1) { 1934 un->un_tlr_flag = TLR_NOT_SUPPORTED; 1935 (void) st_set_target_TLR_mode(un, st_uscsi_cmd); 1936 } else { 1937 un->un_tlr_flag = TLR_SAS_ONE_DEVICE; 1938 } 1939 } else { 1940 un->un_tlr_flag = TLR_NOT_SUPPORTED; 1941 } 1942 } 1943 1944 /* make sure if we are supposed to be variable, make it variable */ 1945 if (dp->options & ST_VARIABLE) { 1946 dp->bsize = 0; 1947 } 1948 1949 if (reserved != ((un->un_rsvd_status & ST_RESERVE) ? ST_RESERVE 1950 : ST_RELEASE)) { 1951 (void) st_reserve_release(un, reserved, st_uscsi_cmd); 1952 } 1953 1954 un->un_unit_attention_flags |= 1; 1955 1956 scsi_log(ST_DEVINFO, st_label, CE_NOTE, "?<%s>\n", dp->name); 1957 1958 } 1959 1960 1961 typedef struct { 1962 int mask; 1963 int bottom; 1964 int top; 1965 char *name; 1966 } conf_limit; 1967 1968 static const conf_limit conf_limits[] = { 1969 1970 -1, 1, 2, "conf version", 1971 -1, MT_ISTS, ST_LAST_TYPE, "drive type", 1972 -1, 0, 0xffffff, "block size", 1973 ST_VALID_OPTS, 0, ST_VALID_OPTS, "options", 1974 -1, 0, 4, "number of densities", 1975 -1, 0, UINT8_MAX, "density code", 1976 -1, 0, 3, "default density", 1977 -1, 0, UINT16_MAX, "non motion timeout", 1978 -1, 0, UINT16_MAX, "I/O timeout", 1979 -1, 0, UINT16_MAX, "space timeout", 1980 -1, 0, UINT16_MAX, "load timeout", 1981 -1, 0, UINT16_MAX, "unload timeout", 1982 -1, 0, UINT16_MAX, "erase timeout", 1983 0, 0, 0, NULL 1984 }; 1985 1986 static int 1987 st_validate_conf_data(struct scsi_tape *un, int *list, int list_len, 1988 const char *conf_name) 1989 { 1990 int dens; 1991 int ndens; 1992 int value; 1993 int type; 1994 int count; 1995 const conf_limit *limit = &conf_limits[0]; 1996 1997 ST_FUNC(ST_DEVINFO, st_validate_conf_data); 1998 1999 ST_DEBUG3(ST_DEVINFO, st_label, CE_NOTE, 2000 "Checking %d entrys total with %d densities\n", list_len, list[4]); 2001 2002 count = list_len; 2003 type = *list; 2004 for (; count && limit->name; count--, list++, limit++) { 2005 2006 value = *list; 2007 if (value & ~limit->mask) { 2008 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 2009 "%s %s value invalid bits set: 0x%X\n", 2010 conf_name, limit->name, value & ~limit->mask); 2011 *list &= limit->mask; 2012 } else if (value < limit->bottom) { 2013 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 2014 "%s %s value too low: value = %d limit %d\n", 2015 conf_name, limit->name, value, limit->bottom); 2016 } else if (value > limit->top) { 2017 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 2018 "%s %s value too high: value = %d limit %d\n", 2019 conf_name, limit->name, value, limit->top); 2020 } else { 2021 ST_DEBUG3(ST_DEVINFO, st_label, CE_CONT, 2022 "%s %s value = 0x%X\n", 2023 conf_name, limit->name, value); 2024 } 2025 2026 /* If not the number of densities continue */ 2027 if (limit != &conf_limits[4]) { 2028 continue; 2029 } 2030 2031 /* If number of densities is not in range can't use config */ 2032 if (value < limit->bottom || value > limit->top) { 2033 return (-1); 2034 } 2035 2036 ndens = min(value, NDENSITIES); 2037 if ((type == 1) && (list_len - ndens) != 6) { 2038 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 2039 "%s conf version 1 with %d densities has %d items" 2040 " should have %d", 2041 conf_name, ndens, list_len, 6 + ndens); 2042 } else if ((type == 2) && (list_len - ndens) != 13) { 2043 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 2044 "%s conf version 2 with %d densities has %d items" 2045 " should have %d", 2046 conf_name, ndens, list_len, 13 + ndens); 2047 } 2048 2049 limit++; 2050 for (dens = 0; dens < ndens && count; dens++) { 2051 count--; 2052 list++; 2053 value = *list; 2054 if (value < limit->bottom) { 2055 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 2056 "%s density[%d] value too low: value =" 2057 " 0x%X limit 0x%X\n", 2058 conf_name, dens, value, limit->bottom); 2059 } else if (value > limit->top) { 2060 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 2061 "%s density[%d] value too high: value =" 2062 " 0x%X limit 0x%X\n", 2063 conf_name, dens, value, limit->top); 2064 } else { 2065 ST_DEBUG3(ST_DEVINFO, st_label, CE_CONT, 2066 "%s density[%d] value = 0x%X\n", 2067 conf_name, dens, value); 2068 } 2069 } 2070 } 2071 2072 return (0); 2073 } 2074 2075 static int 2076 st_get_conf_from_st_dot_conf(struct scsi_tape *un, char *vidpid, 2077 struct st_drivetype *dp) 2078 { 2079 caddr_t config_list = NULL; 2080 caddr_t data_list = NULL; 2081 int *data_ptr; 2082 caddr_t vidptr, prettyptr, datanameptr; 2083 size_t vidlen, prettylen, datanamelen, tripletlen = 0; 2084 int config_list_len, data_list_len, len, i; 2085 int version; 2086 int found = 0; 2087 2088 ST_FUNC(ST_DEVINFO, st_get_conf_from_st_dot_conf); 2089 2090 /* 2091 * Determine type of tape controller. Type is determined by 2092 * checking the vendor ids of the earlier inquiry command and 2093 * comparing those with vids in tape-config-list defined in st.conf 2094 */ 2095 if (ddi_getlongprop(DDI_DEV_T_ANY, ST_DEVINFO, DDI_PROP_DONTPASS, 2096 "tape-config-list", (caddr_t)&config_list, &config_list_len) 2097 != DDI_PROP_SUCCESS) { 2098 return (found); 2099 } 2100 2101 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 2102 "st_get_conf_from_st_dot_conf(): st.conf has tape-config-list\n"); 2103 2104 /* 2105 * Compare vids in each triplet - if it matches, get value for 2106 * data_name and contruct a st_drivetype struct 2107 * tripletlen is not set yet! 2108 */ 2109 for (len = config_list_len, vidptr = config_list; 2110 len > 0; 2111 vidptr += tripletlen, len -= tripletlen) { 2112 2113 vidlen = strlen(vidptr); 2114 prettyptr = vidptr + vidlen + 1; 2115 prettylen = strlen(prettyptr); 2116 datanameptr = prettyptr + prettylen + 1; 2117 datanamelen = strlen(datanameptr); 2118 tripletlen = vidlen + prettylen + datanamelen + 3; 2119 2120 if (vidlen == 0) { 2121 continue; 2122 } 2123 2124 /* 2125 * If inquiry vid dosen't match this triplets vid, 2126 * try the next. 2127 */ 2128 if (strncasecmp(vidpid, vidptr, vidlen)) { 2129 continue; 2130 } 2131 2132 /* 2133 * if prettylen is zero then use the vid string 2134 */ 2135 if (prettylen == 0) { 2136 prettyptr = vidptr; 2137 prettylen = vidlen; 2138 } 2139 2140 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 2141 "vid = %s, pretty=%s, dataname = %s\n", 2142 vidptr, prettyptr, datanameptr); 2143 2144 /* 2145 * get the data list 2146 */ 2147 if (ddi_getlongprop(DDI_DEV_T_ANY, ST_DEVINFO, 0, 2148 datanameptr, (caddr_t)&data_list, 2149 &data_list_len) != DDI_PROP_SUCCESS) { 2150 /* 2151 * Error in getting property value 2152 * print warning! 2153 */ 2154 scsi_log(ST_DEVINFO, st_label, CE_WARN, 2155 "data property (%s) has no value\n", 2156 datanameptr); 2157 continue; 2158 } 2159 2160 /* 2161 * now initialize the st_drivetype struct 2162 */ 2163 (void) strncpy(dp->name, prettyptr, ST_NAMESIZE - 1); 2164 dp->length = (int)min(vidlen, (VIDPIDLEN - 1)); 2165 (void) strncpy(dp->vid, vidptr, dp->length); 2166 data_ptr = (int *)data_list; 2167 /* 2168 * check if data is enough for version, type, 2169 * bsize, options, # of densities, density1, 2170 * density2, ..., default_density 2171 */ 2172 if ((data_list_len < 5 * sizeof (int)) || 2173 (data_list_len < 6 * sizeof (int) + 2174 *(data_ptr + 4) * sizeof (int))) { 2175 /* 2176 * print warning and skip to next triplet. 2177 */ 2178 scsi_log(ST_DEVINFO, st_label, CE_WARN, 2179 "data property (%s) incomplete\n", 2180 datanameptr); 2181 kmem_free(data_list, data_list_len); 2182 continue; 2183 } 2184 2185 if (st_validate_conf_data(un, data_ptr, 2186 data_list_len / sizeof (int), datanameptr)) { 2187 kmem_free(data_list, data_list_len); 2188 scsi_log(ST_DEVINFO, st_label, CE_WARN, 2189 "data property (%s) rejected\n", 2190 datanameptr); 2191 continue; 2192 } 2193 2194 /* 2195 * check version 2196 */ 2197 version = *data_ptr++; 2198 if (version != 1 && version != 2) { 2199 /* print warning but accept it */ 2200 scsi_log(ST_DEVINFO, st_label, CE_WARN, 2201 "Version # for data property (%s) " 2202 "not set to 1 or 2\n", datanameptr); 2203 } 2204 2205 dp->type = *data_ptr++; 2206 dp->bsize = *data_ptr++; 2207 dp->options = *data_ptr++; 2208 dp->options |= ST_DYNAMIC; 2209 len = *data_ptr++; 2210 for (i = 0; i < NDENSITIES; i++) { 2211 if (i < len) { 2212 dp->densities[i] = *data_ptr++; 2213 } 2214 } 2215 dp->default_density = *data_ptr << 3; 2216 if (version == 2 && 2217 data_list_len >= (13 + len) * sizeof (int)) { 2218 data_ptr++; 2219 dp->non_motion_timeout = *data_ptr++; 2220 dp->io_timeout = *data_ptr++; 2221 dp->rewind_timeout = *data_ptr++; 2222 dp->space_timeout = *data_ptr++; 2223 dp->load_timeout = *data_ptr++; 2224 dp->unload_timeout = *data_ptr++; 2225 dp->erase_timeout = *data_ptr++; 2226 } 2227 kmem_free(data_list, data_list_len); 2228 found = 1; 2229 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 2230 "found in st.conf: vid = %s, pretty=%s\n", 2231 dp->vid, dp->name); 2232 break; 2233 } 2234 2235 /* 2236 * free up the memory allocated by ddi_getlongprop 2237 */ 2238 if (config_list) { 2239 kmem_free(config_list, config_list_len); 2240 } 2241 return (found); 2242 } 2243 2244 static int 2245 st_get_conf_from_st_conf_dot_c(struct scsi_tape *un, char *vidpid, 2246 struct st_drivetype *dp) 2247 { 2248 int i; 2249 2250 ST_FUNC(ST_DEVINFO, st_get_conf_from_st_conf_dot_c); 2251 /* 2252 * Determine type of tape controller. Type is determined by 2253 * checking the result of the earlier inquiry command and 2254 * comparing vendor ids with strings in a table declared in st_conf.c. 2255 */ 2256 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2257 "st_get_conf_from_st_conf_dot_c(): looking at st_drivetypes\n"); 2258 2259 for (i = 0; i < st_ndrivetypes; i++) { 2260 if (st_drivetypes[i].length == 0) { 2261 continue; 2262 } 2263 if (strncasecmp(vidpid, st_drivetypes[i].vid, 2264 st_drivetypes[i].length)) { 2265 continue; 2266 } 2267 bcopy(&st_drivetypes[i], dp, sizeof (st_drivetypes[i])); 2268 return (1); 2269 } 2270 return (0); 2271 } 2272 2273 static int 2274 st_get_conf_from_tape_drive(struct scsi_tape *un, char *vidpid, 2275 struct st_drivetype *dp) 2276 { 2277 int bsize; 2278 ulong_t maxbsize; 2279 caddr_t buf; 2280 struct st_drivetype *tem_dp; 2281 struct read_blklim *blklim; 2282 int rval; 2283 int i; 2284 2285 ST_FUNC(ST_DEVINFO, st_get_conf_from_tape_drive); 2286 2287 /* 2288 * Determine the type of tape controller. Type is determined by 2289 * sending SCSI commands to tape drive and deriving the type from 2290 * the returned data. 2291 */ 2292 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2293 "st_get_conf_from_tape_drive(): asking tape drive\n"); 2294 2295 tem_dp = kmem_zalloc(sizeof (struct st_drivetype), KM_SLEEP); 2296 2297 /* 2298 * Make up a name 2299 */ 2300 bcopy(vidpid, tem_dp->name, VIDPIDLEN); 2301 tem_dp->name[VIDPIDLEN] = '\0'; 2302 tem_dp->length = min(strlen(ST_INQUIRY->inq_vid), (VIDPIDLEN - 1)); 2303 (void) strncpy(tem_dp->vid, ST_INQUIRY->inq_vid, tem_dp->length); 2304 /* 2305 * 'clean' vendor and product strings of non-printing chars 2306 */ 2307 for (i = 0; i < VIDPIDLEN - 1; i ++) { 2308 if (tem_dp->name[i] < ' ' || tem_dp->name[i] > '~') { 2309 tem_dp->name[i] = '.'; 2310 } 2311 } 2312 2313 /* 2314 * MODE SENSE to determine block size. 2315 */ 2316 un->un_dp->options |= ST_MODE_SEL_COMP | ST_UNLOADABLE; 2317 rval = st_modesense(un); 2318 if (rval) { 2319 if (rval == EACCES) { 2320 un->un_dp->type = ST_TYPE_INVALID; 2321 rval = 1; 2322 } else { 2323 un->un_dp->options &= ~ST_MODE_SEL_COMP; 2324 rval = 0; 2325 } 2326 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2327 "st_get_conf_from_tape_drive(): fail to mode sense\n"); 2328 goto exit; 2329 } 2330 2331 /* Can mode sense page 0x10 or 0xf */ 2332 tem_dp->options |= ST_MODE_SEL_COMP; 2333 bsize = (un->un_mspl->high_bl << 16) | 2334 (un->un_mspl->mid_bl << 8) | 2335 (un->un_mspl->low_bl); 2336 2337 if (bsize == 0) { 2338 tem_dp->options |= ST_VARIABLE; 2339 tem_dp->bsize = 0; 2340 } else if (bsize > ST_MAXRECSIZE_FIXED) { 2341 rval = st_change_block_size(un, 0); 2342 if (rval) { 2343 if (rval == EACCES) { 2344 un->un_dp->type = ST_TYPE_INVALID; 2345 rval = 1; 2346 } else { 2347 rval = 0; 2348 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2349 "st_get_conf_from_tape_drive(): " 2350 "Fixed record size is too large and" 2351 "cannot switch to variable record size"); 2352 } 2353 goto exit; 2354 } 2355 tem_dp->options |= ST_VARIABLE; 2356 } else { 2357 rval = st_change_block_size(un, 0); 2358 if (rval == 0) { 2359 tem_dp->options |= ST_VARIABLE; 2360 tem_dp->bsize = 0; 2361 } else if (rval != EACCES) { 2362 tem_dp->bsize = bsize; 2363 } else { 2364 un->un_dp->type = ST_TYPE_INVALID; 2365 rval = 1; 2366 goto exit; 2367 } 2368 } 2369 2370 /* 2371 * If READ BLOCk LIMITS works and upper block size limit is 2372 * more than 64K, ST_NO_RECSIZE_LIMIT is supported. 2373 */ 2374 blklim = kmem_zalloc(sizeof (struct read_blklim), KM_SLEEP); 2375 rval = st_read_block_limits(un, blklim); 2376 if (rval) { 2377 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2378 "st_get_conf_from_tape_drive(): " 2379 "fail to read block limits.\n"); 2380 rval = 0; 2381 kmem_free(blklim, sizeof (struct read_blklim)); 2382 goto exit; 2383 } 2384 maxbsize = (blklim->max_hi << 16) + 2385 (blklim->max_mid << 8) + blklim->max_lo; 2386 if (maxbsize > ST_MAXRECSIZE_VARIABLE) { 2387 tem_dp->options |= ST_NO_RECSIZE_LIMIT; 2388 } 2389 kmem_free(blklim, sizeof (struct read_blklim)); 2390 2391 /* 2392 * Inquiry VPD page 0xb0 to see if the tape drive supports WORM 2393 */ 2394 buf = kmem_zalloc(6, KM_SLEEP); 2395 rval = st_get_special_inquiry(un, 6, buf, 0xb0); 2396 if (rval) { 2397 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2398 "st_get_conf_from_tape_drive(): " 2399 "fail to read vitial inquiry.\n"); 2400 rval = 0; 2401 kmem_free(buf, 6); 2402 goto exit; 2403 } 2404 if (buf[4] & 1) { 2405 tem_dp->options |= ST_WORMABLE; 2406 } 2407 kmem_free(buf, 6); 2408 2409 /* Assume BSD BSR KNOWS_EOD */ 2410 tem_dp->options |= ST_BSF | ST_BSR | ST_KNOWS_EOD | ST_UNLOADABLE; 2411 tem_dp->max_rretries = -1; 2412 tem_dp->max_wretries = -1; 2413 2414 /* 2415 * Decide the densities supported by tape drive by sending 2416 * REPORT DENSITY SUPPORT command. 2417 */ 2418 if (st_get_densities_from_tape_drive(un, tem_dp) == 0) { 2419 goto exit; 2420 } 2421 2422 /* 2423 * Decide the timeout values for several commands by sending 2424 * REPORT SUPPORTED OPERATION CODES command. 2425 */ 2426 rval = st_get_timeout_values_from_tape_drive(un, tem_dp); 2427 if (rval == 0 || ((rval == 1) && (tem_dp->type == ST_TYPE_INVALID))) { 2428 goto exit; 2429 } 2430 2431 bcopy(tem_dp, dp, sizeof (struct st_drivetype)); 2432 rval = 1; 2433 2434 exit: 2435 un->un_status = KEY_NO_SENSE; 2436 kmem_free(tem_dp, sizeof (struct st_drivetype)); 2437 return (rval); 2438 } 2439 2440 static int 2441 st_get_densities_from_tape_drive(struct scsi_tape *un, 2442 struct st_drivetype *dp) 2443 { 2444 int i, p; 2445 size_t buflen; 2446 ushort_t des_len; 2447 uchar_t *den_header; 2448 uchar_t num_den; 2449 uchar_t den[NDENSITIES]; 2450 uchar_t deflt[NDENSITIES]; 2451 struct report_density_desc *den_desc; 2452 2453 ST_FUNC(ST_DEVINFO, st_get_densities_from_type_drive); 2454 2455 /* 2456 * Since we have no idea how many densitiy support entries 2457 * will be returned, we send the command firstly assuming 2458 * there is only one. Then we can decide the number of 2459 * entries by available density support length. If multiple 2460 * entries exist, we will resend the command with enough 2461 * buffer size. 2462 */ 2463 buflen = sizeof (struct report_density_header) + 2464 sizeof (struct report_density_desc); 2465 den_header = kmem_zalloc(buflen, KM_SLEEP); 2466 if (st_report_density_support(un, den_header, buflen) != 0) { 2467 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2468 "st_get_conf_from_tape_drive(): fail to report density.\n"); 2469 kmem_free(den_header, buflen); 2470 return (0); 2471 } 2472 des_len = 2473 BE_16(((struct report_density_header *)den_header)->ava_dens_len); 2474 num_den = (des_len - 2) / sizeof (struct report_density_desc); 2475 2476 if (num_den > 1) { 2477 kmem_free(den_header, buflen); 2478 buflen = sizeof (struct report_density_header) + 2479 sizeof (struct report_density_desc) * num_den; 2480 den_header = kmem_zalloc(buflen, KM_SLEEP); 2481 if (st_report_density_support(un, den_header, buflen) != 0) { 2482 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2483 "st_get_conf_from_tape_drive(): " 2484 "fail to report density.\n"); 2485 kmem_free(den_header, buflen); 2486 return (0); 2487 } 2488 } 2489 2490 den_desc = (struct report_density_desc *)(den_header 2491 + sizeof (struct report_density_header)); 2492 2493 /* 2494 * Decide the drive type by assigning organization 2495 */ 2496 for (i = 0; i < ST_NUM_MEMBERS(st_vid_dt); i ++) { 2497 if (strncmp(st_vid_dt[i].vid, (char *)(den_desc->ass_org), 2498 8) == 0) { 2499 dp->type = st_vid_dt[i].type; 2500 break; 2501 } 2502 } 2503 if (i == ST_NUM_MEMBERS(st_vid_dt)) { 2504 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2505 "st_get_conf_from_tape_drive(): " 2506 "can't find match of assigned ort.\n"); 2507 kmem_free(den_header, buflen); 2508 return (0); 2509 } 2510 2511 /* 2512 * The tape drive may support many tape formats, but the st driver 2513 * supports only the four highest densities. Since density code 2514 * values are returned by ascending sequence, we start from the 2515 * last entry of density support data block descriptor. 2516 */ 2517 p = 0; 2518 den_desc += num_den - 1; 2519 for (i = 0; i < num_den && p < NDENSITIES; i ++, den_desc --) { 2520 if ((den_desc->pri_den != 0) && (den_desc->wrtok)) { 2521 if (p != 0) { 2522 if (den_desc->pri_den >= den[p - 1]) { 2523 continue; 2524 } 2525 } 2526 den[p] = den_desc->pri_den; 2527 deflt[p] = den_desc->deflt; 2528 p ++; 2529 } 2530 } 2531 2532 switch (p) { 2533 case 0: 2534 bzero(dp->densities, NDENSITIES); 2535 dp->options |= ST_AUTODEN_OVERRIDE; 2536 dp->default_density = MT_DENSITY4; 2537 break; 2538 2539 case 1: 2540 (void) memset(dp->densities, den[0], NDENSITIES); 2541 dp->options |= ST_AUTODEN_OVERRIDE; 2542 dp->default_density = MT_DENSITY4; 2543 break; 2544 2545 case 2: 2546 dp->densities[0] = den[1]; 2547 dp->densities[1] = den[1]; 2548 dp->densities[2] = den[0]; 2549 dp->densities[3] = den[0]; 2550 if (deflt[0]) { 2551 dp->default_density = MT_DENSITY4; 2552 } else { 2553 dp->default_density = MT_DENSITY2; 2554 } 2555 break; 2556 2557 case 3: 2558 dp->densities[0] = den[2]; 2559 dp->densities[1] = den[1]; 2560 dp->densities[2] = den[0]; 2561 dp->densities[3] = den[0]; 2562 if (deflt[0]) { 2563 dp->default_density = MT_DENSITY4; 2564 } else if (deflt[1]) { 2565 dp->default_density = MT_DENSITY2; 2566 } else { 2567 dp->default_density = MT_DENSITY1; 2568 } 2569 break; 2570 2571 default: 2572 for (i = p; i > p - NDENSITIES; i --) { 2573 dp->densities[i - 1] = den[p - i]; 2574 } 2575 if (deflt[0]) { 2576 dp->default_density = MT_DENSITY4; 2577 } else if (deflt[1]) { 2578 dp->default_density = MT_DENSITY3; 2579 } else if (deflt[2]) { 2580 dp->default_density = MT_DENSITY2; 2581 } else { 2582 dp->default_density = MT_DENSITY1; 2583 } 2584 break; 2585 } 2586 2587 bzero(dp->mediatype, NDENSITIES); 2588 2589 kmem_free(den_header, buflen); 2590 return (1); 2591 } 2592 2593 static int 2594 st_get_timeout_values_from_tape_drive(struct scsi_tape *un, 2595 struct st_drivetype *dp) 2596 { 2597 ushort_t timeout; 2598 int rval; 2599 2600 ST_FUNC(ST_DEVINFO, st_get_timeout_values_from_type_drive); 2601 2602 rval = st_get_timeouts_value(un, SCMD_ERASE, &timeout, 0); 2603 if (rval) { 2604 if (rval == EACCES) { 2605 un->un_dp->type = ST_TYPE_INVALID; 2606 dp->type = ST_TYPE_INVALID; 2607 return (1); 2608 } 2609 return (0); 2610 } 2611 dp->erase_timeout = timeout; 2612 2613 rval = st_get_timeouts_value(un, SCMD_READ, &timeout, 0); 2614 if (rval) { 2615 if (rval == EACCES) { 2616 un->un_dp->type = ST_TYPE_INVALID; 2617 dp->type = ST_TYPE_INVALID; 2618 return (1); 2619 } 2620 return (0); 2621 } 2622 dp->io_timeout = timeout; 2623 2624 rval = st_get_timeouts_value(un, SCMD_WRITE, &timeout, 0); 2625 if (rval) { 2626 if (rval == EACCES) { 2627 un->un_dp->type = ST_TYPE_INVALID; 2628 dp->type = ST_TYPE_INVALID; 2629 return (1); 2630 } 2631 return (0); 2632 } 2633 dp->io_timeout = max(dp->io_timeout, timeout); 2634 2635 rval = st_get_timeouts_value(un, SCMD_SPACE, &timeout, 0); 2636 if (rval) { 2637 if (rval == EACCES) { 2638 un->un_dp->type = ST_TYPE_INVALID; 2639 dp->type = ST_TYPE_INVALID; 2640 return (1); 2641 } 2642 return (0); 2643 } 2644 dp->space_timeout = timeout; 2645 2646 rval = st_get_timeouts_value(un, SCMD_LOAD, &timeout, 0); 2647 if (rval) { 2648 if (rval == EACCES) { 2649 un->un_dp->type = ST_TYPE_INVALID; 2650 dp->type = ST_TYPE_INVALID; 2651 return (1); 2652 } 2653 return (0); 2654 } 2655 dp->load_timeout = timeout; 2656 dp->unload_timeout = timeout; 2657 2658 rval = st_get_timeouts_value(un, SCMD_REWIND, &timeout, 0); 2659 if (rval) { 2660 if (rval == EACCES) { 2661 un->un_dp->type = ST_TYPE_INVALID; 2662 dp->type = ST_TYPE_INVALID; 2663 return (1); 2664 } 2665 return (0); 2666 } 2667 dp->rewind_timeout = timeout; 2668 2669 rval = st_get_timeouts_value(un, SCMD_INQUIRY, &timeout, 0); 2670 if (rval) { 2671 if (rval == EACCES) { 2672 un->un_dp->type = ST_TYPE_INVALID; 2673 dp->type = ST_TYPE_INVALID; 2674 return (1); 2675 } 2676 return (0); 2677 } 2678 dp->non_motion_timeout = timeout; 2679 2680 return (1); 2681 } 2682 2683 static int 2684 st_get_timeouts_value(struct scsi_tape *un, uchar_t option_code, 2685 ushort_t *timeout_value, ushort_t service_action) 2686 { 2687 uchar_t *timeouts; 2688 uchar_t *oper; 2689 uchar_t support; 2690 uchar_t cdbsize; 2691 uchar_t ctdp; 2692 size_t buflen; 2693 int rval; 2694 2695 ST_FUNC(ST_DEVINFO, st_get_timeouts_value); 2696 2697 buflen = sizeof (struct one_com_des) + 2698 sizeof (struct com_timeout_des); 2699 oper = kmem_zalloc(buflen, KM_SLEEP); 2700 rval = st_report_supported_operation(un, oper, option_code, 2701 service_action); 2702 2703 if (rval) { 2704 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2705 "st_get_timeouts_value(): " 2706 "fail to timeouts value for command %d.\n", option_code); 2707 kmem_free(oper, buflen); 2708 return (rval); 2709 } 2710 2711 support = ((struct one_com_des *)oper)->support; 2712 if ((support != SUPPORT_VALUES_SUPPORT_SCSI) && 2713 (support != SUPPORT_VALUES_SUPPORT_VENDOR)) { 2714 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2715 "st_get_timeouts_value(): " 2716 "command %d is not supported.\n", option_code); 2717 kmem_free(oper, buflen); 2718 return (ENOTSUP); 2719 } 2720 2721 ctdp = ((struct one_com_des *)oper)->ctdp; 2722 if (!ctdp) { 2723 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2724 "st_get_timeouts_value(): " 2725 "command timeout is not included.\n"); 2726 kmem_free(oper, buflen); 2727 return (ENOTSUP); 2728 } 2729 2730 cdbsize = BE_16(((struct one_com_des *)oper)->cdb_size); 2731 timeouts = (uchar_t *)(oper + cdbsize + 4); 2732 2733 /* 2734 * Timeout value in seconds is 4 bytes, but we only support the lower 2 2735 * bytes. If the higher 2 bytes are not zero, the timeout value is set 2736 * to 0xFFFF. 2737 */ 2738 if (*(timeouts + 8) != 0 || *(timeouts + 9) != 0) { 2739 *timeout_value = USHRT_MAX; 2740 } else { 2741 *timeout_value = ((*(timeouts + 10)) << 8) | 2742 (*(timeouts + 11)); 2743 } 2744 2745 kmem_free(oper, buflen); 2746 return (0); 2747 } 2748 2749 static int 2750 st_get_default_conf(struct scsi_tape *un, char *vidpid, struct st_drivetype *dp) 2751 { 2752 int i; 2753 2754 ST_FUNC(ST_DEVINFO, st_get_default_conf); 2755 2756 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 2757 "st_get_default_conf(): making drivetype from INQ cmd\n"); 2758 2759 /* 2760 * Make up a name 2761 */ 2762 bcopy("Vendor '", dp->name, 8); 2763 bcopy(vidpid, &dp->name[8], VIDLEN); 2764 bcopy("' Product '", &dp->name[16], 11); 2765 bcopy(&vidpid[8], &dp->name[27], PIDLEN); 2766 dp->name[ST_NAMESIZE - 2] = '\''; 2767 dp->name[ST_NAMESIZE - 1] = '\0'; 2768 dp->length = min(strlen(ST_INQUIRY->inq_vid), (VIDPIDLEN - 1)); 2769 (void) strncpy(dp->vid, ST_INQUIRY->inq_vid, dp->length); 2770 /* 2771 * 'clean' vendor and product strings of non-printing chars 2772 */ 2773 for (i = 0; i < ST_NAMESIZE - 2; i++) { 2774 if (dp->name[i] < ' ' || dp->name[i] > '~') { 2775 dp->name[i] = '.'; 2776 } 2777 } 2778 dp->type = ST_TYPE_INVALID; 2779 dp->options |= (ST_DYNAMIC | ST_UNLOADABLE | ST_MODE_SEL_COMP); 2780 2781 return (1); /* Can Not Fail */ 2782 } 2783 2784 /* 2785 * Regular Unix Entry points 2786 */ 2787 2788 2789 2790 /* ARGSUSED */ 2791 static int 2792 st_open(dev_t *dev_p, int flag, int otyp, cred_t *cred_p) 2793 { 2794 dev_t dev = *dev_p; 2795 int rval = 0; 2796 2797 GET_SOFT_STATE(dev); 2798 2799 ST_ENTR(ST_DEVINFO, st_open); 2800 2801 /* 2802 * validate that we are addressing a sensible unit 2803 */ 2804 mutex_enter(ST_MUTEX); 2805 2806 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 2807 "st_open(node = %s dev = 0x%lx, flag = %d, otyp = %d)\n", 2808 st_dev_name(dev), *dev_p, flag, otyp); 2809 2810 /* 2811 * All device accesss go thru st_strategy() where we check 2812 * suspend status 2813 */ 2814 2815 if (!un->un_attached) { 2816 st_known_tape_type(un); 2817 if (!un->un_attached) { 2818 rval = ENXIO; 2819 goto exit; 2820 } 2821 2822 } 2823 2824 /* 2825 * Check for the case of the tape in the middle of closing. 2826 * This isn't simply a check of the current state, because 2827 * we could be in state of sensing with the previous state 2828 * that of closing. 2829 * 2830 * And don't allow multiple opens. 2831 */ 2832 if (!(flag & (FNDELAY | FNONBLOCK)) && IS_CLOSING(un)) { 2833 un->un_laststate = un->un_state; 2834 un->un_state = ST_STATE_CLOSE_PENDING_OPEN; 2835 while (IS_CLOSING(un) || 2836 un->un_state == ST_STATE_CLOSE_PENDING_OPEN) { 2837 if (cv_wait_sig(&un->un_clscv, ST_MUTEX) == 0) { 2838 rval = EINTR; 2839 un->un_state = un->un_laststate; 2840 goto exit; 2841 } 2842 } 2843 } else if (un->un_state != ST_STATE_CLOSED) { 2844 rval = EBUSY; 2845 goto busy; 2846 } 2847 2848 /* 2849 * record current dev 2850 */ 2851 un->un_dev = dev; 2852 un->un_oflags = flag; /* save for use in st_tape_init() */ 2853 un->un_errno = 0; /* no errors yet */ 2854 un->un_restore_pos = 0; 2855 un->un_rqs_state = 0; 2856 2857 /* 2858 * If we are opening O_NDELAY, or O_NONBLOCK, we don't check for 2859 * anything, leave internal states alone, if fileno >= 0 2860 */ 2861 if (flag & (FNDELAY | FNONBLOCK)) { 2862 switch (un->un_pos.pmode) { 2863 2864 case invalid: 2865 un->un_state = ST_STATE_OFFLINE; 2866 break; 2867 2868 case legacy: 2869 /* 2870 * If position is anything other than rewound. 2871 */ 2872 if (un->un_pos.fileno != 0 || un->un_pos.blkno != 0) { 2873 /* 2874 * set un_read_only/write-protect status. 2875 * 2876 * If the tape is not bot we can assume 2877 * that mspl->wp_status is set properly. 2878 * else 2879 * we need to do a mode sense/Tur once 2880 * again to get the actual tape status.(since 2881 * user might have replaced the tape) 2882 * Hence make the st state OFFLINE so that 2883 * we re-intialize the tape once again. 2884 */ 2885 un->un_read_only = 2886 (un->un_oflags & FWRITE) ? RDWR : RDONLY; 2887 un->un_state = ST_STATE_OPEN_PENDING_IO; 2888 } else { 2889 un->un_state = ST_STATE_OFFLINE; 2890 } 2891 break; 2892 case logical: 2893 if (un->un_pos.lgclblkno == 0) { 2894 un->un_state = ST_STATE_OFFLINE; 2895 } else { 2896 un->un_read_only = 2897 (un->un_oflags & FWRITE) ? RDWR : RDONLY; 2898 un->un_state = ST_STATE_OPEN_PENDING_IO; 2899 } 2900 break; 2901 } 2902 rval = 0; 2903 } else { 2904 /* 2905 * Not opening O_NDELAY. 2906 */ 2907 un->un_state = ST_STATE_OPENING; 2908 2909 /* 2910 * Clear error entry stack 2911 */ 2912 st_empty_error_stack(un); 2913 2914 rval = st_tape_init(un); 2915 if ((rval == EACCES) && (un->un_read_only & WORM)) { 2916 un->un_state = ST_STATE_OPEN_PENDING_IO; 2917 rval = 0; /* so open doesn't fail */ 2918 } else if (rval) { 2919 /* 2920 * Release the tape unit, if reserved and not 2921 * preserve reserve. 2922 */ 2923 if ((un->un_rsvd_status & 2924 (ST_RESERVE | ST_PRESERVE_RESERVE)) == ST_RESERVE) { 2925 (void) st_reserve_release(un, ST_RELEASE, 2926 st_uscsi_cmd); 2927 } 2928 } else { 2929 un->un_state = ST_STATE_OPEN_PENDING_IO; 2930 } 2931 } 2932 2933 exit: 2934 /* 2935 * we don't want any uninvited guests scrogging our data when we're 2936 * busy with something, so for successful opens or failed opens 2937 * (except for EBUSY), reset these counters and state appropriately. 2938 */ 2939 if (rval != EBUSY) { 2940 if (rval) { 2941 un->un_state = ST_STATE_CLOSED; 2942 } 2943 un->un_err_resid = 0; 2944 un->un_retry_ct = 0; 2945 } 2946 busy: 2947 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 2948 "st_open: return val = %x, state = %d\n", rval, un->un_state); 2949 mutex_exit(ST_MUTEX); 2950 return (rval); 2951 2952 } 2953 2954 static int 2955 st_tape_init(struct scsi_tape *un) 2956 { 2957 int err; 2958 int rval = 0; 2959 2960 ST_FUNC(ST_DEVINFO, st_tape_init); 2961 2962 ASSERT(mutex_owned(ST_MUTEX)); 2963 2964 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 2965 "st_tape_init(un = 0x%p, oflags = %d)\n", (void*)un, un->un_oflags); 2966 2967 /* 2968 * Clean up after any errors left by 'last' close. 2969 * This also handles the case of the initial open. 2970 */ 2971 if (un->un_state != ST_STATE_INITIALIZING) { 2972 un->un_laststate = un->un_state; 2973 un->un_state = ST_STATE_OPENING; 2974 } 2975 2976 un->un_kbytes_xferred = 0; 2977 2978 /* 2979 * do a throw away TUR to clear check condition 2980 */ 2981 err = st_cmd(un, SCMD_TEST_UNIT_READY, 0, SYNC_CMD); 2982 2983 /* 2984 * If test unit ready fails because the drive is reserved 2985 * by another host fail the open for no access. 2986 */ 2987 if (err) { 2988 if (un->un_rsvd_status & ST_RESERVATION_CONFLICT) { 2989 un->un_state = ST_STATE_CLOSED; 2990 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 2991 "st_tape_init: RESERVATION CONFLICT\n"); 2992 rval = EACCES; 2993 goto exit; 2994 } else if ((un->un_rsvd_status & 2995 ST_APPLICATION_RESERVATIONS) != 0) { 2996 if ((ST_RQSENSE != NULL) && 2997 (ST_RQSENSE->es_add_code == 0x2a && 2998 ST_RQSENSE->es_qual_code == 0x03)) { 2999 un->un_state = ST_STATE_CLOSED; 3000 rval = EACCES; 3001 goto exit; 3002 } 3003 } 3004 } 3005 3006 /* 3007 * Tape self identification could fail if the tape drive is used by 3008 * another host during attach time. We try to get the tape type 3009 * again. This is also applied to any posponed configuration methods. 3010 */ 3011 if (un->un_dp->type == ST_TYPE_INVALID) { 3012 un->un_comp_page = ST_DEV_DATACOMP_PAGE | ST_DEV_CONFIG_PAGE; 3013 st_known_tape_type(un); 3014 } 3015 3016 /* 3017 * If the tape type is still invalid, try to determine the generic 3018 * configuration. 3019 */ 3020 if (un->un_dp->type == ST_TYPE_INVALID) { 3021 rval = st_determine_generic(un); 3022 if (rval) { 3023 if (rval != EACCES) { 3024 rval = EIO; 3025 } 3026 un->un_state = ST_STATE_CLOSED; 3027 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 3028 "st_tape_init: %s invalid type\n", 3029 rval == EACCES ? "EACCES" : "EIO"); 3030 goto exit; 3031 } 3032 /* 3033 * If this is a Unknown Type drive, 3034 * Use the READ BLOCK LIMITS to determine if 3035 * allow large xfer is approprate if not globally 3036 * disabled with st_allow_large_xfer. 3037 */ 3038 un->un_allow_large_xfer = (uchar_t)st_allow_large_xfer; 3039 } else { 3040 3041 /* 3042 * If we allow_large_xfer (ie >64k) and have not yet found out 3043 * the max block size supported by the drive, 3044 * find it by issueing a READ_BLKLIM command. 3045 * if READ_BLKLIM cmd fails, assume drive doesn't 3046 * allow_large_xfer and min/max block sizes as 1 byte and 63k. 3047 */ 3048 un->un_allow_large_xfer = st_allow_large_xfer && 3049 (un->un_dp->options & ST_NO_RECSIZE_LIMIT); 3050 } 3051 /* 3052 * if maxbsize is unknown, set the maximum block size. 3053 */ 3054 if (un->un_maxbsize == MAXBSIZE_UNKNOWN) { 3055 3056 /* 3057 * Get the Block limits of the tape drive. 3058 * if un->un_allow_large_xfer = 0 , then make sure 3059 * that maxbsize is <= ST_MAXRECSIZE_FIXED. 3060 */ 3061 un->un_rbl = kmem_zalloc(RBLSIZE, KM_SLEEP); 3062 3063 err = st_cmd(un, SCMD_READ_BLKLIM, RBLSIZE, SYNC_CMD); 3064 if (err) { 3065 /* Retry */ 3066 err = st_cmd(un, SCMD_READ_BLKLIM, RBLSIZE, SYNC_CMD); 3067 } 3068 if (!err) { 3069 3070 /* 3071 * if cmd successful, use limit returned 3072 */ 3073 un->un_maxbsize = (un->un_rbl->max_hi << 16) + 3074 (un->un_rbl->max_mid << 8) + 3075 un->un_rbl->max_lo; 3076 un->un_minbsize = (un->un_rbl->min_hi << 8) + 3077 un->un_rbl->min_lo; 3078 un->un_data_mod = 1 << un->un_rbl->granularity; 3079 if ((un->un_maxbsize == 0) || 3080 (un->un_allow_large_xfer == 0 && 3081 un->un_maxbsize > ST_MAXRECSIZE_FIXED)) { 3082 un->un_maxbsize = ST_MAXRECSIZE_FIXED; 3083 3084 } else if (un->un_dp->type == ST_TYPE_DEFAULT) { 3085 /* 3086 * Drive is not one that is configured, But the 3087 * READ BLOCK LIMITS tells us it can do large 3088 * xfers. 3089 */ 3090 if (un->un_maxbsize > ST_MAXRECSIZE_FIXED) { 3091 un->un_dp->options |= 3092 ST_NO_RECSIZE_LIMIT; 3093 } 3094 /* 3095 * If max and mimimum block limits are the 3096 * same this is a fixed block size device. 3097 */ 3098 if (un->un_maxbsize == un->un_minbsize) { 3099 un->un_dp->options &= ~ST_VARIABLE; 3100 } 3101 } 3102 3103 if (un->un_minbsize == 0) { 3104 un->un_minbsize = 1; 3105 } 3106 3107 } else { /* error on read block limits */ 3108 3109 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 3110 "!st_tape_init: Error on READ BLOCK LIMITS," 3111 " errno = %d un_rsvd_status = 0x%X\n", 3112 err, un->un_rsvd_status); 3113 3114 /* 3115 * since read block limits cmd failed, 3116 * do not allow large xfers. 3117 * use old values in st_minphys 3118 */ 3119 if (un->un_rsvd_status & ST_RESERVATION_CONFLICT) { 3120 rval = EACCES; 3121 } else { 3122 un->un_allow_large_xfer = 0; 3123 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 3124 "!Disabling large transfers\n"); 3125 3126 /* 3127 * we guess maxbsize and minbsize 3128 */ 3129 if (un->un_bsize) { 3130 un->un_maxbsize = un->un_minbsize = 3131 un->un_bsize; 3132 } else { 3133 un->un_maxbsize = ST_MAXRECSIZE_FIXED; 3134 un->un_minbsize = 1; 3135 } 3136 /* 3137 * Data Mod must be set, 3138 * Even if read block limits fails. 3139 * Prevents Divide By Zero in st_rw(). 3140 */ 3141 un->un_data_mod = 1; 3142 } 3143 } 3144 if (un->un_rbl) { 3145 kmem_free(un->un_rbl, RBLSIZE); 3146 un->un_rbl = NULL; 3147 } 3148 3149 if (rval) { 3150 goto exit; 3151 } 3152 } 3153 3154 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 3155 "maxdma = %d, maxbsize = %d, minbsize = %d, %s large xfer\n", 3156 un->un_maxdma, un->un_maxbsize, un->un_minbsize, 3157 (un->un_allow_large_xfer ? "ALLOW": "DON'T ALLOW")); 3158 3159 err = st_cmd(un, SCMD_TEST_UNIT_READY, 0, SYNC_CMD); 3160 3161 if (err != 0) { 3162 if (err == EINTR) { 3163 un->un_laststate = un->un_state; 3164 un->un_state = ST_STATE_CLOSED; 3165 rval = EINTR; 3166 goto exit; 3167 } 3168 /* 3169 * Make sure the tape is ready 3170 */ 3171 un->un_pos.pmode = invalid; 3172 if (un->un_status != KEY_UNIT_ATTENTION) { 3173 /* 3174 * allow open no media. Subsequent MTIOCSTATE 3175 * with media present will complete the open 3176 * logic. 3177 */ 3178 un->un_laststate = un->un_state; 3179 if (un->un_oflags & (FNONBLOCK|FNDELAY)) { 3180 un->un_mediastate = MTIO_EJECTED; 3181 un->un_state = ST_STATE_OFFLINE; 3182 rval = 0; 3183 goto exit; 3184 } else { 3185 un->un_state = ST_STATE_CLOSED; 3186 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 3187 "st_tape_init EIO no media, not opened " 3188 "O_NONBLOCK|O_EXCL\n"); 3189 rval = EIO; 3190 goto exit; 3191 } 3192 } 3193 } 3194 3195 /* 3196 * On each open, initialize block size from drivetype struct, 3197 * as it could have been changed by MTSRSZ ioctl. 3198 * Now, ST_VARIABLE simply means drive is capable of variable 3199 * mode. All drives are assumed to support fixed records. 3200 * Hence, un_bsize tells what mode the drive is in. 3201 * un_bsize = 0 - variable record length 3202 * = x - fixed record length is x 3203 */ 3204 un->un_bsize = un->un_dp->bsize; 3205 3206 /* 3207 * If saved position is valid go there 3208 */ 3209 if (un->un_restore_pos) { 3210 un->un_restore_pos = 0; 3211 un->un_pos.fileno = un->un_save_fileno; 3212 un->un_pos.blkno = un->un_save_blkno; 3213 rval = st_validate_tapemarks(un, st_uscsi_cmd, &un->un_pos); 3214 if (rval != 0) { 3215 if (rval != EACCES) { 3216 rval = EIO; 3217 } 3218 un->un_laststate = un->un_state; 3219 un->un_state = ST_STATE_CLOSED; 3220 goto exit; 3221 } 3222 } 3223 3224 if (un->un_pos.pmode == invalid) { 3225 rval = st_loadtape(un); 3226 if (rval) { 3227 if (rval != EACCES) { 3228 rval = EIO; 3229 } 3230 un->un_laststate = un->un_state; 3231 un->un_state = ST_STATE_CLOSED; 3232 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 3233 "st_tape_init: %s can't open tape\n", 3234 rval == EACCES ? "EACCES" : "EIO"); 3235 goto exit; 3236 } 3237 } 3238 3239 /* 3240 * do a mode sense to pick up state of current write-protect, 3241 * Could cause reserve and fail due to conflict. 3242 */ 3243 if (un->un_unit_attention_flags) { 3244 rval = st_modesense(un); 3245 if (rval == EACCES) { 3246 goto exit; 3247 } 3248 } 3249 3250 /* 3251 * If we are opening the tape for writing, check 3252 * to make sure that the tape can be written. 3253 */ 3254 if (un->un_oflags & FWRITE) { 3255 err = 0; 3256 if (un->un_mspl->wp) { 3257 un->un_status = KEY_WRITE_PROTECT; 3258 un->un_laststate = un->un_state; 3259 un->un_state = ST_STATE_CLOSED; 3260 rval = EACCES; 3261 /* 3262 * STK sets the wp bit if volsafe tape is loaded. 3263 */ 3264 if ((un->un_dp->type == MT_ISSTK9840) && 3265 (un->un_dp->options & ST_WORMABLE)) { 3266 un->un_read_only = RDONLY; 3267 } else { 3268 goto exit; 3269 } 3270 } else { 3271 un->un_read_only = RDWR; 3272 } 3273 } else { 3274 un->un_read_only = RDONLY; 3275 } 3276 3277 if (un->un_dp->options & ST_WORMABLE && 3278 un->un_unit_attention_flags) { 3279 un->un_read_only |= un->un_wormable(un); 3280 3281 if (((un->un_read_only == WORM) || 3282 (un->un_read_only == RDWORM)) && 3283 ((un->un_oflags & FWRITE) == FWRITE)) { 3284 un->un_status = KEY_DATA_PROTECT; 3285 rval = EACCES; 3286 ST_DEBUG4(ST_DEVINFO, st_label, CE_NOTE, 3287 "read_only = %d eof = %d oflag = %d\n", 3288 un->un_read_only, un->un_pos.eof, un->un_oflags); 3289 } 3290 } 3291 3292 /* 3293 * If we're opening the tape write-only, we need to 3294 * write 2 filemarks on the HP 1/2 inch drive, to 3295 * create a null file. 3296 */ 3297 if ((un->un_read_only == RDWR) || 3298 (un->un_read_only == WORM) && (un->un_oflags & FWRITE)) { 3299 if (un->un_dp->options & ST_REEL) { 3300 un->un_fmneeded = 2; 3301 } else { 3302 un->un_fmneeded = 1; 3303 } 3304 } else { 3305 un->un_fmneeded = 0; 3306 } 3307 3308 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 3309 "fmneeded = %x\n", un->un_fmneeded); 3310 3311 /* 3312 * Make sure the density can be selected correctly. 3313 * If WORM can only write at the append point which in most cases 3314 * isn't BOP. st_determine_density() with a B_WRITE only attempts 3315 * to set and try densities if a BOP. 3316 */ 3317 if (st_determine_density(un, 3318 un->un_read_only == RDWR ? B_WRITE : B_READ)) { 3319 un->un_status = KEY_ILLEGAL_REQUEST; 3320 un->un_laststate = un->un_state; 3321 un->un_state = ST_STATE_CLOSED; 3322 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 3323 "st_tape_init: EIO can't determine density\n"); 3324 rval = EIO; 3325 goto exit; 3326 } 3327 3328 /* 3329 * Destroy the knowledge that we have 'determined' 3330 * density so that a later read at BOT comes along 3331 * does the right density determination. 3332 */ 3333 3334 un->un_density_known = 0; 3335 3336 3337 /* 3338 * Okay, the tape is loaded and either at BOT or somewhere past. 3339 * Mark the state such that any I/O or tape space operations 3340 * will get/set the right density, etc.. 3341 */ 3342 un->un_laststate = un->un_state; 3343 un->un_lastop = ST_OP_NIL; 3344 un->un_mediastate = MTIO_INSERTED; 3345 cv_broadcast(&un->un_state_cv); 3346 3347 /* 3348 * Set test append flag if writing. 3349 * First write must check that tape is positioned correctly. 3350 */ 3351 un->un_test_append = (un->un_oflags & FWRITE); 3352 3353 /* 3354 * if there are pending unit attention flags. 3355 * Check that the media has not changed. 3356 */ 3357 if (un->un_unit_attention_flags) { 3358 rval = st_get_media_identification(un, st_uscsi_cmd); 3359 if (rval != 0 && rval != EACCES) { 3360 rval = EIO; 3361 } 3362 un->un_unit_attention_flags = 0; 3363 } 3364 3365 exit: 3366 un->un_err_resid = 0; 3367 un->un_last_resid = 0; 3368 un->un_last_count = 0; 3369 3370 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 3371 "st_tape_init: return val = %x\n", rval); 3372 return (rval); 3373 3374 } 3375 3376 3377 3378 /* ARGSUSED */ 3379 static int 3380 st_close(dev_t dev, int flag, int otyp, cred_t *cred_p) 3381 { 3382 int err = 0; 3383 int count, last_state; 3384 minor_t minor = getminor(dev); 3385 #ifdef __x86 3386 struct contig_mem *cp, *cp_temp; 3387 #endif 3388 3389 GET_SOFT_STATE(dev); 3390 3391 ST_ENTR(ST_DEVINFO, st_close); 3392 3393 /* 3394 * wait till all cmds in the pipeline have been completed 3395 */ 3396 mutex_enter(ST_MUTEX); 3397 3398 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 3399 "st_close(dev = 0x%lx, flag = %d, otyp = %d)\n", dev, flag, otyp); 3400 3401 st_wait_for_io(un); 3402 3403 /* turn off persistent errors on close, as we want close to succeed */ 3404 st_turn_pe_off(un); 3405 3406 /* 3407 * set state to indicate that we are in process of closing 3408 */ 3409 last_state = un->un_laststate = un->un_state; 3410 un->un_state = ST_STATE_CLOSING; 3411 3412 ST_POS(ST_DEVINFO, "st_close1:", &un->un_pos); 3413 3414 /* 3415 * BSD behavior: 3416 * a close always causes a silent span to the next file if we've hit 3417 * an EOF (but not yet read across it). 3418 */ 3419 if ((minor & MT_BSD) && (un->un_pos.eof == ST_EOF)) { 3420 if (un->un_pos.pmode != invalid) { 3421 un->un_pos.fileno++; 3422 un->un_pos.blkno = 0; 3423 } 3424 un->un_pos.eof = ST_NO_EOF; 3425 } 3426 3427 /* 3428 * SVR4 behavior for skipping to next file: 3429 * 3430 * If we have not seen a filemark, space to the next file 3431 * 3432 * If we have already seen the filemark we are physically in the next 3433 * file and we only increment the filenumber 3434 */ 3435 if (((minor & (MT_BSD | MT_NOREWIND)) == MT_NOREWIND) && 3436 (flag & FREAD) && /* reading or at least asked to */ 3437 (un->un_mediastate == MTIO_INSERTED) && /* tape loaded */ 3438 (un->un_pos.pmode != invalid) && /* XXX position known */ 3439 ((un->un_pos.blkno != 0) && /* inside a file */ 3440 (un->un_lastop != ST_OP_WRITE) && /* Didn't just write */ 3441 (un->un_lastop != ST_OP_WEOF))) { /* or write filemarks */ 3442 switch (un->un_pos.eof) { 3443 case ST_NO_EOF: 3444 /* 3445 * if we were reading and did not read the complete file 3446 * skip to the next file, leaving the tape correctly 3447 * positioned to read the first record of the next file 3448 * Check first for REEL if we are at EOT by trying to 3449 * read a block 3450 */ 3451 if ((un->un_dp->options & ST_REEL) && 3452 (!(un->un_dp->options & ST_READ_IGNORE_EOFS)) && 3453 (un->un_pos.blkno == 0)) { 3454 if (st_cmd(un, SCMD_SPACE, Blk(1), SYNC_CMD)) { 3455 ST_DEBUG2(ST_DEVINFO, st_label, 3456 SCSI_DEBUG, 3457 "st_close : EIO can't space\n"); 3458 err = EIO; 3459 goto error_out; 3460 } 3461 if (un->un_pos.eof >= ST_EOF_PENDING) { 3462 un->un_pos.eof = ST_EOT_PENDING; 3463 un->un_pos.fileno += 1; 3464 un->un_pos.blkno = 0; 3465 break; 3466 } 3467 } 3468 if (st_cmd(un, SCMD_SPACE, Fmk(1), SYNC_CMD)) { 3469 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 3470 "st_close: EIO can't space #2\n"); 3471 err = EIO; 3472 goto error_out; 3473 } else { 3474 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 3475 "st_close2: fileno=%x,blkno=%x,eof=%x\n", 3476 un->un_pos.fileno, un->un_pos.blkno, 3477 un->un_pos.eof); 3478 un->un_pos.eof = ST_NO_EOF; 3479 } 3480 break; 3481 3482 case ST_EOF_PENDING: 3483 case ST_EOF: 3484 un->un_pos.fileno += 1; 3485 un->un_pos.lgclblkno += 1; 3486 un->un_pos.blkno = 0; 3487 un->un_pos.eof = ST_NO_EOF; 3488 break; 3489 3490 case ST_EOT: 3491 case ST_EOT_PENDING: 3492 case ST_EOM: 3493 /* nothing to do */ 3494 break; 3495 default: 3496 ST_DEBUG(ST_DEVINFO, st_label, CE_PANIC, 3497 "Undefined state 0x%x", un->un_pos.eof); 3498 3499 } 3500 } 3501 3502 3503 /* 3504 * For performance reasons (HP 88780), the driver should 3505 * postpone writing the second tape mark until just before a file 3506 * positioning ioctl is issued (e.g., rewind). This means that 3507 * the user must not manually rewind the tape because the tape will 3508 * be missing the second tape mark which marks EOM. 3509 * However, this small performance improvement is not worth the risk. 3510 */ 3511 3512 /* 3513 * We need to back up over the filemark we inadvertently popped 3514 * over doing a read in between the two filemarks that constitute 3515 * logical eot for 1/2" tapes. Note that ST_EOT_PENDING is only 3516 * set while reading. 3517 * 3518 * If we happen to be at physical eot (ST_EOM) (writing case), 3519 * the writing of filemark(s) will clear the ST_EOM state, which 3520 * we don't want, so we save this state and restore it later. 3521 */ 3522 3523 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 3524 "flag=%x, fmneeded=%x, lastop=%x, eof=%x\n", 3525 flag, un->un_fmneeded, un->un_lastop, un->un_pos.eof); 3526 3527 if (un->un_pos.eof == ST_EOT_PENDING) { 3528 if (minor & MT_NOREWIND) { 3529 if (st_cmd(un, SCMD_SPACE, Fmk(-1), SYNC_CMD)) { 3530 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 3531 "st_close: EIO can't space #3\n"); 3532 err = EIO; 3533 goto error_out; 3534 } else { 3535 un->un_pos.blkno = 0; 3536 un->un_pos.eof = ST_EOT; 3537 } 3538 } else { 3539 un->un_pos.eof = ST_NO_EOF; 3540 } 3541 3542 /* 3543 * Do we need to write a file mark? 3544 * 3545 * only write filemarks if there are fmks to be written and 3546 * - open for write (possibly read/write) 3547 * - the last operation was a write 3548 * or: 3549 * - opened for wronly 3550 * - no data was written 3551 */ 3552 } else if ((un->un_pos.pmode != invalid) && 3553 (un->un_fmneeded > 0) && 3554 (((flag & FWRITE) && 3555 ((un->un_lastop == ST_OP_WRITE)||(un->un_lastop == ST_OP_WEOF))) || 3556 ((flag == FWRITE) && (un->un_lastop == ST_OP_NIL)))) { 3557 3558 /* save ST_EOM state */ 3559 int was_at_eom = (un->un_pos.eof == ST_EOM) ? 1 : 0; 3560 3561 /* 3562 * Note that we will write a filemark if we had opened 3563 * the tape write only and no data was written, thus 3564 * creating a null file. 3565 * 3566 * If the user already wrote one, we only have to write 1 more. 3567 * If they wrote two, we don't have to write any. 3568 */ 3569 3570 count = un->un_fmneeded; 3571 if (count > 0) { 3572 if (st_cmd(un, SCMD_WRITE_FILE_MARK, count, SYNC_CMD)) { 3573 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 3574 "st_close : EIO can't wfm\n"); 3575 err = EIO; 3576 goto error_out; 3577 } 3578 if ((un->un_dp->options & ST_REEL) && 3579 (minor & MT_NOREWIND)) { 3580 if (st_cmd(un, SCMD_SPACE, Fmk(-1), SYNC_CMD)) { 3581 ST_DEBUG2(ST_DEVINFO, st_label, 3582 SCSI_DEBUG, 3583 "st_close : EIO space fmk(-1)\n"); 3584 err = EIO; 3585 goto error_out; 3586 } 3587 un->un_pos.eof = ST_NO_EOF; 3588 /* fix up block number */ 3589 un->un_pos.blkno = 0; 3590 } 3591 } 3592 3593 /* 3594 * If we aren't going to be rewinding, and we were at 3595 * physical eot, restore the state that indicates we 3596 * are at physical eot. Once you have reached physical 3597 * eot, and you close the tape, the only thing you can 3598 * do on the next open is to rewind. Access to trailer 3599 * records is only allowed without closing the device. 3600 */ 3601 if ((minor & MT_NOREWIND) == 0 && was_at_eom) { 3602 un->un_pos.eof = ST_EOM; 3603 } 3604 } 3605 3606 /* 3607 * report soft errors if enabled and available, if we never accessed 3608 * the drive, don't get errors. This will prevent some DAT error 3609 * messages upon LOG SENSE. 3610 */ 3611 if (st_report_soft_errors_on_close && 3612 (un->un_dp->options & ST_SOFT_ERROR_REPORTING) && 3613 (last_state != ST_STATE_OFFLINE)) { 3614 if (st_report_soft_errors(dev, flag)) { 3615 err = EIO; 3616 goto error_out; 3617 } 3618 } 3619 3620 3621 /* 3622 * Do we need to rewind? Can we rewind? 3623 */ 3624 if ((minor & MT_NOREWIND) == 0 && 3625 un->un_pos.pmode != invalid && err == 0) { 3626 /* 3627 * We'd like to rewind with the 3628 * 'immediate' bit set, but this 3629 * causes problems on some drives 3630 * where subsequent opens get a 3631 * 'NOT READY' error condition 3632 * back while the tape is rewinding, 3633 * which is impossible to distinguish 3634 * from the condition of 'no tape loaded'. 3635 * 3636 * Also, for some targets, if you disconnect 3637 * with the 'immediate' bit set, you don't 3638 * actually return right away, i.e., the 3639 * target ignores your request for immediate 3640 * return. 3641 * 3642 * Instead, we'll fire off an async rewind 3643 * command. We'll mark the device as closed, 3644 * and any subsequent open will stall on 3645 * the first TEST_UNIT_READY until the rewind 3646 * completes. 3647 */ 3648 3649 /* 3650 * Used to be if reserve was not supported we'd send an 3651 * asynchronious rewind. Comments above may be slightly invalid 3652 * as the immediate bit was never set. Doing an immedate rewind 3653 * makes sense, I think fixes to not ready status might handle 3654 * the problems described above. 3655 */ 3656 if (un->un_sd->sd_inq->inq_ansi < 2) { 3657 if (st_cmd(un, SCMD_REWIND, 0, SYNC_CMD)) { 3658 err = EIO; 3659 } 3660 } else { 3661 /* flush data for older drives per scsi spec. */ 3662 if (st_cmd(un, SCMD_WRITE_FILE_MARK, 0, SYNC_CMD)) { 3663 err = EIO; 3664 } else { 3665 /* release the drive before rewind immediate */ 3666 if ((un->un_rsvd_status & 3667 (ST_RESERVE | ST_PRESERVE_RESERVE)) == 3668 ST_RESERVE) { 3669 if (st_reserve_release(un, ST_RELEASE, 3670 st_uscsi_cmd)) { 3671 err = EIO; 3672 } 3673 } 3674 3675 /* send rewind with immediate bit set */ 3676 if (st_cmd(un, SCMD_REWIND, 1, ASYNC_CMD)) { 3677 err = EIO; 3678 } 3679 } 3680 } 3681 /* 3682 * Setting positions invalid in case the rewind doesn't 3683 * happen. Drives don't like to rewind if resets happen 3684 * they will tend to move back to where the rewind was 3685 * issued if a reset or something happens so that if a 3686 * write happens the data doesn't get clobbered. 3687 * 3688 * Not a big deal if the position is invalid when the 3689 * open occures it will do a read position. 3690 */ 3691 un->un_pos.pmode = invalid; 3692 un->un_running.pmode = invalid; 3693 3694 if (err == EIO) { 3695 goto error_out; 3696 } 3697 } 3698 3699 /* 3700 * eject tape if necessary 3701 */ 3702 if (un->un_eject_tape_on_failure) { 3703 un->un_eject_tape_on_failure = 0; 3704 if (st_cmd(un, SCMD_LOAD, LD_UNLOAD, SYNC_CMD)) { 3705 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 3706 "st_close : can't unload tape\n"); 3707 err = EIO; 3708 goto error_out; 3709 } else { 3710 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 3711 "st_close : tape unloaded \n"); 3712 un->un_pos.eof = ST_NO_EOF; 3713 un->un_mediastate = MTIO_EJECTED; 3714 } 3715 } 3716 /* 3717 * Release the tape unit, if default reserve/release 3718 * behaviour. 3719 */ 3720 if ((un->un_rsvd_status & 3721 (ST_RESERVE | ST_PRESERVE_RESERVE | 3722 ST_APPLICATION_RESERVATIONS)) == ST_RESERVE) { 3723 (void) st_reserve_release(un, ST_RELEASE, st_uscsi_cmd); 3724 } 3725 error_out: 3726 /* 3727 * clear up state 3728 */ 3729 un->un_laststate = un->un_state; 3730 un->un_state = ST_STATE_CLOSED; 3731 un->un_lastop = ST_OP_NIL; 3732 un->un_throttle = 1; /* assume one request at time, for now */ 3733 un->un_retry_ct = 0; 3734 un->un_errno = 0; 3735 un->un_swr_token = (opaque_t)NULL; 3736 un->un_rsvd_status &= ~(ST_INIT_RESERVE); 3737 3738 /* Restore the options to the init time settings */ 3739 if (un->un_init_options & ST_READ_IGNORE_ILI) { 3740 un->un_dp->options |= ST_READ_IGNORE_ILI; 3741 } else { 3742 un->un_dp->options &= ~ST_READ_IGNORE_ILI; 3743 } 3744 3745 if (un->un_init_options & ST_READ_IGNORE_EOFS) { 3746 un->un_dp->options |= ST_READ_IGNORE_EOFS; 3747 } else { 3748 un->un_dp->options &= ~ST_READ_IGNORE_EOFS; 3749 } 3750 3751 if (un->un_init_options & ST_SHORT_FILEMARKS) { 3752 un->un_dp->options |= ST_SHORT_FILEMARKS; 3753 } else { 3754 un->un_dp->options &= ~ST_SHORT_FILEMARKS; 3755 } 3756 3757 ASSERT(mutex_owned(ST_MUTEX)); 3758 3759 /* 3760 * Signal anyone awaiting a close operation to complete. 3761 */ 3762 cv_signal(&un->un_clscv); 3763 3764 /* 3765 * any kind of error on closing causes all state to be tossed 3766 */ 3767 if (err && un->un_status != KEY_ILLEGAL_REQUEST) { 3768 /* 3769 * note that st_intr has already set 3770 * un_pos.pmode to invalid. 3771 */ 3772 un->un_density_known = 0; 3773 } 3774 3775 #ifdef __x86 3776 /* 3777 * free any contiguous mem alloc'ed for big block I/O 3778 */ 3779 cp = un->un_contig_mem; 3780 while (cp) { 3781 if (cp->cm_addr) { 3782 ddi_dma_mem_free(&cp->cm_acc_hdl); 3783 } 3784 cp_temp = cp; 3785 cp = cp->cm_next; 3786 kmem_free(cp_temp, 3787 sizeof (struct contig_mem) + biosize()); 3788 } 3789 un->un_contig_mem_total_num = 0; 3790 un->un_contig_mem_available_num = 0; 3791 un->un_contig_mem = NULL; 3792 un->un_max_contig_mem_len = 0; 3793 #endif 3794 3795 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 3796 "st_close3: return val = %x, fileno=%x, blkno=%x, eof=%x\n", 3797 err, un->un_pos.fileno, un->un_pos.blkno, un->un_pos.eof); 3798 3799 mutex_exit(ST_MUTEX); 3800 return (err); 3801 } 3802 3803 /* 3804 * These routines perform raw i/o operations. 3805 */ 3806 3807 /* ARGSUSED2 */ 3808 static int 3809 st_aread(dev_t dev, struct aio_req *aio, cred_t *cred_p) 3810 { 3811 #ifdef STDEBUG 3812 GET_SOFT_STATE(dev); 3813 ST_ENTR(ST_DEVINFO, st_aread); 3814 #endif 3815 return (st_arw(dev, aio, B_READ)); 3816 } 3817 3818 3819 /* ARGSUSED2 */ 3820 static int 3821 st_awrite(dev_t dev, struct aio_req *aio, cred_t *cred_p) 3822 { 3823 #ifdef STDEBUG 3824 GET_SOFT_STATE(dev); 3825 ST_ENTR(ST_DEVINFO, st_awrite); 3826 #endif 3827 return (st_arw(dev, aio, B_WRITE)); 3828 } 3829 3830 3831 3832 /* ARGSUSED */ 3833 static int 3834 st_read(dev_t dev, struct uio *uiop, cred_t *cred_p) 3835 { 3836 #ifdef STDEBUG 3837 GET_SOFT_STATE(dev); 3838 ST_ENTR(ST_DEVINFO, st_read); 3839 #endif 3840 return (st_rw(dev, uiop, B_READ)); 3841 } 3842 3843 /* ARGSUSED */ 3844 static int 3845 st_write(dev_t dev, struct uio *uiop, cred_t *cred_p) 3846 { 3847 #ifdef STDEBUG 3848 GET_SOFT_STATE(dev); 3849 ST_ENTR(ST_DEVINFO, st_write); 3850 #endif 3851 return (st_rw(dev, uiop, B_WRITE)); 3852 } 3853 3854 /* 3855 * Due to historical reasons, old limits are: For variable-length devices: 3856 * if greater than 64KB - 1 (ST_MAXRECSIZE_VARIABLE), block into 64 KB - 2 3857 * ST_MAXRECSIZE_VARIABLE_LIMIT) requests; otherwise, 3858 * (let it through unmodified. For fixed-length record devices: 3859 * 63K (ST_MAXRECSIZE_FIXED) is max (default minphys). 3860 * 3861 * The new limits used are un_maxdma (retrieved using scsi_ifgetcap() 3862 * from the HBA) and un_maxbsize (retrieved by sending SCMD_READ_BLKLIM 3863 * command to the drive). 3864 * 3865 */ 3866 static void 3867 st_minphys(struct buf *bp) 3868 { 3869 struct scsi_tape *un; 3870 3871 un = ddi_get_soft_state(st_state, MTUNIT(bp->b_edev)); 3872 3873 ST_FUNC(ST_DEVINFO, st_minphys); 3874 3875 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 3876 "st_minphys(bp = 0x%p): b_bcount = 0x%lx\n", (void *)bp, 3877 bp->b_bcount); 3878 3879 if (un->un_allow_large_xfer) { 3880 3881 /* 3882 * check un_maxbsize for variable length devices only 3883 */ 3884 if (un->un_bsize == 0 && bp->b_bcount > un->un_maxbsize) { 3885 bp->b_bcount = un->un_maxbsize; 3886 } 3887 /* 3888 * can't go more that HBA maxdma limit in either fixed-length 3889 * or variable-length tape drives. 3890 */ 3891 if (bp->b_bcount > un->un_maxdma) { 3892 bp->b_bcount = un->un_maxdma; 3893 } 3894 } else { 3895 3896 /* 3897 * use old fixed limits 3898 */ 3899 if (un->un_bsize == 0) { 3900 if (bp->b_bcount > ST_MAXRECSIZE_VARIABLE) { 3901 bp->b_bcount = ST_MAXRECSIZE_VARIABLE_LIMIT; 3902 } 3903 } else { 3904 if (bp->b_bcount > ST_MAXRECSIZE_FIXED) { 3905 bp->b_bcount = ST_MAXRECSIZE_FIXED; 3906 } 3907 } 3908 } 3909 3910 /* 3911 * For regular raw I/O and Fixed Block length devices, make sure 3912 * the adjusted block count is a whole multiple of the device 3913 * block size. 3914 */ 3915 if (bp != un->un_sbufp && un->un_bsize) { 3916 bp->b_bcount -= (bp->b_bcount % un->un_bsize); 3917 } 3918 } 3919 3920 static int 3921 st_rw(dev_t dev, struct uio *uio, int flag) 3922 { 3923 int rval = 0; 3924 long len; 3925 3926 GET_SOFT_STATE(dev); 3927 3928 ST_FUNC(ST_DEVINFO, st_rw); 3929 3930 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 3931 "st_rw(dev = 0x%lx, flag = %s)\n", dev, 3932 (flag == B_READ ? rd_str: wr_str)); 3933 3934 /* get local copy of transfer length */ 3935 len = uio->uio_iov->iov_len; 3936 3937 mutex_enter(ST_MUTEX); 3938 3939 /* 3940 * Clear error entry stack 3941 */ 3942 st_empty_error_stack(un); 3943 3944 /* 3945 * If in fixed block size mode and requested read or write 3946 * is not an even multiple of that block size. 3947 */ 3948 if ((un->un_bsize != 0) && (len % un->un_bsize != 0)) { 3949 scsi_log(ST_DEVINFO, st_label, CE_WARN, 3950 "%s: not modulo %d block size\n", 3951 (flag == B_WRITE) ? wr_str : rd_str, un->un_bsize); 3952 rval = EINVAL; 3953 } 3954 3955 /* If device has set granularity in the READ_BLKLIM we honor it. */ 3956 if ((un->un_data_mod != 0) && (len % un->un_data_mod != 0)) { 3957 scsi_log(ST_DEVINFO, st_label, CE_WARN, 3958 "%s: not modulo %d device granularity\n", 3959 (flag == B_WRITE) ? wr_str : rd_str, un->un_data_mod); 3960 rval = EINVAL; 3961 } 3962 3963 if (st_recov_sz != sizeof (recov_info) && un->un_multipath) { 3964 scsi_log(ST_DEVINFO, st_label, CE_WARN, mp_misconf); 3965 rval = EFAULT; 3966 } 3967 3968 if (rval != 0) { 3969 un->un_errno = rval; 3970 mutex_exit(ST_MUTEX); 3971 return (rval); 3972 } 3973 3974 /* 3975 * Reset this so it can be set if Berkeley and read over a filemark. 3976 */ 3977 un->un_silent_skip = 0; 3978 mutex_exit(ST_MUTEX); 3979 3980 len = uio->uio_resid; 3981 3982 rval = physio(st_queued_strategy, (struct buf *)NULL, 3983 dev, flag, st_minphys, uio); 3984 /* 3985 * if we have hit logical EOT during this xfer and there is not a 3986 * full residue, then set eof back to ST_EOM to make sure that 3987 * the user will see at least one zero write 3988 * after this short write 3989 */ 3990 mutex_enter(ST_MUTEX); 3991 if (un->un_pos.eof > ST_NO_EOF) { 3992 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 3993 "eof=%d resid=%lx\n", un->un_pos.eof, uio->uio_resid); 3994 } 3995 if (un->un_pos.eof >= ST_EOM && (flag == B_WRITE)) { 3996 if ((uio->uio_resid != len) && (uio->uio_resid != 0)) { 3997 un->un_pos.eof = ST_EOM; 3998 } else if (uio->uio_resid == len) { 3999 un->un_pos.eof = ST_NO_EOF; 4000 } 4001 } 4002 4003 if (un->un_silent_skip && uio->uio_resid != len) { 4004 un->un_pos.eof = ST_EOF; 4005 un->un_pos.blkno = un->un_save_blkno; 4006 un->un_pos.fileno--; 4007 } 4008 4009 un->un_errno = rval; 4010 4011 mutex_exit(ST_MUTEX); 4012 4013 return (rval); 4014 } 4015 4016 static int 4017 st_arw(dev_t dev, struct aio_req *aio, int flag) 4018 { 4019 struct uio *uio = aio->aio_uio; 4020 int rval = 0; 4021 long len; 4022 4023 GET_SOFT_STATE(dev); 4024 4025 ST_FUNC(ST_DEVINFO, st_arw); 4026 4027 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 4028 "st_arw(dev = 0x%lx, flag = %s)\n", dev, 4029 (flag == B_READ ? rd_str: wr_str)); 4030 4031 /* get local copy of transfer length */ 4032 len = uio->uio_iov->iov_len; 4033 4034 mutex_enter(ST_MUTEX); 4035 4036 /* 4037 * If in fixed block size mode and requested read or write 4038 * is not an even multiple of that block size. 4039 */ 4040 if ((un->un_bsize != 0) && (len % un->un_bsize != 0)) { 4041 scsi_log(ST_DEVINFO, st_label, CE_WARN, 4042 "%s: not modulo %d block size\n", 4043 (flag == B_WRITE) ? wr_str : rd_str, un->un_bsize); 4044 rval = EINVAL; 4045 } 4046 4047 /* If device has set granularity in the READ_BLKLIM we honor it. */ 4048 if ((un->un_data_mod != 0) && (len % un->un_data_mod != 0)) { 4049 scsi_log(ST_DEVINFO, st_label, CE_WARN, 4050 "%s: not modulo %d device granularity\n", 4051 (flag == B_WRITE) ? wr_str : rd_str, un->un_data_mod); 4052 rval = EINVAL; 4053 } 4054 4055 if (st_recov_sz != sizeof (recov_info) && un->un_multipath) { 4056 scsi_log(ST_DEVINFO, st_label, CE_WARN, mp_misconf); 4057 rval = EFAULT; 4058 } 4059 4060 if (rval != 0) { 4061 un->un_errno = rval; 4062 mutex_exit(ST_MUTEX); 4063 return (rval); 4064 } 4065 4066 mutex_exit(ST_MUTEX); 4067 4068 len = uio->uio_resid; 4069 4070 rval = 4071 aphysio(st_queued_strategy, anocancel, dev, flag, st_minphys, aio); 4072 4073 /* 4074 * if we have hit logical EOT during this xfer and there is not a 4075 * full residue, then set eof back to ST_EOM to make sure that 4076 * the user will see at least one zero write 4077 * after this short write 4078 * 4079 * we keep this here just in case the application is not using 4080 * persistent errors 4081 */ 4082 mutex_enter(ST_MUTEX); 4083 if (un->un_pos.eof > ST_NO_EOF) { 4084 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4085 "eof=%d resid=%lx\n", un->un_pos.eof, uio->uio_resid); 4086 } 4087 if (un->un_pos.eof >= ST_EOM && (flag == B_WRITE)) { 4088 if ((uio->uio_resid != len) && (uio->uio_resid != 0)) { 4089 un->un_pos.eof = ST_EOM; 4090 } else if (uio->uio_resid == len && 4091 !(un->un_persistence && un->un_persist_errors)) { 4092 un->un_pos.eof = ST_NO_EOF; 4093 } 4094 } 4095 un->un_errno = rval; 4096 mutex_exit(ST_MUTEX); 4097 4098 return (rval); 4099 } 4100 4101 4102 4103 static int 4104 st_queued_strategy(buf_t *bp) 4105 { 4106 struct scsi_tape *un; 4107 char reading = bp->b_flags & B_READ; 4108 int wasopening = 0; 4109 4110 /* 4111 * validate arguments 4112 */ 4113 un = ddi_get_soft_state(st_state, MTUNIT(bp->b_edev)); 4114 if (un == NULL) { 4115 bp->b_resid = bp->b_bcount; 4116 bioerror(bp, ENXIO); 4117 ST_DEBUG6(NULL, st_label, SCSI_DEBUG, 4118 "st_queued_strategy: ENXIO error exit\n"); 4119 biodone(bp); 4120 return (0); 4121 } 4122 4123 ST_ENTR(ST_DEVINFO, st_queued_strategy); 4124 4125 mutex_enter(ST_MUTEX); 4126 4127 while (un->un_pwr_mgmt == ST_PWR_SUSPENDED) { 4128 cv_wait(&un->un_suspend_cv, ST_MUTEX); 4129 } 4130 4131 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 4132 "st_queued_strategy(): bcount=0x%lx, fileno=%d, blkno=%x, eof=%d\n", 4133 bp->b_bcount, un->un_pos.fileno, un->un_pos.blkno, un->un_pos.eof); 4134 4135 /* 4136 * If persistent errors have been flagged, just nix this one. We wait 4137 * for any outstanding I/O's below, so we will be in order. 4138 */ 4139 if (un->un_persistence && un->un_persist_errors) { 4140 goto exit; 4141 } 4142 4143 /* 4144 * If last command was non queued, wait till it finishes. 4145 */ 4146 while (un->un_sbuf_busy) { 4147 cv_wait(&un->un_sbuf_cv, ST_MUTEX); 4148 /* woke up because of an error */ 4149 if (un->un_persistence && un->un_persist_errors) { 4150 goto exit; 4151 } 4152 } 4153 4154 /* 4155 * s_buf and recovery commands shouldn't come here. 4156 */ 4157 ASSERT(bp != un->un_recov_buf); 4158 ASSERT(bp != un->un_sbufp); 4159 4160 /* 4161 * If we haven't done/checked reservation on the tape unit 4162 * do it now. 4163 */ 4164 if ((un->un_rsvd_status & 4165 (ST_RESERVE | ST_APPLICATION_RESERVATIONS)) == 0) { 4166 if ((un->un_dp->options & ST_NO_RESERVE_RELEASE) == 0) { 4167 if (st_reserve_release(un, ST_RESERVE, st_uscsi_cmd)) { 4168 st_bioerror(bp, un->un_errno); 4169 goto exit; 4170 } 4171 } else if (un->un_state == ST_STATE_OPEN_PENDING_IO) { 4172 /* 4173 * Enter here to restore position for possible 4174 * resets when the device was closed and opened 4175 * in O_NDELAY mode subsequently 4176 */ 4177 un->un_state = ST_STATE_INITIALIZING; 4178 (void) st_cmd(un, SCMD_TEST_UNIT_READY, 4179 0, SYNC_CMD); 4180 un->un_state = ST_STATE_OPEN_PENDING_IO; 4181 } 4182 un->un_rsvd_status |= ST_INIT_RESERVE; 4183 } 4184 4185 /* 4186 * If we are offline, we have to initialize everything first. 4187 * This is to handle either when opened with O_NDELAY, or 4188 * we just got a new tape in the drive, after an offline. 4189 * We don't observe O_NDELAY past the open, 4190 * as it will not make sense for tapes. 4191 */ 4192 if (un->un_state == ST_STATE_OFFLINE || un->un_restore_pos) { 4193 /* 4194 * reset state to avoid recursion 4195 */ 4196 un->un_laststate = un->un_state; 4197 un->un_state = ST_STATE_INITIALIZING; 4198 if (st_tape_init(un)) { 4199 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4200 "stioctl : OFFLINE init failure "); 4201 un->un_state = ST_STATE_OFFLINE; 4202 un->un_pos.pmode = invalid; 4203 goto b_done_err; 4204 } 4205 /* un_restore_pos make invalid */ 4206 un->un_state = ST_STATE_OPEN_PENDING_IO; 4207 un->un_restore_pos = 0; 4208 } 4209 /* 4210 * Check for legal operations 4211 */ 4212 if (un->un_pos.pmode == invalid) { 4213 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4214 "strategy with un->un_pos.pmode invalid\n"); 4215 goto b_done_err; 4216 } 4217 4218 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4219 "st_queued_strategy(): regular io\n"); 4220 4221 /* 4222 * Process this first. If we were reading, and we're pending 4223 * logical eot, that means we've bumped one file mark too far. 4224 */ 4225 4226 /* 4227 * Recursion warning: st_cmd will route back through here. 4228 * Not anymore st_cmd will go through st_strategy()! 4229 */ 4230 if (un->un_pos.eof == ST_EOT_PENDING) { 4231 if (st_cmd(un, SCMD_SPACE, Fmk(-1), SYNC_CMD)) { 4232 un->un_pos.pmode = invalid; 4233 un->un_density_known = 0; 4234 goto b_done_err; 4235 } 4236 un->un_pos.blkno = 0; /* fix up block number.. */ 4237 un->un_pos.eof = ST_EOT; 4238 } 4239 4240 /* 4241 * If we are in the process of opening, we may have to 4242 * determine/set the correct density. We also may have 4243 * to do a test_append (if QIC) to see whether we are 4244 * in a position to append to the end of the tape. 4245 * 4246 * If we're already at logical eot, we transition 4247 * to ST_NO_EOF. If we're at physical eot, we punt 4248 * to the switch statement below to handle. 4249 */ 4250 if ((un->un_state == ST_STATE_OPEN_PENDING_IO) || 4251 (un->un_test_append && (un->un_dp->options & ST_QIC))) { 4252 4253 if (un->un_state == ST_STATE_OPEN_PENDING_IO) { 4254 if (st_determine_density(un, (int)reading)) { 4255 goto b_done_err; 4256 } 4257 } 4258 4259 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4260 "pending_io@fileno %d rw %d qic %d eof %d\n", 4261 un->un_pos.fileno, (int)reading, 4262 (un->un_dp->options & ST_QIC) ? 1 : 0, 4263 un->un_pos.eof); 4264 4265 if (!reading && un->un_pos.eof != ST_EOM) { 4266 if (un->un_pos.eof == ST_EOT) { 4267 un->un_pos.eof = ST_NO_EOF; 4268 } else if (un->un_pos.pmode != invalid && 4269 (un->un_dp->options & ST_QIC)) { 4270 /* 4271 * st_test_append() will do it all 4272 */ 4273 st_test_append(bp); 4274 mutex_exit(ST_MUTEX); 4275 return (0); 4276 } 4277 } 4278 if (un->un_state == ST_STATE_OPEN_PENDING_IO) { 4279 wasopening = 1; 4280 } 4281 un->un_laststate = un->un_state; 4282 un->un_state = ST_STATE_OPEN; 4283 } 4284 4285 4286 /* 4287 * Process rest of END OF FILE and END OF TAPE conditions 4288 */ 4289 4290 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4291 "eof=%x, wasopening=%x\n", 4292 un->un_pos.eof, wasopening); 4293 4294 switch (un->un_pos.eof) { 4295 case ST_EOM: 4296 /* 4297 * This allows writes to proceed past physical 4298 * eot. We'll *really* be in trouble if the 4299 * user continues blindly writing data too 4300 * much past this point (unwind the tape). 4301 * Physical eot really means 'early warning 4302 * eot' in this context. 4303 * 4304 * Every other write from now on will succeed 4305 * (if sufficient tape left). 4306 * This write will return with resid == count 4307 * but the next one should be successful 4308 * 4309 * Note that we only transition to logical EOT 4310 * if the last state wasn't the OPENING state. 4311 * We explicitly prohibit running up to physical 4312 * eot, closing the device, and then re-opening 4313 * to proceed. Trailer records may only be gotten 4314 * at by keeping the tape open after hitting eot. 4315 * 4316 * Also note that ST_EOM cannot be set by reading- 4317 * this can only be set during writing. Reading 4318 * up to the end of the tape gets a blank check 4319 * or a double-filemark indication (ST_EOT_PENDING), 4320 * and we prohibit reading after that point. 4321 * 4322 */ 4323 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "EOM\n"); 4324 if (wasopening == 0) { 4325 /* 4326 * this allows st_rw() to reset it back to 4327 * will see a zero write 4328 */ 4329 un->un_pos.eof = ST_WRITE_AFTER_EOM; 4330 } 4331 un->un_status = SUN_KEY_EOT; 4332 goto b_done; 4333 4334 case ST_WRITE_AFTER_EOM: 4335 case ST_EOT: 4336 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "EOT\n"); 4337 un->un_status = SUN_KEY_EOT; 4338 if (SVR4_BEHAVIOR && reading) { 4339 goto b_done_err; 4340 } 4341 4342 if (reading) { 4343 goto b_done; 4344 } 4345 un->un_pos.eof = ST_NO_EOF; 4346 break; 4347 4348 case ST_EOF_PENDING: 4349 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4350 "EOF PENDING\n"); 4351 un->un_status = SUN_KEY_EOF; 4352 if (SVR4_BEHAVIOR) { 4353 un->un_pos.eof = ST_EOF; 4354 goto b_done; 4355 } 4356 /* FALLTHROUGH */ 4357 case ST_EOF: 4358 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "EOF\n"); 4359 un->un_status = SUN_KEY_EOF; 4360 if (SVR4_BEHAVIOR) { 4361 goto b_done_err; 4362 } 4363 4364 if (BSD_BEHAVIOR) { 4365 un->un_pos.eof = ST_NO_EOF; 4366 un->un_pos.fileno += 1; 4367 un->un_pos.blkno = 0; 4368 } 4369 4370 if (reading) { 4371 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4372 "now file %d (read)\n", 4373 un->un_pos.fileno); 4374 goto b_done; 4375 } 4376 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4377 "now file %d (write)\n", un->un_pos.fileno); 4378 break; 4379 default: 4380 un->un_status = 0; 4381 break; 4382 } 4383 4384 bp->b_flags &= ~(B_DONE); 4385 st_bioerror(bp, 0); 4386 bp->av_forw = NULL; 4387 bp->b_resid = 0; 4388 SET_BP_PKT(bp, 0); 4389 4390 4391 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4392 "st_queued_strategy: cmd=0x%p count=%ld resid=%ld flags=0x%x" 4393 " pkt=0x%p\n", 4394 (void *)bp->b_forw, bp->b_bcount, 4395 bp->b_resid, bp->b_flags, (void *)BP_PKT(bp)); 4396 4397 #ifdef __x86 4398 /* 4399 * We will replace bp with a new bp that can do big blk xfer 4400 * if the requested xfer size is bigger than un->un_maxdma_arch 4401 * 4402 * Also, we need to make sure that we're handling real I/O 4403 * by checking group 0/1 SCSI I/O commands, if needed 4404 */ 4405 if (bp->b_bcount > un->un_maxdma_arch && 4406 ((uchar_t)(uintptr_t)bp->b_forw == SCMD_READ || 4407 (uchar_t)(uintptr_t)bp->b_forw == SCMD_READ_G4 || 4408 (uchar_t)(uintptr_t)bp->b_forw == SCMD_WRITE || 4409 (uchar_t)(uintptr_t)bp->b_forw == SCMD_WRITE_G4)) { 4410 mutex_exit(ST_MUTEX); 4411 bp = st_get_bigblk_bp(bp); 4412 mutex_enter(ST_MUTEX); 4413 } 4414 #endif 4415 4416 /* put on wait queue */ 4417 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4418 "st_queued_strategy: un->un_quef = 0x%p, bp = 0x%p\n", 4419 (void *)un->un_quef, (void *)bp); 4420 4421 st_add_to_queue(&un->un_quef, &un->un_quel, un->un_quel, bp); 4422 4423 ST_DO_KSTATS(bp, kstat_waitq_enter); 4424 4425 st_start(un); 4426 4427 mutex_exit(ST_MUTEX); 4428 return (0); 4429 4430 b_done_err: 4431 st_bioerror(bp, EIO); 4432 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4433 "st_queued_strategy : EIO b_done_err\n"); 4434 4435 b_done: 4436 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4437 "st_queued_strategy: b_done\n"); 4438 4439 exit: 4440 /* 4441 * make sure no commands are outstanding or waiting before closing, 4442 * so we can guarantee order 4443 */ 4444 st_wait_for_io(un); 4445 un->un_err_resid = bp->b_resid = bp->b_bcount; 4446 4447 /* override errno here, if persistent errors were flagged */ 4448 if (un->un_persistence && un->un_persist_errors) 4449 bioerror(bp, un->un_errno); 4450 4451 mutex_exit(ST_MUTEX); 4452 4453 biodone(bp); 4454 ASSERT(mutex_owned(ST_MUTEX) == 0); 4455 return (0); 4456 } 4457 4458 4459 static int 4460 st_strategy(struct buf *bp) 4461 { 4462 struct scsi_tape *un; 4463 4464 /* 4465 * validate arguments 4466 */ 4467 un = ddi_get_soft_state(st_state, MTUNIT(bp->b_edev)); 4468 if (un == NULL) { 4469 bp->b_resid = bp->b_bcount; 4470 bioerror(bp, ENXIO); 4471 ST_DEBUG6(NULL, st_label, SCSI_DEBUG, 4472 "st_strategy: ENXIO error exit\n"); 4473 4474 biodone(bp); 4475 return (0); 4476 4477 } 4478 4479 ST_ENTR(ST_DEVINFO, st_strategy); 4480 4481 mutex_enter(ST_MUTEX); 4482 4483 while (un->un_pwr_mgmt == ST_PWR_SUSPENDED) { 4484 cv_wait(&un->un_suspend_cv, ST_MUTEX); 4485 } 4486 4487 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 4488 "st_strategy(): bcount=0x%lx, fileno=%d, blkno=%x, eof=%d\n", 4489 bp->b_bcount, un->un_pos.fileno, un->un_pos.blkno, un->un_pos.eof); 4490 4491 ASSERT((bp == un->un_recov_buf) || (bp == un->un_sbufp)); 4492 4493 bp->b_flags &= ~(B_DONE); 4494 st_bioerror(bp, 0); 4495 bp->av_forw = NULL; 4496 bp->b_resid = 0; 4497 SET_BP_PKT(bp, 0); 4498 4499 4500 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4501 "st_strategy: cmd=0x%x count=%ld resid=%ld flags=0x%x" 4502 " pkt=0x%p\n", 4503 (unsigned char)(uintptr_t)bp->b_forw, bp->b_bcount, 4504 bp->b_resid, bp->b_flags, (void *)BP_PKT(bp)); 4505 ST_DO_KSTATS(bp, kstat_waitq_enter); 4506 4507 st_start(un); 4508 4509 mutex_exit(ST_MUTEX); 4510 return (0); 4511 } 4512 4513 /* 4514 * this routine spaces forward over filemarks 4515 */ 4516 static int 4517 st_space_fmks(struct scsi_tape *un, int64_t count) 4518 { 4519 int rval = 0; 4520 4521 ST_FUNC(ST_DEVINFO, st_space_fmks); 4522 4523 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 4524 "st_space_fmks(dev = 0x%lx, count = %"PRIx64")\n", 4525 un->un_dev, count); 4526 4527 ASSERT(mutex_owned(ST_MUTEX)); 4528 4529 /* 4530 * the risk with doing only one space operation is that we 4531 * may accidentily jump in old data 4532 * the exabyte 8500 reading 8200 tapes cannot use KNOWS_EOD 4533 * because the 8200 does not append a marker; in order not to 4534 * sacrifice the fast file skip, we do a slow skip if the low 4535 * density device has been opened 4536 */ 4537 4538 if ((un->un_dp->options & ST_KNOWS_EOD) && 4539 !((un->un_dp->type == ST_TYPE_EXB8500 && 4540 MT_DENSITY(un->un_dev) == 0))) { 4541 if (st_cmd(un, SCMD_SPACE, Fmk(count), SYNC_CMD)) { 4542 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4543 "space_fmks : EIO can't do space cmd #1\n"); 4544 rval = EIO; 4545 } 4546 } else { 4547 while (count > 0) { 4548 if (st_cmd(un, SCMD_SPACE, Fmk(1), SYNC_CMD)) { 4549 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4550 "space_fmks : EIO can't do space cmd #2\n"); 4551 rval = EIO; 4552 break; 4553 } 4554 count -= 1; 4555 /* 4556 * read a block to see if we have reached 4557 * end of medium (double filemark for reel or 4558 * medium error for others) 4559 */ 4560 if (count > 0) { 4561 if (st_cmd(un, SCMD_SPACE, Blk(1), SYNC_CMD)) { 4562 ST_DEBUG2(ST_DEVINFO, st_label, 4563 SCSI_DEBUG, 4564 "space_fmks : EIO can't do " 4565 "space cmd #3\n"); 4566 rval = EIO; 4567 break; 4568 } 4569 if ((un->un_pos.eof >= ST_EOF_PENDING) && 4570 (un->un_dp->options & ST_REEL)) { 4571 un->un_status = SUN_KEY_EOT; 4572 ST_DEBUG2(ST_DEVINFO, st_label, 4573 SCSI_DEBUG, 4574 "space_fmks : EIO ST_REEL\n"); 4575 rval = EIO; 4576 break; 4577 } else if (IN_EOF(un->un_pos)) { 4578 un->un_pos.eof = ST_NO_EOF; 4579 un->un_pos.fileno++; 4580 un->un_pos.blkno = 0; 4581 count--; 4582 } else if (un->un_pos.eof > ST_EOF) { 4583 ST_DEBUG2(ST_DEVINFO, st_label, 4584 SCSI_DEBUG, 4585 "space_fmks, EIO > ST_EOF\n"); 4586 rval = EIO; 4587 break; 4588 } 4589 4590 } 4591 } 4592 un->un_err_resid = count; 4593 COPY_POS(&un->un_pos, &un->un_err_pos); 4594 } 4595 ASSERT(mutex_owned(ST_MUTEX)); 4596 return (rval); 4597 } 4598 4599 /* 4600 * this routine spaces to EOD 4601 * 4602 * it keeps track of the current filenumber and returns the filenumber after 4603 * the last successful space operation, we keep the number high because as 4604 * tapes are getting larger, the possibility of more and more files exist, 4605 * 0x100000 (1 Meg of files) probably will never have to be changed any time 4606 * soon 4607 */ 4608 #define MAX_SKIP 0x100000 /* somewhat arbitrary */ 4609 4610 static int 4611 st_find_eod(struct scsi_tape *un) 4612 { 4613 tapepos_t savepos; 4614 int64_t sp_type; 4615 int result; 4616 4617 if (un == NULL) { 4618 return (-1); 4619 } 4620 4621 ST_FUNC(ST_DEVINFO, st_find_eod); 4622 4623 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 4624 "st_find_eod(dev = 0x%lx): fileno = %d\n", un->un_dev, 4625 un->un_pos.fileno); 4626 4627 ASSERT(mutex_owned(ST_MUTEX)); 4628 4629 COPY_POS(&savepos, &un->un_pos); 4630 4631 /* 4632 * see if the drive is smart enough to do the skips in 4633 * one operation; 1/2" use two filemarks 4634 * the exabyte 8500 reading 8200 tapes cannot use KNOWS_EOD 4635 * because the 8200 does not append a marker; in order not to 4636 * sacrifice the fast file skip, we do a slow skip if the low 4637 * density device has been opened 4638 */ 4639 if ((un->un_dp->options & ST_KNOWS_EOD) != 0) { 4640 if ((un->un_dp->type == ST_TYPE_EXB8500) && 4641 (MT_DENSITY(un->un_dev) == 0)) { 4642 sp_type = Fmk(1); 4643 } else if (un->un_pos.pmode == logical) { 4644 sp_type = SPACE(SP_EOD, 0); 4645 } else { 4646 sp_type = Fmk(MAX_SKIP); 4647 } 4648 } else { 4649 sp_type = Fmk(1); 4650 } 4651 4652 for (;;) { 4653 result = st_cmd(un, SCMD_SPACE, sp_type, SYNC_CMD); 4654 4655 if (result == 0) { 4656 COPY_POS(&savepos, &un->un_pos); 4657 } 4658 4659 if (sp_type == SPACE(SP_EOD, 0)) { 4660 if (result != 0) { 4661 sp_type = Fmk(MAX_SKIP); 4662 continue; 4663 } 4664 4665 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4666 "st_find_eod: 0x%"PRIx64"\n", 4667 savepos.lgclblkno); 4668 /* 4669 * What we return will become the current file position. 4670 * After completing the space command with the position 4671 * mode that is not invalid a read position command will 4672 * be automaticly issued. If the drive support the long 4673 * read position format a valid file position can be 4674 * returned. 4675 */ 4676 return (un->un_pos.fileno); 4677 } 4678 4679 if (result != 0) { 4680 break; 4681 } 4682 4683 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4684 "count=%"PRIx64", eof=%x, status=%x\n", 4685 SPACE_CNT(sp_type), un->un_pos.eof, un->un_status); 4686 4687 /* 4688 * If we're not EOM smart, space a record 4689 * to see whether we're now in the slot between 4690 * the two sequential filemarks that logical 4691 * EOM consists of (REEL) or hit nowhere land 4692 * (8mm). 4693 */ 4694 if (sp_type == Fmk(1)) { 4695 /* 4696 * no fast skipping, check a record 4697 */ 4698 if (st_cmd(un, SCMD_SPACE, Blk((1)), SYNC_CMD)) { 4699 break; 4700 } 4701 if ((un->un_pos.eof >= ST_EOF_PENDING) && 4702 (un->un_dp->options & ST_REEL)) { 4703 un->un_status = KEY_BLANK_CHECK; 4704 un->un_pos.fileno++; 4705 un->un_pos.blkno = 0; 4706 break; 4707 } 4708 if (IN_EOF(un->un_pos)) { 4709 un->un_pos.eof = ST_NO_EOF; 4710 un->un_pos.fileno++; 4711 un->un_pos.blkno = 0; 4712 } 4713 if (un->un_pos.eof > ST_EOF) { 4714 break; 4715 } 4716 } else { 4717 if (un->un_pos.eof > ST_EOF) { 4718 break; 4719 } 4720 } 4721 } 4722 4723 if (un->un_dp->options & ST_KNOWS_EOD) { 4724 COPY_POS(&savepos, &un->un_pos); 4725 } 4726 4727 ASSERT(mutex_owned(ST_MUTEX)); 4728 4729 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4730 "st_find_eod: %x\n", savepos.fileno); 4731 return (savepos.fileno); 4732 } 4733 4734 4735 /* 4736 * this routine is frequently used in ioctls below; 4737 * it determines whether we know the density and if not will 4738 * determine it 4739 * if we have written the tape before, one or more filemarks are written 4740 * 4741 * depending on the stepflag, the head is repositioned to where it was before 4742 * the filemarks were written in order not to confuse step counts 4743 */ 4744 #define STEPBACK 0 4745 #define NO_STEPBACK 1 4746 4747 static int 4748 st_check_density_or_wfm(dev_t dev, int wfm, int mode, int stepflag) 4749 { 4750 4751 GET_SOFT_STATE(dev); 4752 4753 ST_FUNC(ST_DEVINFO, st_check_density_or_wfm); 4754 4755 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 4756 "st_check_density_or_wfm(dev= 0x%lx, wfm= %d, mode= %d, stpflg= %d)" 4757 "\n", dev, wfm, mode, stepflag); 4758 4759 ASSERT(mutex_owned(ST_MUTEX)); 4760 4761 /* 4762 * If we don't yet know the density of the tape we have inserted, 4763 * we have to either unconditionally set it (if we're 'writing'), 4764 * or we have to determine it. As side effects, check for any 4765 * write-protect errors, and for the need to put out any file-marks 4766 * before positioning a tape. 4767 * 4768 * If we are going to be spacing forward, and we haven't determined 4769 * the tape density yet, we have to do so now... 4770 */ 4771 if (un->un_state == ST_STATE_OPEN_PENDING_IO) { 4772 if (st_determine_density(un, mode)) { 4773 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4774 "check_density_or_wfm : EIO can't determine " 4775 "density\n"); 4776 un->un_errno = EIO; 4777 return (EIO); 4778 } 4779 /* 4780 * Presumably we are at BOT. If we attempt to write, it will 4781 * either work okay, or bomb. We don't do a st_test_append 4782 * unless we're past BOT. 4783 */ 4784 un->un_laststate = un->un_state; 4785 un->un_state = ST_STATE_OPEN; 4786 4787 } else if (un->un_pos.pmode != invalid && un->un_fmneeded > 0 && 4788 ((un->un_lastop == ST_OP_WEOF && wfm) || 4789 (un->un_lastop == ST_OP_WRITE && wfm))) { 4790 4791 tapepos_t spos; 4792 4793 COPY_POS(&spos, &un->un_pos); 4794 4795 /* 4796 * We need to write one or two filemarks. 4797 * In the case of the HP, we need to 4798 * position the head between the two 4799 * marks. 4800 */ 4801 if ((un->un_fmneeded > 0) || (un->un_lastop == ST_OP_WEOF)) { 4802 wfm = un->un_fmneeded; 4803 un->un_fmneeded = 0; 4804 } 4805 4806 if (st_write_fm(dev, wfm)) { 4807 un->un_pos.pmode = invalid; 4808 un->un_density_known = 0; 4809 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4810 "check_density_or_wfm : EIO can't write fm\n"); 4811 un->un_errno = EIO; 4812 return (EIO); 4813 } 4814 4815 if (stepflag == STEPBACK) { 4816 if (st_cmd(un, SCMD_SPACE, Fmk(-wfm), SYNC_CMD)) { 4817 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4818 "check_density_or_wfm : EIO can't space " 4819 "(-wfm)\n"); 4820 un->un_errno = EIO; 4821 return (EIO); 4822 } 4823 COPY_POS(&un->un_pos, &spos); 4824 } 4825 } 4826 4827 /* 4828 * Whatever we do at this point clears the state of the eof flag. 4829 */ 4830 4831 un->un_pos.eof = ST_NO_EOF; 4832 4833 /* 4834 * If writing, let's check that we're positioned correctly 4835 * at the end of tape before issuing the next write. 4836 */ 4837 if (un->un_read_only == RDWR) { 4838 un->un_test_append = 1; 4839 } 4840 4841 ASSERT(mutex_owned(ST_MUTEX)); 4842 return (0); 4843 } 4844 4845 4846 /* 4847 * Wait for all outstaning I/O's to complete 4848 * 4849 * we wait on both ncmds and the wait queue for times when we are flushing 4850 * after persistent errors are flagged, which is when ncmds can be 0, and the 4851 * queue can still have I/O's. This way we preserve order of biodone's. 4852 */ 4853 static void 4854 st_wait_for_io(struct scsi_tape *un) 4855 { 4856 ST_FUNC(ST_DEVINFO, st_wait_for_io); 4857 ASSERT(mutex_owned(ST_MUTEX)); 4858 while ((un->un_ncmds) || (un->un_quef) || (un->un_runqf)) { 4859 cv_wait(&un->un_queue_cv, ST_MUTEX); 4860 } 4861 } 4862 4863 /* 4864 * This routine implements the ioctl calls. It is called 4865 * from the device switch at normal priority. 4866 */ 4867 /*ARGSUSED*/ 4868 static int 4869 st_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p, 4870 int *rval_p) 4871 { 4872 int tmp, rval = 0; 4873 4874 GET_SOFT_STATE(dev); 4875 4876 ST_ENTR(ST_DEVINFO, st_ioctl); 4877 4878 mutex_enter(ST_MUTEX); 4879 4880 ASSERT(un->un_recov_buf_busy == 0); 4881 4882 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 4883 "st_ioctl(): fileno=%x, blkno=%x, eof=%x, state = %d, " 4884 "pe_flag = %d\n", 4885 un->un_pos.fileno, un->un_pos.blkno, un->un_pos.eof, un->un_state, 4886 un->un_persistence && un->un_persist_errors); 4887 4888 /* 4889 * We don't want to block on these, so let them through 4890 * and we don't care about setting driver states here. 4891 */ 4892 if ((cmd == MTIOCGETDRIVETYPE) || 4893 (cmd == MTIOCGUARANTEEDORDER) || 4894 (cmd == MTIOCPERSISTENTSTATUS)) { 4895 goto check_commands; 4896 } 4897 4898 /* 4899 * We clear error entry stack except command 4900 * MTIOCGETERROR and MTIOCGET 4901 */ 4902 if ((cmd != MTIOCGETERROR) && 4903 (cmd != MTIOCGET)) { 4904 st_empty_error_stack(un); 4905 } 4906 4907 /* 4908 * wait for all outstanding commands to complete, or be dequeued. 4909 * And because ioctl's are synchronous commands, any return value 4910 * after this, will be in order 4911 */ 4912 st_wait_for_io(un); 4913 4914 /* 4915 * allow only a through clear errors and persistent status, and 4916 * status 4917 */ 4918 if (un->un_persistence && un->un_persist_errors) { 4919 if ((cmd == MTIOCLRERR) || 4920 (cmd == MTIOCPERSISTENT) || 4921 (cmd == MTIOCGET)) { 4922 goto check_commands; 4923 } else { 4924 rval = un->un_errno; 4925 goto exit; 4926 } 4927 } 4928 4929 ASSERT(un->un_throttle != 0); 4930 un->un_throttle = 1; /* > 1 will never happen here */ 4931 un->un_errno = 0; /* start clean from here */ 4932 4933 /* 4934 * first and foremost, handle any ST_EOT_PENDING cases. 4935 * That is, if a logical eot is pending notice, notice it. 4936 */ 4937 if (un->un_pos.eof == ST_EOT_PENDING) { 4938 int resid = un->un_err_resid; 4939 uchar_t status = un->un_status; 4940 uchar_t lastop = un->un_lastop; 4941 4942 if (st_cmd(un, SCMD_SPACE, Fmk(-1), SYNC_CMD)) { 4943 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4944 "stioctl : EIO can't space fmk(-1)\n"); 4945 rval = EIO; 4946 goto exit; 4947 } 4948 un->un_lastop = lastop; /* restore last operation */ 4949 if (status == SUN_KEY_EOF) { 4950 un->un_status = SUN_KEY_EOT; 4951 } else { 4952 un->un_status = status; 4953 } 4954 un->un_err_resid = resid; 4955 /* fix up block number */ 4956 un->un_err_pos.blkno = un->un_pos.blkno = 0; 4957 /* now we're at logical eot */ 4958 un->un_pos.eof = ST_EOT; 4959 } 4960 4961 /* 4962 * now, handle the rest of the situations 4963 */ 4964 check_commands: 4965 switch (cmd) { 4966 case MTIOCGET: 4967 { 4968 #ifdef _MULTI_DATAMODEL 4969 /* 4970 * For use when a 32 bit app makes a call into a 4971 * 64 bit ioctl 4972 */ 4973 struct mtget32 mtg_local32; 4974 struct mtget32 *mtget_32 = &mtg_local32; 4975 #endif /* _MULTI_DATAMODEL */ 4976 4977 /* Get tape status */ 4978 struct mtget mtg_local; 4979 struct mtget *mtget = &mtg_local; 4980 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 4981 "st_ioctl: MTIOCGET\n"); 4982 4983 bzero((caddr_t)mtget, sizeof (struct mtget)); 4984 mtget->mt_erreg = un->un_status; 4985 mtget->mt_resid = un->un_err_resid; 4986 mtget->mt_dsreg = un->un_retry_ct; 4987 if (un->un_err_pos.pmode == legacy) { 4988 mtget->mt_fileno = un->un_err_pos.fileno; 4989 } else { 4990 mtget->mt_fileno = -1; 4991 } 4992 /* 4993 * If the value is positive fine. 4994 * If its negative we need to return a value based on the 4995 * old way if counting backwards from INF (1,000,000,000). 4996 */ 4997 if (un->un_err_pos.blkno >= 0) { 4998 mtget->mt_blkno = un->un_err_pos.blkno; 4999 } else { 5000 mtget->mt_blkno = INF + 1 - (-un->un_err_pos.blkno); 5001 } 5002 mtget->mt_type = un->un_dp->type; 5003 mtget->mt_flags = MTF_SCSI | MTF_ASF; 5004 if (un->un_read_pos_type != NO_POS) { 5005 mtget->mt_flags |= MTF_LOGICAL_BLOCK; 5006 } 5007 if (un->un_dp->options & ST_REEL) { 5008 mtget->mt_flags |= MTF_REEL; 5009 mtget->mt_bf = 20; 5010 } else { /* 1/4" cartridges */ 5011 switch (mtget->mt_type) { 5012 /* Emulex cartridge tape */ 5013 case MT_ISMT02: 5014 mtget->mt_bf = 40; 5015 break; 5016 default: 5017 mtget->mt_bf = 126; 5018 break; 5019 } 5020 } 5021 5022 /* 5023 * If large transfers are allowed and drive options 5024 * has no record size limit set. Calculate blocking 5025 * factor from the lesser of maxbsize and maxdma. 5026 */ 5027 if ((un->un_allow_large_xfer) && 5028 (un->un_dp->options & ST_NO_RECSIZE_LIMIT)) { 5029 mtget->mt_bf = min(un->un_maxbsize, 5030 un->un_maxdma) / SECSIZE; 5031 } 5032 5033 if (un->un_read_only == WORM || 5034 un->un_read_only == RDWORM) { 5035 mtget->mt_flags |= MTF_WORM_MEDIA; 5036 } 5037 5038 /* 5039 * In persistent error mode sending a non-queued can hang 5040 * because this ioctl gets to be run without turning off 5041 * persistense. Fake the answer based on previous info. 5042 */ 5043 if (un->un_persistence) { 5044 rval = 0; 5045 } else { 5046 rval = st_check_clean_bit(un); 5047 } 5048 if (rval == 0) { 5049 /* 5050 * If zero is returned or in persistent mode, 5051 * use the old data. 5052 */ 5053 if ((un->un_HeadClean & (TAPE_ALERT_SUPPORTED | 5054 TAPE_SEQUENTIAL_SUPPORTED|TAPE_ALERT_NOT_SUPPORTED)) 5055 != TAPE_ALERT_NOT_SUPPORTED) { 5056 mtget->mt_flags |= MTF_TAPE_CLN_SUPPORTED; 5057 } 5058 if (un->un_HeadClean & (TAPE_PREVIOUSLY_DIRTY | 5059 TAPE_ALERT_STILL_DIRTY)) { 5060 mtget->mt_flags |= MTF_TAPE_HEAD_DIRTY; 5061 } 5062 } else { 5063 mtget->mt_flags |= (ushort_t)rval; 5064 rval = 0; 5065 } 5066 5067 un->un_status = 0; /* Reset status */ 5068 un->un_err_resid = 0; 5069 tmp = sizeof (struct mtget); 5070 5071 #ifdef _MULTI_DATAMODEL 5072 5073 switch (ddi_model_convert_from(flag & FMODELS)) { 5074 case DDI_MODEL_ILP32: 5075 /* 5076 * Convert 64 bit back to 32 bit before doing 5077 * copyout. This is what the ILP32 app expects. 5078 */ 5079 mtget_32->mt_erreg = mtget->mt_erreg; 5080 mtget_32->mt_resid = mtget->mt_resid; 5081 mtget_32->mt_dsreg = mtget->mt_dsreg; 5082 mtget_32->mt_fileno = (daddr32_t)mtget->mt_fileno; 5083 mtget_32->mt_blkno = (daddr32_t)mtget->mt_blkno; 5084 mtget_32->mt_type = mtget->mt_type; 5085 mtget_32->mt_flags = mtget->mt_flags; 5086 mtget_32->mt_bf = mtget->mt_bf; 5087 5088 if (ddi_copyout(mtget_32, (void *)arg, 5089 sizeof (struct mtget32), flag)) { 5090 rval = EFAULT; 5091 } 5092 break; 5093 5094 case DDI_MODEL_NONE: 5095 if (ddi_copyout(mtget, (void *)arg, tmp, flag)) { 5096 rval = EFAULT; 5097 } 5098 break; 5099 } 5100 #else /* ! _MULTI_DATAMODE */ 5101 if (ddi_copyout(mtget, (void *)arg, tmp, flag)) { 5102 rval = EFAULT; 5103 } 5104 #endif /* _MULTI_DATAMODE */ 5105 5106 break; 5107 } 5108 case MTIOCGETERROR: 5109 /* 5110 * get error entry from error stack 5111 */ 5112 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 5113 "st_ioctl: MTIOCGETERROR\n"); 5114 5115 rval = st_get_error_entry(un, arg, flag); 5116 5117 break; 5118 5119 case MTIOCSTATE: 5120 { 5121 /* 5122 * return when media presence matches state 5123 */ 5124 enum mtio_state state; 5125 5126 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 5127 "st_ioctl: MTIOCSTATE\n"); 5128 5129 if (ddi_copyin((void *)arg, &state, sizeof (int), flag)) 5130 rval = EFAULT; 5131 5132 mutex_exit(ST_MUTEX); 5133 5134 rval = st_check_media(dev, state); 5135 5136 mutex_enter(ST_MUTEX); 5137 5138 if (rval != 0) { 5139 break; 5140 } 5141 5142 if (ddi_copyout(&un->un_mediastate, (void *)arg, 5143 sizeof (int), flag)) 5144 rval = EFAULT; 5145 break; 5146 5147 } 5148 5149 case MTIOCGETDRIVETYPE: 5150 { 5151 #ifdef _MULTI_DATAMODEL 5152 /* 5153 * For use when a 32 bit app makes a call into a 5154 * 64 bit ioctl 5155 */ 5156 struct mtdrivetype_request32 mtdtrq32; 5157 #endif /* _MULTI_DATAMODEL */ 5158 5159 /* 5160 * return mtdrivetype 5161 */ 5162 struct mtdrivetype_request mtdtrq; 5163 struct mtdrivetype mtdrtyp; 5164 struct mtdrivetype *mtdt = &mtdrtyp; 5165 struct st_drivetype *stdt = un->un_dp; 5166 5167 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 5168 "st_ioctl: MTIOCGETDRIVETYPE\n"); 5169 5170 #ifdef _MULTI_DATAMODEL 5171 switch (ddi_model_convert_from(flag & FMODELS)) { 5172 case DDI_MODEL_ILP32: 5173 { 5174 if (ddi_copyin((void *)arg, &mtdtrq32, 5175 sizeof (struct mtdrivetype_request32), flag)) { 5176 rval = EFAULT; 5177 break; 5178 } 5179 mtdtrq.size = mtdtrq32.size; 5180 mtdtrq.mtdtp = 5181 (struct mtdrivetype *)(uintptr_t)mtdtrq32.mtdtp; 5182 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 5183 "st_ioctl: size 0x%x\n", mtdtrq.size); 5184 break; 5185 } 5186 case DDI_MODEL_NONE: 5187 if (ddi_copyin((void *)arg, &mtdtrq, 5188 sizeof (struct mtdrivetype_request), flag)) { 5189 rval = EFAULT; 5190 break; 5191 } 5192 break; 5193 } 5194 5195 #else /* ! _MULTI_DATAMODEL */ 5196 if (ddi_copyin((void *)arg, &mtdtrq, 5197 sizeof (struct mtdrivetype_request), flag)) { 5198 rval = EFAULT; 5199 break; 5200 } 5201 #endif /* _MULTI_DATAMODEL */ 5202 5203 /* 5204 * if requested size is < 0 then return 5205 * error. 5206 */ 5207 if (mtdtrq.size < 0) { 5208 rval = EINVAL; 5209 break; 5210 } 5211 bzero(mtdt, sizeof (struct mtdrivetype)); 5212 (void) strncpy(mtdt->name, stdt->name, ST_NAMESIZE); 5213 (void) strncpy(mtdt->vid, stdt->vid, VIDPIDLEN - 1); 5214 mtdt->type = stdt->type; 5215 mtdt->bsize = stdt->bsize; 5216 mtdt->options = stdt->options; 5217 mtdt->max_rretries = stdt->max_rretries; 5218 mtdt->max_wretries = stdt->max_wretries; 5219 for (tmp = 0; tmp < NDENSITIES; tmp++) { 5220 mtdt->densities[tmp] = stdt->densities[tmp]; 5221 } 5222 mtdt->default_density = stdt->default_density; 5223 /* 5224 * Speed hasn't been used since the hayday of reel tape. 5225 * For all drives not setting the option ST_KNOWS_MEDIA 5226 * the speed member renamed to mediatype are zeros. 5227 * Those drives that have ST_KNOWS_MEDIA set use the 5228 * new mediatype member which is used to figure the 5229 * type of media loaded. 5230 * 5231 * So as to not break applications speed in the 5232 * mtdrivetype structure is not renamed. 5233 */ 5234 for (tmp = 0; tmp < NDENSITIES; tmp++) { 5235 mtdt->speeds[tmp] = stdt->mediatype[tmp]; 5236 } 5237 mtdt->non_motion_timeout = stdt->non_motion_timeout; 5238 mtdt->io_timeout = stdt->io_timeout; 5239 mtdt->rewind_timeout = stdt->rewind_timeout; 5240 mtdt->space_timeout = stdt->space_timeout; 5241 mtdt->load_timeout = stdt->load_timeout; 5242 mtdt->unload_timeout = stdt->unload_timeout; 5243 mtdt->erase_timeout = stdt->erase_timeout; 5244 5245 /* 5246 * Limit the maximum length of the result to 5247 * sizeof (struct mtdrivetype). 5248 */ 5249 tmp = sizeof (struct mtdrivetype); 5250 if (mtdtrq.size < tmp) 5251 tmp = mtdtrq.size; 5252 if (ddi_copyout(mtdt, mtdtrq.mtdtp, tmp, flag)) { 5253 rval = EFAULT; 5254 } 5255 break; 5256 } 5257 case MTIOCPERSISTENT: 5258 5259 if (ddi_copyin((void *)arg, &tmp, sizeof (tmp), flag)) { 5260 rval = EFAULT; 5261 break; 5262 } 5263 5264 if (tmp) { 5265 st_turn_pe_on(un); 5266 } else { 5267 st_turn_pe_off(un); 5268 } 5269 5270 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 5271 "st_ioctl: MTIOCPERSISTENT : persistence = %d\n", 5272 un->un_persistence); 5273 5274 break; 5275 5276 case MTIOCPERSISTENTSTATUS: 5277 tmp = (int)un->un_persistence; 5278 5279 if (ddi_copyout(&tmp, (void *)arg, sizeof (tmp), flag)) { 5280 rval = EFAULT; 5281 } 5282 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 5283 "st_ioctl: MTIOCPERSISTENTSTATUS:persistence = %d\n", 5284 un->un_persistence); 5285 5286 break; 5287 5288 case MTIOCLRERR: 5289 { 5290 /* clear persistent errors */ 5291 5292 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 5293 "st_ioctl: MTIOCLRERR\n"); 5294 5295 st_clear_pe(un); 5296 5297 break; 5298 } 5299 5300 case MTIOCGUARANTEEDORDER: 5301 { 5302 /* 5303 * this is just a holder to make a valid ioctl and 5304 * it won't be in any earlier release 5305 */ 5306 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 5307 "st_ioctl: MTIOCGUARANTEEDORDER\n"); 5308 5309 break; 5310 } 5311 5312 case MTIOCRESERVE: 5313 { 5314 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 5315 "st_ioctl: MTIOCRESERVE\n"); 5316 5317 /* 5318 * Check if Reserve/Release is supported. 5319 */ 5320 if (un->un_dp->options & ST_NO_RESERVE_RELEASE) { 5321 rval = ENOTTY; 5322 break; 5323 } 5324 5325 rval = st_reserve_release(un, ST_RESERVE, st_uscsi_cmd); 5326 5327 if (rval == 0) { 5328 un->un_rsvd_status |= ST_PRESERVE_RESERVE; 5329 } 5330 break; 5331 } 5332 5333 case MTIOCRELEASE: 5334 { 5335 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 5336 "st_ioctl: MTIOCRELEASE\n"); 5337 5338 /* 5339 * Check if Reserve/Release is supported. 5340 */ 5341 if (un->un_dp->options & ST_NO_RESERVE_RELEASE) { 5342 rval = ENOTTY; 5343 break; 5344 } 5345 5346 /* 5347 * Used to just clear ST_PRESERVE_RESERVE which 5348 * made the reservation release at next close. 5349 * As the user may have opened and then done a 5350 * persistant reservation we now need to drop 5351 * the reservation without closing if the user 5352 * attempts to do this. 5353 */ 5354 rval = st_reserve_release(un, ST_RELEASE, st_uscsi_cmd); 5355 5356 un->un_rsvd_status &= ~ST_PRESERVE_RESERVE; 5357 5358 break; 5359 } 5360 5361 case MTIOCFORCERESERVE: 5362 { 5363 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 5364 "st_ioctl: MTIOCFORCERESERVE\n"); 5365 5366 /* 5367 * Check if Reserve/Release is supported. 5368 */ 5369 if (un->un_dp->options & ST_NO_RESERVE_RELEASE) { 5370 rval = ENOTTY; 5371 break; 5372 } 5373 /* 5374 * allow only super user to run this. 5375 */ 5376 if (drv_priv(cred_p) != 0) { 5377 rval = EPERM; 5378 break; 5379 } 5380 /* 5381 * Throw away reserve, 5382 * not using test-unit-ready 5383 * since reserve can succeed without tape being 5384 * present in the drive. 5385 */ 5386 (void) st_reserve_release(un, ST_RESERVE, st_uscsi_cmd); 5387 5388 rval = st_take_ownership(un, st_uscsi_cmd); 5389 5390 break; 5391 } 5392 5393 case USCSICMD: 5394 { 5395 cred_t *cr; 5396 5397 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 5398 "st_ioctl: USCSICMD\n"); 5399 5400 cr = ddi_get_cred(); 5401 if ((drv_priv(cred_p) != 0) && (drv_priv(cr) != 0)) { 5402 rval = EPERM; 5403 } else { 5404 rval = st_uscsi_cmd(un, (struct uscsi_cmd *)arg, flag); 5405 } 5406 break; 5407 } 5408 case MTIOCTOP: 5409 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 5410 "st_ioctl: MTIOCTOP\n"); 5411 rval = st_mtioctop(un, arg, flag); 5412 break; 5413 5414 case MTIOCLTOP: 5415 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 5416 "st_ioctl: MTIOLCTOP\n"); 5417 rval = st_mtiocltop(un, arg, flag); 5418 break; 5419 5420 case MTIOCREADIGNOREILI: 5421 { 5422 int set_ili; 5423 5424 if (ddi_copyin((void *)arg, &set_ili, 5425 sizeof (set_ili), flag)) { 5426 rval = EFAULT; 5427 break; 5428 } 5429 5430 if (un->un_bsize) { 5431 rval = ENOTTY; 5432 break; 5433 } 5434 5435 switch (set_ili) { 5436 case 0: 5437 un->un_dp->options &= ~ST_READ_IGNORE_ILI; 5438 break; 5439 5440 case 1: 5441 un->un_dp->options |= ST_READ_IGNORE_ILI; 5442 break; 5443 5444 default: 5445 rval = EINVAL; 5446 break; 5447 } 5448 break; 5449 } 5450 5451 case MTIOCREADIGNOREEOFS: 5452 { 5453 int ignore_eof; 5454 5455 if (ddi_copyin((void *)arg, &ignore_eof, 5456 sizeof (ignore_eof), flag)) { 5457 rval = EFAULT; 5458 break; 5459 } 5460 5461 if (!(un->un_dp->options & ST_REEL)) { 5462 rval = ENOTTY; 5463 break; 5464 } 5465 5466 switch (ignore_eof) { 5467 case 0: 5468 un->un_dp->options &= ~ST_READ_IGNORE_EOFS; 5469 break; 5470 5471 case 1: 5472 un->un_dp->options |= ST_READ_IGNORE_EOFS; 5473 break; 5474 5475 default: 5476 rval = EINVAL; 5477 break; 5478 } 5479 break; 5480 } 5481 5482 case MTIOCSHORTFMK: 5483 { 5484 int short_fmk; 5485 5486 if (ddi_copyin((void *)arg, &short_fmk, 5487 sizeof (short_fmk), flag)) { 5488 rval = EFAULT; 5489 break; 5490 } 5491 5492 switch (un->un_dp->type) { 5493 case ST_TYPE_EXB8500: 5494 case ST_TYPE_EXABYTE: 5495 if (!short_fmk) { 5496 un->un_dp->options &= ~ST_SHORT_FILEMARKS; 5497 } else if (short_fmk == 1) { 5498 un->un_dp->options |= ST_SHORT_FILEMARKS; 5499 } else { 5500 rval = EINVAL; 5501 } 5502 break; 5503 5504 default: 5505 rval = ENOTTY; 5506 break; 5507 } 5508 break; 5509 } 5510 5511 case MTIOCGETPOS: 5512 rval = st_update_block_pos(un, st_cmd, 0); 5513 if (rval == 0) { 5514 if (ddi_copyout((void *)&un->un_pos, (void *)arg, 5515 sizeof (tapepos_t), flag)) { 5516 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 5517 "MTIOCGETPOS copy out failed\n"); 5518 rval = EFAULT; 5519 } 5520 } 5521 break; 5522 5523 case MTIOCRESTPOS: 5524 { 5525 tapepos_t dest; 5526 5527 if (ddi_copyin((void *)arg, &dest, sizeof (tapepos_t), 5528 flag) != 0) { 5529 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 5530 "MTIOCRESTPOS copy in failed\n"); 5531 rval = EFAULT; 5532 break; 5533 } 5534 rval = st_validate_tapemarks(un, st_uscsi_cmd, &dest); 5535 if (rval != 0) { 5536 rval = EIO; 5537 } 5538 break; 5539 } 5540 default: 5541 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 5542 "st_ioctl: unknown ioctl\n"); 5543 rval = ENOTTY; 5544 } 5545 5546 exit: 5547 if (!(un->un_persistence && un->un_persist_errors)) { 5548 un->un_errno = rval; 5549 } 5550 5551 mutex_exit(ST_MUTEX); 5552 5553 return (rval); 5554 } 5555 5556 5557 /* 5558 * do some MTIOCTOP tape operations 5559 */ 5560 static int 5561 st_mtioctop(struct scsi_tape *un, intptr_t arg, int flag) 5562 { 5563 #ifdef _MULTI_DATAMODEL 5564 /* 5565 * For use when a 32 bit app makes a call into a 5566 * 64 bit ioctl 5567 */ 5568 struct mtop32 mtop_32_for_64; 5569 #endif /* _MULTI_DATAMODEL */ 5570 struct mtop passed; 5571 struct mtlop local; 5572 int rval = 0; 5573 5574 ST_FUNC(ST_DEVINFO, st_mtioctop); 5575 5576 ASSERT(mutex_owned(ST_MUTEX)); 5577 5578 #ifdef _MULTI_DATAMODEL 5579 switch (ddi_model_convert_from(flag & FMODELS)) { 5580 case DDI_MODEL_ILP32: 5581 if (ddi_copyin((void *)arg, &mtop_32_for_64, 5582 sizeof (struct mtop32), flag)) { 5583 return (EFAULT); 5584 } 5585 local.mt_op = mtop_32_for_64.mt_op; 5586 local.mt_count = (int64_t)mtop_32_for_64.mt_count; 5587 break; 5588 5589 case DDI_MODEL_NONE: 5590 if (ddi_copyin((void *)arg, &passed, sizeof (passed), flag)) { 5591 return (EFAULT); 5592 } 5593 local.mt_op = passed.mt_op; 5594 /* prevent sign extention */ 5595 local.mt_count = (UINT32_MAX & passed.mt_count); 5596 break; 5597 } 5598 5599 #else /* ! _MULTI_DATAMODEL */ 5600 if (ddi_copyin((void *)arg, &passed, sizeof (passed), flag)) { 5601 return (EFAULT); 5602 } 5603 local.mt_op = passed.mt_op; 5604 /* prevent sign extention */ 5605 local.mt_count = (UINT32_MAX & passed.mt_count); 5606 #endif /* _MULTI_DATAMODEL */ 5607 5608 rval = st_do_mtioctop(un, &local); 5609 5610 #ifdef _MULTI_DATAMODEL 5611 switch (ddi_model_convert_from(flag & FMODELS)) { 5612 case DDI_MODEL_ILP32: 5613 if (((uint64_t)local.mt_count) > UINT32_MAX) { 5614 rval = ERANGE; 5615 break; 5616 } 5617 /* 5618 * Convert 64 bit back to 32 bit before doing 5619 * copyout. This is what the ILP32 app expects. 5620 */ 5621 mtop_32_for_64.mt_op = local.mt_op; 5622 mtop_32_for_64.mt_count = local.mt_count; 5623 5624 if (ddi_copyout(&mtop_32_for_64, (void *)arg, 5625 sizeof (struct mtop32), flag)) { 5626 rval = EFAULT; 5627 } 5628 break; 5629 5630 case DDI_MODEL_NONE: 5631 passed.mt_count = local.mt_count; 5632 passed.mt_op = local.mt_op; 5633 if (ddi_copyout(&passed, (void *)arg, sizeof (passed), flag)) { 5634 rval = EFAULT; 5635 } 5636 break; 5637 } 5638 #else /* ! _MULTI_DATAMODE */ 5639 if (((uint64_t)local.mt_count) > UINT32_MAX) { 5640 rval = ERANGE; 5641 } else { 5642 passed.mt_op = local.mt_op; 5643 passed.mt_count = local.mt_count; 5644 if (ddi_copyout(&passed, (void *)arg, sizeof (passed), flag)) { 5645 rval = EFAULT; 5646 } 5647 } 5648 #endif /* _MULTI_DATAMODE */ 5649 5650 5651 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 5652 "st_ioctl: fileno=%x, blkno=%x, eof=%x\n", un->un_pos.fileno, 5653 un->un_pos.blkno, un->un_pos.eof); 5654 5655 if (un->un_pos.pmode == invalid) { 5656 un->un_density_known = 0; 5657 } 5658 5659 ASSERT(mutex_owned(ST_MUTEX)); 5660 return (rval); 5661 } 5662 5663 static int 5664 st_mtiocltop(struct scsi_tape *un, intptr_t arg, int flag) 5665 { 5666 struct mtlop local; 5667 int rval; 5668 5669 ST_FUNC(ST_DEVINFO, st_mtiocltop); 5670 if (ddi_copyin((void *)arg, &local, sizeof (local), flag)) { 5671 return (EFAULT); 5672 } 5673 5674 rval = st_do_mtioctop(un, &local); 5675 5676 if (ddi_copyout(&local, (void *)arg, sizeof (local), flag)) { 5677 rval = EFAULT; 5678 } 5679 return (rval); 5680 } 5681 5682 5683 static int 5684 st_do_mtioctop(struct scsi_tape *un, struct mtlop *mtop) 5685 { 5686 dev_t dev = un->un_dev; 5687 int savefile; 5688 int rval = 0; 5689 5690 ST_FUNC(ST_DEVINFO, st_do_mtioctop); 5691 5692 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 5693 "st_do_mtioctop(): mt_op=%x\n", mtop->mt_op); 5694 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 5695 "fileno=%x, blkno=%x, eof=%x\n", 5696 un->un_pos.fileno, un->un_pos.blkno, un->un_pos.eof); 5697 5698 un->un_status = 0; 5699 5700 /* 5701 * if we are going to mess with a tape, we have to make sure we have 5702 * one and are not offline (i.e. no tape is initialized). We let 5703 * commands pass here that don't actually touch the tape, except for 5704 * loading and initialization (rewinding). 5705 */ 5706 if (un->un_state == ST_STATE_OFFLINE) { 5707 switch (mtop->mt_op) { 5708 case MTLOAD: 5709 case MTNOP: 5710 /* 5711 * We don't want strategy calling st_tape_init here, 5712 * so, change state 5713 */ 5714 un->un_state = ST_STATE_INITIALIZING; 5715 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5716 "st_do_mtioctop : OFFLINE state = %d\n", 5717 un->un_state); 5718 break; 5719 default: 5720 /* 5721 * reinitialize by normal means 5722 */ 5723 rval = st_tape_init(un); 5724 if (rval) { 5725 un->un_state = ST_STATE_INITIALIZING; 5726 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5727 "st_do_mtioctop : OFFLINE init failure "); 5728 un->un_state = ST_STATE_OFFLINE; 5729 un->un_pos.pmode = invalid; 5730 if (rval != EACCES) { 5731 rval = EIO; 5732 } 5733 return (rval); 5734 } 5735 un->un_state = ST_STATE_OPEN_PENDING_IO; 5736 break; 5737 } 5738 } 5739 5740 /* 5741 * If the file position is invalid, allow only those 5742 * commands that properly position the tape and fail 5743 * the rest with EIO 5744 */ 5745 if (un->un_pos.pmode == invalid) { 5746 switch (mtop->mt_op) { 5747 case MTWEOF: 5748 case MTRETEN: 5749 case MTERASE: 5750 case MTEOM: 5751 case MTFSF: 5752 case MTFSR: 5753 case MTBSF: 5754 case MTNBSF: 5755 case MTBSR: 5756 case MTSRSZ: 5757 case MTGRSZ: 5758 case MTSEEK: 5759 case MTBSSF: 5760 case MTFSSF: 5761 return (EIO); 5762 /* NOTREACHED */ 5763 case MTREW: 5764 case MTLOAD: 5765 case MTOFFL: 5766 case MTNOP: 5767 case MTTELL: 5768 case MTLOCK: 5769 case MTUNLOCK: 5770 break; 5771 5772 default: 5773 return (ENOTTY); 5774 /* NOTREACHED */ 5775 } 5776 } 5777 5778 switch (mtop->mt_op) { 5779 case MTERASE: 5780 /* 5781 * MTERASE rewinds the tape, erase it completely, and returns 5782 * to the beginning of the tape 5783 */ 5784 if (un->un_mspl->wp || un->un_read_only & WORM) { 5785 un->un_status = KEY_WRITE_PROTECT; 5786 un->un_err_resid = mtop->mt_count; 5787 COPY_POS(&un->un_err_pos, &un->un_pos); 5788 return (EACCES); 5789 } 5790 if (un->un_dp->options & ST_REEL) { 5791 un->un_fmneeded = 2; 5792 } else { 5793 un->un_fmneeded = 1; 5794 } 5795 mtop->mt_count = mtop->mt_count ? 1 : 0; 5796 if (st_check_density_or_wfm(dev, 1, B_WRITE, NO_STEPBACK) || 5797 st_cmd(un, SCMD_REWIND, 0, SYNC_CMD) || 5798 st_cmd(un, SCMD_ERASE, mtop->mt_count, SYNC_CMD)) { 5799 un->un_pos.pmode = invalid; 5800 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5801 "st_do_mtioctop : EIO space or erase or " 5802 "check den)\n"); 5803 rval = EIO; 5804 } else { 5805 /* QIC and helical scan rewind after erase */ 5806 if (un->un_dp->options & ST_REEL) { 5807 (void) st_cmd(un, SCMD_REWIND, 0, ASYNC_CMD); 5808 } 5809 } 5810 break; 5811 5812 case MTWEOF: 5813 /* 5814 * write an end-of-file record 5815 */ 5816 if (un->un_mspl->wp || un->un_read_only & RDONLY) { 5817 un->un_status = KEY_WRITE_PROTECT; 5818 un->un_err_resid = mtop->mt_count; 5819 COPY_POS(&un->un_err_pos, &un->un_pos); 5820 return (EACCES); 5821 } 5822 5823 /* 5824 * zero count means just flush buffers 5825 * negative count is not permitted 5826 */ 5827 if (mtop->mt_count < 0) { 5828 return (EINVAL); 5829 } 5830 5831 /* Not on worm */ 5832 if (un->un_read_only == RDWR) { 5833 un->un_test_append = 1; 5834 } 5835 5836 if (un->un_state == ST_STATE_OPEN_PENDING_IO) { 5837 if (st_determine_density(un, B_WRITE)) { 5838 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5839 "st_do_mtioctop : EIO : MTWEOF can't " 5840 "determine density"); 5841 return (EIO); 5842 } 5843 } 5844 5845 rval = st_write_fm(dev, (int)mtop->mt_count); 5846 if ((rval != 0) && (rval != EACCES)) { 5847 /* 5848 * Failure due to something other than illegal 5849 * request results in loss of state (st_intr). 5850 */ 5851 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5852 "st_do_mtioctop : EIO : MTWEOF can't write " 5853 "file mark"); 5854 rval = EIO; 5855 } 5856 break; 5857 5858 case MTRETEN: 5859 /* 5860 * retension the tape 5861 */ 5862 if (st_check_density_or_wfm(dev, 1, 0, NO_STEPBACK) || 5863 st_cmd(un, SCMD_LOAD, LD_LOAD | LD_RETEN, SYNC_CMD)) { 5864 un->un_pos.pmode = invalid; 5865 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5866 "st_do_mtioctop : EIO : MTRETEN "); 5867 rval = EIO; 5868 } 5869 break; 5870 5871 case MTREW: 5872 /* 5873 * rewind the tape 5874 */ 5875 if (st_check_density_or_wfm(dev, 1, 0, NO_STEPBACK)) { 5876 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5877 "st_do_mtioctop : EIO:MTREW check " 5878 "density/wfm failed"); 5879 return (EIO); 5880 } 5881 if (st_cmd(un, SCMD_REWIND, 0, SYNC_CMD)) { 5882 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5883 "st_do_mtioctop : EIO : MTREW "); 5884 rval = EIO; 5885 } 5886 break; 5887 5888 case MTOFFL: 5889 /* 5890 * rewinds, and, if appropriate, takes the device offline by 5891 * unloading the tape 5892 */ 5893 if (st_check_density_or_wfm(dev, 1, 0, NO_STEPBACK)) { 5894 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5895 "st_do_mtioctop :EIO:MTOFFL check " 5896 "density/wfm failed"); 5897 return (EIO); 5898 } 5899 (void) st_cmd(un, SCMD_REWIND, 0, SYNC_CMD); 5900 if (st_cmd(un, SCMD_LOAD, LD_UNLOAD, SYNC_CMD)) { 5901 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5902 "st_do_mtioctop : EIO : MTOFFL"); 5903 return (EIO); 5904 } 5905 un->un_pos.eof = ST_NO_EOF; 5906 un->un_laststate = un->un_state; 5907 un->un_state = ST_STATE_OFFLINE; 5908 un->un_mediastate = MTIO_EJECTED; 5909 break; 5910 5911 case MTLOAD: 5912 /* 5913 * This is to load a tape into the drive 5914 * Note that if the tape is not loaded, the device will have 5915 * to be opened via O_NDELAY or O_NONBLOCK. 5916 */ 5917 /* 5918 * Let's try and clean things up, if we are not 5919 * initializing, and then send in the load command, no 5920 * matter what. 5921 * 5922 * load after a media change by the user. 5923 */ 5924 5925 if (un->un_state > ST_STATE_INITIALIZING) { 5926 (void) st_check_density_or_wfm(dev, 1, 0, NO_STEPBACK); 5927 } 5928 rval = st_cmd(un, SCMD_LOAD, LD_LOAD, SYNC_CMD); 5929 /* Load command to a drive that doesn't support load */ 5930 if ((rval == EIO) && 5931 ((un->un_status == KEY_NOT_READY) && 5932 /* Medium not present */ 5933 (un->un_uscsi_rqs_buf->es_add_code == 0x3a) || 5934 ((un->un_status == KEY_ILLEGAL_REQUEST) && 5935 (un->un_dp->type == MT_ISSTK9840) && 5936 /* CSL not present */ 5937 (un->un_uscsi_rqs_buf->es_add_code == 0x80)))) { 5938 rval = ENOTTY; 5939 break; 5940 } else if (rval != EACCES && rval != 0) { 5941 rval = EIO; 5942 } 5943 if (rval) { 5944 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5945 "st_do_mtioctop : %s : MTLOAD\n", 5946 rval == EACCES ? "EACCES" : "EIO"); 5947 /* 5948 * If load tape fails, who knows what happened... 5949 */ 5950 un->un_pos.pmode = invalid; 5951 break; 5952 } 5953 5954 /* 5955 * reset all counters appropriately using rewind, as if LOAD 5956 * succeeds, we are at BOT 5957 */ 5958 un->un_state = ST_STATE_INITIALIZING; 5959 5960 rval = st_tape_init(un); 5961 if ((rval == EACCES) && (un->un_read_only & WORM)) { 5962 rval = 0; 5963 break; 5964 } 5965 5966 if (rval != 0) { 5967 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5968 "st_do_mtioctop : EIO : MTLOAD calls " 5969 "st_tape_init\n"); 5970 rval = EIO; 5971 un->un_state = ST_STATE_OFFLINE; 5972 } 5973 5974 break; 5975 5976 case MTNOP: 5977 un->un_status = 0; /* Reset status */ 5978 un->un_err_resid = 0; 5979 mtop->mt_count = MTUNIT(dev); 5980 break; 5981 5982 case MTEOM: 5983 /* 5984 * positions the tape at a location just after the last file 5985 * written on the tape. For cartridge and 8 mm, this after 5986 * the last file mark; for reel, this is inbetween the two 5987 * last 2 file marks 5988 */ 5989 if ((un->un_pos.pmode == legacy && un->un_pos.eof >= ST_EOT) || 5990 (un->un_lastop == ST_OP_WRITE) || 5991 (un->un_lastop == ST_OP_WEOF)) { 5992 /* 5993 * If the command wants to move to logical end 5994 * of media, and we're already there, we're done. 5995 * If we were at logical eot, we reset the state 5996 * to be *not* at logical eot. 5997 * 5998 * If we're at physical or logical eot, we prohibit 5999 * forward space operations (unconditionally). 6000 * 6001 * Also if the last operation was a write of any 6002 * kind the tape is at EOD. 6003 */ 6004 return (0); 6005 } 6006 /* 6007 * physical tape position may not be what we've been 6008 * telling the user; adjust the request accordingly 6009 */ 6010 if (IN_EOF(un->un_pos)) { 6011 un->un_pos.fileno++; 6012 un->un_pos.blkno = 0; 6013 } 6014 6015 if (st_check_density_or_wfm(dev, 1, B_READ, NO_STEPBACK)) { 6016 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 6017 "st_do_mtioctop : EIO:MTEOM check density/wfm " 6018 " failed"); 6019 return (EIO); 6020 } 6021 6022 /* 6023 * st_find_eod() returns the last fileno we knew about; 6024 */ 6025 savefile = st_find_eod(un); 6026 6027 if ((un->un_status != KEY_BLANK_CHECK) && 6028 (un->un_status != SUN_KEY_EOT)) { 6029 un->un_pos.pmode = invalid; 6030 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 6031 "st_do_mtioctop : EIO : MTEOM status check failed"); 6032 rval = EIO; 6033 } else { 6034 /* 6035 * For 1/2" reel tapes assume logical EOT marked 6036 * by two file marks or we don't care that we may 6037 * be extending the last file on the tape. 6038 */ 6039 if (un->un_dp->options & ST_REEL) { 6040 if (st_cmd(un, SCMD_SPACE, Fmk(-1), SYNC_CMD)) { 6041 un->un_pos.pmode = invalid; 6042 ST_DEBUG2(ST_DEVINFO, st_label, 6043 SCSI_DEBUG, 6044 "st_do_mtioctop : EIO : MTEOM space" 6045 " cmd failed"); 6046 rval = EIO; 6047 break; 6048 } 6049 /* 6050 * Fix up the block number. 6051 */ 6052 un->un_pos.blkno = 0; 6053 un->un_err_pos.blkno = 0; 6054 } 6055 un->un_err_resid = 0; 6056 un->un_pos.fileno = savefile; 6057 un->un_pos.eof = ST_EOT; 6058 } 6059 un->un_status = 0; 6060 break; 6061 6062 case MTFSF: 6063 MAX_SPACE_CNT(mtop->mt_count); 6064 rval = st_mtfsf_ioctl(un, mtop->mt_count); 6065 break; 6066 6067 case MTFSR: 6068 MAX_SPACE_CNT(mtop->mt_count); 6069 rval = st_mtfsr_ioctl(un, mtop->mt_count); 6070 break; 6071 6072 case MTBSF: 6073 MAX_SPACE_CNT(mtop->mt_count); 6074 rval = st_mtbsf_ioctl(un, mtop->mt_count); 6075 break; 6076 6077 case MTNBSF: 6078 MAX_SPACE_CNT(mtop->mt_count); 6079 rval = st_mtnbsf_ioctl(un, mtop->mt_count); 6080 break; 6081 6082 case MTBSR: 6083 MAX_SPACE_CNT(mtop->mt_count); 6084 rval = st_mtbsr_ioctl(un, mtop->mt_count); 6085 break; 6086 6087 case MTBSSF: 6088 MAX_SPACE_CNT(mtop->mt_count); 6089 rval = st_mtbsfm_ioctl(un, mtop->mt_count); 6090 break; 6091 6092 case MTFSSF: 6093 MAX_SPACE_CNT(mtop->mt_count); 6094 rval = st_mtfsfm_ioctl(un, mtop->mt_count); 6095 break; 6096 6097 case MTSRSZ: 6098 6099 /* 6100 * Set record-size to that sent by user 6101 * Check to see if there is reason that the requested 6102 * block size should not be set. 6103 */ 6104 6105 /* If requesting variable block size is it ok? */ 6106 if ((mtop->mt_count == 0) && 6107 ((un->un_dp->options & ST_VARIABLE) == 0)) { 6108 return (ENOTTY); 6109 } 6110 6111 /* 6112 * If requested block size is not variable "0", 6113 * is it less then minimum. 6114 */ 6115 if ((mtop->mt_count != 0) && 6116 (mtop->mt_count < un->un_minbsize)) { 6117 return (EINVAL); 6118 } 6119 6120 /* Is the requested block size more then maximum */ 6121 if ((mtop->mt_count > min(un->un_maxbsize, un->un_maxdma)) && 6122 (un->un_maxbsize != 0)) { 6123 return (EINVAL); 6124 } 6125 6126 /* Is requested block size a modulus the device likes */ 6127 if ((mtop->mt_count % un->un_data_mod) != 0) { 6128 return (EINVAL); 6129 } 6130 6131 if (st_change_block_size(un, (uint32_t)mtop->mt_count) != 0) { 6132 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 6133 "st_ioctl : MTSRSZ : EIO : cant set block size"); 6134 return (EIO); 6135 } 6136 6137 return (0); 6138 6139 case MTGRSZ: 6140 /* 6141 * Get record-size to the user 6142 */ 6143 mtop->mt_count = un->un_bsize; 6144 rval = 0; 6145 break; 6146 6147 case MTTELL: 6148 rval = st_update_block_pos(un, st_cmd, 0); 6149 mtop->mt_count = un->un_pos.lgclblkno; 6150 break; 6151 6152 case MTSEEK: 6153 rval = st_logical_block_locate(un, st_uscsi_cmd, &un->un_pos, 6154 (uint64_t)mtop->mt_count, un->un_pos.partition); 6155 /* 6156 * This bit of magic make mt print the actual position if 6157 * the resulting position was not what was asked for. 6158 */ 6159 if (rval == ESPIPE) { 6160 rval = EIO; 6161 if ((uint64_t)mtop->mt_count != un->un_pos.lgclblkno) { 6162 mtop->mt_op = MTTELL; 6163 mtop->mt_count = un->un_pos.lgclblkno; 6164 } 6165 } 6166 break; 6167 6168 case MTLOCK: 6169 if (st_cmd(un, SCMD_DOORLOCK, MR_LOCK, SYNC_CMD)) { 6170 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 6171 "st_do_mtioctop : EIO : MTLOCK"); 6172 rval = EIO; 6173 } 6174 break; 6175 6176 case MTUNLOCK: 6177 if (st_cmd(un, SCMD_DOORLOCK, MR_UNLOCK, SYNC_CMD)) { 6178 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 6179 "st_do_mtioctop : EIO : MTUNLOCK"); 6180 rval = EIO; 6181 } 6182 break; 6183 6184 default: 6185 rval = ENOTTY; 6186 } 6187 6188 return (rval); 6189 } 6190 6191 6192 /* 6193 * Run a command for uscsi ioctl. 6194 */ 6195 static int 6196 st_uscsi_cmd(struct scsi_tape *un, struct uscsi_cmd *ucmd, int flag) 6197 { 6198 struct uscsi_cmd *uscmd; 6199 struct buf *bp; 6200 enum uio_seg uioseg; 6201 int offline_state = 0; 6202 int err = 0; 6203 dev_t dev = un->un_dev; 6204 6205 ST_FUNC(ST_DEVINFO, st_uscsi_cmd); 6206 6207 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 6208 "st_uscsi_cmd(dev = 0x%lx)\n", un->un_dev); 6209 6210 ASSERT(mutex_owned(ST_MUTEX)); 6211 6212 /* 6213 * We really don't know what commands are coming in here and 6214 * we don't want to limit the commands coming in. 6215 * 6216 * If st_tape_init() gets called from st_strategy(), then we 6217 * will hang the process waiting for un->un_sbuf_busy to be cleared, 6218 * which it never will, as we set it below. To prevent 6219 * st_tape_init() from getting called, we have to set state to other 6220 * than ST_STATE_OFFLINE, so we choose ST_STATE_INITIALIZING, which 6221 * achieves this purpose already. 6222 * 6223 * We use offline_state to preserve the OFFLINE state, if it exists, 6224 * so other entry points to the driver might have the chance to call 6225 * st_tape_init(). 6226 */ 6227 if (un->un_state == ST_STATE_OFFLINE) { 6228 un->un_laststate = ST_STATE_OFFLINE; 6229 un->un_state = ST_STATE_INITIALIZING; 6230 offline_state = 1; 6231 } 6232 6233 mutex_exit(ST_MUTEX); 6234 err = scsi_uscsi_alloc_and_copyin((intptr_t)ucmd, flag, ROUTE, &uscmd); 6235 mutex_enter(ST_MUTEX); 6236 if (err != 0) { 6237 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 6238 "st_uscsi_cmd: scsi_uscsi_alloc_and_copyin failed\n"); 6239 goto exit; 6240 } 6241 6242 uioseg = (flag & FKIOCTL) ? UIO_SYSSPACE : UIO_USERSPACE; 6243 6244 /* check to see if this command requires the drive to be reserved */ 6245 if (uscmd->uscsi_cdb != NULL) { 6246 err = st_check_cdb_for_need_to_reserve(un, 6247 (uchar_t *)uscmd->uscsi_cdb); 6248 if (err) { 6249 goto exit_free; 6250 } 6251 /* 6252 * If this is a space command we need to save the starting 6253 * point so we can retry from there if the command fails. 6254 */ 6255 if ((uscmd->uscsi_cdb[0] == SCMD_SPACE) || 6256 (uscmd->uscsi_cdb[0] == (char)SCMD_SPACE_G4)) { 6257 (void) st_update_block_pos(un, st_cmd, 0); 6258 } 6259 } 6260 6261 /* 6262 * Forground should not be doing anything while recovery is active. 6263 */ 6264 ASSERT(un->un_recov_buf_busy == 0); 6265 6266 /* 6267 * Get buffer resources... 6268 */ 6269 while (un->un_sbuf_busy) 6270 cv_wait(&un->un_sbuf_cv, ST_MUTEX); 6271 un->un_sbuf_busy = 1; 6272 6273 #ifdef STDEBUG 6274 if ((uscmd->uscsi_cdb != NULL) && (st_debug & 0x7) > 6) { 6275 int rw = (uscmd->uscsi_flags & USCSI_READ) ? B_READ : B_WRITE; 6276 st_print_cdb(ST_DEVINFO, st_label, SCSI_DEBUG, 6277 "uscsi cdb", uscmd->uscsi_cdb); 6278 if (uscmd->uscsi_buflen) { 6279 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 6280 "uscsi %s of %ld bytes %s %s space\n", 6281 (rw == B_READ) ? rd_str : wr_str, 6282 uscmd->uscsi_buflen, 6283 (rw == B_READ) ? "to" : "from", 6284 (uioseg == UIO_SYSSPACE) ? "system" : "user"); 6285 } 6286 } 6287 #endif /* STDEBUG */ 6288 6289 /* 6290 * Although st_uscsi_cmd() never makes use of these 6291 * now, we are just being safe and consistent. 6292 */ 6293 uscmd->uscsi_flags &= ~(USCSI_NOINTR | USCSI_NOPARITY | 6294 USCSI_OTAG | USCSI_HTAG | USCSI_HEAD); 6295 6296 un->un_srqbufp = uscmd->uscsi_rqbuf; 6297 bp = un->un_sbufp; 6298 bzero(bp, sizeof (buf_t)); 6299 if (uscmd->uscsi_cdb != NULL) { 6300 bp->b_forw = (struct buf *)(uintptr_t)uscmd->uscsi_cdb[0]; 6301 } 6302 bp->b_back = (struct buf *)uscmd; 6303 6304 mutex_exit(ST_MUTEX); 6305 err = scsi_uscsi_handle_cmd(dev, uioseg, uscmd, st_strategy, bp, NULL); 6306 mutex_enter(ST_MUTEX); 6307 6308 /* 6309 * If scsi reset successful, don't write any filemarks. 6310 */ 6311 if ((err == 0) && (uscmd->uscsi_flags & 6312 (USCSI_RESET_LUN | USCSI_RESET_TARGET | USCSI_RESET_ALL))) { 6313 un->un_fmneeded = 0; 6314 } 6315 6316 exit_free: 6317 /* 6318 * Free resources 6319 */ 6320 un->un_sbuf_busy = 0; 6321 un->un_srqbufp = NULL; 6322 6323 /* 6324 * If was a space command need to update logical block position. 6325 * If the command failed such that positioning is invalid, Don't 6326 * update the position as the user must do this to validate the 6327 * position for data protection. 6328 */ 6329 if ((uscmd->uscsi_cdb != NULL) && 6330 ((uscmd->uscsi_cdb[0] == SCMD_SPACE) || 6331 (uscmd->uscsi_cdb[0] == (char)SCMD_SPACE_G4)) && 6332 (un->un_pos.pmode != invalid)) { 6333 un->un_running.pmode = invalid; 6334 (void) st_update_block_pos(un, st_cmd, 1); 6335 /* 6336 * Set running position to invalid so it updates on the 6337 * next command. 6338 */ 6339 un->un_running.pmode = invalid; 6340 } 6341 cv_signal(&un->un_sbuf_cv); 6342 mutex_exit(ST_MUTEX); 6343 (void) scsi_uscsi_copyout_and_free((intptr_t)ucmd, uscmd); 6344 mutex_enter(ST_MUTEX); 6345 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 6346 "st_uscsi_cmd returns 0x%x\n", err); 6347 6348 exit: 6349 /* don't lose offline state */ 6350 if (offline_state) { 6351 un->un_state = ST_STATE_OFFLINE; 6352 } 6353 6354 ASSERT(mutex_owned(ST_MUTEX)); 6355 return (err); 6356 } 6357 6358 static int 6359 st_write_fm(dev_t dev, int wfm) 6360 { 6361 int i; 6362 int rval; 6363 6364 GET_SOFT_STATE(dev); 6365 6366 ST_FUNC(ST_DEVINFO, st_write_fm); 6367 6368 ASSERT(mutex_owned(ST_MUTEX)); 6369 6370 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 6371 "st_write_fm(dev = 0x%lx, wfm = %d)\n", dev, wfm); 6372 6373 /* 6374 * write one filemark at the time after EOT 6375 */ 6376 if (un->un_pos.eof >= ST_EOT) { 6377 for (i = 0; i < wfm; i++) { 6378 rval = st_cmd(un, SCMD_WRITE_FILE_MARK, 1, SYNC_CMD); 6379 if (rval == EACCES) { 6380 return (rval); 6381 } 6382 if (rval != 0) { 6383 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 6384 "st_write_fm : EIO : write EOT file mark"); 6385 return (EIO); 6386 } 6387 } 6388 } else { 6389 rval = st_cmd(un, SCMD_WRITE_FILE_MARK, wfm, SYNC_CMD); 6390 if (rval == EACCES) { 6391 return (rval); 6392 } 6393 if (rval) { 6394 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 6395 "st_write_fm : EIO : write file mark"); 6396 return (EIO); 6397 } 6398 } 6399 6400 ASSERT(mutex_owned(ST_MUTEX)); 6401 return (0); 6402 } 6403 6404 #ifdef STDEBUG 6405 static void 6406 st_start_dump(struct scsi_tape *un, struct buf *bp) 6407 { 6408 struct scsi_pkt *pkt = BP_PKT(bp); 6409 uchar_t *cdbp = (uchar_t *)pkt->pkt_cdbp; 6410 6411 ST_FUNC(ST_DEVINFO, st_start_dump); 6412 6413 if ((st_debug & 0x7) < 6) 6414 return; 6415 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 6416 "st_start: cmd=0x%p count=%ld resid=%ld flags=0x%x pkt=0x%p\n", 6417 (void *)bp->b_forw, bp->b_bcount, 6418 bp->b_resid, bp->b_flags, (void *)BP_PKT(bp)); 6419 st_print_cdb(ST_DEVINFO, st_label, SCSI_DEBUG, 6420 "st_start: cdb", (caddr_t)cdbp); 6421 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 6422 "st_start: fileno=%d, blk=%d\n", 6423 un->un_pos.fileno, un->un_pos.blkno); 6424 } 6425 #endif 6426 6427 6428 /* 6429 * Command start && done functions 6430 */ 6431 6432 /* 6433 * st_start() 6434 * 6435 * Called from: 6436 * st_strategy() to start a command. 6437 * st_runout() to retry when scsi_pkt allocation fails on previous attempt(s). 6438 * st_attach() when resuming from power down state. 6439 * st_start_restart() to retry transport when device was previously busy. 6440 * st_done_and_mutex_exit() to start the next command when previous is done. 6441 * 6442 * On entry: 6443 * scsi_pkt may or may not be allocated. 6444 * 6445 */ 6446 static void 6447 st_start(struct scsi_tape *un) 6448 { 6449 struct buf *bp; 6450 int status; 6451 int queued; 6452 6453 ST_FUNC(ST_DEVINFO, st_start); 6454 ASSERT(mutex_owned(ST_MUTEX)); 6455 6456 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 6457 "st_start(): dev = 0x%lx\n", un->un_dev); 6458 6459 if (un->un_recov_buf_busy) { 6460 /* recovery commands can happen anytime */ 6461 bp = un->un_recov_buf; 6462 queued = 0; 6463 } else if (un->un_sbuf_busy) { 6464 /* sbuf commands should only happen with an empty queue. */ 6465 ASSERT(un->un_quef == NULL); 6466 ASSERT(un->un_runqf == NULL); 6467 bp = un->un_sbufp; 6468 queued = 0; 6469 } else if (un->un_quef != NULL) { 6470 if (un->un_persistence && un->un_persist_errors) { 6471 return; 6472 } 6473 bp = un->un_quef; 6474 queued = 1; 6475 } else { 6476 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 6477 "st_start() returning no buf found\n"); 6478 return; 6479 } 6480 6481 ASSERT((bp->b_flags & B_DONE) == 0); 6482 6483 /* 6484 * Don't send more than un_throttle commands to the HBA 6485 */ 6486 if ((un->un_throttle <= 0) || (un->un_ncmds >= un->un_throttle)) { 6487 /* 6488 * if doing recovery we know there is outstanding commands. 6489 */ 6490 if (bp != un->un_recov_buf) { 6491 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 6492 "st_start returning throttle = %d or ncmds = %d\n", 6493 un->un_throttle, un->un_ncmds); 6494 if (un->un_ncmds == 0) { 6495 typedef void (*func)(); 6496 func fnc = (func)st_runout; 6497 6498 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 6499 "Sending delayed start to st_runout()\n"); 6500 mutex_exit(ST_MUTEX); 6501 (void) timeout(fnc, un, drv_usectohz(1000000)); 6502 mutex_enter(ST_MUTEX); 6503 } 6504 return; 6505 } 6506 } 6507 6508 /* 6509 * If the buf has no scsi_pkt call st_make_cmd() to get one and 6510 * build the command. 6511 */ 6512 if (BP_PKT(bp) == NULL) { 6513 ASSERT((bp->b_flags & B_DONE) == 0); 6514 st_make_cmd(un, bp, st_runout); 6515 ASSERT((bp->b_flags & B_DONE) == 0); 6516 status = geterror(bp); 6517 6518 /* 6519 * Some HBA's don't call bioerror() to set an error. 6520 * And geterror() returns zero if B_ERROR is not set. 6521 * So if we get zero we must check b_error. 6522 */ 6523 if (status == 0 && bp->b_error != 0) { 6524 status = bp->b_error; 6525 bioerror(bp, status); 6526 } 6527 6528 /* 6529 * Some HBA's convert DDI_DMA_NORESOURCES into ENOMEM. 6530 * In tape ENOMEM has special meaning so we'll change it. 6531 */ 6532 if (status == ENOMEM) { 6533 status = 0; 6534 bioerror(bp, status); 6535 } 6536 6537 /* 6538 * Did it fail and is it retryable? 6539 * If so return and wait for the callback through st_runout. 6540 * Also looks like scsi_init_pkt() will setup a callback even 6541 * if it isn't retryable. 6542 */ 6543 if (BP_PKT(bp) == NULL) { 6544 if (status == 0) { 6545 /* 6546 * If first attempt save state. 6547 */ 6548 if (un->un_state != ST_STATE_RESOURCE_WAIT) { 6549 un->un_laststate = un->un_state; 6550 un->un_state = ST_STATE_RESOURCE_WAIT; 6551 } 6552 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 6553 "temp no resources for pkt\n"); 6554 } else if (status == EINVAL) { 6555 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 6556 "scsi_init_pkt rejected pkt as too big\n"); 6557 if (un->un_persistence) { 6558 st_set_pe_flag(un); 6559 } 6560 } else { 6561 /* 6562 * Unlikely that it would be retryable then not. 6563 */ 6564 if (un->un_state == ST_STATE_RESOURCE_WAIT) { 6565 un->un_state = un->un_laststate; 6566 } 6567 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 6568 "perm no resources for pkt errno = 0x%x\n", 6569 status); 6570 } 6571 return; 6572 } 6573 /* 6574 * Worked this time set the state back. 6575 */ 6576 if (un->un_state == ST_STATE_RESOURCE_WAIT) { 6577 un->un_state = un->un_laststate; 6578 } 6579 } 6580 6581 if (queued) { 6582 /* 6583 * move from waitq to runq 6584 */ 6585 (void) st_remove_from_queue(&un->un_quef, &un->un_quel, bp); 6586 st_add_to_queue(&un->un_runqf, &un->un_runql, un->un_runql, bp); 6587 } 6588 6589 6590 #ifdef STDEBUG 6591 st_start_dump(un, bp); 6592 #endif 6593 6594 /* could not get here if throttle was zero */ 6595 un->un_last_throttle = un->un_throttle; 6596 un->un_throttle = 0; /* so nothing else will come in here */ 6597 un->un_ncmds++; 6598 6599 ST_DO_KSTATS(bp, kstat_waitq_to_runq); 6600 6601 status = st_transport(un, BP_PKT(bp)); 6602 6603 if (un->un_last_throttle) { 6604 un->un_throttle = un->un_last_throttle; 6605 } 6606 6607 if (status != TRAN_ACCEPT) { 6608 ST_DO_KSTATS(bp, kstat_runq_back_to_waitq); 6609 ST_DEBUG(ST_DEVINFO, st_label, CE_WARN, 6610 "Unhappy transport packet status 0x%x\n", status); 6611 6612 if (status == TRAN_BUSY) { 6613 pkt_info *pkti = BP_PKT(bp)->pkt_private; 6614 6615 /* 6616 * If command recovery is enabled and this isn't 6617 * a recovery command try command recovery. 6618 */ 6619 if (pkti->privatelen == sizeof (recov_info) && 6620 bp != un->un_recov_buf) { 6621 ST_RECOV(ST_DEVINFO, st_label, CE_WARN, 6622 "Command Recovery called on busy send\n"); 6623 if (st_command_recovery(un, BP_PKT(bp), 6624 ATTEMPT_RETRY) == JUST_RETURN) { 6625 return; 6626 } 6627 } else { 6628 mutex_exit(ST_MUTEX); 6629 if (st_handle_start_busy(un, bp, 6630 ST_TRAN_BUSY_TIMEOUT, queued) == 0) { 6631 mutex_enter(ST_MUTEX); 6632 return; 6633 } 6634 /* 6635 * if too many retries, fail the transport 6636 */ 6637 mutex_enter(ST_MUTEX); 6638 } 6639 } 6640 scsi_log(ST_DEVINFO, st_label, CE_WARN, 6641 "transport rejected %d\n", status); 6642 bp->b_resid = bp->b_bcount; 6643 6644 ST_DO_KSTATS(bp, kstat_waitq_exit); 6645 ST_DO_ERRSTATS(un, st_transerrs); 6646 if ((bp == un->un_recov_buf) && (status == TRAN_BUSY)) { 6647 st_bioerror(bp, EBUSY); 6648 } else { 6649 st_bioerror(bp, EIO); 6650 st_set_pe_flag(un); 6651 } 6652 st_done_and_mutex_exit(un, bp); 6653 mutex_enter(ST_MUTEX); 6654 } 6655 6656 ASSERT(mutex_owned(ST_MUTEX)); 6657 } 6658 6659 /* 6660 * if the transport is busy, then put this bp back on the waitq 6661 */ 6662 static int 6663 st_handle_start_busy(struct scsi_tape *un, struct buf *bp, 6664 clock_t timeout_interval, int queued) 6665 { 6666 6667 pkt_info *pktinfo = BP_PKT(bp)->pkt_private; 6668 6669 ST_FUNC(ST_DEVINFO, st_handle_start_busy); 6670 6671 mutex_enter(ST_MUTEX); 6672 6673 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 6674 "st_handle_start_busy()\n"); 6675 6676 /* 6677 * Check to see if we hit the retry timeout and one last check for 6678 * making sure this is the last on the runq, if it is not, we have 6679 * to fail 6680 */ 6681 if ((pktinfo->str_retry_cnt++ > st_retry_count) || 6682 ((queued) && (un->un_runql != bp))) { 6683 mutex_exit(ST_MUTEX); 6684 return (-1); 6685 } 6686 6687 if (queued) { 6688 /* put the bp back on the waitq */ 6689 st_add_to_queue(&un->un_quef, &un->un_quel, un->un_quef, bp); 6690 } 6691 6692 /* 6693 * Decrement un_ncmds so that this 6694 * gets thru' st_start() again. 6695 */ 6696 un->un_ncmds--; 6697 6698 if (queued) { 6699 /* 6700 * since this is an error case, we won't have to do this list 6701 * walking much. We've already made sure this bp was the 6702 * last on the runq 6703 */ 6704 (void) st_remove_from_queue(&un->un_runqf, &un->un_runql, bp); 6705 6706 /* 6707 * send a marker pkt, if appropriate 6708 */ 6709 st_hba_unflush(un); 6710 6711 } 6712 /* 6713 * all queues are aligned, we are just waiting to 6714 * transport, don't alloc any more buf p's, when 6715 * st_start is reentered. 6716 */ 6717 (void) timeout(st_start_restart, un, timeout_interval); 6718 6719 mutex_exit(ST_MUTEX); 6720 return (0); 6721 } 6722 6723 6724 /* 6725 * st_runout a callback that is called what a resource allocatation failed 6726 */ 6727 static int 6728 st_runout(caddr_t arg) 6729 { 6730 struct scsi_tape *un = (struct scsi_tape *)arg; 6731 struct buf *bp; 6732 int queued; 6733 6734 ASSERT(un != NULL); 6735 6736 ST_FUNC(ST_DEVINFO, st_runout); 6737 6738 mutex_enter(ST_MUTEX); 6739 6740 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, "st_runout()\n"); 6741 6742 if (un->un_recov_buf_busy != 0) { 6743 bp = un->un_recov_buf; 6744 queued = 0; 6745 } else if (un->un_sbuf_busy != 0) { 6746 /* sbuf commands should only happen with an empty queue. */ 6747 ASSERT(un->un_quef == NULL); 6748 ASSERT(un->un_runqf == NULL); 6749 bp = un->un_sbufp; 6750 queued = 0; 6751 } else if (un->un_quef != NULL) { 6752 bp = un->un_quef; 6753 if (un->un_persistence && un->un_persist_errors) { 6754 mutex_exit(ST_MUTEX); 6755 bp->b_resid = bp->b_bcount; 6756 biodone(bp); 6757 return (1); 6758 } 6759 queued = 1; 6760 } else { 6761 ASSERT(1 == 0); 6762 mutex_exit(ST_MUTEX); 6763 return (1); 6764 } 6765 6766 /* 6767 * failed scsi_init_pkt(). If errno is zero its retryable. 6768 */ 6769 if ((bp != NULL) && (geterror(bp) != 0)) { 6770 6771 scsi_log(ST_DEVINFO, st_label, CE_WARN, 6772 "errors after pkt alloc (b_flags=0x%x, b_error=0x%x)\n", 6773 bp->b_flags, geterror(bp)); 6774 ASSERT((bp->b_flags & B_DONE) == 0); 6775 6776 if (queued) { 6777 (void) st_remove_from_queue(&un->un_quef, &un->un_quel, 6778 bp); 6779 } 6780 mutex_exit(ST_MUTEX); 6781 6782 ASSERT((bp->b_flags & B_DONE) == 0); 6783 6784 /* 6785 * Set resid, Error already set, then unblock calling thread. 6786 */ 6787 bp->b_resid = bp->b_bcount; 6788 biodone(bp); 6789 } else { 6790 /* 6791 * Try Again 6792 */ 6793 st_start(un); 6794 mutex_exit(ST_MUTEX); 6795 } 6796 6797 /* 6798 * Comments courtesy of sd.c 6799 * The scsi_init_pkt routine allows for the callback function to 6800 * return a 0 indicating the callback should be rescheduled or a 1 6801 * indicating not to reschedule. This routine always returns 1 6802 * because the driver always provides a callback function to 6803 * scsi_init_pkt. This results in a callback always being scheduled 6804 * (via the scsi_init_pkt callback implementation) if a resource 6805 * failure occurs. 6806 */ 6807 6808 return (1); 6809 } 6810 6811 /* 6812 * st_done_and_mutex_exit() 6813 * - remove bp from runq 6814 * - start up the next request 6815 * - if this was an asynch bp, clean up 6816 * - exit with released mutex 6817 */ 6818 static void 6819 st_done_and_mutex_exit(struct scsi_tape *un, struct buf *bp) 6820 { 6821 int pe_flagged = 0; 6822 struct scsi_pkt *pkt = BP_PKT(bp); 6823 pkt_info *pktinfo = pkt->pkt_private; 6824 6825 ASSERT(MUTEX_HELD(&un->un_sd->sd_mutex)); 6826 #if !defined(lint) 6827 _NOTE(LOCK_RELEASED_AS_SIDE_EFFECT(&un->un_sd->sd_mutex)) 6828 #endif 6829 6830 ST_FUNC(ST_DEVINFO, st_done_and_mutex_exit); 6831 6832 ASSERT(mutex_owned(ST_MUTEX)); 6833 6834 (void) st_remove_from_queue(&un->un_runqf, &un->un_runql, bp); 6835 6836 un->un_ncmds--; 6837 cv_signal(&un->un_queue_cv); 6838 6839 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 6840 "st_done_and_mutex_exit(): cmd=0x%x count=%ld resid=%ld flags=" 6841 "0x%x\n", pkt->pkt_cdbp[0], bp->b_bcount, 6842 bp->b_resid, bp->b_flags); 6843 6844 6845 /* 6846 * update kstats with transfer count info 6847 */ 6848 if (un->un_stats && (bp != un->un_sbufp) && IS_RW(bp)) { 6849 uint32_t n_done = bp->b_bcount - bp->b_resid; 6850 if (bp->b_flags & B_READ) { 6851 IOSP->reads++; 6852 IOSP->nread += n_done; 6853 } else { 6854 IOSP->writes++; 6855 IOSP->nwritten += n_done; 6856 } 6857 } 6858 6859 /* 6860 * Start the next one before releasing resources on this one, if 6861 * there is something on the queue and persistent errors has not been 6862 * flagged 6863 */ 6864 6865 if ((pe_flagged = (un->un_persistence && un->un_persist_errors)) != 0) { 6866 un->un_last_resid = bp->b_resid; 6867 un->un_last_count = bp->b_bcount; 6868 } 6869 6870 if (un->un_pwr_mgmt == ST_PWR_SUSPENDED) { 6871 cv_broadcast(&un->un_tape_busy_cv); 6872 } else if (un->un_quef && un->un_throttle && !pe_flagged && 6873 (bp != un->un_recov_buf)) { 6874 st_start(un); 6875 } 6876 6877 un->un_retry_ct = max(pktinfo->pkt_retry_cnt, pktinfo->str_retry_cnt); 6878 6879 if (bp == un->un_sbufp && (bp->b_flags & B_ASYNC)) { 6880 /* 6881 * Since we marked this ourselves as ASYNC, 6882 * there isn't anybody around waiting for 6883 * completion any more. 6884 */ 6885 uchar_t *cmd = pkt->pkt_cdbp; 6886 if (*cmd == SCMD_READ || *cmd == SCMD_WRITE) { 6887 bp->b_un.b_addr = (caddr_t)0; 6888 } 6889 ST_DEBUG(ST_DEVINFO, st_label, CE_NOTE, 6890 "st_done_and_mutex_exit(async): freeing pkt\n"); 6891 st_print_cdb(ST_DEVINFO, st_label, CE_NOTE, 6892 "CDB sent with B_ASYNC", (caddr_t)cmd); 6893 if (pkt) { 6894 scsi_destroy_pkt(pkt); 6895 } 6896 un->un_sbuf_busy = 0; 6897 cv_signal(&un->un_sbuf_cv); 6898 mutex_exit(ST_MUTEX); 6899 return; 6900 } 6901 6902 if (bp == un->un_sbufp && BP_UCMD(bp)) { 6903 /* 6904 * Copy status from scsi_pkt to uscsi_cmd 6905 * since st_uscsi_cmd needs it 6906 */ 6907 BP_UCMD(bp)->uscsi_status = SCBP_C(BP_PKT(bp)); 6908 } 6909 6910 6911 #ifdef STDEBUG 6912 if (((st_debug & 0x7) >= 4) && 6913 (((un->un_pos.blkno % 100) == 0) || 6914 (un->un_persistence && un->un_persist_errors))) { 6915 6916 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 6917 "st_d_a_m_exit(): ncmds = %d, thr = %d, " 6918 "un_errno = %d, un_pe = %d\n", 6919 un->un_ncmds, un->un_throttle, un->un_errno, 6920 un->un_persist_errors); 6921 } 6922 6923 #endif 6924 6925 mutex_exit(ST_MUTEX); 6926 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 6927 "st_done_and_mutex_exit: freeing pkt\n"); 6928 6929 if (pkt) { 6930 scsi_destroy_pkt(pkt); 6931 } 6932 6933 biodone(bp); 6934 6935 /* 6936 * now that we biodoned that command, if persistent errors have been 6937 * flagged, flush the waitq 6938 */ 6939 if (pe_flagged) 6940 st_flush(un); 6941 } 6942 6943 6944 /* 6945 * Tape error, flush tape driver queue. 6946 */ 6947 static void 6948 st_flush(struct scsi_tape *un) 6949 { 6950 struct buf *bp; 6951 6952 ST_FUNC(ST_DEVINFO, st_flush); 6953 6954 mutex_enter(ST_MUTEX); 6955 6956 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 6957 "st_flush(), ncmds = %d, quef = 0x%p\n", 6958 un->un_ncmds, (void *)un->un_quef); 6959 6960 /* 6961 * if we still have commands outstanding, wait for them to come in 6962 * before flushing the queue, and make sure there is a queue 6963 */ 6964 if (un->un_ncmds || !un->un_quef) 6965 goto exit; 6966 6967 /* 6968 * we have no more commands outstanding, so let's deal with special 6969 * cases in the queue for EOM and FM. If we are here, and un_errno 6970 * is 0, then we know there was no error and we return a 0 read or 6971 * write before showing errors 6972 */ 6973 6974 /* Flush the wait queue. */ 6975 while ((bp = un->un_quef) != NULL) { 6976 un->un_quef = bp->b_actf; 6977 6978 bp->b_resid = bp->b_bcount; 6979 6980 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 6981 "st_flush() : blkno=%d, err=%d, b_bcount=%ld\n", 6982 un->un_pos.blkno, un->un_errno, bp->b_bcount); 6983 6984 st_set_pe_errno(un); 6985 6986 bioerror(bp, un->un_errno); 6987 6988 mutex_exit(ST_MUTEX); 6989 /* it should have one, but check anyway */ 6990 if (BP_PKT(bp)) { 6991 scsi_destroy_pkt(BP_PKT(bp)); 6992 } 6993 biodone(bp); 6994 mutex_enter(ST_MUTEX); 6995 } 6996 6997 /* 6998 * It's not a bad practice to reset the 6999 * waitq tail pointer to NULL. 7000 */ 7001 un->un_quel = NULL; 7002 7003 exit: 7004 /* we mucked with the queue, so let others know about it */ 7005 cv_signal(&un->un_queue_cv); 7006 mutex_exit(ST_MUTEX); 7007 } 7008 7009 7010 /* 7011 * Utility functions 7012 */ 7013 static int 7014 st_determine_generic(struct scsi_tape *un) 7015 { 7016 int bsize; 7017 static char *cart = "0.25 inch cartridge"; 7018 char *sizestr; 7019 7020 ST_FUNC(ST_DEVINFO, st_determine_generic); 7021 7022 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 7023 "st_determine_generic(un = 0x%p)\n", (void*)un); 7024 7025 ASSERT(mutex_owned(ST_MUTEX)); 7026 7027 if (st_modesense(un)) { 7028 return (-1); 7029 } 7030 7031 bsize = (un->un_mspl->high_bl << 16) | 7032 (un->un_mspl->mid_bl << 8) | 7033 (un->un_mspl->low_bl); 7034 7035 if (bsize == 0) { 7036 un->un_dp->options |= ST_VARIABLE; 7037 un->un_dp->bsize = 0; 7038 un->un_bsize = 0; 7039 } else if (bsize > ST_MAXRECSIZE_FIXED) { 7040 /* 7041 * record size of this device too big. 7042 * try and convert it to variable record length. 7043 * 7044 */ 7045 un->un_dp->options |= ST_VARIABLE; 7046 if (st_change_block_size(un, 0) != 0) { 7047 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 7048 "Fixed Record Size %d is too large\n", bsize); 7049 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 7050 "Cannot switch to variable record size\n"); 7051 un->un_dp->options &= ~ST_VARIABLE; 7052 return (-1); 7053 } 7054 } else if (st_change_block_size(un, 0) == 0) { 7055 /* 7056 * If the drive was set to a non zero block size, 7057 * See if it can be set to a zero block size. 7058 * If it works, ST_VARIABLE so user can set it as they want. 7059 */ 7060 un->un_dp->options |= ST_VARIABLE; 7061 un->un_dp->bsize = 0; 7062 un->un_bsize = 0; 7063 } else { 7064 un->un_dp->bsize = bsize; 7065 un->un_bsize = bsize; 7066 } 7067 7068 7069 switch (un->un_mspl->density) { 7070 default: 7071 case 0x0: 7072 /* 7073 * default density, cannot determine any other 7074 * information. 7075 */ 7076 sizestr = "Unknown type- assuming 0.25 inch cartridge"; 7077 un->un_dp->type = ST_TYPE_DEFAULT; 7078 un->un_dp->options |= (ST_AUTODEN_OVERRIDE|ST_QIC); 7079 break; 7080 case 0x1: 7081 case 0x2: 7082 case 0x3: 7083 case 0x6: 7084 /* 7085 * 1/2" reel 7086 */ 7087 sizestr = "0.50 inch reel"; 7088 un->un_dp->type = ST_TYPE_REEL; 7089 un->un_dp->options |= ST_REEL; 7090 un->un_dp->densities[0] = 0x1; 7091 un->un_dp->densities[1] = 0x2; 7092 un->un_dp->densities[2] = 0x6; 7093 un->un_dp->densities[3] = 0x3; 7094 break; 7095 case 0x4: 7096 case 0x5: 7097 case 0x7: 7098 case 0x0b: 7099 7100 /* 7101 * Quarter inch. 7102 */ 7103 sizestr = cart; 7104 un->un_dp->type = ST_TYPE_DEFAULT; 7105 un->un_dp->options |= ST_QIC; 7106 7107 un->un_dp->densities[1] = 0x4; 7108 un->un_dp->densities[2] = 0x5; 7109 un->un_dp->densities[3] = 0x7; 7110 un->un_dp->densities[0] = 0x0b; 7111 break; 7112 7113 case 0x0f: 7114 case 0x10: 7115 case 0x11: 7116 case 0x12: 7117 /* 7118 * QIC-120, QIC-150, QIC-320, QIC-600 7119 */ 7120 sizestr = cart; 7121 un->un_dp->type = ST_TYPE_DEFAULT; 7122 un->un_dp->options |= ST_QIC; 7123 un->un_dp->densities[0] = 0x0f; 7124 un->un_dp->densities[1] = 0x10; 7125 un->un_dp->densities[2] = 0x11; 7126 un->un_dp->densities[3] = 0x12; 7127 break; 7128 7129 case 0x09: 7130 case 0x0a: 7131 case 0x0c: 7132 case 0x0d: 7133 /* 7134 * 1/2" cartridge tapes. Include HI-TC. 7135 */ 7136 sizestr = cart; 7137 sizestr[2] = '5'; 7138 sizestr[3] = '0'; 7139 un->un_dp->type = ST_TYPE_HIC; 7140 un->un_dp->densities[0] = 0x09; 7141 un->un_dp->densities[1] = 0x0a; 7142 un->un_dp->densities[2] = 0x0c; 7143 un->un_dp->densities[3] = 0x0d; 7144 break; 7145 7146 case 0x13: 7147 /* DDS-2/DDS-3 scsi spec densities */ 7148 case 0x24: 7149 case 0x25: 7150 case 0x26: 7151 sizestr = "DAT Data Storage (DDS)"; 7152 un->un_dp->type = ST_TYPE_DAT; 7153 un->un_dp->options |= ST_AUTODEN_OVERRIDE; 7154 break; 7155 7156 case 0x14: 7157 /* 7158 * Helical Scan (Exabyte) devices 7159 */ 7160 sizestr = "8mm helical scan cartridge"; 7161 un->un_dp->type = ST_TYPE_EXABYTE; 7162 un->un_dp->options |= ST_AUTODEN_OVERRIDE; 7163 break; 7164 } 7165 7166 /* 7167 * Assume LONG ERASE, BSF and BSR 7168 */ 7169 7170 un->un_dp->options |= 7171 (ST_LONG_ERASE | ST_UNLOADABLE | ST_BSF | ST_BSR | ST_KNOWS_EOD); 7172 7173 /* 7174 * Only if mode sense data says no buffered write, set NOBUF 7175 */ 7176 if (un->un_mspl->bufm == 0) 7177 un->un_dp->options |= ST_NOBUF; 7178 7179 /* 7180 * set up large read and write retry counts 7181 */ 7182 7183 un->un_dp->max_rretries = un->un_dp->max_wretries = 1000; 7184 7185 /* 7186 * If this is a 0.50 inch reel tape, and 7187 * it is *not* variable mode, try and 7188 * set it to variable record length 7189 * mode. 7190 */ 7191 if ((un->un_dp->options & ST_REEL) && un->un_bsize != 0 && 7192 (un->un_dp->options & ST_VARIABLE)) { 7193 if (st_change_block_size(un, 0) == 0) { 7194 un->un_dp->bsize = 0; 7195 un->un_mspl->high_bl = un->un_mspl->mid_bl = 7196 un->un_mspl->low_bl = 0; 7197 } 7198 } 7199 7200 /* 7201 * Write to console about type of device found 7202 */ 7203 ST_DEBUG6(ST_DEVINFO, st_label, CE_NOTE, 7204 "Generic Drive, Vendor=%s\n\t%s", un->un_dp->name, 7205 sizestr); 7206 if (un->un_dp->options & ST_VARIABLE) { 7207 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 7208 "!Variable record length I/O\n"); 7209 } else { 7210 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 7211 "!Fixed record length (%d byte blocks) I/O\n", 7212 un->un_dp->bsize); 7213 } 7214 ASSERT(mutex_owned(ST_MUTEX)); 7215 return (0); 7216 } 7217 7218 static int 7219 st_determine_density(struct scsi_tape *un, int rw) 7220 { 7221 int rval = 0; 7222 7223 ST_FUNC(ST_DEVINFO, st_determine_density); 7224 7225 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 7226 "st_determine_density(un = 0x%p, rw = %s)\n", 7227 (void*)un, (rw == B_WRITE ? wr_str: rd_str)); 7228 7229 ASSERT(mutex_owned(ST_MUTEX)); 7230 7231 /* 7232 * If we're past BOT, density is determined already. 7233 */ 7234 if (un->un_pos.pmode == logical) { 7235 if (un->un_pos.lgclblkno != 0) { 7236 goto exit; 7237 } 7238 } else if (un->un_pos.pmode == legacy) { 7239 if ((un->un_pos.fileno != 0) || (un->un_pos.blkno != 0)) { 7240 /* 7241 * XXX: put in a bitch message about attempting to 7242 * XXX: change density past BOT. 7243 */ 7244 goto exit; 7245 } 7246 } else { 7247 goto exit; 7248 } 7249 if ((un->un_pos.pmode == logical) && 7250 (un->un_pos.lgclblkno != 0)) { 7251 goto exit; 7252 } 7253 7254 7255 /* 7256 * If we're going to be writing, we set the density 7257 */ 7258 if (rw == 0 || rw == B_WRITE) { 7259 /* un_curdens is used as an index into densities table */ 7260 un->un_curdens = MT_DENSITY(un->un_dev); 7261 if (st_set_density(un)) { 7262 rval = -1; 7263 } 7264 goto exit; 7265 } 7266 7267 /* 7268 * If density is known already, 7269 * we don't have to get it again.(?) 7270 */ 7271 if (!un->un_density_known) { 7272 if (st_get_density(un)) { 7273 rval = -1; 7274 } 7275 } 7276 7277 exit: 7278 ASSERT(mutex_owned(ST_MUTEX)); 7279 return (rval); 7280 } 7281 7282 7283 /* 7284 * Try to determine density. We do this by attempting to read the 7285 * first record off the tape, cycling through the available density 7286 * codes as we go. 7287 */ 7288 7289 static int 7290 st_get_density(struct scsi_tape *un) 7291 { 7292 int succes = 0, rval = -1, i; 7293 uint_t size; 7294 uchar_t dens, olddens; 7295 7296 ST_FUNC(ST_DEVINFO, st_get_density); 7297 7298 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 7299 "st_get_density(un = 0x%p)\n", (void*)un); 7300 7301 ASSERT(mutex_owned(ST_MUTEX)); 7302 7303 /* 7304 * If Auto Density override is enabled The drive has 7305 * only one density and there is no point in attempting 7306 * find the correct one. 7307 * 7308 * Since most modern drives auto detect the density 7309 * and format of the recorded media before they come 7310 * ready. What this function does is a legacy behavior 7311 * and modern drives not only don't need it, The backup 7312 * utilities that do positioning via uscsi find the un- 7313 * expected rewinds problematic. 7314 * 7315 * The drives that need this are old reel to reel devices. 7316 * I took a swag and said they must be scsi-1 or older. 7317 * I don't beleave there will any of the newer devices 7318 * that need this. There will be some scsi-1 devices that 7319 * don't need this but I don't think they will be using the 7320 * BIG aftermarket backup and restore utilitys. 7321 */ 7322 if ((un->un_dp->options & ST_AUTODEN_OVERRIDE) || 7323 (un->un_sd->sd_inq->inq_ansi > 1)) { 7324 un->un_density_known = 1; 7325 rval = 0; 7326 goto exit; 7327 } 7328 7329 /* 7330 * This will only work on variable record length tapes 7331 * if and only if all variable record length tapes autodensity 7332 * select. 7333 */ 7334 size = (unsigned)(un->un_dp->bsize ? un->un_dp->bsize : SECSIZE); 7335 un->un_tmpbuf = kmem_alloc(size, KM_SLEEP); 7336 7337 /* 7338 * Start at the specified density 7339 */ 7340 7341 dens = olddens = un->un_curdens = MT_DENSITY(un->un_dev); 7342 7343 for (i = 0; i < NDENSITIES; i++, ((un->un_curdens == NDENSITIES - 1) ? 7344 (un->un_curdens = 0) : (un->un_curdens += 1))) { 7345 /* 7346 * If we've done this density before, 7347 * don't bother to do it again. 7348 */ 7349 dens = un->un_dp->densities[un->un_curdens]; 7350 if (i > 0 && dens == olddens) 7351 continue; 7352 olddens = dens; 7353 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7354 "trying density 0x%x\n", dens); 7355 if (st_set_density(un)) { 7356 continue; 7357 } 7358 7359 /* 7360 * XXX - the creates lots of headaches and slowdowns - must 7361 * fix. 7362 */ 7363 succes = (st_cmd(un, SCMD_READ, (int)size, SYNC_CMD) == 0); 7364 if (st_cmd(un, SCMD_REWIND, 0, SYNC_CMD)) { 7365 break; 7366 } 7367 if (succes) { 7368 st_init(un); 7369 rval = 0; 7370 un->un_density_known = 1; 7371 break; 7372 } 7373 } 7374 kmem_free(un->un_tmpbuf, size); 7375 un->un_tmpbuf = 0; 7376 7377 exit: 7378 ASSERT(mutex_owned(ST_MUTEX)); 7379 return (rval); 7380 } 7381 7382 static int 7383 st_set_density(struct scsi_tape *un) 7384 { 7385 int rval = 0; 7386 7387 ST_FUNC(ST_DEVINFO, st_set_density); 7388 7389 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 7390 "st_set_density(un = 0x%p): density = 0x%x\n", (void*)un, 7391 un->un_dp->densities[un->un_curdens]); 7392 7393 ASSERT(mutex_owned(ST_MUTEX)); 7394 7395 un->un_mspl->density = un->un_dp->densities[un->un_curdens]; 7396 7397 if ((un->un_dp->options & ST_AUTODEN_OVERRIDE) == 0) { 7398 /* 7399 * If auto density override is not set, Use mode select 7400 * to set density and compression. 7401 */ 7402 if (st_modeselect(un)) { 7403 rval = -1; 7404 } 7405 } else if ((un->un_dp->options & ST_MODE_SEL_COMP) != 0) { 7406 /* 7407 * If auto density and mode select compression are set, 7408 * This is a drive with one density code but compression 7409 * can be enabled or disabled. 7410 * Set compression but no need to set density. 7411 */ 7412 rval = st_set_compression(un); 7413 if ((rval != 0) && (rval != EALREADY)) { 7414 rval = -1; 7415 } else { 7416 rval = 0; 7417 } 7418 } 7419 7420 /* If sucessful set density and/or compression, mark density known */ 7421 if (rval == 0) { 7422 un->un_density_known = 1; 7423 } 7424 7425 ASSERT(mutex_owned(ST_MUTEX)); 7426 return (rval); 7427 } 7428 7429 static int 7430 st_loadtape(struct scsi_tape *un) 7431 { 7432 int rval; 7433 7434 ST_FUNC(ST_DEVINFO, st_loadtape); 7435 7436 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 7437 "st_loadtape(un = 0x%p)\n", (void*) un); 7438 7439 ASSERT(mutex_owned(ST_MUTEX)); 7440 7441 rval = st_update_block_pos(un, st_cmd, 0); 7442 if (rval == EACCES) { 7443 return (rval); 7444 } 7445 7446 /* 7447 * 'LOAD' the tape to BOT by rewinding 7448 */ 7449 rval = st_cmd(un, SCMD_REWIND, 1, SYNC_CMD); 7450 if (rval == 0) { 7451 st_init(un); 7452 un->un_density_known = 0; 7453 } 7454 7455 ASSERT(mutex_owned(ST_MUTEX)); 7456 return (rval); 7457 } 7458 7459 7460 /* 7461 * Note: QIC devices aren't so smart. If you try to append 7462 * after EOM, the write can fail because the device doesn't know 7463 * it's at EOM. In that case, issue a read. The read should fail 7464 * because there's no data, but the device knows it's at EOM, 7465 * so a subsequent write should succeed. To further confuse matters, 7466 * the target returns the same error if the tape is positioned 7467 * such that a write would overwrite existing data. That's why 7468 * we have to do the append test. A read in the middle of 7469 * recorded data would succeed, thus indicating we're attempting 7470 * something illegal. 7471 */ 7472 7473 7474 static void 7475 st_test_append(struct buf *bp) 7476 { 7477 dev_t dev = bp->b_edev; 7478 struct scsi_tape *un; 7479 uchar_t status; 7480 unsigned bcount; 7481 7482 un = ddi_get_soft_state(st_state, MTUNIT(dev)); 7483 7484 ST_FUNC(ST_DEVINFO, st_test_append); 7485 7486 ASSERT(mutex_owned(ST_MUTEX)); 7487 7488 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 7489 "st_test_append(): fileno %d\n", un->un_pos.fileno); 7490 7491 un->un_laststate = un->un_state; 7492 un->un_state = ST_STATE_APPEND_TESTING; 7493 un->un_test_append = 0; 7494 7495 /* 7496 * first, map in the buffer, because we're doing a double write -- 7497 * first into the kernel, then onto the tape. 7498 */ 7499 bp_mapin(bp); 7500 7501 /* 7502 * get a copy of the data.... 7503 */ 7504 un->un_tmpbuf = kmem_alloc((unsigned)bp->b_bcount, KM_SLEEP); 7505 bcopy(bp->b_un.b_addr, un->un_tmpbuf, (uint_t)bp->b_bcount); 7506 7507 /* 7508 * attempt the write.. 7509 */ 7510 7511 if (st_cmd(un, (int)SCMD_WRITE, (int)bp->b_bcount, SYNC_CMD) == 0) { 7512 success: 7513 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7514 "append write succeeded\n"); 7515 bp->b_resid = un->un_sbufp->b_resid; 7516 mutex_exit(ST_MUTEX); 7517 bcount = (unsigned)bp->b_bcount; 7518 biodone(bp); 7519 mutex_enter(ST_MUTEX); 7520 un->un_laststate = un->un_state; 7521 un->un_state = ST_STATE_OPEN; 7522 kmem_free(un->un_tmpbuf, bcount); 7523 un->un_tmpbuf = NULL; 7524 return; 7525 } 7526 7527 /* 7528 * The append failed. Do a short read. If that fails, we are at EOM 7529 * so we can retry the write command. If that succeeds, than we're 7530 * all screwed up (the controller reported a real error). 7531 * 7532 * XXX: should the dummy read be > SECSIZE? should it be the device's 7533 * XXX: block size? 7534 * 7535 */ 7536 status = un->un_status; 7537 un->un_status = 0; 7538 (void) st_cmd(un, SCMD_READ, SECSIZE, SYNC_CMD); 7539 if (un->un_status == KEY_BLANK_CHECK) { 7540 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7541 "append at EOM\n"); 7542 /* 7543 * Okay- the read failed. We should actually have confused 7544 * the controller enough to allow writing. In any case, the 7545 * i/o is on its own from here on out. 7546 */ 7547 un->un_laststate = un->un_state; 7548 un->un_state = ST_STATE_OPEN; 7549 bcopy(bp->b_un.b_addr, un->un_tmpbuf, (uint_t)bp->b_bcount); 7550 if (st_cmd(un, (int)SCMD_WRITE, (int)bp->b_bcount, 7551 SYNC_CMD) == 0) { 7552 goto success; 7553 } 7554 } 7555 7556 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7557 "append write failed- not at EOM\n"); 7558 bp->b_resid = bp->b_bcount; 7559 st_bioerror(bp, EIO); 7560 7561 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 7562 "st_test_append : EIO : append write failed - not at EOM"); 7563 7564 /* 7565 * backspace one record to get back to where we were 7566 */ 7567 if (st_cmd(un, SCMD_SPACE, Blk(-1), SYNC_CMD)) { 7568 un->un_pos.pmode = invalid; 7569 } 7570 7571 un->un_err_resid = bp->b_resid; 7572 un->un_status = status; 7573 7574 /* 7575 * Note: biodone will do a bp_mapout() 7576 */ 7577 mutex_exit(ST_MUTEX); 7578 bcount = (unsigned)bp->b_bcount; 7579 biodone(bp); 7580 mutex_enter(ST_MUTEX); 7581 un->un_laststate = un->un_state; 7582 un->un_state = ST_STATE_OPEN_PENDING_IO; 7583 kmem_free(un->un_tmpbuf, bcount); 7584 un->un_tmpbuf = NULL; 7585 } 7586 7587 /* 7588 * Special command handler 7589 */ 7590 7591 /* 7592 * common st_cmd code. The fourth parameter states 7593 * whether the caller wishes to await the results 7594 * Note the release of the mutex during most of the function 7595 */ 7596 static int 7597 st_cmd(struct scsi_tape *un, int com, int64_t count, int wait) 7598 { 7599 struct buf *bp; 7600 int err; 7601 uint_t last_err_resid; 7602 7603 ST_FUNC(ST_DEVINFO, st_cmd); 7604 7605 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 7606 "st_cmd(dev = 0x%lx, com = 0x%x, count = %"PRIx64", wait = %d)\n", 7607 un->un_dev, com, count, wait); 7608 7609 ASSERT(MUTEX_HELD(&un->un_sd->sd_mutex)); 7610 ASSERT(mutex_owned(ST_MUTEX)); 7611 7612 #ifdef STDEBUG 7613 if ((st_debug & 0x7)) { 7614 st_debug_cmds(un, com, count, wait); 7615 } 7616 #endif 7617 7618 st_wait_for_io(un); 7619 7620 /* check to see if this command requires the drive to be reserved */ 7621 err = st_check_cmd_for_need_to_reserve(un, com, count); 7622 7623 if (err) { 7624 return (err); 7625 } 7626 7627 /* 7628 * A space command is not recoverable if we don't know were we 7629 * were when it was issued. 7630 */ 7631 if ((com == SCMD_SPACE) || (com == SCMD_SPACE_G4)) { 7632 (void) st_update_block_pos(un, st_cmd, 0); 7633 } 7634 7635 /* 7636 * Forground should not be doing anything while recovery is active. 7637 */ 7638 ASSERT(un->un_recov_buf_busy == 0); 7639 7640 while (un->un_sbuf_busy) 7641 cv_wait(&un->un_sbuf_cv, ST_MUTEX); 7642 un->un_sbuf_busy = 1; 7643 7644 bp = un->un_sbufp; 7645 bzero(bp, sizeof (buf_t)); 7646 7647 bp->b_flags = (wait) ? B_BUSY : B_BUSY|B_ASYNC; 7648 7649 err = st_setup_cmd(un, bp, com, count); 7650 7651 un->un_sbuf_busy = 0; 7652 7653 /* 7654 * If was a space command need to update logical block position. 7655 * Only do this if the command was sucessful or it will mask the fact 7656 * that the space command failed by promoting the pmode to logical. 7657 */ 7658 if (((com == SCMD_SPACE) || (com == SCMD_SPACE_G4)) && 7659 (un->un_pos.pmode != invalid)) { 7660 un->un_running.pmode = invalid; 7661 last_err_resid = un->un_err_resid; 7662 (void) st_update_block_pos(un, st_cmd, 1); 7663 /* 7664 * Set running position to invalid so it updates on the 7665 * next command. 7666 */ 7667 un->un_running.pmode = invalid; 7668 un->un_err_resid = last_err_resid; 7669 } 7670 7671 cv_signal(&un->un_sbuf_cv); 7672 7673 return (err); 7674 } 7675 7676 static int 7677 st_setup_cmd(struct scsi_tape *un, buf_t *bp, int com, int64_t count) 7678 { 7679 int err; 7680 dev_t dev = un->un_dev; 7681 7682 ST_FUNC(ST_DEVINFO, st_setup_cmd); 7683 /* 7684 * Set count to the actual size of the data tranfer. 7685 * For commands with no data transfer, set bp->b_bcount 7686 * to the value to be used when constructing the 7687 * cdb in st_make_cmd(). 7688 */ 7689 switch (com) { 7690 case SCMD_READ: 7691 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7692 "special read %"PRId64"\n", count); 7693 bp->b_flags |= B_READ; 7694 bp->b_un.b_addr = un->un_tmpbuf; 7695 break; 7696 7697 case SCMD_WRITE: 7698 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7699 "special write %"PRId64"\n", count); 7700 bp->b_un.b_addr = un->un_tmpbuf; 7701 break; 7702 7703 case SCMD_WRITE_FILE_MARK: 7704 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7705 "write %"PRId64" file marks\n", count); 7706 bp->b_bcount = count; 7707 count = 0; 7708 break; 7709 7710 case SCMD_REWIND: 7711 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "rewind\n"); 7712 bp->b_bcount = count; 7713 count = 0; 7714 break; 7715 7716 case SCMD_SPACE: 7717 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "space\n"); 7718 /* 7719 * If the user could have entered a number that will 7720 * not fit in the 12 bit count field of space(8), 7721 * use space(16). 7722 */ 7723 if (((int64_t)SPACE_CNT(count) > 0x7fffff) || 7724 ((int64_t)SPACE_CNT(count) < -(0x7fffff))) { 7725 com = SCMD_SPACE_G4; 7726 } 7727 bp->b_bcount = count; 7728 count = 0; 7729 break; 7730 7731 case SCMD_RESERVE: 7732 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "reserve"); 7733 bp->b_bcount = 0; 7734 count = 0; 7735 break; 7736 7737 case SCMD_RELEASE: 7738 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "release"); 7739 bp->b_bcount = 0; 7740 count = 0; 7741 break; 7742 7743 case SCMD_LOAD: 7744 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7745 "%s tape\n", (count & LD_LOAD) ? "load" : "unload"); 7746 bp->b_bcount = count; 7747 count = 0; 7748 break; 7749 7750 case SCMD_ERASE: 7751 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7752 "erase tape\n"); 7753 bp->b_bcount = count; 7754 count = 0; 7755 break; 7756 7757 case SCMD_MODE_SENSE: 7758 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7759 "mode sense\n"); 7760 bp->b_flags |= B_READ; 7761 bp->b_un.b_addr = (caddr_t)(un->un_mspl); 7762 break; 7763 7764 case SCMD_MODE_SELECT: 7765 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7766 "mode select\n"); 7767 bp->b_un.b_addr = (caddr_t)(un->un_mspl); 7768 break; 7769 7770 case SCMD_READ_BLKLIM: 7771 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7772 "read block limits\n"); 7773 bp->b_bcount = count; 7774 bp->b_flags |= B_READ; 7775 bp->b_un.b_addr = (caddr_t)(un->un_rbl); 7776 break; 7777 7778 case SCMD_TEST_UNIT_READY: 7779 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7780 "test unit ready\n"); 7781 bp->b_bcount = 0; 7782 count = 0; 7783 break; 7784 7785 case SCMD_DOORLOCK: 7786 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7787 "%s tape\n", (count & MR_LOCK) ? "lock" : "unlock"); 7788 bp->b_bcount = count = 0; 7789 break; 7790 7791 case SCMD_READ_POSITION: 7792 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7793 "read position\n"); 7794 switch (un->un_read_pos_type) { 7795 case LONG_POS: 7796 count = sizeof (tape_position_long_t); 7797 break; 7798 case EXT_POS: 7799 count = min(count, sizeof (tape_position_ext_t)); 7800 break; 7801 case SHORT_POS: 7802 count = sizeof (tape_position_t); 7803 break; 7804 default: 7805 ST_DEBUG(ST_DEVINFO, st_label, CE_PANIC, 7806 "Unknown read position type 0x%x in " 7807 "st_make_cmd()\n", un->un_read_pos_type); 7808 } 7809 bp->b_bcount = count; 7810 bp->b_flags |= B_READ; 7811 bp->b_un.b_addr = (caddr_t)un->un_read_pos_data; 7812 break; 7813 7814 default: 7815 ST_DEBUG(ST_DEVINFO, st_label, CE_PANIC, 7816 "Unhandled scsi command 0x%x in st_setup_cmd()\n", com); 7817 } 7818 7819 mutex_exit(ST_MUTEX); 7820 7821 if (count > 0) { 7822 int flg = (bp->b_flags & B_READ) ? B_READ : B_WRITE; 7823 /* 7824 * We're going to do actual I/O. 7825 * Set things up for physio. 7826 */ 7827 struct iovec aiov; 7828 struct uio auio; 7829 struct uio *uio = &auio; 7830 7831 bzero(&auio, sizeof (struct uio)); 7832 bzero(&aiov, sizeof (struct iovec)); 7833 aiov.iov_base = bp->b_un.b_addr; 7834 aiov.iov_len = count; 7835 7836 uio->uio_iov = &aiov; 7837 uio->uio_iovcnt = 1; 7838 uio->uio_resid = aiov.iov_len; 7839 uio->uio_segflg = UIO_SYSSPACE; 7840 7841 /* 7842 * Let physio do the rest... 7843 */ 7844 bp->b_forw = (struct buf *)(uintptr_t)com; 7845 bp->b_back = NULL; 7846 err = physio(st_strategy, bp, dev, flg, st_minphys, uio); 7847 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7848 "st_setup_cmd: physio returns %d\n", err); 7849 } else { 7850 /* 7851 * Mimic physio 7852 */ 7853 bp->b_forw = (struct buf *)(uintptr_t)com; 7854 bp->b_back = NULL; 7855 bp->b_edev = dev; 7856 bp->b_dev = cmpdev(dev); 7857 bp->b_blkno = 0; 7858 bp->b_resid = 0; 7859 (void) st_strategy(bp); 7860 if (bp->b_flags & B_ASYNC) { 7861 /* 7862 * This is an async command- the caller won't wait 7863 * and doesn't care about errors. 7864 */ 7865 mutex_enter(ST_MUTEX); 7866 return (0); 7867 } 7868 7869 /* 7870 * BugTraq #4260046 7871 * ---------------- 7872 * Restore Solaris 2.5.1 behavior, namely call biowait 7873 * unconditionally. The old comment said... 7874 * 7875 * "if strategy was flagged with persistent errors, we would 7876 * have an error here, and the bp would never be sent, so we 7877 * don't want to wait on a bp that was never sent...or hang" 7878 * 7879 * The new rationale, courtesy of Chitrank... 7880 * 7881 * "we should unconditionally biowait() here because 7882 * st_strategy() will do a biodone() in the persistent error 7883 * case and the following biowait() will return immediately. 7884 * If not, in the case of "errors after pkt alloc" in 7885 * st_start(), we will not biowait here which will cause the 7886 * next biowait() to return immediately which will cause 7887 * us to send out the next command. In the case where both of 7888 * these use the sbuf, when the first command completes we'll 7889 * free the packet attached to sbuf and the same pkt will 7890 * get freed again when we complete the second command. 7891 * see esc 518987. BTW, it is necessary to do biodone() in 7892 * st_start() for the pkt alloc failure case because physio() 7893 * does biowait() and will hang if we don't do biodone()" 7894 */ 7895 7896 err = biowait(bp); 7897 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7898 "st_setup_cmd: biowait returns %d\n", err); 7899 } 7900 7901 mutex_enter(ST_MUTEX); 7902 7903 return (err); 7904 } 7905 7906 static int 7907 st_set_compression(struct scsi_tape *un) 7908 { 7909 int rval; 7910 int turn_compression_on; 7911 minor_t minor; 7912 7913 ST_FUNC(ST_DEVINFO, st_set_compression); 7914 7915 /* 7916 * Drive either dosn't have compression or it is controlled with 7917 * special density codes. Return ENOTTY so caller 7918 * knows nothing was done. 7919 */ 7920 if ((un->un_dp->options & ST_MODE_SEL_COMP) == 0) { 7921 un->un_comp_page = 0; 7922 return (ENOTTY); 7923 } 7924 7925 /* set compression based on minor node opened */ 7926 minor = MT_DENSITY(un->un_dev); 7927 7928 /* 7929 * If this the compression density or 7930 * the drive has two densities and uses mode select for 7931 * control of compression turn on compression for MT_DENSITY2 7932 * as well. 7933 */ 7934 if ((minor == ST_COMPRESSION_DENSITY) || 7935 (minor == MT_DENSITY(MT_DENSITY2)) && 7936 (un->un_dp->densities[0] == un->un_dp->densities[1]) && 7937 (un->un_dp->densities[2] == un->un_dp->densities[3]) && 7938 (un->un_dp->densities[0] != un->un_dp->densities[2])) { 7939 7940 turn_compression_on = 1; 7941 } else { 7942 turn_compression_on = 0; 7943 } 7944 7945 un->un_mspl->high_bl = (uchar_t)(un->un_bsize >> 16); 7946 un->un_mspl->mid_bl = (uchar_t)(un->un_bsize >> 8); 7947 un->un_mspl->low_bl = (uchar_t)(un->un_bsize); 7948 7949 /* 7950 * Need to determine which page does the device use for compression. 7951 * First try the data compression page. If this fails try the device 7952 * configuration page 7953 */ 7954 7955 if ((un->un_comp_page & ST_DEV_DATACOMP_PAGE) == ST_DEV_DATACOMP_PAGE) { 7956 rval = st_set_datacomp_page(un, turn_compression_on); 7957 if (rval == EALREADY) { 7958 return (rval); 7959 } 7960 if (rval != 0) { 7961 if (un->un_status == KEY_ILLEGAL_REQUEST) { 7962 /* 7963 * This device does not support data 7964 * compression page 7965 */ 7966 un->un_comp_page = ST_DEV_CONFIG_PAGE; 7967 } else if (un->un_state >= ST_STATE_OPEN) { 7968 un->un_pos.pmode = invalid; 7969 rval = EIO; 7970 } else { 7971 rval = -1; 7972 } 7973 } else { 7974 un->un_comp_page = ST_DEV_DATACOMP_PAGE; 7975 } 7976 } 7977 7978 if ((un->un_comp_page & ST_DEV_CONFIG_PAGE) == ST_DEV_CONFIG_PAGE) { 7979 rval = st_set_devconfig_page(un, turn_compression_on); 7980 if (rval == EALREADY) { 7981 return (rval); 7982 } 7983 if (rval != 0) { 7984 if (un->un_status == KEY_ILLEGAL_REQUEST) { 7985 /* 7986 * This device does not support 7987 * compression at all advice the 7988 * user and unset ST_MODE_SEL_COMP 7989 */ 7990 un->un_dp->options &= ~ST_MODE_SEL_COMP; 7991 un->un_comp_page = 0; 7992 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 7993 "Device Does Not Support Compression\n"); 7994 } else if (un->un_state >= ST_STATE_OPEN) { 7995 un->un_pos.pmode = invalid; 7996 rval = EIO; 7997 } else { 7998 rval = -1; 7999 } 8000 } 8001 } 8002 8003 return (rval); 8004 } 8005 8006 /* 8007 * set or unset compression thru device configuration page. 8008 */ 8009 static int 8010 st_set_devconfig_page(struct scsi_tape *un, int compression_on) 8011 { 8012 unsigned char cflag; 8013 int rval = 0; 8014 8015 8016 ST_FUNC(ST_DEVINFO, st_set_devconfig_page); 8017 8018 ASSERT(mutex_owned(ST_MUTEX)); 8019 8020 /* 8021 * if the mode sense page is not the correct one, load the correct one. 8022 */ 8023 if (un->un_mspl->page_code != ST_DEV_CONFIG_PAGE) { 8024 rval = st_gen_mode_sense(un, st_uscsi_cmd, ST_DEV_CONFIG_PAGE, 8025 un->un_mspl, sizeof (struct seq_mode)); 8026 if (rval) 8027 return (rval); 8028 } 8029 8030 /* 8031 * Figure what to set compression flag to. 8032 */ 8033 if (compression_on) { 8034 /* They have selected a compression node */ 8035 if (un->un_dp->type == ST_TYPE_FUJI) { 8036 cflag = 0x84; /* use EDRC */ 8037 } else { 8038 cflag = ST_DEV_CONFIG_DEF_COMP; 8039 } 8040 } else { 8041 cflag = ST_DEV_CONFIG_NO_COMP; 8042 } 8043 8044 /* 8045 * If compression is already set the way it was requested. 8046 * And if this not the first time we has tried. 8047 */ 8048 if ((cflag == un->un_mspl->page.dev.comp_alg) && 8049 (un->un_comp_page == ST_DEV_CONFIG_PAGE)) { 8050 return (EALREADY); 8051 } 8052 8053 un->un_mspl->page.dev.comp_alg = cflag; 8054 /* 8055 * need to send mode select even if correct compression is 8056 * already set since need to set density code 8057 */ 8058 8059 #ifdef STDEBUG 8060 if ((st_debug & 0x7) >= 6) { 8061 st_clean_print(ST_DEVINFO, st_label, SCSI_DEBUG, 8062 "st_set_devconfig_page: sense data for mode select", 8063 (char *)un->un_mspl, sizeof (struct seq_mode)); 8064 } 8065 #endif