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