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 2009 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 static int st_set_target_TLR_mode(struct scsi_tape *un, ubufunc_t ubf); 650 static int st_make_sure_mode_data_is_correct(struct scsi_tape *un, 651 ubufunc_t ubf); 652 653 #ifdef __x86 654 /* 655 * routines for I/O in big block size 656 */ 657 static void st_release_contig_mem(struct scsi_tape *un, struct contig_mem *cp); 658 static struct contig_mem *st_get_contig_mem(struct scsi_tape *un, size_t len, 659 int alloc_flags); 660 static int st_bigblk_xfer_done(struct buf *bp); 661 static struct buf *st_get_bigblk_bp(struct buf *bp); 662 #endif 663 static void st_print_position(dev_info_t *dev, char *label, uint_t level, 664 const char *comment, tapepos_t *pos); 665 666 /* 667 * error statistics create/update functions 668 */ 669 static int st_create_errstats(struct scsi_tape *, int); 670 static int st_validate_tapemarks(struct scsi_tape *un, ubufunc_t ubf, 671 tapepos_t *pos); 672 673 #ifdef STDEBUG 674 static void st_debug_cmds(struct scsi_tape *un, int com, int count, int wait); 675 #endif /* STDEBUG */ 676 static char *st_dev_name(dev_t dev); 677 678 #if !defined(lint) 679 _NOTE(SCHEME_PROTECTS_DATA("unique per pkt", 680 scsi_pkt buf uio scsi_cdb uscsi_cmd)) 681 _NOTE(SCHEME_PROTECTS_DATA("unique per pkt", scsi_extended_sense scsi_status)) 682 _NOTE(SCHEME_PROTECTS_DATA("unique per pkt", recov_info)) 683 _NOTE(SCHEME_PROTECTS_DATA("stable data", scsi_device)) 684 _NOTE(DATA_READABLE_WITHOUT_LOCK(st_drivetype scsi_address)) 685 #endif 686 687 /* 688 * autoconfiguration routines. 689 */ 690 char _depends_on[] = "misc/scsi"; 691 692 static struct modldrv modldrv = { 693 &mod_driverops, /* Type of module. This one is a driver */ 694 "SCSI tape Driver", /* Name of the module. */ 695 &st_ops /* driver ops */ 696 }; 697 698 static struct modlinkage modlinkage = { 699 MODREV_1, &modldrv, NULL 700 }; 701 702 /* 703 * Notes on Post Reset Behavior in the tape driver: 704 * 705 * When the tape drive is opened, the driver attempts to make sure that 706 * the tape head is positioned exactly where it was left when it was last 707 * closed provided the medium is not changed. If the tape drive is 708 * opened in O_NDELAY mode, the repositioning (if necessary for any loss 709 * of position due to reset) will happen when the first tape operation or 710 * I/O occurs. The repositioning (if required) may not be possible under 711 * certain situations such as when the device firmware not able to report 712 * the medium change in the REQUEST SENSE data because of a reset or a 713 * misbehaving bus not allowing the reposition to happen. In such 714 * extraordinary situations, where the driver fails to position the head 715 * at its original position, it will fail the open the first time, to 716 * save the applications from overwriting the data. All further attempts 717 * to open the tape device will result in the driver attempting to load 718 * the tape at BOT (beginning of tape). Also a warning message to 719 * indicate that further attempts to open the tape device may result in 720 * the tape being loaded at BOT will be printed on the console. If the 721 * tape device is opened in O_NDELAY mode, failure to restore the 722 * original tape head position, will result in the failure of the first 723 * tape operation or I/O, Further, the driver will invalidate its 724 * internal tape position which will necessitate the applications to 725 * validate the position by using either a tape positioning ioctl (such 726 * as MTREW) or closing and reopening the tape device. 727 * 728 */ 729 730 int 731 _init(void) 732 { 733 int e; 734 735 if (((e = ddi_soft_state_init(&st_state, 736 sizeof (struct scsi_tape), ST_MAXUNIT)) != 0)) { 737 return (e); 738 } 739 740 if ((e = mod_install(&modlinkage)) != 0) { 741 ddi_soft_state_fini(&st_state); 742 } else { 743 #ifdef STDEBUG 744 mutex_init(&st_debug_mutex, NULL, MUTEX_DRIVER, NULL); 745 #endif 746 747 #if defined(__x86) 748 /* set the max physical address for iob allocs on x86 */ 749 st_alloc_attr.dma_attr_addr_hi = st_max_phys_addr; 750 751 /* 752 * set the sgllen for iob allocs on x86. If this is set less 753 * than the number of pages the buffer will take 754 * (taking into account alignment), it would force the 755 * allocator to try and allocate contiguous pages. 756 */ 757 st_alloc_attr.dma_attr_sgllen = st_sgl_size; 758 #endif 759 } 760 761 return (e); 762 } 763 764 int 765 _fini(void) 766 { 767 int e; 768 769 if ((e = mod_remove(&modlinkage)) != 0) { 770 return (e); 771 } 772 773 #ifdef STDEBUG 774 mutex_destroy(&st_debug_mutex); 775 #endif 776 777 ddi_soft_state_fini(&st_state); 778 779 return (e); 780 } 781 782 int 783 _info(struct modinfo *modinfop) 784 { 785 return (mod_info(&modlinkage, modinfop)); 786 } 787 788 789 static int 790 st_probe(dev_info_t *devi) 791 { 792 int instance; 793 struct scsi_device *devp; 794 int rval; 795 796 #if !defined(__sparc) 797 char *tape_prop; 798 int tape_prop_len; 799 #endif 800 801 ST_ENTR(devi, st_probe); 802 803 /* If self identifying device */ 804 if (ddi_dev_is_sid(devi) == DDI_SUCCESS) { 805 return (DDI_PROBE_DONTCARE); 806 } 807 808 #if !defined(__sparc) 809 /* 810 * Since some x86 HBAs have devnodes that look like SCSI as 811 * far as we can tell but aren't really SCSI (DADK, like mlx) 812 * we check for the presence of the "tape" property. 813 */ 814 if (ddi_prop_op(DDI_DEV_T_NONE, devi, PROP_LEN_AND_VAL_ALLOC, 815 DDI_PROP_CANSLEEP, "tape", 816 (caddr_t)&tape_prop, &tape_prop_len) != DDI_PROP_SUCCESS) { 817 return (DDI_PROBE_FAILURE); 818 } 819 if (strncmp(tape_prop, "sctp", tape_prop_len) != 0) { 820 kmem_free(tape_prop, tape_prop_len); 821 return (DDI_PROBE_FAILURE); 822 } 823 kmem_free(tape_prop, tape_prop_len); 824 #endif 825 826 devp = ddi_get_driver_private(devi); 827 instance = ddi_get_instance(devi); 828 829 if (ddi_get_soft_state(st_state, instance) != NULL) { 830 return (DDI_PROBE_PARTIAL); 831 } 832 833 834 /* 835 * Turn around and call probe routine to see whether 836 * we actually have a tape at this SCSI nexus. 837 */ 838 if (scsi_probe(devp, NULL_FUNC) == SCSIPROBE_EXISTS) { 839 840 /* 841 * In checking the whole inq_dtype byte we are looking at both 842 * the Peripheral Qualifier and the Peripheral Device Type. 843 * For this driver we are only interested in sequential devices 844 * that are connected or capable if connecting to this logical 845 * unit. 846 */ 847 if (devp->sd_inq->inq_dtype == 848 (DTYPE_SEQUENTIAL | DPQ_POSSIBLE)) { 849 ST_DEBUG6(devi, st_label, SCSI_DEBUG, 850 "probe exists\n"); 851 rval = DDI_PROBE_SUCCESS; 852 } else { 853 rval = DDI_PROBE_FAILURE; 854 } 855 } else { 856 ST_DEBUG6(devi, st_label, SCSI_DEBUG, 857 "probe failure: nothing there\n"); 858 rval = DDI_PROBE_FAILURE; 859 } 860 scsi_unprobe(devp); 861 return (rval); 862 } 863 864 static int 865 st_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 866 { 867 int instance; 868 int wide; 869 int dev_instance; 870 int ret_status; 871 struct scsi_device *devp; 872 int node_ix; 873 struct scsi_tape *un; 874 875 ST_ENTR(devi, st_attach); 876 877 devp = ddi_get_driver_private(devi); 878 instance = ddi_get_instance(devi); 879 880 switch (cmd) { 881 case DDI_ATTACH: 882 if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 883 "tape-command-recovery-disable", 0) != 0) { 884 st_recov_sz = sizeof (pkt_info); 885 } 886 if (st_doattach(devp, SLEEP_FUNC) == DDI_FAILURE) { 887 return (DDI_FAILURE); 888 } 889 break; 890 case DDI_RESUME: 891 /* 892 * Suspend/Resume 893 * 894 * When the driver suspended, there might be 895 * outstanding cmds and therefore we need to 896 * reset the suspended flag and resume the scsi 897 * watch thread and restart commands and timeouts 898 */ 899 900 if (!(un = ddi_get_soft_state(st_state, instance))) { 901 return (DDI_FAILURE); 902 } 903 dev_instance = ((un->un_dev == 0) ? MTMINOR(instance) : 904 un->un_dev); 905 906 mutex_enter(ST_MUTEX); 907 908 un->un_throttle = un->un_max_throttle; 909 un->un_tids_at_suspend = 0; 910 un->un_pwr_mgmt = ST_PWR_NORMAL; 911 912 if (un->un_swr_token) { 913 scsi_watch_resume(un->un_swr_token); 914 } 915 916 /* 917 * Restart timeouts 918 */ 919 if ((un->un_tids_at_suspend & ST_DELAY_TID) != 0) { 920 mutex_exit(ST_MUTEX); 921 un->un_delay_tid = timeout( 922 st_delayed_cv_broadcast, un, 923 drv_usectohz((clock_t) 924 MEDIA_ACCESS_DELAY)); 925 mutex_enter(ST_MUTEX); 926 } 927 928 if (un->un_tids_at_suspend & ST_HIB_TID) { 929 mutex_exit(ST_MUTEX); 930 un->un_hib_tid = timeout(st_intr_restart, un, 931 ST_STATUS_BUSY_TIMEOUT); 932 mutex_enter(ST_MUTEX); 933 } 934 935 ret_status = st_clear_unit_attentions(dev_instance, 5); 936 937 /* 938 * now check if we need to restore the tape position 939 */ 940 if ((un->un_suspend_pos.pmode != invalid) && 941 ((un->un_suspend_pos.fileno > 0) || 942 (un->un_suspend_pos.blkno > 0)) || 943 (un->un_suspend_pos.lgclblkno > 0)) { 944 if (ret_status != 0) { 945 /* 946 * tape didn't get good TUR 947 * just print out error messages 948 */ 949 scsi_log(ST_DEVINFO, st_label, CE_WARN, 950 "st_attach-RESUME: tape failure " 951 " tape position will be lost"); 952 } else { 953 /* this prints errors */ 954 (void) st_validate_tapemarks(un, 955 st_uscsi_cmd, &un->un_suspend_pos); 956 } 957 /* 958 * there are no retries, if there is an error 959 * we don't know if the tape has changed 960 */ 961 un->un_suspend_pos.pmode = invalid; 962 } 963 964 /* now we are ready to start up any queued I/Os */ 965 if (un->un_ncmds || un->un_quef) { 966 st_start(un); 967 } 968 969 cv_broadcast(&un->un_suspend_cv); 970 mutex_exit(ST_MUTEX); 971 return (DDI_SUCCESS); 972 973 default: 974 return (DDI_FAILURE); 975 } 976 977 un = ddi_get_soft_state(st_state, instance); 978 979 ST_DEBUG(devi, st_label, SCSI_DEBUG, 980 "st_attach: instance=%x\n", instance); 981 982 /* 983 * Add a zero-length attribute to tell the world we support 984 * kernel ioctls (for layered drivers) 985 */ 986 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP, 987 DDI_KERNEL_IOCTL, NULL, 0); 988 989 ddi_report_dev((dev_info_t *)devi); 990 991 /* 992 * If it's a SCSI-2 tape drive which supports wide, 993 * tell the host adapter to use wide. 994 */ 995 wide = ((devp->sd_inq->inq_rdf == RDF_SCSI2) && 996 (devp->sd_inq->inq_wbus16 || devp->sd_inq->inq_wbus32)) ? 1 : 0; 997 998 if (scsi_ifsetcap(ROUTE, "wide-xfer", wide, 1) == 1) { 999 ST_DEBUG(devi, st_label, SCSI_DEBUG, 1000 "Wide Transfer %s\n", wide ? "enabled" : "disabled"); 1001 } 1002 1003 /* 1004 * enable autorequest sense; keep the rq packet around in case 1005 * the autorequest sense fails because of a busy condition 1006 * do a getcap first in case the capability is not variable 1007 */ 1008 if (scsi_ifgetcap(ROUTE, "auto-rqsense", 1) == 1) { 1009 un->un_arq_enabled = 1; 1010 } else { 1011 un->un_arq_enabled = 1012 ((scsi_ifsetcap(ROUTE, "auto-rqsense", 1, 1) == 1) ? 1 : 0); 1013 } 1014 1015 ST_DEBUG(devi, st_label, SCSI_DEBUG, "auto request sense %s\n", 1016 (un->un_arq_enabled ? "enabled" : "disabled")); 1017 1018 un->un_untagged_qing = 1019 (scsi_ifgetcap(ROUTE, "untagged-qing", 0) == 1); 1020 1021 /* 1022 * XXX - This is just for 2.6. to tell users that write buffering 1023 * has gone away. 1024 */ 1025 if (un->un_arq_enabled && un->un_untagged_qing) { 1026 if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 1027 "tape-driver-buffering", 0) != 0) { 1028 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 1029 "Write Data Buffering has been depricated. Your " 1030 "applications should continue to work normally.\n" 1031 " But, they should ported to use Asynchronous " 1032 " I/O\n" 1033 " For more information, read about " 1034 " tape-driver-buffering " 1035 "property in the st(7d) man page\n"); 1036 } 1037 } 1038 1039 un->un_max_throttle = un->un_throttle = un->un_last_throttle = 1; 1040 un->un_flush_on_errors = 0; 1041 un->un_mkr_pkt = (struct scsi_pkt *)NULL; 1042 1043 ST_DEBUG(devi, st_label, SCSI_DEBUG, 1044 "throttle=%x, max_throttle = %x\n", 1045 un->un_throttle, un->un_max_throttle); 1046 1047 /* initialize persistent errors to nil */ 1048 un->un_persistence = 0; 1049 un->un_persist_errors = 0; 1050 1051 /* 1052 * Get dma-max from HBA driver. If it is not defined, use 64k 1053 */ 1054 un->un_maxdma = scsi_ifgetcap(&devp->sd_address, "dma-max", 1); 1055 if (un->un_maxdma == -1) { 1056 ST_DEBUG(devi, st_label, SCSI_DEBUG, 1057 "Received a value that looked like -1. Using 64k maxdma"); 1058 un->un_maxdma = (64 * ONE_K); 1059 } 1060 1061 #ifdef __x86 1062 /* 1063 * for x86, the device may be able to DMA more than the system will 1064 * allow under some circumstances. We need account for both the HBA's 1065 * and system's contraints. 1066 * 1067 * Get the maximum DMA under worse case conditions. e.g. looking at the 1068 * device constraints, the max copy buffer size, and the worse case 1069 * fragmentation. NOTE: this may differ from dma-max since dma-max 1070 * doesn't take the worse case framentation into account. 1071 * 1072 * e.g. a device may be able to DMA 16MBytes, but can only DMA 1MByte 1073 * if none of the pages are contiguous. Keeping track of both of these 1074 * values allows us to support larger tape block sizes on some devices. 1075 */ 1076 un->un_maxdma_arch = scsi_ifgetcap(&devp->sd_address, "dma-max-arch", 1077 1); 1078 1079 /* 1080 * If the dma-max-arch capability is not implemented, or the value 1081 * comes back higher than what was reported in dma-max, use dma-max. 1082 */ 1083 if ((un->un_maxdma_arch == -1) || 1084 ((uint_t)un->un_maxdma < (uint_t)un->un_maxdma_arch)) { 1085 un->un_maxdma_arch = un->un_maxdma; 1086 } 1087 #endif 1088 1089 /* 1090 * Get the max allowable cdb size 1091 */ 1092 un->un_max_cdb_sz = 1093 scsi_ifgetcap(&devp->sd_address, "max-cdb-length", 1); 1094 if (un->un_max_cdb_sz < CDB_GROUP0) { 1095 ST_DEBUG(devi, st_label, SCSI_DEBUG, 1096 "HBA reported max-cdb-length as %d\n", un->un_max_cdb_sz); 1097 un->un_max_cdb_sz = CDB_GROUP4; /* optimistic default */ 1098 } 1099 1100 if (strcmp(ddi_driver_name(ddi_get_parent(ST_DEVINFO)), "scsi_vhci")) { 1101 un->un_multipath = 0; 1102 } else { 1103 un->un_multipath = 1; 1104 } 1105 1106 un->un_maxbsize = MAXBSIZE_UNKNOWN; 1107 1108 un->un_mediastate = MTIO_NONE; 1109 un->un_HeadClean = TAPE_ALERT_SUPPORT_UNKNOWN; 1110 1111 /* 1112 * initialize kstats 1113 */ 1114 un->un_stats = kstat_create("st", instance, NULL, "tape", 1115 KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT); 1116 if (un->un_stats) { 1117 un->un_stats->ks_lock = ST_MUTEX; 1118 kstat_install(un->un_stats); 1119 } 1120 (void) st_create_errstats(un, instance); 1121 1122 /* 1123 * find the drive type for this target 1124 */ 1125 mutex_enter(ST_MUTEX); 1126 un->un_dev = MTMINOR(instance); 1127 st_known_tape_type(un); 1128 un->un_dev = 0; 1129 mutex_exit(ST_MUTEX); 1130 1131 for (node_ix = 0; node_ix < ST_NUM_MEMBERS(st_minor_data); node_ix++) { 1132 int minor; 1133 char *name; 1134 1135 name = st_minor_data[node_ix].name; 1136 minor = st_minor_data[node_ix].minor; 1137 1138 /* 1139 * For default devices set the density to the 1140 * preferred default density for this device. 1141 */ 1142 if (node_ix <= DEF_BSD_NR) { 1143 minor |= un->un_dp->default_density; 1144 } 1145 minor |= MTMINOR(instance); 1146 1147 if (ddi_create_minor_node(devi, name, S_IFCHR, minor, 1148 DDI_NT_TAPE, NULL) == DDI_SUCCESS) { 1149 continue; 1150 } 1151 1152 ddi_remove_minor_node(devi, NULL); 1153 1154 (void) scsi_reset_notify(ROUTE, SCSI_RESET_CANCEL, 1155 st_reset_notification, (caddr_t)un); 1156 cv_destroy(&un->un_clscv); 1157 cv_destroy(&un->un_sbuf_cv); 1158 cv_destroy(&un->un_queue_cv); 1159 cv_destroy(&un->un_state_cv); 1160 #ifdef __x86 1161 cv_destroy(&un->un_contig_mem_cv); 1162 #endif 1163 cv_destroy(&un->un_suspend_cv); 1164 cv_destroy(&un->un_tape_busy_cv); 1165 cv_destroy(&un->un_recov_buf_cv); 1166 if (un->un_recov_taskq) { 1167 ddi_taskq_destroy(un->un_recov_taskq); 1168 } 1169 if (un->un_sbufp) { 1170 freerbuf(un->un_sbufp); 1171 } 1172 if (un->un_recov_buf) { 1173 freerbuf(un->un_recov_buf); 1174 } 1175 if (un->un_uscsi_rqs_buf) { 1176 kmem_free(un->un_uscsi_rqs_buf, SENSE_LENGTH); 1177 } 1178 if (un->un_mspl) { 1179 i_ddi_mem_free((caddr_t)un->un_mspl, NULL); 1180 } 1181 if (un->un_dp_size) { 1182 kmem_free(un->un_dp, un->un_dp_size); 1183 } 1184 if (un->un_state) { 1185 kstat_delete(un->un_stats); 1186 } 1187 if (un->un_errstats) { 1188 kstat_delete(un->un_errstats); 1189 } 1190 1191 scsi_destroy_pkt(un->un_rqs); 1192 scsi_free_consistent_buf(un->un_rqs_bp); 1193 ddi_soft_state_free(st_state, instance); 1194 devp->sd_private = NULL; 1195 devp->sd_sense = NULL; 1196 1197 ddi_prop_remove_all(devi); 1198 return (DDI_FAILURE); 1199 } 1200 1201 return (DDI_SUCCESS); 1202 } 1203 1204 /* 1205 * st_detach: 1206 * 1207 * we allow a detach if and only if: 1208 * - no tape is currently inserted 1209 * - tape position is at BOT or unknown 1210 * (if it is not at BOT then a no rewind 1211 * device was opened and we have to preserve state) 1212 * - it must be in a closed state : no timeouts or scsi_watch requests 1213 * will exist if it is closed, so we don't need to check for 1214 * them here. 1215 */ 1216 /*ARGSUSED*/ 1217 static int 1218 st_detach(dev_info_t *devi, ddi_detach_cmd_t cmd) 1219 { 1220 int instance; 1221 int result; 1222 struct scsi_device *devp; 1223 struct scsi_tape *un; 1224 clock_t wait_cmds_complete; 1225 1226 ST_ENTR(devi, st_detach); 1227 1228 instance = ddi_get_instance(devi); 1229 1230 if (!(un = ddi_get_soft_state(st_state, instance))) { 1231 return (DDI_FAILURE); 1232 } 1233 1234 mutex_enter(ST_MUTEX); 1235 1236 /* 1237 * Clear error entry stack 1238 */ 1239 st_empty_error_stack(un); 1240 1241 mutex_exit(ST_MUTEX); 1242 1243 switch (cmd) { 1244 1245 case DDI_DETACH: 1246 /* 1247 * Undo what we did in st_attach & st_doattach, 1248 * freeing resources and removing things we installed. 1249 * The system framework guarantees we are not active 1250 * with this devinfo node in any other entry points at 1251 * this time. 1252 */ 1253 1254 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 1255 "st_detach: instance=%x, un=%p\n", instance, 1256 (void *)un); 1257 1258 if (((un->un_dp->options & ST_UNLOADABLE) == 0) || 1259 ((un->un_rsvd_status & ST_APPLICATION_RESERVATIONS) != 0) || 1260 (un->un_ncmds != 0) || (un->un_quef != NULL) || 1261 (un->un_state != ST_STATE_CLOSED)) { 1262 /* 1263 * we cannot unload some targets because the 1264 * inquiry returns junk unless immediately 1265 * after a reset 1266 */ 1267 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 1268 "cannot unload instance %x\n", instance); 1269 un->un_unit_attention_flags |= 4; 1270 return (DDI_FAILURE); 1271 } 1272 1273 /* 1274 * if the tape has been removed then we may unload; 1275 * do a test unit ready and if it returns NOT READY 1276 * then we assume that it is safe to unload. 1277 * as a side effect, pmode may be set to invalid if the 1278 * the test unit ready fails; 1279 * also un_state may be set to non-closed, so reset it 1280 */ 1281 if ((un->un_dev) && /* Been opened since attach */ 1282 ((un->un_pos.pmode == legacy) && 1283 (un->un_pos.fileno > 0) || /* Known position not rewound */ 1284 (un->un_pos.blkno != 0)) || /* Or within first file */ 1285 ((un->un_pos.pmode == logical) && 1286 (un->un_pos.lgclblkno > 0))) { 1287 mutex_enter(ST_MUTEX); 1288 /* 1289 * Send Test Unit Ready in the hopes that if 1290 * the drive is not in the state we think it is. 1291 * And the state will be changed so it can be detached. 1292 * If the command fails to reach the device and 1293 * the drive was not rewound or unloaded we want 1294 * to fail the detach till a user command fails 1295 * where after the detach will succead. 1296 */ 1297 result = st_cmd(un, SCMD_TEST_UNIT_READY, 0, SYNC_CMD); 1298 /* 1299 * After TUR un_state may be set to non-closed, 1300 * so reset it back. 1301 */ 1302 un->un_state = ST_STATE_CLOSED; 1303 mutex_exit(ST_MUTEX); 1304 } 1305 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 1306 "un_status=%x, fileno=%x, blkno=%x\n", 1307 un->un_status, un->un_pos.fileno, un->un_pos.blkno); 1308 1309 /* 1310 * check again: 1311 * if we are not at BOT then it is not safe to unload 1312 */ 1313 if ((un->un_dev) && /* Been opened since attach */ 1314 (result != EACCES) && /* drive is use by somebody */ 1315 ((((un->un_pos.pmode == legacy) && 1316 (un->un_pos.fileno > 0) || /* Known position not rewound */ 1317 (un->un_pos.blkno != 0)) || /* Or within first file */ 1318 ((un->un_pos.pmode == logical) && 1319 (un->un_pos.lgclblkno > 0))) && 1320 ((un->un_state == ST_STATE_CLOSED) && 1321 (un->un_laststate == ST_STATE_CLOSING)))) { 1322 1323 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 1324 "cannot detach: pmode=%d fileno=0x%x, blkno=0x%x" 1325 " lgclblkno=0x%"PRIx64"\n", un->un_pos.pmode, 1326 un->un_pos.fileno, un->un_pos.blkno, 1327 un->un_pos.lgclblkno); 1328 un->un_unit_attention_flags |= 4; 1329 return (DDI_FAILURE); 1330 } 1331 1332 /* 1333 * Just To make sure that we have released the 1334 * tape unit . 1335 */ 1336 if (un->un_dev && (un->un_rsvd_status & ST_RESERVE) && 1337 !DEVI_IS_DEVICE_REMOVED(devi)) { 1338 mutex_enter(ST_MUTEX); 1339 (void) st_reserve_release(un, ST_RELEASE, st_uscsi_cmd); 1340 mutex_exit(ST_MUTEX); 1341 } 1342 1343 /* 1344 * now remove other data structures allocated in st_doattach() 1345 */ 1346 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 1347 "destroying/freeing\n"); 1348 1349 (void) scsi_reset_notify(ROUTE, SCSI_RESET_CANCEL, 1350 st_reset_notification, (caddr_t)un); 1351 cv_destroy(&un->un_clscv); 1352 cv_destroy(&un->un_sbuf_cv); 1353 cv_destroy(&un->un_queue_cv); 1354 cv_destroy(&un->un_suspend_cv); 1355 cv_destroy(&un->un_tape_busy_cv); 1356 cv_destroy(&un->un_recov_buf_cv); 1357 1358 if (un->un_recov_taskq) { 1359 ddi_taskq_destroy(un->un_recov_taskq); 1360 } 1361 1362 if (un->un_hib_tid) { 1363 (void) untimeout(un->un_hib_tid); 1364 un->un_hib_tid = 0; 1365 } 1366 1367 if (un->un_delay_tid) { 1368 (void) untimeout(un->un_delay_tid); 1369 un->un_delay_tid = 0; 1370 } 1371 cv_destroy(&un->un_state_cv); 1372 1373 #ifdef __x86 1374 cv_destroy(&un->un_contig_mem_cv); 1375 1376 if (un->un_contig_mem_hdl != NULL) { 1377 ddi_dma_free_handle(&un->un_contig_mem_hdl); 1378 } 1379 #endif 1380 if (un->un_sbufp) { 1381 freerbuf(un->un_sbufp); 1382 } 1383 if (un->un_recov_buf) { 1384 freerbuf(un->un_recov_buf); 1385 } 1386 if (un->un_uscsi_rqs_buf) { 1387 kmem_free(un->un_uscsi_rqs_buf, SENSE_LENGTH); 1388 } 1389 if (un->un_mspl) { 1390 i_ddi_mem_free((caddr_t)un->un_mspl, NULL); 1391 } 1392 if (un->un_rqs) { 1393 scsi_destroy_pkt(un->un_rqs); 1394 scsi_free_consistent_buf(un->un_rqs_bp); 1395 } 1396 if (un->un_mkr_pkt) { 1397 scsi_destroy_pkt(un->un_mkr_pkt); 1398 } 1399 if (un->un_arq_enabled) { 1400 (void) scsi_ifsetcap(ROUTE, "auto-rqsense", 0, 1); 1401 } 1402 if (un->un_dp_size) { 1403 kmem_free(un->un_dp, un->un_dp_size); 1404 } 1405 if (un->un_stats) { 1406 kstat_delete(un->un_stats); 1407 un->un_stats = (kstat_t *)0; 1408 } 1409 if (un->un_errstats) { 1410 kstat_delete(un->un_errstats); 1411 un->un_errstats = (kstat_t *)0; 1412 } 1413 if (un->un_media_id_len) { 1414 kmem_free(un->un_media_id, un->un_media_id_len); 1415 } 1416 devp = ST_SCSI_DEVP; 1417 ddi_soft_state_free(st_state, instance); 1418 devp->sd_private = NULL; 1419 devp->sd_sense = NULL; 1420 scsi_unprobe(devp); 1421 ddi_prop_remove_all(devi); 1422 ddi_remove_minor_node(devi, NULL); 1423 ST_DEBUG(0, st_label, SCSI_DEBUG, "st_detach done\n"); 1424 return (DDI_SUCCESS); 1425 1426 case DDI_SUSPEND: 1427 1428 /* 1429 * Suspend/Resume 1430 * 1431 * To process DDI_SUSPEND, we must do the following: 1432 * 1433 * - check ddi_removing_power to see if power will be turned 1434 * off. if so, return DDI_FAILURE 1435 * - check if we are already suspended, 1436 * if so, return DDI_FAILURE 1437 * - check if device state is CLOSED, 1438 * if not, return DDI_FAILURE. 1439 * - wait until outstanding operations complete 1440 * - save tape state 1441 * - block new operations 1442 * - cancel pending timeouts 1443 * 1444 */ 1445 1446 if (ddi_removing_power(devi)) { 1447 return (DDI_FAILURE); 1448 } 1449 mutex_enter(ST_MUTEX); 1450 1451 /* 1452 * Shouldn't already be suspended, if so return failure 1453 */ 1454 if (un->un_pwr_mgmt == ST_PWR_SUSPENDED) { 1455 mutex_exit(ST_MUTEX); 1456 return (DDI_FAILURE); 1457 } 1458 if (un->un_state != ST_STATE_CLOSED) { 1459 mutex_exit(ST_MUTEX); 1460 return (DDI_FAILURE); 1461 } 1462 1463 /* 1464 * Wait for all outstanding I/O's to complete 1465 * 1466 * we wait on both ncmds and the wait queue for times 1467 * when we are flushing after persistent errors are 1468 * flagged, which is when ncmds can be 0, and the 1469 * queue can still have I/O's. This way we preserve 1470 * order of biodone's. 1471 */ 1472 wait_cmds_complete = ddi_get_lbolt(); 1473 wait_cmds_complete += 1474 st_wait_cmds_complete * drv_usectohz(1000000); 1475 while (un->un_ncmds || un->un_quef || 1476 (un->un_state == ST_STATE_RESOURCE_WAIT)) { 1477 1478 if (cv_timedwait(&un->un_tape_busy_cv, ST_MUTEX, 1479 wait_cmds_complete) == -1) { 1480 /* 1481 * Time expired then cancel the command 1482 */ 1483 if (st_reset(un, RESET_LUN) == 0) { 1484 if (un->un_last_throttle) { 1485 un->un_throttle = 1486 un->un_last_throttle; 1487 } 1488 mutex_exit(ST_MUTEX); 1489 return (DDI_FAILURE); 1490 } else { 1491 break; 1492 } 1493 } 1494 } 1495 1496 /* 1497 * DDI_SUSPEND says that the system "may" power down, we 1498 * remember the file and block number before rewinding. 1499 * we also need to save state before issuing 1500 * any WRITE_FILE_MARK command. 1501 */ 1502 (void) st_update_block_pos(un, st_cmd, 0); 1503 COPY_POS(&un->un_suspend_pos, &un->un_pos); 1504 1505 1506 /* 1507 * Issue a zero write file fmk command to tell the drive to 1508 * flush any buffered tape marks 1509 */ 1510 (void) st_cmd(un, SCMD_WRITE_FILE_MARK, 0, SYNC_CMD); 1511 1512 /* 1513 * Because not all tape drives correctly implement buffer 1514 * flushing with the zero write file fmk command, issue a 1515 * synchronous rewind command to force data flushing. 1516 * st_validate_tapemarks() will do a rewind during DDI_RESUME 1517 * anyway. 1518 */ 1519 (void) st_cmd(un, SCMD_REWIND, 0, SYNC_CMD); 1520 1521 /* stop any new operations */ 1522 un->un_pwr_mgmt = ST_PWR_SUSPENDED; 1523 un->un_throttle = 0; 1524 1525 /* 1526 * cancel any outstanding timeouts 1527 */ 1528 if (un->un_delay_tid) { 1529 timeout_id_t temp_id = un->un_delay_tid; 1530 un->un_delay_tid = 0; 1531 un->un_tids_at_suspend |= ST_DELAY_TID; 1532 mutex_exit(ST_MUTEX); 1533 (void) untimeout(temp_id); 1534 mutex_enter(ST_MUTEX); 1535 } 1536 1537 if (un->un_hib_tid) { 1538 timeout_id_t temp_id = un->un_hib_tid; 1539 un->un_hib_tid = 0; 1540 un->un_tids_at_suspend |= ST_HIB_TID; 1541 mutex_exit(ST_MUTEX); 1542 (void) untimeout(temp_id); 1543 mutex_enter(ST_MUTEX); 1544 } 1545 1546 /* 1547 * Suspend the scsi_watch_thread 1548 */ 1549 if (un->un_swr_token) { 1550 opaque_t temp_token = un->un_swr_token; 1551 mutex_exit(ST_MUTEX); 1552 scsi_watch_suspend(temp_token); 1553 } else { 1554 mutex_exit(ST_MUTEX); 1555 } 1556 1557 return (DDI_SUCCESS); 1558 1559 default: 1560 ST_DEBUG(0, st_label, SCSI_DEBUG, "st_detach failed\n"); 1561 return (DDI_FAILURE); 1562 } 1563 } 1564 1565 1566 /* ARGSUSED */ 1567 static int 1568 st_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 1569 { 1570 dev_t dev; 1571 struct scsi_tape *un; 1572 int instance, error; 1573 1574 ST_ENTR(dip, st_info); 1575 1576 switch (infocmd) { 1577 case DDI_INFO_DEVT2DEVINFO: 1578 dev = (dev_t)arg; 1579 instance = MTUNIT(dev); 1580 if ((un = ddi_get_soft_state(st_state, instance)) == NULL) 1581 return (DDI_FAILURE); 1582 *result = (void *) ST_DEVINFO; 1583 error = DDI_SUCCESS; 1584 break; 1585 case DDI_INFO_DEVT2INSTANCE: 1586 dev = (dev_t)arg; 1587 instance = MTUNIT(dev); 1588 *result = (void *)(uintptr_t)instance; 1589 error = DDI_SUCCESS; 1590 break; 1591 default: 1592 error = DDI_FAILURE; 1593 } 1594 return (error); 1595 } 1596 1597 static int 1598 st_doattach(struct scsi_device *devp, int (*canwait)()) 1599 { 1600 struct scsi_tape *un = NULL; 1601 recov_info *ri; 1602 int km_flags = (canwait != NULL_FUNC) ? KM_SLEEP : KM_NOSLEEP; 1603 int instance; 1604 size_t rlen; 1605 1606 ST_FUNC(devp->sd_dev, st_doattach); 1607 /* 1608 * Call the routine scsi_probe to do some of the dirty work. 1609 * If the INQUIRY command succeeds, the field sd_inq in the 1610 * device structure will be filled in. 1611 */ 1612 ST_DEBUG(devp->sd_dev, st_label, SCSI_DEBUG, 1613 "st_doattach(): probing\n"); 1614 1615 if (scsi_probe(devp, canwait) == SCSIPROBE_EXISTS) { 1616 1617 /* 1618 * In checking the whole inq_dtype byte we are looking at both 1619 * the Peripheral Qualifier and the Peripheral Device Type. 1620 * For this driver we are only interested in sequential devices 1621 * that are connected or capable if connecting to this logical 1622 * unit. 1623 */ 1624 if (devp->sd_inq->inq_dtype == 1625 (DTYPE_SEQUENTIAL | DPQ_POSSIBLE)) { 1626 ST_DEBUG(devp->sd_dev, st_label, SCSI_DEBUG, 1627 "probe exists\n"); 1628 } else { 1629 /* Something there but not a tape device */ 1630 scsi_unprobe(devp); 1631 return (DDI_FAILURE); 1632 } 1633 } else { 1634 /* Nothing there */ 1635 ST_DEBUG(devp->sd_dev, st_label, SCSI_DEBUG, 1636 "probe failure: nothing there\n"); 1637 scsi_unprobe(devp); 1638 return (DDI_FAILURE); 1639 } 1640 1641 1642 /* 1643 * The actual unit is present. 1644 * Now is the time to fill in the rest of our info.. 1645 */ 1646 instance = ddi_get_instance(devp->sd_dev); 1647 1648 if (ddi_soft_state_zalloc(st_state, instance) != DDI_SUCCESS) { 1649 goto error; 1650 } 1651 un = ddi_get_soft_state(st_state, instance); 1652 1653 ASSERT(un != NULL); 1654 1655 un->un_rqs_bp = scsi_alloc_consistent_buf(&devp->sd_address, NULL, 1656 MAX_SENSE_LENGTH, B_READ, canwait, NULL); 1657 if (un->un_rqs_bp == NULL) { 1658 goto error; 1659 } 1660 un->un_rqs = scsi_init_pkt(&devp->sd_address, NULL, un->un_rqs_bp, 1661 CDB_GROUP0, 1, st_recov_sz, PKT_CONSISTENT, canwait, NULL); 1662 if (!un->un_rqs) { 1663 goto error; 1664 } 1665 ASSERT(un->un_rqs->pkt_resid == 0); 1666 devp->sd_sense = 1667 (struct scsi_extended_sense *)un->un_rqs_bp->b_un.b_addr; 1668 ASSERT(geterror(un->un_rqs_bp) == NULL); 1669 1670 (void) scsi_setup_cdb((union scsi_cdb *)un->un_rqs->pkt_cdbp, 1671 SCMD_REQUEST_SENSE, 0, MAX_SENSE_LENGTH, 0); 1672 FILL_SCSI1_LUN(devp, un->un_rqs); 1673 un->un_rqs->pkt_flags |= (FLAG_SENSING | FLAG_HEAD | FLAG_NODISCON); 1674 un->un_rqs->pkt_time = st_io_time; 1675 un->un_rqs->pkt_comp = st_intr; 1676 ri = (recov_info *)un->un_rqs->pkt_private; 1677 if (st_recov_sz == sizeof (recov_info)) { 1678 ri->privatelen = sizeof (recov_info); 1679 } else { 1680 ri->privatelen = sizeof (pkt_info); 1681 } 1682 1683 un->un_sbufp = getrbuf(km_flags); 1684 un->un_recov_buf = getrbuf(km_flags); 1685 1686 un->un_uscsi_rqs_buf = kmem_alloc(SENSE_LENGTH, KM_SLEEP); 1687 1688 /* 1689 * use i_ddi_mem_alloc() for now until we have an interface to allocate 1690 * memory for DMA which doesn't require a DMA handle. ddi_iopb_alloc() 1691 * is obsolete and we want more flexibility in controlling the DMA 1692 * address constraints. 1693 */ 1694 (void) i_ddi_mem_alloc(devp->sd_dev, &st_alloc_attr, 1695 sizeof (struct seq_mode), ((km_flags == KM_SLEEP) ? 1 : 0), 0, 1696 NULL, (caddr_t *)&un->un_mspl, &rlen, NULL); 1697 1698 (void) i_ddi_mem_alloc(devp->sd_dev, &st_alloc_attr, 1699 sizeof (read_pos_data_t), ((km_flags == KM_SLEEP) ? 1 : 0), 0, 1700 NULL, (caddr_t *)&un->un_read_pos_data, &rlen, NULL); 1701 1702 if (!un->un_sbufp || !un->un_mspl || !un->un_read_pos_data) { 1703 ST_DEBUG6(devp->sd_dev, st_label, SCSI_DEBUG, 1704 "probe partial failure: no space\n"); 1705 goto error; 1706 } 1707 1708 bzero(un->un_mspl, sizeof (struct seq_mode)); 1709 1710 cv_init(&un->un_sbuf_cv, NULL, CV_DRIVER, NULL); 1711 cv_init(&un->un_queue_cv, NULL, CV_DRIVER, NULL); 1712 cv_init(&un->un_clscv, NULL, CV_DRIVER, NULL); 1713 cv_init(&un->un_state_cv, NULL, CV_DRIVER, NULL); 1714 #ifdef __x86 1715 cv_init(&un->un_contig_mem_cv, NULL, CV_DRIVER, NULL); 1716 #endif 1717 1718 /* Initialize power managemnet condition variable */ 1719 cv_init(&un->un_suspend_cv, NULL, CV_DRIVER, NULL); 1720 cv_init(&un->un_tape_busy_cv, NULL, CV_DRIVER, NULL); 1721 cv_init(&un->un_recov_buf_cv, NULL, CV_DRIVER, NULL); 1722 1723 un->un_recov_taskq = ddi_taskq_create(devp->sd_dev, 1724 "un_recov_taskq", 1, TASKQ_DEFAULTPRI, km_flags); 1725 1726 ASSERT(un->un_recov_taskq != NULL); 1727 1728 un->un_pos.pmode = invalid; 1729 un->un_sd = devp; 1730 un->un_swr_token = (opaque_t)NULL; 1731 un->un_comp_page = ST_DEV_DATACOMP_PAGE | ST_DEV_CONFIG_PAGE; 1732 un->un_wormable = st_is_drive_worm; 1733 un->un_media_id_method = st_get_media_identification; 1734 /* 1735 * setting long a initial as it contains logical file info. 1736 * support for long format is mandatory but many drive don't do it. 1737 */ 1738 un->un_read_pos_type = LONG_POS; 1739 1740 un->un_suspend_pos.pmode = invalid; 1741 1742 st_add_recovery_info_to_pkt(un, un->un_rqs_bp, un->un_rqs); 1743 1744 #ifdef __x86 1745 if (ddi_dma_alloc_handle(ST_DEVINFO, &st_contig_mem_dma_attr, 1746 DDI_DMA_SLEEP, NULL, &un->un_contig_mem_hdl) != DDI_SUCCESS) { 1747 ST_DEBUG6(devp->sd_dev, st_label, SCSI_DEBUG, 1748 "allocation of contiguous memory dma handle failed!"); 1749 un->un_contig_mem_hdl = NULL; 1750 goto error; 1751 } 1752 #endif 1753 1754 /* 1755 * Since this driver manages devices with "remote" hardware, 1756 * i.e. the devices themselves have no "reg" properties, 1757 * the SUSPEND/RESUME commands in detach/attach will not be 1758 * called by the power management framework unless we request 1759 * it by creating a "pm-hardware-state" property and setting it 1760 * to value "needs-suspend-resume". 1761 */ 1762 if (ddi_prop_update_string(DDI_DEV_T_NONE, devp->sd_dev, 1763 "pm-hardware-state", "needs-suspend-resume") != 1764 DDI_PROP_SUCCESS) { 1765 1766 ST_DEBUG(devp->sd_dev, st_label, SCSI_DEBUG, 1767 "ddi_prop_update(\"pm-hardware-state\") failed\n"); 1768 goto error; 1769 } 1770 1771 if (ddi_prop_create(DDI_DEV_T_NONE, devp->sd_dev, DDI_PROP_CANSLEEP, 1772 "no-involuntary-power-cycles", NULL, 0) != DDI_PROP_SUCCESS) { 1773 1774 ST_DEBUG(devp->sd_dev, st_label, SCSI_DEBUG, 1775 "ddi_prop_create(\"no-involuntary-power-cycles\") " 1776 "failed\n"); 1777 goto error; 1778 } 1779 1780 (void) scsi_reset_notify(ROUTE, SCSI_RESET_NOTIFY, 1781 st_reset_notification, (caddr_t)un); 1782 1783 ST_DEBUG6(devp->sd_dev, st_label, SCSI_DEBUG, "attach success\n"); 1784 return (DDI_SUCCESS); 1785 1786 error: 1787 devp->sd_sense = NULL; 1788 1789 ddi_remove_minor_node(devp->sd_dev, NULL); 1790 if (un) { 1791 if (un->un_mspl) { 1792 i_ddi_mem_free((caddr_t)un->un_mspl, NULL); 1793 } 1794 if (un->un_read_pos_data) { 1795 i_ddi_mem_free((caddr_t)un->un_read_pos_data, 0); 1796 } 1797 if (un->un_sbufp) { 1798 freerbuf(un->un_sbufp); 1799 } 1800 if (un->un_recov_buf) { 1801 freerbuf(un->un_recov_buf); 1802 } 1803 if (un->un_uscsi_rqs_buf) { 1804 kmem_free(un->un_uscsi_rqs_buf, SENSE_LENGTH); 1805 } 1806 #ifdef __x86 1807 if (un->un_contig_mem_hdl != NULL) { 1808 ddi_dma_free_handle(&un->un_contig_mem_hdl); 1809 } 1810 #endif 1811 if (un->un_rqs) { 1812 scsi_destroy_pkt(un->un_rqs); 1813 } 1814 1815 if (un->un_rqs_bp) { 1816 scsi_free_consistent_buf(un->un_rqs_bp); 1817 } 1818 1819 ddi_soft_state_free(st_state, instance); 1820 devp->sd_private = NULL; 1821 } 1822 1823 if (devp->sd_inq) { 1824 scsi_unprobe(devp); 1825 } 1826 return (DDI_FAILURE); 1827 } 1828 1829 typedef int 1830 (*cfg_functp)(struct scsi_tape *, char *vidpid, struct st_drivetype *); 1831 1832 static cfg_functp config_functs[] = { 1833 st_get_conf_from_st_dot_conf, 1834 st_get_conf_from_st_conf_dot_c, 1835 st_get_conf_from_tape_drive, 1836 st_get_default_conf 1837 }; 1838 1839 1840 /* 1841 * determine tape type, using tape-config-list or built-in table or 1842 * use a generic tape config entry 1843 */ 1844 static void 1845 st_known_tape_type(struct scsi_tape *un) 1846 { 1847 struct st_drivetype *dp; 1848 cfg_functp *config_funct; 1849 uchar_t reserved; 1850 1851 ST_FUNC(ST_DEVINFO, st_known_tape_type); 1852 1853 reserved = (un->un_rsvd_status & ST_RESERVE) ? ST_RESERVE 1854 : ST_RELEASE; 1855 1856 /* 1857 * XXX: Emulex MT-02 (and emulators) predates SCSI-1 and has 1858 * no vid & pid inquiry data. So, we provide one. 1859 */ 1860 if (ST_INQUIRY->inq_len == 0 || 1861 (bcmp("\0\0\0\0\0\0\0\0", ST_INQUIRY->inq_vid, 8) == 0)) { 1862 (void) strcpy((char *)ST_INQUIRY->inq_vid, ST_MT02_NAME); 1863 } 1864 1865 if (un->un_dp_size == 0) { 1866 un->un_dp_size = sizeof (struct st_drivetype); 1867 dp = kmem_zalloc((size_t)un->un_dp_size, KM_SLEEP); 1868 un->un_dp = dp; 1869 } else { 1870 dp = un->un_dp; 1871 } 1872 1873 un->un_dp->non_motion_timeout = st_io_time; 1874 /* 1875 * Loop through the configuration methods till one works. 1876 */ 1877 for (config_funct = &config_functs[0]; ; config_funct++) { 1878 if ((*config_funct)(un, ST_INQUIRY->inq_vid, dp)) { 1879 break; 1880 } 1881 } 1882 1883 /* 1884 * If we didn't just make up this configuration and 1885 * all the density codes are the same.. 1886 * Set Auto Density over ride. 1887 */ 1888 if (*config_funct != st_get_default_conf) { 1889 /* 1890 * If this device is one that is configured and all 1891 * densities are the same, This saves doing gets and set 1892 * that yield nothing. 1893 */ 1894 if ((dp->densities[0]) == (dp->densities[1]) && 1895 (dp->densities[0]) == (dp->densities[2]) && 1896 (dp->densities[0]) == (dp->densities[3])) { 1897 1898 dp->options |= ST_AUTODEN_OVERRIDE; 1899 } 1900 } 1901 1902 1903 /* 1904 * Store tape drive characteristics. 1905 */ 1906 un->un_status = 0; 1907 un->un_attached = 1; 1908 un->un_init_options = dp->options; 1909 1910 /* setup operation time-outs based on options */ 1911 st_calculate_timeouts(un); 1912 1913 /* TLR support */ 1914 if (un->un_dp->type != ST_TYPE_INVALID) { 1915 int result; 1916 1917 /* try and enable TLR */ 1918 un->un_tlr_flag = TLR_SAS_ONE_DEVICE; 1919 result = st_set_target_TLR_mode(un, st_uscsi_cmd); 1920 if (result == EACCES) { 1921 /* 1922 * From attach command failed. 1923 * Set dp type so is run again on open. 1924 */ 1925 un->un_dp->type = ST_TYPE_INVALID; 1926 un->un_tlr_flag = TLR_NOT_KNOWN; 1927 } else if (result == 0) { 1928 if (scsi_ifgetcap(&un->un_sd->sd_address, 1929 "tran-layer-retries", 1) == -1) { 1930 un->un_tlr_flag = TLR_NOT_SUPPORTED; 1931 (void) st_set_target_TLR_mode(un, st_uscsi_cmd); 1932 } else { 1933 un->un_tlr_flag = TLR_SAS_ONE_DEVICE; 1934 } 1935 } else { 1936 un->un_tlr_flag = TLR_NOT_SUPPORTED; 1937 } 1938 } 1939 1940 /* make sure if we are supposed to be variable, make it variable */ 1941 if (dp->options & ST_VARIABLE) { 1942 dp->bsize = 0; 1943 } 1944 1945 if (reserved != ((un->un_rsvd_status & ST_RESERVE) ? ST_RESERVE 1946 : ST_RELEASE)) { 1947 (void) st_reserve_release(un, reserved, st_uscsi_cmd); 1948 } 1949 1950 un->un_unit_attention_flags |= 1; 1951 1952 scsi_log(ST_DEVINFO, st_label, CE_NOTE, "?<%s>\n", dp->name); 1953 1954 } 1955 1956 1957 typedef struct { 1958 int mask; 1959 int bottom; 1960 int top; 1961 char *name; 1962 } conf_limit; 1963 1964 static const conf_limit conf_limits[] = { 1965 1966 -1, 1, 2, "conf version", 1967 -1, MT_ISTS, ST_LAST_TYPE, "drive type", 1968 -1, 0, 0xffffff, "block size", 1969 ST_VALID_OPTS, 0, ST_VALID_OPTS, "options", 1970 -1, 0, 4, "number of densities", 1971 -1, 0, UINT8_MAX, "density code", 1972 -1, 0, 3, "default density", 1973 -1, 0, UINT16_MAX, "non motion timeout", 1974 -1, 0, UINT16_MAX, "I/O timeout", 1975 -1, 0, UINT16_MAX, "space timeout", 1976 -1, 0, UINT16_MAX, "load timeout", 1977 -1, 0, UINT16_MAX, "unload timeout", 1978 -1, 0, UINT16_MAX, "erase timeout", 1979 0, 0, 0, NULL 1980 }; 1981 1982 static int 1983 st_validate_conf_data(struct scsi_tape *un, int *list, int list_len, 1984 const char *conf_name) 1985 { 1986 int dens; 1987 int ndens; 1988 int value; 1989 int type; 1990 int count; 1991 const conf_limit *limit = &conf_limits[0]; 1992 1993 ST_FUNC(ST_DEVINFO, st_validate_conf_data); 1994 1995 ST_DEBUG3(ST_DEVINFO, st_label, CE_NOTE, 1996 "Checking %d entrys total with %d densities\n", list_len, list[4]); 1997 1998 count = list_len; 1999 type = *list; 2000 for (; count && limit->name; count--, list++, limit++) { 2001 2002 value = *list; 2003 if (value & ~limit->mask) { 2004 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 2005 "%s %s value invalid bits set: 0x%X\n", 2006 conf_name, limit->name, value & ~limit->mask); 2007 *list &= limit->mask; 2008 } else if (value < limit->bottom) { 2009 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 2010 "%s %s value too low: value = %d limit %d\n", 2011 conf_name, limit->name, value, limit->bottom); 2012 } else if (value > limit->top) { 2013 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 2014 "%s %s value too high: value = %d limit %d\n", 2015 conf_name, limit->name, value, limit->top); 2016 } else { 2017 ST_DEBUG3(ST_DEVINFO, st_label, CE_CONT, 2018 "%s %s value = 0x%X\n", 2019 conf_name, limit->name, value); 2020 } 2021 2022 /* If not the number of densities continue */ 2023 if (limit != &conf_limits[4]) { 2024 continue; 2025 } 2026 2027 /* If number of densities is not in range can't use config */ 2028 if (value < limit->bottom || value > limit->top) { 2029 return (-1); 2030 } 2031 2032 ndens = min(value, NDENSITIES); 2033 if ((type == 1) && (list_len - ndens) != 6) { 2034 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 2035 "%s conf version 1 with %d densities has %d items" 2036 " should have %d", 2037 conf_name, ndens, list_len, 6 + ndens); 2038 } else if ((type == 2) && (list_len - ndens) != 13) { 2039 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 2040 "%s conf version 2 with %d densities has %d items" 2041 " should have %d", 2042 conf_name, ndens, list_len, 13 + ndens); 2043 } 2044 2045 limit++; 2046 for (dens = 0; dens < ndens && count; dens++) { 2047 count--; 2048 list++; 2049 value = *list; 2050 if (value < limit->bottom) { 2051 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 2052 "%s density[%d] value too low: value =" 2053 " 0x%X limit 0x%X\n", 2054 conf_name, dens, value, limit->bottom); 2055 } else if (value > limit->top) { 2056 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 2057 "%s density[%d] value too high: value =" 2058 " 0x%X limit 0x%X\n", 2059 conf_name, dens, value, limit->top); 2060 } else { 2061 ST_DEBUG3(ST_DEVINFO, st_label, CE_CONT, 2062 "%s density[%d] value = 0x%X\n", 2063 conf_name, dens, value); 2064 } 2065 } 2066 } 2067 2068 return (0); 2069 } 2070 2071 static int 2072 st_get_conf_from_st_dot_conf(struct scsi_tape *un, char *vidpid, 2073 struct st_drivetype *dp) 2074 { 2075 caddr_t config_list = NULL; 2076 caddr_t data_list = NULL; 2077 int *data_ptr; 2078 caddr_t vidptr, prettyptr, datanameptr; 2079 size_t vidlen, prettylen, datanamelen, tripletlen = 0; 2080 int config_list_len, data_list_len, len, i; 2081 int version; 2082 int found = 0; 2083 2084 ST_FUNC(ST_DEVINFO, st_get_conf_from_st_dot_conf); 2085 2086 /* 2087 * Determine type of tape controller. Type is determined by 2088 * checking the vendor ids of the earlier inquiry command and 2089 * comparing those with vids in tape-config-list defined in st.conf 2090 */ 2091 if (ddi_getlongprop(DDI_DEV_T_ANY, ST_DEVINFO, DDI_PROP_DONTPASS, 2092 "tape-config-list", (caddr_t)&config_list, &config_list_len) 2093 != DDI_PROP_SUCCESS) { 2094 return (found); 2095 } 2096 2097 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 2098 "st_get_conf_from_st_dot_conf(): st.conf has tape-config-list\n"); 2099 2100 /* 2101 * Compare vids in each triplet - if it matches, get value for 2102 * data_name and contruct a st_drivetype struct 2103 * tripletlen is not set yet! 2104 */ 2105 for (len = config_list_len, vidptr = config_list; 2106 len > 0; 2107 vidptr += tripletlen, len -= tripletlen) { 2108 2109 vidlen = strlen(vidptr); 2110 prettyptr = vidptr + vidlen + 1; 2111 prettylen = strlen(prettyptr); 2112 datanameptr = prettyptr + prettylen + 1; 2113 datanamelen = strlen(datanameptr); 2114 tripletlen = vidlen + prettylen + datanamelen + 3; 2115 2116 if (vidlen == 0) { 2117 continue; 2118 } 2119 2120 /* 2121 * If inquiry vid dosen't match this triplets vid, 2122 * try the next. 2123 */ 2124 if (strncasecmp(vidpid, vidptr, vidlen)) { 2125 continue; 2126 } 2127 2128 /* 2129 * if prettylen is zero then use the vid string 2130 */ 2131 if (prettylen == 0) { 2132 prettyptr = vidptr; 2133 prettylen = vidlen; 2134 } 2135 2136 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 2137 "vid = %s, pretty=%s, dataname = %s\n", 2138 vidptr, prettyptr, datanameptr); 2139 2140 /* 2141 * get the data list 2142 */ 2143 if (ddi_getlongprop(DDI_DEV_T_ANY, ST_DEVINFO, 0, 2144 datanameptr, (caddr_t)&data_list, 2145 &data_list_len) != DDI_PROP_SUCCESS) { 2146 /* 2147 * Error in getting property value 2148 * print warning! 2149 */ 2150 scsi_log(ST_DEVINFO, st_label, CE_WARN, 2151 "data property (%s) has no value\n", 2152 datanameptr); 2153 continue; 2154 } 2155 2156 /* 2157 * now initialize the st_drivetype struct 2158 */ 2159 (void) strncpy(dp->name, prettyptr, ST_NAMESIZE - 1); 2160 dp->length = (int)min(vidlen, (VIDPIDLEN - 1)); 2161 (void) strncpy(dp->vid, vidptr, dp->length); 2162 data_ptr = (int *)data_list; 2163 /* 2164 * check if data is enough for version, type, 2165 * bsize, options, # of densities, density1, 2166 * density2, ..., default_density 2167 */ 2168 if ((data_list_len < 5 * sizeof (int)) || 2169 (data_list_len < 6 * sizeof (int) + 2170 *(data_ptr + 4) * sizeof (int))) { 2171 /* 2172 * print warning and skip to next triplet. 2173 */ 2174 scsi_log(ST_DEVINFO, st_label, CE_WARN, 2175 "data property (%s) incomplete\n", 2176 datanameptr); 2177 kmem_free(data_list, data_list_len); 2178 continue; 2179 } 2180 2181 if (st_validate_conf_data(un, data_ptr, 2182 data_list_len / sizeof (int), datanameptr)) { 2183 kmem_free(data_list, data_list_len); 2184 scsi_log(ST_DEVINFO, st_label, CE_WARN, 2185 "data property (%s) rejected\n", 2186 datanameptr); 2187 continue; 2188 } 2189 2190 /* 2191 * check version 2192 */ 2193 version = *data_ptr++; 2194 if (version != 1 && version != 2) { 2195 /* print warning but accept it */ 2196 scsi_log(ST_DEVINFO, st_label, CE_WARN, 2197 "Version # for data property (%s) " 2198 "not set to 1 or 2\n", datanameptr); 2199 } 2200 2201 dp->type = *data_ptr++; 2202 dp->bsize = *data_ptr++; 2203 dp->options = *data_ptr++; 2204 dp->options |= ST_DYNAMIC; 2205 len = *data_ptr++; 2206 for (i = 0; i < NDENSITIES; i++) { 2207 if (i < len) { 2208 dp->densities[i] = *data_ptr++; 2209 } 2210 } 2211 dp->default_density = *data_ptr << 3; 2212 if (version == 2 && 2213 data_list_len >= (13 + len) * sizeof (int)) { 2214 data_ptr++; 2215 dp->non_motion_timeout = *data_ptr++; 2216 dp->io_timeout = *data_ptr++; 2217 dp->rewind_timeout = *data_ptr++; 2218 dp->space_timeout = *data_ptr++; 2219 dp->load_timeout = *data_ptr++; 2220 dp->unload_timeout = *data_ptr++; 2221 dp->erase_timeout = *data_ptr++; 2222 } 2223 kmem_free(data_list, data_list_len); 2224 found = 1; 2225 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 2226 "found in st.conf: vid = %s, pretty=%s\n", 2227 dp->vid, dp->name); 2228 break; 2229 } 2230 2231 /* 2232 * free up the memory allocated by ddi_getlongprop 2233 */ 2234 if (config_list) { 2235 kmem_free(config_list, config_list_len); 2236 } 2237 return (found); 2238 } 2239 2240 static int 2241 st_get_conf_from_st_conf_dot_c(struct scsi_tape *un, char *vidpid, 2242 struct st_drivetype *dp) 2243 { 2244 int i; 2245 2246 ST_FUNC(ST_DEVINFO, st_get_conf_from_st_conf_dot_c); 2247 /* 2248 * Determine type of tape controller. Type is determined by 2249 * checking the result of the earlier inquiry command and 2250 * comparing vendor ids with strings in a table declared in st_conf.c. 2251 */ 2252 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2253 "st_get_conf_from_st_conf_dot_c(): looking at st_drivetypes\n"); 2254 2255 for (i = 0; i < st_ndrivetypes; i++) { 2256 if (st_drivetypes[i].length == 0) { 2257 continue; 2258 } 2259 if (strncasecmp(vidpid, st_drivetypes[i].vid, 2260 st_drivetypes[i].length)) { 2261 continue; 2262 } 2263 bcopy(&st_drivetypes[i], dp, sizeof (st_drivetypes[i])); 2264 return (1); 2265 } 2266 return (0); 2267 } 2268 2269 static int 2270 st_get_conf_from_tape_drive(struct scsi_tape *un, char *vidpid, 2271 struct st_drivetype *dp) 2272 { 2273 int bsize; 2274 ulong_t maxbsize; 2275 caddr_t buf; 2276 struct st_drivetype *tem_dp; 2277 struct read_blklim *blklim; 2278 int rval; 2279 int i; 2280 2281 ST_FUNC(ST_DEVINFO, st_get_conf_from_tape_drive); 2282 2283 /* 2284 * Determine the type of tape controller. Type is determined by 2285 * sending SCSI commands to tape drive and deriving the type from 2286 * the returned data. 2287 */ 2288 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2289 "st_get_conf_from_tape_drive(): asking tape drive\n"); 2290 2291 tem_dp = kmem_zalloc(sizeof (struct st_drivetype), KM_SLEEP); 2292 2293 /* 2294 * Make up a name 2295 */ 2296 bcopy(vidpid, tem_dp->name, VIDPIDLEN); 2297 tem_dp->name[VIDPIDLEN] = '\0'; 2298 tem_dp->length = min(strlen(ST_INQUIRY->inq_vid), (VIDPIDLEN - 1)); 2299 (void) strncpy(tem_dp->vid, ST_INQUIRY->inq_vid, tem_dp->length); 2300 /* 2301 * 'clean' vendor and product strings of non-printing chars 2302 */ 2303 for (i = 0; i < VIDPIDLEN - 1; i ++) { 2304 if (tem_dp->name[i] < ' ' || tem_dp->name[i] > '~') { 2305 tem_dp->name[i] = '.'; 2306 } 2307 } 2308 2309 /* 2310 * MODE SENSE to determine block size. 2311 */ 2312 un->un_dp->options |= ST_MODE_SEL_COMP | ST_UNLOADABLE; 2313 rval = st_modesense(un); 2314 if (rval) { 2315 if (rval == EACCES) { 2316 un->un_dp->type = ST_TYPE_INVALID; 2317 rval = 1; 2318 } else { 2319 un->un_dp->options &= ~ST_MODE_SEL_COMP; 2320 rval = 0; 2321 } 2322 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2323 "st_get_conf_from_tape_drive(): fail to mode sense\n"); 2324 goto exit; 2325 } 2326 2327 /* Can mode sense page 0x10 or 0xf */ 2328 tem_dp->options |= ST_MODE_SEL_COMP; 2329 bsize = (un->un_mspl->high_bl << 16) | 2330 (un->un_mspl->mid_bl << 8) | 2331 (un->un_mspl->low_bl); 2332 2333 if (bsize == 0) { 2334 tem_dp->options |= ST_VARIABLE; 2335 tem_dp->bsize = 0; 2336 } else if (bsize > ST_MAXRECSIZE_FIXED) { 2337 rval = st_change_block_size(un, 0); 2338 if (rval) { 2339 if (rval == EACCES) { 2340 un->un_dp->type = ST_TYPE_INVALID; 2341 rval = 1; 2342 } else { 2343 rval = 0; 2344 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2345 "st_get_conf_from_tape_drive(): " 2346 "Fixed record size is too large and" 2347 "cannot switch to variable record size"); 2348 } 2349 goto exit; 2350 } 2351 tem_dp->options |= ST_VARIABLE; 2352 } else { 2353 rval = st_change_block_size(un, 0); 2354 if (rval == 0) { 2355 tem_dp->options |= ST_VARIABLE; 2356 tem_dp->bsize = 0; 2357 } else if (rval != EACCES) { 2358 tem_dp->bsize = bsize; 2359 } else { 2360 un->un_dp->type = ST_TYPE_INVALID; 2361 rval = 1; 2362 goto exit; 2363 } 2364 } 2365 2366 /* 2367 * If READ BLOCk LIMITS works and upper block size limit is 2368 * more than 64K, ST_NO_RECSIZE_LIMIT is supported. 2369 */ 2370 blklim = kmem_zalloc(sizeof (struct read_blklim), KM_SLEEP); 2371 rval = st_read_block_limits(un, blklim); 2372 if (rval) { 2373 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2374 "st_get_conf_from_tape_drive(): " 2375 "fail to read block limits.\n"); 2376 rval = 0; 2377 kmem_free(blklim, sizeof (struct read_blklim)); 2378 goto exit; 2379 } 2380 maxbsize = (blklim->max_hi << 16) + 2381 (blklim->max_mid << 8) + blklim->max_lo; 2382 if (maxbsize > ST_MAXRECSIZE_VARIABLE) { 2383 tem_dp->options |= ST_NO_RECSIZE_LIMIT; 2384 } 2385 kmem_free(blklim, sizeof (struct read_blklim)); 2386 2387 /* 2388 * Inquiry VPD page 0xb0 to see if the tape drive supports WORM 2389 */ 2390 buf = kmem_zalloc(6, KM_SLEEP); 2391 rval = st_get_special_inquiry(un, 6, buf, 0xb0); 2392 if (rval) { 2393 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2394 "st_get_conf_from_tape_drive(): " 2395 "fail to read vitial inquiry.\n"); 2396 rval = 0; 2397 kmem_free(buf, 6); 2398 goto exit; 2399 } 2400 if (buf[4] & 1) { 2401 tem_dp->options |= ST_WORMABLE; 2402 } 2403 kmem_free(buf, 6); 2404 2405 /* Assume BSD BSR KNOWS_EOD */ 2406 tem_dp->options |= ST_BSF | ST_BSR | ST_KNOWS_EOD | ST_UNLOADABLE; 2407 tem_dp->max_rretries = -1; 2408 tem_dp->max_wretries = -1; 2409 2410 /* 2411 * Decide the densities supported by tape drive by sending 2412 * REPORT DENSITY SUPPORT command. 2413 */ 2414 if (st_get_densities_from_tape_drive(un, tem_dp) == 0) { 2415 goto exit; 2416 } 2417 2418 /* 2419 * Decide the timeout values for several commands by sending 2420 * REPORT SUPPORTED OPERATION CODES command. 2421 */ 2422 rval = st_get_timeout_values_from_tape_drive(un, tem_dp); 2423 if (rval == 0 || ((rval == 1) && (tem_dp->type == ST_TYPE_INVALID))) { 2424 goto exit; 2425 } 2426 2427 bcopy(tem_dp, dp, sizeof (struct st_drivetype)); 2428 rval = 1; 2429 2430 exit: 2431 un->un_status = KEY_NO_SENSE; 2432 kmem_free(tem_dp, sizeof (struct st_drivetype)); 2433 return (rval); 2434 } 2435 2436 static int 2437 st_get_densities_from_tape_drive(struct scsi_tape *un, 2438 struct st_drivetype *dp) 2439 { 2440 int i, p; 2441 size_t buflen; 2442 ushort_t des_len; 2443 uchar_t *den_header; 2444 uchar_t num_den; 2445 uchar_t den[NDENSITIES]; 2446 uchar_t deflt[NDENSITIES]; 2447 struct report_density_desc *den_desc; 2448 2449 ST_FUNC(ST_DEVINFO, st_get_densities_from_type_drive); 2450 2451 /* 2452 * Since we have no idea how many densitiy support entries 2453 * will be returned, we send the command firstly assuming 2454 * there is only one. Then we can decide the number of 2455 * entries by available density support length. If multiple 2456 * entries exist, we will resend the command with enough 2457 * buffer size. 2458 */ 2459 buflen = sizeof (struct report_density_header) + 2460 sizeof (struct report_density_desc); 2461 den_header = kmem_zalloc(buflen, KM_SLEEP); 2462 if (st_report_density_support(un, den_header, buflen) != 0) { 2463 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2464 "st_get_conf_from_tape_drive(): fail to report density.\n"); 2465 kmem_free(den_header, buflen); 2466 return (0); 2467 } 2468 des_len = 2469 BE_16(((struct report_density_header *)den_header)->ava_dens_len); 2470 num_den = (des_len - 2) / sizeof (struct report_density_desc); 2471 2472 if (num_den > 1) { 2473 kmem_free(den_header, buflen); 2474 buflen = sizeof (struct report_density_header) + 2475 sizeof (struct report_density_desc) * num_den; 2476 den_header = kmem_zalloc(buflen, KM_SLEEP); 2477 if (st_report_density_support(un, den_header, buflen) != 0) { 2478 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2479 "st_get_conf_from_tape_drive(): " 2480 "fail to report density.\n"); 2481 kmem_free(den_header, buflen); 2482 return (0); 2483 } 2484 } 2485 2486 den_desc = (struct report_density_desc *)(den_header 2487 + sizeof (struct report_density_header)); 2488 2489 /* 2490 * Decide the drive type by assigning organization 2491 */ 2492 for (i = 0; i < ST_NUM_MEMBERS(st_vid_dt); i ++) { 2493 if (strncmp(st_vid_dt[i].vid, (char *)(den_desc->ass_org), 2494 8) == 0) { 2495 dp->type = st_vid_dt[i].type; 2496 break; 2497 } 2498 } 2499 if (i == ST_NUM_MEMBERS(st_vid_dt)) { 2500 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2501 "st_get_conf_from_tape_drive(): " 2502 "can't find match of assigned ort.\n"); 2503 kmem_free(den_header, buflen); 2504 return (0); 2505 } 2506 2507 /* 2508 * The tape drive may support many tape formats, but the st driver 2509 * supports only the four highest densities. Since density code 2510 * values are returned by ascending sequence, we start from the 2511 * last entry of density support data block descriptor. 2512 */ 2513 p = 0; 2514 den_desc += num_den - 1; 2515 for (i = 0; i < num_den && p < NDENSITIES; i ++, den_desc --) { 2516 if ((den_desc->pri_den != 0) && (den_desc->wrtok)) { 2517 if (p != 0) { 2518 if (den_desc->pri_den >= den[p - 1]) { 2519 continue; 2520 } 2521 } 2522 den[p] = den_desc->pri_den; 2523 deflt[p] = den_desc->deflt; 2524 p ++; 2525 } 2526 } 2527 2528 switch (p) { 2529 case 0: 2530 bzero(dp->densities, NDENSITIES); 2531 dp->options |= ST_AUTODEN_OVERRIDE; 2532 dp->default_density = MT_DENSITY4; 2533 break; 2534 2535 case 1: 2536 (void) memset(dp->densities, den[0], NDENSITIES); 2537 dp->options |= ST_AUTODEN_OVERRIDE; 2538 dp->default_density = MT_DENSITY4; 2539 break; 2540 2541 case 2: 2542 dp->densities[0] = den[1]; 2543 dp->densities[1] = den[1]; 2544 dp->densities[2] = den[0]; 2545 dp->densities[3] = den[0]; 2546 if (deflt[0]) { 2547 dp->default_density = MT_DENSITY4; 2548 } else { 2549 dp->default_density = MT_DENSITY2; 2550 } 2551 break; 2552 2553 case 3: 2554 dp->densities[0] = den[2]; 2555 dp->densities[1] = den[1]; 2556 dp->densities[2] = den[0]; 2557 dp->densities[3] = den[0]; 2558 if (deflt[0]) { 2559 dp->default_density = MT_DENSITY4; 2560 } else if (deflt[1]) { 2561 dp->default_density = MT_DENSITY2; 2562 } else { 2563 dp->default_density = MT_DENSITY1; 2564 } 2565 break; 2566 2567 default: 2568 for (i = p; i > p - NDENSITIES; i --) { 2569 dp->densities[i - 1] = den[p - i]; 2570 } 2571 if (deflt[0]) { 2572 dp->default_density = MT_DENSITY4; 2573 } else if (deflt[1]) { 2574 dp->default_density = MT_DENSITY3; 2575 } else if (deflt[2]) { 2576 dp->default_density = MT_DENSITY2; 2577 } else { 2578 dp->default_density = MT_DENSITY1; 2579 } 2580 break; 2581 } 2582 2583 bzero(dp->mediatype, NDENSITIES); 2584 2585 kmem_free(den_header, buflen); 2586 return (1); 2587 } 2588 2589 static int 2590 st_get_timeout_values_from_tape_drive(struct scsi_tape *un, 2591 struct st_drivetype *dp) 2592 { 2593 ushort_t timeout; 2594 int rval; 2595 2596 ST_FUNC(ST_DEVINFO, st_get_timeout_values_from_type_drive); 2597 2598 rval = st_get_timeouts_value(un, SCMD_ERASE, &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->erase_timeout = timeout; 2608 2609 rval = st_get_timeouts_value(un, SCMD_READ, &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->io_timeout = timeout; 2619 2620 rval = st_get_timeouts_value(un, SCMD_WRITE, &timeout, 0); 2621 if (rval) { 2622 if (rval == EACCES) { 2623 un->un_dp->type = ST_TYPE_INVALID; 2624 dp->type = ST_TYPE_INVALID; 2625 return (1); 2626 } 2627 return (0); 2628 } 2629 dp->io_timeout = max(dp->io_timeout, timeout); 2630 2631 rval = st_get_timeouts_value(un, SCMD_SPACE, &timeout, 0); 2632 if (rval) { 2633 if (rval == EACCES) { 2634 un->un_dp->type = ST_TYPE_INVALID; 2635 dp->type = ST_TYPE_INVALID; 2636 return (1); 2637 } 2638 return (0); 2639 } 2640 dp->space_timeout = timeout; 2641 2642 rval = st_get_timeouts_value(un, SCMD_LOAD, &timeout, 0); 2643 if (rval) { 2644 if (rval == EACCES) { 2645 un->un_dp->type = ST_TYPE_INVALID; 2646 dp->type = ST_TYPE_INVALID; 2647 return (1); 2648 } 2649 return (0); 2650 } 2651 dp->load_timeout = timeout; 2652 dp->unload_timeout = timeout; 2653 2654 rval = st_get_timeouts_value(un, SCMD_REWIND, &timeout, 0); 2655 if (rval) { 2656 if (rval == EACCES) { 2657 un->un_dp->type = ST_TYPE_INVALID; 2658 dp->type = ST_TYPE_INVALID; 2659 return (1); 2660 } 2661 return (0); 2662 } 2663 dp->rewind_timeout = timeout; 2664 2665 rval = st_get_timeouts_value(un, SCMD_INQUIRY, &timeout, 0); 2666 if (rval) { 2667 if (rval == EACCES) { 2668 un->un_dp->type = ST_TYPE_INVALID; 2669 dp->type = ST_TYPE_INVALID; 2670 return (1); 2671 } 2672 return (0); 2673 } 2674 dp->non_motion_timeout = timeout; 2675 2676 return (1); 2677 } 2678 2679 static int 2680 st_get_timeouts_value(struct scsi_tape *un, uchar_t option_code, 2681 ushort_t *timeout_value, ushort_t service_action) 2682 { 2683 uchar_t *timeouts; 2684 uchar_t *oper; 2685 uchar_t support; 2686 uchar_t cdbsize; 2687 uchar_t ctdp; 2688 size_t buflen; 2689 int rval; 2690 2691 ST_FUNC(ST_DEVINFO, st_get_timeouts_value); 2692 2693 buflen = sizeof (struct one_com_des) + 2694 sizeof (struct com_timeout_des); 2695 oper = kmem_zalloc(buflen, KM_SLEEP); 2696 rval = st_report_supported_operation(un, oper, option_code, 2697 service_action); 2698 2699 if (rval) { 2700 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2701 "st_get_timeouts_value(): " 2702 "fail to timeouts value for command %d.\n", option_code); 2703 kmem_free(oper, buflen); 2704 return (rval); 2705 } 2706 2707 support = ((struct one_com_des *)oper)->support; 2708 if ((support != SUPPORT_VALUES_SUPPORT_SCSI) && 2709 (support != SUPPORT_VALUES_SUPPORT_VENDOR)) { 2710 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2711 "st_get_timeouts_value(): " 2712 "command %d is not supported.\n", option_code); 2713 kmem_free(oper, buflen); 2714 return (ENOTSUP); 2715 } 2716 2717 ctdp = ((struct one_com_des *)oper)->ctdp; 2718 if (!ctdp) { 2719 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2720 "st_get_timeouts_value(): " 2721 "command timeout is not included.\n"); 2722 kmem_free(oper, buflen); 2723 return (ENOTSUP); 2724 } 2725 2726 cdbsize = BE_16(((struct one_com_des *)oper)->cdb_size); 2727 timeouts = (uchar_t *)(oper + cdbsize + 4); 2728 2729 /* 2730 * Timeout value in seconds is 4 bytes, but we only support the lower 2 2731 * bytes. If the higher 2 bytes are not zero, the timeout value is set 2732 * to 0xFFFF. 2733 */ 2734 if (*(timeouts + 8) != 0 || *(timeouts + 9) != 0) { 2735 *timeout_value = USHRT_MAX; 2736 } else { 2737 *timeout_value = ((*(timeouts + 10)) << 8) | 2738 (*(timeouts + 11)); 2739 } 2740 2741 kmem_free(oper, buflen); 2742 return (0); 2743 } 2744 2745 static int 2746 st_get_default_conf(struct scsi_tape *un, char *vidpid, struct st_drivetype *dp) 2747 { 2748 int i; 2749 2750 ST_FUNC(ST_DEVINFO, st_get_default_conf); 2751 2752 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 2753 "st_get_default_conf(): making drivetype from INQ cmd\n"); 2754 2755 /* 2756 * Make up a name 2757 */ 2758 bcopy("Vendor '", dp->name, 8); 2759 bcopy(vidpid, &dp->name[8], VIDLEN); 2760 bcopy("' Product '", &dp->name[16], 11); 2761 bcopy(&vidpid[8], &dp->name[27], PIDLEN); 2762 dp->name[ST_NAMESIZE - 2] = '\''; 2763 dp->name[ST_NAMESIZE - 1] = '\0'; 2764 dp->length = min(strlen(ST_INQUIRY->inq_vid), (VIDPIDLEN - 1)); 2765 (void) strncpy(dp->vid, ST_INQUIRY->inq_vid, dp->length); 2766 /* 2767 * 'clean' vendor and product strings of non-printing chars 2768 */ 2769 for (i = 0; i < ST_NAMESIZE - 2; i++) { 2770 if (dp->name[i] < ' ' || dp->name[i] > '~') { 2771 dp->name[i] = '.'; 2772 } 2773 } 2774 dp->type = ST_TYPE_INVALID; 2775 dp->options |= (ST_DYNAMIC | ST_UNLOADABLE | ST_MODE_SEL_COMP); 2776 2777 return (1); /* Can Not Fail */ 2778 } 2779 2780 /* 2781 * Regular Unix Entry points 2782 */ 2783 2784 2785 2786 /* ARGSUSED */ 2787 static int 2788 st_open(dev_t *dev_p, int flag, int otyp, cred_t *cred_p) 2789 { 2790 dev_t dev = *dev_p; 2791 int rval = 0; 2792 2793 GET_SOFT_STATE(dev); 2794 2795 ST_ENTR(ST_DEVINFO, st_open); 2796 2797 /* 2798 * validate that we are addressing a sensible unit 2799 */ 2800 mutex_enter(ST_MUTEX); 2801 2802 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 2803 "st_open(node = %s dev = 0x%lx, flag = %d, otyp = %d)\n", 2804 st_dev_name(dev), *dev_p, flag, otyp); 2805 2806 /* 2807 * All device accesss go thru st_strategy() where we check 2808 * suspend status 2809 */ 2810 2811 if (!un->un_attached) { 2812 st_known_tape_type(un); 2813 if (!un->un_attached) { 2814 rval = ENXIO; 2815 goto exit; 2816 } 2817 2818 } 2819 2820 /* 2821 * Check for the case of the tape in the middle of closing. 2822 * This isn't simply a check of the current state, because 2823 * we could be in state of sensing with the previous state 2824 * that of closing. 2825 * 2826 * And don't allow multiple opens. 2827 */ 2828 if (!(flag & (FNDELAY | FNONBLOCK)) && IS_CLOSING(un)) { 2829 un->un_laststate = un->un_state; 2830 un->un_state = ST_STATE_CLOSE_PENDING_OPEN; 2831 while (IS_CLOSING(un) || 2832 un->un_state == ST_STATE_CLOSE_PENDING_OPEN) { 2833 if (cv_wait_sig(&un->un_clscv, ST_MUTEX) == 0) { 2834 rval = EINTR; 2835 un->un_state = un->un_laststate; 2836 goto exit; 2837 } 2838 } 2839 } else if (un->un_state != ST_STATE_CLOSED) { 2840 rval = EBUSY; 2841 goto busy; 2842 } 2843 2844 /* 2845 * record current dev 2846 */ 2847 un->un_dev = dev; 2848 un->un_oflags = flag; /* save for use in st_tape_init() */ 2849 un->un_errno = 0; /* no errors yet */ 2850 un->un_restore_pos = 0; 2851 un->un_rqs_state = 0; 2852 2853 /* 2854 * If we are opening O_NDELAY, or O_NONBLOCK, we don't check for 2855 * anything, leave internal states alone, if fileno >= 0 2856 */ 2857 if (flag & (FNDELAY | FNONBLOCK)) { 2858 switch (un->un_pos.pmode) { 2859 2860 case invalid: 2861 un->un_state = ST_STATE_OFFLINE; 2862 break; 2863 2864 case legacy: 2865 /* 2866 * If position is anything other than rewound. 2867 */ 2868 if (un->un_pos.fileno != 0 || un->un_pos.blkno != 0) { 2869 /* 2870 * set un_read_only/write-protect status. 2871 * 2872 * If the tape is not bot we can assume 2873 * that mspl->wp_status is set properly. 2874 * else 2875 * we need to do a mode sense/Tur once 2876 * again to get the actual tape status.(since 2877 * user might have replaced the tape) 2878 * Hence make the st state OFFLINE so that 2879 * we re-intialize the tape once again. 2880 */ 2881 un->un_read_only = 2882 (un->un_oflags & FWRITE) ? RDWR : RDONLY; 2883 un->un_state = ST_STATE_OPEN_PENDING_IO; 2884 } else { 2885 un->un_state = ST_STATE_OFFLINE; 2886 } 2887 break; 2888 case logical: 2889 if (un->un_pos.lgclblkno == 0) { 2890 un->un_state = ST_STATE_OFFLINE; 2891 } else { 2892 un->un_read_only = 2893 (un->un_oflags & FWRITE) ? RDWR : RDONLY; 2894 un->un_state = ST_STATE_OPEN_PENDING_IO; 2895 } 2896 break; 2897 } 2898 rval = 0; 2899 } else { 2900 /* 2901 * Not opening O_NDELAY. 2902 */ 2903 un->un_state = ST_STATE_OPENING; 2904 2905 /* 2906 * Clear error entry stack 2907 */ 2908 st_empty_error_stack(un); 2909 2910 rval = st_tape_init(un); 2911 if ((rval == EACCES) && (un->un_read_only & WORM)) { 2912 un->un_state = ST_STATE_OPEN_PENDING_IO; 2913 rval = 0; /* so open doesn't fail */ 2914 } else if (rval) { 2915 /* 2916 * Release the tape unit, if reserved and not 2917 * preserve reserve. 2918 */ 2919 if ((un->un_rsvd_status & 2920 (ST_RESERVE | ST_PRESERVE_RESERVE)) == ST_RESERVE) { 2921 (void) st_reserve_release(un, ST_RELEASE, 2922 st_uscsi_cmd); 2923 } 2924 } else { 2925 un->un_state = ST_STATE_OPEN_PENDING_IO; 2926 } 2927 } 2928 2929 exit: 2930 /* 2931 * we don't want any uninvited guests scrogging our data when we're 2932 * busy with something, so for successful opens or failed opens 2933 * (except for EBUSY), reset these counters and state appropriately. 2934 */ 2935 if (rval != EBUSY) { 2936 if (rval) { 2937 un->un_state = ST_STATE_CLOSED; 2938 } 2939 un->un_err_resid = 0; 2940 un->un_retry_ct = 0; 2941 } 2942 busy: 2943 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 2944 "st_open: return val = %x, state = %d\n", rval, un->un_state); 2945 mutex_exit(ST_MUTEX); 2946 return (rval); 2947 2948 } 2949 2950 static int 2951 st_tape_init(struct scsi_tape *un) 2952 { 2953 int err; 2954 int rval = 0; 2955 2956 ST_FUNC(ST_DEVINFO, st_tape_init); 2957 2958 ASSERT(mutex_owned(ST_MUTEX)); 2959 2960 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 2961 "st_tape_init(un = 0x%p, oflags = %d)\n", (void*)un, un->un_oflags); 2962 2963 /* 2964 * Clean up after any errors left by 'last' close. 2965 * This also handles the case of the initial open. 2966 */ 2967 if (un->un_state != ST_STATE_INITIALIZING) { 2968 un->un_laststate = un->un_state; 2969 un->un_state = ST_STATE_OPENING; 2970 } 2971 2972 un->un_kbytes_xferred = 0; 2973 2974 /* 2975 * do a throw away TUR to clear check condition 2976 */ 2977 err = st_cmd(un, SCMD_TEST_UNIT_READY, 0, SYNC_CMD); 2978 2979 /* 2980 * If test unit ready fails because the drive is reserved 2981 * by another host fail the open for no access. 2982 */ 2983 if (err) { 2984 if (un->un_rsvd_status & ST_RESERVATION_CONFLICT) { 2985 un->un_state = ST_STATE_CLOSED; 2986 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 2987 "st_tape_init: RESERVATION CONFLICT\n"); 2988 rval = EACCES; 2989 goto exit; 2990 } else if ((un->un_rsvd_status & 2991 ST_APPLICATION_RESERVATIONS) != 0) { 2992 if ((ST_RQSENSE != NULL) && 2993 (ST_RQSENSE->es_add_code == 0x2a && 2994 ST_RQSENSE->es_qual_code == 0x03)) { 2995 un->un_state = ST_STATE_CLOSED; 2996 rval = EACCES; 2997 goto exit; 2998 } 2999 } 3000 } 3001 3002 /* 3003 * Tape self identification could fail if the tape drive is used by 3004 * another host during attach time. We try to get the tape type 3005 * again. This is also applied to any posponed configuration methods. 3006 */ 3007 if (un->un_dp->type == ST_TYPE_INVALID) { 3008 un->un_comp_page = ST_DEV_DATACOMP_PAGE | ST_DEV_CONFIG_PAGE; 3009 st_known_tape_type(un); 3010 } 3011 3012 /* 3013 * If the tape type is still invalid, try to determine the generic 3014 * configuration. 3015 */ 3016 if (un->un_dp->type == ST_TYPE_INVALID) { 3017 rval = st_determine_generic(un); 3018 if (rval) { 3019 if (rval != EACCES) { 3020 rval = EIO; 3021 } 3022 un->un_state = ST_STATE_CLOSED; 3023 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 3024 "st_tape_init: %s invalid type\n", 3025 rval == EACCES ? "EACCES" : "EIO"); 3026 goto exit; 3027 } 3028 /* 3029 * If this is a Unknown Type drive, 3030 * Use the READ BLOCK LIMITS to determine if 3031 * allow large xfer is approprate if not globally 3032 * disabled with st_allow_large_xfer. 3033 */ 3034 un->un_allow_large_xfer = (uchar_t)st_allow_large_xfer; 3035 } else { 3036 3037 /* 3038 * If we allow_large_xfer (ie >64k) and have not yet found out 3039 * the max block size supported by the drive, 3040 * find it by issueing a READ_BLKLIM command. 3041 * if READ_BLKLIM cmd fails, assume drive doesn't 3042 * allow_large_xfer and min/max block sizes as 1 byte and 63k. 3043 */ 3044 un->un_allow_large_xfer = st_allow_large_xfer && 3045 (un->un_dp->options & ST_NO_RECSIZE_LIMIT); 3046 } 3047 /* 3048 * if maxbsize is unknown, set the maximum block size. 3049 */ 3050 if (un->un_maxbsize == MAXBSIZE_UNKNOWN) { 3051 3052 /* 3053 * Get the Block limits of the tape drive. 3054 * if un->un_allow_large_xfer = 0 , then make sure 3055 * that maxbsize is <= ST_MAXRECSIZE_FIXED. 3056 */ 3057 un->un_rbl = kmem_zalloc(RBLSIZE, KM_SLEEP); 3058 3059 err = st_cmd(un, SCMD_READ_BLKLIM, RBLSIZE, SYNC_CMD); 3060 if (err) { 3061 /* Retry */ 3062 err = st_cmd(un, SCMD_READ_BLKLIM, RBLSIZE, SYNC_CMD); 3063 } 3064 if (!err) { 3065 3066 /* 3067 * if cmd successful, use limit returned 3068 */ 3069 un->un_maxbsize = (un->un_rbl->max_hi << 16) + 3070 (un->un_rbl->max_mid << 8) + 3071 un->un_rbl->max_lo; 3072 un->un_minbsize = (un->un_rbl->min_hi << 8) + 3073 un->un_rbl->min_lo; 3074 un->un_data_mod = 1 << un->un_rbl->granularity; 3075 if ((un->un_maxbsize == 0) || 3076 (un->un_allow_large_xfer == 0 && 3077 un->un_maxbsize > ST_MAXRECSIZE_FIXED)) { 3078 un->un_maxbsize = ST_MAXRECSIZE_FIXED; 3079 3080 } else if (un->un_dp->type == ST_TYPE_DEFAULT) { 3081 /* 3082 * Drive is not one that is configured, But the 3083 * READ BLOCK LIMITS tells us it can do large 3084 * xfers. 3085 */ 3086 if (un->un_maxbsize > ST_MAXRECSIZE_FIXED) { 3087 un->un_dp->options |= 3088 ST_NO_RECSIZE_LIMIT; 3089 } 3090 /* 3091 * If max and mimimum block limits are the 3092 * same this is a fixed block size device. 3093 */ 3094 if (un->un_maxbsize == un->un_minbsize) { 3095 un->un_dp->options &= ~ST_VARIABLE; 3096 } 3097 } 3098 3099 if (un->un_minbsize == 0) { 3100 un->un_minbsize = 1; 3101 } 3102 3103 } else { /* error on read block limits */ 3104 3105 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 3106 "!st_tape_init: Error on READ BLOCK LIMITS," 3107 " errno = %d un_rsvd_status = 0x%X\n", 3108 err, un->un_rsvd_status); 3109 3110 /* 3111 * since read block limits cmd failed, 3112 * do not allow large xfers. 3113 * use old values in st_minphys 3114 */ 3115 if (un->un_rsvd_status & ST_RESERVATION_CONFLICT) { 3116 rval = EACCES; 3117 } else { 3118 un->un_allow_large_xfer = 0; 3119 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 3120 "!Disabling large transfers\n"); 3121 3122 /* 3123 * we guess maxbsize and minbsize 3124 */ 3125 if (un->un_bsize) { 3126 un->un_maxbsize = un->un_minbsize = 3127 un->un_bsize; 3128 } else { 3129 un->un_maxbsize = ST_MAXRECSIZE_FIXED; 3130 un->un_minbsize = 1; 3131 } 3132 /* 3133 * Data Mod must be set, 3134 * Even if read block limits fails. 3135 * Prevents Divide By Zero in st_rw(). 3136 */ 3137 un->un_data_mod = 1; 3138 } 3139 } 3140 if (un->un_rbl) { 3141 kmem_free(un->un_rbl, RBLSIZE); 3142 un->un_rbl = NULL; 3143 } 3144 3145 if (rval) { 3146 goto exit; 3147 } 3148 } 3149 3150 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 3151 "maxdma = %d, maxbsize = %d, minbsize = %d, %s large xfer\n", 3152 un->un_maxdma, un->un_maxbsize, un->un_minbsize, 3153 (un->un_allow_large_xfer ? "ALLOW": "DON'T ALLOW")); 3154 3155 err = st_cmd(un, SCMD_TEST_UNIT_READY, 0, SYNC_CMD); 3156 3157 if (err != 0) { 3158 if (err == EINTR) { 3159 un->un_laststate = un->un_state; 3160 un->un_state = ST_STATE_CLOSED; 3161 rval = EINTR; 3162 goto exit; 3163 } 3164 /* 3165 * Make sure the tape is ready 3166 */ 3167 un->un_pos.pmode = invalid; 3168 if (un->un_status != KEY_UNIT_ATTENTION) { 3169 /* 3170 * allow open no media. Subsequent MTIOCSTATE 3171 * with media present will complete the open 3172 * logic. 3173 */ 3174 un->un_laststate = un->un_state; 3175 if (un->un_oflags & (FNONBLOCK|FNDELAY)) { 3176 un->un_mediastate = MTIO_EJECTED; 3177 un->un_state = ST_STATE_OFFLINE; 3178 rval = 0; 3179 goto exit; 3180 } else { 3181 un->un_state = ST_STATE_CLOSED; 3182 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 3183 "st_tape_init EIO no media, not opened " 3184 "O_NONBLOCK|O_EXCL\n"); 3185 rval = EIO; 3186 goto exit; 3187 } 3188 } 3189 } 3190 3191 /* 3192 * On each open, initialize block size from drivetype struct, 3193 * as it could have been changed by MTSRSZ ioctl. 3194 * Now, ST_VARIABLE simply means drive is capable of variable 3195 * mode. All drives are assumed to support fixed records. 3196 * Hence, un_bsize tells what mode the drive is in. 3197 * un_bsize = 0 - variable record length 3198 * = x - fixed record length is x 3199 */ 3200 un->un_bsize = un->un_dp->bsize; 3201 3202 /* 3203 * If saved position is valid go there 3204 */ 3205 if (un->un_restore_pos) { 3206 un->un_restore_pos = 0; 3207 un->un_pos.fileno = un->un_save_fileno; 3208 un->un_pos.blkno = un->un_save_blkno; 3209 rval = st_validate_tapemarks(un, st_uscsi_cmd, &un->un_pos); 3210 if (rval != 0) { 3211 if (rval != EACCES) { 3212 rval = EIO; 3213 } 3214 un->un_laststate = un->un_state; 3215 un->un_state = ST_STATE_CLOSED; 3216 goto exit; 3217 } 3218 } 3219 3220 if (un->un_pos.pmode == invalid) { 3221 rval = st_loadtape(un); 3222 if (rval) { 3223 if (rval != EACCES) { 3224 rval = EIO; 3225 } 3226 un->un_laststate = un->un_state; 3227 un->un_state = ST_STATE_CLOSED; 3228 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 3229 "st_tape_init: %s can't open tape\n", 3230 rval == EACCES ? "EACCES" : "EIO"); 3231 goto exit; 3232 } 3233 } 3234 3235 /* 3236 * do a mode sense to pick up state of current write-protect, 3237 * Could cause reserve and fail due to conflict. 3238 */ 3239 if (un->un_unit_attention_flags) { 3240 rval = st_modesense(un); 3241 if (rval == EACCES) { 3242 goto exit; 3243 } 3244 } 3245 3246 /* 3247 * If we are opening the tape for writing, check 3248 * to make sure that the tape can be written. 3249 */ 3250 if (un->un_oflags & FWRITE) { 3251 err = 0; 3252 if (un->un_mspl->wp) { 3253 un->un_status = KEY_WRITE_PROTECT; 3254 un->un_laststate = un->un_state; 3255 un->un_state = ST_STATE_CLOSED; 3256 rval = EACCES; 3257 /* 3258 * STK sets the wp bit if volsafe tape is loaded. 3259 */ 3260 if ((un->un_dp->type == MT_ISSTK9840) && 3261 (un->un_dp->options & ST_WORMABLE)) { 3262 un->un_read_only = RDONLY; 3263 } else { 3264 goto exit; 3265 } 3266 } else { 3267 un->un_read_only = RDWR; 3268 } 3269 } else { 3270 un->un_read_only = RDONLY; 3271 } 3272 3273 if (un->un_dp->options & ST_WORMABLE && 3274 un->un_unit_attention_flags) { 3275 un->un_read_only |= un->un_wormable(un); 3276 3277 if (((un->un_read_only == WORM) || 3278 (un->un_read_only == RDWORM)) && 3279 ((un->un_oflags & FWRITE) == FWRITE)) { 3280 un->un_status = KEY_DATA_PROTECT; 3281 rval = EACCES; 3282 ST_DEBUG4(ST_DEVINFO, st_label, CE_NOTE, 3283 "read_only = %d eof = %d oflag = %d\n", 3284 un->un_read_only, un->un_pos.eof, un->un_oflags); 3285 } 3286 } 3287 3288 /* 3289 * If we're opening the tape write-only, we need to 3290 * write 2 filemarks on the HP 1/2 inch drive, to 3291 * create a null file. 3292 */ 3293 if ((un->un_read_only == RDWR) || 3294 (un->un_read_only == WORM) && (un->un_oflags & FWRITE)) { 3295 if (un->un_dp->options & ST_REEL) { 3296 un->un_fmneeded = 2; 3297 } else { 3298 un->un_fmneeded = 1; 3299 } 3300 } else { 3301 un->un_fmneeded = 0; 3302 } 3303 3304 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 3305 "fmneeded = %x\n", un->un_fmneeded); 3306 3307 /* 3308 * Make sure the density can be selected correctly. 3309 * If WORM can only write at the append point which in most cases 3310 * isn't BOP. st_determine_density() with a B_WRITE only attempts 3311 * to set and try densities if a BOP. 3312 */ 3313 if (st_determine_density(un, 3314 un->un_read_only == RDWR ? B_WRITE : B_READ)) { 3315 un->un_status = KEY_ILLEGAL_REQUEST; 3316 un->un_laststate = un->un_state; 3317 un->un_state = ST_STATE_CLOSED; 3318 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 3319 "st_tape_init: EIO can't determine density\n"); 3320 rval = EIO; 3321 goto exit; 3322 } 3323 3324 /* 3325 * Destroy the knowledge that we have 'determined' 3326 * density so that a later read at BOT comes along 3327 * does the right density determination. 3328 */ 3329 3330 un->un_density_known = 0; 3331 3332 3333 /* 3334 * Okay, the tape is loaded and either at BOT or somewhere past. 3335 * Mark the state such that any I/O or tape space operations 3336 * will get/set the right density, etc.. 3337 */ 3338 un->un_laststate = un->un_state; 3339 un->un_lastop = ST_OP_NIL; 3340 un->un_mediastate = MTIO_INSERTED; 3341 cv_broadcast(&un->un_state_cv); 3342 3343 /* 3344 * Set test append flag if writing. 3345 * First write must check that tape is positioned correctly. 3346 */ 3347 un->un_test_append = (un->un_oflags & FWRITE); 3348 3349 /* 3350 * if there are pending unit attention flags. 3351 * Check that the media has not changed. 3352 */ 3353 if (un->un_unit_attention_flags) { 3354 rval = st_get_media_identification(un, st_uscsi_cmd); 3355 if (rval != 0 && rval != EACCES) { 3356 rval = EIO; 3357 } 3358 un->un_unit_attention_flags = 0; 3359 } 3360 3361 exit: 3362 un->un_err_resid = 0; 3363 un->un_last_resid = 0; 3364 un->un_last_count = 0; 3365 3366 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 3367 "st_tape_init: return val = %x\n", rval); 3368 return (rval); 3369 3370 } 3371 3372 3373 3374 /* ARGSUSED */ 3375 static int 3376 st_close(dev_t dev, int flag, int otyp, cred_t *cred_p) 3377 { 3378 int err = 0; 3379 int count, last_state; 3380 minor_t minor = getminor(dev); 3381 #ifdef __x86 3382 struct contig_mem *cp, *cp_temp; 3383 #endif 3384 3385 GET_SOFT_STATE(dev); 3386 3387 ST_ENTR(ST_DEVINFO, st_close); 3388 3389 /* 3390 * wait till all cmds in the pipeline have been completed 3391 */ 3392 mutex_enter(ST_MUTEX); 3393 3394 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 3395 "st_close(dev = 0x%lx, flag = %d, otyp = %d)\n", dev, flag, otyp); 3396 3397 st_wait_for_io(un); 3398 3399 /* turn off persistent errors on close, as we want close to succeed */ 3400 st_turn_pe_off(un); 3401 3402 /* 3403 * set state to indicate that we are in process of closing 3404 */ 3405 last_state = un->un_laststate = un->un_state; 3406 un->un_state = ST_STATE_CLOSING; 3407 3408 ST_POS(ST_DEVINFO, "st_close1:", &un->un_pos); 3409 3410 /* 3411 * BSD behavior: 3412 * a close always causes a silent span to the next file if we've hit 3413 * an EOF (but not yet read across it). 3414 */ 3415 if ((minor & MT_BSD) && (un->un_pos.eof == ST_EOF)) { 3416 if (un->un_pos.pmode != invalid) { 3417 un->un_pos.fileno++; 3418 un->un_pos.blkno = 0; 3419 } 3420 un->un_pos.eof = ST_NO_EOF; 3421 } 3422 3423 /* 3424 * SVR4 behavior for skipping to next file: 3425 * 3426 * If we have not seen a filemark, space to the next file 3427 * 3428 * If we have already seen the filemark we are physically in the next 3429 * file and we only increment the filenumber 3430 */ 3431 if (((minor & (MT_BSD | MT_NOREWIND)) == MT_NOREWIND) && 3432 (flag & FREAD) && /* reading or at least asked to */ 3433 (un->un_mediastate == MTIO_INSERTED) && /* tape loaded */ 3434 (un->un_pos.pmode != invalid) && /* XXX position known */ 3435 ((un->un_pos.blkno != 0) && /* inside a file */ 3436 (un->un_lastop != ST_OP_WRITE) && /* Didn't just write */ 3437 (un->un_lastop != ST_OP_WEOF))) { /* or write filemarks */ 3438 switch (un->un_pos.eof) { 3439 case ST_NO_EOF: 3440 /* 3441 * if we were reading and did not read the complete file 3442 * skip to the next file, leaving the tape correctly 3443 * positioned to read the first record of the next file 3444 * Check first for REEL if we are at EOT by trying to 3445 * read a block 3446 */ 3447 if ((un->un_dp->options & ST_REEL) && 3448 (!(un->un_dp->options & ST_READ_IGNORE_EOFS)) && 3449 (un->un_pos.blkno == 0)) { 3450 if (st_cmd(un, SCMD_SPACE, Blk(1), SYNC_CMD)) { 3451 ST_DEBUG2(ST_DEVINFO, st_label, 3452 SCSI_DEBUG, 3453 "st_close : EIO can't space\n"); 3454 err = EIO; 3455 goto error_out; 3456 } 3457 if (un->un_pos.eof >= ST_EOF_PENDING) { 3458 un->un_pos.eof = ST_EOT_PENDING; 3459 un->un_pos.fileno += 1; 3460 un->un_pos.blkno = 0; 3461 break; 3462 } 3463 } 3464 if (st_cmd(un, SCMD_SPACE, Fmk(1), SYNC_CMD)) { 3465 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 3466 "st_close: EIO can't space #2\n"); 3467 err = EIO; 3468 goto error_out; 3469 } else { 3470 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 3471 "st_close2: fileno=%x,blkno=%x,eof=%x\n", 3472 un->un_pos.fileno, un->un_pos.blkno, 3473 un->un_pos.eof); 3474 un->un_pos.eof = ST_NO_EOF; 3475 } 3476 break; 3477 3478 case ST_EOF_PENDING: 3479 case ST_EOF: 3480 un->un_pos.fileno += 1; 3481 un->un_pos.lgclblkno += 1; 3482 un->un_pos.blkno = 0; 3483 un->un_pos.eof = ST_NO_EOF; 3484 break; 3485 3486 case ST_EOT: 3487 case ST_EOT_PENDING: 3488 case ST_EOM: 3489 /* nothing to do */ 3490 break; 3491 default: 3492 ST_DEBUG(ST_DEVINFO, st_label, CE_PANIC, 3493 "Undefined state 0x%x", un->un_pos.eof); 3494 3495 } 3496 } 3497 3498 3499 /* 3500 * For performance reasons (HP 88780), the driver should 3501 * postpone writing the second tape mark until just before a file 3502 * positioning ioctl is issued (e.g., rewind). This means that 3503 * the user must not manually rewind the tape because the tape will 3504 * be missing the second tape mark which marks EOM. 3505 * However, this small performance improvement is not worth the risk. 3506 */ 3507 3508 /* 3509 * We need to back up over the filemark we inadvertently popped 3510 * over doing a read in between the two filemarks that constitute 3511 * logical eot for 1/2" tapes. Note that ST_EOT_PENDING is only 3512 * set while reading. 3513 * 3514 * If we happen to be at physical eot (ST_EOM) (writing case), 3515 * the writing of filemark(s) will clear the ST_EOM state, which 3516 * we don't want, so we save this state and restore it later. 3517 */ 3518 3519 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 3520 "flag=%x, fmneeded=%x, lastop=%x, eof=%x\n", 3521 flag, un->un_fmneeded, un->un_lastop, un->un_pos.eof); 3522 3523 if (un->un_pos.eof == ST_EOT_PENDING) { 3524 if (minor & MT_NOREWIND) { 3525 if (st_cmd(un, SCMD_SPACE, Fmk(-1), SYNC_CMD)) { 3526 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 3527 "st_close: EIO can't space #3\n"); 3528 err = EIO; 3529 goto error_out; 3530 } else { 3531 un->un_pos.blkno = 0; 3532 un->un_pos.eof = ST_EOT; 3533 } 3534 } else { 3535 un->un_pos.eof = ST_NO_EOF; 3536 } 3537 3538 /* 3539 * Do we need to write a file mark? 3540 * 3541 * only write filemarks if there are fmks to be written and 3542 * - open for write (possibly read/write) 3543 * - the last operation was a write 3544 * or: 3545 * - opened for wronly 3546 * - no data was written 3547 */ 3548 } else if ((un->un_pos.pmode != invalid) && 3549 (un->un_fmneeded > 0) && 3550 (((flag & FWRITE) && 3551 ((un->un_lastop == ST_OP_WRITE)||(un->un_lastop == ST_OP_WEOF))) || 3552 ((flag == FWRITE) && (un->un_lastop == ST_OP_NIL)))) { 3553 3554 /* save ST_EOM state */ 3555 int was_at_eom = (un->un_pos.eof == ST_EOM) ? 1 : 0; 3556 3557 /* 3558 * Note that we will write a filemark if we had opened 3559 * the tape write only and no data was written, thus 3560 * creating a null file. 3561 * 3562 * If the user already wrote one, we only have to write 1 more. 3563 * If they wrote two, we don't have to write any. 3564 */ 3565 3566 count = un->un_fmneeded; 3567 if (count > 0) { 3568 if (st_cmd(un, SCMD_WRITE_FILE_MARK, count, SYNC_CMD)) { 3569 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 3570 "st_close : EIO can't wfm\n"); 3571 err = EIO; 3572 goto error_out; 3573 } 3574 if ((un->un_dp->options & ST_REEL) && 3575 (minor & MT_NOREWIND)) { 3576 if (st_cmd(un, SCMD_SPACE, Fmk(-1), SYNC_CMD)) { 3577 ST_DEBUG2(ST_DEVINFO, st_label, 3578 SCSI_DEBUG, 3579 "st_close : EIO space fmk(-1)\n"); 3580 err = EIO; 3581 goto error_out; 3582 } 3583 un->un_pos.eof = ST_NO_EOF; 3584 /* fix up block number */ 3585 un->un_pos.blkno = 0; 3586 } 3587 } 3588 3589 /* 3590 * If we aren't going to be rewinding, and we were at 3591 * physical eot, restore the state that indicates we 3592 * are at physical eot. Once you have reached physical 3593 * eot, and you close the tape, the only thing you can 3594 * do on the next open is to rewind. Access to trailer 3595 * records is only allowed without closing the device. 3596 */ 3597 if ((minor & MT_NOREWIND) == 0 && was_at_eom) { 3598 un->un_pos.eof = ST_EOM; 3599 } 3600 } 3601 3602 /* 3603 * report soft errors if enabled and available, if we never accessed 3604 * the drive, don't get errors. This will prevent some DAT error 3605 * messages upon LOG SENSE. 3606 */ 3607 if (st_report_soft_errors_on_close && 3608 (un->un_dp->options & ST_SOFT_ERROR_REPORTING) && 3609 (last_state != ST_STATE_OFFLINE)) { 3610 if (st_report_soft_errors(dev, flag)) { 3611 err = EIO; 3612 goto error_out; 3613 } 3614 } 3615 3616 3617 /* 3618 * Do we need to rewind? Can we rewind? 3619 */ 3620 if ((minor & MT_NOREWIND) == 0 && 3621 un->un_pos.pmode != invalid && err == 0) { 3622 /* 3623 * We'd like to rewind with the 3624 * 'immediate' bit set, but this 3625 * causes problems on some drives 3626 * where subsequent opens get a 3627 * 'NOT READY' error condition 3628 * back while the tape is rewinding, 3629 * which is impossible to distinguish 3630 * from the condition of 'no tape loaded'. 3631 * 3632 * Also, for some targets, if you disconnect 3633 * with the 'immediate' bit set, you don't 3634 * actually return right away, i.e., the 3635 * target ignores your request for immediate 3636 * return. 3637 * 3638 * Instead, we'll fire off an async rewind 3639 * command. We'll mark the device as closed, 3640 * and any subsequent open will stall on 3641 * the first TEST_UNIT_READY until the rewind 3642 * completes. 3643 */ 3644 3645 /* 3646 * Used to be if reserve was not supported we'd send an 3647 * asynchronious rewind. Comments above may be slightly invalid 3648 * as the immediate bit was never set. Doing an immedate rewind 3649 * makes sense, I think fixes to not ready status might handle 3650 * the problems described above. 3651 */ 3652 if (un->un_sd->sd_inq->inq_ansi < 2) { 3653 if (st_cmd(un, SCMD_REWIND, 0, SYNC_CMD)) { 3654 err = EIO; 3655 } 3656 } else { 3657 /* flush data for older drives per scsi spec. */ 3658 if (st_cmd(un, SCMD_WRITE_FILE_MARK, 0, SYNC_CMD)) { 3659 err = EIO; 3660 } else { 3661 /* release the drive before rewind immediate */ 3662 if ((un->un_rsvd_status & 3663 (ST_RESERVE | ST_PRESERVE_RESERVE)) == 3664 ST_RESERVE) { 3665 if (st_reserve_release(un, ST_RELEASE, 3666 st_uscsi_cmd)) { 3667 err = EIO; 3668 } 3669 } 3670 3671 /* send rewind with immediate bit set */ 3672 if (st_cmd(un, SCMD_REWIND, 1, ASYNC_CMD)) { 3673 err = EIO; 3674 } 3675 } 3676 } 3677 /* 3678 * Setting positions invalid in case the rewind doesn't 3679 * happen. Drives don't like to rewind if resets happen 3680 * they will tend to move back to where the rewind was 3681 * issued if a reset or something happens so that if a 3682 * write happens the data doesn't get clobbered. 3683 * 3684 * Not a big deal if the position is invalid when the 3685 * open occures it will do a read position. 3686 */ 3687 un->un_pos.pmode = invalid; 3688 un->un_running.pmode = invalid; 3689 3690 if (err == EIO) { 3691 goto error_out; 3692 } 3693 } 3694 3695 /* 3696 * eject tape if necessary 3697 */ 3698 if (un->un_eject_tape_on_failure) { 3699 un->un_eject_tape_on_failure = 0; 3700 if (st_cmd(un, SCMD_LOAD, LD_UNLOAD, SYNC_CMD)) { 3701 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 3702 "st_close : can't unload tape\n"); 3703 err = EIO; 3704 goto error_out; 3705 } else { 3706 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 3707 "st_close : tape unloaded \n"); 3708 un->un_pos.eof = ST_NO_EOF; 3709 un->un_mediastate = MTIO_EJECTED; 3710 } 3711 } 3712 /* 3713 * Release the tape unit, if default reserve/release 3714 * behaviour. 3715 */ 3716 if ((un->un_rsvd_status & 3717 (ST_RESERVE | ST_PRESERVE_RESERVE | 3718 ST_APPLICATION_RESERVATIONS)) == ST_RESERVE) { 3719 (void) st_reserve_release(un, ST_RELEASE, st_uscsi_cmd); 3720 } 3721 error_out: 3722 /* 3723 * clear up state 3724 */ 3725 un->un_laststate = un->un_state; 3726 un->un_state = ST_STATE_CLOSED; 3727 un->un_lastop = ST_OP_NIL; 3728 un->un_throttle = 1; /* assume one request at time, for now */ 3729 un->un_retry_ct = 0; 3730 un->un_errno = 0; 3731 un->un_swr_token = (opaque_t)NULL; 3732 un->un_rsvd_status &= ~(ST_INIT_RESERVE); 3733 3734 /* Restore the options to the init time settings */ 3735 if (un->un_init_options & ST_READ_IGNORE_ILI) { 3736 un->un_dp->options |= ST_READ_IGNORE_ILI; 3737 } else { 3738 un->un_dp->options &= ~ST_READ_IGNORE_ILI; 3739 } 3740 3741 if (un->un_init_options & ST_READ_IGNORE_EOFS) { 3742 un->un_dp->options |= ST_READ_IGNORE_EOFS; 3743 } else { 3744 un->un_dp->options &= ~ST_READ_IGNORE_EOFS; 3745 } 3746 3747 if (un->un_init_options & ST_SHORT_FILEMARKS) { 3748 un->un_dp->options |= ST_SHORT_FILEMARKS; 3749 } else { 3750 un->un_dp->options &= ~ST_SHORT_FILEMARKS; 3751 } 3752 3753 ASSERT(mutex_owned(ST_MUTEX)); 3754 3755 /* 3756 * Signal anyone awaiting a close operation to complete. 3757 */ 3758 cv_signal(&un->un_clscv); 3759 3760 /* 3761 * any kind of error on closing causes all state to be tossed 3762 */ 3763 if (err && un->un_status != KEY_ILLEGAL_REQUEST) { 3764 /* 3765 * note that st_intr has already set 3766 * un_pos.pmode to invalid. 3767 */ 3768 un->un_density_known = 0; 3769 } 3770 3771 #ifdef __x86 3772 /* 3773 * free any contiguous mem alloc'ed for big block I/O 3774 */ 3775 cp = un->un_contig_mem; 3776 while (cp) { 3777 if (cp->cm_addr) { 3778 ddi_dma_mem_free(&cp->cm_acc_hdl); 3779 } 3780 cp_temp = cp; 3781 cp = cp->cm_next; 3782 kmem_free(cp_temp, 3783 sizeof (struct contig_mem) + biosize()); 3784 } 3785 un->un_contig_mem_total_num = 0; 3786 un->un_contig_mem_available_num = 0; 3787 un->un_contig_mem = NULL; 3788 un->un_max_contig_mem_len = 0; 3789 #endif 3790 3791 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 3792 "st_close3: return val = %x, fileno=%x, blkno=%x, eof=%x\n", 3793 err, un->un_pos.fileno, un->un_pos.blkno, un->un_pos.eof); 3794 3795 mutex_exit(ST_MUTEX); 3796 return (err); 3797 } 3798 3799 /* 3800 * These routines perform raw i/o operations. 3801 */ 3802 3803 /* ARGSUSED2 */ 3804 static int 3805 st_aread(dev_t dev, struct aio_req *aio, cred_t *cred_p) 3806 { 3807 #ifdef STDEBUG 3808 GET_SOFT_STATE(dev); 3809 ST_ENTR(ST_DEVINFO, st_aread); 3810 #endif 3811 return (st_arw(dev, aio, B_READ)); 3812 } 3813 3814 3815 /* ARGSUSED2 */ 3816 static int 3817 st_awrite(dev_t dev, struct aio_req *aio, cred_t *cred_p) 3818 { 3819 #ifdef STDEBUG 3820 GET_SOFT_STATE(dev); 3821 ST_ENTR(ST_DEVINFO, st_awrite); 3822 #endif 3823 return (st_arw(dev, aio, B_WRITE)); 3824 } 3825 3826 3827 3828 /* ARGSUSED */ 3829 static int 3830 st_read(dev_t dev, struct uio *uiop, cred_t *cred_p) 3831 { 3832 #ifdef STDEBUG 3833 GET_SOFT_STATE(dev); 3834 ST_ENTR(ST_DEVINFO, st_read); 3835 #endif 3836 return (st_rw(dev, uiop, B_READ)); 3837 } 3838 3839 /* ARGSUSED */ 3840 static int 3841 st_write(dev_t dev, struct uio *uiop, cred_t *cred_p) 3842 { 3843 #ifdef STDEBUG 3844 GET_SOFT_STATE(dev); 3845 ST_ENTR(ST_DEVINFO, st_write); 3846 #endif 3847 return (st_rw(dev, uiop, B_WRITE)); 3848 } 3849 3850 /* 3851 * Due to historical reasons, old limits are: For variable-length devices: 3852 * if greater than 64KB - 1 (ST_MAXRECSIZE_VARIABLE), block into 64 KB - 2 3853 * ST_MAXRECSIZE_VARIABLE_LIMIT) requests; otherwise, 3854 * (let it through unmodified. For fixed-length record devices: 3855 * 63K (ST_MAXRECSIZE_FIXED) is max (default minphys). 3856 * 3857 * The new limits used are un_maxdma (retrieved using scsi_ifgetcap() 3858 * from the HBA) and un_maxbsize (retrieved by sending SCMD_READ_BLKLIM 3859 * command to the drive). 3860 * 3861 */ 3862 static void 3863 st_minphys(struct buf *bp) 3864 { 3865 struct scsi_tape *un; 3866 3867 un = ddi_get_soft_state(st_state, MTUNIT(bp->b_edev)); 3868 3869 ST_FUNC(ST_DEVINFO, st_minphys); 3870 3871 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 3872 "st_minphys(bp = 0x%p): b_bcount = 0x%lx\n", (void *)bp, 3873 bp->b_bcount); 3874 3875 if (un->un_allow_large_xfer) { 3876 3877 /* 3878 * check un_maxbsize for variable length devices only 3879 */ 3880 if (un->un_bsize == 0 && bp->b_bcount > un->un_maxbsize) { 3881 bp->b_bcount = un->un_maxbsize; 3882 } 3883 /* 3884 * can't go more that HBA maxdma limit in either fixed-length 3885 * or variable-length tape drives. 3886 */ 3887 if (bp->b_bcount > un->un_maxdma) { 3888 bp->b_bcount = un->un_maxdma; 3889 } 3890 } else { 3891 3892 /* 3893 * use old fixed limits 3894 */ 3895 if (un->un_bsize == 0) { 3896 if (bp->b_bcount > ST_MAXRECSIZE_VARIABLE) { 3897 bp->b_bcount = ST_MAXRECSIZE_VARIABLE_LIMIT; 3898 } 3899 } else { 3900 if (bp->b_bcount > ST_MAXRECSIZE_FIXED) { 3901 bp->b_bcount = ST_MAXRECSIZE_FIXED; 3902 } 3903 } 3904 } 3905 3906 /* 3907 * For regular raw I/O and Fixed Block length devices, make sure 3908 * the adjusted block count is a whole multiple of the device 3909 * block size. 3910 */ 3911 if (bp != un->un_sbufp && un->un_bsize) { 3912 bp->b_bcount -= (bp->b_bcount % un->un_bsize); 3913 } 3914 } 3915 3916 static int 3917 st_rw(dev_t dev, struct uio *uio, int flag) 3918 { 3919 int rval = 0; 3920 long len; 3921 3922 GET_SOFT_STATE(dev); 3923 3924 ST_FUNC(ST_DEVINFO, st_rw); 3925 3926 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 3927 "st_rw(dev = 0x%lx, flag = %s)\n", dev, 3928 (flag == B_READ ? rd_str: wr_str)); 3929 3930 /* get local copy of transfer length */ 3931 len = uio->uio_iov->iov_len; 3932 3933 mutex_enter(ST_MUTEX); 3934 3935 /* 3936 * Clear error entry stack 3937 */ 3938 st_empty_error_stack(un); 3939 3940 /* 3941 * If in fixed block size mode and requested read or write 3942 * is not an even multiple of that block size. 3943 */ 3944 if ((un->un_bsize != 0) && (len % un->un_bsize != 0)) { 3945 scsi_log(ST_DEVINFO, st_label, CE_WARN, 3946 "%s: not modulo %d block size\n", 3947 (flag == B_WRITE) ? wr_str : rd_str, un->un_bsize); 3948 rval = EINVAL; 3949 } 3950 3951 /* If device has set granularity in the READ_BLKLIM we honor it. */ 3952 if ((un->un_data_mod != 0) && (len % un->un_data_mod != 0)) { 3953 scsi_log(ST_DEVINFO, st_label, CE_WARN, 3954 "%s: not modulo %d device granularity\n", 3955 (flag == B_WRITE) ? wr_str : rd_str, un->un_data_mod); 3956 rval = EINVAL; 3957 } 3958 3959 if (st_recov_sz != sizeof (recov_info) && un->un_multipath) { 3960 scsi_log(ST_DEVINFO, st_label, CE_WARN, mp_misconf); 3961 rval = EFAULT; 3962 } 3963 3964 if (rval != 0) { 3965 un->un_errno = rval; 3966 mutex_exit(ST_MUTEX); 3967 return (rval); 3968 } 3969 3970 /* 3971 * Reset this so it can be set if Berkeley and read over a filemark. 3972 */ 3973 un->un_silent_skip = 0; 3974 mutex_exit(ST_MUTEX); 3975 3976 len = uio->uio_resid; 3977 3978 rval = physio(st_queued_strategy, (struct buf *)NULL, 3979 dev, flag, st_minphys, uio); 3980 /* 3981 * if we have hit logical EOT during this xfer and there is not a 3982 * full residue, then set eof back to ST_EOM to make sure that 3983 * the user will see at least one zero write 3984 * after this short write 3985 */ 3986 mutex_enter(ST_MUTEX); 3987 if (un->un_pos.eof > ST_NO_EOF) { 3988 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 3989 "eof=%d resid=%lx\n", un->un_pos.eof, uio->uio_resid); 3990 } 3991 if (un->un_pos.eof >= ST_EOM && (flag == B_WRITE)) { 3992 if ((uio->uio_resid != len) && (uio->uio_resid != 0)) { 3993 un->un_pos.eof = ST_EOM; 3994 } else if (uio->uio_resid == len) { 3995 un->un_pos.eof = ST_NO_EOF; 3996 } 3997 } 3998 3999 if (un->un_silent_skip && uio->uio_resid != len) { 4000 un->un_pos.eof = ST_EOF; 4001 un->un_pos.blkno = un->un_save_blkno; 4002 un->un_pos.fileno--; 4003 } 4004 4005 un->un_errno = rval; 4006 4007 mutex_exit(ST_MUTEX); 4008 4009 return (rval); 4010 } 4011 4012 static int 4013 st_arw(dev_t dev, struct aio_req *aio, int flag) 4014 { 4015 struct uio *uio = aio->aio_uio; 4016 int rval = 0; 4017 long len; 4018 4019 GET_SOFT_STATE(dev); 4020 4021 ST_FUNC(ST_DEVINFO, st_arw); 4022 4023 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 4024 "st_arw(dev = 0x%lx, flag = %s)\n", dev, 4025 (flag == B_READ ? rd_str: wr_str)); 4026 4027 /* get local copy of transfer length */ 4028 len = uio->uio_iov->iov_len; 4029 4030 mutex_enter(ST_MUTEX); 4031 4032 /* 4033 * If in fixed block size mode and requested read or write 4034 * is not an even multiple of that block size. 4035 */ 4036 if ((un->un_bsize != 0) && (len % un->un_bsize != 0)) { 4037 scsi_log(ST_DEVINFO, st_label, CE_WARN, 4038 "%s: not modulo %d block size\n", 4039 (flag == B_WRITE) ? wr_str : rd_str, un->un_bsize); 4040 rval = EINVAL; 4041 } 4042 4043 /* If device has set granularity in the READ_BLKLIM we honor it. */ 4044 if ((un->un_data_mod != 0) && (len % un->un_data_mod != 0)) { 4045 scsi_log(ST_DEVINFO, st_label, CE_WARN, 4046 "%s: not modulo %d device granularity\n", 4047 (flag == B_WRITE) ? wr_str : rd_str, un->un_data_mod); 4048 rval = EINVAL; 4049 } 4050 4051 if (st_recov_sz != sizeof (recov_info) && un->un_multipath) { 4052 scsi_log(ST_DEVINFO, st_label, CE_WARN, mp_misconf); 4053 rval = EFAULT; 4054 } 4055 4056 if (rval != 0) { 4057 un->un_errno = rval; 4058 mutex_exit(ST_MUTEX); 4059 return (rval); 4060 } 4061 4062 mutex_exit(ST_MUTEX); 4063 4064 len = uio->uio_resid; 4065 4066 rval = 4067 aphysio(st_queued_strategy, anocancel, dev, flag, st_minphys, aio); 4068 4069 /* 4070 * if we have hit logical EOT during this xfer and there is not a 4071 * full residue, then set eof back to ST_EOM to make sure that 4072 * the user will see at least one zero write 4073 * after this short write 4074 * 4075 * we keep this here just in case the application is not using 4076 * persistent errors 4077 */ 4078 mutex_enter(ST_MUTEX); 4079 if (un->un_pos.eof > ST_NO_EOF) { 4080 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4081 "eof=%d resid=%lx\n", un->un_pos.eof, uio->uio_resid); 4082 } 4083 if (un->un_pos.eof >= ST_EOM && (flag == B_WRITE)) { 4084 if ((uio->uio_resid != len) && (uio->uio_resid != 0)) { 4085 un->un_pos.eof = ST_EOM; 4086 } else if (uio->uio_resid == len && 4087 !(un->un_persistence && un->un_persist_errors)) { 4088 un->un_pos.eof = ST_NO_EOF; 4089 } 4090 } 4091 un->un_errno = rval; 4092 mutex_exit(ST_MUTEX); 4093 4094 return (rval); 4095 } 4096 4097 4098 4099 static int 4100 st_queued_strategy(buf_t *bp) 4101 { 4102 struct scsi_tape *un; 4103 char reading = bp->b_flags & B_READ; 4104 int wasopening = 0; 4105 4106 /* 4107 * validate arguments 4108 */ 4109 un = ddi_get_soft_state(st_state, MTUNIT(bp->b_edev)); 4110 if (un == NULL) { 4111 bp->b_resid = bp->b_bcount; 4112 bioerror(bp, ENXIO); 4113 ST_DEBUG6(NULL, st_label, SCSI_DEBUG, 4114 "st_queued_strategy: ENXIO error exit\n"); 4115 biodone(bp); 4116 return (0); 4117 } 4118 4119 ST_ENTR(ST_DEVINFO, st_queued_strategy); 4120 4121 mutex_enter(ST_MUTEX); 4122 4123 while (un->un_pwr_mgmt == ST_PWR_SUSPENDED) { 4124 cv_wait(&un->un_suspend_cv, ST_MUTEX); 4125 } 4126 4127 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 4128 "st_queued_strategy(): bcount=0x%lx, fileno=%d, blkno=%x, eof=%d\n", 4129 bp->b_bcount, un->un_pos.fileno, un->un_pos.blkno, un->un_pos.eof); 4130 4131 /* 4132 * If persistent errors have been flagged, just nix this one. We wait 4133 * for any outstanding I/O's below, so we will be in order. 4134 */ 4135 if (un->un_persistence && un->un_persist_errors) { 4136 goto exit; 4137 } 4138 4139 /* 4140 * If last command was non queued, wait till it finishes. 4141 */ 4142 while (un->un_sbuf_busy) { 4143 cv_wait(&un->un_sbuf_cv, ST_MUTEX); 4144 /* woke up because of an error */ 4145 if (un->un_persistence && un->un_persist_errors) { 4146 goto exit; 4147 } 4148 } 4149 4150 /* 4151 * s_buf and recovery commands shouldn't come here. 4152 */ 4153 ASSERT(bp != un->un_recov_buf); 4154 ASSERT(bp != un->un_sbufp); 4155 4156 /* 4157 * If we haven't done/checked reservation on the tape unit 4158 * do it now. 4159 */ 4160 if ((un->un_rsvd_status & 4161 (ST_RESERVE | ST_APPLICATION_RESERVATIONS)) == 0) { 4162 if ((un->un_dp->options & ST_NO_RESERVE_RELEASE) == 0) { 4163 if (st_reserve_release(un, ST_RESERVE, st_uscsi_cmd)) { 4164 st_bioerror(bp, un->un_errno); 4165 goto exit; 4166 } 4167 } else if (un->un_state == ST_STATE_OPEN_PENDING_IO) { 4168 /* 4169 * Enter here to restore position for possible 4170 * resets when the device was closed and opened 4171 * in O_NDELAY mode subsequently 4172 */ 4173 un->un_state = ST_STATE_INITIALIZING; 4174 (void) st_cmd(un, SCMD_TEST_UNIT_READY, 4175 0, SYNC_CMD); 4176 un->un_state = ST_STATE_OPEN_PENDING_IO; 4177 } 4178 un->un_rsvd_status |= ST_INIT_RESERVE; 4179 } 4180 4181 /* 4182 * If we are offline, we have to initialize everything first. 4183 * This is to handle either when opened with O_NDELAY, or 4184 * we just got a new tape in the drive, after an offline. 4185 * We don't observe O_NDELAY past the open, 4186 * as it will not make sense for tapes. 4187 */ 4188 if (un->un_state == ST_STATE_OFFLINE || un->un_restore_pos) { 4189 /* 4190 * reset state to avoid recursion 4191 */ 4192 un->un_laststate = un->un_state; 4193 un->un_state = ST_STATE_INITIALIZING; 4194 if (st_tape_init(un)) { 4195 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4196 "stioctl : OFFLINE init failure "); 4197 un->un_state = ST_STATE_OFFLINE; 4198 un->un_pos.pmode = invalid; 4199 goto b_done_err; 4200 } 4201 /* un_restore_pos make invalid */ 4202 un->un_state = ST_STATE_OPEN_PENDING_IO; 4203 un->un_restore_pos = 0; 4204 } 4205 /* 4206 * Check for legal operations 4207 */ 4208 if (un->un_pos.pmode == invalid) { 4209 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4210 "strategy with un->un_pos.pmode invalid\n"); 4211 goto b_done_err; 4212 } 4213 4214 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4215 "st_queued_strategy(): regular io\n"); 4216 4217 /* 4218 * Process this first. If we were reading, and we're pending 4219 * logical eot, that means we've bumped one file mark too far. 4220 */ 4221 4222 /* 4223 * Recursion warning: st_cmd will route back through here. 4224 * Not anymore st_cmd will go through st_strategy()! 4225 */ 4226 if (un->un_pos.eof == ST_EOT_PENDING) { 4227 if (st_cmd(un, SCMD_SPACE, Fmk(-1), SYNC_CMD)) { 4228 un->un_pos.pmode = invalid; 4229 un->un_density_known = 0; 4230 goto b_done_err; 4231 } 4232 un->un_pos.blkno = 0; /* fix up block number.. */ 4233 un->un_pos.eof = ST_EOT; 4234 } 4235 4236 /* 4237 * If we are in the process of opening, we may have to 4238 * determine/set the correct density. We also may have 4239 * to do a test_append (if QIC) to see whether we are 4240 * in a position to append to the end of the tape. 4241 * 4242 * If we're already at logical eot, we transition 4243 * to ST_NO_EOF. If we're at physical eot, we punt 4244 * to the switch statement below to handle. 4245 */ 4246 if ((un->un_state == ST_STATE_OPEN_PENDING_IO) || 4247 (un->un_test_append && (un->un_dp->options & ST_QIC))) { 4248 4249 if (un->un_state == ST_STATE_OPEN_PENDING_IO) { 4250 if (st_determine_density(un, (int)reading)) { 4251 goto b_done_err; 4252 } 4253 } 4254 4255 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4256 "pending_io@fileno %d rw %d qic %d eof %d\n", 4257 un->un_pos.fileno, (int)reading, 4258 (un->un_dp->options & ST_QIC) ? 1 : 0, 4259 un->un_pos.eof); 4260 4261 if (!reading && un->un_pos.eof != ST_EOM) { 4262 if (un->un_pos.eof == ST_EOT) { 4263 un->un_pos.eof = ST_NO_EOF; 4264 } else if (un->un_pos.pmode != invalid && 4265 (un->un_dp->options & ST_QIC)) { 4266 /* 4267 * st_test_append() will do it all 4268 */ 4269 st_test_append(bp); 4270 mutex_exit(ST_MUTEX); 4271 return (0); 4272 } 4273 } 4274 if (un->un_state == ST_STATE_OPEN_PENDING_IO) { 4275 wasopening = 1; 4276 } 4277 un->un_laststate = un->un_state; 4278 un->un_state = ST_STATE_OPEN; 4279 } 4280 4281 4282 /* 4283 * Process rest of END OF FILE and END OF TAPE conditions 4284 */ 4285 4286 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4287 "eof=%x, wasopening=%x\n", 4288 un->un_pos.eof, wasopening); 4289 4290 switch (un->un_pos.eof) { 4291 case ST_EOM: 4292 /* 4293 * This allows writes to proceed past physical 4294 * eot. We'll *really* be in trouble if the 4295 * user continues blindly writing data too 4296 * much past this point (unwind the tape). 4297 * Physical eot really means 'early warning 4298 * eot' in this context. 4299 * 4300 * Every other write from now on will succeed 4301 * (if sufficient tape left). 4302 * This write will return with resid == count 4303 * but the next one should be successful 4304 * 4305 * Note that we only transition to logical EOT 4306 * if the last state wasn't the OPENING state. 4307 * We explicitly prohibit running up to physical 4308 * eot, closing the device, and then re-opening 4309 * to proceed. Trailer records may only be gotten 4310 * at by keeping the tape open after hitting eot. 4311 * 4312 * Also note that ST_EOM cannot be set by reading- 4313 * this can only be set during writing. Reading 4314 * up to the end of the tape gets a blank check 4315 * or a double-filemark indication (ST_EOT_PENDING), 4316 * and we prohibit reading after that point. 4317 * 4318 */ 4319 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "EOM\n"); 4320 if (wasopening == 0) { 4321 /* 4322 * this allows st_rw() to reset it back to 4323 * will see a zero write 4324 */ 4325 un->un_pos.eof = ST_WRITE_AFTER_EOM; 4326 } 4327 un->un_status = SUN_KEY_EOT; 4328 goto b_done; 4329 4330 case ST_WRITE_AFTER_EOM: 4331 case ST_EOT: 4332 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "EOT\n"); 4333 un->un_status = SUN_KEY_EOT; 4334 if (SVR4_BEHAVIOR && reading) { 4335 goto b_done_err; 4336 } 4337 4338 if (reading) { 4339 goto b_done; 4340 } 4341 un->un_pos.eof = ST_NO_EOF; 4342 break; 4343 4344 case ST_EOF_PENDING: 4345 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4346 "EOF PENDING\n"); 4347 un->un_status = SUN_KEY_EOF; 4348 if (SVR4_BEHAVIOR) { 4349 un->un_pos.eof = ST_EOF; 4350 goto b_done; 4351 } 4352 /* FALLTHROUGH */ 4353 case ST_EOF: 4354 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "EOF\n"); 4355 un->un_status = SUN_KEY_EOF; 4356 if (SVR4_BEHAVIOR) { 4357 goto b_done_err; 4358 } 4359 4360 if (BSD_BEHAVIOR) { 4361 un->un_pos.eof = ST_NO_EOF; 4362 un->un_pos.fileno += 1; 4363 un->un_pos.blkno = 0; 4364 } 4365 4366 if (reading) { 4367 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4368 "now file %d (read)\n", 4369 un->un_pos.fileno); 4370 goto b_done; 4371 } 4372 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4373 "now file %d (write)\n", un->un_pos.fileno); 4374 break; 4375 default: 4376 un->un_status = 0; 4377 break; 4378 } 4379 4380 bp->b_flags &= ~(B_DONE); 4381 st_bioerror(bp, 0); 4382 bp->av_forw = NULL; 4383 bp->b_resid = 0; 4384 SET_BP_PKT(bp, 0); 4385 4386 4387 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4388 "st_queued_strategy: cmd=0x%p count=%ld resid=%ld flags=0x%x" 4389 " pkt=0x%p\n", 4390 (void *)bp->b_forw, bp->b_bcount, 4391 bp->b_resid, bp->b_flags, (void *)BP_PKT(bp)); 4392 4393 #ifdef __x86 4394 /* 4395 * We will replace bp with a new bp that can do big blk xfer 4396 * if the requested xfer size is bigger than un->un_maxdma_arch 4397 * 4398 * Also, we need to make sure that we're handling real I/O 4399 * by checking group 0/1 SCSI I/O commands, if needed 4400 */ 4401 if (bp->b_bcount > un->un_maxdma_arch && 4402 ((uchar_t)(uintptr_t)bp->b_forw == SCMD_READ || 4403 (uchar_t)(uintptr_t)bp->b_forw == SCMD_READ_G4 || 4404 (uchar_t)(uintptr_t)bp->b_forw == SCMD_WRITE || 4405 (uchar_t)(uintptr_t)bp->b_forw == SCMD_WRITE_G4)) { 4406 mutex_exit(ST_MUTEX); 4407 bp = st_get_bigblk_bp(bp); 4408 mutex_enter(ST_MUTEX); 4409 } 4410 #endif 4411 4412 /* put on wait queue */ 4413 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4414 "st_queued_strategy: un->un_quef = 0x%p, bp = 0x%p\n", 4415 (void *)un->un_quef, (void *)bp); 4416 4417 st_add_to_queue(&un->un_quef, &un->un_quel, un->un_quel, bp); 4418 4419 ST_DO_KSTATS(bp, kstat_waitq_enter); 4420 4421 st_start(un); 4422 4423 mutex_exit(ST_MUTEX); 4424 return (0); 4425 4426 b_done_err: 4427 st_bioerror(bp, EIO); 4428 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4429 "st_queued_strategy : EIO b_done_err\n"); 4430 4431 b_done: 4432 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4433 "st_queued_strategy: b_done\n"); 4434 4435 exit: 4436 /* 4437 * make sure no commands are outstanding or waiting before closing, 4438 * so we can guarantee order 4439 */ 4440 st_wait_for_io(un); 4441 un->un_err_resid = bp->b_resid = bp->b_bcount; 4442 4443 /* override errno here, if persistent errors were flagged */ 4444 if (un->un_persistence && un->un_persist_errors) 4445 bioerror(bp, un->un_errno); 4446 4447 mutex_exit(ST_MUTEX); 4448 4449 biodone(bp); 4450 ASSERT(mutex_owned(ST_MUTEX) == 0); 4451 return (0); 4452 } 4453 4454 4455 static int 4456 st_strategy(struct buf *bp) 4457 { 4458 struct scsi_tape *un; 4459 4460 /* 4461 * validate arguments 4462 */ 4463 un = ddi_get_soft_state(st_state, MTUNIT(bp->b_edev)); 4464 if (un == NULL) { 4465 bp->b_resid = bp->b_bcount; 4466 bioerror(bp, ENXIO); 4467 ST_DEBUG6(NULL, st_label, SCSI_DEBUG, 4468 "st_strategy: ENXIO error exit\n"); 4469 4470 biodone(bp); 4471 return (0); 4472 4473 } 4474 4475 ST_ENTR(ST_DEVINFO, st_strategy); 4476 4477 mutex_enter(ST_MUTEX); 4478 4479 while (un->un_pwr_mgmt == ST_PWR_SUSPENDED) { 4480 cv_wait(&un->un_suspend_cv, ST_MUTEX); 4481 } 4482 4483 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 4484 "st_strategy(): bcount=0x%lx, fileno=%d, blkno=%x, eof=%d\n", 4485 bp->b_bcount, un->un_pos.fileno, un->un_pos.blkno, un->un_pos.eof); 4486 4487 ASSERT((bp == un->un_recov_buf) || (bp == un->un_sbufp)); 4488 4489 bp->b_flags &= ~(B_DONE); 4490 st_bioerror(bp, 0); 4491 bp->av_forw = NULL; 4492 bp->b_resid = 0; 4493 SET_BP_PKT(bp, 0); 4494 4495 4496 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4497 "st_strategy: cmd=0x%x count=%ld resid=%ld flags=0x%x" 4498 " pkt=0x%p\n", 4499 (unsigned char)(uintptr_t)bp->b_forw, bp->b_bcount, 4500 bp->b_resid, bp->b_flags, (void *)BP_PKT(bp)); 4501 ST_DO_KSTATS(bp, kstat_waitq_enter); 4502 4503 st_start(un); 4504 4505 mutex_exit(ST_MUTEX); 4506 return (0); 4507 } 4508 4509 /* 4510 * this routine spaces forward over filemarks 4511 */ 4512 static int 4513 st_space_fmks(struct scsi_tape *un, int64_t count) 4514 { 4515 int rval = 0; 4516 4517 ST_FUNC(ST_DEVINFO, st_space_fmks); 4518 4519 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 4520 "st_space_fmks(dev = 0x%lx, count = %"PRIx64")\n", 4521 un->un_dev, count); 4522 4523 ASSERT(mutex_owned(ST_MUTEX)); 4524 4525 /* 4526 * the risk with doing only one space operation is that we 4527 * may accidentily jump in old data 4528 * the exabyte 8500 reading 8200 tapes cannot use KNOWS_EOD 4529 * because the 8200 does not append a marker; in order not to 4530 * sacrifice the fast file skip, we do a slow skip if the low 4531 * density device has been opened 4532 */ 4533 4534 if ((un->un_dp->options & ST_KNOWS_EOD) && 4535 !((un->un_dp->type == ST_TYPE_EXB8500 && 4536 MT_DENSITY(un->un_dev) == 0))) { 4537 if (st_cmd(un, SCMD_SPACE, Fmk(count), SYNC_CMD)) { 4538 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4539 "space_fmks : EIO can't do space cmd #1\n"); 4540 rval = EIO; 4541 } 4542 } else { 4543 while (count > 0) { 4544 if (st_cmd(un, SCMD_SPACE, Fmk(1), SYNC_CMD)) { 4545 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4546 "space_fmks : EIO can't do space cmd #2\n"); 4547 rval = EIO; 4548 break; 4549 } 4550 count -= 1; 4551 /* 4552 * read a block to see if we have reached 4553 * end of medium (double filemark for reel or 4554 * medium error for others) 4555 */ 4556 if (count > 0) { 4557 if (st_cmd(un, SCMD_SPACE, Blk(1), SYNC_CMD)) { 4558 ST_DEBUG2(ST_DEVINFO, st_label, 4559 SCSI_DEBUG, 4560 "space_fmks : EIO can't do " 4561 "space cmd #3\n"); 4562 rval = EIO; 4563 break; 4564 } 4565 if ((un->un_pos.eof >= ST_EOF_PENDING) && 4566 (un->un_dp->options & ST_REEL)) { 4567 un->un_status = SUN_KEY_EOT; 4568 ST_DEBUG2(ST_DEVINFO, st_label, 4569 SCSI_DEBUG, 4570 "space_fmks : EIO ST_REEL\n"); 4571 rval = EIO; 4572 break; 4573 } else if (IN_EOF(un->un_pos)) { 4574 un->un_pos.eof = ST_NO_EOF; 4575 un->un_pos.fileno++; 4576 un->un_pos.blkno = 0; 4577 count--; 4578 } else if (un->un_pos.eof > ST_EOF) { 4579 ST_DEBUG2(ST_DEVINFO, st_label, 4580 SCSI_DEBUG, 4581 "space_fmks, EIO > ST_EOF\n"); 4582 rval = EIO; 4583 break; 4584 } 4585 4586 } 4587 } 4588 un->un_err_resid = count; 4589 COPY_POS(&un->un_pos, &un->un_err_pos); 4590 } 4591 ASSERT(mutex_owned(ST_MUTEX)); 4592 return (rval); 4593 } 4594 4595 /* 4596 * this routine spaces to EOD 4597 * 4598 * it keeps track of the current filenumber and returns the filenumber after 4599 * the last successful space operation, we keep the number high because as 4600 * tapes are getting larger, the possibility of more and more files exist, 4601 * 0x100000 (1 Meg of files) probably will never have to be changed any time 4602 * soon 4603 */ 4604 #define MAX_SKIP 0x100000 /* somewhat arbitrary */ 4605 4606 static int 4607 st_find_eod(struct scsi_tape *un) 4608 { 4609 tapepos_t savepos; 4610 int64_t sp_type; 4611 int result; 4612 4613 if (un == NULL) { 4614 return (-1); 4615 } 4616 4617 ST_FUNC(ST_DEVINFO, st_find_eod); 4618 4619 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 4620 "st_find_eod(dev = 0x%lx): fileno = %d\n", un->un_dev, 4621 un->un_pos.fileno); 4622 4623 ASSERT(mutex_owned(ST_MUTEX)); 4624 4625 COPY_POS(&savepos, &un->un_pos); 4626 4627 /* 4628 * see if the drive is smart enough to do the skips in 4629 * one operation; 1/2" use two filemarks 4630 * the exabyte 8500 reading 8200 tapes cannot use KNOWS_EOD 4631 * because the 8200 does not append a marker; in order not to 4632 * sacrifice the fast file skip, we do a slow skip if the low 4633 * density device has been opened 4634 */ 4635 if ((un->un_dp->options & ST_KNOWS_EOD) != 0) { 4636 if ((un->un_dp->type == ST_TYPE_EXB8500) && 4637 (MT_DENSITY(un->un_dev) == 0)) { 4638 sp_type = Fmk(1); 4639 } else if (un->un_pos.pmode == logical) { 4640 sp_type = SPACE(SP_EOD, 0); 4641 } else { 4642 sp_type = Fmk(MAX_SKIP); 4643 } 4644 } else { 4645 sp_type = Fmk(1); 4646 } 4647 4648 for (;;) { 4649 result = st_cmd(un, SCMD_SPACE, sp_type, SYNC_CMD); 4650 4651 if (result == 0) { 4652 COPY_POS(&savepos, &un->un_pos); 4653 } 4654 4655 if (sp_type == SPACE(SP_EOD, 0)) { 4656 if (result != 0) { 4657 sp_type = Fmk(MAX_SKIP); 4658 continue; 4659 } 4660 4661 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4662 "st_find_eod: 0x%"PRIx64"\n", 4663 savepos.lgclblkno); 4664 /* 4665 * What we return will become the current file position. 4666 * After completing the space command with the position 4667 * mode that is not invalid a read position command will 4668 * be automaticly issued. If the drive support the long 4669 * read position format a valid file position can be 4670 * returned. 4671 */ 4672 return (un->un_pos.fileno); 4673 } 4674 4675 if (result != 0) { 4676 break; 4677 } 4678 4679 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4680 "count=%"PRIx64", eof=%x, status=%x\n", 4681 SPACE_CNT(sp_type), un->un_pos.eof, un->un_status); 4682 4683 /* 4684 * If we're not EOM smart, space a record 4685 * to see whether we're now in the slot between 4686 * the two sequential filemarks that logical 4687 * EOM consists of (REEL) or hit nowhere land 4688 * (8mm). 4689 */ 4690 if (sp_type == Fmk(1)) { 4691 /* 4692 * no fast skipping, check a record 4693 */ 4694 if (st_cmd(un, SCMD_SPACE, Blk((1)), SYNC_CMD)) { 4695 break; 4696 } 4697 if ((un->un_pos.eof >= ST_EOF_PENDING) && 4698 (un->un_dp->options & ST_REEL)) { 4699 un->un_status = KEY_BLANK_CHECK; 4700 un->un_pos.fileno++; 4701 un->un_pos.blkno = 0; 4702 break; 4703 } 4704 if (IN_EOF(un->un_pos)) { 4705 un->un_pos.eof = ST_NO_EOF; 4706 un->un_pos.fileno++; 4707 un->un_pos.blkno = 0; 4708 } 4709 if (un->un_pos.eof > ST_EOF) { 4710 break; 4711 } 4712 } else { 4713 if (un->un_pos.eof > ST_EOF) { 4714 break; 4715 } 4716 } 4717 } 4718 4719 if (un->un_dp->options & ST_KNOWS_EOD) { 4720 COPY_POS(&savepos, &un->un_pos); 4721 } 4722 4723 ASSERT(mutex_owned(ST_MUTEX)); 4724 4725 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4726 "st_find_eod: %x\n", savepos.fileno); 4727 return (savepos.fileno); 4728 } 4729 4730 4731 /* 4732 * this routine is frequently used in ioctls below; 4733 * it determines whether we know the density and if not will 4734 * determine it 4735 * if we have written the tape before, one or more filemarks are written 4736 * 4737 * depending on the stepflag, the head is repositioned to where it was before 4738 * the filemarks were written in order not to confuse step counts 4739 */ 4740 #define STEPBACK 0 4741 #define NO_STEPBACK 1 4742 4743 static int 4744 st_check_density_or_wfm(dev_t dev, int wfm, int mode, int stepflag) 4745 { 4746 4747 GET_SOFT_STATE(dev); 4748 4749 ST_FUNC(ST_DEVINFO, st_check_density_or_wfm); 4750 4751 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 4752 "st_check_density_or_wfm(dev= 0x%lx, wfm= %d, mode= %d, stpflg= %d)" 4753 "\n", dev, wfm, mode, stepflag); 4754 4755 ASSERT(mutex_owned(ST_MUTEX)); 4756 4757 /* 4758 * If we don't yet know the density of the tape we have inserted, 4759 * we have to either unconditionally set it (if we're 'writing'), 4760 * or we have to determine it. As side effects, check for any 4761 * write-protect errors, and for the need to put out any file-marks 4762 * before positioning a tape. 4763 * 4764 * If we are going to be spacing forward, and we haven't determined 4765 * the tape density yet, we have to do so now... 4766 */ 4767 if (un->un_state == ST_STATE_OPEN_PENDING_IO) { 4768 if (st_determine_density(un, mode)) { 4769 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4770 "check_density_or_wfm : EIO can't determine " 4771 "density\n"); 4772 un->un_errno = EIO; 4773 return (EIO); 4774 } 4775 /* 4776 * Presumably we are at BOT. If we attempt to write, it will 4777 * either work okay, or bomb. We don't do a st_test_append 4778 * unless we're past BOT. 4779 */ 4780 un->un_laststate = un->un_state; 4781 un->un_state = ST_STATE_OPEN; 4782 4783 } else if (un->un_pos.pmode != invalid && un->un_fmneeded > 0 && 4784 ((un->un_lastop == ST_OP_WEOF && wfm) || 4785 (un->un_lastop == ST_OP_WRITE && wfm))) { 4786 4787 tapepos_t spos; 4788 4789 COPY_POS(&spos, &un->un_pos); 4790 4791 /* 4792 * We need to write one or two filemarks. 4793 * In the case of the HP, we need to 4794 * position the head between the two 4795 * marks. 4796 */ 4797 if ((un->un_fmneeded > 0) || (un->un_lastop == ST_OP_WEOF)) { 4798 wfm = un->un_fmneeded; 4799 un->un_fmneeded = 0; 4800 } 4801 4802 if (st_write_fm(dev, wfm)) { 4803 un->un_pos.pmode = invalid; 4804 un->un_density_known = 0; 4805 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4806 "check_density_or_wfm : EIO can't write fm\n"); 4807 un->un_errno = EIO; 4808 return (EIO); 4809 } 4810 4811 if (stepflag == STEPBACK) { 4812 if (st_cmd(un, SCMD_SPACE, Fmk(-wfm), SYNC_CMD)) { 4813 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4814 "check_density_or_wfm : EIO can't space " 4815 "(-wfm)\n"); 4816 un->un_errno = EIO; 4817 return (EIO); 4818 } 4819 COPY_POS(&un->un_pos, &spos); 4820 } 4821 } 4822 4823 /* 4824 * Whatever we do at this point clears the state of the eof flag. 4825 */ 4826 4827 un->un_pos.eof = ST_NO_EOF; 4828 4829 /* 4830 * If writing, let's check that we're positioned correctly 4831 * at the end of tape before issuing the next write. 4832 */ 4833 if (un->un_read_only == RDWR) { 4834 un->un_test_append = 1; 4835 } 4836 4837 ASSERT(mutex_owned(ST_MUTEX)); 4838 return (0); 4839 } 4840 4841 4842 /* 4843 * Wait for all outstaning I/O's to complete 4844 * 4845 * we wait on both ncmds and the wait queue for times when we are flushing 4846 * after persistent errors are flagged, which is when ncmds can be 0, and the 4847 * queue can still have I/O's. This way we preserve order of biodone's. 4848 */ 4849 static void 4850 st_wait_for_io(struct scsi_tape *un) 4851 { 4852 ST_FUNC(ST_DEVINFO, st_wait_for_io); 4853 ASSERT(mutex_owned(ST_MUTEX)); 4854 while ((un->un_ncmds) || (un->un_quef) || (un->un_runqf)) { 4855 cv_wait(&un->un_queue_cv, ST_MUTEX); 4856 } 4857 } 4858 4859 /* 4860 * This routine implements the ioctl calls. It is called 4861 * from the device switch at normal priority. 4862 */ 4863 /*ARGSUSED*/ 4864 static int 4865 st_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p, 4866 int *rval_p) 4867 { 4868 int tmp, rval = 0; 4869 4870 GET_SOFT_STATE(dev); 4871 4872 ST_ENTR(ST_DEVINFO, st_ioctl); 4873 4874 mutex_enter(ST_MUTEX); 4875 4876 ASSERT(un->un_recov_buf_busy == 0); 4877 4878 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 4879 "st_ioctl(): fileno=%x, blkno=%x, eof=%x, state = %d, " 4880 "pe_flag = %d\n", 4881 un->un_pos.fileno, un->un_pos.blkno, un->un_pos.eof, un->un_state, 4882 un->un_persistence && un->un_persist_errors); 4883 4884 /* 4885 * We don't want to block on these, so let them through 4886 * and we don't care about setting driver states here. 4887 */ 4888 if ((cmd == MTIOCGETDRIVETYPE) || 4889 (cmd == MTIOCGUARANTEEDORDER) || 4890 (cmd == MTIOCPERSISTENTSTATUS)) { 4891 goto check_commands; 4892 } 4893 4894 /* 4895 * We clear error entry stack except command 4896 * MTIOCGETERROR and MTIOCGET 4897 */ 4898 if ((cmd != MTIOCGETERROR) && 4899 (cmd != MTIOCGET)) { 4900 st_empty_error_stack(un); 4901 } 4902 4903 /* 4904 * wait for all outstanding commands to complete, or be dequeued. 4905 * And because ioctl's are synchronous commands, any return value 4906 * after this, will be in order 4907 */ 4908 st_wait_for_io(un); 4909 4910 /* 4911 * allow only a through clear errors and persistent status, and 4912 * status 4913 */ 4914 if (un->un_persistence && un->un_persist_errors) { 4915 if ((cmd == MTIOCLRERR) || 4916 (cmd == MTIOCPERSISTENT) || 4917 (cmd == MTIOCGET)) { 4918 goto check_commands; 4919 } else { 4920 rval = un->un_errno; 4921 goto exit; 4922 } 4923 } 4924 4925 ASSERT(un->un_throttle != 0); 4926 un->un_throttle = 1; /* > 1 will never happen here */ 4927 un->un_errno = 0; /* start clean from here */ 4928 4929 /* 4930 * first and foremost, handle any ST_EOT_PENDING cases. 4931 * That is, if a logical eot is pending notice, notice it. 4932 */ 4933 if (un->un_pos.eof == ST_EOT_PENDING) { 4934 int resid = un->un_err_resid; 4935 uchar_t status = un->un_status; 4936 uchar_t lastop = un->un_lastop; 4937 4938 if (st_cmd(un, SCMD_SPACE, Fmk(-1), SYNC_CMD)) { 4939 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4940 "stioctl : EIO can't space fmk(-1)\n"); 4941 rval = EIO; 4942 goto exit; 4943 } 4944 un->un_lastop = lastop; /* restore last operation */ 4945 if (status == SUN_KEY_EOF) { 4946 un->un_status = SUN_KEY_EOT; 4947 } else { 4948 un->un_status = status; 4949 } 4950 un->un_err_resid = resid; 4951 /* fix up block number */ 4952 un->un_err_pos.blkno = un->un_pos.blkno = 0; 4953 /* now we're at logical eot */ 4954 un->un_pos.eof = ST_EOT; 4955 } 4956 4957 /* 4958 * now, handle the rest of the situations 4959 */ 4960 check_commands: 4961 switch (cmd) { 4962 case MTIOCGET: 4963 { 4964 #ifdef _MULTI_DATAMODEL 4965 /* 4966 * For use when a 32 bit app makes a call into a 4967 * 64 bit ioctl 4968 */ 4969 struct mtget32 mtg_local32; 4970 struct mtget32 *mtget_32 = &mtg_local32; 4971 #endif /* _MULTI_DATAMODEL */ 4972 4973 /* Get tape status */ 4974 struct mtget mtg_local; 4975 struct mtget *mtget = &mtg_local; 4976 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 4977 "st_ioctl: MTIOCGET\n"); 4978 4979 bzero((caddr_t)mtget, sizeof (struct mtget)); 4980 mtget->mt_erreg = un->un_status; 4981 mtget->mt_resid = un->un_err_resid; 4982 mtget->mt_dsreg = un->un_retry_ct; 4983 if (un->un_err_pos.pmode == legacy) { 4984 mtget->mt_fileno = un->un_err_pos.fileno; 4985 } else { 4986 mtget->mt_fileno = -1; 4987 } 4988 /* 4989 * If the value is positive fine. 4990 * If its negative we need to return a value based on the 4991 * old way if counting backwards from INF (1,000,000,000). 4992 */ 4993 if (un->un_err_pos.blkno >= 0) { 4994 mtget->mt_blkno = un->un_err_pos.blkno; 4995 } else { 4996 mtget->mt_blkno = INF + 1 - (-un->un_err_pos.blkno); 4997 } 4998 mtget->mt_type = un->un_dp->type; 4999 mtget->mt_flags = MTF_SCSI | MTF_ASF; 5000 if (un->un_read_pos_type != NO_POS) { 5001 mtget->mt_flags |= MTF_LOGICAL_BLOCK; 5002 } 5003 if (un->un_dp->options & ST_REEL) { 5004 mtget->mt_flags |= MTF_REEL; 5005 mtget->mt_bf = 20; 5006 } else { /* 1/4" cartridges */ 5007 switch (mtget->mt_type) { 5008 /* Emulex cartridge tape */ 5009 case MT_ISMT02: 5010 mtget->mt_bf = 40; 5011 break; 5012 default: 5013 mtget->mt_bf = 126; 5014 break; 5015 } 5016 } 5017 5018 /* 5019 * If large transfers are allowed and drive options 5020 * has no record size limit set. Calculate blocking 5021 * factor from the lesser of maxbsize and maxdma. 5022 */ 5023 if ((un->un_allow_large_xfer) && 5024 (un->un_dp->options & ST_NO_RECSIZE_LIMIT)) { 5025 mtget->mt_bf = min(un->un_maxbsize, 5026 un->un_maxdma) / SECSIZE; 5027 } 5028 5029 if (un->un_read_only == WORM || 5030 un->un_read_only == RDWORM) { 5031 mtget->mt_flags |= MTF_WORM_MEDIA; 5032 } 5033 5034 /* 5035 * In persistent error mode sending a non-queued can hang 5036 * because this ioctl gets to be run without turning off 5037 * persistense. Fake the answer based on previous info. 5038 */ 5039 if (un->un_persistence) { 5040 rval = 0; 5041 } else { 5042 rval = st_check_clean_bit(un); 5043 } 5044 if (rval == 0) { 5045 /* 5046 * If zero is returned or in persistent mode, 5047 * use the old data. 5048 */ 5049 if ((un->un_HeadClean & (TAPE_ALERT_SUPPORTED | 5050 TAPE_SEQUENTIAL_SUPPORTED|TAPE_ALERT_NOT_SUPPORTED)) 5051 != TAPE_ALERT_NOT_SUPPORTED) { 5052 mtget->mt_flags |= MTF_TAPE_CLN_SUPPORTED; 5053 } 5054 if (un->un_HeadClean & (TAPE_PREVIOUSLY_DIRTY | 5055 TAPE_ALERT_STILL_DIRTY)) { 5056 mtget->mt_flags |= MTF_TAPE_HEAD_DIRTY; 5057 } 5058 } else { 5059 mtget->mt_flags |= (ushort_t)rval; 5060 rval = 0; 5061 } 5062 5063 un->un_status = 0; /* Reset status */ 5064 un->un_err_resid = 0; 5065 tmp = sizeof (struct mtget); 5066 5067 #ifdef _MULTI_DATAMODEL 5068 5069 switch (ddi_model_convert_from(flag & FMODELS)) { 5070 case DDI_MODEL_ILP32: 5071 /* 5072 * Convert 64 bit back to 32 bit before doing 5073 * copyout. This is what the ILP32 app expects. 5074 */ 5075 mtget_32->mt_erreg = mtget->mt_erreg; 5076 mtget_32->mt_resid = mtget->mt_resid; 5077 mtget_32->mt_dsreg = mtget->mt_dsreg; 5078 mtget_32->mt_fileno = (daddr32_t)mtget->mt_fileno; 5079 mtget_32->mt_blkno = (daddr32_t)mtget->mt_blkno; 5080 mtget_32->mt_type = mtget->mt_type; 5081 mtget_32->mt_flags = mtget->mt_flags; 5082 mtget_32->mt_bf = mtget->mt_bf; 5083 5084 if (ddi_copyout(mtget_32, (void *)arg, 5085 sizeof (struct mtget32), flag)) { 5086 rval = EFAULT; 5087 } 5088 break; 5089 5090 case DDI_MODEL_NONE: 5091 if (ddi_copyout(mtget, (void *)arg, tmp, flag)) { 5092 rval = EFAULT; 5093 } 5094 break; 5095 } 5096 #else /* ! _MULTI_DATAMODE */ 5097 if (ddi_copyout(mtget, (void *)arg, tmp, flag)) { 5098 rval = EFAULT; 5099 } 5100 #endif /* _MULTI_DATAMODE */ 5101 5102 break; 5103 } 5104 case MTIOCGETERROR: 5105 /* 5106 * get error entry from error stack 5107 */ 5108 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 5109 "st_ioctl: MTIOCGETERROR\n"); 5110 5111 rval = st_get_error_entry(un, arg, flag); 5112 5113 break; 5114 5115 case MTIOCSTATE: 5116 { 5117 /* 5118 * return when media presence matches state 5119 */ 5120 enum mtio_state state; 5121 5122 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 5123 "st_ioctl: MTIOCSTATE\n"); 5124 5125 if (ddi_copyin((void *)arg, &state, sizeof (int), flag)) 5126 rval = EFAULT; 5127 5128 mutex_exit(ST_MUTEX); 5129 5130 rval = st_check_media(dev, state); 5131 5132 mutex_enter(ST_MUTEX); 5133 5134 if (rval != 0) { 5135 break; 5136 } 5137 5138 if (ddi_copyout(&un->un_mediastate, (void *)arg, 5139 sizeof (int), flag)) 5140 rval = EFAULT; 5141 break; 5142 5143 } 5144 5145 case MTIOCGETDRIVETYPE: 5146 { 5147 #ifdef _MULTI_DATAMODEL 5148 /* 5149 * For use when a 32 bit app makes a call into a 5150 * 64 bit ioctl 5151 */ 5152 struct mtdrivetype_request32 mtdtrq32; 5153 #endif /* _MULTI_DATAMODEL */ 5154 5155 /* 5156 * return mtdrivetype 5157 */ 5158 struct mtdrivetype_request mtdtrq; 5159 struct mtdrivetype mtdrtyp; 5160 struct mtdrivetype *mtdt = &mtdrtyp; 5161 struct st_drivetype *stdt = un->un_dp; 5162 5163 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 5164 "st_ioctl: MTIOCGETDRIVETYPE\n"); 5165 5166 #ifdef _MULTI_DATAMODEL 5167 switch (ddi_model_convert_from(flag & FMODELS)) { 5168 case DDI_MODEL_ILP32: 5169 { 5170 if (ddi_copyin((void *)arg, &mtdtrq32, 5171 sizeof (struct mtdrivetype_request32), flag)) { 5172 rval = EFAULT; 5173 break; 5174 } 5175 mtdtrq.size = mtdtrq32.size; 5176 mtdtrq.mtdtp = 5177 (struct mtdrivetype *)(uintptr_t)mtdtrq32.mtdtp; 5178 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 5179 "st_ioctl: size 0x%x\n", mtdtrq.size); 5180 break; 5181 } 5182 case DDI_MODEL_NONE: 5183 if (ddi_copyin((void *)arg, &mtdtrq, 5184 sizeof (struct mtdrivetype_request), flag)) { 5185 rval = EFAULT; 5186 break; 5187 } 5188 break; 5189 } 5190 5191 #else /* ! _MULTI_DATAMODEL */ 5192 if (ddi_copyin((void *)arg, &mtdtrq, 5193 sizeof (struct mtdrivetype_request), flag)) { 5194 rval = EFAULT; 5195 break; 5196 } 5197 #endif /* _MULTI_DATAMODEL */ 5198 5199 /* 5200 * if requested size is < 0 then return 5201 * error. 5202 */ 5203 if (mtdtrq.size < 0) { 5204 rval = EINVAL; 5205 break; 5206 } 5207 bzero(mtdt, sizeof (struct mtdrivetype)); 5208 (void) strncpy(mtdt->name, stdt->name, ST_NAMESIZE); 5209 (void) strncpy(mtdt->vid, stdt->vid, VIDPIDLEN - 1); 5210 mtdt->type = stdt->type; 5211 mtdt->bsize = stdt->bsize; 5212 mtdt->options = stdt->options; 5213 mtdt->max_rretries = stdt->max_rretries; 5214 mtdt->max_wretries = stdt->max_wretries; 5215 for (tmp = 0; tmp < NDENSITIES; tmp++) { 5216 mtdt->densities[tmp] = stdt->densities[tmp]; 5217 } 5218 mtdt->default_density = stdt->default_density; 5219 /* 5220 * Speed hasn't been used since the hayday of reel tape. 5221 * For all drives not setting the option ST_KNOWS_MEDIA 5222 * the speed member renamed to mediatype are zeros. 5223 * Those drives that have ST_KNOWS_MEDIA set use the 5224 * new mediatype member which is used to figure the 5225 * type of media loaded. 5226 * 5227 * So as to not break applications speed in the 5228 * mtdrivetype structure is not renamed. 5229 */ 5230 for (tmp = 0; tmp < NDENSITIES; tmp++) { 5231 mtdt->speeds[tmp] = stdt->mediatype[tmp]; 5232 } 5233 mtdt->non_motion_timeout = stdt->non_motion_timeout; 5234 mtdt->io_timeout = stdt->io_timeout; 5235 mtdt->rewind_timeout = stdt->rewind_timeout; 5236 mtdt->space_timeout = stdt->space_timeout; 5237 mtdt->load_timeout = stdt->load_timeout; 5238 mtdt->unload_timeout = stdt->unload_timeout; 5239 mtdt->erase_timeout = stdt->erase_timeout; 5240 5241 /* 5242 * Limit the maximum length of the result to 5243 * sizeof (struct mtdrivetype). 5244 */ 5245 tmp = sizeof (struct mtdrivetype); 5246 if (mtdtrq.size < tmp) 5247 tmp = mtdtrq.size; 5248 if (ddi_copyout(mtdt, mtdtrq.mtdtp, tmp, flag)) { 5249 rval = EFAULT; 5250 } 5251 break; 5252 } 5253 case MTIOCPERSISTENT: 5254 5255 if (ddi_copyin((void *)arg, &tmp, sizeof (tmp), flag)) { 5256 rval = EFAULT; 5257 break; 5258 } 5259 5260 if (tmp) { 5261 st_turn_pe_on(un); 5262 } else { 5263 st_turn_pe_off(un); 5264 } 5265 5266 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 5267 "st_ioctl: MTIOCPERSISTENT : persistence = %d\n", 5268 un->un_persistence); 5269 5270 break; 5271 5272 case MTIOCPERSISTENTSTATUS: 5273 tmp = (int)un->un_persistence; 5274 5275 if (ddi_copyout(&tmp, (void *)arg, sizeof (tmp), flag)) { 5276 rval = EFAULT; 5277 } 5278 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 5279 "st_ioctl: MTIOCPERSISTENTSTATUS:persistence = %d\n", 5280 un->un_persistence); 5281 5282 break; 5283 5284 case MTIOCLRERR: 5285 { 5286 /* clear persistent errors */ 5287 5288 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 5289 "st_ioctl: MTIOCLRERR\n"); 5290 5291 st_clear_pe(un); 5292 5293 break; 5294 } 5295 5296 case MTIOCGUARANTEEDORDER: 5297 { 5298 /* 5299 * this is just a holder to make a valid ioctl and 5300 * it won't be in any earlier release 5301 */ 5302 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 5303 "st_ioctl: MTIOCGUARANTEEDORDER\n"); 5304 5305 break; 5306 } 5307 5308 case MTIOCRESERVE: 5309 { 5310 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 5311 "st_ioctl: MTIOCRESERVE\n"); 5312 5313 /* 5314 * Check if Reserve/Release is supported. 5315 */ 5316 if (un->un_dp->options & ST_NO_RESERVE_RELEASE) { 5317 rval = ENOTTY; 5318 break; 5319 } 5320 5321 rval = st_reserve_release(un, ST_RESERVE, st_uscsi_cmd); 5322 5323 if (rval == 0) { 5324 un->un_rsvd_status |= ST_PRESERVE_RESERVE; 5325 } 5326 break; 5327 } 5328 5329 case MTIOCRELEASE: 5330 { 5331 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 5332 "st_ioctl: MTIOCRELEASE\n"); 5333 5334 /* 5335 * Check if Reserve/Release is supported. 5336 */ 5337 if (un->un_dp->options & ST_NO_RESERVE_RELEASE) { 5338 rval = ENOTTY; 5339 break; 5340 } 5341 5342 /* 5343 * Used to just clear ST_PRESERVE_RESERVE which 5344 * made the reservation release at next close. 5345 * As the user may have opened and then done a 5346 * persistant reservation we now need to drop 5347 * the reservation without closing if the user 5348 * attempts to do this. 5349 */ 5350 rval = st_reserve_release(un, ST_RELEASE, st_uscsi_cmd); 5351 5352 un->un_rsvd_status &= ~ST_PRESERVE_RESERVE; 5353 5354 break; 5355 } 5356 5357 case MTIOCFORCERESERVE: 5358 { 5359 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 5360 "st_ioctl: MTIOCFORCERESERVE\n"); 5361 5362 /* 5363 * Check if Reserve/Release is supported. 5364 */ 5365 if (un->un_dp->options & ST_NO_RESERVE_RELEASE) { 5366 rval = ENOTTY; 5367 break; 5368 } 5369 /* 5370 * allow only super user to run this. 5371 */ 5372 if (drv_priv(cred_p) != 0) { 5373 rval = EPERM; 5374 break; 5375 } 5376 /* 5377 * Throw away reserve, 5378 * not using test-unit-ready 5379 * since reserve can succeed without tape being 5380 * present in the drive. 5381 */ 5382 (void) st_reserve_release(un, ST_RESERVE, st_uscsi_cmd); 5383 5384 rval = st_take_ownership(un, st_uscsi_cmd); 5385 5386 break; 5387 } 5388 5389 case USCSICMD: 5390 { 5391 cred_t *cr; 5392 5393 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 5394 "st_ioctl: USCSICMD\n"); 5395 5396 cr = ddi_get_cred(); 5397 if ((drv_priv(cred_p) != 0) && (drv_priv(cr) != 0)) { 5398 rval = EPERM; 5399 } else { 5400 rval = st_uscsi_cmd(un, (struct uscsi_cmd *)arg, flag); 5401 } 5402 break; 5403 } 5404 case MTIOCTOP: 5405 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 5406 "st_ioctl: MTIOCTOP\n"); 5407 rval = st_mtioctop(un, arg, flag); 5408 break; 5409 5410 case MTIOCLTOP: 5411 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 5412 "st_ioctl: MTIOLCTOP\n"); 5413 rval = st_mtiocltop(un, arg, flag); 5414 break; 5415 5416 case MTIOCREADIGNOREILI: 5417 { 5418 int set_ili; 5419 5420 if (ddi_copyin((void *)arg, &set_ili, 5421 sizeof (set_ili), flag)) { 5422 rval = EFAULT; 5423 break; 5424 } 5425 5426 if (un->un_bsize) { 5427 rval = ENOTTY; 5428 break; 5429 } 5430 5431 switch (set_ili) { 5432 case 0: 5433 un->un_dp->options &= ~ST_READ_IGNORE_ILI; 5434 break; 5435 5436 case 1: 5437 un->un_dp->options |= ST_READ_IGNORE_ILI; 5438 break; 5439 5440 default: 5441 rval = EINVAL; 5442 break; 5443 } 5444 break; 5445 } 5446 5447 case MTIOCREADIGNOREEOFS: 5448 { 5449 int ignore_eof; 5450 5451 if (ddi_copyin((void *)arg, &ignore_eof, 5452 sizeof (ignore_eof), flag)) { 5453 rval = EFAULT; 5454 break; 5455 } 5456 5457 if (!(un->un_dp->options & ST_REEL)) { 5458 rval = ENOTTY; 5459 break; 5460 } 5461 5462 switch (ignore_eof) { 5463 case 0: 5464 un->un_dp->options &= ~ST_READ_IGNORE_EOFS; 5465 break; 5466 5467 case 1: 5468 un->un_dp->options |= ST_READ_IGNORE_EOFS; 5469 break; 5470 5471 default: 5472 rval = EINVAL; 5473 break; 5474 } 5475 break; 5476 } 5477 5478 case MTIOCSHORTFMK: 5479 { 5480 int short_fmk; 5481 5482 if (ddi_copyin((void *)arg, &short_fmk, 5483 sizeof (short_fmk), flag)) { 5484 rval = EFAULT; 5485 break; 5486 } 5487 5488 switch (un->un_dp->type) { 5489 case ST_TYPE_EXB8500: 5490 case ST_TYPE_EXABYTE: 5491 if (!short_fmk) { 5492 un->un_dp->options &= ~ST_SHORT_FILEMARKS; 5493 } else if (short_fmk == 1) { 5494 un->un_dp->options |= ST_SHORT_FILEMARKS; 5495 } else { 5496 rval = EINVAL; 5497 } 5498 break; 5499 5500 default: 5501 rval = ENOTTY; 5502 break; 5503 } 5504 break; 5505 } 5506 5507 case MTIOCGETPOS: 5508 rval = st_update_block_pos(un, st_cmd, 0); 5509 if (rval == 0) { 5510 if (ddi_copyout((void *)&un->un_pos, (void *)arg, 5511 sizeof (tapepos_t), flag)) { 5512 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 5513 "MTIOCGETPOS copy out failed\n"); 5514 rval = EFAULT; 5515 } 5516 } 5517 break; 5518 5519 case MTIOCRESTPOS: 5520 { 5521 tapepos_t dest; 5522 5523 if (ddi_copyin((void *)arg, &dest, sizeof (tapepos_t), 5524 flag) != 0) { 5525 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 5526 "MTIOCRESTPOS copy in failed\n"); 5527 rval = EFAULT; 5528 break; 5529 } 5530 rval = st_validate_tapemarks(un, st_uscsi_cmd, &dest); 5531 if (rval != 0) { 5532 rval = EIO; 5533 } 5534 break; 5535 } 5536 default: 5537 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 5538 "st_ioctl: unknown ioctl\n"); 5539 rval = ENOTTY; 5540 } 5541 5542 exit: 5543 if (!(un->un_persistence && un->un_persist_errors)) { 5544 un->un_errno = rval; 5545 } 5546 5547 mutex_exit(ST_MUTEX); 5548 5549 return (rval); 5550 } 5551 5552 5553 /* 5554 * do some MTIOCTOP tape operations 5555 */ 5556 static int 5557 st_mtioctop(struct scsi_tape *un, intptr_t arg, int flag) 5558 { 5559 #ifdef _MULTI_DATAMODEL 5560 /* 5561 * For use when a 32 bit app makes a call into a 5562 * 64 bit ioctl 5563 */ 5564 struct mtop32 mtop_32_for_64; 5565 #endif /* _MULTI_DATAMODEL */ 5566 struct mtop passed; 5567 struct mtlop local; 5568 int rval = 0; 5569 5570 ST_FUNC(ST_DEVINFO, st_mtioctop); 5571 5572 ASSERT(mutex_owned(ST_MUTEX)); 5573 5574 #ifdef _MULTI_DATAMODEL 5575 switch (ddi_model_convert_from(flag & FMODELS)) { 5576 case DDI_MODEL_ILP32: 5577 if (ddi_copyin((void *)arg, &mtop_32_for_64, 5578 sizeof (struct mtop32), flag)) { 5579 return (EFAULT); 5580 } 5581 local.mt_op = mtop_32_for_64.mt_op; 5582 local.mt_count = (int64_t)mtop_32_for_64.mt_count; 5583 break; 5584 5585 case DDI_MODEL_NONE: 5586 if (ddi_copyin((void *)arg, &passed, sizeof (passed), flag)) { 5587 return (EFAULT); 5588 } 5589 local.mt_op = passed.mt_op; 5590 /* prevent sign extention */ 5591 local.mt_count = (UINT32_MAX & passed.mt_count); 5592 break; 5593 } 5594 5595 #else /* ! _MULTI_DATAMODEL */ 5596 if (ddi_copyin((void *)arg, &passed, sizeof (passed), flag)) { 5597 return (EFAULT); 5598 } 5599 local.mt_op = passed.mt_op; 5600 /* prevent sign extention */ 5601 local.mt_count = (UINT32_MAX & passed.mt_count); 5602 #endif /* _MULTI_DATAMODEL */ 5603 5604 rval = st_do_mtioctop(un, &local); 5605 5606 #ifdef _MULTI_DATAMODEL 5607 switch (ddi_model_convert_from(flag & FMODELS)) { 5608 case DDI_MODEL_ILP32: 5609 if (((uint64_t)local.mt_count) > UINT32_MAX) { 5610 rval = ERANGE; 5611 break; 5612 } 5613 /* 5614 * Convert 64 bit back to 32 bit before doing 5615 * copyout. This is what the ILP32 app expects. 5616 */ 5617 mtop_32_for_64.mt_op = local.mt_op; 5618 mtop_32_for_64.mt_count = local.mt_count; 5619 5620 if (ddi_copyout(&mtop_32_for_64, (void *)arg, 5621 sizeof (struct mtop32), flag)) { 5622 rval = EFAULT; 5623 } 5624 break; 5625 5626 case DDI_MODEL_NONE: 5627 passed.mt_count = local.mt_count; 5628 passed.mt_op = local.mt_op; 5629 if (ddi_copyout(&passed, (void *)arg, sizeof (passed), flag)) { 5630 rval = EFAULT; 5631 } 5632 break; 5633 } 5634 #else /* ! _MULTI_DATAMODE */ 5635 if (((uint64_t)local.mt_count) > UINT32_MAX) { 5636 rval = ERANGE; 5637 } else { 5638 passed.mt_op = local.mt_op; 5639 passed.mt_count = local.mt_count; 5640 if (ddi_copyout(&passed, (void *)arg, sizeof (passed), flag)) { 5641 rval = EFAULT; 5642 } 5643 } 5644 #endif /* _MULTI_DATAMODE */ 5645 5646 5647 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 5648 "st_ioctl: fileno=%x, blkno=%x, eof=%x\n", un->un_pos.fileno, 5649 un->un_pos.blkno, un->un_pos.eof); 5650 5651 if (un->un_pos.pmode == invalid) { 5652 un->un_density_known = 0; 5653 } 5654 5655 ASSERT(mutex_owned(ST_MUTEX)); 5656 return (rval); 5657 } 5658 5659 static int 5660 st_mtiocltop(struct scsi_tape *un, intptr_t arg, int flag) 5661 { 5662 struct mtlop local; 5663 int rval; 5664 5665 ST_FUNC(ST_DEVINFO, st_mtiocltop); 5666 if (ddi_copyin((void *)arg, &local, sizeof (local), flag)) { 5667 return (EFAULT); 5668 } 5669 5670 rval = st_do_mtioctop(un, &local); 5671 5672 if (ddi_copyout(&local, (void *)arg, sizeof (local), flag)) { 5673 rval = EFAULT; 5674 } 5675 return (rval); 5676 } 5677 5678 5679 static int 5680 st_do_mtioctop(struct scsi_tape *un, struct mtlop *mtop) 5681 { 5682 dev_t dev = un->un_dev; 5683 int savefile; 5684 int rval = 0; 5685 5686 ST_FUNC(ST_DEVINFO, st_do_mtioctop); 5687 5688 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 5689 "st_do_mtioctop(): mt_op=%x\n", mtop->mt_op); 5690 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 5691 "fileno=%x, blkno=%x, eof=%x\n", 5692 un->un_pos.fileno, un->un_pos.blkno, un->un_pos.eof); 5693 5694 un->un_status = 0; 5695 5696 /* 5697 * if we are going to mess with a tape, we have to make sure we have 5698 * one and are not offline (i.e. no tape is initialized). We let 5699 * commands pass here that don't actually touch the tape, except for 5700 * loading and initialization (rewinding). 5701 */ 5702 if (un->un_state == ST_STATE_OFFLINE) { 5703 switch (mtop->mt_op) { 5704 case MTLOAD: 5705 case MTNOP: 5706 /* 5707 * We don't want strategy calling st_tape_init here, 5708 * so, change state 5709 */ 5710 un->un_state = ST_STATE_INITIALIZING; 5711 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5712 "st_do_mtioctop : OFFLINE state = %d\n", 5713 un->un_state); 5714 break; 5715 default: 5716 /* 5717 * reinitialize by normal means 5718 */ 5719 rval = st_tape_init(un); 5720 if (rval) { 5721 un->un_state = ST_STATE_INITIALIZING; 5722 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5723 "st_do_mtioctop : OFFLINE init failure "); 5724 un->un_state = ST_STATE_OFFLINE; 5725 un->un_pos.pmode = invalid; 5726 if (rval != EACCES) { 5727 rval = EIO; 5728 } 5729 return (rval); 5730 } 5731 un->un_state = ST_STATE_OPEN_PENDING_IO; 5732 break; 5733 } 5734 } 5735 5736 /* 5737 * If the file position is invalid, allow only those 5738 * commands that properly position the tape and fail 5739 * the rest with EIO 5740 */ 5741 if (un->un_pos.pmode == invalid) { 5742 switch (mtop->mt_op) { 5743 case MTWEOF: 5744 case MTRETEN: 5745 case MTERASE: 5746 case MTEOM: 5747 case MTFSF: 5748 case MTFSR: 5749 case MTBSF: 5750 case MTNBSF: 5751 case MTBSR: 5752 case MTSRSZ: 5753 case MTGRSZ: 5754 case MTSEEK: 5755 case MTBSSF: 5756 case MTFSSF: 5757 return (EIO); 5758 /* NOTREACHED */ 5759 case MTREW: 5760 case MTLOAD: 5761 case MTOFFL: 5762 case MTNOP: 5763 case MTTELL: 5764 case MTLOCK: 5765 case MTUNLOCK: 5766 break; 5767 5768 default: 5769 return (ENOTTY); 5770 /* NOTREACHED */ 5771 } 5772 } 5773 5774 switch (mtop->mt_op) { 5775 case MTERASE: 5776 /* 5777 * MTERASE rewinds the tape, erase it completely, and returns 5778 * to the beginning of the tape 5779 */ 5780 if (un->un_mspl->wp || un->un_read_only & WORM) { 5781 un->un_status = KEY_WRITE_PROTECT; 5782 un->un_err_resid = mtop->mt_count; 5783 COPY_POS(&un->un_err_pos, &un->un_pos); 5784 return (EACCES); 5785 } 5786 if (un->un_dp->options & ST_REEL) { 5787 un->un_fmneeded = 2; 5788 } else { 5789 un->un_fmneeded = 1; 5790 } 5791 mtop->mt_count = mtop->mt_count ? 1 : 0; 5792 if (st_check_density_or_wfm(dev, 1, B_WRITE, NO_STEPBACK) || 5793 st_cmd(un, SCMD_REWIND, 0, SYNC_CMD) || 5794 st_cmd(un, SCMD_ERASE, mtop->mt_count, SYNC_CMD)) { 5795 un->un_pos.pmode = invalid; 5796 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5797 "st_do_mtioctop : EIO space or erase or " 5798 "check den)\n"); 5799 rval = EIO; 5800 } else { 5801 /* QIC and helical scan rewind after erase */ 5802 if (un->un_dp->options & ST_REEL) { 5803 (void) st_cmd(un, SCMD_REWIND, 0, ASYNC_CMD); 5804 } 5805 } 5806 break; 5807 5808 case MTWEOF: 5809 /* 5810 * write an end-of-file record 5811 */ 5812 if (un->un_mspl->wp || un->un_read_only & RDONLY) { 5813 un->un_status = KEY_WRITE_PROTECT; 5814 un->un_err_resid = mtop->mt_count; 5815 COPY_POS(&un->un_err_pos, &un->un_pos); 5816 return (EACCES); 5817 } 5818 5819 /* 5820 * zero count means just flush buffers 5821 * negative count is not permitted 5822 */ 5823 if (mtop->mt_count < 0) { 5824 return (EINVAL); 5825 } 5826 5827 /* Not on worm */ 5828 if (un->un_read_only == RDWR) { 5829 un->un_test_append = 1; 5830 } 5831 5832 if (un->un_state == ST_STATE_OPEN_PENDING_IO) { 5833 if (st_determine_density(un, B_WRITE)) { 5834 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5835 "st_do_mtioctop : EIO : MTWEOF can't " 5836 "determine density"); 5837 return (EIO); 5838 } 5839 } 5840 5841 rval = st_write_fm(dev, (int)mtop->mt_count); 5842 if ((rval != 0) && (rval != EACCES)) { 5843 /* 5844 * Failure due to something other than illegal 5845 * request results in loss of state (st_intr). 5846 */ 5847 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5848 "st_do_mtioctop : EIO : MTWEOF can't write " 5849 "file mark"); 5850 rval = EIO; 5851 } 5852 break; 5853 5854 case MTRETEN: 5855 /* 5856 * retension the tape 5857 */ 5858 if (st_check_density_or_wfm(dev, 1, 0, NO_STEPBACK) || 5859 st_cmd(un, SCMD_LOAD, LD_LOAD | LD_RETEN, SYNC_CMD)) { 5860 un->un_pos.pmode = invalid; 5861 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5862 "st_do_mtioctop : EIO : MTRETEN "); 5863 rval = EIO; 5864 } 5865 break; 5866 5867 case MTREW: 5868 /* 5869 * rewind the tape 5870 */ 5871 if (st_check_density_or_wfm(dev, 1, 0, NO_STEPBACK)) { 5872 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5873 "st_do_mtioctop : EIO:MTREW check " 5874 "density/wfm failed"); 5875 return (EIO); 5876 } 5877 if (st_cmd(un, SCMD_REWIND, 0, SYNC_CMD)) { 5878 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5879 "st_do_mtioctop : EIO : MTREW "); 5880 rval = EIO; 5881 } 5882 break; 5883 5884 case MTOFFL: 5885 /* 5886 * rewinds, and, if appropriate, takes the device offline by 5887 * unloading the tape 5888 */ 5889 if (st_check_density_or_wfm(dev, 1, 0, NO_STEPBACK)) { 5890 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5891 "st_do_mtioctop :EIO:MTOFFL check " 5892 "density/wfm failed"); 5893 return (EIO); 5894 } 5895 (void) st_cmd(un, SCMD_REWIND, 0, SYNC_CMD); 5896 if (st_cmd(un, SCMD_LOAD, LD_UNLOAD, SYNC_CMD)) { 5897 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5898 "st_do_mtioctop : EIO : MTOFFL"); 5899 return (EIO); 5900 } 5901 un->un_pos.eof = ST_NO_EOF; 5902 un->un_laststate = un->un_state; 5903 un->un_state = ST_STATE_OFFLINE; 5904 un->un_mediastate = MTIO_EJECTED; 5905 break; 5906 5907 case MTLOAD: 5908 /* 5909 * This is to load a tape into the drive 5910 * Note that if the tape is not loaded, the device will have 5911 * to be opened via O_NDELAY or O_NONBLOCK. 5912 */ 5913 /* 5914 * Let's try and clean things up, if we are not 5915 * initializing, and then send in the load command, no 5916 * matter what. 5917 * 5918 * load after a media change by the user. 5919 */ 5920 5921 if (un->un_state > ST_STATE_INITIALIZING) { 5922 (void) st_check_density_or_wfm(dev, 1, 0, NO_STEPBACK); 5923 } 5924 rval = st_cmd(un, SCMD_LOAD, LD_LOAD, SYNC_CMD); 5925 /* Load command to a drive that doesn't support load */ 5926 if ((rval == EIO) && 5927 ((un->un_status == KEY_NOT_READY) && 5928 /* Medium not present */ 5929 (un->un_uscsi_rqs_buf->es_add_code == 0x3a) || 5930 ((un->un_status == KEY_ILLEGAL_REQUEST) && 5931 (un->un_dp->type == MT_ISSTK9840) && 5932 /* CSL not present */ 5933 (un->un_uscsi_rqs_buf->es_add_code == 0x80)))) { 5934 rval = ENOTTY; 5935 break; 5936 } else if (rval != EACCES && rval != 0) { 5937 rval = EIO; 5938 } 5939 if (rval) { 5940 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5941 "st_do_mtioctop : %s : MTLOAD\n", 5942 rval == EACCES ? "EACCES" : "EIO"); 5943 /* 5944 * If load tape fails, who knows what happened... 5945 */ 5946 un->un_pos.pmode = invalid; 5947 break; 5948 } 5949 5950 /* 5951 * reset all counters appropriately using rewind, as if LOAD 5952 * succeeds, we are at BOT 5953 */ 5954 un->un_state = ST_STATE_INITIALIZING; 5955 5956 rval = st_tape_init(un); 5957 if ((rval == EACCES) && (un->un_read_only & WORM)) { 5958 rval = 0; 5959 break; 5960 } 5961 5962 if (rval != 0) { 5963 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5964 "st_do_mtioctop : EIO : MTLOAD calls " 5965 "st_tape_init\n"); 5966 rval = EIO; 5967 un->un_state = ST_STATE_OFFLINE; 5968 } 5969 5970 break; 5971 5972 case MTNOP: 5973 un->un_status = 0; /* Reset status */ 5974 un->un_err_resid = 0; 5975 mtop->mt_count = MTUNIT(dev); 5976 break; 5977 5978 case MTEOM: 5979 /* 5980 * positions the tape at a location just after the last file 5981 * written on the tape. For cartridge and 8 mm, this after 5982 * the last file mark; for reel, this is inbetween the two 5983 * last 2 file marks 5984 */ 5985 if ((un->un_pos.pmode == legacy && un->un_pos.eof >= ST_EOT) || 5986 (un->un_lastop == ST_OP_WRITE) || 5987 (un->un_lastop == ST_OP_WEOF)) { 5988 /* 5989 * If the command wants to move to logical end 5990 * of media, and we're already there, we're done. 5991 * If we were at logical eot, we reset the state 5992 * to be *not* at logical eot. 5993 * 5994 * If we're at physical or logical eot, we prohibit 5995 * forward space operations (unconditionally). 5996 * 5997 * Also if the last operation was a write of any 5998 * kind the tape is at EOD. 5999 */ 6000 return (0); 6001 } 6002 /* 6003 * physical tape position may not be what we've been 6004 * telling the user; adjust the request accordingly 6005 */ 6006 if (IN_EOF(un->un_pos)) { 6007 un->un_pos.fileno++; 6008 un->un_pos.blkno = 0; 6009 } 6010 6011 if (st_check_density_or_wfm(dev, 1, B_READ, NO_STEPBACK)) { 6012 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 6013 "st_do_mtioctop : EIO:MTEOM check density/wfm " 6014 " failed"); 6015 return (EIO); 6016 } 6017 6018 /* 6019 * st_find_eod() returns the last fileno we knew about; 6020 */ 6021 savefile = st_find_eod(un); 6022 6023 if ((un->un_status != KEY_BLANK_CHECK) && 6024 (un->un_status != SUN_KEY_EOT)) { 6025 un->un_pos.pmode = invalid; 6026 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 6027 "st_do_mtioctop : EIO : MTEOM status check failed"); 6028 rval = EIO; 6029 } else { 6030 /* 6031 * For 1/2" reel tapes assume logical EOT marked 6032 * by two file marks or we don't care that we may 6033 * be extending the last file on the tape. 6034 */ 6035 if (un->un_dp->options & ST_REEL) { 6036 if (st_cmd(un, SCMD_SPACE, Fmk(-1), SYNC_CMD)) { 6037 un->un_pos.pmode = invalid; 6038 ST_DEBUG2(ST_DEVINFO, st_label, 6039 SCSI_DEBUG, 6040 "st_do_mtioctop : EIO : MTEOM space" 6041 " cmd failed"); 6042 rval = EIO; 6043 break; 6044 } 6045 /* 6046 * Fix up the block number. 6047 */ 6048 un->un_pos.blkno = 0; 6049 un->un_err_pos.blkno = 0; 6050 } 6051 un->un_err_resid = 0; 6052 un->un_pos.fileno = savefile; 6053 un->un_pos.eof = ST_EOT; 6054 } 6055 un->un_status = 0; 6056 break; 6057 6058 case MTFSF: 6059 MAX_SPACE_CNT(mtop->mt_count); 6060 rval = st_mtfsf_ioctl(un, mtop->mt_count); 6061 break; 6062 6063 case MTFSR: 6064 MAX_SPACE_CNT(mtop->mt_count); 6065 rval = st_mtfsr_ioctl(un, mtop->mt_count); 6066 break; 6067 6068 case MTBSF: 6069 MAX_SPACE_CNT(mtop->mt_count); 6070 rval = st_mtbsf_ioctl(un, mtop->mt_count); 6071 break; 6072 6073 case MTNBSF: 6074 MAX_SPACE_CNT(mtop->mt_count); 6075 rval = st_mtnbsf_ioctl(un, mtop->mt_count); 6076 break; 6077 6078 case MTBSR: 6079 MAX_SPACE_CNT(mtop->mt_count); 6080 rval = st_mtbsr_ioctl(un, mtop->mt_count); 6081 break; 6082 6083 case MTBSSF: 6084 MAX_SPACE_CNT(mtop->mt_count); 6085 rval = st_mtbsfm_ioctl(un, mtop->mt_count); 6086 break; 6087 6088 case MTFSSF: 6089 MAX_SPACE_CNT(mtop->mt_count); 6090 rval = st_mtfsfm_ioctl(un, mtop->mt_count); 6091 break; 6092 6093 case MTSRSZ: 6094 6095 /* 6096 * Set record-size to that sent by user 6097 * Check to see if there is reason that the requested 6098 * block size should not be set. 6099 */ 6100 6101 /* If requesting variable block size is it ok? */ 6102 if ((mtop->mt_count == 0) && 6103 ((un->un_dp->options & ST_VARIABLE) == 0)) { 6104 return (ENOTTY); 6105 } 6106 6107 /* 6108 * If requested block size is not variable "0", 6109 * is it less then minimum. 6110 */ 6111 if ((mtop->mt_count != 0) && 6112 (mtop->mt_count < un->un_minbsize)) { 6113 return (EINVAL); 6114 } 6115 6116 /* Is the requested block size more then maximum */ 6117 if ((mtop->mt_count > min(un->un_maxbsize, un->un_maxdma)) && 6118 (un->un_maxbsize != 0)) { 6119 return (EINVAL); 6120 } 6121 6122 /* Is requested block size a modulus the device likes */ 6123 if ((mtop->mt_count % un->un_data_mod) != 0) { 6124 return (EINVAL); 6125 } 6126 6127 if (st_change_block_size(un, (uint32_t)mtop->mt_count) != 0) { 6128 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 6129 "st_ioctl : MTSRSZ : EIO : cant set block size"); 6130 return (EIO); 6131 } 6132 6133 return (0); 6134 6135 case MTGRSZ: 6136 /* 6137 * Get record-size to the user 6138 */ 6139 mtop->mt_count = un->un_bsize; 6140 rval = 0; 6141 break; 6142 6143 case MTTELL: 6144 rval = st_update_block_pos(un, st_cmd, 0); 6145 mtop->mt_count = un->un_pos.lgclblkno; 6146 break; 6147 6148 case MTSEEK: 6149 rval = st_logical_block_locate(un, st_uscsi_cmd, &un->un_pos, 6150 (uint64_t)mtop->mt_count, un->un_pos.partition); 6151 /* 6152 * This bit of magic make mt print the actual position if 6153 * the resulting position was not what was asked for. 6154 */ 6155 if (rval == ESPIPE) { 6156 rval = EIO; 6157 if ((uint64_t)mtop->mt_count != un->un_pos.lgclblkno) { 6158 mtop->mt_op = MTTELL; 6159 mtop->mt_count = un->un_pos.lgclblkno; 6160 } 6161 } 6162 break; 6163 6164 case MTLOCK: 6165 if (st_cmd(un, SCMD_DOORLOCK, MR_LOCK, SYNC_CMD)) { 6166 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 6167 "st_do_mtioctop : EIO : MTLOCK"); 6168 rval = EIO; 6169 } 6170 break; 6171 6172 case MTUNLOCK: 6173 if (st_cmd(un, SCMD_DOORLOCK, MR_UNLOCK, SYNC_CMD)) { 6174 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 6175 "st_do_mtioctop : EIO : MTUNLOCK"); 6176 rval = EIO; 6177 } 6178 break; 6179 6180 default: 6181 rval = ENOTTY; 6182 } 6183 6184 return (rval); 6185 } 6186 6187 6188 /* 6189 * Run a command for uscsi ioctl. 6190 */ 6191 static int 6192 st_uscsi_cmd(struct scsi_tape *un, struct uscsi_cmd *ucmd, int flag) 6193 { 6194 struct uscsi_cmd *uscmd; 6195 struct buf *bp; 6196 enum uio_seg uioseg; 6197 int offline_state = 0; 6198 int err = 0; 6199 dev_t dev = un->un_dev; 6200 6201 ST_FUNC(ST_DEVINFO, st_uscsi_cmd); 6202 6203 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 6204 "st_uscsi_cmd(dev = 0x%lx)\n", un->un_dev); 6205 6206 ASSERT(mutex_owned(ST_MUTEX)); 6207 6208 /* 6209 * We really don't know what commands are coming in here and 6210 * we don't want to limit the commands coming in. 6211 * 6212 * If st_tape_init() gets called from st_strategy(), then we 6213 * will hang the process waiting for un->un_sbuf_busy to be cleared, 6214 * which it never will, as we set it below. To prevent 6215 * st_tape_init() from getting called, we have to set state to other 6216 * than ST_STATE_OFFLINE, so we choose ST_STATE_INITIALIZING, which 6217 * achieves this purpose already. 6218 * 6219 * We use offline_state to preserve the OFFLINE state, if it exists, 6220 * so other entry points to the driver might have the chance to call 6221 * st_tape_init(). 6222 */ 6223 if (un->un_state == ST_STATE_OFFLINE) { 6224 un->un_laststate = ST_STATE_OFFLINE; 6225 un->un_state = ST_STATE_INITIALIZING; 6226 offline_state = 1; 6227 } 6228 6229 mutex_exit(ST_MUTEX); 6230 err = scsi_uscsi_alloc_and_copyin((intptr_t)ucmd, flag, ROUTE, &uscmd); 6231 mutex_enter(ST_MUTEX); 6232 if (err != 0) { 6233 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 6234 "st_uscsi_cmd: scsi_uscsi_alloc_and_copyin failed\n"); 6235 goto exit; 6236 } 6237 6238 uioseg = (flag & FKIOCTL) ? UIO_SYSSPACE : UIO_USERSPACE; 6239 6240 /* check to see if this command requires the drive to be reserved */ 6241 if (uscmd->uscsi_cdb != NULL) { 6242 err = st_check_cdb_for_need_to_reserve(un, 6243 (uchar_t *)uscmd->uscsi_cdb); 6244 if (err) { 6245 goto exit_free; 6246 } 6247 /* 6248 * If this is a space command we need to save the starting 6249 * point so we can retry from there if the command fails. 6250 */ 6251 if ((uscmd->uscsi_cdb[0] == SCMD_SPACE) || 6252 (uscmd->uscsi_cdb[0] == (char)SCMD_SPACE_G4)) { 6253 (void) st_update_block_pos(un, st_cmd, 0); 6254 } 6255 } 6256 6257 /* 6258 * Forground should not be doing anything while recovery is active. 6259 */ 6260 ASSERT(un->un_recov_buf_busy == 0); 6261 6262 /* 6263 * Get buffer resources... 6264 */ 6265 while (un->un_sbuf_busy) 6266 cv_wait(&un->un_sbuf_cv, ST_MUTEX); 6267 un->un_sbuf_busy = 1; 6268 6269 #ifdef STDEBUG 6270 if ((uscmd->uscsi_cdb != NULL) && (st_debug & 0x7) > 6) { 6271 int rw = (uscmd->uscsi_flags & USCSI_READ) ? B_READ : B_WRITE; 6272 st_print_cdb(ST_DEVINFO, st_label, SCSI_DEBUG, 6273 "uscsi cdb", uscmd->uscsi_cdb); 6274 if (uscmd->uscsi_buflen) { 6275 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 6276 "uscsi %s of %ld bytes %s %s space\n", 6277 (rw == B_READ) ? rd_str : wr_str, 6278 uscmd->uscsi_buflen, 6279 (rw == B_READ) ? "to" : "from", 6280 (uioseg == UIO_SYSSPACE) ? "system" : "user"); 6281 } 6282 } 6283 #endif /* STDEBUG */ 6284 6285 /* 6286 * Although st_uscsi_cmd() never makes use of these 6287 * now, we are just being safe and consistent. 6288 */ 6289 uscmd->uscsi_flags &= ~(USCSI_NOINTR | USCSI_NOPARITY | 6290 USCSI_OTAG | USCSI_HTAG | USCSI_HEAD); 6291 6292 un->un_srqbufp = uscmd->uscsi_rqbuf; 6293 bp = un->un_sbufp; 6294 bzero(bp, sizeof (buf_t)); 6295 if (uscmd->uscsi_cdb != NULL) { 6296 bp->b_forw = (struct buf *)(uintptr_t)uscmd->uscsi_cdb[0]; 6297 } 6298 bp->b_back = (struct buf *)uscmd; 6299 6300 mutex_exit(ST_MUTEX); 6301 err = scsi_uscsi_handle_cmd(dev, uioseg, uscmd, st_strategy, bp, NULL); 6302 mutex_enter(ST_MUTEX); 6303 6304 /* 6305 * If scsi reset successful, don't write any filemarks. 6306 */ 6307 if ((err == 0) && (uscmd->uscsi_flags & 6308 (USCSI_RESET_LUN | USCSI_RESET_TARGET | USCSI_RESET_ALL))) { 6309 un->un_fmneeded = 0; 6310 } 6311 6312 exit_free: 6313 /* 6314 * Free resources 6315 */ 6316 un->un_sbuf_busy = 0; 6317 un->un_srqbufp = NULL; 6318 6319 /* 6320 * If was a space command need to update logical block position. 6321 * If the command failed such that positioning is invalid, Don't 6322 * update the position as the user must do this to validate the 6323 * position for data protection. 6324 */ 6325 if ((uscmd->uscsi_cdb != NULL) && 6326 ((uscmd->uscsi_cdb[0] == SCMD_SPACE) || 6327 (uscmd->uscsi_cdb[0] == (char)SCMD_SPACE_G4)) && 6328 (un->un_pos.pmode != invalid)) { 6329 un->un_running.pmode = invalid; 6330 (void) st_update_block_pos(un, st_cmd, 1); 6331 /* 6332 * Set running position to invalid so it updates on the 6333 * next command. 6334 */ 6335 un->un_running.pmode = invalid; 6336 } 6337 cv_signal(&un->un_sbuf_cv); 6338 mutex_exit(ST_MUTEX); 6339 (void) scsi_uscsi_copyout_and_free((intptr_t)ucmd, uscmd); 6340 mutex_enter(ST_MUTEX); 6341 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 6342 "st_uscsi_cmd returns 0x%x\n", err); 6343 6344 exit: 6345 /* don't lose offline state */ 6346 if (offline_state) { 6347 un->un_state = ST_STATE_OFFLINE; 6348 } 6349 6350 ASSERT(mutex_owned(ST_MUTEX)); 6351 return (err); 6352 } 6353 6354 static int 6355 st_write_fm(dev_t dev, int wfm) 6356 { 6357 int i; 6358 int rval; 6359 6360 GET_SOFT_STATE(dev); 6361 6362 ST_FUNC(ST_DEVINFO, st_write_fm); 6363 6364 ASSERT(mutex_owned(ST_MUTEX)); 6365 6366 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 6367 "st_write_fm(dev = 0x%lx, wfm = %d)\n", dev, wfm); 6368 6369 /* 6370 * write one filemark at the time after EOT 6371 */ 6372 if (un->un_pos.eof >= ST_EOT) { 6373 for (i = 0; i < wfm; i++) { 6374 rval = st_cmd(un, SCMD_WRITE_FILE_MARK, 1, SYNC_CMD); 6375 if (rval == EACCES) { 6376 return (rval); 6377 } 6378 if (rval != 0) { 6379 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 6380 "st_write_fm : EIO : write EOT file mark"); 6381 return (EIO); 6382 } 6383 } 6384 } else { 6385 rval = st_cmd(un, SCMD_WRITE_FILE_MARK, wfm, SYNC_CMD); 6386 if (rval == EACCES) { 6387 return (rval); 6388 } 6389 if (rval) { 6390 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 6391 "st_write_fm : EIO : write file mark"); 6392 return (EIO); 6393 } 6394 } 6395 6396 ASSERT(mutex_owned(ST_MUTEX)); 6397 return (0); 6398 } 6399 6400 #ifdef STDEBUG 6401 static void 6402 st_start_dump(struct scsi_tape *un, struct buf *bp) 6403 { 6404 struct scsi_pkt *pkt = BP_PKT(bp); 6405 uchar_t *cdbp = (uchar_t *)pkt->pkt_cdbp; 6406 6407 ST_FUNC(ST_DEVINFO, st_start_dump); 6408 6409 if ((st_debug & 0x7) < 6) 6410 return; 6411 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 6412 "st_start: cmd=0x%p count=%ld resid=%ld flags=0x%x pkt=0x%p\n", 6413 (void *)bp->b_forw, bp->b_bcount, 6414 bp->b_resid, bp->b_flags, (void *)BP_PKT(bp)); 6415 st_print_cdb(ST_DEVINFO, st_label, SCSI_DEBUG, 6416 "st_start: cdb", (caddr_t)cdbp); 6417 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 6418 "st_start: fileno=%d, blk=%d\n", 6419 un->un_pos.fileno, un->un_pos.blkno); 6420 } 6421 #endif 6422 6423 6424 /* 6425 * Command start && done functions 6426 */ 6427 6428 /* 6429 * st_start() 6430 * 6431 * Called from: 6432 * st_strategy() to start a command. 6433 * st_runout() to retry when scsi_pkt allocation fails on previous attempt(s). 6434 * st_attach() when resuming from power down state. 6435 * st_start_restart() to retry transport when device was previously busy. 6436 * st_done_and_mutex_exit() to start the next command when previous is done. 6437 * 6438 * On entry: 6439 * scsi_pkt may or may not be allocated. 6440 * 6441 */ 6442 static void 6443 st_start(struct scsi_tape *un) 6444 { 6445 struct buf *bp; 6446 int status; 6447 int queued; 6448 6449 ST_FUNC(ST_DEVINFO, st_start); 6450 ASSERT(mutex_owned(ST_MUTEX)); 6451 6452 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 6453 "st_start(): dev = 0x%lx\n", un->un_dev); 6454 6455 if (un->un_recov_buf_busy) { 6456 /* recovery commands can happen anytime */ 6457 bp = un->un_recov_buf; 6458 queued = 0; 6459 } else if (un->un_sbuf_busy) { 6460 /* sbuf commands should only happen with an empty queue. */ 6461 ASSERT(un->un_quef == NULL); 6462 ASSERT(un->un_runqf == NULL); 6463 bp = un->un_sbufp; 6464 queued = 0; 6465 } else if (un->un_quef != NULL) { 6466 if (un->un_persistence && un->un_persist_errors) { 6467 return; 6468 } 6469 bp = un->un_quef; 6470 queued = 1; 6471 } else { 6472 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 6473 "st_start() returning no buf found\n"); 6474 return; 6475 } 6476 6477 ASSERT((bp->b_flags & B_DONE) == 0); 6478 6479 /* 6480 * Don't send more than un_throttle commands to the HBA 6481 */ 6482 if ((un->un_throttle <= 0) || (un->un_ncmds >= un->un_throttle)) { 6483 /* 6484 * if doing recovery we know there is outstanding commands. 6485 */ 6486 if (bp != un->un_recov_buf) { 6487 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 6488 "st_start returning throttle = %d or ncmds = %d\n", 6489 un->un_throttle, un->un_ncmds); 6490 if (un->un_ncmds == 0) { 6491 typedef void (*func)(); 6492 func fnc = (func)st_runout; 6493 6494 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 6495 "Sending delayed start to st_runout()\n"); 6496 mutex_exit(ST_MUTEX); 6497 (void) timeout(fnc, un, drv_usectohz(1000000)); 6498 mutex_enter(ST_MUTEX); 6499 } 6500 return; 6501 } 6502 } 6503 6504 /* 6505 * If the buf has no scsi_pkt call st_make_cmd() to get one and 6506 * build the command. 6507 */ 6508 if (BP_PKT(bp) == NULL) { 6509 ASSERT((bp->b_flags & B_DONE) == 0); 6510 st_make_cmd(un, bp, st_runout); 6511 ASSERT((bp->b_flags & B_DONE) == 0); 6512 status = geterror(bp); 6513 6514 /* 6515 * Some HBA's don't call bioerror() to set an error. 6516 * And geterror() returns zero if B_ERROR is not set. 6517 * So if we get zero we must check b_error. 6518 */ 6519 if (status == 0 && bp->b_error != 0) { 6520 status = bp->b_error; 6521 bioerror(bp, status); 6522 } 6523 6524 /* 6525 * Some HBA's convert DDI_DMA_NORESOURCES into ENOMEM. 6526 * In tape ENOMEM has special meaning so we'll change it. 6527 */ 6528 if (status == ENOMEM) { 6529 status = 0; 6530 bioerror(bp, status); 6531 } 6532 6533 /* 6534 * Did it fail and is it retryable? 6535 * If so return and wait for the callback through st_runout. 6536 * Also looks like scsi_init_pkt() will setup a callback even 6537 * if it isn't retryable. 6538 */ 6539 if (BP_PKT(bp) == NULL) { 6540 if (status == 0) { 6541 /* 6542 * If first attempt save state. 6543 */ 6544 if (un->un_state != ST_STATE_RESOURCE_WAIT) { 6545 un->un_laststate = un->un_state; 6546 un->un_state = ST_STATE_RESOURCE_WAIT; 6547 } 6548 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 6549 "temp no resources for pkt\n"); 6550 } else if (status == EINVAL) { 6551 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 6552 "scsi_init_pkt rejected pkt as too big\n"); 6553 if (un->un_persistence) { 6554 st_set_pe_flag(un); 6555 } 6556 } else { 6557 /* 6558 * Unlikely that it would be retryable then not. 6559 */ 6560 if (un->un_state == ST_STATE_RESOURCE_WAIT) { 6561 un->un_state = un->un_laststate; 6562 } 6563 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 6564 "perm no resources for pkt errno = 0x%x\n", 6565 status); 6566 } 6567 return; 6568 } 6569 /* 6570 * Worked this time set the state back. 6571 */ 6572 if (un->un_state == ST_STATE_RESOURCE_WAIT) { 6573 un->un_state = un->un_laststate; 6574 } 6575 } 6576 6577 if (queued) { 6578 /* 6579 * move from waitq to runq 6580 */ 6581 (void) st_remove_from_queue(&un->un_quef, &un->un_quel, bp); 6582 st_add_to_queue(&un->un_runqf, &un->un_runql, un->un_runql, bp); 6583 } 6584 6585 6586 #ifdef STDEBUG 6587 st_start_dump(un, bp); 6588 #endif 6589 6590 /* could not get here if throttle was zero */ 6591 un->un_last_throttle = un->un_throttle; 6592 un->un_throttle = 0; /* so nothing else will come in here */ 6593 un->un_ncmds++; 6594 6595 ST_DO_KSTATS(bp, kstat_waitq_to_runq); 6596 6597 status = st_transport(un, BP_PKT(bp)); 6598 6599 if (un->un_last_throttle) { 6600 un->un_throttle = un->un_last_throttle; 6601 } 6602 6603 if (status != TRAN_ACCEPT) { 6604 ST_DO_KSTATS(bp, kstat_runq_back_to_waitq); 6605 ST_DEBUG(ST_DEVINFO, st_label, CE_WARN, 6606 "Unhappy transport packet status 0x%x\n", status); 6607 6608 if (status == TRAN_BUSY) { 6609 pkt_info *pkti = BP_PKT(bp)->pkt_private; 6610 6611 /* 6612 * If command recovery is enabled and this isn't 6613 * a recovery command try command recovery. 6614 */ 6615 if (pkti->privatelen == sizeof (recov_info) && 6616 bp != un->un_recov_buf) { 6617 ST_RECOV(ST_DEVINFO, st_label, CE_WARN, 6618 "Command Recovery called on busy send\n"); 6619 if (st_command_recovery(un, BP_PKT(bp), 6620 ATTEMPT_RETRY) == JUST_RETURN) { 6621 return; 6622 } 6623 } else { 6624 mutex_exit(ST_MUTEX); 6625 if (st_handle_start_busy(un, bp, 6626 ST_TRAN_BUSY_TIMEOUT, queued) == 0) { 6627 mutex_enter(ST_MUTEX); 6628 return; 6629 } 6630 /* 6631 * if too many retries, fail the transport 6632 */ 6633 mutex_enter(ST_MUTEX); 6634 } 6635 } 6636 scsi_log(ST_DEVINFO, st_label, CE_WARN, 6637 "transport rejected %d\n", status); 6638 bp->b_resid = bp->b_bcount; 6639 6640 ST_DO_KSTATS(bp, kstat_waitq_exit); 6641 ST_DO_ERRSTATS(un, st_transerrs); 6642 if ((bp == un->un_recov_buf) && (status == TRAN_BUSY)) { 6643 st_bioerror(bp, EBUSY); 6644 } else { 6645 st_bioerror(bp, EIO); 6646 st_set_pe_flag(un); 6647 } 6648 st_done_and_mutex_exit(un, bp); 6649 mutex_enter(ST_MUTEX); 6650 } 6651 6652 ASSERT(mutex_owned(ST_MUTEX)); 6653 } 6654 6655 /* 6656 * if the transport is busy, then put this bp back on the waitq 6657 */ 6658 static int 6659 st_handle_start_busy(struct scsi_tape *un, struct buf *bp, 6660 clock_t timeout_interval, int queued) 6661 { 6662 6663 pkt_info *pktinfo = BP_PKT(bp)->pkt_private; 6664 6665 ST_FUNC(ST_DEVINFO, st_handle_start_busy); 6666 6667 mutex_enter(ST_MUTEX); 6668 6669 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 6670 "st_handle_start_busy()\n"); 6671 6672 /* 6673 * Check to see if we hit the retry timeout and one last check for 6674 * making sure this is the last on the runq, if it is not, we have 6675 * to fail 6676 */ 6677 if ((pktinfo->str_retry_cnt++ > st_retry_count) || 6678 ((queued) && (un->un_runql != bp))) { 6679 mutex_exit(ST_MUTEX); 6680 return (-1); 6681 } 6682 6683 if (queued) { 6684 /* put the bp back on the waitq */ 6685 st_add_to_queue(&un->un_quef, &un->un_quel, un->un_quef, bp); 6686 } 6687 6688 /* 6689 * Decrement un_ncmds so that this 6690 * gets thru' st_start() again. 6691 */ 6692 un->un_ncmds--; 6693 6694 if (queued) { 6695 /* 6696 * since this is an error case, we won't have to do this list 6697 * walking much. We've already made sure this bp was the 6698 * last on the runq 6699 */ 6700 (void) st_remove_from_queue(&un->un_runqf, &un->un_runql, bp); 6701 6702 /* 6703 * send a marker pkt, if appropriate 6704 */ 6705 st_hba_unflush(un); 6706 6707 } 6708 /* 6709 * all queues are aligned, we are just waiting to 6710 * transport, don't alloc any more buf p's, when 6711 * st_start is reentered. 6712 */ 6713 (void) timeout(st_start_restart, un, timeout_interval); 6714 6715 mutex_exit(ST_MUTEX); 6716 return (0); 6717 } 6718 6719 6720 /* 6721 * st_runout a callback that is called what a resource allocatation failed 6722 */ 6723 static int 6724 st_runout(caddr_t arg) 6725 { 6726 struct scsi_tape *un = (struct scsi_tape *)arg; 6727 struct buf *bp; 6728 int queued; 6729 6730 ASSERT(un != NULL); 6731 6732 ST_FUNC(ST_DEVINFO, st_runout); 6733 6734 mutex_enter(ST_MUTEX); 6735 6736 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, "st_runout()\n"); 6737 6738 if (un->un_recov_buf_busy != 0) { 6739 bp = un->un_recov_buf; 6740 queued = 0; 6741 } else if (un->un_sbuf_busy != 0) { 6742 /* sbuf commands should only happen with an empty queue. */ 6743 ASSERT(un->un_quef == NULL); 6744 ASSERT(un->un_runqf == NULL); 6745 bp = un->un_sbufp; 6746 queued = 0; 6747 } else if (un->un_quef != NULL) { 6748 bp = un->un_quef; 6749 if (un->un_persistence && un->un_persist_errors) { 6750 mutex_exit(ST_MUTEX); 6751 bp->b_resid = bp->b_bcount; 6752 biodone(bp); 6753 return (1); 6754 } 6755 queued = 1; 6756 } else { 6757 ASSERT(1 == 0); 6758 mutex_exit(ST_MUTEX); 6759 return (1); 6760 } 6761 6762 /* 6763 * failed scsi_init_pkt(). If errno is zero its retryable. 6764 */ 6765 if ((bp != NULL) && (geterror(bp) != 0)) { 6766 6767 scsi_log(ST_DEVINFO, st_label, CE_WARN, 6768 "errors after pkt alloc (b_flags=0x%x, b_error=0x%x)\n", 6769 bp->b_flags, geterror(bp)); 6770 ASSERT((bp->b_flags & B_DONE) == 0); 6771 6772 if (queued) { 6773 (void) st_remove_from_queue(&un->un_quef, &un->un_quel, 6774 bp); 6775 } 6776 mutex_exit(ST_MUTEX); 6777 6778 ASSERT((bp->b_flags & B_DONE) == 0); 6779 6780 /* 6781 * Set resid, Error already set, then unblock calling thread. 6782 */ 6783 bp->b_resid = bp->b_bcount; 6784 biodone(bp); 6785 } else { 6786 /* 6787 * Try Again 6788 */ 6789 st_start(un); 6790 mutex_exit(ST_MUTEX); 6791 } 6792 6793 /* 6794 * Comments courtesy of sd.c 6795 * The scsi_init_pkt routine allows for the callback function to 6796 * return a 0 indicating the callback should be rescheduled or a 1 6797 * indicating not to reschedule. This routine always returns 1 6798 * because the driver always provides a callback function to 6799 * scsi_init_pkt. This results in a callback always being scheduled 6800 * (via the scsi_init_pkt callback implementation) if a resource 6801 * failure occurs. 6802 */ 6803 6804 return (1); 6805 } 6806 6807 /* 6808 * st_done_and_mutex_exit() 6809 * - remove bp from runq 6810 * - start up the next request 6811 * - if this was an asynch bp, clean up 6812 * - exit with released mutex 6813 */ 6814 static void 6815 st_done_and_mutex_exit(struct scsi_tape *un, struct buf *bp) 6816 { 6817 int pe_flagged = 0; 6818 struct scsi_pkt *pkt = BP_PKT(bp); 6819 pkt_info *pktinfo = pkt->pkt_private; 6820 6821 ASSERT(MUTEX_HELD(&un->un_sd->sd_mutex)); 6822 #if !defined(lint) 6823 _NOTE(LOCK_RELEASED_AS_SIDE_EFFECT(&un->un_sd->sd_mutex)) 6824 #endif 6825 6826 ST_FUNC(ST_DEVINFO, st_done_and_mutex_exit); 6827 6828 ASSERT(mutex_owned(ST_MUTEX)); 6829 6830 (void) st_remove_from_queue(&un->un_runqf, &un->un_runql, bp); 6831 6832 un->un_ncmds--; 6833 cv_signal(&un->un_queue_cv); 6834 6835 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 6836 "st_done_and_mutex_exit(): cmd=0x%x count=%ld resid=%ld flags=" 6837 "0x%x\n", pkt->pkt_cdbp[0], bp->b_bcount, 6838 bp->b_resid, bp->b_flags); 6839 6840 6841 /* 6842 * update kstats with transfer count info 6843 */ 6844 if (un->un_stats && (bp != un->un_sbufp) && IS_RW(bp)) { 6845 uint32_t n_done = bp->b_bcount - bp->b_resid; 6846 if (bp->b_flags & B_READ) { 6847 IOSP->reads++; 6848 IOSP->nread += n_done; 6849 } else { 6850 IOSP->writes++; 6851 IOSP->nwritten += n_done; 6852 } 6853 } 6854 6855 /* 6856 * Start the next one before releasing resources on this one, if 6857 * there is something on the queue and persistent errors has not been 6858 * flagged 6859 */ 6860 6861 if ((pe_flagged = (un->un_persistence && un->un_persist_errors)) != 0) { 6862 un->un_last_resid = bp->b_resid; 6863 un->un_last_count = bp->b_bcount; 6864 } 6865 6866 if (un->un_pwr_mgmt == ST_PWR_SUSPENDED) { 6867 cv_broadcast(&un->un_tape_busy_cv); 6868 } else if (un->un_quef && un->un_throttle && !pe_flagged && 6869 (bp != un->un_recov_buf)) { 6870 st_start(un); 6871 } 6872 6873 un->un_retry_ct = max(pktinfo->pkt_retry_cnt, pktinfo->str_retry_cnt); 6874 6875 if (bp == un->un_sbufp && (bp->b_flags & B_ASYNC)) { 6876 /* 6877 * Since we marked this ourselves as ASYNC, 6878 * there isn't anybody around waiting for 6879 * completion any more. 6880 */ 6881 uchar_t *cmd = pkt->pkt_cdbp; 6882 if (*cmd == SCMD_READ || *cmd == SCMD_WRITE) { 6883 bp->b_un.b_addr = (caddr_t)0; 6884 } 6885 ST_DEBUG(ST_DEVINFO, st_label, CE_NOTE, 6886 "st_done_and_mutex_exit(async): freeing pkt\n"); 6887 st_print_cdb(ST_DEVINFO, st_label, CE_NOTE, 6888 "CDB sent with B_ASYNC", (caddr_t)cmd); 6889 if (pkt) { 6890 scsi_destroy_pkt(pkt); 6891 } 6892 un->un_sbuf_busy = 0; 6893 cv_signal(&un->un_sbuf_cv); 6894 mutex_exit(ST_MUTEX); 6895 return; 6896 } 6897 6898 if (bp == un->un_sbufp && BP_UCMD(bp)) { 6899 /* 6900 * Copy status from scsi_pkt to uscsi_cmd 6901 * since st_uscsi_cmd needs it 6902 */ 6903 BP_UCMD(bp)->uscsi_status = SCBP_C(BP_PKT(bp)); 6904 } 6905 6906 6907 #ifdef STDEBUG 6908 if (((st_debug & 0x7) >= 4) && 6909 (((un->un_pos.blkno % 100) == 0) || 6910 (un->un_persistence && un->un_persist_errors))) { 6911 6912 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 6913 "st_d_a_m_exit(): ncmds = %d, thr = %d, " 6914 "un_errno = %d, un_pe = %d\n", 6915 un->un_ncmds, un->un_throttle, un->un_errno, 6916 un->un_persist_errors); 6917 } 6918 6919 #endif 6920 6921 mutex_exit(ST_MUTEX); 6922 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 6923 "st_done_and_mutex_exit: freeing pkt\n"); 6924 6925 if (pkt) { 6926 scsi_destroy_pkt(pkt); 6927 } 6928 6929 biodone(bp); 6930 6931 /* 6932 * now that we biodoned that command, if persistent errors have been 6933 * flagged, flush the waitq 6934 */ 6935 if (pe_flagged) 6936 st_flush(un); 6937 } 6938 6939 6940 /* 6941 * Tape error, flush tape driver queue. 6942 */ 6943 static void 6944 st_flush(struct scsi_tape *un) 6945 { 6946 struct buf *bp; 6947 6948 ST_FUNC(ST_DEVINFO, st_flush); 6949 6950 mutex_enter(ST_MUTEX); 6951 6952 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 6953 "st_flush(), ncmds = %d, quef = 0x%p\n", 6954 un->un_ncmds, (void *)un->un_quef); 6955 6956 /* 6957 * if we still have commands outstanding, wait for them to come in 6958 * before flushing the queue, and make sure there is a queue 6959 */ 6960 if (un->un_ncmds || !un->un_quef) 6961 goto exit; 6962 6963 /* 6964 * we have no more commands outstanding, so let's deal with special 6965 * cases in the queue for EOM and FM. If we are here, and un_errno 6966 * is 0, then we know there was no error and we return a 0 read or 6967 * write before showing errors 6968 */ 6969 6970 /* Flush the wait queue. */ 6971 while ((bp = un->un_quef) != NULL) { 6972 un->un_quef = bp->b_actf; 6973 6974 bp->b_resid = bp->b_bcount; 6975 6976 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 6977 "st_flush() : blkno=%d, err=%d, b_bcount=%ld\n", 6978 un->un_pos.blkno, un->un_errno, bp->b_bcount); 6979 6980 st_set_pe_errno(un); 6981 6982 bioerror(bp, un->un_errno); 6983 6984 mutex_exit(ST_MUTEX); 6985 /* it should have one, but check anyway */ 6986 if (BP_PKT(bp)) { 6987 scsi_destroy_pkt(BP_PKT(bp)); 6988 } 6989 biodone(bp); 6990 mutex_enter(ST_MUTEX); 6991 } 6992 6993 /* 6994 * It's not a bad practice to reset the 6995 * waitq tail pointer to NULL. 6996 */ 6997 un->un_quel = NULL; 6998 6999 exit: 7000 /* we mucked with the queue, so let others know about it */ 7001 cv_signal(&un->un_queue_cv); 7002 mutex_exit(ST_MUTEX); 7003 } 7004 7005 7006 /* 7007 * Utility functions 7008 */ 7009 static int 7010 st_determine_generic(struct scsi_tape *un) 7011 { 7012 int bsize; 7013 static char *cart = "0.25 inch cartridge"; 7014 char *sizestr; 7015 7016 ST_FUNC(ST_DEVINFO, st_determine_generic); 7017 7018 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 7019 "st_determine_generic(un = 0x%p)\n", (void*)un); 7020 7021 ASSERT(mutex_owned(ST_MUTEX)); 7022 7023 if (st_modesense(un)) { 7024 return (-1); 7025 } 7026 7027 bsize = (un->un_mspl->high_bl << 16) | 7028 (un->un_mspl->mid_bl << 8) | 7029 (un->un_mspl->low_bl); 7030 7031 if (bsize == 0) { 7032 un->un_dp->options |= ST_VARIABLE; 7033 un->un_dp->bsize = 0; 7034 un->un_bsize = 0; 7035 } else if (bsize > ST_MAXRECSIZE_FIXED) { 7036 /* 7037 * record size of this device too big. 7038 * try and convert it to variable record length. 7039 * 7040 */ 7041 un->un_dp->options |= ST_VARIABLE; 7042 if (st_change_block_size(un, 0) != 0) { 7043 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 7044 "Fixed Record Size %d is too large\n", bsize); 7045 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 7046 "Cannot switch to variable record size\n"); 7047 un->un_dp->options &= ~ST_VARIABLE; 7048 return (-1); 7049 } 7050 } else if (st_change_block_size(un, 0) == 0) { 7051 /* 7052 * If the drive was set to a non zero block size, 7053 * See if it can be set to a zero block size. 7054 * If it works, ST_VARIABLE so user can set it as they want. 7055 */ 7056 un->un_dp->options |= ST_VARIABLE; 7057 un->un_dp->bsize = 0; 7058 un->un_bsize = 0; 7059 } else { 7060 un->un_dp->bsize = bsize; 7061 un->un_bsize = bsize; 7062 } 7063 7064 7065 switch (un->un_mspl->density) { 7066 default: 7067 case 0x0: 7068 /* 7069 * default density, cannot determine any other 7070 * information. 7071 */ 7072 sizestr = "Unknown type- assuming 0.25 inch cartridge"; 7073 un->un_dp->type = ST_TYPE_DEFAULT; 7074 un->un_dp->options |= (ST_AUTODEN_OVERRIDE|ST_QIC); 7075 break; 7076 case 0x1: 7077 case 0x2: 7078 case 0x3: 7079 case 0x6: 7080 /* 7081 * 1/2" reel 7082 */ 7083 sizestr = "0.50 inch reel"; 7084 un->un_dp->type = ST_TYPE_REEL; 7085 un->un_dp->options |= ST_REEL; 7086 un->un_dp->densities[0] = 0x1; 7087 un->un_dp->densities[1] = 0x2; 7088 un->un_dp->densities[2] = 0x6; 7089 un->un_dp->densities[3] = 0x3; 7090 break; 7091 case 0x4: 7092 case 0x5: 7093 case 0x7: 7094 case 0x0b: 7095 7096 /* 7097 * Quarter inch. 7098 */ 7099 sizestr = cart; 7100 un->un_dp->type = ST_TYPE_DEFAULT; 7101 un->un_dp->options |= ST_QIC; 7102 7103 un->un_dp->densities[1] = 0x4; 7104 un->un_dp->densities[2] = 0x5; 7105 un->un_dp->densities[3] = 0x7; 7106 un->un_dp->densities[0] = 0x0b; 7107 break; 7108 7109 case 0x0f: 7110 case 0x10: 7111 case 0x11: 7112 case 0x12: 7113 /* 7114 * QIC-120, QIC-150, QIC-320, QIC-600 7115 */ 7116 sizestr = cart; 7117 un->un_dp->type = ST_TYPE_DEFAULT; 7118 un->un_dp->options |= ST_QIC; 7119 un->un_dp->densities[0] = 0x0f; 7120 un->un_dp->densities[1] = 0x10; 7121 un->un_dp->densities[2] = 0x11; 7122 un->un_dp->densities[3] = 0x12; 7123 break; 7124 7125 case 0x09: 7126 case 0x0a: 7127 case 0x0c: 7128 case 0x0d: 7129 /* 7130 * 1/2" cartridge tapes. Include HI-TC. 7131 */ 7132 sizestr = cart; 7133 sizestr[2] = '5'; 7134 sizestr[3] = '0'; 7135 un->un_dp->type = ST_TYPE_HIC; 7136 un->un_dp->densities[0] = 0x09; 7137 un->un_dp->densities[1] = 0x0a; 7138 un->un_dp->densities[2] = 0x0c; 7139 un->un_dp->densities[3] = 0x0d; 7140 break; 7141 7142 case 0x13: 7143 /* DDS-2/DDS-3 scsi spec densities */ 7144 case 0x24: 7145 case 0x25: 7146 case 0x26: 7147 sizestr = "DAT Data Storage (DDS)"; 7148 un->un_dp->type = ST_TYPE_DAT; 7149 un->un_dp->options |= ST_AUTODEN_OVERRIDE; 7150 break; 7151 7152 case 0x14: 7153 /* 7154 * Helical Scan (Exabyte) devices 7155 */ 7156 sizestr = "8mm helical scan cartridge"; 7157 un->un_dp->type = ST_TYPE_EXABYTE; 7158 un->un_dp->options |= ST_AUTODEN_OVERRIDE; 7159 break; 7160 } 7161 7162 /* 7163 * Assume LONG ERASE, BSF and BSR 7164 */ 7165 7166 un->un_dp->options |= 7167 (ST_LONG_ERASE | ST_UNLOADABLE | ST_BSF | ST_BSR | ST_KNOWS_EOD); 7168 7169 /* 7170 * Only if mode sense data says no buffered write, set NOBUF 7171 */ 7172 if (un->un_mspl->bufm == 0) 7173 un->un_dp->options |= ST_NOBUF; 7174 7175 /* 7176 * set up large read and write retry counts 7177 */ 7178 7179 un->un_dp->max_rretries = un->un_dp->max_wretries = 1000; 7180 7181 /* 7182 * If this is a 0.50 inch reel tape, and 7183 * it is *not* variable mode, try and 7184 * set it to variable record length 7185 * mode. 7186 */ 7187 if ((un->un_dp->options & ST_REEL) && un->un_bsize != 0 && 7188 (un->un_dp->options & ST_VARIABLE)) { 7189 if (st_change_block_size(un, 0) == 0) { 7190 un->un_dp->bsize = 0; 7191 un->un_mspl->high_bl = un->un_mspl->mid_bl = 7192 un->un_mspl->low_bl = 0; 7193 } 7194 } 7195 7196 /* 7197 * Write to console about type of device found 7198 */ 7199 ST_DEBUG6(ST_DEVINFO, st_label, CE_NOTE, 7200 "Generic Drive, Vendor=%s\n\t%s", un->un_dp->name, 7201 sizestr); 7202 if (un->un_dp->options & ST_VARIABLE) { 7203 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 7204 "!Variable record length I/O\n"); 7205 } else { 7206 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 7207 "!Fixed record length (%d byte blocks) I/O\n", 7208 un->un_dp->bsize); 7209 } 7210 ASSERT(mutex_owned(ST_MUTEX)); 7211 return (0); 7212 } 7213 7214 static int 7215 st_determine_density(struct scsi_tape *un, int rw) 7216 { 7217 int rval = 0; 7218 7219 ST_FUNC(ST_DEVINFO, st_determine_density); 7220 7221 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 7222 "st_determine_density(un = 0x%p, rw = %s)\n", 7223 (void*)un, (rw == B_WRITE ? wr_str: rd_str)); 7224 7225 ASSERT(mutex_owned(ST_MUTEX)); 7226 7227 /* 7228 * If we're past BOT, density is determined already. 7229 */ 7230 if (un->un_pos.pmode == logical) { 7231 if (un->un_pos.lgclblkno != 0) { 7232 goto exit; 7233 } 7234 } else if (un->un_pos.pmode == legacy) { 7235 if ((un->un_pos.fileno != 0) || (un->un_pos.blkno != 0)) { 7236 /* 7237 * XXX: put in a bitch message about attempting to 7238 * XXX: change density past BOT. 7239 */ 7240 goto exit; 7241 } 7242 } else { 7243 goto exit; 7244 } 7245 if ((un->un_pos.pmode == logical) && 7246 (un->un_pos.lgclblkno != 0)) { 7247 goto exit; 7248 } 7249 7250 7251 /* 7252 * If we're going to be writing, we set the density 7253 */ 7254 if (rw == 0 || rw == B_WRITE) { 7255 /* un_curdens is used as an index into densities table */ 7256 un->un_curdens = MT_DENSITY(un->un_dev); 7257 if (st_set_density(un)) { 7258 rval = -1; 7259 } 7260 goto exit; 7261 } 7262 7263 /* 7264 * If density is known already, 7265 * we don't have to get it again.(?) 7266 */ 7267 if (!un->un_density_known) { 7268 if (st_get_density(un)) { 7269 rval = -1; 7270 } 7271 } 7272 7273 exit: 7274 ASSERT(mutex_owned(ST_MUTEX)); 7275 return (rval); 7276 } 7277 7278 7279 /* 7280 * Try to determine density. We do this by attempting to read the 7281 * first record off the tape, cycling through the available density 7282 * codes as we go. 7283 */ 7284 7285 static int 7286 st_get_density(struct scsi_tape *un) 7287 { 7288 int succes = 0, rval = -1, i; 7289 uint_t size; 7290 uchar_t dens, olddens; 7291 7292 ST_FUNC(ST_DEVINFO, st_get_density); 7293 7294 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 7295 "st_get_density(un = 0x%p)\n", (void*)un); 7296 7297 ASSERT(mutex_owned(ST_MUTEX)); 7298 7299 /* 7300 * If Auto Density override is enabled The drive has 7301 * only one density and there is no point in attempting 7302 * find the correct one. 7303 * 7304 * Since most modern drives auto detect the density 7305 * and format of the recorded media before they come 7306 * ready. What this function does is a legacy behavior 7307 * and modern drives not only don't need it, The backup 7308 * utilities that do positioning via uscsi find the un- 7309 * expected rewinds problematic. 7310 * 7311 * The drives that need this are old reel to reel devices. 7312 * I took a swag and said they must be scsi-1 or older. 7313 * I don't beleave there will any of the newer devices 7314 * that need this. There will be some scsi-1 devices that 7315 * don't need this but I don't think they will be using the 7316 * BIG aftermarket backup and restore utilitys. 7317 */ 7318 if ((un->un_dp->options & ST_AUTODEN_OVERRIDE) || 7319 (un->un_sd->sd_inq->inq_ansi > 1)) { 7320 un->un_density_known = 1; 7321 rval = 0; 7322 goto exit; 7323 } 7324 7325 /* 7326 * This will only work on variable record length tapes 7327 * if and only if all variable record length tapes autodensity 7328 * select. 7329 */ 7330 size = (unsigned)(un->un_dp->bsize ? un->un_dp->bsize : SECSIZE); 7331 un->un_tmpbuf = kmem_alloc(size, KM_SLEEP); 7332 7333 /* 7334 * Start at the specified density 7335 */ 7336 7337 dens = olddens = un->un_curdens = MT_DENSITY(un->un_dev); 7338 7339 for (i = 0; i < NDENSITIES; i++, ((un->un_curdens == NDENSITIES - 1) ? 7340 (un->un_curdens = 0) : (un->un_curdens += 1))) { 7341 /* 7342 * If we've done this density before, 7343 * don't bother to do it again. 7344 */ 7345 dens = un->un_dp->densities[un->un_curdens]; 7346 if (i > 0 && dens == olddens) 7347 continue; 7348 olddens = dens; 7349 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7350 "trying density 0x%x\n", dens); 7351 if (st_set_density(un)) { 7352 continue; 7353 } 7354 7355 /* 7356 * XXX - the creates lots of headaches and slowdowns - must 7357 * fix. 7358 */ 7359 succes = (st_cmd(un, SCMD_READ, (int)size, SYNC_CMD) == 0); 7360 if (st_cmd(un, SCMD_REWIND, 0, SYNC_CMD)) { 7361 break; 7362 } 7363 if (succes) { 7364 st_init(un); 7365 rval = 0; 7366 un->un_density_known = 1; 7367 break; 7368 } 7369 } 7370 kmem_free(un->un_tmpbuf, size); 7371 un->un_tmpbuf = 0; 7372 7373 exit: 7374 ASSERT(mutex_owned(ST_MUTEX)); 7375 return (rval); 7376 } 7377 7378 static int 7379 st_set_density(struct scsi_tape *un) 7380 { 7381 int rval = 0; 7382 7383 ST_FUNC(ST_DEVINFO, st_set_density); 7384 7385 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 7386 "st_set_density(un = 0x%p): density = 0x%x\n", (void*)un, 7387 un->un_dp->densities[un->un_curdens]); 7388 7389 ASSERT(mutex_owned(ST_MUTEX)); 7390 7391 un->un_mspl->density = un->un_dp->densities[un->un_curdens]; 7392 7393 if ((un->un_dp->options & ST_AUTODEN_OVERRIDE) == 0) { 7394 /* 7395 * If auto density override is not set, Use mode select 7396 * to set density and compression. 7397 */ 7398 if (st_modeselect(un)) { 7399 rval = -1; 7400 } 7401 } else if ((un->un_dp->options & ST_MODE_SEL_COMP) != 0) { 7402 /* 7403 * If auto density and mode select compression are set, 7404 * This is a drive with one density code but compression 7405 * can be enabled or disabled. 7406 * Set compression but no need to set density. 7407 */ 7408 rval = st_set_compression(un); 7409 if ((rval != 0) && (rval != EALREADY)) { 7410 rval = -1; 7411 } else { 7412 rval = 0; 7413 } 7414 } 7415 7416 /* If sucessful set density and/or compression, mark density known */ 7417 if (rval == 0) { 7418 un->un_density_known = 1; 7419 } 7420 7421 ASSERT(mutex_owned(ST_MUTEX)); 7422 return (rval); 7423 } 7424 7425 static int 7426 st_loadtape(struct scsi_tape *un) 7427 { 7428 int rval; 7429 7430 ST_FUNC(ST_DEVINFO, st_loadtape); 7431 7432 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 7433 "st_loadtape(un = 0x%p)\n", (void*) un); 7434 7435 ASSERT(mutex_owned(ST_MUTEX)); 7436 7437 rval = st_update_block_pos(un, st_cmd, 0); 7438 if (rval == EACCES) { 7439 return (rval); 7440 } 7441 7442 /* 7443 * 'LOAD' the tape to BOT by rewinding 7444 */ 7445 rval = st_cmd(un, SCMD_REWIND, 1, SYNC_CMD); 7446 if (rval == 0) { 7447 st_init(un); 7448 un->un_density_known = 0; 7449 } 7450 7451 ASSERT(mutex_owned(ST_MUTEX)); 7452 return (rval); 7453 } 7454 7455 7456 /* 7457 * Note: QIC devices aren't so smart. If you try to append 7458 * after EOM, the write can fail because the device doesn't know 7459 * it's at EOM. In that case, issue a read. The read should fail 7460 * because there's no data, but the device knows it's at EOM, 7461 * so a subsequent write should succeed. To further confuse matters, 7462 * the target returns the same error if the tape is positioned 7463 * such that a write would overwrite existing data. That's why 7464 * we have to do the append test. A read in the middle of 7465 * recorded data would succeed, thus indicating we're attempting 7466 * something illegal. 7467 */ 7468 7469 7470 static void 7471 st_test_append(struct buf *bp) 7472 { 7473 dev_t dev = bp->b_edev; 7474 struct scsi_tape *un; 7475 uchar_t status; 7476 unsigned bcount; 7477 7478 un = ddi_get_soft_state(st_state, MTUNIT(dev)); 7479 7480 ST_FUNC(ST_DEVINFO, st_test_append); 7481 7482 ASSERT(mutex_owned(ST_MUTEX)); 7483 7484 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 7485 "st_test_append(): fileno %d\n", un->un_pos.fileno); 7486 7487 un->un_laststate = un->un_state; 7488 un->un_state = ST_STATE_APPEND_TESTING; 7489 un->un_test_append = 0; 7490 7491 /* 7492 * first, map in the buffer, because we're doing a double write -- 7493 * first into the kernel, then onto the tape. 7494 */ 7495 bp_mapin(bp); 7496 7497 /* 7498 * get a copy of the data.... 7499 */ 7500 un->un_tmpbuf = kmem_alloc((unsigned)bp->b_bcount, KM_SLEEP); 7501 bcopy(bp->b_un.b_addr, un->un_tmpbuf, (uint_t)bp->b_bcount); 7502 7503 /* 7504 * attempt the write.. 7505 */ 7506 7507 if (st_cmd(un, (int)SCMD_WRITE, (int)bp->b_bcount, SYNC_CMD) == 0) { 7508 success: 7509 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7510 "append write succeeded\n"); 7511 bp->b_resid = un->un_sbufp->b_resid; 7512 mutex_exit(ST_MUTEX); 7513 bcount = (unsigned)bp->b_bcount; 7514 biodone(bp); 7515 mutex_enter(ST_MUTEX); 7516 un->un_laststate = un->un_state; 7517 un->un_state = ST_STATE_OPEN; 7518 kmem_free(un->un_tmpbuf, bcount); 7519 un->un_tmpbuf = NULL; 7520 return; 7521 } 7522 7523 /* 7524 * The append failed. Do a short read. If that fails, we are at EOM 7525 * so we can retry the write command. If that succeeds, than we're 7526 * all screwed up (the controller reported a real error). 7527 * 7528 * XXX: should the dummy read be > SECSIZE? should it be the device's 7529 * XXX: block size? 7530 * 7531 */ 7532 status = un->un_status; 7533 un->un_status = 0; 7534 (void) st_cmd(un, SCMD_READ, SECSIZE, SYNC_CMD); 7535 if (un->un_status == KEY_BLANK_CHECK) { 7536 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7537 "append at EOM\n"); 7538 /* 7539 * Okay- the read failed. We should actually have confused 7540 * the controller enough to allow writing. In any case, the 7541 * i/o is on its own from here on out. 7542 */ 7543 un->un_laststate = un->un_state; 7544 un->un_state = ST_STATE_OPEN; 7545 bcopy(bp->b_un.b_addr, un->un_tmpbuf, (uint_t)bp->b_bcount); 7546 if (st_cmd(un, (int)SCMD_WRITE, (int)bp->b_bcount, 7547 SYNC_CMD) == 0) { 7548 goto success; 7549 } 7550 } 7551 7552 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7553 "append write failed- not at EOM\n"); 7554 bp->b_resid = bp->b_bcount; 7555 st_bioerror(bp, EIO); 7556 7557 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 7558 "st_test_append : EIO : append write failed - not at EOM"); 7559 7560 /* 7561 * backspace one record to get back to where we were 7562 */ 7563 if (st_cmd(un, SCMD_SPACE, Blk(-1), SYNC_CMD)) { 7564 un->un_pos.pmode = invalid; 7565 } 7566 7567 un->un_err_resid = bp->b_resid; 7568 un->un_status = status; 7569 7570 /* 7571 * Note: biodone will do a bp_mapout() 7572 */ 7573 mutex_exit(ST_MUTEX); 7574 bcount = (unsigned)bp->b_bcount; 7575 biodone(bp); 7576 mutex_enter(ST_MUTEX); 7577 un->un_laststate = un->un_state; 7578 un->un_state = ST_STATE_OPEN_PENDING_IO; 7579 kmem_free(un->un_tmpbuf, bcount); 7580 un->un_tmpbuf = NULL; 7581 } 7582 7583 /* 7584 * Special command handler 7585 */ 7586 7587 /* 7588 * common st_cmd code. The fourth parameter states 7589 * whether the caller wishes to await the results 7590 * Note the release of the mutex during most of the function 7591 */ 7592 static int 7593 st_cmd(struct scsi_tape *un, int com, int64_t count, int wait) 7594 { 7595 struct buf *bp; 7596 int err; 7597 uint_t last_err_resid; 7598 7599 ST_FUNC(ST_DEVINFO, st_cmd); 7600 7601 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 7602 "st_cmd(dev = 0x%lx, com = 0x%x, count = %"PRIx64", wait = %d)\n", 7603 un->un_dev, com, count, wait); 7604 7605 ASSERT(MUTEX_HELD(&un->un_sd->sd_mutex)); 7606 ASSERT(mutex_owned(ST_MUTEX)); 7607 7608 #ifdef STDEBUG 7609 if ((st_debug & 0x7)) { 7610 st_debug_cmds(un, com, count, wait); 7611 } 7612 #endif 7613 7614 st_wait_for_io(un); 7615 7616 /* check to see if this command requires the drive to be reserved */ 7617 err = st_check_cmd_for_need_to_reserve(un, com, count); 7618 7619 if (err) { 7620 return (err); 7621 } 7622 7623 /* 7624 * A space command is not recoverable if we don't know were we 7625 * were when it was issued. 7626 */ 7627 if ((com == SCMD_SPACE) || (com == SCMD_SPACE_G4)) { 7628 (void) st_update_block_pos(un, st_cmd, 0); 7629 } 7630 7631 /* 7632 * Forground should not be doing anything while recovery is active. 7633 */ 7634 ASSERT(un->un_recov_buf_busy == 0); 7635 7636 while (un->un_sbuf_busy) 7637 cv_wait(&un->un_sbuf_cv, ST_MUTEX); 7638 un->un_sbuf_busy = 1; 7639 7640 bp = un->un_sbufp; 7641 bzero(bp, sizeof (buf_t)); 7642 7643 bp->b_flags = (wait) ? B_BUSY : B_BUSY|B_ASYNC; 7644 7645 err = st_setup_cmd(un, bp, com, count); 7646 7647 un->un_sbuf_busy = 0; 7648 7649 /* 7650 * If was a space command need to update logical block position. 7651 * Only do this if the command was sucessful or it will mask the fact 7652 * that the space command failed by promoting the pmode to logical. 7653 */ 7654 if (((com == SCMD_SPACE) || (com == SCMD_SPACE_G4)) && 7655 (un->un_pos.pmode != invalid)) { 7656 un->un_running.pmode = invalid; 7657 last_err_resid = un->un_err_resid; 7658 (void) st_update_block_pos(un, st_cmd, 1); 7659 /* 7660 * Set running position to invalid so it updates on the 7661 * next command. 7662 */ 7663 un->un_running.pmode = invalid; 7664 un->un_err_resid = last_err_resid; 7665 } 7666 7667 cv_signal(&un->un_sbuf_cv); 7668 7669 return (err); 7670 } 7671 7672 static int 7673 st_setup_cmd(struct scsi_tape *un, buf_t *bp, int com, int64_t count) 7674 { 7675 int err; 7676 dev_t dev = un->un_dev; 7677 7678 ST_FUNC(ST_DEVINFO, st_setup_cmd); 7679 /* 7680 * Set count to the actual size of the data tranfer. 7681 * For commands with no data transfer, set bp->b_bcount 7682 * to the value to be used when constructing the 7683 * cdb in st_make_cmd(). 7684 */ 7685 switch (com) { 7686 case SCMD_READ: 7687 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7688 "special read %"PRId64"\n", count); 7689 bp->b_flags |= B_READ; 7690 bp->b_un.b_addr = un->un_tmpbuf; 7691 break; 7692 7693 case SCMD_WRITE: 7694 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7695 "special write %"PRId64"\n", count); 7696 bp->b_un.b_addr = un->un_tmpbuf; 7697 break; 7698 7699 case SCMD_WRITE_FILE_MARK: 7700 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7701 "write %"PRId64" file marks\n", count); 7702 bp->b_bcount = count; 7703 count = 0; 7704 break; 7705 7706 case SCMD_REWIND: 7707 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "rewind\n"); 7708 bp->b_bcount = count; 7709 count = 0; 7710 break; 7711 7712 case SCMD_SPACE: 7713 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "space\n"); 7714 /* 7715 * If the user could have entered a number that will 7716 * not fit in the 12 bit count field of space(8), 7717 * use space(16). 7718 */ 7719 if (((int64_t)SPACE_CNT(count) > 0x7fffff) || 7720 ((int64_t)SPACE_CNT(count) < -(0x7fffff))) { 7721 com = SCMD_SPACE_G4; 7722 } 7723 bp->b_bcount = count; 7724 count = 0; 7725 break; 7726 7727 case SCMD_RESERVE: 7728 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "reserve"); 7729 bp->b_bcount = 0; 7730 count = 0; 7731 break; 7732 7733 case SCMD_RELEASE: 7734 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "release"); 7735 bp->b_bcount = 0; 7736 count = 0; 7737 break; 7738 7739 case SCMD_LOAD: 7740 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7741 "%s tape\n", (count & LD_LOAD) ? "load" : "unload"); 7742 bp->b_bcount = count; 7743 count = 0; 7744 break; 7745 7746 case SCMD_ERASE: 7747 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7748 "erase tape\n"); 7749 bp->b_bcount = count; 7750 count = 0; 7751 break; 7752 7753 case SCMD_MODE_SENSE: 7754 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7755 "mode sense\n"); 7756 bp->b_flags |= B_READ; 7757 bp->b_un.b_addr = (caddr_t)(un->un_mspl); 7758 break; 7759 7760 case SCMD_MODE_SELECT: 7761 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7762 "mode select\n"); 7763 bp->b_un.b_addr = (caddr_t)(un->un_mspl); 7764 break; 7765 7766 case SCMD_READ_BLKLIM: 7767 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7768 "read block limits\n"); 7769 bp->b_bcount = count; 7770 bp->b_flags |= B_READ; 7771 bp->b_un.b_addr = (caddr_t)(un->un_rbl); 7772 break; 7773 7774 case SCMD_TEST_UNIT_READY: 7775 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7776 "test unit ready\n"); 7777 bp->b_bcount = 0; 7778 count = 0; 7779 break; 7780 7781 case SCMD_DOORLOCK: 7782 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7783 "%s tape\n", (count & MR_LOCK) ? "lock" : "unlock"); 7784 bp->b_bcount = count = 0; 7785 break; 7786 7787 case SCMD_READ_POSITION: 7788 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7789 "read position\n"); 7790 switch (un->un_read_pos_type) { 7791 case LONG_POS: 7792 count = sizeof (tape_position_long_t); 7793 break; 7794 case EXT_POS: 7795 count = min(count, sizeof (tape_position_ext_t)); 7796 break; 7797 case SHORT_POS: 7798 count = sizeof (tape_position_t); 7799 break; 7800 default: 7801 ST_DEBUG(ST_DEVINFO, st_label, CE_PANIC, 7802 "Unknown read position type 0x%x in " 7803 "st_make_cmd()\n", un->un_read_pos_type); 7804 } 7805 bp->b_bcount = count; 7806 bp->b_flags |= B_READ; 7807 bp->b_un.b_addr = (caddr_t)un->un_read_pos_data; 7808 break; 7809 7810 default: 7811 ST_DEBUG(ST_DEVINFO, st_label, CE_PANIC, 7812 "Unhandled scsi command 0x%x in st_setup_cmd()\n", com); 7813 } 7814 7815 mutex_exit(ST_MUTEX); 7816 7817 if (count > 0) { 7818 int flg = (bp->b_flags & B_READ) ? B_READ : B_WRITE; 7819 /* 7820 * We're going to do actual I/O. 7821 * Set things up for physio. 7822 */ 7823 struct iovec aiov; 7824 struct uio auio; 7825 struct uio *uio = &auio; 7826 7827 bzero(&auio, sizeof (struct uio)); 7828 bzero(&aiov, sizeof (struct iovec)); 7829 aiov.iov_base = bp->b_un.b_addr; 7830 aiov.iov_len = count; 7831 7832 uio->uio_iov = &aiov; 7833 uio->uio_iovcnt = 1; 7834 uio->uio_resid = aiov.iov_len; 7835 uio->uio_segflg = UIO_SYSSPACE; 7836 7837 /* 7838 * Let physio do the rest... 7839 */ 7840 bp->b_forw = (struct buf *)(uintptr_t)com; 7841 bp->b_back = NULL; 7842 err = physio(st_strategy, bp, dev, flg, st_minphys, uio); 7843 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7844 "st_setup_cmd: physio returns %d\n", err); 7845 } else { 7846 /* 7847 * Mimic physio 7848 */ 7849 bp->b_forw = (struct buf *)(uintptr_t)com; 7850 bp->b_back = NULL; 7851 bp->b_edev = dev; 7852 bp->b_dev = cmpdev(dev); 7853 bp->b_blkno = 0; 7854 bp->b_resid = 0; 7855 (void) st_strategy(bp); 7856 if (bp->b_flags & B_ASYNC) { 7857 /* 7858 * This is an async command- the caller won't wait 7859 * and doesn't care about errors. 7860 */ 7861 mutex_enter(ST_MUTEX); 7862 return (0); 7863 } 7864 7865 /* 7866 * BugTraq #4260046 7867 * ---------------- 7868 * Restore Solaris 2.5.1 behavior, namely call biowait 7869 * unconditionally. The old comment said... 7870 * 7871 * "if strategy was flagged with persistent errors, we would 7872 * have an error here, and the bp would never be sent, so we 7873 * don't want to wait on a bp that was never sent...or hang" 7874 * 7875 * The new rationale, courtesy of Chitrank... 7876 * 7877 * "we should unconditionally biowait() here because 7878 * st_strategy() will do a biodone() in the persistent error 7879 * case and the following biowait() will return immediately. 7880 * If not, in the case of "errors after pkt alloc" in 7881 * st_start(), we will not biowait here which will cause the 7882 * next biowait() to return immediately which will cause 7883 * us to send out the next command. In the case where both of 7884 * these use the sbuf, when the first command completes we'll 7885 * free the packet attached to sbuf and the same pkt will 7886 * get freed again when we complete the second command. 7887 * see esc 518987. BTW, it is necessary to do biodone() in 7888 * st_start() for the pkt alloc failure case because physio() 7889 * does biowait() and will hang if we don't do biodone()" 7890 */ 7891 7892 err = biowait(bp); 7893 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7894 "st_setup_cmd: biowait returns %d\n", err); 7895 } 7896 7897 mutex_enter(ST_MUTEX); 7898 7899 return (err); 7900 } 7901 7902 static int 7903 st_set_compression(struct scsi_tape *un) 7904 { 7905 int rval; 7906 int turn_compression_on; 7907 minor_t minor; 7908 7909 ST_FUNC(ST_DEVINFO, st_set_compression); 7910 7911 /* 7912 * Drive either dosn't have compression or it is controlled with 7913 * special density codes. Return ENOTTY so caller 7914 * knows nothing was done. 7915 */ 7916 if ((un->un_dp->options & ST_MODE_SEL_COMP) == 0) { 7917 un->un_comp_page = 0; 7918 return (ENOTTY); 7919 } 7920 7921 /* set compression based on minor node opened */ 7922 minor = MT_DENSITY(un->un_dev); 7923 7924 /* 7925 * If this the compression density or 7926 * the drive has two densities and uses mode select for 7927 * control of compression turn on compression for MT_DENSITY2 7928 * as well. 7929 */ 7930 if ((minor == ST_COMPRESSION_DENSITY) || 7931 (minor == MT_DENSITY(MT_DENSITY2)) && 7932 (un->un_dp->densities[0] == un->un_dp->densities[1]) && 7933 (un->un_dp->densities[2] == un->un_dp->densities[3]) && 7934 (un->un_dp->densities[0] != un->un_dp->densities[2])) { 7935 7936 turn_compression_on = 1; 7937 } else { 7938 turn_compression_on = 0; 7939 } 7940 7941 un->un_mspl->high_bl = (uchar_t)(un->un_bsize >> 16); 7942 un->un_mspl->mid_bl = (uchar_t)(un->un_bsize >> 8); 7943 un->un_mspl->low_bl = (uchar_t)(un->un_bsize); 7944 7945 /* 7946 * Need to determine which page does the device use for compression. 7947 * First try the data compression page. If this fails try the device 7948 * configuration page 7949 */ 7950 7951 if ((un->un_comp_page & ST_DEV_DATACOMP_PAGE) == ST_DEV_DATACOMP_PAGE) { 7952 rval = st_set_datacomp_page(un, turn_compression_on); 7953 if (rval == EALREADY) { 7954 return (rval); 7955 } 7956 if (rval != 0) { 7957 if (un->un_status == KEY_ILLEGAL_REQUEST) { 7958 /* 7959 * This device does not support data 7960 * compression page 7961 */ 7962 un->un_comp_page = ST_DEV_CONFIG_PAGE; 7963 } else if (un->un_state >= ST_STATE_OPEN) { 7964 un->un_pos.pmode = invalid; 7965 rval = EIO; 7966 } else { 7967 rval = -1; 7968 } 7969 } else { 7970 un->un_comp_page = ST_DEV_DATACOMP_PAGE; 7971 } 7972 } 7973 7974 if ((un->un_comp_page & ST_DEV_CONFIG_PAGE) == ST_DEV_CONFIG_PAGE) { 7975 rval = st_set_devconfig_page(un, turn_compression_on); 7976 if (rval == EALREADY) { 7977 return (rval); 7978 } 7979 if (rval != 0) { 7980 if (un->un_status == KEY_ILLEGAL_REQUEST) { 7981 /* 7982 * This device does not support 7983 * compression at all advice the 7984 * user and unset ST_MODE_SEL_COMP 7985 */ 7986 un->un_dp->options &= ~ST_MODE_SEL_COMP; 7987 un->un_comp_page = 0; 7988 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 7989 "Device Does Not Support Compression\n"); 7990 } else if (un->un_state >= ST_STATE_OPEN) { 7991 un->un_pos.pmode = invalid; 7992 rval = EIO; 7993 } else { 7994 rval = -1; 7995 } 7996 } 7997 } 7998 7999 return (rval); 8000 } 8001 8002 /* 8003 * set or unset compression thru device configuration page. 8004 */ 8005 static int 8006 st_set_devconfig_page(struct scsi_tape *un, int compression_on) 8007 { 8008 unsigned char cflag; 8009 int rval = 0; 8010 8011 8012 ST_FUNC(ST_DEVINFO, st_set_devconfig_page); 8013 8014 ASSERT(mutex_owned(ST_MUTEX)); 8015 8016 /* 8017 * if the mode sense page is not the correct one, load the correct one. 8018 */ 8019 if (un->un_mspl->page_code != ST_DEV_CONFIG_PAGE) { 8020 rval = st_gen_mode_sense(un, st_uscsi_cmd, ST_DEV_CONFIG_PAGE, 8021 un->un_mspl, sizeof (struct seq_mode)); 8022 if (rval) 8023 return (rval); 8024 } 8025 8026 /* 8027 * Figure what to set compression flag to. 8028 */ 8029 if (compression_on) { 8030 /* They have selected a compression node */ 8031 if (un->un_dp->type == ST_TYPE_FUJI) { 8032 cflag = 0x84; /* use EDRC */ 8033 } else { 8034 cflag = ST_DEV_CONFIG_DEF_COMP; 8035 } 8036 } else { 8037 cflag = ST_DEV_CONFIG_NO_COMP; 8038 } 8039 8040 /* 8041 * If compression is already set the way it was requested. 8042 * And if this not the first time we has tried. 8043 */ 8044 if ((cflag == un->un_mspl->page.dev.comp_alg) && 8045 (un->un_comp_page == ST_DEV_CONFIG_PAGE)) { 8046 return (EALREADY); 8047 } 8048 8049 un->un_mspl->page.dev.comp_alg = cflag; 8050 /* 8051 * need to send mode select even if correct compression is 8052 * already set since need to set density code 8053 */ 8054 8055 #ifdef STDEBUG 8056 if ((st_debug & 0x7) >= 6) { 8057 st_clean_print(ST_DEVINFO, st_label, SCSI_DEBUG, 8058 "st_set_devconfig_page: sense data for mode select", 8059 (char *)un->un_mspl, sizeof (struct seq_mode)); 8060 } 8061 #endif 8062 rval = st_gen_mode_select(un, st_uscsi_cmd, un->un_mspl, 8063 sizeof (struct seq_mode)); 8064 8065 return (<