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