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