Home | History | Annotate | Line # | Download | only in scsipi
st.c revision 1.3
      1 /*
      2  * Written by Julian Elischer (julian (at) tfs.com)
      3  * Hacked by Theo de Raadt <deraadt (at) fsa.ca>
      4  * for TRW Financial Systems for use under the MACH(2.5) operating system.
      5  *
      6  * TRW Financial Systems, in accordance with their agreement with Carnegie
      7  * Mellon University, makes this software available to CMU to distribute
      8  * or use in any manner that they see fit as long as this message is kept with
      9  * the software. For this reason TFS also grants any other persons or
     10  * organisations permission to use or modify this software.
     11  *
     12  * TFS supplies this software to be publicly redistributed
     13  * on the understanding that TFS is not responsible for the correct
     14  * functioning of this software in any circumstances.
     15  */
     16 
     17 /*
     18  * To do:
     19  * work out some better way of guessing what a good timeout is going
     20  * to be depending on whether we expect to retension or not.
     21  *
     22  */
     23 
     24 #include "st.h"
     25 #undef NST
     26 #define NST 32
     27 
     28 #include "sys/types.h"
     29 #include "sys/param.h"
     30 #include "sys/systm.h"
     31 #include "sys/errno.h"
     32 #include "sys/malloc.h"
     33 #include "sys/ioctl.h"
     34 #include "sys/buf.h"
     35 #include "sys/proc.h"
     36 #include "sys/user.h"
     37 #include "sys/mtio.h"
     38 #include "sys/dkbad.h"
     39 #include "sys/disklabel.h"
     40 #include "scsi/scsi_all.h"
     41 #include "scsi/scsi_tape.h"
     42 #include "scsi/scsiconf.h"
     43 #include "scsi/stdefs.h"
     44 
     45 long int ststrats, stqueues;
     46 
     47 #define	ST_RETRIES	4
     48 
     49 #define MODE(z)		((minor(z) & 0x03))
     50 #define DSTY(z)		(((minor(z) >> 2) & 0x03))
     51 #define UNIT(z)		((minor(z) >> 4))
     52 
     53 #define DSTY_QIC120  3
     54 #define DSTY_QIC150  2
     55 #define DSTY_QIC525  1
     56 
     57 #define QIC120     0x0f
     58 #define QIC150     0x10
     59 #define QIC525     0x11
     60 
     61 #define ESUCCESS 0
     62 
     63 int st_debug = 0;
     64 
     65 struct st_data *st_data[NST];
     66 static int next_st_unit = 0;
     67 
     68 /*
     69  * The routine called by the low level scsi routine when it discovers
     70  * A device suitable for this driver
     71  */
     72 int
     73 stattach(int masunit, struct scsi_switch *sw, int physid, int unit)
     74 {
     75 	struct st_data *st;
     76 	unsigned char *tbl;
     77 	int targ, lun, i;
     78 
     79 	targ = physid >> 3;
     80 	lun = physid & 7;
     81 
     82 	/*printf("stattach: st%d at %s%d target %d lun %d\n",
     83 		unit, sw->name, masunit, targ, lun);*/
     84 
     85 	if(unit >= NST)
     86 		return -1;
     87 	if(st_data[unit])
     88 		return -1;
     89 
     90 	st = st_data[unit] = (struct st_data *)malloc(sizeof *st,
     91 		M_TEMP, M_NOWAIT);
     92 	bzero(st, sizeof *st);
     93 
     94 	st->sc_sw = sw;
     95 	st->ctlr = masunit;
     96 	st->targ = targ;
     97 	st->lu = lun;
     98 
     99 	/*
    100 	 * Use the subdriver to request information regarding
    101 	 * the drive. We cannot use interrupts yet, so the
    102 	 * request must specify this.
    103 	 */
    104 	if( st_mode_sense(unit, SCSI_NOSLEEP |  SCSI_NOMASK | SCSI_SILENT))
    105 		printf("st%d at %s%d targ %d lun %d: %d blocks of %d bytes\n",
    106 			unit, sw->name, masunit, targ, lun,
    107 			st->numblks, st->blksiz);
    108 	else
    109 		printf("st%d at %s%d targ %d lun %d: offline\n",
    110 			unit, sw->name, masunit, targ, lun);
    111 
    112 	/*
    113 	 * Set up the bufs for this device
    114 	 */
    115 	st->buf_queue.b_active = 0;
    116 	st->buf_queue.b_actf = 0;
    117 	st->buf_queue.b_actl = 0;
    118 	st->initialized = 1;
    119 	return 0;
    120 }
    121 
    122 /*
    123  *	open the device.
    124 */
    125 int
    126 stopen(dev_t dev)
    127 {
    128 	int errcode = 0;
    129 	int unit, mode, dsty;
    130 	int dsty_code;
    131 	struct st_data *st;
    132 	unit = UNIT(dev);
    133 	mode = MODE(dev);
    134 	dsty = DSTY(dev);
    135 	st = st_data[unit];
    136 
    137 	/*
    138 	 * Check the unit is legal
    139 	 */
    140 	if( unit >= NST ) {
    141 		errcode = ENXIO;
    142 		return errcode;
    143 	}
    144 
    145 	/*
    146 	 * Only allow one at a time
    147 	 */
    148 	if(st->flags & ST_OPEN) {
    149 		printf("already open\n");
    150 		errcode = ENXIO;
    151 		goto bad;
    152 	}
    153 	/*
    154 	 * Set up the mode flags according to the minor number
    155 	 * ensure all open flags are in a known state
    156 	 */
    157 	st->flags &= ~ST_PER_OPEN;
    158 	switch(mode) {
    159 	case 2:
    160 	case 0:
    161 		st->flags &= ~ST_NOREWIND;
    162 		break;
    163 	case 3:
    164 	case 1:
    165 		st->flags |= ST_NOREWIND;
    166 		break;
    167 	default:
    168 		printf("st%d: Bad mode (minor number)%d\n", unit, mode);
    169 		return EINVAL;
    170 	}
    171 	/*
    172 	* Check density code: 0 is drive default
    173 	*/
    174 	switch(dsty) {
    175 	case 0:
    176 		dsty_code = 0;
    177 		break;
    178 	case DSTY_QIC120:
    179 		dsty_code = QIC120;
    180 		break;
    181 	case DSTY_QIC150:
    182 		dsty_code = QIC150;
    183 		break;
    184 	case DSTY_QIC525:
    185 		dsty_code = QIC525;
    186 		break;
    187 	default:
    188 		printf("st%d: Bad density (minor number)%d\n", unit, dsty);
    189 		return EINVAL;
    190 	}
    191 
    192 	if(scsi_debug & (PRINTROUTINES | TRACEOPENS))
    193 		printf("stopen: dev=0x%x (unit %d (of %d))\n", dev, unit, NST);
    194 
    195 	/*
    196 	 * Make sure the device has been initialised
    197 	 */
    198 	if (!st->initialized) {
    199 		printf("uninitialized\n");
    200 		return ENXIO;
    201 	}
    202 
    203 	/*
    204 	 * Check that it is still responding and ok.
    205 	 */
    206 	if(scsi_debug & TRACEOPENS)
    207 		printf("device is ");
    208 	if (!(st_req_sense(unit, 0))) {
    209 		errcode = ENXIO;
    210 		if(scsi_debug & TRACEOPENS)
    211 			printf("not responding\n");
    212 		goto bad;
    213 	}
    214 	if(scsi_debug & TRACEOPENS)
    215 		printf("ok\n");
    216 
    217 	if(!(st_test_ready(unit, 0))) {
    218 		/*printf("st%d not ready\n", unit);*/
    219 		return EIO;
    220 	}
    221 
    222 	if(!st->info_valid)		/* is media new? */
    223 		if(!st_load(unit, LD_LOAD, 0))
    224 			return EIO;
    225 
    226 	if(!st_rd_blk_lim(unit, 0))
    227 		return EIO;
    228 
    229 	if(!st_mode_sense(unit, 0))
    230 		return EIO;
    231 
    232 	if(!st_mode_select(unit, 0, dsty_code))
    233 		return EIO;
    234 
    235 	st->info_valid = TRUE;
    236 
    237 	st_prevent(unit, PR_PREVENT, 0); /* who cares if it fails? */
    238 
    239 	/*
    240 	 * Load the physical device parameters
    241 	 */
    242 	if(scsi_debug & TRACEOPENS)
    243 		printf("Params loaded ");
    244 	st->flags |= ST_OPEN;
    245 
    246 bad:
    247 	return errcode;
    248 }
    249 
    250 /*
    251  * close the device.. only called if we are the LAST
    252  * occurence of an open device
    253 */
    254 int
    255 stclose(dev_t dev)
    256 {
    257 	unsigned char unit, mode;
    258 	struct st_data *st;
    259 
    260 	unit = UNIT(dev);
    261 	mode = MODE(dev);
    262 	st = st_data[unit];
    263 
    264 	if(scsi_debug & TRACEOPENS)
    265 		printf("Closing device");
    266 	if(st->flags & ST_WRITTEN)
    267 		st_write_filemarks(unit, 1, 0);
    268 
    269 	st->flags &= ~ST_WRITTEN;
    270 	switch(mode) {
    271 	case 0:
    272 		st_rewind(unit, FALSE, SCSI_SILENT);
    273 		st_prevent(unit, PR_ALLOW, SCSI_SILENT);
    274 		break;
    275 	case 1:
    276 		st_prevent(unit, PR_ALLOW, SCSI_SILENT);
    277 		break;
    278 	case 2:
    279 		st_rewind(unit, FALSE, SCSI_SILENT);
    280 		st_prevent(unit, PR_ALLOW, SCSI_SILENT);
    281 		st_load(unit, LD_UNLOAD, SCSI_SILENT);
    282 		break;
    283 	case 3:
    284 		st_prevent(unit, PR_ALLOW, SCSI_SILENT);
    285 		st_load(unit, LD_UNLOAD, SCSI_SILENT);
    286 		break;
    287 	default:
    288 		printf("st%d:close: Bad mode (minor number)%d how's it open?\n",
    289 				unit, mode);
    290 		return EINVAL;
    291 	}
    292 	st->flags &= ~ST_PER_OPEN;
    293 	return 0;
    294 }
    295 
    296 /*
    297  * trim the size of the transfer if needed,
    298  * called by physio
    299  * basically the smaller of our min and the scsi driver's*
    300  * minphys
    301  */
    302 void
    303 stminphys(struct buf *bp)
    304 {
    305 	(*(st_data[UNIT(bp->b_dev)]->sc_sw->scsi_minphys))(bp);
    306 }
    307 
    308 /*
    309  * Actually translate the requested transfer into
    310  * one the physical driver can understand
    311  * The transfer is described by a buf and will include
    312  * only one physical transfer.
    313  */
    314 int
    315 ststrategy(struct buf *bp)
    316 {
    317 	struct st_data *st;
    318 	struct buf	 *dp;
    319 	unsigned char unit;
    320 	unsigned int opri;
    321 
    322 	if (bp->b_bcount == 0)
    323 		goto done;
    324 
    325 	ststrats++;
    326 	unit = UNIT((bp->b_dev));
    327 	st = st_data[unit];
    328 	if(scsi_debug & PRINTROUTINES)
    329 		printf("\nststrategy ");
    330 	if(scsi_debug & SHOWREQUESTS)
    331 		printf("st%d: %d bytes @ blk%d\n", unit, bp->b_bcount, bp->b_blkno);
    332 
    333 	/*
    334 	 * Odd sized request on fixed drives are verboten
    335 	 */
    336 	if((st->flags & ST_FIXEDBLOCKS) && bp->b_bcount % st->blkmin) {
    337 		printf("st%d: bad request, must be multiple of %d\n",
    338 			unit, st->blkmin);
    339 		bp->b_error = EIO;
    340 		goto bad;
    341 	}
    342 
    343 	stminphys(bp);
    344 	opri = splbio();
    345 	dp = &st->buf_queue;
    346 
    347 	/*
    348 	 * Place it in the queue of disk activities for this tape*
    349 	 * at the end
    350 	 */
    351 	while ( dp->b_actf)
    352 		dp = dp->b_actf;
    353 	dp->b_actf = bp;
    354 	bp->b_actf = NULL;
    355 
    356 	/*
    357 	 * Tell the device to get going on the transfer if it's
    358 	 * not doing anything, otherwise just wait for completion*
    359 	 */
    360 	ststart(unit);
    361 
    362 	splx(opri);
    363 	return;
    364 bad:
    365 	bp->b_flags |= B_ERROR;
    366 done:
    367 	/*
    368 	 * Correctly set the buf to indicate a completed xfer
    369 	 */
    370 	iodone(bp);
    371 	return;
    372 }
    373 
    374 
    375 /*
    376  * ststart looks to see if there is a buf waiting for the device
    377  * and that the device is not already busy. If both are true,
    378  * It deques the buf and creates a scsi command to perform the
    379  * transfer in the buf. The transfer request will call st_done
    380  * on completion, which will in turn call this routine again
    381  * so that the next queued transfer is performed.
    382  * The bufs are queued by the strategy routine (ststrategy)
    383  *
    384  * This routine is also called after other non-queued requests
    385  * have been made of the scsi driver, to ensure that the queue
    386  * continues to be drained.
    387 */
    388 /* ststart() is called at splbio */
    389 int
    390 ststart(int unit)
    391 {
    392 	struct st_data *st = st_data[unit];
    393 	register struct buf *bp = 0, *dp;
    394 	struct scsi_rw_tape cmd;
    395 	struct scsi_xfer *xs;
    396 	int drivecount, blkno, nblk;
    397 
    398 	if(scsi_debug & PRINTROUTINES)
    399 		printf("ststart%d ", unit);
    400 
    401 	/*
    402 	 * See if there is a buf to do and we are not already
    403 	 * doing one
    404 	 */
    405 	xs = &st->scsi_xfer;
    406 	if(xs->flags & INUSE)
    407 		return;    /* unit already underway */
    408 
    409 trynext:
    410 	if(st->blockwait) {
    411 		wakeup(&st->blockwait);
    412 		return;
    413 	}
    414 
    415 	dp = &st->buf_queue;
    416 	if ((bp = dp->b_actf) != NULL)
    417 		dp->b_actf = bp->b_actf;
    418 	else
    419 		return;
    420 	xs->flags = INUSE;    /* Now ours */
    421 
    422 	/*
    423 	 * We have a buf, now we should move the data into
    424 	 * a scsi_xfer definition and try start it
    425 	 */
    426 
    427 	/*
    428 	 *  If we are at a filemark but have not reported it yet
    429 	 * then we should report it now
    430 	 */
    431 	if(st->flags & ST_AT_FILEMARK) {
    432 		bp->b_error = 0;
    433 		bp->b_flags |= B_ERROR;	/* EOF*/
    434 		st->flags &= ~ST_AT_FILEMARK;
    435 		biodone(bp);
    436 		xs->flags = 0; /* won't need it now */
    437 		goto trynext;
    438 	}
    439 
    440 	/*
    441 	 *  If we are at EOM but have not reported it yet
    442 	 * then we should report it now
    443 	 */
    444 	if(st->flags & ST_AT_EOM) {
    445 		bp->b_error = EIO;
    446 		bp->b_flags |= B_ERROR;
    447 		st->flags &= ~ST_AT_EOM;
    448 		biodone(bp);
    449 		xs->flags = 0; /* won't need it now */
    450 		goto trynext;
    451 	}
    452 
    453 	/*
    454 	 *  Fill out the scsi command
    455 	 */
    456 	bzero(&cmd, sizeof(cmd));
    457 	if((bp->b_flags & B_READ) == B_WRITE) {
    458 		st->flags |= ST_WRITTEN;
    459 		xs->flags |= SCSI_DATA_OUT;
    460 	}
    461 	else
    462 		xs->flags |= SCSI_DATA_IN;
    463 	cmd.op_code = (bp->b_flags & B_READ) ? READ_COMMAND_TAPE : WRITE_COMMAND_TAPE;
    464 
    465 	/*
    466 	 * Handle "fixed-block-mode" tape drives by using the    *
    467 	 * block count instead of the length.
    468 	 */
    469 	if(st->flags & ST_FIXEDBLOCKS) {
    470 		cmd.fixed = 1;
    471 		lto3b(bp->b_bcount/st->blkmin, cmd.len);
    472 	}
    473 	else
    474 		lto3b(bp->b_bcount, cmd.len);
    475 
    476 	/*
    477 	 * Fill out the scsi_xfer structure
    478 	 *	Note: we cannot sleep as we may be an interrupt
    479 	 */
    480 	xs->flags |= SCSI_NOSLEEP;
    481 	xs->adapter = st->ctlr;
    482 	xs->targ = st->targ;
    483 	xs->lu = st->lu;
    484 	xs->retries = 1;	/* can't retry on tape*/
    485 	xs->timeout = 100000; /* allow 100 secs for retension */
    486 	xs->cmd = (struct scsi_generic *)&cmd;
    487 	xs->cmdlen = sizeof(cmd);
    488 	xs->data = (u_char *)bp->b_un.b_addr;
    489 	xs->datalen = bp->b_bcount;
    490 	xs->resid = bp->b_bcount;
    491 	xs->when_done = st_done;
    492 	xs->done_arg = unit;
    493 	xs->done_arg2 = (int)xs;
    494 	xs->error = XS_NOERROR;
    495 	xs->bp = bp;
    496 
    497 #if defined(OSF) || defined(FIX_ME)
    498 	if (bp->b_flags & B_PHYS) {
    499 	 	xs->data = (u_char*)map_pva_kva(bp->b_proc, bp->b_un.b_addr,
    500 			bp->b_bcount, st_window[unit],
    501 			(bp->b_flags&B_READ)?B_WRITE:B_READ);
    502 	} else
    503 		xs->data = (u_char*)bp->b_un.b_addr;
    504 #endif /* defined(OSF) */
    505 
    506 	if ( (*(st->sc_sw->scsi_cmd))(xs) != SUCCESSFULLY_QUEUED) {
    507 		printf("st%d: oops not queued", unit);
    508 		xs->error = XS_DRIVER_STUFFUP;
    509 		st_done(unit, xs);
    510 	}
    511 	stqueues++;
    512 }
    513 
    514 /*
    515  * This routine is called by the scsi interrupt when
    516  * the transfer is complete.
    517 */
    518 int
    519 st_done(int unit, struct scsi_xfer *xs)
    520 {
    521 	struct st_data *st = st_data[unit];
    522 	struct buf *bp;
    523 	int retval;
    524 
    525 	if(scsi_debug & PRINTROUTINES)
    526 		printf("st_done%d ", unit);
    527 	if (! (xs->flags & INUSE))
    528 		panic("scsi_xfer not in use!");
    529 
    530 	if(bp = xs->bp) {
    531 		switch(xs->error) {
    532 		case XS_NOERROR:
    533 			bp->b_flags &= ~B_ERROR;
    534 			bp->b_error = 0;
    535 			bp->b_resid = 0;
    536 			break;
    537 		case XS_SENSE:
    538 			retval = st_interpret_sense(unit, xs);
    539 			if(retval) {
    540 				/*
    541 				 * We have a real error, the bit should
    542 				 * be set to indicate this. The return
    543 				 * value will contain the unix error code*
    544 				 * that the error interpretation routine
    545 				 * thought was suitable, so pass this
    546 				 * value back in the buf structure.
    547 				 * Furthermore we return information
    548 				 * saying that no data was transferred
    549 				 */
    550 				bp->b_flags |= B_ERROR;
    551 				bp->b_error = retval;
    552 				bp->b_resid =  bp->b_bcount;
    553 				st->flags &= ~(ST_AT_FILEMARK|ST_AT_EOM);
    554 			} else if(xs->resid && ( xs->resid != xs->datalen )) {
    555 				/*
    556 				 * Here we have the tricky part..
    557 				 * We successfully read less data than
    558 				 * we requested. (but not 0)
    559 				 *------for variable blocksize tapes:----*
    560 				 * UNDER 386BSD:
    561 				 * We should legitimatly have the error
    562 				 * bit set, with the error value set to
    563 				 * zero.. This is to indicate to the
    564 				 * physio code that while we didn't get
    565 				 * as much information as was requested,
    566 				 * we did reach the end of the record
    567 				 * and so physio should not call us
    568 				 * again for more data... we have it all
    569 				 * SO SET THE ERROR BIT!
    570 				 *
    571 				 * UNDER MACH:(CMU)
    572 				 * To indicate the same as above, we
    573 				 * need only have a non 0 resid that is
    574 				 * less than the b_bcount, but the
    575 				 * ERROR BIT MUST BE CLEAR! (sigh)
    576 				 *
    577 				 * UNDER OSF1:
    578 				 * To indicate the same as above, we
    579 				 * need to have a non 0 resid that is
    580 				 * less than the b_bcount, but the
    581 				 * ERROR BIT MUST BE SET! (gasp)(sigh)
    582 				 *
    583 				 *-------for fixed blocksize device------*
    584 				 * We could have read some successful
    585 				 * records before hitting
    586 				 * the EOF or EOT. These must be passed
    587 				 * to the user, before we report the
    588 				 * EOx. Only if there is no data for the
    589 				 * user do we report it now. (via an EIO
    590 				 * for EOM and resid == count for EOF).
    591 				 * We will report the EOx NEXT time..
    592 				 */
    593 				bp->b_flags |= B_ERROR;
    594 				bp->b_error = 0;
    595 				bp->b_resid = xs->resid;
    596 				if((st->flags & ST_FIXEDBLOCKS)) {
    597 					bp->b_resid *= st->blkmin;
    598 					if(  (st->flags & ST_AT_EOM)
    599 				 	  && (bp->b_resid == bp->b_bcount)) {
    600 						bp->b_error = EIO;
    601 						st->flags &= ~ST_AT_EOM;
    602 					}
    603 				}
    604 				xs->error = XS_NOERROR;
    605 				break;
    606 			} else {
    607 				/*
    608 				 * We have come out of the error handler
    609 				 * with no error code.. we have also
    610 				 * not had an ili (would have gone to
    611 				 * the previous clause). Now we need to
    612 				 * distiguish between succesful read of
    613 				 * no data (EOF or EOM) and successfull
    614 				 * read of all requested data.
    615 				 * At least all o/s agree that:
    616 				 * 0 bytes read with no error is EOF
    617 				 * 0 bytes read with an EIO is EOM
    618 				 */
    619 				bp->b_resid = bp->b_bcount;
    620 				if(st->flags & ST_AT_FILEMARK) {
    621 					st->flags &= ~ST_AT_FILEMARK;
    622 					bp->b_flags &= ~B_ERROR;
    623 					bp->b_error = 0;
    624 					break;
    625 				}
    626 				if(st->flags & ST_AT_EOM) {
    627 					bp->b_flags |= B_ERROR;
    628 					bp->b_error = EIO;
    629 					st->flags &= ~ST_AT_EOM;
    630 					break;
    631 				}
    632 				printf("st%d:error ignored\n", unit);
    633 			}
    634 			break;
    635 		case    XS_TIMEOUT:
    636 			printf("st%d timeout\n", unit);
    637 			break;
    638 		case    XS_BUSY:	/* should retry -- how? */
    639 			/*
    640 			 * SHOULD put buf back at head of queue
    641 			 * and decrement retry count in (*xs)
    642 			 * HOWEVER, this should work as a kludge
    643 			 */
    644 			if(xs->retries--) {
    645 				xs->flags &= ~ITSDONE;
    646 				xs->error = XS_NOERROR;
    647 				if ( (*(st->sc_sw->scsi_cmd))(xs)
    648 				   == SUCCESSFULLY_QUEUED) {
    649 					/* don't wake the job, ok? */
    650 					return;
    651 				}
    652 				printf("device busy");
    653 				xs->flags |= ITSDONE;
    654 			}
    655 		case XS_DRIVER_STUFFUP:
    656 			bp->b_flags |= B_ERROR;
    657 			bp->b_error = EIO;
    658 			break;
    659 		default:
    660 			printf("st%d: unknown error category from scsi driver\n",
    661 				unit);
    662 		}
    663 		biodone(bp);
    664 		xs->flags = 0;	/* no longer in use */
    665 		ststart(unit);		/* If there's another waiting.. do it */
    666 	} else
    667 		wakeup(xs);
    668 }
    669 
    670 /*
    671  * Perform special action on behalf of the user
    672  * Knows about the internals of this device
    673 */
    674 int
    675 stioctl(dev_t dev, int cmd, caddr_t arg, int mode)
    676 {
    677 	struct st_data *st;
    678 	struct mtop *mt;
    679 	struct mtget *g;
    680 	unsigned int opri;
    681 	unsigned char unit;
    682 	register i, j;
    683 	int errcode=0, number, flags, ret;
    684 
    685 	/*
    686 	 * Find the device that the user is talking about
    687 	 */
    688 	flags = 0;	/* give error messages, act on errors etc. */
    689 	unit = UNIT(dev);
    690 	st = st_data[unit];
    691 
    692 	switch(cmd) {
    693 	case MTIOCGET:
    694 		g = (struct mtget *)arg;
    695 		bzero(g, sizeof *g);
    696 		g->mt_type = 0x7;	/* Ultrix compat */
    697 		ret=TRUE;
    698 		break;
    699 	case MTIOCTOP:
    700 		mt = (struct mtop *)arg;
    701 
    702 		if (st_debug)
    703 			printf("[sctape_sstatus: %x %x]\n", mt->mt_op, mt->mt_count);
    704 
    705 		/* compat: in U*x it is a short */
    706 		number = mt->mt_count;
    707 		switch ((short)(mt->mt_op)) {
    708 		case MTWEOF:	/* write an end-of-file record */
    709 			ret = st_write_filemarks(unit, number, flags);
    710 			st->flags &= ~ST_WRITTEN;
    711 			break;
    712 		case MTFSF:	/* forward space file */
    713 			ret = st_space(unit, number, SP_FILEMARKS, flags);
    714 			break;
    715 		case MTBSF:	/* backward space file */
    716 			ret = st_space(unit, -number, SP_FILEMARKS, flags);
    717 			break;
    718 		case MTFSR:	/* forward space record */
    719 			ret = st_space(unit, number, SP_BLKS, flags);
    720 			break;
    721 		case MTBSR:	/* backward space record */
    722 			ret = st_space(unit, -number, SP_BLKS, flags);
    723 			break;
    724 		case MTREW:	/* rewind */
    725 			ret = st_rewind(unit, FALSE, flags);
    726 			break;
    727 		case MTOFFL:	/* rewind and put the drive offline */
    728 			if((ret = st_rewind(unit, FALSE, flags))) {
    729 				st_prevent(unit, PR_ALLOW, 0);
    730 				ret = st_load(unit, LD_UNLOAD, flags);
    731 			} else
    732 				printf("rewind failed, unit still loaded\n");
    733 			break;
    734 		case MTNOP:	/* no operation, sets status only */
    735 		case MTCACHE:	/* enable controller cache */
    736 		case MTNOCACHE:	/* disable controller cache */
    737 			ret = TRUE;
    738 			break;
    739 		default:
    740 			return EINVAL;
    741 		}
    742 		break;
    743 	case MTIOCIEOT:
    744 	case MTIOCEEOT:
    745 		ret=TRUE;
    746 		break;
    747 	}
    748 	return ret ? ESUCCESS : EIO;
    749 }
    750 
    751 
    752 /*
    753  * Check with the device that it is ok, (via scsi driver)*
    754 */
    755 int
    756 st_req_sense(int unit, int flags)
    757 {
    758 	struct scsi_sense_data sense;
    759 	struct scsi_sense	scsi_cmd;
    760 
    761 	bzero(&scsi_cmd, sizeof(scsi_cmd));
    762 	scsi_cmd.op_code = REQUEST_SENSE;
    763 	scsi_cmd.length = sizeof(sense);
    764 
    765 	if (st_scsi_cmd(unit, (struct scsi_generic *)&scsi_cmd,
    766 	    sizeof(scsi_cmd), (u_char *)&sense, sizeof(sense),
    767 	    100000, flags | SCSI_DATA_IN) != 0)
    768 		return FALSE;
    769 	else
    770 		return TRUE;
    771 }
    772 
    773 /*
    774  * Get scsi driver to send a "are you ready" command
    775 */
    776 int
    777 st_test_ready(int unit, int flags)
    778 {
    779 	struct scsi_test_unit_ready scsi_cmd;
    780 
    781 	bzero(&scsi_cmd, sizeof(scsi_cmd));
    782 	scsi_cmd.op_code = TEST_UNIT_READY;
    783 
    784 	if (st_scsi_cmd(unit, (struct scsi_generic *)&scsi_cmd,
    785 	    sizeof(scsi_cmd), (u_char *)0, 0, 100000, flags) != 0)
    786 		return FALSE;
    787 	else
    788 		return TRUE;
    789 }
    790 
    791 
    792 #ifdef	__STDC__
    793 #define b2tol(a)	(((unsigned)(a##_1) << 8) + (unsigned)a##_0 )
    794 #else
    795 #define b2tol(a)	(((unsigned)(a/**/_1) << 8) + (unsigned)a/**/_0 )
    796 #endif
    797 
    798 /*
    799  * Ask the drive what it's min and max blk sizes are.
    800 */
    801 int
    802 st_rd_blk_lim(int unit, int flags)
    803 {
    804 	struct st_data *st = st_data[unit];
    805 	struct scsi_blk_limits scsi_cmd;
    806 	struct scsi_blk_limits_data scsi_blkl;
    807 
    808 	st = st_data[unit];
    809 
    810 	/*
    811 	 * First check if we have it all loaded
    812 	 */
    813 	if (st->info_valid)
    814 		goto done;
    815 
    816 	/*
    817 	 * do a 'Read Block Limits'
    818 	 */
    819 	bzero(&scsi_cmd, sizeof(scsi_cmd));
    820 	scsi_cmd.op_code = READ_BLK_LIMITS;
    821 
    822 	/*
    823 	 * do the command,	update the global values
    824 	 */
    825 	if (st_scsi_cmd(unit, (struct scsi_generic *)&scsi_cmd,
    826 	    sizeof(scsi_cmd), (u_char *)&scsi_blkl, sizeof(scsi_blkl),
    827 	    5000, flags | SCSI_DATA_IN) != 0) {
    828 		if(!(flags & SCSI_SILENT))
    829 			printf("could not get blk limits for unit %d\n", unit);
    830 		st->info_valid = FALSE;
    831 		return FALSE;
    832 	}
    833 	if (st_debug)
    834 		printf(" (%d <= blksiz <= %d\n) ", b2tol(scsi_blkl.min_length),
    835 			_3btol(&scsi_blkl.max_length_2));
    836 
    837 	st->blkmin = b2tol(scsi_blkl.min_length);
    838 	st->blkmax = _3btol(&scsi_blkl.max_length_2);
    839 
    840 done:
    841 	if(st->blkmin && (st->blkmin == st->blkmax))
    842 		st->flags |= ST_FIXEDBLOCKS;
    843 	return TRUE;
    844 }
    845 
    846 /*
    847  * Get the scsi driver to send a full inquiry to the
    848  * device and use the results to fill out the global
    849  * parameter structure.
    850 */
    851 int
    852 st_mode_sense(int unit, int flags)
    853 {
    854 	struct st_data *st = st_data[unit];
    855 	struct scsi_mode_sense scsi_cmd;
    856 	struct {
    857 		struct scsi_mode_header_tape header;
    858 		struct blk_desc	blk_desc;
    859 	} scsi_s;
    860 
    861 	/*
    862 	 * First check if we have it all loaded
    863 	 */
    864 	if(st->info_valid)
    865 		return TRUE;
    866 	/*
    867 	 * First do a mode sense
    868 	 */
    869 	bzero(&scsi_cmd, sizeof(scsi_cmd));
    870 	scsi_cmd.op_code = MODE_SENSE;
    871 	scsi_cmd.length = sizeof(scsi_s);
    872 
    873 	/*
    874 	 * do the command, but we don't need the results
    875 	 * just print them for our interest's sake
    876 	 */
    877 	if (st_scsi_cmd(unit, (struct scsi_generic *)&scsi_cmd,
    878 	    sizeof(scsi_cmd), (u_char *)&scsi_s, sizeof(scsi_s),
    879 	    5000, flags | SCSI_DATA_IN) != 0) {
    880 		if(!(flags & SCSI_SILENT))
    881 			printf("could not mode sense for unit %d\n", unit);
    882 		st->info_valid = FALSE;
    883 		return FALSE;
    884 	}
    885 	if (st_debug)
    886 		printf("unit %d: %d blocks of %d bytes, write %s, %sbuffered\n",
    887 			unit,
    888 			_3btol((u_char *)&scsi_s.blk_desc.nblocks),
    889 			_3btol((u_char *)&scsi_s.blk_desc.blklen),
    890 			scsi_s.header.write_protected ? "protected" : "enabled",
    891 			scsi_s.header.buf_mode ? "" : "un");
    892 
    893 	st->numblks = _3btol((u_char *)&scsi_s.blk_desc.nblocks);
    894 	st->blksiz = _3btol((u_char *)&scsi_s.blk_desc.blklen);
    895 	return TRUE;
    896 }
    897 
    898 /*
    899  * Get the scsi driver to send a full inquiry to the
    900  * device and use the results to fill out the global
    901  * parameter structure.
    902 */
    903 int
    904 st_mode_select(int unit, int flags, int dsty_code)
    905 {
    906 	struct st_data *st = st_data[unit];
    907 	struct scsi_mode_select scsi_cmd;
    908 	struct {
    909 		struct scsi_mode_header_tape header;
    910 		struct blk_desc	blk_desc;
    911 	} dat;
    912 
    913 	/*
    914 	 * Set up for a mode select
    915 	 */
    916 	bzero(&dat, sizeof(dat));
    917 	bzero(&scsi_cmd, sizeof(scsi_cmd));
    918 	scsi_cmd.op_code = MODE_SELECT;
    919 	scsi_cmd.length = sizeof(dat);
    920 	dat.header.blk_desc_len = sizeof(struct  blk_desc);
    921 	dat.header.buf_mode = 1;
    922 	dat.blk_desc.density = dsty_code;
    923 	if(st->flags & ST_FIXEDBLOCKS)
    924 		lto3b(st->blkmin, dat.blk_desc.blklen);
    925 
    926 /*	lto3b( st->numblks, dat.blk_desc.nblocks); use defaults!!!!
    927 	lto3b( st->blksiz, dat.blk_desc.blklen);
    928  */
    929 	/*
    930 	 * do the command
    931 	 */
    932 	if (st_scsi_cmd(unit, (struct scsi_generic *)&scsi_cmd,
    933 	    sizeof(scsi_cmd), (u_char *)&dat, sizeof(dat),
    934 	    5000, flags | SCSI_DATA_OUT) != 0) {
    935 		if(!(flags & SCSI_SILENT))
    936 			printf("could not mode select for unit %d\n", unit);
    937 		st->info_valid = FALSE;
    938 		return FALSE;
    939 	}
    940 	return TRUE;
    941 }
    942 
    943 /*
    944  * skip N blocks/filemarks/seq filemarks/eom
    945 */
    946 int
    947 st_space(int unit, int number, int what, int flags)
    948 {
    949 	struct st_data *st = st_data[unit];
    950 	struct scsi_space scsi_cmd;
    951 
    952 	/* if we are at a filemark now, we soon won't be*/
    953 	st->flags &= ~(ST_AT_FILEMARK | ST_AT_EOM);
    954 	bzero(&scsi_cmd, sizeof(scsi_cmd));
    955 	scsi_cmd.op_code = SPACE;
    956 	scsi_cmd.code = what;
    957 	lto3b(number, scsi_cmd.number);
    958 	if (st_scsi_cmd(unit, (struct scsi_generic *)&scsi_cmd,
    959 	    sizeof(scsi_cmd), (u_char *)0, 0, 600000, flags) != 0) {
    960 		if(!(flags & SCSI_SILENT))
    961 			printf("could not space st%d\n", unit);
    962 		st->info_valid = FALSE;
    963 		return FALSE;
    964 	}
    965 	return TRUE;
    966 }
    967 
    968 /*
    969  * write N filemarks
    970 */
    971 int
    972 st_write_filemarks(int unit, int number, int flags)
    973 {
    974 	struct st_data *st = st_data[unit];
    975 	struct scsi_write_filemarks scsi_cmd;
    976 
    977 	st->flags &= ~(ST_AT_FILEMARK);
    978 	bzero(&scsi_cmd, sizeof(scsi_cmd));
    979 	scsi_cmd.op_code = WRITE_FILEMARKS;
    980 	lto3b(number, scsi_cmd.number);
    981 	if (st_scsi_cmd(unit, (struct scsi_generic *)&scsi_cmd,
    982 	    sizeof(scsi_cmd), (u_char *)0, 0, 100000, flags) != 0) {
    983 		if(!(flags & SCSI_SILENT))
    984 			printf("could not write_filemarks st%d\n", unit);
    985 		st->info_valid = FALSE;
    986 		return FALSE;
    987 	}
    988 	return TRUE;
    989 }
    990 
    991 /*
    992  * load /unload (with retension if true)
    993 */
    994 int
    995 st_load(int unit, int type, int flags)
    996 {
    997 	struct st_data *st = st_data[unit];
    998 	struct  scsi_load  scsi_cmd;
    999 
   1000 	st->flags &= ~(ST_AT_FILEMARK | ST_AT_EOM);
   1001 	bzero(&scsi_cmd, sizeof(scsi_cmd));
   1002 	scsi_cmd.op_code = LOAD_UNLOAD;
   1003 	scsi_cmd.load=type;
   1004 	if (type == LD_LOAD)
   1005 	{
   1006 		/*scsi_cmd.reten=TRUE;*/
   1007 		scsi_cmd.reten=FALSE;
   1008 	}
   1009 	else
   1010 	{
   1011 		scsi_cmd.reten=FALSE;
   1012 	}
   1013 	if (st_scsi_cmd(unit, (struct scsi_generic *)&scsi_cmd,
   1014 	    sizeof(scsi_cmd), (u_char *)0, 0, 30000, flags) != 0) {
   1015 		if(!(flags & SCSI_SILENT))
   1016 			printf("cannot load/unload  st%d\n", unit);
   1017 		st->info_valid = FALSE;
   1018 		return FALSE;
   1019 	}
   1020 	return TRUE;
   1021 }
   1022 
   1023 /*
   1024  * Prevent or allow the user to remove the tape
   1025 */
   1026 int
   1027 st_prevent(int unit, int type, int flags)
   1028 {
   1029 	struct st_data *st = st_data[unit];
   1030 	struct scsi_prevent	scsi_cmd;
   1031 
   1032 	bzero(&scsi_cmd, sizeof(scsi_cmd));
   1033 	scsi_cmd.op_code = PREVENT_ALLOW;
   1034 	scsi_cmd.prevent=type;
   1035 	if (st_scsi_cmd(unit, (struct scsi_generic *)&scsi_cmd,
   1036 	    sizeof(scsi_cmd), (u_char *)0, 0, 5000, flags) != 0) {
   1037 		if(!(flags & SCSI_SILENT))
   1038 			printf("cannot prevent/allow on st%d\n", unit);
   1039 		st->info_valid = FALSE;
   1040 		return FALSE;
   1041 	}
   1042 	return TRUE;
   1043 }
   1044 
   1045 /*
   1046  *  Rewind the device
   1047 */
   1048 int
   1049 st_rewind(int unit, int immed, int flags)
   1050 {
   1051 	struct st_data *st = st_data[unit];
   1052 	struct scsi_rewind	scsi_cmd;
   1053 
   1054 	st->flags &= ~(ST_AT_FILEMARK | ST_AT_EOM);
   1055 	bzero(&scsi_cmd, sizeof(scsi_cmd));
   1056 	scsi_cmd.op_code = REWIND;
   1057 	scsi_cmd.immed=immed;
   1058 	if (st_scsi_cmd(unit, (struct scsi_generic *)&scsi_cmd,
   1059 	    sizeof(scsi_cmd), (u_char *)0, 0, immed?5000:300000, flags) != 0) {
   1060 		if(!(flags & SCSI_SILENT))
   1061 			printf("could not rewind st%d\n", unit);
   1062 		st->info_valid = FALSE;
   1063 		return FALSE;
   1064 	}
   1065 	return TRUE;
   1066 }
   1067 
   1068 /*
   1069  * ask the scsi driver to perform a command for us.
   1070  * Call it through the switch table, and tell it which
   1071  * sub-unit we want, and what target and lu we wish to
   1072  * talk to. Also tell it where to find the command
   1073  * how long int is.
   1074  * Also tell it where to read/write the data, and how
   1075  * long the data is supposed to be
   1076 */
   1077 int
   1078 st_scsi_cmd(int unit, struct scsi_generic *scsi_cmd, int cmdlen,
   1079 	u_char *data_addr, int datalen, int timeout, int flags)
   1080 {
   1081 	struct st_data *st = st_data[unit];
   1082 	struct scsi_xfer *xs;
   1083 	int retval, s;
   1084 
   1085 	if(scsi_debug & PRINTROUTINES)
   1086 		printf("\nst_scsi_cmd%d ", unit);
   1087 	if(!st->sc_sw) {
   1088 		printf("st%d: not set up\n", unit);
   1089 		return EINVAL;
   1090 	}
   1091 
   1092 	xs = &st->scsi_xfer;
   1093 	if(!(flags & SCSI_NOMASK))
   1094 		s = splbio();
   1095 	st->blockwait++;	/* there is someone waiting */
   1096 	while (xs->flags & INUSE)
   1097 		sleep(&st->blockwait, PRIBIO+1);
   1098 	st->blockwait--;
   1099 	xs->flags = INUSE;
   1100 	if(!(flags & SCSI_NOMASK))
   1101 		splx(s);
   1102 
   1103 	/*
   1104 	 * Fill out the scsi_xfer structure
   1105 	 */
   1106 	xs->flags	|= flags;
   1107 	xs->adapter = st->ctlr;
   1108 	xs->targ = st->targ;
   1109 	xs->lu = st->lu;
   1110 	xs->retries = ST_RETRIES;
   1111 	xs->timeout = timeout;
   1112 	xs->cmd = scsi_cmd;
   1113 	xs->cmdlen = cmdlen;
   1114 	xs->data = data_addr;
   1115 	xs->datalen = datalen;
   1116 	xs->resid = datalen;
   1117 	xs->when_done = (flags & SCSI_NOMASK) ? (int (*)())0 : st_done;
   1118 	xs->done_arg = unit;
   1119 	xs->done_arg2 = (int)xs;
   1120 retry:
   1121 	xs->error = XS_NOERROR;
   1122 	xs->bp = 0;
   1123 	retval = (*(st->sc_sw->scsi_cmd))(xs);
   1124 	switch(retval) {
   1125 	case SUCCESSFULLY_QUEUED:
   1126 		s = splbio();
   1127 		while(!(xs->flags & ITSDONE))
   1128 			sleep(xs,PRIBIO+1);
   1129 		splx(s);
   1130 	case HAD_ERROR:
   1131 	case COMPLETE:
   1132 		switch(xs->error) {
   1133 		case XS_NOERROR:
   1134 			retval = ESUCCESS;
   1135 			break;
   1136 		case XS_SENSE:
   1137 			retval = st_interpret_sense(unit, xs);
   1138 			/* only useful for reads */
   1139 			if (retval)
   1140 				st->flags &= ~(ST_AT_FILEMARK | ST_AT_EOM);
   1141 			else {
   1142 				xs->error = XS_NOERROR;
   1143 				retval = ESUCCESS;
   1144 			}
   1145 			break;
   1146 		case XS_DRIVER_STUFFUP:
   1147 			retval = EIO;
   1148 			break;
   1149 		case    XS_TIMEOUT:
   1150 		case    XS_BUSY:
   1151 			if(xs->retries-- ) {
   1152 				xs->flags &= ~ITSDONE;
   1153 				goto retry;
   1154 			}
   1155 			retval = EIO;
   1156 			break;
   1157 		default:
   1158 			retval = EIO;
   1159 			printf("st%d: unknown error category from scsi driver\n",
   1160 				unit);
   1161 			break;
   1162 		}
   1163 		break;
   1164 	case 	TRY_AGAIN_LATER:
   1165 		if(xs->retries--) {
   1166 			xs->flags &= ~ITSDONE;
   1167 			goto retry;
   1168 		}
   1169 		retval = EIO;
   1170 		break;
   1171 	default:
   1172 		retval = EIO;
   1173 	}
   1174 	xs->flags = 0;	/* it's free! */
   1175 	ststart(unit);
   1176 
   1177 	return retval;
   1178 }
   1179 
   1180 /*
   1181  * Look at the returned sense and act on the error and detirmine
   1182  * The unix error number to pass back... (0 = report no error)
   1183 */
   1184 
   1185 int
   1186 st_interpret_sense(int unit, struct scsi_xfer *xs)
   1187 {
   1188 	struct st_data *st = st_data[unit];
   1189 	struct scsi_sense_data *sense;
   1190 	int silent = xs->flags & SCSI_SILENT, key;
   1191 
   1192 	/*
   1193 	 * If errors are ok, report a success
   1194 	 */
   1195 	if(xs->flags & SCSI_ERR_OK)
   1196 		return ESUCCESS;
   1197 
   1198 	/*
   1199 	 * Get the sense fields and work out what CLASS
   1200 	 */
   1201 	sense = &(xs->sense);
   1202 	if(st_debug) {
   1203 		int count = 0;
   1204 		printf("code%x class%x valid%x\n", sense->error_code,
   1205 			sense->error_class, sense->valid);
   1206 		printf("seg%x key%x ili%x eom%x fmark%x\n",
   1207 			sense->ext.extended.segment, sense->ext.extended.sense_key,
   1208 			sense->ext.extended.ili, sense->ext.extended.eom,
   1209 			sense->ext.extended.filemark);
   1210 		printf("info: %x %x %x %x followed by %d extra bytes\n",
   1211 			sense->ext.extended.info[0], sense->ext.extended.info[1],
   1212 			sense->ext.extended.info[2], sense->ext.extended.info[3],
   1213 			sense->ext.extended.extra_len);
   1214 		printf("extra: ");
   1215 		while(count < sense->ext.extended.extra_len)
   1216 			printf("%x ", sense->ext.extended.extra_bytes[count++]);
   1217 		printf("\n");
   1218 	}
   1219 
   1220 	switch(sense->error_class) {
   1221 	case 0:
   1222 	case 1:
   1223 	case 2:
   1224 	case 3:
   1225 	case 4:
   1226 	case 5:
   1227 	case 6:
   1228 		if(!silent)
   1229 			printf("st%d: error class %d code %d\n", unit,
   1230 				sense->error_class, sense->error_code);
   1231 		if(sense->valid)
   1232 			if(!silent)
   1233 				printf("block no. %d (decimal)\n",
   1234 					(sense->ext.unextended.blockhi <<16),
   1235 					+ (sense->ext.unextended.blockmed <<8),
   1236 					+ (sense->ext.unextended.blocklow ));
   1237 		return EIO;
   1238 	case 7:
   1239 		/*
   1240 		 * If it's class 7, use the extended stuff and interpret
   1241 		 * the key
   1242 		 */
   1243 		if(sense->ext.extended.eom)
   1244 			st->flags |= ST_AT_EOM;
   1245 		if(sense->ext.extended.filemark)
   1246 			st->flags |= ST_AT_FILEMARK;
   1247 
   1248 		if(sense->ext.extended.ili) {
   1249 			if(sense->valid) {
   1250 				/*
   1251 				 * In all ili cases, note that
   1252 				 * the resid is non-0 AND not
   1253 				 * unchanged.
   1254 				 */
   1255 				xs->resid = ntohl(*((long *)sense->ext.extended.info));
   1256 				if(xs->bp) {
   1257 					if(xs->resid < 0) {
   1258 						/* never on block devices */
   1259 						/*
   1260 						 * it's only really bad
   1261 						 * if we have lost data
   1262 						 * (the record was
   1263 						 * bigger than the read)
   1264 						 */
   1265 						return EIO;
   1266 					}
   1267 				}
   1268 			} else
   1269 				printf("BAD length error?");
   1270 		}
   1271 
   1272 		key = sense->ext.extended.sense_key;
   1273 		switch(key) {
   1274 		case 0x0:
   1275 			return ESUCCESS;
   1276 		case 0x1:
   1277 			if(!silent) {
   1278 				printf("st%d: soft error(corrected) ", unit);
   1279 				if(sense->valid) {
   1280 			   		printf("block no. %d (decimal)\n",
   1281 			  		(sense->ext.extended.info[0] <<24)|
   1282 			  		(sense->ext.extended.info[1] <<16)|
   1283 			  		(sense->ext.extended.info[2] <<8)|
   1284 			  		(sense->ext.extended.info[3] ));
   1285 				} else
   1286 			 		printf("\n");
   1287 			}
   1288 			return ESUCCESS;
   1289 		case 0x2:
   1290 			if(!silent)
   1291 				printf("st%d: not ready\n", unit);
   1292 			return ENODEV;
   1293 		case 0x3:
   1294 			if(!silent) {
   1295 				printf("st%d: medium error ", unit);
   1296 				if(sense->valid) {
   1297 			   		printf("block no. %d (decimal)\n",
   1298 			  		(sense->ext.extended.info[0] <<24)|
   1299 			  		(sense->ext.extended.info[1] <<16)|
   1300 			  		(sense->ext.extended.info[2] <<8)|
   1301 			  		(sense->ext.extended.info[3] ));
   1302 				} else
   1303 			 		printf("\n");
   1304 			}
   1305 			return EIO;
   1306 		case 0x4:
   1307 			if(!silent)
   1308 				printf("st%d: non-media hardware failure\n",
   1309 					unit);
   1310 			return EIO;
   1311 		case 0x5:
   1312 			if(!silent)
   1313 				printf("st%d: illegal request\n", unit);
   1314 			return EINVAL;
   1315 		case 0x6:
   1316 			if(!silent)
   1317 				printf("st%d: Unit attention.\n", unit);
   1318 			st->flags &= ~(ST_AT_FILEMARK|ST_AT_EOM);
   1319 			st->info_valid = FALSE;
   1320 			if (st->flags & ST_OPEN) /* TEMP!!!! */
   1321 				return EIO;
   1322 			return ESUCCESS;
   1323 		case 0x7:
   1324 			if(!silent) {
   1325 				printf("st%d: attempted protection violation ",
   1326 					unit);
   1327 				if(sense->valid) {
   1328 			   		printf("block no. %d (decimal)\n",
   1329 			  		(sense->ext.extended.info[0] <<24)|
   1330 			  		(sense->ext.extended.info[1] <<16)|
   1331 			  		(sense->ext.extended.info[2] <<8)|
   1332 			  		(sense->ext.extended.info[3] ));
   1333 				} else
   1334 			 		printf("\n");
   1335 			}
   1336 			return EACCES;
   1337 		case 0x8:
   1338 			if(!silent) {
   1339 				printf("st%d: block wrong state (worm)\n", unit);
   1340 				if(sense->valid) {
   1341 			   		printf("block no. %d (decimal)\n",
   1342 			  		(sense->ext.extended.info[0] <<24)|
   1343 			  		(sense->ext.extended.info[1] <<16)|
   1344 			  		(sense->ext.extended.info[2] <<8)|
   1345 			  		(sense->ext.extended.info[3] ));
   1346 				} else
   1347 			 		printf("\n");
   1348 			}
   1349 			return EIO;
   1350 		case 0x9:
   1351 			if(!silent)
   1352 				printf("st%d: vendor unique\n", unit);
   1353 			return EIO;
   1354 		case 0xa:
   1355 			if(!silent)
   1356 				printf("st%d: copy aborted\n", unit);
   1357 			return EIO;
   1358 		case 0xb:
   1359 			if(!silent)
   1360 				printf("st%d: command aborted\n", unit);
   1361 			return EIO;
   1362 		case 0xc:
   1363 			if(!silent) {
   1364 				printf("st%d: search returned\n", unit);
   1365 				if(sense->valid) {
   1366 			   		printf("block no. %d (decimal)\n",
   1367 			  		(sense->ext.extended.info[0] <<24)|
   1368 			  		(sense->ext.extended.info[1] <<16)|
   1369 			  		(sense->ext.extended.info[2] <<8)|
   1370 			  		(sense->ext.extended.info[3] ));
   1371 				} else
   1372 			 		printf("\n");
   1373 			}
   1374 			return ESUCCESS;
   1375 		case 0xd:
   1376 			if(!silent)
   1377 				printf("st%d: volume overflow\n", unit);
   1378 			return ENOSPC;
   1379 		case 0xe:
   1380 			if(!silent) {
   1381 			 	printf("st%d: verify miscompare\n", unit);
   1382 				if(sense->valid) {
   1383 			   		printf("block no. %d (decimal)\n",
   1384 			  		(sense->ext.extended.info[0] <<24)|
   1385 			  		(sense->ext.extended.info[1] <<16)|
   1386 			  		(sense->ext.extended.info[2] <<8)|
   1387 			  		(sense->ext.extended.info[3] ));
   1388 				} else
   1389 			 		printf("\n");
   1390 			}
   1391 			return EIO;
   1392 		case 0xf:
   1393 			if(!silent)
   1394 				printf("st%d: unknown error key\n", unit);
   1395 			return EIO;
   1396 		}
   1397 		break;
   1398 	}
   1399 	return 0;
   1400 }
   1401