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