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