xref: /original-bsd/sys/vax/uba/ts.c (revision 4b9b56dc)
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.11 (Berkeley) 04/12/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 "user.h"
25 #include "proc.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 	caddr_t	sc_ctty;	/* user's controlling tty (vnode) */
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_ctty = (caddr_t)(u.u_procp->p_flag&SCTTY ?
258 			u.u_procp->p_session->s_ttyvp : 0);
259 	return (0);
260 }
261 
262 /*
263  * Close tape device.
264  *
265  * If tape was open for writing or last operation was
266  * a write, then write two EOF's and backspace over the last one.
267  * Unless this is a non-rewinding special file, rewind the tape.
268  * Make the tape available to others.
269  */
270 tsclose(dev, flag)
271 	register dev_t dev;
272 	register int flag;
273 {
274 	register struct ts_softc *sc = &ts_softc[TSUNIT(dev)];
275 
276 	if (flag == FWRITE || (flag&FWRITE) && sc->sc_lastiow) {
277 		tscommand(dev, TS_WEOF, 1);
278 		tscommand(dev, TS_WEOF, 1);
279 		tscommand(dev, TS_SREV, 1);
280 	}
281 	if ((minor(dev)&T_NOREWIND) == 0)
282 		/*
283 		 * 0 count means don't hang waiting for rewind complete
284 		 * rather ctsbuf stays busy until the operation completes
285 		 * preventing further opens from completing by
286 		 * preventing a TS_SENSE from completing.
287 		 */
288 		tscommand(dev, TS_REW, 0);
289 	if (sc->sc_blks > 100 && sc->sc_softerrs > sc->sc_blks / 100)
290 		log(LOG_INFO, "ts%d: %d soft errors in %d blocks\n",
291 		    TSUNIT(dev), sc->sc_softerrs, sc->sc_blks);
292 	sc->sc_openf = 0;
293 	return (0);
294 }
295 
296 /*
297  * Initialize a TS11.  Set device characteristics.
298  */
299 tsinit(ctlr)
300 	register int ctlr;
301 {
302 	register struct ts_softc *sc = &ts_softc[ctlr];
303 	register struct uba_ctlr *um = tsminfo[ctlr];
304 	register struct tsdevice *addr = (struct tsdevice *)um->um_addr;
305 	register int i;
306 
307 	if (addr->tssr & (TS_NBA|TS_OFL) || sc->sc_ts.t_sts.s_xs0 & TS_BOT) {
308 		addr->tssr = 0;		/* subsystem initialize */
309 		tswait(addr);
310 		i = (int)&sc->sc_ubaddr->t_char;
311 		sc->sc_ts.t_cmd.c_loba = i;
312 		sc->sc_ts.t_cmd.c_hiba = (i >> 16) & 3;
313 		sc->sc_ts.t_cmd.c_size = sizeof(struct ts_char);
314 		sc->sc_ts.t_cmd.c_cmd = TS_ACK | TS_CVC | TS_SETCHR;
315 		sc->sc_ts.t_char.char_addr = (int)&sc->sc_ubaddr->t_sts;
316 		sc->sc_ts.t_char.char_size = sizeof(struct ts_sts);
317 		sc->sc_ts.t_char.char_mode = TS_ESS | TS_EAI | TS_ERI |
318 			/* TS_ENB | */ sc->sc_density;
319 		addr->tsdb = sc->sc_uba;
320 		tswait(addr);
321 		if (addr->tssr & TS_NBA)
322 			return(1);
323 	}
324 	return(0);
325 }
326 
327 /*
328  * Execute a command on the tape drive
329  * a specified number of times.
330  */
331 tscommand(dev, com, count)
332 	dev_t dev;
333 	int com, count;
334 {
335 	register struct buf *bp;
336 	register int s;
337 	int didsleep = 0;
338 
339 	bp = &ctsbuf[TSUNIT(dev)];
340 	s = spl5();
341 	while (bp->b_flags&B_BUSY) {
342 		/*
343 		 * This special check is because B_BUSY never
344 		 * gets cleared in the non-waiting rewind case.
345 		 */
346 		if (bp->b_repcnt == 0 && (bp->b_flags&B_DONE))
347 			break;
348 		bp->b_flags |= B_WANTED;
349 		sleep((caddr_t)bp, PRIBIO);
350 		didsleep = 1;
351 	}
352 	bp->b_flags = B_BUSY|B_READ;
353 	splx(s);
354 	if (didsleep)
355 		(void) tsinit(tsdinfo[TSUNIT(dev)]->ui_ctlr);
356 	bp->b_dev = dev;
357 	bp->b_repcnt = count;
358 	bp->b_command = com;
359 	bp->b_blkno = 0;
360 	tsstrategy(bp);
361 	/*
362 	 * In case of rewind from close, don't wait.
363 	 * This is the only case where count can be 0.
364 	 */
365 	if (count == 0)
366 		return;
367 	biowait(bp);
368 	if (bp->b_flags&B_WANTED)
369 		wakeup((caddr_t)bp);
370 	bp->b_flags &= B_ERROR;
371 }
372 
373 /*
374  * Queue a tape operation.
375  */
376 tsstrategy(bp)
377 	register struct buf *bp;
378 {
379 	register int tsunit = TSUNIT(bp->b_dev);
380 	register struct uba_ctlr *um;
381 	register struct buf *dp;
382 	int s;
383 
384 	/*
385 	 * Put transfer at end of controller queue
386 	 */
387 	bp->av_forw = NULL;
388 	um = tsdinfo[tsunit]->ui_mi;
389 	dp = &tsutab[tsunit];
390 	s = spl5();
391 	if (dp->b_actf == NULL)
392 		dp->b_actf = bp;
393 	else
394 		dp->b_actl->av_forw = bp;
395 	dp->b_actl = bp;
396 	um->um_tab.b_actf = um->um_tab.b_actl = dp;
397 	/*
398 	 * If the controller is not busy, get
399 	 * it going.
400 	 */
401 	if (um->um_tab.b_active == 0)
402 		tsstart(um);
403 	splx(s);
404 }
405 
406 /*
407  * Start activity on a ts controller.
408  */
409 tsstart(um)
410 	register struct uba_ctlr *um;
411 {
412 	register struct buf *bp;
413 	register struct tsdevice *addr = (struct tsdevice *)um->um_addr;
414 	register struct ts_softc *sc;
415 	register struct uba_device *ui;
416 	register int tsunit;
417 	int cmd;
418 	daddr_t blkno;
419 
420 	/*
421 	 * Start the controller if there is something for it to do.
422 	 */
423 loop:
424 	if ((bp = um->um_tab.b_actf->b_actf) == NULL) {
425 		um->um_tab.b_active = 0;
426 		return;
427 	}
428 	tsunit = TSUNIT(bp->b_dev);
429 	ui = tsdinfo[tsunit];
430 	sc = &ts_softc[tsunit];
431 	/*
432 	 * Default is that last command was NOT a write command;
433 	 * if we finish a write command we will notice this in tsintr().
434 	 */
435 	sc->sc_lastiow = 0;
436 	if (sc->sc_openf < 0 || (addr->tssr&TS_OFL)) {
437 		/*
438 		 * Have had a hard error on a non-raw tape
439 		 * or the tape unit is now unavailable
440 		 * (e.g. taken off line).
441 		 */
442 		bp->b_flags |= B_ERROR;
443 		goto next;
444 	}
445 	if (bp == &ctsbuf[tsunit]) {
446 		/*
447 		 * Execute control operation with the specified count.
448 		 */
449 		um->um_tab.b_active =
450 		    bp->b_command == TS_REW ? SREW : SCOM;
451 		sc->sc_ts.t_cmd.c_repcnt = bp->b_repcnt;
452 		goto dobpcmd;
453 	}
454 	/*
455 	 * For raw I/O, save the current block
456 	 * number in case we have to retry.
457 	 */
458 	if (bp->b_flags & B_RAW) {
459 		if (um->um_tab.b_errcnt == 0)
460 			sc->sc_blkno = bdbtofsb(bp->b_blkno);
461 	} else {
462 		/*
463 		 * Handle boundary cases for operation
464 		 * on non-raw tapes.
465 		 */
466 		if (bdbtofsb(bp->b_blkno) > sc->sc_nxrec) {
467 			/*
468 			 * Can't read past known end-of-file.
469 			 */
470 			bp->b_flags |= B_ERROR;
471 			bp->b_error = ENXIO;
472 			goto next;
473 		}
474 		if (bdbtofsb(bp->b_blkno) == sc->sc_nxrec &&
475 		    bp->b_flags&B_READ) {
476 			/*
477 			 * Reading at end of file returns 0 bytes.
478 			 */
479 			bp->b_resid = bp->b_bcount;
480 			clrbuf(bp);
481 			goto next;
482 		}
483 		if ((bp->b_flags&B_READ) == 0)
484 			/*
485 			 * Writing sets EOF
486 			 */
487 			sc->sc_nxrec = bdbtofsb(bp->b_blkno) + 1;
488 	}
489 
490 	/*
491 	 * If the data transfer command is in the correct place,
492 	 * set up all the registers except the csr, and give
493 	 * control over to the UNIBUS adapter routines, to
494 	 * wait for resources to start the i/o.
495 	 */
496 	if ((blkno = sc->sc_blkno) == bdbtofsb(bp->b_blkno)) {
497 		sc->sc_ts.t_cmd.c_size = bp->b_bcount;
498 		if ((bp->b_flags&B_READ) == 0)
499 			cmd = TS_WCOM;
500 		else
501 			cmd = TS_RCOM;
502 		if (um->um_tab.b_errcnt)
503 			cmd |= TS_RETRY;
504 		um->um_tab.b_active = SIO;
505 		sc->sc_ts.t_cmd.c_cmd = TS_ACK | TS_CVC | TS_IE | cmd;
506 		(void) ubago(ui);
507 		return;
508 	}
509 	/*
510 	 * Tape positioned incorrectly;
511 	 * set to seek forwards or backwards to the correct spot.
512 	 * This happens for raw tapes only on error retries.
513 	 */
514 	um->um_tab.b_active = SSEEK;
515 	if (blkno < bdbtofsb(bp->b_blkno)) {
516 		bp->b_command = TS_SFORW;
517 		sc->sc_ts.t_cmd.c_repcnt = bdbtofsb(bp->b_blkno) - blkno;
518 	} else {
519 		bp->b_command = TS_SREV;
520 		sc->sc_ts.t_cmd.c_repcnt = blkno - bdbtofsb(bp->b_blkno);
521 	}
522 dobpcmd:
523 	/*
524 	 * Do the command in bp.
525 	 */
526 	sc->sc_ts.t_cmd.c_cmd = TS_ACK | TS_CVC | TS_IE | bp->b_command;
527 	addr->tsdb = sc->sc_uba;
528 	return;
529 
530 next:
531 	/*
532 	 * Done with this operation due to error or
533 	 * the fact that it doesn't do anything.
534 	 * Release UBA resources (if any), dequeue
535 	 * the transfer and continue processing this slave.
536 	 */
537 	if (um->um_ubinfo)
538 		ubadone(um);
539 	um->um_tab.b_errcnt = 0;
540 	um->um_tab.b_actf->b_actf = bp->av_forw;
541 	biodone(bp);
542 	goto loop;
543 }
544 
545 /*
546  * The UNIBUS resources we needed have been
547  * allocated to us; start the device.
548  */
549 tsdgo(um)
550 	register struct uba_ctlr *um;
551 {
552 	register struct ts_softc *sc = &ts_softc[um->um_ctlr];
553 	register int i;
554 
555 	/*
556 	 * The uba code uses byte-offset mode if using bdp;
557 	 * mask off the low bit here.
558 	 */
559 	i = UBAI_ADDR(um->um_ubinfo);
560 	if (UBAI_BDP(um->um_ubinfo))
561 		i &= ~1;
562 	sc->sc_ts.t_cmd.c_loba = i;
563 	sc->sc_ts.t_cmd.c_hiba = (i >> 16) & 3;
564 	((struct tsdevice *)um->um_addr)->tsdb = sc->sc_uba;
565 }
566 
567 /*
568  * Ts interrupt routine.
569  */
570 /*ARGSUSED*/
571 tsintr(tsunit)
572 	register int tsunit;
573 {
574 	register struct buf *bp;
575 	register struct uba_ctlr *um;
576 	register struct tsdevice *addr;
577 	register struct ts_softc *sc;
578 	register int state;
579 
580 #ifdef QBA
581 	(void) spl5();
582 #endif
583 	um = tsdinfo[tsunit]->ui_mi;
584 	if ((bp = um->um_tab.b_actf->b_actf) == NULL)
585 		return;
586 	addr = (struct tsdevice *)um->um_addr;
587 	/*
588 	 * If last command was a rewind, and tape is still
589 	 * rewinding, wait for the rewind complete interrupt.
590 	 *
591 	 * SHOULD NEVER GET AN INTERRUPT IN THIS STATE.
592 	 */
593 	if (um->um_tab.b_active == SREW) {
594 		um->um_tab.b_active = SCOM;
595 		if ((addr->tssr&TS_SSR) == 0)
596 			return;
597 	}
598 	/*
599 	 * An operation completed... record status
600 	 */
601 	sc = &ts_softc[um->um_ctlr];
602 	if ((bp->b_flags & B_READ) == 0)
603 		sc->sc_lastiow = 1;
604 	state = um->um_tab.b_active;
605 	/*
606 	 * Check for errors.
607 	 */
608 	if (addr->tssr&TS_SC) {
609 		switch (addr->tssr & TS_TC) {
610 		case TS_UNREC:		/* unrecoverable */
611 		case TS_FATAL:		/* fatal error */
612 		case TS_RECNM:		/* recoverable, no motion */
613 			break;
614 		case TS_ATTN:		/* attention */
615 			if (sc->sc_ts.t_sts.s_xs0 & TS_VCK) {
616 				/* volume check - may have changed tapes */
617 				bp->b_flags |= B_ERROR;
618 				goto ignoreerr;
619 			}
620 			break;
621 
622 		case TS_SUCC:		/* success termination */
623 			printf("ts%d: success\n", tsunit);
624 			goto ignoreerr;
625 
626 		case TS_ALERT:		/* tape status alert */
627 			/*
628 			 * If we hit the end of the tape file,
629 			 * update our position.
630 			 */
631 			if (sc->sc_ts.t_sts.s_xs0 & (TS_TMK|TS_EOT)) {
632 				tsseteof(bp);	/* set blkno and nxrec */
633 				state = SCOM;	/* force completion */
634 				/*
635 				 * Stuff bc so it will be unstuffed correctly
636 				 * later to get resid.
637 				 */
638 				sc->sc_ts.t_sts.s_rbpcr = bp->b_bcount;
639 				goto opdone;
640 			}
641 			/*
642 			 * If we were reading raw tape and the record was too
643 			 * long or too short, we don't consider this an error.
644 			 */
645 			if ((bp->b_flags & (B_READ|B_RAW)) == (B_READ|B_RAW) &&
646 			    sc->sc_ts.t_sts.s_xs0&(TS_RLS|TS_RLL))
647 				goto ignoreerr;
648 			/* FALLTHROUGH */
649 
650 		case TS_RECOV:		/* recoverable, tape moved */
651 			/*
652 			 * If this was an i/o operation retry up to 8 times.
653 			 */
654 			if (state == SIO) {
655 				if (++um->um_tab.b_errcnt < 7) {
656 					ubadone(um);
657 					goto opcont;
658 				} else
659 					sc->sc_blkno++;
660 			} else {
661 				/*
662 				 * Non-i/o errors on non-raw tape
663 				 * cause it to close.
664 				 */
665 				if ((bp->b_flags&B_RAW) == 0 &&
666 				    sc->sc_openf > 0)
667 					sc->sc_openf = -1;
668 			}
669 			break;
670 
671 		case TS_REJECT:		/* function reject */
672 			if (state == SIO && sc->sc_ts.t_sts.s_xs0 & TS_WLE)
673 				tprintf(sc->sc_ctty, "ts%d: write locked\n",
674 				    tsunit);
675 			if ((sc->sc_ts.t_sts.s_xs0 & TS_ONL) == 0)
676 				tprintf(sc->sc_ctty, "ts%d: offline\n",
677 				    tsunit);
678 			break;
679 		}
680 		/*
681 		 * Couldn't recover error
682 		 */
683 		tprintf(sc->sc_ctty, "ts%d: hard error bn%d tssr=%b xs0=%b",
684 		    tsunit, bp->b_blkno, addr->tssr, TSSR_BITS,
685 		    sc->sc_ts.t_sts.s_xs0, TSXS0_BITS);
686 		if (sc->sc_ts.t_sts.s_xs1)
687 			tprintf(sc->sc_ctty, " xs1=%b", sc->sc_ts.t_sts.s_xs1,
688 			    TSXS1_BITS);
689 		if (sc->sc_ts.t_sts.s_xs2)
690 			tprintf(sc->sc_ctty, " xs2=%b", sc->sc_ts.t_sts.s_xs2,
691 			    TSXS2_BITS);
692 		if (sc->sc_ts.t_sts.s_xs3)
693 			tprintf(sc->sc_ctty, " xs3=%b", sc->sc_ts.t_sts.s_xs3,
694 			    TSXS3_BITS);
695 		tprintf(sc->sc_ctty, "\n");
696 		bp->b_flags |= B_ERROR;
697 		goto opdone;
698 	}
699 	/*
700 	 * Advance tape control FSM.
701 	 */
702 ignoreerr:
703 	switch (state) {
704 
705 	case SIO:
706 		/*
707 		 * Read/write increments tape block number
708 		 */
709 		sc->sc_blkno++;
710 		sc->sc_blks++;
711 		if (um->um_tab.b_errcnt)
712 			sc->sc_softerrs++;
713 		goto opdone;
714 
715 	case SCOM:
716 		/*
717 		 * For forward/backward space record update current position.
718 		 */
719 		if (bp == &ctsbuf[tsunit])
720 			switch ((int)bp->b_command) {
721 
722 			case TS_SFORW:
723 				sc->sc_blkno += bp->b_repcnt;
724 				break;
725 
726 			case TS_SREV:
727 				sc->sc_blkno -= bp->b_repcnt;
728 				break;
729 			}
730 		goto opdone;
731 
732 	case SSEEK:
733 		sc->sc_blkno = bdbtofsb(bp->b_blkno);
734 		goto opcont;
735 
736 	default:
737 		panic("tsintr");
738 	}
739 opdone:
740 	/*
741 	 * Reset error count and remove
742 	 * from device queue.
743 	 */
744 	um->um_tab.b_errcnt = 0;
745 	um->um_tab.b_actf->b_actf = bp->av_forw;
746 	bp->b_resid = sc->sc_ts.t_sts.s_rbpcr;
747 	ubadone(um);
748 	biodone(bp);
749 	if (um->um_tab.b_actf->b_actf == 0) {
750 		um->um_tab.b_active = 0;
751 		return;
752 	}
753 opcont:
754 	tsstart(um);
755 }
756 
757 tsseteof(bp)
758 	register struct buf *bp;
759 {
760 	register int tsunit = TSUNIT(bp->b_dev);
761 	register struct ts_softc *sc = &ts_softc[tsdinfo[tsunit]->ui_ctlr];
762 
763 	if (bp == &ctsbuf[tsunit]) {
764 		if (sc->sc_blkno > bdbtofsb(bp->b_blkno)) {
765 			/* reversing */
766 			sc->sc_nxrec = bdbtofsb(bp->b_blkno) -
767 				sc->sc_ts.t_sts.s_rbpcr;
768 			sc->sc_blkno = sc->sc_nxrec;
769 		} else {
770 			/* spacing forward */
771 			sc->sc_blkno = bdbtofsb(bp->b_blkno) +
772 				sc->sc_ts.t_sts.s_rbpcr;
773 			sc->sc_nxrec = sc->sc_blkno - 1;
774 		}
775 		return;
776 	}
777 	/* eof on read */
778 	sc->sc_nxrec = bdbtofsb(bp->b_blkno);
779 }
780 
781 tsreset(uban)
782 	int uban;
783 {
784 	register struct uba_ctlr *um;
785 	register struct uba_device *ui;
786 	register int ts11, i;
787 
788 	for (ts11 = 0; ts11 < NTS; ts11++) {
789 		if ((um = tsminfo[ts11]) == 0 || um->um_alive == 0 ||
790 		    um->um_ubanum != uban)
791 			continue;
792 		printf(" ts%d", ts11);
793 		um->um_tab.b_active = 0;
794 		um->um_tab.b_actf = um->um_tab.b_actl = 0;
795 		if (ts_softc[ts11].sc_openf > 0)
796 			ts_softc[ts11].sc_openf = -1;
797 		if (um->um_ubinfo) {
798 			printf("<%d>", UBAI_BDP(um->um_ubinfo));
799 			um->um_ubinfo = 0;
800 		}
801 		/*
802 		 * tsdinfo should be 1-to-1 with tsminfo, but someone
803 		 * might have screwed up the config file:
804 		 */
805 		for (i = 0; i < NTS; i++) {
806 			if ((ui = tsdinfo[i]) != NULL &&
807 			    ui->ui_alive && ui->ui_mi == um) {
808 				um->um_tab.b_actf = um->um_tab.b_actl =
809 				    &tsutab[i];
810 				break;
811 			}
812 		}
813 		tsmap(&ts_softc[ts11], uban, (int *)NULL);
814 		(void) tsinit(ts11);
815 		tsstart(um);
816 	}
817 }
818 
819 /*ARGSUSED*/
820 tsioctl(dev, cmd, data, flag)
821 	caddr_t data;
822 	dev_t dev;
823 {
824 	int tsunit = TSUNIT(dev);
825 	register struct ts_softc *sc = &ts_softc[tsdinfo[tsunit]->ui_ctlr];
826 	register struct buf *bp = &ctsbuf[TSUNIT(dev)];
827 	register int callcount;
828 	int fcount, error = 0;
829 	struct mtop *mtop;
830 	struct mtget *mtget;
831 	/* we depend of the values and order of the MT codes here */
832 	static int tsops[] =
833 	 {TS_WEOF,TS_SFORWF,TS_SREVF,TS_SFORW,TS_SREV,TS_REW,TS_OFFL,TS_SENSE};
834 
835 	switch (cmd) {
836 
837 	case MTIOCTOP:	/* tape operation */
838 		mtop = (struct mtop *)data;
839 		switch (mtop->mt_op) {
840 
841 		case MTWEOF:
842 			callcount = mtop->mt_count;
843 			fcount = 1;
844 			break;
845 
846 		case MTFSF: case MTBSF:
847 		case MTFSR: case MTBSR:
848 			callcount = 1;
849 			fcount = mtop->mt_count;
850 			break;
851 
852 		case MTREW: case MTOFFL: case MTNOP:
853 			callcount = 1;
854 			fcount = 1;
855 			break;
856 
857 		default:
858 			return (ENXIO);
859 		}
860 		if (callcount <= 0 || fcount <= 0)
861 			return (EINVAL);
862 		while (--callcount >= 0) {
863 			tscommand(dev, tsops[mtop->mt_op], fcount);
864 			if ((mtop->mt_op == MTFSR || mtop->mt_op == MTBSR) &&
865 			    bp->b_resid)
866 				return (EIO);
867 			if ((bp->b_flags&B_ERROR) ||
868 			    sc->sc_ts.t_sts.s_xs0&TS_BOT)
869 				break;
870 		}
871 		if (bp->b_flags&B_ERROR)
872 			if ((error = bp->b_error)==0)
873 				return (EIO);
874 		return (error);
875 
876 	case MTIOCGET:
877 		mtget = (struct mtget *)data;
878 		mtget->mt_dsreg = 0;
879 		mtget->mt_erreg = sc->sc_ts.t_sts.s_xs0;
880 		mtget->mt_resid = sc->sc_resid;
881 		mtget->mt_type = MT_ISTS;
882 		break;
883 
884 	default:
885 		return (ENXIO);
886 	}
887 	return (0);
888 }
889 
890 #define	DBSIZE	20
891 
892 tsdump(dev)
893 	dev_t dev;
894 {
895 	register struct uba_device *ui;
896 	register struct uba_regs *uba;
897 	register struct tsdevice *addr;
898 	register int i;
899 	register struct pte *io;
900 	int blk, num, unit, reg, start;
901 	u_short db;
902 	struct ts_tsdata *tc, *tc_ubaddr;
903 
904 	unit = TSUNIT(dev);
905 	if (unit >= NTS)
906 		return (ENXIO);
907 #define	phys(a,b)	((b)((int)(a)&0x7fffffff))
908 	ui = phys(tsdinfo[unit], struct uba_device *);
909 	if (ui->ui_alive == 0)
910 		return (ENXIO);
911 	uba = phys(ui->ui_hd, struct uba_hd *)->uh_physuba;
912 	ubainit(uba);
913 	addr = (struct tsdevice *)ui->ui_physaddr;
914 
915 	/* map a ts_tsdata structure */
916 	tc = phys(&ts_softc[0].sc_ts, struct ts_tsdata *);
917 	num = btoc(sizeof(struct ts_tsdata)) + 1;
918 	io = &uba->uba_map[reg = NUBMREG - num];
919 	for (i = 0; i < num; i++)
920 		*(int *)io++ = UBAMR_MRV | (btop(tc) + i);
921 	i = (((int)tc & PGOFSET) | (reg << 9));
922 	tc_ubaddr = (struct ts_tsdata *)i;
923 	db = i + ((i >> 16) & 3);
924 
925 	/* init the drive */
926 	addr->tssr = 0;
927 	tswait(addr);
928 	if ((addr->tssr & (TS_NBA|TS_OFL)) != TS_NBA)
929 		return (EFAULT);
930 
931 	/* set characteristics */
932 	i = (int)&tc_ubaddr->t_char;
933 	tc->t_cmd.c_loba = i;
934 	tc->t_cmd.c_hiba = (i >> 16) & 3;
935 	tc->t_cmd.c_size = sizeof(struct ts_char);
936 	tc->t_cmd.c_cmd = TS_ACK | TS_CVC | TS_SETCHR;
937 	tc->t_char.char_addr = (int)&tc_ubaddr->t_sts;
938 	tc->t_char.char_size = sizeof(struct ts_sts);
939 	tc->t_char.char_mode = TS_ESS;
940 	addr->tsdb = db;
941 	tswait(addr);
942 	if (addr->tssr & TS_NBA)
943 		return (ENXIO);
944 
945 	/* dump */
946 	tc->t_cmd.c_cmd = TS_ACK | TS_WCOM;
947 	tc->t_cmd.c_repcnt = 1;
948 	num = maxfree;
949 	for (start = 0, num = maxfree; num > 0; start += blk, num -= blk) {
950 		blk = num > DBSIZE ? DBSIZE : num;
951 		io = uba->uba_map;
952 		for (i = 0; i < blk; i++)
953 			*(int *)io++ = UBAMR_MRV | (1 << UBAMR_DPSHIFT) |
954 				(start + i);
955 		*(int *)io = 0;
956 		addr->tsdb = db;
957 		tswait(addr);
958 	}
959 
960 	/* eof */
961 	tc->t_cmd.c_cmd = TS_WEOF;
962 	addr->tsdb = db;
963 	tswait(addr);
964 	addr->tsdb = db;
965 	tswait(addr);
966 
967 	if (addr->tssr&TS_SC)
968 		return (EIO);
969 	addr->tssr = 0;
970 	tswait(addr);
971 	return (0);
972 }
973 
974 tswait(addr)
975 	register struct tsdevice *addr;
976 {
977 
978 	while ((addr->tssr & TS_SSR) == 0)
979 		/* void */;
980 }
981 
982 #endif /* NTS > 0 */
983