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