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