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