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