xref: /netbsd/sys/dev/gpib/mt.c (revision 53802318)
1*53802318Sriastradh /*	$NetBSD: mt.c,v 1.34 2019/12/01 16:22:10 riastradh Exp $ */
20f968376Sgmcgarry 
30f968376Sgmcgarry /*-
40f968376Sgmcgarry  * Copyright (c) 1996-2003 The NetBSD Foundation, Inc.
50f968376Sgmcgarry  * All rights reserved.
60f968376Sgmcgarry  *
70f968376Sgmcgarry  * This code is derived from software contributed to The NetBSD Foundation
80f968376Sgmcgarry  * by Jason R. Thorpe.
90f968376Sgmcgarry  *
100f968376Sgmcgarry  * Redistribution and use in source and binary forms, with or without
110f968376Sgmcgarry  * modification, are permitted provided that the following conditions
120f968376Sgmcgarry  * are met:
130f968376Sgmcgarry  * 1. Redistributions of source code must retain the above copyright
140f968376Sgmcgarry  *    notice, this list of conditions and the following disclaimer.
150f968376Sgmcgarry  * 2. Redistributions in binary form must reproduce the above copyright
160f968376Sgmcgarry  *    notice, this list of conditions and the following disclaimer in the
170f968376Sgmcgarry  *    documentation and/or other materials provided with the distribution.
180f968376Sgmcgarry  *
190f968376Sgmcgarry  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
200f968376Sgmcgarry  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
210f968376Sgmcgarry  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
220f968376Sgmcgarry  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
230f968376Sgmcgarry  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
240f968376Sgmcgarry  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
250f968376Sgmcgarry  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
260f968376Sgmcgarry  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
270f968376Sgmcgarry  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
280f968376Sgmcgarry  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
290f968376Sgmcgarry  * POSSIBILITY OF SUCH DAMAGE.
300f968376Sgmcgarry  */
310f968376Sgmcgarry 
320f968376Sgmcgarry /*
338ac3875aSrmind  * Copyright (c) 1988 University of Utah.
340f968376Sgmcgarry  * Copyright (c) 1982, 1990, 1993
350f968376Sgmcgarry  *	The Regents of the University of California.  All rights reserved.
360f968376Sgmcgarry  *
370f968376Sgmcgarry  * This code is derived from software contributed to Berkeley by
380f968376Sgmcgarry  * the Systems Programming Group of the University of Utah Computer
390f968376Sgmcgarry  * Science Department.
400f968376Sgmcgarry  *
410f968376Sgmcgarry  * Redistribution and use in source and binary forms, with or without
420f968376Sgmcgarry  * modification, are permitted provided that the following conditions
430f968376Sgmcgarry  * are met:
440f968376Sgmcgarry  * 1. Redistributions of source code must retain the above copyright
450f968376Sgmcgarry  *    notice, this list of conditions and the following disclaimer.
460f968376Sgmcgarry  * 2. Redistributions in binary form must reproduce the above copyright
470f968376Sgmcgarry  *    notice, this list of conditions and the following disclaimer in the
480f968376Sgmcgarry  *    documentation and/or other materials provided with the distribution.
49aad01611Sagc  * 3. Neither the name of the University nor the names of its contributors
50aad01611Sagc  *    may be used to endorse or promote products derived from this software
51aad01611Sagc  *    without specific prior written permission.
52aad01611Sagc  *
53aad01611Sagc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54aad01611Sagc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55aad01611Sagc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56aad01611Sagc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57aad01611Sagc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58aad01611Sagc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59aad01611Sagc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60aad01611Sagc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61aad01611Sagc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62aad01611Sagc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63aad01611Sagc  * SUCH DAMAGE.
64aad01611Sagc  *
65aad01611Sagc  * from: Utah $Hdr: rd.c 1.44 92/12/26$
66aad01611Sagc  *
67aad01611Sagc  *	@(#)rd.c	8.2 (Berkeley) 5/19/94
68aad01611Sagc  */
69aad01611Sagc 
70aad01611Sagc /*
710f968376Sgmcgarry  * Magnetic tape driver (HP7974a, HP7978a/b, HP7979a, HP7980a, HP7980xc)
720f968376Sgmcgarry  * Original version contributed by Mt. Xinu.
730f968376Sgmcgarry  * Modified for 4.4BSD by Mark Davies and Andrew Vignaux, Department of
740f968376Sgmcgarry  * Computer Science, Victoria University of Wellington
750f968376Sgmcgarry  */
760f968376Sgmcgarry 
770f968376Sgmcgarry #include <sys/cdefs.h>
78*53802318Sriastradh __KERNEL_RCSID(0, "$NetBSD: mt.c,v 1.34 2019/12/01 16:22:10 riastradh Exp $");
790f968376Sgmcgarry 
800f968376Sgmcgarry #include <sys/param.h>
810f968376Sgmcgarry #include <sys/systm.h>
820f968376Sgmcgarry #include <sys/callout.h>
830f968376Sgmcgarry #include <sys/buf.h>
8405f25dccSyamt #include <sys/bufq.h>
850f968376Sgmcgarry #include <sys/ioctl.h>
860f968376Sgmcgarry #include <sys/mtio.h>
870f968376Sgmcgarry #include <sys/file.h>
880f968376Sgmcgarry #include <sys/proc.h>
890f968376Sgmcgarry #include <sys/tty.h>
900f968376Sgmcgarry #include <sys/kernel.h>
910f968376Sgmcgarry #include <sys/tprintf.h>
920f968376Sgmcgarry #include <sys/device.h>
930f968376Sgmcgarry #include <sys/conf.h>
940f968376Sgmcgarry 
950f968376Sgmcgarry #include <dev/gpib/gpibvar.h>
960f968376Sgmcgarry #include <dev/gpib/cs80busvar.h>
970f968376Sgmcgarry 
980f968376Sgmcgarry #include <dev/gpib/mtreg.h>
990f968376Sgmcgarry 
1001d42e734Sriastradh #include "ioconf.h"
1011d42e734Sriastradh 
1020f968376Sgmcgarry #ifdef DEBUG
1030f968376Sgmcgarry int	mtdebug = 0;
1040f968376Sgmcgarry #define MDB_ANY		0xff
1050f968376Sgmcgarry #define MDB_FOLLOW	0x01
1060f968376Sgmcgarry #define	DPRINTF(mask, str)	if (mtdebug & (mask)) printf str
1070f968376Sgmcgarry #else
1080f968376Sgmcgarry #define	DPRINTF(mask, str)	/* nothing */
1090f968376Sgmcgarry #endif
1100f968376Sgmcgarry 
1110f968376Sgmcgarry struct	mt_softc {
1125f819ca3Schs 	device_t sc_dev;
1130f968376Sgmcgarry 
1140f968376Sgmcgarry 	gpib_chipset_tag_t sc_ic;
1150f968376Sgmcgarry 	gpib_handle_t sc_hdl;
1160f968376Sgmcgarry 
1170f968376Sgmcgarry 	int	sc_slave;	/* GPIB slave address (0-6) */
1180f968376Sgmcgarry 	short	sc_flags;	/* see below */
1190f968376Sgmcgarry 	u_char	sc_lastdsj;	/* place for DSJ in mtreaddsj() */
1200f968376Sgmcgarry 	u_char	sc_lastecmd;	/* place for End Command in mtreaddsj() */
1210f968376Sgmcgarry 	short	sc_recvtimeo;	/* count of gpibsend timeouts to prevent hang */
1220f968376Sgmcgarry 	short	sc_statindex;	/* index for next sc_stat when MTF_STATTIMEO */
1230f968376Sgmcgarry 	struct	mt_stat sc_stat;/* status bytes last read from device */
1240f968376Sgmcgarry 	short	sc_density;	/* current density of tape (mtio.h format) */
1250f968376Sgmcgarry 	short	sc_type;	/* tape drive model (hardware IDs) */
1260f968376Sgmcgarry 	tpr_t	sc_ttyp;
127aec75b1cSyamt 	struct bufq_state *sc_tab;/* buf queue */
1280f968376Sgmcgarry 	int	sc_active;
1290f968376Sgmcgarry 	struct buf sc_bufstore;	/* XXX buffer storage */
1300f968376Sgmcgarry 
1310f968376Sgmcgarry 	struct	callout sc_start_ch;
1320f968376Sgmcgarry 	struct	callout sc_intr_ch;
1330f968376Sgmcgarry };
1340f968376Sgmcgarry 
1350f968376Sgmcgarry #define	MTUNIT(x)	(minor(x) & 0x03)
1360f968376Sgmcgarry 
13743061c75Sreinoud #define B_CMD		B_DEVPRIVATE	/* command buf instead of data */
1380f968376Sgmcgarry #define	b_cmd		b_blkno		/* blkno holds cmd when B_CMD */
1390f968376Sgmcgarry 
140529e91fcScegger int	mtmatch(device_t, cfdata_t, void *);
141529e91fcScegger void	mtattach(device_t, device_t, void *);
1420f968376Sgmcgarry 
1435f819ca3Schs CFATTACH_DECL_NEW(mt, sizeof(struct mt_softc),
1440f968376Sgmcgarry 	mtmatch, mtattach, NULL, NULL);
1450f968376Sgmcgarry 
1460f968376Sgmcgarry int	mtlookup(int, int, int);
1470f968376Sgmcgarry void	mtustart(struct mt_softc *);
1480f968376Sgmcgarry int	mtreaddsj(struct mt_softc *, int);
1490f968376Sgmcgarry int	mtcommand(dev_t, int, int);
1500f968376Sgmcgarry 
1510f968376Sgmcgarry void	mtintr_callout(void *);
1520f968376Sgmcgarry void	mtstart_callout(void *);
1530f968376Sgmcgarry 
1540f968376Sgmcgarry void	mtcallback(void *, int);
1550f968376Sgmcgarry void	mtstart(struct mt_softc *);
1560f968376Sgmcgarry void	mtintr(struct mt_softc  *);
1570f968376Sgmcgarry 
1580f968376Sgmcgarry dev_type_open(mtopen);
1590f968376Sgmcgarry dev_type_close(mtclose);
1600f968376Sgmcgarry dev_type_read(mtread);
1610f968376Sgmcgarry dev_type_write(mtwrite);
1620f968376Sgmcgarry dev_type_ioctl(mtioctl);
1630f968376Sgmcgarry dev_type_strategy(mtstrategy);
1640f968376Sgmcgarry 
1650f968376Sgmcgarry const struct bdevsw mt_bdevsw = {
16676258fa0Sdholland 	.d_open = mtopen,
16776258fa0Sdholland 	.d_close = mtclose,
16876258fa0Sdholland 	.d_strategy = mtstrategy,
16976258fa0Sdholland 	.d_ioctl = mtioctl,
17076258fa0Sdholland 	.d_dump = nodump,
17176258fa0Sdholland 	.d_psize = nosize,
172da8feef9Sdholland 	.d_discard = nodiscard,
17376258fa0Sdholland 	.d_flag = D_TAPE
1740f968376Sgmcgarry };
1750f968376Sgmcgarry 
1760f968376Sgmcgarry const struct cdevsw mt_cdevsw = {
17776258fa0Sdholland 	.d_open = mtopen,
17876258fa0Sdholland 	.d_close = mtclose,
17976258fa0Sdholland 	.d_read = mtread,
18076258fa0Sdholland 	.d_write = mtwrite,
18176258fa0Sdholland 	.d_ioctl = mtioctl,
18276258fa0Sdholland 	.d_stop = nostop,
18376258fa0Sdholland 	.d_tty = notty,
18476258fa0Sdholland 	.d_poll = nopoll,
18576258fa0Sdholland 	.d_mmap = nommap,
18676258fa0Sdholland 	.d_kqfilter = nokqfilter,
1871a918832Sdholland 	.d_discard = nodiscard,
18876258fa0Sdholland 	.d_flag = D_TAPE
1890f968376Sgmcgarry };
1900f968376Sgmcgarry 
1910f968376Sgmcgarry 
1920f968376Sgmcgarry struct	mtinfo {
1930f968376Sgmcgarry 	u_short	hwid;
19422974bb6Scube 	const char	*desc;
1950f968376Sgmcgarry } mtinfo[] = {
1960f968376Sgmcgarry 	{ MT7978ID,	"7978"	},
1970f968376Sgmcgarry 	{ MT7979AID,	"7979A"	},
1980f968376Sgmcgarry 	{ MT7980ID,	"7980"	},
1990f968376Sgmcgarry 	{ MT7974AID,	"7974A"	},
2000f968376Sgmcgarry };
2010f968376Sgmcgarry int	nmtinfo = sizeof(mtinfo) / sizeof(mtinfo[0]);
2020f968376Sgmcgarry 
2030f968376Sgmcgarry 
2040f968376Sgmcgarry int
mtlookup(int id,int slave,int punit)205454af1c0Sdsl mtlookup(int id, int slave, int punit)
2060f968376Sgmcgarry {
2070f968376Sgmcgarry 	int i;
2080f968376Sgmcgarry 
2090f968376Sgmcgarry 	for (i = 0; i < nmtinfo; i++)
2100f968376Sgmcgarry 		if (mtinfo[i].hwid == id)
2110f968376Sgmcgarry 			break;
2120f968376Sgmcgarry 	if (i == nmtinfo)
2130f968376Sgmcgarry 		return (-1);
2140f968376Sgmcgarry 	return (0);
2150f968376Sgmcgarry }
2160f968376Sgmcgarry 
2170f968376Sgmcgarry int
mtmatch(device_t parent,cfdata_t match,void * aux)218529e91fcScegger mtmatch(device_t parent, cfdata_t match, void *aux)
2190f968376Sgmcgarry {
2200f968376Sgmcgarry 	struct cs80bus_attach_args *ca = aux;
2210f968376Sgmcgarry 
2220f968376Sgmcgarry 	ca->ca_punit = 0;
2230f968376Sgmcgarry 	return (mtlookup(ca->ca_id, ca->ca_slave, ca->ca_punit) == 0);
2240f968376Sgmcgarry }
2250f968376Sgmcgarry 
2260f968376Sgmcgarry void
mtattach(device_t parent,device_t self,void * aux)227529e91fcScegger mtattach(device_t parent, device_t self, void *aux)
2280f968376Sgmcgarry {
22992c7bba3Sthorpej 	struct mt_softc *sc = device_private(self);
2300f968376Sgmcgarry 	struct cs80bus_attach_args *ca = aux;
2310f968376Sgmcgarry 	int type;
2320f968376Sgmcgarry 
2332e354748Smsaitoh 	sc->sc_dev = self;
2340f968376Sgmcgarry 	sc->sc_ic = ca->ca_ic;
2350f968376Sgmcgarry 	sc->sc_slave = ca->ca_slave;
2360f968376Sgmcgarry 
2370f968376Sgmcgarry 	if ((type = mtlookup(ca->ca_id, ca->ca_slave, ca->ca_punit)) < 0)
2380f968376Sgmcgarry 		return;
2390f968376Sgmcgarry 
240fea98925Smsaitoh 	aprint_naive("\n");
241fea98925Smsaitoh 	aprint_normal(": %s tape\n", mtinfo[type].desc);
2420f968376Sgmcgarry 
2430f968376Sgmcgarry 	sc->sc_type = type;
2440f968376Sgmcgarry 	sc->sc_flags = MTF_EXISTS;
2450f968376Sgmcgarry 
246aec75b1cSyamt 	bufq_alloc(&sc->sc_tab, "fcfs", 0);
24788ab7da9Sad 	callout_init(&sc->sc_start_ch, 0);
24888ab7da9Sad 	callout_init(&sc->sc_intr_ch, 0);
2490f968376Sgmcgarry 
2500f968376Sgmcgarry 	if (gpibregister(sc->sc_ic, sc->sc_slave, mtcallback, sc,
2510f968376Sgmcgarry 	    &sc->sc_hdl)) {
2525f819ca3Schs 		aprint_error_dev(sc->sc_dev, "can't register callback\n");
2530f968376Sgmcgarry 		return;
2540f968376Sgmcgarry 	}
2550f968376Sgmcgarry }
2560f968376Sgmcgarry 
2570f968376Sgmcgarry /*
2580f968376Sgmcgarry  * Perform a read of "Device Status Jump" register and update the
2590f968376Sgmcgarry  * status if necessary.  If status is read, the given "ecmd" is also
2600f968376Sgmcgarry  * performed, unless "ecmd" is zero.  Returns DSJ value, -1 on failure
2610f968376Sgmcgarry  * and -2 on "temporary" failure.
2620f968376Sgmcgarry  */
2630f968376Sgmcgarry int
mtreaddsj(struct mt_softc * sc,int ecmd)264454af1c0Sdsl mtreaddsj(struct mt_softc *sc, int ecmd)
2650f968376Sgmcgarry {
2660f968376Sgmcgarry 	int retval;
2670f968376Sgmcgarry 
2680f968376Sgmcgarry 	if (sc->sc_flags & MTF_STATTIMEO)
2690f968376Sgmcgarry 		goto getstats;
2700f968376Sgmcgarry 	retval = gpibrecv(sc->sc_ic,
2710f968376Sgmcgarry 	    (sc->sc_flags & MTF_DSJTIMEO) ? -1 : sc->sc_slave,
2720f968376Sgmcgarry 	    MTT_DSJ, &(sc->sc_lastdsj), 1);
2730f968376Sgmcgarry 	sc->sc_flags &= ~MTF_DSJTIMEO;
2740f968376Sgmcgarry 	if (retval != 1) {
2750f968376Sgmcgarry 		DPRINTF(MDB_ANY, ("%s can't gpibrecv DSJ",
2765f819ca3Schs 		    device_xname(sc->sc_dev)));
2770f968376Sgmcgarry 		if (sc->sc_recvtimeo == 0)
2780f968376Sgmcgarry 			sc->sc_recvtimeo = hz;
2790f968376Sgmcgarry 		if (--sc->sc_recvtimeo == 0)
2800f968376Sgmcgarry 			return (-1);
2810f968376Sgmcgarry 		if (retval == 0)
2820f968376Sgmcgarry 			sc->sc_flags |= MTF_DSJTIMEO;
2830f968376Sgmcgarry 		return (-2);
2840f968376Sgmcgarry 	}
2850f968376Sgmcgarry 	sc->sc_recvtimeo = 0;
2860f968376Sgmcgarry 	sc->sc_statindex = 0;
2875f819ca3Schs 	DPRINTF(MDB_ANY, ("%s readdsj: 0x%x", device_xname(sc->sc_dev),
2880f968376Sgmcgarry 	    sc->sc_lastdsj));
2890f968376Sgmcgarry 	sc->sc_lastecmd = ecmd;
2900f968376Sgmcgarry 	switch (sc->sc_lastdsj) {
2910f968376Sgmcgarry 	    case 0:
2920f968376Sgmcgarry 		if (ecmd & MTE_DSJ_FORCE)
2930f968376Sgmcgarry 			break;
2940f968376Sgmcgarry 		return (0);
2950f968376Sgmcgarry 
2960f968376Sgmcgarry 	    case 2:
2970f968376Sgmcgarry 		sc->sc_lastecmd = MTE_COMPLETE;
2980f968376Sgmcgarry 	    case 1:
2990f968376Sgmcgarry 		break;
3000f968376Sgmcgarry 
3010f968376Sgmcgarry 	    default:
3025f819ca3Schs 		printf("%s readdsj: DSJ 0x%x\n", device_xname(sc->sc_dev),
3030f968376Sgmcgarry 		    sc->sc_lastdsj);
3040f968376Sgmcgarry 		return (-1);
3050f968376Sgmcgarry 	}
3060f968376Sgmcgarry 
3070f968376Sgmcgarry getstats:
3080f968376Sgmcgarry 	retval = gpibrecv(sc->sc_ic,
3090f968376Sgmcgarry 	    (sc->sc_flags & MTF_STATCONT) ? -1 : sc->sc_slave, MTT_STAT,
3100f968376Sgmcgarry 	     ((char *)&(sc->sc_stat)) + sc->sc_statindex,
3110f968376Sgmcgarry 	    sizeof(sc->sc_stat) - sc->sc_statindex);
3120f968376Sgmcgarry 	sc->sc_flags &= ~(MTF_STATTIMEO | MTF_STATCONT);
3130f968376Sgmcgarry 	if (retval != sizeof(sc->sc_stat) - sc->sc_statindex) {
3140f968376Sgmcgarry 		if (sc->sc_recvtimeo == 0)
3150f968376Sgmcgarry 			sc->sc_recvtimeo = hz;
3160f968376Sgmcgarry 		if (--sc->sc_recvtimeo != 0) {
3170f968376Sgmcgarry 			if (retval >= 0) {
3180f968376Sgmcgarry 				sc->sc_statindex += retval;
3190f968376Sgmcgarry 				sc->sc_flags |= MTF_STATCONT;
3200f968376Sgmcgarry 			}
3210f968376Sgmcgarry 			sc->sc_flags |= MTF_STATTIMEO;
3220f968376Sgmcgarry 			return (-2);
3230f968376Sgmcgarry 		}
324fea98925Smsaitoh 		printf("%s readdsj: can't read status",
325fea98925Smsaitoh 		    device_xname(sc->sc_dev));
3260f968376Sgmcgarry 		return (-1);
3270f968376Sgmcgarry 	}
3280f968376Sgmcgarry 	sc->sc_recvtimeo = 0;
3290f968376Sgmcgarry 	sc->sc_statindex = 0;
3300f968376Sgmcgarry 	DPRINTF(MDB_ANY, ("%s readdsj: status is %x %x %x %x %x %x",
3315f819ca3Schs 	    device_xname(sc->sc_dev),
3320f968376Sgmcgarry 	    sc->sc_stat1, sc->sc_stat2, sc->sc_stat3,
3330f968376Sgmcgarry 	    sc->sc_stat4, sc->sc_stat5, sc->sc_stat6));
3340f968376Sgmcgarry 	if (sc->sc_lastecmd)
3350f968376Sgmcgarry 		(void) gpibsend(sc->sc_ic, sc->sc_slave,
3360f968376Sgmcgarry 		    MTL_ECMD, &(sc->sc_lastecmd), 1);
3370f968376Sgmcgarry 	return ((int) sc->sc_lastdsj);
3380f968376Sgmcgarry }
3390f968376Sgmcgarry 
3400f968376Sgmcgarry int
mtopen(dev_t dev,int flag,int mode,struct lwp * l)3413e484e61Scegger mtopen(dev_t dev, int flag, int mode, struct lwp *l)
3420f968376Sgmcgarry {
3430f968376Sgmcgarry 	struct mt_softc *sc;
3440f968376Sgmcgarry 	int req_den;
3450f968376Sgmcgarry 	int error;
3460f968376Sgmcgarry 
3473e484e61Scegger 	sc = device_lookup_private(&mt_cd, MTUNIT(dev));
3480f968376Sgmcgarry 	if (sc == NULL || (sc->sc_flags & MTF_EXISTS) == 0)
3490f968376Sgmcgarry 		return (ENXIO);
3500f968376Sgmcgarry 
3510f968376Sgmcgarry 	if (sc->sc_flags & MTF_OPEN)
3520f968376Sgmcgarry 		return (EBUSY);
3530f968376Sgmcgarry 
3545f819ca3Schs 	DPRINTF(MDB_ANY, ("%s open: flags 0x%x", device_xname(sc->sc_dev),
3550f968376Sgmcgarry 	    sc->sc_flags));
3560f968376Sgmcgarry 
3570f968376Sgmcgarry 	sc->sc_flags |= MTF_OPEN;
35822974bb6Scube 	sc->sc_ttyp = tprintf_open(l->l_proc);
3590f968376Sgmcgarry 	if ((sc->sc_flags & MTF_ALIVE) == 0) {
3600f968376Sgmcgarry 		error = mtcommand(dev, MTRESET, 0);
3610f968376Sgmcgarry 		if (error != 0 || (sc->sc_flags & MTF_ALIVE) == 0)
3620f968376Sgmcgarry 			goto errout;
3630f968376Sgmcgarry 		if ((sc->sc_stat1 & (SR1_BOT | SR1_ONLINE)) == SR1_ONLINE)
3640f968376Sgmcgarry 			(void) mtcommand(dev, MTREW, 0);
3650f968376Sgmcgarry 	}
3660f968376Sgmcgarry 	for (;;) {
3670f968376Sgmcgarry 		if ((error = mtcommand(dev, MTNOP, 0)) != 0)
3680f968376Sgmcgarry 			goto errout;
3690f968376Sgmcgarry 		if (!(sc->sc_flags & MTF_REW))
3700f968376Sgmcgarry 			break;
371faa8e1b3Spooka 		error = kpause("mt", true, hz, NULL);
372faa8e1b3Spooka 		if (error != 0 && error != EWOULDBLOCK) {
3730f968376Sgmcgarry 			error = EINTR;
3740f968376Sgmcgarry 			goto errout;
3750f968376Sgmcgarry 		}
3760f968376Sgmcgarry 	}
3770f968376Sgmcgarry 	if ((flag & FWRITE) && (sc->sc_stat1 & SR1_RO)) {
3780f968376Sgmcgarry 		error = EROFS;
3790f968376Sgmcgarry 		goto errout;
3800f968376Sgmcgarry 	}
3810f968376Sgmcgarry 	if (!(sc->sc_stat1 & SR1_ONLINE)) {
3825f819ca3Schs 		uprintf("%s: not online\n", device_xname(sc->sc_dev));
3830f968376Sgmcgarry 		error = EIO;
3840f968376Sgmcgarry 		goto errout;
3850f968376Sgmcgarry 	}
3860f968376Sgmcgarry 	/*
3870f968376Sgmcgarry 	 * Select density:
3880f968376Sgmcgarry 	 *  - find out what density the drive is set to
3890f968376Sgmcgarry 	 *	(i.e. the density of the current tape)
3900f968376Sgmcgarry 	 *  - if we are going to write
3910f968376Sgmcgarry 	 *    - if we're not at the beginning of the tape
3920f968376Sgmcgarry 	 *      - complain if we want to change densities
3930f968376Sgmcgarry 	 *    - otherwise, select the mtcommand to set the density
3940f968376Sgmcgarry 	 *
3950f968376Sgmcgarry 	 * If the drive doesn't support it then don't change the recorded
3960f968376Sgmcgarry 	 * density.
3970f968376Sgmcgarry 	 *
3980f968376Sgmcgarry 	 * The original MOREbsd code had these additional conditions
3990f968376Sgmcgarry 	 * for the mid-tape change
4000f968376Sgmcgarry 	 *
4010f968376Sgmcgarry 	 *	req_den != T_BADBPI &&
4020f968376Sgmcgarry 	 *	sc->sc_density != T_6250BPI
4030f968376Sgmcgarry 	 *
4040f968376Sgmcgarry 	 * which suggests that it would be possible to write multiple
4050f968376Sgmcgarry 	 * densities if req_den == T_BAD_BPI or the current tape
4060f968376Sgmcgarry 	 * density was 6250.  Testing of our 7980 suggests that the
4070f968376Sgmcgarry 	 * device cannot change densities mid-tape.
4080f968376Sgmcgarry 	 *
4090f968376Sgmcgarry 	 * ajv@comp.vuw.ac.nz
4100f968376Sgmcgarry 	 */
4110f968376Sgmcgarry 	sc->sc_density = (sc->sc_stat2 & SR2_6250) ? T_6250BPI : (
4120f968376Sgmcgarry 			 (sc->sc_stat3 & SR3_1600) ? T_1600BPI : (
4130f968376Sgmcgarry 			 (sc->sc_stat3 & SR3_800) ? T_800BPI : -1));
4140f968376Sgmcgarry 	req_den = (dev & T_DENSEL);
4150f968376Sgmcgarry 
4160f968376Sgmcgarry 	if (flag & FWRITE) {
4170f968376Sgmcgarry 		if (!(sc->sc_stat1 & SR1_BOT)) {
4180f968376Sgmcgarry 			if (sc->sc_density != req_den) {
4190f968376Sgmcgarry 				uprintf("%s: can't change density mid-tape\n",
4205f819ca3Schs 				    device_xname(sc->sc_dev));
4210f968376Sgmcgarry 				error = EIO;
4220f968376Sgmcgarry 				goto errout;
4230f968376Sgmcgarry 			}
4240f968376Sgmcgarry 		}
4250f968376Sgmcgarry 		else {
4260f968376Sgmcgarry 			int mtset_density =
4270f968376Sgmcgarry 			    (req_den == T_800BPI  ? MTSET800BPI : (
4280f968376Sgmcgarry 			     req_den == T_1600BPI ? MTSET1600BPI : (
4290f968376Sgmcgarry 			     req_den == T_6250BPI ? MTSET6250BPI : (
4300f968376Sgmcgarry 			     sc->sc_type == MT7980ID
4310f968376Sgmcgarry 						  ? MTSET6250DC
4320f968376Sgmcgarry 						  : MTSET6250BPI))));
4330f968376Sgmcgarry 			if (mtcommand(dev, mtset_density, 0) == 0)
4340f968376Sgmcgarry 				sc->sc_density = req_den;
4350f968376Sgmcgarry 		}
4360f968376Sgmcgarry 	}
4370f968376Sgmcgarry 	return (0);
4380f968376Sgmcgarry errout:
4390f968376Sgmcgarry 	sc->sc_flags &= ~MTF_OPEN;
4400f968376Sgmcgarry 	return (error);
4410f968376Sgmcgarry }
4420f968376Sgmcgarry 
4430f968376Sgmcgarry int
mtclose(dev_t dev,int flag,int fmt,struct lwp * l)4443e484e61Scegger mtclose(dev_t dev, int flag, int fmt, struct lwp *l)
4450f968376Sgmcgarry {
4460f968376Sgmcgarry 	struct mt_softc *sc;
4470f968376Sgmcgarry 
4483e484e61Scegger 	sc = device_lookup_private(&mt_cd, MTUNIT(dev));
4490f968376Sgmcgarry 	if (sc == NULL)
4500f968376Sgmcgarry 		return (ENXIO);
4510f968376Sgmcgarry 
4520f968376Sgmcgarry 	if (sc->sc_flags & MTF_WRT) {
4530f968376Sgmcgarry 		(void) mtcommand(dev, MTWEOF, 2);
4540f968376Sgmcgarry 		(void) mtcommand(dev, MTBSF, 0);
4550f968376Sgmcgarry 	}
4560f968376Sgmcgarry 	if ((minor(dev) & T_NOREWIND) == 0)
4570f968376Sgmcgarry 		(void) mtcommand(dev, MTREW, 0);
4580f968376Sgmcgarry 	sc->sc_flags &= ~MTF_OPEN;
4590f968376Sgmcgarry 	tprintf_close(sc->sc_ttyp);
4600f968376Sgmcgarry 	return (0);
4610f968376Sgmcgarry }
4620f968376Sgmcgarry 
4630f968376Sgmcgarry int
mtcommand(dev_t dev,int cmd,int cnt)4643e484e61Scegger mtcommand(dev_t dev, int cmd, int cnt)
4650f968376Sgmcgarry {
4660f968376Sgmcgarry 	struct mt_softc *sc;
4670f968376Sgmcgarry 	struct buf *bp;
4680f968376Sgmcgarry 	int error = 0;
4690f968376Sgmcgarry 
4703e484e61Scegger 	sc = device_lookup_private(&mt_cd, MTUNIT(dev));
4710f968376Sgmcgarry 	bp = &sc->sc_bufstore;
4720f968376Sgmcgarry 
4734a780c9aSad 	if (bp->b_cflags & BC_BUSY)
4740f968376Sgmcgarry 		return (EBUSY);
4750f968376Sgmcgarry 
4760f968376Sgmcgarry 	bp->b_cmd = cmd;
4770f968376Sgmcgarry 	bp->b_dev = dev;
4784a780c9aSad 	bp->b_objlock = &buffer_lock;
4790f968376Sgmcgarry 	do {
4804a780c9aSad 		bp->b_cflags = BC_BUSY;
4814a780c9aSad 		bp->b_flags = B_CMD;
4824a780c9aSad 		bp->b_oflags = 0;
4830f968376Sgmcgarry 		mtstrategy(bp);
4840f968376Sgmcgarry 		biowait(bp);
48566fefd11Sad 		if (bp->b_error != 0) {
4860f968376Sgmcgarry 			error = (int) (unsigned) bp->b_error;
4870f968376Sgmcgarry 			break;
4880f968376Sgmcgarry 		}
4890f968376Sgmcgarry 	} while (--cnt > 0);
4900f968376Sgmcgarry #if 0
4914a780c9aSad 	bp->b_cflags = 0 /*&= ~BC_BUSY*/;
4920f968376Sgmcgarry #else
4934a780c9aSad 	bp->b_cflags &= ~BC_BUSY;
4940f968376Sgmcgarry #endif
4950f968376Sgmcgarry 	return (error);
4960f968376Sgmcgarry }
4970f968376Sgmcgarry 
4980f968376Sgmcgarry /*
4990f968376Sgmcgarry  * Only thing to check here is for legal record lengths (writes only).
5000f968376Sgmcgarry  */
5010f968376Sgmcgarry void
mtstrategy(struct buf * bp)5023e484e61Scegger mtstrategy(struct buf *bp)
5030f968376Sgmcgarry {
5040f968376Sgmcgarry 	struct mt_softc *sc;
5050f968376Sgmcgarry 	int s;
5060f968376Sgmcgarry 
5073e484e61Scegger 	sc = device_lookup_private(&mt_cd, MTUNIT(bp->b_dev));
5080f968376Sgmcgarry 
5095f819ca3Schs 	DPRINTF(MDB_ANY, ("%s strategy", device_xname(sc->sc_dev)));
5100f968376Sgmcgarry 
5110f968376Sgmcgarry 	if ((bp->b_flags & (B_CMD | B_READ)) == 0) {
5120f968376Sgmcgarry #define WRITE_BITS_IGNORED	8
5130f968376Sgmcgarry #if 0
5140f968376Sgmcgarry 		if (bp->b_bcount & ((1 << WRITE_BITS_IGNORED) - 1)) {
5150f968376Sgmcgarry 			tprintf(sc->sc_ttyp,
5160f968376Sgmcgarry 			    "%s: write record must be multiple of %d\n",
5175f819ca3Schs 			    device_xname(sc->sc_dev), 1 << WRITE_BITS_IGNORED);
5180f968376Sgmcgarry 			goto error;
5190f968376Sgmcgarry 		}
5200f968376Sgmcgarry #endif
5210f968376Sgmcgarry 		s = 16 * 1024;
5220f968376Sgmcgarry 		if (sc->sc_stat2 & SR2_LONGREC) {
5230f968376Sgmcgarry 			switch (sc->sc_density) {
5240f968376Sgmcgarry 			    case T_1600BPI:
5250f968376Sgmcgarry 				s = 32 * 1024;
5260f968376Sgmcgarry 				break;
5270f968376Sgmcgarry 
5280f968376Sgmcgarry 			    case T_6250BPI:
5290f968376Sgmcgarry 			    case T_BADBPI:
5300f968376Sgmcgarry 				s = 60 * 1024;
5310f968376Sgmcgarry 				break;
5320f968376Sgmcgarry 			}
5330f968376Sgmcgarry 		}
5340f968376Sgmcgarry 		if (bp->b_bcount > s) {
5350f968376Sgmcgarry 			tprintf(sc->sc_ttyp,
53622974bb6Scube 				"%s: write record (%d) too big: limit (%d)\n",
5375f819ca3Schs 				device_xname(sc->sc_dev), bp->b_bcount, s);
5380f968376Sgmcgarry #if 0 /* XXX see above */
5390f968376Sgmcgarry 	    error:
5400f968376Sgmcgarry #endif
5410f968376Sgmcgarry 			bp->b_error = EIO;
5420f968376Sgmcgarry 			biodone(bp);
5430f968376Sgmcgarry 			return;
5440f968376Sgmcgarry 		}
5450f968376Sgmcgarry 	}
5460f968376Sgmcgarry 	s = splbio();
54770de9736Syamt 	bufq_put(sc->sc_tab, bp);
5480f968376Sgmcgarry 	if (sc->sc_active == 0) {
5490f968376Sgmcgarry 		sc->sc_active = 1;
5500f968376Sgmcgarry 		mtustart(sc);
5510f968376Sgmcgarry 	}
5520f968376Sgmcgarry 	splx(s);
5530f968376Sgmcgarry }
5540f968376Sgmcgarry 
5550f968376Sgmcgarry void
mtustart(struct mt_softc * sc)556454af1c0Sdsl mtustart(struct mt_softc *sc)
5570f968376Sgmcgarry {
5580f968376Sgmcgarry 
5595f819ca3Schs 	DPRINTF(MDB_ANY, ("%s ustart", device_xname(sc->sc_dev)));
5600f968376Sgmcgarry 	if (gpibrequest(sc->sc_ic, sc->sc_hdl))
5610f968376Sgmcgarry 		mtstart(sc);
5620f968376Sgmcgarry }
5630f968376Sgmcgarry 
5640f968376Sgmcgarry void
mtcallback(void * v,int action)565454af1c0Sdsl mtcallback(void *v, int action)
5660f968376Sgmcgarry {
5670f968376Sgmcgarry 	struct mt_softc *sc = v;
5680f968376Sgmcgarry 
5690f968376Sgmcgarry 	DPRINTF(MDB_FOLLOW, ("mtcallback: v=%p, action=%d\n", v, action));
5700f968376Sgmcgarry 
5710f968376Sgmcgarry 	switch (action) {
5720f968376Sgmcgarry 	case GPIBCBF_START:
5730f968376Sgmcgarry 		mtstart(sc);
5740f968376Sgmcgarry 		break;
5750f968376Sgmcgarry 	case GPIBCBF_INTR:
5760f968376Sgmcgarry 		mtintr(sc);
5770f968376Sgmcgarry 		break;
5780f968376Sgmcgarry #ifdef DEBUG
5790f968376Sgmcgarry 	default:
5800f968376Sgmcgarry 		printf("mtcallback: unknown action %d\n", action);
5810f968376Sgmcgarry 		break;
5820f968376Sgmcgarry #endif
5830f968376Sgmcgarry 	}
5840f968376Sgmcgarry }
5850f968376Sgmcgarry 
5860f968376Sgmcgarry void
mtintr_callout(void * arg)587454af1c0Sdsl mtintr_callout(void *arg)
5880f968376Sgmcgarry {
5890f968376Sgmcgarry 	struct mt_softc *sc = arg;
5900f968376Sgmcgarry 	int s = splbio();
5910f968376Sgmcgarry 
5920f968376Sgmcgarry 	gpibppclear(sc->sc_ic);
5930f968376Sgmcgarry 	mtintr(sc);
5940f968376Sgmcgarry 	splx(s);
5950f968376Sgmcgarry }
5960f968376Sgmcgarry 
5970f968376Sgmcgarry void
mtstart_callout(void * arg)598454af1c0Sdsl mtstart_callout(void *arg)
5990f968376Sgmcgarry {
6000f968376Sgmcgarry 	int s = splbio();
6010f968376Sgmcgarry 
6020f968376Sgmcgarry 	mtstart((struct mt_softc *)arg);
6030f968376Sgmcgarry 	splx(s);
6040f968376Sgmcgarry }
6050f968376Sgmcgarry 
6060f968376Sgmcgarry void
mtstart(struct mt_softc * sc)607454af1c0Sdsl mtstart(struct mt_softc *sc)
6080f968376Sgmcgarry {
6090f968376Sgmcgarry 	struct buf *bp;
6100f968376Sgmcgarry 	short	cmdcount = 1;
6110f968376Sgmcgarry 	u_char	cmdbuf[2];
6120f968376Sgmcgarry 
6135f819ca3Schs 	DPRINTF(MDB_ANY, ("%s start", device_xname(sc->sc_dev)));
6140f968376Sgmcgarry 	sc->sc_flags &= ~MTF_WRT;
61570de9736Syamt 	bp = bufq_peek(sc->sc_tab);
6160f968376Sgmcgarry 	if ((sc->sc_flags & MTF_ALIVE) == 0 &&
6170f968376Sgmcgarry 	    ((bp->b_flags & B_CMD) == 0 || bp->b_cmd != MTRESET))
6180f968376Sgmcgarry 		goto fatalerror;
6190f968376Sgmcgarry 
6200f968376Sgmcgarry 	if (sc->sc_flags & MTF_REW) {
6210f968376Sgmcgarry 		if (!gpibpptest(sc->sc_ic, sc->sc_slave))
6220f968376Sgmcgarry 			goto stillrew;
6230f968376Sgmcgarry 		switch (mtreaddsj(sc, MTE_DSJ_FORCE|MTE_COMPLETE|MTE_IDLE)) {
6240f968376Sgmcgarry 		    case 0:
6250f968376Sgmcgarry 		    case 1:
6260f968376Sgmcgarry 		stillrew:
6270f968376Sgmcgarry 			if ((sc->sc_stat1 & SR1_BOT) ||
6280f968376Sgmcgarry 			    !(sc->sc_stat1 & SR1_ONLINE)) {
6290f968376Sgmcgarry 				sc->sc_flags &= ~MTF_REW;
6300f968376Sgmcgarry 				break;
6310f968376Sgmcgarry 			}
6325a2e28a2Skamil 			/* FALLTHROUGH */
6330f968376Sgmcgarry 		    case -2:
6340f968376Sgmcgarry 			/*
6350f968376Sgmcgarry 			 * -2 means "timeout" reading DSJ, which is probably
6360f968376Sgmcgarry 			 * temporary.  This is considered OK when doing a NOP,
6370f968376Sgmcgarry 			 * but not otherwise.
6380f968376Sgmcgarry 			 */
6390f968376Sgmcgarry 			if (sc->sc_flags & (MTF_DSJTIMEO | MTF_STATTIMEO)) {
6400f968376Sgmcgarry 				callout_reset(&sc->sc_start_ch, hz >> 5,
6410f968376Sgmcgarry 				    mtstart_callout, sc);
6420f968376Sgmcgarry 				return;
6430f968376Sgmcgarry 			}
6445a2e28a2Skamil 			/* FALLTHROUGH */
6450f968376Sgmcgarry 		    case 2:
6460f968376Sgmcgarry 			if (bp->b_cmd != MTNOP || !(bp->b_flags & B_CMD)) {
6470f968376Sgmcgarry 				bp->b_error = EBUSY;
64866fefd11Sad 				goto done;
6490f968376Sgmcgarry 			}
6500f968376Sgmcgarry 			goto done;
6510f968376Sgmcgarry 
6520f968376Sgmcgarry 		    default:
6530f968376Sgmcgarry 			goto fatalerror;
6540f968376Sgmcgarry 		}
6550f968376Sgmcgarry 	}
6560f968376Sgmcgarry 	if (bp->b_flags & B_CMD) {
6570f968376Sgmcgarry 		if (sc->sc_flags & MTF_PASTEOT) {
6580f968376Sgmcgarry 			switch(bp->b_cmd) {
6590f968376Sgmcgarry 			    case MTFSF:
6600f968376Sgmcgarry 			    case MTWEOF:
6610f968376Sgmcgarry 			    case MTFSR:
6620f968376Sgmcgarry 				bp->b_error = ENOSPC;
66366fefd11Sad 				goto done;
6640f968376Sgmcgarry 
6650f968376Sgmcgarry 			    case MTBSF:
6660f968376Sgmcgarry 			    case MTOFFL:
6670f968376Sgmcgarry 			    case MTBSR:
6680f968376Sgmcgarry 			    case MTREW:
6690f968376Sgmcgarry 				sc->sc_flags &= ~(MTF_PASTEOT | MTF_ATEOT);
6700f968376Sgmcgarry 				break;
6710f968376Sgmcgarry 			}
6720f968376Sgmcgarry 		}
6730f968376Sgmcgarry 		switch(bp->b_cmd) {
6740f968376Sgmcgarry 		    case MTFSF:
6750f968376Sgmcgarry 			if (sc->sc_flags & MTF_HITEOF)
6760f968376Sgmcgarry 				goto done;
6770f968376Sgmcgarry 			cmdbuf[0] = MTTC_FSF;
6780f968376Sgmcgarry 			break;
6790f968376Sgmcgarry 
6800f968376Sgmcgarry 		    case MTBSF:
6810f968376Sgmcgarry 			if (sc->sc_flags & MTF_HITBOF)
6820f968376Sgmcgarry 				goto done;
6830f968376Sgmcgarry 			cmdbuf[0] = MTTC_BSF;
6840f968376Sgmcgarry 			break;
6850f968376Sgmcgarry 
6860f968376Sgmcgarry 		    case MTOFFL:
6870f968376Sgmcgarry 			sc->sc_flags |= MTF_REW;
6880f968376Sgmcgarry 			cmdbuf[0] = MTTC_REWOFF;
6890f968376Sgmcgarry 			break;
6900f968376Sgmcgarry 
6910f968376Sgmcgarry 		    case MTWEOF:
6920f968376Sgmcgarry 			cmdbuf[0] = MTTC_WFM;
6930f968376Sgmcgarry 			break;
6940f968376Sgmcgarry 
6950f968376Sgmcgarry 		    case MTBSR:
6960f968376Sgmcgarry 			cmdbuf[0] = MTTC_BSR;
6970f968376Sgmcgarry 			break;
6980f968376Sgmcgarry 
6990f968376Sgmcgarry 		    case MTFSR:
7000f968376Sgmcgarry 			cmdbuf[0] = MTTC_FSR;
7010f968376Sgmcgarry 			break;
7020f968376Sgmcgarry 
7030f968376Sgmcgarry 		    case MTREW:
7040f968376Sgmcgarry 			sc->sc_flags |= MTF_REW;
7050f968376Sgmcgarry 			cmdbuf[0] = MTTC_REW;
7060f968376Sgmcgarry 			break;
7070f968376Sgmcgarry 
7080f968376Sgmcgarry 		    case MTNOP:
7090f968376Sgmcgarry 			/*
7100f968376Sgmcgarry 			 * NOP is supposed to set status bits.
7110f968376Sgmcgarry 			 * Force readdsj to do it.
7120f968376Sgmcgarry 			 */
7130f968376Sgmcgarry 			switch (mtreaddsj(sc,
7140f968376Sgmcgarry 			  MTE_DSJ_FORCE | MTE_COMPLETE | MTE_IDLE)) {
7150f968376Sgmcgarry 			    default:
7160f968376Sgmcgarry 				goto done;
7170f968376Sgmcgarry 
7180f968376Sgmcgarry 			    case -1:
7190f968376Sgmcgarry 				/*
7200f968376Sgmcgarry 				 * If this fails, perform a device clear
7210f968376Sgmcgarry 				 * to fix any protocol problems and (most
7220f968376Sgmcgarry 				 * likely) get the status.
7230f968376Sgmcgarry 				 */
7240f968376Sgmcgarry 				bp->b_cmd = MTRESET;
7250f968376Sgmcgarry 				break;
7260f968376Sgmcgarry 
7270f968376Sgmcgarry 			    case -2:
7280f968376Sgmcgarry 				callout_reset(&sc->sc_start_ch, hz >> 5,
7290f968376Sgmcgarry 				    mtstart_callout, sc);
7300f968376Sgmcgarry 				return;
7310f968376Sgmcgarry 			}
732*53802318Sriastradh 			__unreachable();
7330f968376Sgmcgarry 
7340f968376Sgmcgarry 		    case MTRESET:
7350f968376Sgmcgarry 			/*
7360f968376Sgmcgarry 			 * 1) selected device clear (send with "-2" secondary)
7370f968376Sgmcgarry 			 * 2) set timeout, then wait for "service request"
7380f968376Sgmcgarry 			 * 3) interrupt will read DSJ (and END COMPLETE-IDLE)
7390f968376Sgmcgarry 			 */
7400f968376Sgmcgarry 			if (gpibsend(sc->sc_ic, sc->sc_slave, -2, NULL, 0)){
7415f819ca3Schs 				aprint_error_dev(sc->sc_dev, "can't reset");
7420f968376Sgmcgarry 				goto fatalerror;
7430f968376Sgmcgarry 			}
7440f968376Sgmcgarry 			callout_reset(&sc->sc_intr_ch, 4*hz, mtintr_callout,
7450f968376Sgmcgarry 			    sc);
7460f968376Sgmcgarry 			gpibawait(sc->sc_ic);
7470f968376Sgmcgarry 			return;
7480f968376Sgmcgarry 
7490f968376Sgmcgarry 		    case MTSET800BPI:
7500f968376Sgmcgarry 			cmdbuf[0] = MTTC_800;
7510f968376Sgmcgarry 			break;
7520f968376Sgmcgarry 
7530f968376Sgmcgarry 		    case MTSET1600BPI:
7540f968376Sgmcgarry 			cmdbuf[0] = MTTC_1600;
7550f968376Sgmcgarry 			break;
7560f968376Sgmcgarry 
7570f968376Sgmcgarry 		    case MTSET6250BPI:
7580f968376Sgmcgarry 			cmdbuf[0] = MTTC_6250;
7590f968376Sgmcgarry 			break;
7600f968376Sgmcgarry 
7610f968376Sgmcgarry 		    case MTSET6250DC:
7620f968376Sgmcgarry 			cmdbuf[0] = MTTC_DC6250;
7630f968376Sgmcgarry 			break;
7640f968376Sgmcgarry 		}
7650f968376Sgmcgarry 	} else {
7660f968376Sgmcgarry 		if (sc->sc_flags & MTF_PASTEOT) {
7670f968376Sgmcgarry 			bp->b_error = ENOSPC;
76866fefd11Sad 			goto done;
7690f968376Sgmcgarry 		}
7700f968376Sgmcgarry 		if (bp->b_flags & B_READ) {
7710f968376Sgmcgarry 			sc->sc_flags |= MTF_IO;
7720f968376Sgmcgarry 			cmdbuf[0] = MTTC_READ;
7730f968376Sgmcgarry 		} else {
7740f968376Sgmcgarry 			sc->sc_flags |= MTF_WRT | MTF_IO;
7750f968376Sgmcgarry 			cmdbuf[0] = MTTC_WRITE;
7760f968376Sgmcgarry 			cmdbuf[1] = (bp->b_bcount +((1 << WRITE_BITS_IGNORED) - 1)) >> WRITE_BITS_IGNORED;
7770f968376Sgmcgarry 			cmdcount = 2;
7780f968376Sgmcgarry 		}
7790f968376Sgmcgarry 	}
7800f968376Sgmcgarry 	if (gpibsend(sc->sc_ic, sc->sc_slave, MTL_TCMD, cmdbuf, cmdcount)
7810f968376Sgmcgarry 	    == cmdcount) {
7820f968376Sgmcgarry 		if (sc->sc_flags & MTF_REW)
7830f968376Sgmcgarry 			goto done;
7840f968376Sgmcgarry 		gpibawait(sc->sc_ic);
7850f968376Sgmcgarry 		return;
7860f968376Sgmcgarry 	}
7870f968376Sgmcgarry fatalerror:
7880f968376Sgmcgarry 	/*
7890f968376Sgmcgarry 	 * If anything fails, the drive is probably hosed, so mark it not
7900f968376Sgmcgarry 	 * "ALIVE" (but it EXISTS and is OPEN or we wouldn't be here, and
7910f968376Sgmcgarry 	 * if, last we heard, it was REWinding, remember that).
7920f968376Sgmcgarry 	 */
7930f968376Sgmcgarry 	sc->sc_flags &= MTF_EXISTS | MTF_OPEN | MTF_REW;
7940f968376Sgmcgarry 	bp->b_error = EIO;
7950f968376Sgmcgarry done:
7960f968376Sgmcgarry 	sc->sc_flags &= ~(MTF_HITEOF | MTF_HITBOF);
79770de9736Syamt 	(void)bufq_get(sc->sc_tab);
7980f968376Sgmcgarry 	biodone(bp);
7990f968376Sgmcgarry 	gpibrelease(sc->sc_ic, sc->sc_hdl);
80070de9736Syamt 	if ((bp = bufq_peek(sc->sc_tab)) == NULL)
8010f968376Sgmcgarry 		sc->sc_active = 0;
8020f968376Sgmcgarry 	else
8030f968376Sgmcgarry 		mtustart(sc);
8040f968376Sgmcgarry }
8050f968376Sgmcgarry 
8060f968376Sgmcgarry void
mtintr(struct mt_softc * sc)807454af1c0Sdsl mtintr(struct mt_softc *sc)
8080f968376Sgmcgarry {
8090f968376Sgmcgarry 	struct buf *bp;
8100f968376Sgmcgarry 	int slave, dir, i;
8110f968376Sgmcgarry 	u_char cmdbuf[4];
8120f968376Sgmcgarry 
8130f968376Sgmcgarry 	slave = sc->sc_slave;
8140f968376Sgmcgarry 
81570de9736Syamt 	bp = bufq_peek(sc->sc_tab);
8160f968376Sgmcgarry 	if (bp == NULL) {
8175f819ca3Schs 		printf("%s intr: bp == NULL", device_xname(sc->sc_dev));
8180f968376Sgmcgarry 		return;
8190f968376Sgmcgarry 	}
8200f968376Sgmcgarry 
8215f819ca3Schs 	DPRINTF(MDB_ANY, ("%s intr", device_xname(sc->sc_dev)));
8220f968376Sgmcgarry 
8230f968376Sgmcgarry 	/*
8240f968376Sgmcgarry 	 * Some operation completed.  Read status bytes and report errors.
8250f968376Sgmcgarry 	 * Clear EOF flags here `cause they're set once on specific conditions
8260f968376Sgmcgarry 	 * below when a command succeeds.
8270f968376Sgmcgarry 	 * A DSJ of 2 always means keep waiting.  If the command was READ
8280f968376Sgmcgarry 	 * (and we're in data DMA phase) stop data transfer first.
8290f968376Sgmcgarry 	 */
8300f968376Sgmcgarry 	sc->sc_flags &= ~(MTF_HITEOF | MTF_HITBOF);
8310f968376Sgmcgarry 	if ((bp->b_flags & (B_CMD|B_READ)) == B_READ &&
8320f968376Sgmcgarry 	    !(sc->sc_flags & (MTF_IO | MTF_STATTIMEO | MTF_DSJTIMEO))){
8330f968376Sgmcgarry 		cmdbuf[0] = MTE_STOP;
8340f968376Sgmcgarry 		(void) gpibsend(sc->sc_ic, slave, MTL_ECMD,cmdbuf,1);
8350f968376Sgmcgarry 	}
8360f968376Sgmcgarry 	switch (mtreaddsj(sc, 0)) {
8370f968376Sgmcgarry 	    case 0:
8380f968376Sgmcgarry 		break;
8390f968376Sgmcgarry 
8400f968376Sgmcgarry 	    case 1:
8410f968376Sgmcgarry 		/*
8420f968376Sgmcgarry 		 * If we're in the middle of a READ/WRITE and have yet to
8430f968376Sgmcgarry 		 * start the data transfer, a DSJ of one should terminate it.
8440f968376Sgmcgarry 		 */
8450f968376Sgmcgarry 		sc->sc_flags &= ~MTF_IO;
8460f968376Sgmcgarry 		break;
8470f968376Sgmcgarry 
8480f968376Sgmcgarry 	    case 2:
8490f968376Sgmcgarry 		(void) gpibawait(sc->sc_ic);
8500f968376Sgmcgarry 		return;
8510f968376Sgmcgarry 
8520f968376Sgmcgarry 	    case -2:
8530f968376Sgmcgarry 		/*
8540f968376Sgmcgarry 		 * -2 means that the drive failed to respond quickly enough
8550f968376Sgmcgarry 		 * to the request for DSJ.  It's probably just "busy" figuring
8560f968376Sgmcgarry 		 * it out and will know in a little bit...
8570f968376Sgmcgarry 		 */
8580f968376Sgmcgarry 		callout_reset(&sc->sc_intr_ch, hz >> 5, mtintr_callout, sc);
8590f968376Sgmcgarry 		return;
8600f968376Sgmcgarry 
8610f968376Sgmcgarry 	    default:
862fea98925Smsaitoh 		printf("%s intr: can't get drive stat",
863fea98925Smsaitoh 		    device_xname(sc->sc_dev));
8640f968376Sgmcgarry 		goto error;
8650f968376Sgmcgarry 	}
8660f968376Sgmcgarry 	if (sc->sc_stat1 & (SR1_ERR | SR1_REJECT)) {
8670f968376Sgmcgarry 		i = sc->sc_stat4 & SR4_ERCLMASK;
8680f968376Sgmcgarry 		printf("%s: %s error, retry %d, SR2/3 %x/%x, code %d",
8695f819ca3Schs 			device_xname(sc->sc_dev), i == SR4_DEVICE ? "device" :
8700f968376Sgmcgarry 			(i == SR4_PROTOCOL ? "protocol" :
8710f968376Sgmcgarry 			(i == SR4_SELFTEST ? "selftest" : "unknown")),
8720f968376Sgmcgarry 			sc->sc_stat4 & SR4_RETRYMASK, sc->sc_stat2,
8730f968376Sgmcgarry 			sc->sc_stat3, sc->sc_stat5);
8740f968376Sgmcgarry 
8750f968376Sgmcgarry 		if ((bp->b_flags & B_CMD) && bp->b_cmd == MTRESET)
8760f968376Sgmcgarry 			callout_stop(&sc->sc_intr_ch);
8770f968376Sgmcgarry 		if (sc->sc_stat3 & SR3_POWERUP)
8780f968376Sgmcgarry 			sc->sc_flags &= MTF_OPEN | MTF_EXISTS;
8790f968376Sgmcgarry 		goto error;
8800f968376Sgmcgarry 	}
8810f968376Sgmcgarry 	/*
8820f968376Sgmcgarry 	 * Report and clear any soft errors.
8830f968376Sgmcgarry 	 */
8840f968376Sgmcgarry 	if (sc->sc_stat1 & SR1_SOFTERR) {
8855f819ca3Schs 		printf("%s: soft error, retry %d\n", device_xname(sc->sc_dev),
8860f968376Sgmcgarry 		    sc->sc_stat4 & SR4_RETRYMASK);
8870f968376Sgmcgarry 		sc->sc_stat1 &= ~SR1_SOFTERR;
8880f968376Sgmcgarry 	}
8890f968376Sgmcgarry 	/*
8900f968376Sgmcgarry 	 * We've initiated a read or write, but haven't actually started to
8910f968376Sgmcgarry 	 * DMA the data yet.  At this point, the drive's ready.
8920f968376Sgmcgarry 	 */
8930f968376Sgmcgarry 	if (sc->sc_flags & MTF_IO) {
8940f968376Sgmcgarry 		sc->sc_flags &= ~MTF_IO;
8950f968376Sgmcgarry 		dir = (bp->b_flags & B_READ ? GPIB_READ : GPIB_WRITE);
8960f968376Sgmcgarry 		gpibxfer(sc->sc_ic, slave,
8970f968376Sgmcgarry 		    dir == GPIB_READ ? MTT_READ : MTL_WRITE,
8980f968376Sgmcgarry 		    bp->b_data, bp->b_bcount, dir, dir == GPIB_READ);
8990f968376Sgmcgarry 		return;
9000f968376Sgmcgarry 	}
9010f968376Sgmcgarry 	/*
9020f968376Sgmcgarry 	 * Check for End Of Tape - we're allowed to hit EOT and then write (or
9030f968376Sgmcgarry 	 * read) one more record.  If we get here and have not already hit EOT,
9040f968376Sgmcgarry 	 * return ENOSPC to inform the process that it's hit it.  If we get
9050f968376Sgmcgarry 	 * here and HAVE already hit EOT, don't allow any more operations that
9060f968376Sgmcgarry 	 * move the tape forward.
9070f968376Sgmcgarry 	 */
9080f968376Sgmcgarry 	if (sc->sc_stat1 & SR1_EOT) {
9090f968376Sgmcgarry 		if (sc->sc_flags & MTF_ATEOT)
9100f968376Sgmcgarry 			sc->sc_flags |= MTF_PASTEOT;
9110f968376Sgmcgarry 		else {
9120f968376Sgmcgarry 			bp->b_error = ENOSPC;
9130f968376Sgmcgarry 			sc->sc_flags |= MTF_ATEOT;
9140f968376Sgmcgarry 		}
9150f968376Sgmcgarry 	}
9160f968376Sgmcgarry 	/*
9170f968376Sgmcgarry 	 * If a motion command was being executed, check for Tape Marks.
9180f968376Sgmcgarry 	 * If we were doing data, make sure we got the right amount, and
9190f968376Sgmcgarry 	 * check for hitting tape marks on reads.
9200f968376Sgmcgarry 	 */
9210f968376Sgmcgarry 	if (bp->b_flags & B_CMD) {
9220f968376Sgmcgarry 		if (sc->sc_stat1 & SR1_EOF) {
9230f968376Sgmcgarry 			if (bp->b_cmd == MTFSR)
9240f968376Sgmcgarry 				sc->sc_flags |= MTF_HITEOF;
9250f968376Sgmcgarry 			if (bp->b_cmd == MTBSR)
9260f968376Sgmcgarry 				sc->sc_flags |= MTF_HITBOF;
9270f968376Sgmcgarry 		}
9280f968376Sgmcgarry 		if (bp->b_cmd == MTRESET) {
9290f968376Sgmcgarry 			callout_stop(&sc->sc_intr_ch);
9300f968376Sgmcgarry 			sc->sc_flags |= MTF_ALIVE;
9310f968376Sgmcgarry 		}
9320f968376Sgmcgarry 	} else {
9330f968376Sgmcgarry 		i = gpibrecv(sc->sc_ic, slave, MTT_BCNT, cmdbuf, 2);
9340f968376Sgmcgarry 		if (i != 2) {
935fea98925Smsaitoh 			aprint_error_dev(sc->sc_dev,
936fea98925Smsaitoh 			    "intr: can't get xfer length\n");
9370f968376Sgmcgarry 			goto error;
9380f968376Sgmcgarry 		}
9390f968376Sgmcgarry 		i = (int) *((u_short *) cmdbuf);
9400f968376Sgmcgarry 		if (i <= bp->b_bcount) {
9410f968376Sgmcgarry 			if (i == 0)
9420f968376Sgmcgarry 				sc->sc_flags |= MTF_HITEOF;
9430f968376Sgmcgarry 			bp->b_resid = bp->b_bcount - i;
9440762f691Stsutsui 			DPRINTF(MDB_ANY, ("%s intr: bcount %d, resid %d",
9455f819ca3Schs 			    device_xname(sc->sc_dev),
9460762f691Stsutsui 			    bp->b_bcount, bp->b_resid));
9470f968376Sgmcgarry 		} else {
9480f968376Sgmcgarry 			tprintf(sc->sc_ttyp,
94922974bb6Scube 				"%s: record (%d) larger than wanted (%d)\n",
9505f819ca3Schs 				device_xname(sc->sc_dev), i, bp->b_bcount);
9510f968376Sgmcgarry error:
9520f968376Sgmcgarry 			sc->sc_flags &= ~MTF_IO;
9530f968376Sgmcgarry 			bp->b_error = EIO;
9540f968376Sgmcgarry 		}
9550f968376Sgmcgarry 	}
9560f968376Sgmcgarry 	/*
9570f968376Sgmcgarry 	 * The operation is completely done.
9580f968376Sgmcgarry 	 * Let the drive know with an END command.
9590f968376Sgmcgarry 	 */
9600f968376Sgmcgarry 	cmdbuf[0] = MTE_COMPLETE | MTE_IDLE;
9610f968376Sgmcgarry 	(void) gpibsend(sc->sc_ic, slave, MTL_ECMD, cmdbuf, 1);
9620f968376Sgmcgarry 	bp->b_flags &= ~B_CMD;
96370de9736Syamt 	(void)bufq_get(sc->sc_tab);
9640f968376Sgmcgarry 	biodone(bp);
9650f968376Sgmcgarry 	gpibrelease(sc->sc_ic, sc->sc_hdl);
96670de9736Syamt 	if (bufq_peek(sc->sc_tab) == NULL)
9670f968376Sgmcgarry 		sc->sc_active = 0;
9680f968376Sgmcgarry 	else
9690f968376Sgmcgarry 		mtustart(sc);
9700f968376Sgmcgarry }
9710f968376Sgmcgarry 
9720f968376Sgmcgarry int
mtread(dev_t dev,struct uio * uio,int flags)9733e484e61Scegger mtread(dev_t dev, struct uio *uio, int flags)
9740f968376Sgmcgarry {
975610bc1e5Sad 	return (physio(mtstrategy, NULL, dev, B_READ, minphys, uio));
9760f968376Sgmcgarry }
9770f968376Sgmcgarry 
9780f968376Sgmcgarry int
mtwrite(dev_t dev,struct uio * uio,int flags)9793e484e61Scegger mtwrite(dev_t dev, struct uio *uio, int flags)
9800f968376Sgmcgarry {
981610bc1e5Sad 	return (physio(mtstrategy, NULL, dev, B_WRITE, minphys, uio));
9820f968376Sgmcgarry }
9830f968376Sgmcgarry 
9840f968376Sgmcgarry int
mtioctl(dev_t dev,u_long cmd,void * data,int flag,struct lwp * l)985454af1c0Sdsl mtioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
9860f968376Sgmcgarry {
9870f968376Sgmcgarry 	struct mtop *op;
9880f968376Sgmcgarry 	int cnt;
9890f968376Sgmcgarry 
9900f968376Sgmcgarry 	switch (cmd) {
9910f968376Sgmcgarry 	    case MTIOCTOP:
9920f968376Sgmcgarry 		op = (struct mtop *)data;
9930f968376Sgmcgarry 		switch(op->mt_op) {
9940f968376Sgmcgarry 		    case MTWEOF:
9950f968376Sgmcgarry 		    case MTFSF:
9960f968376Sgmcgarry 		    case MTBSR:
9970f968376Sgmcgarry 		    case MTBSF:
9980f968376Sgmcgarry 		    case MTFSR:
9990f968376Sgmcgarry 			cnt = op->mt_count;
10000f968376Sgmcgarry 			break;
10010f968376Sgmcgarry 
10020f968376Sgmcgarry 		    case MTOFFL:
10030f968376Sgmcgarry 		    case MTREW:
10040f968376Sgmcgarry 		    case MTNOP:
10050f968376Sgmcgarry 			cnt = 0;
10060f968376Sgmcgarry 			break;
10070f968376Sgmcgarry 
10080f968376Sgmcgarry 		    default:
10090f968376Sgmcgarry 			return (EINVAL);
10100f968376Sgmcgarry 		}
10110f968376Sgmcgarry 		return (mtcommand(dev, op->mt_op, cnt));
10120f968376Sgmcgarry 
10130f968376Sgmcgarry 	    case MTIOCGET:
10140f968376Sgmcgarry 		break;
10150f968376Sgmcgarry 
10160f968376Sgmcgarry 	    default:
10170f968376Sgmcgarry 		return (EINVAL);
10180f968376Sgmcgarry 	}
10190f968376Sgmcgarry 	return (0);
10200f968376Sgmcgarry }
1021