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