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