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