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