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