xref: /csrg-svn/sys/vax/uba/ts.c (revision 5438)
1 /*	ts.c	4.21	82/01/17	*/
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 	register int s;
324 
325 	bp = &ctsbuf[TSUNIT(dev)];
326 	s = 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 	splx(s);
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 	register int s;
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 	s = 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 	splx(s);
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 
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 		(void) 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