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