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