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