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