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