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