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