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