Home | History | Annotate | Line # | Download | only in dev
mt.c revision 1.2
      1 /*	$NetBSD: mt.c,v 1.2 1995/12/02 18:22:04 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1992, The University of Utah and
      5  * the Computer Systems Laboratory at the University of Utah (CSL).
      6  * All rights reserved.
      7  *
      8  * Permission to use, copy, modify and distribute this software is hereby
      9  * granted provided that (1) source code retains these copyright, permission,
     10  * and disclaimer notices, and (2) redistributions including binaries
     11  * reproduce the notices in supporting documentation, and (3) all advertising
     12  * materials mentioning features or use of this software display the following
     13  * acknowledgement: ``This product includes software developed by the
     14  * Computer Systems Laboratory at the University of Utah.''
     15  *
     16  * THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS
     17  * IS" CONDITION.  THE UNIVERSITY OF UTAH AND CSL DISCLAIM ANY LIABILITY OF
     18  * ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     19  *
     20  * CSL requests users of this software to return to csl-dist (at) cs.utah.edu any
     21  * improvements that they make and grant CSL redistribution rights.
     22  *
     23  *	Utah $Hdr: mt.c 1.8 95/09/12$
     24  */
     25 /*	@(#)mt.c	3.9	90/07/10	mt Xinu
     26  *
     27  * Magnetic tape driver (7974a, 7978a/b, 7979a, 7980a, 7980xc)
     28  * Original version contributed by Mt. Xinu.
     29  * Modified for 4.4BSD by Mark Davies and Andrew Vignaux, Department of
     30  * Computer Science, Victoria University of Wellington
     31  */
     32 #include "mt.h"
     33 #if NMT > 0
     34 
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/buf.h>
     38 #include <sys/ioctl.h>
     39 #include <sys/mtio.h>
     40 #include <sys/file.h>
     41 #include <sys/proc.h>
     42 #include <sys/errno.h>
     43 #include <sys/syslog.h>
     44 #include <sys/tty.h>
     45 #include <sys/kernel.h>
     46 #include <sys/tprintf.h>
     47 
     48 #include <hp300/dev/device.h>
     49 #include <hp300/dev/hpibvar.h>
     50 #include <hp300/dev/mtreg.h>
     51 
     52 
     53 struct	mtinfo {
     54 	u_short	hwid;
     55 	char	*desc;
     56 } mtinfo[] = {
     57 	MT7978ID,	"7978",
     58 	MT7979AID,	"7979A",
     59 	MT7980ID,	"7980",
     60 	MT7974AID,	"7974A",
     61 };
     62 int	nmtinfo = sizeof(mtinfo) / sizeof(mtinfo[0]);
     63 
     64 struct	mt_softc {
     65 	struct	hp_device *sc_hd;
     66 	short	sc_hpibno;	/* logical HPIB this slave it attached to */
     67 	short	sc_slave;	/* HPIB slave address (0-6) */
     68 	short	sc_flags;	/* see below */
     69 	u_char	sc_lastdsj;	/* place for DSJ in mtreaddsj() */
     70 	u_char	sc_lastecmd;	/* place for End Command in mtreaddsj() */
     71 	short	sc_recvtimeo;	/* count of hpibsend timeouts to prevent hang */
     72 	short	sc_statindex;	/* index for next sc_stat when MTF_STATTIMEO */
     73 	struct	mt_stat sc_stat;/* status bytes last read from device */
     74 	short	sc_density;	/* current density of tape (mtio.h format) */
     75 	short	sc_type;	/* tape drive model (hardware IDs) */
     76 	struct	devqueue sc_dq;	/* HPIB device queue member */
     77 	tpr_t	sc_ttyp;
     78 } mt_softc[NMT];
     79 struct	buf mttab[NMT];
     80 struct  buf mtbuf[NMT];
     81 
     82 #ifdef DEBUG
     83 int	mtdebug = 0;
     84 #define	dlog	if (mtdebug) log
     85 #else
     86 #define	dlog	if (0) log
     87 #endif
     88 
     89 #define	UNIT(x)		(minor(x) & 3)
     90 
     91 #define B_CMD		B_XXX		/* command buf instead of data */
     92 #define	b_cmd		b_blkno		/* blkno holds cmd when B_CMD */
     93 
     94 int	mtmatch(), mtintr();
     95 void	mtattach(), mtustart(), mtstart(), mtgo(), mtstrategy();
     96 struct	driver mtdriver = {
     97 	mtmatch, mtattach, "mt", (int (*)()) mtstart, (int (*)()) mtgo, mtintr,
     98 };
     99 
    100 int
    101 mtmatch(hd)
    102 	register struct hp_device *hd;
    103 {
    104 	register int unit;
    105 	register int hpibno = hd->hp_ctlr;
    106 	register int slave = hd->hp_slave;
    107 	register struct mt_softc *sc = &mt_softc[hd->hp_unit];
    108 	register int id;
    109 	register struct buf *bp;
    110 
    111 	sc->sc_hd = hd;
    112 
    113 	for (bp = mttab; bp < &mttab[NMT]; bp++)
    114 		bp->b_actb = &bp->b_actf;
    115 	unit = hpibid(hpibno, slave);
    116 	for (id = 0; id < nmtinfo; id++)
    117 		if (unit == mtinfo[id].hwid)
    118 			return (1);
    119 	return (0);			/* not a known HP magtape */
    120 }
    121 
    122 void
    123 mtattach(hd)
    124 	register struct hp_device *hd;
    125 {
    126 	register int unit;
    127 	register int hpibno = hd->hp_ctlr;
    128 	register int slave = hd->hp_slave;
    129 	register struct mt_softc *sc;
    130 	register int id;
    131 	register struct buf *bp;
    132 
    133 	/* XXX Ick. */
    134 	unit = hpibid(hpibno, slave);
    135 	for (id = 0; id < nmtinfo; id++)
    136 		if (unit == mtinfo[id].hwid)
    137 			break;
    138 
    139 	unit = hd->hp_unit;
    140 	sc = &mt_softc[unit];
    141 	sc->sc_type = mtinfo[id].hwid;
    142 	printf(": %s tape\n", mtinfo[id].desc);
    143 
    144 	sc->sc_hpibno = hpibno;
    145 	sc->sc_slave = slave;
    146 	sc->sc_flags = MTF_EXISTS;
    147 	sc->sc_dq.dq_ctlr = hpibno;
    148 	sc->sc_dq.dq_unit = unit;
    149 	sc->sc_dq.dq_slave = slave;
    150 	sc->sc_dq.dq_driver = &mtdriver;
    151 }
    152 
    153 /*
    154  * Perform a read of "Device Status Jump" register and update the
    155  * status if necessary.  If status is read, the given "ecmd" is also
    156  * performed, unless "ecmd" is zero.  Returns DSJ value, -1 on failure
    157  * and -2 on "temporary" failure.
    158  */
    159 mtreaddsj(unit, ecmd)
    160 	register int unit;
    161 	int ecmd;
    162 {
    163 	register struct mt_softc *sc = &mt_softc[unit];
    164 	int retval;
    165 
    166 	if (sc->sc_flags & MTF_STATTIMEO)
    167 		goto getstats;
    168 	retval = hpibrecv(sc->sc_hpibno,
    169 			  (sc->sc_flags & MTF_DSJTIMEO) ? -1 : sc->sc_slave,
    170 			  MTT_DSJ, &(sc->sc_lastdsj), 1);
    171 	sc->sc_flags &= ~MTF_DSJTIMEO;
    172 	if (retval != 1) {
    173 		dlog(LOG_DEBUG, "mt%d can't hpibrecv DSJ\n", unit);
    174 		if (sc->sc_recvtimeo == 0)
    175 			sc->sc_recvtimeo = hz;
    176 		if (--sc->sc_recvtimeo == 0)
    177 			return (-1);
    178 		if (retval == 0)
    179 			sc->sc_flags |= MTF_DSJTIMEO;
    180 		return (-2);
    181 	}
    182 	sc->sc_recvtimeo = 0;
    183 	sc->sc_statindex = 0;
    184 	dlog(LOG_DEBUG, "mt%d readdsj: 0x%x\n", unit, sc->sc_lastdsj);
    185 	sc->sc_lastecmd = ecmd;
    186 	switch (sc->sc_lastdsj) {
    187 	    case 0:
    188 		if (ecmd & MTE_DSJ_FORCE)
    189 			break;
    190 		return (0);
    191 
    192 	    case 2:
    193 		sc->sc_lastecmd = MTE_COMPLETE;
    194 	    case 1:
    195 		break;
    196 
    197 	    default:
    198 		log(LOG_ERR, "mt%d readdsj: DSJ 0x%x\n", unit, sc->sc_lastdsj);
    199 		return (-1);
    200 	}
    201     getstats:
    202 	retval = hpibrecv(sc->sc_hpibno,
    203 			  (sc->sc_flags & MTF_STATCONT) ? -1 : sc->sc_slave,
    204 			  MTT_STAT, ((char *)&(sc->sc_stat)) + sc->sc_statindex,
    205 			  sizeof(sc->sc_stat) - sc->sc_statindex);
    206 	sc->sc_flags &= ~(MTF_STATTIMEO | MTF_STATCONT);
    207 	if (retval != sizeof(sc->sc_stat) - sc->sc_statindex) {
    208 		if (sc->sc_recvtimeo == 0)
    209 			sc->sc_recvtimeo = hz;
    210 		if (--sc->sc_recvtimeo != 0) {
    211 			if (retval >= 0) {
    212 				sc->sc_statindex += retval;
    213 				sc->sc_flags |= MTF_STATCONT;
    214 			}
    215 			sc->sc_flags |= MTF_STATTIMEO;
    216 			return (-2);
    217 		}
    218 		log(LOG_ERR, "mt%d readdsj: can't read status\n", unit);
    219 		return (-1);
    220 	}
    221 	sc->sc_recvtimeo = 0;
    222 	sc->sc_statindex = 0;
    223 	dlog(LOG_DEBUG, "mt%d readdsj: status is %x %x %x %x %x %x\n", unit,
    224 		sc->sc_stat1, sc->sc_stat2, sc->sc_stat3,
    225 		sc->sc_stat4, sc->sc_stat5, sc->sc_stat6);
    226 	if (sc->sc_lastecmd)
    227 		(void) hpibsend(sc->sc_hpibno, sc->sc_slave,
    228 				MTL_ECMD, &(sc->sc_lastecmd), 1);
    229 	return ((int) sc->sc_lastdsj);
    230 }
    231 
    232 mtopen(dev, flag, mode, p)
    233 	dev_t dev;
    234 	int flag, mode;
    235 	struct proc *p;
    236 {
    237 	register int unit = UNIT(dev);
    238 	register struct mt_softc *sc = &mt_softc[unit];
    239 	register int req_den;
    240 	int error;
    241 
    242 	dlog(LOG_DEBUG, "mt%d open: flags 0x%x\n", unit, sc->sc_flags);
    243 	if (unit >= NMT || (sc->sc_flags & MTF_EXISTS) == 0)
    244 		return (ENXIO);
    245 	if (sc->sc_flags & MTF_OPEN)
    246 		return (EBUSY);
    247 	sc->sc_flags |= MTF_OPEN;
    248 	sc->sc_ttyp = tprintf_open(p);
    249 	if ((sc->sc_flags & MTF_ALIVE) == 0) {
    250 		error = mtcommand(dev, MTRESET, 0);
    251 		if (error != 0 || (sc->sc_flags & MTF_ALIVE) == 0)
    252 			goto errout;
    253 		if ((sc->sc_stat1 & (SR1_BOT | SR1_ONLINE)) == SR1_ONLINE)
    254 			(void) mtcommand(dev, MTREW, 0);
    255 	}
    256 	for (;;) {
    257 		if ((error = mtcommand(dev, MTNOP, 0)) != 0)
    258 			goto errout;
    259 		if (!(sc->sc_flags & MTF_REW))
    260 			break;
    261 		if (tsleep((caddr_t) &lbolt, PCATCH | (PZERO + 1), "mt", 0) != 0) {
    262 			error = EINTR;
    263 			goto errout;
    264 		}
    265 	}
    266 	if ((flag & FWRITE) && (sc->sc_stat1 & SR1_RO)) {
    267 		error = EROFS;
    268 		goto errout;
    269 	}
    270 	if (!(sc->sc_stat1 & SR1_ONLINE)) {
    271 		uprintf("%s: not online\n", sc->sc_hd->hp_xname);
    272 		error = EIO;
    273 		goto errout;
    274 	}
    275 	/*
    276 	 * Select density:
    277 	 *  - find out what density the drive is set to
    278 	 *	(i.e. the density of the current tape)
    279 	 *  - if we are going to write
    280 	 *    - if we're not at the beginning of the tape
    281 	 *      - complain if we want to change densities
    282 	 *    - otherwise, select the mtcommand to set the density
    283 	 *
    284 	 * If the drive doesn't support it then don't change the recorded
    285 	 * density.
    286 	 *
    287 	 * The original MOREbsd code had these additional conditions
    288 	 * for the mid-tape change
    289 	 *
    290 	 *	req_den != T_BADBPI &&
    291 	 *	sc->sc_density != T_6250BPI
    292 	 *
    293 	 * which suggests that it would be possible to write multiple
    294 	 * densities if req_den == T_BAD_BPI or the current tape
    295 	 * density was 6250.  Testing of our 7980 suggests that the
    296 	 * device cannot change densities mid-tape.
    297 	 *
    298 	 * ajv (at) comp.vuw.ac.nz
    299 	 */
    300 	sc->sc_density = (sc->sc_stat2 & SR2_6250) ? T_6250BPI : (
    301 			 (sc->sc_stat3 & SR3_1600) ? T_1600BPI : (
    302 			 (sc->sc_stat3 & SR3_800) ? T_800BPI : -1));
    303 	req_den = (dev & T_DENSEL);
    304 
    305 	if (flag & FWRITE) {
    306 		if (!(sc->sc_stat1 & SR1_BOT)) {
    307 			if (sc->sc_density != req_den) {
    308 				uprintf("%s: can't change density mid-tape\n",
    309 				    sc->sc_hd->hp_xname);
    310 				error = EIO;
    311 				goto errout;
    312 			}
    313 		}
    314 		else {
    315 			int mtset_density =
    316 			    (req_den == T_800BPI  ? MTSET800BPI : (
    317 			     req_den == T_1600BPI ? MTSET1600BPI : (
    318 			     req_den == T_6250BPI ? MTSET6250BPI : (
    319 			     sc->sc_type == MT7980ID
    320 						  ? MTSET6250DC
    321 						  : MTSET6250BPI))));
    322 			if (mtcommand(dev, mtset_density, 0) == 0)
    323 				sc->sc_density = req_den;
    324 		}
    325 	}
    326 	return (0);
    327 errout:
    328 	sc->sc_flags &= ~MTF_OPEN;
    329 	return (error);
    330 }
    331 
    332 mtclose(dev, flag)
    333 	dev_t dev;
    334 	int flag;
    335 {
    336 	register struct mt_softc *sc = &mt_softc[UNIT(dev)];
    337 
    338 	if (sc->sc_flags & MTF_WRT) {
    339 		(void) mtcommand(dev, MTWEOF, 2);
    340 		(void) mtcommand(dev, MTBSF, 0);
    341 	}
    342 	if ((minor(dev) & T_NOREWIND) == 0)
    343 		(void) mtcommand(dev, MTREW, 0);
    344 	sc->sc_flags &= ~MTF_OPEN;
    345 	tprintf_close(sc->sc_ttyp);
    346 	return (0);
    347 }
    348 
    349 mtcommand(dev, cmd, cnt)
    350 	dev_t dev;
    351 	int cmd;
    352 	int cnt;
    353 {
    354 	register struct buf *bp = &mtbuf[UNIT(dev)];
    355 	int error = 0;
    356 
    357 #if 1
    358 	if (bp->b_flags & B_BUSY)
    359 		return (EBUSY);
    360 #endif
    361 	bp->b_cmd = cmd;
    362 	bp->b_dev = dev;
    363 	do {
    364 		bp->b_flags = B_BUSY | B_CMD;
    365 		mtstrategy(bp);
    366 		iowait(bp);
    367 		if (bp->b_flags & B_ERROR) {
    368 			error = (int) (unsigned) bp->b_error;
    369 			break;
    370 		}
    371 	} while (--cnt > 0);
    372 #if 0
    373 	bp->b_flags = 0 /*&= ~B_BUSY*/;
    374 #else
    375 	bp->b_flags &= ~B_BUSY;
    376 #endif
    377 	return (error);
    378 }
    379 
    380 /*
    381  * Only thing to check here is for legal record lengths (writes only).
    382  */
    383 void
    384 mtstrategy(bp)
    385 	register struct buf *bp;
    386 {
    387 	register struct mt_softc *sc;
    388 	register struct buf *dp;
    389 	register int unit;
    390 	register int s;
    391 
    392 	unit = UNIT(bp->b_dev);
    393 	sc = &mt_softc[unit];
    394 	dlog(LOG_DEBUG, "mt%d strategy\n", unit);
    395 	if ((bp->b_flags & (B_CMD | B_READ)) == 0) {
    396 #define WRITE_BITS_IGNORED	8
    397 #if 0
    398 		if (bp->b_bcount & ((1 << WRITE_BITS_IGNORED) - 1)) {
    399 			tprintf(sc->sc_ttyp,
    400 				"%s: write record must be multiple of %d\n",
    401 				sc->sc_hd->hp_xname, 1 << WRITE_BITS_IGNORED);
    402 			goto error;
    403 		}
    404 #endif
    405 		s = 16 * 1024;
    406 		if (sc->sc_stat2 & SR2_LONGREC) {
    407 			switch (sc->sc_density) {
    408 			    case T_1600BPI:
    409 				s = 32 * 1024;
    410 				break;
    411 
    412 			    case T_6250BPI:
    413 			    case T_BADBPI:
    414 				s = 60 * 1024;
    415 				break;
    416 			}
    417 		}
    418 		if (bp->b_bcount > s) {
    419 			tprintf(sc->sc_ttyp,
    420 				"%s: write record (%d) too big: limit (%d)\n",
    421 				sc->sc_hd->hp_xname, bp->b_bcount, s);
    422 	    error:
    423 			bp->b_flags |= B_ERROR;
    424 			bp->b_error = EIO;
    425 			iodone(bp);
    426 			return;
    427 		}
    428 	}
    429 	dp = &mttab[unit];
    430 	bp->b_actf = NULL;
    431 	s = splbio();
    432 	bp->b_actb = dp->b_actb;
    433 	*dp->b_actb = bp;
    434 	dp->b_actb = &bp->b_actf;
    435 	if (dp->b_active == 0) {
    436 		dp->b_active = 1;
    437 		mtustart(unit);
    438 	}
    439 	splx(s);
    440 }
    441 
    442 void
    443 mtustart(unit)
    444 	register int unit;
    445 {
    446 
    447 	dlog(LOG_DEBUG, "mt%d ustart\n", unit);
    448 	if (hpibreq(&(mt_softc[unit].sc_dq)))
    449 		mtstart(unit);
    450 }
    451 
    452 #define hpibppclear(unit) \
    453         { hpib_softc[unit].sc_flags &= ~HPIBF_PPOLL; }
    454 
    455 void
    456 spl_mtintr(unit)
    457 	int unit;
    458 {
    459 	int s = splbio();
    460 
    461 	hpibppclear(mt_softc[unit].sc_hpibno);
    462 	mtintr(unit);
    463 	(void) splx(s);
    464 }
    465 
    466 void
    467 spl_mtstart(unit)
    468 	int unit;
    469 {
    470 	int s = splbio();
    471 
    472 	mtstart(unit);
    473 	(void) splx(s);
    474 }
    475 
    476 void
    477 mtstart(unit)
    478 	register int unit;
    479 {
    480 	register struct mt_softc *sc = &mt_softc[unit];
    481 	register struct buf *bp, *dp;
    482 	short	cmdcount = 1;
    483 	u_char	cmdbuf[2];
    484 
    485 	dlog(LOG_DEBUG, "mt%d start\n", unit);
    486 	sc->sc_flags &= ~MTF_WRT;
    487 	bp = mttab[unit].b_actf;
    488 	if ((sc->sc_flags & MTF_ALIVE) == 0 &&
    489 	    ((bp->b_flags & B_CMD) == 0 || bp->b_cmd != MTRESET))
    490 		goto fatalerror;
    491 
    492 	if (sc->sc_flags & MTF_REW) {
    493 		if (!hpibpptest(sc->sc_hpibno, sc->sc_slave))
    494 			goto stillrew;
    495 		switch (mtreaddsj(unit, MTE_DSJ_FORCE|MTE_COMPLETE|MTE_IDLE)) {
    496 		    case 0:
    497 		    case 1:
    498 		stillrew:
    499 			if ((sc->sc_stat1 & SR1_BOT) ||
    500 			    !(sc->sc_stat1 & SR1_ONLINE)) {
    501 				sc->sc_flags &= ~MTF_REW;
    502 				break;
    503 			}
    504 		    case -2:
    505 			/*
    506 			 * -2 means "timeout" reading DSJ, which is probably
    507 			 * temporary.  This is considered OK when doing a NOP,
    508 			 * but not otherwise.
    509 			 */
    510 			if (sc->sc_flags & (MTF_DSJTIMEO | MTF_STATTIMEO)) {
    511 				timeout(spl_mtstart, (void *)unit, hz >> 5);
    512 				return;
    513 			}
    514 		    case 2:
    515 			if (bp->b_cmd != MTNOP || !(bp->b_flags & B_CMD)) {
    516 				bp->b_error = EBUSY;
    517 				goto errdone;
    518 			}
    519 			goto done;
    520 
    521 		    default:
    522 			goto fatalerror;
    523 		}
    524 	}
    525 	if (bp->b_flags & B_CMD) {
    526 		if (sc->sc_flags & MTF_PASTEOT) {
    527 			switch(bp->b_cmd) {
    528 			    case MTFSF:
    529 			    case MTWEOF:
    530 			    case MTFSR:
    531 				bp->b_error = ENOSPC;
    532 				goto errdone;
    533 
    534 			    case MTBSF:
    535 			    case MTOFFL:
    536 			    case MTBSR:
    537 			    case MTREW:
    538 				sc->sc_flags &= ~(MTF_PASTEOT | MTF_ATEOT);
    539 				break;
    540 			}
    541 		}
    542 		switch(bp->b_cmd) {
    543 		    case MTFSF:
    544 			if (sc->sc_flags & MTF_HITEOF)
    545 				goto done;
    546 			cmdbuf[0] = MTTC_FSF;
    547 			break;
    548 
    549 		    case MTBSF:
    550 			if (sc->sc_flags & MTF_HITBOF)
    551 				goto done;
    552 			cmdbuf[0] = MTTC_BSF;
    553 			break;
    554 
    555 		    case MTOFFL:
    556 			sc->sc_flags |= MTF_REW;
    557 			cmdbuf[0] = MTTC_REWOFF;
    558 			break;
    559 
    560 		    case MTWEOF:
    561 			cmdbuf[0] = MTTC_WFM;
    562 			break;
    563 
    564 		    case MTBSR:
    565 			cmdbuf[0] = MTTC_BSR;
    566 			break;
    567 
    568 		    case MTFSR:
    569 			cmdbuf[0] = MTTC_FSR;
    570 			break;
    571 
    572 		    case MTREW:
    573 			sc->sc_flags |= MTF_REW;
    574 			cmdbuf[0] = MTTC_REW;
    575 			break;
    576 
    577 		    case MTNOP:
    578 			/*
    579 			 * NOP is supposed to set status bits.
    580 			 * Force readdsj to do it.
    581 			 */
    582 			switch (mtreaddsj(unit,
    583 				    MTE_DSJ_FORCE | MTE_COMPLETE | MTE_IDLE)) {
    584 			    default:
    585 				goto done;
    586 
    587 			    case -1:
    588 				/*
    589 				 * If this fails, perform a device clear
    590 				 * to fix any protocol problems and (most
    591 				 * likely) get the status.
    592 				 */
    593 				bp->b_cmd = MTRESET;
    594 				break;
    595 
    596 			    case -2:
    597 				timeout(spl_mtstart, (void *)unit, hz >> 5);
    598 				return;
    599 			}
    600 
    601 		    case MTRESET:
    602 			/*
    603 			 * 1) selected device clear (send with "-2" secondary)
    604 			 * 2) set timeout, then wait for "service request"
    605 			 * 3) interrupt will read DSJ (and END COMPLETE-IDLE)
    606 			 */
    607 			if (hpibsend(sc->sc_hpibno, sc->sc_slave, -2, NULL, 0)){
    608 				log(LOG_ERR, "mt%d can't reset\n", unit);
    609 				goto fatalerror;
    610 			}
    611 			timeout(spl_mtintr, (void *)unit, 4 * hz);
    612 			hpibawait(sc->sc_hpibno, sc->sc_slave);
    613 			return;
    614 
    615 		    case MTSET800BPI:
    616 			cmdbuf[0] = MTTC_800;
    617 			break;
    618 
    619 		    case MTSET1600BPI:
    620 			cmdbuf[0] = MTTC_1600;
    621 			break;
    622 
    623 		    case MTSET6250BPI:
    624 			cmdbuf[0] = MTTC_6250;
    625 			break;
    626 
    627 		    case MTSET6250DC:
    628 			cmdbuf[0] = MTTC_DC6250;
    629 			break;
    630 		}
    631 	} else {
    632 		if (sc->sc_flags & MTF_PASTEOT) {
    633 			bp->b_error = ENOSPC;
    634 			goto errdone;
    635 		}
    636 		if (bp->b_flags & B_READ) {
    637 			sc->sc_flags |= MTF_IO;
    638 			cmdbuf[0] = MTTC_READ;
    639 		} else {
    640 			sc->sc_flags |= MTF_WRT | MTF_IO;
    641 			cmdbuf[0] = MTTC_WRITE;
    642 			cmdbuf[1] = (bp->b_bcount + ((1 << WRITE_BITS_IGNORED) - 1)) >> WRITE_BITS_IGNORED;
    643 			cmdcount = 2;
    644 		}
    645 	}
    646 	if (hpibsend(sc->sc_hpibno, sc->sc_slave, MTL_TCMD, cmdbuf, cmdcount)
    647 	    == cmdcount) {
    648 		if (sc->sc_flags & MTF_REW)
    649 			goto done;
    650 		hpibawait(sc->sc_hpibno);
    651 		return;
    652 	}
    653 fatalerror:
    654 	/*
    655 	 * If anything fails, the drive is probably hosed, so mark it not
    656 	 * "ALIVE" (but it EXISTS and is OPEN or we wouldn't be here, and
    657 	 * if, last we heard, it was REWinding, remember that).
    658 	 */
    659 	sc->sc_flags &= MTF_EXISTS | MTF_OPEN | MTF_REW;
    660 	bp->b_error = EIO;
    661 errdone:
    662 	bp->b_flags |= B_ERROR;
    663 done:
    664 	sc->sc_flags &= ~(MTF_HITEOF | MTF_HITBOF);
    665 	iodone(bp);
    666 	if (dp = bp->b_actf)
    667 		dp->b_actb = bp->b_actb;
    668 	else
    669 		mttab[unit].b_actb = bp->b_actb;
    670 	*bp->b_actb = dp;
    671 	hpibfree(&(sc->sc_dq));
    672 	if ((bp = dp) == NULL)
    673 		mttab[unit].b_active = 0;
    674 	else
    675 		mtustart(unit);
    676 }
    677 
    678 /*
    679  * The Utah code had a bug which meant that the driver was unable to read.
    680  * "rw" was initialized to bp->b_flags & B_READ before "bp" was initialized.
    681  *   -- ajv (at) comp.vuw.ac.nz
    682  */
    683 void
    684 mtgo(unit)
    685 	register int unit;
    686 {
    687 	register struct mt_softc *sc = &mt_softc[unit];
    688 	register struct buf *bp;
    689 	int rw;
    690 
    691 	dlog(LOG_DEBUG, "mt%d go\n", unit);
    692 	bp = mttab[unit].b_actf;
    693 	rw = bp->b_flags & B_READ;
    694 	hpibgo(sc->sc_hpibno, sc->sc_slave, rw ? MTT_READ : MTL_WRITE,
    695 	       bp->b_un.b_addr, bp->b_bcount, rw, rw != 0);
    696 }
    697 
    698 mtintr(unit)
    699 	register int unit;
    700 {
    701 	register struct mt_softc *sc = &mt_softc[unit];
    702 	register struct buf *bp, *dp;
    703 	register int i;
    704 	u_char	cmdbuf[4];
    705 
    706 	bp = mttab[unit].b_actf;
    707 	if (bp == NULL) {
    708 		log(LOG_ERR, "mt%d intr: bp == NULL\n", unit);
    709 		return;
    710 	}
    711 	dlog(LOG_DEBUG, "mt%d intr\n", unit);
    712 	/*
    713 	 * Some operation completed.  Read status bytes and report errors.
    714 	 * Clear EOF flags here `cause they're set once on specific conditions
    715 	 * below when a command succeeds.
    716 	 * A DSJ of 2 always means keep waiting.  If the command was READ
    717 	 * (and we're in data DMA phase) stop data transfer first.
    718 	 */
    719 	sc->sc_flags &= ~(MTF_HITEOF | MTF_HITBOF);
    720 	if ((bp->b_flags & (B_CMD|B_READ)) == B_READ &&
    721 	    !(sc->sc_flags & (MTF_IO | MTF_STATTIMEO | MTF_DSJTIMEO))){
    722 		cmdbuf[0] = MTE_STOP;
    723 		(void) hpibsend(sc->sc_hpibno, sc->sc_slave, MTL_ECMD,cmdbuf,1);
    724 	}
    725 	switch (mtreaddsj(unit, 0)) {
    726 	    case 0:
    727 		break;
    728 
    729 	    case 1:
    730 		/*
    731 		 * If we're in the middle of a READ/WRITE and have yet to
    732 		 * start the data transfer, a DSJ of one should terminate it.
    733 		 */
    734 		sc->sc_flags &= ~MTF_IO;
    735 		break;
    736 
    737 	    case 2:
    738 		(void) hpibawait(sc->sc_hpibno);
    739 		return;
    740 
    741 	    case -2:
    742 		/*
    743 		 * -2 means that the drive failed to respond quickly enough
    744 		 * to the request for DSJ.  It's probably just "busy" figuring
    745 		 * it out and will know in a little bit...
    746 		 */
    747 		timeout(spl_mtintr, (void *)unit, hz >> 5);
    748 		return;
    749 
    750 	    default:
    751 		log(LOG_ERR, "mt%d intr: can't get drive stat\n", unit);
    752 		goto error;
    753 	}
    754 	if (sc->sc_stat1 & (SR1_ERR | SR1_REJECT)) {
    755 		i = sc->sc_stat4 & SR4_ERCLMASK;
    756 		log(LOG_ERR, "%s: %s error, retry %d, SR2/3 %x/%x, code %d\n",
    757 			sc->sc_hd->hp_xname, i == SR4_DEVICE ? "device" :
    758 			(i == SR4_PROTOCOL ? "protocol" :
    759 			(i == SR4_SELFTEST ? "selftest" : "unknown")),
    760 			sc->sc_stat4 & SR4_RETRYMASK, sc->sc_stat2,
    761 			sc->sc_stat3, sc->sc_stat5);
    762 
    763 		if ((bp->b_flags & B_CMD) && bp->b_cmd == MTRESET)
    764 			untimeout(spl_mtintr, (void *)unit);
    765 		if (sc->sc_stat3 & SR3_POWERUP)
    766 			sc->sc_flags &= MTF_OPEN | MTF_EXISTS;
    767 		goto error;
    768 	}
    769 	/*
    770 	 * Report and clear any soft errors.
    771 	 */
    772 	if (sc->sc_stat1 & SR1_SOFTERR) {
    773 		log(LOG_WARNING, "%s: soft error, retry %d\n",
    774 			sc->sc_hd->hp_xname, sc->sc_stat4 & SR4_RETRYMASK);
    775 		sc->sc_stat1 &= ~SR1_SOFTERR;
    776 	}
    777 	/*
    778 	 * We've initiated a read or write, but haven't actually started to
    779 	 * DMA the data yet.  At this point, the drive's ready.
    780 	 */
    781 	if (sc->sc_flags & MTF_IO) {
    782 		sc->sc_flags &= ~MTF_IO;
    783 		if (hpibustart(sc->sc_hpibno))
    784 			mtgo(unit);
    785 		return;
    786 	}
    787 	/*
    788 	 * Check for End Of Tape - we're allowed to hit EOT and then write (or
    789 	 * read) one more record.  If we get here and have not already hit EOT,
    790 	 * return ENOSPC to inform the process that it's hit it.  If we get
    791 	 * here and HAVE already hit EOT, don't allow any more operations that
    792 	 * move the tape forward.
    793 	 */
    794 	if (sc->sc_stat1 & SR1_EOT) {
    795 		if (sc->sc_flags & MTF_ATEOT)
    796 			sc->sc_flags |= MTF_PASTEOT;
    797 		else {
    798 			bp->b_flags |= B_ERROR;
    799 			bp->b_error = ENOSPC;
    800 			sc->sc_flags |= MTF_ATEOT;
    801 		}
    802 	}
    803 	/*
    804 	 * If a motion command was being executed, check for Tape Marks.
    805 	 * If we were doing data, make sure we got the right amount, and
    806 	 * check for hitting tape marks on reads.
    807 	 */
    808 	if (bp->b_flags & B_CMD) {
    809 		if (sc->sc_stat1 & SR1_EOF) {
    810 			if (bp->b_cmd == MTFSR)
    811 				sc->sc_flags |= MTF_HITEOF;
    812 			if (bp->b_cmd == MTBSR)
    813 				sc->sc_flags |= MTF_HITBOF;
    814 		}
    815 		if (bp->b_cmd == MTRESET) {
    816 			untimeout(spl_mtintr, (void *)unit);
    817 			sc->sc_flags |= MTF_ALIVE;
    818 		}
    819 	} else {
    820 		i = hpibrecv(sc->sc_hpibno, sc->sc_slave, MTT_BCNT, cmdbuf, 2);
    821 		if (i != 2) {
    822 			log(LOG_ERR, "mt%d intr: can't get xfer length\n");
    823 			goto error;
    824 		}
    825 		i = (int) *((u_short *) cmdbuf);
    826 		if (i <= bp->b_bcount) {
    827 			if (i == 0)
    828 				sc->sc_flags |= MTF_HITEOF;
    829 			bp->b_resid = bp->b_bcount - i;
    830 			dlog(LOG_DEBUG, "mt%d intr: bcount %d, resid %d\n",
    831 				unit, bp->b_bcount, bp->b_resid);
    832 		} else {
    833 			tprintf(sc->sc_ttyp,
    834 				"%s: record (%d) larger than wanted (%d)\n",
    835 				sc->sc_hd->hp_xname, i, bp->b_bcount);
    836     error:
    837 			sc->sc_flags &= ~MTF_IO;
    838 			bp->b_error = EIO;
    839 			bp->b_flags |= B_ERROR;
    840 		}
    841 	}
    842 	/*
    843 	 * The operation is completely done.
    844 	 * Let the drive know with an END command.
    845 	 */
    846 	cmdbuf[0] = MTE_COMPLETE | MTE_IDLE;
    847 	(void) hpibsend(sc->sc_hpibno, sc->sc_slave, MTL_ECMD, cmdbuf, 1);
    848 	bp->b_flags &= ~B_CMD;
    849 	iodone(bp);
    850 	if (dp = bp->b_actf)
    851 		dp->b_actb = bp->b_actb;
    852 	else
    853 		mttab[unit].b_actb = bp->b_actb;
    854 	*bp->b_actb = dp;
    855 	hpibfree(&(sc->sc_dq));
    856 #if 0
    857 	if (bp /*mttab[unit].b_actf*/ == NULL)
    858 #else
    859 	if (mttab[unit].b_actf == NULL)
    860 #endif
    861 		mttab[unit].b_active = 0;
    862 	else
    863 		mtustart(unit);
    864 }
    865 
    866 mtread(dev, uio)
    867 	dev_t dev;
    868 	struct uio *uio;
    869 {
    870 	return(physio(mtstrategy, &mtbuf[UNIT(dev)], dev, B_READ, minphys, uio));
    871 }
    872 
    873 mtwrite(dev, uio)
    874 	dev_t dev;
    875 	struct uio *uio;
    876 {
    877 	return(physio(mtstrategy, &mtbuf[UNIT(dev)], dev, B_WRITE, minphys, uio));
    878 }
    879 
    880 mtioctl(dev, cmd, data, flag)
    881 	dev_t dev;
    882 	u_long cmd;
    883 	caddr_t data;
    884 	int flag;
    885 {
    886 	register struct mtop *op;
    887 	int cnt;
    888 
    889 	switch (cmd) {
    890 	    case MTIOCTOP:
    891 		op = (struct mtop *)data;
    892 		switch(op->mt_op) {
    893 		    case MTWEOF:
    894 		    case MTFSF:
    895 		    case MTBSR:
    896 		    case MTBSF:
    897 		    case MTFSR:
    898 			cnt = op->mt_count;
    899 			break;
    900 
    901 		    case MTOFFL:
    902 		    case MTREW:
    903 		    case MTNOP:
    904 			cnt = 0;
    905 			break;
    906 
    907 		    default:
    908 			return (EINVAL);
    909 		}
    910 		return (mtcommand(dev, op->mt_op, cnt));
    911 
    912 	    case MTIOCGET:
    913 		break;
    914 
    915 	    default:
    916 		return (EINVAL);
    917 	}
    918 	return (0);
    919 }
    920 
    921 /*ARGSUSED*/
    922 mtdump(dev)
    923 	dev_t dev;
    924 {
    925 	return(ENXIO);
    926 }
    927 
    928 #endif /* NMT > 0 */
    929