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