Home | History | Annotate | Line # | Download | only in dev
mt.c revision 1.47
      1 /*	$NetBSD: mt.c,v 1.47 2009/05/18 20:41:57 ad 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.47 2009/05/18 20:41:57 ad 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 	struct mt_softc *sc = device_lookup_private(&mt_cd,UNIT(dev));
    428 	struct buf *bp = &sc->sc_bufstore;
    429 	int error = 0;
    430 
    431 #if 1
    432 	if (bp->b_cflags & BC_BUSY)
    433 		return EBUSY;
    434 #endif
    435 	bp->b_cmd = cmd;
    436 	bp->b_dev = dev;
    437 	do {
    438 		bp->b_cflags = BC_BUSY;
    439 		bp->b_flags = B_CMD;
    440 		mtstrategy(bp);
    441 		biowait(bp);
    442 		if (bp->b_error != 0) {
    443 			error = bp->b_error;
    444 			break;
    445 		}
    446 	} while (--cnt > 0);
    447 #if 0
    448 	bp->b_flags = 0 /*&= ~BC_BUSY*/;
    449 #else
    450 	bp->b_flags &= ~BC_BUSY;
    451 #endif
    452 	return error;
    453 }
    454 
    455 /*
    456  * Only thing to check here is for legal record lengths (writes only).
    457  */
    458 static void
    459 mtstrategy(struct buf *bp)
    460 {
    461 	struct mt_softc *sc;
    462 	int s;
    463 
    464 	sc = device_lookup_private(&mt_cd,UNIT(bp->b_dev));
    465 	dlog(LOG_DEBUG, "%s strategy", device_xname(sc->sc_dev));
    466 	if ((bp->b_flags & (B_CMD | B_READ)) == 0) {
    467 #define WRITE_BITS_IGNORED	8
    468 #if 0
    469 		if (bp->b_bcount & ((1 << WRITE_BITS_IGNORED) - 1)) {
    470 			tprintf(sc->sc_ttyp,
    471 			    "%s: write record must be multiple of %d\n",
    472 			    device_xname(sc->sc_dev), 1 << WRITE_BITS_IGNORED);
    473 			goto error;
    474 		}
    475 #endif
    476 		s = 16 * 1024;
    477 		if (sc->sc_stat2 & SR2_LONGREC) {
    478 			switch (sc->sc_density) {
    479 			    case T_1600BPI:
    480 				s = 32 * 1024;
    481 				break;
    482 
    483 			    case T_6250BPI:
    484 			    case T_BADBPI:
    485 				s = 60 * 1024;
    486 				break;
    487 			}
    488 		}
    489 		if (bp->b_bcount > s) {
    490 			tprintf(sc->sc_ttyp,
    491 			    "%s: write record (%d) too big: limit (%d)\n",
    492 			    device_xname(sc->sc_dev), bp->b_bcount, s);
    493 #if 0 /* XXX see above */
    494 	    error:
    495 #endif
    496 			bp->b_error = EIO;
    497 			biodone(bp);
    498 			return;
    499 		}
    500 	}
    501 	s = splbio();
    502 	bufq_put(sc->sc_tab, bp);
    503 	if (sc->sc_active == 0) {
    504 		sc->sc_active = 1;
    505 		mtustart(sc);
    506 	}
    507 	splx(s);
    508 }
    509 
    510 static void
    511 mtustart(struct mt_softc *sc)
    512 {
    513 
    514 	dlog(LOG_DEBUG, "%s ustart", device_xname(sc->sc_dev));
    515 	if (hpibreq(device_parent(sc->sc_dev), &sc->sc_hq))
    516 		mtstart(sc);
    517 }
    518 
    519 static void
    520 spl_mtintr(void *arg)
    521 {
    522 	struct mt_softc *sc = arg;
    523 	int s = splbio();
    524 
    525 	hpibppclear(sc->sc_hpibno);
    526 	mtintr(sc);
    527 	splx(s);
    528 }
    529 
    530 static void
    531 spl_mtstart(void *arg)
    532 {
    533 	int s = splbio();
    534 
    535 	mtstart(arg);
    536 	splx(s);
    537 }
    538 
    539 static void
    540 mtstart(void *arg)
    541 {
    542 	struct mt_softc *sc = arg;
    543 	struct buf *bp;
    544 	short	cmdcount = 1;
    545 	u_char	cmdbuf[2];
    546 
    547 	dlog(LOG_DEBUG, "%s start", device_xname(sc->sc_dev));
    548 	sc->sc_flags &= ~MTF_WRT;
    549 	bp = bufq_peek(sc->sc_tab);
    550 	if ((sc->sc_flags & MTF_ALIVE) == 0 &&
    551 	    ((bp->b_flags & B_CMD) == 0 || bp->b_cmd != MTRESET))
    552 		goto fatalerror;
    553 
    554 	if (sc->sc_flags & MTF_REW) {
    555 		if (!hpibpptest(sc->sc_hpibno, sc->sc_slave))
    556 			goto stillrew;
    557 		switch (mtreaddsj(sc, MTE_DSJ_FORCE|MTE_COMPLETE|MTE_IDLE)) {
    558 		    case 0:
    559 		    case 1:
    560 		stillrew:
    561 			if ((sc->sc_stat1 & SR1_BOT) ||
    562 			    !(sc->sc_stat1 & SR1_ONLINE)) {
    563 				sc->sc_flags &= ~MTF_REW;
    564 				break;
    565 			}
    566 		    case -2:
    567 			/*
    568 			 * -2 means "timeout" reading DSJ, which is probably
    569 			 * temporary.  This is considered OK when doing a NOP,
    570 			 * but not otherwise.
    571 			 */
    572 			if (sc->sc_flags & (MTF_DSJTIMEO | MTF_STATTIMEO)) {
    573 				callout_reset(&sc->sc_start_ch, hz >> 5,
    574 				    spl_mtstart, sc);
    575 				return;
    576 			}
    577 		    case 2:
    578 			if (bp->b_cmd != MTNOP || !(bp->b_flags & B_CMD)) {
    579 				bp->b_error = EBUSY;
    580 				goto done;
    581 			}
    582 			goto done;
    583 
    584 		    default:
    585 			goto fatalerror;
    586 		}
    587 	}
    588 	if (bp->b_flags & B_CMD) {
    589 		if (sc->sc_flags & MTF_PASTEOT) {
    590 			switch(bp->b_cmd) {
    591 			    case MTFSF:
    592 			    case MTWEOF:
    593 			    case MTFSR:
    594 				bp->b_error = ENOSPC;
    595 				goto done;
    596 
    597 			    case MTBSF:
    598 			    case MTOFFL:
    599 			    case MTBSR:
    600 			    case MTREW:
    601 				sc->sc_flags &= ~(MTF_PASTEOT | MTF_ATEOT);
    602 				break;
    603 			}
    604 		}
    605 		switch(bp->b_cmd) {
    606 		    case MTFSF:
    607 			if (sc->sc_flags & MTF_HITEOF)
    608 				goto done;
    609 			cmdbuf[0] = MTTC_FSF;
    610 			break;
    611 
    612 		    case MTBSF:
    613 			if (sc->sc_flags & MTF_HITBOF)
    614 				goto done;
    615 			cmdbuf[0] = MTTC_BSF;
    616 			break;
    617 
    618 		    case MTOFFL:
    619 			sc->sc_flags |= MTF_REW;
    620 			cmdbuf[0] = MTTC_REWOFF;
    621 			break;
    622 
    623 		    case MTWEOF:
    624 			cmdbuf[0] = MTTC_WFM;
    625 			break;
    626 
    627 		    case MTBSR:
    628 			cmdbuf[0] = MTTC_BSR;
    629 			break;
    630 
    631 		    case MTFSR:
    632 			cmdbuf[0] = MTTC_FSR;
    633 			break;
    634 
    635 		    case MTREW:
    636 			sc->sc_flags |= MTF_REW;
    637 			cmdbuf[0] = MTTC_REW;
    638 			break;
    639 
    640 		    case MTNOP:
    641 			/*
    642 			 * NOP is supposed to set status bits.
    643 			 * Force readdsj to do it.
    644 			 */
    645 			switch (mtreaddsj(sc,
    646 			  MTE_DSJ_FORCE | MTE_COMPLETE | MTE_IDLE)) {
    647 			    default:
    648 				goto done;
    649 
    650 			    case -1:
    651 				/*
    652 				 * If this fails, perform a device clear
    653 				 * to fix any protocol problems and (most
    654 				 * likely) get the status.
    655 				 */
    656 				bp->b_cmd = MTRESET;
    657 				break;
    658 
    659 			    case -2:
    660 				callout_reset(&sc->sc_start_ch, hz >> 5,
    661 				    spl_mtstart, sc);
    662 				return;
    663 			}
    664 
    665 		    case MTRESET:
    666 			/*
    667 			 * 1) selected device clear (send with "-2" secondary)
    668 			 * 2) set timeout, then wait for "service request"
    669 			 * 3) interrupt will read DSJ (and END COMPLETE-IDLE)
    670 			 */
    671 			if (hpibsend(sc->sc_hpibno, sc->sc_slave, -2, NULL, 0)){
    672 				log(LOG_ERR, "%s can't reset",
    673 				    device_xname(sc->sc_dev));
    674 				goto fatalerror;
    675 			}
    676 			callout_reset(&sc->sc_intr_ch, 4 * hz, spl_mtintr, sc);
    677 			hpibawait(sc->sc_hpibno);
    678 			return;
    679 
    680 		    case MTSET800BPI:
    681 			cmdbuf[0] = MTTC_800;
    682 			break;
    683 
    684 		    case MTSET1600BPI:
    685 			cmdbuf[0] = MTTC_1600;
    686 			break;
    687 
    688 		    case MTSET6250BPI:
    689 			cmdbuf[0] = MTTC_6250;
    690 			break;
    691 
    692 		    case MTSET6250DC:
    693 			cmdbuf[0] = MTTC_DC6250;
    694 			break;
    695 		}
    696 	} else {
    697 		if (sc->sc_flags & MTF_PASTEOT) {
    698 			bp->b_error = ENOSPC;
    699 			goto done;
    700 		}
    701 		if (bp->b_flags & B_READ) {
    702 			sc->sc_flags |= MTF_IO;
    703 			cmdbuf[0] = MTTC_READ;
    704 		} else {
    705 			sc->sc_flags |= MTF_WRT | MTF_IO;
    706 			cmdbuf[0] = MTTC_WRITE;
    707 			cmdbuf[1] = (bp->b_bcount + ((1 << WRITE_BITS_IGNORED) - 1)) >> WRITE_BITS_IGNORED;
    708 			cmdcount = 2;
    709 		}
    710 	}
    711 	if (hpibsend(sc->sc_hpibno, sc->sc_slave, MTL_TCMD, cmdbuf, cmdcount)
    712 	    == cmdcount) {
    713 		if (sc->sc_flags & MTF_REW)
    714 			goto done;
    715 		hpibawait(sc->sc_hpibno);
    716 		return;
    717 	}
    718 fatalerror:
    719 	/*
    720 	 * If anything fails, the drive is probably hosed, so mark it not
    721 	 * "ALIVE" (but it EXISTS and is OPEN or we wouldn't be here, and
    722 	 * if, last we heard, it was REWinding, remember that).
    723 	 */
    724 	sc->sc_flags &= MTF_EXISTS | MTF_OPEN | MTF_REW;
    725 	bp->b_error = EIO;
    726 done:
    727 	sc->sc_flags &= ~(MTF_HITEOF | MTF_HITBOF);
    728 	(void)bufq_get(sc->sc_tab);
    729 	biodone(bp);
    730 	hpibfree(device_parent(sc->sc_dev), &sc->sc_hq);
    731 	if ((bp = bufq_peek(sc->sc_tab)) == NULL)
    732 		sc->sc_active = 0;
    733 	else
    734 		mtustart(sc);
    735 }
    736 
    737 /*
    738  * The Utah code had a bug which meant that the driver was unable to read.
    739  * "rw" was initialized to bp->b_flags & B_READ before "bp" was initialized.
    740  *   -- ajv (at) comp.vuw.ac.nz
    741  */
    742 static void
    743 mtgo(void *arg)
    744 {
    745 	struct mt_softc *sc = arg;
    746 	struct buf *bp;
    747 	int rw;
    748 
    749 	dlog(LOG_DEBUG, "%s go", device_xname(sc->sc_dev));
    750 	bp = bufq_peek(sc->sc_tab);
    751 	rw = bp->b_flags & B_READ;
    752 	hpibgo(sc->sc_hpibno, sc->sc_slave, rw ? MTT_READ : MTL_WRITE,
    753 	    bp->b_data, bp->b_bcount, rw, rw != 0);
    754 }
    755 
    756 static void
    757 mtintr(void *arg)
    758 {
    759 	struct mt_softc *sc = arg;
    760 	struct buf *bp;
    761 	int i;
    762 	u_char cmdbuf[4];
    763 
    764 	bp = bufq_peek(sc->sc_tab);
    765 	if (bp == NULL) {
    766 		log(LOG_ERR, "%s intr: bp == NULL", device_xname(sc->sc_dev));
    767 		return;
    768 	}
    769 
    770 	dlog(LOG_DEBUG, "%s intr", device_xname(sc->sc_dev));
    771 
    772 	/*
    773 	 * Some operation completed.  Read status bytes and report errors.
    774 	 * Clear EOF flags here `cause they're set once on specific conditions
    775 	 * below when a command succeeds.
    776 	 * A DSJ of 2 always means keep waiting.  If the command was READ
    777 	 * (and we're in data DMA phase) stop data transfer first.
    778 	 */
    779 	sc->sc_flags &= ~(MTF_HITEOF | MTF_HITBOF);
    780 	if ((bp->b_flags & (B_CMD|B_READ)) == B_READ &&
    781 	    !(sc->sc_flags & (MTF_IO | MTF_STATTIMEO | MTF_DSJTIMEO))){
    782 		cmdbuf[0] = MTE_STOP;
    783 		(void) hpibsend(sc->sc_hpibno, sc->sc_slave, MTL_ECMD,cmdbuf,1);
    784 	}
    785 	switch (mtreaddsj(sc, 0)) {
    786 	    case 0:
    787 		break;
    788 
    789 	    case 1:
    790 		/*
    791 		 * If we're in the middle of a READ/WRITE and have yet to
    792 		 * start the data transfer, a DSJ of one should terminate it.
    793 		 */
    794 		sc->sc_flags &= ~MTF_IO;
    795 		break;
    796 
    797 	    case 2:
    798 		(void) hpibawait(sc->sc_hpibno);
    799 		return;
    800 
    801 	    case -2:
    802 		/*
    803 		 * -2 means that the drive failed to respond quickly enough
    804 		 * to the request for DSJ.  It's probably just "busy" figuring
    805 		 * it out and will know in a little bit...
    806 		 */
    807 		callout_reset(&sc->sc_intr_ch, hz >> 5, spl_mtintr, sc);
    808 		return;
    809 
    810 	    default:
    811 		log(LOG_ERR, "%s intr: can't get drive stat",
    812 		    device_xname(sc->sc_dev));
    813 		goto error;
    814 	}
    815 	if (sc->sc_stat1 & (SR1_ERR | SR1_REJECT)) {
    816 		i = sc->sc_stat4 & SR4_ERCLMASK;
    817 		log(LOG_ERR, "%s: %s error, retry %d, SR2/3 %x/%x, code %d",
    818 			device_xname(sc->sc_dev), i == SR4_DEVICE ? "device" :
    819 			(i == SR4_PROTOCOL ? "protocol" :
    820 			(i == SR4_SELFTEST ? "selftest" : "unknown")),
    821 			sc->sc_stat4 & SR4_RETRYMASK, sc->sc_stat2,
    822 			sc->sc_stat3, sc->sc_stat5);
    823 
    824 		if ((bp->b_flags & B_CMD) && bp->b_cmd == MTRESET)
    825 			callout_stop(&sc->sc_intr_ch);
    826 		if (sc->sc_stat3 & SR3_POWERUP)
    827 			sc->sc_flags &= MTF_OPEN | MTF_EXISTS;
    828 		goto error;
    829 	}
    830 	/*
    831 	 * Report and clear any soft errors.
    832 	 */
    833 	if (sc->sc_stat1 & SR1_SOFTERR) {
    834 		log(LOG_WARNING, "%s: soft error, retry %d\n",
    835 		    device_xname(sc->sc_dev), sc->sc_stat4 & SR4_RETRYMASK);
    836 		sc->sc_stat1 &= ~SR1_SOFTERR;
    837 	}
    838 	/*
    839 	 * We've initiated a read or write, but haven't actually started to
    840 	 * DMA the data yet.  At this point, the drive's ready.
    841 	 */
    842 	if (sc->sc_flags & MTF_IO) {
    843 		sc->sc_flags &= ~MTF_IO;
    844 		if (hpibustart(sc->sc_hpibno))
    845 			mtgo(sc);
    846 		return;
    847 	}
    848 	/*
    849 	 * Check for End Of Tape - we're allowed to hit EOT and then write (or
    850 	 * read) one more record.  If we get here and have not already hit EOT,
    851 	 * return ENOSPC to inform the process that it's hit it.  If we get
    852 	 * here and HAVE already hit EOT, don't allow any more operations that
    853 	 * move the tape forward.
    854 	 */
    855 	if (sc->sc_stat1 & SR1_EOT) {
    856 		if (sc->sc_flags & MTF_ATEOT)
    857 			sc->sc_flags |= MTF_PASTEOT;
    858 		else {
    859 			bp->b_error = ENOSPC;
    860 			sc->sc_flags |= MTF_ATEOT;
    861 		}
    862 	}
    863 	/*
    864 	 * If a motion command was being executed, check for Tape Marks.
    865 	 * If we were doing data, make sure we got the right amount, and
    866 	 * check for hitting tape marks on reads.
    867 	 */
    868 	if (bp->b_flags & B_CMD) {
    869 		if (sc->sc_stat1 & SR1_EOF) {
    870 			if (bp->b_cmd == MTFSR)
    871 				sc->sc_flags |= MTF_HITEOF;
    872 			if (bp->b_cmd == MTBSR)
    873 				sc->sc_flags |= MTF_HITBOF;
    874 		}
    875 		if (bp->b_cmd == MTRESET) {
    876 			callout_stop(&sc->sc_intr_ch);
    877 			sc->sc_flags |= MTF_ALIVE;
    878 		}
    879 	} else {
    880 		i = hpibrecv(sc->sc_hpibno, sc->sc_slave, MTT_BCNT, cmdbuf, 2);
    881 		if (i != 2) {
    882 			log(LOG_ERR, "%s intr: can't get xfer length\n",
    883 			    device_xname(sc->sc_dev));
    884 			goto error;
    885 		}
    886 		i = (int) *((u_short *) cmdbuf);
    887 		if (i <= bp->b_bcount) {
    888 			if (i == 0)
    889 				sc->sc_flags |= MTF_HITEOF;
    890 			bp->b_resid = bp->b_bcount - i;
    891 			dlog(LOG_DEBUG, "%s intr: bcount %d, resid %d",
    892 			    device_xname(sc->sc_dev), bp->b_bcount,
    893 			    bp->b_resid);
    894 		} else {
    895 			tprintf(sc->sc_ttyp,
    896 				"%s: record (%d) larger than wanted (%d)\n",
    897 				device_xname(sc->sc_dev), i, bp->b_bcount);
    898  error:
    899 			sc->sc_flags &= ~MTF_IO;
    900 			bp->b_error = EIO;
    901 		}
    902 	}
    903 	/*
    904 	 * The operation is completely done.
    905 	 * Let the drive know with an END command.
    906 	 */
    907 	cmdbuf[0] = MTE_COMPLETE | MTE_IDLE;
    908 	(void) hpibsend(sc->sc_hpibno, sc->sc_slave, MTL_ECMD, cmdbuf, 1);
    909 	bp->b_flags &= ~B_CMD;
    910 	(void)bufq_get(sc->sc_tab);
    911 	biodone(bp);
    912 	hpibfree(device_parent(sc->sc_dev), &sc->sc_hq);
    913 	if (bufq_peek(sc->sc_tab) == NULL)
    914 		sc->sc_active = 0;
    915 	else
    916 		mtustart(sc);
    917 }
    918 
    919 static int
    920 mtread(dev_t dev, struct uio *uio, int flags)
    921 {
    922 	struct mt_softc *sc = device_lookup_private(&mt_cd,UNIT(dev));
    923 
    924 	return physio(mtstrategy, NULL, dev, B_READ, minphys, uio);
    925 }
    926 
    927 static int
    928 mtwrite(dev_t dev, struct uio *uio, int flags)
    929 {
    930 	struct mt_softc *sc = device_lookup_private(&mt_cd,UNIT(dev));
    931 
    932 	return physio(mtstrategy, NULL, dev, B_WRITE, minphys, uio);
    933 }
    934 
    935 static int
    936 mtioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    937 {
    938 	struct mtop *op;
    939 	int cnt;
    940 
    941 	switch (cmd) {
    942 	    case MTIOCTOP:
    943 		op = (struct mtop *)data;
    944 		switch(op->mt_op) {
    945 		    case MTWEOF:
    946 		    case MTFSF:
    947 		    case MTBSR:
    948 		    case MTBSF:
    949 		    case MTFSR:
    950 			cnt = op->mt_count;
    951 			break;
    952 
    953 		    case MTOFFL:
    954 		    case MTREW:
    955 		    case MTNOP:
    956 			cnt = 0;
    957 			break;
    958 
    959 		    default:
    960 			return EINVAL;
    961 		}
    962 		return mtcommand(dev, op->mt_op, cnt);
    963 
    964 	    case MTIOCGET:
    965 		break;
    966 
    967 	    default:
    968 		return EINVAL;
    969 	}
    970 	return 0;
    971 }
    972