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