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