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 2007 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 /* 30 * SCSI SCSA-compliant and not-so-DDI-compliant Tape Driver 31 */ 32 33 #if defined(lint) && !defined(DEBUG) 34 #define DEBUG 1 35 #endif 36 37 #include <sys/modctl.h> 38 #include <sys/scsi/scsi.h> 39 #include <sys/mtio.h> 40 #include <sys/scsi/targets/stdef.h> 41 #include <sys/file.h> 42 #include <sys/kstat.h> 43 #include <sys/ddidmareq.h> 44 #include <sys/ddi.h> 45 #include <sys/sunddi.h> 46 47 #define IOSP KSTAT_IO_PTR(un->un_stats) 48 /* 49 * stats maintained only for reads/writes as commands 50 * like rewind etc skew the wait/busy times 51 */ 52 #define IS_RW(bp) ((bp)->b_bcount > 0) 53 #define ST_DO_KSTATS(bp, kstat_function) \ 54 if ((bp != un->un_sbufp) && un->un_stats && IS_RW(bp)) { \ 55 kstat_function(IOSP); \ 56 } 57 58 #define ST_DO_ERRSTATS(un, x) \ 59 if (un->un_errstats) { \ 60 struct st_errstats *stp; \ 61 stp = (struct st_errstats *)un->un_errstats->ks_data; \ 62 stp->x.value.ul++; \ 63 } 64 65 #define FILL_SCSI1_LUN(devp, pkt) \ 66 if ((devp)->sd_inq->inq_ansi == 0x1) { \ 67 int _lun; \ 68 _lun = ddi_prop_get_int(DDI_DEV_T_ANY, (devp)->sd_dev, \ 69 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_LUN, 0); \ 70 if (_lun > 0) { \ 71 ((union scsi_cdb *)(pkt)->pkt_cdbp)->scc_lun = \ 72 _lun; \ 73 } \ 74 } 75 76 /* 77 * get an available contig mem header, cp. 78 * when big_enough is true, we will return NULL, if no big enough 79 * contig mem is found. 80 * when big_enough is false, we will try to find cp containing big 81 * enough contig mem. if not found, we will ruturn the last cp available. 82 * 83 * used by st_get_contig_mem() 84 */ 85 #define ST_GET_CONTIG_MEM_HEAD(un, cp, len, big_enough) { \ 86 struct contig_mem *tmp_cp = NULL; \ 87 for ((cp) = (un)->un_contig_mem; \ 88 (cp) != NULL; \ 89 tmp_cp = (cp), (cp) = (cp)->cm_next) { \ 90 if (((cp)->cm_len >= (len)) || \ 91 (!(big_enough) && ((cp)->cm_next == NULL))) { \ 92 if (tmp_cp == NULL) { \ 93 (un)->un_contig_mem = (cp)->cm_next; \ 94 } else { \ 95 tmp_cp->cm_next = (cp)->cm_next; \ 96 } \ 97 (cp)->cm_next = NULL; \ 98 (un)->un_contig_mem_available_num--; \ 99 break; \ 100 } \ 101 } \ 102 } 103 104 #define ST_NUM_MEMBERS(array) (sizeof (array) / sizeof (array[0])) 105 106 /* 107 * Global External Data Definitions 108 */ 109 extern struct scsi_key_strings scsi_cmds[]; 110 111 /* 112 * Local Static Data 113 */ 114 static void *st_state; 115 static char *st_label = "st"; 116 117 #if defined(__i386) || defined(__amd64) 118 /* 119 * We need to use below DMA attr to alloc physically contiguous 120 * memory to do I/O in big block size 121 */ 122 static ddi_dma_attr_t st_contig_mem_dma_attr = { 123 DMA_ATTR_V0, /* version number */ 124 0x0, /* lowest usable address */ 125 0xFFFFFFFFull, /* high DMA address range */ 126 0xFFFFFFFFull, /* DMA counter register */ 127 1, /* DMA address alignment */ 128 1, /* DMA burstsizes */ 129 1, /* min effective DMA size */ 130 0xFFFFFFFFull, /* max DMA xfer size */ 131 0xFFFFFFFFull, /* segment boundary */ 132 1, /* s/g list length */ 133 1, /* granularity of device */ 134 0 /* DMA transfer flags */ 135 }; 136 137 static ddi_device_acc_attr_t st_acc_attr = { 138 DDI_DEVICE_ATTR_V0, 139 DDI_NEVERSWAP_ACC, 140 DDI_STRICTORDER_ACC 141 }; 142 143 /* set limitation for the number of contig_mem */ 144 static int st_max_contig_mem_num = ST_MAX_CONTIG_MEM_NUM; 145 #endif 146 147 /* 148 * Tunable parameters 149 * 150 * DISCLAIMER 151 * ---------- 152 * These parameters are intended for use only in system testing; if you use 153 * them in production systems, you do so at your own risk. Altering any 154 * variable not listed below may cause unpredictable system behavior. 155 * 156 * st_check_media_time 157 * 158 * Three second state check 159 * 160 * st_allow_large_xfer 161 * 162 * Gated with ST_NO_RECSIZE_LIMIT 163 * 164 * 0 - Transfers larger than 64KB will not be allowed 165 * regardless of the setting of ST_NO_RECSIZE_LIMIT 166 * 1 - Transfers larger than 64KB will be allowed 167 * if ST_NO_RECSIZE_LIMIT is TRUE for the drive 168 * 169 * st_report_soft_errors_on_close 170 * 171 * Gated with ST_SOFT_ERROR_REPORTING 172 * 173 * 0 - Errors will not be reported on close regardless 174 * of the setting of ST_SOFT_ERROR_REPORTING 175 * 176 * 1 - Errors will be reported on close if 177 * ST_SOFT_ERROR_REPORTING is TRUE for the drive 178 */ 179 static int st_selection_retry_count = ST_SEL_RETRY_COUNT; 180 static int st_retry_count = ST_RETRY_COUNT; 181 182 static int st_io_time = ST_IO_TIME; 183 static int st_long_timeout_x = ST_LONG_TIMEOUT_X; 184 185 static int st_space_time = ST_SPACE_TIME; 186 static int st_long_space_time_x = ST_LONG_SPACE_TIME_X; 187 188 static int st_error_level = SCSI_ERR_RETRYABLE; 189 static int st_check_media_time = 3000000; /* 3 Second State Check */ 190 191 static int st_max_throttle = ST_MAX_THROTTLE; 192 193 static clock_t st_wait_cmds_complete = ST_WAIT_CMDS_COMPLETE; 194 195 static int st_allow_large_xfer = 1; 196 static int st_report_soft_errors_on_close = 1; 197 198 /* 199 * End of tunable parameters list 200 */ 201 202 203 204 /* 205 * Asynchronous I/O and persistent errors, refer to PSARC/1995/228 206 * 207 * Asynchronous I/O's main offering is that it is a non-blocking way to do 208 * reads and writes. The driver will queue up all the requests it gets and 209 * have them ready to transport to the HBA. Unfortunately, we cannot always 210 * just ship the I/O requests to the HBA, as there errors and exceptions 211 * that may happen when we don't want the HBA to continue. Therein comes 212 * the flush-on-errors capability. If the HBA supports it, then st will 213 * send in st_max_throttle I/O requests at the same time. 214 * 215 * Persistent errors : This was also reasonably simple. In the interrupt 216 * routines, if there was an error or exception (FM, LEOT, media error, 217 * transport error), the persistent error bits are set and shuts everything 218 * down, but setting the throttle to zero. If we hit and exception in the 219 * HBA, and flush-on-errors were set, we wait for all outstanding I/O's to 220 * come back (with CMD_ABORTED), then flush all bp's in the wait queue with 221 * the appropriate error, and this will preserve order. Of course, depending 222 * on the exception we have to show a zero read or write before we show 223 * errors back to the application. 224 */ 225 226 extern const int st_ndrivetypes; /* defined in st_conf.c */ 227 extern const struct st_drivetype st_drivetypes[]; 228 229 #ifdef STDEBUG 230 static int st_soft_error_report_debug = 0; 231 volatile int st_debug = 0; 232 #endif 233 234 #define ST_MT02_NAME "Emulex MT02 QIC-11/24 " 235 236 static const struct driver_minor_data { 237 char *name; 238 int minor; 239 } st_minor_data[] = { 240 /* 241 * The top 4 entries are for the default densities, 242 * don't alter their position. 243 */ 244 {"", 0}, 245 {"n", MT_NOREWIND}, 246 {"b", MT_BSD}, 247 {"bn", MT_NOREWIND | MT_BSD}, 248 {"l", MT_DENSITY1}, 249 {"m", MT_DENSITY2}, 250 {"h", MT_DENSITY3}, 251 {"c", MT_DENSITY4}, 252 {"u", MT_DENSITY4}, 253 {"ln", MT_DENSITY1 | MT_NOREWIND}, 254 {"mn", MT_DENSITY2 | MT_NOREWIND}, 255 {"hn", MT_DENSITY3 | MT_NOREWIND}, 256 {"cn", MT_DENSITY4 | MT_NOREWIND}, 257 {"un", MT_DENSITY4 | MT_NOREWIND}, 258 {"lb", MT_DENSITY1 | MT_BSD}, 259 {"mb", MT_DENSITY2 | MT_BSD}, 260 {"hb", MT_DENSITY3 | MT_BSD}, 261 {"cb", MT_DENSITY4 | MT_BSD}, 262 {"ub", MT_DENSITY4 | MT_BSD}, 263 {"lbn", MT_DENSITY1 | MT_NOREWIND | MT_BSD}, 264 {"mbn", MT_DENSITY2 | MT_NOREWIND | MT_BSD}, 265 {"hbn", MT_DENSITY3 | MT_NOREWIND | MT_BSD}, 266 {"cbn", MT_DENSITY4 | MT_NOREWIND | MT_BSD}, 267 {"ubn", MT_DENSITY4 | MT_NOREWIND | MT_BSD} 268 }; 269 270 /* strings used in many debug and warning messages */ 271 static const char wr_str[] = "write"; 272 static const char rd_str[] = "read"; 273 static const char wrg_str[] = "writing"; 274 static const char rdg_str[] = "reading"; 275 276 /* default density offsets in the table above */ 277 #define DEF_BLANK 0 278 #define DEF_NOREWIND 1 279 #define DEF_BSD 2 280 #define DEF_BSD_NR 3 281 282 /* Sense Key, ASC/ASCQ for which tape ejection is needed */ 283 284 static struct tape_failure_code { 285 uchar_t key; 286 uchar_t add_code; 287 uchar_t qual_code; 288 } st_tape_failure_code[] = { 289 { KEY_HARDWARE_ERROR, 0x15, 0x01}, 290 { KEY_HARDWARE_ERROR, 0x44, 0x00}, 291 { KEY_HARDWARE_ERROR, 0x53, 0x00}, 292 { KEY_HARDWARE_ERROR, 0x53, 0x01}, 293 { KEY_NOT_READY, 0x53, 0x00}, 294 { 0xff} 295 }; 296 297 /* clean bit position and mask */ 298 299 static struct cln_bit_position { 300 ushort_t cln_bit_byte; 301 uchar_t cln_bit_mask; 302 } st_cln_bit_position[] = { 303 { 21, 0x08}, 304 { 70, 0xc0}, 305 { 18, 0x81} /* 80 bit indicates in bit mode, 1 bit clean light is on */ 306 }; 307 308 /* 309 * architecture dependent allocation restrictions. For x86, we'll set 310 * dma_attr_addr_hi to st_max_phys_addr and dma_attr_sgllen to 311 * st_sgl_size during _init(). 312 */ 313 #if defined(__sparc) 314 static ddi_dma_attr_t st_alloc_attr = { 315 DMA_ATTR_V0, /* version number */ 316 0x0, /* lowest usable address */ 317 0xFFFFFFFFull, /* high DMA address range */ 318 0xFFFFFFFFull, /* DMA counter register */ 319 1, /* DMA address alignment */ 320 1, /* DMA burstsizes */ 321 1, /* min effective DMA size */ 322 0xFFFFFFFFull, /* max DMA xfer size */ 323 0xFFFFFFFFull, /* segment boundary */ 324 1, /* s/g list length */ 325 512, /* granularity of device */ 326 0 /* DMA transfer flags */ 327 }; 328 #elif defined(__x86) 329 static ddi_dma_attr_t st_alloc_attr = { 330 DMA_ATTR_V0, /* version number */ 331 0x0, /* lowest usable address */ 332 0x0, /* high DMA address range [set in _init()] */ 333 0xFFFFull, /* DMA counter register */ 334 512, /* DMA address alignment */ 335 1, /* DMA burstsizes */ 336 1, /* min effective DMA size */ 337 0xFFFFFFFFull, /* max DMA xfer size */ 338 0xFFFFFFFFull, /* segment boundary */ 339 0, /* s/g list length */ 340 512, /* granularity of device [set in _init()] */ 341 0 /* DMA transfer flags */ 342 }; 343 uint64_t st_max_phys_addr = 0xFFFFFFFFull; 344 int st_sgl_size = 0xF; 345 346 #endif 347 348 /* 349 * Configuration Data: 350 * 351 * Device driver ops vector 352 */ 353 static int st_aread(dev_t dev, struct aio_req *aio, cred_t *cred_p); 354 static int st_awrite(dev_t dev, struct aio_req *aio, cred_t *cred_p); 355 static int st_read(dev_t dev, struct uio *uio_p, cred_t *cred_p); 356 static int st_write(dev_t dev, struct uio *uio_p, cred_t *cred_p); 357 static int st_open(dev_t *devp, int flag, int otyp, cred_t *cred_p); 358 static int st_close(dev_t dev, int flag, int otyp, cred_t *cred_p); 359 static int st_strategy(struct buf *bp); 360 static int st_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, 361 cred_t *cred_p, int *rval_p); 362 extern int nulldev(), nodev(); 363 364 static struct cb_ops st_cb_ops = { 365 st_open, /* open */ 366 st_close, /* close */ 367 st_strategy, /* strategy */ 368 nodev, /* print */ 369 nodev, /* dump */ 370 st_read, /* read */ 371 st_write, /* write */ 372 st_ioctl, /* ioctl */ 373 nodev, /* devmap */ 374 nodev, /* mmap */ 375 nodev, /* segmap */ 376 nochpoll, /* poll */ 377 ddi_prop_op, /* cb_prop_op */ 378 0, /* streamtab */ 379 D_64BIT | D_MP | D_NEW | D_HOTPLUG | 380 D_OPEN_RETURNS_EINTR, /* cb_flag */ 381 CB_REV, /* cb_rev */ 382 st_aread, /* async I/O read entry point */ 383 st_awrite /* async I/O write entry point */ 384 385 }; 386 387 static int stinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, 388 void **result); 389 static int st_probe(dev_info_t *dev); 390 static int st_attach(dev_info_t *dev, ddi_attach_cmd_t cmd); 391 static int st_detach(dev_info_t *dev, ddi_detach_cmd_t cmd); 392 393 static struct dev_ops st_ops = { 394 DEVO_REV, /* devo_rev, */ 395 0, /* refcnt */ 396 stinfo, /* info */ 397 nulldev, /* identify */ 398 st_probe, /* probe */ 399 st_attach, /* attach */ 400 st_detach, /* detach */ 401 nodev, /* reset */ 402 &st_cb_ops, /* driver operations */ 403 (struct bus_ops *)0, /* bus operations */ 404 nulldev /* power */ 405 }; 406 407 /* 408 * Local Function Declarations 409 */ 410 static void st_clean_print(dev_info_t *dev, char *label, uint_t level, 411 char *title, char *data, int len); 412 static int st_doattach(struct scsi_device *devp, int (*canwait)()); 413 static void st_known_tape_type(struct scsi_tape *un); 414 static int st_get_conf_from_st_dot_conf(struct scsi_tape *, char *, 415 struct st_drivetype *); 416 static int st_get_conf_from_st_conf_dot_c(struct scsi_tape *, char *, 417 struct st_drivetype *); 418 static int st_get_default_conf(struct scsi_tape *, char *, 419 struct st_drivetype *); 420 static int st_rw(dev_t dev, struct uio *uio, int flag); 421 static int st_arw(dev_t dev, struct aio_req *aio, int flag); 422 static int st_find_eom(dev_t dev); 423 static int st_check_density_or_wfm(dev_t dev, int wfm, int mode, int stepflag); 424 static int st_ioctl_cmd(dev_t dev, struct uscsi_cmd *, int flag); 425 static int st_mtioctop(struct scsi_tape *un, intptr_t arg, int flag); 426 static void st_start(struct scsi_tape *un); 427 static int st_handle_start_busy(struct scsi_tape *un, struct buf *bp, 428 clock_t timeout_interval); 429 static int st_handle_intr_busy(struct scsi_tape *un, struct buf *bp, 430 clock_t timeout_interval); 431 static int st_handle_intr_retry_lcmd(struct scsi_tape *un, struct buf *bp); 432 static void st_done_and_mutex_exit(struct scsi_tape *un, struct buf *bp); 433 static void st_init(struct scsi_tape *un); 434 static void st_make_cmd(struct scsi_tape *un, struct buf *bp, 435 int (*func)(caddr_t)); 436 static void st_make_uscsi_cmd(struct scsi_tape *, struct uscsi_cmd *, 437 struct buf *bp, int (*func)(caddr_t)); 438 static void st_intr(struct scsi_pkt *pkt); 439 static void st_set_state(struct scsi_tape *un); 440 static void st_test_append(struct buf *bp); 441 static int st_runout(caddr_t); 442 static int st_cmd(dev_t dev, int com, int count, int wait); 443 static int st_set_compression(struct scsi_tape *un); 444 static int st_write_fm(dev_t dev, int wfm); 445 static int st_determine_generic(dev_t dev); 446 static int st_determine_density(dev_t dev, int rw); 447 static int st_get_density(dev_t dev); 448 static int st_set_density(dev_t dev); 449 static int st_loadtape(dev_t dev); 450 static int st_modesense(struct scsi_tape *un); 451 static int st_modeselect(struct scsi_tape *un); 452 static int st_handle_incomplete(struct scsi_tape *un, struct buf *bp); 453 static int st_wrongtapetype(struct scsi_tape *un); 454 static int st_check_error(struct scsi_tape *un, struct scsi_pkt *pkt); 455 static int st_handle_sense(struct scsi_tape *un, struct buf *bp); 456 static int st_handle_autosense(struct scsi_tape *un, struct buf *bp); 457 static int st_decode_sense(struct scsi_tape *un, struct buf *bp, int amt, 458 struct scsi_status *); 459 static int st_report_soft_errors(dev_t dev, int flag); 460 static void st_delayed_cv_broadcast(void *arg); 461 static int st_check_media(dev_t dev, enum mtio_state state); 462 static int st_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp); 463 static void st_intr_restart(void *arg); 464 static void st_start_restart(void *arg); 465 static int st_gen_mode_sense(struct scsi_tape *un, int page, 466 struct seq_mode *page_data, int page_size); 467 static int st_change_block_size(dev_t dev, uint32_t nblksz); 468 static int st_gen_mode_select(struct scsi_tape *un, struct seq_mode *page_data, 469 int page_size); 470 static int st_tape_init(dev_t dev); 471 static void st_flush(struct scsi_tape *un); 472 static void st_set_pe_errno(struct scsi_tape *un); 473 static void st_hba_unflush(struct scsi_tape *un); 474 static void st_turn_pe_on(struct scsi_tape *un); 475 static void st_turn_pe_off(struct scsi_tape *un); 476 static void st_set_pe_flag(struct scsi_tape *un); 477 static void st_clear_pe(struct scsi_tape *un); 478 static void st_wait_for_io(struct scsi_tape *un); 479 static int st_set_devconfig_page(struct scsi_tape *un, int compression_on); 480 static int st_set_datacomp_page(struct scsi_tape *un, int compression_on); 481 static int st_reserve_release(struct scsi_tape *un, int command); 482 static int st_check_cdb_for_need_to_reserve(struct scsi_tape *un, caddr_t cdb); 483 static int st_check_cmd_for_need_to_reserve(struct scsi_tape *un, uchar_t cmd, 484 int count); 485 static int st_take_ownership(dev_t dev); 486 static int st_check_asc_ascq(struct scsi_tape *un); 487 static int st_check_clean_bit(dev_t dev); 488 static int st_check_alert_flags(dev_t dev); 489 static int st_check_sequential_clean_bit(dev_t dev); 490 static int st_check_sense_clean_bit(dev_t dev); 491 static int st_clear_unit_attentions(dev_t dev_instance, int max_trys); 492 static void st_calculate_timeouts(struct scsi_tape *un); 493 static writablity st_is_drive_worm(struct scsi_tape *un); 494 static int st_read_attributes(struct scsi_tape *un, uint16_t attribute, 495 caddr_t buf, size_t size); 496 static int st_get_special_inquiry(struct scsi_tape *un, uchar_t size, 497 caddr_t dest, uchar_t page); 498 499 #if defined(__i386) || defined(__amd64) 500 /* 501 * routines for I/O in big block size 502 */ 503 static void st_release_contig_mem(struct scsi_tape *un, struct contig_mem *cp); 504 static struct contig_mem *st_get_contig_mem(struct scsi_tape *un, size_t len, 505 int alloc_flags); 506 static int st_bigblk_xfer_done(struct buf *bp); 507 static struct buf *st_get_bigblk_bp(struct buf *bp); 508 #endif 509 510 /* 511 * error statistics create/update functions 512 */ 513 static int st_create_errstats(struct scsi_tape *, int); 514 static int st_validate_tapemarks(struct scsi_tape *un, int fileno, daddr_t bn); 515 516 #ifdef STDEBUG 517 static void st_debug_cmds(struct scsi_tape *un, int com, int count, int wait); 518 static char *st_dev_name(dev_t dev); 519 #endif /* STDEBUG */ 520 521 #if !defined(lint) 522 _NOTE(SCHEME_PROTECTS_DATA("unique per pkt", 523 scsi_pkt buf uio scsi_cdb uscsi_cmd)) 524 _NOTE(SCHEME_PROTECTS_DATA("unique per pkt", scsi_extended_sense scsi_status)) 525 _NOTE(SCHEME_PROTECTS_DATA("stable data", scsi_device)) 526 _NOTE(DATA_READABLE_WITHOUT_LOCK(st_drivetype scsi_address)) 527 #endif 528 529 /* 530 * autoconfiguration routines. 531 */ 532 char _depends_on[] = "misc/scsi"; 533 534 static struct modldrv modldrv = { 535 &mod_driverops, /* Type of module. This one is a driver */ 536 "SCSI tape Driver %I%", /* Name of the module. */ 537 &st_ops /* driver ops */ 538 }; 539 540 static struct modlinkage modlinkage = { 541 MODREV_1, &modldrv, NULL 542 }; 543 544 /* 545 * Notes on Post Reset Behavior in the tape driver: 546 * 547 * When the tape drive is opened, the driver attempts to make sure that 548 * the tape head is positioned exactly where it was left when it was last 549 * closed provided the medium is not changed. If the tape drive is 550 * opened in O_NDELAY mode, the repositioning (if necessary for any loss 551 * of position due to reset) will happen when the first tape operation or 552 * I/O occurs. The repositioning (if required) may not be possible under 553 * certain situations such as when the device firmware not able to report 554 * the medium change in the REQUEST SENSE data because of a reset or a 555 * misbehaving bus not allowing the reposition to happen. In such 556 * extraordinary situations, where the driver fails to position the head 557 * at its original position, it will fail the open the first time, to 558 * save the applications from overwriting the data. All further attempts 559 * to open the tape device will result in the driver attempting to load 560 * the tape at BOT (beginning of tape). Also a warning message to 561 * indicate that further attempts to open the tape device may result in 562 * the tape being loaded at BOT will be printed on the console. If the 563 * tape device is opened in O_NDELAY mode, failure to restore the 564 * original tape head position, will result in the failure of the first 565 * tape operation or I/O, Further, the driver will invalidate its 566 * internal tape position which will necessitate the applications to 567 * validate the position by using either a tape positioning ioctl (such 568 * as MTREW) or closing and reopening the tape device. 569 * 570 */ 571 572 int 573 _init(void) 574 { 575 int e; 576 577 if (((e = ddi_soft_state_init(&st_state, 578 sizeof (struct scsi_tape), ST_MAXUNIT)) != 0)) { 579 return (e); 580 } 581 582 if ((e = mod_install(&modlinkage)) != 0) { 583 ddi_soft_state_fini(&st_state); 584 } 585 586 #if defined(__x86) 587 /* set the max physical address for iob allocs on x86 */ 588 st_alloc_attr.dma_attr_addr_hi = st_max_phys_addr; 589 590 /* 591 * set the sgllen for iob allocs on x86. If this is set less than 592 * the number of pages the buffer will take (taking into account 593 * alignment), it would force the allocator to try and allocate 594 * contiguous pages. 595 */ 596 st_alloc_attr.dma_attr_sgllen = st_sgl_size; 597 #endif 598 599 return (e); 600 } 601 602 int 603 _fini(void) 604 { 605 int e; 606 607 if ((e = mod_remove(&modlinkage)) != 0) { 608 return (e); 609 } 610 611 ddi_soft_state_fini(&st_state); 612 613 return (e); 614 } 615 616 int 617 _info(struct modinfo *modinfop) 618 { 619 return (mod_info(&modlinkage, modinfop)); 620 } 621 622 623 static int 624 st_probe(dev_info_t *devi) 625 { 626 int instance; 627 struct scsi_device *devp; 628 int rval; 629 630 #if !defined(__sparc) 631 char *tape_prop; 632 int tape_prop_len; 633 #endif 634 635 /* If self identifying device */ 636 if (ddi_dev_is_sid(devi) == DDI_SUCCESS) { 637 return (DDI_PROBE_DONTCARE); 638 } 639 640 #if !defined(__sparc) 641 /* 642 * Since some x86 HBAs have devnodes that look like SCSI as 643 * far as we can tell but aren't really SCSI (DADK, like mlx) 644 * we check for the presence of the "tape" property. 645 */ 646 if (ddi_prop_op(DDI_DEV_T_NONE, devi, PROP_LEN_AND_VAL_ALLOC, 647 DDI_PROP_CANSLEEP, "tape", 648 (caddr_t)&tape_prop, &tape_prop_len) != DDI_PROP_SUCCESS) { 649 return (DDI_PROBE_FAILURE); 650 } 651 if (strncmp(tape_prop, "sctp", tape_prop_len) != 0) { 652 kmem_free(tape_prop, tape_prop_len); 653 return (DDI_PROBE_FAILURE); 654 } 655 kmem_free(tape_prop, tape_prop_len); 656 #endif 657 658 devp = ddi_get_driver_private(devi); 659 instance = ddi_get_instance(devi); 660 661 if (ddi_get_soft_state(st_state, instance) != NULL) { 662 return (DDI_PROBE_PARTIAL); 663 } 664 665 666 /* 667 * Turn around and call probe routine to see whether 668 * we actually have a tape at this SCSI nexus. 669 */ 670 if (scsi_probe(devp, NULL_FUNC) == SCSIPROBE_EXISTS) { 671 672 /* 673 * In checking the whole inq_dtype byte we are looking at both 674 * the Peripheral Qualifier and the Peripheral Device Type. 675 * For this driver we are only interested in sequential devices 676 * that are connected or capable if connecting to this logical 677 * unit. 678 */ 679 if (devp->sd_inq->inq_dtype == 680 (DTYPE_SEQUENTIAL | DPQ_POSSIBLE)) { 681 ST_DEBUG6(devi, st_label, SCSI_DEBUG, 682 "probe exists\n"); 683 rval = DDI_PROBE_SUCCESS; 684 } else { 685 rval = DDI_PROBE_FAILURE; 686 } 687 } else { 688 ST_DEBUG6(devi, st_label, SCSI_DEBUG, 689 "probe failure: nothing there\n"); 690 rval = DDI_PROBE_FAILURE; 691 } 692 scsi_unprobe(devp); 693 return (rval); 694 } 695 696 static int 697 st_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 698 { 699 int instance; 700 int wide; 701 int dev_instance; 702 int ret_status; 703 struct scsi_device *devp; 704 int node_ix; 705 struct scsi_tape *un; 706 707 devp = ddi_get_driver_private(devi); 708 instance = ddi_get_instance(devi); 709 710 switch (cmd) { 711 case DDI_ATTACH: 712 if (st_doattach(devp, SLEEP_FUNC) == DDI_FAILURE) { 713 return (DDI_FAILURE); 714 } 715 break; 716 case DDI_RESUME: 717 /* 718 * Suspend/Resume 719 * 720 * When the driver suspended, there might be 721 * outstanding cmds and therefore we need to 722 * reset the suspended flag and resume the scsi 723 * watch thread and restart commands and timeouts 724 */ 725 726 if (!(un = ddi_get_soft_state(st_state, instance))) { 727 return (DDI_FAILURE); 728 } 729 dev_instance = ((un->un_dev == 0) ? MTMINOR(instance) : 730 un->un_dev); 731 732 mutex_enter(ST_MUTEX); 733 734 un->un_throttle = un->un_max_throttle; 735 un->un_tids_at_suspend = 0; 736 un->un_pwr_mgmt = ST_PWR_NORMAL; 737 738 if (un->un_swr_token) { 739 scsi_watch_resume(un->un_swr_token); 740 } 741 742 /* 743 * Restart timeouts 744 */ 745 if ((un->un_tids_at_suspend & ST_DELAY_TID) != 0) { 746 mutex_exit(ST_MUTEX); 747 un->un_delay_tid = 748 timeout(st_delayed_cv_broadcast, un, 749 drv_usectohz((clock_t) 750 MEDIA_ACCESS_DELAY)); 751 mutex_enter(ST_MUTEX); 752 } 753 754 if (un->un_tids_at_suspend & ST_HIB_TID) { 755 mutex_exit(ST_MUTEX); 756 un->un_hib_tid = timeout(st_intr_restart, un, 757 ST_STATUS_BUSY_TIMEOUT); 758 mutex_enter(ST_MUTEX); 759 } 760 761 ret_status = st_clear_unit_attentions(dev_instance, 5); 762 763 /* 764 * now check if we need to restore the tape position 765 */ 766 if ((un->un_suspend_fileno > 0) || 767 (un->un_suspend_blkno > 0)) { 768 if (ret_status != 0) { 769 /* 770 * tape didn't get good TUR 771 * just print out error messages 772 */ 773 scsi_log(ST_DEVINFO, st_label, CE_WARN, 774 "st_attach-RESUME: tape failure " 775 " tape position will be lost"); 776 } else { 777 /* this prints errors */ 778 (void) st_validate_tapemarks(un, 779 un->un_suspend_fileno, 780 un->un_suspend_blkno); 781 } 782 /* 783 * there are no retries, if there is an error 784 * we don't know if the tape has changed 785 */ 786 un->un_suspend_fileno = 0; 787 un->un_suspend_blkno = 0; 788 } 789 790 /* now we are ready to start up any queued I/Os */ 791 if (un->un_ncmds || un->un_quef) { 792 st_start(un); 793 } 794 795 cv_broadcast(&un->un_suspend_cv); 796 mutex_exit(ST_MUTEX); 797 return (DDI_SUCCESS); 798 799 default: 800 return (DDI_FAILURE); 801 } 802 803 un = ddi_get_soft_state(st_state, instance); 804 805 ST_DEBUG(devi, st_label, SCSI_DEBUG, 806 "st_attach: instance=%x\n", instance); 807 808 /* 809 * find the drive type for this target 810 */ 811 st_known_tape_type(un); 812 813 for (node_ix = 0; node_ix < ST_NUM_MEMBERS(st_minor_data); node_ix++) { 814 int minor; 815 char *name; 816 817 name = st_minor_data[node_ix].name; 818 minor = st_minor_data[node_ix].minor; 819 820 /* 821 * For default devices set the density to the 822 * preferred default density for this device. 823 */ 824 if (node_ix <= DEF_BSD_NR) { 825 minor |= un->un_dp->default_density; 826 } 827 minor |= MTMINOR(instance); 828 829 if (ddi_create_minor_node(devi, name, S_IFCHR, minor, 830 DDI_NT_TAPE, NULL) == DDI_SUCCESS) { 831 continue; 832 } 833 834 ddi_remove_minor_node(devi, NULL); 835 if (un) { 836 cv_destroy(&un->un_clscv); 837 cv_destroy(&un->un_sbuf_cv); 838 cv_destroy(&un->un_queue_cv); 839 cv_destroy(&un->un_state_cv); 840 cv_destroy(&un->un_suspend_cv); 841 cv_destroy(&un->un_tape_busy_cv); 842 843 if (un->un_sbufp) { 844 freerbuf(un->un_sbufp); 845 } 846 if (un->un_uscsi_rqs_buf) { 847 kmem_free(un->un_uscsi_rqs_buf, SENSE_LENGTH); 848 } 849 if (un->un_mspl) { 850 i_ddi_mem_free((caddr_t)un->un_mspl, NULL); 851 } 852 scsi_destroy_pkt(un->un_rqs); 853 scsi_free_consistent_buf(un->un_rqs_bp); 854 ddi_soft_state_free(st_state, instance); 855 devp->sd_private = NULL; 856 devp->sd_sense = NULL; 857 858 } 859 ddi_prop_remove_all(devi); 860 return (DDI_FAILURE); 861 } 862 863 /* 864 * Add a zero-length attribute to tell the world we support 865 * kernel ioctls (for layered drivers) 866 */ 867 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP, 868 DDI_KERNEL_IOCTL, NULL, 0); 869 870 ddi_report_dev((dev_info_t *)devi); 871 872 /* 873 * If it's a SCSI-2 tape drive which supports wide, 874 * tell the host adapter to use wide. 875 */ 876 wide = ((devp->sd_inq->inq_rdf == RDF_SCSI2) && 877 (devp->sd_inq->inq_wbus16 || devp->sd_inq->inq_wbus32)) ? 878 1 : 0; 879 880 if (scsi_ifsetcap(ROUTE, "wide-xfer", wide, 1) == 1) { 881 ST_DEBUG(devi, st_label, SCSI_DEBUG, 882 "Wide Transfer %s\n", wide ? "enabled" : "disabled"); 883 } 884 885 /* 886 * enable autorequest sense; keep the rq packet around in case 887 * the autorequest sense fails because of a busy condition 888 * do a getcap first in case the capability is not variable 889 */ 890 if (scsi_ifgetcap(ROUTE, "auto-rqsense", 1) == 1) { 891 un->un_arq_enabled = 1; 892 } else { 893 un->un_arq_enabled = 894 ((scsi_ifsetcap(ROUTE, "auto-rqsense", 1, 1) == 1) ? 1 : 0); 895 } 896 897 898 ST_DEBUG(devi, st_label, SCSI_DEBUG, "auto request sense %s\n", 899 (un->un_arq_enabled ? "enabled" : "disabled")); 900 901 un->un_untagged_qing = 902 (scsi_ifgetcap(ROUTE, "untagged-qing", 0) == 1); 903 904 /* 905 * XXX - This is just for 2.6. to tell users that write buffering 906 * has gone away. 907 */ 908 if (un->un_arq_enabled && un->un_untagged_qing) { 909 if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 910 "tape-driver-buffering", 0) != 0) { 911 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 912 "Write Data Buffering has been depricated. Your " 913 "applications should continue to work normally.\n" 914 " But, they should ported to use Asynchronous " 915 " I/O\n" 916 " For more information, read about " 917 " tape-driver-buffering " 918 "property in the st(7d) man page\n"); 919 } 920 } 921 922 un->un_max_throttle = un->un_throttle = un->un_last_throttle = 1; 923 un->un_flush_on_errors = 0; 924 un->un_mkr_pkt = (struct scsi_pkt *)NULL; 925 926 ST_DEBUG(devi, st_label, SCSI_DEBUG, 927 "throttle=%x, max_throttle = %x\n", 928 un->un_throttle, un->un_max_throttle); 929 930 /* initialize persistent errors to nil */ 931 un->un_persistence = 0; 932 un->un_persist_errors = 0; 933 934 /* 935 * Get dma-max from HBA driver. If it is not defined, use 64k 936 */ 937 un->un_maxdma = scsi_ifgetcap(&devp->sd_address, "dma-max", 1); 938 if (un->un_maxdma == -1) { 939 ST_DEBUG(devi, st_label, SCSI_DEBUG, 940 "Receaved a value that looked like -1. Using 64k maxdma"); 941 un->un_maxdma = (64 * 1024); 942 } 943 944 /* 945 * Get the max allowable cdb size 946 */ 947 un->un_max_cdb_sz = 948 scsi_ifgetcap(&devp->sd_address, "max-cdb-length", 1); 949 if (un->un_max_cdb_sz < CDB_GROUP0) { 950 ST_DEBUG(devi, st_label, SCSI_DEBUG, 951 "HBA reported max-cdb-length as %d\n", un->un_max_cdb_sz); 952 un->un_max_cdb_sz = CDB_GROUP4; /* optimistic default */ 953 } 954 955 un->un_maxbsize = MAXBSIZE_UNKNOWN; 956 957 un->un_mediastate = MTIO_NONE; 958 un->un_HeadClean = TAPE_ALERT_SUPPORT_UNKNOWN; 959 960 /* 961 * initialize kstats 962 */ 963 un->un_stats = kstat_create("st", instance, NULL, "tape", 964 KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT); 965 if (un->un_stats) { 966 un->un_stats->ks_lock = ST_MUTEX; 967 kstat_install(un->un_stats); 968 } 969 (void) st_create_errstats(un, instance); 970 971 return (DDI_SUCCESS); 972 } 973 974 /* 975 * st_detach: 976 * 977 * we allow a detach if and only if: 978 * - no tape is currently inserted 979 * - tape position is at BOT or unknown 980 * (if it is not at BOT then a no rewind 981 * device was opened and we have to preserve state) 982 * - it must be in a closed state : no timeouts or scsi_watch requests 983 * will exist if it is closed, so we don't need to check for 984 * them here. 985 */ 986 /*ARGSUSED*/ 987 static int 988 st_detach(dev_info_t *devi, ddi_detach_cmd_t cmd) 989 { 990 int instance; 991 int dev_instance; 992 struct scsi_device *devp; 993 struct scsi_tape *un; 994 clock_t wait_cmds_complete; 995 996 instance = ddi_get_instance(devi); 997 998 if (!(un = ddi_get_soft_state(st_state, instance))) { 999 return (DDI_FAILURE); 1000 } 1001 1002 switch (cmd) { 1003 1004 case DDI_DETACH: 1005 /* 1006 * Undo what we did in st_attach & st_doattach, 1007 * freeing resources and removing things we installed. 1008 * The system framework guarantees we are not active 1009 * with this devinfo node in any other entry points at 1010 * this time. 1011 */ 1012 1013 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 1014 "st_detach: instance=%x, un=%p\n", instance, 1015 (void *)un); 1016 1017 if (((un->un_dp->options & ST_UNLOADABLE) == 0) || 1018 (un->un_ncmds != 0) || (un->un_quef != NULL) || 1019 (un->un_state != ST_STATE_CLOSED)) { 1020 /* 1021 * we cannot unload some targets because the 1022 * inquiry returns junk unless immediately 1023 * after a reset 1024 */ 1025 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 1026 "cannot unload instance %x\n", instance); 1027 return (DDI_FAILURE); 1028 } 1029 1030 /* 1031 * if the tape has been removed then we may unload; 1032 * do a test unit ready and if it returns NOT READY 1033 * then we assume that it is safe to unload. 1034 * as a side effect, fileno may be set to -1 if the 1035 * the test unit ready fails; 1036 * also un_state may be set to non-closed, so reset it 1037 */ 1038 if ((un->un_dev) && /* Been opened since attach */ 1039 ((un->un_fileno > 0) || /* Known position not rewound */ 1040 (un->un_blkno != 0))) { /* Or within first file */ 1041 mutex_enter(ST_MUTEX); 1042 /* 1043 * Send Test Unit Ready in the hopes that if 1044 * the drive is not in the state we think it is. 1045 * And the state will be changed so it can be detached. 1046 * If the command fails to reach the device and 1047 * the drive was not rewound or unloaded we want 1048 * to fail the detach till a user command fails 1049 * where after the detach will succead. 1050 */ 1051 (void) st_cmd(un->un_dev, SCMD_TEST_UNIT_READY, 1052 0, SYNC_CMD); 1053 /* 1054 * After TUR un_state may be set to non-closed, 1055 * so reset it back. 1056 */ 1057 un->un_state = ST_STATE_CLOSED; 1058 mutex_exit(ST_MUTEX); 1059 } 1060 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 1061 "un_status=%x, fileno=%x, blkno=%lx\n", 1062 un->un_status, un->un_fileno, un->un_blkno); 1063 1064 /* 1065 * check again: 1066 * if we are not at BOT then it is not safe to unload 1067 */ 1068 if ((un->un_dev) && /* Been opened since attach */ 1069 ((un->un_fileno > 0) || /* Known position not rewound */ 1070 (un->un_blkno != 0))) { /* Or within first file */ 1071 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 1072 "cannot detach: fileno=%x, blkno=%lx\n", 1073 un->un_fileno, un->un_blkno); 1074 return (DDI_FAILURE); 1075 } 1076 1077 /* 1078 * Just To make sure that we have released the 1079 * tape unit . 1080 */ 1081 if (un->un_dev && (un->un_rsvd_status & ST_RESERVE) && 1082 !DEVI_IS_DEVICE_REMOVED(devi)) { 1083 mutex_enter(ST_MUTEX); 1084 (void) st_reserve_release(un, ST_RELEASE); 1085 mutex_exit(ST_MUTEX); 1086 } 1087 1088 /* 1089 * now remove other data structures allocated in st_doattach() 1090 */ 1091 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 1092 "destroying/freeing\n"); 1093 cv_destroy(&un->un_clscv); 1094 cv_destroy(&un->un_sbuf_cv); 1095 cv_destroy(&un->un_queue_cv); 1096 cv_destroy(&un->un_suspend_cv); 1097 cv_destroy(&un->un_tape_busy_cv); 1098 1099 if (un->un_hib_tid) { 1100 (void) untimeout(un->un_hib_tid); 1101 un->un_hib_tid = 0; 1102 } 1103 1104 if (un->un_delay_tid) { 1105 (void) untimeout(un->un_delay_tid); 1106 un->un_delay_tid = 0; 1107 } 1108 cv_destroy(&un->un_state_cv); 1109 1110 #if defined(__i386) || defined(__amd64) 1111 if (un->un_contig_mem_hdl != NULL) { 1112 ddi_dma_free_handle(&un->un_contig_mem_hdl); 1113 } 1114 #endif 1115 if (un->un_sbufp) { 1116 freerbuf(un->un_sbufp); 1117 } 1118 if (un->un_uscsi_rqs_buf) { 1119 kmem_free(un->un_uscsi_rqs_buf, SENSE_LENGTH); 1120 } 1121 if (un->un_mspl) { 1122 i_ddi_mem_free((caddr_t)un->un_mspl, NULL); 1123 } 1124 if (un->un_rqs) { 1125 scsi_destroy_pkt(un->un_rqs); 1126 scsi_free_consistent_buf(un->un_rqs_bp); 1127 } 1128 if (un->un_mkr_pkt) { 1129 scsi_destroy_pkt(un->un_mkr_pkt); 1130 } 1131 if (un->un_arq_enabled) { 1132 (void) scsi_ifsetcap(ROUTE, "auto-rqsense", 0, 1); 1133 } 1134 if (un->un_dp_size) { 1135 kmem_free(un->un_dp, un->un_dp_size); 1136 } 1137 if (un->un_stats) { 1138 kstat_delete(un->un_stats); 1139 un->un_stats = (kstat_t *)0; 1140 } 1141 if (un->un_errstats) { 1142 kstat_delete(un->un_errstats); 1143 un->un_errstats = (kstat_t *)0; 1144 } 1145 devp = ST_SCSI_DEVP; 1146 ddi_soft_state_free(st_state, instance); 1147 devp->sd_private = NULL; 1148 devp->sd_sense = NULL; 1149 scsi_unprobe(devp); 1150 ddi_prop_remove_all(devi); 1151 ddi_remove_minor_node(devi, NULL); 1152 ST_DEBUG(0, st_label, SCSI_DEBUG, "st_detach done\n"); 1153 return (DDI_SUCCESS); 1154 1155 case DDI_SUSPEND: 1156 1157 /* 1158 * Suspend/Resume 1159 * 1160 * To process DDI_SUSPEND, we must do the following: 1161 * 1162 * - check ddi_removing_power to see if power will be turned 1163 * off. if so, return DDI_FAILURE 1164 * - check if we are already suspended, 1165 * if so, return DDI_FAILURE 1166 * - check if device state is CLOSED, 1167 * if not, return DDI_FAILURE. 1168 * - wait until outstanding operations complete 1169 * - save tape state 1170 * - block new operations 1171 * - cancel pending timeouts 1172 * 1173 */ 1174 1175 if (ddi_removing_power(devi)) 1176 return (DDI_FAILURE); 1177 1178 mutex_enter(ST_MUTEX); 1179 1180 /* 1181 * Shouldn't already be suspended, if so return failure 1182 */ 1183 if (un->un_pwr_mgmt == ST_PWR_SUSPENDED) { 1184 mutex_exit(ST_MUTEX); 1185 return (DDI_FAILURE); 1186 } 1187 if (un->un_state != ST_STATE_CLOSED) { 1188 mutex_exit(ST_MUTEX); 1189 return (DDI_FAILURE); 1190 } 1191 1192 /* 1193 * Wait for all outstanding I/O's to complete 1194 * 1195 * we wait on both ncmds and the wait queue for times 1196 * when we are flushing after persistent errors are 1197 * flagged, which is when ncmds can be 0, and the 1198 * queue can still have I/O's. This way we preserve 1199 * order of biodone's. 1200 */ 1201 wait_cmds_complete = ddi_get_lbolt(); 1202 wait_cmds_complete += 1203 st_wait_cmds_complete * drv_usectohz(1000000); 1204 while (un->un_ncmds || un->un_quef || 1205 (un->un_state == ST_STATE_RESOURCE_WAIT)) { 1206 1207 if (cv_timedwait(&un->un_tape_busy_cv, ST_MUTEX, 1208 wait_cmds_complete) == -1) { 1209 /* 1210 * Time expired then cancel the command 1211 */ 1212 mutex_exit(ST_MUTEX); 1213 if (scsi_reset(ROUTE, RESET_TARGET) == 0) { 1214 mutex_enter(ST_MUTEX); 1215 if (un->un_last_throttle) { 1216 un->un_throttle = 1217 un->un_last_throttle; 1218 } 1219 mutex_exit(ST_MUTEX); 1220 return (DDI_FAILURE); 1221 } else { 1222 mutex_enter(ST_MUTEX); 1223 break; 1224 } 1225 } 1226 } 1227 1228 /* 1229 * DDI_SUSPEND says that the system "may" power down, we 1230 * remember the file and block number before rewinding. 1231 * we also need to save state before issuing 1232 * any WRITE_FILE_MARK command. 1233 */ 1234 if (un->un_fileno < 0) { 1235 un->un_suspend_fileno = 0; 1236 un->un_suspend_blkno = 0; 1237 } else { 1238 un->un_suspend_fileno = un->un_fileno; 1239 un->un_suspend_blkno = un->un_blkno; 1240 } 1241 dev_instance = ((un->un_dev == 0) ? MTMINOR(instance) : 1242 un->un_dev); 1243 1244 /* 1245 * Issue a zero write file fmk command to tell the drive to 1246 * flush any buffered tape marks 1247 */ 1248 (void) st_cmd(dev_instance, SCMD_WRITE_FILE_MARK, 0, SYNC_CMD); 1249 1250 /* 1251 * Because not all tape drives correctly implement buffer 1252 * flushing with the zero write file fmk command, issue a 1253 * synchronous rewind command to force data flushing. 1254 * st_validate_tapemarks() will do a rewind during DDI_RESUME 1255 * anyway. 1256 */ 1257 (void) st_cmd(dev_instance, SCMD_REWIND, 0, SYNC_CMD); 1258 1259 /* stop any new operations */ 1260 un->un_pwr_mgmt = ST_PWR_SUSPENDED; 1261 un->un_throttle = 0; 1262 1263 /* 1264 * cancel any outstanding timeouts 1265 */ 1266 if (un->un_delay_tid) { 1267 timeout_id_t temp_id = un->un_delay_tid; 1268 un->un_delay_tid = 0; 1269 un->un_tids_at_suspend |= ST_DELAY_TID; 1270 mutex_exit(ST_MUTEX); 1271 (void) untimeout(temp_id); 1272 mutex_enter(ST_MUTEX); 1273 } 1274 1275 if (un->un_hib_tid) { 1276 timeout_id_t temp_id = un->un_hib_tid; 1277 un->un_hib_tid = 0; 1278 un->un_tids_at_suspend |= ST_HIB_TID; 1279 mutex_exit(ST_MUTEX); 1280 (void) untimeout(temp_id); 1281 mutex_enter(ST_MUTEX); 1282 } 1283 1284 /* 1285 * Suspend the scsi_watch_thread 1286 */ 1287 if (un->un_swr_token) { 1288 opaque_t temp_token = un->un_swr_token; 1289 mutex_exit(ST_MUTEX); 1290 scsi_watch_suspend(temp_token); 1291 } else { 1292 mutex_exit(ST_MUTEX); 1293 } 1294 1295 return (DDI_SUCCESS); 1296 1297 default: 1298 ST_DEBUG(0, st_label, SCSI_DEBUG, "st_detach failed\n"); 1299 return (DDI_FAILURE); 1300 } 1301 } 1302 1303 1304 /* ARGSUSED */ 1305 static int 1306 stinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 1307 { 1308 dev_t dev; 1309 struct scsi_tape *un; 1310 int instance, error; 1311 switch (infocmd) { 1312 case DDI_INFO_DEVT2DEVINFO: 1313 dev = (dev_t)arg; 1314 instance = MTUNIT(dev); 1315 if ((un = ddi_get_soft_state(st_state, instance)) == NULL) 1316 return (DDI_FAILURE); 1317 *result = (void *) ST_DEVINFO; 1318 error = DDI_SUCCESS; 1319 break; 1320 case DDI_INFO_DEVT2INSTANCE: 1321 dev = (dev_t)arg; 1322 instance = MTUNIT(dev); 1323 *result = (void *)(uintptr_t)instance; 1324 error = DDI_SUCCESS; 1325 break; 1326 default: 1327 error = DDI_FAILURE; 1328 } 1329 return (error); 1330 } 1331 1332 static int 1333 st_doattach(struct scsi_device *devp, int (*canwait)()) 1334 { 1335 struct scsi_pkt *rqpkt = NULL; 1336 struct scsi_tape *un = NULL; 1337 int km_flags = (canwait != NULL_FUNC) ? KM_SLEEP : KM_NOSLEEP; 1338 int instance; 1339 struct buf *bp; 1340 size_t rlen; 1341 1342 /* 1343 * Call the routine scsi_probe to do some of the dirty work. 1344 * If the INQUIRY command succeeds, the field sd_inq in the 1345 * device structure will be filled in. 1346 */ 1347 ST_DEBUG(devp->sd_dev, st_label, SCSI_DEBUG, 1348 "st_doattach(): probing\n"); 1349 1350 if (scsi_probe(devp, canwait) == SCSIPROBE_EXISTS) { 1351 1352 /* 1353 * In checking the whole inq_dtype byte we are looking at both 1354 * the Peripheral Qualifier and the Peripheral Device Type. 1355 * For this driver we are only interested in sequential devices 1356 * that are connected or capable if connecting to this logical 1357 * unit. 1358 */ 1359 if (devp->sd_inq->inq_dtype == 1360 (DTYPE_SEQUENTIAL | DPQ_POSSIBLE)) { 1361 ST_DEBUG(devp->sd_dev, st_label, SCSI_DEBUG, 1362 "probe exists\n"); 1363 } else { 1364 /* Something there but not a tape device */ 1365 scsi_unprobe(devp); 1366 return (DDI_FAILURE); 1367 } 1368 } else { 1369 /* Nothing there */ 1370 ST_DEBUG(devp->sd_dev, st_label, SCSI_DEBUG, 1371 "probe failure: nothing there\n"); 1372 scsi_unprobe(devp); 1373 return (DDI_FAILURE); 1374 } 1375 1376 bp = scsi_alloc_consistent_buf(&devp->sd_address, (struct buf *)NULL, 1377 SENSE_LENGTH, B_READ, canwait, NULL); 1378 if (!bp) { 1379 goto error; 1380 } 1381 rqpkt = scsi_init_pkt(&devp->sd_address, 1382 (struct scsi_pkt *)NULL, bp, CDB_GROUP0, 1, 0, 1383 PKT_CONSISTENT, canwait, NULL); 1384 if (!rqpkt) { 1385 goto error; 1386 } 1387 devp->sd_sense = (struct scsi_extended_sense *)bp->b_un.b_addr; 1388 ASSERT(geterror(bp) == NULL); 1389 1390 (void) scsi_setup_cdb((union scsi_cdb *)rqpkt->pkt_cdbp, 1391 SCMD_REQUEST_SENSE, 0, SENSE_LENGTH, 0); 1392 FILL_SCSI1_LUN(devp, rqpkt); 1393 1394 /* 1395 * The actual unit is present. 1396 * Now is the time to fill in the rest of our info.. 1397 */ 1398 instance = ddi_get_instance(devp->sd_dev); 1399 1400 if (ddi_soft_state_zalloc(st_state, instance) != DDI_SUCCESS) { 1401 goto error; 1402 } 1403 un = ddi_get_soft_state(st_state, instance); 1404 1405 ASSERT(un != NULL); 1406 1407 un->un_sbufp = getrbuf(km_flags); 1408 1409 un->un_uscsi_rqs_buf = kmem_alloc(SENSE_LENGTH, KM_SLEEP); 1410 1411 /* 1412 * use i_ddi_mem_alloc() for now until we have an interface to allocate 1413 * memory for DMA which doesn't require a DMA handle. ddi_iopb_alloc() 1414 * is obsolete and we want more flexibility in controlling the DMA 1415 * address constraints. 1416 */ 1417 (void) i_ddi_mem_alloc(devp->sd_dev, &st_alloc_attr, 1418 sizeof (struct seq_mode), ((km_flags == KM_SLEEP) ? 1 : 0), 0, 1419 NULL, (caddr_t *)&un->un_mspl, &rlen, NULL); 1420 1421 if (!un->un_sbufp || !un->un_mspl) { 1422 if (un->un_mspl) { 1423 i_ddi_mem_free((caddr_t)un->un_mspl, NULL); 1424 } 1425 ST_DEBUG6(devp->sd_dev, st_label, SCSI_DEBUG, 1426 "probe partial failure: no space\n"); 1427 goto error; 1428 } 1429 1430 bzero(un->un_mspl, sizeof (struct seq_mode)); 1431 1432 cv_init(&un->un_sbuf_cv, NULL, CV_DRIVER, NULL); 1433 cv_init(&un->un_queue_cv, NULL, CV_DRIVER, NULL); 1434 cv_init(&un->un_clscv, NULL, CV_DRIVER, NULL); 1435 cv_init(&un->un_state_cv, NULL, CV_DRIVER, NULL); 1436 #if defined(__i386) || defined(__amd64) 1437 cv_init(&un->un_contig_mem_cv, NULL, CV_DRIVER, NULL); 1438 #endif 1439 1440 /* Initialize power managemnet condition variable */ 1441 cv_init(&un->un_suspend_cv, NULL, CV_DRIVER, NULL); 1442 cv_init(&un->un_tape_busy_cv, NULL, CV_DRIVER, NULL); 1443 1444 rqpkt->pkt_flags |= (FLAG_SENSING | FLAG_HEAD | FLAG_NODISCON); 1445 1446 un->un_fileno = -1; 1447 rqpkt->pkt_time = st_io_time; 1448 rqpkt->pkt_comp = st_intr; 1449 un->un_rqs = rqpkt; 1450 un->un_sd = devp; 1451 un->un_rqs_bp = bp; 1452 un->un_swr_token = (opaque_t)NULL; 1453 un->un_comp_page = ST_DEV_DATACOMP_PAGE | ST_DEV_CONFIG_PAGE; 1454 un->un_wormable = st_is_drive_worm; 1455 1456 un->un_suspend_fileno = 0; 1457 un->un_suspend_blkno = 0; 1458 1459 #if defined(__i386) || defined(__amd64) 1460 if (ddi_dma_alloc_handle(ST_DEVINFO, &st_contig_mem_dma_attr, 1461 DDI_DMA_SLEEP, NULL, &un->un_contig_mem_hdl) != DDI_SUCCESS) { 1462 ST_DEBUG6(devp->sd_dev, st_label, SCSI_DEBUG, 1463 "allocation of contiguous memory dma handle failed!"); 1464 un->un_contig_mem_hdl = NULL; 1465 goto error; 1466 } 1467 #endif 1468 1469 /* 1470 * Since this driver manages devices with "remote" hardware, 1471 * i.e. the devices themselves have no "reg" properties, 1472 * the SUSPEND/RESUME commands in detach/attach will not be 1473 * called by the power management framework unless we request 1474 * it by creating a "pm-hardware-state" property and setting it 1475 * to value "needs-suspend-resume". 1476 */ 1477 if (ddi_prop_update_string(DDI_DEV_T_NONE, devp->sd_dev, 1478 "pm-hardware-state", "needs-suspend-resume") != 1479 DDI_PROP_SUCCESS) { 1480 1481 ST_DEBUG(devp->sd_dev, st_label, SCSI_DEBUG, 1482 "ddi_prop_update(\"pm-hardware-state\") failed\n"); 1483 goto error; 1484 } 1485 1486 if (ddi_prop_create(DDI_DEV_T_NONE, devp->sd_dev, DDI_PROP_CANSLEEP, 1487 "no-involuntary-power-cycles", NULL, 0) != DDI_PROP_SUCCESS) { 1488 1489 ST_DEBUG(devp->sd_dev, st_label, SCSI_DEBUG, 1490 "ddi_prop_create(\"no-involuntary-power-cycles\") " 1491 "failed\n"); 1492 goto error; 1493 } 1494 1495 ST_DEBUG6(devp->sd_dev, st_label, SCSI_DEBUG, "probe success\n"); 1496 return (DDI_SUCCESS); 1497 1498 error: 1499 devp->sd_sense = NULL; 1500 1501 ddi_remove_minor_node(devp->sd_dev, NULL); 1502 if (un) { 1503 if (un->un_mspl) { 1504 i_ddi_mem_free((caddr_t)un->un_mspl, NULL); 1505 } 1506 if (un->un_sbufp) { 1507 freerbuf(un->un_sbufp); 1508 } 1509 if (un->un_uscsi_rqs_buf) { 1510 kmem_free(un->un_uscsi_rqs_buf, SENSE_LENGTH); 1511 } 1512 #if defined(__i386) || defined(__amd64) 1513 if (un->un_contig_mem_hdl != NULL) { 1514 ddi_dma_free_handle(&un->un_contig_mem_hdl); 1515 } 1516 #endif 1517 ddi_soft_state_free(st_state, instance); 1518 devp->sd_private = NULL; 1519 } 1520 1521 if (rqpkt) { 1522 scsi_destroy_pkt(rqpkt); 1523 } 1524 1525 if (bp) { 1526 scsi_free_consistent_buf(bp); 1527 } 1528 1529 if (devp->sd_inq) { 1530 scsi_unprobe(devp); 1531 } 1532 return (DDI_FAILURE); 1533 } 1534 1535 typedef int 1536 (*cfg_functp)(struct scsi_tape *, char *vidpid, struct st_drivetype *); 1537 1538 static cfg_functp config_functs[] = { 1539 st_get_conf_from_st_dot_conf, 1540 st_get_conf_from_st_conf_dot_c, 1541 st_get_default_conf 1542 }; 1543 1544 1545 /* 1546 * determine tape type, using tape-config-list or built-in table or 1547 * use a generic tape config entry 1548 */ 1549 static void 1550 st_known_tape_type(struct scsi_tape *un) 1551 { 1552 struct st_drivetype *dp; 1553 cfg_functp *config_funct; 1554 1555 /* 1556 * XXX: Emulex MT-02 (and emulators) predates SCSI-1 and has 1557 * no vid & pid inquiry data. So, we provide one. 1558 */ 1559 if (ST_INQUIRY->inq_len == 0 || 1560 (bcmp("\0\0\0\0\0\0\0\0", ST_INQUIRY->inq_vid, 8) == 0)) { 1561 (void) strcpy((char *)ST_INQUIRY->inq_vid, ST_MT02_NAME); 1562 } 1563 1564 un->un_dp_size = sizeof (struct st_drivetype); 1565 dp = kmem_zalloc((size_t)un->un_dp_size, KM_SLEEP); 1566 un->un_dp = dp; 1567 1568 /* 1569 * Loop through the configuration methods till one works. 1570 */ 1571 for (config_funct = &config_functs[0]; ; config_funct++) { 1572 if ((*config_funct)(un, ST_INQUIRY->inq_vid, dp)) { 1573 break; 1574 } 1575 } 1576 1577 /* 1578 * If we didn't just make up this configuration and 1579 * all the density codes are the same.. 1580 * Set Auto Density over ride. 1581 */ 1582 if (*config_funct != st_get_default_conf) { 1583 /* 1584 * If this device is one that is configured and all 1585 * densities are the same, This saves doing gets and set 1586 * that yield nothing. 1587 */ 1588 if ((dp->densities[0]) == (dp->densities[1]) && 1589 (dp->densities[0]) == (dp->densities[2]) && 1590 (dp->densities[0]) == (dp->densities[3])) { 1591 1592 dp->options |= ST_AUTODEN_OVERRIDE; 1593 } 1594 } 1595 1596 1597 /* 1598 * Store tape drive characteristics. 1599 */ 1600 un->un_status = 0; 1601 un->un_attached = 1; 1602 un->un_init_options = dp->options; 1603 1604 /* setup operation time-outs based on options */ 1605 st_calculate_timeouts(un); 1606 1607 /* make sure if we are supposed to be variable, make it variable */ 1608 if (dp->options & ST_VARIABLE) { 1609 dp->bsize = 0; 1610 } 1611 1612 scsi_log(ST_DEVINFO, st_label, CE_NOTE, "?<%s>\n", dp->name); 1613 } 1614 1615 1616 typedef struct { 1617 int mask; 1618 int bottom; 1619 int top; 1620 char *name; 1621 } conf_limit; 1622 1623 static const conf_limit conf_limits[] = { 1624 1625 -1, 1, 2, "conf version", 1626 -1, MT_ISTS, ST_LAST_TYPE, "drive type", 1627 -1, 0, 0xffffff, "block size", 1628 ST_VALID_OPTS, 0, ST_VALID_OPTS, "options", 1629 -1, 0, 4, "number of densities", 1630 -1, 0, UINT8_MAX, "density code", 1631 -1, 0, 3, "default density", 1632 -1, 0, UINT16_MAX, "non motion timeout", 1633 -1, 0, UINT16_MAX, "I/O timeout", 1634 -1, 0, UINT16_MAX, "space timeout", 1635 -1, 0, UINT16_MAX, "load timeout", 1636 -1, 0, UINT16_MAX, "unload timeout", 1637 -1, 0, UINT16_MAX, "erase timeout", 1638 0, 0, 0, NULL 1639 }; 1640 1641 static int 1642 st_validate_conf_data(struct scsi_tape *un, int *list, int list_len, 1643 const char *conf_name) 1644 { 1645 int dens; 1646 int ndens; 1647 int value; 1648 int type; 1649 int count; 1650 const conf_limit *limit = &conf_limits[0]; 1651 1652 ST_DEBUG3(ST_DEVINFO, st_label, CE_NOTE, 1653 "Checking %d entrys total with %d densities\n", list_len, list[4]); 1654 1655 count = list_len; 1656 type = *list; 1657 for (; count && limit->name; count--, list++, limit++) { 1658 1659 value = *list; 1660 if (value & ~limit->mask) { 1661 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 1662 "%s %s value invalid bits set: 0x%X\n", 1663 conf_name, limit->name, value & ~limit->mask); 1664 *list &= limit->mask; 1665 } else if (value < limit->bottom) { 1666 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 1667 "%s %s value too low: value = %d limit %d\n", 1668 conf_name, limit->name, value, limit->bottom); 1669 } else if (value > limit->top) { 1670 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 1671 "%s %s value too high: value = %d limit %d\n", 1672 conf_name, limit->name, value, limit->top); 1673 } else { 1674 ST_DEBUG3(ST_DEVINFO, st_label, CE_CONT, 1675 "%s %s value = 0x%X\n", 1676 conf_name, limit->name, value); 1677 } 1678 1679 /* If not the number of densities continue */ 1680 if (limit != &conf_limits[4]) { 1681 continue; 1682 } 1683 1684 /* If number of densities is not in range can't use config */ 1685 if (value < limit->bottom || value > limit->top) { 1686 return (-1); 1687 } 1688 1689 ndens = min(value, NDENSITIES); 1690 if ((type == 1) && (list_len - ndens) != 6) { 1691 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 1692 "%s conf version 1 with %d densities has %d items" 1693 " should have %d", 1694 conf_name, ndens, list_len, 6 + ndens); 1695 } else if ((type == 2) && (list_len - ndens) != 13) { 1696 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 1697 "%s conf version 2 with %d densities has %d items" 1698 " should have %d", 1699 conf_name, ndens, list_len, 13 + ndens); 1700 } 1701 1702 limit++; 1703 for (dens = 0; dens < ndens && count; dens++) { 1704 count--; 1705 list++; 1706 value = *list; 1707 if (value < limit->bottom) { 1708 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 1709 "%s density[%d] value too low: value =" 1710 " 0x%X limit 0x%X\n", 1711 conf_name, dens, value, limit->bottom); 1712 } else if (value > limit->top) { 1713 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 1714 "%s density[%d] value too high: value =" 1715 " 0x%X limit 0x%X\n", 1716 conf_name, dens, value, limit->top); 1717 } else { 1718 ST_DEBUG3(ST_DEVINFO, st_label, CE_CONT, 1719 "%s density[%d] value = 0x%X\n", 1720 conf_name, dens, value); 1721 } 1722 } 1723 } 1724 1725 return (0); 1726 } 1727 1728 static int 1729 st_get_conf_from_st_dot_conf(struct scsi_tape *un, char *vidpid, 1730 struct st_drivetype *dp) 1731 { 1732 caddr_t config_list = NULL; 1733 caddr_t data_list = NULL; 1734 int *data_ptr; 1735 caddr_t vidptr, prettyptr, datanameptr; 1736 size_t vidlen, prettylen, datanamelen, tripletlen = 0; 1737 int config_list_len, data_list_len, len, i; 1738 int version; 1739 int found = 0; 1740 1741 1742 /* 1743 * Determine type of tape controller. Type is determined by 1744 * checking the vendor ids of the earlier inquiry command and 1745 * comparing those with vids in tape-config-list defined in st.conf 1746 */ 1747 if (ddi_getlongprop(DDI_DEV_T_ANY, ST_DEVINFO, DDI_PROP_DONTPASS, 1748 "tape-config-list", (caddr_t)&config_list, &config_list_len) 1749 != DDI_PROP_SUCCESS) { 1750 return (found); 1751 } 1752 1753 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 1754 "st_get_conf_from_st_dot_conf(): st.conf has tape-config-list\n"); 1755 1756 /* 1757 * Compare vids in each triplet - if it matches, get value for 1758 * data_name and contruct a st_drivetype struct 1759 * tripletlen is not set yet! 1760 */ 1761 for (len = config_list_len, vidptr = config_list; 1762 len > 0; 1763 vidptr += tripletlen, len -= tripletlen) { 1764 1765 vidlen = strlen(vidptr); 1766 prettyptr = vidptr + vidlen + 1; 1767 prettylen = strlen(prettyptr); 1768 datanameptr = prettyptr + prettylen + 1; 1769 datanamelen = strlen(datanameptr); 1770 tripletlen = vidlen + prettylen + datanamelen + 3; 1771 1772 if (vidlen == 0) { 1773 continue; 1774 } 1775 1776 /* 1777 * If inquiry vid dosen't match this triplets vid, 1778 * try the next. 1779 */ 1780 if (strncasecmp(vidpid, vidptr, vidlen)) { 1781 continue; 1782 } 1783 1784 /* 1785 * if prettylen is zero then use the vid string 1786 */ 1787 if (prettylen == 0) { 1788 prettyptr = vidptr; 1789 prettylen = vidlen; 1790 } 1791 1792 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 1793 "vid = %s, pretty=%s, dataname = %s\n", 1794 vidptr, prettyptr, datanameptr); 1795 1796 /* 1797 * get the data list 1798 */ 1799 if (ddi_getlongprop(DDI_DEV_T_ANY, ST_DEVINFO, 0, 1800 datanameptr, (caddr_t)&data_list, 1801 &data_list_len) != DDI_PROP_SUCCESS) { 1802 /* 1803 * Error in getting property value 1804 * print warning! 1805 */ 1806 scsi_log(ST_DEVINFO, st_label, CE_WARN, 1807 "data property (%s) has no value\n", 1808 datanameptr); 1809 continue; 1810 } 1811 1812 /* 1813 * now initialize the st_drivetype struct 1814 */ 1815 (void) strncpy(dp->name, prettyptr, ST_NAMESIZE - 1); 1816 dp->length = (int)min(vidlen, (VIDPIDLEN - 1)); 1817 (void) strncpy(dp->vid, vidptr, dp->length); 1818 data_ptr = (int *)data_list; 1819 /* 1820 * check if data is enough for version, type, 1821 * bsize, options, # of densities, density1, 1822 * density2, ..., default_density 1823 */ 1824 if ((data_list_len < 5 * sizeof (int)) || 1825 (data_list_len < 6 * sizeof (int) + 1826 *(data_ptr + 4) * sizeof (int))) { 1827 /* 1828 * print warning and skip to next triplet. 1829 */ 1830 scsi_log(ST_DEVINFO, st_label, CE_WARN, 1831 "data property (%s) incomplete\n", 1832 datanameptr); 1833 kmem_free(data_list, data_list_len); 1834 continue; 1835 } 1836 1837 if (st_validate_conf_data(un, data_ptr, 1838 data_list_len / sizeof (int), datanameptr)) { 1839 kmem_free(data_list, data_list_len); 1840 scsi_log(ST_DEVINFO, st_label, CE_WARN, 1841 "data property (%s) rejected\n", 1842 datanameptr); 1843 continue; 1844 } 1845 1846 /* 1847 * check version 1848 */ 1849 version = *data_ptr++; 1850 if (version != 1 && version != 2) { 1851 /* print warning but accept it */ 1852 scsi_log(ST_DEVINFO, st_label, CE_WARN, 1853 "Version # for data property (%s) " 1854 "not set to 1 or 2\n", datanameptr); 1855 } 1856 1857 dp->type = *data_ptr++; 1858 dp->bsize = *data_ptr++; 1859 dp->options = *data_ptr++; 1860 dp->options |= ST_DYNAMIC; 1861 len = *data_ptr++; 1862 for (i = 0; i < NDENSITIES; i++) { 1863 if (i < len) { 1864 dp->densities[i] = *data_ptr++; 1865 } 1866 } 1867 dp->default_density = *data_ptr << 3; 1868 if (version == 2 && 1869 data_list_len >= (13 + len) * sizeof (int)) { 1870 data_ptr++; 1871 dp->non_motion_timeout = *data_ptr++; 1872 dp->io_timeout = *data_ptr++; 1873 dp->rewind_timeout = *data_ptr++; 1874 dp->space_timeout = *data_ptr++; 1875 dp->load_timeout = *data_ptr++; 1876 dp->unload_timeout = *data_ptr++; 1877 dp->erase_timeout = *data_ptr++; 1878 } 1879 kmem_free(data_list, data_list_len); 1880 found = 1; 1881 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 1882 "found in st.conf: vid = %s, pretty=%s\n", 1883 dp->vid, dp->name); 1884 break; 1885 } 1886 1887 /* 1888 * free up the memory allocated by ddi_getlongprop 1889 */ 1890 if (config_list) { 1891 kmem_free(config_list, config_list_len); 1892 } 1893 return (found); 1894 } 1895 1896 static int 1897 st_get_conf_from_st_conf_dot_c(struct scsi_tape *un, char *vidpid, 1898 struct st_drivetype *dp) 1899 { 1900 int i; 1901 1902 /* 1903 * Determine type of tape controller. Type is determined by 1904 * checking the result of the earlier inquiry command and 1905 * comparing vendor ids with strings in a table declared in st_conf.c. 1906 */ 1907 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 1908 "st_get_conf_from_st_conf_dot_c(): looking at st_drivetypes\n"); 1909 1910 for (i = 0; i < st_ndrivetypes; i++) { 1911 if (st_drivetypes[i].length == 0) { 1912 continue; 1913 } 1914 if (strncasecmp(vidpid, st_drivetypes[i].vid, 1915 st_drivetypes[i].length)) { 1916 continue; 1917 } 1918 bcopy(&st_drivetypes[i], dp, sizeof (st_drivetypes[i])); 1919 return (1); 1920 } 1921 return (0); 1922 } 1923 1924 static int 1925 st_get_default_conf(struct scsi_tape *un, char *vidpid, struct st_drivetype *dp) 1926 { 1927 int i; 1928 1929 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 1930 "st_get_default_conf(): making drivetype from INQ cmd\n"); 1931 1932 1933 /* 1934 * Make up a name 1935 */ 1936 bcopy("Vendor '", dp->name, 8); 1937 bcopy(vidpid, &dp->name[8], VIDLEN); 1938 bcopy("' Product '", &dp->name[16], 11); 1939 bcopy(&vidpid[8], &dp->name[27], PIDLEN); 1940 dp->name[ST_NAMESIZE - 2] = '\''; 1941 dp->name[ST_NAMESIZE - 1] = '\0'; 1942 dp->length = min(strlen(ST_INQUIRY->inq_vid), (VIDPIDLEN - 1)); 1943 (void) strncpy(dp->vid, ST_INQUIRY->inq_vid, dp->length); 1944 /* 1945 * 'clean' vendor and product strings of non-printing chars 1946 */ 1947 for (i = 0; i < ST_NAMESIZE - 2; i++) { 1948 if (dp->name[i] < ' ' || dp->name[i] > '~') { 1949 dp->name[i] = '.'; 1950 } 1951 } 1952 dp->type = ST_TYPE_INVALID; 1953 dp->options |= (ST_DYNAMIC | ST_UNLOADABLE | ST_MODE_SEL_COMP); 1954 1955 return (1); /* Can Not Fail */ 1956 } 1957 1958 /* 1959 * Regular Unix Entry points 1960 */ 1961 1962 1963 1964 /* ARGSUSED */ 1965 static int 1966 st_open(dev_t *dev_p, int flag, int otyp, cred_t *cred_p) 1967 { 1968 dev_t dev = *dev_p; 1969 int rval = 0; 1970 1971 GET_SOFT_STATE(dev); 1972 1973 /* 1974 * validate that we are addressing a sensible unit 1975 */ 1976 mutex_enter(ST_MUTEX); 1977 1978 #ifdef STDEBUG 1979 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 1980 "st_open(node = %s dev = 0x%lx, flag = %d, otyp = %d)\n", 1981 st_dev_name(dev), *dev_p, flag, otyp); 1982 #endif 1983 1984 /* 1985 * All device accesss go thru st_strategy() where we check 1986 * suspend status 1987 */ 1988 1989 if (!un->un_attached) { 1990 st_known_tape_type(un); 1991 if (!un->un_attached) { 1992 rval = ENXIO; 1993 goto exit; 1994 } 1995 1996 } 1997 1998 /* 1999 * Check for the case of the tape in the middle of closing. 2000 * This isn't simply a check of the current state, because 2001 * we could be in state of sensing with the previous state 2002 * that of closing. 2003 * 2004 * And don't allow multiple opens. 2005 */ 2006 if (!(flag & (FNDELAY | FNONBLOCK)) && IS_CLOSING(un)) { 2007 un->un_laststate = un->un_state; 2008 un->un_state = ST_STATE_CLOSE_PENDING_OPEN; 2009 while (IS_CLOSING(un) || 2010 un->un_state == ST_STATE_CLOSE_PENDING_OPEN) { 2011 if (cv_wait_sig(&un->un_clscv, ST_MUTEX) == 0) { 2012 rval = EINTR; 2013 un->un_state = un->un_laststate; 2014 goto exit; 2015 } 2016 } 2017 } else if (un->un_state != ST_STATE_CLOSED) { 2018 rval = EBUSY; 2019 goto busy; 2020 } 2021 2022 /* 2023 * record current dev 2024 */ 2025 un->un_dev = dev; 2026 un->un_oflags = flag; /* save for use in st_tape_init() */ 2027 un->un_errno = 0; /* no errors yet */ 2028 un->un_restore_pos = 0; 2029 un->un_rqs_state = 0; 2030 2031 /* 2032 * If we are opening O_NDELAY, or O_NONBLOCK, we don't check for 2033 * anything, leave internal states alone, if fileno >= 0 2034 */ 2035 if (flag & (FNDELAY | FNONBLOCK)) { 2036 if (un->un_fileno < 0 || (un->un_fileno == 0 && 2037 un->un_blkno == 0)) { 2038 un->un_state = ST_STATE_OFFLINE; 2039 } else if (un->un_fileno > 0 || 2040 (un->un_fileno == 0 && un->un_blkno != 0)) { 2041 /* 2042 * set un_read_only/write-protect status. 2043 * 2044 * If the tape is not bot we can assume 2045 * that mspl->wp_status is set properly. 2046 * else 2047 * we need to do a mode sense/Tur once 2048 * again to get the actual tape status.(since 2049 * user might have replaced the tape) 2050 * Hence make the st state OFFLINE so that 2051 * we re-intialize the tape once again. 2052 */ 2053 un->un_read_only = 2054 (un->un_oflags & FWRITE) ? RDWR : RDONLY; 2055 un->un_state = ST_STATE_OPEN_PENDING_IO; 2056 } else { 2057 un->un_state = ST_STATE_OFFLINE; 2058 } 2059 rval = 0; 2060 } else { 2061 /* 2062 * Not opening O_NDELAY. 2063 */ 2064 un->un_state = ST_STATE_OPENING; 2065 2066 rval = st_tape_init(dev); 2067 if ((rval == EACCES) && (un->un_read_only & WORM)) { 2068 un->un_state = ST_STATE_OPEN_PENDING_IO; 2069 rval = 0; /* so open doesn't fail */ 2070 } else if (rval) { 2071 /* 2072 * Release the tape unit, if reserved and not 2073 * preserve reserve. 2074 */ 2075 if ((un->un_rsvd_status & 2076 (ST_RESERVE | ST_PRESERVE_RESERVE)) == ST_RESERVE) { 2077 (void) st_reserve_release(un, ST_RELEASE); 2078 } 2079 } else { 2080 un->un_state = ST_STATE_OPEN_PENDING_IO; 2081 } 2082 } 2083 2084 exit: 2085 /* 2086 * we don't want any uninvited guests scrogging our data when we're 2087 * busy with something, so for successful opens or failed opens 2088 * (except for EBUSY), reset these counters and state appropriately. 2089 */ 2090 if (rval != EBUSY) { 2091 if (rval) { 2092 un->un_state = ST_STATE_CLOSED; 2093 } 2094 un->un_err_resid = 0; 2095 un->un_retry_ct = 0; 2096 un->un_tran_retry_ct = 0; 2097 } 2098 busy: 2099 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 2100 "st_open: return val = %x, state = %d\n", rval, un->un_state); 2101 mutex_exit(ST_MUTEX); 2102 return (rval); 2103 2104 } 2105 2106 static int 2107 st_tape_init(dev_t dev) 2108 { 2109 int err; 2110 int rval = 0; 2111 2112 GET_SOFT_STATE(dev); 2113 2114 ASSERT(mutex_owned(ST_MUTEX)); 2115 2116 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 2117 "st_tape_init(dev = 0x%lx, oflags = %d)\n", dev, un->un_oflags); 2118 2119 /* 2120 * Clean up after any errors left by 'last' close. 2121 * This also handles the case of the initial open. 2122 */ 2123 if (un->un_state != ST_STATE_INITIALIZING) { 2124 un->un_laststate = un->un_state; 2125 un->un_state = ST_STATE_OPENING; 2126 } 2127 2128 un->un_kbytes_xferred = 0; 2129 2130 /* 2131 * do a throw away TUR to clear check condition 2132 */ 2133 err = st_cmd(dev, SCMD_TEST_UNIT_READY, 0, SYNC_CMD); 2134 2135 /* 2136 * If test unit ready fails because the drive is reserved 2137 * by another host fail the open for no access. 2138 */ 2139 if (err) { 2140 if (un->un_rsvd_status & ST_RESERVATION_CONFLICT) { 2141 un->un_state = ST_STATE_CLOSED; 2142 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 2143 "st_tape_init: RESERVATION CONFLICT\n"); 2144 rval = EACCES; 2145 goto exit; 2146 } 2147 } 2148 2149 /* 2150 * See whether this is a generic device that we haven't figured 2151 * anything out about yet. 2152 */ 2153 if (un->un_dp->type == ST_TYPE_INVALID) { 2154 rval = st_determine_generic(dev); 2155 if (rval) { 2156 if (rval != EACCES) { 2157 rval = EIO; 2158 } 2159 un->un_state = ST_STATE_CLOSED; 2160 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2161 "st_tape_init: %s invalid type\n", 2162 rval == EACCES ? "EACCES" : "EIO"); 2163 goto exit; 2164 } 2165 /* 2166 * If this is a Unknown Type drive, 2167 * Use the READ BLOCK LIMITS to determine if 2168 * allow large xfer is approprate if not globally 2169 * disabled with st_allow_large_xfer. 2170 */ 2171 un->un_allow_large_xfer = (uchar_t)st_allow_large_xfer; 2172 } else { 2173 2174 /* 2175 * If we allow_large_xfer (ie >64k) and have not yet found out 2176 * the max block size supported by the drive, 2177 * find it by issueing a READ_BLKLIM command. 2178 * if READ_BLKLIM cmd fails, assume drive doesn't 2179 * allow_large_xfer and min/max block sizes as 1 byte and 63k. 2180 */ 2181 un->un_allow_large_xfer = st_allow_large_xfer && 2182 (un->un_dp->options & ST_NO_RECSIZE_LIMIT); 2183 } 2184 /* 2185 * if maxbsize is unknown, set the maximum block size. 2186 */ 2187 if (un->un_maxbsize == MAXBSIZE_UNKNOWN) { 2188 2189 /* 2190 * Get the Block limits of the tape drive. 2191 * if un->un_allow_large_xfer = 0 , then make sure 2192 * that maxbsize is <= ST_MAXRECSIZE_FIXED. 2193 */ 2194 un->un_rbl = kmem_zalloc(RBLSIZE, KM_SLEEP); 2195 2196 err = st_cmd(dev, SCMD_READ_BLKLIM, RBLSIZE, SYNC_CMD); 2197 if (err) { 2198 /* Retry */ 2199 err = st_cmd(dev, SCMD_READ_BLKLIM, RBLSIZE, SYNC_CMD); 2200 } 2201 if (!err) { 2202 2203 /* 2204 * if cmd successful, use limit returned 2205 */ 2206 un->un_maxbsize = (un->un_rbl->max_hi << 16) + 2207 (un->un_rbl->max_mid << 8) + 2208 un->un_rbl->max_lo; 2209 un->un_minbsize = (un->un_rbl->min_hi << 8) + 2210 un->un_rbl->min_lo; 2211 un->un_data_mod = 1 << un->un_rbl->granularity; 2212 if ((un->un_maxbsize == 0) || 2213 (un->un_allow_large_xfer == 0 && 2214 un->un_maxbsize > ST_MAXRECSIZE_FIXED)) { 2215 un->un_maxbsize = ST_MAXRECSIZE_FIXED; 2216 2217 } else if (un->un_dp->type == ST_TYPE_DEFAULT) { 2218 /* 2219 * Drive is not one that is configured, But the 2220 * READ BLOCK LIMITS tells us it can do large 2221 * xfers. 2222 */ 2223 if (un->un_maxbsize > ST_MAXRECSIZE_FIXED) { 2224 un->un_dp->options |= 2225 ST_NO_RECSIZE_LIMIT; 2226 } 2227 /* 2228 * If max and mimimum block limits are the 2229 * same this is a fixed block size device. 2230 */ 2231 if (un->un_maxbsize == un->un_minbsize) { 2232 un->un_dp->options &= ~ST_VARIABLE; 2233 } 2234 } 2235 2236 if (un->un_minbsize == 0) { 2237 un->un_minbsize = 1; 2238 } 2239 2240 } else { /* error on read block limits */ 2241 2242 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 2243 "!st_tape_init: Error on READ BLOCK LIMITS," 2244 " errno = %d un_rsvd_status = 0x%X\n", 2245 err, un->un_rsvd_status); 2246 2247 /* 2248 * since read block limits cmd failed, 2249 * do not allow large xfers. 2250 * use old values in st_minphys 2251 */ 2252 if (un->un_rsvd_status & ST_RESERVATION_CONFLICT) { 2253 rval = EACCES; 2254 } else { 2255 un->un_allow_large_xfer = 0; 2256 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 2257 "!Disabling large transfers\n"); 2258 2259 /* 2260 * we guess maxbsize and minbsize 2261 */ 2262 if (un->un_bsize) { 2263 un->un_maxbsize = un->un_minbsize = 2264 un->un_bsize; 2265 } else { 2266 un->un_maxbsize = ST_MAXRECSIZE_FIXED; 2267 un->un_minbsize = 1; 2268 } 2269 /* 2270 * Data Mod must be set, 2271 * Even if read block limits fails. 2272 * Prevents Divide By Zero in st_rw(). 2273 */ 2274 un->un_data_mod = 1; 2275 } 2276 } 2277 if (un->un_rbl) { 2278 kmem_free(un->un_rbl, RBLSIZE); 2279 un->un_rbl = NULL; 2280 } 2281 2282 if (rval) { 2283 goto exit; 2284 } 2285 } 2286 2287 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 2288 "maxdma = %d, maxbsize = %d, minbsize = %d, %s large xfer\n", 2289 un->un_maxdma, un->un_maxbsize, un->un_minbsize, 2290 (un->un_allow_large_xfer ? "ALLOW": "DON'T ALLOW")); 2291 2292 err = st_cmd(dev, SCMD_TEST_UNIT_READY, 0, SYNC_CMD); 2293 2294 if (err != 0) { 2295 if (err == EINTR) { 2296 un->un_laststate = un->un_state; 2297 un->un_state = ST_STATE_CLOSED; 2298 rval = EINTR; 2299 goto exit; 2300 } 2301 /* 2302 * Make sure the tape is ready 2303 */ 2304 un->un_fileno = -1; 2305 if (un->un_status != KEY_UNIT_ATTENTION) { 2306 /* 2307 * allow open no media. Subsequent MTIOCSTATE 2308 * with media present will complete the open 2309 * logic. 2310 */ 2311 un->un_laststate = un->un_state; 2312 if (un->un_oflags & (FNONBLOCK|FNDELAY)) { 2313 un->un_mediastate = MTIO_EJECTED; 2314 un->un_state = ST_STATE_OFFLINE; 2315 rval = 0; 2316 goto exit; 2317 } else { 2318 un->un_state = ST_STATE_CLOSED; 2319 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2320 "st_tape_init EIO no media, not opened " 2321 "O_NONBLOCK|O_EXCL\n"); 2322 rval = EIO; 2323 goto exit; 2324 } 2325 } 2326 } 2327 2328 /* 2329 * On each open, initialize block size from drivetype struct, 2330 * as it could have been changed by MTSRSZ ioctl. 2331 * Now, ST_VARIABLE simply means drive is capable of variable 2332 * mode. All drives are assumed to support fixed records. 2333 * Hence, un_bsize tells what mode the drive is in. 2334 * un_bsize = 0 - variable record length 2335 * = x - fixed record length is x 2336 */ 2337 un->un_bsize = un->un_dp->bsize; 2338 2339 if (un->un_restore_pos) { 2340 rval = st_validate_tapemarks(un, un->un_save_fileno, 2341 un->un_save_blkno); 2342 if (rval != 0) { 2343 if (rval != EACCES) { 2344 rval = EIO; 2345 } 2346 un->un_restore_pos = 0; 2347 un->un_laststate = un->un_state; 2348 un->un_state = ST_STATE_CLOSED; 2349 goto exit; 2350 } 2351 un->un_restore_pos = 0; 2352 } 2353 2354 if (un->un_fileno < 0) { 2355 rval = st_loadtape(dev); 2356 if (rval) { 2357 if (rval != EACCES) { 2358 rval = EIO; 2359 } 2360 un->un_laststate = un->un_state; 2361 un->un_state = ST_STATE_CLOSED; 2362 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2363 "st_tape_init: %s can't open tape\n", 2364 rval == EACCES ? "EACCES" : "EIO"); 2365 goto exit; 2366 } 2367 } 2368 2369 /* 2370 * do a mode sense to pick up state of current write-protect, 2371 * Could cause reserve and fail due to conflict. 2372 */ 2373 rval = st_modesense(un); 2374 if (rval == EACCES) { 2375 goto exit; 2376 } 2377 2378 /* 2379 * If we are opening the tape for writing, check 2380 * to make sure that the tape can be written. 2381 */ 2382 if (un->un_oflags & FWRITE) { 2383 err = 0; 2384 if (un->un_mspl->wp) { 2385 un->un_status = KEY_WRITE_PROTECT; 2386 un->un_laststate = un->un_state; 2387 un->un_state = ST_STATE_CLOSED; 2388 rval = EACCES; 2389 /* 2390 * STK sets the wp bit if volsafe tape is loaded. 2391 */ 2392 if ((un->un_dp->type == MT_ISSTK9840) && 2393 (un->un_dp->options & ST_WORMABLE)) { 2394 un->un_read_only = RDONLY; 2395 } else { 2396 goto exit; 2397 } 2398 } else { 2399 un->un_read_only = RDWR; 2400 } 2401 } else { 2402 un->un_read_only = RDONLY; 2403 } 2404 2405 if (un->un_dp->options & ST_WORMABLE) { 2406 un->un_read_only |= un->un_wormable(un); 2407 2408 if (((un->un_read_only == WORM) || 2409 (un->un_read_only == RDWORM)) && 2410 ((un->un_oflags & FWRITE) == FWRITE)) { 2411 un->un_status = KEY_DATA_PROTECT; 2412 rval = EACCES; 2413 ST_DEBUG4(ST_DEVINFO, st_label, CE_NOTE, 2414 "read_only = %d eof = %d oflag = %d\n", 2415 un->un_read_only, un->un_eof, un->un_oflags); 2416 } 2417 } 2418 2419 /* 2420 * If we're opening the tape write-only, we need to 2421 * write 2 filemarks on the HP 1/2 inch drive, to 2422 * create a null file. 2423 */ 2424 if ((un->un_read_only == RDWR) || 2425 (un->un_read_only == WORM) && (un->un_oflags & FWRITE)) { 2426 if (un->un_dp->options & ST_REEL) { 2427 un->un_fmneeded = 2; 2428 } else { 2429 un->un_fmneeded = 1; 2430 } 2431 } else { 2432 un->un_fmneeded = 0; 2433 } 2434 2435 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 2436 "fmneeded = %x\n", un->un_fmneeded); 2437 2438 /* 2439 * Make sure the density can be selected correctly. 2440 * If WORM can only write at the append point which in most cases 2441 * isn't BOP. st_determine_density() with a B_WRITE only attempts 2442 * to set and try densities if a BOP. 2443 */ 2444 if (st_determine_density(dev, 2445 un->un_read_only == RDWR ? B_WRITE : B_READ)) { 2446 un->un_status = KEY_ILLEGAL_REQUEST; 2447 un->un_laststate = un->un_state; 2448 un->un_state = ST_STATE_CLOSED; 2449 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 2450 "st_tape_init: EIO can't determine density\n"); 2451 rval = EIO; 2452 goto exit; 2453 } 2454 2455 /* 2456 * Destroy the knowledge that we have 'determined' 2457 * density so that a later read at BOT comes along 2458 * does the right density determination. 2459 */ 2460 2461 un->un_density_known = 0; 2462 2463 2464 /* 2465 * Okay, the tape is loaded and either at BOT or somewhere past. 2466 * Mark the state such that any I/O or tape space operations 2467 * will get/set the right density, etc.. 2468 */ 2469 un->un_laststate = un->un_state; 2470 un->un_lastop = ST_OP_NIL; 2471 un->un_mediastate = MTIO_INSERTED; 2472 cv_broadcast(&un->un_state_cv); 2473 2474 /* 2475 * Set test append flag if writing. 2476 * First write must check that tape is positioned correctly. 2477 */ 2478 un->un_test_append = (un->un_oflags & FWRITE); 2479 2480 exit: 2481 un->un_err_resid = 0; 2482 un->un_last_resid = 0; 2483 un->un_last_count = 0; 2484 2485 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 2486 "st_tape_init: return val = %x\n", rval); 2487 return (rval); 2488 2489 } 2490 2491 2492 2493 /* ARGSUSED */ 2494 static int 2495 st_close(dev_t dev, int flag, int otyp, cred_t *cred_p) 2496 { 2497 int err = 0; 2498 int norew, count, last_state; 2499 #if defined(__i386) || defined(__amd64) 2500 struct contig_mem *cp, *cp_temp; 2501 #endif 2502 2503 GET_SOFT_STATE(dev); 2504 2505 /* 2506 * wait till all cmds in the pipeline have been completed 2507 */ 2508 mutex_enter(ST_MUTEX); 2509 2510 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 2511 "st_close(dev = 0x%lx, flag = %d, otyp = %d)\n", dev, flag, otyp); 2512 2513 st_wait_for_io(un); 2514 2515 /* turn off persistent errors on close, as we want close to succeed */ 2516 TURN_PE_OFF(un); 2517 2518 /* 2519 * set state to indicate that we are in process of closing 2520 */ 2521 last_state = un->un_laststate = un->un_state; 2522 un->un_state = ST_STATE_CLOSING; 2523 2524 /* 2525 * BSD behavior: 2526 * a close always causes a silent span to the next file if we've hit 2527 * an EOF (but not yet read across it). 2528 */ 2529 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 2530 "st_close1: fileno=%x, blkno=%lx, un_eof=%x\n", un->un_fileno, 2531 un->un_blkno, un->un_eof); 2532 2533 2534 if (BSD_BEHAVIOR && (un->un_eof == ST_EOF)) { 2535 if (un->un_fileno >= 0) { 2536 un->un_fileno++; 2537 un->un_blkno = 0; 2538 } 2539 un->un_eof = ST_NO_EOF; 2540 } 2541 2542 /* 2543 * rewinding? 2544 */ 2545 norew = (getminor(dev) & MT_NOREWIND); 2546 2547 /* 2548 * SVR4 behavior for skipping to next file: 2549 * 2550 * If we have not seen a filemark, space to the next file 2551 * 2552 * If we have already seen the filemark we are physically in the next 2553 * file and we only increment the filenumber 2554 */ 2555 2556 if (norew && SVR4_BEHAVIOR && (flag & FREAD) && (un->un_blkno != 0) && 2557 (un->un_lastop != ST_OP_WRITE)) { 2558 switch (un->un_eof) { 2559 case ST_NO_EOF: 2560 /* 2561 * if we were reading and did not read the complete file 2562 * skip to the next file, leaving the tape correctly 2563 * positioned to read the first record of the next file 2564 * Check first for REEL if we are at EOT by trying to 2565 * read a block 2566 */ 2567 if ((un->un_dp->options & ST_REEL) && 2568 (!(un->un_dp->options & ST_READ_IGNORE_EOFS)) && 2569 (un->un_blkno == 0)) { 2570 if (st_cmd(dev, SCMD_SPACE, Blk(1), SYNC_CMD)) { 2571 ST_DEBUG2(ST_DEVINFO, st_label, 2572 SCSI_DEBUG, 2573 "st_close : EIO can't space\n"); 2574 err = EIO; 2575 break; 2576 } 2577 if (un->un_eof >= ST_EOF_PENDING) { 2578 un->un_eof = ST_EOT_PENDING; 2579 un->un_fileno += 1; 2580 un->un_blkno = 0; 2581 break; 2582 } 2583 } 2584 if (st_cmd(dev, SCMD_SPACE, Fmk(1), SYNC_CMD)) { 2585 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2586 "st_close: EIO can't space #2\n"); 2587 err = EIO; 2588 } else { 2589 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 2590 "st_close2: fileno=%x,blkno=%lx," 2591 "un_eof=%x\n", 2592 un->un_fileno, un->un_blkno, un->un_eof); 2593 un->un_eof = ST_NO_EOF; 2594 } 2595 break; 2596 2597 case ST_EOF_PENDING: 2598 case ST_EOF: 2599 un->un_fileno += 1; 2600 un->un_blkno = 0; 2601 un->un_eof = ST_NO_EOF; 2602 break; 2603 2604 case ST_EOT: 2605 case ST_EOT_PENDING: 2606 /* nothing to do */ 2607 break; 2608 default: 2609 scsi_log(ST_DEVINFO, st_label, CE_PANIC, 2610 "Undefined state 0x%x", un->un_eof); 2611 2612 } 2613 } 2614 2615 2616 /* 2617 * For performance reasons (HP 88780), the driver should 2618 * postpone writing the second tape mark until just before a file 2619 * positioning ioctl is issued (e.g., rewind). This means that 2620 * the user must not manually rewind the tape because the tape will 2621 * be missing the second tape mark which marks EOM. 2622 * However, this small performance improvement is not worth the risk. 2623 */ 2624 2625 /* 2626 * We need to back up over the filemark we inadvertently popped 2627 * over doing a read in between the two filemarks that constitute 2628 * logical eot for 1/2" tapes. Note that ST_EOT_PENDING is only 2629 * set while reading. 2630 * 2631 * If we happen to be at physical eot (ST_EOM) (writing case), 2632 * the writing of filemark(s) will clear the ST_EOM state, which 2633 * we don't want, so we save this state and restore it later. 2634 */ 2635 2636 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 2637 "flag=%x, fmneeded=%x, lastop=%x, eof=%x\n", 2638 flag, un->un_fmneeded, un->un_lastop, un->un_eof); 2639 2640 if (un->un_eof == ST_EOT_PENDING) { 2641 if (norew) { 2642 if (st_cmd(dev, SCMD_SPACE, Fmk((-1)), SYNC_CMD)) { 2643 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2644 "st_close: EIO can't space #3\n"); 2645 err = EIO; 2646 } else { 2647 un->un_blkno = 0; 2648 un->un_eof = ST_EOT; 2649 } 2650 } else { 2651 un->un_eof = ST_NO_EOF; 2652 } 2653 2654 /* 2655 * Do we need to write a file mark? 2656 * 2657 * only write filemarks if there are fmks to be written and 2658 * - open for write (possibly read/write) 2659 * - the last operation was a write 2660 * or: 2661 * - opened for wronly 2662 * - no data was written 2663 */ 2664 } else if ((un->un_fileno >= 0) && (un->un_fmneeded > 0) && 2665 (((flag & FWRITE) && (un->un_lastop == ST_OP_WRITE)) || 2666 ((flag & FWRITE) && (un->un_lastop == ST_OP_WEOF)) || 2667 ((flag == FWRITE) && (un->un_lastop == ST_OP_NIL)))) { 2668 2669 /* save ST_EOM state */ 2670 int was_at_eom = (un->un_eof == ST_EOM) ? 1 : 0; 2671 2672 /* 2673 * Note that we will write a filemark if we had opened 2674 * the tape write only and no data was written, thus 2675 * creating a null file. 2676 * 2677 * If the user already wrote one, we only have to write 1 more. 2678 * If they wrote two, we don't have to write any. 2679 */ 2680 2681 count = un->un_fmneeded; 2682 if (count > 0) { 2683 if (st_cmd(dev, SCMD_WRITE_FILE_MARK, 2684 count, SYNC_CMD)) { 2685 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2686 "st_close : EIO can't wfm\n"); 2687 err = EIO; 2688 } 2689 if ((un->un_dp->options & ST_REEL) && norew) { 2690 if (st_cmd(dev, SCMD_SPACE, Fmk((-1)), 2691 SYNC_CMD)) { 2692 ST_DEBUG2(ST_DEVINFO, st_label, 2693 SCSI_DEBUG, 2694 "st_close : EIO space fmk(-1)\n"); 2695 err = EIO; 2696 } 2697 un->un_eof = ST_NO_EOF; 2698 /* fix up block number */ 2699 un->un_blkno = 0; 2700 } 2701 } 2702 2703 /* 2704 * If we aren't going to be rewinding, and we were at 2705 * physical eot, restore the state that indicates we 2706 * are at physical eot. Once you have reached physical 2707 * eot, and you close the tape, the only thing you can 2708 * do on the next open is to rewind. Access to trailer 2709 * records is only allowed without closing the device. 2710 */ 2711 if (norew == 0 && was_at_eom) 2712 un->un_eof = ST_EOM; 2713 } 2714 2715 /* 2716 * report soft errors if enabled and available, if we never accessed 2717 * the drive, don't get errors. This will prevent some DAT error 2718 * messages upon LOG SENSE. 2719 */ 2720 if (st_report_soft_errors_on_close && 2721 (un->un_dp->options & ST_SOFT_ERROR_REPORTING) && 2722 (last_state != ST_STATE_OFFLINE)) { 2723 (void) st_report_soft_errors(dev, flag); 2724 } 2725 2726 2727 /* 2728 * Do we need to rewind? Can we rewind? 2729 */ 2730 if (norew == 0 && un->un_fileno >= 0 && err == 0) { 2731 /* 2732 * We'd like to rewind with the 2733 * 'immediate' bit set, but this 2734 * causes problems on some drives 2735 * where subsequent opens get a 2736 * 'NOT READY' error condition 2737 * back while the tape is rewinding, 2738 * which is impossible to distinguish 2739 * from the condition of 'no tape loaded'. 2740 * 2741 * Also, for some targets, if you disconnect 2742 * with the 'immediate' bit set, you don't 2743 * actually return right away, i.e., the 2744 * target ignores your request for immediate 2745 * return. 2746 * 2747 * Instead, we'll fire off an async rewind 2748 * command. We'll mark the device as closed, 2749 * and any subsequent open will stall on 2750 * the first TEST_UNIT_READY until the rewind 2751 * completes. 2752 */ 2753 2754 /* 2755 * Used to be if reserve was not supported we'd send an 2756 * asynchronious rewind. Comments above may be slightly invalid 2757 * as the immediate bit was never set. Doing an immedate rewind 2758 * makes sense, I think fixes to not ready status might handle 2759 * the problems described above. 2760 */ 2761 if (un->un_sd->sd_inq->inq_ansi < 2) { 2762 (void) st_cmd(dev, SCMD_REWIND, 0, SYNC_CMD); 2763 } else { 2764 (void) st_cmd(dev, SCMD_REWIND, 0, ASYNC_CMD); 2765 } 2766 } 2767 2768 /* 2769 * eject tape if necessary 2770 */ 2771 if (un->un_eject_tape_on_failure) { 2772 un->un_eject_tape_on_failure = 0; 2773 if (st_cmd(dev, SCMD_LOAD, 0, SYNC_CMD)) { 2774 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2775 "st_close : can't unload tape\n"); 2776 } else { 2777 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2778 "st_close : tape unloaded \n"); 2779 un->un_eof = ST_NO_EOF; 2780 un->un_mediastate = MTIO_EJECTED; 2781 } 2782 } 2783 /* 2784 * Release the tape unit, if default reserve/release 2785 * behaviour. 2786 */ 2787 if ((un->un_rsvd_status & 2788 (ST_RESERVE | ST_PRESERVE_RESERVE)) == ST_RESERVE) { 2789 (void) st_reserve_release(un, ST_RELEASE); 2790 } 2791 2792 /* 2793 * clear up state 2794 */ 2795 un->un_laststate = un->un_state; 2796 un->un_state = ST_STATE_CLOSED; 2797 un->un_lastop = ST_OP_NIL; 2798 un->un_throttle = 1; /* assume one request at time, for now */ 2799 un->un_retry_ct = 0; 2800 un->un_tran_retry_ct = 0; 2801 un->un_errno = 0; 2802 un->un_swr_token = (opaque_t)NULL; 2803 un->un_rsvd_status &= ~(ST_INIT_RESERVE); 2804 2805 /* Restore the options to the init time settings */ 2806 if (un->un_init_options & ST_READ_IGNORE_ILI) { 2807 un->un_dp->options |= ST_READ_IGNORE_ILI; 2808 } else { 2809 un->un_dp->options &= ~ST_READ_IGNORE_ILI; 2810 } 2811 2812 if (un->un_init_options & ST_READ_IGNORE_EOFS) { 2813 un->un_dp->options |= ST_READ_IGNORE_EOFS; 2814 } else { 2815 un->un_dp->options &= ~ST_READ_IGNORE_EOFS; 2816 } 2817 2818 if (un->un_init_options & ST_SHORT_FILEMARKS) { 2819 un->un_dp->options |= ST_SHORT_FILEMARKS; 2820 } else { 2821 un->un_dp->options &= ~ST_SHORT_FILEMARKS; 2822 } 2823 2824 ASSERT(mutex_owned(ST_MUTEX)); 2825 2826 /* 2827 * Signal anyone awaiting a close operation to complete. 2828 */ 2829 cv_signal(&un->un_clscv); 2830 2831 /* 2832 * any kind of error on closing causes all state to be tossed 2833 */ 2834 if (err && un->un_status != KEY_ILLEGAL_REQUEST) { 2835 un->un_density_known = 0; 2836 /* 2837 * note that st_intr has already set un_fileno to -1 2838 */ 2839 } 2840 2841 #if defined(__i386) || defined(__amd64) 2842 /* 2843 * free any contiguous mem alloc'ed for big block I/O 2844 */ 2845 cp = un->un_contig_mem; 2846 while (cp) { 2847 if (cp->cm_addr) { 2848 ddi_dma_mem_free(&cp->cm_acc_hdl); 2849 } 2850 cp_temp = cp; 2851 cp = cp->cm_next; 2852 kmem_free(cp_temp, 2853 sizeof (struct contig_mem) + biosize()); 2854 } 2855 un->un_contig_mem_total_num = 0; 2856 un->un_contig_mem_available_num = 0; 2857 un->un_contig_mem = NULL; 2858 un->un_max_contig_mem_len = 0; 2859 #endif 2860 2861 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 2862 "st_close3: return val = %x, fileno=%x, blkno=%lx, un_eof=%x\n", 2863 err, un->un_fileno, un->un_blkno, un->un_eof); 2864 2865 mutex_exit(ST_MUTEX); 2866 return (err); 2867 } 2868 2869 /* 2870 * These routines perform raw i/o operations. 2871 */ 2872 2873 /* ARGSUSED2 */ 2874 static int 2875 st_aread(dev_t dev, struct aio_req *aio, cred_t *cred_p) 2876 { 2877 return (st_arw(dev, aio, B_READ)); 2878 } 2879 2880 2881 /* ARGSUSED2 */ 2882 static int 2883 st_awrite(dev_t dev, struct aio_req *aio, cred_t *cred_p) 2884 { 2885 return (st_arw(dev, aio, B_WRITE)); 2886 } 2887 2888 2889 2890 /* ARGSUSED */ 2891 static int 2892 st_read(dev_t dev, struct uio *uiop, cred_t *cred_p) 2893 { 2894 return (st_rw(dev, uiop, B_READ)); 2895 } 2896 2897 /* ARGSUSED */ 2898 static int 2899 st_write(dev_t dev, struct uio *uiop, cred_t *cred_p) 2900 { 2901 return (st_rw(dev, uiop, B_WRITE)); 2902 } 2903 2904 /* 2905 * Due to historical reasons, old limits are: For variable-length devices: 2906 * if greater than 64KB - 1 (ST_MAXRECSIZE_VARIABLE), block into 64 KB - 2 2907 * ST_MAXRECSIZE_VARIABLE_LIMIT) requests; otherwise, 2908 * (let it through unmodified. For fixed-length record devices: 2909 * 63K (ST_MAXRECSIZE_FIXED) is max (default minphys). 2910 * 2911 * The new limits used are un_maxdma (retrieved using scsi_ifgetcap() 2912 * from the HBA) and un_maxbsize (retrieved by sending SCMD_READ_BLKLIM 2913 * command to the drive). 2914 * 2915 */ 2916 static void 2917 st_minphys(struct buf *bp) 2918 { 2919 struct scsi_tape *un; 2920 2921 un = ddi_get_soft_state(st_state, MTUNIT(bp->b_edev)); 2922 2923 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 2924 "st_minphys(bp = 0x%p): b_bcount = 0x%lx\n", (void *)bp, 2925 bp->b_bcount); 2926 2927 if (un->un_allow_large_xfer) { 2928 2929 /* 2930 * check un_maxbsize for variable length devices only 2931 */ 2932 if (un->un_bsize == 0 && bp->b_bcount > un->un_maxbsize) { 2933 bp->b_bcount = un->un_maxbsize; 2934 } 2935 /* 2936 * can't go more that HBA maxdma limit in either fixed-length 2937 * or variable-length tape drives. 2938 */ 2939 if (bp->b_bcount > un->un_maxdma) { 2940 bp->b_bcount = un->un_maxdma; 2941 } 2942 } else { 2943 2944 /* 2945 * use old fixed limits 2946 */ 2947 if (un->un_bsize == 0) { 2948 if (bp->b_bcount > ST_MAXRECSIZE_VARIABLE) { 2949 bp->b_bcount = ST_MAXRECSIZE_VARIABLE_LIMIT; 2950 } 2951 } else { 2952 if (bp->b_bcount > ST_MAXRECSIZE_FIXED) { 2953 bp->b_bcount = ST_MAXRECSIZE_FIXED; 2954 } 2955 } 2956 } 2957 2958 /* 2959 * For regular raw I/O and Fixed Block length devices, make sure 2960 * the adjusted block count is a whole multiple of the device 2961 * block size. 2962 */ 2963 if (bp != un->un_sbufp && un->un_bsize) { 2964 bp->b_bcount -= (bp->b_bcount % un->un_bsize); 2965 } 2966 } 2967 2968 static int 2969 st_rw(dev_t dev, struct uio *uio, int flag) 2970 { 2971 int rval = 0; 2972 long len; 2973 2974 GET_SOFT_STATE(dev); 2975 2976 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 2977 "st_rw(dev = 0x%lx, flag = %s)\n", dev, 2978 (flag == B_READ ? rd_str: wr_str)); 2979 2980 /* get local copy of transfer length */ 2981 len = uio->uio_iov->iov_len; 2982 2983 mutex_enter(ST_MUTEX); 2984 2985 /* 2986 * If in fixed block size mode and requested read or write 2987 * is not an even multiple of that block size. 2988 */ 2989 if ((un->un_bsize != 0) && (len % un->un_bsize != 0)) { 2990 scsi_log(ST_DEVINFO, st_label, CE_WARN, 2991 "%s: not modulo %d block size\n", 2992 (flag == B_WRITE) ? wr_str : rd_str, un->un_bsize); 2993 rval = EINVAL; 2994 } 2995 2996 /* If device has set granularity in the READ_BLKLIM we honor it. */ 2997 if ((un->un_data_mod != 0) && (len % un->un_data_mod != 0)) { 2998 scsi_log(ST_DEVINFO, st_label, CE_WARN, 2999 "%s: not modulo %d device granularity\n", 3000 (flag == B_WRITE) ? wr_str : rd_str, un->un_data_mod); 3001 rval = EINVAL; 3002 } 3003 3004 if (rval != 0) { 3005 un->un_errno = rval; 3006 mutex_exit(ST_MUTEX); 3007 return (rval); 3008 } 3009 3010 un->un_silent_skip = 0; 3011 mutex_exit(ST_MUTEX); 3012 3013 len = uio->uio_resid; 3014 3015 rval = physio(st_strategy, (struct buf *)NULL, 3016 dev, flag, st_minphys, uio); 3017 /* 3018 * if we have hit logical EOT during this xfer and there is not a 3019 * full residue, then set un_eof back to ST_EOM to make sure that 3020 * the user will see at least one zero write 3021 * after this short write 3022 */ 3023 mutex_enter(ST_MUTEX); 3024 if (un->un_eof > ST_NO_EOF) { 3025 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 3026 "un_eof=%d resid=%lx\n", un->un_eof, uio->uio_resid); 3027 } 3028 if (un->un_eof >= ST_EOM && (flag == B_WRITE)) { 3029 if ((uio->uio_resid != len) && (uio->uio_resid != 0)) { 3030 un->un_eof = ST_EOM; 3031 } else if (uio->uio_resid == len) { 3032 un->un_eof = ST_NO_EOF; 3033 } 3034 } 3035 3036 if (un->un_silent_skip && uio->uio_resid != len) { 3037 un->un_eof = ST_EOF; 3038 un->un_blkno = un->un_save_blkno; 3039 un->un_fileno--; 3040 } 3041 3042 un->un_errno = rval; 3043 3044 mutex_exit(ST_MUTEX); 3045 3046 return (rval); 3047 } 3048 3049 static int 3050 st_arw(dev_t dev, struct aio_req *aio, int flag) 3051 { 3052 struct uio *uio = aio->aio_uio; 3053 int rval = 0; 3054 long len; 3055 3056 GET_SOFT_STATE(dev); 3057 3058 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 3059 "st_rw(dev = 0x%lx, flag = %s)\n", dev, 3060 (flag == B_READ ? rd_str: wr_str)); 3061 3062 /* get local copy of transfer length */ 3063 len = uio->uio_iov->iov_len; 3064 3065 mutex_enter(ST_MUTEX); 3066 3067 /* 3068 * If in fixed block size mode and requested read or write 3069 * is not an even multiple of that block size. 3070 */ 3071 if ((un->un_bsize != 0) && (len % un->un_bsize != 0)) { 3072 scsi_log(ST_DEVINFO, st_label, CE_WARN, 3073 "%s: not modulo %d block size\n", 3074 (flag == B_WRITE) ? wr_str : rd_str, un->un_bsize); 3075 rval = EINVAL; 3076 } 3077 3078 /* If device has set granularity in the READ_BLKLIM we honor it. */ 3079 if ((un->un_data_mod != 0) && (len % un->un_data_mod != 0)) { 3080 scsi_log(ST_DEVINFO, st_label, CE_WARN, 3081 "%s: not modulo %d device granularity\n", 3082 (flag == B_WRITE) ? wr_str : rd_str, un->un_data_mod); 3083 rval = EINVAL; 3084 } 3085 3086 if (rval != 0) { 3087 un->un_errno = rval; 3088 mutex_exit(ST_MUTEX); 3089 return (rval); 3090 } 3091 3092 mutex_exit(ST_MUTEX); 3093 3094 len = uio->uio_resid; 3095 3096 rval = aphysio(st_strategy, anocancel, dev, flag, st_minphys, aio); 3097 3098 /* 3099 * if we have hit logical EOT during this xfer and there is not a 3100 * full residue, then set un_eof back to ST_EOM to make sure that 3101 * the user will see at least one zero write 3102 * after this short write 3103 * 3104 * we keep this here just in case the application is not using 3105 * persistent errors 3106 */ 3107 mutex_enter(ST_MUTEX); 3108 if (un->un_eof > ST_NO_EOF) { 3109 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 3110 "un_eof=%d resid=%lx\n", un->un_eof, uio->uio_resid); 3111 } 3112 if (un->un_eof >= ST_EOM && (flag == B_WRITE)) { 3113 if ((uio->uio_resid != len) && (uio->uio_resid != 0)) { 3114 un->un_eof = ST_EOM; 3115 } else if (uio->uio_resid == len && !IS_PE_FLAG_SET(un)) { 3116 un->un_eof = ST_NO_EOF; 3117 } 3118 } 3119 un->un_errno = rval; 3120 mutex_exit(ST_MUTEX); 3121 3122 return (rval); 3123 } 3124 3125 3126 3127 static int 3128 st_strategy(struct buf *bp) 3129 { 3130 struct scsi_tape *un; 3131 dev_t dev = bp->b_edev; 3132 3133 /* 3134 * validate arguments 3135 */ 3136 if ((un = ddi_get_soft_state(st_state, MTUNIT(bp->b_edev))) == NULL) { 3137 bp->b_resid = bp->b_bcount; 3138 mutex_enter(ST_MUTEX); 3139 st_bioerror(bp, ENXIO); 3140 mutex_exit(ST_MUTEX); 3141 goto error; 3142 } 3143 3144 mutex_enter(ST_MUTEX); 3145 3146 while (un->un_pwr_mgmt == ST_PWR_SUSPENDED) { 3147 cv_wait(&un->un_suspend_cv, ST_MUTEX); 3148 } 3149 3150 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 3151 "st_strategy(): bcount=0x%lx, fileno=%d, blkno=%lx, eof=%d\n", 3152 bp->b_bcount, un->un_fileno, un->un_blkno, un->un_eof); 3153 3154 /* 3155 * If persistent errors have been flagged, just nix this one. We wait 3156 * for any outstanding I/O's below, so we will be in order. 3157 */ 3158 if (IS_PE_FLAG_SET(un)) 3159 goto exit; 3160 3161 if (bp != un->un_sbufp) { 3162 char reading = bp->b_flags & B_READ; 3163 int wasopening = 0; 3164 3165 /* 3166 * If we haven't done/checked reservation on the tape unit 3167 * do it now. 3168 */ 3169 if ((un->un_rsvd_status & 3170 (ST_RESERVE | ST_APPLICATION_RESERVATIONS)) == 0) { 3171 if ((un->un_dp->options & ST_NO_RESERVE_RELEASE) == 0) { 3172 if (st_reserve_release(un, ST_RESERVE)) { 3173 st_bioerror(bp, un->un_errno); 3174 goto exit; 3175 } 3176 } else if (un->un_state == ST_STATE_OPEN_PENDING_IO) { 3177 /* 3178 * Enter here to restore position for possible 3179 * resets when the device was closed and opened 3180 * in O_NDELAY mode subsequently 3181 */ 3182 un->un_state = ST_STATE_INITIALIZING; 3183 (void) st_cmd(dev, SCMD_TEST_UNIT_READY, 3184 0, SYNC_CMD); 3185 un->un_state = ST_STATE_OPEN_PENDING_IO; 3186 } 3187 un->un_rsvd_status |= ST_INIT_RESERVE; 3188 } 3189 3190 /* 3191 * If we are offline, we have to initialize everything first. 3192 * This is to handle either when opened with O_NDELAY, or 3193 * we just got a new tape in the drive, after an offline. 3194 * We don't observe O_NDELAY past the open, 3195 * as it will not make sense for tapes. 3196 */ 3197 if (un->un_state == ST_STATE_OFFLINE || un->un_restore_pos) { 3198 /* reset state to avoid recursion */ 3199 un->un_state = ST_STATE_INITIALIZING; 3200 if (st_tape_init(dev)) { 3201 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 3202 "stioctl : OFFLINE init failure "); 3203 un->un_state = ST_STATE_OFFLINE; 3204 un->un_fileno = -1; 3205 goto b_done_err; 3206 } 3207 un->un_state = ST_STATE_OPEN_PENDING_IO; 3208 } 3209 /* 3210 * Check for legal operations 3211 */ 3212 if (un->un_fileno < 0) { 3213 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 3214 "strategy with un->un_fileno < 0\n"); 3215 goto b_done_err; 3216 } 3217 3218 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 3219 "st_strategy(): regular io\n"); 3220 3221 /* 3222 * Process this first. If we were reading, and we're pending 3223 * logical eot, that means we've bumped one file mark too far. 3224 */ 3225 3226 /* 3227 * Recursion warning: st_cmd will route back through here. 3228 */ 3229 if (un->un_eof == ST_EOT_PENDING) { 3230 if (st_cmd(dev, SCMD_SPACE, Fmk((-1)), SYNC_CMD)) { 3231 un->un_fileno = -1; 3232 un->un_density_known = 0; 3233 goto b_done_err; 3234 } 3235 un->un_blkno = 0; /* fix up block number.. */ 3236 un->un_eof = ST_EOT; 3237 } 3238 3239 /* 3240 * If we are in the process of opening, we may have to 3241 * determine/set the correct density. We also may have 3242 * to do a test_append (if QIC) to see whether we are 3243 * in a position to append to the end of the tape. 3244 * 3245 * If we're already at logical eot, we transition 3246 * to ST_NO_EOF. If we're at physical eot, we punt 3247 * to the switch statement below to handle. 3248 */ 3249 if ((un->un_state == ST_STATE_OPEN_PENDING_IO) || 3250 (un->un_test_append && (un->un_dp->options & ST_QIC))) { 3251 3252 if (un->un_state == ST_STATE_OPEN_PENDING_IO) { 3253 if (st_determine_density(dev, (int)reading)) { 3254 goto b_done_err; 3255 } 3256 } 3257 3258 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 3259 "pending_io@fileno %d rw %d qic %d eof %d\n", 3260 un->un_fileno, (int)reading, 3261 (un->un_dp->options & ST_QIC) ? 1 : 0, 3262 un->un_eof); 3263 3264 if (!reading && un->un_eof != ST_EOM) { 3265 if (un->un_eof == ST_EOT) { 3266 un->un_eof = ST_NO_EOF; 3267 } else if (un->un_fileno > 0 && 3268 (un->un_dp->options & ST_QIC)) { 3269 /* 3270 * st_test_append() will do it all 3271 */ 3272 st_test_append(bp); 3273 goto done; 3274 } 3275 } 3276 if (un->un_state == ST_STATE_OPEN_PENDING_IO) { 3277 wasopening = 1; 3278 } 3279 un->un_laststate = un->un_state; 3280 un->un_state = ST_STATE_OPEN; 3281 } 3282 3283 3284 /* 3285 * Process rest of END OF FILE and END OF TAPE conditions 3286 */ 3287 3288 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 3289 "un_eof=%x, wasopening=%x\n", 3290 un->un_eof, wasopening); 3291 3292 switch (un->un_eof) { 3293 case ST_EOM: 3294 /* 3295 * This allows writes to proceed past physical 3296 * eot. We'll *really* be in trouble if the 3297 * user continues blindly writing data too 3298 * much past this point (unwind the tape). 3299 * Physical eot really means 'early warning 3300 * eot' in this context. 3301 * 3302 * Every other write from now on will succeed 3303 * (if sufficient tape left). 3304 * This write will return with resid == count 3305 * but the next one should be successful 3306 * 3307 * Note that we only transition to logical EOT 3308 * if the last state wasn't the OPENING state. 3309 * We explicitly prohibit running up to physical 3310 * eot, closing the device, and then re-opening 3311 * to proceed. Trailer records may only be gotten 3312 * at by keeping the tape open after hitting eot. 3313 * 3314 * Also note that ST_EOM cannot be set by reading- 3315 * this can only be set during writing. Reading 3316 * up to the end of the tape gets a blank check 3317 * or a double-filemark indication (ST_EOT_PENDING), 3318 * and we prohibit reading after that point. 3319 * 3320 */ 3321 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "EOM\n"); 3322 if (wasopening == 0) { 3323 /* 3324 * this allows st_rw() to reset it back to 3325 * ST_EOM to make sure that the application 3326 * will see a zero write 3327 */ 3328 un->un_eof = ST_WRITE_AFTER_EOM; 3329 } 3330 un->un_status = SUN_KEY_EOT; 3331 goto b_done; 3332 3333 case ST_WRITE_AFTER_EOM: 3334 case ST_EOT: 3335 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "EOT\n"); 3336 un->un_status = SUN_KEY_EOT; 3337 if (SVR4_BEHAVIOR && reading) { 3338 goto b_done_err; 3339 } 3340 3341 if (reading) { 3342 goto b_done; 3343 } 3344 un->un_eof = ST_NO_EOF; 3345 break; 3346 3347 case ST_EOF_PENDING: 3348 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 3349 "EOF PENDING\n"); 3350 un->un_status = SUN_KEY_EOF; 3351 if (SVR4_BEHAVIOR) { 3352 un->un_eof = ST_EOF; 3353 goto b_done; 3354 } 3355 /* FALLTHROUGH */ 3356 case ST_EOF: 3357 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "EOF\n"); 3358 un->un_status = SUN_KEY_EOF; 3359 if (SVR4_BEHAVIOR) { 3360 goto b_done_err; 3361 } 3362 3363 if (BSD_BEHAVIOR) { 3364 un->un_eof = ST_NO_EOF; 3365 un->un_fileno += 1; 3366 un->un_blkno = 0; 3367 } 3368 3369 if (reading) { 3370 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 3371 "now file %d (read)\n", 3372 un->un_fileno); 3373 goto b_done; 3374 } 3375 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 3376 "now file %d (write)\n", un->un_fileno); 3377 break; 3378 default: 3379 un->un_status = 0; 3380 break; 3381 } 3382 } 3383 3384 bp->b_flags &= ~(B_DONE); 3385 st_bioerror(bp, 0); 3386 bp->av_forw = NULL; 3387 bp->b_resid = 0; 3388 SET_BP_PKT(bp, 0); 3389 3390 3391 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 3392 "st_strategy: cmd=0x%p count=%ld resid=%ld flags=0x%x" 3393 " pkt=0x%p\n", 3394 (void *)bp->b_forw, bp->b_bcount, 3395 bp->b_resid, bp->b_flags, (void *)BP_PKT(bp)); 3396 3397 #if defined(__i386) || defined(__amd64) 3398 /* 3399 * We will replace bp with a new bp that can do big blk xfer 3400 * if the requested xfer size is bigger than ST_BIGBLK_XFER 3401 * 3402 * Also, we need to make sure that we're handling real I/O 3403 * by checking group 0/1 SCSI I/O commands, if needed 3404 */ 3405 if (bp->b_bcount > ST_BIGBLK_XFER && 3406 (bp != un->un_sbufp || 3407 (uchar_t)(uintptr_t)bp->b_forw == SCMD_READ || 3408 (uchar_t)(uintptr_t)bp->b_forw == SCMD_READ_G1 || 3409 (uchar_t)(uintptr_t)bp->b_forw == SCMD_WRITE || 3410 (uchar_t)(uintptr_t)bp->b_forw == SCMD_WRITE_G1)) { 3411 mutex_exit(ST_MUTEX); 3412 bp = st_get_bigblk_bp(bp); 3413 mutex_enter(ST_MUTEX); 3414 } 3415 #endif 3416 3417 /* put on wait queue */ 3418 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 3419 "st_strategy: un->un_quef = 0x%p, bp = 0x%p\n", 3420 (void *)un->un_quef, (void *)bp); 3421 3422 if (un->un_quef) { 3423 un->un_quel->b_actf = bp; 3424 } else { 3425 un->un_quef = bp; 3426 } 3427 un->un_quel = bp; 3428 3429 ST_DO_KSTATS(bp, kstat_waitq_enter); 3430 3431 st_start(un); 3432 3433 done: 3434 mutex_exit(ST_MUTEX); 3435 return (0); 3436 3437 3438 error: 3439 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 3440 "st_strategy: error exit\n"); 3441 3442 biodone(bp); 3443 return (0); 3444 3445 b_done_err: 3446 st_bioerror(bp, EIO); 3447 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 3448 "st_strategy : EIO b_done_err\n"); 3449 3450 b_done: 3451 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 3452 "st_strategy: b_done\n"); 3453 3454 exit: 3455 /* 3456 * make sure no commands are outstanding or waiting before closing, 3457 * so we can guarantee order 3458 */ 3459 st_wait_for_io(un); 3460 un->un_err_resid = bp->b_resid = bp->b_bcount; 3461 3462 /* override errno here, if persistent errors were flagged */ 3463 if (IS_PE_FLAG_SET(un)) 3464 bioerror(bp, un->un_errno); 3465 3466 mutex_exit(ST_MUTEX); 3467 3468 biodone(bp); 3469 ASSERT(mutex_owned(ST_MUTEX) == 0); 3470 return (0); 3471 } 3472 3473 3474 3475 /* 3476 * this routine spaces forward over filemarks 3477 */ 3478 static int 3479 st_space_fmks(dev_t dev, int count) 3480 { 3481 int rval = 0; 3482 3483 GET_SOFT_STATE(dev); 3484 3485 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 3486 "st_space_fmks(dev = 0x%lx, count = %d)\n", dev, count); 3487 3488 ASSERT(mutex_owned(ST_MUTEX)); 3489 3490 /* 3491 * the risk with doing only one space operation is that we 3492 * may accidentily jump in old data 3493 * the exabyte 8500 reading 8200 tapes cannot use KNOWS_EOD 3494 * because the 8200 does not append a marker; in order not to 3495 * sacrifice the fast file skip, we do a slow skip if the low 3496 * density device has been opened 3497 */ 3498 3499 if ((un->un_dp->options & ST_KNOWS_EOD) && 3500 !((un->un_dp->type == ST_TYPE_EXB8500 && MT_DENSITY(dev) == 0))) { 3501 if (st_cmd(dev, SCMD_SPACE, Fmk(count), SYNC_CMD)) { 3502 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 3503 "space_fmks : EIO can't do space cmd #1\n"); 3504 rval = EIO; 3505 } 3506 } else { 3507 while (count > 0) { 3508 if (st_cmd(dev, SCMD_SPACE, Fmk(1), SYNC_CMD)) { 3509 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 3510 "space_fmks : EIO can't do space cmd #2\n"); 3511 rval = EIO; 3512 break; 3513 } 3514 count -= 1; 3515 /* 3516 * read a block to see if we have reached 3517 * end of medium (double filemark for reel or 3518 * medium error for others) 3519 */ 3520 if (count > 0) { 3521 if (st_cmd(dev, SCMD_SPACE, Blk(1), 3522 SYNC_CMD)) { 3523 ST_DEBUG2(ST_DEVINFO, st_label, 3524 SCSI_DEBUG, 3525 "space_fmks : EIO can't do " 3526 "space cmd #3\n"); 3527 rval = EIO; 3528 break; 3529 } 3530 if ((un->un_eof >= ST_EOF_PENDING) && 3531 (un->un_dp->options & ST_REEL)) { 3532 un->un_status = SUN_KEY_EOT; 3533 ST_DEBUG2(ST_DEVINFO, st_label, 3534 SCSI_DEBUG, 3535 "space_fmks : EIO ST_REEL\n"); 3536 rval = EIO; 3537 break; 3538 } else if (IN_EOF(un)) { 3539 un->un_eof = ST_NO_EOF; 3540 un->un_fileno++; 3541 un->un_blkno = 0; 3542 count--; 3543 } else if (un->un_eof > ST_EOF) { 3544 ST_DEBUG2(ST_DEVINFO, st_label, 3545 SCSI_DEBUG, 3546 "space_fmks, EIO > ST_EOF\n"); 3547 rval = EIO; 3548 break; 3549 } 3550 3551 } 3552 } 3553 un->un_err_resid = count; 3554 un->un_err_fileno = un->un_fileno; 3555 un->un_err_blkno = un->un_blkno; 3556 } 3557 ASSERT(mutex_owned(ST_MUTEX)); 3558 return (rval); 3559 } 3560 3561 /* 3562 * this routine spaces to EOM 3563 * 3564 * it keeps track of the current filenumber and returns the filenumber after 3565 * the last successful space operation, we keep the number high because as 3566 * tapes are getting larger, the possibility of more and more files exist, 3567 * 0x100000 (1 Meg of files) probably will never have to be changed any time 3568 * soon 3569 */ 3570 #define MAX_SKIP 0x100000 /* somewhat arbitrary */ 3571 3572 static int 3573 st_find_eom(dev_t dev) 3574 { 3575 int count, savefile; 3576 struct scsi_tape *un; 3577 int instance; 3578 3579 instance = MTUNIT(dev); 3580 if ((un = ddi_get_soft_state(st_state, instance)) == NULL) 3581 return (-1); 3582 3583 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 3584 "st_find_eom(dev = 0x%lx): fileno = %d\n", dev, un->un_fileno); 3585 3586 ASSERT(mutex_owned(ST_MUTEX)); 3587 3588 savefile = un->un_fileno; 3589 3590 /* 3591 * see if the drive is smart enough to do the skips in 3592 * one operation; 1/2" use two filemarks 3593 * the exabyte 8500 reading 8200 tapes cannot use KNOWS_EOD 3594 * because the 8200 does not append a marker; in order not to 3595 * sacrifice the fast file skip, we do a slow skip if the low 3596 * density device has been opened 3597 */ 3598 if ((un->un_dp->options & ST_KNOWS_EOD) && 3599 !((un->un_dp->type == ST_TYPE_EXB8500 && MT_DENSITY(dev) == 0))) { 3600 count = MAX_SKIP; 3601 } else { 3602 count = 1; 3603 } 3604 3605 while (st_cmd(dev, SCMD_SPACE, Fmk(count), SYNC_CMD) == 0) { 3606 3607 savefile = un->un_fileno; 3608 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 3609 "count=%x, eof=%x, status=%x\n", count, un->un_eof, 3610 un->un_status); 3611 3612 /* 3613 * If we're not EOM smart, space a record 3614 * to see whether we're now in the slot between 3615 * the two sequential filemarks that logical 3616 * EOM consists of (REEL) or hit nowhere land 3617 * (8mm). 3618 */ 3619 if (count == 1) { 3620 /* 3621 * no fast skipping, check a record 3622 */ 3623 if (st_cmd(dev, SCMD_SPACE, Blk((1)), SYNC_CMD)) 3624 break; 3625 else if ((un->un_eof >= ST_EOF_PENDING) && 3626 (un->un_dp->options & ST_REEL)) { 3627 un->un_status = KEY_BLANK_CHECK; 3628 un->un_fileno++; 3629 un->un_blkno = 0; 3630 break; 3631 } else if (IN_EOF(un)) { 3632 un->un_eof = ST_NO_EOF; 3633 un->un_fileno++; 3634 un->un_blkno = 0; 3635 } else if (un->un_eof > ST_EOF) { 3636 break; 3637 } 3638 } else { 3639 if (un->un_eof > ST_EOF) { 3640 break; 3641 } 3642 } 3643 } 3644 3645 if (un->un_dp->options & ST_KNOWS_EOD) { 3646 savefile = un->un_fileno; 3647 } 3648 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 3649 "st_find_eom: %x\n", savefile); 3650 ASSERT(mutex_owned(ST_MUTEX)); 3651 return (savefile); 3652 } 3653 3654 3655 /* 3656 * this routine is frequently used in ioctls below; 3657 * it determines whether we know the density and if not will 3658 * determine it 3659 * if we have written the tape before, one or more filemarks are written 3660 * 3661 * depending on the stepflag, the head is repositioned to where it was before 3662 * the filemarks were written in order not to confuse step counts 3663 */ 3664 #define STEPBACK 0 3665 #define NO_STEPBACK 1 3666 3667 static int 3668 st_check_density_or_wfm(dev_t dev, int wfm, int mode, int stepflag) 3669 { 3670 3671 GET_SOFT_STATE(dev); 3672 3673 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 3674 "st_check_density_or_wfm(dev= 0x%lx, wfm= %d, mode= %d, stpflg= %d)\n", 3675 dev, wfm, mode, stepflag); 3676 3677 ASSERT(mutex_owned(ST_MUTEX)); 3678 3679 /* 3680 * If we don't yet know the density of the tape we have inserted, 3681 * we have to either unconditionally set it (if we're 'writing'), 3682 * or we have to determine it. As side effects, check for any 3683 * write-protect errors, and for the need to put out any file-marks 3684 * before positioning a tape. 3685 * 3686 * If we are going to be spacing forward, and we haven't determined 3687 * the tape density yet, we have to do so now... 3688 */ 3689 if (un->un_state == ST_STATE_OPEN_PENDING_IO) { 3690 if (st_determine_density(dev, mode)) { 3691 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 3692 "check_density_or_wfm : EIO can't determine " 3693 "density\n"); 3694 un->un_errno = EIO; 3695 return (EIO); 3696 } 3697 /* 3698 * Presumably we are at BOT. If we attempt to write, it will 3699 * either work okay, or bomb. We don't do a st_test_append 3700 * unless we're past BOT. 3701 */ 3702 un->un_laststate = un->un_state; 3703 un->un_state = ST_STATE_OPEN; 3704 3705 } else if (un->un_fileno >= 0 && un->un_fmneeded > 0 && 3706 ((un->un_lastop == ST_OP_WEOF && wfm) || 3707 (un->un_lastop == ST_OP_WRITE && wfm))) { 3708 3709 daddr_t blkno = un->un_blkno; 3710 int fileno = un->un_fileno; 3711 3712 /* 3713 * We need to write one or two filemarks. 3714 * In the case of the HP, we need to 3715 * position the head between the two 3716 * marks. 3717 */ 3718 if ((un->un_fmneeded > 0) || (un->un_lastop == ST_OP_WEOF)) { 3719 wfm = un->un_fmneeded; 3720 un->un_fmneeded = 0; 3721 } 3722 3723 if (st_write_fm(dev, wfm)) { 3724 un->un_fileno = -1; 3725 un->un_density_known = 0; 3726 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 3727 "check_density_or_wfm : EIO can't write fm\n"); 3728 un->un_errno = EIO; 3729 return (EIO); 3730 } 3731 3732 if (stepflag == STEPBACK) { 3733 if (st_cmd(dev, SCMD_SPACE, Fmk((-wfm)), SYNC_CMD)) { 3734 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 3735 "check_density_or_wfm : EIO can't space " 3736 "(-wfm)\n"); 3737 un->un_errno = EIO; 3738 return (EIO); 3739 } 3740 un->un_blkno = blkno; 3741 un->un_fileno = fileno; 3742 } 3743 } 3744 3745 /* 3746 * Whatever we do at this point clears the state of the eof flag. 3747 */ 3748 3749 un->un_eof = ST_NO_EOF; 3750 3751 /* 3752 * If writing, let's check that we're positioned correctly 3753 * at the end of tape before issuing the next write. 3754 */ 3755 if (un->un_read_only == RDWR) { 3756 un->un_test_append = 1; 3757 } 3758 3759 ASSERT(mutex_owned(ST_MUTEX)); 3760 return (0); 3761 } 3762 3763 3764 /* 3765 * Wait for all outstaning I/O's to complete 3766 * 3767 * we wait on both ncmds and the wait queue for times when we are flushing 3768 * after persistent errors are flagged, which is when ncmds can be 0, and the 3769 * queue can still have I/O's. This way we preserve order of biodone's. 3770 */ 3771 static void 3772 st_wait_for_io(struct scsi_tape *un) 3773 { 3774 ASSERT(mutex_owned(ST_MUTEX)); 3775 while (un->un_ncmds || un->un_quef) { 3776 cv_wait(&un->un_queue_cv, ST_MUTEX); 3777 } 3778 } 3779 3780 /* 3781 * This routine implements the ioctl calls. It is called 3782 * from the device switch at normal priority. 3783 */ 3784 /*ARGSUSED*/ 3785 static int 3786 st_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p, 3787 int *rval_p) 3788 { 3789 int tmp, rval = 0; 3790 3791 GET_SOFT_STATE(dev); 3792 3793 mutex_enter(ST_MUTEX); 3794 3795 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 3796 "st_ioctl(): fileno=%x, blkno=%lx, un_eof=%x, state = %d, " 3797 "pe_flag = %d\n", 3798 un->un_fileno, un->un_blkno, un->un_eof, un->un_state, 3799 IS_PE_FLAG_SET(un)); 3800 3801 /* 3802 * We don't want to block on these, so let them through 3803 * and we don't care about setting driver states here. 3804 */ 3805 if ((cmd == MTIOCGETDRIVETYPE) || 3806 (cmd == MTIOCGUARANTEEDORDER) || 3807 (cmd == MTIOCPERSISTENTSTATUS)) { 3808 goto check_commands; 3809 } 3810 3811 /* 3812 * wait for all outstanding commands to complete, or be dequeued. 3813 * And because ioctl's are synchronous commands, any return value 3814 * after this, will be in order 3815 */ 3816 st_wait_for_io(un); 3817 3818 /* 3819 * allow only a through clear errors and persistent status, and 3820 * status 3821 */ 3822 if (IS_PE_FLAG_SET(un)) { 3823 if ((cmd == MTIOCLRERR) || 3824 (cmd == MTIOCPERSISTENT) || 3825 (cmd == MTIOCGET)) { 3826 goto check_commands; 3827 } else { 3828 rval = un->un_errno; 3829 goto exit; 3830 } 3831 } 3832 3833 un->un_throttle = 1; /* > 1 will never happen here */ 3834 un->un_errno = 0; /* start clean from here */ 3835 3836 /* 3837 * first and foremost, handle any ST_EOT_PENDING cases. 3838 * That is, if a logical eot is pending notice, notice it. 3839 */ 3840 if (un->un_eof == ST_EOT_PENDING) { 3841 int resid = un->un_err_resid; 3842 uchar_t status = un->un_status; 3843 uchar_t lastop = un->un_lastop; 3844 3845 if (st_cmd(dev, SCMD_SPACE, Fmk((-1)), SYNC_CMD)) { 3846 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 3847 "stioctl : EIO can't space fmk(-1)\n"); 3848 rval = EIO; 3849 goto exit; 3850 } 3851 un->un_lastop = lastop; /* restore last operation */ 3852 if (status == SUN_KEY_EOF) { 3853 un->un_status = SUN_KEY_EOT; 3854 } else { 3855 un->un_status = status; 3856 } 3857 un->un_err_resid = resid; 3858 un->un_err_blkno = un->un_blkno = 0; /* fix up block number */ 3859 un->un_eof = ST_EOT; /* now we're at logical eot */ 3860 } 3861 3862 /* 3863 * now, handle the rest of the situations 3864 */ 3865 check_commands: 3866 switch (cmd) { 3867 case MTIOCGET: 3868 { 3869 #ifdef _MULTI_DATAMODEL 3870 /* 3871 * For use when a 32 bit app makes a call into a 3872 * 64 bit ioctl 3873 */ 3874 struct mtget32 mtg_local32; 3875 struct mtget32 *mtget_32 = &mtg_local32; 3876 #endif /* _MULTI_DATAMODEL */ 3877 3878 /* Get tape status */ 3879 struct mtget mtg_local; 3880 struct mtget *mtget = &mtg_local; 3881 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 3882 "st_ioctl: MTIOCGET\n"); 3883 3884 bzero(mtget, sizeof (struct mtget)); 3885 mtget->mt_erreg = un->un_status; 3886 mtget->mt_resid = un->un_err_resid; 3887 mtget->mt_dsreg = un->un_retry_ct; 3888 mtget->mt_fileno = un->un_err_fileno; 3889 mtget->mt_blkno = un->un_err_blkno; 3890 mtget->mt_type = un->un_dp->type; 3891 mtget->mt_flags = MTF_SCSI | MTF_ASF; 3892 if (un->un_dp->options & ST_REEL) { 3893 mtget->mt_flags |= MTF_REEL; 3894 mtget->mt_bf = 20; 3895 } else { /* 1/4" cartridges */ 3896 switch (mtget->mt_type) { 3897 /* Emulex cartridge tape */ 3898 case MT_ISMT02: 3899 mtget->mt_bf = 40; 3900 break; 3901 default: 3902 mtget->mt_bf = 126; 3903 break; 3904 } 3905 } 3906 3907 /* 3908 * If large transfers are allowed and drive options 3909 * has no record size limit set. Calculate blocking 3910 * factor from the lesser of maxbsize and maxdma. 3911 */ 3912 if ((un->un_allow_large_xfer) && 3913 (un->un_dp->options & ST_NO_RECSIZE_LIMIT)) { 3914 mtget->mt_bf = min(un->un_maxbsize, 3915 un->un_maxdma) / SECSIZE; 3916 } 3917 3918 if (un->un_read_only == WORM || 3919 un->un_read_only == RDWORM) { 3920 mtget->mt_flags |= MTF_WORM_MEDIA; 3921 } 3922 3923 rval = st_check_clean_bit(dev); 3924 if (rval == -1) { 3925 rval = EIO; 3926 goto exit; 3927 } else { 3928 mtget->mt_flags |= (ushort_t)rval; 3929 rval = 0; 3930 } 3931 3932 un->un_status = 0; /* Reset status */ 3933 un->un_err_resid = 0; 3934 tmp = sizeof (struct mtget); 3935 3936 #ifdef _MULTI_DATAMODEL 3937 3938 switch (ddi_model_convert_from(flag & FMODELS)) { 3939 case DDI_MODEL_ILP32: 3940 /* 3941 * Convert 64 bit back to 32 bit before doing 3942 * copyout. This is what the ILP32 app expects. 3943 */ 3944 mtget_32->mt_erreg = mtget->mt_erreg; 3945 mtget_32->mt_resid = mtget->mt_resid; 3946 mtget_32->mt_dsreg = mtget->mt_dsreg; 3947 mtget_32->mt_fileno = (daddr32_t)mtget->mt_fileno; 3948 mtget_32->mt_blkno = (daddr32_t)mtget->mt_blkno; 3949 mtget_32->mt_type = mtget->mt_type; 3950 mtget_32->mt_flags = mtget->mt_flags; 3951 mtget_32->mt_bf = mtget->mt_bf; 3952 3953 if (ddi_copyout(mtget_32, (void *)arg, 3954 sizeof (struct mtget32), flag)) { 3955 rval = EFAULT; 3956 } 3957 break; 3958 3959 case DDI_MODEL_NONE: 3960 if (ddi_copyout(mtget, (void *)arg, tmp, flag)) { 3961 rval = EFAULT; 3962 } 3963 break; 3964 } 3965 #else /* ! _MULTI_DATAMODE */ 3966 if (ddi_copyout(mtget, (void *)arg, tmp, flag)) { 3967 rval = EFAULT; 3968 } 3969 #endif /* _MULTI_DATAMODE */ 3970 3971 break; 3972 } 3973 case MTIOCSTATE: 3974 { 3975 /* 3976 * return when media presence matches state 3977 */ 3978 enum mtio_state state; 3979 3980 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 3981 "st_ioctl: MTIOCSTATE\n"); 3982 3983 if (ddi_copyin((void *)arg, &state, sizeof (int), flag)) 3984 rval = EFAULT; 3985 3986 mutex_exit(ST_MUTEX); 3987 3988 rval = st_check_media(dev, state); 3989 3990 mutex_enter(ST_MUTEX); 3991 3992 if (rval != 0) { 3993 break; 3994 } 3995 3996 if (ddi_copyout(&un->un_mediastate, (void *)arg, 3997 sizeof (int), flag)) 3998 rval = EFAULT; 3999 break; 4000 4001 } 4002 4003 case MTIOCGETDRIVETYPE: 4004 { 4005 #ifdef _MULTI_DATAMODEL 4006 /* 4007 * For use when a 32 bit app makes a call into a 4008 * 64 bit ioctl 4009 */ 4010 struct mtdrivetype_request32 mtdtrq32; 4011 #endif /* _MULTI_DATAMODEL */ 4012 4013 /* 4014 * return mtdrivetype 4015 */ 4016 struct mtdrivetype_request mtdtrq; 4017 struct mtdrivetype mtdrtyp; 4018 struct mtdrivetype *mtdt = &mtdrtyp; 4019 struct st_drivetype *stdt = un->un_dp; 4020 4021 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 4022 "st_ioctl: MTIOCGETDRIVETYPE\n"); 4023 4024 #ifdef _MULTI_DATAMODEL 4025 switch (ddi_model_convert_from(flag & FMODELS)) { 4026 case DDI_MODEL_ILP32: 4027 { 4028 if (ddi_copyin((void *)arg, &mtdtrq32, 4029 sizeof (struct mtdrivetype_request32), flag)) { 4030 rval = EFAULT; 4031 break; 4032 } 4033 mtdtrq.size = mtdtrq32.size; 4034 mtdtrq.mtdtp = 4035 (struct mtdrivetype *)(uintptr_t)mtdtrq32.mtdtp; 4036 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 4037 "st_ioctl: size 0x%x\n", mtdtrq.size); 4038 break; 4039 } 4040 case DDI_MODEL_NONE: 4041 if (ddi_copyin((void *)arg, &mtdtrq, 4042 sizeof (struct mtdrivetype_request), flag)) { 4043 rval = EFAULT; 4044 break; 4045 } 4046 break; 4047 } 4048 4049 #else /* ! _MULTI_DATAMODEL */ 4050 if (ddi_copyin((void *)arg, &mtdtrq, 4051 sizeof (struct mtdrivetype_request), flag)) { 4052 rval = EFAULT; 4053 break; 4054 } 4055 #endif /* _MULTI_DATAMODEL */ 4056 4057 /* 4058 * if requested size is < 0 then return 4059 * error. 4060 */ 4061 if (mtdtrq.size < 0) { 4062 rval = EINVAL; 4063 break; 4064 } 4065 bzero(mtdt, sizeof (struct mtdrivetype)); 4066 (void) strncpy(mtdt->name, stdt->name, ST_NAMESIZE); 4067 (void) strncpy(mtdt->vid, stdt->vid, VIDPIDLEN - 1); 4068 mtdt->type = stdt->type; 4069 mtdt->bsize = stdt->bsize; 4070 mtdt->options = stdt->options; 4071 mtdt->max_rretries = stdt->max_rretries; 4072 mtdt->max_wretries = stdt->max_wretries; 4073 for (tmp = 0; tmp < NDENSITIES; tmp++) 4074 mtdt->densities[tmp] = stdt->densities[tmp]; 4075 mtdt->default_density = stdt->default_density; 4076 /* 4077 * Speed hasn't been used since the hayday of reel tape. 4078 * For all drives not setting the option ST_KNOWS_MEDIA 4079 * the speed member renamed to mediatype are zeros. 4080 * Those drives that have ST_KNOWS_MEDIA set use the 4081 * new mediatype member which is used to figure the 4082 * type of media loaded. 4083 * 4084 * So as to not break applications speed in the 4085 * mtdrivetype structure is not renamed. 4086 */ 4087 for (tmp = 0; tmp < NDENSITIES; tmp++) { 4088 mtdt->speeds[tmp] = stdt->mediatype[tmp]; 4089 } 4090 mtdt->non_motion_timeout = stdt->non_motion_timeout; 4091 mtdt->io_timeout = stdt->io_timeout; 4092 mtdt->rewind_timeout = stdt->rewind_timeout; 4093 mtdt->space_timeout = stdt->space_timeout; 4094 mtdt->load_timeout = stdt->load_timeout; 4095 mtdt->unload_timeout = stdt->unload_timeout; 4096 mtdt->erase_timeout = stdt->erase_timeout; 4097 4098 /* 4099 * Limit the maximum length of the result to 4100 * sizeof (struct mtdrivetype). 4101 */ 4102 tmp = sizeof (struct mtdrivetype); 4103 if (mtdtrq.size < tmp) 4104 tmp = mtdtrq.size; 4105 if (ddi_copyout(mtdt, mtdtrq.mtdtp, tmp, flag)) { 4106 rval = EFAULT; 4107 } 4108 break; 4109 } 4110 case MTIOCPERSISTENT: 4111 { 4112 int persistence = 0; 4113 4114 if (ddi_copyin((void *)arg, &persistence, 4115 sizeof (int), flag)) { 4116 rval = EFAULT; 4117 break; 4118 } 4119 4120 /* non zero sets it, only 0 turns it off */ 4121 un->un_persistence = (uchar_t)persistence ? 1 : 0; 4122 4123 if (un->un_persistence) 4124 TURN_PE_ON(un); 4125 else 4126 TURN_PE_OFF(un); 4127 4128 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 4129 "st_ioctl: MTIOCPERSISTENT : persistence = %d\n", 4130 un->un_persistence); 4131 4132 break; 4133 } 4134 case MTIOCPERSISTENTSTATUS: 4135 { 4136 int persistence = (int)un->un_persistence; 4137 4138 if (ddi_copyout(&persistence, (void *)arg, 4139 sizeof (int), flag)) { 4140 rval = EFAULT; 4141 } 4142 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 4143 "st_ioctl: MTIOCPERSISTENTSTATUS:persistece = %d\n", 4144 un->un_persistence); 4145 4146 break; 4147 } 4148 4149 4150 case MTIOCLRERR: 4151 { 4152 /* clear persistent errors */ 4153 4154 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 4155 "st_ioctl: MTIOCLRERR\n"); 4156 4157 CLEAR_PE(un); 4158 4159 break; 4160 } 4161 4162 case MTIOCGUARANTEEDORDER: 4163 { 4164 /* 4165 * this is just a holder to make a valid ioctl and 4166 * it won't be in any earlier release 4167 */ 4168 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 4169 "st_ioctl: MTIOCGUARANTEEDORDER\n"); 4170 4171 break; 4172 } 4173 4174 case MTIOCRESERVE: 4175 { 4176 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 4177 "st_ioctl: MTIOCRESERVE\n"); 4178 4179 /* 4180 * Check if Reserve/Release is supported. 4181 */ 4182 if (un->un_dp->options & ST_NO_RESERVE_RELEASE) { 4183 rval = ENOTTY; 4184 break; 4185 } 4186 4187 rval = st_reserve_release(un, ST_RESERVE); 4188 4189 if (rval == 0) { 4190 un->un_rsvd_status |= ST_PRESERVE_RESERVE; 4191 } 4192 break; 4193 } 4194 4195 case MTIOCRELEASE: 4196 { 4197 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 4198 "st_ioctl: MTIOCRELEASE\n"); 4199 4200 /* 4201 * Check if Reserve/Release is supported. 4202 */ 4203 if (un->un_dp->options & ST_NO_RESERVE_RELEASE) { 4204 rval = ENOTTY; 4205 break; 4206 } 4207 4208 /* 4209 * Used to just clear ST_PRESERVE_RESERVE which 4210 * made the reservation release at next close. 4211 * As the user may have opened and then done a 4212 * persistant reservation we now need to drop 4213 * the reservation without closing if the user 4214 * attempts to do this. 4215 */ 4216 rval = st_reserve_release(un, ST_RELEASE); 4217 4218 un->un_rsvd_status &= ~ST_PRESERVE_RESERVE; 4219 4220 break; 4221 } 4222 4223 case MTIOCFORCERESERVE: 4224 { 4225 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 4226 "st_ioctl: MTIOCFORCERESERVE\n"); 4227 4228 /* 4229 * Check if Reserve/Release is supported. 4230 */ 4231 if (un->un_dp->options & ST_NO_RESERVE_RELEASE) { 4232 rval = ENOTTY; 4233 break; 4234 } 4235 /* 4236 * allow only super user to run this. 4237 */ 4238 if (drv_priv(cred_p) != 0) { 4239 rval = EPERM; 4240 break; 4241 } 4242 /* 4243 * Throw away reserve, 4244 * not using test-unit-ready 4245 * since reserve can succeed without tape being 4246 * present in the drive. 4247 */ 4248 (void) st_reserve_release(un, ST_RESERVE); 4249 4250 rval = st_take_ownership(dev); 4251 4252 break; 4253 } 4254 4255 case USCSICMD: 4256 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 4257 "st_ioctl: USCSICMD\n"); 4258 { 4259 cred_t *cr; 4260 cr = ddi_get_cred(); 4261 if ((drv_priv(cred_p) != 0) && (drv_priv(cr) != 0)) { 4262 rval = EPERM; 4263 } else { 4264 rval = st_ioctl_cmd(dev, (struct uscsi_cmd *)arg, 4265 flag); 4266 } 4267 } 4268 break; 4269 4270 case MTIOCTOP: 4271 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 4272 "st_ioctl: MTIOCTOP\n"); 4273 rval = st_mtioctop(un, arg, flag); 4274 break; 4275 4276 case MTIOCREADIGNOREILI: 4277 { 4278 int set_ili; 4279 4280 if (ddi_copyin((void *)arg, &set_ili, 4281 sizeof (set_ili), flag)) { 4282 rval = EFAULT; 4283 break; 4284 } 4285 4286 if (un->un_bsize) { 4287 rval = ENOTTY; 4288 break; 4289 } 4290 4291 switch (set_ili) { 4292 case 0: 4293 un->un_dp->options &= ~ST_READ_IGNORE_ILI; 4294 break; 4295 4296 case 1: 4297 un->un_dp->options |= ST_READ_IGNORE_ILI; 4298 break; 4299 4300 default: 4301 rval = EINVAL; 4302 break; 4303 } 4304 break; 4305 } 4306 4307 case MTIOCREADIGNOREEOFS: 4308 { 4309 int ignore_eof; 4310 4311 if (ddi_copyin((void *)arg, &ignore_eof, 4312 sizeof (ignore_eof), flag)) { 4313 rval = EFAULT; 4314 break; 4315 } 4316 4317 if (!(un->un_dp->options & ST_REEL)) { 4318 rval = ENOTTY; 4319 break; 4320 } 4321 4322 switch (ignore_eof) { 4323 case 0: 4324 un->un_dp->options &= ~ST_READ_IGNORE_EOFS; 4325 break; 4326 4327 case 1: 4328 un->un_dp->options |= ST_READ_IGNORE_EOFS; 4329 break; 4330 4331 default: 4332 rval = EINVAL; 4333 break; 4334 } 4335 break; 4336 } 4337 4338 case MTIOCSHORTFMK: 4339 { 4340 int short_fmk; 4341 4342 if (ddi_copyin((void *)arg, &short_fmk, 4343 sizeof (short_fmk), flag)) { 4344 rval = EFAULT; 4345 break; 4346 } 4347 4348 switch (un->un_dp->type) { 4349 case ST_TYPE_EXB8500: 4350 case ST_TYPE_EXABYTE: 4351 if (!short_fmk) { 4352 un->un_dp->options &= 4353 ~ST_SHORT_FILEMARKS; 4354 } else if (short_fmk == 1) { 4355 un->un_dp->options |= 4356 ST_SHORT_FILEMARKS; 4357 } else { 4358 rval = EINVAL; 4359 } 4360 break; 4361 4362 default: 4363 rval = ENOTTY; 4364 break; 4365 } 4366 break; 4367 } 4368 4369 default: 4370 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 4371 "st_ioctl: unknown ioctl\n"); 4372 rval = ENOTTY; 4373 } 4374 4375 exit: 4376 if (!IS_PE_FLAG_SET(un)) 4377 un->un_errno = rval; 4378 4379 mutex_exit(ST_MUTEX); 4380 4381 return (rval); 4382 } 4383 4384 4385 /* 4386 * do some MTIOCTOP tape operations 4387 */ 4388 static int 4389 st_mtioctop(struct scsi_tape *un, intptr_t arg, int flag) 4390 { 4391 struct mtop *mtop, local; 4392 int savefile, tmp, rval = 0; 4393 dev_t dev = un->un_dev; 4394 #ifdef _MULTI_DATAMODEL 4395 /* 4396 * For use when a 32 bit app makes a call into a 4397 * 64 bit ioctl 4398 */ 4399 struct mtop32 mtop_32_for_64; 4400 #endif /* _MULTI_DATAMODEL */ 4401 4402 4403 ASSERT(mutex_owned(ST_MUTEX)); 4404 4405 mtop = &local; 4406 #ifdef _MULTI_DATAMODEL 4407 switch (ddi_model_convert_from(flag & FMODELS)) { 4408 case DDI_MODEL_ILP32: 4409 { 4410 if (ddi_copyin((void *)arg, &mtop_32_for_64, 4411 sizeof (struct mtop32), flag)) { 4412 return (EFAULT); 4413 } 4414 mtop->mt_op = mtop_32_for_64.mt_op; 4415 mtop->mt_count = (daddr_t)mtop_32_for_64.mt_count; 4416 break; 4417 } 4418 case DDI_MODEL_NONE: 4419 if (ddi_copyin((void *)arg, mtop, 4420 sizeof (struct mtop), flag)) { 4421 return (EFAULT); 4422 } 4423 break; 4424 } 4425 4426 #else /* ! _MULTI_DATAMODEL */ 4427 if (ddi_copyin((void *)arg, mtop, sizeof (struct mtop), flag)) { 4428 return (EFAULT); 4429 } 4430 #endif /* _MULTI_DATAMODEL */ 4431 4432 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 4433 "st_mtioctop(): mt_op=%x\n", mtop->mt_op); 4434 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 4435 "fileno=%x, blkno=%lx, un_eof=%x\n", un->un_fileno, un->un_blkno, 4436 un->un_eof); 4437 4438 rval = 0; 4439 un->un_status = 0; 4440 4441 /* 4442 * if we are going to mess with a tape, we have to make sure we have 4443 * one and are not offline (i.e. no tape is initialized). We let 4444 * commands pass here that don't actually touch the tape, except for 4445 * loading and initialization (rewinding). 4446 */ 4447 if (un->un_state == ST_STATE_OFFLINE) { 4448 switch (mtop->mt_op) { 4449 case MTLOAD: 4450 case MTNOP: 4451 /* 4452 * We don't want strategy calling st_tape_init here, 4453 * so, change state 4454 */ 4455 un->un_state = ST_STATE_INITIALIZING; 4456 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4457 "st_mtioctop : OFFLINE state = %d\n", 4458 un->un_state); 4459 break; 4460 default: 4461 /* 4462 * reinitialize by normal means 4463 */ 4464 rval = st_tape_init(dev); 4465 if (rval) { 4466 un->un_state = ST_STATE_INITIALIZING; 4467 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4468 "st_mtioctop : OFFLINE init failure "); 4469 un->un_state = ST_STATE_OFFLINE; 4470 un->un_fileno = -1; 4471 if (rval != EACCES) { 4472 rval = EIO; 4473 } 4474 return (rval); 4475 } 4476 un->un_state = ST_STATE_OPEN_PENDING_IO; 4477 break; 4478 } 4479 } 4480 4481 /* 4482 * If the file position is invalid, allow only those 4483 * commands that properly position the tape and fail 4484 * the rest with EIO 4485 */ 4486 if (un->un_fileno < 0) { 4487 switch (mtop->mt_op) { 4488 case MTWEOF: 4489 case MTRETEN: 4490 case MTERASE: 4491 case MTEOM: 4492 case MTFSF: 4493 case MTFSR: 4494 case MTBSF: 4495 case MTNBSF: 4496 case MTBSR: 4497 case MTSRSZ: 4498 case MTGRSZ: 4499 return (EIO); 4500 /* NOTREACHED */ 4501 case MTREW: 4502 case MTLOAD: 4503 case MTOFFL: 4504 case MTNOP: 4505 break; 4506 4507 default: 4508 return (ENOTTY); 4509 /* NOTREACHED */ 4510 } 4511 } 4512 4513 switch (mtop->mt_op) { 4514 case MTERASE: 4515 /* 4516 * MTERASE rewinds the tape, erase it completely, and returns 4517 * to the beginning of the tape 4518 */ 4519 if (un->un_mspl->wp || un->un_read_only & WORM) { 4520 un->un_status = KEY_WRITE_PROTECT; 4521 un->un_err_resid = mtop->mt_count; 4522 un->un_err_fileno = un->un_fileno; 4523 un->un_err_blkno = un->un_blkno; 4524 return (EACCES); 4525 } 4526 if (un->un_dp->options & ST_REEL) { 4527 un->un_fmneeded = 2; 4528 } else { 4529 un->un_fmneeded = 1; 4530 } 4531 if (st_check_density_or_wfm(dev, 1, B_WRITE, NO_STEPBACK) || 4532 st_cmd(dev, SCMD_REWIND, 0, SYNC_CMD) || 4533 st_cmd(dev, SCMD_ERASE, 0, SYNC_CMD)) { 4534 un->un_fileno = -1; 4535 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4536 "st_mtioctop : EIO space or erase or check den)\n"); 4537 rval = EIO; 4538 } else { 4539 /* QIC and helical scan rewind after erase */ 4540 if (un->un_dp->options & ST_REEL) { 4541 (void) st_cmd(dev, SCMD_REWIND, 0, ASYNC_CMD); 4542 } 4543 } 4544 break; 4545 4546 case MTWEOF: 4547 /* 4548 * write an end-of-file record 4549 */ 4550 if (un->un_mspl->wp || un->un_read_only & RDONLY) { 4551 un->un_status = KEY_WRITE_PROTECT; 4552 un->un_err_resid = mtop->mt_count; 4553 un->un_err_fileno = un->un_fileno; 4554 un->un_err_blkno = un->un_blkno; 4555 return (EACCES); 4556 } 4557 4558 /* 4559 * zero count means just flush buffers 4560 * negative count is not permitted 4561 */ 4562 if (mtop->mt_count < 0) 4563 return (EINVAL); 4564 4565 /* Not on worm */ 4566 if (un->un_read_only == RDWR) { 4567 un->un_test_append = 1; 4568 } 4569 4570 if (un->un_state == ST_STATE_OPEN_PENDING_IO) { 4571 if (st_determine_density(dev, B_WRITE)) { 4572 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4573 "st_mtioctop : EIO : MTWEOF can't determine" 4574 "density"); 4575 return (EIO); 4576 } 4577 } 4578 4579 rval = st_write_fm(dev, (int)mtop->mt_count); 4580 if ((rval != 0) && (rval != EACCES)) { 4581 /* 4582 * Failure due to something other than illegal 4583 * request results in loss of state (st_intr). 4584 */ 4585 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4586 "st_mtioctop : EIO : MTWEOF can't write file mark"); 4587 rval = EIO; 4588 } 4589 break; 4590 4591 case MTRETEN: 4592 /* 4593 * retension the tape 4594 */ 4595 if (st_check_density_or_wfm(dev, 1, 0, NO_STEPBACK) || 4596 st_cmd(dev, SCMD_LOAD, 3, SYNC_CMD)) { 4597 un->un_fileno = -1; 4598 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4599 "st_mtioctop : EIO : MTRETEN "); 4600 rval = EIO; 4601 } 4602 break; 4603 4604 case MTREW: 4605 /* 4606 * rewind the tape 4607 */ 4608 if (st_check_density_or_wfm(dev, 1, 0, NO_STEPBACK)) { 4609 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4610 "st_mtioctop : EIO:MTREW check density/wfm failed"); 4611 return (EIO); 4612 } 4613 if (st_cmd(dev, SCMD_REWIND, 0, SYNC_CMD)) { 4614 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4615 "st_mtioctop : EIO : MTREW "); 4616 rval = EIO; 4617 } 4618 break; 4619 4620 case MTOFFL: 4621 /* 4622 * rewinds, and, if appropriate, takes the device offline by 4623 * unloading the tape 4624 */ 4625 if (st_check_density_or_wfm(dev, 1, 0, NO_STEPBACK)) { 4626 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4627 "st_mtioctop :EIO:MTOFFL check density/wfm failed"); 4628 return (EIO); 4629 } 4630 (void) st_cmd(dev, SCMD_REWIND, 0, SYNC_CMD); 4631 if (st_cmd(dev, SCMD_LOAD, 0, SYNC_CMD)) { 4632 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4633 "st_mtioctop : EIO : MTOFFL"); 4634 return (EIO); 4635 } 4636 un->un_eof = ST_NO_EOF; 4637 un->un_laststate = un->un_state; 4638 un->un_state = ST_STATE_OFFLINE; 4639 un->un_mediastate = MTIO_EJECTED; 4640 break; 4641 4642 case MTLOAD: 4643 /* 4644 * This is to load a tape into the drive 4645 * Note that if the tape is not loaded, the device will have 4646 * to be opened via O_NDELAY or O_NONBLOCK. 4647 */ 4648 /* 4649 * Let's try and clean things up, if we are not 4650 * initializing, and then send in the load command, no 4651 * matter what. 4652 * 4653 * load after a media change by the user. 4654 */ 4655 4656 if (un->un_state > ST_STATE_INITIALIZING) 4657 (void) st_check_density_or_wfm(dev, 1, 0, NO_STEPBACK); 4658 rval = st_cmd(dev, SCMD_LOAD, 1, SYNC_CMD); 4659 if (rval) { 4660 if (rval != EACCES) { 4661 rval = EIO; 4662 } 4663 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4664 "st_mtioctop : %s : MTLOAD\n", 4665 rval == EACCES ? "EACCES" : "EIO"); 4666 /* 4667 * If load tape fails, who knows what happened... 4668 */ 4669 un->un_fileno = -1; 4670 break; 4671 } 4672 4673 /* 4674 * reset all counters appropriately using rewind, as if LOAD 4675 * succeeds, we are at BOT 4676 */ 4677 un->un_state = ST_STATE_INITIALIZING; 4678 4679 rval = st_tape_init(dev); 4680 if ((rval == EACCES) && (un->un_read_only & WORM)) { 4681 rval = 0; 4682 break; 4683 } 4684 4685 if (rval != 0) { 4686 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4687 "st_mtioctop : EIO : MTLOAD calls st_tape_init\n"); 4688 rval = EIO; 4689 un->un_state = ST_STATE_OFFLINE; 4690 } 4691 4692 break; 4693 4694 case MTNOP: 4695 un->un_status = 0; /* Reset status */ 4696 un->un_err_resid = 0; 4697 break; 4698 4699 case MTEOM: 4700 /* 4701 * positions the tape at a location just after the last file 4702 * written on the tape. For cartridge and 8 mm, this after 4703 * the last file mark; for reel, this is inbetween the two 4704 * last 2 file marks 4705 */ 4706 if ((un->un_eof >= ST_EOT) || 4707 (un->un_lastop == ST_OP_WRITE) || 4708 (un->un_lastop == ST_OP_WEOF)) { 4709 /* 4710 * If the command wants to move to logical end 4711 * of media, and we're already there, we're done. 4712 * If we were at logical eot, we reset the state 4713 * to be *not* at logical eot. 4714 * 4715 * If we're at physical or logical eot, we prohibit 4716 * forward space operations (unconditionally). 4717 * 4718 * Also if the last operation was a write of any 4719 * kind the tape is at EOD. 4720 */ 4721 return (0); 4722 } 4723 /* 4724 * physical tape position may not be what we've been 4725 * telling the user; adjust the request accordingly 4726 */ 4727 if (IN_EOF(un)) { 4728 un->un_fileno++; 4729 un->un_blkno = 0; 4730 } 4731 4732 if (st_check_density_or_wfm(dev, 1, B_READ, NO_STEPBACK)) { 4733 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4734 "st_mtioctop : EIO:MTEOM check density/wfm failed"); 4735 return (EIO); 4736 } 4737 4738 /* 4739 * st_find_eom() returns the last fileno we knew about; 4740 */ 4741 savefile = st_find_eom(dev); 4742 4743 if ((un->un_status != KEY_BLANK_CHECK) && 4744 (un->un_status != SUN_KEY_EOT)) { 4745 un->un_fileno = -1; 4746 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4747 "st_mtioctop : EIO : MTEOM status check failed"); 4748 rval = EIO; 4749 } else { 4750 /* 4751 * For 1/2" reel tapes assume logical EOT marked 4752 * by two file marks or we don't care that we may 4753 * be extending the last file on the tape. 4754 */ 4755 if (un->un_dp->options & ST_REEL) { 4756 if (st_cmd(dev, SCMD_SPACE, Fmk((-1)), 4757 SYNC_CMD)) { 4758 un->un_fileno = -1; 4759 ST_DEBUG2(ST_DEVINFO, st_label, 4760 SCSI_DEBUG, 4761 "st_mtioctop : EIO : MTEOM space " 4762 "cmd failed"); 4763 rval = EIO; 4764 break; 4765 } 4766 /* 4767 * Fix up the block number. 4768 */ 4769 un->un_blkno = 0; 4770 un->un_err_blkno = 0; 4771 } 4772 un->un_err_resid = 0; 4773 un->un_fileno = savefile; 4774 un->un_eof = ST_EOT; 4775 } 4776 un->un_status = 0; 4777 break; 4778 4779 case MTFSF: 4780 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 4781 "fsf: count=%lx, eof=%x\n", mtop->mt_count, 4782 un->un_eof); 4783 /* 4784 * forward space over filemark 4785 * 4786 * For ASF we allow a count of 0 on fsf which means 4787 * we just want to go to beginning of current file. 4788 * Equivalent to "nbsf(0)" or "bsf(1) + fsf". 4789 * Allow stepping over double fmk with reel 4790 */ 4791 if ((un->un_eof >= ST_EOT) && (mtop->mt_count > 0) && 4792 ((un->un_dp->options & ST_REEL) == 0)) { 4793 /* we're at EOM */ 4794 un->un_err_resid = mtop->mt_count; 4795 un->un_status = KEY_BLANK_CHECK; 4796 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4797 "st_mtioctop : EIO : MTFSF at EOM"); 4798 return (EIO); 4799 } 4800 4801 /* 4802 * physical tape position may not be what we've been 4803 * telling the user; adjust the request accordingly 4804 */ 4805 if (IN_EOF(un)) { 4806 un->un_fileno++; 4807 un->un_blkno = 0; 4808 /* 4809 * For positive direction case, we're now covered. 4810 * For zero or negative direction, we're covered 4811 * (almost) 4812 */ 4813 mtop->mt_count--; 4814 } 4815 4816 if (st_check_density_or_wfm(dev, 1, B_READ, STEPBACK)) { 4817 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4818 "st_mtioctop : EIO : MTFSF density/wfm failed"); 4819 return (EIO); 4820 } 4821 4822 4823 /* 4824 * Forward space file marks. 4825 * We leave ourselves at block zero 4826 * of the target file number. 4827 */ 4828 if (mtop->mt_count < 0) { 4829 mtop->mt_count = -mtop->mt_count; 4830 mtop->mt_op = MTNBSF; 4831 goto bspace; 4832 } 4833 fspace: 4834 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 4835 "fspace: count=%lx, eof=%x\n", mtop->mt_count, 4836 un->un_eof); 4837 if ((tmp = mtop->mt_count) == 0) { 4838 if (un->un_blkno == 0) { 4839 un->un_err_resid = 0; 4840 un->un_err_fileno = un->un_fileno; 4841 un->un_err_blkno = un->un_blkno; 4842 break; 4843 } else if (un->un_fileno == 0) { 4844 rval = st_cmd(dev, SCMD_REWIND, 0, SYNC_CMD); 4845 } else if (un->un_dp->options & ST_BSF) { 4846 rval = (st_cmd(dev, SCMD_SPACE, Fmk((-1)), 4847 SYNC_CMD) || 4848 st_cmd(dev, SCMD_SPACE, Fmk(1), SYNC_CMD)); 4849 } else { 4850 tmp = un->un_fileno; 4851 rval = (st_cmd(dev, SCMD_REWIND, 0, SYNC_CMD) || 4852 st_cmd(dev, SCMD_SPACE, (int)Fmk(tmp), 4853 SYNC_CMD)); 4854 } 4855 if (rval != 0) { 4856 un->un_fileno = -1; 4857 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4858 "st_mtioctop : EIO : fspace fileno = -1"); 4859 4860 rval = EIO; 4861 } 4862 } else { 4863 rval = st_space_fmks(dev, tmp); 4864 } 4865 4866 if (mtop->mt_op == MTBSF && rval != EIO) { 4867 /* 4868 * we came here with a count < 0; we now need 4869 * to skip back to end up before the filemark 4870 */ 4871 mtop->mt_count = 1; 4872 goto bspace; 4873 } 4874 break; 4875 4876 4877 case MTFSR: 4878 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 4879 "fsr: count=%lx, eof=%x\n", mtop->mt_count, 4880 un->un_eof); 4881 /* 4882 * forward space to inter-record gap 4883 * 4884 */ 4885 if ((un->un_eof >= ST_EOT) && (mtop->mt_count > 0)) { 4886 /* we're at EOM */ 4887 un->un_err_resid = mtop->mt_count; 4888 un->un_status = KEY_BLANK_CHECK; 4889 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4890 "st_mtioctop : EIO : MTFSR un_eof > ST_EOT"); 4891 return (EIO); 4892 } 4893 4894 if (mtop->mt_count == 0) { 4895 un->un_err_fileno = un->un_fileno; 4896 un->un_err_blkno = un->un_blkno; 4897 un->un_err_resid = 0; 4898 if (IN_EOF(un) && SVR4_BEHAVIOR) { 4899 un->un_status = SUN_KEY_EOF; 4900 } 4901 return (0); 4902 } 4903 4904 /* 4905 * physical tape position may not be what we've been 4906 * telling the user; adjust the position accordingly 4907 */ 4908 if (IN_EOF(un)) { 4909 daddr_t blkno = un->un_blkno; 4910 int fileno = un->un_fileno; 4911 uchar_t lastop = un->un_lastop; 4912 if (st_cmd(dev, SCMD_SPACE, Fmk((-1)), SYNC_CMD) 4913 == -1) { 4914 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4915 "st_mtioctop : EIO :MTFSR count && IN_EOF"); 4916 return (EIO); 4917 } 4918 4919 un->un_blkno = blkno; 4920 un->un_fileno = fileno; 4921 un->un_lastop = lastop; 4922 } 4923 4924 if (st_check_density_or_wfm(dev, 1, B_READ, STEPBACK)) { 4925 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4926 "st_mtioctop : EIO : MTFSR st_check_den"); 4927 return (EIO); 4928 } 4929 4930 space_records: 4931 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 4932 "space_records: count=%lx, eof=%x\n", mtop->mt_count, 4933 un->un_eof); 4934 tmp = un->un_blkno + mtop->mt_count; 4935 if (tmp == un->un_blkno) { 4936 un->un_err_resid = 0; 4937 un->un_err_fileno = un->un_fileno; 4938 un->un_err_blkno = un->un_blkno; 4939 break; 4940 } else if (un->un_blkno < tmp || 4941 (un->un_dp->options & ST_BSR)) { 4942 /* 4943 * If we're spacing forward, or the device can 4944 * backspace records, we can just use the SPACE 4945 * command. 4946 */ 4947 tmp = tmp - un->un_blkno; 4948 if (st_cmd(dev, SCMD_SPACE, Blk(tmp), SYNC_CMD)) { 4949 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4950 "st_mtioctop :EIO:space_records can't spc"); 4951 rval = EIO; 4952 } else if (un->un_eof >= ST_EOF_PENDING) { 4953 /* 4954 * check if we hit BOT/EOT 4955 */ 4956 if (tmp < 0 && un->un_eof == ST_EOM) { 4957 un->un_status = SUN_KEY_BOT; 4958 un->un_eof = ST_NO_EOF; 4959 } else if (tmp < 0 && un->un_eof == 4960 ST_EOF_PENDING) { 4961 int residue = un->un_err_resid; 4962 /* 4963 * we skipped over a filemark 4964 * and need to go forward again 4965 */ 4966 if (st_cmd(dev, SCMD_SPACE, Fmk(1), 4967 SYNC_CMD)) { 4968 ST_DEBUG2(ST_DEVINFO, 4969 st_label, SCSI_DEBUG, 4970 "st_mtioctop : EIO : " 4971 "space_records can't " 4972 "space #2"); 4973 rval = EIO; 4974 } 4975 un->un_err_resid = residue; 4976 } 4977 if (rval == 0) { 4978 ST_DEBUG2(ST_DEVINFO, st_label, 4979 SCSI_DEBUG, 4980 "st_mtioctop : EIO : space_rec rval" 4981 " == 0"); 4982 rval = EIO; 4983 } 4984 } 4985 } else { 4986 /* 4987 * else we rewind, space forward across filemarks to 4988 * the desired file, and then space records to the 4989 * desired block. 4990 */ 4991 4992 int t = un->un_fileno; /* save current file */ 4993 4994 if (tmp < 0) { 4995 /* 4996 * Wups - we're backing up over a filemark 4997 */ 4998 if (un->un_blkno != 0 && 4999 (st_cmd(dev, SCMD_REWIND, 0, SYNC_CMD) || 5000 st_cmd(dev, SCMD_SPACE, Fmk(t), SYNC_CMD))) 5001 un->un_fileno = -1; 5002 un->un_err_resid = -tmp; 5003 if (un->un_fileno == 0 && un->un_blkno == 0) { 5004 un->un_status = SUN_KEY_BOT; 5005 un->un_eof = ST_NO_EOF; 5006 } else if (un->un_fileno > 0) { 5007 un->un_status = SUN_KEY_EOF; 5008 un->un_eof = ST_NO_EOF; 5009 } 5010 un->un_err_fileno = un->un_fileno; 5011 un->un_err_blkno = un->un_blkno; 5012 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5013 "st_mtioctop :EIO:space_records : tmp < 0"); 5014 rval = EIO; 5015 } else if (st_cmd(dev, SCMD_REWIND, 0, SYNC_CMD) || 5016 st_cmd(dev, SCMD_SPACE, Fmk(t), SYNC_CMD) || 5017 st_cmd(dev, SCMD_SPACE, Blk(tmp), 5018 SYNC_CMD)) { 5019 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5020 "st_mtioctop : EIO :space_records : rewind " 5021 "and space failed"); 5022 un->un_fileno = -1; 5023 rval = EIO; 5024 } 5025 } 5026 break; 5027 5028 5029 case MTBSF: 5030 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 5031 "bsf: count=%lx, eof=%x\n", mtop->mt_count, 5032 un->un_eof); 5033 /* 5034 * backward space of file filemark (1/2" and 8mm) 5035 * tape position will end on the beginning of tape side 5036 * of the desired file mark 5037 */ 5038 if ((un->un_dp->options & ST_BSF) == 0) { 5039 return (ENOTTY); 5040 } 5041 5042 /* 5043 * If a negative count (which implies a forward space op) 5044 * is specified, and we're at logical or physical eot, 5045 * bounce the request. 5046 */ 5047 5048 if (un->un_eof >= ST_EOT && mtop->mt_count < 0) { 5049 un->un_err_resid = mtop->mt_count; 5050 un->un_status = SUN_KEY_EOT; 5051 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5052 "st_ioctl : EIO : MTBSF : un_eof > ST_EOF"); 5053 return (EIO); 5054 } 5055 /* 5056 * physical tape position may not be what we've been 5057 * telling the user; adjust the request accordingly 5058 */ 5059 if (IN_EOF(un)) { 5060 un->un_fileno++; 5061 un->un_blkno = 0; 5062 mtop->mt_count++; 5063 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 5064 "bsf in eof: count=%ld, op=%x\n", 5065 mtop->mt_count, mtop->mt_op); 5066 5067 } 5068 5069 if (st_check_density_or_wfm(dev, 1, 0, STEPBACK)) { 5070 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5071 "st_ioctl : EIO : MTBSF : check den wfm"); 5072 return (EIO); 5073 } 5074 5075 if (mtop->mt_count <= 0) { 5076 /* 5077 * for a negative count, we need to step forward 5078 * first and then step back again 5079 */ 5080 mtop->mt_count = -mtop->mt_count+1; 5081 goto fspace; 5082 } 5083 5084 bspace: 5085 { 5086 int skip_cnt, end_at_eof; 5087 5088 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 5089 "bspace: count=%lx, eof=%x\n", mtop->mt_count, 5090 un->un_eof); 5091 /* 5092 * Backspace files (MTNBSF): 5093 * 5094 * For tapes that can backspace, backspace 5095 * count+1 filemarks and then run forward over 5096 * a filemark 5097 * 5098 * For tapes that can't backspace, 5099 * calculate desired filenumber 5100 * (un->un_fileno - count), rewind, 5101 * and then space forward this amount 5102 * 5103 * Backspace filemarks (MTBSF) 5104 * 5105 * For tapes that can backspace, backspace count 5106 * filemarks 5107 * 5108 * For tapes that can't backspace, calculate 5109 * desired filenumber (un->un_fileno - count), 5110 * add 1, rewind, space forward this amount, 5111 * and mark state as ST_EOF_PENDING appropriately. 5112 */ 5113 5114 if (mtop->mt_op == MTBSF) { 5115 end_at_eof = 1; 5116 } else { 5117 end_at_eof = 0; 5118 } 5119 5120 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 5121 "bspace: mt_op=%x, count=%lx, fileno=%x, blkno=%lx\n", 5122 mtop->mt_op, mtop->mt_count, un->un_fileno, un->un_blkno); 5123 5124 /* 5125 * Handle the simple case of BOT 5126 * playing a role in these cmds. 5127 * We do this by calculating the 5128 * ending file number. If the ending 5129 * file is < BOT, rewind and set an 5130 * error and mark resid appropriately. 5131 * If we're backspacing a file (not a 5132 * filemark) and the target file is 5133 * the first file on the tape, just 5134 * rewind. 5135 */ 5136 5137 tmp = un->un_fileno - mtop->mt_count; 5138 if ((end_at_eof && tmp < 0) || (end_at_eof == 0 && tmp <= 0)) { 5139 if (st_cmd(dev, SCMD_REWIND, 0, SYNC_CMD)) { 5140 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5141 "st_ioctl : EIO : bspace : end_at_eof && " 5142 "tmp < 0"); 5143 rval = EIO; 5144 } 5145 if (tmp < 0) { 5146 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5147 "st_ioctl : EIO : bspace : tmp < 0"); 5148 rval = EIO; 5149 un->un_err_resid = -tmp; 5150 un->un_status = SUN_KEY_BOT; 5151 } 5152 break; 5153 } 5154 5155 if (un->un_dp->options & ST_BSF) { 5156 skip_cnt = 1 - end_at_eof; 5157 /* 5158 * If we are going to end up at the beginning 5159 * of the file, we have to space one extra file 5160 * first, and then space forward later. 5161 */ 5162 tmp = -(mtop->mt_count + skip_cnt); 5163 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 5164 "skip_cnt=%x, tmp=%x\n", skip_cnt, tmp); 5165 if (st_cmd(dev, SCMD_SPACE, Fmk(tmp), SYNC_CMD)) { 5166 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5167 "st_ioctl : EIO : bspace : can't space " 5168 "tmp"); 5169 rval = EIO; 5170 } 5171 } else { 5172 if (st_cmd(dev, SCMD_REWIND, 0, SYNC_CMD)) { 5173 rval = EIO; 5174 } else { 5175 skip_cnt = tmp + end_at_eof; 5176 } 5177 } 5178 5179 /* 5180 * If we have to space forward, do so... 5181 */ 5182 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 5183 "space forward skip_cnt=%x, rval=%x\n", skip_cnt, rval); 5184 if (rval == 0 && skip_cnt) { 5185 if (st_cmd(dev, SCMD_SPACE, Fmk(skip_cnt), SYNC_CMD)) { 5186 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5187 "st_ioctl : EIO : bspace : can't space " 5188 "skip_cnt"); 5189 rval = EIO; 5190 } else if (end_at_eof) { 5191 /* 5192 * If we had to space forward, and we're 5193 * not a tape that can backspace, mark state 5194 * as if we'd just seen a filemark during a 5195 * a read. 5196 */ 5197 if ((un->un_dp->options & ST_BSF) == 0) { 5198 un->un_eof = ST_EOF_PENDING; 5199 un->un_fileno -= 1; 5200 un->un_blkno = INF; 5201 } 5202 } 5203 } 5204 5205 if (rval != 0) { 5206 un->un_fileno = -1; 5207 } 5208 break; 5209 } 5210 5211 case MTNBSF: 5212 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 5213 "nbsf: count=%lx, eof=%x\n", mtop->mt_count, 5214 un->un_eof); 5215 /* 5216 * backward space file to beginning of file 5217 * 5218 * If a negative count (which implies a forward space op) 5219 * is specified, and we're at logical or physical eot, 5220 * bounce the request. 5221 */ 5222 5223 if (un->un_eof >= ST_EOT && mtop->mt_count < 0) { 5224 un->un_err_resid = mtop->mt_count; 5225 un->un_status = SUN_KEY_EOT; 5226 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5227 "st_ioctl : EIO : > EOT and count < 0"); 5228 return (EIO); 5229 } 5230 /* 5231 * physical tape position may not be what we've been 5232 * telling the user; adjust the request accordingly 5233 */ 5234 if (IN_EOF(un)) { 5235 un->un_fileno++; 5236 un->un_blkno = 0; 5237 mtop->mt_count++; 5238 } 5239 5240 if (st_check_density_or_wfm(dev, 1, 0, STEPBACK)) { 5241 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5242 "st_ioctl : EIO : MTNBSF check den and wfm"); 5243 return (EIO); 5244 } 5245 5246 mtnbsf: 5247 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 5248 "mtnbsf: count=%lx, eof=%x\n", mtop->mt_count, 5249 un->un_eof); 5250 if (mtop->mt_count <= 0) { 5251 mtop->mt_op = MTFSF; 5252 mtop->mt_count = -mtop->mt_count; 5253 goto fspace; 5254 } 5255 goto bspace; 5256 5257 case MTBSR: 5258 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 5259 "bsr: count=%lx, eof=%x\n", mtop->mt_count, 5260 un->un_eof); 5261 /* 5262 * backward space into inter-record gap 5263 * 5264 * If a negative count (which implies a forward space op) 5265 * is specified, and we're at logical or physical eot, 5266 * bounce the request. 5267 */ 5268 if (un->un_eof >= ST_EOT && mtop->mt_count < 0) { 5269 un->un_err_resid = mtop->mt_count; 5270 un->un_status = SUN_KEY_EOT; 5271 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5272 "st_ioctl : EIO : MTBSR > EOT"); 5273 return (EIO); 5274 } 5275 5276 if (mtop->mt_count == 0) { 5277 un->un_err_fileno = un->un_fileno; 5278 un->un_err_blkno = un->un_blkno; 5279 un->un_err_resid = 0; 5280 if (IN_EOF(un) && SVR4_BEHAVIOR) { 5281 un->un_status = SUN_KEY_EOF; 5282 } 5283 return (0); 5284 } 5285 5286 /* 5287 * physical tape position may not be what we've been 5288 * telling the user; adjust the position accordingly. 5289 * bsr can not skip filemarks and continue to skip records 5290 * therefore if we are logically before the filemark but 5291 * physically at the EOT side of the filemark, we need to step 5292 * back; this allows fsr N where N > number of blocks in file 5293 * followed by bsr 1 to position at the beginning of last block 5294 */ 5295 if (IN_EOF(un)) { 5296 int blkno = un->un_blkno; 5297 int fileno = un->un_fileno; 5298 uchar_t lastop = un->un_lastop; 5299 if (st_cmd(dev, SCMD_SPACE, Fmk((-1)), SYNC_CMD) 5300 == -1) { 5301 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5302 "st_write_fm : EIO : MTBSR can't space"); 5303 return (EIO); 5304 } 5305 5306 un->un_blkno = blkno; 5307 un->un_fileno = fileno; 5308 un->un_lastop = lastop; 5309 } 5310 5311 un->un_eof = ST_NO_EOF; 5312 5313 if (st_check_density_or_wfm(dev, 1, 0, STEPBACK)) { 5314 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5315 "st_ioctl : EIO : MTBSR : can't set density or " 5316 "wfm"); 5317 return (EIO); 5318 } 5319 5320 mtop->mt_count = -mtop->mt_count; 5321 goto space_records; 5322 5323 case MTSRSZ: 5324 5325 /* 5326 * Set record-size to that sent by user 5327 * Check to see if there is reason that the requested 5328 * block size should not be set. 5329 */ 5330 5331 /* If requesting variable block size is it ok? */ 5332 if ((mtop->mt_count == 0) && 5333 ((un->un_dp->options & ST_VARIABLE) == 0)) { 5334 return (ENOTTY); 5335 } 5336 5337 /* 5338 * If requested block size is not variable "0", 5339 * is it less then minimum. 5340 */ 5341 if ((mtop->mt_count != 0) && 5342 (mtop->mt_count < un->un_minbsize)) { 5343 return (EINVAL); 5344 } 5345 5346 /* Is the requested block size more then maximum */ 5347 if ((mtop->mt_count > min(un->un_maxbsize, un->un_maxdma)) && 5348 (un->un_maxbsize != 0)) { 5349 return (EINVAL); 5350 } 5351 5352 /* Is requested block size a modulus the device likes */ 5353 if ((mtop->mt_count % un->un_data_mod) != 0) { 5354 return (EINVAL); 5355 } 5356 5357 if (st_change_block_size(dev, (uint32_t)mtop->mt_count) != 0) { 5358 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5359 "st_ioctl : MTSRSZ : EIO : cant set block size"); 5360 return (EIO); 5361 } 5362 5363 return (0); 5364 5365 case MTGRSZ: 5366 { 5367 #ifdef _MULTI_DATAMODEL 5368 /* 5369 * For use when a 32 bit app makes a call into a 5370 * 64 bit ioctl 5371 */ 5372 struct mtop32 mtop_32_for_64; 5373 #endif /* _MULTI_DATAMODEL */ 5374 5375 5376 /* 5377 * Get record-size to the user 5378 */ 5379 mtop->mt_count = un->un_bsize; 5380 5381 #ifdef _MULTI_DATAMODEL 5382 switch (ddi_model_convert_from(flag & FMODELS)) { 5383 case DDI_MODEL_ILP32: 5384 /* 5385 * Convert 64 bit back to 32 bit before doing 5386 * copyout. This is what the ILP32 app expects. 5387 */ 5388 mtop_32_for_64.mt_op = mtop->mt_op; 5389 mtop_32_for_64.mt_count = mtop->mt_count; 5390 5391 if (ddi_copyout(&mtop_32_for_64, (void *)arg, 5392 sizeof (struct mtop32), flag)) { 5393 return (EFAULT); 5394 } 5395 break; 5396 5397 case DDI_MODEL_NONE: 5398 if (ddi_copyout(mtop, (void *)arg, 5399 sizeof (struct mtop), flag)) { 5400 return (EFAULT); 5401 } 5402 break; 5403 } 5404 #else /* ! _MULTI_DATAMODE */ 5405 if (ddi_copyout(mtop, (void *)arg, sizeof (struct mtop), flag)) 5406 return (EFAULT); 5407 5408 #endif /* _MULTI_DATAMODE */ 5409 5410 return (0); 5411 } 5412 default: 5413 rval = ENOTTY; 5414 } 5415 5416 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 5417 "st_ioctl: fileno=%x, blkno=%lx, un_eof=%x\n", un->un_fileno, 5418 un->un_blkno, un->un_eof); 5419 5420 if (un->un_fileno < 0) { 5421 un->un_density_known = 0; 5422 } 5423 5424 ASSERT(mutex_owned(ST_MUTEX)); 5425 return (rval); 5426 } 5427 5428 5429 /* 5430 * Run a command for uscsi ioctl. 5431 */ 5432 static int 5433 st_ioctl_cmd(dev_t dev, struct uscsi_cmd *ucmd, int flag) 5434 { 5435 struct uscsi_cmd *uscmd; 5436 struct buf *bp; 5437 enum uio_seg uioseg; 5438 int offline_state = 0; 5439 int err = 0; 5440 int rw; 5441 5442 GET_SOFT_STATE(dev); 5443 5444 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 5445 "st_ioctl_cmd(dev = 0x%lx)\n", dev); 5446 5447 ASSERT(mutex_owned(ST_MUTEX)); 5448 5449 /* 5450 * We really don't know what commands are coming in here and 5451 * we don't want to limit the commands coming in. 5452 * 5453 * If st_tape_init() gets called from st_strategy(), then we 5454 * will hang the process waiting for un->un_sbuf_busy to be cleared, 5455 * which it never will, as we set it below. To prevent 5456 * st_tape_init() from getting called, we have to set state to other 5457 * than ST_STATE_OFFLINE, so we choose ST_STATE_INITIALIZING, which 5458 * achieves this purpose already. 5459 * 5460 * We use offline_state to preserve the OFFLINE state, if it exists, 5461 * so other entry points to the driver might have the chance to call 5462 * st_tape_init(). 5463 */ 5464 if (un->un_state == ST_STATE_OFFLINE) { 5465 un->un_laststate = ST_STATE_OFFLINE; 5466 un->un_state = ST_STATE_INITIALIZING; 5467 offline_state = 1; 5468 } 5469 5470 mutex_exit(ST_MUTEX); 5471 err = scsi_uscsi_alloc_and_copyin((intptr_t)ucmd, flag, 5472 ROUTE, &uscmd); 5473 mutex_enter(ST_MUTEX); 5474 if (err != 0) { 5475 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5476 "st_ioctl_cmd: scsi_uscsi_alloc_and_copyin failed\n"); 5477 goto exit; 5478 } 5479 5480 uioseg = (flag & FKIOCTL) ? UIO_SYSSPACE : UIO_USERSPACE; 5481 rw = (uscmd->uscsi_flags & USCSI_READ) ? B_READ : B_WRITE; 5482 5483 /* check to see if this command requires the drive to be reserved */ 5484 if (uscmd->uscsi_cdb != NULL) { 5485 err = st_check_cdb_for_need_to_reserve(un, 5486 &((char *)uscmd->uscsi_cdb)[0]); 5487 if (err) { 5488 goto exit_free; 5489 } 5490 } 5491 5492 /* 5493 * Get buffer resources... 5494 */ 5495 while (un->un_sbuf_busy) 5496 cv_wait(&un->un_sbuf_cv, ST_MUTEX); 5497 un->un_sbuf_busy = 1; 5498 5499 #ifdef STDEBUG 5500 if (uscmd->uscsi_cdb != NULL && st_debug > 6) { 5501 st_clean_print(ST_DEVINFO, st_label, SCSI_DEBUG, 5502 "uscsi cdb", uscmd->uscsi_cdb, uscmd->uscsi_cdblen); 5503 if (uscmd->uscsi_buflen) { 5504 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 5505 "uscsi %s of %ld bytes %s %s space\n", 5506 (rw == B_READ) ? rd_str : wr_str, 5507 uscmd->uscsi_buflen, 5508 (rw == B_READ) ? "to" : "from", 5509 (uioseg == UIO_SYSSPACE) ? "system" : "user"); 5510 } 5511 } 5512 #endif /* ST_DEBUG */ 5513 5514 /* 5515 * Although st_ioctl_cmd() never makes use of these 5516 * now, we are just being safe and consistent. 5517 */ 5518 uscmd->uscsi_flags &= ~(USCSI_NOINTR | USCSI_NOPARITY | 5519 USCSI_OTAG | USCSI_HTAG | USCSI_HEAD); 5520 5521 un->un_srqbufp = uscmd->uscsi_rqbuf; 5522 bp = un->un_sbufp; 5523 bzero(bp, sizeof (buf_t)); 5524 if (uscmd->uscsi_cdb != NULL) { 5525 bp->b_forw = 5526 (struct buf *)(uintptr_t)((char *)uscmd->uscsi_cdb)[0]; 5527 bp->b_back = (struct buf *)uscmd; 5528 } 5529 5530 mutex_exit(ST_MUTEX); 5531 err = scsi_uscsi_handle_cmd(dev, uioseg, uscmd, 5532 st_strategy, bp, NULL); 5533 mutex_enter(ST_MUTEX); 5534 5535 /* 5536 * If scsi reset successful, don't write any filemarks. 5537 */ 5538 if ((err == 0) && (uscmd->uscsi_flags & 5539 (USCSI_RESET_LUN | USCSI_RESET_TARGET | USCSI_RESET_ALL))) { 5540 un->un_fmneeded = 0; 5541 } 5542 5543 exit_free: 5544 /* 5545 * Free resources 5546 */ 5547 un->un_sbuf_busy = 0; 5548 un->un_srqbufp = NULL; 5549 cv_signal(&un->un_sbuf_cv); 5550 mutex_exit(ST_MUTEX); 5551 (void) scsi_uscsi_copyout_and_free((intptr_t)ucmd, uscmd); 5552 mutex_enter(ST_MUTEX); 5553 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 5554 "st_ioctl_cmd returns 0x%x\n", err); 5555 5556 exit: 5557 /* don't lose offline state */ 5558 if (offline_state) 5559 un->un_state = ST_STATE_OFFLINE; 5560 5561 ASSERT(mutex_owned(ST_MUTEX)); 5562 return (err); 5563 } 5564 5565 static int 5566 st_write_fm(dev_t dev, int wfm) 5567 { 5568 int i; 5569 int rval; 5570 5571 GET_SOFT_STATE(dev); 5572 5573 ASSERT(mutex_owned(ST_MUTEX)); 5574 5575 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 5576 "st_write_fm(dev = 0x%lx, wfm = %d)\n", dev, wfm); 5577 5578 /* 5579 * write one filemark at the time after EOT 5580 */ 5581 if (un->un_eof >= ST_EOT) { 5582 for (i = 0; i < wfm; i++) { 5583 rval = st_cmd(dev, SCMD_WRITE_FILE_MARK, 1, SYNC_CMD); 5584 if (rval == EACCES) { 5585 return (rval); 5586 } 5587 if (rval != 0) { 5588 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5589 "st_write_fm : EIO : write EOT file mark"); 5590 return (EIO); 5591 } 5592 } 5593 } else { 5594 rval = st_cmd(dev, SCMD_WRITE_FILE_MARK, wfm, SYNC_CMD); 5595 if (rval == EACCES) { 5596 return (rval); 5597 } 5598 if (rval) { 5599 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5600 "st_write_fm : EIO : write file mark"); 5601 return (EIO); 5602 } 5603 } 5604 5605 ASSERT(mutex_owned(ST_MUTEX)); 5606 return (0); 5607 } 5608 5609 #ifdef STDEBUG 5610 static void 5611 start_dump(struct scsi_tape *un, struct buf *bp) 5612 { 5613 struct scsi_pkt *pkt = BP_PKT(bp); 5614 uchar_t *cdbp = (uchar_t *)pkt->pkt_cdbp; 5615 5616 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 5617 "st_start: cmd=0x%p count=%ld resid=%ld flags=0x%x pkt=0x%p\n", 5618 (void *)bp->b_forw, bp->b_bcount, 5619 bp->b_resid, bp->b_flags, (void *)BP_PKT(bp)); 5620 5621 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 5622 "st_start: cdb %x %x %x %x %x %x, fileno=%d, blk=%ld\n", 5623 cdbp[0], cdbp[1], cdbp[2], 5624 cdbp[3], cdbp[4], cdbp[5], un->un_fileno, 5625 un->un_blkno); 5626 } 5627 #endif 5628 5629 5630 /* 5631 * Command start && done functions 5632 */ 5633 5634 /* 5635 * st_start() 5636 * 5637 * Called from: 5638 * st_strategy() to start a command. 5639 * st_runout() to retry when scsi_pkt allocation fails on previous attempt(s). 5640 * st_attach() when resuming from power down state. 5641 * st_start_restart() to retry transport when device was previously busy. 5642 * st_done_and_mutex_exit() to start the next command when previous is done. 5643 * 5644 * On entry: 5645 * scsi_pkt may or may not be allocated. 5646 * 5647 */ 5648 static void 5649 st_start(struct scsi_tape *un) 5650 { 5651 struct buf *bp; 5652 int status; 5653 5654 ASSERT(mutex_owned(ST_MUTEX)); 5655 5656 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 5657 "st_start(): dev = 0x%lx\n", un->un_dev); 5658 5659 if ((bp = un->un_quef) == NULL) { 5660 return; 5661 } 5662 5663 ASSERT((bp->b_flags & B_DONE) == 0); 5664 5665 /* 5666 * Don't send more than un_throttle commands to the HBA 5667 */ 5668 if ((un->un_throttle <= 0) || (un->un_ncmds >= un->un_throttle)) { 5669 return; 5670 } 5671 5672 /* 5673 * If the buf has no scsi_pkt call st_make_cmd() to get one and 5674 * build the command. 5675 */ 5676 if (BP_PKT(bp) == NULL) { 5677 ASSERT((bp->b_flags & B_DONE) == 0); 5678 st_make_cmd(un, bp, st_runout); 5679 ASSERT((bp->b_flags & B_DONE) == 0); 5680 status = geterror(bp); 5681 5682 /* 5683 * Some HBA's don't call bioerror() to set an error. 5684 * And geterror() returns zero if B_ERROR is not set. 5685 * So if we get zero we must check b_error. 5686 */ 5687 if (status == 0 && bp->b_error != 0) { 5688 status = bp->b_error; 5689 bioerror(bp, status); 5690 } 5691 5692 /* 5693 * Some HBA's convert DDI_DMA_NORESOURCES into ENOMEM. 5694 * In tape ENOMEM has special meaning so we'll change it. 5695 */ 5696 if (status == ENOMEM) { 5697 status = 0; 5698 bioerror(bp, status); 5699 } 5700 5701 /* 5702 * Did it fail and is it retryable? 5703 * If so return and wait for the callback through st_runout. 5704 * Also looks like scsi_init_pkt() will setup a callback even 5705 * if it isn't retryable. 5706 */ 5707 if (BP_PKT(bp) == NULL) { 5708 if (status == 0) { 5709 /* 5710 * If first attempt save state. 5711 */ 5712 if (un->un_state != ST_STATE_RESOURCE_WAIT) { 5713 un->un_laststate = un->un_state; 5714 un->un_state = ST_STATE_RESOURCE_WAIT; 5715 } 5716 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5717 "temp no resources for pkt\n"); 5718 } else { 5719 /* 5720 * Unlikely that it would be retryable then not. 5721 */ 5722 if (un->un_state == ST_STATE_RESOURCE_WAIT) { 5723 un->un_state = un->un_laststate; 5724 } 5725 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 5726 "perm no resources for pkt errno = 0x%x\n", 5727 status); 5728 } 5729 return; 5730 } 5731 /* 5732 * Worked this time set the state back. 5733 */ 5734 if (un->un_state == ST_STATE_RESOURCE_WAIT) { 5735 un->un_state = un->un_laststate; 5736 } 5737 } 5738 5739 /* 5740 * move from waitq to runq 5741 */ 5742 un->un_quef = bp->b_actf; 5743 if (un->un_quel == bp) { 5744 /* 5745 * For the case of queue having one 5746 * element, set the tail pointer to 5747 * point to the element. 5748 */ 5749 un->un_quel = bp->b_actf; 5750 } 5751 5752 bp->b_actf = NULL; 5753 5754 if (un->un_runqf) { 5755 un->un_runql->b_actf = bp; 5756 } else { 5757 un->un_runqf = bp; 5758 } 5759 un->un_runql = bp; 5760 5761 5762 #ifdef STDEBUG 5763 start_dump(un, bp); 5764 #endif 5765 5766 /* could not get here if throttle was zero */ 5767 un->un_last_throttle = un->un_throttle; 5768 un->un_throttle = 0; /* so nothing else will come in here */ 5769 un->un_ncmds++; 5770 5771 ST_DO_KSTATS(bp, kstat_waitq_to_runq); 5772 5773 mutex_exit(ST_MUTEX); 5774 5775 status = scsi_transport(BP_PKT(bp)); 5776 5777 mutex_enter(ST_MUTEX); 5778 5779 if (un->un_last_throttle) { 5780 un->un_throttle = un->un_last_throttle; 5781 } 5782 5783 if (status != TRAN_ACCEPT) { 5784 ST_DO_KSTATS(bp, kstat_runq_back_to_waitq); 5785 mutex_exit(ST_MUTEX); 5786 5787 if (status == TRAN_BUSY) { 5788 /* if too many retries, fail the transport */ 5789 if (st_handle_start_busy(un, bp, 5790 ST_TRAN_BUSY_TIMEOUT) == 0) 5791 goto done; 5792 } 5793 scsi_log(ST_DEVINFO, st_label, CE_WARN, 5794 "transport rejected\n"); 5795 bp->b_resid = bp->b_bcount; 5796 5797 5798 #ifndef __lock_lint 5799 /* 5800 * warlock doesn't understand this potential 5801 * recursion? 5802 */ 5803 mutex_enter(ST_MUTEX); 5804 ST_DO_KSTATS(bp, kstat_waitq_exit); 5805 ST_DO_ERRSTATS(un, st_transerrs); 5806 st_bioerror(bp, EIO); 5807 SET_PE_FLAG(un); 5808 st_done_and_mutex_exit(un, bp); 5809 #endif 5810 } else { 5811 un->un_tran_retry_ct = 0; 5812 mutex_exit(ST_MUTEX); 5813 } 5814 5815 done: 5816 5817 mutex_enter(ST_MUTEX); 5818 } 5819 5820 /* 5821 * if the transport is busy, then put this bp back on the waitq 5822 */ 5823 static int 5824 st_handle_start_busy(struct scsi_tape *un, struct buf *bp, 5825 clock_t timeout_interval) 5826 { 5827 struct buf *last_quef, *runq_bp; 5828 int rval = 0; 5829 5830 mutex_enter(ST_MUTEX); 5831 5832 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 5833 "st_handle_start_busy()\n"); 5834 5835 /* 5836 * Check to see if we hit the retry timeout and one last check for 5837 * making sure this is the last on the runq, if it is not, we have 5838 * to fail 5839 */ 5840 if (((int)un->un_tran_retry_ct++ > st_retry_count) || 5841 (un->un_runql != bp)) { 5842 rval = -1; 5843 goto exit; 5844 } 5845 5846 /* put the bp back on the waitq */ 5847 if (un->un_quef) { 5848 last_quef = un->un_quef; 5849 un->un_quef = bp; 5850 bp->b_actf = last_quef; 5851 } else { 5852 bp->b_actf = NULL; 5853 un->un_quef = bp; 5854 un->un_quel = bp; 5855 } 5856 5857 /* 5858 * Decrement un_ncmds so that this 5859 * gets thru' st_start() again. 5860 */ 5861 un->un_ncmds--; 5862 5863 /* 5864 * since this is an error case, we won't have to do 5865 * this list walking much. We've already made sure this bp was the 5866 * last on the runq 5867 */ 5868 runq_bp = un->un_runqf; 5869 5870 if (un->un_runqf == bp) { 5871 un->un_runqf = NULL; 5872 un->un_runql = NULL; 5873 } else { 5874 while (runq_bp) { 5875 if (runq_bp->b_actf == bp) { 5876 runq_bp->b_actf = NULL; 5877 un->un_runql = runq_bp; 5878 break; 5879 } 5880 runq_bp = runq_bp->b_actf; 5881 } 5882 } 5883 5884 5885 /* 5886 * send a marker pkt, if appropriate 5887 */ 5888 st_hba_unflush(un); 5889 5890 /* 5891 * all queues are aligned, we are just waiting to 5892 * transport, don't alloc any more buf p's, when 5893 * st_start is reentered. 5894 */ 5895 (void) timeout(st_start_restart, un, timeout_interval); 5896 5897 exit: 5898 mutex_exit(ST_MUTEX); 5899 return (rval); 5900 } 5901 5902 5903 /* 5904 * st_runout a callback that is called what a resource allocatation failed 5905 */ 5906 static int 5907 st_runout(caddr_t arg) 5908 { 5909 struct scsi_tape *un = (struct scsi_tape *)arg; 5910 struct buf *bp; 5911 ASSERT(un != NULL); 5912 5913 mutex_enter(ST_MUTEX); 5914 5915 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, "st_runout()\n"); 5916 5917 bp = un->un_quef; 5918 5919 /* 5920 * failed scsi_init_pkt(). If errno is zero its retryable. 5921 */ 5922 if ((bp != NULL) && (geterror(bp) != 0)) { 5923 5924 scsi_log(ST_DEVINFO, st_label, CE_WARN, 5925 "errors after pkt alloc (b_flags=0x%x, b_error=0x%x)\n", 5926 bp->b_flags, geterror(bp)); 5927 ASSERT((bp->b_flags & B_DONE) == 0); 5928 5929 un->un_quef = bp->b_actf; 5930 if (un->un_quel == bp) { 5931 /* 5932 * For the case of queue having one 5933 * element, set the tail pointer to 5934 * point to the element. 5935 */ 5936 un->un_quel = bp->b_actf; 5937 } 5938 mutex_exit(ST_MUTEX); 5939 bp->b_actf = NULL; 5940 5941 ASSERT((bp->b_flags & B_DONE) == 0); 5942 5943 /* 5944 * Set resid, Error already set, then unblock calling thread. 5945 */ 5946 bp->b_resid = bp->b_bcount; 5947 biodone(bp); 5948 } else { 5949 /* 5950 * Try Again 5951 */ 5952 st_start(un); 5953 mutex_exit(ST_MUTEX); 5954 } 5955 5956 /* 5957 * Comments courtesy of sd.c 5958 * The scsi_init_pkt routine allows for the callback function to 5959 * return a 0 indicating the callback should be rescheduled or a 1 5960 * indicating not to reschedule. This routine always returns 1 5961 * because the driver always provides a callback function to 5962 * scsi_init_pkt. This results in a callback always being scheduled 5963 * (via the scsi_init_pkt callback implementation) if a resource 5964 * failure occurs. 5965 */ 5966 5967 return (1); 5968 } 5969 5970 /* 5971 * st_done_and_mutex_exit() 5972 * - remove bp from runq 5973 * - start up the next request 5974 * - if this was an asynch bp, clean up 5975 * - exit with released mutex 5976 */ 5977 static void 5978 st_done_and_mutex_exit(struct scsi_tape *un, struct buf *bp) 5979 { 5980 struct buf *runqbp, *prevbp; 5981 int pe_flagged = 0; 5982 5983 ASSERT(MUTEX_HELD(&un->un_sd->sd_mutex)); 5984 #if !defined(lint) 5985 _NOTE(LOCK_RELEASED_AS_SIDE_EFFECT(&un->un_sd->sd_mutex)) 5986 #endif 5987 ASSERT(mutex_owned(ST_MUTEX)); 5988 5989 /* 5990 * if bp is still on the runq (anywhere), then remove it 5991 */ 5992 prevbp = NULL; 5993 for (runqbp = un->un_runqf; runqbp != 0; runqbp = runqbp->b_actf) { 5994 if (runqbp == bp) { 5995 if (runqbp == un->un_runqf) { 5996 un->un_runqf = bp->b_actf; 5997 } else { 5998 prevbp->b_actf = bp->b_actf; 5999 } 6000 if (un->un_runql == bp) { 6001 un->un_runql = prevbp; 6002 } 6003 break; 6004 } 6005 prevbp = runqbp; 6006 } 6007 bp->b_actf = NULL; 6008 6009 un->un_ncmds--; 6010 cv_signal(&un->un_queue_cv); 6011 6012 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 6013 "st_done_and_mutex_exit(): cmd=0x%x count=%ld resid=%ld flags=0x%x\n", 6014 (uchar_t)*((caddr_t)(BP_PKT(bp))->pkt_cdbp), 6015 bp->b_bcount, bp->b_resid, bp->b_flags); 6016 6017 6018 /* 6019 * update kstats with transfer count info 6020 */ 6021 if (un->un_stats && (bp != un->un_sbufp) && IS_RW(bp)) { 6022 uint32_t n_done = bp->b_bcount - bp->b_resid; 6023 if (bp->b_flags & B_READ) { 6024 IOSP->reads++; 6025 IOSP->nread += n_done; 6026 } else { 6027 IOSP->writes++; 6028 IOSP->nwritten += n_done; 6029 } 6030 } 6031 6032 /* 6033 * Start the next one before releasing resources on this one, if 6034 * there is something on the queue and persistent errors has not been 6035 * flagged 6036 */ 6037 6038 if ((pe_flagged = IS_PE_FLAG_SET(un)) != 0) { 6039 un->un_last_resid = bp->b_resid; 6040 un->un_last_count = bp->b_bcount; 6041 } 6042 6043 if (un->un_pwr_mgmt == ST_PWR_SUSPENDED) { 6044 cv_broadcast(&un->un_tape_busy_cv); 6045 } else if (un->un_quef && un->un_throttle && !pe_flagged) { 6046 st_start(un); 6047 } 6048 6049 if (bp == un->un_sbufp && (bp->b_flags & B_ASYNC)) { 6050 /* 6051 * Since we marked this ourselves as ASYNC, 6052 * there isn't anybody around waiting for 6053 * completion any more. 6054 */ 6055 uchar_t com = (uchar_t)(uintptr_t)bp->b_forw; 6056 if (com == SCMD_READ || com == SCMD_WRITE) { 6057 bp->b_un.b_addr = (caddr_t)0; 6058 } 6059 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 6060 "st_done_and_mutex_exit(async): freeing pkt\n"); 6061 scsi_destroy_pkt(BP_PKT(bp)); 6062 un->un_sbuf_busy = 0; 6063 cv_signal(&un->un_sbuf_cv); 6064 mutex_exit(ST_MUTEX); 6065 return; 6066 } 6067 6068 if (bp == un->un_sbufp && BP_UCMD(bp)) { 6069 /* 6070 * Copy status from scsi_pkt to uscsi_cmd 6071 * since st_ioctl_cmd needs it 6072 */ 6073 BP_UCMD(bp)->uscsi_status = SCBP_C(BP_PKT(bp)); 6074 } 6075 6076 6077 #ifdef STDEBUG 6078 if ((st_debug >= 4) && 6079 (((un->un_blkno % 100) == 0) || IS_PE_FLAG_SET(un))) { 6080 6081 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 6082 "st_d_a_m_exit(): ncmds = %d, thr = %d, " 6083 "un_errno = %d, un_pe = %d\n", 6084 un->un_ncmds, un->un_throttle, un->un_errno, 6085 un->un_persist_errors); 6086 } 6087 6088 #endif 6089 6090 mutex_exit(ST_MUTEX); 6091 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 6092 "st_done_and_mutex_exit: freeing pkt\n"); 6093 6094 scsi_destroy_pkt(BP_PKT(bp)); 6095 6096 biodone(bp); 6097 6098 /* 6099 * now that we biodoned that command, if persistent errors have been 6100 * flagged, flush the waitq 6101 */ 6102 if (pe_flagged) 6103 st_flush(un); 6104 } 6105 6106 6107 /* 6108 * Tape error, flush tape driver queue. 6109 */ 6110 static void 6111 st_flush(struct scsi_tape *un) 6112 { 6113 struct buf *bp; 6114 6115 mutex_enter(ST_MUTEX); 6116 6117 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 6118 "st_flush(), ncmds = %d, quef = 0x%p\n", 6119 un->un_ncmds, (void *)un->un_quef); 6120 6121 /* 6122 * if we still have commands outstanding, wait for them to come in 6123 * before flushing the queue, and make sure there is a queue 6124 */ 6125 if (un->un_ncmds || !un->un_quef) 6126 goto exit; 6127 6128 /* 6129 * we have no more commands outstanding, so let's deal with special 6130 * cases in the queue for EOM and FM. If we are here, and un_errno 6131 * is 0, then we know there was no error and we return a 0 read or 6132 * write before showing errors 6133 */ 6134 6135 /* Flush the wait queue. */ 6136 while ((bp = un->un_quef) != NULL) { 6137 un->un_quef = bp->b_actf; 6138 6139 bp->b_resid = bp->b_bcount; 6140 6141 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 6142 "st_flush() : blkno=%ld, err=%d, b_bcount=%ld\n", 6143 un->un_blkno, un->un_errno, bp->b_bcount); 6144 6145 st_set_pe_errno(un); 6146 6147 bioerror(bp, un->un_errno); 6148 6149 mutex_exit(ST_MUTEX); 6150 /* it should have one, but check anyway */ 6151 if (BP_PKT(bp)) { 6152 scsi_destroy_pkt(BP_PKT(bp)); 6153 } 6154 biodone(bp); 6155 mutex_enter(ST_MUTEX); 6156 } 6157 6158 /* 6159 * It's not a bad practice to reset the 6160 * waitq tail pointer to NULL. 6161 */ 6162 un->un_quel = NULL; 6163 6164 exit: 6165 /* we mucked with the queue, so let others know about it */ 6166 cv_signal(&un->un_queue_cv); 6167 mutex_exit(ST_MUTEX); 6168 } 6169 6170 6171 /* 6172 * Utility functions 6173 */ 6174 static int 6175 st_determine_generic(dev_t dev) 6176 { 6177 int bsize; 6178 static char *cart = "0.25 inch cartridge"; 6179 char *sizestr; 6180 6181 GET_SOFT_STATE(dev); 6182 6183 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 6184 "st_determine_generic(dev = 0x%lx)\n", dev); 6185 6186 ASSERT(mutex_owned(ST_MUTEX)); 6187 6188 if (st_modesense(un)) { 6189 return (-1); 6190 } 6191 6192 bsize = (un->un_mspl->high_bl << 16) | 6193 (un->un_mspl->mid_bl << 8) | 6194 (un->un_mspl->low_bl); 6195 6196 if (bsize == 0) { 6197 un->un_dp->options |= ST_VARIABLE; 6198 un->un_dp->bsize = 0; 6199 un->un_bsize = 0; 6200 } else if (bsize > ST_MAXRECSIZE_FIXED) { 6201 /* 6202 * record size of this device too big. 6203 * try and convert it to variable record length. 6204 * 6205 */ 6206 un->un_dp->options |= ST_VARIABLE; 6207 if (st_change_block_size(dev, 0) != 0) { 6208 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 6209 "Fixed Record Size %d is too large\n", bsize); 6210 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 6211 "Cannot switch to variable record size\n"); 6212 un->un_dp->options &= ~ST_VARIABLE; 6213 return (-1); 6214 } 6215 } else if (st_change_block_size(dev, 0) == 0) { 6216 /* 6217 * If the drive was set to a non zero block size, 6218 * See if it can be set to a zero block size. 6219 * If it works, ST_VARIABLE so user can set it as they want. 6220 */ 6221 un->un_dp->options |= ST_VARIABLE; 6222 un->un_dp->bsize = 0; 6223 un->un_bsize = 0; 6224 } else { 6225 un->un_dp->bsize = bsize; 6226 un->un_bsize = bsize; 6227 } 6228 6229 6230 switch (un->un_mspl->density) { 6231 default: 6232 case 0x0: 6233 /* 6234 * default density, cannot determine any other 6235 * information. 6236 */ 6237 sizestr = "Unknown type- assuming 0.25 inch cartridge"; 6238 un->un_dp->type = ST_TYPE_DEFAULT; 6239 un->un_dp->options |= (ST_AUTODEN_OVERRIDE|ST_QIC); 6240 break; 6241 case 0x1: 6242 case 0x2: 6243 case 0x3: 6244 case 0x6: 6245 /* 6246 * 1/2" reel 6247 */ 6248 sizestr = "0.50 inch reel"; 6249 un->un_dp->type = ST_TYPE_REEL; 6250 un->un_dp->options |= ST_REEL; 6251 un->un_dp->densities[0] = 0x1; 6252 un->un_dp->densities[1] = 0x2; 6253 un->un_dp->densities[2] = 0x6; 6254 un->un_dp->densities[3] = 0x3; 6255 break; 6256 case 0x4: 6257 case 0x5: 6258 case 0x7: 6259 case 0x0b: 6260 6261 /* 6262 * Quarter inch. 6263 */ 6264 sizestr = cart; 6265 un->un_dp->type = ST_TYPE_DEFAULT; 6266 un->un_dp->options |= ST_QIC; 6267 6268 un->un_dp->densities[1] = 0x4; 6269 un->un_dp->densities[2] = 0x5; 6270 un->un_dp->densities[3] = 0x7; 6271 un->un_dp->densities[0] = 0x0b; 6272 break; 6273 6274 case 0x0f: 6275 case 0x10: 6276 case 0x11: 6277 case 0x12: 6278 /* 6279 * QIC-120, QIC-150, QIC-320, QIC-600 6280 */ 6281 sizestr = cart; 6282 un->un_dp->type = ST_TYPE_DEFAULT; 6283 un->un_dp->options |= ST_QIC; 6284 un->un_dp->densities[0] = 0x0f; 6285 un->un_dp->densities[1] = 0x10; 6286 un->un_dp->densities[2] = 0x11; 6287 un->un_dp->densities[3] = 0x12; 6288 break; 6289 6290 case 0x09: 6291 case 0x0a: 6292 case 0x0c: 6293 case 0x0d: 6294 /* 6295 * 1/2" cartridge tapes. Include HI-TC. 6296 */ 6297 sizestr = cart; 6298 sizestr[2] = '5'; 6299 sizestr[3] = '0'; 6300 un->un_dp->type = ST_TYPE_HIC; 6301 un->un_dp->densities[0] = 0x09; 6302 un->un_dp->densities[1] = 0x0a; 6303 un->un_dp->densities[2] = 0x0c; 6304 un->un_dp->densities[3] = 0x0d; 6305 break; 6306 6307 case 0x13: 6308 /* DDS-2/DDS-3 scsi spec densities */ 6309 case 0x24: 6310 case 0x25: 6311 case 0x26: 6312 sizestr = "DAT Data Storage (DDS)"; 6313 un->un_dp->type = ST_TYPE_DAT; 6314 un->un_dp->options |= ST_AUTODEN_OVERRIDE; 6315 break; 6316 6317 case 0x14: 6318 /* 6319 * Helical Scan (Exabyte) devices 6320 */ 6321 sizestr = "8mm helical scan cartridge"; 6322 un->un_dp->type = ST_TYPE_EXABYTE; 6323 un->un_dp->options |= ST_AUTODEN_OVERRIDE; 6324 break; 6325 } 6326 6327 /* 6328 * Assume LONG ERASE, BSF and BSR 6329 */ 6330 6331 un->un_dp->options |= (ST_LONG_ERASE|ST_UNLOADABLE|ST_BSF| 6332 ST_BSR|ST_KNOWS_EOD); 6333 6334 /* 6335 * Only if mode sense data says no buffered write, set NOBUF 6336 */ 6337 if (un->un_mspl->bufm == 0) 6338 un->un_dp->options |= ST_NOBUF; 6339 6340 /* 6341 * set up large read and write retry counts 6342 */ 6343 6344 un->un_dp->max_rretries = un->un_dp->max_wretries = 1000; 6345 6346 /* 6347 * If this is a 0.50 inch reel tape, and 6348 * it is *not* variable mode, try and 6349 * set it to variable record length 6350 * mode. 6351 */ 6352 if ((un->un_dp->options & ST_REEL) && un->un_bsize != 0 && 6353 (un->un_dp->options & ST_VARIABLE)) { 6354 if (st_change_block_size(dev, 0) == 0) { 6355 un->un_dp->bsize = 0; 6356 un->un_mspl->high_bl = un->un_mspl->mid_bl = 6357 un->un_mspl->low_bl = 0; 6358 } 6359 } 6360 6361 /* 6362 * Write to console about type of device found 6363 */ 6364 ST_DEBUG6(ST_DEVINFO, st_label, CE_NOTE, 6365 "Generic Drive, Vendor=%s\n\t%s", un->un_dp->name, 6366 sizestr); 6367 if (un->un_dp->options & ST_VARIABLE) { 6368 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 6369 "!Variable record length I/O\n"); 6370 } else { 6371 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 6372 "!Fixed record length (%d byte blocks) I/O\n", 6373 un->un_dp->bsize); 6374 } 6375 ASSERT(mutex_owned(ST_MUTEX)); 6376 return (0); 6377 } 6378 6379 static int 6380 st_determine_density(dev_t dev, int rw) 6381 { 6382 int rval = 0; 6383 6384 GET_SOFT_STATE(dev); 6385 6386 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 6387 "st_determine_density(dev = 0x%lx, rw = %s)\n", 6388 dev, (rw == B_WRITE ? wr_str: rd_str)); 6389 6390 ASSERT(mutex_owned(ST_MUTEX)); 6391 6392 /* 6393 * If we're past BOT, density is determined already. 6394 */ 6395 if (un->un_fileno > 0 || (un->un_fileno == 0 && un->un_blkno != 0)) { 6396 /* 6397 * XXX: put in a bitch message about attempting to 6398 * XXX: change density past BOT. 6399 */ 6400 goto exit; 6401 } 6402 6403 /* 6404 * If we're going to be writing, we set the density 6405 */ 6406 if (rw == 0 || rw == B_WRITE) { 6407 /* un_curdens is used as an index into densities table */ 6408 un->un_curdens = MT_DENSITY(un->un_dev); 6409 if (st_set_density(dev)) { 6410 rval = -1; 6411 } 6412 goto exit; 6413 } 6414 6415 /* 6416 * If density is known already, 6417 * we don't have to get it again.(?) 6418 */ 6419 if (!un->un_density_known) { 6420 if (st_get_density(dev)) { 6421 rval = -1; 6422 } 6423 } 6424 6425 exit: 6426 ASSERT(mutex_owned(ST_MUTEX)); 6427 return (rval); 6428 } 6429 6430 6431 /* 6432 * Try to determine density. We do this by attempting to read the 6433 * first record off the tape, cycling through the available density 6434 * codes as we go. 6435 */ 6436 6437 static int 6438 st_get_density(dev_t dev) 6439 { 6440 int succes = 0, rval = -1, i; 6441 uint_t size; 6442 uchar_t dens, olddens; 6443 6444 GET_SOFT_STATE(dev); 6445 6446 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 6447 "st_get_density(dev = 0x%lx)\n", dev); 6448 6449 ASSERT(mutex_owned(ST_MUTEX)); 6450 6451 /* 6452 * If Auto Density override is enabled The drive has 6453 * only one density and there is no point in attempting 6454 * find the correct one. 6455 * 6456 * Since most modern drives auto detect the density 6457 * and format of the recorded media before they come 6458 * ready. What this function does is a legacy behavior 6459 * and modern drives not only don't need it, The backup 6460 * utilities that do positioning via uscsi find the un- 6461 * expected rewinds problematic. 6462 * 6463 * The drives that need this are old reel to reel devices. 6464 * I took a swag and said they must be scsi-1 or older. 6465 * I don't beleave there will any of the newer devices 6466 * that need this. There will be some scsi-1 devices that 6467 * don't need this but I don't think they will be using the 6468 * BIG aftermarket backup and restore utilitys. 6469 */ 6470 if ((un->un_dp->options & ST_AUTODEN_OVERRIDE) || 6471 (un->un_sd->sd_inq->inq_ansi > 1)) { 6472 un->un_density_known = 1; 6473 rval = 0; 6474 goto exit; 6475 } 6476 6477 /* 6478 * This will only work on variable record length tapes 6479 * if and only if all variable record length tapes autodensity 6480 * select. 6481 */ 6482 size = (unsigned)(un->un_dp->bsize ? un->un_dp->bsize : SECSIZE); 6483 un->un_tmpbuf = kmem_alloc(size, KM_SLEEP); 6484 6485 /* 6486 * Start at the specified density 6487 */ 6488 6489 dens = olddens = un->un_curdens = MT_DENSITY(un->un_dev); 6490 6491 for (i = 0; i < NDENSITIES; i++, ((un->un_curdens == NDENSITIES - 1) ? 6492 (un->un_curdens = 0) : 6493 (un->un_curdens += 1))) { 6494 /* 6495 * If we've done this density before, 6496 * don't bother to do it again. 6497 */ 6498 dens = un->un_dp->densities[un->un_curdens]; 6499 if (i > 0 && dens == olddens) 6500 continue; 6501 olddens = dens; 6502 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 6503 "trying density 0x%x\n", dens); 6504 if (st_set_density(dev)) { 6505 continue; 6506 } 6507 6508 /* 6509 * XXX - the creates lots of headaches and slowdowns - must 6510 * fix. 6511 */ 6512 succes = (st_cmd(dev, SCMD_READ, (int)size, SYNC_CMD) == 0); 6513 if (st_cmd(dev, SCMD_REWIND, 0, SYNC_CMD)) { 6514 break; 6515 } 6516 if (succes) { 6517 st_init(un); 6518 rval = 0; 6519 un->un_density_known = 1; 6520 break; 6521 } 6522 } 6523 kmem_free(un->un_tmpbuf, size); 6524 un->un_tmpbuf = 0; 6525 6526 exit: 6527 ASSERT(mutex_owned(ST_MUTEX)); 6528 return (rval); 6529 } 6530 6531 static int 6532 st_set_density(dev_t dev) 6533 { 6534 int rval = 0; 6535 6536 GET_SOFT_STATE(dev); 6537 6538 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 6539 "st_set_density(dev = 0x%lx): density = 0x%x\n", dev, 6540 un->un_dp->densities[un->un_curdens]); 6541 6542 ASSERT(mutex_owned(ST_MUTEX)); 6543 6544 un->un_mspl->density = un->un_dp->densities[un->un_curdens]; 6545 6546 if ((un->un_dp->options & ST_AUTODEN_OVERRIDE) == 0) { 6547 /* 6548 * If auto density override is not set, Use mode select 6549 * to set density and compression. 6550 */ 6551 if (st_modeselect(un)) { 6552 rval = -1; 6553 } 6554 } else if ((un->un_dp->options & ST_MODE_SEL_COMP) != 0) { 6555 /* 6556 * If auto density and mode select compression are set, 6557 * This is a drive with one density code but compression 6558 * can be enabled or disabled. 6559 * Set compression but no need to set density. 6560 */ 6561 rval = st_set_compression(un); 6562 if ((rval != 0) && (rval != EALREADY)) { 6563 rval = -1; 6564 } else { 6565 rval = 0; 6566 } 6567 } 6568 6569 /* If sucessful set density and/or compression, mark density known */ 6570 if (rval == 0) { 6571 un->un_density_known = 1; 6572 } 6573 6574 ASSERT(mutex_owned(ST_MUTEX)); 6575 return (rval); 6576 } 6577 6578 static int 6579 st_loadtape(dev_t dev) 6580 { 6581 int rval; 6582 6583 GET_SOFT_STATE(dev); 6584 6585 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 6586 "st_loadtape(dev = 0x%lx)\n", dev); 6587 6588 ASSERT(mutex_owned(ST_MUTEX)); 6589 6590 /* 6591 * 'LOAD' the tape to BOT by rewinding 6592 */ 6593 rval = st_cmd(dev, SCMD_REWIND, 1, SYNC_CMD); 6594 if (rval == 0) { 6595 st_init(un); 6596 un->un_density_known = 0; 6597 } 6598 6599 ASSERT(mutex_owned(ST_MUTEX)); 6600 return (rval); 6601 } 6602 6603 6604 /* 6605 * Note: QIC devices aren't so smart. If you try to append 6606 * after EOM, the write can fail because the device doesn't know 6607 * it's at EOM. In that case, issue a read. The read should fail 6608 * because there's no data, but the device knows it's at EOM, 6609 * so a subsequent write should succeed. To further confuse matters, 6610 * the target returns the same error if the tape is positioned 6611 * such that a write would overwrite existing data. That's why 6612 * we have to do the append test. A read in the middle of 6613 * recorded data would succeed, thus indicating we're attempting 6614 * something illegal. 6615 */ 6616 6617 void bp_mapin(struct buf *bp); 6618 6619 static void 6620 st_test_append(struct buf *bp) 6621 { 6622 dev_t dev = bp->b_edev; 6623 struct scsi_tape *un; 6624 uchar_t status; 6625 unsigned bcount; 6626 6627 un = ddi_get_soft_state(st_state, MTUNIT(dev)); 6628 6629 ASSERT(mutex_owned(ST_MUTEX)); 6630 6631 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 6632 "st_test_append(): fileno %d\n", un->un_fileno); 6633 6634 un->un_laststate = un->un_state; 6635 un->un_state = ST_STATE_APPEND_TESTING; 6636 un->un_test_append = 0; 6637 6638 /* 6639 * first, map in the buffer, because we're doing a double write -- 6640 * first into the kernel, then onto the tape. 6641 */ 6642 bp_mapin(bp); 6643 6644 /* 6645 * get a copy of the data.... 6646 */ 6647 un->un_tmpbuf = kmem_alloc((unsigned)bp->b_bcount, KM_SLEEP); 6648 bcopy(bp->b_un.b_addr, un->un_tmpbuf, (uint_t)bp->b_bcount); 6649 6650 /* 6651 * attempt the write.. 6652 */ 6653 6654 if (st_cmd(dev, (int)SCMD_WRITE, (int)bp->b_bcount, SYNC_CMD) == 0) { 6655 success: 6656 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 6657 "append write succeeded\n"); 6658 bp->b_resid = un->un_sbufp->b_resid; 6659 mutex_exit(ST_MUTEX); 6660 bcount = (unsigned)bp->b_bcount; 6661 biodone(bp); 6662 mutex_enter(ST_MUTEX); 6663 un->un_laststate = un->un_state; 6664 un->un_state = ST_STATE_OPEN; 6665 kmem_free(un->un_tmpbuf, bcount); 6666 un->un_tmpbuf = NULL; 6667 return; 6668 } 6669 6670 /* 6671 * The append failed. Do a short read. If that fails, we are at EOM 6672 * so we can retry the write command. If that succeeds, than we're 6673 * all screwed up (the controller reported a real error). 6674 * 6675 * XXX: should the dummy read be > SECSIZE? should it be the device's 6676 * XXX: block size? 6677 * 6678 */ 6679 status = un->un_status; 6680 un->un_status = 0; 6681 (void) st_cmd(dev, SCMD_READ, SECSIZE, SYNC_CMD); 6682 if (un->un_status == KEY_BLANK_CHECK) { 6683 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 6684 "append at EOM\n"); 6685 /* 6686 * Okay- the read failed. We should actually have confused 6687 * the controller enough to allow writing. In any case, the 6688 * i/o is on its own from here on out. 6689 */ 6690 un->un_laststate = un->un_state; 6691 un->un_state = ST_STATE_OPEN; 6692 bcopy(bp->b_un.b_addr, un->un_tmpbuf, (uint_t)bp->b_bcount); 6693 if (st_cmd(dev, (int)SCMD_WRITE, (int)bp->b_bcount, 6694 SYNC_CMD) == 0) { 6695 goto success; 6696 } 6697 } 6698 6699 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 6700 "append write failed- not at EOM\n"); 6701 bp->b_resid = bp->b_bcount; 6702 st_bioerror(bp, EIO); 6703 6704 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 6705 "st_test_append : EIO : append write failed - not at EOM"); 6706 6707 /* 6708 * backspace one record to get back to where we were 6709 */ 6710 if (st_cmd(dev, SCMD_SPACE, Blk(-1), SYNC_CMD)) { 6711 un->un_fileno = -1; 6712 } 6713 6714 un->un_err_resid = bp->b_resid; 6715 un->un_status = status; 6716 6717 /* 6718 * Note: biodone will do a bp_mapout() 6719 */ 6720 mutex_exit(ST_MUTEX); 6721 bcount = (unsigned)bp->b_bcount; 6722 biodone(bp); 6723 mutex_enter(ST_MUTEX); 6724 un->un_laststate = un->un_state; 6725 un->un_state = ST_STATE_OPEN_PENDING_IO; 6726 kmem_free(un->un_tmpbuf, bcount); 6727 un->un_tmpbuf = NULL; 6728 } 6729 6730 /* 6731 * Special command handler 6732 */ 6733 6734 /* 6735 * common st_cmd code. The fourth parameter states 6736 * whether the caller wishes to await the results 6737 * Note the release of the mutex during most of the function 6738 */ 6739 static int 6740 st_cmd(dev_t dev, int com, int count, int wait) 6741 { 6742 struct buf *bp; 6743 int err; 6744 6745 GET_SOFT_STATE(dev); 6746 6747 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 6748 "st_cmd(dev = 0x%lx, com = 0x%x, count = %x, wait = %d)\n", 6749 dev, com, count, wait); 6750 6751 ASSERT(MUTEX_HELD(&un->un_sd->sd_mutex)); 6752 ASSERT(mutex_owned(ST_MUTEX)); 6753 6754 #ifdef STDEBUG 6755 if (st_debug) 6756 st_debug_cmds(un, com, count, wait); 6757 #endif 6758 6759 /* check to see if this command requires the drive to be reserved */ 6760 err = st_check_cmd_for_need_to_reserve(un, com, count); 6761 6762 if (err) { 6763 return (err); 6764 } 6765 6766 while (un->un_sbuf_busy) 6767 cv_wait(&un->un_sbuf_cv, ST_MUTEX); 6768 un->un_sbuf_busy = 1; 6769 6770 bp = un->un_sbufp; 6771 bzero(bp, sizeof (buf_t)); 6772 6773 bp->b_flags = (wait) ? B_BUSY : B_BUSY|B_ASYNC; 6774 6775 /* 6776 * Set count to the actual size of the data tranfer. 6777 * For commands with no data transfer, set bp->b_bcount 6778 * to the value to be used when constructing the 6779 * cdb in st_make_cmd(). 6780 */ 6781 switch (com) { 6782 case SCMD_READ: 6783 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 6784 "special read %d\n", count); 6785 bp->b_flags |= B_READ; 6786 bp->b_un.b_addr = un->un_tmpbuf; 6787 break; 6788 6789 case SCMD_WRITE: 6790 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 6791 "special write %d\n", count); 6792 bp->b_un.b_addr = un->un_tmpbuf; 6793 break; 6794 6795 case SCMD_WRITE_FILE_MARK: 6796 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 6797 "write %d file marks\n", count); 6798 bp->b_bcount = count; 6799 count = 0; 6800 break; 6801 6802 case SCMD_REWIND: 6803 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "rewind\n"); 6804 bp->b_bcount = 0; 6805 count = 0; 6806 break; 6807 6808 case SCMD_SPACE: 6809 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "space\n"); 6810 bp->b_bcount = count; 6811 count = 0; 6812 break; 6813 6814 case SCMD_RESERVE: 6815 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "reserve"); 6816 bp->b_bcount = 0; 6817 count = 0; 6818 break; 6819 6820 case SCMD_RELEASE: 6821 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "release"); 6822 bp->b_bcount = 0; 6823 count = 0; 6824 break; 6825 6826 case SCMD_LOAD: 6827 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 6828 "%s tape\n", (count) ? "load" : "unload"); 6829 bp->b_bcount = count; 6830 count = 0; 6831 break; 6832 6833 case SCMD_ERASE: 6834 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 6835 "erase tape\n"); 6836 bp->b_bcount = 0; 6837 count = 0; 6838 break; 6839 6840 case SCMD_MODE_SENSE: 6841 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 6842 "mode sense\n"); 6843 bp->b_flags |= B_READ; 6844 bp->b_un.b_addr = (caddr_t)(un->un_mspl); 6845 break; 6846 6847 case SCMD_MODE_SELECT: 6848 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 6849 "mode select\n"); 6850 bp->b_un.b_addr = (caddr_t)(un->un_mspl); 6851 break; 6852 6853 case SCMD_READ_BLKLIM: 6854 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 6855 "read block limits\n"); 6856 bp->b_flags |= B_READ; 6857 bp->b_un.b_addr = (caddr_t)(un->un_rbl); 6858 break; 6859 6860 case SCMD_TEST_UNIT_READY: 6861 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 6862 "test unit ready\n"); 6863 bp->b_bcount = 0; 6864 count = 0; 6865 break; 6866 default: 6867 ST_DEBUG(ST_DEVINFO, st_label, CE_PANIC, 6868 "Unhandled scsi command 0x%x in st_cmd()\n", com); 6869 } 6870 6871 mutex_exit(ST_MUTEX); 6872 6873 if (count > 0) { 6874 /* 6875 * We're going to do actual I/O. 6876 * Set things up for physio. 6877 */ 6878 struct iovec aiov; 6879 struct uio auio; 6880 struct uio *uio = &auio; 6881 6882 bzero(&auio, sizeof (struct uio)); 6883 bzero(&aiov, sizeof (struct iovec)); 6884 aiov.iov_base = bp->b_un.b_addr; 6885 aiov.iov_len = count; 6886 6887 uio->uio_iov = &aiov; 6888 uio->uio_iovcnt = 1; 6889 uio->uio_resid = aiov.iov_len; 6890 uio->uio_segflg = UIO_SYSSPACE; 6891 6892 /* 6893 * Let physio do the rest... 6894 */ 6895 bp->b_forw = (struct buf *)(uintptr_t)com; 6896 bp->b_back = NULL; 6897 err = physio(st_strategy, bp, dev, 6898 (bp->b_flags & B_READ) ? B_READ : B_WRITE, 6899 st_minphys, uio); 6900 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 6901 "st_cmd: physio returns %d\n", err); 6902 } else { 6903 /* 6904 * Mimic physio 6905 */ 6906 bp->b_forw = (struct buf *)(uintptr_t)com; 6907 bp->b_back = NULL; 6908 bp->b_edev = dev; 6909 bp->b_dev = cmpdev(dev); 6910 bp->b_blkno = 0; 6911 bp->b_resid = 0; 6912 (void) st_strategy(bp); 6913 if (!wait) { 6914 /* 6915 * This is an async command- the caller won't wait 6916 * and doesn't care about errors. 6917 */ 6918 mutex_enter(ST_MUTEX); 6919 return (0); 6920 } 6921 6922 /* 6923 * BugTraq #4260046 6924 * ---------------- 6925 * Restore Solaris 2.5.1 behavior, namely call biowait 6926 * unconditionally. The old comment said... 6927 * 6928 * "if strategy was flagged with persistent errors, we would 6929 * have an error here, and the bp would never be sent, so we 6930 * don't want to wait on a bp that was never sent...or hang" 6931 * 6932 * The new rationale, courtesy of Chitrank... 6933 * 6934 * "we should unconditionally biowait() here because 6935 * st_strategy() will do a biodone() in the persistent error 6936 * case and the following biowait() will return immediately. 6937 * If not, in the case of "errors after pkt alloc" in 6938 * st_start(), we will not biowait here which will cause the 6939 * next biowait() to return immediately which will cause 6940 * us to send out the next command. In the case where both of 6941 * these use the sbuf, when the first command completes we'll 6942 * free the packet attached to sbuf and the same pkt will 6943 * get freed again when we complete the second command. 6944 * see esc 518987. BTW, it is necessary to do biodone() in 6945 * st_start() for the pkt alloc failure case because physio() 6946 * does biowait() and will hang if we don't do biodone()" 6947 */ 6948 6949 err = biowait(bp); 6950 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 6951 "st_cmd: biowait returns %d\n", err); 6952 } 6953 mutex_enter(ST_MUTEX); 6954 6955 un->un_sbuf_busy = 0; 6956 cv_signal(&un->un_sbuf_cv); 6957 return (err); 6958 } 6959 6960 static int 6961 st_set_compression(struct scsi_tape *un) 6962 { 6963 int rval; 6964 int turn_compression_on; 6965 minor_t minor; 6966 6967 /* 6968 * Drive either dosn't have compression or it is controlled with 6969 * special density codes. Return ENOTTY so caller 6970 * knows nothing was done. 6971 */ 6972 if ((un->un_dp->options & ST_MODE_SEL_COMP) == 0) { 6973 un->un_comp_page = 0; 6974 return (ENOTTY); 6975 } 6976 6977 /* set compression based on minor node opened */ 6978 minor = MT_DENSITY(un->un_dev); 6979 6980 /* 6981 * If this the compression density or 6982 * the drive has two densities and uses mode select for 6983 * control of compression turn on compression for MT_DENSITY2 6984 * as well. 6985 */ 6986 if ((minor == ST_COMPRESSION_DENSITY) || 6987 (minor == MT_DENSITY(MT_DENSITY2)) && 6988 (un->un_dp->densities[0] == un->un_dp->densities[1]) && 6989 (un->un_dp->densities[2] == un->un_dp->densities[3]) && 6990 (un->un_dp->densities[0] != un->un_dp->densities[2])) { 6991 6992 turn_compression_on = 1; 6993 } else { 6994 turn_compression_on = 0; 6995 } 6996 6997 un->un_mspl->high_bl = (uchar_t)(un->un_bsize >> 16); 6998 un->un_mspl->mid_bl = (uchar_t)(un->un_bsize >> 8); 6999 un->un_mspl->low_bl = (uchar_t)(un->un_bsize); 7000 7001 /* 7002 * Need to determine which page does the device use for compression. 7003 * First try the data compression page. If this fails try the device 7004 * configuration page 7005 */ 7006 7007 if ((un->un_comp_page & ST_DEV_DATACOMP_PAGE) == ST_DEV_DATACOMP_PAGE) { 7008 rval = st_set_datacomp_page(un, turn_compression_on); 7009 if (rval == EALREADY) { 7010 return (rval); 7011 } 7012 if (rval != 0) { 7013 if (un->un_status == KEY_ILLEGAL_REQUEST) { 7014 /* 7015 * This device does not support data 7016 * compression page 7017 */ 7018 un->un_comp_page = ST_DEV_CONFIG_PAGE; 7019 } else if (un->un_state >= ST_STATE_OPEN) { 7020 un->un_fileno = -1; 7021 rval = EIO; 7022 } else { 7023 rval = -1; 7024 } 7025 } else { 7026 un->un_comp_page = ST_DEV_DATACOMP_PAGE; 7027 } 7028 } 7029 7030 if ((un->un_comp_page & ST_DEV_CONFIG_PAGE) == ST_DEV_CONFIG_PAGE) { 7031 rval = st_set_devconfig_page(un, turn_compression_on); 7032 if (rval == EALREADY) { 7033 return (rval); 7034 } 7035 if (rval != 0) { 7036 if (un->un_status == KEY_ILLEGAL_REQUEST) { 7037 /* 7038 * This device does not support 7039 * compression at all advice the 7040 * user and unset ST_MODE_SEL_COMP 7041 */ 7042 un->un_dp->options &= ~ST_MODE_SEL_COMP; 7043 un->un_comp_page = 0; 7044 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 7045 "Device Does Not Support Compression\n"); 7046 } else if (un->un_state >= ST_STATE_OPEN) { 7047 un->un_fileno = -1; 7048 rval = EIO; 7049 } else { 7050 rval = -1; 7051 } 7052 } 7053 } 7054 7055 return (rval); 7056 } 7057 7058 /* 7059 * set or unset compression thru device configuration page. 7060 */ 7061 static int 7062 st_set_devconfig_page(struct scsi_tape *un, int compression_on) 7063 { 7064 unsigned char cflag; 7065 int rval = 0; 7066 7067 ASSERT(mutex_owned(ST_MUTEX)); 7068 7069 /* 7070 * Figure what to set compression flag to. 7071 */ 7072 if (compression_on) { 7073 /* They have selected a compression node */ 7074 if (un->un_dp->type == ST_TYPE_FUJI) { 7075 cflag = 0x84; /* use EDRC */ 7076 } else { 7077 cflag = ST_DEV_CONFIG_DEF_COMP; 7078 } 7079 } else { 7080 cflag = ST_DEV_CONFIG_NO_COMP; 7081 } 7082 7083 /* 7084 * If compression is already set the way it was requested. 7085 * And if this not the first time we has tried. 7086 */ 7087 if ((cflag == un->un_mspl->page.dev.comp_alg) && 7088 (un->un_comp_page == ST_DEV_DATACOMP_PAGE)) { 7089 return (EALREADY); 7090 } 7091 7092 un->un_mspl->page.dev.comp_alg = cflag; 7093 /* 7094 * need to send mode select even if correct compression is 7095 * already set since need to set density code 7096 */ 7097 7098 #ifdef STDEBUG 7099 if (st_debug >= 6) { 7100 st_clean_print(ST_DEVINFO, st_label, SCSI_DEBUG, 7101 "st_set_devconfig_page: sense data for mode select", 7102 (char *)un->un_mspl, sizeof (struct seq_mode)); 7103 } 7104 #endif 7105 rval = st_gen_mode_select(un, un->un_mspl, sizeof (struct seq_mode)); 7106 7107 return (rval); 7108 } 7109 7110 /* 7111 * set/reset compression bit thru data compression page 7112 */ 7113 static int 7114 st_set_datacomp_page(struct scsi_tape *un, int compression_on) 7115 { 7116 int compression_on_already; 7117 int rval = 0; 7118 7119 ASSERT(mutex_owned(ST_MUTEX)); 7120 7121 /* 7122 * If drive is not capable of compression (at this time) 7123 * return EALREADY so caller doesn't think that this page 7124 * is not supported. This check is for drives that can 7125 * disable compression from the front panel or configuration. 7126 * I doubt that a drive that supports this page is not really 7127 * capable of compression. 7128 */ 7129 if (un->un_mspl->page.comp.dcc == 0) { 7130 return (EALREADY); 7131 } 7132 7133 /* See if compression currently turned on */ 7134 if (un->un_mspl->page.comp.dce) { 7135 compression_on_already = 1; 7136 } else { 7137 compression_on_already = 0; 7138 } 7139 7140 /* 7141 * If compression is already set the way it was requested. 7142 * And if this not the first time we has tried. 7143 */ 7144 if ((compression_on == compression_on_already) && 7145 (un->un_comp_page == ST_DEV_DATACOMP_PAGE)) { 7146 return (EALREADY); 7147 } 7148 7149 /* 7150 * if we are already set to the appropriate compression 7151 * mode, don't set it again 7152 */ 7153 if (compression_on) { 7154 /* compression selected */ 7155 un->un_mspl->page.comp.dce = 1; 7156 } else { 7157 un->un_mspl->page.comp.dce = 0; 7158 } 7159 7160 7161 #ifdef STDEBUG 7162 if (st_debug >= 6) { 7163 st_clean_print(ST_DEVINFO, st_label, SCSI_DEBUG, 7164 "st_set_datacomp_page: sense data for mode select", 7165 (char *)un->un_mspl, sizeof (struct seq_mode)); 7166 } 7167 #endif 7168 rval = st_gen_mode_select(un, un->un_mspl, sizeof (struct seq_mode)); 7169 7170 return (rval); 7171 } 7172 7173 static int 7174 st_modesense(struct scsi_tape *un) 7175 { 7176 int rval; 7177 uchar_t page; 7178 7179 page = un->un_comp_page; 7180 7181 switch (page) { 7182 case ST_DEV_DATACOMP_PAGE: 7183 case ST_DEV_CONFIG_PAGE: /* fall through */ 7184 rval = st_gen_mode_sense(un, page, un->un_mspl, 7185 sizeof (struct seq_mode)); 7186 break; 7187 7188 case ST_DEV_DATACOMP_PAGE | ST_DEV_CONFIG_PAGE: 7189 if (un->un_dp->options & ST_MODE_SEL_COMP) { 7190 page = ST_DEV_DATACOMP_PAGE; 7191 rval = st_gen_mode_sense(un, page, un->un_mspl, 7192 sizeof (struct seq_mode)); 7193 if (rval == 0 && un->un_mspl->page_code == page) { 7194 un->un_comp_page = page; 7195 break; 7196 } 7197 page = ST_DEV_CONFIG_PAGE; 7198 rval = st_gen_mode_sense(un, page, un->un_mspl, 7199 sizeof (struct seq_mode)); 7200 if (rval == 0 && un->un_mspl->page_code == page) { 7201 un->un_comp_page = page; 7202 break; 7203 } 7204 un->un_dp->options &= ~ST_MODE_SEL_COMP; 7205 un->un_comp_page = 0; 7206 } else { 7207 un->un_comp_page = 0; 7208 } 7209 7210 default: /* fall through */ 7211 rval = st_cmd(un->un_dev, SCMD_MODE_SENSE, MSIZE, SYNC_CMD); 7212 } 7213 return (rval); 7214 } 7215 7216 static int 7217 st_modeselect(struct scsi_tape *un) 7218 { 7219 int rval = 0; 7220 int ix; 7221 7222 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 7223 "st_modeselect(dev = 0x%lx): density = 0x%x\n", 7224 un->un_dev, un->un_mspl->density); 7225 7226 ASSERT(mutex_owned(ST_MUTEX)); 7227 7228 /* 7229 * The parameter list should be the same for all of the 7230 * cases that follow so set them here 7231 * 7232 * Try mode select first if if fails set fields manually 7233 */ 7234 rval = st_modesense(un); 7235 if (rval != 0) { 7236 ST_DEBUG3(ST_DEVINFO, st_label, CE_WARN, 7237 "st_modeselect: First mode sense failed\n"); 7238 un->un_mspl->bd_len = 8; 7239 un->un_mspl->high_nb = 0; 7240 un->un_mspl->mid_nb = 0; 7241 un->un_mspl->low_nb = 0; 7242 } 7243 un->un_mspl->high_bl = (uchar_t)(un->un_bsize >> 16); 7244 un->un_mspl->mid_bl = (uchar_t)(un->un_bsize >> 8); 7245 un->un_mspl->low_bl = (uchar_t)(un->un_bsize); 7246 7247 7248 /* 7249 * If configured to use a specific density code for a media type. 7250 * curdens is previously set by the minor node opened. 7251 * If the media type doesn't match the minor node we change it so it 7252 * looks like the correct one was opened. 7253 */ 7254 if (un->un_dp->options & ST_KNOWS_MEDIA) { 7255 uchar_t best; 7256 7257 for (best = 0xff, ix = 0; ix < NDENSITIES; ix++) { 7258 if (un->un_mspl->media_type == 7259 un->un_dp->mediatype[ix]) { 7260 best = ix; 7261 /* 7262 * It matches but it might not be the only one. 7263 * Use the highest matching media type but not 7264 * to exceed the density selected by the open. 7265 */ 7266 if (ix < un->un_curdens) { 7267 continue; 7268 } 7269 un->un_curdens = ix; 7270 break; 7271 } 7272 } 7273 /* If a match was found best will not be 0xff any more */ 7274 if (best < NDENSITIES) { 7275 ST_DEBUG3(ST_DEVINFO, st_label, CE_WARN, 7276 "found media 0x%X using density 0x%X\n", 7277 un->un_mspl->media_type, 7278 un->un_dp->densities[best]); 7279 un->un_mspl->density = un->un_dp->densities[best]; 7280 } else { 7281 /* Otherwise set density based on minor node opened */ 7282 un->un_mspl->density = 7283 un->un_dp->densities[un->un_curdens]; 7284 } 7285 } else { 7286 un->un_mspl->density = un->un_dp->densities[un->un_curdens]; 7287 } 7288 7289 if (un->un_dp->options & ST_NOBUF) { 7290 un->un_mspl->bufm = 0; 7291 } else { 7292 un->un_mspl->bufm = 1; 7293 } 7294 7295 rval = st_set_compression(un); 7296 7297 /* 7298 * If st_set_compression returned invalid or already it 7299 * found no need to do the mode select. 7300 * So do it here. 7301 */ 7302 if ((rval == ENOTTY) || (rval == EALREADY)) { 7303 7304 /* Zero non-writeable fields */ 7305 un->un_mspl->data_len = 0; 7306 un->un_mspl->media_type = 0; 7307 un->un_mspl->wp = 0; 7308 7309 /* need to set the density code */ 7310 rval = st_cmd(un->un_dev, SCMD_MODE_SELECT, MSIZE, SYNC_CMD); 7311 if (rval != 0) { 7312 if (un->un_state >= ST_STATE_OPEN) { 7313 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 7314 "unable to set tape mode\n"); 7315 un->un_fileno = -1; 7316 rval = EIO; 7317 } else { 7318 rval = -1; 7319 } 7320 } 7321 } 7322 7323 /* 7324 * The spec recommends to send a mode sense after a mode select 7325 */ 7326 (void) st_modesense(un); 7327 7328 ASSERT(mutex_owned(ST_MUTEX)); 7329 7330 return (rval); 7331 } 7332 7333 /* 7334 * st_gen_mode_sense 7335 * 7336 * generic mode sense.. it allows for any page 7337 */ 7338 static int 7339 st_gen_mode_sense(struct scsi_tape *un, int page, struct seq_mode *page_data, 7340 int page_size) 7341 { 7342 7343 int r; 7344 char cdb[CDB_GROUP0]; 7345 struct uscsi_cmd *com; 7346 7347 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 7348 7349 bzero(cdb, CDB_GROUP0); 7350 cdb[0] = SCMD_MODE_SENSE; 7351 cdb[2] = (char)page; 7352 cdb[4] = (char)page_size; 7353 7354 com->uscsi_cdb = cdb; 7355 com->uscsi_cdblen = CDB_GROUP0; 7356 com->uscsi_bufaddr = (caddr_t)page_data; 7357 com->uscsi_buflen = page_size; 7358 com->uscsi_timeout = un->un_dp->non_motion_timeout; 7359 com->uscsi_flags = USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ; 7360 7361 r = st_ioctl_cmd(un->un_dev, com, FKIOCTL); 7362 kmem_free(com, sizeof (*com)); 7363 return (r); 7364 } 7365 7366 /* 7367 * st_gen_mode_select 7368 * 7369 * generic mode select.. it allows for any page 7370 */ 7371 static int 7372 st_gen_mode_select(struct scsi_tape *un, struct seq_mode *page_data, 7373 int page_size) 7374 { 7375 7376 int r; 7377 char cdb[CDB_GROUP0]; 7378 struct uscsi_cmd *com; 7379 7380 /* Zero non-writeable fields */ 7381 page_data->data_len = 0; 7382 page_data->media_type = 0; 7383 page_data->wp = 0; 7384 7385 /* 7386 * If mode select has any page data, zero the ps (Page Savable) bit. 7387 */ 7388 if (page_size > MSIZE) { 7389 page_data->ps = 0; 7390 } 7391 7392 7393 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 7394 7395 /* 7396 * then, do a mode select to set what ever info 7397 */ 7398 bzero(cdb, CDB_GROUP0); 7399 cdb[0] = SCMD_MODE_SELECT; 7400 cdb[1] = 0x10; /* set PF bit for many third party drives */ 7401 cdb[4] = (char)page_size; 7402 7403 com->uscsi_cdb = cdb; 7404 com->uscsi_cdblen = CDB_GROUP0; 7405 com->uscsi_bufaddr = (caddr_t)page_data; 7406 com->uscsi_buflen = page_size; 7407 com->uscsi_timeout = un->un_dp->non_motion_timeout; 7408 com->uscsi_flags = USCSI_DIAGNOSE | USCSI_SILENT | USCSI_WRITE; 7409 7410 r = st_ioctl_cmd(un->un_dev, com, FKIOCTL); 7411 7412 kmem_free(com, sizeof (*com)); 7413 return (r); 7414 } 7415 7416 /* 7417 * Changes devices blocksize and bsize to requested blocksize nblksz. 7418 * Returns returned value from first failed call or zero on success. 7419 */ 7420 static int 7421 st_change_block_size(dev_t dev, uint32_t nblksz) 7422 { 7423 struct seq_mode *current; 7424 int rval; 7425 uint32_t oldblksz; 7426 7427 GET_SOFT_STATE(dev); 7428 7429 current = kmem_zalloc(MSIZE, KM_SLEEP); 7430 7431 /* Read current settings */ 7432 rval = st_gen_mode_sense(un, 0, current, MSIZE); 7433 if (rval != 0) { 7434 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 7435 "mode sense for change block size failed: rval = %d", rval); 7436 goto finish; 7437 } 7438 7439 /* Figure the current block size */ 7440 oldblksz = 7441 (current->high_bl << 16) | 7442 (current->mid_bl << 8) | 7443 (current->low_bl); 7444 7445 /* If current block size is the same as requested were done */ 7446 if (oldblksz == nblksz) { 7447 un->un_bsize = nblksz; 7448 rval = 0; 7449 goto finish; 7450 } 7451 7452 /* Change to requested block size */ 7453 current->high_bl = (uchar_t)(nblksz >> 16); 7454 current->mid_bl = (uchar_t)(nblksz >> 8); 7455 current->low_bl = (uchar_t)(nblksz); 7456 7457 /* Attempt to change block size */ 7458 rval = st_gen_mode_select(un, current, MSIZE); 7459 if (rval != 0) { 7460 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 7461 "Set new block size failed: rval = %d", rval); 7462 goto finish; 7463 } 7464 7465 /* Read back and verify setting */ 7466 rval = st_modesense(un); 7467 if (rval == 0) { 7468 un->un_bsize = 7469 (un->un_mspl->high_bl << 16) | 7470 (un->un_mspl->mid_bl << 8) | 7471 (un->un_mspl->low_bl); 7472 7473 if (un->un_bsize != nblksz) { 7474 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 7475 "Blocksize set does not equal requested blocksize" 7476 "(read: %u requested: %u)\n", nblksz, un->un_bsize); 7477 rval = EIO; 7478 } 7479 } 7480 finish: 7481 kmem_free(current, MSIZE); 7482 return (rval); 7483 } 7484 7485 7486 static void 7487 st_init(struct scsi_tape *un) 7488 { 7489 ASSERT(mutex_owned(ST_MUTEX)); 7490 7491 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 7492 "st_init(): dev = 0x%lx, will reset fileno, blkno, eof\n", un->un_dev); 7493 7494 un->un_blkno = 0; 7495 un->un_fileno = 0; 7496 un->un_lastop = ST_OP_NIL; 7497 un->un_eof = ST_NO_EOF; 7498 un->un_pwr_mgmt = ST_PWR_NORMAL; 7499 if (st_error_level != SCSI_ERR_ALL) { 7500 if (DEBUGGING) { 7501 st_error_level = SCSI_ERR_ALL; 7502 } else { 7503 st_error_level = SCSI_ERR_RETRYABLE; 7504 } 7505 } 7506 } 7507 7508 7509 static void 7510 st_make_cmd(struct scsi_tape *un, struct buf *bp, int (*func)(caddr_t)) 7511 { 7512 struct scsi_pkt *pkt; 7513 struct uscsi_cmd *ucmd; 7514 int count, tval = 0; 7515 int flags = 0; 7516 uchar_t com; 7517 char fixbit; 7518 7519 ASSERT(mutex_owned(ST_MUTEX)); 7520 7521 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 7522 "st_make_cmd(): dev = 0x%lx\n", un->un_dev); 7523 7524 7525 /* 7526 * fixbit is for setting the Fixed Mode and Suppress Incorrect 7527 * Length Indicator bits on read/write commands, for setting 7528 * the Long bit on erase commands, and for setting the Code 7529 * Field bits on space commands. 7530 * XXX why do we set lastop here? 7531 */ 7532 7533 if (bp != un->un_sbufp) { /* regular raw I/O */ 7534 int stat_size = (un->un_arq_enabled ? 7535 sizeof (struct scsi_arq_status) : 1); 7536 pkt = scsi_init_pkt(ROUTE, NULL, bp, 7537 CDB_GROUP0, stat_size, 0, 0, func, (caddr_t)un); 7538 if (pkt == NULL) { 7539 goto exit; 7540 } 7541 SET_BP_PKT(bp, pkt); 7542 if (un->un_bsize == 0) { 7543 count = bp->b_bcount; 7544 fixbit = 0; 7545 } else { 7546 count = bp->b_bcount / un->un_bsize; 7547 fixbit = 1; 7548 } 7549 if (bp->b_flags & B_READ) { 7550 com = SCMD_READ; 7551 un->un_lastop = ST_OP_READ; 7552 if ((un->un_bsize == 0) && /* Not Fixed Block */ 7553 (un->un_dp->options & ST_READ_IGNORE_ILI)) { 7554 fixbit = 2; 7555 } 7556 } else { 7557 com = SCMD_WRITE; 7558 un->un_lastop = ST_OP_WRITE; 7559 } 7560 7561 tval = un->un_dp->io_timeout; 7562 7563 /* 7564 * For really large xfers, increase timeout 7565 */ 7566 if (bp->b_bcount > (10 * ONE_MEG)) 7567 tval *= bp->b_bcount/(10 * ONE_MEG); 7568 7569 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7570 "%s %ld amt 0x%lx\n", (com == SCMD_WRITE) ? 7571 wr_str: rd_str, un->un_blkno, bp->b_bcount); 7572 7573 } else if ((ucmd = BP_UCMD(bp)) != NULL) { 7574 /* 7575 * uscsi - build command, allocate scsi resources 7576 */ 7577 st_make_uscsi_cmd(un, ucmd, bp, func); 7578 goto exit; 7579 7580 } else { /* special I/O */ 7581 struct buf *allocbp = NULL; 7582 int stat_size = (un->un_arq_enabled ? 7583 sizeof (struct scsi_arq_status) : 1); 7584 7585 7586 com = (uchar_t)(uintptr_t)bp->b_forw; 7587 count = bp->b_bcount; 7588 7589 switch (com) { 7590 case SCMD_READ: 7591 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7592 "special read %d\n", count); 7593 if (un->un_bsize == 0) { 7594 fixbit = 2; /* suppress SILI */ 7595 } else { 7596 fixbit = 1; /* Fixed Block Mode */ 7597 count /= un->un_bsize; 7598 } 7599 allocbp = bp; 7600 un->un_lastop = ST_OP_READ; 7601 tval = un->un_dp->io_timeout; 7602 break; 7603 7604 case SCMD_WRITE: 7605 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7606 "special write %d\n", count); 7607 if (un->un_bsize != 0) { 7608 fixbit = 1; /* Fixed Block Mode */ 7609 count /= un->un_bsize; 7610 } else { 7611 fixbit = 0; 7612 } 7613 allocbp = bp; 7614 un->un_lastop = ST_OP_WRITE; 7615 tval = un->un_dp->io_timeout; 7616 break; 7617 7618 case SCMD_WRITE_FILE_MARK: 7619 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7620 "write %d file marks\n", count); 7621 un->un_lastop = ST_OP_WEOF; 7622 fixbit = 0; 7623 tval = un->un_dp->io_timeout; 7624 break; 7625 7626 case SCMD_REWIND: 7627 if (bp->b_flags & B_ASYNC) { 7628 fixbit = 1; 7629 } else { 7630 fixbit = 0; 7631 } 7632 count = 0; 7633 un->un_lastop = ST_OP_CTL; 7634 tval = un->un_dp->rewind_timeout; 7635 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7636 "rewind\n"); 7637 break; 7638 7639 case SCMD_SPACE: 7640 fixbit = Isfmk(count); 7641 count = (int)space_cnt(count); 7642 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7643 "space %d %s from file %d blk %ld\n", 7644 count, (fixbit) ? "filemarks" : "records", 7645 un->un_fileno, un->un_blkno); 7646 un->un_lastop = ST_OP_CTL; 7647 tval = un->un_dp->space_timeout; 7648 break; 7649 7650 case SCMD_LOAD: 7651 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7652 "%s tape\n", (count & 1) ? "load" : "unload"); 7653 fixbit = 0; 7654 7655 /* Loading or Unloading */ 7656 if (count & 1) { 7657 tval = un->un_dp->load_timeout; 7658 } else { 7659 tval = un->un_dp->unload_timeout; 7660 } 7661 /* Is Retension requested */ 7662 if (count & 2) { 7663 tval += un->un_dp->rewind_timeout; 7664 } 7665 un->un_lastop = ST_OP_CTL; 7666 break; 7667 7668 case SCMD_ERASE: 7669 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7670 "erase tape\n"); 7671 count = 0; 7672 /* 7673 * We support long erase only 7674 */ 7675 fixbit = 1; 7676 tval = un->un_dp->erase_timeout; 7677 un->un_lastop = ST_OP_CTL; 7678 break; 7679 7680 case SCMD_MODE_SENSE: 7681 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7682 "mode sense\n"); 7683 allocbp = bp; 7684 fixbit = 0; 7685 tval = un->un_dp->non_motion_timeout; 7686 break; 7687 7688 case SCMD_MODE_SELECT: 7689 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7690 "mode select\n"); 7691 allocbp = bp; 7692 fixbit = 0; 7693 tval = un->un_dp->non_motion_timeout; 7694 break; 7695 7696 case SCMD_RESERVE: 7697 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7698 "reserve\n"); 7699 fixbit = 0; 7700 tval = un->un_dp->non_motion_timeout; 7701 break; 7702 7703 case SCMD_RELEASE: 7704 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7705 "release\n"); 7706 fixbit = 0; 7707 tval = un->un_dp->non_motion_timeout; 7708 break; 7709 7710 case SCMD_READ_BLKLIM: 7711 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7712 "read block limits\n"); 7713 allocbp = bp; 7714 fixbit = count = 0; 7715 tval = un->un_dp->non_motion_timeout; 7716 break; 7717 7718 case SCMD_TEST_UNIT_READY: 7719 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7720 "test unit ready\n"); 7721 fixbit = 0; 7722 tval = un->un_dp->non_motion_timeout; 7723 break; 7724 default: 7725 ST_DEBUG(ST_DEVINFO, st_label, CE_PANIC, 7726 "Unhandled scsi command 0x%x in st_make_cmd()\n", 7727 com); 7728 } 7729 pkt = scsi_init_pkt(ROUTE, NULL, allocbp, 7730 CDB_GROUP0, stat_size, 0, 0, func, (caddr_t)un); 7731 if (pkt == NULL) { 7732 goto exit; 7733 } 7734 if (allocbp) 7735 ASSERT(geterror(allocbp) == 0); 7736 7737 } 7738 7739 (void) scsi_setup_cdb((union scsi_cdb *)pkt->pkt_cdbp, 7740 com, 0, (uint_t)count, 0); 7741 FILL_SCSI1_LUN(un->un_sd, pkt); 7742 /* 7743 * Initialize the SILI/Fixed bits of the byte 1 of cdb. 7744 */ 7745 ((union scsi_cdb *)(pkt->pkt_cdbp))->t_code = fixbit; 7746 pkt->pkt_flags = flags; 7747 7748 /* 7749 * If ST_SHORT_FILEMARKS bit is ON for EXABYTE 7750 * device, set the Vendor Unique bit to 7751 * write Short File Mark. 7752 */ 7753 if (com == SCMD_WRITE_FILE_MARK && 7754 un->un_dp->options & ST_SHORT_FILEMARKS) { 7755 switch (un->un_dp->type) { 7756 case ST_TYPE_EXB8500: 7757 case ST_TYPE_EXABYTE: 7758 /* 7759 * Now the Vendor Unique bit 7 in Byte 5 of CDB 7760 * is set to to write Short File Mark 7761 */ 7762 ((union scsi_cdb *)pkt->pkt_cdbp)->g0_vu_1 = 1; 7763 break; 7764 7765 default: 7766 /* 7767 * Well, if ST_SHORT_FILEMARKS is set for other 7768 * tape drives, it is just ignored 7769 */ 7770 break; 7771 } 7772 } 7773 ASSERT(tval); 7774 pkt->pkt_time = tval; 7775 pkt->pkt_comp = st_intr; 7776 pkt->pkt_private = (opaque_t)bp; 7777 SET_BP_PKT(bp, pkt); 7778 7779 exit: 7780 ASSERT(mutex_owned(ST_MUTEX)); 7781 } 7782 7783 7784 /* 7785 * Build a command based on a uscsi command; 7786 */ 7787 static void 7788 st_make_uscsi_cmd(struct scsi_tape *un, struct uscsi_cmd *ucmd, 7789 struct buf *bp, int (*func)(caddr_t)) 7790 { 7791 struct scsi_pkt *pkt; 7792 caddr_t cdb; 7793 int cdblen; 7794 int stat_size; 7795 7796 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 7797 "st_make_uscsi_cmd(): dev = 0x%lx\n", un->un_dev); 7798 7799 if (ucmd->uscsi_flags & USCSI_RQENABLE) { 7800 stat_size = (un->un_arq_enabled ? 7801 sizeof (struct scsi_arq_status) : 1); 7802 } else { 7803 stat_size = 1; 7804 } 7805 7806 ASSERT(mutex_owned(ST_MUTEX)); 7807 7808 un->un_lastop = ST_OP_CTL; /* usual */ 7809 7810 cdb = ucmd->uscsi_cdb; 7811 cdblen = ucmd->uscsi_cdblen; 7812 7813 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7814 "st_make_uscsi_cmd: buflen=%ld bcount=%ld\n", 7815 ucmd->uscsi_buflen, bp->b_bcount); 7816 pkt = scsi_init_pkt(ROUTE, NULL, 7817 (bp->b_bcount > 0) ? bp : NULL, 7818 cdblen, stat_size, 0, 0, func, (caddr_t)un); 7819 if (pkt == NULL) { 7820 goto exit; 7821 } 7822 7823 bcopy(cdb, pkt->pkt_cdbp, (uint_t)cdblen); 7824 7825 #ifdef STDEBUG 7826 if (st_debug >= 6) { 7827 st_clean_print(ST_DEVINFO, st_label, SCSI_DEBUG, 7828 "pkt_cdbp", (char *)cdb, cdblen); 7829 } 7830 #endif 7831 7832 if (ucmd->uscsi_flags & USCSI_SILENT) { 7833 pkt->pkt_flags |= FLAG_SILENT; 7834 } 7835 7836 pkt->pkt_time = ucmd->uscsi_timeout; 7837 pkt->pkt_comp = st_intr; 7838 pkt->pkt_private = (opaque_t)bp; 7839 SET_BP_PKT(bp, pkt); 7840 exit: 7841 ASSERT(mutex_owned(ST_MUTEX)); 7842 } 7843 7844 7845 /* 7846 * restart cmd currently at the head of the runq 7847 * 7848 * If scsi_transport() succeeds or the retries 7849 * count exhausted, restore the throttle that was 7850 * zeroed out in st_handle_intr_busy(). 7851 * 7852 */ 7853 static void 7854 st_intr_restart(void *arg) 7855 { 7856 struct scsi_tape *un = arg; 7857 struct buf *bp; 7858 int status = TRAN_ACCEPT; 7859 7860 mutex_enter(ST_MUTEX); 7861 7862 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 7863 "st_intr_restart(), un = 0x%p\n", (void *)un); 7864 7865 un->un_hib_tid = 0; 7866 7867 /* 7868 * move from waitq to runq, if there is anything on the waitq 7869 */ 7870 if ((bp = un->un_quef) == NULL) { 7871 mutex_exit(ST_MUTEX); 7872 return; 7873 } 7874 7875 /* 7876 * Here we know : 7877 * throttle = 0, via st_handle_intr_busy 7878 */ 7879 7880 if (un->un_quel == bp) { 7881 un->un_quel = NULL; 7882 un->un_quef = NULL; /* we know it's the first one */ 7883 } else { 7884 un->un_quef = bp->b_actf; 7885 } 7886 bp->b_actf = NULL; 7887 7888 if (un->un_runqf) { 7889 /* 7890 * not good, we don't want to requeue something after 7891 * another. 7892 */ 7893 mutex_exit(ST_MUTEX); 7894 goto done_error; 7895 } else { 7896 un->un_runqf = bp; 7897 un->un_runql = bp; 7898 } 7899 7900 ST_DO_KSTATS(bp, kstat_waitq_to_runq); 7901 7902 mutex_exit(ST_MUTEX); 7903 7904 status = scsi_transport(BP_PKT(bp)); 7905 7906 mutex_enter(ST_MUTEX); 7907 7908 if (status != TRAN_ACCEPT) { 7909 ST_DO_KSTATS(bp, kstat_runq_back_to_waitq); 7910 mutex_exit(ST_MUTEX); 7911 7912 if (status == TRAN_BUSY) { 7913 if (st_handle_intr_busy(un, bp, 7914 ST_TRAN_BUSY_TIMEOUT) == 0) 7915 return; /* timeout is setup again */ 7916 } 7917 7918 } else { 7919 un->un_tran_retry_ct = 0; 7920 if (un->un_last_throttle) { 7921 un->un_throttle = un->un_last_throttle; 7922 } 7923 mutex_exit(ST_MUTEX); 7924 return; 7925 } 7926 7927 done_error: 7928 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 7929 "restart transport rejected\n"); 7930 bp->b_resid = bp->b_bcount; 7931 7932 #ifndef __lock_lint 7933 /* 7934 * warlock doesn't understand this potential 7935 * recursion? 7936 */ 7937 mutex_enter(ST_MUTEX); 7938 if (un->un_last_throttle) { 7939 un->un_throttle = un->un_last_throttle; 7940 } 7941 if (status != TRAN_ACCEPT) 7942 ST_DO_ERRSTATS(un, st_transerrs); 7943 ST_DO_KSTATS(bp, kstat_waitq_exit); 7944 SET_PE_FLAG(un); 7945 st_bioerror(bp, EIO); 7946 st_done_and_mutex_exit(un, bp); 7947 #endif 7948 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 7949 "busy restart aborted\n"); 7950 } 7951 7952 /* 7953 * st_check_media(): 7954 * Periodically check the media state using scsi_watch service; 7955 * this service calls back after TUR and possibly request sense 7956 * the callback handler (st_media_watch_cb()) decodes the request sense 7957 * data (if any) 7958 */ 7959 7960 static int 7961 st_check_media(dev_t dev, enum mtio_state state) 7962 { 7963 int rval = 0; 7964 enum mtio_state prev_state; 7965 opaque_t token = NULL; 7966 7967 GET_SOFT_STATE(dev); 7968 7969 mutex_enter(ST_MUTEX); 7970 7971 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 7972 "st_check_media:state=%x, mediastate=%x\n", 7973 state, un->un_mediastate); 7974 7975 prev_state = un->un_mediastate; 7976 7977 /* 7978 * is there anything to do? 7979 */ 7980 retry: 7981 if (state == un->un_mediastate || un->un_mediastate == MTIO_NONE) { 7982 /* 7983 * submit the request to the scsi_watch service; 7984 * scsi_media_watch_cb() does the real work 7985 */ 7986 mutex_exit(ST_MUTEX); 7987 token = scsi_watch_request_submit(ST_SCSI_DEVP, 7988 st_check_media_time, SENSE_LENGTH, 7989 st_media_watch_cb, (caddr_t)dev); 7990 if (token == NULL) { 7991 rval = EAGAIN; 7992 goto done; 7993 } 7994 mutex_enter(ST_MUTEX); 7995 7996 un->un_swr_token = token; 7997 un->un_specified_mediastate = state; 7998 7999 /* 8000 * now wait for media change 8001 * we will not be signalled unless mediastate == state but it 8002 * still better to test for this condition, since there 8003 * is a 5 sec cv_broadcast delay when 8004 * mediastate == MTIO_INSERTED 8005 */ 8006 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 8007 "st_check_media:waiting for media state change\n"); 8008 while (un->un_mediastate == state) { 8009 if (cv_wait_sig(&un->un_state_cv, ST_MUTEX) == 0) { 8010 mutex_exit(ST_MUTEX); 8011 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 8012 "st_check_media:waiting for media state " 8013 "was interrupted\n"); 8014 rval = EINTR; 8015 goto done; 8016 } 8017 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 8018 "st_check_media:received signal, state=%x\n", 8019 un->un_mediastate); 8020 } 8021 } 8022 8023 /* 8024 * if we transitioned to MTIO_INSERTED, media has really been 8025 * inserted. If TUR fails, it is probably a exabyte slow spin up. 8026 * Reset and retry the state change. If everything is ok, replay 8027 * the open() logic. 8028 */ 8029 if ((un->un_mediastate == MTIO_INSERTED) && 8030 (un->un_state == ST_STATE_OFFLINE)) { 8031 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 8032 "st_check_media: calling st_cmd to confirm inserted\n"); 8033 8034 /* 8035 * set this early so that TUR will make it through strategy 8036 * without triggering a st_tape_init(). We needed it set 8037 * before calling st_tape_init() ourselves anyway. If TUR 8038 * fails, set it back 8039 */ 8040 un->un_state = ST_STATE_INITIALIZING; 8041 8042 /* 8043 * If not reserved fail as getting reservation conflict 8044 * will make this hang forever. 8045 */ 8046 if ((un->un_rsvd_status & 8047 (ST_RESERVE | ST_APPLICATION_RESERVATIONS)) == 0) { 8048 mutex_exit(ST_MUTEX); 8049 rval = EACCES; 8050 goto done; 8051 } 8052 rval = st_cmd(dev, SCMD_TEST_UNIT_READY, 0, SYNC_CMD); 8053 if (rval == EACCES) { 8054 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 8055 "st_check_media: TUR got Reservation Conflict\n"); 8056 mutex_exit(ST_MUTEX); 8057 goto done; 8058 } 8059 if (rval) { 8060 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 8061 "st_check_media: TUR failed, going to retry\n"); 8062 un->un_mediastate = prev_state; 8063 un->un_state = ST_STATE_OFFLINE; 8064 goto retry; 8065 } 8066 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 8067 "st_check_media: media inserted\n"); 8068 8069 /* this also rewinds the tape */ 8070 rval = st_tape_init(dev); 8071 if (rval != 0) { 8072 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 8073 "st_check_media : OFFLINE init failure "); 8074 un->un_state = ST_STATE_OFFLINE; 8075 un->un_fileno = -1; 8076 } else { 8077 un->un_state = ST_STATE_OPEN_PENDING_IO; 8078 un->un_fileno = 0; 8079 un->un_blkno = 0; 8080 } 8081 } else if ((un->un_mediastate == MTIO_EJECTED) && 8082 (un->un_state != ST_STATE_OFFLINE)) { 8083 /* 8084 * supported devices must be rewound before ejection 8085 * rewind resets fileno & blkno 8086 */ 8087 un->un_laststate = un->un_state; 8088 un->un_state = ST_STATE_OFFLINE; 8089 } 8090 mutex_exit(ST_MUTEX); 8091 done: 8092 if (token) { 8093 (void) scsi_watch_request_terminate(token, 8094 SCSI_WATCH_TERMINATE_WAIT); 8095 mutex_enter(ST_MUTEX); 8096 un->un_swr_token = (opaque_t)NULL; 8097 mutex_exit(ST_MUTEX); 8098 } 8099 8100 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, "st_check_media: done\n"); 8101 8102 return (rval); 8103 } 8104 8105 /* 8106 * st_media_watch_cb() is called by scsi_watch_thread for 8107 * verifying the request sense data (if any) 8108 */ 8109 static int 8110 st_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp) 8111 { 8112 struct scsi_status *statusp = resultp->statusp; 8113 struct scsi_extended_sense *sensep = resultp->sensep; 8114 uchar_t actual_sense_length = resultp->actual_sense_length; 8115 struct scsi_tape *un; 8116 enum mtio_state state = MTIO_NONE; 8117 int instance; 8118 dev_t dev = (dev_t)arg; 8119 8120 instance = MTUNIT(dev); 8121 if ((un = ddi_get_soft_state(st_state, instance)) == NULL) { 8122 return (-1); 8123 } 8124 8125 mutex_enter(ST_MUTEX); 8126 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 8127 "st_media_watch_cb: status=%x, sensep=%p, len=%x\n", 8128 *((char *)statusp), (void *)sensep, 8129 actual_sense_length); 8130 8131 /* 8132 * if there was a check condition then sensep points to valid 8133 * sense data 8134 * if status was not a check condition but a reservation or busy 8135 * status then the new state is MTIO_NONE 8136 */ 8137 if (sensep) { 8138 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 8139 "st_media_watch_cb: KEY=%x, ASC=%x, ASCQ=%x\n", 8140 sensep->es_key, sensep->es_add_code, sensep->es_qual_code); 8141 8142 switch (un->un_dp->type) { 8143 default: 8144 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 8145 "st_media_watch_cb: unknown drive type %d, default to ST_TYPE_HP\n", 8146 un->un_dp->type); 8147 /* FALLTHROUGH */ 8148 8149 case ST_TYPE_STC3490: /* STK 4220 1/2" cartridge */ 8150 case ST_TYPE_FUJI: /* 1/2" cartridge */ 8151 case ST_TYPE_HP: /* HP 88780 1/2" reel */ 8152 if (un->un_dp->type == ST_TYPE_FUJI) { 8153 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 8154 "st_media_watch_cb: ST_TYPE_FUJI\n"); 8155 } else { 8156 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 8157 "st_media_watch_cb: ST_TYPE_HP\n"); 8158 } 8159 switch (sensep->es_key) { 8160 case KEY_UNIT_ATTENTION: 8161 /* not ready to ready transition */ 8162 /* hp/es_qual_code == 80 on>off>on */ 8163 /* hp/es_qual_code == 0 on>off>unld>ld>on */ 8164 if (sensep->es_add_code == 0x28) { 8165 state = MTIO_INSERTED; 8166 } 8167 break; 8168 case KEY_NOT_READY: 8169 /* in process, rewinding or loading */ 8170 if ((sensep->es_add_code == 0x04) && 8171 (sensep->es_qual_code == 0x00)) { 8172 state = MTIO_EJECTED; 8173 } 8174 break; 8175 } 8176 break; 8177 8178 case ST_TYPE_EXB8500: /* Exabyte 8500 */ 8179 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 8180 "st_media_watch_cb: ST_TYPE_EXB8500\n"); 8181 switch (sensep->es_key) { 8182 case KEY_UNIT_ATTENTION: 8183 /* operator medium removal request */ 8184 if ((sensep->es_add_code == 0x5a) && 8185 (sensep->es_qual_code == 0x01)) { 8186 state = MTIO_EJECTED; 8187 /* not ready to ready transition */ 8188 } else if ((sensep->es_add_code == 0x28) && 8189 (sensep->es_qual_code == 0x00)) { 8190 state = MTIO_INSERTED; 8191 } 8192 break; 8193 case KEY_NOT_READY: 8194 /* medium not present */ 8195 if (sensep->es_add_code == 0x3a) { 8196 state = MTIO_EJECTED; 8197 } 8198 break; 8199 } 8200 break; 8201 case ST_TYPE_EXABYTE: /* Exabyte 8200 */ 8202 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 8203 "st_media_watch_cb: ST_TYPE_EXABYTE\n"); 8204 switch (sensep->es_key) { 8205 case KEY_NOT_READY: 8206 if ((sensep->es_add_code == 0x04) && 8207 (sensep->es_qual_code == 0x00)) { 8208 /* volume not mounted? */ 8209 state = MTIO_EJECTED; 8210 } else if (sensep->es_add_code == 0x3a) { 8211 state = MTIO_EJECTED; 8212 } 8213 break; 8214 case KEY_UNIT_ATTENTION: 8215 state = MTIO_EJECTED; 8216 break; 8217 } 8218 break; 8219 8220 case ST_TYPE_DLT: /* quantum DLT4xxx */ 8221 switch (sensep->es_key) { 8222 case KEY_UNIT_ATTENTION: 8223 if (sensep->es_add_code == 0x28) { 8224 state = MTIO_INSERTED; 8225 } 8226 break; 8227 case KEY_NOT_READY: 8228 if (sensep->es_add_code == 0x04) { 8229 /* in transition but could be either */ 8230 state = un->un_specified_mediastate; 8231 } else if ((sensep->es_add_code == 0x3a) && 8232 (sensep->es_qual_code == 0x00)) { 8233 state = MTIO_EJECTED; 8234 } 8235 break; 8236 } 8237 break; 8238 } 8239 } else if (*((char *)statusp) == STATUS_GOOD) { 8240 state = MTIO_INSERTED; 8241 } 8242 8243 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 8244 "st_media_watch_cb:state=%x, specified=%x\n", 8245 state, un->un_specified_mediastate); 8246 8247 /* 8248 * now signal the waiting thread if this is *not* the specified state; 8249 * delay the signal if the state is MTIO_INSERTED 8250 * to allow the target to recover 8251 */ 8252 if (state != un->un_specified_mediastate) { 8253 un->un_mediastate = state; 8254 if (state == MTIO_INSERTED) { 8255 /* 8256 * delay the signal to give the drive a chance 8257 * to do what it apparently needs to do 8258 */ 8259 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 8260 "st_media_watch_cb:delayed cv_broadcast\n"); 8261 un->un_delay_tid = timeout(st_delayed_cv_broadcast, 8262 un, drv_usectohz((clock_t)MEDIA_ACCESS_DELAY)); 8263 } else { 8264 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 8265 "st_media_watch_cb:immediate cv_broadcast\n"); 8266 cv_broadcast(&un->un_state_cv); 8267 } 8268 } 8269 mutex_exit(ST_MUTEX); 8270 return (0); 8271 } 8272 8273 /* 8274 * delayed cv_broadcast to allow for target to recover 8275 * from media insertion 8276 */ 8277 static void 8278 st_delayed_cv_broadcast(void *arg) 8279 { 8280 struct scsi_tape *un = arg; 8281 8282 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 8283 "st_delayed_cv_broadcast:delayed cv_broadcast\n"); 8284 8285 mutex_enter(ST_MUTEX); 8286 cv_broadcast(&un->un_state_cv); 8287 mutex_exit(ST_MUTEX); 8288 } 8289 8290 /* 8291 * restart cmd currently at the start of the waitq 8292 */ 8293 static void 8294 st_start_restart(void *arg) 8295 { 8296 struct scsi_tape *un = arg; 8297 8298 ASSERT(un != NULL); 8299 8300 mutex_enter(ST_MUTEX); 8301 8302 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 8303 "st_tran_restart()\n"); 8304 8305 if (un->un_quef) { 8306 st_start(un); 8307 } 8308 8309 mutex_exit(ST_MUTEX); 8310 } 8311 8312 8313 /* 8314 * Command completion processing 8315 * 8316 */ 8317 static void 8318 st_intr(struct scsi_pkt *pkt) 8319 { 8320 struct scsi_tape *un; 8321 struct buf *last_runqf; 8322 struct buf *bp; 8323 int action = COMMAND_DONE; 8324 clock_t timout; 8325 int status; 8326 8327 bp = (struct buf *)pkt->pkt_private; 8328 un = ddi_get_soft_state(st_state, MTUNIT(bp->b_edev)); 8329 8330 mutex_enter(ST_MUTEX); 8331 8332 un->un_rqs_state &= ~(ST_RQS_ERROR); 8333 8334 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, "st_intr()\n"); 8335 8336 if (pkt->pkt_reason != CMD_CMPLT) { 8337 8338 /* If device has gone away not much else to do */ 8339 if (pkt->pkt_reason == CMD_DEV_GONE) { 8340 action = COMMAND_DONE_ERROR; 8341 } else if (un->un_state == ST_STATE_SENSING) { 8342 ST_DO_ERRSTATS(un, st_transerrs); 8343 action = COMMAND_DONE_ERROR; 8344 } else { 8345 action = st_handle_incomplete(un, bp); 8346 } 8347 /* 8348 * At this point we know that the command was successfully 8349 * completed. Now what? 8350 */ 8351 } else if (un->un_arq_enabled && 8352 (pkt->pkt_state & STATE_ARQ_DONE)) { 8353 /* 8354 * the transport layer successfully completed an autorqsense 8355 */ 8356 action = st_handle_autosense(un, bp); 8357 8358 } else if (un->un_state == ST_STATE_SENSING) { 8359 /* 8360 * okay. We were running a REQUEST SENSE. Find 8361 * out what to do next. 8362 * some actions are based on un_state, hence 8363 * restore the state st was in before ST_STATE_SENSING. 8364 */ 8365 un->un_state = un->un_laststate; 8366 action = st_handle_sense(un, bp); 8367 /* 8368 * set pkt back to original packet in case we will have 8369 * to requeue it 8370 */ 8371 pkt = BP_PKT(bp); 8372 } else if ((SCBP(pkt)->sts_busy) || (SCBP(pkt)->sts_chk)) { 8373 /* 8374 * Okay, we weren't running a REQUEST SENSE. Call a routine 8375 * to see if the status bits we're okay. If a request sense 8376 * is to be run, that will happen. 8377 */ 8378 action = st_check_error(un, pkt); 8379 } 8380 8381 if (un->un_pwr_mgmt == ST_PWR_SUSPENDED) { 8382 switch (action) { 8383 case QUE_COMMAND: 8384 /* 8385 * return cmd to head to the queue 8386 * since we are suspending so that 8387 * it gets restarted during resume 8388 */ 8389 if (un->un_runqf) { 8390 last_runqf = un->un_runqf; 8391 un->un_runqf = bp; 8392 bp->b_actf = last_runqf; 8393 } else { 8394 bp->b_actf = NULL; 8395 un->un_runqf = bp; 8396 un->un_runql = bp; 8397 } 8398 action = JUST_RETURN; 8399 break; 8400 8401 case QUE_SENSE: 8402 action = COMMAND_DONE_ERROR; 8403 break; 8404 8405 default: 8406 break; 8407 } 8408 } 8409 8410 /* 8411 * Restore old state if we were sensing. 8412 */ 8413 if (un->un_state == ST_STATE_SENSING && action != QUE_SENSE) { 8414 un->un_state = un->un_laststate; 8415 } 8416 8417 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 8418 "st_intr: pkt=%p, bp=%p, action=%x, status=%x\n", 8419 (void *)pkt, (void *)bp, action, SCBP_C(pkt)); 8420 8421 8422 switch (action) { 8423 case COMMAND_DONE_EACCES: 8424 /* this is to report a reservation conflict */ 8425 st_bioerror(bp, EACCES); 8426 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 8427 "Reservation Conflict \n"); 8428 8429 /*FALLTHROUGH*/ 8430 case COMMAND_DONE_ERROR: 8431 if (un->un_eof < ST_EOT_PENDING && 8432 un->un_state >= ST_STATE_OPEN) { 8433 /* 8434 * all errors set state of the tape to 'unknown' 8435 * unless we're at EOT or are doing append testing. 8436 * If sense key was illegal request, preserve state. 8437 */ 8438 if (un->un_status != KEY_ILLEGAL_REQUEST) { 8439 un->un_fileno = -1; 8440 } 8441 } 8442 un->un_err_resid = bp->b_resid = bp->b_bcount; 8443 /* 8444 * since we have an error (COMMAND_DONE_ERROR), we want to 8445 * make sure an error ocurrs, so make sure at least EIO is 8446 * returned 8447 */ 8448 if (geterror(bp) == 0) 8449 st_bioerror(bp, EIO); 8450 8451 SET_PE_FLAG(un); 8452 if (!(un->un_rqs_state & ST_RQS_ERROR) && 8453 (un->un_errno == EIO)) { 8454 un->un_rqs_state &= ~(ST_RQS_VALID); 8455 } 8456 goto done; 8457 8458 case COMMAND_DONE_ERROR_RECOVERED: 8459 un->un_err_resid = bp->b_resid = bp->b_bcount; 8460 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 8461 "st_intr(): COMMAND_DONE_ERROR_RECOVERED"); 8462 if (geterror(bp) == 0) 8463 st_bioerror(bp, EIO); 8464 SET_PE_FLAG(un); 8465 if (!(un->un_rqs_state & ST_RQS_ERROR) && 8466 (un->un_errno == EIO)) { 8467 un->un_rqs_state &= ~(ST_RQS_VALID); 8468 } 8469 /*FALLTHROUGH*/ 8470 case COMMAND_DONE: 8471 st_set_state(un); 8472 done: 8473 ST_DO_KSTATS(bp, kstat_runq_exit); 8474 st_done_and_mutex_exit(un, bp); 8475 return; 8476 8477 case QUE_SENSE: 8478 if ((un->un_ncmds > 1) && !un->un_flush_on_errors) 8479 goto sense_error; 8480 8481 if (un->un_state != ST_STATE_SENSING) { 8482 un->un_laststate = un->un_state; 8483 un->un_state = ST_STATE_SENSING; 8484 } 8485 8486 un->un_rqs->pkt_private = (opaque_t)bp; 8487 bzero(ST_RQSENSE, SENSE_LENGTH); 8488 8489 if (un->un_throttle) { 8490 un->un_last_throttle = un->un_throttle; 8491 un->un_throttle = 0; 8492 } 8493 8494 mutex_exit(ST_MUTEX); 8495 8496 /* 8497 * never retry this, some other command will have nuked the 8498 * sense, anyway 8499 */ 8500 status = scsi_transport(un->un_rqs); 8501 8502 mutex_enter(ST_MUTEX); 8503 8504 if (un->un_last_throttle) { 8505 un->un_throttle = un->un_last_throttle; 8506 } 8507 8508 if (status == TRAN_ACCEPT) { 8509 mutex_exit(ST_MUTEX); 8510 return; 8511 } 8512 if (status != TRAN_BUSY) 8513 ST_DO_ERRSTATS(un, st_transerrs); 8514 sense_error: 8515 un->un_fileno = -1; 8516 st_bioerror(bp, EIO); 8517 SET_PE_FLAG(un); 8518 goto done; 8519 8520 case QUE_BUSY_COMMAND: 8521 /* longish timeout */ 8522 timout = ST_STATUS_BUSY_TIMEOUT; 8523 goto que_it_up; 8524 8525 case QUE_COMMAND: 8526 /* short timeout */ 8527 timout = ST_TRAN_BUSY_TIMEOUT; 8528 que_it_up: 8529 /* 8530 * let st_handle_intr_busy put this bp back on waitq and make 8531 * checks to see if it is ok to requeue the command. 8532 */ 8533 ST_DO_KSTATS(bp, kstat_runq_back_to_waitq); 8534 8535 /* 8536 * Save the throttle before setting up the timeout 8537 */ 8538 if (un->un_throttle) { 8539 un->un_last_throttle = un->un_throttle; 8540 } 8541 mutex_exit(ST_MUTEX); 8542 if (st_handle_intr_busy(un, bp, timout) == 0) 8543 return; /* timeout is setup again */ 8544 8545 mutex_enter(ST_MUTEX); 8546 un->un_fileno = -1; 8547 un->un_err_resid = bp->b_resid = bp->b_bcount; 8548 st_bioerror(bp, EIO); 8549 SET_PE_FLAG(un); 8550 goto done; 8551 8552 case QUE_LAST_COMMAND: 8553 8554 if ((un->un_ncmds > 1) && !un->un_flush_on_errors) { 8555 scsi_log(ST_DEVINFO, st_label, CE_CONT, 8556 "un_ncmds: %d can't retry cmd \n", un->un_ncmds); 8557 goto last_command_error; 8558 } 8559 mutex_exit(ST_MUTEX); 8560 if (st_handle_intr_retry_lcmd(un, bp) == 0) 8561 return; 8562 mutex_enter(ST_MUTEX); 8563 last_command_error: 8564 un->un_err_resid = bp->b_resid = bp->b_bcount; 8565 un->un_fileno = -1; 8566 st_bioerror(bp, EIO); 8567 SET_PE_FLAG(un); 8568 goto done; 8569 8570 case JUST_RETURN: 8571 default: 8572 ST_DO_KSTATS(bp, kstat_runq_back_to_waitq); 8573 mutex_exit(ST_MUTEX); 8574 return; 8575 } 8576 /*NOTREACHED*/ 8577 } 8578 8579 static int 8580 st_handle_incomplete(struct scsi_tape *un, struct buf *bp) 8581 { 8582 static char *fail = "SCSI transport failed: reason '%s': %s\n"; 8583 int rval = COMMAND_DONE_ERROR; 8584 struct scsi_pkt *pkt = (un->un_state == ST_STATE_SENSING) ? 8585 un->un_rqs : BP_PKT(bp); 8586 int result; 8587 8588 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 8589 "st_handle_incomplete(): dev = 0x%lx\n", un->un_dev); 8590 8591 ASSERT(mutex_owned(ST_MUTEX)); 8592 8593 switch (pkt->pkt_reason) { 8594 case CMD_INCOMPLETE: /* tran stopped with not normal state */ 8595 /* 8596 * this occurs when accessing a powered down drive, no 8597 * need to complain; just fail the open 8598 */ 8599 #ifdef STDEBUG 8600 if (st_debug >= 1) { 8601 st_clean_print(ST_DEVINFO, st_label, CE_WARN, 8602 "Failed CDB", (char *)pkt->pkt_cdbp, CDB_SIZE); 8603 } 8604 8605 #endif 8606 /* 8607 * if we have commands outstanding in HBA, and a command 8608 * comes back incomplete, we're hosed, so reset target 8609 * If we have the bus, but cmd_incomplete, we probably just 8610 * have a failed selection, so don't reset the target, just 8611 * requeue the command and try again 8612 */ 8613 if ((un->un_ncmds > 1) || (pkt->pkt_state != STATE_GOT_BUS)) { 8614 goto reset_target; 8615 } 8616 8617 /* 8618 * Retry selection a couple more times if we're 8619 * open. If opening, we only try just once to 8620 * reduce probe time for nonexistant devices. 8621 */ 8622 if ((un->un_laststate > ST_STATE_OPENING) && 8623 ((int)un->un_retry_ct < st_selection_retry_count)) { 8624 rval = QUE_COMMAND; 8625 } 8626 ST_DO_ERRSTATS(un, st_transerrs); 8627 break; 8628 8629 case CMD_ABORTED: 8630 /* 8631 * most likely this is caused by flush-on-error support. If 8632 * it was not there, the we're in trouble. 8633 */ 8634 if (!un->un_flush_on_errors) { 8635 un->un_status = SUN_KEY_FATAL; 8636 goto reset_target; 8637 } 8638 8639 st_set_pe_errno(un); 8640 bioerror(bp, un->un_errno); 8641 if (un->un_errno) 8642 return (COMMAND_DONE_ERROR); 8643 else 8644 return (COMMAND_DONE); 8645 8646 case CMD_TIMEOUT: /* Command timed out */ 8647 un->un_status = SUN_KEY_TIMEOUT; 8648 8649 /*FALLTHROUGH*/ 8650 default: 8651 reset_target: 8652 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 8653 "transport completed with %s\n", 8654 scsi_rname(pkt->pkt_reason)); 8655 ST_DO_ERRSTATS(un, st_transerrs); 8656 if ((pkt->pkt_state & STATE_GOT_TARGET) && 8657 ((pkt->pkt_statistics & (STAT_BUS_RESET | STAT_DEV_RESET | 8658 STAT_ABORTED)) == 0)) { 8659 8660 /* 8661 * If we haven't reserved the drive don't reset it. 8662 */ 8663 if ((un->un_rsvd_status & 8664 (ST_RESERVE | ST_APPLICATION_RESERVATIONS)) == 0) { 8665 return (rval); 8666 } 8667 8668 mutex_exit(ST_MUTEX); 8669 8670 result = scsi_reset(ROUTE, RESET_TARGET); 8671 /* 8672 * if target reset fails, then pull the chain 8673 */ 8674 if (result == 0) { 8675 result = scsi_reset(ROUTE, RESET_ALL); 8676 } 8677 mutex_enter(ST_MUTEX); 8678 8679 if ((result == 0) && (un->un_state >= ST_STATE_OPEN)) { 8680 /* no hope left to recover */ 8681 scsi_log(ST_DEVINFO, st_label, CE_WARN, 8682 "recovery by resets failed\n"); 8683 return (rval); 8684 } 8685 } 8686 } 8687 8688 if ((pkt->pkt_reason == CMD_RESET) || (pkt->pkt_statistics & 8689 (STAT_BUS_RESET | STAT_DEV_RESET))) { 8690 if ((un->un_rsvd_status & ST_RESERVE)) { 8691 un->un_rsvd_status |= ST_LOST_RESERVE; 8692 ST_DEBUG3(ST_DEVINFO, st_label, CE_WARN, 8693 "Lost Reservation\n"); 8694 } 8695 } 8696 8697 if ((int)un->un_retry_ct++ < st_retry_count) { 8698 if (un->un_pwr_mgmt == ST_PWR_SUSPENDED) { 8699 rval = QUE_COMMAND; 8700 } else if (bp == un->un_sbufp) { 8701 switch ((uchar_t)(uintptr_t)bp->b_forw) { 8702 case SCMD_MODE_SENSE: 8703 case SCMD_MODE_SELECT: 8704 case SCMD_READ_BLKLIM: 8705 case SCMD_REWIND: 8706 case SCMD_LOAD: 8707 case SCMD_TEST_UNIT_READY: 8708 /* 8709 * These commands can be rerun with impunity 8710 */ 8711 rval = QUE_COMMAND; 8712 break; 8713 8714 default: 8715 break; 8716 } 8717 } 8718 } else { 8719 rval = COMMAND_DONE_ERROR; 8720 } 8721 8722 if (un->un_state >= ST_STATE_OPEN) { 8723 scsi_log(ST_DEVINFO, st_label, CE_WARN, 8724 fail, scsi_rname(pkt->pkt_reason), 8725 (rval == COMMAND_DONE_ERROR)? 8726 "giving up" : "retrying command"); 8727 } 8728 return (rval); 8729 } 8730 8731 /* 8732 * if the device is busy, then put this bp back on the waitq, on the 8733 * interrupt thread, where we want the head of the queue and not the 8734 * end 8735 * 8736 * The callers of this routine should take measures to save the 8737 * un_throttle in un_last_throttle which will be restored in 8738 * st_intr_restart(). The only exception should be st_intr_restart() 8739 * calling this routine for which the saving is already done. 8740 */ 8741 static int 8742 st_handle_intr_busy(struct scsi_tape *un, struct buf *bp, 8743 clock_t timeout_interval) 8744 { 8745 struct buf *last_quef; 8746 int rval = 0; 8747 8748 mutex_enter(ST_MUTEX); 8749 8750 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 8751 "st_handle_intr_busy(), un = 0x%p\n", (void *)un); 8752 8753 /* 8754 * Check to see if we hit the retry timeout. We check to make sure 8755 * this is the first one on the runq and make sure we have not 8756 * queued up any more, so this one has to be the last on the list 8757 * also. If it is not, we have to fail. If it is not the first, but 8758 * is the last we are in trouble anyway, as we are in the interrupt 8759 * context here. 8760 */ 8761 if (((int)un->un_tran_retry_ct++ > st_retry_count) || 8762 ((un->un_runqf != bp) && (un->un_runql != bp))) { 8763 rval = -1; 8764 goto exit; 8765 } 8766 8767 /* put the bp back on the waitq */ 8768 if (un->un_quef) { 8769 last_quef = un->un_quef; 8770 un->un_quef = bp; 8771 bp->b_actf = last_quef; 8772 } else { 8773 bp->b_actf = NULL; 8774 un->un_quef = bp; 8775 un->un_quel = bp; 8776 } 8777 8778 /* 8779 * We know that this is the first and last on the runq at this time, 8780 * so we just nullify those two queues 8781 */ 8782 un->un_runqf = NULL; 8783 un->un_runql = NULL; 8784 8785 /* 8786 * We don't want any other commands being started in the mean time. 8787 * If start had just released mutex after putting something on the 8788 * runq, we won't even get here. 8789 */ 8790 un->un_throttle = 0; 8791 8792 /* 8793 * send a marker pkt, if appropriate 8794 */ 8795 st_hba_unflush(un); 8796 8797 /* 8798 * all queues are aligned, we are just waiting to 8799 * transport 8800 */ 8801 un->un_hib_tid = timeout(st_intr_restart, un, timeout_interval); 8802 8803 exit: 8804 mutex_exit(ST_MUTEX); 8805 return (rval); 8806 } 8807 8808 static int 8809 st_handle_sense(struct scsi_tape *un, struct buf *bp) 8810 { 8811 struct scsi_pkt *rqpkt = un->un_rqs; 8812 int rval = COMMAND_DONE_ERROR; 8813 int amt; 8814 8815 ASSERT(mutex_owned(ST_MUTEX)); 8816 8817 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 8818 "st_handle_sense()\n"); 8819 8820 if (SCBP(rqpkt)->sts_busy) { 8821 ST_DEBUG4(ST_DEVINFO, st_label, CE_WARN, 8822 "busy unit on request sense\n"); 8823 if ((int)un->un_retry_ct++ < st_retry_count) { 8824 rval = QUE_BUSY_COMMAND; 8825 } 8826 return (rval); 8827 } else if (SCBP(rqpkt)->sts_chk) { 8828 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 8829 "Check Condition on REQUEST SENSE\n"); 8830 return (rval); 8831 } 8832 8833 /* was there enough data? */ 8834 amt = (int)SENSE_LENGTH - rqpkt->pkt_resid; 8835 if ((rqpkt->pkt_state & STATE_XFERRED_DATA) == 0 || 8836 (amt < SUN_MIN_SENSE_LENGTH)) { 8837 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 8838 "REQUEST SENSE couldn't get sense data\n"); 8839 return (rval); 8840 } 8841 return (st_decode_sense(un, bp, amt, SCBP(rqpkt))); 8842 } 8843 8844 static int 8845 st_handle_autosense(struct scsi_tape *un, struct buf *bp) 8846 { 8847 struct scsi_pkt *pkt = BP_PKT(bp); 8848 struct scsi_arq_status *arqstat = 8849 (struct scsi_arq_status *)pkt->pkt_scbp; 8850 int rval = COMMAND_DONE_ERROR; 8851 int amt; 8852 8853 ASSERT(mutex_owned(ST_MUTEX)); 8854 8855 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 8856 "st_handle_autosense()\n"); 8857 8858 if (arqstat->sts_rqpkt_status.sts_busy) { 8859 ST_DEBUG4(ST_DEVINFO, st_label, CE_WARN, 8860 "busy unit on request sense\n"); 8861 /* 8862 * we return QUE_SENSE so st_intr will setup the SENSE cmd. 8863 * the disadvantage is that we do not have any delay for the 8864 * second retry of rqsense and we have to keep a packet around 8865 */ 8866 return (QUE_SENSE); 8867 8868 } else if (arqstat->sts_rqpkt_reason != CMD_CMPLT) { 8869 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 8870 "transport error on REQUEST SENSE\n"); 8871 if ((arqstat->sts_rqpkt_state & STATE_GOT_TARGET) && 8872 ((arqstat->sts_rqpkt_statistics & 8873 (STAT_BUS_RESET | STAT_DEV_RESET | STAT_ABORTED)) == 0)) { 8874 mutex_exit(ST_MUTEX); 8875 if (scsi_reset(ROUTE, RESET_TARGET) == 0) { 8876 /* 8877 * if target reset fails, then pull the chain 8878 */ 8879 if (scsi_reset(ROUTE, RESET_ALL) == 0) { 8880 ST_DEBUG6(ST_DEVINFO, st_label, 8881 CE_WARN, 8882 "recovery by resets failed\n"); 8883 } 8884 } 8885 mutex_enter(ST_MUTEX); 8886 } 8887 return (rval); 8888 8889 } else if (arqstat->sts_rqpkt_status.sts_chk) { 8890 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 8891 "Check Condition on REQUEST SENSE\n"); 8892 return (rval); 8893 } 8894 8895 8896 /* was there enough data? */ 8897 amt = (int)SENSE_LENGTH - arqstat->sts_rqpkt_resid; 8898 if ((arqstat->sts_rqpkt_state & STATE_XFERRED_DATA) == 0 || 8899 (amt < SUN_MIN_SENSE_LENGTH)) { 8900 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 8901 "REQUEST SENSE couldn't get sense data\n"); 8902 return (rval); 8903 } 8904 8905 bcopy(&arqstat->sts_sensedata, ST_RQSENSE, SENSE_LENGTH); 8906 8907 return (st_decode_sense(un, bp, amt, &arqstat->sts_rqpkt_status)); 8908 } 8909 8910 static int 8911 st_decode_sense(struct scsi_tape *un, struct buf *bp, int amt, 8912 struct scsi_status *statusp) 8913 { 8914 struct scsi_pkt *pkt = BP_PKT(bp); 8915 int rval = COMMAND_DONE_ERROR; 8916 long resid; 8917 struct scsi_extended_sense *sensep = ST_RQSENSE; 8918 int severity; 8919 int get_error; 8920 8921 ASSERT(mutex_owned(ST_MUTEX)); 8922 8923 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 8924 "st_decode_sense()\n"); 8925 8926 /* 8927 * For uscsi commands, squirrel away a copy of the 8928 * results of the Request Sense. 8929 */ 8930 if (USCSI_CMD(bp)) { 8931 struct uscsi_cmd *ucmd = BP_UCMD(bp); 8932 ucmd->uscsi_rqstatus = *(uchar_t *)statusp; 8933 if (ucmd->uscsi_rqlen && un->un_srqbufp) { 8934 uchar_t rqlen = min((uchar_t)amt, ucmd->uscsi_rqlen); 8935 ucmd->uscsi_rqresid = ucmd->uscsi_rqlen - rqlen; 8936 bcopy(ST_RQSENSE, un->un_srqbufp, rqlen); 8937 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 8938 "st_decode_sense: stat=0x%x resid=0x%x\n", 8939 ucmd->uscsi_rqstatus, ucmd->uscsi_rqresid); 8940 } 8941 } 8942 8943 /* 8944 * If the drive is an MT-02, reposition the 8945 * secondary error code into the proper place. 8946 * 8947 * XXX MT-02 is non-CCS tape, so secondary error code 8948 * is in byte 8. However, in SCSI-2, tape has CCS definition 8949 * so it's in byte 12. 8950 */ 8951 if (un->un_dp->type == ST_TYPE_EMULEX) { 8952 sensep->es_code = sensep->es_add_info[0]; 8953 } 8954 8955 /* for normal I/O check extract the resid values. */ 8956 if (bp != un->un_sbufp) { 8957 if (sensep->es_valid) { 8958 resid = (sensep->es_info_1 << 24) | 8959 (sensep->es_info_2 << 16) | 8960 (sensep->es_info_3 << 8) | 8961 (sensep->es_info_4); 8962 if (un->un_bsize) { 8963 resid *= un->un_bsize; 8964 } 8965 } else if (pkt->pkt_state & STATE_XFERRED_DATA) { 8966 resid = pkt->pkt_resid; 8967 } else { 8968 resid = bp->b_bcount; 8969 } 8970 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 8971 "st_handle_sense (rw): xferred bit = %d, resid=%ld (%d), " 8972 "pkt_resid=%ld\n", pkt->pkt_state & STATE_XFERRED_DATA, 8973 resid, 8974 (sensep->es_info_1 << 24) | 8975 (sensep->es_info_2 << 16) | 8976 (sensep->es_info_3 << 8) | 8977 (sensep->es_info_4), 8978 pkt->pkt_resid); 8979 /* 8980 * The problem is, what should we believe? 8981 */ 8982 if (resid && (pkt->pkt_resid == 0)) { 8983 pkt->pkt_resid = resid; 8984 } 8985 } else { 8986 /* 8987 * If the command is SCMD_SPACE, we need to get the 8988 * residual as returned in the sense data, to adjust 8989 * our idea of current tape position correctly 8990 */ 8991 if ((CDBP(pkt)->scc_cmd == SCMD_SPACE || 8992 CDBP(pkt)->scc_cmd == SCMD_WRITE_FILE_MARK) && 8993 (sensep->es_valid)) { 8994 resid = (sensep->es_info_1 << 24) | 8995 (sensep->es_info_2 << 16) | 8996 (sensep->es_info_3 << 8) | 8997 (sensep->es_info_4); 8998 bp->b_resid = resid; 8999 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 9000 "st_handle_sense(other): resid=%ld\n", 9001 resid); 9002 } else { 9003 /* 9004 * If the special command is SCMD_READ, 9005 * the correct resid will be set later. 9006 */ 9007 resid = bp->b_bcount; 9008 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 9009 "st_handle_sense(special read): resid=%ld\n", 9010 resid); 9011 } 9012 } 9013 9014 if ((un->un_state >= ST_STATE_OPEN) && 9015 (DEBUGGING || st_error_level == SCSI_ERR_ALL)) { 9016 st_clean_print(ST_DEVINFO, st_label, CE_NOTE, 9017 "Failed CDB", (char *)pkt->pkt_cdbp, CDB_SIZE); 9018 st_clean_print(ST_DEVINFO, st_label, CE_CONT, 9019 "sense data", (char *)sensep, amt); 9020 scsi_log(ST_DEVINFO, st_label, CE_CONT, 9021 "count 0x%lx resid 0x%lx pktresid 0x%lx\n", 9022 bp->b_bcount, resid, pkt->pkt_resid); 9023 } 9024 9025 switch (un->un_status = sensep->es_key) { 9026 case KEY_NO_SENSE: 9027 severity = SCSI_ERR_INFO; 9028 9029 /* 9030 * Erase, locate or rewind operation in progress, retry 9031 * ASC ASCQ 9032 * 00 18 Erase operation in progress 9033 * 00 19 Locate operation in progress 9034 * 00 1A Rewind operation in progress 9035 */ 9036 if (sensep->es_add_code == 0 && 9037 ((sensep->es_qual_code == 0x18) || 9038 (sensep->es_qual_code == 0x19) || 9039 (sensep->es_qual_code == 0x1a))) { 9040 rval = QUE_COMMAND; 9041 break; 9042 } 9043 9044 goto common; 9045 9046 case KEY_RECOVERABLE_ERROR: 9047 severity = SCSI_ERR_RECOVERED; 9048 if ((sensep->es_class == CLASS_EXTENDED_SENSE) && 9049 (sensep->es_code == ST_DEFERRED_ERROR)) { 9050 if (un->un_dp->options & 9051 ST_RETRY_ON_RECOVERED_DEFERRED_ERROR) { 9052 rval = QUE_LAST_COMMAND; 9053 scsi_errmsg(ST_SCSI_DEVP, pkt, st_label, severity, 9054 un->un_blkno, un->un_err_blkno, scsi_cmds, 9055 sensep); 9056 scsi_log(ST_DEVINFO, st_label, CE_CONT, 9057 "Command will be retried\n"); 9058 } else { 9059 severity = SCSI_ERR_FATAL; 9060 rval = COMMAND_DONE_ERROR_RECOVERED; 9061 ST_DO_ERRSTATS(un, st_softerrs); 9062 scsi_errmsg(ST_SCSI_DEVP, pkt, st_label, severity, 9063 un->un_blkno, un->un_err_blkno, scsi_cmds, 9064 sensep); 9065 } 9066 break; 9067 } 9068 common: 9069 /* 9070 * XXX only want reads to be stopped by filemarks. 9071 * Don't want them to be stopped by EOT. EOT matters 9072 * only on write. 9073 */ 9074 if (sensep->es_filmk && !sensep->es_eom) { 9075 rval = COMMAND_DONE; 9076 } else if (sensep->es_eom) { 9077 rval = COMMAND_DONE; 9078 } else if (sensep->es_ili) { 9079 /* 9080 * Fun with variable length record devices: 9081 * for specifying larger blocks sizes than the 9082 * actual physical record size. 9083 */ 9084 if (un->un_bsize == 0 && resid > 0) { 9085 /* 9086 * XXX! Ugly. 9087 * The requested blocksize is > tape blocksize, 9088 * so this is ok, so we just return the 9089 * actual size xferred. 9090 */ 9091 pkt->pkt_resid = resid; 9092 rval = COMMAND_DONE; 9093 } else if (un->un_bsize == 0 && resid < 0) { 9094 /* 9095 * The requested blocksize is < tape blocksize, 9096 * so this is not ok, so we err with ENOMEM 9097 */ 9098 rval = COMMAND_DONE_ERROR_RECOVERED; 9099 st_bioerror(bp, ENOMEM); 9100 } else { 9101 ST_DO_ERRSTATS(un, st_softerrs); 9102 severity = SCSI_ERR_FATAL; 9103 rval = COMMAND_DONE_ERROR; 9104 st_bioerror(bp, EINVAL); 9105 } 9106 } else { 9107 /* 9108 * we hope and pray for this just being 9109 * something we can ignore (ie. a 9110 * truly recoverable soft error) 9111 */ 9112 rval = COMMAND_DONE; 9113 } 9114 if (sensep->es_filmk) { 9115 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 9116 "filemark\n"); 9117 un->un_status = SUN_KEY_EOF; 9118 un->un_eof = ST_EOF_PENDING; 9119 SET_PE_FLAG(un); 9120 } 9121 9122 /* 9123 * ignore eom when reading, a fmk should terminate reading 9124 */ 9125 if ((sensep->es_eom) && 9126 (CDBP(pkt)->scc_cmd != SCMD_READ)) { 9127 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "eom\n"); 9128 un->un_status = SUN_KEY_EOT; 9129 un->un_eof = ST_EOM; 9130 SET_PE_FLAG(un); 9131 } 9132 9133 break; 9134 9135 case KEY_ILLEGAL_REQUEST: 9136 9137 if (un->un_laststate >= ST_STATE_OPEN) { 9138 ST_DO_ERRSTATS(un, st_softerrs); 9139 severity = SCSI_ERR_FATAL; 9140 } else { 9141 severity = SCSI_ERR_INFO; 9142 } 9143 break; 9144 9145 case KEY_MEDIUM_ERROR: 9146 ST_DO_ERRSTATS(un, st_harderrs); 9147 severity = SCSI_ERR_FATAL; 9148 9149 /* 9150 * for (buffered) writes, a medium error must be fatal 9151 */ 9152 if (CDBP(pkt)->scc_cmd != SCMD_WRITE) { 9153 rval = COMMAND_DONE_ERROR_RECOVERED; 9154 } 9155 9156 check_keys: 9157 /* 9158 * attempt to process the keys in the presence of 9159 * other errors 9160 */ 9161 if (sensep->es_ili && rval != COMMAND_DONE_ERROR) { 9162 /* 9163 * Fun with variable length record devices: 9164 * for specifying larger blocks sizes than the 9165 * actual physical record size. 9166 */ 9167 if (un->un_bsize == 0 && resid > 0) { 9168 /* 9169 * XXX! Ugly 9170 */ 9171 pkt->pkt_resid = resid; 9172 } else if (un->un_bsize == 0 && resid < 0) { 9173 st_bioerror(bp, EINVAL); 9174 } else { 9175 severity = SCSI_ERR_FATAL; 9176 rval = COMMAND_DONE_ERROR; 9177 st_bioerror(bp, EINVAL); 9178 } 9179 } 9180 if (sensep->es_filmk) { 9181 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 9182 "filemark\n"); 9183 un->un_status = SUN_KEY_EOF; 9184 un->un_eof = ST_EOF_PENDING; 9185 SET_PE_FLAG(un); 9186 } 9187 9188 /* 9189 * ignore eom when reading, a fmk should terminate reading 9190 */ 9191 if ((sensep->es_eom) && 9192 (CDBP(pkt)->scc_cmd != SCMD_READ)) { 9193 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "eom\n"); 9194 un->un_status = SUN_KEY_EOT; 9195 un->un_eof = ST_EOM; 9196 SET_PE_FLAG(un); 9197 } 9198 9199 break; 9200 9201 case KEY_VOLUME_OVERFLOW: 9202 ST_DO_ERRSTATS(un, st_softerrs); 9203 un->un_eof = ST_EOM; 9204 severity = SCSI_ERR_FATAL; 9205 rval = COMMAND_DONE_ERROR; 9206 goto check_keys; 9207 9208 case KEY_HARDWARE_ERROR: 9209 ST_DO_ERRSTATS(un, st_harderrs); 9210 severity = SCSI_ERR_FATAL; 9211 rval = COMMAND_DONE_ERROR; 9212 if (un->un_dp->options & ST_EJECT_ON_CHANGER_FAILURE) 9213 un->un_eject_tape_on_failure = st_check_asc_ascq(un); 9214 break; 9215 9216 case KEY_BLANK_CHECK: 9217 ST_DO_ERRSTATS(un, st_softerrs); 9218 severity = SCSI_ERR_INFO; 9219 9220 /* 9221 * if not a special request and some data was xferred then it 9222 * it is not an error yet 9223 */ 9224 if (bp != un->un_sbufp && (bp->b_flags & B_READ)) { 9225 /* 9226 * no error for read with or without data xferred 9227 */ 9228 un->un_status = SUN_KEY_EOT; 9229 un->un_eof = ST_EOT; 9230 rval = COMMAND_DONE_ERROR; 9231 SET_PE_FLAG(un); 9232 goto check_keys; 9233 } else if (bp != un->un_sbufp && 9234 (pkt->pkt_state & STATE_XFERRED_DATA)) { 9235 rval = COMMAND_DONE; 9236 } else { 9237 rval = COMMAND_DONE_ERROR_RECOVERED; 9238 } 9239 9240 if (un->un_laststate >= ST_STATE_OPEN) { 9241 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 9242 "blank check\n"); 9243 un->un_eof = ST_EOM; 9244 } 9245 if ((CDBP(pkt)->scc_cmd == SCMD_SPACE) && 9246 (un->un_dp->options & ST_KNOWS_EOD) && 9247 (severity = SCSI_ERR_INFO)) { 9248 /* 9249 * we were doing a fast forward by skipping 9250 * multiple fmk at the time 9251 */ 9252 st_bioerror(bp, EIO); 9253 severity = SCSI_ERR_RECOVERED; 9254 rval = COMMAND_DONE; 9255 } 9256 SET_PE_FLAG(un); 9257 goto check_keys; 9258 9259 case KEY_WRITE_PROTECT: 9260 if (st_wrongtapetype(un)) { 9261 un->un_status = SUN_KEY_WRONGMEDIA; 9262 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 9263 "wrong tape for writing- use DC6150 tape (or equivalent)\n"); 9264 severity = SCSI_ERR_UNKNOWN; 9265 } else { 9266 severity = SCSI_ERR_FATAL; 9267 } 9268 ST_DO_ERRSTATS(un, st_harderrs); 9269 rval = COMMAND_DONE_ERROR; 9270 st_bioerror(bp, EACCES); 9271 break; 9272 9273 case KEY_UNIT_ATTENTION: 9274 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 9275 "KEY_UNIT_ATTENTION : un_state = %d\n", un->un_state); 9276 9277 /* 9278 * If we have detected a Bus Reset and the tape 9279 * drive has been reserved. 9280 */ 9281 if (ST_RQSENSE->es_add_code == 0x29 && 9282 (un->un_rsvd_status & ST_RESERVE)) { 9283 un->un_rsvd_status |= ST_LOST_RESERVE; 9284 ST_DEBUG(ST_DEVINFO, st_label, CE_WARN, 9285 "st_decode_sense: Lost Reservation\n"); 9286 } 9287 9288 if (un->un_state <= ST_STATE_OPENING) { 9289 /* 9290 * Look, the tape isn't open yet, now determine 9291 * if the cause is a BUS RESET, Save the file and 9292 * Block positions for the callers to recover from 9293 * the loss of position. 9294 */ 9295 if ((un->un_fileno >= 0) && 9296 (un->un_fileno || un->un_blkno)) { 9297 if (ST_RQSENSE->es_add_code == 0x29) { 9298 un->un_save_fileno = un->un_fileno; 9299 un->un_save_blkno = un->un_blkno; 9300 un->un_restore_pos = 1; 9301 } 9302 } 9303 9304 if ((int)un->un_retry_ct++ < st_retry_count) { 9305 rval = QUE_COMMAND; 9306 } else { 9307 rval = COMMAND_DONE_ERROR; 9308 } 9309 severity = SCSI_ERR_INFO; 9310 9311 } else { 9312 /* 9313 * Check if it is an Unexpected Unit Attention. 9314 * If state is >= ST_STATE_OPEN, we have 9315 * already done the initialization . 9316 * In this case it is Fatal Error 9317 * since no further reading/writing 9318 * can be done with fileno set to < 0. 9319 */ 9320 if (un->un_state >= ST_STATE_OPEN) { 9321 ST_DO_ERRSTATS(un, st_harderrs); 9322 severity = SCSI_ERR_FATAL; 9323 } else { 9324 severity = SCSI_ERR_INFO; 9325 } 9326 rval = COMMAND_DONE_ERROR; 9327 } 9328 un->un_fileno = -1; 9329 9330 break; 9331 9332 case KEY_NOT_READY: 9333 /* 9334 * If in process of getting ready retry. 9335 */ 9336 if (sensep->es_add_code == 0x04 && 9337 sensep->es_qual_code == 0x01 && 9338 un->un_retry_ct++ < st_retry_count) { 9339 rval = QUE_COMMAND; 9340 severity = SCSI_ERR_INFO; 9341 } else { 9342 /* give up */ 9343 rval = COMMAND_DONE_ERROR; 9344 severity = SCSI_ERR_FATAL; 9345 } 9346 9347 /* 9348 * If this was an error and after device opened 9349 * do error stats. 9350 */ 9351 if (rval == COMMAND_DONE_ERROR && 9352 un->un_state > ST_STATE_OPENING) { 9353 ST_DO_ERRSTATS(un, st_harderrs); 9354 } 9355 9356 if (ST_RQSENSE->es_add_code == 0x3a) { 9357 if (st_error_level >= SCSI_ERR_FATAL) 9358 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 9359 "Tape not inserted in drive\n"); 9360 un->un_mediastate = MTIO_EJECTED; 9361 cv_broadcast(&un->un_state_cv); 9362 } 9363 if ((un->un_dp->options & ST_EJECT_ON_CHANGER_FAILURE) && 9364 (rval != QUE_COMMAND)) 9365 un->un_eject_tape_on_failure = st_check_asc_ascq(un); 9366 break; 9367 9368 case KEY_ABORTED_COMMAND: 9369 9370 /* 9371 * Probably a parity error... 9372 * if we retry here then this may cause data to be 9373 * written twice or data skipped during reading 9374 */ 9375 ST_DO_ERRSTATS(un, st_harderrs); 9376 severity = SCSI_ERR_FATAL; 9377 rval = COMMAND_DONE_ERROR; 9378 goto check_keys; 9379 9380 default: 9381 /* 9382 * Undecoded sense key. Try retries and hope 9383 * that will fix the problem. Otherwise, we're 9384 * dead. 9385 */ 9386 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 9387 "Unhandled Sense Key '%s'\n", 9388 sense_keys[un->un_status]); 9389 ST_DO_ERRSTATS(un, st_harderrs); 9390 severity = SCSI_ERR_FATAL; 9391 rval = COMMAND_DONE_ERROR; 9392 goto check_keys; 9393 } 9394 9395 if ((!(pkt->pkt_flags & FLAG_SILENT) && 9396 un->un_state >= ST_STATE_OPEN) && (DEBUGGING || 9397 (un->un_laststate > ST_STATE_OPENING) && 9398 (severity >= st_error_level))) { 9399 9400 scsi_errmsg(ST_SCSI_DEVP, pkt, st_label, severity, 9401 un->un_blkno, un->un_err_blkno, scsi_cmds, sensep); 9402 if (sensep->es_filmk) { 9403 scsi_log(ST_DEVINFO, st_label, CE_CONT, 9404 "File Mark Detected\n"); 9405 } 9406 if (sensep->es_eom) { 9407 scsi_log(ST_DEVINFO, st_label, CE_CONT, 9408 "End-of-Media Detected\n"); 9409 } 9410 if (sensep->es_ili) { 9411 scsi_log(ST_DEVINFO, st_label, CE_CONT, 9412 "Incorrect Length Indicator Set\n"); 9413 } 9414 } 9415 get_error = geterror(bp); 9416 if (((rval == COMMAND_DONE_ERROR) || 9417 (rval == COMMAND_DONE_ERROR_RECOVERED)) && 9418 ((get_error == EIO) || (get_error == 0))) { 9419 un->un_rqs_state |= (ST_RQS_ERROR | ST_RQS_VALID); 9420 bcopy(ST_RQSENSE, un->un_uscsi_rqs_buf, SENSE_LENGTH); 9421 if (un->un_rqs_state & ST_RQS_READ) { 9422 un->un_rqs_state &= ~(ST_RQS_READ); 9423 } else { 9424 un->un_rqs_state |= ST_RQS_OVR; 9425 } 9426 } 9427 9428 return (rval); 9429 } 9430 9431 9432 static int 9433 st_handle_intr_retry_lcmd(struct scsi_tape *un, struct buf *bp) 9434 { 9435 int status = TRAN_ACCEPT; 9436 9437 mutex_enter(ST_MUTEX); 9438 9439 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 9440 "st_handle_intr_rtr_lcmd(), un = 0x%p\n", (void *)un); 9441 9442 /* 9443 * Check to see if we hit the retry timeout. We check to make sure 9444 * this is the first one on the runq and make sure we have not 9445 * queued up any more, so this one has to be the last on the list 9446 * also. If it is not, we have to fail. If it is not the first, but 9447 * is the last we are in trouble anyway, as we are in the interrupt 9448 * context here. 9449 */ 9450 if (((int)un->un_retry_ct > st_retry_count) || 9451 ((un->un_runqf != bp) && (un->un_runql != bp))) { 9452 goto exit; 9453 } 9454 9455 if (un->un_throttle) { 9456 un->un_last_throttle = un->un_throttle; 9457 un->un_throttle = 0; 9458 } 9459 9460 /* 9461 * Here we know : bp is the first and last one on the runq 9462 * it is not necessary to put it back on the head of the 9463 * waitq and then move from waitq to runq. Save this queuing 9464 * and call scsi_transport. 9465 */ 9466 9467 mutex_exit(ST_MUTEX); 9468 9469 status = scsi_transport(BP_PKT(bp)); 9470 9471 mutex_enter(ST_MUTEX); 9472 9473 if (status == TRAN_ACCEPT) { 9474 un->un_tran_retry_ct = 0; 9475 if (un->un_last_throttle) { 9476 un->un_throttle = un->un_last_throttle; 9477 } 9478 mutex_exit(ST_MUTEX); 9479 9480 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 9481 "restart transport \n"); 9482 return (0); 9483 } 9484 9485 ST_DO_KSTATS(bp, kstat_runq_back_to_waitq); 9486 mutex_exit(ST_MUTEX); 9487 9488 if (status == TRAN_BUSY) { 9489 if (st_handle_intr_busy(un, bp, 9490 ST_TRAN_BUSY_TIMEOUT) == 0) 9491 return (0); 9492 } 9493 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 9494 "restart transport rejected\n"); 9495 mutex_enter(ST_MUTEX); 9496 ST_DO_ERRSTATS(un, st_transerrs); 9497 if (un->un_last_throttle) { 9498 un->un_throttle = un->un_last_throttle; 9499 } 9500 exit: 9501 mutex_exit(ST_MUTEX); 9502 return (-1); 9503 } 9504 9505 static int 9506 st_wrongtapetype(struct scsi_tape *un) 9507 { 9508 9509 ASSERT(mutex_owned(ST_MUTEX)); 9510 9511 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 9512 "st_wrongtapetype()\n"); 9513 9514 /* 9515 * Hack to handle 600A, 600XTD, 6150 && 660 vs. 300XL tapes... 9516 */ 9517 if (un->un_dp && (un->un_dp->options & ST_QIC) && un->un_mspl) { 9518 switch (un->un_dp->type) { 9519 case ST_TYPE_WANGTEK: 9520 case ST_TYPE_ARCHIVE: 9521 /* 9522 * If this really worked, we could go off of 9523 * the density codes set in the modesense 9524 * page. For this drive, 0x10 == QIC-120, 9525 * 0xf == QIC-150, and 0x5 should be for 9526 * both QIC-24 and, maybe, QIC-11. However, 9527 * the h/w doesn't do what the manual says 9528 * that it should, so we'll key off of 9529 * getting a WRITE PROTECT error AND wp *not* 9530 * set in the mode sense information. 9531 */ 9532 /* 9533 * XXX but we already know that status is 9534 * write protect, so don't check it again. 9535 */ 9536 9537 if (un->un_status == KEY_WRITE_PROTECT && 9538 un->un_mspl->wp == 0) { 9539 return (1); 9540 } 9541 break; 9542 default: 9543 break; 9544 } 9545 } 9546 return (0); 9547 } 9548 9549 static int 9550 st_check_error(struct scsi_tape *un, struct scsi_pkt *pkt) 9551 { 9552 int action; 9553 9554 ASSERT(mutex_owned(ST_MUTEX)); 9555 9556 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, "st_check_error()\n"); 9557 9558 if (SCBP_C(pkt) == STATUS_RESERVATION_CONFLICT) { 9559 action = COMMAND_DONE_EACCES; 9560 un->un_rsvd_status |= ST_RESERVATION_CONFLICT; 9561 } else if (SCBP(pkt)->sts_busy) { 9562 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, "unit busy\n"); 9563 if ((int)un->un_retry_ct++ < st_retry_count) { 9564 action = QUE_BUSY_COMMAND; 9565 } else if ((un->un_rsvd_status & 9566 (ST_RESERVE | ST_APPLICATION_RESERVATIONS)) == 0) { 9567 /* 9568 * If this is a command done before reserve is done 9569 * don't reset. 9570 */ 9571 action = COMMAND_DONE_ERROR; 9572 } else { 9573 ST_DEBUG2(ST_DEVINFO, st_label, CE_WARN, 9574 "unit busy too long\n"); 9575 mutex_exit(ST_MUTEX); 9576 if (scsi_reset(ROUTE, RESET_TARGET) == 0) { 9577 (void) scsi_reset(ROUTE, RESET_ALL); 9578 } 9579 mutex_enter(ST_MUTEX); 9580 action = COMMAND_DONE_ERROR; 9581 } 9582 } else if (SCBP(pkt)->sts_chk) { 9583 /* 9584 * we should only get here if the auto rqsense failed 9585 * thru a uscsi cmd without autorequest sense 9586 * so we just try again 9587 */ 9588 action = QUE_SENSE; 9589 } else { 9590 action = COMMAND_DONE; 9591 } 9592 return (action); 9593 } 9594 9595 static void 9596 st_calc_bnum(struct scsi_tape *un, struct buf *bp) 9597 { 9598 int n; 9599 9600 ASSERT(mutex_owned(ST_MUTEX)); 9601 9602 if (un->un_bsize == 0) { 9603 n = ((bp->b_bcount - bp->b_resid == 0) ? 0 : 1); 9604 un->un_kbytes_xferred += (bp->b_bcount - bp->b_resid)/1000; 9605 } else { 9606 n = ((bp->b_bcount - bp->b_resid) / un->un_bsize); 9607 } 9608 un->un_blkno += n; 9609 } 9610 9611 static void 9612 st_set_state(struct scsi_tape *un) 9613 { 9614 struct buf *bp = un->un_runqf; 9615 struct scsi_pkt *sp = BP_PKT(bp); 9616 struct uscsi_cmd *ucmd; 9617 9618 ASSERT(mutex_owned(ST_MUTEX)); 9619 9620 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 9621 "st_set_state(): un_eof=%x fmneeded=%x pkt_resid=0x%lx (%ld)\n", 9622 un->un_eof, un->un_fmneeded, sp->pkt_resid, sp->pkt_resid); 9623 9624 if (bp != un->un_sbufp) { 9625 #ifdef STDEBUG 9626 if (DEBUGGING && sp->pkt_resid) { 9627 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 9628 "pkt_resid %ld bcount %ld\n", 9629 sp->pkt_resid, bp->b_bcount); 9630 } 9631 #endif 9632 bp->b_resid = sp->pkt_resid; 9633 st_calc_bnum(un, bp); 9634 if (bp->b_flags & B_READ) { 9635 un->un_lastop = ST_OP_READ; 9636 un->un_fmneeded = 0; 9637 } else { 9638 un->un_lastop = ST_OP_WRITE; 9639 if (un->un_dp->options & ST_REEL) { 9640 un->un_fmneeded = 2; 9641 } else { 9642 un->un_fmneeded = 1; 9643 } 9644 } 9645 /* 9646 * all is honky dory at this point, so let's 9647 * readjust the throttle, to increase speed, if we 9648 * have not throttled down. 9649 */ 9650 if (un->un_throttle) 9651 un->un_throttle = un->un_max_throttle; 9652 } else { 9653 char saved_lastop = un->un_lastop; 9654 uchar_t cmd = (uchar_t)(intptr_t)bp->b_forw; 9655 9656 un->un_lastop = ST_OP_CTL; 9657 9658 switch (cmd) { 9659 case SCMD_WRITE: 9660 bp->b_resid = sp->pkt_resid; 9661 un->un_lastop = ST_OP_WRITE; 9662 st_calc_bnum(un, bp); 9663 if (un->un_dp->options & ST_REEL) { 9664 un->un_fmneeded = 2; 9665 } else { 9666 un->un_fmneeded = 1; 9667 } 9668 break; 9669 case SCMD_READ: 9670 bp->b_resid = sp->pkt_resid; 9671 un->un_lastop = ST_OP_READ; 9672 st_calc_bnum(un, bp); 9673 un->un_fmneeded = 0; 9674 break; 9675 case SCMD_WRITE_FILE_MARK: 9676 if (un->un_eof != ST_EOM) 9677 un->un_eof = ST_NO_EOF; 9678 un->un_lastop = ST_OP_WEOF; 9679 un->un_fileno += (bp->b_bcount - bp->b_resid); 9680 un->un_blkno = 0; 9681 if (un->un_dp->options & ST_REEL) { 9682 un->un_fmneeded -= 9683 (bp->b_bcount - bp->b_resid); 9684 if (un->un_fmneeded < 0) { 9685 un->un_fmneeded = 0; 9686 } 9687 } else { 9688 un->un_fmneeded = 0; 9689 } 9690 9691 break; 9692 case SCMD_REWIND: 9693 un->un_eof = ST_NO_EOF; 9694 un->un_fileno = 0; 9695 un->un_blkno = 0; 9696 break; 9697 9698 case SCMD_SPACE: 9699 { 9700 int space_fmk, count; 9701 long resid; 9702 9703 count = (int)space_cnt(bp->b_bcount); 9704 resid = (long)space_cnt(bp->b_resid); 9705 space_fmk = ((bp->b_bcount) & (1<<24)) ? 1 : 0; 9706 9707 9708 if (count >= 0) { 9709 if (space_fmk) { 9710 if (un->un_eof <= ST_EOF) { 9711 un->un_eof = ST_NO_EOF; 9712 } 9713 un->un_fileno += (count - resid); 9714 un->un_blkno = 0; 9715 } else { 9716 un->un_blkno += count - resid; 9717 } 9718 } else if (count < 0) { 9719 if (space_fmk) { 9720 un->un_fileno -= 9721 ((-count) - resid); 9722 if (un->un_fileno < 0) { 9723 un->un_fileno = 0; 9724 un->un_blkno = 0; 9725 } else { 9726 un->un_blkno = INF; 9727 } 9728 } else { 9729 if (un->un_eof >= ST_EOF_PENDING) { 9730 /* 9731 * we stepped back into 9732 * a previous file; we are not 9733 * making an effort to pretend that 9734 * we are still in the current file 9735 * ie. logical == physical position 9736 * and leave it to st_ioctl to correct 9737 */ 9738 if (un->un_fileno > 0) { 9739 un->un_fileno--; 9740 un->un_blkno = INF; 9741 } else { 9742 un->un_blkno = 0; 9743 } 9744 } else { 9745 un->un_blkno -= 9746 (-count) - resid; 9747 } 9748 } 9749 } 9750 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 9751 "aft_space rs %ld fil %d blk %ld\n", 9752 resid, un->un_fileno, un->un_blkno); 9753 break; 9754 } 9755 case SCMD_LOAD: 9756 if (bp->b_bcount & 0x1) { 9757 un->un_fileno = 0; 9758 } else { 9759 un->un_state = ST_STATE_OFFLINE; 9760 un->un_fileno = -1; 9761 } 9762 un->un_density_known = 0; 9763 un->un_eof = ST_NO_EOF; 9764 un->un_blkno = 0; 9765 break; 9766 case SCMD_ERASE: 9767 un->un_eof = ST_NO_EOF; 9768 un->un_blkno = 0; 9769 un->un_fileno = 0; 9770 break; 9771 case SCMD_RESERVE: 9772 un->un_rsvd_status |= ST_RESERVE; 9773 un->un_rsvd_status &= 9774 ~(ST_RELEASE | ST_LOST_RESERVE | 9775 ST_RESERVATION_CONFLICT); 9776 un->un_lastop = saved_lastop; 9777 break; 9778 case SCMD_RELEASE: 9779 un->un_rsvd_status |= ST_RELEASE; 9780 un->un_rsvd_status &= 9781 ~(ST_RESERVE | ST_LOST_RESERVE | 9782 ST_RESERVATION_CONFLICT); 9783 un->un_lastop = saved_lastop; 9784 break; 9785 case SCMD_PERSISTENT_RESERVE_IN: 9786 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 9787 "PGR_IN command\n"); 9788 break; 9789 case SCMD_PERSISTENT_RESERVE_OUT: 9790 switch (sp->pkt_cdbp[1] & ST_SA_MASK) { 9791 case ST_SA_SCSI3_RESERVE: 9792 case ST_SA_SCSI3_PREEMPT: 9793 case ST_SA_SCSI3_PREEMPTANDABORT: 9794 un->un_rsvd_status |= 9795 ST_APPLICATION_RESERVATIONS; 9796 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 9797 "PGR Reserve and set: entering" 9798 " ST_APPLICATION_RESERVATIONS mode"); 9799 break; 9800 case ST_SA_SCSI3_RELEASE: 9801 case ST_SA_SCSI3_CLEAR: 9802 un->un_rsvd_status &= 9803 ~ST_APPLICATION_RESERVATIONS; 9804 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 9805 "PGR Release and reset: exiting" 9806 " ST_APPLICATION_RESERVATIONS mode"); 9807 break; 9808 } 9809 break; 9810 case SCMD_TEST_UNIT_READY: 9811 case SCMD_READ_BLKLIM: 9812 case SCMD_REQUEST_SENSE: 9813 case SCMD_INQUIRY: 9814 case SCMD_RECOVER_BUF: 9815 case SCMD_MODE_SELECT: 9816 case SCMD_MODE_SENSE: 9817 case SCMD_DOORLOCK: 9818 case SCMD_READ_POSITION: 9819 case SCMD_READ_BUFFER: 9820 case SCMD_REPORT_DENSITIES: 9821 case SCMD_LOG_SELECT_G1: 9822 case SCMD_LOG_SENSE_G1: 9823 case SCMD_REPORT_LUNS: 9824 case SCMD_READ_ATTRIBUTE: 9825 un->un_lastop = saved_lastop; 9826 break; 9827 case SCMD_LOCATE: /* Locate makes position unknown */ 9828 default: 9829 /* 9830 * Unknown command, If was USCSI and USCSI_SILENT 9831 * flag was not set, set position to unknown. 9832 */ 9833 if ((((ucmd = BP_UCMD(bp)) != NULL) && 9834 (ucmd->uscsi_flags & USCSI_SILENT) == 0)) { 9835 ST_DEBUG2(ST_DEVINFO, st_label, CE_WARN, 9836 "unknown cmd 0x%X caused loss of state\n", 9837 cmd); 9838 } else { 9839 break; 9840 } 9841 /* FALLTHROUGH */ 9842 case SCMD_WRITE_BUFFER: /* Writes new firmware to device */ 9843 un->un_fileno = -1; 9844 break; 9845 } 9846 } 9847 9848 /* 9849 * In the st driver we have a logical and physical file position. 9850 * Under BSD behavior, when you get a zero read, the logical position 9851 * is before the filemark but after the last record of the file. 9852 * The physical position is after the filemark. MTIOCGET should always 9853 * return the logical file position. 9854 * 9855 * The next read gives a silent skip to the next file. 9856 * Under SVR4, the logical file position remains before the filemark 9857 * until the file is closed or a space operation is performed. 9858 * Hence set err_resid and err_file before changing fileno if case 9859 * BSD Behaviour. 9860 */ 9861 un->un_err_resid = bp->b_resid; 9862 un->un_err_fileno = un->un_fileno; 9863 un->un_err_blkno = un->un_blkno; 9864 un->un_retry_ct = 0; 9865 9866 9867 /* 9868 * If we've seen a filemark via the last read operation 9869 * advance the file counter, but mark things such that 9870 * the next read operation gets a zero count. We have 9871 * to put this here to handle the case of sitting right 9872 * at the end of a tape file having seen the file mark, 9873 * but the tape is closed and then re-opened without 9874 * any further i/o. That is, the position information 9875 * must be updated before a close. 9876 */ 9877 9878 if (un->un_lastop == ST_OP_READ && un->un_eof == ST_EOF_PENDING) { 9879 /* 9880 * If we're a 1/2" tape, and we get a filemark 9881 * right on block 0, *AND* we were not in the 9882 * first file on the tape, and we've hit logical EOM. 9883 * We'll mark the state so that later we do the 9884 * right thing (in st_close(), st_strategy() or 9885 * st_ioctl()). 9886 * 9887 */ 9888 if ((un->un_dp->options & ST_REEL) && 9889 !(un->un_dp->options & ST_READ_IGNORE_EOFS) && 9890 un->un_blkno == 0 && un->un_fileno > 0) { 9891 un->un_eof = ST_EOT_PENDING; 9892 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 9893 "eot pending\n"); 9894 un->un_fileno++; 9895 un->un_blkno = 0; 9896 } else if (BSD_BEHAVIOR) { 9897 /* 9898 * If the read of the filemark was a side effect 9899 * of reading some blocks (i.e., data was actually 9900 * read), then the EOF mark is pending and the 9901 * bump into the next file awaits the next read 9902 * operation (which will return a zero count), or 9903 * a close or a space operation, else the bump 9904 * into the next file occurs now. 9905 */ 9906 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 9907 "resid=%lx, bcount=%lx\n", 9908 bp->b_resid, bp->b_bcount); 9909 if (bp->b_resid != bp->b_bcount) { 9910 un->un_eof = ST_EOF; 9911 } else { 9912 un->un_silent_skip = 1; 9913 un->un_eof = ST_NO_EOF; 9914 un->un_fileno++; 9915 un->un_save_blkno = un->un_blkno; 9916 un->un_blkno = 0; 9917 } 9918 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 9919 "eof of file %d, un_eof=%d\n", 9920 un->un_fileno, un->un_eof); 9921 } else if (SVR4_BEHAVIOR) { 9922 /* 9923 * If the read of the filemark was a side effect 9924 * of reading some blocks (i.e., data was actually 9925 * read), then the next read should return 0 9926 */ 9927 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 9928 "resid=%lx, bcount=%lx\n", 9929 bp->b_resid, bp->b_bcount); 9930 if (bp->b_resid == bp->b_bcount) { 9931 un->un_eof = ST_EOF; 9932 } 9933 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 9934 "eof of file=%d, un_eof=%d\n", 9935 un->un_fileno, un->un_eof); 9936 } 9937 } 9938 } 9939 9940 /* 9941 * set the correct un_errno, to take corner cases into consideration 9942 */ 9943 static void 9944 st_set_pe_errno(struct scsi_tape *un) 9945 { 9946 ASSERT(mutex_owned(ST_MUTEX)); 9947 9948 /* if errno is already set, don't reset it */ 9949 if (un->un_errno) 9950 return; 9951 9952 /* here un_errno == 0 */ 9953 /* 9954 * if the last transfer before flushing all the 9955 * waiting I/O's, was 0 (resid = count), then we 9956 * want to give the user an error on all the rest, 9957 * so here. If there was a transfer, we set the 9958 * resid and counts to 0, and let it drop through, 9959 * giving a zero return. the next I/O will then 9960 * give an error. 9961 */ 9962 if (un->un_last_resid == un->un_last_count) { 9963 switch (un->un_eof) { 9964 case ST_EOM: 9965 un->un_errno = ENOMEM; 9966 break; 9967 case ST_EOT: 9968 case ST_EOF: 9969 un->un_errno = EIO; 9970 break; 9971 } 9972 } else { 9973 /* 9974 * we know they did not have a zero, so make 9975 * sure they get one 9976 */ 9977 un->un_last_resid = un->un_last_count = 0; 9978 } 9979 } 9980 9981 9982 /* 9983 * send in a marker pkt to terminate flushing of commands by BBA (via 9984 * flush-on-errors) property. The HBA will always return TRAN_ACCEPT 9985 */ 9986 static void 9987 st_hba_unflush(struct scsi_tape *un) 9988 { 9989 ASSERT(mutex_owned(ST_MUTEX)); 9990 9991 if (!un->un_flush_on_errors) 9992 return; 9993 9994 #ifdef FLUSH_ON_ERRORS 9995 9996 if (!un->un_mkr_pkt) { 9997 un->un_mkr_pkt = scsi_init_pkt(ROUTE, NULL, (struct buf *)NULL, 9998 NULL, 0, 0, 0, SLEEP_FUNC, NULL); 9999 10000 /* we slept, so it must be there */ 10001 pkt->pkt_flags |= FLAG_FLUSH_MARKER; 10002 } 10003 10004 mutex_exit(ST_MUTEX); 10005 scsi_transport(un->un_mkr_pkt); 10006 mutex_enter(ST_MUTEX); 10007 #endif 10008 } 10009 10010 static void 10011 st_clean_print(dev_info_t *dev, char *label, uint_t level, 10012 char *title, char *data, int len) 10013 { 10014 int i; 10015 char buf[256]; 10016 10017 (void) sprintf(buf, "%s: ", title); 10018 for (i = 0; i < len; i++) { 10019 (void) sprintf(&buf[(int)strlen(buf)], "0x%x ", 10020 (data[i] & 0xff)); 10021 } 10022 (void) sprintf(&buf[(int)strlen(buf)], "\n"); 10023 10024 scsi_log(dev, label, level, "%s", buf); 10025 } 10026 10027 /* 10028 * Conditionally enabled debugging 10029 */ 10030 #ifdef STDEBUG 10031 static void 10032 st_debug_cmds(struct scsi_tape *un, int com, int count, int wait) 10033 { 10034 char tmpbuf[64]; 10035 10036 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 10037 "cmd=%s count=0x%x (%d) %ssync\n", 10038 scsi_cmd_name(com, scsi_cmds, tmpbuf), 10039 count, count, 10040 wait == ASYNC_CMD ? "a" : ""); 10041 } 10042 10043 /* 10044 * Returns pointer to name of minor node name of device 'dev'. 10045 */ 10046 static char * 10047 st_dev_name(dev_t dev) 10048 { 10049 const char density[] = { 'l', 'm', 'h', 'c' }; 10050 static char name[4]; 10051 minor_t minor; 10052 int nprt = 0; 10053 10054 minor = getminor(dev); 10055 name[nprt] = density[(minor & MT_DENSITY_MASK) >> 3]; 10056 10057 if (minor & MT_BSD) { 10058 name[++nprt] = 'b'; 10059 } 10060 10061 if (minor & MT_NOREWIND) { 10062 name[++nprt] = 'n'; 10063 } 10064 10065 /* NULL terminator */ 10066 name[++nprt] = 0; 10067 10068 return (name); 10069 } 10070 #endif /* STDEBUG */ 10071 10072 /* 10073 * Soft error reporting, so far unique to each drive 10074 * 10075 * Currently supported: exabyte and DAT soft error reporting 10076 */ 10077 static int 10078 st_report_exabyte_soft_errors(dev_t dev, int flag) 10079 { 10080 uchar_t *sensep; 10081 int amt; 10082 int rval = 0; 10083 char cdb[CDB_GROUP0], *c = cdb; 10084 struct uscsi_cmd *com; 10085 10086 GET_SOFT_STATE(dev); 10087 10088 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 10089 "st_report_exabyte_soft_errors(dev = 0x%lx, flag = %d)\n", 10090 dev, flag); 10091 10092 ASSERT(mutex_owned(ST_MUTEX)); 10093 10094 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 10095 sensep = kmem_zalloc(TAPE_SENSE_LENGTH, KM_SLEEP); 10096 10097 *c++ = SCMD_REQUEST_SENSE; 10098 *c++ = 0; 10099 *c++ = 0; 10100 *c++ = 0; 10101 *c++ = TAPE_SENSE_LENGTH; 10102 /* 10103 * set CLRCNT (byte 5, bit 7 which clears the error counts) 10104 */ 10105 *c = (char)0x80; 10106 10107 com->uscsi_cdb = cdb; 10108 com->uscsi_cdblen = CDB_GROUP0; 10109 com->uscsi_bufaddr = (caddr_t)sensep; 10110 com->uscsi_buflen = TAPE_SENSE_LENGTH; 10111 com->uscsi_flags = USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ; 10112 com->uscsi_timeout = un->un_dp->non_motion_timeout; 10113 10114 rval = st_ioctl_cmd(dev, com, FKIOCTL); 10115 if (rval || com->uscsi_status) { 10116 goto done; 10117 } 10118 10119 /* 10120 * was there enough data? 10121 */ 10122 amt = (int)TAPE_SENSE_LENGTH - com->uscsi_resid; 10123 10124 if ((amt >= 19) && un->un_kbytes_xferred) { 10125 uint_t count, error_rate; 10126 uint_t rate; 10127 10128 if (sensep[21] & CLN) { 10129 scsi_log(ST_DEVINFO, st_label, CE_WARN, 10130 "Periodic head cleaning required"); 10131 } 10132 if (un->un_kbytes_xferred < (EXABYTE_MIN_TRANSFER/1000)) 10133 goto done; 10134 /* 10135 * check if soft error reporting needs to be done. 10136 */ 10137 count = sensep[16] << 16 | sensep[17] << 8 | sensep[18]; 10138 count &= 0xffffff; 10139 error_rate = (count * 100)/un->un_kbytes_xferred; 10140 10141 #ifdef STDEBUG 10142 if (st_soft_error_report_debug) { 10143 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 10144 "Exabyte Soft Error Report:\n"); 10145 scsi_log(ST_DEVINFO, st_label, CE_CONT, 10146 "read/write error counter: %d\n", count); 10147 scsi_log(ST_DEVINFO, st_label, CE_CONT, 10148 "number of bytes transferred: %dK\n", 10149 un->un_kbytes_xferred); 10150 scsi_log(ST_DEVINFO, st_label, CE_CONT, 10151 "error_rate: %d%%\n", error_rate); 10152 10153 if (amt >= 22) { 10154 scsi_log(ST_DEVINFO, st_label, CE_CONT, 10155 "unit sense: 0x%b 0x%b 0x%b\n", 10156 sensep[19], SENSE_19_BITS, 10157 sensep[20], SENSE_20_BITS, 10158 sensep[21], SENSE_21_BITS); 10159 } 10160 if (amt >= 27) { 10161 scsi_log(ST_DEVINFO, st_label, CE_CONT, 10162 "tracking retry counter: %d\n", 10163 sensep[26]); 10164 scsi_log(ST_DEVINFO, st_label, CE_CONT, 10165 "read/write retry counter: %d\n", 10166 sensep[27]); 10167 } 10168 } 10169 #endif 10170 10171 if (flag & FWRITE) { 10172 rate = EXABYTE_WRITE_ERROR_THRESHOLD; 10173 } else { 10174 rate = EXABYTE_READ_ERROR_THRESHOLD; 10175 } 10176 if (error_rate >= rate) { 10177 scsi_log(ST_DEVINFO, st_label, CE_WARN, 10178 "Soft error rate (%d%%) during %s was too high", 10179 error_rate, 10180 ((flag & FWRITE) ? wrg_str : rdg_str)); 10181 scsi_log(ST_DEVINFO, st_label, CE_CONT, 10182 "Please, replace tape cartridge\n"); 10183 } 10184 } 10185 10186 done: 10187 kmem_free(com, sizeof (*com)); 10188 kmem_free(sensep, TAPE_SENSE_LENGTH); 10189 10190 if (rval != 0) { 10191 scsi_log(ST_DEVINFO, st_label, CE_WARN, 10192 "exabyte soft error reporting failed\n"); 10193 } 10194 return (rval); 10195 } 10196 10197 /* 10198 * this is very specific to Archive 4mm dat 10199 */ 10200 #define ONEGIG (1024 * 1024 * 1024) 10201 10202 static int 10203 st_report_dat_soft_errors(dev_t dev, int flag) 10204 { 10205 uchar_t *sensep; 10206 int amt, i; 10207 int rval = 0; 10208 char cdb[CDB_GROUP1], *c = cdb; 10209 struct uscsi_cmd *com; 10210 10211 GET_SOFT_STATE(dev); 10212 10213 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 10214 "st_report_dat_soft_errors(dev = 0x%lx, flag = %d)\n", dev, flag); 10215 10216 ASSERT(mutex_owned(ST_MUTEX)); 10217 10218 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 10219 sensep = kmem_zalloc(LOG_SENSE_LENGTH, KM_SLEEP); 10220 10221 *c++ = SCMD_LOG_SENSE_G1; 10222 *c++ = 0; 10223 *c++ = (flag & FWRITE) ? 0x42 : 0x43; 10224 *c++ = 0; 10225 *c++ = 0; 10226 *c++ = 0; 10227 *c++ = 2; 10228 *c++ = 0; 10229 *c++ = (char)LOG_SENSE_LENGTH; 10230 *c = 0; 10231 com->uscsi_cdb = cdb; 10232 com->uscsi_cdblen = CDB_GROUP1; 10233 com->uscsi_bufaddr = (caddr_t)sensep; 10234 com->uscsi_buflen = LOG_SENSE_LENGTH; 10235 com->uscsi_flags = 10236 USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ; 10237 com->uscsi_timeout = un->un_dp->non_motion_timeout; 10238 rval = st_ioctl_cmd(dev, com, FKIOCTL); 10239 if (rval) { 10240 scsi_log(ST_DEVINFO, st_label, CE_WARN, 10241 "DAT soft error reporting failed\n"); 10242 } 10243 if (rval || com->uscsi_status) { 10244 goto done; 10245 } 10246 10247 /* 10248 * was there enough data? 10249 */ 10250 amt = (int)LOG_SENSE_LENGTH - com->uscsi_resid; 10251 10252 if ((amt >= MIN_LOG_SENSE_LENGTH) && un->un_kbytes_xferred) { 10253 int total, retries, param_code; 10254 10255 total = -1; 10256 retries = -1; 10257 amt = sensep[3] + 4; 10258 10259 10260 #ifdef STDEBUG 10261 if (st_soft_error_report_debug) { 10262 (void) printf("logsense:"); 10263 for (i = 0; i < MIN_LOG_SENSE_LENGTH; i++) { 10264 if (i % 16 == 0) { 10265 (void) printf("\t\n"); 10266 } 10267 (void) printf(" %x", sensep[i]); 10268 } 10269 (void) printf("\n"); 10270 } 10271 #endif 10272 10273 /* 10274 * parse the param_codes 10275 */ 10276 if (sensep[0] == 2 || sensep[0] == 3) { 10277 for (i = 4; i < amt; i++) { 10278 param_code = (sensep[i++] << 8); 10279 param_code += sensep[i++]; 10280 i++; /* skip control byte */ 10281 if (param_code == 5) { 10282 if (sensep[i++] == 4) { 10283 total = (sensep[i++] << 24); 10284 total += (sensep[i++] << 16); 10285 total += (sensep[i++] << 8); 10286 total += sensep[i]; 10287 } 10288 } else if (param_code == 0x8007) { 10289 if (sensep[i++] == 2) { 10290 retries = sensep[i++] << 8; 10291 retries += sensep[i]; 10292 } 10293 } else { 10294 i += sensep[i]; 10295 } 10296 } 10297 } 10298 10299 /* 10300 * if the log sense returned valid numbers then determine 10301 * the read and write error thresholds based on the amount of 10302 * data transferred 10303 */ 10304 10305 if (total > 0 && retries > 0) { 10306 short normal_retries = 0; 10307 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 10308 "total xferred (%s) =%x, retries=%x\n", 10309 ((flag & FWRITE) ? wrg_str : rdg_str), 10310 total, retries); 10311 10312 if (flag & FWRITE) { 10313 if (total <= 10314 WRITE_SOFT_ERROR_WARNING_THRESHOLD) { 10315 normal_retries = 10316 DAT_SMALL_WRITE_ERROR_THRESHOLD; 10317 } else { 10318 normal_retries = 10319 DAT_LARGE_WRITE_ERROR_THRESHOLD; 10320 } 10321 } else { 10322 if (total <= 10323 READ_SOFT_ERROR_WARNING_THRESHOLD) { 10324 normal_retries = 10325 DAT_SMALL_READ_ERROR_THRESHOLD; 10326 } else { 10327 normal_retries = 10328 DAT_LARGE_READ_ERROR_THRESHOLD; 10329 } 10330 } 10331 10332 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 10333 "normal retries=%d\n", normal_retries); 10334 10335 if (retries >= normal_retries) { 10336 scsi_log(ST_DEVINFO, st_label, CE_WARN, 10337 "Soft error rate (retries = %d) during " 10338 "%s was too high", retries, 10339 ((flag & FWRITE) ? wrg_str : rdg_str)); 10340 scsi_log(ST_DEVINFO, st_label, CE_CONT, 10341 "Periodic head cleaning required " 10342 "and/or replace tape cartridge\n"); 10343 } 10344 10345 } else if (total == -1 || retries == -1) { 10346 scsi_log(ST_DEVINFO, st_label, CE_WARN, 10347 "log sense parameter code does not make sense\n"); 10348 } 10349 } 10350 10351 /* 10352 * reset all values 10353 */ 10354 c = cdb; 10355 *c++ = SCMD_LOG_SELECT_G1; 10356 *c++ = 2; /* this resets all values */ 10357 *c++ = (char)0xc0; 10358 *c++ = 0; 10359 *c++ = 0; 10360 *c++ = 0; 10361 *c++ = 0; 10362 *c++ = 0; 10363 *c++ = 0; 10364 *c = 0; 10365 com->uscsi_bufaddr = NULL; 10366 com->uscsi_buflen = 0; 10367 com->uscsi_flags = USCSI_DIAGNOSE | USCSI_SILENT; 10368 rval = st_ioctl_cmd(dev, com, FKIOCTL); 10369 if (rval) { 10370 scsi_log(ST_DEVINFO, st_label, CE_WARN, 10371 "DAT soft error reset failed\n"); 10372 } 10373 done: 10374 kmem_free(com, sizeof (*com)); 10375 kmem_free(sensep, LOG_SENSE_LENGTH); 10376 return (rval); 10377 } 10378 10379 static int 10380 st_report_soft_errors(dev_t dev, int flag) 10381 { 10382 GET_SOFT_STATE(dev); 10383 10384 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 10385 "st_report_soft_errors(dev = 0x%lx, flag = %d)\n", dev, flag); 10386 10387 ASSERT(mutex_owned(ST_MUTEX)); 10388 10389 switch (un->un_dp->type) { 10390 case ST_TYPE_EXB8500: 10391 case ST_TYPE_EXABYTE: 10392 return (st_report_exabyte_soft_errors(dev, flag)); 10393 /*NOTREACHED*/ 10394 case ST_TYPE_PYTHON: 10395 return (st_report_dat_soft_errors(dev, flag)); 10396 /*NOTREACHED*/ 10397 default: 10398 un->un_dp->options &= ~ST_SOFT_ERROR_REPORTING; 10399 return (-1); 10400 } 10401 } 10402 10403 /* 10404 * persistent error routines 10405 */ 10406 10407 /* 10408 * enable persistent errors, and set the throttle appropriately, checking 10409 * for flush-on-errors capability 10410 */ 10411 static void 10412 st_turn_pe_on(struct scsi_tape *un) 10413 { 10414 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, "st_pe_on\n"); 10415 ASSERT(mutex_owned(ST_MUTEX)); 10416 10417 un->un_persistence = 1; 10418 10419 /* 10420 * only use flush-on-errors if auto-request-sense and untagged-qing are 10421 * enabled. This will simplify the error handling for request senses 10422 */ 10423 10424 if (un->un_arq_enabled && un->un_untagged_qing) { 10425 uchar_t f_o_e; 10426 10427 mutex_exit(ST_MUTEX); 10428 f_o_e = (scsi_ifsetcap(ROUTE, "flush-on-errors", 1, 1) == 1) ? 10429 1 : 0; 10430 mutex_enter(ST_MUTEX); 10431 10432 un->un_flush_on_errors = f_o_e; 10433 } else { 10434 un->un_flush_on_errors = 0; 10435 } 10436 10437 if (un->un_flush_on_errors) 10438 un->un_max_throttle = (uchar_t)st_max_throttle; 10439 else 10440 un->un_max_throttle = 1; 10441 10442 if (un->un_dp->options & ST_RETRY_ON_RECOVERED_DEFERRED_ERROR) 10443 un->un_max_throttle = 1; 10444 10445 /* this will send a marker pkt */ 10446 CLEAR_PE(un); 10447 } 10448 10449 /* 10450 * This turns persistent errors permanently off 10451 */ 10452 static void 10453 st_turn_pe_off(struct scsi_tape *un) 10454 { 10455 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, "st_pe_off\n"); 10456 ASSERT(mutex_owned(ST_MUTEX)); 10457 10458 /* turn it off for good */ 10459 un->un_persistence = 0; 10460 10461 /* this will send a marker pkt */ 10462 CLEAR_PE(un); 10463 10464 /* turn off flush on error capability, if enabled */ 10465 if (un->un_flush_on_errors) { 10466 mutex_exit(ST_MUTEX); 10467 (void) scsi_ifsetcap(ROUTE, "flush-on-errors", 0, 1); 10468 mutex_enter(ST_MUTEX); 10469 } 10470 10471 10472 un->un_flush_on_errors = 0; 10473 } 10474 10475 /* 10476 * This clear persistent errors, allowing more commands through, and also 10477 * sending a marker packet. 10478 */ 10479 static void 10480 st_clear_pe(struct scsi_tape *un) 10481 { 10482 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, "st_pe_clear\n"); 10483 ASSERT(mutex_owned(ST_MUTEX)); 10484 10485 un->un_persist_errors = 0; 10486 un->un_throttle = un->un_last_throttle = 1; 10487 un->un_errno = 0; 10488 st_hba_unflush(un); 10489 } 10490 10491 /* 10492 * This will flag persistent errors, shutting everything down, if the 10493 * application had enabled persistent errors via MTIOCPERSISTENT 10494 */ 10495 static void 10496 st_set_pe_flag(struct scsi_tape *un) 10497 { 10498 ASSERT(mutex_owned(ST_MUTEX)); 10499 10500 if (un->un_persistence) { 10501 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, "st_pe_flag\n"); 10502 un->un_persist_errors = 1; 10503 un->un_throttle = un->un_last_throttle = 0; 10504 } 10505 } 10506 10507 /* 10508 * List of commands that are allowed to be done while another host holds 10509 * the reservation. 10510 */ 10511 struct { 10512 uchar_t cmd; 10513 uchar_t byte; /* byte to look for data */ 10514 uint32_t mask; /* bits that matter in the above data */ 10515 } rcmds[] = { 10516 { SCMD_TEST_UNIT_READY, 0, 0 }, /* may fail on older drives */ 10517 { SCMD_REQUEST_SENSE, 0, 0 }, 10518 { SCMD_READ_BLKLIM, 0, 0 }, 10519 { SCMD_INQUIRY, 0, 0 }, 10520 { SCMD_RESERVE, 0, 0 }, 10521 { SCMD_RELEASE, 0, 0 }, 10522 { SCMD_DOORLOCK, 4, 3 }, /* allow (unlock) media access only */ 10523 { SCMD_REPORT_DENSITIES, 0, 0 }, 10524 { SCMD_LOG_SENSE_G1, 0, 0 }, 10525 { SCMD_PERSISTENT_RESERVE_IN, 0, 0 }, 10526 { SCMD_PERSISTENT_RESERVE_OUT, 0, 0 }, 10527 { SCMD_REPORT_LUNS, 0, 0 } 10528 }; 10529 10530 static int 10531 st_do_reserve(struct scsi_tape *un) 10532 { 10533 int rval; 10534 10535 /* 10536 * Issue a Throw-Away reserve command to clear the 10537 * check condition. 10538 * If the current behaviour of reserve/release is to 10539 * hold reservation across opens , and if a Bus reset 10540 * has been issued between opens then this command 10541 * would set the ST_LOST_RESERVE flags in rsvd_status. 10542 * In this case return an EACCES so that user knows that 10543 * reservation has been lost in between opens. 10544 * If this error is not returned and we continue with 10545 * successful open , then user may think position of the 10546 * tape is still the same but inreality we would rewind the 10547 * tape and continue from BOT. 10548 */ 10549 rval = st_reserve_release(un, ST_RESERVE); 10550 if (rval) { 10551 if ((un->un_rsvd_status & ST_LOST_RESERVE_BETWEEN_OPENS) == 10552 ST_LOST_RESERVE_BETWEEN_OPENS) { 10553 un->un_rsvd_status &= ~(ST_LOST_RESERVE | ST_RESERVE); 10554 un->un_errno = EACCES; 10555 return (EACCES); 10556 } 10557 rval = st_reserve_release(un, ST_RESERVE); 10558 } 10559 if (rval == 0) { 10560 un->un_rsvd_status |= ST_INIT_RESERVE; 10561 } 10562 10563 return (rval); 10564 } 10565 10566 static int 10567 st_check_cdb_for_need_to_reserve(struct scsi_tape *un, caddr_t cdb) 10568 { 10569 int i; 10570 int rval = 0; 10571 10572 /* 10573 * If already reserved no need to do it again. 10574 * Also if Reserve and Release are disabled Just return. 10575 */ 10576 if ((un->un_rsvd_status & (ST_RESERVE | ST_APPLICATION_RESERVATIONS)) || 10577 (un->un_dp->options & ST_NO_RESERVE_RELEASE)) { 10578 ST_DEBUG6(ST_DEVINFO, st_label, CE_NOTE, 10579 "st_check_cdb_for_need_to_reserve() reserve unneeded 0x%x", 10580 cdb[0]); 10581 return (0); 10582 } 10583 10584 /* See if command is on the list */ 10585 for (i = 0; i < ST_NUM_MEMBERS(rcmds); i++) { 10586 if ((uchar_t)cdb[0] == rcmds[i].cmd) { 10587 /* 10588 * cmd is on list. 10589 * if byte is zero always allowed. 10590 */ 10591 if (rcmds[i].byte == 0) { 10592 return (rval); 10593 } 10594 if (((cdb[rcmds[i].byte]) & (rcmds[i].mask)) == 0) { 10595 return (rval); 10596 } 10597 break; 10598 } 10599 } 10600 10601 ST_DEBUG6(ST_DEVINFO, st_label, CE_NOTE, 10602 "Command 0x%x requires reservation", cdb[0]); 10603 10604 rval = st_do_reserve(un); 10605 10606 return (rval); 10607 } 10608 10609 static int 10610 st_check_cmd_for_need_to_reserve(struct scsi_tape *un, uchar_t cmd, int cnt) 10611 { 10612 int i; 10613 int rval = 0; 10614 10615 if ((un->un_rsvd_status & (ST_RESERVE | ST_APPLICATION_RESERVATIONS)) || 10616 (un->un_dp->options & ST_NO_RESERVE_RELEASE)) { 10617 ST_DEBUG6(ST_DEVINFO, st_label, CE_NOTE, 10618 "st_check_cmd_for_need_to_reserve() reserve unneeded 0x%x", 10619 cmd); 10620 return (0); 10621 } 10622 10623 /* See if command is on the list */ 10624 for (i = 0; i < ST_NUM_MEMBERS(rcmds); i++) { 10625 if (cmd == rcmds[i].cmd) { 10626 /* 10627 * cmd is on list. 10628 * if byte is zero always allowed. 10629 */ 10630 if (rcmds[i].byte == 0) { 10631 return (rval); 10632 } 10633 if (((rcmds[i].mask) & cnt) == 0) { 10634 return (rval); 10635 } 10636 break; 10637 } 10638 } 10639 10640 ST_DEBUG6(ST_DEVINFO, st_label, CE_NOTE, 10641 "Cmd 0x%x requires reservation", cmd); 10642 10643 rval = st_do_reserve(un); 10644 10645 return (rval); 10646 } 10647 10648 static int 10649 st_reserve_release(struct scsi_tape *un, int cmd) 10650 { 10651 struct uscsi_cmd uscsi_cmd; 10652 struct uscsi_cmd *com = &uscsi_cmd; 10653 int rval; 10654 char cdb[CDB_GROUP0]; 10655 10656 10657 ASSERT(mutex_owned(ST_MUTEX)); 10658 10659 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 10660 "st_reserve_release: %s \n", 10661 (cmd == ST_RELEASE)? "Releasing":"Reserving"); 10662 10663 bzero(cdb, CDB_GROUP0); 10664 if (cmd == ST_RELEASE) { 10665 cdb[0] = SCMD_RELEASE; 10666 } else { 10667 cdb[0] = SCMD_RESERVE; 10668 } 10669 bzero(com, sizeof (struct uscsi_cmd)); 10670 com->uscsi_flags = USCSI_WRITE; 10671 com->uscsi_cdb = cdb; 10672 com->uscsi_cdblen = CDB_GROUP0; 10673 com->uscsi_timeout = un->un_dp->non_motion_timeout; 10674 10675 rval = st_ioctl_cmd(un->un_dev, com, FKIOCTL); 10676 10677 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 10678 "st_reserve_release: rval(1)=%d\n", rval); 10679 10680 if (rval) { 10681 if (com->uscsi_status == STATUS_RESERVATION_CONFLICT) { 10682 rval = EACCES; 10683 } 10684 /* 10685 * dynamically turn off reserve/release support 10686 * in case of drives which do not support 10687 * reserve/release command(ATAPI drives). 10688 */ 10689 if (un->un_status == KEY_ILLEGAL_REQUEST) { 10690 if (un->un_dp->options & ST_NO_RESERVE_RELEASE) { 10691 un->un_dp->options |= ST_NO_RESERVE_RELEASE; 10692 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 10693 "Tape unit does not support " 10694 "reserve/release \n"); 10695 } 10696 rval = 0; 10697 } 10698 } 10699 return (rval); 10700 } 10701 10702 static int 10703 st_take_ownership(dev_t dev) 10704 { 10705 int rval; 10706 10707 GET_SOFT_STATE(dev); 10708 ASSERT(mutex_owned(ST_MUTEX)); 10709 10710 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 10711 "st_take_ownership: Entering ...\n"); 10712 10713 10714 rval = st_reserve_release(un, ST_RESERVE); 10715 /* 10716 * XXX -> Should reset be done only if we get EACCES. 10717 * . 10718 */ 10719 if (rval) { 10720 mutex_exit(ST_MUTEX); 10721 if (scsi_reset(ROUTE, RESET_TARGET) == 0) { 10722 if (scsi_reset(ROUTE, RESET_ALL) == 0) { 10723 mutex_enter(ST_MUTEX); 10724 return (EIO); 10725 } 10726 } 10727 mutex_enter(ST_MUTEX); 10728 un->un_rsvd_status &= 10729 ~(ST_LOST_RESERVE | ST_RESERVATION_CONFLICT); 10730 10731 mutex_exit(ST_MUTEX); 10732 delay(drv_usectohz(ST_RESERVATION_DELAY)); 10733 mutex_enter(ST_MUTEX); 10734 /* 10735 * remove the check condition. 10736 */ 10737 (void) st_reserve_release(un, ST_RESERVE); 10738 if ((rval = st_reserve_release(un, ST_RESERVE)) != 0) { 10739 if ((st_reserve_release(un, ST_RESERVE)) != 0) { 10740 rval = (un->un_rsvd_status & 10741 ST_RESERVATION_CONFLICT) ? EACCES : EIO; 10742 return (rval); 10743 } 10744 } 10745 /* 10746 * Set tape state to ST_STATE_OFFLINE , in case if 10747 * the user wants to continue and start using 10748 * the tape. 10749 */ 10750 un->un_state = ST_STATE_OFFLINE; 10751 un->un_rsvd_status |= ST_INIT_RESERVE; 10752 } 10753 return (rval); 10754 } 10755 10756 static int 10757 st_create_errstats(struct scsi_tape *un, int instance) 10758 { 10759 char kstatname[KSTAT_STRLEN]; 10760 10761 /* 10762 * Create device error kstats 10763 */ 10764 10765 if (un->un_errstats == (kstat_t *)0) { 10766 (void) sprintf(kstatname, "st%d,err", instance); 10767 un->un_errstats = kstat_create("sterr", instance, kstatname, 10768 "device_error", KSTAT_TYPE_NAMED, 10769 sizeof (struct st_errstats) / sizeof (kstat_named_t), 10770 KSTAT_FLAG_PERSISTENT); 10771 10772 if (un->un_errstats) { 10773 struct st_errstats *stp; 10774 10775 stp = (struct st_errstats *)un->un_errstats->ks_data; 10776 kstat_named_init(&stp->st_softerrs, "Soft Errors", 10777 KSTAT_DATA_ULONG); 10778 kstat_named_init(&stp->st_harderrs, "Hard Errors", 10779 KSTAT_DATA_ULONG); 10780 kstat_named_init(&stp->st_transerrs, "Transport Errors", 10781 KSTAT_DATA_ULONG); 10782 kstat_named_init(&stp->st_vid, "Vendor", 10783 KSTAT_DATA_CHAR); 10784 kstat_named_init(&stp->st_pid, "Product", 10785 KSTAT_DATA_CHAR); 10786 kstat_named_init(&stp->st_revision, "Revision", 10787 KSTAT_DATA_CHAR); 10788 kstat_named_init(&stp->st_serial, "Serial No", 10789 KSTAT_DATA_CHAR); 10790 un->un_errstats->ks_private = un; 10791 un->un_errstats->ks_update = nulldev; 10792 kstat_install(un->un_errstats); 10793 /* 10794 * Fill in the static data 10795 */ 10796 (void) strncpy(&stp->st_vid.value.c[0], 10797 ST_INQUIRY->inq_vid, 8); 10798 /* 10799 * XXX: Emulex MT-02 (and emulators) predates 10800 * SCSI-1 and has no vid & pid inquiry data. 10801 */ 10802 if (ST_INQUIRY->inq_len != 0) { 10803 (void) strncpy(&stp->st_pid.value.c[0], 10804 ST_INQUIRY->inq_pid, 16); 10805 (void) strncpy(&stp->st_revision.value.c[0], 10806 ST_INQUIRY->inq_revision, 4); 10807 (void) strncpy(&stp->st_serial.value.c[0], 10808 ST_INQUIRY->inq_serial, 12); 10809 } 10810 } 10811 } 10812 return (0); 10813 } 10814 10815 static int 10816 st_validate_tapemarks(struct scsi_tape *un, int fileno, daddr_t blkno) 10817 { 10818 dev_t dev; 10819 int rval; 10820 10821 ASSERT(MUTEX_HELD(&un->un_sd->sd_mutex)); 10822 ASSERT(mutex_owned(ST_MUTEX)); 10823 10824 dev = un->un_dev; 10825 10826 scsi_log(ST_DEVINFO, st_label, CE_NOTE, "Restoring tape" 10827 " position at fileno=%x, blkno=%lx....", fileno, blkno); 10828 10829 /* 10830 * Rewind ? Oh yeah, Fidelity has got the STK F/W changed 10831 * so as not to rewind tape on RESETS: Gee, Has life ever 10832 * been simple in tape land ? 10833 */ 10834 rval = st_cmd(dev, SCMD_REWIND, 0, SYNC_CMD); 10835 if (rval) { 10836 scsi_log(ST_DEVINFO, st_label, CE_WARN, 10837 "Failed to restore the last file and block position: In" 10838 " this state, Tape will be loaded at BOT during next open"); 10839 un->un_fileno = -1; 10840 return (rval); 10841 } 10842 10843 if (fileno) { 10844 rval = st_cmd(dev, SCMD_SPACE, Fmk(fileno), SYNC_CMD); 10845 if (rval) { 10846 scsi_log(ST_DEVINFO, st_label, CE_WARN, 10847 "Failed to restore the last file position: In this " 10848 " state, Tape will be loaded at BOT during next open"); 10849 un->un_fileno = -1; 10850 return (rval); 10851 } 10852 } 10853 10854 if (blkno) { 10855 rval = st_cmd(dev, SCMD_SPACE, Blk(blkno), SYNC_CMD); 10856 if (rval) { 10857 scsi_log(ST_DEVINFO, st_label, CE_WARN, 10858 "Failed to restore the last block position: In this" 10859 " state, tape will be loaded at BOT during next open"); 10860 un->un_fileno = -1; 10861 return (rval); 10862 } 10863 } 10864 10865 return (0); 10866 } 10867 10868 /* 10869 * check sense key, ASC, ASCQ in order to determine if the tape needs 10870 * to be ejected 10871 */ 10872 10873 static int 10874 st_check_asc_ascq(struct scsi_tape *un) 10875 { 10876 struct scsi_extended_sense *sensep = ST_RQSENSE; 10877 struct tape_failure_code *code; 10878 10879 for (code = st_tape_failure_code; code->key != 0xff; code++) { 10880 if ((code->key == sensep->es_key) && 10881 (code->add_code == sensep->es_add_code) && 10882 (code->qual_code == sensep->es_qual_code)) 10883 return (1); 10884 } 10885 return (0); 10886 } 10887 10888 /* 10889 * st_logpage_supported() sends a Log Sense command with 10890 * page code = 0 = Supported Log Pages Page to the device, 10891 * to see whether the page 'page' is supported. 10892 * Return values are: 10893 * -1 if the Log Sense command fails 10894 * 0 if page is not supported 10895 * 1 if page is supported 10896 */ 10897 10898 static int 10899 st_logpage_supported(dev_t dev, uchar_t page) 10900 { 10901 uchar_t *sp, *sensep; 10902 unsigned length; 10903 struct uscsi_cmd *com; 10904 int rval; 10905 char cdb[CDB_GROUP1] = { 10906 SCMD_LOG_SENSE_G1, 10907 0, 10908 SUPPORTED_LOG_PAGES_PAGE, 10909 0, 10910 0, 10911 0, 10912 0, 10913 0, 10914 (char)LOG_SENSE_LENGTH, 10915 0 10916 }; 10917 10918 GET_SOFT_STATE(dev); 10919 ASSERT(mutex_owned(ST_MUTEX)); 10920 10921 com = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 10922 sensep = kmem_zalloc(LOG_SENSE_LENGTH, KM_SLEEP); 10923 10924 com->uscsi_cdb = cdb; 10925 com->uscsi_cdblen = CDB_GROUP1; 10926 com->uscsi_bufaddr = (caddr_t)sensep; 10927 com->uscsi_buflen = LOG_SENSE_LENGTH; 10928 com->uscsi_flags = 10929 USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ; 10930 com->uscsi_timeout = un->un_dp->non_motion_timeout; 10931 rval = st_ioctl_cmd(dev, com, FKIOCTL); 10932 if (rval || com->uscsi_status) { 10933 /* uscsi-command failed */ 10934 rval = -1; 10935 } else { 10936 10937 sp = sensep + 3; 10938 10939 for (length = *sp++; length > 0; length--, sp++) { 10940 10941 if (*sp == page) { 10942 rval = 1; 10943 break; 10944 } 10945 } 10946 } 10947 kmem_free(com, sizeof (struct uscsi_cmd)); 10948 kmem_free(sensep, LOG_SENSE_LENGTH); 10949 return (rval); 10950 } 10951 10952 10953 /* 10954 * st_check_clean_bit() gets the status of the tape's cleaning bit. 10955 * 10956 * If the device does support the TapeAlert log page, then the cleaning bit 10957 * information will be read from this page. Otherwise we will see if one of 10958 * ST_CLN_TYPE_1, ST_CLN_TYPE_2 or ST_CLN_TYPE_3 is set in the properties of 10959 * the device, which means, that we can get the cleaning bit information via 10960 * a RequestSense command. 10961 * If both methods of getting cleaning bit information are not supported 10962 * st_check_clean_bit() will return with 0. Otherwise st_check_clean_bit() 10963 * returns with 10964 * - MTF_TAPE_CLN_SUPPORTED if cleaning bit is not set or 10965 * - MTF_TAPE_CLN_SUPPORTED | MTF_TAPE_HEAD_DIRTY if cleaning bit is set. 10966 * If the call to st_ioctl_cmd() to do the Log Sense or the Request Sense 10967 * command fails, or if the amount of Request Sense data is not enough, then 10968 * st_check_clean_bit() returns with -1. 10969 */ 10970 10971 static int 10972 st_check_clean_bit(dev_t dev) 10973 { 10974 int rval = 0; 10975 10976 GET_SOFT_STATE(dev); 10977 10978 ASSERT(mutex_owned(ST_MUTEX)); 10979 10980 if (un->un_HeadClean & TAPE_ALERT_NOT_SUPPORTED) { 10981 return (rval); 10982 } 10983 10984 if (un->un_HeadClean == TAPE_ALERT_SUPPORT_UNKNOWN) { 10985 10986 rval = st_logpage_supported(dev, TAPE_SEQUENTIAL_PAGE); 10987 if (rval == 1) { 10988 10989 un->un_HeadClean |= TAPE_SEQUENTIAL_SUPPORTED; 10990 } 10991 10992 rval = st_logpage_supported(dev, TAPE_ALERT_PAGE); 10993 if (rval == 1) { 10994 10995 un->un_HeadClean |= TAPE_ALERT_SUPPORTED; 10996 } 10997 10998 if (un->un_HeadClean == TAPE_ALERT_SUPPORT_UNKNOWN) { 10999 11000 un->un_HeadClean = TAPE_ALERT_NOT_SUPPORTED; 11001 } 11002 } 11003 11004 rval = 0; 11005 11006 if (un->un_HeadClean & TAPE_SEQUENTIAL_SUPPORTED) { 11007 11008 rval = st_check_sequential_clean_bit(dev); 11009 } 11010 11011 if ((rval <= 0) && (un->un_HeadClean & TAPE_ALERT_SUPPORTED)) { 11012 11013 rval = st_check_alert_flags(dev); 11014 } 11015 11016 if ((rval <= 0) && (un->un_dp->options & ST_CLN_MASK)) { 11017 11018 rval = st_check_sense_clean_bit(dev); 11019 } 11020 11021 if (rval < 0) { 11022 return (rval); 11023 } 11024 11025 /* 11026 * If found a supported means to check need to clean. 11027 */ 11028 if (rval & MTF_TAPE_CLN_SUPPORTED) { 11029 11030 /* 11031 * head needs to be cleaned. 11032 */ 11033 if (rval & MTF_TAPE_HEAD_DIRTY) { 11034 11035 /* 11036 * Print log message only first time 11037 * found needing cleaned. 11038 */ 11039 if ((un->un_HeadClean & TAPE_PREVIOUSLY_DIRTY) == 0) { 11040 11041 scsi_log(ST_DEVINFO, st_label, CE_WARN, 11042 "Periodic head cleaning required"); 11043 11044 un->un_HeadClean |= TAPE_PREVIOUSLY_DIRTY; 11045 } 11046 11047 } else { 11048 11049 un->un_HeadClean &= ~TAPE_PREVIOUSLY_DIRTY; 11050 } 11051 } 11052 11053 return (rval); 11054 } 11055 11056 11057 static int 11058 st_check_sequential_clean_bit(dev_t dev) 11059 { 11060 int rval; 11061 int ix; 11062 ushort_t parameter; 11063 struct uscsi_cmd *cmd; 11064 struct log_sequential_page *sp; 11065 struct log_sequential_page_parameter *prm; 11066 char cdb[CDB_GROUP1] = { 11067 SCMD_LOG_SENSE_G1, 11068 0, 11069 TAPE_SEQUENTIAL_PAGE | CURRENT_CUMULATIVE_VALUES, 11070 0, 11071 0, 11072 0, 11073 0, 11074 (char)(sizeof (struct log_sequential_page) >> 8), 11075 (char)(sizeof (struct log_sequential_page)), 11076 0 11077 }; 11078 11079 GET_SOFT_STATE(dev); 11080 11081 cmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 11082 sp = kmem_zalloc(sizeof (struct log_sequential_page), KM_SLEEP); 11083 11084 cmd->uscsi_flags = 11085 USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ; 11086 cmd->uscsi_timeout = un->un_dp->non_motion_timeout; 11087 cmd->uscsi_cdb = cdb; 11088 cmd->uscsi_cdblen = CDB_GROUP1; 11089 cmd->uscsi_bufaddr = (caddr_t)sp; 11090 cmd->uscsi_buflen = sizeof (struct log_sequential_page); 11091 11092 rval = st_ioctl_cmd(dev, cmd, FKIOCTL); 11093 11094 if (rval || cmd->uscsi_status || cmd->uscsi_resid) { 11095 11096 rval = -1; 11097 11098 } else if (sp->log_page.code != TAPE_SEQUENTIAL_PAGE) { 11099 11100 rval = -1; 11101 } 11102 11103 prm = &sp->param[0]; 11104 11105 for (ix = 0; rval == 0 && ix < TAPE_SEQUENTIAL_PAGE_PARA; ix++) { 11106 11107 if (prm->log_param.length == 0) { 11108 break; 11109 } 11110 11111 parameter = (((prm->log_param.pc_hi << 8) & 0xff00) + 11112 (prm->log_param.pc_lo & 0xff)); 11113 11114 if (parameter == SEQUENTIAL_NEED_CLN) { 11115 11116 rval = MTF_TAPE_CLN_SUPPORTED; 11117 if (prm->param_value[prm->log_param.length - 1]) { 11118 11119 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11120 "sequential log says head dirty\n"); 11121 rval |= MTF_TAPE_HEAD_DIRTY; 11122 } 11123 } 11124 prm = (struct log_sequential_page_parameter *) 11125 &prm->param_value[prm->log_param.length]; 11126 } 11127 11128 kmem_free(cmd, sizeof (struct uscsi_cmd)); 11129 kmem_free(sp, sizeof (struct log_sequential_page)); 11130 11131 return (rval); 11132 } 11133 11134 11135 static int 11136 st_check_alert_flags(dev_t dev) 11137 { 11138 struct st_tape_alert *ta; 11139 struct uscsi_cmd *com; 11140 unsigned ix, length; 11141 int rval; 11142 tape_alert_flags flag; 11143 char cdb[CDB_GROUP1] = { 11144 SCMD_LOG_SENSE_G1, 11145 0, 11146 TAPE_ALERT_PAGE | CURRENT_THRESHOLD_VALUES, 11147 0, 11148 0, 11149 0, 11150 0, 11151 (char)(sizeof (struct st_tape_alert) >> 8), 11152 (char)(sizeof (struct st_tape_alert)), 11153 0 11154 }; 11155 11156 GET_SOFT_STATE(dev); 11157 11158 com = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 11159 ta = kmem_zalloc(sizeof (struct st_tape_alert), KM_SLEEP); 11160 11161 com->uscsi_cdb = cdb; 11162 com->uscsi_cdblen = CDB_GROUP1; 11163 com->uscsi_bufaddr = (caddr_t)ta; 11164 com->uscsi_buflen = sizeof (struct st_tape_alert); 11165 com->uscsi_flags = 11166 USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ; 11167 com->uscsi_timeout = un->un_dp->non_motion_timeout; 11168 11169 rval = st_ioctl_cmd(dev, com, FKIOCTL); 11170 11171 if (rval || com->uscsi_status || com->uscsi_resid) { 11172 11173 rval = -1; /* uscsi-command failed */ 11174 11175 } else if (ta->log_page.code != TAPE_ALERT_PAGE) { 11176 11177 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11178 "Not Alert Log Page returned 0x%X\n", ta->log_page.code); 11179 rval = -1; 11180 } 11181 11182 length = (ta->log_page.length_hi << 8) + ta->log_page.length_lo; 11183 11184 11185 if (length != TAPE_ALERT_PARAMETER_LENGTH) { 11186 11187 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11188 "TapeAlert length %d\n", length); 11189 } 11190 11191 11192 for (ix = 0; ix < TAPE_ALERT_MAX_PARA; ix++) { 11193 11194 /* 11195 * if rval is bad before the first pass don't bother 11196 */ 11197 if (ix == 0 && rval != 0) { 11198 11199 break; 11200 } 11201 11202 flag = ((ta->param[ix].log_param.pc_hi << 8) + 11203 ta->param[ix].log_param.pc_lo); 11204 11205 if ((ta->param[ix].param_value & 1) == 0) { 11206 continue; 11207 } 11208 /* 11209 * check to see if current parameter is of interest. 11210 * CLEAN_FOR_ERRORS is vendor specific to 9840 9940 stk's. 11211 */ 11212 if ((flag == TAF_CLEAN_NOW) || 11213 (flag == TAF_CLEAN_PERIODIC) || 11214 ((flag == CLEAN_FOR_ERRORS) && 11215 (un->un_dp->type == ST_TYPE_STK9840))) { 11216 11217 rval = MTF_TAPE_CLN_SUPPORTED; 11218 11219 11220 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11221 "alert_page drive needs clean %d\n", flag); 11222 un->un_HeadClean |= TAPE_ALERT_STILL_DIRTY; 11223 rval |= MTF_TAPE_HEAD_DIRTY; 11224 11225 } else if (flag == TAF_CLEANING_MEDIA) { 11226 11227 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11228 "alert_page drive was cleaned\n"); 11229 un->un_HeadClean &= ~TAPE_ALERT_STILL_DIRTY; 11230 } 11231 11232 } 11233 11234 /* 11235 * Report it as dirty till we see it cleaned 11236 */ 11237 if (un->un_HeadClean & TAPE_ALERT_STILL_DIRTY) { 11238 11239 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11240 "alert_page still dirty\n"); 11241 rval |= MTF_TAPE_HEAD_DIRTY; 11242 } 11243 11244 kmem_free(com, sizeof (struct uscsi_cmd)); 11245 kmem_free(ta, sizeof (struct st_tape_alert)); 11246 11247 return (rval); 11248 } 11249 11250 11251 static int 11252 st_check_sense_clean_bit(dev_t dev) 11253 { 11254 uchar_t *sensep; 11255 char cdb[CDB_GROUP0]; 11256 struct uscsi_cmd *com; 11257 ushort_t byte_pos; 11258 uchar_t bit_mask; 11259 unsigned length; 11260 int index; 11261 int rval; 11262 11263 GET_SOFT_STATE(dev); 11264 11265 /* 11266 * Since this tape does not support Tape Alert, 11267 * we now try to get the cleanbit status via 11268 * Request Sense. 11269 */ 11270 11271 if ((un->un_dp->options & ST_CLN_MASK) == ST_CLN_TYPE_1) { 11272 11273 index = 0; 11274 11275 } else if ((un->un_dp->options & ST_CLN_MASK) == ST_CLN_TYPE_2) { 11276 11277 index = 1; 11278 11279 } else if ((un->un_dp->options & ST_CLN_MASK) == ST_CLN_TYPE_3) { 11280 11281 index = 2; 11282 11283 } else { 11284 11285 return (-1); 11286 } 11287 11288 byte_pos = st_cln_bit_position[index].cln_bit_byte; 11289 bit_mask = st_cln_bit_position[index].cln_bit_mask; 11290 length = byte_pos + 1; 11291 11292 com = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 11293 sensep = kmem_zalloc(length, KM_SLEEP); 11294 11295 cdb[0] = SCMD_REQUEST_SENSE; 11296 cdb[1] = 0; 11297 cdb[2] = 0; 11298 cdb[3] = 0; 11299 cdb[4] = (char)length; 11300 cdb[5] = 0; 11301 11302 com->uscsi_cdb = cdb; 11303 com->uscsi_cdblen = CDB_GROUP0; 11304 com->uscsi_bufaddr = (caddr_t)sensep; 11305 com->uscsi_buflen = length; 11306 com->uscsi_flags = 11307 USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ; 11308 com->uscsi_timeout = un->un_dp->non_motion_timeout; 11309 11310 rval = st_ioctl_cmd(dev, com, FKIOCTL); 11311 11312 if (rval || com->uscsi_status || com->uscsi_resid) { 11313 11314 rval = -1; 11315 11316 } else { 11317 11318 rval = MTF_TAPE_CLN_SUPPORTED; 11319 if ((sensep[byte_pos] & bit_mask) == bit_mask) { 11320 11321 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11322 "sense data says head dirty\n"); 11323 rval |= MTF_TAPE_HEAD_DIRTY; 11324 } 11325 } 11326 11327 kmem_free(com, sizeof (struct uscsi_cmd)); 11328 kmem_free(sensep, length); 11329 return (rval); 11330 } 11331 11332 /* 11333 * st_clear_unit_attention 11334 * 11335 * run test unit ready's to clear out outstanding 11336 * unit attentions. 11337 * returns zero for SUCCESS or the errno from st_cmd call 11338 */ 11339 static int 11340 st_clear_unit_attentions(dev_t dev_instance, int max_trys) 11341 { 11342 int i = 0; 11343 int rval; 11344 11345 do { 11346 rval = st_cmd(dev_instance, SCMD_TEST_UNIT_READY, 0, SYNC_CMD); 11347 } while ((rval != 0) && (rval != ENXIO) && (++i < max_trys)); 11348 return (rval); 11349 } 11350 11351 static void 11352 st_calculate_timeouts(struct scsi_tape *un) 11353 { 11354 if (un->un_dp->non_motion_timeout == 0) { 11355 if (un->un_dp->options & ST_LONG_TIMEOUTS) { 11356 un->un_dp->non_motion_timeout = 11357 st_io_time * st_long_timeout_x; 11358 } else { 11359 un->un_dp->non_motion_timeout = (ushort_t)st_io_time; 11360 } 11361 } 11362 11363 if (un->un_dp->io_timeout == 0) { 11364 if (un->un_dp->options & ST_LONG_TIMEOUTS) { 11365 un->un_dp->io_timeout = st_io_time * st_long_timeout_x; 11366 } else { 11367 un->un_dp->io_timeout = (ushort_t)st_io_time; 11368 } 11369 } 11370 11371 if (un->un_dp->rewind_timeout == 0) { 11372 if (un->un_dp->options & ST_LONG_TIMEOUTS) { 11373 un->un_dp->rewind_timeout = 11374 st_space_time * st_long_timeout_x; 11375 } else { 11376 un->un_dp->rewind_timeout = (ushort_t)st_space_time; 11377 } 11378 } 11379 11380 if (un->un_dp->space_timeout == 0) { 11381 if (un->un_dp->options & ST_LONG_TIMEOUTS) { 11382 un->un_dp->space_timeout = 11383 st_space_time * st_long_timeout_x; 11384 } else { 11385 un->un_dp->space_timeout = (ushort_t)st_space_time; 11386 } 11387 } 11388 11389 if (un->un_dp->load_timeout == 0) { 11390 if (un->un_dp->options & ST_LONG_TIMEOUTS) { 11391 un->un_dp->load_timeout = 11392 st_space_time * st_long_timeout_x; 11393 } else { 11394 un->un_dp->load_timeout = (ushort_t)st_space_time; 11395 } 11396 } 11397 11398 if (un->un_dp->unload_timeout == 0) { 11399 if (un->un_dp->options & ST_LONG_TIMEOUTS) { 11400 un->un_dp->unload_timeout = 11401 st_space_time * st_long_timeout_x; 11402 } else { 11403 un->un_dp->unload_timeout = (ushort_t)st_space_time; 11404 } 11405 } 11406 11407 if (un->un_dp->erase_timeout == 0) { 11408 if (un->un_dp->options & ST_LONG_ERASE) { 11409 un->un_dp->erase_timeout = 11410 st_space_time * st_long_space_time_x; 11411 } else { 11412 un->un_dp->erase_timeout = (ushort_t)st_space_time; 11413 } 11414 } 11415 } 11416 11417 11418 /*ARGSUSED*/ 11419 static writablity 11420 st_is_not_wormable(struct scsi_tape *un) 11421 { 11422 return (RDWR); 11423 } 11424 11425 static writablity 11426 st_is_hp_lto_tape_worm(struct scsi_tape *un) 11427 { 11428 writablity wrt; 11429 11430 11431 /* Mode sense should be current */ 11432 switch (un->un_mspl->media_type) { 11433 case 0x00: 11434 switch (un->un_mspl->density) { 11435 case 0x40: 11436 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11437 "Drive has standard Gen I media loaded\n"); 11438 break; 11439 case 0x42: 11440 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11441 "Drive has standard Gen II media loaded\n"); 11442 break; 11443 case 0x44: 11444 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11445 "Drive has standard Gen III media loaded\n"); 11446 break; 11447 case 0x46: 11448 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11449 "Drive has standard Gen IV media loaded\n"); 11450 break; 11451 default: 11452 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11453 "Drive has standard unknown 0x%X media loaded\n", 11454 un->un_mspl->density); 11455 } 11456 wrt = RDWR; 11457 break; 11458 case 0x01: 11459 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11460 "Drive has WORM medium loaded\n"); 11461 wrt = WORM; 11462 break; 11463 case 0x80: 11464 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11465 "Drive has CD-ROM emulation medium loaded\n"); 11466 wrt = WORM; 11467 break; 11468 default: 11469 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11470 "Drive has an unexpected medium type 0x%X loaded\n", 11471 un->un_mspl->media_type); 11472 wrt = RDWR; 11473 } 11474 11475 return (wrt); 11476 } 11477 11478 #define LTO_REQ_INQUIRY 44 11479 static writablity 11480 st_is_hp_lto_worm(struct scsi_tape *un) 11481 { 11482 char *buf; 11483 int result; 11484 writablity wrt; 11485 11486 buf = kmem_zalloc(LTO_REQ_INQUIRY, KM_SLEEP); 11487 11488 result = st_get_special_inquiry(un, LTO_REQ_INQUIRY, buf, 0); 11489 11490 if (result != 0) { 11491 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11492 "Read Standard Inquiry for WORM support failed"); 11493 wrt = ERROR; 11494 } else if ((buf[40] & 1) == 0) { 11495 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11496 "Drive is NOT WORMable\n"); 11497 /* This drive doesn't support it so don't check again */ 11498 un->un_dp->options &= ~ST_WORMABLE; 11499 wrt = RDWR; 11500 un->un_wormable = st_is_not_wormable; 11501 } else { 11502 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11503 "Drive supports WORM version %d\n", buf[40] >> 1); 11504 un->un_wormable = st_is_hp_lto_tape_worm; 11505 wrt = un->un_wormable(un); 11506 } 11507 11508 kmem_free(buf, LTO_REQ_INQUIRY); 11509 11510 /* 11511 * If drive doesn't support it no point in checking further. 11512 */ 11513 return (wrt); 11514 } 11515 11516 static writablity 11517 st_is_t10_worm_device(struct scsi_tape *un) 11518 { 11519 writablity wrt; 11520 11521 if (un->un_mspl->media_type == 0x3c) { 11522 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11523 "Drive has WORM media loaded\n"); 11524 wrt = WORM; 11525 } else { 11526 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11527 "Drive has non WORM media loaded\n"); 11528 wrt = RDWR; 11529 } 11530 return (wrt); 11531 } 11532 11533 #define SEQ_CAP_PAGE (char)0xb0 11534 static writablity 11535 st_is_t10_worm(struct scsi_tape *un) 11536 { 11537 char *buf; 11538 int result; 11539 writablity wrt; 11540 11541 buf = kmem_zalloc(6, KM_SLEEP); 11542 11543 result = st_get_special_inquiry(un, 6, buf, SEQ_CAP_PAGE); 11544 11545 if (result != 0) { 11546 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11547 "Read Vitial Inquiry for Sequental Capability" 11548 " WORM support failed %x", result); 11549 wrt = ERROR; 11550 } else if ((buf[4] & 1) == 0) { 11551 ASSERT(buf[1] == SEQ_CAP_PAGE); 11552 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11553 "Drive is NOT WORMable\n"); 11554 /* This drive doesn't support it so don't check again */ 11555 un->un_dp->options &= ~ST_WORMABLE; 11556 wrt = RDWR; 11557 un->un_wormable = st_is_not_wormable; 11558 } else { 11559 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11560 "Drive supports WORM\n"); 11561 un->un_wormable = st_is_t10_worm_device; 11562 wrt = un->un_wormable(un); 11563 } 11564 11565 kmem_free(buf, 6); 11566 11567 return (wrt); 11568 } 11569 11570 11571 #define STK_REQ_SENSE 26 11572 11573 static writablity 11574 st_is_stk_worm(struct scsi_tape *un) 11575 { 11576 char cdb[CDB_GROUP0] = {SCMD_REQUEST_SENSE, 0, 0, 0, STK_REQ_SENSE, 0}; 11577 struct scsi_extended_sense *sense; 11578 struct uscsi_cmd *cmd; 11579 char *buf; 11580 int result; 11581 writablity wrt; 11582 11583 cmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 11584 buf = kmem_alloc(STK_REQ_SENSE, KM_SLEEP); 11585 sense = (struct scsi_extended_sense *)buf; 11586 11587 cmd->uscsi_flags = USCSI_READ; 11588 cmd->uscsi_timeout = un->un_dp->non_motion_timeout; 11589 cmd->uscsi_cdb = &cdb[0]; 11590 cmd->uscsi_bufaddr = buf; 11591 cmd->uscsi_buflen = STK_REQ_SENSE; 11592 cmd->uscsi_cdblen = CDB_GROUP0; 11593 cmd->uscsi_rqlen = 0; 11594 cmd->uscsi_rqbuf = NULL; 11595 11596 result = st_ioctl_cmd(un->un_dev, cmd, FKIOCTL); 11597 11598 if (result != 0 || cmd->uscsi_status != 0) { 11599 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11600 "Request Sense for WORM failed"); 11601 wrt = RDWR; 11602 } else if (sense->es_add_len + 8 < 24) { 11603 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11604 "Drive didn't send enough sense data for WORM byte %d\n", 11605 sense->es_add_len + 8); 11606 wrt = RDWR; 11607 un->un_wormable = st_is_not_wormable; 11608 } else if ((buf[24]) & 0x02) { 11609 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11610 "Drive has WORM tape loaded\n"); 11611 wrt = WORM; 11612 un->un_wormable = st_is_stk_worm; 11613 } else { 11614 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11615 "Drive has normal tape loaded\n"); 11616 wrt = RDWR; 11617 un->un_wormable = st_is_stk_worm; 11618 } 11619 11620 kmem_free(buf, STK_REQ_SENSE); 11621 kmem_free(cmd, sizeof (struct uscsi_cmd)); 11622 return (wrt); 11623 } 11624 11625 #define DLT_INQ_SZ 44 11626 11627 static writablity 11628 st_is_dlt_tape_worm(struct scsi_tape *un) 11629 { 11630 caddr_t buf; 11631 int result; 11632 writablity wrt; 11633 11634 buf = kmem_alloc(DLT_INQ_SZ, KM_SLEEP); 11635 11636 /* Read Attribute Media Type */ 11637 11638 result = st_read_attributes(un, 0x0408, buf, 10); 11639 11640 /* 11641 * If this quantum drive is attached via an HBA that cannot 11642 * support thr read attributes command return error in the 11643 * hope that someday they will support the t10 method. 11644 */ 11645 if (result == EINVAL && un->un_max_cdb_sz < CDB_GROUP4) { 11646 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 11647 "Read Attribute Command for WORM Media detection is not " 11648 "supported on the HBA that this drive is attached to."); 11649 wrt = RDWR; 11650 un->un_wormable = st_is_not_wormable; 11651 goto out; 11652 } 11653 11654 if (result != 0) { 11655 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11656 "Read Attribute Command for WORM Media returned 0x%x", 11657 result); 11658 wrt = RDWR; 11659 un->un_dp->options &= ~ST_WORMABLE; 11660 goto out; 11661 } 11662 11663 if ((uchar_t)buf[9] == 0x80) { 11664 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11665 "Drive media is WORM\n"); 11666 wrt = WORM; 11667 } else { 11668 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11669 "Drive media is not WORM Media 0x%x\n", (uchar_t)buf[9]); 11670 wrt = RDWR; 11671 } 11672 11673 out: 11674 kmem_free(buf, DLT_INQ_SZ); 11675 return (wrt); 11676 } 11677 11678 static writablity 11679 st_is_dlt_worm(struct scsi_tape *un) 11680 { 11681 caddr_t buf; 11682 int result; 11683 writablity wrt; 11684 11685 buf = kmem_alloc(DLT_INQ_SZ, KM_SLEEP); 11686 11687 result = st_get_special_inquiry(un, DLT_INQ_SZ, buf, 0xC0); 11688 11689 if (result != 0) { 11690 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11691 "Read Vendor Specific Inquiry for WORM support failed"); 11692 wrt = RDWR; 11693 goto out; 11694 } 11695 11696 if ((buf[2] & 1) == 0) { 11697 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11698 "Drive is not WORMable\n"); 11699 wrt = RDWR; 11700 un->un_dp->options &= ~ST_WORMABLE; 11701 un->un_wormable = st_is_not_wormable; 11702 goto out; 11703 } else { 11704 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11705 "Drive is WORMable\n"); 11706 un->un_wormable = st_is_dlt_tape_worm; 11707 wrt = un->un_wormable(un); 11708 } 11709 out: 11710 kmem_free(buf, DLT_INQ_SZ); 11711 11712 return (wrt); 11713 } 11714 11715 typedef struct { 11716 struct modeheader_seq header; 11717 #if defined(_BIT_FIELDS_LTOH) /* X86 */ 11718 uchar_t pagecode :6, 11719 :2; 11720 uchar_t page_len; 11721 uchar_t syslogalive :2, 11722 device :1, 11723 abs :1, 11724 ulpbot :1, 11725 prth :1, 11726 ponej :1, 11727 ait :1; 11728 uchar_t span; 11729 11730 uchar_t :6, 11731 worm :1, 11732 mic :1; 11733 uchar_t worm_cap :1, 11734 :7; 11735 uint32_t :32; 11736 #else /* SPARC */ 11737 uchar_t :2, 11738 pagecode :6; 11739 uchar_t page_len; 11740 uchar_t ait :1, 11741 device :1, 11742 abs :1, 11743 ulpbot :1, 11744 prth :1, 11745 ponej :1, 11746 syslogalive :2; 11747 uchar_t span; 11748 uchar_t mic :1, 11749 worm :1, 11750 :6; 11751 uchar_t :7, 11752 worm_cap :1; 11753 uint32_t :32; 11754 #endif 11755 }ait_dev_con; 11756 11757 #define AIT_DEV_PAGE 0x31 11758 static writablity 11759 st_is_sony_worm(struct scsi_tape *un) 11760 { 11761 int result; 11762 writablity wrt; 11763 ait_dev_con *ait_conf; 11764 11765 ait_conf = kmem_zalloc(sizeof (ait_dev_con), KM_SLEEP); 11766 11767 result = st_gen_mode_sense(un, AIT_DEV_PAGE, 11768 (struct seq_mode *)ait_conf, sizeof (ait_dev_con)); 11769 11770 if (result == 0) { 11771 11772 if (ait_conf->pagecode != AIT_DEV_PAGE) { 11773 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11774 "returned page 0x%x not 0x%x AIT_DEV_PAGE\n", 11775 ait_conf->pagecode, AIT_DEV_PAGE); 11776 wrt = RDWR; 11777 un->un_wormable = st_is_not_wormable; 11778 11779 } else if (ait_conf->worm_cap) { 11780 11781 un->un_wormable = st_is_sony_worm; 11782 11783 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11784 "Drives is WORMable\n"); 11785 if (ait_conf->worm) { 11786 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11787 "Media is WORM\n"); 11788 wrt = WORM; 11789 } else { 11790 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11791 "Media is not WORM\n"); 11792 wrt = RDWR; 11793 } 11794 11795 } else { 11796 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11797 "Drives not is WORMable\n"); 11798 wrt = RDWR; 11799 /* No further checking required */ 11800 un->un_dp->options &= ~ST_WORMABLE; 11801 } 11802 11803 } else { 11804 11805 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11806 "AIT device config mode sense page read command failed" 11807 " result = %d ", result); 11808 wrt = ERROR; 11809 un->un_wormable = st_is_not_wormable; 11810 } 11811 11812 kmem_free(ait_conf, sizeof (ait_dev_con)); 11813 return (wrt); 11814 } 11815 11816 static writablity 11817 st_is_drive_worm(struct scsi_tape *un) 11818 { 11819 writablity wrt; 11820 11821 switch (un->un_dp->type) { 11822 case MT_ISDLT: 11823 wrt = st_is_dlt_worm(un); 11824 break; 11825 11826 case MT_ISSTK9840: 11827 wrt = st_is_stk_worm(un); 11828 break; 11829 11830 case MT_IS8MM: 11831 case MT_ISAIT: 11832 wrt = st_is_sony_worm(un); 11833 break; 11834 11835 case MT_LTO: 11836 if (strncmp("HP ", un->un_dp->vid, 3) == 0) { 11837 wrt = st_is_hp_lto_worm(un); 11838 } else { 11839 wrt = st_is_t10_worm(un); 11840 } 11841 break; 11842 11843 default: 11844 wrt = ERROR; 11845 break; 11846 } 11847 11848 /* 11849 * If any of the above failed try the t10 standard method. 11850 */ 11851 if (wrt == ERROR) { 11852 wrt = st_is_t10_worm(un); 11853 } 11854 11855 /* 11856 * Unknown method for detecting WORM media. 11857 */ 11858 if (wrt == ERROR) { 11859 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11860 "Unknown method for WORM media detection\n"); 11861 wrt = RDWR; 11862 un->un_dp->options &= ~ST_WORMABLE; 11863 } 11864 11865 return (wrt); 11866 } 11867 11868 static int 11869 st_read_attributes(struct scsi_tape *un, uint16_t attribute, caddr_t buf, 11870 size_t size) 11871 { 11872 uchar_t cdb[CDB_GROUP4]; 11873 struct uscsi_cmd *cmd; 11874 int result; 11875 11876 cdb[0] = SCMD_READ_ATTRIBUTE; 11877 cdb[1] = 0; 11878 cdb[2] = 0; 11879 cdb[3] = 0; 11880 cdb[4] = 0; 11881 cdb[5] = 0; 11882 cdb[6] = 0; 11883 cdb[7] = 0; 11884 cdb[8] = (uchar_t)(attribute >> 8); 11885 cdb[9] = (uchar_t)(attribute); 11886 cdb[10] = (uchar_t)(size >> 24); 11887 cdb[11] = (uchar_t)(size >> 16); 11888 cdb[12] = (uchar_t)(size >> 8); 11889 cdb[13] = (uchar_t)(size); 11890 cdb[14] = 0; 11891 cdb[15] = 0; 11892 11893 cmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 11894 11895 cmd->uscsi_flags = USCSI_READ | USCSI_DIAGNOSE; 11896 cmd->uscsi_timeout = un->un_dp->non_motion_timeout; 11897 cmd->uscsi_cdb = (caddr_t)&cdb[0]; 11898 cmd->uscsi_bufaddr = (caddr_t)buf; 11899 cmd->uscsi_buflen = size; 11900 cmd->uscsi_cdblen = sizeof (cdb); 11901 11902 result = st_ioctl_cmd(un->un_dev, cmd, FKIOCTL); 11903 11904 if (result != 0 || cmd->uscsi_status != 0) { 11905 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11906 "st_read_attribute failed: result %d status %d\n", 11907 result, cmd->uscsi_status); 11908 if (result == 0) { 11909 result = EIO; 11910 } 11911 } 11912 11913 kmem_free(cmd, sizeof (cdb)); 11914 return (result); 11915 } 11916 11917 static int 11918 st_get_special_inquiry(struct scsi_tape *un, uchar_t size, caddr_t dest, 11919 uchar_t page) 11920 { 11921 char cdb[CDB_GROUP0]; 11922 struct scsi_extended_sense *sense; 11923 struct uscsi_cmd *cmd; 11924 int result; 11925 11926 cdb[0] = SCMD_INQUIRY; 11927 cdb[1] = page ? 1 : 0; 11928 cdb[2] = page; 11929 cdb[3] = 0; 11930 cdb[4] = size; 11931 cdb[5] = 0; 11932 11933 cmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 11934 sense = kmem_alloc(sizeof (struct scsi_extended_sense), KM_SLEEP); 11935 11936 cmd->uscsi_flags = USCSI_READ | USCSI_RQENABLE; 11937 cmd->uscsi_timeout = un->un_dp->non_motion_timeout; 11938 cmd->uscsi_cdb = &cdb[0]; 11939 cmd->uscsi_bufaddr = dest; 11940 cmd->uscsi_buflen = size; 11941 cmd->uscsi_cdblen = CDB_GROUP0; 11942 cmd->uscsi_rqlen = sizeof (struct scsi_extended_sense); 11943 cmd->uscsi_rqbuf = (caddr_t)sense; 11944 11945 result = st_ioctl_cmd(un->un_dev, cmd, FKIOCTL); 11946 11947 if (result != 0 || cmd->uscsi_status != 0) { 11948 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11949 "st_get_special_inquiry() failed for page %x", page); 11950 if (result == 0) { 11951 result = EIO; 11952 } 11953 } 11954 11955 kmem_free(sense, sizeof (struct scsi_extended_sense)); 11956 kmem_free(cmd, sizeof (struct uscsi_cmd)); 11957 11958 return (result); 11959 } 11960 11961 11962 #if defined(__i386) || defined(__amd64) 11963 11964 /* 11965 * release contig_mem and wake up waiting thread, if any 11966 */ 11967 static void 11968 st_release_contig_mem(struct scsi_tape *un, struct contig_mem *cp) 11969 { 11970 mutex_enter(ST_MUTEX); 11971 11972 cp->cm_next = un->un_contig_mem; 11973 un->un_contig_mem = cp; 11974 un->un_contig_mem_available_num++; 11975 cv_broadcast(&un->un_contig_mem_cv); 11976 11977 mutex_exit(ST_MUTEX); 11978 } 11979 11980 /* 11981 * St_get_contig_mem will return a contig_mem if there is one available 11982 * in current system. Otherwise, it will try to alloc one, if the total 11983 * number of contig_mem is within st_max_contig_mem_num. 11984 * It will sleep, if allowed by caller or return NULL, if no contig_mem 11985 * is available for now. 11986 */ 11987 static struct contig_mem * 11988 st_get_contig_mem(struct scsi_tape *un, size_t len, int alloc_flags) 11989 { 11990 size_t rlen; 11991 struct contig_mem *cp = NULL; 11992 ddi_acc_handle_t acc_hdl; 11993 caddr_t addr; 11994 int big_enough = 0; 11995 int (*dma_alloc_cb)() = (alloc_flags == KM_SLEEP) ? 11996 DDI_DMA_SLEEP : DDI_DMA_DONTWAIT; 11997 11998 /* Try to get one available contig_mem */ 11999 mutex_enter(ST_MUTEX); 12000 if (un->un_contig_mem_available_num > 0) { 12001 ST_GET_CONTIG_MEM_HEAD(un, cp, len, big_enough); 12002 } else if (un->un_contig_mem_total_num < st_max_contig_mem_num) { 12003 /* 12004 * we failed to get one. we're going to 12005 * alloc one more contig_mem for this I/O 12006 */ 12007 mutex_exit(ST_MUTEX); 12008 cp = (struct contig_mem *)kmem_zalloc( 12009 sizeof (struct contig_mem) + biosize(), 12010 alloc_flags); 12011 if (cp == NULL) { 12012 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 12013 "alloc contig_mem failure\n"); 12014 return (NULL); /* cannot get one */ 12015 } 12016 cp->cm_bp = (struct buf *) 12017 (((caddr_t)cp) + sizeof (struct contig_mem)); 12018 bioinit(cp->cm_bp); 12019 mutex_enter(ST_MUTEX); 12020 un->un_contig_mem_total_num++; /* one more available */ 12021 } else { 12022 /* 12023 * we failed to get one and we're NOT allowed to 12024 * alloc more contig_mem 12025 */ 12026 if (alloc_flags == KM_SLEEP) { 12027 while (un->un_contig_mem_available_num <= 0) { 12028 cv_wait(&un->un_contig_mem_cv, 12029 ST_MUTEX); 12030 } 12031 ST_GET_CONTIG_MEM_HEAD(un, cp, len, big_enough); 12032 } else { 12033 mutex_exit(ST_MUTEX); 12034 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 12035 "alloc contig_mem failure\n"); 12036 return (NULL); /* cannot get one */ 12037 } 12038 } 12039 mutex_exit(ST_MUTEX); 12040 12041 /* We need to check if this block of mem is big enough for this I/O */ 12042 if (cp->cm_len < len) { 12043 /* not big enough, need to alloc a new one */ 12044 if (ddi_dma_mem_alloc(un->un_contig_mem_hdl, len, &st_acc_attr, 12045 DDI_DMA_STREAMING, dma_alloc_cb, NULL, 12046 &addr, &rlen, &acc_hdl) != DDI_SUCCESS) { 12047 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 12048 "alloc contig_mem failure: not enough mem\n"); 12049 st_release_contig_mem(un, cp); 12050 cp = NULL; 12051 } else { 12052 if (cp->cm_addr) { 12053 /* release previous one before attach new one */ 12054 ddi_dma_mem_free(&cp->cm_acc_hdl); 12055 } 12056 mutex_enter(ST_MUTEX); 12057 un->un_max_contig_mem_len = 12058 un->un_max_contig_mem_len >= len ? 12059 un->un_max_contig_mem_len : len; 12060 mutex_exit(ST_MUTEX); 12061 12062 /* attach new mem to this cp */ 12063 cp->cm_addr = addr; 12064 cp->cm_acc_hdl = acc_hdl; 12065 cp->cm_len = len; 12066 12067 goto alloc_ok; /* get one usable cp */ 12068 } 12069 } else { 12070 goto alloc_ok; /* get one usable cp */ 12071 } 12072 12073 /* cannot find/alloc a usable cp, when we get here */ 12074 12075 mutex_enter(ST_MUTEX); 12076 if ((un->un_max_contig_mem_len < len) || 12077 (alloc_flags != KM_SLEEP)) { 12078 mutex_exit(ST_MUTEX); 12079 return (NULL); 12080 } 12081 12082 /* 12083 * we're allowed to sleep, and there is one big enough 12084 * contig mem in the system, which is currently in use, 12085 * wait for it... 12086 */ 12087 big_enough = 1; 12088 do { 12089 cv_wait(&un->un_contig_mem_cv, ST_MUTEX); 12090 ST_GET_CONTIG_MEM_HEAD(un, cp, len, big_enough); 12091 } while (cp == NULL); 12092 mutex_exit(ST_MUTEX); 12093 12094 /* we get the big enough contig mem, finally */ 12095 12096 alloc_ok: 12097 /* init bp attached to this cp */ 12098 bioreset(cp->cm_bp); 12099 cp->cm_bp->b_un.b_addr = cp->cm_addr; 12100 cp->cm_bp->b_private = (void *)cp; 12101 12102 return (cp); 12103 } 12104 12105 /* 12106 * this is the biodone func for the bp used in big block I/O 12107 */ 12108 static int 12109 st_bigblk_xfer_done(struct buf *bp) 12110 { 12111 struct contig_mem *cp; 12112 struct buf *orig_bp; 12113 int remapped = 0; 12114 int ioerr; 12115 struct scsi_tape *un; 12116 12117 /* sanity check */ 12118 if (bp == NULL) { 12119 return (DDI_FAILURE); 12120 } 12121 12122 un = ddi_get_soft_state(st_state, MTUNIT(bp->b_edev)); 12123 if (un == NULL) { 12124 return (DDI_FAILURE); 12125 } 12126 12127 cp = (struct contig_mem *)bp->b_private; 12128 orig_bp = cp->cm_bp; /* get back the bp we have replaced */ 12129 cp->cm_bp = bp; 12130 12131 /* special handling for special I/O */ 12132 if (cp->cm_use_sbuf) { 12133 #ifndef __lock_lint 12134 ASSERT(un->un_sbuf_busy); 12135 #endif 12136 un->un_sbufp = orig_bp; 12137 cp->cm_use_sbuf = 0; 12138 } 12139 12140 orig_bp->b_resid = bp->b_resid; 12141 ioerr = geterror(bp); 12142 if (ioerr != 0) { 12143 bioerror(orig_bp, ioerr); 12144 } else if (orig_bp->b_flags & B_READ) { 12145 /* copy data back to original bp */ 12146 if (orig_bp->b_flags & (B_PHYS | B_PAGEIO)) { 12147 bp_mapin(orig_bp); 12148 remapped = 1; 12149 } 12150 bcopy(bp->b_un.b_addr, orig_bp->b_un.b_addr, 12151 bp->b_bcount - bp->b_resid); 12152 if (remapped) 12153 bp_mapout(orig_bp); 12154 } 12155 12156 st_release_contig_mem(un, cp); 12157 12158 biodone(orig_bp); 12159 12160 return (DDI_SUCCESS); 12161 } 12162 12163 /* 12164 * We use this func to replace original bp that may not be able to do I/O 12165 * in big block size with one that can 12166 */ 12167 static struct buf * 12168 st_get_bigblk_bp(struct buf *bp) 12169 { 12170 struct contig_mem *cp; 12171 struct scsi_tape *un; 12172 struct buf *cont_bp; 12173 int remapped = 0; 12174 12175 un = ddi_get_soft_state(st_state, MTUNIT(bp->b_edev)); 12176 if (un == NULL) { 12177 return (bp); 12178 } 12179 12180 /* try to get one contig_mem */ 12181 cp = st_get_contig_mem(un, bp->b_bcount, KM_SLEEP); 12182 if (!cp) { 12183 scsi_log(ST_DEVINFO, st_label, CE_WARN, 12184 "Cannot alloc contig buf for I/O for %lu blk size", 12185 bp->b_bcount); 12186 return (bp); 12187 } 12188 cont_bp = cp->cm_bp; 12189 cp->cm_bp = bp; 12190 12191 /* make sure that we "are" using un_sbufp for special I/O */ 12192 if (bp == un->un_sbufp) { 12193 #ifndef __lock_lint 12194 ASSERT(un->un_sbuf_busy); 12195 #endif 12196 un->un_sbufp = cont_bp; 12197 cp->cm_use_sbuf = 1; 12198 } 12199 12200 /* clone bp */ 12201 cont_bp->b_bcount = bp->b_bcount; 12202 cont_bp->b_resid = bp->b_resid; 12203 cont_bp->b_iodone = st_bigblk_xfer_done; 12204 cont_bp->b_file = bp->b_file; 12205 cont_bp->b_offset = bp->b_offset; 12206 cont_bp->b_dip = bp->b_dip; 12207 cont_bp->b_error = 0; 12208 cont_bp->b_proc = NULL; 12209 cont_bp->b_flags = bp->b_flags & ~(B_PAGEIO | B_PHYS | B_SHADOW); 12210 cont_bp->b_shadow = NULL; 12211 cont_bp->b_pages = NULL; 12212 cont_bp->b_edev = bp->b_edev; 12213 cont_bp->b_dev = bp->b_dev; 12214 cont_bp->b_lblkno = bp->b_lblkno; 12215 cont_bp->b_forw = bp->b_forw; 12216 cont_bp->b_back = bp->b_back; 12217 cont_bp->av_forw = bp->av_forw; 12218 cont_bp->av_back = bp->av_back; 12219 cont_bp->b_bufsize = bp->b_bufsize; 12220 12221 /* get data in original bp */ 12222 if (bp->b_flags & B_WRITE) { 12223 if (bp->b_flags & (B_PHYS | B_PAGEIO)) { 12224 bp_mapin(bp); 12225 remapped = 1; 12226 } 12227 bcopy(bp->b_un.b_addr, cont_bp->b_un.b_addr, bp->b_bcount); 12228 if (remapped) 12229 bp_mapout(bp); 12230 } 12231 12232 return (cont_bp); 12233 } 12234 #else 12235 #ifdef __lock_lint 12236 static int 12237 st_bigblk_xfer_done(struct buf *bp) 12238 { 12239 return (0); 12240 } 12241 #endif 12242 #endif 12243