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