Home | History | Annotate | Line # | Download | only in scsipi
ch.c revision 1.15
      1  1.15  christos /*	$NetBSD: ch.c,v 1.15 1996/02/14 21:47:07 christos Exp $	*/
      2   1.8       cgd 
      3   1.1       cgd /*
      4   1.6   mycroft  * Copyright (c) 1994 Charles Hannum.  All rights reserved.
      5   1.6   mycroft  *
      6   1.6   mycroft  * Redistribution and use in source and binary forms, with or without
      7   1.6   mycroft  * modification, are permitted provided that the following conditions
      8   1.6   mycroft  * are met:
      9   1.6   mycroft  * 1. Redistributions of source code must retain the above copyright
     10   1.6   mycroft  *    notice, this list of conditions and the following disclaimer.
     11   1.6   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.6   mycroft  *    notice, this list of conditions and the following disclaimer in the
     13   1.6   mycroft  *    documentation and/or other materials provided with the distribution.
     14   1.6   mycroft  * 3. All advertising materials mentioning features or use of this software
     15   1.6   mycroft  *    must display the following acknowledgement:
     16   1.6   mycroft  *	This product includes software developed by Charles Hannum.
     17   1.6   mycroft  * 4. The name of the author may not be used to endorse or promote products
     18   1.6   mycroft  *    derived from this software without specific prior written permission.
     19   1.6   mycroft  *
     20   1.6   mycroft  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21   1.6   mycroft  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22   1.6   mycroft  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23   1.6   mycroft  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24   1.6   mycroft  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25   1.6   mycroft  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26   1.6   mycroft  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27   1.6   mycroft  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28   1.6   mycroft  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29   1.6   mycroft  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32   1.6   mycroft /*
     33   1.6   mycroft  * Originally written by grefen@?????
     34   1.6   mycroft  * Based on scsi drivers by Julian Elischer (julian (at) tfs.com)
     35   1.6   mycroft  */
     36   1.1       cgd 
     37   1.5   mycroft #include <sys/types.h>
     38   1.1       cgd #include <sys/param.h>
     39   1.1       cgd #include <sys/systm.h>
     40   1.1       cgd #include <sys/errno.h>
     41   1.1       cgd #include <sys/ioctl.h>
     42   1.1       cgd #include <sys/buf.h>
     43   1.1       cgd #include <sys/proc.h>
     44   1.1       cgd #include <sys/user.h>
     45   1.1       cgd #include <sys/chio.h>
     46   1.6   mycroft #include <sys/device.h>
     47   1.1       cgd 
     48   1.1       cgd #include <scsi/scsi_all.h>
     49   1.1       cgd #include <scsi/scsi_changer.h>
     50   1.1       cgd #include <scsi/scsiconf.h>
     51  1.15  christos #include <scsi/scsi_conf.h>
     52   1.1       cgd 
     53   1.6   mycroft #define	CHRETRIES	2
     54   1.1       cgd 
     55   1.6   mycroft #define CHMODE(z)	(minor(z) & 0x0f)
     56   1.6   mycroft #define CHUNIT(z)	(minor(z) >> 4)
     57   1.1       cgd 
     58  1.13   mycroft struct ch_softc {
     59   1.6   mycroft 	struct device sc_dev;
     60   1.1       cgd 
     61   1.6   mycroft 	struct scsi_link *sc_link;	/* all the inter level info */
     62  1.13   mycroft 	u_int16_t chmo;			/* Offset of first CHM */
     63  1.13   mycroft 	u_int16_t chms;			/* No. of CHM */
     64  1.13   mycroft 	u_int16_t slots;		/* No. of Storage Elements */
     65  1.13   mycroft 	u_int16_t sloto;		/* Offset of first SE */
     66  1.13   mycroft 	u_int16_t imexs;		/* No. of Import/Export Slots */
     67  1.13   mycroft 	u_int16_t imexo;		/* Offset of first IM/EX */
     68  1.13   mycroft 	u_int16_t drives;		/* No. of CTS */
     69  1.13   mycroft 	u_int16_t driveo;		/* Offset of first CTS */
     70  1.13   mycroft 	u_int16_t rot;			/* CHM can rotate */
     71  1.13   mycroft 	u_long  op_matrix;		/* possible operations */
     72  1.13   mycroft 	u_int16_t lsterr;		/* details of lasterror */
     73   1.6   mycroft 	u_char  stor;			/* posible Storage locations */
     74   1.6   mycroft };
     75   1.6   mycroft 
     76  1.15  christos int	chmatch __P((struct device *, void *, void *));
     77  1.15  christos void	chattach __P((struct device *, struct device *, void *));
     78  1.15  christos int	ch_getelem __P((struct ch_softc *, short *, int, int , char *, int));
     79  1.15  christos int	ch_move __P((struct ch_softc *, short *, int, int , int , int ));
     80  1.15  christos int	ch_position __P((struct ch_softc *, short *, int, int , int ));
     81  1.15  christos int	ch_mode_sense __P((struct ch_softc *, int));
     82   1.6   mycroft 
     83   1.6   mycroft struct cfdriver chcd = {
     84  1.13   mycroft 	NULL, "ch", chmatch, chattach, DV_DULL, sizeof(struct ch_softc)
     85   1.6   mycroft };
     86   1.1       cgd 
     87   1.6   mycroft /*
     88   1.6   mycroft  * This driver is so simple it uses all the default services
     89   1.6   mycroft  */
     90   1.6   mycroft struct scsi_device ch_switch = {
     91   1.6   mycroft 	NULL,
     92   1.6   mycroft 	NULL,
     93   1.6   mycroft 	NULL,
     94   1.6   mycroft 	NULL,
     95   1.6   mycroft };
     96   1.6   mycroft 
     97  1.13   mycroft struct scsi_inquiry_pattern ch_patterns[] = {
     98  1.13   mycroft 	{T_CHANGER, T_REMOV,
     99  1.13   mycroft 	 "",         "",                 ""},
    100  1.13   mycroft };
    101  1.13   mycroft 
    102  1.13   mycroft int
    103  1.13   mycroft chmatch(parent, match, aux)
    104  1.13   mycroft 	struct device *parent;
    105  1.13   mycroft 	void *match, *aux;
    106  1.13   mycroft {
    107  1.13   mycroft 	struct scsibus_attach_args *sa = aux;
    108  1.13   mycroft 	int priority;
    109  1.13   mycroft 
    110  1.13   mycroft 	(void)scsi_inqmatch(sa->sa_inqbuf,
    111  1.13   mycroft 	    (caddr_t)ch_patterns, sizeof(ch_patterns)/sizeof(ch_patterns[0]),
    112  1.13   mycroft 	    sizeof(ch_patterns[0]), &priority);
    113  1.13   mycroft 	return (priority);
    114  1.13   mycroft }
    115  1.13   mycroft 
    116   1.6   mycroft /*
    117   1.6   mycroft  * The routine called by the low level scsi routine when it discovers
    118   1.6   mycroft  * a device suitable for this driver.
    119   1.6   mycroft  */
    120   1.6   mycroft void
    121   1.6   mycroft chattach(parent, self, aux)
    122   1.6   mycroft 	struct device *parent, *self;
    123   1.6   mycroft 	void *aux;
    124   1.1       cgd {
    125  1.13   mycroft 	struct ch_softc *ch = (void *)self;
    126  1.13   mycroft 	struct scsibus_attach_args *sa = aux;
    127  1.13   mycroft 	struct scsi_link *sc_link = sa->sa_sc_link;
    128   1.1       cgd 
    129   1.6   mycroft 	SC_DEBUG(sc_link, SDEV_DB2, ("chattach: "));
    130   1.1       cgd 
    131   1.6   mycroft 	/*
    132   1.6   mycroft 	 * Store information needed to contact our base driver
    133   1.6   mycroft 	 */
    134   1.6   mycroft 	ch->sc_link = sc_link;
    135   1.6   mycroft 	sc_link->device = &ch_switch;
    136  1.11   mycroft 	sc_link->device_softc = ch;
    137  1.13   mycroft 	sc_link->openings = 1;
    138   1.6   mycroft 
    139   1.6   mycroft 	/*
    140   1.6   mycroft 	 * Use the subdriver to request information regarding
    141   1.6   mycroft 	 * the drive. We cannot use interrupts yet, so the
    142   1.6   mycroft 	 * request must specify this.
    143   1.6   mycroft 	 */
    144  1.13   mycroft 	if (ch_mode_sense(ch, SCSI_AUTOCONF) != 0)
    145   1.6   mycroft 		printf(": offline\n");
    146   1.7   mycroft 	else
    147   1.6   mycroft 		printf(": %d slot(s), %d drive(s), %d arm(s), %d i/e-slot(s)\n",
    148   1.6   mycroft 		    ch->slots, ch->drives, ch->chms, ch->imexs);
    149   1.1       cgd }
    150   1.1       cgd 
    151   1.6   mycroft /*
    152   1.6   mycroft  *    open the device.
    153   1.6   mycroft  */
    154   1.6   mycroft int
    155  1.15  christos chopen(dev, flags, mode, p)
    156   1.6   mycroft 	dev_t dev;
    157  1.15  christos 	int flags;
    158  1.15  christos 	int mode;
    159  1.15  christos 	struct proc *p;
    160   1.1       cgd {
    161   1.6   mycroft 	int error = 0;
    162  1.15  christos 	int unit;
    163  1.13   mycroft 	struct ch_softc *ch;
    164   1.6   mycroft 	struct scsi_link *sc_link;
    165   1.6   mycroft 
    166   1.6   mycroft 	unit = CHUNIT(dev);
    167   1.6   mycroft 	if (unit >= chcd.cd_ndevs)
    168   1.6   mycroft 		return ENXIO;
    169   1.6   mycroft 	ch = chcd.cd_devs[unit];
    170   1.7   mycroft 	if (!ch)
    171   1.6   mycroft 		return ENXIO;
    172   1.6   mycroft 
    173   1.6   mycroft 	sc_link = ch->sc_link;
    174   1.6   mycroft 
    175  1.12   mycroft 	SC_DEBUG(sc_link, SDEV_DB1,
    176  1.12   mycroft 	    ("chopen: dev=0x%x (unit %d (of %d))\n", dev, unit, chcd.cd_ndevs));
    177  1.12   mycroft 
    178  1.12   mycroft 	/*
    179  1.12   mycroft 	 * Only allow one at a time
    180  1.12   mycroft 	 */
    181  1.12   mycroft 	if (sc_link->flags & SDEV_OPEN) {
    182  1.12   mycroft 		printf("%s: already open\n", ch->sc_dev.dv_xname);
    183   1.6   mycroft 		return EBUSY;
    184  1.12   mycroft 	}
    185   1.6   mycroft 
    186   1.6   mycroft 	/*
    187   1.6   mycroft 	 * Catch any unit attention errors.
    188   1.6   mycroft 	 */
    189  1.15  christos 	error = scsi_test_unit_ready(sc_link, SCSI_IGNORE_MEDIA_CHANGE);
    190  1.15  christos 	if (error)
    191  1.13   mycroft 		goto bad;
    192   1.6   mycroft 
    193  1.13   mycroft 	sc_link->flags |= SDEV_OPEN;	/* unit attn are now errors */
    194   1.6   mycroft 
    195   1.6   mycroft 	/*
    196   1.6   mycroft 	 * Make sure data is loaded
    197   1.6   mycroft 	 */
    198  1.15  christos 	if ((error = ch_mode_sense(ch, 0)) != 0) {
    199   1.6   mycroft 		printf("%s: offline\n", ch->sc_dev.dv_xname);
    200  1.12   mycroft 		goto bad;
    201   1.6   mycroft 	}
    202   1.6   mycroft 
    203  1.12   mycroft 	SC_DEBUG(sc_link, SDEV_DB3, ("open complete\n"));
    204   1.6   mycroft 	return 0;
    205  1.12   mycroft 
    206  1.12   mycroft bad:
    207  1.12   mycroft 	sc_link->flags &= ~SDEV_OPEN;
    208  1.12   mycroft 	return error;
    209   1.6   mycroft }
    210   1.6   mycroft 
    211   1.6   mycroft /*
    212   1.6   mycroft  * close the device.. only called if we are the LAST
    213   1.6   mycroft  * occurence of an open device
    214   1.6   mycroft  */
    215   1.6   mycroft int
    216  1.15  christos chclose(dev, flags, mode, p)
    217   1.6   mycroft 	dev_t dev;
    218  1.15  christos 	int flags;
    219  1.15  christos 	int mode;
    220  1.15  christos 	struct proc *p;
    221   1.1       cgd {
    222  1.13   mycroft 	struct ch_softc *ch = chcd.cd_devs[CHUNIT(dev)];
    223   1.6   mycroft 
    224  1.12   mycroft 	SC_DEBUG(ch->sc_link, SDEV_DB1, ("closing\n"));
    225  1.12   mycroft 	ch->sc_link->flags &= ~SDEV_OPEN;
    226   1.6   mycroft 
    227   1.6   mycroft 	return 0;
    228   1.6   mycroft }
    229   1.6   mycroft 
    230   1.6   mycroft /*
    231   1.6   mycroft  * Perform special action on behalf of the user
    232   1.6   mycroft  * Knows about the internals of this device
    233   1.6   mycroft  */
    234   1.6   mycroft int
    235  1.13   mycroft chioctl(dev, cmd, arg, mode, p)
    236   1.6   mycroft 	dev_t dev;
    237  1.10       cgd 	u_long cmd;
    238   1.6   mycroft 	caddr_t arg;
    239   1.6   mycroft 	int mode;
    240  1.13   mycroft 	struct proc *p;
    241   1.6   mycroft {
    242  1.13   mycroft 	struct ch_softc *ch = chcd.cd_devs[CHUNIT(dev)];
    243   1.6   mycroft 	struct scsi_link *sc_link = ch->sc_link;
    244   1.7   mycroft 	int flags;
    245   1.6   mycroft 
    246   1.6   mycroft 	/*
    247   1.6   mycroft 	 * Find the device that the user is talking about
    248   1.6   mycroft 	 */
    249   1.6   mycroft 	flags = 0;		/* give error messages, act on errors etc. */
    250   1.6   mycroft 
    251   1.6   mycroft 	switch (cmd) {
    252   1.6   mycroft 	case CHIOOP: {
    253   1.6   mycroft 		struct chop *chop = (struct chop *) arg;
    254   1.6   mycroft 		SC_DEBUG(sc_link, SDEV_DB2, ("[chtape_chop: %x]\n",
    255   1.6   mycroft 		    chop->ch_op));
    256   1.6   mycroft 
    257   1.6   mycroft 		switch (chop->ch_op) {
    258   1.6   mycroft 		case CHGETPARAM:
    259   1.6   mycroft 			chop->u.getparam.chmo = ch->chmo;
    260   1.6   mycroft 			chop->u.getparam.chms = ch->chms;
    261   1.6   mycroft 			chop->u.getparam.sloto = ch->sloto;
    262   1.6   mycroft 			chop->u.getparam.slots = ch->slots;
    263   1.6   mycroft 			chop->u.getparam.imexo = ch->imexo;
    264   1.6   mycroft 			chop->u.getparam.imexs = ch->imexs;
    265   1.6   mycroft 			chop->u.getparam.driveo = ch->driveo;
    266   1.6   mycroft 			chop->u.getparam.drives = ch->drives;
    267   1.6   mycroft 			chop->u.getparam.rot = ch->rot;
    268   1.6   mycroft 			chop->result = 0;
    269   1.6   mycroft 			return 0;
    270   1.6   mycroft 			break;
    271   1.6   mycroft 		case CHPOSITION:
    272   1.6   mycroft 			return ch_position(ch, &chop->result,
    273   1.6   mycroft 			    chop->u.position.chm, chop->u.position.to, flags);
    274   1.6   mycroft 		case CHMOVE:
    275   1.6   mycroft 			return ch_move(ch, &chop->result, chop->u.position.chm,
    276   1.6   mycroft 			    chop->u.move.from, chop->u.move.to, flags);
    277   1.6   mycroft 		case CHGETELEM:
    278   1.6   mycroft 			return ch_getelem(ch, &chop->result,
    279   1.6   mycroft 			    chop->u.get_elem_stat.type,
    280   1.6   mycroft 			    chop->u.get_elem_stat.from,
    281  1.15  christos 			    (char *) &chop->u.get_elem_stat.elem_data, flags);
    282   1.6   mycroft 		default:
    283   1.6   mycroft 			return EINVAL;
    284   1.1       cgd 		}
    285   1.6   mycroft 	}
    286   1.1       cgd 	default:
    287  1.13   mycroft 		return scsi_do_ioctl(sc_link, dev, cmd, arg, mode, p);
    288   1.1       cgd 	}
    289   1.6   mycroft #ifdef DIAGNOSTIC
    290   1.6   mycroft 	panic("chioctl: impossible");
    291   1.6   mycroft #endif
    292   1.1       cgd }
    293   1.1       cgd 
    294   1.6   mycroft int
    295   1.6   mycroft ch_getelem(ch, stat, type, from, data, flags)
    296  1.13   mycroft 	struct ch_softc *ch;
    297   1.6   mycroft 	short *stat;
    298   1.6   mycroft 	int type, from;
    299   1.6   mycroft 	char *data;
    300   1.6   mycroft 	int flags;
    301   1.1       cgd {
    302   1.1       cgd 	struct scsi_read_element_status scsi_cmd;
    303   1.1       cgd 	char elbuf[32];
    304   1.6   mycroft 	int error;
    305   1.1       cgd 
    306   1.1       cgd 	bzero(&scsi_cmd, sizeof(scsi_cmd));
    307  1.13   mycroft 	scsi_cmd.opcode = READ_ELEMENT_STATUS;
    308   1.6   mycroft 	scsi_cmd.byte2 = type;
    309   1.6   mycroft 	scsi_cmd.starting_element_addr[0] = (from >> 8) & 0xff;
    310   1.6   mycroft 	scsi_cmd.starting_element_addr[1] = from & 0xff;
    311   1.6   mycroft 	scsi_cmd.number_of_elements[1] = 1;
    312   1.6   mycroft 	scsi_cmd.allocation_length[2] = 32;
    313   1.6   mycroft 
    314   1.6   mycroft 	error = scsi_scsi_cmd(ch->sc_link, (struct scsi_generic *) &scsi_cmd,
    315   1.6   mycroft 	    sizeof(scsi_cmd), (u_char *) elbuf, 32, CHRETRIES, 100000, NULL,
    316   1.6   mycroft 	    SCSI_DATA_IN | flags);
    317   1.6   mycroft 	if (error)
    318   1.6   mycroft 		*stat = ch->lsterr;
    319   1.6   mycroft 	else
    320   1.6   mycroft 		*stat = 0;
    321   1.6   mycroft 	bcopy(elbuf + 16, data, 16);
    322   1.6   mycroft 	return error;
    323   1.1       cgd }
    324   1.1       cgd 
    325   1.6   mycroft int
    326   1.6   mycroft ch_move(ch, stat, chm, from, to, flags)
    327  1.13   mycroft 	struct ch_softc *ch;
    328   1.6   mycroft 	short *stat;
    329   1.6   mycroft 	int chm, from, to, flags;
    330   1.1       cgd {
    331   1.1       cgd 	struct scsi_move_medium scsi_cmd;
    332   1.6   mycroft 	int error;
    333   1.1       cgd 
    334   1.1       cgd 	bzero(&scsi_cmd, sizeof(scsi_cmd));
    335  1.13   mycroft 	scsi_cmd.opcode = MOVE_MEDIUM;
    336   1.6   mycroft 	scsi_cmd.transport_element_address[0] = (chm >> 8) & 0xff;
    337   1.6   mycroft 	scsi_cmd.transport_element_address[1] = chm & 0xff;
    338   1.6   mycroft 	scsi_cmd.source_address[0] = (from >> 8) & 0xff;
    339   1.6   mycroft 	scsi_cmd.source_address[1] = from & 0xff;
    340   1.6   mycroft 	scsi_cmd.destination_address[0] = (to >> 8) & 0xff;
    341   1.6   mycroft 	scsi_cmd.destination_address[1] = to & 0xff;
    342   1.6   mycroft 	scsi_cmd.invert = (chm & CH_INVERT) ? 1 : 0;
    343   1.6   mycroft 	error = scsi_scsi_cmd(ch->sc_link, (struct scsi_generic *) &scsi_cmd,
    344   1.6   mycroft 	    sizeof(scsi_cmd), NULL, 0, CHRETRIES, 100000, NULL, flags);
    345   1.6   mycroft 	if (error)
    346   1.6   mycroft 		*stat = ch->lsterr;
    347   1.6   mycroft 	else
    348   1.6   mycroft 		*stat = 0;
    349   1.6   mycroft 	return error;
    350   1.1       cgd }
    351   1.1       cgd 
    352   1.6   mycroft int
    353   1.6   mycroft ch_position(ch, stat, chm, to, flags)
    354  1.13   mycroft 	struct ch_softc *ch;
    355   1.6   mycroft 	short *stat;
    356   1.6   mycroft 	int chm, to, flags;
    357   1.1       cgd {
    358   1.1       cgd 	struct scsi_position_to_element scsi_cmd;
    359   1.6   mycroft 	int error;
    360   1.1       cgd 
    361   1.1       cgd 	bzero(&scsi_cmd, sizeof(scsi_cmd));
    362  1.13   mycroft 	scsi_cmd.opcode = POSITION_TO_ELEMENT;
    363   1.6   mycroft 	scsi_cmd.transport_element_address[0] = (chm >> 8) & 0xff;
    364   1.6   mycroft 	scsi_cmd.transport_element_address[1] = chm & 0xff;
    365   1.6   mycroft 	scsi_cmd.source_address[0] = (to >> 8) & 0xff;
    366   1.6   mycroft 	scsi_cmd.source_address[1] = to & 0xff;
    367   1.6   mycroft 	scsi_cmd.invert = (chm & CH_INVERT) ? 1 : 0;
    368   1.6   mycroft 	error = scsi_scsi_cmd(ch->sc_link, (struct scsi_generic *) &scsi_cmd,
    369   1.6   mycroft 	    sizeof(scsi_cmd), NULL, 0, CHRETRIES, 100000, NULL, flags);
    370   1.6   mycroft 	if (error)
    371   1.6   mycroft 		*stat = ch->lsterr;
    372   1.6   mycroft 	else
    373   1.6   mycroft 		*stat = 0;
    374   1.6   mycroft 	return error;
    375   1.1       cgd }
    376   1.1       cgd 
    377   1.6   mycroft /*
    378   1.6   mycroft  * Get the scsi driver to send a full inquiry to the
    379   1.6   mycroft  * device and use the results to fill out the global
    380   1.6   mycroft  * parameter structure.
    381   1.6   mycroft  */
    382   1.6   mycroft int
    383   1.6   mycroft ch_mode_sense(ch, flags)
    384  1.13   mycroft 	struct ch_softc *ch;
    385   1.6   mycroft 	int flags;
    386   1.1       cgd {
    387   1.1       cgd 	struct scsi_mode_sense scsi_cmd;
    388   1.6   mycroft 	u_char scsi_sense[128];	/* Can't use scsi_mode_sense_data because of
    389   1.6   mycroft 				 * missing block descriptor.
    390   1.6   mycroft 				 */
    391   1.1       cgd 	u_char *b;
    392   1.6   mycroft 	int i, l;
    393   1.6   mycroft 	int error;
    394   1.6   mycroft 	struct scsi_link *sc_link = ch->sc_link;
    395   1.6   mycroft 
    396   1.6   mycroft 	/*
    397   1.6   mycroft 	 * First check if we have it all loaded
    398   1.6   mycroft 	 */
    399   1.6   mycroft 	if (sc_link->flags & SDEV_MEDIA_LOADED)
    400   1.6   mycroft 		return 0;
    401   1.6   mycroft 
    402   1.6   mycroft 	/*
    403   1.6   mycroft 	 * First do a mode sense
    404   1.6   mycroft 	 */
    405   1.6   mycroft 	bzero(&scsi_cmd, sizeof(scsi_cmd));
    406  1.13   mycroft 	scsi_cmd.opcode = MODE_SENSE;
    407   1.6   mycroft 	scsi_cmd.byte2 = SMS_DBD;
    408   1.6   mycroft 	scsi_cmd.page = 0x3f;	/* All Pages */
    409   1.6   mycroft 	scsi_cmd.length = sizeof(scsi_sense);
    410   1.6   mycroft 
    411   1.6   mycroft 	/*
    412   1.6   mycroft 	 * Read in the pages
    413   1.6   mycroft 	 */
    414  1.15  christos 	error = scsi_scsi_cmd(sc_link, (struct scsi_generic *) &scsi_cmd,
    415  1.15  christos 			      sizeof(scsi_cmd), (u_char *) &scsi_sense,
    416  1.15  christos 			      sizeof(scsi_sense), CHRETRIES, 5000, NULL,
    417  1.15  christos 			      flags | SCSI_DATA_IN);
    418  1.15  christos 	if (error) {
    419  1.13   mycroft 		printf("%s: could not mode sense\n", ch->sc_dev.dv_xname);
    420   1.6   mycroft 		return error;
    421   1.6   mycroft 	}
    422   1.6   mycroft 
    423   1.6   mycroft 	sc_link->flags |= SDEV_MEDIA_LOADED;
    424   1.6   mycroft 	l = scsi_sense[0] - 3;
    425   1.6   mycroft 	b = &scsi_sense[4];
    426   1.6   mycroft 
    427   1.6   mycroft 	/*
    428   1.6   mycroft 	 * To avoid alignment problems
    429   1.6   mycroft 	 */
    430   1.6   mycroft /* XXXX - FIX THIS FOR MSB */
    431   1.6   mycroft #define p2copy(valp)	 (valp[1] | (valp[0]<<8)); valp+=2
    432   1.6   mycroft #define p4copy(valp)	 (valp[3] | (valp[2]<<8) | (valp[1]<<16) | (valp[0]<<24)); valp+=4
    433   1.1       cgd #if 0
    434   1.6   mycroft 	printf("\nmode_sense %d\n", l);
    435   1.6   mycroft 	for (i = 0; i < l + 4; i++)
    436   1.6   mycroft 		printf("%x%c", scsi_sense[i], i % 8 == 7 ? '\n' : ':');
    437   1.1       cgd 	printf("\n");
    438   1.1       cgd #endif
    439   1.6   mycroft 	for (i = 0; i < l;) {
    440   1.6   mycroft 		u_char pc = (*b++) & 0x3f;
    441   1.6   mycroft 		u_char pl = *b++;
    442   1.6   mycroft 		u_char *bb = b;
    443   1.6   mycroft 		switch (pc) {
    444   1.6   mycroft 		case 0x1d:
    445   1.6   mycroft 			ch->chmo = p2copy(bb);
    446   1.6   mycroft 			ch->chms = p2copy(bb);
    447   1.6   mycroft 			ch->sloto = p2copy(bb);
    448   1.6   mycroft 			ch->slots = p2copy(bb);
    449   1.6   mycroft 			ch->imexo = p2copy(bb);
    450   1.6   mycroft 			ch->imexs = p2copy(bb);
    451   1.6   mycroft 			ch->driveo = p2copy(bb);
    452   1.6   mycroft 			ch->drives = p2copy(bb);
    453   1.6   mycroft 			break;
    454   1.6   mycroft 		case 0x1e:
    455   1.6   mycroft 			ch->rot = *b & 0x1;
    456   1.1       cgd 			break;
    457   1.6   mycroft 		case 0x1f:
    458   1.6   mycroft 			ch->stor = *b & 0xf;
    459   1.6   mycroft 			bb += 2;
    460   1.6   mycroft 			ch->stor = p4copy(bb);
    461   1.1       cgd 			break;
    462   1.1       cgd 		default:
    463   1.6   mycroft 			break;
    464   1.1       cgd 		}
    465   1.6   mycroft 		b += pl;
    466   1.6   mycroft 		i += pl + 2;
    467   1.1       cgd 	}
    468   1.6   mycroft 	SC_DEBUG(sc_link, SDEV_DB2,
    469   1.6   mycroft 	    (" cht(%d-%d)slot(%d-%d)imex(%d-%d)cts(%d-%d) %s rotate\n",
    470   1.6   mycroft 	    ch->chmo, ch->chms, ch->sloto, ch->slots, ch->imexo, ch->imexs,
    471   1.6   mycroft 	    ch->driveo, ch->drives, ch->rot ? "can" : "can't"));
    472   1.6   mycroft 	return 0;
    473   1.1       cgd }
    474