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