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