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