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