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