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