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 2008 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* 28 * SCSI SCSA-compliant and not-so-DDI-compliant Tape Driver 29 */ 30 31 #if defined(lint) && !defined(DEBUG) 32 #define DEBUG 1 33 #endif 34 35 #include <sys/modctl.h> 36 #include <sys/scsi/scsi.h> 37 #include <sys/mtio.h> 38 #include <sys/scsi/targets/stdef.h> 39 #include <sys/file.h> 40 #include <sys/kstat.h> 41 #include <sys/ddidmareq.h> 42 #include <sys/ddi.h> 43 #include <sys/sunddi.h> 44 #include <sys/byteorder.h> 45 46 #define IOSP KSTAT_IO_PTR(un->un_stats) 47 /* 48 * stats maintained only for reads/writes as commands 49 * like rewind etc skew the wait/busy times 50 */ 51 #define IS_RW(bp) ((bp)->b_bcount > 0) 52 #define ST_DO_KSTATS(bp, kstat_function) \ 53 if ((bp != un->un_sbufp) && un->un_stats && IS_RW(bp)) { \ 54 kstat_function(IOSP); \ 55 } 56 57 #define ST_DO_ERRSTATS(un, x) \ 58 if (un->un_errstats) { \ 59 struct st_errstats *stp; \ 60 stp = (struct st_errstats *)un->un_errstats->ks_data; \ 61 stp->x.value.ul++; \ 62 } 63 64 #define FILL_SCSI1_LUN(devp, pkt) \ 65 if ((devp)->sd_inq->inq_ansi == 0x1) { \ 66 int _lun; \ 67 _lun = ddi_prop_get_int(DDI_DEV_T_ANY, (devp)->sd_dev, \ 68 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_LUN, 0); \ 69 if (_lun > 0) { \ 70 ((union scsi_cdb *)(pkt)->pkt_cdbp)->scc_lun = \ 71 _lun; \ 72 } \ 73 } 74 75 /* 76 * get an available contig mem header, cp. 77 * when big_enough is true, we will return NULL, if no big enough 78 * contig mem is found. 79 * when big_enough is false, we will try to find cp containing big 80 * enough contig mem. if not found, we will ruturn the last cp available. 81 * 82 * used by st_get_contig_mem() 83 */ 84 #define ST_GET_CONTIG_MEM_HEAD(un, cp, len, big_enough) { \ 85 struct contig_mem *tmp_cp = NULL; \ 86 for ((cp) = (un)->un_contig_mem; \ 87 (cp) != NULL; \ 88 tmp_cp = (cp), (cp) = (cp)->cm_next) { \ 89 if (((cp)->cm_len >= (len)) || \ 90 (!(big_enough) && ((cp)->cm_next == NULL))) { \ 91 if (tmp_cp == NULL) { \ 92 (un)->un_contig_mem = (cp)->cm_next; \ 93 } else { \ 94 tmp_cp->cm_next = (cp)->cm_next; \ 95 } \ 96 (cp)->cm_next = NULL; \ 97 (un)->un_contig_mem_available_num--; \ 98 break; \ 99 } \ 100 } \ 101 } 102 103 #define ST_NUM_MEMBERS(array) (sizeof (array) / sizeof (array[0])) 104 #define COPY_POS(dest, source) bcopy(source, dest, sizeof (tapepos_t)) 105 #define ISALNUM(byte) \ 106 (((byte) >= 'a' && (byte) <= 'z') || \ 107 ((byte) >= 'A' && (byte) <= 'Z') || \ 108 ((byte) >= '0' && (byte) <= '9')) 109 110 #define ONE_K 1024 111 112 #define MAX_SPACE_CNT(cnt) if (cnt >= 0) { \ 113 if (cnt > MIN(SP_CNT_MASK, INT32_MAX)) \ 114 return (EINVAL); \ 115 } else { \ 116 if (-(cnt) > MIN(SP_CNT_MASK, INT32_MAX)) \ 117 return (EINVAL); \ 118 } \ 119 120 /* 121 * Global External Data Definitions 122 */ 123 extern struct scsi_key_strings scsi_cmds[]; 124 extern uchar_t scsi_cdb_size[]; 125 126 /* 127 * Local Static Data 128 */ 129 static void *st_state; 130 static char *const st_label = "st"; 131 static volatile int st_recov_sz = sizeof (recov_info); 132 static const char mp_misconf[] = { 133 "St Tape is misconfigured, MPxIO enabled and " 134 "tape-command-recovery-disable set in st.conf\n" 135 }; 136 137 #ifdef __x86 138 /* 139 * We need to use below DMA attr to alloc physically contiguous 140 * memory to do I/O in big block size 141 */ 142 static ddi_dma_attr_t st_contig_mem_dma_attr = { 143 DMA_ATTR_V0, /* version number */ 144 0x0, /* lowest usable address */ 145 0xFFFFFFFFull, /* high DMA address range */ 146 0xFFFFFFFFull, /* DMA counter register */ 147 1, /* DMA address alignment */ 148 1, /* DMA burstsizes */ 149 1, /* min effective DMA size */ 150 0xFFFFFFFFull, /* max DMA xfer size */ 151 0xFFFFFFFFull, /* segment boundary */ 152 1, /* s/g list length */ 153 1, /* granularity of device */ 154 0 /* DMA transfer flags */ 155 }; 156 157 static ddi_device_acc_attr_t st_acc_attr = { 158 DDI_DEVICE_ATTR_V0, 159 DDI_NEVERSWAP_ACC, 160 DDI_STRICTORDER_ACC 161 }; 162 163 /* set limitation for the number of contig_mem */ 164 static int st_max_contig_mem_num = ST_MAX_CONTIG_MEM_NUM; 165 #endif 166 167 /* 168 * Tunable parameters 169 * 170 * DISCLAIMER 171 * ---------- 172 * These parameters are intended for use only in system testing; if you use 173 * them in production systems, you do so at your own risk. Altering any 174 * variable not listed below may cause unpredictable system behavior. 175 * 176 * st_check_media_time 177 * 178 * Three second state check 179 * 180 * st_allow_large_xfer 181 * 182 * Gated with ST_NO_RECSIZE_LIMIT 183 * 184 * 0 - Transfers larger than 64KB will not be allowed 185 * regardless of the setting of ST_NO_RECSIZE_LIMIT 186 * 1 - Transfers larger than 64KB will be allowed 187 * if ST_NO_RECSIZE_LIMIT is TRUE for the drive 188 * 189 * st_report_soft_errors_on_close 190 * 191 * Gated with ST_SOFT_ERROR_REPORTING 192 * 193 * 0 - Errors will not be reported on close regardless 194 * of the setting of ST_SOFT_ERROR_REPORTING 195 * 196 * 1 - Errors will be reported on close if 197 * ST_SOFT_ERROR_REPORTING is TRUE for the drive 198 */ 199 static int st_selection_retry_count = ST_SEL_RETRY_COUNT; 200 static int st_retry_count = ST_RETRY_COUNT; 201 202 static int st_io_time = ST_IO_TIME; 203 static int st_long_timeout_x = ST_LONG_TIMEOUT_X; 204 205 static int st_space_time = ST_SPACE_TIME; 206 static int st_long_space_time_x = ST_LONG_SPACE_TIME_X; 207 208 static int st_error_level = SCSI_ERR_RETRYABLE; 209 static int st_check_media_time = 3000000; /* 3 Second State Check */ 210 211 static int st_max_throttle = ST_MAX_THROTTLE; 212 213 static clock_t st_wait_cmds_complete = ST_WAIT_CMDS_COMPLETE; 214 215 static int st_allow_large_xfer = 1; 216 static int st_report_soft_errors_on_close = 1; 217 218 /* 219 * End of tunable parameters list 220 */ 221 222 223 224 /* 225 * Asynchronous I/O and persistent errors, refer to PSARC/1995/228 226 * 227 * Asynchronous I/O's main offering is that it is a non-blocking way to do 228 * reads and writes. The driver will queue up all the requests it gets and 229 * have them ready to transport to the HBA. Unfortunately, we cannot always 230 * just ship the I/O requests to the HBA, as there errors and exceptions 231 * that may happen when we don't want the HBA to continue. Therein comes 232 * the flush-on-errors capability. If the HBA supports it, then st will 233 * send in st_max_throttle I/O requests at the same time. 234 * 235 * Persistent errors : This was also reasonably simple. In the interrupt 236 * routines, if there was an error or exception (FM, LEOT, media error, 237 * transport error), the persistent error bits are set and shuts everything 238 * down, but setting the throttle to zero. If we hit and exception in the 239 * HBA, and flush-on-errors were set, we wait for all outstanding I/O's to 240 * come back (with CMD_ABORTED), then flush all bp's in the wait queue with 241 * the appropriate error, and this will preserve order. Of course, depending 242 * on the exception we have to show a zero read or write before we show 243 * errors back to the application. 244 */ 245 246 extern const int st_ndrivetypes; /* defined in st_conf.c */ 247 extern const struct st_drivetype st_drivetypes[]; 248 extern const char st_conf_version[]; 249 250 #ifdef STDEBUG 251 static int st_soft_error_report_debug = 0; 252 volatile int st_debug = 0; 253 static volatile dev_info_t *st_lastdev; 254 static kmutex_t st_debug_mutex; 255 #endif 256 257 #define ST_MT02_NAME "Emulex MT02 QIC-11/24 " 258 259 static const struct vid_drivetype { 260 char *vid; 261 char type; 262 } st_vid_dt[] = { 263 {"LTO-CVE ", MT_LTO}, 264 {"QUANTUM ", MT_ISDLT}, 265 {"SONY ", MT_ISAIT}, 266 {"STK ", MT_ISSTK9840} 267 }; 268 269 static const struct driver_minor_data { 270 char *name; 271 int minor; 272 } st_minor_data[] = { 273 /* 274 * The top 4 entries are for the default densities, 275 * don't alter their position. 276 */ 277 {"", 0}, 278 {"n", MT_NOREWIND}, 279 {"b", MT_BSD}, 280 {"bn", MT_NOREWIND | MT_BSD}, 281 {"l", MT_DENSITY1}, 282 {"m", MT_DENSITY2}, 283 {"h", MT_DENSITY3}, 284 {"c", MT_DENSITY4}, 285 {"u", MT_DENSITY4}, 286 {"ln", MT_DENSITY1 | MT_NOREWIND}, 287 {"mn", MT_DENSITY2 | MT_NOREWIND}, 288 {"hn", MT_DENSITY3 | MT_NOREWIND}, 289 {"cn", MT_DENSITY4 | MT_NOREWIND}, 290 {"un", MT_DENSITY4 | MT_NOREWIND}, 291 {"lb", MT_DENSITY1 | MT_BSD}, 292 {"mb", MT_DENSITY2 | MT_BSD}, 293 {"hb", MT_DENSITY3 | MT_BSD}, 294 {"cb", MT_DENSITY4 | MT_BSD}, 295 {"ub", MT_DENSITY4 | MT_BSD}, 296 {"lbn", MT_DENSITY1 | MT_NOREWIND | MT_BSD}, 297 {"mbn", MT_DENSITY2 | MT_NOREWIND | MT_BSD}, 298 {"hbn", MT_DENSITY3 | MT_NOREWIND | MT_BSD}, 299 {"cbn", MT_DENSITY4 | MT_NOREWIND | MT_BSD}, 300 {"ubn", MT_DENSITY4 | MT_NOREWIND | MT_BSD} 301 }; 302 303 /* strings used in many debug and warning messages */ 304 static const char wr_str[] = "write"; 305 static const char rd_str[] = "read"; 306 static const char wrg_str[] = "writing"; 307 static const char rdg_str[] = "reading"; 308 static const char *space_strs[] = { 309 "records", 310 "filemarks", 311 "sequential filemarks", 312 "eod", 313 "setmarks", 314 "sequential setmarks", 315 "Reserved", 316 "Reserved" 317 }; 318 static const char *load_strs[] = { 319 "unload", /* LD_UNLOAD 0 */ 320 "load", /* LD_LOAD 1 */ 321 "retension", /* LD_RETEN 2 */ 322 "load reten", /* LD_LOAD | LD_RETEN 3 */ 323 "eod", /* LD_EOT 4 */ 324 "load EOD", /* LD_LOAD | LD_EOT 5 */ 325 "reten EOD", /* LD_RETEN | LD_EOT 6 */ 326 "load reten EOD" /* LD_LOAD|LD_RETEN|LD_EOT 7 */ 327 "hold", /* LD_HOLD 8 */ 328 "load and hold" /* LD_LOAD | LD_HOLD 9 */ 329 }; 330 331 static const char *errstatenames[] = { 332 "COMMAND_DONE", 333 "COMMAND_DONE_ERROR", 334 "COMMAND_DONE_ERROR_RECOVERED", 335 "QUE_COMMAND", 336 "QUE_BUSY_COMMAND", 337 "QUE_SENSE", 338 "JUST_RETURN", 339 "COMMAND_DONE_EACCES", 340 "QUE_LAST_COMMAND", 341 "COMMAND_TIMEOUT", 342 "PATH_FAILED", 343 "DEVICE_RESET", 344 "DEVICE_TAMPER", 345 "ATTEMPT_RETRY" 346 }; 347 348 const char *bogusID = "Unknown Media ID"; 349 350 /* default density offsets in the table above */ 351 #define DEF_BLANK 0 352 #define DEF_NOREWIND 1 353 #define DEF_BSD 2 354 #define DEF_BSD_NR 3 355 356 /* Sense Key, ASC/ASCQ for which tape ejection is needed */ 357 358 static struct tape_failure_code { 359 uchar_t key; 360 uchar_t add_code; 361 uchar_t qual_code; 362 } st_tape_failure_code[] = { 363 { KEY_HARDWARE_ERROR, 0x15, 0x01}, 364 { KEY_HARDWARE_ERROR, 0x44, 0x00}, 365 { KEY_HARDWARE_ERROR, 0x53, 0x00}, 366 { KEY_HARDWARE_ERROR, 0x53, 0x01}, 367 { KEY_NOT_READY, 0x53, 0x00}, 368 { 0xff} 369 }; 370 371 /* clean bit position and mask */ 372 373 static struct cln_bit_position { 374 ushort_t cln_bit_byte; 375 uchar_t cln_bit_mask; 376 } st_cln_bit_position[] = { 377 { 21, 0x08}, 378 { 70, 0xc0}, 379 { 18, 0x81} /* 80 bit indicates in bit mode, 1 bit clean light is on */ 380 }; 381 382 /* 383 * architecture dependent allocation restrictions. For x86, we'll set 384 * dma_attr_addr_hi to st_max_phys_addr and dma_attr_sgllen to 385 * st_sgl_size during _init(). 386 */ 387 #if defined(__sparc) 388 static ddi_dma_attr_t st_alloc_attr = { 389 DMA_ATTR_V0, /* version number */ 390 0x0, /* lowest usable address */ 391 0xFFFFFFFFull, /* high DMA address range */ 392 0xFFFFFFFFull, /* DMA counter register */ 393 1, /* DMA address alignment */ 394 1, /* DMA burstsizes */ 395 1, /* min effective DMA size */ 396 0xFFFFFFFFull, /* max DMA xfer size */ 397 0xFFFFFFFFull, /* segment boundary */ 398 1, /* s/g list length */ 399 512, /* granularity of device */ 400 0 /* DMA transfer flags */ 401 }; 402 #elif defined(__x86) 403 static ddi_dma_attr_t st_alloc_attr = { 404 DMA_ATTR_V0, /* version number */ 405 0x0, /* lowest usable address */ 406 0x0, /* high DMA address range [set in _init()] */ 407 0xFFFFull, /* DMA counter register */ 408 512, /* DMA address alignment */ 409 1, /* DMA burstsizes */ 410 1, /* min effective DMA size */ 411 0xFFFFFFFFull, /* max DMA xfer size */ 412 0xFFFFFFFFull, /* segment boundary */ 413 0, /* s/g list length */ 414 512, /* granularity of device [set in _init()] */ 415 0 /* DMA transfer flags */ 416 }; 417 uint64_t st_max_phys_addr = 0xFFFFFFFFull; 418 int st_sgl_size = 0xF; 419 420 #endif 421 422 /* 423 * Configuration Data: 424 * 425 * Device driver ops vector 426 */ 427 static int st_aread(dev_t dev, struct aio_req *aio, cred_t *cred_p); 428 static int st_awrite(dev_t dev, struct aio_req *aio, cred_t *cred_p); 429 static int st_read(dev_t dev, struct uio *uio_p, cred_t *cred_p); 430 static int st_write(dev_t dev, struct uio *uio_p, cred_t *cred_p); 431 static int st_open(dev_t *devp, int flag, int otyp, cred_t *cred_p); 432 static int st_close(dev_t dev, int flag, int otyp, cred_t *cred_p); 433 static int st_strategy(struct buf *bp); 434 static int st_queued_strategy(buf_t *bp); 435 static int st_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, 436 cred_t *cred_p, int *rval_p); 437 extern int nulldev(), nodev(); 438 439 static struct cb_ops st_cb_ops = { 440 st_open, /* open */ 441 st_close, /* close */ 442 st_queued_strategy, /* strategy Not Block device but async checks */ 443 nodev, /* print */ 444 nodev, /* dump */ 445 st_read, /* read */ 446 st_write, /* write */ 447 st_ioctl, /* ioctl */ 448 nodev, /* devmap */ 449 nodev, /* mmap */ 450 nodev, /* segmap */ 451 nochpoll, /* poll */ 452 ddi_prop_op, /* cb_prop_op */ 453 0, /* streamtab */ 454 D_64BIT | D_MP | D_NEW | D_HOTPLUG | 455 D_OPEN_RETURNS_EINTR, /* cb_flag */ 456 CB_REV, /* cb_rev */ 457 st_aread, /* async I/O read entry point */ 458 st_awrite /* async I/O write entry point */ 459 460 }; 461 462 static int st_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, 463 void **result); 464 static int st_probe(dev_info_t *dev); 465 static int st_attach(dev_info_t *dev, ddi_attach_cmd_t cmd); 466 static int st_detach(dev_info_t *dev, ddi_detach_cmd_t cmd); 467 468 static struct dev_ops st_ops = { 469 DEVO_REV, /* devo_rev, */ 470 0, /* refcnt */ 471 st_info, /* info */ 472 nulldev, /* identify */ 473 st_probe, /* probe */ 474 st_attach, /* attach */ 475 st_detach, /* detach */ 476 nodev, /* reset */ 477 &st_cb_ops, /* driver operations */ 478 (struct bus_ops *)0, /* bus operations */ 479 nulldev, /* power */ 480 ddi_quiesce_not_needed, /* devo_quiesce */ 481 }; 482 483 /* 484 * Local Function Declarations 485 */ 486 static char *st_print_scsi_cmd(char cmd); 487 static void st_print_cdb(dev_info_t *dip, char *label, uint_t level, 488 char *title, char *cdb); 489 static void st_clean_print(dev_info_t *dev, char *label, uint_t level, 490 char *title, char *data, int len); 491 static int st_doattach(struct scsi_device *devp, int (*canwait)()); 492 static void st_known_tape_type(struct scsi_tape *un); 493 static int st_get_conf_from_st_dot_conf(struct scsi_tape *, char *, 494 struct st_drivetype *); 495 static int st_get_conf_from_st_conf_dot_c(struct scsi_tape *, char *, 496 struct st_drivetype *); 497 static int st_get_conf_from_tape_drive(struct scsi_tape *, char *, 498 struct st_drivetype *); 499 static int st_get_densities_from_tape_drive(struct scsi_tape *, 500 struct st_drivetype *); 501 static int st_get_timeout_values_from_tape_drive(struct scsi_tape *, 502 struct st_drivetype *); 503 static int st_get_timeouts_value(struct scsi_tape *, uchar_t, ushort_t *, 504 ushort_t); 505 static int st_get_default_conf(struct scsi_tape *, char *, 506 struct st_drivetype *); 507 static int st_rw(dev_t dev, struct uio *uio, int flag); 508 static int st_arw(dev_t dev, struct aio_req *aio, int flag); 509 static int st_find_eod(struct scsi_tape *un); 510 static int st_check_density_or_wfm(dev_t dev, int wfm, int mode, int stepflag); 511 static int st_uscsi_cmd(struct scsi_tape *un, struct uscsi_cmd *, int flag); 512 static int st_mtioctop(struct scsi_tape *un, intptr_t arg, int flag); 513 static int st_mtiocltop(struct scsi_tape *un, intptr_t arg, int flag); 514 static int st_do_mtioctop(struct scsi_tape *un, struct mtlop *mtop); 515 static void st_start(struct scsi_tape *un); 516 static int st_handle_start_busy(struct scsi_tape *un, struct buf *bp, 517 clock_t timeout_interval, int queued); 518 static int st_handle_intr_busy(struct scsi_tape *un, struct buf *bp, 519 clock_t timeout_interval); 520 static int st_handle_intr_retry_lcmd(struct scsi_tape *un, struct buf *bp); 521 static void st_done_and_mutex_exit(struct scsi_tape *un, struct buf *bp); 522 static void st_init(struct scsi_tape *un); 523 static void st_make_cmd(struct scsi_tape *un, struct buf *bp, 524 int (*func)(caddr_t)); 525 static void st_make_uscsi_cmd(struct scsi_tape *, struct uscsi_cmd *, 526 struct buf *bp, int (*func)(caddr_t)); 527 static void st_intr(struct scsi_pkt *pkt); 528 static void st_set_state(struct scsi_tape *un, buf_t *bp); 529 static void st_test_append(struct buf *bp); 530 static int st_runout(caddr_t); 531 static int st_cmd(struct scsi_tape *un, int com, int64_t count, int wait); 532 static int st_setup_cmd(struct scsi_tape *un, buf_t *bp, int com, 533 int64_t count); 534 static int st_set_compression(struct scsi_tape *un); 535 static int st_write_fm(dev_t dev, int wfm); 536 static int st_determine_generic(struct scsi_tape *un); 537 static int st_determine_density(struct scsi_tape *un, int rw); 538 static int st_get_density(struct scsi_tape *un); 539 static int st_set_density(struct scsi_tape *un); 540 static int st_loadtape(struct scsi_tape *un); 541 static int st_modesense(struct scsi_tape *un); 542 static int st_modeselect(struct scsi_tape *un); 543 static errstate st_handle_incomplete(struct scsi_tape *un, struct buf *bp); 544 static int st_wrongtapetype(struct scsi_tape *un); 545 static errstate st_check_error(struct scsi_tape *un, struct scsi_pkt *pkt); 546 static errstate st_handle_sense(struct scsi_tape *un, struct buf *bp, 547 tapepos_t *); 548 static errstate st_handle_autosense(struct scsi_tape *un, struct buf *bp, 549 tapepos_t *); 550 static int st_get_error_entry(struct scsi_tape *un, intptr_t arg, int flag); 551 static void st_update_error_stack(struct scsi_tape *un, struct scsi_pkt *pkt, 552 struct scsi_arq_status *cmd); 553 static void st_empty_error_stack(struct scsi_tape *un); 554 static errstate st_decode_sense(struct scsi_tape *un, struct buf *bp, int amt, 555 struct scsi_arq_status *, tapepos_t *); 556 static int st_report_soft_errors(dev_t dev, int flag); 557 static void st_delayed_cv_broadcast(void *arg); 558 static int st_check_media(dev_t dev, enum mtio_state state); 559 static int st_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp); 560 static void st_intr_restart(void *arg); 561 static void st_start_restart(void *arg); 562 static int st_gen_mode_sense(struct scsi_tape *un, ubufunc_t ubf, int page, 563 struct seq_mode *page_data, int page_size); 564 static int st_change_block_size(struct scsi_tape *un, uint32_t nblksz); 565 static int st_gen_mode_select(struct scsi_tape *un, ubufunc_t ubf, 566 struct seq_mode *page_data, int page_size); 567 static int st_read_block_limits(struct scsi_tape *un, 568 struct read_blklim *read_blk); 569 static int st_report_density_support(struct scsi_tape *un, 570 uchar_t *density_data, size_t buflen); 571 static int st_report_supported_operation(struct scsi_tape *un, 572 uchar_t *oper_data, uchar_t option_code, ushort_t service_action); 573 static int st_tape_init(struct scsi_tape *un); 574 static void st_flush(struct scsi_tape *un); 575 static void st_set_pe_errno(struct scsi_tape *un); 576 static void st_hba_unflush(struct scsi_tape *un); 577 static void st_turn_pe_on(struct scsi_tape *un); 578 static void st_turn_pe_off(struct scsi_tape *un); 579 static void st_set_pe_flag(struct scsi_tape *un); 580 static void st_clear_pe(struct scsi_tape *un); 581 static void st_wait_for_io(struct scsi_tape *un); 582 static int st_set_devconfig_page(struct scsi_tape *un, int compression_on); 583 static int st_set_datacomp_page(struct scsi_tape *un, int compression_on); 584 static int st_reserve_release(struct scsi_tape *un, int command, ubufunc_t ubf); 585 static int st_check_cdb_for_need_to_reserve(struct scsi_tape *un, uchar_t *cdb); 586 static int st_check_cmd_for_need_to_reserve(struct scsi_tape *un, uchar_t cmd, 587 int count); 588 static int st_take_ownership(struct scsi_tape *un, ubufunc_t ubf); 589 static int st_check_asc_ascq(struct scsi_tape *un); 590 static int st_check_clean_bit(struct scsi_tape *un); 591 static int st_check_alert_flags(struct scsi_tape *un); 592 static int st_check_sequential_clean_bit(struct scsi_tape *un); 593 static int st_check_sense_clean_bit(struct scsi_tape *un); 594 static int st_clear_unit_attentions(dev_t dev_instance, int max_trys); 595 static void st_calculate_timeouts(struct scsi_tape *un); 596 static writablity st_is_drive_worm(struct scsi_tape *un); 597 static int st_read_attributes(struct scsi_tape *un, uint16_t attribute, 598 void *buf, size_t size, ubufunc_t bufunc); 599 static int st_get_special_inquiry(struct scsi_tape *un, uchar_t size, 600 caddr_t dest, uchar_t page); 601 static int st_update_block_pos(struct scsi_tape *un, bufunc_t bf, 602 int post_space); 603 static int st_interpret_read_pos(struct scsi_tape const *un, tapepos_t *dest, 604 read_p_types type, size_t data_sz, const caddr_t responce, int post_space); 605 static int st_get_read_pos(struct scsi_tape *un, buf_t *bp); 606 static int st_logical_block_locate(struct scsi_tape *un, ubufunc_t ubf, 607 tapepos_t *pos, uint64_t lblk, uchar_t partition); 608 static int st_mtfsf_ioctl(struct scsi_tape *un, int64_t files); 609 static int st_mtfsr_ioctl(struct scsi_tape *un, int64_t count); 610 static int st_mtbsf_ioctl(struct scsi_tape *un, int64_t files); 611 static int st_mtnbsf_ioctl(struct scsi_tape *un, int64_t count); 612 static int st_mtbsr_ioctl(struct scsi_tape *un, int64_t num); 613 static int st_mtfsfm_ioctl(struct scsi_tape *un, int64_t cnt); 614 static int st_mtbsfm_ioctl(struct scsi_tape *un, int64_t cnt); 615 static int st_backward_space_files(struct scsi_tape *un, int64_t count, 616 int infront); 617 static int st_forward_space_files(struct scsi_tape *un, int64_t files); 618 static int st_scenic_route_to_begining_of_file(struct scsi_tape *un, 619 int32_t fileno); 620 static int st_space_to_begining_of_file(struct scsi_tape *un); 621 static int st_space_records(struct scsi_tape *un, int64_t records); 622 static int st_get_media_identification(struct scsi_tape *un, ubufunc_t bufunc); 623 static errstate st_command_recovery(struct scsi_tape *un, struct scsi_pkt *pkt, 624 errstate onentry); 625 static void st_recover(void *arg); 626 static void st_recov_cb(struct scsi_pkt *pkt); 627 static int st_rcmd(struct scsi_tape *un, int com, int64_t count, int wait); 628 static int st_uscsi_rcmd(struct scsi_tape *un, struct uscsi_cmd *ucmd, 629 int flag); 630 static void st_add_recovery_info_to_pkt(struct scsi_tape *un, buf_t *bp, 631 struct scsi_pkt *cmd); 632 static int st_check_mode_for_change(struct scsi_tape *un, ubufunc_t ubf); 633 static int st_test_path_to_device(struct scsi_tape *un); 634 static int st_recovery_read_pos(struct scsi_tape *un, read_p_types type, 635 read_pos_data_t *raw); 636 static int st_recovery_get_position(struct scsi_tape *un, tapepos_t *read, 637 read_pos_data_t *raw); 638 static int st_compare_expected_position(struct scsi_tape *un, st_err_info *ei, 639 cmd_attribute const * cmd_att, tapepos_t *read); 640 static errstate st_recover_reissue_pkt(struct scsi_tape *us, 641 struct scsi_pkt *pkt); 642 static int st_transport(struct scsi_tape *un, struct scsi_pkt *pkt); 643 static buf_t *st_remove_from_queue(buf_t **head, buf_t **tail, buf_t *bp); 644 static void st_add_to_queue(buf_t **head, buf_t **tail, buf_t *end, buf_t *bp); 645 static int st_reset(struct scsi_tape *un, int reset_type); 646 static void st_reset_notification(caddr_t arg); 647 static const cmd_attribute *st_lookup_cmd_attribute(unsigned char cmd); 648 649 #ifdef __x86 650 /* 651 * routines for I/O in big block size 652 */ 653 static void st_release_contig_mem(struct scsi_tape *un, struct contig_mem *cp); 654 static struct contig_mem *st_get_contig_mem(struct scsi_tape *un, size_t len, 655 int alloc_flags); 656 static int st_bigblk_xfer_done(struct buf *bp); 657 static struct buf *st_get_bigblk_bp(struct buf *bp); 658 #endif 659 static void st_print_position(dev_info_t *dev, char *label, uint_t level, 660 const char *comment, tapepos_t *pos); 661 662 /* 663 * error statistics create/update functions 664 */ 665 static int st_create_errstats(struct scsi_tape *, int); 666 static int st_validate_tapemarks(struct scsi_tape *un, ubufunc_t ubf, 667 tapepos_t *pos); 668 669 #ifdef STDEBUG 670 static void st_debug_cmds(struct scsi_tape *un, int com, int count, int wait); 671 #endif /* STDEBUG */ 672 static char *st_dev_name(dev_t dev); 673 674 #if !defined(lint) 675 _NOTE(SCHEME_PROTECTS_DATA("unique per pkt", 676 scsi_pkt buf uio scsi_cdb uscsi_cmd)) 677 _NOTE(SCHEME_PROTECTS_DATA("unique per pkt", scsi_extended_sense scsi_status)) 678 _NOTE(SCHEME_PROTECTS_DATA("unique per pkt", recov_info)) 679 _NOTE(SCHEME_PROTECTS_DATA("stable data", scsi_device)) 680 _NOTE(DATA_READABLE_WITHOUT_LOCK(st_drivetype scsi_address)) 681 #endif 682 683 /* 684 * autoconfiguration routines. 685 */ 686 char _depends_on[] = "misc/scsi"; 687 688 static struct modldrv modldrv = { 689 &mod_driverops, /* Type of module. This one is a driver */ 690 "SCSI tape Driver", /* Name of the module. */ 691 &st_ops /* driver ops */ 692 }; 693 694 static struct modlinkage modlinkage = { 695 MODREV_1, &modldrv, NULL 696 }; 697 698 /* 699 * Notes on Post Reset Behavior in the tape driver: 700 * 701 * When the tape drive is opened, the driver attempts to make sure that 702 * the tape head is positioned exactly where it was left when it was last 703 * closed provided the medium is not changed. If the tape drive is 704 * opened in O_NDELAY mode, the repositioning (if necessary for any loss 705 * of position due to reset) will happen when the first tape operation or 706 * I/O occurs. The repositioning (if required) may not be possible under 707 * certain situations such as when the device firmware not able to report 708 * the medium change in the REQUEST SENSE data because of a reset or a 709 * misbehaving bus not allowing the reposition to happen. In such 710 * extraordinary situations, where the driver fails to position the head 711 * at its original position, it will fail the open the first time, to 712 * save the applications from overwriting the data. All further attempts 713 * to open the tape device will result in the driver attempting to load 714 * the tape at BOT (beginning of tape). Also a warning message to 715 * indicate that further attempts to open the tape device may result in 716 * the tape being loaded at BOT will be printed on the console. If the 717 * tape device is opened in O_NDELAY mode, failure to restore the 718 * original tape head position, will result in the failure of the first 719 * tape operation or I/O, Further, the driver will invalidate its 720 * internal tape position which will necessitate the applications to 721 * validate the position by using either a tape positioning ioctl (such 722 * as MTREW) or closing and reopening the tape device. 723 * 724 */ 725 726 int 727 _init(void) 728 { 729 int e; 730 731 if (((e = ddi_soft_state_init(&st_state, 732 sizeof (struct scsi_tape), ST_MAXUNIT)) != 0)) { 733 return (e); 734 } 735 736 if ((e = mod_install(&modlinkage)) != 0) { 737 ddi_soft_state_fini(&st_state); 738 } else { 739 #ifdef STDEBUG 740 mutex_init(&st_debug_mutex, NULL, MUTEX_DRIVER, NULL); 741 #endif 742 743 #if defined(__x86) 744 /* set the max physical address for iob allocs on x86 */ 745 st_alloc_attr.dma_attr_addr_hi = st_max_phys_addr; 746 747 /* 748 * set the sgllen for iob allocs on x86. If this is set less 749 * than the number of pages the buffer will take 750 * (taking into account alignment), it would force the 751 * allocator to try and allocate contiguous pages. 752 */ 753 st_alloc_attr.dma_attr_sgllen = st_sgl_size; 754 #endif 755 } 756 757 return (e); 758 } 759 760 int 761 _fini(void) 762 { 763 int e; 764 765 if ((e = mod_remove(&modlinkage)) != 0) { 766 return (e); 767 } 768 769 #ifdef STDEBUG 770 mutex_destroy(&st_debug_mutex); 771 #endif 772 773 ddi_soft_state_fini(&st_state); 774 775 return (e); 776 } 777 778 int 779 _info(struct modinfo *modinfop) 780 { 781 return (mod_info(&modlinkage, modinfop)); 782 } 783 784 785 static int 786 st_probe(dev_info_t *devi) 787 { 788 int instance; 789 struct scsi_device *devp; 790 int rval; 791 792 #if !defined(__sparc) 793 char *tape_prop; 794 int tape_prop_len; 795 #endif 796 797 ST_ENTR(devi, st_probe); 798 799 /* If self identifying device */ 800 if (ddi_dev_is_sid(devi) == DDI_SUCCESS) { 801 return (DDI_PROBE_DONTCARE); 802 } 803 804 #if !defined(__sparc) 805 /* 806 * Since some x86 HBAs have devnodes that look like SCSI as 807 * far as we can tell but aren't really SCSI (DADK, like mlx) 808 * we check for the presence of the "tape" property. 809 */ 810 if (ddi_prop_op(DDI_DEV_T_NONE, devi, PROP_LEN_AND_VAL_ALLOC, 811 DDI_PROP_CANSLEEP, "tape", 812 (caddr_t)&tape_prop, &tape_prop_len) != DDI_PROP_SUCCESS) { 813 return (DDI_PROBE_FAILURE); 814 } 815 if (strncmp(tape_prop, "sctp", tape_prop_len) != 0) { 816 kmem_free(tape_prop, tape_prop_len); 817 return (DDI_PROBE_FAILURE); 818 } 819 kmem_free(tape_prop, tape_prop_len); 820 #endif 821 822 devp = ddi_get_driver_private(devi); 823 instance = ddi_get_instance(devi); 824 825 if (ddi_get_soft_state(st_state, instance) != NULL) { 826 return (DDI_PROBE_PARTIAL); 827 } 828 829 830 /* 831 * Turn around and call probe routine to see whether 832 * we actually have a tape at this SCSI nexus. 833 */ 834 if (scsi_probe(devp, NULL_FUNC) == SCSIPROBE_EXISTS) { 835 836 /* 837 * In checking the whole inq_dtype byte we are looking at both 838 * the Peripheral Qualifier and the Peripheral Device Type. 839 * For this driver we are only interested in sequential devices 840 * that are connected or capable if connecting to this logical 841 * unit. 842 */ 843 if (devp->sd_inq->inq_dtype == 844 (DTYPE_SEQUENTIAL | DPQ_POSSIBLE)) { 845 ST_DEBUG6(devi, st_label, SCSI_DEBUG, 846 "probe exists\n"); 847 rval = DDI_PROBE_SUCCESS; 848 } else { 849 rval = DDI_PROBE_FAILURE; 850 } 851 } else { 852 ST_DEBUG6(devi, st_label, SCSI_DEBUG, 853 "probe failure: nothing there\n"); 854 rval = DDI_PROBE_FAILURE; 855 } 856 scsi_unprobe(devp); 857 return (rval); 858 } 859 860 static int 861 st_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 862 { 863 int instance; 864 int wide; 865 int dev_instance; 866 int ret_status; 867 struct scsi_device *devp; 868 int node_ix; 869 struct scsi_tape *un; 870 871 ST_ENTR(devi, st_attach); 872 873 devp = ddi_get_driver_private(devi); 874 instance = ddi_get_instance(devi); 875 876 switch (cmd) { 877 case DDI_ATTACH: 878 if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 879 "tape-command-recovery-disable", 0) != 0) { 880 st_recov_sz = sizeof (pkt_info); 881 } 882 if (st_doattach(devp, SLEEP_FUNC) == DDI_FAILURE) { 883 return (DDI_FAILURE); 884 } 885 break; 886 case DDI_RESUME: 887 /* 888 * Suspend/Resume 889 * 890 * When the driver suspended, there might be 891 * outstanding cmds and therefore we need to 892 * reset the suspended flag and resume the scsi 893 * watch thread and restart commands and timeouts 894 */ 895 896 if (!(un = ddi_get_soft_state(st_state, instance))) { 897 return (DDI_FAILURE); 898 } 899 dev_instance = ((un->un_dev == 0) ? MTMINOR(instance) : 900 un->un_dev); 901 902 mutex_enter(ST_MUTEX); 903 904 un->un_throttle = un->un_max_throttle; 905 un->un_tids_at_suspend = 0; 906 un->un_pwr_mgmt = ST_PWR_NORMAL; 907 908 if (un->un_swr_token) { 909 scsi_watch_resume(un->un_swr_token); 910 } 911 912 /* 913 * Restart timeouts 914 */ 915 if ((un->un_tids_at_suspend & ST_DELAY_TID) != 0) { 916 mutex_exit(ST_MUTEX); 917 un->un_delay_tid = timeout( 918 st_delayed_cv_broadcast, un, 919 drv_usectohz((clock_t) 920 MEDIA_ACCESS_DELAY)); 921 mutex_enter(ST_MUTEX); 922 } 923 924 if (un->un_tids_at_suspend & ST_HIB_TID) { 925 mutex_exit(ST_MUTEX); 926 un->un_hib_tid = timeout(st_intr_restart, un, 927 ST_STATUS_BUSY_TIMEOUT); 928 mutex_enter(ST_MUTEX); 929 } 930 931 ret_status = st_clear_unit_attentions(dev_instance, 5); 932 933 /* 934 * now check if we need to restore the tape position 935 */ 936 if ((un->un_suspend_pos.pmode != invalid) && 937 ((un->un_suspend_pos.fileno > 0) || 938 (un->un_suspend_pos.blkno > 0)) || 939 (un->un_suspend_pos.lgclblkno > 0)) { 940 if (ret_status != 0) { 941 /* 942 * tape didn't get good TUR 943 * just print out error messages 944 */ 945 scsi_log(ST_DEVINFO, st_label, CE_WARN, 946 "st_attach-RESUME: tape failure " 947 " tape position will be lost"); 948 } else { 949 /* this prints errors */ 950 (void) st_validate_tapemarks(un, 951 st_uscsi_cmd, &un->un_suspend_pos); 952 } 953 /* 954 * there are no retries, if there is an error 955 * we don't know if the tape has changed 956 */ 957 un->un_suspend_pos.pmode = invalid; 958 } 959 960 /* now we are ready to start up any queued I/Os */ 961 if (un->un_ncmds || un->un_quef) { 962 st_start(un); 963 } 964 965 cv_broadcast(&un->un_suspend_cv); 966 mutex_exit(ST_MUTEX); 967 return (DDI_SUCCESS); 968 969 default: 970 return (DDI_FAILURE); 971 } 972 973 un = ddi_get_soft_state(st_state, instance); 974 975 ST_DEBUG(devi, st_label, SCSI_DEBUG, 976 "st_attach: instance=%x\n", instance); 977 978 /* 979 * Add a zero-length attribute to tell the world we support 980 * kernel ioctls (for layered drivers) 981 */ 982 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP, 983 DDI_KERNEL_IOCTL, NULL, 0); 984 985 ddi_report_dev((dev_info_t *)devi); 986 987 /* 988 * If it's a SCSI-2 tape drive which supports wide, 989 * tell the host adapter to use wide. 990 */ 991 wide = ((devp->sd_inq->inq_rdf == RDF_SCSI2) && 992 (devp->sd_inq->inq_wbus16 || devp->sd_inq->inq_wbus32)) ? 1 : 0; 993 994 if (scsi_ifsetcap(ROUTE, "wide-xfer", wide, 1) == 1) { 995 ST_DEBUG(devi, st_label, SCSI_DEBUG, 996 "Wide Transfer %s\n", wide ? "enabled" : "disabled"); 997 } 998 999 /* 1000 * enable autorequest sense; keep the rq packet around in case 1001 * the autorequest sense fails because of a busy condition 1002 * do a getcap first in case the capability is not variable 1003 */ 1004 if (scsi_ifgetcap(ROUTE, "auto-rqsense", 1) == 1) { 1005 un->un_arq_enabled = 1; 1006 } else { 1007 un->un_arq_enabled = 1008 ((scsi_ifsetcap(ROUTE, "auto-rqsense", 1, 1) == 1) ? 1 : 0); 1009 } 1010 1011 ST_DEBUG(devi, st_label, SCSI_DEBUG, "auto request sense %s\n", 1012 (un->un_arq_enabled ? "enabled" : "disabled")); 1013 1014 un->un_untagged_qing = 1015 (scsi_ifgetcap(ROUTE, "untagged-qing", 0) == 1); 1016 1017 /* 1018 * XXX - This is just for 2.6. to tell users that write buffering 1019 * has gone away. 1020 */ 1021 if (un->un_arq_enabled && un->un_untagged_qing) { 1022 if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 1023 "tape-driver-buffering", 0) != 0) { 1024 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 1025 "Write Data Buffering has been depricated. Your " 1026 "applications should continue to work normally.\n" 1027 " But, they should ported to use Asynchronous " 1028 " I/O\n" 1029 " For more information, read about " 1030 " tape-driver-buffering " 1031 "property in the st(7d) man page\n"); 1032 } 1033 } 1034 1035 un->un_max_throttle = un->un_throttle = un->un_last_throttle = 1; 1036 un->un_flush_on_errors = 0; 1037 un->un_mkr_pkt = (struct scsi_pkt *)NULL; 1038 1039 ST_DEBUG(devi, st_label, SCSI_DEBUG, 1040 "throttle=%x, max_throttle = %x\n", 1041 un->un_throttle, un->un_max_throttle); 1042 1043 /* initialize persistent errors to nil */ 1044 un->un_persistence = 0; 1045 un->un_persist_errors = 0; 1046 1047 /* 1048 * Get dma-max from HBA driver. If it is not defined, use 64k 1049 */ 1050 un->un_maxdma = scsi_ifgetcap(&devp->sd_address, "dma-max", 1); 1051 if (un->un_maxdma == -1) { 1052 ST_DEBUG(devi, st_label, SCSI_DEBUG, 1053 "Received a value that looked like -1. Using 64k maxdma"); 1054 un->un_maxdma = (64 * ONE_K); 1055 } 1056 1057 #ifdef __x86 1058 /* 1059 * for x86, the device may be able to DMA more than the system will 1060 * allow under some circumstances. We need account for both the HBA's 1061 * and system's contraints. 1062 * 1063 * Get the maximum DMA under worse case conditions. e.g. looking at the 1064 * device constraints, the max copy buffer size, and the worse case 1065 * fragmentation. NOTE: this may differ from dma-max since dma-max 1066 * doesn't take the worse case framentation into account. 1067 * 1068 * e.g. a device may be able to DMA 16MBytes, but can only DMA 1MByte 1069 * if none of the pages are contiguous. Keeping track of both of these 1070 * values allows us to support larger tape block sizes on some devices. 1071 */ 1072 un->un_maxdma_arch = scsi_ifgetcap(&devp->sd_address, "dma-max-arch", 1073 1); 1074 1075 /* 1076 * If the dma-max-arch capability is not implemented, or the value 1077 * comes back higher than what was reported in dma-max, use dma-max. 1078 */ 1079 if ((un->un_maxdma_arch == -1) || 1080 ((uint_t)un->un_maxdma < (uint_t)un->un_maxdma_arch)) { 1081 un->un_maxdma_arch = un->un_maxdma; 1082 } 1083 #endif 1084 1085 /* 1086 * Get the max allowable cdb size 1087 */ 1088 un->un_max_cdb_sz = 1089 scsi_ifgetcap(&devp->sd_address, "max-cdb-length", 1); 1090 if (un->un_max_cdb_sz < CDB_GROUP0) { 1091 ST_DEBUG(devi, st_label, SCSI_DEBUG, 1092 "HBA reported max-cdb-length as %d\n", un->un_max_cdb_sz); 1093 un->un_max_cdb_sz = CDB_GROUP4; /* optimistic default */ 1094 } 1095 1096 if (strcmp(ddi_driver_name(ddi_get_parent(ST_DEVINFO)), "scsi_vhci")) { 1097 un->un_multipath = 0; 1098 } else { 1099 un->un_multipath = 1; 1100 } 1101 1102 un->un_maxbsize = MAXBSIZE_UNKNOWN; 1103 1104 un->un_mediastate = MTIO_NONE; 1105 un->un_HeadClean = TAPE_ALERT_SUPPORT_UNKNOWN; 1106 1107 /* 1108 * initialize kstats 1109 */ 1110 un->un_stats = kstat_create("st", instance, NULL, "tape", 1111 KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT); 1112 if (un->un_stats) { 1113 un->un_stats->ks_lock = ST_MUTEX; 1114 kstat_install(un->un_stats); 1115 } 1116 (void) st_create_errstats(un, instance); 1117 1118 /* 1119 * find the drive type for this target 1120 */ 1121 mutex_enter(ST_MUTEX); 1122 un->un_dev = MTMINOR(instance); 1123 st_known_tape_type(un); 1124 un->un_dev = 0; 1125 mutex_exit(ST_MUTEX); 1126 1127 for (node_ix = 0; node_ix < ST_NUM_MEMBERS(st_minor_data); node_ix++) { 1128 int minor; 1129 char *name; 1130 1131 name = st_minor_data[node_ix].name; 1132 minor = st_minor_data[node_ix].minor; 1133 1134 /* 1135 * For default devices set the density to the 1136 * preferred default density for this device. 1137 */ 1138 if (node_ix <= DEF_BSD_NR) { 1139 minor |= un->un_dp->default_density; 1140 } 1141 minor |= MTMINOR(instance); 1142 1143 if (ddi_create_minor_node(devi, name, S_IFCHR, minor, 1144 DDI_NT_TAPE, NULL) == DDI_SUCCESS) { 1145 continue; 1146 } 1147 1148 ddi_remove_minor_node(devi, NULL); 1149 1150 (void) scsi_reset_notify(ROUTE, SCSI_RESET_CANCEL, 1151 st_reset_notification, (caddr_t)un); 1152 cv_destroy(&un->un_clscv); 1153 cv_destroy(&un->un_sbuf_cv); 1154 cv_destroy(&un->un_queue_cv); 1155 cv_destroy(&un->un_state_cv); 1156 #ifdef __x86 1157 cv_destroy(&un->un_contig_mem_cv); 1158 #endif 1159 cv_destroy(&un->un_suspend_cv); 1160 cv_destroy(&un->un_tape_busy_cv); 1161 cv_destroy(&un->un_recov_buf_cv); 1162 if (un->un_recov_taskq) { 1163 ddi_taskq_destroy(un->un_recov_taskq); 1164 } 1165 if (un->un_sbufp) { 1166 freerbuf(un->un_sbufp); 1167 } 1168 if (un->un_recov_buf) { 1169 freerbuf(un->un_recov_buf); 1170 } 1171 if (un->un_uscsi_rqs_buf) { 1172 kmem_free(un->un_uscsi_rqs_buf, SENSE_LENGTH); 1173 } 1174 if (un->un_mspl) { 1175 i_ddi_mem_free((caddr_t)un->un_mspl, NULL); 1176 } 1177 if (un->un_dp_size) { 1178 kmem_free(un->un_dp, un->un_dp_size); 1179 } 1180 if (un->un_state) { 1181 kstat_delete(un->un_stats); 1182 } 1183 if (un->un_errstats) { 1184 kstat_delete(un->un_errstats); 1185 } 1186 1187 scsi_destroy_pkt(un->un_rqs); 1188 scsi_free_consistent_buf(un->un_rqs_bp); 1189 ddi_soft_state_free(st_state, instance); 1190 devp->sd_private = NULL; 1191 devp->sd_sense = NULL; 1192 1193 ddi_prop_remove_all(devi); 1194 return (DDI_FAILURE); 1195 } 1196 1197 return (DDI_SUCCESS); 1198 } 1199 1200 /* 1201 * st_detach: 1202 * 1203 * we allow a detach if and only if: 1204 * - no tape is currently inserted 1205 * - tape position is at BOT or unknown 1206 * (if it is not at BOT then a no rewind 1207 * device was opened and we have to preserve state) 1208 * - it must be in a closed state : no timeouts or scsi_watch requests 1209 * will exist if it is closed, so we don't need to check for 1210 * them here. 1211 */ 1212 /*ARGSUSED*/ 1213 static int 1214 st_detach(dev_info_t *devi, ddi_detach_cmd_t cmd) 1215 { 1216 int instance; 1217 int result; 1218 struct scsi_device *devp; 1219 struct scsi_tape *un; 1220 clock_t wait_cmds_complete; 1221 1222 ST_ENTR(devi, st_detach); 1223 1224 instance = ddi_get_instance(devi); 1225 1226 if (!(un = ddi_get_soft_state(st_state, instance))) { 1227 return (DDI_FAILURE); 1228 } 1229 1230 mutex_enter(ST_MUTEX); 1231 1232 /* 1233 * Clear error entry stack 1234 */ 1235 st_empty_error_stack(un); 1236 1237 mutex_exit(ST_MUTEX); 1238 1239 switch (cmd) { 1240 1241 case DDI_DETACH: 1242 /* 1243 * Undo what we did in st_attach & st_doattach, 1244 * freeing resources and removing things we installed. 1245 * The system framework guarantees we are not active 1246 * with this devinfo node in any other entry points at 1247 * this time. 1248 */ 1249 1250 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 1251 "st_detach: instance=%x, un=%p\n", instance, 1252 (void *)un); 1253 1254 if (((un->un_dp->options & ST_UNLOADABLE) == 0) || 1255 ((un->un_rsvd_status & ST_APPLICATION_RESERVATIONS) != 0) || 1256 (un->un_ncmds != 0) || (un->un_quef != NULL) || 1257 (un->un_state != ST_STATE_CLOSED)) { 1258 /* 1259 * we cannot unload some targets because the 1260 * inquiry returns junk unless immediately 1261 * after a reset 1262 */ 1263 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 1264 "cannot unload instance %x\n", instance); 1265 un->un_unit_attention_flags |= 4; 1266 return (DDI_FAILURE); 1267 } 1268 1269 /* 1270 * if the tape has been removed then we may unload; 1271 * do a test unit ready and if it returns NOT READY 1272 * then we assume that it is safe to unload. 1273 * as a side effect, pmode may be set to invalid if the 1274 * the test unit ready fails; 1275 * also un_state may be set to non-closed, so reset it 1276 */ 1277 if ((un->un_dev) && /* Been opened since attach */ 1278 ((un->un_pos.pmode == legacy) && 1279 (un->un_pos.fileno > 0) || /* Known position not rewound */ 1280 (un->un_pos.blkno != 0)) || /* Or within first file */ 1281 ((un->un_pos.pmode == logical) && 1282 (un->un_pos.lgclblkno > 0))) { 1283 mutex_enter(ST_MUTEX); 1284 /* 1285 * Send Test Unit Ready in the hopes that if 1286 * the drive is not in the state we think it is. 1287 * And the state will be changed so it can be detached. 1288 * If the command fails to reach the device and 1289 * the drive was not rewound or unloaded we want 1290 * to fail the detach till a user command fails 1291 * where after the detach will succead. 1292 */ 1293 result = st_cmd(un, SCMD_TEST_UNIT_READY, 0, SYNC_CMD); 1294 /* 1295 * After TUR un_state may be set to non-closed, 1296 * so reset it back. 1297 */ 1298 un->un_state = ST_STATE_CLOSED; 1299 mutex_exit(ST_MUTEX); 1300 } 1301 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 1302 "un_status=%x, fileno=%x, blkno=%x\n", 1303 un->un_status, un->un_pos.fileno, un->un_pos.blkno); 1304 1305 /* 1306 * check again: 1307 * if we are not at BOT then it is not safe to unload 1308 */ 1309 if ((un->un_dev) && /* Been opened since attach */ 1310 (result != EACCES) && /* drive is use by somebody */ 1311 (((un->un_pos.pmode == legacy) && 1312 (un->un_pos.fileno > 0) || /* Known position not rewound */ 1313 (un->un_pos.blkno != 0)) || /* Or within first file */ 1314 ((un->un_pos.pmode == logical) && 1315 (un->un_pos.lgclblkno > 0)))) { 1316 1317 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 1318 "cannot detach: pmode=%d fileno=0x%x, blkno=0x%x" 1319 " lgclblkno=0x%"PRIx64"\n", un->un_pos.pmode, 1320 un->un_pos.fileno, un->un_pos.blkno, 1321 un->un_pos.lgclblkno); 1322 un->un_unit_attention_flags |= 4; 1323 return (DDI_FAILURE); 1324 } 1325 1326 /* 1327 * Just To make sure that we have released the 1328 * tape unit . 1329 */ 1330 if (un->un_dev && (un->un_rsvd_status & ST_RESERVE) && 1331 !DEVI_IS_DEVICE_REMOVED(devi)) { 1332 mutex_enter(ST_MUTEX); 1333 (void) st_reserve_release(un, ST_RELEASE, st_uscsi_cmd); 1334 mutex_exit(ST_MUTEX); 1335 } 1336 1337 /* 1338 * now remove other data structures allocated in st_doattach() 1339 */ 1340 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 1341 "destroying/freeing\n"); 1342 1343 (void) scsi_reset_notify(ROUTE, SCSI_RESET_CANCEL, 1344 st_reset_notification, (caddr_t)un); 1345 cv_destroy(&un->un_clscv); 1346 cv_destroy(&un->un_sbuf_cv); 1347 cv_destroy(&un->un_queue_cv); 1348 cv_destroy(&un->un_suspend_cv); 1349 cv_destroy(&un->un_tape_busy_cv); 1350 cv_destroy(&un->un_recov_buf_cv); 1351 1352 if (un->un_recov_taskq) { 1353 ddi_taskq_destroy(un->un_recov_taskq); 1354 } 1355 1356 if (un->un_hib_tid) { 1357 (void) untimeout(un->un_hib_tid); 1358 un->un_hib_tid = 0; 1359 } 1360 1361 if (un->un_delay_tid) { 1362 (void) untimeout(un->un_delay_tid); 1363 un->un_delay_tid = 0; 1364 } 1365 cv_destroy(&un->un_state_cv); 1366 1367 #ifdef __x86 1368 cv_destroy(&un->un_contig_mem_cv); 1369 1370 if (un->un_contig_mem_hdl != NULL) { 1371 ddi_dma_free_handle(&un->un_contig_mem_hdl); 1372 } 1373 #endif 1374 if (un->un_sbufp) { 1375 freerbuf(un->un_sbufp); 1376 } 1377 if (un->un_recov_buf) { 1378 freerbuf(un->un_recov_buf); 1379 } 1380 if (un->un_uscsi_rqs_buf) { 1381 kmem_free(un->un_uscsi_rqs_buf, SENSE_LENGTH); 1382 } 1383 if (un->un_mspl) { 1384 i_ddi_mem_free((caddr_t)un->un_mspl, NULL); 1385 } 1386 if (un->un_rqs) { 1387 scsi_destroy_pkt(un->un_rqs); 1388 scsi_free_consistent_buf(un->un_rqs_bp); 1389 } 1390 if (un->un_mkr_pkt) { 1391 scsi_destroy_pkt(un->un_mkr_pkt); 1392 } 1393 if (un->un_arq_enabled) { 1394 (void) scsi_ifsetcap(ROUTE, "auto-rqsense", 0, 1); 1395 } 1396 if (un->un_dp_size) { 1397 kmem_free(un->un_dp, un->un_dp_size); 1398 } 1399 if (un->un_stats) { 1400 kstat_delete(un->un_stats); 1401 un->un_stats = (kstat_t *)0; 1402 } 1403 if (un->un_errstats) { 1404 kstat_delete(un->un_errstats); 1405 un->un_errstats = (kstat_t *)0; 1406 } 1407 if (un->un_media_id_len) { 1408 kmem_free(un->un_media_id, un->un_media_id_len); 1409 } 1410 devp = ST_SCSI_DEVP; 1411 ddi_soft_state_free(st_state, instance); 1412 devp->sd_private = NULL; 1413 devp->sd_sense = NULL; 1414 scsi_unprobe(devp); 1415 ddi_prop_remove_all(devi); 1416 ddi_remove_minor_node(devi, NULL); 1417 ST_DEBUG(0, st_label, SCSI_DEBUG, "st_detach done\n"); 1418 return (DDI_SUCCESS); 1419 1420 case DDI_SUSPEND: 1421 1422 /* 1423 * Suspend/Resume 1424 * 1425 * To process DDI_SUSPEND, we must do the following: 1426 * 1427 * - check ddi_removing_power to see if power will be turned 1428 * off. if so, return DDI_FAILURE 1429 * - check if we are already suspended, 1430 * if so, return DDI_FAILURE 1431 * - check if device state is CLOSED, 1432 * if not, return DDI_FAILURE. 1433 * - wait until outstanding operations complete 1434 * - save tape state 1435 * - block new operations 1436 * - cancel pending timeouts 1437 * 1438 */ 1439 1440 if (ddi_removing_power(devi)) { 1441 return (DDI_FAILURE); 1442 } 1443 mutex_enter(ST_MUTEX); 1444 1445 /* 1446 * Shouldn't already be suspended, if so return failure 1447 */ 1448 if (un->un_pwr_mgmt == ST_PWR_SUSPENDED) { 1449 mutex_exit(ST_MUTEX); 1450 return (DDI_FAILURE); 1451 } 1452 if (un->un_state != ST_STATE_CLOSED) { 1453 mutex_exit(ST_MUTEX); 1454 return (DDI_FAILURE); 1455 } 1456 1457 /* 1458 * Wait for all outstanding I/O's to complete 1459 * 1460 * we wait on both ncmds and the wait queue for times 1461 * when we are flushing after persistent errors are 1462 * flagged, which is when ncmds can be 0, and the 1463 * queue can still have I/O's. This way we preserve 1464 * order of biodone's. 1465 */ 1466 wait_cmds_complete = ddi_get_lbolt(); 1467 wait_cmds_complete += 1468 st_wait_cmds_complete * drv_usectohz(1000000); 1469 while (un->un_ncmds || un->un_quef || 1470 (un->un_state == ST_STATE_RESOURCE_WAIT)) { 1471 1472 if (cv_timedwait(&un->un_tape_busy_cv, ST_MUTEX, 1473 wait_cmds_complete) == -1) { 1474 /* 1475 * Time expired then cancel the command 1476 */ 1477 if (st_reset(un, RESET_LUN) == 0) { 1478 if (un->un_last_throttle) { 1479 un->un_throttle = 1480 un->un_last_throttle; 1481 } 1482 mutex_exit(ST_MUTEX); 1483 return (DDI_FAILURE); 1484 } else { 1485 break; 1486 } 1487 } 1488 } 1489 1490 /* 1491 * DDI_SUSPEND says that the system "may" power down, we 1492 * remember the file and block number before rewinding. 1493 * we also need to save state before issuing 1494 * any WRITE_FILE_MARK command. 1495 */ 1496 (void) st_update_block_pos(un, st_cmd, 0); 1497 COPY_POS(&un->un_suspend_pos, &un->un_pos); 1498 1499 1500 /* 1501 * Issue a zero write file fmk command to tell the drive to 1502 * flush any buffered tape marks 1503 */ 1504 (void) st_cmd(un, SCMD_WRITE_FILE_MARK, 0, SYNC_CMD); 1505 1506 /* 1507 * Because not all tape drives correctly implement buffer 1508 * flushing with the zero write file fmk command, issue a 1509 * synchronous rewind command to force data flushing. 1510 * st_validate_tapemarks() will do a rewind during DDI_RESUME 1511 * anyway. 1512 */ 1513 (void) st_cmd(un, SCMD_REWIND, 0, SYNC_CMD); 1514 1515 /* stop any new operations */ 1516 un->un_pwr_mgmt = ST_PWR_SUSPENDED; 1517 un->un_throttle = 0; 1518 1519 /* 1520 * cancel any outstanding timeouts 1521 */ 1522 if (un->un_delay_tid) { 1523 timeout_id_t temp_id = un->un_delay_tid; 1524 un->un_delay_tid = 0; 1525 un->un_tids_at_suspend |= ST_DELAY_TID; 1526 mutex_exit(ST_MUTEX); 1527 (void) untimeout(temp_id); 1528 mutex_enter(ST_MUTEX); 1529 } 1530 1531 if (un->un_hib_tid) { 1532 timeout_id_t temp_id = un->un_hib_tid; 1533 un->un_hib_tid = 0; 1534 un->un_tids_at_suspend |= ST_HIB_TID; 1535 mutex_exit(ST_MUTEX); 1536 (void) untimeout(temp_id); 1537 mutex_enter(ST_MUTEX); 1538 } 1539 1540 /* 1541 * Suspend the scsi_watch_thread 1542 */ 1543 if (un->un_swr_token) { 1544 opaque_t temp_token = un->un_swr_token; 1545 mutex_exit(ST_MUTEX); 1546 scsi_watch_suspend(temp_token); 1547 } else { 1548 mutex_exit(ST_MUTEX); 1549 } 1550 1551 return (DDI_SUCCESS); 1552 1553 default: 1554 ST_DEBUG(0, st_label, SCSI_DEBUG, "st_detach failed\n"); 1555 return (DDI_FAILURE); 1556 } 1557 } 1558 1559 1560 /* ARGSUSED */ 1561 static int 1562 st_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 1563 { 1564 dev_t dev; 1565 struct scsi_tape *un; 1566 int instance, error; 1567 1568 ST_ENTR(dip, st_info); 1569 1570 switch (infocmd) { 1571 case DDI_INFO_DEVT2DEVINFO: 1572 dev = (dev_t)arg; 1573 instance = MTUNIT(dev); 1574 if ((un = ddi_get_soft_state(st_state, instance)) == NULL) 1575 return (DDI_FAILURE); 1576 *result = (void *) ST_DEVINFO; 1577 error = DDI_SUCCESS; 1578 break; 1579 case DDI_INFO_DEVT2INSTANCE: 1580 dev = (dev_t)arg; 1581 instance = MTUNIT(dev); 1582 *result = (void *)(uintptr_t)instance; 1583 error = DDI_SUCCESS; 1584 break; 1585 default: 1586 error = DDI_FAILURE; 1587 } 1588 return (error); 1589 } 1590 1591 static int 1592 st_doattach(struct scsi_device *devp, int (*canwait)()) 1593 { 1594 struct scsi_tape *un = NULL; 1595 recov_info *ri; 1596 int km_flags = (canwait != NULL_FUNC) ? KM_SLEEP : KM_NOSLEEP; 1597 int instance; 1598 size_t rlen; 1599 1600 ST_FUNC(devp->sd_dev, st_doattach); 1601 /* 1602 * Call the routine scsi_probe to do some of the dirty work. 1603 * If the INQUIRY command succeeds, the field sd_inq in the 1604 * device structure will be filled in. 1605 */ 1606 ST_DEBUG(devp->sd_dev, st_label, SCSI_DEBUG, 1607 "st_doattach(): probing\n"); 1608 1609 if (scsi_probe(devp, canwait) == SCSIPROBE_EXISTS) { 1610 1611 /* 1612 * In checking the whole inq_dtype byte we are looking at both 1613 * the Peripheral Qualifier and the Peripheral Device Type. 1614 * For this driver we are only interested in sequential devices 1615 * that are connected or capable if connecting to this logical 1616 * unit. 1617 */ 1618 if (devp->sd_inq->inq_dtype == 1619 (DTYPE_SEQUENTIAL | DPQ_POSSIBLE)) { 1620 ST_DEBUG(devp->sd_dev, st_label, SCSI_DEBUG, 1621 "probe exists\n"); 1622 } else { 1623 /* Something there but not a tape device */ 1624 scsi_unprobe(devp); 1625 return (DDI_FAILURE); 1626 } 1627 } else { 1628 /* Nothing there */ 1629 ST_DEBUG(devp->sd_dev, st_label, SCSI_DEBUG, 1630 "probe failure: nothing there\n"); 1631 scsi_unprobe(devp); 1632 return (DDI_FAILURE); 1633 } 1634 1635 1636 /* 1637 * The actual unit is present. 1638 * Now is the time to fill in the rest of our info.. 1639 */ 1640 instance = ddi_get_instance(devp->sd_dev); 1641 1642 if (ddi_soft_state_zalloc(st_state, instance) != DDI_SUCCESS) { 1643 goto error; 1644 } 1645 un = ddi_get_soft_state(st_state, instance); 1646 1647 ASSERT(un != NULL); 1648 1649 un->un_rqs_bp = scsi_alloc_consistent_buf(&devp->sd_address, NULL, 1650 MAX_SENSE_LENGTH, B_READ, canwait, NULL); 1651 if (un->un_rqs_bp == NULL) { 1652 goto error; 1653 } 1654 un->un_rqs = scsi_init_pkt(&devp->sd_address, NULL, un->un_rqs_bp, 1655 CDB_GROUP0, 1, st_recov_sz, PKT_CONSISTENT, canwait, NULL); 1656 if (!un->un_rqs) { 1657 goto error; 1658 } 1659 ASSERT(un->un_rqs->pkt_resid == 0); 1660 devp->sd_sense = 1661 (struct scsi_extended_sense *)un->un_rqs_bp->b_un.b_addr; 1662 ASSERT(geterror(un->un_rqs_bp) == NULL); 1663 1664 (void) scsi_setup_cdb((union scsi_cdb *)un->un_rqs->pkt_cdbp, 1665 SCMD_REQUEST_SENSE, 0, MAX_SENSE_LENGTH, 0); 1666 FILL_SCSI1_LUN(devp, un->un_rqs); 1667 un->un_rqs->pkt_flags |= (FLAG_SENSING | FLAG_HEAD | FLAG_NODISCON); 1668 un->un_rqs->pkt_time = st_io_time; 1669 un->un_rqs->pkt_comp = st_intr; 1670 ri = (recov_info *)un->un_rqs->pkt_private; 1671 if (st_recov_sz == sizeof (recov_info)) { 1672 ri->privatelen = sizeof (recov_info); 1673 } else { 1674 ri->privatelen = sizeof (pkt_info); 1675 } 1676 1677 un->un_sbufp = getrbuf(km_flags); 1678 un->un_recov_buf = getrbuf(km_flags); 1679 1680 un->un_uscsi_rqs_buf = kmem_alloc(SENSE_LENGTH, KM_SLEEP); 1681 1682 /* 1683 * use i_ddi_mem_alloc() for now until we have an interface to allocate 1684 * memory for DMA which doesn't require a DMA handle. ddi_iopb_alloc() 1685 * is obsolete and we want more flexibility in controlling the DMA 1686 * address constraints. 1687 */ 1688 (void) i_ddi_mem_alloc(devp->sd_dev, &st_alloc_attr, 1689 sizeof (struct seq_mode), ((km_flags == KM_SLEEP) ? 1 : 0), 0, 1690 NULL, (caddr_t *)&un->un_mspl, &rlen, NULL); 1691 1692 (void) i_ddi_mem_alloc(devp->sd_dev, &st_alloc_attr, 1693 sizeof (read_pos_data_t), ((km_flags == KM_SLEEP) ? 1 : 0), 0, 1694 NULL, (caddr_t *)&un->un_read_pos_data, &rlen, NULL); 1695 1696 if (!un->un_sbufp || !un->un_mspl || !un->un_read_pos_data) { 1697 ST_DEBUG6(devp->sd_dev, st_label, SCSI_DEBUG, 1698 "probe partial failure: no space\n"); 1699 goto error; 1700 } 1701 1702 bzero(un->un_mspl, sizeof (struct seq_mode)); 1703 1704 cv_init(&un->un_sbuf_cv, NULL, CV_DRIVER, NULL); 1705 cv_init(&un->un_queue_cv, NULL, CV_DRIVER, NULL); 1706 cv_init(&un->un_clscv, NULL, CV_DRIVER, NULL); 1707 cv_init(&un->un_state_cv, NULL, CV_DRIVER, NULL); 1708 #ifdef __x86 1709 cv_init(&un->un_contig_mem_cv, NULL, CV_DRIVER, NULL); 1710 #endif 1711 1712 /* Initialize power managemnet condition variable */ 1713 cv_init(&un->un_suspend_cv, NULL, CV_DRIVER, NULL); 1714 cv_init(&un->un_tape_busy_cv, NULL, CV_DRIVER, NULL); 1715 cv_init(&un->un_recov_buf_cv, NULL, CV_DRIVER, NULL); 1716 1717 un->un_recov_taskq = ddi_taskq_create(devp->sd_dev, 1718 "un_recov_taskq", 1, TASKQ_DEFAULTPRI, km_flags); 1719 1720 ASSERT(un->un_recov_taskq != NULL); 1721 1722 un->un_pos.pmode = invalid; 1723 un->un_sd = devp; 1724 un->un_swr_token = (opaque_t)NULL; 1725 un->un_comp_page = ST_DEV_DATACOMP_PAGE | ST_DEV_CONFIG_PAGE; 1726 un->un_wormable = st_is_drive_worm; 1727 un->un_media_id_method = st_get_media_identification; 1728 /* 1729 * setting long a initial as it contains logical file info. 1730 * support for long format is mandatory but many drive don't do it. 1731 */ 1732 un->un_read_pos_type = LONG_POS; 1733 1734 un->un_suspend_pos.pmode = invalid; 1735 1736 st_add_recovery_info_to_pkt(un, un->un_rqs_bp, un->un_rqs); 1737 1738 #ifdef __x86 1739 if (ddi_dma_alloc_handle(ST_DEVINFO, &st_contig_mem_dma_attr, 1740 DDI_DMA_SLEEP, NULL, &un->un_contig_mem_hdl) != DDI_SUCCESS) { 1741 ST_DEBUG6(devp->sd_dev, st_label, SCSI_DEBUG, 1742 "allocation of contiguous memory dma handle failed!"); 1743 un->un_contig_mem_hdl = NULL; 1744 goto error; 1745 } 1746 #endif 1747 1748 /* 1749 * Since this driver manages devices with "remote" hardware, 1750 * i.e. the devices themselves have no "reg" properties, 1751 * the SUSPEND/RESUME commands in detach/attach will not be 1752 * called by the power management framework unless we request 1753 * it by creating a "pm-hardware-state" property and setting it 1754 * to value "needs-suspend-resume". 1755 */ 1756 if (ddi_prop_update_string(DDI_DEV_T_NONE, devp->sd_dev, 1757 "pm-hardware-state", "needs-suspend-resume") != 1758 DDI_PROP_SUCCESS) { 1759 1760 ST_DEBUG(devp->sd_dev, st_label, SCSI_DEBUG, 1761 "ddi_prop_update(\"pm-hardware-state\") failed\n"); 1762 goto error; 1763 } 1764 1765 if (ddi_prop_create(DDI_DEV_T_NONE, devp->sd_dev, DDI_PROP_CANSLEEP, 1766 "no-involuntary-power-cycles", NULL, 0) != DDI_PROP_SUCCESS) { 1767 1768 ST_DEBUG(devp->sd_dev, st_label, SCSI_DEBUG, 1769 "ddi_prop_create(\"no-involuntary-power-cycles\") " 1770 "failed\n"); 1771 goto error; 1772 } 1773 1774 (void) scsi_reset_notify(ROUTE, SCSI_RESET_NOTIFY, 1775 st_reset_notification, (caddr_t)un); 1776 1777 ST_DEBUG6(devp->sd_dev, st_label, SCSI_DEBUG, "attach success\n"); 1778 return (DDI_SUCCESS); 1779 1780 error: 1781 devp->sd_sense = NULL; 1782 1783 ddi_remove_minor_node(devp->sd_dev, NULL); 1784 if (un) { 1785 if (un->un_mspl) { 1786 i_ddi_mem_free((caddr_t)un->un_mspl, NULL); 1787 } 1788 if (un->un_read_pos_data) { 1789 i_ddi_mem_free((caddr_t)un->un_read_pos_data, 0); 1790 } 1791 if (un->un_sbufp) { 1792 freerbuf(un->un_sbufp); 1793 } 1794 if (un->un_recov_buf) { 1795 freerbuf(un->un_recov_buf); 1796 } 1797 if (un->un_uscsi_rqs_buf) { 1798 kmem_free(un->un_uscsi_rqs_buf, SENSE_LENGTH); 1799 } 1800 #ifdef __x86 1801 if (un->un_contig_mem_hdl != NULL) { 1802 ddi_dma_free_handle(&un->un_contig_mem_hdl); 1803 } 1804 #endif 1805 if (un->un_rqs) { 1806 scsi_destroy_pkt(un->un_rqs); 1807 } 1808 1809 if (un->un_rqs_bp) { 1810 scsi_free_consistent_buf(un->un_rqs_bp); 1811 } 1812 1813 ddi_soft_state_free(st_state, instance); 1814 devp->sd_private = NULL; 1815 } 1816 1817 if (devp->sd_inq) { 1818 scsi_unprobe(devp); 1819 } 1820 return (DDI_FAILURE); 1821 } 1822 1823 typedef int 1824 (*cfg_functp)(struct scsi_tape *, char *vidpid, struct st_drivetype *); 1825 1826 static cfg_functp config_functs[] = { 1827 st_get_conf_from_st_dot_conf, 1828 st_get_conf_from_st_conf_dot_c, 1829 st_get_conf_from_tape_drive, 1830 st_get_default_conf 1831 }; 1832 1833 1834 /* 1835 * determine tape type, using tape-config-list or built-in table or 1836 * use a generic tape config entry 1837 */ 1838 static void 1839 st_known_tape_type(struct scsi_tape *un) 1840 { 1841 struct st_drivetype *dp; 1842 cfg_functp *config_funct; 1843 uchar_t reserved; 1844 1845 ST_FUNC(ST_DEVINFO, st_known_tape_type); 1846 1847 reserved = (un->un_rsvd_status & ST_RESERVE) ? ST_RESERVE 1848 : ST_RELEASE; 1849 1850 /* 1851 * XXX: Emulex MT-02 (and emulators) predates SCSI-1 and has 1852 * no vid & pid inquiry data. So, we provide one. 1853 */ 1854 if (ST_INQUIRY->inq_len == 0 || 1855 (bcmp("\0\0\0\0\0\0\0\0", ST_INQUIRY->inq_vid, 8) == 0)) { 1856 (void) strcpy((char *)ST_INQUIRY->inq_vid, ST_MT02_NAME); 1857 } 1858 1859 if (un->un_dp_size == 0) { 1860 un->un_dp_size = sizeof (struct st_drivetype); 1861 dp = kmem_zalloc((size_t)un->un_dp_size, KM_SLEEP); 1862 un->un_dp = dp; 1863 } else { 1864 dp = un->un_dp; 1865 } 1866 1867 un->un_dp->non_motion_timeout = st_io_time; 1868 /* 1869 * Loop through the configuration methods till one works. 1870 */ 1871 for (config_funct = &config_functs[0]; ; config_funct++) { 1872 if ((*config_funct)(un, ST_INQUIRY->inq_vid, dp)) { 1873 break; 1874 } 1875 } 1876 1877 /* 1878 * If we didn't just make up this configuration and 1879 * all the density codes are the same.. 1880 * Set Auto Density over ride. 1881 */ 1882 if (*config_funct != st_get_default_conf) { 1883 /* 1884 * If this device is one that is configured and all 1885 * densities are the same, This saves doing gets and set 1886 * that yield nothing. 1887 */ 1888 if ((dp->densities[0]) == (dp->densities[1]) && 1889 (dp->densities[0]) == (dp->densities[2]) && 1890 (dp->densities[0]) == (dp->densities[3])) { 1891 1892 dp->options |= ST_AUTODEN_OVERRIDE; 1893 } 1894 } 1895 1896 1897 /* 1898 * Store tape drive characteristics. 1899 */ 1900 un->un_status = 0; 1901 un->un_attached = 1; 1902 un->un_init_options = dp->options; 1903 1904 /* setup operation time-outs based on options */ 1905 st_calculate_timeouts(un); 1906 1907 /* make sure if we are supposed to be variable, make it variable */ 1908 if (dp->options & ST_VARIABLE) { 1909 dp->bsize = 0; 1910 } 1911 1912 if (reserved != ((un->un_rsvd_status & ST_RESERVE) ? ST_RESERVE 1913 : ST_RELEASE)) { 1914 (void) st_reserve_release(un, reserved, st_uscsi_cmd); 1915 } 1916 1917 un->un_unit_attention_flags |= 1; 1918 1919 scsi_log(ST_DEVINFO, st_label, CE_NOTE, "?<%s>\n", dp->name); 1920 1921 } 1922 1923 1924 typedef struct { 1925 int mask; 1926 int bottom; 1927 int top; 1928 char *name; 1929 } conf_limit; 1930 1931 static const conf_limit conf_limits[] = { 1932 1933 -1, 1, 2, "conf version", 1934 -1, MT_ISTS, ST_LAST_TYPE, "drive type", 1935 -1, 0, 0xffffff, "block size", 1936 ST_VALID_OPTS, 0, ST_VALID_OPTS, "options", 1937 -1, 0, 4, "number of densities", 1938 -1, 0, UINT8_MAX, "density code", 1939 -1, 0, 3, "default density", 1940 -1, 0, UINT16_MAX, "non motion timeout", 1941 -1, 0, UINT16_MAX, "I/O timeout", 1942 -1, 0, UINT16_MAX, "space timeout", 1943 -1, 0, UINT16_MAX, "load timeout", 1944 -1, 0, UINT16_MAX, "unload timeout", 1945 -1, 0, UINT16_MAX, "erase timeout", 1946 0, 0, 0, NULL 1947 }; 1948 1949 static int 1950 st_validate_conf_data(struct scsi_tape *un, int *list, int list_len, 1951 const char *conf_name) 1952 { 1953 int dens; 1954 int ndens; 1955 int value; 1956 int type; 1957 int count; 1958 const conf_limit *limit = &conf_limits[0]; 1959 1960 ST_FUNC(ST_DEVINFO, st_validate_conf_data); 1961 1962 ST_DEBUG3(ST_DEVINFO, st_label, CE_NOTE, 1963 "Checking %d entrys total with %d densities\n", list_len, list[4]); 1964 1965 count = list_len; 1966 type = *list; 1967 for (; count && limit->name; count--, list++, limit++) { 1968 1969 value = *list; 1970 if (value & ~limit->mask) { 1971 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 1972 "%s %s value invalid bits set: 0x%X\n", 1973 conf_name, limit->name, value & ~limit->mask); 1974 *list &= limit->mask; 1975 } else if (value < limit->bottom) { 1976 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 1977 "%s %s value too low: value = %d limit %d\n", 1978 conf_name, limit->name, value, limit->bottom); 1979 } else if (value > limit->top) { 1980 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 1981 "%s %s value too high: value = %d limit %d\n", 1982 conf_name, limit->name, value, limit->top); 1983 } else { 1984 ST_DEBUG3(ST_DEVINFO, st_label, CE_CONT, 1985 "%s %s value = 0x%X\n", 1986 conf_name, limit->name, value); 1987 } 1988 1989 /* If not the number of densities continue */ 1990 if (limit != &conf_limits[4]) { 1991 continue; 1992 } 1993 1994 /* If number of densities is not in range can't use config */ 1995 if (value < limit->bottom || value > limit->top) { 1996 return (-1); 1997 } 1998 1999 ndens = min(value, NDENSITIES); 2000 if ((type == 1) && (list_len - ndens) != 6) { 2001 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 2002 "%s conf version 1 with %d densities has %d items" 2003 " should have %d", 2004 conf_name, ndens, list_len, 6 + ndens); 2005 } else if ((type == 2) && (list_len - ndens) != 13) { 2006 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 2007 "%s conf version 2 with %d densities has %d items" 2008 " should have %d", 2009 conf_name, ndens, list_len, 13 + ndens); 2010 } 2011 2012 limit++; 2013 for (dens = 0; dens < ndens && count; dens++) { 2014 count--; 2015 list++; 2016 value = *list; 2017 if (value < limit->bottom) { 2018 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 2019 "%s density[%d] value too low: value =" 2020 " 0x%X limit 0x%X\n", 2021 conf_name, dens, value, limit->bottom); 2022 } else if (value > limit->top) { 2023 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 2024 "%s density[%d] value too high: value =" 2025 " 0x%X limit 0x%X\n", 2026 conf_name, dens, value, limit->top); 2027 } else { 2028 ST_DEBUG3(ST_DEVINFO, st_label, CE_CONT, 2029 "%s density[%d] value = 0x%X\n", 2030 conf_name, dens, value); 2031 } 2032 } 2033 } 2034 2035 return (0); 2036 } 2037 2038 static int 2039 st_get_conf_from_st_dot_conf(struct scsi_tape *un, char *vidpid, 2040 struct st_drivetype *dp) 2041 { 2042 caddr_t config_list = NULL; 2043 caddr_t data_list = NULL; 2044 int *data_ptr; 2045 caddr_t vidptr, prettyptr, datanameptr; 2046 size_t vidlen, prettylen, datanamelen, tripletlen = 0; 2047 int config_list_len, data_list_len, len, i; 2048 int version; 2049 int found = 0; 2050 2051 ST_FUNC(ST_DEVINFO, st_get_conf_from_st_dot_conf); 2052 2053 /* 2054 * Determine type of tape controller. Type is determined by 2055 * checking the vendor ids of the earlier inquiry command and 2056 * comparing those with vids in tape-config-list defined in st.conf 2057 */ 2058 if (ddi_getlongprop(DDI_DEV_T_ANY, ST_DEVINFO, DDI_PROP_DONTPASS, 2059 "tape-config-list", (caddr_t)&config_list, &config_list_len) 2060 != DDI_PROP_SUCCESS) { 2061 return (found); 2062 } 2063 2064 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 2065 "st_get_conf_from_st_dot_conf(): st.conf has tape-config-list\n"); 2066 2067 /* 2068 * Compare vids in each triplet - if it matches, get value for 2069 * data_name and contruct a st_drivetype struct 2070 * tripletlen is not set yet! 2071 */ 2072 for (len = config_list_len, vidptr = config_list; 2073 len > 0; 2074 vidptr += tripletlen, len -= tripletlen) { 2075 2076 vidlen = strlen(vidptr); 2077 prettyptr = vidptr + vidlen + 1; 2078 prettylen = strlen(prettyptr); 2079 datanameptr = prettyptr + prettylen + 1; 2080 datanamelen = strlen(datanameptr); 2081 tripletlen = vidlen + prettylen + datanamelen + 3; 2082 2083 if (vidlen == 0) { 2084 continue; 2085 } 2086 2087 /* 2088 * If inquiry vid dosen't match this triplets vid, 2089 * try the next. 2090 */ 2091 if (strncasecmp(vidpid, vidptr, vidlen)) { 2092 continue; 2093 } 2094 2095 /* 2096 * if prettylen is zero then use the vid string 2097 */ 2098 if (prettylen == 0) { 2099 prettyptr = vidptr; 2100 prettylen = vidlen; 2101 } 2102 2103 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 2104 "vid = %s, pretty=%s, dataname = %s\n", 2105 vidptr, prettyptr, datanameptr); 2106 2107 /* 2108 * get the data list 2109 */ 2110 if (ddi_getlongprop(DDI_DEV_T_ANY, ST_DEVINFO, 0, 2111 datanameptr, (caddr_t)&data_list, 2112 &data_list_len) != DDI_PROP_SUCCESS) { 2113 /* 2114 * Error in getting property value 2115 * print warning! 2116 */ 2117 scsi_log(ST_DEVINFO, st_label, CE_WARN, 2118 "data property (%s) has no value\n", 2119 datanameptr); 2120 continue; 2121 } 2122 2123 /* 2124 * now initialize the st_drivetype struct 2125 */ 2126 (void) strncpy(dp->name, prettyptr, ST_NAMESIZE - 1); 2127 dp->length = (int)min(vidlen, (VIDPIDLEN - 1)); 2128 (void) strncpy(dp->vid, vidptr, dp->length); 2129 data_ptr = (int *)data_list; 2130 /* 2131 * check if data is enough for version, type, 2132 * bsize, options, # of densities, density1, 2133 * density2, ..., default_density 2134 */ 2135 if ((data_list_len < 5 * sizeof (int)) || 2136 (data_list_len < 6 * sizeof (int) + 2137 *(data_ptr + 4) * sizeof (int))) { 2138 /* 2139 * print warning and skip to next triplet. 2140 */ 2141 scsi_log(ST_DEVINFO, st_label, CE_WARN, 2142 "data property (%s) incomplete\n", 2143 datanameptr); 2144 kmem_free(data_list, data_list_len); 2145 continue; 2146 } 2147 2148 if (st_validate_conf_data(un, data_ptr, 2149 data_list_len / sizeof (int), datanameptr)) { 2150 kmem_free(data_list, data_list_len); 2151 scsi_log(ST_DEVINFO, st_label, CE_WARN, 2152 "data property (%s) rejected\n", 2153 datanameptr); 2154 continue; 2155 } 2156 2157 /* 2158 * check version 2159 */ 2160 version = *data_ptr++; 2161 if (version != 1 && version != 2) { 2162 /* print warning but accept it */ 2163 scsi_log(ST_DEVINFO, st_label, CE_WARN, 2164 "Version # for data property (%s) " 2165 "not set to 1 or 2\n", datanameptr); 2166 } 2167 2168 dp->type = *data_ptr++; 2169 dp->bsize = *data_ptr++; 2170 dp->options = *data_ptr++; 2171 dp->options |= ST_DYNAMIC; 2172 len = *data_ptr++; 2173 for (i = 0; i < NDENSITIES; i++) { 2174 if (i < len) { 2175 dp->densities[i] = *data_ptr++; 2176 } 2177 } 2178 dp->default_density = *data_ptr << 3; 2179 if (version == 2 && 2180 data_list_len >= (13 + len) * sizeof (int)) { 2181 data_ptr++; 2182 dp->non_motion_timeout = *data_ptr++; 2183 dp->io_timeout = *data_ptr++; 2184 dp->rewind_timeout = *data_ptr++; 2185 dp->space_timeout = *data_ptr++; 2186 dp->load_timeout = *data_ptr++; 2187 dp->unload_timeout = *data_ptr++; 2188 dp->erase_timeout = *data_ptr++; 2189 } 2190 kmem_free(data_list, data_list_len); 2191 found = 1; 2192 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 2193 "found in st.conf: vid = %s, pretty=%s\n", 2194 dp->vid, dp->name); 2195 break; 2196 } 2197 2198 /* 2199 * free up the memory allocated by ddi_getlongprop 2200 */ 2201 if (config_list) { 2202 kmem_free(config_list, config_list_len); 2203 } 2204 return (found); 2205 } 2206 2207 static int 2208 st_get_conf_from_st_conf_dot_c(struct scsi_tape *un, char *vidpid, 2209 struct st_drivetype *dp) 2210 { 2211 int i; 2212 2213 ST_FUNC(ST_DEVINFO, st_get_conf_from_st_conf_dot_c); 2214 /* 2215 * Determine type of tape controller. Type is determined by 2216 * checking the result of the earlier inquiry command and 2217 * comparing vendor ids with strings in a table declared in st_conf.c. 2218 */ 2219 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2220 "st_get_conf_from_st_conf_dot_c(): looking at st_drivetypes\n"); 2221 2222 for (i = 0; i < st_ndrivetypes; i++) { 2223 if (st_drivetypes[i].length == 0) { 2224 continue; 2225 } 2226 if (strncasecmp(vidpid, st_drivetypes[i].vid, 2227 st_drivetypes[i].length)) { 2228 continue; 2229 } 2230 bcopy(&st_drivetypes[i], dp, sizeof (st_drivetypes[i])); 2231 return (1); 2232 } 2233 return (0); 2234 } 2235 2236 static int 2237 st_get_conf_from_tape_drive(struct scsi_tape *un, char *vidpid, 2238 struct st_drivetype *dp) 2239 { 2240 int bsize; 2241 ulong_t maxbsize; 2242 caddr_t buf; 2243 struct st_drivetype *tem_dp; 2244 struct read_blklim *blklim; 2245 int rval; 2246 int i; 2247 2248 ST_FUNC(ST_DEVINFO, st_get_conf_from_tape_drive); 2249 2250 /* 2251 * Determine the type of tape controller. Type is determined by 2252 * sending SCSI commands to tape drive and deriving the type from 2253 * the returned data. 2254 */ 2255 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2256 "st_get_conf_from_tape_drive(): asking tape drive\n"); 2257 2258 tem_dp = kmem_zalloc(sizeof (struct st_drivetype), KM_SLEEP); 2259 2260 /* 2261 * Make up a name 2262 */ 2263 bcopy(vidpid, tem_dp->name, VIDPIDLEN); 2264 tem_dp->name[VIDPIDLEN] = '\0'; 2265 tem_dp->length = min(strlen(ST_INQUIRY->inq_vid), (VIDPIDLEN - 1)); 2266 (void) strncpy(tem_dp->vid, ST_INQUIRY->inq_vid, tem_dp->length); 2267 /* 2268 * 'clean' vendor and product strings of non-printing chars 2269 */ 2270 for (i = 0; i < VIDPIDLEN - 1; i ++) { 2271 if (tem_dp->name[i] < ' ' || tem_dp->name[i] > '~') { 2272 tem_dp->name[i] = '.'; 2273 } 2274 } 2275 2276 /* 2277 * MODE SENSE to determine block size. 2278 */ 2279 un->un_dp->options |= ST_MODE_SEL_COMP | ST_UNLOADABLE; 2280 rval = st_modesense(un); 2281 if (rval) { 2282 if (rval == EACCES) { 2283 un->un_dp->type = ST_TYPE_INVALID; 2284 rval = 1; 2285 } else { 2286 un->un_dp->options &= ~ST_MODE_SEL_COMP; 2287 rval = 0; 2288 } 2289 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2290 "st_get_conf_from_tape_drive(): fail to mode sense\n"); 2291 goto exit; 2292 } 2293 2294 /* Can mode sense page 0x10 or 0xf */ 2295 tem_dp->options |= ST_MODE_SEL_COMP; 2296 bsize = (un->un_mspl->high_bl << 16) | 2297 (un->un_mspl->mid_bl << 8) | 2298 (un->un_mspl->low_bl); 2299 2300 if (bsize == 0) { 2301 tem_dp->options |= ST_VARIABLE; 2302 tem_dp->bsize = 0; 2303 } else if (bsize > ST_MAXRECSIZE_FIXED) { 2304 rval = st_change_block_size(un, 0); 2305 if (rval) { 2306 if (rval == EACCES) { 2307 un->un_dp->type = ST_TYPE_INVALID; 2308 rval = 1; 2309 } else { 2310 rval = 0; 2311 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2312 "st_get_conf_from_tape_drive(): " 2313 "Fixed record size is too large and" 2314 "cannot switch to variable record size"); 2315 } 2316 goto exit; 2317 } 2318 tem_dp->options |= ST_VARIABLE; 2319 } else { 2320 rval = st_change_block_size(un, 0); 2321 if (rval == 0) { 2322 tem_dp->options |= ST_VARIABLE; 2323 tem_dp->bsize = 0; 2324 } else if (rval != EACCES) { 2325 tem_dp->bsize = bsize; 2326 } else { 2327 un->un_dp->type = ST_TYPE_INVALID; 2328 rval = 1; 2329 goto exit; 2330 } 2331 } 2332 2333 /* 2334 * If READ BLOCk LIMITS works and upper block size limit is 2335 * more than 64K, ST_NO_RECSIZE_LIMIT is supported. 2336 */ 2337 blklim = kmem_zalloc(sizeof (struct read_blklim), KM_SLEEP); 2338 rval = st_read_block_limits(un, blklim); 2339 if (rval) { 2340 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2341 "st_get_conf_from_tape_drive(): " 2342 "fail to read block limits.\n"); 2343 rval = 0; 2344 kmem_free(blklim, sizeof (struct read_blklim)); 2345 goto exit; 2346 } 2347 maxbsize = (blklim->max_hi << 16) + 2348 (blklim->max_mid << 8) + blklim->max_lo; 2349 if (maxbsize > ST_MAXRECSIZE_VARIABLE) { 2350 tem_dp->options |= ST_NO_RECSIZE_LIMIT; 2351 } 2352 kmem_free(blklim, sizeof (struct read_blklim)); 2353 2354 /* 2355 * Inquiry VPD page 0xb0 to see if the tape drive supports WORM 2356 */ 2357 buf = kmem_zalloc(6, KM_SLEEP); 2358 rval = st_get_special_inquiry(un, 6, buf, 0xb0); 2359 if (rval) { 2360 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2361 "st_get_conf_from_tape_drive(): " 2362 "fail to read vitial inquiry.\n"); 2363 rval = 0; 2364 kmem_free(buf, 6); 2365 goto exit; 2366 } 2367 if (buf[4] & 1) { 2368 tem_dp->options |= ST_WORMABLE; 2369 } 2370 kmem_free(buf, 6); 2371 2372 /* Assume BSD BSR KNOWS_EOD */ 2373 tem_dp->options |= ST_BSF | ST_BSR | ST_KNOWS_EOD | ST_UNLOADABLE; 2374 tem_dp->max_rretries = -1; 2375 tem_dp->max_wretries = -1; 2376 2377 /* 2378 * Decide the densities supported by tape drive by sending 2379 * REPORT DENSITY SUPPORT command. 2380 */ 2381 if (st_get_densities_from_tape_drive(un, tem_dp) == 0) { 2382 goto exit; 2383 } 2384 2385 /* 2386 * Decide the timeout values for several commands by sending 2387 * REPORT SUPPORTED OPERATION CODES command. 2388 */ 2389 rval = st_get_timeout_values_from_tape_drive(un, tem_dp); 2390 if (rval == 0 || ((rval == 1) && (tem_dp->type == ST_TYPE_INVALID))) { 2391 goto exit; 2392 } 2393 2394 bcopy(tem_dp, dp, sizeof (struct st_drivetype)); 2395 rval = 1; 2396 2397 exit: 2398 un->un_status = KEY_NO_SENSE; 2399 kmem_free(tem_dp, sizeof (struct st_drivetype)); 2400 return (rval); 2401 } 2402 2403 static int 2404 st_get_densities_from_tape_drive(struct scsi_tape *un, 2405 struct st_drivetype *dp) 2406 { 2407 int i, p; 2408 size_t buflen; 2409 ushort_t des_len; 2410 uchar_t *den_header; 2411 uchar_t num_den; 2412 uchar_t den[NDENSITIES]; 2413 uchar_t deflt[NDENSITIES]; 2414 struct report_density_desc *den_desc; 2415 2416 ST_FUNC(ST_DEVINFO, st_get_densities_from_type_drive); 2417 2418 /* 2419 * Since we have no idea how many densitiy support entries 2420 * will be returned, we send the command firstly assuming 2421 * there is only one. Then we can decide the number of 2422 * entries by available density support length. If multiple 2423 * entries exist, we will resend the command with enough 2424 * buffer size. 2425 */ 2426 buflen = sizeof (struct report_density_header) + 2427 sizeof (struct report_density_desc); 2428 den_header = kmem_zalloc(buflen, KM_SLEEP); 2429 if (st_report_density_support(un, den_header, buflen) != 0) { 2430 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2431 "st_get_conf_from_tape_drive(): fail to report density.\n"); 2432 kmem_free(den_header, buflen); 2433 return (0); 2434 } 2435 des_len = 2436 BE_16(((struct report_density_header *)den_header)->ava_dens_len); 2437 num_den = (des_len - 2) / sizeof (struct report_density_desc); 2438 2439 if (num_den > 1) { 2440 kmem_free(den_header, buflen); 2441 buflen = sizeof (struct report_density_header) + 2442 sizeof (struct report_density_desc) * num_den; 2443 den_header = kmem_zalloc(buflen, KM_SLEEP); 2444 if (st_report_density_support(un, den_header, buflen) != 0) { 2445 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2446 "st_get_conf_from_tape_drive(): " 2447 "fail to report density.\n"); 2448 kmem_free(den_header, buflen); 2449 return (0); 2450 } 2451 } 2452 2453 den_desc = (struct report_density_desc *)(den_header 2454 + sizeof (struct report_density_header)); 2455 2456 /* 2457 * Decide the drive type by assigning organization 2458 */ 2459 for (i = 0; i < ST_NUM_MEMBERS(st_vid_dt); i ++) { 2460 if (strncmp(st_vid_dt[i].vid, (char *)(den_desc->ass_org), 2461 8) == 0) { 2462 dp->type = st_vid_dt[i].type; 2463 break; 2464 } 2465 } 2466 if (i == ST_NUM_MEMBERS(st_vid_dt)) { 2467 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2468 "st_get_conf_from_tape_drive(): " 2469 "can't find match of assigned ort.\n"); 2470 kmem_free(den_header, buflen); 2471 return (0); 2472 } 2473 2474 /* 2475 * The tape drive may support many tape formats, but the st driver 2476 * supports only the four highest densities. Since density code 2477 * values are returned by ascending sequence, we start from the 2478 * last entry of density support data block descriptor. 2479 */ 2480 p = 0; 2481 den_desc += num_den - 1; 2482 for (i = 0; i < num_den && p < NDENSITIES; i ++, den_desc --) { 2483 if ((den_desc->pri_den != 0) && (den_desc->wrtok)) { 2484 if (p != 0) { 2485 if (den_desc->pri_den >= den[p - 1]) { 2486 continue; 2487 } 2488 } 2489 den[p] = den_desc->pri_den; 2490 deflt[p] = den_desc->deflt; 2491 p ++; 2492 } 2493 } 2494 2495 switch (p) { 2496 case 0: 2497 bzero(dp->densities, NDENSITIES); 2498 dp->options |= ST_AUTODEN_OVERRIDE; 2499 dp->default_density = MT_DENSITY4; 2500 break; 2501 2502 case 1: 2503 (void) memset(dp->densities, den[0], NDENSITIES); 2504 dp->options |= ST_AUTODEN_OVERRIDE; 2505 dp->default_density = MT_DENSITY4; 2506 break; 2507 2508 case 2: 2509 dp->densities[0] = den[1]; 2510 dp->densities[1] = den[1]; 2511 dp->densities[2] = den[0]; 2512 dp->densities[3] = den[0]; 2513 if (deflt[0]) { 2514 dp->default_density = MT_DENSITY4; 2515 } else { 2516 dp->default_density = MT_DENSITY2; 2517 } 2518 break; 2519 2520 case 3: 2521 dp->densities[0] = den[2]; 2522 dp->densities[1] = den[1]; 2523 dp->densities[2] = den[0]; 2524 dp->densities[3] = den[0]; 2525 if (deflt[0]) { 2526 dp->default_density = MT_DENSITY4; 2527 } else if (deflt[1]) { 2528 dp->default_density = MT_DENSITY2; 2529 } else { 2530 dp->default_density = MT_DENSITY1; 2531 } 2532 break; 2533 2534 default: 2535 for (i = p; i > p - NDENSITIES; i --) { 2536 dp->densities[i - 1] = den[p - i]; 2537 } 2538 if (deflt[0]) { 2539 dp->default_density = MT_DENSITY4; 2540 } else if (deflt[1]) { 2541 dp->default_density = MT_DENSITY3; 2542 } else if (deflt[2]) { 2543 dp->default_density = MT_DENSITY2; 2544 } else { 2545 dp->default_density = MT_DENSITY1; 2546 } 2547 break; 2548 } 2549 2550 bzero(dp->mediatype, NDENSITIES); 2551 2552 kmem_free(den_header, buflen); 2553 return (1); 2554 } 2555 2556 static int 2557 st_get_timeout_values_from_tape_drive(struct scsi_tape *un, 2558 struct st_drivetype *dp) 2559 { 2560 ushort_t timeout; 2561 int rval; 2562 2563 ST_FUNC(ST_DEVINFO, st_get_timeout_values_from_type_drive); 2564 2565 rval = st_get_timeouts_value(un, SCMD_ERASE, &timeout, 0); 2566 if (rval) { 2567 if (rval == EACCES) { 2568 un->un_dp->type = ST_TYPE_INVALID; 2569 dp->type = ST_TYPE_INVALID; 2570 return (1); 2571 } 2572 return (0); 2573 } 2574 dp->erase_timeout = timeout; 2575 2576 rval = st_get_timeouts_value(un, SCMD_READ, &timeout, 0); 2577 if (rval) { 2578 if (rval == EACCES) { 2579 un->un_dp->type = ST_TYPE_INVALID; 2580 dp->type = ST_TYPE_INVALID; 2581 return (1); 2582 } 2583 return (0); 2584 } 2585 dp->io_timeout = timeout; 2586 2587 rval = st_get_timeouts_value(un, SCMD_WRITE, &timeout, 0); 2588 if (rval) { 2589 if (rval == EACCES) { 2590 un->un_dp->type = ST_TYPE_INVALID; 2591 dp->type = ST_TYPE_INVALID; 2592 return (1); 2593 } 2594 return (0); 2595 } 2596 dp->io_timeout = max(dp->io_timeout, timeout); 2597 2598 rval = st_get_timeouts_value(un, SCMD_SPACE, &timeout, 0); 2599 if (rval) { 2600 if (rval == EACCES) { 2601 un->un_dp->type = ST_TYPE_INVALID; 2602 dp->type = ST_TYPE_INVALID; 2603 return (1); 2604 } 2605 return (0); 2606 } 2607 dp->space_timeout = timeout; 2608 2609 rval = st_get_timeouts_value(un, SCMD_LOAD, &timeout, 0); 2610 if (rval) { 2611 if (rval == EACCES) { 2612 un->un_dp->type = ST_TYPE_INVALID; 2613 dp->type = ST_TYPE_INVALID; 2614 return (1); 2615 } 2616 return (0); 2617 } 2618 dp->load_timeout = timeout; 2619 dp->unload_timeout = timeout; 2620 2621 rval = st_get_timeouts_value(un, SCMD_REWIND, &timeout, 0); 2622 if (rval) { 2623 if (rval == EACCES) { 2624 un->un_dp->type = ST_TYPE_INVALID; 2625 dp->type = ST_TYPE_INVALID; 2626 return (1); 2627 } 2628 return (0); 2629 } 2630 dp->rewind_timeout = timeout; 2631 2632 rval = st_get_timeouts_value(un, SCMD_INQUIRY, &timeout, 0); 2633 if (rval) { 2634 if (rval == EACCES) { 2635 un->un_dp->type = ST_TYPE_INVALID; 2636 dp->type = ST_TYPE_INVALID; 2637 return (1); 2638 } 2639 return (0); 2640 } 2641 dp->non_motion_timeout = timeout; 2642 2643 return (1); 2644 } 2645 2646 static int 2647 st_get_timeouts_value(struct scsi_tape *un, uchar_t option_code, 2648 ushort_t *timeout_value, ushort_t service_action) 2649 { 2650 uchar_t *timeouts; 2651 uchar_t *oper; 2652 uchar_t support; 2653 uchar_t cdbsize; 2654 uchar_t ctdp; 2655 size_t buflen; 2656 int rval; 2657 2658 ST_FUNC(ST_DEVINFO, st_get_timeouts_value); 2659 2660 buflen = sizeof (struct one_com_des) + 2661 sizeof (struct com_timeout_des); 2662 oper = kmem_zalloc(buflen, KM_SLEEP); 2663 rval = st_report_supported_operation(un, oper, option_code, 2664 service_action); 2665 2666 if (rval) { 2667 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2668 "st_get_timeouts_value(): " 2669 "fail to timeouts value for command %d.\n", option_code); 2670 kmem_free(oper, buflen); 2671 return (rval); 2672 } 2673 2674 support = ((struct one_com_des *)oper)->support; 2675 if ((support != SUPPORT_VALUES_SUPPORT_SCSI) && 2676 (support != SUPPORT_VALUES_SUPPORT_VENDOR)) { 2677 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2678 "st_get_timeouts_value(): " 2679 "command %d is not supported.\n", option_code); 2680 kmem_free(oper, buflen); 2681 return (ENOTSUP); 2682 } 2683 2684 ctdp = ((struct one_com_des *)oper)->ctdp; 2685 if (!ctdp) { 2686 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2687 "st_get_timeouts_value(): " 2688 "command timeout is not included.\n"); 2689 kmem_free(oper, buflen); 2690 return (ENOTSUP); 2691 } 2692 2693 cdbsize = BE_16(((struct one_com_des *)oper)->cdb_size); 2694 timeouts = (uchar_t *)(oper + cdbsize + 4); 2695 2696 /* 2697 * Timeout value in seconds is 4 bytes, but we only support the lower 2 2698 * bytes. If the higher 2 bytes are not zero, the timeout value is set 2699 * to 0xFFFF. 2700 */ 2701 if (*(timeouts + 8) != 0 || *(timeouts + 9) != 0) { 2702 *timeout_value = USHRT_MAX; 2703 } else { 2704 *timeout_value = ((*(timeouts + 10)) << 8) | 2705 (*(timeouts + 11)); 2706 } 2707 2708 kmem_free(oper, buflen); 2709 return (0); 2710 } 2711 2712 static int 2713 st_get_default_conf(struct scsi_tape *un, char *vidpid, struct st_drivetype *dp) 2714 { 2715 int i; 2716 2717 ST_FUNC(ST_DEVINFO, st_get_default_conf); 2718 2719 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 2720 "st_get_default_conf(): making drivetype from INQ cmd\n"); 2721 2722 /* 2723 * Make up a name 2724 */ 2725 bcopy("Vendor '", dp->name, 8); 2726 bcopy(vidpid, &dp->name[8], VIDLEN); 2727 bcopy("' Product '", &dp->name[16], 11); 2728 bcopy(&vidpid[8], &dp->name[27], PIDLEN); 2729 dp->name[ST_NAMESIZE - 2] = '\''; 2730 dp->name[ST_NAMESIZE - 1] = '\0'; 2731 dp->length = min(strlen(ST_INQUIRY->inq_vid), (VIDPIDLEN - 1)); 2732 (void) strncpy(dp->vid, ST_INQUIRY->inq_vid, dp->length); 2733 /* 2734 * 'clean' vendor and product strings of non-printing chars 2735 */ 2736 for (i = 0; i < ST_NAMESIZE - 2; i++) { 2737 if (dp->name[i] < ' ' || dp->name[i] > '~') { 2738 dp->name[i] = '.'; 2739 } 2740 } 2741 dp->type = ST_TYPE_INVALID; 2742 dp->options |= (ST_DYNAMIC | ST_UNLOADABLE | ST_MODE_SEL_COMP); 2743 2744 return (1); /* Can Not Fail */ 2745 } 2746 2747 /* 2748 * Regular Unix Entry points 2749 */ 2750 2751 2752 2753 /* ARGSUSED */ 2754 static int 2755 st_open(dev_t *dev_p, int flag, int otyp, cred_t *cred_p) 2756 { 2757 dev_t dev = *dev_p; 2758 int rval = 0; 2759 2760 GET_SOFT_STATE(dev); 2761 2762 ST_ENTR(ST_DEVINFO, st_open); 2763 2764 /* 2765 * validate that we are addressing a sensible unit 2766 */ 2767 mutex_enter(ST_MUTEX); 2768 2769 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 2770 "st_open(node = %s dev = 0x%lx, flag = %d, otyp = %d)\n", 2771 st_dev_name(dev), *dev_p, flag, otyp); 2772 2773 /* 2774 * All device accesss go thru st_strategy() where we check 2775 * suspend status 2776 */ 2777 2778 if (!un->un_attached) { 2779 st_known_tape_type(un); 2780 if (!un->un_attached) { 2781 rval = ENXIO; 2782 goto exit; 2783 } 2784 2785 } 2786 2787 /* 2788 * Check for the case of the tape in the middle of closing. 2789 * This isn't simply a check of the current state, because 2790 * we could be in state of sensing with the previous state 2791 * that of closing. 2792 * 2793 * And don't allow multiple opens. 2794 */ 2795 if (!(flag & (FNDELAY | FNONBLOCK)) && IS_CLOSING(un)) { 2796 un->un_laststate = un->un_state; 2797 un->un_state = ST_STATE_CLOSE_PENDING_OPEN; 2798 while (IS_CLOSING(un) || 2799 un->un_state == ST_STATE_CLOSE_PENDING_OPEN) { 2800 if (cv_wait_sig(&un->un_clscv, ST_MUTEX) == 0) { 2801 rval = EINTR; 2802 un->un_state = un->un_laststate; 2803 goto exit; 2804 } 2805 } 2806 } else if (un->un_state != ST_STATE_CLOSED) { 2807 rval = EBUSY; 2808 goto busy; 2809 } 2810 2811 /* 2812 * record current dev 2813 */ 2814 un->un_dev = dev; 2815 un->un_oflags = flag; /* save for use in st_tape_init() */ 2816 un->un_errno = 0; /* no errors yet */ 2817 un->un_restore_pos = 0; 2818 un->un_rqs_state = 0; 2819 2820 /* 2821 * If we are opening O_NDELAY, or O_NONBLOCK, we don't check for 2822 * anything, leave internal states alone, if fileno >= 0 2823 */ 2824 if (flag & (FNDELAY | FNONBLOCK)) { 2825 switch (un->un_pos.pmode) { 2826 2827 case invalid: 2828 un->un_state = ST_STATE_OFFLINE; 2829 break; 2830 2831 case legacy: 2832 /* 2833 * If position is anything other than rewound. 2834 */ 2835 if (un->un_pos.fileno != 0 || un->un_pos.blkno != 0) { 2836 /* 2837 * set un_read_only/write-protect status. 2838 * 2839 * If the tape is not bot we can assume 2840 * that mspl->wp_status is set properly. 2841 * else 2842 * we need to do a mode sense/Tur once 2843 * again to get the actual tape status.(since 2844 * user might have replaced the tape) 2845 * Hence make the st state OFFLINE so that 2846 * we re-intialize the tape once again. 2847 */ 2848 un->un_read_only = 2849 (un->un_oflags & FWRITE) ? RDWR : RDONLY; 2850 un->un_state = ST_STATE_OPEN_PENDING_IO; 2851 } else { 2852 un->un_state = ST_STATE_OFFLINE; 2853 } 2854 break; 2855 case logical: 2856 if (un->un_pos.lgclblkno == 0) { 2857 un->un_state = ST_STATE_OFFLINE; 2858 } else { 2859 un->un_read_only = 2860 (un->un_oflags & FWRITE) ? RDWR : RDONLY; 2861 un->un_state = ST_STATE_OPEN_PENDING_IO; 2862 } 2863 break; 2864 } 2865 rval = 0; 2866 } else { 2867 /* 2868 * Not opening O_NDELAY. 2869 */ 2870 un->un_state = ST_STATE_OPENING; 2871 2872 /* 2873 * Clear error entry stack 2874 */ 2875 st_empty_error_stack(un); 2876 2877 rval = st_tape_init(un); 2878 if ((rval == EACCES) && (un->un_read_only & WORM)) { 2879 un->un_state = ST_STATE_OPEN_PENDING_IO; 2880 rval = 0; /* so open doesn't fail */ 2881 } else if (rval) { 2882 /* 2883 * Release the tape unit, if reserved and not 2884 * preserve reserve. 2885 */ 2886 if ((un->un_rsvd_status & 2887 (ST_RESERVE | ST_PRESERVE_RESERVE)) == ST_RESERVE) { 2888 (void) st_reserve_release(un, ST_RELEASE, 2889 st_uscsi_cmd); 2890 } 2891 } else { 2892 un->un_state = ST_STATE_OPEN_PENDING_IO; 2893 } 2894 } 2895 2896 exit: 2897 /* 2898 * we don't want any uninvited guests scrogging our data when we're 2899 * busy with something, so for successful opens or failed opens 2900 * (except for EBUSY), reset these counters and state appropriately. 2901 */ 2902 if (rval != EBUSY) { 2903 if (rval) { 2904 un->un_state = ST_STATE_CLOSED; 2905 } 2906 un->un_err_resid = 0; 2907 un->un_retry_ct = 0; 2908 } 2909 busy: 2910 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 2911 "st_open: return val = %x, state = %d\n", rval, un->un_state); 2912 mutex_exit(ST_MUTEX); 2913 return (rval); 2914 2915 } 2916 2917 static int 2918 st_tape_init(struct scsi_tape *un) 2919 { 2920 int err; 2921 int rval = 0; 2922 2923 ST_FUNC(ST_DEVINFO, st_tape_init); 2924 2925 ASSERT(mutex_owned(ST_MUTEX)); 2926 2927 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 2928 "st_tape_init(un = 0x%p, oflags = %d)\n", (void*)un, un->un_oflags); 2929 2930 /* 2931 * Clean up after any errors left by 'last' close. 2932 * This also handles the case of the initial open. 2933 */ 2934 if (un->un_state != ST_STATE_INITIALIZING) { 2935 un->un_laststate = un->un_state; 2936 un->un_state = ST_STATE_OPENING; 2937 } 2938 2939 un->un_kbytes_xferred = 0; 2940 2941 /* 2942 * do a throw away TUR to clear check condition 2943 */ 2944 err = st_cmd(un, SCMD_TEST_UNIT_READY, 0, SYNC_CMD); 2945 2946 /* 2947 * If test unit ready fails because the drive is reserved 2948 * by another host fail the open for no access. 2949 */ 2950 if (err) { 2951 if (un->un_rsvd_status & ST_RESERVATION_CONFLICT) { 2952 un->un_state = ST_STATE_CLOSED; 2953 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 2954 "st_tape_init: RESERVATION CONFLICT\n"); 2955 rval = EACCES; 2956 goto exit; 2957 } else if ((un->un_rsvd_status & 2958 ST_APPLICATION_RESERVATIONS) != 0) { 2959 if ((ST_RQSENSE != NULL) && 2960 (ST_RQSENSE->es_add_code == 0x2a && 2961 ST_RQSENSE->es_qual_code == 0x03)) { 2962 un->un_state = ST_STATE_CLOSED; 2963 rval = EACCES; 2964 goto exit; 2965 } 2966 } 2967 } 2968 2969 /* 2970 * Tape self identification could fail if the tape drive is used by 2971 * another host during attach time. We try to get the tape type 2972 * again. This is also applied to any posponed configuration methods. 2973 */ 2974 if (un->un_dp->type == ST_TYPE_INVALID) { 2975 un->un_comp_page = ST_DEV_DATACOMP_PAGE | ST_DEV_CONFIG_PAGE; 2976 st_known_tape_type(un); 2977 } 2978 2979 /* 2980 * If the tape type is still invalid, try to determine the generic 2981 * configuration. 2982 */ 2983 if (un->un_dp->type == ST_TYPE_INVALID) { 2984 rval = st_determine_generic(un); 2985 if (rval) { 2986 if (rval != EACCES) { 2987 rval = EIO; 2988 } 2989 un->un_state = ST_STATE_CLOSED; 2990 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2991 "st_tape_init: %s invalid type\n", 2992 rval == EACCES ? "EACCES" : "EIO"); 2993 goto exit; 2994 } 2995 /* 2996 * If this is a Unknown Type drive, 2997 * Use the READ BLOCK LIMITS to determine if 2998 * allow large xfer is approprate if not globally 2999 * disabled with st_allow_large_xfer. 3000 */ 3001 un->un_allow_large_xfer = (uchar_t)st_allow_large_xfer; 3002 } else { 3003 3004 /* 3005 * If we allow_large_xfer (ie >64k) and have not yet found out 3006 * the max block size supported by the drive, 3007 * find it by issueing a READ_BLKLIM command. 3008 * if READ_BLKLIM cmd fails, assume drive doesn't 3009 * allow_large_xfer and min/max block sizes as 1 byte and 63k. 3010 */ 3011 un->un_allow_large_xfer = st_allow_large_xfer && 3012 (un->un_dp->options & ST_NO_RECSIZE_LIMIT); 3013 } 3014 /* 3015 * if maxbsize is unknown, set the maximum block size. 3016 */ 3017 if (un->un_maxbsize == MAXBSIZE_UNKNOWN) { 3018 3019 /* 3020 * Get the Block limits of the tape drive. 3021 * if un->un_allow_large_xfer = 0 , then make sure 3022 * that maxbsize is <= ST_MAXRECSIZE_FIXED. 3023 */ 3024 un->un_rbl = kmem_zalloc(RBLSIZE, KM_SLEEP); 3025 3026 err = st_cmd(un, SCMD_READ_BLKLIM, RBLSIZE, SYNC_CMD); 3027 if (err) { 3028 /* Retry */ 3029 err = st_cmd(un, SCMD_READ_BLKLIM, RBLSIZE, SYNC_CMD); 3030 } 3031 if (!err) { 3032 3033 /* 3034 * if cmd successful, use limit returned 3035 */ 3036 un->un_maxbsize = (un->un_rbl->max_hi << 16) + 3037 (un->un_rbl->max_mid << 8) + 3038 un->un_rbl->max_lo; 3039 un->un_minbsize = (un->un_rbl->min_hi << 8) + 3040 un->un_rbl->min_lo; 3041 un->un_data_mod = 1 << un->un_rbl->granularity; 3042 if ((un->un_maxbsize == 0) || 3043 (un->un_allow_large_xfer == 0 && 3044 un->un_maxbsize > ST_MAXRECSIZE_FIXED)) { 3045 un->un_maxbsize = ST_MAXRECSIZE_FIXED; 3046 3047 } else if (un->un_dp->type == ST_TYPE_DEFAULT) { 3048 /* 3049 * Drive is not one that is configured, But the 3050 * READ BLOCK LIMITS tells us it can do large 3051 * xfers. 3052 */ 3053 if (un->un_maxbsize > ST_MAXRECSIZE_FIXED) { 3054 un->un_dp->options |= 3055 ST_NO_RECSIZE_LIMIT; 3056 } 3057 /* 3058 * If max and mimimum block limits are the 3059 * same this is a fixed block size device. 3060 */ 3061 if (un->un_maxbsize == un->un_minbsize) { 3062 un->un_dp->options &= ~ST_VARIABLE; 3063 } 3064 } 3065 3066 if (un->un_minbsize == 0) { 3067 un->un_minbsize = 1; 3068 } 3069 3070 } else { /* error on read block limits */ 3071 3072 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 3073 "!st_tape_init: Error on READ BLOCK LIMITS," 3074 " errno = %d un_rsvd_status = 0x%X\n", 3075 err, un->un_rsvd_status); 3076 3077 /* 3078 * since read block limits cmd failed, 3079 * do not allow large xfers. 3080 * use old values in st_minphys 3081 */ 3082 if (un->un_rsvd_status & ST_RESERVATION_CONFLICT) { 3083 rval = EACCES; 3084 } else { 3085 un->un_allow_large_xfer = 0; 3086 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 3087 "!Disabling large transfers\n"); 3088 3089 /* 3090 * we guess maxbsize and minbsize 3091 */ 3092 if (un->un_bsize) { 3093 un->un_maxbsize = un->un_minbsize = 3094 un->un_bsize; 3095 } else { 3096 un->un_maxbsize = ST_MAXRECSIZE_FIXED; 3097 un->un_minbsize = 1; 3098 } 3099 /* 3100 * Data Mod must be set, 3101 * Even if read block limits fails. 3102 * Prevents Divide By Zero in st_rw(). 3103 */ 3104 un->un_data_mod = 1; 3105 } 3106 } 3107 if (un->un_rbl) { 3108 kmem_free(un->un_rbl, RBLSIZE); 3109 un->un_rbl = NULL; 3110 } 3111 3112 if (rval) { 3113 goto exit; 3114 } 3115 } 3116 3117 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 3118 "maxdma = %d, maxbsize = %d, minbsize = %d, %s large xfer\n", 3119 un->un_maxdma, un->un_maxbsize, un->un_minbsize, 3120 (un->un_allow_large_xfer ? "ALLOW": "DON'T ALLOW")); 3121 3122 err = st_cmd(un, SCMD_TEST_UNIT_READY, 0, SYNC_CMD); 3123 3124 if (err != 0) { 3125 if (err == EINTR) { 3126 un->un_laststate = un->un_state; 3127 un->un_state = ST_STATE_CLOSED; 3128 rval = EINTR; 3129 goto exit; 3130 } 3131 /* 3132 * Make sure the tape is ready 3133 */ 3134 un->un_pos.pmode = invalid; 3135 if (un->un_status != KEY_UNIT_ATTENTION) { 3136 /* 3137 * allow open no media. Subsequent MTIOCSTATE 3138 * with media present will complete the open 3139 * logic. 3140 */ 3141 un->un_laststate = un->un_state; 3142 if (un->un_oflags & (FNONBLOCK|FNDELAY)) { 3143 un->un_mediastate = MTIO_EJECTED; 3144 un->un_state = ST_STATE_OFFLINE; 3145 rval = 0; 3146 goto exit; 3147 } else { 3148 un->un_state = ST_STATE_CLOSED; 3149 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 3150 "st_tape_init EIO no media, not opened " 3151 "O_NONBLOCK|O_EXCL\n"); 3152 rval = EIO; 3153 goto exit; 3154 } 3155 } 3156 } 3157 3158 /* 3159 * On each open, initialize block size from drivetype struct, 3160 * as it could have been changed by MTSRSZ ioctl. 3161 * Now, ST_VARIABLE simply means drive is capable of variable 3162 * mode. All drives are assumed to support fixed records. 3163 * Hence, un_bsize tells what mode the drive is in. 3164 * un_bsize = 0 - variable record length 3165 * = x - fixed record length is x 3166 */ 3167 un->un_bsize = un->un_dp->bsize; 3168 3169 /* 3170 * If saved position is valid go there 3171 */ 3172 if (un->un_restore_pos) { 3173 un->un_restore_pos = 0; 3174 un->un_pos.fileno = un->un_save_fileno; 3175 un->un_pos.blkno = un->un_save_blkno; 3176 rval = st_validate_tapemarks(un, st_uscsi_cmd, &un->un_pos); 3177 if (rval != 0) { 3178 if (rval != EACCES) { 3179 rval = EIO; 3180 } 3181 un->un_laststate = un->un_state; 3182 un->un_state = ST_STATE_CLOSED; 3183 goto exit; 3184 } 3185 } 3186 3187 if (un->un_pos.pmode == invalid) { 3188 rval = st_loadtape(un); 3189 if (rval) { 3190 if (rval != EACCES) { 3191 rval = EIO; 3192 } 3193 un->un_laststate = un->un_state; 3194 un->un_state = ST_STATE_CLOSED; 3195 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 3196 "st_tape_init: %s can't open tape\n", 3197 rval == EACCES ? "EACCES" : "EIO"); 3198 goto exit; 3199 } 3200 } 3201 3202 /* 3203 * do a mode sense to pick up state of current write-protect, 3204 * Could cause reserve and fail due to conflict. 3205 */ 3206 if (un->un_unit_attention_flags) { 3207 rval = st_modesense(un); 3208 if (rval == EACCES) { 3209 goto exit; 3210 } 3211 } 3212 3213 /* 3214 * If we are opening the tape for writing, check 3215 * to make sure that the tape can be written. 3216 */ 3217 if (un->un_oflags & FWRITE) { 3218 err = 0; 3219 if (un->un_mspl->wp) { 3220 un->un_status = KEY_WRITE_PROTECT; 3221 un->un_laststate = un->un_state; 3222 un->un_state = ST_STATE_CLOSED; 3223 rval = EACCES; 3224 /* 3225 * STK sets the wp bit if volsafe tape is loaded. 3226 */ 3227 if ((un->un_dp->type == MT_ISSTK9840) && 3228 (un->un_dp->options & ST_WORMABLE)) { 3229 un->un_read_only = RDONLY; 3230 } else { 3231 goto exit; 3232 } 3233 } else { 3234 un->un_read_only = RDWR; 3235 } 3236 } else { 3237 un->un_read_only = RDONLY; 3238 } 3239 3240 if (un->un_dp->options & ST_WORMABLE && 3241 un->un_unit_attention_flags) { 3242 un->un_read_only |= un->un_wormable(un); 3243 3244 if (((un->un_read_only == WORM) || 3245 (un->un_read_only == RDWORM)) && 3246 ((un->un_oflags & FWRITE) == FWRITE)) { 3247 un->un_status = KEY_DATA_PROTECT; 3248 rval = EACCES; 3249 ST_DEBUG4(ST_DEVINFO, st_label, CE_NOTE, 3250 "read_only = %d eof = %d oflag = %d\n", 3251 un->un_read_only, un->un_pos.eof, un->un_oflags); 3252 } 3253 } 3254 3255 /* 3256 * If we're opening the tape write-only, we need to 3257 * write 2 filemarks on the HP 1/2 inch drive, to 3258 * create a null file. 3259 */ 3260 if ((un->un_read_only == RDWR) || 3261 (un->un_read_only == WORM) && (un->un_oflags & FWRITE)) { 3262 if (un->un_dp->options & ST_REEL) { 3263 un->un_fmneeded = 2; 3264 } else { 3265 un->un_fmneeded = 1; 3266 } 3267 } else { 3268 un->un_fmneeded = 0; 3269 } 3270 3271 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 3272 "fmneeded = %x\n", un->un_fmneeded); 3273 3274 /* 3275 * Make sure the density can be selected correctly. 3276 * If WORM can only write at the append point which in most cases 3277 * isn't BOP. st_determine_density() with a B_WRITE only attempts 3278 * to set and try densities if a BOP. 3279 */ 3280 if (st_determine_density(un, 3281 un->un_read_only == RDWR ? B_WRITE : B_READ)) { 3282 un->un_status = KEY_ILLEGAL_REQUEST; 3283 un->un_laststate = un->un_state; 3284 un->un_state = ST_STATE_CLOSED; 3285 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 3286 "st_tape_init: EIO can't determine density\n"); 3287 rval = EIO; 3288 goto exit; 3289 } 3290 3291 /* 3292 * Destroy the knowledge that we have 'determined' 3293 * density so that a later read at BOT comes along 3294 * does the right density determination. 3295 */ 3296 3297 un->un_density_known = 0; 3298 3299 3300 /* 3301 * Okay, the tape is loaded and either at BOT or somewhere past. 3302 * Mark the state such that any I/O or tape space operations 3303 * will get/set the right density, etc.. 3304 */ 3305 un->un_laststate = un->un_state; 3306 un->un_lastop = ST_OP_NIL; 3307 un->un_mediastate = MTIO_INSERTED; 3308 cv_broadcast(&un->un_state_cv); 3309 3310 /* 3311 * Set test append flag if writing. 3312 * First write must check that tape is positioned correctly. 3313 */ 3314 un->un_test_append = (un->un_oflags & FWRITE); 3315 3316 /* 3317 * if there are pending unit attention flags. 3318 * Check that the media has not changed. 3319 */ 3320 if (un->un_unit_attention_flags) { 3321 rval = st_get_media_identification(un, st_uscsi_cmd); 3322 if (rval != 0 && rval != EACCES) { 3323 rval = EIO; 3324 } 3325 un->un_unit_attention_flags = 0; 3326 } 3327 3328 exit: 3329 un->un_err_resid = 0; 3330 un->un_last_resid = 0; 3331 un->un_last_count = 0; 3332 3333 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 3334 "st_tape_init: return val = %x\n", rval); 3335 return (rval); 3336 3337 } 3338 3339 3340 3341 /* ARGSUSED */ 3342 static int 3343 st_close(dev_t dev, int flag, int otyp, cred_t *cred_p) 3344 { 3345 int err = 0; 3346 int count, last_state; 3347 minor_t minor = getminor(dev); 3348 #ifdef __x86 3349 struct contig_mem *cp, *cp_temp; 3350 #endif 3351 3352 GET_SOFT_STATE(dev); 3353 3354 ST_ENTR(ST_DEVINFO, st_close); 3355 3356 /* 3357 * wait till all cmds in the pipeline have been completed 3358 */ 3359 mutex_enter(ST_MUTEX); 3360 3361 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 3362 "st_close(dev = 0x%lx, flag = %d, otyp = %d)\n", dev, flag, otyp); 3363 3364 st_wait_for_io(un); 3365 3366 /* turn off persistent errors on close, as we want close to succeed */ 3367 st_turn_pe_off(un); 3368 3369 /* 3370 * set state to indicate that we are in process of closing 3371 */ 3372 last_state = un->un_laststate = un->un_state; 3373 un->un_state = ST_STATE_CLOSING; 3374 3375 ST_POS(ST_DEVINFO, "st_close1:", &un->un_pos); 3376 3377 /* 3378 * BSD behavior: 3379 * a close always causes a silent span to the next file if we've hit 3380 * an EOF (but not yet read across it). 3381 */ 3382 if ((minor & MT_BSD) && (un->un_pos.eof == ST_EOF)) { 3383 if (un->un_pos.pmode != invalid) { 3384 un->un_pos.fileno++; 3385 un->un_pos.blkno = 0; 3386 } 3387 un->un_pos.eof = ST_NO_EOF; 3388 } 3389 3390 /* 3391 * SVR4 behavior for skipping to next file: 3392 * 3393 * If we have not seen a filemark, space to the next file 3394 * 3395 * If we have already seen the filemark we are physically in the next 3396 * file and we only increment the filenumber 3397 */ 3398 if (((minor & (MT_BSD | MT_NOREWIND)) == MT_NOREWIND) && 3399 (flag & FREAD) && /* reading or at least asked to */ 3400 (un->un_mediastate == MTIO_INSERTED) && /* tape loaded */ 3401 (un->un_pos.pmode != invalid) && /* XXX position known */ 3402 ((un->un_pos.blkno != 0) && /* inside a file */ 3403 (un->un_lastop != ST_OP_WRITE) && /* Didn't just write */ 3404 (un->un_lastop != ST_OP_WEOF))) { /* or write filemarks */ 3405 switch (un->un_pos.eof) { 3406 case ST_NO_EOF: 3407 /* 3408 * if we were reading and did not read the complete file 3409 * skip to the next file, leaving the tape correctly 3410 * positioned to read the first record of the next file 3411 * Check first for REEL if we are at EOT by trying to 3412 * read a block 3413 */ 3414 if ((un->un_dp->options & ST_REEL) && 3415 (!(un->un_dp->options & ST_READ_IGNORE_EOFS)) && 3416 (un->un_pos.blkno == 0)) { 3417 if (st_cmd(un, SCMD_SPACE, Blk(1), SYNC_CMD)) { 3418 ST_DEBUG2(ST_DEVINFO, st_label, 3419 SCSI_DEBUG, 3420 "st_close : EIO can't space\n"); 3421 err = EIO; 3422 goto error_out; 3423 } 3424 if (un->un_pos.eof >= ST_EOF_PENDING) { 3425 un->un_pos.eof = ST_EOT_PENDING; 3426 un->un_pos.fileno += 1; 3427 un->un_pos.blkno = 0; 3428 break; 3429 } 3430 } 3431 if (st_cmd(un, SCMD_SPACE, Fmk(1), SYNC_CMD)) { 3432 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 3433 "st_close: EIO can't space #2\n"); 3434 err = EIO; 3435 goto error_out; 3436 } else { 3437 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 3438 "st_close2: fileno=%x,blkno=%x,eof=%x\n", 3439 un->un_pos.fileno, un->un_pos.blkno, 3440 un->un_pos.eof); 3441 un->un_pos.eof = ST_NO_EOF; 3442 } 3443 break; 3444 3445 case ST_EOF_PENDING: 3446 case ST_EOF: 3447 un->un_pos.fileno += 1; 3448 un->un_pos.lgclblkno += 1; 3449 un->un_pos.blkno = 0; 3450 un->un_pos.eof = ST_NO_EOF; 3451 break; 3452 3453 case ST_EOT: 3454 case ST_EOT_PENDING: 3455 case ST_EOM: 3456 /* nothing to do */ 3457 break; 3458 default: 3459 ST_DEBUG(ST_DEVINFO, st_label, CE_PANIC, 3460 "Undefined state 0x%x", un->un_pos.eof); 3461 3462 } 3463 } 3464 3465 3466 /* 3467 * For performance reasons (HP 88780), the driver should 3468 * postpone writing the second tape mark until just before a file 3469 * positioning ioctl is issued (e.g., rewind). This means that 3470 * the user must not manually rewind the tape because the tape will 3471 * be missing the second tape mark which marks EOM. 3472 * However, this small performance improvement is not worth the risk. 3473 */ 3474 3475 /* 3476 * We need to back up over the filemark we inadvertently popped 3477 * over doing a read in between the two filemarks that constitute 3478 * logical eot for 1/2" tapes. Note that ST_EOT_PENDING is only 3479 * set while reading. 3480 * 3481 * If we happen to be at physical eot (ST_EOM) (writing case), 3482 * the writing of filemark(s) will clear the ST_EOM state, which 3483 * we don't want, so we save this state and restore it later. 3484 */ 3485 3486 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 3487 "flag=%x, fmneeded=%x, lastop=%x, eof=%x\n", 3488 flag, un->un_fmneeded, un->un_lastop, un->un_pos.eof); 3489 3490 if (un->un_pos.eof == ST_EOT_PENDING) { 3491 if (minor & MT_NOREWIND) { 3492 if (st_cmd(un, SCMD_SPACE, Fmk(-1), SYNC_CMD)) { 3493 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 3494 "st_close: EIO can't space #3\n"); 3495 err = EIO; 3496 goto error_out; 3497 } else { 3498 un->un_pos.blkno = 0; 3499 un->un_pos.eof = ST_EOT; 3500 } 3501 } else { 3502 un->un_pos.eof = ST_NO_EOF; 3503 } 3504 3505 /* 3506 * Do we need to write a file mark? 3507 * 3508 * only write filemarks if there are fmks to be written and 3509 * - open for write (possibly read/write) 3510 * - the last operation was a write 3511 * or: 3512 * - opened for wronly 3513 * - no data was written 3514 */ 3515 } else if ((un->un_pos.pmode != invalid) && 3516 (un->un_fmneeded > 0) && 3517 (((flag & FWRITE) && 3518 ((un->un_lastop == ST_OP_WRITE)||(un->un_lastop == ST_OP_WEOF))) || 3519 ((flag == FWRITE) && (un->un_lastop == ST_OP_NIL)))) { 3520 3521 /* save ST_EOM state */ 3522 int was_at_eom = (un->un_pos.eof == ST_EOM) ? 1 : 0; 3523 3524 /* 3525 * Note that we will write a filemark if we had opened 3526 * the tape write only and no data was written, thus 3527 * creating a null file. 3528 * 3529 * If the user already wrote one, we only have to write 1 more. 3530 * If they wrote two, we don't have to write any. 3531 */ 3532 3533 count = un->un_fmneeded; 3534 if (count > 0) { 3535 if (st_cmd(un, SCMD_WRITE_FILE_MARK, count, SYNC_CMD)) { 3536 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 3537 "st_close : EIO can't wfm\n"); 3538 err = EIO; 3539 goto error_out; 3540 } 3541 if ((un->un_dp->options & ST_REEL) && 3542 (minor & MT_NOREWIND)) { 3543 if (st_cmd(un, SCMD_SPACE, Fmk(-1), SYNC_CMD)) { 3544 ST_DEBUG2(ST_DEVINFO, st_label, 3545 SCSI_DEBUG, 3546 "st_close : EIO space fmk(-1)\n"); 3547 err = EIO; 3548 goto error_out; 3549 } 3550 un->un_pos.eof = ST_NO_EOF; 3551 /* fix up block number */ 3552 un->un_pos.blkno = 0; 3553 } 3554 } 3555 3556 /* 3557 * If we aren't going to be rewinding, and we were at 3558 * physical eot, restore the state that indicates we 3559 * are at physical eot. Once you have reached physical 3560 * eot, and you close the tape, the only thing you can 3561 * do on the next open is to rewind. Access to trailer 3562 * records is only allowed without closing the device. 3563 */ 3564 if ((minor & MT_NOREWIND) == 0 && was_at_eom) { 3565 un->un_pos.eof = ST_EOM; 3566 } 3567 } 3568 3569 /* 3570 * report soft errors if enabled and available, if we never accessed 3571 * the drive, don't get errors. This will prevent some DAT error 3572 * messages upon LOG SENSE. 3573 */ 3574 if (st_report_soft_errors_on_close && 3575 (un->un_dp->options & ST_SOFT_ERROR_REPORTING) && 3576 (last_state != ST_STATE_OFFLINE)) { 3577 if (st_report_soft_errors(dev, flag)) { 3578 err = EIO; 3579 goto error_out; 3580 } 3581 } 3582 3583 3584 /* 3585 * Do we need to rewind? Can we rewind? 3586 */ 3587 if ((minor & MT_NOREWIND) == 0 && 3588 un->un_pos.pmode != invalid && err == 0) { 3589 /* 3590 * We'd like to rewind with the 3591 * 'immediate' bit set, but this 3592 * causes problems on some drives 3593 * where subsequent opens get a 3594 * 'NOT READY' error condition 3595 * back while the tape is rewinding, 3596 * which is impossible to distinguish 3597 * from the condition of 'no tape loaded'. 3598 * 3599 * Also, for some targets, if you disconnect 3600 * with the 'immediate' bit set, you don't 3601 * actually return right away, i.e., the 3602 * target ignores your request for immediate 3603 * return. 3604 * 3605 * Instead, we'll fire off an async rewind 3606 * command. We'll mark the device as closed, 3607 * and any subsequent open will stall on 3608 * the first TEST_UNIT_READY until the rewind 3609 * completes. 3610 */ 3611 3612 /* 3613 * Used to be if reserve was not supported we'd send an 3614 * asynchronious rewind. Comments above may be slightly invalid 3615 * as the immediate bit was never set. Doing an immedate rewind 3616 * makes sense, I think fixes to not ready status might handle 3617 * the problems described above. 3618 */ 3619 if (un->un_sd->sd_inq->inq_ansi < 2) { 3620 if (st_cmd(un, SCMD_REWIND, 0, SYNC_CMD)) { 3621 err = EIO; 3622 } 3623 } else { 3624 /* flush data for older drives per scsi spec. */ 3625 if (st_cmd(un, SCMD_WRITE_FILE_MARK, 0, SYNC_CMD)) { 3626 err = EIO; 3627 } else { 3628 /* release the drive before rewind immediate */ 3629 if ((un->un_rsvd_status & 3630 (ST_RESERVE | ST_PRESERVE_RESERVE)) == 3631 ST_RESERVE) { 3632 if (st_reserve_release(un, ST_RELEASE, 3633 st_uscsi_cmd)) { 3634 err = EIO; 3635 } 3636 } 3637 3638 /* send rewind with immediate bit set */ 3639 if (st_cmd(un, SCMD_REWIND, 1, ASYNC_CMD)) { 3640 err = EIO; 3641 } 3642 } 3643 } 3644 /* 3645 * Setting positions invalid in case the rewind doesn't 3646 * happen. Drives don't like to rewind if resets happen 3647 * they will tend to move back to where the rewind was 3648 * issued if a reset or something happens so that if a 3649 * write happens the data doesn't get clobbered. 3650 * 3651 * Not a big deal if the position is invalid when the 3652 * open occures it will do a read position. 3653 */ 3654 un->un_pos.pmode = invalid; 3655 un->un_running.pmode = invalid; 3656 3657 if (err == EIO) { 3658 goto error_out; 3659 } 3660 } 3661 3662 /* 3663 * eject tape if necessary 3664 */ 3665 if (un->un_eject_tape_on_failure) { 3666 un->un_eject_tape_on_failure = 0; 3667 if (st_cmd(un, SCMD_LOAD, LD_UNLOAD, SYNC_CMD)) { 3668 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 3669 "st_close : can't unload tape\n"); 3670 err = EIO; 3671 goto error_out; 3672 } else { 3673 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 3674 "st_close : tape unloaded \n"); 3675 un->un_pos.eof = ST_NO_EOF; 3676 un->un_mediastate = MTIO_EJECTED; 3677 } 3678 } 3679 /* 3680 * Release the tape unit, if default reserve/release 3681 * behaviour. 3682 */ 3683 if ((un->un_rsvd_status & 3684 (ST_RESERVE | ST_PRESERVE_RESERVE | 3685 ST_APPLICATION_RESERVATIONS)) == ST_RESERVE) { 3686 (void) st_reserve_release(un, ST_RELEASE, st_uscsi_cmd); 3687 } 3688 error_out: 3689 /* 3690 * clear up state 3691 */ 3692 un->un_laststate = un->un_state; 3693 un->un_state = ST_STATE_CLOSED; 3694 un->un_lastop = ST_OP_NIL; 3695 un->un_throttle = 1; /* assume one request at time, for now */ 3696 un->un_retry_ct = 0; 3697 un->un_errno = 0; 3698 un->un_swr_token = (opaque_t)NULL; 3699 un->un_rsvd_status &= ~(ST_INIT_RESERVE); 3700 3701 /* Restore the options to the init time settings */ 3702 if (un->un_init_options & ST_READ_IGNORE_ILI) { 3703 un->un_dp->options |= ST_READ_IGNORE_ILI; 3704 } else { 3705 un->un_dp->options &= ~ST_READ_IGNORE_ILI; 3706 } 3707 3708 if (un->un_init_options & ST_READ_IGNORE_EOFS) { 3709 un->un_dp->options |= ST_READ_IGNORE_EOFS; 3710 } else { 3711 un->un_dp->options &= ~ST_READ_IGNORE_EOFS; 3712 } 3713 3714 if (un->un_init_options & ST_SHORT_FILEMARKS) { 3715 un->un_dp->options |= ST_SHORT_FILEMARKS; 3716 } else { 3717 un->un_dp->options &= ~ST_SHORT_FILEMARKS; 3718 } 3719 3720 ASSERT(mutex_owned(ST_MUTEX)); 3721 3722 /* 3723 * Signal anyone awaiting a close operation to complete. 3724 */ 3725 cv_signal(&un->un_clscv); 3726 3727 /* 3728 * any kind of error on closing causes all state to be tossed 3729 */ 3730 if (err && un->un_status != KEY_ILLEGAL_REQUEST) { 3731 /* 3732 * note that st_intr has already set 3733 * un_pos.pmode to invalid. 3734 */ 3735 un->un_density_known = 0; 3736 } 3737 3738 #ifdef __x86 3739 /* 3740 * free any contiguous mem alloc'ed for big block I/O 3741 */ 3742 cp = un->un_contig_mem; 3743 while (cp) { 3744 if (cp->cm_addr) { 3745 ddi_dma_mem_free(&cp->cm_acc_hdl); 3746 } 3747 cp_temp = cp; 3748 cp = cp->cm_next; 3749 kmem_free(cp_temp, 3750 sizeof (struct contig_mem) + biosize()); 3751 } 3752 un->un_contig_mem_total_num = 0; 3753 un->un_contig_mem_available_num = 0; 3754 un->un_contig_mem = NULL; 3755 un->un_max_contig_mem_len = 0; 3756 #endif 3757 3758 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 3759 "st_close3: return val = %x, fileno=%x, blkno=%x, eof=%x\n", 3760 err, un->un_pos.fileno, un->un_pos.blkno, un->un_pos.eof); 3761 3762 mutex_exit(ST_MUTEX); 3763 return (err); 3764 } 3765 3766 /* 3767 * These routines perform raw i/o operations. 3768 */ 3769 3770 /* ARGSUSED2 */ 3771 static int 3772 st_aread(dev_t dev, struct aio_req *aio, cred_t *cred_p) 3773 { 3774 #ifdef STDEBUG 3775 GET_SOFT_STATE(dev); 3776 ST_ENTR(ST_DEVINFO, st_aread); 3777 #endif 3778 return (st_arw(dev, aio, B_READ)); 3779 } 3780 3781 3782 /* ARGSUSED2 */ 3783 static int 3784 st_awrite(dev_t dev, struct aio_req *aio, cred_t *cred_p) 3785 { 3786 #ifdef STDEBUG 3787 GET_SOFT_STATE(dev); 3788 ST_ENTR(ST_DEVINFO, st_awrite); 3789 #endif 3790 return (st_arw(dev, aio, B_WRITE)); 3791 } 3792 3793 3794 3795 /* ARGSUSED */ 3796 static int 3797 st_read(dev_t dev, struct uio *uiop, cred_t *cred_p) 3798 { 3799 #ifdef STDEBUG 3800 GET_SOFT_STATE(dev); 3801 ST_ENTR(ST_DEVINFO, st_read); 3802 #endif 3803 return (st_rw(dev, uiop, B_READ)); 3804 } 3805 3806 /* ARGSUSED */ 3807 static int 3808 st_write(dev_t dev, struct uio *uiop, cred_t *cred_p) 3809 { 3810 #ifdef STDEBUG 3811 GET_SOFT_STATE(dev); 3812 ST_ENTR(ST_DEVINFO, st_write); 3813 #endif 3814 return (st_rw(dev, uiop, B_WRITE)); 3815 } 3816 3817 /* 3818 * Due to historical reasons, old limits are: For variable-length devices: 3819 * if greater than 64KB - 1 (ST_MAXRECSIZE_VARIABLE), block into 64 KB - 2 3820 * ST_MAXRECSIZE_VARIABLE_LIMIT) requests; otherwise, 3821 * (let it through unmodified. For fixed-length record devices: 3822 * 63K (ST_MAXRECSIZE_FIXED) is max (default minphys). 3823 * 3824 * The new limits used are un_maxdma (retrieved using scsi_ifgetcap() 3825 * from the HBA) and un_maxbsize (retrieved by sending SCMD_READ_BLKLIM 3826 * command to the drive). 3827 * 3828 */ 3829 static void 3830 st_minphys(struct buf *bp) 3831 { 3832 struct scsi_tape *un; 3833 3834 un = ddi_get_soft_state(st_state, MTUNIT(bp->b_edev)); 3835 3836 ST_FUNC(ST_DEVINFO, st_minphys); 3837 3838 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 3839 "st_minphys(bp = 0x%p): b_bcount = 0x%lx\n", (void *)bp, 3840 bp->b_bcount); 3841 3842 if (un->un_allow_large_xfer) { 3843 3844 /* 3845 * check un_maxbsize for variable length devices only 3846 */ 3847 if (un->un_bsize == 0 && bp->b_bcount > un->un_maxbsize) { 3848 bp->b_bcount = un->un_maxbsize; 3849 } 3850 /* 3851 * can't go more that HBA maxdma limit in either fixed-length 3852 * or variable-length tape drives. 3853 */ 3854 if (bp->b_bcount > un->un_maxdma) { 3855 bp->b_bcount = un->un_maxdma; 3856 } 3857 } else { 3858 3859 /* 3860 * use old fixed limits 3861 */ 3862 if (un->un_bsize == 0) { 3863 if (bp->b_bcount > ST_MAXRECSIZE_VARIABLE) { 3864 bp->b_bcount = ST_MAXRECSIZE_VARIABLE_LIMIT; 3865 } 3866 } else { 3867 if (bp->b_bcount > ST_MAXRECSIZE_FIXED) { 3868 bp->b_bcount = ST_MAXRECSIZE_FIXED; 3869 } 3870 } 3871 } 3872 3873 /* 3874 * For regular raw I/O and Fixed Block length devices, make sure 3875 * the adjusted block count is a whole multiple of the device 3876 * block size. 3877 */ 3878 if (bp != un->un_sbufp && un->un_bsize) { 3879 bp->b_bcount -= (bp->b_bcount % un->un_bsize); 3880 } 3881 } 3882 3883 static int 3884 st_rw(dev_t dev, struct uio *uio, int flag) 3885 { 3886 int rval = 0; 3887 long len; 3888 3889 GET_SOFT_STATE(dev); 3890 3891 ST_FUNC(ST_DEVINFO, st_rw); 3892 3893 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 3894 "st_rw(dev = 0x%lx, flag = %s)\n", dev, 3895 (flag == B_READ ? rd_str: wr_str)); 3896 3897 /* get local copy of transfer length */ 3898 len = uio->uio_iov->iov_len; 3899 3900 mutex_enter(ST_MUTEX); 3901 3902 /* 3903 * Clear error entry stack 3904 */ 3905 st_empty_error_stack(un); 3906 3907 /* 3908 * If in fixed block size mode and requested read or write 3909 * is not an even multiple of that block size. 3910 */ 3911 if ((un->un_bsize != 0) && (len % un->un_bsize != 0)) { 3912 scsi_log(ST_DEVINFO, st_label, CE_WARN, 3913 "%s: not modulo %d block size\n", 3914 (flag == B_WRITE) ? wr_str : rd_str, un->un_bsize); 3915 rval = EINVAL; 3916 } 3917 3918 /* If device has set granularity in the READ_BLKLIM we honor it. */ 3919 if ((un->un_data_mod != 0) && (len % un->un_data_mod != 0)) { 3920 scsi_log(ST_DEVINFO, st_label, CE_WARN, 3921 "%s: not modulo %d device granularity\n", 3922 (flag == B_WRITE) ? wr_str : rd_str, un->un_data_mod); 3923 rval = EINVAL; 3924 } 3925 3926 if (st_recov_sz != sizeof (recov_info) && un->un_multipath) { 3927 scsi_log(ST_DEVINFO, st_label, CE_WARN, mp_misconf); 3928 rval = EFAULT; 3929 } 3930 3931 if (rval != 0) { 3932 un->un_errno = rval; 3933 mutex_exit(ST_MUTEX); 3934 return (rval); 3935 } 3936 3937 /* 3938 * Reset this so it can be set if Berkeley and read over a filemark. 3939 */ 3940 un->un_silent_skip = 0; 3941 mutex_exit(ST_MUTEX); 3942 3943 len = uio->uio_resid; 3944 3945 rval = physio(st_queued_strategy, (struct buf *)NULL, 3946 dev, flag, st_minphys, uio); 3947 /* 3948 * if we have hit logical EOT during this xfer and there is not a 3949 * full residue, then set eof back to ST_EOM to make sure that 3950 * the user will see at least one zero write 3951 * after this short write 3952 */ 3953 mutex_enter(ST_MUTEX); 3954 if (un->un_pos.eof > ST_NO_EOF) { 3955 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 3956 "eof=%d resid=%lx\n", un->un_pos.eof, uio->uio_resid); 3957 } 3958 if (un->un_pos.eof >= ST_EOM && (flag == B_WRITE)) { 3959 if ((uio->uio_resid != len) && (uio->uio_resid != 0)) { 3960 un->un_pos.eof = ST_EOM; 3961 } else if (uio->uio_resid == len) { 3962 un->un_pos.eof = ST_NO_EOF; 3963 } 3964 } 3965 3966 if (un->un_silent_skip && uio->uio_resid != len) { 3967 un->un_pos.eof = ST_EOF; 3968 un->un_pos.blkno = un->un_save_blkno; 3969 un->un_pos.fileno--; 3970 } 3971 3972 un->un_errno = rval; 3973 3974 mutex_exit(ST_MUTEX); 3975 3976 return (rval); 3977 } 3978 3979 static int 3980 st_arw(dev_t dev, struct aio_req *aio, int flag) 3981 { 3982 struct uio *uio = aio->aio_uio; 3983 int rval = 0; 3984 long len; 3985 3986 GET_SOFT_STATE(dev); 3987 3988 ST_FUNC(ST_DEVINFO, st_arw); 3989 3990 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 3991 "st_arw(dev = 0x%lx, flag = %s)\n", dev, 3992 (flag == B_READ ? rd_str: wr_str)); 3993 3994 /* get local copy of transfer length */ 3995 len = uio->uio_iov->iov_len; 3996 3997 mutex_enter(ST_MUTEX); 3998 3999 /* 4000 * If in fixed block size mode and requested read or write 4001 * is not an even multiple of that block size. 4002 */ 4003 if ((un->un_bsize != 0) && (len % un->un_bsize != 0)) { 4004 scsi_log(ST_DEVINFO, st_label, CE_WARN, 4005 "%s: not modulo %d block size\n", 4006 (flag == B_WRITE) ? wr_str : rd_str, un->un_bsize); 4007 rval = EINVAL; 4008 } 4009 4010 /* If device has set granularity in the READ_BLKLIM we honor it. */ 4011 if ((un->un_data_mod != 0) && (len % un->un_data_mod != 0)) { 4012 scsi_log(ST_DEVINFO, st_label, CE_WARN, 4013 "%s: not modulo %d device granularity\n", 4014 (flag == B_WRITE) ? wr_str : rd_str, un->un_data_mod); 4015 rval = EINVAL; 4016 } 4017 4018 if (st_recov_sz != sizeof (recov_info) && un->un_multipath) { 4019 scsi_log(ST_DEVINFO, st_label, CE_WARN, mp_misconf); 4020 rval = EFAULT; 4021 } 4022 4023 if (rval != 0) { 4024 un->un_errno = rval; 4025 mutex_exit(ST_MUTEX); 4026 return (rval); 4027 } 4028 4029 mutex_exit(ST_MUTEX); 4030 4031 len = uio->uio_resid; 4032 4033 rval = 4034 aphysio(st_queued_strategy, anocancel, dev, flag, st_minphys, aio); 4035 4036 /* 4037 * if we have hit logical EOT during this xfer and there is not a 4038 * full residue, then set eof back to ST_EOM to make sure that 4039 * the user will see at least one zero write 4040 * after this short write 4041 * 4042 * we keep this here just in case the application is not using 4043 * persistent errors 4044 */ 4045 mutex_enter(ST_MUTEX); 4046 if (un->un_pos.eof > ST_NO_EOF) { 4047 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4048 "eof=%d resid=%lx\n", un->un_pos.eof, uio->uio_resid); 4049 } 4050 if (un->un_pos.eof >= ST_EOM && (flag == B_WRITE)) { 4051 if ((uio->uio_resid != len) && (uio->uio_resid != 0)) { 4052 un->un_pos.eof = ST_EOM; 4053 } else if (uio->uio_resid == len && 4054 !(un->un_persistence && un->un_persist_errors)) { 4055 un->un_pos.eof = ST_NO_EOF; 4056 } 4057 } 4058 un->un_errno = rval; 4059 mutex_exit(ST_MUTEX); 4060 4061 return (rval); 4062 } 4063 4064 4065 4066 static int 4067 st_queued_strategy(buf_t *bp) 4068 { 4069 struct scsi_tape *un; 4070 char reading = bp->b_flags & B_READ; 4071 int wasopening = 0; 4072 4073 /* 4074 * validate arguments 4075 */ 4076 un = ddi_get_soft_state(st_state, MTUNIT(bp->b_edev)); 4077 if (un == NULL) { 4078 bp->b_resid = bp->b_bcount; 4079 bioerror(bp, ENXIO); 4080 ST_DEBUG6(NULL, st_label, SCSI_DEBUG, 4081 "st_queued_strategy: ENXIO error exit\n"); 4082 biodone(bp); 4083 return (0); 4084 } 4085 4086 ST_ENTR(ST_DEVINFO, st_queued_strategy); 4087 4088 mutex_enter(ST_MUTEX); 4089 4090 while (un->un_pwr_mgmt == ST_PWR_SUSPENDED) { 4091 cv_wait(&un->un_suspend_cv, ST_MUTEX); 4092 } 4093 4094 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 4095 "st_queued_strategy(): bcount=0x%lx, fileno=%d, blkno=%x, eof=%d\n", 4096 bp->b_bcount, un->un_pos.fileno, un->un_pos.blkno, un->un_pos.eof); 4097 4098 /* 4099 * If persistent errors have been flagged, just nix this one. We wait 4100 * for any outstanding I/O's below, so we will be in order. 4101 */ 4102 if (un->un_persistence && un->un_persist_errors) { 4103 goto exit; 4104 } 4105 4106 /* 4107 * If last command was non queued, wait till it finishes. 4108 */ 4109 while (un->un_sbuf_busy) { 4110 cv_wait(&un->un_sbuf_cv, ST_MUTEX); 4111 /* woke up because of an error */ 4112 if (un->un_persistence && un->un_persist_errors) { 4113 goto exit; 4114 } 4115 } 4116 4117 /* 4118 * s_buf and recovery commands shouldn't come here. 4119 */ 4120 ASSERT(bp != un->un_recov_buf); 4121 ASSERT(bp != un->un_sbufp); 4122 4123 /* 4124 * If we haven't done/checked reservation on the tape unit 4125 * do it now. 4126 */ 4127 if ((un->un_rsvd_status & 4128 (ST_RESERVE | ST_APPLICATION_RESERVATIONS)) == 0) { 4129 if ((un->un_dp->options & ST_NO_RESERVE_RELEASE) == 0) { 4130 if (st_reserve_release(un, ST_RESERVE, st_uscsi_cmd)) { 4131 st_bioerror(bp, un->un_errno); 4132 goto exit; 4133 } 4134 } else if (un->un_state == ST_STATE_OPEN_PENDING_IO) { 4135 /* 4136 * Enter here to restore position for possible 4137 * resets when the device was closed and opened 4138 * in O_NDELAY mode subsequently 4139 */ 4140 un->un_state = ST_STATE_INITIALIZING; 4141 (void) st_cmd(un, SCMD_TEST_UNIT_READY, 4142 0, SYNC_CMD); 4143 un->un_state = ST_STATE_OPEN_PENDING_IO; 4144 } 4145 un->un_rsvd_status |= ST_INIT_RESERVE; 4146 } 4147 4148 /* 4149 * If we are offline, we have to initialize everything first. 4150 * This is to handle either when opened with O_NDELAY, or 4151 * we just got a new tape in the drive, after an offline. 4152 * We don't observe O_NDELAY past the open, 4153 * as it will not make sense for tapes. 4154 */ 4155 if (un->un_state == ST_STATE_OFFLINE || un->un_restore_pos) { 4156 /* 4157 * reset state to avoid recursion 4158 */ 4159 un->un_laststate = un->un_state; 4160 un->un_state = ST_STATE_INITIALIZING; 4161 if (st_tape_init(un)) { 4162 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4163 "stioctl : OFFLINE init failure "); 4164 un->un_state = ST_STATE_OFFLINE; 4165 un->un_pos.pmode = invalid; 4166 goto b_done_err; 4167 } 4168 /* un_restore_pos make invalid */ 4169 un->un_state = ST_STATE_OPEN_PENDING_IO; 4170 un->un_restore_pos = 0; 4171 } 4172 /* 4173 * Check for legal operations 4174 */ 4175 if (un->un_pos.pmode == invalid) { 4176 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4177 "strategy with un->un_pos.pmode invalid\n"); 4178 goto b_done_err; 4179 } 4180 4181 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4182 "st_queued_strategy(): regular io\n"); 4183 4184 /* 4185 * Process this first. If we were reading, and we're pending 4186 * logical eot, that means we've bumped one file mark too far. 4187 */ 4188 4189 /* 4190 * Recursion warning: st_cmd will route back through here. 4191 * Not anymore st_cmd will go through st_strategy()! 4192 */ 4193 if (un->un_pos.eof == ST_EOT_PENDING) { 4194 if (st_cmd(un, SCMD_SPACE, Fmk(-1), SYNC_CMD)) { 4195 un->un_pos.pmode = invalid; 4196 un->un_density_known = 0; 4197 goto b_done_err; 4198 } 4199 un->un_pos.blkno = 0; /* fix up block number.. */ 4200 un->un_pos.eof = ST_EOT; 4201 } 4202 4203 /* 4204 * If we are in the process of opening, we may have to 4205 * determine/set the correct density. We also may have 4206 * to do a test_append (if QIC) to see whether we are 4207 * in a position to append to the end of the tape. 4208 * 4209 * If we're already at logical eot, we transition 4210 * to ST_NO_EOF. If we're at physical eot, we punt 4211 * to the switch statement below to handle. 4212 */ 4213 if ((un->un_state == ST_STATE_OPEN_PENDING_IO) || 4214 (un->un_test_append && (un->un_dp->options & ST_QIC))) { 4215 4216 if (un->un_state == ST_STATE_OPEN_PENDING_IO) { 4217 if (st_determine_density(un, (int)reading)) { 4218 goto b_done_err; 4219 } 4220 } 4221 4222 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4223 "pending_io@fileno %d rw %d qic %d eof %d\n", 4224 un->un_pos.fileno, (int)reading, 4225 (un->un_dp->options & ST_QIC) ? 1 : 0, 4226 un->un_pos.eof); 4227 4228 if (!reading && un->un_pos.eof != ST_EOM) { 4229 if (un->un_pos.eof == ST_EOT) { 4230 un->un_pos.eof = ST_NO_EOF; 4231 } else if (un->un_pos.pmode != invalid && 4232 (un->un_dp->options & ST_QIC)) { 4233 /* 4234 * st_test_append() will do it all 4235 */ 4236 st_test_append(bp); 4237 mutex_exit(ST_MUTEX); 4238 return (0); 4239 } 4240 } 4241 if (un->un_state == ST_STATE_OPEN_PENDING_IO) { 4242 wasopening = 1; 4243 } 4244 un->un_laststate = un->un_state; 4245 un->un_state = ST_STATE_OPEN; 4246 } 4247 4248 4249 /* 4250 * Process rest of END OF FILE and END OF TAPE conditions 4251 */ 4252 4253 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4254 "eof=%x, wasopening=%x\n", 4255 un->un_pos.eof, wasopening); 4256 4257 switch (un->un_pos.eof) { 4258 case ST_EOM: 4259 /* 4260 * This allows writes to proceed past physical 4261 * eot. We'll *really* be in trouble if the 4262 * user continues blindly writing data too 4263 * much past this point (unwind the tape). 4264 * Physical eot really means 'early warning 4265 * eot' in this context. 4266 * 4267 * Every other write from now on will succeed 4268 * (if sufficient tape left). 4269 * This write will return with resid == count 4270 * but the next one should be successful 4271 * 4272 * Note that we only transition to logical EOT 4273 * if the last state wasn't the OPENING state. 4274 * We explicitly prohibit running up to physical 4275 * eot, closing the device, and then re-opening 4276 * to proceed. Trailer records may only be gotten 4277 * at by keeping the tape open after hitting eot. 4278 * 4279 * Also note that ST_EOM cannot be set by reading- 4280 * this can only be set during writing. Reading 4281 * up to the end of the tape gets a blank check 4282 * or a double-filemark indication (ST_EOT_PENDING), 4283 * and we prohibit reading after that point. 4284 * 4285 */ 4286 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "EOM\n"); 4287 if (wasopening == 0) { 4288 /* 4289 * this allows st_rw() to reset it back to 4290 * will see a zero write 4291 */ 4292 un->un_pos.eof = ST_WRITE_AFTER_EOM; 4293 } 4294 un->un_status = SUN_KEY_EOT; 4295 goto b_done; 4296 4297 case ST_WRITE_AFTER_EOM: 4298 case ST_EOT: 4299 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "EOT\n"); 4300 un->un_status = SUN_KEY_EOT; 4301 if (SVR4_BEHAVIOR && reading) { 4302 goto b_done_err; 4303 } 4304 4305 if (reading) { 4306 goto b_done; 4307 } 4308 un->un_pos.eof = ST_NO_EOF; 4309 break; 4310 4311 case ST_EOF_PENDING: 4312 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4313 "EOF PENDING\n"); 4314 un->un_status = SUN_KEY_EOF; 4315 if (SVR4_BEHAVIOR) { 4316 un->un_pos.eof = ST_EOF; 4317 goto b_done; 4318 } 4319 /* FALLTHROUGH */ 4320 case ST_EOF: 4321 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "EOF\n"); 4322 un->un_status = SUN_KEY_EOF; 4323 if (SVR4_BEHAVIOR) { 4324 goto b_done_err; 4325 } 4326 4327 if (BSD_BEHAVIOR) { 4328 un->un_pos.eof = ST_NO_EOF; 4329 un->un_pos.fileno += 1; 4330 un->un_pos.blkno = 0; 4331 } 4332 4333 if (reading) { 4334 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4335 "now file %d (read)\n", 4336 un->un_pos.fileno); 4337 goto b_done; 4338 } 4339 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4340 "now file %d (write)\n", un->un_pos.fileno); 4341 break; 4342 default: 4343 un->un_status = 0; 4344 break; 4345 } 4346 4347 bp->b_flags &= ~(B_DONE); 4348 st_bioerror(bp, 0); 4349 bp->av_forw = NULL; 4350 bp->b_resid = 0; 4351 SET_BP_PKT(bp, 0); 4352 4353 4354 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4355 "st_queued_strategy: cmd=0x%p count=%ld resid=%ld flags=0x%x" 4356 " pkt=0x%p\n", 4357 (void *)bp->b_forw, bp->b_bcount, 4358 bp->b_resid, bp->b_flags, (void *)BP_PKT(bp)); 4359 4360 #ifdef __x86 4361 /* 4362 * We will replace bp with a new bp that can do big blk xfer 4363 * if the requested xfer size is bigger than un->un_maxdma_arch 4364 * 4365 * Also, we need to make sure that we're handling real I/O 4366 * by checking group 0/1 SCSI I/O commands, if needed 4367 */ 4368 if (bp->b_bcount > un->un_maxdma_arch && 4369 ((uchar_t)(uintptr_t)bp->b_forw == SCMD_READ || 4370 (uchar_t)(uintptr_t)bp->b_forw == SCMD_READ_G4 || 4371 (uchar_t)(uintptr_t)bp->b_forw == SCMD_WRITE || 4372 (uchar_t)(uintptr_t)bp->b_forw == SCMD_WRITE_G4)) { 4373 mutex_exit(ST_MUTEX); 4374 bp = st_get_bigblk_bp(bp); 4375 mutex_enter(ST_MUTEX); 4376 } 4377 #endif 4378 4379 /* put on wait queue */ 4380 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4381 "st_queued_strategy: un->un_quef = 0x%p, bp = 0x%p\n", 4382 (void *)un->un_quef, (void *)bp); 4383 4384 st_add_to_queue(&un->un_quef, &un->un_quel, un->un_quel, bp); 4385 4386 ST_DO_KSTATS(bp, kstat_waitq_enter); 4387 4388 st_start(un); 4389 4390 mutex_exit(ST_MUTEX); 4391 return (0); 4392 4393 b_done_err: 4394 st_bioerror(bp, EIO); 4395 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4396 "st_queued_strategy : EIO b_done_err\n"); 4397 4398 b_done: 4399 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4400 "st_queued_strategy: b_done\n"); 4401 4402 exit: 4403 /* 4404 * make sure no commands are outstanding or waiting before closing, 4405 * so we can guarantee order 4406 */ 4407 st_wait_for_io(un); 4408 un->un_err_resid = bp->b_resid = bp->b_bcount; 4409 4410 /* override errno here, if persistent errors were flagged */ 4411 if (un->un_persistence && un->un_persist_errors) 4412 bioerror(bp, un->un_errno); 4413 4414 mutex_exit(ST_MUTEX); 4415 4416 biodone(bp); 4417 ASSERT(mutex_owned(ST_MUTEX) == 0); 4418 return (0); 4419 } 4420 4421 4422 static int 4423 st_strategy(struct buf *bp) 4424 { 4425 struct scsi_tape *un; 4426 4427 /* 4428 * validate arguments 4429 */ 4430 un = ddi_get_soft_state(st_state, MTUNIT(bp->b_edev)); 4431 if (un == NULL) { 4432 bp->b_resid = bp->b_bcount; 4433 bioerror(bp, ENXIO); 4434 ST_DEBUG6(NULL, st_label, SCSI_DEBUG, 4435 "st_strategy: ENXIO error exit\n"); 4436 4437 biodone(bp); 4438 return (0); 4439 4440 } 4441 4442 ST_ENTR(ST_DEVINFO, st_strategy); 4443 4444 mutex_enter(ST_MUTEX); 4445 4446 while (un->un_pwr_mgmt == ST_PWR_SUSPENDED) { 4447 cv_wait(&un->un_suspend_cv, ST_MUTEX); 4448 } 4449 4450 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 4451 "st_strategy(): bcount=0x%lx, fileno=%d, blkno=%x, eof=%d\n", 4452 bp->b_bcount, un->un_pos.fileno, un->un_pos.blkno, un->un_pos.eof); 4453 4454 ASSERT((bp == un->un_recov_buf) || (bp == un->un_sbufp)); 4455 4456 bp->b_flags &= ~(B_DONE); 4457 st_bioerror(bp, 0); 4458 bp->av_forw = NULL; 4459 bp->b_resid = 0; 4460 SET_BP_PKT(bp, 0); 4461 4462 4463 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4464 "st_strategy: cmd=0x%x count=%ld resid=%ld flags=0x%x" 4465 " pkt=0x%p\n", 4466 (unsigned char)(uintptr_t)bp->b_forw, bp->b_bcount, 4467 bp->b_resid, bp->b_flags, (void *)BP_PKT(bp)); 4468 ST_DO_KSTATS(bp, kstat_waitq_enter); 4469 4470 st_start(un); 4471 4472 mutex_exit(ST_MUTEX); 4473 return (0); 4474 } 4475 4476 /* 4477 * this routine spaces forward over filemarks 4478 */ 4479 static int 4480 st_space_fmks(struct scsi_tape *un, int64_t count) 4481 { 4482 int rval = 0; 4483 4484 ST_FUNC(ST_DEVINFO, st_space_fmks); 4485 4486 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 4487 "st_space_fmks(dev = 0x%lx, count = %"PRIx64")\n", 4488 un->un_dev, count); 4489 4490 ASSERT(mutex_owned(ST_MUTEX)); 4491 4492 /* 4493 * the risk with doing only one space operation is that we 4494 * may accidentily jump in old data 4495 * the exabyte 8500 reading 8200 tapes cannot use KNOWS_EOD 4496 * because the 8200 does not append a marker; in order not to 4497 * sacrifice the fast file skip, we do a slow skip if the low 4498 * density device has been opened 4499 */ 4500 4501 if ((un->un_dp->options & ST_KNOWS_EOD) && 4502 !((un->un_dp->type == ST_TYPE_EXB8500 && 4503 MT_DENSITY(un->un_dev) == 0))) { 4504 if (st_cmd(un, SCMD_SPACE, Fmk(count), SYNC_CMD)) { 4505 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4506 "space_fmks : EIO can't do space cmd #1\n"); 4507 rval = EIO; 4508 } 4509 } else { 4510 while (count > 0) { 4511 if (st_cmd(un, SCMD_SPACE, Fmk(1), SYNC_CMD)) { 4512 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4513 "space_fmks : EIO can't do space cmd #2\n"); 4514 rval = EIO; 4515 break; 4516 } 4517 count -= 1; 4518 /* 4519 * read a block to see if we have reached 4520 * end of medium (double filemark for reel or 4521 * medium error for others) 4522 */ 4523 if (count > 0) { 4524 if (st_cmd(un, SCMD_SPACE, Blk(1), SYNC_CMD)) { 4525 ST_DEBUG2(ST_DEVINFO, st_label, 4526 SCSI_DEBUG, 4527 "space_fmks : EIO can't do " 4528 "space cmd #3\n"); 4529 rval = EIO; 4530 break; 4531 } 4532 if ((un->un_pos.eof >= ST_EOF_PENDING) && 4533 (un->un_dp->options & ST_REEL)) { 4534 un->un_status = SUN_KEY_EOT; 4535 ST_DEBUG2(ST_DEVINFO, st_label, 4536 SCSI_DEBUG, 4537 "space_fmks : EIO ST_REEL\n"); 4538 rval = EIO; 4539 break; 4540 } else if (IN_EOF(un->un_pos)) { 4541 un->un_pos.eof = ST_NO_EOF; 4542 un->un_pos.fileno++; 4543 un->un_pos.blkno = 0; 4544 count--; 4545 } else if (un->un_pos.eof > ST_EOF) { 4546 ST_DEBUG2(ST_DEVINFO, st_label, 4547 SCSI_DEBUG, 4548 "space_fmks, EIO > ST_EOF\n"); 4549 rval = EIO; 4550 break; 4551 } 4552 4553 } 4554 } 4555 un->un_err_resid = count; 4556 COPY_POS(&un->un_pos, &un->un_err_pos); 4557 } 4558 ASSERT(mutex_owned(ST_MUTEX)); 4559 return (rval); 4560 } 4561 4562 /* 4563 * this routine spaces to EOD 4564 * 4565 * it keeps track of the current filenumber and returns the filenumber after 4566 * the last successful space operation, we keep the number high because as 4567 * tapes are getting larger, the possibility of more and more files exist, 4568 * 0x100000 (1 Meg of files) probably will never have to be changed any time 4569 * soon 4570 */ 4571 #define MAX_SKIP 0x100000 /* somewhat arbitrary */ 4572 4573 static int 4574 st_find_eod(struct scsi_tape *un) 4575 { 4576 tapepos_t savepos; 4577 int64_t sp_type; 4578 int result; 4579 4580 if (un == NULL) { 4581 return (-1); 4582 } 4583 4584 ST_FUNC(ST_DEVINFO, st_find_eod); 4585 4586 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 4587 "st_find_eod(dev = 0x%lx): fileno = %d\n", un->un_dev, 4588 un->un_pos.fileno); 4589 4590 ASSERT(mutex_owned(ST_MUTEX)); 4591 4592 COPY_POS(&savepos, &un->un_pos); 4593 4594 /* 4595 * see if the drive is smart enough to do the skips in 4596 * one operation; 1/2" use two filemarks 4597 * the exabyte 8500 reading 8200 tapes cannot use KNOWS_EOD 4598 * because the 8200 does not append a marker; in order not to 4599 * sacrifice the fast file skip, we do a slow skip if the low 4600 * density device has been opened 4601 */ 4602 if ((un->un_dp->options & ST_KNOWS_EOD) != 0) { 4603 if ((un->un_dp->type == ST_TYPE_EXB8500) && 4604 (MT_DENSITY(un->un_dev) == 0)) { 4605 sp_type = Fmk(1); 4606 } else if (un->un_pos.pmode == logical) { 4607 sp_type = SPACE(SP_EOD, 0); 4608 } else { 4609 sp_type = Fmk(MAX_SKIP); 4610 } 4611 } else { 4612 sp_type = Fmk(1); 4613 } 4614 4615 for (;;) { 4616 result = st_cmd(un, SCMD_SPACE, sp_type, SYNC_CMD); 4617 4618 if (result == 0) { 4619 COPY_POS(&savepos, &un->un_pos); 4620 } 4621 4622 if (sp_type == SPACE(SP_EOD, 0)) { 4623 if (result != 0) { 4624 sp_type = Fmk(MAX_SKIP); 4625 continue; 4626 } 4627 4628 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4629 "st_find_eod: 0x%"PRIx64"\n", 4630 savepos.lgclblkno); 4631 /* 4632 * What we return will become the current file position. 4633 * After completing the space command with the position 4634 * mode that is not invalid a read position command will 4635 * be automaticly issued. If the drive support the long 4636 * read position format a valid file position can be 4637 * returned. 4638 */ 4639 return (un->un_pos.fileno); 4640 } 4641 4642 if (result != 0) { 4643 break; 4644 } 4645 4646 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4647 "count=%"PRIx64", eof=%x, status=%x\n", 4648 SPACE_CNT(sp_type), un->un_pos.eof, un->un_status); 4649 4650 /* 4651 * If we're not EOM smart, space a record 4652 * to see whether we're now in the slot between 4653 * the two sequential filemarks that logical 4654 * EOM consists of (REEL) or hit nowhere land 4655 * (8mm). 4656 */ 4657 if (sp_type == Fmk(1)) { 4658 /* 4659 * no fast skipping, check a record 4660 */ 4661 if (st_cmd(un, SCMD_SPACE, Blk((1)), SYNC_CMD)) { 4662 break; 4663 } 4664 if ((un->un_pos.eof >= ST_EOF_PENDING) && 4665 (un->un_dp->options & ST_REEL)) { 4666 un->un_status = KEY_BLANK_CHECK; 4667 un->un_pos.fileno++; 4668 un->un_pos.blkno = 0; 4669 break; 4670 } 4671 if (IN_EOF(un->un_pos)) { 4672 un->un_pos.eof = ST_NO_EOF; 4673 un->un_pos.fileno++; 4674 un->un_pos.blkno = 0; 4675 } 4676 if (un->un_pos.eof > ST_EOF) { 4677 break; 4678 } 4679 } else { 4680 if (un->un_pos.eof > ST_EOF) { 4681 break; 4682 } 4683 } 4684 } 4685 4686 if (un->un_dp->options & ST_KNOWS_EOD) { 4687 COPY_POS(&savepos, &un->un_pos); 4688 } 4689 4690 ASSERT(mutex_owned(ST_MUTEX)); 4691 4692 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4693 "st_find_eod: %x\n", savepos.fileno); 4694 return (savepos.fileno); 4695 } 4696 4697 4698 /* 4699 * this routine is frequently used in ioctls below; 4700 * it determines whether we know the density and if not will 4701 * determine it 4702 * if we have written the tape before, one or more filemarks are written 4703 * 4704 * depending on the stepflag, the head is repositioned to where it was before 4705 * the filemarks were written in order not to confuse step counts 4706 */ 4707 #define STEPBACK 0 4708 #define NO_STEPBACK 1 4709 4710 static int 4711 st_check_density_or_wfm(dev_t dev, int wfm, int mode, int stepflag) 4712 { 4713 4714 GET_SOFT_STATE(dev); 4715 4716 ST_FUNC(ST_DEVINFO, st_check_density_or_wfm); 4717 4718 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 4719 "st_check_density_or_wfm(dev= 0x%lx, wfm= %d, mode= %d, stpflg= %d)" 4720 "\n", dev, wfm, mode, stepflag); 4721 4722 ASSERT(mutex_owned(ST_MUTEX)); 4723 4724 /* 4725 * If we don't yet know the density of the tape we have inserted, 4726 * we have to either unconditionally set it (if we're 'writing'), 4727 * or we have to determine it. As side effects, check for any 4728 * write-protect errors, and for the need to put out any file-marks 4729 * before positioning a tape. 4730 * 4731 * If we are going to be spacing forward, and we haven't determined 4732 * the tape density yet, we have to do so now... 4733 */ 4734 if (un->un_state == ST_STATE_OPEN_PENDING_IO) { 4735 if (st_determine_density(un, mode)) { 4736 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4737 "check_density_or_wfm : EIO can't determine " 4738 "density\n"); 4739 un->un_errno = EIO; 4740 return (EIO); 4741 } 4742 /* 4743 * Presumably we are at BOT. If we attempt to write, it will 4744 * either work okay, or bomb. We don't do a st_test_append 4745 * unless we're past BOT. 4746 */ 4747 un->un_laststate = un->un_state; 4748 un->un_state = ST_STATE_OPEN; 4749 4750 } else if (un->un_pos.pmode != invalid && un->un_fmneeded > 0 && 4751 ((un->un_lastop == ST_OP_WEOF && wfm) || 4752 (un->un_lastop == ST_OP_WRITE && wfm))) { 4753 4754 tapepos_t spos; 4755 4756 COPY_POS(&spos, &un->un_pos); 4757 4758 /* 4759 * We need to write one or two filemarks. 4760 * In the case of the HP, we need to 4761 * position the head between the two 4762 * marks. 4763 */ 4764 if ((un->un_fmneeded > 0) || (un->un_lastop == ST_OP_WEOF)) { 4765 wfm = un->un_fmneeded; 4766 un->un_fmneeded = 0; 4767 } 4768 4769 if (st_write_fm(dev, wfm)) { 4770 un->un_pos.pmode = invalid; 4771 un->un_density_known = 0; 4772 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4773 "check_density_or_wfm : EIO can't write fm\n"); 4774 un->un_errno = EIO; 4775 return (EIO); 4776 } 4777 4778 if (stepflag == STEPBACK) { 4779 if (st_cmd(un, SCMD_SPACE, Fmk(-wfm), SYNC_CMD)) { 4780 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4781 "check_density_or_wfm : EIO can't space " 4782 "(-wfm)\n"); 4783 un->un_errno = EIO; 4784 return (EIO); 4785 } 4786 COPY_POS(&un->un_pos, &spos); 4787 } 4788 } 4789 4790 /* 4791 * Whatever we do at this point clears the state of the eof flag. 4792 */ 4793 4794 un->un_pos.eof = ST_NO_EOF; 4795 4796 /* 4797 * If writing, let's check that we're positioned correctly 4798 * at the end of tape before issuing the next write. 4799 */ 4800 if (un->un_read_only == RDWR) { 4801 un->un_test_append = 1; 4802 } 4803 4804 ASSERT(mutex_owned(ST_MUTEX)); 4805 return (0); 4806 } 4807 4808 4809 /* 4810 * Wait for all outstaning I/O's to complete 4811 * 4812 * we wait on both ncmds and the wait queue for times when we are flushing 4813 * after persistent errors are flagged, which is when ncmds can be 0, and the 4814 * queue can still have I/O's. This way we preserve order of biodone's. 4815 */ 4816 static void 4817 st_wait_for_io(struct scsi_tape *un) 4818 { 4819 ST_FUNC(ST_DEVINFO, st_wait_for_io); 4820 ASSERT(mutex_owned(ST_MUTEX)); 4821 while ((un->un_ncmds) || (un->un_quef) || (un->un_runqf)) { 4822 cv_wait(&un->un_queue_cv, ST_MUTEX); 4823 } 4824 } 4825 4826 /* 4827 * This routine implements the ioctl calls. It is called 4828 * from the device switch at normal priority. 4829 */ 4830 /*ARGSUSED*/ 4831 static int 4832 st_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p, 4833 int *rval_p) 4834 { 4835 int tmp, rval = 0; 4836 4837 GET_SOFT_STATE(dev); 4838 4839 ST_ENTR(ST_DEVINFO, st_ioctl); 4840 4841 mutex_enter(ST_MUTEX); 4842 4843 ASSERT(un->un_recov_buf_busy == 0); 4844 4845 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 4846 "st_ioctl(): fileno=%x, blkno=%x, eof=%x, state = %d, " 4847 "pe_flag = %d\n", 4848 un->un_pos.fileno, un->un_pos.blkno, un->un_pos.eof, un->un_state, 4849 un->un_persistence && un->un_persist_errors); 4850 4851 /* 4852 * We don't want to block on these, so let them through 4853 * and we don't care about setting driver states here. 4854 */ 4855 if ((cmd == MTIOCGETDRIVETYPE) || 4856 (cmd == MTIOCGUARANTEEDORDER) || 4857 (cmd == MTIOCPERSISTENTSTATUS)) { 4858 goto check_commands; 4859 } 4860 4861 /* 4862 * We clear error entry stack except command 4863 * MTIOCGETERROR and MTIOCGET 4864 */ 4865 if ((cmd != MTIOCGETERROR) && 4866 (cmd != MTIOCGET)) { 4867 st_empty_error_stack(un); 4868 } 4869 4870 /* 4871 * wait for all outstanding commands to complete, or be dequeued. 4872 * And because ioctl's are synchronous commands, any return value 4873 * after this, will be in order 4874 */ 4875 st_wait_for_io(un); 4876 4877 /* 4878 * allow only a through clear errors and persistent status, and 4879 * status 4880 */ 4881 if (un->un_persistence && un->un_persist_errors) { 4882 if ((cmd == MTIOCLRERR) || 4883 (cmd == MTIOCPERSISTENT) || 4884 (cmd == MTIOCGET)) { 4885 goto check_commands; 4886 } else { 4887 rval = un->un_errno; 4888 goto exit; 4889 } 4890 } 4891 4892 ASSERT(un->un_throttle != 0); 4893 un->un_throttle = 1; /* > 1 will never happen here */ 4894 un->un_errno = 0; /* start clean from here */ 4895 4896 /* 4897 * first and foremost, handle any ST_EOT_PENDING cases. 4898 * That is, if a logical eot is pending notice, notice it. 4899 */ 4900 if (un->un_pos.eof == ST_EOT_PENDING) { 4901 int resid = un->un_err_resid; 4902 uchar_t status = un->un_status; 4903 uchar_t lastop = un->un_lastop; 4904 4905 if (st_cmd(un, SCMD_SPACE, Fmk(-1), SYNC_CMD)) { 4906 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4907 "stioctl : EIO can't space fmk(-1)\n"); 4908 rval = EIO; 4909 goto exit; 4910 } 4911 un->un_lastop = lastop; /* restore last operation */ 4912 if (status == SUN_KEY_EOF) { 4913 un->un_status = SUN_KEY_EOT; 4914 } else { 4915 un->un_status = status; 4916 } 4917 un->un_err_resid = resid; 4918 /* fix up block number */ 4919 un->un_err_pos.blkno = un->un_pos.blkno = 0; 4920 /* now we're at logical eot */ 4921 un->un_pos.eof = ST_EOT; 4922 } 4923 4924 /* 4925 * now, handle the rest of the situations 4926 */ 4927 check_commands: 4928 switch (cmd) { 4929 case MTIOCGET: 4930 { 4931 #ifdef _MULTI_DATAMODEL 4932 /* 4933 * For use when a 32 bit app makes a call into a 4934 * 64 bit ioctl 4935 */ 4936 struct mtget32 mtg_local32; 4937 struct mtget32 *mtget_32 = &mtg_local32; 4938 #endif /* _MULTI_DATAMODEL */ 4939 4940 /* Get tape status */ 4941 struct mtget mtg_local; 4942 struct mtget *mtget = &mtg_local; 4943 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 4944 "st_ioctl: MTIOCGET\n"); 4945 4946 bzero((caddr_t)mtget, sizeof (struct mtget)); 4947 mtget->mt_erreg = un->un_status; 4948 mtget->mt_resid = un->un_err_resid; 4949 mtget->mt_dsreg = un->un_retry_ct; 4950 if (un->un_err_pos.pmode == legacy) { 4951 mtget->mt_fileno = un->un_err_pos.fileno; 4952 } else { 4953 mtget->mt_fileno = -1; 4954 } 4955 /* 4956 * If the value is positive fine. 4957 * If its negative we need to return a value based on the 4958 * old way if counting backwards from INF (1,000,000,000). 4959 */ 4960 if (un->un_err_pos.blkno >= 0) { 4961 mtget->mt_blkno = un->un_err_pos.blkno; 4962 } else { 4963 mtget->mt_blkno = INF + 1 - (-un->un_err_pos.blkno); 4964 } 4965 mtget->mt_type = un->un_dp->type; 4966 mtget->mt_flags = MTF_SCSI | MTF_ASF; 4967 if (un->un_read_pos_type != NO_POS) { 4968 mtget->mt_flags |= MTF_LOGICAL_BLOCK; 4969 } 4970 if (un->un_dp->options & ST_REEL) { 4971 mtget->mt_flags |= MTF_REEL; 4972 mtget->mt_bf = 20; 4973 } else { /* 1/4" cartridges */ 4974 switch (mtget->mt_type) { 4975 /* Emulex cartridge tape */ 4976 case MT_ISMT02: 4977 mtget->mt_bf = 40; 4978 break; 4979 default: 4980 mtget->mt_bf = 126; 4981 break; 4982 } 4983 } 4984 4985 /* 4986 * If large transfers are allowed and drive options 4987 * has no record size limit set. Calculate blocking 4988 * factor from the lesser of maxbsize and maxdma. 4989 */ 4990 if ((un->un_allow_large_xfer) && 4991 (un->un_dp->options & ST_NO_RECSIZE_LIMIT)) { 4992 mtget->mt_bf = min(un->un_maxbsize, 4993 un->un_maxdma) / SECSIZE; 4994 } 4995 4996 if (un->un_read_only == WORM || 4997 un->un_read_only == RDWORM) { 4998 mtget->mt_flags |= MTF_WORM_MEDIA; 4999 } 5000 5001 /* 5002 * In persistent error mode sending a non-queued can hang 5003 * because this ioctl gets to be run without turning off 5004 * persistense. Fake the answer based on previous info. 5005 */ 5006 if (un->un_persistence) { 5007 rval = 0; 5008 } else { 5009 rval = st_check_clean_bit(un); 5010 } 5011 if (rval == 0) { 5012 /* 5013 * If zero is returned or in persistent mode, 5014 * use the old data. 5015 */ 5016 if ((un->un_HeadClean & (TAPE_ALERT_SUPPORTED | 5017 TAPE_SEQUENTIAL_SUPPORTED|TAPE_ALERT_NOT_SUPPORTED)) 5018 != TAPE_ALERT_NOT_SUPPORTED) { 5019 mtget->mt_flags |= MTF_TAPE_CLN_SUPPORTED; 5020 } 5021 if (un->un_HeadClean & (TAPE_PREVIOUSLY_DIRTY | 5022 TAPE_ALERT_STILL_DIRTY)) { 5023 mtget->mt_flags |= MTF_TAPE_HEAD_DIRTY; 5024 } 5025 } else { 5026 mtget->mt_flags |= (ushort_t)rval; 5027 rval = 0; 5028 } 5029 5030 un->un_status = 0; /* Reset status */ 5031 un->un_err_resid = 0; 5032 tmp = sizeof (struct mtget); 5033 5034 #ifdef _MULTI_DATAMODEL 5035 5036 switch (ddi_model_convert_from(flag & FMODELS)) { 5037 case DDI_MODEL_ILP32: 5038 /* 5039 * Convert 64 bit back to 32 bit before doing 5040 * copyout. This is what the ILP32 app expects. 5041 */ 5042 mtget_32->mt_erreg = mtget->mt_erreg; 5043 mtget_32->mt_resid = mtget->mt_resid; 5044 mtget_32->mt_dsreg = mtget->mt_dsreg; 5045 mtget_32->mt_fileno = (daddr32_t)mtget->mt_fileno; 5046 mtget_32->mt_blkno = (daddr32_t)mtget->mt_blkno; 5047 mtget_32->mt_type = mtget->mt_type; 5048 mtget_32->mt_flags = mtget->mt_flags; 5049 mtget_32->mt_bf = mtget->mt_bf; 5050 5051 if (ddi_copyout(mtget_32, (void *)arg, 5052 sizeof (struct mtget32), flag)) { 5053 rval = EFAULT; 5054 } 5055 break; 5056 5057 case DDI_MODEL_NONE: 5058 if (ddi_copyout(mtget, (void *)arg, tmp, flag)) { 5059 rval = EFAULT; 5060 } 5061 break; 5062 } 5063 #else /* ! _MULTI_DATAMODE */ 5064 if (ddi_copyout(mtget, (void *)arg, tmp, flag)) { 5065 rval = EFAULT; 5066 } 5067 #endif /* _MULTI_DATAMODE */ 5068 5069 break; 5070 } 5071 case MTIOCGETERROR: 5072 /* 5073 * get error entry from error stack 5074 */ 5075 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 5076 "st_ioctl: MTIOCGETERROR\n"); 5077 5078 rval = st_get_error_entry(un, arg, flag); 5079 5080 break; 5081 5082 case MTIOCSTATE: 5083 { 5084 /* 5085 * return when media presence matches state 5086 */ 5087 enum mtio_state state; 5088 5089 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 5090 "st_ioctl: MTIOCSTATE\n"); 5091 5092 if (ddi_copyin((void *)arg, &state, sizeof (int), flag)) 5093 rval = EFAULT; 5094 5095 mutex_exit(ST_MUTEX); 5096 5097 rval = st_check_media(dev, state); 5098 5099 mutex_enter(ST_MUTEX); 5100 5101 if (rval != 0) { 5102 break; 5103 } 5104 5105 if (ddi_copyout(&un->un_mediastate, (void *)arg, 5106 sizeof (int), flag)) 5107 rval = EFAULT; 5108 break; 5109 5110 } 5111 5112 case MTIOCGETDRIVETYPE: 5113 { 5114 #ifdef _MULTI_DATAMODEL 5115 /* 5116 * For use when a 32 bit app makes a call into a 5117 * 64 bit ioctl 5118 */ 5119 struct mtdrivetype_request32 mtdtrq32; 5120 #endif /* _MULTI_DATAMODEL */ 5121 5122 /* 5123 * return mtdrivetype 5124 */ 5125 struct mtdrivetype_request mtdtrq; 5126 struct mtdrivetype mtdrtyp; 5127 struct mtdrivetype *mtdt = &mtdrtyp; 5128 struct st_drivetype *stdt = un->un_dp; 5129 5130 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 5131 "st_ioctl: MTIOCGETDRIVETYPE\n"); 5132 5133 #ifdef _MULTI_DATAMODEL 5134 switch (ddi_model_convert_from(flag & FMODELS)) { 5135 case DDI_MODEL_ILP32: 5136 { 5137 if (ddi_copyin((void *)arg, &mtdtrq32, 5138 sizeof (struct mtdrivetype_request32), flag)) { 5139 rval = EFAULT; 5140 break; 5141 } 5142 mtdtrq.size = mtdtrq32.size; 5143 mtdtrq.mtdtp = 5144 (struct mtdrivetype *)(uintptr_t)mtdtrq32.mtdtp; 5145 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 5146 "st_ioctl: size 0x%x\n", mtdtrq.size); 5147 break; 5148 } 5149 case DDI_MODEL_NONE: 5150 if (ddi_copyin((void *)arg, &mtdtrq, 5151 sizeof (struct mtdrivetype_request), flag)) { 5152 rval = EFAULT; 5153 break; 5154 } 5155 break; 5156 } 5157 5158 #else /* ! _MULTI_DATAMODEL */ 5159 if (ddi_copyin((void *)arg, &mtdtrq, 5160 sizeof (struct mtdrivetype_request), flag)) { 5161 rval = EFAULT; 5162 break; 5163 } 5164 #endif /* _MULTI_DATAMODEL */ 5165 5166 /* 5167 * if requested size is < 0 then return 5168 * error. 5169 */ 5170 if (mtdtrq.size < 0) { 5171 rval = EINVAL; 5172 break; 5173 } 5174 bzero(mtdt, sizeof (struct mtdrivetype)); 5175 (void) strncpy(mtdt->name, stdt->name, ST_NAMESIZE); 5176 (void) strncpy(mtdt->vid, stdt->vid, VIDPIDLEN - 1); 5177 mtdt->type = stdt->type; 5178 mtdt->bsize = stdt->bsize; 5179 mtdt->options = stdt->options; 5180 mtdt->max_rretries = stdt->max_rretries; 5181 mtdt->max_wretries = stdt->max_wretries; 5182 for (tmp = 0; tmp < NDENSITIES; tmp++) { 5183 mtdt->densities[tmp] = stdt->densities[tmp]; 5184 } 5185 mtdt->default_density = stdt->default_density; 5186 /* 5187 * Speed hasn't been used since the hayday of reel tape. 5188 * For all drives not setting the option ST_KNOWS_MEDIA 5189 * the speed member renamed to mediatype are zeros. 5190 * Those drives that have ST_KNOWS_MEDIA set use the 5191 * new mediatype member which is used to figure the 5192 * type of media loaded. 5193 * 5194 * So as to not break applications speed in the 5195 * mtdrivetype structure is not renamed. 5196 */ 5197 for (tmp = 0; tmp < NDENSITIES; tmp++) { 5198 mtdt->speeds[tmp] = stdt->mediatype[tmp]; 5199 } 5200 mtdt->non_motion_timeout = stdt->non_motion_timeout; 5201 mtdt->io_timeout = stdt->io_timeout; 5202 mtdt->rewind_timeout = stdt->rewind_timeout; 5203 mtdt->space_timeout = stdt->space_timeout; 5204 mtdt->load_timeout = stdt->load_timeout; 5205 mtdt->unload_timeout = stdt->unload_timeout; 5206 mtdt->erase_timeout = stdt->erase_timeout; 5207 5208 /* 5209 * Limit the maximum length of the result to 5210 * sizeof (struct mtdrivetype). 5211 */ 5212 tmp = sizeof (struct mtdrivetype); 5213 if (mtdtrq.size < tmp) 5214 tmp = mtdtrq.size; 5215 if (ddi_copyout(mtdt, mtdtrq.mtdtp, tmp, flag)) { 5216 rval = EFAULT; 5217 } 5218 break; 5219 } 5220 case MTIOCPERSISTENT: 5221 5222 if (ddi_copyin((void *)arg, &tmp, sizeof (tmp), flag)) { 5223 rval = EFAULT; 5224 break; 5225 } 5226 5227 if (tmp) { 5228 st_turn_pe_on(un); 5229 } else { 5230 st_turn_pe_off(un); 5231 } 5232 5233 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 5234 "st_ioctl: MTIOCPERSISTENT : persistence = %d\n", 5235 un->un_persistence); 5236 5237 break; 5238 5239 case MTIOCPERSISTENTSTATUS: 5240 tmp = (int)un->un_persistence; 5241 5242 if (ddi_copyout(&tmp, (void *)arg, sizeof (tmp), flag)) { 5243 rval = EFAULT; 5244 } 5245 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 5246 "st_ioctl: MTIOCPERSISTENTSTATUS:persistence = %d\n", 5247 un->un_persistence); 5248 5249 break; 5250 5251 case MTIOCLRERR: 5252 { 5253 /* clear persistent errors */ 5254 5255 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 5256 "st_ioctl: MTIOCLRERR\n"); 5257 5258 st_clear_pe(un); 5259 5260 break; 5261 } 5262 5263 case MTIOCGUARANTEEDORDER: 5264 { 5265 /* 5266 * this is just a holder to make a valid ioctl and 5267 * it won't be in any earlier release 5268 */ 5269 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 5270 "st_ioctl: MTIOCGUARANTEEDORDER\n"); 5271 5272 break; 5273 } 5274 5275 case MTIOCRESERVE: 5276 { 5277 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 5278 "st_ioctl: MTIOCRESERVE\n"); 5279 5280 /* 5281 * Check if Reserve/Release is supported. 5282 */ 5283 if (un->un_dp->options & ST_NO_RESERVE_RELEASE) { 5284 rval = ENOTTY; 5285 break; 5286 } 5287 5288 rval = st_reserve_release(un, ST_RESERVE, st_uscsi_cmd); 5289 5290 if (rval == 0) { 5291 un->un_rsvd_status |= ST_PRESERVE_RESERVE; 5292 } 5293 break; 5294 } 5295 5296 case MTIOCRELEASE: 5297 { 5298 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 5299 "st_ioctl: MTIOCRELEASE\n"); 5300 5301 /* 5302 * Check if Reserve/Release is supported. 5303 */ 5304 if (un->un_dp->options & ST_NO_RESERVE_RELEASE) { 5305 rval = ENOTTY; 5306 break; 5307 } 5308 5309 /* 5310 * Used to just clear ST_PRESERVE_RESERVE which 5311 * made the reservation release at next close. 5312 * As the user may have opened and then done a 5313 * persistant reservation we now need to drop 5314 * the reservation without closing if the user 5315 * attempts to do this. 5316 */ 5317 rval = st_reserve_release(un, ST_RELEASE, st_uscsi_cmd); 5318 5319 un->un_rsvd_status &= ~ST_PRESERVE_RESERVE; 5320 5321 break; 5322 } 5323 5324 case MTIOCFORCERESERVE: 5325 { 5326 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 5327 "st_ioctl: MTIOCFORCERESERVE\n"); 5328 5329 /* 5330 * Check if Reserve/Release is supported. 5331 */ 5332 if (un->un_dp->options & ST_NO_RESERVE_RELEASE) { 5333 rval = ENOTTY; 5334 break; 5335 } 5336 /* 5337 * allow only super user to run this. 5338 */ 5339 if (drv_priv(cred_p) != 0) { 5340 rval = EPERM; 5341 break; 5342 } 5343 /* 5344 * Throw away reserve, 5345 * not using test-unit-ready 5346 * since reserve can succeed without tape being 5347 * present in the drive. 5348 */ 5349 (void) st_reserve_release(un, ST_RESERVE, st_uscsi_cmd); 5350 5351 rval = st_take_ownership(un, st_uscsi_cmd); 5352 5353 break; 5354 } 5355 5356 case USCSICMD: 5357 { 5358 cred_t *cr; 5359 5360 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 5361 "st_ioctl: USCSICMD\n"); 5362 5363 cr = ddi_get_cred(); 5364 if ((drv_priv(cred_p) != 0) && (drv_priv(cr) != 0)) { 5365 rval = EPERM; 5366 } else { 5367 rval = st_uscsi_cmd(un, (struct uscsi_cmd *)arg, flag); 5368 } 5369 break; 5370 } 5371 case MTIOCTOP: 5372 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 5373 "st_ioctl: MTIOCTOP\n"); 5374 rval = st_mtioctop(un, arg, flag); 5375 break; 5376 5377 case MTIOCLTOP: 5378 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 5379 "st_ioctl: MTIOLCTOP\n"); 5380 rval = st_mtiocltop(un, arg, flag); 5381 break; 5382 5383 case MTIOCREADIGNOREILI: 5384 { 5385 int set_ili; 5386 5387 if (ddi_copyin((void *)arg, &set_ili, 5388 sizeof (set_ili), flag)) { 5389 rval = EFAULT; 5390 break; 5391 } 5392 5393 if (un->un_bsize) { 5394 rval = ENOTTY; 5395 break; 5396 } 5397 5398 switch (set_ili) { 5399 case 0: 5400 un->un_dp->options &= ~ST_READ_IGNORE_ILI; 5401 break; 5402 5403 case 1: 5404 un->un_dp->options |= ST_READ_IGNORE_ILI; 5405 break; 5406 5407 default: 5408 rval = EINVAL; 5409 break; 5410 } 5411 break; 5412 } 5413 5414 case MTIOCREADIGNOREEOFS: 5415 { 5416 int ignore_eof; 5417 5418 if (ddi_copyin((void *)arg, &ignore_eof, 5419 sizeof (ignore_eof), flag)) { 5420 rval = EFAULT; 5421 break; 5422 } 5423 5424 if (!(un->un_dp->options & ST_REEL)) { 5425 rval = ENOTTY; 5426 break; 5427 } 5428 5429 switch (ignore_eof) { 5430 case 0: 5431 un->un_dp->options &= ~ST_READ_IGNORE_EOFS; 5432 break; 5433 5434 case 1: 5435 un->un_dp->options |= ST_READ_IGNORE_EOFS; 5436 break; 5437 5438 default: 5439 rval = EINVAL; 5440 break; 5441 } 5442 break; 5443 } 5444 5445 case MTIOCSHORTFMK: 5446 { 5447 int short_fmk; 5448 5449 if (ddi_copyin((void *)arg, &short_fmk, 5450 sizeof (short_fmk), flag)) { 5451 rval = EFAULT; 5452 break; 5453 } 5454 5455 switch (un->un_dp->type) { 5456 case ST_TYPE_EXB8500: 5457 case ST_TYPE_EXABYTE: 5458 if (!short_fmk) { 5459 un->un_dp->options &= ~ST_SHORT_FILEMARKS; 5460 } else if (short_fmk == 1) { 5461 un->un_dp->options |= ST_SHORT_FILEMARKS; 5462 } else { 5463 rval = EINVAL; 5464 } 5465 break; 5466 5467 default: 5468 rval = ENOTTY; 5469 break; 5470 } 5471 break; 5472 } 5473 5474 case MTIOCGETPOS: 5475 rval = st_update_block_pos(un, st_cmd, 0); 5476 if (rval == 0) { 5477 if (ddi_copyout((void *)&un->un_pos, (void *)arg, 5478 sizeof (tapepos_t), flag)) { 5479 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 5480 "MTIOCGETPOS copy out failed\n"); 5481 rval = EFAULT; 5482 } 5483 } 5484 break; 5485 5486 case MTIOCRESTPOS: 5487 { 5488 tapepos_t dest; 5489 5490 if (ddi_copyin((void *)arg, &dest, sizeof (tapepos_t), 5491 flag) != 0) { 5492 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 5493 "MTIOCRESTPOS copy in failed\n"); 5494 rval = EFAULT; 5495 break; 5496 } 5497 rval = st_validate_tapemarks(un, st_uscsi_cmd, &dest); 5498 if (rval != 0) { 5499 rval = EIO; 5500 } 5501 break; 5502 } 5503 default: 5504 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 5505 "st_ioctl: unknown ioctl\n"); 5506 rval = ENOTTY; 5507 } 5508 5509 exit: 5510 if (!(un->un_persistence && un->un_persist_errors)) { 5511 un->un_errno = rval; 5512 } 5513 5514 mutex_exit(ST_MUTEX); 5515 5516 return (rval); 5517 } 5518 5519 5520 /* 5521 * do some MTIOCTOP tape operations 5522 */ 5523 static int 5524 st_mtioctop(struct scsi_tape *un, intptr_t arg, int flag) 5525 { 5526 #ifdef _MULTI_DATAMODEL 5527 /* 5528 * For use when a 32 bit app makes a call into a 5529 * 64 bit ioctl 5530 */ 5531 struct mtop32 mtop_32_for_64; 5532 #endif /* _MULTI_DATAMODEL */ 5533 struct mtop passed; 5534 struct mtlop local; 5535 int rval = 0; 5536 5537 ST_FUNC(ST_DEVINFO, st_mtioctop); 5538 5539 ASSERT(mutex_owned(ST_MUTEX)); 5540 5541 #ifdef _MULTI_DATAMODEL 5542 switch (ddi_model_convert_from(flag & FMODELS)) { 5543 case DDI_MODEL_ILP32: 5544 if (ddi_copyin((void *)arg, &mtop_32_for_64, 5545 sizeof (struct mtop32), flag)) { 5546 return (EFAULT); 5547 } 5548 local.mt_op = mtop_32_for_64.mt_op; 5549 local.mt_count = (int64_t)mtop_32_for_64.mt_count; 5550 break; 5551 5552 case DDI_MODEL_NONE: 5553 if (ddi_copyin((void *)arg, &passed, sizeof (passed), flag)) { 5554 return (EFAULT); 5555 } 5556 local.mt_op = passed.mt_op; 5557 /* prevent sign extention */ 5558 local.mt_count = (UINT32_MAX & passed.mt_count); 5559 break; 5560 } 5561 5562 #else /* ! _MULTI_DATAMODEL */ 5563 if (ddi_copyin((void *)arg, &passed, sizeof (passed), flag)) { 5564 return (EFAULT); 5565 } 5566 local.mt_op = passed.mt_op; 5567 /* prevent sign extention */ 5568 local.mt_count = (UINT32_MAX & passed.mt_count); 5569 #endif /* _MULTI_DATAMODEL */ 5570 5571 rval = st_do_mtioctop(un, &local); 5572 5573 #ifdef _MULTI_DATAMODEL 5574 switch (ddi_model_convert_from(flag & FMODELS)) { 5575 case DDI_MODEL_ILP32: 5576 if (((uint64_t)local.mt_count) > UINT32_MAX) { 5577 rval = ERANGE; 5578 break; 5579 } 5580 /* 5581 * Convert 64 bit back to 32 bit before doing 5582 * copyout. This is what the ILP32 app expects. 5583 */ 5584 mtop_32_for_64.mt_op = local.mt_op; 5585 mtop_32_for_64.mt_count = local.mt_count; 5586 5587 if (ddi_copyout(&mtop_32_for_64, (void *)arg, 5588 sizeof (struct mtop32), flag)) { 5589 rval = EFAULT; 5590 } 5591 break; 5592 5593 case DDI_MODEL_NONE: 5594 passed.mt_count = local.mt_count; 5595 passed.mt_op = local.mt_op; 5596 if (ddi_copyout(&passed, (void *)arg, sizeof (passed), flag)) { 5597 rval = EFAULT; 5598 } 5599 break; 5600 } 5601 #else /* ! _MULTI_DATAMODE */ 5602 if (((uint64_t)local.mt_count) > UINT32_MAX) { 5603 rval = ERANGE; 5604 } else { 5605 passed.mt_op = local.mt_op; 5606 passed.mt_count = local.mt_count; 5607 if (ddi_copyout(&passed, (void *)arg, sizeof (passed), flag)) { 5608 rval = EFAULT; 5609 } 5610 } 5611 #endif /* _MULTI_DATAMODE */ 5612 5613 5614 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 5615 "st_ioctl: fileno=%x, blkno=%x, eof=%x\n", un->un_pos.fileno, 5616 un->un_pos.blkno, un->un_pos.eof); 5617 5618 if (un->un_pos.pmode == invalid) { 5619 un->un_density_known = 0; 5620 } 5621 5622 ASSERT(mutex_owned(ST_MUTEX)); 5623 return (rval); 5624 } 5625 5626 static int 5627 st_mtiocltop(struct scsi_tape *un, intptr_t arg, int flag) 5628 { 5629 struct mtlop local; 5630 int rval; 5631 5632 ST_FUNC(ST_DEVINFO, st_mtiocltop); 5633 if (ddi_copyin((void *)arg, &local, sizeof (local), flag)) { 5634 return (EFAULT); 5635 } 5636 5637 rval = st_do_mtioctop(un, &local); 5638 5639 if (ddi_copyout(&local, (void *)arg, sizeof (local), flag)) { 5640 rval = EFAULT; 5641 } 5642 return (rval); 5643 } 5644 5645 5646 static int 5647 st_do_mtioctop(struct scsi_tape *un, struct mtlop *mtop) 5648 { 5649 dev_t dev = un->un_dev; 5650 int savefile; 5651 int rval = 0; 5652 5653 ST_FUNC(ST_DEVINFO, st_do_mtioctop); 5654 5655 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 5656 "st_do_mtioctop(): mt_op=%x\n", mtop->mt_op); 5657 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 5658 "fileno=%x, blkno=%x, eof=%x\n", 5659 un->un_pos.fileno, un->un_pos.blkno, un->un_pos.eof); 5660 5661 un->un_status = 0; 5662 5663 /* 5664 * if we are going to mess with a tape, we have to make sure we have 5665 * one and are not offline (i.e. no tape is initialized). We let 5666 * commands pass here that don't actually touch the tape, except for 5667 * loading and initialization (rewinding). 5668 */ 5669 if (un->un_state == ST_STATE_OFFLINE) { 5670 switch (mtop->mt_op) { 5671 case MTLOAD: 5672 case MTNOP: 5673 /* 5674 * We don't want strategy calling st_tape_init here, 5675 * so, change state 5676 */ 5677 un->un_state = ST_STATE_INITIALIZING; 5678 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5679 "st_do_mtioctop : OFFLINE state = %d\n", 5680 un->un_state); 5681 break; 5682 default: 5683 /* 5684 * reinitialize by normal means 5685 */ 5686 rval = st_tape_init(un); 5687 if (rval) { 5688 un->un_state = ST_STATE_INITIALIZING; 5689 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5690 "st_do_mtioctop : OFFLINE init failure "); 5691 un->un_state = ST_STATE_OFFLINE; 5692 un->un_pos.pmode = invalid; 5693 if (rval != EACCES) { 5694 rval = EIO; 5695 } 5696 return (rval); 5697 } 5698 un->un_state = ST_STATE_OPEN_PENDING_IO; 5699 break; 5700 } 5701 } 5702 5703 /* 5704 * If the file position is invalid, allow only those 5705 * commands that properly position the tape and fail 5706 * the rest with EIO 5707 */ 5708 if (un->un_pos.pmode == invalid) { 5709 switch (mtop->mt_op) { 5710 case MTWEOF: 5711 case MTRETEN: 5712 case MTERASE: 5713 case MTEOM: 5714 case MTFSF: 5715 case MTFSR: 5716 case MTBSF: 5717 case MTNBSF: 5718 case MTBSR: 5719 case MTSRSZ: 5720 case MTGRSZ: 5721 case MTSEEK: 5722 case MTBSSF: 5723 case MTFSSF: 5724 return (EIO); 5725 /* NOTREACHED */ 5726 case MTREW: 5727 case MTLOAD: 5728 case MTOFFL: 5729 case MTNOP: 5730 case MTTELL: 5731 case MTLOCK: 5732 case MTUNLOCK: 5733 break; 5734 5735 default: 5736 return (ENOTTY); 5737 /* NOTREACHED */ 5738 } 5739 } 5740 5741 switch (mtop->mt_op) { 5742 case MTERASE: 5743 /* 5744 * MTERASE rewinds the tape, erase it completely, and returns 5745 * to the beginning of the tape 5746 */ 5747 if (un->un_mspl->wp || un->un_read_only & WORM) { 5748 un->un_status = KEY_WRITE_PROTECT; 5749 un->un_err_resid = mtop->mt_count; 5750 COPY_POS(&un->un_err_pos, &un->un_pos); 5751 return (EACCES); 5752 } 5753 if (un->un_dp->options & ST_REEL) { 5754 un->un_fmneeded = 2; 5755 } else { 5756 un->un_fmneeded = 1; 5757 } 5758 mtop->mt_count = mtop->mt_count ? 1 : 0; 5759 if (st_check_density_or_wfm(dev, 1, B_WRITE, NO_STEPBACK) || 5760 st_cmd(un, SCMD_REWIND, 0, SYNC_CMD) || 5761 st_cmd(un, SCMD_ERASE, mtop->mt_count, SYNC_CMD)) { 5762 un->un_pos.pmode = invalid; 5763 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5764 "st_do_mtioctop : EIO space or erase or " 5765 "check den)\n"); 5766 rval = EIO; 5767 } else { 5768 /* QIC and helical scan rewind after erase */ 5769 if (un->un_dp->options & ST_REEL) { 5770 (void) st_cmd(un, SCMD_REWIND, 0, ASYNC_CMD); 5771 } 5772 } 5773 break; 5774 5775 case MTWEOF: 5776 /* 5777 * write an end-of-file record 5778 */ 5779 if (un->un_mspl->wp || un->un_read_only & RDONLY) { 5780 un->un_status = KEY_WRITE_PROTECT; 5781 un->un_err_resid = mtop->mt_count; 5782 COPY_POS(&un->un_err_pos, &un->un_pos); 5783 return (EACCES); 5784 } 5785 5786 /* 5787 * zero count means just flush buffers 5788 * negative count is not permitted 5789 */ 5790 if (mtop->mt_count < 0) { 5791 return (EINVAL); 5792 } 5793 5794 /* Not on worm */ 5795 if (un->un_read_only == RDWR) { 5796 un->un_test_append = 1; 5797 } 5798 5799 if (un->un_state == ST_STATE_OPEN_PENDING_IO) { 5800 if (st_determine_density(un, B_WRITE)) { 5801 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5802 "st_do_mtioctop : EIO : MTWEOF can't " 5803 "determine density"); 5804 return (EIO); 5805 } 5806 } 5807 5808 rval = st_write_fm(dev, (int)mtop->mt_count); 5809 if ((rval != 0) && (rval != EACCES)) { 5810 /* 5811 * Failure due to something other than illegal 5812 * request results in loss of state (st_intr). 5813 */ 5814 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5815 "st_do_mtioctop : EIO : MTWEOF can't write " 5816 "file mark"); 5817 rval = EIO; 5818 } 5819 break; 5820 5821 case MTRETEN: 5822 /* 5823 * retension the tape 5824 */ 5825 if (st_check_density_or_wfm(dev, 1, 0, NO_STEPBACK) || 5826 st_cmd(un, SCMD_LOAD, LD_LOAD | LD_RETEN, SYNC_CMD)) { 5827 un->un_pos.pmode = invalid; 5828 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5829 "st_do_mtioctop : EIO : MTRETEN "); 5830 rval = EIO; 5831 } 5832 break; 5833 5834 case MTREW: 5835 /* 5836 * rewind the tape 5837 */ 5838 if (st_check_density_or_wfm(dev, 1, 0, NO_STEPBACK)) { 5839 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5840 "st_do_mtioctop : EIO:MTREW check " 5841 "density/wfm failed"); 5842 return (EIO); 5843 } 5844 if (st_cmd(un, SCMD_REWIND, 0, SYNC_CMD)) { 5845 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5846 "st_do_mtioctop : EIO : MTREW "); 5847 rval = EIO; 5848 } 5849 break; 5850 5851 case MTOFFL: 5852 /* 5853 * rewinds, and, if appropriate, takes the device offline by 5854 * unloading the tape 5855 */ 5856 if (st_check_density_or_wfm(dev, 1, 0, NO_STEPBACK)) { 5857 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5858 "st_do_mtioctop :EIO:MTOFFL check " 5859 "density/wfm failed"); 5860 return (EIO); 5861 } 5862 (void) st_cmd(un, SCMD_REWIND, 0, SYNC_CMD); 5863 if (st_cmd(un, SCMD_LOAD, LD_UNLOAD, SYNC_CMD)) { 5864 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5865 "st_do_mtioctop : EIO : MTOFFL"); 5866 return (EIO); 5867 } 5868 un->un_pos.eof = ST_NO_EOF; 5869 un->un_laststate = un->un_state; 5870 un->un_state = ST_STATE_OFFLINE; 5871 un->un_mediastate = MTIO_EJECTED; 5872 break; 5873 5874 case MTLOAD: 5875 /* 5876 * This is to load a tape into the drive 5877 * Note that if the tape is not loaded, the device will have 5878 * to be opened via O_NDELAY or O_NONBLOCK. 5879 */ 5880 /* 5881 * Let's try and clean things up, if we are not 5882 * initializing, and then send in the load command, no 5883 * matter what. 5884 * 5885 * load after a media change by the user. 5886 */ 5887 5888 if (un->un_state > ST_STATE_INITIALIZING) { 5889 (void) st_check_density_or_wfm(dev, 1, 0, NO_STEPBACK); 5890 } 5891 rval = st_cmd(un, SCMD_LOAD, LD_LOAD, SYNC_CMD); 5892 /* Load command to a drive that doesn't support load */ 5893 if ((rval == EIO) && 5894 ((un->un_status == KEY_NOT_READY) && 5895 /* Medium not present */ 5896 (un->un_uscsi_rqs_buf->es_add_code == 0x3a) || 5897 ((un->un_status == KEY_ILLEGAL_REQUEST) && 5898 (un->un_dp->type == MT_ISSTK9840) && 5899 /* CSL not present */ 5900 (un->un_uscsi_rqs_buf->es_add_code == 0x80)))) { 5901 rval = ENOTTY; 5902 break; 5903 } else if (rval != EACCES && rval != 0) { 5904 rval = EIO; 5905 } 5906 if (rval) { 5907 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5908 "st_do_mtioctop : %s : MTLOAD\n", 5909 rval == EACCES ? "EACCES" : "EIO"); 5910 /* 5911 * If load tape fails, who knows what happened... 5912 */ 5913 un->un_pos.pmode = invalid; 5914 break; 5915 } 5916 5917 /* 5918 * reset all counters appropriately using rewind, as if LOAD 5919 * succeeds, we are at BOT 5920 */ 5921 un->un_state = ST_STATE_INITIALIZING; 5922 5923 rval = st_tape_init(un); 5924 if ((rval == EACCES) && (un->un_read_only & WORM)) { 5925 rval = 0; 5926 break; 5927 } 5928 5929 if (rval != 0) { 5930 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5931 "st_do_mtioctop : EIO : MTLOAD calls " 5932 "st_tape_init\n"); 5933 rval = EIO; 5934 un->un_state = ST_STATE_OFFLINE; 5935 } 5936 5937 break; 5938 5939 case MTNOP: 5940 un->un_status = 0; /* Reset status */ 5941 un->un_err_resid = 0; 5942 mtop->mt_count = MTUNIT(dev); 5943 break; 5944 5945 case MTEOM: 5946 /* 5947 * positions the tape at a location just after the last file 5948 * written on the tape. For cartridge and 8 mm, this after 5949 * the last file mark; for reel, this is inbetween the two 5950 * last 2 file marks 5951 */ 5952 if ((un->un_pos.pmode == legacy && un->un_pos.eof >= ST_EOT) || 5953 (un->un_lastop == ST_OP_WRITE) || 5954 (un->un_lastop == ST_OP_WEOF)) { 5955 /* 5956 * If the command wants to move to logical end 5957 * of media, and we're already there, we're done. 5958 * If we were at logical eot, we reset the state 5959 * to be *not* at logical eot. 5960 * 5961 * If we're at physical or logical eot, we prohibit 5962 * forward space operations (unconditionally). 5963 * 5964 * Also if the last operation was a write of any 5965 * kind the tape is at EOD. 5966 */ 5967 return (0); 5968 } 5969 /* 5970 * physical tape position may not be what we've been 5971 * telling the user; adjust the request accordingly 5972 */ 5973 if (IN_EOF(un->un_pos)) { 5974 un->un_pos.fileno++; 5975 un->un_pos.blkno = 0; 5976 } 5977 5978 if (st_check_density_or_wfm(dev, 1, B_READ, NO_STEPBACK)) { 5979 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5980 "st_do_mtioctop : EIO:MTEOM check density/wfm " 5981 " failed"); 5982 return (EIO); 5983 } 5984 5985 /* 5986 * st_find_eod() returns the last fileno we knew about; 5987 */ 5988 savefile = st_find_eod(un); 5989 5990 if ((un->un_status != KEY_BLANK_CHECK) && 5991 (un->un_status != SUN_KEY_EOT)) { 5992 un->un_pos.pmode = invalid; 5993 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5994 "st_do_mtioctop : EIO : MTEOM status check failed"); 5995 rval = EIO; 5996 } else { 5997 /* 5998 * For 1/2" reel tapes assume logical EOT marked 5999 * by two file marks or we don't care that we may 6000 * be extending the last file on the tape. 6001 */ 6002 if (un->un_dp->options & ST_REEL) { 6003 if (st_cmd(un, SCMD_SPACE, Fmk(-1), SYNC_CMD)) { 6004 un->un_pos.pmode = invalid; 6005 ST_DEBUG2(ST_DEVINFO, st_label, 6006 SCSI_DEBUG, 6007 "st_do_mtioctop : EIO : MTEOM space" 6008 " cmd failed"); 6009 rval = EIO; 6010 break; 6011 } 6012 /* 6013 * Fix up the block number. 6014 */ 6015 un->un_pos.blkno = 0; 6016 un->un_err_pos.blkno = 0; 6017 } 6018 un->un_err_resid = 0; 6019 un->un_pos.fileno = savefile; 6020 un->un_pos.eof = ST_EOT; 6021 } 6022 un->un_status = 0; 6023 break; 6024 6025 case MTFSF: 6026 MAX_SPACE_CNT(mtop->mt_count); 6027 rval = st_mtfsf_ioctl(un, mtop->mt_count); 6028 break; 6029 6030 case MTFSR: 6031 MAX_SPACE_CNT(mtop->mt_count); 6032 rval = st_mtfsr_ioctl(un, mtop->mt_count); 6033 break; 6034 6035 case MTBSF: 6036 MAX_SPACE_CNT(mtop->mt_count); 6037 rval = st_mtbsf_ioctl(un, mtop->mt_count); 6038 break; 6039 6040 case MTNBSF: 6041 MAX_SPACE_CNT(mtop->mt_count); 6042 rval = st_mtnbsf_ioctl(un, mtop->mt_count); 6043 break; 6044 6045 case MTBSR: 6046 MAX_SPACE_CNT(mtop->mt_count); 6047 rval = st_mtbsr_ioctl(un, mtop->mt_count); 6048 break; 6049 6050 case MTBSSF: 6051 MAX_SPACE_CNT(mtop->mt_count); 6052 rval = st_mtbsfm_ioctl(un, mtop->mt_count); 6053 break; 6054 6055 case MTFSSF: 6056 MAX_SPACE_CNT(mtop->mt_count); 6057 rval = st_mtfsfm_ioctl(un, mtop->mt_count); 6058 break; 6059 6060 case MTSRSZ: 6061 6062 /* 6063 * Set record-size to that sent by user 6064 * Check to see if there is reason that the requested 6065 * block size should not be set. 6066 */ 6067 6068 /* If requesting variable block size is it ok? */ 6069 if ((mtop->mt_count == 0) && 6070 ((un->un_dp->options & ST_VARIABLE) == 0)) { 6071 return (ENOTTY); 6072 } 6073 6074 /* 6075 * If requested block size is not variable "0", 6076 * is it less then minimum. 6077 */ 6078 if ((mtop->mt_count != 0) && 6079 (mtop->mt_count < un->un_minbsize)) { 6080 return (EINVAL); 6081 } 6082 6083 /* Is the requested block size more then maximum */ 6084 if ((mtop->mt_count > min(un->un_maxbsize, un->un_maxdma)) && 6085 (un->un_maxbsize != 0)) { 6086 return (EINVAL); 6087 } 6088 6089 /* Is requested block size a modulus the device likes */ 6090 if ((mtop->mt_count % un->un_data_mod) != 0) { 6091 return (EINVAL); 6092 } 6093 6094 if (st_change_block_size(un, (uint32_t)mtop->mt_count) != 0) { 6095 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 6096 "st_ioctl : MTSRSZ : EIO : cant set block size"); 6097 return (EIO); 6098 } 6099 6100 return (0); 6101 6102 case MTGRSZ: 6103 /* 6104 * Get record-size to the user 6105 */ 6106 mtop->mt_count = un->un_bsize; 6107 rval = 0; 6108 break; 6109 6110 case MTTELL: 6111 rval = st_update_block_pos(un, st_cmd, 0); 6112 mtop->mt_count = un->un_pos.lgclblkno; 6113 break; 6114 6115 case MTSEEK: 6116 rval = st_logical_block_locate(un, st_uscsi_cmd, &un->un_pos, 6117 (uint64_t)mtop->mt_count, un->un_pos.partition); 6118 /* 6119 * This bit of magic make mt print the actual position if 6120 * the resulting position was not what was asked for. 6121 */ 6122 if (rval == ESPIPE) { 6123 rval = EIO; 6124 if ((uint64_t)mtop->mt_count != un->un_pos.lgclblkno) { 6125 mtop->mt_op = MTTELL; 6126 mtop->mt_count = un->un_pos.lgclblkno; 6127 } 6128 } 6129 break; 6130 6131 case MTLOCK: 6132 if (st_cmd(un, SCMD_DOORLOCK, MR_LOCK, SYNC_CMD)) { 6133 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 6134 "st_do_mtioctop : EIO : MTLOCK"); 6135 rval = EIO; 6136 } 6137 break; 6138 6139 case MTUNLOCK: 6140 if (st_cmd(un, SCMD_DOORLOCK, MR_UNLOCK, SYNC_CMD)) { 6141 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 6142 "st_do_mtioctop : EIO : MTUNLOCK"); 6143 rval = EIO; 6144 } 6145 break; 6146 6147 default: 6148 rval = ENOTTY; 6149 } 6150 6151 return (rval); 6152 } 6153 6154 6155 /* 6156 * Run a command for uscsi ioctl. 6157 */ 6158 static int 6159 st_uscsi_cmd(struct scsi_tape *un, struct uscsi_cmd *ucmd, int flag) 6160 { 6161 struct uscsi_cmd *uscmd; 6162 struct buf *bp; 6163 enum uio_seg uioseg; 6164 int offline_state = 0; 6165 int err = 0; 6166 dev_t dev = un->un_dev; 6167 6168 ST_FUNC(ST_DEVINFO, st_uscsi_cmd); 6169 6170 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 6171 "st_uscsi_cmd(dev = 0x%lx)\n", un->un_dev); 6172 6173 ASSERT(mutex_owned(ST_MUTEX)); 6174 6175 /* 6176 * We really don't know what commands are coming in here and 6177 * we don't want to limit the commands coming in. 6178 * 6179 * If st_tape_init() gets called from st_strategy(), then we 6180 * will hang the process waiting for un->un_sbuf_busy to be cleared, 6181 * which it never will, as we set it below. To prevent 6182 * st_tape_init() from getting called, we have to set state to other 6183 * than ST_STATE_OFFLINE, so we choose ST_STATE_INITIALIZING, which 6184 * achieves this purpose already. 6185 * 6186 * We use offline_state to preserve the OFFLINE state, if it exists, 6187 * so other entry points to the driver might have the chance to call 6188 * st_tape_init(). 6189 */ 6190 if (un->un_state == ST_STATE_OFFLINE) { 6191 un->un_laststate = ST_STATE_OFFLINE; 6192 un->un_state = ST_STATE_INITIALIZING; 6193 offline_state = 1; 6194 } 6195 6196 mutex_exit(ST_MUTEX); 6197 err = scsi_uscsi_alloc_and_copyin((intptr_t)ucmd, flag, ROUTE, &uscmd); 6198 mutex_enter(ST_MUTEX); 6199 if (err != 0) { 6200 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 6201 "st_uscsi_cmd: scsi_uscsi_alloc_and_copyin failed\n"); 6202 goto exit; 6203 } 6204 6205 uioseg = (flag & FKIOCTL) ? UIO_SYSSPACE : UIO_USERSPACE; 6206 6207 /* check to see if this command requires the drive to be reserved */ 6208 if (uscmd->uscsi_cdb != NULL) { 6209 err = st_check_cdb_for_need_to_reserve(un, 6210 (uchar_t *)uscmd->uscsi_cdb); 6211 if (err) { 6212 goto exit_free; 6213 } 6214 /* 6215 * If this is a space command we need to save the starting 6216 * point so we can retry from there if the command fails. 6217 */ 6218 if ((uscmd->uscsi_cdb[0] == SCMD_SPACE) || 6219 (uscmd->uscsi_cdb[0] == (char)SCMD_SPACE_G4)) { 6220 (void) st_update_block_pos(un, st_cmd, 0); 6221 } 6222 } 6223 6224 /* 6225 * Forground should not be doing anything while recovery is active. 6226 */ 6227 ASSERT(un->un_recov_buf_busy == 0); 6228 6229 /* 6230 * Get buffer resources... 6231 */ 6232 while (un->un_sbuf_busy) 6233 cv_wait(&un->un_sbuf_cv, ST_MUTEX); 6234 un->un_sbuf_busy = 1; 6235 6236 #ifdef STDEBUG 6237 if ((uscmd->uscsi_cdb != NULL) && (st_debug & 0x7) > 6) { 6238 int rw = (uscmd->uscsi_flags & USCSI_READ) ? B_READ : B_WRITE; 6239 st_print_cdb(ST_DEVINFO, st_label, SCSI_DEBUG, 6240 "uscsi cdb", uscmd->uscsi_cdb); 6241 if (uscmd->uscsi_buflen) { 6242 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 6243 "uscsi %s of %ld bytes %s %s space\n", 6244 (rw == B_READ) ? rd_str : wr_str, 6245 uscmd->uscsi_buflen, 6246 (rw == B_READ) ? "to" : "from", 6247 (uioseg == UIO_SYSSPACE) ? "system" : "user"); 6248 } 6249 } 6250 #endif /* STDEBUG */ 6251 6252 /* 6253 * Although st_uscsi_cmd() never makes use of these 6254 * now, we are just being safe and consistent. 6255 */ 6256 uscmd->uscsi_flags &= ~(USCSI_NOINTR | USCSI_NOPARITY | 6257 USCSI_OTAG | USCSI_HTAG | USCSI_HEAD); 6258 6259 un->un_srqbufp = uscmd->uscsi_rqbuf; 6260 bp = un->un_sbufp; 6261 bzero(bp, sizeof (buf_t)); 6262 if (uscmd->uscsi_cdb != NULL) { 6263 bp->b_forw = (struct buf *)(uintptr_t)uscmd->uscsi_cdb[0]; 6264 } 6265 bp->b_back = (struct buf *)uscmd; 6266 6267 mutex_exit(ST_MUTEX); 6268 err = scsi_uscsi_handle_cmd(dev, uioseg, uscmd, st_strategy, bp, NULL); 6269 mutex_enter(ST_MUTEX); 6270 6271 /* 6272 * If scsi reset successful, don't write any filemarks. 6273 */ 6274 if ((err == 0) && (uscmd->uscsi_flags & 6275 (USCSI_RESET_LUN | USCSI_RESET_TARGET | USCSI_RESET_ALL))) { 6276 un->un_fmneeded = 0; 6277 } 6278 6279 exit_free: 6280 /* 6281 * Free resources 6282 */ 6283 un->un_sbuf_busy = 0; 6284 un->un_srqbufp = NULL; 6285 6286 /* 6287 * If was a space command need to update logical block position. 6288 * If the command failed such that positioning is invalid, Don't 6289 * update the position as the user must do this to validate the 6290 * position for data protection. 6291 */ 6292 if ((uscmd->uscsi_cdb != NULL) && 6293 ((uscmd->uscsi_cdb[0] == SCMD_SPACE) || 6294 (uscmd->uscsi_cdb[0] == (char)SCMD_SPACE_G4)) && 6295 (un->un_pos.pmode != invalid)) { 6296 un->un_running.pmode = invalid; 6297 (void) st_update_block_pos(un, st_cmd, 1); 6298 /* 6299 * Set running position to invalid so it updates on the 6300 * next command. 6301 */ 6302 un->un_running.pmode = invalid; 6303 } 6304 cv_signal(&un->un_sbuf_cv); 6305 mutex_exit(ST_MUTEX); 6306 (void) scsi_uscsi_copyout_and_free((intptr_t)ucmd, uscmd); 6307 mutex_enter(ST_MUTEX); 6308 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 6309 "st_uscsi_cmd returns 0x%x\n", err); 6310 6311 exit: 6312 /* don't lose offline state */ 6313 if (offline_state) { 6314 un->un_state = ST_STATE_OFFLINE; 6315 } 6316 6317 ASSERT(mutex_owned(ST_MUTEX)); 6318 return (err); 6319 } 6320 6321 static int 6322 st_write_fm(dev_t dev, int wfm) 6323 { 6324 int i; 6325 int rval; 6326 6327 GET_SOFT_STATE(dev); 6328 6329 ST_FUNC(ST_DEVINFO, st_write_fm); 6330 6331 ASSERT(mutex_owned(ST_MUTEX)); 6332 6333 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 6334 "st_write_fm(dev = 0x%lx, wfm = %d)\n", dev, wfm); 6335 6336 /* 6337 * write one filemark at the time after EOT 6338 */ 6339 if (un->un_pos.eof >= ST_EOT) { 6340 for (i = 0; i < wfm; i++) { 6341 rval = st_cmd(un, SCMD_WRITE_FILE_MARK, 1, SYNC_CMD); 6342 if (rval == EACCES) { 6343 return (rval); 6344 } 6345 if (rval != 0) { 6346 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 6347 "st_write_fm : EIO : write EOT file mark"); 6348 return (EIO); 6349 } 6350 } 6351 } else { 6352 rval = st_cmd(un, SCMD_WRITE_FILE_MARK, wfm, SYNC_CMD); 6353 if (rval == EACCES) { 6354 return (rval); 6355 } 6356 if (rval) { 6357 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 6358 "st_write_fm : EIO : write file mark"); 6359 return (EIO); 6360 } 6361 } 6362 6363 ASSERT(mutex_owned(ST_MUTEX)); 6364 return (0); 6365 } 6366 6367 #ifdef STDEBUG 6368 static void 6369 st_start_dump(struct scsi_tape *un, struct buf *bp) 6370 { 6371 struct scsi_pkt *pkt = BP_PKT(bp); 6372 uchar_t *cdbp = (uchar_t *)pkt->pkt_cdbp; 6373 6374 ST_FUNC(ST_DEVINFO, st_start_dump); 6375 6376 if ((st_debug & 0x7) < 6) 6377 return; 6378 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 6379 "st_start: cmd=0x%p count=%ld resid=%ld flags=0x%x pkt=0x%p\n", 6380 (void *)bp->b_forw, bp->b_bcount, 6381 bp->b_resid, bp->b_flags, (void *)BP_PKT(bp)); 6382 st_print_cdb(ST_DEVINFO, st_label, SCSI_DEBUG, 6383 "st_start: cdb", (caddr_t)cdbp); 6384 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 6385 "st_start: fileno=%d, blk=%d\n", 6386 un->un_pos.fileno, un->un_pos.blkno); 6387 } 6388 #endif 6389 6390 6391 /* 6392 * Command start && done functions 6393 */ 6394 6395 /* 6396 * st_start() 6397 * 6398 * Called from: 6399 * st_strategy() to start a command. 6400 * st_runout() to retry when scsi_pkt allocation fails on previous attempt(s). 6401 * st_attach() when resuming from power down state. 6402 * st_start_restart() to retry transport when device was previously busy. 6403 * st_done_and_mutex_exit() to start the next command when previous is done. 6404 * 6405 * On entry: 6406 * scsi_pkt may or may not be allocated. 6407 * 6408 */ 6409 static void 6410 st_start(struct scsi_tape *un) 6411 { 6412 struct buf *bp; 6413 int status; 6414 int queued; 6415 6416 ST_FUNC(ST_DEVINFO, st_start); 6417 ASSERT(mutex_owned(ST_MUTEX)); 6418 6419 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 6420 "st_start(): dev = 0x%lx\n", un->un_dev); 6421 6422 if (un->un_recov_buf_busy) { 6423 /* recovery commands can happen anytime */ 6424 bp = un->un_recov_buf; 6425 queued = 0; 6426 } else if (un->un_sbuf_busy) { 6427 /* sbuf commands should only happen with an empty queue. */ 6428 ASSERT(un->un_quef == NULL); 6429 ASSERT(un->un_runqf == NULL); 6430 bp = un->un_sbufp; 6431 queued = 0; 6432 } else if (un->un_quef != NULL) { 6433 if (un->un_persistence && un->un_persist_errors) { 6434 return; 6435 } 6436 bp = un->un_quef; 6437 queued = 1; 6438 } else { 6439 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 6440 "st_start() returning no buf found\n"); 6441 return; 6442 } 6443 6444 ASSERT((bp->b_flags & B_DONE) == 0); 6445 6446 /* 6447 * Don't send more than un_throttle commands to the HBA 6448 */ 6449 if ((un->un_throttle <= 0) || (un->un_ncmds >= un->un_throttle)) { 6450 /* 6451 * if doing recovery we know there is outstanding commands. 6452 */ 6453 if (bp != un->un_recov_buf) { 6454 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 6455 "st_start returning throttle = %d or ncmds = %d\n", 6456 un->un_throttle, un->un_ncmds); 6457 if (un->un_ncmds == 0) { 6458 typedef void (*func)(); 6459 func fnc = (func)st_runout; 6460 6461 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 6462 "Sending delayed start to st_runout()\n"); 6463 mutex_exit(ST_MUTEX); 6464 (void) timeout(fnc, un, drv_usectohz(1000000)); 6465 mutex_enter(ST_MUTEX); 6466 } 6467 return; 6468 } 6469 } 6470 6471 /* 6472 * If the buf has no scsi_pkt call st_make_cmd() to get one and 6473 * build the command. 6474 */ 6475 if (BP_PKT(bp) == NULL) { 6476 ASSERT((bp->b_flags & B_DONE) == 0); 6477 st_make_cmd(un, bp, st_runout); 6478 ASSERT((bp->b_flags & B_DONE) == 0); 6479 status = geterror(bp); 6480 6481 /* 6482 * Some HBA's don't call bioerror() to set an error. 6483 * And geterror() returns zero if B_ERROR is not set. 6484 * So if we get zero we must check b_error. 6485 */ 6486 if (status == 0 && bp->b_error != 0) { 6487 status = bp->b_error; 6488 bioerror(bp, status); 6489 } 6490 6491 /* 6492 * Some HBA's convert DDI_DMA_NORESOURCES into ENOMEM. 6493 * In tape ENOMEM has special meaning so we'll change it. 6494 */ 6495 if (status == ENOMEM) { 6496 status = 0; 6497 bioerror(bp, status); 6498 } 6499 6500 /* 6501 * Did it fail and is it retryable? 6502 * If so return and wait for the callback through st_runout. 6503 * Also looks like scsi_init_pkt() will setup a callback even 6504 * if it isn't retryable. 6505 */ 6506 if (BP_PKT(bp) == NULL) { 6507 if (status == 0) { 6508 /* 6509 * If first attempt save state. 6510 */ 6511 if (un->un_state != ST_STATE_RESOURCE_WAIT) { 6512 un->un_laststate = un->un_state; 6513 un->un_state = ST_STATE_RESOURCE_WAIT; 6514 } 6515 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 6516 "temp no resources for pkt\n"); 6517 } else if (status == EINVAL) { 6518 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 6519 "scsi_init_pkt rejected pkt as too big\n"); 6520 if (un->un_persistence) { 6521 st_set_pe_flag(un); 6522 } 6523 } else { 6524 /* 6525 * Unlikely that it would be retryable then not. 6526 */ 6527 if (un->un_state == ST_STATE_RESOURCE_WAIT) { 6528 un->un_state = un->un_laststate; 6529 } 6530 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 6531 "perm no resources for pkt errno = 0x%x\n", 6532 status); 6533 } 6534 return; 6535 } 6536 /* 6537 * Worked this time set the state back. 6538 */ 6539 if (un->un_state == ST_STATE_RESOURCE_WAIT) { 6540 un->un_state = un->un_laststate; 6541 } 6542 } 6543 6544 if (queued) { 6545 /* 6546 * move from waitq to runq 6547 */ 6548 (void) st_remove_from_queue(&un->un_quef, &un->un_quel, bp); 6549 st_add_to_queue(&un->un_runqf, &un->un_runql, un->un_runql, bp); 6550 } 6551 6552 6553 #ifdef STDEBUG 6554 st_start_dump(un, bp); 6555 #endif 6556 6557 /* could not get here if throttle was zero */ 6558 un->un_last_throttle = un->un_throttle; 6559 un->un_throttle = 0; /* so nothing else will come in here */ 6560 un->un_ncmds++; 6561 6562 ST_DO_KSTATS(bp, kstat_waitq_to_runq); 6563 6564 status = st_transport(un, BP_PKT(bp)); 6565 6566 if (un->un_last_throttle) { 6567 un->un_throttle = un->un_last_throttle; 6568 } 6569 6570 if (status != TRAN_ACCEPT) { 6571 ST_DO_KSTATS(bp, kstat_runq_back_to_waitq); 6572 ST_DEBUG(ST_DEVINFO, st_label, CE_WARN, 6573 "Unhappy transport packet status 0x%x\n", status); 6574 6575 if (status == TRAN_BUSY) { 6576 pkt_info *pkti = BP_PKT(bp)->pkt_private; 6577 6578 /* 6579 * If command recovery is enabled and this isn't 6580 * a recovery command try command recovery. 6581 */ 6582 if (pkti->privatelen == sizeof (recov_info) && 6583 bp != un->un_recov_buf) { 6584 ST_RECOV(ST_DEVINFO, st_label, CE_WARN, 6585 "Command Recovery called on busy send\n"); 6586 if (st_command_recovery(un, BP_PKT(bp), 6587 ATTEMPT_RETRY) == JUST_RETURN) { 6588 return; 6589 } 6590 } else { 6591 mutex_exit(ST_MUTEX); 6592 if (st_handle_start_busy(un, bp, 6593 ST_TRAN_BUSY_TIMEOUT, queued) == 0) { 6594 mutex_enter(ST_MUTEX); 6595 return; 6596 } 6597 /* 6598 * if too many retries, fail the transport 6599 */ 6600 mutex_enter(ST_MUTEX); 6601 } 6602 } 6603 scsi_log(ST_DEVINFO, st_label, CE_WARN, 6604 "transport rejected %d\n", status); 6605 bp->b_resid = bp->b_bcount; 6606 6607 ST_DO_KSTATS(bp, kstat_waitq_exit); 6608 ST_DO_ERRSTATS(un, st_transerrs); 6609 if ((bp == un->un_recov_buf) && (status == TRAN_BUSY)) { 6610 st_bioerror(bp, EBUSY); 6611 } else { 6612 st_bioerror(bp, EIO); 6613 st_set_pe_flag(un); 6614 } 6615 st_done_and_mutex_exit(un, bp); 6616 mutex_enter(ST_MUTEX); 6617 } 6618 6619 ASSERT(mutex_owned(ST_MUTEX)); 6620 } 6621 6622 /* 6623 * if the transport is busy, then put this bp back on the waitq 6624 */ 6625 static int 6626 st_handle_start_busy(struct scsi_tape *un, struct buf *bp, 6627 clock_t timeout_interval, int queued) 6628 { 6629 6630 pkt_info *pktinfo = BP_PKT(bp)->pkt_private; 6631 6632 ST_FUNC(ST_DEVINFO, st_handle_start_busy); 6633 6634 mutex_enter(ST_MUTEX); 6635 6636 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 6637 "st_handle_start_busy()\n"); 6638 6639 /* 6640 * Check to see if we hit the retry timeout and one last check for 6641 * making sure this is the last on the runq, if it is not, we have 6642 * to fail 6643 */ 6644 if ((pktinfo->str_retry_cnt++ > st_retry_count) || 6645 ((queued) && (un->un_runql != bp))) { 6646 mutex_exit(ST_MUTEX); 6647 return (-1); 6648 } 6649 6650 if (queued) { 6651 /* put the bp back on the waitq */ 6652 st_add_to_queue(&un->un_quef, &un->un_quel, un->un_quef, bp); 6653 } 6654 6655 /* 6656 * Decrement un_ncmds so that this 6657 * gets thru' st_start() again. 6658 */ 6659 un->un_ncmds--; 6660 6661 if (queued) { 6662 /* 6663 * since this is an error case, we won't have to do this list 6664 * walking much. We've already made sure this bp was the 6665 * last on the runq 6666 */ 6667 (void) st_remove_from_queue(&un->un_runqf, &un->un_runql, bp); 6668 6669 /* 6670 * send a marker pkt, if appropriate 6671 */ 6672 st_hba_unflush(un); 6673 6674 } 6675 /* 6676 * all queues are aligned, we are just waiting to 6677 * transport, don't alloc any more buf p's, when 6678 * st_start is reentered. 6679 */ 6680 (void) timeout(st_start_restart, un, timeout_interval); 6681 6682 mutex_exit(ST_MUTEX); 6683 return (0); 6684 } 6685 6686 6687 /* 6688 * st_runout a callback that is called what a resource allocatation failed 6689 */ 6690 static int 6691 st_runout(caddr_t arg) 6692 { 6693 struct scsi_tape *un = (struct scsi_tape *)arg; 6694 struct buf *bp; 6695 int queued; 6696 6697 ASSERT(un != NULL); 6698 6699 ST_FUNC(ST_DEVINFO, st_runout); 6700 6701 mutex_enter(ST_MUTEX); 6702 6703 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, "st_runout()\n"); 6704 6705 if (un->un_recov_buf_busy != 0) { 6706 bp = un->un_recov_buf; 6707 queued = 0; 6708 } else if (un->un_sbuf_busy != 0) { 6709 /* sbuf commands should only happen with an empty queue. */ 6710 ASSERT(un->un_quef == NULL); 6711 ASSERT(un->un_runqf == NULL); 6712 bp = un->un_sbufp; 6713 queued = 0; 6714 } else if (un->un_quef != NULL) { 6715 bp = un->un_quef; 6716 if (un->un_persistence && un->un_persist_errors) { 6717 mutex_exit(ST_MUTEX); 6718 bp->b_resid = bp->b_bcount; 6719 biodone(bp); 6720 return (1); 6721 } 6722 queued = 1; 6723 } else { 6724 ASSERT(1 == 0); 6725 mutex_exit(ST_MUTEX); 6726 return (1); 6727 } 6728 6729 /* 6730 * failed scsi_init_pkt(). If errno is zero its retryable. 6731 */ 6732 if ((bp != NULL) && (geterror(bp) != 0)) { 6733 6734 scsi_log(ST_DEVINFO, st_label, CE_WARN, 6735 "errors after pkt alloc (b_flags=0x%x, b_error=0x%x)\n", 6736 bp->b_flags, geterror(bp)); 6737 ASSERT((bp->b_flags & B_DONE) == 0); 6738 6739 if (queued) { 6740 (void) st_remove_from_queue(&un->un_quef, &un->un_quel, 6741 bp); 6742 } 6743 mutex_exit(ST_MUTEX); 6744 6745 ASSERT((bp->b_flags & B_DONE) == 0); 6746 6747 /* 6748 * Set resid, Error already set, then unblock calling thread. 6749 */ 6750 bp->b_resid = bp->b_bcount; 6751 biodone(bp); 6752 } else { 6753 /* 6754 * Try Again 6755 */ 6756 st_start(un); 6757 mutex_exit(ST_MUTEX); 6758 } 6759 6760 /* 6761 * Comments courtesy of sd.c 6762 * The scsi_init_pkt routine allows for the callback function to 6763 * return a 0 indicating the callback should be rescheduled or a 1 6764 * indicating not to reschedule. This routine always returns 1 6765 * because the driver always provides a callback function to 6766 * scsi_init_pkt. This results in a callback always being scheduled 6767 * (via the scsi_init_pkt callback implementation) if a resource 6768 * failure occurs. 6769 */ 6770 6771 return (1); 6772 } 6773 6774 /* 6775 * st_done_and_mutex_exit() 6776 * - remove bp from runq 6777 * - start up the next request 6778 * - if this was an asynch bp, clean up 6779 * - exit with released mutex 6780 */ 6781 static void 6782 st_done_and_mutex_exit(struct scsi_tape *un, struct buf *bp) 6783 { 6784 int pe_flagged = 0; 6785 struct scsi_pkt *pkt = BP_PKT(bp); 6786 pkt_info *pktinfo = pkt->pkt_private; 6787 6788 ASSERT(MUTEX_HELD(&un->un_sd->sd_mutex)); 6789 #if !defined(lint) 6790 _NOTE(LOCK_RELEASED_AS_SIDE_EFFECT(&un->un_sd->sd_mutex)) 6791 #endif 6792 6793 ST_FUNC(ST_DEVINFO, st_done_and_mutex_exit); 6794 6795 ASSERT(mutex_owned(ST_MUTEX)); 6796 6797 (void) st_remove_from_queue(&un->un_runqf, &un->un_runql, bp); 6798 6799 un->un_ncmds--; 6800 cv_signal(&un->un_queue_cv); 6801 6802 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 6803 "st_done_and_mutex_exit(): cmd=0x%x count=%ld resid=%ld flags=" 6804 "0x%x\n", pkt->pkt_cdbp[0], bp->b_bcount, 6805 bp->b_resid, bp->b_flags); 6806 6807 6808 /* 6809 * update kstats with transfer count info 6810 */ 6811 if (un->un_stats && (bp != un->un_sbufp) && IS_RW(bp)) { 6812 uint32_t n_done = bp->b_bcount - bp->b_resid; 6813 if (bp->b_flags & B_READ) { 6814 IOSP->reads++; 6815 IOSP->nread += n_done; 6816 } else { 6817 IOSP->writes++; 6818 IOSP->nwritten += n_done; 6819 } 6820 } 6821 6822 /* 6823 * Start the next one before releasing resources on this one, if 6824 * there is something on the queue and persistent errors has not been 6825 * flagged 6826 */ 6827 6828 if ((pe_flagged = (un->un_persistence && un->un_persist_errors)) != 0) { 6829 un->un_last_resid = bp->b_resid; 6830 un->un_last_count = bp->b_bcount; 6831 } 6832 6833 if (un->un_pwr_mgmt == ST_PWR_SUSPENDED) { 6834 cv_broadcast(&un->un_tape_busy_cv); 6835 } else if (un->un_quef && un->un_throttle && !pe_flagged && 6836 (bp != un->un_recov_buf)) { 6837 st_start(un); 6838 } 6839 6840 un->un_retry_ct = max(pktinfo->pkt_retry_cnt, pktinfo->str_retry_cnt); 6841 6842 if (bp == un->un_sbufp && (bp->b_flags & B_ASYNC)) { 6843 /* 6844 * Since we marked this ourselves as ASYNC, 6845 * there isn't anybody around waiting for 6846 * completion any more. 6847 */ 6848 uchar_t *cmd = pkt->pkt_cdbp; 6849 if (*cmd == SCMD_READ || *cmd == SCMD_WRITE) { 6850 bp->b_un.b_addr = (caddr_t)0; 6851 } 6852 ST_DEBUG(ST_DEVINFO, st_label, CE_NOTE, 6853 "st_done_and_mutex_exit(async): freeing pkt\n"); 6854 st_print_cdb(ST_DEVINFO, st_label, CE_NOTE, 6855 "CDB sent with B_ASYNC", (caddr_t)cmd); 6856 if (pkt) { 6857 scsi_destroy_pkt(pkt); 6858 } 6859 un->un_sbuf_busy = 0; 6860 cv_signal(&un->un_sbuf_cv); 6861 mutex_exit(ST_MUTEX); 6862 return; 6863 } 6864 6865 if (bp == un->un_sbufp && BP_UCMD(bp)) { 6866 /* 6867 * Copy status from scsi_pkt to uscsi_cmd 6868 * since st_uscsi_cmd needs it 6869 */ 6870 BP_UCMD(bp)->uscsi_status = SCBP_C(BP_PKT(bp)); 6871 } 6872 6873 6874 #ifdef STDEBUG 6875 if (((st_debug & 0x7) >= 4) && 6876 (((un->un_pos.blkno % 100) == 0) || 6877 (un->un_persistence && un->un_persist_errors))) { 6878 6879 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 6880 "st_d_a_m_exit(): ncmds = %d, thr = %d, " 6881 "un_errno = %d, un_pe = %d\n", 6882 un->un_ncmds, un->un_throttle, un->un_errno, 6883 un->un_persist_errors); 6884 } 6885 6886 #endif 6887 6888 mutex_exit(ST_MUTEX); 6889 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 6890 "st_done_and_mutex_exit: freeing pkt\n"); 6891 6892 if (pkt) { 6893 scsi_destroy_pkt(pkt); 6894 } 6895 6896 biodone(bp); 6897 6898 /* 6899 * now that we biodoned that command, if persistent errors have been 6900 * flagged, flush the waitq 6901 */ 6902 if (pe_flagged) 6903 st_flush(un); 6904 } 6905 6906 6907 /* 6908 * Tape error, flush tape driver queue. 6909 */ 6910 static void 6911 st_flush(struct scsi_tape *un) 6912 { 6913 struct buf *bp; 6914 6915 ST_FUNC(ST_DEVINFO, st_flush); 6916 6917 mutex_enter(ST_MUTEX); 6918 6919 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 6920 "st_flush(), ncmds = %d, quef = 0x%p\n", 6921 un->un_ncmds, (void *)un->un_quef); 6922 6923 /* 6924 * if we still have commands outstanding, wait for them to come in 6925 * before flushing the queue, and make sure there is a queue 6926 */ 6927 if (un->un_ncmds || !un->un_quef) 6928 goto exit; 6929 6930 /* 6931 * we have no more commands outstanding, so let's deal with special 6932 * cases in the queue for EOM and FM. If we are here, and un_errno 6933 * is 0, then we know there was no error and we return a 0 read or 6934 * write before showing errors 6935 */ 6936 6937 /* Flush the wait queue. */ 6938 while ((bp = un->un_quef) != NULL) { 6939 un->un_quef = bp->b_actf; 6940 6941 bp->b_resid = bp->b_bcount; 6942 6943 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 6944 "st_flush() : blkno=%d, err=%d, b_bcount=%ld\n", 6945 un->un_pos.blkno, un->un_errno, bp->b_bcount); 6946 6947 st_set_pe_errno(un); 6948 6949 bioerror(bp, un->un_errno); 6950 6951 mutex_exit(ST_MUTEX); 6952 /* it should have one, but check anyway */ 6953 if (BP_PKT(bp)) { 6954 scsi_destroy_pkt(BP_PKT(bp)); 6955 } 6956 biodone(bp); 6957 mutex_enter(ST_MUTEX); 6958 } 6959 6960 /* 6961 * It's not a bad practice to reset the 6962 * waitq tail pointer to NULL. 6963 */ 6964 un->un_quel = NULL; 6965 6966 exit: 6967 /* we mucked with the queue, so let others know about it */ 6968 cv_signal(&un->un_queue_cv); 6969 mutex_exit(ST_MUTEX); 6970 } 6971 6972 6973 /* 6974 * Utility functions 6975 */ 6976 static int 6977 st_determine_generic(struct scsi_tape *un) 6978 { 6979 int bsize; 6980 static char *cart = "0.25 inch cartridge"; 6981 char *sizestr; 6982 6983 ST_FUNC(ST_DEVINFO, st_determine_generic); 6984 6985 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 6986 "st_determine_generic(un = 0x%p)\n", (void*)un); 6987 6988 ASSERT(mutex_owned(ST_MUTEX)); 6989 6990 if (st_modesense(un)) { 6991 return (-1); 6992 } 6993 6994 bsize = (un->un_mspl->high_bl << 16) | 6995 (un->un_mspl->mid_bl << 8) | 6996 (un->un_mspl->low_bl); 6997 6998 if (bsize == 0) { 6999 un->un_dp->options |= ST_VARIABLE; 7000 un->un_dp->bsize = 0; 7001 un->un_bsize = 0; 7002 } else if (bsize > ST_MAXRECSIZE_FIXED) { 7003 /* 7004 * record size of this device too big. 7005 * try and convert it to variable record length. 7006 * 7007 */ 7008 un->un_dp->options |= ST_VARIABLE; 7009 if (st_change_block_size(un, 0) != 0) { 7010 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 7011 "Fixed Record Size %d is too large\n", bsize); 7012 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 7013 "Cannot switch to variable record size\n"); 7014 un->un_dp->options &= ~ST_VARIABLE; 7015 return (-1); 7016 } 7017 } else if (st_change_block_size(un, 0) == 0) { 7018 /* 7019 * If the drive was set to a non zero block size, 7020 * See if it can be set to a zero block size. 7021 * If it works, ST_VARIABLE so user can set it as they want. 7022 */ 7023 un->un_dp->options |= ST_VARIABLE; 7024 un->un_dp->bsize = 0; 7025 un->un_bsize = 0; 7026 } else { 7027 un->un_dp->bsize = bsize; 7028 un->un_bsize = bsize; 7029 } 7030 7031 7032 switch (un->un_mspl->density) { 7033 default: 7034 case 0x0: 7035 /* 7036 * default density, cannot determine any other 7037 * information. 7038 */ 7039 sizestr = "Unknown type- assuming 0.25 inch cartridge"; 7040 un->un_dp->type = ST_TYPE_DEFAULT; 7041 un->un_dp->options |= (ST_AUTODEN_OVERRIDE|ST_QIC); 7042 break; 7043 case 0x1: 7044 case 0x2: 7045 case 0x3: 7046 case 0x6: 7047 /* 7048 * 1/2" reel 7049 */ 7050 sizestr = "0.50 inch reel"; 7051 un->un_dp->type = ST_TYPE_REEL; 7052 un->un_dp->options |= ST_REEL; 7053 un->un_dp->densities[0] = 0x1; 7054 un->un_dp->densities[1] = 0x2; 7055 un->un_dp->densities[2] = 0x6; 7056 un->un_dp->densities[3] = 0x3; 7057 break; 7058 case 0x4: 7059 case 0x5: 7060 case 0x7: 7061 case 0x0b: 7062 7063 /* 7064 * Quarter inch. 7065 */ 7066 sizestr = cart; 7067 un->un_dp->type = ST_TYPE_DEFAULT; 7068 un->un_dp->options |= ST_QIC; 7069 7070 un->un_dp->densities[1] = 0x4; 7071 un->un_dp->densities[2] = 0x5; 7072 un->un_dp->densities[3] = 0x7; 7073 un->un_dp->densities[0] = 0x0b; 7074 break; 7075 7076 case 0x0f: 7077 case 0x10: 7078 case 0x11: 7079 case 0x12: 7080 /* 7081 * QIC-120, QIC-150, QIC-320, QIC-600 7082 */ 7083 sizestr = cart; 7084 un->un_dp->type = ST_TYPE_DEFAULT; 7085 un->un_dp->options |= ST_QIC; 7086 un->un_dp->densities[0] = 0x0f; 7087 un->un_dp->densities[1] = 0x10; 7088 un->un_dp->densities[2] = 0x11; 7089 un->un_dp->densities[3] = 0x12; 7090 break; 7091 7092 case 0x09: 7093 case 0x0a: 7094 case 0x0c: 7095 case 0x0d: 7096 /* 7097 * 1/2" cartridge tapes. Include HI-TC. 7098 */ 7099 sizestr = cart; 7100 sizestr[2] = '5'; 7101 sizestr[3] = '0'; 7102 un->un_dp->type = ST_TYPE_HIC; 7103 un->un_dp->densities[0] = 0x09; 7104 un->un_dp->densities[1] = 0x0a; 7105 un->un_dp->densities[2] = 0x0c; 7106 un->un_dp->densities[3] = 0x0d; 7107 break; 7108 7109 case 0x13: 7110 /* DDS-2/DDS-3 scsi spec densities */ 7111 case 0x24: 7112 case 0x25: 7113 case 0x26: 7114 sizestr = "DAT Data Storage (DDS)"; 7115 un->un_dp->type = ST_TYPE_DAT; 7116 un->un_dp->options |= ST_AUTODEN_OVERRIDE; 7117 break; 7118 7119 case 0x14: 7120 /* 7121 * Helical Scan (Exabyte) devices 7122 */ 7123 sizestr = "8mm helical scan cartridge"; 7124 un->un_dp->type = ST_TYPE_EXABYTE; 7125 un->un_dp->options |= ST_AUTODEN_OVERRIDE; 7126 break; 7127 } 7128 7129 /* 7130 * Assume LONG ERASE, BSF and BSR 7131 */ 7132 7133 un->un_dp->options |= 7134 (ST_LONG_ERASE | ST_UNLOADABLE | ST_BSF | ST_BSR | ST_KNOWS_EOD); 7135 7136 /* 7137 * Only if mode sense data says no buffered write, set NOBUF 7138 */ 7139 if (un->un_mspl->bufm == 0) 7140 un->un_dp->options |= ST_NOBUF; 7141 7142 /* 7143 * set up large read and write retry counts 7144 */ 7145 7146 un->un_dp->max_rretries = un->un_dp->max_wretries = 1000; 7147 7148 /* 7149 * If this is a 0.50 inch reel tape, and 7150 * it is *not* variable mode, try and 7151 * set it to variable record length 7152 * mode. 7153 */ 7154 if ((un->un_dp->options & ST_REEL) && un->un_bsize != 0 && 7155 (un->un_dp->options & ST_VARIABLE)) { 7156 if (st_change_block_size(un, 0) == 0) { 7157 un->un_dp->bsize = 0; 7158 un->un_mspl->high_bl = un->un_mspl->mid_bl = 7159 un->un_mspl->low_bl = 0; 7160 } 7161 } 7162 7163 /* 7164 * Write to console about type of device found 7165 */ 7166 ST_DEBUG6(ST_DEVINFO, st_label, CE_NOTE, 7167 "Generic Drive, Vendor=%s\n\t%s", un->un_dp->name, 7168 sizestr); 7169 if (un->un_dp->options & ST_VARIABLE) { 7170 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 7171 "!Variable record length I/O\n"); 7172 } else { 7173 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 7174 "!Fixed record length (%d byte blocks) I/O\n", 7175 un->un_dp->bsize); 7176 } 7177 ASSERT(mutex_owned(ST_MUTEX)); 7178 return (0); 7179 } 7180 7181 static int 7182 st_determine_density(struct scsi_tape *un, int rw) 7183 { 7184 int rval = 0; 7185 7186 ST_FUNC(ST_DEVINFO, st_determine_density); 7187 7188 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 7189 "st_determine_density(un = 0x%p, rw = %s)\n", 7190 (void*)un, (rw == B_WRITE ? wr_str: rd_str)); 7191 7192 ASSERT(mutex_owned(ST_MUTEX)); 7193 7194 /* 7195 * If we're past BOT, density is determined already. 7196 */ 7197 if (un->un_pos.pmode == logical) { 7198 if (un->un_pos.lgclblkno != 0) { 7199 goto exit; 7200 } 7201 } else if (un->un_pos.pmode == legacy) { 7202 if ((un->un_pos.fileno != 0) || (un->un_pos.blkno != 0)) { 7203 /* 7204 * XXX: put in a bitch message about attempting to 7205 * XXX: change density past BOT. 7206 */ 7207 goto exit; 7208 } 7209 } else { 7210 goto exit; 7211 } 7212 if ((un->un_pos.pmode == logical) && 7213 (un->un_pos.lgclblkno != 0)) { 7214 goto exit; 7215 } 7216 7217 7218 /* 7219 * If we're going to be writing, we set the density 7220 */ 7221 if (rw == 0 || rw == B_WRITE) { 7222 /* un_curdens is used as an index into densities table */ 7223 un->un_curdens = MT_DENSITY(un->un_dev); 7224 if (st_set_density(un)) { 7225 rval = -1; 7226 } 7227 goto exit; 7228 } 7229 7230 /* 7231 * If density is known already, 7232 * we don't have to get it again.(?) 7233 */ 7234 if (!un->un_density_known) { 7235 if (st_get_density(un)) { 7236 rval = -1; 7237 } 7238 } 7239 7240 exit: 7241 ASSERT(mutex_owned(ST_MUTEX)); 7242 return (rval); 7243 } 7244 7245 7246 /* 7247 * Try to determine density. We do this by attempting to read the 7248 * first record off the tape, cycling through the available density 7249 * codes as we go. 7250 */ 7251 7252 static int 7253 st_get_density(struct scsi_tape *un) 7254 { 7255 int succes = 0, rval = -1, i; 7256 uint_t size; 7257 uchar_t dens, olddens; 7258 7259 ST_FUNC(ST_DEVINFO, st_get_density); 7260 7261 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 7262 "st_get_density(un = 0x%p)\n", (void*)un); 7263 7264 ASSERT(mutex_owned(ST_MUTEX)); 7265 7266 /* 7267 * If Auto Density override is enabled The drive has 7268 * only one density and there is no point in attempting 7269 * find the correct one. 7270 * 7271 * Since most modern drives auto detect the density 7272 * and format of the recorded media before they come 7273 * ready. What this function does is a legacy behavior 7274 * and modern drives not only don't need it, The backup 7275 * utilities that do positioning via uscsi find the un- 7276 * expected rewinds problematic. 7277 * 7278 * The drives that need this are old reel to reel devices. 7279 * I took a swag and said they must be scsi-1 or older. 7280 * I don't beleave there will any of the newer devices 7281 * that need this. There will be some scsi-1 devices that 7282 * don't need this but I don't think they will be using the 7283 * BIG aftermarket backup and restore utilitys. 7284 */ 7285 if ((un->un_dp->options & ST_AUTODEN_OVERRIDE) || 7286 (un->un_sd->sd_inq->inq_ansi > 1)) { 7287 un->un_density_known = 1; 7288 rval = 0; 7289 goto exit; 7290 } 7291 7292 /* 7293 * This will only work on variable record length tapes 7294 * if and only if all variable record length tapes autodensity 7295 * select. 7296 */ 7297 size = (unsigned)(un->un_dp->bsize ? un->un_dp->bsize : SECSIZE); 7298 un->un_tmpbuf = kmem_alloc(size, KM_SLEEP); 7299 7300 /* 7301 * Start at the specified density 7302 */ 7303 7304 dens = olddens = un->un_curdens = MT_DENSITY(un->un_dev); 7305 7306 for (i = 0; i < NDENSITIES; i++, ((un->un_curdens == NDENSITIES - 1) ? 7307 (un->un_curdens = 0) : (un->un_curdens += 1))) { 7308 /* 7309 * If we've done this density before, 7310 * don't bother to do it again. 7311 */ 7312 dens = un->un_dp->densities[un->un_curdens]; 7313 if (i > 0 && dens == olddens) 7314 continue; 7315 olddens = dens; 7316 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7317 "trying density 0x%x\n", dens); 7318 if (st_set_density(un)) { 7319 continue; 7320 } 7321 7322 /* 7323 * XXX - the creates lots of headaches and slowdowns - must 7324 * fix. 7325 */ 7326 succes = (st_cmd(un, SCMD_READ, (int)size, SYNC_CMD) == 0); 7327 if (st_cmd(un, SCMD_REWIND, 0, SYNC_CMD)) { 7328 break; 7329 } 7330 if (succes) { 7331 st_init(un); 7332 rval = 0; 7333 un->un_density_known = 1; 7334 break; 7335 } 7336 } 7337 kmem_free(un->un_tmpbuf, size); 7338 un->un_tmpbuf = 0; 7339 7340 exit: 7341 ASSERT(mutex_owned(ST_MUTEX)); 7342 return (rval); 7343 } 7344 7345 static int 7346 st_set_density(struct scsi_tape *un) 7347 { 7348 int rval = 0; 7349 7350 ST_FUNC(ST_DEVINFO, st_set_density); 7351 7352 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 7353 "st_set_density(un = 0x%p): density = 0x%x\n", (void*)un, 7354 un->un_dp->densities[un->un_curdens]); 7355 7356 ASSERT(mutex_owned(ST_MUTEX)); 7357 7358 un->un_mspl->density = un->un_dp->densities[un->un_curdens]; 7359 7360 if ((un->un_dp->options & ST_AUTODEN_OVERRIDE) == 0) { 7361 /* 7362 * If auto density override is not set, Use mode select 7363 * to set density and compression. 7364 */ 7365 if (st_modeselect(un)) { 7366 rval = -1; 7367 } 7368 } else if ((un->un_dp->options & ST_MODE_SEL_COMP) != 0) { 7369 /* 7370 * If auto density and mode select compression are set, 7371 * This is a drive with one density code but compression 7372 * can be enabled or disabled. 7373 * Set compression but no need to set density. 7374 */ 7375 rval = st_set_compression(un); 7376 if ((rval != 0) && (rval != EALREADY)) { 7377 rval = -1; 7378 } else { 7379 rval = 0; 7380 } 7381 } 7382 7383 /* If sucessful set density and/or compression, mark density known */ 7384 if (rval == 0) { 7385 un->un_density_known = 1; 7386 } 7387 7388 ASSERT(mutex_owned(ST_MUTEX)); 7389 return (rval); 7390 } 7391 7392 static int 7393 st_loadtape(struct scsi_tape *un) 7394 { 7395 int rval; 7396 7397 ST_FUNC(ST_DEVINFO, st_loadtape); 7398 7399 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 7400 "st_loadtape(un = 0x%p)\n", (void*) un); 7401 7402 ASSERT(mutex_owned(ST_MUTEX)); 7403 7404 rval = st_update_block_pos(un, st_cmd, 0); 7405 if (rval == EACCES) { 7406 return (rval); 7407 } 7408 7409 /* 7410 * 'LOAD' the tape to BOT by rewinding 7411 */ 7412 rval = st_cmd(un, SCMD_REWIND, 1, SYNC_CMD); 7413 if (rval == 0) { 7414 st_init(un); 7415 un->un_density_known = 0; 7416 } 7417 7418 ASSERT(mutex_owned(ST_MUTEX)); 7419 return (rval); 7420 } 7421 7422 7423 /* 7424 * Note: QIC devices aren't so smart. If you try to append 7425 * after EOM, the write can fail because the device doesn't know 7426 * it's at EOM. In that case, issue a read. The read should fail 7427 * because there's no data, but the device knows it's at EOM, 7428 * so a subsequent write should succeed. To further confuse matters, 7429 * the target returns the same error if the tape is positioned 7430 * such that a write would overwrite existing data. That's why 7431 * we have to do the append test. A read in the middle of 7432 * recorded data would succeed, thus indicating we're attempting 7433 * something illegal. 7434 */ 7435 7436 7437 static void 7438 st_test_append(struct buf *bp) 7439 { 7440 dev_t dev = bp->b_edev; 7441 struct scsi_tape *un; 7442 uchar_t status; 7443 unsigned bcount; 7444 7445 un = ddi_get_soft_state(st_state, MTUNIT(dev)); 7446 7447 ST_FUNC(ST_DEVINFO, st_test_append); 7448 7449 ASSERT(mutex_owned(ST_MUTEX)); 7450 7451 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 7452 "st_test_append(): fileno %d\n", un->un_pos.fileno); 7453 7454 un->un_laststate = un->un_state; 7455 un->un_state = ST_STATE_APPEND_TESTING; 7456 un->un_test_append = 0; 7457 7458 /* 7459 * first, map in the buffer, because we're doing a double write -- 7460 * first into the kernel, then onto the tape. 7461 */ 7462 bp_mapin(bp); 7463 7464 /* 7465 * get a copy of the data.... 7466 */ 7467 un->un_tmpbuf = kmem_alloc((unsigned)bp->b_bcount, KM_SLEEP); 7468 bcopy(bp->b_un.b_addr, un->un_tmpbuf, (uint_t)bp->b_bcount); 7469 7470 /* 7471 * attempt the write.. 7472 */ 7473 7474 if (st_cmd(un, (int)SCMD_WRITE, (int)bp->b_bcount, SYNC_CMD) == 0) { 7475 success: 7476 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7477 "append write succeeded\n"); 7478 bp->b_resid = un->un_sbufp->b_resid; 7479 mutex_exit(ST_MUTEX); 7480 bcount = (unsigned)bp->b_bcount; 7481 biodone(bp); 7482 mutex_enter(ST_MUTEX); 7483 un->un_laststate = un->un_state; 7484 un->un_state = ST_STATE_OPEN; 7485 kmem_free(un->un_tmpbuf, bcount); 7486 un->un_tmpbuf = NULL; 7487 return; 7488 } 7489 7490 /* 7491 * The append failed. Do a short read. If that fails, we are at EOM 7492 * so we can retry the write command. If that succeeds, than we're 7493 * all screwed up (the controller reported a real error). 7494 * 7495 * XXX: should the dummy read be > SECSIZE? should it be the device's 7496 * XXX: block size? 7497 * 7498 */ 7499 status = un->un_status; 7500 un->un_status = 0; 7501 (void) st_cmd(un, SCMD_READ, SECSIZE, SYNC_CMD); 7502 if (un->un_status == KEY_BLANK_CHECK) { 7503 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7504 "append at EOM\n"); 7505 /* 7506 * Okay- the read failed. We should actually have confused 7507 * the controller enough to allow writing. In any case, the 7508 * i/o is on its own from here on out. 7509 */ 7510 un->un_laststate = un->un_state; 7511 un->un_state = ST_STATE_OPEN; 7512 bcopy(bp->b_un.b_addr, un->un_tmpbuf, (uint_t)bp->b_bcount); 7513 if (st_cmd(un, (int)SCMD_WRITE, (int)bp->b_bcount, 7514 SYNC_CMD) == 0) { 7515 goto success; 7516 } 7517 } 7518 7519 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7520 "append write failed- not at EOM\n"); 7521 bp->b_resid = bp->b_bcount; 7522 st_bioerror(bp, EIO); 7523 7524 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 7525 "st_test_append : EIO : append write failed - not at EOM"); 7526 7527 /* 7528 * backspace one record to get back to where we were 7529 */ 7530 if (st_cmd(un, SCMD_SPACE, Blk(-1), SYNC_CMD)) { 7531 un->un_pos.pmode = invalid; 7532 } 7533 7534 un->un_err_resid = bp->b_resid; 7535 un->un_status = status; 7536 7537 /* 7538 * Note: biodone will do a bp_mapout() 7539 */ 7540 mutex_exit(ST_MUTEX); 7541 bcount = (unsigned)bp->b_bcount; 7542 biodone(bp); 7543 mutex_enter(ST_MUTEX); 7544 un->un_laststate = un->un_state; 7545 un->un_state = ST_STATE_OPEN_PENDING_IO; 7546 kmem_free(un->un_tmpbuf, bcount); 7547 un->un_tmpbuf = NULL; 7548 } 7549 7550 /* 7551 * Special command handler 7552 */ 7553 7554 /* 7555 * common st_cmd code. The fourth parameter states 7556 * whether the caller wishes to await the results 7557 * Note the release of the mutex during most of the function 7558 */ 7559 static int 7560 st_cmd(struct scsi_tape *un, int com, int64_t count, int wait) 7561 { 7562 struct buf *bp; 7563 int err; 7564 uint_t last_err_resid; 7565 7566 ST_FUNC(ST_DEVINFO, st_cmd); 7567 7568 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 7569 "st_cmd(dev = 0x%lx, com = 0x%x, count = %"PRIx64", wait = %d)\n", 7570 un->un_dev, com, count, wait); 7571 7572 ASSERT(MUTEX_HELD(&un->un_sd->sd_mutex)); 7573 ASSERT(mutex_owned(ST_MUTEX)); 7574 7575 #ifdef STDEBUG 7576 if ((st_debug & 0x7)) { 7577 st_debug_cmds(un, com, count, wait); 7578 } 7579 #endif 7580 7581 st_wait_for_io(un); 7582 7583 /* check to see if this command requires the drive to be reserved */ 7584 err = st_check_cmd_for_need_to_reserve(un, com, count); 7585 7586 if (err) { 7587 return (err); 7588 } 7589 7590 /* 7591 * A space command is not recoverable if we don't know were we 7592 * were when it was issued. 7593 */ 7594 if ((com == SCMD_SPACE) || (com == SCMD_SPACE_G4)) { 7595 (void) st_update_block_pos(un, st_cmd, 0); 7596 } 7597 7598 /* 7599 * Forground should not be doing anything while recovery is active. 7600 */ 7601 ASSERT(un->un_recov_buf_busy == 0); 7602 7603 while (un->un_sbuf_busy) 7604 cv_wait(&un->un_sbuf_cv, ST_MUTEX); 7605 un->un_sbuf_busy = 1; 7606 7607 bp = un->un_sbufp; 7608 bzero(bp, sizeof (buf_t)); 7609 7610 bp->b_flags = (wait) ? B_BUSY : B_BUSY|B_ASYNC; 7611 7612 err = st_setup_cmd(un, bp, com, count); 7613 7614 un->un_sbuf_busy = 0; 7615 7616 /* 7617 * If was a space command need to update logical block position. 7618 * Only do this if the command was sucessful or it will mask the fact 7619 * that the space command failed by promoting the pmode to logical. 7620 */ 7621 if (((com == SCMD_SPACE) || (com == SCMD_SPACE_G4)) && 7622 (un->un_pos.pmode != invalid)) { 7623 un->un_running.pmode = invalid; 7624 last_err_resid = un->un_err_resid; 7625 (void) st_update_block_pos(un, st_cmd, 1); 7626 /* 7627 * Set running position to invalid so it updates on the 7628 * next command. 7629 */ 7630 un->un_running.pmode = invalid; 7631 un->un_err_resid = last_err_resid; 7632 } 7633 7634 cv_signal(&un->un_sbuf_cv); 7635 7636 return (err); 7637 } 7638 7639 static int 7640 st_setup_cmd(struct scsi_tape *un, buf_t *bp, int com, int64_t count) 7641 { 7642 int err; 7643 dev_t dev = un->un_dev; 7644 7645 ST_FUNC(ST_DEVINFO, st_setup_cmd); 7646 /* 7647 * Set count to the actual size of the data tranfer. 7648 * For commands with no data transfer, set bp->b_bcount 7649 * to the value to be used when constructing the 7650 * cdb in st_make_cmd(). 7651 */ 7652 switch (com) { 7653 case SCMD_READ: 7654 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7655 "special read %"PRId64"\n", count); 7656 bp->b_flags |= B_READ; 7657 bp->b_un.b_addr = un->un_tmpbuf; 7658 break; 7659 7660 case SCMD_WRITE: 7661 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7662 "special write %"PRId64"\n", count); 7663 bp->b_un.b_addr = un->un_tmpbuf; 7664 break; 7665 7666 case SCMD_WRITE_FILE_MARK: 7667 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7668 "write %"PRId64" file marks\n", count); 7669 bp->b_bcount = count; 7670 count = 0; 7671 break; 7672 7673 case SCMD_REWIND: 7674 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "rewind\n"); 7675 bp->b_bcount = count; 7676 count = 0; 7677 break; 7678 7679 case SCMD_SPACE: 7680 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "space\n"); 7681 /* 7682 * If the user could have entered a number that will 7683 * not fit in the 12 bit count field of space(8), 7684 * use space(16). 7685 */ 7686 if (((int64_t)SPACE_CNT(count) > 0x7fffff) || 7687 ((int64_t)SPACE_CNT(count) < -(0x7fffff))) { 7688 com = SCMD_SPACE_G4; 7689 } 7690 bp->b_bcount = count; 7691 count = 0; 7692 break; 7693 7694 case SCMD_RESERVE: 7695 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "reserve"); 7696 bp->b_bcount = 0; 7697 count = 0; 7698 break; 7699 7700 case SCMD_RELEASE: 7701 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "release"); 7702 bp->b_bcount = 0; 7703 count = 0; 7704 break; 7705 7706 case SCMD_LOAD: 7707 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7708 "%s tape\n", (count & LD_LOAD) ? "load" : "unload"); 7709 bp->b_bcount = count; 7710 count = 0; 7711 break; 7712 7713 case SCMD_ERASE: 7714 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7715 "erase tape\n"); 7716 bp->b_bcount = count; 7717 count = 0; 7718 break; 7719 7720 case SCMD_MODE_SENSE: 7721 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7722 "mode sense\n"); 7723 bp->b_flags |= B_READ; 7724 bp->b_un.b_addr = (caddr_t)(un->un_mspl); 7725 break; 7726 7727 case SCMD_MODE_SELECT: 7728 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7729 "mode select\n"); 7730 bp->b_un.b_addr = (caddr_t)(un->un_mspl); 7731 break; 7732 7733 case SCMD_READ_BLKLIM: 7734 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7735 "read block limits\n"); 7736 bp->b_bcount = count; 7737 bp->b_flags |= B_READ; 7738 bp->b_un.b_addr = (caddr_t)(un->un_rbl); 7739 break; 7740 7741 case SCMD_TEST_UNIT_READY: 7742 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7743 "test unit ready\n"); 7744 bp->b_bcount = 0; 7745 count = 0; 7746 break; 7747 7748 case SCMD_DOORLOCK: 7749 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7750 "%s tape\n", (count & MR_LOCK) ? "lock" : "unlock"); 7751 bp->b_bcount = count = 0; 7752 break; 7753 7754 case SCMD_READ_POSITION: 7755 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7756 "read position\n"); 7757 switch (un->un_read_pos_type) { 7758 case LONG_POS: 7759 count = sizeof (tape_position_long_t); 7760 break; 7761 case EXT_POS: 7762 count = min(count, sizeof (tape_position_ext_t)); 7763 break; 7764 case SHORT_POS: 7765 count = sizeof (tape_position_t); 7766 break; 7767 default: 7768 ST_DEBUG(ST_DEVINFO, st_label, CE_PANIC, 7769 "Unknown read position type 0x%x in " 7770 "st_make_cmd()\n", un->un_read_pos_type); 7771 } 7772 bp->b_bcount = count; 7773 bp->b_flags |= B_READ; 7774 bp->b_un.b_addr = (caddr_t)un->un_read_pos_data; 7775 break; 7776 7777 default: 7778 ST_DEBUG(ST_DEVINFO, st_label, CE_PANIC, 7779 "Unhandled scsi command 0x%x in st_setup_cmd()\n", com); 7780 } 7781 7782 mutex_exit(ST_MUTEX); 7783 7784 if (count > 0) { 7785 int flg = (bp->b_flags & B_READ) ? B_READ : B_WRITE; 7786 /* 7787 * We're going to do actual I/O. 7788 * Set things up for physio. 7789 */ 7790 struct iovec aiov; 7791 struct uio auio; 7792 struct uio *uio = &auio; 7793 7794 bzero(&auio, sizeof (struct uio)); 7795 bzero(&aiov, sizeof (struct iovec)); 7796 aiov.iov_base = bp->b_un.b_addr; 7797 aiov.iov_len = count; 7798 7799 uio->uio_iov = &aiov; 7800 uio->uio_iovcnt = 1; 7801 uio->uio_resid = aiov.iov_len; 7802 uio->uio_segflg = UIO_SYSSPACE; 7803 7804 /* 7805 * Let physio do the rest... 7806 */ 7807 bp->b_forw = (struct buf *)(uintptr_t)com; 7808 bp->b_back = NULL; 7809 err = physio(st_strategy, bp, dev, flg, st_minphys, uio); 7810 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7811 "st_setup_cmd: physio returns %d\n", err); 7812 } else { 7813 /* 7814 * Mimic physio 7815 */ 7816 bp->b_forw = (struct buf *)(uintptr_t)com; 7817 bp->b_back = NULL; 7818 bp->b_edev = dev; 7819 bp->b_dev = cmpdev(dev); 7820 bp->b_blkno = 0; 7821 bp->b_resid = 0; 7822 (void) st_strategy(bp); 7823 if (bp->b_flags & B_ASYNC) { 7824 /* 7825 * This is an async command- the caller won't wait 7826 * and doesn't care about errors. 7827 */ 7828 mutex_enter(ST_MUTEX); 7829 return (0); 7830 } 7831 7832 /* 7833 * BugTraq #4260046 7834 * ---------------- 7835 * Restore Solaris 2.5.1 behavior, namely call biowait 7836 * unconditionally. The old comment said... 7837 * 7838 * "if strategy was flagged with persistent errors, we would 7839 * have an error here, and the bp would never be sent, so we 7840 * don't want to wait on a bp that was never sent...or hang" 7841 * 7842 * The new rationale, courtesy of Chitrank... 7843 * 7844 * "we should unconditionally biowait() here because 7845 * st_strategy() will do a biodone() in the persistent error 7846 * case and the following biowait() will return immediately. 7847 * If not, in the case of "errors after pkt alloc" in 7848 * st_start(), we will not biowait here which will cause the 7849 * next biowait() to return immediately which will cause 7850 * us to send out the next command. In the case where both of 7851 * these use the sbuf, when the first command completes we'll 7852 * free the packet attached to sbuf and the same pkt will 7853 * get freed again when we complete the second command. 7854 * see esc 518987. BTW, it is necessary to do biodone() in 7855 * st_start() for the pkt alloc failure case because physio() 7856 * does biowait() and will hang if we don't do biodone()" 7857 */ 7858 7859 err = biowait(bp); 7860 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7861 "st_setup_cmd: biowait returns %d\n", err); 7862 } 7863 7864 mutex_enter(ST_MUTEX); 7865 7866 return (err); 7867 } 7868 7869 static int 7870 st_set_compression(struct scsi_tape *un) 7871 { 7872 int rval; 7873 int turn_compression_on; 7874 minor_t minor; 7875 7876 ST_FUNC(ST_DEVINFO, st_set_compression); 7877 7878 /* 7879 * Drive either dosn't have compression or it is controlled with 7880 * special density codes. Return ENOTTY so caller 7881 * knows nothing was done. 7882 */ 7883 if ((un->un_dp->options & ST_MODE_SEL_COMP) == 0) { 7884 un->un_comp_page = 0; 7885 return (ENOTTY); 7886 } 7887 7888 /* set compression based on minor node opened */ 7889 minor = MT_DENSITY(un->un_dev); 7890 7891 /* 7892 * If this the compression density or 7893 * the drive has two densities and uses mode select for 7894 * control of compression turn on compression for MT_DENSITY2 7895 * as well. 7896 */ 7897 if ((minor == ST_COMPRESSION_DENSITY) || 7898 (minor == MT_DENSITY(MT_DENSITY2)) && 7899 (un->un_dp->densities[0] == un->un_dp->densities[1]) && 7900 (un->un_dp->densities[2] == un->un_dp->densities[3]) && 7901 (un->un_dp->densities[0] != un->un_dp->densities[2])) { 7902 7903 turn_compression_on = 1; 7904 } else { 7905 turn_compression_on = 0; 7906 } 7907 7908 un->un_mspl->high_bl = (uchar_t)(un->un_bsize >> 16); 7909 un->un_mspl->mid_bl = (uchar_t)(un->un_bsize >> 8); 7910 un->un_mspl->low_bl = (uchar_t)(un->un_bsize); 7911 7912 /* 7913 * Need to determine which page does the device use for compression. 7914 * First try the data compression page. If this fails try the device 7915 * configuration page 7916 */ 7917 7918 if ((un->un_comp_page & ST_DEV_DATACOMP_PAGE) == ST_DEV_DATACOMP_PAGE) { 7919 rval = st_set_datacomp_page(un, turn_compression_on); 7920 if (rval == EALREADY) { 7921 return (rval); 7922 } 7923 if (rval != 0) { 7924 if (un->un_status == KEY_ILLEGAL_REQUEST) { 7925 /* 7926 * This device does not support data 7927 * compression page 7928 */ 7929 un->un_comp_page = ST_DEV_CONFIG_PAGE; 7930 } else if (un->un_state >= ST_STATE_OPEN) { 7931 un->un_pos.pmode = invalid; 7932 rval = EIO; 7933 } else { 7934 rval = -1; 7935 } 7936 } else { 7937 un->un_comp_page = ST_DEV_DATACOMP_PAGE; 7938 } 7939 } 7940 7941 if ((un->un_comp_page & ST_DEV_CONFIG_PAGE) == ST_DEV_CONFIG_PAGE) { 7942 rval = st_set_devconfig_page(un, turn_compression_on); 7943 if (rval == EALREADY) { 7944 return (rval); 7945 } 7946 if (rval != 0) { 7947 if (un->un_status == KEY_ILLEGAL_REQUEST) { 7948 /* 7949 * This device does not support 7950 * compression at all advice the 7951 * user and unset ST_MODE_SEL_COMP 7952 */ 7953 un->un_dp->options &= ~ST_MODE_SEL_COMP; 7954 un->un_comp_page = 0; 7955 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 7956 "Device Does Not Support Compression\n"); 7957 } else if (un->un_state >= ST_STATE_OPEN) { 7958 un->un_pos.pmode = invalid; 7959 rval = EIO; 7960 } else { 7961 rval = -1; 7962 } 7963 } 7964 } 7965 7966 return (rval); 7967 } 7968 7969 /* 7970 * set or unset compression thru device configuration page. 7971 */ 7972 static int 7973 st_set_devconfig_page(struct scsi_tape *un, int compression_on) 7974 { 7975 unsigned char cflag; 7976 int rval = 0; 7977 7978 7979 ST_FUNC(ST_DEVINFO, st_set_devconfig_page); 7980 7981 ASSERT(mutex_owned(ST_MUTEX)); 7982 /* 7983 * Figure what to set compression flag to. 7984 */ 7985 if (compression_on) { 7986 /* They have selected a compression node */ 7987 if (un->un_dp->type == ST_TYPE_FUJI) { 7988 cflag = 0x84; /* use EDRC */ 7989 } else { 7990 cflag = ST_DEV_CONFIG_DEF_COMP; 7991 } 7992 } else { 7993 cflag = ST_DEV_CONFIG_NO_COMP; 7994 } 7995 7996 /* 7997 * If compression is already set the way it was requested. 7998 * And if this not the first time we has tried. 7999 */ 8000 if ((cflag == un->un_mspl->page.dev.comp_alg) && 8001 (un->un_comp_page == ST_DEV_DATACOMP_PAGE)) { 8002 return (EALREADY); 8003 } 8004 8005 un->un_mspl->page.dev.comp_alg = cflag; 8006 /* 8007 * need to send mode select even if correct compression is 8008 * already set since need to set density code 8009 */ 8010 8011 #ifdef STDEBUG 8012 if ((st_debug & 0x7) >= 6) { 8013 st_clean_print(ST_DEVINFO, st_label, SCSI_DEBUG, 8014 "st_set_devconfig_page: sense data for mode select", 8015 (char *)un->un_mspl, sizeof (struct seq_mode)); 8016 } 8017 #endif 8018 rval = st_gen_mode_select(un, st_uscsi_cmd, un->un_mspl, 8019 sizeof (struct seq_mode)); 8020 8021 return (rval); 8022 } 8023 8024 /* 8025 * set/reset compression bit thru data compression page 8026 */ 8027 static int 8028 st_set_datacomp_page(struct scsi_tape *un, int compression_on) 8029 { 8030 int compression_on_already; 8031 int rval = 0; 8032 8033 8034 ST_FUNC(ST_DEVINFO, st_set_datacomp_page); 8035 8036 ASSERT(mutex_owned(ST_MUTEX)); 8037 /* 8038 * If drive is not capable of compression (at this time) 8039 * return EALREADY so caller doesn't think that this page 8040 * is not supported. This check is for drives that can 8041 * disable compression from the front panel or configuration. 8042 * I doubt that a drive that supports this page is not really 8043 * capable of compression. 8044 */ 8045 if (un->un_mspl->page.comp.dcc == 0) { 8046 return (EALREADY); 8047 } 8048 8049 /* See if compression currently turned on */ 8050 if (un->un_mspl->page.comp.dce) { 8051 compression_on_already = 1; 8052 } else { 8053 compression_on_already = 0; 8054 } 8055 8056 /* 8057 * If compression is already set the way it was requested. 8058 * And if this not the first time we has tried. 8059 */ 8060 if ((compression_on == compression_on_already) && 8061 (un->un_comp_page == ST_DEV_DATACOMP_PAGE)) { 8062 return (EALREADY); 8063 } 8064 8065 /* 8066 * if we are already set to the appropriate compression 8067 * mode, don't set it again 8068 */ 8069 if (compression_on) { 8070 /* compression selected */ 8071 un->un_mspl->page.comp.dce = 1; 8072 } else { 8073 un->un_mspl->page.comp.dce = 0; 8074 } 8075 8076 8077 #ifdef STDEBUG 8078 if ((st_debug & 0x7) >= 6) { 8079 st_clean_print(ST_DEVINFO, st_label, SCSI_DEBUG, 8080 "st_set_datacomp_page: sense data for mode select", 8081 (char *)un->un_mspl, sizeof (struct seq_mode)); 8082 } 8083 #endif 8084 rval = st_gen_mode_select(un, st_uscsi_cmd, un->un_mspl, 8085 sizeof (struct seq_mode)); 8086 8087 return (rval); 8088 } 8089 8090 static int 8091 st_modesense(struct scsi_tape *un) 8092 { 8093 int rval; 8094 uchar_t page; 8095 8096 ST_FUNC(ST_DEVINFO, st_modesense); 8097 8098 page = un->un_comp_page; 8099 8100 switch (page) { 8101 case ST_DEV_DATACOMP_PAGE: 8102 case ST_DEV_CONFIG_PAGE: /* FALLTHROUGH */ 8103 rval = st_gen_mode_sense(un, st_uscsi_cmd, page, un->un_mspl, 8104 sizeof (struct seq_mode)); 8105 break; 8106 8107 case ST_DEV_DATACOMP_PAGE | ST_DEV_CONFIG_PAGE: 8108 if (un->un_dp->options & ST_MODE_SEL_COMP) { 8109 page = ST_DEV_CONFIG_PAGE; 8110 rval = st_gen_mode_sense(un, st_uscsi_cmd, page, 8111 un->un_mspl, sizeof (struct seq_mode)); 8112 if (rval == 0 && un->un_mspl->page_code == page) { 8113 un->un_comp_page = page; 8114 break; 8115 } 8116 page = ST_DEV_DATACOMP_PAGE; 8117 rval = st_gen_mode_sense(un, st_uscsi_cmd, page, 8118 un->un_mspl, sizeof (struct seq_mode)); 8119 if (rval == 0 && un->un_mspl->page_code == page) { 8120 un->un_comp_page = page; 8121 break; 8122 } 8123 un->un_dp->options &= ~ST_MODE_SEL_COMP; 8124 un->un_comp_page = 0; 8125 } else { 8126 un->un_comp_page = 0; 8127 } 8128 8129 default: /* FALLTHROUGH */ 8130 rval = st_cmd(un, SCMD_MODE_SENSE, MSIZE, SYNC_CMD); 8131 } 8132 return (rval); 8133 } 8134 8135 static int 8136 st_modeselect(struct scsi_tape *un) 8137 { 8138 int rval = 0; 8139 int ix; 8140 8141 ST_FUNC(ST_DEVINFO, st_modeselect); 8142 8143 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 8144 "st_modeselect(dev = 0x%lx): density = 0x%x\n", 8145 un->un_dev, un->un_mspl->density); 8146 8147 ASSERT(mutex_owned(ST_MUTEX)); 8148 8149 /* 8150 * The parameter list should be the same for all of the 8151 * cases that follow so set them here 8152 * 8153 * Try mode select first if if fails set fields manually 8154 */ 8155 rval = st_modesense(un); 8156 if (rval != 0) { 8157 ST_DEBUG3(ST_DEVINFO, st_label, CE_WARN, 8158 "st_modeselect: First mode sense failed\n"); 8159 un->un_mspl->bd_len = 8; 8160 un->un_mspl->high_nb = 0; 8161 un->un_mspl->mid_nb = 0; 8162 un->un_mspl->low_nb = 0; 8163 } 8164 un->un_mspl->high_bl = (uchar_t)(un->un_bsize >> 16); 8165 un->un_mspl->mid_bl = (uchar_t)(un->un_bsize >> 8); 8166 un->un_mspl->low_bl = (uchar_t)(un->un_bsize); 8167 8168 8169 /* 8170 * If configured to use a specific density code for a media type. 8171 * curdens is previously set by the minor node opened. 8172 * If the media type doesn't match the minor node we change it so it 8173 * looks like the correct one was opened. 8174 */ 8175 if (un->un_dp->options & ST_KNOWS_MEDIA) { 8176 uchar_t best; 8177 8178 for (best = 0xff, ix = 0; ix < NDENSITIES; ix++) { 8179 if (un->un_mspl->media_type == 8180 un->un_dp->mediatype[ix]) { 8181 best = ix; 8182 /* 8183 * It matches but it might not be the only one. 8184 * Use the highest matching media type but not 8185 * to exceed the density selected by the open. 8186 */ 8187 if (ix < un->un_curdens) { 8188 continue; 8189 } 8190 un->un_curdens = ix; 8191 break; 8192 } 8193 } 8194 /* If a match was found best will not be 0xff any more */ 8195 if (best < NDENSITIES) { 8196 ST_DEBUG3(ST_DEVINFO, st_label, CE_WARN, 8197 "found media 0x%X using density 0x%X\n", 8198 un->un_mspl->media_type, 8199 un->un_dp->densities[best]); 8200 un->un_mspl->density = un->un_dp->densities[best]; 8201 } else { 8202 /* Otherwise set density based on minor node opened */ 8203 un->un_mspl->density = 8204 un->un_dp->densities[un->un_curdens]; 8205 } 8206 } else { 8207 un->un_mspl->density = un->un_dp->densities[un->un_curdens]; 8208 } 8209 8210 if (un->un_dp->options & ST_NOBUF) { 8211 un->un_mspl->bufm = 0; 8212 } else { 8213 un->un_mspl->bufm = 1; 8214 } 8215 8216 rval = st_set_compression(un); 8217 8218 /* 8219 * If st_set_compression returned invalid or already it 8220 * found no need to do the mode select. 8221 * So do it here. 8222 */ 8223 if ((rval == ENOTTY) || (rval == EALREADY)) { 8224 8225 /* Zero non-writeable fields */ 8226 un->un_mspl->data_len = 0; 8227 un->un_mspl->media_type = 0; 8228 un->un_mspl->wp = 0; 8229 8230 /* need to set the density code */ 8231 rval = st_cmd(un, SCMD_MODE_SELECT, MSIZE, SYNC_CMD); 8232 if (rval != 0) { 8233 if (un->un_state >= ST_STATE_OPEN) { 8234 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 8235 "unable to set tape mode\n"); 8236 un->un_pos.pmode = invalid; 8237 rval = EIO; 8238 } else { 8239 rval = -1; 8240 } 8241 } 8242 } 8243 8244 /* 8245 * The spec recommends to send a mode sense after a mode select 8246 */ 8247 (void) st_modesense(un); 8248 8249 ASSERT(mutex_owned(ST_MUTEX)); 8250 8251 return (rval); 8252 } 8253 8254 /* 8255 * st_gen_mode_sense 8256 * 8257 * generic mode sense.. it allows for any page 8258 */ 8259 static int 8260 st_gen_mode_sense(struct scsi_tape *un, ubufunc_t ubf, int page, 8261 struct seq_mode *page_data, int page_size) 8262 { 8263 8264 int r; 8265 char cdb[CDB_GROUP0]; 8266 struct uscsi_cmd *com; 8267 struct scsi_arq_status status; 8268 8269 ST_FUNC(ST_DEVINFO, st_gen_mode_sense); 8270 8271 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 8272 8273 bzero(cdb, CDB_GROUP0); 8274 cdb[0] = SCMD_MODE_SENSE; 8275 cdb[2] = (char)page; 8276 cdb[4] = (char)page_size; 8277 8278 com->uscsi_cdb = cdb; 8279 com->uscsi_cdblen = CDB_GROUP0; 8280 com->uscsi_bufaddr = (caddr_t)page_data; 8281 com->uscsi_buflen = page_size; 8282 com->uscsi_rqlen = sizeof (status); 8283 com->uscsi_rqbuf = (caddr_t)&status; 8284 com->uscsi_timeout = un->un_dp->non_motion_timeout; 8285 com->uscsi_flags = USCSI_DIAGNOSE | USCSI_RQENABLE | USCSI_READ; 8286 8287 r = ubf(un, com, FKIOCTL); 8288 kmem_free(com, sizeof (*com)); 8289 return (r); 8290 } 8291 8292 /* 8293 * st_gen_mode_select 8294 * 8295 * generic mode select.. it allows for any page 8296 */ 8297 static int 8298 st_gen_mode_select(struct scsi_tape *un, ubufunc_t ubf, 8299 struct seq_mode *page_data, int page_size) 8300 { 8301 8302 int r; 8303 char cdb[CDB_GROUP0]; 8304 struct uscsi_cmd *com; 8305 struct scsi_arq_status status; 8306 8307 ST_FUNC(ST_DEVINFO, st_gen_mode_select); 8308 8309 /* Zero non-writeable fields */ 8310 page_data->data_len = 0; 8311 page_data->media_type = 0; 8312 page_data->wp = 0; 8313 8314 /* 8315 * If mode select has any page data, zero the ps (Page Savable) bit. 8316 */ 8317 if (page_size > MSIZE) { 8318 page_data->ps = 0; 8319 } 8320 8321 8322 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 8323 8324 /* 8325 * then, do a mode select to set what ever info 8326 */ 8327 bzero(cdb, CDB_GROUP0); 8328 cdb[0] = SCMD_MODE_SELECT; 8329 cdb[1] = 0x10; /* set PF bit for many third party drives */ 8330 cdb[4] = (char)page_size; 8331 8332 com->uscsi_cdb = cdb; 8333 com->uscsi_cdblen = CDB_GROUP0; 8334 com->uscsi_bufaddr = (caddr_t)page_data; 8335 com->uscsi_buflen = page_size; 8336 com->uscsi_rqlen = sizeof (status); 8337 com->uscsi_rqbuf = (caddr_t)&status; 8338 com->uscsi_timeout = un->un_dp->non_motion_timeout; 8339 com->uscsi_flags = USCSI_DIAGNOSE | USCSI_RQENABLE | USCSI_WRITE; 8340 8341 r = ubf(un, com, FKIOCTL); 8342 8343 kmem_free(com, sizeof (*com)); 8344 return (r); 8345 } 8346 8347 static int 8348 st_read_block_limits(struct scsi_tape *un, struct read_blklim *read_blk) 8349 { 8350 int rval; 8351 char cdb[CDB_GROUP0]; 8352 struct uscsi_cmd *com; 8353 struct scsi_arq_status status; 8354 8355 ST_FUNC(ST_DEVINFO, st_read_block_limits); 8356 8357 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 8358 8359 bzero(cdb, CDB_GROUP0); 8360 cdb[0] = SCMD_READ_BLKLIM; 8361 8362 com->uscsi_cdb = cdb; 8363 com->uscsi_cdblen = CDB_GROUP0; 8364 com->uscsi_bufaddr = (caddr_t)read_blk; 8365 com->uscsi_buflen = sizeof (struct read_blklim); 8366 com->uscsi_rqlen = sizeof (status); 8367 com->uscsi_rqbuf = (caddr_t)&status; 8368 com->uscsi_timeout = un->un_dp->non_motion_timeout; 8369 com->uscsi_flags = USCSI_DIAGNOSE | USCSI_RQENABLE | USCSI_READ; 8370 8371 rval = st_uscsi_cmd(un, com, FKIOCTL); 8372 if (com->uscsi_status || com->uscsi_resid) { 8373 rval = -1; 8374 } 8375 8376 kmem_free(com, sizeof (*com)); 8377 return (rval); 8378 } 8379 8380 static int 8381 st_report_density_support(struct scsi_tape *un, uchar_t *density_data, 8382 size_t buflen) 8383 { 8384 int rval; 8385 char cdb[CDB_GROUP1]; 8386 struct uscsi_cmd *com; 8387 struct scsi_arq_status status; 8388 8389 ST_FUNC(ST_DEVINFO, st_report_density_support); 8390 8391 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 8392 8393 bzero(cdb, CDB_GROUP1); 8394 cdb[0] = SCMD_REPORT_DENSITIES; 8395 cdb[7] = (buflen & 0xff00) >> 8; 8396 cdb[8] = buflen & 0xff; 8397 8398 com->uscsi_cdb = cdb; 8399 com->uscsi_cdblen = CDB_GROUP1; 8400 com->uscsi_bufaddr = (caddr_t)density_data; 8401 com->uscsi_buflen = buflen; 8402 com->uscsi_rqlen = sizeof (status); 8403 com->uscsi_rqbuf = (caddr_t)&status; 8404 com->uscsi_timeout = un->un_dp->non_motion_timeout; 8405 com->uscsi_flags = USCSI_DIAGNOSE | USCSI_RQENABLE | USCSI_READ; 8406 8407 rval = st_uscsi_cmd(un, com, FKIOCTL); 8408 if (com->uscsi_status || com->uscsi_resid) { 8409 rval = -1; 8410 } 8411 8412 kmem_free(com, sizeof (*com)); 8413 return (rval); 8414 } 8415 8416 static int 8417 st_report_supported_operation(struct scsi_tape *un, uchar_t *oper_data, 8418 uchar_t option_code, ushort_t service_action) 8419 { 8420 int rval; 8421 char cdb[CDB_GROUP5]; 8422 struct uscsi_cmd *com; 8423 struct scsi_arq_status status; 8424 uint32_t allo_length; 8425 8426 ST_FUNC(ST_DEVINFO, st_report_supported_operation); 8427 8428 allo_length = sizeof (struct one_com_des) + 8429 sizeof (struct com_timeout_des); 8430 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 8431 8432 bzero(cdb, CDB_GROUP5); 8433 cdb[0] = (char)SCMD_MAINTENANCE_IN; 8434 cdb[1] = SSVC_ACTION_GET_SUPPORTED_OPERATIONS; 8435 if (service_action) { 8436 cdb[2] = (char)(ONE_COMMAND_DATA_FORMAT | 0x80); /* RCTD */ 8437 cdb[4] = (service_action & 0xff00) >> 8; 8438 cdb[5] = service_action & 0xff; 8439 } else { 8440 cdb[2] = (char)(ONE_COMMAND_NO_SERVICE_DATA_FORMAT | 8441 0x80); /* RCTD */ 8442 } 8443 cdb[3] = option_code; 8444 cdb[6] = (allo_length & 0xff000000) >> 24; 8445 cdb[7] = (allo_length & 0xff0000) >> 16; 8446 cdb[8] = (allo_length & 0xff00) >> 8; 8447 cdb[9] = allo_length & 0xff; 8448 8449 com->uscsi_cdb = cdb; 8450 com->uscsi_cdblen = CDB_GROUP5; 8451 com->uscsi_bufaddr = (caddr_t)oper_data; 8452 com->uscsi_buflen = allo_length; 8453 com->uscsi_rqlen = sizeof (status); 8454 com->uscsi_rqbuf = (caddr_t)&status; 8455 com->uscsi_timeout = un->un_dp->non_motion_timeout; 8456 com->uscsi_flags = USCSI_DIAGNOSE | USCSI_RQENABLE | USCSI_READ; 8457 8458 rval = st_uscsi_cmd(un, com, FKIOCTL); 8459 if (com->uscsi_status) { 8460 rval = -1; 8461 } 8462 8463 kmem_free(com, sizeof (*com)); 8464 return (rval); 8465 } 8466 8467 /* 8468 * Changes devices blocksize and bsize to requested blocksize nblksz. 8469 * Returns returned value from first failed call or zero on success. 8470 */ 8471 static int 8472 st_change_block_size(struct scsi_tape *un, uint32_t nblksz) 8473 { 8474 struct seq_mode *current; 8475 int rval; 8476 uint32_t oldblksz; 8477 8478 ST_FUNC(ST_DEVINFO, st_change_block_size); 8479 8480 current = kmem_zalloc(MSIZE, KM_SLEEP); 8481 8482 /* 8483 * If we haven't got the compression page yet, do that first. 8484 */ 8485 if (un->un_comp_page == (ST_DEV_DATACOMP_PAGE | ST_DEV_CONFIG_PAGE)) { 8486 (void) st_modesense(un); 8487 } 8488 8489 /* Read current settings */ 8490 rval = st_gen_mode_sense(un, st_uscsi_cmd, 0, current, MSIZE); 8491 if (rval != 0) { 8492 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 8493 "mode sense for change block size failed: rval = %d", rval); 8494 goto finish; 8495 } 8496 8497 /* Figure the current block size */ 8498 oldblksz = 8499 (current->high_bl << 16) | 8500 (current->mid_bl << 8) | 8501 (current->low_bl); 8502 8503 /* If current block size is the same as requested were done */ 8504 if (oldblksz == nblksz) { 8505 un->un_bsize = nblksz; 8506 rval = 0; 8507 goto finish; 8508 } 8509 8510 /* Change to requested block size */ 8511 current->high_bl = (uchar_t)(nblksz >> 16); 8512 current->mid_bl = (uchar_t)(nblksz >> 8); 8513 current->low_bl = (uchar_t)(nblksz); 8514 8515 /* Attempt to change block size */ 8516 rval = st_gen_mode_select(un, st_uscsi_cmd, current, MSIZE); 8517 if (rval != 0) { 8518 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 8519 "Set new block size failed: rval = %d", rval); 8520 goto finish; 8521 } 8522 8523 /* Read back and verify setting */ 8524 rval = st_modesense(un); 8525 if (rval == 0) { 8526 un->un_bsize = 8527 (un->un_mspl->high_bl << 16) | 8528 (un->un_mspl->mid_bl << 8) | 8529 (un->un_mspl->low_bl); 8530 8531 if (un->un_bsize != nblksz) { 8532 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 8533 "Blocksize set does not equal requested blocksize" 8534 "(read: %u requested: %u)\n", nblksz, un->un_bsize); 8535 rval = EIO; 8536 } 8537 } 8538 finish: 8539 kmem_free(current, MSIZE); 8540 return (rval); 8541 } 8542 8543 8544 static void 8545 st_init(struct scsi_tape *un) 8546 { 8547 ST_FUNC(ST_DEVINFO, st_init); 8548 8549 ASSERT(mutex_owned(ST_MUTEX)); 8550 8551 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 8552 "st_init(): dev = 0x%lx, will reset fileno, blkno, eof\n", 8553 un->un_dev); 8554 8555 un->un_pos.blkno = 0; 8556 un->un_pos.fileno = 0; 8557 un->un_lastop = ST_OP_NIL; 8558 un->un_pos.eof = ST_NO_EOF; 8559 un->un_pwr_mgmt = ST_PWR_NORMAL; 8560 if (st_error_level != SCSI_ERR_ALL) { 8561 if (DEBUGGING) { 8562 st_error_level = SCSI_ERR_ALL; 8563 } else { 8564 st_error_level = SCSI_ERR_RETRYABLE; 8565 } 8566 } 8567 } 8568 8569 8570 static void 8571 st_make_cmd(struct scsi_tape *un, struct buf *bp, int (*func)(caddr_t)) 8572 { 8573 struct scsi_pkt *pkt; 8574 struct uscsi_cmd *ucmd; 8575 recov_info *ri; 8576 int tval = 0; 8577 int64_t count; 8578 uint32_t additional = 0; 8579 uint32_t address = 0; 8580 union scsi_cdb *ucdb; 8581 int flags = 0; 8582 int cdb_len = CDB_GROUP0; /* default */ 8583 uchar_t com; 8584 char fixbit; 8585 char short_fm = 0; 8586 optype prev_op = un->un_lastop; 8587 int stat_size = 8588 (un->un_arq_enabled ? sizeof (struct scsi_arq_status) : 1); 8589 8590 ST_FUNC(ST_DEVINFO, st_make_cmd); 8591 8592 ASSERT(mutex_owned(ST_MUTEX)); 8593 8594 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 8595 "st_make_cmd(): dev = 0x%lx\n", un->un_dev); 8596 8597 8598 /* 8599 * fixbit is for setting the Fixed Mode and Suppress Incorrect 8600 * Length Indicator bits on read/write commands, for setting 8601 * the Long bit on erase commands, and for setting the Code 8602 * Field bits on space commands. 8603 */ 8604 8605 /* regular raw I/O */ 8606 if ((bp != un->un_sbufp) && (bp != un->un_recov_buf)) { 8607 pkt = scsi_init_pkt(ROUTE, NULL, bp, 8608 CDB_GROUP0, stat_size, st_recov_sz, 0, func, 8609 (caddr_t)un); 8610 if (pkt == NULL) { 8611 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 8612 "Read Write scsi_init_pkt() failure\n"); 8613 goto exit; 8614 } 8615 ASSERT(pkt->pkt_resid == 0); 8616 #ifdef STDEBUG 8617 bzero(pkt->pkt_private, st_recov_sz); 8618 bzero(pkt->pkt_scbp, stat_size); 8619 #endif 8620 ri = (recov_info *)pkt->pkt_private; 8621 ri->privatelen = st_recov_sz; 8622 if (un->un_bsize == 0) { 8623 count = bp->b_bcount; 8624 fixbit = 0; 8625 } else { 8626 count = bp->b_bcount / un->un_bsize; 8627 fixbit = 1; 8628 } 8629 if (bp->b_flags & B_READ) { 8630 com = SCMD_READ; 8631 un->un_lastop = ST_OP_READ; 8632 if ((un->un_bsize == 0) && /* Not Fixed Block */ 8633 (un->un_dp->options & ST_READ_IGNORE_ILI)) { 8634 fixbit = 2; 8635 } 8636 } else { 8637 com = SCMD_WRITE; 8638 un->un_lastop = ST_OP_WRITE; 8639 } 8640 tval = un->un_dp->io_timeout; 8641 8642 /* 8643 * For really large xfers, increase timeout 8644 */ 8645 if (bp->b_bcount > (10 * ONE_MEG)) 8646 tval *= bp->b_bcount/(10 * ONE_MEG); 8647 8648 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 8649 "%s %d amt 0x%lx\n", (com == SCMD_WRITE) ? 8650 wr_str: rd_str, un->un_pos.blkno, bp->b_bcount); 8651 8652 } else if ((ucmd = BP_UCMD(bp)) != NULL) { 8653 /* 8654 * uscsi - build command, allocate scsi resources 8655 */ 8656 st_make_uscsi_cmd(un, ucmd, bp, func); 8657 goto exit; 8658 8659 } else { /* special I/O */ 8660 struct buf *allocbp = NULL; 8661 com = (uchar_t)(uintptr_t)bp->b_forw; 8662 count = bp->b_bcount; 8663 8664 switch (com) { 8665 case SCMD_READ: 8666 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 8667 "special read %"PRId64"\n", count); 8668 if (un->un_bsize == 0) { 8669 fixbit = 2; /* suppress SILI */ 8670 } else { 8671 fixbit = 1; /* Fixed Block Mode */ 8672 count /= un->un_bsize; 8673 } 8674 allocbp = bp; 8675 un->un_lastop = ST_OP_READ; 8676 tval = un->un_dp->io_timeout; 8677 break; 8678 8679 case SCMD_WRITE: 8680 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 8681 "special write %"PRId64"\n", count); 8682 if (un->un_bsize != 0) { 8683 fixbit = 1; /* Fixed Block Mode */ 8684 count /= un->un_bsize; 8685 } else { 8686 fixbit = 0; 8687 } 8688 allocbp = bp; 8689 un->un_lastop = ST_OP_WRITE; 8690 tval = un->un_dp->io_timeout; 8691 break; 8692 8693 case SCMD_WRITE_FILE_MARK: 8694 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 8695 "write %"PRId64" file marks\n", count); 8696 un->un_lastop = ST_OP_WEOF; 8697 fixbit = 0; 8698 tval = un->un_dp->io_timeout; 8699 /* 8700 * If ST_SHORT_FILEMARKS bit is ON for EXABYTE 8701 * device, set the Vendor Unique bit to 8702 * write Short File Mark. 8703 */ 8704 if ((un->un_dp->options & ST_SHORT_FILEMARKS) && 8705 ((un->un_dp->type == ST_TYPE_EXB8500) || 8706 (un->un_dp->type == ST_TYPE_EXABYTE))) { 8707 /* 8708 * Now the Vendor Unique bit 7 in Byte 5 of CDB 8709 * is set to to write Short File Mark 8710 */ 8711 short_fm = 1; 8712 } 8713 break; 8714 8715 case SCMD_REWIND: 8716 /* 8717 * In the case of rewind we're gona do the rewind with 8718 * the immediate bit set so status will be retured when 8719 * the command is accepted by the device. We clear the 8720 * B_ASYNC flag so we wait for that acceptance. 8721 */ 8722 fixbit = 0; 8723 if (bp->b_flags & B_ASYNC) { 8724 allocbp = bp; 8725 if (count) { 8726 fixbit = 1; 8727 bp->b_flags &= ~B_ASYNC; 8728 } 8729 } 8730 count = 0; 8731 bp->b_bcount = 0; 8732 un->un_lastop = ST_OP_CTL; 8733 tval = un->un_dp->rewind_timeout; 8734 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 8735 "rewind\n"); 8736 break; 8737 8738 case SCMD_SPACE_G4: 8739 cdb_len = CDB_GROUP4; 8740 fixbit = SPACE_TYPE(bp->b_bcount); 8741 count = SPACE_CNT(bp->b_bcount); 8742 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 8743 " %s space %s %"PRId64" from file %d blk %d\n", 8744 bp->b_bcount & SP_BACKSP ? "backward" : "forward", 8745 space_strs[fixbit & 7], count, 8746 un->un_pos.fileno, un->un_pos.blkno); 8747 address = (count >> 48) & 0x1fff; 8748 additional = (count >> 16) & 0xffffffff; 8749 count &= 0xffff; 8750 count <<= 16; 8751 un->un_lastop = ST_OP_CTL; 8752 tval = un->un_dp->space_timeout; 8753 break; 8754 8755 case SCMD_SPACE: 8756 fixbit = SPACE_TYPE(bp->b_bcount); 8757 count = SPACE_CNT(bp->b_bcount); 8758 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 8759 " %s space %s %"PRId64" from file %d blk %d\n", 8760 bp->b_bcount & SP_BACKSP ? "backward" : "forward", 8761 space_strs[fixbit & 7], count, 8762 un->un_pos.fileno, un->un_pos.blkno); 8763 count &= 0xffffffff; 8764 un->un_lastop = ST_OP_CTL; 8765 tval = un->un_dp->space_timeout; 8766 break; 8767 8768 case SCMD_LOAD: 8769 ASSERT(count < 10); 8770 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 8771 "%s tape\n", load_strs[count]); 8772 fixbit = 0; 8773 8774 /* Loading or Unloading */ 8775 if (count & LD_LOAD) { 8776 tval = un->un_dp->load_timeout; 8777 } else { 8778 tval = un->un_dp->unload_timeout; 8779 } 8780 /* Is Retension requested */ 8781 if (count & LD_RETEN) { 8782 tval += un->un_dp->rewind_timeout; 8783 } 8784 un->un_lastop = ST_OP_CTL; 8785 break; 8786 8787 case SCMD_ERASE: 8788 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 8789 "erase tape\n"); 8790 ASSERT(count == 1); /* mt sets this */ 8791 if (count == 1) { 8792 /* 8793 * do long erase 8794 */ 8795 fixbit = 1; /* Long */ 8796 8797 /* Drive might not honor immidiate bit */ 8798 tval = un->un_dp->erase_timeout; 8799 } else { 8800 /* Short Erase */ 8801 tval = un->un_dp->erase_timeout; 8802 fixbit = 0; 8803 } 8804 un->un_lastop = ST_OP_CTL; 8805 count = 0; 8806 break; 8807 8808 case SCMD_MODE_SENSE: 8809 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 8810 "mode sense\n"); 8811 allocbp = bp; 8812 fixbit = 0; 8813 tval = un->un_dp->non_motion_timeout; 8814 un->un_lastop = ST_OP_CTL; 8815 break; 8816 8817 case SCMD_MODE_SELECT: 8818 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 8819 "mode select\n"); 8820 allocbp = bp; 8821 fixbit = 0; 8822 tval = un->un_dp->non_motion_timeout; 8823 un->un_lastop = ST_OP_CTL; 8824 break; 8825 8826 case SCMD_RESERVE: 8827 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 8828 "reserve\n"); 8829 fixbit = 0; 8830 tval = un->un_dp->non_motion_timeout; 8831 un->un_lastop = ST_OP_CTL; 8832 break; 8833 8834 case SCMD_RELEASE: 8835 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 8836 "release\n"); 8837 fixbit = 0; 8838 tval = un->un_dp->non_motion_timeout; 8839 un->un_lastop = ST_OP_CTL; 8840 break; 8841 8842 case SCMD_READ_BLKLIM: 8843 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 8844 "read block limits\n"); 8845 allocbp = bp; 8846 fixbit = count = 0; 8847 tval = un->un_dp->non_motion_timeout; 8848 un->un_lastop = ST_OP_CTL; 8849 break; 8850 8851 case SCMD_TEST_UNIT_READY: 8852 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 8853 "test unit ready\n"); 8854 fixbit = 0; 8855 tval = un->un_dp->non_motion_timeout; 8856 un->un_lastop = ST_OP_CTL; 8857 break; 8858 8859 case SCMD_DOORLOCK: 8860 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 8861 "prevent/allow media removal\n"); 8862 fixbit = 0; 8863 tval = un->un_dp->non_motion_timeout; 8864 un->un_lastop = ST_OP_CTL; 8865 break; 8866 8867 case SCMD_READ_POSITION: 8868 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 8869 "read position\n"); 8870 fixbit = un->un_read_pos_type; 8871 cdb_len = CDB_GROUP1; 8872 tval = un->un_dp->non_motion_timeout; 8873 allocbp = bp; 8874 un->un_lastop = ST_OP_CTL; 8875 switch (un->un_read_pos_type) { 8876 case LONG_POS: 8877 count = 0; 8878 break; 8879 case EXT_POS: 8880 count = sizeof (tape_position_ext_t); 8881 break; 8882 case SHORT_POS: 8883 count = 0; 8884 break; 8885 default: 8886 ST_DEBUG(ST_DEVINFO, st_label, CE_PANIC, 8887 "Unknown read position type 0x%x in " 8888 " st_make_cmd()\n", un->un_read_pos_type); 8889 } 8890 break; 8891 8892 default: 8893 ST_DEBUG(ST_DEVINFO, st_label, CE_PANIC, 8894 "Unhandled scsi command 0x%x in st_make_cmd()\n", 8895 com); 8896 } 8897 8898 pkt = scsi_init_pkt(ROUTE, NULL, allocbp, cdb_len, stat_size, 8899 st_recov_sz, 0, func, (caddr_t)un); 8900 if (pkt == NULL) { 8901 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 8902 "generic command scsi_init_pkt() failure\n"); 8903 goto exit; 8904 } 8905 8906 ASSERT(pkt->pkt_resid == 0); 8907 #ifdef STDEBUG 8908 bzero(pkt->pkt_private, st_recov_sz); 8909 bzero(pkt->pkt_scbp, stat_size); 8910 #endif 8911 ri = (recov_info *)pkt->pkt_private; 8912 ri->privatelen = st_recov_sz; 8913 if (allocbp) { 8914 ASSERT(geterror(allocbp) == 0); 8915 } 8916 8917 } 8918 8919 ucdb = (union scsi_cdb *)pkt->pkt_cdbp; 8920 8921 (void) scsi_setup_cdb(ucdb, com, address, (uint_t)count, additional); 8922 FILL_SCSI1_LUN(un->un_sd, pkt); 8923 /* 8924 * Initialize the SILI/Fixed bits of the byte 1 of cdb. 8925 */ 8926 ucdb->t_code = fixbit; 8927 ucdb->g0_vu_1 = short_fm; 8928 pkt->pkt_flags = flags; 8929 8930 ASSERT(tval); 8931 pkt->pkt_time = tval; 8932 if (bp == un->un_recov_buf) { 8933 pkt->pkt_comp = st_recov_cb; 8934 } else { 8935 pkt->pkt_comp = st_intr; 8936 } 8937 8938 st_add_recovery_info_to_pkt(un, bp, pkt); 8939 8940 /* 8941 * If we just write data to tape and did a command that doesn't 8942 * change position, we still need to write a filemark. 8943 */ 8944 if ((prev_op == ST_OP_WRITE) || (prev_op == ST_OP_WEOF)) { 8945 recov_info *rcvi = pkt->pkt_private; 8946 cmd_attribute const *atrib; 8947 8948 if (rcvi->privatelen == sizeof (recov_info)) { 8949 atrib = rcvi->cmd_attrib; 8950 } else { 8951 atrib = st_lookup_cmd_attribute(com); 8952 } 8953 if (atrib->chg_tape_direction == DIR_NONE) { 8954 un->un_lastop = prev_op; 8955 } 8956 } 8957 8958 exit: 8959 ASSERT(mutex_owned(ST_MUTEX)); 8960 } 8961 8962 8963 /* 8964 * Build a command based on a uscsi command; 8965 */ 8966 static void 8967 st_make_uscsi_cmd(struct scsi_tape *un, struct uscsi_cmd *ucmd, 8968 struct buf *bp, int (*func)(caddr_t)) 8969 { 8970 struct scsi_pkt *pkt; 8971 recov_info *ri; 8972 caddr_t cdb; 8973 int cdblen; 8974 int stat_size = 1; 8975 int flags = 0; 8976 8977 ST_FUNC(ST_DEVINFO, st_make_uscsi_cmd); 8978 8979 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 8980 "st_make_uscsi_cmd(): dev = 0x%lx\n", un->un_dev); 8981 8982 if (ucmd->uscsi_flags & USCSI_RQENABLE) { 8983 if (un->un_arq_enabled) { 8984 if (ucmd->uscsi_rqlen > SENSE_LENGTH) { 8985 stat_size = (int)(ucmd->uscsi_rqlen) + 8986 sizeof (struct scsi_arq_status) - 8987 sizeof (struct scsi_extended_sense); 8988 flags = PKT_XARQ; 8989 } else { 8990 stat_size = sizeof (struct scsi_arq_status); 8991 } 8992 } 8993 } 8994 8995 ASSERT(mutex_owned(ST_MUTEX)); 8996 8997 cdb = ucmd->uscsi_cdb; 8998 cdblen = ucmd->uscsi_cdblen; 8999 9000 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 9001 "st_make_uscsi_cmd: buflen=%ld bcount=%ld\n", 9002 ucmd->uscsi_buflen, bp->b_bcount); 9003 pkt = scsi_init_pkt(ROUTE, NULL, 9004 (bp->b_bcount > 0) ? bp : NULL, 9005 cdblen, stat_size, st_recov_sz, flags, func, (caddr_t)un); 9006 if (pkt == NULL) { 9007 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 9008 "uscsi command scsi_init_pkt() failure\n"); 9009 goto exit; 9010 } 9011 9012 ASSERT(pkt->pkt_resid == 0); 9013 #ifdef STDEBUG 9014 bzero(pkt->pkt_private, st_recov_sz); 9015 bzero(pkt->pkt_scbp, stat_size); 9016 #endif 9017 ri = (recov_info *)pkt->pkt_private; 9018 ri->privatelen = st_recov_sz; 9019 9020 bcopy(cdb, pkt->pkt_cdbp, (uint_t)cdblen); 9021 9022 #ifdef STDEBUG 9023 if ((st_debug & 0x7) >= 6) { 9024 st_clean_print(ST_DEVINFO, st_label, SCSI_DEBUG, 9025 "pkt_cdbp", (char *)cdb, cdblen); 9026 } 9027 #endif 9028 9029 if (ucmd->uscsi_flags & USCSI_SILENT) { 9030 pkt->pkt_flags |= FLAG_SILENT; 9031 } 9032 9033 pkt->pkt_time = ucmd->uscsi_timeout; 9034 if (bp == un->un_recov_buf) { 9035 pkt->pkt_comp = st_recov_cb; 9036 } else { 9037 pkt->pkt_comp = st_intr; 9038 } 9039 st_add_recovery_info_to_pkt(un, bp, pkt); 9040 exit: 9041 ASSERT(mutex_owned(ST_MUTEX)); 9042 } 9043 9044 9045 /* 9046 * restart cmd currently at the head of the runq 9047 * 9048 * If scsi_transport() succeeds or the retries 9049 * count exhausted, restore the throttle that was 9050 * zeroed out in st_handle_intr_busy(). 9051 * 9052 */ 9053 static void 9054 st_intr_restart(void *arg) 9055 { 9056 struct scsi_tape *un = arg; 9057 struct buf *bp; 9058 int queued; 9059 int status = TRAN_ACCEPT; 9060 9061 mutex_enter(ST_MUTEX); 9062 9063 ST_FUNC(ST_DEVINFO, st_intr_restart); 9064 9065 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 9066 "st_intr_restart(), un = 0x%p\n", (void *)un); 9067 9068 un->un_hib_tid = 0; 9069 9070 if (un->un_recov_buf_busy != 0) { 9071 bp = un->un_recov_buf; 9072 queued = 0; 9073 } else if (un->un_sbuf_busy != 0) { 9074 bp = un->un_sbufp; 9075 queued = 0; 9076 } else if (un->un_quef != NULL) { 9077 bp = un->un_quef; 9078 queued = 1; 9079 } else { 9080 mutex_exit(ST_MUTEX); 9081 return; 9082 } 9083 9084 /* 9085 * Here we know : 9086 * throttle = 0, via st_handle_intr_busy 9087 */ 9088 9089 if (queued) { 9090 /* 9091 * move from waitq to runq, if there is anything on the waitq 9092 */ 9093 (void) st_remove_from_queue(&un->un_quef, &un->un_quef, bp); 9094 9095 if (un->un_runqf) { 9096 /* 9097 * not good, we don't want to requeue something after 9098 * another. 9099 */ 9100 goto done_error; 9101 } else { 9102 un->un_runqf = bp; 9103 un->un_runql = bp; 9104 } 9105 } 9106 9107 ST_CDB(ST_DEVINFO, "Interrupt restart CDB", 9108 (char *)BP_PKT(bp)->pkt_cdbp); 9109 9110 ST_DO_KSTATS(bp, kstat_waitq_to_runq); 9111 9112 status = st_transport(un, BP_PKT(bp)); 9113 9114 if (status != TRAN_ACCEPT) { 9115 ST_DO_KSTATS(bp, kstat_runq_back_to_waitq); 9116 9117 if (status == TRAN_BUSY) { 9118 pkt_info *pkti = BP_PKT(bp)->pkt_private; 9119 9120 if (pkti->privatelen == sizeof (recov_info) && 9121 un->un_unit_attention_flags && 9122 bp != un->un_recov_buf) { 9123 un->un_unit_attention_flags = 0; 9124 ST_RECOV(ST_DEVINFO, st_label, CE_WARN, 9125 "Command Recovery called on busy resend\n"); 9126 if (st_command_recovery(un, BP_PKT(bp), 9127 ATTEMPT_RETRY) == JUST_RETURN) { 9128 mutex_exit(ST_MUTEX); 9129 return; 9130 } 9131 } 9132 mutex_exit(ST_MUTEX); 9133 if (st_handle_intr_busy(un, bp, 9134 ST_TRAN_BUSY_TIMEOUT) == 0) 9135 return; /* timeout is setup again */ 9136 mutex_enter(ST_MUTEX); 9137 } 9138 9139 done_error: 9140 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 9141 "restart transport rejected\n"); 9142 bp->b_resid = bp->b_bcount; 9143 9144 if (un->un_last_throttle) { 9145 un->un_throttle = un->un_last_throttle; 9146 } 9147 if (status != TRAN_ACCEPT) { 9148 ST_DO_ERRSTATS(un, st_transerrs); 9149 } 9150 ST_DO_KSTATS(bp, kstat_waitq_exit); 9151 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 9152 "busy restart aborted\n"); 9153 st_set_pe_flag(un); 9154 st_bioerror(bp, EIO); 9155 st_done_and_mutex_exit(un, bp); 9156 } else { 9157 if (un->un_last_throttle) { 9158 un->un_throttle = un->un_last_throttle; 9159 } 9160 mutex_exit(ST_MUTEX); 9161 } 9162 } 9163 9164 /* 9165 * st_check_media(): 9166 * Periodically check the media state using scsi_watch service; 9167 * this service calls back after TUR and possibly request sense 9168 * the callback handler (st_media_watch_cb()) decodes the request sense 9169 * data (if any) 9170 */ 9171 9172 static int 9173 st_check_media(dev_t dev, enum mtio_state state) 9174 { 9175 int rval = 0; 9176 enum mtio_state prev_state; 9177 opaque_t token = NULL; 9178 9179 GET_SOFT_STATE(dev); 9180 9181 ST_FUNC(ST_DEVINFO, st_check_media); 9182 9183 mutex_enter(ST_MUTEX); 9184 9185 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 9186 "st_check_media:state=%x, mediastate=%x\n", 9187 state, un->un_mediastate); 9188 9189 prev_state = un->un_mediastate; 9190 9191 /* 9192 * is there anything to do? 9193 */ 9194 retry: 9195 if (state == un->un_mediastate || un->un_mediastate == MTIO_NONE) { 9196 /* 9197 * submit the request to the scsi_watch service; 9198 * scsi_media_watch_cb() does the real work 9199 */ 9200 mutex_exit(ST_MUTEX); 9201 token = scsi_watch_request_submit(ST_SCSI_DEVP, 9202 st_check_media_time, SENSE_LENGTH, 9203 st_media_watch_cb, (caddr_t)dev); 9204 if (token == NULL) { 9205 rval = EAGAIN; 9206 goto done; 9207 } 9208 mutex_enter(ST_MUTEX); 9209 9210 un->un_swr_token = token; 9211 un->un_specified_mediastate = state; 9212 9213 /* 9214 * now wait for media change 9215 * we will not be signalled unless mediastate == state but it 9216 * still better to test for this condition, since there 9217 * is a 5 sec cv_broadcast delay when 9218 * mediastate == MTIO_INSERTED 9219 */ 9220 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 9221 "st_check_media:waiting for media state change\n"); 9222 while (un->un_mediastate == state) { 9223 if (cv_wait_sig(&un->un_state_cv, ST_MUTEX) == 0) { 9224 mutex_exit(ST_MUTEX); 9225 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 9226 "st_check_media:waiting for media state " 9227 "was interrupted\n"); 9228 rval = EINTR; 9229 goto done; 9230 } 9231 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 9232 "st_check_media:received signal, state=%x\n", 9233 un->un_mediastate); 9234 } 9235 } 9236 9237 /* 9238 * if we transitioned to MTIO_INSERTED, media has really been 9239 * inserted. If TUR fails, it is probably a exabyte slow spin up. 9240 * Reset and retry the state change. If everything is ok, replay 9241 * the open() logic. 9242 */ 9243 if ((un->un_mediastate == MTIO_INSERTED) && 9244 (un->un_state == ST_STATE_OFFLINE)) { 9245 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 9246 "st_check_media: calling st_cmd to confirm inserted\n"); 9247 9248 /* 9249 * set this early so that TUR will make it through strategy 9250 * without triggering a st_tape_init(). We needed it set 9251 * before calling st_tape_init() ourselves anyway. If TUR 9252 * fails, set it back 9253 */ 9254 un->un_state = ST_STATE_INITIALIZING; 9255 9256 /* 9257 * If not reserved fail as getting reservation conflict 9258 * will make this hang forever. 9259 */ 9260 if ((un->un_rsvd_status & 9261 (ST_RESERVE | ST_APPLICATION_RESERVATIONS)) == 0) { 9262 mutex_exit(ST_MUTEX); 9263 rval = EACCES; 9264 goto done; 9265 } 9266 rval = st_cmd(un, SCMD_TEST_UNIT_READY, 0, SYNC_CMD); 9267 if (rval == EACCES) { 9268 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 9269 "st_check_media: TUR got Reservation Conflict\n"); 9270 mutex_exit(ST_MUTEX); 9271 goto done; 9272 } 9273 if (rval) { 9274 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 9275 "st_check_media: TUR failed, going to retry\n"); 9276 un->un_mediastate = prev_state; 9277 un->un_state = ST_STATE_OFFLINE; 9278 goto retry; 9279 } 9280 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 9281 "st_check_media: media inserted\n"); 9282 9283 /* this also rewinds the tape */ 9284 rval = st_tape_init(un); 9285 if (rval != 0) { 9286 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 9287 "st_check_media : OFFLINE init failure "); 9288 un->un_state = ST_STATE_OFFLINE; 9289 un->un_pos.pmode = invalid; 9290 } else { 9291 un->un_state = ST_STATE_OPEN_PENDING_IO; 9292 } 9293 } else if ((un->un_mediastate == MTIO_EJECTED) && 9294 (un->un_state != ST_STATE_OFFLINE)) { 9295 /* 9296 * supported devices must be rewound before ejection 9297 * rewind resets fileno & blkno 9298 */ 9299 un->un_laststate = un->un_state; 9300 un->un_state = ST_STATE_OFFLINE; 9301 } 9302 mutex_exit(ST_MUTEX); 9303 done: 9304 if (token) { 9305 (void) scsi_watch_request_terminate(token, 9306 SCSI_WATCH_TERMINATE_WAIT); 9307 mutex_enter(ST_MUTEX); 9308 un->un_swr_token = (opaque_t)NULL; 9309 mutex_exit(ST_MUTEX); 9310 } 9311 9312 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, "st_check_media: done\n"); 9313 9314 return (rval); 9315 } 9316 9317 /* 9318 * st_media_watch_cb() is called by scsi_watch_thread for 9319 * verifying the request sense data (if any) 9320 */ 9321 static int 9322 st_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp) 9323 { 9324 struct scsi_status *statusp = resultp->statusp; 9325 struct scsi_extended_sense *sensep = resultp->sensep; 9326 uchar_t actual_sense_length = resultp->actual_sense_length; 9327 struct scsi_tape *un; 9328 enum mtio_state state = MTIO_NONE; 9329 int instance; 9330 dev_t dev = (dev_t)arg; 9331 9332 instance = MTUNIT(dev); 9333 if ((un = ddi_get_soft_state(st_state, instance)) == NULL) { 9334 return (-1); 9335 } 9336 9337 mutex_enter(ST_MUTEX); 9338 ST_FUNC(ST_DEVINFO, st_media_watch_cb); 9339 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 9340 "st_media_watch_cb: status=%x, sensep=%p, len=%x\n", 9341 *((char *)statusp), (void *)sensep, 9342 actual_sense_length); 9343 9344 9345 /* 9346 * if there was a check condition then sensep points to valid 9347 * sense data 9348 * if status was not a check condition but a reservation or busy 9349 * status then the new state is MTIO_NONE 9350 */ 9351 if (sensep) { 9352 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 9353 "st_media_watch_cb: KEY=%x, ASC=%x, ASCQ=%x\n", 9354 sensep->es_key, sensep->es_add_code, sensep->es_qual_code); 9355 9356 switch (un->un_dp->type) { 9357 default: 9358 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 9359 "st_media_watch_cb: unknown drive type %d, " 9360 "default to ST_TYPE_HP\n", un->un_dp->type); 9361 /* FALLTHROUGH */ 9362 9363 case ST_TYPE_STC3490: /* STK 4220 1/2" cartridge */ 9364 case ST_TYPE_FUJI: /* 1/2" cartridge */ 9365 case ST_TYPE_HP: /* HP 88780 1/2" reel */ 9366 if (un->un_dp->type == ST_TYPE_FUJI) { 9367 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 9368 "st_media_watch_cb: ST_TYPE_FUJI\n"); 9369 } else { 9370 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 9371 "st_media_watch_cb: ST_TYPE_HP\n"); 9372 } 9373 switch (sensep->es_key) { 9374 case KEY_UNIT_ATTENTION: 9375 /* not ready to ready transition */ 9376 /* hp/es_qual_code == 80 on>off>on */ 9377 /* hp/es_qual_code == 0 on>off>unld>ld>on */ 9378 if (sensep->es_add_code == 0x28) { 9379 state = MTIO_INSERTED; 9380 } 9381 break; 9382 case KEY_NOT_READY: 9383 /* in process, rewinding or loading */ 9384 if ((sensep->es_add_code == 0x04) && 9385 (sensep->es_qual_code == 0x00)) { 9386 state = MTIO_EJECTED; 9387 } 9388 break; 9389 } 9390 break; 9391 9392 case ST_TYPE_EXB8500: /* Exabyte 8500 */ 9393 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 9394 "st_media_watch_cb: ST_TYPE_EXB8500\n"); 9395 switch (sensep->es_key) { 9396 case KEY_UNIT_ATTENTION: 9397 /* operator medium removal request */ 9398 if ((sensep->es_add_code == 0x5a) && 9399 (sensep->es_qual_code == 0x01)) { 9400 state = MTIO_EJECTED; 9401 /* not ready to ready transition */ 9402 } else if ((sensep->es_add_code == 0x28) && 9403 (sensep->es_qual_code == 0x00)) { 9404 state = MTIO_INSERTED; 9405 } 9406 break; 9407 case KEY_NOT_READY: 9408 /* medium not present */ 9409 if (sensep->es_add_code == 0x3a) { 9410 state = MTIO_EJECTED; 9411 } 9412 break; 9413 } 9414 break; 9415 case ST_TYPE_EXABYTE: /* Exabyte 8200 */ 9416 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 9417 "st_media_watch_cb: ST_TYPE_EXABYTE\n"); 9418 switch (sensep->es_key) { 9419 case KEY_NOT_READY: 9420 if ((sensep->es_add_code == 0x04) && 9421 (sensep->es_qual_code == 0x00)) { 9422 /* volume not mounted? */ 9423 state = MTIO_EJECTED; 9424 } else if (sensep->es_add_code == 0x3a) { 9425 state = MTIO_EJECTED; 9426 } 9427 break; 9428 case KEY_UNIT_ATTENTION: 9429 state = MTIO_EJECTED; 9430 break; 9431 } 9432 break; 9433 9434 case ST_TYPE_DLT: /* quantum DLT4xxx */ 9435 switch (sensep->es_key) { 9436 case KEY_UNIT_ATTENTION: 9437 if (sensep->es_add_code == 0x28) { 9438 state = MTIO_INSERTED; 9439 } 9440 break; 9441 case KEY_NOT_READY: 9442 if (sensep->es_add_code == 0x04) { 9443 /* in transition but could be either */ 9444 state = un->un_specified_mediastate; 9445 } else if ((sensep->es_add_code == 0x3a) && 9446 (sensep->es_qual_code == 0x00)) { 9447 state = MTIO_EJECTED; 9448 } 9449 break; 9450 } 9451 break; 9452 } 9453 } else if (*((char *)statusp) == STATUS_GOOD) { 9454 state = MTIO_INSERTED; 9455 } 9456 9457 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 9458 "st_media_watch_cb:state=%x, specified=%x\n", 9459 state, un->un_specified_mediastate); 9460 9461 /* 9462 * now signal the waiting thread if this is *not* the specified state; 9463 * delay the signal if the state is MTIO_INSERTED 9464 * to allow the target to recover 9465 */ 9466 if (state != un->un_specified_mediastate) { 9467 un->un_mediastate = state; 9468 if (state == MTIO_INSERTED) { 9469 /* 9470 * delay the signal to give the drive a chance 9471 * to do what it apparently needs to do 9472 */ 9473 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 9474 "st_media_watch_cb:delayed cv_broadcast\n"); 9475 un->un_delay_tid = timeout(st_delayed_cv_broadcast, 9476 un, drv_usectohz((clock_t)MEDIA_ACCESS_DELAY)); 9477 } else { 9478 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 9479 "st_media_watch_cb:immediate cv_broadcast\n"); 9480 cv_broadcast(&un->un_state_cv); 9481 } 9482 } 9483 mutex_exit(ST_MUTEX); 9484 return (0); 9485 } 9486 9487 /* 9488 * delayed cv_broadcast to allow for target to recover 9489 * from media insertion 9490 */ 9491 static void 9492 st_delayed_cv_broadcast(void *arg) 9493 { 9494 struct scsi_tape *un = arg; 9495 9496 ST_FUNC(ST_DEVINFO, st_delayed_cv_broadcast); 9497 9498 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 9499 "st_delayed_cv_broadcast:delayed cv_broadcast\n"); 9500 9501 mutex_enter(ST_MUTEX); 9502 cv_broadcast(&un->un_state_cv); 9503 mutex_exit(ST_MUTEX); 9504 } 9505 9506 /* 9507 * restart cmd currently at the start of the waitq 9508 */ 9509 static void 9510 st_start_restart(void *arg) 9511 { 9512 struct scsi_tape *un = arg; 9513 9514 ST_FUNC(ST_DEVINFO, st_start_restart); 9515 9516 ASSERT(un != NULL); 9517 9518 mutex_enter(ST_MUTEX); 9519 9520 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, "st_tran_restart()\n"); 9521 9522 st_start(un); 9523 9524 mutex_exit(ST_MUTEX); 9525 } 9526 9527 9528 /* 9529 * Command completion processing 9530 * 9531 */ 9532 static void 9533 st_intr(struct scsi_pkt *pkt) 9534 { 9535 recov_info *rcv = pkt->pkt_private; 9536 struct buf *bp = rcv->cmd_bp; 9537 struct scsi_tape *un; 9538 errstate action = COMMAND_DONE; 9539 clock_t timout; 9540 int status; 9541 9542 un = ddi_get_soft_state(st_state, MTUNIT(bp->b_edev)); 9543 9544 ST_FUNC(ST_DEVINFO, st_intr); 9545 9546 ASSERT(un != NULL); 9547 9548 mutex_enter(ST_MUTEX); 9549 9550 ASSERT(bp != un->un_recov_buf); 9551 9552 un->un_rqs_state &= ~(ST_RQS_ERROR); 9553 9554 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, "st_intr()\n"); 9555 9556 if (pkt->pkt_reason != CMD_CMPLT) { 9557 ST_DEBUG(ST_DEVINFO, st_label, CE_WARN, 9558 "Unhappy packet status reason = %s statistics = 0x%x\n", 9559 scsi_rname(pkt->pkt_reason), pkt->pkt_statistics); 9560 9561 /* If device has gone away not much else to do */ 9562 if (pkt->pkt_reason == CMD_DEV_GONE) { 9563 action = COMMAND_DONE_ERROR; 9564 } else if ((pkt == un->un_rqs) || 9565 (un->un_state == ST_STATE_SENSING)) { 9566 ASSERT(pkt == un->un_rqs); 9567 ASSERT(un->un_state == ST_STATE_SENSING); 9568 un->un_state = un->un_laststate; 9569 rcv->cmd_bp = un->un_rqs_bp; 9570 ST_DO_ERRSTATS(un, st_transerrs); 9571 action = COMMAND_DONE_ERROR; 9572 } else { 9573 action = st_handle_incomplete(un, bp); 9574 } 9575 /* 9576 * At this point we know that the command was successfully 9577 * completed. Now what? 9578 */ 9579 } else if ((pkt == un->un_rqs) || (un->un_state == ST_STATE_SENSING)) { 9580 /* 9581 * okay. We were running a REQUEST SENSE. Find 9582 * out what to do next. 9583 */ 9584 ASSERT(pkt == un->un_rqs); 9585 ASSERT(un->un_state == ST_STATE_SENSING); 9586 scsi_sync_pkt(pkt); 9587 action = st_handle_sense(un, bp, &un->un_pos); 9588 /* 9589 * Make rqs isn't going to be retied. 9590 */ 9591 if (action != QUE_BUSY_COMMAND && action != QUE_COMMAND) { 9592 /* 9593 * set pkt back to original packet in case we will have 9594 * to requeue it 9595 */ 9596 pkt = BP_PKT(bp); 9597 rcv->cmd_bp = un->un_rqs_bp; 9598 /* 9599 * some actions are based on un_state, hence 9600 * restore the state st was in before ST_STATE_SENSING. 9601 */ 9602 un->un_state = un->un_laststate; 9603 } 9604 9605 } else if (un->un_arq_enabled && (pkt->pkt_state & STATE_ARQ_DONE)) { 9606 /* 9607 * the transport layer successfully completed an autorqsense 9608 */ 9609 action = st_handle_autosense(un, bp, &un->un_pos); 9610 9611 } else if ((SCBP(pkt)->sts_busy) || 9612 (SCBP(pkt)->sts_chk) || 9613 (SCBP(pkt)->sts_vu7)) { 9614 /* 9615 * Okay, we weren't running a REQUEST SENSE. Call a routine 9616 * to see if the status bits we're okay. If a request sense 9617 * is to be run, that will happen. 9618 */ 9619 action = st_check_error(un, pkt); 9620 } 9621 9622 if (un->un_pwr_mgmt == ST_PWR_SUSPENDED) { 9623 switch (action) { 9624 case QUE_COMMAND: 9625 /* 9626 * return cmd to head to the queue 9627 * since we are suspending so that 9628 * it gets restarted during resume 9629 */ 9630 st_add_to_queue(&un->un_runqf, &un->un_runql, 9631 un->un_runqf, bp); 9632 9633 action = JUST_RETURN; 9634 break; 9635 9636 case QUE_SENSE: 9637 action = COMMAND_DONE_ERROR; 9638 break; 9639 9640 default: 9641 break; 9642 } 9643 } 9644 9645 /* 9646 * check for undetected path failover. 9647 */ 9648 if ((un->un_multipath) && 9649 (un->un_last_path_instance != pkt->pkt_path_instance)) { 9650 if (un->un_state > ST_STATE_OPENING) { 9651 ST_RECOV(ST_DEVINFO, st_label, CE_NOTE, 9652 "Failover detected, action is %s\n", 9653 errstatenames[action]); 9654 if (action == COMMAND_DONE) { 9655 action = PATH_FAILED; 9656 } 9657 } 9658 un->un_last_path_instance = pkt->pkt_path_instance; 9659 } 9660 9661 /* 9662 * Restore old state if we were sensing. 9663 */ 9664 if (un->un_state == ST_STATE_SENSING && action != QUE_SENSE) { 9665 un->un_state = un->un_laststate; 9666 } 9667 9668 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 9669 "st_intr: pkt=%p, bp=%p, action=%s, status=%x\n", 9670 (void *)pkt, (void *)bp, errstatenames[action], SCBP_C(pkt)); 9671 9672 again: 9673 switch (action) { 9674 case COMMAND_DONE_EACCES: 9675 /* this is to report a reservation conflict */ 9676 st_bioerror(bp, EACCES); 9677 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 9678 "Reservation Conflict \n"); 9679 un->un_pos.pmode = invalid; 9680 9681 /*FALLTHROUGH*/ 9682 case COMMAND_DONE_ERROR: 9683 if (un->un_pos.eof < ST_EOT_PENDING && 9684 un->un_state >= ST_STATE_OPEN) { 9685 /* 9686 * all errors set state of the tape to 'unknown' 9687 * unless we're at EOT or are doing append testing. 9688 * If sense key was illegal request, preserve state. 9689 */ 9690 if (un->un_status != KEY_ILLEGAL_REQUEST) { 9691 un->un_pos.pmode = invalid; 9692 } 9693 } 9694 9695 un->un_err_resid = bp->b_resid = bp->b_bcount; 9696 /* 9697 * since we have an error (COMMAND_DONE_ERROR), we want to 9698 * make sure an error ocurrs, so make sure at least EIO is 9699 * returned 9700 */ 9701 if (geterror(bp) == 0) 9702 st_bioerror(bp, EIO); 9703 9704 st_set_pe_flag(un); 9705 if (!(un->un_rqs_state & ST_RQS_ERROR) && 9706 (un->un_errno == EIO)) { 9707 un->un_rqs_state &= ~(ST_RQS_VALID); 9708 } 9709 break; 9710 9711 case COMMAND_DONE_ERROR_RECOVERED: 9712 un->un_err_resid = bp->b_resid = bp->b_bcount; 9713 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 9714 "st_intr(): COMMAND_DONE_ERROR_RECOVERED"); 9715 if (geterror(bp) == 0) { 9716 st_bioerror(bp, EIO); 9717 } 9718 st_set_pe_flag(un); 9719 if (!(un->un_rqs_state & ST_RQS_ERROR) && 9720 (un->un_errno == EIO)) { 9721 un->un_rqs_state &= ~(ST_RQS_VALID); 9722 } 9723 /*FALLTHROUGH*/ 9724 case COMMAND_DONE: 9725 st_set_state(un, bp); 9726 break; 9727 9728 case QUE_SENSE: 9729 if ((un->un_ncmds > 1) && !un->un_flush_on_errors) 9730 goto sense_error; 9731 9732 if (un->un_state != ST_STATE_SENSING) { 9733 un->un_laststate = un->un_state; 9734 un->un_state = ST_STATE_SENSING; 9735 } 9736 9737 /* 9738 * zero the sense data. 9739 */ 9740 bzero(un->un_rqs->pkt_scbp, SENSE_LENGTH); 9741 9742 /* 9743 * If this is not a retry on QUE_SENSE point to the original 9744 * bp of the command that got us here. 9745 */ 9746 if (pkt != un->un_rqs) { 9747 ((recov_info *)un->un_rqs->pkt_private)->cmd_bp = bp; 9748 } 9749 9750 if (un->un_throttle) { 9751 un->un_last_throttle = un->un_throttle; 9752 un->un_throttle = 0; 9753 } 9754 9755 ST_CDB(ST_DEVINFO, "Queue sense CDB", 9756 (char *)BP_PKT(bp)->pkt_cdbp); 9757 9758 /* 9759 * never retry this, some other command will have nuked the 9760 * sense, anyway 9761 */ 9762 status = st_transport(un, un->un_rqs); 9763 9764 if (un->un_last_throttle) { 9765 un->un_throttle = un->un_last_throttle; 9766 } 9767 9768 if (status == TRAN_ACCEPT) { 9769 mutex_exit(ST_MUTEX); 9770 return; 9771 } 9772 if (status != TRAN_BUSY) 9773 ST_DO_ERRSTATS(un, st_transerrs); 9774 sense_error: 9775 un->un_pos.pmode = invalid; 9776 st_bioerror(bp, EIO); 9777 st_set_pe_flag(un); 9778 break; 9779 9780 case QUE_BUSY_COMMAND: 9781 /* longish timeout */ 9782 timout = ST_STATUS_BUSY_TIMEOUT; 9783 goto que_it_up; 9784 9785 case QUE_COMMAND: 9786 /* short timeout */ 9787 timout = ST_TRAN_BUSY_TIMEOUT; 9788 que_it_up: 9789 /* 9790 * let st_handle_intr_busy put this bp back on waitq and make 9791 * checks to see if it is ok to requeue the command. 9792 */ 9793 ST_DO_KSTATS(bp, kstat_runq_back_to_waitq); 9794 9795 /* 9796 * Save the throttle before setting up the timeout 9797 */ 9798 if (un->un_throttle) { 9799 un->un_last_throttle = un->un_throttle; 9800 } 9801 mutex_exit(ST_MUTEX); 9802 if (st_handle_intr_busy(un, bp, timout) == 0) 9803 return; /* timeout is setup again */ 9804 9805 mutex_enter(ST_MUTEX); 9806 un->un_pos.pmode = invalid; 9807 un->un_err_resid = bp->b_resid = bp->b_bcount; 9808 st_bioerror(bp, EIO); 9809 st_set_pe_flag(un); 9810 break; 9811 9812 case QUE_LAST_COMMAND: 9813 9814 if ((un->un_ncmds > 1) && !un->un_flush_on_errors) { 9815 scsi_log(ST_DEVINFO, st_label, CE_CONT, 9816 "un_ncmds: %d can't retry cmd \n", un->un_ncmds); 9817 goto last_command_error; 9818 } 9819 mutex_exit(ST_MUTEX); 9820 if (st_handle_intr_retry_lcmd(un, bp) == 0) 9821 return; 9822 mutex_enter(ST_MUTEX); 9823 last_command_error: 9824 un->un_err_resid = bp->b_resid = bp->b_bcount; 9825 un->un_pos.pmode = invalid; 9826 st_bioerror(bp, EIO); 9827 st_set_pe_flag(un); 9828 break; 9829 9830 case COMMAND_TIMEOUT: 9831 case DEVICE_RESET: 9832 case DEVICE_TAMPER: 9833 case ATTEMPT_RETRY: 9834 case PATH_FAILED: 9835 ST_RECOV(ST_DEVINFO, st_label, CE_WARN, 9836 "Command Recovery called on %s status\n", 9837 errstatenames[action]); 9838 action = st_command_recovery(un, pkt, action); 9839 goto again; 9840 9841 default: 9842 ASSERT(0); 9843 /* FALLTHRU */ 9844 case JUST_RETURN: 9845 ST_DO_KSTATS(bp, kstat_runq_back_to_waitq); 9846 mutex_exit(ST_MUTEX); 9847 return; 9848 } 9849 9850 ST_DO_KSTATS(bp, kstat_runq_exit); 9851 st_done_and_mutex_exit(un, bp); 9852 } 9853 9854 static errstate 9855 st_handle_incomplete(struct scsi_tape *un, struct buf *bp) 9856 { 9857 static char *fail = "SCSI transport failed: reason '%s': %s\n"; 9858 recov_info *rinfo; 9859 errstate rval = COMMAND_DONE_ERROR; 9860 struct scsi_pkt *pkt = (un->un_state == ST_STATE_SENSING) ? 9861 un->un_rqs : BP_PKT(bp); 9862 int result; 9863 9864 ST_FUNC(ST_DEVINFO, st_handle_incomplete); 9865 9866 rinfo = (recov_info *)pkt->pkt_private; 9867 9868 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 9869 "st_handle_incomplete(): dev = 0x%lx\n", un->un_dev); 9870 9871 ASSERT(mutex_owned(ST_MUTEX)); 9872 9873 switch (pkt->pkt_reason) { 9874 case CMD_INCOMPLETE: /* tran stopped with not normal state */ 9875 /* 9876 * this occurs when accessing a powered down drive, no 9877 * need to complain; just fail the open 9878 */ 9879 ST_CDB(ST_DEVINFO, "Incomplete CDB", (char *)pkt->pkt_cdbp); 9880 9881 /* 9882 * if we have commands outstanding in HBA, and a command 9883 * comes back incomplete, we're hosed, so reset target 9884 * If we have the bus, but cmd_incomplete, we probably just 9885 * have a failed selection, so don't reset the target, just 9886 * requeue the command and try again 9887 */ 9888 if ((un->un_ncmds > 1) || (pkt->pkt_state != STATE_GOT_BUS)) { 9889 goto reset_target; 9890 } 9891 9892 /* 9893 * Retry selection a couple more times if we're 9894 * open. If opening, we only try just once to 9895 * reduce probe time for nonexistant devices. 9896 */ 9897 if ((un->un_laststate > ST_STATE_OPENING) && 9898 (rinfo->pkt_retry_cnt < st_selection_retry_count)) { 9899 /* XXX check retriable? */ 9900 rval = QUE_COMMAND; 9901 } 9902 ST_DO_ERRSTATS(un, st_transerrs); 9903 break; 9904 9905 case CMD_ABORTED: 9906 /* 9907 * most likely this is caused by flush-on-error support. If 9908 * it was not there, the we're in trouble. 9909 */ 9910 if (!un->un_flush_on_errors) { 9911 un->un_status = SUN_KEY_FATAL; 9912 goto reset_target; 9913 } 9914 9915 st_set_pe_errno(un); 9916 bioerror(bp, un->un_errno); 9917 if (un->un_errno) 9918 return (COMMAND_DONE_ERROR); 9919 else 9920 return (COMMAND_DONE); 9921 9922 case CMD_TIMEOUT: /* Command timed out */ 9923 un->un_status = SUN_KEY_TIMEOUT; 9924 return (COMMAND_TIMEOUT); 9925 9926 case CMD_TRAN_ERR: 9927 case CMD_RESET: 9928 if (pkt->pkt_statistics & (STAT_BUS_RESET | STAT_DEV_RESET)) { 9929 if ((un->un_rsvd_status & 9930 (ST_RESERVE | ST_APPLICATION_RESERVATIONS)) == 9931 ST_RESERVE) { 9932 un->un_rsvd_status |= ST_LOST_RESERVE; 9933 ST_DEBUG3(ST_DEVINFO, st_label, CE_WARN, 9934 "Lost Reservation\n"); 9935 } 9936 rval = DEVICE_RESET; 9937 return (rval); 9938 } 9939 if (pkt->pkt_statistics & (STAT_ABORTED | STAT_TERMINATED)) { 9940 rval = DEVICE_RESET; 9941 return (rval); 9942 } 9943 /*FALLTHROUGH*/ 9944 default: 9945 scsi_log(ST_DEVINFO, st_label, CE_WARN, 9946 "Unhandled packet status reason = %s statistics = 0x%x\n", 9947 scsi_rname(pkt->pkt_reason), pkt->pkt_statistics); 9948 reset_target: 9949 9950 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 9951 "transport completed with %s\n", 9952 scsi_rname(pkt->pkt_reason)); 9953 ST_DO_ERRSTATS(un, st_transerrs); 9954 if ((pkt->pkt_state & STATE_GOT_TARGET) && 9955 ((pkt->pkt_statistics & (STAT_BUS_RESET | STAT_DEV_RESET | 9956 STAT_ABORTED)) == 0)) { 9957 9958 /* 9959 * If we haven't reserved the drive don't reset it. 9960 */ 9961 if ((un->un_rsvd_status & 9962 (ST_RESERVE | ST_APPLICATION_RESERVATIONS)) == 0) { 9963 return (rval); 9964 } 9965 9966 /* 9967 * if we aren't lost yet we will be soon. 9968 */ 9969 un->un_pos.pmode = invalid; 9970 9971 result = st_reset(un, RESET_LUN); 9972 9973 if ((result == 0) && (un->un_state >= ST_STATE_OPEN)) { 9974 /* no hope left to recover */ 9975 scsi_log(ST_DEVINFO, st_label, CE_WARN, 9976 "recovery by resets failed\n"); 9977 return (rval); 9978 } 9979 } 9980 } 9981 9982 9983 if (rinfo->pkt_retry_cnt++ < st_retry_count) { 9984 if (un->un_pwr_mgmt == ST_PWR_SUSPENDED) { 9985 rval = QUE_COMMAND; 9986 } else if (bp == un->un_sbufp) { 9987 if (rinfo->privatelen == sizeof (recov_info)) { 9988 if (rinfo->cmd_attrib->retriable) { 9989 /* 9990 * These commands can be rerun 9991 * with impunity 9992 */ 9993 rval = QUE_COMMAND; 9994 } 9995 } else { 9996 cmd_attribute const *attrib; 9997 attrib = 9998 st_lookup_cmd_attribute(pkt->pkt_cdbp[0]); 9999 if (attrib->retriable) { 10000 rval = QUE_COMMAND; 10001 } 10002 } 10003 } 10004 } else { 10005 rval = COMMAND_DONE_ERROR; 10006 } 10007 10008 if (un->un_state >= ST_STATE_OPEN) { 10009 scsi_log(ST_DEVINFO, st_label, CE_WARN, 10010 fail, scsi_rname(pkt->pkt_reason), 10011 (rval == COMMAND_DONE_ERROR)? 10012 "giving up" : "retrying command"); 10013 } 10014 return (rval); 10015 } 10016 10017 /* 10018 * if the device is busy, then put this bp back on the waitq, on the 10019 * interrupt thread, where we want the head of the queue and not the 10020 * end 10021 * 10022 * The callers of this routine should take measures to save the 10023 * un_throttle in un_last_throttle which will be restored in 10024 * st_intr_restart(). The only exception should be st_intr_restart() 10025 * calling this routine for which the saving is already done. 10026 */ 10027 static int 10028 st_handle_intr_busy(struct scsi_tape *un, struct buf *bp, 10029 clock_t timeout_interval) 10030 { 10031 10032 int queued; 10033 int rval = 0; 10034 pkt_info *pktinfo = BP_PKT(bp)->pkt_private; 10035 10036 mutex_enter(ST_MUTEX); 10037 10038 ST_FUNC(ST_DEVINFO, st_handle_intr_busy); 10039 10040 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 10041 "st_handle_intr_busy(), un = 0x%p\n", (void *)un); 10042 10043 if ((bp != un->un_sbufp) && (bp != un->un_recov_buf)) { 10044 queued = 1; 10045 } else { 10046 queued = 0; 10047 } 10048 10049 /* 10050 * Check to see if we hit the retry timeout. We check to make sure 10051 * this is the first one on the runq and make sure we have not 10052 * queued up any more, so this one has to be the last on the list 10053 * also. If it is not, we have to fail. If it is not the first, but 10054 * is the last we are in trouble anyway, as we are in the interrupt 10055 * context here. 10056 */ 10057 if ((pktinfo->str_retry_cnt++ > st_retry_count) || 10058 ((un->un_runqf != bp) && (un->un_runql != bp) && (queued))) { 10059 rval = -1; 10060 goto exit; 10061 } 10062 10063 /* put the bp back on the waitq */ 10064 if (queued) { 10065 (void) st_remove_from_queue(&un->un_runqf, &un->un_runql, bp); 10066 st_add_to_queue(&un->un_quef, &un->un_quel, un->un_quef, bp); 10067 } 10068 10069 /* 10070 * We don't want any other commands being started in the mean time. 10071 * If start had just released mutex after putting something on the 10072 * runq, we won't even get here. 10073 */ 10074 un->un_throttle = 0; 10075 10076 /* 10077 * send a marker pkt, if appropriate 10078 */ 10079 st_hba_unflush(un); 10080 10081 /* 10082 * all queues are aligned, we are just waiting to 10083 * transport 10084 */ 10085 un->un_hib_tid = timeout(st_intr_restart, un, timeout_interval); 10086 10087 exit: 10088 mutex_exit(ST_MUTEX); 10089 return (rval); 10090 } 10091 10092 /* 10093 * To get one error entry from error stack 10094 */ 10095 static int 10096 st_get_error_entry(struct scsi_tape *un, intptr_t arg, int flag) 10097 { 10098 #ifdef _MULTI_DATAMODEL 10099 /* 10100 * For use when a 32 bit app makes a call into a 10101 * 64 bit ioctl 10102 */ 10103 struct mterror_entry32 err_entry32; 10104 #endif /* _MULTI_DATAMODEL */ 10105 10106 int rval = 0; 10107 struct mterror_entry err_entry; 10108 struct mterror_entry_stack *err_link_entry_p; 10109 size_t arq_status_len_in, arq_status_len_kr; 10110 10111 ST_FUNC(ST_DEVINFO, st_get_error_entry); 10112 10113 ASSERT(mutex_owned(ST_MUTEX)); 10114 10115 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 10116 "st_get_error_entry()\n"); 10117 10118 /* 10119 * if error record stack empty, return ENXIO 10120 */ 10121 if (un->un_error_entry_stk == NULL) { 10122 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 10123 "st_get_error_entry: Error Entry Stack Empty!\n"); 10124 rval = ENXIO; 10125 goto ret; 10126 } 10127 10128 /* 10129 * get the top entry from stack 10130 */ 10131 err_link_entry_p = un->un_error_entry_stk; 10132 arq_status_len_kr = 10133 err_link_entry_p->mtees_entry.mtee_arq_status_len; 10134 10135 #ifdef _MULTI_DATAMODEL 10136 switch (ddi_model_convert_from(flag & FMODELS)) { 10137 case DDI_MODEL_ILP32: 10138 if (ddi_copyin((void *)arg, &err_entry32, 10139 MTERROR_ENTRY_SIZE_32, flag)) { 10140 rval = EFAULT; 10141 goto ret; 10142 } 10143 10144 arq_status_len_in = 10145 (size_t)err_entry32.mtee_arq_status_len; 10146 10147 err_entry32.mtee_cdb_len = 10148 (size32_t)err_link_entry_p->mtees_entry.mtee_cdb_len; 10149 10150 if (arq_status_len_in > arq_status_len_kr) 10151 err_entry32.mtee_arq_status_len = 10152 (size32_t)arq_status_len_kr; 10153 10154 if (ddi_copyout( 10155 err_link_entry_p->mtees_entry.mtee_cdb_buf, 10156 (void *)(uintptr_t)err_entry32.mtee_cdb_buf, 10157 err_entry32.mtee_cdb_len, flag)) { 10158 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 10159 "st_get_error_entry: Copy cdb buffer error!"); 10160 rval = EFAULT; 10161 } 10162 10163 if (ddi_copyout( 10164 err_link_entry_p->mtees_entry.mtee_arq_status, 10165 (void *)(uintptr_t)err_entry32.mtee_arq_status, 10166 err_entry32.mtee_arq_status_len, flag)) { 10167 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 10168 "st_get_error_entry: copy arq status error!"); 10169 rval = EFAULT; 10170 } 10171 10172 if (ddi_copyout(&err_entry32, (void *)arg, 10173 MTERROR_ENTRY_SIZE_32, flag)) { 10174 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 10175 "st_get_error_entry: copy arq status out error!"); 10176 rval = EFAULT; 10177 } 10178 break; 10179 10180 case DDI_MODEL_NONE: 10181 if (ddi_copyin((void *)arg, &err_entry, 10182 MTERROR_ENTRY_SIZE_64, flag)) { 10183 rval = EFAULT; 10184 goto ret; 10185 } 10186 arq_status_len_in = err_entry.mtee_arq_status_len; 10187 10188 err_entry.mtee_cdb_len = 10189 err_link_entry_p->mtees_entry.mtee_cdb_len; 10190 10191 if (arq_status_len_in > arq_status_len_kr) 10192 err_entry.mtee_arq_status_len = 10193 arq_status_len_kr; 10194 10195 if (ddi_copyout( 10196 err_link_entry_p->mtees_entry.mtee_cdb_buf, 10197 err_entry.mtee_cdb_buf, 10198 err_entry.mtee_cdb_len, flag)) { 10199 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 10200 "st_get_error_entry: Copy cdb buffer error!"); 10201 rval = EFAULT; 10202 } 10203 10204 if (ddi_copyout( 10205 err_link_entry_p->mtees_entry.mtee_arq_status, 10206 err_entry.mtee_arq_status, 10207 err_entry.mtee_arq_status_len, flag)) { 10208 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 10209 "st_get_error_entry: copy arq status error!"); 10210 rval = EFAULT; 10211 } 10212 10213 if (ddi_copyout(&err_entry, (void *)arg, 10214 MTERROR_ENTRY_SIZE_64, flag)) { 10215 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 10216 "st_get_error_entry: copy arq status out error!"); 10217 rval = EFAULT; 10218 } 10219 break; 10220 } 10221 #else /* _MULTI_DATAMODEL */ 10222 if (ddi_copyin((void *)arg, &err_entry, 10223 MTERROR_ENTRY_SIZE_64, flag)) { 10224 rval = EFAULT; 10225 goto ret; 10226 } 10227 arq_status_len_in = err_entry.mtee_arq_status_len; 10228 10229 err_entry.mtee_cdb_len = 10230 err_link_entry_p->mtees_entry.mtee_cdb_len; 10231 10232 if (arq_status_len_in > arq_status_len_kr) 10233 err_entry.mtee_arq_status_len = 10234 arq_status_len_kr; 10235 10236 if (ddi_copyout( 10237 err_link_entry_p->mtees_entry.mtee_cdb_buf, 10238 err_entry.mtee_cdb_buf, 10239 err_entry.mtee_cdb_len, flag)) { 10240 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 10241 "st_get_error_entry: Copy cdb buffer error!"); 10242 rval = EFAULT; 10243 } 10244 10245 if (ddi_copyout( 10246 err_link_entry_p->mtees_entry.mtee_arq_status, 10247 err_entry.mtee_arq_status, 10248 err_entry.mtee_arq_status_len, flag)) { 10249 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 10250 "st_get_error_entry: copy arq status buffer error!"); 10251 rval = EFAULT; 10252 } 10253 10254 if (ddi_copyout(&err_entry, (void *)arg, 10255 MTERROR_ENTRY_SIZE_64, flag)) { 10256 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 10257 "st_get_error_entry: copy arq status out error!"); 10258 rval = EFAULT; 10259 } 10260 #endif /* _MULTI_DATAMODEL */ 10261 10262 /* 10263 * update stack 10264 */ 10265 un->un_error_entry_stk = err_link_entry_p->mtees_nextp; 10266 10267 kmem_free(err_link_entry_p->mtees_entry.mtee_cdb_buf, 10268 err_link_entry_p->mtees_entry.mtee_cdb_len); 10269 err_link_entry_p->mtees_entry.mtee_cdb_buf = NULL; 10270 10271 kmem_free(err_link_entry_p->mtees_entry.mtee_arq_status, 10272 SECMDS_STATUS_SIZE); 10273 err_link_entry_p->mtees_entry.mtee_arq_status = NULL; 10274 10275 kmem_free(err_link_entry_p, MTERROR_LINK_ENTRY_SIZE); 10276 err_link_entry_p = NULL; 10277 ret: 10278 return (rval); 10279 } 10280 10281 /* 10282 * MTIOCGETERROR ioctl needs to retrieve the current sense data along with 10283 * the scsi CDB command which causes the error and generates sense data and 10284 * the scsi status. 10285 * 10286 * error-record stack 10287 * 10288 * 10289 * TOP BOTTOM 10290 * ------------------------------------------ 10291 * | 0 | 1 | 2 | ... | n | 10292 * ------------------------------------------ 10293 * ^ 10294 * | 10295 * pointer to error entry 10296 * 10297 * when st driver generates one sense data record, it creates a error-entry 10298 * and pushes it onto the stack. 10299 * 10300 */ 10301 10302 static void 10303 st_update_error_stack(struct scsi_tape *un, 10304 struct scsi_pkt *pkt, 10305 struct scsi_arq_status *cmd) 10306 { 10307 struct mterror_entry_stack *err_entry_tmp; 10308 uchar_t *cdbp = (uchar_t *)pkt->pkt_cdbp; 10309 size_t cdblen = scsi_cdb_size[CDB_GROUPID(cdbp[0])]; 10310 10311 ST_FUNC(ST_DEVINFO, st_update_error_stack); 10312 10313 ASSERT(mutex_owned(ST_MUTEX)); 10314 10315 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 10316 "st_update_error_stack()\n"); 10317 10318 ASSERT(cmd); 10319 ASSERT(cdbp); 10320 if (cdblen == 0) { 10321 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 10322 "st_update_error_stack: CDB length error!\n"); 10323 return; 10324 } 10325 10326 err_entry_tmp = kmem_alloc(MTERROR_LINK_ENTRY_SIZE, KM_SLEEP); 10327 ASSERT(err_entry_tmp != NULL); 10328 10329 err_entry_tmp->mtees_entry.mtee_cdb_buf = 10330 kmem_alloc(cdblen, KM_SLEEP); 10331 ASSERT(err_entry_tmp->mtees_entry.mtee_cdb_buf != NULL); 10332 10333 err_entry_tmp->mtees_entry.mtee_arq_status = 10334 kmem_alloc(SECMDS_STATUS_SIZE, KM_SLEEP); 10335 ASSERT(err_entry_tmp->mtees_entry.mtee_arq_status != NULL); 10336 10337 /* 10338 * copy cdb command & length to current error entry 10339 */ 10340 err_entry_tmp->mtees_entry.mtee_cdb_len = cdblen; 10341 bcopy(cdbp, err_entry_tmp->mtees_entry.mtee_cdb_buf, cdblen); 10342 10343 /* 10344 * copy scsi status length to current error entry 10345 */ 10346 err_entry_tmp->mtees_entry.mtee_arq_status_len = 10347 SECMDS_STATUS_SIZE; 10348 10349 /* 10350 * copy sense data and scsi status to current error entry 10351 */ 10352 bcopy(cmd, err_entry_tmp->mtees_entry.mtee_arq_status, 10353 SECMDS_STATUS_SIZE); 10354 10355 err_entry_tmp->mtees_nextp = un->un_error_entry_stk; 10356 un->un_error_entry_stk = err_entry_tmp; 10357 10358 } 10359 10360 /* 10361 * Empty all the error entry in stack 10362 */ 10363 static void 10364 st_empty_error_stack(struct scsi_tape *un) 10365 { 10366 struct mterror_entry_stack *linkp; 10367 10368 ST_FUNC(ST_DEVINFO, st_empty_error_stack); 10369 10370 ASSERT(mutex_owned(ST_MUTEX)); 10371 10372 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 10373 "st_empty_entry_stack()\n"); 10374 10375 while (un->un_error_entry_stk != NULL) { 10376 linkp = un->un_error_entry_stk; 10377 un->un_error_entry_stk = 10378 un->un_error_entry_stk->mtees_nextp; 10379 10380 if (linkp->mtees_entry.mtee_cdb_buf != NULL) 10381 kmem_free(linkp->mtees_entry.mtee_cdb_buf, 10382 linkp->mtees_entry.mtee_cdb_len); 10383 10384 if (linkp->mtees_entry.mtee_arq_status != NULL) 10385 kmem_free(linkp->mtees_entry.mtee_arq_status, 10386 linkp->mtees_entry.mtee_arq_status_len); 10387 10388 kmem_free(linkp, MTERROR_LINK_ENTRY_SIZE); 10389 linkp = NULL; 10390 } 10391 } 10392 10393 static errstate 10394 st_handle_sense(struct scsi_tape *un, struct buf *bp, tapepos_t *pos) 10395 { 10396 struct scsi_pkt *pkt = BP_PKT(bp); 10397 struct scsi_pkt *rqpkt = un->un_rqs; 10398 struct scsi_arq_status arqstat; 10399 recov_info *rcif = pkt->pkt_private; 10400 10401 errstate rval = COMMAND_DONE_ERROR; 10402 int amt; 10403 10404 ST_FUNC(ST_DEVINFO, st_handle_sense); 10405 10406 ASSERT(mutex_owned(ST_MUTEX)); 10407 10408 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 10409 "st_handle_sense()\n"); 10410 10411 if (SCBP(rqpkt)->sts_busy) { 10412 if (rcif->privatelen == sizeof (recov_info)) { 10413 ST_RECOV(ST_DEVINFO, st_label, CE_WARN, 10414 "Attempt recovery of busy unit on request sense\n"); 10415 rval = ATTEMPT_RETRY; 10416 } else if (rcif->pkt_retry_cnt++ < st_retry_count) { 10417 ST_DEBUG4(ST_DEVINFO, st_label, CE_WARN, 10418 "Retry busy unit on request sense\n"); 10419 rval = QUE_BUSY_COMMAND; 10420 } 10421 return (rval); 10422 } else if (SCBP(rqpkt)->sts_chk) { 10423 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 10424 "Check Condition on REQUEST SENSE\n"); 10425 return (rval); 10426 } 10427 10428 /* 10429 * Make sure there is sense data to look at. 10430 */ 10431 if ((rqpkt->pkt_state & (STATE_GOT_BUS | STATE_GOT_TARGET | 10432 STATE_SENT_CMD | STATE_GOT_STATUS)) != (STATE_GOT_BUS | 10433 STATE_GOT_TARGET | STATE_SENT_CMD | STATE_GOT_STATUS)) { 10434 return (rval); 10435 } 10436 10437 /* was there enough data? */ 10438 amt = (int)MAX_SENSE_LENGTH - rqpkt->pkt_resid; 10439 if ((rqpkt->pkt_state & STATE_XFERRED_DATA) == 0 || 10440 (amt < SUN_MIN_SENSE_LENGTH)) { 10441 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 10442 "REQUEST SENSE couldn't get sense data\n"); 10443 return (rval); 10444 } 10445 10446 bcopy(SCBP(pkt), &arqstat.sts_status, 10447 sizeof (struct scsi_status)); 10448 bcopy(SCBP(rqpkt), &arqstat.sts_rqpkt_status, 10449 sizeof (struct scsi_status)); 10450 arqstat.sts_rqpkt_reason = rqpkt->pkt_reason; 10451 arqstat.sts_rqpkt_resid = rqpkt->pkt_resid; 10452 arqstat.sts_rqpkt_state = rqpkt->pkt_state; 10453 arqstat.sts_rqpkt_statistics = rqpkt->pkt_statistics; 10454 bcopy(ST_RQSENSE, &arqstat.sts_sensedata, SENSE_LENGTH); 10455 10456 /* 10457 * copy one arqstat entry in the sense data buffer 10458 */ 10459 st_update_error_stack(un, pkt, &arqstat); 10460 return (st_decode_sense(un, bp, amt, &arqstat, pos)); 10461 } 10462 10463 static errstate 10464 st_handle_autosense(struct scsi_tape *un, struct buf *bp, tapepos_t *pos) 10465 { 10466 struct scsi_pkt *pkt = BP_PKT(bp); 10467 struct scsi_arq_status *arqstat = 10468 (struct scsi_arq_status *)pkt->pkt_scbp; 10469 errstate rval = COMMAND_DONE_ERROR; 10470 int amt; 10471 10472 ST_FUNC(ST_DEVINFO, st_handle_autosense); 10473 10474 ASSERT(mutex_owned(ST_MUTEX)); 10475 10476 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 10477 "st_handle_autosense()\n"); 10478 10479 if (arqstat->sts_rqpkt_status.sts_busy) { 10480 ST_DEBUG4(ST_DEVINFO, st_label, CE_WARN, 10481 "busy unit on request sense\n"); 10482 /* 10483 * we return QUE_SENSE so st_intr will setup the SENSE cmd. 10484 * the disadvantage is that we do not have any delay for the 10485 * second retry of rqsense and we have to keep a packet around 10486 */ 10487 return (QUE_SENSE); 10488 10489 } else if (arqstat->sts_rqpkt_reason != CMD_CMPLT) { 10490 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 10491 "transport error on REQUEST SENSE\n"); 10492 if ((arqstat->sts_rqpkt_state & STATE_GOT_TARGET) && 10493 ((arqstat->sts_rqpkt_statistics & 10494 (STAT_BUS_RESET | STAT_DEV_RESET | STAT_ABORTED)) == 0)) { 10495 if (st_reset(un, RESET_LUN) == 0) { 10496 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 10497 "recovery by resets failed\n"); 10498 } 10499 } 10500 return (rval); 10501 10502 } else if (arqstat->sts_rqpkt_status.sts_chk) { 10503 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 10504 "Check Condition on REQUEST SENSE\n"); 10505 return (rval); 10506 } 10507 10508 10509 /* was there enough data? */ 10510 if (pkt->pkt_state & STATE_XARQ_DONE) { 10511 amt = (int)MAX_SENSE_LENGTH - arqstat->sts_rqpkt_resid; 10512 } else { 10513 if (arqstat->sts_rqpkt_resid > SENSE_LENGTH) { 10514 amt = (int)MAX_SENSE_LENGTH - arqstat->sts_rqpkt_resid; 10515 } else { 10516 amt = (int)SENSE_LENGTH - arqstat->sts_rqpkt_resid; 10517 } 10518 } 10519 if ((arqstat->sts_rqpkt_state & STATE_XFERRED_DATA) == 0 || 10520 (amt < SUN_MIN_SENSE_LENGTH)) { 10521 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 10522 "REQUEST SENSE couldn't get sense data\n"); 10523 return (rval); 10524 } 10525 10526 if (pkt->pkt_state & STATE_XARQ_DONE) { 10527 bcopy(&arqstat->sts_sensedata, ST_RQSENSE, MAX_SENSE_LENGTH); 10528 } else { 10529 bcopy(&arqstat->sts_sensedata, ST_RQSENSE, SENSE_LENGTH); 10530 } 10531 10532 /* 10533 * copy one arqstat entry in the sense data buffer 10534 */ 10535 st_update_error_stack(un, pkt, arqstat); 10536 10537 return (st_decode_sense(un, bp, amt, arqstat, pos)); 10538 } 10539 10540 static errstate 10541 st_decode_sense(struct scsi_tape *un, struct buf *bp, int amt, 10542 struct scsi_arq_status *statusp, tapepos_t *pos) 10543 { 10544 struct scsi_pkt *pkt = BP_PKT(bp); 10545 recov_info *ri = pkt->pkt_private; 10546 errstate rval = COMMAND_DONE_ERROR; 10547 cmd_attribute const *attrib; 10548 long resid; 10549 struct scsi_extended_sense *sensep = ST_RQSENSE; 10550 int severity; 10551 int get_error; 10552 10553 ST_FUNC(ST_DEVINFO, st_decode_sense); 10554 10555 ASSERT(mutex_owned(ST_MUTEX)); 10556 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 10557 "st_decode_sense()\n"); 10558 10559 /* 10560 * For uscsi commands, squirrel away a copy of the 10561 * results of the Request Sense. 10562 */ 10563 if (USCSI_CMD(bp)) { 10564 struct uscsi_cmd *ucmd = BP_UCMD(bp); 10565 ucmd->uscsi_rqstatus = *(uchar_t *)statusp; 10566 if (ucmd->uscsi_rqlen && un->un_srqbufp) { 10567 uchar_t rqlen = min((uchar_t)amt, ucmd->uscsi_rqlen); 10568 ucmd->uscsi_rqresid = ucmd->uscsi_rqlen - rqlen; 10569 bcopy(ST_RQSENSE, un->un_srqbufp, rqlen); 10570 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 10571 "st_decode_sense: stat=0x%x resid=0x%x\n", 10572 ucmd->uscsi_rqstatus, ucmd->uscsi_rqresid); 10573 } 10574 } 10575 10576 if (ri->privatelen == sizeof (recov_info)) { 10577 attrib = ri->cmd_attrib; 10578 } else { 10579 attrib = st_lookup_cmd_attribute(pkt->pkt_cdbp[0]); 10580 } 10581 10582 /* 10583 * If the drive is an MT-02, reposition the 10584 * secondary error code into the proper place. 10585 * 10586 * XXX MT-02 is non-CCS tape, so secondary error code 10587 * is in byte 8. However, in SCSI-2, tape has CCS definition 10588 * so it's in byte 12. 10589 */ 10590 if (un->un_dp->type == ST_TYPE_EMULEX) { 10591 sensep->es_code = sensep->es_add_info[0]; 10592 } 10593 10594 ST_CDB(ST_DEVINFO, "st_decode_sense failed CDB", 10595 (caddr_t)&CDBP(pkt)->scc_cmd); 10596 10597 ST_SENSE(ST_DEVINFO, "st_decode_sense sense data", (caddr_t)statusp, 10598 sizeof (*statusp)); 10599 10600 /* for normal I/O check extract the resid values. */ 10601 if (bp != un->un_sbufp && bp != un->un_recov_buf) { 10602 if (sensep->es_valid) { 10603 resid = 10604 (sensep->es_info_1 << 24) | 10605 (sensep->es_info_2 << 16) | 10606 (sensep->es_info_3 << 8) | 10607 (sensep->es_info_4); 10608 /* If fixed block */ 10609 if (un->un_bsize) { 10610 resid *= un->un_bsize; 10611 } 10612 } else if (pkt->pkt_state & STATE_XFERRED_DATA) { 10613 resid = pkt->pkt_resid; 10614 } else { 10615 resid = bp->b_bcount; 10616 } 10617 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 10618 "st_decode_sense (rw): xferred bit = %d, resid=%ld (%d), " 10619 "pkt_resid=%ld\n", pkt->pkt_state & STATE_XFERRED_DATA, 10620 resid, 10621 (sensep->es_info_1 << 24) | 10622 (sensep->es_info_2 << 16) | 10623 (sensep->es_info_3 << 8) | 10624 (sensep->es_info_4), 10625 pkt->pkt_resid); 10626 /* 10627 * The problem is, what should we believe? 10628 */ 10629 if (resid && (pkt->pkt_resid == 0)) { 10630 pkt->pkt_resid = resid; 10631 } 10632 } else { 10633 /* 10634 * If the command is SCMD_SPACE, we need to get the 10635 * residual as returned in the sense data, to adjust 10636 * our idea of current tape position correctly 10637 */ 10638 if ((sensep->es_valid) && 10639 (CDBP(pkt)->scc_cmd == SCMD_LOCATE) || 10640 (CDBP(pkt)->scc_cmd == SCMD_LOCATE_G4) || 10641 (CDBP(pkt)->scc_cmd == SCMD_SPACE) || 10642 (CDBP(pkt)->scc_cmd == SCMD_SPACE_G4) || 10643 (CDBP(pkt)->scc_cmd == SCMD_WRITE_FILE_MARK)) { 10644 resid = 10645 (sensep->es_info_1 << 24) | 10646 (sensep->es_info_2 << 16) | 10647 (sensep->es_info_3 << 8) | 10648 (sensep->es_info_4); 10649 bp->b_resid = resid; 10650 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 10651 "st_decode_sense(other): resid=%ld\n", resid); 10652 } else { 10653 /* 10654 * If the special command is SCMD_READ, 10655 * the correct resid will be set later. 10656 */ 10657 if (attrib->get_cnt != NULL) { 10658 resid = attrib->get_cnt(pkt->pkt_cdbp); 10659 } else { 10660 resid = bp->b_bcount; 10661 } 10662 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 10663 "st_decode_sense(special read): resid=%ld\n", 10664 resid); 10665 } 10666 } 10667 10668 if ((un->un_state >= ST_STATE_OPEN) && 10669 (DEBUGGING || st_error_level == SCSI_ERR_ALL)) { 10670 st_print_cdb(ST_DEVINFO, st_label, CE_NOTE, 10671 "Failed CDB", (char *)pkt->pkt_cdbp); 10672 st_clean_print(ST_DEVINFO, st_label, CE_CONT, 10673 "sense data", (char *)sensep, amt); 10674 scsi_log(ST_DEVINFO, st_label, CE_CONT, 10675 "count 0x%lx resid 0x%lx pktresid 0x%lx\n", 10676 bp->b_bcount, resid, pkt->pkt_resid); 10677 } 10678 10679 switch (un->un_status = sensep->es_key) { 10680 case KEY_NO_SENSE: 10681 severity = SCSI_ERR_INFO; 10682 10683 /* 10684 * Erase, locate or rewind operation in progress, retry 10685 * ASC ASCQ 10686 * 00 18 Erase operation in progress 10687 * 00 19 Locate operation in progress 10688 * 00 1A Rewind operation in progress 10689 */ 10690 if (sensep->es_add_code == 0 && 10691 ((sensep->es_qual_code == 0x18) || 10692 (sensep->es_qual_code == 0x19) || 10693 (sensep->es_qual_code == 0x1a))) { 10694 rval = QUE_BUSY_COMMAND; 10695 break; 10696 } 10697 10698 goto common; 10699 10700 case KEY_RECOVERABLE_ERROR: 10701 severity = SCSI_ERR_RECOVERED; 10702 if ((sensep->es_class == CLASS_EXTENDED_SENSE) && 10703 (sensep->es_code == ST_DEFERRED_ERROR)) { 10704 if (un->un_dp->options & 10705 ST_RETRY_ON_RECOVERED_DEFERRED_ERROR) { 10706 rval = QUE_LAST_COMMAND; 10707 scsi_errmsg(ST_SCSI_DEVP, pkt, st_label, 10708 severity, pos->lgclblkno, 10709 un->un_err_pos.lgclblkno, scsi_cmds, 10710 sensep); 10711 scsi_log(ST_DEVINFO, st_label, CE_CONT, 10712 "Command will be retried\n"); 10713 } else { 10714 severity = SCSI_ERR_FATAL; 10715 rval = COMMAND_DONE_ERROR_RECOVERED; 10716 ST_DO_ERRSTATS(un, st_softerrs); 10717 scsi_errmsg(ST_SCSI_DEVP, pkt, st_label, 10718 severity, pos->lgclblkno, 10719 un->un_err_pos.lgclblkno, scsi_cmds, 10720 sensep); 10721 } 10722 break; 10723 } 10724 common: 10725 /* 10726 * XXX only want reads to be stopped by filemarks. 10727 * Don't want them to be stopped by EOT. EOT matters 10728 * only on write. 10729 */ 10730 if (sensep->es_filmk && !sensep->es_eom) { 10731 rval = COMMAND_DONE; 10732 } else if (sensep->es_eom) { 10733 rval = COMMAND_DONE; 10734 } else if (sensep->es_ili) { 10735 /* 10736 * Fun with variable length record devices: 10737 * for specifying larger blocks sizes than the 10738 * actual physical record size. 10739 */ 10740 if (un->un_bsize == 0 && resid > 0) { 10741 /* 10742 * XXX! Ugly. 10743 * The requested blocksize is > tape blocksize, 10744 * so this is ok, so we just return the 10745 * actual size xferred. 10746 */ 10747 pkt->pkt_resid = resid; 10748 rval = COMMAND_DONE; 10749 } else if (un->un_bsize == 0 && resid < 0) { 10750 /* 10751 * The requested blocksize is < tape blocksize, 10752 * so this is not ok, so we err with ENOMEM 10753 */ 10754 rval = COMMAND_DONE_ERROR_RECOVERED; 10755 st_bioerror(bp, ENOMEM); 10756 } else { 10757 ST_DO_ERRSTATS(un, st_softerrs); 10758 severity = SCSI_ERR_FATAL; 10759 rval = COMMAND_DONE_ERROR; 10760 st_bioerror(bp, EINVAL); 10761 un->un_running.pmode = invalid; 10762 } 10763 } else { 10764 /* 10765 * we hope and pray for this just being 10766 * something we can ignore (ie. a 10767 * truly recoverable soft error) 10768 */ 10769 rval = COMMAND_DONE; 10770 } 10771 if (sensep->es_filmk) { 10772 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 10773 "filemark\n"); 10774 un->un_status = SUN_KEY_EOF; 10775 pos->eof = ST_EOF_PENDING; 10776 st_set_pe_flag(un); 10777 } 10778 10779 /* 10780 * ignore eom when reading, a fmk should terminate reading 10781 */ 10782 if ((sensep->es_eom) && 10783 (CDBP(pkt)->scc_cmd != SCMD_READ)) { 10784 if ((sensep->es_add_code == 0) && 10785 (sensep->es_qual_code == 4)) { 10786 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 10787 "bot\n"); 10788 un->un_status = SUN_KEY_BOT; 10789 pos->eof = ST_NO_EOF; 10790 pos->lgclblkno = 0; 10791 pos->fileno = 0; 10792 pos->blkno = 0; 10793 if (pos->pmode != legacy) 10794 pos->pmode = legacy; 10795 } else { 10796 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 10797 "eom\n"); 10798 un->un_status = SUN_KEY_EOT; 10799 pos->eof = ST_EOM; 10800 } 10801 st_set_pe_flag(un); 10802 } 10803 10804 break; 10805 10806 case KEY_ILLEGAL_REQUEST: 10807 10808 if (un->un_laststate >= ST_STATE_OPEN) { 10809 ST_DO_ERRSTATS(un, st_softerrs); 10810 severity = SCSI_ERR_FATAL; 10811 } else { 10812 severity = SCSI_ERR_INFO; 10813 } 10814 break; 10815 10816 case KEY_MEDIUM_ERROR: 10817 ST_DO_ERRSTATS(un, st_harderrs); 10818 severity = SCSI_ERR_FATAL; 10819 check_keys: 10820 /* 10821 * attempt to process the keys in the presence of 10822 * other errors 10823 */ 10824 if (sensep->es_ili && rval != COMMAND_DONE_ERROR) { 10825 /* 10826 * Fun with variable length record devices: 10827 * for specifying larger blocks sizes than the 10828 * actual physical record size. 10829 */ 10830 if (un->un_bsize == 0 && resid > 0) { 10831 /* 10832 * XXX! Ugly 10833 */ 10834 pkt->pkt_resid = resid; 10835 } else if (un->un_bsize == 0 && resid < 0) { 10836 st_bioerror(bp, EINVAL); 10837 } else { 10838 severity = SCSI_ERR_FATAL; 10839 rval = COMMAND_DONE_ERROR; 10840 st_bioerror(bp, EINVAL); 10841 } 10842 } 10843 if (sensep->es_filmk) { 10844 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 10845 "filemark\n"); 10846 un->un_status = SUN_KEY_EOF; 10847 pos->eof = ST_EOF_PENDING; 10848 st_set_pe_flag(un); 10849 } 10850 10851 /* 10852 * ignore eom when reading, a fmk should terminate reading 10853 */ 10854 if ((sensep->es_eom) && 10855 (CDBP(pkt)->scc_cmd != SCMD_READ)) { 10856 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "eom\n"); 10857 un->un_status = SUN_KEY_EOT; 10858 pos->eof = ST_EOM; 10859 st_set_pe_flag(un); 10860 } 10861 10862 break; 10863 10864 case KEY_VOLUME_OVERFLOW: 10865 ST_DO_ERRSTATS(un, st_softerrs); 10866 pos->eof = ST_EOM; 10867 severity = SCSI_ERR_FATAL; 10868 rval = COMMAND_DONE_ERROR; 10869 goto check_keys; 10870 10871 case KEY_HARDWARE_ERROR: 10872 ST_DO_ERRSTATS(un, st_harderrs); 10873 severity = SCSI_ERR_FATAL; 10874 rval = COMMAND_DONE_ERROR; 10875 if (un->un_dp->options & ST_EJECT_ON_CHANGER_FAILURE) 10876 un->un_eject_tape_on_failure = st_check_asc_ascq(un); 10877 break; 10878 10879 case KEY_BLANK_CHECK: 10880 ST_DO_ERRSTATS(un, st_softerrs); 10881 severity = SCSI_ERR_INFO; 10882 10883 /* 10884 * if not a special request and some data was xferred then it 10885 * it is not an error yet 10886 */ 10887 if (bp != un->un_sbufp && (bp->b_flags & B_READ)) { 10888 /* 10889 * no error for read with or without data xferred 10890 */ 10891 un->un_status = SUN_KEY_EOT; 10892 pos->eof = ST_EOT; 10893 rval = COMMAND_DONE_ERROR; 10894 un->un_running.pmode = invalid; 10895 st_set_pe_flag(un); 10896 goto check_keys; 10897 } else if (bp != un->un_sbufp && 10898 (pkt->pkt_state & STATE_XFERRED_DATA)) { 10899 rval = COMMAND_DONE; 10900 } else { 10901 rval = COMMAND_DONE_ERROR_RECOVERED; 10902 } 10903 10904 if (un->un_laststate >= ST_STATE_OPEN) { 10905 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 10906 "blank check\n"); 10907 pos->eof = ST_EOM; 10908 } 10909 if ((CDBP(pkt)->scc_cmd == SCMD_LOCATE) || 10910 (CDBP(pkt)->scc_cmd == SCMD_LOCATE_G4) || 10911 (CDBP(pkt)->scc_cmd == SCMD_SPACE) && 10912 (un->un_dp->options & ST_KNOWS_EOD)) { 10913 /* 10914 * we were doing a fast forward by skipping 10915 * multiple fmk at the time 10916 */ 10917 st_bioerror(bp, EIO); 10918 severity = SCSI_ERR_RECOVERED; 10919 rval = COMMAND_DONE; 10920 } 10921 st_set_pe_flag(un); 10922 goto check_keys; 10923 10924 case KEY_WRITE_PROTECT: 10925 if (st_wrongtapetype(un)) { 10926 un->un_status = SUN_KEY_WRONGMEDIA; 10927 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 10928 "wrong tape for writing- use DC6150 tape " 10929 "(or equivalent)\n"); 10930 severity = SCSI_ERR_UNKNOWN; 10931 } else { 10932 severity = SCSI_ERR_FATAL; 10933 } 10934 ST_DO_ERRSTATS(un, st_harderrs); 10935 rval = COMMAND_DONE_ERROR; 10936 st_bioerror(bp, EACCES); 10937 break; 10938 10939 case KEY_UNIT_ATTENTION: 10940 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 10941 "KEY_UNIT_ATTENTION : un_state = %d\n", un->un_state); 10942 10943 un->un_unit_attention_flags |= 1; 10944 /* 10945 * If we have detected a Bus Reset and the tape 10946 * drive has been reserved. 10947 */ 10948 if (ST_RQSENSE->es_add_code == 0x29) { 10949 rval = DEVICE_RESET; 10950 if ((un->un_rsvd_status & 10951 (ST_RESERVE | ST_APPLICATION_RESERVATIONS)) == 10952 ST_RESERVE) { 10953 un->un_rsvd_status |= ST_LOST_RESERVE; 10954 ST_DEBUG(ST_DEVINFO, st_label, CE_WARN, 10955 "st_decode_sense: Lost Reservation\n"); 10956 } 10957 } 10958 10959 /* 10960 * If this is a recovery command and retrable, retry. 10961 */ 10962 if (bp == un->un_recov_buf) { 10963 severity = SCSI_ERR_INFO; 10964 if (attrib->retriable && 10965 ri->pkt_retry_cnt++ < st_retry_count) { 10966 rval = QUE_COMMAND; 10967 } else { 10968 rval = COMMAND_DONE_ERROR; 10969 } 10970 break; /* Don't set position invalid */ 10971 } 10972 10973 /* 10974 * If ST_APPLICATION_RESERVATIONS is set, 10975 * If the asc/ascq indicates that the reservation 10976 * has been cleared just allow the write to continue 10977 * which would force a scsi 2 reserve. 10978 * If preempted that persistent reservation 10979 * the scsi 2 reserve would get a reservation conflict. 10980 */ 10981 if ((un->un_rsvd_status & 10982 ST_APPLICATION_RESERVATIONS) != 0) { 10983 /* 10984 * RESERVATIONS PREEMPTED 10985 * With MPxIO this could be a fail over? XXX 10986 */ 10987 if (ST_RQSENSE->es_add_code == 0x2a && 10988 ST_RQSENSE->es_qual_code == 0x03) { 10989 severity = SCSI_ERR_INFO; 10990 rval = COMMAND_DONE_ERROR; 10991 pos->pmode = invalid; 10992 break; 10993 /* 10994 * RESERVATIONS RELEASED 10995 */ 10996 } else if (ST_RQSENSE->es_add_code == 0x2a && 10997 ST_RQSENSE->es_qual_code == 0x04) { 10998 severity = SCSI_ERR_INFO; 10999 rval = COMMAND_DONE; 11000 break; 11001 } 11002 } 11003 11004 if (un->un_state <= ST_STATE_OPENING) { 11005 /* 11006 * Look, the tape isn't open yet, now determine 11007 * if the cause is a BUS RESET, Save the file 11008 * and Block positions for the callers to 11009 * recover from the loss of position. 11010 */ 11011 severity = SCSI_ERR_INFO; 11012 if ((pos->pmode != invalid) && 11013 (rval == DEVICE_RESET) && 11014 (un->un_restore_pos != 1)) { 11015 un->un_save_fileno = pos->fileno; 11016 un->un_save_blkno = pos->blkno; 11017 un->un_restore_pos = 1; 11018 } 11019 11020 if (attrib->retriable && 11021 ri->pkt_retry_cnt++ < st_retry_count) { 11022 rval = QUE_COMMAND; 11023 } else if (rval == DEVICE_RESET) { 11024 break; 11025 } else { 11026 rval = COMMAND_DONE_ERROR; 11027 } 11028 /* 11029 * Means it thinks the mode parameters have changed. 11030 * This is the result of a reset clearing settings or 11031 * another initiator changing what we set. 11032 */ 11033 } 11034 if (ST_RQSENSE->es_add_code == 0x2a) { 11035 if (ST_RQSENSE->es_qual_code == 0x1) { 11036 /* Error recovery will modeselect and retry. */ 11037 rval = DEVICE_TAMPER; 11038 severity = SCSI_ERR_INFO; 11039 break; /* don't set position invalid */ 11040 } 11041 if (ST_RQSENSE->es_qual_code == 0x0 || 11042 ST_RQSENSE->es_qual_code == 0x2 || 11043 ST_RQSENSE->es_qual_code == 0x3 || 11044 ST_RQSENSE->es_qual_code == 0x4 || 11045 ST_RQSENSE->es_qual_code == 0x5 || 11046 ST_RQSENSE->es_qual_code == 0x6 || 11047 ST_RQSENSE->es_qual_code == 0x7) { 11048 rval = DEVICE_TAMPER; 11049 severity = SCSI_ERR_INFO; 11050 } 11051 } else if (ST_RQSENSE->es_add_code == 0x28 && 11052 ((ST_RQSENSE->es_qual_code == 0x0) || 11053 ST_RQSENSE->es_qual_code == 0x5)) { 11054 /* 11055 * Not Ready to Ready change, Media may have changed. 11056 */ 11057 rval = DEVICE_TAMPER; 11058 severity = SCSI_ERR_RETRYABLE; 11059 } else { 11060 if (rval != DEVICE_RESET) { 11061 rval = COMMAND_DONE_ERROR; 11062 } else { 11063 /* 11064 * Returning DEVICE_RESET will call 11065 * error recovery. 11066 */ 11067 severity = SCSI_ERR_INFO; 11068 break; /* don't set position invalid */ 11069 } 11070 /* 11071 * Check if it is an Unexpected Unit Attention. 11072 * If state is >= ST_STATE_OPEN, we have 11073 * already done the initialization . 11074 * In this case it is Fatal Error 11075 * since no further reading/writing 11076 * can be done with fileno set to < 0. 11077 */ 11078 if (un->un_state >= ST_STATE_OPEN) { 11079 ST_DO_ERRSTATS(un, st_harderrs); 11080 severity = SCSI_ERR_FATAL; 11081 } else { 11082 severity = SCSI_ERR_INFO; 11083 } 11084 } 11085 11086 pos->pmode = invalid; 11087 11088 break; 11089 11090 case KEY_NOT_READY: 11091 /* 11092 * If in process of getting ready retry. 11093 */ 11094 if (sensep->es_add_code == 0x04) { 11095 switch (sensep->es_qual_code) { 11096 case 0x07: 11097 /* 11098 * We get here when the tape is rewinding. 11099 * QUE_BUSY_COMMAND retries every 10 seconds. 11100 */ 11101 if (ri->pkt_retry_cnt++ < 11102 (un->un_dp->rewind_timeout / 10)) { 11103 rval = QUE_BUSY_COMMAND; 11104 severity = SCSI_ERR_INFO; 11105 } else { 11106 /* give up */ 11107 rval = COMMAND_DONE_ERROR; 11108 severity = SCSI_ERR_FATAL; 11109 } 11110 break; 11111 case 0x01: 11112 if (ri->pkt_retry_cnt++ < st_retry_count) { 11113 rval = QUE_COMMAND; 11114 severity = SCSI_ERR_INFO; 11115 break; 11116 } 11117 default: /* FALLTHRU */ 11118 /* give up */ 11119 rval = COMMAND_DONE_ERROR; 11120 severity = SCSI_ERR_FATAL; 11121 } 11122 } else { 11123 /* give up */ 11124 rval = COMMAND_DONE_ERROR; 11125 severity = SCSI_ERR_FATAL; 11126 } 11127 11128 /* 11129 * If this was an error and after device opened 11130 * do error stats. 11131 */ 11132 if (rval == COMMAND_DONE_ERROR && 11133 un->un_state > ST_STATE_OPENING) { 11134 ST_DO_ERRSTATS(un, st_harderrs); 11135 } 11136 11137 if (ST_RQSENSE->es_add_code == 0x3a) { 11138 if (st_error_level >= SCSI_ERR_FATAL) 11139 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 11140 "Tape not inserted in drive\n"); 11141 un->un_mediastate = MTIO_EJECTED; 11142 cv_broadcast(&un->un_state_cv); 11143 } 11144 if ((un->un_dp->options & ST_EJECT_ON_CHANGER_FAILURE) && 11145 (rval != QUE_COMMAND)) 11146 un->un_eject_tape_on_failure = st_check_asc_ascq(un); 11147 break; 11148 11149 case KEY_ABORTED_COMMAND: 11150 /* XXX Do drives return this when they see a lost light? */ 11151 /* Testing would say yes */ 11152 11153 if (ri->pkt_retry_cnt++ < st_retry_count) { 11154 rval = ATTEMPT_RETRY; 11155 severity = SCSI_ERR_RETRYABLE; 11156 goto check_keys; 11157 } 11158 /* 11159 * Probably a parity error... 11160 * if we retry here then this may cause data to be 11161 * written twice or data skipped during reading 11162 */ 11163 ST_DO_ERRSTATS(un, st_harderrs); 11164 severity = SCSI_ERR_FATAL; 11165 rval = COMMAND_DONE_ERROR; 11166 goto check_keys; 11167 11168 default: 11169 /* 11170 * Undecoded sense key. Try retries and hope 11171 * that will fix the problem. Otherwise, we're 11172 * dead. 11173 */ 11174 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 11175 "Unhandled Sense Key '%s'\n", 11176 sense_keys[un->un_status]); 11177 ST_DO_ERRSTATS(un, st_harderrs); 11178 severity = SCSI_ERR_FATAL; 11179 rval = COMMAND_DONE_ERROR; 11180 goto check_keys; 11181 } 11182 11183 if ((!(pkt->pkt_flags & FLAG_SILENT) && 11184 un->un_state >= ST_STATE_OPEN) && (DEBUGGING || 11185 (un->un_laststate > ST_STATE_OPENING) && 11186 (severity >= st_error_level))) { 11187 11188 scsi_errmsg(ST_SCSI_DEVP, pkt, st_label, severity, 11189 pos->lgclblkno, un->un_err_pos.lgclblkno, 11190 scsi_cmds, sensep); 11191 if (sensep->es_filmk) { 11192 scsi_log(ST_DEVINFO, st_label, CE_CONT, 11193 "File Mark Detected\n"); 11194 } 11195 if (sensep->es_eom) { 11196 scsi_log(ST_DEVINFO, st_label, CE_CONT, 11197 "End-of-Media Detected\n"); 11198 } 11199 if (sensep->es_ili) { 11200 scsi_log(ST_DEVINFO, st_label, CE_CONT, 11201 "Incorrect Length Indicator Set\n"); 11202 } 11203 } 11204 get_error = geterror(bp); 11205 if (((rval == COMMAND_DONE_ERROR) || 11206 (rval == COMMAND_DONE_ERROR_RECOVERED)) && 11207 ((get_error == EIO) || (get_error == 0))) { 11208 un->un_rqs_state |= (ST_RQS_ERROR | ST_RQS_VALID); 11209 bcopy(ST_RQSENSE, un->un_uscsi_rqs_buf, SENSE_LENGTH); 11210 if (un->un_rqs_state & ST_RQS_READ) { 11211 un->un_rqs_state &= ~(ST_RQS_READ); 11212 } else { 11213 un->un_rqs_state |= ST_RQS_OVR; 11214 } 11215 } 11216 11217 return (rval); 11218 } 11219 11220 11221 static int 11222 st_handle_intr_retry_lcmd(struct scsi_tape *un, struct buf *bp) 11223 { 11224 int status = TRAN_ACCEPT; 11225 pkt_info *pktinfo = BP_PKT(bp)->pkt_private; 11226 11227 mutex_enter(ST_MUTEX); 11228 11229 ST_FUNC(ST_DEVINFO, st_handle_intr_retry_lcmd); 11230 11231 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 11232 "st_handle_intr_rtr_lcmd(), un = 0x%p\n", (void *)un); 11233 11234 /* 11235 * Check to see if we hit the retry timeout. We check to make sure 11236 * this is the first one on the runq and make sure we have not 11237 * queued up any more, so this one has to be the last on the list 11238 * also. If it is not, we have to fail. If it is not the first, but 11239 * is the last we are in trouble anyway, as we are in the interrupt 11240 * context here. 11241 */ 11242 if ((pktinfo->pkt_retry_cnt > st_retry_count) || 11243 ((un->un_runqf != bp) && (un->un_runql != bp))) { 11244 goto exit; 11245 } 11246 11247 if (un->un_throttle) { 11248 un->un_last_throttle = un->un_throttle; 11249 un->un_throttle = 0; 11250 } 11251 11252 /* 11253 * Here we know : bp is the first and last one on the runq 11254 * it is not necessary to put it back on the head of the 11255 * waitq and then move from waitq to runq. Save this queuing 11256 * and call scsi_transport. 11257 */ 11258 ST_CDB(ST_DEVINFO, "Retry lcmd CDB", (char *)BP_PKT(bp)->pkt_cdbp); 11259 11260 status = st_transport(un, BP_PKT(bp)); 11261 11262 if (status == TRAN_ACCEPT) { 11263 if (un->un_last_throttle) { 11264 un->un_throttle = un->un_last_throttle; 11265 } 11266 mutex_exit(ST_MUTEX); 11267 11268 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 11269 "restart transport \n"); 11270 return (0); 11271 } 11272 11273 ST_DO_KSTATS(bp, kstat_runq_back_to_waitq); 11274 mutex_exit(ST_MUTEX); 11275 11276 if (status == TRAN_BUSY) { 11277 if (st_handle_intr_busy(un, bp, ST_TRAN_BUSY_TIMEOUT) == 0) { 11278 return (0); 11279 } 11280 } 11281 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 11282 "restart transport rejected\n"); 11283 mutex_enter(ST_MUTEX); 11284 ST_DO_ERRSTATS(un, st_transerrs); 11285 if (un->un_last_throttle) { 11286 un->un_throttle = un->un_last_throttle; 11287 } 11288 exit: 11289 mutex_exit(ST_MUTEX); 11290 return (-1); 11291 } 11292 11293 static int 11294 st_wrongtapetype(struct scsi_tape *un) 11295 { 11296 11297 ST_FUNC(ST_DEVINFO, st_wrongtapetype); 11298 11299 ASSERT(mutex_owned(ST_MUTEX)); 11300 11301 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, "st_wrongtapetype()\n"); 11302 11303 /* 11304 * Hack to handle 600A, 600XTD, 6150 && 660 vs. 300XL tapes... 11305 */ 11306 if (un->un_dp && (un->un_dp->options & ST_QIC) && un->un_mspl) { 11307 switch (un->un_dp->type) { 11308 case ST_TYPE_WANGTEK: 11309 case ST_TYPE_ARCHIVE: 11310 /* 11311 * If this really worked, we could go off of 11312 * the density codes set in the modesense 11313 * page. For this drive, 0x10 == QIC-120, 11314 * 0xf == QIC-150, and 0x5 should be for 11315 * both QIC-24 and, maybe, QIC-11. However, 11316 * the h/w doesn't do what the manual says 11317 * that it should, so we'll key off of 11318 * getting a WRITE PROTECT error AND wp *not* 11319 * set in the mode sense information. 11320 */ 11321 /* 11322 * XXX but we already know that status is 11323 * write protect, so don't check it again. 11324 */ 11325 11326 if (un->un_status == KEY_WRITE_PROTECT && 11327 un->un_mspl->wp == 0) { 11328 return (1); 11329 } 11330 break; 11331 default: 11332 break; 11333 } 11334 } 11335 return (0); 11336 } 11337 11338 static errstate 11339 st_check_error(struct scsi_tape *un, struct scsi_pkt *pkt) 11340 { 11341 errstate action; 11342 recov_info *rcvi = pkt->pkt_private; 11343 buf_t *bp = rcvi->cmd_bp; 11344 struct scsi_arq_status *stat = (struct scsi_arq_status *)pkt->pkt_scbp; 11345 11346 ST_FUNC(ST_DEVINFO, st_check_error); 11347 11348 ASSERT(mutex_owned(ST_MUTEX)); 11349 11350 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, "st_check_error()\n"); 11351 11352 switch (SCBP_C(pkt)) { 11353 case STATUS_RESERVATION_CONFLICT: 11354 /* 11355 * Command recovery is enabled, not just opening, 11356 * we had the drive reserved and we thing its ours. 11357 * Call recovery to attempt to take it back. 11358 */ 11359 if ((rcvi->privatelen == sizeof (recov_info)) && 11360 (bp != un->un_recov_buf) && 11361 (un->un_state > ST_STATE_OPEN_PENDING_IO) && 11362 ((un->un_rsvd_status & (ST_RESERVE | 11363 ST_APPLICATION_RESERVATIONS)) != 0)) { 11364 action = ATTEMPT_RETRY; 11365 un->un_rsvd_status |= ST_LOST_RESERVE; 11366 } else { 11367 action = COMMAND_DONE_EACCES; 11368 un->un_rsvd_status |= ST_RESERVATION_CONFLICT; 11369 } 11370 break; 11371 11372 case STATUS_BUSY: 11373 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, "unit busy\n"); 11374 if (rcvi->privatelen == sizeof (recov_info) && 11375 un->un_multipath && (pkt->pkt_state == (STATE_GOT_BUS | 11376 STATE_GOT_TARGET | STATE_SENT_CMD | STATE_GOT_STATUS))) { 11377 /* 11378 * Status returned by scsi_vhci indicating path 11379 * has failed over. 11380 */ 11381 action = PATH_FAILED; 11382 break; 11383 } 11384 /* FALLTHRU */ 11385 case STATUS_QFULL: 11386 if (rcvi->privatelen == sizeof (recov_info)) { 11387 /* 11388 * If recovery is inabled use it instead of 11389 * blind reties. 11390 */ 11391 action = ATTEMPT_RETRY; 11392 } else if (rcvi->pkt_retry_cnt++ < st_retry_count) { 11393 action = QUE_BUSY_COMMAND; 11394 } else if ((un->un_rsvd_status & 11395 (ST_RESERVE | ST_APPLICATION_RESERVATIONS)) == 0) { 11396 /* 11397 * If this is a command done before reserve is done 11398 * don't reset. 11399 */ 11400 action = COMMAND_DONE_ERROR; 11401 } else { 11402 ST_DEBUG2(ST_DEVINFO, st_label, CE_WARN, 11403 "unit busy too long\n"); 11404 (void) st_reset(un, RESET_ALL); 11405 action = COMMAND_DONE_ERROR; 11406 } 11407 break; 11408 11409 case STATUS_CHECK: 11410 case STATUS_TERMINATED: 11411 /* 11412 * we should only get here if the auto rqsense failed 11413 * thru a uscsi cmd without autorequest sense 11414 * so we just try again 11415 */ 11416 if (un->un_arq_enabled && 11417 stat->sts_rqpkt_reason == CMD_CMPLT && 11418 (stat->sts_rqpkt_state & (STATE_GOT_BUS | 11419 STATE_GOT_TARGET | STATE_SENT_CMD | STATE_GOT_STATUS)) == 11420 (STATE_GOT_BUS | STATE_GOT_TARGET | STATE_SENT_CMD | 11421 STATE_GOT_STATUS)) { 11422 11423 ST_DEBUG2(ST_DEVINFO, st_label, CE_WARN, 11424 "Really got sense data\n"); 11425 action = st_decode_sense(un, bp, MAX_SENSE_LENGTH - 11426 pkt->pkt_resid, stat, &un->un_pos); 11427 } else { 11428 ST_DEBUG2(ST_DEVINFO, st_label, CE_WARN, 11429 "Trying to queue sense command\n"); 11430 action = QUE_SENSE; 11431 } 11432 break; 11433 11434 case STATUS_TASK_ABORT: 11435 /* 11436 * This is an aborted task. This can be a reset on the other 11437 * port of a multiport drive. Lets try and recover it. 11438 */ 11439 action = DEVICE_RESET; 11440 break; 11441 11442 default: 11443 action = COMMAND_DONE; 11444 ST_DEBUG(ST_DEVINFO, st_label, CE_PANIC, 11445 "Unexpected scsi status byte 0x%x\n", SCBP_C(pkt)); 11446 } 11447 return (action); 11448 } 11449 11450 static void 11451 st_calc_bnum(struct scsi_tape *un, struct buf *bp, struct scsi_pkt *pkt) 11452 { 11453 int nblks; 11454 int nfiles; 11455 long count; 11456 recov_info *ri = pkt->pkt_private; 11457 cmd_attribute const *attrib; 11458 11459 ST_FUNC(ST_DEVINFO, st_calc_bnum); 11460 11461 ASSERT(mutex_owned(ST_MUTEX)); 11462 11463 if (ri->privatelen == sizeof (recov_info)) { 11464 attrib = ri->cmd_attrib; 11465 ASSERT(attrib->recov_pos_type == POS_EXPECTED); 11466 ASSERT(attrib->chg_tape_pos); 11467 } else { 11468 ri = NULL; 11469 attrib = st_lookup_cmd_attribute(pkt->pkt_cdbp[0]); 11470 } 11471 11472 count = bp->b_bcount - bp->b_resid; 11473 11474 /* Command reads or writes data */ 11475 if (attrib->transfers_data != TRAN_NONE) { 11476 if (count == 0) { 11477 /* failed writes should not make it here */ 11478 ASSERT(attrib->transfers_data == TRAN_READ); 11479 nblks = 0; 11480 nfiles = 1; 11481 } else if (un->un_bsize == 0) { 11482 /* 11483 * If variable block mode. 11484 * Fixed bit in CBD should be zero. 11485 */ 11486 ASSERT((pkt->pkt_cdbp[1] & 1) == 0); 11487 nblks = 1; 11488 un->un_kbytes_xferred += (count / ONE_K); 11489 nfiles = 0; 11490 } else { 11491 /* 11492 * If fixed block mode. 11493 * Fixed bit in CBD should be one. 11494 */ 11495 ASSERT((pkt->pkt_cdbp[1] & 1) == 1); 11496 nblks = (count / un->un_bsize); 11497 un->un_kbytes_xferred += (nblks * un->un_bsize) / ONE_K; 11498 nfiles = 0; 11499 } 11500 /* 11501 * So its possable to read some blocks and hit a filemark. 11502 * Example reading in fixed block mode where more then one 11503 * block at a time is requested. In this case because the 11504 * filemark is hit something less then the requesed number 11505 * of blocks is read. 11506 */ 11507 if (un->un_pos.eof == ST_EOF_PENDING && bp->b_resid) { 11508 nfiles = 1; 11509 } 11510 } else { 11511 nblks = 0; 11512 nfiles = count; 11513 } 11514 11515 /* 11516 * If some command failed after this one started and it seems 11517 * to have finshed without error count the position. 11518 */ 11519 if (un->un_persistence && un->un_persist_errors) { 11520 ASSERT(un->un_pos.pmode != invalid); 11521 } 11522 11523 if (attrib->chg_tape_direction == DIR_FORW) { 11524 un->un_pos.blkno += nblks; 11525 un->un_pos.lgclblkno += nblks; 11526 un->un_pos.lgclblkno += nfiles; 11527 } else if (attrib->chg_tape_direction == DIR_REVC) { 11528 un->un_pos.blkno -= nblks; 11529 un->un_pos.lgclblkno -= nblks; 11530 un->un_pos.lgclblkno -= nfiles; 11531 } else { 11532 ASSERT(0); 11533 } 11534 11535 /* recovery disabled */ 11536 if (ri == NULL) { 11537 un->un_running.pmode = invalid; 11538 return; 11539 } 11540 11541 /* 11542 * If we didn't just read a filemark. 11543 */ 11544 if (un->un_pos.eof != ST_EOF_PENDING) { 11545 ASSERT(nblks != 0 && nfiles == 0); 11546 /* 11547 * If Previously calulated expected position does not match 11548 * debug the expected position. 11549 */ 11550 if ((ri->pos.pmode != invalid) && nblks && 11551 ((un->un_pos.blkno != ri->pos.blkno) || 11552 (un->un_pos.lgclblkno != ri->pos.lgclblkno))) { 11553 #ifdef STDEBUG 11554 st_print_position(ST_DEVINFO, st_label, CE_NOTE, 11555 "Expected", &ri->pos); 11556 st_print_position(ST_DEVINFO, st_label, CE_NOTE, 11557 "But Got", &un->un_pos); 11558 #endif 11559 un->un_running.pmode = invalid; 11560 } 11561 } else { 11562 ASSERT(nfiles != 0); 11563 if (un->un_running.pmode != invalid) { 11564 /* 11565 * blkno and lgclblkno already counted in 11566 * st_add_recovery_info_to_pkt(). Since a block was not 11567 * read and a filemark was. 11568 */ 11569 if (attrib->chg_tape_direction == DIR_FORW) { 11570 un->un_running.fileno++; 11571 un->un_running.blkno = 0; 11572 } else if (attrib->chg_tape_direction == DIR_REVC) { 11573 un->un_running.fileno--; 11574 un->un_running.blkno = LASTBLK; 11575 } 11576 } 11577 } 11578 } 11579 11580 static void 11581 st_set_state(struct scsi_tape *un, struct buf *bp) 11582 { 11583 struct scsi_pkt *sp = BP_PKT(bp); 11584 struct uscsi_cmd *ucmd; 11585 11586 ST_FUNC(ST_DEVINFO, st_set_state); 11587 11588 ASSERT(mutex_owned(ST_MUTEX)); 11589 ASSERT(bp != un->un_recov_buf); 11590 11591 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 11592 "st_set_state(): eof=%x fmneeded=%x pkt_resid=0x%lx (%ld)\n", 11593 un->un_pos.eof, un->un_fmneeded, sp->pkt_resid, sp->pkt_resid); 11594 11595 if (bp != un->un_sbufp) { 11596 #ifdef STDEBUG 11597 if (DEBUGGING && sp->pkt_resid) { 11598 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 11599 "pkt_resid %ld bcount %ld\n", 11600 sp->pkt_resid, bp->b_bcount); 11601 } 11602 #endif 11603 bp->b_resid = sp->pkt_resid; 11604 if (geterror(bp) != EIO) { 11605 st_calc_bnum(un, bp, sp); 11606 } 11607 if (bp->b_flags & B_READ) { 11608 un->un_lastop = ST_OP_READ; 11609 un->un_fmneeded = 0; 11610 } else { 11611 un->un_lastop = ST_OP_WRITE; 11612 if (un->un_dp->options & ST_REEL) { 11613 un->un_fmneeded = 2; 11614 } else { 11615 un->un_fmneeded = 1; 11616 } 11617 } 11618 /* 11619 * all is honky dory at this point, so let's 11620 * readjust the throttle, to increase speed, if we 11621 * have not throttled down. 11622 */ 11623 if (un->un_throttle) { 11624 un->un_throttle = un->un_max_throttle; 11625 } 11626 } else { 11627 optype new_lastop = ST_OP_NIL; 11628 uchar_t cmd = (uchar_t)(intptr_t)bp->b_forw; 11629 11630 switch (cmd) { 11631 case SCMD_WRITE: 11632 case SCMD_WRITE_G4: 11633 bp->b_resid = sp->pkt_resid; 11634 new_lastop = ST_OP_WRITE; 11635 if (geterror(bp) == EIO) { 11636 break; 11637 } 11638 st_calc_bnum(un, bp, sp); 11639 if (un->un_dp->options & ST_REEL) { 11640 un->un_fmneeded = 2; 11641 } else { 11642 un->un_fmneeded = 1; 11643 } 11644 break; 11645 case SCMD_READ: 11646 case SCMD_READ_G4: 11647 bp->b_resid = sp->pkt_resid; 11648 new_lastop = ST_OP_READ; 11649 if (geterror(bp) == EIO) { 11650 break; 11651 } 11652 st_calc_bnum(un, bp, sp); 11653 un->un_fmneeded = 0; 11654 break; 11655 case SCMD_WRITE_FILE_MARK_G4: 11656 case SCMD_WRITE_FILE_MARK: 11657 { 11658 int fmdone; 11659 11660 if (un->un_pos.eof != ST_EOM) { 11661 un->un_pos.eof = ST_NO_EOF; 11662 } 11663 fmdone = (bp->b_bcount - bp->b_resid); 11664 if (fmdone > 0) { 11665 un->un_lastop = new_lastop = ST_OP_WEOF; 11666 un->un_pos.lgclblkno += fmdone; 11667 un->un_pos.fileno += fmdone; 11668 un->un_pos.blkno = 0; 11669 } else { 11670 new_lastop = ST_OP_CTL; 11671 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11672 "Flushed buffer\n"); 11673 } 11674 if (fmdone > un->un_fmneeded) { 11675 un->un_fmneeded = 0; 11676 } else { 11677 un->un_fmneeded -= fmdone; 11678 } 11679 break; 11680 } 11681 case SCMD_REWIND: 11682 un->un_pos.eof = ST_NO_EOF; 11683 un->un_pos.fileno = 0; 11684 un->un_pos.blkno = 0; 11685 un->un_pos.lgclblkno = 0; 11686 if (un->un_pos.pmode != legacy) 11687 un->un_pos.pmode = legacy; 11688 new_lastop = ST_OP_CTL; 11689 un->un_restore_pos = 0; 11690 break; 11691 11692 case SCMD_SPACE: 11693 case SCMD_SPACE_G4: 11694 { 11695 int64_t count; 11696 int64_t resid; 11697 int64_t done; 11698 cmd_attribute const *attrib; 11699 recov_info *ri = sp->pkt_private; 11700 11701 if (ri->privatelen == sizeof (recov_info)) { 11702 attrib = ri->cmd_attrib; 11703 } else { 11704 attrib = 11705 st_lookup_cmd_attribute(sp->pkt_cdbp[0]); 11706 } 11707 11708 resid = (int64_t)SPACE_CNT(bp->b_resid); 11709 count = (int64_t)attrib->get_cnt(sp->pkt_cdbp); 11710 11711 if (count >= 0) { 11712 done = (count - resid); 11713 } else { 11714 done = ((-count) - resid); 11715 } 11716 if (done > 0) { 11717 un->un_lastop = new_lastop = ST_OP_CTL; 11718 } else { 11719 new_lastop = ST_OP_CTL; 11720 } 11721 11722 ST_SPAC(ST_DEVINFO, st_label, CE_WARN, 11723 "space cmd: cdb[1] = %s\n" 11724 "space data: = 0x%lx\n" 11725 "space count: = %"PRId64"\n" 11726 "space resid: = %"PRId64"\n" 11727 "spaces done: = %"PRId64"\n" 11728 "fileno before = %d\n" 11729 "blkno before = %d\n", 11730 space_strs[sp->pkt_cdbp[1] & 7], 11731 bp->b_bcount, 11732 count, resid, done, 11733 un->un_pos.fileno, un->un_pos.blkno); 11734 11735 switch (sp->pkt_cdbp[1]) { 11736 case SPACE_TYPE(SP_FLM): 11737 /* Space file forward */ 11738 if (count >= 0) { 11739 if (un->un_pos.eof <= ST_EOF) { 11740 un->un_pos.eof = ST_NO_EOF; 11741 } 11742 un->un_pos.fileno += done; 11743 un->un_pos.blkno = 0; 11744 break; 11745 } 11746 /* Space file backward */ 11747 if (done > un->un_pos.fileno) { 11748 un->un_pos.fileno = 0; 11749 un->un_pos.blkno = 0; 11750 } else { 11751 un->un_pos.fileno -= done; 11752 un->un_pos.blkno = LASTBLK; 11753 un->un_running.pmode = invalid; 11754 } 11755 break; 11756 case SPACE_TYPE(SP_BLK): 11757 /* Space block forward */ 11758 if (count >= 0) { 11759 un->un_pos.blkno += done; 11760 break; 11761 } 11762 /* Space block backward */ 11763 if (un->un_pos.eof >= ST_EOF_PENDING) { 11764 /* 11765 * we stepped back into 11766 * a previous file; we are not 11767 * making an effort to pretend that 11768 * we are still in the current file 11769 * ie. logical == physical position 11770 * and leave it to st_ioctl to correct 11771 */ 11772 if (done > un->un_pos.blkno) { 11773 un->un_pos.blkno = 0; 11774 } else { 11775 un->un_pos.fileno--; 11776 un->un_pos.blkno = LASTBLK; 11777 un->un_running.pmode = invalid; 11778 } 11779 } else { 11780 un->un_pos.blkno -= done; 11781 } 11782 break; 11783 case SPACE_TYPE(SP_SQFLM): 11784 un->un_pos.pmode = logical; 11785 un->un_pos.blkno = 0; 11786 un->un_lastop = new_lastop = ST_OP_CTL; 11787 break; 11788 case SPACE_TYPE(SP_EOD): 11789 un->un_pos.pmode = logical; 11790 un->un_pos.eof = ST_EOM; 11791 un->un_status = KEY_BLANK_CHECK; 11792 break; 11793 default: 11794 un->un_pos.pmode = invalid; 11795 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 11796 "Unsupported space cmd: %s\n", 11797 space_strs[sp->pkt_cdbp[1] & 7]); 11798 11799 un->un_lastop = new_lastop = ST_OP_CTL; 11800 } 11801 11802 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 11803 "after_space rs %"PRId64" fil %d blk %d\n", 11804 resid, un->un_pos.fileno, un->un_pos.blkno); 11805 11806 break; 11807 } 11808 case SCMD_LOAD: 11809 if ((bp->b_bcount & (LD_LOAD | LD_EOT)) == LD_LOAD) { 11810 un->un_pos.fileno = 0; 11811 if (un->un_pos.pmode != legacy) 11812 un->un_pos.pmode = legacy; 11813 } else { 11814 un->un_state = ST_STATE_OFFLINE; 11815 un->un_pos.pmode = invalid; 11816 11817 } 11818 /* 11819 * If we are loading or unloading we expect the media id 11820 * to change. Lets make it unknown. 11821 */ 11822 if (un->un_media_id != bogusID && un->un_media_id_len) { 11823 kmem_free(un->un_media_id, un->un_media_id_len); 11824 un->un_media_id = NULL; 11825 un->un_media_id_len = 0; 11826 } 11827 un->un_density_known = 0; 11828 un->un_pos.eof = ST_NO_EOF; 11829 un->un_pos.blkno = 0; 11830 un->un_lastop = new_lastop = ST_OP_CTL; 11831 break; 11832 case SCMD_ERASE: 11833 un->un_pos.eof = ST_NO_EOF; 11834 un->un_pos.blkno = 0; 11835 un->un_pos.fileno = 0; 11836 un->un_pos.lgclblkno = 0; 11837 if (un->un_pos.pmode != legacy) 11838 un->un_pos.pmode = legacy; 11839 new_lastop = ST_OP_CTL; 11840 break; 11841 case SCMD_RESERVE: 11842 un->un_rsvd_status |= ST_RESERVE; 11843 un->un_rsvd_status &= 11844 ~(ST_RELEASE | ST_LOST_RESERVE | 11845 ST_RESERVATION_CONFLICT | ST_INITIATED_RESET); 11846 new_lastop = ST_OP_CTL; 11847 break; 11848 case SCMD_RELEASE: 11849 un->un_rsvd_status |= ST_RELEASE; 11850 un->un_rsvd_status &= 11851 ~(ST_RESERVE | ST_LOST_RESERVE | 11852 ST_RESERVATION_CONFLICT | ST_INITIATED_RESET); 11853 new_lastop = ST_OP_CTL; 11854 break; 11855 case SCMD_PERSISTENT_RESERVE_IN: 11856 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 11857 "PGR_IN command\n"); 11858 new_lastop = ST_OP_CTL; 11859 break; 11860 case SCMD_PERSISTENT_RESERVE_OUT: 11861 switch (sp->pkt_cdbp[1] & ST_SA_MASK) { 11862 case ST_SA_SCSI3_RESERVE: 11863 case ST_SA_SCSI3_PREEMPT: 11864 case ST_SA_SCSI3_PREEMPTANDABORT: 11865 un->un_rsvd_status |= 11866 (ST_APPLICATION_RESERVATIONS | ST_RESERVE); 11867 un->un_rsvd_status &= ~(ST_RELEASE | 11868 ST_LOST_RESERVE | ST_RESERVATION_CONFLICT | 11869 ST_INITIATED_RESET); 11870 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 11871 "PGR Reserve and set: entering" 11872 " ST_APPLICATION_RESERVATIONS mode"); 11873 break; 11874 case ST_SA_SCSI3_REGISTER: 11875 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 11876 "PGR Reserve register key"); 11877 un->un_rsvd_status |= ST_INIT_RESERVE; 11878 break; 11879 case ST_SA_SCSI3_CLEAR: 11880 un->un_rsvd_status &= ~ST_INIT_RESERVE; 11881 /* FALLTHROUGH */ 11882 case ST_SA_SCSI3_RELEASE: 11883 un->un_rsvd_status &= 11884 ~(ST_APPLICATION_RESERVATIONS | ST_RESERVE | 11885 ST_LOST_RESERVE | ST_RESERVATION_CONFLICT | 11886 ST_INITIATED_RESET); 11887 un->un_rsvd_status |= ST_RELEASE; 11888 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 11889 "PGR Release and reset: exiting" 11890 " ST_APPLICATION_RESERVATIONS mode"); 11891 break; 11892 } 11893 new_lastop = ST_OP_CTL; 11894 break; 11895 case SCMD_TEST_UNIT_READY: 11896 case SCMD_READ_BLKLIM: 11897 case SCMD_REQUEST_SENSE: 11898 case SCMD_INQUIRY: 11899 case SCMD_RECOVER_BUF: 11900 case SCMD_MODE_SELECT: 11901 case SCMD_MODE_SENSE: 11902 case SCMD_DOORLOCK: 11903 case SCMD_READ_BUFFER: 11904 case SCMD_REPORT_DENSITIES: 11905 case SCMD_LOG_SELECT_G1: 11906 case SCMD_LOG_SENSE_G1: 11907 case SCMD_REPORT_LUNS: 11908 case SCMD_READ_ATTRIBUTE: 11909 case SCMD_WRITE_ATTRIBUTE: 11910 case SCMD_SVC_ACTION_IN_G5: 11911 new_lastop = ST_OP_CTL; 11912 break; 11913 case SCMD_READ_POSITION: 11914 new_lastop = ST_OP_CTL; 11915 /* 11916 * Only if the buf used was un_sbufp. 11917 * Among other things the prevents read positions used 11918 * as part of error recovery from messing up our 11919 * current position as they will use un_recov_buf. 11920 */ 11921 if (USCSI_CMD(bp)) { 11922 (void) st_get_read_pos(un, bp); 11923 } 11924 break; 11925 case SCMD_LOCATE: 11926 case SCMD_LOCATE_G4: 11927 /* Locate makes position mode no longer legacy */ 11928 un->un_lastop = new_lastop = ST_OP_CTL; 11929 break; 11930 case SCMD_MAINTENANCE_IN: 11931 switch (sp->pkt_cdbp[1]) { 11932 case SSVC_ACTION_GET_SUPPORTED_OPERATIONS: 11933 case SSVC_ACTION_SET_TARGET_PORT_GROUPS: 11934 new_lastop = ST_OP_CTL; 11935 break; 11936 } 11937 if (new_lastop != ST_OP_NIL) { 11938 break; 11939 } 11940 default: 11941 /* 11942 * Unknown command, If was USCSI and USCSI_SILENT 11943 * flag was not set, set position to unknown. 11944 */ 11945 if ((((ucmd = BP_UCMD(bp)) != NULL) && 11946 (ucmd->uscsi_flags & USCSI_SILENT) == 0)) { 11947 ST_DEBUG2(ST_DEVINFO, st_label, CE_WARN, 11948 "unknown cmd 0x%X caused loss of state\n", 11949 cmd); 11950 } else { 11951 /* 11952 * keep the old agreement to allow unknown 11953 * commands with the USCSI_SILENT set. 11954 * This prevents ASSERT below. 11955 */ 11956 new_lastop = ST_OP_CTL; 11957 break; 11958 } 11959 /* FALLTHROUGH */ 11960 case SCMD_WRITE_BUFFER: /* Writes new firmware to device */ 11961 un->un_pos.pmode = invalid; 11962 un->un_lastop = new_lastop = ST_OP_CTL; 11963 break; 11964 } 11965 11966 /* new_lastop should have been changed */ 11967 ASSERT(new_lastop != ST_OP_NIL); 11968 11969 /* If un_lastop should copy new_lastop */ 11970 if (((un->un_lastop == ST_OP_WRITE) || 11971 (un->un_lastop == ST_OP_WEOF)) && 11972 new_lastop != ST_OP_CTL) { 11973 un->un_lastop = new_lastop; 11974 } 11975 } 11976 11977 /* 11978 * In the st driver we have a logical and physical file position. 11979 * Under BSD behavior, when you get a zero read, the logical position 11980 * is before the filemark but after the last record of the file. 11981 * The physical position is after the filemark. MTIOCGET should always 11982 * return the logical file position. 11983 * 11984 * The next read gives a silent skip to the next file. 11985 * Under SVR4, the logical file position remains before the filemark 11986 * until the file is closed or a space operation is performed. 11987 * Hence set err_resid and err_file before changing fileno if case 11988 * BSD Behaviour. 11989 */ 11990 un->un_err_resid = bp->b_resid; 11991 COPY_POS(&un->un_err_pos, &un->un_pos); 11992 11993 11994 /* 11995 * If we've seen a filemark via the last read operation 11996 * advance the file counter, but mark things such that 11997 * the next read operation gets a zero count. We have 11998 * to put this here to handle the case of sitting right 11999 * at the end of a tape file having seen the file mark, 12000 * but the tape is closed and then re-opened without 12001 * any further i/o. That is, the position information 12002 * must be updated before a close. 12003 */ 12004 12005 if (un->un_lastop == ST_OP_READ && un->un_pos.eof == ST_EOF_PENDING) { 12006 /* 12007 * If we're a 1/2" tape, and we get a filemark 12008 * right on block 0, *AND* we were not in the 12009 * first file on the tape, and we've hit logical EOM. 12010 * We'll mark the state so that later we do the 12011 * right thing (in st_close(), st_strategy() or 12012 * st_ioctl()). 12013 * 12014 */ 12015 if ((un->un_dp->options & ST_REEL) && 12016 !(un->un_dp->options & ST_READ_IGNORE_EOFS) && 12017 un->un_pos.blkno == 0 && un->un_pos.fileno > 0) { 12018 un->un_pos.eof = ST_EOT_PENDING; 12019 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 12020 "eot pending\n"); 12021 un->un_pos.fileno++; 12022 un->un_pos.blkno = 0; 12023 } else if (BSD_BEHAVIOR) { 12024 /* 12025 * If the read of the filemark was a side effect 12026 * of reading some blocks (i.e., data was actually 12027 * read), then the EOF mark is pending and the 12028 * bump into the next file awaits the next read 12029 * operation (which will return a zero count), or 12030 * a close or a space operation, else the bump 12031 * into the next file occurs now. 12032 */ 12033 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 12034 "resid=%lx, bcount=%lx\n", 12035 bp->b_resid, bp->b_bcount); 12036 12037 if (bp->b_resid != bp->b_bcount) { 12038 un->un_pos.eof = ST_EOF; 12039 } else { 12040 un->un_silent_skip = 1; 12041 un->un_pos.eof = ST_NO_EOF; 12042 un->un_pos.fileno++; 12043 un->un_pos.lgclblkno++; 12044 un->un_save_blkno = un->un_pos.blkno; 12045 un->un_pos.blkno = 0; 12046 } 12047 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 12048 "eof of file %d, eof=%d\n", 12049 un->un_pos.fileno, un->un_pos.eof); 12050 } else if (SVR4_BEHAVIOR) { 12051 /* 12052 * If the read of the filemark was a side effect 12053 * of reading some blocks (i.e., data was actually 12054 * read), then the next read should return 0 12055 */ 12056 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 12057 "resid=%lx, bcount=%lx\n", 12058 bp->b_resid, bp->b_bcount); 12059 if (bp->b_resid == bp->b_bcount) { 12060 un->un_pos.eof = ST_EOF; 12061 } 12062 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 12063 "eof of file=%d, eof=%d\n", 12064 un->un_pos.fileno, un->un_pos.eof); 12065 } 12066 } 12067 } 12068 12069 /* 12070 * set the correct un_errno, to take corner cases into consideration 12071 */ 12072 static void 12073 st_set_pe_errno(struct scsi_tape *un) 12074 { 12075 ST_FUNC(ST_DEVINFO, st_set_pe_errno); 12076 12077 ASSERT(mutex_owned(ST_MUTEX)); 12078 12079 /* if errno is already set, don't reset it */ 12080 if (un->un_errno) 12081 return; 12082 12083 /* here un_errno == 0 */ 12084 /* 12085 * if the last transfer before flushing all the 12086 * waiting I/O's, was 0 (resid = count), then we 12087 * want to give the user an error on all the rest, 12088 * so here. If there was a transfer, we set the 12089 * resid and counts to 0, and let it drop through, 12090 * giving a zero return. the next I/O will then 12091 * give an error. 12092 */ 12093 if (un->un_last_resid == un->un_last_count) { 12094 switch (un->un_pos.eof) { 12095 case ST_EOM: 12096 un->un_errno = ENOMEM; 12097 break; 12098 case ST_EOT: 12099 case ST_EOF: 12100 un->un_errno = EIO; 12101 break; 12102 } 12103 } else { 12104 /* 12105 * we know they did not have a zero, so make 12106 * sure they get one 12107 */ 12108 un->un_last_resid = un->un_last_count = 0; 12109 } 12110 } 12111 12112 12113 /* 12114 * send in a marker pkt to terminate flushing of commands by BBA (via 12115 * flush-on-errors) property. The HBA will always return TRAN_ACCEPT 12116 */ 12117 static void 12118 st_hba_unflush(struct scsi_tape *un) 12119 { 12120 ST_FUNC(ST_DEVINFO, st_hba_unflush); 12121 12122 ASSERT(mutex_owned(ST_MUTEX)); 12123 12124 if (!un->un_flush_on_errors) 12125 return; 12126 12127 #ifdef FLUSH_ON_ERRORS 12128 12129 if (!un->un_mkr_pkt) { 12130 un->un_mkr_pkt = scsi_init_pkt(ROUTE, NULL, (struct buf *)NULL, 12131 NULL, 0, 0, 0, SLEEP_FUNC, NULL); 12132 12133 /* we slept, so it must be there */ 12134 pkt->pkt_flags |= FLAG_FLUSH_MARKER; 12135 } 12136 12137 st_transport(un, un->un_mkr_pkt); 12138 #endif 12139 } 12140 12141 static char * 12142 st_print_scsi_cmd(char cmd) 12143 { 12144 char tmp[64]; 12145 char *cpnt; 12146 12147 cpnt = scsi_cmd_name(cmd, scsi_cmds, tmp); 12148 /* tmp goes out of scope on return and caller sees garbage */ 12149 if (cpnt == tmp) { 12150 cpnt = "Unknown Command"; 12151 } 12152 return (cpnt); 12153 } 12154 12155 static void 12156 st_print_cdb(dev_info_t *dip, char *label, uint_t level, 12157 char *title, char *cdb) 12158 { 12159 int len = scsi_cdb_size[CDB_GROUPID(cdb[0])]; 12160 char buf[256]; 12161 struct scsi_tape *un; 12162 int instance = ddi_get_instance(dip); 12163 12164 un = ddi_get_soft_state(st_state, instance); 12165 12166 ST_FUNC(dip, st_print_cdb); 12167 12168 /* force one line output so repeated commands are printed once */ 12169 if ((st_debug & 0x180) == 0x100) { 12170 scsi_log(dip, label, level, "node %s cmd %s\n", 12171 st_dev_name(un->un_dev), st_print_scsi_cmd(*cdb)); 12172 return; 12173 } 12174 12175 /* force one line output so repeated CDB's are printed once */ 12176 if ((st_debug & 0x180) == 0x80) { 12177 st_clean_print(dip, label, level, NULL, cdb, len); 12178 } else { 12179 (void) sprintf(buf, "%s for cmd(%s)", title, 12180 st_print_scsi_cmd(*cdb)); 12181 st_clean_print(dip, label, level, buf, cdb, len); 12182 } 12183 } 12184 12185 static void 12186 st_clean_print(dev_info_t *dev, char *label, uint_t level, 12187 char *title, char *data, int len) 12188 { 12189 int i; 12190 int c; 12191 char *format; 12192 char buf[256]; 12193 uchar_t byte; 12194 12195 ST_FUNC(dev, st_clean_print); 12196 12197 12198 if (title) { 12199 (void) sprintf(buf, "%s:\n", title); 12200 scsi_log(dev, label, level, "%s", buf); 12201 level = CE_CONT; 12202 } 12203 12204 for (i = 0; i < len; ) { 12205 buf[0] = 0; 12206 for (c = 0; c < 8 && i < len; c++, i++) { 12207 byte = (uchar_t)data[i]; 12208 if (byte < 0x10) 12209 format = "0x0%x "; 12210 else 12211 format = "0x%x "; 12212 (void) sprintf(&buf[(int)strlen(buf)], format, byte); 12213 } 12214 (void) sprintf(&buf[(int)strlen(buf)], "\n"); 12215 12216 scsi_log(dev, label, level, "%s\n", buf); 12217 level = CE_CONT; 12218 } 12219 } 12220 12221 /* 12222 * Conditionally enabled debugging 12223 */ 12224 #ifdef STDEBUG 12225 static void 12226 st_debug_cmds(struct scsi_tape *un, int com, int count, int wait) 12227 { 12228 char tmpbuf[64]; 12229 12230 ST_FUNC(ST_DEVINFO, st_debug_cmds); 12231 12232 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 12233 "cmd=%s count=0x%x (%d) %ssync\n", 12234 scsi_cmd_name(com, scsi_cmds, tmpbuf), 12235 count, count, 12236 wait == ASYNC_CMD ? "a" : ""); 12237 } 12238 #endif /* STDEBUG */ 12239 12240 /* 12241 * Returns pointer to name of minor node name of device 'dev'. 12242 */ 12243 static char * 12244 st_dev_name(dev_t dev) 12245 { 12246 struct scsi_tape *un; 12247 const char density[] = { 'l', 'm', 'h', 'c' }; 12248 static char name[32]; 12249 minor_t minor; 12250 int instance; 12251 int nprt = 0; 12252 12253 minor = getminor(dev); 12254 instance = ((minor & 0xff80) >> 5) | (minor & 3); 12255 un = ddi_get_soft_state(st_state, instance); 12256 if (un) { 12257 ST_FUNC(ST_DEVINFO, st_dev_name); 12258 } 12259 12260 name[nprt] = density[(minor & MT_DENSITY_MASK) >> 3]; 12261 12262 if (minor & MT_BSD) { 12263 name[++nprt] = 'b'; 12264 } 12265 12266 if (minor & MT_NOREWIND) { 12267 name[++nprt] = 'n'; 12268 } 12269 12270 /* NULL terminator */ 12271 name[++nprt] = 0; 12272 12273 return (name); 12274 } 12275 12276 /* 12277 * Soft error reporting, so far unique to each drive 12278 * 12279 * Currently supported: exabyte and DAT soft error reporting 12280 */ 12281 static int 12282 st_report_exabyte_soft_errors(dev_t dev, int flag) 12283 { 12284 uchar_t *sensep; 12285 int amt; 12286 int rval = 0; 12287 char cdb[CDB_GROUP0], *c = cdb; 12288 struct uscsi_cmd *com; 12289 12290 GET_SOFT_STATE(dev); 12291 12292 ST_FUNC(ST_DEVINFO, st_report_exabyte_soft_errors); 12293 12294 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 12295 "st_report_exabyte_soft_errors(dev = 0x%lx, flag = %d)\n", 12296 dev, flag); 12297 12298 ASSERT(mutex_owned(ST_MUTEX)); 12299 12300 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 12301 sensep = kmem_zalloc(TAPE_SENSE_LENGTH, KM_SLEEP); 12302 12303 *c++ = SCMD_REQUEST_SENSE; 12304 *c++ = 0; 12305 *c++ = 0; 12306 *c++ = 0; 12307 *c++ = TAPE_SENSE_LENGTH; 12308 /* 12309 * set CLRCNT (byte 5, bit 7 which clears the error counts) 12310 */ 12311 *c = (char)0x80; 12312 12313 com->uscsi_cdb = cdb; 12314 com->uscsi_cdblen = CDB_GROUP0; 12315 com->uscsi_bufaddr = (caddr_t)sensep; 12316 com->uscsi_buflen = TAPE_SENSE_LENGTH; 12317 com->uscsi_flags = USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ; 12318 com->uscsi_timeout = un->un_dp->non_motion_timeout; 12319 12320 rval = st_uscsi_cmd(un, com, FKIOCTL); 12321 if (rval || com->uscsi_status) { 12322 goto done; 12323 } 12324 12325 /* 12326 * was there enough data? 12327 */ 12328 amt = (int)TAPE_SENSE_LENGTH - com->uscsi_resid; 12329 12330 if ((amt >= 19) && un->un_kbytes_xferred) { 12331 uint_t count, error_rate; 12332 uint_t rate; 12333 12334 if (sensep[21] & CLN) { 12335 scsi_log(ST_DEVINFO, st_label, CE_WARN, 12336 "Periodic head cleaning required"); 12337 } 12338 if (un->un_kbytes_xferred < (EXABYTE_MIN_TRANSFER/ONE_K)) { 12339 goto done; 12340 } 12341 /* 12342 * check if soft error reporting needs to be done. 12343 */ 12344 count = sensep[16] << 16 | sensep[17] << 8 | sensep[18]; 12345 count &= 0xffffff; 12346 error_rate = (count * 100)/un->un_kbytes_xferred; 12347 12348 #ifdef STDEBUG 12349 if (st_soft_error_report_debug) { 12350 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 12351 "Exabyte Soft Error Report:\n"); 12352 scsi_log(ST_DEVINFO, st_label, CE_CONT, 12353 "read/write error counter: %d\n", count); 12354 scsi_log(ST_DEVINFO, st_label, CE_CONT, 12355 "number of bytes transferred: %dK\n", 12356 un->un_kbytes_xferred); 12357 scsi_log(ST_DEVINFO, st_label, CE_CONT, 12358 "error_rate: %d%%\n", error_rate); 12359 12360 if (amt >= 22) { 12361 scsi_log(ST_DEVINFO, st_label, CE_CONT, 12362 "unit sense: 0x%b 0x%b 0x%b\n", 12363 sensep[19], SENSE_19_BITS, 12364 sensep[20], SENSE_20_BITS, 12365 sensep[21], SENSE_21_BITS); 12366 } 12367 if (amt >= 27) { 12368 scsi_log(ST_DEVINFO, st_label, CE_CONT, 12369 "tracking retry counter: %d\n", 12370 sensep[26]); 12371 scsi_log(ST_DEVINFO, st_label, CE_CONT, 12372 "read/write retry counter: %d\n", 12373 sensep[27]); 12374 } 12375 } 12376 #endif 12377 12378 if (flag & FWRITE) { 12379 rate = EXABYTE_WRITE_ERROR_THRESHOLD; 12380 } else { 12381 rate = EXABYTE_READ_ERROR_THRESHOLD; 12382 } 12383 if (error_rate >= rate) { 12384 scsi_log(ST_DEVINFO, st_label, CE_WARN, 12385 "Soft error rate (%d%%) during %s was too high", 12386 error_rate, 12387 ((flag & FWRITE) ? wrg_str : rdg_str)); 12388 scsi_log(ST_DEVINFO, st_label, CE_CONT, 12389 "Please, replace tape cartridge\n"); 12390 } 12391 } 12392 12393 done: 12394 kmem_free(com, sizeof (*com)); 12395 kmem_free(sensep, TAPE_SENSE_LENGTH); 12396 12397 if (rval != 0) { 12398 scsi_log(ST_DEVINFO, st_label, CE_WARN, 12399 "exabyte soft error reporting failed\n"); 12400 } 12401 return (rval); 12402 } 12403 12404 /* 12405 * this is very specific to Archive 4mm dat 12406 */ 12407 #define ONE_GIG (ONE_K * ONE_K * ONE_K) 12408 12409 static int 12410 st_report_dat_soft_errors(dev_t dev, int flag) 12411 { 12412 uchar_t *sensep; 12413 int amt, i; 12414 int rval = 0; 12415 char cdb[CDB_GROUP1], *c = cdb; 12416 struct uscsi_cmd *com; 12417 struct scsi_arq_status status; 12418 12419 GET_SOFT_STATE(dev); 12420 12421 ST_FUNC(ST_DEVINFO, st_report_dat_soft_errors); 12422 12423 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 12424 "st_report_dat_soft_errors(dev = 0x%lx, flag = %d)\n", dev, flag); 12425 12426 ASSERT(mutex_owned(ST_MUTEX)); 12427 12428 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 12429 sensep = kmem_zalloc(LOG_SENSE_LENGTH, KM_SLEEP); 12430 12431 *c++ = SCMD_LOG_SENSE_G1; 12432 *c++ = 0; 12433 *c++ = (flag & FWRITE) ? 0x42 : 0x43; 12434 *c++ = 0; 12435 *c++ = 0; 12436 *c++ = 0; 12437 *c++ = 2; 12438 *c++ = 0; 12439 *c++ = (char)LOG_SENSE_LENGTH; 12440 *c = 0; 12441 com->uscsi_cdb = cdb; 12442 com->uscsi_cdblen = CDB_GROUP1; 12443 com->uscsi_bufaddr = (caddr_t)sensep; 12444 com->uscsi_buflen = LOG_SENSE_LENGTH; 12445 com->uscsi_rqlen = sizeof (status); 12446 com->uscsi_rqbuf = (caddr_t)&status; 12447 com->uscsi_flags = USCSI_DIAGNOSE | USCSI_RQENABLE | USCSI_READ; 12448 com->uscsi_timeout = un->un_dp->non_motion_timeout; 12449 rval = st_uscsi_cmd(un, com, FKIOCTL); 12450 if (rval) { 12451 scsi_log(ST_DEVINFO, st_label, CE_WARN, 12452 "DAT soft error reporting failed\n"); 12453 } 12454 if (rval || com->uscsi_status) { 12455 goto done; 12456 } 12457 12458 /* 12459 * was there enough data? 12460 */ 12461 amt = (int)LOG_SENSE_LENGTH - com->uscsi_resid; 12462 12463 if ((amt >= MIN_LOG_SENSE_LENGTH) && un->un_kbytes_xferred) { 12464 int total, retries, param_code; 12465 12466 total = -1; 12467 retries = -1; 12468 amt = sensep[3] + 4; 12469 12470 12471 #ifdef STDEBUG 12472 if (st_soft_error_report_debug) { 12473 (void) printf("logsense:"); 12474 for (i = 0; i < MIN_LOG_SENSE_LENGTH; i++) { 12475 if (i % 16 == 0) { 12476 (void) printf("\t\n"); 12477 } 12478 (void) printf(" %x", sensep[i]); 12479 } 12480 (void) printf("\n"); 12481 } 12482 #endif 12483 12484 /* 12485 * parse the param_codes 12486 */ 12487 if (sensep[0] == 2 || sensep[0] == 3) { 12488 for (i = 4; i < amt; i++) { 12489 param_code = (sensep[i++] << 8); 12490 param_code += sensep[i++]; 12491 i++; /* skip control byte */ 12492 if (param_code == 5) { 12493 if (sensep[i++] == 4) { 12494 total = (sensep[i++] << 24); 12495 total += (sensep[i++] << 16); 12496 total += (sensep[i++] << 8); 12497 total += sensep[i]; 12498 } 12499 } else if (param_code == 0x8007) { 12500 if (sensep[i++] == 2) { 12501 retries = sensep[i++] << 8; 12502 retries += sensep[i]; 12503 } 12504 } else { 12505 i += sensep[i]; 12506 } 12507 } 12508 } 12509 12510 /* 12511 * if the log sense returned valid numbers then determine 12512 * the read and write error thresholds based on the amount of 12513 * data transferred 12514 */ 12515 12516 if (total > 0 && retries > 0) { 12517 short normal_retries = 0; 12518 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 12519 "total xferred (%s) =%x, retries=%x\n", 12520 ((flag & FWRITE) ? wrg_str : rdg_str), 12521 total, retries); 12522 12523 if (flag & FWRITE) { 12524 if (total <= 12525 WRITE_SOFT_ERROR_WARNING_THRESHOLD) { 12526 normal_retries = 12527 DAT_SMALL_WRITE_ERROR_THRESHOLD; 12528 } else { 12529 normal_retries = 12530 DAT_LARGE_WRITE_ERROR_THRESHOLD; 12531 } 12532 } else { 12533 if (total <= 12534 READ_SOFT_ERROR_WARNING_THRESHOLD) { 12535 normal_retries = 12536 DAT_SMALL_READ_ERROR_THRESHOLD; 12537 } else { 12538 normal_retries = 12539 DAT_LARGE_READ_ERROR_THRESHOLD; 12540 } 12541 } 12542 12543 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 12544 "normal retries=%d\n", normal_retries); 12545 12546 if (retries >= normal_retries) { 12547 scsi_log(ST_DEVINFO, st_label, CE_WARN, 12548 "Soft error rate (retries = %d) during " 12549 "%s was too high", retries, 12550 ((flag & FWRITE) ? wrg_str : rdg_str)); 12551 scsi_log(ST_DEVINFO, st_label, CE_CONT, 12552 "Periodic head cleaning required " 12553 "and/or replace tape cartridge\n"); 12554 } 12555 12556 } else if (total == -1 || retries == -1) { 12557 scsi_log(ST_DEVINFO, st_label, CE_WARN, 12558 "log sense parameter code does not make sense\n"); 12559 } 12560 } 12561 12562 /* 12563 * reset all values 12564 */ 12565 c = cdb; 12566 *c++ = SCMD_LOG_SELECT_G1; 12567 *c++ = 2; /* this resets all values */ 12568 *c++ = (char)0xc0; 12569 *c++ = 0; 12570 *c++ = 0; 12571 *c++ = 0; 12572 *c++ = 0; 12573 *c++ = 0; 12574 *c++ = 0; 12575 *c = 0; 12576 com->uscsi_bufaddr = NULL; 12577 com->uscsi_buflen = 0; 12578 com->uscsi_flags = USCSI_DIAGNOSE | USCSI_SILENT; 12579 rval = st_uscsi_cmd(un, com, FKIOCTL); 12580 if (rval) { 12581 scsi_log(ST_DEVINFO, st_label, CE_WARN, 12582 "DAT soft error reset failed\n"); 12583 } 12584 done: 12585 kmem_free(com, sizeof (*com)); 12586 kmem_free(sensep, LOG_SENSE_LENGTH); 12587 return (rval); 12588 } 12589 12590 static int 12591 st_report_soft_errors(dev_t dev, int flag) 12592 { 12593 GET_SOFT_STATE(dev); 12594 12595 ST_FUNC(ST_DEVINFO, st_report_soft_errors); 12596 12597 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 12598 "st_report_soft_errors(dev = 0x%lx, flag = %d)\n", dev, flag); 12599 12600 ASSERT(mutex_owned(ST_MUTEX)); 12601 12602 switch (un->un_dp->type) { 12603 case ST_TYPE_EXB8500: 12604 case ST_TYPE_EXABYTE: 12605 return (st_report_exabyte_soft_errors(dev, flag)); 12606 /*NOTREACHED*/ 12607 case ST_TYPE_PYTHON: 12608 return (st_report_dat_soft_errors(dev, flag)); 12609 /*NOTREACHED*/ 12610 default: 12611 un->un_dp->options &= ~ST_SOFT_ERROR_REPORTING; 12612 return (-1); 12613 } 12614 } 12615 12616 /* 12617 * persistent error routines 12618 */ 12619 12620 /* 12621 * enable persistent errors, and set the throttle appropriately, checking 12622 * for flush-on-errors capability 12623 */ 12624 static void 12625 st_turn_pe_on(struct scsi_tape *un) 12626 { 12627 ST_FUNC(ST_DEVINFO, st_turn_pe_on); 12628 12629 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, "st_pe_on\n"); 12630 ASSERT(mutex_owned(ST_MUTEX)); 12631 12632 un->un_persistence = 1; 12633 12634 /* 12635 * only use flush-on-errors if auto-request-sense and untagged-qing are 12636 * enabled. This will simplify the error handling for request senses 12637 */ 12638 12639 if (un->un_arq_enabled && un->un_untagged_qing) { 12640 uchar_t f_o_e; 12641 12642 mutex_exit(ST_MUTEX); 12643 f_o_e = (scsi_ifsetcap(ROUTE, "flush-on-errors", 1, 1) == 1) ? 12644 1 : 0; 12645 mutex_enter(ST_MUTEX); 12646 12647 un->un_flush_on_errors = f_o_e; 12648 } else { 12649 un->un_flush_on_errors = 0; 12650 } 12651 12652 if (un->un_flush_on_errors) 12653 un->un_max_throttle = (uchar_t)st_max_throttle; 12654 else 12655 un->un_max_throttle = 1; 12656 12657 if (un->un_dp->options & ST_RETRY_ON_RECOVERED_DEFERRED_ERROR) 12658 un->un_max_throttle = 1; 12659 12660 /* this will send a marker pkt */ 12661 st_clear_pe(un); 12662 } 12663 12664 /* 12665 * This turns persistent errors permanently off 12666 */ 12667 static void 12668 st_turn_pe_off(struct scsi_tape *un) 12669 { 12670 ST_FUNC(ST_DEVINFO, st_turn_pe_off); 12671 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, "st_pe_off\n"); 12672 ASSERT(mutex_owned(ST_MUTEX)); 12673 12674 /* turn it off for good */ 12675 un->un_persistence = 0; 12676 12677 /* this will send a marker pkt */ 12678 st_clear_pe(un); 12679 12680 /* turn off flush on error capability, if enabled */ 12681 if (un->un_flush_on_errors) { 12682 mutex_exit(ST_MUTEX); 12683 (void) scsi_ifsetcap(ROUTE, "flush-on-errors", 0, 1); 12684 mutex_enter(ST_MUTEX); 12685 } 12686 12687 12688 un->un_flush_on_errors = 0; 12689 } 12690 12691 /* 12692 * This clear persistent errors, allowing more commands through, and also 12693 * sending a marker packet. 12694 */ 12695 static void 12696 st_clear_pe(struct scsi_tape *un) 12697 { 12698 ST_FUNC(ST_DEVINFO, st_clear_pe); 12699 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, "st_pe_clear\n"); 12700 ASSERT(mutex_owned(ST_MUTEX)); 12701 12702 un->un_persist_errors = 0; 12703 un->un_throttle = un->un_last_throttle = 1; 12704 un->un_errno = 0; 12705 st_hba_unflush(un); 12706 } 12707 12708 /* 12709 * This will flag persistent errors, shutting everything down, if the 12710 * application had enabled persistent errors via MTIOCPERSISTENT 12711 */ 12712 static void 12713 st_set_pe_flag(struct scsi_tape *un) 12714 { 12715 ST_FUNC(ST_DEVINFO, st_set_pe_flag); 12716 ASSERT(mutex_owned(ST_MUTEX)); 12717 12718 if (un->un_persistence) { 12719 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, "st_pe_flag\n"); 12720 un->un_persist_errors = 1; 12721 un->un_throttle = un->un_last_throttle = 0; 12722 cv_broadcast(&un->un_sbuf_cv); 12723 } 12724 } 12725 12726 static int 12727 st_do_reserve(struct scsi_tape *un) 12728 { 12729 int rval; 12730 int was_lost = un->un_rsvd_status & ST_LOST_RESERVE; 12731 12732 ST_FUNC(ST_DEVINFO, st_do_reserve); 12733 12734 /* 12735 * Issue a Throw-Away reserve command to clear the 12736 * check condition. 12737 * If the current behaviour of reserve/release is to 12738 * hold reservation across opens , and if a Bus reset 12739 * has been issued between opens then this command 12740 * would set the ST_LOST_RESERVE flags in rsvd_status. 12741 * In this case return an EACCES so that user knows that 12742 * reservation has been lost in between opens. 12743 * If this error is not returned and we continue with 12744 * successful open , then user may think position of the 12745 * tape is still the same but inreality we would rewind the 12746 * tape and continue from BOT. 12747 */ 12748 rval = st_reserve_release(un, ST_RESERVE, st_uscsi_cmd); 12749 if (rval) { 12750 if ((un->un_rsvd_status & ST_LOST_RESERVE_BETWEEN_OPENS) == 12751 ST_LOST_RESERVE_BETWEEN_OPENS) { 12752 un->un_rsvd_status &= ~(ST_LOST_RESERVE | ST_RESERVE); 12753 un->un_errno = EACCES; 12754 return (EACCES); 12755 } 12756 rval = st_reserve_release(un, ST_RESERVE, st_uscsi_cmd); 12757 } 12758 if (rval == 0) { 12759 un->un_rsvd_status |= ST_INIT_RESERVE; 12760 } 12761 if (was_lost) { 12762 un->un_running.pmode = invalid; 12763 } 12764 12765 return (rval); 12766 } 12767 12768 static int 12769 st_check_cdb_for_need_to_reserve(struct scsi_tape *un, uchar_t *cdb) 12770 { 12771 int rval; 12772 cmd_attribute const *attrib; 12773 12774 ST_FUNC(ST_DEVINFO, st_check_cdb_for_need_to_reserve); 12775 12776 /* 12777 * If already reserved no need to do it again. 12778 * Also if Reserve and Release are disabled Just return. 12779 */ 12780 if ((un->un_rsvd_status & (ST_APPLICATION_RESERVATIONS)) || 12781 ((un->un_rsvd_status & (ST_RESERVE | ST_LOST_RESERVE)) == 12782 ST_RESERVE) || (un->un_dp->options & ST_NO_RESERVE_RELEASE)) { 12783 ST_DEBUG6(ST_DEVINFO, st_label, CE_NOTE, 12784 "st_check_cdb_for_need_to_reserve() reserve unneeded %s", 12785 st_print_scsi_cmd((uchar_t)cdb[0])); 12786 return (0); 12787 } 12788 12789 /* See if command is on the list */ 12790 attrib = st_lookup_cmd_attribute(cdb[0]); 12791 12792 if (attrib == NULL) { 12793 rval = 1; /* Not found, when in doubt reserve */ 12794 } else if ((attrib->requires_reserve) != 0) { 12795 rval = 1; 12796 } else if ((attrib->reserve_byte) != 0) { 12797 /* 12798 * cmd is on list. 12799 * if byte is zero always allowed. 12800 */ 12801 rval = 1; 12802 } else if (((cdb[attrib->reserve_byte]) & 12803 (attrib->reserve_mask)) != 0) { 12804 rval = 1; 12805 } else { 12806 rval = 0; 12807 } 12808 12809 if (rval) { 12810 ST_DEBUG6(ST_DEVINFO, st_label, CE_NOTE, 12811 "Command %s requires reservation", 12812 st_print_scsi_cmd(cdb[0])); 12813 12814 rval = st_do_reserve(un); 12815 } 12816 12817 return (rval); 12818 } 12819 12820 static int 12821 st_check_cmd_for_need_to_reserve(struct scsi_tape *un, uchar_t cmd, int cnt) 12822 { 12823 int rval; 12824 cmd_attribute const *attrib; 12825 12826 ST_FUNC(ST_DEVINFO, st_check_cmd_for_need_to_reserve); 12827 12828 /* 12829 * Do not reserve when already reserved, when not supported or when 12830 * auto-rewinding on device closure. 12831 */ 12832 if ((un->un_rsvd_status & (ST_APPLICATION_RESERVATIONS)) || 12833 ((un->un_rsvd_status & (ST_RESERVE | ST_LOST_RESERVE)) == 12834 ST_RESERVE) || (un->un_dp->options & ST_NO_RESERVE_RELEASE) || 12835 ((un->un_state == ST_STATE_CLOSING) && (cmd == SCMD_REWIND))) { 12836 ST_DEBUG6(ST_DEVINFO, st_label, CE_NOTE, 12837 "st_check_cmd_for_need_to_reserve() reserve unneeded %s", 12838 st_print_scsi_cmd(cmd)); 12839 return (0); 12840 } 12841 12842 /* search for this command on the list */ 12843 attrib = st_lookup_cmd_attribute(cmd); 12844 12845 if (attrib == NULL) { 12846 rval = 1; /* Not found, when in doubt reserve */ 12847 } else if ((attrib->requires_reserve) != 0) { 12848 rval = 1; 12849 } else if ((attrib->reserve_byte) != 0) { 12850 /* 12851 * cmd is on list. 12852 * if byte is zero always allowed. 12853 */ 12854 rval = 1; 12855 } else if (((attrib->reserve_mask) & cnt) != 0) { 12856 rval = 1; 12857 } else { 12858 rval = 0; 12859 } 12860 12861 if (rval) { 12862 ST_DEBUG6(ST_DEVINFO, st_label, CE_NOTE, 12863 "Cmd %s requires reservation", st_print_scsi_cmd(cmd)); 12864 12865 rval = st_do_reserve(un); 12866 } 12867 12868 return (rval); 12869 } 12870 12871 static int 12872 st_reserve_release(struct scsi_tape *un, int cmd, ubufunc_t ubf) 12873 { 12874 struct uscsi_cmd uscsi_cmd; 12875 int rval; 12876 char cdb[CDB_GROUP0]; 12877 struct scsi_arq_status stat; 12878 12879 12880 12881 ST_FUNC(ST_DEVINFO, st_reserve_release); 12882 12883 ASSERT(mutex_owned(ST_MUTEX)); 12884 12885 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 12886 "st_reserve_release: %s \n", 12887 (cmd == ST_RELEASE)? "Releasing":"Reserving"); 12888 12889 bzero(&cdb, CDB_GROUP0); 12890 if (cmd == ST_RELEASE) { 12891 cdb[0] = SCMD_RELEASE; 12892 } else { 12893 cdb[0] = SCMD_RESERVE; 12894 } 12895 bzero(&uscsi_cmd, sizeof (struct uscsi_cmd)); 12896 uscsi_cmd.uscsi_flags = USCSI_WRITE | USCSI_RQENABLE; 12897 uscsi_cmd.uscsi_cdb = cdb; 12898 uscsi_cmd.uscsi_cdblen = CDB_GROUP0; 12899 uscsi_cmd.uscsi_timeout = un->un_dp->non_motion_timeout; 12900 uscsi_cmd.uscsi_rqbuf = (caddr_t)&stat; 12901 uscsi_cmd.uscsi_rqlen = sizeof (stat); 12902 12903 rval = ubf(un, &uscsi_cmd, FKIOCTL); 12904 12905 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 12906 "st_reserve_release: rval(1)=%d\n", rval); 12907 12908 if (rval) { 12909 if (uscsi_cmd.uscsi_status == STATUS_RESERVATION_CONFLICT) { 12910 rval = EACCES; 12911 } 12912 /* 12913 * dynamically turn off reserve/release support 12914 * in case of drives which do not support 12915 * reserve/release command(ATAPI drives). 12916 */ 12917 if (un->un_status == KEY_ILLEGAL_REQUEST) { 12918 if ((un->un_dp->options & ST_NO_RESERVE_RELEASE) == 0) { 12919 un->un_dp->options |= ST_NO_RESERVE_RELEASE; 12920 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 12921 "Tape unit does not support " 12922 "reserve/release \n"); 12923 } 12924 rval = 0; 12925 } 12926 } 12927 return (rval); 12928 } 12929 12930 static int 12931 st_take_ownership(struct scsi_tape *un, ubufunc_t ubf) 12932 { 12933 int rval; 12934 12935 ST_FUNC(ST_DEVINFO, st_take_ownership); 12936 12937 ASSERT(mutex_owned(ST_MUTEX)); 12938 12939 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 12940 "st_take_ownership: Entering ...\n"); 12941 12942 12943 rval = st_reserve_release(un, ST_RESERVE, ubf); 12944 /* 12945 * XXX -> Should reset be done only if we get EACCES. 12946 * . 12947 */ 12948 if (rval) { 12949 if (st_reset(un, RESET_LUN) == 0) { 12950 return (EIO); 12951 } 12952 un->un_rsvd_status &= 12953 ~(ST_LOST_RESERVE | ST_RESERVATION_CONFLICT); 12954 12955 mutex_exit(ST_MUTEX); 12956 delay(drv_usectohz(ST_RESERVATION_DELAY)); 12957 mutex_enter(ST_MUTEX); 12958 /* 12959 * remove the check condition. 12960 */ 12961 (void) st_reserve_release(un, ST_RESERVE, ubf); 12962 rval = st_reserve_release(un, ST_RESERVE, ubf); 12963 if (rval != 0) { 12964 if ((st_reserve_release(un, ST_RESERVE, ubf)) 12965 != 0) { 12966 rval = (un->un_rsvd_status & 12967 ST_RESERVATION_CONFLICT) ? EACCES : EIO; 12968 return (rval); 12969 } 12970 } 12971 /* 12972 * Set tape state to ST_STATE_OFFLINE , in case if 12973 * the user wants to continue and start using 12974 * the tape. 12975 */ 12976 un->un_state = ST_STATE_OFFLINE; 12977 un->un_rsvd_status |= ST_INIT_RESERVE; 12978 } 12979 return (rval); 12980 } 12981 12982 static int 12983 st_create_errstats(struct scsi_tape *un, int instance) 12984 { 12985 char kstatname[KSTAT_STRLEN]; 12986 12987 ST_FUNC(ST_DEVINFO, st_create_errstats); 12988 12989 /* 12990 * Create device error kstats 12991 */ 12992 12993 if (un->un_errstats == (kstat_t *)0) { 12994 (void) sprintf(kstatname, "st%d,err", instance); 12995 un->un_errstats = kstat_create("sterr", instance, kstatname, 12996 "device_error", KSTAT_TYPE_NAMED, 12997 sizeof (struct st_errstats) / sizeof (kstat_named_t), 12998 KSTAT_FLAG_PERSISTENT); 12999 13000 if (un->un_errstats) { 13001 struct st_errstats *stp; 13002 13003 stp = (struct st_errstats *)un->un_errstats->ks_data; 13004 kstat_named_init(&stp->st_softerrs, "Soft Errors", 13005 KSTAT_DATA_ULONG); 13006 kstat_named_init(&stp->st_harderrs, "Hard Errors", 13007 KSTAT_DATA_ULONG); 13008 kstat_named_init(&stp->st_transerrs, "Transport Errors", 13009 KSTAT_DATA_ULONG); 13010 kstat_named_init(&stp->st_vid, "Vendor", 13011 KSTAT_DATA_CHAR); 13012 kstat_named_init(&stp->st_pid, "Product", 13013 KSTAT_DATA_CHAR); 13014 kstat_named_init(&stp->st_revision, "Revision", 13015 KSTAT_DATA_CHAR); 13016 kstat_named_init(&stp->st_serial, "Serial No", 13017 KSTAT_DATA_CHAR); 13018 un->un_errstats->ks_private = un; 13019 un->un_errstats->ks_update = nulldev; 13020 kstat_install(un->un_errstats); 13021 /* 13022 * Fill in the static data 13023 */ 13024 (void) strncpy(&stp->st_vid.value.c[0], 13025 ST_INQUIRY->inq_vid, 8); 13026 /* 13027 * XXX: Emulex MT-02 (and emulators) predates 13028 * SCSI-1 and has no vid & pid inquiry data. 13029 */ 13030 if (ST_INQUIRY->inq_len != 0) { 13031 (void) strncpy(&stp->st_pid.value.c[0], 13032 ST_INQUIRY->inq_pid, 16); 13033 (void) strncpy(&stp->st_revision.value.c[0], 13034 ST_INQUIRY->inq_revision, 4); 13035 (void) strncpy(&stp->st_serial.value.c[0], 13036 ST_INQUIRY->inq_serial, 12); 13037 } 13038 } 13039 } 13040 return (0); 13041 } 13042 13043 static int 13044 st_validate_tapemarks(struct scsi_tape *un, ubufunc_t ubf, tapepos_t *pos) 13045 { 13046 int rval; 13047 bufunc_t bf = (ubf == st_uscsi_rcmd) ? st_rcmd : st_cmd; 13048 13049 ST_FUNC(ST_DEVINFO, st_validate_tapemarks); 13050 13051 ASSERT(MUTEX_HELD(&un->un_sd->sd_mutex)); 13052 ASSERT(mutex_owned(ST_MUTEX)); 13053 13054 /* Can't restore an invalid position */ 13055 if (pos->pmode == invalid) { 13056 return (4); 13057 } 13058 13059 /* 13060 * Assumtions: 13061 * If a position was read and is in logical position mode. 13062 * If a drive supports read position it supports locate. 13063 * If the read position type is not NO_POS. even though 13064 * a read position make not have been attemped yet. 13065 * 13066 * The drive can locate to the position. 13067 */ 13068 if (pos->pmode == logical || un->un_read_pos_type != NO_POS) { 13069 /* 13070 * If position mode is logical or legacy mode try 13071 * to locate there as it is faster. 13072 * If it fails try the old way. 13073 */ 13074 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 13075 "Restoring tape position to lgclblkbo=0x%"PRIx64"....", 13076 pos->lgclblkno); 13077 13078 if (st_logical_block_locate(un, st_uscsi_cmd, &un->un_pos, 13079 pos->lgclblkno, pos->partition) == 0) { 13080 /* Assume we are there copy rest of position back */ 13081 if (un->un_pos.lgclblkno == pos->lgclblkno) { 13082 COPY_POS(&un->un_pos, pos); 13083 } 13084 return (0); 13085 } 13086 13087 /* 13088 * If logical block locate failed to restore a logical 13089 * position, can't recover. 13090 */ 13091 if (pos->pmode == logical) { 13092 return (-1); 13093 } 13094 } 13095 13096 13097 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 13098 "Restoring tape position at fileno=%x, blkno=%x....", 13099 pos->fileno, pos->blkno); 13100 13101 /* 13102 * Rewind ? Oh yeah, Fidelity has got the STK F/W changed 13103 * so as not to rewind tape on RESETS: Gee, Has life ever 13104 * been simple in tape land ? 13105 */ 13106 rval = bf(un, SCMD_REWIND, 0, SYNC_CMD); 13107 if (rval) { 13108 scsi_log(ST_DEVINFO, st_label, CE_WARN, 13109 "Failed to restore the last file and block position: In" 13110 " this state, Tape will be loaded at BOT during next open"); 13111 un->un_pos.pmode = invalid; 13112 return (rval); 13113 } 13114 13115 /* If the position was as the result of back space file */ 13116 if (pos->blkno > (INF / 2)) { 13117 /* Go one extra file forward */ 13118 pos->fileno++; 13119 /* Figure how many blocks to back into the previous file */ 13120 pos->blkno = -(INF - pos->blkno); 13121 } 13122 13123 /* Go to requested fileno */ 13124 if (pos->fileno) { 13125 rval = st_cmd(un, SCMD_SPACE, Fmk(pos->fileno), SYNC_CMD); 13126 if (rval) { 13127 scsi_log(ST_DEVINFO, st_label, CE_WARN, 13128 "Failed to restore the last file position: In this " 13129 " state, Tape will be loaded at BOT during next" 13130 " open %d", __LINE__); 13131 un->un_pos.pmode = invalid; 13132 pos->pmode = invalid; 13133 return (rval); 13134 } 13135 } 13136 13137 /* 13138 * If backing into a file we already did an extra file forward. 13139 * Now we have to back over the filemark to get to the end of 13140 * the previous file. The blkno has been ajusted to a negative 13141 * value so we will get to the expected location. 13142 */ 13143 if (pos->blkno) { 13144 rval = bf(un, SCMD_SPACE, Fmk(-1), SYNC_CMD); 13145 if (rval) { 13146 scsi_log(ST_DEVINFO, st_label, CE_WARN, 13147 "Failed to restore the last file position: In this " 13148 " state, Tape will be loaded at BOT during next" 13149 " open %d", __LINE__); 13150 un->un_pos.pmode = invalid; 13151 pos->pmode = invalid; 13152 return (rval); 13153 } 13154 } 13155 13156 /* 13157 * The position mode, block and fileno should be correct, 13158 * This updates eof and logical position information. 13159 */ 13160 un->un_pos.eof = pos->eof; 13161 un->un_pos.lgclblkno = pos->lgclblkno; 13162 13163 return (0); 13164 } 13165 13166 /* 13167 * check sense key, ASC, ASCQ in order to determine if the tape needs 13168 * to be ejected 13169 */ 13170 13171 static int 13172 st_check_asc_ascq(struct scsi_tape *un) 13173 { 13174 struct scsi_extended_sense *sensep = ST_RQSENSE; 13175 struct tape_failure_code *code; 13176 13177 ST_FUNC(ST_DEVINFO, st_check_asc_ascq); 13178 13179 for (code = st_tape_failure_code; code->key != 0xff; code++) { 13180 if ((code->key == sensep->es_key) && 13181 (code->add_code == sensep->es_add_code) && 13182 (code->qual_code == sensep->es_qual_code)) 13183 return (1); 13184 } 13185 return (0); 13186 } 13187 13188 /* 13189 * st_logpage_supported() sends a Log Sense command with 13190 * page code = 0 = Supported Log Pages Page to the device, 13191 * to see whether the page 'page' is supported. 13192 * Return values are: 13193 * -1 if the Log Sense command fails 13194 * 0 if page is not supported 13195 * 1 if page is supported 13196 */ 13197 13198 static int 13199 st_logpage_supported(struct scsi_tape *un, uchar_t page) 13200 { 13201 uchar_t *sp, *sensep; 13202 unsigned length; 13203 struct uscsi_cmd *com; 13204 struct scsi_arq_status status; 13205 int rval; 13206 char cdb[CDB_GROUP1] = { 13207 SCMD_LOG_SENSE_G1, 13208 0, 13209 SUPPORTED_LOG_PAGES_PAGE, 13210 0, 13211 0, 13212 0, 13213 0, 13214 0, 13215 (char)LOG_SENSE_LENGTH, 13216 0 13217 }; 13218 13219 ST_FUNC(ST_DEVINFO, st_logpage_supported); 13220 13221 ASSERT(mutex_owned(ST_MUTEX)); 13222 13223 com = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 13224 sensep = kmem_zalloc(LOG_SENSE_LENGTH, KM_SLEEP); 13225 13226 com->uscsi_cdb = cdb; 13227 com->uscsi_cdblen = CDB_GROUP1; 13228 com->uscsi_bufaddr = (caddr_t)sensep; 13229 com->uscsi_buflen = LOG_SENSE_LENGTH; 13230 com->uscsi_rqlen = sizeof (status); 13231 com->uscsi_rqbuf = (caddr_t)&status; 13232 com->uscsi_flags = 13233 USCSI_DIAGNOSE | USCSI_RQENABLE | USCSI_READ; 13234 com->uscsi_timeout = un->un_dp->non_motion_timeout; 13235 rval = st_uscsi_cmd(un, com, FKIOCTL); 13236 if (rval || com->uscsi_status) { 13237 /* uscsi-command failed */ 13238 rval = -1; 13239 } else { 13240 13241 sp = sensep + 3; 13242 13243 for (length = *sp++; length > 0; length--, sp++) { 13244 13245 if (*sp == page) { 13246 rval = 1; 13247 break; 13248 } 13249 } 13250 } 13251 kmem_free(com, sizeof (struct uscsi_cmd)); 13252 kmem_free(sensep, LOG_SENSE_LENGTH); 13253 return (rval); 13254 } 13255 13256 13257 /* 13258 * st_check_clean_bit() gets the status of the tape's cleaning bit. 13259 * 13260 * If the device does support the TapeAlert log page, then the cleaning bit 13261 * information will be read from this page. Otherwise we will see if one of 13262 * ST_CLN_TYPE_1, ST_CLN_TYPE_2 or ST_CLN_TYPE_3 is set in the properties of 13263 * the device, which means, that we can get the cleaning bit information via 13264 * a RequestSense command. 13265 * If both methods of getting cleaning bit information are not supported 13266 * st_check_clean_bit() will return with 0. Otherwise st_check_clean_bit() 13267 * returns with 13268 * - MTF_TAPE_CLN_SUPPORTED if cleaning bit is not set or 13269 * - MTF_TAPE_CLN_SUPPORTED | MTF_TAPE_HEAD_DIRTY if cleaning bit is set. 13270 * If the call to st_uscsi_cmd() to do the Log Sense or the Request Sense 13271 * command fails, or if the amount of Request Sense data is not enough, then 13272 * st_check_clean_bit() returns with -1. 13273 */ 13274 13275 static int 13276 st_check_clean_bit(struct scsi_tape *un) 13277 { 13278 int rval = 0; 13279 13280 ST_FUNC(ST_DEVINFO, st_check_clean_bit); 13281 13282 ASSERT(mutex_owned(ST_MUTEX)); 13283 13284 if (un->un_HeadClean & TAPE_ALERT_NOT_SUPPORTED) { 13285 return (rval); 13286 } 13287 13288 if (un->un_HeadClean == TAPE_ALERT_SUPPORT_UNKNOWN) { 13289 13290 rval = st_logpage_supported(un, TAPE_SEQUENTIAL_PAGE); 13291 if (rval == -1) { 13292 return (0); 13293 } 13294 if (rval == 1) { 13295 13296 un->un_HeadClean |= TAPE_SEQUENTIAL_SUPPORTED; 13297 } 13298 13299 rval = st_logpage_supported(un, TAPE_ALERT_PAGE); 13300 if (rval == -1) { 13301 return (0); 13302 } 13303 if (rval == 1) { 13304 13305 un->un_HeadClean |= TAPE_ALERT_SUPPORTED; 13306 } 13307 13308 if (un->un_HeadClean == TAPE_ALERT_SUPPORT_UNKNOWN) { 13309 13310 un->un_HeadClean = TAPE_ALERT_NOT_SUPPORTED; 13311 } 13312 } 13313 13314 rval = 0; 13315 13316 if (un->un_HeadClean & TAPE_SEQUENTIAL_SUPPORTED) { 13317 13318 rval = st_check_sequential_clean_bit(un); 13319 if (rval == -1) { 13320 return (0); 13321 } 13322 } 13323 13324 if ((rval == 0) && (un->un_HeadClean & TAPE_ALERT_SUPPORTED)) { 13325 13326 rval = st_check_alert_flags(un); 13327 if (rval == -1) { 13328 return (0); 13329 } 13330 } 13331 13332 if ((rval == 0) && (un->un_dp->options & ST_CLN_MASK)) { 13333 13334 rval = st_check_sense_clean_bit(un); 13335 if (rval == -1) { 13336 return (0); 13337 } 13338 } 13339 13340 /* 13341 * If found a supported means to check need to clean. 13342 */ 13343 if (rval & MTF_TAPE_CLN_SUPPORTED) { 13344 13345 /* 13346 * head needs to be cleaned. 13347 */ 13348 if (rval & MTF_TAPE_HEAD_DIRTY) { 13349 13350 /* 13351 * Print log message only first time 13352 * found needing cleaned. 13353 */ 13354 if ((un->un_HeadClean & TAPE_PREVIOUSLY_DIRTY) == 0) { 13355 13356 scsi_log(ST_DEVINFO, st_label, CE_WARN, 13357 "Periodic head cleaning required"); 13358 13359 un->un_HeadClean |= TAPE_PREVIOUSLY_DIRTY; 13360 } 13361 13362 } else { 13363 13364 un->un_HeadClean &= ~TAPE_PREVIOUSLY_DIRTY; 13365 } 13366 } 13367 13368 return (rval); 13369 } 13370 13371 13372 static int 13373 st_check_sequential_clean_bit(struct scsi_tape *un) 13374 { 13375 int rval; 13376 int ix; 13377 ushort_t parameter; 13378 struct uscsi_cmd *cmd; 13379 struct log_sequential_page *sp; 13380 struct log_sequential_page_parameter *prm; 13381 struct scsi_arq_status status; 13382 char cdb[CDB_GROUP1] = { 13383 SCMD_LOG_SENSE_G1, 13384 0, 13385 TAPE_SEQUENTIAL_PAGE | CURRENT_CUMULATIVE_VALUES, 13386 0, 13387 0, 13388 0, 13389 0, 13390 (char)(sizeof (struct log_sequential_page) >> 8), 13391 (char)(sizeof (struct log_sequential_page)), 13392 0 13393 }; 13394 13395 ST_FUNC(ST_DEVINFO, st_check_sequential_clean_bit); 13396 13397 cmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 13398 sp = kmem_zalloc(sizeof (struct log_sequential_page), KM_SLEEP); 13399 13400 cmd->uscsi_flags = 13401 USCSI_DIAGNOSE | USCSI_RQENABLE | USCSI_READ; 13402 cmd->uscsi_timeout = un->un_dp->non_motion_timeout; 13403 cmd->uscsi_cdb = cdb; 13404 cmd->uscsi_cdblen = CDB_GROUP1; 13405 cmd->uscsi_bufaddr = (caddr_t)sp; 13406 cmd->uscsi_buflen = sizeof (struct log_sequential_page); 13407 cmd->uscsi_rqlen = sizeof (status); 13408 cmd->uscsi_rqbuf = (caddr_t)&status; 13409 13410 rval = st_uscsi_cmd(un, cmd, FKIOCTL); 13411 13412 if (rval || cmd->uscsi_status || cmd->uscsi_resid) { 13413 13414 rval = -1; 13415 13416 } else if (sp->log_page.code != TAPE_SEQUENTIAL_PAGE) { 13417 13418 rval = -1; 13419 } 13420 13421 prm = &sp->param[0]; 13422 13423 for (ix = 0; rval == 0 && ix < TAPE_SEQUENTIAL_PAGE_PARA; ix++) { 13424 13425 if (prm->log_param.length == 0) { 13426 break; 13427 } 13428 13429 parameter = (((prm->log_param.pc_hi << 8) & 0xff00) + 13430 (prm->log_param.pc_lo & 0xff)); 13431 13432 if (parameter == SEQUENTIAL_NEED_CLN) { 13433 13434 rval = MTF_TAPE_CLN_SUPPORTED; 13435 if (prm->param_value[prm->log_param.length - 1]) { 13436 13437 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13438 "sequential log says head dirty\n"); 13439 rval |= MTF_TAPE_HEAD_DIRTY; 13440 } 13441 } 13442 prm = (struct log_sequential_page_parameter *) 13443 &prm->param_value[prm->log_param.length]; 13444 } 13445 13446 kmem_free(cmd, sizeof (struct uscsi_cmd)); 13447 kmem_free(sp, sizeof (struct log_sequential_page)); 13448 13449 return (rval); 13450 } 13451 13452 13453 static int 13454 st_check_alert_flags(struct scsi_tape *un) 13455 { 13456 struct st_tape_alert *ta; 13457 struct uscsi_cmd *com; 13458 struct scsi_arq_status status; 13459 unsigned ix, length; 13460 int rval; 13461 tape_alert_flags flag; 13462 char cdb[CDB_GROUP1] = { 13463 SCMD_LOG_SENSE_G1, 13464 0, 13465 TAPE_ALERT_PAGE | CURRENT_THRESHOLD_VALUES, 13466 0, 13467 0, 13468 0, 13469 0, 13470 (char)(sizeof (struct st_tape_alert) >> 8), 13471 (char)(sizeof (struct st_tape_alert)), 13472 0 13473 }; 13474 13475 ST_FUNC(ST_DEVINFO, st_check_alert_clean_bit); 13476 13477 com = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 13478 ta = kmem_zalloc(sizeof (struct st_tape_alert), KM_SLEEP); 13479 13480 com->uscsi_cdb = cdb; 13481 com->uscsi_cdblen = CDB_GROUP1; 13482 com->uscsi_bufaddr = (caddr_t)ta; 13483 com->uscsi_buflen = sizeof (struct st_tape_alert); 13484 com->uscsi_rqlen = sizeof (status); 13485 com->uscsi_rqbuf = (caddr_t)&status; 13486 com->uscsi_flags = 13487 USCSI_DIAGNOSE | USCSI_RQENABLE | USCSI_READ; 13488 com->uscsi_timeout = un->un_dp->non_motion_timeout; 13489 13490 rval = st_uscsi_cmd(un, com, FKIOCTL); 13491 13492 if (rval || com->uscsi_status || com->uscsi_resid) { 13493 13494 rval = -1; /* uscsi-command failed */ 13495 13496 } else if (ta->log_page.code != TAPE_ALERT_PAGE) { 13497 13498 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13499 "Not Alert Log Page returned 0x%X\n", ta->log_page.code); 13500 rval = -1; 13501 } 13502 13503 length = (ta->log_page.length_hi << 8) + ta->log_page.length_lo; 13504 13505 13506 if (length != TAPE_ALERT_PARAMETER_LENGTH) { 13507 13508 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13509 "TapeAlert length %d\n", length); 13510 } 13511 13512 13513 for (ix = 0; ix < TAPE_ALERT_MAX_PARA; ix++) { 13514 13515 /* 13516 * if rval is bad before the first pass don't bother 13517 */ 13518 if (ix == 0 && rval != 0) { 13519 13520 break; 13521 } 13522 13523 flag = ((ta->param[ix].log_param.pc_hi << 8) + 13524 ta->param[ix].log_param.pc_lo); 13525 13526 if ((ta->param[ix].param_value & 1) == 0) { 13527 continue; 13528 } 13529 /* 13530 * check to see if current parameter is of interest. 13531 * CLEAN_FOR_ERRORS is vendor specific to 9840 9940 stk's. 13532 */ 13533 if ((flag == TAF_CLEAN_NOW) || 13534 (flag == TAF_CLEAN_PERIODIC) || 13535 ((flag == CLEAN_FOR_ERRORS) && 13536 (un->un_dp->type == ST_TYPE_STK9840))) { 13537 13538 rval = MTF_TAPE_CLN_SUPPORTED; 13539 13540 13541 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13542 "alert_page drive needs clean %d\n", flag); 13543 un->un_HeadClean |= TAPE_ALERT_STILL_DIRTY; 13544 rval |= MTF_TAPE_HEAD_DIRTY; 13545 13546 } else if (flag == TAF_CLEANING_MEDIA) { 13547 13548 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13549 "alert_page drive was cleaned\n"); 13550 un->un_HeadClean &= ~TAPE_ALERT_STILL_DIRTY; 13551 } 13552 13553 } 13554 13555 /* 13556 * Report it as dirty till we see it cleaned 13557 */ 13558 if (un->un_HeadClean & TAPE_ALERT_STILL_DIRTY) { 13559 13560 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13561 "alert_page still dirty\n"); 13562 rval |= MTF_TAPE_HEAD_DIRTY; 13563 } 13564 13565 kmem_free(com, sizeof (struct uscsi_cmd)); 13566 kmem_free(ta, sizeof (struct st_tape_alert)); 13567 13568 return (rval); 13569 } 13570 13571 13572 static int 13573 st_check_sense_clean_bit(struct scsi_tape *un) 13574 { 13575 uchar_t *sensep; 13576 char cdb[CDB_GROUP0]; 13577 struct uscsi_cmd *com; 13578 ushort_t byte_pos; 13579 uchar_t bit_mask; 13580 unsigned length; 13581 int index; 13582 int rval; 13583 13584 ST_FUNC(ST_DEVINFO, st_check_sense_clean_bit); 13585 13586 /* 13587 * Since this tape does not support Tape Alert, 13588 * we now try to get the cleanbit status via 13589 * Request Sense. 13590 */ 13591 13592 if ((un->un_dp->options & ST_CLN_MASK) == ST_CLN_TYPE_1) { 13593 13594 index = 0; 13595 13596 } else if ((un->un_dp->options & ST_CLN_MASK) == ST_CLN_TYPE_2) { 13597 13598 index = 1; 13599 13600 } else if ((un->un_dp->options & ST_CLN_MASK) == ST_CLN_TYPE_3) { 13601 13602 index = 2; 13603 13604 } else { 13605 13606 return (-1); 13607 } 13608 13609 byte_pos = st_cln_bit_position[index].cln_bit_byte; 13610 bit_mask = st_cln_bit_position[index].cln_bit_mask; 13611 length = byte_pos + 1; 13612 13613 com = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 13614 sensep = kmem_zalloc(length, KM_SLEEP); 13615 13616 cdb[0] = SCMD_REQUEST_SENSE; 13617 cdb[1] = 0; 13618 cdb[2] = 0; 13619 cdb[3] = 0; 13620 cdb[4] = (char)length; 13621 cdb[5] = 0; 13622 13623 com->uscsi_cdb = cdb; 13624 com->uscsi_cdblen = CDB_GROUP0; 13625 com->uscsi_bufaddr = (caddr_t)sensep; 13626 com->uscsi_buflen = length; 13627 com->uscsi_flags = 13628 USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ; 13629 com->uscsi_timeout = un->un_dp->non_motion_timeout; 13630 13631 rval = st_uscsi_cmd(un, com, FKIOCTL); 13632 13633 if (rval || com->uscsi_status || com->uscsi_resid) { 13634 13635 rval = -1; 13636 13637 } else { 13638 13639 rval = MTF_TAPE_CLN_SUPPORTED; 13640 if ((sensep[byte_pos] & bit_mask) == bit_mask) { 13641 13642 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13643 "sense data says head dirty\n"); 13644 rval |= MTF_TAPE_HEAD_DIRTY; 13645 } 13646 } 13647 13648 kmem_free(com, sizeof (struct uscsi_cmd)); 13649 kmem_free(sensep, length); 13650 return (rval); 13651 } 13652 13653 /* 13654 * st_clear_unit_attention 13655 * 13656 * run test unit ready's to clear out outstanding 13657 * unit attentions. 13658 * returns zero for SUCCESS or the errno from st_cmd call 13659 */ 13660 static int 13661 st_clear_unit_attentions(dev_t dev_instance, int max_trys) 13662 { 13663 int i = 0; 13664 int rval; 13665 13666 GET_SOFT_STATE(dev_instance); 13667 ST_FUNC(ST_DEVINFO, st_clear_unit_attentions); 13668 13669 do { 13670 rval = st_cmd(un, SCMD_TEST_UNIT_READY, 0, SYNC_CMD); 13671 } while ((rval != 0) && (rval != ENXIO) && (++i < max_trys)); 13672 return (rval); 13673 } 13674 13675 static void 13676 st_calculate_timeouts(struct scsi_tape *un) 13677 { 13678 ST_FUNC(ST_DEVINFO, st_calculate_timeouts); 13679 13680 if (un->un_dp->non_motion_timeout == 0) { 13681 if (un->un_dp->options & ST_LONG_TIMEOUTS) { 13682 un->un_dp->non_motion_timeout = 13683 st_io_time * st_long_timeout_x; 13684 } else { 13685 un->un_dp->non_motion_timeout = (ushort_t)st_io_time; 13686 } 13687 } 13688 13689 if (un->un_dp->io_timeout == 0) { 13690 if (un->un_dp->options & ST_LONG_TIMEOUTS) { 13691 un->un_dp->io_timeout = st_io_time * st_long_timeout_x; 13692 } else { 13693 un->un_dp->io_timeout = (ushort_t)st_io_time; 13694 } 13695 } 13696 13697 if (un->un_dp->rewind_timeout == 0) { 13698 if (un->un_dp->options & ST_LONG_TIMEOUTS) { 13699 un->un_dp->rewind_timeout = 13700 st_space_time * st_long_timeout_x; 13701 } else { 13702 un->un_dp->rewind_timeout = (ushort_t)st_space_time; 13703 } 13704 } 13705 13706 if (un->un_dp->space_timeout == 0) { 13707 if (un->un_dp->options & ST_LONG_TIMEOUTS) { 13708 un->un_dp->space_timeout = 13709 st_space_time * st_long_timeout_x; 13710 } else { 13711 un->un_dp->space_timeout = (ushort_t)st_space_time; 13712 } 13713 } 13714 13715 if (un->un_dp->load_timeout == 0) { 13716 if (un->un_dp->options & ST_LONG_TIMEOUTS) { 13717 un->un_dp->load_timeout = 13718 st_space_time * st_long_timeout_x; 13719 } else { 13720 un->un_dp->load_timeout = (ushort_t)st_space_time; 13721 } 13722 } 13723 13724 if (un->un_dp->unload_timeout == 0) { 13725 if (un->un_dp->options & ST_LONG_TIMEOUTS) { 13726 un->un_dp->unload_timeout = 13727 st_space_time * st_long_timeout_x; 13728 } else { 13729 un->un_dp->unload_timeout = (ushort_t)st_space_time; 13730 } 13731 } 13732 13733 if (un->un_dp->erase_timeout == 0) { 13734 if (un->un_dp->options & ST_LONG_ERASE) { 13735 un->un_dp->erase_timeout = 13736 st_space_time * st_long_space_time_x; 13737 } else { 13738 un->un_dp->erase_timeout = (ushort_t)st_space_time; 13739 } 13740 } 13741 } 13742 13743 13744 static writablity 13745 st_is_not_wormable(struct scsi_tape *un) 13746 { 13747 ST_FUNC(ST_DEVINFO, st_is_not_wormable); 13748 return (RDWR); 13749 } 13750 13751 static writablity 13752 st_is_hp_dat_tape_worm(struct scsi_tape *un) 13753 { 13754 writablity wrt; 13755 13756 ST_FUNC(ST_DEVINFO, st_is_hp_dat_tape_worm); 13757 13758 /* Mode sense should be current */ 13759 if (un->un_mspl->media_type == 1) { 13760 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13761 "Drive has WORM media loaded\n"); 13762 wrt = WORM; 13763 } else { 13764 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13765 "Drive has non WORM media loaded\n"); 13766 wrt = RDWR; 13767 } 13768 return (wrt); 13769 } 13770 13771 #define HP_DAT_INQUIRY 0x4A 13772 static writablity 13773 st_is_hp_dat_worm(struct scsi_tape *un) 13774 { 13775 char *buf; 13776 int result; 13777 writablity wrt; 13778 13779 ST_FUNC(ST_DEVINFO, st_is_hp_dat_worm); 13780 13781 buf = kmem_zalloc(HP_DAT_INQUIRY, KM_SLEEP); 13782 13783 result = st_get_special_inquiry(un, HP_DAT_INQUIRY, buf, 0); 13784 13785 if (result != 0) { 13786 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13787 "Read Standard Inquiry for WORM support failed"); 13788 wrt = FAILED; 13789 } else if ((buf[40] & 1) == 0) { 13790 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13791 "Drive is NOT WORMable\n"); 13792 /* This drive doesn't support it so don't check again */ 13793 un->un_dp->options &= ~ST_WORMABLE; 13794 wrt = RDWR; 13795 un->un_wormable = st_is_not_wormable; 13796 } else { 13797 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13798 "Drive supports WORM version %d\n", buf[40] >> 1); 13799 un->un_wormable = st_is_hp_dat_tape_worm; 13800 wrt = un->un_wormable(un); 13801 } 13802 13803 kmem_free(buf, HP_DAT_INQUIRY); 13804 13805 /* 13806 * If drive doesn't support it no point in checking further. 13807 */ 13808 return (wrt); 13809 } 13810 13811 static writablity 13812 st_is_hp_lto_tape_worm(struct scsi_tape *un) 13813 { 13814 writablity wrt; 13815 13816 ST_FUNC(ST_DEVINFO, st_is_hp_lto_tape_worm); 13817 13818 /* Mode sense should be current */ 13819 switch (un->un_mspl->media_type) { 13820 case 0x00: 13821 switch (un->un_mspl->density) { 13822 case 0x40: 13823 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13824 "Drive has standard Gen I media loaded\n"); 13825 break; 13826 case 0x42: 13827 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13828 "Drive has standard Gen II media loaded\n"); 13829 break; 13830 case 0x44: 13831 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13832 "Drive has standard Gen III media loaded\n"); 13833 break; 13834 case 0x46: 13835 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13836 "Drive has standard Gen IV media loaded\n"); 13837 break; 13838 default: 13839 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13840 "Drive has standard unknown 0x%X media loaded\n", 13841 un->un_mspl->density); 13842 } 13843 wrt = RDWR; 13844 break; 13845 case 0x01: 13846 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13847 "Drive has WORM medium loaded\n"); 13848 wrt = WORM; 13849 break; 13850 case 0x80: 13851 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13852 "Drive has CD-ROM emulation medium loaded\n"); 13853 wrt = WORM; 13854 break; 13855 default: 13856 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13857 "Drive has an unexpected medium type 0x%X loaded\n", 13858 un->un_mspl->media_type); 13859 wrt = RDWR; 13860 } 13861 13862 return (wrt); 13863 } 13864 13865 #define LTO_REQ_INQUIRY 44 13866 static writablity 13867 st_is_hp_lto_worm(struct scsi_tape *un) 13868 { 13869 char *buf; 13870 int result; 13871 writablity wrt; 13872 13873 ST_FUNC(ST_DEVINFO, st_is_hp_lto_worm); 13874 13875 buf = kmem_zalloc(LTO_REQ_INQUIRY, KM_SLEEP); 13876 13877 result = st_get_special_inquiry(un, LTO_REQ_INQUIRY, buf, 0); 13878 13879 if (result != 0) { 13880 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13881 "Read Standard Inquiry for WORM support failed"); 13882 wrt = FAILED; 13883 } else if ((buf[40] & 1) == 0) { 13884 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13885 "Drive is NOT WORMable\n"); 13886 /* This drive doesn't support it so don't check again */ 13887 un->un_dp->options &= ~ST_WORMABLE; 13888 wrt = RDWR; 13889 un->un_wormable = st_is_not_wormable; 13890 } else { 13891 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13892 "Drive supports WORM version %d\n", buf[40] >> 1); 13893 un->un_wormable = st_is_hp_lto_tape_worm; 13894 wrt = un->un_wormable(un); 13895 } 13896 13897 kmem_free(buf, LTO_REQ_INQUIRY); 13898 13899 /* 13900 * If drive doesn't support it no point in checking further. 13901 */ 13902 return (wrt); 13903 } 13904 13905 static writablity 13906 st_is_t10_worm_device(struct scsi_tape *un) 13907 { 13908 writablity wrt; 13909 13910 ST_FUNC(ST_DEVINFO, st_is_t10_worm_device); 13911 13912 if (un->un_mspl->media_type == 0x3c) { 13913 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13914 "Drive has WORM media loaded\n"); 13915 wrt = WORM; 13916 } else { 13917 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13918 "Drive has non WORM media loaded\n"); 13919 wrt = RDWR; 13920 } 13921 return (wrt); 13922 } 13923 13924 #define SEQ_CAP_PAGE (char)0xb0 13925 static writablity 13926 st_is_t10_worm(struct scsi_tape *un) 13927 { 13928 char *buf; 13929 int result; 13930 writablity wrt; 13931 13932 ST_FUNC(ST_DEVINFO, st_is_t10_worm); 13933 13934 buf = kmem_zalloc(6, KM_SLEEP); 13935 13936 result = st_get_special_inquiry(un, 6, buf, SEQ_CAP_PAGE); 13937 13938 if (result != 0) { 13939 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13940 "Read Vitial Inquiry for Sequental Capability" 13941 " WORM support failed %x", result); 13942 wrt = FAILED; 13943 } else if ((buf[4] & 1) == 0) { 13944 ASSERT(buf[1] == SEQ_CAP_PAGE); 13945 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13946 "Drive is NOT WORMable\n"); 13947 /* This drive doesn't support it so don't check again */ 13948 un->un_dp->options &= ~ST_WORMABLE; 13949 wrt = RDWR; 13950 un->un_wormable = st_is_not_wormable; 13951 } else { 13952 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13953 "Drive supports WORM\n"); 13954 un->un_wormable = st_is_t10_worm_device; 13955 wrt = un->un_wormable(un); 13956 } 13957 13958 kmem_free(buf, 6); 13959 13960 return (wrt); 13961 } 13962 13963 13964 #define STK_REQ_SENSE 26 13965 13966 static writablity 13967 st_is_stk_worm(struct scsi_tape *un) 13968 { 13969 char cdb[CDB_GROUP0] = {SCMD_REQUEST_SENSE, 0, 0, 0, STK_REQ_SENSE, 0}; 13970 struct scsi_extended_sense *sense; 13971 struct uscsi_cmd *cmd; 13972 char *buf; 13973 int result; 13974 writablity wrt; 13975 13976 ST_FUNC(ST_DEVINFO, st_is_stk_worm); 13977 13978 cmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 13979 buf = kmem_alloc(STK_REQ_SENSE, KM_SLEEP); 13980 sense = (struct scsi_extended_sense *)buf; 13981 13982 cmd->uscsi_flags = USCSI_READ; 13983 cmd->uscsi_timeout = un->un_dp->non_motion_timeout; 13984 cmd->uscsi_cdb = &cdb[0]; 13985 cmd->uscsi_bufaddr = buf; 13986 cmd->uscsi_buflen = STK_REQ_SENSE; 13987 cmd->uscsi_cdblen = CDB_GROUP0; 13988 cmd->uscsi_rqlen = 0; 13989 cmd->uscsi_rqbuf = NULL; 13990 13991 result = st_uscsi_cmd(un, cmd, FKIOCTL); 13992 13993 if (result != 0 || cmd->uscsi_status != 0) { 13994 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13995 "Request Sense for WORM failed"); 13996 wrt = RDWR; 13997 } else if (sense->es_add_len + 8 < 24) { 13998 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13999 "Drive didn't send enough sense data for WORM byte %d\n", 14000 sense->es_add_len + 8); 14001 wrt = RDWR; 14002 un->un_wormable = st_is_not_wormable; 14003 } else if ((buf[24]) & 0x02) { 14004 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 14005 "Drive has WORM tape loaded\n"); 14006 wrt = WORM; 14007 un->un_wormable = st_is_stk_worm; 14008 } else { 14009 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 14010 "Drive has normal tape loaded\n"); 14011 wrt = RDWR; 14012 un->un_wormable = st_is_stk_worm; 14013 } 14014 14015 kmem_free(buf, STK_REQ_SENSE); 14016 kmem_free(cmd, sizeof (struct uscsi_cmd)); 14017 return (wrt); 14018 } 14019 14020 #define DLT_INQ_SZ 44 14021 14022 static writablity 14023 st_is_dlt_tape_worm(struct scsi_tape *un) 14024 { 14025 caddr_t buf; 14026 int result; 14027 writablity wrt; 14028 14029 ST_FUNC(ST_DEVINFO, st_is_dlt_tape_worm); 14030 14031 buf = kmem_alloc(DLT_INQ_SZ, KM_SLEEP); 14032 14033 /* Read Attribute Media Type */ 14034 14035 result = st_read_attributes(un, 0x0408, buf, 10, st_uscsi_cmd); 14036 14037 /* 14038 * If this quantum drive is attached via an HBA that cannot 14039 * support thr read attributes command return error in the 14040 * hope that someday they will support the t10 method. 14041 */ 14042 if (result == EINVAL && un->un_max_cdb_sz < CDB_GROUP4) { 14043 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 14044 "Read Attribute Command for WORM Media detection is not " 14045 "supported on the HBA that this drive is attached to."); 14046 wrt = RDWR; 14047 un->un_wormable = st_is_not_wormable; 14048 goto out; 14049 } 14050 14051 if (result != 0) { 14052 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 14053 "Read Attribute Command for WORM Media returned 0x%x", 14054 result); 14055 wrt = RDWR; 14056 un->un_dp->options &= ~ST_WORMABLE; 14057 goto out; 14058 } 14059 14060 if ((uchar_t)buf[9] == 0x80) { 14061 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 14062 "Drive media is WORM\n"); 14063 wrt = WORM; 14064 } else { 14065 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 14066 "Drive media is not WORM Media 0x%x\n", (uchar_t)buf[9]); 14067 wrt = RDWR; 14068 } 14069 14070 out: 14071 kmem_free(buf, DLT_INQ_SZ); 14072 return (wrt); 14073 } 14074 14075 static writablity 14076 st_is_dlt_worm(struct scsi_tape *un) 14077 { 14078 caddr_t buf; 14079 int result; 14080 writablity wrt; 14081 14082 ST_FUNC(ST_DEVINFO, st_is_dlt_worm); 14083 14084 buf = kmem_alloc(DLT_INQ_SZ, KM_SLEEP); 14085 14086 result = st_get_special_inquiry(un, DLT_INQ_SZ, buf, 0xC0); 14087 14088 if (result != 0) { 14089 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 14090 "Read Vendor Specific Inquiry for WORM support failed"); 14091 wrt = RDWR; 14092 goto out; 14093 } 14094 14095 if ((buf[2] & 1) == 0) { 14096 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 14097 "Drive is not WORMable\n"); 14098 wrt = RDWR; 14099 un->un_dp->options &= ~ST_WORMABLE; 14100 un->un_wormable = st_is_not_wormable; 14101 goto out; 14102 } else { 14103 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 14104 "Drive is WORMable\n"); 14105 un->un_wormable = st_is_dlt_tape_worm; 14106 wrt = un->un_wormable(un); 14107 } 14108 out: 14109 kmem_free(buf, DLT_INQ_SZ); 14110 14111 return (wrt); 14112 } 14113 14114 typedef struct { 14115 struct modeheader_seq header; 14116 #if defined(_BIT_FIELDS_LTOH) /* X86 */ 14117 uchar_t pagecode :6, 14118 :2; 14119 uchar_t page_len; 14120 uchar_t syslogalive :2, 14121 device :1, 14122 abs :1, 14123 ulpbot :1, 14124 prth :1, 14125 ponej :1, 14126 ait :1; 14127 uchar_t span; 14128 14129 uchar_t :6, 14130 worm :1, 14131 mic :1; 14132 uchar_t worm_cap :1, 14133 :7; 14134 uint32_t :32; 14135 #else /* SPARC */ 14136 uchar_t :2, 14137 pagecode :6; 14138 uchar_t page_len; 14139 uchar_t ait :1, 14140 device :1, 14141 abs :1, 14142 ulpbot :1, 14143 prth :1, 14144 ponej :1, 14145 syslogalive :2; 14146 uchar_t span; 14147 uchar_t mic :1, 14148 worm :1, 14149 :6; 14150 uchar_t :7, 14151 worm_cap :1; 14152 uint32_t :32; 14153 #endif 14154 }ait_dev_con; 14155 14156 #define AIT_DEV_PAGE 0x31 14157 static writablity 14158 st_is_sony_worm(struct scsi_tape *un) 14159 { 14160 int result; 14161 writablity wrt; 14162 ait_dev_con *ait_conf; 14163 14164 ST_FUNC(ST_DEVINFO, st_is_sony_worm); 14165 14166 ait_conf = kmem_zalloc(sizeof (ait_dev_con), KM_SLEEP); 14167 14168 result = st_gen_mode_sense(un, st_uscsi_cmd, AIT_DEV_PAGE, 14169 (struct seq_mode *)ait_conf, sizeof (ait_dev_con)); 14170 14171 if (result == 0) { 14172 14173 if (ait_conf->pagecode != AIT_DEV_PAGE) { 14174 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 14175 "returned page 0x%x not 0x%x AIT_DEV_PAGE\n", 14176 ait_conf->pagecode, AIT_DEV_PAGE); 14177 wrt = RDWR; 14178 un->un_wormable = st_is_not_wormable; 14179 14180 } else if (ait_conf->worm_cap) { 14181 14182 un->un_wormable = st_is_sony_worm; 14183 14184 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 14185 "Drives is WORMable\n"); 14186 if (ait_conf->worm) { 14187 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 14188 "Media is WORM\n"); 14189 wrt = WORM; 14190 } else { 14191 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 14192 "Media is not WORM\n"); 14193 wrt = RDWR; 14194 } 14195 14196 } else { 14197 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 14198 "Drives not is WORMable\n"); 14199 wrt = RDWR; 14200 /* No further checking required */ 14201 un->un_dp->options &= ~ST_WORMABLE; 14202 } 14203 14204 } else { 14205 14206 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 14207 "AIT device config mode sense page read command failed" 14208 " result = %d ", result); 14209 wrt = FAILED; 14210 un->un_wormable = st_is_not_wormable; 14211 } 14212 14213 kmem_free(ait_conf, sizeof (ait_dev_con)); 14214 return (wrt); 14215 } 14216 14217 static writablity 14218 st_is_drive_worm(struct scsi_tape *un) 14219 { 14220 writablity wrt; 14221 14222 ST_FUNC(ST_DEVINFO, st_is_sony_worm); 14223 14224 switch (un->un_dp->type) { 14225 case MT_ISDLT: 14226 wrt = st_is_dlt_worm(un); 14227 break; 14228 14229 case MT_ISSTK9840: 14230 wrt = st_is_stk_worm(un); 14231 break; 14232 14233 case MT_IS8MM: 14234 case MT_ISAIT: 14235 wrt = st_is_sony_worm(un); 14236 break; 14237 14238 case MT_LTO: 14239 if (strncmp("HP ", un->un_dp->vid, 3) == 0) { 14240 wrt = st_is_hp_lto_worm(un); 14241 } else { 14242 wrt = st_is_t10_worm(un); 14243 } 14244 break; 14245 14246 case MT_ISDAT: 14247 if (strncmp("HP ", un->un_dp->vid, 3) == 0) { 14248 wrt = st_is_hp_dat_worm(un); 14249 } else { 14250 wrt = st_is_t10_worm(un); 14251 } 14252 break; 14253 14254 default: 14255 wrt = FAILED; 14256 break; 14257 } 14258 14259 /* 14260 * If any of the above failed try the t10 standard method. 14261 */ 14262 if (wrt == FAILED) { 14263 wrt = st_is_t10_worm(un); 14264 } 14265 14266 /* 14267 * Unknown method for detecting WORM media. 14268 */ 14269 if (wrt == FAILED) { 14270 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 14271 "Unknown method for WORM media detection\n"); 14272 wrt = RDWR; 14273 un->un_dp->options &= ~ST_WORMABLE; 14274 } 14275 14276 return (wrt); 14277 } 14278 14279 static int 14280 st_read_attributes(struct scsi_tape *un, uint16_t attribute, void *pnt, 14281 size_t size, ubufunc_t bufunc) 14282 { 14283 char cdb[CDB_GROUP4]; 14284 int result; 14285 struct uscsi_cmd *cmd; 14286 struct scsi_arq_status status; 14287 14288 caddr_t buf = (caddr_t)pnt; 14289 14290 ST_FUNC(ST_DEVINFO, st_read_attributes); 14291 14292 if (un->un_sd->sd_inq->inq_ansi < 3) { 14293 return (ENOTTY); 14294 } 14295 14296 cmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 14297 14298 cdb[0] = (char)SCMD_READ_ATTRIBUTE; 14299 cdb[1] = 0; 14300 cdb[2] = 0; 14301 cdb[3] = 0; 14302 cdb[4] = 0; 14303 cdb[5] = 0; 14304 cdb[6] = 0; 14305 cdb[7] = 0; 14306 cdb[8] = (char)(attribute >> 8); 14307 cdb[9] = (char)(attribute); 14308 cdb[10] = (char)(size >> 24); 14309 cdb[11] = (char)(size >> 16); 14310 cdb[12] = (char)(size >> 8); 14311 cdb[13] = (char)(size); 14312 cdb[14] = 0; 14313 cdb[15] = 0; 14314 14315 14316 cmd->uscsi_flags = USCSI_READ | USCSI_RQENABLE | USCSI_DIAGNOSE; 14317 cmd->uscsi_timeout = un->un_dp->non_motion_timeout; 14318 cmd->uscsi_cdb = &cdb[0]; 14319 cmd->uscsi_bufaddr = (caddr_t)buf; 14320 cmd->uscsi_buflen = size; 14321 cmd->uscsi_cdblen = sizeof (cdb); 14322 cmd->uscsi_rqlen = sizeof (status); 14323 cmd->uscsi_rqbuf = (caddr_t)&status; 14324 14325 result = bufunc(un, cmd, FKIOCTL); 14326 14327 if (result != 0 || cmd->uscsi_status != 0) { 14328 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 14329 "st_read_attribute failed: result %d status %d\n", 14330 result, cmd->uscsi_status); 14331 /* 14332 * If this returns invalid operation code don't try again. 14333 */ 14334 if (un->un_sd->sd_sense->es_key == KEY_ILLEGAL_REQUEST && 14335 un->un_sd->sd_sense->es_add_code == 0x20) { 14336 result = ENOTTY; 14337 } else if (result == 0) { 14338 result = EIO; 14339 } 14340 14341 } else { 14342 14343 /* 14344 * The attribute retured should match the attribute requested. 14345 */ 14346 if (buf[4] != cdb[8] || buf[5] != cdb[9]) { 14347 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 14348 "st_read_attribute got wrong data back expected " 14349 "0x%x got 0x%x\n", attribute, buf[6] << 8 | buf[7]); 14350 st_clean_print(ST_DEVINFO, st_label, SCSI_DEBUG, 14351 "bad? data", buf, size); 14352 result = EIO; 14353 } 14354 } 14355 14356 kmem_free(cmd, sizeof (struct uscsi_cmd)); 14357 14358 return (result); 14359 } 14360 14361 static int 14362 st_get_special_inquiry(struct scsi_tape *un, uchar_t size, caddr_t dest, 14363 uchar_t page) 14364 { 14365 char cdb[CDB_GROUP0]; 14366 struct scsi_extended_sense *sense; 14367 struct uscsi_cmd *cmd; 14368 int result; 14369 14370 ST_FUNC(ST_DEVINFO, st_get_special_inquiry); 14371 14372 cdb[0] = SCMD_INQUIRY; 14373 cdb[1] = page ? 1 : 0; 14374 cdb[2] = page; 14375 cdb[3] = 0; 14376 cdb[4] = size; 14377 cdb[5] = 0; 14378 14379 cmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 14380 sense = kmem_alloc(sizeof (struct scsi_extended_sense), KM_SLEEP); 14381 14382 cmd->uscsi_flags = USCSI_READ | USCSI_RQENABLE; 14383 cmd->uscsi_timeout = un->un_dp->non_motion_timeout; 14384 cmd->uscsi_cdb = &cdb[0]; 14385 cmd->uscsi_bufaddr = dest; 14386 cmd->uscsi_buflen = size; 14387 cmd->uscsi_cdblen = CDB_GROUP0; 14388 cmd->uscsi_rqlen = sizeof (struct scsi_extended_sense); 14389 cmd->uscsi_rqbuf = (caddr_t)sense; 14390 14391 result = st_uscsi_cmd(un, cmd, FKIOCTL); 14392 14393 if (result != 0 || cmd->uscsi_status != 0) { 14394 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 14395 "st_get_special_inquiry() failed for page %x", page); 14396 if (result == 0) { 14397 result = EIO; 14398 } 14399 } 14400 14401 kmem_free(sense, sizeof (struct scsi_extended_sense)); 14402 kmem_free(cmd, sizeof (struct uscsi_cmd)); 14403 14404 return (result); 14405 } 14406 14407 14408 static int 14409 st_update_block_pos(struct scsi_tape *un, bufunc_t bf, int post_space) 14410 { 14411 int rval = ENOTTY; 14412 uchar_t status = un->un_status; 14413 posmode previous_pmode = un->un_running.pmode; 14414 14415 ST_FUNC(ST_DEVINFO, st_update_block_pos); 14416 14417 while (un->un_read_pos_type != NO_POS) { 14418 rval = bf(un, SCMD_READ_POSITION, 32, SYNC_CMD); 14419 14420 /* 14421 * If read position command returned good status 14422 * Parse the data to see if the position can be interpreted. 14423 */ 14424 if ((rval == 0) && 14425 ((rval = st_interpret_read_pos(un, &un->un_pos, 14426 un->un_read_pos_type, 32, (caddr_t)un->un_read_pos_data, 14427 post_space)) == 0)) { 14428 /* 14429 * Update the running position as well if un_pos was 14430 * ok. But only if recovery is enabled. 14431 */ 14432 if (st_recov_sz != sizeof (recov_info)) { 14433 break; 14434 } 14435 rval = st_interpret_read_pos(un, &un->un_running, 14436 un->un_read_pos_type, 32, 14437 (caddr_t)un->un_read_pos_data, post_space); 14438 un->un_status = status; 14439 break; 14440 } else if (un->un_status == KEY_UNIT_ATTENTION) { 14441 un->un_running.pmode = previous_pmode; 14442 continue; 14443 } else if (un->un_status != KEY_ILLEGAL_REQUEST) { 14444 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 14445 "st_update_block_pos() read position cmd 0x%x" 14446 " returned 0x%x un_status = %d", 14447 un->un_read_pos_type, rval, un->un_status); 14448 /* ENOTTY means it read garbage. try something else. */ 14449 if (rval == ENOTTY) { 14450 rval = EIO; /* so ENOTTY is not final rval */ 14451 } else { 14452 break; 14453 } 14454 } else { 14455 ST_DEBUG4(ST_DEVINFO, st_label, CE_NOTE, 14456 "st_update_block_pos() read position cmd %x" 14457 " returned %x", un->un_read_pos_type, rval); 14458 un->un_running.pmode = previous_pmode; 14459 } 14460 14461 switch (un->un_read_pos_type) { 14462 case SHORT_POS: 14463 un->un_read_pos_type = NO_POS; 14464 break; 14465 14466 case LONG_POS: 14467 un->un_read_pos_type = EXT_POS; 14468 break; 14469 14470 case EXT_POS: 14471 un->un_read_pos_type = SHORT_POS; 14472 break; 14473 14474 default: 14475 ST_DEBUG(ST_DEVINFO, st_label, CE_PANIC, 14476 "Unexpected read position type 0x%x", 14477 un->un_read_pos_type); 14478 } 14479 un->un_status = KEY_NO_SENSE; 14480 } 14481 14482 return (rval); 14483 } 14484 14485 static int 14486 st_get_read_pos(struct scsi_tape *un, buf_t *bp) 14487 { 14488 int result; 14489 size_t d_sz; 14490 caddr_t pos_info; 14491 struct uscsi_cmd *cmd = (struct uscsi_cmd *)bp->b_back; 14492 14493 ST_FUNC(ST_DEVINFO, st_get_read_pos); 14494 14495 if (cmd->uscsi_bufaddr == NULL || cmd->uscsi_buflen <= 0) { 14496 return (0); 14497 } 14498 14499 if (bp_mapin_common(bp, VM_NOSLEEP) == NULL) { 14500 14501 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 14502 "bp_mapin_common() failed"); 14503 14504 return (EIO); 14505 } 14506 14507 d_sz = bp->b_bcount - bp->b_resid; 14508 if (d_sz == 0) { 14509 bp_mapout(bp); 14510 return (EIO); 14511 } 14512 14513 /* 14514 * Copy the buf to a double-word aligned memory that can hold the 14515 * tape_position_t data structure. 14516 */ 14517 if ((pos_info = kmem_alloc(d_sz, KM_NOSLEEP)) == NULL) { 14518 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 14519 "kmem_alloc() failed"); 14520 bp_mapout(bp); 14521 return (EIO); 14522 } 14523 bcopy(bp->b_un.b_addr, pos_info, d_sz); 14524 14525 #ifdef STDEBUG 14526 if ((st_debug & 0x7) > 2) { 14527 st_clean_print(ST_DEVINFO, st_label, SCSI_DEBUG, 14528 "st_get_read_pos() position info", 14529 pos_info, bp->b_bcount); 14530 } 14531 #endif 14532 14533 result = st_interpret_read_pos(un, &un->un_pos, cmd->uscsi_cdb[1], 14534 d_sz, pos_info, 0); 14535 14536 COPY_POS(&un->un_running, &un->un_pos); 14537 14538 kmem_free(pos_info, d_sz); 14539 bp_mapout(bp); 14540 14541 return (result); 14542 } 14543 14544 #if defined(_BIG_ENDIAN) 14545 14546 #define FIX_ENDIAN16(x) 14547 #define FIX_ENDIAN32(x) 14548 #define FIX_ENDIAN64(x) 14549 14550 #elif defined(_LITTLE_ENDIAN) 14551 14552 static void 14553 st_swap16(uint16_t *val) 14554 { 14555 uint16_t tmp; 14556 14557 tmp = (*val >> 8) & 0xff; 14558 tmp |= (*val << 8) & 0xff00; 14559 14560 *val = tmp; 14561 } 14562 14563 static void 14564 st_swap32(uint32_t *val) 14565 { 14566 uint32_t tmp; 14567 14568 tmp = (*val >> 24) & 0xff; 14569 tmp |= (*val >> 8) & 0xff00; 14570 tmp |= (*val << 8) & 0xff0000; 14571 tmp |= (*val << 24) & 0xff000000; 14572 14573 *val = tmp; 14574 } 14575 14576 static void 14577 st_swap64(uint64_t *val) 14578 { 14579 uint32_t low; 14580 uint32_t high; 14581 14582 low = (uint32_t)(*val); 14583 high = (uint32_t)(*val >> 32); 14584 14585 st_swap32(&low); 14586 st_swap32(&high); 14587 14588 *val = high; 14589 *val |= ((uint64_t)low << 32); 14590 } 14591 14592 #define FIX_ENDIAN16(x) st_swap16(x) 14593 #define FIX_ENDIAN32(x) st_swap32(x) 14594 #define FIX_ENDIAN64(x) st_swap64(x) 14595 #endif 14596 14597 /* 14598 * st_interpret_read_pos() 14599 * 14600 * Returns: 14601 * 0 If secsessful. 14602 * EIO If read postion responce data was unuseable or invalid. 14603 * ERANGE If the position of the drive is too large for the read_p_type. 14604 * ENOTTY If the responce data looks invalid for the read position type. 14605 */ 14606 14607 static int 14608 st_interpret_read_pos(struct scsi_tape const *un, tapepos_t *dest, 14609 read_p_types type, size_t data_sz, const caddr_t responce, int post_space) 14610 { 14611 int rval = 0; 14612 int flag = 0; 14613 tapepos_t org; 14614 14615 ST_FUNC(ST_DEVINFO, st_interpret_read_pos); 14616 14617 /* 14618 * We expect the position value to change after a space command. 14619 * So if post_space is set we don't print out what has changed. 14620 */ 14621 if ((dest != &un->un_pos) && (post_space == 0) && 14622 (st_recov_sz == sizeof (recov_info))) { 14623 COPY_POS(&org, dest); 14624 flag = 1; 14625 } 14626 14627 /* 14628 * See what kind of read position was requested. 14629 */ 14630 switch (type) { 14631 14632 case SHORT_POS: /* Short data format */ 14633 { 14634 tape_position_t *pos_info = (tape_position_t *)responce; 14635 uint32_t value; 14636 14637 /* If reserved fields are non zero don't use the data */ 14638 if (pos_info->reserved0 || pos_info->reserved1 || 14639 pos_info->reserved2[0] || pos_info->reserved2[1] || 14640 pos_info->reserved3) { 14641 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 14642 "Invalid Read Short Position Data returned\n"); 14643 rval = EIO; 14644 break; 14645 } 14646 /* 14647 * Position is to large to use this type of read position. 14648 */ 14649 if (pos_info->posi_err == 1) { 14650 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 14651 "Drive reported position error\n"); 14652 rval = ERANGE; 14653 break; 14654 } 14655 /* 14656 * If your at the begining of partition and end at the same 14657 * time it's very small partition or bad data. 14658 */ 14659 if (pos_info->begin_of_part && pos_info->end_of_part) { 14660 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 14661 "SHORT_POS returned begin and end of" 14662 " partition\n"); 14663 rval = EIO; 14664 break; 14665 } 14666 14667 if (pos_info->blk_posi_unkwn == 0) { 14668 14669 value = pos_info->host_block; 14670 FIX_ENDIAN32(&value); 14671 14672 /* 14673 * If the tape is rewound the host blcok should be 0. 14674 */ 14675 if ((pos_info->begin_of_part == 1) && 14676 (value != 0)) { 14677 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 14678 "SHORT_POS returned begin of partition" 14679 " but host block was 0x%x\n", value); 14680 rval = EIO; 14681 break; 14682 } 14683 14684 if (dest->lgclblkno != value) { 14685 if (flag) 14686 flag++; 14687 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 14688 "SHORT_POS current logical 0x%"PRIx64" read" 14689 " 0x%x\n", dest->lgclblkno, value); 14690 } 14691 14692 dest->lgclblkno = (uint64_t)value; 14693 14694 /* 14695 * If the begining of partition is true and the 14696 * block number is zero we will beleive that it is 14697 * rewound. Promote the pmode to legacy. 14698 */ 14699 if ((pos_info->begin_of_part == 1) && 14700 (value == 0)) { 14701 dest->blkno = 0; 14702 dest->fileno = 0; 14703 if (dest->pmode != legacy) 14704 dest->pmode = legacy; 14705 /* 14706 * otherwise if the pmode was invalid, 14707 * promote it to logical. 14708 */ 14709 } else if (dest->pmode == invalid) { 14710 dest->pmode = logical; 14711 } 14712 14713 if (dest->partition != pos_info->partition_number) { 14714 if (flag) 14715 flag++; 14716 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 14717 "SHORT_POS current partition %d read %d\n", 14718 dest->partition, 14719 pos_info->partition_number); 14720 } 14721 14722 dest->partition = pos_info->partition_number; 14723 14724 } else { 14725 dest->pmode = invalid; 14726 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 14727 "Tape drive reported block position as unknown\n"); 14728 } 14729 break; 14730 } 14731 14732 case LONG_POS: /* Long data format */ 14733 { 14734 uint64_t value; 14735 tape_position_long_t *long_pos_info = 14736 (tape_position_long_t *)responce; 14737 14738 /* If reserved fields are non zero don't use the data */ 14739 if ((long_pos_info->reserved0) || 14740 (long_pos_info->reserved1) || 14741 (long_pos_info->reserved2)) { 14742 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 14743 "Invalid Read Long Position Data returned\n"); 14744 rval = ENOTTY; 14745 break; 14746 } 14747 14748 /* Is position Valid */ 14749 if (long_pos_info->blk_posi_unkwn == 0) { 14750 uint32_t part; 14751 14752 value = long_pos_info->block_number; 14753 FIX_ENDIAN64(&value); 14754 14755 /* 14756 * If it says we are at the begining of partition 14757 * the block value better be 0. 14758 */ 14759 if ((long_pos_info->begin_of_part == 1) && 14760 (value != 0)) { 14761 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 14762 "LONG_POS returned begin of partition but" 14763 " block number was 0x%"PRIx64"\n", value); 14764 rval = ENOTTY; 14765 break; 14766 } 14767 /* 14768 * Can't be at the start and the end of the partition 14769 * at the same time if the partition is larger the 0. 14770 */ 14771 if (long_pos_info->begin_of_part && 14772 long_pos_info->end_of_part) { 14773 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 14774 "LONG_POS returned begin and end of" 14775 " partition\n"); 14776 rval = ENOTTY; 14777 break; 14778 } 14779 14780 /* 14781 * If the logical block number is not what we expected. 14782 */ 14783 if (dest->lgclblkno != value) { 14784 if (flag) 14785 flag++; 14786 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 14787 "LONG_POS current logical 0x%"PRIx64 14788 " read 0x%"PRIx64"\n", 14789 dest->lgclblkno, value); 14790 } 14791 dest->lgclblkno = value; 14792 14793 /* 14794 * If the begining of partition is true and the 14795 * block number is zero we will beleive that it is 14796 * rewound. Promote the pmode to legacy. 14797 */ 14798 if ((long_pos_info->begin_of_part == 1) && 14799 (long_pos_info->block_number == 0)) { 14800 dest->blkno = 0; 14801 dest->fileno = 0; 14802 if (dest->pmode != legacy) 14803 dest->pmode = legacy; 14804 /* 14805 * otherwise if the pmode was invalid, 14806 * promote it to logical. 14807 */ 14808 } else if (dest->pmode == invalid) { 14809 dest->pmode = logical; 14810 } 14811 14812 part = long_pos_info->partition; 14813 FIX_ENDIAN32(&part); 14814 if (dest->partition != part) { 14815 if (flag) 14816 flag++; 14817 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 14818 "LONG_POS current partition %d" 14819 " read %d\n", dest->partition, part); 14820 } 14821 dest->partition = part; 14822 } else { 14823 /* 14824 * If the drive doesn't know location, 14825 * we don't either. 14826 */ 14827 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 14828 "Tape drive reported block position as unknown\n"); 14829 dest->pmode = invalid; 14830 } 14831 14832 /* Is file position valid */ 14833 if (long_pos_info->mrk_posi_unkwn == 0) { 14834 value = long_pos_info->file_number; 14835 FIX_ENDIAN64(&value); 14836 /* 14837 * If it says we are at the begining of partition 14838 * the block value better be 0. 14839 */ 14840 if ((long_pos_info->begin_of_part == 1) && 14841 (value != 0)) { 14842 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 14843 "LONG_POS returned begin of partition but" 14844 " block number was 0x%"PRIx64"\n", value); 14845 rval = ENOTTY; 14846 break; 14847 } 14848 if (((dest->pmode == legacy) || 14849 (dest->pmode == logical)) && 14850 (dest->fileno != value)) { 14851 if (flag) 14852 flag++; 14853 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 14854 "LONG_POS fileno 0x%"PRIx64 14855 " not un_pos %x\n", value, 14856 dest->fileno); 14857 } else if (dest->pmode == invalid) { 14858 dest->pmode = logical; 14859 } 14860 dest->fileno = (int32_t)value; 14861 } 14862 14863 if (dest->pmode != invalid && long_pos_info->end_of_part) { 14864 dest->eof = ST_EOT; 14865 } 14866 14867 break; 14868 } 14869 14870 case EXT_POS: /* Extended data format */ 14871 { 14872 uint64_t value; 14873 uint16_t len; 14874 tape_position_ext_t *ext_pos_info = 14875 (tape_position_ext_t *)responce; 14876 14877 /* Make sure that there is enough data there */ 14878 if (data_sz < 16) { 14879 break; 14880 } 14881 14882 /* If reserved fields are non zero don't use the data */ 14883 if (ext_pos_info->reserved0 || ext_pos_info->reserved1) { 14884 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 14885 "EXT_POS reserved fields not zero\n"); 14886 rval = ENOTTY; 14887 break; 14888 } 14889 14890 /* 14891 * In the unlikely event of overflowing 64 bits of position. 14892 */ 14893 if (ext_pos_info->posi_err != 0) { 14894 rval = ERANGE; 14895 break; 14896 } 14897 14898 len = ext_pos_info->parameter_len; 14899 FIX_ENDIAN16(&len); 14900 14901 if (len != 0x1c) { 14902 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 14903 "EXT_POS parameter_len should be 0x1c was 0x%x\n", 14904 len); 14905 rval = ENOTTY; 14906 break; 14907 } 14908 14909 /* Is block position information valid */ 14910 if (ext_pos_info->blk_posi_unkwn == 0) { 14911 14912 value = ext_pos_info->host_block; 14913 FIX_ENDIAN64(&value); 14914 if ((ext_pos_info->begin_of_part == 1) && 14915 (value != 0)) { 14916 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 14917 "EXT_POS returned begining of partition but" 14918 " the host block was 0x%"PRIx64"\n", value); 14919 rval = ENOTTY; 14920 break; 14921 } 14922 14923 if (dest->lgclblkno != value) { 14924 if (flag) 14925 flag++; 14926 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 14927 "EXT_POS current logical 0x%"PRIx64 14928 " read 0x%"PRIx64"\n", 14929 dest->lgclblkno, value); 14930 } 14931 dest->lgclblkno = value; 14932 14933 /* 14934 * If the begining of partition is true and the 14935 * block number is zero we will beleive that it is 14936 * rewound. Promote the pmode to legacy. 14937 */ 14938 if ((ext_pos_info->begin_of_part == 1) && 14939 (ext_pos_info->host_block == 0)) { 14940 dest->blkno = 0; 14941 dest->fileno = 0; 14942 if (dest->pmode != legacy) { 14943 dest->pmode = legacy; 14944 } 14945 /* 14946 * otherwise if the pmode was invalid, 14947 * promote it to logical. 14948 */ 14949 } else if (dest->pmode == invalid) { 14950 dest->pmode = logical; 14951 } 14952 14953 if (dest->partition != ext_pos_info->partition) { 14954 if (flag) 14955 flag++; 14956 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 14957 "EXT_POS current partition %d read %d\n", 14958 dest->partition, 14959 ext_pos_info->partition); 14960 } 14961 dest->partition = ext_pos_info->partition; 14962 14963 } else { 14964 dest->pmode = invalid; 14965 } 14966 break; 14967 } 14968 14969 default: 14970 ST_DEBUG(ST_DEVINFO, st_label, CE_PANIC, 14971 "Got unexpected SCMD_READ_POSITION type %d\n", type); 14972 rval = EIO; 14973 } 14974 14975 if ((flag > 1) && (rval == 0) && (org.pmode != invalid)) { 14976 st_print_position(ST_DEVINFO, st_label, CE_NOTE, 14977 "position read in", &org); 14978 st_print_position(ST_DEVINFO, st_label, CE_NOTE, 14979 "position read out", dest); 14980 } 14981 14982 return (rval); 14983 } 14984 14985 static int 14986 st_logical_block_locate(struct scsi_tape *un, ubufunc_t ubf, tapepos_t *pos, 14987 uint64_t lblk, uchar_t partition) 14988 { 14989 int rval; 14990 char cdb[CDB_GROUP4]; 14991 struct uscsi_cmd *cmd; 14992 struct scsi_extended_sense sense; 14993 bufunc_t bf = (ubf == st_uscsi_cmd) ? st_cmd : st_rcmd; 14994 14995 ST_FUNC(ST_DEVINFO, st_logical_block_locate); 14996 /* 14997 * Not sure what to do when doing recovery and not wanting 14998 * to update un_pos 14999 */ 15000 15001 cmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 15002 15003 if (lblk <= INT32_MAX) { 15004 cmd->uscsi_cdblen = CDB_GROUP1; 15005 cdb[0] = SCMD_LOCATE; 15006 cdb[1] = pos->partition == partition ? 0 : 2; 15007 cdb[2] = 0; 15008 cdb[3] = (char)(lblk >> 24); 15009 cdb[4] = (char)(lblk >> 16); 15010 cdb[5] = (char)(lblk >> 8); 15011 cdb[6] = (char)(lblk); 15012 cdb[7] = 0; 15013 cdb[8] = partition; 15014 cdb[9] = 0; 15015 } else { 15016 /* 15017 * If the drive doesn't give a 64 bit read position data 15018 * it is unlikely it will accept 64 bit locates. 15019 */ 15020 if (un->un_read_pos_type != LONG_POS) { 15021 kmem_free(cmd, sizeof (struct uscsi_cmd)); 15022 return (ERANGE); 15023 } 15024 cmd->uscsi_cdblen = CDB_GROUP4; 15025 cdb[0] = (char)SCMD_LOCATE_G4; 15026 cdb[1] = pos->partition == partition ? 0 : 2; 15027 cdb[2] = 0; 15028 cdb[3] = partition; 15029 cdb[4] = (char)(lblk >> 56); 15030 cdb[5] = (char)(lblk >> 48); 15031 cdb[6] = (char)(lblk >> 40); 15032 cdb[7] = (char)(lblk >> 32); 15033 cdb[8] = (char)(lblk >> 24); 15034 cdb[9] = (char)(lblk >> 16); 15035 cdb[10] = (char)(lblk >> 8); 15036 cdb[11] = (char)(lblk); 15037 cdb[12] = 0; 15038 cdb[13] = 0; 15039 cdb[14] = 0; 15040 cdb[15] = 0; 15041 } 15042 15043 15044 cmd->uscsi_flags = USCSI_WRITE | USCSI_DIAGNOSE | USCSI_RQENABLE; 15045 cmd->uscsi_rqbuf = (caddr_t)&sense; 15046 cmd->uscsi_rqlen = sizeof (sense); 15047 cmd->uscsi_timeout = un->un_dp->space_timeout; 15048 cmd->uscsi_cdb = cdb; 15049 15050 rval = ubf(un, cmd, FKIOCTL); 15051 15052 pos->pmode = logical; 15053 pos->eof = ST_NO_EOF; 15054 15055 if (lblk > INT32_MAX) { 15056 /* 15057 * XXX This is a work around till we handle Descriptor format 15058 * sense data. Since we are sending a command where the standard 15059 * sense data can not correctly represent a correct residual in 15060 * 4 bytes. 15061 */ 15062 if (un->un_status == KEY_ILLEGAL_REQUEST) { 15063 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 15064 "Big LOCATE ILLEGAL_REQUEST: rval = %d\n", rval); 15065 /* Doesn't like big locate command */ 15066 un->un_status = 0; 15067 rval = ERANGE; 15068 } else if ((un->un_pos.pmode == invalid) || (rval != 0)) { 15069 /* Aborted big locate command */ 15070 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 15071 "Big LOCATE resulted in invalid pos: rval = %d\n", 15072 rval); 15073 un->un_status = 0; 15074 rval = EIO; 15075 } else if (st_update_block_pos(un, bf, 1)) { 15076 /* read position failed */ 15077 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 15078 "Big LOCATE and read pos: rval = %d\n", rval); 15079 rval = EIO; 15080 } else if (lblk > un->un_pos.lgclblkno) { 15081 /* read position worked but position was not expected */ 15082 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 15083 "Big LOCATE and recover read less then desired 0x%" 15084 PRIx64"\n", un->un_pos.lgclblkno); 15085 un->un_err_resid = lblk - un->un_pos.lgclblkno; 15086 un->un_status = KEY_BLANK_CHECK; 15087 rval = ESPIPE; 15088 } else if (lblk == un->un_pos.lgclblkno) { 15089 /* read position was what was expected */ 15090 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 15091 "Big LOCATE and recover seems to have worked\n"); 15092 un->un_err_resid = 0; 15093 rval = 0; 15094 } else { 15095 ST_DEBUG(ST_DEVINFO, st_label, CE_PANIC, 15096 "BIGLOCATE end up going backwards"); 15097 un->un_err_resid = lblk; 15098 rval = EIO; 15099 } 15100 15101 } else if (rval == 0) { 15102 /* Worked as requested */ 15103 pos->lgclblkno = lblk; 15104 15105 } else if (((cmd->uscsi_status & ST_STATUS_MASK) == STATUS_CHECK) && 15106 (cmd->uscsi_resid != 0)) { 15107 /* Got part way there but wasn't enough blocks on tape */ 15108 pos->lgclblkno = lblk - cmd->uscsi_resid; 15109 un->un_err_resid = cmd->uscsi_resid; 15110 un->un_status = KEY_BLANK_CHECK; 15111 rval = ESPIPE; 15112 15113 } else if (st_update_block_pos(un, bf, 1) == 0) { 15114 /* Got part way there but drive didn't tell what we missed by */ 15115 un->un_err_resid = lblk - pos->lgclblkno; 15116 un->un_status = KEY_BLANK_CHECK; 15117 rval = ESPIPE; 15118 15119 } else { 15120 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 15121 "Failed LOCATE and recover pos: rval = %d status = %d\n", 15122 rval, cmd->uscsi_status); 15123 un->un_err_resid = lblk; 15124 un->un_status = KEY_ILLEGAL_REQUEST; 15125 pos->pmode = invalid; 15126 rval = EIO; 15127 } 15128 15129 kmem_free(cmd, sizeof (struct uscsi_cmd)); 15130 15131 return (rval); 15132 } 15133 15134 static int 15135 st_mtfsf_ioctl(struct scsi_tape *un, int64_t files) 15136 { 15137 int rval; 15138 15139 ST_FUNC(ST_DEVINFO, st_mtfsf_ioctl); 15140 15141 15142 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 15143 "st_mtfsf_ioctl: count=%"PRIx64", eof=%x\n", files, un->un_pos.eof); 15144 #if 0 15145 if ((IN_EOF(un->un_pos)) && (files == 1)) { 15146 un->un_pos.fileno++; 15147 un->un_pos.blkno = 0; 15148 return (0); 15149 } 15150 #endif 15151 /* pmode == invalid already handled */ 15152 if (un->un_pos.pmode == legacy) { 15153 /* 15154 * forward space over filemark 15155 * 15156 * For ASF we allow a count of 0 on fsf which means 15157 * we just want to go to beginning of current file. 15158 * Equivalent to "nbsf(0)" or "bsf(1) + fsf". 15159 * Allow stepping over double fmk with reel 15160 */ 15161 if ((un->un_pos.eof >= ST_EOT) && 15162 (files > 0) && 15163 ((un->un_dp->options & ST_REEL) == 0)) { 15164 /* we're at EOM */ 15165 un->un_err_resid = files; 15166 un->un_status = KEY_BLANK_CHECK; 15167 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 15168 "st_mtfsf_ioctl: EIO : MTFSF at EOM"); 15169 return (EIO); 15170 } 15171 15172 /* 15173 * physical tape position may not be what we've been 15174 * telling the user; adjust the request accordingly 15175 */ 15176 if (IN_EOF(un->un_pos)) { 15177 un->un_pos.fileno++; 15178 un->un_pos.blkno = 0; 15179 /* 15180 * For positive direction case, we're now covered. 15181 * For zero or negative direction, we're covered 15182 * (almost) 15183 */ 15184 files--; 15185 } 15186 15187 } 15188 15189 if (st_check_density_or_wfm(un->un_dev, 1, B_READ, STEPBACK)) { 15190 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 15191 "st_mtfsf_ioctl: EIO : MTFSF density/wfm failed"); 15192 return (EIO); 15193 } 15194 15195 15196 /* 15197 * Forward space file marks. 15198 * We leave ourselves at block zero 15199 * of the target file number. 15200 */ 15201 if (files < 0) { 15202 rval = st_backward_space_files(un, -files, 0); 15203 } else { 15204 rval = st_forward_space_files(un, files); 15205 } 15206 15207 return (rval); 15208 } 15209 15210 static int 15211 st_forward_space_files(struct scsi_tape *un, int64_t count) 15212 { 15213 int rval; 15214 15215 ST_FUNC(ST_DEVINFO, st_forward_space_files); 15216 15217 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 15218 "fspace: count=%"PRIx64", eof=%x\n", count, un->un_pos.eof); 15219 15220 ASSERT(count >= 0); 15221 ASSERT(un->un_pos.pmode != invalid); 15222 15223 /* 15224 * A space with a count of zero means take me to the start of file. 15225 */ 15226 if (count == 0) { 15227 15228 /* Hay look were already there */ 15229 if (un->un_pos.pmode == legacy && un->un_pos.blkno == 0) { 15230 un->un_err_resid = 0; 15231 COPY_POS(&un->un_err_pos, &un->un_pos); 15232 return (0); 15233 } 15234 15235 /* 15236 * Well we are in the first file. 15237 * A rewind will get to the start. 15238 */ 15239 if (un->un_pos.pmode == legacy && un->un_pos.fileno == 0) { 15240 rval = st_cmd(un, SCMD_REWIND, 0, SYNC_CMD); 15241 15242 /* 15243 * Can we backspace to get there? 15244 * This should work in logical mode. 15245 */ 15246 } else if (un->un_dp->options & ST_BSF) { 15247 rval = st_space_to_begining_of_file(un); 15248 15249 /* 15250 * Can't back space but current file number is known, 15251 * So rewind and space from the begining of the partition. 15252 */ 15253 } else if (un->un_pos.pmode == legacy) { 15254 rval = st_scenic_route_to_begining_of_file(un, 15255 un->un_pos.fileno); 15256 15257 /* 15258 * pmode is logical and ST_BSF is not set. 15259 * The LONG_POS read position contains the fileno. 15260 * If the read position works, rewind and space. 15261 */ 15262 } else if (un->un_read_pos_type == LONG_POS) { 15263 rval = st_cmd(un, SCMD_READ_POSITION, 0, SYNC_CMD); 15264 if (rval) { 15265 /* 15266 * We didn't get the file position from the 15267 * read position command. 15268 * We are going to trust the drive to backspace 15269 * and then position after the filemark. 15270 */ 15271 rval = st_space_to_begining_of_file(un); 15272 } 15273 rval = st_interpret_read_pos(un, &un->un_pos, LONG_POS, 15274 32, (caddr_t)un->un_read_pos_data, 0); 15275 if ((rval) && (un->un_pos.pmode == invalid)) { 15276 rval = st_space_to_begining_of_file(un); 15277 } else { 15278 rval = st_scenic_route_to_begining_of_file(un, 15279 un->un_pos.fileno); 15280 } 15281 } else { 15282 rval = EIO; 15283 } 15284 /* 15285 * If something didn't work we are lost 15286 */ 15287 if (rval != 0) { 15288 un->un_pos.pmode = invalid; 15289 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 15290 "st_mtioctop : EIO : fspace pmode invalid"); 15291 15292 rval = EIO; 15293 } 15294 15295 } else { 15296 rval = st_space_fmks(un, count); 15297 } 15298 15299 if (rval != EIO && count < 0) { 15300 /* 15301 * we came here with a count < 0; we now need 15302 * to skip back to end up before the filemark 15303 */ 15304 rval = st_backward_space_files(un, 1, 1); 15305 } 15306 15307 return (rval); 15308 } 15309 15310 static int 15311 st_scenic_route_to_begining_of_file(struct scsi_tape *un, int32_t fileno) 15312 { 15313 int rval; 15314 15315 ST_FUNC(ST_DEVINFO, st_scenic_route_to_begining_of_file); 15316 15317 if (st_cmd(un, SCMD_REWIND, 0, SYNC_CMD)) { 15318 rval = EIO; 15319 } else if (st_cmd(un, SCMD_SPACE, Fmk(fileno), SYNC_CMD)) { 15320 rval = EIO; 15321 } 15322 15323 return (rval); 15324 } 15325 15326 static int 15327 st_space_to_begining_of_file(struct scsi_tape *un) 15328 { 15329 int rval; 15330 15331 ST_FUNC(ST_DEVINFO, st_space_to_begining_of_file); 15332 15333 /* 15334 * Back space of the file at the begining of the file. 15335 */ 15336 rval = st_cmd(un, SCMD_SPACE, Fmk(-1), SYNC_CMD); 15337 if (rval) { 15338 rval = EIO; 15339 return (rval); 15340 } 15341 15342 /* 15343 * Other interesting answers might be crashed BOT which isn't bad. 15344 */ 15345 if (un->un_status == SUN_KEY_BOT) { 15346 return (rval); 15347 } 15348 15349 un->un_running.pmode = invalid; 15350 15351 /* 15352 * Now we are on the BOP side of the filemark. Forward space to 15353 * the EOM side and we are at the begining of the file. 15354 */ 15355 rval = st_cmd(un, SCMD_SPACE, Fmk(1), SYNC_CMD); 15356 if (rval) { 15357 rval = EIO; 15358 } 15359 15360 return (rval); 15361 } 15362 15363 static int 15364 st_mtfsr_ioctl(struct scsi_tape *un, int64_t count) 15365 { 15366 15367 ST_FUNC(ST_DEVINFO, st_mtfsr_ioctl); 15368 15369 /* 15370 * forward space to inter-record gap 15371 * 15372 */ 15373 15374 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 15375 "st_ioctl_fsr: count=%"PRIx64", eof=%x\n", count, un->un_pos.eof); 15376 15377 if (un->un_pos.pmode == legacy) { 15378 /* 15379 * If were are at end of tape and count is forward. 15380 * Return blank check. 15381 */ 15382 if ((un->un_pos.eof >= ST_EOT) && (count > 0)) { 15383 /* we're at EOM */ 15384 un->un_err_resid = count; 15385 un->un_status = KEY_BLANK_CHECK; 15386 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 15387 "st_mtfsr_ioctl: EIO : MTFSR eof > ST_EOT"); 15388 return (EIO); 15389 } 15390 15391 /* 15392 * If count is zero there is nothing to do. 15393 */ 15394 if (count == 0) { 15395 un->un_err_pos.fileno = un->un_pos.fileno; 15396 un->un_err_pos.blkno = un->un_pos.blkno; 15397 un->un_err_resid = 0; 15398 if (IN_EOF(un->un_pos) && SVR4_BEHAVIOR) { 15399 un->un_status = SUN_KEY_EOF; 15400 } 15401 return (0); 15402 } 15403 15404 /* 15405 * physical tape position may not be what we've been 15406 * telling the user; adjust the position accordingly 15407 */ 15408 if (IN_EOF(un->un_pos)) { 15409 daddr_t blkno = un->un_pos.blkno; 15410 int fileno = un->un_pos.fileno; 15411 15412 optype lastop = un->un_lastop; 15413 if (st_cmd(un, SCMD_SPACE, Fmk(-1), SYNC_CMD) 15414 == -1) { 15415 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 15416 "st_mtfsr_ioctl:EIO:MTFSR count && IN_EOF"); 15417 return (EIO); 15418 } 15419 15420 un->un_pos.blkno = blkno; 15421 un->un_pos.fileno = fileno; 15422 un->un_lastop = lastop; 15423 } 15424 } 15425 15426 if (st_check_density_or_wfm(un->un_dev, 1, B_READ, STEPBACK)) { 15427 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 15428 "st_mtfsr_ioctl: EIO : MTFSR st_check_den"); 15429 return (EIO); 15430 } 15431 15432 return (st_space_records(un, count)); 15433 } 15434 15435 static int 15436 st_space_records(struct scsi_tape *un, int64_t count) 15437 { 15438 int64_t dblk; 15439 int rval = 0; 15440 15441 ST_FUNC(ST_DEVINFO, st_space_records); 15442 15443 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 15444 "st_space_records: count=%"PRIx64", eof=%x\n", 15445 count, un->un_pos.eof); 15446 15447 if (un->un_pos.pmode == logical) { 15448 rval = st_cmd(un, SCMD_SPACE, Blk(count), SYNC_CMD); 15449 if (rval != 0) { 15450 rval = EIO; 15451 } 15452 return (rval); 15453 } 15454 15455 dblk = count + un->un_pos.blkno; 15456 15457 /* Already there */ 15458 if (dblk == un->un_pos.blkno) { 15459 un->un_err_resid = 0; 15460 COPY_POS(&un->un_err_pos, &un->un_pos); 15461 return (0); 15462 } 15463 15464 /* 15465 * If the destination block is forward 15466 * or the drive will backspace records. 15467 */ 15468 if (un->un_pos.blkno < dblk || (un->un_dp->options & ST_BSR)) { 15469 /* 15470 * If we're spacing forward, or the device can 15471 * backspace records, we can just use the SPACE 15472 * command. 15473 */ 15474 dblk -= un->un_pos.blkno; 15475 if (st_cmd(un, SCMD_SPACE, Blk(dblk), SYNC_CMD)) { 15476 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 15477 "st_space_records:EIO:space_records can't spc"); 15478 rval = EIO; 15479 } else if (un->un_pos.eof >= ST_EOF_PENDING) { 15480 /* 15481 * check if we hit BOT/EOT 15482 */ 15483 if (dblk < 0 && un->un_pos.eof == ST_EOM) { 15484 un->un_status = SUN_KEY_BOT; 15485 un->un_pos.eof = ST_NO_EOF; 15486 } else if (dblk < 0 && 15487 un->un_pos.eof == ST_EOF_PENDING) { 15488 int residue = un->un_err_resid; 15489 /* 15490 * we skipped over a filemark 15491 * and need to go forward again 15492 */ 15493 if (st_cmd(un, SCMD_SPACE, Fmk(1), SYNC_CMD)) { 15494 ST_DEBUG2(ST_DEVINFO, st_label, 15495 SCSI_DEBUG, "st_space_records: EIO" 15496 " : can't space #2"); 15497 rval = EIO; 15498 } 15499 un->un_err_resid = residue; 15500 } 15501 if (rval == 0) { 15502 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 15503 "st_space_records: EIO : space_rec rval" 15504 " == 0"); 15505 rval = EIO; 15506 } 15507 } 15508 } else { 15509 /* 15510 * else we rewind, space forward across filemarks to 15511 * the desired file, and then space records to the 15512 * desired block. 15513 */ 15514 15515 int dfile = un->un_pos.fileno; /* save current file */ 15516 15517 if (dblk < 0) { 15518 /* 15519 * Wups - we're backing up over a filemark 15520 */ 15521 if (un->un_pos.blkno != 0 && 15522 (st_cmd(un, SCMD_REWIND, 0, SYNC_CMD) || 15523 st_cmd(un, SCMD_SPACE, Fmk(dfile), SYNC_CMD))) { 15524 un->un_pos.pmode = invalid; 15525 } 15526 un->un_err_resid = -dblk; 15527 if (un->un_pos.fileno == 0 && un->un_pos.blkno == 0) { 15528 un->un_status = SUN_KEY_BOT; 15529 un->un_pos.eof = ST_NO_EOF; 15530 } else if (un->un_pos.fileno > 0) { 15531 un->un_status = SUN_KEY_EOF; 15532 un->un_pos.eof = ST_NO_EOF; 15533 } 15534 COPY_POS(&un->un_err_pos, &un->un_pos); 15535 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 15536 "st_space_records:EIO:space_records : dblk < 0"); 15537 rval = EIO; 15538 } else if (st_cmd(un, SCMD_REWIND, 0, SYNC_CMD) || 15539 st_cmd(un, SCMD_SPACE, Fmk(dfile), SYNC_CMD) || 15540 st_cmd(un, SCMD_SPACE, Blk(dblk), SYNC_CMD)) { 15541 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 15542 "st_space_records: EIO :space_records : rewind " 15543 "and space failed"); 15544 un->un_pos.pmode = invalid; 15545 rval = EIO; 15546 } 15547 } 15548 15549 return (rval); 15550 } 15551 15552 static int 15553 st_mtbsf_ioctl(struct scsi_tape *un, int64_t files) 15554 { 15555 ST_FUNC(ST_DEVINFO, st_mtbsf_ioctl); 15556 15557 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 15558 "st_mtbsf_ioctl: count=%"PRIx64", eof=%x\n", files, un->un_pos.eof); 15559 /* 15560 * backward space of file filemark (1/2" and 8mm) 15561 * tape position will end on the beginning of tape side 15562 * of the desired file mark 15563 */ 15564 if ((un->un_dp->options & ST_BSF) == 0) { 15565 return (ENOTTY); 15566 } 15567 15568 if (un->un_pos.pmode == legacy) { 15569 15570 /* 15571 * If a negative count (which implies a forward space op) 15572 * is specified, and we're at logical or physical eot, 15573 * bounce the request. 15574 */ 15575 15576 if (un->un_pos.eof >= ST_EOT && files < 0) { 15577 un->un_err_resid = files; 15578 un->un_status = SUN_KEY_EOT; 15579 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 15580 "st_ioctl_mt_bsf : EIO : MTBSF : eof > ST_EOF"); 15581 return (EIO); 15582 } 15583 /* 15584 * physical tape position may not be what we've been 15585 * telling the user; adjust the request accordingly 15586 */ 15587 if (IN_EOF(un->un_pos)) { 15588 un->un_pos.fileno++; 15589 un->un_pos.blkno = 0; 15590 files++; 15591 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 15592 "st_mtbsf_ioctl in eof: count=%"PRIx64", op=%x\n", 15593 files, MTBSF); 15594 15595 } 15596 } 15597 15598 if (st_check_density_or_wfm(un->un_dev, 1, 0, STEPBACK)) { 15599 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 15600 "st_ioctl : EIO : MTBSF : check den wfm"); 15601 return (EIO); 15602 } 15603 15604 if (files <= 0) { 15605 /* 15606 * for a negative count, we need to step forward 15607 * first and then step back again 15608 */ 15609 files = -files + 1; 15610 return (st_forward_space_files(un, files)); 15611 } 15612 return (st_backward_space_files(un, files, 1)); 15613 } 15614 15615 static int 15616 st_backward_space_files(struct scsi_tape *un, int64_t count, int infront) 15617 { 15618 int64_t end_fileno; 15619 int64_t skip_cnt; 15620 int rval = 0; 15621 15622 ST_FUNC(ST_DEVINFO, st_backward_space_files); 15623 15624 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 15625 "st_backward_space_files: count=%"PRIx64" eof=%x\n", 15626 count, un->un_pos.eof); 15627 /* 15628 * Backspace files (MTNBSF): infront == 0 15629 * 15630 * For tapes that can backspace, backspace 15631 * count+1 filemarks and then run forward over 15632 * a filemark 15633 * 15634 * For tapes that can't backspace, 15635 * calculate desired filenumber 15636 * (un->un_pos.fileno - count), rewind, 15637 * and then space forward this amount 15638 * 15639 * Backspace filemarks (MTBSF) infront == 1 15640 * 15641 * For tapes that can backspace, backspace count 15642 * filemarks 15643 * 15644 * For tapes that can't backspace, calculate 15645 * desired filenumber (un->un_pos.fileno - count), 15646 * add 1, rewind, space forward this amount, 15647 * and mark state as ST_EOF_PENDING appropriately. 15648 */ 15649 15650 if (un->un_pos.pmode == logical) { 15651 15652 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 15653 "st_backward_space_files: mt_op=%x count=%"PRIx64 15654 "lgclblkno=%"PRIx64"\n", infront?MTBSF:MTNBSF, count, 15655 un->un_pos.lgclblkno); 15656 15657 15658 /* In case a drive that won't back space gets in logical mode */ 15659 if ((un->un_dp->options & ST_BSF) == 0) { 15660 rval = EIO; 15661 return (rval); 15662 } 15663 if ((infront == 1) && 15664 (st_cmd(un, SCMD_SPACE, Fmk(-count), SYNC_CMD))) { 15665 rval = EIO; 15666 return (rval); 15667 } else if ((infront == 0) && 15668 (st_cmd(un, SCMD_SPACE, Fmk((-count)-1), SYNC_CMD)) && 15669 (st_cmd(un, SCMD_SPACE, Fmk(1), SYNC_CMD))) { 15670 rval = EIO; 15671 return (rval); 15672 } 15673 return (rval); 15674 } 15675 15676 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 15677 "st_backward_space_files: mt_op=%x count=%"PRIx64 15678 "fileno=%x blkno=%x\n", 15679 infront?MTBSF:MTNBSF, count, un->un_pos.fileno, un->un_pos.blkno); 15680 15681 15682 15683 /* 15684 * Handle the simple case of BOT 15685 * playing a role in these cmds. 15686 * We do this by calculating the 15687 * ending file number. If the ending 15688 * file is < BOT, rewind and set an 15689 * error and mark resid appropriately. 15690 * If we're backspacing a file (not a 15691 * filemark) and the target file is 15692 * the first file on the tape, just 15693 * rewind. 15694 */ 15695 15696 /* figure expected destination of this SPACE command */ 15697 end_fileno = un->un_pos.fileno - count; 15698 15699 /* 15700 * Would the end effect of this SPACE be the same as rewinding? 15701 * If so just rewind instead. 15702 */ 15703 if ((infront != 0) && (end_fileno < 0) || 15704 (infront == 0) && (end_fileno <= 0)) { 15705 if (st_cmd(un, SCMD_REWIND, 0, SYNC_CMD)) { 15706 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 15707 "st_backward_space_files: EIO : " 15708 "rewind in lou of BSF failed\n"); 15709 rval = EIO; 15710 } 15711 if (end_fileno < 0) { 15712 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 15713 "st_backward_space_files: EIO : " 15714 "back space file greater then fileno\n"); 15715 rval = EIO; 15716 un->un_err_resid = -end_fileno; 15717 un->un_status = SUN_KEY_BOT; 15718 } 15719 return (rval); 15720 } 15721 15722 if (un->un_dp->options & ST_BSF) { 15723 skip_cnt = 1 - infront; 15724 /* 15725 * If we are going to end up at the beginning 15726 * of the file, we have to space one extra file 15727 * first, and then space forward later. 15728 */ 15729 end_fileno = -(count + skip_cnt); 15730 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 15731 "skip_cnt=%"PRIx64", tmp=%"PRIx64"\n", 15732 skip_cnt, end_fileno); 15733 if (st_cmd(un, SCMD_SPACE, Fmk(end_fileno), SYNC_CMD)) { 15734 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 15735 "st_backward_space_files:EIO:back space fm failed"); 15736 rval = EIO; 15737 } 15738 } else { 15739 if (st_cmd(un, SCMD_REWIND, 0, SYNC_CMD)) { 15740 rval = EIO; 15741 } else { 15742 skip_cnt = end_fileno + infront; 15743 } 15744 } 15745 15746 /* 15747 * If we have to space forward, do so... 15748 */ 15749 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 15750 "space forward skip_cnt=%"PRIx64", rval=%x\n", skip_cnt, rval); 15751 15752 if (rval == 0 && skip_cnt) { 15753 if (st_cmd(un, SCMD_SPACE, Fmk(skip_cnt), SYNC_CMD)) { 15754 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 15755 "st_backward_space_files:EIO:space fm skip count"); 15756 rval = EIO; 15757 } else if (infront) { 15758 /* 15759 * If we had to space forward, and we're 15760 * not a tape that can backspace, mark state 15761 * as if we'd just seen a filemark during a 15762 * a read. 15763 */ 15764 if ((un->un_dp->options & ST_BSF) == 0) { 15765 un->un_pos.eof = ST_EOF_PENDING; 15766 un->un_pos.fileno -= 1; 15767 un->un_pos.blkno = LASTBLK; 15768 un->un_running.pmode = invalid; 15769 } 15770 } 15771 } 15772 15773 if (rval != 0) { 15774 un->un_pos.pmode = invalid; 15775 } 15776 15777 return (rval); 15778 } 15779 15780 static int 15781 st_mtnbsf_ioctl(struct scsi_tape *un, int64_t count) 15782 { 15783 int rval; 15784 15785 ST_FUNC(ST_DEVINFO, st_mtnbsf_ioctl); 15786 15787 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 15788 "nbsf: count=%"PRIx64", eof=%x\n", count, un->un_pos.eof); 15789 15790 if (un->un_pos.pmode == legacy) { 15791 /* 15792 * backward space file to beginning of file 15793 * 15794 * If a negative count (which implies a forward space op) 15795 * is specified, and we're at logical or physical eot, 15796 * bounce the request. 15797 */ 15798 15799 if (un->un_pos.eof >= ST_EOT && count < 0) { 15800 un->un_err_resid = count; 15801 un->un_status = SUN_KEY_EOT; 15802 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 15803 "st_ioctl : EIO : > EOT and count < 0"); 15804 return (EIO); 15805 } 15806 /* 15807 * physical tape position may not be what we've been 15808 * telling the user; adjust the request accordingly 15809 */ 15810 if (IN_EOF(un->un_pos)) { 15811 un->un_pos.fileno++; 15812 un->un_pos.blkno = 0; 15813 count++; 15814 } 15815 } 15816 15817 if (st_check_density_or_wfm(un->un_dev, 1, 0, STEPBACK)) { 15818 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 15819 "st_ioctl : EIO : MTNBSF check den and wfm"); 15820 return (EIO); 15821 } 15822 15823 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 15824 "mtnbsf: count=%"PRIx64", eof=%x\n", count, un->un_pos.eof); 15825 15826 if (count <= 0) { 15827 rval = st_forward_space_files(un, -count); 15828 } else { 15829 rval = st_backward_space_files(un, count, 0); 15830 } 15831 return (rval); 15832 } 15833 15834 static int 15835 st_mtbsr_ioctl(struct scsi_tape *un, int64_t num) 15836 { 15837 ST_FUNC(ST_DEVINFO, st_mtbsr_ioctl); 15838 15839 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 15840 "bsr: count=%"PRIx64", eof=%x\n", num, un->un_pos.eof); 15841 15842 if (un->un_pos.pmode == legacy) { 15843 /* 15844 * backward space into inter-record gap 15845 * 15846 * If a negative count (which implies a forward space op) 15847 * is specified, and we're at logical or physical eot, 15848 * bounce the request. 15849 */ 15850 if (un->un_pos.eof >= ST_EOT && num < 0) { 15851 un->un_err_resid = num; 15852 un->un_status = SUN_KEY_EOT; 15853 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 15854 "st_ioctl : EIO : MTBSR > EOT"); 15855 return (EIO); 15856 } 15857 15858 if (num == 0) { 15859 COPY_POS(&un->un_err_pos, &un->un_pos); 15860 un->un_err_resid = 0; 15861 if (IN_EOF(un->un_pos) && SVR4_BEHAVIOR) { 15862 un->un_status = SUN_KEY_EOF; 15863 } 15864 return (0); 15865 } 15866 15867 /* 15868 * physical tape position may not be what we've been 15869 * telling the user; adjust the position accordingly. 15870 * bsr can not skip filemarks and continue to skip records 15871 * therefore if we are logically before the filemark but 15872 * physically at the EOT side of the filemark, we need to step 15873 * back; this allows fsr N where N > number of blocks in file 15874 * followed by bsr 1 to position at the beginning of last block 15875 */ 15876 if (IN_EOF(un->un_pos)) { 15877 tapepos_t save; 15878 optype lastop = un->un_lastop; 15879 15880 COPY_POS(&save, &un->un_pos); 15881 if (st_cmd(un, SCMD_SPACE, Fmk(-1), SYNC_CMD) == -1) { 15882 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 15883 "st_mtbsr_ioctl: EIO : MTBSR can't space"); 15884 return (EIO); 15885 } 15886 15887 COPY_POS(&un->un_pos, &save); 15888 un->un_lastop = lastop; 15889 } 15890 } 15891 15892 un->un_pos.eof = ST_NO_EOF; 15893 15894 if (st_check_density_or_wfm(un->un_dev, 1, 0, STEPBACK)) { 15895 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 15896 "st_ioctl : EIO : MTBSR : can't set density or wfm"); 15897 return (EIO); 15898 } 15899 15900 num = -num; 15901 return (st_space_records(un, num)); 15902 } 15903 15904 static int 15905 st_mtfsfm_ioctl(struct scsi_tape *un, int64_t cnt) 15906 { 15907 int rval; 15908 15909 ST_FUNC(ST_DEVINFO, st_mtfsfm_ioctl); 15910 15911 rval = st_cmd(un, SCMD_SPACE, SPACE(SP_SQFLM, cnt), SYNC_CMD); 15912 if (rval == 0) { 15913 un->un_pos.pmode = logical; 15914 } else if ((un->un_status == KEY_ILLEGAL_REQUEST) && 15915 (un->un_sd->sd_sense->es_add_code == 0x24)) { 15916 /* 15917 * Drive says invalid field in cdb. 15918 * Doesn't like space multiple. Position isn't lost. 15919 */ 15920 un->un_err_resid = cnt; 15921 un->un_status = 0; 15922 rval = ENOTTY; 15923 } else { 15924 un->un_err_resid = cnt; 15925 un->un_pos.pmode = invalid; 15926 } 15927 return (rval); 15928 } 15929 15930 static int 15931 st_mtbsfm_ioctl(struct scsi_tape *un, int64_t cnt) 15932 { 15933 int rval; 15934 15935 ST_FUNC(ST_DEVINFO, st_mtbsfm_ioctl); 15936 15937 rval = st_cmd(un, SCMD_SPACE, SPACE(SP_SQFLM, -cnt), SYNC_CMD); 15938 if (rval == 0) { 15939 un->un_pos.pmode = logical; 15940 } else if ((un->un_status == KEY_ILLEGAL_REQUEST) && 15941 (un->un_sd->sd_sense->es_add_code == 0x24)) { 15942 /* 15943 * Drive says invalid field in cdb. 15944 * Doesn't like space multiple. Position isn't lost. 15945 */ 15946 un->un_err_resid = cnt; 15947 un->un_status = 0; 15948 rval = ENOTTY; 15949 } else { 15950 un->un_err_resid = cnt; 15951 un->un_pos.pmode = invalid; 15952 } 15953 return (rval); 15954 } 15955 15956 #ifdef __x86 15957 15958 /* 15959 * release contig_mem and wake up waiting thread, if any 15960 */ 15961 static void 15962 st_release_contig_mem(struct scsi_tape *un, struct contig_mem *cp) 15963 { 15964 mutex_enter(ST_MUTEX); 15965 15966 ST_FUNC(ST_DEVINFO, st_release_contig_mem); 15967 15968 cp->cm_next = un->un_contig_mem; 15969 un->un_contig_mem = cp; 15970 un->un_contig_mem_available_num++; 15971 cv_broadcast(&un->un_contig_mem_cv); 15972 15973 mutex_exit(ST_MUTEX); 15974 } 15975 15976 /* 15977 * St_get_contig_mem will return a contig_mem if there is one available 15978 * in current system. Otherwise, it will try to alloc one, if the total 15979 * number of contig_mem is within st_max_contig_mem_num. 15980 * It will sleep, if allowed by caller or return NULL, if no contig_mem 15981 * is available for now. 15982 */ 15983 static struct contig_mem * 15984 st_get_contig_mem(struct scsi_tape *un, size_t len, int alloc_flags) 15985 { 15986 size_t rlen; 15987 struct contig_mem *cp = NULL; 15988 ddi_acc_handle_t acc_hdl; 15989 caddr_t addr; 15990 int big_enough = 0; 15991 int (*dma_alloc_cb)() = (alloc_flags == KM_SLEEP) ? 15992 DDI_DMA_SLEEP : DDI_DMA_DONTWAIT; 15993 15994 /* Try to get one available contig_mem */ 15995 mutex_enter(ST_MUTEX); 15996 15997 ST_FUNC(ST_DEVINFO, st_get_contig_mem); 15998 15999 if (un->un_contig_mem_available_num > 0) { 16000 ST_GET_CONTIG_MEM_HEAD(un, cp, len, big_enough); 16001 } else if (un->un_contig_mem_total_num < st_max_contig_mem_num) { 16002 /* 16003 * we failed to get one. we're going to 16004 * alloc one more contig_mem for this I/O 16005 */ 16006 mutex_exit(ST_MUTEX); 16007 cp = (struct contig_mem *)kmem_zalloc( 16008 sizeof (struct contig_mem) + biosize(), 16009 alloc_flags); 16010 if (cp == NULL) { 16011 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 16012 "alloc contig_mem failure\n"); 16013 return (NULL); /* cannot get one */ 16014 } 16015 cp->cm_bp = (struct buf *) 16016 (((caddr_t)cp) + sizeof (struct contig_mem)); 16017 bioinit(cp->cm_bp); 16018 mutex_enter(ST_MUTEX); 16019 un->un_contig_mem_total_num++; /* one more available */ 16020 } else { 16021 /* 16022 * we failed to get one and we're NOT allowed to 16023 * alloc more contig_mem 16024 */ 16025 if (alloc_flags == KM_SLEEP) { 16026 while (un->un_contig_mem_available_num <= 0) { 16027 cv_wait(&un->un_contig_mem_cv, ST_MUTEX); 16028 } 16029 ST_GET_CONTIG_MEM_HEAD(un, cp, len, big_enough); 16030 } else { 16031 mutex_exit(ST_MUTEX); 16032 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 16033 "alloc contig_mem failure\n"); 16034 return (NULL); /* cannot get one */ 16035 } 16036 } 16037 mutex_exit(ST_MUTEX); 16038 16039 /* We need to check if this block of mem is big enough for this I/O */ 16040 if (cp->cm_len < len) { 16041 /* not big enough, need to alloc a new one */ 16042 if (ddi_dma_mem_alloc(un->un_contig_mem_hdl, len, &st_acc_attr, 16043 DDI_DMA_STREAMING, dma_alloc_cb, NULL, 16044 &addr, &rlen, &acc_hdl) != DDI_SUCCESS) { 16045 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 16046 "alloc contig_mem failure: not enough mem\n"); 16047 st_release_contig_mem(un, cp); 16048 cp = NULL; 16049 } else { 16050 if (cp->cm_addr) { 16051 /* release previous one before attach new one */ 16052 ddi_dma_mem_free(&cp->cm_acc_hdl); 16053 } 16054 mutex_enter(ST_MUTEX); 16055 un->un_max_contig_mem_len = 16056 un->un_max_contig_mem_len >= len ? 16057 un->un_max_contig_mem_len : len; 16058 mutex_exit(ST_MUTEX); 16059 16060 /* attach new mem to this cp */ 16061 cp->cm_addr = addr; 16062 cp->cm_acc_hdl = acc_hdl; 16063 cp->cm_len = len; 16064 16065 goto alloc_ok; /* get one usable cp */ 16066 } 16067 } else { 16068 goto alloc_ok; /* get one usable cp */ 16069 } 16070 16071 /* cannot find/alloc a usable cp, when we get here */ 16072 16073 mutex_enter(ST_MUTEX); 16074 if ((un->un_max_contig_mem_len < len) || 16075 (alloc_flags != KM_SLEEP)) { 16076 mutex_exit(ST_MUTEX); 16077 return (NULL); 16078 } 16079 16080 /* 16081 * we're allowed to sleep, and there is one big enough 16082 * contig mem in the system, which is currently in use, 16083 * wait for it... 16084 */ 16085 big_enough = 1; 16086 do { 16087 cv_wait(&un->un_contig_mem_cv, ST_MUTEX); 16088 ST_GET_CONTIG_MEM_HEAD(un, cp, len, big_enough); 16089 } while (cp == NULL); 16090 mutex_exit(ST_MUTEX); 16091 16092 /* we get the big enough contig mem, finally */ 16093 16094 alloc_ok: 16095 /* init bp attached to this cp */ 16096 bioreset(cp->cm_bp); 16097 cp->cm_bp->b_un.b_addr = cp->cm_addr; 16098 cp->cm_bp->b_private = (void *)cp; 16099 16100 return (cp); 16101 } 16102 16103 /* 16104 * this is the biodone func for the bp used in big block I/O 16105 */ 16106 static int 16107 st_bigblk_xfer_done(struct buf *bp) 16108 { 16109 struct contig_mem *cp; 16110 struct buf *orig_bp; 16111 int ioerr; 16112 struct scsi_tape *un; 16113 16114 /* sanity check */ 16115 if (bp == NULL) { 16116 return (DDI_FAILURE); 16117 } 16118 16119 un = ddi_get_soft_state(st_state, MTUNIT(bp->b_edev)); 16120 if (un == NULL) { 16121 return (DDI_FAILURE); 16122 } 16123 16124 ST_FUNC(ST_DEVINFO, st_bigblk_xfer_done); 16125 16126 cp = (struct contig_mem *)bp->b_private; 16127 orig_bp = cp->cm_bp; /* get back the bp we have replaced */ 16128 cp->cm_bp = bp; 16129 16130 /* special handling for special I/O */ 16131 if (cp->cm_use_sbuf) { 16132 #ifndef __lock_lint 16133 ASSERT(un->un_sbuf_busy); 16134 #endif 16135 un->un_sbufp = orig_bp; 16136 cp->cm_use_sbuf = 0; 16137 } 16138 16139 orig_bp->b_resid = bp->b_resid; 16140 ioerr = geterror(bp); 16141 if (ioerr != 0) { 16142 bioerror(orig_bp, ioerr); 16143 } else if (orig_bp->b_flags & B_READ) { 16144 /* copy data back to original bp */ 16145 (void) bp_copyout(bp->b_un.b_addr, orig_bp, 0, 16146 bp->b_bcount - bp->b_resid); 16147 } 16148 16149 st_release_contig_mem(un, cp); 16150 16151 biodone(orig_bp); 16152 16153 return (DDI_SUCCESS); 16154 } 16155 16156 /* 16157 * We use this func to replace original bp that may not be able to do I/O 16158 * in big block size with one that can 16159 */ 16160 static struct buf * 16161 st_get_bigblk_bp(struct buf *bp) 16162 { 16163 struct contig_mem *cp; 16164 struct scsi_tape *un; 16165 struct buf *cont_bp; 16166 16167 un = ddi_get_soft_state(st_state, MTUNIT(bp->b_edev)); 16168 if (un == NULL) { 16169 return (bp); 16170 } 16171 16172 ST_FUNC(ST_DEVINFO, st_get_bigblk_bp); 16173 16174 /* try to get one contig_mem */ 16175 cp = st_get_contig_mem(un, bp->b_bcount, KM_SLEEP); 16176 if (!cp) { 16177 scsi_log(ST_DEVINFO, st_label, CE_WARN, 16178 "Cannot alloc contig buf for I/O for %lu blk size", 16179 bp->b_bcount); 16180 return (bp); 16181 } 16182 cont_bp = cp->cm_bp; 16183 cp->cm_bp = bp; 16184 16185 /* make sure that we "are" using un_sbufp for special I/O */ 16186 if (bp == un->un_sbufp) { 16187 #ifndef __lock_lint 16188 ASSERT(un->un_sbuf_busy); 16189 #endif 16190 un->un_sbufp = cont_bp; 16191 cp->cm_use_sbuf = 1; 16192 } 16193 16194 /* clone bp */ 16195 cont_bp->b_bcount = bp->b_bcount; 16196 cont_bp->b_resid = bp->b_resid; 16197 cont_bp->b_iodone = st_bigblk_xfer_done; 16198 cont_bp->b_file = bp->b_file; 16199 cont_bp->b_offset = bp->b_offset; 16200 cont_bp->b_dip = bp->b_dip; 16201 cont_bp->b_error = 0; 16202 cont_bp->b_proc = NULL; 16203 cont_bp->b_flags = bp->b_flags & ~(B_PAGEIO | B_PHYS | B_SHADOW); 16204 cont_bp->b_shadow = NULL; 16205 cont_bp->b_pages = NULL; 16206 cont_bp->b_edev = bp->b_edev; 16207 cont_bp->b_dev = bp->b_dev; 16208 cont_bp->b_lblkno = bp->b_lblkno; 16209 cont_bp->b_forw = bp->b_forw; 16210 cont_bp->b_back = bp->b_back; 16211 cont_bp->av_forw = bp->av_forw; 16212 cont_bp->av_back = bp->av_back; 16213 cont_bp->b_bufsize = bp->b_bufsize; 16214 16215 /* get data in original bp */ 16216 if (bp->b_flags & B_WRITE) { 16217 (void) bp_copyin(bp, cont_bp->b_un.b_addr, 0, bp->b_bcount); 16218 } 16219 16220 return (cont_bp); 16221 } 16222 #else 16223 #ifdef __lock_lint 16224 static int 16225 st_bigblk_xfer_done(struct buf *bp) 16226 { 16227 return (0); 16228 } 16229 #endif 16230 #endif 16231 16232 static const char *eof_status[] = 16233 { 16234 "NO_EOF", 16235 "EOF_PENDING", 16236 "EOF", 16237 "EOT_PENDING", 16238 "EOT", 16239 "EOM", 16240 "AFTER_EOM" 16241 }; 16242 static const char *mode[] = { 16243 "invalid", 16244 "legacy", 16245 "logical" 16246 }; 16247 16248 static void 16249 st_print_position(dev_info_t *dev, char *label, uint_t level, 16250 const char *comment, tapepos_t *pos) 16251 { 16252 ST_FUNC(dev, st_print_position); 16253 16254 scsi_log(dev, label, level, 16255 "%s Position data:\n", comment); 16256 scsi_log(dev, label, CE_CONT, 16257 "Positioning mode = %s", mode[pos->pmode]); 16258 scsi_log(dev, label, CE_CONT, 16259 "End Of File/Tape = %s", eof_status[pos->eof]); 16260 scsi_log(dev, label, CE_CONT, 16261 "File Number = 0x%x", pos->fileno); 16262 scsi_log(dev, label, CE_CONT, 16263 "Block Number = 0x%x", pos->blkno); 16264 scsi_log(dev, label, CE_CONT, 16265 "Logical Block = 0x%"PRIx64, pos->lgclblkno); 16266 scsi_log(dev, label, CE_CONT, 16267 "Partition Number = 0x%x", pos->partition); 16268 } 16269 static int 16270 st_check_if_media_changed(struct scsi_tape *un, caddr_t data, int size) 16271 { 16272 16273 int result = 0; 16274 int i; 16275 ST_FUNC(ST_DEVINFO, st_check_if_media_changed); 16276 16277 /* 16278 * find non alpha numeric working from the end. 16279 */ 16280 for (i = size - 1; i >= 0; i--) { 16281 if (ISALNUM(data[i]) == 0 || data[i] == ' ') { 16282 data[i] = 0; 16283 size = i; 16284 } 16285 } 16286 16287 if (size == 1) { 16288 /* 16289 * Drive seems to think its returning useful data 16290 * but it looks like all junk 16291 */ 16292 return (result); 16293 } 16294 16295 size++; 16296 16297 /* 16298 * Actually got a valid serial number. 16299 * If never stored one before alloc space for it. 16300 */ 16301 if (un->un_media_id_len == 0) { 16302 un->un_media_id = kmem_zalloc(size, KM_SLEEP); 16303 un->un_media_id_len = size; 16304 (void) strncpy(un->un_media_id, data, min(size, strlen(data))); 16305 un->un_media_id[min(size, strlen(data))] = 0; 16306 ST_DEBUG1(ST_DEVINFO, st_label, SCSI_DEBUG, 16307 "Found Media Id %s length = %d\n", un->un_media_id, size); 16308 } else if (size > un->un_media_id_len) { 16309 if (strncmp(un->un_media_id, data, size) != 0) { 16310 result = ESPIPE; 16311 } 16312 ST_DEBUG1(ST_DEVINFO, st_label, SCSI_DEBUG, 16313 "Longer Media Id old ID:%s new ID:%s\n", 16314 un->un_media_id, data); 16315 kmem_free(un->un_media_id, un->un_media_id_len); 16316 un->un_media_id = kmem_zalloc(size, KM_SLEEP); 16317 un->un_media_id_len = size; 16318 (void) strncpy(un->un_media_id, data, size); 16319 un->un_media_id[size] = 0; 16320 } else if (strncmp(data, un->un_media_id, 16321 min(size, un->un_media_id_len)) != 0) { 16322 ST_DEBUG1(ST_DEVINFO, st_label, SCSI_DEBUG, 16323 "Old Media Id %s length = %d New %s length = %d\n", 16324 un->un_media_id, un->un_media_id_len, data, size); 16325 bzero(un->un_media_id, un->un_media_id_len); 16326 (void) strncpy(un->un_media_id, data, min(size, strlen(data))); 16327 un->un_media_id[min(size, strlen(data))] = 0; 16328 result = ESPIPE; 16329 } else { 16330 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 16331 "Media Id still %s\n", un->un_media_id); 16332 } 16333 16334 ASSERT(strlen(un->un_media_id) <= size); 16335 16336 return (result); 16337 } 16338 #define ID_SIZE 32 16339 typedef struct 16340 { 16341 uchar_t avilable_data0; 16342 uchar_t avilable_data1; 16343 uchar_t avilable_data2; 16344 uchar_t avilable_data3; 16345 uchar_t attribute_msb; 16346 uchar_t attribute_lsb; 16347 #ifdef _BIT_FIELDS_LTOH 16348 uchar_t format : 2, 16349 : 5, 16350 read_only : 1; 16351 #else 16352 uchar_t read_only : 1, 16353 : 5, 16354 format : 2; 16355 #endif 16356 uchar_t attribute_len_msb; 16357 uchar_t attribute_len_lsb; 16358 }attribute_header; 16359 16360 typedef struct { 16361 attribute_header header; 16362 char data[1]; 16363 }mam_attribute; 16364 16365 static int 16366 st_handle_hex_media_id(struct scsi_tape *un, void *pnt, int size) 16367 { 16368 int result; 16369 int newsize = (size << 1) + 3; /* extra for leading 0x and null term */ 16370 int i; 16371 uchar_t byte; 16372 char *format; 16373 uchar_t *data = (uchar_t *)pnt; 16374 char *buf = kmem_alloc(newsize, KM_SLEEP); 16375 16376 ST_FUNC(ST_DEVINFO, st_handle_hex_media_id); 16377 16378 (void) sprintf(buf, "0x"); 16379 for (i = 0; i < size; i++) { 16380 byte = data[i]; 16381 if (byte < 0x10) 16382 format = "0%x"; 16383 else 16384 format = "%x"; 16385 (void) sprintf(&buf[(int)strlen(buf)], format, byte); 16386 } 16387 result = st_check_if_media_changed(un, buf, newsize); 16388 16389 kmem_free(buf, newsize); 16390 16391 return (result); 16392 } 16393 16394 16395 static int 16396 st_get_media_id_via_read_attribute(struct scsi_tape *un, ubufunc_t bufunc) 16397 { 16398 int result; 16399 mam_attribute *buffer; 16400 int size; 16401 int newsize; 16402 16403 ST_FUNC(ST_DEVINFO, st_get_media_id_via_read_attribute); 16404 size = sizeof (attribute_header) + max(un->un_media_id_len, ID_SIZE); 16405 again: 16406 buffer = kmem_zalloc(size, KM_SLEEP); 16407 result = st_read_attributes(un, 0x0401, buffer, size, bufunc); 16408 if (result == 0) { 16409 16410 newsize = (buffer->header.attribute_len_msb << 8) | 16411 buffer->header.attribute_len_lsb; 16412 16413 if (newsize + sizeof (attribute_header) > size) { 16414 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 16415 "resizing read attribute data from %d to %d format" 16416 " %d\n", size, (int)sizeof (attribute_header) + 16417 newsize, buffer->header.format); 16418 kmem_free(buffer, size); 16419 size = newsize + sizeof (attribute_header); 16420 goto again; 16421 } 16422 16423 un->un_media_id_method = st_get_media_id_via_read_attribute; 16424 if (buffer->header.format == 0) { 16425 result = 16426 st_handle_hex_media_id(un, buffer->data, newsize); 16427 } else { 16428 result = st_check_if_media_changed(un, buffer->data, 16429 newsize); 16430 } 16431 } else if (result == EINVAL && un->un_max_cdb_sz < CDB_GROUP4) { 16432 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 16433 "Read Attribute Command for Media Identification is not " 16434 "supported on the HBA that this drive is attached to."); 16435 result = ENOTTY; 16436 } 16437 16438 kmem_free(buffer, size); 16439 un->un_status = 0; 16440 16441 return (result); 16442 } 16443 16444 16445 static int 16446 st_get_media_id_via_media_serial_cmd(struct scsi_tape *un, ubufunc_t bufunc) 16447 { 16448 char cdb[CDB_GROUP5]; 16449 struct uscsi_cmd *ucmd; 16450 struct scsi_extended_sense sense; 16451 int rval; 16452 int size = max(un->un_media_id_len, ID_SIZE); 16453 caddr_t buf; 16454 16455 ST_FUNC(ST_DEVINFO, st_get_media_id_via_media_serial_cmd); 16456 16457 if (un->un_sd->sd_inq->inq_ansi < 3) { 16458 return (ENOTTY); 16459 } 16460 16461 ucmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 16462 upsize: 16463 buf = kmem_alloc(size, KM_SLEEP); 16464 16465 cdb[0] = (char)SCMD_SVC_ACTION_IN_G5; 16466 cdb[1] = SSVC_ACTION_READ_MEDIA_SERIAL; 16467 cdb[2] = 0; 16468 cdb[3] = 0; 16469 cdb[4] = 0; 16470 cdb[5] = 0; 16471 cdb[6] = (char)(size >> 24); 16472 cdb[7] = (char)(size >> 16); 16473 cdb[8] = (char)(size >> 8); 16474 cdb[9] = (char)(size); 16475 cdb[10] = 0; 16476 cdb[11] = 0; 16477 16478 ucmd->uscsi_flags = USCSI_READ | USCSI_RQENABLE; 16479 ucmd->uscsi_timeout = un->un_dp->non_motion_timeout; 16480 ucmd->uscsi_cdb = &cdb[0]; 16481 ucmd->uscsi_cdblen = sizeof (cdb); 16482 ucmd->uscsi_bufaddr = buf; 16483 ucmd->uscsi_buflen = size; 16484 ucmd->uscsi_rqbuf = (caddr_t)&sense; 16485 ucmd->uscsi_rqlen = sizeof (sense); 16486 16487 rval = bufunc(un, ucmd, FKIOCTL); 16488 16489 if (rval || ucmd->uscsi_status != 0) { 16490 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 16491 "media serial command returned %d scsi_status %d" 16492 " rqstatus %d", rval, ucmd->uscsi_status, 16493 ucmd->uscsi_rqstatus); 16494 /* 16495 * If this returns invalid operation code don't try again. 16496 */ 16497 if (sense.es_key == KEY_ILLEGAL_REQUEST && 16498 sense.es_add_code == 0x20) { 16499 rval = ENOTTY; 16500 } else if (rval == 0) { 16501 rval = EIO; 16502 } 16503 un->un_status = 0; 16504 } else { 16505 int act_size; 16506 16507 /* 16508 * get reported size. 16509 */ 16510 act_size = (int)buf[3] | (int)(buf[2] << 8) | 16511 (int)(buf[1] << 16) | (int)(buf[0] << 24); 16512 16513 /* documentation says mod 4. */ 16514 while (act_size & 3) { 16515 act_size++; 16516 } 16517 16518 /* 16519 * If reported size is larger that we our buffer. 16520 * Free the old one and allocate one that is larger 16521 * enough and re-issuse the command. 16522 */ 16523 if (act_size + 4 > size) { 16524 kmem_free(buf, size); 16525 size = act_size + 4; 16526 goto upsize; 16527 } 16528 16529 if (act_size == 0) { 16530 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 16531 "media serial number is not available"); 16532 un->un_status = 0; 16533 rval = 0; 16534 } else { 16535 /* 16536 * set data pointer to point to the start 16537 * of that serial number. 16538 */ 16539 un->un_media_id_method = 16540 st_get_media_id_via_media_serial_cmd; 16541 rval = 16542 st_check_if_media_changed(un, &buf[4], act_size); 16543 } 16544 } 16545 16546 kmem_free(ucmd, sizeof (struct uscsi_cmd)); 16547 kmem_free(buf, size); 16548 16549 return (rval); 16550 } 16551 16552 16553 /* ARGSUSED */ 16554 static int 16555 st_bogus_media_id(struct scsi_tape *un, ubufunc_t bufunc) 16556 { 16557 ST_FUNC(ST_DEVINFO, st_bogus_media_id); 16558 16559 ASSERT(un->un_media_id == NULL || un->un_media_id == bogusID); 16560 ASSERT(un->un_media_id_len == 0); 16561 un->un_media_id = (char *)bogusID; 16562 un->un_media_id_len = 0; 16563 return (0); 16564 } 16565 16566 typedef int (*media_chk_function)(struct scsi_tape *, ubufunc_t bufunc); 16567 16568 media_chk_function media_chk_functions[] = { 16569 st_get_media_id_via_media_serial_cmd, 16570 st_get_media_id_via_read_attribute, 16571 st_bogus_media_id 16572 }; 16573 16574 static int 16575 st_get_media_identification(struct scsi_tape *un, ubufunc_t bufunc) 16576 { 16577 int result = 0; 16578 int i; 16579 16580 ST_FUNC(ST_DEVINFO, st_get_media_identification); 16581 16582 for (i = 0; i < ST_NUM_MEMBERS(media_chk_functions); i++) { 16583 if (result == ENOTTY) { 16584 /* 16585 * Last operation type not supported by this device. 16586 * Make so next time it doesn`t do that again. 16587 */ 16588 un->un_media_id_method = media_chk_functions[i]; 16589 } else if (un->un_media_id_method != media_chk_functions[i] && 16590 un->un_media_id_method != st_get_media_identification) { 16591 continue; 16592 } 16593 result = media_chk_functions[i](un, bufunc); 16594 /* 16595 * If result indicates the function was successful or 16596 * that the media is not the same as last known, break. 16597 */ 16598 if (result == 0 || result == ESPIPE) { 16599 break; 16600 } 16601 } 16602 16603 return (result); 16604 } 16605 16606 static errstate 16607 st_command_recovery(struct scsi_tape *un, struct scsi_pkt *pkt, 16608 errstate onentry) 16609 { 16610 16611 int ret; 16612 st_err_info *errinfo; 16613 recov_info *ri = (recov_info *)pkt->pkt_private; 16614 16615 ST_FUNC(ST_DEVINFO, st_command_recovery); 16616 16617 ASSERT(MUTEX_HELD(&un->un_sd->sd_mutex)); 16618 16619 ASSERT(un->un_recov_buf_busy == 0); 16620 16621 /* 16622 * Don't try and recover a reset that this device sent. 16623 */ 16624 if (un->un_rsvd_status & ST_INITIATED_RESET && 16625 onentry == DEVICE_RESET) { 16626 return (COMMAND_DONE_ERROR); 16627 } 16628 16629 /* 16630 * See if expected position was passed with scsi_pkt. 16631 */ 16632 if (ri->privatelen == sizeof (recov_info)) { 16633 16634 /* 16635 * Not for this command. 16636 */ 16637 if (ri->cmd_attrib->do_not_recover) { 16638 return (COMMAND_DONE_ERROR); 16639 } 16640 16641 /* 16642 * Create structure to hold all error state info. 16643 */ 16644 errinfo = kmem_zalloc(ST_ERR_INFO_SIZE, KM_SLEEP); 16645 errinfo->ei_error_type = onentry; 16646 errinfo->ei_failing_bp = ri->cmd_bp; 16647 COPY_POS(&errinfo->ei_expected_pos, &ri->pos); 16648 } else { 16649 /* disabled */ 16650 return (COMMAND_DONE_ERROR); 16651 } 16652 16653 bcopy(pkt, &errinfo->ei_failed_pkt, scsi_pkt_size()); 16654 bcopy(pkt->pkt_scbp, &errinfo->ei_failing_status, SECMDS_STATUS_SIZE); 16655 ret = ddi_taskq_dispatch(un->un_recov_taskq, st_recover, errinfo, 16656 DDI_NOSLEEP); 16657 ASSERT(ret == DDI_SUCCESS); 16658 if (ret != DDI_SUCCESS) { 16659 kmem_free(errinfo, ST_ERR_INFO_SIZE); 16660 return (COMMAND_DONE_ERROR); 16661 } 16662 return (JUST_RETURN); /* release calling thread */ 16663 } 16664 16665 16666 static void 16667 st_recov_ret(struct scsi_tape *un, st_err_info *errinfo, errstate err) 16668 { 16669 int error_number; 16670 buf_t *bp; 16671 16672 16673 ST_FUNC(ST_DEVINFO, st_recov_ret); 16674 16675 ASSERT(MUTEX_HELD(&un->un_sd->sd_mutex)); 16676 #if !defined(lint) 16677 _NOTE(LOCK_RELEASED_AS_SIDE_EFFECT(&un->un_sd->sd_mutex)) 16678 #endif 16679 16680 bp = errinfo->ei_failing_bp; 16681 kmem_free(errinfo, ST_ERR_INFO_SIZE); 16682 16683 switch (err) { 16684 case JUST_RETURN: 16685 mutex_exit(&un->un_sd->sd_mutex); 16686 return; 16687 16688 case COMMAND_DONE: 16689 case COMMAND_DONE_ERROR_RECOVERED: 16690 ST_DO_KSTATS(bp, kstat_runq_exit); 16691 error_number = 0; 16692 break; 16693 16694 default: 16695 ST_DEBUG(ST_DEVINFO, st_label, CE_PANIC, 16696 "st_recov_ret with unhandled errstat %d\n", err); 16697 /* FALLTHROUGH */ 16698 case COMMAND_DONE_ERROR: 16699 case COMMAND_DONE_EACCES: 16700 ST_DO_KSTATS(bp, kstat_waitq_exit); 16701 ST_DO_ERRSTATS(un, st_transerrs); 16702 error_number = EIO; 16703 st_set_pe_flag(un); 16704 break; 16705 16706 } 16707 16708 st_bioerror(bp, error_number); 16709 st_done_and_mutex_exit(un, bp); 16710 } 16711 16712 16713 static void 16714 st_recover(void *arg) 16715 { 16716 st_err_info *const errinfo = (st_err_info *)arg; 16717 uchar_t com = errinfo->ei_failed_pkt.pkt_cdbp[0]; 16718 struct scsi_tape *un; 16719 tapepos_t cur_pos; 16720 int rval; 16721 errstate status = COMMAND_DONE_ERROR; 16722 recov_info *rcv; 16723 buf_t *bp; 16724 16725 16726 rcv = errinfo->ei_failed_pkt.pkt_private; 16727 ASSERT(rcv->privatelen == sizeof (recov_info)); 16728 bp = rcv->cmd_bp; 16729 16730 un = ddi_get_soft_state(st_state, MTUNIT(bp->b_edev)); 16731 16732 ASSERT(un != NULL); 16733 16734 mutex_enter(ST_MUTEX); 16735 16736 ST_FUNC(ST_DEVINFO, st_recover); 16737 16738 ST_CDB(ST_DEVINFO, "Recovering command", 16739 (caddr_t)errinfo->ei_failed_pkt.pkt_cdbp); 16740 ST_SENSE(ST_DEVINFO, "sense status for failed command", 16741 (caddr_t)&errinfo->ei_failing_status, 16742 sizeof (struct scsi_arq_status)); 16743 ST_POS(ST_DEVINFO, rcv->cmd_attrib->recov_pos_type == POS_STARTING ? 16744 "starting position for recovery command" : 16745 "expected position for recovery command", 16746 &errinfo->ei_expected_pos); 16747 16748 rval = st_test_path_to_device(un); 16749 16750 ST_RECOV(ST_DEVINFO, st_label, CE_NOTE, 16751 "st_recover called with %s, TUR returned %d\n", 16752 errstatenames[errinfo->ei_error_type], rval); 16753 /* 16754 * If the drive responed to the TUR lets try and get it to sync 16755 * any data it might have in the buffer. 16756 */ 16757 if (rval == 0 && rcv->cmd_attrib->chg_tape_data) { 16758 (void) st_rcmd(un, SCMD_WRITE_FILE_MARK, 0, SYNC_CMD); 16759 } 16760 switch (errinfo->ei_error_type) { 16761 case ATTEMPT_RETRY: 16762 case COMMAND_TIMEOUT: 16763 case DEVICE_RESET: 16764 case PATH_FAILED: 16765 /* 16766 * For now if we can't talk to the device we are done. 16767 * If the drive is reserved we can try to get it back. 16768 */ 16769 if (rval != 0 && rval != EACCES) { 16770 st_recov_ret(un, errinfo, COMMAND_DONE_ERROR); 16771 return; 16772 } 16773 16774 /* 16775 * If scsi II lost reserve try and get it back. 16776 */ 16777 if ((((un->un_rsvd_status & 16778 (ST_LOST_RESERVE | ST_APPLICATION_RESERVATIONS)) == 16779 ST_LOST_RESERVE)) && 16780 (errinfo->ei_failed_pkt.pkt_cdbp[0] != SCMD_RELEASE)) { 16781 rval = st_reserve_release(un, ST_RESERVE, 16782 st_uscsi_rcmd); 16783 if (rval != 0) { 16784 if (st_take_ownership(un, st_uscsi_rcmd) != 0) { 16785 st_recov_ret(un, errinfo, 16786 COMMAND_DONE_EACCES); 16787 return; 16788 } 16789 } 16790 un->un_rsvd_status |= ST_RESERVE; 16791 un->un_rsvd_status &= ~(ST_RELEASE | ST_LOST_RESERVE | 16792 ST_RESERVATION_CONFLICT | ST_INITIATED_RESET); 16793 } 16794 rval = st_check_mode_for_change(un, st_uscsi_rcmd); 16795 if (rval) { 16796 rval = st_gen_mode_select(un, st_uscsi_rcmd, 16797 un->un_mspl, sizeof (struct seq_mode)); 16798 } 16799 if (rval) { 16800 st_recov_ret(un, errinfo, COMMAND_DONE_ERROR); 16801 return; 16802 } 16803 break; 16804 case DEVICE_TAMPER: 16805 /* 16806 * Check if the ASC/ASCQ says mode data has changed. 16807 */ 16808 if ((errinfo->ei_failing_status.sts_sensedata.es_add_code == 16809 0x2a) && 16810 (errinfo->ei_failing_status.sts_sensedata.es_qual_code == 16811 0x01)) { 16812 /* 16813 * See if mode sense changed. 16814 */ 16815 rval = st_check_mode_for_change(un, st_uscsi_rcmd); 16816 if (rval) { 16817 /* 16818 * If so change it back. 16819 */ 16820 rval = st_gen_mode_select(un, st_uscsi_rcmd, 16821 un->un_mspl, sizeof (struct seq_mode)); 16822 } 16823 if (rval) { 16824 st_recov_ret(un, errinfo, COMMAND_DONE_ERROR); 16825 return; 16826 } 16827 } 16828 /* 16829 * if we have a media id and its not bogus. 16830 * Check to see if it the same. 16831 */ 16832 if (un->un_media_id != NULL && un->un_media_id != bogusID) { 16833 rval = st_get_media_identification(un, st_uscsi_rcmd); 16834 if (rval == ESPIPE) { 16835 st_recov_ret(un, errinfo, COMMAND_DONE_EACCES); 16836 return; 16837 } 16838 } 16839 break; 16840 default: 16841 ST_DEBUG(ST_DEVINFO, st_label, CE_PANIC, 16842 "Unhandled error type %s in st_recover() 0x%x\n", 16843 errstatenames[errinfo->ei_error_type], com); 16844 } 16845 16846 /* 16847 * if command is retriable retry it. 16848 * Special case here. The command attribute for SCMD_REQUEST_SENSE 16849 * does not say that it is retriable. That because if you reissue a 16850 * request sense and the target responds the sense data will have 16851 * been consumed and no long be valid. If we get a busy status on 16852 * request sense while the state is ST_STATE_SENSING this will 16853 * reissue that pkt. 16854 * 16855 * XXX If this request sense gets sent to a different port then 16856 * the original command that failed was sent on it will not get 16857 * valid sense data for that command. 16858 */ 16859 if (rcv->cmd_attrib->retriable || un->un_rqs_bp == bp) { 16860 status = st_recover_reissue_pkt(un, &errinfo->ei_failed_pkt); 16861 16862 /* 16863 * if drive doesn't support read position we are done 16864 */ 16865 } else if (un->un_read_pos_type == NO_POS) { 16866 status = COMMAND_DONE_ERROR; 16867 /* 16868 * If this command results in a changed tape position, 16869 * lets see where we are. 16870 */ 16871 } else if (rcv->cmd_attrib->chg_tape_pos) { 16872 /* 16873 * XXX May be a reason to choose a different type here. 16874 * Long format has file position information. 16875 * Short and Extended have information about whats 16876 * in the buffer. St's positioning assumes in the buffer 16877 * to be the same as on tape. 16878 */ 16879 rval = st_compare_expected_position(un, errinfo, 16880 rcv->cmd_attrib, &cur_pos); 16881 if (rval == 0) { 16882 status = COMMAND_DONE; 16883 } else if (rval == EAGAIN) { 16884 status = st_recover_reissue_pkt(un, 16885 &errinfo->ei_failed_pkt); 16886 } else { 16887 status = COMMAND_DONE_ERROR; 16888 } 16889 } else { 16890 ASSERT(0); 16891 } 16892 16893 st_recov_ret(un, errinfo, status); 16894 } 16895 16896 static void 16897 st_recov_cb(struct scsi_pkt *pkt) 16898 { 16899 struct scsi_tape *un; 16900 struct buf *bp; 16901 recov_info *rcv; 16902 errstate action = COMMAND_DONE_ERROR; 16903 int timout = ST_TRAN_BUSY_TIMEOUT; /* short (default) timeout */ 16904 16905 /* 16906 * Get the buf from the packet. 16907 */ 16908 rcv = pkt->pkt_private; 16909 ASSERT(rcv->privatelen == sizeof (recov_info)); 16910 bp = rcv->cmd_bp; 16911 16912 /* 16913 * get the unit from the buf. 16914 */ 16915 un = ddi_get_soft_state(st_state, MTUNIT(bp->b_edev)); 16916 ASSERT(un != NULL); 16917 16918 ST_FUNC(ST_DEVINFO, st_recov_cb); 16919 16920 mutex_enter(ST_MUTEX); 16921 16922 ASSERT(bp == un->un_recov_buf); 16923 16924 16925 switch (pkt->pkt_reason) { 16926 case CMD_CMPLT: 16927 if (un->un_arq_enabled && pkt->pkt_state & STATE_ARQ_DONE) { 16928 action = st_handle_autosense(un, bp, &rcv->pos); 16929 } else if ((SCBP(pkt)->sts_busy) || 16930 (SCBP(pkt)->sts_chk) || 16931 (SCBP(pkt)->sts_vu7)) { 16932 action = st_check_error(un, pkt); 16933 } else { 16934 action = COMMAND_DONE; 16935 } 16936 break; 16937 case CMD_TIMEOUT: 16938 action = COMMAND_TIMEOUT; 16939 break; 16940 case CMD_TRAN_ERR: 16941 action = QUE_COMMAND; 16942 break; 16943 default: 16944 ST_DEBUG(ST_DEVINFO, st_label, CE_PANIC, 16945 "pkt_reason not handled yet %s", 16946 scsi_rname(pkt->pkt_reason)); 16947 action = COMMAND_DONE_ERROR; 16948 } 16949 16950 /* 16951 * check for undetected path failover. 16952 */ 16953 if ((un->un_multipath) && 16954 (un->un_last_path_instance != pkt->pkt_path_instance)) { 16955 if (un->un_state > ST_STATE_OPENING) { 16956 ST_RECOV(ST_DEVINFO, st_label, CE_NOTE, 16957 "Failover detected in recovery, action is %s\n", 16958 errstatenames[action]); 16959 } 16960 un->un_last_path_instance = pkt->pkt_path_instance; 16961 } 16962 16963 ST_RECOV(ST_DEVINFO, st_label, CE_WARN, 16964 "Recovery call back got %s status on %s\n", 16965 errstatenames[action], st_print_scsi_cmd(pkt->pkt_cdbp[0])); 16966 16967 switch (action) { 16968 case COMMAND_DONE: 16969 break; 16970 16971 case COMMAND_DONE_EACCES: 16972 bioerror(bp, EACCES); 16973 break; 16974 16975 case COMMAND_DONE_ERROR_RECOVERED: /* XXX maybe wrong */ 16976 ASSERT(0); 16977 break; 16978 16979 case COMMAND_TIMEOUT: 16980 case COMMAND_DONE_ERROR: 16981 bioerror(bp, EIO); 16982 break; 16983 16984 case DEVICE_RESET: 16985 case QUE_BUSY_COMMAND: 16986 case PATH_FAILED: 16987 /* longish timeout */ 16988 timout = ST_STATUS_BUSY_TIMEOUT; 16989 /* FALLTHRU */ 16990 case QUE_COMMAND: 16991 case DEVICE_TAMPER: 16992 case ATTEMPT_RETRY: 16993 /* 16994 * let st_handle_intr_busy put this bp back on waitq and make 16995 * checks to see if it is ok to requeue the command. 16996 */ 16997 ST_DO_KSTATS(bp, kstat_runq_back_to_waitq); 16998 16999 /* 17000 * Save the throttle before setting up the timeout 17001 */ 17002 if (un->un_throttle) { 17003 un->un_last_throttle = un->un_throttle; 17004 } 17005 mutex_exit(ST_MUTEX); 17006 if (st_handle_intr_busy(un, bp, timout) == 0) { 17007 return; /* timeout is setup again */ 17008 } 17009 mutex_enter(ST_MUTEX); 17010 un->un_pos.pmode = invalid; 17011 un->un_err_resid = bp->b_resid = bp->b_bcount; 17012 st_bioerror(bp, EIO); 17013 st_set_pe_flag(un); 17014 break; 17015 17016 default: 17017 ST_DEBUG(ST_DEVINFO, st_label, CE_PANIC, 17018 "Unhandled recovery state 0x%x\n", action); 17019 un->un_pos.pmode = invalid; 17020 un->un_err_resid = bp->b_resid = bp->b_bcount; 17021 st_bioerror(bp, EIO); 17022 st_set_pe_flag(un); 17023 break; 17024 } 17025 17026 st_done_and_mutex_exit(un, bp); 17027 } 17028 17029 static int 17030 st_rcmd(struct scsi_tape *un, int com, int64_t count, int wait) 17031 { 17032 struct buf *bp; 17033 int err; 17034 17035 ST_FUNC(ST_DEVINFO, st_rcmd); 17036 17037 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 17038 "st_rcmd(un = 0x%p, com = 0x%x, count = %"PRIx64", wait = %d)\n", 17039 (void *)un, com, count, wait); 17040 17041 ASSERT(MUTEX_HELD(&un->un_sd->sd_mutex)); 17042 ASSERT(mutex_owned(ST_MUTEX)); 17043 17044 #ifdef STDEBUG 17045 if ((st_debug & 0x7)) { 17046 st_debug_cmds(un, com, count, wait); 17047 } 17048 #endif 17049 17050 while (un->un_recov_buf_busy) 17051 cv_wait(&un->un_recov_buf_cv, ST_MUTEX); 17052 un->un_recov_buf_busy = 1; 17053 17054 bp = un->un_recov_buf; 17055 bzero(bp, sizeof (buf_t)); 17056 17057 bp->b_flags = (wait) ? B_BUSY : B_BUSY|B_ASYNC; 17058 17059 err = st_setup_cmd(un, bp, com, count); 17060 17061 un->un_recov_buf_busy = 0; 17062 17063 cv_signal(&un->un_recov_buf_cv); 17064 17065 return (err); 17066 } 17067 17068 /* args used */ 17069 static int 17070 st_uscsi_rcmd(struct scsi_tape *un, struct uscsi_cmd *ucmd, int flag) 17071 { 17072 int rval; 17073 buf_t *bp; 17074 17075 ST_FUNC(ST_DEVINFO, st_uscsi_rcmd); 17076 ASSERT(flag == FKIOCTL); 17077 17078 /* 17079 * Get buffer resources... 17080 */ 17081 while (un->un_recov_buf_busy) 17082 cv_wait(&un->un_recov_buf_cv, ST_MUTEX); 17083 un->un_recov_buf_busy = 1; 17084 17085 bp = un->un_recov_buf; 17086 bzero(bp, sizeof (buf_t)); 17087 17088 bp->b_forw = (struct buf *)(uintptr_t)ucmd->uscsi_cdb[0]; 17089 bp->b_back = (struct buf *)ucmd; 17090 17091 mutex_exit(ST_MUTEX); 17092 rval = scsi_uscsi_handle_cmd(un->un_dev, UIO_SYSSPACE, ucmd, 17093 st_strategy, bp, NULL); 17094 mutex_enter(ST_MUTEX); 17095 17096 ucmd->uscsi_resid = bp->b_resid; 17097 17098 /* 17099 * Free resources 17100 */ 17101 un->un_recov_buf_busy = 0; 17102 cv_signal(&un->un_recov_buf_cv); 17103 17104 return (rval); 17105 } 17106 17107 /* 17108 * Add data to scsi_pkt to help know what to do if the command fails. 17109 */ 17110 static void 17111 st_add_recovery_info_to_pkt(struct scsi_tape *un, buf_t *bp, 17112 struct scsi_pkt *pkt) 17113 { 17114 uint64_t count; 17115 recov_info *rinfo = (recov_info *)pkt->pkt_private; 17116 17117 ST_FUNC(ST_DEVINFO, st_add_recovery_info_to_pkt); 17118 17119 ASSERT(rinfo->privatelen == sizeof (pkt_info) || 17120 rinfo->privatelen == sizeof (recov_info)); 17121 17122 SET_BP_PKT(bp, pkt); 17123 rinfo->cmd_bp = bp; 17124 17125 if (rinfo->privatelen != sizeof (recov_info)) { 17126 return; 17127 } 17128 17129 rinfo->cmd_bp = bp; 17130 17131 rinfo->cmd_attrib = NULL; 17132 17133 /* 17134 * lookup the command attributes and add them to the recovery info. 17135 */ 17136 rinfo->cmd_attrib = st_lookup_cmd_attribute(pkt->pkt_cdbp[0]); 17137 17138 ASSERT(rinfo->cmd_attrib); 17139 17140 /* 17141 * For commands that there is no way to figure the expected position 17142 * once completed, we save the position the command was started from 17143 * so that if they fail we can position back and try again. 17144 * This has already been done in st_cmd() or st_iscsi_cmd(). 17145 */ 17146 if (rinfo->cmd_attrib->recov_pos_type == POS_STARTING) { 17147 /* save current position as the starting position. */ 17148 COPY_POS(&rinfo->pos, &un->un_pos); 17149 un->un_running.pmode = invalid; 17150 return; 17151 } 17152 17153 /* 17154 * Don't want to update the running position for recovery. 17155 */ 17156 if (bp == un->un_recov_buf) { 17157 rinfo->pos.pmode = un->un_running.pmode; 17158 return; 17159 } 17160 /* 17161 * If running position is invalid copy the current position. 17162 * Running being set invalid means we are not in a read, write 17163 * or write filemark sequence. 17164 * We'll copy the current position and start from there. 17165 */ 17166 if (un->un_running.pmode == invalid) { 17167 COPY_POS(&un->un_running, &un->un_pos); 17168 COPY_POS(&rinfo->pos, &un->un_running); 17169 } else { 17170 COPY_POS(&rinfo->pos, &un->un_running); 17171 if (rinfo->pos.pmode == legacy) { 17172 /* 17173 * Always should be more logical blocks then 17174 * data blocks and files marks. 17175 */ 17176 ASSERT((rinfo->pos.blkno >= 0) ? 17177 rinfo->pos.lgclblkno >= 17178 (rinfo->pos.blkno + rinfo->pos.fileno) : 1); 17179 } 17180 } 17181 17182 /* 17183 * If the command is not expected to change the drive position 17184 * then the running position should be the expected position. 17185 */ 17186 if (rinfo->cmd_attrib->chg_tape_pos == 0) { 17187 ASSERT(rinfo->cmd_attrib->chg_tape_direction == DIR_NONE); 17188 return; 17189 } 17190 17191 if (rinfo->cmd_attrib->explicit) { 17192 ASSERT(rinfo->pos.pmode != invalid); 17193 ASSERT(rinfo->cmd_attrib->get_cnt); 17194 count = rinfo->cmd_attrib->get_cnt(pkt->pkt_cdbp); 17195 /* 17196 * This is a user generated CDB. 17197 */ 17198 if (bp == un->un_sbufp) { 17199 uint64_t lbn; 17200 17201 lbn = rinfo->cmd_attrib->get_lba(pkt->pkt_cdbp); 17202 17203 /* 17204 * See if this CDB will generate a locate or change 17205 * partition. 17206 */ 17207 if ((lbn != un->un_running.lgclblkno) || 17208 (pkt->pkt_cdbp[3] != un->un_running.partition)) { 17209 rinfo->pos.partition = pkt->pkt_cdbp[3]; 17210 rinfo->pos.pmode = logical; 17211 rinfo->pos.lgclblkno = lbn; 17212 un->un_running.partition = pkt->pkt_cdbp[3]; 17213 un->un_running.pmode = logical; 17214 un->un_running.lgclblkno = lbn; 17215 } 17216 } else { 17217 uint64_t lbn = un->un_running.lgclblkno; 17218 17219 pkt->pkt_cdbp[3] = (uchar_t)un->un_running.partition; 17220 17221 pkt->pkt_cdbp[4] = (uchar_t)(lbn >> 56); 17222 pkt->pkt_cdbp[5] = (uchar_t)(lbn >> 48); 17223 pkt->pkt_cdbp[6] = (uchar_t)(lbn >> 40); 17224 pkt->pkt_cdbp[7] = (uchar_t)(lbn >> 32); 17225 pkt->pkt_cdbp[8] = (uchar_t)(lbn >> 24); 17226 pkt->pkt_cdbp[9] = (uchar_t)(lbn >> 16); 17227 pkt->pkt_cdbp[10] = (uchar_t)(lbn >> 8); 17228 pkt->pkt_cdbp[11] = (uchar_t)(lbn); 17229 } 17230 rinfo->pos.lgclblkno += count; 17231 rinfo->pos.blkno += count; 17232 un->un_running.lgclblkno += count; 17233 return; 17234 } 17235 17236 if (rinfo->cmd_attrib->chg_tape_pos) { 17237 17238 /* should not have got an invalid position from running. */ 17239 if (un->un_mediastate == MTIO_INSERTED) { 17240 ASSERT(rinfo->pos.pmode != invalid); 17241 } 17242 17243 /* should have either a get count or or get lba function */ 17244 ASSERT(rinfo->cmd_attrib->get_cnt != NULL || 17245 rinfo->cmd_attrib->get_lba != NULL); 17246 17247 /* only explicit commands have both and they're handled above */ 17248 ASSERT(!(rinfo->cmd_attrib->get_cnt != NULL && 17249 rinfo->cmd_attrib->get_lba != NULL)); 17250 17251 /* if it has a get count function */ 17252 if (rinfo->cmd_attrib->get_cnt != NULL) { 17253 count = rinfo->cmd_attrib->get_cnt(pkt->pkt_cdbp); 17254 if (count == 0) { 17255 return; 17256 } 17257 /* 17258 * Changes position but doesn't transfer data. 17259 * i.e. rewind, write_file_mark and load. 17260 */ 17261 if (rinfo->cmd_attrib->transfers_data == TRAN_NONE) { 17262 switch (rinfo->cmd_attrib->chg_tape_direction) { 17263 case DIR_NONE: /* Erase */ 17264 ASSERT(rinfo->cmd_attrib->cmd == 17265 SCMD_ERASE); 17266 break; 17267 case DIR_FORW: /* write_file_mark */ 17268 rinfo->pos.fileno += count; 17269 rinfo->pos.lgclblkno += count; 17270 rinfo->pos.blkno = 0; 17271 un->un_running.fileno += count; 17272 un->un_running.lgclblkno += count; 17273 un->un_running.blkno = 0; 17274 break; 17275 case DIR_REVC: /* rewind */ 17276 rinfo->pos.fileno = 0; 17277 rinfo->pos.lgclblkno = 0; 17278 rinfo->pos.blkno = 0; 17279 rinfo->pos.eof = ST_NO_EOF; 17280 rinfo->pos.pmode = legacy; 17281 un->un_running.fileno = 0; 17282 un->un_running.lgclblkno = 0; 17283 un->un_running.blkno = 0; 17284 un->un_running.eof = ST_NO_EOF; 17285 if (un->un_running.pmode != legacy) 17286 un->un_running.pmode = legacy; 17287 break; 17288 case DIR_EITH: /* Load unload */ 17289 ASSERT(rinfo->cmd_attrib->cmd == 17290 SCMD_LOAD); 17291 switch (count & (LD_LOAD | LD_RETEN | 17292 LD_RETEN | LD_HOLD)) { 17293 case LD_UNLOAD: 17294 case LD_RETEN: 17295 case LD_HOLD: 17296 case LD_LOAD | LD_HOLD: 17297 case LD_EOT | LD_HOLD: 17298 case LD_RETEN | LD_HOLD: 17299 rinfo->pos.pmode = invalid; 17300 un->un_running.pmode = invalid; 17301 break; 17302 case LD_EOT: 17303 case LD_LOAD | LD_EOT: 17304 rinfo->pos.eof = ST_EOT; 17305 rinfo->pos.pmode = invalid; 17306 un->un_running.eof = ST_EOT; 17307 un->un_running.pmode = invalid; 17308 break; 17309 case LD_LOAD: 17310 case LD_RETEN | LD_LOAD: 17311 rinfo->pos.fileno = 0; 17312 rinfo->pos.lgclblkno = 0; 17313 rinfo->pos.blkno = 0; 17314 rinfo->pos.eof = ST_NO_EOF; 17315 rinfo->pos.pmode = legacy; 17316 un->un_running.fileno = 0; 17317 un->un_running.lgclblkno = 0; 17318 un->un_running.blkno = 0; 17319 un->un_running.eof = ST_NO_EOF; 17320 break; 17321 default: 17322 ASSERT(0); 17323 } 17324 break; 17325 default: 17326 ASSERT(0); 17327 break; 17328 } 17329 } else { 17330 /* 17331 * Changes position and does transfer data. 17332 * i.e. read or write. 17333 */ 17334 switch (rinfo->cmd_attrib->chg_tape_direction) { 17335 case DIR_FORW: 17336 rinfo->pos.lgclblkno += count; 17337 rinfo->pos.blkno += count; 17338 un->un_running.lgclblkno += count; 17339 un->un_running.blkno += count; 17340 break; 17341 case DIR_REVC: 17342 rinfo->pos.lgclblkno -= count; 17343 rinfo->pos.blkno -= count; 17344 un->un_running.lgclblkno -= count; 17345 un->un_running.blkno -= count; 17346 break; 17347 default: 17348 ASSERT(0); 17349 break; 17350 } 17351 } 17352 } else if (rinfo->cmd_attrib->get_lba != NULL) { 17353 /* Have a get LBA fuction. i.e. Locate */ 17354 ASSERT(rinfo->cmd_attrib->chg_tape_direction == 17355 DIR_EITH); 17356 count = rinfo->cmd_attrib->get_lba(pkt->pkt_cdbp); 17357 un->un_running.lgclblkno = count; 17358 un->un_running.blkno = 0; 17359 un->un_running.fileno = 0; 17360 un->un_running.pmode = logical; 17361 rinfo->pos.lgclblkno = count; 17362 rinfo->pos.pmode = invalid; 17363 } else { 17364 ASSERT(0); 17365 } 17366 return; 17367 } 17368 17369 ST_CDB(ST_DEVINFO, "Unhanded CDB for position prediction", 17370 (char *)pkt->pkt_cdbp); 17371 17372 } 17373 17374 static int 17375 st_check_mode_for_change(struct scsi_tape *un, ubufunc_t ubf) 17376 { 17377 struct seq_mode *current; 17378 int rval; 17379 int i; 17380 caddr_t this; 17381 caddr_t that; 17382 17383 ST_FUNC(ST_DEVINFO, st_check_mode_for_change); 17384 17385 /* recovery called with mode tamper before mode selection */ 17386 if (un->un_comp_page == (ST_DEV_DATACOMP_PAGE | ST_DEV_CONFIG_PAGE)) { 17387 ST_RECOV(ST_DEVINFO, st_label, CE_NOTE, 17388 "Mode Select not done yet"); 17389 return (0); 17390 } 17391 17392 current = kmem_zalloc(sizeof (struct seq_mode), KM_SLEEP); 17393 17394 rval = st_gen_mode_sense(un, ubf, un->un_comp_page, current, 17395 sizeof (struct seq_mode)); 17396 if (rval != 0) { 17397 ST_RECOV(ST_DEVINFO, st_label, CE_NOTE, 17398 "Mode Sense for mode verification failed"); 17399 kmem_free(current, sizeof (struct seq_mode)); 17400 return (rval); 17401 } 17402 17403 this = (caddr_t)current; 17404 that = (caddr_t)un->un_mspl; 17405 17406 rval = bcmp(this, that, sizeof (struct seq_mode)); 17407 if (rval == 0) { 17408 ST_RECOV(ST_DEVINFO, st_label, CE_NOTE, 17409 "Found no changes in mode data"); 17410 } 17411 #ifdef STDEBUG 17412 else { 17413 for (i = 1; i < sizeof (struct seq_mode); i++) { 17414 if (this[i] != that[i]) { 17415 ST_RECOV(ST_DEVINFO, st_label, CE_CONT, 17416 "sense data changed at byte %d was " 17417 "0x%x now 0x%x", i, 17418 (uchar_t)that[i], (uchar_t)this[i]); 17419 } 17420 } 17421 } 17422 #endif 17423 kmem_free(current, sizeof (struct seq_mode)); 17424 17425 return (rval); 17426 } 17427 17428 static int 17429 st_test_path_to_device(struct scsi_tape *un) 17430 { 17431 int rval = 0; 17432 int limit = st_retry_count; 17433 17434 ST_FUNC(ST_DEVINFO, st_test_path_to_device); 17435 17436 /* 17437 * XXX Newer drives may not RESEVATION CONFLICT a TUR. 17438 */ 17439 do { 17440 if (rval != 0) { 17441 mutex_exit(ST_MUTEX); 17442 delay(drv_usectohz(1000000)); 17443 mutex_enter(ST_MUTEX); 17444 } 17445 rval = st_rcmd(un, SCMD_TEST_UNIT_READY, 0, SYNC_CMD); 17446 ST_RECOV(ST_DEVINFO, st_label, CE_NOTE, 17447 "ping TUR returned 0x%x", rval); 17448 limit--; 17449 } while (((rval == EACCES) || (rval == EBUSY)) && limit); 17450 17451 if (un->un_status == KEY_NOT_READY || un->un_mediastate == MTIO_EJECTED) 17452 rval = 0; 17453 17454 return (rval); 17455 } 17456 17457 /* 17458 * Does read position using recov_buf and doesn't update un_pos. 17459 * Does what ever kind of read position you want. 17460 */ 17461 static int 17462 st_recovery_read_pos(struct scsi_tape *un, read_p_types type, 17463 read_pos_data_t *raw) 17464 { 17465 int rval; 17466 struct uscsi_cmd cmd; 17467 struct scsi_arq_status status; 17468 char cdb[CDB_GROUP1]; 17469 17470 ST_FUNC(ST_DEVINFO, st_recovery_read_pos); 17471 bzero(&cmd, sizeof (cmd)); 17472 17473 cdb[0] = SCMD_READ_POSITION; 17474 cdb[1] = type; 17475 cdb[2] = 0; 17476 cdb[3] = 0; 17477 cdb[4] = 0; 17478 cdb[5] = 0; 17479 cdb[6] = 0; 17480 cdb[7] = 0; 17481 cdb[8] = (type == EXT_POS) ? 28 : 0; 17482 cdb[9] = 0; 17483 17484 cmd.uscsi_flags = USCSI_READ | USCSI_RQENABLE; 17485 cmd.uscsi_timeout = un->un_dp->non_motion_timeout; 17486 cmd.uscsi_cdb = cdb; 17487 cmd.uscsi_cdblen = sizeof (cdb); 17488 cmd.uscsi_rqlen = sizeof (status); 17489 cmd.uscsi_rqbuf = (caddr_t)&status; 17490 cmd.uscsi_bufaddr = (caddr_t)raw; 17491 switch (type) { 17492 case SHORT_POS: 17493 cmd.uscsi_buflen = sizeof (tape_position_t); 17494 break; 17495 case LONG_POS: 17496 cmd.uscsi_buflen = sizeof (tape_position_long_t); 17497 break; 17498 case EXT_POS: 17499 cmd.uscsi_buflen = sizeof (tape_position_ext_t); 17500 break; 17501 default: 17502 ASSERT(0); 17503 } 17504 17505 rval = st_uscsi_rcmd(un, &cmd, FKIOCTL); 17506 if (cmd.uscsi_status) { 17507 rval = EIO; 17508 } 17509 return (rval); 17510 } 17511 17512 static int 17513 st_recovery_get_position(struct scsi_tape *un, tapepos_t *read, 17514 read_pos_data_t *raw) 17515 { 17516 int rval; 17517 read_p_types type = un->un_read_pos_type; 17518 17519 ST_FUNC(ST_DEVINFO, st_recovery_get_position); 17520 17521 do { 17522 rval = st_recovery_read_pos(un, type, raw); 17523 if (rval != 0) { 17524 switch (type) { 17525 case SHORT_POS: 17526 type = NO_POS; 17527 break; 17528 17529 case LONG_POS: 17530 type = EXT_POS; 17531 break; 17532 17533 case EXT_POS: 17534 type = SHORT_POS; 17535 break; 17536 17537 default: 17538 type = LONG_POS; 17539 break; 17540 17541 } 17542 } else { 17543 if (type != un->un_read_pos_type) { 17544 un->un_read_pos_type = type; 17545 } 17546 break; 17547 } 17548 } while (type != NO_POS); 17549 17550 if (rval == 0) { 17551 rval = st_interpret_read_pos(un, read, type, 17552 sizeof (read_pos_data_t), (caddr_t)raw, 1); 17553 } 17554 return (rval); 17555 } 17556 17557 /* 17558 * based on the command do we retry, continue or give up? 17559 * possable return values? 17560 * zero do nothing looks fine. 17561 * EAGAIN retry. 17562 * EIO failed makes no sense. 17563 */ 17564 static int 17565 st_compare_expected_position(struct scsi_tape *un, st_err_info *ei, 17566 cmd_attribute const * cmd_att, tapepos_t *read) 17567 { 17568 int rval; 17569 read_pos_data_t *readp_datap; 17570 17571 ST_FUNC(ST_DEVINFO, st_compare_expected_position); 17572 17573 ASSERT(un != NULL); 17574 ASSERT(ei != NULL); 17575 ASSERT(read != NULL); 17576 ASSERT(cmd_att->chg_tape_pos); 17577 17578 COPY_POS(read, &ei->ei_expected_pos); 17579 17580 readp_datap = kmem_zalloc(sizeof (read_pos_data_t), KM_SLEEP); 17581 17582 rval = st_recovery_get_position(un, read, readp_datap); 17583 17584 kmem_free(readp_datap, sizeof (read_pos_data_t)); 17585 17586 if (rval != 0) { 17587 return (EIO); 17588 } 17589 17590 ST_POS(ST_DEVINFO, "st_compare_expected_position", read); 17591 17592 if ((read->pmode == invalid) || 17593 (ei->ei_expected_pos.pmode == invalid)) { 17594 return (EIO); 17595 } 17596 17597 /* 17598 * Command that changes tape position and have an expected position 17599 * if it were to chave completed sucessfully. 17600 */ 17601 if (cmd_att->recov_pos_type == POS_EXPECTED) { 17602 uint32_t count; 17603 int64_t difference; 17604 uchar_t reposition = 0; 17605 17606 ASSERT(cmd_att->get_cnt); 17607 count = cmd_att->get_cnt(ei->ei_failed_pkt.pkt_cdbp); 17608 17609 ST_RECOV(ST_DEVINFO, st_label, CE_NOTE, 17610 "Got count from CDB and it was %d\n", count); 17611 17612 /* 17613 * At expected? 17614 */ 17615 if (read->lgclblkno == ei->ei_expected_pos.lgclblkno) { 17616 ST_RECOV(ST_DEVINFO, st_label, CE_NOTE, 17617 "Found drive to be at expected position\n"); 17618 17619 /* 17620 * If the command should move tape and it got a busy 17621 * it shouldn't be in the expected position. 17622 */ 17623 if (ei->ei_failing_status.sts_status.sts_busy != 0) { 17624 reposition = 1; 17625 17626 /* 17627 * If the command doesn't transfer data should be good. 17628 */ 17629 } else if (cmd_att->transfers_data == TRAN_NONE) { 17630 return (0); /* Good */ 17631 17632 /* 17633 * Command transfers data, should have done so. 17634 */ 17635 } else if (ei->ei_failed_pkt.pkt_state & 17636 STATE_XFERRED_DATA) { 17637 return (0); /* Good */ 17638 } else { 17639 reposition = 1; 17640 } 17641 } 17642 17643 if (cmd_att->chg_tape_direction == DIR_FORW) { 17644 difference = 17645 ei->ei_expected_pos.lgclblkno - read->lgclblkno; 17646 17647 ST_RECOV(ST_DEVINFO, st_label, CE_NOTE, 17648 "difference between expected and actual is %" 17649 PRId64"\n", difference); 17650 if (count == difference && reposition == 0) { 17651 ST_RECOV(ST_DEVINFO, st_label, CE_NOTE, 17652 "Found failed FORW command, retrying\n"); 17653 return (EAGAIN); 17654 } 17655 17656 /* 17657 * If rewound or somewhere between the starting position 17658 * and the expected position (partial read or write). 17659 * Locate to the starting position and try the whole 17660 * thing over again. 17661 */ 17662 if ((read->lgclblkno == 0) || 17663 ((difference > 0) && (difference < count))) { 17664 rval = st_logical_block_locate(un, 17665 st_uscsi_rcmd, read, 17666 ei->ei_expected_pos.lgclblkno - count, 17667 ei->ei_expected_pos.partition); 17668 if (rval == 0) { 17669 ST_RECOV(ST_DEVINFO, st_label, 17670 CE_NOTE, "reestablished FORW" 17671 " command retrying\n"); 17672 return (EAGAIN); 17673 } 17674 /* 17675 * This handles flushed read ahead on the drive or 17676 * an aborted read that presents as a busy and advanced 17677 * the tape position. 17678 */ 17679 } else if ((cmd_att->transfers_data == TRAN_READ) && 17680 ((difference < 0) || (reposition == 1))) { 17681 rval = st_logical_block_locate(un, 17682 st_uscsi_rcmd, read, 17683 ei->ei_expected_pos.lgclblkno - count, 17684 ei->ei_expected_pos.partition); 17685 if (rval == 0) { 17686 ST_RECOV(ST_DEVINFO, st_label, 17687 CE_NOTE, "reestablished FORW" 17688 " read command retrying\n"); 17689 return (EAGAIN); 17690 } 17691 /* 17692 * XXX swag seeing difference of 2 on write filemark. 17693 * If the space to the starting position works on a 17694 * write that means the previous write made it to tape. 17695 * If not we lost data and have to give up. 17696 * 17697 * The plot thickens. Now I am attempting to cover a 17698 * count of 1 and a differance of 2 on a write. 17699 */ 17700 } else if ((difference > count) || (reposition == 1)) { 17701 rval = st_logical_block_locate(un, 17702 st_uscsi_rcmd, read, 17703 ei->ei_expected_pos.lgclblkno - count, 17704 ei->ei_expected_pos.partition); 17705 if (rval == 0) { 17706 ST_RECOV(ST_DEVINFO, st_label, 17707 CE_NOTE, "reestablished FORW" 17708 " write command retrying\n"); 17709 return (EAGAIN); 17710 } 17711 ST_RECOV(ST_DEVINFO, st_label, CE_NOTE, 17712 "Seek to block %"PRId64" returned %d\n", 17713 ei->ei_expected_pos.lgclblkno - count, 17714 rval); 17715 } else { 17716 ST_RECOV(ST_DEVINFO, st_label, CE_NOTE, 17717 "Not expected transfers_data = %d " 17718 "difference = %"PRId64, 17719 cmd_att->transfers_data, difference); 17720 } 17721 17722 return (EIO); 17723 17724 } else if (cmd_att->chg_tape_direction == DIR_REVC) { 17725 /* Don't think we can write backwards */ 17726 ASSERT(cmd_att->transfers_data != TRAN_WRTE); 17727 difference = 17728 read->lgclblkno - ei->ei_expected_pos.lgclblkno; 17729 ST_RECOV(ST_DEVINFO, st_label, CE_NOTE, 17730 "difference between expected and actual is %" 17731 PRId64"\n", difference); 17732 if (count == difference && reposition == 0) { 17733 ST_RECOV(ST_DEVINFO, st_label, CE_NOTE, 17734 "Found failed REVC command, retrying\n"); 17735 return (EAGAIN); 17736 } 17737 if ((read->lgclblkno == 0) || 17738 ((difference > 0) && (difference < count))) { 17739 rval = st_logical_block_locate(un, 17740 st_uscsi_rcmd, read, 17741 ei->ei_expected_pos.lgclblkno + count, 17742 ei->ei_expected_pos.partition); 17743 if (rval == 0) { 17744 ST_RECOV(ST_DEVINFO, st_label, 17745 CE_NOTE, "reestablished REVC" 17746 " command retrying\n"); 17747 return (EAGAIN); 17748 } 17749 /* This handles read ahead in reverse direction */ 17750 } else if ((cmd_att->transfers_data == TRAN_READ) && 17751 (difference < 0) || (reposition == 1)) { 17752 rval = st_logical_block_locate(un, 17753 st_uscsi_rcmd, read, 17754 ei->ei_expected_pos.lgclblkno - count, 17755 ei->ei_expected_pos.partition); 17756 if (rval == 0) { 17757 ST_RECOV(ST_DEVINFO, st_label, 17758 CE_NOTE, "reestablished REVC" 17759 " read command retrying\n"); 17760 return (EAGAIN); 17761 } 17762 } else { 17763 ST_RECOV(ST_DEVINFO, st_label, CE_NOTE, 17764 "Not expected transfers_data = %d " 17765 "difference = %"PRId64, 17766 cmd_att->transfers_data, difference); 17767 } 17768 return (EIO); 17769 17770 } else { 17771 /* 17772 * Commands that change tape position either 17773 * direction or don't change position should not 17774 * get here. 17775 */ 17776 ASSERT(0); 17777 } 17778 ST_RECOV(ST_DEVINFO, st_label, CE_NOTE, 17779 "Didn't find a recoverable position, Failing\n"); 17780 17781 /* 17782 * Command that changes tape position and can only be recovered 17783 * by going back to the point of origin and retrying. 17784 * 17785 * Example SCMD_SPACE. 17786 */ 17787 } else if (cmd_att->recov_pos_type == POS_STARTING) { 17788 /* 17789 * This type of command stores the starting position. 17790 * If the read position is the starting position, 17791 * reissue the command. 17792 */ 17793 if (ei->ei_expected_pos.lgclblkno == read->lgclblkno) { 17794 ST_RECOV(ST_DEVINFO, st_label, CE_NOTE, 17795 "Found Space command at starting position, " 17796 "Reissuing\n"); 17797 return (EAGAIN); 17798 } 17799 /* 17800 * Not in the position that the command was originally issued, 17801 * Attempt to locate to that position. 17802 */ 17803 rval = st_logical_block_locate(un, st_uscsi_rcmd, read, 17804 ei->ei_expected_pos.lgclblkno, 17805 ei->ei_expected_pos.partition); 17806 if (rval) { 17807 ST_RECOV(ST_DEVINFO, st_label, CE_NOTE, 17808 "Found Space at an unexpected position and locate " 17809 "back to starting position failed\n"); 17810 return (EIO); 17811 } 17812 ST_RECOV(ST_DEVINFO, st_label, CE_NOTE, 17813 "Found Space at an unexpected position and locate " 17814 "back to starting position worked, Reissuing\n"); 17815 return (EAGAIN); 17816 } 17817 st_print_position(ST_DEVINFO, st_label, CE_NOTE, 17818 "Unhandled attribute/expected position", &ei->ei_expected_pos); 17819 st_print_position(ST_DEVINFO, st_label, CE_NOTE, 17820 "Read position above did not make sense", read); 17821 ASSERT(0); 17822 return (EIO); 17823 } 17824 17825 static errstate 17826 st_recover_reissue_pkt(struct scsi_tape *un, struct scsi_pkt *oldpkt) 17827 { 17828 buf_t *bp; 17829 buf_t *pkt_bp; 17830 struct scsi_pkt *newpkt; 17831 cmd_attribute const *attrib; 17832 recov_info *rcv = oldpkt->pkt_private; 17833 uint_t cdblen; 17834 int queued = 0; 17835 int rval; 17836 int flags = 0; 17837 int stat_size = 17838 (un->un_arq_enabled ? sizeof (struct scsi_arq_status) : 1); 17839 17840 ST_FUNC(ST_DEVINFO, st_recover_reissue_pkt); 17841 17842 bp = rcv->cmd_bp; 17843 17844 if (rcv->privatelen == sizeof (recov_info)) { 17845 attrib = rcv->cmd_attrib; 17846 } else { 17847 attrib = st_lookup_cmd_attribute(oldpkt->pkt_cdbp[0]); 17848 } 17849 17850 /* 17851 * Some non-uscsi commands use the b_bcount for values that 17852 * have nothing to do with how much data is transfered. 17853 * In those cases we need to hide the buf_t from scsi_init_pkt(). 17854 */ 17855 if ((BP_UCMD(bp)) && (bp->b_bcount)) { 17856 pkt_bp = bp; 17857 } else if (attrib->transfers_data == TRAN_NONE) { 17858 pkt_bp = NULL; 17859 } else { 17860 pkt_bp = bp; 17861 } 17862 17863 /* 17864 * if this is a queued command make sure it the only one in the 17865 * run queue. 17866 */ 17867 if (bp != un->un_sbufp && bp != un->un_recov_buf) { 17868 ASSERT(un->un_runqf == un->un_runql); 17869 ASSERT(un->un_runqf == bp); 17870 queued = 1; 17871 } 17872 17873 cdblen = scsi_cdb_size[CDB_GROUPID(oldpkt->pkt_cdbp[0])]; 17874 17875 if (pkt_bp == un->un_rqs_bp) { 17876 flags |= PKT_CONSISTENT; 17877 stat_size = 1; 17878 } 17879 17880 newpkt = scsi_init_pkt(ROUTE, NULL, pkt_bp, cdblen, 17881 stat_size, rcv->privatelen, flags, NULL_FUNC, NULL); 17882 if (newpkt == NULL) { 17883 ST_RECOV(ST_DEVINFO, st_label, CE_NOTE, 17884 "Reissue pkt scsi_init_pkt() failure\n"); 17885 return (COMMAND_DONE_ERROR); 17886 } 17887 17888 ASSERT(newpkt->pkt_resid == 0); 17889 bp->b_flags &= ~(B_DONE); 17890 bp->b_resid = 0; 17891 st_bioerror(bp, 0); 17892 17893 bcopy(oldpkt->pkt_private, newpkt->pkt_private, rcv->privatelen); 17894 17895 newpkt->pkt_comp = oldpkt->pkt_comp; 17896 newpkt->pkt_time = oldpkt->pkt_time; 17897 17898 bzero(newpkt->pkt_scbp, stat_size); 17899 bcopy(oldpkt->pkt_cdbp, newpkt->pkt_cdbp, cdblen); 17900 17901 newpkt->pkt_state = 0; 17902 newpkt->pkt_statistics = 0; 17903 17904 /* 17905 * oldpkt passed in was a copy of the original. 17906 * to distroy we need the address of the original. 17907 */ 17908 oldpkt = BP_PKT(bp); 17909 17910 if (oldpkt == un->un_rqs) { 17911 ASSERT(bp == un->un_rqs_bp); 17912 un->un_rqs = newpkt; 17913 } 17914 17915 SET_BP_PKT(bp, newpkt); 17916 17917 scsi_destroy_pkt(oldpkt); 17918 17919 rval = st_transport(un, newpkt); 17920 if (rval == TRAN_ACCEPT) { 17921 return (JUST_RETURN); 17922 } 17923 ST_RECOV(ST_DEVINFO, st_label, CE_NOTE, 17924 "Reissue pkt st_transport(0x%x) failure\n", rval); 17925 if (rval != TRAN_BUSY) { 17926 return (COMMAND_DONE_ERROR); 17927 } 17928 mutex_exit(ST_MUTEX); 17929 rval = st_handle_start_busy(un, bp, ST_TRAN_BUSY_TIMEOUT, queued); 17930 mutex_enter(ST_MUTEX); 17931 if (rval) { 17932 return (COMMAND_DONE_ERROR); 17933 } 17934 17935 return (JUST_RETURN); 17936 } 17937 17938 static int 17939 st_transport(struct scsi_tape *un, struct scsi_pkt *pkt) 17940 { 17941 int status; 17942 17943 ST_FUNC(ST_DEVINFO, st_transport); 17944 17945 ST_CDB(ST_DEVINFO, "transport CDB", (caddr_t)pkt->pkt_cdbp); 17946 17947 mutex_exit(ST_MUTEX); 17948 17949 status = scsi_transport(pkt); 17950 17951 mutex_enter(ST_MUTEX); 17952 17953 return (status); 17954 } 17955 17956 /* 17957 * Removed the buf_t bp from the queue referenced to by head and tail. 17958 * Returns the buf_t pointer if it is found in the queue. 17959 * Returns NULL if it is not found. 17960 */ 17961 static buf_t * 17962 st_remove_from_queue(buf_t **head, buf_t **tail, buf_t *bp) 17963 { 17964 buf_t *runqbp; 17965 buf_t *prevbp = NULL; 17966 17967 for (runqbp = *head; runqbp != 0; runqbp = runqbp->av_forw) { 17968 if (runqbp == bp) { 17969 /* found it, is it at the head? */ 17970 if (runqbp == *head) { 17971 *head = bp->av_forw; 17972 } else { 17973 prevbp->av_forw = bp->av_forw; 17974 } 17975 if (*tail == bp) { 17976 *tail = prevbp; 17977 } 17978 bp->av_forw = NULL; 17979 return (bp); /* found and removed */ 17980 } 17981 prevbp = runqbp; 17982 } 17983 return (NULL); 17984 } 17985 17986 /* 17987 * Adds a buf_t to the queue pointed to by head and tail. 17988 * Adds it either to the head end or the tail end based on which 17989 * the passed variable end (head or tail) points at. 17990 */ 17991 static void 17992 st_add_to_queue(buf_t **head, buf_t **tail, buf_t *end, buf_t *bp) 17993 { 17994 17995 bp->av_forw = NULL; 17996 if (*head) { 17997 /* Queue is not empty */ 17998 if (end == *head) { 17999 /* Add at front of queue */ 18000 bp->av_forw = *head; 18001 *head = bp; 18002 } else if (end == *tail) { 18003 /* Add at end of queue */ 18004 (*tail)->av_forw = bp; 18005 *tail = bp; 18006 } else { 18007 ASSERT(0); 18008 } 18009 } else { 18010 /* Queue is empty */ 18011 *head = bp; 18012 *tail = bp; 18013 } 18014 } 18015 18016 18017 static uint64_t 18018 st_get_cdb_g0_rw_count(uchar_t *cdb) 18019 { 18020 uint64_t count; 18021 18022 if ((cdb[1]) & 1) { 18023 /* fixed block mode, the count is the number of blocks */ 18024 count = 18025 cdb[2] << 16 | 18026 cdb[3] << 8 | 18027 cdb[4]; 18028 } else { 18029 /* variable block mode, the count is the block size */ 18030 count = 1; 18031 } 18032 return (count); 18033 } 18034 18035 static uint64_t 18036 st_get_cdb_g0_sign_count(uchar_t *cdb) 18037 { 18038 uint64_t count; 18039 18040 count = 18041 cdb[2] << 16 | 18042 cdb[3] << 8 | 18043 cdb[4]; 18044 /* 18045 * If the sign bit of the 3 byte value is set, extended it. 18046 */ 18047 if (count & 0x800000) { 18048 count |= 0xffffffffff000000; 18049 } 18050 return (count); 18051 } 18052 18053 static uint64_t 18054 st_get_cdb_g0_count(uchar_t *cdb) 18055 { 18056 uint64_t count; 18057 18058 count = 18059 cdb[2] << 16 | 18060 cdb[3] << 8 | 18061 cdb[4]; 18062 return (count); 18063 } 18064 18065 static uint64_t 18066 st_get_cdb_g5_rw_cnt(uchar_t *cdb) 18067 { 18068 uint64_t count; 18069 18070 if ((cdb[1]) & 1) { 18071 /* fixed block mode */ 18072 count = 18073 cdb[12] << 16 | 18074 cdb[13] << 8 | 18075 cdb[14]; 18076 } else { 18077 /* variable block mode */ 18078 count = 1; 18079 } 18080 return (count); 18081 } 18082 18083 static uint64_t 18084 st_get_no_count(uchar_t *cdb) 18085 { 18086 ASSERT(cdb[0] == SCMD_REWIND); 18087 return ((uint64_t)cdb[0]); 18088 } 18089 18090 static uint64_t 18091 st_get_load_options(uchar_t *cdb) 18092 { 18093 return ((uint64_t)(cdb[4] | (LD_HOLD << 1))); 18094 } 18095 18096 static uint64_t 18097 st_get_erase_options(uchar_t *cdb) 18098 { 18099 return (cdb[1] | (cdb[0] << 8)); 18100 } 18101 18102 static uint64_t 18103 st_get_cdb_g1_lba(uchar_t *cdb) 18104 { 18105 uint64_t lba; 18106 18107 lba = 18108 cdb[3] << 24 | 18109 cdb[4] << 16 | 18110 cdb[5] << 8 | 18111 cdb[6]; 18112 return (lba); 18113 } 18114 18115 static uint64_t 18116 st_get_cdb_g5_count(uchar_t *cdb) 18117 { 18118 uint64_t count = 18119 cdb[12] << 16 | 18120 cdb[13] << 8 | 18121 cdb[14]; 18122 18123 return (count); 18124 } 18125 18126 static uint64_t 18127 st_get_cdb_g4g5_cnt(uchar_t *cdb) 18128 { 18129 uint64_t lba; 18130 18131 lba = 18132 (uint64_t)cdb[4] << 56 | 18133 (uint64_t)cdb[5] << 48 | 18134 (uint64_t)cdb[6] << 40 | 18135 (uint64_t)cdb[7] << 32 | 18136 (uint64_t)cdb[8] << 24 | 18137 (uint64_t)cdb[9] << 16 | 18138 (uint64_t)cdb[10] << 8 | 18139 (uint64_t)cdb[11]; 18140 return (lba); 18141 } 18142 18143 static const cmd_attribute cmd_attributes[] = { 18144 { SCMD_READ, 18145 1, 0, 1, 0, 0, DIR_FORW, TRAN_READ, POS_EXPECTED, 18146 0, 0, 0, st_get_cdb_g0_rw_count }, 18147 { SCMD_WRITE, 18148 1, 0, 1, 1, 0, DIR_FORW, TRAN_WRTE, POS_EXPECTED, 18149 0, 0, 0, st_get_cdb_g0_rw_count }, 18150 { SCMD_TEST_UNIT_READY, 18151 0, 1, 0, 0, 0, DIR_NONE, TRAN_NONE, POS_EXPECTED, 18152 0, 0, 0 }, 18153 { SCMD_REWIND, 18154 1, 1, 1, 0, 0, DIR_REVC, TRAN_NONE, POS_EXPECTED, 18155 0, 0, 0, st_get_no_count }, 18156 { SCMD_REQUEST_SENSE, 18157 0, 0, 0, 0, 0, DIR_NONE, TRAN_READ, POS_EXPECTED, 18158 0, 0, 0 }, 18159 { SCMD_READ_BLKLIM, 18160 0, 1, 0, 0, 0, DIR_NONE, TRAN_READ, POS_EXPECTED, 18161 0, 0, 0 }, 18162 { SCMD_READ_G4, 18163 1, 0, 1, 0, 1, DIR_FORW, TRAN_READ, POS_EXPECTED, 18164 0, 0, 0, st_get_cdb_g5_rw_cnt, st_get_cdb_g4g5_cnt }, 18165 { SCMD_WRITE_G4, 18166 1, 0, 1, 1, 1, DIR_FORW, TRAN_WRTE, POS_EXPECTED, 18167 0, 0, 0, st_get_cdb_g5_rw_cnt, st_get_cdb_g4g5_cnt }, 18168 { SCMD_READ_REVERSE, 18169 1, 0, 1, 1, 0, DIR_REVC, TRAN_READ, POS_EXPECTED, 18170 0, 0, 0, st_get_cdb_g0_rw_count }, 18171 { SCMD_READ_REVERSE_G4, 18172 1, 0, 1, 1, 1, DIR_REVC, TRAN_READ, POS_EXPECTED, 18173 0, 0, 0, st_get_cdb_g5_rw_cnt, st_get_cdb_g4g5_cnt }, 18174 { SCMD_WRITE_FILE_MARK, 18175 1, 0, 1, 1, 0, DIR_FORW, TRAN_NONE, POS_EXPECTED, 18176 0, 0, 0, st_get_cdb_g0_count }, 18177 { SCMD_WRITE_FILE_MARK_G4, 18178 1, 0, 1, 1, 1, DIR_FORW, TRAN_NONE, POS_EXPECTED, 18179 0, 0, 0, st_get_cdb_g5_count, st_get_cdb_g4g5_cnt }, 18180 { SCMD_SPACE, 18181 1, 0, 1, 0, 0, DIR_EITH, TRAN_NONE, POS_STARTING, 18182 0, 0, 0, st_get_cdb_g0_sign_count }, 18183 { SCMD_SPACE_G4, 18184 1, 0, 1, 0, 0, DIR_EITH, TRAN_NONE, POS_STARTING, 18185 0, 0, 0, st_get_cdb_g4g5_cnt }, 18186 { SCMD_INQUIRY, 18187 0, 1, 0, 0, 0, DIR_NONE, TRAN_READ, POS_EXPECTED, 18188 0, 0, 0 }, 18189 { SCMD_VERIFY_G0, 18190 1, 0, 1, 0, 0, DIR_FORW, TRAN_NONE, POS_EXPECTED, 18191 0, 0, 0, st_get_cdb_g0_rw_count }, 18192 { SCMD_VERIFY_G4, 18193 1, 0, 1, 0, 1, DIR_FORW, TRAN_NONE, POS_EXPECTED, 18194 0, 0, 0, st_get_cdb_g5_rw_cnt, st_get_cdb_g4g5_cnt }, 18195 { SCMD_RECOVER_BUF, 18196 1, 0, 1, 1, 0, DIR_REVC, TRAN_READ, POS_EXPECTED, 18197 0, 0, 0 }, 18198 { SCMD_MODE_SELECT, 18199 1, 1, 0, 0, 0, DIR_NONE, TRAN_WRTE, POS_EXPECTED, 18200 0, 0, 0 }, 18201 { SCMD_RESERVE, 18202 0, 1, 0, 0, 0, DIR_NONE, TRAN_NONE, POS_EXPECTED, 18203 0, 0, 0 }, 18204 { SCMD_RELEASE, 18205 0, 1, 0, 0, 0, DIR_NONE, TRAN_NONE, POS_EXPECTED, 18206 0, 0, 0 }, 18207 { SCMD_ERASE, 18208 1, 0, 1, 1, 0, DIR_NONE, TRAN_NONE, POS_EXPECTED, 18209 0, 0, 0, st_get_erase_options }, 18210 { SCMD_MODE_SENSE, 18211 1, 1, 0, 0, 0, DIR_NONE, TRAN_READ, POS_EXPECTED, 18212 0, 0, 0 }, 18213 { SCMD_LOAD, 18214 1, 1, 1, 0, 0, DIR_EITH, TRAN_NONE, POS_EXPECTED, 18215 0, 0, 0, st_get_load_options }, 18216 { SCMD_GDIAG, 18217 1, 1, 0, 0, 0, DIR_NONE, TRAN_READ, POS_EXPECTED, 18218 1, 0, 0 }, 18219 { SCMD_SDIAG, 18220 1, 0, 1, 1, 0, DIR_EITH, TRAN_WRTE, POS_EXPECTED, 18221 1, 0, 0 }, 18222 { SCMD_DOORLOCK, 18223 0, 1, 0, 0, 0, DIR_NONE, TRAN_NONE, POS_EXPECTED, 18224 0, 4, 3 }, 18225 { SCMD_LOCATE, 18226 1, 1, 1, 0, 0, DIR_EITH, TRAN_NONE, POS_EXPECTED, 18227 0, 0, 0, NULL, st_get_cdb_g1_lba }, 18228 { SCMD_READ_POSITION, 18229 1, 1, 0, 0, 0, DIR_NONE, TRAN_READ, POS_EXPECTED, 18230 0, 0, 0 }, 18231 { SCMD_WRITE_BUFFER, 18232 1, 0, 0, 0, 0, DIR_NONE, TRAN_WRTE, POS_EXPECTED, 18233 1, 0, 0 }, 18234 { SCMD_READ_BUFFER, 18235 1, 0, 0, 0, 0, DIR_NONE, TRAN_READ, POS_EXPECTED, 18236 1, 0, 0 }, 18237 { SCMD_REPORT_DENSITIES, 18238 0, 1, 0, 0, 0, DIR_NONE, TRAN_READ, POS_EXPECTED, 18239 0, 0, 0 }, 18240 { SCMD_LOG_SELECT_G1, 18241 1, 1, 0, 0, 0, DIR_NONE, TRAN_WRTE, POS_EXPECTED, 18242 0, 0, 0 }, 18243 { SCMD_LOG_SENSE_G1, 18244 1, 1, 0, 0, 0, DIR_NONE, TRAN_READ, POS_EXPECTED, 18245 0, 0, 0 }, 18246 { SCMD_PRIN, 18247 0, 1, 0, 0, 0, DIR_NONE, TRAN_READ, POS_EXPECTED, 18248 0, 0, 0 }, 18249 { SCMD_PROUT, 18250 0, 1, 0, 0, 0, DIR_NONE, TRAN_WRTE, POS_EXPECTED, 18251 0, 0, 0 }, 18252 { SCMD_READ_ATTRIBUTE, 18253 1, 1, 0, 0, 0, DIR_NONE, TRAN_READ, POS_EXPECTED, 18254 0, 0, 0 }, 18255 { SCMD_WRITE_ATTRIBUTE, 18256 1, 1, 0, 0, 0, DIR_NONE, TRAN_WRTE, POS_EXPECTED, 18257 0, 0, 0 }, 18258 { SCMD_LOCATE_G4, 18259 1, 1, 1, 0, 0, DIR_EITH, TRAN_NONE, POS_EXPECTED, 18260 0, 0, 0, NULL, st_get_cdb_g4g5_cnt }, 18261 { SCMD_REPORT_LUNS, 18262 0, 1, 0, 0, 0, DIR_NONE, TRAN_READ, POS_EXPECTED, 18263 0, 0, 0 }, 18264 { SCMD_SVC_ACTION_IN_G5, 18265 1, 1, 0, 0, 0, DIR_NONE, TRAN_READ, POS_EXPECTED, 18266 0, 0, 0 }, 18267 { SCMD_MAINTENANCE_IN, 18268 1, 1, 0, 0, 0, DIR_NONE, TRAN_READ, POS_EXPECTED, 18269 0, 0, 0 }, 18270 { SCMD_MAINTENANCE_OUT, 18271 1, 1, 0, 0, 0, DIR_NONE, TRAN_WRTE, POS_EXPECTED, 18272 0, 0, 0 }, 18273 { 0xff, /* Default attribute for unsupported commands */ 18274 1, 0, 0, 0, 0, DIR_NONE, TRAN_NONE, POS_STARTING, 18275 1, 0, 0, NULL, NULL } 18276 }; 18277 18278 static const cmd_attribute * 18279 st_lookup_cmd_attribute(unsigned char cmd) 18280 { 18281 int i; 18282 cmd_attribute const *attribute; 18283 18284 for (i = 0; i < ST_NUM_MEMBERS(cmd_attributes); i++) { 18285 attribute = &cmd_attributes[i]; 18286 if (attribute->cmd == cmd) { 18287 return (attribute); 18288 } 18289 } 18290 ASSERT(attribute); 18291 return (attribute); 18292 } 18293 18294 static int 18295 st_reset(struct scsi_tape *un, int reset_type) 18296 { 18297 int rval; 18298 18299 ASSERT(MUTEX_HELD(&un->un_sd->sd_mutex)); 18300 18301 ST_FUNC(ST_DEVINFO, st_reset); 18302 un->un_rsvd_status |= ST_INITIATED_RESET; 18303 mutex_exit(ST_MUTEX); 18304 do { 18305 rval = scsi_reset(&un->un_sd->sd_address, reset_type); 18306 if (rval == 0) { 18307 switch (reset_type) { 18308 case RESET_LUN: 18309 ST_DEBUG3(ST_DEVINFO, st_label, CE_WARN, 18310 "LUN reset failed trying target reset"); 18311 reset_type = RESET_TARGET; 18312 break; 18313 case RESET_TARGET: 18314 ST_DEBUG3(ST_DEVINFO, st_label, CE_WARN, 18315 "target reset failed trying bus reset"); 18316 reset_type = RESET_BUS; 18317 break; 18318 case RESET_BUS: 18319 ST_DEBUG3(ST_DEVINFO, st_label, CE_WARN, 18320 "bus reset failed trying all reset"); 18321 reset_type = RESET_ALL; 18322 default: 18323 mutex_enter(ST_MUTEX); 18324 return (rval); 18325 } 18326 } 18327 } while (rval == 0); 18328 mutex_enter(ST_MUTEX); 18329 return (rval); 18330 } 18331 18332 18333 static void 18334 st_reset_notification(caddr_t arg) 18335 { 18336 struct scsi_tape *un = (struct scsi_tape *)arg; 18337 18338 ST_FUNC(ST_DEVINFO, st_reset_notification); 18339 mutex_enter(ST_MUTEX); 18340 18341 un->un_unit_attention_flags |= 2; 18342 if ((un->un_rsvd_status & (ST_RESERVE | ST_APPLICATION_RESERVATIONS)) == 18343 ST_RESERVE) { 18344 un->un_rsvd_status |= ST_LOST_RESERVE; 18345 ST_DEBUG2(ST_DEVINFO, st_label, CE_WARN, 18346 "Lost Reservation notification"); 18347 } else { 18348 ST_DEBUG2(ST_DEVINFO, st_label, CE_WARN, 18349 "reset notification"); 18350 } 18351 18352 if ((un->un_restore_pos == 0) && 18353 (un->un_state == ST_STATE_CLOSED) || 18354 (un->un_state == ST_STATE_OPEN_PENDING_IO) || 18355 (un->un_state == ST_STATE_CLOSING)) { 18356 un->un_restore_pos = 1; 18357 } 18358 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 18359 "reset and state was %d\n", un->un_state); 18360 mutex_exit(ST_MUTEX); 18361 } 18362