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