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