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