xref: /netbsd-src/sys/dev/gpib/mt.c (revision deb6f0161a9109e7de9b519dc8dfb9478668dcdd)
1 /*	$NetBSD: mt.c,v 1.31 2017/10/28 04:53:56 riastradh 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 2017/10/28 04:53:56 riastradh 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@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 		    case -2:
632 			/*
633 			 * -2 means "timeout" reading DSJ, which is probably
634 			 * temporary.  This is considered OK when doing a NOP,
635 			 * but not otherwise.
636 			 */
637 			if (sc->sc_flags & (MTF_DSJTIMEO | MTF_STATTIMEO)) {
638 				callout_reset(&sc->sc_start_ch, hz >> 5,
639 				    mtstart_callout, sc);
640 				return;
641 			}
642 		    case 2:
643 			if (bp->b_cmd != MTNOP || !(bp->b_flags & B_CMD)) {
644 				bp->b_error = EBUSY;
645 				goto done;
646 			}
647 			goto done;
648 
649 		    default:
650 			goto fatalerror;
651 		}
652 	}
653 	if (bp->b_flags & B_CMD) {
654 		if (sc->sc_flags & MTF_PASTEOT) {
655 			switch(bp->b_cmd) {
656 			    case MTFSF:
657 			    case MTWEOF:
658 			    case MTFSR:
659 				bp->b_error = ENOSPC;
660 				goto done;
661 
662 			    case MTBSF:
663 			    case MTOFFL:
664 			    case MTBSR:
665 			    case MTREW:
666 				sc->sc_flags &= ~(MTF_PASTEOT | MTF_ATEOT);
667 				break;
668 			}
669 		}
670 		switch(bp->b_cmd) {
671 		    case MTFSF:
672 			if (sc->sc_flags & MTF_HITEOF)
673 				goto done;
674 			cmdbuf[0] = MTTC_FSF;
675 			break;
676 
677 		    case MTBSF:
678 			if (sc->sc_flags & MTF_HITBOF)
679 				goto done;
680 			cmdbuf[0] = MTTC_BSF;
681 			break;
682 
683 		    case MTOFFL:
684 			sc->sc_flags |= MTF_REW;
685 			cmdbuf[0] = MTTC_REWOFF;
686 			break;
687 
688 		    case MTWEOF:
689 			cmdbuf[0] = MTTC_WFM;
690 			break;
691 
692 		    case MTBSR:
693 			cmdbuf[0] = MTTC_BSR;
694 			break;
695 
696 		    case MTFSR:
697 			cmdbuf[0] = MTTC_FSR;
698 			break;
699 
700 		    case MTREW:
701 			sc->sc_flags |= MTF_REW;
702 			cmdbuf[0] = MTTC_REW;
703 			break;
704 
705 		    case MTNOP:
706 			/*
707 			 * NOP is supposed to set status bits.
708 			 * Force readdsj to do it.
709 			 */
710 			switch (mtreaddsj(sc,
711 			  MTE_DSJ_FORCE | MTE_COMPLETE | MTE_IDLE)) {
712 			    default:
713 				goto done;
714 
715 			    case -1:
716 				/*
717 				 * If this fails, perform a device clear
718 				 * to fix any protocol problems and (most
719 				 * likely) get the status.
720 				 */
721 				bp->b_cmd = MTRESET;
722 				break;
723 
724 			    case -2:
725 				callout_reset(&sc->sc_start_ch, hz >> 5,
726 				    mtstart_callout, sc);
727 				return;
728 			}
729 
730 		    case MTRESET:
731 			/*
732 			 * 1) selected device clear (send with "-2" secondary)
733 			 * 2) set timeout, then wait for "service request"
734 			 * 3) interrupt will read DSJ (and END COMPLETE-IDLE)
735 			 */
736 			if (gpibsend(sc->sc_ic, sc->sc_slave, -2, NULL, 0)){
737 				aprint_error_dev(sc->sc_dev, "can't reset");
738 				goto fatalerror;
739 			}
740 			callout_reset(&sc->sc_intr_ch, 4*hz, mtintr_callout,
741 			    sc);
742 			gpibawait(sc->sc_ic);
743 			return;
744 
745 		    case MTSET800BPI:
746 			cmdbuf[0] = MTTC_800;
747 			break;
748 
749 		    case MTSET1600BPI:
750 			cmdbuf[0] = MTTC_1600;
751 			break;
752 
753 		    case MTSET6250BPI:
754 			cmdbuf[0] = MTTC_6250;
755 			break;
756 
757 		    case MTSET6250DC:
758 			cmdbuf[0] = MTTC_DC6250;
759 			break;
760 		}
761 	} else {
762 		if (sc->sc_flags & MTF_PASTEOT) {
763 			bp->b_error = ENOSPC;
764 			goto done;
765 		}
766 		if (bp->b_flags & B_READ) {
767 			sc->sc_flags |= MTF_IO;
768 			cmdbuf[0] = MTTC_READ;
769 		} else {
770 			sc->sc_flags |= MTF_WRT | MTF_IO;
771 			cmdbuf[0] = MTTC_WRITE;
772 			cmdbuf[1] = (bp->b_bcount +((1 << WRITE_BITS_IGNORED) - 1)) >> WRITE_BITS_IGNORED;
773 			cmdcount = 2;
774 		}
775 	}
776 	if (gpibsend(sc->sc_ic, sc->sc_slave, MTL_TCMD, cmdbuf, cmdcount)
777 	    == cmdcount) {
778 		if (sc->sc_flags & MTF_REW)
779 			goto done;
780 		gpibawait(sc->sc_ic);
781 		return;
782 	}
783 fatalerror:
784 	/*
785 	 * If anything fails, the drive is probably hosed, so mark it not
786 	 * "ALIVE" (but it EXISTS and is OPEN or we wouldn't be here, and
787 	 * if, last we heard, it was REWinding, remember that).
788 	 */
789 	sc->sc_flags &= MTF_EXISTS | MTF_OPEN | MTF_REW;
790 	bp->b_error = EIO;
791 done:
792 	sc->sc_flags &= ~(MTF_HITEOF | MTF_HITBOF);
793 	(void)bufq_get(sc->sc_tab);
794 	biodone(bp);
795 	gpibrelease(sc->sc_ic, sc->sc_hdl);
796 	if ((bp = bufq_peek(sc->sc_tab)) == NULL)
797 		sc->sc_active = 0;
798 	else
799 		mtustart(sc);
800 }
801 
802 void
803 mtintr(struct mt_softc *sc)
804 {
805 	struct buf *bp;
806 	int slave, dir, i;
807 	u_char cmdbuf[4];
808 
809 	slave = sc->sc_slave;
810 
811 	bp = bufq_peek(sc->sc_tab);
812 	if (bp == NULL) {
813 		printf("%s intr: bp == NULL", device_xname(sc->sc_dev));
814 		return;
815 	}
816 
817 	DPRINTF(MDB_ANY, ("%s intr", device_xname(sc->sc_dev)));
818 
819 	/*
820 	 * Some operation completed.  Read status bytes and report errors.
821 	 * Clear EOF flags here `cause they're set once on specific conditions
822 	 * below when a command succeeds.
823 	 * A DSJ of 2 always means keep waiting.  If the command was READ
824 	 * (and we're in data DMA phase) stop data transfer first.
825 	 */
826 	sc->sc_flags &= ~(MTF_HITEOF | MTF_HITBOF);
827 	if ((bp->b_flags & (B_CMD|B_READ)) == B_READ &&
828 	    !(sc->sc_flags & (MTF_IO | MTF_STATTIMEO | MTF_DSJTIMEO))){
829 		cmdbuf[0] = MTE_STOP;
830 		(void) gpibsend(sc->sc_ic, slave, MTL_ECMD,cmdbuf,1);
831 	}
832 	switch (mtreaddsj(sc, 0)) {
833 	    case 0:
834 		break;
835 
836 	    case 1:
837 		/*
838 		 * If we're in the middle of a READ/WRITE and have yet to
839 		 * start the data transfer, a DSJ of one should terminate it.
840 		 */
841 		sc->sc_flags &= ~MTF_IO;
842 		break;
843 
844 	    case 2:
845 		(void) gpibawait(sc->sc_ic);
846 		return;
847 
848 	    case -2:
849 		/*
850 		 * -2 means that the drive failed to respond quickly enough
851 		 * to the request for DSJ.  It's probably just "busy" figuring
852 		 * it out and will know in a little bit...
853 		 */
854 		callout_reset(&sc->sc_intr_ch, hz >> 5, mtintr_callout, sc);
855 		return;
856 
857 	    default:
858 		printf("%s intr: can't get drive stat",
859 		    device_xname(sc->sc_dev));
860 		goto error;
861 	}
862 	if (sc->sc_stat1 & (SR1_ERR | SR1_REJECT)) {
863 		i = sc->sc_stat4 & SR4_ERCLMASK;
864 		printf("%s: %s error, retry %d, SR2/3 %x/%x, code %d",
865 			device_xname(sc->sc_dev), i == SR4_DEVICE ? "device" :
866 			(i == SR4_PROTOCOL ? "protocol" :
867 			(i == SR4_SELFTEST ? "selftest" : "unknown")),
868 			sc->sc_stat4 & SR4_RETRYMASK, sc->sc_stat2,
869 			sc->sc_stat3, sc->sc_stat5);
870 
871 		if ((bp->b_flags & B_CMD) && bp->b_cmd == MTRESET)
872 			callout_stop(&sc->sc_intr_ch);
873 		if (sc->sc_stat3 & SR3_POWERUP)
874 			sc->sc_flags &= MTF_OPEN | MTF_EXISTS;
875 		goto error;
876 	}
877 	/*
878 	 * Report and clear any soft errors.
879 	 */
880 	if (sc->sc_stat1 & SR1_SOFTERR) {
881 		printf("%s: soft error, retry %d\n", device_xname(sc->sc_dev),
882 		    sc->sc_stat4 & SR4_RETRYMASK);
883 		sc->sc_stat1 &= ~SR1_SOFTERR;
884 	}
885 	/*
886 	 * We've initiated a read or write, but haven't actually started to
887 	 * DMA the data yet.  At this point, the drive's ready.
888 	 */
889 	if (sc->sc_flags & MTF_IO) {
890 		sc->sc_flags &= ~MTF_IO;
891 		dir = (bp->b_flags & B_READ ? GPIB_READ : GPIB_WRITE);
892 		gpibxfer(sc->sc_ic, slave,
893 		    dir == GPIB_READ ? MTT_READ : MTL_WRITE,
894 		    bp->b_data, bp->b_bcount, dir, dir == GPIB_READ);
895 		return;
896 	}
897 	/*
898 	 * Check for End Of Tape - we're allowed to hit EOT and then write (or
899 	 * read) one more record.  If we get here and have not already hit EOT,
900 	 * return ENOSPC to inform the process that it's hit it.  If we get
901 	 * here and HAVE already hit EOT, don't allow any more operations that
902 	 * move the tape forward.
903 	 */
904 	if (sc->sc_stat1 & SR1_EOT) {
905 		if (sc->sc_flags & MTF_ATEOT)
906 			sc->sc_flags |= MTF_PASTEOT;
907 		else {
908 			bp->b_error = ENOSPC;
909 			sc->sc_flags |= MTF_ATEOT;
910 		}
911 	}
912 	/*
913 	 * If a motion command was being executed, check for Tape Marks.
914 	 * If we were doing data, make sure we got the right amount, and
915 	 * check for hitting tape marks on reads.
916 	 */
917 	if (bp->b_flags & B_CMD) {
918 		if (sc->sc_stat1 & SR1_EOF) {
919 			if (bp->b_cmd == MTFSR)
920 				sc->sc_flags |= MTF_HITEOF;
921 			if (bp->b_cmd == MTBSR)
922 				sc->sc_flags |= MTF_HITBOF;
923 		}
924 		if (bp->b_cmd == MTRESET) {
925 			callout_stop(&sc->sc_intr_ch);
926 			sc->sc_flags |= MTF_ALIVE;
927 		}
928 	} else {
929 		i = gpibrecv(sc->sc_ic, slave, MTT_BCNT, cmdbuf, 2);
930 		if (i != 2) {
931 			aprint_error_dev(sc->sc_dev,
932 			    "intr: can't get xfer length\n");
933 			goto error;
934 		}
935 		i = (int) *((u_short *) cmdbuf);
936 		if (i <= bp->b_bcount) {
937 			if (i == 0)
938 				sc->sc_flags |= MTF_HITEOF;
939 			bp->b_resid = bp->b_bcount - i;
940 			DPRINTF(MDB_ANY, ("%s intr: bcount %d, resid %d",
941 			    device_xname(sc->sc_dev),
942 			    bp->b_bcount, bp->b_resid));
943 		} else {
944 			tprintf(sc->sc_ttyp,
945 				"%s: record (%d) larger than wanted (%d)\n",
946 				device_xname(sc->sc_dev), i, bp->b_bcount);
947 error:
948 			sc->sc_flags &= ~MTF_IO;
949 			bp->b_error = EIO;
950 		}
951 	}
952 	/*
953 	 * The operation is completely done.
954 	 * Let the drive know with an END command.
955 	 */
956 	cmdbuf[0] = MTE_COMPLETE | MTE_IDLE;
957 	(void) gpibsend(sc->sc_ic, slave, MTL_ECMD, cmdbuf, 1);
958 	bp->b_flags &= ~B_CMD;
959 	(void)bufq_get(sc->sc_tab);
960 	biodone(bp);
961 	gpibrelease(sc->sc_ic, sc->sc_hdl);
962 	if (bufq_peek(sc->sc_tab) == NULL)
963 		sc->sc_active = 0;
964 	else
965 		mtustart(sc);
966 }
967 
968 int
969 mtread(dev_t dev, struct uio *uio, int flags)
970 {
971 	return (physio(mtstrategy, NULL, dev, B_READ, minphys, uio));
972 }
973 
974 int
975 mtwrite(dev_t dev, struct uio *uio, int flags)
976 {
977 	return (physio(mtstrategy, NULL, dev, B_WRITE, minphys, uio));
978 }
979 
980 int
981 mtioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
982 {
983 	struct mtop *op;
984 	int cnt;
985 
986 	switch (cmd) {
987 	    case MTIOCTOP:
988 		op = (struct mtop *)data;
989 		switch(op->mt_op) {
990 		    case MTWEOF:
991 		    case MTFSF:
992 		    case MTBSR:
993 		    case MTBSF:
994 		    case MTFSR:
995 			cnt = op->mt_count;
996 			break;
997 
998 		    case MTOFFL:
999 		    case MTREW:
1000 		    case MTNOP:
1001 			cnt = 0;
1002 			break;
1003 
1004 		    default:
1005 			return (EINVAL);
1006 		}
1007 		return (mtcommand(dev, op->mt_op, cnt));
1008 
1009 	    case MTIOCGET:
1010 		break;
1011 
1012 	    default:
1013 		return (EINVAL);
1014 	}
1015 	return (0);
1016 }
1017