xref: /csrg-svn/sys/vax/uba/tm.c (revision 39938)
1 /*
2  * Copyright (c) 1982, 1986 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  *
6  *	@(#)tm.c	7.6 (Berkeley) 01/17/90
7  */
8 
9 #include "te.h"
10 #include "ts.h"
11 #if NTE > 0
12 /*
13  * TM11/TE10 tape driver
14  *
15  * TODO:
16  *	test driver with more than one controller
17  *	test reset code
18  *	what happens if you offline tape during rewind?
19  *	test using file system on tape
20  */
21 #include "param.h"
22 #include "systm.h"
23 #include "buf.h"
24 #include "dir.h"
25 #include "conf.h"
26 #include "user.h"
27 #include "file.h"
28 #include "map.h"
29 #include "vm.h"
30 #include "ioctl.h"
31 #include "mtio.h"
32 #include "cmap.h"
33 #include "uio.h"
34 #include "kernel.h"
35 #include "tty.h"
36 #include "syslog.h"
37 
38 #include "machine/pte.h"
39 #include "../vax/cpu.h"
40 #include "ubareg.h"
41 #include "ubavar.h"
42 #include "tmreg.h"
43 
44 /*
45  * There is a ctmbuf per tape controller.
46  * It is used as the token to pass to the internal routines
47  * to execute tape ioctls, and also acts as a lock on the slaves
48  * on the controller, since there is only one per controller.
49  * In particular, when the tape is rewinding on close we release
50  * the user process but any further attempts to use the tape drive
51  * before the rewind completes will hang waiting for ctmbuf.
52  */
53 struct	buf	ctmbuf[NTM];
54 
55 /*
56  * Driver unibus interface routines and variables.
57  */
58 int	tmprobe(), tmslave(), tmattach(), tmdgo(), tmintr();
59 struct	uba_ctlr *tmminfo[NTM];
60 struct	uba_device *tedinfo[NTE];
61 struct	buf teutab[NTE];
62 short	tetotm[NTE];
63 u_short	tmstd[] = { 0772520, 0 };
64 struct	uba_driver tmdriver =
65  { tmprobe, tmslave, tmattach, tmdgo, tmstd, "te", tedinfo, "tm", tmminfo, 0 };
66 
67 /* bits in minor device */
68 #define	TEUNIT(dev)	(minor(dev)&03)
69 #define	TMUNIT(dev)	(tetotm[TEUNIT(dev)])
70 #define	T_NOREWIND	04
71 #define	T_1600BPI	0x8
72 
73 #define	INF	(daddr_t)1000000L
74 
75 /*
76  * Software state per tape transport.
77  *
78  * 1. A tape drive is a unique-open device; we refuse opens when it is already.
79  * 2. We keep track of the current position on a block tape and seek
80  *    before operations by forward/back spacing if necessary.
81  * 3. We remember if the last operation was a write on a tape, so if a tape
82  *    is open read write and the last thing done is a write we can
83  *    write a standard end of tape mark (two eofs).
84  * 4. We remember the status registers after the last command, using
85  *    then internally and returning them to the SENSE ioctl.
86  * 5. We remember the last density the tape was used at.  If it is
87  *    not a BOT when we start using it and we are writing, we don't
88  *    let the density be changed.
89  */
90 struct	te_softc {
91 	char	sc_openf;	/* lock against multiple opens */
92 	char	sc_lastiow;	/* last op was a write */
93 	daddr_t	sc_blkno;	/* block number, for block device tape */
94 	daddr_t	sc_nxrec;	/* position of end of tape, if known */
95 	u_short	sc_erreg;	/* copy of last erreg */
96 	u_short	sc_ioerreg;	/* copy of last erreg for I/O command */
97 	u_short	sc_dsreg;	/* copy of last dsreg */
98 	short	sc_resid;	/* copy of last bc */
99 #ifdef unneeded
100 	short	sc_lastcmd;	/* last command to handle direction changes */
101 #endif
102 	u_short	sc_dens;	/* prototype command with density info */
103 	short	sc_tact;	/* timeout is active */
104 	daddr_t	sc_timo;	/* time until timeout expires */
105 	int	sc_blks;	/* number of I/O operations since open */
106 	int	sc_softerrs;	/* number of soft I/O errors since open */
107 	struct	tty *sc_ttyp;	/* record user's tty for errors */
108 } te_softc[NTE];
109 #ifdef unneeded
110 int	tmgapsdcnt;		/* DEBUG */
111 #endif
112 
113 /*
114  * States for um->um_tab.b_active, the per controller state flag.
115  * This is used to sequence control in the driver.
116  */
117 #define	SSEEK	1		/* seeking */
118 #define	SIO	2		/* doing seq i/o */
119 #define	SCOM	3		/* sending control command */
120 #define	SREW	4		/* sending a drive rewind */
121 
122 /*
123  * Determine if there is a controller for
124  * a tm at address reg.  Our goal is to make the
125  * device interrupt.
126  */
127 tmprobe(reg)
128 	caddr_t reg;
129 {
130 	register int br, cvec;		/* must be r11,r10; value-result */
131 
132 #ifdef lint
133 	br = 0; cvec = br; br = cvec;
134 	tmintr(0);
135 #endif
136 	((struct tmdevice *)reg)->tmcs = TM_IE;
137 	/*
138 	 * If this is a tm11, it ought to have interrupted
139 	 * by now, if it isn't (ie: it is a ts04) then we just
140 	 * hope that it didn't interrupt, so autoconf will ignore it.
141 	 * Just in case, we will reference one
142 	 * of the more distant registers, and hope for a machine
143 	 * check, or similar disaster if this is a ts.
144 	 *
145 	 * Note: on an 11/780, badaddr will just generate
146 	 * a uba error for a ts; but our caller will notice that
147 	 * so we won't check for it.
148 	 */
149 	if (badaddr((caddr_t)&((struct tmdevice *)reg)->tmrd, 2))
150 		return (0);
151 	return (sizeof (struct tmdevice));
152 }
153 
154 /*
155  * Due to a design flaw, we cannot ascertain if the tape
156  * exists or not unless it is on line - ie: unless a tape is
157  * mounted. This is too servere a restriction to bear,
158  * so all units are assumed to exist.
159  */
160 /*ARGSUSED*/
161 tmslave(ui, reg)
162 	struct uba_device *ui;
163 	caddr_t reg;
164 {
165 
166 	return (1);
167 }
168 
169 /*
170  * Record attachment of the unit to the controller.
171  */
172 /*ARGSUSED*/
173 tmattach(ui)
174 	struct uba_device *ui;
175 {
176 	/*
177 	 * Tetotm is used in TMUNIT to index the ctmbuf
178 	 * array given a te unit number.
179 	 */
180 	tetotm[ui->ui_unit] = ui->ui_mi->um_ctlr;
181 }
182 
183 int	tmtimer();
184 /*
185  * Open the device.  Tapes are unique open
186  * devices, so we refuse if it is already open.
187  * We also check that a tape is available, and
188  * don't block waiting here; if you want to wait
189  * for a tape you should timeout in user code.
190  */
191 
192 #ifdef AVIV
193 int tmdens[4] = { 0x6000, 0x0000, 0x2000, 0 };
194 #endif AVIV
195 int tmdiag;
196 
197 tmopen(dev, flag)
198 	dev_t dev;
199 	int flag;
200 {
201 	register int teunit;
202 	register struct uba_device *ui;
203 	register struct te_softc *sc;
204 	int olddens, dens;
205 	int s;
206 
207 	teunit = TEUNIT(dev);
208 	if (teunit>=NTE || (ui = tedinfo[teunit]) == 0 || ui->ui_alive == 0)
209 		return (ENXIO);
210 	if ((sc = &te_softc[teunit])->sc_openf)
211 		return (EBUSY);
212 	sc->sc_openf = 1;
213 	olddens = sc->sc_dens;
214 	dens = TM_IE | TM_GO | (ui->ui_slave << 8);
215 #ifndef AVIV
216 	if ((minor(dev) & T_1600BPI) == 0)
217 		dens |= TM_D800;
218 #else AVIV
219 	dens |= tmdens[(minor(dev)>>3)&03];
220 #endif AVIV
221 	sc->sc_dens = dens;
222 get:
223 	tmcommand(dev, TM_SENSE, 1);
224 	if (sc->sc_erreg&TMER_SDWN) {
225 		sleep((caddr_t)&lbolt, PZERO+1);
226 		goto get;
227 	}
228 	sc->sc_dens = olddens;
229 	if ((sc->sc_erreg&(TMER_SELR|TMER_TUR)) != (TMER_SELR|TMER_TUR)) {
230 		uprintf("te%d: not online\n", teunit);
231 		sc->sc_openf = 0;
232 		return (EIO);
233 	}
234 	if ((flag&FWRITE) && (sc->sc_erreg&TMER_WRL)) {
235 		uprintf("te%d: no write ring\n", teunit);
236 		sc->sc_openf = 0;
237 		return (EIO);
238 	}
239 	if ((sc->sc_erreg&TMER_BOT) == 0 && (flag&FWRITE) &&
240 	    dens != sc->sc_dens) {
241 		uprintf("te%d: can't change density in mid-tape\n", teunit);
242 		sc->sc_openf = 0;
243 		return (EIO);
244 	}
245 	sc->sc_blkno = (daddr_t)0;
246 	sc->sc_nxrec = INF;
247 	sc->sc_lastiow = 0;
248 	sc->sc_dens = dens;
249 	sc->sc_blks = 0;
250 	sc->sc_softerrs = 0;
251 	sc->sc_ttyp = u.u_ttyp;
252 	s = splclock();
253 	if (sc->sc_tact == 0) {
254 		sc->sc_timo = INF;
255 		sc->sc_tact = 1;
256 		timeout(tmtimer, (caddr_t)dev, 5*hz);
257 	}
258 	splx(s);
259 	return (0);
260 }
261 
262 /*
263  * Close tape device.
264  *
265  * If tape was open for writing or last operation was
266  * a write, then write two EOF's and backspace over the last one.
267  * Unless this is a non-rewinding special file, rewind the tape.
268  * Make the tape available to others.
269  */
270 tmclose(dev, flag)
271 	register dev_t dev;
272 	register flag;
273 {
274 	register struct te_softc *sc = &te_softc[TEUNIT(dev)];
275 
276 	if (flag == FWRITE || (flag&FWRITE) && sc->sc_lastiow) {
277 		tmcommand(dev, TM_WEOF, 1);
278 		tmcommand(dev, TM_WEOF, 1);
279 		tmcommand(dev, TM_SREV, 1);
280 	}
281 	if ((minor(dev)&T_NOREWIND) == 0)
282 		/*
283 		 * 0 count means don't hang waiting for rewind complete
284 		 * rather ctmbuf stays busy until the operation completes
285 		 * preventing further opens from completing by
286 		 * preventing a TM_SENSE from completing.
287 		 */
288 		tmcommand(dev, TM_REW, 0);
289 	if (sc->sc_blks > 100 && sc->sc_softerrs > sc->sc_blks / 100)
290 		log(LOG_INFO, "te%d: %d soft errors in %d blocks\n",
291 		    TEUNIT(dev), sc->sc_softerrs, sc->sc_blks);
292 	sc->sc_openf = 0;
293 	return (0);
294 }
295 
296 /*
297  * Execute a command on the tape drive
298  * a specified number of times.
299  */
300 tmcommand(dev, com, count)
301 	dev_t dev;
302 	int com, count;
303 {
304 	register struct buf *bp;
305 	register int s;
306 
307 	bp = &ctmbuf[TMUNIT(dev)];
308 	s = spl5();
309 	while (bp->b_flags&B_BUSY) {
310 		/*
311 		 * This special check is because B_BUSY never
312 		 * gets cleared in the non-waiting rewind case.
313 		 */
314 		if (bp->b_repcnt == 0 && (bp->b_flags&B_DONE))
315 			break;
316 		bp->b_flags |= B_WANTED;
317 		sleep((caddr_t)bp, PRIBIO);
318 	}
319 	bp->b_flags = B_BUSY|B_READ;
320 	splx(s);
321 	bp->b_dev = dev;
322 	bp->b_repcnt = -count;
323 	bp->b_command = com;
324 	bp->b_blkno = 0;
325 	tmstrategy(bp);
326 	/*
327 	 * In case of rewind from close, don't wait.
328 	 * This is the only case where count can be 0.
329 	 */
330 	if (count == 0)
331 		return;
332 	iowait(bp);
333 	if (bp->b_flags&B_WANTED)
334 		wakeup((caddr_t)bp);
335 	bp->b_flags &= B_ERROR;
336 }
337 
338 /*
339  * Queue a tape operation.
340  */
341 tmstrategy(bp)
342 	register struct buf *bp;
343 {
344 	int teunit = TEUNIT(bp->b_dev);
345 	register struct uba_ctlr *um;
346 	register struct buf *dp;
347 	int s;
348 
349 	/*
350 	 * Put transfer at end of unit queue
351 	 */
352 	dp = &teutab[teunit];
353 	bp->av_forw = NULL;
354 	s = spl5();
355 	um = tedinfo[teunit]->ui_mi;
356 	if (dp->b_actf == NULL) {
357 		dp->b_actf = bp;
358 		/*
359 		 * Transport not already active...
360 		 * put at end of controller queue.
361 		 */
362 		dp->b_forw = NULL;
363 		if (um->um_tab.b_actf == NULL)
364 			um->um_tab.b_actf = dp;
365 		else
366 			um->um_tab.b_actl->b_forw = dp;
367 		um->um_tab.b_actl = dp;
368 	} else
369 		dp->b_actl->av_forw = bp;
370 	dp->b_actl = bp;
371 	/*
372 	 * If the controller is not busy, get
373 	 * it going.
374 	 */
375 	if (um->um_tab.b_active == 0)
376 		tmstart(um);
377 	splx(s);
378 }
379 
380 /*
381  * Start activity on a tm controller.
382  */
383 tmstart(um)
384 	register struct uba_ctlr *um;
385 {
386 	register struct buf *bp, *dp;
387 	register struct tmdevice *addr = (struct tmdevice *)um->um_addr;
388 	register struct te_softc *sc;
389 	register struct uba_device *ui;
390 	int teunit, cmd;
391 	daddr_t blkno;
392 
393 	/*
394 	 * Look for an idle transport on the controller.
395 	 */
396 loop:
397 	if ((dp = um->um_tab.b_actf) == NULL)
398 		return;
399 	if ((bp = dp->b_actf) == NULL) {
400 		um->um_tab.b_actf = dp->b_forw;
401 		goto loop;
402 	}
403 	teunit = TEUNIT(bp->b_dev);
404 	ui = tedinfo[teunit];
405 	/*
406 	 * Record pre-transfer status (e.g. for TM_SENSE)
407 	 */
408 	sc = &te_softc[teunit];
409 	addr = (struct tmdevice *)um->um_addr;
410 	addr->tmcs = (ui->ui_slave << 8);
411 	sc->sc_dsreg = addr->tmcs;
412 	sc->sc_erreg = addr->tmer;
413 	sc->sc_resid = addr->tmbc;
414 	/*
415 	 * Default is that last command was NOT a write command;
416 	 * if we do a write command we will notice this in tmintr().
417 	 */
418 	sc->sc_lastiow = 0;
419 	if (sc->sc_openf < 0 || (addr->tmcs&TM_CUR) == 0) {
420 		/*
421 		 * Have had a hard error on a non-raw tape
422 		 * or the tape unit is now unavailable
423 		 * (e.g. taken off line).
424 		 */
425 		bp->b_flags |= B_ERROR;
426 		goto next;
427 	}
428 	if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) {
429 		/*
430 		 * Execute control operation with the specified count.
431 		 */
432 		if (bp->b_command == TM_SENSE)
433 			goto next;
434 		/*
435 		 * Set next state; give 5 minutes to complete
436 		 * rewind, or 10 seconds per iteration (minimum 60
437 		 * seconds and max 5 minutes) to complete other ops.
438 		 */
439 		if (bp->b_command == TM_REW) {
440 			um->um_tab.b_active = SREW;
441 			sc->sc_timo = 5 * 60;
442 		} else {
443 			um->um_tab.b_active = SCOM;
444 			sc->sc_timo =
445 			    imin(imax(10*(int)-bp->b_repcnt,60),5*60);
446 		}
447 		if (bp->b_command == TM_SFORW || bp->b_command == TM_SREV)
448 			addr->tmbc = bp->b_repcnt;
449 		goto dobpcmd;
450 	}
451 	/*
452 	 * For raw I/O, fudge the current block number
453 	 * so we don't seek except on a retry.
454 	 */
455 	if (bp->b_flags & B_RAW) {
456 		if (um->um_tab.b_errcnt == 0) {
457 			sc->sc_blkno = bdbtofsb(bp->b_blkno);
458 			sc->sc_nxrec = sc->sc_blkno + 1;
459 		}
460 	} else {
461 		/*
462 		 * Handle boundary cases for operation
463 		 * on non-raw tapes.
464 		 */
465 		if (bdbtofsb(bp->b_blkno) > sc->sc_nxrec) {
466 			/*
467 			 * Can't read past known end-of-file.
468 			 */
469 			bp->b_flags |= B_ERROR;
470 			bp->b_error = ENXIO;
471 			goto next;
472 		}
473 		if (bdbtofsb(bp->b_blkno) == sc->sc_nxrec &&
474 		    bp->b_flags&B_READ) {
475 			/*
476 			 * Reading at end of file returns 0 bytes.
477 			 */
478 			bp->b_resid = bp->b_bcount;
479 			clrbuf(bp);
480 			goto next;
481 		}
482 		if ((bp->b_flags&B_READ) == 0)
483 			/*
484 			 * Writing sets EOF
485 			 */
486 			sc->sc_nxrec = bdbtofsb(bp->b_blkno) + 1;
487 	}
488 	/*
489 	 * If the data transfer command is in the correct place,
490 	 * set up all the registers except the csr, and give
491 	 * control over to the UNIBUS adapter routines, to
492 	 * wait for resources to start the i/o.
493 	 */
494 	if ((blkno = sc->sc_blkno) == bdbtofsb(bp->b_blkno)) {
495 		addr->tmbc = -bp->b_bcount;
496 		if ((bp->b_flags&B_READ) == 0) {
497 			if (um->um_tab.b_errcnt &&
498 			    (sc->sc_ioerreg&(TMER_HARD|TMER_SOFT)) != TMER_BGL)
499 				cmd = TM_WIRG;
500 			else
501 				cmd = TM_WCOM;
502 		} else
503 			cmd = TM_RCOM;
504 		um->um_tab.b_active = SIO;
505 		um->um_cmd = sc->sc_dens|cmd;
506 #ifdef notdef
507 		if (tmreverseop(sc->sc_lastcmd))
508 			while (addr->tmer & TMER_SDWN)
509 				DELAY(10),tmgapsdcnt++;
510 		sc->sc_lastcmd = TM_RCOM;		/* will serve */
511 #endif
512 		sc->sc_timo = 60;	/* premature, but should serve */
513 		(void) ubago(ui);
514 		return;
515 	}
516 	/*
517 	 * Tape positioned incorrectly;
518 	 * set to seek forwards or backwards to the correct spot.
519 	 * This happens for raw tapes only on error retries.
520 	 */
521 	um->um_tab.b_active = SSEEK;
522 	if (blkno < bdbtofsb(bp->b_blkno)) {
523 		bp->b_command = TM_SFORW;
524 		addr->tmbc = blkno - bdbtofsb(bp->b_blkno);
525 	} else {
526 		bp->b_command = TM_SREV;
527 		addr->tmbc = bdbtofsb(bp->b_blkno) - blkno;
528 	}
529 	sc->sc_timo = imin(imax(10 * -addr->tmbc, 60), 5 * 60);
530 dobpcmd:
531 #ifdef notdef
532 	/*
533 	 * It is strictly necessary to wait for the tape
534 	 * to stop before changing directions, but the TC11
535 	 * handles this for us.
536 	 */
537 	if (tmreverseop(sc->sc_lastcmd) != tmreverseop(bp->b_command))
538 		while (addr->tmer & TM_SDWN)
539 			DELAY(10),tmgapsdcnt++;
540 	sc->sc_lastcmd = bp->b_command;
541 #endif
542 	/*
543 	 * Do the command in bp.
544 	 */
545 	addr->tmcs = (sc->sc_dens | bp->b_command);
546 	return;
547 
548 next:
549 	/*
550 	 * Done with this operation due to error or
551 	 * the fact that it doesn't do anything.
552 	 * Release UBA resources (if any), dequeue
553 	 * the transfer and continue processing this slave.
554 	 */
555 	if (um->um_ubinfo)
556 		ubadone(um);
557 	um->um_tab.b_errcnt = 0;
558 	dp->b_actf = bp->av_forw;
559 	iodone(bp);
560 	goto loop;
561 }
562 
563 /*
564  * The UNIBUS resources we needed have been
565  * allocated to us; start the device.
566  */
567 tmdgo(um)
568 	register struct uba_ctlr *um;
569 {
570 	register struct tmdevice *addr = (struct tmdevice *)um->um_addr;
571 
572 	addr->tmba = um->um_ubinfo;
573 	addr->tmcs = um->um_cmd | ((um->um_ubinfo >> 12) & 0x30);
574 }
575 
576 /*
577  * Tm interrupt routine.
578  */
579 /*ARGSUSED*/
580 tmintr(tm11)
581 	int tm11;
582 {
583 	struct buf *dp;
584 	register struct buf *bp;
585 	register struct uba_ctlr *um = tmminfo[tm11];
586 	register struct tmdevice *addr;
587 	register struct te_softc *sc;
588 	int teunit;
589 	register state;
590 
591 	if ((dp = um->um_tab.b_actf) == NULL)
592 		return;
593 	bp = dp->b_actf;
594 	teunit = TEUNIT(bp->b_dev);
595 	addr = (struct tmdevice *)tedinfo[teunit]->ui_addr;
596 	sc = &te_softc[teunit];
597 	/*
598 	 * If last command was a rewind, and tape is still
599 	 * rewinding, wait for the rewind complete interrupt.
600 	 */
601 	if (um->um_tab.b_active == SREW) {
602 		um->um_tab.b_active = SCOM;
603 		if (addr->tmer&TMER_RWS) {
604 			sc->sc_timo = 5*60;		/* 5 minutes */
605 			return;
606 		}
607 	}
608 	/*
609 	 * An operation completed... record status
610 	 */
611 	sc->sc_timo = INF;
612 	if (um->um_tab.b_active == SIO)
613 		sc->sc_ioerreg = addr->tmer;
614 	sc->sc_dsreg = addr->tmcs;
615 	sc->sc_erreg = addr->tmer;
616 	sc->sc_resid = addr->tmbc;
617 	if ((bp->b_flags & B_READ) == 0)
618 		sc->sc_lastiow = 1;
619 	state = um->um_tab.b_active;
620 	um->um_tab.b_active = 0;
621 	/*
622 	 * Check for errors.
623 	 */
624 	if (addr->tmcs&TM_ERR) {
625 		while (addr->tmer & TMER_SDWN)
626 			DELAY(10);			/* await settle down */
627 		/*
628 		 * If we hit the end of the tape file, update our position.
629 		 */
630 		if (addr->tmer&TMER_EOF) {
631 			tmseteof(bp);		/* set blkno and nxrec */
632 			state = SCOM;		/* force completion */
633 			/*
634 			 * Stuff bc so it will be unstuffed correctly
635 			 * later to get resid.
636 			 */
637 			addr->tmbc = -bp->b_bcount;
638 			goto opdone;
639 		}
640 		/*
641 		 * If we were reading raw tape and the only error was that the
642 		 * record was too long, then we don't consider this an error.
643 		 */
644 		if ((bp->b_flags & (B_READ|B_RAW)) == (B_READ|B_RAW) &&
645 		    (addr->tmer&(TMER_HARD|TMER_SOFT)) == TMER_RLE)
646 			goto ignoreerr;
647 		/*
648 		 * If error is not hard, and this was an i/o operation
649 		 * retry up to 8 times.
650 		 */
651 		if ((addr->tmer&TMER_HARD)==0 && state==SIO) {
652 			if (++um->um_tab.b_errcnt < 7) {
653 				if (tmdiag)
654 					log(LOG_DEBUG,
655 					    "te%d: soft error bn%d er=%b\n",
656 					    minor(bp->b_dev)&03,
657 					    bp->b_blkno, sc->sc_erreg,
658 					    TMER_BITS);
659 				sc->sc_blkno++;
660 				ubadone(um);
661 				goto opcont;
662 			}
663 		} else
664 			/*
665 			 * Hard or non-i/o errors on non-raw tape
666 			 * cause it to close.
667 			 */
668 			if ((bp->b_flags&B_RAW) == 0 && sc->sc_openf > 0)
669 				sc->sc_openf = -1;
670 		/*
671 		 * Couldn't recover error
672 		 */
673 		tprintf(sc->sc_ttyp,
674 		    "te%d: hard error bn%d er=%b\n", minor(bp->b_dev)&03,
675 		    bp->b_blkno, sc->sc_erreg, TMER_BITS);
676 #ifdef	AVIV
677 		if (tmdiag) {
678 			addr->tmmr = DAB;
679 			printf("reject code 0%o", addr->tmmr & DAB_MASK);
680 			addr->tmmr = DTS;
681 			if (addr->tmmr & DTS_MASK)
682 			    printf(", dead track 0%o", addr->tmmr & DTS_MASK);
683 			addr->tmmr = RWERR;
684 			printf(", read/write errors %b\n",
685 			    addr->tmmr & RWERR_MASK,
686 			    RWERR_BITS);
687 			addr->tmmr = DRSENSE;
688 			printf("drive sense %b, ", addr->tmmr & DRSENSE_MASK,
689 			    DRSENSE_BITS);
690 			printf("fsr %b\n", addr->tmfsr, FSR_BITS);
691 		}
692 #endif AVIV
693 		bp->b_flags |= B_ERROR;
694 		goto opdone;
695 	}
696 	/*
697 	 * Advance tape control FSM.
698 	 */
699 ignoreerr:
700 	switch (state) {
701 
702 	case SIO:
703 		/*
704 		 * Read/write increments tape block number
705 		 */
706 		sc->sc_blkno++;
707 		sc->sc_blks++;
708 		if (um->um_tab.b_errcnt)
709 			sc->sc_softerrs++;
710 		goto opdone;
711 
712 	case SCOM:
713 		/*
714 		 * For forward/backward space record update current position.
715 		 */
716 		if (bp == &ctmbuf[TMUNIT(bp->b_dev)])
717 		switch ((int)bp->b_command) {
718 
719 		case TM_SFORW:
720 			sc->sc_blkno -= bp->b_repcnt;
721 			break;
722 
723 		case TM_SREV:
724 			sc->sc_blkno += bp->b_repcnt;
725 			break;
726 		}
727 		goto opdone;
728 
729 	case SSEEK:
730 		sc->sc_blkno = bdbtofsb(bp->b_blkno);
731 		goto opcont;
732 
733 	default:
734 		panic("tmintr");
735 	}
736 opdone:
737 	/*
738 	 * Reset error count and remove
739 	 * from device queue.
740 	 */
741 	um->um_tab.b_errcnt = 0;
742 	dp->b_actf = bp->av_forw;
743 	/*
744 	 * Check resid; watch out for resid >32767 (tmbc not negative).
745 	 */
746 	bp->b_resid = ((int) -addr->tmbc) & 0xffff;
747 	ubadone(um);
748 	iodone(bp);
749 	/*
750 	 * Circulate slave to end of controller
751 	 * queue to give other slaves a chance.
752 	 */
753 	um->um_tab.b_actf = dp->b_forw;
754 	if (dp->b_actf) {
755 		dp->b_forw = NULL;
756 		if (um->um_tab.b_actf == NULL)
757 			um->um_tab.b_actf = dp;
758 		else
759 			um->um_tab.b_actl->b_forw = dp;
760 		um->um_tab.b_actl = dp;
761 	}
762 	if (um->um_tab.b_actf == 0)
763 		return;
764 opcont:
765 	tmstart(um);
766 }
767 
768 tmtimer(dev)
769 	int dev;
770 {
771 	register struct te_softc *sc = &te_softc[TEUNIT(dev)];
772 	register short x;
773 
774 	if (sc->sc_timo != INF && (sc->sc_timo -= 5) < 0) {
775 		printf("te%d: lost interrupt\n", TEUNIT(dev));
776 		sc->sc_timo = INF;
777 		x = spl5();
778 		tmintr(TMUNIT(dev));
779 		(void) splx(x);
780 	}
781 	timeout(tmtimer, (caddr_t)dev, 5*hz);
782 }
783 
784 tmseteof(bp)
785 	register struct buf *bp;
786 {
787 	register int teunit = TEUNIT(bp->b_dev);
788 	register struct tmdevice *addr =
789 	    (struct tmdevice *)tedinfo[teunit]->ui_addr;
790 	register struct te_softc *sc = &te_softc[teunit];
791 
792 	if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) {
793 		if (sc->sc_blkno > bdbtofsb(bp->b_blkno)) {
794 			/* reversing */
795 			sc->sc_nxrec = bdbtofsb(bp->b_blkno) - addr->tmbc;
796 			sc->sc_blkno = sc->sc_nxrec;
797 		} else {
798 			/* spacing forward */
799 			sc->sc_blkno = bdbtofsb(bp->b_blkno) + addr->tmbc;
800 			sc->sc_nxrec = sc->sc_blkno - 1;
801 		}
802 		return;
803 	}
804 	/* eof on read */
805 	sc->sc_nxrec = bdbtofsb(bp->b_blkno);
806 }
807 
808 tmreset(uban)
809 	int uban;
810 {
811 	register struct uba_ctlr *um;
812 	register tm11, teunit;
813 	register struct uba_device *ui;
814 	register struct buf *dp;
815 
816 	for (tm11 = 0; tm11 < NTM; tm11++) {
817 		if ((um = tmminfo[tm11]) == 0 || um->um_alive == 0 ||
818 		   um->um_ubanum != uban)
819 			continue;
820 		printf(" tm%d", tm11);
821 		um->um_tab.b_active = 0;
822 		um->um_tab.b_actf = um->um_tab.b_actl = 0;
823 		if (um->um_ubinfo) {
824 			printf("<%d>", (um->um_ubinfo>>28)&0xf);
825 			um->um_ubinfo = 0;
826 		}
827 		((struct tmdevice *)(um->um_addr))->tmcs = TM_DCLR;
828 		for (teunit = 0; teunit < NTE; teunit++) {
829 			if ((ui = tedinfo[teunit]) == 0 || ui->ui_mi != um ||
830 			    ui->ui_alive == 0)
831 				continue;
832 			dp = &teutab[teunit];
833 			dp->b_active = 0;
834 			dp->b_forw = 0;
835 			if (um->um_tab.b_actf == NULL)
836 				um->um_tab.b_actf = dp;
837 			else
838 				um->um_tab.b_actl->b_forw = dp;
839 			um->um_tab.b_actl = dp;
840 			if (te_softc[teunit].sc_openf > 0)
841 				te_softc[teunit].sc_openf = -1;
842 		}
843 		tmstart(um);
844 	}
845 }
846 
847 /*ARGSUSED*/
848 tmioctl(dev, cmd, data, flag)
849 	caddr_t data;
850 	dev_t dev;
851 {
852 	int teunit = TEUNIT(dev);
853 	register struct te_softc *sc = &te_softc[teunit];
854 	register struct buf *bp = &ctmbuf[TMUNIT(dev)];
855 	register callcount;
856 	int fcount;
857 	struct mtop *mtop;
858 	struct mtget *mtget;
859 	/* we depend of the values and order of the MT codes here */
860 	static tmops[] =
861 	   {TM_WEOF,TM_SFORW,TM_SREV,TM_SFORW,TM_SREV,TM_REW,TM_OFFL,TM_SENSE};
862 
863 	switch (cmd) {
864 
865 	case MTIOCTOP:	/* tape operation */
866 		mtop = (struct mtop *)data;
867 		switch (mtop->mt_op) {
868 
869 		case MTWEOF:
870 			callcount = mtop->mt_count;
871 			fcount = 1;
872 			break;
873 
874 		case MTFSF: case MTBSF:
875 			callcount = mtop->mt_count;
876 			fcount = INF;
877 			break;
878 
879 		case MTFSR: case MTBSR:
880 			callcount = 1;
881 			fcount = mtop->mt_count;
882 			break;
883 
884 		case MTREW: case MTOFFL: case MTNOP:
885 			callcount = 1;
886 			fcount = 1;
887 			break;
888 
889 		default:
890 			return (ENXIO);
891 		}
892 		if (callcount <= 0 || fcount <= 0)
893 			return (EINVAL);
894 		while (--callcount >= 0) {
895 			tmcommand(dev, tmops[mtop->mt_op], fcount);
896 			if ((mtop->mt_op == MTFSR || mtop->mt_op == MTBSR) &&
897 			    bp->b_resid)
898 				return (EIO);
899 			if ((bp->b_flags&B_ERROR) || sc->sc_erreg&TMER_BOT)
900 				break;
901 		}
902 		return (geterror(bp));
903 
904 	case MTIOCGET:
905 		mtget = (struct mtget *)data;
906 		mtget->mt_dsreg = sc->sc_dsreg;
907 		mtget->mt_erreg = sc->sc_erreg;
908 		mtget->mt_resid = sc->sc_resid;
909 		mtget->mt_type = MT_ISTM;
910 		break;
911 
912 	default:
913 		return (ENXIO);
914 	}
915 	return (0);
916 }
917 
918 #define	DBSIZE	20
919 
920 tmdump()
921 {
922 	register struct uba_device *ui;
923 	register struct uba_regs *up;
924 	register struct tmdevice *addr;
925 	int blk, num;
926 	int start;
927 
928 	start = 0;
929 	num = maxfree;
930 #define	phys(a,b)	((b)((int)(a)&0x7fffffff))
931 	if (tedinfo[0] == 0)
932 		return (ENXIO);
933 	ui = phys(tedinfo[0], struct uba_device *);
934 	up = phys(ui->ui_hd, struct uba_hd *)->uh_physuba;
935 	ubainit(up);
936 	DELAY(1000000);
937 	addr = (struct tmdevice *)ui->ui_physaddr;
938 	tmwait(addr);
939 	addr->tmcs = TM_DCLR | TM_GO;
940 	while (num > 0) {
941 		blk = num > DBSIZE ? DBSIZE : num;
942 		tmdwrite(start, blk, addr, up);
943 		start += blk;
944 		num -= blk;
945 	}
946 	tmeof(addr);
947 	tmeof(addr);
948 	tmwait(addr);
949 	if (addr->tmcs&TM_ERR)
950 		return (EIO);
951 	addr->tmcs = TM_REW | TM_GO;
952 	tmwait(addr);
953 	return (0);
954 }
955 
956 tmdwrite(dbuf, num, addr, up)
957 	register dbuf, num;
958 	register struct tmdevice *addr;
959 	struct uba_regs *up;
960 {
961 	register struct pte *io;
962 	register int npf;
963 
964 	tmwait(addr);
965 	io = up->uba_map;
966 	npf = num+1;
967 	while (--npf != 0)
968 		 *(int *)io++ = (dbuf++ | (1<<UBAMR_DPSHIFT) | UBAMR_MRV);
969 	*(int *)io = 0;
970 	addr->tmbc = -(num*NBPG);
971 	addr->tmba = 0;
972 	addr->tmcs = TM_WCOM | TM_GO;
973 }
974 
975 tmwait(addr)
976 	register struct tmdevice *addr;
977 {
978 	register s;
979 
980 	do
981 		s = addr->tmcs;
982 	while ((s & TM_CUR) == 0);
983 }
984 
985 tmeof(addr)
986 	struct tmdevice *addr;
987 {
988 
989 	tmwait(addr);
990 	addr->tmcs = TM_WEOF | TM_GO;
991 }
992 #endif
993