Home | History | Annotate | Line # | Download | only in scsipi
ch.c revision 1.35
      1  1.35   thorpej /*	$NetBSD: ch.c,v 1.35 1998/11/20 00:35:39 thorpej Exp $	*/
      2   1.8       cgd 
      3   1.1       cgd /*
      4  1.35   thorpej  * Copyright (c) 1996, 1997, 1998 Jason R. Thorpe <thorpej (at) and.com>
      5  1.20   thorpej  * All rights reserved.
      6  1.20   thorpej  *
      7  1.20   thorpej  * Partially based on an autochanger driver written by Stefan Grefen
      8  1.20   thorpej  * and on an autochanger driver written by the Systems Programming Group
      9  1.20   thorpej  * at the University of Utah Computer Science Department.
     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.20   thorpej  *    must display the following acknowledgements:
     21  1.20   thorpej  *	This product includes software developed by Jason R. Thorpe
     22  1.20   thorpej  *	for And Communications, http://www.and.com/
     23   1.6   mycroft  * 4. The name of the author may not be used to endorse or promote products
     24   1.6   mycroft  *    derived from this software without specific prior written permission.
     25   1.6   mycroft  *
     26   1.6   mycroft  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     27   1.6   mycroft  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     28   1.6   mycroft  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     29   1.6   mycroft  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     30  1.20   thorpej  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     31  1.20   thorpej  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     32  1.20   thorpej  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     33  1.20   thorpej  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     34  1.20   thorpej  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  1.20   thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  1.20   thorpej  * SUCH DAMAGE.
     37   1.6   mycroft  */
     38   1.1       cgd 
     39   1.1       cgd #include <sys/param.h>
     40   1.1       cgd #include <sys/systm.h>
     41   1.1       cgd #include <sys/errno.h>
     42   1.1       cgd #include <sys/ioctl.h>
     43   1.1       cgd #include <sys/buf.h>
     44   1.1       cgd #include <sys/proc.h>
     45   1.1       cgd #include <sys/user.h>
     46  1.20   thorpej #include <sys/chio.h>
     47   1.6   mycroft #include <sys/device.h>
     48  1.20   thorpej #include <sys/malloc.h>
     49  1.21  christos #include <sys/conf.h>
     50  1.22   thorpej #include <sys/fcntl.h>
     51   1.1       cgd 
     52  1.27    bouyer #include <dev/scsipi/scsipi_all.h>
     53  1.27    bouyer #include <dev/scsipi/scsi_all.h>
     54  1.27    bouyer #include <dev/scsipi/scsi_changer.h>
     55  1.27    bouyer #include <dev/scsipi/scsiconf.h>
     56   1.1       cgd 
     57  1.20   thorpej #define CHRETRIES	2
     58  1.20   thorpej #define CHUNIT(x)	(minor((x)))
     59  1.20   thorpej 
     60  1.20   thorpej struct ch_softc {
     61  1.20   thorpej 	struct device	sc_dev;		/* generic device info */
     62  1.27    bouyer 	struct scsipi_link *sc_link;	/* link in the SCSI bus */
     63  1.20   thorpej 
     64  1.20   thorpej 	int		sc_picker;	/* current picker */
     65   1.1       cgd 
     66  1.20   thorpej 	/*
     67  1.20   thorpej 	 * The following information is obtained from the
     68  1.20   thorpej 	 * element address assignment page.
     69  1.20   thorpej 	 */
     70  1.20   thorpej 	int		sc_firsts[4];	/* firsts, indexed by CHET_* */
     71  1.20   thorpej 	int		sc_counts[4];	/* counts, indexed by CHET_* */
     72   1.1       cgd 
     73  1.20   thorpej 	/*
     74  1.20   thorpej 	 * The following mask defines the legal combinations
     75  1.20   thorpej 	 * of elements for the MOVE MEDIUM command.
     76  1.20   thorpej 	 */
     77  1.20   thorpej 	u_int8_t	sc_movemask[4];
     78  1.20   thorpej 
     79  1.20   thorpej 	/*
     80  1.20   thorpej 	 * As above, but for EXCHANGE MEDIUM.
     81  1.20   thorpej 	 */
     82  1.20   thorpej 	u_int8_t	sc_exchangemask[4];
     83   1.1       cgd 
     84  1.20   thorpej 	int		flags;		/* misc. info */
     85  1.26   thorpej 
     86  1.26   thorpej 	/*
     87  1.26   thorpej 	 * Quirks; see below.
     88  1.26   thorpej 	 */
     89  1.26   thorpej 	int		sc_settledelay;	/* delay for settle */
     90  1.26   thorpej 
     91   1.6   mycroft };
     92   1.6   mycroft 
     93  1.20   thorpej /* sc_flags */
     94  1.20   thorpej #define CHF_ROTATE	0x01		/* picker can rotate */
     95  1.20   thorpej 
     96  1.20   thorpej /* Autoconfiguration glue */
     97  1.25       cgd int	chmatch __P((struct device *, struct cfdata *, void *));
     98  1.15  christos void	chattach __P((struct device *, struct device *, void *));
     99   1.6   mycroft 
    100  1.17   thorpej struct cfattach ch_ca = {
    101  1.17   thorpej 	sizeof(struct ch_softc), chmatch, chattach
    102  1.17   thorpej };
    103  1.17   thorpej 
    104  1.32   thorpej extern struct cfdriver ch_cd;
    105   1.1       cgd 
    106  1.27    bouyer struct scsipi_inquiry_pattern ch_patterns[] = {
    107  1.20   thorpej 	{T_CHANGER, T_REMOV,
    108  1.20   thorpej 	 "",		"",		""},
    109  1.20   thorpej };
    110  1.20   thorpej 
    111  1.20   thorpej /* SCSI glue */
    112  1.27    bouyer struct scsipi_device ch_switch = {
    113  1.20   thorpej 	NULL, NULL, NULL, NULL
    114   1.6   mycroft };
    115   1.6   mycroft 
    116  1.20   thorpej int	ch_move __P((struct ch_softc *, struct changer_move *));
    117  1.20   thorpej int	ch_exchange __P((struct ch_softc *, struct changer_exchange *));
    118  1.20   thorpej int	ch_position __P((struct ch_softc *, struct changer_position *));
    119  1.28    mjacob int	ch_ielem __P((struct ch_softc *));
    120  1.20   thorpej int	ch_usergetelemstatus __P((struct ch_softc *, int, u_int8_t *));
    121  1.20   thorpej int	ch_getelemstatus __P((struct ch_softc *, int, int, caddr_t, size_t));
    122  1.20   thorpej int	ch_get_params __P((struct ch_softc *, int));
    123  1.29     enami void	ch_get_quirks __P((struct ch_softc *,
    124  1.29     enami 	    struct scsipi_inquiry_pattern *));
    125  1.26   thorpej 
    126  1.26   thorpej /*
    127  1.26   thorpej  * SCSI changer quirks.
    128  1.26   thorpej  */
    129  1.26   thorpej struct chquirk {
    130  1.27    bouyer 	struct	scsipi_inquiry_pattern cq_match; /* device id pattern */
    131  1.26   thorpej 	int	cq_settledelay;	/* settle delay, in seconds */
    132  1.26   thorpej };
    133  1.26   thorpej 
    134  1.26   thorpej struct chquirk chquirks[] = {
    135  1.26   thorpej 	{{T_CHANGER, T_REMOV,
    136  1.26   thorpej 	  "SPECTRA",	"9000",		"0200"},
    137  1.26   thorpej 	 75},
    138  1.26   thorpej };
    139  1.13   mycroft 
    140  1.13   mycroft int
    141  1.13   mycroft chmatch(parent, match, aux)
    142  1.13   mycroft 	struct device *parent;
    143  1.25       cgd 	struct cfdata *match;
    144  1.25       cgd 	void *aux;
    145  1.13   mycroft {
    146  1.27    bouyer 	struct scsipibus_attach_args *sa = aux;
    147  1.13   mycroft 	int priority;
    148  1.13   mycroft 
    149  1.27    bouyer 	(void)scsipi_inqmatch(&sa->sa_inqbuf,
    150  1.29     enami 	    (caddr_t)ch_patterns, sizeof(ch_patterns) / sizeof(ch_patterns[0]),
    151  1.13   mycroft 	    sizeof(ch_patterns[0]), &priority);
    152  1.20   thorpej 
    153  1.13   mycroft 	return (priority);
    154  1.13   mycroft }
    155  1.13   mycroft 
    156  1.20   thorpej void
    157   1.6   mycroft chattach(parent, self, aux)
    158   1.6   mycroft 	struct device *parent, *self;
    159   1.6   mycroft 	void *aux;
    160   1.1       cgd {
    161  1.20   thorpej 	struct ch_softc *sc = (struct ch_softc *)self;
    162  1.27    bouyer 	struct scsipibus_attach_args *sa = aux;
    163  1.27    bouyer 	struct scsipi_link *link = sa->sa_sc_link;
    164  1.20   thorpej 
    165  1.20   thorpej 	/* Glue into the SCSI bus */
    166  1.20   thorpej 	sc->sc_link = link;
    167  1.20   thorpej 	link->device = &ch_switch;
    168  1.20   thorpej 	link->device_softc = sc;
    169  1.20   thorpej 	link->openings = 1;
    170   1.1       cgd 
    171  1.24  christos 	printf("\n");
    172   1.1       cgd 
    173   1.6   mycroft 	/*
    174  1.26   thorpej 	 * Find out our device's quirks.
    175  1.26   thorpej 	 */
    176  1.27    bouyer 	ch_get_quirks(sc, &sa->sa_inqbuf);
    177  1.26   thorpej 
    178  1.26   thorpej 	/*
    179  1.26   thorpej 	 * Some changers require a long time to settle out, to do
    180  1.26   thorpej 	 * tape inventory, for instance.
    181  1.26   thorpej 	 */
    182  1.26   thorpej 	if (sc->sc_settledelay) {
    183  1.26   thorpej 		printf("%s: waiting %d seconds for changer to settle...\n",
    184  1.26   thorpej 		    sc->sc_dev.dv_xname, sc->sc_settledelay);
    185  1.26   thorpej 		delay(1000000 * sc->sc_settledelay);
    186  1.26   thorpej 	}
    187  1.26   thorpej 
    188  1.26   thorpej 	/*
    189  1.20   thorpej 	 * Get information about the device.  Note we can't use
    190  1.20   thorpej 	 * interrupts yet.
    191   1.6   mycroft 	 */
    192  1.20   thorpej 	if (ch_get_params(sc, SCSI_AUTOCONF))
    193  1.24  christos 		printf("%s: offline\n", sc->sc_dev.dv_xname);
    194  1.20   thorpej 	else {
    195  1.26   thorpej #define PLURAL(c)	(c) == 1 ? "" : "s"
    196  1.26   thorpej 		printf("%s: %d slot%s, %d drive%s, %d picker%s, %d portal%s\n",
    197  1.20   thorpej 		    sc->sc_dev.dv_xname,
    198  1.26   thorpej 		    sc->sc_counts[CHET_ST], PLURAL(sc->sc_counts[CHET_ST]),
    199  1.26   thorpej 		    sc->sc_counts[CHET_DT], PLURAL(sc->sc_counts[CHET_DT]),
    200  1.26   thorpej 		    sc->sc_counts[CHET_MT], PLURAL(sc->sc_counts[CHET_MT]),
    201  1.26   thorpej 		    sc->sc_counts[CHET_IE], PLURAL(sc->sc_counts[CHET_IE]));
    202  1.26   thorpej #undef PLURAL
    203  1.20   thorpej #ifdef CHANGER_DEBUG
    204  1.24  christos 		printf("%s: move mask: 0x%x 0x%x 0x%x 0x%x\n",
    205  1.20   thorpej 		    sc->sc_dev.dv_xname,
    206  1.20   thorpej 		    sc->sc_movemask[CHET_MT], sc->sc_movemask[CHET_ST],
    207  1.20   thorpej 		    sc->sc_movemask[CHET_IE], sc->sc_movemask[CHET_DT]);
    208  1.24  christos 		printf("%s: exchange mask: 0x%x 0x%x 0x%x 0x%x\n",
    209  1.20   thorpej 		    sc->sc_dev.dv_xname,
    210  1.20   thorpej 		    sc->sc_exchangemask[CHET_MT], sc->sc_exchangemask[CHET_ST],
    211  1.20   thorpej 		    sc->sc_exchangemask[CHET_IE], sc->sc_exchangemask[CHET_DT]);
    212  1.20   thorpej #endif /* CHANGER_DEBUG */
    213  1.20   thorpej 	}
    214   1.6   mycroft 
    215  1.20   thorpej 	/* Default the current picker. */
    216  1.20   thorpej 	sc->sc_picker = sc->sc_firsts[CHET_MT];
    217   1.1       cgd }
    218   1.1       cgd 
    219  1.20   thorpej int
    220  1.20   thorpej chopen(dev, flags, fmt, p)
    221   1.6   mycroft 	dev_t dev;
    222  1.20   thorpej 	int flags, fmt;
    223  1.15  christos 	struct proc *p;
    224   1.1       cgd {
    225  1.20   thorpej 	struct ch_softc *sc;
    226  1.35   thorpej 	int unit, error;
    227   1.6   mycroft 
    228   1.6   mycroft 	unit = CHUNIT(dev);
    229  1.20   thorpej 	if ((unit >= ch_cd.cd_ndevs) ||
    230  1.20   thorpej 	    ((sc = ch_cd.cd_devs[unit]) == NULL))
    231  1.20   thorpej 		return (ENXIO);
    232  1.12   mycroft 
    233  1.12   mycroft 	/*
    234  1.20   thorpej 	 * Only allow one open at a time.
    235  1.12   mycroft 	 */
    236  1.20   thorpej 	if (sc->sc_link->flags & SDEV_OPEN)
    237  1.20   thorpej 		return (EBUSY);
    238  1.20   thorpej 
    239  1.35   thorpej 	if ((error = scsipi_adapter_addref(sc->sc_link)) != 0)
    240  1.35   thorpej 		return (error);
    241  1.35   thorpej 
    242  1.20   thorpej 	sc->sc_link->flags |= SDEV_OPEN;
    243   1.6   mycroft 
    244   1.6   mycroft 	/*
    245  1.20   thorpej 	 * Absorb any unit attention errors.  Ignore "not ready"
    246  1.20   thorpej 	 * since this might occur if e.g. a tape isn't actually
    247  1.20   thorpej 	 * loaded in the drive.
    248   1.6   mycroft 	 */
    249  1.27    bouyer 	error = scsipi_test_unit_ready(sc->sc_link,
    250  1.21  christos 	    SCSI_IGNORE_NOT_READY|SCSI_IGNORE_MEDIA_CHANGE);
    251  1.21  christos 	if (error)
    252  1.13   mycroft 		goto bad;
    253   1.6   mycroft 
    254   1.6   mycroft 	/*
    255  1.20   thorpej 	 * Make sure our parameters are up to date.
    256   1.6   mycroft 	 */
    257  1.21  christos 	if ((error = ch_get_params(sc, 0)) != 0)
    258  1.12   mycroft 		goto bad;
    259   1.6   mycroft 
    260  1.20   thorpej 	return (0);
    261  1.12   mycroft 
    262  1.20   thorpej  bad:
    263  1.35   thorpej 	scsipi_adapter_delref(sc->sc_link);
    264  1.20   thorpej 	sc->sc_link->flags &= ~SDEV_OPEN;
    265  1.20   thorpej 	return (error);
    266   1.6   mycroft }
    267   1.6   mycroft 
    268  1.20   thorpej int
    269  1.20   thorpej chclose(dev, flags, fmt, p)
    270   1.6   mycroft 	dev_t dev;
    271  1.20   thorpej 	int flags, fmt;
    272  1.15  christos 	struct proc *p;
    273   1.1       cgd {
    274  1.20   thorpej 	struct ch_softc *sc = ch_cd.cd_devs[CHUNIT(dev)];
    275   1.6   mycroft 
    276  1.35   thorpej 	scsipi_adapter_delref(sc->sc_link);
    277  1.20   thorpej 	sc->sc_link->flags &= ~SDEV_OPEN;
    278  1.20   thorpej 	return (0);
    279   1.6   mycroft }
    280   1.6   mycroft 
    281  1.20   thorpej int
    282  1.20   thorpej chioctl(dev, cmd, data, flags, p)
    283   1.6   mycroft 	dev_t dev;
    284  1.10       cgd 	u_long cmd;
    285  1.20   thorpej 	caddr_t data;
    286  1.20   thorpej 	int flags;
    287  1.13   mycroft 	struct proc *p;
    288   1.6   mycroft {
    289  1.20   thorpej 	struct ch_softc *sc = ch_cd.cd_devs[CHUNIT(dev)];
    290  1.20   thorpej 	int error = 0;
    291  1.22   thorpej 
    292  1.22   thorpej 	/*
    293  1.22   thorpej 	 * If this command can change the device's state, we must
    294  1.22   thorpej 	 * have the device open for writing.
    295  1.22   thorpej 	 */
    296  1.22   thorpej 	switch (cmd) {
    297  1.22   thorpej 	case CHIOGPICKER:
    298  1.22   thorpej 	case CHIOGPARAMS:
    299  1.22   thorpej 	case CHIOGSTATUS:
    300  1.22   thorpej 		break;
    301  1.22   thorpej 
    302  1.22   thorpej 	default:
    303  1.22   thorpej 		if ((flags & FWRITE) == 0)
    304  1.22   thorpej 			return (EBADF);
    305  1.22   thorpej 	}
    306  1.20   thorpej 
    307  1.20   thorpej 	switch (cmd) {
    308  1.20   thorpej 	case CHIOMOVE:
    309  1.20   thorpej 		error = ch_move(sc, (struct changer_move *)data);
    310  1.20   thorpej 		break;
    311  1.20   thorpej 
    312  1.20   thorpej 	case CHIOEXCHANGE:
    313  1.20   thorpej 		error = ch_exchange(sc, (struct changer_exchange *)data);
    314  1.20   thorpej 		break;
    315  1.20   thorpej 
    316  1.20   thorpej 	case CHIOPOSITION:
    317  1.20   thorpej 		error = ch_position(sc, (struct changer_position *)data);
    318  1.20   thorpej 		break;
    319  1.20   thorpej 
    320  1.20   thorpej 	case CHIOGPICKER:
    321  1.20   thorpej 		*(int *)data = sc->sc_picker - sc->sc_firsts[CHET_MT];
    322  1.20   thorpej 		break;
    323  1.20   thorpej 
    324  1.20   thorpej 	case CHIOSPICKER:	{
    325  1.20   thorpej 		int new_picker = *(int *)data;
    326  1.20   thorpej 
    327  1.20   thorpej 		if (new_picker > (sc->sc_counts[CHET_MT] - 1))
    328  1.20   thorpej 			return (EINVAL);
    329  1.20   thorpej 		sc->sc_picker = sc->sc_firsts[CHET_MT] + new_picker;
    330  1.20   thorpej 		break;		}
    331  1.20   thorpej 
    332  1.20   thorpej 	case CHIOGPARAMS:	{
    333  1.20   thorpej 		struct changer_params *cp = (struct changer_params *)data;
    334  1.20   thorpej 
    335  1.20   thorpej 		cp->cp_curpicker = sc->sc_picker - sc->sc_firsts[CHET_MT];
    336  1.20   thorpej 		cp->cp_npickers = sc->sc_counts[CHET_MT];
    337  1.20   thorpej 		cp->cp_nslots = sc->sc_counts[CHET_ST];
    338  1.20   thorpej 		cp->cp_nportals = sc->sc_counts[CHET_IE];
    339  1.20   thorpej 		cp->cp_ndrives = sc->sc_counts[CHET_DT];
    340  1.20   thorpej 		break;		}
    341  1.20   thorpej 
    342  1.28    mjacob 	case CHIOIELEM:
    343  1.28    mjacob 		error = ch_ielem(sc);
    344  1.28    mjacob 		break;
    345  1.28    mjacob 
    346  1.20   thorpej 	case CHIOGSTATUS:	{
    347  1.20   thorpej 		struct changer_element_status *ces =
    348  1.20   thorpej 		    (struct changer_element_status *)data;
    349  1.20   thorpej 
    350  1.20   thorpej 		error = ch_usergetelemstatus(sc, ces->ces_type, ces->ces_data);
    351  1.20   thorpej 		break;		}
    352   1.6   mycroft 
    353  1.20   thorpej 	/* Implement prevent/allow? */
    354   1.6   mycroft 
    355   1.1       cgd 	default:
    356  1.27    bouyer 		error = scsipi_do_ioctl(sc->sc_link, dev, cmd, data, flags, p);
    357  1.20   thorpej 		break;
    358   1.1       cgd 	}
    359  1.20   thorpej 
    360  1.20   thorpej 	return (error);
    361   1.1       cgd }
    362   1.1       cgd 
    363  1.20   thorpej int
    364  1.20   thorpej ch_move(sc, cm)
    365  1.20   thorpej 	struct ch_softc *sc;
    366  1.20   thorpej 	struct changer_move *cm;
    367   1.1       cgd {
    368  1.20   thorpej 	struct scsi_move_medium cmd;
    369  1.20   thorpej 	u_int16_t fromelem, toelem;
    370  1.20   thorpej 
    371  1.20   thorpej 	/*
    372  1.20   thorpej 	 * Check arguments.
    373  1.20   thorpej 	 */
    374  1.20   thorpej 	if ((cm->cm_fromtype > CHET_DT) || (cm->cm_totype > CHET_DT))
    375  1.20   thorpej 		return (EINVAL);
    376  1.20   thorpej 	if ((cm->cm_fromunit > (sc->sc_counts[cm->cm_fromtype] - 1)) ||
    377  1.20   thorpej 	    (cm->cm_tounit > (sc->sc_counts[cm->cm_totype] - 1)))
    378  1.20   thorpej 		return (ENODEV);
    379  1.20   thorpej 
    380  1.20   thorpej 	/*
    381  1.20   thorpej 	 * Check the request against the changer's capabilities.
    382  1.20   thorpej 	 */
    383  1.20   thorpej 	if ((sc->sc_movemask[cm->cm_fromtype] & (1 << cm->cm_totype)) == 0)
    384  1.20   thorpej 		return (EINVAL);
    385  1.20   thorpej 
    386  1.20   thorpej 	/*
    387  1.20   thorpej 	 * Calculate the source and destination elements.
    388  1.20   thorpej 	 */
    389  1.20   thorpej 	fromelem = sc->sc_firsts[cm->cm_fromtype] + cm->cm_fromunit;
    390  1.20   thorpej 	toelem = sc->sc_firsts[cm->cm_totype] + cm->cm_tounit;
    391  1.20   thorpej 
    392  1.20   thorpej 	/*
    393  1.20   thorpej 	 * Build the SCSI command.
    394  1.20   thorpej 	 */
    395  1.20   thorpej 	bzero(&cmd, sizeof(cmd));
    396  1.20   thorpej 	cmd.opcode = MOVE_MEDIUM;
    397  1.20   thorpej 	_lto2b(sc->sc_picker, cmd.tea);
    398  1.20   thorpej 	_lto2b(fromelem, cmd.src);
    399  1.20   thorpej 	_lto2b(toelem, cmd.dst);
    400  1.20   thorpej 	if (cm->cm_flags & CM_INVERT)
    401  1.20   thorpej 		cmd.flags |= MOVE_MEDIUM_INVERT;
    402  1.20   thorpej 
    403  1.20   thorpej 	/*
    404  1.20   thorpej 	 * Send command to changer.
    405  1.20   thorpej 	 */
    406  1.31   thorpej 	return (scsipi_command(sc->sc_link,
    407  1.29     enami 	    (struct scsipi_generic *)&cmd, sizeof(cmd), NULL, 0, CHRETRIES,
    408  1.29     enami 	    100000, NULL, 0));
    409   1.1       cgd }
    410   1.1       cgd 
    411  1.20   thorpej int
    412  1.20   thorpej ch_exchange(sc, ce)
    413  1.20   thorpej 	struct ch_softc *sc;
    414  1.20   thorpej 	struct changer_exchange *ce;
    415  1.20   thorpej {
    416  1.20   thorpej 	struct scsi_exchange_medium cmd;
    417  1.20   thorpej 	u_int16_t src, dst1, dst2;
    418  1.20   thorpej 
    419  1.20   thorpej 	/*
    420  1.20   thorpej 	 * Check arguments.
    421  1.20   thorpej 	 */
    422  1.20   thorpej 	if ((ce->ce_srctype > CHET_DT) || (ce->ce_fdsttype > CHET_DT) ||
    423  1.20   thorpej 	    (ce->ce_sdsttype > CHET_DT))
    424  1.20   thorpej 		return (EINVAL);
    425  1.20   thorpej 	if ((ce->ce_srcunit > (sc->sc_counts[ce->ce_srctype] - 1)) ||
    426  1.20   thorpej 	    (ce->ce_fdstunit > (sc->sc_counts[ce->ce_fdsttype] - 1)) ||
    427  1.20   thorpej 	    (ce->ce_sdstunit > (sc->sc_counts[ce->ce_sdsttype] - 1)))
    428  1.20   thorpej 		return (ENODEV);
    429  1.20   thorpej 
    430  1.20   thorpej 	/*
    431  1.20   thorpej 	 * Check the request against the changer's capabilities.
    432  1.20   thorpej 	 */
    433  1.20   thorpej 	if (((sc->sc_exchangemask[ce->ce_srctype] &
    434  1.20   thorpej 	     (1 << ce->ce_fdsttype)) == 0) ||
    435  1.20   thorpej 	    ((sc->sc_exchangemask[ce->ce_fdsttype] &
    436  1.20   thorpej 	     (1 << ce->ce_sdsttype)) == 0))
    437  1.20   thorpej 		return (EINVAL);
    438  1.20   thorpej 
    439  1.20   thorpej 	/*
    440  1.20   thorpej 	 * Calculate the source and destination elements.
    441  1.20   thorpej 	 */
    442  1.20   thorpej 	src = sc->sc_firsts[ce->ce_srctype] + ce->ce_srcunit;
    443  1.20   thorpej 	dst1 = sc->sc_firsts[ce->ce_fdsttype] + ce->ce_fdstunit;
    444  1.20   thorpej 	dst2 = sc->sc_firsts[ce->ce_sdsttype] + ce->ce_sdstunit;
    445  1.20   thorpej 
    446  1.20   thorpej 	/*
    447  1.20   thorpej 	 * Build the SCSI command.
    448  1.20   thorpej 	 */
    449  1.20   thorpej 	bzero(&cmd, sizeof(cmd));
    450  1.20   thorpej 	cmd.opcode = EXCHANGE_MEDIUM;
    451  1.20   thorpej 	_lto2b(sc->sc_picker, cmd.tea);
    452  1.20   thorpej 	_lto2b(src, cmd.src);
    453  1.20   thorpej 	_lto2b(dst1, cmd.fdst);
    454  1.20   thorpej 	_lto2b(dst2, cmd.sdst);
    455  1.20   thorpej 	if (ce->ce_flags & CE_INVERT1)
    456  1.20   thorpej 		cmd.flags |= EXCHANGE_MEDIUM_INV1;
    457  1.20   thorpej 	if (ce->ce_flags & CE_INVERT2)
    458  1.20   thorpej 		cmd.flags |= EXCHANGE_MEDIUM_INV2;
    459  1.20   thorpej 
    460  1.20   thorpej 	/*
    461  1.20   thorpej 	 * Send command to changer.
    462  1.20   thorpej 	 */
    463  1.31   thorpej 	return (scsipi_command(sc->sc_link,
    464  1.29     enami 	    (struct scsipi_generic *)&cmd, sizeof(cmd), NULL, 0, CHRETRIES,
    465  1.29     enami 	    100000, NULL, 0));
    466   1.1       cgd }
    467   1.1       cgd 
    468  1.20   thorpej int
    469  1.20   thorpej ch_position(sc, cp)
    470  1.20   thorpej 	struct ch_softc *sc;
    471  1.20   thorpej 	struct changer_position *cp;
    472  1.20   thorpej {
    473  1.20   thorpej 	struct scsi_position_to_element cmd;
    474  1.20   thorpej 	u_int16_t dst;
    475  1.20   thorpej 
    476  1.20   thorpej 	/*
    477  1.20   thorpej 	 * Check arguments.
    478  1.20   thorpej 	 */
    479  1.20   thorpej 	if (cp->cp_type > CHET_DT)
    480  1.20   thorpej 		return (EINVAL);
    481  1.20   thorpej 	if (cp->cp_unit > (sc->sc_counts[cp->cp_type] - 1))
    482  1.20   thorpej 		return (ENODEV);
    483  1.20   thorpej 
    484  1.20   thorpej 	/*
    485  1.20   thorpej 	 * Calculate the destination element.
    486  1.20   thorpej 	 */
    487  1.20   thorpej 	dst = sc->sc_firsts[cp->cp_type] + cp->cp_unit;
    488  1.20   thorpej 
    489  1.20   thorpej 	/*
    490  1.20   thorpej 	 * Build the SCSI command.
    491  1.20   thorpej 	 */
    492  1.20   thorpej 	bzero(&cmd, sizeof(cmd));
    493  1.20   thorpej 	cmd.opcode = POSITION_TO_ELEMENT;
    494  1.20   thorpej 	_lto2b(sc->sc_picker, cmd.tea);
    495  1.20   thorpej 	_lto2b(dst, cmd.dst);
    496  1.20   thorpej 	if (cp->cp_flags & CP_INVERT)
    497  1.20   thorpej 		cmd.flags |= POSITION_TO_ELEMENT_INVERT;
    498  1.20   thorpej 
    499  1.20   thorpej 	/*
    500  1.20   thorpej 	 * Send command to changer.
    501  1.20   thorpej 	 */
    502  1.31   thorpej 	return (scsipi_command(sc->sc_link,
    503  1.29     enami 	    (struct scsipi_generic *)&cmd, sizeof(cmd), NULL, 0, CHRETRIES,
    504  1.29     enami 	    100000, NULL, 0));
    505   1.1       cgd }
    506   1.1       cgd 
    507   1.6   mycroft /*
    508  1.20   thorpej  * Perform a READ ELEMENT STATUS on behalf of the user, and return to
    509  1.20   thorpej  * the user only the data the user is interested in (i.e. an array of
    510  1.20   thorpej  * flags bytes).
    511   1.6   mycroft  */
    512  1.20   thorpej int
    513  1.20   thorpej ch_usergetelemstatus(sc, chet, uptr)
    514  1.20   thorpej 	struct ch_softc *sc;
    515  1.20   thorpej 	int chet;
    516  1.20   thorpej 	u_int8_t *uptr;
    517  1.20   thorpej {
    518  1.20   thorpej 	struct read_element_status_header *st_hdr;
    519  1.20   thorpej 	struct read_element_status_page_header *pg_hdr;
    520  1.20   thorpej 	struct read_element_status_descriptor *desc;
    521  1.20   thorpej 	caddr_t data = NULL;
    522  1.20   thorpej 	size_t size, desclen;
    523  1.20   thorpej 	int avail, i, error = 0;
    524  1.20   thorpej 	u_int8_t *user_data = NULL;
    525  1.20   thorpej 
    526  1.20   thorpej 	/*
    527  1.20   thorpej 	 * If there are no elements of the requested type in the changer,
    528  1.20   thorpej 	 * the request is invalid.
    529  1.20   thorpej 	 */
    530  1.20   thorpej 	if (sc->sc_counts[chet] == 0)
    531  1.20   thorpej 		return (EINVAL);
    532  1.20   thorpej 
    533  1.20   thorpej 	/*
    534  1.20   thorpej 	 * Request one descriptor for the given element type.  This
    535  1.20   thorpej 	 * is used to determine the size of the descriptor so that
    536  1.20   thorpej 	 * we can allocate enough storage for all of them.  We assume
    537  1.20   thorpej 	 * that the first one can fit into 1k.
    538  1.20   thorpej 	 */
    539  1.20   thorpej 	data = (caddr_t)malloc(1024, M_DEVBUF, M_WAITOK);
    540  1.21  christos 	error = ch_getelemstatus(sc, sc->sc_firsts[chet], 1, data, 1024);
    541  1.21  christos 	if (error)
    542  1.20   thorpej 		goto done;
    543  1.20   thorpej 
    544  1.20   thorpej 	st_hdr = (struct read_element_status_header *)data;
    545  1.20   thorpej 	pg_hdr = (struct read_element_status_page_header *)((u_long)st_hdr +
    546  1.20   thorpej 	    sizeof(struct read_element_status_header));
    547  1.20   thorpej 	desclen = _2btol(pg_hdr->edl);
    548  1.20   thorpej 
    549  1.20   thorpej 	size = sizeof(struct read_element_status_header) +
    550  1.20   thorpej 	    sizeof(struct read_element_status_page_header) +
    551  1.20   thorpej 	    (desclen * sc->sc_counts[chet]);
    552  1.20   thorpej 
    553  1.20   thorpej 	/*
    554  1.20   thorpej 	 * Reallocate storage for descriptors and get them from the
    555  1.20   thorpej 	 * device.
    556  1.20   thorpej 	 */
    557  1.20   thorpej 	free(data, M_DEVBUF);
    558  1.20   thorpej 	data = (caddr_t)malloc(size, M_DEVBUF, M_WAITOK);
    559  1.21  christos 	error = ch_getelemstatus(sc, sc->sc_firsts[chet],
    560  1.21  christos 	    sc->sc_counts[chet], data, size);
    561  1.21  christos 	if (error)
    562  1.20   thorpej 		goto done;
    563  1.20   thorpej 
    564  1.20   thorpej 	/*
    565  1.20   thorpej 	 * Fill in the user status array.
    566  1.20   thorpej 	 */
    567  1.20   thorpej 	st_hdr = (struct read_element_status_header *)data;
    568  1.20   thorpej 	avail = _2btol(st_hdr->count);
    569  1.28    mjacob 
    570  1.20   thorpej 	if (avail != sc->sc_counts[chet])
    571  1.24  christos 		printf("%s: warning, READ ELEMENT STATUS avail != count\n",
    572  1.20   thorpej 		    sc->sc_dev.dv_xname);
    573  1.20   thorpej 
    574  1.20   thorpej 	user_data = (u_int8_t *)malloc(avail, M_DEVBUF, M_WAITOK);
    575  1.20   thorpej 
    576  1.20   thorpej 	desc = (struct read_element_status_descriptor *)((u_long)data +
    577  1.20   thorpej 	    sizeof(struct read_element_status_header) +
    578  1.20   thorpej 	    sizeof(struct read_element_status_page_header));
    579  1.20   thorpej 	for (i = 0; i < avail; ++i) {
    580  1.20   thorpej 		user_data[i] = desc->flags1;
    581  1.20   thorpej 		(u_long)desc += desclen;
    582  1.20   thorpej 	}
    583  1.20   thorpej 
    584  1.20   thorpej 	/* Copy flags array out to userspace. */
    585  1.20   thorpej 	error = copyout(user_data, uptr, avail);
    586  1.20   thorpej 
    587  1.20   thorpej  done:
    588  1.20   thorpej 	if (data != NULL)
    589  1.20   thorpej 		free(data, M_DEVBUF);
    590  1.20   thorpej 	if (user_data != NULL)
    591  1.20   thorpej 		free(user_data, M_DEVBUF);
    592  1.20   thorpej 	return (error);
    593  1.20   thorpej }
    594  1.20   thorpej 
    595  1.20   thorpej int
    596  1.20   thorpej ch_getelemstatus(sc, first, count, data, datalen)
    597  1.20   thorpej 	struct ch_softc *sc;
    598  1.20   thorpej 	int first, count;
    599  1.20   thorpej 	caddr_t data;
    600  1.20   thorpej 	size_t datalen;
    601   1.1       cgd {
    602  1.20   thorpej 	struct scsi_read_element_status cmd;
    603   1.6   mycroft 
    604   1.6   mycroft 	/*
    605  1.20   thorpej 	 * Build SCSI command.
    606   1.6   mycroft 	 */
    607  1.20   thorpej 	bzero(&cmd, sizeof(cmd));
    608  1.20   thorpej 	cmd.opcode = READ_ELEMENT_STATUS;
    609  1.20   thorpej 	_lto2b(first, cmd.sea);
    610  1.20   thorpej 	_lto2b(count, cmd.count);
    611  1.20   thorpej 	_lto3b(datalen, cmd.len);
    612   1.6   mycroft 
    613   1.6   mycroft 	/*
    614  1.20   thorpej 	 * Send command to changer.
    615   1.6   mycroft 	 */
    616  1.31   thorpej 	return (scsipi_command(sc->sc_link,
    617  1.29     enami 	    (struct scsipi_generic *)&cmd, sizeof(cmd),
    618  1.30   thorpej 	    (u_char *)data, datalen, CHRETRIES, 100000, NULL, SCSI_DATA_IN));
    619  1.20   thorpej }
    620  1.20   thorpej 
    621  1.20   thorpej 
    622  1.28    mjacob int
    623  1.28    mjacob ch_ielem(sc)
    624  1.28    mjacob 	struct ch_softc *sc;
    625  1.28    mjacob {
    626  1.33    mjacob 	int tmo;
    627  1.28    mjacob 	struct scsi_initialize_element_status cmd;
    628  1.28    mjacob 
    629  1.28    mjacob 	/*
    630  1.28    mjacob 	 * Build SCSI command.
    631  1.28    mjacob 	 */
    632  1.28    mjacob 	bzero(&cmd, sizeof(cmd));
    633  1.28    mjacob 	cmd.opcode = INITIALIZE_ELEMENT_STATUS;
    634  1.28    mjacob 
    635  1.28    mjacob 	/*
    636  1.28    mjacob 	 * Send command to changer.
    637  1.33    mjacob 	 *
    638  1.33    mjacob 	 * The problem is, how long to allow for the command?
    639  1.33    mjacob 	 * It can take a *really* long time, and also depends
    640  1.33    mjacob 	 * on unknowable factors such as whether there are
    641  1.33    mjacob 	 * *almost* readable labels on tapes that a barcode
    642  1.33    mjacob 	 * reader is trying to decipher.
    643  1.33    mjacob 	 *
    644  1.33    mjacob 	 * I'm going to make this long enough to allow 5 minutes
    645  1.33    mjacob 	 * per element plus an initial 10 minute wait.
    646  1.33    mjacob 	 */
    647  1.33    mjacob 	tmo =	sc->sc_counts[CHET_MT] +
    648  1.33    mjacob 		sc->sc_counts[CHET_ST] +
    649  1.33    mjacob 		sc->sc_counts[CHET_IE] +
    650  1.33    mjacob 		sc->sc_counts[CHET_DT];
    651  1.33    mjacob 	tmo *= 5 * 1000;
    652  1.33    mjacob 	tmo += (10 * 60 * 1000);
    653  1.33    mjacob 
    654  1.31   thorpej 	return (scsipi_command(sc->sc_link,
    655  1.29     enami 	    (struct scsipi_generic *)&cmd, sizeof(cmd),
    656  1.33    mjacob 	    NULL, 0, CHRETRIES, tmo, NULL, 0));
    657  1.28    mjacob }
    658  1.28    mjacob 
    659  1.20   thorpej /*
    660  1.20   thorpej  * Ask the device about itself and fill in the parameters in our
    661  1.20   thorpej  * softc.
    662  1.20   thorpej  */
    663  1.20   thorpej int
    664  1.20   thorpej ch_get_params(sc, scsiflags)
    665  1.20   thorpej 	struct ch_softc *sc;
    666  1.20   thorpej 	int scsiflags;
    667  1.20   thorpej {
    668  1.20   thorpej 	struct scsi_mode_sense cmd;
    669  1.20   thorpej 	struct scsi_mode_sense_data {
    670  1.20   thorpej 		struct scsi_mode_header header;
    671  1.20   thorpej 		union {
    672  1.20   thorpej 			struct page_element_address_assignment ea;
    673  1.20   thorpej 			struct page_transport_geometry_parameters tg;
    674  1.20   thorpej 			struct page_device_capabilities cap;
    675  1.20   thorpej 		} pages;
    676  1.20   thorpej 	} sense_data;
    677  1.20   thorpej 	int error, from;
    678  1.20   thorpej 	u_int8_t *moves, *exchanges;
    679   1.6   mycroft 
    680   1.6   mycroft 	/*
    681  1.20   thorpej 	 * Grab info from the element address assignment page.
    682   1.6   mycroft 	 */
    683  1.20   thorpej 	bzero(&cmd, sizeof(cmd));
    684  1.20   thorpej 	bzero(&sense_data, sizeof(sense_data));
    685  1.27    bouyer 	cmd.opcode = SCSI_MODE_SENSE;
    686  1.20   thorpej 	cmd.byte2 |= 0x08;	/* disable block descriptors */
    687  1.20   thorpej 	cmd.page = 0x1d;
    688  1.20   thorpej 	cmd.length = (sizeof(sense_data) & 0xff);
    689  1.31   thorpej 	error = scsipi_command(sc->sc_link,
    690  1.29     enami 	    (struct scsipi_generic *)&cmd, sizeof(cmd), (u_char *)&sense_data,
    691  1.29     enami 	    sizeof(sense_data), CHRETRIES, 6000, NULL,
    692  1.29     enami 	    scsiflags | SCSI_DATA_IN);
    693  1.15  christos 	if (error) {
    694  1.24  christos 		printf("%s: could not sense element address page\n",
    695  1.21  christos 		    sc->sc_dev.dv_xname);
    696  1.20   thorpej 		return (error);
    697   1.6   mycroft 	}
    698   1.6   mycroft 
    699  1.20   thorpej 	sc->sc_firsts[CHET_MT] = _2btol(sense_data.pages.ea.mtea);
    700  1.20   thorpej 	sc->sc_counts[CHET_MT] = _2btol(sense_data.pages.ea.nmte);
    701  1.20   thorpej 	sc->sc_firsts[CHET_ST] = _2btol(sense_data.pages.ea.fsea);
    702  1.20   thorpej 	sc->sc_counts[CHET_ST] = _2btol(sense_data.pages.ea.nse);
    703  1.20   thorpej 	sc->sc_firsts[CHET_IE] = _2btol(sense_data.pages.ea.fieea);
    704  1.20   thorpej 	sc->sc_counts[CHET_IE] = _2btol(sense_data.pages.ea.niee);
    705  1.20   thorpej 	sc->sc_firsts[CHET_DT] = _2btol(sense_data.pages.ea.fdtea);
    706  1.20   thorpej 	sc->sc_counts[CHET_DT] = _2btol(sense_data.pages.ea.ndte);
    707  1.20   thorpej 
    708  1.20   thorpej 	/* XXX ask for page trasport geom */
    709   1.6   mycroft 
    710   1.6   mycroft 	/*
    711  1.20   thorpej 	 * Grab info from the capabilities page.
    712  1.20   thorpej 	 */
    713  1.20   thorpej 	bzero(&cmd, sizeof(cmd));
    714  1.20   thorpej 	bzero(&sense_data, sizeof(sense_data));
    715  1.27    bouyer 	cmd.opcode = SCSI_MODE_SENSE;
    716  1.28    mjacob 	/*
    717  1.28    mjacob 	 * XXX: Note: not all changers can deal with disabled block descriptors
    718  1.28    mjacob 	 */
    719  1.28    mjacob 	cmd.byte2 = 0x08;	/* disable block descriptors */
    720  1.20   thorpej 	cmd.page = 0x1f;
    721  1.20   thorpej 	cmd.length = (sizeof(sense_data) & 0xff);
    722  1.31   thorpej 	error = scsipi_command(sc->sc_link,
    723  1.29     enami 	    (struct scsipi_generic *)&cmd, sizeof(cmd), (u_char *)&sense_data,
    724  1.29     enami 	    sizeof(sense_data), CHRETRIES, 6000, NULL,
    725  1.29     enami 	    scsiflags | SCSI_DATA_IN);
    726  1.20   thorpej 	if (error) {
    727  1.24  christos 		printf("%s: could not sense capabilities page\n",
    728  1.21  christos 		    sc->sc_dev.dv_xname);
    729  1.20   thorpej 		return (error);
    730  1.20   thorpej 	}
    731  1.20   thorpej 
    732  1.20   thorpej 	bzero(sc->sc_movemask, sizeof(sc->sc_movemask));
    733  1.20   thorpej 	bzero(sc->sc_exchangemask, sizeof(sc->sc_exchangemask));
    734  1.20   thorpej 	moves = &sense_data.pages.cap.move_from_mt;
    735  1.20   thorpej 	exchanges = &sense_data.pages.cap.exchange_with_mt;
    736  1.20   thorpej 	for (from = CHET_MT; from <= CHET_DT; ++from) {
    737  1.20   thorpej 		sc->sc_movemask[from] = moves[from];
    738  1.20   thorpej 		sc->sc_exchangemask[from] = exchanges[from];
    739   1.1       cgd 	}
    740  1.20   thorpej 
    741  1.20   thorpej 	sc->sc_link->flags |= SDEV_MEDIA_LOADED;
    742  1.20   thorpej 	return (0);
    743  1.26   thorpej }
    744  1.26   thorpej 
    745  1.26   thorpej void
    746  1.26   thorpej ch_get_quirks(sc, inqbuf)
    747  1.26   thorpej 	struct ch_softc *sc;
    748  1.27    bouyer 	struct scsipi_inquiry_pattern *inqbuf;
    749  1.26   thorpej {
    750  1.26   thorpej 	struct chquirk *match;
    751  1.26   thorpej 	int priority;
    752  1.26   thorpej 
    753  1.26   thorpej 	sc->sc_settledelay = 0;
    754  1.26   thorpej 
    755  1.27    bouyer 	match = (struct chquirk *)scsipi_inqmatch(inqbuf,
    756  1.26   thorpej 	    (caddr_t)chquirks,
    757  1.26   thorpej 	    sizeof(chquirks) / sizeof(chquirks[0]),
    758  1.26   thorpej 	    sizeof(chquirks[0]), &priority);
    759  1.29     enami 	if (priority != 0)
    760  1.26   thorpej 		sc->sc_settledelay = match->cq_settledelay;
    761   1.1       cgd }
    762