1 /* $NetBSD: ch.c,v 1.43 2000/06/09 08:54:21 enami 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, 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), 701 XS_CTL_DATA_ONSTACK, 0); 702 if (error) 703 return (error); 704 705 size = sizeof(struct read_element_status_header) + 706 _3btol(st_hdr.nbytes); 707 708 /* 709 * We must have at least room for the status header and 710 * one page header (since we only ask for one element type 711 * at a time). 712 */ 713 if (size < (sizeof(struct read_element_status_header) + 714 sizeof(struct read_element_status_page_header))) 715 return (EIO); 716 717 /* 718 * Allocate the storage and do the request again. 719 */ 720 data = malloc(size, M_DEVBUF, M_WAITOK); 721 error = ch_getelemstatus(sc, sc->sc_firsts[chet], 722 sc->sc_counts[chet], data, size, 0, 0); 723 if (error) 724 goto done; 725 726 st_hdrp = (struct read_element_status_header *)data; 727 pg_hdrp = (struct read_element_status_page_header *)((u_long)st_hdrp + 728 sizeof(struct read_element_status_header)); 729 desclen = _2btol(pg_hdrp->edl); 730 731 /* 732 * Fill in the user status array. 733 */ 734 avail = _2btol(st_hdrp->count); 735 736 if (avail != sc->sc_counts[chet]) 737 printf("%s: warning, READ ELEMENT STATUS avail != count\n", 738 sc->sc_dev.dv_xname); 739 740 desc = (struct read_element_status_descriptor *)((u_long)data + 741 sizeof(struct read_element_status_header) + 742 sizeof(struct read_element_status_page_header)); 743 for (i = 0; i < avail; ++i) { 744 user_data = desc->flags1; 745 error = copyout(&user_data, &uptr[i], avail); 746 if (error) 747 break; 748 (u_long)desc += desclen; 749 } 750 751 done: 752 if (data != NULL) 753 free(data, M_DEVBUF); 754 return (error); 755 } 756 757 /* 758 * Perform a READ ELEMENT STATUS on behalf of the user. This returns 759 * the new (more complete) data format. 760 */ 761 int 762 ch_usergetelemstatus(sc, cesr) 763 struct ch_softc *sc; 764 struct changer_element_status_request *cesr; 765 { 766 struct scsibus_softc *parent; 767 struct scsipi_link *dtlink; 768 struct device *dtdev; 769 struct read_element_status_header *st_hdrp, st_hdr; 770 struct read_element_status_page_header *pg_hdrp; 771 struct read_element_status_descriptor *desc; 772 struct changer_volume_tag *avol, *pvol; 773 size_t size, desclen, stddesclen, offset; 774 int first, avail, i, error = 0; 775 caddr_t data; 776 void *uvendptr; 777 struct changer_element_status ces; 778 779 /* 780 * Check arguments. 781 */ 782 if (cesr->cesr_type > CHET_DT) 783 return (EINVAL); 784 if (sc->sc_counts[cesr->cesr_type] == 0) 785 return (ENODEV); 786 if (cesr->cesr_unit > (sc->sc_counts[cesr->cesr_type] - 1)) 787 return (ENODEV); 788 if (cesr->cesr_count > 789 (sc->sc_counts[cesr->cesr_type] + cesr->cesr_unit)) 790 return (EINVAL); 791 792 /* 793 * Do the request the user wants, but only read the status header. 794 * This will tell us the amount of storage we must allocate 795 * in order to read all the data. 796 */ 797 error = ch_getelemstatus(sc, sc->sc_firsts[cesr->cesr_type] + 798 cesr->cesr_unit, cesr->cesr_count, &st_hdr, sizeof(st_hdr), 0, 799 cesr->cesr_flags); 800 if (error) 801 return (error); 802 803 size = sizeof(struct read_element_status_header) + 804 _3btol(st_hdr.nbytes); 805 806 /* 807 * We must have at least room for the status header and 808 * one page header (since we only ask for oen element type 809 * at a time). 810 */ 811 if (size < (sizeof(struct read_element_status_header) + 812 sizeof(struct read_element_status_page_header))) 813 return (EIO); 814 815 /* 816 * Allocate the storage and do the request again. 817 */ 818 data = malloc(size, M_DEVBUF, M_WAITOK); 819 error = ch_getelemstatus(sc, sc->sc_firsts[cesr->cesr_type] + 820 cesr->cesr_unit, cesr->cesr_count, data, size, 0, 821 cesr->cesr_flags); 822 if (error) 823 goto done; 824 825 st_hdrp = (struct read_element_status_header *)data; 826 pg_hdrp = (struct read_element_status_page_header *)((u_long)st_hdrp + 827 sizeof(struct read_element_status_header)); 828 desclen = _2btol(pg_hdrp->edl); 829 830 /* 831 * Fill in the user status array. 832 */ 833 first = _2btol(st_hdrp->fear); 834 if (first < (sc->sc_firsts[cesr->cesr_type] + cesr->cesr_unit) || 835 first >= (sc->sc_firsts[cesr->cesr_type] + cesr->cesr_unit + 836 cesr->cesr_count)) { 837 error = EIO; 838 goto done; 839 } 840 first -= sc->sc_firsts[cesr->cesr_type] + cesr->cesr_unit; 841 842 avail = _2btol(st_hdrp->count); 843 if (avail <= 0 || avail > cesr->cesr_count) { 844 error = EIO; 845 goto done; 846 } 847 848 offset = sizeof(struct read_element_status_header) + 849 sizeof(struct read_element_status_page_header); 850 851 for (i = 0; i < cesr->cesr_count; i++) { 852 memset(&ces, 0, sizeof(ces)); 853 if (i < first || i >= (first + avail)) { 854 error = copyout(&ces, &cesr->cesr_data[i], 855 sizeof(ces)); 856 if (error) 857 goto done; 858 } 859 860 desc = (struct read_element_status_descriptor *) 861 (data + offset); 862 stddesclen = sizeof(struct read_element_status_descriptor); 863 offset += desclen; 864 865 ces.ces_flags = CESTATUS_STATUS_VALID; 866 867 /* 868 * The SCSI flags conveniently map directly to the 869 * chio API flags. 870 */ 871 ces.ces_flags |= (desc->flags1 & 0x3f); 872 873 ces.ces_asc = desc->sense_code; 874 ces.ces_ascq = desc->sense_qual; 875 876 /* 877 * For Data Transport elemenets, get the SCSI ID and LUN, 878 * and attempt to map them to a device name if they're 879 * on the same SCSI bus. 880 */ 881 if (desc->dt_scsi_flags & READ_ELEMENT_STATUS_DT_IDVALID) { 882 ces.ces_target = desc->dt_scsi_addr; 883 ces.ces_flags |= CESTATUS_TARGET_VALID; 884 } 885 if (desc->dt_scsi_flags & READ_ELEMENT_STATUS_DT_LUVALID) { 886 ces.ces_lun = desc->dt_scsi_flags & 887 READ_ELEMENT_STATUS_DT_LUNMASK; 888 ces.ces_flags |= CESTATUS_LUN_VALID; 889 } 890 if (desc->dt_scsi_flags & READ_ELEMENT_STATUS_DT_NOTBUS) 891 ces.ces_flags |= CESTATUS_NOTBUS; 892 else if ((ces.ces_flags & 893 (CESTATUS_TARGET_VALID|CESTATUS_LUN_VALID)) == 894 (CESTATUS_TARGET_VALID|CESTATUS_LUN_VALID)) { 895 parent = (struct scsibus_softc *)sc->sc_dev.dv_parent; 896 if (ces.ces_target <= parent->sc_maxtarget && 897 ces.ces_lun <= parent->sc_maxlun && 898 (dtlink = 899 parent->sc_link[ces.ces_target][ces.ces_lun]) 900 != NULL && 901 (dtdev = dtlink->device_softc) != NULL) { 902 strcpy(ces.ces_xname, dtdev->dv_xname); 903 ces.ces_flags |= CESTATUS_XNAME_VALID; 904 } 905 } 906 907 if (desc->flags2 & READ_ELEMENT_STATUS_INVERT) 908 ces.ces_flags |= CESTATUS_INVERTED; 909 910 if (desc->flags2 & READ_ELEMENT_STATUS_SVALID) { 911 if (ch_map_element(sc, _2btol(desc->ssea), 912 &ces.ces_from_type, &ces.ces_from_unit)) 913 ces.ces_flags |= CESTATUS_FROM_VALID; 914 } 915 916 /* 917 * Extract volume tag information. 918 */ 919 switch (pg_hdrp->flags & 920 (READ_ELEMENT_STATUS_PVOLTAG|READ_ELEMENT_STATUS_AVOLTAG)) { 921 case (READ_ELEMENT_STATUS_PVOLTAG|READ_ELEMENT_STATUS_AVOLTAG): 922 pvol = (struct changer_volume_tag *)(desc + 1); 923 avol = pvol + 1; 924 break; 925 926 case READ_ELEMENT_STATUS_PVOLTAG: 927 pvol = (struct changer_volume_tag *)(desc + 1); 928 avol = NULL; 929 break; 930 931 case READ_ELEMENT_STATUS_AVOLTAG: 932 pvol = NULL; 933 avol = (struct changer_volume_tag *)(desc + 1); 934 break; 935 936 default: 937 avol = pvol = NULL; 938 break; 939 } 940 941 if (pvol != NULL) { 942 ch_voltag_convert_in(pvol, &ces.ces_pvoltag); 943 ces.ces_flags |= CESTATUS_PVOL_VALID; 944 stddesclen += sizeof(struct changer_volume_tag); 945 } 946 if (avol != NULL) { 947 ch_voltag_convert_in(avol, &ces.ces_avoltag); 948 ces.ces_flags |= CESTATUS_AVOL_VALID; 949 stddesclen += sizeof(struct changer_volume_tag); 950 } 951 952 /* 953 * Compute vendor-specific length. Note the 4 reserved 954 * bytes between the volume tags and the vendor-specific 955 * data. Copy it out of the user wants it. 956 */ 957 stddesclen += 4; 958 if (desclen > stddesclen) 959 ces.ces_vendor_len = desclen - stddesclen; 960 961 if (ces.ces_vendor_len != 0 && cesr->cesr_vendor_data != NULL) { 962 error = copyin(&cesr->cesr_vendor_data[i], &uvendptr, 963 sizeof(uvendptr)); 964 if (error) 965 goto done; 966 error = copyout((void *)((u_long)desc + stddesclen), 967 uvendptr, ces.ces_vendor_len); 968 if (error) 969 goto done; 970 } 971 972 /* 973 * Now copy out the status descriptor we've constructed. 974 */ 975 error = copyout(&ces, &cesr->cesr_data[i], sizeof(ces)); 976 if (error) 977 goto done; 978 } 979 980 done: 981 if (data != NULL) 982 free(data, M_DEVBUF); 983 return (error); 984 } 985 986 int 987 ch_getelemstatus(sc, first, count, data, datalen, scsiflags, flags) 988 struct ch_softc *sc; 989 int first, count; 990 void *data; 991 size_t datalen; 992 int scsiflags; 993 int flags; 994 { 995 struct scsi_read_element_status cmd; 996 997 /* 998 * Build SCSI command. 999 */ 1000 bzero(&cmd, sizeof(cmd)); 1001 cmd.opcode = READ_ELEMENT_STATUS; 1002 cmd.byte2 = ELEMENT_TYPE_ALL; 1003 if (flags & CESR_VOLTAGS) 1004 cmd.byte2 |= READ_ELEMENT_STATUS_VOLTAG; 1005 _lto2b(first, cmd.sea); 1006 _lto2b(count, cmd.count); 1007 _lto3b(datalen, cmd.len); 1008 1009 /* 1010 * Send command to changer. 1011 */ 1012 return (scsipi_command(sc->sc_link, 1013 (struct scsipi_generic *)&cmd, sizeof(cmd), 1014 (u_char *)data, datalen, CHRETRIES, 100000, NULL, 1015 scsiflags | XS_CTL_DATA_IN)); 1016 } 1017 1018 int 1019 ch_setvoltag(sc, csvr) 1020 struct ch_softc *sc; 1021 struct changer_set_voltag_request *csvr; 1022 { 1023 struct scsi_send_volume_tag cmd; 1024 struct changer_volume_tag voltag; 1025 void *data = NULL; 1026 size_t datalen = 0; 1027 int error; 1028 u_int16_t dst; 1029 1030 /* 1031 * Check arguments. 1032 */ 1033 if (csvr->csvr_type > CHET_DT) 1034 return (EINVAL); 1035 if (csvr->csvr_unit > (sc->sc_counts[csvr->csvr_type] - 1)) 1036 return (ENODEV); 1037 1038 dst = sc->sc_firsts[csvr->csvr_type] + csvr->csvr_unit; 1039 1040 /* 1041 * Build the SCSI command. 1042 */ 1043 bzero(&cmd, sizeof(cmd)); 1044 cmd.opcode = SEND_VOLUME_TAG; 1045 _lto2b(dst, cmd.eaddr); 1046 1047 #define ALTERNATE (csvr->csvr_flags & CSVR_ALTERNATE) 1048 1049 switch (csvr->csvr_flags & CSVR_MODE_MASK) { 1050 case CSVR_MODE_SET: 1051 cmd.sac = ALTERNATE ? SAC_ASSERT_ALT : SAC_ASSERT_PRIMARY; 1052 break; 1053 1054 case CSVR_MODE_REPLACE: 1055 cmd.sac = ALTERNATE ? SAC_REPLACE_ALT : SAC_REPLACE_PRIMARY; 1056 break; 1057 1058 case CSVR_MODE_CLEAR: 1059 cmd.sac = ALTERNATE ? SAC_UNDEFINED_ALT : SAC_UNDEFINED_PRIMARY; 1060 break; 1061 1062 default: 1063 return (EINVAL); 1064 } 1065 1066 #undef ALTERNATE 1067 1068 if (cmd.sac < SAC_UNDEFINED_PRIMARY) { 1069 error = ch_voltag_convert_out(&csvr->csvr_voltag, &voltag); 1070 if (error) 1071 return (error); 1072 data = &voltag; 1073 datalen = sizeof(voltag); 1074 _lto2b(datalen, cmd.length); 1075 } 1076 1077 /* 1078 * Send command to changer. 1079 */ 1080 return (scsipi_command(sc->sc_link, 1081 (struct scsipi_generic *)&cmd, sizeof(cmd), 1082 (u_char *)data, datalen, CHRETRIES, 100000, NULL, 1083 datalen ? XS_CTL_DATA_OUT | XS_CTL_DATA_ONSTACK : 0)); 1084 } 1085 1086 int 1087 ch_ielem(sc) 1088 struct ch_softc *sc; 1089 { 1090 int tmo; 1091 struct scsi_initialize_element_status cmd; 1092 1093 /* 1094 * Build SCSI command. 1095 */ 1096 bzero(&cmd, sizeof(cmd)); 1097 cmd.opcode = INITIALIZE_ELEMENT_STATUS; 1098 1099 /* 1100 * Send command to changer. 1101 * 1102 * The problem is, how long to allow for the command? 1103 * It can take a *really* long time, and also depends 1104 * on unknowable factors such as whether there are 1105 * *almost* readable labels on tapes that a barcode 1106 * reader is trying to decipher. 1107 * 1108 * I'm going to make this long enough to allow 5 minutes 1109 * per element plus an initial 10 minute wait. 1110 */ 1111 tmo = sc->sc_counts[CHET_MT] + 1112 sc->sc_counts[CHET_ST] + 1113 sc->sc_counts[CHET_IE] + 1114 sc->sc_counts[CHET_DT]; 1115 tmo *= 5 * 60 * 1000; 1116 tmo += (10 * 60 * 1000); 1117 1118 return (scsipi_command(sc->sc_link, 1119 (struct scsipi_generic *)&cmd, sizeof(cmd), 1120 NULL, 0, CHRETRIES, tmo, NULL, XS_CTL_IGNORE_ILLEGAL_REQUEST)); 1121 } 1122 1123 /* 1124 * Ask the device about itself and fill in the parameters in our 1125 * softc. 1126 */ 1127 int 1128 ch_get_params(sc, scsiflags) 1129 struct ch_softc *sc; 1130 int scsiflags; 1131 { 1132 struct scsi_mode_sense cmd; 1133 struct scsi_mode_sense_data { 1134 struct scsi_mode_header header; 1135 union { 1136 struct page_element_address_assignment ea; 1137 struct page_transport_geometry_parameters tg; 1138 struct page_device_capabilities cap; 1139 } pages; 1140 } sense_data; 1141 int error, from; 1142 u_int8_t *moves, *exchanges; 1143 1144 /* 1145 * Grab info from the element address assignment page. 1146 */ 1147 bzero(&cmd, sizeof(cmd)); 1148 bzero(&sense_data, sizeof(sense_data)); 1149 cmd.opcode = SCSI_MODE_SENSE; 1150 cmd.byte2 |= 0x08; /* disable block descriptors */ 1151 cmd.page = 0x1d; 1152 cmd.length = (sizeof(sense_data) & 0xff); 1153 error = scsipi_command(sc->sc_link, 1154 (struct scsipi_generic *)&cmd, sizeof(cmd), (u_char *)&sense_data, 1155 sizeof(sense_data), CHRETRIES, 6000, NULL, 1156 scsiflags | XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK); 1157 if (error) { 1158 printf("%s: could not sense element address page\n", 1159 sc->sc_dev.dv_xname); 1160 return (error); 1161 } 1162 1163 sc->sc_firsts[CHET_MT] = _2btol(sense_data.pages.ea.mtea); 1164 sc->sc_counts[CHET_MT] = _2btol(sense_data.pages.ea.nmte); 1165 sc->sc_firsts[CHET_ST] = _2btol(sense_data.pages.ea.fsea); 1166 sc->sc_counts[CHET_ST] = _2btol(sense_data.pages.ea.nse); 1167 sc->sc_firsts[CHET_IE] = _2btol(sense_data.pages.ea.fieea); 1168 sc->sc_counts[CHET_IE] = _2btol(sense_data.pages.ea.niee); 1169 sc->sc_firsts[CHET_DT] = _2btol(sense_data.pages.ea.fdtea); 1170 sc->sc_counts[CHET_DT] = _2btol(sense_data.pages.ea.ndte); 1171 1172 /* XXX ask for transport geometry page XXX */ 1173 1174 /* 1175 * Grab info from the capabilities page. 1176 */ 1177 bzero(&cmd, sizeof(cmd)); 1178 bzero(&sense_data, sizeof(sense_data)); 1179 cmd.opcode = SCSI_MODE_SENSE; 1180 /* 1181 * XXX: Note: not all changers can deal with disabled block descriptors 1182 */ 1183 cmd.byte2 = 0x08; /* disable block descriptors */ 1184 cmd.page = 0x1f; 1185 cmd.length = (sizeof(sense_data) & 0xff); 1186 error = scsipi_command(sc->sc_link, 1187 (struct scsipi_generic *)&cmd, sizeof(cmd), (u_char *)&sense_data, 1188 sizeof(sense_data), CHRETRIES, 6000, NULL, 1189 scsiflags | XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK); 1190 if (error) { 1191 printf("%s: could not sense capabilities page\n", 1192 sc->sc_dev.dv_xname); 1193 return (error); 1194 } 1195 1196 bzero(sc->sc_movemask, sizeof(sc->sc_movemask)); 1197 bzero(sc->sc_exchangemask, sizeof(sc->sc_exchangemask)); 1198 moves = &sense_data.pages.cap.move_from_mt; 1199 exchanges = &sense_data.pages.cap.exchange_with_mt; 1200 for (from = CHET_MT; from <= CHET_DT; ++from) { 1201 sc->sc_movemask[from] = moves[from]; 1202 sc->sc_exchangemask[from] = exchanges[from]; 1203 } 1204 1205 #ifdef CH_AUTOMATIC_IELEM_POLICY 1206 /* 1207 * If we need to do an Init-Element-Status, 1208 * do that now that we know what's in the changer. 1209 */ 1210 if ((scsiflags & XS_CTL_IGNORE_MEDIA_CHANGE) == 0) { 1211 if ((sc->sc_link->flags & SDEV_MEDIA_LOADED) == 0) 1212 error = ch_ielem(sc); 1213 if (error == 0) 1214 sc->sc_link->flags |= SDEV_MEDIA_LOADED; 1215 else 1216 sc->sc_link->flags &= ~SDEV_MEDIA_LOADED; 1217 } 1218 #endif 1219 return (error); 1220 } 1221 1222 void 1223 ch_get_quirks(sc, inqbuf) 1224 struct ch_softc *sc; 1225 struct scsipi_inquiry_pattern *inqbuf; 1226 { 1227 struct chquirk *match; 1228 int priority; 1229 1230 sc->sc_settledelay = 0; 1231 1232 match = (struct chquirk *)scsipi_inqmatch(inqbuf, 1233 (caddr_t)chquirks, 1234 sizeof(chquirks) / sizeof(chquirks[0]), 1235 sizeof(chquirks[0]), &priority); 1236 if (priority != 0) 1237 sc->sc_settledelay = match->cq_settledelay; 1238 } 1239 1240 int 1241 ch_map_element(sc, elem, typep, unitp) 1242 struct ch_softc *sc; 1243 u_int16_t elem; 1244 int *typep, *unitp; 1245 { 1246 int chet; 1247 1248 for (chet = CHET_MT; chet <= CHET_DT; chet++) { 1249 if (elem >= sc->sc_firsts[chet] && 1250 elem < (sc->sc_firsts[chet] + sc->sc_counts[chet])) { 1251 *typep = chet; 1252 *unitp = elem - sc->sc_firsts[chet]; 1253 return (1); 1254 } 1255 } 1256 return (0); 1257 } 1258 1259 void 1260 ch_voltag_convert_in(sv, cv) 1261 const struct changer_volume_tag *sv; 1262 struct changer_voltag *cv; 1263 { 1264 int i; 1265 1266 memset(cv, 0, sizeof(struct changer_voltag)); 1267 1268 /* 1269 * Copy the volume tag string from the SCSI representation. 1270 * Per the SCSI-2 spec, we stop at the first blank character. 1271 */ 1272 for (i = 0; i < sizeof(sv->volid); i++) { 1273 if (sv->volid[i] == ' ') 1274 break; 1275 cv->cv_tag[i] = sv->volid[i]; 1276 } 1277 cv->cv_tag[i] = '\0'; 1278 1279 cv->cv_serial = _2btol(sv->volseq); 1280 } 1281 1282 int 1283 ch_voltag_convert_out(cv, sv) 1284 const struct changer_voltag *cv; 1285 struct changer_volume_tag *sv; 1286 { 1287 int i; 1288 1289 memset(sv, ' ', sizeof(struct changer_volume_tag)); 1290 1291 for (i = 0; i < sizeof(sv->volid); i++) { 1292 if (cv->cv_tag[i] == '\0') 1293 break; 1294 /* 1295 * Limit the character set to what is suggested in 1296 * the SCSI-2 spec. 1297 */ 1298 if ((cv->cv_tag[i] < '0' || cv->cv_tag[i] > '9') && 1299 (cv->cv_tag[i] < 'A' || cv->cv_tag[i] > 'Z') && 1300 (cv->cv_tag[i] != '_')) 1301 return (EINVAL); 1302 sv->volid[i] = cv->cv_tag[i]; 1303 } 1304 1305 _lto2b(cv->cv_serial, sv->volseq); 1306 1307 return (0); 1308 } 1309