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