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