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