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