1 /* $OpenBSD: ch.c,v 1.64 2019/12/06 16:57:24 krw Exp $ */ 2 /* $NetBSD: ch.c,v 1.26 1997/02/21 22:06:52 thorpej Exp $ */ 3 4 /* 5 * Copyright (c) 1996, 1997 Jason R. Thorpe <thorpej@and.com> 6 * All rights reserved. 7 * 8 * Partially based on an autochanger driver written by Stefan Grefen 9 * and on an autochanger driver written by the Systems Programming Group 10 * at the University of Utah Computer Science Department. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgements: 22 * This product includes software developed by Jason R. Thorpe 23 * for And Communications, http://www.and.com/ 24 * 4. The name of the author may not be used to endorse or promote products 25 * derived from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 28 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 29 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 30 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 31 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 32 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 33 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 34 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 35 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 * SUCH DAMAGE. 38 */ 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/errno.h> 43 #include <sys/ioctl.h> 44 #include <sys/chio.h> 45 #include <sys/device.h> 46 #include <sys/malloc.h> 47 #include <sys/pool.h> 48 #include <sys/conf.h> 49 #include <sys/fcntl.h> 50 51 #include <scsi/scsi_all.h> 52 #include <scsi/scsi_changer.h> 53 #include <scsi/scsiconf.h> 54 55 #define CHRETRIES 2 56 #define CHUNIT(x) (minor((x))) 57 58 struct ch_softc { 59 struct device sc_dev; /* generic device info */ 60 struct scsi_link *sc_link; /* link in the SCSI bus */ 61 62 int sc_picker; /* current picker */ 63 64 /* 65 * The following information is obtained from the 66 * element address assignment page. 67 */ 68 int sc_firsts[4]; /* firsts, indexed by CHET_* */ 69 int sc_counts[4]; /* counts, indexed by CHET_* */ 70 71 /* 72 * The following mask defines the legal combinations 73 * of elements for the MOVE MEDIUM command. 74 */ 75 u_int8_t sc_movemask[4]; 76 77 /* 78 * As above, but for EXCHANGE MEDIUM. 79 */ 80 u_int8_t sc_exchangemask[4]; 81 82 int flags; /* misc. info */ 83 84 /* 85 * Quirks; see below. 86 */ 87 int sc_settledelay; /* delay for settle */ 88 89 }; 90 91 /* sc_flags */ 92 #define CHF_ROTATE 0x01 /* picker can rotate */ 93 94 /* Autoconfiguration glue */ 95 int chmatch(struct device *, void *, void *); 96 void chattach(struct device *, struct device *, void *); 97 98 struct cfattach ch_ca = { 99 sizeof(struct ch_softc), chmatch, chattach 100 }; 101 102 struct cfdriver ch_cd = { 103 NULL, "ch", DV_DULL 104 }; 105 106 const struct scsi_inquiry_pattern ch_patterns[] = { 107 {T_CHANGER, T_REMOV, 108 "", "", ""}, 109 }; 110 111 int ch_move(struct ch_softc *, struct changer_move *); 112 int ch_exchange(struct ch_softc *, struct changer_exchange *); 113 int ch_position(struct ch_softc *, struct changer_position *); 114 int ch_usergetelemstatus(struct ch_softc *, 115 struct changer_element_status_request *); 116 int ch_getelemstatus(struct ch_softc *, int, int, caddr_t, size_t, int); 117 int ch_get_params(struct ch_softc *, int); 118 int ch_interpret_sense(struct scsi_xfer *xs); 119 void ch_get_quirks(struct ch_softc *, struct scsi_inquiry_data *); 120 121 /* 122 * SCSI changer quirks. 123 */ 124 struct chquirk { 125 struct scsi_inquiry_pattern cq_match; /* device id pattern */ 126 int cq_settledelay; /* settle delay, in seconds */ 127 }; 128 129 struct chquirk chquirks[] = { 130 {{T_CHANGER, T_REMOV, 131 "SPECTRA", "9000", "0200"}, 132 75}, 133 }; 134 135 int 136 chmatch(struct device *parent, void *match, void *aux) 137 { 138 struct scsi_attach_args *sa = aux; 139 int priority; 140 141 (void)scsi_inqmatch(sa->sa_inqbuf, 142 ch_patterns, nitems(ch_patterns), 143 sizeof(ch_patterns[0]), &priority); 144 145 return priority; 146 } 147 148 void 149 chattach(struct device *parent, struct device *self, void *aux) 150 { 151 struct ch_softc *sc = (struct ch_softc *)self; 152 struct scsi_attach_args *sa = aux; 153 struct scsi_link *link = sa->sa_sc_link; 154 155 /* Glue into the SCSI bus */ 156 sc->sc_link = link; 157 link->interpret_sense = ch_interpret_sense; 158 link->device_softc = sc; 159 link->openings = 1; 160 161 printf("\n"); 162 163 /* 164 * Store our our device's quirks. 165 */ 166 ch_get_quirks(sc, sa->sa_inqbuf); 167 168 } 169 170 int 171 chopen(dev_t dev, int flags, int fmt, struct proc *p) 172 { 173 struct ch_softc *sc; 174 int oldcounts[4]; 175 int i, unit, error = 0; 176 177 unit = CHUNIT(dev); 178 if ((unit >= ch_cd.cd_ndevs) || 179 ((sc = ch_cd.cd_devs[unit]) == NULL)) 180 return ENXIO; 181 182 /* 183 * Only allow one open at a time. 184 */ 185 if (ISSET(sc->sc_link->flags, SDEV_OPEN)) 186 return EBUSY; 187 188 SET(sc->sc_link->flags, SDEV_OPEN); 189 190 /* 191 * Absorb any unit attention errors. We must notice 192 * "Not ready" errors as a changer will report "In the 193 * process of getting ready" any time it must rescan 194 * itself to determine the state of the changer. 195 */ 196 error = scsi_test_unit_ready(sc->sc_link, TEST_READY_RETRIES, 197 SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE); 198 if (error) 199 goto bad; 200 201 /* 202 * Get information about the device. Save old information 203 * so we can decide whether to be verbose about new parameters. 204 */ 205 for (i = 0; i < 4; i++) { 206 oldcounts[i] = sc->sc_counts[i]; 207 } 208 error = ch_get_params(sc, scsi_autoconf); 209 if (error) 210 goto bad; 211 212 for (i = 0; i < 4; i++) { 213 if (oldcounts[i] != sc->sc_counts[i]) { 214 break; 215 } 216 } 217 if (i < 4) { 218 #ifdef CHANGER_DEBUG 219 #define PLURAL(c) (c) == 1 ? "" : "s" 220 printf("%s: %d slot%s, %d drive%s, %d picker%s, %d portal%s\n", 221 sc->sc_dev.dv_xname, 222 sc->sc_counts[CHET_ST], PLURAL(sc->sc_counts[CHET_ST]), 223 sc->sc_counts[CHET_DT], PLURAL(sc->sc_counts[CHET_DT]), 224 sc->sc_counts[CHET_MT], PLURAL(sc->sc_counts[CHET_MT]), 225 sc->sc_counts[CHET_IE], PLURAL(sc->sc_counts[CHET_IE])); 226 #undef PLURAL 227 printf("%s: move mask: 0x%x 0x%x 0x%x 0x%x\n", 228 sc->sc_dev.dv_xname, 229 sc->sc_movemask[CHET_MT], sc->sc_movemask[CHET_ST], 230 sc->sc_movemask[CHET_IE], sc->sc_movemask[CHET_DT]); 231 printf("%s: exchange mask: 0x%x 0x%x 0x%x 0x%x\n", 232 sc->sc_dev.dv_xname, 233 sc->sc_exchangemask[CHET_MT], sc->sc_exchangemask[CHET_ST], 234 sc->sc_exchangemask[CHET_IE], sc->sc_exchangemask[CHET_DT]); 235 #endif /* CHANGER_DEBUG */ 236 } 237 238 /* Default the current picker. */ 239 sc->sc_picker = sc->sc_firsts[CHET_MT]; 240 241 return 0; 242 243 bad: 244 CLR(sc->sc_link->flags, SDEV_OPEN); 245 return error; 246 } 247 248 int 249 chclose(dev_t dev, int flags, int fmt, struct proc *p) 250 { 251 struct ch_softc *sc = ch_cd.cd_devs[CHUNIT(dev)]; 252 253 CLR(sc->sc_link->flags, SDEV_OPEN); 254 return 0; 255 } 256 257 int 258 chioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p) 259 { 260 struct ch_softc *sc = ch_cd.cd_devs[CHUNIT(dev)]; 261 int error = 0; 262 263 /* 264 * If this command can change the device's state, we must 265 * have the device open for writing. 266 */ 267 switch (cmd) { 268 case CHIOGPICKER: 269 case CHIOGPARAMS: 270 case CHIOGSTATUS: 271 break; 272 273 default: 274 if (!ISSET(flags, FWRITE)) 275 return EBADF; 276 } 277 278 switch (cmd) { 279 case CHIOMOVE: 280 error = ch_move(sc, (struct changer_move *)data); 281 break; 282 283 case CHIOEXCHANGE: 284 error = ch_exchange(sc, (struct changer_exchange *)data); 285 break; 286 287 case CHIOPOSITION: 288 error = ch_position(sc, (struct changer_position *)data); 289 break; 290 291 case CHIOGPICKER: 292 *(int *)data = sc->sc_picker - sc->sc_firsts[CHET_MT]; 293 break; 294 295 case CHIOSPICKER: { 296 int new_picker = *(int *)data; 297 298 if (new_picker > (sc->sc_counts[CHET_MT] - 1)) 299 return EINVAL; 300 sc->sc_picker = sc->sc_firsts[CHET_MT] + new_picker; 301 break; } 302 303 case CHIOGPARAMS: { 304 struct changer_params *cp = (struct changer_params *)data; 305 306 cp->cp_curpicker = sc->sc_picker - sc->sc_firsts[CHET_MT]; 307 cp->cp_npickers = sc->sc_counts[CHET_MT]; 308 cp->cp_nslots = sc->sc_counts[CHET_ST]; 309 cp->cp_nportals = sc->sc_counts[CHET_IE]; 310 cp->cp_ndrives = sc->sc_counts[CHET_DT]; 311 break; } 312 313 case CHIOGSTATUS: { 314 struct changer_element_status_request *cesr = 315 (struct changer_element_status_request *)data; 316 317 error = ch_usergetelemstatus(sc, cesr); 318 break; } 319 320 /* Implement prevent/allow? */ 321 322 default: 323 error = scsi_do_ioctl(sc->sc_link, cmd, data, flags); 324 break; 325 } 326 327 return error; 328 } 329 330 int 331 ch_move(struct ch_softc *sc, struct changer_move *cm) 332 { 333 struct scsi_move_medium *cmd; 334 struct scsi_xfer *xs; 335 int error; 336 u_int16_t fromelem, toelem; 337 338 /* 339 * Check arguments. 340 */ 341 if ((cm->cm_fromtype > CHET_DT) || (cm->cm_totype > CHET_DT)) 342 return EINVAL; 343 if ((cm->cm_fromunit > (sc->sc_counts[cm->cm_fromtype] - 1)) || 344 (cm->cm_tounit > (sc->sc_counts[cm->cm_totype] - 1))) 345 return ENODEV; 346 347 /* 348 * Check the request against the changer's capabilities. 349 */ 350 if ((sc->sc_movemask[cm->cm_fromtype] & (1 << cm->cm_totype)) == 0) 351 return EINVAL; 352 353 /* 354 * Calculate the source and destination elements. 355 */ 356 fromelem = sc->sc_firsts[cm->cm_fromtype] + cm->cm_fromunit; 357 toelem = sc->sc_firsts[cm->cm_totype] + cm->cm_tounit; 358 359 /* 360 * Build the SCSI command. 361 */ 362 xs = scsi_xs_get(sc->sc_link, 0); 363 if (xs == NULL) 364 return ENOMEM; 365 xs->cmdlen = sizeof(*cmd); 366 xs->retries = CHRETRIES; 367 xs->timeout = 100000; 368 369 cmd = (struct scsi_move_medium *)xs->cmd; 370 cmd->opcode = MOVE_MEDIUM; 371 _lto2b(sc->sc_picker, cmd->tea); 372 _lto2b(fromelem, cmd->src); 373 _lto2b(toelem, cmd->dst); 374 if (ISSET(cm->cm_flags, CM_INVERT)) 375 SET(cmd->flags, MOVE_MEDIUM_INVERT); 376 377 error = scsi_xs_sync(xs); 378 scsi_xs_put(xs); 379 380 return error; 381 } 382 383 int 384 ch_exchange(struct ch_softc *sc, struct changer_exchange *ce) 385 { 386 struct scsi_exchange_medium *cmd; 387 struct scsi_xfer *xs; 388 int error; 389 u_int16_t src, dst1, dst2; 390 391 /* 392 * Check arguments. 393 */ 394 if ((ce->ce_srctype > CHET_DT) || (ce->ce_fdsttype > CHET_DT) || 395 (ce->ce_sdsttype > CHET_DT)) 396 return EINVAL; 397 if ((ce->ce_srcunit > (sc->sc_counts[ce->ce_srctype] - 1)) || 398 (ce->ce_fdstunit > (sc->sc_counts[ce->ce_fdsttype] - 1)) || 399 (ce->ce_sdstunit > (sc->sc_counts[ce->ce_sdsttype] - 1))) 400 return ENODEV; 401 402 /* 403 * Check the request against the changer's capabilities. 404 */ 405 if (((sc->sc_exchangemask[ce->ce_srctype] & 406 (1 << ce->ce_fdsttype)) == 0) || 407 ((sc->sc_exchangemask[ce->ce_fdsttype] & 408 (1 << ce->ce_sdsttype)) == 0)) 409 return EINVAL; 410 411 /* 412 * Calculate the source and destination elements. 413 */ 414 src = sc->sc_firsts[ce->ce_srctype] + ce->ce_srcunit; 415 dst1 = sc->sc_firsts[ce->ce_fdsttype] + ce->ce_fdstunit; 416 dst2 = sc->sc_firsts[ce->ce_sdsttype] + ce->ce_sdstunit; 417 418 /* 419 * Build the SCSI command. 420 */ 421 xs = scsi_xs_get(sc->sc_link, 0); 422 if (xs == NULL) 423 return ENOMEM; 424 xs->cmdlen = sizeof(*cmd); 425 xs->retries = CHRETRIES; 426 xs->timeout = 100000; 427 428 cmd = (struct scsi_exchange_medium *)xs->cmd; 429 cmd->opcode = EXCHANGE_MEDIUM; 430 _lto2b(sc->sc_picker, cmd->tea); 431 _lto2b(src, cmd->src); 432 _lto2b(dst1, cmd->fdst); 433 _lto2b(dst2, cmd->sdst); 434 if (ISSET(ce->ce_flags, CE_INVERT1)) 435 SET(cmd->flags, EXCHANGE_MEDIUM_INV1); 436 if (ISSET(ce->ce_flags, CE_INVERT2)) 437 SET(cmd->flags, EXCHANGE_MEDIUM_INV2); 438 439 error = scsi_xs_sync(xs); 440 scsi_xs_put(xs); 441 442 return error; 443 } 444 445 int 446 ch_position(struct ch_softc *sc, struct changer_position *cp) 447 { 448 struct scsi_position_to_element *cmd; 449 struct scsi_xfer *xs; 450 int error; 451 u_int16_t dst; 452 453 /* 454 * Check arguments. 455 */ 456 if (cp->cp_type > CHET_DT) 457 return EINVAL; 458 if (cp->cp_unit > (sc->sc_counts[cp->cp_type] - 1)) 459 return ENODEV; 460 461 /* 462 * Calculate the destination element. 463 */ 464 dst = sc->sc_firsts[cp->cp_type] + cp->cp_unit; 465 466 /* 467 * Build the SCSI command. 468 */ 469 xs = scsi_xs_get(sc->sc_link, 0); 470 if (xs == NULL) 471 return ENOMEM; 472 xs->cmdlen = sizeof(*cmd); 473 xs->retries = CHRETRIES; 474 xs->timeout = 100000; 475 476 cmd = (struct scsi_position_to_element *)xs->cmd; 477 cmd->opcode = POSITION_TO_ELEMENT; 478 _lto2b(sc->sc_picker, cmd->tea); 479 _lto2b(dst, cmd->dst); 480 if (ISSET(cp->cp_flags, CP_INVERT)) 481 SET(cmd->flags, POSITION_TO_ELEMENT_INVERT); 482 483 error = scsi_xs_sync(xs); 484 scsi_xs_put(xs); 485 486 return error; 487 } 488 489 /* 490 * Copy a volume tag to a volume_tag struct, converting SCSI byte order 491 * to host native byte order in the volume serial number. The volume 492 * label as returned by the changer is transferred to user mode as 493 * nul-terminated string. Volume labels are truncated at the first 494 * space, as suggested by SCSI-2. 495 */ 496 static void 497 copy_voltag(struct changer_voltag *uvoltag, struct volume_tag *voltag) 498 { 499 int i; 500 501 for (i=0; i<CH_VOLTAG_MAXLEN; i++) { 502 char c = voltag->vif[i]; 503 if (c && c != ' ') 504 uvoltag->cv_volid[i] = c; 505 else 506 break; 507 } 508 uvoltag->cv_volid[i] = '\0'; 509 uvoltag->cv_serial = _2btol(voltag->vsn); 510 } 511 512 /* 513 * Copy an an element status descriptor to a user-mode 514 * changer_element_status structure. 515 */ 516 static void 517 copy_element_status(int flags, struct read_element_status_descriptor *desc, 518 struct changer_element_status *ces) 519 { 520 ces->ces_flags = desc->flags1; 521 522 if (ISSET(flags, READ_ELEMENT_STATUS_PVOLTAG)) 523 copy_voltag(&ces->ces_pvoltag, &desc->pvoltag); 524 if (ISSET(flags, READ_ELEMENT_STATUS_AVOLTAG)) 525 copy_voltag(&ces->ces_avoltag, &desc->avoltag); 526 } 527 528 /* 529 * Perform a READ ELEMENT STATUS on behalf of the user, and return to 530 * the user only the data the user is interested in (i.e. an array of 531 * changer_element_status structures) 532 */ 533 int 534 ch_usergetelemstatus(struct ch_softc *sc, 535 struct changer_element_status_request *cesr) 536 { 537 struct changer_element_status *user_data = NULL; 538 struct read_element_status_header *st_hdr; 539 struct read_element_status_page_header *pg_hdr; 540 caddr_t desc; 541 caddr_t data = NULL; 542 size_t size, desclen, udsize; 543 int avail, chet, i, want_voltags; 544 int error = 0; 545 546 chet = cesr->cesr_type; 547 want_voltags = (cesr->cesr_flags & CESR_VOLTAGS) ? 1 : 0; 548 549 /* 550 * If there are no elements of the requested type in the changer, 551 * the request is invalid. 552 */ 553 if (sc->sc_counts[chet] == 0) 554 return EINVAL; 555 556 /* 557 * Request one descriptor for the given element type. This 558 * is used to determine the size of the descriptor so that 559 * we can allocate enough storage for all of them. We assume 560 * that the first one can fit into 1k. 561 */ 562 size = 1024; 563 data = dma_alloc(size, PR_WAITOK); 564 error = ch_getelemstatus(sc, sc->sc_firsts[chet], 1, data, size, 565 want_voltags); 566 if (error) 567 goto done; 568 569 st_hdr = (struct read_element_status_header *)data; 570 pg_hdr = (struct read_element_status_page_header *) (st_hdr + 1); 571 desclen = _2btol(pg_hdr->edl); 572 573 dma_free(data, size); 574 575 /* 576 * Reallocate storage for descriptors and get them from the 577 * device. 578 */ 579 size = sizeof(struct read_element_status_header) + 580 sizeof(struct read_element_status_page_header) + 581 (desclen * sc->sc_counts[chet]); 582 data = dma_alloc(size, PR_WAITOK); 583 error = ch_getelemstatus(sc, sc->sc_firsts[chet], 584 sc->sc_counts[chet], data, size, want_voltags); 585 if (error) 586 goto done; 587 588 /* 589 * Fill in the user status array. 590 */ 591 st_hdr = (struct read_element_status_header *)data; 592 pg_hdr = (struct read_element_status_page_header *) (st_hdr + 1); 593 594 avail = _2btol(st_hdr->count); 595 if (avail != sc->sc_counts[chet]) { 596 error = EINVAL; 597 goto done; 598 } 599 600 user_data = mallocarray(avail, sizeof(struct changer_element_status), 601 M_DEVBUF, M_WAITOK | M_ZERO); 602 udsize = avail * sizeof(struct changer_element_status); 603 604 desc = (caddr_t)(pg_hdr + 1); 605 for (i = 0; i < avail; ++i) { 606 struct changer_element_status *ces = &(user_data[i]); 607 copy_element_status(pg_hdr->flags, 608 (struct read_element_status_descriptor *)desc, ces); 609 desc += desclen; 610 } 611 612 /* Copy array out to userspace. */ 613 error = copyout(user_data, cesr->cesr_data, udsize); 614 615 done: 616 if (data != NULL) 617 dma_free(data, size); 618 if (user_data != NULL) 619 free(user_data, M_DEVBUF, udsize); 620 return error; 621 } 622 623 int 624 ch_getelemstatus(struct ch_softc *sc, int first, int count, caddr_t data, 625 size_t datalen, int voltag) 626 { 627 struct scsi_read_element_status *cmd; 628 struct scsi_xfer *xs; 629 int error; 630 631 /* 632 * Build SCSI command. 633 */ 634 xs = scsi_xs_get(sc->sc_link, SCSI_DATA_IN); 635 if (xs == NULL) 636 return ENOMEM; 637 xs->cmdlen = sizeof(*cmd); 638 xs->data = data; 639 xs->datalen = datalen; 640 xs->retries = CHRETRIES; 641 xs->timeout = 100000; 642 643 cmd = (struct scsi_read_element_status *)xs->cmd; 644 cmd->opcode = READ_ELEMENT_STATUS; 645 _lto2b(first, cmd->sea); 646 _lto2b(count, cmd->count); 647 _lto3b(datalen, cmd->len); 648 if (voltag) 649 SET(cmd->byte2, READ_ELEMENT_STATUS_VOLTAG); 650 651 error = scsi_xs_sync(xs); 652 scsi_xs_put(xs); 653 654 return error; 655 } 656 657 /* 658 * Ask the device about itself and fill in the parameters in our 659 * softc. 660 */ 661 int 662 ch_get_params(struct ch_softc *sc, int flags) 663 { 664 union scsi_mode_sense_buf *data; 665 struct page_element_address_assignment *ea; 666 struct page_device_capabilities *cap; 667 u_int8_t *moves, *exchanges; 668 int big, error, from; 669 670 data = dma_alloc(sizeof(*data), PR_NOWAIT); 671 if (data == NULL) 672 return ENOMEM; 673 674 /* 675 * Grab info from the element address assignment page (0x1d). 676 */ 677 error = scsi_do_mode_sense(sc->sc_link, EA_PAGE, data, 678 (void **)&ea, sizeof(*ea), flags, &big); 679 if (error == 0 && ea == NULL) 680 error = EIO; 681 if (error != 0) { 682 #ifdef CHANGER_DEBUG 683 printf("%s: could not sense element address page\n", 684 sc->sc_dev.dv_xname); 685 #endif /* CHANGER_DEBUG */ 686 dma_free(data, sizeof(*data)); 687 return error; 688 } 689 690 sc->sc_firsts[CHET_MT] = _2btol(ea->mtea); 691 sc->sc_counts[CHET_MT] = _2btol(ea->nmte); 692 sc->sc_firsts[CHET_ST] = _2btol(ea->fsea); 693 sc->sc_counts[CHET_ST] = _2btol(ea->nse); 694 sc->sc_firsts[CHET_IE] = _2btol(ea->fieea); 695 sc->sc_counts[CHET_IE] = _2btol(ea->niee); 696 sc->sc_firsts[CHET_DT] = _2btol(ea->fdtea); 697 sc->sc_counts[CHET_DT] = _2btol(ea->ndte); 698 699 /* XXX Ask for transport geometry page. */ 700 701 /* 702 * Grab info from the capabilities page (0x1f). 703 */ 704 error = scsi_do_mode_sense(sc->sc_link, CAP_PAGE, data, 705 (void **)&cap, sizeof(*cap), flags, &big); 706 if (error == 0 && cap == NULL) 707 error = EIO; 708 if (error != 0) { 709 #ifdef CHANGER_DEBUG 710 printf("%s: could not sense capabilities page\n", 711 sc->sc_dev.dv_xname); 712 #endif /* CHANGER_DEBUG */ 713 dma_free(data, sizeof(*data)); 714 return error; 715 } 716 717 bzero(sc->sc_movemask, sizeof(sc->sc_movemask)); 718 bzero(sc->sc_exchangemask, sizeof(sc->sc_exchangemask)); 719 moves = &cap->move_from_mt; 720 exchanges = &cap->exchange_with_mt; 721 for (from = CHET_MT; from <= CHET_DT; ++from) { 722 sc->sc_movemask[from] = moves[from]; 723 sc->sc_exchangemask[from] = exchanges[from]; 724 } 725 726 SET(sc->sc_link->flags, SDEV_MEDIA_LOADED); 727 dma_free(data, sizeof(*data)); 728 return 0; 729 } 730 731 void 732 ch_get_quirks(struct ch_softc *sc, struct scsi_inquiry_data *inqbuf) 733 { 734 const struct chquirk *match; 735 int priority; 736 737 sc->sc_settledelay = 0; 738 739 match = (const struct chquirk *)scsi_inqmatch(inqbuf, 740 (caddr_t)chquirks, 741 sizeof(chquirks) / sizeof(chquirks[0]), 742 sizeof(chquirks[0]), &priority); 743 if (priority != 0) { 744 sc->sc_settledelay = match->cq_settledelay; 745 } 746 } 747 748 /* 749 * Look at the returned sense and act on the error and detirmine 750 * The unix error number to pass back... (0 = report no error) 751 * (-1 = continue processing) 752 */ 753 int 754 ch_interpret_sense(struct scsi_xfer *xs) 755 { 756 struct scsi_sense_data *sense = &xs->sense; 757 struct scsi_link *link = xs->sc_link; 758 u_int8_t serr, skey; 759 760 serr = sense->error_code & SSD_ERRCODE; 761 skey = sense->flags & SSD_KEY; 762 763 if (!ISSET(link->flags, SDEV_OPEN) || 764 (serr != SSD_ERRCODE_CURRENT && serr != SSD_ERRCODE_DEFERRED)) 765 return scsi_interpret_sense(xs); 766 767 switch (skey) { 768 769 /* 770 * We do custom processing in ch for the unit becoming ready 771 * case. in this case we do not allow xs->retries to be 772 * decremented only on the "Unit Becoming Ready" case. This is 773 * because tape changers report "Unit Becoming Ready" when they 774 * rescan their state (i.e. when the door got opened) and can 775 * take a long time for large units. Rather than having a 776 * massive timeout for all operations (which would cause other 777 * problems) we allow changers to wait (but be interruptable 778 * with Ctrl-C) forever as long as they are reporting that they 779 * are becoming ready. all other cases are handled as per the 780 * default. 781 */ 782 case SKEY_NOT_READY: 783 if (ISSET(xs->flags, SCSI_IGNORE_NOT_READY)) 784 return 0; 785 switch (ASC_ASCQ(sense)) { 786 case SENSE_NOT_READY_BECOMING_READY: 787 SC_DEBUG(link, SDEV_DB1, ("not ready: busy (%#x)\n", 788 sense->add_sense_code_qual)); 789 /* don't count this as a retry */ 790 xs->retries++; 791 return scsi_delay(xs, 1); 792 default: 793 return scsi_interpret_sense(xs); 794 } 795 default: 796 return scsi_interpret_sense(xs); 797 } 798 } 799