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