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