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