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