Home | History | Annotate | Line # | Download | only in gpib
mt.c revision 1.17
      1 /*	$NetBSD: mt.c,v 1.17 2009/03/14 15:36:17 dsl 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.17 2009/03/14 15:36:17 dsl 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(int id, int slave, int punit)
    228 {
    229 	int i;
    230 
    231 	for (i = 0; i < nmtinfo; i++)
    232 		if (mtinfo[i].hwid == id)
    233 			break;
    234 	if (i == nmtinfo)
    235 		return (-1);
    236 	return (0);
    237 }
    238 
    239 int
    240 mtmatch(struct device *parent, struct cfdata *match, void *aux)
    241 {
    242 	struct cs80bus_attach_args *ca = aux;
    243 
    244 	ca->ca_punit = 0;
    245 	return (mtlookup(ca->ca_id, ca->ca_slave, ca->ca_punit) == 0);
    246 }
    247 
    248 void
    249 mtattach(parent, self, aux)
    250 	struct device *parent, *self;
    251 	void *aux;
    252 {
    253 	struct mt_softc *sc = device_private(self);
    254 	struct cs80bus_attach_args *ca = aux;
    255 	int type;
    256 
    257 	sc->sc_ic = ca->ca_ic;
    258 	sc->sc_slave = ca->ca_slave;
    259 
    260 	if ((type = mtlookup(ca->ca_id, ca->ca_slave, ca->ca_punit)) < 0)
    261 		return;
    262 
    263 	printf(": %s tape\n", mtinfo[type].desc);
    264 
    265 	sc->sc_type = type;
    266 	sc->sc_flags = MTF_EXISTS;
    267 
    268 	bufq_alloc(&sc->sc_tab, "fcfs", 0);
    269 	callout_init(&sc->sc_start_ch, 0);
    270 	callout_init(&sc->sc_intr_ch, 0);
    271 
    272 	if (gpibregister(sc->sc_ic, sc->sc_slave, mtcallback, sc,
    273 	    &sc->sc_hdl)) {
    274 		aprint_error_dev(&sc->sc_dev, "can't register callback\n");
    275 		return;
    276 	}
    277 }
    278 
    279 /*
    280  * Perform a read of "Device Status Jump" register and update the
    281  * status if necessary.  If status is read, the given "ecmd" is also
    282  * performed, unless "ecmd" is zero.  Returns DSJ value, -1 on failure
    283  * and -2 on "temporary" failure.
    284  */
    285 int
    286 mtreaddsj(struct mt_softc *sc, int ecmd)
    287 {
    288 	int retval;
    289 
    290 	if (sc->sc_flags & MTF_STATTIMEO)
    291 		goto getstats;
    292 	retval = gpibrecv(sc->sc_ic,
    293 	    (sc->sc_flags & MTF_DSJTIMEO) ? -1 : sc->sc_slave,
    294 	    MTT_DSJ, &(sc->sc_lastdsj), 1);
    295 	sc->sc_flags &= ~MTF_DSJTIMEO;
    296 	if (retval != 1) {
    297 		DPRINTF(MDB_ANY, ("%s can't gpibrecv DSJ",
    298 		    device_xname(&sc->sc_dev)));
    299 		if (sc->sc_recvtimeo == 0)
    300 			sc->sc_recvtimeo = hz;
    301 		if (--sc->sc_recvtimeo == 0)
    302 			return (-1);
    303 		if (retval == 0)
    304 			sc->sc_flags |= MTF_DSJTIMEO;
    305 		return (-2);
    306 	}
    307 	sc->sc_recvtimeo = 0;
    308 	sc->sc_statindex = 0;
    309 	DPRINTF(MDB_ANY, ("%s readdsj: 0x%x", device_xname(&sc->sc_dev),
    310 	    sc->sc_lastdsj));
    311 	sc->sc_lastecmd = ecmd;
    312 	switch (sc->sc_lastdsj) {
    313 	    case 0:
    314 		if (ecmd & MTE_DSJ_FORCE)
    315 			break;
    316 		return (0);
    317 
    318 	    case 2:
    319 		sc->sc_lastecmd = MTE_COMPLETE;
    320 	    case 1:
    321 		break;
    322 
    323 	    default:
    324 		printf("%s readdsj: DSJ 0x%x\n", device_xname(&sc->sc_dev),
    325 		    sc->sc_lastdsj);
    326 		return (-1);
    327 	}
    328 
    329 getstats:
    330 	retval = gpibrecv(sc->sc_ic,
    331 	    (sc->sc_flags & MTF_STATCONT) ? -1 : sc->sc_slave, MTT_STAT,
    332 	     ((char *)&(sc->sc_stat)) + sc->sc_statindex,
    333 	    sizeof(sc->sc_stat) - sc->sc_statindex);
    334 	sc->sc_flags &= ~(MTF_STATTIMEO | MTF_STATCONT);
    335 	if (retval != sizeof(sc->sc_stat) - sc->sc_statindex) {
    336 		if (sc->sc_recvtimeo == 0)
    337 			sc->sc_recvtimeo = hz;
    338 		if (--sc->sc_recvtimeo != 0) {
    339 			if (retval >= 0) {
    340 				sc->sc_statindex += retval;
    341 				sc->sc_flags |= MTF_STATCONT;
    342 			}
    343 			sc->sc_flags |= MTF_STATTIMEO;
    344 			return (-2);
    345 		}
    346 		printf("%s readdsj: can't read status", device_xname(&sc->sc_dev));
    347 		return (-1);
    348 	}
    349 	sc->sc_recvtimeo = 0;
    350 	sc->sc_statindex = 0;
    351 	DPRINTF(MDB_ANY, ("%s readdsj: status is %x %x %x %x %x %x",
    352 	    device_xname(&sc->sc_dev),
    353 	    sc->sc_stat1, sc->sc_stat2, sc->sc_stat3,
    354 	    sc->sc_stat4, sc->sc_stat5, sc->sc_stat6));
    355 	if (sc->sc_lastecmd)
    356 		(void) gpibsend(sc->sc_ic, sc->sc_slave,
    357 		    MTL_ECMD, &(sc->sc_lastecmd), 1);
    358 	return ((int) sc->sc_lastdsj);
    359 }
    360 
    361 int
    362 mtopen(dev_t dev, int flag, int mode, struct lwp *l)
    363 {
    364 	struct mt_softc *sc;
    365 	int req_den;
    366 	int error;
    367 
    368 	sc = device_lookup_private(&mt_cd, MTUNIT(dev));
    369 	if (sc == NULL || (sc->sc_flags & MTF_EXISTS) == 0)
    370 		return (ENXIO);
    371 
    372 	if (sc->sc_flags & MTF_OPEN)
    373 		return (EBUSY);
    374 
    375 	DPRINTF(MDB_ANY, ("%s open: flags 0x%x", device_xname(&sc->sc_dev),
    376 	    sc->sc_flags));
    377 
    378 	sc->sc_flags |= MTF_OPEN;
    379 	sc->sc_ttyp = tprintf_open(l->l_proc);
    380 	if ((sc->sc_flags & MTF_ALIVE) == 0) {
    381 		error = mtcommand(dev, MTRESET, 0);
    382 		if (error != 0 || (sc->sc_flags & MTF_ALIVE) == 0)
    383 			goto errout;
    384 		if ((sc->sc_stat1 & (SR1_BOT | SR1_ONLINE)) == SR1_ONLINE)
    385 			(void) mtcommand(dev, MTREW, 0);
    386 	}
    387 	for (;;) {
    388 		if ((error = mtcommand(dev, MTNOP, 0)) != 0)
    389 			goto errout;
    390 		if (!(sc->sc_flags & MTF_REW))
    391 			break;
    392 		if (tsleep((void *) &lbolt, PCATCH | (PZERO + 1),
    393 		    "mt", 0) != 0) {
    394 			error = EINTR;
    395 			goto errout;
    396 		}
    397 	}
    398 	if ((flag & FWRITE) && (sc->sc_stat1 & SR1_RO)) {
    399 		error = EROFS;
    400 		goto errout;
    401 	}
    402 	if (!(sc->sc_stat1 & SR1_ONLINE)) {
    403 		uprintf("%s: not online\n", device_xname(&sc->sc_dev));
    404 		error = EIO;
    405 		goto errout;
    406 	}
    407 	/*
    408 	 * Select density:
    409 	 *  - find out what density the drive is set to
    410 	 *	(i.e. the density of the current tape)
    411 	 *  - if we are going to write
    412 	 *    - if we're not at the beginning of the tape
    413 	 *      - complain if we want to change densities
    414 	 *    - otherwise, select the mtcommand to set the density
    415 	 *
    416 	 * If the drive doesn't support it then don't change the recorded
    417 	 * density.
    418 	 *
    419 	 * The original MOREbsd code had these additional conditions
    420 	 * for the mid-tape change
    421 	 *
    422 	 *	req_den != T_BADBPI &&
    423 	 *	sc->sc_density != T_6250BPI
    424 	 *
    425 	 * which suggests that it would be possible to write multiple
    426 	 * densities if req_den == T_BAD_BPI or the current tape
    427 	 * density was 6250.  Testing of our 7980 suggests that the
    428 	 * device cannot change densities mid-tape.
    429 	 *
    430 	 * ajv (at) comp.vuw.ac.nz
    431 	 */
    432 	sc->sc_density = (sc->sc_stat2 & SR2_6250) ? T_6250BPI : (
    433 			 (sc->sc_stat3 & SR3_1600) ? T_1600BPI : (
    434 			 (sc->sc_stat3 & SR3_800) ? T_800BPI : -1));
    435 	req_den = (dev & T_DENSEL);
    436 
    437 	if (flag & FWRITE) {
    438 		if (!(sc->sc_stat1 & SR1_BOT)) {
    439 			if (sc->sc_density != req_den) {
    440 				uprintf("%s: can't change density mid-tape\n",
    441 				    device_xname(&sc->sc_dev));
    442 				error = EIO;
    443 				goto errout;
    444 			}
    445 		}
    446 		else {
    447 			int mtset_density =
    448 			    (req_den == T_800BPI  ? MTSET800BPI : (
    449 			     req_den == T_1600BPI ? MTSET1600BPI : (
    450 			     req_den == T_6250BPI ? MTSET6250BPI : (
    451 			     sc->sc_type == MT7980ID
    452 						  ? MTSET6250DC
    453 						  : MTSET6250BPI))));
    454 			if (mtcommand(dev, mtset_density, 0) == 0)
    455 				sc->sc_density = req_den;
    456 		}
    457 	}
    458 	return (0);
    459 errout:
    460 	sc->sc_flags &= ~MTF_OPEN;
    461 	return (error);
    462 }
    463 
    464 int
    465 mtclose(dev_t dev, int flag, int fmt, struct lwp *l)
    466 {
    467 	struct mt_softc *sc;
    468 
    469 	sc = device_lookup_private(&mt_cd, MTUNIT(dev));
    470 	if (sc == NULL)
    471 		return (ENXIO);
    472 
    473 	if (sc->sc_flags & MTF_WRT) {
    474 		(void) mtcommand(dev, MTWEOF, 2);
    475 		(void) mtcommand(dev, MTBSF, 0);
    476 	}
    477 	if ((minor(dev) & T_NOREWIND) == 0)
    478 		(void) mtcommand(dev, MTREW, 0);
    479 	sc->sc_flags &= ~MTF_OPEN;
    480 	tprintf_close(sc->sc_ttyp);
    481 	return (0);
    482 }
    483 
    484 int
    485 mtcommand(dev_t dev, int cmd, int cnt)
    486 {
    487 	struct mt_softc *sc;
    488 	struct buf *bp;
    489 	int error = 0;
    490 
    491 	sc = device_lookup_private(&mt_cd, MTUNIT(dev));
    492 	bp = &sc->sc_bufstore;
    493 
    494 	if (bp->b_cflags & BC_BUSY)
    495 		return (EBUSY);
    496 
    497 	bp->b_cmd = cmd;
    498 	bp->b_dev = dev;
    499 	bp->b_objlock = &buffer_lock;
    500 	do {
    501 		bp->b_cflags = BC_BUSY;
    502 		bp->b_flags = B_CMD;
    503 		bp->b_oflags = 0;
    504 		mtstrategy(bp);
    505 		biowait(bp);
    506 		if (bp->b_error != 0) {
    507 			error = (int) (unsigned) bp->b_error;
    508 			break;
    509 		}
    510 	} while (--cnt > 0);
    511 #if 0
    512 	bp->b_cflags = 0 /*&= ~BC_BUSY*/;
    513 #else
    514 	bp->b_cflags &= ~BC_BUSY;
    515 #endif
    516 	return (error);
    517 }
    518 
    519 /*
    520  * Only thing to check here is for legal record lengths (writes only).
    521  */
    522 void
    523 mtstrategy(struct buf *bp)
    524 {
    525 	struct mt_softc *sc;
    526 	int s;
    527 
    528 	sc = device_lookup_private(&mt_cd, MTUNIT(bp->b_dev));
    529 
    530 	DPRINTF(MDB_ANY, ("%s strategy", device_xname(&sc->sc_dev)));
    531 
    532 	if ((bp->b_flags & (B_CMD | B_READ)) == 0) {
    533 #define WRITE_BITS_IGNORED	8
    534 #if 0
    535 		if (bp->b_bcount & ((1 << WRITE_BITS_IGNORED) - 1)) {
    536 			tprintf(sc->sc_ttyp,
    537 				"%s: write record must be multiple of %d\n",
    538 				device_xname(&sc->sc_dev), 1 << WRITE_BITS_IGNORED);
    539 			goto error;
    540 		}
    541 #endif
    542 		s = 16 * 1024;
    543 		if (sc->sc_stat2 & SR2_LONGREC) {
    544 			switch (sc->sc_density) {
    545 			    case T_1600BPI:
    546 				s = 32 * 1024;
    547 				break;
    548 
    549 			    case T_6250BPI:
    550 			    case T_BADBPI:
    551 				s = 60 * 1024;
    552 				break;
    553 			}
    554 		}
    555 		if (bp->b_bcount > s) {
    556 			tprintf(sc->sc_ttyp,
    557 				"%s: write record (%d) too big: limit (%d)\n",
    558 				device_xname(&sc->sc_dev), bp->b_bcount, s);
    559 #if 0 /* XXX see above */
    560 	    error:
    561 #endif
    562 			bp->b_error = EIO;
    563 			biodone(bp);
    564 			return;
    565 		}
    566 	}
    567 	s = splbio();
    568 	bufq_put(sc->sc_tab, bp);
    569 	if (sc->sc_active == 0) {
    570 		sc->sc_active = 1;
    571 		mtustart(sc);
    572 	}
    573 	splx(s);
    574 }
    575 
    576 void
    577 mtustart(struct mt_softc *sc)
    578 {
    579 
    580 	DPRINTF(MDB_ANY, ("%s ustart", device_xname(&sc->sc_dev)));
    581 	if (gpibrequest(sc->sc_ic, sc->sc_hdl))
    582 		mtstart(sc);
    583 }
    584 
    585 void
    586 mtcallback(void *v, int action)
    587 {
    588 	struct mt_softc *sc = v;
    589 
    590 	DPRINTF(MDB_FOLLOW, ("mtcallback: v=%p, action=%d\n", v, action));
    591 
    592 	switch (action) {
    593 	case GPIBCBF_START:
    594 		mtstart(sc);
    595 		break;
    596 	case GPIBCBF_INTR:
    597 		mtintr(sc);
    598 		break;
    599 #ifdef DEBUG
    600 	default:
    601 		printf("mtcallback: unknown action %d\n", action);
    602 		break;
    603 #endif
    604 	}
    605 }
    606 
    607 void
    608 mtintr_callout(void *arg)
    609 {
    610 	struct mt_softc *sc = arg;
    611 	int s = splbio();
    612 
    613 	gpibppclear(sc->sc_ic);
    614 	mtintr(sc);
    615 	splx(s);
    616 }
    617 
    618 void
    619 mtstart_callout(void *arg)
    620 {
    621 	int s = splbio();
    622 
    623 	mtstart((struct mt_softc *)arg);
    624 	splx(s);
    625 }
    626 
    627 void
    628 mtstart(struct mt_softc *sc)
    629 {
    630 	struct buf *bp;
    631 	short	cmdcount = 1;
    632 	u_char	cmdbuf[2];
    633 
    634 	DPRINTF(MDB_ANY, ("%s start", device_xname(&sc->sc_dev)));
    635 	sc->sc_flags &= ~MTF_WRT;
    636 	bp = bufq_peek(sc->sc_tab);
    637 	if ((sc->sc_flags & MTF_ALIVE) == 0 &&
    638 	    ((bp->b_flags & B_CMD) == 0 || bp->b_cmd != MTRESET))
    639 		goto fatalerror;
    640 
    641 	if (sc->sc_flags & MTF_REW) {
    642 		if (!gpibpptest(sc->sc_ic, sc->sc_slave))
    643 			goto stillrew;
    644 		switch (mtreaddsj(sc, MTE_DSJ_FORCE|MTE_COMPLETE|MTE_IDLE)) {
    645 		    case 0:
    646 		    case 1:
    647 		stillrew:
    648 			if ((sc->sc_stat1 & SR1_BOT) ||
    649 			    !(sc->sc_stat1 & SR1_ONLINE)) {
    650 				sc->sc_flags &= ~MTF_REW;
    651 				break;
    652 			}
    653 		    case -2:
    654 			/*
    655 			 * -2 means "timeout" reading DSJ, which is probably
    656 			 * temporary.  This is considered OK when doing a NOP,
    657 			 * but not otherwise.
    658 			 */
    659 			if (sc->sc_flags & (MTF_DSJTIMEO | MTF_STATTIMEO)) {
    660 				callout_reset(&sc->sc_start_ch, hz >> 5,
    661 				    mtstart_callout, sc);
    662 				return;
    663 			}
    664 		    case 2:
    665 			if (bp->b_cmd != MTNOP || !(bp->b_flags & B_CMD)) {
    666 				bp->b_error = EBUSY;
    667 				goto done;
    668 			}
    669 			goto done;
    670 
    671 		    default:
    672 			goto fatalerror;
    673 		}
    674 	}
    675 	if (bp->b_flags & B_CMD) {
    676 		if (sc->sc_flags & MTF_PASTEOT) {
    677 			switch(bp->b_cmd) {
    678 			    case MTFSF:
    679 			    case MTWEOF:
    680 			    case MTFSR:
    681 				bp->b_error = ENOSPC;
    682 				goto done;
    683 
    684 			    case MTBSF:
    685 			    case MTOFFL:
    686 			    case MTBSR:
    687 			    case MTREW:
    688 				sc->sc_flags &= ~(MTF_PASTEOT | MTF_ATEOT);
    689 				break;
    690 			}
    691 		}
    692 		switch(bp->b_cmd) {
    693 		    case MTFSF:
    694 			if (sc->sc_flags & MTF_HITEOF)
    695 				goto done;
    696 			cmdbuf[0] = MTTC_FSF;
    697 			break;
    698 
    699 		    case MTBSF:
    700 			if (sc->sc_flags & MTF_HITBOF)
    701 				goto done;
    702 			cmdbuf[0] = MTTC_BSF;
    703 			break;
    704 
    705 		    case MTOFFL:
    706 			sc->sc_flags |= MTF_REW;
    707 			cmdbuf[0] = MTTC_REWOFF;
    708 			break;
    709 
    710 		    case MTWEOF:
    711 			cmdbuf[0] = MTTC_WFM;
    712 			break;
    713 
    714 		    case MTBSR:
    715 			cmdbuf[0] = MTTC_BSR;
    716 			break;
    717 
    718 		    case MTFSR:
    719 			cmdbuf[0] = MTTC_FSR;
    720 			break;
    721 
    722 		    case MTREW:
    723 			sc->sc_flags |= MTF_REW;
    724 			cmdbuf[0] = MTTC_REW;
    725 			break;
    726 
    727 		    case MTNOP:
    728 			/*
    729 			 * NOP is supposed to set status bits.
    730 			 * Force readdsj to do it.
    731 			 */
    732 			switch (mtreaddsj(sc,
    733 			  MTE_DSJ_FORCE | MTE_COMPLETE | MTE_IDLE)) {
    734 			    default:
    735 				goto done;
    736 
    737 			    case -1:
    738 				/*
    739 				 * If this fails, perform a device clear
    740 				 * to fix any protocol problems and (most
    741 				 * likely) get the status.
    742 				 */
    743 				bp->b_cmd = MTRESET;
    744 				break;
    745 
    746 			    case -2:
    747 				callout_reset(&sc->sc_start_ch, hz >> 5,
    748 				    mtstart_callout, sc);
    749 				return;
    750 			}
    751 
    752 		    case MTRESET:
    753 			/*
    754 			 * 1) selected device clear (send with "-2" secondary)
    755 			 * 2) set timeout, then wait for "service request"
    756 			 * 3) interrupt will read DSJ (and END COMPLETE-IDLE)
    757 			 */
    758 			if (gpibsend(sc->sc_ic, sc->sc_slave, -2, NULL, 0)){
    759 				aprint_error_dev(&sc->sc_dev, "can't reset");
    760 				goto fatalerror;
    761 			}
    762 			callout_reset(&sc->sc_intr_ch, 4*hz, mtintr_callout,
    763 			    sc);
    764 			gpibawait(sc->sc_ic);
    765 			return;
    766 
    767 		    case MTSET800BPI:
    768 			cmdbuf[0] = MTTC_800;
    769 			break;
    770 
    771 		    case MTSET1600BPI:
    772 			cmdbuf[0] = MTTC_1600;
    773 			break;
    774 
    775 		    case MTSET6250BPI:
    776 			cmdbuf[0] = MTTC_6250;
    777 			break;
    778 
    779 		    case MTSET6250DC:
    780 			cmdbuf[0] = MTTC_DC6250;
    781 			break;
    782 		}
    783 	} else {
    784 		if (sc->sc_flags & MTF_PASTEOT) {
    785 			bp->b_error = ENOSPC;
    786 			goto done;
    787 		}
    788 		if (bp->b_flags & B_READ) {
    789 			sc->sc_flags |= MTF_IO;
    790 			cmdbuf[0] = MTTC_READ;
    791 		} else {
    792 			sc->sc_flags |= MTF_WRT | MTF_IO;
    793 			cmdbuf[0] = MTTC_WRITE;
    794 			cmdbuf[1] = (bp->b_bcount +((1 << WRITE_BITS_IGNORED) - 1)) >> WRITE_BITS_IGNORED;
    795 			cmdcount = 2;
    796 		}
    797 	}
    798 	if (gpibsend(sc->sc_ic, sc->sc_slave, MTL_TCMD, cmdbuf, cmdcount)
    799 	    == cmdcount) {
    800 		if (sc->sc_flags & MTF_REW)
    801 			goto done;
    802 		gpibawait(sc->sc_ic);
    803 		return;
    804 	}
    805 fatalerror:
    806 	/*
    807 	 * If anything fails, the drive is probably hosed, so mark it not
    808 	 * "ALIVE" (but it EXISTS and is OPEN or we wouldn't be here, and
    809 	 * if, last we heard, it was REWinding, remember that).
    810 	 */
    811 	sc->sc_flags &= MTF_EXISTS | MTF_OPEN | MTF_REW;
    812 	bp->b_error = EIO;
    813 done:
    814 	sc->sc_flags &= ~(MTF_HITEOF | MTF_HITBOF);
    815 	(void)bufq_get(sc->sc_tab);
    816 	biodone(bp);
    817 	gpibrelease(sc->sc_ic, sc->sc_hdl);
    818 	if ((bp = bufq_peek(sc->sc_tab)) == NULL)
    819 		sc->sc_active = 0;
    820 	else
    821 		mtustart(sc);
    822 }
    823 
    824 void
    825 mtintr(struct mt_softc *sc)
    826 {
    827 	struct buf *bp;
    828 	int slave, dir, i;
    829 	u_char cmdbuf[4];
    830 
    831 	slave = sc->sc_slave;
    832 
    833 	bp = bufq_peek(sc->sc_tab);
    834 	if (bp == NULL) {
    835 		printf("%s intr: bp == NULL", device_xname(&sc->sc_dev));
    836 		return;
    837 	}
    838 
    839 	DPRINTF(MDB_ANY, ("%s intr", device_xname(&sc->sc_dev)));
    840 
    841 	/*
    842 	 * Some operation completed.  Read status bytes and report errors.
    843 	 * Clear EOF flags here `cause they're set once on specific conditions
    844 	 * below when a command succeeds.
    845 	 * A DSJ of 2 always means keep waiting.  If the command was READ
    846 	 * (and we're in data DMA phase) stop data transfer first.
    847 	 */
    848 	sc->sc_flags &= ~(MTF_HITEOF | MTF_HITBOF);
    849 	if ((bp->b_flags & (B_CMD|B_READ)) == B_READ &&
    850 	    !(sc->sc_flags & (MTF_IO | MTF_STATTIMEO | MTF_DSJTIMEO))){
    851 		cmdbuf[0] = MTE_STOP;
    852 		(void) gpibsend(sc->sc_ic, slave, MTL_ECMD,cmdbuf,1);
    853 	}
    854 	switch (mtreaddsj(sc, 0)) {
    855 	    case 0:
    856 		break;
    857 
    858 	    case 1:
    859 		/*
    860 		 * If we're in the middle of a READ/WRITE and have yet to
    861 		 * start the data transfer, a DSJ of one should terminate it.
    862 		 */
    863 		sc->sc_flags &= ~MTF_IO;
    864 		break;
    865 
    866 	    case 2:
    867 		(void) gpibawait(sc->sc_ic);
    868 		return;
    869 
    870 	    case -2:
    871 		/*
    872 		 * -2 means that the drive failed to respond quickly enough
    873 		 * to the request for DSJ.  It's probably just "busy" figuring
    874 		 * it out and will know in a little bit...
    875 		 */
    876 		callout_reset(&sc->sc_intr_ch, hz >> 5, mtintr_callout, sc);
    877 		return;
    878 
    879 	    default:
    880 		printf("%s intr: can't get drive stat", device_xname(&sc->sc_dev));
    881 		goto error;
    882 	}
    883 	if (sc->sc_stat1 & (SR1_ERR | SR1_REJECT)) {
    884 		i = sc->sc_stat4 & SR4_ERCLMASK;
    885 		printf("%s: %s error, retry %d, SR2/3 %x/%x, code %d",
    886 			device_xname(&sc->sc_dev), i == SR4_DEVICE ? "device" :
    887 			(i == SR4_PROTOCOL ? "protocol" :
    888 			(i == SR4_SELFTEST ? "selftest" : "unknown")),
    889 			sc->sc_stat4 & SR4_RETRYMASK, sc->sc_stat2,
    890 			sc->sc_stat3, sc->sc_stat5);
    891 
    892 		if ((bp->b_flags & B_CMD) && bp->b_cmd == MTRESET)
    893 			callout_stop(&sc->sc_intr_ch);
    894 		if (sc->sc_stat3 & SR3_POWERUP)
    895 			sc->sc_flags &= MTF_OPEN | MTF_EXISTS;
    896 		goto error;
    897 	}
    898 	/*
    899 	 * Report and clear any soft errors.
    900 	 */
    901 	if (sc->sc_stat1 & SR1_SOFTERR) {
    902 		printf("%s: soft error, retry %d\n", device_xname(&sc->sc_dev),
    903 		    sc->sc_stat4 & SR4_RETRYMASK);
    904 		sc->sc_stat1 &= ~SR1_SOFTERR;
    905 	}
    906 	/*
    907 	 * We've initiated a read or write, but haven't actually started to
    908 	 * DMA the data yet.  At this point, the drive's ready.
    909 	 */
    910 	if (sc->sc_flags & MTF_IO) {
    911 		sc->sc_flags &= ~MTF_IO;
    912 		dir = (bp->b_flags & B_READ ? GPIB_READ : GPIB_WRITE);
    913 		gpibxfer(sc->sc_ic, slave,
    914 		    dir == GPIB_READ ? MTT_READ : MTL_WRITE,
    915 		    bp->b_data, bp->b_bcount, dir, dir == GPIB_READ);
    916 		return;
    917 	}
    918 	/*
    919 	 * Check for End Of Tape - we're allowed to hit EOT and then write (or
    920 	 * read) one more record.  If we get here and have not already hit EOT,
    921 	 * return ENOSPC to inform the process that it's hit it.  If we get
    922 	 * here and HAVE already hit EOT, don't allow any more operations that
    923 	 * move the tape forward.
    924 	 */
    925 	if (sc->sc_stat1 & SR1_EOT) {
    926 		if (sc->sc_flags & MTF_ATEOT)
    927 			sc->sc_flags |= MTF_PASTEOT;
    928 		else {
    929 			bp->b_error = ENOSPC;
    930 			sc->sc_flags |= MTF_ATEOT;
    931 		}
    932 	}
    933 	/*
    934 	 * If a motion command was being executed, check for Tape Marks.
    935 	 * If we were doing data, make sure we got the right amount, and
    936 	 * check for hitting tape marks on reads.
    937 	 */
    938 	if (bp->b_flags & B_CMD) {
    939 		if (sc->sc_stat1 & SR1_EOF) {
    940 			if (bp->b_cmd == MTFSR)
    941 				sc->sc_flags |= MTF_HITEOF;
    942 			if (bp->b_cmd == MTBSR)
    943 				sc->sc_flags |= MTF_HITBOF;
    944 		}
    945 		if (bp->b_cmd == MTRESET) {
    946 			callout_stop(&sc->sc_intr_ch);
    947 			sc->sc_flags |= MTF_ALIVE;
    948 		}
    949 	} else {
    950 		i = gpibrecv(sc->sc_ic, slave, MTT_BCNT, cmdbuf, 2);
    951 		if (i != 2) {
    952 			aprint_error_dev(&sc->sc_dev, "intr: can't get xfer length\n");
    953 			goto error;
    954 		}
    955 		i = (int) *((u_short *) cmdbuf);
    956 		if (i <= bp->b_bcount) {
    957 			if (i == 0)
    958 				sc->sc_flags |= MTF_HITEOF;
    959 			bp->b_resid = bp->b_bcount - i;
    960 			DPRINTF(MDB_ANY, ("%s intr: bcount %ld, resid %ld",
    961 			    device_xname(&sc->sc_dev), bp->b_bcount, bp->b_resid));
    962 		} else {
    963 			tprintf(sc->sc_ttyp,
    964 				"%s: record (%d) larger than wanted (%d)\n",
    965 				device_xname(&sc->sc_dev), i, bp->b_bcount);
    966 error:
    967 			sc->sc_flags &= ~MTF_IO;
    968 			bp->b_error = EIO;
    969 		}
    970 	}
    971 	/*
    972 	 * The operation is completely done.
    973 	 * Let the drive know with an END command.
    974 	 */
    975 	cmdbuf[0] = MTE_COMPLETE | MTE_IDLE;
    976 	(void) gpibsend(sc->sc_ic, slave, MTL_ECMD, cmdbuf, 1);
    977 	bp->b_flags &= ~B_CMD;
    978 	(void)bufq_get(sc->sc_tab);
    979 	biodone(bp);
    980 	gpibrelease(sc->sc_ic, sc->sc_hdl);
    981 	if (bufq_peek(sc->sc_tab) == NULL)
    982 		sc->sc_active = 0;
    983 	else
    984 		mtustart(sc);
    985 }
    986 
    987 int
    988 mtread(dev_t dev, struct uio *uio, int flags)
    989 {
    990 	struct mt_softc *sc;
    991 
    992 	sc = device_lookup_private(&mt_cd, MTUNIT(dev));
    993 
    994 	return (physio(mtstrategy, &sc->sc_bufstore,
    995 	    dev, B_READ, minphys, uio));
    996 }
    997 
    998 int
    999 mtwrite(dev_t dev, struct uio *uio, int flags)
   1000 {
   1001 	struct mt_softc *sc;
   1002 
   1003 	sc = device_lookup_private(&mt_cd, MTUNIT(dev));
   1004 
   1005 	return (physio(mtstrategy, &sc->sc_bufstore,
   1006 	    dev, B_WRITE, minphys, uio));
   1007 }
   1008 
   1009 int
   1010 mtioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
   1011 {
   1012 	struct mtop *op;
   1013 	int cnt;
   1014 
   1015 	switch (cmd) {
   1016 	    case MTIOCTOP:
   1017 		op = (struct mtop *)data;
   1018 		switch(op->mt_op) {
   1019 		    case MTWEOF:
   1020 		    case MTFSF:
   1021 		    case MTBSR:
   1022 		    case MTBSF:
   1023 		    case MTFSR:
   1024 			cnt = op->mt_count;
   1025 			break;
   1026 
   1027 		    case MTOFFL:
   1028 		    case MTREW:
   1029 		    case MTNOP:
   1030 			cnt = 0;
   1031 			break;
   1032 
   1033 		    default:
   1034 			return (EINVAL);
   1035 		}
   1036 		return (mtcommand(dev, op->mt_op, cnt));
   1037 
   1038 	    case MTIOCGET:
   1039 		break;
   1040 
   1041 	    default:
   1042 		return (EINVAL);
   1043 	}
   1044 	return (0);
   1045 }
   1046