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