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