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