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