xref: /csrg-svn/sys/vax/uba/ts.c (revision 44396)
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.12 (Berkeley) 06/28/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 "file.h"
25 #include "map.h"
26 #include "vm.h"
27 #include "ioctl.h"
28 #include "mtio.h"
29 #include "cmap.h"
30 #include "uio.h"
31 #include "syslog.h"
32 #include "tprintf.h"
33 
34 #include "machine/pte.h"
35 #include "../vax/cpu.h"
36 #include "ubareg.h"
37 #include "ubavar.h"
38 #include "tsreg.h"
39 
40 /*
41  * There is a ctsbuf per tape controller.
42  * It is used as the token to pass to the internal routines
43  * to execute tape ioctls.
44  * In particular, when the tape is rewinding on close we release
45  * the user process but any further attempts to use the tape drive
46  * before the rewind completes will hang waiting for ctsbuf.
47  */
48 struct	buf	ctsbuf[NTS];
49 
50 /*
51  * Driver unibus interface routines and variables.
52  */
53 int	tsprobe(), tsslave(), tsattach(), tsdgo(), tsintr();
54 struct	uba_ctlr *tsminfo[NTS];
55 struct	uba_device *tsdinfo[NTS];
56 struct buf	tsutab[NTS];
57 u_short	tsstd[] = { 0772520, 0 };
58 /* need all these even though controller == drive */
59 struct	uba_driver zsdriver =
60  { tsprobe, tsslave, tsattach, tsdgo, tsstd, "ts", tsdinfo, "zs", tsminfo };
61 
62 /* bits in minor device */
63 #define	TSUNIT(dev)	(minor(dev)&03)
64 #define	T_NOREWIND	04
65 #define	T_1600BPI	010
66 
67 #define	INF	(daddr_t)1000000L
68 
69 /*
70  * Software state per tape transport.
71  * Also contains hardware state in message packets.
72  *
73  * 1. A tape drive is a unique-open device; we refuse opens when it is already.
74  * 2. We keep track of the current position on a block tape and seek
75  *    before operations by forward/back spacing if necessary.
76  * 3. We remember if the last operation was a write on a tape, so if a tape
77  *    is open read write and the last thing done is a write we can
78  *    write a standard end of tape mark (two eofs).
79  * 4. We remember the status registers after the last command, using
80  *    then internally and returning them to the SENSE ioctl.
81  */
82 struct	ts_tsdata {		/* data shared with ts11 controller */
83 	struct	ts_cmd t_cmd;	/* the command packet (must be first) */
84 	struct	ts_sts t_sts;	/* status packet, for returned status */
85 	struct	ts_char t_char;	/* characteristics packet */
86 };
87 struct	ts_softc {
88 	char	sc_openf;	/* lock against multiple opens */
89 	char	sc_lastiow;	/* last op was a write */
90 	short	sc_resid;	/* copy of last bc */
91 	daddr_t	sc_blkno;	/* block number, for block device tape */
92 	daddr_t	sc_nxrec;	/* position of end of tape, if known */
93 	struct	ts_tsdata sc_ts;/* command and status packets */
94 	struct	ts_tsdata *sc_ubaddr; /* Unibus address of tsdata structure */
95 	u_short	sc_uba;		/* Unibus addr of cmd pkt for tsdb */
96 	short	sc_density;	/* value |'ed into char_mode for TC13 density */
97 	tpr_t	sc_tpr;		/* tprintf handle */
98 	int	sc_blks;	/* number of I/O operations since open */
99 	int	sc_softerrs;	/* number of soft I/O errors since open */
100 } ts_softc[NTS];
101 
102 /*
103  * States for um->um_tab.b_active, the per controller state flag.
104  * This is used to sequence control in the driver.
105  */
106 #define	SSEEK	1		/* seeking */
107 #define	SIO	2		/* doing seq i/o */
108 #define	SCOM	3		/* sending control command */
109 #define	SREW	4		/* sending a drive rewind */
110 
111 /*
112  * Determine if there is a controller for
113  * a ts at address reg.  Our goal is to make the
114  * device interrupt.
115  */
116 /*ARGSUSED*/
117 tsprobe(reg, ctlr, um)
118 	caddr_t reg;
119 	int ctlr;
120 	struct uba_ctlr *um;
121 {
122 	register int br, cvec;		/* must be r11,r10; value-result */
123 	register struct tsdevice *addr = (struct tsdevice *)reg;
124 	register struct ts_softc *sc;
125 	register int i;
126 	int a;
127 
128 #ifdef lint
129 	br = 0; cvec = br; br = cvec;
130 	tsintr(0);
131 #endif
132 	addr->tssr = 0;		/* initialize subsystem */
133 	DELAY(100);
134 	if ((addr->tssr & TS_NBA) == 0)
135 		return (0);
136 
137 	/*
138 	 * Make it interrupt.
139 	 * TS_SETCHR|TS_IE alone refuses to interrupt for me.
140 	 */
141 	sc = &ts_softc[ctlr];
142 	tsmap(sc, numuba, &a);
143 	i = (int)&sc->sc_ubaddr->t_char;
144 	sc->sc_ts.t_cmd.c_loba = i;
145 	sc->sc_ts.t_cmd.c_hiba = (i >> 16) & 3;
146 	sc->sc_ts.t_cmd.c_size = sizeof(struct ts_char);
147 	sc->sc_ts.t_cmd.c_cmd = TS_ACK | TS_SETCHR;
148 	sc->sc_ts.t_char.char_addr = (int)&sc->sc_ubaddr->t_sts;
149 	sc->sc_ts.t_char.char_size = sizeof(struct ts_sts);
150 	sc->sc_ts.t_char.char_mode = 0;	/* mode is unimportant */
151 	addr->tsdb = sc->sc_uba;
152 	DELAY(20000);
153 	sc->sc_ts.t_cmd.c_cmd = TS_ACK | TS_CVC | TS_IE | TS_SENSE;
154 	sc->sc_ts.t_cmd.c_repcnt = 1;
155 	addr->tsdb = sc->sc_uba;
156 	DELAY(20000);
157 	/* should have interrupted by now */
158 
159 	if (cvec == 0 || cvec == 0x200)	/* no interrupt */
160 		ubarelse(numuba, &a);
161 
162 	return (sizeof (struct tsdevice));
163 }
164 
165 /*
166  * Map in the command, status, and characteristics packet.  We
167  * make them contiguous to keep overhead down.  This also sets
168  * sc_uba (which then never changes).
169  */
170 tsmap(sc, uban, a)
171 	register struct ts_softc *sc;
172 	int uban, *a;
173 {
174 	register int i;
175 
176 	i = uballoc(uban, (caddr_t)&sc->sc_ts, sizeof(sc->sc_ts), 0);
177 	if (a != NULL)
178 		*a = i;
179 	i = UBAI_ADDR(i);
180 	sc->sc_ubaddr = (struct ts_tsdata *)i;
181 	/*
182 	 * Note that i == the Unibus address of the command packet,
183 	 * and that it is a multiple of 4 (guaranteed by the compiler).
184 	 */
185 	sc->sc_uba = i + ((i >> 16) & 3);
186 }
187 
188 /*
189  * TS11 only supports one drive per controller;
190  * check for ui_slave == 0.
191  */
192 /*ARGSUSED*/
193 tsslave(ui, reg)
194 	struct uba_device *ui;
195 	caddr_t reg;
196 {
197 
198 	return (ui->ui_slave == 0);	/* non-zero slave not allowed */
199 }
200 
201 /*
202  * Record attachment of the unit to the controller.
203  */
204 /*ARGSUSED*/
205 tsattach(ui)
206 	struct uba_device *ui;
207 {
208 
209 	/* void */
210 }
211 
212 /*
213  * Open the device.  Tapes are unique open
214  * devices, so we refuse if it is already open.
215  */
216 tsopen(dev, flag)
217 	dev_t dev;
218 	int flag;
219 {
220 	register int tsunit = TSUNIT(dev);
221 	register struct uba_device *ui;
222 	register struct ts_softc *sc;
223 
224 	if (tsunit >= NTS || (ui = tsdinfo[tsunit]) == 0 || ui->ui_alive == 0)
225 		return (ENXIO);
226 	if ((sc = &ts_softc[ui->ui_ctlr])->sc_openf)
227 		return (EBUSY);
228 	sc->sc_openf = 1;
229 	sc->sc_density = (minor(dev) & T_1600BPI) ? TS_NRZI : 0;
230 	tscommand(dev, TS_SENSE, 1);
231 	if (ctsbuf[tsunit].b_flags & B_ERROR)
232 		/*
233 		 * Try it again in case it went off-line
234 		 */
235 		tscommand(dev, TS_SENSE, 1);
236 	if (tsinit(ui->ui_ctlr)) {
237 		sc->sc_openf = 0;
238 		return (ENXIO);
239 	}
240 	if ((sc->sc_ts.t_sts.s_xs0&TS_ONL) == 0) {
241 		sc->sc_openf = 0;
242 		uprintf("ts%d: not online\n", tsunit);
243 		return (EIO);
244 	}
245 	if ((flag&FWRITE) && (sc->sc_ts.t_sts.s_xs0&TS_WLK)) {
246 		sc->sc_openf = 0;
247 		uprintf("ts%d: no write ring\n", tsunit);
248 		return (EIO);
249 	}
250 	sc->sc_blkno = (daddr_t)0;
251 	sc->sc_nxrec = INF;
252 	sc->sc_lastiow = 0;
253 	sc->sc_blks = 0;
254 	sc->sc_softerrs = 0;
255 	sc->sc_tpr = tprintf_open();
256 	return (0);
257 }
258 
259 /*
260  * Close tape device.
261  *
262  * If tape was open for writing or last operation was
263  * a write, then write two EOF's and backspace over the last one.
264  * Unless this is a non-rewinding special file, rewind the tape.
265  * Make the tape available to others.
266  */
267 tsclose(dev, flag)
268 	register dev_t dev;
269 	register int flag;
270 {
271 	register struct ts_softc *sc = &ts_softc[TSUNIT(dev)];
272 
273 	if (flag == FWRITE || (flag&FWRITE) && sc->sc_lastiow) {
274 		tscommand(dev, TS_WEOF, 1);
275 		tscommand(dev, TS_WEOF, 1);
276 		tscommand(dev, TS_SREV, 1);
277 	}
278 	if ((minor(dev)&T_NOREWIND) == 0)
279 		/*
280 		 * 0 count means don't hang waiting for rewind complete
281 		 * rather ctsbuf stays busy until the operation completes
282 		 * preventing further opens from completing by
283 		 * preventing a TS_SENSE from completing.
284 		 */
285 		tscommand(dev, TS_REW, 0);
286 	if (sc->sc_blks > 100 && sc->sc_softerrs > sc->sc_blks / 100)
287 		log(LOG_INFO, "ts%d: %d soft errors in %d blocks\n",
288 		    TSUNIT(dev), sc->sc_softerrs, sc->sc_blks);
289 	tprintf_close(sc->sc_tpr);
290 	sc->sc_openf = 0;
291 	return (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_tpr, "ts%d: write locked\n",
672 				    tsunit);
673 			if ((sc->sc_ts.t_sts.s_xs0 & TS_ONL) == 0)
674 				tprintf(sc->sc_tpr, "ts%d: offline\n",
675 				    tsunit);
676 			break;
677 		}
678 		/*
679 		 * Couldn't recover error
680 		 */
681 		tprintf(sc->sc_tpr, "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_tpr, " 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_tpr, " 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_tpr, " xs3=%b", sc->sc_ts.t_sts.s_xs3,
692 			    TSXS3_BITS);
693 		tprintf(sc->sc_tpr, "\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, error = 0;
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 		if (bp->b_flags&B_ERROR)
870 			if ((error = bp->b_error)==0)
871 				return (EIO);
872 		return (error);
873 
874 	case MTIOCGET:
875 		mtget = (struct mtget *)data;
876 		mtget->mt_dsreg = 0;
877 		mtget->mt_erreg = sc->sc_ts.t_sts.s_xs0;
878 		mtget->mt_resid = sc->sc_resid;
879 		mtget->mt_type = MT_ISTS;
880 		break;
881 
882 	default:
883 		return (ENXIO);
884 	}
885 	return (0);
886 }
887 
888 #define	DBSIZE	20
889 
890 tsdump(dev)
891 	dev_t dev;
892 {
893 	register struct uba_device *ui;
894 	register struct uba_regs *uba;
895 	register struct tsdevice *addr;
896 	register int i;
897 	register struct pte *io;
898 	int blk, num, unit, reg, start;
899 	u_short db;
900 	struct ts_tsdata *tc, *tc_ubaddr;
901 
902 	unit = TSUNIT(dev);
903 	if (unit >= NTS)
904 		return (ENXIO);
905 #define	phys(a,b)	((b)((int)(a)&0x7fffffff))
906 	ui = phys(tsdinfo[unit], struct uba_device *);
907 	if (ui->ui_alive == 0)
908 		return (ENXIO);
909 	uba = phys(ui->ui_hd, struct uba_hd *)->uh_physuba;
910 	ubainit(uba);
911 	addr = (struct tsdevice *)ui->ui_physaddr;
912 
913 	/* map a ts_tsdata structure */
914 	tc = phys(&ts_softc[0].sc_ts, struct ts_tsdata *);
915 	num = btoc(sizeof(struct ts_tsdata)) + 1;
916 	io = &uba->uba_map[reg = NUBMREG - num];
917 	for (i = 0; i < num; i++)
918 		*(int *)io++ = UBAMR_MRV | (btop(tc) + i);
919 	i = (((int)tc & PGOFSET) | (reg << 9));
920 	tc_ubaddr = (struct ts_tsdata *)i;
921 	db = i + ((i >> 16) & 3);
922 
923 	/* init the drive */
924 	addr->tssr = 0;
925 	tswait(addr);
926 	if ((addr->tssr & (TS_NBA|TS_OFL)) != TS_NBA)
927 		return (EFAULT);
928 
929 	/* set characteristics */
930 	i = (int)&tc_ubaddr->t_char;
931 	tc->t_cmd.c_loba = i;
932 	tc->t_cmd.c_hiba = (i >> 16) & 3;
933 	tc->t_cmd.c_size = sizeof(struct ts_char);
934 	tc->t_cmd.c_cmd = TS_ACK | TS_CVC | TS_SETCHR;
935 	tc->t_char.char_addr = (int)&tc_ubaddr->t_sts;
936 	tc->t_char.char_size = sizeof(struct ts_sts);
937 	tc->t_char.char_mode = TS_ESS;
938 	addr->tsdb = db;
939 	tswait(addr);
940 	if (addr->tssr & TS_NBA)
941 		return (ENXIO);
942 
943 	/* dump */
944 	tc->t_cmd.c_cmd = TS_ACK | TS_WCOM;
945 	tc->t_cmd.c_repcnt = 1;
946 	num = maxfree;
947 	for (start = 0, num = maxfree; num > 0; start += blk, num -= blk) {
948 		blk = num > DBSIZE ? DBSIZE : num;
949 		io = uba->uba_map;
950 		for (i = 0; i < blk; i++)
951 			*(int *)io++ = UBAMR_MRV | (1 << UBAMR_DPSHIFT) |
952 				(start + i);
953 		*(int *)io = 0;
954 		addr->tsdb = db;
955 		tswait(addr);
956 	}
957 
958 	/* eof */
959 	tc->t_cmd.c_cmd = TS_WEOF;
960 	addr->tsdb = db;
961 	tswait(addr);
962 	addr->tsdb = db;
963 	tswait(addr);
964 
965 	if (addr->tssr&TS_SC)
966 		return (EIO);
967 	addr->tssr = 0;
968 	tswait(addr);
969 	return (0);
970 }
971 
972 tswait(addr)
973 	register struct tsdevice *addr;
974 {
975 
976 	while ((addr->tssr & TS_SSR) == 0)
977 		/* void */;
978 }
979 
980 #endif /* NTS > 0 */
981