xref: /original-bsd/sys/vax/uba/tm.c (revision de38840f)
1 /*	tm.c	4.40	81/07/09	*/
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 #endif
139 	((struct device *)reg)->tmcs = TM_IE;
140 	/*
141 	 * If this is a tm11, it ought to have interrupted
142 	 * by now, if it isn't (ie: it is a ts04) then we just
143 	 * hope that it didn't interrupt, so autoconf will ignore it.
144 	 * Just in case, we will reference one
145 	 * of the more distant registers, and hope for a machine
146 	 * check, or similar disaster if this is a ts.
147 	 *
148 	 * Note: on an 11/780, badaddr will just generate
149 	 * a uba error for a ts; but our caller will notice that
150 	 * so we won't check for it.
151 	 */
152 	if (badaddr((caddr_t)&((struct device *)reg)->tmrd, 2))
153 		return (0);
154 	return (1);
155 }
156 
157 /*
158  * Due to a design flaw, we cannot ascertain if the tape
159  * exists or not unless it is on line - ie: unless a tape is
160  * mounted. This is too servere a restriction to bear,
161  * so all units are assumed to exist.
162  */
163 /*ARGSUSED*/
164 tmslave(ui, reg)
165 	struct uba_device *ui;
166 	caddr_t reg;
167 {
168 
169 	return (1);
170 }
171 
172 /*
173  * Record attachment of the unit to the controller.
174  */
175 /*ARGSUSED*/
176 tmattach(ui)
177 	struct uba_device *ui;
178 {
179 
180 #if NTS > 0
181 	havetm = 1;
182 #endif
183 	/*
184 	 * Tetotm is used in TMUNIT to index the ctmbuf and rtmbuf
185 	 * arrays given a te unit number.
186 	 */
187 	tetotm[ui->ui_unit] = ui->ui_mi->um_ctlr;
188 }
189 
190 int	tmtimer();
191 /*
192  * Open the device.  Tapes are unique open
193  * devices, so we refuse if it is already open.
194  * We also check that a tape is available, and
195  * don't block waiting here; if you want to wait
196  * for a tape you should timeout in user code.
197  */
198 tmopen(dev, flag)
199 	dev_t dev;
200 	int flag;
201 {
202 	register int teunit;
203 	register struct uba_device *ui;
204 	register struct te_softc *sc;
205 	int olddens, dens;
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 		u.u_error = ENXIO;
211 		return;
212 	}
213 	olddens = sc->sc_dens;
214 	dens = TM_IE | TM_GO | (ui->ui_slave << 8);
215 	if ((minor(dev) & T_1600BPI) == 0)
216 		dens |= TM_D800;
217 	sc->sc_dens = dens;
218 get:
219 	tmcommand(dev, TM_SENSE, 1);
220 	if (sc->sc_erreg&TMER_SDWN) {
221 		sleep((caddr_t)&lbolt, PZERO+1);
222 		goto get;
223 	}
224 	sc->sc_dens = olddens;
225 	if ((sc->sc_erreg&(TMER_SELR|TMER_TUR)) != (TMER_SELR|TMER_TUR)) {
226 		uprintf("te%d: not online\n", teunit);
227 		u.u_error = EIO;
228 		return;
229 	}
230 	if ((flag&FWRITE) && (sc->sc_erreg&TMER_WRL)) {
231 		uprintf("te%d: no write ring\n", teunit);
232 		u.u_error = EIO;
233 		return;
234 	}
235 	if ((sc->sc_erreg&TMER_BOT) == 0 && (flag&FWRITE) &&
236 	    dens != sc->sc_dens) {
237 		uprintf("te%d: can't change density in mid-tape\n", teunit);
238 		u.u_error = EIO;
239 		return;
240 	}
241 	sc->sc_openf = 1;
242 	sc->sc_blkno = (daddr_t)0;
243 	sc->sc_nxrec = INF;
244 	sc->sc_lastiow = 0;
245 	sc->sc_dens = dens;
246 	(void) spl6();
247 	if (sc->sc_tact == 0) {
248 		sc->sc_timo = INF;
249 		sc->sc_tact = 1;
250 		timeout(tmtimer, (caddr_t)dev, 5*hz);
251 	}
252 	(void) spl0();
253 }
254 
255 /*
256  * Close tape device.
257  *
258  * If tape was open for writing or last operation was
259  * a write, then write two EOF's and backspace over the last one.
260  * Unless this is a non-rewinding special file, rewind the tape.
261  * Make the tape available to others.
262  */
263 tmclose(dev, flag)
264 	register dev_t dev;
265 	register flag;
266 {
267 	register struct te_softc *sc = &te_softc[TEUNIT(dev)];
268 
269 	if (flag == FWRITE || (flag&FWRITE) && sc->sc_lastiow) {
270 		tmcommand(dev, TM_WEOF, 1);
271 		tmcommand(dev, TM_WEOF, 1);
272 		tmcommand(dev, TM_SREV, 1);
273 	}
274 	if ((minor(dev)&T_NOREWIND) == 0)
275 		/*
276 		 * 0 count means don't hang waiting for rewind complete
277 		 * rather ctmbuf stays busy until the operation completes
278 		 * preventing further opens from completing by
279 		 * preventing a TM_SENSE from completing.
280 		 */
281 		tmcommand(dev, TM_REW, 0);
282 	sc->sc_openf = 0;
283 }
284 
285 /*
286  * Execute a command on the tape drive
287  * a specified number of times.
288  */
289 tmcommand(dev, com, count)
290 	dev_t dev;
291 	int com, count;
292 {
293 	register struct buf *bp;
294 
295 	bp = &ctmbuf[TMUNIT(dev)];
296 	(void) spl5();
297 	while (bp->b_flags&B_BUSY) {
298 		/*
299 		 * This special check is because B_BUSY never
300 		 * gets cleared in the non-waiting rewind case.
301 		 */
302 		if (bp->b_repcnt == 0 && (bp->b_flags&B_DONE))
303 			break;
304 		bp->b_flags |= B_WANTED;
305 		sleep((caddr_t)bp, PRIBIO);
306 	}
307 	bp->b_flags = B_BUSY|B_READ;
308 	(void) spl0();
309 	bp->b_dev = dev;
310 	bp->b_repcnt = -count;
311 	bp->b_command = com;
312 	bp->b_blkno = 0;
313 	tmstrategy(bp);
314 	/*
315 	 * In case of rewind from close, don't wait.
316 	 * This is the only case where count can be 0.
317 	 */
318 	if (count == 0)
319 		return;
320 	iowait(bp);
321 	if (bp->b_flags&B_WANTED)
322 		wakeup((caddr_t)bp);
323 	bp->b_flags &= B_ERROR;
324 }
325 
326 /*
327  * Queue a tape operation.
328  */
329 tmstrategy(bp)
330 	register struct buf *bp;
331 {
332 	int teunit = TEUNIT(bp->b_dev);
333 	register struct uba_ctlr *um;
334 	register struct buf *dp;
335 
336 	/*
337 	 * Put transfer at end of unit queue
338 	 */
339 	dp = &teutab[teunit];
340 	bp->av_forw = NULL;
341 	(void) spl5();
342 	um = tedinfo[teunit]->ui_mi;
343 	if (dp->b_actf == NULL) {
344 		dp->b_actf = bp;
345 		/*
346 		 * Transport not already active...
347 		 * put at end of controller queue.
348 		 */
349 		dp->b_forw = NULL;
350 		if (um->um_tab.b_actf == NULL)
351 			um->um_tab.b_actf = dp;
352 		else
353 			um->um_tab.b_actl->b_forw = dp;
354 		um->um_tab.b_actl = dp;
355 	} else
356 		dp->b_actl->av_forw = bp;
357 	dp->b_actl = bp;
358 	/*
359 	 * If the controller is not busy, get
360 	 * it going.
361 	 */
362 	if (um->um_tab.b_active == 0)
363 		tmstart(um);
364 	(void) spl0();
365 }
366 
367 /*
368  * Start activity on a tm controller.
369  */
370 tmstart(um)
371 	register struct uba_ctlr *um;
372 {
373 	register struct buf *bp, *dp;
374 	register struct device *addr = (struct device *)um->um_addr;
375 	register struct te_softc *sc;
376 	register struct uba_device *ui;
377 	int teunit, cmd;
378 	daddr_t blkno;
379 
380 	/*
381 	 * Look for an idle transport on the controller.
382 	 */
383 loop:
384 	if ((dp = um->um_tab.b_actf) == NULL)
385 		return;
386 	if ((bp = dp->b_actf) == NULL) {
387 		um->um_tab.b_actf = dp->b_forw;
388 		goto loop;
389 	}
390 	teunit = TEUNIT(bp->b_dev);
391 	ui = tedinfo[teunit];
392 	/*
393 	 * Record pre-transfer status (e.g. for TM_SENSE)
394 	 */
395 	sc = &te_softc[teunit];
396 	addr = (struct device *)um->um_addr;
397 	addr->tmcs = (ui->ui_slave << 8);
398 	sc->sc_dsreg = addr->tmcs;
399 	sc->sc_erreg = addr->tmer;
400 	sc->sc_resid = addr->tmbc;
401 	/*
402 	 * Default is that last command was NOT a write command;
403 	 * if we do a write command we will notice this in tmintr().
404 	 */
405 	sc->sc_lastiow = 0;
406 	if (sc->sc_openf < 0 || (addr->tmcs&TM_CUR) == 0) {
407 		/*
408 		 * Have had a hard error on a non-raw tape
409 		 * or the tape unit is now unavailable
410 		 * (e.g. taken off line).
411 		 */
412 		bp->b_flags |= B_ERROR;
413 		goto next;
414 	}
415 	if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) {
416 		/*
417 		 * Execute control operation with the specified count.
418 		 */
419 		if (bp->b_command == TM_SENSE)
420 			goto next;
421 		/*
422 		 * Set next state; give 5 minutes to complete
423 		 * rewind, or 10 seconds per iteration (minimum 60
424 		 * seconds and max 5 minutes) to complete other ops.
425 		 */
426 		if (bp->b_command == TM_REW) {
427 			um->um_tab.b_active = SREW;
428 			sc->sc_timo = 5 * 60;
429 		} else {
430 			um->um_tab.b_active = SCOM;
431 			sc->sc_timo = 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 (dbtofsb(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 (dbtofsb(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 = dbtofsb(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) == dbtofsb(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 < dbtofsb(bp->b_blkno)) {
499 		bp->b_command = TM_SFORW;
500 		addr->tmbc = blkno - dbtofsb(bp->b_blkno);
501 	} else {
502 		bp->b_command = TM_SREV;
503 		addr->tmbc = dbtofsb(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 device *addr = (struct device *)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 device *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 device *)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 		printf("te%d: hard error bn%d er=%b\n", minor(bp->b_dev)&03,
642 		    bp->b_blkno, sc->sc_erreg, TMER_BITS);
643 		bp->b_flags |= B_ERROR;
644 		goto opdone;
645 	}
646 	/*
647 	 * Advance tape control FSM.
648 	 */
649 ignoreerr:
650 	switch (state) {
651 
652 	case SIO:
653 		/*
654 		 * Read/write increments tape block number
655 		 */
656 		sc->sc_blkno++;
657 		goto opdone;
658 
659 	case SCOM:
660 		/*
661 		 * For forward/backward space record update current position.
662 		 */
663 		if (bp == &ctmbuf[TMUNIT(bp->b_dev)])
664 		switch (bp->b_command) {
665 
666 		case TM_SFORW:
667 			sc->sc_blkno -= bp->b_repcnt;
668 			break;
669 
670 		case TM_SREV:
671 			sc->sc_blkno += bp->b_repcnt;
672 			break;
673 		}
674 		goto opdone;
675 
676 	case SSEEK:
677 		sc->sc_blkno = dbtofsb(bp->b_blkno);
678 		goto opcont;
679 
680 	default:
681 		panic("tmintr");
682 	}
683 opdone:
684 	/*
685 	 * Reset error count and remove
686 	 * from device queue.
687 	 */
688 	um->um_tab.b_errcnt = 0;
689 	dp->b_actf = bp->av_forw;
690 	bp->b_resid = -addr->tmbc;
691 	ubadone(um);
692 	iodone(bp);
693 	/*
694 	 * Circulate slave to end of controller
695 	 * queue to give other slaves a chance.
696 	 */
697 	um->um_tab.b_actf = dp->b_forw;
698 	if (dp->b_actf) {
699 		dp->b_forw = NULL;
700 		if (um->um_tab.b_actf == NULL)
701 			um->um_tab.b_actf = dp;
702 		else
703 			um->um_tab.b_actl->b_forw = dp;
704 		um->um_tab.b_actl = dp;
705 	}
706 	if (um->um_tab.b_actf == 0)
707 		return;
708 opcont:
709 	tmstart(um);
710 }
711 
712 tmtimer(dev)
713 	int dev;
714 {
715 	register struct te_softc *sc = &te_softc[TEUNIT(dev)];
716 
717 	if (sc->sc_timo != INF && (sc->sc_timo -= 5) < 0) {
718 		printf("te%d: lost interrupt\n");
719 		sc->sc_timo = INF;
720 		(void) spl5();
721 		tmintr(TMUNIT(dev));
722 		(void) spl0();
723 	}
724 	timeout(tmtimer, (caddr_t)dev, 5*hz);
725 }
726 
727 tmseteof(bp)
728 	register struct buf *bp;
729 {
730 	register int teunit = TEUNIT(bp->b_dev);
731 	register struct device *addr =
732 	    (struct device *)tedinfo[teunit]->ui_addr;
733 	register struct te_softc *sc = &te_softc[teunit];
734 
735 	if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) {
736 		if (sc->sc_blkno > dbtofsb(bp->b_blkno)) {
737 			/* reversing */
738 			sc->sc_nxrec = dbtofsb(bp->b_blkno) - addr->tmbc;
739 			sc->sc_blkno = sc->sc_nxrec;
740 		} else {
741 			/* spacing forward */
742 			sc->sc_blkno = dbtofsb(bp->b_blkno) + addr->tmbc;
743 			sc->sc_nxrec = sc->sc_blkno - 1;
744 		}
745 		return;
746 	}
747 	/* eof on read */
748 	sc->sc_nxrec = dbtofsb(bp->b_blkno);
749 }
750 
751 tmread(dev)
752 	dev_t dev;
753 {
754 
755 	tmphys(dev);
756 	if (u.u_error)
757 		return;
758 	physio(tmstrategy, &rtmbuf[TMUNIT(dev)], dev, B_READ, minphys);
759 }
760 
761 tmwrite(dev)
762 	dev_t dev;
763 {
764 
765 	tmphys(dev);
766 	if (u.u_error)
767 		return;
768 	physio(tmstrategy, &rtmbuf[TMUNIT(dev)], dev, B_WRITE, minphys);
769 }
770 
771 /*
772  * Check that a raw device exists.
773  * If it does, set up sc_blkno and sc_nxrec
774  * so that the tape will appear positioned correctly.
775  */
776 tmphys(dev)
777 	dev_t dev;
778 {
779 	register int teunit = TEUNIT(dev);
780 	register daddr_t a;
781 	register struct te_softc *sc;
782 	register struct uba_device *ui;
783 
784 	if (teunit >= NTE || (ui=tedinfo[teunit]) == 0 || ui->ui_alive == 0) {
785 		u.u_error = ENXIO;
786 		return;
787 	}
788 	sc = &te_softc[teunit];
789 	a = dbtofsb(u.u_offset >> 9);
790 	sc->sc_blkno = a;
791 	sc->sc_nxrec = a + 1;
792 }
793 
794 tmreset(uban)
795 	int uban;
796 {
797 	register struct uba_ctlr *um;
798 	register tm11, teunit;
799 	register struct uba_device *ui;
800 	register struct buf *dp;
801 
802 	for (tm11 = 0; tm11 < NTM; tm11++) {
803 		if ((um = tmminfo[tm11]) == 0 || um->um_alive == 0 ||
804 		   um->um_ubanum != uban)
805 			continue;
806 		printf(" tm%d", tm11);
807 		um->um_tab.b_active = 0;
808 		um->um_tab.b_actf = um->um_tab.b_actl = 0;
809 		if (um->um_ubinfo) {
810 			printf("<%d>", (um->um_ubinfo>>28)&0xf);
811 			ubadone(um);
812 		}
813 		((struct device *)(um->um_addr))->tmcs = TM_DCLR;
814 		for (teunit = 0; teunit < NTE; teunit++) {
815 			if ((ui = tedinfo[teunit]) == 0 || ui->ui_mi != um ||
816 			    ui->ui_alive == 0)
817 				continue;
818 			dp = &teutab[teunit];
819 			dp->b_active = 0;
820 			dp->b_forw = 0;
821 			if (um->um_tab.b_actf == NULL)
822 				um->um_tab.b_actf = dp;
823 			else
824 				um->um_tab.b_actl->b_forw = dp;
825 			um->um_tab.b_actl = dp;
826 			if (te_softc[teunit].sc_openf > 0)
827 				te_softc[teunit].sc_openf = -1;
828 		}
829 		tmstart(um);
830 	}
831 }
832 
833 /*ARGSUSED*/
834 tmioctl(dev, cmd, addr, flag)
835 	caddr_t addr;
836 	dev_t dev;
837 {
838 	int teunit = TEUNIT(dev);
839 	register struct te_softc *sc = &te_softc[teunit];
840 	register struct buf *bp = &ctmbuf[TMUNIT(dev)];
841 	register callcount;
842 	int fcount;
843 	struct mtop mtop;
844 	struct mtget mtget;
845 	/* we depend of the values and order of the MT codes here */
846 	static tmops[] =
847 	   {TM_WEOF,TM_SFORW,TM_SREV,TM_SFORW,TM_SREV,TM_REW,TM_OFFL,TM_SENSE};
848 
849 	switch (cmd) {
850 		case MTIOCTOP:	/* tape operation */
851 		if (copyin((caddr_t)addr, (caddr_t)&mtop, sizeof(mtop))) {
852 			u.u_error = EFAULT;
853 			return;
854 		}
855 		switch(mtop.mt_op) {
856 		case MTWEOF:
857 			callcount = mtop.mt_count;
858 			fcount = 1;
859 			break;
860 		case MTFSF: case MTBSF:
861 			callcount = mtop.mt_count;
862 			fcount = INF;
863 			break;
864 		case MTFSR: case MTBSR:
865 			callcount = 1;
866 			fcount = mtop.mt_count;
867 			break;
868 		case MTREW: case MTOFFL: case MTNOP:
869 			callcount = 1;
870 			fcount = 1;
871 			break;
872 		default:
873 			u.u_error = ENXIO;
874 			return;
875 		}
876 		if (callcount <= 0 || fcount <= 0) {
877 			u.u_error = ENXIO;
878 			return;
879 		}
880 		while (--callcount >= 0) {
881 			tmcommand(dev, tmops[mtop.mt_op], fcount);
882 			if ((mtop.mt_op == MTFSR || mtop.mt_op == MTBSR) &&
883 			    bp->b_resid) {
884 				u.u_error = EIO;
885 				break;
886 			}
887 			if ((bp->b_flags&B_ERROR) || sc->sc_erreg&TMER_BOT)
888 				break;
889 		}
890 		geterror(bp);
891 		return;
892 	case MTIOCGET:
893 		mtget.mt_dsreg = sc->sc_dsreg;
894 		mtget.mt_erreg = sc->sc_erreg;
895 		mtget.mt_resid = sc->sc_resid;
896 		mtget.mt_type = MT_ISTM;
897 		if (copyout((caddr_t)&mtget, addr, sizeof(mtget)))
898 			u.u_error = EFAULT;
899 		return;
900 	default:
901 		u.u_error = ENXIO;
902 	}
903 }
904 
905 #define	DBSIZE	20
906 
907 tmdump()
908 {
909 	register struct uba_device *ui;
910 	register struct uba_regs *up;
911 	register struct device *addr;
912 	int blk, num;
913 	int start;
914 
915 	start = 0;
916 	num = maxfree;
917 #define	phys(a,b)	((b)((int)(a)&0x7fffffff))
918 	if (tedinfo[0] == 0)
919 		return (ENXIO);
920 	ui = phys(tedinfo[0], struct uba_device *);
921 	up = phys(ui->ui_hd, struct uba_hd *)->uh_physuba;
922 	ubainit(up);
923 	DELAY(1000000);
924 	addr = (struct device *)ui->ui_physaddr;
925 	tmwait(addr);
926 	addr->tmcs = TM_DCLR | TM_GO;
927 	while (num > 0) {
928 		blk = num > DBSIZE ? DBSIZE : num;
929 		tmdwrite(start, blk, addr, up);
930 		start += blk;
931 		num -= blk;
932 	}
933 	tmeof(addr);
934 	tmeof(addr);
935 	tmwait(addr);
936 	if (addr->tmcs&TM_ERR)
937 		return (EIO);
938 	addr->tmcs = TM_REW | TM_GO;
939 	tmwait(addr);
940 	return (0);
941 }
942 
943 tmdwrite(dbuf, num, addr, up)
944 	register dbuf, num;
945 	register struct device *addr;
946 	struct uba_regs *up;
947 {
948 	register struct pte *io;
949 	register int npf;
950 
951 	tmwait(addr);
952 	io = up->uba_map;
953 	npf = num+1;
954 	while (--npf != 0)
955 		 *(int *)io++ = (dbuf++ | (1<<UBAMR_DPSHIFT) | UBAMR_MRV);
956 	*(int *)io = 0;
957 	addr->tmbc = -(num*NBPG);
958 	addr->tmba = 0;
959 	addr->tmcs = TM_WCOM | TM_GO;
960 }
961 
962 tmwait(addr)
963 	register struct device *addr;
964 {
965 	register s;
966 
967 	do
968 		s = addr->tmcs;
969 	while ((s & TM_CUR) == 0);
970 }
971 
972 tmeof(addr)
973 	struct device *addr;
974 {
975 
976 	tmwait(addr);
977 	addr->tmcs = TM_WEOF | TM_GO;
978 }
979 #endif
980