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