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