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