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