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