xref: /original-bsd/sys/vax/uba/tm.c (revision 18f6d767)
1 /*	tm.c	6.5	85/03/13	*/
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 "../machine/pte.h"
17 
18 #include "param.h"
19 #include "systm.h"
20 #include "buf.h"
21 #include "dir.h"
22 #include "conf.h"
23 #include "user.h"
24 #include "file.h"
25 #include "map.h"
26 #include "vm.h"
27 #include "ioctl.h"
28 #include "mtio.h"
29 #include "cmap.h"
30 #include "uio.h"
31 #include "kernel.h"
32 #include "tty.h"
33 
34 #include "../vax/cpu.h"
35 #include "ubareg.h"
36 #include "ubavar.h"
37 #include "tmreg.h"
38 
39 /*
40  * There is a ctmbuf per tape controller.
41  * It is used as the token to pass to the internal routines
42  * to execute tape ioctls, and also acts as a lock on the slaves
43  * on the controller, since there is only one per controller.
44  * In particular, when the tape is rewinding on close we release
45  * the user process but any further attempts to use the tape drive
46  * before the rewind completes will hang waiting for ctmbuf.
47  */
48 struct	buf	ctmbuf[NTM];
49 
50 /*
51  * Raw tape operations use rtmbuf.  The driver
52  * notices when rtmbuf is being used and allows the user
53  * program to continue after errors and read records
54  * not of the standard length (BSIZE).
55  */
56 struct	buf	rtmbuf[NTM];
57 
58 /*
59  * Driver unibus interface routines and variables.
60  */
61 int	tmprobe(), tmslave(), tmattach(), tmdgo(), tmintr();
62 struct	uba_ctlr *tmminfo[NTM];
63 struct	uba_device *tedinfo[NTE];
64 struct	buf teutab[NTE];
65 short	tetotm[NTE];
66 u_short	tmstd[] = { 0772520, 0 };
67 struct	uba_driver tmdriver =
68  { tmprobe, tmslave, tmattach, tmdgo, tmstd, "te", tedinfo, "tm", tmminfo, 0 };
69 
70 /* bits in minor device */
71 #define	TEUNIT(dev)	(minor(dev)&03)
72 #define	TMUNIT(dev)	(tetotm[TEUNIT(dev)])
73 #define	T_NOREWIND	04
74 #define	T_1600BPI	08
75 
76 #define	INF	(daddr_t)1000000L
77 
78 /*
79  * Software state per tape transport.
80  *
81  * 1. A tape drive is a unique-open device; we refuse opens when it is already.
82  * 2. We keep track of the current position on a block tape and seek
83  *    before operations by forward/back spacing if necessary.
84  * 3. We remember if the last operation was a write on a tape, so if a tape
85  *    is open read write and the last thing done is a write we can
86  *    write a standard end of tape mark (two eofs).
87  * 4. We remember the status registers after the last command, using
88  *    then internally and returning them to the SENSE ioctl.
89  * 5. We remember the last density the tape was used at.  If it is
90  *    not a BOT when we start using it and we are writing, we don't
91  *    let the density be changed.
92  */
93 struct	te_softc {
94 	char	sc_openf;	/* lock against multiple opens */
95 	char	sc_lastiow;	/* last op was a write */
96 	daddr_t	sc_blkno;	/* block number, for block device tape */
97 	daddr_t	sc_nxrec;	/* position of end of tape, if known */
98 	u_short	sc_erreg;	/* copy of last erreg */
99 	u_short	sc_dsreg;	/* copy of last dsreg */
100 	short	sc_resid;	/* copy of last bc */
101 #ifdef unneeded
102 	short	sc_lastcmd;	/* last command to handle direction changes */
103 #endif
104 	u_short	sc_dens;	/* prototype command with density info */
105 	short	sc_tact;	/* timeout is active */
106 	daddr_t	sc_timo;	/* time until timeout expires */
107 	struct	tty *sc_ttyp;	/* record user's tty for errors */
108 } te_softc[NTE];
109 #ifdef unneeded
110 int	tmgapsdcnt;		/* DEBUG */
111 #endif
112 
113 /*
114  * States for um->um_tab.b_active, the per controller state flag.
115  * This is used to sequence control in the driver.
116  */
117 #define	SSEEK	1		/* seeking */
118 #define	SIO	2		/* doing seq i/o */
119 #define	SCOM	3		/* sending control command */
120 #define	SREW	4		/* sending a drive rewind */
121 
122 /*
123  * Determine if there is a controller for
124  * a tm at address reg.  Our goal is to make the
125  * device interrupt.
126  */
127 tmprobe(reg)
128 	caddr_t reg;
129 {
130 	register int br, cvec;		/* must be r11,r10; value-result */
131 
132 #ifdef lint
133 	br = 0; cvec = br; br = cvec;
134 	tmintr(0);
135 #endif
136 	((struct tmdevice *)reg)->tmcs = TM_IE;
137 	/*
138 	 * If this is a tm11, it ought to have interrupted
139 	 * by now, if it isn't (ie: it is a ts04) then we just
140 	 * hope that it didn't interrupt, so autoconf will ignore it.
141 	 * Just in case, we will reference one
142 	 * of the more distant registers, and hope for a machine
143 	 * check, or similar disaster if this is a ts.
144 	 *
145 	 * Note: on an 11/780, badaddr will just generate
146 	 * a uba error for a ts; but our caller will notice that
147 	 * so we won't check for it.
148 	 */
149 	if (badaddr((caddr_t)&((struct tmdevice *)reg)->tmrd, 2))
150 		return (0);
151 	return (sizeof (struct tmdevice));
152 }
153 
154 /*
155  * Due to a design flaw, we cannot ascertain if the tape
156  * exists or not unless it is on line - ie: unless a tape is
157  * mounted. This is too servere a restriction to bear,
158  * so all units are assumed to exist.
159  */
160 /*ARGSUSED*/
161 tmslave(ui, reg)
162 	struct uba_device *ui;
163 	caddr_t reg;
164 {
165 
166 	return (1);
167 }
168 
169 /*
170  * Record attachment of the unit to the controller.
171  */
172 /*ARGSUSED*/
173 tmattach(ui)
174 	struct uba_device *ui;
175 {
176 	/*
177 	 * Tetotm is used in TMUNIT to index the ctmbuf and rtmbuf
178 	 * arrays given a te unit number.
179 	 */
180 	tetotm[ui->ui_unit] = ui->ui_mi->um_ctlr;
181 }
182 
183 int	tmtimer();
184 /*
185  * Open the device.  Tapes are unique open
186  * devices, so we refuse if it is already open.
187  * We also check that a tape is available, and
188  * don't block waiting here; if you want to wait
189  * for a tape you should timeout in user code.
190  */
191 tmopen(dev, flag)
192 	dev_t dev;
193 	int flag;
194 {
195 	register int teunit;
196 	register struct uba_device *ui;
197 	register struct te_softc *sc;
198 	int olddens, dens;
199 	int s;
200 
201 	teunit = TEUNIT(dev);
202 	if (teunit>=NTE || (sc = &te_softc[teunit])->sc_openf ||
203 	    (ui = tedinfo[teunit]) == 0 || ui->ui_alive == 0)
204 		return (ENXIO);
205 	olddens = sc->sc_dens;
206 	dens = TM_IE | TM_GO | (ui->ui_slave << 8);
207 	if ((minor(dev) & T_1600BPI) == 0)
208 		dens |= TM_D800;
209 	sc->sc_dens = dens;
210 get:
211 	tmcommand(dev, TM_SENSE, 1);
212 	if (sc->sc_erreg&TMER_SDWN) {
213 		sleep((caddr_t)&lbolt, PZERO+1);
214 		goto get;
215 	}
216 	sc->sc_dens = olddens;
217 	if ((sc->sc_erreg&(TMER_SELR|TMER_TUR)) != (TMER_SELR|TMER_TUR)) {
218 		uprintf("te%d: not online\n", teunit);
219 		return (EIO);
220 	}
221 	if ((flag&FWRITE) && (sc->sc_erreg&TMER_WRL)) {
222 		uprintf("te%d: no write ring\n", teunit);
223 		return (EIO);
224 	}
225 	if ((sc->sc_erreg&TMER_BOT) == 0 && (flag&FWRITE) &&
226 	    dens != sc->sc_dens) {
227 		uprintf("te%d: can't change density in mid-tape\n", teunit);
228 		return (EIO);
229 	}
230 	sc->sc_openf = 1;
231 	sc->sc_blkno = (daddr_t)0;
232 	sc->sc_nxrec = INF;
233 	sc->sc_lastiow = 0;
234 	sc->sc_dens = dens;
235 	sc->sc_ttyp = u.u_ttyp;
236 	s = spl6();
237 	if (sc->sc_tact == 0) {
238 		sc->sc_timo = INF;
239 		sc->sc_tact = 1;
240 		timeout(tmtimer, (caddr_t)dev, 5*hz);
241 	}
242 	splx(s);
243 	return (0);
244 }
245 
246 /*
247  * Close tape device.
248  *
249  * If tape was open for writing or last operation was
250  * a write, then write two EOF's and backspace over the last one.
251  * Unless this is a non-rewinding special file, rewind the tape.
252  * Make the tape available to others.
253  */
254 tmclose(dev, flag)
255 	register dev_t dev;
256 	register flag;
257 {
258 	register struct te_softc *sc = &te_softc[TEUNIT(dev)];
259 
260 	if (flag == FWRITE || (flag&FWRITE) && sc->sc_lastiow) {
261 		tmcommand(dev, TM_WEOF, 1);
262 		tmcommand(dev, TM_WEOF, 1);
263 		tmcommand(dev, TM_SREV, 1);
264 	}
265 	if ((minor(dev)&T_NOREWIND) == 0)
266 		/*
267 		 * 0 count means don't hang waiting for rewind complete
268 		 * rather ctmbuf stays busy until the operation completes
269 		 * preventing further opens from completing by
270 		 * preventing a TM_SENSE from completing.
271 		 */
272 		tmcommand(dev, TM_REW, 0);
273 	sc->sc_openf = 0;
274 }
275 
276 /*
277  * Execute a command on the tape drive
278  * a specified number of times.
279  */
280 tmcommand(dev, com, count)
281 	dev_t dev;
282 	int com, count;
283 {
284 	register struct buf *bp;
285 	register int s;
286 
287 	bp = &ctmbuf[TMUNIT(dev)];
288 	s = spl5();
289 	while (bp->b_flags&B_BUSY) {
290 		/*
291 		 * This special check is because B_BUSY never
292 		 * gets cleared in the non-waiting rewind case.
293 		 */
294 		if (bp->b_repcnt == 0 && (bp->b_flags&B_DONE))
295 			break;
296 		bp->b_flags |= B_WANTED;
297 		sleep((caddr_t)bp, PRIBIO);
298 	}
299 	bp->b_flags = B_BUSY|B_READ;
300 	splx(s);
301 	bp->b_dev = dev;
302 	bp->b_repcnt = -count;
303 	bp->b_command = com;
304 	bp->b_blkno = 0;
305 	tmstrategy(bp);
306 	/*
307 	 * In case of rewind from close, don't wait.
308 	 * This is the only case where count can be 0.
309 	 */
310 	if (count == 0)
311 		return;
312 	iowait(bp);
313 	if (bp->b_flags&B_WANTED)
314 		wakeup((caddr_t)bp);
315 	bp->b_flags &= B_ERROR;
316 }
317 
318 /*
319  * Queue a tape operation.
320  */
321 tmstrategy(bp)
322 	register struct buf *bp;
323 {
324 	int teunit = TEUNIT(bp->b_dev);
325 	register struct uba_ctlr *um;
326 	register struct buf *dp;
327 	int s;
328 
329 	/*
330 	 * Put transfer at end of unit queue
331 	 */
332 	dp = &teutab[teunit];
333 	bp->av_forw = NULL;
334 	s = spl5();
335 	um = tedinfo[teunit]->ui_mi;
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 		if (um->um_tab.b_actf == NULL)
344 			um->um_tab.b_actf = dp;
345 		else
346 			um->um_tab.b_actl->b_forw = dp;
347 		um->um_tab.b_actl = dp;
348 	} else
349 		dp->b_actl->av_forw = bp;
350 	dp->b_actl = bp;
351 	/*
352 	 * If the controller is not busy, get
353 	 * it going.
354 	 */
355 	if (um->um_tab.b_active == 0)
356 		tmstart(um);
357 	splx(s);
358 }
359 
360 /*
361  * Start activity on a tm controller.
362  */
363 tmstart(um)
364 	register struct uba_ctlr *um;
365 {
366 	register struct buf *bp, *dp;
367 	register struct tmdevice *addr = (struct tmdevice *)um->um_addr;
368 	register struct te_softc *sc;
369 	register struct uba_device *ui;
370 	int teunit, cmd;
371 	daddr_t blkno;
372 
373 	/*
374 	 * Look for an idle transport on the controller.
375 	 */
376 loop:
377 	if ((dp = um->um_tab.b_actf) == NULL)
378 		return;
379 	if ((bp = dp->b_actf) == NULL) {
380 		um->um_tab.b_actf = dp->b_forw;
381 		goto loop;
382 	}
383 	teunit = TEUNIT(bp->b_dev);
384 	ui = tedinfo[teunit];
385 	/*
386 	 * Record pre-transfer status (e.g. for TM_SENSE)
387 	 */
388 	sc = &te_softc[teunit];
389 	addr = (struct tmdevice *)um->um_addr;
390 	addr->tmcs = (ui->ui_slave << 8);
391 	sc->sc_dsreg = addr->tmcs;
392 	sc->sc_erreg = addr->tmer;
393 	sc->sc_resid = addr->tmbc;
394 	/*
395 	 * Default is that last command was NOT a write command;
396 	 * if we do a write command we will notice this in tmintr().
397 	 */
398 	sc->sc_lastiow = 0;
399 	if (sc->sc_openf < 0 || (addr->tmcs&TM_CUR) == 0) {
400 		/*
401 		 * Have had a hard error on a non-raw tape
402 		 * or the tape unit is now unavailable
403 		 * (e.g. taken off line).
404 		 */
405 		bp->b_flags |= B_ERROR;
406 		goto next;
407 	}
408 	if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) {
409 		/*
410 		 * Execute control operation with the specified count.
411 		 */
412 		if (bp->b_command == TM_SENSE)
413 			goto next;
414 		/*
415 		 * Set next state; give 5 minutes to complete
416 		 * rewind, or 10 seconds per iteration (minimum 60
417 		 * seconds and max 5 minutes) to complete other ops.
418 		 */
419 		if (bp->b_command == TM_REW) {
420 			um->um_tab.b_active = SREW;
421 			sc->sc_timo = 5 * 60;
422 		} else {
423 			um->um_tab.b_active = SCOM;
424 			sc->sc_timo =
425 			    imin(imax(10*(int)-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 (bdbtofsb(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 (bdbtofsb(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 = bdbtofsb(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) == bdbtofsb(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 < bdbtofsb(bp->b_blkno)) {
493 		bp->b_command = TM_SFORW;
494 		addr->tmbc = blkno - bdbtofsb(bp->b_blkno);
495 	} else {
496 		bp->b_command = TM_SREV;
497 		addr->tmbc = bdbtofsb(bp->b_blkno) - blkno;
498 	}
499 	sc->sc_timo = imin(imax(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 tmdevice *addr = (struct tmdevice *)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 tmdevice *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 tmdevice *)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 		tprintf(sc->sc_ttyp,
636 		    "te%d: hard error bn%d er=%b\n", minor(bp->b_dev)&03,
637 		    bp->b_blkno, sc->sc_erreg, TMER_BITS);
638 		bp->b_flags |= B_ERROR;
639 		goto opdone;
640 	}
641 	/*
642 	 * Advance tape control FSM.
643 	 */
644 ignoreerr:
645 	switch (state) {
646 
647 	case SIO:
648 		/*
649 		 * Read/write increments tape block number
650 		 */
651 		sc->sc_blkno++;
652 		goto opdone;
653 
654 	case SCOM:
655 		/*
656 		 * For forward/backward space record update current position.
657 		 */
658 		if (bp == &ctmbuf[TMUNIT(bp->b_dev)])
659 		switch (bp->b_command) {
660 
661 		case TM_SFORW:
662 			sc->sc_blkno -= bp->b_repcnt;
663 			break;
664 
665 		case TM_SREV:
666 			sc->sc_blkno += bp->b_repcnt;
667 			break;
668 		}
669 		goto opdone;
670 
671 	case SSEEK:
672 		sc->sc_blkno = bdbtofsb(bp->b_blkno);
673 		goto opcont;
674 
675 	default:
676 		panic("tmintr");
677 	}
678 opdone:
679 	/*
680 	 * Reset error count and remove
681 	 * from device queue.
682 	 */
683 	um->um_tab.b_errcnt = 0;
684 	dp->b_actf = bp->av_forw;
685 	/*
686 	 * Check resid; watch out for resid >32767 (tmbc not negative).
687 	 */
688 	bp->b_resid = ((int) -addr->tmbc) & 0xffff;
689 	ubadone(um);
690 	iodone(bp);
691 	/*
692 	 * Circulate slave to end of controller
693 	 * queue to give other slaves a chance.
694 	 */
695 	um->um_tab.b_actf = dp->b_forw;
696 	if (dp->b_actf) {
697 		dp->b_forw = NULL;
698 		if (um->um_tab.b_actf == NULL)
699 			um->um_tab.b_actf = dp;
700 		else
701 			um->um_tab.b_actl->b_forw = dp;
702 		um->um_tab.b_actl = dp;
703 	}
704 	if (um->um_tab.b_actf == 0)
705 		return;
706 opcont:
707 	tmstart(um);
708 }
709 
710 tmtimer(dev)
711 	int dev;
712 {
713 	register struct te_softc *sc = &te_softc[TEUNIT(dev)];
714 	register short x;
715 
716 	if (sc->sc_timo != INF && (sc->sc_timo -= 5) < 0) {
717 		printf("te%d: lost interrupt\n", TEUNIT(dev));
718 		sc->sc_timo = INF;
719 		x = spl5();
720 		tmintr(TMUNIT(dev));
721 		(void) splx(x);
722 	}
723 	timeout(tmtimer, (caddr_t)dev, 5*hz);
724 }
725 
726 tmseteof(bp)
727 	register struct buf *bp;
728 {
729 	register int teunit = TEUNIT(bp->b_dev);
730 	register struct tmdevice *addr =
731 	    (struct tmdevice *)tedinfo[teunit]->ui_addr;
732 	register struct te_softc *sc = &te_softc[teunit];
733 
734 	if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) {
735 		if (sc->sc_blkno > bdbtofsb(bp->b_blkno)) {
736 			/* reversing */
737 			sc->sc_nxrec = bdbtofsb(bp->b_blkno) - addr->tmbc;
738 			sc->sc_blkno = sc->sc_nxrec;
739 		} else {
740 			/* spacing forward */
741 			sc->sc_blkno = bdbtofsb(bp->b_blkno) + addr->tmbc;
742 			sc->sc_nxrec = sc->sc_blkno - 1;
743 		}
744 		return;
745 	}
746 	/* eof on read */
747 	sc->sc_nxrec = bdbtofsb(bp->b_blkno);
748 }
749 
750 tmread(dev, uio)
751 	dev_t dev;
752 	struct uio *uio;
753 {
754 	int errno;
755 
756 	errno = tmphys(dev, uio);
757 	if (errno)
758 		return (errno);
759 	return (physio(tmstrategy, &rtmbuf[TMUNIT(dev)], dev, B_READ, minphys, uio));
760 }
761 
762 tmwrite(dev, uio)
763 	dev_t dev;
764 	struct uio *uio;
765 {
766 	int errno;
767 
768 	errno = tmphys(dev, uio);
769 	if (errno)
770 		return (errno);
771 	return (physio(tmstrategy, &rtmbuf[TMUNIT(dev)], dev, B_WRITE, minphys, uio));
772 }
773 
774 /*
775  * Check that a raw device exists.
776  * If it does, set up sc_blkno and sc_nxrec
777  * so that the tape will appear positioned correctly.
778  */
779 tmphys(dev, uio)
780 	dev_t dev;
781 	struct uio *uio;
782 {
783 	register int teunit = TEUNIT(dev);
784 	register daddr_t a;
785 	register struct te_softc *sc;
786 	register struct uba_device *ui;
787 
788 	if (teunit >= NTE || (ui=tedinfo[teunit]) == 0 || ui->ui_alive == 0)
789 		return (ENXIO);
790 	sc = &te_softc[teunit];
791 	a = bdbtofsb(uio->uio_offset >> 9);
792 	sc->sc_blkno = a;
793 	sc->sc_nxrec = a + 1;
794 	return (0);
795 }
796 
797 tmreset(uban)
798 	int uban;
799 {
800 	register struct uba_ctlr *um;
801 	register tm11, teunit;
802 	register struct uba_device *ui;
803 	register struct buf *dp;
804 
805 	for (tm11 = 0; tm11 < NTM; tm11++) {
806 		if ((um = tmminfo[tm11]) == 0 || um->um_alive == 0 ||
807 		   um->um_ubanum != uban)
808 			continue;
809 		printf(" tm%d", tm11);
810 		um->um_tab.b_active = 0;
811 		um->um_tab.b_actf = um->um_tab.b_actl = 0;
812 		if (um->um_ubinfo) {
813 			printf("<%d>", (um->um_ubinfo>>28)&0xf);
814 			um->um_ubinfo = 0;
815 		}
816 		((struct tmdevice *)(um->um_addr))->tmcs = TM_DCLR;
817 		for (teunit = 0; teunit < NTE; teunit++) {
818 			if ((ui = tedinfo[teunit]) == 0 || ui->ui_mi != um ||
819 			    ui->ui_alive == 0)
820 				continue;
821 			dp = &teutab[teunit];
822 			dp->b_active = 0;
823 			dp->b_forw = 0;
824 			if (um->um_tab.b_actf == NULL)
825 				um->um_tab.b_actf = dp;
826 			else
827 				um->um_tab.b_actl->b_forw = dp;
828 			um->um_tab.b_actl = dp;
829 			if (te_softc[teunit].sc_openf > 0)
830 				te_softc[teunit].sc_openf = -1;
831 		}
832 		tmstart(um);
833 	}
834 }
835 
836 /*ARGSUSED*/
837 tmioctl(dev, cmd, data, flag)
838 	caddr_t data;
839 	dev_t dev;
840 {
841 	int teunit = TEUNIT(dev);
842 	register struct te_softc *sc = &te_softc[teunit];
843 	register struct buf *bp = &ctmbuf[TMUNIT(dev)];
844 	register callcount;
845 	int fcount;
846 	struct mtop *mtop;
847 	struct mtget *mtget;
848 	/* we depend of the values and order of the MT codes here */
849 	static tmops[] =
850 	   {TM_WEOF,TM_SFORW,TM_SREV,TM_SFORW,TM_SREV,TM_REW,TM_OFFL,TM_SENSE};
851 
852 	switch (cmd) {
853 
854 	case MTIOCTOP:	/* tape operation */
855 		mtop = (struct mtop *)data;
856 		switch (mtop->mt_op) {
857 
858 		case MTWEOF:
859 			callcount = mtop->mt_count;
860 			fcount = 1;
861 			break;
862 
863 		case MTFSF: case MTBSF:
864 			callcount = mtop->mt_count;
865 			fcount = INF;
866 			break;
867 
868 		case MTFSR: case MTBSR:
869 			callcount = 1;
870 			fcount = mtop->mt_count;
871 			break;
872 
873 		case MTREW: case MTOFFL: case MTNOP:
874 			callcount = 1;
875 			fcount = 1;
876 			break;
877 
878 		default:
879 			return (ENXIO);
880 		}
881 		if (callcount <= 0 || fcount <= 0)
882 			return (EINVAL);
883 		while (--callcount >= 0) {
884 			tmcommand(dev, tmops[mtop->mt_op], fcount);
885 			if ((mtop->mt_op == MTFSR || mtop->mt_op == MTBSR) &&
886 			    bp->b_resid)
887 				return (EIO);
888 			if ((bp->b_flags&B_ERROR) || sc->sc_erreg&TMER_BOT)
889 				break;
890 		}
891 		return (geterror(bp));
892 
893 	case MTIOCGET:
894 		mtget = (struct mtget *)data;
895 		mtget->mt_dsreg = sc->sc_dsreg;
896 		mtget->mt_erreg = sc->sc_erreg;
897 		mtget->mt_resid = sc->sc_resid;
898 		mtget->mt_type = MT_ISTM;
899 		break;
900 
901 	default:
902 		return (ENXIO);
903 	}
904 	return (0);
905 }
906 
907 #define	DBSIZE	20
908 
909 tmdump()
910 {
911 	register struct uba_device *ui;
912 	register struct uba_regs *up;
913 	register struct tmdevice *addr;
914 	int blk, num;
915 	int start;
916 
917 	start = 0;
918 	num = maxfree;
919 #define	phys(a,b)	((b)((int)(a)&0x7fffffff))
920 	if (tedinfo[0] == 0)
921 		return (ENXIO);
922 	ui = phys(tedinfo[0], struct uba_device *);
923 	up = phys(ui->ui_hd, struct uba_hd *)->uh_physuba;
924 	ubainit(up);
925 	DELAY(1000000);
926 	addr = (struct tmdevice *)ui->ui_physaddr;
927 	tmwait(addr);
928 	addr->tmcs = TM_DCLR | TM_GO;
929 	while (num > 0) {
930 		blk = num > DBSIZE ? DBSIZE : num;
931 		tmdwrite(start, blk, addr, up);
932 		start += blk;
933 		num -= blk;
934 	}
935 	tmeof(addr);
936 	tmeof(addr);
937 	tmwait(addr);
938 	if (addr->tmcs&TM_ERR)
939 		return (EIO);
940 	addr->tmcs = TM_REW | TM_GO;
941 	tmwait(addr);
942 	return (0);
943 }
944 
945 tmdwrite(dbuf, num, addr, up)
946 	register dbuf, num;
947 	register struct tmdevice *addr;
948 	struct uba_regs *up;
949 {
950 	register struct pte *io;
951 	register int npf;
952 
953 	tmwait(addr);
954 	io = up->uba_map;
955 	npf = num+1;
956 	while (--npf != 0)
957 		 *(int *)io++ = (dbuf++ | (1<<UBAMR_DPSHIFT) | UBAMR_MRV);
958 	*(int *)io = 0;
959 	addr->tmbc = -(num*NBPG);
960 	addr->tmba = 0;
961 	addr->tmcs = TM_WCOM | TM_GO;
962 }
963 
964 tmwait(addr)
965 	register struct tmdevice *addr;
966 {
967 	register s;
968 
969 	do
970 		s = addr->tmcs;
971 	while ((s & TM_CUR) == 0);
972 }
973 
974 tmeof(addr)
975 	struct tmdevice *addr;
976 {
977 
978 	tmwait(addr);
979 	addr->tmcs = TM_WEOF | TM_GO;
980 }
981 #endif
982