xref: /netbsd-src/sys/arch/hp300/dev/mt.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*	$NetBSD: mt.c,v 1.54 2014/07/25 08:10:33 dholland Exp $	*/
2 
3 /*-
4  * Copyright (c) 1996, 1997 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) 1992, The University of Utah and
34  * the Computer Systems Laboratory at the University of Utah (CSL).
35  * All rights reserved.
36  *
37  * Permission to use, copy, modify and distribute this software is hereby
38  * granted provided that (1) source code retains these copyright, permission,
39  * and disclaimer notices, and (2) redistributions including binaries
40  * reproduce the notices in supporting documentation, and (3) all advertising
41  * materials mentioning features or use of this software display the following
42  * acknowledgement: ``This product includes software developed by the
43  * Computer Systems Laboratory at the University of Utah.''
44  *
45  * THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS
46  * IS" CONDITION.  THE UNIVERSITY OF UTAH AND CSL DISCLAIM ANY LIABILITY OF
47  * ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
48  *
49  * CSL requests users of this software to return to csl-dist@cs.utah.edu any
50  * improvements that they make and grant CSL redistribution rights.
51  *
52  *	Utah $Hdr: mt.c 1.8 95/09/12$
53  */
54 /*	@(#)mt.c	3.9	90/07/10	mt Xinu
55  *
56  * Magnetic tape driver (7974a, 7978a/b, 7979a, 7980a, 7980xc)
57  * Original version contributed by Mt. Xinu.
58  * Modified for 4.4BSD by Mark Davies and Andrew Vignaux, Department of
59  * Computer Science, Victoria University of Wellington
60  */
61 
62 #include <sys/cdefs.h>
63 __KERNEL_RCSID(0, "$NetBSD: mt.c,v 1.54 2014/07/25 08:10:33 dholland Exp $");
64 
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/callout.h>
68 #include <sys/buf.h>
69 #include <sys/bufq.h>
70 #include <sys/ioctl.h>
71 #include <sys/mtio.h>
72 #include <sys/file.h>
73 #include <sys/proc.h>
74 #include <sys/errno.h>
75 #include <sys/syslog.h>
76 #include <sys/tty.h>
77 #include <sys/kernel.h>
78 #include <sys/tprintf.h>
79 #include <sys/device.h>
80 #include <sys/conf.h>
81 
82 #include <hp300/dev/hpibvar.h>
83 
84 #include <hp300/dev/mtreg.h>
85 
86 #include "ioconf.h"
87 
88 static const struct mtinfo {
89 	u_short	hwid;
90 	const char *desc;
91 } mtinfo[] = {
92 	{ MT7978ID,	"7978"	},
93 	{ MT7979AID,	"7979A"	},
94 	{ MT7980ID,	"7980"	},
95 	{ MT7974AID,	"7974A"	},
96 };
97 static const int nmtinfo = sizeof(mtinfo) / sizeof(mtinfo[0]);
98 
99 struct	mt_softc {
100 	device_t sc_dev;
101 	struct	callout sc_start_ch;
102 	struct	callout sc_intr_ch;
103 	int	sc_hpibno;	/* logical HPIB this slave it attached to */
104 	int	sc_slave;	/* HPIB slave address (0-6) */
105 	short	sc_flags;	/* see below */
106 	u_char	sc_lastdsj;	/* place for DSJ in mtreaddsj() */
107 	u_char	sc_lastecmd;	/* place for End Command in mtreaddsj() */
108 	short	sc_recvtimeo;	/* count of hpibsend timeouts to prevent hang */
109 	short	sc_statindex;	/* index for next sc_stat when MTF_STATTIMEO */
110 	struct	mt_stat sc_stat;/* status bytes last read from device */
111 	short	sc_density;	/* current density of tape (mtio.h format) */
112 	short	sc_type;	/* tape drive model (hardware IDs) */
113 	struct	hpibqueue sc_hq; /* HPIB device queue member */
114 	tpr_t	sc_ttyp;
115 	struct bufq_state *sc_tab;/* buf queue */
116 	int	sc_active;
117 };
118 
119 #ifdef DEBUG
120 int	mtdebug = 0;
121 #define	dlog	if (mtdebug) log
122 #else
123 #define	dlog	if (0) log
124 #endif
125 
126 #define	UNIT(x)		(minor(x) & 3)
127 
128 #define B_CMD		B_DEVPRIVATE	/* command buf instead of data */
129 #define	b_cmd		b_blkno		/* blkno holds cmd when B_CMD */
130 
131 static int	mtmatch(device_t, cfdata_t, void *);
132 static void	mtattach(device_t, device_t, void *);
133 
134 CFATTACH_DECL_NEW(mt, sizeof(struct mt_softc),
135     mtmatch, mtattach, NULL, NULL);
136 
137 static dev_type_open(mtopen);
138 static dev_type_close(mtclose);
139 static dev_type_read(mtread);
140 static dev_type_write(mtwrite);
141 static dev_type_ioctl(mtioctl);
142 static dev_type_strategy(mtstrategy);
143 
144 const struct bdevsw mt_bdevsw = {
145 	.d_open = mtopen,
146 	.d_close = mtclose,
147 	.d_strategy = mtstrategy,
148 	.d_ioctl = mtioctl,
149 	.d_dump = nodump,
150 	.d_psize = nosize,
151 	.d_discard = nodiscard,
152 	.d_flag = D_TAPE
153 };
154 
155 const struct cdevsw mt_cdevsw = {
156 	.d_open = mtopen,
157 	.d_close = mtclose,
158 	.d_read = mtread,
159 	.d_write = mtwrite,
160 	.d_ioctl = mtioctl,
161 	.d_stop = nostop,
162 	.d_tty = notty,
163 	.d_poll = nopoll,
164 	.d_mmap = nommap,
165 	.d_kqfilter = nokqfilter,
166 	.d_discard = nodiscard,
167 	.d_flag = D_TAPE
168 };
169 
170 static int	mtident(struct mt_softc *, struct hpibbus_attach_args *);
171 static void	mtustart(struct mt_softc *);
172 static int	mtreaddsj(struct mt_softc *, int);
173 static int	mtcommand(dev_t, int, int);
174 static void	spl_mtintr(void *);
175 static void	spl_mtstart(void *);
176 
177 static void	mtstart(void *);
178 static void	mtgo(void *);
179 static void	mtintr(void *);
180 
181 static int
182 mtmatch(device_t parent, cfdata_t cf, void *aux)
183 {
184 	struct hpibbus_attach_args *ha = aux;
185 
186 	return mtident(NULL, ha);
187 }
188 
189 static void
190 mtattach(device_t parent, device_t self, void *aux)
191 {
192 	struct mt_softc *sc = device_private(self);
193 	struct hpibbus_attach_args *ha = aux;
194 	int hpibno, slave;
195 
196 	sc->sc_dev = self;
197 	if (mtident(sc, ha) == 0) {
198 		aprint_error(": impossible!\n");
199 		return;
200 	}
201 
202 	hpibno = device_unit(parent);
203 	slave = ha->ha_slave;
204 
205 	bufq_alloc(&sc->sc_tab, "fcfs", 0);
206 	callout_init(&sc->sc_start_ch, 0);
207 	callout_init(&sc->sc_intr_ch, 0);
208 
209 	sc->sc_hpibno = hpibno;
210 	sc->sc_slave = slave;
211 	sc->sc_flags = MTF_EXISTS;
212 
213 	/* Initialize hpib job queue entry. */
214 	sc->sc_hq.hq_softc = sc;
215 	sc->sc_hq.hq_slave = sc->sc_slave;
216 	sc->sc_hq.hq_start = mtstart;
217 	sc->sc_hq.hq_go = mtgo;
218 	sc->sc_hq.hq_intr = mtintr;
219 }
220 
221 static int
222 mtident(struct mt_softc *sc, struct hpibbus_attach_args *ha)
223 {
224 	int i;
225 
226 	for (i = 0; i < nmtinfo; i++) {
227 		if (ha->ha_id == mtinfo[i].hwid) {
228 			if (sc != NULL) {
229 				sc->sc_type = mtinfo[i].hwid;
230 				aprint_normal(": %s tape\n", mtinfo[i].desc);
231 			}
232 			return 1;
233 		}
234 	}
235 	return 0;
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 static 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 = hpibrecv(sc->sc_hpibno,
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 		dlog(LOG_DEBUG, "%s can't hpibrecv 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 	dlog(LOG_DEBUG, "%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 		log(LOG_ERR, "%s readdsj: DSJ 0x%x\n", device_xname(sc->sc_dev),
284 		    sc->sc_lastdsj);
285 		return -1;
286 	}
287  getstats:
288 	retval = hpibrecv(sc->sc_hpibno,
289 	    (sc->sc_flags & MTF_STATCONT) ? -1 : sc->sc_slave,
290 	    MTT_STAT, ((char *)&(sc->sc_stat)) + sc->sc_statindex,
291 	    sizeof(sc->sc_stat) - sc->sc_statindex);
292 	sc->sc_flags &= ~(MTF_STATTIMEO | MTF_STATCONT);
293 	if (retval != sizeof(sc->sc_stat) - sc->sc_statindex) {
294 		if (sc->sc_recvtimeo == 0)
295 			sc->sc_recvtimeo = hz;
296 		if (--sc->sc_recvtimeo != 0) {
297 			if (retval >= 0) {
298 				sc->sc_statindex += retval;
299 				sc->sc_flags |= MTF_STATCONT;
300 			}
301 			sc->sc_flags |= MTF_STATTIMEO;
302 			return -2;
303 		}
304 		log(LOG_ERR, "%s readdsj: can't read status",
305 		    device_xname(sc->sc_dev));
306 		return -1;
307 	}
308 	sc->sc_recvtimeo = 0;
309 	sc->sc_statindex = 0;
310 	dlog(LOG_DEBUG, "%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) hpibsend(sc->sc_hpibno, sc->sc_slave,
316 		    MTL_ECMD, &(sc->sc_lastecmd), 1);
317 	return (int)sc->sc_lastdsj;
318 }
319 
320 static 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, UNIT(dev));
328 	if (sc == NULL)
329 		return ENXIO;
330 
331 	if ((sc->sc_flags & MTF_EXISTS) == 0)
332 		return ENXIO;
333 
334 	dlog(LOG_DEBUG, "%s open: flags 0x%x", device_xname(sc->sc_dev),
335 	    sc->sc_flags);
336 	if (sc->sc_flags & MTF_OPEN)
337 		return EBUSY;
338 	sc->sc_flags |= MTF_OPEN;
339 	sc->sc_ttyp = tprintf_open(l->l_proc);
340 	if ((sc->sc_flags & MTF_ALIVE) == 0) {
341 		error = mtcommand(dev, MTRESET, 0);
342 		if (error != 0 || (sc->sc_flags & MTF_ALIVE) == 0)
343 			goto errout;
344 		if ((sc->sc_stat1 & (SR1_BOT | SR1_ONLINE)) == SR1_ONLINE)
345 			(void) mtcommand(dev, MTREW, 0);
346 	}
347 	for (;;) {
348 		if ((error = mtcommand(dev, MTNOP, 0)) != 0)
349 			goto errout;
350 		if (!(sc->sc_flags & MTF_REW))
351 			break;
352 		error = kpause("mt", true, hz, NULL);
353 		if (error != 0 && error != EWOULDBLOCK) {
354 			error = EINTR;
355 			goto errout;
356 		}
357 	}
358 	if ((flag & FWRITE) && (sc->sc_stat1 & SR1_RO)) {
359 		error = EROFS;
360 		goto errout;
361 	}
362 	if (!(sc->sc_stat1 & SR1_ONLINE)) {
363 		uprintf("%s: not online\n", device_xname(sc->sc_dev));
364 		error = EIO;
365 		goto errout;
366 	}
367 	/*
368 	 * Select density:
369 	 *  - find out what density the drive is set to
370 	 *	(i.e. the density of the current tape)
371 	 *  - if we are going to write
372 	 *    - if we're not at the beginning of the tape
373 	 *      - complain if we want to change densities
374 	 *    - otherwise, select the mtcommand to set the density
375 	 *
376 	 * If the drive doesn't support it then don't change the recorded
377 	 * density.
378 	 *
379 	 * The original MOREbsd code had these additional conditions
380 	 * for the mid-tape change
381 	 *
382 	 *	req_den != T_BADBPI &&
383 	 *	sc->sc_density != T_6250BPI
384 	 *
385 	 * which suggests that it would be possible to write multiple
386 	 * densities if req_den == T_BAD_BPI or the current tape
387 	 * density was 6250.  Testing of our 7980 suggests that the
388 	 * device cannot change densities mid-tape.
389 	 *
390 	 * ajv@comp.vuw.ac.nz
391 	 */
392 	sc->sc_density = (sc->sc_stat2 & SR2_6250) ? T_6250BPI : (
393 			 (sc->sc_stat3 & SR3_1600) ? T_1600BPI : (
394 			 (sc->sc_stat3 & SR3_800) ? T_800BPI : -1));
395 	req_den = (dev & T_DENSEL);
396 
397 	if (flag & FWRITE) {
398 		if (!(sc->sc_stat1 & SR1_BOT)) {
399 			if (sc->sc_density != req_den) {
400 				uprintf("%s: can't change density mid-tape\n",
401 				    device_xname(sc->sc_dev));
402 				error = EIO;
403 				goto errout;
404 			}
405 		}
406 		else {
407 			int mtset_density =
408 			    (req_den == T_800BPI  ? MTSET800BPI : (
409 			     req_den == T_1600BPI ? MTSET1600BPI : (
410 			     req_den == T_6250BPI ? MTSET6250BPI : (
411 			     sc->sc_type == MT7980ID
412 						  ? MTSET6250DC
413 						  : MTSET6250BPI))));
414 			if (mtcommand(dev, mtset_density, 0) == 0)
415 				sc->sc_density = req_den;
416 		}
417 	}
418 	return 0;
419 errout:
420 	sc->sc_flags &= ~MTF_OPEN;
421 	return error;
422 }
423 
424 static int
425 mtclose(dev_t dev, int flag, int fmt, struct lwp *l)
426 {
427 	struct mt_softc *sc = device_lookup_private(&mt_cd,UNIT(dev));
428 
429 	if (sc->sc_flags & MTF_WRT) {
430 		(void) mtcommand(dev, MTWEOF, 2);
431 		(void) mtcommand(dev, MTBSF, 0);
432 	}
433 	if ((minor(dev) & T_NOREWIND) == 0)
434 		(void) mtcommand(dev, MTREW, 0);
435 	sc->sc_flags &= ~MTF_OPEN;
436 	tprintf_close(sc->sc_ttyp);
437 	return 0;
438 }
439 
440 static int
441 mtcommand(dev_t dev, int cmd, int cnt)
442 {
443 	int error = 0;
444 	buf_t *bp;
445 
446 	bp = getiobuf(NULL, true);
447 	bp->b_cmd = cmd;
448 	bp->b_dev = dev;
449 	do {
450 		bp->b_cflags = BC_BUSY;
451 		bp->b_flags = B_CMD;
452 		bp->b_oflags = 0;
453 		mtstrategy(bp);
454 		biowait(bp);
455 		if (bp->b_error != 0) {
456 			error = bp->b_error;
457 			break;
458 		}
459 	} while (--cnt > 0);
460 	putiobuf(bp);
461 
462 	return error;
463 }
464 
465 /*
466  * Only thing to check here is for legal record lengths (writes only).
467  */
468 static void
469 mtstrategy(struct buf *bp)
470 {
471 	struct mt_softc *sc;
472 	int s;
473 
474 	sc = device_lookup_private(&mt_cd,UNIT(bp->b_dev));
475 	dlog(LOG_DEBUG, "%s strategy", device_xname(sc->sc_dev));
476 	if ((bp->b_flags & (B_CMD | B_READ)) == 0) {
477 #define WRITE_BITS_IGNORED	8
478 #if 0
479 		if (bp->b_bcount & ((1 << WRITE_BITS_IGNORED) - 1)) {
480 			tprintf(sc->sc_ttyp,
481 			    "%s: write record must be multiple of %d\n",
482 			    device_xname(sc->sc_dev), 1 << WRITE_BITS_IGNORED);
483 			goto error;
484 		}
485 #endif
486 		s = 16 * 1024;
487 		if (sc->sc_stat2 & SR2_LONGREC) {
488 			switch (sc->sc_density) {
489 			    case T_1600BPI:
490 				s = 32 * 1024;
491 				break;
492 
493 			    case T_6250BPI:
494 			    case T_BADBPI:
495 				s = 60 * 1024;
496 				break;
497 			}
498 		}
499 		if (bp->b_bcount > s) {
500 			tprintf(sc->sc_ttyp,
501 			    "%s: write record (%d) too big: limit (%d)\n",
502 			    device_xname(sc->sc_dev), bp->b_bcount, s);
503 #if 0 /* XXX see above */
504 	    error:
505 #endif
506 			bp->b_error = EIO;
507 			biodone(bp);
508 			return;
509 		}
510 	}
511 	s = splbio();
512 	bufq_put(sc->sc_tab, bp);
513 	if (sc->sc_active == 0) {
514 		sc->sc_active = 1;
515 		mtustart(sc);
516 	}
517 	splx(s);
518 }
519 
520 static void
521 mtustart(struct mt_softc *sc)
522 {
523 
524 	dlog(LOG_DEBUG, "%s ustart", device_xname(sc->sc_dev));
525 	if (hpibreq(device_parent(sc->sc_dev), &sc->sc_hq))
526 		mtstart(sc);
527 }
528 
529 static void
530 spl_mtintr(void *arg)
531 {
532 	struct mt_softc *sc = arg;
533 	int s = splbio();
534 
535 	hpibppclear(sc->sc_hpibno);
536 	mtintr(sc);
537 	splx(s);
538 }
539 
540 static void
541 spl_mtstart(void *arg)
542 {
543 	int s = splbio();
544 
545 	mtstart(arg);
546 	splx(s);
547 }
548 
549 static void
550 mtstart(void *arg)
551 {
552 	struct mt_softc *sc = arg;
553 	struct buf *bp;
554 	short	cmdcount = 1;
555 	u_char	cmdbuf[2];
556 
557 	dlog(LOG_DEBUG, "%s start", device_xname(sc->sc_dev));
558 	sc->sc_flags &= ~MTF_WRT;
559 	bp = bufq_peek(sc->sc_tab);
560 	if ((sc->sc_flags & MTF_ALIVE) == 0 &&
561 	    ((bp->b_flags & B_CMD) == 0 || bp->b_cmd != MTRESET))
562 		goto fatalerror;
563 
564 	if (sc->sc_flags & MTF_REW) {
565 		if (!hpibpptest(sc->sc_hpibno, sc->sc_slave))
566 			goto stillrew;
567 		switch (mtreaddsj(sc, MTE_DSJ_FORCE|MTE_COMPLETE|MTE_IDLE)) {
568 		    case 0:
569 		    case 1:
570 		stillrew:
571 			if ((sc->sc_stat1 & SR1_BOT) ||
572 			    !(sc->sc_stat1 & SR1_ONLINE)) {
573 				sc->sc_flags &= ~MTF_REW;
574 				break;
575 			}
576 		    case -2:
577 			/*
578 			 * -2 means "timeout" reading DSJ, which is probably
579 			 * temporary.  This is considered OK when doing a NOP,
580 			 * but not otherwise.
581 			 */
582 			if (sc->sc_flags & (MTF_DSJTIMEO | MTF_STATTIMEO)) {
583 				callout_reset(&sc->sc_start_ch, hz >> 5,
584 				    spl_mtstart, sc);
585 				return;
586 			}
587 		    case 2:
588 			if (bp->b_cmd != MTNOP || !(bp->b_flags & B_CMD)) {
589 				bp->b_error = EBUSY;
590 				goto done;
591 			}
592 			goto done;
593 
594 		    default:
595 			goto fatalerror;
596 		}
597 	}
598 	if (bp->b_flags & B_CMD) {
599 		if (sc->sc_flags & MTF_PASTEOT) {
600 			switch(bp->b_cmd) {
601 			    case MTFSF:
602 			    case MTWEOF:
603 			    case MTFSR:
604 				bp->b_error = ENOSPC;
605 				goto done;
606 
607 			    case MTBSF:
608 			    case MTOFFL:
609 			    case MTBSR:
610 			    case MTREW:
611 				sc->sc_flags &= ~(MTF_PASTEOT | MTF_ATEOT);
612 				break;
613 			}
614 		}
615 		switch(bp->b_cmd) {
616 		    case MTFSF:
617 			if (sc->sc_flags & MTF_HITEOF)
618 				goto done;
619 			cmdbuf[0] = MTTC_FSF;
620 			break;
621 
622 		    case MTBSF:
623 			if (sc->sc_flags & MTF_HITBOF)
624 				goto done;
625 			cmdbuf[0] = MTTC_BSF;
626 			break;
627 
628 		    case MTOFFL:
629 			sc->sc_flags |= MTF_REW;
630 			cmdbuf[0] = MTTC_REWOFF;
631 			break;
632 
633 		    case MTWEOF:
634 			cmdbuf[0] = MTTC_WFM;
635 			break;
636 
637 		    case MTBSR:
638 			cmdbuf[0] = MTTC_BSR;
639 			break;
640 
641 		    case MTFSR:
642 			cmdbuf[0] = MTTC_FSR;
643 			break;
644 
645 		    case MTREW:
646 			sc->sc_flags |= MTF_REW;
647 			cmdbuf[0] = MTTC_REW;
648 			break;
649 
650 		    case MTNOP:
651 			/*
652 			 * NOP is supposed to set status bits.
653 			 * Force readdsj to do it.
654 			 */
655 			switch (mtreaddsj(sc,
656 			  MTE_DSJ_FORCE | MTE_COMPLETE | MTE_IDLE)) {
657 			    default:
658 				goto done;
659 
660 			    case -1:
661 				/*
662 				 * If this fails, perform a device clear
663 				 * to fix any protocol problems and (most
664 				 * likely) get the status.
665 				 */
666 				bp->b_cmd = MTRESET;
667 				break;
668 
669 			    case -2:
670 				callout_reset(&sc->sc_start_ch, hz >> 5,
671 				    spl_mtstart, sc);
672 				return;
673 			}
674 
675 		    case MTRESET:
676 			/*
677 			 * 1) selected device clear (send with "-2" secondary)
678 			 * 2) set timeout, then wait for "service request"
679 			 * 3) interrupt will read DSJ (and END COMPLETE-IDLE)
680 			 */
681 			if (hpibsend(sc->sc_hpibno, sc->sc_slave, -2, NULL, 0)){
682 				log(LOG_ERR, "%s can't reset",
683 				    device_xname(sc->sc_dev));
684 				goto fatalerror;
685 			}
686 			callout_reset(&sc->sc_intr_ch, 4 * hz, spl_mtintr, sc);
687 			hpibawait(sc->sc_hpibno);
688 			return;
689 
690 		    case MTSET800BPI:
691 			cmdbuf[0] = MTTC_800;
692 			break;
693 
694 		    case MTSET1600BPI:
695 			cmdbuf[0] = MTTC_1600;
696 			break;
697 
698 		    case MTSET6250BPI:
699 			cmdbuf[0] = MTTC_6250;
700 			break;
701 
702 		    case MTSET6250DC:
703 			cmdbuf[0] = MTTC_DC6250;
704 			break;
705 		}
706 	} else {
707 		if (sc->sc_flags & MTF_PASTEOT) {
708 			bp->b_error = ENOSPC;
709 			goto done;
710 		}
711 		if (bp->b_flags & B_READ) {
712 			sc->sc_flags |= MTF_IO;
713 			cmdbuf[0] = MTTC_READ;
714 		} else {
715 			sc->sc_flags |= MTF_WRT | MTF_IO;
716 			cmdbuf[0] = MTTC_WRITE;
717 			cmdbuf[1] = (bp->b_bcount + ((1 << WRITE_BITS_IGNORED) - 1)) >> WRITE_BITS_IGNORED;
718 			cmdcount = 2;
719 		}
720 	}
721 	if (hpibsend(sc->sc_hpibno, sc->sc_slave, MTL_TCMD, cmdbuf, cmdcount)
722 	    == cmdcount) {
723 		if (sc->sc_flags & MTF_REW)
724 			goto done;
725 		hpibawait(sc->sc_hpibno);
726 		return;
727 	}
728 fatalerror:
729 	/*
730 	 * If anything fails, the drive is probably hosed, so mark it not
731 	 * "ALIVE" (but it EXISTS and is OPEN or we wouldn't be here, and
732 	 * if, last we heard, it was REWinding, remember that).
733 	 */
734 	sc->sc_flags &= MTF_EXISTS | MTF_OPEN | MTF_REW;
735 	bp->b_error = EIO;
736 done:
737 	sc->sc_flags &= ~(MTF_HITEOF | MTF_HITBOF);
738 	(void)bufq_get(sc->sc_tab);
739 	biodone(bp);
740 	hpibfree(device_parent(sc->sc_dev), &sc->sc_hq);
741 	if ((bp = bufq_peek(sc->sc_tab)) == NULL)
742 		sc->sc_active = 0;
743 	else
744 		mtustart(sc);
745 }
746 
747 /*
748  * The Utah code had a bug which meant that the driver was unable to read.
749  * "rw" was initialized to bp->b_flags & B_READ before "bp" was initialized.
750  *   -- ajv@comp.vuw.ac.nz
751  */
752 static void
753 mtgo(void *arg)
754 {
755 	struct mt_softc *sc = arg;
756 	struct buf *bp;
757 	int rw;
758 
759 	dlog(LOG_DEBUG, "%s go", device_xname(sc->sc_dev));
760 	bp = bufq_peek(sc->sc_tab);
761 	rw = bp->b_flags & B_READ;
762 	hpibgo(sc->sc_hpibno, sc->sc_slave, rw ? MTT_READ : MTL_WRITE,
763 	    bp->b_data, bp->b_bcount, rw, rw != 0);
764 }
765 
766 static void
767 mtintr(void *arg)
768 {
769 	struct mt_softc *sc = arg;
770 	struct buf *bp;
771 	int i;
772 	u_char cmdbuf[4];
773 
774 	bp = bufq_peek(sc->sc_tab);
775 	if (bp == NULL) {
776 		log(LOG_ERR, "%s intr: bp == NULL", device_xname(sc->sc_dev));
777 		return;
778 	}
779 
780 	dlog(LOG_DEBUG, "%s intr", device_xname(sc->sc_dev));
781 
782 	/*
783 	 * Some operation completed.  Read status bytes and report errors.
784 	 * Clear EOF flags here `cause they're set once on specific conditions
785 	 * below when a command succeeds.
786 	 * A DSJ of 2 always means keep waiting.  If the command was READ
787 	 * (and we're in data DMA phase) stop data transfer first.
788 	 */
789 	sc->sc_flags &= ~(MTF_HITEOF | MTF_HITBOF);
790 	if ((bp->b_flags & (B_CMD|B_READ)) == B_READ &&
791 	    !(sc->sc_flags & (MTF_IO | MTF_STATTIMEO | MTF_DSJTIMEO))){
792 		cmdbuf[0] = MTE_STOP;
793 		(void) hpibsend(sc->sc_hpibno, sc->sc_slave, MTL_ECMD,cmdbuf,1);
794 	}
795 	switch (mtreaddsj(sc, 0)) {
796 	    case 0:
797 		break;
798 
799 	    case 1:
800 		/*
801 		 * If we're in the middle of a READ/WRITE and have yet to
802 		 * start the data transfer, a DSJ of one should terminate it.
803 		 */
804 		sc->sc_flags &= ~MTF_IO;
805 		break;
806 
807 	    case 2:
808 		(void) hpibawait(sc->sc_hpibno);
809 		return;
810 
811 	    case -2:
812 		/*
813 		 * -2 means that the drive failed to respond quickly enough
814 		 * to the request for DSJ.  It's probably just "busy" figuring
815 		 * it out and will know in a little bit...
816 		 */
817 		callout_reset(&sc->sc_intr_ch, hz >> 5, spl_mtintr, sc);
818 		return;
819 
820 	    default:
821 		log(LOG_ERR, "%s intr: can't get drive stat",
822 		    device_xname(sc->sc_dev));
823 		goto error;
824 	}
825 	if (sc->sc_stat1 & (SR1_ERR | SR1_REJECT)) {
826 		i = sc->sc_stat4 & SR4_ERCLMASK;
827 		log(LOG_ERR, "%s: %s error, retry %d, SR2/3 %x/%x, code %d",
828 			device_xname(sc->sc_dev), i == SR4_DEVICE ? "device" :
829 			(i == SR4_PROTOCOL ? "protocol" :
830 			(i == SR4_SELFTEST ? "selftest" : "unknown")),
831 			sc->sc_stat4 & SR4_RETRYMASK, sc->sc_stat2,
832 			sc->sc_stat3, sc->sc_stat5);
833 
834 		if ((bp->b_flags & B_CMD) && bp->b_cmd == MTRESET)
835 			callout_stop(&sc->sc_intr_ch);
836 		if (sc->sc_stat3 & SR3_POWERUP)
837 			sc->sc_flags &= MTF_OPEN | MTF_EXISTS;
838 		goto error;
839 	}
840 	/*
841 	 * Report and clear any soft errors.
842 	 */
843 	if (sc->sc_stat1 & SR1_SOFTERR) {
844 		log(LOG_WARNING, "%s: soft error, retry %d\n",
845 		    device_xname(sc->sc_dev), sc->sc_stat4 & SR4_RETRYMASK);
846 		sc->sc_stat1 &= ~SR1_SOFTERR;
847 	}
848 	/*
849 	 * We've initiated a read or write, but haven't actually started to
850 	 * DMA the data yet.  At this point, the drive's ready.
851 	 */
852 	if (sc->sc_flags & MTF_IO) {
853 		sc->sc_flags &= ~MTF_IO;
854 		if (hpibustart(sc->sc_hpibno))
855 			mtgo(sc);
856 		return;
857 	}
858 	/*
859 	 * Check for End Of Tape - we're allowed to hit EOT and then write (or
860 	 * read) one more record.  If we get here and have not already hit EOT,
861 	 * return ENOSPC to inform the process that it's hit it.  If we get
862 	 * here and HAVE already hit EOT, don't allow any more operations that
863 	 * move the tape forward.
864 	 */
865 	if (sc->sc_stat1 & SR1_EOT) {
866 		if (sc->sc_flags & MTF_ATEOT)
867 			sc->sc_flags |= MTF_PASTEOT;
868 		else {
869 			bp->b_error = ENOSPC;
870 			sc->sc_flags |= MTF_ATEOT;
871 		}
872 	}
873 	/*
874 	 * If a motion command was being executed, check for Tape Marks.
875 	 * If we were doing data, make sure we got the right amount, and
876 	 * check for hitting tape marks on reads.
877 	 */
878 	if (bp->b_flags & B_CMD) {
879 		if (sc->sc_stat1 & SR1_EOF) {
880 			if (bp->b_cmd == MTFSR)
881 				sc->sc_flags |= MTF_HITEOF;
882 			if (bp->b_cmd == MTBSR)
883 				sc->sc_flags |= MTF_HITBOF;
884 		}
885 		if (bp->b_cmd == MTRESET) {
886 			callout_stop(&sc->sc_intr_ch);
887 			sc->sc_flags |= MTF_ALIVE;
888 		}
889 	} else {
890 		i = hpibrecv(sc->sc_hpibno, sc->sc_slave, MTT_BCNT, cmdbuf, 2);
891 		if (i != 2) {
892 			log(LOG_ERR, "%s intr: can't get xfer length\n",
893 			    device_xname(sc->sc_dev));
894 			goto error;
895 		}
896 		i = (int) *((u_short *) cmdbuf);
897 		if (i <= bp->b_bcount) {
898 			if (i == 0)
899 				sc->sc_flags |= MTF_HITEOF;
900 			bp->b_resid = bp->b_bcount - i;
901 			dlog(LOG_DEBUG, "%s intr: bcount %d, resid %d",
902 			    device_xname(sc->sc_dev), bp->b_bcount,
903 			    bp->b_resid);
904 		} else {
905 			tprintf(sc->sc_ttyp,
906 				"%s: record (%d) larger than wanted (%d)\n",
907 				device_xname(sc->sc_dev), i, bp->b_bcount);
908  error:
909 			sc->sc_flags &= ~MTF_IO;
910 			bp->b_error = EIO;
911 		}
912 	}
913 	/*
914 	 * The operation is completely done.
915 	 * Let the drive know with an END command.
916 	 */
917 	cmdbuf[0] = MTE_COMPLETE | MTE_IDLE;
918 	(void) hpibsend(sc->sc_hpibno, sc->sc_slave, MTL_ECMD, cmdbuf, 1);
919 	bp->b_flags &= ~B_CMD;
920 	(void)bufq_get(sc->sc_tab);
921 	biodone(bp);
922 	hpibfree(device_parent(sc->sc_dev), &sc->sc_hq);
923 	if (bufq_peek(sc->sc_tab) == NULL)
924 		sc->sc_active = 0;
925 	else
926 		mtustart(sc);
927 }
928 
929 static int
930 mtread(dev_t dev, struct uio *uio, int flags)
931 {
932 
933 	return physio(mtstrategy, NULL, dev, B_READ, minphys, uio);
934 }
935 
936 static int
937 mtwrite(dev_t dev, struct uio *uio, int flags)
938 {
939 
940 	return physio(mtstrategy, NULL, dev, B_WRITE, minphys, uio);
941 }
942 
943 static int
944 mtioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
945 {
946 	struct mtop *op;
947 	int cnt;
948 
949 	switch (cmd) {
950 	    case MTIOCTOP:
951 		op = (struct mtop *)data;
952 		switch(op->mt_op) {
953 		    case MTWEOF:
954 		    case MTFSF:
955 		    case MTBSR:
956 		    case MTBSF:
957 		    case MTFSR:
958 			cnt = op->mt_count;
959 			break;
960 
961 		    case MTOFFL:
962 		    case MTREW:
963 		    case MTNOP:
964 			cnt = 0;
965 			break;
966 
967 		    default:
968 			return EINVAL;
969 		}
970 		return mtcommand(dev, op->mt_op, cnt);
971 
972 	    case MTIOCGET:
973 		break;
974 
975 	    default:
976 		return EINVAL;
977 	}
978 	return 0;
979 }
980