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