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