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