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