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