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