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