1 /* $NetBSD: ch.c,v 1.92 2017/10/25 08:12:39 maya Exp $ */ 2 3 /*- 4 * Copyright (c) 1996, 1997, 1998, 1999, 2004 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 9 * NASA Ames Research Center. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include <sys/cdefs.h> 34 __KERNEL_RCSID(0, "$NetBSD: ch.c,v 1.92 2017/10/25 08:12:39 maya Exp $"); 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/kernel.h> 39 #include <sys/errno.h> 40 #include <sys/ioctl.h> 41 #include <sys/buf.h> 42 #include <sys/proc.h> 43 #include <sys/chio.h> 44 #include <sys/device.h> 45 #include <sys/malloc.h> 46 #include <sys/conf.h> 47 #include <sys/fcntl.h> 48 #include <sys/vnode.h> 49 #include <sys/time.h> 50 #include <sys/select.h> 51 #include <sys/poll.h> 52 53 #include <dev/scsipi/scsipi_all.h> 54 #include <dev/scsipi/scsi_all.h> 55 #include <dev/scsipi/scsi_changer.h> 56 #include <dev/scsipi/scsiconf.h> 57 58 #define CHRETRIES 2 59 #define CHTIMEOUT (5 * 60 * 1000) 60 61 #define CHUNIT(x) (minor((x))) 62 63 struct ch_softc { 64 device_t sc_dev; /* generic device info */ 65 struct scsipi_periph *sc_periph;/* our periph data */ 66 67 u_int sc_events; /* event bitmask */ 68 struct selinfo sc_selq; /* select/poll queue for events */ 69 70 int sc_flags; /* misc. info */ 71 72 int sc_picker; /* current picker */ 73 74 /* 75 * The following information is obtained from the 76 * element address assignment page. 77 */ 78 int sc_firsts[4]; /* firsts, indexed by CHET_* */ 79 int sc_counts[4]; /* counts, indexed by CHET_* */ 80 81 /* 82 * The following mask defines the legal combinations 83 * of elements for the MOVE MEDIUM command. 84 */ 85 u_int8_t sc_movemask[4]; 86 87 /* 88 * As above, but for EXCHANGE MEDIUM. 89 */ 90 u_int8_t sc_exchangemask[4]; 91 92 /* 93 * Quirks; see below. 94 */ 95 int sc_settledelay; /* delay for settle */ 96 97 }; 98 99 /* sc_flags */ 100 #define CHF_ROTATE 0x01 /* picker can rotate */ 101 102 /* Autoconfiguration glue */ 103 static int chmatch(device_t, cfdata_t, void *); 104 static void chattach(device_t, device_t, void *); 105 106 CFATTACH_DECL_NEW(ch, sizeof(struct ch_softc), 107 chmatch, chattach, NULL, NULL); 108 109 extern struct cfdriver ch_cd; 110 111 static struct scsipi_inquiry_pattern ch_patterns[] = { 112 {T_CHANGER, T_REMOV, 113 "", "", ""}, 114 }; 115 116 static dev_type_open(chopen); 117 static dev_type_close(chclose); 118 static dev_type_read(chread); 119 static dev_type_ioctl(chioctl); 120 static dev_type_poll(chpoll); 121 static dev_type_kqfilter(chkqfilter); 122 123 const struct cdevsw ch_cdevsw = { 124 .d_open = chopen, 125 .d_close = chclose, 126 .d_read = chread, 127 .d_write = nowrite, 128 .d_ioctl = chioctl, 129 .d_stop = nostop, 130 .d_tty = notty, 131 .d_poll = chpoll, 132 .d_mmap = nommap, 133 .d_kqfilter = chkqfilter, 134 .d_discard = nodiscard, 135 .d_flag = D_OTHER | D_MPSAFE 136 }; 137 138 /* SCSI glue */ 139 static int ch_interpret_sense(struct scsipi_xfer *); 140 141 static const struct scsipi_periphsw ch_switch = { 142 ch_interpret_sense, /* check our error handler first */ 143 NULL, /* no queue; our commands are synchronous */ 144 NULL, /* have no async handler */ 145 NULL, /* nothing to be done when xfer is done */ 146 }; 147 148 static int ch_move(struct ch_softc *, struct changer_move_request *); 149 static int ch_exchange(struct ch_softc *, 150 struct changer_exchange_request *); 151 static int ch_position(struct ch_softc *, 152 struct changer_position_request *); 153 static int ch_ielem(struct ch_softc *); 154 static int ch_ousergetelemstatus(struct ch_softc *, int, u_int8_t *); 155 static int ch_usergetelemstatus(struct ch_softc *, 156 struct changer_element_status_request *); 157 static int ch_getelemstatus(struct ch_softc *, int, int, void *, 158 size_t, int, int); 159 static int ch_setvoltag(struct ch_softc *, 160 struct changer_set_voltag_request *); 161 static int ch_get_params(struct ch_softc *, int); 162 static void ch_get_quirks(struct ch_softc *, 163 struct scsipi_inquiry_pattern *); 164 static void ch_event(struct ch_softc *, u_int); 165 static int ch_map_element(struct ch_softc *, u_int16_t, int *, int *); 166 167 static void ch_voltag_convert_in(const struct changer_volume_tag *, 168 struct changer_voltag *); 169 static int ch_voltag_convert_out(const struct changer_voltag *, 170 struct changer_volume_tag *); 171 172 /* 173 * SCSI changer quirks. 174 */ 175 struct chquirk { 176 struct scsipi_inquiry_pattern cq_match; /* device id pattern */ 177 int cq_settledelay; /* settle delay, in seconds */ 178 }; 179 180 static const struct chquirk chquirks[] = { 181 {{T_CHANGER, T_REMOV, 182 "SPECTRA", "9000", "0200"}, 183 75}, 184 }; 185 186 static int 187 chmatch(device_t parent, cfdata_t match, 188 void *aux) 189 { 190 struct scsipibus_attach_args *sa = aux; 191 int priority; 192 193 (void)scsipi_inqmatch(&sa->sa_inqbuf, 194 (void *)ch_patterns, sizeof(ch_patterns) / sizeof(ch_patterns[0]), 195 sizeof(ch_patterns[0]), &priority); 196 197 return (priority); 198 } 199 200 static void 201 chattach(device_t parent, device_t self, void *aux) 202 { 203 struct ch_softc *sc = device_private(self); 204 struct scsipibus_attach_args *sa = aux; 205 struct scsipi_periph *periph = sa->sa_periph; 206 207 sc->sc_dev = self; 208 selinit(&sc->sc_selq); 209 210 /* Glue into the SCSI bus */ 211 sc->sc_periph = periph; 212 periph->periph_dev = sc->sc_dev; 213 periph->periph_switch = &ch_switch; 214 215 printf("\n"); 216 217 /* 218 * Find out our device's quirks. 219 */ 220 ch_get_quirks(sc, &sa->sa_inqbuf); 221 222 /* 223 * Some changers require a long time to settle out, to do 224 * tape inventory, for instance. 225 */ 226 if (sc->sc_settledelay) { 227 printf("%s: waiting %d seconds for changer to settle...\n", 228 device_xname(sc->sc_dev), sc->sc_settledelay); 229 delay(1000000 * sc->sc_settledelay); 230 } 231 232 /* 233 * Get information about the device. Note we can't use 234 * interrupts yet. 235 */ 236 if (ch_get_params(sc, XS_CTL_DISCOVERY|XS_CTL_IGNORE_MEDIA_CHANGE)) 237 printf("%s: offline\n", device_xname(sc->sc_dev)); 238 else { 239 #define PLURAL(c) (c) == 1 ? "" : "s" 240 printf("%s: %d slot%s, %d drive%s, %d picker%s, %d portal%s\n", 241 device_xname(sc->sc_dev), 242 sc->sc_counts[CHET_ST], PLURAL(sc->sc_counts[CHET_ST]), 243 sc->sc_counts[CHET_DT], PLURAL(sc->sc_counts[CHET_DT]), 244 sc->sc_counts[CHET_MT], PLURAL(sc->sc_counts[CHET_MT]), 245 sc->sc_counts[CHET_IE], PLURAL(sc->sc_counts[CHET_IE])); 246 #undef PLURAL 247 #ifdef CHANGER_DEBUG 248 printf("%s: move mask: 0x%x 0x%x 0x%x 0x%x\n", 249 device_xname(sc->sc_dev), 250 sc->sc_movemask[CHET_MT], sc->sc_movemask[CHET_ST], 251 sc->sc_movemask[CHET_IE], sc->sc_movemask[CHET_DT]); 252 printf("%s: exchange mask: 0x%x 0x%x 0x%x 0x%x\n", 253 device_xname(sc->sc_dev), 254 sc->sc_exchangemask[CHET_MT], sc->sc_exchangemask[CHET_ST], 255 sc->sc_exchangemask[CHET_IE], sc->sc_exchangemask[CHET_DT]); 256 #endif /* CHANGER_DEBUG */ 257 } 258 259 /* Default the current picker. */ 260 sc->sc_picker = sc->sc_firsts[CHET_MT]; 261 } 262 263 static int 264 chopen(dev_t dev, int flags, int fmt, struct lwp *l) 265 { 266 struct ch_softc *sc; 267 struct scsipi_periph *periph; 268 struct scsipi_adapter *adapt; 269 int unit, error; 270 271 unit = CHUNIT(dev); 272 sc = device_lookup_private(&ch_cd, unit); 273 if (sc == NULL) 274 return (ENXIO); 275 276 periph = sc->sc_periph; 277 adapt = periph->periph_channel->chan_adapter; 278 279 /* 280 * Only allow one open at a time. 281 */ 282 if (periph->periph_flags & PERIPH_OPEN) 283 return (EBUSY); 284 285 if ((error = scsipi_adapter_addref(adapt)) != 0) 286 return (error); 287 288 /* 289 * Make sure the unit is on-line. If a UNIT ATTENTION 290 * occurs, we will mark that an Init-Element-Status is 291 * needed in ch_get_params(). 292 * 293 * We ignore NOT READY in case e.g a magazine isn't actually 294 * loaded into the changer or a tape isn't in the drive. 295 */ 296 error = scsipi_test_unit_ready(periph, XS_CTL_IGNORE_NOT_READY); 297 if (error) 298 goto bad; 299 300 periph->periph_flags |= PERIPH_OPEN; 301 302 /* 303 * Make sure our parameters are up to date. 304 */ 305 if ((error = ch_get_params(sc, 0)) != 0) 306 goto bad; 307 308 return (0); 309 310 bad: 311 scsipi_adapter_delref(adapt); 312 periph->periph_flags &= ~PERIPH_OPEN; 313 return (error); 314 } 315 316 static int 317 chclose(dev_t dev, int flags, int fmt, struct lwp *l) 318 { 319 struct ch_softc *sc = device_lookup_private(&ch_cd, CHUNIT(dev)); 320 struct scsipi_periph *periph = sc->sc_periph; 321 struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter; 322 323 scsipi_wait_drain(periph); 324 325 scsipi_adapter_delref(adapt); 326 327 sc->sc_events = 0; 328 329 periph->periph_flags &= ~PERIPH_OPEN; 330 return (0); 331 } 332 333 static int 334 chread(dev_t dev, struct uio *uio, int flags) 335 { 336 struct ch_softc *sc = device_lookup_private(&ch_cd, CHUNIT(dev)); 337 int error; 338 339 if (uio->uio_resid != CHANGER_EVENT_SIZE) 340 return (EINVAL); 341 342 /* 343 * Read never blocks; if there are no events pending, we just 344 * return an all-clear bitmask. 345 */ 346 error = uiomove(&sc->sc_events, CHANGER_EVENT_SIZE, uio); 347 if (error == 0) 348 sc->sc_events = 0; 349 return (error); 350 } 351 352 static int 353 chioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l) 354 { 355 struct ch_softc *sc = device_lookup_private(&ch_cd, CHUNIT(dev)); 356 int error = 0; 357 358 /* 359 * If this command can change the device's state, we must 360 * have the device open for writing. 361 */ 362 switch (cmd) { 363 case CHIOGPICKER: 364 case CHIOGPARAMS: 365 case OCHIOGSTATUS: 366 break; 367 368 default: 369 if ((flags & FWRITE) == 0) 370 return (EBADF); 371 } 372 373 switch (cmd) { 374 case CHIOMOVE: 375 error = ch_move(sc, (struct changer_move_request *)data); 376 break; 377 378 case CHIOEXCHANGE: 379 error = ch_exchange(sc, 380 (struct changer_exchange_request *)data); 381 break; 382 383 case CHIOPOSITION: 384 error = ch_position(sc, 385 (struct changer_position_request *)data); 386 break; 387 388 case CHIOGPICKER: 389 *(int *)data = sc->sc_picker - sc->sc_firsts[CHET_MT]; 390 break; 391 392 case CHIOSPICKER: 393 { 394 int new_picker = *(int *)data; 395 396 if (new_picker > (sc->sc_counts[CHET_MT] - 1)) 397 return (EINVAL); 398 sc->sc_picker = sc->sc_firsts[CHET_MT] + new_picker; 399 break; 400 } 401 402 case CHIOGPARAMS: 403 { 404 struct changer_params *cp = (struct changer_params *)data; 405 406 cp->cp_curpicker = sc->sc_picker - sc->sc_firsts[CHET_MT]; 407 cp->cp_npickers = sc->sc_counts[CHET_MT]; 408 cp->cp_nslots = sc->sc_counts[CHET_ST]; 409 cp->cp_nportals = sc->sc_counts[CHET_IE]; 410 cp->cp_ndrives = sc->sc_counts[CHET_DT]; 411 break; 412 } 413 414 case CHIOIELEM: 415 error = ch_ielem(sc); 416 if (error == 0) { 417 sc->sc_periph->periph_flags |= PERIPH_MEDIA_LOADED; 418 } 419 break; 420 421 case OCHIOGSTATUS: 422 { 423 struct ochanger_element_status_request *cesr = 424 (struct ochanger_element_status_request *)data; 425 426 error = ch_ousergetelemstatus(sc, cesr->cesr_type, 427 cesr->cesr_data); 428 break; 429 } 430 431 case CHIOGSTATUS: 432 error = ch_usergetelemstatus(sc, 433 (struct changer_element_status_request *)data); 434 break; 435 436 case CHIOSVOLTAG: 437 error = ch_setvoltag(sc, 438 (struct changer_set_voltag_request *)data); 439 break; 440 441 /* Implement prevent/allow? */ 442 443 default: 444 error = scsipi_do_ioctl(sc->sc_periph, dev, cmd, data, 445 flags, l); 446 break; 447 } 448 449 return (error); 450 } 451 452 static int 453 chpoll(dev_t dev, int events, struct lwp *l) 454 { 455 struct ch_softc *sc = device_lookup_private(&ch_cd, CHUNIT(dev)); 456 int revents; 457 458 revents = events & (POLLOUT | POLLWRNORM); 459 460 if ((events & (POLLIN | POLLRDNORM)) == 0) 461 return (revents); 462 463 if (sc->sc_events == 0) 464 revents |= events & (POLLIN | POLLRDNORM); 465 else 466 selrecord(l, &sc->sc_selq); 467 468 return (revents); 469 } 470 471 static void 472 filt_chdetach(struct knote *kn) 473 { 474 struct ch_softc *sc = kn->kn_hook; 475 476 SLIST_REMOVE(&sc->sc_selq.sel_klist, kn, knote, kn_selnext); 477 } 478 479 static int 480 filt_chread(struct knote *kn, long hint) 481 { 482 struct ch_softc *sc = kn->kn_hook; 483 484 if (sc->sc_events == 0) 485 return (0); 486 kn->kn_data = CHANGER_EVENT_SIZE; 487 return (1); 488 } 489 490 static const struct filterops chread_filtops = { 491 .f_isfd = 1, 492 .f_attach = NULL, 493 .f_detach = filt_chdetach, 494 .f_event = filt_chread, 495 }; 496 497 static const struct filterops chwrite_filtops = { 498 .f_isfd = 1, 499 .f_attach = NULL, 500 .f_detach = filt_chdetach, 501 .f_event = filt_seltrue, 502 }; 503 504 static int 505 chkqfilter(dev_t dev, struct knote *kn) 506 { 507 struct ch_softc *sc = device_lookup_private(&ch_cd, CHUNIT(dev)); 508 struct klist *klist; 509 510 switch (kn->kn_filter) { 511 case EVFILT_READ: 512 klist = &sc->sc_selq.sel_klist; 513 kn->kn_fop = &chread_filtops; 514 break; 515 516 case EVFILT_WRITE: 517 klist = &sc->sc_selq.sel_klist; 518 kn->kn_fop = &chwrite_filtops; 519 break; 520 521 default: 522 return (EINVAL); 523 } 524 525 kn->kn_hook = sc; 526 527 SLIST_INSERT_HEAD(klist, kn, kn_selnext); 528 529 return (0); 530 } 531 532 static int 533 ch_interpret_sense(struct scsipi_xfer *xs) 534 { 535 struct scsipi_periph *periph = xs->xs_periph; 536 struct scsi_sense_data *sense = &xs->sense.scsi_sense; 537 struct ch_softc *sc = device_private(periph->periph_dev); 538 u_int16_t asc_ascq; 539 540 /* 541 * If the periph is already recovering, just do the 542 * normal error recovering. 543 */ 544 if (periph->periph_flags & PERIPH_RECOVERING) 545 return (EJUSTRETURN); 546 547 /* 548 * If it isn't an extended or extended/deferred error, let 549 * the generic code handle it. 550 */ 551 if (SSD_RCODE(sense->response_code) != SSD_RCODE_CURRENT && 552 SSD_RCODE(sense->response_code) != SSD_RCODE_DEFERRED) 553 return (EJUSTRETURN); 554 555 /* 556 * We're only interested in condtions that 557 * indicate potential inventory violation. 558 * 559 * We use ASC/ASCQ codes for this. 560 */ 561 562 asc_ascq = (((u_int16_t) sense->asc) << 8) | 563 sense->ascq; 564 565 switch (asc_ascq) { 566 case 0x2800: 567 /* "Not Ready To Ready Transition (Medium May Have Changed)" */ 568 case 0x2900: 569 /* "Power On, Reset, or Bus Device Reset Occurred" */ 570 sc->sc_periph->periph_flags &= ~PERIPH_MEDIA_LOADED; 571 /* 572 * Enqueue an Element-Status-Changed event, and wake up 573 * any processes waiting for them. 574 */ 575 if ((xs->xs_control & XS_CTL_IGNORE_MEDIA_CHANGE) == 0) 576 ch_event(sc, CHEV_ELEMENT_STATUS_CHANGED); 577 break; 578 default: 579 break; 580 } 581 582 return (EJUSTRETURN); 583 } 584 585 static void 586 ch_event(struct ch_softc *sc, u_int event) 587 { 588 589 sc->sc_events |= event; 590 selnotify(&sc->sc_selq, 0, 0); 591 } 592 593 static int 594 ch_move(struct ch_softc *sc, struct changer_move_request *cm) 595 { 596 struct scsi_move_medium cmd; 597 u_int16_t fromelem, toelem; 598 599 /* 600 * Check arguments. 601 */ 602 if ((cm->cm_fromtype > CHET_DT) || (cm->cm_totype > CHET_DT)) 603 return (EINVAL); 604 if ((cm->cm_fromunit > (sc->sc_counts[cm->cm_fromtype] - 1)) || 605 (cm->cm_tounit > (sc->sc_counts[cm->cm_totype] - 1))) 606 return (ENODEV); 607 608 /* 609 * Check the request against the changer's capabilities. 610 */ 611 if ((sc->sc_movemask[cm->cm_fromtype] & (1 << cm->cm_totype)) == 0) 612 return (ENODEV); 613 614 /* 615 * Calculate the source and destination elements. 616 */ 617 fromelem = sc->sc_firsts[cm->cm_fromtype] + cm->cm_fromunit; 618 toelem = sc->sc_firsts[cm->cm_totype] + cm->cm_tounit; 619 620 /* 621 * Build the SCSI command. 622 */ 623 memset(&cmd, 0, sizeof(cmd)); 624 cmd.opcode = MOVE_MEDIUM; 625 _lto2b(sc->sc_picker, cmd.tea); 626 _lto2b(fromelem, cmd.src); 627 _lto2b(toelem, cmd.dst); 628 if (cm->cm_flags & CM_INVERT) 629 cmd.flags |= MOVE_MEDIUM_INVERT; 630 631 /* 632 * Send command to changer. 633 */ 634 return (scsipi_command(sc->sc_periph, (void *)&cmd, sizeof(cmd), 0, 0, 635 CHRETRIES, CHTIMEOUT, NULL, 0)); 636 } 637 638 static int 639 ch_exchange(struct ch_softc *sc, struct changer_exchange_request *ce) 640 { 641 struct scsi_exchange_medium cmd; 642 u_int16_t src, dst1, dst2; 643 644 /* 645 * Check arguments. 646 */ 647 if ((ce->ce_srctype > CHET_DT) || (ce->ce_fdsttype > CHET_DT) || 648 (ce->ce_sdsttype > CHET_DT)) 649 return (EINVAL); 650 if ((ce->ce_srcunit > (sc->sc_counts[ce->ce_srctype] - 1)) || 651 (ce->ce_fdstunit > (sc->sc_counts[ce->ce_fdsttype] - 1)) || 652 (ce->ce_sdstunit > (sc->sc_counts[ce->ce_sdsttype] - 1))) 653 return (ENODEV); 654 655 /* 656 * Check the request against the changer's capabilities. 657 */ 658 if (((sc->sc_exchangemask[ce->ce_srctype] & 659 (1 << ce->ce_fdsttype)) == 0) || 660 ((sc->sc_exchangemask[ce->ce_fdsttype] & 661 (1 << ce->ce_sdsttype)) == 0)) 662 return (ENODEV); 663 664 /* 665 * Calculate the source and destination elements. 666 */ 667 src = sc->sc_firsts[ce->ce_srctype] + ce->ce_srcunit; 668 dst1 = sc->sc_firsts[ce->ce_fdsttype] + ce->ce_fdstunit; 669 dst2 = sc->sc_firsts[ce->ce_sdsttype] + ce->ce_sdstunit; 670 671 /* 672 * Build the SCSI command. 673 */ 674 memset(&cmd, 0, sizeof(cmd)); 675 cmd.opcode = EXCHANGE_MEDIUM; 676 _lto2b(sc->sc_picker, cmd.tea); 677 _lto2b(src, cmd.src); 678 _lto2b(dst1, cmd.fdst); 679 _lto2b(dst2, cmd.sdst); 680 if (ce->ce_flags & CE_INVERT1) 681 cmd.flags |= EXCHANGE_MEDIUM_INV1; 682 if (ce->ce_flags & CE_INVERT2) 683 cmd.flags |= EXCHANGE_MEDIUM_INV2; 684 685 /* 686 * Send command to changer. 687 */ 688 return (scsipi_command(sc->sc_periph, (void *)&cmd, sizeof(cmd), 0, 0, 689 CHRETRIES, CHTIMEOUT, NULL, 0)); 690 } 691 692 static int 693 ch_position(struct ch_softc *sc, struct changer_position_request *cp) 694 { 695 struct scsi_position_to_element cmd; 696 u_int16_t dst; 697 698 /* 699 * Check arguments. 700 */ 701 if (cp->cp_type > CHET_DT) 702 return (EINVAL); 703 if (cp->cp_unit > (sc->sc_counts[cp->cp_type] - 1)) 704 return (ENODEV); 705 706 /* 707 * Calculate the destination element. 708 */ 709 dst = sc->sc_firsts[cp->cp_type] + cp->cp_unit; 710 711 /* 712 * Build the SCSI command. 713 */ 714 memset(&cmd, 0, sizeof(cmd)); 715 cmd.opcode = POSITION_TO_ELEMENT; 716 _lto2b(sc->sc_picker, cmd.tea); 717 _lto2b(dst, cmd.dst); 718 if (cp->cp_flags & CP_INVERT) 719 cmd.flags |= POSITION_TO_ELEMENT_INVERT; 720 721 /* 722 * Send command to changer. 723 */ 724 return (scsipi_command(sc->sc_periph, (void *)&cmd, sizeof(cmd), 0, 0, 725 CHRETRIES, CHTIMEOUT, NULL, 0)); 726 } 727 728 /* 729 * Perform a READ ELEMENT STATUS on behalf of the user, and return to 730 * the user only the data the user is interested in. This returns the 731 * old data format. 732 */ 733 static int 734 ch_ousergetelemstatus(struct ch_softc *sc, int chet, u_int8_t *uptr) 735 { 736 struct read_element_status_header *st_hdrp, st_hdr; 737 struct read_element_status_page_header *pg_hdrp; 738 struct read_element_status_descriptor *desc; 739 size_t size, desclen; 740 void *data; 741 int avail, i, error = 0; 742 u_int8_t user_data; 743 744 /* 745 * If there are no elements of the requested type in the changer, 746 * the request is invalid. 747 */ 748 if (sc->sc_counts[chet] == 0) 749 return (EINVAL); 750 751 /* 752 * Do the request the user wants, but only read the status header. 753 * This will tell us the amount of storage we must allocate in 754 * order to read all data. 755 */ 756 error = ch_getelemstatus(sc, sc->sc_firsts[chet], 757 sc->sc_counts[chet], &st_hdr, sizeof(st_hdr), 0, 0); 758 if (error) 759 return (error); 760 761 size = sizeof(struct read_element_status_header) + 762 _3btol(st_hdr.nbytes); 763 764 /* 765 * We must have at least room for the status header and 766 * one page header (since we only ask for one element type 767 * at a time). 768 */ 769 if (size < (sizeof(struct read_element_status_header) + 770 sizeof(struct read_element_status_page_header))) 771 return (EIO); 772 773 /* 774 * Allocate the storage and do the request again. 775 */ 776 data = malloc(size, M_DEVBUF, M_WAITOK); 777 error = ch_getelemstatus(sc, sc->sc_firsts[chet], 778 sc->sc_counts[chet], data, size, 0, 0); 779 if (error) 780 goto done; 781 782 st_hdrp = (struct read_element_status_header *)data; 783 pg_hdrp = (struct read_element_status_page_header *)((u_long)st_hdrp + 784 sizeof(struct read_element_status_header)); 785 desclen = _2btol(pg_hdrp->edl); 786 787 /* 788 * Fill in the user status array. 789 */ 790 avail = _2btol(st_hdrp->count); 791 792 if (avail != sc->sc_counts[chet]) 793 printf("%s: warning, READ ELEMENT STATUS avail != count\n", 794 device_xname(sc->sc_dev)); 795 796 desc = (struct read_element_status_descriptor *)((u_long)data + 797 sizeof(struct read_element_status_header) + 798 sizeof(struct read_element_status_page_header)); 799 for (i = 0; i < avail; ++i) { 800 user_data = desc->flags1; 801 error = copyout(&user_data, &uptr[i], avail); 802 if (error) 803 break; 804 desc = (struct read_element_status_descriptor *)((u_long)desc 805 + desclen); 806 } 807 808 done: 809 if (data != NULL) 810 free(data, M_DEVBUF); 811 return (error); 812 } 813 814 /* 815 * Perform a READ ELEMENT STATUS on behalf of the user. This returns 816 * the new (more complete) data format. 817 */ 818 static int 819 ch_usergetelemstatus(struct ch_softc *sc, 820 struct changer_element_status_request *cesr) 821 { 822 struct scsipi_channel *chan = sc->sc_periph->periph_channel; 823 struct scsipi_periph *dtperiph; 824 struct read_element_status_header *st_hdrp, st_hdr; 825 struct read_element_status_page_header *pg_hdrp; 826 struct read_element_status_descriptor *desc; 827 struct changer_volume_tag *avol, *pvol; 828 size_t size, desclen, stddesclen, offset; 829 int first, avail, i, error = 0; 830 void *data; 831 void *uvendptr; 832 struct changer_element_status ces; 833 834 /* 835 * Check arguments. 836 */ 837 if (cesr->cesr_type > CHET_DT) 838 return (EINVAL); 839 if (sc->sc_counts[cesr->cesr_type] == 0) 840 return (ENODEV); 841 if (cesr->cesr_unit > (sc->sc_counts[cesr->cesr_type] - 1)) 842 return (ENODEV); 843 if (cesr->cesr_count > 844 (sc->sc_counts[cesr->cesr_type] + cesr->cesr_unit)) 845 return (EINVAL); 846 847 /* 848 * Do the request the user wants, but only read the status header. 849 * This will tell us the amount of storage we must allocate 850 * in order to read all the data. 851 */ 852 error = ch_getelemstatus(sc, sc->sc_firsts[cesr->cesr_type] + 853 cesr->cesr_unit, cesr->cesr_count, &st_hdr, sizeof(st_hdr), 0, 854 cesr->cesr_flags); 855 if (error) 856 return (error); 857 858 size = sizeof(struct read_element_status_header) + 859 _3btol(st_hdr.nbytes); 860 861 /* 862 * We must have at least room for the status header and 863 * one page header (since we only ask for oen element type 864 * at a time). 865 */ 866 if (size < (sizeof(struct read_element_status_header) + 867 sizeof(struct read_element_status_page_header))) 868 return (EIO); 869 870 /* 871 * Allocate the storage and do the request again. 872 */ 873 data = malloc(size, M_DEVBUF, M_WAITOK); 874 error = ch_getelemstatus(sc, sc->sc_firsts[cesr->cesr_type] + 875 cesr->cesr_unit, cesr->cesr_count, data, size, 0, 876 cesr->cesr_flags); 877 if (error) 878 goto done; 879 880 st_hdrp = (struct read_element_status_header *)data; 881 pg_hdrp = (struct read_element_status_page_header *)((u_long)st_hdrp + 882 sizeof(struct read_element_status_header)); 883 desclen = _2btol(pg_hdrp->edl); 884 885 /* 886 * Fill in the user status array. 887 */ 888 first = _2btol(st_hdrp->fear); 889 if (first < (sc->sc_firsts[cesr->cesr_type] + cesr->cesr_unit) || 890 first >= (sc->sc_firsts[cesr->cesr_type] + cesr->cesr_unit + 891 cesr->cesr_count)) { 892 error = EIO; 893 goto done; 894 } 895 first -= sc->sc_firsts[cesr->cesr_type] + cesr->cesr_unit; 896 897 avail = _2btol(st_hdrp->count); 898 if (avail <= 0 || avail > cesr->cesr_count) { 899 error = EIO; 900 goto done; 901 } 902 903 offset = sizeof(struct read_element_status_header) + 904 sizeof(struct read_element_status_page_header); 905 906 for (i = 0; i < cesr->cesr_count; i++) { 907 memset(&ces, 0, sizeof(ces)); 908 if (i < first || i >= (first + avail)) { 909 error = copyout(&ces, &cesr->cesr_data[i], 910 sizeof(ces)); 911 if (error) 912 goto done; 913 } 914 915 desc = (struct read_element_status_descriptor *) 916 ((char *)data + offset); 917 stddesclen = sizeof(struct read_element_status_descriptor); 918 offset += desclen; 919 920 ces.ces_flags = CESTATUS_STATUS_VALID; 921 922 /* 923 * The SCSI flags conveniently map directly to the 924 * chio API flags. 925 */ 926 ces.ces_flags |= (desc->flags1 & 0x3f); 927 928 ces.ces_asc = desc->sense_code; 929 ces.ces_ascq = desc->sense_qual; 930 931 /* 932 * For Data Transport elemenets, get the SCSI ID and LUN, 933 * and attempt to map them to a device name if they're 934 * on the same SCSI bus. 935 */ 936 if (desc->dt_scsi_flags & READ_ELEMENT_STATUS_DT_IDVALID) { 937 ces.ces_target = desc->dt_scsi_addr; 938 ces.ces_flags |= CESTATUS_TARGET_VALID; 939 } 940 if (desc->dt_scsi_flags & READ_ELEMENT_STATUS_DT_LUVALID) { 941 ces.ces_lun = desc->dt_scsi_flags & 942 READ_ELEMENT_STATUS_DT_LUNMASK; 943 ces.ces_flags |= CESTATUS_LUN_VALID; 944 } 945 if (desc->dt_scsi_flags & READ_ELEMENT_STATUS_DT_NOTBUS) 946 ces.ces_flags |= CESTATUS_NOTBUS; 947 else if ((ces.ces_flags & 948 (CESTATUS_TARGET_VALID|CESTATUS_LUN_VALID)) == 949 (CESTATUS_TARGET_VALID|CESTATUS_LUN_VALID)) { 950 if (ces.ces_target < chan->chan_ntargets && 951 ces.ces_lun < chan->chan_nluns && 952 (dtperiph = scsipi_lookup_periph(chan, 953 ces.ces_target, ces.ces_lun)) != NULL && 954 dtperiph->periph_dev != NULL) { 955 strlcpy(ces.ces_xname, 956 device_xname(dtperiph->periph_dev), 957 sizeof(ces.ces_xname)); 958 ces.ces_flags |= CESTATUS_XNAME_VALID; 959 } 960 } 961 962 if (desc->flags2 & READ_ELEMENT_STATUS_INVERT) 963 ces.ces_flags |= CESTATUS_INVERTED; 964 965 if (desc->flags2 & READ_ELEMENT_STATUS_SVALID) { 966 if (ch_map_element(sc, _2btol(desc->ssea), 967 &ces.ces_from_type, &ces.ces_from_unit)) 968 ces.ces_flags |= CESTATUS_FROM_VALID; 969 } 970 971 /* 972 * Extract volume tag information. 973 */ 974 switch (pg_hdrp->flags & 975 (READ_ELEMENT_STATUS_PVOLTAG|READ_ELEMENT_STATUS_AVOLTAG)) { 976 case (READ_ELEMENT_STATUS_PVOLTAG|READ_ELEMENT_STATUS_AVOLTAG): 977 pvol = (struct changer_volume_tag *)(desc + 1); 978 avol = pvol + 1; 979 break; 980 981 case READ_ELEMENT_STATUS_PVOLTAG: 982 pvol = (struct changer_volume_tag *)(desc + 1); 983 avol = NULL; 984 break; 985 986 case READ_ELEMENT_STATUS_AVOLTAG: 987 pvol = NULL; 988 avol = (struct changer_volume_tag *)(desc + 1); 989 break; 990 991 default: 992 avol = pvol = NULL; 993 break; 994 } 995 996 if (pvol != NULL) { 997 ch_voltag_convert_in(pvol, &ces.ces_pvoltag); 998 ces.ces_flags |= CESTATUS_PVOL_VALID; 999 stddesclen += sizeof(struct changer_volume_tag); 1000 } 1001 if (avol != NULL) { 1002 ch_voltag_convert_in(avol, &ces.ces_avoltag); 1003 ces.ces_flags |= CESTATUS_AVOL_VALID; 1004 stddesclen += sizeof(struct changer_volume_tag); 1005 } 1006 1007 /* 1008 * Compute vendor-specific length. Note the 4 reserved 1009 * bytes between the volume tags and the vendor-specific 1010 * data. Copy it out of the user wants it. 1011 */ 1012 stddesclen += 4; 1013 if (desclen > stddesclen) 1014 ces.ces_vendor_len = desclen - stddesclen; 1015 1016 if (ces.ces_vendor_len != 0 && cesr->cesr_vendor_data != NULL) { 1017 error = copyin(&cesr->cesr_vendor_data[i], &uvendptr, 1018 sizeof(uvendptr)); 1019 if (error) 1020 goto done; 1021 error = copyout((void *)((u_long)desc + stddesclen), 1022 uvendptr, ces.ces_vendor_len); 1023 if (error) 1024 goto done; 1025 } 1026 1027 /* 1028 * Now copy out the status descriptor we've constructed. 1029 */ 1030 error = copyout(&ces, &cesr->cesr_data[i], sizeof(ces)); 1031 if (error) 1032 goto done; 1033 } 1034 1035 done: 1036 if (data != NULL) 1037 free(data, M_DEVBUF); 1038 return (error); 1039 } 1040 1041 static int 1042 ch_getelemstatus(struct ch_softc *sc, int first, int count, void *data, 1043 size_t datalen, int scsiflags, int flags) 1044 { 1045 struct scsi_read_element_status cmd; 1046 1047 /* 1048 * Build SCSI command. 1049 */ 1050 memset(&cmd, 0, sizeof(cmd)); 1051 cmd.opcode = READ_ELEMENT_STATUS; 1052 cmd.byte2 = ELEMENT_TYPE_ALL; 1053 if (flags & CESR_VOLTAGS) 1054 cmd.byte2 |= READ_ELEMENT_STATUS_VOLTAG; 1055 _lto2b(first, cmd.sea); 1056 _lto2b(count, cmd.count); 1057 _lto3b(datalen, cmd.len); 1058 1059 /* 1060 * Send command to changer. 1061 */ 1062 return (scsipi_command(sc->sc_periph, (void *)&cmd, sizeof(cmd), 1063 (void *)data, datalen, 1064 CHRETRIES, CHTIMEOUT, NULL, scsiflags | XS_CTL_DATA_IN)); 1065 } 1066 1067 static int 1068 ch_setvoltag(struct ch_softc *sc, struct changer_set_voltag_request *csvr) 1069 { 1070 struct scsi_send_volume_tag cmd; 1071 struct changer_volume_tag voltag; 1072 void *data = NULL; 1073 size_t datalen = 0; 1074 int error; 1075 u_int16_t dst; 1076 1077 /* 1078 * Check arguments. 1079 */ 1080 if (csvr->csvr_type > CHET_DT) 1081 return (EINVAL); 1082 if (csvr->csvr_unit > (sc->sc_counts[csvr->csvr_type] - 1)) 1083 return (ENODEV); 1084 1085 dst = sc->sc_firsts[csvr->csvr_type] + csvr->csvr_unit; 1086 1087 /* 1088 * Build the SCSI command. 1089 */ 1090 memset(&cmd, 0, sizeof(cmd)); 1091 cmd.opcode = SEND_VOLUME_TAG; 1092 _lto2b(dst, cmd.eaddr); 1093 1094 #define ALTERNATE (csvr->csvr_flags & CSVR_ALTERNATE) 1095 1096 switch (csvr->csvr_flags & CSVR_MODE_MASK) { 1097 case CSVR_MODE_SET: 1098 cmd.sac = ALTERNATE ? SAC_ASSERT_ALT : SAC_ASSERT_PRIMARY; 1099 break; 1100 1101 case CSVR_MODE_REPLACE: 1102 cmd.sac = ALTERNATE ? SAC_REPLACE_ALT : SAC_REPLACE_PRIMARY; 1103 break; 1104 1105 case CSVR_MODE_CLEAR: 1106 cmd.sac = ALTERNATE ? SAC_UNDEFINED_ALT : SAC_UNDEFINED_PRIMARY; 1107 break; 1108 1109 default: 1110 return (EINVAL); 1111 } 1112 1113 #undef ALTERNATE 1114 1115 if (cmd.sac < SAC_UNDEFINED_PRIMARY) { 1116 error = ch_voltag_convert_out(&csvr->csvr_voltag, &voltag); 1117 if (error) 1118 return (error); 1119 data = &voltag; 1120 datalen = sizeof(voltag); 1121 _lto2b(datalen, cmd.length); 1122 } 1123 1124 /* 1125 * Send command to changer. 1126 */ 1127 return (scsipi_command(sc->sc_periph, (void *)&cmd, sizeof(cmd), 1128 (void *)data, datalen, CHRETRIES, CHTIMEOUT, NULL, 1129 datalen ? XS_CTL_DATA_OUT : 0)); 1130 } 1131 1132 static int 1133 ch_ielem(struct ch_softc *sc) 1134 { 1135 int tmo; 1136 struct scsi_initialize_element_status cmd; 1137 1138 /* 1139 * Build SCSI command. 1140 */ 1141 memset(&cmd, 0, sizeof(cmd)); 1142 cmd.opcode = INITIALIZE_ELEMENT_STATUS; 1143 1144 /* 1145 * Send command to changer. 1146 * 1147 * The problem is, how long to allow for the command? 1148 * It can take a *really* long time, and also depends 1149 * on unknowable factors such as whether there are 1150 * *almost* readable labels on tapes that a barcode 1151 * reader is trying to decipher. 1152 * 1153 * I'm going to make this long enough to allow 5 minutes 1154 * per element plus an initial 10 minute wait. 1155 */ 1156 tmo = sc->sc_counts[CHET_MT] + 1157 sc->sc_counts[CHET_ST] + 1158 sc->sc_counts[CHET_IE] + 1159 sc->sc_counts[CHET_DT]; 1160 tmo *= 5 * 60 * 1000; 1161 tmo += (10 * 60 * 1000); 1162 1163 return (scsipi_command(sc->sc_periph, (void *)&cmd, sizeof(cmd), 0, 0, 1164 CHRETRIES, tmo, NULL, XS_CTL_IGNORE_ILLEGAL_REQUEST)); 1165 } 1166 1167 /* 1168 * Ask the device about itself and fill in the parameters in our 1169 * softc. 1170 */ 1171 static int 1172 ch_get_params(struct ch_softc *sc, int scsiflags) 1173 { 1174 struct scsi_mode_sense_data { 1175 struct scsi_mode_parameter_header_6 header; 1176 union { 1177 struct page_element_address_assignment ea; 1178 struct page_transport_geometry_parameters tg; 1179 struct page_device_capabilities cap; 1180 } pages; 1181 } sense_data; 1182 int error, from; 1183 u_int8_t *moves, *exchanges; 1184 1185 /* 1186 * Grab info from the element address assignment page. 1187 */ 1188 memset(&sense_data, 0, sizeof(sense_data)); 1189 error = scsipi_mode_sense(sc->sc_periph, SMS_DBD, 0x1d, 1190 &sense_data.header, sizeof(sense_data), 1191 scsiflags, CHRETRIES, 6000); 1192 if (error) { 1193 aprint_error_dev(sc->sc_dev, "could not sense element address page\n"); 1194 return (error); 1195 } 1196 1197 sc->sc_firsts[CHET_MT] = _2btol(sense_data.pages.ea.mtea); 1198 sc->sc_counts[CHET_MT] = _2btol(sense_data.pages.ea.nmte); 1199 sc->sc_firsts[CHET_ST] = _2btol(sense_data.pages.ea.fsea); 1200 sc->sc_counts[CHET_ST] = _2btol(sense_data.pages.ea.nse); 1201 sc->sc_firsts[CHET_IE] = _2btol(sense_data.pages.ea.fieea); 1202 sc->sc_counts[CHET_IE] = _2btol(sense_data.pages.ea.niee); 1203 sc->sc_firsts[CHET_DT] = _2btol(sense_data.pages.ea.fdtea); 1204 sc->sc_counts[CHET_DT] = _2btol(sense_data.pages.ea.ndte); 1205 1206 /* XXX ask for transport geometry page XXX */ 1207 1208 /* 1209 * Grab info from the capabilities page. 1210 */ 1211 memset(&sense_data, 0, sizeof(sense_data)); 1212 /* 1213 * XXX: Note: not all changers can deal with disabled block descriptors 1214 */ 1215 error = scsipi_mode_sense(sc->sc_periph, SMS_DBD, 0x1f, 1216 &sense_data.header, sizeof(sense_data), 1217 scsiflags, CHRETRIES, 6000); 1218 if (error) { 1219 aprint_error_dev(sc->sc_dev, "could not sense capabilities page\n"); 1220 return (error); 1221 } 1222 1223 memset(sc->sc_movemask, 0, sizeof(sc->sc_movemask)); 1224 memset(sc->sc_exchangemask, 0, sizeof(sc->sc_exchangemask)); 1225 moves = &sense_data.pages.cap.move_from_mt; 1226 exchanges = &sense_data.pages.cap.exchange_with_mt; 1227 for (from = CHET_MT; from <= CHET_DT; ++from) { 1228 sc->sc_movemask[from] = moves[from]; 1229 sc->sc_exchangemask[from] = exchanges[from]; 1230 } 1231 1232 #ifdef CH_AUTOMATIC_IELEM_POLICY 1233 /* 1234 * If we need to do an Init-Element-Status, 1235 * do that now that we know what's in the changer. 1236 */ 1237 if ((scsiflags & XS_CTL_IGNORE_MEDIA_CHANGE) == 0) { 1238 if ((sc->sc_periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) 1239 error = ch_ielem(sc); 1240 if (error == 0) 1241 sc->sc_periph->periph_flags |= PERIPH_MEDIA_LOADED; 1242 else 1243 sc->sc_periph->periph_flags &= ~PERIPH_MEDIA_LOADED; 1244 } 1245 #endif 1246 return (error); 1247 } 1248 1249 static void 1250 ch_get_quirks(struct ch_softc *sc, struct scsipi_inquiry_pattern *inqbuf) 1251 { 1252 const struct chquirk *match; 1253 int priority; 1254 1255 sc->sc_settledelay = 0; 1256 1257 match = scsipi_inqmatch(inqbuf, chquirks, 1258 sizeof(chquirks) / sizeof(chquirks[0]), 1259 sizeof(chquirks[0]), &priority); 1260 if (priority != 0) 1261 sc->sc_settledelay = match->cq_settledelay; 1262 } 1263 1264 static int 1265 ch_map_element(struct ch_softc *sc, u_int16_t elem, int *typep, int *unitp) 1266 { 1267 int chet; 1268 1269 for (chet = CHET_MT; chet <= CHET_DT; chet++) { 1270 if (elem >= sc->sc_firsts[chet] && 1271 elem < (sc->sc_firsts[chet] + sc->sc_counts[chet])) { 1272 *typep = chet; 1273 *unitp = elem - sc->sc_firsts[chet]; 1274 return (1); 1275 } 1276 } 1277 return (0); 1278 } 1279 1280 static void 1281 ch_voltag_convert_in(const struct changer_volume_tag *sv, 1282 struct changer_voltag *cv) 1283 { 1284 int i; 1285 1286 memset(cv, 0, sizeof(struct changer_voltag)); 1287 1288 /* 1289 * Copy the volume tag string from the SCSI representation. 1290 * Per the SCSI-2 spec, we stop at the first blank character. 1291 */ 1292 for (i = 0; i < sizeof(sv->volid); i++) { 1293 if (sv->volid[i] == ' ') 1294 break; 1295 cv->cv_tag[i] = sv->volid[i]; 1296 } 1297 cv->cv_tag[i] = '\0'; 1298 1299 cv->cv_serial = _2btol(sv->volseq); 1300 } 1301 1302 static int 1303 ch_voltag_convert_out(const struct changer_voltag *cv, 1304 struct changer_volume_tag *sv) 1305 { 1306 int i; 1307 1308 memset(sv, ' ', sizeof(struct changer_volume_tag)); 1309 1310 for (i = 0; i < sizeof(sv->volid); i++) { 1311 if (cv->cv_tag[i] == '\0') 1312 break; 1313 /* 1314 * Limit the character set to what is suggested in 1315 * the SCSI-2 spec. 1316 */ 1317 if ((cv->cv_tag[i] < '0' || cv->cv_tag[i] > '9') && 1318 (cv->cv_tag[i] < 'A' || cv->cv_tag[i] > 'Z') && 1319 (cv->cv_tag[i] != '_')) 1320 return (EINVAL); 1321 sv->volid[i] = cv->cv_tag[i]; 1322 } 1323 1324 _lto2b(cv->cv_serial, sv->volseq); 1325 1326 return (0); 1327 } 1328