xref: /csrg-svn/sys/vax/uba/ut.c (revision 23357)
1 /*
2  * Copyright (c) 1982 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  *
6  *	@(#)ut.c	6.5 (Berkeley) 06/08/85
7  */
8 
9 #include "tj.h"
10 #if NUT > 0
11 /*
12  * System Industries Model 9700 Tape Drive
13  *   emulates a TU45 on the UNIBUS
14  *
15  * TODO:
16  *	check out attention processing
17  *	try reset code and dump code
18  */
19 #include "../machine/pte.h"
20 
21 #include "param.h"
22 #include "systm.h"
23 #include "buf.h"
24 #include "conf.h"
25 #include "dir.h"
26 #include "file.h"
27 #include "user.h"
28 #include "map.h"
29 #include "ioctl.h"
30 #include "mtio.h"
31 #include "cmap.h"
32 #include "uio.h"
33 #include "kernel.h"
34 #include "tty.h"
35 
36 #include "../vax/cpu.h"
37 #include "ubareg.h"
38 #include "ubavar.h"
39 #include "utreg.h"
40 
41 struct	buf	rutbuf[NUT];	/* bufs for raw i/o */
42 struct	buf	cutbuf[NUT];	/* bufs for control operations */
43 struct	buf	tjutab[NTJ];	/* bufs for slave queue headers */
44 
45 struct uba_ctlr *utminfo[NUT];
46 struct uba_device *tjdinfo[NTJ];
47 int utprobe(), utslave(), utattach(), utdgo(), utintr(), uttimer();
48 u_short utstd[] = { 0772440, 0 };
49 struct uba_driver utdriver =
50   { utprobe, utslave, utattach, utdgo, utstd, "tj", tjdinfo, "ut", utminfo, 0 };
51 
52 #define	MASKREG(reg)	((reg)&0xffff)
53 
54 /* bits in minor device */
55 #define	TJUNIT(dev)	(minor(dev)&03)
56 #define	T_NOREWIND	04
57 #define	T_1600BPI	010
58 #define	T_6250BPI	020
59 short	utdens[] = { UT_NRZI, UT_PE, UT_GCR, UT_NRZI };
60 
61 /* slave to controller mapping table */
62 short	tjtout[NTJ];
63 #define UTUNIT(dev)	(tjtout[TJUNIT(dev)])
64 
65 #define	INF	(daddr_t)1000000L	/* a block number that wont exist */
66 
67 struct	tj_softc {
68 	char	sc_openf;	/* exclusive open */
69 	char	sc_lastiow;	/* last I/O operation was a write */
70 	daddr_t	sc_blkno;	/* next block to transfer */
71 	daddr_t	sc_nxrec;	/* next record on tape */
72 	u_short	sc_erreg;	/* image of uter */
73 	u_short	sc_dsreg;	/* image of utds */
74 	u_short	sc_resid;	/* residual from transfer */
75 	u_short	sc_dens;	/* sticky selected density */
76 	daddr_t	sc_timo;	/* time until timeout expires */
77 	short	sc_tact;	/* timeout is active flag */
78 	struct	tty *sc_ttyp;	/* record user's tty for errors */
79 } tj_softc[NTJ];
80 
81 /*
82  * Internal per/slave states found in sc_state
83  */
84 #define	SSEEK		1	/* seeking */
85 #define	SIO		2	/* doing sequential I/O */
86 #define	SCOM		3	/* sending a control command */
87 #define	SREW		4	/* doing a rewind op */
88 #define	SERASE		5	/* erase inter-record gap */
89 #define	SERASED		6	/* erased inter-record gap */
90 
91 /*ARGSUSED*/
92 utprobe(reg)
93 	caddr_t reg;
94 {
95 	register int br, cvec;
96 #ifdef lint
97 	br=0; cvec=br; br=cvec;
98 	utintr(0);
99 #endif
100 	/*
101 	 * The SI documentation says you must set the RDY bit
102 	 * (even though it's read-only) to force an interrupt.
103 	 */
104 	((struct utdevice *) reg)->utcs1 = UT_IE|UT_NOP|UT_RDY;
105 	DELAY(10000);
106 	return (sizeof (struct utdevice));
107 }
108 
109 /*ARGSUSED*/
110 utslave(ui, reg)
111 	struct uba_device *ui;
112 	caddr_t reg;
113 {
114 	/*
115 	 * A real TU45 would support the slave present bit
116 	 * int the drive type register, but this thing doesn't,
117 	 * so there's no way to determine if a slave is present or not.
118 	 */
119 	 return(1);
120 }
121 
122 utattach(ui)
123 	struct uba_device *ui;
124 {
125 	tjtout[ui->ui_unit] = ui->ui_mi->um_ctlr;
126 }
127 
128 /*
129  * Open the device with exclusive access.
130  */
131 utopen(dev, flag)
132 	dev_t dev;
133 	int flag;
134 {
135 	register int tjunit = TJUNIT(dev);
136 	register struct uba_device *ui;
137 	register struct tj_softc *sc;
138 	int olddens, dens;
139 	register int s;
140 
141 	if (tjunit >= NTJ || (sc = &tj_softc[tjunit])->sc_openf ||
142 	    (ui = tjdinfo[tjunit]) == 0 || ui->ui_alive == 0)
143 		return (ENXIO);
144 	olddens = sc->sc_dens;
145 	dens = sc->sc_dens =
146 	    utdens[(minor(dev)&(T_1600BPI|T_6250BPI))>>3]|
147 	      PDP11FMT|(ui->ui_slave&07);
148 get:
149 	utcommand(dev, UT_SENSE, 1);
150 	if (sc->sc_dsreg&UTDS_PIP) {
151 		sleep((caddr_t)&lbolt, PZERO+1);
152 		goto get;
153 	}
154 	sc->sc_dens = olddens;
155 	if ((sc->sc_dsreg&UTDS_MOL) == 0) {
156 		uprintf("tj%d: not online\n", tjunit);
157 		return (EIO);
158 	}
159 	if ((flag&FWRITE) && (sc->sc_dsreg&UTDS_WRL)) {
160 		uprintf("tj%d: no write ring\n", tjunit);
161 		return (EIO);
162 	}
163 	if ((sc->sc_dsreg&UTDS_BOT) == 0 && (flag&FWRITE) &&
164 	    dens != sc->sc_dens) {
165 		uprintf("tj%d: can't change density in mid-tape\n", tjunit);
166 		return (EIO);
167 	}
168 	sc->sc_openf = 1;
169 	sc->sc_blkno = (daddr_t)0;
170 	sc->sc_nxrec = INF;
171 	sc->sc_lastiow = 0;
172 	sc->sc_dens = dens;
173 	sc->sc_ttyp = u.u_ttyp;
174 	/*
175 	 * For 6250 bpi take exclusive use of the UNIBUS.
176 	 */
177 	ui->ui_driver->ud_xclu = (dens&(T_1600BPI|T_6250BPI)) == T_6250BPI;
178 	s = spl6();
179 	if (sc->sc_tact == 0) {
180 		sc->sc_timo = INF;
181 		sc->sc_tact = 1;
182 		timeout(uttimer, (caddr_t)dev, 5*hz);
183 	}
184 	splx(s);
185 	return (0);
186 }
187 
188 utclose(dev, flag)
189 	register dev_t dev;
190 	register flag;
191 {
192 	register struct tj_softc *sc = &tj_softc[TJUNIT(dev)];
193 
194 	if (flag == FWRITE || ((flag&FWRITE) && sc->sc_lastiow)) {
195 		utcommand(dev, UT_WEOF, 1);
196 		utcommand(dev, UT_WEOF, 1);
197 		utcommand(dev, UT_SREV, 1);
198 	}
199 	if ((minor(dev)&T_NOREWIND) == 0)
200 		utcommand(dev, UT_REW, 0);
201 	sc->sc_openf = 0;
202 }
203 
204 utcommand(dev, com, count)
205 	dev_t dev;
206 	int com, count;
207 {
208 	register struct buf *bp;
209 	register int s;
210 
211 	bp = &cutbuf[UTUNIT(dev)];
212 	s = spl5();
213 	while (bp->b_flags&B_BUSY) {
214 		if(bp->b_repcnt == 0 && (bp->b_flags&B_DONE))
215 			break;
216 		bp->b_flags |= B_WANTED;
217 		sleep((caddr_t)bp, PRIBIO);
218 	}
219 	bp->b_flags = B_BUSY|B_READ;
220 	splx(s);
221 	bp->b_dev = dev;
222 	bp->b_command = com;
223 	bp->b_repcnt = count;
224 	bp->b_blkno = 0;
225 	utstrategy(bp);
226 	if (count == 0)
227 		return;
228 	iowait(bp);
229 	if (bp->b_flags&B_WANTED)
230 		wakeup((caddr_t)bp);
231 	bp->b_flags &= B_ERROR;
232 }
233 
234 /*
235  * Queue a tape operation.
236  */
237 utstrategy(bp)
238 	register struct buf *bp;
239 {
240 	int tjunit = TJUNIT(bp->b_dev);
241 	register struct uba_ctlr *um;
242 	register struct buf *dp;
243 
244 	/*
245 	 * Put transfer at end of unit queue
246 	 */
247 	dp = &tjutab[tjunit];
248 	bp->av_forw = NULL;
249 	um = tjdinfo[tjunit]->ui_mi;
250 	(void) spl5();
251 	if (dp->b_actf == NULL) {
252 		dp->b_actf = bp;
253 		/*
254 		 * Transport not active, so...
255 		 * put at end of controller queue
256 		 */
257 		dp->b_forw = NULL;
258 		if (um->um_tab.b_actf == NULL)
259 			um->um_tab.b_actf = dp;
260 		else
261 			um->um_tab.b_actl->b_forw = dp;
262 		um->um_tab.b_actl = dp;
263 	} else
264 		dp->b_actl->av_forw = bp;
265 	dp->b_actl = bp;
266 	/*
267 	 * If the controller is not busy, set it going.
268 	 */
269 	if (um->um_tab.b_state == 0)
270 		utstart(um);
271 	(void) spl0();
272 }
273 
274 utstart(um)
275 	register struct uba_ctlr *um;
276 {
277 	register struct utdevice *addr;
278 	register struct buf *bp, *dp;
279 	register struct tj_softc *sc;
280 	struct uba_device *ui;
281 	int tjunit;
282 	daddr_t blkno;
283 
284 loop:
285 	/*
286 	 * Scan controller queue looking for units with
287 	 * transaction queues to dispatch
288 	 */
289 	if ((dp = um->um_tab.b_actf) == NULL)
290 		return;
291 	if ((bp = dp->b_actf) == NULL) {
292 		um->um_tab.b_actf = dp->b_forw;
293 		goto loop;
294 	}
295 	addr = (struct utdevice *)um->um_addr;
296 	tjunit = TJUNIT(bp->b_dev);
297 	ui = tjdinfo[tjunit];
298 	sc = &tj_softc[tjunit];
299 	/* note slave select, density, and format were merged on open */
300 	addr->uttc = sc->sc_dens;
301 	sc->sc_dsreg = addr->utds;
302 	sc->sc_erreg = addr->uter;
303 	sc->sc_resid = MASKREG(addr->utfc);
304 	/*
305 	 * Default is that last command was NOT a write command;
306 	 * if we do a write command we will notice this in utintr().
307 	 */
308 	sc->sc_lastiow = 0;
309 	if (sc->sc_openf < 0 || (addr->utds&UTDS_MOL) == 0) {
310 		/*
311 		 * Have had a hard error on a non-raw tape
312 		 * or the tape unit is now unavailable
313 		 * (e.g. taken off line).
314 		 */
315 		bp->b_flags |= B_ERROR;
316 		goto next;
317 	}
318 	if (bp == &cutbuf[UTUNIT(bp->b_dev)]) {
319 		/*
320 		 * Execute a control operation with the specified
321 		 * count.
322 		 */
323 		if (bp->b_command == UT_SENSE)
324 			goto next;
325 		if (bp->b_command == UT_SFORW && (addr->utds & UTDS_EOT)) {
326 			bp->b_resid = bp->b_bcount;
327 			goto next;
328 		}
329 		/*
330 		 * Set next state; handle timeouts
331 		 */
332 		if (bp->b_command == UT_REW) {
333 			um->um_tab.b_state = SREW;
334 			sc->sc_timo = 5*60;
335 		} else {
336 			um->um_tab.b_state = SCOM;
337 			sc->sc_timo = imin(imax(10*(int)-bp->b_repcnt,60),5*60);
338 		}
339 		/* NOTE: this depends on the ut command values */
340 		if (bp->b_command >= UT_SFORW && bp->b_command <= UT_SREVF)
341 			addr->utfc = -bp->b_repcnt;
342 		goto dobpcmd;
343 	}
344 	/*
345 	 * The following checks boundary conditions for operations
346 	 * on non-raw tapes.  On raw tapes the initialization of
347 	 * sc->sc_nxrec by utphys causes them to be skipped normally
348 	 * (except in the case of retries).
349 	 */
350 	if (bdbtofsb(bp->b_blkno) > sc->sc_nxrec) {
351 		/* can't read past end of file */
352 		bp->b_flags |= B_ERROR;
353 		bp->b_error = ENXIO;
354 		goto next;
355 	}
356 	if (bdbtofsb(bp->b_blkno) == sc->sc_nxrec && (bp->b_flags&B_READ)) {
357 		/* read at eof returns 0 count */
358 		bp->b_resid = bp->b_bcount;
359 		clrbuf(bp);
360 		goto next;
361 	}
362 	if ((bp->b_flags&B_READ) == 0)
363 		sc->sc_nxrec = bdbtofsb(bp->b_blkno)+1;
364 	/*
365 	 * If the tape is correctly positioned, set up all the
366 	 * registers but the csr, and give control over to the
367 	 * UNIBUS adaptor routines, to wait for resources to
368 	 * start I/O.
369 	 */
370 	if ((blkno = sc->sc_blkno) == bdbtofsb(bp->b_blkno)) {
371 		addr->utwc = -(((bp->b_bcount)+1)>>1);
372 		addr->utfc = -bp->b_bcount;
373 		if ((bp->b_flags&B_READ) == 0) {
374 			/*
375 			 * On write error retries erase the
376 			 * inter-record gap before rewriting.
377 			 */
378 			if (um->um_tab.b_errcnt) {
379 				if (um->um_tab.b_state != SERASED) {
380 					um->um_tab.b_state = SERASE;
381 					sc->sc_timo = 60;
382 					addr->utcs1 = UT_ERASE|UT_IE|UT_GO;
383 					return;
384 				}
385 			}
386 			if (addr->utds & UTDS_EOT) {
387 				bp->b_resid = bp->b_bcount;
388 				um->um_tab.b_state = 0;
389 				goto next;
390 			}
391 			um->um_cmd = UT_WCOM;
392 		} else
393 			um->um_cmd = UT_RCOM;
394 		sc->sc_timo = 60;
395 		um->um_tab.b_state = SIO;
396 		(void) ubago(ui);
397 		return;
398 	}
399 	/*
400 	 * Tape positioned incorrectly; seek forwards or
401 	 * backwards to the correct spot.  This happens for
402 	 * raw tapes only on error retries.
403 	 */
404 	um->um_tab.b_state = SSEEK;
405 	if (blkno < bdbtofsb(bp->b_blkno)) {
406 		addr->utfc = blkno - bdbtofsb(bp->b_blkno);
407 		bp->b_command = UT_SFORW;
408 	} else {
409 		addr->utfc = bdbtofsb(bp->b_blkno) - blkno;
410 		bp->b_command = UT_SREV;
411 	}
412 	sc->sc_timo = imin(imax(10 * -addr->utfc, 60), 5*60);
413 
414 dobpcmd:
415 	/*
416 	 * Perform the command setup in bp.
417 	 */
418 	addr->utcs1 = bp->b_command|UT_IE|UT_GO;
419 	return;
420 next:
421 	/*
422 	 * Advance to the next command in the slave queue,
423 	 * posting notice and releasing resources as needed.
424 	 */
425 	if (um->um_ubinfo)
426 		ubadone(um);
427 	um->um_tab.b_errcnt = 0;
428 	dp->b_actf = bp->av_forw;
429 	iodone(bp);
430 	goto loop;
431 }
432 
433 /*
434  * Start operation on controller --
435  * UNIBUS resources have been allocated.
436  */
437 utdgo(um)
438 	register struct uba_ctlr *um;
439 {
440 	register struct utdevice *addr = (struct utdevice *)um->um_addr;
441 
442 	addr->utba = (u_short) um->um_ubinfo;
443 	addr->utcs1 = um->um_cmd|((um->um_ubinfo>>8)&0x300)|UT_IE|UT_GO;
444 }
445 
446 /*
447  * Ut interrupt handler
448  */
449 /*ARGSUSED*/
450 utintr(ut11)
451 	int ut11;
452 {
453 	struct buf *dp;
454 	register struct buf *bp;
455 	register struct uba_ctlr *um = utminfo[ut11];
456 	register struct utdevice *addr;
457 	register struct tj_softc *sc;
458 	u_short tjunit, cs2, cs1;
459 	register state;
460 
461 	if ((dp = um->um_tab.b_actf) == NULL)
462 		return;
463 	bp = dp->b_actf;
464 	tjunit = TJUNIT(bp->b_dev);
465 	addr = (struct utdevice *)tjdinfo[tjunit]->ui_addr;
466 	sc = &tj_softc[tjunit];
467 	/*
468 	 * Record status...
469 	 */
470 	sc->sc_timo = INF;
471 	sc->sc_dsreg = addr->utds;
472 	sc->sc_erreg = addr->uter;
473 	sc->sc_resid = MASKREG(addr->utfc);
474 	if ((bp->b_flags&B_READ) == 0)
475 		sc->sc_lastiow = 1;
476 	state = um->um_tab.b_state;
477 	um->um_tab.b_state = 0;
478 	/*
479 	 * Check for errors...
480 	 */
481 	if ((addr->utds&UTDS_ERR) || (addr->utcs1&UT_TRE)) {
482 		/*
483 		 * To clear the ERR bit, we must issue a drive clear
484 		 * command, and to clear the TRE bit we must set the
485 		 * controller clear bit.
486 		 */
487 		cs2 = addr->utcs2;
488 		if ((cs1 = addr->utcs1)&UT_TRE)
489 			addr->utcs2 |= UTCS2_CLR;
490 		/* is this dangerous ?? */
491 		while ((addr->utcs1&UT_RDY) == 0)
492 			;
493 		addr->utcs1 = UT_CLEAR|UT_GO;
494 		/*
495 		 * If we were reading at 1600 or 6250 bpi and the error
496 		 * was corrected, then don't consider this an error.
497 		 */
498 		if (sc->sc_erreg & UTER_COR && (bp->b_flags & B_READ) &&
499 		    (addr->uttc & UTTC_DEN) != UT_NRZI) {
500 			tprintf(sc->sc_ttyp,
501 			  "ut%d: soft error bn%d cs1=%b er=%b cs2=%b ds=%b\n",
502 			  tjunit, bp->b_blkno, cs1, UT_BITS, sc->sc_erreg,
503 			  UTER_BITS, cs2, UTCS2_BITS, sc->sc_dsreg, UTDS_BITS);
504 			sc->sc_erreg &= ~UTER_COR;
505 		}
506 		/*
507 		 * If we were reading from a raw tape and the only error
508 		 * was that the record was too long, then we don't consider
509 		 * this an error.
510 		 */
511 		if (bp == &rutbuf[UTUNIT(bp->b_dev)] && (bp->b_flags&B_READ) &&
512 		    (sc->sc_erreg&UTER_FCE))
513 			sc->sc_erreg &= ~UTER_FCE;
514 		if (sc->sc_erreg == 0)
515 			goto ignoreerr;
516 		/*
517 		 * Fix up errors which occur due to backspacing
518 		 * "over" the front of the tape.
519 		 */
520 		if ((sc->sc_dsreg & UTDS_BOT) && bp->b_command == UT_SREV &&
521 		    ((sc->sc_erreg &= ~(UTER_NEF|UTER_FCE)) == 0))
522 			goto opdone;
523 		/*
524 		 * Retry soft errors up to 8 times
525 		 */
526 		if ((sc->sc_erreg&UTER_HARD) == 0 && state == SIO) {
527 			if (++um->um_tab.b_errcnt < 7) {
528 				sc->sc_blkno++;
529 				ubadone(um);
530 				goto opcont;
531 			}
532 		}
533 		/*
534 		 * Hard or non-I/O errors on non-raw tape
535 		 * cause it to close.
536 		 */
537 		if (sc->sc_openf > 0 && bp != &rutbuf[UTUNIT(bp->b_dev)])
538 			sc->sc_openf = -1;
539 		/*
540 		 * Couldn't recover error.
541 		 */
542 		tprintf(sc->sc_ttyp,
543 			"ut%d: hard error bn%d cs1=%b er=%b cs2=%b ds=%b\n",
544 			tjunit, bp->b_blkno, cs1, UT_BITS, sc->sc_erreg,
545 			UTER_BITS, cs2, UTCS2_BITS, sc->sc_dsreg, UTDS_BITS);
546 		bp->b_flags |= B_ERROR;
547 		goto opdone;
548 	}
549 
550 ignoreerr:
551 	/*
552 	 * If we hit a tape mark update our position.
553 	 */
554 	if (sc->sc_dsreg & UTDS_TM && bp->b_flags & B_READ) {
555 		/*
556 		 * Set blkno and nxrec
557 		 */
558 		if (bp == &cutbuf[UTUNIT(bp->b_dev)]) {
559 			if (sc->sc_blkno > bdbtofsb(bp->b_blkno)) {
560 				sc->sc_nxrec =
561 				     bdbtofsb(bp->b_blkno) - addr->utfc;
562 				sc->sc_blkno = sc->sc_nxrec;
563 			} else {
564 				sc->sc_blkno =
565 				     bdbtofsb(bp->b_blkno) + addr->utfc;
566 				sc->sc_nxrec = sc->sc_blkno-1;
567 			}
568 		} else
569 			sc->sc_nxrec = bdbtofsb(bp->b_blkno);
570 		/*
571 		 * Note: if we get a tape mark on a read, the
572 		 * frame count register will be zero, so b_resid
573 		 * will be calculated correctly below.
574 		 */
575 		goto opdone;
576 	}
577 	/*
578 	 * Advance tape control FSM.
579 	 */
580 	switch (state) {
581 
582 	case SIO:		/* read/write increments tape block # */
583 		sc->sc_blkno++;
584 		break;
585 
586 	case SCOM:		/* motion commands update current position */
587 		if (bp == &cutbuf[UTUNIT(bp->b_dev)])
588 		switch (bp->b_command) {
589 
590 		case UT_SFORW:
591 			sc->sc_blkno -= bp->b_repcnt;
592 			break;
593 
594 		case UT_SREV:
595 			sc->sc_blkno += bp->b_repcnt;
596 			break;
597 
598 		case UT_REWOFFL:
599 			addr->utcs1 = UT_CLEAR|UT_GO;
600 			break;
601 		}
602 		break;
603 
604 	case SSEEK:
605 		sc->sc_blkno = bdbtofsb(bp->b_blkno);
606 		goto opcont;
607 
608 	case SERASE:
609 		/*
610 		 * Completed erase of the inter-record gap due to a
611 		 * write error; now retry the write operation.
612 		 */
613 		um->um_tab.b_state = SERASED;
614 		goto opcont;
615 
616 	case SREW:			/* clear attention bit */
617 		addr->utcs1 = UT_CLEAR|UT_GO;
618 		break;
619 
620 	default:
621 		printf("bad state %d\n", state);
622 		panic("utintr");
623 	}
624 
625 opdone:
626 	/*
627 	 * Reset error count and remove
628 	 * from device queue
629 	 */
630 	um->um_tab.b_errcnt = 0;
631 	dp->b_actf = bp->av_forw;
632 	/*
633 	 * For read command, frame count register contains
634 	 * actual length of tape record.  Otherwise, it
635 	 * holds negative residual count.
636 	 */
637 	if (state == SIO && um->um_cmd == UT_RCOM) {
638 		bp->b_resid = 0;
639 		if (bp->b_bcount > MASKREG(addr->utfc))
640 			bp->b_resid = bp->b_bcount - MASKREG(addr->utfc);
641 	} else
642 		bp->b_resid = MASKREG(-addr->utfc);
643 	ubadone(um);
644 	iodone(bp);
645 	/*
646 	 * Circulate slave to end of controller queue
647 	 * to give other slaves a chance
648 	 */
649 	um->um_tab.b_actf = dp->b_forw;
650 	if (dp->b_actf) {
651 		dp->b_forw = NULL;
652 		if (um->um_tab.b_actf == NULL)
653 			um->um_tab.b_actf = dp;
654 		else
655 			um->um_tab.b_actl->b_forw = dp;
656 		um->um_tab.b_actl = dp;
657 	}
658 	if (um->um_tab.b_actf == 0)
659 		return;
660 opcont:
661 	utstart(um);
662 }
663 
664 /*
665  * Watchdog timer routine.
666  */
667 uttimer(dev)
668 	int dev;
669 {
670 	register struct tj_softc *sc = &tj_softc[TJUNIT(dev)];
671 	register short x;
672 
673 	if (sc->sc_timo != INF && (sc->sc_timo -= 5) < 0) {
674 		printf("tj%d: lost interrupt\n", TJUNIT(dev));
675 		sc->sc_timo = INF;
676 		x = spl5();
677 		utintr(UTUNIT(dev));
678 		(void) splx(x);
679 	}
680 	timeout(uttimer, (caddr_t)dev, 5*hz);
681 }
682 
683 /*
684  * Raw interface for a read
685  */
686 utread(dev, uio)
687 	dev_t dev;
688 	struct uio *uio;
689 {
690 	int errno;
691 
692 	errno = utphys(dev, uio);
693 	if (errno)
694 		return (errno);
695 	return (physio(utstrategy, &rutbuf[UTUNIT(dev)], dev, B_READ, minphys, uio));
696 }
697 
698 /*
699  * Raw interface for a write
700  */
701 utwrite(dev, uio)
702 	dev_t dev;
703 	struct uio *uio;
704 {
705 	int errno;
706 
707 	errno = utphys(dev, uio);
708 	if (errno)
709 		return (errno);
710 	return (physio(utstrategy, &rutbuf[UTUNIT(dev)], dev, B_WRITE, minphys, uio));
711 }
712 
713 /*
714  * Check for valid device number dev and update our notion
715  * of where we are on the tape
716  */
717 utphys(dev, uio)
718 	dev_t dev;
719 	struct uio *uio;
720 {
721 	register int tjunit = TJUNIT(dev);
722 	register struct tj_softc *sc;
723 	register struct uba_device *ui;
724 
725 	if (tjunit >= NTJ || (ui=tjdinfo[tjunit]) == 0 || ui->ui_alive == 0)
726 		return (ENXIO);
727 	sc = &tj_softc[tjunit];
728 	sc->sc_blkno = bdbtofsb(uio->uio_offset>>9);
729 	sc->sc_nxrec = sc->sc_blkno+1;
730 	return (0);
731 }
732 
733 /*ARGSUSED*/
734 utioctl(dev, cmd, data, flag)
735 	dev_t dev;
736 	caddr_t data;
737 {
738 	register struct tj_softc *sc = &tj_softc[TJUNIT(dev)];
739 	register struct buf *bp = &cutbuf[UTUNIT(dev)];
740 	register callcount;
741 	int fcount;
742 	struct mtop *mtop;
743 	struct mtget *mtget;
744 	/* we depend of the values and order of the MT codes here */
745 	static utops[] =
746       {UT_WEOF,UT_SFORWF,UT_SREVF,UT_SFORW,UT_SREV,UT_REW,UT_REWOFFL,UT_SENSE};
747 
748 	switch (cmd) {
749 
750 	case MTIOCTOP:
751 		mtop = (struct mtop *)data;
752 		switch(mtop->mt_op) {
753 
754 		case MTWEOF:
755 		case MTFSF: case MTBSF:
756 		case MTFSR: case MTBSR:
757 			callcount = mtop->mt_count;
758 			fcount = 1;
759 			break;
760 
761 		case MTREW: case MTOFFL: case MTNOP:
762 			callcount = 1;
763 			fcount = 1;
764 			break;
765 
766 		default:
767 			return (ENXIO);
768 		}
769 		if (callcount <= 0 || fcount <= 0)
770 			return (EINVAL);
771 		while (--callcount >= 0) {
772 			utcommand(dev, utops[mtop->mt_op], fcount);
773 			if ((bp->b_flags&B_ERROR) || (sc->sc_dsreg&UTDS_BOT))
774 				break;
775 		}
776 		return (geterror(bp));
777 
778 	case MTIOCGET:
779 		mtget = (struct mtget *)data;
780 		mtget->mt_dsreg = sc->sc_dsreg;
781 		mtget->mt_erreg = sc->sc_erreg;
782 		mtget->mt_resid = sc->sc_resid;
783 		mtget->mt_type = MT_ISUT;
784 		break;
785 
786 	default:
787 		return (ENXIO);
788 	}
789 	return (0);
790 }
791 
792 utreset(uban)
793 	int uban;
794 {
795 	register struct uba_ctlr *um;
796 	register ut11, tjunit;
797 	register struct uba_device *ui;
798 	register struct buf *dp;
799 
800 	for (ut11 = 0; ut11 < NUT; ut11++) {
801 		if ((um = utminfo[ut11]) == 0 || um->um_alive == 0 ||
802 		   um->um_ubanum != uban)
803 			continue;
804 		printf(" ut%d", ut11);
805 		um->um_tab.b_state = 0;
806 		um->um_tab.b_actf = um->um_tab.b_actl = 0;
807 		if (um->um_ubinfo) {
808 			printf("<%d>", (um->um_ubinfo>>28)&0xf);
809 			um->um_ubinfo = 0;
810 		}
811 		((struct utdevice *)(um->um_addr))->utcs1 = UT_CLEAR|UT_GO;
812 		((struct utdevice *)(um->um_addr))->utcs2 |= UTCS2_CLR;
813 		for (tjunit = 0; tjunit < NTJ; tjunit++) {
814 			if ((ui = tjdinfo[tjunit]) == 0 || ui->ui_mi != um ||
815 			    ui->ui_alive == 0)
816 				continue;
817 			dp = &tjutab[tjunit];
818 			dp->b_state = 0;
819 			dp->b_forw = 0;
820 			if (um->um_tab.b_actf == NULL)
821 				um->um_tab.b_actf = dp;
822 			else
823 				um->um_tab.b_actl->b_forw = dp;
824 			um->um_tab.b_actl = dp;
825 			if (tj_softc[tjunit].sc_openf > 0)
826 				tj_softc[tjunit].sc_openf = -1;
827 		}
828 		utstart(um);
829 	}
830 }
831 
832 /*
833  * Do a stand-alone core dump to tape --
834  * from here down, routines are used only in dump context
835  */
836 #define	DBSIZE	20
837 
838 utdump()
839 {
840 	register struct uba_device *ui;
841 	register struct uba_regs *up;
842 	register struct utdevice *addr;
843 	int blk, num = maxfree;
844 	int start = 0;
845 
846 #define	phys(a,b)		((b)((int)(a)&0x7fffffff))
847 	if (tjdinfo[0] == 0)
848 		return (ENXIO);
849 	ui = phys(tjdinfo[0], struct uba_device *);
850 	up = phys(ui->ui_hd, struct uba_hd *)->uh_physuba;
851 	ubainit(up);
852 	DELAY(1000000);
853 	addr = (struct utdevice *)ui->ui_physaddr;
854 	utwait(addr);
855 	/*
856 	 * Be sure to set the appropriate density here.  We use
857 	 * 6250, but maybe it should be done at 1600 to insure the
858 	 * tape can be read by most any other tape drive available.
859 	 */
860 	addr->uttc = UT_GCR|PDP11FMT;	/* implicit slave 0 or-ed in */
861 	addr->utcs1 = UT_CLEAR|UT_GO;
862 	while (num > 0) {
863 		blk = num > DBSIZE ? DBSIZE : num;
864 		utdwrite(start, blk, addr, up);
865 		if ((addr->utds&UTDS_ERR) || (addr->utcs1&UT_TRE))
866 			return(EIO);
867 		start += blk;
868 		num -= blk;
869 	}
870 	uteof(addr);
871 	uteof(addr);
872 	utwait(addr);
873 	if ((addr->utds&UTDS_ERR) || (addr->utcs1&UT_TRE))
874 		return(EIO);
875 	addr->utcs1 = UT_REW|UT_GO;
876 	return (0);
877 }
878 
879 utdwrite(dbuf, num, addr, up)
880 	register dbuf, num;
881 	register struct utdevice *addr;
882 	struct uba_regs *up;
883 {
884 	register struct pte *io;
885 	register int npf;
886 
887 	utwait(addr);
888 	io = up->uba_map;
889 	npf = num + 1;
890 	while (--npf != 0)
891 		*(int *)io++ = (dbuf++ | (1<<UBAMR_DPSHIFT) | UBAMR_MRV);
892 	*(int *)io = 0;
893 	addr->utwc = -((num*NBPG)>>1);
894 	addr->utfc = -(num*NBPG);
895 	addr->utba = 0;
896 	addr->utcs1 = UT_WCOM|UT_GO;
897 }
898 
899 utwait(addr)
900 	struct utdevice *addr;
901 {
902 	register s;
903 
904 	do
905 		s = addr->utds;
906 	while ((s&UTDS_DRY) == 0);
907 }
908 
909 uteof(addr)
910 	struct utdevice *addr;
911 {
912 
913 	utwait(addr);
914 	addr->utcs1 = UT_WEOF|UT_GO;
915 }
916 #endif
917