Home | History | Annotate | Line # | Download | only in scsipi
ch.c revision 1.16
      1  1.16   thorpej /*	$NetBSD: ch.c,v 1.16 1996/03/05 00:15:09 thorpej 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.16   thorpej 	printf("\n");
    145  1.16   thorpej 	printf("%s: ", ch->sc_dev.dv_xname);
    146  1.13   mycroft 	if (ch_mode_sense(ch, SCSI_AUTOCONF) != 0)
    147  1.16   thorpej 		printf("offline\n");
    148   1.7   mycroft 	else
    149  1.16   thorpej 		printf("%d slot(s), %d drive(s), %d arm(s), %d i/e-slot(s)\n",
    150   1.6   mycroft 		    ch->slots, ch->drives, ch->chms, ch->imexs);
    151   1.1       cgd }
    152   1.1       cgd 
    153   1.6   mycroft /*
    154   1.6   mycroft  *    open the device.
    155   1.6   mycroft  */
    156   1.6   mycroft int
    157  1.15  christos chopen(dev, flags, mode, p)
    158   1.6   mycroft 	dev_t dev;
    159  1.15  christos 	int flags;
    160  1.15  christos 	int mode;
    161  1.15  christos 	struct proc *p;
    162   1.1       cgd {
    163   1.6   mycroft 	int error = 0;
    164  1.15  christos 	int unit;
    165  1.13   mycroft 	struct ch_softc *ch;
    166   1.6   mycroft 	struct scsi_link *sc_link;
    167   1.6   mycroft 
    168   1.6   mycroft 	unit = CHUNIT(dev);
    169   1.6   mycroft 	if (unit >= chcd.cd_ndevs)
    170   1.6   mycroft 		return ENXIO;
    171   1.6   mycroft 	ch = chcd.cd_devs[unit];
    172   1.7   mycroft 	if (!ch)
    173   1.6   mycroft 		return ENXIO;
    174   1.6   mycroft 
    175   1.6   mycroft 	sc_link = ch->sc_link;
    176   1.6   mycroft 
    177  1.12   mycroft 	SC_DEBUG(sc_link, SDEV_DB1,
    178  1.12   mycroft 	    ("chopen: dev=0x%x (unit %d (of %d))\n", dev, unit, chcd.cd_ndevs));
    179  1.12   mycroft 
    180  1.12   mycroft 	/*
    181  1.12   mycroft 	 * Only allow one at a time
    182  1.12   mycroft 	 */
    183  1.12   mycroft 	if (sc_link->flags & SDEV_OPEN) {
    184  1.12   mycroft 		printf("%s: already open\n", ch->sc_dev.dv_xname);
    185   1.6   mycroft 		return EBUSY;
    186  1.12   mycroft 	}
    187   1.6   mycroft 
    188   1.6   mycroft 	/*
    189   1.6   mycroft 	 * Catch any unit attention errors.
    190   1.6   mycroft 	 */
    191  1.15  christos 	error = scsi_test_unit_ready(sc_link, SCSI_IGNORE_MEDIA_CHANGE);
    192  1.15  christos 	if (error)
    193  1.13   mycroft 		goto bad;
    194   1.6   mycroft 
    195  1.13   mycroft 	sc_link->flags |= SDEV_OPEN;	/* unit attn are now errors */
    196   1.6   mycroft 
    197   1.6   mycroft 	/*
    198   1.6   mycroft 	 * Make sure data is loaded
    199   1.6   mycroft 	 */
    200  1.15  christos 	if ((error = ch_mode_sense(ch, 0)) != 0) {
    201   1.6   mycroft 		printf("%s: offline\n", ch->sc_dev.dv_xname);
    202  1.12   mycroft 		goto bad;
    203   1.6   mycroft 	}
    204   1.6   mycroft 
    205  1.12   mycroft 	SC_DEBUG(sc_link, SDEV_DB3, ("open complete\n"));
    206   1.6   mycroft 	return 0;
    207  1.12   mycroft 
    208  1.12   mycroft bad:
    209  1.12   mycroft 	sc_link->flags &= ~SDEV_OPEN;
    210  1.12   mycroft 	return error;
    211   1.6   mycroft }
    212   1.6   mycroft 
    213   1.6   mycroft /*
    214   1.6   mycroft  * close the device.. only called if we are the LAST
    215   1.6   mycroft  * occurence of an open device
    216   1.6   mycroft  */
    217   1.6   mycroft int
    218  1.15  christos chclose(dev, flags, mode, p)
    219   1.6   mycroft 	dev_t dev;
    220  1.15  christos 	int flags;
    221  1.15  christos 	int mode;
    222  1.15  christos 	struct proc *p;
    223   1.1       cgd {
    224  1.13   mycroft 	struct ch_softc *ch = chcd.cd_devs[CHUNIT(dev)];
    225   1.6   mycroft 
    226  1.12   mycroft 	SC_DEBUG(ch->sc_link, SDEV_DB1, ("closing\n"));
    227  1.12   mycroft 	ch->sc_link->flags &= ~SDEV_OPEN;
    228   1.6   mycroft 
    229   1.6   mycroft 	return 0;
    230   1.6   mycroft }
    231   1.6   mycroft 
    232   1.6   mycroft /*
    233   1.6   mycroft  * Perform special action on behalf of the user
    234   1.6   mycroft  * Knows about the internals of this device
    235   1.6   mycroft  */
    236   1.6   mycroft int
    237  1.13   mycroft chioctl(dev, cmd, arg, mode, p)
    238   1.6   mycroft 	dev_t dev;
    239  1.10       cgd 	u_long cmd;
    240   1.6   mycroft 	caddr_t arg;
    241   1.6   mycroft 	int mode;
    242  1.13   mycroft 	struct proc *p;
    243   1.6   mycroft {
    244  1.13   mycroft 	struct ch_softc *ch = chcd.cd_devs[CHUNIT(dev)];
    245   1.6   mycroft 	struct scsi_link *sc_link = ch->sc_link;
    246   1.7   mycroft 	int flags;
    247   1.6   mycroft 
    248   1.6   mycroft 	/*
    249   1.6   mycroft 	 * Find the device that the user is talking about
    250   1.6   mycroft 	 */
    251   1.6   mycroft 	flags = 0;		/* give error messages, act on errors etc. */
    252   1.6   mycroft 
    253   1.6   mycroft 	switch (cmd) {
    254   1.6   mycroft 	case CHIOOP: {
    255   1.6   mycroft 		struct chop *chop = (struct chop *) arg;
    256   1.6   mycroft 		SC_DEBUG(sc_link, SDEV_DB2, ("[chtape_chop: %x]\n",
    257   1.6   mycroft 		    chop->ch_op));
    258   1.6   mycroft 
    259   1.6   mycroft 		switch (chop->ch_op) {
    260   1.6   mycroft 		case CHGETPARAM:
    261   1.6   mycroft 			chop->u.getparam.chmo = ch->chmo;
    262   1.6   mycroft 			chop->u.getparam.chms = ch->chms;
    263   1.6   mycroft 			chop->u.getparam.sloto = ch->sloto;
    264   1.6   mycroft 			chop->u.getparam.slots = ch->slots;
    265   1.6   mycroft 			chop->u.getparam.imexo = ch->imexo;
    266   1.6   mycroft 			chop->u.getparam.imexs = ch->imexs;
    267   1.6   mycroft 			chop->u.getparam.driveo = ch->driveo;
    268   1.6   mycroft 			chop->u.getparam.drives = ch->drives;
    269   1.6   mycroft 			chop->u.getparam.rot = ch->rot;
    270   1.6   mycroft 			chop->result = 0;
    271   1.6   mycroft 			return 0;
    272   1.6   mycroft 			break;
    273   1.6   mycroft 		case CHPOSITION:
    274   1.6   mycroft 			return ch_position(ch, &chop->result,
    275   1.6   mycroft 			    chop->u.position.chm, chop->u.position.to, flags);
    276   1.6   mycroft 		case CHMOVE:
    277   1.6   mycroft 			return ch_move(ch, &chop->result, chop->u.position.chm,
    278   1.6   mycroft 			    chop->u.move.from, chop->u.move.to, flags);
    279   1.6   mycroft 		case CHGETELEM:
    280   1.6   mycroft 			return ch_getelem(ch, &chop->result,
    281   1.6   mycroft 			    chop->u.get_elem_stat.type,
    282   1.6   mycroft 			    chop->u.get_elem_stat.from,
    283  1.15  christos 			    (char *) &chop->u.get_elem_stat.elem_data, flags);
    284   1.6   mycroft 		default:
    285   1.6   mycroft 			return EINVAL;
    286   1.1       cgd 		}
    287   1.6   mycroft 	}
    288   1.1       cgd 	default:
    289  1.13   mycroft 		return scsi_do_ioctl(sc_link, dev, cmd, arg, mode, p);
    290   1.1       cgd 	}
    291   1.6   mycroft #ifdef DIAGNOSTIC
    292   1.6   mycroft 	panic("chioctl: impossible");
    293   1.6   mycroft #endif
    294   1.1       cgd }
    295   1.1       cgd 
    296   1.6   mycroft int
    297   1.6   mycroft ch_getelem(ch, stat, type, from, data, flags)
    298  1.13   mycroft 	struct ch_softc *ch;
    299   1.6   mycroft 	short *stat;
    300   1.6   mycroft 	int type, from;
    301   1.6   mycroft 	char *data;
    302   1.6   mycroft 	int flags;
    303   1.1       cgd {
    304   1.1       cgd 	struct scsi_read_element_status scsi_cmd;
    305   1.1       cgd 	char elbuf[32];
    306   1.6   mycroft 	int error;
    307   1.1       cgd 
    308   1.1       cgd 	bzero(&scsi_cmd, sizeof(scsi_cmd));
    309  1.13   mycroft 	scsi_cmd.opcode = READ_ELEMENT_STATUS;
    310   1.6   mycroft 	scsi_cmd.byte2 = type;
    311   1.6   mycroft 	scsi_cmd.starting_element_addr[0] = (from >> 8) & 0xff;
    312   1.6   mycroft 	scsi_cmd.starting_element_addr[1] = from & 0xff;
    313   1.6   mycroft 	scsi_cmd.number_of_elements[1] = 1;
    314   1.6   mycroft 	scsi_cmd.allocation_length[2] = 32;
    315   1.6   mycroft 
    316   1.6   mycroft 	error = scsi_scsi_cmd(ch->sc_link, (struct scsi_generic *) &scsi_cmd,
    317   1.6   mycroft 	    sizeof(scsi_cmd), (u_char *) elbuf, 32, CHRETRIES, 100000, NULL,
    318   1.6   mycroft 	    SCSI_DATA_IN | flags);
    319   1.6   mycroft 	if (error)
    320   1.6   mycroft 		*stat = ch->lsterr;
    321   1.6   mycroft 	else
    322   1.6   mycroft 		*stat = 0;
    323   1.6   mycroft 	bcopy(elbuf + 16, data, 16);
    324   1.6   mycroft 	return error;
    325   1.1       cgd }
    326   1.1       cgd 
    327   1.6   mycroft int
    328   1.6   mycroft ch_move(ch, stat, chm, from, to, flags)
    329  1.13   mycroft 	struct ch_softc *ch;
    330   1.6   mycroft 	short *stat;
    331   1.6   mycroft 	int chm, from, to, flags;
    332   1.1       cgd {
    333   1.1       cgd 	struct scsi_move_medium scsi_cmd;
    334   1.6   mycroft 	int error;
    335   1.1       cgd 
    336   1.1       cgd 	bzero(&scsi_cmd, sizeof(scsi_cmd));
    337  1.13   mycroft 	scsi_cmd.opcode = MOVE_MEDIUM;
    338   1.6   mycroft 	scsi_cmd.transport_element_address[0] = (chm >> 8) & 0xff;
    339   1.6   mycroft 	scsi_cmd.transport_element_address[1] = chm & 0xff;
    340   1.6   mycroft 	scsi_cmd.source_address[0] = (from >> 8) & 0xff;
    341   1.6   mycroft 	scsi_cmd.source_address[1] = from & 0xff;
    342   1.6   mycroft 	scsi_cmd.destination_address[0] = (to >> 8) & 0xff;
    343   1.6   mycroft 	scsi_cmd.destination_address[1] = to & 0xff;
    344   1.6   mycroft 	scsi_cmd.invert = (chm & CH_INVERT) ? 1 : 0;
    345   1.6   mycroft 	error = scsi_scsi_cmd(ch->sc_link, (struct scsi_generic *) &scsi_cmd,
    346   1.6   mycroft 	    sizeof(scsi_cmd), NULL, 0, CHRETRIES, 100000, NULL, flags);
    347   1.6   mycroft 	if (error)
    348   1.6   mycroft 		*stat = ch->lsterr;
    349   1.6   mycroft 	else
    350   1.6   mycroft 		*stat = 0;
    351   1.6   mycroft 	return error;
    352   1.1       cgd }
    353   1.1       cgd 
    354   1.6   mycroft int
    355   1.6   mycroft ch_position(ch, stat, chm, to, flags)
    356  1.13   mycroft 	struct ch_softc *ch;
    357   1.6   mycroft 	short *stat;
    358   1.6   mycroft 	int chm, to, flags;
    359   1.1       cgd {
    360   1.1       cgd 	struct scsi_position_to_element scsi_cmd;
    361   1.6   mycroft 	int error;
    362   1.1       cgd 
    363   1.1       cgd 	bzero(&scsi_cmd, sizeof(scsi_cmd));
    364  1.13   mycroft 	scsi_cmd.opcode = POSITION_TO_ELEMENT;
    365   1.6   mycroft 	scsi_cmd.transport_element_address[0] = (chm >> 8) & 0xff;
    366   1.6   mycroft 	scsi_cmd.transport_element_address[1] = chm & 0xff;
    367   1.6   mycroft 	scsi_cmd.source_address[0] = (to >> 8) & 0xff;
    368   1.6   mycroft 	scsi_cmd.source_address[1] = to & 0xff;
    369   1.6   mycroft 	scsi_cmd.invert = (chm & CH_INVERT) ? 1 : 0;
    370   1.6   mycroft 	error = scsi_scsi_cmd(ch->sc_link, (struct scsi_generic *) &scsi_cmd,
    371   1.6   mycroft 	    sizeof(scsi_cmd), NULL, 0, CHRETRIES, 100000, NULL, flags);
    372   1.6   mycroft 	if (error)
    373   1.6   mycroft 		*stat = ch->lsterr;
    374   1.6   mycroft 	else
    375   1.6   mycroft 		*stat = 0;
    376   1.6   mycroft 	return error;
    377   1.1       cgd }
    378   1.1       cgd 
    379   1.6   mycroft /*
    380   1.6   mycroft  * Get the scsi driver to send a full inquiry to the
    381   1.6   mycroft  * device and use the results to fill out the global
    382   1.6   mycroft  * parameter structure.
    383   1.6   mycroft  */
    384   1.6   mycroft int
    385   1.6   mycroft ch_mode_sense(ch, flags)
    386  1.13   mycroft 	struct ch_softc *ch;
    387   1.6   mycroft 	int flags;
    388   1.1       cgd {
    389   1.1       cgd 	struct scsi_mode_sense scsi_cmd;
    390   1.6   mycroft 	u_char scsi_sense[128];	/* Can't use scsi_mode_sense_data because of
    391   1.6   mycroft 				 * missing block descriptor.
    392   1.6   mycroft 				 */
    393   1.1       cgd 	u_char *b;
    394   1.6   mycroft 	int i, l;
    395   1.6   mycroft 	int error;
    396   1.6   mycroft 	struct scsi_link *sc_link = ch->sc_link;
    397   1.6   mycroft 
    398   1.6   mycroft 	/*
    399   1.6   mycroft 	 * First check if we have it all loaded
    400   1.6   mycroft 	 */
    401   1.6   mycroft 	if (sc_link->flags & SDEV_MEDIA_LOADED)
    402   1.6   mycroft 		return 0;
    403   1.6   mycroft 
    404   1.6   mycroft 	/*
    405   1.6   mycroft 	 * First do a mode sense
    406   1.6   mycroft 	 */
    407   1.6   mycroft 	bzero(&scsi_cmd, sizeof(scsi_cmd));
    408  1.13   mycroft 	scsi_cmd.opcode = MODE_SENSE;
    409   1.6   mycroft 	scsi_cmd.byte2 = SMS_DBD;
    410   1.6   mycroft 	scsi_cmd.page = 0x3f;	/* All Pages */
    411   1.6   mycroft 	scsi_cmd.length = sizeof(scsi_sense);
    412   1.6   mycroft 
    413   1.6   mycroft 	/*
    414   1.6   mycroft 	 * Read in the pages
    415   1.6   mycroft 	 */
    416  1.15  christos 	error = scsi_scsi_cmd(sc_link, (struct scsi_generic *) &scsi_cmd,
    417  1.15  christos 			      sizeof(scsi_cmd), (u_char *) &scsi_sense,
    418  1.15  christos 			      sizeof(scsi_sense), CHRETRIES, 5000, NULL,
    419  1.15  christos 			      flags | SCSI_DATA_IN);
    420  1.15  christos 	if (error) {
    421  1.13   mycroft 		printf("%s: could not mode sense\n", ch->sc_dev.dv_xname);
    422   1.6   mycroft 		return error;
    423   1.6   mycroft 	}
    424   1.6   mycroft 
    425   1.6   mycroft 	sc_link->flags |= SDEV_MEDIA_LOADED;
    426   1.6   mycroft 	l = scsi_sense[0] - 3;
    427   1.6   mycroft 	b = &scsi_sense[4];
    428   1.6   mycroft 
    429   1.6   mycroft 	/*
    430   1.6   mycroft 	 * To avoid alignment problems
    431   1.6   mycroft 	 */
    432   1.6   mycroft /* XXXX - FIX THIS FOR MSB */
    433   1.6   mycroft #define p2copy(valp)	 (valp[1] | (valp[0]<<8)); valp+=2
    434   1.6   mycroft #define p4copy(valp)	 (valp[3] | (valp[2]<<8) | (valp[1]<<16) | (valp[0]<<24)); valp+=4
    435   1.1       cgd #if 0
    436   1.6   mycroft 	printf("\nmode_sense %d\n", l);
    437   1.6   mycroft 	for (i = 0; i < l + 4; i++)
    438   1.6   mycroft 		printf("%x%c", scsi_sense[i], i % 8 == 7 ? '\n' : ':');
    439   1.1       cgd 	printf("\n");
    440   1.1       cgd #endif
    441   1.6   mycroft 	for (i = 0; i < l;) {
    442   1.6   mycroft 		u_char pc = (*b++) & 0x3f;
    443   1.6   mycroft 		u_char pl = *b++;
    444   1.6   mycroft 		u_char *bb = b;
    445   1.6   mycroft 		switch (pc) {
    446   1.6   mycroft 		case 0x1d:
    447   1.6   mycroft 			ch->chmo = p2copy(bb);
    448   1.6   mycroft 			ch->chms = p2copy(bb);
    449   1.6   mycroft 			ch->sloto = p2copy(bb);
    450   1.6   mycroft 			ch->slots = p2copy(bb);
    451   1.6   mycroft 			ch->imexo = p2copy(bb);
    452   1.6   mycroft 			ch->imexs = p2copy(bb);
    453   1.6   mycroft 			ch->driveo = p2copy(bb);
    454   1.6   mycroft 			ch->drives = p2copy(bb);
    455   1.6   mycroft 			break;
    456   1.6   mycroft 		case 0x1e:
    457   1.6   mycroft 			ch->rot = *b & 0x1;
    458   1.1       cgd 			break;
    459   1.6   mycroft 		case 0x1f:
    460   1.6   mycroft 			ch->stor = *b & 0xf;
    461   1.6   mycroft 			bb += 2;
    462   1.6   mycroft 			ch->stor = p4copy(bb);
    463   1.1       cgd 			break;
    464   1.1       cgd 		default:
    465   1.6   mycroft 			break;
    466   1.1       cgd 		}
    467   1.6   mycroft 		b += pl;
    468   1.6   mycroft 		i += pl + 2;
    469   1.1       cgd 	}
    470   1.6   mycroft 	SC_DEBUG(sc_link, SDEV_DB2,
    471   1.6   mycroft 	    (" cht(%d-%d)slot(%d-%d)imex(%d-%d)cts(%d-%d) %s rotate\n",
    472   1.6   mycroft 	    ch->chmo, ch->chms, ch->sloto, ch->slots, ch->imexo, ch->imexs,
    473   1.6   mycroft 	    ch->driveo, ch->drives, ch->rot ? "can" : "can't"));
    474   1.6   mycroft 	return 0;
    475   1.1       cgd }
    476