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