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