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