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