Home | History | Annotate | Line # | Download | only in scsipi
ch.c revision 1.9
      1  1.9  mycroft /*	$NetBSD: ch.c,v 1.9 1994/10/20 20:31:26 mycroft 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.1      cgd 
     52  1.6  mycroft #define	CHRETRIES	2
     53  1.1      cgd 
     54  1.6  mycroft #define CHMODE(z)	(minor(z) & 0x0f)
     55  1.6  mycroft #define CHUNIT(z)	(minor(z) >> 4)
     56  1.1      cgd 
     57  1.6  mycroft struct ch_data {
     58  1.6  mycroft 	struct device sc_dev;
     59  1.1      cgd 
     60  1.7  mycroft 	int flags;
     61  1.7  mycroft #define CHOPEN		0x01
     62  1.6  mycroft 	struct scsi_link *sc_link;	/* all the inter level info */
     63  1.6  mycroft 	u_int16 chmo;			/* Offset of first CHM */
     64  1.6  mycroft 	u_int16 chms;			/* No. of CHM */
     65  1.6  mycroft 	u_int16 slots;			/* No. of Storage Elements */
     66  1.6  mycroft 	u_int16 sloto;			/* Offset of first SE */
     67  1.6  mycroft 	u_int16 imexs;			/* No. of Import/Export Slots */
     68  1.6  mycroft 	u_int16 imexo;			/* Offset of first IM/EX */
     69  1.6  mycroft 	u_int16 drives;			/* No. of CTS */
     70  1.6  mycroft 	u_int16 driveo;			/* Offset of first CTS */
     71  1.6  mycroft 	u_int16 rot;			/* CHM can rotate */
     72  1.6  mycroft 	u_long  op_matrix;		/* possible opertaions */
     73  1.6  mycroft 	u_int16 lsterr;			/* details of lasterror */
     74  1.6  mycroft 	u_char  stor;			/* posible Storage locations */
     75  1.6  mycroft };
     76  1.6  mycroft 
     77  1.6  mycroft void chattach __P((struct device *, struct device *, void *));
     78  1.6  mycroft 
     79  1.6  mycroft struct cfdriver chcd = {
     80  1.6  mycroft 	NULL, "ch", scsi_targmatch, chattach, DV_DULL, sizeof(struct ch_data)
     81  1.6  mycroft };
     82  1.1      cgd 
     83  1.6  mycroft /*
     84  1.6  mycroft  * This driver is so simple it uses all the default services
     85  1.6  mycroft  */
     86  1.6  mycroft struct scsi_device ch_switch = {
     87  1.6  mycroft 	NULL,
     88  1.6  mycroft 	NULL,
     89  1.6  mycroft 	NULL,
     90  1.6  mycroft 	NULL,
     91  1.6  mycroft 	"ch",
     92  1.6  mycroft 	0
     93  1.6  mycroft };
     94  1.6  mycroft 
     95  1.6  mycroft /*
     96  1.6  mycroft  * The routine called by the low level scsi routine when it discovers
     97  1.6  mycroft  * a device suitable for this driver.
     98  1.6  mycroft  */
     99  1.6  mycroft void
    100  1.6  mycroft chattach(parent, self, aux)
    101  1.6  mycroft 	struct device *parent, *self;
    102  1.6  mycroft 	void *aux;
    103  1.1      cgd {
    104  1.6  mycroft 	struct ch_data *ch = (void *)self;
    105  1.6  mycroft 	struct scsi_link *sc_link = aux;
    106  1.6  mycroft 	unsigned char *tbl;
    107  1.1      cgd 
    108  1.6  mycroft 	SC_DEBUG(sc_link, SDEV_DB2, ("chattach: "));
    109  1.1      cgd 
    110  1.6  mycroft 	/*
    111  1.6  mycroft 	 * Store information needed to contact our base driver
    112  1.6  mycroft 	 */
    113  1.6  mycroft 	ch->sc_link = sc_link;
    114  1.6  mycroft 	sc_link->device = &ch_switch;
    115  1.6  mycroft 	sc_link->dev_unit = ch->sc_dev.dv_unit;
    116  1.6  mycroft 
    117  1.6  mycroft 	/*
    118  1.6  mycroft 	 * Use the subdriver to request information regarding
    119  1.6  mycroft 	 * the drive. We cannot use interrupts yet, so the
    120  1.6  mycroft 	 * request must specify this.
    121  1.6  mycroft 	 */
    122  1.7  mycroft 	if ((ch_mode_sense(ch, SCSI_NOSLEEP | SCSI_NOMASK)))
    123  1.6  mycroft 		printf(": offline\n");
    124  1.7  mycroft 	else
    125  1.6  mycroft 		printf(": %d slot(s), %d drive(s), %d arm(s), %d i/e-slot(s)\n",
    126  1.6  mycroft 		    ch->slots, ch->drives, ch->chms, ch->imexs);
    127  1.1      cgd }
    128  1.1      cgd 
    129  1.6  mycroft /*
    130  1.6  mycroft  *    open the device.
    131  1.6  mycroft  */
    132  1.6  mycroft int
    133  1.1      cgd chopen(dev)
    134  1.6  mycroft 	dev_t dev;
    135  1.1      cgd {
    136  1.6  mycroft 	int error = 0;
    137  1.6  mycroft 	int unit, mode;
    138  1.6  mycroft 	struct ch_data *ch;
    139  1.6  mycroft 	struct scsi_link *sc_link;
    140  1.6  mycroft 
    141  1.6  mycroft 	unit = CHUNIT(dev);
    142  1.6  mycroft 	mode = CHMODE(dev);
    143  1.6  mycroft 
    144  1.6  mycroft 	if (unit >= chcd.cd_ndevs)
    145  1.6  mycroft 		return ENXIO;
    146  1.6  mycroft 	ch = chcd.cd_devs[unit];
    147  1.7  mycroft 	if (!ch)
    148  1.6  mycroft 		return ENXIO;
    149  1.6  mycroft 
    150  1.6  mycroft 	sc_link = ch->sc_link;
    151  1.6  mycroft 	SC_DEBUG(sc_link, SDEV_DB1, ("chopen: dev=0x%x (unit %d (of %d))\n",
    152  1.6  mycroft 	    dev, unit, chcd.cd_ndevs));
    153  1.6  mycroft 
    154  1.6  mycroft 	if (ch->flags & CHOPEN)
    155  1.6  mycroft 		return EBUSY;
    156  1.6  mycroft 
    157  1.6  mycroft 	/*
    158  1.6  mycroft 	 * Catch any unit attention errors.
    159  1.6  mycroft 	 */
    160  1.6  mycroft 	scsi_test_unit_ready(sc_link, SCSI_SILENT);
    161  1.6  mycroft 
    162  1.6  mycroft 	sc_link->flags |= SDEV_OPEN;
    163  1.6  mycroft 	/*
    164  1.6  mycroft 	 * Check that it is still responding and ok.
    165  1.6  mycroft 	 */
    166  1.6  mycroft 	if (error = (scsi_test_unit_ready(sc_link, 0))) {
    167  1.6  mycroft 		printf("%s: not ready\n", ch->sc_dev.dv_xname);
    168  1.6  mycroft 		sc_link->flags &= ~SDEV_OPEN;
    169  1.6  mycroft 		return error;
    170  1.6  mycroft 	}
    171  1.6  mycroft 
    172  1.6  mycroft 	/*
    173  1.6  mycroft 	 * Make sure data is loaded
    174  1.6  mycroft 	 */
    175  1.6  mycroft 	if (error = (ch_mode_sense(ch, SCSI_NOSLEEP | SCSI_NOMASK))) {
    176  1.6  mycroft 		printf("%s: offline\n", ch->sc_dev.dv_xname);
    177  1.6  mycroft 		sc_link->flags &= ~SDEV_OPEN;
    178  1.6  mycroft 		return error;
    179  1.6  mycroft 	}
    180  1.6  mycroft 
    181  1.6  mycroft 	ch->flags |= CHOPEN;
    182  1.6  mycroft 	return 0;
    183  1.6  mycroft }
    184  1.6  mycroft 
    185  1.6  mycroft /*
    186  1.6  mycroft  * close the device.. only called if we are the LAST
    187  1.6  mycroft  * occurence of an open device
    188  1.6  mycroft  */
    189  1.6  mycroft int
    190  1.1      cgd chclose(dev)
    191  1.6  mycroft 	dev_t dev;
    192  1.1      cgd {
    193  1.6  mycroft 	int unit, mode;
    194  1.6  mycroft 	struct ch_data *ch;
    195  1.6  mycroft 	struct scsi_link *sc_link;
    196  1.6  mycroft 
    197  1.6  mycroft 	unit = CHUNIT(dev);
    198  1.6  mycroft 	mode = CHMODE(dev);
    199  1.6  mycroft 	ch = chcd.cd_devs[unit];
    200  1.6  mycroft 	sc_link = ch->sc_link;
    201  1.6  mycroft 
    202  1.6  mycroft 	SC_DEBUG(sc_link, SDEV_DB1, ("closing"));
    203  1.6  mycroft 	ch->flags &= ~CHOPEN;
    204  1.6  mycroft 	sc_link->flags &= ~SDEV_OPEN;
    205  1.6  mycroft 	return 0;
    206  1.6  mycroft }
    207  1.6  mycroft 
    208  1.6  mycroft /*
    209  1.6  mycroft  * Perform special action on behalf of the user
    210  1.6  mycroft  * Knows about the internals of this device
    211  1.6  mycroft  */
    212  1.6  mycroft int
    213  1.1      cgd chioctl(dev, cmd, arg, mode)
    214  1.6  mycroft 	dev_t dev;
    215  1.6  mycroft 	int cmd;
    216  1.6  mycroft 	caddr_t arg;
    217  1.6  mycroft 	int mode;
    218  1.6  mycroft {
    219  1.6  mycroft 	struct ch_data *ch = chcd.cd_devs[CHUNIT(dev)];
    220  1.6  mycroft 	struct scsi_link *sc_link = ch->sc_link;
    221  1.6  mycroft 	int number;
    222  1.7  mycroft 	int flags;
    223  1.6  mycroft 
    224  1.6  mycroft 	/*
    225  1.6  mycroft 	 * Find the device that the user is talking about
    226  1.6  mycroft 	 */
    227  1.6  mycroft 	flags = 0;		/* give error messages, act on errors etc. */
    228  1.6  mycroft 
    229  1.6  mycroft 	switch (cmd) {
    230  1.6  mycroft 	case CHIOOP: {
    231  1.6  mycroft 		struct chop *chop = (struct chop *) arg;
    232  1.6  mycroft 		SC_DEBUG(sc_link, SDEV_DB2, ("[chtape_chop: %x]\n",
    233  1.6  mycroft 		    chop->ch_op));
    234  1.6  mycroft 
    235  1.6  mycroft 		switch (chop->ch_op) {
    236  1.6  mycroft 		case CHGETPARAM:
    237  1.6  mycroft 			chop->u.getparam.chmo = ch->chmo;
    238  1.6  mycroft 			chop->u.getparam.chms = ch->chms;
    239  1.6  mycroft 			chop->u.getparam.sloto = ch->sloto;
    240  1.6  mycroft 			chop->u.getparam.slots = ch->slots;
    241  1.6  mycroft 			chop->u.getparam.imexo = ch->imexo;
    242  1.6  mycroft 			chop->u.getparam.imexs = ch->imexs;
    243  1.6  mycroft 			chop->u.getparam.driveo = ch->driveo;
    244  1.6  mycroft 			chop->u.getparam.drives = ch->drives;
    245  1.6  mycroft 			chop->u.getparam.rot = ch->rot;
    246  1.6  mycroft 			chop->result = 0;
    247  1.6  mycroft 			return 0;
    248  1.6  mycroft 			break;
    249  1.6  mycroft 		case CHPOSITION:
    250  1.6  mycroft 			return ch_position(ch, &chop->result,
    251  1.6  mycroft 			    chop->u.position.chm, chop->u.position.to, flags);
    252  1.6  mycroft 		case CHMOVE:
    253  1.6  mycroft 			return ch_move(ch, &chop->result, chop->u.position.chm,
    254  1.6  mycroft 			    chop->u.move.from, chop->u.move.to, flags);
    255  1.6  mycroft 		case CHGETELEM:
    256  1.6  mycroft 			return ch_getelem(ch, &chop->result,
    257  1.6  mycroft 			    chop->u.get_elem_stat.type,
    258  1.6  mycroft 			    chop->u.get_elem_stat.from,
    259  1.6  mycroft 			    &chop->u.get_elem_stat.elem_data, flags);
    260  1.6  mycroft 		default:
    261  1.6  mycroft 			return EINVAL;
    262  1.1      cgd 		}
    263  1.6  mycroft 	}
    264  1.1      cgd 	default:
    265  1.9  mycroft 		return scsi_do_ioctl(sc_link, dev, cmd, arg, mode);
    266  1.1      cgd 	}
    267  1.6  mycroft #ifdef DIAGNOSTIC
    268  1.6  mycroft 	panic("chioctl: impossible");
    269  1.6  mycroft #endif
    270  1.1      cgd }
    271  1.1      cgd 
    272  1.6  mycroft int
    273  1.6  mycroft ch_getelem(ch, stat, type, from, data, flags)
    274  1.6  mycroft 	struct ch_data *ch;
    275  1.6  mycroft 	short *stat;
    276  1.6  mycroft 	int type, from;
    277  1.6  mycroft 	char *data;
    278  1.6  mycroft 	int flags;
    279  1.1      cgd {
    280  1.1      cgd 	struct scsi_read_element_status scsi_cmd;
    281  1.1      cgd 	char elbuf[32];
    282  1.6  mycroft 	int error;
    283  1.1      cgd 
    284  1.1      cgd 	bzero(&scsi_cmd, sizeof(scsi_cmd));
    285  1.1      cgd 	scsi_cmd.op_code = READ_ELEMENT_STATUS;
    286  1.6  mycroft 	scsi_cmd.byte2 = type;
    287  1.6  mycroft 	scsi_cmd.starting_element_addr[0] = (from >> 8) & 0xff;
    288  1.6  mycroft 	scsi_cmd.starting_element_addr[1] = from & 0xff;
    289  1.6  mycroft 	scsi_cmd.number_of_elements[1] = 1;
    290  1.6  mycroft 	scsi_cmd.allocation_length[2] = 32;
    291  1.6  mycroft 
    292  1.6  mycroft 	error = scsi_scsi_cmd(ch->sc_link, (struct scsi_generic *) &scsi_cmd,
    293  1.6  mycroft 	    sizeof(scsi_cmd), (u_char *) elbuf, 32, CHRETRIES, 100000, NULL,
    294  1.6  mycroft 	    SCSI_DATA_IN | flags);
    295  1.6  mycroft 	if (error)
    296  1.6  mycroft 		*stat = ch->lsterr;
    297  1.6  mycroft 	else
    298  1.6  mycroft 		*stat = 0;
    299  1.6  mycroft 	bcopy(elbuf + 16, data, 16);
    300  1.6  mycroft 	return error;
    301  1.1      cgd }
    302  1.1      cgd 
    303  1.6  mycroft int
    304  1.6  mycroft ch_move(ch, stat, chm, from, to, flags)
    305  1.6  mycroft 	struct ch_data *ch;
    306  1.6  mycroft 	short *stat;
    307  1.6  mycroft 	int chm, from, to, flags;
    308  1.1      cgd {
    309  1.1      cgd 	struct scsi_move_medium scsi_cmd;
    310  1.6  mycroft 	int error;
    311  1.1      cgd 
    312  1.1      cgd 	bzero(&scsi_cmd, sizeof(scsi_cmd));
    313  1.1      cgd 	scsi_cmd.op_code = MOVE_MEDIUM;
    314  1.6  mycroft 	scsi_cmd.transport_element_address[0] = (chm >> 8) & 0xff;
    315  1.6  mycroft 	scsi_cmd.transport_element_address[1] = chm & 0xff;
    316  1.6  mycroft 	scsi_cmd.source_address[0] = (from >> 8) & 0xff;
    317  1.6  mycroft 	scsi_cmd.source_address[1] = from & 0xff;
    318  1.6  mycroft 	scsi_cmd.destination_address[0] = (to >> 8) & 0xff;
    319  1.6  mycroft 	scsi_cmd.destination_address[1] = to & 0xff;
    320  1.6  mycroft 	scsi_cmd.invert = (chm & CH_INVERT) ? 1 : 0;
    321  1.6  mycroft 	error = scsi_scsi_cmd(ch->sc_link, (struct scsi_generic *) &scsi_cmd,
    322  1.6  mycroft 	    sizeof(scsi_cmd), NULL, 0, CHRETRIES, 100000, NULL, flags);
    323  1.6  mycroft 	if (error)
    324  1.6  mycroft 		*stat = ch->lsterr;
    325  1.6  mycroft 	else
    326  1.6  mycroft 		*stat = 0;
    327  1.6  mycroft 	return error;
    328  1.1      cgd }
    329  1.1      cgd 
    330  1.6  mycroft int
    331  1.6  mycroft ch_position(ch, stat, chm, to, flags)
    332  1.6  mycroft 	struct ch_data *ch;
    333  1.6  mycroft 	short *stat;
    334  1.6  mycroft 	int chm, to, flags;
    335  1.1      cgd {
    336  1.1      cgd 	struct scsi_position_to_element scsi_cmd;
    337  1.6  mycroft 	int error;
    338  1.1      cgd 
    339  1.1      cgd 	bzero(&scsi_cmd, sizeof(scsi_cmd));
    340  1.1      cgd 	scsi_cmd.op_code = POSITION_TO_ELEMENT;
    341  1.6  mycroft 	scsi_cmd.transport_element_address[0] = (chm >> 8) & 0xff;
    342  1.6  mycroft 	scsi_cmd.transport_element_address[1] = chm & 0xff;
    343  1.6  mycroft 	scsi_cmd.source_address[0] = (to >> 8) & 0xff;
    344  1.6  mycroft 	scsi_cmd.source_address[1] = to & 0xff;
    345  1.6  mycroft 	scsi_cmd.invert = (chm & CH_INVERT) ? 1 : 0;
    346  1.6  mycroft 	error = scsi_scsi_cmd(ch->sc_link, (struct scsi_generic *) &scsi_cmd,
    347  1.6  mycroft 	    sizeof(scsi_cmd), NULL, 0, CHRETRIES, 100000, NULL, flags);
    348  1.6  mycroft 	if (error)
    349  1.6  mycroft 		*stat = ch->lsterr;
    350  1.6  mycroft 	else
    351  1.6  mycroft 		*stat = 0;
    352  1.6  mycroft 	return error;
    353  1.1      cgd }
    354  1.1      cgd 
    355  1.1      cgd #ifdef	__STDC__
    356  1.6  mycroft #define b2tol(a)	(((unsigned)(a##_1) << 8) | (unsigned)a##_0)
    357  1.1      cgd #else
    358  1.6  mycroft #define b2tol(a)	(((unsigned)(a/**/_1) << 8) | (unsigned)a/**/_0)
    359  1.1      cgd #endif
    360  1.1      cgd 
    361  1.6  mycroft /*
    362  1.6  mycroft  * Get the scsi driver to send a full inquiry to the
    363  1.6  mycroft  * device and use the results to fill out the global
    364  1.6  mycroft  * parameter structure.
    365  1.6  mycroft  */
    366  1.6  mycroft int
    367  1.6  mycroft ch_mode_sense(ch, flags)
    368  1.6  mycroft 	struct ch_data *ch;
    369  1.6  mycroft 	int flags;
    370  1.1      cgd {
    371  1.1      cgd 	struct scsi_mode_sense scsi_cmd;
    372  1.6  mycroft 	u_char scsi_sense[128];	/* Can't use scsi_mode_sense_data because of
    373  1.6  mycroft 				 * missing block descriptor.
    374  1.6  mycroft 				 */
    375  1.1      cgd 	u_char *b;
    376  1.6  mycroft 	int i, l;
    377  1.6  mycroft 	int error;
    378  1.6  mycroft 	struct scsi_link *sc_link = ch->sc_link;
    379  1.6  mycroft 
    380  1.6  mycroft 	/*
    381  1.6  mycroft 	 * First check if we have it all loaded
    382  1.6  mycroft 	 */
    383  1.6  mycroft 	if (sc_link->flags & SDEV_MEDIA_LOADED)
    384  1.6  mycroft 		return 0;
    385  1.6  mycroft 
    386  1.6  mycroft 	/*
    387  1.6  mycroft 	 * First do a mode sense
    388  1.6  mycroft 	 */
    389  1.6  mycroft 	/* sc_link->flags &= ~SDEV_MEDIA_LOADED; *//*XXX */
    390  1.6  mycroft 	bzero(&scsi_cmd, sizeof(scsi_cmd));
    391  1.6  mycroft 	scsi_cmd.op_code = MODE_SENSE;
    392  1.6  mycroft 	scsi_cmd.byte2 = SMS_DBD;
    393  1.6  mycroft 	scsi_cmd.page = 0x3f;	/* All Pages */
    394  1.6  mycroft 	scsi_cmd.length = sizeof(scsi_sense);
    395  1.6  mycroft 
    396  1.6  mycroft 	/*
    397  1.6  mycroft 	 * Read in the pages
    398  1.6  mycroft 	 */
    399  1.6  mycroft 	error = scsi_scsi_cmd(sc_link, (struct scsi_generic *) &scsi_cmd,
    400  1.6  mycroft 	    sizeof(scsi_cmd), (u_char *) &scsi_sense, sizeof(scsi_sense),
    401  1.6  mycroft 	    CHRETRIES, 5000, NULL, flags | SCSI_DATA_IN);
    402  1.6  mycroft 	if (error) {
    403  1.6  mycroft 		if (!(flags & SCSI_SILENT))
    404  1.6  mycroft 			printf("%s: could not mode sense\n",
    405  1.6  mycroft 			    ch->sc_dev.dv_xname);
    406  1.6  mycroft 		return error;
    407  1.6  mycroft 	}
    408  1.6  mycroft 
    409  1.6  mycroft 	sc_link->flags |= SDEV_MEDIA_LOADED;
    410  1.6  mycroft 	l = scsi_sense[0] - 3;
    411  1.6  mycroft 	b = &scsi_sense[4];
    412  1.6  mycroft 
    413  1.6  mycroft 	/*
    414  1.6  mycroft 	 * To avoid alignment problems
    415  1.6  mycroft 	 */
    416  1.6  mycroft /* XXXX - FIX THIS FOR MSB */
    417  1.6  mycroft #define p2copy(valp)	 (valp[1] | (valp[0]<<8)); valp+=2
    418  1.6  mycroft #define p4copy(valp)	 (valp[3] | (valp[2]<<8) | (valp[1]<<16) | (valp[0]<<24)); valp+=4
    419  1.1      cgd #if 0
    420  1.6  mycroft 	printf("\nmode_sense %d\n", l);
    421  1.6  mycroft 	for (i = 0; i < l + 4; i++)
    422  1.6  mycroft 		printf("%x%c", scsi_sense[i], i % 8 == 7 ? '\n' : ':');
    423  1.1      cgd 	printf("\n");
    424  1.1      cgd #endif
    425  1.6  mycroft 	for (i = 0; i < l;) {
    426  1.6  mycroft 		u_char pc = (*b++) & 0x3f;
    427  1.6  mycroft 		u_char pl = *b++;
    428  1.6  mycroft 		u_char *bb = b;
    429  1.6  mycroft 		switch (pc) {
    430  1.6  mycroft 		case 0x1d:
    431  1.6  mycroft 			ch->chmo = p2copy(bb);
    432  1.6  mycroft 			ch->chms = p2copy(bb);
    433  1.6  mycroft 			ch->sloto = p2copy(bb);
    434  1.6  mycroft 			ch->slots = p2copy(bb);
    435  1.6  mycroft 			ch->imexo = p2copy(bb);
    436  1.6  mycroft 			ch->imexs = p2copy(bb);
    437  1.6  mycroft 			ch->driveo = p2copy(bb);
    438  1.6  mycroft 			ch->drives = p2copy(bb);
    439  1.6  mycroft 			break;
    440  1.6  mycroft 		case 0x1e:
    441  1.6  mycroft 			ch->rot = *b & 0x1;
    442  1.1      cgd 			break;
    443  1.6  mycroft 		case 0x1f:
    444  1.6  mycroft 			ch->stor = *b & 0xf;
    445  1.6  mycroft 			bb += 2;
    446  1.6  mycroft 			ch->stor = p4copy(bb);
    447  1.1      cgd 			break;
    448  1.1      cgd 		default:
    449  1.6  mycroft 			break;
    450  1.1      cgd 		}
    451  1.6  mycroft 		b += pl;
    452  1.6  mycroft 		i += pl + 2;
    453  1.1      cgd 	}
    454  1.6  mycroft 	SC_DEBUG(sc_link, SDEV_DB2,
    455  1.6  mycroft 	    (" cht(%d-%d)slot(%d-%d)imex(%d-%d)cts(%d-%d) %s rotate\n",
    456  1.6  mycroft 	    ch->chmo, ch->chms, ch->sloto, ch->slots, ch->imexo, ch->imexs,
    457  1.6  mycroft 	    ch->driveo, ch->drives, ch->rot ? "can" : "can't"));
    458  1.6  mycroft 	return 0;
    459  1.1      cgd }
    460