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