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