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