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