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 2007 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 /* 30 * SCSI SCSA-compliant and not-so-DDI-compliant Tape Driver 31 */ 32 33 #if defined(lint) && !defined(DEBUG) 34 #define DEBUG 1 35 #endif 36 37 #include <sys/modctl.h> 38 #include <sys/scsi/scsi.h> 39 #include <sys/mtio.h> 40 #include <sys/scsi/targets/stdef.h> 41 #include <sys/file.h> 42 #include <sys/kstat.h> 43 #include <sys/ddidmareq.h> 44 #include <sys/ddi.h> 45 #include <sys/sunddi.h> 46 47 #define IOSP KSTAT_IO_PTR(un->un_stats) 48 /* 49 * stats maintained only for reads/writes as commands 50 * like rewind etc skew the wait/busy times 51 */ 52 #define IS_RW(bp) ((bp)->b_bcount > 0) 53 #define ST_DO_KSTATS(bp, kstat_function) \ 54 if ((bp != un->un_sbufp) && un->un_stats && IS_RW(bp)) { \ 55 kstat_function(IOSP); \ 56 } 57 58 #define ST_DO_ERRSTATS(un, x) \ 59 if (un->un_errstats) { \ 60 struct st_errstats *stp; \ 61 stp = (struct st_errstats *)un->un_errstats->ks_data; \ 62 stp->x.value.ul++; \ 63 } 64 65 #define FILL_SCSI1_LUN(devp, pkt) \ 66 if ((devp)->sd_inq->inq_ansi == 0x1) { \ 67 int _lun; \ 68 _lun = ddi_prop_get_int(DDI_DEV_T_ANY, (devp)->sd_dev, \ 69 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_LUN, 0); \ 70 if (_lun > 0) { \ 71 ((union scsi_cdb *)(pkt)->pkt_cdbp)->scc_lun = \ 72 _lun; \ 73 } \ 74 } 75 76 /* 77 * get an available contig mem header, cp. 78 * when big_enough is true, we will return NULL, if no big enough 79 * contig mem is found. 80 * when big_enough is false, we will try to find cp containing big 81 * enough contig mem. if not found, we will ruturn the last cp available. 82 * 83 * used by st_get_contig_mem() 84 */ 85 #define ST_GET_CONTIG_MEM_HEAD(un, cp, len, big_enough) { \ 86 struct contig_mem *tmp_cp = NULL; \ 87 for ((cp) = (un)->un_contig_mem; \ 88 (cp) != NULL; \ 89 tmp_cp = (cp), (cp) = (cp)->cm_next) { \ 90 if (((cp)->cm_len >= (len)) || \ 91 (!(big_enough) && ((cp)->cm_next == NULL))) { \ 92 if (tmp_cp == NULL) { \ 93 (un)->un_contig_mem = (cp)->cm_next; \ 94 } else { \ 95 tmp_cp->cm_next = (cp)->cm_next; \ 96 } \ 97 (cp)->cm_next = NULL; \ 98 (un)->un_contig_mem_available_num--; \ 99 break; \ 100 } \ 101 } \ 102 } 103 104 #define ST_NUM_MEMBERS(array) (sizeof (array) / sizeof (array[0])) 105 #define COPY_POS(dest, source) bcopy(source, dest, sizeof (tapepos_t)) 106 107 #define ONE_K 1024 108 109 /* 110 * Global External Data Definitions 111 */ 112 extern struct scsi_key_strings scsi_cmds[]; 113 extern uchar_t scsi_cdb_size[]; 114 115 /* 116 * Local Static Data 117 */ 118 static void *st_state; 119 static char *const st_label = "st"; 120 121 #ifdef __x86 122 /* 123 * We need to use below DMA attr to alloc physically contiguous 124 * memory to do I/O in big block size 125 */ 126 static ddi_dma_attr_t st_contig_mem_dma_attr = { 127 DMA_ATTR_V0, /* version number */ 128 0x0, /* lowest usable address */ 129 0xFFFFFFFFull, /* high DMA address range */ 130 0xFFFFFFFFull, /* DMA counter register */ 131 1, /* DMA address alignment */ 132 1, /* DMA burstsizes */ 133 1, /* min effective DMA size */ 134 0xFFFFFFFFull, /* max DMA xfer size */ 135 0xFFFFFFFFull, /* segment boundary */ 136 1, /* s/g list length */ 137 1, /* granularity of device */ 138 0 /* DMA transfer flags */ 139 }; 140 141 static ddi_device_acc_attr_t st_acc_attr = { 142 DDI_DEVICE_ATTR_V0, 143 DDI_NEVERSWAP_ACC, 144 DDI_STRICTORDER_ACC 145 }; 146 147 /* set limitation for the number of contig_mem */ 148 static int st_max_contig_mem_num = ST_MAX_CONTIG_MEM_NUM; 149 #endif 150 151 /* 152 * Tunable parameters 153 * 154 * DISCLAIMER 155 * ---------- 156 * These parameters are intended for use only in system testing; if you use 157 * them in production systems, you do so at your own risk. Altering any 158 * variable not listed below may cause unpredictable system behavior. 159 * 160 * st_check_media_time 161 * 162 * Three second state check 163 * 164 * st_allow_large_xfer 165 * 166 * Gated with ST_NO_RECSIZE_LIMIT 167 * 168 * 0 - Transfers larger than 64KB will not be allowed 169 * regardless of the setting of ST_NO_RECSIZE_LIMIT 170 * 1 - Transfers larger than 64KB will be allowed 171 * if ST_NO_RECSIZE_LIMIT is TRUE for the drive 172 * 173 * st_report_soft_errors_on_close 174 * 175 * Gated with ST_SOFT_ERROR_REPORTING 176 * 177 * 0 - Errors will not be reported on close regardless 178 * of the setting of ST_SOFT_ERROR_REPORTING 179 * 180 * 1 - Errors will be reported on close if 181 * ST_SOFT_ERROR_REPORTING is TRUE for the drive 182 */ 183 static int st_selection_retry_count = ST_SEL_RETRY_COUNT; 184 static int st_retry_count = ST_RETRY_COUNT; 185 186 static int st_io_time = ST_IO_TIME; 187 static int st_long_timeout_x = ST_LONG_TIMEOUT_X; 188 189 static int st_space_time = ST_SPACE_TIME; 190 static int st_long_space_time_x = ST_LONG_SPACE_TIME_X; 191 192 static int st_error_level = SCSI_ERR_RETRYABLE; 193 static int st_check_media_time = 3000000; /* 3 Second State Check */ 194 195 static int st_max_throttle = ST_MAX_THROTTLE; 196 197 static clock_t st_wait_cmds_complete = ST_WAIT_CMDS_COMPLETE; 198 199 static int st_allow_large_xfer = 1; 200 static int st_report_soft_errors_on_close = 1; 201 202 /* 203 * End of tunable parameters list 204 */ 205 206 207 208 /* 209 * Asynchronous I/O and persistent errors, refer to PSARC/1995/228 210 * 211 * Asynchronous I/O's main offering is that it is a non-blocking way to do 212 * reads and writes. The driver will queue up all the requests it gets and 213 * have them ready to transport to the HBA. Unfortunately, we cannot always 214 * just ship the I/O requests to the HBA, as there errors and exceptions 215 * that may happen when we don't want the HBA to continue. Therein comes 216 * the flush-on-errors capability. If the HBA supports it, then st will 217 * send in st_max_throttle I/O requests at the same time. 218 * 219 * Persistent errors : This was also reasonably simple. In the interrupt 220 * routines, if there was an error or exception (FM, LEOT, media error, 221 * transport error), the persistent error bits are set and shuts everything 222 * down, but setting the throttle to zero. If we hit and exception in the 223 * HBA, and flush-on-errors were set, we wait for all outstanding I/O's to 224 * come back (with CMD_ABORTED), then flush all bp's in the wait queue with 225 * the appropriate error, and this will preserve order. Of course, depending 226 * on the exception we have to show a zero read or write before we show 227 * errors back to the application. 228 */ 229 230 extern const int st_ndrivetypes; /* defined in st_conf.c */ 231 extern const struct st_drivetype st_drivetypes[]; 232 extern const char st_conf_version[]; 233 234 #ifdef STDEBUG 235 static int st_soft_error_report_debug = 0; 236 volatile int st_debug = 0; 237 #endif 238 239 #define ST_MT02_NAME "Emulex MT02 QIC-11/24 " 240 241 static const struct driver_minor_data { 242 char *name; 243 int minor; 244 } st_minor_data[] = { 245 /* 246 * The top 4 entries are for the default densities, 247 * don't alter their position. 248 */ 249 {"", 0}, 250 {"n", MT_NOREWIND}, 251 {"b", MT_BSD}, 252 {"bn", MT_NOREWIND | MT_BSD}, 253 {"l", MT_DENSITY1}, 254 {"m", MT_DENSITY2}, 255 {"h", MT_DENSITY3}, 256 {"c", MT_DENSITY4}, 257 {"u", MT_DENSITY4}, 258 {"ln", MT_DENSITY1 | MT_NOREWIND}, 259 {"mn", MT_DENSITY2 | MT_NOREWIND}, 260 {"hn", MT_DENSITY3 | MT_NOREWIND}, 261 {"cn", MT_DENSITY4 | MT_NOREWIND}, 262 {"un", MT_DENSITY4 | MT_NOREWIND}, 263 {"lb", MT_DENSITY1 | MT_BSD}, 264 {"mb", MT_DENSITY2 | MT_BSD}, 265 {"hb", MT_DENSITY3 | MT_BSD}, 266 {"cb", MT_DENSITY4 | MT_BSD}, 267 {"ub", MT_DENSITY4 | MT_BSD}, 268 {"lbn", MT_DENSITY1 | MT_NOREWIND | MT_BSD}, 269 {"mbn", MT_DENSITY2 | MT_NOREWIND | MT_BSD}, 270 {"hbn", MT_DENSITY3 | MT_NOREWIND | MT_BSD}, 271 {"cbn", MT_DENSITY4 | MT_NOREWIND | MT_BSD}, 272 {"ubn", MT_DENSITY4 | MT_NOREWIND | MT_BSD} 273 }; 274 275 /* strings used in many debug and warning messages */ 276 static const char wr_str[] = "write"; 277 static const char rd_str[] = "read"; 278 static const char wrg_str[] = "writing"; 279 static const char rdg_str[] = "reading"; 280 static const char *space_strs[] = { 281 "records", 282 "filemarks", 283 "sequential filemarks", 284 "eod", 285 "setmarks", 286 "sequential setmarks", 287 "Reserved", 288 "Reserved" 289 }; 290 291 /* default density offsets in the table above */ 292 #define DEF_BLANK 0 293 #define DEF_NOREWIND 1 294 #define DEF_BSD 2 295 #define DEF_BSD_NR 3 296 297 /* Sense Key, ASC/ASCQ for which tape ejection is needed */ 298 299 static struct tape_failure_code { 300 uchar_t key; 301 uchar_t add_code; 302 uchar_t qual_code; 303 } st_tape_failure_code[] = { 304 { KEY_HARDWARE_ERROR, 0x15, 0x01}, 305 { KEY_HARDWARE_ERROR, 0x44, 0x00}, 306 { KEY_HARDWARE_ERROR, 0x53, 0x00}, 307 { KEY_HARDWARE_ERROR, 0x53, 0x01}, 308 { KEY_NOT_READY, 0x53, 0x00}, 309 { 0xff} 310 }; 311 312 /* clean bit position and mask */ 313 314 static struct cln_bit_position { 315 ushort_t cln_bit_byte; 316 uchar_t cln_bit_mask; 317 } st_cln_bit_position[] = { 318 { 21, 0x08}, 319 { 70, 0xc0}, 320 { 18, 0x81} /* 80 bit indicates in bit mode, 1 bit clean light is on */ 321 }; 322 323 /* 324 * architecture dependent allocation restrictions. For x86, we'll set 325 * dma_attr_addr_hi to st_max_phys_addr and dma_attr_sgllen to 326 * st_sgl_size during _init(). 327 */ 328 #if defined(__sparc) 329 static ddi_dma_attr_t st_alloc_attr = { 330 DMA_ATTR_V0, /* version number */ 331 0x0, /* lowest usable address */ 332 0xFFFFFFFFull, /* high DMA address range */ 333 0xFFFFFFFFull, /* DMA counter register */ 334 1, /* DMA address alignment */ 335 1, /* DMA burstsizes */ 336 1, /* min effective DMA size */ 337 0xFFFFFFFFull, /* max DMA xfer size */ 338 0xFFFFFFFFull, /* segment boundary */ 339 1, /* s/g list length */ 340 512, /* granularity of device */ 341 0 /* DMA transfer flags */ 342 }; 343 #elif defined(__x86) 344 static ddi_dma_attr_t st_alloc_attr = { 345 DMA_ATTR_V0, /* version number */ 346 0x0, /* lowest usable address */ 347 0x0, /* high DMA address range [set in _init()] */ 348 0xFFFFull, /* DMA counter register */ 349 512, /* DMA address alignment */ 350 1, /* DMA burstsizes */ 351 1, /* min effective DMA size */ 352 0xFFFFFFFFull, /* max DMA xfer size */ 353 0xFFFFFFFFull, /* segment boundary */ 354 0, /* s/g list length */ 355 512, /* granularity of device [set in _init()] */ 356 0 /* DMA transfer flags */ 357 }; 358 uint64_t st_max_phys_addr = 0xFFFFFFFFull; 359 int st_sgl_size = 0xF; 360 361 #endif 362 363 /* 364 * Configuration Data: 365 * 366 * Device driver ops vector 367 */ 368 static int st_aread(dev_t dev, struct aio_req *aio, cred_t *cred_p); 369 static int st_awrite(dev_t dev, struct aio_req *aio, cred_t *cred_p); 370 static int st_read(dev_t dev, struct uio *uio_p, cred_t *cred_p); 371 static int st_write(dev_t dev, struct uio *uio_p, cred_t *cred_p); 372 static int st_open(dev_t *devp, int flag, int otyp, cred_t *cred_p); 373 static int st_close(dev_t dev, int flag, int otyp, cred_t *cred_p); 374 static int st_strategy(struct buf *bp); 375 static int st_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, 376 cred_t *cred_p, int *rval_p); 377 extern int nulldev(), nodev(); 378 379 static struct cb_ops st_cb_ops = { 380 st_open, /* open */ 381 st_close, /* close */ 382 st_strategy, /* strategy */ 383 nodev, /* print */ 384 nodev, /* dump */ 385 st_read, /* read */ 386 st_write, /* write */ 387 st_ioctl, /* ioctl */ 388 nodev, /* devmap */ 389 nodev, /* mmap */ 390 nodev, /* segmap */ 391 nochpoll, /* poll */ 392 ddi_prop_op, /* cb_prop_op */ 393 0, /* streamtab */ 394 D_64BIT | D_MP | D_NEW | D_HOTPLUG | 395 D_OPEN_RETURNS_EINTR, /* cb_flag */ 396 CB_REV, /* cb_rev */ 397 st_aread, /* async I/O read entry point */ 398 st_awrite /* async I/O write entry point */ 399 400 }; 401 402 static int stinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, 403 void **result); 404 static int st_probe(dev_info_t *dev); 405 static int st_attach(dev_info_t *dev, ddi_attach_cmd_t cmd); 406 static int st_detach(dev_info_t *dev, ddi_detach_cmd_t cmd); 407 408 static struct dev_ops st_ops = { 409 DEVO_REV, /* devo_rev, */ 410 0, /* refcnt */ 411 stinfo, /* info */ 412 nulldev, /* identify */ 413 st_probe, /* probe */ 414 st_attach, /* attach */ 415 st_detach, /* detach */ 416 nodev, /* reset */ 417 &st_cb_ops, /* driver operations */ 418 (struct bus_ops *)0, /* bus operations */ 419 nulldev /* power */ 420 }; 421 422 /* 423 * Local Function Declarations 424 */ 425 static char *st_print_scsi_cmd(char cmd); 426 static void st_print_cdb(dev_info_t *dip, char *label, uint_t level, 427 char *title, char *cdb); 428 static void st_clean_print(dev_info_t *dev, char *label, uint_t level, 429 char *title, char *data, int len); 430 static int st_doattach(struct scsi_device *devp, int (*canwait)()); 431 static void st_known_tape_type(struct scsi_tape *un); 432 static int st_get_conf_from_st_dot_conf(struct scsi_tape *, char *, 433 struct st_drivetype *); 434 static int st_get_conf_from_st_conf_dot_c(struct scsi_tape *, char *, 435 struct st_drivetype *); 436 static int st_get_default_conf(struct scsi_tape *, char *, 437 struct st_drivetype *); 438 static int st_rw(dev_t dev, struct uio *uio, int flag); 439 static int st_arw(dev_t dev, struct aio_req *aio, int flag); 440 static int st_find_eod(dev_t dev); 441 static int st_check_density_or_wfm(dev_t dev, int wfm, int mode, int stepflag); 442 static int st_ioctl_cmd(dev_t dev, struct uscsi_cmd *, int flag); 443 static int st_mtioctop(struct scsi_tape *un, intptr_t arg, int flag); 444 static int st_mtiocltop(struct scsi_tape *un, intptr_t arg, int flag); 445 static int st_do_mtioctop(struct scsi_tape *un, struct mtlop *mtop); 446 static void st_start(struct scsi_tape *un); 447 static int st_handle_start_busy(struct scsi_tape *un, struct buf *bp, 448 clock_t timeout_interval); 449 static int st_handle_intr_busy(struct scsi_tape *un, struct buf *bp, 450 clock_t timeout_interval); 451 static int st_handle_intr_retry_lcmd(struct scsi_tape *un, struct buf *bp); 452 static void st_done_and_mutex_exit(struct scsi_tape *un, struct buf *bp); 453 static void st_init(struct scsi_tape *un); 454 static void st_make_cmd(struct scsi_tape *un, struct buf *bp, 455 int (*func)(caddr_t)); 456 static void st_make_uscsi_cmd(struct scsi_tape *, struct uscsi_cmd *, 457 struct buf *bp, int (*func)(caddr_t)); 458 static void st_intr(struct scsi_pkt *pkt); 459 static void st_set_state(struct scsi_tape *un); 460 static void st_test_append(struct buf *bp); 461 static int st_runout(caddr_t); 462 static int st_cmd(dev_t dev, int com, int count, int wait); 463 static int st_set_compression(struct scsi_tape *un); 464 static int st_write_fm(dev_t dev, int wfm); 465 static int st_determine_generic(dev_t dev); 466 static int st_determine_density(dev_t dev, int rw); 467 static int st_get_density(dev_t dev); 468 static int st_set_density(dev_t dev); 469 static int st_loadtape(dev_t dev); 470 static int st_modesense(struct scsi_tape *un); 471 static int st_modeselect(struct scsi_tape *un); 472 static int st_handle_incomplete(struct scsi_tape *un, struct buf *bp); 473 static int st_wrongtapetype(struct scsi_tape *un); 474 static int st_check_error(struct scsi_tape *un, struct scsi_pkt *pkt); 475 static int st_handle_sense(struct scsi_tape *un, struct buf *bp); 476 static int st_handle_autosense(struct scsi_tape *un, struct buf *bp); 477 static int st_decode_sense(struct scsi_tape *un, struct buf *bp, int amt, 478 struct scsi_status *); 479 static int st_get_error_entry(struct scsi_tape *un, intptr_t arg, int flag); 480 static void st_update_error_stack(struct scsi_tape *un, struct scsi_pkt *pkt, 481 struct scsi_arq_status *cmd); 482 static void st_empty_error_stack(struct scsi_tape *un); 483 static int st_report_soft_errors(dev_t dev, int flag); 484 static void st_delayed_cv_broadcast(void *arg); 485 static int st_check_media(dev_t dev, enum mtio_state state); 486 static int st_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp); 487 static void st_intr_restart(void *arg); 488 static void st_start_restart(void *arg); 489 static int st_gen_mode_sense(struct scsi_tape *un, int page, 490 struct seq_mode *page_data, int page_size); 491 static int st_change_block_size(dev_t dev, uint32_t nblksz); 492 static int st_gen_mode_select(struct scsi_tape *un, struct seq_mode *page_data, 493 int page_size); 494 static int st_tape_init(dev_t dev); 495 static void st_flush(struct scsi_tape *un); 496 static void st_set_pe_errno(struct scsi_tape *un); 497 static void st_hba_unflush(struct scsi_tape *un); 498 static void st_turn_pe_on(struct scsi_tape *un); 499 static void st_turn_pe_off(struct scsi_tape *un); 500 static void st_set_pe_flag(struct scsi_tape *un); 501 static void st_clear_pe(struct scsi_tape *un); 502 static void st_wait_for_io(struct scsi_tape *un); 503 static int st_set_devconfig_page(struct scsi_tape *un, int compression_on); 504 static int st_set_datacomp_page(struct scsi_tape *un, int compression_on); 505 static int st_reserve_release(struct scsi_tape *un, int command); 506 static int st_check_cdb_for_need_to_reserve(struct scsi_tape *un, caddr_t cdb); 507 static int st_check_cmd_for_need_to_reserve(struct scsi_tape *un, uchar_t cmd, 508 int count); 509 static int st_take_ownership(dev_t dev); 510 static int st_check_asc_ascq(struct scsi_tape *un); 511 static int st_check_clean_bit(dev_t dev); 512 static int st_check_alert_flags(dev_t dev); 513 static int st_check_sequential_clean_bit(dev_t dev); 514 static int st_check_sense_clean_bit(dev_t dev); 515 static int st_clear_unit_attentions(dev_t dev_instance, int max_trys); 516 static void st_calculate_timeouts(struct scsi_tape *un); 517 static writablity st_is_drive_worm(struct scsi_tape *un); 518 static int st_read_attributes(struct scsi_tape *un, uint16_t attribute, 519 caddr_t buf, size_t size); 520 static int st_get_special_inquiry(struct scsi_tape *un, uchar_t size, 521 caddr_t dest, uchar_t page); 522 static int st_update_block_pos(struct scsi_tape *un); 523 static int st_interpret_read_pos(struct scsi_tape *un, read_p_types type, 524 size_t data_sz, caddr_t responce); 525 static int st_get_read_pos(struct scsi_tape *un, buf_t *bp); 526 static int st_logical_block_locate(struct scsi_tape *un, uint64_t lblk, 527 uchar_t partition); 528 static int st_mtfsf_ioctl(struct scsi_tape *un, int files); 529 static int st_mtfsr_ioctl(struct scsi_tape *un, int count); 530 static int st_mtbsf_ioctl(struct scsi_tape *un, int files); 531 static int st_mtnbsf_ioctl(struct scsi_tape *un, int count); 532 static int st_mtbsr_ioctl(struct scsi_tape *un, int num); 533 static int st_mtfsfm_ioctl(struct scsi_tape *un, int cnt); 534 static int st_mtbsfm_ioctl(struct scsi_tape *un, int cnt); 535 static int st_backward_space_files(struct scsi_tape *un, int count, 536 int infront); 537 static int st_forward_space_files(struct scsi_tape *un, int files); 538 static int st_scenic_route_to_begining_of_file(struct scsi_tape *un, 539 int32_t fileno); 540 static int st_space_to_begining_of_file(struct scsi_tape *un); 541 static int st_space_records(struct scsi_tape *un, int records); 542 543 #ifdef __x86 544 /* 545 * routines for I/O in big block size 546 */ 547 static void st_release_contig_mem(struct scsi_tape *un, struct contig_mem *cp); 548 static struct contig_mem *st_get_contig_mem(struct scsi_tape *un, size_t len, 549 int alloc_flags); 550 static int st_bigblk_xfer_done(struct buf *bp); 551 static struct buf *st_get_bigblk_bp(struct buf *bp); 552 #endif 553 static void st_print_position(struct scsi_tape *un, const char *comment, 554 tapepos_t *pos); 555 556 /* 557 * error statistics create/update functions 558 */ 559 static int st_create_errstats(struct scsi_tape *, int); 560 static int st_validate_tapemarks(struct scsi_tape *un, tapepos_t *pos); 561 562 #ifdef STDEBUG 563 static void st_debug_cmds(struct scsi_tape *un, int com, int count, int wait); 564 static char *st_dev_name(dev_t dev); 565 #endif /* STDEBUG */ 566 567 #if !defined(lint) 568 _NOTE(SCHEME_PROTECTS_DATA("unique per pkt", 569 scsi_pkt buf uio scsi_cdb uscsi_cmd)) 570 _NOTE(SCHEME_PROTECTS_DATA("unique per pkt", scsi_extended_sense scsi_status)) 571 _NOTE(SCHEME_PROTECTS_DATA("stable data", scsi_device)) 572 _NOTE(DATA_READABLE_WITHOUT_LOCK(st_drivetype scsi_address)) 573 #endif 574 575 /* 576 * autoconfiguration routines. 577 */ 578 char _depends_on[] = "misc/scsi"; 579 580 static struct modldrv modldrv = { 581 &mod_driverops, /* Type of module. This one is a driver */ 582 "SCSI tape Driver %I%", /* Name of the module. */ 583 &st_ops /* driver ops */ 584 }; 585 586 static struct modlinkage modlinkage = { 587 MODREV_1, &modldrv, NULL 588 }; 589 590 /* 591 * Notes on Post Reset Behavior in the tape driver: 592 * 593 * When the tape drive is opened, the driver attempts to make sure that 594 * the tape head is positioned exactly where it was left when it was last 595 * closed provided the medium is not changed. If the tape drive is 596 * opened in O_NDELAY mode, the repositioning (if necessary for any loss 597 * of position due to reset) will happen when the first tape operation or 598 * I/O occurs. The repositioning (if required) may not be possible under 599 * certain situations such as when the device firmware not able to report 600 * the medium change in the REQUEST SENSE data because of a reset or a 601 * misbehaving bus not allowing the reposition to happen. In such 602 * extraordinary situations, where the driver fails to position the head 603 * at its original position, it will fail the open the first time, to 604 * save the applications from overwriting the data. All further attempts 605 * to open the tape device will result in the driver attempting to load 606 * the tape at BOT (beginning of tape). Also a warning message to 607 * indicate that further attempts to open the tape device may result in 608 * the tape being loaded at BOT will be printed on the console. If the 609 * tape device is opened in O_NDELAY mode, failure to restore the 610 * original tape head position, will result in the failure of the first 611 * tape operation or I/O, Further, the driver will invalidate its 612 * internal tape position which will necessitate the applications to 613 * validate the position by using either a tape positioning ioctl (such 614 * as MTREW) or closing and reopening the tape device. 615 * 616 */ 617 618 int 619 _init(void) 620 { 621 int e; 622 623 if (((e = ddi_soft_state_init(&st_state, 624 sizeof (struct scsi_tape), ST_MAXUNIT)) != 0)) { 625 return (e); 626 } 627 628 if ((e = mod_install(&modlinkage)) != 0) { 629 ddi_soft_state_fini(&st_state); 630 } 631 632 #if defined(__x86) 633 /* set the max physical address for iob allocs on x86 */ 634 st_alloc_attr.dma_attr_addr_hi = st_max_phys_addr; 635 636 /* 637 * set the sgllen for iob allocs on x86. If this is set less than 638 * the number of pages the buffer will take (taking into account 639 * alignment), it would force the allocator to try and allocate 640 * contiguous pages. 641 */ 642 st_alloc_attr.dma_attr_sgllen = st_sgl_size; 643 #endif 644 645 return (e); 646 } 647 648 int 649 _fini(void) 650 { 651 int e; 652 653 if ((e = mod_remove(&modlinkage)) != 0) { 654 return (e); 655 } 656 657 ddi_soft_state_fini(&st_state); 658 659 return (e); 660 } 661 662 int 663 _info(struct modinfo *modinfop) 664 { 665 return (mod_info(&modlinkage, modinfop)); 666 } 667 668 669 static int 670 st_probe(dev_info_t *devi) 671 { 672 int instance; 673 struct scsi_device *devp; 674 int rval; 675 676 #if !defined(__sparc) 677 char *tape_prop; 678 int tape_prop_len; 679 #endif 680 681 ST_ENTR(devi, st_probe); 682 683 /* If self identifying device */ 684 if (ddi_dev_is_sid(devi) == DDI_SUCCESS) { 685 return (DDI_PROBE_DONTCARE); 686 } 687 688 #if !defined(__sparc) 689 /* 690 * Since some x86 HBAs have devnodes that look like SCSI as 691 * far as we can tell but aren't really SCSI (DADK, like mlx) 692 * we check for the presence of the "tape" property. 693 */ 694 if (ddi_prop_op(DDI_DEV_T_NONE, devi, PROP_LEN_AND_VAL_ALLOC, 695 DDI_PROP_CANSLEEP, "tape", 696 (caddr_t)&tape_prop, &tape_prop_len) != DDI_PROP_SUCCESS) { 697 return (DDI_PROBE_FAILURE); 698 } 699 if (strncmp(tape_prop, "sctp", tape_prop_len) != 0) { 700 kmem_free(tape_prop, tape_prop_len); 701 return (DDI_PROBE_FAILURE); 702 } 703 kmem_free(tape_prop, tape_prop_len); 704 #endif 705 706 devp = ddi_get_driver_private(devi); 707 instance = ddi_get_instance(devi); 708 709 if (ddi_get_soft_state(st_state, instance) != NULL) { 710 return (DDI_PROBE_PARTIAL); 711 } 712 713 714 /* 715 * Turn around and call probe routine to see whether 716 * we actually have a tape at this SCSI nexus. 717 */ 718 if (scsi_probe(devp, NULL_FUNC) == SCSIPROBE_EXISTS) { 719 720 /* 721 * In checking the whole inq_dtype byte we are looking at both 722 * the Peripheral Qualifier and the Peripheral Device Type. 723 * For this driver we are only interested in sequential devices 724 * that are connected or capable if connecting to this logical 725 * unit. 726 */ 727 if (devp->sd_inq->inq_dtype == 728 (DTYPE_SEQUENTIAL | DPQ_POSSIBLE)) { 729 ST_DEBUG6(devi, st_label, SCSI_DEBUG, 730 "probe exists\n"); 731 rval = DDI_PROBE_SUCCESS; 732 } else { 733 rval = DDI_PROBE_FAILURE; 734 } 735 } else { 736 ST_DEBUG6(devi, st_label, SCSI_DEBUG, 737 "probe failure: nothing there\n"); 738 rval = DDI_PROBE_FAILURE; 739 } 740 scsi_unprobe(devp); 741 return (rval); 742 } 743 744 static int 745 st_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 746 { 747 int instance; 748 int wide; 749 int dev_instance; 750 int ret_status; 751 struct scsi_device *devp; 752 int node_ix; 753 struct scsi_tape *un; 754 755 ST_ENTR(devi, st_attach); 756 757 devp = ddi_get_driver_private(devi); 758 instance = ddi_get_instance(devi); 759 760 switch (cmd) { 761 case DDI_ATTACH: 762 if (st_doattach(devp, SLEEP_FUNC) == DDI_FAILURE) { 763 return (DDI_FAILURE); 764 } 765 break; 766 case DDI_RESUME: 767 /* 768 * Suspend/Resume 769 * 770 * When the driver suspended, there might be 771 * outstanding cmds and therefore we need to 772 * reset the suspended flag and resume the scsi 773 * watch thread and restart commands and timeouts 774 */ 775 776 if (!(un = ddi_get_soft_state(st_state, instance))) { 777 return (DDI_FAILURE); 778 } 779 dev_instance = ((un->un_dev == 0) ? MTMINOR(instance) : 780 un->un_dev); 781 782 mutex_enter(ST_MUTEX); 783 784 un->un_throttle = un->un_max_throttle; 785 un->un_tids_at_suspend = 0; 786 un->un_pwr_mgmt = ST_PWR_NORMAL; 787 788 if (un->un_swr_token) { 789 scsi_watch_resume(un->un_swr_token); 790 } 791 792 /* 793 * Restart timeouts 794 */ 795 if ((un->un_tids_at_suspend & ST_DELAY_TID) != 0) { 796 mutex_exit(ST_MUTEX); 797 un->un_delay_tid = timeout( 798 st_delayed_cv_broadcast, un, 799 drv_usectohz((clock_t) 800 MEDIA_ACCESS_DELAY)); 801 mutex_enter(ST_MUTEX); 802 } 803 804 if (un->un_tids_at_suspend & ST_HIB_TID) { 805 mutex_exit(ST_MUTEX); 806 un->un_hib_tid = timeout(st_intr_restart, un, 807 ST_STATUS_BUSY_TIMEOUT); 808 mutex_enter(ST_MUTEX); 809 } 810 811 ret_status = st_clear_unit_attentions(dev_instance, 5); 812 813 /* 814 * now check if we need to restore the tape position 815 */ 816 if ((un->un_suspend_pos.pmode != invalid) && 817 ((un->un_suspend_pos.fileno > 0) || 818 (un->un_suspend_pos.blkno > 0)) || 819 (un->un_suspend_pos.lgclblkno > 0)) { 820 if (ret_status != 0) { 821 /* 822 * tape didn't get good TUR 823 * just print out error messages 824 */ 825 scsi_log(ST_DEVINFO, st_label, CE_WARN, 826 "st_attach-RESUME: tape failure " 827 " tape position will be lost"); 828 } else { 829 /* this prints errors */ 830 (void) st_validate_tapemarks(un, 831 &un->un_suspend_pos); 832 } 833 /* 834 * there are no retries, if there is an error 835 * we don't know if the tape has changed 836 */ 837 un->un_suspend_pos.pmode = invalid; 838 } 839 840 /* now we are ready to start up any queued I/Os */ 841 if (un->un_ncmds || un->un_quef) { 842 st_start(un); 843 } 844 845 cv_broadcast(&un->un_suspend_cv); 846 mutex_exit(ST_MUTEX); 847 return (DDI_SUCCESS); 848 849 default: 850 return (DDI_FAILURE); 851 } 852 853 un = ddi_get_soft_state(st_state, instance); 854 855 ST_DEBUG(devi, st_label, SCSI_DEBUG, 856 "st_attach: instance=%x\n", instance); 857 858 /* 859 * find the drive type for this target 860 */ 861 st_known_tape_type(un); 862 863 for (node_ix = 0; node_ix < ST_NUM_MEMBERS(st_minor_data); node_ix++) { 864 int minor; 865 char *name; 866 867 name = st_minor_data[node_ix].name; 868 minor = st_minor_data[node_ix].minor; 869 870 /* 871 * For default devices set the density to the 872 * preferred default density for this device. 873 */ 874 if (node_ix <= DEF_BSD_NR) { 875 minor |= un->un_dp->default_density; 876 } 877 minor |= MTMINOR(instance); 878 879 if (ddi_create_minor_node(devi, name, S_IFCHR, minor, 880 DDI_NT_TAPE, NULL) == DDI_SUCCESS) { 881 continue; 882 } 883 884 ddi_remove_minor_node(devi, NULL); 885 if (un) { 886 cv_destroy(&un->un_clscv); 887 cv_destroy(&un->un_sbuf_cv); 888 cv_destroy(&un->un_queue_cv); 889 cv_destroy(&un->un_state_cv); 890 cv_destroy(&un->un_suspend_cv); 891 cv_destroy(&un->un_tape_busy_cv); 892 893 if (un->un_sbufp) { 894 freerbuf(un->un_sbufp); 895 } 896 if (un->un_uscsi_rqs_buf) { 897 kmem_free(un->un_uscsi_rqs_buf, SENSE_LENGTH); 898 } 899 if (un->un_mspl) { 900 i_ddi_mem_free((caddr_t)un->un_mspl, NULL); 901 } 902 scsi_destroy_pkt(un->un_rqs); 903 scsi_free_consistent_buf(un->un_rqs_bp); 904 ddi_soft_state_free(st_state, instance); 905 devp->sd_private = NULL; 906 devp->sd_sense = NULL; 907 908 } 909 ddi_prop_remove_all(devi); 910 return (DDI_FAILURE); 911 } 912 913 /* 914 * Add a zero-length attribute to tell the world we support 915 * kernel ioctls (for layered drivers) 916 */ 917 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP, 918 DDI_KERNEL_IOCTL, NULL, 0); 919 920 ddi_report_dev((dev_info_t *)devi); 921 922 /* 923 * If it's a SCSI-2 tape drive which supports wide, 924 * tell the host adapter to use wide. 925 */ 926 wide = ((devp->sd_inq->inq_rdf == RDF_SCSI2) && 927 (devp->sd_inq->inq_wbus16 || devp->sd_inq->inq_wbus32)) ? 1 : 0; 928 929 if (scsi_ifsetcap(ROUTE, "wide-xfer", wide, 1) == 1) { 930 ST_DEBUG(devi, st_label, SCSI_DEBUG, 931 "Wide Transfer %s\n", wide ? "enabled" : "disabled"); 932 } 933 934 /* 935 * enable autorequest sense; keep the rq packet around in case 936 * the autorequest sense fails because of a busy condition 937 * do a getcap first in case the capability is not variable 938 */ 939 if (scsi_ifgetcap(ROUTE, "auto-rqsense", 1) == 1) { 940 un->un_arq_enabled = 1; 941 } else { 942 un->un_arq_enabled = 943 ((scsi_ifsetcap(ROUTE, "auto-rqsense", 1, 1) == 1) ? 1 : 0); 944 } 945 946 ST_DEBUG(devi, st_label, SCSI_DEBUG, "auto request sense %s\n", 947 (un->un_arq_enabled ? "enabled" : "disabled")); 948 949 un->un_untagged_qing = 950 (scsi_ifgetcap(ROUTE, "untagged-qing", 0) == 1); 951 952 /* 953 * XXX - This is just for 2.6. to tell users that write buffering 954 * has gone away. 955 */ 956 if (un->un_arq_enabled && un->un_untagged_qing) { 957 if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 958 "tape-driver-buffering", 0) != 0) { 959 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 960 "Write Data Buffering has been depricated. Your " 961 "applications should continue to work normally.\n" 962 " But, they should ported to use Asynchronous " 963 " I/O\n" 964 " For more information, read about " 965 " tape-driver-buffering " 966 "property in the st(7d) man page\n"); 967 } 968 } 969 970 un->un_max_throttle = un->un_throttle = un->un_last_throttle = 1; 971 un->un_flush_on_errors = 0; 972 un->un_mkr_pkt = (struct scsi_pkt *)NULL; 973 974 ST_DEBUG(devi, st_label, SCSI_DEBUG, 975 "throttle=%x, max_throttle = %x\n", 976 un->un_throttle, un->un_max_throttle); 977 978 /* initialize persistent errors to nil */ 979 un->un_persistence = 0; 980 un->un_persist_errors = 0; 981 982 /* 983 * Get dma-max from HBA driver. If it is not defined, use 64k 984 */ 985 un->un_maxdma = scsi_ifgetcap(&devp->sd_address, "dma-max", 1); 986 if (un->un_maxdma == -1) { 987 ST_DEBUG(devi, st_label, SCSI_DEBUG, 988 "Received a value that looked like -1. Using 64k maxdma"); 989 un->un_maxdma = (64 * ONE_K); 990 } 991 992 #ifdef __x86 993 /* 994 * for x86, the device may be able to DMA more than the system will 995 * allow under some circumstances. We need account for both the HBA's 996 * and system's contraints. 997 * 998 * Get the maximum DMA under worse case conditions. e.g. looking at the 999 * device constraints, the max copy buffer size, and the worse case 1000 * fragmentation. NOTE: this may differ from dma-max since dma-max 1001 * doesn't take the worse case framentation into account. 1002 * 1003 * e.g. a device may be able to DMA 16MBytes, but can only DMA 1MByte 1004 * if none of the pages are contiguous. Keeping track of both of these 1005 * values allows us to support larger tape block sizes on some devices. 1006 */ 1007 un->un_maxdma_arch = scsi_ifgetcap(&devp->sd_address, "dma-max-arch", 1008 1); 1009 1010 /* 1011 * If the dma-max-arch capability is not implemented, or the value 1012 * comes back higher than what was reported in dma-max, use dma-max. 1013 */ 1014 if ((un->un_maxdma_arch == -1) || 1015 ((uint_t)un->un_maxdma < (uint_t)un->un_maxdma_arch)) { 1016 un->un_maxdma_arch = un->un_maxdma; 1017 } 1018 #endif 1019 1020 /* 1021 * Get the max allowable cdb size 1022 */ 1023 un->un_max_cdb_sz = 1024 scsi_ifgetcap(&devp->sd_address, "max-cdb-length", 1); 1025 if (un->un_max_cdb_sz < CDB_GROUP0) { 1026 ST_DEBUG(devi, st_label, SCSI_DEBUG, 1027 "HBA reported max-cdb-length as %d\n", un->un_max_cdb_sz); 1028 un->un_max_cdb_sz = CDB_GROUP4; /* optimistic default */ 1029 } 1030 1031 un->un_maxbsize = MAXBSIZE_UNKNOWN; 1032 1033 un->un_mediastate = MTIO_NONE; 1034 un->un_HeadClean = TAPE_ALERT_SUPPORT_UNKNOWN; 1035 1036 /* 1037 * initialize kstats 1038 */ 1039 un->un_stats = kstat_create("st", instance, NULL, "tape", 1040 KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT); 1041 if (un->un_stats) { 1042 un->un_stats->ks_lock = ST_MUTEX; 1043 kstat_install(un->un_stats); 1044 } 1045 (void) st_create_errstats(un, instance); 1046 1047 return (DDI_SUCCESS); 1048 } 1049 1050 /* 1051 * st_detach: 1052 * 1053 * we allow a detach if and only if: 1054 * - no tape is currently inserted 1055 * - tape position is at BOT or unknown 1056 * (if it is not at BOT then a no rewind 1057 * device was opened and we have to preserve state) 1058 * - it must be in a closed state : no timeouts or scsi_watch requests 1059 * will exist if it is closed, so we don't need to check for 1060 * them here. 1061 */ 1062 /*ARGSUSED*/ 1063 static int 1064 st_detach(dev_info_t *devi, ddi_detach_cmd_t cmd) 1065 { 1066 int instance; 1067 int dev_instance; 1068 struct scsi_device *devp; 1069 struct scsi_tape *un; 1070 clock_t wait_cmds_complete; 1071 1072 ST_ENTR(devi, st_detach); 1073 1074 instance = ddi_get_instance(devi); 1075 1076 if (!(un = ddi_get_soft_state(st_state, instance))) { 1077 return (DDI_FAILURE); 1078 } 1079 1080 switch (cmd) { 1081 1082 case DDI_DETACH: 1083 /* 1084 * Undo what we did in st_attach & st_doattach, 1085 * freeing resources and removing things we installed. 1086 * The system framework guarantees we are not active 1087 * with this devinfo node in any other entry points at 1088 * this time. 1089 */ 1090 1091 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 1092 "st_detach: instance=%x, un=%p\n", instance, 1093 (void *)un); 1094 1095 if (((un->un_dp->options & ST_UNLOADABLE) == 0) || 1096 (un->un_ncmds != 0) || (un->un_quef != NULL) || 1097 (un->un_state != ST_STATE_CLOSED)) { 1098 /* 1099 * we cannot unload some targets because the 1100 * inquiry returns junk unless immediately 1101 * after a reset 1102 */ 1103 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 1104 "cannot unload instance %x\n", instance); 1105 return (DDI_FAILURE); 1106 } 1107 1108 /* 1109 * if the tape has been removed then we may unload; 1110 * do a test unit ready and if it returns NOT READY 1111 * then we assume that it is safe to unload. 1112 * as a side effect, pmode may be set to invalid if the 1113 * the test unit ready fails; 1114 * also un_state may be set to non-closed, so reset it 1115 */ 1116 if ((un->un_dev) && /* Been opened since attach */ 1117 ((un->un_pos.pmode == legacy) && 1118 (un->un_pos.fileno > 0) || /* Known position not rewound */ 1119 (un->un_pos.blkno != 0)) || /* Or within first file */ 1120 ((un->un_pos.pmode == logical) && 1121 (un->un_pos.lgclblkno > 0))) { 1122 mutex_enter(ST_MUTEX); 1123 /* 1124 * Send Test Unit Ready in the hopes that if 1125 * the drive is not in the state we think it is. 1126 * And the state will be changed so it can be detached. 1127 * If the command fails to reach the device and 1128 * the drive was not rewound or unloaded we want 1129 * to fail the detach till a user command fails 1130 * where after the detach will succead. 1131 */ 1132 (void) st_cmd(un->un_dev, SCMD_TEST_UNIT_READY, 1133 0, SYNC_CMD); 1134 /* 1135 * After TUR un_state may be set to non-closed, 1136 * so reset it back. 1137 */ 1138 un->un_state = ST_STATE_CLOSED; 1139 mutex_exit(ST_MUTEX); 1140 } 1141 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 1142 "un_status=%x, fileno=%x, blkno=%x\n", 1143 un->un_status, un->un_pos.fileno, un->un_pos.blkno); 1144 1145 /* 1146 * check again: 1147 * if we are not at BOT then it is not safe to unload 1148 */ 1149 if ((un->un_dev) && /* Been opened since attach */ 1150 (((un->un_pos.pmode == legacy) && 1151 (un->un_pos.fileno > 0) || /* Known position not rewound */ 1152 (un->un_pos.blkno != 0)) || /* Or within first file */ 1153 ((un->un_pos.pmode == logical) && 1154 (un->un_pos.lgclblkno > 0)))) { 1155 1156 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 1157 "cannot detach: pmode=%d fileno=%x, blkno=%x" 1158 " lgclblkno=0x%"PRIx64"\n", un->un_pos.pmode, 1159 un->un_pos.fileno, un->un_pos.blkno, 1160 un->un_pos.lgclblkno); 1161 return (DDI_FAILURE); 1162 } 1163 1164 /* 1165 * Just To make sure that we have released the 1166 * tape unit . 1167 */ 1168 if (un->un_dev && (un->un_rsvd_status & ST_RESERVE) && 1169 !DEVI_IS_DEVICE_REMOVED(devi)) { 1170 mutex_enter(ST_MUTEX); 1171 (void) st_reserve_release(un, ST_RELEASE); 1172 mutex_exit(ST_MUTEX); 1173 } 1174 1175 /* 1176 * now remove other data structures allocated in st_doattach() 1177 */ 1178 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 1179 "destroying/freeing\n"); 1180 cv_destroy(&un->un_clscv); 1181 cv_destroy(&un->un_sbuf_cv); 1182 cv_destroy(&un->un_queue_cv); 1183 cv_destroy(&un->un_suspend_cv); 1184 cv_destroy(&un->un_tape_busy_cv); 1185 1186 if (un->un_hib_tid) { 1187 (void) untimeout(un->un_hib_tid); 1188 un->un_hib_tid = 0; 1189 } 1190 1191 if (un->un_delay_tid) { 1192 (void) untimeout(un->un_delay_tid); 1193 un->un_delay_tid = 0; 1194 } 1195 cv_destroy(&un->un_state_cv); 1196 1197 #ifdef __x86 1198 if (un->un_contig_mem_hdl != NULL) { 1199 ddi_dma_free_handle(&un->un_contig_mem_hdl); 1200 } 1201 #endif 1202 if (un->un_sbufp) { 1203 freerbuf(un->un_sbufp); 1204 } 1205 if (un->un_uscsi_rqs_buf) { 1206 kmem_free(un->un_uscsi_rqs_buf, SENSE_LENGTH); 1207 } 1208 if (un->un_mspl) { 1209 i_ddi_mem_free((caddr_t)un->un_mspl, NULL); 1210 } 1211 if (un->un_rqs) { 1212 scsi_destroy_pkt(un->un_rqs); 1213 scsi_free_consistent_buf(un->un_rqs_bp); 1214 } 1215 if (un->un_mkr_pkt) { 1216 scsi_destroy_pkt(un->un_mkr_pkt); 1217 } 1218 if (un->un_arq_enabled) { 1219 (void) scsi_ifsetcap(ROUTE, "auto-rqsense", 0, 1); 1220 } 1221 if (un->un_dp_size) { 1222 kmem_free(un->un_dp, un->un_dp_size); 1223 } 1224 if (un->un_stats) { 1225 kstat_delete(un->un_stats); 1226 un->un_stats = (kstat_t *)0; 1227 } 1228 if (un->un_errstats) { 1229 kstat_delete(un->un_errstats); 1230 un->un_errstats = (kstat_t *)0; 1231 } 1232 devp = ST_SCSI_DEVP; 1233 ddi_soft_state_free(st_state, instance); 1234 devp->sd_private = NULL; 1235 devp->sd_sense = NULL; 1236 scsi_unprobe(devp); 1237 ddi_prop_remove_all(devi); 1238 ddi_remove_minor_node(devi, NULL); 1239 ST_DEBUG(0, st_label, SCSI_DEBUG, "st_detach done\n"); 1240 return (DDI_SUCCESS); 1241 1242 case DDI_SUSPEND: 1243 1244 /* 1245 * Suspend/Resume 1246 * 1247 * To process DDI_SUSPEND, we must do the following: 1248 * 1249 * - check ddi_removing_power to see if power will be turned 1250 * off. if so, return DDI_FAILURE 1251 * - check if we are already suspended, 1252 * if so, return DDI_FAILURE 1253 * - check if device state is CLOSED, 1254 * if not, return DDI_FAILURE. 1255 * - wait until outstanding operations complete 1256 * - save tape state 1257 * - block new operations 1258 * - cancel pending timeouts 1259 * 1260 */ 1261 1262 if (ddi_removing_power(devi)) { 1263 return (DDI_FAILURE); 1264 } 1265 mutex_enter(ST_MUTEX); 1266 1267 /* 1268 * Shouldn't already be suspended, if so return failure 1269 */ 1270 if (un->un_pwr_mgmt == ST_PWR_SUSPENDED) { 1271 mutex_exit(ST_MUTEX); 1272 return (DDI_FAILURE); 1273 } 1274 if (un->un_state != ST_STATE_CLOSED) { 1275 mutex_exit(ST_MUTEX); 1276 return (DDI_FAILURE); 1277 } 1278 1279 /* 1280 * Wait for all outstanding I/O's to complete 1281 * 1282 * we wait on both ncmds and the wait queue for times 1283 * when we are flushing after persistent errors are 1284 * flagged, which is when ncmds can be 0, and the 1285 * queue can still have I/O's. This way we preserve 1286 * order of biodone's. 1287 */ 1288 wait_cmds_complete = ddi_get_lbolt(); 1289 wait_cmds_complete += 1290 st_wait_cmds_complete * drv_usectohz(1000000); 1291 while (un->un_ncmds || un->un_quef || 1292 (un->un_state == ST_STATE_RESOURCE_WAIT)) { 1293 1294 if (cv_timedwait(&un->un_tape_busy_cv, ST_MUTEX, 1295 wait_cmds_complete) == -1) { 1296 /* 1297 * Time expired then cancel the command 1298 */ 1299 mutex_exit(ST_MUTEX); 1300 if (scsi_reset(ROUTE, RESET_TARGET) == 0) { 1301 mutex_enter(ST_MUTEX); 1302 if (un->un_last_throttle) { 1303 un->un_throttle = 1304 un->un_last_throttle; 1305 } 1306 mutex_exit(ST_MUTEX); 1307 return (DDI_FAILURE); 1308 } else { 1309 mutex_enter(ST_MUTEX); 1310 break; 1311 } 1312 } 1313 } 1314 1315 /* 1316 * DDI_SUSPEND says that the system "may" power down, we 1317 * remember the file and block number before rewinding. 1318 * we also need to save state before issuing 1319 * any WRITE_FILE_MARK command. 1320 */ 1321 (void) st_update_block_pos(un); 1322 COPY_POS(&un->un_suspend_pos, &un->un_pos); 1323 1324 dev_instance = ((un->un_dev == 0) ? MTMINOR(instance) : 1325 un->un_dev); 1326 1327 /* 1328 * Issue a zero write file fmk command to tell the drive to 1329 * flush any buffered tape marks 1330 */ 1331 (void) st_cmd(dev_instance, SCMD_WRITE_FILE_MARK, 0, SYNC_CMD); 1332 1333 /* 1334 * Because not all tape drives correctly implement buffer 1335 * flushing with the zero write file fmk command, issue a 1336 * synchronous rewind command to force data flushing. 1337 * st_validate_tapemarks() will do a rewind during DDI_RESUME 1338 * anyway. 1339 */ 1340 (void) st_cmd(dev_instance, SCMD_REWIND, 0, SYNC_CMD); 1341 1342 /* stop any new operations */ 1343 un->un_pwr_mgmt = ST_PWR_SUSPENDED; 1344 un->un_throttle = 0; 1345 1346 /* 1347 * cancel any outstanding timeouts 1348 */ 1349 if (un->un_delay_tid) { 1350 timeout_id_t temp_id = un->un_delay_tid; 1351 un->un_delay_tid = 0; 1352 un->un_tids_at_suspend |= ST_DELAY_TID; 1353 mutex_exit(ST_MUTEX); 1354 (void) untimeout(temp_id); 1355 mutex_enter(ST_MUTEX); 1356 } 1357 1358 if (un->un_hib_tid) { 1359 timeout_id_t temp_id = un->un_hib_tid; 1360 un->un_hib_tid = 0; 1361 un->un_tids_at_suspend |= ST_HIB_TID; 1362 mutex_exit(ST_MUTEX); 1363 (void) untimeout(temp_id); 1364 mutex_enter(ST_MUTEX); 1365 } 1366 1367 /* 1368 * Suspend the scsi_watch_thread 1369 */ 1370 if (un->un_swr_token) { 1371 opaque_t temp_token = un->un_swr_token; 1372 mutex_exit(ST_MUTEX); 1373 scsi_watch_suspend(temp_token); 1374 } else { 1375 mutex_exit(ST_MUTEX); 1376 } 1377 1378 return (DDI_SUCCESS); 1379 1380 default: 1381 ST_DEBUG(0, st_label, SCSI_DEBUG, "st_detach failed\n"); 1382 return (DDI_FAILURE); 1383 } 1384 } 1385 1386 1387 /* ARGSUSED */ 1388 static int 1389 stinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 1390 { 1391 dev_t dev; 1392 struct scsi_tape *un; 1393 int instance, error; 1394 1395 ST_ENTR(dip, stinfo); 1396 1397 switch (infocmd) { 1398 case DDI_INFO_DEVT2DEVINFO: 1399 dev = (dev_t)arg; 1400 instance = MTUNIT(dev); 1401 if ((un = ddi_get_soft_state(st_state, instance)) == NULL) 1402 return (DDI_FAILURE); 1403 *result = (void *) ST_DEVINFO; 1404 error = DDI_SUCCESS; 1405 break; 1406 case DDI_INFO_DEVT2INSTANCE: 1407 dev = (dev_t)arg; 1408 instance = MTUNIT(dev); 1409 *result = (void *)(uintptr_t)instance; 1410 error = DDI_SUCCESS; 1411 break; 1412 default: 1413 error = DDI_FAILURE; 1414 } 1415 return (error); 1416 } 1417 1418 static int 1419 st_doattach(struct scsi_device *devp, int (*canwait)()) 1420 { 1421 struct scsi_pkt *rqpkt = NULL; 1422 struct scsi_tape *un = NULL; 1423 int km_flags = (canwait != NULL_FUNC) ? KM_SLEEP : KM_NOSLEEP; 1424 int instance; 1425 struct buf *bp; 1426 size_t rlen; 1427 1428 ST_FUNC(devp->sd_dev, st_doattach); 1429 /* 1430 * Call the routine scsi_probe to do some of the dirty work. 1431 * If the INQUIRY command succeeds, the field sd_inq in the 1432 * device structure will be filled in. 1433 */ 1434 ST_DEBUG(devp->sd_dev, st_label, SCSI_DEBUG, 1435 "st_doattach(): probing\n"); 1436 1437 if (scsi_probe(devp, canwait) == SCSIPROBE_EXISTS) { 1438 1439 /* 1440 * In checking the whole inq_dtype byte we are looking at both 1441 * the Peripheral Qualifier and the Peripheral Device Type. 1442 * For this driver we are only interested in sequential devices 1443 * that are connected or capable if connecting to this logical 1444 * unit. 1445 */ 1446 if (devp->sd_inq->inq_dtype == 1447 (DTYPE_SEQUENTIAL | DPQ_POSSIBLE)) { 1448 ST_DEBUG(devp->sd_dev, st_label, SCSI_DEBUG, 1449 "probe exists\n"); 1450 } else { 1451 /* Something there but not a tape device */ 1452 scsi_unprobe(devp); 1453 return (DDI_FAILURE); 1454 } 1455 } else { 1456 /* Nothing there */ 1457 ST_DEBUG(devp->sd_dev, st_label, SCSI_DEBUG, 1458 "probe failure: nothing there\n"); 1459 scsi_unprobe(devp); 1460 return (DDI_FAILURE); 1461 } 1462 1463 bp = scsi_alloc_consistent_buf(&devp->sd_address, (struct buf *)NULL, 1464 SENSE_LENGTH, B_READ, canwait, NULL); 1465 if (!bp) { 1466 goto error; 1467 } 1468 rqpkt = scsi_init_pkt(&devp->sd_address, NULL, bp, CDB_GROUP0, 1, 0, 1469 PKT_CONSISTENT, canwait, NULL); 1470 if (!rqpkt) { 1471 goto error; 1472 } 1473 devp->sd_sense = (struct scsi_extended_sense *)bp->b_un.b_addr; 1474 ASSERT(geterror(bp) == NULL); 1475 1476 (void) scsi_setup_cdb((union scsi_cdb *)rqpkt->pkt_cdbp, 1477 SCMD_REQUEST_SENSE, 0, SENSE_LENGTH, 0); 1478 FILL_SCSI1_LUN(devp, rqpkt); 1479 1480 /* 1481 * The actual unit is present. 1482 * Now is the time to fill in the rest of our info.. 1483 */ 1484 instance = ddi_get_instance(devp->sd_dev); 1485 1486 if (ddi_soft_state_zalloc(st_state, instance) != DDI_SUCCESS) { 1487 goto error; 1488 } 1489 un = ddi_get_soft_state(st_state, instance); 1490 1491 ASSERT(un != NULL); 1492 1493 un->un_sbufp = getrbuf(km_flags); 1494 1495 un->un_uscsi_rqs_buf = kmem_alloc(SENSE_LENGTH, KM_SLEEP); 1496 1497 /* 1498 * use i_ddi_mem_alloc() for now until we have an interface to allocate 1499 * memory for DMA which doesn't require a DMA handle. ddi_iopb_alloc() 1500 * is obsolete and we want more flexibility in controlling the DMA 1501 * address constraints. 1502 */ 1503 (void) i_ddi_mem_alloc(devp->sd_dev, &st_alloc_attr, 1504 sizeof (struct seq_mode), ((km_flags == KM_SLEEP) ? 1 : 0), 0, 1505 NULL, (caddr_t *)&un->un_mspl, &rlen, NULL); 1506 1507 (void) i_ddi_mem_alloc(devp->sd_dev, &st_alloc_attr, 1508 sizeof (read_pos_data_t), ((km_flags == KM_SLEEP) ? 1 : 0), 0, 1509 NULL, (caddr_t *)&un->un_read_pos_data, &rlen, NULL); 1510 1511 if (!un->un_sbufp || !un->un_mspl || !un->un_read_pos_data) { 1512 ST_DEBUG6(devp->sd_dev, st_label, SCSI_DEBUG, 1513 "probe partial failure: no space\n"); 1514 goto error; 1515 } 1516 1517 bzero(un->un_mspl, sizeof (struct seq_mode)); 1518 1519 cv_init(&un->un_sbuf_cv, NULL, CV_DRIVER, NULL); 1520 cv_init(&un->un_queue_cv, NULL, CV_DRIVER, NULL); 1521 cv_init(&un->un_clscv, NULL, CV_DRIVER, NULL); 1522 cv_init(&un->un_state_cv, NULL, CV_DRIVER, NULL); 1523 #ifdef __x86 1524 cv_init(&un->un_contig_mem_cv, NULL, CV_DRIVER, NULL); 1525 #endif 1526 1527 /* Initialize power managemnet condition variable */ 1528 cv_init(&un->un_suspend_cv, NULL, CV_DRIVER, NULL); 1529 cv_init(&un->un_tape_busy_cv, NULL, CV_DRIVER, NULL); 1530 1531 rqpkt->pkt_flags |= (FLAG_SENSING | FLAG_HEAD | FLAG_NODISCON); 1532 1533 un->un_pos.pmode = invalid; 1534 rqpkt->pkt_time = st_io_time; 1535 rqpkt->pkt_comp = st_intr; 1536 un->un_rqs = rqpkt; 1537 un->un_sd = devp; 1538 un->un_rqs_bp = bp; 1539 un->un_swr_token = (opaque_t)NULL; 1540 un->un_comp_page = ST_DEV_DATACOMP_PAGE | ST_DEV_CONFIG_PAGE; 1541 un->un_wormable = st_is_drive_worm; 1542 un->un_read_pos_type = LONG_POS; 1543 1544 un->un_suspend_pos.pmode = invalid; 1545 1546 #ifdef __x86 1547 if (ddi_dma_alloc_handle(ST_DEVINFO, &st_contig_mem_dma_attr, 1548 DDI_DMA_SLEEP, NULL, &un->un_contig_mem_hdl) != DDI_SUCCESS) { 1549 ST_DEBUG6(devp->sd_dev, st_label, SCSI_DEBUG, 1550 "allocation of contiguous memory dma handle failed!"); 1551 un->un_contig_mem_hdl = NULL; 1552 goto error; 1553 } 1554 #endif 1555 1556 /* 1557 * Since this driver manages devices with "remote" hardware, 1558 * i.e. the devices themselves have no "reg" properties, 1559 * the SUSPEND/RESUME commands in detach/attach will not be 1560 * called by the power management framework unless we request 1561 * it by creating a "pm-hardware-state" property and setting it 1562 * to value "needs-suspend-resume". 1563 */ 1564 if (ddi_prop_update_string(DDI_DEV_T_NONE, devp->sd_dev, 1565 "pm-hardware-state", "needs-suspend-resume") != 1566 DDI_PROP_SUCCESS) { 1567 1568 ST_DEBUG(devp->sd_dev, st_label, SCSI_DEBUG, 1569 "ddi_prop_update(\"pm-hardware-state\") failed\n"); 1570 goto error; 1571 } 1572 1573 if (ddi_prop_create(DDI_DEV_T_NONE, devp->sd_dev, DDI_PROP_CANSLEEP, 1574 "no-involuntary-power-cycles", NULL, 0) != DDI_PROP_SUCCESS) { 1575 1576 ST_DEBUG(devp->sd_dev, st_label, SCSI_DEBUG, 1577 "ddi_prop_create(\"no-involuntary-power-cycles\") " 1578 "failed\n"); 1579 goto error; 1580 } 1581 1582 ST_DEBUG6(devp->sd_dev, st_label, SCSI_DEBUG, "probe success\n"); 1583 return (DDI_SUCCESS); 1584 1585 error: 1586 devp->sd_sense = NULL; 1587 1588 ddi_remove_minor_node(devp->sd_dev, NULL); 1589 if (un) { 1590 if (un->un_mspl) { 1591 i_ddi_mem_free((caddr_t)un->un_mspl, NULL); 1592 } 1593 if (un->un_read_pos_data) { 1594 i_ddi_mem_free((caddr_t)un->un_read_pos_data, 0); 1595 } 1596 if (un->un_sbufp) { 1597 freerbuf(un->un_sbufp); 1598 } 1599 if (un->un_uscsi_rqs_buf) { 1600 kmem_free(un->un_uscsi_rqs_buf, SENSE_LENGTH); 1601 } 1602 #ifdef __x86 1603 if (un->un_contig_mem_hdl != NULL) { 1604 ddi_dma_free_handle(&un->un_contig_mem_hdl); 1605 } 1606 #endif 1607 ddi_soft_state_free(st_state, instance); 1608 devp->sd_private = NULL; 1609 } 1610 1611 if (rqpkt) { 1612 scsi_destroy_pkt(rqpkt); 1613 } 1614 1615 if (bp) { 1616 scsi_free_consistent_buf(bp); 1617 } 1618 1619 if (devp->sd_inq) { 1620 scsi_unprobe(devp); 1621 } 1622 return (DDI_FAILURE); 1623 } 1624 1625 typedef int 1626 (*cfg_functp)(struct scsi_tape *, char *vidpid, struct st_drivetype *); 1627 1628 static cfg_functp config_functs[] = { 1629 st_get_conf_from_st_dot_conf, 1630 st_get_conf_from_st_conf_dot_c, 1631 st_get_default_conf 1632 }; 1633 1634 1635 /* 1636 * determine tape type, using tape-config-list or built-in table or 1637 * use a generic tape config entry 1638 */ 1639 static void 1640 st_known_tape_type(struct scsi_tape *un) 1641 { 1642 struct st_drivetype *dp; 1643 cfg_functp *config_funct; 1644 1645 ST_FUNC(ST_DEVINFO, st_known_tape_type); 1646 /* 1647 * XXX: Emulex MT-02 (and emulators) predates SCSI-1 and has 1648 * no vid & pid inquiry data. So, we provide one. 1649 */ 1650 if (ST_INQUIRY->inq_len == 0 || 1651 (bcmp("\0\0\0\0\0\0\0\0", ST_INQUIRY->inq_vid, 8) == 0)) { 1652 (void) strcpy((char *)ST_INQUIRY->inq_vid, ST_MT02_NAME); 1653 } 1654 1655 un->un_dp_size = sizeof (struct st_drivetype); 1656 dp = kmem_zalloc((size_t)un->un_dp_size, KM_SLEEP); 1657 un->un_dp = dp; 1658 1659 /* 1660 * Loop through the configuration methods till one works. 1661 */ 1662 for (config_funct = &config_functs[0]; ; config_funct++) { 1663 if ((*config_funct)(un, ST_INQUIRY->inq_vid, dp)) { 1664 break; 1665 } 1666 } 1667 1668 /* 1669 * If we didn't just make up this configuration and 1670 * all the density codes are the same.. 1671 * Set Auto Density over ride. 1672 */ 1673 if (*config_funct != st_get_default_conf) { 1674 /* 1675 * If this device is one that is configured and all 1676 * densities are the same, This saves doing gets and set 1677 * that yield nothing. 1678 */ 1679 if ((dp->densities[0]) == (dp->densities[1]) && 1680 (dp->densities[0]) == (dp->densities[2]) && 1681 (dp->densities[0]) == (dp->densities[3])) { 1682 1683 dp->options |= ST_AUTODEN_OVERRIDE; 1684 } 1685 } 1686 1687 1688 /* 1689 * Store tape drive characteristics. 1690 */ 1691 un->un_status = 0; 1692 un->un_attached = 1; 1693 un->un_init_options = dp->options; 1694 1695 /* setup operation time-outs based on options */ 1696 st_calculate_timeouts(un); 1697 1698 /* make sure if we are supposed to be variable, make it variable */ 1699 if (dp->options & ST_VARIABLE) { 1700 dp->bsize = 0; 1701 } 1702 1703 scsi_log(ST_DEVINFO, st_label, CE_NOTE, "?<%s>\n", dp->name); 1704 } 1705 1706 1707 typedef struct { 1708 int mask; 1709 int bottom; 1710 int top; 1711 char *name; 1712 } conf_limit; 1713 1714 static const conf_limit conf_limits[] = { 1715 1716 -1, 1, 2, "conf version", 1717 -1, MT_ISTS, ST_LAST_TYPE, "drive type", 1718 -1, 0, 0xffffff, "block size", 1719 ST_VALID_OPTS, 0, ST_VALID_OPTS, "options", 1720 -1, 0, 4, "number of densities", 1721 -1, 0, UINT8_MAX, "density code", 1722 -1, 0, 3, "default density", 1723 -1, 0, UINT16_MAX, "non motion timeout", 1724 -1, 0, UINT16_MAX, "I/O timeout", 1725 -1, 0, UINT16_MAX, "space timeout", 1726 -1, 0, UINT16_MAX, "load timeout", 1727 -1, 0, UINT16_MAX, "unload timeout", 1728 -1, 0, UINT16_MAX, "erase timeout", 1729 0, 0, 0, NULL 1730 }; 1731 1732 static int 1733 st_validate_conf_data(struct scsi_tape *un, int *list, int list_len, 1734 const char *conf_name) 1735 { 1736 int dens; 1737 int ndens; 1738 int value; 1739 int type; 1740 int count; 1741 const conf_limit *limit = &conf_limits[0]; 1742 1743 ST_FUNC(ST_DEVINFO, st_validate_conf_data); 1744 1745 ST_DEBUG3(ST_DEVINFO, st_label, CE_NOTE, 1746 "Checking %d entrys total with %d densities\n", list_len, list[4]); 1747 1748 count = list_len; 1749 type = *list; 1750 for (; count && limit->name; count--, list++, limit++) { 1751 1752 value = *list; 1753 if (value & ~limit->mask) { 1754 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 1755 "%s %s value invalid bits set: 0x%X\n", 1756 conf_name, limit->name, value & ~limit->mask); 1757 *list &= limit->mask; 1758 } else if (value < limit->bottom) { 1759 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 1760 "%s %s value too low: value = %d limit %d\n", 1761 conf_name, limit->name, value, limit->bottom); 1762 } else if (value > limit->top) { 1763 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 1764 "%s %s value too high: value = %d limit %d\n", 1765 conf_name, limit->name, value, limit->top); 1766 } else { 1767 ST_DEBUG3(ST_DEVINFO, st_label, CE_CONT, 1768 "%s %s value = 0x%X\n", 1769 conf_name, limit->name, value); 1770 } 1771 1772 /* If not the number of densities continue */ 1773 if (limit != &conf_limits[4]) { 1774 continue; 1775 } 1776 1777 /* If number of densities is not in range can't use config */ 1778 if (value < limit->bottom || value > limit->top) { 1779 return (-1); 1780 } 1781 1782 ndens = min(value, NDENSITIES); 1783 if ((type == 1) && (list_len - ndens) != 6) { 1784 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 1785 "%s conf version 1 with %d densities has %d items" 1786 " should have %d", 1787 conf_name, ndens, list_len, 6 + ndens); 1788 } else if ((type == 2) && (list_len - ndens) != 13) { 1789 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 1790 "%s conf version 2 with %d densities has %d items" 1791 " should have %d", 1792 conf_name, ndens, list_len, 13 + ndens); 1793 } 1794 1795 limit++; 1796 for (dens = 0; dens < ndens && count; dens++) { 1797 count--; 1798 list++; 1799 value = *list; 1800 if (value < limit->bottom) { 1801 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 1802 "%s density[%d] value too low: value =" 1803 " 0x%X limit 0x%X\n", 1804 conf_name, dens, value, limit->bottom); 1805 } else if (value > limit->top) { 1806 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 1807 "%s density[%d] value too high: value =" 1808 " 0x%X limit 0x%X\n", 1809 conf_name, dens, value, limit->top); 1810 } else { 1811 ST_DEBUG3(ST_DEVINFO, st_label, CE_CONT, 1812 "%s density[%d] value = 0x%X\n", 1813 conf_name, dens, value); 1814 } 1815 } 1816 } 1817 1818 return (0); 1819 } 1820 1821 static int 1822 st_get_conf_from_st_dot_conf(struct scsi_tape *un, char *vidpid, 1823 struct st_drivetype *dp) 1824 { 1825 caddr_t config_list = NULL; 1826 caddr_t data_list = NULL; 1827 int *data_ptr; 1828 caddr_t vidptr, prettyptr, datanameptr; 1829 size_t vidlen, prettylen, datanamelen, tripletlen = 0; 1830 int config_list_len, data_list_len, len, i; 1831 int version; 1832 int found = 0; 1833 1834 ST_FUNC(ST_DEVINFO, st_get_conf_from_st_dot_conf); 1835 1836 /* 1837 * Determine type of tape controller. Type is determined by 1838 * checking the vendor ids of the earlier inquiry command and 1839 * comparing those with vids in tape-config-list defined in st.conf 1840 */ 1841 if (ddi_getlongprop(DDI_DEV_T_ANY, ST_DEVINFO, DDI_PROP_DONTPASS, 1842 "tape-config-list", (caddr_t)&config_list, &config_list_len) 1843 != DDI_PROP_SUCCESS) { 1844 return (found); 1845 } 1846 1847 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 1848 "st_get_conf_from_st_dot_conf(): st.conf has tape-config-list\n"); 1849 1850 /* 1851 * Compare vids in each triplet - if it matches, get value for 1852 * data_name and contruct a st_drivetype struct 1853 * tripletlen is not set yet! 1854 */ 1855 for (len = config_list_len, vidptr = config_list; 1856 len > 0; 1857 vidptr += tripletlen, len -= tripletlen) { 1858 1859 vidlen = strlen(vidptr); 1860 prettyptr = vidptr + vidlen + 1; 1861 prettylen = strlen(prettyptr); 1862 datanameptr = prettyptr + prettylen + 1; 1863 datanamelen = strlen(datanameptr); 1864 tripletlen = vidlen + prettylen + datanamelen + 3; 1865 1866 if (vidlen == 0) { 1867 continue; 1868 } 1869 1870 /* 1871 * If inquiry vid dosen't match this triplets vid, 1872 * try the next. 1873 */ 1874 if (strncasecmp(vidpid, vidptr, vidlen)) { 1875 continue; 1876 } 1877 1878 /* 1879 * if prettylen is zero then use the vid string 1880 */ 1881 if (prettylen == 0) { 1882 prettyptr = vidptr; 1883 prettylen = vidlen; 1884 } 1885 1886 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 1887 "vid = %s, pretty=%s, dataname = %s\n", 1888 vidptr, prettyptr, datanameptr); 1889 1890 /* 1891 * get the data list 1892 */ 1893 if (ddi_getlongprop(DDI_DEV_T_ANY, ST_DEVINFO, 0, 1894 datanameptr, (caddr_t)&data_list, 1895 &data_list_len) != DDI_PROP_SUCCESS) { 1896 /* 1897 * Error in getting property value 1898 * print warning! 1899 */ 1900 scsi_log(ST_DEVINFO, st_label, CE_WARN, 1901 "data property (%s) has no value\n", 1902 datanameptr); 1903 continue; 1904 } 1905 1906 /* 1907 * now initialize the st_drivetype struct 1908 */ 1909 (void) strncpy(dp->name, prettyptr, ST_NAMESIZE - 1); 1910 dp->length = (int)min(vidlen, (VIDPIDLEN - 1)); 1911 (void) strncpy(dp->vid, vidptr, dp->length); 1912 data_ptr = (int *)data_list; 1913 /* 1914 * check if data is enough for version, type, 1915 * bsize, options, # of densities, density1, 1916 * density2, ..., default_density 1917 */ 1918 if ((data_list_len < 5 * sizeof (int)) || 1919 (data_list_len < 6 * sizeof (int) + 1920 *(data_ptr + 4) * sizeof (int))) { 1921 /* 1922 * print warning and skip to next triplet. 1923 */ 1924 scsi_log(ST_DEVINFO, st_label, CE_WARN, 1925 "data property (%s) incomplete\n", 1926 datanameptr); 1927 kmem_free(data_list, data_list_len); 1928 continue; 1929 } 1930 1931 if (st_validate_conf_data(un, data_ptr, 1932 data_list_len / sizeof (int), datanameptr)) { 1933 kmem_free(data_list, data_list_len); 1934 scsi_log(ST_DEVINFO, st_label, CE_WARN, 1935 "data property (%s) rejected\n", 1936 datanameptr); 1937 continue; 1938 } 1939 1940 /* 1941 * check version 1942 */ 1943 version = *data_ptr++; 1944 if (version != 1 && version != 2) { 1945 /* print warning but accept it */ 1946 scsi_log(ST_DEVINFO, st_label, CE_WARN, 1947 "Version # for data property (%s) " 1948 "not set to 1 or 2\n", datanameptr); 1949 } 1950 1951 dp->type = *data_ptr++; 1952 dp->bsize = *data_ptr++; 1953 dp->options = *data_ptr++; 1954 dp->options |= ST_DYNAMIC; 1955 len = *data_ptr++; 1956 for (i = 0; i < NDENSITIES; i++) { 1957 if (i < len) { 1958 dp->densities[i] = *data_ptr++; 1959 } 1960 } 1961 dp->default_density = *data_ptr << 3; 1962 if (version == 2 && 1963 data_list_len >= (13 + len) * sizeof (int)) { 1964 data_ptr++; 1965 dp->non_motion_timeout = *data_ptr++; 1966 dp->io_timeout = *data_ptr++; 1967 dp->rewind_timeout = *data_ptr++; 1968 dp->space_timeout = *data_ptr++; 1969 dp->load_timeout = *data_ptr++; 1970 dp->unload_timeout = *data_ptr++; 1971 dp->erase_timeout = *data_ptr++; 1972 } 1973 kmem_free(data_list, data_list_len); 1974 found = 1; 1975 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 1976 "found in st.conf: vid = %s, pretty=%s\n", 1977 dp->vid, dp->name); 1978 break; 1979 } 1980 1981 /* 1982 * free up the memory allocated by ddi_getlongprop 1983 */ 1984 if (config_list) { 1985 kmem_free(config_list, config_list_len); 1986 } 1987 return (found); 1988 } 1989 1990 static int 1991 st_get_conf_from_st_conf_dot_c(struct scsi_tape *un, char *vidpid, 1992 struct st_drivetype *dp) 1993 { 1994 int i; 1995 1996 ST_FUNC(ST_DEVINFO, st_get_conf_from_st_conf_dot_c); 1997 /* 1998 * Determine type of tape controller. Type is determined by 1999 * checking the result of the earlier inquiry command and 2000 * comparing vendor ids with strings in a table declared in st_conf.c. 2001 */ 2002 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2003 "st_get_conf_from_st_conf_dot_c(): looking at st_drivetypes\n"); 2004 2005 for (i = 0; i < st_ndrivetypes; i++) { 2006 if (st_drivetypes[i].length == 0) { 2007 continue; 2008 } 2009 if (strncasecmp(vidpid, st_drivetypes[i].vid, 2010 st_drivetypes[i].length)) { 2011 continue; 2012 } 2013 bcopy(&st_drivetypes[i], dp, sizeof (st_drivetypes[i])); 2014 return (1); 2015 } 2016 return (0); 2017 } 2018 2019 static int 2020 st_get_default_conf(struct scsi_tape *un, char *vidpid, struct st_drivetype *dp) 2021 { 2022 int i; 2023 2024 ST_FUNC(ST_DEVINFO, st_get_default_conf); 2025 2026 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 2027 "st_get_default_conf(): making drivetype from INQ cmd\n"); 2028 2029 2030 /* 2031 * Make up a name 2032 */ 2033 bcopy("Vendor '", dp->name, 8); 2034 bcopy(vidpid, &dp->name[8], VIDLEN); 2035 bcopy("' Product '", &dp->name[16], 11); 2036 bcopy(&vidpid[8], &dp->name[27], PIDLEN); 2037 dp->name[ST_NAMESIZE - 2] = '\''; 2038 dp->name[ST_NAMESIZE - 1] = '\0'; 2039 dp->length = min(strlen(ST_INQUIRY->inq_vid), (VIDPIDLEN - 1)); 2040 (void) strncpy(dp->vid, ST_INQUIRY->inq_vid, dp->length); 2041 /* 2042 * 'clean' vendor and product strings of non-printing chars 2043 */ 2044 for (i = 0; i < ST_NAMESIZE - 2; i++) { 2045 if (dp->name[i] < ' ' || dp->name[i] > '~') { 2046 dp->name[i] = '.'; 2047 } 2048 } 2049 dp->type = ST_TYPE_INVALID; 2050 dp->options |= (ST_DYNAMIC | ST_UNLOADABLE | ST_MODE_SEL_COMP); 2051 2052 return (1); /* Can Not Fail */ 2053 } 2054 2055 /* 2056 * Regular Unix Entry points 2057 */ 2058 2059 2060 2061 /* ARGSUSED */ 2062 static int 2063 st_open(dev_t *dev_p, int flag, int otyp, cred_t *cred_p) 2064 { 2065 dev_t dev = *dev_p; 2066 int rval = 0; 2067 2068 GET_SOFT_STATE(dev); 2069 2070 ST_ENTR(ST_DEVINFO, st_open); 2071 2072 /* 2073 * validate that we are addressing a sensible unit 2074 */ 2075 mutex_enter(ST_MUTEX); 2076 2077 #ifdef STDEBUG 2078 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 2079 "st_open(node = %s dev = 0x%lx, flag = %d, otyp = %d)\n", 2080 st_dev_name(dev), *dev_p, flag, otyp); 2081 #endif 2082 2083 /* 2084 * All device accesss go thru st_strategy() where we check 2085 * suspend status 2086 */ 2087 2088 if (!un->un_attached) { 2089 st_known_tape_type(un); 2090 if (!un->un_attached) { 2091 rval = ENXIO; 2092 goto exit; 2093 } 2094 2095 } 2096 2097 /* 2098 * Check for the case of the tape in the middle of closing. 2099 * This isn't simply a check of the current state, because 2100 * we could be in state of sensing with the previous state 2101 * that of closing. 2102 * 2103 * And don't allow multiple opens. 2104 */ 2105 if (!(flag & (FNDELAY | FNONBLOCK)) && IS_CLOSING(un)) { 2106 un->un_laststate = un->un_state; 2107 un->un_state = ST_STATE_CLOSE_PENDING_OPEN; 2108 while (IS_CLOSING(un) || 2109 un->un_state == ST_STATE_CLOSE_PENDING_OPEN) { 2110 if (cv_wait_sig(&un->un_clscv, ST_MUTEX) == 0) { 2111 rval = EINTR; 2112 un->un_state = un->un_laststate; 2113 goto exit; 2114 } 2115 } 2116 } else if (un->un_state != ST_STATE_CLOSED) { 2117 rval = EBUSY; 2118 goto busy; 2119 } 2120 2121 /* 2122 * record current dev 2123 */ 2124 un->un_dev = dev; 2125 un->un_oflags = flag; /* save for use in st_tape_init() */ 2126 un->un_errno = 0; /* no errors yet */ 2127 un->un_restore_pos = 0; 2128 un->un_rqs_state = 0; 2129 2130 /* 2131 * If we are opening O_NDELAY, or O_NONBLOCK, we don't check for 2132 * anything, leave internal states alone, if fileno >= 0 2133 */ 2134 if (flag & (FNDELAY | FNONBLOCK)) { 2135 switch (un->un_pos.pmode) { 2136 2137 case invalid: 2138 un->un_state = ST_STATE_OFFLINE; 2139 break; 2140 2141 case legacy: 2142 /* 2143 * If position is anything other than rewound. 2144 */ 2145 if (un->un_pos.fileno != 0 || un->un_pos.blkno != 0) { 2146 /* 2147 * set un_read_only/write-protect status. 2148 * 2149 * If the tape is not bot we can assume 2150 * that mspl->wp_status is set properly. 2151 * else 2152 * we need to do a mode sense/Tur once 2153 * again to get the actual tape status.(since 2154 * user might have replaced the tape) 2155 * Hence make the st state OFFLINE so that 2156 * we re-intialize the tape once again. 2157 */ 2158 un->un_read_only = 2159 (un->un_oflags & FWRITE) ? RDWR : RDONLY; 2160 un->un_state = ST_STATE_OPEN_PENDING_IO; 2161 } else { 2162 un->un_state = ST_STATE_OFFLINE; 2163 } 2164 break; 2165 case logical: 2166 /* swag not sure how we were open last time */ 2167 (void) st_update_block_pos(un); 2168 if (un->un_pos.lgclblkno == 0) { 2169 un->un_state = ST_STATE_OFFLINE; 2170 } else { 2171 un->un_read_only = 2172 (un->un_oflags & FWRITE) ? 0 : 1; 2173 un->un_state = ST_STATE_OPEN_PENDING_IO; 2174 } 2175 break; 2176 } 2177 rval = 0; 2178 } else { 2179 /* 2180 * Not opening O_NDELAY. 2181 */ 2182 un->un_state = ST_STATE_OPENING; 2183 2184 /* 2185 * Clear error entry stack 2186 */ 2187 st_empty_error_stack(un); 2188 2189 rval = st_tape_init(dev); 2190 if ((rval == EACCES) && (un->un_read_only & WORM)) { 2191 un->un_state = ST_STATE_OPEN_PENDING_IO; 2192 rval = 0; /* so open doesn't fail */ 2193 } else if (rval) { 2194 /* 2195 * Release the tape unit, if reserved and not 2196 * preserve reserve. 2197 */ 2198 if ((un->un_rsvd_status & 2199 (ST_RESERVE | ST_PRESERVE_RESERVE)) == ST_RESERVE) { 2200 (void) st_reserve_release(un, ST_RELEASE); 2201 } 2202 } else { 2203 un->un_state = ST_STATE_OPEN_PENDING_IO; 2204 } 2205 } 2206 2207 exit: 2208 /* 2209 * we don't want any uninvited guests scrogging our data when we're 2210 * busy with something, so for successful opens or failed opens 2211 * (except for EBUSY), reset these counters and state appropriately. 2212 */ 2213 if (rval != EBUSY) { 2214 if (rval) { 2215 un->un_state = ST_STATE_CLOSED; 2216 } 2217 un->un_err_resid = 0; 2218 un->un_retry_ct = 0; 2219 un->un_tran_retry_ct = 0; 2220 } 2221 busy: 2222 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 2223 "st_open: return val = %x, state = %d\n", rval, un->un_state); 2224 mutex_exit(ST_MUTEX); 2225 return (rval); 2226 2227 } 2228 2229 static int 2230 st_tape_init(dev_t dev) 2231 { 2232 int err; 2233 int rval = 0; 2234 2235 GET_SOFT_STATE(dev); 2236 2237 ST_FUNC(ST_DEVINFO, st_tape_init); 2238 2239 ASSERT(mutex_owned(ST_MUTEX)); 2240 2241 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 2242 "st_tape_init(dev = 0x%lx, oflags = %d)\n", dev, un->un_oflags); 2243 2244 /* 2245 * Clean up after any errors left by 'last' close. 2246 * This also handles the case of the initial open. 2247 */ 2248 if (un->un_state != ST_STATE_INITIALIZING) { 2249 un->un_laststate = un->un_state; 2250 un->un_state = ST_STATE_OPENING; 2251 } 2252 2253 un->un_kbytes_xferred = 0; 2254 2255 /* 2256 * do a throw away TUR to clear check condition 2257 */ 2258 err = st_cmd(dev, SCMD_TEST_UNIT_READY, 0, SYNC_CMD); 2259 2260 /* 2261 * If test unit ready fails because the drive is reserved 2262 * by another host fail the open for no access. 2263 */ 2264 if (err) { 2265 if (un->un_rsvd_status & ST_RESERVATION_CONFLICT) { 2266 un->un_state = ST_STATE_CLOSED; 2267 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 2268 "st_tape_init: RESERVATION CONFLICT\n"); 2269 rval = EACCES; 2270 goto exit; 2271 } 2272 } 2273 2274 /* 2275 * See whether this is a generic device that we haven't figured 2276 * anything out about yet. 2277 */ 2278 if (un->un_dp->type == ST_TYPE_INVALID) { 2279 rval = st_determine_generic(dev); 2280 if (rval) { 2281 if (rval != EACCES) { 2282 rval = EIO; 2283 } 2284 un->un_state = ST_STATE_CLOSED; 2285 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2286 "st_tape_init: %s invalid type\n", 2287 rval == EACCES ? "EACCES" : "EIO"); 2288 goto exit; 2289 } 2290 /* 2291 * If this is a Unknown Type drive, 2292 * Use the READ BLOCK LIMITS to determine if 2293 * allow large xfer is approprate if not globally 2294 * disabled with st_allow_large_xfer. 2295 */ 2296 un->un_allow_large_xfer = (uchar_t)st_allow_large_xfer; 2297 } else { 2298 2299 /* 2300 * If we allow_large_xfer (ie >64k) and have not yet found out 2301 * the max block size supported by the drive, 2302 * find it by issueing a READ_BLKLIM command. 2303 * if READ_BLKLIM cmd fails, assume drive doesn't 2304 * allow_large_xfer and min/max block sizes as 1 byte and 63k. 2305 */ 2306 un->un_allow_large_xfer = st_allow_large_xfer && 2307 (un->un_dp->options & ST_NO_RECSIZE_LIMIT); 2308 } 2309 /* 2310 * if maxbsize is unknown, set the maximum block size. 2311 */ 2312 if (un->un_maxbsize == MAXBSIZE_UNKNOWN) { 2313 2314 /* 2315 * Get the Block limits of the tape drive. 2316 * if un->un_allow_large_xfer = 0 , then make sure 2317 * that maxbsize is <= ST_MAXRECSIZE_FIXED. 2318 */ 2319 un->un_rbl = kmem_zalloc(RBLSIZE, KM_SLEEP); 2320 2321 err = st_cmd(dev, SCMD_READ_BLKLIM, RBLSIZE, SYNC_CMD); 2322 if (err) { 2323 /* Retry */ 2324 err = st_cmd(dev, SCMD_READ_BLKLIM, RBLSIZE, SYNC_CMD); 2325 } 2326 if (!err) { 2327 2328 /* 2329 * if cmd successful, use limit returned 2330 */ 2331 un->un_maxbsize = (un->un_rbl->max_hi << 16) + 2332 (un->un_rbl->max_mid << 8) + 2333 un->un_rbl->max_lo; 2334 un->un_minbsize = (un->un_rbl->min_hi << 8) + 2335 un->un_rbl->min_lo; 2336 un->un_data_mod = 1 << un->un_rbl->granularity; 2337 if ((un->un_maxbsize == 0) || 2338 (un->un_allow_large_xfer == 0 && 2339 un->un_maxbsize > ST_MAXRECSIZE_FIXED)) { 2340 un->un_maxbsize = ST_MAXRECSIZE_FIXED; 2341 2342 } else if (un->un_dp->type == ST_TYPE_DEFAULT) { 2343 /* 2344 * Drive is not one that is configured, But the 2345 * READ BLOCK LIMITS tells us it can do large 2346 * xfers. 2347 */ 2348 if (un->un_maxbsize > ST_MAXRECSIZE_FIXED) { 2349 un->un_dp->options |= 2350 ST_NO_RECSIZE_LIMIT; 2351 } 2352 /* 2353 * If max and mimimum block limits are the 2354 * same this is a fixed block size device. 2355 */ 2356 if (un->un_maxbsize == un->un_minbsize) { 2357 un->un_dp->options &= ~ST_VARIABLE; 2358 } 2359 } 2360 2361 if (un->un_minbsize == 0) { 2362 un->un_minbsize = 1; 2363 } 2364 2365 } else { /* error on read block limits */ 2366 2367 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 2368 "!st_tape_init: Error on READ BLOCK LIMITS," 2369 " errno = %d un_rsvd_status = 0x%X\n", 2370 err, un->un_rsvd_status); 2371 2372 /* 2373 * since read block limits cmd failed, 2374 * do not allow large xfers. 2375 * use old values in st_minphys 2376 */ 2377 if (un->un_rsvd_status & ST_RESERVATION_CONFLICT) { 2378 rval = EACCES; 2379 } else { 2380 un->un_allow_large_xfer = 0; 2381 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 2382 "!Disabling large transfers\n"); 2383 2384 /* 2385 * we guess maxbsize and minbsize 2386 */ 2387 if (un->un_bsize) { 2388 un->un_maxbsize = un->un_minbsize = 2389 un->un_bsize; 2390 } else { 2391 un->un_maxbsize = ST_MAXRECSIZE_FIXED; 2392 un->un_minbsize = 1; 2393 } 2394 /* 2395 * Data Mod must be set, 2396 * Even if read block limits fails. 2397 * Prevents Divide By Zero in st_rw(). 2398 */ 2399 un->un_data_mod = 1; 2400 } 2401 } 2402 if (un->un_rbl) { 2403 kmem_free(un->un_rbl, RBLSIZE); 2404 un->un_rbl = NULL; 2405 } 2406 2407 if (rval) { 2408 goto exit; 2409 } 2410 } 2411 2412 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 2413 "maxdma = %d, maxbsize = %d, minbsize = %d, %s large xfer\n", 2414 un->un_maxdma, un->un_maxbsize, un->un_minbsize, 2415 (un->un_allow_large_xfer ? "ALLOW": "DON'T ALLOW")); 2416 2417 err = st_cmd(dev, SCMD_TEST_UNIT_READY, 0, SYNC_CMD); 2418 2419 if (err != 0) { 2420 if (err == EINTR) { 2421 un->un_laststate = un->un_state; 2422 un->un_state = ST_STATE_CLOSED; 2423 rval = EINTR; 2424 goto exit; 2425 } 2426 /* 2427 * Make sure the tape is ready 2428 */ 2429 un->un_pos.pmode = invalid; 2430 if (un->un_status != KEY_UNIT_ATTENTION) { 2431 /* 2432 * allow open no media. Subsequent MTIOCSTATE 2433 * with media present will complete the open 2434 * logic. 2435 */ 2436 un->un_laststate = un->un_state; 2437 if (un->un_oflags & (FNONBLOCK|FNDELAY)) { 2438 un->un_mediastate = MTIO_EJECTED; 2439 un->un_state = ST_STATE_OFFLINE; 2440 rval = 0; 2441 goto exit; 2442 } else { 2443 un->un_state = ST_STATE_CLOSED; 2444 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2445 "st_tape_init EIO no media, not opened " 2446 "O_NONBLOCK|O_EXCL\n"); 2447 rval = EIO; 2448 goto exit; 2449 } 2450 } 2451 } 2452 2453 /* 2454 * On each open, initialize block size from drivetype struct, 2455 * as it could have been changed by MTSRSZ ioctl. 2456 * Now, ST_VARIABLE simply means drive is capable of variable 2457 * mode. All drives are assumed to support fixed records. 2458 * Hence, un_bsize tells what mode the drive is in. 2459 * un_bsize = 0 - variable record length 2460 * = x - fixed record length is x 2461 */ 2462 un->un_bsize = un->un_dp->bsize; 2463 2464 /* 2465 * If saved position is valid go there 2466 */ 2467 if (un->un_restore_pos) { 2468 rval = st_validate_tapemarks(un, &un->un_pos); 2469 if (rval != 0) { 2470 if (rval != EACCES) { 2471 rval = EIO; 2472 } 2473 un->un_restore_pos = 0; 2474 un->un_laststate = un->un_state; 2475 un->un_state = ST_STATE_CLOSED; 2476 goto exit; 2477 } 2478 un->un_pos.fileno = un->un_save_fileno; 2479 un->un_pos.blkno = un->un_save_blkno; 2480 un->un_restore_pos = 0; 2481 } 2482 2483 if (un->un_pos.pmode == invalid) { 2484 rval = st_loadtape(dev); 2485 if (rval) { 2486 if (rval != EACCES) { 2487 rval = EIO; 2488 } 2489 un->un_laststate = un->un_state; 2490 un->un_state = ST_STATE_CLOSED; 2491 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2492 "st_tape_init: %s can't open tape\n", 2493 rval == EACCES ? "EACCES" : "EIO"); 2494 goto exit; 2495 } 2496 } 2497 2498 /* 2499 * do a mode sense to pick up state of current write-protect, 2500 * Could cause reserve and fail due to conflict. 2501 */ 2502 rval = st_modesense(un); 2503 if (rval == EACCES) { 2504 goto exit; 2505 } 2506 2507 /* 2508 * If we are opening the tape for writing, check 2509 * to make sure that the tape can be written. 2510 */ 2511 if (un->un_oflags & FWRITE) { 2512 err = 0; 2513 if (un->un_mspl->wp) { 2514 un->un_status = KEY_WRITE_PROTECT; 2515 un->un_laststate = un->un_state; 2516 un->un_state = ST_STATE_CLOSED; 2517 rval = EACCES; 2518 /* 2519 * STK sets the wp bit if volsafe tape is loaded. 2520 */ 2521 if ((un->un_dp->type == MT_ISSTK9840) && 2522 (un->un_dp->options & ST_WORMABLE)) { 2523 un->un_read_only = RDONLY; 2524 } else { 2525 goto exit; 2526 } 2527 } else { 2528 un->un_read_only = RDWR; 2529 } 2530 } else { 2531 un->un_read_only = RDONLY; 2532 } 2533 2534 if (un->un_dp->options & ST_WORMABLE) { 2535 un->un_read_only |= un->un_wormable(un); 2536 2537 if (((un->un_read_only == WORM) || 2538 (un->un_read_only == RDWORM)) && 2539 ((un->un_oflags & FWRITE) == FWRITE)) { 2540 un->un_status = KEY_DATA_PROTECT; 2541 rval = EACCES; 2542 ST_DEBUG4(ST_DEVINFO, st_label, CE_NOTE, 2543 "read_only = %d eof = %d oflag = %d\n", 2544 un->un_read_only, un->un_pos.eof, un->un_oflags); 2545 } 2546 } 2547 2548 /* 2549 * If we're opening the tape write-only, we need to 2550 * write 2 filemarks on the HP 1/2 inch drive, to 2551 * create a null file. 2552 */ 2553 if ((un->un_read_only == RDWR) || 2554 (un->un_read_only == WORM) && (un->un_oflags & FWRITE)) { 2555 if (un->un_dp->options & ST_REEL) { 2556 un->un_fmneeded = 2; 2557 } else { 2558 un->un_fmneeded = 1; 2559 } 2560 } else { 2561 un->un_fmneeded = 0; 2562 } 2563 2564 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 2565 "fmneeded = %x\n", un->un_fmneeded); 2566 2567 /* 2568 * Make sure the density can be selected correctly. 2569 * If WORM can only write at the append point which in most cases 2570 * isn't BOP. st_determine_density() with a B_WRITE only attempts 2571 * to set and try densities if a BOP. 2572 */ 2573 if (st_determine_density(dev, 2574 un->un_read_only == RDWR ? B_WRITE : B_READ)) { 2575 un->un_status = KEY_ILLEGAL_REQUEST; 2576 un->un_laststate = un->un_state; 2577 un->un_state = ST_STATE_CLOSED; 2578 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 2579 "st_tape_init: EIO can't determine density\n"); 2580 rval = EIO; 2581 goto exit; 2582 } 2583 2584 /* 2585 * Destroy the knowledge that we have 'determined' 2586 * density so that a later read at BOT comes along 2587 * does the right density determination. 2588 */ 2589 2590 un->un_density_known = 0; 2591 2592 2593 /* 2594 * Okay, the tape is loaded and either at BOT or somewhere past. 2595 * Mark the state such that any I/O or tape space operations 2596 * will get/set the right density, etc.. 2597 */ 2598 un->un_laststate = un->un_state; 2599 un->un_lastop = ST_OP_NIL; 2600 un->un_mediastate = MTIO_INSERTED; 2601 cv_broadcast(&un->un_state_cv); 2602 2603 /* 2604 * Set test append flag if writing. 2605 * First write must check that tape is positioned correctly. 2606 */ 2607 un->un_test_append = (un->un_oflags & FWRITE); 2608 2609 exit: 2610 un->un_err_resid = 0; 2611 un->un_last_resid = 0; 2612 un->un_last_count = 0; 2613 2614 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 2615 "st_tape_init: return val = %x\n", rval); 2616 return (rval); 2617 2618 } 2619 2620 2621 2622 /* ARGSUSED */ 2623 static int 2624 st_close(dev_t dev, int flag, int otyp, cred_t *cred_p) 2625 { 2626 int err = 0; 2627 int norew, count, last_state; 2628 #ifdef __x86 2629 struct contig_mem *cp, *cp_temp; 2630 #endif 2631 2632 GET_SOFT_STATE(dev); 2633 2634 ST_ENTR(ST_DEVINFO, st_close); 2635 2636 /* 2637 * wait till all cmds in the pipeline have been completed 2638 */ 2639 mutex_enter(ST_MUTEX); 2640 2641 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 2642 "st_close(dev = 0x%lx, flag = %d, otyp = %d)\n", dev, flag, otyp); 2643 2644 st_wait_for_io(un); 2645 2646 /* turn off persistent errors on close, as we want close to succeed */ 2647 TURN_PE_OFF(un); 2648 2649 /* 2650 * set state to indicate that we are in process of closing 2651 */ 2652 last_state = un->un_laststate = un->un_state; 2653 un->un_state = ST_STATE_CLOSING; 2654 2655 /* 2656 * BSD behavior: 2657 * a close always causes a silent span to the next file if we've hit 2658 * an EOF (but not yet read across it). 2659 */ 2660 #ifdef DEBUG 2661 if ((st_debug & 0xf) >= 6) 2662 st_print_position(un, "st_close1:", &un->un_pos); 2663 #endif 2664 2665 if (BSD_BEHAVIOR && (un->un_pos.eof == ST_EOF)) { 2666 if (un->un_pos.pmode != invalid) { 2667 un->un_pos.fileno++; 2668 un->un_pos.blkno = 0; 2669 } 2670 un->un_pos.eof = ST_NO_EOF; 2671 } 2672 2673 /* 2674 * rewinding? 2675 */ 2676 norew = (getminor(dev) & MT_NOREWIND); 2677 2678 /* 2679 * SVR4 behavior for skipping to next file: 2680 * 2681 * If we have not seen a filemark, space to the next file 2682 * 2683 * If we have already seen the filemark we are physically in the next 2684 * file and we only increment the filenumber 2685 */ 2686 2687 2688 if (norew && SVR4_BEHAVIOR && (flag & FREAD) && 2689 (un->un_pos.blkno != 0) && 2690 ((un->un_lastop != ST_OP_WRITE) && (un->un_lastop != ST_OP_WEOF))) { 2691 switch (un->un_pos.eof) { 2692 case ST_NO_EOF: 2693 /* 2694 * if we were reading and did not read the complete file 2695 * skip to the next file, leaving the tape correctly 2696 * positioned to read the first record of the next file 2697 * Check first for REEL if we are at EOT by trying to 2698 * read a block 2699 */ 2700 if ((un->un_dp->options & ST_REEL) && 2701 (!(un->un_dp->options & ST_READ_IGNORE_EOFS)) && 2702 (un->un_pos.blkno == 0)) { 2703 if (st_cmd(dev, SCMD_SPACE, Blk(1), SYNC_CMD)) { 2704 ST_DEBUG2(ST_DEVINFO, st_label, 2705 SCSI_DEBUG, 2706 "st_close : EIO can't space\n"); 2707 err = EIO; 2708 break; 2709 } 2710 if (un->un_pos.eof >= ST_EOF_PENDING) { 2711 un->un_pos.eof = ST_EOT_PENDING; 2712 un->un_pos.fileno += 1; 2713 un->un_pos.blkno = 0; 2714 break; 2715 } 2716 } 2717 if (st_cmd(dev, SCMD_SPACE, Fmk(1), SYNC_CMD)) { 2718 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2719 "st_close: EIO can't space #2\n"); 2720 err = EIO; 2721 } else { 2722 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 2723 "st_close2: fileno=%x,blkno=%x,eof=%x\n", 2724 un->un_pos.fileno, un->un_pos.blkno, 2725 un->un_pos.eof); 2726 un->un_pos.eof = ST_NO_EOF; 2727 } 2728 break; 2729 2730 case ST_EOF_PENDING: 2731 case ST_EOF: 2732 un->un_pos.fileno += 1; 2733 un->un_pos.blkno = 0; 2734 un->un_pos.eof = ST_NO_EOF; 2735 break; 2736 2737 case ST_EOT: 2738 case ST_EOT_PENDING: 2739 /* nothing to do */ 2740 break; 2741 default: 2742 scsi_log(ST_DEVINFO, st_label, CE_PANIC, 2743 "Undefined state 0x%x", un->un_pos.eof); 2744 2745 } 2746 } 2747 2748 2749 /* 2750 * For performance reasons (HP 88780), the driver should 2751 * postpone writing the second tape mark until just before a file 2752 * positioning ioctl is issued (e.g., rewind). This means that 2753 * the user must not manually rewind the tape because the tape will 2754 * be missing the second tape mark which marks EOM. 2755 * However, this small performance improvement is not worth the risk. 2756 */ 2757 2758 /* 2759 * We need to back up over the filemark we inadvertently popped 2760 * over doing a read in between the two filemarks that constitute 2761 * logical eot for 1/2" tapes. Note that ST_EOT_PENDING is only 2762 * set while reading. 2763 * 2764 * If we happen to be at physical eot (ST_EOM) (writing case), 2765 * the writing of filemark(s) will clear the ST_EOM state, which 2766 * we don't want, so we save this state and restore it later. 2767 */ 2768 2769 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 2770 "flag=%x, fmneeded=%x, lastop=%x, eof=%x\n", 2771 flag, un->un_fmneeded, un->un_lastop, un->un_pos.eof); 2772 2773 if (un->un_pos.eof == ST_EOT_PENDING) { 2774 if (norew) { 2775 if (st_cmd(dev, SCMD_SPACE, Fmk((-1)), SYNC_CMD)) { 2776 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2777 "st_close: EIO can't space #3\n"); 2778 err = EIO; 2779 } else { 2780 un->un_pos.blkno = 0; 2781 un->un_pos.eof = ST_EOT; 2782 } 2783 } else { 2784 un->un_pos.eof = ST_NO_EOF; 2785 } 2786 2787 /* 2788 * Do we need to write a file mark? 2789 * 2790 * only write filemarks if there are fmks to be written and 2791 * - open for write (possibly read/write) 2792 * - the last operation was a write 2793 * or: 2794 * - opened for wronly 2795 * - no data was written 2796 */ 2797 } else if ((un->un_pos.pmode != invalid) && (un->un_fmneeded > 0) && 2798 (((flag & FWRITE) && (un->un_lastop == ST_OP_WRITE)) || 2799 ((flag & FWRITE) && (un->un_lastop == ST_OP_WEOF)) || 2800 ((flag == FWRITE) && (un->un_lastop == ST_OP_NIL)))) { 2801 2802 /* save ST_EOM state */ 2803 int was_at_eom = (un->un_pos.eof == ST_EOM) ? 1 : 0; 2804 2805 /* 2806 * Note that we will write a filemark if we had opened 2807 * the tape write only and no data was written, thus 2808 * creating a null file. 2809 * 2810 * If the user already wrote one, we only have to write 1 more. 2811 * If they wrote two, we don't have to write any. 2812 */ 2813 2814 count = un->un_fmneeded; 2815 if (count > 0) { 2816 if (st_cmd(dev, SCMD_WRITE_FILE_MARK, 2817 count, SYNC_CMD)) { 2818 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2819 "st_close : EIO can't wfm\n"); 2820 err = EIO; 2821 } 2822 if ((un->un_dp->options & ST_REEL) && norew) { 2823 if (st_cmd(dev, SCMD_SPACE, Fmk((-1)), 2824 SYNC_CMD)) { 2825 ST_DEBUG2(ST_DEVINFO, st_label, 2826 SCSI_DEBUG, 2827 "st_close : EIO space fmk(-1)\n"); 2828 err = EIO; 2829 } 2830 un->un_pos.eof = ST_NO_EOF; 2831 /* fix up block number */ 2832 un->un_pos.blkno = 0; 2833 } 2834 } 2835 2836 /* 2837 * If we aren't going to be rewinding, and we were at 2838 * physical eot, restore the state that indicates we 2839 * are at physical eot. Once you have reached physical 2840 * eot, and you close the tape, the only thing you can 2841 * do on the next open is to rewind. Access to trailer 2842 * records is only allowed without closing the device. 2843 */ 2844 if (norew == 0 && was_at_eom) { 2845 un->un_pos.eof = ST_EOM; 2846 } 2847 } 2848 2849 /* 2850 * report soft errors if enabled and available, if we never accessed 2851 * the drive, don't get errors. This will prevent some DAT error 2852 * messages upon LOG SENSE. 2853 */ 2854 if (st_report_soft_errors_on_close && 2855 (un->un_dp->options & ST_SOFT_ERROR_REPORTING) && 2856 (last_state != ST_STATE_OFFLINE)) { 2857 (void) st_report_soft_errors(dev, flag); 2858 } 2859 2860 2861 /* 2862 * Do we need to rewind? Can we rewind? 2863 */ 2864 if (norew == 0 && un->un_pos.pmode != invalid && err == 0) { 2865 /* 2866 * We'd like to rewind with the 2867 * 'immediate' bit set, but this 2868 * causes problems on some drives 2869 * where subsequent opens get a 2870 * 'NOT READY' error condition 2871 * back while the tape is rewinding, 2872 * which is impossible to distinguish 2873 * from the condition of 'no tape loaded'. 2874 * 2875 * Also, for some targets, if you disconnect 2876 * with the 'immediate' bit set, you don't 2877 * actually return right away, i.e., the 2878 * target ignores your request for immediate 2879 * return. 2880 * 2881 * Instead, we'll fire off an async rewind 2882 * command. We'll mark the device as closed, 2883 * and any subsequent open will stall on 2884 * the first TEST_UNIT_READY until the rewind 2885 * completes. 2886 */ 2887 2888 /* 2889 * Used to be if reserve was not supported we'd send an 2890 * asynchronious rewind. Comments above may be slightly invalid 2891 * as the immediate bit was never set. Doing an immedate rewind 2892 * makes sense, I think fixes to not ready status might handle 2893 * the problems described above. 2894 */ 2895 if (un->un_sd->sd_inq->inq_ansi < 2) { 2896 (void) st_cmd(dev, SCMD_REWIND, 0, SYNC_CMD); 2897 } else { 2898 (void) st_cmd(dev, SCMD_REWIND, 0, ASYNC_CMD); 2899 } 2900 } 2901 2902 /* 2903 * eject tape if necessary 2904 */ 2905 if (un->un_eject_tape_on_failure) { 2906 un->un_eject_tape_on_failure = 0; 2907 if (st_cmd(dev, SCMD_LOAD, LD_UNLOAD, SYNC_CMD)) { 2908 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2909 "st_close : can't unload tape\n"); 2910 } else { 2911 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 2912 "st_close : tape unloaded \n"); 2913 un->un_pos.eof = ST_NO_EOF; 2914 un->un_mediastate = MTIO_EJECTED; 2915 } 2916 } 2917 /* 2918 * Release the tape unit, if default reserve/release 2919 * behaviour. 2920 */ 2921 if ((un->un_rsvd_status & 2922 (ST_RESERVE | ST_PRESERVE_RESERVE)) == ST_RESERVE) { 2923 (void) st_reserve_release(un, ST_RELEASE); 2924 } 2925 2926 /* 2927 * clear up state 2928 */ 2929 un->un_laststate = un->un_state; 2930 un->un_state = ST_STATE_CLOSED; 2931 un->un_lastop = ST_OP_NIL; 2932 un->un_throttle = 1; /* assume one request at time, for now */ 2933 un->un_retry_ct = 0; 2934 un->un_tran_retry_ct = 0; 2935 un->un_errno = 0; 2936 un->un_swr_token = (opaque_t)NULL; 2937 un->un_rsvd_status &= ~(ST_INIT_RESERVE); 2938 2939 /* Restore the options to the init time settings */ 2940 if (un->un_init_options & ST_READ_IGNORE_ILI) { 2941 un->un_dp->options |= ST_READ_IGNORE_ILI; 2942 } else { 2943 un->un_dp->options &= ~ST_READ_IGNORE_ILI; 2944 } 2945 2946 if (un->un_init_options & ST_READ_IGNORE_EOFS) { 2947 un->un_dp->options |= ST_READ_IGNORE_EOFS; 2948 } else { 2949 un->un_dp->options &= ~ST_READ_IGNORE_EOFS; 2950 } 2951 2952 if (un->un_init_options & ST_SHORT_FILEMARKS) { 2953 un->un_dp->options |= ST_SHORT_FILEMARKS; 2954 } else { 2955 un->un_dp->options &= ~ST_SHORT_FILEMARKS; 2956 } 2957 2958 ASSERT(mutex_owned(ST_MUTEX)); 2959 2960 /* 2961 * Signal anyone awaiting a close operation to complete. 2962 */ 2963 cv_signal(&un->un_clscv); 2964 2965 /* 2966 * any kind of error on closing causes all state to be tossed 2967 */ 2968 if (err && un->un_status != KEY_ILLEGAL_REQUEST) { 2969 /* 2970 * note that st_intr has already set 2971 * un_pos.pmode to invalid. 2972 */ 2973 un->un_density_known = 0; 2974 } 2975 2976 #ifdef __x86 2977 /* 2978 * free any contiguous mem alloc'ed for big block I/O 2979 */ 2980 cp = un->un_contig_mem; 2981 while (cp) { 2982 if (cp->cm_addr) { 2983 ddi_dma_mem_free(&cp->cm_acc_hdl); 2984 } 2985 cp_temp = cp; 2986 cp = cp->cm_next; 2987 kmem_free(cp_temp, 2988 sizeof (struct contig_mem) + biosize()); 2989 } 2990 un->un_contig_mem_total_num = 0; 2991 un->un_contig_mem_available_num = 0; 2992 un->un_contig_mem = NULL; 2993 un->un_max_contig_mem_len = 0; 2994 #endif 2995 2996 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 2997 "st_close3: return val = %x, fileno=%x, blkno=%x, eof=%x\n", 2998 err, un->un_pos.fileno, un->un_pos.blkno, un->un_pos.eof); 2999 3000 mutex_exit(ST_MUTEX); 3001 return (err); 3002 } 3003 3004 /* 3005 * These routines perform raw i/o operations. 3006 */ 3007 3008 /* ARGSUSED2 */ 3009 static int 3010 st_aread(dev_t dev, struct aio_req *aio, cred_t *cred_p) 3011 { 3012 #ifdef DEBUG 3013 GET_SOFT_STATE(dev); 3014 ST_ENTR(ST_DEVINFO, st_aread); 3015 #endif 3016 return (st_arw(dev, aio, B_READ)); 3017 } 3018 3019 3020 /* ARGSUSED2 */ 3021 static int 3022 st_awrite(dev_t dev, struct aio_req *aio, cred_t *cred_p) 3023 { 3024 #ifdef DEBUG 3025 GET_SOFT_STATE(dev); 3026 ST_ENTR(ST_DEVINFO, st_awrite); 3027 #endif 3028 return (st_arw(dev, aio, B_WRITE)); 3029 } 3030 3031 3032 3033 /* ARGSUSED */ 3034 static int 3035 st_read(dev_t dev, struct uio *uiop, cred_t *cred_p) 3036 { 3037 #ifdef DEBUG 3038 GET_SOFT_STATE(dev); 3039 ST_ENTR(ST_DEVINFO, st_read); 3040 #endif 3041 return (st_rw(dev, uiop, B_READ)); 3042 } 3043 3044 /* ARGSUSED */ 3045 static int 3046 st_write(dev_t dev, struct uio *uiop, cred_t *cred_p) 3047 { 3048 #ifdef DEBUG 3049 GET_SOFT_STATE(dev); 3050 ST_ENTR(ST_DEVINFO, st_write); 3051 #endif 3052 return (st_rw(dev, uiop, B_WRITE)); 3053 } 3054 3055 /* 3056 * Due to historical reasons, old limits are: For variable-length devices: 3057 * if greater than 64KB - 1 (ST_MAXRECSIZE_VARIABLE), block into 64 KB - 2 3058 * ST_MAXRECSIZE_VARIABLE_LIMIT) requests; otherwise, 3059 * (let it through unmodified. For fixed-length record devices: 3060 * 63K (ST_MAXRECSIZE_FIXED) is max (default minphys). 3061 * 3062 * The new limits used are un_maxdma (retrieved using scsi_ifgetcap() 3063 * from the HBA) and un_maxbsize (retrieved by sending SCMD_READ_BLKLIM 3064 * command to the drive). 3065 * 3066 */ 3067 static void 3068 st_minphys(struct buf *bp) 3069 { 3070 struct scsi_tape *un; 3071 3072 un = ddi_get_soft_state(st_state, MTUNIT(bp->b_edev)); 3073 3074 ST_FUNC(ST_DEVINFO, st_minphys); 3075 3076 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 3077 "st_minphys(bp = 0x%p): b_bcount = 0x%lx\n", (void *)bp, 3078 bp->b_bcount); 3079 3080 if (un->un_allow_large_xfer) { 3081 3082 /* 3083 * check un_maxbsize for variable length devices only 3084 */ 3085 if (un->un_bsize == 0 && bp->b_bcount > un->un_maxbsize) { 3086 bp->b_bcount = un->un_maxbsize; 3087 } 3088 /* 3089 * can't go more that HBA maxdma limit in either fixed-length 3090 * or variable-length tape drives. 3091 */ 3092 if (bp->b_bcount > un->un_maxdma) { 3093 bp->b_bcount = un->un_maxdma; 3094 } 3095 } else { 3096 3097 /* 3098 * use old fixed limits 3099 */ 3100 if (un->un_bsize == 0) { 3101 if (bp->b_bcount > ST_MAXRECSIZE_VARIABLE) { 3102 bp->b_bcount = ST_MAXRECSIZE_VARIABLE_LIMIT; 3103 } 3104 } else { 3105 if (bp->b_bcount > ST_MAXRECSIZE_FIXED) { 3106 bp->b_bcount = ST_MAXRECSIZE_FIXED; 3107 } 3108 } 3109 } 3110 3111 /* 3112 * For regular raw I/O and Fixed Block length devices, make sure 3113 * the adjusted block count is a whole multiple of the device 3114 * block size. 3115 */ 3116 if (bp != un->un_sbufp && un->un_bsize) { 3117 bp->b_bcount -= (bp->b_bcount % un->un_bsize); 3118 } 3119 } 3120 3121 static int 3122 st_rw(dev_t dev, struct uio *uio, int flag) 3123 { 3124 int rval = 0; 3125 long len; 3126 3127 GET_SOFT_STATE(dev); 3128 3129 ST_FUNC(ST_DEVINFO, st_rw); 3130 3131 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 3132 "st_rw(dev = 0x%lx, flag = %s)\n", dev, 3133 (flag == B_READ ? rd_str: wr_str)); 3134 3135 /* get local copy of transfer length */ 3136 len = uio->uio_iov->iov_len; 3137 3138 mutex_enter(ST_MUTEX); 3139 3140 /* 3141 * Clear error entry stack 3142 */ 3143 st_empty_error_stack(un); 3144 3145 /* 3146 * If in fixed block size mode and requested read or write 3147 * is not an even multiple of that block size. 3148 */ 3149 if ((un->un_bsize != 0) && (len % un->un_bsize != 0)) { 3150 scsi_log(ST_DEVINFO, st_label, CE_WARN, 3151 "%s: not modulo %d block size\n", 3152 (flag == B_WRITE) ? wr_str : rd_str, un->un_bsize); 3153 rval = EINVAL; 3154 } 3155 3156 /* If device has set granularity in the READ_BLKLIM we honor it. */ 3157 if ((un->un_data_mod != 0) && (len % un->un_data_mod != 0)) { 3158 scsi_log(ST_DEVINFO, st_label, CE_WARN, 3159 "%s: not modulo %d device granularity\n", 3160 (flag == B_WRITE) ? wr_str : rd_str, un->un_data_mod); 3161 rval = EINVAL; 3162 } 3163 3164 if (rval != 0) { 3165 un->un_errno = rval; 3166 mutex_exit(ST_MUTEX); 3167 return (rval); 3168 } 3169 3170 /* 3171 * Reset this so it can be set if Berkeley and read over a filemark. 3172 */ 3173 un->un_silent_skip = 0; 3174 mutex_exit(ST_MUTEX); 3175 3176 len = uio->uio_resid; 3177 3178 rval = physio(st_strategy, (struct buf *)NULL, 3179 dev, flag, st_minphys, uio); 3180 /* 3181 * if we have hit logical EOT during this xfer and there is not a 3182 * full residue, then set eof back to ST_EOM to make sure that 3183 * the user will see at least one zero write 3184 * after this short write 3185 */ 3186 mutex_enter(ST_MUTEX); 3187 if (un->un_pos.eof > ST_NO_EOF) { 3188 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 3189 "eof=%d resid=%lx\n", un->un_pos.eof, uio->uio_resid); 3190 } 3191 if (un->un_pos.eof >= ST_EOM && (flag == B_WRITE)) { 3192 if ((uio->uio_resid != len) && (uio->uio_resid != 0)) { 3193 un->un_pos.eof = ST_EOM; 3194 } else if (uio->uio_resid == len) { 3195 un->un_pos.eof = ST_NO_EOF; 3196 } 3197 } 3198 3199 if (un->un_silent_skip && uio->uio_resid != len) { 3200 un->un_pos.eof = ST_EOF; 3201 un->un_pos.blkno = un->un_save_blkno; 3202 un->un_pos.fileno--; 3203 } 3204 3205 un->un_errno = rval; 3206 3207 mutex_exit(ST_MUTEX); 3208 3209 return (rval); 3210 } 3211 3212 static int 3213 st_arw(dev_t dev, struct aio_req *aio, int flag) 3214 { 3215 struct uio *uio = aio->aio_uio; 3216 int rval = 0; 3217 long len; 3218 3219 GET_SOFT_STATE(dev); 3220 3221 ST_FUNC(ST_DEVINFO, st_arw); 3222 3223 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 3224 "st_rw(dev = 0x%lx, flag = %s)\n", dev, 3225 (flag == B_READ ? rd_str: wr_str)); 3226 3227 /* get local copy of transfer length */ 3228 len = uio->uio_iov->iov_len; 3229 3230 mutex_enter(ST_MUTEX); 3231 3232 /* 3233 * If in fixed block size mode and requested read or write 3234 * is not an even multiple of that block size. 3235 */ 3236 if ((un->un_bsize != 0) && (len % un->un_bsize != 0)) { 3237 scsi_log(ST_DEVINFO, st_label, CE_WARN, 3238 "%s: not modulo %d block size\n", 3239 (flag == B_WRITE) ? wr_str : rd_str, un->un_bsize); 3240 rval = EINVAL; 3241 } 3242 3243 /* If device has set granularity in the READ_BLKLIM we honor it. */ 3244 if ((un->un_data_mod != 0) && (len % un->un_data_mod != 0)) { 3245 scsi_log(ST_DEVINFO, st_label, CE_WARN, 3246 "%s: not modulo %d device granularity\n", 3247 (flag == B_WRITE) ? wr_str : rd_str, un->un_data_mod); 3248 rval = EINVAL; 3249 } 3250 3251 if (rval != 0) { 3252 un->un_errno = rval; 3253 mutex_exit(ST_MUTEX); 3254 return (rval); 3255 } 3256 3257 mutex_exit(ST_MUTEX); 3258 3259 len = uio->uio_resid; 3260 3261 rval = aphysio(st_strategy, anocancel, dev, flag, st_minphys, aio); 3262 3263 /* 3264 * if we have hit logical EOT during this xfer and there is not a 3265 * full residue, then set eof back to ST_EOM to make sure that 3266 * the user will see at least one zero write 3267 * after this short write 3268 * 3269 * we keep this here just in case the application is not using 3270 * persistent errors 3271 */ 3272 mutex_enter(ST_MUTEX); 3273 if (un->un_pos.eof > ST_NO_EOF) { 3274 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 3275 "eof=%d resid=%lx\n", un->un_pos.eof, uio->uio_resid); 3276 } 3277 if (un->un_pos.eof >= ST_EOM && (flag == B_WRITE)) { 3278 if ((uio->uio_resid != len) && (uio->uio_resid != 0)) { 3279 un->un_pos.eof = ST_EOM; 3280 } else if (uio->uio_resid == len && !IS_PE_FLAG_SET(un)) { 3281 un->un_pos.eof = ST_NO_EOF; 3282 } 3283 } 3284 un->un_errno = rval; 3285 mutex_exit(ST_MUTEX); 3286 3287 return (rval); 3288 } 3289 3290 3291 3292 static int 3293 st_strategy(struct buf *bp) 3294 { 3295 struct scsi_tape *un; 3296 dev_t dev = bp->b_edev; 3297 3298 /* 3299 * validate arguments 3300 */ 3301 if ((un = ddi_get_soft_state(st_state, MTUNIT(bp->b_edev))) == NULL) { 3302 bp->b_resid = bp->b_bcount; 3303 mutex_enter(ST_MUTEX); 3304 st_bioerror(bp, ENXIO); 3305 mutex_exit(ST_MUTEX); 3306 goto error; 3307 } 3308 3309 ST_ENTR(ST_DEVINFO, st_strategy); 3310 3311 mutex_enter(ST_MUTEX); 3312 3313 while (un->un_pwr_mgmt == ST_PWR_SUSPENDED) { 3314 cv_wait(&un->un_suspend_cv, ST_MUTEX); 3315 } 3316 3317 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 3318 "st_strategy(): bcount=0x%lx, fileno=%d, blkno=%x, eof=%d\n", 3319 bp->b_bcount, un->un_pos.fileno, un->un_pos.blkno, un->un_pos.eof); 3320 3321 /* 3322 * If persistent errors have been flagged, just nix this one. We wait 3323 * for any outstanding I/O's below, so we will be in order. 3324 */ 3325 if (IS_PE_FLAG_SET(un)) { 3326 goto exit; 3327 } 3328 3329 if (bp != un->un_sbufp) { 3330 char reading = bp->b_flags & B_READ; 3331 int wasopening = 0; 3332 3333 /* 3334 * If we haven't done/checked reservation on the tape unit 3335 * do it now. 3336 */ 3337 if ((un->un_rsvd_status & 3338 (ST_RESERVE | ST_APPLICATION_RESERVATIONS)) == 0) { 3339 if ((un->un_dp->options & ST_NO_RESERVE_RELEASE) == 0) { 3340 if (st_reserve_release(un, ST_RESERVE)) { 3341 st_bioerror(bp, un->un_errno); 3342 goto exit; 3343 } 3344 } else if (un->un_state == ST_STATE_OPEN_PENDING_IO) { 3345 /* 3346 * Enter here to restore position for possible 3347 * resets when the device was closed and opened 3348 * in O_NDELAY mode subsequently 3349 */ 3350 un->un_state = ST_STATE_INITIALIZING; 3351 (void) st_cmd(dev, SCMD_TEST_UNIT_READY, 3352 0, SYNC_CMD); 3353 un->un_state = ST_STATE_OPEN_PENDING_IO; 3354 } 3355 un->un_rsvd_status |= ST_INIT_RESERVE; 3356 } 3357 3358 /* 3359 * If we are offline, we have to initialize everything first. 3360 * This is to handle either when opened with O_NDELAY, or 3361 * we just got a new tape in the drive, after an offline. 3362 * We don't observe O_NDELAY past the open, 3363 * as it will not make sense for tapes. 3364 */ 3365 if (un->un_state == ST_STATE_OFFLINE || un->un_restore_pos) { 3366 /* reset state to avoid recursion */ 3367 un->un_state = ST_STATE_INITIALIZING; 3368 if (st_tape_init(dev)) { 3369 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 3370 "stioctl : OFFLINE init failure "); 3371 un->un_state = ST_STATE_OFFLINE; 3372 un->un_pos.pmode = invalid; 3373 goto b_done_err; 3374 } 3375 un->un_state = ST_STATE_OPEN_PENDING_IO; 3376 } 3377 /* 3378 * Check for legal operations 3379 */ 3380 if (un->un_pos.pmode == invalid) { 3381 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 3382 "strategy with un->un_pos.pmode invalid\n"); 3383 goto b_done_err; 3384 } 3385 3386 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 3387 "st_strategy(): regular io\n"); 3388 3389 /* 3390 * Process this first. If we were reading, and we're pending 3391 * logical eot, that means we've bumped one file mark too far. 3392 */ 3393 3394 /* 3395 * Recursion warning: st_cmd will route back through here. 3396 */ 3397 if (un->un_pos.eof == ST_EOT_PENDING) { 3398 if (st_cmd(dev, SCMD_SPACE, Fmk((-1)), SYNC_CMD)) { 3399 un->un_pos.pmode = invalid; 3400 un->un_density_known = 0; 3401 goto b_done_err; 3402 } 3403 un->un_pos.blkno = 0; /* fix up block number.. */ 3404 un->un_pos.eof = ST_EOT; 3405 } 3406 3407 /* 3408 * If we are in the process of opening, we may have to 3409 * determine/set the correct density. We also may have 3410 * to do a test_append (if QIC) to see whether we are 3411 * in a position to append to the end of the tape. 3412 * 3413 * If we're already at logical eot, we transition 3414 * to ST_NO_EOF. If we're at physical eot, we punt 3415 * to the switch statement below to handle. 3416 */ 3417 if ((un->un_state == ST_STATE_OPEN_PENDING_IO) || 3418 (un->un_test_append && (un->un_dp->options & ST_QIC))) { 3419 3420 if (un->un_state == ST_STATE_OPEN_PENDING_IO) { 3421 if (st_determine_density(dev, (int)reading)) { 3422 goto b_done_err; 3423 } 3424 } 3425 3426 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 3427 "pending_io@fileno %d rw %d qic %d eof %d\n", 3428 un->un_pos.fileno, (int)reading, 3429 (un->un_dp->options & ST_QIC) ? 1 : 0, 3430 un->un_pos.eof); 3431 3432 if (!reading && un->un_pos.eof != ST_EOM) { 3433 if (un->un_pos.eof == ST_EOT) { 3434 un->un_pos.eof = ST_NO_EOF; 3435 } else if (un->un_pos.pmode != invalid && 3436 (un->un_dp->options & ST_QIC)) { 3437 /* 3438 * st_test_append() will do it all 3439 */ 3440 st_test_append(bp); 3441 goto done; 3442 } 3443 } 3444 if (un->un_state == ST_STATE_OPEN_PENDING_IO) { 3445 wasopening = 1; 3446 } 3447 un->un_laststate = un->un_state; 3448 un->un_state = ST_STATE_OPEN; 3449 } 3450 3451 3452 /* 3453 * Process rest of END OF FILE and END OF TAPE conditions 3454 */ 3455 3456 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 3457 "eof=%x, wasopening=%x\n", 3458 un->un_pos.eof, wasopening); 3459 3460 switch (un->un_pos.eof) { 3461 case ST_EOM: 3462 /* 3463 * This allows writes to proceed past physical 3464 * eot. We'll *really* be in trouble if the 3465 * user continues blindly writing data too 3466 * much past this point (unwind the tape). 3467 * Physical eot really means 'early warning 3468 * eot' in this context. 3469 * 3470 * Every other write from now on will succeed 3471 * (if sufficient tape left). 3472 * This write will return with resid == count 3473 * but the next one should be successful 3474 * 3475 * Note that we only transition to logical EOT 3476 * if the last state wasn't the OPENING state. 3477 * We explicitly prohibit running up to physical 3478 * eot, closing the device, and then re-opening 3479 * to proceed. Trailer records may only be gotten 3480 * at by keeping the tape open after hitting eot. 3481 * 3482 * Also note that ST_EOM cannot be set by reading- 3483 * this can only be set during writing. Reading 3484 * up to the end of the tape gets a blank check 3485 * or a double-filemark indication (ST_EOT_PENDING), 3486 * and we prohibit reading after that point. 3487 * 3488 */ 3489 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "EOM\n"); 3490 if (wasopening == 0) { 3491 /* 3492 * this allows st_rw() to reset it back to 3493 * ST_EOM to make sure that the application 3494 * will see a zero write 3495 */ 3496 un->un_pos.eof = ST_WRITE_AFTER_EOM; 3497 } 3498 un->un_status = SUN_KEY_EOT; 3499 goto b_done; 3500 3501 case ST_WRITE_AFTER_EOM: 3502 case ST_EOT: 3503 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "EOT\n"); 3504 un->un_status = SUN_KEY_EOT; 3505 if (SVR4_BEHAVIOR && reading) { 3506 goto b_done_err; 3507 } 3508 3509 if (reading) { 3510 goto b_done; 3511 } 3512 un->un_pos.eof = ST_NO_EOF; 3513 break; 3514 3515 case ST_EOF_PENDING: 3516 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 3517 "EOF PENDING\n"); 3518 un->un_status = SUN_KEY_EOF; 3519 if (SVR4_BEHAVIOR) { 3520 un->un_pos.eof = ST_EOF; 3521 goto b_done; 3522 } 3523 /* FALLTHROUGH */ 3524 case ST_EOF: 3525 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "EOF\n"); 3526 un->un_status = SUN_KEY_EOF; 3527 if (SVR4_BEHAVIOR) { 3528 goto b_done_err; 3529 } 3530 3531 if (BSD_BEHAVIOR) { 3532 un->un_pos.eof = ST_NO_EOF; 3533 un->un_pos.fileno += 1; 3534 un->un_pos.blkno = 0; 3535 } 3536 3537 if (reading) { 3538 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 3539 "now file %d (read)\n", 3540 un->un_pos.fileno); 3541 goto b_done; 3542 } 3543 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 3544 "now file %d (write)\n", un->un_pos.fileno); 3545 break; 3546 default: 3547 un->un_status = 0; 3548 break; 3549 } 3550 } 3551 3552 bp->b_flags &= ~(B_DONE); 3553 st_bioerror(bp, 0); 3554 bp->av_forw = NULL; 3555 bp->b_resid = 0; 3556 SET_BP_PKT(bp, 0); 3557 3558 3559 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 3560 "st_strategy: cmd=0x%p count=%ld resid=%ld flags=0x%x" 3561 " pkt=0x%p\n", 3562 (void *)bp->b_forw, bp->b_bcount, 3563 bp->b_resid, bp->b_flags, (void *)BP_PKT(bp)); 3564 3565 #ifdef __x86 3566 /* 3567 * We will replace bp with a new bp that can do big blk xfer 3568 * if the requested xfer size is bigger than un->un_maxdma_arch 3569 * 3570 * Also, we need to make sure that we're handling real I/O 3571 * by checking group 0/1 SCSI I/O commands, if needed 3572 */ 3573 if (bp->b_bcount > un->un_maxdma_arch && 3574 (bp != un->un_sbufp || 3575 (uchar_t)(uintptr_t)bp->b_forw == SCMD_READ || 3576 (uchar_t)(uintptr_t)bp->b_forw == SCMD_READ_G1 || 3577 (uchar_t)(uintptr_t)bp->b_forw == SCMD_WRITE || 3578 (uchar_t)(uintptr_t)bp->b_forw == SCMD_WRITE_G1)) { 3579 mutex_exit(ST_MUTEX); 3580 bp = st_get_bigblk_bp(bp); 3581 mutex_enter(ST_MUTEX); 3582 } 3583 #endif 3584 3585 /* put on wait queue */ 3586 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 3587 "st_strategy: un->un_quef = 0x%p, bp = 0x%p\n", 3588 (void *)un->un_quef, (void *)bp); 3589 3590 if (un->un_quef) { 3591 un->un_quel->b_actf = bp; 3592 } else { 3593 un->un_quef = bp; 3594 } 3595 un->un_quel = bp; 3596 3597 ST_DO_KSTATS(bp, kstat_waitq_enter); 3598 3599 st_start(un); 3600 3601 done: 3602 mutex_exit(ST_MUTEX); 3603 return (0); 3604 3605 3606 error: 3607 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 3608 "st_strategy: error exit\n"); 3609 3610 biodone(bp); 3611 return (0); 3612 3613 b_done_err: 3614 st_bioerror(bp, EIO); 3615 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 3616 "st_strategy : EIO b_done_err\n"); 3617 3618 b_done: 3619 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 3620 "st_strategy: b_done\n"); 3621 3622 exit: 3623 /* 3624 * make sure no commands are outstanding or waiting before closing, 3625 * so we can guarantee order 3626 */ 3627 st_wait_for_io(un); 3628 un->un_err_resid = bp->b_resid = bp->b_bcount; 3629 3630 /* override errno here, if persistent errors were flagged */ 3631 if (IS_PE_FLAG_SET(un)) 3632 bioerror(bp, un->un_errno); 3633 3634 mutex_exit(ST_MUTEX); 3635 3636 biodone(bp); 3637 ASSERT(mutex_owned(ST_MUTEX) == 0); 3638 return (0); 3639 } 3640 3641 3642 3643 /* 3644 * this routine spaces forward over filemarks 3645 */ 3646 static int 3647 st_space_fmks(dev_t dev, int count) 3648 { 3649 int rval = 0; 3650 3651 GET_SOFT_STATE(dev); 3652 3653 ST_FUNC(ST_DEVINFO, st_space_fmks); 3654 3655 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 3656 "st_space_fmks(dev = 0x%lx, count = %d)\n", dev, count); 3657 3658 ASSERT(mutex_owned(ST_MUTEX)); 3659 3660 /* 3661 * the risk with doing only one space operation is that we 3662 * may accidentily jump in old data 3663 * the exabyte 8500 reading 8200 tapes cannot use KNOWS_EOD 3664 * because the 8200 does not append a marker; in order not to 3665 * sacrifice the fast file skip, we do a slow skip if the low 3666 * density device has been opened 3667 */ 3668 3669 if ((un->un_dp->options & ST_KNOWS_EOD) && 3670 !((un->un_dp->type == ST_TYPE_EXB8500 && MT_DENSITY(dev) == 0))) { 3671 if (st_cmd(dev, SCMD_SPACE, Fmk(count), SYNC_CMD)) { 3672 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 3673 "space_fmks : EIO can't do space cmd #1\n"); 3674 rval = EIO; 3675 } 3676 } else { 3677 while (count > 0) { 3678 if (st_cmd(dev, SCMD_SPACE, Fmk(1), SYNC_CMD)) { 3679 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 3680 "space_fmks : EIO can't do space cmd #2\n"); 3681 rval = EIO; 3682 break; 3683 } 3684 count -= 1; 3685 /* 3686 * read a block to see if we have reached 3687 * end of medium (double filemark for reel or 3688 * medium error for others) 3689 */ 3690 if (count > 0) { 3691 if (st_cmd(dev, SCMD_SPACE, Blk(1), 3692 SYNC_CMD)) { 3693 ST_DEBUG2(ST_DEVINFO, st_label, 3694 SCSI_DEBUG, 3695 "space_fmks : EIO can't do " 3696 "space cmd #3\n"); 3697 rval = EIO; 3698 break; 3699 } 3700 if ((un->un_pos.eof >= ST_EOF_PENDING) && 3701 (un->un_dp->options & ST_REEL)) { 3702 un->un_status = SUN_KEY_EOT; 3703 ST_DEBUG2(ST_DEVINFO, st_label, 3704 SCSI_DEBUG, 3705 "space_fmks : EIO ST_REEL\n"); 3706 rval = EIO; 3707 break; 3708 } else if (IN_EOF(un->un_pos)) { 3709 un->un_pos.eof = ST_NO_EOF; 3710 un->un_pos.fileno++; 3711 un->un_pos.blkno = 0; 3712 count--; 3713 } else if (un->un_pos.eof > ST_EOF) { 3714 ST_DEBUG2(ST_DEVINFO, st_label, 3715 SCSI_DEBUG, 3716 "space_fmks, EIO > ST_EOF\n"); 3717 rval = EIO; 3718 break; 3719 } 3720 3721 } 3722 } 3723 un->un_err_resid = count; 3724 COPY_POS(&un->un_pos, &un->un_err_pos); 3725 } 3726 ASSERT(mutex_owned(ST_MUTEX)); 3727 return (rval); 3728 } 3729 3730 /* 3731 * this routine spaces to EOD 3732 * 3733 * it keeps track of the current filenumber and returns the filenumber after 3734 * the last successful space operation, we keep the number high because as 3735 * tapes are getting larger, the possibility of more and more files exist, 3736 * 0x100000 (1 Meg of files) probably will never have to be changed any time 3737 * soon 3738 */ 3739 #define MAX_SKIP 0x100000 /* somewhat arbitrary */ 3740 3741 static int 3742 st_find_eod(dev_t dev) 3743 { 3744 tapepos_t savepos; 3745 int sp_type; 3746 struct scsi_tape *un; 3747 int instance; 3748 int result; 3749 3750 instance = MTUNIT(dev); 3751 un = ddi_get_soft_state(st_state, instance); 3752 if (un == NULL) { 3753 return (-1); 3754 } 3755 3756 ST_FUNC(ST_DEVINFO, st_find_eod); 3757 3758 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 3759 "st_find_eod(dev = 0x%lx): fileno = %d\n", dev, un->un_pos.fileno); 3760 3761 ASSERT(mutex_owned(ST_MUTEX)); 3762 3763 COPY_POS(&savepos, &un->un_pos); 3764 3765 /* 3766 * see if the drive is smart enough to do the skips in 3767 * one operation; 1/2" use two filemarks 3768 * the exabyte 8500 reading 8200 tapes cannot use KNOWS_EOD 3769 * because the 8200 does not append a marker; in order not to 3770 * sacrifice the fast file skip, we do a slow skip if the low 3771 * density device has been opened 3772 */ 3773 if ((un->un_dp->options & ST_KNOWS_EOD) != 0) { 3774 if ((un->un_dp->type == ST_TYPE_EXB8500) && 3775 (MT_DENSITY(dev) == 0)) { 3776 sp_type = Fmk(1); 3777 } else if (un->un_pos.pmode == logical) { 3778 sp_type = SPACE(SP_EOD, 0); 3779 } else { 3780 sp_type = Fmk(MAX_SKIP); 3781 } 3782 } else { 3783 sp_type = Fmk(1); 3784 } 3785 3786 for (;;) { 3787 result = st_cmd(dev, SCMD_SPACE, sp_type, SYNC_CMD); 3788 3789 if (result == 0) { 3790 COPY_POS(&savepos, &un->un_pos); 3791 } 3792 3793 if (sp_type == SPACE(SP_EOD, 0)) { 3794 if (result != 0) { 3795 sp_type = Fmk(MAX_SKIP); 3796 continue; 3797 } 3798 3799 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 3800 "st_find_eod: 0x%"PRIx64"\n", 3801 savepos.lgclblkno); 3802 /* 3803 * What we return will become the current file position. 3804 * After completing the space command with the position 3805 * mode that is not invalid a read position command will 3806 * be automaticly issued. If the drive support the long 3807 * read position format a valid file position can be 3808 * returned. 3809 */ 3810 return (un->un_pos.fileno); 3811 } 3812 3813 if (result != 0) { 3814 break; 3815 } 3816 3817 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 3818 "count=%x, eof=%x, status=%x\n", 3819 SPACE_CNT(sp_type), un->un_pos.eof, un->un_status); 3820 3821 /* 3822 * If we're not EOM smart, space a record 3823 * to see whether we're now in the slot between 3824 * the two sequential filemarks that logical 3825 * EOM consists of (REEL) or hit nowhere land 3826 * (8mm). 3827 */ 3828 if (sp_type == Fmk(1)) { 3829 /* 3830 * no fast skipping, check a record 3831 */ 3832 if (st_cmd(dev, SCMD_SPACE, Blk((1)), SYNC_CMD)) { 3833 break; 3834 } 3835 if ((un->un_pos.eof >= ST_EOF_PENDING) && 3836 (un->un_dp->options & ST_REEL)) { 3837 un->un_status = KEY_BLANK_CHECK; 3838 un->un_pos.fileno++; 3839 un->un_pos.blkno = 0; 3840 break; 3841 } 3842 if (IN_EOF(un->un_pos)) { 3843 un->un_pos.eof = ST_NO_EOF; 3844 un->un_pos.fileno++; 3845 un->un_pos.blkno = 0; 3846 } 3847 if (un->un_pos.eof > ST_EOF) { 3848 break; 3849 } 3850 } else { 3851 if (un->un_pos.eof > ST_EOF) { 3852 break; 3853 } 3854 } 3855 } 3856 3857 if (un->un_dp->options & ST_KNOWS_EOD) { 3858 COPY_POS(&savepos, &un->un_pos); 3859 } 3860 3861 ASSERT(mutex_owned(ST_MUTEX)); 3862 3863 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 3864 "st_find_eod: %x\n", savepos.fileno); 3865 return (savepos.fileno); 3866 } 3867 3868 3869 /* 3870 * this routine is frequently used in ioctls below; 3871 * it determines whether we know the density and if not will 3872 * determine it 3873 * if we have written the tape before, one or more filemarks are written 3874 * 3875 * depending on the stepflag, the head is repositioned to where it was before 3876 * the filemarks were written in order not to confuse step counts 3877 */ 3878 #define STEPBACK 0 3879 #define NO_STEPBACK 1 3880 3881 static int 3882 st_check_density_or_wfm(dev_t dev, int wfm, int mode, int stepflag) 3883 { 3884 3885 GET_SOFT_STATE(dev); 3886 3887 ST_FUNC(ST_DEVINFO, st_check_density_or_wfm); 3888 3889 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 3890 "st_check_density_or_wfm(dev= 0x%lx, wfm= %d, mode= %d, stpflg= %d)" 3891 "\n", dev, wfm, mode, stepflag); 3892 3893 ASSERT(mutex_owned(ST_MUTEX)); 3894 3895 /* 3896 * If we don't yet know the density of the tape we have inserted, 3897 * we have to either unconditionally set it (if we're 'writing'), 3898 * or we have to determine it. As side effects, check for any 3899 * write-protect errors, and for the need to put out any file-marks 3900 * before positioning a tape. 3901 * 3902 * If we are going to be spacing forward, and we haven't determined 3903 * the tape density yet, we have to do so now... 3904 */ 3905 if (un->un_state == ST_STATE_OPEN_PENDING_IO) { 3906 if (st_determine_density(dev, mode)) { 3907 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 3908 "check_density_or_wfm : EIO can't determine " 3909 "density\n"); 3910 un->un_errno = EIO; 3911 return (EIO); 3912 } 3913 /* 3914 * Presumably we are at BOT. If we attempt to write, it will 3915 * either work okay, or bomb. We don't do a st_test_append 3916 * unless we're past BOT. 3917 */ 3918 un->un_laststate = un->un_state; 3919 un->un_state = ST_STATE_OPEN; 3920 3921 } else if (un->un_pos.pmode != invalid && un->un_fmneeded > 0 && 3922 ((un->un_lastop == ST_OP_WEOF && wfm) || 3923 (un->un_lastop == ST_OP_WRITE && wfm))) { 3924 3925 tapepos_t spos; 3926 3927 COPY_POS(&spos, &un->un_pos); 3928 3929 /* 3930 * We need to write one or two filemarks. 3931 * In the case of the HP, we need to 3932 * position the head between the two 3933 * marks. 3934 */ 3935 if ((un->un_fmneeded > 0) || (un->un_lastop == ST_OP_WEOF)) { 3936 wfm = un->un_fmneeded; 3937 un->un_fmneeded = 0; 3938 } 3939 3940 if (st_write_fm(dev, wfm)) { 3941 un->un_pos.pmode = invalid; 3942 un->un_density_known = 0; 3943 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 3944 "check_density_or_wfm : EIO can't write fm\n"); 3945 un->un_errno = EIO; 3946 return (EIO); 3947 } 3948 3949 if (stepflag == STEPBACK) { 3950 if (st_cmd(dev, SCMD_SPACE, Fmk((-wfm)), SYNC_CMD)) { 3951 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 3952 "check_density_or_wfm : EIO can't space " 3953 "(-wfm)\n"); 3954 un->un_errno = EIO; 3955 return (EIO); 3956 } 3957 COPY_POS(&un->un_pos, &spos); 3958 } 3959 } 3960 3961 /* 3962 * Whatever we do at this point clears the state of the eof flag. 3963 */ 3964 3965 un->un_pos.eof = ST_NO_EOF; 3966 3967 /* 3968 * If writing, let's check that we're positioned correctly 3969 * at the end of tape before issuing the next write. 3970 */ 3971 if (un->un_read_only == RDWR) { 3972 un->un_test_append = 1; 3973 } 3974 3975 ASSERT(mutex_owned(ST_MUTEX)); 3976 return (0); 3977 } 3978 3979 3980 /* 3981 * Wait for all outstaning I/O's to complete 3982 * 3983 * we wait on both ncmds and the wait queue for times when we are flushing 3984 * after persistent errors are flagged, which is when ncmds can be 0, and the 3985 * queue can still have I/O's. This way we preserve order of biodone's. 3986 */ 3987 static void 3988 st_wait_for_io(struct scsi_tape *un) 3989 { 3990 ST_FUNC(ST_DEVINFO, st_wait_for_io); 3991 ASSERT(mutex_owned(ST_MUTEX)); 3992 while (un->un_ncmds && un->un_quef) { /* XXX fix for async write@EOM */ 3993 cv_wait(&un->un_queue_cv, ST_MUTEX); 3994 } 3995 } 3996 3997 /* 3998 * This routine implements the ioctl calls. It is called 3999 * from the device switch at normal priority. 4000 */ 4001 /*ARGSUSED*/ 4002 static int 4003 st_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p, 4004 int *rval_p) 4005 { 4006 int tmp, rval = 0; 4007 4008 GET_SOFT_STATE(dev); 4009 4010 ST_ENTR(ST_DEVINFO, st_ioctl); 4011 4012 mutex_enter(ST_MUTEX); 4013 4014 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 4015 "st_ioctl(): fileno=%x, blkno=%x, eof=%x, state = %d, " 4016 "pe_flag = %d\n", 4017 un->un_pos.fileno, un->un_pos.blkno, un->un_pos.eof, un->un_state, 4018 IS_PE_FLAG_SET(un)); 4019 4020 /* 4021 * We don't want to block on these, so let them through 4022 * and we don't care about setting driver states here. 4023 */ 4024 if ((cmd == MTIOCGETDRIVETYPE) || 4025 (cmd == MTIOCGUARANTEEDORDER) || 4026 (cmd == MTIOCPERSISTENTSTATUS)) { 4027 goto check_commands; 4028 } 4029 4030 /* 4031 * We clear error entry stack except command 4032 * MTIOCGETERROR and MTIOCGET 4033 */ 4034 if ((cmd != MTIOCGETERROR) && 4035 (cmd != MTIOCGET)) { 4036 st_empty_error_stack(un); 4037 } 4038 4039 /* 4040 * wait for all outstanding commands to complete, or be dequeued. 4041 * And because ioctl's are synchronous commands, any return value 4042 * after this, will be in order 4043 */ 4044 st_wait_for_io(un); 4045 4046 /* 4047 * allow only a through clear errors and persistent status, and 4048 * status 4049 */ 4050 if (IS_PE_FLAG_SET(un)) { 4051 if ((cmd == MTIOCLRERR) || 4052 (cmd == MTIOCPERSISTENT) || 4053 (cmd == MTIOCGET)) { 4054 goto check_commands; 4055 } else { 4056 rval = un->un_errno; 4057 goto exit; 4058 } 4059 } 4060 4061 un->un_throttle = 1; /* > 1 will never happen here */ 4062 un->un_errno = 0; /* start clean from here */ 4063 4064 /* 4065 * first and foremost, handle any ST_EOT_PENDING cases. 4066 * That is, if a logical eot is pending notice, notice it. 4067 */ 4068 if (un->un_pos.eof == ST_EOT_PENDING) { 4069 int resid = un->un_err_resid; 4070 uchar_t status = un->un_status; 4071 uchar_t lastop = un->un_lastop; 4072 4073 if (st_cmd(dev, SCMD_SPACE, Fmk((-1)), SYNC_CMD)) { 4074 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4075 "stioctl : EIO can't space fmk(-1)\n"); 4076 rval = EIO; 4077 goto exit; 4078 } 4079 un->un_lastop = lastop; /* restore last operation */ 4080 if (status == SUN_KEY_EOF) { 4081 un->un_status = SUN_KEY_EOT; 4082 } else { 4083 un->un_status = status; 4084 } 4085 un->un_err_resid = resid; 4086 /* fix up block number */ 4087 un->un_err_pos.blkno = un->un_pos.blkno = 0; 4088 /* now we're at logical eot */ 4089 un->un_pos.eof = ST_EOT; 4090 } 4091 4092 /* 4093 * now, handle the rest of the situations 4094 */ 4095 check_commands: 4096 switch (cmd) { 4097 case MTIOCGET: 4098 { 4099 #ifdef _MULTI_DATAMODEL 4100 /* 4101 * For use when a 32 bit app makes a call into a 4102 * 64 bit ioctl 4103 */ 4104 struct mtget32 mtg_local32; 4105 struct mtget32 *mtget_32 = &mtg_local32; 4106 #endif /* _MULTI_DATAMODEL */ 4107 4108 /* Get tape status */ 4109 struct mtget mtg_local; 4110 struct mtget *mtget = &mtg_local; 4111 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 4112 "st_ioctl: MTIOCGET\n"); 4113 4114 bzero((caddr_t)mtget, sizeof (struct mtget)); 4115 mtget->mt_erreg = un->un_status; 4116 mtget->mt_resid = un->un_err_resid; 4117 mtget->mt_dsreg = un->un_retry_ct; 4118 if (un->un_err_pos.pmode == legacy) { 4119 mtget->mt_fileno = un->un_err_pos.fileno; 4120 } else { 4121 mtget->mt_fileno = -1; 4122 } 4123 mtget->mt_blkno = un->un_err_pos.blkno; 4124 mtget->mt_type = un->un_dp->type; 4125 mtget->mt_flags = MTF_SCSI | MTF_ASF; 4126 if (un->un_read_pos_type != NO_POS) { 4127 mtget->mt_flags |= MTF_LOGICAL_BLOCK; 4128 } 4129 if (un->un_dp->options & ST_REEL) { 4130 mtget->mt_flags |= MTF_REEL; 4131 mtget->mt_bf = 20; 4132 } else { /* 1/4" cartridges */ 4133 switch (mtget->mt_type) { 4134 /* Emulex cartridge tape */ 4135 case MT_ISMT02: 4136 mtget->mt_bf = 40; 4137 break; 4138 default: 4139 mtget->mt_bf = 126; 4140 break; 4141 } 4142 } 4143 4144 /* 4145 * If large transfers are allowed and drive options 4146 * has no record size limit set. Calculate blocking 4147 * factor from the lesser of maxbsize and maxdma. 4148 */ 4149 if ((un->un_allow_large_xfer) && 4150 (un->un_dp->options & ST_NO_RECSIZE_LIMIT)) { 4151 mtget->mt_bf = min(un->un_maxbsize, 4152 un->un_maxdma) / SECSIZE; 4153 } 4154 4155 if (un->un_read_only == WORM || 4156 un->un_read_only == RDWORM) { 4157 mtget->mt_flags |= MTF_WORM_MEDIA; 4158 } 4159 4160 rval = st_check_clean_bit(dev); 4161 if (rval == -1) { 4162 rval = EIO; 4163 goto exit; 4164 } else { 4165 mtget->mt_flags |= (ushort_t)rval; 4166 rval = 0; 4167 } 4168 4169 un->un_status = 0; /* Reset status */ 4170 un->un_err_resid = 0; 4171 tmp = sizeof (struct mtget); 4172 4173 #ifdef _MULTI_DATAMODEL 4174 4175 switch (ddi_model_convert_from(flag & FMODELS)) { 4176 case DDI_MODEL_ILP32: 4177 /* 4178 * Convert 64 bit back to 32 bit before doing 4179 * copyout. This is what the ILP32 app expects. 4180 */ 4181 mtget_32->mt_erreg = mtget->mt_erreg; 4182 mtget_32->mt_resid = mtget->mt_resid; 4183 mtget_32->mt_dsreg = mtget->mt_dsreg; 4184 mtget_32->mt_fileno = (daddr32_t)mtget->mt_fileno; 4185 mtget_32->mt_blkno = (daddr32_t)mtget->mt_blkno; 4186 mtget_32->mt_type = mtget->mt_type; 4187 mtget_32->mt_flags = mtget->mt_flags; 4188 mtget_32->mt_bf = mtget->mt_bf; 4189 4190 if (ddi_copyout(mtget_32, (void *)arg, 4191 sizeof (struct mtget32), flag)) { 4192 rval = EFAULT; 4193 } 4194 break; 4195 4196 case DDI_MODEL_NONE: 4197 if (ddi_copyout(mtget, (void *)arg, tmp, flag)) { 4198 rval = EFAULT; 4199 } 4200 break; 4201 } 4202 #else /* ! _MULTI_DATAMODE */ 4203 if (ddi_copyout(mtget, (void *)arg, tmp, flag)) { 4204 rval = EFAULT; 4205 } 4206 #endif /* _MULTI_DATAMODE */ 4207 4208 break; 4209 } 4210 case MTIOCGETERROR: 4211 /* 4212 * get error entry from error stack 4213 */ 4214 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 4215 "st_ioctl: MTIOCGETERROR\n"); 4216 4217 rval = st_get_error_entry(un, arg, flag); 4218 4219 break; 4220 4221 case MTIOCSTATE: 4222 { 4223 /* 4224 * return when media presence matches state 4225 */ 4226 enum mtio_state state; 4227 4228 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 4229 "st_ioctl: MTIOCSTATE\n"); 4230 4231 if (ddi_copyin((void *)arg, &state, sizeof (int), flag)) 4232 rval = EFAULT; 4233 4234 mutex_exit(ST_MUTEX); 4235 4236 rval = st_check_media(dev, state); 4237 4238 mutex_enter(ST_MUTEX); 4239 4240 if (rval != 0) { 4241 break; 4242 } 4243 4244 if (ddi_copyout(&un->un_mediastate, (void *)arg, 4245 sizeof (int), flag)) 4246 rval = EFAULT; 4247 break; 4248 4249 } 4250 4251 case MTIOCGETDRIVETYPE: 4252 { 4253 #ifdef _MULTI_DATAMODEL 4254 /* 4255 * For use when a 32 bit app makes a call into a 4256 * 64 bit ioctl 4257 */ 4258 struct mtdrivetype_request32 mtdtrq32; 4259 #endif /* _MULTI_DATAMODEL */ 4260 4261 /* 4262 * return mtdrivetype 4263 */ 4264 struct mtdrivetype_request mtdtrq; 4265 struct mtdrivetype mtdrtyp; 4266 struct mtdrivetype *mtdt = &mtdrtyp; 4267 struct st_drivetype *stdt = un->un_dp; 4268 4269 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 4270 "st_ioctl: MTIOCGETDRIVETYPE\n"); 4271 4272 #ifdef _MULTI_DATAMODEL 4273 switch (ddi_model_convert_from(flag & FMODELS)) { 4274 case DDI_MODEL_ILP32: 4275 { 4276 if (ddi_copyin((void *)arg, &mtdtrq32, 4277 sizeof (struct mtdrivetype_request32), flag)) { 4278 rval = EFAULT; 4279 break; 4280 } 4281 mtdtrq.size = mtdtrq32.size; 4282 mtdtrq.mtdtp = 4283 (struct mtdrivetype *)(uintptr_t)mtdtrq32.mtdtp; 4284 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 4285 "st_ioctl: size 0x%x\n", mtdtrq.size); 4286 break; 4287 } 4288 case DDI_MODEL_NONE: 4289 if (ddi_copyin((void *)arg, &mtdtrq, 4290 sizeof (struct mtdrivetype_request), flag)) { 4291 rval = EFAULT; 4292 break; 4293 } 4294 break; 4295 } 4296 4297 #else /* ! _MULTI_DATAMODEL */ 4298 if (ddi_copyin((void *)arg, &mtdtrq, 4299 sizeof (struct mtdrivetype_request), flag)) { 4300 rval = EFAULT; 4301 break; 4302 } 4303 #endif /* _MULTI_DATAMODEL */ 4304 4305 /* 4306 * if requested size is < 0 then return 4307 * error. 4308 */ 4309 if (mtdtrq.size < 0) { 4310 rval = EINVAL; 4311 break; 4312 } 4313 bzero(mtdt, sizeof (struct mtdrivetype)); 4314 (void) strncpy(mtdt->name, stdt->name, ST_NAMESIZE); 4315 (void) strncpy(mtdt->vid, stdt->vid, VIDPIDLEN - 1); 4316 mtdt->type = stdt->type; 4317 mtdt->bsize = stdt->bsize; 4318 mtdt->options = stdt->options; 4319 mtdt->max_rretries = stdt->max_rretries; 4320 mtdt->max_wretries = stdt->max_wretries; 4321 for (tmp = 0; tmp < NDENSITIES; tmp++) { 4322 mtdt->densities[tmp] = stdt->densities[tmp]; 4323 } 4324 mtdt->default_density = stdt->default_density; 4325 /* 4326 * Speed hasn't been used since the hayday of reel tape. 4327 * For all drives not setting the option ST_KNOWS_MEDIA 4328 * the speed member renamed to mediatype are zeros. 4329 * Those drives that have ST_KNOWS_MEDIA set use the 4330 * new mediatype member which is used to figure the 4331 * type of media loaded. 4332 * 4333 * So as to not break applications speed in the 4334 * mtdrivetype structure is not renamed. 4335 */ 4336 for (tmp = 0; tmp < NDENSITIES; tmp++) { 4337 mtdt->speeds[tmp] = stdt->mediatype[tmp]; 4338 } 4339 mtdt->non_motion_timeout = stdt->non_motion_timeout; 4340 mtdt->io_timeout = stdt->io_timeout; 4341 mtdt->rewind_timeout = stdt->rewind_timeout; 4342 mtdt->space_timeout = stdt->space_timeout; 4343 mtdt->load_timeout = stdt->load_timeout; 4344 mtdt->unload_timeout = stdt->unload_timeout; 4345 mtdt->erase_timeout = stdt->erase_timeout; 4346 4347 /* 4348 * Limit the maximum length of the result to 4349 * sizeof (struct mtdrivetype). 4350 */ 4351 tmp = sizeof (struct mtdrivetype); 4352 if (mtdtrq.size < tmp) 4353 tmp = mtdtrq.size; 4354 if (ddi_copyout(mtdt, mtdtrq.mtdtp, tmp, flag)) { 4355 rval = EFAULT; 4356 } 4357 break; 4358 } 4359 case MTIOCPERSISTENT: 4360 { 4361 int persistence = 0; 4362 4363 if (ddi_copyin((void *)arg, &persistence, 4364 sizeof (int), flag)) { 4365 rval = EFAULT; 4366 break; 4367 } 4368 4369 /* non zero sets it, only 0 turns it off */ 4370 un->un_persistence = (uchar_t)persistence ? 1 : 0; 4371 4372 if (un->un_persistence) { 4373 TURN_PE_ON(un); 4374 } else { 4375 TURN_PE_OFF(un); 4376 } 4377 4378 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 4379 "st_ioctl: MTIOCPERSISTENT : persistence = %d\n", 4380 un->un_persistence); 4381 4382 break; 4383 } 4384 case MTIOCPERSISTENTSTATUS: 4385 { 4386 int persistence = (int)un->un_persistence; 4387 4388 if (ddi_copyout(&persistence, (void *)arg, 4389 sizeof (int), flag)) { 4390 rval = EFAULT; 4391 } 4392 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 4393 "st_ioctl: MTIOCPERSISTENTSTATUS:persistece = %d\n", 4394 un->un_persistence); 4395 4396 break; 4397 } 4398 4399 4400 case MTIOCLRERR: 4401 { 4402 /* clear persistent errors */ 4403 4404 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 4405 "st_ioctl: MTIOCLRERR\n"); 4406 4407 CLEAR_PE(un); 4408 4409 break; 4410 } 4411 4412 case MTIOCGUARANTEEDORDER: 4413 { 4414 /* 4415 * this is just a holder to make a valid ioctl and 4416 * it won't be in any earlier release 4417 */ 4418 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 4419 "st_ioctl: MTIOCGUARANTEEDORDER\n"); 4420 4421 break; 4422 } 4423 4424 case MTIOCRESERVE: 4425 { 4426 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 4427 "st_ioctl: MTIOCRESERVE\n"); 4428 4429 /* 4430 * Check if Reserve/Release is supported. 4431 */ 4432 if (un->un_dp->options & ST_NO_RESERVE_RELEASE) { 4433 rval = ENOTTY; 4434 break; 4435 } 4436 4437 rval = st_reserve_release(un, ST_RESERVE); 4438 4439 if (rval == 0) { 4440 un->un_rsvd_status |= ST_PRESERVE_RESERVE; 4441 } 4442 break; 4443 } 4444 4445 case MTIOCRELEASE: 4446 { 4447 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 4448 "st_ioctl: MTIOCRELEASE\n"); 4449 4450 /* 4451 * Check if Reserve/Release is supported. 4452 */ 4453 if (un->un_dp->options & ST_NO_RESERVE_RELEASE) { 4454 rval = ENOTTY; 4455 break; 4456 } 4457 4458 /* 4459 * Used to just clear ST_PRESERVE_RESERVE which 4460 * made the reservation release at next close. 4461 * As the user may have opened and then done a 4462 * persistant reservation we now need to drop 4463 * the reservation without closing if the user 4464 * attempts to do this. 4465 */ 4466 rval = st_reserve_release(un, ST_RELEASE); 4467 4468 un->un_rsvd_status &= ~ST_PRESERVE_RESERVE; 4469 4470 break; 4471 } 4472 4473 case MTIOCFORCERESERVE: 4474 { 4475 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 4476 "st_ioctl: MTIOCFORCERESERVE\n"); 4477 4478 /* 4479 * Check if Reserve/Release is supported. 4480 */ 4481 if (un->un_dp->options & ST_NO_RESERVE_RELEASE) { 4482 rval = ENOTTY; 4483 break; 4484 } 4485 /* 4486 * allow only super user to run this. 4487 */ 4488 if (drv_priv(cred_p) != 0) { 4489 rval = EPERM; 4490 break; 4491 } 4492 /* 4493 * Throw away reserve, 4494 * not using test-unit-ready 4495 * since reserve can succeed without tape being 4496 * present in the drive. 4497 */ 4498 (void) st_reserve_release(un, ST_RESERVE); 4499 4500 rval = st_take_ownership(dev); 4501 4502 break; 4503 } 4504 4505 case USCSICMD: 4506 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 4507 "st_ioctl: USCSICMD\n"); 4508 { 4509 cred_t *cr; 4510 cr = ddi_get_cred(); 4511 if ((drv_priv(cred_p) != 0) && (drv_priv(cr) != 0)) { 4512 rval = EPERM; 4513 } else { 4514 rval = st_ioctl_cmd(dev, (struct uscsi_cmd *)arg, 4515 flag); 4516 } 4517 } 4518 break; 4519 4520 case MTIOCTOP: 4521 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 4522 "st_ioctl: MTIOCTOP\n"); 4523 rval = st_mtioctop(un, arg, flag); 4524 break; 4525 4526 case MTIOCLTOP: 4527 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 4528 "st_ioctl: MTIOLCTOP\n"); 4529 rval = st_mtiocltop(un, arg, flag); 4530 break; 4531 4532 case MTIOCREADIGNOREILI: 4533 { 4534 int set_ili; 4535 4536 if (ddi_copyin((void *)arg, &set_ili, 4537 sizeof (set_ili), flag)) { 4538 rval = EFAULT; 4539 break; 4540 } 4541 4542 if (un->un_bsize) { 4543 rval = ENOTTY; 4544 break; 4545 } 4546 4547 switch (set_ili) { 4548 case 0: 4549 un->un_dp->options &= ~ST_READ_IGNORE_ILI; 4550 break; 4551 4552 case 1: 4553 un->un_dp->options |= ST_READ_IGNORE_ILI; 4554 break; 4555 4556 default: 4557 rval = EINVAL; 4558 break; 4559 } 4560 break; 4561 } 4562 4563 case MTIOCREADIGNOREEOFS: 4564 { 4565 int ignore_eof; 4566 4567 if (ddi_copyin((void *)arg, &ignore_eof, 4568 sizeof (ignore_eof), flag)) { 4569 rval = EFAULT; 4570 break; 4571 } 4572 4573 if (!(un->un_dp->options & ST_REEL)) { 4574 rval = ENOTTY; 4575 break; 4576 } 4577 4578 switch (ignore_eof) { 4579 case 0: 4580 un->un_dp->options &= ~ST_READ_IGNORE_EOFS; 4581 break; 4582 4583 case 1: 4584 un->un_dp->options |= ST_READ_IGNORE_EOFS; 4585 break; 4586 4587 default: 4588 rval = EINVAL; 4589 break; 4590 } 4591 break; 4592 } 4593 4594 case MTIOCSHORTFMK: 4595 { 4596 int short_fmk; 4597 4598 if (ddi_copyin((void *)arg, &short_fmk, 4599 sizeof (short_fmk), flag)) { 4600 rval = EFAULT; 4601 break; 4602 } 4603 4604 switch (un->un_dp->type) { 4605 case ST_TYPE_EXB8500: 4606 case ST_TYPE_EXABYTE: 4607 if (!short_fmk) { 4608 un->un_dp->options &= ~ST_SHORT_FILEMARKS; 4609 } else if (short_fmk == 1) { 4610 un->un_dp->options |= ST_SHORT_FILEMARKS; 4611 } else { 4612 rval = EINVAL; 4613 } 4614 break; 4615 4616 default: 4617 rval = ENOTTY; 4618 break; 4619 } 4620 break; 4621 } 4622 4623 case MTIOCGETPOS: 4624 rval = st_update_block_pos(un); 4625 if (rval == 0) { 4626 if (ddi_copyout((void *)&un->un_pos, (void *)arg, 4627 sizeof (tapepos_t), flag)) { 4628 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 4629 "MTIOCGETPOS copy out failed\n"); 4630 rval = EFAULT; 4631 } 4632 } 4633 break; 4634 4635 case MTIOCRESTPOS: 4636 { 4637 tapepos_t dest; 4638 4639 if (ddi_copyin((void *)arg, &dest, sizeof (tapepos_t), 4640 flag) != 0) { 4641 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 4642 "MTIOCRESTPOS copy in failed\n"); 4643 rval = EFAULT; 4644 break; 4645 } 4646 rval = st_validate_tapemarks(un, &dest); 4647 if (rval != 0) { 4648 rval = EIO; 4649 } 4650 break; 4651 } 4652 default: 4653 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 4654 "st_ioctl: unknown ioctl\n"); 4655 rval = ENOTTY; 4656 } 4657 4658 exit: 4659 if (!IS_PE_FLAG_SET(un)) { 4660 un->un_errno = rval; 4661 } 4662 4663 mutex_exit(ST_MUTEX); 4664 4665 return (rval); 4666 } 4667 4668 4669 /* 4670 * do some MTIOCTOP tape operations 4671 */ 4672 static int 4673 st_mtioctop(struct scsi_tape *un, intptr_t arg, int flag) 4674 { 4675 #ifdef _MULTI_DATAMODEL 4676 /* 4677 * For use when a 32 bit app makes a call into a 4678 * 64 bit ioctl 4679 */ 4680 struct mtop32 mtop_32_for_64; 4681 #endif /* _MULTI_DATAMODEL */ 4682 struct mtop passed; 4683 struct mtlop local; 4684 int rval = 0; 4685 4686 ST_FUNC(ST_DEVINFO, st_mtioctop); 4687 4688 ASSERT(mutex_owned(ST_MUTEX)); 4689 4690 #ifdef _MULTI_DATAMODEL 4691 switch (ddi_model_convert_from(flag & FMODELS)) { 4692 case DDI_MODEL_ILP32: 4693 if (ddi_copyin((void *)arg, &mtop_32_for_64, 4694 sizeof (struct mtop32), flag)) { 4695 return (EFAULT); 4696 } 4697 local.mt_op = mtop_32_for_64.mt_op; 4698 local.mt_count = (int64_t)mtop_32_for_64.mt_count; 4699 break; 4700 4701 case DDI_MODEL_NONE: 4702 if (ddi_copyin((void *)arg, &passed, sizeof (passed), flag)) { 4703 return (EFAULT); 4704 } 4705 local.mt_op = passed.mt_op; 4706 /* prevent sign extention */ 4707 local.mt_count = (UINT32_MAX & passed.mt_count); 4708 break; 4709 } 4710 4711 #else /* ! _MULTI_DATAMODEL */ 4712 if (ddi_copyin((void *)arg, &passed, sizeof (passed), flag)) { 4713 return (EFAULT); 4714 } 4715 local.mt_op = passed.mt_op; 4716 /* prevent sign extention */ 4717 local.mt_count = (UINT32_MAX & passed.mt_count); 4718 #endif /* _MULTI_DATAMODEL */ 4719 4720 rval = st_do_mtioctop(un, &local); 4721 4722 #ifdef _MULTI_DATAMODEL 4723 switch (ddi_model_convert_from(flag & FMODELS)) { 4724 case DDI_MODEL_ILP32: 4725 if (((uint64_t)local.mt_count) > UINT32_MAX) { 4726 rval = ERANGE; 4727 break; 4728 } 4729 /* 4730 * Convert 64 bit back to 32 bit before doing 4731 * copyout. This is what the ILP32 app expects. 4732 */ 4733 mtop_32_for_64.mt_op = local.mt_op; 4734 mtop_32_for_64.mt_count = local.mt_count; 4735 4736 if (ddi_copyout(&mtop_32_for_64, (void *)arg, 4737 sizeof (struct mtop32), flag)) { 4738 rval = EFAULT; 4739 } 4740 break; 4741 4742 case DDI_MODEL_NONE: 4743 passed.mt_count = local.mt_count; 4744 passed.mt_op = local.mt_op; 4745 if (ddi_copyout(&passed, (void *)arg, sizeof (passed), flag)) { 4746 rval = EFAULT; 4747 } 4748 break; 4749 } 4750 #else /* ! _MULTI_DATAMODE */ 4751 if (((uint64_t)local.mt_count) > UINT32_MAX) { 4752 rval = ERANGE; 4753 } else { 4754 passed.mt_op = local.mt_op; 4755 passed.mt_count = local.mt_count; 4756 if (ddi_copyout(&passed, (void *)arg, sizeof (passed), flag)) { 4757 rval = EFAULT; 4758 } 4759 } 4760 #endif /* _MULTI_DATAMODE */ 4761 4762 4763 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 4764 "st_ioctl: fileno=%x, blkno=%x, eof=%x\n", un->un_pos.fileno, 4765 un->un_pos.blkno, un->un_pos.eof); 4766 4767 if (un->un_pos.pmode == invalid) { 4768 un->un_density_known = 0; 4769 } 4770 4771 ASSERT(mutex_owned(ST_MUTEX)); 4772 return (rval); 4773 } 4774 4775 static int 4776 st_mtiocltop(struct scsi_tape *un, intptr_t arg, int flag) 4777 { 4778 struct mtlop local; 4779 int rval; 4780 4781 ST_FUNC(ST_DEVINFO, st_mtiocltop); 4782 if (ddi_copyin((void *)arg, &local, sizeof (local), flag)) { 4783 return (EFAULT); 4784 } 4785 4786 rval = st_do_mtioctop(un, &local); 4787 4788 if (ddi_copyout(&local, (void *)arg, sizeof (local), flag)) { 4789 rval = EFAULT; 4790 } 4791 return (rval); 4792 } 4793 4794 4795 static int 4796 st_do_mtioctop(struct scsi_tape *un, struct mtlop *mtop) 4797 { 4798 dev_t dev = un->un_dev; 4799 int savefile; 4800 int rval = 0; 4801 4802 ST_FUNC(ST_DEVINFO, st_do_mtioctop); 4803 4804 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 4805 "st_do_mtioctop(): mt_op=%x\n", mtop->mt_op); 4806 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 4807 "fileno=%x, blkno=%x, eof=%x\n", 4808 un->un_pos.fileno, un->un_pos.blkno, un->un_pos.eof); 4809 4810 un->un_status = 0; 4811 4812 /* 4813 * if we are going to mess with a tape, we have to make sure we have 4814 * one and are not offline (i.e. no tape is initialized). We let 4815 * commands pass here that don't actually touch the tape, except for 4816 * loading and initialization (rewinding). 4817 */ 4818 if (un->un_state == ST_STATE_OFFLINE) { 4819 switch (mtop->mt_op) { 4820 case MTLOAD: 4821 case MTNOP: 4822 /* 4823 * We don't want strategy calling st_tape_init here, 4824 * so, change state 4825 */ 4826 un->un_state = ST_STATE_INITIALIZING; 4827 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4828 "st_do_mtioctop : OFFLINE state = %d\n", 4829 un->un_state); 4830 break; 4831 default: 4832 /* 4833 * reinitialize by normal means 4834 */ 4835 rval = st_tape_init(dev); 4836 if (rval) { 4837 un->un_state = ST_STATE_INITIALIZING; 4838 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4839 "st_do_mtioctop : OFFLINE init failure "); 4840 un->un_state = ST_STATE_OFFLINE; 4841 un->un_pos.pmode = invalid; 4842 if (rval != EACCES) { 4843 rval = EIO; 4844 } 4845 return (rval); 4846 } 4847 un->un_state = ST_STATE_OPEN_PENDING_IO; 4848 break; 4849 } 4850 } 4851 4852 /* 4853 * If the file position is invalid, allow only those 4854 * commands that properly position the tape and fail 4855 * the rest with EIO 4856 */ 4857 if (un->un_pos.pmode == invalid) { 4858 switch (mtop->mt_op) { 4859 case MTWEOF: 4860 case MTRETEN: 4861 case MTERASE: 4862 case MTEOM: 4863 case MTFSF: 4864 case MTFSR: 4865 case MTBSF: 4866 case MTNBSF: 4867 case MTBSR: 4868 case MTSRSZ: 4869 case MTGRSZ: 4870 case MTSEEK: 4871 case MTBSSF: 4872 case MTFSSF: 4873 return (EIO); 4874 /* NOTREACHED */ 4875 case MTREW: 4876 case MTLOAD: 4877 case MTOFFL: 4878 case MTNOP: 4879 case MTTELL: 4880 case MTLOCK: 4881 case MTUNLOCK: 4882 break; 4883 4884 default: 4885 return (ENOTTY); 4886 /* NOTREACHED */ 4887 } 4888 } 4889 4890 switch (mtop->mt_op) { 4891 case MTERASE: 4892 /* 4893 * MTERASE rewinds the tape, erase it completely, and returns 4894 * to the beginning of the tape 4895 */ 4896 if (un->un_mspl->wp || un->un_read_only & WORM) { 4897 un->un_status = KEY_WRITE_PROTECT; 4898 un->un_err_resid = mtop->mt_count; 4899 COPY_POS(&un->un_err_pos, &un->un_pos); 4900 return (EACCES); 4901 } 4902 if (un->un_dp->options & ST_REEL) { 4903 un->un_fmneeded = 2; 4904 } else { 4905 un->un_fmneeded = 1; 4906 } 4907 if (st_check_density_or_wfm(dev, 1, B_WRITE, NO_STEPBACK) || 4908 st_cmd(dev, SCMD_REWIND, 0, SYNC_CMD) || 4909 st_cmd(dev, SCMD_ERASE, 0, SYNC_CMD)) { 4910 un->un_pos.pmode = invalid; 4911 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4912 "st_do_mtioctop : EIO space or erase or " 4913 "check den)\n"); 4914 rval = EIO; 4915 } else { 4916 /* QIC and helical scan rewind after erase */ 4917 if (un->un_dp->options & ST_REEL) { 4918 (void) st_cmd(dev, SCMD_REWIND, 0, ASYNC_CMD); 4919 } 4920 } 4921 break; 4922 4923 case MTWEOF: 4924 /* 4925 * write an end-of-file record 4926 */ 4927 if (un->un_mspl->wp || un->un_read_only & RDONLY) { 4928 un->un_status = KEY_WRITE_PROTECT; 4929 un->un_err_resid = mtop->mt_count; 4930 COPY_POS(&un->un_err_pos, &un->un_pos); 4931 return (EACCES); 4932 } 4933 4934 /* 4935 * zero count means just flush buffers 4936 * negative count is not permitted 4937 */ 4938 if (mtop->mt_count < 0) { 4939 return (EINVAL); 4940 } 4941 4942 /* Not on worm */ 4943 if (un->un_read_only == RDWR) { 4944 un->un_test_append = 1; 4945 } 4946 4947 if (un->un_state == ST_STATE_OPEN_PENDING_IO) { 4948 if (st_determine_density(dev, B_WRITE)) { 4949 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4950 "st_do_mtioctop : EIO : MTWEOF can't " 4951 "determine density"); 4952 return (EIO); 4953 } 4954 } 4955 4956 rval = st_write_fm(dev, (int)mtop->mt_count); 4957 if ((rval != 0) && (rval != EACCES)) { 4958 /* 4959 * Failure due to something other than illegal 4960 * request results in loss of state (st_intr). 4961 */ 4962 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4963 "st_do_mtioctop : EIO : MTWEOF can't write " 4964 "file mark"); 4965 rval = EIO; 4966 } 4967 break; 4968 4969 case MTRETEN: 4970 /* 4971 * retension the tape 4972 */ 4973 if (st_check_density_or_wfm(dev, 1, 0, NO_STEPBACK) || 4974 st_cmd(dev, SCMD_LOAD, LD_LOAD | LD_RETEN, SYNC_CMD)) { 4975 un->un_pos.pmode = invalid; 4976 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4977 "st_do_mtioctop : EIO : MTRETEN "); 4978 rval = EIO; 4979 } 4980 break; 4981 4982 case MTREW: 4983 /* 4984 * rewind the tape 4985 */ 4986 if (st_check_density_or_wfm(dev, 1, 0, NO_STEPBACK)) { 4987 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4988 "st_do_mtioctop : EIO:MTREW check " 4989 "density/wfm failed"); 4990 return (EIO); 4991 } 4992 if (st_cmd(dev, SCMD_REWIND, 0, SYNC_CMD)) { 4993 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 4994 "st_do_mtioctop : EIO : MTREW "); 4995 rval = EIO; 4996 } 4997 break; 4998 4999 case MTOFFL: 5000 /* 5001 * rewinds, and, if appropriate, takes the device offline by 5002 * unloading the tape 5003 */ 5004 if (st_check_density_or_wfm(dev, 1, 0, NO_STEPBACK)) { 5005 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5006 "st_do_mtioctop :EIO:MTOFFL check " 5007 "density/wfm failed"); 5008 return (EIO); 5009 } 5010 (void) st_cmd(dev, SCMD_REWIND, 0, SYNC_CMD); 5011 if (st_cmd(dev, SCMD_LOAD, LD_UNLOAD, SYNC_CMD)) { 5012 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5013 "st_do_mtioctop : EIO : MTOFFL"); 5014 return (EIO); 5015 } 5016 un->un_pos.eof = ST_NO_EOF; 5017 un->un_laststate = un->un_state; 5018 un->un_state = ST_STATE_OFFLINE; 5019 un->un_mediastate = MTIO_EJECTED; 5020 break; 5021 5022 case MTLOAD: 5023 /* 5024 * This is to load a tape into the drive 5025 * Note that if the tape is not loaded, the device will have 5026 * to be opened via O_NDELAY or O_NONBLOCK. 5027 */ 5028 /* 5029 * Let's try and clean things up, if we are not 5030 * initializing, and then send in the load command, no 5031 * matter what. 5032 * 5033 * load after a media change by the user. 5034 */ 5035 5036 if (un->un_state > ST_STATE_INITIALIZING) { 5037 (void) st_check_density_or_wfm(dev, 1, 0, NO_STEPBACK); 5038 } 5039 rval = st_cmd(dev, SCMD_LOAD, LD_LOAD, SYNC_CMD); 5040 /* Load command to a drive that doesn't support load */ 5041 if ((rval == EIO) && 5042 ((un->un_status == KEY_NOT_READY) && 5043 /* Medium not present */ 5044 (un->un_uscsi_rqs_buf->es_add_code == 0x3a) || 5045 ((un->un_status == KEY_ILLEGAL_REQUEST) && 5046 (un->un_dp->type == MT_ISSTK9840) && 5047 /* CSL not present */ 5048 (un->un_uscsi_rqs_buf->es_add_code == 0x80)))) { 5049 rval = ENOTTY; 5050 break; 5051 } else if (rval != EACCES) { 5052 rval = EIO; 5053 } 5054 if (rval) { 5055 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5056 "st_do_mtioctop : %s : MTLOAD\n", 5057 rval == EACCES ? "EACCES" : "EIO"); 5058 /* 5059 * If load tape fails, who knows what happened... 5060 */ 5061 un->un_pos.pmode = invalid; 5062 break; 5063 } 5064 5065 /* 5066 * reset all counters appropriately using rewind, as if LOAD 5067 * succeeds, we are at BOT 5068 */ 5069 un->un_state = ST_STATE_INITIALIZING; 5070 5071 rval = st_tape_init(dev); 5072 if ((rval == EACCES) && (un->un_read_only & WORM)) { 5073 rval = 0; 5074 break; 5075 } 5076 5077 if (rval != 0) { 5078 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5079 "st_do_mtioctop : EIO : MTLOAD calls " 5080 "st_tape_init\n"); 5081 rval = EIO; 5082 un->un_state = ST_STATE_OFFLINE; 5083 } 5084 5085 break; 5086 5087 case MTNOP: 5088 un->un_status = 0; /* Reset status */ 5089 un->un_err_resid = 0; 5090 mtop->mt_count = MTUNIT(dev); 5091 break; 5092 5093 case MTEOM: 5094 /* 5095 * positions the tape at a location just after the last file 5096 * written on the tape. For cartridge and 8 mm, this after 5097 * the last file mark; for reel, this is inbetween the two 5098 * last 2 file marks 5099 */ 5100 if ((un->un_pos.pmode == legacy && un->un_pos.eof >= ST_EOT) || 5101 (un->un_lastop == ST_OP_WRITE) || 5102 (un->un_lastop == ST_OP_WEOF)) { 5103 /* 5104 * If the command wants to move to logical end 5105 * of media, and we're already there, we're done. 5106 * If we were at logical eot, we reset the state 5107 * to be *not* at logical eot. 5108 * 5109 * If we're at physical or logical eot, we prohibit 5110 * forward space operations (unconditionally). 5111 * 5112 * Also if the last operation was a write of any 5113 * kind the tape is at EOD. 5114 */ 5115 return (0); 5116 } 5117 /* 5118 * physical tape position may not be what we've been 5119 * telling the user; adjust the request accordingly 5120 */ 5121 if (IN_EOF(un->un_pos)) { 5122 un->un_pos.fileno++; 5123 un->un_pos.blkno = 0; 5124 } 5125 5126 if (st_check_density_or_wfm(dev, 1, B_READ, NO_STEPBACK)) { 5127 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5128 "st_do_mtioctop : EIO:MTEOM check density/wfm " 5129 " failed"); 5130 return (EIO); 5131 } 5132 5133 /* 5134 * st_find_eod() returns the last fileno we knew about; 5135 */ 5136 savefile = st_find_eod(dev); 5137 5138 if ((un->un_status != KEY_BLANK_CHECK) && 5139 (un->un_status != SUN_KEY_EOT)) { 5140 un->un_pos.pmode = invalid; 5141 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5142 "st_do_mtioctop : EIO : MTEOM status check failed"); 5143 rval = EIO; 5144 } else { 5145 /* 5146 * For 1/2" reel tapes assume logical EOT marked 5147 * by two file marks or we don't care that we may 5148 * be extending the last file on the tape. 5149 */ 5150 if (un->un_dp->options & ST_REEL) { 5151 if (st_cmd(dev, SCMD_SPACE, Fmk((-1)), 5152 SYNC_CMD)) { 5153 un->un_pos.pmode = invalid; 5154 ST_DEBUG2(ST_DEVINFO, st_label, 5155 SCSI_DEBUG, 5156 "st_do_mtioctop : EIO : MTEOM space" 5157 " cmd failed"); 5158 rval = EIO; 5159 break; 5160 } 5161 /* 5162 * Fix up the block number. 5163 */ 5164 un->un_pos.blkno = 0; 5165 un->un_err_pos.blkno = 0; 5166 } 5167 un->un_err_resid = 0; 5168 un->un_pos.fileno = savefile; 5169 un->un_pos.eof = ST_EOT; 5170 } 5171 un->un_status = 0; 5172 break; 5173 5174 case MTFSF: 5175 rval = st_mtfsf_ioctl(un, mtop->mt_count); 5176 break; 5177 5178 case MTFSR: 5179 rval = st_mtfsr_ioctl(un, mtop->mt_count); 5180 break; 5181 5182 case MTBSF: 5183 rval = st_mtbsf_ioctl(un, mtop->mt_count); 5184 break; 5185 5186 case MTNBSF: 5187 rval = st_mtnbsf_ioctl(un, mtop->mt_count); 5188 break; 5189 5190 case MTBSR: 5191 rval = st_mtbsr_ioctl(un, mtop->mt_count); 5192 break; 5193 5194 case MTBSSF: 5195 rval = st_mtbsfm_ioctl(un, mtop->mt_count); 5196 break; 5197 5198 case MTFSSF: 5199 rval = st_mtfsfm_ioctl(un, mtop->mt_count); 5200 break; 5201 5202 case MTSRSZ: 5203 5204 /* 5205 * Set record-size to that sent by user 5206 * Check to see if there is reason that the requested 5207 * block size should not be set. 5208 */ 5209 5210 /* If requesting variable block size is it ok? */ 5211 if ((mtop->mt_count == 0) && 5212 ((un->un_dp->options & ST_VARIABLE) == 0)) { 5213 return (ENOTTY); 5214 } 5215 5216 /* 5217 * If requested block size is not variable "0", 5218 * is it less then minimum. 5219 */ 5220 if ((mtop->mt_count != 0) && 5221 (mtop->mt_count < un->un_minbsize)) { 5222 return (EINVAL); 5223 } 5224 5225 /* Is the requested block size more then maximum */ 5226 if ((mtop->mt_count > min(un->un_maxbsize, un->un_maxdma)) && 5227 (un->un_maxbsize != 0)) { 5228 return (EINVAL); 5229 } 5230 5231 /* Is requested block size a modulus the device likes */ 5232 if ((mtop->mt_count % un->un_data_mod) != 0) { 5233 return (EINVAL); 5234 } 5235 5236 if (st_change_block_size(dev, (uint32_t)mtop->mt_count) != 0) { 5237 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5238 "st_ioctl : MTSRSZ : EIO : cant set block size"); 5239 return (EIO); 5240 } 5241 5242 return (0); 5243 5244 case MTGRSZ: 5245 /* 5246 * Get record-size to the user 5247 */ 5248 mtop->mt_count = un->un_bsize; 5249 rval = 0; 5250 break; 5251 5252 case MTTELL: 5253 rval = st_update_block_pos(un); 5254 mtop->mt_count = un->un_pos.lgclblkno; 5255 break; 5256 5257 case MTSEEK: 5258 rval = st_logical_block_locate(un, (uint64_t)mtop->mt_count, 5259 un->un_pos.partition); 5260 /* 5261 * This bit of magic make mt print the actual position if 5262 * the resulting position was not what was asked for. 5263 */ 5264 if (rval == ESPIPE) { 5265 rval = EIO; 5266 if ((uint64_t)mtop->mt_count != un->un_pos.lgclblkno) { 5267 mtop->mt_op = MTTELL; 5268 mtop->mt_count = un->un_pos.lgclblkno; 5269 } 5270 } 5271 break; 5272 5273 case MTLOCK: 5274 if (st_cmd(dev, SCMD_DOORLOCK, MR_LOCK, SYNC_CMD)) { 5275 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5276 "st_do_mtioctop : EIO : MTLOCK"); 5277 rval = EIO; 5278 } 5279 break; 5280 5281 case MTUNLOCK: 5282 if (st_cmd(dev, SCMD_DOORLOCK, MR_UNLOCK, SYNC_CMD)) { 5283 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5284 "st_do_mtioctop : EIO : MTUNLOCK"); 5285 rval = EIO; 5286 } 5287 break; 5288 5289 default: 5290 rval = ENOTTY; 5291 } 5292 5293 return (rval); 5294 } 5295 5296 5297 /* 5298 * Run a command for uscsi ioctl. 5299 */ 5300 static int 5301 st_ioctl_cmd(dev_t dev, struct uscsi_cmd *ucmd, int flag) 5302 { 5303 struct uscsi_cmd *uscmd; 5304 struct buf *bp; 5305 enum uio_seg uioseg; 5306 int offline_state = 0; 5307 int err = 0; 5308 5309 GET_SOFT_STATE(dev); 5310 5311 ST_FUNC(ST_DEVINFO, st_ioctl_cmd); 5312 5313 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 5314 "st_ioctl_cmd(dev = 0x%lx)\n", dev); 5315 5316 ASSERT(mutex_owned(ST_MUTEX)); 5317 5318 /* 5319 * We really don't know what commands are coming in here and 5320 * we don't want to limit the commands coming in. 5321 * 5322 * If st_tape_init() gets called from st_strategy(), then we 5323 * will hang the process waiting for un->un_sbuf_busy to be cleared, 5324 * which it never will, as we set it below. To prevent 5325 * st_tape_init() from getting called, we have to set state to other 5326 * than ST_STATE_OFFLINE, so we choose ST_STATE_INITIALIZING, which 5327 * achieves this purpose already. 5328 * 5329 * We use offline_state to preserve the OFFLINE state, if it exists, 5330 * so other entry points to the driver might have the chance to call 5331 * st_tape_init(). 5332 */ 5333 if (un->un_state == ST_STATE_OFFLINE) { 5334 un->un_laststate = ST_STATE_OFFLINE; 5335 un->un_state = ST_STATE_INITIALIZING; 5336 offline_state = 1; 5337 } 5338 5339 mutex_exit(ST_MUTEX); 5340 err = scsi_uscsi_alloc_and_copyin((intptr_t)ucmd, flag, 5341 ROUTE, &uscmd); 5342 mutex_enter(ST_MUTEX); 5343 if (err != 0) { 5344 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5345 "st_ioctl_cmd: scsi_uscsi_alloc_and_copyin failed\n"); 5346 goto exit; 5347 } 5348 5349 uioseg = (flag & FKIOCTL) ? UIO_SYSSPACE : UIO_USERSPACE; 5350 5351 /* check to see if this command requires the drive to be reserved */ 5352 if (uscmd->uscsi_cdb != NULL) { 5353 err = st_check_cdb_for_need_to_reserve(un, 5354 &((char *)uscmd->uscsi_cdb)[0]); 5355 if (err) { 5356 goto exit_free; 5357 } 5358 } 5359 5360 /* 5361 * Get buffer resources... 5362 */ 5363 while (un->un_sbuf_busy) 5364 cv_wait(&un->un_sbuf_cv, ST_MUTEX); 5365 un->un_sbuf_busy = 1; 5366 5367 #ifdef STDEBUG 5368 if ((uscmd->uscsi_cdb != NULL) && (st_debug & 0xf) > 6) { 5369 int rw = (uscmd->uscsi_flags & USCSI_READ) ? B_READ : B_WRITE; 5370 st_print_cdb(ST_DEVINFO, st_label, SCSI_DEBUG, 5371 "uscsi cdb", uscmd->uscsi_cdb); 5372 if (uscmd->uscsi_buflen) { 5373 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 5374 "uscsi %s of %ld bytes %s %s space\n", 5375 (rw == B_READ) ? rd_str : wr_str, 5376 uscmd->uscsi_buflen, 5377 (rw == B_READ) ? "to" : "from", 5378 (uioseg == UIO_SYSSPACE) ? "system" : "user"); 5379 } 5380 } 5381 #endif /* ST_DEBUG */ 5382 5383 /* 5384 * Although st_ioctl_cmd() never makes use of these 5385 * now, we are just being safe and consistent. 5386 */ 5387 uscmd->uscsi_flags &= ~(USCSI_NOINTR | USCSI_NOPARITY | 5388 USCSI_OTAG | USCSI_HTAG | USCSI_HEAD); 5389 5390 un->un_srqbufp = uscmd->uscsi_rqbuf; 5391 bp = un->un_sbufp; 5392 bzero(bp, sizeof (buf_t)); 5393 if (uscmd->uscsi_cdb != NULL) { 5394 bp->b_forw = 5395 (struct buf *)(uintptr_t)((char *)uscmd->uscsi_cdb)[0]; 5396 bp->b_back = (struct buf *)uscmd; 5397 } 5398 5399 mutex_exit(ST_MUTEX); 5400 err = scsi_uscsi_handle_cmd(dev, uioseg, uscmd, 5401 st_strategy, bp, NULL); 5402 mutex_enter(ST_MUTEX); 5403 5404 /* 5405 * If scsi reset successful, don't write any filemarks. 5406 */ 5407 if ((err == 0) && (uscmd->uscsi_flags & 5408 (USCSI_RESET_LUN | USCSI_RESET_TARGET | USCSI_RESET_ALL))) { 5409 un->un_fmneeded = 0; 5410 } 5411 5412 exit_free: 5413 /* 5414 * Free resources 5415 */ 5416 un->un_sbuf_busy = 0; 5417 un->un_srqbufp = NULL; 5418 5419 /* 5420 * If was a space command need to update logical block position. 5421 * If the command failed such that positioning is invalid, Don't 5422 * update the position as the user must do this to validate the 5423 * position for data protection. 5424 */ 5425 if ((uscmd->uscsi_cdb != NULL) && 5426 (uscmd->uscsi_cdb[0] == SCMD_SPACE) && 5427 (un->un_pos.pmode != invalid)) { 5428 uchar_t status = un->un_status; 5429 (void) st_update_block_pos(un); 5430 un->un_status = status; 5431 } 5432 cv_signal(&un->un_sbuf_cv); 5433 mutex_exit(ST_MUTEX); 5434 (void) scsi_uscsi_copyout_and_free((intptr_t)ucmd, uscmd); 5435 mutex_enter(ST_MUTEX); 5436 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 5437 "st_ioctl_cmd returns 0x%x\n", err); 5438 5439 exit: 5440 /* don't lose offline state */ 5441 if (offline_state) { 5442 un->un_state = ST_STATE_OFFLINE; 5443 } 5444 5445 ASSERT(mutex_owned(ST_MUTEX)); 5446 return (err); 5447 } 5448 5449 static int 5450 st_write_fm(dev_t dev, int wfm) 5451 { 5452 int i; 5453 int rval; 5454 5455 GET_SOFT_STATE(dev); 5456 5457 ST_FUNC(ST_DEVINFO, st_write_fm); 5458 5459 ASSERT(mutex_owned(ST_MUTEX)); 5460 5461 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 5462 "st_write_fm(dev = 0x%lx, wfm = %d)\n", dev, wfm); 5463 5464 /* 5465 * write one filemark at the time after EOT 5466 */ 5467 if (un->un_pos.eof >= ST_EOT) { 5468 for (i = 0; i < wfm; i++) { 5469 rval = st_cmd(dev, SCMD_WRITE_FILE_MARK, 1, SYNC_CMD); 5470 if (rval == EACCES) { 5471 return (rval); 5472 } 5473 if (rval != 0) { 5474 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5475 "st_write_fm : EIO : write EOT file mark"); 5476 return (EIO); 5477 } 5478 } 5479 } else { 5480 rval = st_cmd(dev, SCMD_WRITE_FILE_MARK, wfm, SYNC_CMD); 5481 if (rval == EACCES) { 5482 return (rval); 5483 } 5484 if (rval) { 5485 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5486 "st_write_fm : EIO : write file mark"); 5487 return (EIO); 5488 } 5489 } 5490 5491 ASSERT(mutex_owned(ST_MUTEX)); 5492 return (0); 5493 } 5494 5495 #ifdef STDEBUG 5496 static void 5497 start_dump(struct scsi_tape *un, struct buf *bp) 5498 { 5499 struct scsi_pkt *pkt = BP_PKT(bp); 5500 uchar_t *cdbp = (uchar_t *)pkt->pkt_cdbp; 5501 5502 ST_FUNC(ST_DEVINFO, start_dump); 5503 5504 if ((st_debug & 0xf) < 6) 5505 return; 5506 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 5507 "st_start: cmd=0x%p count=%ld resid=%ld flags=0x%x pkt=0x%p\n", 5508 (void *)bp->b_forw, bp->b_bcount, 5509 bp->b_resid, bp->b_flags, (void *)BP_PKT(bp)); 5510 5511 st_print_cdb(ST_DEVINFO, st_label, SCSI_DEBUG, 5512 "st_start: cdb", (caddr_t)cdbp); 5513 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 5514 "st_start: fileno=%d, blk=%d\n", 5515 un->un_pos.fileno, un->un_pos.blkno); 5516 } 5517 #endif 5518 5519 5520 /* 5521 * Command start && done functions 5522 */ 5523 5524 /* 5525 * st_start() 5526 * 5527 * Called from: 5528 * st_strategy() to start a command. 5529 * st_runout() to retry when scsi_pkt allocation fails on previous attempt(s). 5530 * st_attach() when resuming from power down state. 5531 * st_start_restart() to retry transport when device was previously busy. 5532 * st_done_and_mutex_exit() to start the next command when previous is done. 5533 * 5534 * On entry: 5535 * scsi_pkt may or may not be allocated. 5536 * 5537 */ 5538 static void 5539 st_start(struct scsi_tape *un) 5540 { 5541 struct buf *bp; 5542 int status; 5543 5544 ST_FUNC(ST_DEVINFO, st_start); 5545 ASSERT(mutex_owned(ST_MUTEX)); 5546 5547 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 5548 "st_start(): dev = 0x%lx\n", un->un_dev); 5549 5550 if ((bp = un->un_quef) == NULL) { 5551 return; 5552 } 5553 5554 ASSERT((bp->b_flags & B_DONE) == 0); 5555 5556 /* 5557 * Don't send more than un_throttle commands to the HBA 5558 */ 5559 if ((un->un_throttle <= 0) || (un->un_ncmds >= un->un_throttle)) { 5560 return; 5561 } 5562 5563 /* 5564 * If the buf has no scsi_pkt call st_make_cmd() to get one and 5565 * build the command. 5566 */ 5567 if (BP_PKT(bp) == NULL) { 5568 ASSERT((bp->b_flags & B_DONE) == 0); 5569 st_make_cmd(un, bp, st_runout); 5570 ASSERT((bp->b_flags & B_DONE) == 0); 5571 status = geterror(bp); 5572 5573 /* 5574 * Some HBA's don't call bioerror() to set an error. 5575 * And geterror() returns zero if B_ERROR is not set. 5576 * So if we get zero we must check b_error. 5577 */ 5578 if (status == 0 && bp->b_error != 0) { 5579 status = bp->b_error; 5580 bioerror(bp, status); 5581 } 5582 5583 /* 5584 * Some HBA's convert DDI_DMA_NORESOURCES into ENOMEM. 5585 * In tape ENOMEM has special meaning so we'll change it. 5586 */ 5587 if (status == ENOMEM) { 5588 status = 0; 5589 bioerror(bp, status); 5590 } 5591 5592 /* 5593 * Did it fail and is it retryable? 5594 * If so return and wait for the callback through st_runout. 5595 * Also looks like scsi_init_pkt() will setup a callback even 5596 * if it isn't retryable. 5597 */ 5598 if (BP_PKT(bp) == NULL) { 5599 if (status == 0) { 5600 /* 5601 * If first attempt save state. 5602 */ 5603 if (un->un_state != ST_STATE_RESOURCE_WAIT) { 5604 un->un_laststate = un->un_state; 5605 un->un_state = ST_STATE_RESOURCE_WAIT; 5606 } 5607 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 5608 "temp no resources for pkt\n"); 5609 } else { 5610 /* 5611 * Unlikely that it would be retryable then not. 5612 */ 5613 if (un->un_state == ST_STATE_RESOURCE_WAIT) { 5614 un->un_state = un->un_laststate; 5615 } 5616 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 5617 "perm no resources for pkt errno = 0x%x\n", 5618 status); 5619 } 5620 return; 5621 } 5622 /* 5623 * Worked this time set the state back. 5624 */ 5625 if (un->un_state == ST_STATE_RESOURCE_WAIT) { 5626 un->un_state = un->un_laststate; 5627 } 5628 } 5629 5630 /* 5631 * move from waitq to runq 5632 */ 5633 un->un_quef = bp->b_actf; 5634 if (un->un_quel == bp) { 5635 /* 5636 * For the case of queue having one 5637 * element, set the tail pointer to 5638 * point to the element. 5639 */ 5640 un->un_quel = bp->b_actf; 5641 } 5642 5643 bp->b_actf = NULL; 5644 5645 if (un->un_runqf) { 5646 un->un_runql->b_actf = bp; 5647 } else { 5648 un->un_runqf = bp; 5649 } 5650 un->un_runql = bp; 5651 5652 5653 ST_CDB(ST_DEVINFO, "Start CDB", (char *)BP_PKT(bp)->pkt_cdbp); 5654 5655 #ifdef STDEBUG 5656 start_dump(un, bp); 5657 #endif 5658 5659 /* could not get here if throttle was zero */ 5660 un->un_last_throttle = un->un_throttle; 5661 un->un_throttle = 0; /* so nothing else will come in here */ 5662 un->un_ncmds++; 5663 5664 ST_DO_KSTATS(bp, kstat_waitq_to_runq); 5665 5666 mutex_exit(ST_MUTEX); 5667 5668 status = scsi_transport(BP_PKT(bp)); 5669 5670 mutex_enter(ST_MUTEX); 5671 5672 if (un->un_last_throttle) { 5673 un->un_throttle = un->un_last_throttle; 5674 } 5675 5676 if (status != TRAN_ACCEPT) { 5677 ST_DO_KSTATS(bp, kstat_runq_back_to_waitq); 5678 mutex_exit(ST_MUTEX); 5679 5680 if (status == TRAN_BUSY) { 5681 /* if too many retries, fail the transport */ 5682 if (st_handle_start_busy(un, bp, 5683 ST_TRAN_BUSY_TIMEOUT) == 0) 5684 goto done; 5685 } 5686 scsi_log(ST_DEVINFO, st_label, CE_WARN, 5687 "transport rejected\n"); 5688 bp->b_resid = bp->b_bcount; 5689 5690 5691 #ifndef __lock_lint 5692 /* 5693 * warlock doesn't understand this potential 5694 * recursion? 5695 */ 5696 mutex_enter(ST_MUTEX); 5697 ST_DO_KSTATS(bp, kstat_waitq_exit); 5698 ST_DO_ERRSTATS(un, st_transerrs); 5699 st_bioerror(bp, EIO); 5700 SET_PE_FLAG(un); 5701 st_done_and_mutex_exit(un, bp); 5702 #endif 5703 } else { 5704 un->un_tran_retry_ct = 0; 5705 mutex_exit(ST_MUTEX); 5706 } 5707 5708 done: 5709 5710 mutex_enter(ST_MUTEX); 5711 } 5712 5713 /* 5714 * if the transport is busy, then put this bp back on the waitq 5715 */ 5716 static int 5717 st_handle_start_busy(struct scsi_tape *un, struct buf *bp, 5718 clock_t timeout_interval) 5719 { 5720 struct buf *last_quef, *runq_bp; 5721 int rval = 0; 5722 5723 ST_FUNC(ST_DEVINFO, st_handle_start_busy); 5724 5725 mutex_enter(ST_MUTEX); 5726 5727 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 5728 "st_handle_start_busy()\n"); 5729 5730 /* 5731 * Check to see if we hit the retry timeout and one last check for 5732 * making sure this is the last on the runq, if it is not, we have 5733 * to fail 5734 */ 5735 if (((int)un->un_tran_retry_ct++ > st_retry_count) || 5736 (un->un_runql != bp)) { 5737 rval = -1; 5738 goto exit; 5739 } 5740 5741 /* put the bp back on the waitq */ 5742 if (un->un_quef) { 5743 last_quef = un->un_quef; 5744 un->un_quef = bp; 5745 bp->b_actf = last_quef; 5746 } else { 5747 bp->b_actf = NULL; 5748 un->un_quef = bp; 5749 un->un_quel = bp; 5750 } 5751 5752 /* 5753 * Decrement un_ncmds so that this 5754 * gets thru' st_start() again. 5755 */ 5756 un->un_ncmds--; 5757 5758 /* 5759 * since this is an error case, we won't have to do 5760 * this list walking much. We've already made sure this bp was the 5761 * last on the runq 5762 */ 5763 runq_bp = un->un_runqf; 5764 5765 if (un->un_runqf == bp) { 5766 un->un_runqf = NULL; 5767 un->un_runql = NULL; 5768 } else { 5769 while (runq_bp) { 5770 if (runq_bp->b_actf == bp) { 5771 runq_bp->b_actf = NULL; 5772 un->un_runql = runq_bp; 5773 break; 5774 } 5775 runq_bp = runq_bp->b_actf; 5776 } 5777 } 5778 5779 5780 /* 5781 * send a marker pkt, if appropriate 5782 */ 5783 st_hba_unflush(un); 5784 5785 /* 5786 * all queues are aligned, we are just waiting to 5787 * transport, don't alloc any more buf p's, when 5788 * st_start is reentered. 5789 */ 5790 (void) timeout(st_start_restart, un, timeout_interval); 5791 5792 exit: 5793 mutex_exit(ST_MUTEX); 5794 return (rval); 5795 } 5796 5797 5798 /* 5799 * st_runout a callback that is called what a resource allocatation failed 5800 */ 5801 static int 5802 st_runout(caddr_t arg) 5803 { 5804 struct scsi_tape *un = (struct scsi_tape *)arg; 5805 struct buf *bp; 5806 ASSERT(un != NULL); 5807 5808 ST_FUNC(ST_DEVINFO, st_runout); 5809 5810 mutex_enter(ST_MUTEX); 5811 5812 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, "st_runout()\n"); 5813 5814 bp = un->un_quef; 5815 5816 /* 5817 * failed scsi_init_pkt(). If errno is zero its retryable. 5818 */ 5819 if ((bp != NULL) && (geterror(bp) != 0)) { 5820 5821 scsi_log(ST_DEVINFO, st_label, CE_WARN, 5822 "errors after pkt alloc (b_flags=0x%x, b_error=0x%x)\n", 5823 bp->b_flags, geterror(bp)); 5824 ASSERT((bp->b_flags & B_DONE) == 0); 5825 5826 un->un_quef = bp->b_actf; 5827 if (un->un_quel == bp) { 5828 /* 5829 * For the case of queue having one 5830 * element, set the tail pointer to 5831 * point to the element. 5832 */ 5833 un->un_quel = bp->b_actf; 5834 } 5835 mutex_exit(ST_MUTEX); 5836 bp->b_actf = NULL; 5837 5838 ASSERT((bp->b_flags & B_DONE) == 0); 5839 5840 /* 5841 * Set resid, Error already set, then unblock calling thread. 5842 */ 5843 bp->b_resid = bp->b_bcount; 5844 biodone(bp); 5845 } else { 5846 /* 5847 * Try Again 5848 */ 5849 st_start(un); 5850 mutex_exit(ST_MUTEX); 5851 } 5852 5853 /* 5854 * Comments courtesy of sd.c 5855 * The scsi_init_pkt routine allows for the callback function to 5856 * return a 0 indicating the callback should be rescheduled or a 1 5857 * indicating not to reschedule. This routine always returns 1 5858 * because the driver always provides a callback function to 5859 * scsi_init_pkt. This results in a callback always being scheduled 5860 * (via the scsi_init_pkt callback implementation) if a resource 5861 * failure occurs. 5862 */ 5863 5864 return (1); 5865 } 5866 5867 /* 5868 * st_done_and_mutex_exit() 5869 * - remove bp from runq 5870 * - start up the next request 5871 * - if this was an asynch bp, clean up 5872 * - exit with released mutex 5873 */ 5874 static void 5875 st_done_and_mutex_exit(struct scsi_tape *un, struct buf *bp) 5876 { 5877 struct buf *runqbp, *prevbp; 5878 int pe_flagged = 0; 5879 5880 ASSERT(MUTEX_HELD(&un->un_sd->sd_mutex)); 5881 #if !defined(lint) 5882 _NOTE(LOCK_RELEASED_AS_SIDE_EFFECT(&un->un_sd->sd_mutex)) 5883 #endif 5884 5885 ST_FUNC(ST_DEVINFO, st_done_and_mutex_exit); 5886 5887 ASSERT(mutex_owned(ST_MUTEX)); 5888 5889 /* 5890 * if bp is still on the runq (anywhere), then remove it 5891 */ 5892 prevbp = NULL; 5893 for (runqbp = un->un_runqf; runqbp != 0; runqbp = runqbp->b_actf) { 5894 if (runqbp == bp) { 5895 if (runqbp == un->un_runqf) { 5896 un->un_runqf = bp->b_actf; 5897 } else { 5898 prevbp->b_actf = bp->b_actf; 5899 } 5900 if (un->un_runql == bp) { 5901 un->un_runql = prevbp; 5902 } 5903 break; 5904 } 5905 prevbp = runqbp; 5906 } 5907 bp->b_actf = NULL; 5908 5909 un->un_ncmds--; 5910 cv_signal(&un->un_queue_cv); 5911 5912 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 5913 "st_done_and_mutex_exit(): cmd=0x%x count=%ld resid=%ld flags=" 5914 "0x%x\n", (uchar_t)*((caddr_t)(BP_PKT(bp))->pkt_cdbp), bp->b_bcount, 5915 bp->b_resid, bp->b_flags); 5916 5917 5918 /* 5919 * update kstats with transfer count info 5920 */ 5921 if (un->un_stats && (bp != un->un_sbufp) && IS_RW(bp)) { 5922 uint32_t n_done = bp->b_bcount - bp->b_resid; 5923 if (bp->b_flags & B_READ) { 5924 IOSP->reads++; 5925 IOSP->nread += n_done; 5926 } else { 5927 IOSP->writes++; 5928 IOSP->nwritten += n_done; 5929 } 5930 } 5931 5932 /* 5933 * Start the next one before releasing resources on this one, if 5934 * there is something on the queue and persistent errors has not been 5935 * flagged 5936 */ 5937 5938 if ((pe_flagged = IS_PE_FLAG_SET(un)) != 0) { 5939 un->un_last_resid = bp->b_resid; 5940 un->un_last_count = bp->b_bcount; 5941 } 5942 5943 if (un->un_pwr_mgmt == ST_PWR_SUSPENDED) { 5944 cv_broadcast(&un->un_tape_busy_cv); 5945 } else if (un->un_quef && un->un_throttle && !pe_flagged) { 5946 st_start(un); 5947 } 5948 5949 if (bp == un->un_sbufp && (bp->b_flags & B_ASYNC)) { 5950 /* 5951 * Since we marked this ourselves as ASYNC, 5952 * there isn't anybody around waiting for 5953 * completion any more. 5954 */ 5955 uchar_t com = (uchar_t)(uintptr_t)bp->b_forw; 5956 if (com == SCMD_READ || com == SCMD_WRITE) { 5957 bp->b_un.b_addr = (caddr_t)0; 5958 } 5959 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 5960 "st_done_and_mutex_exit(async): freeing pkt\n"); 5961 scsi_destroy_pkt(BP_PKT(bp)); 5962 un->un_sbuf_busy = 0; 5963 cv_signal(&un->un_sbuf_cv); 5964 mutex_exit(ST_MUTEX); 5965 return; 5966 } 5967 5968 if (bp == un->un_sbufp && BP_UCMD(bp)) { 5969 /* 5970 * Copy status from scsi_pkt to uscsi_cmd 5971 * since st_ioctl_cmd needs it 5972 */ 5973 BP_UCMD(bp)->uscsi_status = SCBP_C(BP_PKT(bp)); 5974 } 5975 5976 5977 #ifdef STDEBUG 5978 if (((st_debug & 0xf) >= 4) && 5979 (((un->un_pos.blkno % 100) == 0) || IS_PE_FLAG_SET(un))) { 5980 5981 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 5982 "st_d_a_m_exit(): ncmds = %d, thr = %d, " 5983 "un_errno = %d, un_pe = %d\n", 5984 un->un_ncmds, un->un_throttle, un->un_errno, 5985 un->un_persist_errors); 5986 } 5987 5988 #endif 5989 5990 mutex_exit(ST_MUTEX); 5991 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 5992 "st_done_and_mutex_exit: freeing pkt\n"); 5993 5994 scsi_destroy_pkt(BP_PKT(bp)); 5995 5996 biodone(bp); 5997 5998 /* 5999 * now that we biodoned that command, if persistent errors have been 6000 * flagged, flush the waitq 6001 */ 6002 if (pe_flagged) 6003 st_flush(un); 6004 } 6005 6006 6007 /* 6008 * Tape error, flush tape driver queue. 6009 */ 6010 static void 6011 st_flush(struct scsi_tape *un) 6012 { 6013 struct buf *bp; 6014 6015 ST_FUNC(ST_DEVINFO, st_flush); 6016 6017 mutex_enter(ST_MUTEX); 6018 6019 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 6020 "st_flush(), ncmds = %d, quef = 0x%p\n", 6021 un->un_ncmds, (void *)un->un_quef); 6022 6023 /* 6024 * if we still have commands outstanding, wait for them to come in 6025 * before flushing the queue, and make sure there is a queue 6026 */ 6027 if (un->un_ncmds || !un->un_quef) 6028 goto exit; 6029 6030 /* 6031 * we have no more commands outstanding, so let's deal with special 6032 * cases in the queue for EOM and FM. If we are here, and un_errno 6033 * is 0, then we know there was no error and we return a 0 read or 6034 * write before showing errors 6035 */ 6036 6037 /* Flush the wait queue. */ 6038 while ((bp = un->un_quef) != NULL) { 6039 un->un_quef = bp->b_actf; 6040 6041 bp->b_resid = bp->b_bcount; 6042 6043 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 6044 "st_flush() : blkno=%d, err=%d, b_bcount=%ld\n", 6045 un->un_pos.blkno, un->un_errno, bp->b_bcount); 6046 6047 st_set_pe_errno(un); 6048 6049 bioerror(bp, un->un_errno); 6050 6051 mutex_exit(ST_MUTEX); 6052 /* it should have one, but check anyway */ 6053 if (BP_PKT(bp)) { 6054 scsi_destroy_pkt(BP_PKT(bp)); 6055 } 6056 biodone(bp); 6057 mutex_enter(ST_MUTEX); 6058 } 6059 6060 /* 6061 * It's not a bad practice to reset the 6062 * waitq tail pointer to NULL. 6063 */ 6064 un->un_quel = NULL; 6065 6066 exit: 6067 /* we mucked with the queue, so let others know about it */ 6068 cv_signal(&un->un_queue_cv); 6069 mutex_exit(ST_MUTEX); 6070 } 6071 6072 6073 /* 6074 * Utility functions 6075 */ 6076 static int 6077 st_determine_generic(dev_t dev) 6078 { 6079 int bsize; 6080 static char *cart = "0.25 inch cartridge"; 6081 char *sizestr; 6082 6083 GET_SOFT_STATE(dev); 6084 6085 ST_FUNC(ST_DEVINFO, st_determine_generic); 6086 6087 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 6088 "st_determine_generic(dev = 0x%lx)\n", dev); 6089 6090 ASSERT(mutex_owned(ST_MUTEX)); 6091 6092 if (st_modesense(un)) { 6093 return (-1); 6094 } 6095 6096 bsize = (un->un_mspl->high_bl << 16) | 6097 (un->un_mspl->mid_bl << 8) | 6098 (un->un_mspl->low_bl); 6099 6100 if (bsize == 0) { 6101 un->un_dp->options |= ST_VARIABLE; 6102 un->un_dp->bsize = 0; 6103 un->un_bsize = 0; 6104 } else if (bsize > ST_MAXRECSIZE_FIXED) { 6105 /* 6106 * record size of this device too big. 6107 * try and convert it to variable record length. 6108 * 6109 */ 6110 un->un_dp->options |= ST_VARIABLE; 6111 if (st_change_block_size(dev, 0) != 0) { 6112 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 6113 "Fixed Record Size %d is too large\n", bsize); 6114 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 6115 "Cannot switch to variable record size\n"); 6116 un->un_dp->options &= ~ST_VARIABLE; 6117 return (-1); 6118 } 6119 } else if (st_change_block_size(dev, 0) == 0) { 6120 /* 6121 * If the drive was set to a non zero block size, 6122 * See if it can be set to a zero block size. 6123 * If it works, ST_VARIABLE so user can set it as they want. 6124 */ 6125 un->un_dp->options |= ST_VARIABLE; 6126 un->un_dp->bsize = 0; 6127 un->un_bsize = 0; 6128 } else { 6129 un->un_dp->bsize = bsize; 6130 un->un_bsize = bsize; 6131 } 6132 6133 6134 switch (un->un_mspl->density) { 6135 default: 6136 case 0x0: 6137 /* 6138 * default density, cannot determine any other 6139 * information. 6140 */ 6141 sizestr = "Unknown type- assuming 0.25 inch cartridge"; 6142 un->un_dp->type = ST_TYPE_DEFAULT; 6143 un->un_dp->options |= (ST_AUTODEN_OVERRIDE|ST_QIC); 6144 break; 6145 case 0x1: 6146 case 0x2: 6147 case 0x3: 6148 case 0x6: 6149 /* 6150 * 1/2" reel 6151 */ 6152 sizestr = "0.50 inch reel"; 6153 un->un_dp->type = ST_TYPE_REEL; 6154 un->un_dp->options |= ST_REEL; 6155 un->un_dp->densities[0] = 0x1; 6156 un->un_dp->densities[1] = 0x2; 6157 un->un_dp->densities[2] = 0x6; 6158 un->un_dp->densities[3] = 0x3; 6159 break; 6160 case 0x4: 6161 case 0x5: 6162 case 0x7: 6163 case 0x0b: 6164 6165 /* 6166 * Quarter inch. 6167 */ 6168 sizestr = cart; 6169 un->un_dp->type = ST_TYPE_DEFAULT; 6170 un->un_dp->options |= ST_QIC; 6171 6172 un->un_dp->densities[1] = 0x4; 6173 un->un_dp->densities[2] = 0x5; 6174 un->un_dp->densities[3] = 0x7; 6175 un->un_dp->densities[0] = 0x0b; 6176 break; 6177 6178 case 0x0f: 6179 case 0x10: 6180 case 0x11: 6181 case 0x12: 6182 /* 6183 * QIC-120, QIC-150, QIC-320, QIC-600 6184 */ 6185 sizestr = cart; 6186 un->un_dp->type = ST_TYPE_DEFAULT; 6187 un->un_dp->options |= ST_QIC; 6188 un->un_dp->densities[0] = 0x0f; 6189 un->un_dp->densities[1] = 0x10; 6190 un->un_dp->densities[2] = 0x11; 6191 un->un_dp->densities[3] = 0x12; 6192 break; 6193 6194 case 0x09: 6195 case 0x0a: 6196 case 0x0c: 6197 case 0x0d: 6198 /* 6199 * 1/2" cartridge tapes. Include HI-TC. 6200 */ 6201 sizestr = cart; 6202 sizestr[2] = '5'; 6203 sizestr[3] = '0'; 6204 un->un_dp->type = ST_TYPE_HIC; 6205 un->un_dp->densities[0] = 0x09; 6206 un->un_dp->densities[1] = 0x0a; 6207 un->un_dp->densities[2] = 0x0c; 6208 un->un_dp->densities[3] = 0x0d; 6209 break; 6210 6211 case 0x13: 6212 /* DDS-2/DDS-3 scsi spec densities */ 6213 case 0x24: 6214 case 0x25: 6215 case 0x26: 6216 sizestr = "DAT Data Storage (DDS)"; 6217 un->un_dp->type = ST_TYPE_DAT; 6218 un->un_dp->options |= ST_AUTODEN_OVERRIDE; 6219 break; 6220 6221 case 0x14: 6222 /* 6223 * Helical Scan (Exabyte) devices 6224 */ 6225 sizestr = "8mm helical scan cartridge"; 6226 un->un_dp->type = ST_TYPE_EXABYTE; 6227 un->un_dp->options |= ST_AUTODEN_OVERRIDE; 6228 break; 6229 } 6230 6231 /* 6232 * Assume LONG ERASE, BSF and BSR 6233 */ 6234 6235 un->un_dp->options |= 6236 (ST_LONG_ERASE | ST_UNLOADABLE | ST_BSF | ST_BSR | ST_KNOWS_EOD); 6237 6238 /* 6239 * Only if mode sense data says no buffered write, set NOBUF 6240 */ 6241 if (un->un_mspl->bufm == 0) 6242 un->un_dp->options |= ST_NOBUF; 6243 6244 /* 6245 * set up large read and write retry counts 6246 */ 6247 6248 un->un_dp->max_rretries = un->un_dp->max_wretries = 1000; 6249 6250 /* 6251 * If this is a 0.50 inch reel tape, and 6252 * it is *not* variable mode, try and 6253 * set it to variable record length 6254 * mode. 6255 */ 6256 if ((un->un_dp->options & ST_REEL) && un->un_bsize != 0 && 6257 (un->un_dp->options & ST_VARIABLE)) { 6258 if (st_change_block_size(dev, 0) == 0) { 6259 un->un_dp->bsize = 0; 6260 un->un_mspl->high_bl = un->un_mspl->mid_bl = 6261 un->un_mspl->low_bl = 0; 6262 } 6263 } 6264 6265 /* 6266 * Write to console about type of device found 6267 */ 6268 ST_DEBUG6(ST_DEVINFO, st_label, CE_NOTE, 6269 "Generic Drive, Vendor=%s\n\t%s", un->un_dp->name, 6270 sizestr); 6271 if (un->un_dp->options & ST_VARIABLE) { 6272 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 6273 "!Variable record length I/O\n"); 6274 } else { 6275 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 6276 "!Fixed record length (%d byte blocks) I/O\n", 6277 un->un_dp->bsize); 6278 } 6279 ASSERT(mutex_owned(ST_MUTEX)); 6280 return (0); 6281 } 6282 6283 static int 6284 st_determine_density(dev_t dev, int rw) 6285 { 6286 int rval = 0; 6287 6288 GET_SOFT_STATE(dev); 6289 6290 ST_FUNC(ST_DEVINFO, st_determine_density); 6291 6292 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 6293 "st_determine_density(dev = 0x%lx, rw = %s)\n", 6294 dev, (rw == B_WRITE ? wr_str: rd_str)); 6295 6296 ASSERT(mutex_owned(ST_MUTEX)); 6297 6298 /* 6299 * If we're past BOT, density is determined already. 6300 */ 6301 if (un->un_pos.pmode == logical) { 6302 if (un->un_pos.lgclblkno != 0) { 6303 goto exit; 6304 } 6305 } else if (un->un_pos.pmode == legacy) { 6306 if ((un->un_pos.fileno != 0) || (un->un_pos.blkno != 0)) { 6307 /* 6308 * XXX: put in a bitch message about attempting to 6309 * XXX: change density past BOT. 6310 */ 6311 goto exit; 6312 } 6313 } else { 6314 goto exit; 6315 } 6316 6317 6318 /* 6319 * If we're going to be writing, we set the density 6320 */ 6321 if (rw == 0 || rw == B_WRITE) { 6322 /* un_curdens is used as an index into densities table */ 6323 un->un_curdens = MT_DENSITY(un->un_dev); 6324 if (st_set_density(dev)) { 6325 rval = -1; 6326 } 6327 goto exit; 6328 } 6329 6330 /* 6331 * If density is known already, 6332 * we don't have to get it again.(?) 6333 */ 6334 if (!un->un_density_known) { 6335 if (st_get_density(dev)) { 6336 rval = -1; 6337 } 6338 } 6339 6340 exit: 6341 ASSERT(mutex_owned(ST_MUTEX)); 6342 return (rval); 6343 } 6344 6345 6346 /* 6347 * Try to determine density. We do this by attempting to read the 6348 * first record off the tape, cycling through the available density 6349 * codes as we go. 6350 */ 6351 6352 static int 6353 st_get_density(dev_t dev) 6354 { 6355 int succes = 0, rval = -1, i; 6356 uint_t size; 6357 uchar_t dens, olddens; 6358 6359 GET_SOFT_STATE(dev); 6360 6361 ST_FUNC(ST_DEVINFO, st_get_density); 6362 6363 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 6364 "st_get_density(dev = 0x%lx)\n", dev); 6365 6366 ASSERT(mutex_owned(ST_MUTEX)); 6367 6368 /* 6369 * If Auto Density override is enabled The drive has 6370 * only one density and there is no point in attempting 6371 * find the correct one. 6372 * 6373 * Since most modern drives auto detect the density 6374 * and format of the recorded media before they come 6375 * ready. What this function does is a legacy behavior 6376 * and modern drives not only don't need it, The backup 6377 * utilities that do positioning via uscsi find the un- 6378 * expected rewinds problematic. 6379 * 6380 * The drives that need this are old reel to reel devices. 6381 * I took a swag and said they must be scsi-1 or older. 6382 * I don't beleave there will any of the newer devices 6383 * that need this. There will be some scsi-1 devices that 6384 * don't need this but I don't think they will be using the 6385 * BIG aftermarket backup and restore utilitys. 6386 */ 6387 if ((un->un_dp->options & ST_AUTODEN_OVERRIDE) || 6388 (un->un_sd->sd_inq->inq_ansi > 1)) { 6389 un->un_density_known = 1; 6390 rval = 0; 6391 goto exit; 6392 } 6393 6394 /* 6395 * This will only work on variable record length tapes 6396 * if and only if all variable record length tapes autodensity 6397 * select. 6398 */ 6399 size = (unsigned)(un->un_dp->bsize ? un->un_dp->bsize : SECSIZE); 6400 un->un_tmpbuf = kmem_alloc(size, KM_SLEEP); 6401 6402 /* 6403 * Start at the specified density 6404 */ 6405 6406 dens = olddens = un->un_curdens = MT_DENSITY(un->un_dev); 6407 6408 for (i = 0; i < NDENSITIES; i++, ((un->un_curdens == NDENSITIES - 1) ? 6409 (un->un_curdens = 0) : (un->un_curdens += 1))) { 6410 /* 6411 * If we've done this density before, 6412 * don't bother to do it again. 6413 */ 6414 dens = un->un_dp->densities[un->un_curdens]; 6415 if (i > 0 && dens == olddens) 6416 continue; 6417 olddens = dens; 6418 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 6419 "trying density 0x%x\n", dens); 6420 if (st_set_density(dev)) { 6421 continue; 6422 } 6423 6424 /* 6425 * XXX - the creates lots of headaches and slowdowns - must 6426 * fix. 6427 */ 6428 succes = (st_cmd(dev, SCMD_READ, (int)size, SYNC_CMD) == 0); 6429 if (st_cmd(dev, SCMD_REWIND, 0, SYNC_CMD)) { 6430 break; 6431 } 6432 if (succes) { 6433 st_init(un); 6434 rval = 0; 6435 un->un_density_known = 1; 6436 break; 6437 } 6438 } 6439 kmem_free(un->un_tmpbuf, size); 6440 un->un_tmpbuf = 0; 6441 6442 exit: 6443 ASSERT(mutex_owned(ST_MUTEX)); 6444 return (rval); 6445 } 6446 6447 static int 6448 st_set_density(dev_t dev) 6449 { 6450 int rval = 0; 6451 6452 GET_SOFT_STATE(dev); 6453 6454 ST_FUNC(ST_DEVINFO, st_set_density); 6455 6456 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 6457 "st_set_density(dev = 0x%lx): density = 0x%x\n", dev, 6458 un->un_dp->densities[un->un_curdens]); 6459 6460 ASSERT(mutex_owned(ST_MUTEX)); 6461 6462 un->un_mspl->density = un->un_dp->densities[un->un_curdens]; 6463 6464 if ((un->un_dp->options & ST_AUTODEN_OVERRIDE) == 0) { 6465 /* 6466 * If auto density override is not set, Use mode select 6467 * to set density and compression. 6468 */ 6469 if (st_modeselect(un)) { 6470 rval = -1; 6471 } 6472 } else if ((un->un_dp->options & ST_MODE_SEL_COMP) != 0) { 6473 /* 6474 * If auto density and mode select compression are set, 6475 * This is a drive with one density code but compression 6476 * can be enabled or disabled. 6477 * Set compression but no need to set density. 6478 */ 6479 rval = st_set_compression(un); 6480 if ((rval != 0) && (rval != EALREADY)) { 6481 rval = -1; 6482 } else { 6483 rval = 0; 6484 } 6485 } 6486 6487 /* If sucessful set density and/or compression, mark density known */ 6488 if (rval == 0) { 6489 un->un_density_known = 1; 6490 } 6491 6492 ASSERT(mutex_owned(ST_MUTEX)); 6493 return (rval); 6494 } 6495 6496 static int 6497 st_loadtape(dev_t dev) 6498 { 6499 int rval; 6500 6501 GET_SOFT_STATE(dev); 6502 6503 ST_FUNC(ST_DEVINFO, st_load_tape); 6504 6505 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 6506 "st_loadtape(dev = 0x%lx)\n", dev); 6507 6508 ASSERT(mutex_owned(ST_MUTEX)); 6509 6510 /* 6511 * 'LOAD' the tape to BOT by rewinding 6512 */ 6513 rval = st_cmd(dev, SCMD_REWIND, 1, SYNC_CMD); 6514 if (rval == 0) { 6515 st_init(un); 6516 un->un_density_known = 0; 6517 } 6518 6519 ASSERT(mutex_owned(ST_MUTEX)); 6520 return (rval); 6521 } 6522 6523 6524 /* 6525 * Note: QIC devices aren't so smart. If you try to append 6526 * after EOM, the write can fail because the device doesn't know 6527 * it's at EOM. In that case, issue a read. The read should fail 6528 * because there's no data, but the device knows it's at EOM, 6529 * so a subsequent write should succeed. To further confuse matters, 6530 * the target returns the same error if the tape is positioned 6531 * such that a write would overwrite existing data. That's why 6532 * we have to do the append test. A read in the middle of 6533 * recorded data would succeed, thus indicating we're attempting 6534 * something illegal. 6535 */ 6536 6537 6538 static void 6539 st_test_append(struct buf *bp) 6540 { 6541 dev_t dev = bp->b_edev; 6542 struct scsi_tape *un; 6543 uchar_t status; 6544 unsigned bcount; 6545 6546 un = ddi_get_soft_state(st_state, MTUNIT(dev)); 6547 6548 ST_FUNC(ST_DEVINFO, st_test_append); 6549 6550 ASSERT(mutex_owned(ST_MUTEX)); 6551 6552 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 6553 "st_test_append(): fileno %d\n", un->un_pos.fileno); 6554 6555 un->un_laststate = un->un_state; 6556 un->un_state = ST_STATE_APPEND_TESTING; 6557 un->un_test_append = 0; 6558 6559 /* 6560 * first, map in the buffer, because we're doing a double write -- 6561 * first into the kernel, then onto the tape. 6562 */ 6563 bp_mapin(bp); 6564 6565 /* 6566 * get a copy of the data.... 6567 */ 6568 un->un_tmpbuf = kmem_alloc((unsigned)bp->b_bcount, KM_SLEEP); 6569 bcopy(bp->b_un.b_addr, un->un_tmpbuf, (uint_t)bp->b_bcount); 6570 6571 /* 6572 * attempt the write.. 6573 */ 6574 6575 if (st_cmd(dev, (int)SCMD_WRITE, (int)bp->b_bcount, SYNC_CMD) == 0) { 6576 success: 6577 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 6578 "append write succeeded\n"); 6579 bp->b_resid = un->un_sbufp->b_resid; 6580 mutex_exit(ST_MUTEX); 6581 bcount = (unsigned)bp->b_bcount; 6582 biodone(bp); 6583 mutex_enter(ST_MUTEX); 6584 un->un_laststate = un->un_state; 6585 un->un_state = ST_STATE_OPEN; 6586 kmem_free(un->un_tmpbuf, bcount); 6587 un->un_tmpbuf = NULL; 6588 return; 6589 } 6590 6591 /* 6592 * The append failed. Do a short read. If that fails, we are at EOM 6593 * so we can retry the write command. If that succeeds, than we're 6594 * all screwed up (the controller reported a real error). 6595 * 6596 * XXX: should the dummy read be > SECSIZE? should it be the device's 6597 * XXX: block size? 6598 * 6599 */ 6600 status = un->un_status; 6601 un->un_status = 0; 6602 (void) st_cmd(dev, SCMD_READ, SECSIZE, SYNC_CMD); 6603 if (un->un_status == KEY_BLANK_CHECK) { 6604 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 6605 "append at EOM\n"); 6606 /* 6607 * Okay- the read failed. We should actually have confused 6608 * the controller enough to allow writing. In any case, the 6609 * i/o is on its own from here on out. 6610 */ 6611 un->un_laststate = un->un_state; 6612 un->un_state = ST_STATE_OPEN; 6613 bcopy(bp->b_un.b_addr, un->un_tmpbuf, (uint_t)bp->b_bcount); 6614 if (st_cmd(dev, (int)SCMD_WRITE, (int)bp->b_bcount, 6615 SYNC_CMD) == 0) { 6616 goto success; 6617 } 6618 } 6619 6620 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 6621 "append write failed- not at EOM\n"); 6622 bp->b_resid = bp->b_bcount; 6623 st_bioerror(bp, EIO); 6624 6625 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 6626 "st_test_append : EIO : append write failed - not at EOM"); 6627 6628 /* 6629 * backspace one record to get back to where we were 6630 */ 6631 if (st_cmd(dev, SCMD_SPACE, Blk(-1), SYNC_CMD)) { 6632 un->un_pos.pmode = invalid; 6633 } 6634 6635 un->un_err_resid = bp->b_resid; 6636 un->un_status = status; 6637 6638 /* 6639 * Note: biodone will do a bp_mapout() 6640 */ 6641 mutex_exit(ST_MUTEX); 6642 bcount = (unsigned)bp->b_bcount; 6643 biodone(bp); 6644 mutex_enter(ST_MUTEX); 6645 un->un_laststate = un->un_state; 6646 un->un_state = ST_STATE_OPEN_PENDING_IO; 6647 kmem_free(un->un_tmpbuf, bcount); 6648 un->un_tmpbuf = NULL; 6649 } 6650 6651 /* 6652 * Special command handler 6653 */ 6654 6655 /* 6656 * common st_cmd code. The fourth parameter states 6657 * whether the caller wishes to await the results 6658 * Note the release of the mutex during most of the function 6659 */ 6660 static int 6661 st_cmd(dev_t dev, int com, int count, int wait) 6662 { 6663 struct buf *bp; 6664 int err; 6665 6666 GET_SOFT_STATE(dev); 6667 6668 ST_FUNC(ST_DEVINFO, st_cmd); 6669 6670 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 6671 "st_cmd(dev = 0x%lx, com = 0x%x, count = %x, wait = %d)\n", 6672 dev, com, count, wait); 6673 6674 ASSERT(MUTEX_HELD(&un->un_sd->sd_mutex)); 6675 ASSERT(mutex_owned(ST_MUTEX)); 6676 6677 #ifdef STDEBUG 6678 if ((st_debug & 0xf)) { 6679 st_debug_cmds(un, com, count, wait); 6680 } 6681 #endif 6682 6683 /* check to see if this command requires the drive to be reserved */ 6684 err = st_check_cmd_for_need_to_reserve(un, com, count); 6685 6686 if (err) { 6687 return (err); 6688 } 6689 6690 while (un->un_sbuf_busy) 6691 cv_wait(&un->un_sbuf_cv, ST_MUTEX); 6692 un->un_sbuf_busy = 1; 6693 6694 bp = un->un_sbufp; 6695 bzero(bp, sizeof (buf_t)); 6696 6697 bp->b_flags = (wait) ? B_BUSY : B_BUSY|B_ASYNC; 6698 6699 /* 6700 * Set count to the actual size of the data tranfer. 6701 * For commands with no data transfer, set bp->b_bcount 6702 * to the value to be used when constructing the 6703 * cdb in st_make_cmd(). 6704 */ 6705 switch (com) { 6706 case SCMD_READ: 6707 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 6708 "special read %d\n", count); 6709 bp->b_flags |= B_READ; 6710 bp->b_un.b_addr = un->un_tmpbuf; 6711 break; 6712 6713 case SCMD_WRITE: 6714 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 6715 "special write %d\n", count); 6716 bp->b_un.b_addr = un->un_tmpbuf; 6717 break; 6718 6719 case SCMD_WRITE_FILE_MARK: 6720 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 6721 "write %d file marks\n", count); 6722 bp->b_bcount = count; 6723 count = 0; 6724 break; 6725 6726 case SCMD_REWIND: 6727 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "rewind\n"); 6728 bp->b_bcount = 0; 6729 count = 0; 6730 break; 6731 6732 case SCMD_SPACE: 6733 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "space\n"); 6734 /* 6735 * XXX The user could have entered a number that will 6736 * not fit in the 12 bit count field. Whats new here 6737 * checking that. Down the road this should use space(16). 6738 */ 6739 if ((SPACE_CNT(count) > 0x7fffff) || 6740 (SPACE_CNT(count) < -(0x7fffff))) { 6741 un->un_sbuf_busy = 0; 6742 cv_signal(&un->un_sbuf_cv); 6743 return (EINVAL); 6744 } 6745 bp->b_bcount = count; 6746 count = 0; 6747 break; 6748 6749 case SCMD_RESERVE: 6750 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "reserve"); 6751 bp->b_bcount = 0; 6752 count = 0; 6753 break; 6754 6755 case SCMD_RELEASE: 6756 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "release"); 6757 bp->b_bcount = 0; 6758 count = 0; 6759 break; 6760 6761 case SCMD_LOAD: 6762 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 6763 "%s tape\n", (count & LD_LOAD) ? "load" : "unload"); 6764 bp->b_bcount = count; 6765 count = 0; 6766 break; 6767 6768 case SCMD_ERASE: 6769 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 6770 "erase tape\n"); 6771 bp->b_bcount = 0; 6772 count = 0; 6773 break; 6774 6775 case SCMD_MODE_SENSE: 6776 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 6777 "mode sense\n"); 6778 bp->b_flags |= B_READ; 6779 bp->b_un.b_addr = (caddr_t)(un->un_mspl); 6780 break; 6781 6782 case SCMD_MODE_SELECT: 6783 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 6784 "mode select\n"); 6785 bp->b_un.b_addr = (caddr_t)(un->un_mspl); 6786 break; 6787 6788 case SCMD_READ_BLKLIM: 6789 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 6790 "read block limits\n"); 6791 bp->b_flags |= B_READ; 6792 bp->b_un.b_addr = (caddr_t)(un->un_rbl); 6793 break; 6794 6795 case SCMD_TEST_UNIT_READY: 6796 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 6797 "test unit ready\n"); 6798 bp->b_bcount = 0; 6799 count = 0; 6800 break; 6801 6802 case SCMD_DOORLOCK: 6803 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 6804 "%s tape\n", (count & MR_LOCK) ? "lock" : "unlock"); 6805 bp->b_bcount = count = 0; 6806 break; 6807 6808 case SCMD_READ_POSITION: 6809 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 6810 "read position\n"); 6811 switch (un->un_read_pos_type) { 6812 case LONG_POS: 6813 count = sizeof (tape_position_long_t); 6814 break; 6815 case EXT_POS: 6816 count = min(count, sizeof (tape_position_ext_t)); 6817 break; 6818 case SHORT_POS: 6819 count = sizeof (tape_position_t); 6820 break; 6821 default: 6822 ST_DEBUG(ST_DEVINFO, st_label, CE_PANIC, 6823 "Unknown read position type 0x%x in " 6824 "st_make_cmd()\n", un->un_read_pos_type); 6825 } 6826 bp->b_bcount = count; 6827 bp->b_flags |= B_READ; 6828 bp->b_un.b_addr = (caddr_t)un->un_read_pos_data; 6829 break; 6830 6831 default: 6832 ST_DEBUG(ST_DEVINFO, st_label, CE_PANIC, 6833 "Unhandled scsi command 0x%x in st_cmd()\n", com); 6834 } 6835 6836 mutex_exit(ST_MUTEX); 6837 6838 if (count > 0) { 6839 /* 6840 * We're going to do actual I/O. 6841 * Set things up for physio. 6842 */ 6843 struct iovec aiov; 6844 struct uio auio; 6845 struct uio *uio = &auio; 6846 6847 bzero(&auio, sizeof (struct uio)); 6848 bzero(&aiov, sizeof (struct iovec)); 6849 aiov.iov_base = bp->b_un.b_addr; 6850 aiov.iov_len = count; 6851 6852 uio->uio_iov = &aiov; 6853 uio->uio_iovcnt = 1; 6854 uio->uio_resid = aiov.iov_len; 6855 uio->uio_segflg = UIO_SYSSPACE; 6856 6857 /* 6858 * Let physio do the rest... 6859 */ 6860 bp->b_forw = (struct buf *)(uintptr_t)com; 6861 bp->b_back = NULL; 6862 err = physio(st_strategy, bp, dev, 6863 (bp->b_flags & B_READ) ? B_READ : B_WRITE, 6864 st_minphys, uio); 6865 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 6866 "st_cmd: physio returns %d\n", err); 6867 } else { 6868 /* 6869 * Mimic physio 6870 */ 6871 bp->b_forw = (struct buf *)(uintptr_t)com; 6872 bp->b_back = NULL; 6873 bp->b_edev = dev; 6874 bp->b_dev = cmpdev(dev); 6875 bp->b_blkno = 0; 6876 bp->b_resid = 0; 6877 (void) st_strategy(bp); 6878 if (!wait) { 6879 /* 6880 * This is an async command- the caller won't wait 6881 * and doesn't care about errors. 6882 */ 6883 mutex_enter(ST_MUTEX); 6884 return (0); 6885 } 6886 6887 /* 6888 * BugTraq #4260046 6889 * ---------------- 6890 * Restore Solaris 2.5.1 behavior, namely call biowait 6891 * unconditionally. The old comment said... 6892 * 6893 * "if strategy was flagged with persistent errors, we would 6894 * have an error here, and the bp would never be sent, so we 6895 * don't want to wait on a bp that was never sent...or hang" 6896 * 6897 * The new rationale, courtesy of Chitrank... 6898 * 6899 * "we should unconditionally biowait() here because 6900 * st_strategy() will do a biodone() in the persistent error 6901 * case and the following biowait() will return immediately. 6902 * If not, in the case of "errors after pkt alloc" in 6903 * st_start(), we will not biowait here which will cause the 6904 * next biowait() to return immediately which will cause 6905 * us to send out the next command. In the case where both of 6906 * these use the sbuf, when the first command completes we'll 6907 * free the packet attached to sbuf and the same pkt will 6908 * get freed again when we complete the second command. 6909 * see esc 518987. BTW, it is necessary to do biodone() in 6910 * st_start() for the pkt alloc failure case because physio() 6911 * does biowait() and will hang if we don't do biodone()" 6912 */ 6913 6914 err = biowait(bp); 6915 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 6916 "st_cmd: biowait returns %d\n", err); 6917 } 6918 mutex_enter(ST_MUTEX); 6919 6920 un->un_sbuf_busy = 0; 6921 6922 /* 6923 * If was a space command need to update logical block position. 6924 * If the command failed such that positioning is invalid, Don't 6925 * update the position as the user must do this to validate the 6926 * position for data protection. 6927 */ 6928 if ((com == SCMD_SPACE) && (un->un_pos.pmode != invalid)) { 6929 uchar_t status = un->un_status; 6930 (void) st_update_block_pos(un); 6931 un->un_status = status; 6932 } 6933 6934 cv_signal(&un->un_sbuf_cv); 6935 return (err); 6936 } 6937 6938 static int 6939 st_set_compression(struct scsi_tape *un) 6940 { 6941 int rval; 6942 int turn_compression_on; 6943 minor_t minor; 6944 6945 ST_FUNC(ST_DEVINFO, st_set_compression); 6946 6947 /* 6948 * Drive either dosn't have compression or it is controlled with 6949 * special density codes. Return ENOTTY so caller 6950 * knows nothing was done. 6951 */ 6952 if ((un->un_dp->options & ST_MODE_SEL_COMP) == 0) { 6953 un->un_comp_page = 0; 6954 return (ENOTTY); 6955 } 6956 6957 /* set compression based on minor node opened */ 6958 minor = MT_DENSITY(un->un_dev); 6959 6960 /* 6961 * If this the compression density or 6962 * the drive has two densities and uses mode select for 6963 * control of compression turn on compression for MT_DENSITY2 6964 * as well. 6965 */ 6966 if ((minor == ST_COMPRESSION_DENSITY) || 6967 (minor == MT_DENSITY(MT_DENSITY2)) && 6968 (un->un_dp->densities[0] == un->un_dp->densities[1]) && 6969 (un->un_dp->densities[2] == un->un_dp->densities[3]) && 6970 (un->un_dp->densities[0] != un->un_dp->densities[2])) { 6971 6972 turn_compression_on = 1; 6973 } else { 6974 turn_compression_on = 0; 6975 } 6976 6977 un->un_mspl->high_bl = (uchar_t)(un->un_bsize >> 16); 6978 un->un_mspl->mid_bl = (uchar_t)(un->un_bsize >> 8); 6979 un->un_mspl->low_bl = (uchar_t)(un->un_bsize); 6980 6981 /* 6982 * Need to determine which page does the device use for compression. 6983 * First try the data compression page. If this fails try the device 6984 * configuration page 6985 */ 6986 6987 if ((un->un_comp_page & ST_DEV_DATACOMP_PAGE) == ST_DEV_DATACOMP_PAGE) { 6988 rval = st_set_datacomp_page(un, turn_compression_on); 6989 if (rval == EALREADY) { 6990 return (rval); 6991 } 6992 if (rval != 0) { 6993 if (un->un_status == KEY_ILLEGAL_REQUEST) { 6994 /* 6995 * This device does not support data 6996 * compression page 6997 */ 6998 un->un_comp_page = ST_DEV_CONFIG_PAGE; 6999 } else if (un->un_state >= ST_STATE_OPEN) { 7000 un->un_pos.pmode = invalid; 7001 rval = EIO; 7002 } else { 7003 rval = -1; 7004 } 7005 } else { 7006 un->un_comp_page = ST_DEV_DATACOMP_PAGE; 7007 } 7008 } 7009 7010 if ((un->un_comp_page & ST_DEV_CONFIG_PAGE) == ST_DEV_CONFIG_PAGE) { 7011 rval = st_set_devconfig_page(un, turn_compression_on); 7012 if (rval == EALREADY) { 7013 return (rval); 7014 } 7015 if (rval != 0) { 7016 if (un->un_status == KEY_ILLEGAL_REQUEST) { 7017 /* 7018 * This device does not support 7019 * compression at all advice the 7020 * user and unset ST_MODE_SEL_COMP 7021 */ 7022 un->un_dp->options &= ~ST_MODE_SEL_COMP; 7023 un->un_comp_page = 0; 7024 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 7025 "Device Does Not Support Compression\n"); 7026 } else if (un->un_state >= ST_STATE_OPEN) { 7027 un->un_pos.pmode = invalid; 7028 rval = EIO; 7029 } else { 7030 rval = -1; 7031 } 7032 } 7033 } 7034 7035 return (rval); 7036 } 7037 7038 /* 7039 * set or unset compression thru device configuration page. 7040 */ 7041 static int 7042 st_set_devconfig_page(struct scsi_tape *un, int compression_on) 7043 { 7044 unsigned char cflag; 7045 int rval = 0; 7046 7047 7048 ST_FUNC(ST_DEVINFO, st_set_devconfig_page); 7049 7050 ASSERT(mutex_owned(ST_MUTEX)); 7051 /* 7052 * Figure what to set compression flag to. 7053 */ 7054 if (compression_on) { 7055 /* They have selected a compression node */ 7056 if (un->un_dp->type == ST_TYPE_FUJI) { 7057 cflag = 0x84; /* use EDRC */ 7058 } else { 7059 cflag = ST_DEV_CONFIG_DEF_COMP; 7060 } 7061 } else { 7062 cflag = ST_DEV_CONFIG_NO_COMP; 7063 } 7064 7065 /* 7066 * If compression is already set the way it was requested. 7067 * And if this not the first time we has tried. 7068 */ 7069 if ((cflag == un->un_mspl->page.dev.comp_alg) && 7070 (un->un_comp_page == ST_DEV_DATACOMP_PAGE)) { 7071 return (EALREADY); 7072 } 7073 7074 un->un_mspl->page.dev.comp_alg = cflag; 7075 /* 7076 * need to send mode select even if correct compression is 7077 * already set since need to set density code 7078 */ 7079 7080 #ifdef STDEBUG 7081 if ((st_debug & 0xf) >= 6) { 7082 st_clean_print(ST_DEVINFO, st_label, SCSI_DEBUG, 7083 "st_set_devconfig_page: sense data for mode select", 7084 (char *)un->un_mspl, sizeof (struct seq_mode)); 7085 } 7086 #endif 7087 rval = st_gen_mode_select(un, un->un_mspl, sizeof (struct seq_mode)); 7088 7089 return (rval); 7090 } 7091 7092 /* 7093 * set/reset compression bit thru data compression page 7094 */ 7095 static int 7096 st_set_datacomp_page(struct scsi_tape *un, int compression_on) 7097 { 7098 int compression_on_already; 7099 int rval = 0; 7100 7101 7102 ST_FUNC(ST_DEVINFO, st_set_datacomp_page); 7103 7104 ASSERT(mutex_owned(ST_MUTEX)); 7105 /* 7106 * If drive is not capable of compression (at this time) 7107 * return EALREADY so caller doesn't think that this page 7108 * is not supported. This check is for drives that can 7109 * disable compression from the front panel or configuration. 7110 * I doubt that a drive that supports this page is not really 7111 * capable of compression. 7112 */ 7113 if (un->un_mspl->page.comp.dcc == 0) { 7114 return (EALREADY); 7115 } 7116 7117 /* See if compression currently turned on */ 7118 if (un->un_mspl->page.comp.dce) { 7119 compression_on_already = 1; 7120 } else { 7121 compression_on_already = 0; 7122 } 7123 7124 /* 7125 * If compression is already set the way it was requested. 7126 * And if this not the first time we has tried. 7127 */ 7128 if ((compression_on == compression_on_already) && 7129 (un->un_comp_page == ST_DEV_DATACOMP_PAGE)) { 7130 return (EALREADY); 7131 } 7132 7133 /* 7134 * if we are already set to the appropriate compression 7135 * mode, don't set it again 7136 */ 7137 if (compression_on) { 7138 /* compression selected */ 7139 un->un_mspl->page.comp.dce = 1; 7140 } else { 7141 un->un_mspl->page.comp.dce = 0; 7142 } 7143 7144 7145 #ifdef STDEBUG 7146 if ((st_debug & 0xf) >= 6) { 7147 st_clean_print(ST_DEVINFO, st_label, SCSI_DEBUG, 7148 "st_set_datacomp_page: sense data for mode select", 7149 (char *)un->un_mspl, sizeof (struct seq_mode)); 7150 } 7151 #endif 7152 rval = st_gen_mode_select(un, un->un_mspl, sizeof (struct seq_mode)); 7153 7154 return (rval); 7155 } 7156 7157 static int 7158 st_modesense(struct scsi_tape *un) 7159 { 7160 int rval; 7161 uchar_t page; 7162 7163 ST_FUNC(ST_DEVINFO, st_modesense); 7164 7165 page = un->un_comp_page; 7166 7167 switch (page) { 7168 case ST_DEV_DATACOMP_PAGE: 7169 case ST_DEV_CONFIG_PAGE: /* fall through */ 7170 rval = st_gen_mode_sense(un, page, un->un_mspl, 7171 sizeof (struct seq_mode)); 7172 break; 7173 7174 case ST_DEV_DATACOMP_PAGE | ST_DEV_CONFIG_PAGE: 7175 if (un->un_dp->options & ST_MODE_SEL_COMP) { 7176 page = ST_DEV_DATACOMP_PAGE; 7177 rval = st_gen_mode_sense(un, page, un->un_mspl, 7178 sizeof (struct seq_mode)); 7179 if (rval == 0 && un->un_mspl->page_code == page) { 7180 un->un_comp_page = page; 7181 break; 7182 } 7183 page = ST_DEV_CONFIG_PAGE; 7184 rval = st_gen_mode_sense(un, page, un->un_mspl, 7185 sizeof (struct seq_mode)); 7186 if (rval == 0 && un->un_mspl->page_code == page) { 7187 un->un_comp_page = page; 7188 break; 7189 } 7190 un->un_dp->options &= ~ST_MODE_SEL_COMP; 7191 un->un_comp_page = 0; 7192 } else { 7193 un->un_comp_page = 0; 7194 } 7195 7196 default: /* fall through */ 7197 rval = st_cmd(un->un_dev, SCMD_MODE_SENSE, MSIZE, SYNC_CMD); 7198 } 7199 return (rval); 7200 } 7201 7202 static int 7203 st_modeselect(struct scsi_tape *un) 7204 { 7205 int rval = 0; 7206 int ix; 7207 7208 ST_FUNC(ST_DEVINFO, st_modeselect); 7209 7210 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 7211 "st_modeselect(dev = 0x%lx): density = 0x%x\n", 7212 un->un_dev, un->un_mspl->density); 7213 7214 ASSERT(mutex_owned(ST_MUTEX)); 7215 7216 /* 7217 * The parameter list should be the same for all of the 7218 * cases that follow so set them here 7219 * 7220 * Try mode select first if if fails set fields manually 7221 */ 7222 rval = st_modesense(un); 7223 if (rval != 0) { 7224 ST_DEBUG3(ST_DEVINFO, st_label, CE_WARN, 7225 "st_modeselect: First mode sense failed\n"); 7226 un->un_mspl->bd_len = 8; 7227 un->un_mspl->high_nb = 0; 7228 un->un_mspl->mid_nb = 0; 7229 un->un_mspl->low_nb = 0; 7230 } 7231 un->un_mspl->high_bl = (uchar_t)(un->un_bsize >> 16); 7232 un->un_mspl->mid_bl = (uchar_t)(un->un_bsize >> 8); 7233 un->un_mspl->low_bl = (uchar_t)(un->un_bsize); 7234 7235 7236 /* 7237 * If configured to use a specific density code for a media type. 7238 * curdens is previously set by the minor node opened. 7239 * If the media type doesn't match the minor node we change it so it 7240 * looks like the correct one was opened. 7241 */ 7242 if (un->un_dp->options & ST_KNOWS_MEDIA) { 7243 uchar_t best; 7244 7245 for (best = 0xff, ix = 0; ix < NDENSITIES; ix++) { 7246 if (un->un_mspl->media_type == 7247 un->un_dp->mediatype[ix]) { 7248 best = ix; 7249 /* 7250 * It matches but it might not be the only one. 7251 * Use the highest matching media type but not 7252 * to exceed the density selected by the open. 7253 */ 7254 if (ix < un->un_curdens) { 7255 continue; 7256 } 7257 un->un_curdens = ix; 7258 break; 7259 } 7260 } 7261 /* If a match was found best will not be 0xff any more */ 7262 if (best < NDENSITIES) { 7263 ST_DEBUG3(ST_DEVINFO, st_label, CE_WARN, 7264 "found media 0x%X using density 0x%X\n", 7265 un->un_mspl->media_type, 7266 un->un_dp->densities[best]); 7267 un->un_mspl->density = un->un_dp->densities[best]; 7268 } else { 7269 /* Otherwise set density based on minor node opened */ 7270 un->un_mspl->density = 7271 un->un_dp->densities[un->un_curdens]; 7272 } 7273 } else { 7274 un->un_mspl->density = un->un_dp->densities[un->un_curdens]; 7275 } 7276 7277 if (un->un_dp->options & ST_NOBUF) { 7278 un->un_mspl->bufm = 0; 7279 } else { 7280 un->un_mspl->bufm = 1; 7281 } 7282 7283 rval = st_set_compression(un); 7284 7285 /* 7286 * If st_set_compression returned invalid or already it 7287 * found no need to do the mode select. 7288 * So do it here. 7289 */ 7290 if ((rval == ENOTTY) || (rval == EALREADY)) { 7291 7292 /* Zero non-writeable fields */ 7293 un->un_mspl->data_len = 0; 7294 un->un_mspl->media_type = 0; 7295 un->un_mspl->wp = 0; 7296 7297 /* need to set the density code */ 7298 rval = st_cmd(un->un_dev, SCMD_MODE_SELECT, MSIZE, SYNC_CMD); 7299 if (rval != 0) { 7300 if (un->un_state >= ST_STATE_OPEN) { 7301 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 7302 "unable to set tape mode\n"); 7303 un->un_pos.pmode = invalid; 7304 rval = EIO; 7305 } else { 7306 rval = -1; 7307 } 7308 } 7309 } 7310 7311 /* 7312 * The spec recommends to send a mode sense after a mode select 7313 */ 7314 (void) st_modesense(un); 7315 7316 ASSERT(mutex_owned(ST_MUTEX)); 7317 7318 return (rval); 7319 } 7320 7321 /* 7322 * st_gen_mode_sense 7323 * 7324 * generic mode sense.. it allows for any page 7325 */ 7326 static int 7327 st_gen_mode_sense(struct scsi_tape *un, int page, struct seq_mode *page_data, 7328 int page_size) 7329 { 7330 7331 int r; 7332 char cdb[CDB_GROUP0]; 7333 struct uscsi_cmd *com; 7334 7335 ST_FUNC(ST_DEVINFO, st_gen_mode_sense); 7336 7337 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 7338 7339 bzero(cdb, CDB_GROUP0); 7340 cdb[0] = SCMD_MODE_SENSE; 7341 cdb[2] = (char)page; 7342 cdb[4] = (char)page_size; 7343 7344 com->uscsi_cdb = cdb; 7345 com->uscsi_cdblen = CDB_GROUP0; 7346 com->uscsi_bufaddr = (caddr_t)page_data; 7347 com->uscsi_buflen = page_size; 7348 com->uscsi_timeout = un->un_dp->non_motion_timeout; 7349 com->uscsi_flags = USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ; 7350 7351 r = st_ioctl_cmd(un->un_dev, com, FKIOCTL); 7352 kmem_free(com, sizeof (*com)); 7353 return (r); 7354 } 7355 7356 /* 7357 * st_gen_mode_select 7358 * 7359 * generic mode select.. it allows for any page 7360 */ 7361 static int 7362 st_gen_mode_select(struct scsi_tape *un, struct seq_mode *page_data, 7363 int page_size) 7364 { 7365 7366 int r; 7367 char cdb[CDB_GROUP0]; 7368 struct uscsi_cmd *com; 7369 7370 ST_FUNC(ST_DEVINFO, st_gen_mode_select); 7371 7372 /* Zero non-writeable fields */ 7373 page_data->data_len = 0; 7374 page_data->media_type = 0; 7375 page_data->wp = 0; 7376 7377 /* 7378 * If mode select has any page data, zero the ps (Page Savable) bit. 7379 */ 7380 if (page_size > MSIZE) { 7381 page_data->ps = 0; 7382 } 7383 7384 7385 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 7386 7387 /* 7388 * then, do a mode select to set what ever info 7389 */ 7390 bzero(cdb, CDB_GROUP0); 7391 cdb[0] = SCMD_MODE_SELECT; 7392 cdb[1] = 0x10; /* set PF bit for many third party drives */ 7393 cdb[4] = (char)page_size; 7394 7395 com->uscsi_cdb = cdb; 7396 com->uscsi_cdblen = CDB_GROUP0; 7397 com->uscsi_bufaddr = (caddr_t)page_data; 7398 com->uscsi_buflen = page_size; 7399 com->uscsi_timeout = un->un_dp->non_motion_timeout; 7400 com->uscsi_flags = USCSI_DIAGNOSE | USCSI_SILENT | USCSI_WRITE; 7401 7402 r = st_ioctl_cmd(un->un_dev, com, FKIOCTL); 7403 7404 kmem_free(com, sizeof (*com)); 7405 return (r); 7406 } 7407 7408 /* 7409 * Changes devices blocksize and bsize to requested blocksize nblksz. 7410 * Returns returned value from first failed call or zero on success. 7411 */ 7412 static int 7413 st_change_block_size(dev_t dev, uint32_t nblksz) 7414 { 7415 struct seq_mode *current; 7416 int rval; 7417 uint32_t oldblksz; 7418 7419 GET_SOFT_STATE(dev); 7420 7421 ST_FUNC(ST_DEVINFO, st_change_block_size); 7422 7423 current = kmem_zalloc(MSIZE, KM_SLEEP); 7424 7425 /* Read current settings */ 7426 rval = st_gen_mode_sense(un, 0, current, MSIZE); 7427 if (rval != 0) { 7428 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 7429 "mode sense for change block size failed: rval = %d", rval); 7430 goto finish; 7431 } 7432 7433 /* Figure the current block size */ 7434 oldblksz = 7435 (current->high_bl << 16) | 7436 (current->mid_bl << 8) | 7437 (current->low_bl); 7438 7439 /* If current block size is the same as requested were done */ 7440 if (oldblksz == nblksz) { 7441 un->un_bsize = nblksz; 7442 rval = 0; 7443 goto finish; 7444 } 7445 7446 /* Change to requested block size */ 7447 current->high_bl = (uchar_t)(nblksz >> 16); 7448 current->mid_bl = (uchar_t)(nblksz >> 8); 7449 current->low_bl = (uchar_t)(nblksz); 7450 7451 /* Attempt to change block size */ 7452 rval = st_gen_mode_select(un, current, MSIZE); 7453 if (rval != 0) { 7454 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 7455 "Set new block size failed: rval = %d", rval); 7456 goto finish; 7457 } 7458 7459 /* Read back and verify setting */ 7460 rval = st_modesense(un); 7461 if (rval == 0) { 7462 un->un_bsize = 7463 (un->un_mspl->high_bl << 16) | 7464 (un->un_mspl->mid_bl << 8) | 7465 (un->un_mspl->low_bl); 7466 7467 if (un->un_bsize != nblksz) { 7468 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 7469 "Blocksize set does not equal requested blocksize" 7470 "(read: %u requested: %u)\n", nblksz, un->un_bsize); 7471 rval = EIO; 7472 } 7473 } 7474 finish: 7475 kmem_free(current, MSIZE); 7476 return (rval); 7477 } 7478 7479 7480 static void 7481 st_init(struct scsi_tape *un) 7482 { 7483 ST_FUNC(ST_DEVINFO, st_init); 7484 7485 ASSERT(mutex_owned(ST_MUTEX)); 7486 7487 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 7488 "st_init(): dev = 0x%lx, will reset fileno, blkno, eof\n", 7489 un->un_dev); 7490 7491 un->un_pos.blkno = 0; 7492 un->un_pos.fileno = 0; 7493 un->un_lastop = ST_OP_NIL; 7494 un->un_pos.eof = ST_NO_EOF; 7495 un->un_pwr_mgmt = ST_PWR_NORMAL; 7496 if (st_error_level != SCSI_ERR_ALL) { 7497 if (DEBUGGING) { 7498 st_error_level = SCSI_ERR_ALL; 7499 } else { 7500 st_error_level = SCSI_ERR_RETRYABLE; 7501 } 7502 } 7503 } 7504 7505 7506 static void 7507 st_make_cmd(struct scsi_tape *un, struct buf *bp, int (*func)(caddr_t)) 7508 { 7509 struct scsi_pkt *pkt; 7510 struct uscsi_cmd *ucmd; 7511 int count, tval = 0; 7512 uint_t addr = 0; 7513 int flags = 0; 7514 int cdb_len = CDB_GROUP0; /* default */ 7515 uchar_t com; 7516 char fixbit; 7517 7518 ST_FUNC(ST_DEVINFO, st_make_cmd); 7519 7520 ASSERT(mutex_owned(ST_MUTEX)); 7521 7522 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 7523 "st_make_cmd(): dev = 0x%lx\n", un->un_dev); 7524 7525 7526 /* 7527 * fixbit is for setting the Fixed Mode and Suppress Incorrect 7528 * Length Indicator bits on read/write commands, for setting 7529 * the Long bit on erase commands, and for setting the Code 7530 * Field bits on space commands. 7531 * XXX why do we set lastop here? 7532 */ 7533 7534 if (bp != un->un_sbufp) { /* regular raw I/O */ 7535 int stat_size = (un->un_arq_enabled ? 7536 sizeof (struct scsi_arq_status) : 1); 7537 pkt = scsi_init_pkt(ROUTE, NULL, bp, 7538 CDB_GROUP0, stat_size, 0, 0, func, (caddr_t)un); 7539 if (pkt == NULL) { 7540 goto exit; 7541 } 7542 SET_BP_PKT(bp, pkt); 7543 if (un->un_bsize == 0) { 7544 count = bp->b_bcount; 7545 fixbit = 0; 7546 } else { 7547 count = bp->b_bcount / un->un_bsize; 7548 fixbit = 1; 7549 } 7550 if (bp->b_flags & B_READ) { 7551 com = SCMD_READ; 7552 un->un_lastop = ST_OP_READ; 7553 if ((un->un_bsize == 0) && /* Not Fixed Block */ 7554 (un->un_dp->options & ST_READ_IGNORE_ILI)) { 7555 fixbit = 2; 7556 } 7557 } else { 7558 com = SCMD_WRITE; 7559 un->un_lastop = ST_OP_WRITE; 7560 } 7561 7562 tval = un->un_dp->io_timeout; 7563 7564 /* 7565 * For really large xfers, increase timeout 7566 */ 7567 if (bp->b_bcount > (10 * ONE_MEG)) 7568 tval *= bp->b_bcount/(10 * ONE_MEG); 7569 7570 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7571 "%s %d amt 0x%lx\n", (com == SCMD_WRITE) ? 7572 wr_str: rd_str, un->un_pos.blkno, bp->b_bcount); 7573 7574 } else if ((ucmd = BP_UCMD(bp)) != NULL) { 7575 /* 7576 * uscsi - build command, allocate scsi resources 7577 */ 7578 st_make_uscsi_cmd(un, ucmd, bp, func); 7579 goto exit; 7580 7581 } else { /* special I/O */ 7582 int stat_size = (un->un_arq_enabled ? 7583 sizeof (struct scsi_arq_status) : 1); 7584 struct buf *allocbp = NULL; 7585 com = (uchar_t)(uintptr_t)bp->b_forw; 7586 count = bp->b_bcount; 7587 7588 switch (com) { 7589 case SCMD_READ: 7590 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7591 "special read %d\n", count); 7592 if (un->un_bsize == 0) { 7593 fixbit = 2; /* suppress SILI */ 7594 } else { 7595 fixbit = 1; /* Fixed Block Mode */ 7596 count /= un->un_bsize; 7597 } 7598 allocbp = bp; 7599 un->un_lastop = ST_OP_READ; 7600 tval = un->un_dp->io_timeout; 7601 break; 7602 7603 case SCMD_WRITE: 7604 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7605 "special write %d\n", count); 7606 if (un->un_bsize != 0) { 7607 fixbit = 1; /* Fixed Block Mode */ 7608 count /= un->un_bsize; 7609 } else { 7610 fixbit = 0; 7611 } 7612 allocbp = bp; 7613 un->un_lastop = ST_OP_WRITE; 7614 tval = un->un_dp->io_timeout; 7615 break; 7616 7617 case SCMD_WRITE_FILE_MARK: 7618 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7619 "write %d file marks\n", count); 7620 un->un_lastop = ST_OP_WEOF; 7621 fixbit = 0; 7622 tval = un->un_dp->io_timeout; 7623 break; 7624 7625 case SCMD_REWIND: 7626 if (bp->b_flags & B_ASYNC) { 7627 fixbit = 1; 7628 } else { 7629 fixbit = 0; 7630 } 7631 count = 0; 7632 un->un_lastop = ST_OP_CTL; 7633 tval = un->un_dp->rewind_timeout; 7634 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7635 "rewind\n"); 7636 break; 7637 7638 case SCMD_SPACE: 7639 fixbit = SPACE_TYPE(count); 7640 count = (int)SPACE_CNT(count); 7641 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7642 "space %s %d from file %d blk %d\n", 7643 space_strs[fixbit & 7], count, 7644 un->un_pos.fileno, un->un_pos.blkno); 7645 un->un_lastop = ST_OP_CTL; 7646 tval = un->un_dp->space_timeout; 7647 break; 7648 7649 case SCMD_LOAD: 7650 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7651 "%s tape\n", (count & LD_LOAD) ? "load" : "unload"); 7652 fixbit = 0; 7653 7654 /* Loading or Unloading */ 7655 if (count & LD_LOAD) { 7656 tval = un->un_dp->load_timeout; 7657 } else { 7658 tval = un->un_dp->unload_timeout; 7659 } 7660 /* Is Retension requested */ 7661 if (count & LD_RETEN) { 7662 tval += un->un_dp->rewind_timeout; 7663 } 7664 un->un_lastop = ST_OP_CTL; 7665 break; 7666 7667 case SCMD_ERASE: 7668 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7669 "erase tape\n"); 7670 count = 0; 7671 /* 7672 * We support long erase only 7673 */ 7674 fixbit = 1; 7675 tval = un->un_dp->erase_timeout; 7676 un->un_lastop = ST_OP_CTL; 7677 break; 7678 7679 case SCMD_MODE_SENSE: 7680 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7681 "mode sense\n"); 7682 allocbp = bp; 7683 fixbit = 0; 7684 tval = un->un_dp->non_motion_timeout; 7685 un->un_lastop = ST_OP_CTL; 7686 break; 7687 7688 case SCMD_MODE_SELECT: 7689 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7690 "mode select\n"); 7691 allocbp = bp; 7692 fixbit = 0; 7693 tval = un->un_dp->non_motion_timeout; 7694 un->un_lastop = ST_OP_CTL; 7695 break; 7696 7697 case SCMD_RESERVE: 7698 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7699 "reserve\n"); 7700 fixbit = 0; 7701 tval = un->un_dp->non_motion_timeout; 7702 un->un_lastop = ST_OP_CTL; 7703 break; 7704 7705 case SCMD_RELEASE: 7706 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7707 "release\n"); 7708 fixbit = 0; 7709 tval = un->un_dp->non_motion_timeout; 7710 un->un_lastop = ST_OP_CTL; 7711 break; 7712 7713 case SCMD_READ_BLKLIM: 7714 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7715 "read block limits\n"); 7716 allocbp = bp; 7717 fixbit = count = 0; 7718 tval = un->un_dp->non_motion_timeout; 7719 un->un_lastop = ST_OP_CTL; 7720 break; 7721 7722 case SCMD_TEST_UNIT_READY: 7723 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7724 "test unit ready\n"); 7725 fixbit = 0; 7726 tval = un->un_dp->non_motion_timeout; 7727 un->un_lastop = ST_OP_CTL; 7728 break; 7729 7730 case SCMD_DOORLOCK: 7731 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7732 "prevent/allow media removal\n"); 7733 fixbit = 0; 7734 tval = un->un_dp->non_motion_timeout; 7735 un->un_lastop = ST_OP_CTL; 7736 break; 7737 7738 case SCMD_READ_POSITION: 7739 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7740 "read position\n"); 7741 fixbit = un->un_read_pos_type; 7742 cdb_len = CDB_GROUP1; 7743 tval = un->un_dp->non_motion_timeout; 7744 allocbp = bp; 7745 un->un_lastop = ST_OP_CTL; 7746 switch (un->un_read_pos_type) { 7747 case LONG_POS: 7748 count = 0; 7749 break; 7750 case EXT_POS: 7751 count = sizeof (tape_position_ext_t); 7752 break; 7753 case SHORT_POS: 7754 count = 0; 7755 break; 7756 default: 7757 ST_DEBUG(ST_DEVINFO, st_label, CE_PANIC, 7758 "Unknown read position type 0x%x in " 7759 " st_make_cmd()\n", un->un_read_pos_type); 7760 } 7761 break; 7762 7763 default: 7764 ST_DEBUG(ST_DEVINFO, st_label, CE_PANIC, 7765 "Unhandled scsi command 0x%x in st_make_cmd()\n", 7766 com); 7767 } 7768 pkt = scsi_init_pkt(ROUTE, NULL, allocbp, cdb_len, stat_size, 7769 0, 0, func, (caddr_t)un); 7770 if (pkt == NULL) { 7771 goto exit; 7772 } 7773 if (allocbp) { 7774 ASSERT(geterror(allocbp) == 0); 7775 } 7776 7777 } 7778 7779 7780 (void) scsi_setup_cdb((union scsi_cdb *)pkt->pkt_cdbp, 7781 com, addr, (uint_t)count, 0); 7782 FILL_SCSI1_LUN(un->un_sd, pkt); 7783 /* 7784 * Initialize the SILI/Fixed bits of the byte 1 of cdb. 7785 */ 7786 ((union scsi_cdb *)(pkt->pkt_cdbp))->t_code = fixbit; 7787 pkt->pkt_flags = flags; 7788 7789 #ifdef STDEBUG 7790 if ((st_debug & 0xf) >= 6) { 7791 st_clean_print(ST_DEVINFO, st_label, SCSI_DEBUG, 7792 "cmd cdb", (char *)pkt->pkt_cdbp, cdb_len); 7793 } 7794 #endif 7795 7796 /* 7797 * If ST_SHORT_FILEMARKS bit is ON for EXABYTE 7798 * device, set the Vendor Unique bit to 7799 * write Short File Mark. 7800 */ 7801 if (com == SCMD_WRITE_FILE_MARK && 7802 un->un_dp->options & ST_SHORT_FILEMARKS) { 7803 switch (un->un_dp->type) { 7804 case ST_TYPE_EXB8500: 7805 case ST_TYPE_EXABYTE: 7806 /* 7807 * Now the Vendor Unique bit 7 in Byte 5 of CDB 7808 * is set to to write Short File Mark 7809 */ 7810 ((union scsi_cdb *)pkt->pkt_cdbp)->g0_vu_1 = 1; 7811 break; 7812 7813 default: 7814 /* 7815 * Well, if ST_SHORT_FILEMARKS is set for other 7816 * tape drives, it is just ignored 7817 */ 7818 break; 7819 } 7820 } 7821 ASSERT(tval); 7822 pkt->pkt_time = tval; 7823 pkt->pkt_comp = st_intr; 7824 pkt->pkt_private = (opaque_t)bp; 7825 7826 SET_BP_PKT(bp, pkt); 7827 7828 exit: 7829 ASSERT(mutex_owned(ST_MUTEX)); 7830 } 7831 7832 7833 /* 7834 * Build a command based on a uscsi command; 7835 */ 7836 static void 7837 st_make_uscsi_cmd(struct scsi_tape *un, struct uscsi_cmd *ucmd, 7838 struct buf *bp, int (*func)(caddr_t)) 7839 { 7840 struct scsi_pkt *pkt; 7841 caddr_t cdb; 7842 int cdblen; 7843 int stat_size; 7844 7845 ST_FUNC(ST_DEVINFO, st_make_uscsi_cmd); 7846 7847 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 7848 "st_make_uscsi_cmd(): dev = 0x%lx\n", un->un_dev); 7849 7850 if (ucmd->uscsi_flags & USCSI_RQENABLE) { 7851 stat_size = (un->un_arq_enabled ? 7852 sizeof (struct scsi_arq_status) : 1); 7853 } else { 7854 stat_size = 1; 7855 } 7856 7857 ASSERT(mutex_owned(ST_MUTEX)); 7858 7859 cdb = ucmd->uscsi_cdb; 7860 cdblen = ucmd->uscsi_cdblen; 7861 7862 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 7863 "st_make_uscsi_cmd: buflen=%ld bcount=%ld\n", 7864 ucmd->uscsi_buflen, bp->b_bcount); 7865 pkt = scsi_init_pkt(ROUTE, NULL, 7866 (bp->b_bcount > 0) ? bp : NULL, 7867 cdblen, stat_size, 0, 0, func, (caddr_t)un); 7868 if (pkt == NULL) { 7869 goto exit; 7870 } 7871 7872 bcopy(cdb, pkt->pkt_cdbp, (uint_t)cdblen); 7873 7874 #ifdef STDEBUG 7875 if ((st_debug & 0xf) >= 6) { 7876 st_clean_print(ST_DEVINFO, st_label, SCSI_DEBUG, 7877 "pkt_cdbp", (char *)cdb, cdblen); 7878 } 7879 #endif 7880 7881 if (ucmd->uscsi_flags & USCSI_SILENT) { 7882 pkt->pkt_flags |= FLAG_SILENT; 7883 } 7884 7885 pkt->pkt_time = ucmd->uscsi_timeout; 7886 pkt->pkt_comp = st_intr; 7887 pkt->pkt_private = (opaque_t)bp; 7888 7889 SET_BP_PKT(bp, pkt); 7890 exit: 7891 ASSERT(mutex_owned(ST_MUTEX)); 7892 } 7893 7894 7895 /* 7896 * restart cmd currently at the head of the runq 7897 * 7898 * If scsi_transport() succeeds or the retries 7899 * count exhausted, restore the throttle that was 7900 * zeroed out in st_handle_intr_busy(). 7901 * 7902 */ 7903 static void 7904 st_intr_restart(void *arg) 7905 { 7906 struct scsi_tape *un = arg; 7907 struct buf *bp; 7908 int status = TRAN_ACCEPT; 7909 7910 mutex_enter(ST_MUTEX); 7911 7912 ST_FUNC(ST_DEVINFO, st_intr_restart); 7913 7914 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 7915 "st_intr_restart(), un = 0x%p\n", (void *)un); 7916 7917 un->un_hib_tid = 0; 7918 7919 /* 7920 * move from waitq to runq, if there is anything on the waitq 7921 */ 7922 if ((bp = un->un_quef) == NULL) { 7923 mutex_exit(ST_MUTEX); 7924 return; 7925 } 7926 7927 /* 7928 * Here we know : 7929 * throttle = 0, via st_handle_intr_busy 7930 */ 7931 7932 if (un->un_quel == bp) { 7933 un->un_quel = NULL; 7934 un->un_quef = NULL; /* we know it's the first one */ 7935 } else { 7936 un->un_quef = bp->b_actf; 7937 } 7938 bp->b_actf = NULL; 7939 7940 if (un->un_runqf) { 7941 /* 7942 * not good, we don't want to requeue something after 7943 * another. 7944 */ 7945 mutex_exit(ST_MUTEX); 7946 goto done_error; 7947 } else { 7948 un->un_runqf = bp; 7949 un->un_runql = bp; 7950 } 7951 7952 ST_DO_KSTATS(bp, kstat_waitq_to_runq); 7953 7954 mutex_exit(ST_MUTEX); 7955 7956 status = scsi_transport(BP_PKT(bp)); 7957 7958 mutex_enter(ST_MUTEX); 7959 7960 if (status != TRAN_ACCEPT) { 7961 ST_DO_KSTATS(bp, kstat_runq_back_to_waitq); 7962 mutex_exit(ST_MUTEX); 7963 7964 if (status == TRAN_BUSY) { 7965 if (st_handle_intr_busy(un, bp, 7966 ST_TRAN_BUSY_TIMEOUT) == 0) 7967 return; /* timeout is setup again */ 7968 } 7969 7970 } else { 7971 un->un_tran_retry_ct = 0; 7972 if (un->un_last_throttle) { 7973 un->un_throttle = un->un_last_throttle; 7974 } 7975 mutex_exit(ST_MUTEX); 7976 return; 7977 } 7978 7979 done_error: 7980 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 7981 "restart transport rejected\n"); 7982 bp->b_resid = bp->b_bcount; 7983 7984 #ifndef __lock_lint 7985 /* 7986 * warlock doesn't understand this potential 7987 * recursion? 7988 */ 7989 mutex_enter(ST_MUTEX); 7990 if (un->un_last_throttle) { 7991 un->un_throttle = un->un_last_throttle; 7992 } 7993 if (status != TRAN_ACCEPT) 7994 ST_DO_ERRSTATS(un, st_transerrs); 7995 ST_DO_KSTATS(bp, kstat_waitq_exit); 7996 SET_PE_FLAG(un); 7997 st_bioerror(bp, EIO); 7998 st_done_and_mutex_exit(un, bp); 7999 #endif 8000 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 8001 "busy restart aborted\n"); 8002 } 8003 8004 /* 8005 * st_check_media(): 8006 * Periodically check the media state using scsi_watch service; 8007 * this service calls back after TUR and possibly request sense 8008 * the callback handler (st_media_watch_cb()) decodes the request sense 8009 * data (if any) 8010 */ 8011 8012 static int 8013 st_check_media(dev_t dev, enum mtio_state state) 8014 { 8015 int rval = 0; 8016 enum mtio_state prev_state; 8017 opaque_t token = NULL; 8018 8019 GET_SOFT_STATE(dev); 8020 8021 ST_FUNC(ST_DEVINFO, st_check_media); 8022 8023 mutex_enter(ST_MUTEX); 8024 8025 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 8026 "st_check_media:state=%x, mediastate=%x\n", 8027 state, un->un_mediastate); 8028 8029 prev_state = un->un_mediastate; 8030 8031 /* 8032 * is there anything to do? 8033 */ 8034 retry: 8035 if (state == un->un_mediastate || un->un_mediastate == MTIO_NONE) { 8036 /* 8037 * submit the request to the scsi_watch service; 8038 * scsi_media_watch_cb() does the real work 8039 */ 8040 mutex_exit(ST_MUTEX); 8041 token = scsi_watch_request_submit(ST_SCSI_DEVP, 8042 st_check_media_time, SENSE_LENGTH, 8043 st_media_watch_cb, (caddr_t)dev); 8044 if (token == NULL) { 8045 rval = EAGAIN; 8046 goto done; 8047 } 8048 mutex_enter(ST_MUTEX); 8049 8050 un->un_swr_token = token; 8051 un->un_specified_mediastate = state; 8052 8053 /* 8054 * now wait for media change 8055 * we will not be signalled unless mediastate == state but it 8056 * still better to test for this condition, since there 8057 * is a 5 sec cv_broadcast delay when 8058 * mediastate == MTIO_INSERTED 8059 */ 8060 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 8061 "st_check_media:waiting for media state change\n"); 8062 while (un->un_mediastate == state) { 8063 if (cv_wait_sig(&un->un_state_cv, ST_MUTEX) == 0) { 8064 mutex_exit(ST_MUTEX); 8065 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 8066 "st_check_media:waiting for media state " 8067 "was interrupted\n"); 8068 rval = EINTR; 8069 goto done; 8070 } 8071 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 8072 "st_check_media:received signal, state=%x\n", 8073 un->un_mediastate); 8074 } 8075 } 8076 8077 /* 8078 * if we transitioned to MTIO_INSERTED, media has really been 8079 * inserted. If TUR fails, it is probably a exabyte slow spin up. 8080 * Reset and retry the state change. If everything is ok, replay 8081 * the open() logic. 8082 */ 8083 if ((un->un_mediastate == MTIO_INSERTED) && 8084 (un->un_state == ST_STATE_OFFLINE)) { 8085 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 8086 "st_check_media: calling st_cmd to confirm inserted\n"); 8087 8088 /* 8089 * set this early so that TUR will make it through strategy 8090 * without triggering a st_tape_init(). We needed it set 8091 * before calling st_tape_init() ourselves anyway. If TUR 8092 * fails, set it back 8093 */ 8094 un->un_state = ST_STATE_INITIALIZING; 8095 8096 /* 8097 * If not reserved fail as getting reservation conflict 8098 * will make this hang forever. 8099 */ 8100 if ((un->un_rsvd_status & 8101 (ST_RESERVE | ST_APPLICATION_RESERVATIONS)) == 0) { 8102 mutex_exit(ST_MUTEX); 8103 rval = EACCES; 8104 goto done; 8105 } 8106 rval = st_cmd(dev, SCMD_TEST_UNIT_READY, 0, SYNC_CMD); 8107 if (rval == EACCES) { 8108 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 8109 "st_check_media: TUR got Reservation Conflict\n"); 8110 mutex_exit(ST_MUTEX); 8111 goto done; 8112 } 8113 if (rval) { 8114 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 8115 "st_check_media: TUR failed, going to retry\n"); 8116 un->un_mediastate = prev_state; 8117 un->un_state = ST_STATE_OFFLINE; 8118 goto retry; 8119 } 8120 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 8121 "st_check_media: media inserted\n"); 8122 8123 /* this also rewinds the tape */ 8124 rval = st_tape_init(dev); 8125 if (rval != 0) { 8126 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 8127 "st_check_media : OFFLINE init failure "); 8128 un->un_state = ST_STATE_OFFLINE; 8129 un->un_pos.pmode = invalid; 8130 } else { 8131 un->un_state = ST_STATE_OPEN_PENDING_IO; 8132 un->un_pos.fileno = 0; 8133 un->un_pos.blkno = 0; 8134 un->un_pos.lgclblkno = 0; 8135 } 8136 } else if ((un->un_mediastate == MTIO_EJECTED) && 8137 (un->un_state != ST_STATE_OFFLINE)) { 8138 /* 8139 * supported devices must be rewound before ejection 8140 * rewind resets fileno & blkno 8141 */ 8142 un->un_laststate = un->un_state; 8143 un->un_state = ST_STATE_OFFLINE; 8144 } 8145 mutex_exit(ST_MUTEX); 8146 done: 8147 if (token) { 8148 (void) scsi_watch_request_terminate(token, 8149 SCSI_WATCH_TERMINATE_WAIT); 8150 mutex_enter(ST_MUTEX); 8151 un->un_swr_token = (opaque_t)NULL; 8152 mutex_exit(ST_MUTEX); 8153 } 8154 8155 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, "st_check_media: done\n"); 8156 8157 return (rval); 8158 } 8159 8160 /* 8161 * st_media_watch_cb() is called by scsi_watch_thread for 8162 * verifying the request sense data (if any) 8163 */ 8164 static int 8165 st_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp) 8166 { 8167 struct scsi_status *statusp = resultp->statusp; 8168 struct scsi_extended_sense *sensep = resultp->sensep; 8169 uchar_t actual_sense_length = resultp->actual_sense_length; 8170 struct scsi_tape *un; 8171 enum mtio_state state = MTIO_NONE; 8172 int instance; 8173 dev_t dev = (dev_t)arg; 8174 8175 instance = MTUNIT(dev); 8176 if ((un = ddi_get_soft_state(st_state, instance)) == NULL) { 8177 return (-1); 8178 } 8179 8180 mutex_enter(ST_MUTEX); 8181 ST_FUNC(ST_DEVINFO, st_media_watch_cb); 8182 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 8183 "st_media_watch_cb: status=%x, sensep=%p, len=%x\n", 8184 *((char *)statusp), (void *)sensep, 8185 actual_sense_length); 8186 8187 8188 /* 8189 * if there was a check condition then sensep points to valid 8190 * sense data 8191 * if status was not a check condition but a reservation or busy 8192 * status then the new state is MTIO_NONE 8193 */ 8194 if (sensep) { 8195 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 8196 "st_media_watch_cb: KEY=%x, ASC=%x, ASCQ=%x\n", 8197 sensep->es_key, sensep->es_add_code, sensep->es_qual_code); 8198 8199 switch (un->un_dp->type) { 8200 default: 8201 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 8202 "st_media_watch_cb: unknown drive type %d, " 8203 "default to ST_TYPE_HP\n", un->un_dp->type); 8204 /* FALLTHROUGH */ 8205 8206 case ST_TYPE_STC3490: /* STK 4220 1/2" cartridge */ 8207 case ST_TYPE_FUJI: /* 1/2" cartridge */ 8208 case ST_TYPE_HP: /* HP 88780 1/2" reel */ 8209 if (un->un_dp->type == ST_TYPE_FUJI) { 8210 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 8211 "st_media_watch_cb: ST_TYPE_FUJI\n"); 8212 } else { 8213 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 8214 "st_media_watch_cb: ST_TYPE_HP\n"); 8215 } 8216 switch (sensep->es_key) { 8217 case KEY_UNIT_ATTENTION: 8218 /* not ready to ready transition */ 8219 /* hp/es_qual_code == 80 on>off>on */ 8220 /* hp/es_qual_code == 0 on>off>unld>ld>on */ 8221 if (sensep->es_add_code == 0x28) { 8222 state = MTIO_INSERTED; 8223 } 8224 break; 8225 case KEY_NOT_READY: 8226 /* in process, rewinding or loading */ 8227 if ((sensep->es_add_code == 0x04) && 8228 (sensep->es_qual_code == 0x00)) { 8229 state = MTIO_EJECTED; 8230 } 8231 break; 8232 } 8233 break; 8234 8235 case ST_TYPE_EXB8500: /* Exabyte 8500 */ 8236 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 8237 "st_media_watch_cb: ST_TYPE_EXB8500\n"); 8238 switch (sensep->es_key) { 8239 case KEY_UNIT_ATTENTION: 8240 /* operator medium removal request */ 8241 if ((sensep->es_add_code == 0x5a) && 8242 (sensep->es_qual_code == 0x01)) { 8243 state = MTIO_EJECTED; 8244 /* not ready to ready transition */ 8245 } else if ((sensep->es_add_code == 0x28) && 8246 (sensep->es_qual_code == 0x00)) { 8247 state = MTIO_INSERTED; 8248 } 8249 break; 8250 case KEY_NOT_READY: 8251 /* medium not present */ 8252 if (sensep->es_add_code == 0x3a) { 8253 state = MTIO_EJECTED; 8254 } 8255 break; 8256 } 8257 break; 8258 case ST_TYPE_EXABYTE: /* Exabyte 8200 */ 8259 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 8260 "st_media_watch_cb: ST_TYPE_EXABYTE\n"); 8261 switch (sensep->es_key) { 8262 case KEY_NOT_READY: 8263 if ((sensep->es_add_code == 0x04) && 8264 (sensep->es_qual_code == 0x00)) { 8265 /* volume not mounted? */ 8266 state = MTIO_EJECTED; 8267 } else if (sensep->es_add_code == 0x3a) { 8268 state = MTIO_EJECTED; 8269 } 8270 break; 8271 case KEY_UNIT_ATTENTION: 8272 state = MTIO_EJECTED; 8273 break; 8274 } 8275 break; 8276 8277 case ST_TYPE_DLT: /* quantum DLT4xxx */ 8278 switch (sensep->es_key) { 8279 case KEY_UNIT_ATTENTION: 8280 if (sensep->es_add_code == 0x28) { 8281 state = MTIO_INSERTED; 8282 } 8283 break; 8284 case KEY_NOT_READY: 8285 if (sensep->es_add_code == 0x04) { 8286 /* in transition but could be either */ 8287 state = un->un_specified_mediastate; 8288 } else if ((sensep->es_add_code == 0x3a) && 8289 (sensep->es_qual_code == 0x00)) { 8290 state = MTIO_EJECTED; 8291 } 8292 break; 8293 } 8294 break; 8295 } 8296 } else if (*((char *)statusp) == STATUS_GOOD) { 8297 state = MTIO_INSERTED; 8298 } 8299 8300 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 8301 "st_media_watch_cb:state=%x, specified=%x\n", 8302 state, un->un_specified_mediastate); 8303 8304 /* 8305 * now signal the waiting thread if this is *not* the specified state; 8306 * delay the signal if the state is MTIO_INSERTED 8307 * to allow the target to recover 8308 */ 8309 if (state != un->un_specified_mediastate) { 8310 un->un_mediastate = state; 8311 if (state == MTIO_INSERTED) { 8312 /* 8313 * delay the signal to give the drive a chance 8314 * to do what it apparently needs to do 8315 */ 8316 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 8317 "st_media_watch_cb:delayed cv_broadcast\n"); 8318 un->un_delay_tid = timeout(st_delayed_cv_broadcast, 8319 un, drv_usectohz((clock_t)MEDIA_ACCESS_DELAY)); 8320 } else { 8321 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 8322 "st_media_watch_cb:immediate cv_broadcast\n"); 8323 cv_broadcast(&un->un_state_cv); 8324 } 8325 } 8326 mutex_exit(ST_MUTEX); 8327 return (0); 8328 } 8329 8330 /* 8331 * delayed cv_broadcast to allow for target to recover 8332 * from media insertion 8333 */ 8334 static void 8335 st_delayed_cv_broadcast(void *arg) 8336 { 8337 struct scsi_tape *un = arg; 8338 8339 ST_FUNC(ST_DEVINFO, st_delayed_cv_broadcast); 8340 8341 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 8342 "st_delayed_cv_broadcast:delayed cv_broadcast\n"); 8343 8344 mutex_enter(ST_MUTEX); 8345 cv_broadcast(&un->un_state_cv); 8346 mutex_exit(ST_MUTEX); 8347 } 8348 8349 /* 8350 * restart cmd currently at the start of the waitq 8351 */ 8352 static void 8353 st_start_restart(void *arg) 8354 { 8355 struct scsi_tape *un = arg; 8356 8357 ST_FUNC(ST_DEVINFO, st_start_restart); 8358 8359 ASSERT(un != NULL); 8360 8361 mutex_enter(ST_MUTEX); 8362 8363 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 8364 "st_tran_restart()\n"); 8365 8366 if (un->un_quef) { 8367 st_start(un); 8368 } 8369 8370 mutex_exit(ST_MUTEX); 8371 } 8372 8373 8374 /* 8375 * Command completion processing 8376 * 8377 */ 8378 static void 8379 st_intr(struct scsi_pkt *pkt) 8380 { 8381 struct scsi_tape *un; 8382 struct buf *last_runqf; 8383 struct buf *bp; 8384 int action = COMMAND_DONE; 8385 clock_t timout; 8386 int status; 8387 8388 8389 bp = pkt->pkt_private; 8390 8391 un = ddi_get_soft_state(st_state, MTUNIT(bp->b_edev)); 8392 8393 ST_FUNC(ST_DEVINFO, st_intr); 8394 8395 mutex_enter(ST_MUTEX); 8396 8397 un->un_rqs_state &= ~(ST_RQS_ERROR); 8398 8399 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, "st_intr()\n"); 8400 8401 if (pkt->pkt_reason != CMD_CMPLT) { 8402 8403 /* If device has gone away not much else to do */ 8404 if (pkt->pkt_reason == CMD_DEV_GONE) { 8405 action = COMMAND_DONE_ERROR; 8406 } else if (un->un_state == ST_STATE_SENSING) { 8407 ST_DO_ERRSTATS(un, st_transerrs); 8408 action = COMMAND_DONE_ERROR; 8409 } else { 8410 action = st_handle_incomplete(un, bp); 8411 } 8412 /* 8413 * At this point we know that the command was successfully 8414 * completed. Now what? 8415 */ 8416 } else if (un->un_arq_enabled && 8417 (pkt->pkt_state & STATE_ARQ_DONE)) { 8418 /* 8419 * the transport layer successfully completed an autorqsense 8420 */ 8421 action = st_handle_autosense(un, bp); 8422 8423 } else if (un->un_state == ST_STATE_SENSING) { 8424 /* 8425 * okay. We were running a REQUEST SENSE. Find 8426 * out what to do next. 8427 * some actions are based on un_state, hence 8428 * restore the state st was in before ST_STATE_SENSING. 8429 */ 8430 un->un_state = un->un_laststate; 8431 action = st_handle_sense(un, bp); 8432 /* 8433 * set pkt back to original packet in case we will have 8434 * to requeue it 8435 */ 8436 pkt = BP_PKT(bp); 8437 } else if ((SCBP(pkt)->sts_busy) || (SCBP(pkt)->sts_chk)) { 8438 /* 8439 * Okay, we weren't running a REQUEST SENSE. Call a routine 8440 * to see if the status bits we're okay. If a request sense 8441 * is to be run, that will happen. 8442 */ 8443 action = st_check_error(un, pkt); 8444 } 8445 8446 if (un->un_pwr_mgmt == ST_PWR_SUSPENDED) { 8447 switch (action) { 8448 case QUE_COMMAND: 8449 /* 8450 * return cmd to head to the queue 8451 * since we are suspending so that 8452 * it gets restarted during resume 8453 */ 8454 if (un->un_runqf) { 8455 last_runqf = un->un_runqf; 8456 un->un_runqf = bp; 8457 bp->b_actf = last_runqf; 8458 } else { 8459 bp->b_actf = NULL; 8460 un->un_runqf = bp; 8461 un->un_runql = bp; 8462 } 8463 action = JUST_RETURN; 8464 break; 8465 8466 case QUE_SENSE: 8467 action = COMMAND_DONE_ERROR; 8468 break; 8469 8470 default: 8471 break; 8472 } 8473 } 8474 8475 /* 8476 * Restore old state if we were sensing. 8477 */ 8478 if (un->un_state == ST_STATE_SENSING && action != QUE_SENSE) { 8479 un->un_state = un->un_laststate; 8480 } 8481 8482 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 8483 "st_intr: pkt=%p, bp=%p, action=%x, status=%x\n", 8484 (void *)pkt, (void *)bp, action, SCBP_C(pkt)); 8485 8486 8487 switch (action) { 8488 case COMMAND_DONE_EACCES: 8489 /* this is to report a reservation conflict */ 8490 st_bioerror(bp, EACCES); 8491 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 8492 "Reservation Conflict \n"); 8493 un->un_pos.pmode = invalid; 8494 8495 /*FALLTHROUGH*/ 8496 case COMMAND_DONE_ERROR: 8497 if (un->un_pos.eof < ST_EOT_PENDING && 8498 un->un_state >= ST_STATE_OPEN) { 8499 /* 8500 * all errors set state of the tape to 'unknown' 8501 * unless we're at EOT or are doing append testing. 8502 * If sense key was illegal request, preserve state. 8503 */ 8504 if (un->un_status != KEY_ILLEGAL_REQUEST) { 8505 un->un_pos.pmode = invalid; 8506 } 8507 } 8508 8509 un->un_err_resid = bp->b_resid = bp->b_bcount; 8510 /* 8511 * since we have an error (COMMAND_DONE_ERROR), we want to 8512 * make sure an error ocurrs, so make sure at least EIO is 8513 * returned 8514 */ 8515 if (geterror(bp) == 0) 8516 st_bioerror(bp, EIO); 8517 8518 SET_PE_FLAG(un); 8519 if (!(un->un_rqs_state & ST_RQS_ERROR) && 8520 (un->un_errno == EIO)) { 8521 un->un_rqs_state &= ~(ST_RQS_VALID); 8522 } 8523 goto done; 8524 8525 case COMMAND_DONE_ERROR_RECOVERED: 8526 un->un_err_resid = bp->b_resid = bp->b_bcount; 8527 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 8528 "st_intr(): COMMAND_DONE_ERROR_RECOVERED"); 8529 if (geterror(bp) == 0) { 8530 st_bioerror(bp, EIO); 8531 } 8532 SET_PE_FLAG(un); 8533 if (!(un->un_rqs_state & ST_RQS_ERROR) && 8534 (un->un_errno == EIO)) { 8535 un->un_rqs_state &= ~(ST_RQS_VALID); 8536 } 8537 /*FALLTHROUGH*/ 8538 case COMMAND_DONE: 8539 st_set_state(un); 8540 done: 8541 ST_DO_KSTATS(bp, kstat_runq_exit); 8542 st_done_and_mutex_exit(un, bp); 8543 return; 8544 8545 case QUE_SENSE: 8546 if ((un->un_ncmds > 1) && !un->un_flush_on_errors) 8547 goto sense_error; 8548 8549 if (un->un_state != ST_STATE_SENSING) { 8550 un->un_laststate = un->un_state; 8551 un->un_state = ST_STATE_SENSING; 8552 } 8553 8554 un->un_rqs->pkt_private = (opaque_t)bp; 8555 bzero(ST_RQSENSE, SENSE_LENGTH); 8556 8557 if (un->un_throttle) { 8558 un->un_last_throttle = un->un_throttle; 8559 un->un_throttle = 0; 8560 } 8561 8562 mutex_exit(ST_MUTEX); 8563 8564 /* 8565 * never retry this, some other command will have nuked the 8566 * sense, anyway 8567 */ 8568 status = scsi_transport(un->un_rqs); 8569 8570 mutex_enter(ST_MUTEX); 8571 8572 if (un->un_last_throttle) { 8573 un->un_throttle = un->un_last_throttle; 8574 } 8575 8576 if (status == TRAN_ACCEPT) { 8577 mutex_exit(ST_MUTEX); 8578 return; 8579 } 8580 if (status != TRAN_BUSY) 8581 ST_DO_ERRSTATS(un, st_transerrs); 8582 sense_error: 8583 un->un_pos.pmode = invalid; 8584 st_bioerror(bp, EIO); 8585 SET_PE_FLAG(un); 8586 goto done; 8587 8588 case QUE_BUSY_COMMAND: 8589 /* longish timeout */ 8590 timout = ST_STATUS_BUSY_TIMEOUT; 8591 goto que_it_up; 8592 8593 case QUE_COMMAND: 8594 /* short timeout */ 8595 timout = ST_TRAN_BUSY_TIMEOUT; 8596 que_it_up: 8597 /* 8598 * let st_handle_intr_busy put this bp back on waitq and make 8599 * checks to see if it is ok to requeue the command. 8600 */ 8601 ST_DO_KSTATS(bp, kstat_runq_back_to_waitq); 8602 8603 /* 8604 * Save the throttle before setting up the timeout 8605 */ 8606 if (un->un_throttle) { 8607 un->un_last_throttle = un->un_throttle; 8608 } 8609 mutex_exit(ST_MUTEX); 8610 if (st_handle_intr_busy(un, bp, timout) == 0) 8611 return; /* timeout is setup again */ 8612 8613 mutex_enter(ST_MUTEX); 8614 un->un_pos.pmode = invalid; 8615 un->un_err_resid = bp->b_resid = bp->b_bcount; 8616 st_bioerror(bp, EIO); 8617 SET_PE_FLAG(un); 8618 goto done; 8619 8620 case QUE_LAST_COMMAND: 8621 8622 if ((un->un_ncmds > 1) && !un->un_flush_on_errors) { 8623 scsi_log(ST_DEVINFO, st_label, CE_CONT, 8624 "un_ncmds: %d can't retry cmd \n", un->un_ncmds); 8625 goto last_command_error; 8626 } 8627 mutex_exit(ST_MUTEX); 8628 if (st_handle_intr_retry_lcmd(un, bp) == 0) 8629 return; 8630 mutex_enter(ST_MUTEX); 8631 last_command_error: 8632 un->un_err_resid = bp->b_resid = bp->b_bcount; 8633 un->un_pos.pmode = invalid; 8634 st_bioerror(bp, EIO); 8635 SET_PE_FLAG(un); 8636 goto done; 8637 8638 case JUST_RETURN: 8639 default: 8640 ST_DO_KSTATS(bp, kstat_runq_back_to_waitq); 8641 mutex_exit(ST_MUTEX); 8642 return; 8643 } 8644 /*NOTREACHED*/ 8645 } 8646 8647 static int 8648 st_handle_incomplete(struct scsi_tape *un, struct buf *bp) 8649 { 8650 static char *fail = "SCSI transport failed: reason '%s': %s\n"; 8651 int rval = COMMAND_DONE_ERROR; 8652 struct scsi_pkt *pkt = (un->un_state == ST_STATE_SENSING) ? 8653 un->un_rqs : BP_PKT(bp); 8654 int result; 8655 8656 ST_FUNC(ST_DEVINFO, st_handle_incomplete); 8657 8658 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 8659 "st_handle_incomplete(): dev = 0x%lx\n", un->un_dev); 8660 8661 ASSERT(mutex_owned(ST_MUTEX)); 8662 8663 switch (pkt->pkt_reason) { 8664 case CMD_INCOMPLETE: /* tran stopped with not normal state */ 8665 /* 8666 * this occurs when accessing a powered down drive, no 8667 * need to complain; just fail the open 8668 */ 8669 ST_CDB(ST_DEVINFO, "Incomplete CDB", (char *)pkt->pkt_cdbp); 8670 8671 /* 8672 * if we have commands outstanding in HBA, and a command 8673 * comes back incomplete, we're hosed, so reset target 8674 * If we have the bus, but cmd_incomplete, we probably just 8675 * have a failed selection, so don't reset the target, just 8676 * requeue the command and try again 8677 */ 8678 if ((un->un_ncmds > 1) || (pkt->pkt_state != STATE_GOT_BUS)) { 8679 goto reset_target; 8680 } 8681 8682 /* 8683 * Retry selection a couple more times if we're 8684 * open. If opening, we only try just once to 8685 * reduce probe time for nonexistant devices. 8686 */ 8687 if ((un->un_laststate > ST_STATE_OPENING) && 8688 ((int)un->un_retry_ct < st_selection_retry_count)) { 8689 rval = QUE_COMMAND; 8690 } 8691 ST_DO_ERRSTATS(un, st_transerrs); 8692 break; 8693 8694 case CMD_ABORTED: 8695 /* 8696 * most likely this is caused by flush-on-error support. If 8697 * it was not there, the we're in trouble. 8698 */ 8699 if (!un->un_flush_on_errors) { 8700 un->un_status = SUN_KEY_FATAL; 8701 goto reset_target; 8702 } 8703 8704 st_set_pe_errno(un); 8705 bioerror(bp, un->un_errno); 8706 if (un->un_errno) 8707 return (COMMAND_DONE_ERROR); 8708 else 8709 return (COMMAND_DONE); 8710 8711 case CMD_TIMEOUT: /* Command timed out */ 8712 un->un_status = SUN_KEY_TIMEOUT; 8713 8714 /*FALLTHROUGH*/ 8715 default: 8716 reset_target: 8717 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 8718 "transport completed with %s\n", 8719 scsi_rname(pkt->pkt_reason)); 8720 ST_DO_ERRSTATS(un, st_transerrs); 8721 if ((pkt->pkt_state & STATE_GOT_TARGET) && 8722 ((pkt->pkt_statistics & (STAT_BUS_RESET | STAT_DEV_RESET | 8723 STAT_ABORTED)) == 0)) { 8724 8725 /* 8726 * If we haven't reserved the drive don't reset it. 8727 */ 8728 if ((un->un_rsvd_status & 8729 (ST_RESERVE | ST_APPLICATION_RESERVATIONS)) == 0) { 8730 return (rval); 8731 } 8732 8733 /* 8734 * if we aren't lost yet we will be soon. 8735 */ 8736 un->un_pos.pmode = invalid; 8737 8738 mutex_exit(ST_MUTEX); 8739 8740 result = scsi_reset(ROUTE, RESET_TARGET); 8741 /* 8742 * if target reset fails, then pull the chain 8743 */ 8744 if (result == 0) { 8745 result = scsi_reset(ROUTE, RESET_ALL); 8746 } 8747 mutex_enter(ST_MUTEX); 8748 8749 if ((result == 0) && (un->un_state >= ST_STATE_OPEN)) { 8750 /* no hope left to recover */ 8751 scsi_log(ST_DEVINFO, st_label, CE_WARN, 8752 "recovery by resets failed\n"); 8753 return (rval); 8754 } 8755 } 8756 } 8757 8758 if ((pkt->pkt_reason == CMD_RESET) || (pkt->pkt_statistics & 8759 (STAT_BUS_RESET | STAT_DEV_RESET))) { 8760 if ((un->un_rsvd_status & ST_RESERVE)) { 8761 un->un_rsvd_status |= ST_LOST_RESERVE; 8762 ST_DEBUG3(ST_DEVINFO, st_label, CE_WARN, 8763 "Lost Reservation\n"); 8764 } 8765 } 8766 8767 if ((int)un->un_retry_ct++ < st_retry_count) { 8768 if (un->un_pwr_mgmt == ST_PWR_SUSPENDED) { 8769 rval = QUE_COMMAND; 8770 } else if (bp == un->un_sbufp) { 8771 switch ((uchar_t)(uintptr_t)bp->b_forw) { 8772 case SCMD_MODE_SENSE: 8773 case SCMD_MODE_SELECT: 8774 case SCMD_READ_BLKLIM: 8775 case SCMD_REWIND: 8776 case SCMD_LOAD: 8777 case SCMD_TEST_UNIT_READY: 8778 /* 8779 * These commands can be rerun with impunity 8780 */ 8781 rval = QUE_COMMAND; 8782 break; 8783 8784 default: 8785 break; 8786 } 8787 } 8788 } else { 8789 rval = COMMAND_DONE_ERROR; 8790 } 8791 8792 if (un->un_state >= ST_STATE_OPEN) { 8793 scsi_log(ST_DEVINFO, st_label, CE_WARN, 8794 fail, scsi_rname(pkt->pkt_reason), 8795 (rval == COMMAND_DONE_ERROR)? 8796 "giving up" : "retrying command"); 8797 } 8798 return (rval); 8799 } 8800 8801 /* 8802 * if the device is busy, then put this bp back on the waitq, on the 8803 * interrupt thread, where we want the head of the queue and not the 8804 * end 8805 * 8806 * The callers of this routine should take measures to save the 8807 * un_throttle in un_last_throttle which will be restored in 8808 * st_intr_restart(). The only exception should be st_intr_restart() 8809 * calling this routine for which the saving is already done. 8810 */ 8811 static int 8812 st_handle_intr_busy(struct scsi_tape *un, struct buf *bp, 8813 clock_t timeout_interval) 8814 { 8815 struct buf *last_quef; 8816 int rval = 0; 8817 8818 mutex_enter(ST_MUTEX); 8819 8820 ST_FUNC(ST_DEVINFO, st_handle_intr_busy); 8821 8822 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 8823 "st_handle_intr_busy(), un = 0x%p\n", (void *)un); 8824 8825 /* 8826 * Check to see if we hit the retry timeout. We check to make sure 8827 * this is the first one on the runq and make sure we have not 8828 * queued up any more, so this one has to be the last on the list 8829 * also. If it is not, we have to fail. If it is not the first, but 8830 * is the last we are in trouble anyway, as we are in the interrupt 8831 * context here. 8832 */ 8833 if (((int)un->un_tran_retry_ct++ > st_retry_count) || 8834 ((un->un_runqf != bp) && (un->un_runql != bp))) { 8835 rval = -1; 8836 goto exit; 8837 } 8838 8839 /* put the bp back on the waitq */ 8840 if (un->un_quef) { 8841 last_quef = un->un_quef; 8842 un->un_quef = bp; 8843 bp->b_actf = last_quef; 8844 } else { 8845 bp->b_actf = NULL; 8846 un->un_quef = bp; 8847 un->un_quel = bp; 8848 } 8849 8850 /* 8851 * We know that this is the first and last on the runq at this time, 8852 * so we just nullify those two queues 8853 */ 8854 un->un_runqf = NULL; 8855 un->un_runql = NULL; 8856 8857 /* 8858 * We don't want any other commands being started in the mean time. 8859 * If start had just released mutex after putting something on the 8860 * runq, we won't even get here. 8861 */ 8862 un->un_throttle = 0; 8863 8864 /* 8865 * send a marker pkt, if appropriate 8866 */ 8867 st_hba_unflush(un); 8868 8869 /* 8870 * all queues are aligned, we are just waiting to 8871 * transport 8872 */ 8873 un->un_hib_tid = timeout(st_intr_restart, un, timeout_interval); 8874 8875 exit: 8876 mutex_exit(ST_MUTEX); 8877 return (rval); 8878 } 8879 8880 /* 8881 * To get one error entry from error stack 8882 */ 8883 static int 8884 st_get_error_entry(struct scsi_tape *un, intptr_t arg, int flag) 8885 { 8886 #ifdef _MULTI_DATAMODEL 8887 /* 8888 * For use when a 32 bit app makes a call into a 8889 * 64 bit ioctl 8890 */ 8891 struct mterror_entry32 err_entry32; 8892 #endif /* _MULTI_DATAMODEL */ 8893 8894 int rval = 0; 8895 struct mterror_entry err_entry; 8896 struct mterror_entry_stack *err_link_entry_p; 8897 size_t arq_status_len_in, arq_status_len_kr; 8898 8899 ST_FUNC(ST_DEVINFO, st_get_error_entry); 8900 8901 ASSERT(mutex_owned(ST_MUTEX)); 8902 8903 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 8904 "st_get_error_entry()\n"); 8905 8906 /* 8907 * if error record stack empty, return ENXIO 8908 */ 8909 if (un->un_error_entry_stk == NULL) { 8910 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 8911 "st_get_error_entry: Error Entry Stack Empty!\n"); 8912 rval = ENXIO; 8913 goto ret; 8914 } 8915 8916 /* 8917 * get the top entry from stack 8918 */ 8919 err_link_entry_p = un->un_error_entry_stk; 8920 arq_status_len_kr = 8921 err_link_entry_p->mtees_entry.mtee_arq_status_len; 8922 8923 #ifdef _MULTI_DATAMODEL 8924 switch (ddi_model_convert_from(flag & FMODELS)) { 8925 case DDI_MODEL_ILP32: 8926 if (ddi_copyin((void *)arg, &err_entry32, 8927 MTERROR_ENTRY_SIZE_32, flag)) { 8928 rval = EFAULT; 8929 goto ret; 8930 } 8931 8932 arq_status_len_in = 8933 (size_t)err_entry32.mtee_arq_status_len; 8934 8935 err_entry32.mtee_cdb_len = 8936 (size32_t)err_link_entry_p->mtees_entry.mtee_cdb_len; 8937 8938 if (arq_status_len_in > arq_status_len_kr) 8939 err_entry32.mtee_arq_status_len = 8940 (size32_t)arq_status_len_kr; 8941 8942 if (ddi_copyout( 8943 err_link_entry_p->mtees_entry.mtee_cdb_buf, 8944 (void *)(uintptr_t)err_entry32.mtee_cdb_buf, 8945 err_entry32.mtee_cdb_len, flag)) { 8946 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 8947 "st_get_error_entry: Copy cdb buffer error!"); 8948 rval = EFAULT; 8949 } 8950 8951 if (ddi_copyout( 8952 err_link_entry_p->mtees_entry.mtee_arq_status, 8953 (void *)(uintptr_t)err_entry32.mtee_arq_status, 8954 err_entry32.mtee_arq_status_len, flag)) { 8955 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 8956 "st_get_error_entry: copy arq status error!"); 8957 rval = EFAULT; 8958 } 8959 8960 if (ddi_copyout(&err_entry32, (void *)arg, 8961 MTERROR_ENTRY_SIZE_32, flag)) { 8962 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 8963 "st_get_error_entry: copy arq status out error!"); 8964 rval = EFAULT; 8965 } 8966 break; 8967 8968 case DDI_MODEL_NONE: 8969 if (ddi_copyin((void *)arg, &err_entry, 8970 MTERROR_ENTRY_SIZE_64, flag)) { 8971 rval = EFAULT; 8972 goto ret; 8973 } 8974 arq_status_len_in = err_entry.mtee_arq_status_len; 8975 8976 err_entry.mtee_cdb_len = 8977 err_link_entry_p->mtees_entry.mtee_cdb_len; 8978 8979 if (arq_status_len_in > arq_status_len_kr) 8980 err_entry.mtee_arq_status_len = 8981 arq_status_len_kr; 8982 8983 if (ddi_copyout( 8984 err_link_entry_p->mtees_entry.mtee_cdb_buf, 8985 err_entry.mtee_cdb_buf, 8986 err_entry.mtee_cdb_len, flag)) { 8987 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 8988 "st_get_error_entry: Copy cdb buffer error!"); 8989 rval = EFAULT; 8990 } 8991 8992 if (ddi_copyout( 8993 err_link_entry_p->mtees_entry.mtee_arq_status, 8994 err_entry.mtee_arq_status, 8995 err_entry.mtee_arq_status_len, flag)) { 8996 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 8997 "st_get_error_entry: copy arq status error!"); 8998 rval = EFAULT; 8999 } 9000 9001 if (ddi_copyout(&err_entry, (void *)arg, 9002 MTERROR_ENTRY_SIZE_64, flag)) { 9003 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 9004 "st_get_error_entry: copy arq status out error!"); 9005 rval = EFAULT; 9006 } 9007 break; 9008 } 9009 #else /* _MULTI_DATAMODEL */ 9010 if (ddi_copyin((void *)arg, &err_entry, 9011 MTERROR_ENTRY_SIZE_64, flag)) { 9012 rval = EFAULT; 9013 goto ret; 9014 } 9015 arq_status_len_in = err_entry.mtee_arq_status_len; 9016 9017 err_entry.mtee_cdb_len = 9018 err_link_entry_p->mtees_entry.mtee_cdb_len; 9019 9020 if (arq_status_len_in > arq_status_len_kr) 9021 err_entry.mtee_arq_status_len = 9022 arq_status_len_kr; 9023 9024 if (ddi_copyout( 9025 err_link_entry_p->mtees_entry.mtee_cdb_buf, 9026 err_entry.mtee_cdb_buf, 9027 err_entry.mtee_cdb_len, flag)) { 9028 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 9029 "st_get_error_entry: Copy cdb buffer error!"); 9030 rval = EFAULT; 9031 } 9032 9033 if (ddi_copyout( 9034 err_link_entry_p->mtees_entry.mtee_arq_status, 9035 err_entry.mtee_arq_status, 9036 err_entry.mtee_arq_status_len, flag)) { 9037 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 9038 "st_get_error_entry: copy arq status buffer error!"); 9039 rval = EFAULT; 9040 } 9041 9042 if (ddi_copyout(&err_entry, (void *)arg, 9043 MTERROR_ENTRY_SIZE_64, flag)) { 9044 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 9045 "st_get_error_entry: copy arq status out error!"); 9046 rval = EFAULT; 9047 } 9048 #endif /* _MULTI_DATAMODEL */ 9049 9050 /* 9051 * update stack 9052 */ 9053 un->un_error_entry_stk = err_link_entry_p->mtees_nextp; 9054 9055 kmem_free(err_link_entry_p->mtees_entry.mtee_cdb_buf, 9056 err_link_entry_p->mtees_entry.mtee_cdb_len); 9057 err_link_entry_p->mtees_entry.mtee_cdb_buf = NULL; 9058 9059 kmem_free(err_link_entry_p->mtees_entry.mtee_arq_status, 9060 SECMDS_STATUS_SIZE); 9061 err_link_entry_p->mtees_entry.mtee_arq_status = NULL; 9062 9063 kmem_free(err_link_entry_p, MTERROR_LINK_ENTRY_SIZE); 9064 err_link_entry_p = NULL; 9065 ret: 9066 return (rval); 9067 } 9068 9069 /* 9070 * MTIOCGETERROR ioctl needs to retrieve the current sense data along with 9071 * the scsi CDB command which causes the error and generates sense data and 9072 * the scsi status. 9073 * 9074 * error-record stack 9075 * 9076 * 9077 * TOP BOTTOM 9078 * ------------------------------------------ 9079 * | 0 | 1 | 2 | ... | n | 9080 * ------------------------------------------ 9081 * ^ 9082 * | 9083 * pointer to error entry 9084 * 9085 * when st driver generates one sense data record, it creates a error-entry 9086 * and pushes it onto the stack. 9087 * 9088 */ 9089 9090 static void 9091 st_update_error_stack(struct scsi_tape *un, 9092 struct scsi_pkt *pkt, 9093 struct scsi_arq_status *cmd) 9094 { 9095 struct mterror_entry_stack *err_entry_tmp; 9096 uchar_t *cdbp = (uchar_t *)pkt->pkt_cdbp; 9097 size_t cdblen = scsi_cdb_size[CDB_GROUPID(cdbp[0])]; 9098 9099 ST_FUNC(ST_DEVINFO, st_update_error_stack); 9100 9101 ASSERT(mutex_owned(ST_MUTEX)); 9102 9103 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 9104 "st_update_error_stack()\n"); 9105 9106 ASSERT(cmd); 9107 ASSERT(cdbp); 9108 if (cdblen == 0) { 9109 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 9110 "st_update_error_stack: CDB length error!\n"); 9111 return; 9112 } 9113 9114 err_entry_tmp = kmem_alloc(MTERROR_LINK_ENTRY_SIZE, KM_SLEEP); 9115 ASSERT(err_entry_tmp != NULL); 9116 9117 err_entry_tmp->mtees_entry.mtee_cdb_buf = 9118 kmem_alloc(cdblen, KM_SLEEP); 9119 ASSERT(err_entry_tmp->mtees_entry.mtee_cdb_buf != NULL); 9120 9121 err_entry_tmp->mtees_entry.mtee_arq_status = 9122 kmem_alloc(SECMDS_STATUS_SIZE, KM_SLEEP); 9123 ASSERT(err_entry_tmp->mtees_entry.mtee_arq_status != NULL); 9124 9125 /* 9126 * copy cdb command & length to current error entry 9127 */ 9128 err_entry_tmp->mtees_entry.mtee_cdb_len = cdblen; 9129 bcopy(cdbp, err_entry_tmp->mtees_entry.mtee_cdb_buf, cdblen); 9130 9131 /* 9132 * copy scsi status length to current error entry 9133 */ 9134 err_entry_tmp->mtees_entry.mtee_arq_status_len = 9135 SECMDS_STATUS_SIZE; 9136 9137 /* 9138 * copy sense data and scsi status to current error entry 9139 */ 9140 bcopy(cmd, err_entry_tmp->mtees_entry.mtee_arq_status, 9141 SECMDS_STATUS_SIZE); 9142 9143 err_entry_tmp->mtees_nextp = un->un_error_entry_stk; 9144 un->un_error_entry_stk = err_entry_tmp; 9145 9146 } 9147 9148 /* 9149 * Empty all the error entry in stack 9150 */ 9151 static void 9152 st_empty_error_stack(struct scsi_tape *un) 9153 { 9154 struct mterror_entry_stack *linkp; 9155 9156 ST_FUNC(ST_DEVINFO, st_empty_error_stack); 9157 9158 ASSERT(mutex_owned(ST_MUTEX)); 9159 9160 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 9161 "st_empty_entry_stack()\n"); 9162 9163 while (un->un_error_entry_stk != NULL) { 9164 linkp = un->un_error_entry_stk; 9165 un->un_error_entry_stk = 9166 un->un_error_entry_stk->mtees_nextp; 9167 kmem_free(linkp, MTERROR_LINK_ENTRY_SIZE); 9168 linkp = NULL; 9169 } 9170 } 9171 9172 static int 9173 st_handle_sense(struct scsi_tape *un, struct buf *bp) 9174 { 9175 struct scsi_pkt *pkt = BP_PKT(bp); 9176 struct scsi_pkt *rqpkt = un->un_rqs; 9177 struct scsi_arq_status arqstat; 9178 9179 int rval = COMMAND_DONE_ERROR; 9180 int amt; 9181 9182 ST_FUNC(ST_DEVINFO, st_handle_sense); 9183 9184 ASSERT(mutex_owned(ST_MUTEX)); 9185 9186 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 9187 "st_handle_sense()\n"); 9188 9189 if (SCBP(rqpkt)->sts_busy) { 9190 ST_DEBUG4(ST_DEVINFO, st_label, CE_WARN, 9191 "busy unit on request sense\n"); 9192 if ((int)un->un_retry_ct++ < st_retry_count) { 9193 rval = QUE_BUSY_COMMAND; 9194 } 9195 return (rval); 9196 } else if (SCBP(rqpkt)->sts_chk) { 9197 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 9198 "Check Condition on REQUEST SENSE\n"); 9199 return (rval); 9200 } 9201 9202 /* was there enough data? */ 9203 amt = (int)SENSE_LENGTH - rqpkt->pkt_resid; 9204 if ((rqpkt->pkt_state & STATE_XFERRED_DATA) == 0 || 9205 (amt < SUN_MIN_SENSE_LENGTH)) { 9206 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 9207 "REQUEST SENSE couldn't get sense data\n"); 9208 return (rval); 9209 } 9210 9211 bcopy(SCBP(pkt), &arqstat.sts_status, 9212 sizeof (struct scsi_status)); 9213 bcopy(SCBP(rqpkt), &arqstat.sts_rqpkt_status, 9214 sizeof (struct scsi_status)); 9215 arqstat.sts_rqpkt_reason = rqpkt->pkt_reason; 9216 arqstat.sts_rqpkt_resid = rqpkt->pkt_resid; 9217 arqstat.sts_rqpkt_state = rqpkt->pkt_state; 9218 arqstat.sts_rqpkt_statistics = rqpkt->pkt_statistics; 9219 bcopy(ST_RQSENSE, &arqstat.sts_sensedata, SENSE_LENGTH); 9220 9221 /* 9222 * copy one arqstat entry in the sense data buffer 9223 */ 9224 st_update_error_stack(un, pkt, &arqstat); 9225 return (st_decode_sense(un, bp, amt, SCBP(rqpkt))); 9226 } 9227 9228 static int 9229 st_handle_autosense(struct scsi_tape *un, struct buf *bp) 9230 { 9231 struct scsi_pkt *pkt = BP_PKT(bp); 9232 struct scsi_arq_status *arqstat = 9233 (struct scsi_arq_status *)pkt->pkt_scbp; 9234 int rval = COMMAND_DONE_ERROR; 9235 int amt; 9236 9237 ST_FUNC(ST_DEVINFO, st_handle_autosense); 9238 9239 ASSERT(mutex_owned(ST_MUTEX)); 9240 9241 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 9242 "st_handle_autosense()\n"); 9243 9244 if (arqstat->sts_rqpkt_status.sts_busy) { 9245 ST_DEBUG4(ST_DEVINFO, st_label, CE_WARN, 9246 "busy unit on request sense\n"); 9247 /* 9248 * we return QUE_SENSE so st_intr will setup the SENSE cmd. 9249 * the disadvantage is that we do not have any delay for the 9250 * second retry of rqsense and we have to keep a packet around 9251 */ 9252 return (QUE_SENSE); 9253 9254 } else if (arqstat->sts_rqpkt_reason != CMD_CMPLT) { 9255 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 9256 "transport error on REQUEST SENSE\n"); 9257 if ((arqstat->sts_rqpkt_state & STATE_GOT_TARGET) && 9258 ((arqstat->sts_rqpkt_statistics & 9259 (STAT_BUS_RESET | STAT_DEV_RESET | STAT_ABORTED)) == 0)) { 9260 mutex_exit(ST_MUTEX); 9261 if (scsi_reset(ROUTE, RESET_TARGET) == 0) { 9262 /* 9263 * if target reset fails, then pull the chain 9264 */ 9265 if (scsi_reset(ROUTE, RESET_ALL) == 0) { 9266 ST_DEBUG6(ST_DEVINFO, st_label, 9267 CE_WARN, 9268 "recovery by resets failed\n"); 9269 } 9270 } 9271 mutex_enter(ST_MUTEX); 9272 } 9273 return (rval); 9274 9275 } else if (arqstat->sts_rqpkt_status.sts_chk) { 9276 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 9277 "Check Condition on REQUEST SENSE\n"); 9278 return (rval); 9279 } 9280 9281 9282 /* was there enough data? */ 9283 amt = (int)SENSE_LENGTH - arqstat->sts_rqpkt_resid; 9284 if ((arqstat->sts_rqpkt_state & STATE_XFERRED_DATA) == 0 || 9285 (amt < SUN_MIN_SENSE_LENGTH)) { 9286 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 9287 "REQUEST SENSE couldn't get sense data\n"); 9288 return (rval); 9289 } 9290 9291 /* 9292 * copy one arqstat entry in the sense data buffer 9293 */ 9294 st_update_error_stack(un, pkt, arqstat); 9295 9296 bcopy(&arqstat->sts_sensedata, ST_RQSENSE, SENSE_LENGTH); 9297 9298 return (st_decode_sense(un, bp, amt, &arqstat->sts_rqpkt_status)); 9299 } 9300 9301 static int 9302 st_decode_sense(struct scsi_tape *un, struct buf *bp, int amt, 9303 struct scsi_status *statusp) 9304 { 9305 struct scsi_pkt *pkt = BP_PKT(bp); 9306 int rval = COMMAND_DONE_ERROR; 9307 long resid; 9308 struct scsi_extended_sense *sensep = ST_RQSENSE; 9309 int severity; 9310 int get_error; 9311 9312 ST_FUNC(ST_DEVINFO, st_decode_sense); 9313 9314 ASSERT(mutex_owned(ST_MUTEX)); 9315 9316 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 9317 "st_decode_sense()\n"); 9318 9319 /* 9320 * For uscsi commands, squirrel away a copy of the 9321 * results of the Request Sense. 9322 */ 9323 if (USCSI_CMD(bp)) { 9324 struct uscsi_cmd *ucmd = BP_UCMD(bp); 9325 ucmd->uscsi_rqstatus = *(uchar_t *)statusp; 9326 if (ucmd->uscsi_rqlen && un->un_srqbufp) { 9327 uchar_t rqlen = min((uchar_t)amt, ucmd->uscsi_rqlen); 9328 ucmd->uscsi_rqresid = ucmd->uscsi_rqlen - rqlen; 9329 bcopy(ST_RQSENSE, un->un_srqbufp, rqlen); 9330 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 9331 "st_decode_sense: stat=0x%x resid=0x%x\n", 9332 ucmd->uscsi_rqstatus, ucmd->uscsi_rqresid); 9333 } 9334 } 9335 9336 /* 9337 * If the drive is an MT-02, reposition the 9338 * secondary error code into the proper place. 9339 * 9340 * XXX MT-02 is non-CCS tape, so secondary error code 9341 * is in byte 8. However, in SCSI-2, tape has CCS definition 9342 * so it's in byte 12. 9343 */ 9344 if (un->un_dp->type == ST_TYPE_EMULEX) { 9345 sensep->es_code = sensep->es_add_info[0]; 9346 } 9347 9348 ST_CDB(ST_DEVINFO, "st_decode_sense failed CDB", 9349 (caddr_t)&CDBP(pkt)->scc_cmd); 9350 9351 ST_SENSE(ST_DEVINFO, "st_decode_sense sense data", (caddr_t)sensep, 9352 sizeof (*sensep)); 9353 9354 /* for normal I/O check extract the resid values. */ 9355 if (bp != un->un_sbufp) { 9356 if (sensep->es_valid) { 9357 resid = 9358 (sensep->es_info_1 << 24) | 9359 (sensep->es_info_2 << 16) | 9360 (sensep->es_info_3 << 8) | 9361 (sensep->es_info_4); 9362 /* If fixed block */ 9363 if (un->un_bsize) { 9364 resid *= un->un_bsize; 9365 } 9366 } else if (pkt->pkt_state & STATE_XFERRED_DATA) { 9367 resid = pkt->pkt_resid; 9368 } else { 9369 resid = bp->b_bcount; 9370 } 9371 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 9372 "st_handle_sense (rw): xferred bit = %d, resid=%ld (%d), " 9373 "pkt_resid=%ld\n", pkt->pkt_state & STATE_XFERRED_DATA, 9374 resid, 9375 (sensep->es_info_1 << 24) | 9376 (sensep->es_info_2 << 16) | 9377 (sensep->es_info_3 << 8) | 9378 (sensep->es_info_4), 9379 pkt->pkt_resid); 9380 /* 9381 * The problem is, what should we believe? 9382 */ 9383 if (resid && (pkt->pkt_resid == 0)) { 9384 pkt->pkt_resid = resid; 9385 } 9386 } else { 9387 /* 9388 * If the command is SCMD_SPACE, we need to get the 9389 * residual as returned in the sense data, to adjust 9390 * our idea of current tape position correctly 9391 */ 9392 if ((CDBP(pkt)->scc_cmd == SCMD_LOCATE) || 9393 (CDBP(pkt)->scc_cmd == SCMD_LOCATE_G4) || 9394 (CDBP(pkt)->scc_cmd == SCMD_SPACE) || 9395 (CDBP(pkt)->scc_cmd == SCMD_WRITE_FILE_MARK) && 9396 (sensep->es_valid)) { 9397 resid = 9398 (sensep->es_info_1 << 24) | 9399 (sensep->es_info_2 << 16) | 9400 (sensep->es_info_3 << 8) | 9401 (sensep->es_info_4); 9402 bp->b_resid = resid; 9403 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 9404 "st_handle_sense(other): resid=%ld\n", resid); 9405 } else { 9406 /* 9407 * If the special command is SCMD_READ, 9408 * the correct resid will be set later. 9409 */ 9410 resid = bp->b_bcount; 9411 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 9412 "st_handle_sense(special read): resid=%ld\n", 9413 resid); 9414 } 9415 } 9416 9417 if ((un->un_state >= ST_STATE_OPEN) && 9418 (DEBUGGING || st_error_level == SCSI_ERR_ALL)) { 9419 st_print_cdb(ST_DEVINFO, st_label, CE_NOTE, 9420 "Failed CDB", (char *)pkt->pkt_cdbp); 9421 st_clean_print(ST_DEVINFO, st_label, CE_CONT, 9422 "sense data", (char *)sensep, amt); 9423 scsi_log(ST_DEVINFO, st_label, CE_CONT, 9424 "count 0x%lx resid 0x%lx pktresid 0x%lx\n", 9425 bp->b_bcount, resid, pkt->pkt_resid); 9426 } 9427 9428 switch (un->un_status = sensep->es_key) { 9429 case KEY_NO_SENSE: 9430 severity = SCSI_ERR_INFO; 9431 9432 /* 9433 * Erase, locate or rewind operation in progress, retry 9434 * ASC ASCQ 9435 * 00 18 Erase operation in progress 9436 * 00 19 Locate operation in progress 9437 * 00 1A Rewind operation in progress 9438 */ 9439 if (sensep->es_add_code == 0 && 9440 ((sensep->es_qual_code == 0x18) || 9441 (sensep->es_qual_code == 0x19) || 9442 (sensep->es_qual_code == 0x1a))) { 9443 rval = QUE_COMMAND; 9444 break; 9445 } 9446 9447 goto common; 9448 9449 case KEY_RECOVERABLE_ERROR: 9450 severity = SCSI_ERR_RECOVERED; 9451 if ((sensep->es_class == CLASS_EXTENDED_SENSE) && 9452 (sensep->es_code == ST_DEFERRED_ERROR)) { 9453 if (un->un_dp->options & 9454 ST_RETRY_ON_RECOVERED_DEFERRED_ERROR) { 9455 rval = QUE_LAST_COMMAND; 9456 scsi_errmsg(ST_SCSI_DEVP, pkt, st_label, 9457 severity, un->un_pos.lgclblkno, 9458 un->un_err_pos.lgclblkno, scsi_cmds, 9459 sensep); 9460 scsi_log(ST_DEVINFO, st_label, CE_CONT, 9461 "Command will be retried\n"); 9462 } else { 9463 severity = SCSI_ERR_FATAL; 9464 rval = COMMAND_DONE_ERROR_RECOVERED; 9465 ST_DO_ERRSTATS(un, st_softerrs); 9466 scsi_errmsg(ST_SCSI_DEVP, pkt, st_label, 9467 severity, un->un_pos.lgclblkno, 9468 un->un_err_pos.lgclblkno, scsi_cmds, 9469 sensep); 9470 } 9471 break; 9472 } 9473 common: 9474 /* 9475 * XXX only want reads to be stopped by filemarks. 9476 * Don't want them to be stopped by EOT. EOT matters 9477 * only on write. 9478 */ 9479 if (sensep->es_filmk && !sensep->es_eom) { 9480 rval = COMMAND_DONE; 9481 } else if (sensep->es_eom) { 9482 rval = COMMAND_DONE; 9483 } else if (sensep->es_ili) { 9484 /* 9485 * Fun with variable length record devices: 9486 * for specifying larger blocks sizes than the 9487 * actual physical record size. 9488 */ 9489 if (un->un_bsize == 0 && resid > 0) { 9490 /* 9491 * XXX! Ugly. 9492 * The requested blocksize is > tape blocksize, 9493 * so this is ok, so we just return the 9494 * actual size xferred. 9495 */ 9496 pkt->pkt_resid = resid; 9497 rval = COMMAND_DONE; 9498 } else if (un->un_bsize == 0 && resid < 0) { 9499 /* 9500 * The requested blocksize is < tape blocksize, 9501 * so this is not ok, so we err with ENOMEM 9502 */ 9503 rval = COMMAND_DONE_ERROR_RECOVERED; 9504 st_bioerror(bp, ENOMEM); 9505 } else { 9506 ST_DO_ERRSTATS(un, st_softerrs); 9507 severity = SCSI_ERR_FATAL; 9508 rval = COMMAND_DONE_ERROR; 9509 st_bioerror(bp, EINVAL); 9510 } 9511 } else { 9512 /* 9513 * we hope and pray for this just being 9514 * something we can ignore (ie. a 9515 * truly recoverable soft error) 9516 */ 9517 rval = COMMAND_DONE; 9518 } 9519 if (sensep->es_filmk) { 9520 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 9521 "filemark\n"); 9522 un->un_status = SUN_KEY_EOF; 9523 un->un_pos.eof = ST_EOF_PENDING; 9524 SET_PE_FLAG(un); 9525 } 9526 9527 /* 9528 * ignore eom when reading, a fmk should terminate reading 9529 */ 9530 if ((sensep->es_eom) && 9531 (CDBP(pkt)->scc_cmd != SCMD_READ)) { 9532 if ((sensep->es_add_code == 0) && 9533 (sensep->es_qual_code == 4)) { 9534 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 9535 "bot\n"); 9536 un->un_status = SUN_KEY_BOT; 9537 un->un_pos.eof = ST_NO_EOF; 9538 un->un_pos.lgclblkno = 0; 9539 un->un_pos.fileno = 0; 9540 un->un_pos.blkno = 0; 9541 un->un_pos.pmode = legacy; 9542 } else { 9543 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 9544 "eom\n"); 9545 un->un_status = SUN_KEY_EOT; 9546 un->un_pos.eof = ST_EOM; 9547 } 9548 SET_PE_FLAG(un); 9549 } 9550 9551 break; 9552 9553 case KEY_ILLEGAL_REQUEST: 9554 9555 if (un->un_laststate >= ST_STATE_OPEN) { 9556 ST_DO_ERRSTATS(un, st_softerrs); 9557 severity = SCSI_ERR_FATAL; 9558 } else { 9559 severity = SCSI_ERR_INFO; 9560 } 9561 break; 9562 9563 case KEY_MEDIUM_ERROR: 9564 ST_DO_ERRSTATS(un, st_harderrs); 9565 severity = SCSI_ERR_FATAL; 9566 9567 /* 9568 * for (buffered) writes, a medium error must be fatal 9569 */ 9570 if (CDBP(pkt)->scc_cmd != SCMD_WRITE) { 9571 rval = COMMAND_DONE_ERROR_RECOVERED; 9572 } 9573 9574 check_keys: 9575 /* 9576 * attempt to process the keys in the presence of 9577 * other errors 9578 */ 9579 if (sensep->es_ili && rval != COMMAND_DONE_ERROR) { 9580 /* 9581 * Fun with variable length record devices: 9582 * for specifying larger blocks sizes than the 9583 * actual physical record size. 9584 */ 9585 if (un->un_bsize == 0 && resid > 0) { 9586 /* 9587 * XXX! Ugly 9588 */ 9589 pkt->pkt_resid = resid; 9590 } else if (un->un_bsize == 0 && resid < 0) { 9591 st_bioerror(bp, EINVAL); 9592 } else { 9593 severity = SCSI_ERR_FATAL; 9594 rval = COMMAND_DONE_ERROR; 9595 st_bioerror(bp, EINVAL); 9596 } 9597 } 9598 if (sensep->es_filmk) { 9599 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 9600 "filemark\n"); 9601 un->un_status = SUN_KEY_EOF; 9602 un->un_pos.eof = ST_EOF_PENDING; 9603 SET_PE_FLAG(un); 9604 } 9605 9606 /* 9607 * ignore eom when reading, a fmk should terminate reading 9608 */ 9609 if ((sensep->es_eom) && 9610 (CDBP(pkt)->scc_cmd != SCMD_READ)) { 9611 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "eom\n"); 9612 un->un_status = SUN_KEY_EOT; 9613 un->un_pos.eof = ST_EOM; 9614 SET_PE_FLAG(un); 9615 } 9616 9617 break; 9618 9619 case KEY_VOLUME_OVERFLOW: 9620 ST_DO_ERRSTATS(un, st_softerrs); 9621 un->un_pos.eof = ST_EOM; 9622 severity = SCSI_ERR_FATAL; 9623 rval = COMMAND_DONE_ERROR; 9624 goto check_keys; 9625 9626 case KEY_HARDWARE_ERROR: 9627 ST_DO_ERRSTATS(un, st_harderrs); 9628 severity = SCSI_ERR_FATAL; 9629 rval = COMMAND_DONE_ERROR; 9630 if (un->un_dp->options & ST_EJECT_ON_CHANGER_FAILURE) 9631 un->un_eject_tape_on_failure = st_check_asc_ascq(un); 9632 break; 9633 9634 case KEY_BLANK_CHECK: 9635 ST_DO_ERRSTATS(un, st_softerrs); 9636 severity = SCSI_ERR_INFO; 9637 9638 /* 9639 * if not a special request and some data was xferred then it 9640 * it is not an error yet 9641 */ 9642 if (bp != un->un_sbufp && (bp->b_flags & B_READ)) { 9643 /* 9644 * no error for read with or without data xferred 9645 */ 9646 un->un_status = SUN_KEY_EOT; 9647 un->un_pos.eof = ST_EOT; 9648 rval = COMMAND_DONE_ERROR; 9649 SET_PE_FLAG(un); 9650 goto check_keys; 9651 } else if (bp != un->un_sbufp && 9652 (pkt->pkt_state & STATE_XFERRED_DATA)) { 9653 rval = COMMAND_DONE; 9654 } else { 9655 rval = COMMAND_DONE_ERROR_RECOVERED; 9656 } 9657 9658 if (un->un_laststate >= ST_STATE_OPEN) { 9659 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 9660 "blank check\n"); 9661 un->un_pos.eof = ST_EOM; 9662 } 9663 if ((CDBP(pkt)->scc_cmd == SCMD_LOCATE) || 9664 (CDBP(pkt)->scc_cmd == SCMD_LOCATE_G4) || 9665 (CDBP(pkt)->scc_cmd == SCMD_SPACE) && 9666 (un->un_dp->options & ST_KNOWS_EOD)) { 9667 /* 9668 * we were doing a fast forward by skipping 9669 * multiple fmk at the time 9670 */ 9671 st_bioerror(bp, EIO); 9672 severity = SCSI_ERR_RECOVERED; 9673 rval = COMMAND_DONE; 9674 } 9675 SET_PE_FLAG(un); 9676 goto check_keys; 9677 9678 case KEY_WRITE_PROTECT: 9679 if (st_wrongtapetype(un)) { 9680 un->un_status = SUN_KEY_WRONGMEDIA; 9681 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 9682 "wrong tape for writing- use DC6150 tape (or equivalent)\n"); 9683 severity = SCSI_ERR_UNKNOWN; 9684 } else { 9685 severity = SCSI_ERR_FATAL; 9686 } 9687 ST_DO_ERRSTATS(un, st_harderrs); 9688 rval = COMMAND_DONE_ERROR; 9689 st_bioerror(bp, EACCES); 9690 break; 9691 9692 case KEY_UNIT_ATTENTION: 9693 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 9694 "KEY_UNIT_ATTENTION : un_state = %d\n", un->un_state); 9695 9696 /* 9697 * If we have detected a Bus Reset and the tape 9698 * drive has been reserved. 9699 */ 9700 if (ST_RQSENSE->es_add_code == 0x29 && 9701 (un->un_rsvd_status & ST_RESERVE)) { 9702 un->un_rsvd_status |= ST_LOST_RESERVE; 9703 ST_DEBUG(ST_DEVINFO, st_label, CE_WARN, 9704 "st_decode_sense: Lost Reservation\n"); 9705 } 9706 9707 if (un->un_state <= ST_STATE_OPENING) { 9708 /* 9709 * Look, the tape isn't open yet, now determine 9710 * if the cause is a BUS RESET, Save the file and 9711 * Block positions for the callers to recover from 9712 * the loss of position. 9713 */ 9714 if (un->un_pos.pmode != invalid) { 9715 if (ST_RQSENSE->es_add_code == 0x29) { 9716 un->un_save_fileno = un->un_pos.fileno; 9717 un->un_save_blkno = un->un_pos.blkno; 9718 un->un_restore_pos = 1; 9719 } 9720 } 9721 9722 if ((int)un->un_retry_ct++ < st_retry_count) { 9723 rval = QUE_COMMAND; 9724 } else { 9725 rval = COMMAND_DONE_ERROR; 9726 } 9727 severity = SCSI_ERR_INFO; 9728 9729 } else { 9730 /* 9731 * Check if it is an Unexpected Unit Attention. 9732 * If state is >= ST_STATE_OPEN, we have 9733 * already done the initialization . 9734 * In this case it is Fatal Error 9735 * since no further reading/writing 9736 * can be done with fileno set to < 0. 9737 */ 9738 if (un->un_state >= ST_STATE_OPEN) { 9739 ST_DO_ERRSTATS(un, st_harderrs); 9740 severity = SCSI_ERR_FATAL; 9741 } else { 9742 severity = SCSI_ERR_INFO; 9743 } 9744 rval = COMMAND_DONE_ERROR; 9745 } 9746 un->un_pos.pmode = invalid; 9747 9748 break; 9749 9750 case KEY_NOT_READY: 9751 /* 9752 * If in process of getting ready retry. 9753 */ 9754 if (sensep->es_add_code == 0x04 && 9755 sensep->es_qual_code == 0x01 && 9756 un->un_retry_ct++ < st_retry_count) { 9757 rval = QUE_COMMAND; 9758 severity = SCSI_ERR_INFO; 9759 } else { 9760 /* give up */ 9761 rval = COMMAND_DONE_ERROR; 9762 severity = SCSI_ERR_FATAL; 9763 } 9764 9765 /* 9766 * If this was an error and after device opened 9767 * do error stats. 9768 */ 9769 if (rval == COMMAND_DONE_ERROR && 9770 un->un_state > ST_STATE_OPENING) { 9771 ST_DO_ERRSTATS(un, st_harderrs); 9772 } 9773 9774 if (ST_RQSENSE->es_add_code == 0x3a) { 9775 if (st_error_level >= SCSI_ERR_FATAL) 9776 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 9777 "Tape not inserted in drive\n"); 9778 un->un_mediastate = MTIO_EJECTED; 9779 cv_broadcast(&un->un_state_cv); 9780 } 9781 if ((un->un_dp->options & ST_EJECT_ON_CHANGER_FAILURE) && 9782 (rval != QUE_COMMAND)) 9783 un->un_eject_tape_on_failure = st_check_asc_ascq(un); 9784 break; 9785 9786 case KEY_ABORTED_COMMAND: 9787 9788 /* 9789 * Probably a parity error... 9790 * if we retry here then this may cause data to be 9791 * written twice or data skipped during reading 9792 */ 9793 ST_DO_ERRSTATS(un, st_harderrs); 9794 severity = SCSI_ERR_FATAL; 9795 rval = COMMAND_DONE_ERROR; 9796 goto check_keys; 9797 9798 default: 9799 /* 9800 * Undecoded sense key. Try retries and hope 9801 * that will fix the problem. Otherwise, we're 9802 * dead. 9803 */ 9804 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 9805 "Unhandled Sense Key '%s'\n", 9806 sense_keys[un->un_status]); 9807 ST_DO_ERRSTATS(un, st_harderrs); 9808 severity = SCSI_ERR_FATAL; 9809 rval = COMMAND_DONE_ERROR; 9810 goto check_keys; 9811 } 9812 9813 if ((!(pkt->pkt_flags & FLAG_SILENT) && 9814 un->un_state >= ST_STATE_OPEN) && (DEBUGGING || 9815 (un->un_laststate > ST_STATE_OPENING) && 9816 (severity >= st_error_level))) { 9817 9818 scsi_errmsg(ST_SCSI_DEVP, pkt, st_label, severity, 9819 un->un_pos.lgclblkno, un->un_err_pos.lgclblkno, 9820 scsi_cmds, sensep); 9821 if (sensep->es_filmk) { 9822 scsi_log(ST_DEVINFO, st_label, CE_CONT, 9823 "File Mark Detected\n"); 9824 } 9825 if (sensep->es_eom) { 9826 scsi_log(ST_DEVINFO, st_label, CE_CONT, 9827 "End-of-Media Detected\n"); 9828 } 9829 if (sensep->es_ili) { 9830 scsi_log(ST_DEVINFO, st_label, CE_CONT, 9831 "Incorrect Length Indicator Set\n"); 9832 } 9833 } 9834 get_error = geterror(bp); 9835 if (((rval == COMMAND_DONE_ERROR) || 9836 (rval == COMMAND_DONE_ERROR_RECOVERED)) && 9837 ((get_error == EIO) || (get_error == 0))) { 9838 un->un_rqs_state |= (ST_RQS_ERROR | ST_RQS_VALID); 9839 bcopy(ST_RQSENSE, un->un_uscsi_rqs_buf, SENSE_LENGTH); 9840 if (un->un_rqs_state & ST_RQS_READ) { 9841 un->un_rqs_state &= ~(ST_RQS_READ); 9842 } else { 9843 un->un_rqs_state |= ST_RQS_OVR; 9844 } 9845 } 9846 9847 return (rval); 9848 } 9849 9850 9851 static int 9852 st_handle_intr_retry_lcmd(struct scsi_tape *un, struct buf *bp) 9853 { 9854 int status = TRAN_ACCEPT; 9855 9856 mutex_enter(ST_MUTEX); 9857 9858 ST_FUNC(ST_DEVINFO, st_handle_intr_retry_lcmd); 9859 9860 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 9861 "st_handle_intr_rtr_lcmd(), un = 0x%p\n", (void *)un); 9862 9863 /* 9864 * Check to see if we hit the retry timeout. We check to make sure 9865 * this is the first one on the runq and make sure we have not 9866 * queued up any more, so this one has to be the last on the list 9867 * also. If it is not, we have to fail. If it is not the first, but 9868 * is the last we are in trouble anyway, as we are in the interrupt 9869 * context here. 9870 */ 9871 if (((int)un->un_retry_ct > st_retry_count) || 9872 ((un->un_runqf != bp) && (un->un_runql != bp))) { 9873 goto exit; 9874 } 9875 9876 if (un->un_throttle) { 9877 un->un_last_throttle = un->un_throttle; 9878 un->un_throttle = 0; 9879 } 9880 9881 /* 9882 * Here we know : bp is the first and last one on the runq 9883 * it is not necessary to put it back on the head of the 9884 * waitq and then move from waitq to runq. Save this queuing 9885 * and call scsi_transport. 9886 */ 9887 9888 mutex_exit(ST_MUTEX); 9889 9890 status = scsi_transport(BP_PKT(bp)); 9891 9892 mutex_enter(ST_MUTEX); 9893 9894 if (status == TRAN_ACCEPT) { 9895 un->un_tran_retry_ct = 0; 9896 if (un->un_last_throttle) { 9897 un->un_throttle = un->un_last_throttle; 9898 } 9899 mutex_exit(ST_MUTEX); 9900 9901 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 9902 "restart transport \n"); 9903 return (0); 9904 } 9905 9906 ST_DO_KSTATS(bp, kstat_runq_back_to_waitq); 9907 mutex_exit(ST_MUTEX); 9908 9909 if (status == TRAN_BUSY) { 9910 if (st_handle_intr_busy(un, bp, ST_TRAN_BUSY_TIMEOUT) == 0) { 9911 return (0); 9912 } 9913 } 9914 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 9915 "restart transport rejected\n"); 9916 mutex_enter(ST_MUTEX); 9917 ST_DO_ERRSTATS(un, st_transerrs); 9918 if (un->un_last_throttle) { 9919 un->un_throttle = un->un_last_throttle; 9920 } 9921 exit: 9922 mutex_exit(ST_MUTEX); 9923 return (-1); 9924 } 9925 9926 static int 9927 st_wrongtapetype(struct scsi_tape *un) 9928 { 9929 9930 ST_FUNC(ST_DEVINFO, st_wrongtapetype); 9931 9932 ASSERT(mutex_owned(ST_MUTEX)); 9933 9934 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, "st_wrongtapetype()\n"); 9935 9936 /* 9937 * Hack to handle 600A, 600XTD, 6150 && 660 vs. 300XL tapes... 9938 */ 9939 if (un->un_dp && (un->un_dp->options & ST_QIC) && un->un_mspl) { 9940 switch (un->un_dp->type) { 9941 case ST_TYPE_WANGTEK: 9942 case ST_TYPE_ARCHIVE: 9943 /* 9944 * If this really worked, we could go off of 9945 * the density codes set in the modesense 9946 * page. For this drive, 0x10 == QIC-120, 9947 * 0xf == QIC-150, and 0x5 should be for 9948 * both QIC-24 and, maybe, QIC-11. However, 9949 * the h/w doesn't do what the manual says 9950 * that it should, so we'll key off of 9951 * getting a WRITE PROTECT error AND wp *not* 9952 * set in the mode sense information. 9953 */ 9954 /* 9955 * XXX but we already know that status is 9956 * write protect, so don't check it again. 9957 */ 9958 9959 if (un->un_status == KEY_WRITE_PROTECT && 9960 un->un_mspl->wp == 0) { 9961 return (1); 9962 } 9963 break; 9964 default: 9965 break; 9966 } 9967 } 9968 return (0); 9969 } 9970 9971 static int 9972 st_check_error(struct scsi_tape *un, struct scsi_pkt *pkt) 9973 { 9974 int action; 9975 9976 ST_FUNC(ST_DEVINFO, st_check_error); 9977 9978 ASSERT(mutex_owned(ST_MUTEX)); 9979 9980 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, "st_check_error()\n"); 9981 9982 if (SCBP_C(pkt) == STATUS_RESERVATION_CONFLICT) { 9983 action = COMMAND_DONE_EACCES; 9984 un->un_rsvd_status |= ST_RESERVATION_CONFLICT; 9985 } else if (SCBP(pkt)->sts_busy) { 9986 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, "unit busy\n"); 9987 if ((int)un->un_retry_ct++ < st_retry_count) { 9988 action = QUE_BUSY_COMMAND; 9989 } else if ((un->un_rsvd_status & 9990 (ST_RESERVE | ST_APPLICATION_RESERVATIONS)) == 0) { 9991 /* 9992 * If this is a command done before reserve is done 9993 * don't reset. 9994 */ 9995 action = COMMAND_DONE_ERROR; 9996 } else { 9997 ST_DEBUG2(ST_DEVINFO, st_label, CE_WARN, 9998 "unit busy too long\n"); 9999 mutex_exit(ST_MUTEX); 10000 if (scsi_reset(ROUTE, RESET_TARGET) == 0) { 10001 (void) scsi_reset(ROUTE, RESET_ALL); 10002 } 10003 mutex_enter(ST_MUTEX); 10004 action = COMMAND_DONE_ERROR; 10005 } 10006 } else if (SCBP(pkt)->sts_chk) { 10007 /* 10008 * we should only get here if the auto rqsense failed 10009 * thru a uscsi cmd without autorequest sense 10010 * so we just try again 10011 */ 10012 action = QUE_SENSE; 10013 } else { 10014 action = COMMAND_DONE; 10015 } 10016 return (action); 10017 } 10018 10019 static void 10020 st_calc_bnum(struct scsi_tape *un, struct buf *bp) 10021 { 10022 int nblks; 10023 10024 ST_FUNC(ST_DEVINFO, st_calc_bnum); 10025 10026 ASSERT(mutex_owned(ST_MUTEX)); 10027 10028 /* If variable block mode */ 10029 if (un->un_bsize == 0) { 10030 nblks = ((bp->b_bcount - bp->b_resid == 0) ? 0 : 1); 10031 un->un_kbytes_xferred += (bp->b_bcount - bp->b_resid) / ONE_K; 10032 } else { 10033 nblks = ((bp->b_bcount - bp->b_resid) / un->un_bsize); 10034 un->un_kbytes_xferred += (nblks * un->un_bsize) / ONE_K; 10035 } 10036 un->un_pos.blkno += nblks; 10037 un->un_pos.lgclblkno += nblks; 10038 } 10039 10040 static void 10041 st_set_state(struct scsi_tape *un) 10042 { 10043 struct buf *bp = un->un_runqf; 10044 struct scsi_pkt *sp = BP_PKT(bp); 10045 struct uscsi_cmd *ucmd; 10046 10047 ST_FUNC(ST_DEVINFO, st_set_state); 10048 10049 ASSERT(mutex_owned(ST_MUTEX)); 10050 10051 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 10052 "st_set_state(): eof=%x fmneeded=%x pkt_resid=0x%lx (%ld)\n", 10053 un->un_pos.eof, un->un_fmneeded, sp->pkt_resid, sp->pkt_resid); 10054 10055 if (bp != un->un_sbufp) { 10056 #ifdef STDEBUG 10057 if (DEBUGGING && sp->pkt_resid) { 10058 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 10059 "pkt_resid %ld bcount %ld\n", 10060 sp->pkt_resid, bp->b_bcount); 10061 } 10062 #endif 10063 bp->b_resid = sp->pkt_resid; 10064 st_calc_bnum(un, bp); 10065 if (bp->b_flags & B_READ) { 10066 un->un_lastop = ST_OP_READ; 10067 un->un_fmneeded = 0; 10068 } else { 10069 un->un_lastop = ST_OP_WRITE; 10070 if (un->un_dp->options & ST_REEL) { 10071 un->un_fmneeded = 2; 10072 } else { 10073 un->un_fmneeded = 1; 10074 } 10075 } 10076 /* 10077 * all is honky dory at this point, so let's 10078 * readjust the throttle, to increase speed, if we 10079 * have not throttled down. 10080 */ 10081 if (un->un_throttle) { 10082 un->un_throttle = un->un_max_throttle; 10083 } 10084 } else { 10085 optype new_lastop; 10086 uchar_t cmd = (uchar_t)(intptr_t)bp->b_forw; 10087 10088 un->un_lastop = ST_OP_CTL; 10089 10090 switch (cmd) { 10091 case SCMD_WRITE: 10092 bp->b_resid = sp->pkt_resid; 10093 new_lastop = ST_OP_WRITE; 10094 st_calc_bnum(un, bp); 10095 if (un->un_dp->options & ST_REEL) { 10096 un->un_fmneeded = 2; 10097 } else { 10098 un->un_fmneeded = 1; 10099 } 10100 break; 10101 case SCMD_READ: 10102 bp->b_resid = sp->pkt_resid; 10103 new_lastop = ST_OP_READ; 10104 st_calc_bnum(un, bp); 10105 un->un_fmneeded = 0; 10106 break; 10107 case SCMD_WRITE_FILE_MARK: 10108 { 10109 int fmdone; 10110 10111 if (un->un_pos.eof != ST_EOM) { 10112 un->un_pos.eof = ST_NO_EOF; 10113 } 10114 fmdone = (bp->b_bcount - bp->b_resid); 10115 if (fmdone > 0) { 10116 un->un_lastop = new_lastop = ST_OP_WEOF; 10117 un->un_pos.lgclblkno += fmdone; 10118 un->un_pos.fileno += fmdone; 10119 un->un_pos.blkno = 0; 10120 } else { 10121 new_lastop = ST_OP_CTL; 10122 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 10123 "Flushed buffer\n"); 10124 } 10125 if (fmdone > un->un_fmneeded) { 10126 un->un_fmneeded = 0; 10127 } else { 10128 un->un_fmneeded -= fmdone; 10129 } 10130 break; 10131 } 10132 case SCMD_REWIND: 10133 un->un_pos.eof = ST_NO_EOF; 10134 un->un_pos.fileno = 0; 10135 un->un_pos.blkno = 0; 10136 un->un_pos.lgclblkno = 0; 10137 un->un_pos.pmode = legacy; 10138 new_lastop = ST_OP_CTL; 10139 break; 10140 10141 case SCMD_SPACE: 10142 { 10143 int count; 10144 long resid; 10145 int done; 10146 10147 count = (int)SPACE_CNT(bp->b_bcount); 10148 /* if was a uscsi space cmd b_bcount == 0 */ 10149 if (count == 0) { 10150 count = 10151 (sp->pkt_cdbp[2] << 16) | 10152 (sp->pkt_cdbp[3] << 8) | 10153 (sp->pkt_cdbp[4]); 10154 } 10155 resid = (long)SPACE_CNT(bp->b_resid); 10156 if (count >= 0) { 10157 done = (count - resid); 10158 } else { 10159 done = ((-count) - resid); 10160 } 10161 if (done > 0) { 10162 un->un_lastop = new_lastop = ST_OP_CTL; 10163 } else { 10164 new_lastop = ST_OP_CTL; 10165 } 10166 10167 ST_SPAC(ST_DEVINFO, st_label, SCSI_DEBUG, 10168 "space cmd: cdb[1] = %s\n" 10169 "space data: = 0x%lx\n" 10170 "space count: = %d\n" 10171 "space resid: = %ld\n" 10172 "spaces done: = %d\n" 10173 "fileno before = %d\n" 10174 "blkno before = %d\n", 10175 space_strs[sp->pkt_cdbp[1] & 7], 10176 bp->b_bcount, 10177 count, resid, done, 10178 un->un_pos.fileno, un->un_pos.blkno); 10179 10180 switch (sp->pkt_cdbp[1]) { 10181 case SPACE_TYPE(SP_FLM): 10182 /* Space file forward */ 10183 if (count >= 0) { 10184 if (un->un_pos.eof <= ST_EOF) { 10185 un->un_pos.eof = ST_NO_EOF; 10186 } 10187 un->un_pos.fileno += done; 10188 un->un_pos.blkno = 0; 10189 break; 10190 } 10191 /* Space file backward */ 10192 if (done > un->un_pos.fileno) { 10193 un->un_pos.fileno = 0; 10194 un->un_pos.blkno = 0; 10195 } else { 10196 un->un_pos.fileno -= done; 10197 un->un_pos.blkno = INF; 10198 } 10199 break; 10200 case SPACE_TYPE(SP_BLK): 10201 /* Space block forward */ 10202 if (count >= 0) { 10203 un->un_pos.blkno += done; 10204 break; 10205 } 10206 /* Space block backward */ 10207 if (un->un_pos.eof >= ST_EOF_PENDING) { 10208 /* 10209 * we stepped back into 10210 * a previous file; we are not 10211 * making an effort to pretend that 10212 * we are still in the current file 10213 * ie. logical == physical position 10214 * and leave it to st_ioctl to correct 10215 */ 10216 if (done > un->un_pos.blkno) { 10217 un->un_pos.blkno = 0; 10218 } else { 10219 un->un_pos.fileno--; 10220 un->un_pos.blkno = INF; 10221 } 10222 } else { 10223 un->un_pos.blkno -= done; 10224 } 10225 break; 10226 case SPACE_TYPE(SP_SQFLM): 10227 un->un_pos.pmode = logical; 10228 un->un_pos.blkno = 0; 10229 un->un_lastop = new_lastop = ST_OP_CTL; 10230 break; 10231 case SPACE_TYPE(SP_EOD): 10232 un->un_pos.pmode = logical; 10233 un->un_pos.eof = ST_EOM; 10234 un->un_status = KEY_BLANK_CHECK; 10235 break; 10236 default: 10237 un->un_pos.pmode = invalid; 10238 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 10239 "Unsupported space cmd: %s\n", 10240 space_strs[sp->pkt_cdbp[1] & 7]); 10241 10242 un->un_lastop = new_lastop = ST_OP_CTL; 10243 } 10244 10245 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 10246 "after_space rs %ld fil %d blk %d\n", 10247 resid, un->un_pos.fileno, un->un_pos.blkno); 10248 10249 break; 10250 } 10251 case SCMD_LOAD: 10252 if ((bp->b_bcount & (LD_LOAD | LD_EOT)) == LD_LOAD) { 10253 un->un_pos.fileno = 0; 10254 un->un_pos.pmode = legacy; 10255 } else { 10256 un->un_state = ST_STATE_OFFLINE; 10257 un->un_pos.pmode = invalid; 10258 } 10259 un->un_density_known = 0; 10260 un->un_pos.eof = ST_NO_EOF; 10261 un->un_pos.blkno = 0; 10262 un->un_lastop = new_lastop = ST_OP_CTL; 10263 break; 10264 case SCMD_ERASE: 10265 un->un_pos.eof = ST_NO_EOF; 10266 un->un_pos.blkno = 0; 10267 un->un_pos.fileno = 0; 10268 un->un_pos.lgclblkno = 0; 10269 un->un_pos.pmode = legacy; 10270 new_lastop = ST_OP_CTL; 10271 break; 10272 case SCMD_RESERVE: 10273 un->un_rsvd_status |= ST_RESERVE; 10274 un->un_rsvd_status &= 10275 ~(ST_RELEASE | ST_LOST_RESERVE | 10276 ST_RESERVATION_CONFLICT); 10277 new_lastop = un->un_lastop; 10278 break; 10279 case SCMD_RELEASE: 10280 un->un_rsvd_status |= ST_RELEASE; 10281 un->un_rsvd_status &= 10282 ~(ST_RESERVE | ST_LOST_RESERVE | 10283 ST_RESERVATION_CONFLICT); 10284 new_lastop = ST_OP_CTL; 10285 break; 10286 case SCMD_PERSISTENT_RESERVE_IN: 10287 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 10288 "PGR_IN command\n"); 10289 break; 10290 case SCMD_PERSISTENT_RESERVE_OUT: 10291 switch (sp->pkt_cdbp[1] & ST_SA_MASK) { 10292 case ST_SA_SCSI3_RESERVE: 10293 case ST_SA_SCSI3_PREEMPT: 10294 case ST_SA_SCSI3_PREEMPTANDABORT: 10295 un->un_rsvd_status |= 10296 ST_APPLICATION_RESERVATIONS; 10297 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 10298 "PGR Reserve and set: entering" 10299 " ST_APPLICATION_RESERVATIONS mode"); 10300 break; 10301 case ST_SA_SCSI3_RELEASE: 10302 case ST_SA_SCSI3_CLEAR: 10303 un->un_rsvd_status &= 10304 ~ST_APPLICATION_RESERVATIONS; 10305 ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN, 10306 "PGR Release and reset: exiting" 10307 " ST_APPLICATION_RESERVATIONS mode"); 10308 break; 10309 } 10310 break; 10311 case SCMD_TEST_UNIT_READY: 10312 case SCMD_READ_BLKLIM: 10313 case SCMD_REQUEST_SENSE: 10314 case SCMD_INQUIRY: 10315 case SCMD_RECOVER_BUF: 10316 case SCMD_MODE_SELECT: 10317 case SCMD_MODE_SENSE: 10318 case SCMD_DOORLOCK: 10319 case SCMD_READ_BUFFER: 10320 case SCMD_REPORT_DENSITIES: 10321 case SCMD_LOG_SELECT_G1: 10322 case SCMD_LOG_SENSE_G1: 10323 case SCMD_REPORT_LUNS: 10324 case SCMD_READ_ATTRIBUTE: 10325 case SCMD_READ_MEDIA_SERIAL: 10326 new_lastop = ST_OP_CTL; 10327 break; 10328 case SCMD_READ_POSITION: 10329 new_lastop = ST_OP_CTL; 10330 if (USCSI_CMD(bp)) { 10331 (void) st_get_read_pos(un, bp); 10332 } 10333 break; 10334 case SCMD_LOCATE: 10335 case SCMD_LOCATE_G4: 10336 /* Locate makes position mode no longer legacy */ 10337 un->un_lastop = new_lastop = ST_OP_CTL; 10338 break; 10339 default: 10340 /* 10341 * Unknown command, If was USCSI and USCSI_SILENT 10342 * flag was not set, set position to unknown. 10343 */ 10344 if ((((ucmd = BP_UCMD(bp)) != NULL) && 10345 (ucmd->uscsi_flags & USCSI_SILENT) == 0)) { 10346 ST_DEBUG2(ST_DEVINFO, st_label, CE_WARN, 10347 "unknown cmd 0x%X caused loss of state\n", 10348 cmd); 10349 } else { 10350 break; 10351 } 10352 /* FALLTHROUGH */ 10353 case SCMD_WRITE_BUFFER: /* Writes new firmware to device */ 10354 un->un_pos.pmode = invalid; 10355 un->un_lastop = new_lastop = ST_OP_CTL; 10356 break; 10357 } 10358 10359 /* new_lastop should have been changed */ 10360 ASSERT(new_lastop != ST_OP_NIL); 10361 10362 /* If un_lastop should copy new_lastop */ 10363 if (((un->un_lastop == ST_OP_WRITE) || 10364 (un->un_lastop == ST_OP_WEOF)) && 10365 new_lastop != ST_OP_CTL) { 10366 un->un_lastop = new_lastop; 10367 } 10368 } 10369 10370 /* 10371 * In the st driver we have a logical and physical file position. 10372 * Under BSD behavior, when you get a zero read, the logical position 10373 * is before the filemark but after the last record of the file. 10374 * The physical position is after the filemark. MTIOCGET should always 10375 * return the logical file position. 10376 * 10377 * The next read gives a silent skip to the next file. 10378 * Under SVR4, the logical file position remains before the filemark 10379 * until the file is closed or a space operation is performed. 10380 * Hence set err_resid and err_file before changing fileno if case 10381 * BSD Behaviour. 10382 */ 10383 un->un_err_resid = bp->b_resid; 10384 COPY_POS(&un->un_err_pos, &un->un_pos); 10385 un->un_retry_ct = 0; 10386 10387 10388 /* 10389 * If we've seen a filemark via the last read operation 10390 * advance the file counter, but mark things such that 10391 * the next read operation gets a zero count. We have 10392 * to put this here to handle the case of sitting right 10393 * at the end of a tape file having seen the file mark, 10394 * but the tape is closed and then re-opened without 10395 * any further i/o. That is, the position information 10396 * must be updated before a close. 10397 */ 10398 10399 if (un->un_lastop == ST_OP_READ && un->un_pos.eof == ST_EOF_PENDING) { 10400 /* 10401 * If we're a 1/2" tape, and we get a filemark 10402 * right on block 0, *AND* we were not in the 10403 * first file on the tape, and we've hit logical EOM. 10404 * We'll mark the state so that later we do the 10405 * right thing (in st_close(), st_strategy() or 10406 * st_ioctl()). 10407 * 10408 */ 10409 if ((un->un_dp->options & ST_REEL) && 10410 !(un->un_dp->options & ST_READ_IGNORE_EOFS) && 10411 un->un_pos.blkno == 0 && un->un_pos.fileno > 0) { 10412 un->un_pos.eof = ST_EOT_PENDING; 10413 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 10414 "eot pending\n"); 10415 un->un_pos.fileno++; 10416 un->un_pos.blkno = 0; 10417 } else if (BSD_BEHAVIOR) { 10418 /* 10419 * If the read of the filemark was a side effect 10420 * of reading some blocks (i.e., data was actually 10421 * read), then the EOF mark is pending and the 10422 * bump into the next file awaits the next read 10423 * operation (which will return a zero count), or 10424 * a close or a space operation, else the bump 10425 * into the next file occurs now. 10426 */ 10427 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 10428 "resid=%lx, bcount=%lx\n", 10429 bp->b_resid, bp->b_bcount); 10430 10431 if (bp->b_resid != bp->b_bcount) { 10432 un->un_pos.eof = ST_EOF; 10433 } else { 10434 un->un_silent_skip = 1; 10435 un->un_pos.eof = ST_NO_EOF; 10436 un->un_pos.fileno++; 10437 un->un_pos.lgclblkno++; 10438 un->un_save_blkno = un->un_pos.blkno; 10439 un->un_pos.blkno = 0; 10440 } 10441 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 10442 "eof of file %d, eof=%d\n", 10443 un->un_pos.fileno, un->un_pos.eof); 10444 } else if (SVR4_BEHAVIOR) { 10445 /* 10446 * If the read of the filemark was a side effect 10447 * of reading some blocks (i.e., data was actually 10448 * read), then the next read should return 0 10449 */ 10450 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 10451 "resid=%lx, bcount=%lx\n", 10452 bp->b_resid, bp->b_bcount); 10453 if (bp->b_resid == bp->b_bcount) { 10454 un->un_pos.eof = ST_EOF; 10455 } 10456 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 10457 "eof of file=%d, eof=%d\n", 10458 un->un_pos.fileno, un->un_pos.eof); 10459 } 10460 } 10461 } 10462 10463 /* 10464 * set the correct un_errno, to take corner cases into consideration 10465 */ 10466 static void 10467 st_set_pe_errno(struct scsi_tape *un) 10468 { 10469 ST_FUNC(ST_DEVINFO, st_set_pe_errno); 10470 10471 ASSERT(mutex_owned(ST_MUTEX)); 10472 10473 /* if errno is already set, don't reset it */ 10474 if (un->un_errno) 10475 return; 10476 10477 /* here un_errno == 0 */ 10478 /* 10479 * if the last transfer before flushing all the 10480 * waiting I/O's, was 0 (resid = count), then we 10481 * want to give the user an error on all the rest, 10482 * so here. If there was a transfer, we set the 10483 * resid and counts to 0, and let it drop through, 10484 * giving a zero return. the next I/O will then 10485 * give an error. 10486 */ 10487 if (un->un_last_resid == un->un_last_count) { 10488 switch (un->un_pos.eof) { 10489 case ST_EOM: 10490 un->un_errno = ENOMEM; 10491 break; 10492 case ST_EOT: 10493 case ST_EOF: 10494 un->un_errno = EIO; 10495 break; 10496 } 10497 } else { 10498 /* 10499 * we know they did not have a zero, so make 10500 * sure they get one 10501 */ 10502 un->un_last_resid = un->un_last_count = 0; 10503 } 10504 } 10505 10506 10507 /* 10508 * send in a marker pkt to terminate flushing of commands by BBA (via 10509 * flush-on-errors) property. The HBA will always return TRAN_ACCEPT 10510 */ 10511 static void 10512 st_hba_unflush(struct scsi_tape *un) 10513 { 10514 ST_FUNC(ST_DEVINFO, st_hba_unflush); 10515 10516 ASSERT(mutex_owned(ST_MUTEX)); 10517 10518 if (!un->un_flush_on_errors) 10519 return; 10520 10521 #ifdef FLUSH_ON_ERRORS 10522 10523 if (!un->un_mkr_pkt) { 10524 un->un_mkr_pkt = scsi_init_pkt(ROUTE, NULL, (struct buf *)NULL, 10525 NULL, 0, 0, 0, SLEEP_FUNC, NULL); 10526 10527 /* we slept, so it must be there */ 10528 pkt->pkt_flags |= FLAG_FLUSH_MARKER; 10529 } 10530 10531 mutex_exit(ST_MUTEX); 10532 scsi_transport(un->un_mkr_pkt); 10533 mutex_enter(ST_MUTEX); 10534 #endif 10535 } 10536 10537 static char * 10538 st_print_scsi_cmd(char cmd) 10539 { 10540 char tmp[64]; 10541 char *cpnt; 10542 10543 cpnt = scsi_cmd_name(cmd, scsi_cmds, tmp); 10544 /* tmp goes out of scope on return and caller sees garbage */ 10545 if (cpnt == tmp) { 10546 cpnt = "Unknown Command"; 10547 } 10548 return (cpnt); 10549 } 10550 10551 static void 10552 st_print_cdb(dev_info_t *dip, char *label, uint_t level, 10553 char *title, char *cdb) 10554 { 10555 int len = scsi_cdb_size[CDB_GROUPID(cdb[0])]; 10556 char buf[256]; 10557 int instance = ddi_get_instance(dip); 10558 struct scsi_tape *un; 10559 10560 un = ddi_get_soft_state(st_state, instance); 10561 10562 ST_FUNC(dip, st_print_cdb); 10563 10564 #ifdef DEBUG 10565 if ((st_debug & 0x180) == 0x100) { 10566 scsi_log(dip, label, level, "node %s cmd %s\n", 10567 st_dev_name(un->un_dev), st_print_scsi_cmd(*cdb)); 10568 return; 10569 } 10570 #endif 10571 (void) sprintf(buf, "%s for cmd(%s)", title, st_print_scsi_cmd(*cdb)); 10572 st_clean_print(dip, label, level, buf, cdb, len); 10573 } 10574 10575 static void 10576 st_clean_print(dev_info_t *dev, char *label, uint_t level, 10577 char *title, char *data, int len) 10578 { 10579 int i; 10580 int c; 10581 char *format; 10582 char buf[256]; 10583 uchar_t byte; 10584 10585 ST_FUNC(dev, st_clean_print); 10586 10587 (void) sprintf(buf, "%s:\n", title); 10588 scsi_log(dev, label, level, "%s", buf); 10589 level = CE_CONT; 10590 for (i = 0; i < len; ) { 10591 buf[0] = 0; 10592 for (c = 0; c < 8 && i < len; c++, i++) { 10593 byte = (uchar_t)data[i]; 10594 if (byte < 0x10) 10595 format = "0x0%x "; 10596 else 10597 format = "0x%x "; 10598 (void) sprintf(&buf[(int)strlen(buf)], format, byte); 10599 } 10600 (void) sprintf(&buf[(int)strlen(buf)], "\n"); 10601 10602 scsi_log(dev, label, level, "%s\n", buf); 10603 } 10604 } 10605 10606 /* 10607 * Conditionally enabled debugging 10608 */ 10609 #ifdef STDEBUG 10610 static void 10611 st_debug_cmds(struct scsi_tape *un, int com, int count, int wait) 10612 { 10613 char tmpbuf[64]; 10614 10615 ST_FUNC(ST_DEVINFO, st_debug_cmds); 10616 10617 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 10618 "cmd=%s count=0x%x (%d) %ssync\n", 10619 scsi_cmd_name(com, scsi_cmds, tmpbuf), 10620 count, count, 10621 wait == ASYNC_CMD ? "a" : ""); 10622 } 10623 10624 /* 10625 * Returns pointer to name of minor node name of device 'dev'. 10626 */ 10627 static char * 10628 st_dev_name(dev_t dev) 10629 { 10630 struct scsi_tape *un; 10631 const char density[] = { 'l', 'm', 'h', 'c' }; 10632 static char name[4]; 10633 minor_t minor; 10634 int instance; 10635 int nprt = 0; 10636 10637 minor = getminor(dev); 10638 instance = ((minor & 0xff80) >> 5) | (minor & 3); 10639 un = ddi_get_soft_state(st_state, instance); 10640 if (un) { 10641 ST_FUNC(ST_DEVINFO, st_dev_name); 10642 } 10643 10644 name[nprt] = density[(minor & MT_DENSITY_MASK) >> 3]; 10645 10646 if (minor & MT_BSD) { 10647 name[++nprt] = 'b'; 10648 } 10649 10650 if (minor & MT_NOREWIND) { 10651 name[++nprt] = 'n'; 10652 } 10653 10654 /* NULL terminator */ 10655 name[++nprt] = 0; 10656 10657 return (name); 10658 } 10659 #endif /* STDEBUG */ 10660 10661 /* 10662 * Soft error reporting, so far unique to each drive 10663 * 10664 * Currently supported: exabyte and DAT soft error reporting 10665 */ 10666 static int 10667 st_report_exabyte_soft_errors(dev_t dev, int flag) 10668 { 10669 uchar_t *sensep; 10670 int amt; 10671 int rval = 0; 10672 char cdb[CDB_GROUP0], *c = cdb; 10673 struct uscsi_cmd *com; 10674 10675 GET_SOFT_STATE(dev); 10676 10677 ST_FUNC(ST_DEVINFO, st_report_exabyte_soft_errors); 10678 10679 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 10680 "st_report_exabyte_soft_errors(dev = 0x%lx, flag = %d)\n", 10681 dev, flag); 10682 10683 ASSERT(mutex_owned(ST_MUTEX)); 10684 10685 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 10686 sensep = kmem_zalloc(TAPE_SENSE_LENGTH, KM_SLEEP); 10687 10688 *c++ = SCMD_REQUEST_SENSE; 10689 *c++ = 0; 10690 *c++ = 0; 10691 *c++ = 0; 10692 *c++ = TAPE_SENSE_LENGTH; 10693 /* 10694 * set CLRCNT (byte 5, bit 7 which clears the error counts) 10695 */ 10696 *c = (char)0x80; 10697 10698 com->uscsi_cdb = cdb; 10699 com->uscsi_cdblen = CDB_GROUP0; 10700 com->uscsi_bufaddr = (caddr_t)sensep; 10701 com->uscsi_buflen = TAPE_SENSE_LENGTH; 10702 com->uscsi_flags = USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ; 10703 com->uscsi_timeout = un->un_dp->non_motion_timeout; 10704 10705 rval = st_ioctl_cmd(dev, com, FKIOCTL); 10706 if (rval || com->uscsi_status) { 10707 goto done; 10708 } 10709 10710 /* 10711 * was there enough data? 10712 */ 10713 amt = (int)TAPE_SENSE_LENGTH - com->uscsi_resid; 10714 10715 if ((amt >= 19) && un->un_kbytes_xferred) { 10716 uint_t count, error_rate; 10717 uint_t rate; 10718 10719 if (sensep[21] & CLN) { 10720 scsi_log(ST_DEVINFO, st_label, CE_WARN, 10721 "Periodic head cleaning required"); 10722 } 10723 if (un->un_kbytes_xferred < (EXABYTE_MIN_TRANSFER/ONE_K)) { 10724 goto done; 10725 } 10726 /* 10727 * check if soft error reporting needs to be done. 10728 */ 10729 count = sensep[16] << 16 | sensep[17] << 8 | sensep[18]; 10730 count &= 0xffffff; 10731 error_rate = (count * 100)/un->un_kbytes_xferred; 10732 10733 #ifdef STDEBUG 10734 if (st_soft_error_report_debug) { 10735 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 10736 "Exabyte Soft Error Report:\n"); 10737 scsi_log(ST_DEVINFO, st_label, CE_CONT, 10738 "read/write error counter: %d\n", count); 10739 scsi_log(ST_DEVINFO, st_label, CE_CONT, 10740 "number of bytes transferred: %dK\n", 10741 un->un_kbytes_xferred); 10742 scsi_log(ST_DEVINFO, st_label, CE_CONT, 10743 "error_rate: %d%%\n", error_rate); 10744 10745 if (amt >= 22) { 10746 scsi_log(ST_DEVINFO, st_label, CE_CONT, 10747 "unit sense: 0x%b 0x%b 0x%b\n", 10748 sensep[19], SENSE_19_BITS, 10749 sensep[20], SENSE_20_BITS, 10750 sensep[21], SENSE_21_BITS); 10751 } 10752 if (amt >= 27) { 10753 scsi_log(ST_DEVINFO, st_label, CE_CONT, 10754 "tracking retry counter: %d\n", 10755 sensep[26]); 10756 scsi_log(ST_DEVINFO, st_label, CE_CONT, 10757 "read/write retry counter: %d\n", 10758 sensep[27]); 10759 } 10760 } 10761 #endif 10762 10763 if (flag & FWRITE) { 10764 rate = EXABYTE_WRITE_ERROR_THRESHOLD; 10765 } else { 10766 rate = EXABYTE_READ_ERROR_THRESHOLD; 10767 } 10768 if (error_rate >= rate) { 10769 scsi_log(ST_DEVINFO, st_label, CE_WARN, 10770 "Soft error rate (%d%%) during %s was too high", 10771 error_rate, 10772 ((flag & FWRITE) ? wrg_str : rdg_str)); 10773 scsi_log(ST_DEVINFO, st_label, CE_CONT, 10774 "Please, replace tape cartridge\n"); 10775 } 10776 } 10777 10778 done: 10779 kmem_free(com, sizeof (*com)); 10780 kmem_free(sensep, TAPE_SENSE_LENGTH); 10781 10782 if (rval != 0) { 10783 scsi_log(ST_DEVINFO, st_label, CE_WARN, 10784 "exabyte soft error reporting failed\n"); 10785 } 10786 return (rval); 10787 } 10788 10789 /* 10790 * this is very specific to Archive 4mm dat 10791 */ 10792 #define ONE_GIG (ONE_K * ONE_K * ONE_K) 10793 10794 static int 10795 st_report_dat_soft_errors(dev_t dev, int flag) 10796 { 10797 uchar_t *sensep; 10798 int amt, i; 10799 int rval = 0; 10800 char cdb[CDB_GROUP1], *c = cdb; 10801 struct uscsi_cmd *com; 10802 10803 GET_SOFT_STATE(dev); 10804 10805 ST_FUNC(ST_DEVINFO, st_report_dat_soft_errors); 10806 10807 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 10808 "st_report_dat_soft_errors(dev = 0x%lx, flag = %d)\n", dev, flag); 10809 10810 ASSERT(mutex_owned(ST_MUTEX)); 10811 10812 com = kmem_zalloc(sizeof (*com), KM_SLEEP); 10813 sensep = kmem_zalloc(LOG_SENSE_LENGTH, KM_SLEEP); 10814 10815 *c++ = SCMD_LOG_SENSE_G1; 10816 *c++ = 0; 10817 *c++ = (flag & FWRITE) ? 0x42 : 0x43; 10818 *c++ = 0; 10819 *c++ = 0; 10820 *c++ = 0; 10821 *c++ = 2; 10822 *c++ = 0; 10823 *c++ = (char)LOG_SENSE_LENGTH; 10824 *c = 0; 10825 com->uscsi_cdb = cdb; 10826 com->uscsi_cdblen = CDB_GROUP1; 10827 com->uscsi_bufaddr = (caddr_t)sensep; 10828 com->uscsi_buflen = LOG_SENSE_LENGTH; 10829 com->uscsi_flags = 10830 USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ; 10831 com->uscsi_timeout = un->un_dp->non_motion_timeout; 10832 rval = st_ioctl_cmd(dev, com, FKIOCTL); 10833 if (rval) { 10834 scsi_log(ST_DEVINFO, st_label, CE_WARN, 10835 "DAT soft error reporting failed\n"); 10836 } 10837 if (rval || com->uscsi_status) { 10838 goto done; 10839 } 10840 10841 /* 10842 * was there enough data? 10843 */ 10844 amt = (int)LOG_SENSE_LENGTH - com->uscsi_resid; 10845 10846 if ((amt >= MIN_LOG_SENSE_LENGTH) && un->un_kbytes_xferred) { 10847 int total, retries, param_code; 10848 10849 total = -1; 10850 retries = -1; 10851 amt = sensep[3] + 4; 10852 10853 10854 #ifdef STDEBUG 10855 if (st_soft_error_report_debug) { 10856 (void) printf("logsense:"); 10857 for (i = 0; i < MIN_LOG_SENSE_LENGTH; i++) { 10858 if (i % 16 == 0) { 10859 (void) printf("\t\n"); 10860 } 10861 (void) printf(" %x", sensep[i]); 10862 } 10863 (void) printf("\n"); 10864 } 10865 #endif 10866 10867 /* 10868 * parse the param_codes 10869 */ 10870 if (sensep[0] == 2 || sensep[0] == 3) { 10871 for (i = 4; i < amt; i++) { 10872 param_code = (sensep[i++] << 8); 10873 param_code += sensep[i++]; 10874 i++; /* skip control byte */ 10875 if (param_code == 5) { 10876 if (sensep[i++] == 4) { 10877 total = (sensep[i++] << 24); 10878 total += (sensep[i++] << 16); 10879 total += (sensep[i++] << 8); 10880 total += sensep[i]; 10881 } 10882 } else if (param_code == 0x8007) { 10883 if (sensep[i++] == 2) { 10884 retries = sensep[i++] << 8; 10885 retries += sensep[i]; 10886 } 10887 } else { 10888 i += sensep[i]; 10889 } 10890 } 10891 } 10892 10893 /* 10894 * if the log sense returned valid numbers then determine 10895 * the read and write error thresholds based on the amount of 10896 * data transferred 10897 */ 10898 10899 if (total > 0 && retries > 0) { 10900 short normal_retries = 0; 10901 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 10902 "total xferred (%s) =%x, retries=%x\n", 10903 ((flag & FWRITE) ? wrg_str : rdg_str), 10904 total, retries); 10905 10906 if (flag & FWRITE) { 10907 if (total <= 10908 WRITE_SOFT_ERROR_WARNING_THRESHOLD) { 10909 normal_retries = 10910 DAT_SMALL_WRITE_ERROR_THRESHOLD; 10911 } else { 10912 normal_retries = 10913 DAT_LARGE_WRITE_ERROR_THRESHOLD; 10914 } 10915 } else { 10916 if (total <= 10917 READ_SOFT_ERROR_WARNING_THRESHOLD) { 10918 normal_retries = 10919 DAT_SMALL_READ_ERROR_THRESHOLD; 10920 } else { 10921 normal_retries = 10922 DAT_LARGE_READ_ERROR_THRESHOLD; 10923 } 10924 } 10925 10926 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 10927 "normal retries=%d\n", normal_retries); 10928 10929 if (retries >= normal_retries) { 10930 scsi_log(ST_DEVINFO, st_label, CE_WARN, 10931 "Soft error rate (retries = %d) during " 10932 "%s was too high", retries, 10933 ((flag & FWRITE) ? wrg_str : rdg_str)); 10934 scsi_log(ST_DEVINFO, st_label, CE_CONT, 10935 "Periodic head cleaning required " 10936 "and/or replace tape cartridge\n"); 10937 } 10938 10939 } else if (total == -1 || retries == -1) { 10940 scsi_log(ST_DEVINFO, st_label, CE_WARN, 10941 "log sense parameter code does not make sense\n"); 10942 } 10943 } 10944 10945 /* 10946 * reset all values 10947 */ 10948 c = cdb; 10949 *c++ = SCMD_LOG_SELECT_G1; 10950 *c++ = 2; /* this resets all values */ 10951 *c++ = (char)0xc0; 10952 *c++ = 0; 10953 *c++ = 0; 10954 *c++ = 0; 10955 *c++ = 0; 10956 *c++ = 0; 10957 *c++ = 0; 10958 *c = 0; 10959 com->uscsi_bufaddr = NULL; 10960 com->uscsi_buflen = 0; 10961 com->uscsi_flags = USCSI_DIAGNOSE | USCSI_SILENT; 10962 rval = st_ioctl_cmd(dev, com, FKIOCTL); 10963 if (rval) { 10964 scsi_log(ST_DEVINFO, st_label, CE_WARN, 10965 "DAT soft error reset failed\n"); 10966 } 10967 done: 10968 kmem_free(com, sizeof (*com)); 10969 kmem_free(sensep, LOG_SENSE_LENGTH); 10970 return (rval); 10971 } 10972 10973 static int 10974 st_report_soft_errors(dev_t dev, int flag) 10975 { 10976 GET_SOFT_STATE(dev); 10977 10978 ST_FUNC(ST_DEVINFO, st_report_soft_errors); 10979 10980 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 10981 "st_report_soft_errors(dev = 0x%lx, flag = %d)\n", dev, flag); 10982 10983 ASSERT(mutex_owned(ST_MUTEX)); 10984 10985 switch (un->un_dp->type) { 10986 case ST_TYPE_EXB8500: 10987 case ST_TYPE_EXABYTE: 10988 return (st_report_exabyte_soft_errors(dev, flag)); 10989 /*NOTREACHED*/ 10990 case ST_TYPE_PYTHON: 10991 return (st_report_dat_soft_errors(dev, flag)); 10992 /*NOTREACHED*/ 10993 default: 10994 un->un_dp->options &= ~ST_SOFT_ERROR_REPORTING; 10995 return (-1); 10996 } 10997 } 10998 10999 /* 11000 * persistent error routines 11001 */ 11002 11003 /* 11004 * enable persistent errors, and set the throttle appropriately, checking 11005 * for flush-on-errors capability 11006 */ 11007 static void 11008 st_turn_pe_on(struct scsi_tape *un) 11009 { 11010 ST_FUNC(ST_DEVINFO, st_turn_pe_on); 11011 11012 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, "st_pe_on\n"); 11013 ASSERT(mutex_owned(ST_MUTEX)); 11014 11015 un->un_persistence = 1; 11016 11017 /* 11018 * only use flush-on-errors if auto-request-sense and untagged-qing are 11019 * enabled. This will simplify the error handling for request senses 11020 */ 11021 11022 if (un->un_arq_enabled && un->un_untagged_qing) { 11023 uchar_t f_o_e; 11024 11025 mutex_exit(ST_MUTEX); 11026 f_o_e = (scsi_ifsetcap(ROUTE, "flush-on-errors", 1, 1) == 1) ? 11027 1 : 0; 11028 mutex_enter(ST_MUTEX); 11029 11030 un->un_flush_on_errors = f_o_e; 11031 } else { 11032 un->un_flush_on_errors = 0; 11033 } 11034 11035 if (un->un_flush_on_errors) 11036 un->un_max_throttle = (uchar_t)st_max_throttle; 11037 else 11038 un->un_max_throttle = 1; 11039 11040 if (un->un_dp->options & ST_RETRY_ON_RECOVERED_DEFERRED_ERROR) 11041 un->un_max_throttle = 1; 11042 11043 /* this will send a marker pkt */ 11044 CLEAR_PE(un); 11045 } 11046 11047 /* 11048 * This turns persistent errors permanently off 11049 */ 11050 static void 11051 st_turn_pe_off(struct scsi_tape *un) 11052 { 11053 ST_FUNC(ST_DEVINFO, st_turn_pe_off); 11054 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, "st_pe_off\n"); 11055 ASSERT(mutex_owned(ST_MUTEX)); 11056 11057 /* turn it off for good */ 11058 un->un_persistence = 0; 11059 11060 /* this will send a marker pkt */ 11061 CLEAR_PE(un); 11062 11063 /* turn off flush on error capability, if enabled */ 11064 if (un->un_flush_on_errors) { 11065 mutex_exit(ST_MUTEX); 11066 (void) scsi_ifsetcap(ROUTE, "flush-on-errors", 0, 1); 11067 mutex_enter(ST_MUTEX); 11068 } 11069 11070 11071 un->un_flush_on_errors = 0; 11072 } 11073 11074 /* 11075 * This clear persistent errors, allowing more commands through, and also 11076 * sending a marker packet. 11077 */ 11078 static void 11079 st_clear_pe(struct scsi_tape *un) 11080 { 11081 ST_FUNC(ST_DEVINFO, st_clear_pe); 11082 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, "st_pe_clear\n"); 11083 ASSERT(mutex_owned(ST_MUTEX)); 11084 11085 un->un_persist_errors = 0; 11086 un->un_throttle = un->un_last_throttle = 1; 11087 un->un_errno = 0; 11088 st_hba_unflush(un); 11089 } 11090 11091 /* 11092 * This will flag persistent errors, shutting everything down, if the 11093 * application had enabled persistent errors via MTIOCPERSISTENT 11094 */ 11095 static void 11096 st_set_pe_flag(struct scsi_tape *un) 11097 { 11098 ST_FUNC(ST_DEVINFO, st_set_pe_flag); 11099 ASSERT(mutex_owned(ST_MUTEX)); 11100 11101 if (un->un_persistence) { 11102 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, "st_pe_flag\n"); 11103 un->un_persist_errors = 1; 11104 un->un_throttle = un->un_last_throttle = 0; 11105 } 11106 } 11107 11108 /* 11109 * List of commands that are allowed to be done while another host holds 11110 * the reservation. 11111 */ 11112 struct { 11113 uchar_t cmd; 11114 uchar_t byte; /* byte to look for data */ 11115 uint32_t mask; /* bits that matter in the above data */ 11116 } rcmds[] = { 11117 { SCMD_TEST_UNIT_READY, 0, 0 }, /* may fail on older drives */ 11118 { SCMD_REQUEST_SENSE, 0, 0 }, 11119 { SCMD_READ_BLKLIM, 0, 0 }, 11120 { SCMD_INQUIRY, 0, 0 }, 11121 { SCMD_RESERVE, 0, 0 }, 11122 { SCMD_RELEASE, 0, 0 }, 11123 { SCMD_DOORLOCK, 4, 3 }, /* allow (unlock) media access only */ 11124 { SCMD_REPORT_DENSITIES, 0, 0 }, 11125 { SCMD_LOG_SENSE_G1, 0, 0 }, 11126 { SCMD_PERSISTENT_RESERVE_IN, 0, 0 }, 11127 { SCMD_PERSISTENT_RESERVE_OUT, 0, 0 }, 11128 { SCMD_REPORT_LUNS, 0, 0 } 11129 }; 11130 11131 static int 11132 st_do_reserve(struct scsi_tape *un) 11133 { 11134 int rval; 11135 11136 ST_FUNC(ST_DEVINFO, st_do_reserve); 11137 11138 /* 11139 * Issue a Throw-Away reserve command to clear the 11140 * check condition. 11141 * If the current behaviour of reserve/release is to 11142 * hold reservation across opens , and if a Bus reset 11143 * has been issued between opens then this command 11144 * would set the ST_LOST_RESERVE flags in rsvd_status. 11145 * In this case return an EACCES so that user knows that 11146 * reservation has been lost in between opens. 11147 * If this error is not returned and we continue with 11148 * successful open , then user may think position of the 11149 * tape is still the same but inreality we would rewind the 11150 * tape and continue from BOT. 11151 */ 11152 rval = st_reserve_release(un, ST_RESERVE); 11153 if (rval) { 11154 if ((un->un_rsvd_status & ST_LOST_RESERVE_BETWEEN_OPENS) == 11155 ST_LOST_RESERVE_BETWEEN_OPENS) { 11156 un->un_rsvd_status &= ~(ST_LOST_RESERVE | ST_RESERVE); 11157 un->un_errno = EACCES; 11158 return (EACCES); 11159 } 11160 rval = st_reserve_release(un, ST_RESERVE); 11161 } 11162 if (rval == 0) { 11163 un->un_rsvd_status |= ST_INIT_RESERVE; 11164 } 11165 11166 return (rval); 11167 } 11168 11169 static int 11170 st_check_cdb_for_need_to_reserve(struct scsi_tape *un, caddr_t cdb) 11171 { 11172 int i; 11173 int rval = 0; 11174 11175 ST_FUNC(ST_DEVINFO, st_check_cdb_for_need_to_reserve); 11176 11177 /* 11178 * If already reserved no need to do it again. 11179 * Also if Reserve and Release are disabled Just return. 11180 */ 11181 if ((un->un_rsvd_status & (ST_RESERVE | ST_APPLICATION_RESERVATIONS)) || 11182 (un->un_dp->options & ST_NO_RESERVE_RELEASE)) { 11183 ST_DEBUG6(ST_DEVINFO, st_label, CE_NOTE, 11184 "st_check_cdb_for_need_to_reserve() reserve unneeded %s", 11185 st_print_scsi_cmd((uchar_t)cdb[0])); 11186 return (0); 11187 } 11188 11189 /* See if command is on the list */ 11190 for (i = 0; i < ST_NUM_MEMBERS(rcmds); i++) { 11191 if ((uchar_t)cdb[0] == rcmds[i].cmd) { 11192 /* 11193 * cmd is on list. 11194 * if byte is zero always allowed. 11195 */ 11196 if (rcmds[i].byte == 0) { 11197 return (rval); 11198 } 11199 if (((cdb[rcmds[i].byte]) & (rcmds[i].mask)) == 0) { 11200 return (rval); 11201 } 11202 break; 11203 } 11204 } 11205 11206 ST_DEBUG6(ST_DEVINFO, st_label, CE_NOTE, 11207 "Command %s requires reservation", st_print_scsi_cmd(cdb[0])); 11208 11209 rval = st_do_reserve(un); 11210 11211 return (rval); 11212 } 11213 11214 static int 11215 st_check_cmd_for_need_to_reserve(struct scsi_tape *un, uchar_t cmd, int cnt) 11216 { 11217 int i; 11218 int rval = 0; 11219 11220 ST_FUNC(ST_DEVINFO, st_check_cmd_for_need_to_reserve); 11221 11222 if ((un->un_rsvd_status & (ST_RESERVE | ST_APPLICATION_RESERVATIONS)) || 11223 (un->un_dp->options & ST_NO_RESERVE_RELEASE)) { 11224 ST_DEBUG6(ST_DEVINFO, st_label, CE_NOTE, 11225 "st_check_cmd_for_need_to_reserve() reserve unneeded %s", 11226 st_print_scsi_cmd(cmd)); 11227 return (0); 11228 } 11229 11230 /* See if command is on the list */ 11231 for (i = 0; i < ST_NUM_MEMBERS(rcmds); i++) { 11232 if (cmd == rcmds[i].cmd) { 11233 /* 11234 * cmd is on list. 11235 * if byte is zero always allowed. 11236 */ 11237 if (rcmds[i].byte == 0) { 11238 return (rval); 11239 } 11240 if (((rcmds[i].mask) & cnt) == 0) { 11241 return (rval); 11242 } 11243 break; 11244 } 11245 } 11246 11247 ST_DEBUG6(ST_DEVINFO, st_label, CE_NOTE, 11248 "Cmd %s requires reservation", st_print_scsi_cmd(cmd)); 11249 11250 rval = st_do_reserve(un); 11251 11252 return (rval); 11253 } 11254 11255 static int 11256 st_reserve_release(struct scsi_tape *un, int cmd) 11257 { 11258 struct uscsi_cmd uscsi_cmd; 11259 struct uscsi_cmd *com = &uscsi_cmd; 11260 int rval; 11261 char cdb[CDB_GROUP0]; 11262 11263 11264 11265 ST_FUNC(ST_DEVINFO, st_reserve_release); 11266 11267 ASSERT(mutex_owned(ST_MUTEX)); 11268 11269 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 11270 "st_reserve_release: %s \n", 11271 (cmd == ST_RELEASE)? "Releasing":"Reserving"); 11272 11273 bzero(cdb, CDB_GROUP0); 11274 if (cmd == ST_RELEASE) { 11275 cdb[0] = SCMD_RELEASE; 11276 } else { 11277 cdb[0] = SCMD_RESERVE; 11278 } 11279 bzero(com, sizeof (struct uscsi_cmd)); 11280 com->uscsi_flags = USCSI_WRITE; 11281 com->uscsi_cdb = cdb; 11282 com->uscsi_cdblen = CDB_GROUP0; 11283 com->uscsi_timeout = un->un_dp->non_motion_timeout; 11284 11285 rval = st_ioctl_cmd(un->un_dev, com, FKIOCTL); 11286 11287 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 11288 "st_reserve_release: rval(1)=%d\n", rval); 11289 11290 if (rval) { 11291 if (com->uscsi_status == STATUS_RESERVATION_CONFLICT) { 11292 rval = EACCES; 11293 } 11294 /* 11295 * dynamically turn off reserve/release support 11296 * in case of drives which do not support 11297 * reserve/release command(ATAPI drives). 11298 */ 11299 if (un->un_status == KEY_ILLEGAL_REQUEST) { 11300 if (un->un_dp->options & ST_NO_RESERVE_RELEASE) { 11301 un->un_dp->options |= ST_NO_RESERVE_RELEASE; 11302 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 11303 "Tape unit does not support " 11304 "reserve/release \n"); 11305 } 11306 rval = 0; 11307 } 11308 } 11309 return (rval); 11310 } 11311 11312 static int 11313 st_take_ownership(dev_t dev) 11314 { 11315 int rval; 11316 11317 GET_SOFT_STATE(dev); 11318 11319 ST_FUNC(ST_DEVINFO, st_take_ownership); 11320 11321 ASSERT(mutex_owned(ST_MUTEX)); 11322 11323 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 11324 "st_take_ownership: Entering ...\n"); 11325 11326 11327 rval = st_reserve_release(un, ST_RESERVE); 11328 /* 11329 * XXX -> Should reset be done only if we get EACCES. 11330 * . 11331 */ 11332 if (rval) { 11333 mutex_exit(ST_MUTEX); 11334 if (scsi_reset(ROUTE, RESET_TARGET) == 0) { 11335 if (scsi_reset(ROUTE, RESET_ALL) == 0) { 11336 mutex_enter(ST_MUTEX); 11337 return (EIO); 11338 } 11339 } 11340 mutex_enter(ST_MUTEX); 11341 un->un_rsvd_status &= 11342 ~(ST_LOST_RESERVE | ST_RESERVATION_CONFLICT); 11343 11344 mutex_exit(ST_MUTEX); 11345 delay(drv_usectohz(ST_RESERVATION_DELAY)); 11346 mutex_enter(ST_MUTEX); 11347 /* 11348 * remove the check condition. 11349 */ 11350 (void) st_reserve_release(un, ST_RESERVE); 11351 if ((rval = st_reserve_release(un, ST_RESERVE)) != 0) { 11352 if ((st_reserve_release(un, ST_RESERVE)) != 0) { 11353 rval = (un->un_rsvd_status & 11354 ST_RESERVATION_CONFLICT) ? EACCES : EIO; 11355 return (rval); 11356 } 11357 } 11358 /* 11359 * Set tape state to ST_STATE_OFFLINE , in case if 11360 * the user wants to continue and start using 11361 * the tape. 11362 */ 11363 un->un_state = ST_STATE_OFFLINE; 11364 un->un_rsvd_status |= ST_INIT_RESERVE; 11365 } 11366 return (rval); 11367 } 11368 11369 static int 11370 st_create_errstats(struct scsi_tape *un, int instance) 11371 { 11372 char kstatname[KSTAT_STRLEN]; 11373 11374 ST_FUNC(ST_DEVINFO, st_create_errstats); 11375 11376 /* 11377 * Create device error kstats 11378 */ 11379 11380 if (un->un_errstats == (kstat_t *)0) { 11381 (void) sprintf(kstatname, "st%d,err", instance); 11382 un->un_errstats = kstat_create("sterr", instance, kstatname, 11383 "device_error", KSTAT_TYPE_NAMED, 11384 sizeof (struct st_errstats) / sizeof (kstat_named_t), 11385 KSTAT_FLAG_PERSISTENT); 11386 11387 if (un->un_errstats) { 11388 struct st_errstats *stp; 11389 11390 stp = (struct st_errstats *)un->un_errstats->ks_data; 11391 kstat_named_init(&stp->st_softerrs, "Soft Errors", 11392 KSTAT_DATA_ULONG); 11393 kstat_named_init(&stp->st_harderrs, "Hard Errors", 11394 KSTAT_DATA_ULONG); 11395 kstat_named_init(&stp->st_transerrs, "Transport Errors", 11396 KSTAT_DATA_ULONG); 11397 kstat_named_init(&stp->st_vid, "Vendor", 11398 KSTAT_DATA_CHAR); 11399 kstat_named_init(&stp->st_pid, "Product", 11400 KSTAT_DATA_CHAR); 11401 kstat_named_init(&stp->st_revision, "Revision", 11402 KSTAT_DATA_CHAR); 11403 kstat_named_init(&stp->st_serial, "Serial No", 11404 KSTAT_DATA_CHAR); 11405 un->un_errstats->ks_private = un; 11406 un->un_errstats->ks_update = nulldev; 11407 kstat_install(un->un_errstats); 11408 /* 11409 * Fill in the static data 11410 */ 11411 (void) strncpy(&stp->st_vid.value.c[0], 11412 ST_INQUIRY->inq_vid, 8); 11413 /* 11414 * XXX: Emulex MT-02 (and emulators) predates 11415 * SCSI-1 and has no vid & pid inquiry data. 11416 */ 11417 if (ST_INQUIRY->inq_len != 0) { 11418 (void) strncpy(&stp->st_pid.value.c[0], 11419 ST_INQUIRY->inq_pid, 16); 11420 (void) strncpy(&stp->st_revision.value.c[0], 11421 ST_INQUIRY->inq_revision, 4); 11422 (void) strncpy(&stp->st_serial.value.c[0], 11423 ST_INQUIRY->inq_serial, 12); 11424 } 11425 } 11426 } 11427 return (0); 11428 } 11429 11430 static int 11431 st_validate_tapemarks(struct scsi_tape *un, tapepos_t *pos) 11432 { 11433 dev_t dev; 11434 int rval; 11435 11436 ST_FUNC(ST_DEVINFO, st_validate_tapemarks); 11437 11438 ASSERT(MUTEX_HELD(&un->un_sd->sd_mutex)); 11439 ASSERT(mutex_owned(ST_MUTEX)); 11440 11441 /* Can't restore an invalid position */ 11442 if (pos->pmode == invalid) { 11443 return (4); 11444 } 11445 11446 /* 11447 * Assumtions: 11448 * If a position was read and is in logical position mode. 11449 * If a drive supports read position it supports locate. 11450 * If the read position type is not NO_POS. even though 11451 * a read position make not have been attemped yet. 11452 * 11453 * The drive can locate to the position. 11454 */ 11455 if (pos->pmode == logical || un->un_read_pos_type != NO_POS) { 11456 /* 11457 * If position mode is logical or legacy mode try 11458 * to locate there as it is faster. 11459 * If it fails try the old way. 11460 */ 11461 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 11462 "Restoring tape position to lgclblkbo=0x%"PRIx64"....", 11463 pos->lgclblkno); 11464 11465 if (st_logical_block_locate(un, pos->lgclblkno, pos->partition) 11466 == 0) { 11467 /* Assume we are there copy rest of position back */ 11468 if (un->un_pos.lgclblkno == pos->lgclblkno) { 11469 COPY_POS(&un->un_pos, pos); 11470 } 11471 return (0); 11472 } 11473 11474 /* 11475 * If logical block locate failed to restore a logical 11476 * position, can't recover. 11477 */ 11478 if (pos->pmode == logical) { 11479 return (-1); 11480 } 11481 } 11482 11483 dev = un->un_dev; 11484 11485 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 11486 "Restoring tape position at fileno=%x, blkno=%x....", 11487 pos->fileno, pos->blkno); 11488 11489 /* 11490 * Rewind ? Oh yeah, Fidelity has got the STK F/W changed 11491 * so as not to rewind tape on RESETS: Gee, Has life ever 11492 * been simple in tape land ? 11493 */ 11494 rval = st_cmd(dev, SCMD_REWIND, 0, SYNC_CMD); 11495 if (rval) { 11496 scsi_log(ST_DEVINFO, st_label, CE_WARN, 11497 "Failed to restore the last file and block position: In" 11498 " this state, Tape will be loaded at BOT during next open"); 11499 un->un_pos.pmode = invalid; 11500 return (rval); 11501 } 11502 11503 /* If the position was as the result of back space file */ 11504 if (pos->blkno > (INF / 2)) { 11505 /* Go one extra file forward */ 11506 pos->fileno++; 11507 /* Figure how many blocks to back into the previous file */ 11508 pos->blkno = -(INF - pos->blkno); 11509 } 11510 11511 /* Go to requested fileno */ 11512 if (pos->fileno) { 11513 rval = st_cmd(dev, SCMD_SPACE, Fmk(pos->fileno), SYNC_CMD); 11514 if (rval) { 11515 scsi_log(ST_DEVINFO, st_label, CE_WARN, 11516 "Failed to restore the last file position: In this " 11517 " state, Tape will be loaded at BOT during next" 11518 " open %d", __LINE__); 11519 un->un_pos.pmode = invalid; 11520 pos->pmode = invalid; 11521 return (rval); 11522 } 11523 } 11524 11525 /* 11526 * If backing into a file we already did an extra file forward. 11527 * Now we have to back over the filemark to get to the end of 11528 * the previous file. The blkno has been ajusted to a negative 11529 * value so we will get to the expected location. 11530 */ 11531 if (pos->blkno) { 11532 rval = st_cmd(dev, SCMD_SPACE, Fmk(-1), SYNC_CMD); 11533 if (rval) { 11534 scsi_log(ST_DEVINFO, st_label, CE_WARN, 11535 "Failed to restore the last file position: In this " 11536 " state, Tape will be loaded at BOT during next" 11537 " open %d", __LINE__); 11538 un->un_pos.pmode = invalid; 11539 pos->pmode = invalid; 11540 return (rval); 11541 } 11542 } 11543 11544 /* 11545 * The position mode, block and fileno should be correct, 11546 * This updates eof and logical position information. 11547 */ 11548 un->un_pos.eof = pos->eof; 11549 un->un_pos.lgclblkno = pos->lgclblkno; 11550 11551 return (0); 11552 } 11553 11554 /* 11555 * check sense key, ASC, ASCQ in order to determine if the tape needs 11556 * to be ejected 11557 */ 11558 11559 static int 11560 st_check_asc_ascq(struct scsi_tape *un) 11561 { 11562 struct scsi_extended_sense *sensep = ST_RQSENSE; 11563 struct tape_failure_code *code; 11564 11565 ST_FUNC(ST_DEVINFO, st_check_asc_ascq); 11566 11567 for (code = st_tape_failure_code; code->key != 0xff; code++) { 11568 if ((code->key == sensep->es_key) && 11569 (code->add_code == sensep->es_add_code) && 11570 (code->qual_code == sensep->es_qual_code)) 11571 return (1); 11572 } 11573 return (0); 11574 } 11575 11576 /* 11577 * st_logpage_supported() sends a Log Sense command with 11578 * page code = 0 = Supported Log Pages Page to the device, 11579 * to see whether the page 'page' is supported. 11580 * Return values are: 11581 * -1 if the Log Sense command fails 11582 * 0 if page is not supported 11583 * 1 if page is supported 11584 */ 11585 11586 static int 11587 st_logpage_supported(dev_t dev, uchar_t page) 11588 { 11589 uchar_t *sp, *sensep; 11590 unsigned length; 11591 struct uscsi_cmd *com; 11592 int rval; 11593 char cdb[CDB_GROUP1] = { 11594 SCMD_LOG_SENSE_G1, 11595 0, 11596 SUPPORTED_LOG_PAGES_PAGE, 11597 0, 11598 0, 11599 0, 11600 0, 11601 0, 11602 (char)LOG_SENSE_LENGTH, 11603 0 11604 }; 11605 11606 GET_SOFT_STATE(dev); 11607 11608 ST_FUNC(ST_DEVINFO, st_logpage_supported); 11609 11610 ASSERT(mutex_owned(ST_MUTEX)); 11611 11612 com = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 11613 sensep = kmem_zalloc(LOG_SENSE_LENGTH, KM_SLEEP); 11614 11615 com->uscsi_cdb = cdb; 11616 com->uscsi_cdblen = CDB_GROUP1; 11617 com->uscsi_bufaddr = (caddr_t)sensep; 11618 com->uscsi_buflen = LOG_SENSE_LENGTH; 11619 com->uscsi_flags = 11620 USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ; 11621 com->uscsi_timeout = un->un_dp->non_motion_timeout; 11622 rval = st_ioctl_cmd(dev, com, FKIOCTL); 11623 if (rval || com->uscsi_status) { 11624 /* uscsi-command failed */ 11625 rval = -1; 11626 } else { 11627 11628 sp = sensep + 3; 11629 11630 for (length = *sp++; length > 0; length--, sp++) { 11631 11632 if (*sp == page) { 11633 rval = 1; 11634 break; 11635 } 11636 } 11637 } 11638 kmem_free(com, sizeof (struct uscsi_cmd)); 11639 kmem_free(sensep, LOG_SENSE_LENGTH); 11640 return (rval); 11641 } 11642 11643 11644 /* 11645 * st_check_clean_bit() gets the status of the tape's cleaning bit. 11646 * 11647 * If the device does support the TapeAlert log page, then the cleaning bit 11648 * information will be read from this page. Otherwise we will see if one of 11649 * ST_CLN_TYPE_1, ST_CLN_TYPE_2 or ST_CLN_TYPE_3 is set in the properties of 11650 * the device, which means, that we can get the cleaning bit information via 11651 * a RequestSense command. 11652 * If both methods of getting cleaning bit information are not supported 11653 * st_check_clean_bit() will return with 0. Otherwise st_check_clean_bit() 11654 * returns with 11655 * - MTF_TAPE_CLN_SUPPORTED if cleaning bit is not set or 11656 * - MTF_TAPE_CLN_SUPPORTED | MTF_TAPE_HEAD_DIRTY if cleaning bit is set. 11657 * If the call to st_ioctl_cmd() to do the Log Sense or the Request Sense 11658 * command fails, or if the amount of Request Sense data is not enough, then 11659 * st_check_clean_bit() returns with -1. 11660 */ 11661 11662 static int 11663 st_check_clean_bit(dev_t dev) 11664 { 11665 int rval = 0; 11666 11667 GET_SOFT_STATE(dev); 11668 11669 ST_FUNC(ST_DEVINFO, st_check_clean_bit); 11670 11671 ASSERT(mutex_owned(ST_MUTEX)); 11672 11673 if (un->un_HeadClean & TAPE_ALERT_NOT_SUPPORTED) { 11674 return (rval); 11675 } 11676 11677 if (un->un_HeadClean == TAPE_ALERT_SUPPORT_UNKNOWN) { 11678 11679 rval = st_logpage_supported(dev, TAPE_SEQUENTIAL_PAGE); 11680 if (rval == 1) { 11681 11682 un->un_HeadClean |= TAPE_SEQUENTIAL_SUPPORTED; 11683 } 11684 11685 rval = st_logpage_supported(dev, TAPE_ALERT_PAGE); 11686 if (rval == 1) { 11687 11688 un->un_HeadClean |= TAPE_ALERT_SUPPORTED; 11689 } 11690 11691 if (un->un_HeadClean == TAPE_ALERT_SUPPORT_UNKNOWN) { 11692 11693 un->un_HeadClean = TAPE_ALERT_NOT_SUPPORTED; 11694 } 11695 } 11696 11697 rval = 0; 11698 11699 if (un->un_HeadClean & TAPE_SEQUENTIAL_SUPPORTED) { 11700 11701 rval = st_check_sequential_clean_bit(dev); 11702 } 11703 11704 if ((rval <= 0) && (un->un_HeadClean & TAPE_ALERT_SUPPORTED)) { 11705 11706 rval = st_check_alert_flags(dev); 11707 } 11708 11709 if ((rval <= 0) && (un->un_dp->options & ST_CLN_MASK)) { 11710 11711 rval = st_check_sense_clean_bit(dev); 11712 } 11713 11714 if (rval < 0) { 11715 return (rval); 11716 } 11717 11718 /* 11719 * If found a supported means to check need to clean. 11720 */ 11721 if (rval & MTF_TAPE_CLN_SUPPORTED) { 11722 11723 /* 11724 * head needs to be cleaned. 11725 */ 11726 if (rval & MTF_TAPE_HEAD_DIRTY) { 11727 11728 /* 11729 * Print log message only first time 11730 * found needing cleaned. 11731 */ 11732 if ((un->un_HeadClean & TAPE_PREVIOUSLY_DIRTY) == 0) { 11733 11734 scsi_log(ST_DEVINFO, st_label, CE_WARN, 11735 "Periodic head cleaning required"); 11736 11737 un->un_HeadClean |= TAPE_PREVIOUSLY_DIRTY; 11738 } 11739 11740 } else { 11741 11742 un->un_HeadClean &= ~TAPE_PREVIOUSLY_DIRTY; 11743 } 11744 } 11745 11746 return (rval); 11747 } 11748 11749 11750 static int 11751 st_check_sequential_clean_bit(dev_t dev) 11752 { 11753 int rval; 11754 int ix; 11755 ushort_t parameter; 11756 struct uscsi_cmd *cmd; 11757 struct log_sequential_page *sp; 11758 struct log_sequential_page_parameter *prm; 11759 char cdb[CDB_GROUP1] = { 11760 SCMD_LOG_SENSE_G1, 11761 0, 11762 TAPE_SEQUENTIAL_PAGE | CURRENT_CUMULATIVE_VALUES, 11763 0, 11764 0, 11765 0, 11766 0, 11767 (char)(sizeof (struct log_sequential_page) >> 8), 11768 (char)(sizeof (struct log_sequential_page)), 11769 0 11770 }; 11771 11772 GET_SOFT_STATE(dev); 11773 11774 ST_FUNC(ST_DEVINFO, st_check_sequential_clean_bit); 11775 11776 cmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 11777 sp = kmem_zalloc(sizeof (struct log_sequential_page), KM_SLEEP); 11778 11779 cmd->uscsi_flags = 11780 USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ; 11781 cmd->uscsi_timeout = un->un_dp->non_motion_timeout; 11782 cmd->uscsi_cdb = cdb; 11783 cmd->uscsi_cdblen = CDB_GROUP1; 11784 cmd->uscsi_bufaddr = (caddr_t)sp; 11785 cmd->uscsi_buflen = sizeof (struct log_sequential_page); 11786 11787 rval = st_ioctl_cmd(dev, cmd, FKIOCTL); 11788 11789 if (rval || cmd->uscsi_status || cmd->uscsi_resid) { 11790 11791 rval = -1; 11792 11793 } else if (sp->log_page.code != TAPE_SEQUENTIAL_PAGE) { 11794 11795 rval = -1; 11796 } 11797 11798 prm = &sp->param[0]; 11799 11800 for (ix = 0; rval == 0 && ix < TAPE_SEQUENTIAL_PAGE_PARA; ix++) { 11801 11802 if (prm->log_param.length == 0) { 11803 break; 11804 } 11805 11806 parameter = (((prm->log_param.pc_hi << 8) & 0xff00) + 11807 (prm->log_param.pc_lo & 0xff)); 11808 11809 if (parameter == SEQUENTIAL_NEED_CLN) { 11810 11811 rval = MTF_TAPE_CLN_SUPPORTED; 11812 if (prm->param_value[prm->log_param.length - 1]) { 11813 11814 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11815 "sequential log says head dirty\n"); 11816 rval |= MTF_TAPE_HEAD_DIRTY; 11817 } 11818 } 11819 prm = (struct log_sequential_page_parameter *) 11820 &prm->param_value[prm->log_param.length]; 11821 } 11822 11823 kmem_free(cmd, sizeof (struct uscsi_cmd)); 11824 kmem_free(sp, sizeof (struct log_sequential_page)); 11825 11826 return (rval); 11827 } 11828 11829 11830 static int 11831 st_check_alert_flags(dev_t dev) 11832 { 11833 struct st_tape_alert *ta; 11834 struct uscsi_cmd *com; 11835 unsigned ix, length; 11836 int rval; 11837 tape_alert_flags flag; 11838 char cdb[CDB_GROUP1] = { 11839 SCMD_LOG_SENSE_G1, 11840 0, 11841 TAPE_ALERT_PAGE | CURRENT_THRESHOLD_VALUES, 11842 0, 11843 0, 11844 0, 11845 0, 11846 (char)(sizeof (struct st_tape_alert) >> 8), 11847 (char)(sizeof (struct st_tape_alert)), 11848 0 11849 }; 11850 11851 GET_SOFT_STATE(dev); 11852 11853 ST_FUNC(ST_DEVINFO, st_check_alert_clean_bit); 11854 11855 com = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 11856 ta = kmem_zalloc(sizeof (struct st_tape_alert), KM_SLEEP); 11857 11858 com->uscsi_cdb = cdb; 11859 com->uscsi_cdblen = CDB_GROUP1; 11860 com->uscsi_bufaddr = (caddr_t)ta; 11861 com->uscsi_buflen = sizeof (struct st_tape_alert); 11862 com->uscsi_flags = 11863 USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ; 11864 com->uscsi_timeout = un->un_dp->non_motion_timeout; 11865 11866 rval = st_ioctl_cmd(dev, com, FKIOCTL); 11867 11868 if (rval || com->uscsi_status || com->uscsi_resid) { 11869 11870 rval = -1; /* uscsi-command failed */ 11871 11872 } else if (ta->log_page.code != TAPE_ALERT_PAGE) { 11873 11874 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11875 "Not Alert Log Page returned 0x%X\n", ta->log_page.code); 11876 rval = -1; 11877 } 11878 11879 length = (ta->log_page.length_hi << 8) + ta->log_page.length_lo; 11880 11881 11882 if (length != TAPE_ALERT_PARAMETER_LENGTH) { 11883 11884 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11885 "TapeAlert length %d\n", length); 11886 } 11887 11888 11889 for (ix = 0; ix < TAPE_ALERT_MAX_PARA; ix++) { 11890 11891 /* 11892 * if rval is bad before the first pass don't bother 11893 */ 11894 if (ix == 0 && rval != 0) { 11895 11896 break; 11897 } 11898 11899 flag = ((ta->param[ix].log_param.pc_hi << 8) + 11900 ta->param[ix].log_param.pc_lo); 11901 11902 if ((ta->param[ix].param_value & 1) == 0) { 11903 continue; 11904 } 11905 /* 11906 * check to see if current parameter is of interest. 11907 * CLEAN_FOR_ERRORS is vendor specific to 9840 9940 stk's. 11908 */ 11909 if ((flag == TAF_CLEAN_NOW) || 11910 (flag == TAF_CLEAN_PERIODIC) || 11911 ((flag == CLEAN_FOR_ERRORS) && 11912 (un->un_dp->type == ST_TYPE_STK9840))) { 11913 11914 rval = MTF_TAPE_CLN_SUPPORTED; 11915 11916 11917 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11918 "alert_page drive needs clean %d\n", flag); 11919 un->un_HeadClean |= TAPE_ALERT_STILL_DIRTY; 11920 rval |= MTF_TAPE_HEAD_DIRTY; 11921 11922 } else if (flag == TAF_CLEANING_MEDIA) { 11923 11924 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11925 "alert_page drive was cleaned\n"); 11926 un->un_HeadClean &= ~TAPE_ALERT_STILL_DIRTY; 11927 } 11928 11929 } 11930 11931 /* 11932 * Report it as dirty till we see it cleaned 11933 */ 11934 if (un->un_HeadClean & TAPE_ALERT_STILL_DIRTY) { 11935 11936 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 11937 "alert_page still dirty\n"); 11938 rval |= MTF_TAPE_HEAD_DIRTY; 11939 } 11940 11941 kmem_free(com, sizeof (struct uscsi_cmd)); 11942 kmem_free(ta, sizeof (struct st_tape_alert)); 11943 11944 return (rval); 11945 } 11946 11947 11948 static int 11949 st_check_sense_clean_bit(dev_t dev) 11950 { 11951 uchar_t *sensep; 11952 char cdb[CDB_GROUP0]; 11953 struct uscsi_cmd *com; 11954 ushort_t byte_pos; 11955 uchar_t bit_mask; 11956 unsigned length; 11957 int index; 11958 int rval; 11959 11960 GET_SOFT_STATE(dev); 11961 11962 ST_FUNC(ST_DEVINFO, st_check_sense_clean_bit); 11963 11964 /* 11965 * Since this tape does not support Tape Alert, 11966 * we now try to get the cleanbit status via 11967 * Request Sense. 11968 */ 11969 11970 if ((un->un_dp->options & ST_CLN_MASK) == ST_CLN_TYPE_1) { 11971 11972 index = 0; 11973 11974 } else if ((un->un_dp->options & ST_CLN_MASK) == ST_CLN_TYPE_2) { 11975 11976 index = 1; 11977 11978 } else if ((un->un_dp->options & ST_CLN_MASK) == ST_CLN_TYPE_3) { 11979 11980 index = 2; 11981 11982 } else { 11983 11984 return (-1); 11985 } 11986 11987 byte_pos = st_cln_bit_position[index].cln_bit_byte; 11988 bit_mask = st_cln_bit_position[index].cln_bit_mask; 11989 length = byte_pos + 1; 11990 11991 com = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 11992 sensep = kmem_zalloc(length, KM_SLEEP); 11993 11994 cdb[0] = SCMD_REQUEST_SENSE; 11995 cdb[1] = 0; 11996 cdb[2] = 0; 11997 cdb[3] = 0; 11998 cdb[4] = (char)length; 11999 cdb[5] = 0; 12000 12001 com->uscsi_cdb = cdb; 12002 com->uscsi_cdblen = CDB_GROUP0; 12003 com->uscsi_bufaddr = (caddr_t)sensep; 12004 com->uscsi_buflen = length; 12005 com->uscsi_flags = 12006 USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ; 12007 com->uscsi_timeout = un->un_dp->non_motion_timeout; 12008 12009 rval = st_ioctl_cmd(dev, com, FKIOCTL); 12010 12011 if (rval || com->uscsi_status || com->uscsi_resid) { 12012 12013 rval = -1; 12014 12015 } else { 12016 12017 rval = MTF_TAPE_CLN_SUPPORTED; 12018 if ((sensep[byte_pos] & bit_mask) == bit_mask) { 12019 12020 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 12021 "sense data says head dirty\n"); 12022 rval |= MTF_TAPE_HEAD_DIRTY; 12023 } 12024 } 12025 12026 kmem_free(com, sizeof (struct uscsi_cmd)); 12027 kmem_free(sensep, length); 12028 return (rval); 12029 } 12030 12031 /* 12032 * st_clear_unit_attention 12033 * 12034 * run test unit ready's to clear out outstanding 12035 * unit attentions. 12036 * returns zero for SUCCESS or the errno from st_cmd call 12037 */ 12038 static int 12039 st_clear_unit_attentions(dev_t dev_instance, int max_trys) 12040 { 12041 int i = 0; 12042 int rval; 12043 12044 #ifdef DEBUG 12045 GET_SOFT_STATE(dev_instance); 12046 ST_FUNC(ST_DEVINFO, st_clear_unit_attentions); 12047 #endif 12048 12049 do { 12050 rval = st_cmd(dev_instance, SCMD_TEST_UNIT_READY, 0, SYNC_CMD); 12051 } while ((rval != 0) && (rval != ENXIO) && (++i < max_trys)); 12052 return (rval); 12053 } 12054 12055 static void 12056 st_calculate_timeouts(struct scsi_tape *un) 12057 { 12058 ST_FUNC(ST_DEVINFO, st_calculate_timeouts); 12059 12060 if (un->un_dp->non_motion_timeout == 0) { 12061 if (un->un_dp->options & ST_LONG_TIMEOUTS) { 12062 un->un_dp->non_motion_timeout = 12063 st_io_time * st_long_timeout_x; 12064 } else { 12065 un->un_dp->non_motion_timeout = (ushort_t)st_io_time; 12066 } 12067 } 12068 12069 if (un->un_dp->io_timeout == 0) { 12070 if (un->un_dp->options & ST_LONG_TIMEOUTS) { 12071 un->un_dp->io_timeout = st_io_time * st_long_timeout_x; 12072 } else { 12073 un->un_dp->io_timeout = (ushort_t)st_io_time; 12074 } 12075 } 12076 12077 if (un->un_dp->rewind_timeout == 0) { 12078 if (un->un_dp->options & ST_LONG_TIMEOUTS) { 12079 un->un_dp->rewind_timeout = 12080 st_space_time * st_long_timeout_x; 12081 } else { 12082 un->un_dp->rewind_timeout = (ushort_t)st_space_time; 12083 } 12084 } 12085 12086 if (un->un_dp->space_timeout == 0) { 12087 if (un->un_dp->options & ST_LONG_TIMEOUTS) { 12088 un->un_dp->space_timeout = 12089 st_space_time * st_long_timeout_x; 12090 } else { 12091 un->un_dp->space_timeout = (ushort_t)st_space_time; 12092 } 12093 } 12094 12095 if (un->un_dp->load_timeout == 0) { 12096 if (un->un_dp->options & ST_LONG_TIMEOUTS) { 12097 un->un_dp->load_timeout = 12098 st_space_time * st_long_timeout_x; 12099 } else { 12100 un->un_dp->load_timeout = (ushort_t)st_space_time; 12101 } 12102 } 12103 12104 if (un->un_dp->unload_timeout == 0) { 12105 if (un->un_dp->options & ST_LONG_TIMEOUTS) { 12106 un->un_dp->unload_timeout = 12107 st_space_time * st_long_timeout_x; 12108 } else { 12109 un->un_dp->unload_timeout = (ushort_t)st_space_time; 12110 } 12111 } 12112 12113 if (un->un_dp->erase_timeout == 0) { 12114 if (un->un_dp->options & ST_LONG_ERASE) { 12115 un->un_dp->erase_timeout = 12116 st_space_time * st_long_space_time_x; 12117 } else { 12118 un->un_dp->erase_timeout = (ushort_t)st_space_time; 12119 } 12120 } 12121 } 12122 12123 12124 static writablity 12125 st_is_not_wormable(struct scsi_tape *un) 12126 { 12127 ST_FUNC(ST_DEVINFO, st_is_not_wormable); 12128 return (RDWR); 12129 } 12130 12131 static writablity 12132 st_is_hp_dat_tape_worm(struct scsi_tape *un) 12133 { 12134 writablity wrt; 12135 12136 ST_FUNC(ST_DEVINFO, st_is_hp_dat_tape_worm); 12137 12138 /* Mode sense should be current */ 12139 if (un->un_mspl->media_type == 1) { 12140 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 12141 "Drive has WORM media loaded\n"); 12142 wrt = WORM; 12143 } else { 12144 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 12145 "Drive has non WORM media loaded\n"); 12146 wrt = RDWR; 12147 } 12148 return (wrt); 12149 } 12150 12151 #define HP_DAT_INQUIRY 0x4A 12152 static writablity 12153 st_is_hp_dat_worm(struct scsi_tape *un) 12154 { 12155 char *buf; 12156 int result; 12157 writablity wrt; 12158 12159 ST_FUNC(ST_DEVINFO, st_is_hp_dat_worm); 12160 12161 buf = kmem_zalloc(HP_DAT_INQUIRY, KM_SLEEP); 12162 12163 result = st_get_special_inquiry(un, HP_DAT_INQUIRY, buf, 0); 12164 12165 if (result != 0) { 12166 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 12167 "Read Standard Inquiry for WORM support failed"); 12168 wrt = FAILED; 12169 } else if ((buf[40] & 1) == 0) { 12170 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 12171 "Drive is NOT WORMable\n"); 12172 /* This drive doesn't support it so don't check again */ 12173 un->un_dp->options &= ~ST_WORMABLE; 12174 wrt = RDWR; 12175 un->un_wormable = st_is_not_wormable; 12176 } else { 12177 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 12178 "Drive supports WORM version %d\n", buf[40] >> 1); 12179 un->un_wormable = st_is_hp_dat_tape_worm; 12180 wrt = un->un_wormable(un); 12181 } 12182 12183 kmem_free(buf, HP_DAT_INQUIRY); 12184 12185 /* 12186 * If drive doesn't support it no point in checking further. 12187 */ 12188 return (wrt); 12189 } 12190 12191 static writablity 12192 st_is_hp_lto_tape_worm(struct scsi_tape *un) 12193 { 12194 writablity wrt; 12195 12196 ST_FUNC(ST_DEVINFO, st_is_hp_lto_tape_worm); 12197 12198 /* Mode sense should be current */ 12199 switch (un->un_mspl->media_type) { 12200 case 0x00: 12201 switch (un->un_mspl->density) { 12202 case 0x40: 12203 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 12204 "Drive has standard Gen I media loaded\n"); 12205 break; 12206 case 0x42: 12207 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 12208 "Drive has standard Gen II media loaded\n"); 12209 break; 12210 case 0x44: 12211 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 12212 "Drive has standard Gen III media loaded\n"); 12213 break; 12214 case 0x46: 12215 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 12216 "Drive has standard Gen IV media loaded\n"); 12217 break; 12218 default: 12219 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 12220 "Drive has standard unknown 0x%X media loaded\n", 12221 un->un_mspl->density); 12222 } 12223 wrt = RDWR; 12224 break; 12225 case 0x01: 12226 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 12227 "Drive has WORM medium loaded\n"); 12228 wrt = WORM; 12229 break; 12230 case 0x80: 12231 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 12232 "Drive has CD-ROM emulation medium loaded\n"); 12233 wrt = WORM; 12234 break; 12235 default: 12236 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 12237 "Drive has an unexpected medium type 0x%X loaded\n", 12238 un->un_mspl->media_type); 12239 wrt = RDWR; 12240 } 12241 12242 return (wrt); 12243 } 12244 12245 #define LTO_REQ_INQUIRY 44 12246 static writablity 12247 st_is_hp_lto_worm(struct scsi_tape *un) 12248 { 12249 char *buf; 12250 int result; 12251 writablity wrt; 12252 12253 ST_FUNC(ST_DEVINFO, st_is_hp_lto_worm); 12254 12255 buf = kmem_zalloc(LTO_REQ_INQUIRY, KM_SLEEP); 12256 12257 result = st_get_special_inquiry(un, LTO_REQ_INQUIRY, buf, 0); 12258 12259 if (result != 0) { 12260 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 12261 "Read Standard Inquiry for WORM support failed"); 12262 wrt = FAILED; 12263 } else if ((buf[40] & 1) == 0) { 12264 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 12265 "Drive is NOT WORMable\n"); 12266 /* This drive doesn't support it so don't check again */ 12267 un->un_dp->options &= ~ST_WORMABLE; 12268 wrt = RDWR; 12269 un->un_wormable = st_is_not_wormable; 12270 } else { 12271 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 12272 "Drive supports WORM version %d\n", buf[40] >> 1); 12273 un->un_wormable = st_is_hp_lto_tape_worm; 12274 wrt = un->un_wormable(un); 12275 } 12276 12277 kmem_free(buf, LTO_REQ_INQUIRY); 12278 12279 /* 12280 * If drive doesn't support it no point in checking further. 12281 */ 12282 return (wrt); 12283 } 12284 12285 static writablity 12286 st_is_t10_worm_device(struct scsi_tape *un) 12287 { 12288 writablity wrt; 12289 12290 ST_FUNC(ST_DEVINFO, st_is_t10_worm_device); 12291 12292 if (un->un_mspl->media_type == 0x3c) { 12293 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 12294 "Drive has WORM media loaded\n"); 12295 wrt = WORM; 12296 } else { 12297 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 12298 "Drive has non WORM media loaded\n"); 12299 wrt = RDWR; 12300 } 12301 return (wrt); 12302 } 12303 12304 #define SEQ_CAP_PAGE (char)0xb0 12305 static writablity 12306 st_is_t10_worm(struct scsi_tape *un) 12307 { 12308 char *buf; 12309 int result; 12310 writablity wrt; 12311 12312 ST_FUNC(ST_DEVINFO, st_is_t10_worm); 12313 12314 buf = kmem_zalloc(6, KM_SLEEP); 12315 12316 result = st_get_special_inquiry(un, 6, buf, SEQ_CAP_PAGE); 12317 12318 if (result != 0) { 12319 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 12320 "Read Vitial Inquiry for Sequental Capability" 12321 " WORM support failed %x", result); 12322 wrt = FAILED; 12323 } else if ((buf[4] & 1) == 0) { 12324 ASSERT(buf[1] == SEQ_CAP_PAGE); 12325 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 12326 "Drive is NOT WORMable\n"); 12327 /* This drive doesn't support it so don't check again */ 12328 un->un_dp->options &= ~ST_WORMABLE; 12329 wrt = RDWR; 12330 un->un_wormable = st_is_not_wormable; 12331 } else { 12332 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 12333 "Drive supports WORM\n"); 12334 un->un_wormable = st_is_t10_worm_device; 12335 wrt = un->un_wormable(un); 12336 } 12337 12338 kmem_free(buf, 6); 12339 12340 return (wrt); 12341 } 12342 12343 12344 #define STK_REQ_SENSE 26 12345 12346 static writablity 12347 st_is_stk_worm(struct scsi_tape *un) 12348 { 12349 char cdb[CDB_GROUP0] = {SCMD_REQUEST_SENSE, 0, 0, 0, STK_REQ_SENSE, 0}; 12350 struct scsi_extended_sense *sense; 12351 struct uscsi_cmd *cmd; 12352 char *buf; 12353 int result; 12354 writablity wrt; 12355 12356 ST_FUNC(ST_DEVINFO, st_is_stk_worm); 12357 12358 cmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 12359 buf = kmem_alloc(STK_REQ_SENSE, KM_SLEEP); 12360 sense = (struct scsi_extended_sense *)buf; 12361 12362 cmd->uscsi_flags = USCSI_READ; 12363 cmd->uscsi_timeout = un->un_dp->non_motion_timeout; 12364 cmd->uscsi_cdb = &cdb[0]; 12365 cmd->uscsi_bufaddr = buf; 12366 cmd->uscsi_buflen = STK_REQ_SENSE; 12367 cmd->uscsi_cdblen = CDB_GROUP0; 12368 cmd->uscsi_rqlen = 0; 12369 cmd->uscsi_rqbuf = NULL; 12370 12371 result = st_ioctl_cmd(un->un_dev, cmd, FKIOCTL); 12372 12373 if (result != 0 || cmd->uscsi_status != 0) { 12374 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 12375 "Request Sense for WORM failed"); 12376 wrt = RDWR; 12377 } else if (sense->es_add_len + 8 < 24) { 12378 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 12379 "Drive didn't send enough sense data for WORM byte %d\n", 12380 sense->es_add_len + 8); 12381 wrt = RDWR; 12382 un->un_wormable = st_is_not_wormable; 12383 } else if ((buf[24]) & 0x02) { 12384 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 12385 "Drive has WORM tape loaded\n"); 12386 wrt = WORM; 12387 un->un_wormable = st_is_stk_worm; 12388 } else { 12389 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 12390 "Drive has normal tape loaded\n"); 12391 wrt = RDWR; 12392 un->un_wormable = st_is_stk_worm; 12393 } 12394 12395 kmem_free(buf, STK_REQ_SENSE); 12396 kmem_free(cmd, sizeof (struct uscsi_cmd)); 12397 return (wrt); 12398 } 12399 12400 #define DLT_INQ_SZ 44 12401 12402 static writablity 12403 st_is_dlt_tape_worm(struct scsi_tape *un) 12404 { 12405 caddr_t buf; 12406 int result; 12407 writablity wrt; 12408 12409 ST_FUNC(ST_DEVINFO, st_is_dlt_tape_worm); 12410 12411 buf = kmem_alloc(DLT_INQ_SZ, KM_SLEEP); 12412 12413 /* Read Attribute Media Type */ 12414 12415 result = st_read_attributes(un, 0x0408, buf, 10); 12416 12417 /* 12418 * If this quantum drive is attached via an HBA that cannot 12419 * support thr read attributes command return error in the 12420 * hope that someday they will support the t10 method. 12421 */ 12422 if (result == EINVAL && un->un_max_cdb_sz < CDB_GROUP4) { 12423 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 12424 "Read Attribute Command for WORM Media detection is not " 12425 "supported on the HBA that this drive is attached to."); 12426 wrt = RDWR; 12427 un->un_wormable = st_is_not_wormable; 12428 goto out; 12429 } 12430 12431 if (result != 0) { 12432 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 12433 "Read Attribute Command for WORM Media returned 0x%x", 12434 result); 12435 wrt = RDWR; 12436 un->un_dp->options &= ~ST_WORMABLE; 12437 goto out; 12438 } 12439 12440 if ((uchar_t)buf[9] == 0x80) { 12441 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 12442 "Drive media is WORM\n"); 12443 wrt = WORM; 12444 } else { 12445 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 12446 "Drive media is not WORM Media 0x%x\n", (uchar_t)buf[9]); 12447 wrt = RDWR; 12448 } 12449 12450 out: 12451 kmem_free(buf, DLT_INQ_SZ); 12452 return (wrt); 12453 } 12454 12455 static writablity 12456 st_is_dlt_worm(struct scsi_tape *un) 12457 { 12458 caddr_t buf; 12459 int result; 12460 writablity wrt; 12461 12462 ST_FUNC(ST_DEVINFO, st_is_dlt_worm); 12463 12464 buf = kmem_alloc(DLT_INQ_SZ, KM_SLEEP); 12465 12466 result = st_get_special_inquiry(un, DLT_INQ_SZ, buf, 0xC0); 12467 12468 if (result != 0) { 12469 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 12470 "Read Vendor Specific Inquiry for WORM support failed"); 12471 wrt = RDWR; 12472 goto out; 12473 } 12474 12475 if ((buf[2] & 1) == 0) { 12476 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 12477 "Drive is not WORMable\n"); 12478 wrt = RDWR; 12479 un->un_dp->options &= ~ST_WORMABLE; 12480 un->un_wormable = st_is_not_wormable; 12481 goto out; 12482 } else { 12483 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 12484 "Drive is WORMable\n"); 12485 un->un_wormable = st_is_dlt_tape_worm; 12486 wrt = un->un_wormable(un); 12487 } 12488 out: 12489 kmem_free(buf, DLT_INQ_SZ); 12490 12491 return (wrt); 12492 } 12493 12494 typedef struct { 12495 struct modeheader_seq header; 12496 #if defined(_BIT_FIELDS_LTOH) /* X86 */ 12497 uchar_t pagecode :6, 12498 :2; 12499 uchar_t page_len; 12500 uchar_t syslogalive :2, 12501 device :1, 12502 abs :1, 12503 ulpbot :1, 12504 prth :1, 12505 ponej :1, 12506 ait :1; 12507 uchar_t span; 12508 12509 uchar_t :6, 12510 worm :1, 12511 mic :1; 12512 uchar_t worm_cap :1, 12513 :7; 12514 uint32_t :32; 12515 #else /* SPARC */ 12516 uchar_t :2, 12517 pagecode :6; 12518 uchar_t page_len; 12519 uchar_t ait :1, 12520 device :1, 12521 abs :1, 12522 ulpbot :1, 12523 prth :1, 12524 ponej :1, 12525 syslogalive :2; 12526 uchar_t span; 12527 uchar_t mic :1, 12528 worm :1, 12529 :6; 12530 uchar_t :7, 12531 worm_cap :1; 12532 uint32_t :32; 12533 #endif 12534 }ait_dev_con; 12535 12536 #define AIT_DEV_PAGE 0x31 12537 static writablity 12538 st_is_sony_worm(struct scsi_tape *un) 12539 { 12540 int result; 12541 writablity wrt; 12542 ait_dev_con *ait_conf; 12543 12544 ST_FUNC(ST_DEVINFO, st_is_sony_worm); 12545 12546 ait_conf = kmem_zalloc(sizeof (ait_dev_con), KM_SLEEP); 12547 12548 result = st_gen_mode_sense(un, AIT_DEV_PAGE, 12549 (struct seq_mode *)ait_conf, sizeof (ait_dev_con)); 12550 12551 if (result == 0) { 12552 12553 if (ait_conf->pagecode != AIT_DEV_PAGE) { 12554 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 12555 "returned page 0x%x not 0x%x AIT_DEV_PAGE\n", 12556 ait_conf->pagecode, AIT_DEV_PAGE); 12557 wrt = RDWR; 12558 un->un_wormable = st_is_not_wormable; 12559 12560 } else if (ait_conf->worm_cap) { 12561 12562 un->un_wormable = st_is_sony_worm; 12563 12564 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 12565 "Drives is WORMable\n"); 12566 if (ait_conf->worm) { 12567 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 12568 "Media is WORM\n"); 12569 wrt = WORM; 12570 } else { 12571 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 12572 "Media is not WORM\n"); 12573 wrt = RDWR; 12574 } 12575 12576 } else { 12577 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 12578 "Drives not is WORMable\n"); 12579 wrt = RDWR; 12580 /* No further checking required */ 12581 un->un_dp->options &= ~ST_WORMABLE; 12582 } 12583 12584 } else { 12585 12586 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 12587 "AIT device config mode sense page read command failed" 12588 " result = %d ", result); 12589 wrt = FAILED; 12590 un->un_wormable = st_is_not_wormable; 12591 } 12592 12593 kmem_free(ait_conf, sizeof (ait_dev_con)); 12594 return (wrt); 12595 } 12596 12597 static writablity 12598 st_is_drive_worm(struct scsi_tape *un) 12599 { 12600 writablity wrt; 12601 12602 ST_FUNC(ST_DEVINFO, st_is_sony_worm); 12603 12604 switch (un->un_dp->type) { 12605 case MT_ISDLT: 12606 wrt = st_is_dlt_worm(un); 12607 break; 12608 12609 case MT_ISSTK9840: 12610 wrt = st_is_stk_worm(un); 12611 break; 12612 12613 case MT_IS8MM: 12614 case MT_ISAIT: 12615 wrt = st_is_sony_worm(un); 12616 break; 12617 12618 case MT_LTO: 12619 if (strncmp("HP ", un->un_dp->vid, 3) == 0) { 12620 wrt = st_is_hp_lto_worm(un); 12621 } else { 12622 wrt = st_is_t10_worm(un); 12623 } 12624 break; 12625 12626 case MT_ISDAT: 12627 if (strncmp("HP ", un->un_dp->vid, 3) == 0) { 12628 wrt = st_is_hp_dat_worm(un); 12629 } else { 12630 wrt = st_is_t10_worm(un); 12631 } 12632 break; 12633 12634 default: 12635 wrt = FAILED; 12636 break; 12637 } 12638 12639 /* 12640 * If any of the above failed try the t10 standard method. 12641 */ 12642 if (wrt == FAILED) { 12643 wrt = st_is_t10_worm(un); 12644 } 12645 12646 /* 12647 * Unknown method for detecting WORM media. 12648 */ 12649 if (wrt == FAILED) { 12650 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 12651 "Unknown method for WORM media detection\n"); 12652 wrt = RDWR; 12653 un->un_dp->options &= ~ST_WORMABLE; 12654 } 12655 12656 return (wrt); 12657 } 12658 12659 static int 12660 st_read_attributes(struct scsi_tape *un, uint16_t attribute, caddr_t buf, 12661 size_t size) 12662 { 12663 char cdb[CDB_GROUP4]; 12664 int result; 12665 struct uscsi_cmd *cmd; 12666 12667 ST_FUNC(ST_DEVINFO, st_read_attributes); 12668 12669 cmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 12670 12671 cdb[0] = (char)SCMD_READ_ATTRIBUTE; 12672 cdb[1] = 0; 12673 cdb[2] = 0; 12674 cdb[3] = 0; 12675 cdb[4] = 0; 12676 cdb[5] = 0; 12677 cdb[6] = 0; 12678 cdb[7] = 0; 12679 cdb[8] = (char)(attribute >> 8); 12680 cdb[9] = (char)(attribute); 12681 cdb[10] = (char)(size >> 24); 12682 cdb[11] = (char)(size >> 16); 12683 cdb[12] = (char)(size >> 8); 12684 cdb[13] = (char)(size); 12685 cdb[14] = 0; 12686 cdb[15] = 0; 12687 12688 12689 cmd->uscsi_flags = USCSI_READ | USCSI_DIAGNOSE; 12690 cmd->uscsi_timeout = un->un_dp->non_motion_timeout; 12691 cmd->uscsi_cdb = &cdb[0]; 12692 cmd->uscsi_bufaddr = (caddr_t)buf; 12693 cmd->uscsi_buflen = size; 12694 cmd->uscsi_cdblen = sizeof (cdb); 12695 12696 result = st_ioctl_cmd(un->un_dev, cmd, FKIOCTL); 12697 12698 if (result != 0 || cmd->uscsi_status != 0) { 12699 ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, 12700 "st_read_attribute failed: result %d status %d\n", 12701 result, cmd->uscsi_status); 12702 if (result == 0) { 12703 result = EIO; 12704 } 12705 goto exit; 12706 } 12707 12708 /* 12709 * The attribute retured should match the attribute requested. 12710 */ 12711 if (buf[4] != cdb[8] || buf[5] != cdb[9]) { 12712 st_clean_print(ST_DEVINFO, st_label, SCSI_DEBUG, 12713 "bad? data", buf, size); 12714 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 12715 "st_read_attribute got wrong data back expected 0x%x" 12716 " got 0x%x\n", attribute, buf[6] << 8 | buf[7]); 12717 result = EIO; 12718 } 12719 exit: 12720 kmem_free(cmd, sizeof (struct uscsi_cmd)); 12721 12722 return (result); 12723 } 12724 12725 static int 12726 st_get_special_inquiry(struct scsi_tape *un, uchar_t size, caddr_t dest, 12727 uchar_t page) 12728 { 12729 char cdb[CDB_GROUP0]; 12730 struct scsi_extended_sense *sense; 12731 struct uscsi_cmd *cmd; 12732 int result; 12733 12734 ST_FUNC(ST_DEVINFO, st_get_special_inquiry); 12735 12736 cdb[0] = SCMD_INQUIRY; 12737 cdb[1] = page ? 1 : 0; 12738 cdb[2] = page; 12739 cdb[3] = 0; 12740 cdb[4] = size; 12741 cdb[5] = 0; 12742 12743 cmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 12744 sense = kmem_alloc(sizeof (struct scsi_extended_sense), KM_SLEEP); 12745 12746 cmd->uscsi_flags = USCSI_READ | USCSI_RQENABLE; 12747 cmd->uscsi_timeout = un->un_dp->non_motion_timeout; 12748 cmd->uscsi_cdb = &cdb[0]; 12749 cmd->uscsi_bufaddr = dest; 12750 cmd->uscsi_buflen = size; 12751 cmd->uscsi_cdblen = CDB_GROUP0; 12752 cmd->uscsi_rqlen = sizeof (struct scsi_extended_sense); 12753 cmd->uscsi_rqbuf = (caddr_t)sense; 12754 12755 result = st_ioctl_cmd(un->un_dev, cmd, FKIOCTL); 12756 12757 if (result != 0 || cmd->uscsi_status != 0) { 12758 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 12759 "st_get_special_inquiry() failed for page %x", page); 12760 if (result == 0) { 12761 result = EIO; 12762 } 12763 } 12764 12765 kmem_free(sense, sizeof (struct scsi_extended_sense)); 12766 kmem_free(cmd, sizeof (struct uscsi_cmd)); 12767 12768 return (result); 12769 } 12770 12771 12772 static int 12773 st_update_block_pos(struct scsi_tape *un) 12774 { 12775 int rval = ENOTTY; 12776 12777 ST_FUNC(ST_DEVINFO, st_update_block_pos); 12778 12779 while (un->un_read_pos_type != NO_POS) { 12780 rval = st_cmd(un->un_dev, SCMD_READ_POSITION, 32, SYNC_CMD); 12781 12782 if (rval == 0) { 12783 rval = st_interpret_read_pos(un, un->un_read_pos_type, 12784 32, (caddr_t)un->un_read_pos_data); 12785 break; 12786 } else if (un->un_status == KEY_UNIT_ATTENTION) { 12787 continue; 12788 } else if (un->un_status != KEY_ILLEGAL_REQUEST) { 12789 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 12790 "st_update_block_pos() read position cmd %x" 12791 " returned %x un_status = %d", 12792 un->un_read_pos_type, rval, un->un_status); 12793 break; 12794 } else { 12795 ST_DEBUG4(ST_DEVINFO, st_label, CE_NOTE, 12796 "st_update_block_pos() read position cmd %x" 12797 " returned %x", un->un_read_pos_type, rval); 12798 } 12799 12800 switch (un->un_read_pos_type) { 12801 case SHORT_POS: 12802 un->un_read_pos_type = NO_POS; 12803 break; 12804 12805 case LONG_POS: 12806 un->un_read_pos_type = EXT_POS; 12807 break; 12808 12809 case EXT_POS: 12810 un->un_read_pos_type = SHORT_POS; 12811 break; 12812 12813 default: 12814 ST_DEBUG(ST_DEVINFO, st_label, CE_PANIC, 12815 "Unexpected read position type 0x%x", 12816 un->un_read_pos_type); 12817 } 12818 } 12819 12820 return (rval); 12821 } 12822 12823 static int 12824 st_get_read_pos(struct scsi_tape *un, buf_t *bp) 12825 { 12826 int result; 12827 size_t d_sz; 12828 caddr_t pos_info; 12829 struct uscsi_cmd *cmd = (struct uscsi_cmd *)bp->b_back; 12830 12831 ST_FUNC(ST_DEVINFO, st_get_read_pos); 12832 12833 if (cmd->uscsi_bufaddr == NULL || cmd->uscsi_buflen <= 0) { 12834 return (0); 12835 } 12836 12837 if (bp_mapin_common(bp, VM_NOSLEEP) == NULL) { 12838 12839 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 12840 "bp_mapin_common() failed"); 12841 12842 return (EIO); 12843 } 12844 12845 pos_info = bp->b_un.b_addr; 12846 d_sz = bp->b_bcount - bp->b_resid; 12847 12848 #ifdef DEBUG 12849 if ((st_debug & 0xf) > 2) { 12850 st_clean_print(ST_DEVINFO, st_label, SCSI_DEBUG, 12851 "st_get_read_pos() position info", 12852 pos_info, bp->b_bcount); 12853 } 12854 #endif 12855 12856 result = st_interpret_read_pos(un, cmd->uscsi_cdb[1], d_sz, pos_info); 12857 12858 bp_mapout(bp); 12859 12860 return (result); 12861 } 12862 12863 #if defined(_BIG_ENDIAN) 12864 12865 #define FIX_ENDIAN32(x) 12866 #define FIX_ENDIAN64(x) 12867 12868 #elif defined(_LITTLE_ENDIAN) 12869 12870 static void 12871 st_swap32(uint32_t *val) 12872 { 12873 uint32_t tmp; 12874 12875 tmp = (*val >> 24) & 0xff; 12876 tmp |= (*val >> 8) & 0xff00; 12877 tmp |= (*val << 8) & 0xff0000; 12878 tmp |= (*val << 24) & 0xff000000; 12879 12880 *val = tmp; 12881 } 12882 12883 static void 12884 st_swap64(uint64_t *val) 12885 { 12886 uint32_t low; 12887 uint32_t high; 12888 12889 low = (uint32_t)(*val); 12890 high = (uint32_t)(*val >> 32); 12891 12892 st_swap32(&low); 12893 st_swap32(&high); 12894 12895 *val = high; 12896 *val |= ((uint64_t)low << 32); 12897 } 12898 12899 #define FIX_ENDIAN32(x) st_swap32(x) 12900 #define FIX_ENDIAN64(x) st_swap64(x) 12901 #endif 12902 12903 static int 12904 st_interpret_read_pos(struct scsi_tape *un, read_p_types type, 12905 size_t data_sz, caddr_t responce) 12906 { 12907 int rval = 0; 12908 12909 ST_FUNC(ST_DEVINFO, st_interpret_read_pos); 12910 12911 /* 12912 * Look at byte 1 of cdb to see what kind of read position 12913 * was requested. 12914 */ 12915 switch (type) { 12916 12917 case SHORT_POS: /* Short data format */ 12918 { 12919 tape_position_t *pos_info = (tape_position_t *)responce; 12920 uint32_t value; 12921 12922 /* If reserved fields are non zero don't use the data */ 12923 if (pos_info->reserved0 || pos_info->reserved1 || 12924 pos_info->reserved2[0] || pos_info->reserved2[1] || 12925 pos_info->reserved3) { 12926 rval = EIO; 12927 break; 12928 } 12929 /* 12930 * Position is to large to use this type of read position. 12931 */ 12932 if (pos_info->posi_err == 1) { 12933 rval = ERANGE; 12934 break; 12935 } 12936 12937 if (pos_info->blk_posi_unkwn == 0) { 12938 12939 if (un->un_pos.partition != 12940 pos_info->partition_number) { 12941 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 12942 "SHORT_POS current partition %d read %d\n", 12943 un->un_pos.partition, 12944 pos_info->partition_number); 12945 } 12946 un->un_pos.partition = pos_info->partition_number; 12947 value = pos_info->host_block; 12948 FIX_ENDIAN32(&value); 12949 12950 if (un->un_pos.lgclblkno != value) { 12951 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 12952 "SHORT_POS current logical 0x%"PRIx64" read" 12953 " 0x%x\n", un->un_pos.lgclblkno, value); 12954 } 12955 12956 un->un_pos.lgclblkno = (uint64_t)value; 12957 12958 if (pos_info->begin_of_part && pos_info->end_of_part) { 12959 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 12960 "SHORT_POS returned begin and end of" 12961 " partition\n"); 12962 break; 12963 } 12964 /* Is drive rewound */ 12965 if ((pos_info->begin_of_part == 1) && 12966 (pos_info->host_block == 0)) { 12967 un->un_pos.blkno = 0; 12968 un->un_pos.fileno = 0; 12969 un->un_pos.pmode = legacy; 12970 } else if (un->un_pos.pmode == invalid) { 12971 /* If we were lost now were found */ 12972 un->un_pos.pmode = logical; 12973 } 12974 } else { 12975 un->un_pos.pmode = invalid; 12976 } 12977 break; 12978 } 12979 12980 case LONG_POS: /* Long data format */ 12981 { 12982 uint64_t value; 12983 tape_position_long_t *long_pos_info = 12984 (tape_position_long_t *)responce; 12985 12986 /* If reserved fields are non zero don't use the data */ 12987 if ((long_pos_info->reserved0) || 12988 (long_pos_info->reserved1) || 12989 (long_pos_info->reserved2)) { 12990 rval = EIO; 12991 break; 12992 } 12993 12994 /* Is position Valid */ 12995 if (long_pos_info->blk_posi_unkwn == 0) { 12996 uint32_t part; 12997 12998 part = long_pos_info->partition; 12999 FIX_ENDIAN32(&part); 13000 if (un->un_pos.partition != part) { 13001 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 13002 "LONG_POS current partition %d" 13003 " read %d\n", un->un_pos.partition, part); 13004 } 13005 un->un_pos.partition = part; 13006 value = long_pos_info->block_number; 13007 FIX_ENDIAN64(&value); 13008 if (un->un_pos.lgclblkno != value) { 13009 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 13010 "LONG_POS current logical 0x%"PRIx64 13011 " read 0x%"PRIx64"\n", 13012 un->un_pos.lgclblkno, value); 13013 } 13014 un->un_pos.lgclblkno = value; 13015 13016 if (long_pos_info->begin_of_part && 13017 long_pos_info->end_of_part) { 13018 ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, 13019 "LONG_POS returned begin and end of" 13020 " partition\n"); 13021 break; 13022 } 13023 if ((long_pos_info->begin_of_part == 1) && 13024 (long_pos_info->block_number == 0)) { 13025 un->un_pos.blkno = 0; 13026 un->un_pos.fileno = 0; 13027 un->un_pos.pmode = legacy; 13028 } else if (un->un_pos.pmode == invalid) { 13029 un->un_pos.pmode = logical; 13030 } 13031 } else { 13032 /* 13033 * If the drive doesn't know location, 13034 * we don't either. 13035 */ 13036 un->un_pos.pmode = invalid; 13037 } 13038 13039 value = long_pos_info->file_number; 13040 FIX_ENDIAN64(&value); 13041 /* Is file position valid */ 13042 if (long_pos_info->mrk_posi_unkwn == 0) { 13043 if (((un->un_pos.pmode == legacy) || 13044 (un->un_pos.pmode == logical)) && 13045 (un->un_pos.fileno != value)) { 13046 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 13047 "LONG_POS fileno 0x%"PRIx64 13048 " not un_pos %x\n", value, 13049 un->un_pos.fileno); 13050 } else if (un->un_pos.pmode == invalid) { 13051 un->un_pos.pmode = logical; 13052 } 13053 un->un_pos.fileno = (int32_t)value; 13054 } else { 13055 /* 13056 * If the drive doesn't know its position, 13057 * we don't either. 13058 */ 13059 un->un_pos.pmode = invalid; 13060 } 13061 if (un->un_pos.pmode != invalid && long_pos_info->end_of_part) { 13062 un->un_pos.eof = ST_EOT; 13063 } 13064 13065 break; 13066 } 13067 13068 case EXT_POS: /* Extended data format */ 13069 { 13070 uint64_t value; 13071 tape_position_ext_t *ext_pos_info = 13072 (tape_position_ext_t *)responce; 13073 13074 /* Make sure that there is enough data there */ 13075 if (data_sz < 16) { 13076 break; 13077 } 13078 13079 /* If reserved fields are non zero don't use the data */ 13080 if (ext_pos_info->reserved0 || ext_pos_info->reserved1) { 13081 rval = EIO; 13082 break; 13083 } 13084 13085 /* 13086 * In the unlikely event of overflowing 64 bits of position. 13087 */ 13088 if (ext_pos_info->posi_err != 0) { 13089 rval = ERANGE; 13090 break; 13091 } 13092 13093 /* Is block position information valid */ 13094 if (ext_pos_info->blk_posi_unkwn == 0) { 13095 13096 if (un->un_pos.partition != ext_pos_info->partition) { 13097 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 13098 "EXT_POS current partition %d read %d\n", 13099 un->un_pos.partition, 13100 ext_pos_info->partition); 13101 } 13102 un->un_pos.partition = ext_pos_info->partition; 13103 13104 value = ext_pos_info->host_block; 13105 FIX_ENDIAN64(&value); 13106 if (un->un_pos.lgclblkno != value) { 13107 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 13108 "EXT_POS current logical 0x%"PRIx64 13109 " read 0x%"PRIx64"\n", 13110 un->un_pos.lgclblkno, value); 13111 } 13112 un->un_pos.lgclblkno = value; 13113 if ((ext_pos_info->begin_of_part == 1) && 13114 (ext_pos_info->host_block == 0)) { 13115 un->un_pos.blkno = 0; 13116 un->un_pos.fileno = 0; 13117 un->un_pos.pmode = legacy; 13118 } else if (un->un_pos.pmode == invalid) { 13119 un->un_pos.pmode = logical; 13120 } 13121 } else { 13122 un->un_pos.pmode = invalid; 13123 } 13124 break; 13125 } 13126 13127 default: 13128 ST_DEBUG(ST_DEVINFO, st_label, CE_PANIC, 13129 "Got unexpected SCMD_READ_POSITION type %d\n", type); 13130 rval = EIO; 13131 } 13132 13133 return (rval); 13134 } 13135 13136 static int 13137 st_logical_block_locate(struct scsi_tape *un, uint64_t lblk, uchar_t partition) 13138 { 13139 int rval; 13140 char cdb[CDB_GROUP4]; 13141 struct uscsi_cmd *cmd; 13142 struct scsi_extended_sense sense; 13143 13144 ST_FUNC(ST_DEVINFO, st_logical_block_locate); 13145 13146 cmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP); 13147 13148 if (lblk <= INT32_MAX) { 13149 cmd->uscsi_cdblen = CDB_GROUP1; 13150 cdb[0] = SCMD_LOCATE; 13151 cdb[1] = un->un_pos.partition == partition ? 0 : 2; 13152 cdb[2] = 0; 13153 cdb[3] = (char)(lblk >> 24); 13154 cdb[4] = (char)(lblk >> 16); 13155 cdb[5] = (char)(lblk >> 8); 13156 cdb[6] = (char)(lblk); 13157 cdb[7] = 0; 13158 cdb[8] = partition; 13159 cdb[9] = 0; 13160 } else { 13161 /* 13162 * If the drive doesn't give a 64 bit read position data 13163 * it is unlikely it will accept 64 bit locates. 13164 */ 13165 if (un->un_read_pos_type != LONG_POS) { 13166 kmem_free(cmd, sizeof (struct uscsi_cmd)); 13167 return (ERANGE); 13168 } 13169 cmd->uscsi_cdblen = CDB_GROUP4; 13170 cdb[0] = (char)SCMD_LOCATE_G4; 13171 cdb[1] = un->un_pos.partition == partition ? 0 : 2; 13172 cdb[2] = 0; 13173 cdb[3] = partition; 13174 cdb[4] = (char)(lblk >> 56); 13175 cdb[5] = (char)(lblk >> 48); 13176 cdb[6] = (char)(lblk >> 40); 13177 cdb[7] = (char)(lblk >> 32); 13178 cdb[8] = (char)(lblk >> 24); 13179 cdb[9] = (char)(lblk >> 16); 13180 cdb[10] = (char)(lblk >> 8); 13181 cdb[11] = (char)(lblk); 13182 cdb[12] = 0; 13183 cdb[13] = 0; 13184 cdb[14] = 0; 13185 cdb[15] = 0; 13186 } 13187 13188 13189 cmd->uscsi_flags = USCSI_WRITE | USCSI_DIAGNOSE | USCSI_RQENABLE; 13190 cmd->uscsi_rqbuf = (caddr_t)&sense; 13191 cmd->uscsi_rqlen = sizeof (sense); 13192 cmd->uscsi_timeout = un->un_dp->space_timeout; 13193 cmd->uscsi_cdb = cdb; 13194 13195 rval = st_ioctl_cmd(un->un_dev, cmd, FKIOCTL); 13196 13197 un->un_pos.pmode = logical; 13198 un->un_pos.eof = ST_NO_EOF; 13199 13200 if (lblk > INT32_MAX) { 13201 /* 13202 * XXX This is a work around till we handle Descriptor format 13203 * sense data. Since we are sending a command where the standard 13204 * sense data can not correctly represent a correct residual in 13205 * 4 bytes. 13206 */ 13207 if (un->un_status == KEY_ILLEGAL_REQUEST) { 13208 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 13209 "Big LOCATE ILLEGAL_REQUEST: rval = %d\n", rval); 13210 /* Doesn't like big locate command */ 13211 un->un_status = 0; 13212 rval = ERANGE; 13213 } else if ((un->un_pos.pmode == invalid) || (rval != 0)) { 13214 /* Aborted big locate command */ 13215 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 13216 "Big LOCATE resulted in invalid pos: rval = %d\n", 13217 rval); 13218 un->un_status = 0; 13219 rval = EIO; 13220 } else if (st_update_block_pos(un)) { 13221 /* read position failed */ 13222 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 13223 "Big LOCATE and read pos: rval = %d\n", rval); 13224 rval = EIO; 13225 } else if (lblk > un->un_pos.lgclblkno) { 13226 /* read position worked but position was not expected */ 13227 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 13228 "Big LOCATE and recover read less then desired 0x%" 13229 PRIx64"\n", un->un_pos.lgclblkno); 13230 un->un_err_resid = lblk - un->un_pos.lgclblkno; 13231 un->un_status = KEY_BLANK_CHECK; 13232 rval = ESPIPE; 13233 } else if (lblk == un->un_pos.lgclblkno) { 13234 /* read position was what was expected */ 13235 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 13236 "Big LOCATE and recover seems to have worked\n"); 13237 un->un_err_resid = 0; 13238 rval = 0; 13239 } else { 13240 ST_DEBUG(ST_DEVINFO, st_label, CE_PANIC, 13241 "BIGLOCATE end up going backwards"); 13242 un->un_err_resid = lblk; 13243 rval = EIO; 13244 } 13245 13246 } else if (rval == 0) { 13247 /* Worked as requested */ 13248 un->un_pos.lgclblkno = lblk; 13249 13250 } else if (((cmd->uscsi_status & STATUS_MASK) == STATUS_CHECK) && 13251 (cmd->uscsi_resid != 0)) { 13252 /* Got part way there but wasn't enough blocks on tape */ 13253 un->un_pos.lgclblkno = lblk - cmd->uscsi_resid; 13254 un->un_err_resid = cmd->uscsi_resid; 13255 un->un_status = KEY_BLANK_CHECK; 13256 rval = ESPIPE; 13257 13258 } else if (st_update_block_pos(un) == 0) { 13259 /* Got part way there but drive didn't tell what we missed by */ 13260 un->un_err_resid = lblk - un->un_pos.lgclblkno; 13261 un->un_status = KEY_BLANK_CHECK; 13262 rval = ESPIPE; 13263 13264 } else { 13265 scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG, 13266 "Failed LOCATE and recover pos: rval = %d status = %d\n", 13267 rval, cmd->uscsi_status); 13268 un->un_err_resid = lblk; 13269 un->un_status = KEY_ILLEGAL_REQUEST; 13270 un->un_pos.pmode = invalid; 13271 rval = EIO; 13272 } 13273 13274 kmem_free(cmd, sizeof (struct uscsi_cmd)); 13275 13276 return (rval); 13277 } 13278 13279 static int 13280 st_mtfsf_ioctl(struct scsi_tape *un, int files) 13281 { 13282 int rval; 13283 13284 ST_FUNC(ST_DEVINFO, st_mtfsf_ioctl); 13285 13286 13287 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 13288 "st_mtfsf_ioctl: count=%x, eof=%x\n", files, un->un_pos.eof); 13289 13290 /* pmode == invalid already handled */ 13291 if (un->un_pos.pmode == legacy) { 13292 /* 13293 * forward space over filemark 13294 * 13295 * For ASF we allow a count of 0 on fsf which means 13296 * we just want to go to beginning of current file. 13297 * Equivalent to "nbsf(0)" or "bsf(1) + fsf". 13298 * Allow stepping over double fmk with reel 13299 */ 13300 if ((un->un_pos.eof >= ST_EOT) && 13301 (files > 0) && 13302 ((un->un_dp->options & ST_REEL) == 0)) { 13303 /* we're at EOM */ 13304 un->un_err_resid = files; 13305 un->un_status = KEY_BLANK_CHECK; 13306 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13307 "st_mtfsf_ioctl: EIO : MTFSF at EOM"); 13308 return (EIO); 13309 } 13310 13311 /* 13312 * physical tape position may not be what we've been 13313 * telling the user; adjust the request accordingly 13314 */ 13315 if (IN_EOF(un->un_pos)) { 13316 un->un_pos.fileno++; 13317 un->un_pos.blkno = 0; 13318 /* 13319 * For positive direction case, we're now covered. 13320 * For zero or negative direction, we're covered 13321 * (almost) 13322 */ 13323 files--; 13324 } 13325 13326 } 13327 13328 if (st_check_density_or_wfm(un->un_dev, 1, B_READ, STEPBACK)) { 13329 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13330 "st_mtfsf_ioctl: EIO : MTFSF density/wfm failed"); 13331 return (EIO); 13332 } 13333 13334 13335 /* 13336 * Forward space file marks. 13337 * We leave ourselves at block zero 13338 * of the target file number. 13339 */ 13340 if (files < 0) { 13341 rval = st_backward_space_files(un, -files, 0); 13342 } else { 13343 rval = st_forward_space_files(un, files); 13344 } 13345 13346 return (rval); 13347 } 13348 13349 static int 13350 st_forward_space_files(struct scsi_tape *un, int count) 13351 { 13352 dev_t dev; 13353 int rval; 13354 13355 ST_FUNC(ST_DEVINFO, st_forward_space_files); 13356 13357 dev = un->un_dev; 13358 13359 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 13360 "fspace: count=%x, eof=%x\n", count, un->un_pos.eof); 13361 13362 ASSERT(count >= 0); 13363 ASSERT(un->un_pos.pmode != invalid); 13364 13365 /* 13366 * A space with a count of zero means take me to the start of file. 13367 */ 13368 if (count == 0) { 13369 13370 /* Hay look were already there */ 13371 if (un->un_pos.pmode == legacy && un->un_pos.blkno == 0 && 13372 un->un_pos.fileno == 0) { 13373 un->un_err_resid = 0; 13374 COPY_POS(&un->un_err_pos, &un->un_pos); 13375 return (0); 13376 } 13377 13378 /* 13379 * Well we are in the first file. 13380 * A rewind will get to the start. 13381 */ 13382 if (un->un_pos.pmode == legacy && un->un_pos.fileno == 0) { 13383 rval = st_cmd(dev, SCMD_REWIND, 0, SYNC_CMD); 13384 13385 /* 13386 * Can we backspace to get there? 13387 * This should work in logical mode. 13388 */ 13389 } else if (un->un_dp->options & ST_BSF) { 13390 rval = st_space_to_begining_of_file(un); 13391 13392 /* 13393 * Can't back space but current file number is known, 13394 * So rewind and space from the begining of the partition. 13395 */ 13396 } else if (un->un_pos.pmode == legacy) { 13397 rval = st_scenic_route_to_begining_of_file(un, 13398 un->un_pos.fileno); 13399 13400 /* 13401 * pmode is logical and ST_BSF is not set. 13402 * The LONG_POS read position contains the fileno. 13403 * If the read position works, rewind and space. 13404 */ 13405 } else if (un->un_read_pos_type == LONG_POS) { 13406 rval = st_cmd(dev, SCMD_READ_POSITION, 0, SYNC_CMD); 13407 if (rval) { 13408 /* 13409 * We didn't get the file position from the 13410 * read position command. 13411 * We are going to trust the drive to backspace 13412 * and then position after the filemark. 13413 */ 13414 rval = st_space_to_begining_of_file(un); 13415 } 13416 rval = st_interpret_read_pos(un, LONG_POS, 32, 13417 (caddr_t)un->un_read_pos_data); 13418 if ((rval) && (un->un_pos.pmode == invalid)) { 13419 rval = st_space_to_begining_of_file(un); 13420 } else { 13421 rval = st_scenic_route_to_begining_of_file(un, 13422 un->un_pos.fileno); 13423 } 13424 } else { 13425 rval = EIO; 13426 } 13427 /* 13428 * If something didn't work we are lost 13429 */ 13430 if (rval != 0) { 13431 un->un_pos.pmode = invalid; 13432 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13433 "st_mtioctop : EIO : fspace pmode invalid"); 13434 13435 rval = EIO; 13436 } 13437 13438 } else { 13439 rval = st_space_fmks(dev, count); 13440 } 13441 13442 if (rval != EIO && count < 0) { 13443 /* 13444 * we came here with a count < 0; we now need 13445 * to skip back to end up before the filemark 13446 */ 13447 rval = st_backward_space_files(un, 1, 1); 13448 } 13449 13450 return (rval); 13451 } 13452 13453 static int 13454 st_scenic_route_to_begining_of_file(struct scsi_tape *un, int32_t fileno) 13455 { 13456 int rval; 13457 13458 ST_FUNC(ST_DEVINFO, st_scenic_route_to_begining_of_file); 13459 13460 if (st_cmd(un->un_dev, SCMD_REWIND, 0, SYNC_CMD)) { 13461 rval = EIO; 13462 } else if (st_cmd(un->un_dev, SCMD_SPACE, Fmk(fileno), SYNC_CMD)) { 13463 rval = EIO; 13464 } 13465 13466 return (rval); 13467 } 13468 13469 static int 13470 st_space_to_begining_of_file(struct scsi_tape *un) 13471 { 13472 int rval; 13473 13474 ST_FUNC(ST_DEVINFO, st_space_to_begining_of_file); 13475 13476 /* 13477 * Back space of the file at the begining of the file. 13478 */ 13479 rval = st_cmd(un->un_dev, SCMD_SPACE, Fmk((-1)), SYNC_CMD); 13480 if (rval) { 13481 rval = EIO; 13482 return (rval); 13483 } 13484 13485 /* 13486 * Other interesting answers might be crashed BOT which isn't bad. 13487 */ 13488 if (un->un_status == SUN_KEY_BOT) { 13489 return (rval); 13490 } 13491 13492 /* 13493 * Now we are on the BOP side of the filemark. Forward space to 13494 * the EOM side and we are at the begining of the file. 13495 */ 13496 rval = st_cmd(un->un_dev, SCMD_SPACE, Fmk(1), SYNC_CMD); 13497 if (rval) { 13498 rval = EIO; 13499 } 13500 13501 return (rval); 13502 } 13503 13504 static int 13505 st_mtfsr_ioctl(struct scsi_tape *un, int count) 13506 { 13507 13508 ST_FUNC(ST_DEVINFO, st_mtfsr_ioctl); 13509 13510 /* 13511 * forward space to inter-record gap 13512 * 13513 */ 13514 13515 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 13516 "st_ioctl_fsr: count=%x, eof=%x\n", count, un->un_pos.eof); 13517 13518 if (un->un_pos.pmode == legacy) { 13519 /* 13520 * If were are at end of tape and count is forward. 13521 * Return blank check. 13522 */ 13523 if ((un->un_pos.eof >= ST_EOT) && (count > 0)) { 13524 /* we're at EOM */ 13525 un->un_err_resid = count; 13526 un->un_status = KEY_BLANK_CHECK; 13527 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13528 "st_mtfsr_ioctl: EIO : MTFSR eof > ST_EOT"); 13529 return (EIO); 13530 } 13531 13532 /* 13533 * If count is zero there is nothing to do. 13534 */ 13535 if (count == 0) { 13536 un->un_err_pos.fileno = un->un_pos.fileno; 13537 un->un_err_pos.blkno = un->un_pos.blkno; 13538 un->un_err_resid = 0; 13539 if (IN_EOF(un->un_pos) && SVR4_BEHAVIOR) { 13540 un->un_status = SUN_KEY_EOF; 13541 } 13542 return (0); 13543 } 13544 13545 /* 13546 * physical tape position may not be what we've been 13547 * telling the user; adjust the position accordingly 13548 */ 13549 if (IN_EOF(un->un_pos)) { 13550 daddr_t blkno = un->un_pos.blkno; 13551 int fileno = un->un_pos.fileno; 13552 13553 optype lastop = un->un_lastop; 13554 if (st_cmd(un->un_dev, SCMD_SPACE, Fmk((-1)), SYNC_CMD) 13555 == -1) { 13556 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13557 "st_mtfsr_ioctl:EIO:MTFSR count && IN_EOF"); 13558 return (EIO); 13559 } 13560 13561 un->un_pos.blkno = blkno; 13562 un->un_pos.fileno = fileno; 13563 un->un_lastop = lastop; 13564 } 13565 } 13566 13567 if (st_check_density_or_wfm(un->un_dev, 1, B_READ, STEPBACK)) { 13568 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13569 "st_mtfsr_ioctl: EIO : MTFSR st_check_den"); 13570 return (EIO); 13571 } 13572 13573 return (st_space_records(un, count)); 13574 } 13575 13576 static int 13577 st_space_records(struct scsi_tape *un, int count) 13578 { 13579 int dblk; 13580 int rval = 0; 13581 13582 ST_FUNC(ST_DEVINFO, st_space_records); 13583 13584 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 13585 "st_space_records: count=%x, eof=%x\n", count, un->un_pos.eof); 13586 13587 if (un->un_pos.pmode == logical) { 13588 rval = st_cmd(un->un_dev, SCMD_SPACE, Blk(count), SYNC_CMD); 13589 if (rval != 0) { 13590 rval = EIO; 13591 } 13592 return (rval); 13593 } 13594 13595 dblk = un->un_pos.blkno + count; 13596 13597 /* Already there */ 13598 if (dblk == un->un_pos.blkno) { 13599 un->un_err_resid = 0; 13600 COPY_POS(&un->un_err_pos, &un->un_pos); 13601 return (0); 13602 } 13603 13604 /* 13605 * If the destination block is forward 13606 * or the drive will backspace records. 13607 */ 13608 if (un->un_pos.blkno < dblk || (un->un_dp->options & ST_BSR)) { 13609 /* 13610 * If we're spacing forward, or the device can 13611 * backspace records, we can just use the SPACE 13612 * command. 13613 */ 13614 dblk -= un->un_pos.blkno; 13615 if (st_cmd(un->un_dev, SCMD_SPACE, Blk(dblk), SYNC_CMD)) { 13616 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13617 "st_space_records:EIO:space_records can't spc"); 13618 rval = EIO; 13619 } else if (un->un_pos.eof >= ST_EOF_PENDING) { 13620 /* 13621 * check if we hit BOT/EOT 13622 */ 13623 if (dblk < 0 && un->un_pos.eof == ST_EOM) { 13624 un->un_status = SUN_KEY_BOT; 13625 un->un_pos.eof = ST_NO_EOF; 13626 } else if (dblk < 0 && 13627 un->un_pos.eof == ST_EOF_PENDING) { 13628 int residue = un->un_err_resid; 13629 /* 13630 * we skipped over a filemark 13631 * and need to go forward again 13632 */ 13633 if (st_cmd(un->un_dev, SCMD_SPACE, Fmk(1), 13634 SYNC_CMD)) { 13635 ST_DEBUG2(ST_DEVINFO, st_label, 13636 SCSI_DEBUG, "st_space_records: EIO" 13637 " : can't space #2"); 13638 rval = EIO; 13639 } 13640 un->un_err_resid = residue; 13641 } 13642 if (rval == 0) { 13643 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13644 "st_space_records: EIO : space_rec rval" 13645 " == 0"); 13646 rval = EIO; 13647 } 13648 } 13649 } else { 13650 /* 13651 * else we rewind, space forward across filemarks to 13652 * the desired file, and then space records to the 13653 * desired block. 13654 */ 13655 13656 int dfile = un->un_pos.fileno; /* save current file */ 13657 13658 if (dblk < 0) { 13659 /* 13660 * Wups - we're backing up over a filemark 13661 */ 13662 if (un->un_pos.blkno != 0 && 13663 (st_cmd(un->un_dev, SCMD_REWIND, 0, SYNC_CMD) || 13664 st_cmd(un->un_dev, SCMD_SPACE, Fmk(dfile), 13665 SYNC_CMD))) { 13666 un->un_pos.pmode = invalid; 13667 } 13668 un->un_err_resid = -dblk; 13669 if (un->un_pos.fileno == 0 && un->un_pos.blkno == 0) { 13670 un->un_status = SUN_KEY_BOT; 13671 un->un_pos.eof = ST_NO_EOF; 13672 } else if (un->un_pos.fileno > 0) { 13673 un->un_status = SUN_KEY_EOF; 13674 un->un_pos.eof = ST_NO_EOF; 13675 } 13676 COPY_POS(&un->un_err_pos, &un->un_pos); 13677 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13678 "st_space_records:EIO:space_records : dblk < 0"); 13679 rval = EIO; 13680 } else if (st_cmd(un->un_dev, SCMD_REWIND, 0, SYNC_CMD) || 13681 st_cmd(un->un_dev, SCMD_SPACE, Fmk(dfile), SYNC_CMD) || 13682 st_cmd(un->un_dev, SCMD_SPACE, Blk(dblk), SYNC_CMD)) { 13683 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13684 "st_space_records: EIO :space_records : rewind " 13685 "and space failed"); 13686 un->un_pos.pmode = invalid; 13687 rval = EIO; 13688 } 13689 } 13690 13691 return (rval); 13692 } 13693 13694 static int 13695 st_mtbsf_ioctl(struct scsi_tape *un, int files) 13696 { 13697 ST_FUNC(ST_DEVINFO, st_mtbsf_ioctl); 13698 13699 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 13700 "st_mtbsf_ioctl: count=%x, eof=%x\n", files, un->un_pos.eof); 13701 /* 13702 * backward space of file filemark (1/2" and 8mm) 13703 * tape position will end on the beginning of tape side 13704 * of the desired file mark 13705 */ 13706 if ((un->un_dp->options & ST_BSF) == 0) { 13707 return (ENOTTY); 13708 } 13709 13710 if (un->un_pos.pmode == legacy) { 13711 13712 /* 13713 * If a negative count (which implies a forward space op) 13714 * is specified, and we're at logical or physical eot, 13715 * bounce the request. 13716 */ 13717 13718 if (un->un_pos.eof >= ST_EOT && files < 0) { 13719 un->un_err_resid = files; 13720 un->un_status = SUN_KEY_EOT; 13721 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13722 "st_ioctl_mt_bsf : EIO : MTBSF : eof > ST_EOF"); 13723 return (EIO); 13724 } 13725 /* 13726 * physical tape position may not be what we've been 13727 * telling the user; adjust the request accordingly 13728 */ 13729 if (IN_EOF(un->un_pos)) { 13730 un->un_pos.fileno++; 13731 un->un_pos.blkno = 0; 13732 files++; 13733 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 13734 "st_mtbsf_ioctl in eof: count=%d, op=%x\n", 13735 files, MTBSF); 13736 13737 } 13738 } 13739 13740 if (st_check_density_or_wfm(un->un_dev, 1, 0, STEPBACK)) { 13741 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13742 "st_ioctl : EIO : MTBSF : check den wfm"); 13743 return (EIO); 13744 } 13745 13746 if (files <= 0) { 13747 /* 13748 * for a negative count, we need to step forward 13749 * first and then step back again 13750 */ 13751 files = -files + 1; 13752 return (st_forward_space_files(un, files)); 13753 } 13754 return (st_backward_space_files(un, files, 1)); 13755 } 13756 13757 static int 13758 st_backward_space_files(struct scsi_tape *un, int count, int infront) 13759 { 13760 int end_fileno; 13761 int skip_cnt; 13762 int rval = 0; 13763 13764 ST_FUNC(ST_DEVINFO, st_backward_space_files); 13765 13766 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 13767 "st_backward_space_files: count=%x eof=%x\n", 13768 count, un->un_pos.eof); 13769 /* 13770 * Backspace files (MTNBSF): infront == 0 13771 * 13772 * For tapes that can backspace, backspace 13773 * count+1 filemarks and then run forward over 13774 * a filemark 13775 * 13776 * For tapes that can't backspace, 13777 * calculate desired filenumber 13778 * (un->un_pos.fileno - count), rewind, 13779 * and then space forward this amount 13780 * 13781 * Backspace filemarks (MTBSF) infront == 1 13782 * 13783 * For tapes that can backspace, backspace count 13784 * filemarks 13785 * 13786 * For tapes that can't backspace, calculate 13787 * desired filenumber (un->un_pos.fileno - count), 13788 * add 1, rewind, space forward this amount, 13789 * and mark state as ST_EOF_PENDING appropriately. 13790 */ 13791 13792 if (un->un_pos.pmode == logical) { 13793 13794 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 13795 "st_backward_space_files: mt_op=%x count=%x" 13796 "lgclblkno=%"PRIx64"\n", infront?MTBSF:MTNBSF, count, 13797 un->un_pos.lgclblkno); 13798 13799 13800 /* In case a drive that won't back space gets in logical mode */ 13801 if ((un->un_dp->options & ST_BSF) == 0) { 13802 rval = EIO; 13803 return (rval); 13804 } 13805 if (st_cmd(un->un_dev, SCMD_SPACE, Fmk(-count), SYNC_CMD)) { 13806 rval = EIO; 13807 return (rval); 13808 } 13809 if ((infront != 0) && 13810 (st_cmd(un->un_dev, SCMD_SPACE, Fmk(1), SYNC_CMD))) { 13811 rval = EIO; 13812 return (rval); 13813 } 13814 return (rval); 13815 } 13816 13817 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 13818 "st_backward_space_files: mt_op=%x count=%x fileno=%x blkno=%x\n", 13819 infront?MTBSF:MTNBSF, count, un->un_pos.fileno, un->un_pos.blkno); 13820 13821 13822 13823 /* 13824 * Handle the simple case of BOT 13825 * playing a role in these cmds. 13826 * We do this by calculating the 13827 * ending file number. If the ending 13828 * file is < BOT, rewind and set an 13829 * error and mark resid appropriately. 13830 * If we're backspacing a file (not a 13831 * filemark) and the target file is 13832 * the first file on the tape, just 13833 * rewind. 13834 */ 13835 13836 /* figure expected destination of this SPACE command */ 13837 end_fileno = un->un_pos.fileno - count; 13838 13839 /* 13840 * Would the end effect of this SPACE be the same as rewinding? 13841 * If so just rewind instead. 13842 */ 13843 if ((infront != 0) && (end_fileno < 0) || 13844 (infront == 0) && (end_fileno <= 0)) { 13845 if (st_cmd(un->un_dev, SCMD_REWIND, 0, SYNC_CMD)) { 13846 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13847 "st_backward_space_files: EIO : " 13848 "rewind in lou of BSF failed\n"); 13849 rval = EIO; 13850 } 13851 if (end_fileno < 0) { 13852 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13853 "st_backward_space_files: EIO : " 13854 "back space file greater then fileno\n"); 13855 rval = EIO; 13856 un->un_err_resid = -end_fileno; 13857 un->un_status = SUN_KEY_BOT; 13858 } 13859 return (rval); 13860 } 13861 13862 if (un->un_dp->options & ST_BSF) { 13863 skip_cnt = 1 - infront; 13864 /* 13865 * If we are going to end up at the beginning 13866 * of the file, we have to space one extra file 13867 * first, and then space forward later. 13868 */ 13869 end_fileno = -(count + skip_cnt); 13870 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 13871 "skip_cnt=%x, tmp=%x\n", skip_cnt, end_fileno); 13872 if (st_cmd(un->un_dev, SCMD_SPACE, Fmk(end_fileno), SYNC_CMD)) { 13873 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13874 "st_backward_space_files:EIO:back space fm failed"); 13875 rval = EIO; 13876 } 13877 } else { 13878 if (st_cmd(un->un_dev, SCMD_REWIND, 0, SYNC_CMD)) { 13879 rval = EIO; 13880 } else { 13881 skip_cnt = end_fileno + infront; 13882 } 13883 } 13884 13885 /* 13886 * If we have to space forward, do so... 13887 */ 13888 ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, 13889 "space forward skip_cnt=%x, rval=%x\n", skip_cnt, rval); 13890 13891 if (rval == 0 && skip_cnt) { 13892 if (st_cmd(un->un_dev, SCMD_SPACE, Fmk(skip_cnt), SYNC_CMD)) { 13893 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13894 "st_backward_space_files:EIO:space fm skip count"); 13895 rval = EIO; 13896 } else if (infront) { 13897 /* 13898 * If we had to space forward, and we're 13899 * not a tape that can backspace, mark state 13900 * as if we'd just seen a filemark during a 13901 * a read. 13902 */ 13903 if ((un->un_dp->options & ST_BSF) == 0) { 13904 un->un_pos.eof = ST_EOF_PENDING; 13905 un->un_pos.fileno -= 1; 13906 un->un_pos.blkno = INF; 13907 } 13908 } 13909 } 13910 13911 if (rval != 0) { 13912 un->un_pos.pmode = invalid; 13913 } 13914 13915 return (rval); 13916 } 13917 13918 static int 13919 st_mtnbsf_ioctl(struct scsi_tape *un, int count) 13920 { 13921 int rval; 13922 13923 ST_FUNC(ST_DEVINFO, st_mtnbsf_ioctl); 13924 13925 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 13926 "nbsf: count=%x, eof=%x\n", count, un->un_pos.eof); 13927 13928 if (un->un_pos.pmode == legacy) { 13929 /* 13930 * backward space file to beginning of file 13931 * 13932 * If a negative count (which implies a forward space op) 13933 * is specified, and we're at logical or physical eot, 13934 * bounce the request. 13935 */ 13936 13937 if (un->un_pos.eof >= ST_EOT && count < 0) { 13938 un->un_err_resid = count; 13939 un->un_status = SUN_KEY_EOT; 13940 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13941 "st_ioctl : EIO : > EOT and count < 0"); 13942 return (EIO); 13943 } 13944 /* 13945 * physical tape position may not be what we've been 13946 * telling the user; adjust the request accordingly 13947 */ 13948 if (IN_EOF(un->un_pos)) { 13949 un->un_pos.fileno++; 13950 un->un_pos.blkno = 0; 13951 count++; 13952 } 13953 } 13954 13955 if (st_check_density_or_wfm(un->un_dev, 1, 0, STEPBACK)) { 13956 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13957 "st_ioctl : EIO : MTNBSF check den and wfm"); 13958 return (EIO); 13959 } 13960 13961 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 13962 "mtnbsf: count=%x, eof=%x\n", count, un->un_pos.eof); 13963 13964 if (count <= 0) { 13965 rval = st_forward_space_files(un, -count); 13966 } else { 13967 rval = st_backward_space_files(un, count, 0); 13968 } 13969 return (rval); 13970 } 13971 13972 static int 13973 st_mtbsr_ioctl(struct scsi_tape *un, int num) 13974 { 13975 ST_FUNC(ST_DEVINFO, st_mtbsr_ioctl); 13976 13977 ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, 13978 "bsr: count=%x, eof=%x\n", num, un->un_pos.eof); 13979 13980 if (un->un_pos.pmode == legacy) { 13981 /* 13982 * backward space into inter-record gap 13983 * 13984 * If a negative count (which implies a forward space op) 13985 * is specified, and we're at logical or physical eot, 13986 * bounce the request. 13987 */ 13988 if (un->un_pos.eof >= ST_EOT && num < 0) { 13989 un->un_err_resid = num; 13990 un->un_status = SUN_KEY_EOT; 13991 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 13992 "st_ioctl : EIO : MTBSR > EOT"); 13993 return (EIO); 13994 } 13995 13996 if (num == 0) { 13997 COPY_POS(&un->un_err_pos, &un->un_pos); 13998 un->un_err_resid = 0; 13999 if (IN_EOF(un->un_pos) && SVR4_BEHAVIOR) { 14000 un->un_status = SUN_KEY_EOF; 14001 } 14002 return (0); 14003 } 14004 14005 /* 14006 * physical tape position may not be what we've been 14007 * telling the user; adjust the position accordingly. 14008 * bsr can not skip filemarks and continue to skip records 14009 * therefore if we are logically before the filemark but 14010 * physically at the EOT side of the filemark, we need to step 14011 * back; this allows fsr N where N > number of blocks in file 14012 * followed by bsr 1 to position at the beginning of last block 14013 */ 14014 if (IN_EOF(un->un_pos)) { 14015 tapepos_t save; 14016 optype lastop = un->un_lastop; 14017 14018 COPY_POS(&save, &un->un_pos); 14019 if (st_cmd(un->un_dev, SCMD_SPACE, Fmk((-1)), SYNC_CMD) 14020 == -1) { 14021 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 14022 "st_write_fm : EIO : MTBSR can't space"); 14023 return (EIO); 14024 } 14025 14026 COPY_POS(&un->un_pos, &save); 14027 un->un_lastop = lastop; 14028 } 14029 } 14030 14031 un->un_pos.eof = ST_NO_EOF; 14032 14033 if (st_check_density_or_wfm(un->un_dev, 1, 0, STEPBACK)) { 14034 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 14035 "st_ioctl : EIO : MTBSR : can't set density or wfm"); 14036 return (EIO); 14037 } 14038 14039 num = -num; 14040 return (st_space_records(un, num)); 14041 } 14042 14043 static int 14044 st_mtfsfm_ioctl(struct scsi_tape *un, int cnt) 14045 { 14046 int rval; 14047 14048 ST_FUNC(ST_DEVINFO, st_mtfsfm_ioctl); 14049 14050 rval = st_cmd(un->un_dev, SCMD_SPACE, SPACE(SP_SQFLM, cnt), SYNC_CMD); 14051 if (rval == 0) { 14052 un->un_pos.pmode = logical; 14053 } else if ((un->un_status == KEY_ILLEGAL_REQUEST) && 14054 (un->un_sd->sd_sense->es_add_code == 0x24)) { 14055 /* 14056 * Drive says invalid field in cdb. 14057 * Doesn't like space multiple. Position isn't lost. 14058 */ 14059 un->un_err_resid = cnt; 14060 un->un_status = 0; 14061 rval = ENOTTY; 14062 } else { 14063 un->un_err_resid = cnt; 14064 un->un_pos.pmode = invalid; 14065 } 14066 return (rval); 14067 } 14068 14069 static int 14070 st_mtbsfm_ioctl(struct scsi_tape *un, int cnt) 14071 { 14072 int rval; 14073 14074 ST_FUNC(ST_DEVINFO, st_mtbsfm_ioctl); 14075 14076 rval = st_cmd(un->un_dev, SCMD_SPACE, SPACE(SP_SQFLM, -cnt), SYNC_CMD); 14077 if (rval == 0) { 14078 un->un_pos.pmode = logical; 14079 } else if ((un->un_status == KEY_ILLEGAL_REQUEST) && 14080 (un->un_sd->sd_sense->es_add_code == 0x24)) { 14081 /* 14082 * Drive says invalid field in cdb. 14083 * Doesn't like space multiple. Position isn't lost. 14084 */ 14085 un->un_err_resid = cnt; 14086 un->un_status = 0; 14087 rval = ENOTTY; 14088 } else { 14089 un->un_err_resid = cnt; 14090 un->un_pos.pmode = invalid; 14091 } 14092 return (rval); 14093 } 14094 14095 #ifdef __x86 14096 14097 /* 14098 * release contig_mem and wake up waiting thread, if any 14099 */ 14100 static void 14101 st_release_contig_mem(struct scsi_tape *un, struct contig_mem *cp) 14102 { 14103 mutex_enter(ST_MUTEX); 14104 14105 ST_FUNC(ST_DEVINFO, st_release_contig_mem); 14106 14107 cp->cm_next = un->un_contig_mem; 14108 un->un_contig_mem = cp; 14109 un->un_contig_mem_available_num++; 14110 cv_broadcast(&un->un_contig_mem_cv); 14111 14112 mutex_exit(ST_MUTEX); 14113 } 14114 14115 /* 14116 * St_get_contig_mem will return a contig_mem if there is one available 14117 * in current system. Otherwise, it will try to alloc one, if the total 14118 * number of contig_mem is within st_max_contig_mem_num. 14119 * It will sleep, if allowed by caller or return NULL, if no contig_mem 14120 * is available for now. 14121 */ 14122 static struct contig_mem * 14123 st_get_contig_mem(struct scsi_tape *un, size_t len, int alloc_flags) 14124 { 14125 size_t rlen; 14126 struct contig_mem *cp = NULL; 14127 ddi_acc_handle_t acc_hdl; 14128 caddr_t addr; 14129 int big_enough = 0; 14130 int (*dma_alloc_cb)() = (alloc_flags == KM_SLEEP) ? 14131 DDI_DMA_SLEEP : DDI_DMA_DONTWAIT; 14132 14133 /* Try to get one available contig_mem */ 14134 mutex_enter(ST_MUTEX); 14135 14136 ST_FUNC(ST_DEVINFO, st_get_contig_mem); 14137 14138 if (un->un_contig_mem_available_num > 0) { 14139 ST_GET_CONTIG_MEM_HEAD(un, cp, len, big_enough); 14140 } else if (un->un_contig_mem_total_num < st_max_contig_mem_num) { 14141 /* 14142 * we failed to get one. we're going to 14143 * alloc one more contig_mem for this I/O 14144 */ 14145 mutex_exit(ST_MUTEX); 14146 cp = (struct contig_mem *)kmem_zalloc( 14147 sizeof (struct contig_mem) + biosize(), 14148 alloc_flags); 14149 if (cp == NULL) { 14150 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 14151 "alloc contig_mem failure\n"); 14152 return (NULL); /* cannot get one */ 14153 } 14154 cp->cm_bp = (struct buf *) 14155 (((caddr_t)cp) + sizeof (struct contig_mem)); 14156 bioinit(cp->cm_bp); 14157 mutex_enter(ST_MUTEX); 14158 un->un_contig_mem_total_num++; /* one more available */ 14159 } else { 14160 /* 14161 * we failed to get one and we're NOT allowed to 14162 * alloc more contig_mem 14163 */ 14164 if (alloc_flags == KM_SLEEP) { 14165 while (un->un_contig_mem_available_num <= 0) { 14166 cv_wait(&un->un_contig_mem_cv, 14167 ST_MUTEX); 14168 } 14169 ST_GET_CONTIG_MEM_HEAD(un, cp, len, big_enough); 14170 } else { 14171 mutex_exit(ST_MUTEX); 14172 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 14173 "alloc contig_mem failure\n"); 14174 return (NULL); /* cannot get one */ 14175 } 14176 } 14177 mutex_exit(ST_MUTEX); 14178 14179 /* We need to check if this block of mem is big enough for this I/O */ 14180 if (cp->cm_len < len) { 14181 /* not big enough, need to alloc a new one */ 14182 if (ddi_dma_mem_alloc(un->un_contig_mem_hdl, len, &st_acc_attr, 14183 DDI_DMA_STREAMING, dma_alloc_cb, NULL, 14184 &addr, &rlen, &acc_hdl) != DDI_SUCCESS) { 14185 ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG, 14186 "alloc contig_mem failure: not enough mem\n"); 14187 st_release_contig_mem(un, cp); 14188 cp = NULL; 14189 } else { 14190 if (cp->cm_addr) { 14191 /* release previous one before attach new one */ 14192 ddi_dma_mem_free(&cp->cm_acc_hdl); 14193 } 14194 mutex_enter(ST_MUTEX); 14195 un->un_max_contig_mem_len = 14196 un->un_max_contig_mem_len >= len ? 14197 un->un_max_contig_mem_len : len; 14198 mutex_exit(ST_MUTEX); 14199 14200 /* attach new mem to this cp */ 14201 cp->cm_addr = addr; 14202 cp->cm_acc_hdl = acc_hdl; 14203 cp->cm_len = len; 14204 14205 goto alloc_ok; /* get one usable cp */ 14206 } 14207 } else { 14208 goto alloc_ok; /* get one usable cp */ 14209 } 14210 14211 /* cannot find/alloc a usable cp, when we get here */ 14212 14213 mutex_enter(ST_MUTEX); 14214 if ((un->un_max_contig_mem_len < len) || 14215 (alloc_flags != KM_SLEEP)) { 14216 mutex_exit(ST_MUTEX); 14217 return (NULL); 14218 } 14219 14220 /* 14221 * we're allowed to sleep, and there is one big enough 14222 * contig mem in the system, which is currently in use, 14223 * wait for it... 14224 */ 14225 big_enough = 1; 14226 do { 14227 cv_wait(&un->un_contig_mem_cv, ST_MUTEX); 14228 ST_GET_CONTIG_MEM_HEAD(un, cp, len, big_enough); 14229 } while (cp == NULL); 14230 mutex_exit(ST_MUTEX); 14231 14232 /* we get the big enough contig mem, finally */ 14233 14234 alloc_ok: 14235 /* init bp attached to this cp */ 14236 bioreset(cp->cm_bp); 14237 cp->cm_bp->b_un.b_addr = cp->cm_addr; 14238 cp->cm_bp->b_private = (void *)cp; 14239 14240 return (cp); 14241 } 14242 14243 /* 14244 * this is the biodone func for the bp used in big block I/O 14245 */ 14246 static int 14247 st_bigblk_xfer_done(struct buf *bp) 14248 { 14249 struct contig_mem *cp; 14250 struct buf *orig_bp; 14251 int ioerr; 14252 struct scsi_tape *un; 14253 14254 /* sanity check */ 14255 if (bp == NULL) { 14256 return (DDI_FAILURE); 14257 } 14258 14259 un = ddi_get_soft_state(st_state, MTUNIT(bp->b_edev)); 14260 if (un == NULL) { 14261 return (DDI_FAILURE); 14262 } 14263 14264 ST_FUNC(ST_DEVINFO, st_bigblk_xfer_done); 14265 14266 cp = (struct contig_mem *)bp->b_private; 14267 orig_bp = cp->cm_bp; /* get back the bp we have replaced */ 14268 cp->cm_bp = bp; 14269 14270 /* special handling for special I/O */ 14271 if (cp->cm_use_sbuf) { 14272 #ifndef __lock_lint 14273 ASSERT(un->un_sbuf_busy); 14274 #endif 14275 un->un_sbufp = orig_bp; 14276 cp->cm_use_sbuf = 0; 14277 } 14278 14279 orig_bp->b_resid = bp->b_resid; 14280 ioerr = geterror(bp); 14281 if (ioerr != 0) { 14282 bioerror(orig_bp, ioerr); 14283 } else if (orig_bp->b_flags & B_READ) { 14284 /* copy data back to original bp */ 14285 (void) bp_copyout(bp->b_un.b_addr, orig_bp, 0, 14286 bp->b_bcount - bp->b_resid); 14287 } 14288 14289 st_release_contig_mem(un, cp); 14290 14291 biodone(orig_bp); 14292 14293 return (DDI_SUCCESS); 14294 } 14295 14296 /* 14297 * We use this func to replace original bp that may not be able to do I/O 14298 * in big block size with one that can 14299 */ 14300 static struct buf * 14301 st_get_bigblk_bp(struct buf *bp) 14302 { 14303 struct contig_mem *cp; 14304 struct scsi_tape *un; 14305 struct buf *cont_bp; 14306 14307 un = ddi_get_soft_state(st_state, MTUNIT(bp->b_edev)); 14308 if (un == NULL) { 14309 return (bp); 14310 } 14311 14312 ST_FUNC(ST_DEVINFO, st_get_bigblk_bp); 14313 14314 /* try to get one contig_mem */ 14315 cp = st_get_contig_mem(un, bp->b_bcount, KM_SLEEP); 14316 if (!cp) { 14317 scsi_log(ST_DEVINFO, st_label, CE_WARN, 14318 "Cannot alloc contig buf for I/O for %lu blk size", 14319 bp->b_bcount); 14320 return (bp); 14321 } 14322 cont_bp = cp->cm_bp; 14323 cp->cm_bp = bp; 14324 14325 /* make sure that we "are" using un_sbufp for special I/O */ 14326 if (bp == un->un_sbufp) { 14327 #ifndef __lock_lint 14328 ASSERT(un->un_sbuf_busy); 14329 #endif 14330 un->un_sbufp = cont_bp; 14331 cp->cm_use_sbuf = 1; 14332 } 14333 14334 /* clone bp */ 14335 cont_bp->b_bcount = bp->b_bcount; 14336 cont_bp->b_resid = bp->b_resid; 14337 cont_bp->b_iodone = st_bigblk_xfer_done; 14338 cont_bp->b_file = bp->b_file; 14339 cont_bp->b_offset = bp->b_offset; 14340 cont_bp->b_dip = bp->b_dip; 14341 cont_bp->b_error = 0; 14342 cont_bp->b_proc = NULL; 14343 cont_bp->b_flags = bp->b_flags & ~(B_PAGEIO | B_PHYS | B_SHADOW); 14344 cont_bp->b_shadow = NULL; 14345 cont_bp->b_pages = NULL; 14346 cont_bp->b_edev = bp->b_edev; 14347 cont_bp->b_dev = bp->b_dev; 14348 cont_bp->b_lblkno = bp->b_lblkno; 14349 cont_bp->b_forw = bp->b_forw; 14350 cont_bp->b_back = bp->b_back; 14351 cont_bp->av_forw = bp->av_forw; 14352 cont_bp->av_back = bp->av_back; 14353 cont_bp->b_bufsize = bp->b_bufsize; 14354 14355 /* get data in original bp */ 14356 if (bp->b_flags & B_WRITE) { 14357 (void) bp_copyin(bp, cont_bp->b_un.b_addr, 0, bp->b_bcount); 14358 } 14359 14360 return (cont_bp); 14361 } 14362 #else 14363 #ifdef __lock_lint 14364 static int 14365 st_bigblk_xfer_done(struct buf *bp) 14366 { 14367 return (0); 14368 } 14369 #endif 14370 #endif 14371 14372 static const char *eof_status[] = 14373 { 14374 "NO_EOF", 14375 "EOF_PENDING", 14376 "EOF", 14377 "EOT_PENDING", 14378 "EOT", 14379 "EOM", 14380 "AFTER_EOM" 14381 }; 14382 static const char *mode[] = { 14383 "invalid", 14384 "legacy", 14385 "logical" 14386 }; 14387 14388 static void 14389 st_print_position(struct scsi_tape *un, const char *comment, tapepos_t *pos) 14390 { 14391 ST_FUNC(ST_DEVINFO, st_print_position); 14392 scsi_log(ST_DEVINFO, st_label, CE_NOTE, 14393 "%s Position data:\n", comment); 14394 scsi_log(ST_DEVINFO, st_label, CE_CONT, 14395 "Positioning mode = %s", mode[pos->pmode]); 14396 scsi_log(ST_DEVINFO, st_label, CE_CONT, 14397 "End Of File/Tape = %s", eof_status[pos->eof]); 14398 scsi_log(ST_DEVINFO, st_label, CE_CONT, 14399 "File Number = 0x%x", pos->fileno); 14400 scsi_log(ST_DEVINFO, st_label, CE_CONT, 14401 "Block Number = 0x%x", pos->blkno); 14402 scsi_log(ST_DEVINFO, st_label, CE_CONT, 14403 "Logical Block = 0x%"PRIx64, pos->lgclblkno); 14404 scsi_log(ST_DEVINFO, st_label, CE_CONT, 14405 "Partition Number = 0x%x", pos->partition); 14406 } 14407