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