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