xref: /original-bsd/sys/vax/uba/ts.c (revision 6c57d260)
1 /*	ts.c	4.16	81/05/10	*/
2 
3 #include "ts.h"
4 #include "te.h"
5 #if NTS > 0
6 #if TSDEBUG
7 #define printd if(tsdebug)printf
8 int tsdebug;
9 #endif
10 /*
11  * TS11 tape driver
12  *
13  * TODO:
14  *	test driver with more than one controller
15  *	test reset code
16  *	test dump code
17  *	test rewinds without hanging in driver
18  *	what happens if you offline tape during rewind?
19  *	test using file system on tape
20  */
21 #include "../h/param.h"
22 #include "../h/systm.h"
23 #include "../h/buf.h"
24 #include "../h/dir.h"
25 #include "../h/conf.h"
26 #include "../h/user.h"
27 #include "../h/file.h"
28 #include "../h/map.h"
29 #include "../h/pte.h"
30 #include "../h/vm.h"
31 #include "../h/ubareg.h"
32 #include "../h/ubavar.h"
33 #include "../h/mtio.h"
34 #include "../h/ioctl.h"
35 #include "../h/cmap.h"
36 #include "../h/cpu.h"
37 
38 #include "../h/tsreg.h"
39 
40 /*
41  * There is a ctsbuf per tape controller.
42  * It is used as the token to pass to the internal routines
43  * to execute tape ioctls.
44  * In particular, when the tape is rewinding on close we release
45  * the user process but any further attempts to use the tape drive
46  * before the rewind completes will hang waiting for ctsbuf.
47  */
48 struct	buf	ctsbuf[NTS];
49 
50 /*
51  * Raw tape operations use rtsbuf.  The driver
52  * notices when rtsbuf is being used and allows the user
53  * program to continue after errors and read records
54  * not of the standard length (BSIZE).
55  */
56 struct	buf	rtsbuf[NTS];
57 
58 /*
59  * Driver unibus interface routines and variables.
60  */
61 int	tsprobe(), tsslave(), tsattach(), tsdgo(), tsintr();
62 struct	uba_ctlr *tsminfo[NTS];
63 struct	uba_device *tsdinfo[NTS];
64 struct buf	tsbuf[NTS];
65 u_short	tsstd[] = { 0772520, 0 };
66 /*** PROBABLY DON'T NEED ALL THESE SINCE CONTROLLER == DRIVE ***/
67 struct	uba_driver zsdriver =
68  { tsprobe, tsslave, tsattach, tsdgo, tsstd, "ts", tsdinfo, "zs", tsminfo, 0 };
69 
70 /* bits in minor device */
71 #define	TSUNIT(dev)	(minor(dev)&03)
72 #define	T_NOREWIND	04
73 
74 #define	INF	(daddr_t)1000000L
75 
76 /*
77  * Software state per tape transport.
78  * Also contains hardware state in message packets.
79  *
80  * 1. A tape drive is a unique-open device; we refuse opens when it is already.
81  * 2. We keep track of the current position on a block tape and seek
82  *    before operations by forward/back spacing if necessary.
83  * 3. We remember if the last operation was a write on a tape, so if a tape
84  *    is open read write and the last thing done is a write we can
85  *    write a standard end of tape mark (two eofs).
86  * 4. We remember the status registers after the last command, using
87  *    then internally and returning them to the SENSE ioctl.
88  */
89 struct	ts_softc {
90 	char	sc_openf;	/* lock against multiple opens */
91 	char	sc_lastiow;	/* last op was a write */
92 	short	sc_resid;	/* copy of last bc */
93 	daddr_t	sc_blkno;	/* block number, for block device tape */
94 	daddr_t	sc_nxrec;	/* position of end of tape, if known */
95 	struct ts_cmd sc_cmd;	/* the command packet - ADDR MUST BE 0 MOD 4 */
96 	struct ts_sts sc_sts;	/* status packet, for returned status */
97 	struct ts_char sc_char;	/* characteristics packet */
98 	u_short	sc_uba;		/* Unibus addr of cmd pkt for tsdb */
99 } ts_softc[NTS];
100 
101 struct ts_softc *ts_ubaddr;	/* Unibus address of ts_softc */
102 
103 /*
104  * States for um->um_tab.b_active, the per controller state flag.
105  * This is used to sequence control in the driver.
106  */
107 #define	SSEEK	1		/* seeking */
108 #define	SIO	2		/* doing seq i/o */
109 #define	SCOM	3		/* sending control command */
110 #define	SREW	4		/* sending a drive rewind */
111 
112 #if NTM > 0
113 /* kludge... see tm.c */
114 extern	havetm;
115 #endif
116 /*
117  * Determine if there is a controller for
118  * a ts at address reg.  Our goal is to make the
119  * device interrupt.
120  */
121 tsprobe(reg)
122 	caddr_t reg;
123 {
124 	register int br, cvec;		/* must be r11,r10; value-result */
125 
126 #ifdef lint
127 	br = 0; cvec = br; br = cvec;
128 #endif
129 	/****************/
130 	/*		*/
131 	/*  K L U D G E */
132 	/*		*/
133 	/****************/
134 
135 #if NTM > 0
136 	if (havetm)
137 		return (0);
138 #endif
139 	/* IT'S TOO HARD TO MAKE THIS THING INTERRUPT
140 	   JUST TO FIND ITS VECTOR */
141 	cvec = 0224;
142 	br = 0x15;
143 }
144 
145 /*
146  * TS11 only supports one drive per controller;
147  * check for ui_slave == 0.
148  *
149  * DO WE REALLY NEED THIS ROUTINE???
150  */
151 /*ARGSUSED*/
152 tsslave(ui, reg)
153 	struct uba_device *ui;
154 	caddr_t reg;
155 {
156 
157 	if (ui->ui_slave)	/* non-zero slave not allowed */
158 		return(0);
159 	return (1);
160 }
161 
162 /*
163  * Record attachment of the unit to the controller.
164  *
165  * SHOULD THIS ROUTINE DO ANYTHING???
166  */
167 /*ARGSUSED*/
168 tsattach(ui)
169 	struct uba_device *ui;
170 {
171 
172 }
173 
174 /*
175  * Open the device.  Tapes are unique open
176  * devices, so we refuse if it is already open.
177  * We also check that a tape is available, and
178  * don't block waiting here; if you want to wait
179  * for a tape you should timeout in user code.
180  */
181 tsopen(dev, flag)
182 	dev_t dev;
183 	int flag;
184 {
185 	register int tsunit;
186 	register struct uba_device *ui;
187 	register struct ts_softc *sc;
188 
189 	tsunit = TSUNIT(dev);
190 	if (tsunit>=NTS || (sc = &ts_softc[tsunit])->sc_openf ||
191 	    (ui = tsdinfo[tsunit]) == 0 || ui->ui_alive == 0) {
192 		u.u_error = ENXIO;
193 		return;
194 	}
195 	if (tsinit(tsunit)) {
196 		u.u_error = ENXIO;
197 #ifdef TSDEBUG
198 		printd("init failed\n");
199 #endif
200 		return;
201 	}
202 #ifdef TSDEBUG
203 	printd("init ok\n");
204 #endif
205 	tscommand(dev, TS_SENSE, 1);
206 #ifdef TSDEBUG
207 	printd("sense xs0 %o\n", sc->sc_sts.s_xs0);
208 #endif
209 	if ((sc->sc_sts.s_xs0&TS_ONL) == 0) {
210 		uprintf("ts%d: not online\n", tsunit);
211 		u.u_error = EIO;
212 		return;
213 	}
214 	if ((flag&(FREAD|FWRITE)) == FWRITE && (sc->sc_sts.s_xs0&TS_WLK)) {
215 		uprintf("ts%d: no write ring\n", tsunit);
216 		u.u_error = EIO;
217 		return;
218 	}
219 	sc->sc_openf = 1;
220 	sc->sc_blkno = (daddr_t)0;
221 	sc->sc_nxrec = INF;
222 	sc->sc_lastiow = 0;
223 }
224 
225 /*
226  * Close tape device.
227  *
228  * If tape was open for writing or last operation was
229  * a write, then write two EOF's and backspace over the last one.
230  * Unless this is a non-rewinding special file, rewind the tape.
231  * Make the tape available to others.
232  */
233 tsclose(dev, flag)
234 	register dev_t dev;
235 	register flag;
236 {
237 	register struct ts_softc *sc = &ts_softc[TSUNIT(dev)];
238 
239 	if (flag == FWRITE || (flag&FWRITE) && sc->sc_lastiow) {
240 		tscommand(dev, TS_WEOF, 1);
241 		tscommand(dev, TS_WEOF, 1);
242 		tscommand(dev, TS_SREV, 1);
243 	}
244 	if ((minor(dev)&T_NOREWIND) == 0)
245 		/*
246 		 * 0 count means don't hang waiting for rewind complete
247 		 * rather ctsbuf stays busy until the operation completes
248 		 * preventing further opens from completing by
249 		 * preventing a TS_SENSE from completing.
250 		 */
251 		tscommand(dev, TS_REW, 0);
252 	sc->sc_openf = 0;
253 }
254 
255 /*
256  * Initialize the TS11.  Set up Unibus mapping for command
257  * packets and set device characteristics.
258  */
259 tsinit(unit)
260 	register int unit;
261 {
262 	register struct ts_softc *sc = &ts_softc[unit];
263 	register struct uba_ctlr *um = tsminfo[unit];
264 	register struct device *addr = (struct device *)um->um_addr;
265 	register int i;
266 
267 	/*
268 	 * Map the command and message packets into Unibus
269 	 * address space.  We do all the command and message
270 	 * packets at once to minimize the amount of Unibus
271 	 * mapping necessary.
272 	 */
273 	if (ts_ubaddr == 0) {
274 		ctsbuf[unit].b_un.b_addr = (caddr_t)ts_softc;
275 		ctsbuf[unit].b_bcount = sizeof(ts_softc);
276 		i = ubasetup(um->um_ubanum, &ctsbuf[unit], 0);
277 		i &= 0777777;
278 		ts_ubaddr = (struct ts_softc *)i;
279 		/* MAKE SURE WE DON'T GET UNIBUS ADDRESS ZERO */
280 		if (ts_ubaddr == 0)
281 			printf("ts%d: zero ubaddr\n", unit);
282 	}
283 	/*
284 	 * Now initialize the TS11 controller.
285 	 * Set the characteristics.
286 	 */
287 	if (addr->tssr & (TS_NBA|TS_OFL)) {
288 		addr->tssr = 0;		/* subsystem initialize */
289 		tswait(addr);
290 		i = (int)&ts_ubaddr[unit].sc_cmd;	/* Unibus addr of cmd */
291 		if (i&3) {
292 			printf("addr mod 4 != 0\n");
293 			return(1);
294 		}
295 		sc->sc_uba = (u_short)(i + ((i>>16)&3));
296 		sc->sc_char.char_addr = (int)&ts_ubaddr[unit].sc_sts;
297 		sc->sc_char.char_size = sizeof(struct ts_sts);
298 		sc->sc_char.char_mode = TS_ESS;
299 		sc->sc_cmd.c_cmd = TS_ACK | TS_SETCHR;
300 		i = (int)&ts_ubaddr[unit].sc_char;
301 		sc->sc_cmd.c_loba = i;
302 		sc->sc_cmd.c_hiba = (i>>16)&3;
303 		sc->sc_cmd.c_size = sizeof(struct ts_char);
304 		addr->tsdb = sc->sc_uba;
305 		tswait(addr);
306 /*
307 		printd("%o %o %o %o %o %o %o %o\n", addr->tssr, sc->sc_sts.s_sts, sc->sc_sts.s_len, sc->sc_sts.s_rbpcr, sc->sc_sts.s_xs0, sc->sc_sts.s_xs1,sc->sc_sts.s_xs1,sc->sc_sts.s_xs2,sc->sc_sts.s_xs3);
308 */
309 		if (addr->tssr & TS_NBA)
310 			return(1);
311 	}
312 	return(0);
313 }
314 
315 /*
316  * Execute a command on the tape drive
317  * a specified number of times.
318  */
319 tscommand(dev, com, count)
320 	dev_t dev;
321 	int com, count;
322 {
323 	register struct buf *bp;
324 
325 	bp = &ctsbuf[TSUNIT(dev)];
326 	(void) spl5();
327 	while (bp->b_flags&B_BUSY) {
328 		/*
329 		 * This special check is because B_BUSY never
330 		 * gets cleared in the non-waiting rewind case.
331 		 */
332 		if (bp->b_repcnt == 0 && (bp->b_flags&B_DONE))
333 			break;
334 		bp->b_flags |= B_WANTED;
335 		sleep((caddr_t)bp, PRIBIO);
336 	}
337 	bp->b_flags = B_BUSY|B_READ;
338 	(void) spl0();
339 #ifdef TSDEBUG
340 	printd("command %o dev %x count %d\n", com, dev, count);
341 #endif
342 	bp->b_dev = dev;
343 	bp->b_repcnt = count;
344 	bp->b_command = com;
345 	bp->b_blkno = 0;
346 	tsstrategy(bp);
347 	/*
348 	 * In case of rewind from close, don't wait.
349 	 * This is the only case where count can be 0.
350 	 */
351 	if (count == 0)
352 		return;
353 	iowait(bp);
354 	if (bp->b_flags&B_WANTED)
355 		wakeup((caddr_t)bp);
356 	bp->b_flags &= B_ERROR;
357 }
358 
359 /*
360  * Queue a tape operation.
361  */
362 tsstrategy(bp)
363 	register struct buf *bp;
364 {
365 	int tsunit = TSUNIT(bp->b_dev);
366 	register struct uba_ctlr *um;
367 	register struct buf *dp;
368 
369 	/*
370 	 * Put transfer at end of controller queue
371 	 */
372 	bp->av_forw = NULL;
373 	um = tsdinfo[tsunit]->ui_mi;
374 	dp = &tsbuf[tsunit];
375 	(void) spl5();
376 	if (dp->b_actf == NULL)
377 		dp->b_actf = bp;
378 	else
379 		dp->b_actl->av_forw = bp;
380 	dp->b_actl = bp;
381 	um->um_tab.b_actf = um->um_tab.b_actl = dp;
382 	/*
383 	 * If the controller is not busy, get
384 	 * it going.
385 	 */
386 	if (um->um_tab.b_active == 0)
387 		tsstart(um);
388 	(void) spl0();
389 }
390 
391 /*
392  * Start activity on a ts controller.
393  */
394 tsstart(um)
395 	register struct uba_ctlr *um;
396 {
397 	register struct buf *bp;
398 	register struct device *addr = (struct device *)um->um_addr;
399 	register struct ts_softc *sc;
400 	register struct ts_cmd *tc;
401 	register struct uba_device *ui;
402 	int tsunit, cmd;
403 	daddr_t blkno;
404 
405 	/*
406 	 * Start the controller if there is something for it to do.
407 	 */
408 loop:
409 	if ((bp = um->um_tab.b_actf->b_actf) == NULL)
410 		return;
411 	tsunit = TSUNIT(bp->b_dev);
412 	ui = tsdinfo[tsunit];
413 	sc = &ts_softc[tsunit];
414 	tc = &sc->sc_cmd;
415 	/*
416 	 * Default is that last command was NOT a write command;
417 	 * if we do a write command we will notice this in tsintr().
418 	 */
419 	sc->sc_lastiow = 0;
420 	if (sc->sc_openf < 0 || (addr->tssr&TS_OFL)) {
421 		/*
422 		 * Have had a hard error on a non-raw tape
423 		 * or the tape unit is now unavailable
424 		 * (e.g. taken off line).
425 		 */
426 		bp->b_flags |= B_ERROR;
427 		goto next;
428 	}
429 	if (bp == &ctsbuf[TSUNIT(bp->b_dev)]) {
430 		/*
431 		 * Execute control operation with the specified count.
432 		 */
433 		um->um_tab.b_active =
434 		    bp->b_command == TS_REW ? SREW : SCOM;
435 		tc->c_repcnt = bp->b_repcnt;
436 #ifdef TSDEBUG
437 		printd("strat: do cmd\n");
438 #endif
439 		goto dobpcmd;
440 	}
441 	/*
442 	 * The following checks handle boundary cases for operation
443 	 * on non-raw tapes.  On raw tapes the initialization of
444 	 * sc->sc_nxrec by tsphys causes them to be skipped normally
445 	 * (except in the case of retries).
446 	 */
447 	if (dbtofsb(bp->b_blkno) > sc->sc_nxrec) {
448 		/*
449 		 * Can't read past known end-of-file.
450 		 */
451 		bp->b_flags |= B_ERROR;
452 		bp->b_error = ENXIO;
453 		goto next;
454 	}
455 	if (dbtofsb(bp->b_blkno) == sc->sc_nxrec &&
456 	    bp->b_flags&B_READ) {
457 		/*
458 		 * Reading at end of file returns 0 bytes.
459 		 */
460 		bp->b_resid = bp->b_bcount;
461 		clrbuf(bp);
462 		goto next;
463 	}
464 	if ((bp->b_flags&B_READ) == 0)
465 		/*
466 		 * Writing sets EOF
467 		 */
468 		sc->sc_nxrec = dbtofsb(bp->b_blkno) + 1;
469 	/*
470 	 * If the data transfer command is in the correct place,
471 	 * set up all the registers except the csr, and give
472 	 * control over to the UNIBUS adapter routines, to
473 	 * wait for resources to start the i/o.
474 	 */
475 	if ((blkno = sc->sc_blkno) == dbtofsb(bp->b_blkno)) {
476 		tc->c_size = bp->b_bcount;
477 		if ((bp->b_flags&B_READ) == 0)
478 			cmd = TS_WCOM;
479 		else
480 			cmd = TS_RCOM;
481 		if (um->um_tab.b_errcnt)
482 			cmd |= TS_RETRY;
483 		um->um_tab.b_active = SIO;
484 		tc->c_cmd = TS_ACK | TS_CVC | TS_IE | cmd;
485 #ifdef TSDEBUG
486 		printd("r/w %o size %d\n", tc->c_cmd, tc->c_size);
487 #endif
488 		(void) ubago(ui);
489 		return;
490 	}
491 	/*
492 	 * Tape positioned incorrectly;
493 	 * set to seek forwards or backwards to the correct spot.
494 	 * This happens for raw tapes only on error retries.
495 	 */
496 	um->um_tab.b_active = SSEEK;
497 #ifdef TSDEBUG
498 	printd("seek blkno %d b_blkno %d\n", blkno, bp->b_blkno);
499 #endif
500 	if (blkno < dbtofsb(bp->b_blkno)) {
501 		bp->b_command = TS_SFORW;
502 		tc->c_repcnt = dbtofsb(bp->b_blkno) - blkno;
503 	} else {
504 		bp->b_command = TS_SREV;
505 		tc->c_repcnt = blkno - dbtofsb(bp->b_blkno);
506 	}
507 dobpcmd:
508 	/*
509 	 * Do the command in bp.
510 	 */
511 	tc->c_cmd = TS_ACK | TS_CVC | TS_IE | bp->b_command;
512 	addr->tsdb = sc->sc_uba;
513 	return;
514 
515 next:
516 	/*
517 	 * Done with this operation due to error or
518 	 * the fact that it doesn't do anything.
519 	 * Release UBA resources (if any), dequeue
520 	 * the transfer and continue processing this slave.
521 	 */
522 	if (um->um_ubinfo)
523 		ubadone(um);
524 	um->um_tab.b_errcnt = 0;
525 	um->um_tab.b_actf->b_actf = bp->av_forw;
526 	iodone(bp);
527 	goto loop;
528 }
529 
530 /*
531  * The UNIBUS resources we needed have been
532  * allocated to us; start the device.
533  */
534 tsdgo(um)
535 	register struct uba_ctlr *um;
536 {
537 	register struct device *addr = (struct device *)um->um_addr;
538 	register struct ts_softc *sc = &ts_softc[um->um_ctlr];
539 	register int i;
540 
541 	i = um->um_ubinfo & 0777777;
542 #ifdef TSDEBUG
543 	printd("dgo addr %o\n", i);
544 #endif
545 	sc->sc_cmd.c_loba = i;
546 	sc->sc_cmd.c_hiba = (i>>16)&3;
547 	addr->tsdb = sc->sc_uba;
548 }
549 
550 /*
551  * Ts interrupt routine.
552  */
553 /*ARGSUSED*/
554 tsintr(ts11)
555 	int ts11;
556 {
557 	register struct buf *bp;
558 	register struct uba_ctlr *um = tsminfo[ts11];
559 	register struct device *addr;
560 	register struct ts_softc *sc;
561 	int tsunit;
562 	register state;
563 
564 #ifdef TSDEBUG
565 	printd("intr\n");
566 #endif
567 	if ((bp = um->um_tab.b_actf->b_actf) == NULL)
568 		return;
569 	tsunit = TSUNIT(bp->b_dev);
570 	addr = (struct device *)tsdinfo[tsunit]->ui_addr;
571 	/*
572 	 * If last command was a rewind, and tape is still
573 	 * rewinding, wait for the rewind complete interrupt.
574 	 *
575 	 * SHOULD NEVER GET AN INTERRUPT IN THIS STATE.
576 	 */
577 	if (um->um_tab.b_active == SREW) {
578 		um->um_tab.b_active = SCOM;
579 		if ((addr->tssr&TS_SSR) == 0)
580 			return;
581 	}
582 	/*
583 	 * An operation completed... record status
584 	 */
585 #ifdef TSDEBUG
586 	printd("  ok1\n");
587 #endif
588 	sc = &ts_softc[tsunit];
589 	if ((bp->b_flags & B_READ) == 0)
590 		sc->sc_lastiow = 1;
591 	state = um->um_tab.b_active;
592 	um->um_tab.b_active = 0;
593 	/*
594 	 * Check for errors.
595 	 */
596 	if (addr->tssr&TS_SC) {
597 		switch (addr->tssr & TS_TC) {
598 		case TS_UNREC:		/* unrecoverable */
599 		case TS_FATAL:		/* fatal error */
600 		case TS_ATTN:		/* attention (shouldn't happen) */
601 		case TS_RECNM:		/* recoverable, no motion */
602 			break;
603 
604 		case TS_SUCC:		/* success termination */
605 			printf("ts%d: success\n", TSUNIT(minor(bp->b_dev)));
606 			goto ignoreerr;
607 
608 		case TS_ALERT:		/* tape status alert */
609 			/*
610 			 * If we hit the end of the tape file,
611 			 * update our position.
612 			 */
613 			if (sc->sc_sts.s_xs0 & (TS_TMK|TS_EOT)) {
614 				tsseteof(bp);		/* set blkno and nxrec */
615 				state = SCOM;		/* force completion */
616 				/*
617 				 * Stuff bc so it will be unstuffed correctly
618 				 * later to get resid.
619 				 */
620 				sc->sc_sts.s_rbpcr = bp->b_bcount;
621 				goto opdone;
622 			}
623 			/*
624 			 * If we were reading raw tape and the record was too long
625 			 * or too short, then we don't consider this an error.
626 			 */
627 			if (bp == &rtsbuf[TSUNIT(bp->b_dev)] && (bp->b_flags&B_READ) &&
628 			    sc->sc_sts.s_xs0&(TS_RLS|TS_RLL))
629 				goto ignoreerr;
630 		case TS_RECOV:		/* recoverable, tape moved */
631 			/*
632 			 * If this was an i/o operation retry up to 8 times.
633 			 */
634 			if (state==SIO) {
635 				if (++um->um_tab.b_errcnt < 7) {
636 					ubadone(um);
637 					goto opcont;
638 				} else
639 					sc->sc_blkno++;
640 			} else {
641 				/*
642 				 * Non-i/o errors on non-raw tape
643 				 * cause it to close.
644 				 */
645 				if (sc->sc_openf>0 && bp != &rtsbuf[TSUNIT(bp->b_dev)])
646 					sc->sc_openf = -1;
647 			}
648 			break;
649 
650 		case TS_REJECT:		/* function reject */
651 			if (state == SIO && sc->sc_sts.s_xs0 & TS_WLE)
652 				printf("ts%d: write locked\n", TSUNIT(bp->b_dev));
653 			if ((sc->sc_sts.s_xs0 & TS_ONL) == 0)
654 				printf("ts%d: offline\n", TSUNIT(bp->b_dev));
655 			break;
656 		}
657 		/*
658 		 * Couldn't recover error
659 		 */
660 		printf("ts%d: hard error bn%d xs0=%b", TSUNIT(bp->b_dev),
661 		    bp->b_blkno, sc->sc_sts.s_xs0, TSXS0_BITS);
662 		if (sc->sc_sts.s_xs1)
663 			printf(" xs1=%b", sc->sc_sts.s_xs1, TSXS1_BITS);
664 		if (sc->sc_sts.s_xs2)
665 			printf(" xs2=%b", sc->sc_sts.s_xs2, TSXS2_BITS);
666 		if (sc->sc_sts.s_xs3)
667 			printf(" xs3=%b", sc->sc_sts.s_xs3, TSXS3_BITS);
668 		printf("\n");
669 		bp->b_flags |= B_ERROR;
670 		goto opdone;
671 	}
672 	/*
673 	 * Advance tape control FSM.
674 	 */
675 ignoreerr:
676 	switch (state) {
677 
678 	case SIO:
679 		/*
680 		 * Read/write increments tape block number
681 		 */
682 		sc->sc_blkno++;
683 		goto opdone;
684 
685 	case SCOM:
686 		/*
687 		 * For forward/backward space record update current position.
688 		 */
689 		if (bp == &ctsbuf[TSUNIT(bp->b_dev)])
690 		switch (bp->b_command) {
691 
692 		case TS_SFORW:
693 			sc->sc_blkno += bp->b_repcnt;
694 			break;
695 
696 		case TS_SREV:
697 			sc->sc_blkno -= bp->b_repcnt;
698 			break;
699 		}
700 		goto opdone;
701 
702 	case SSEEK:
703 		sc->sc_blkno = dbtofsb(bp->b_blkno);
704 		goto opcont;
705 
706 	default:
707 		panic("tsintr");
708 	}
709 opdone:
710 	/*
711 	 * Reset error count and remove
712 	 * from device queue.
713 	 */
714 	um->um_tab.b_errcnt = 0;
715 	um->um_tab.b_actf->b_actf = bp->av_forw;
716 	bp->b_resid = sc->sc_sts.s_rbpcr;
717 	ubadone(um);
718 #ifdef TSDEBUG
719 	printd("  iodone\n");
720 #endif
721 	iodone(bp);
722 	if (um->um_tab.b_actf->b_actf == 0)
723 		return;
724 opcont:
725 	tsstart(um);
726 }
727 
728 tsseteof(bp)
729 	register struct buf *bp;
730 {
731 	register int tsunit = TSUNIT(bp->b_dev);
732 	register struct ts_softc *sc = &ts_softc[tsunit];
733 
734 	if (bp == &ctsbuf[TSUNIT(bp->b_dev)]) {
735 		if (sc->sc_blkno > dbtofsb(bp->b_blkno)) {
736 			/* reversing */
737 			sc->sc_nxrec = dbtofsb(bp->b_blkno) - sc->sc_sts.s_rbpcr;
738 			sc->sc_blkno = sc->sc_nxrec;
739 		} else {
740 			/* spacing forward */
741 			sc->sc_blkno = dbtofsb(bp->b_blkno) + sc->sc_sts.s_rbpcr;
742 			sc->sc_nxrec = sc->sc_blkno - 1;
743 		}
744 		return;
745 	}
746 	/* eof on read */
747 	sc->sc_nxrec = dbtofsb(bp->b_blkno);
748 }
749 
750 tsread(dev)
751 	dev_t dev;
752 {
753 
754 	tsphys(dev);
755 	if (u.u_error)
756 		return;
757 	physio(tsstrategy, &rtsbuf[TSUNIT(dev)], dev, B_READ, minphys);
758 }
759 
760 tswrite(dev)
761 	dev_t dev;
762 {
763 
764 	tsphys(dev);
765 	if (u.u_error)
766 		return;
767 	physio(tsstrategy, &rtsbuf[TSUNIT(dev)], dev, B_WRITE, minphys);
768 }
769 
770 /*
771  * Check that a raw device exists.
772  * If it does, set up sc_blkno and sc_nxrec
773  * so that the tape will appear positioned correctly.
774  */
775 tsphys(dev)
776 	dev_t dev;
777 {
778 	register int tsunit = TSUNIT(dev);
779 	register daddr_t a;
780 	register struct ts_softc *sc;
781 	register struct uba_device *ui;
782 
783 	if (tsunit >= NTS || (ui=tsdinfo[tsunit]) == 0 || ui->ui_alive == 0) {
784 		u.u_error = ENXIO;
785 		return;
786 	}
787 	sc = &ts_softc[tsunit];
788 	a = dbtofsb(u.u_offset >> 9);
789 	sc->sc_blkno = a;
790 	sc->sc_nxrec = a + 1;
791 }
792 
793 tsreset(uban)
794 	int uban;
795 {
796 	register struct uba_ctlr *um;
797 	register ts11;
798 	register struct buf *dp;
799 
800 	for (ts11 = 0; ts11 < NTS; ts11++) {
801 		if ((um = tsminfo[ts11]) == 0 || um->um_alive == 0 ||
802 		   um->um_ubanum != uban)
803 			continue;
804 		printf(" ts%d", ts11);
805 		um->um_tab.b_active = 0;
806 		um->um_tab.b_actf = um->um_tab.b_actl = 0;
807 		ts_softc[ts11].sc_openf = -1;
808 		if (um->um_ubinfo) {
809 			printf("<%d>", (um->um_ubinfo>>28)&0xf);
810 			ubadone(um);
811 		}
812 		tsinit(ts11);
813 		tsstart(um);
814 	}
815 }
816 
817 /*ARGSUSED*/
818 tsioctl(dev, cmd, addr, flag)
819 	caddr_t addr;
820 	dev_t dev;
821 {
822 	int tsunit = TSUNIT(dev);
823 	register struct ts_softc *sc = &ts_softc[tsunit];
824 	register struct buf *bp = &ctsbuf[TSUNIT(dev)];
825 	register callcount;
826 	int fcount;
827 	struct mtop mtop;
828 	struct mtget mtget;
829 	/* we depend of the values and order of the MT codes here */
830 	static tsops[] =
831 	 {TS_WEOF,TS_SFORWF,TS_SREVF,TS_SFORW,TS_SREV,TS_REW,TS_OFFL,TS_SENSE};
832 
833 	switch (cmd) {
834 	case MTIOCTOP:	/* tape operation */
835 		if (copyin((caddr_t)addr, (caddr_t)&mtop, sizeof(mtop))) {
836 			u.u_error = EFAULT;
837 			return;
838 		}
839 		switch(mtop.mt_op) {
840 		case MTWEOF:
841 			callcount = mtop.mt_count;
842 			fcount = 1;
843 			break;
844 		case MTFSF: case MTBSF:
845 		case MTFSR: case MTBSR:
846 			callcount = 1;
847 			fcount = mtop.mt_count;
848 			break;
849 		case MTREW: case MTOFFL: case MTNOP:
850 			callcount = 1;
851 			fcount = 1;
852 			break;
853 		default:
854 			u.u_error = ENXIO;
855 			return;
856 		}
857 		if (callcount <= 0 || fcount <= 0) {
858 			u.u_error = ENXIO;
859 			return;
860 		}
861 		while (--callcount >= 0) {
862 			tscommand(dev, tsops[mtop.mt_op], fcount);
863 			if ((mtop.mt_op == MTFSR || mtop.mt_op == MTBSR) &&
864 			    bp->b_resid) {
865 				u.u_error = EIO;
866 				break;
867 			}
868 			if ((bp->b_flags&B_ERROR) || sc->sc_sts.s_xs0&TS_BOT)
869 				break;
870 		}
871 		geterror(bp);
872 		return;
873 	case MTIOCGET:
874 		mtget.mt_dsreg = 0;
875 		mtget.mt_erreg = sc->sc_sts.s_xs0;
876 		mtget.mt_resid = sc->sc_resid;
877 		mtget.mt_type = MT_ISTS;
878 		if (copyout((caddr_t)&mtget, addr, sizeof(mtget)))
879 			u.u_error = EFAULT;
880 		return;
881 	default:
882 		u.u_error = ENXIO;
883 	}
884 }
885 
886 #define	DBSIZE	20
887 
888 tsdump()
889 {
890 	register struct uba_device *ui;
891 	register struct uba_regs *up;
892 	register struct device *addr;
893 	int blk, num;
894 	int start;
895 
896 	start = 0;
897 	num = maxfree;
898 #define	phys(a,b)	((b)((int)(a)&0x7fffffff))
899 	if (tsdinfo[0] == 0)
900 		return (ENXIO);
901 	ui = phys(tsdinfo[0], struct uba_device *);
902 	up = phys(ui->ui_hd, struct uba_hd *)->uh_physuba;
903 	ubainit(up);
904 	DELAY(1000000);
905 	addr = (struct device *)ui->ui_physaddr;
906 	addr->tssr = 0;
907 	tswait(addr);
908 	while (num > 0) {
909 		blk = num > DBSIZE ? DBSIZE : num;
910 		tsdwrite(start, blk, addr, up);
911 		start += blk;
912 		num -= blk;
913 	}
914 	tseof(addr);
915 	tseof(addr);
916 	tswait(addr);
917 	if (addr->tssr&TS_SC)
918 		return (EIO);
919 	addr->tssr = 0;
920 	tswait(addr);
921 	return (0);
922 }
923 
924 tsdwrite(dbuf, num, addr, up)
925 	register dbuf, num;
926 	register struct device *addr;
927 	struct uba_regs *up;
928 {
929 	register struct pte *io;
930 	register int npf;
931 
932 	tswait(addr);
933 	io = up->uba_map;
934 	npf = num+1;
935 	while (--npf != 0)
936 		 *(int *)io++ = (dbuf++ | (1<<UBAMR_DPSHIFT) | UBAMR_MRV);
937 	*(int *)io = 0;
938 #ifdef notyet
939 	addr->tsbc = -(num*NBPG);
940 	addr->tsba = 0;
941 	addr->tscs = TS_WCOM | TM_GO;
942 #endif
943 }
944 
945 tswait(addr)
946 	register struct device *addr;
947 {
948 	register s;
949 
950 	do
951 		s = addr->tssr;
952 	while ((s & TS_SSR) == 0);
953 }
954 
955 tseof(addr)
956 	struct device *addr;
957 {
958 
959 	tswait(addr);
960 #ifdef notyet
961 	addr->tscs = TS_WEOF | TM_GO;
962 #endif
963 }
964 #endif
965