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