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