xref: /csrg-svn/sys/vax/uba/tm.c (revision 40903)
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  *	@(#)tm.c	7.10 (Berkeley) 04/12/90
7  */
8 
9 #include "te.h"
10 #include "ts.h"
11 #if NTE > 0
12 /*
13  * TM11/TE10 tape driver
14  *
15  * TODO:
16  *	test driver with more than one controller
17  *	test reset code
18  *	what happens if you offline tape during rewind?
19  *	test using file system on tape
20  */
21 #include "param.h"
22 #include "systm.h"
23 #include "buf.h"
24 #include "conf.h"
25 #include "user.h"
26 #include "proc.h"
27 #include "file.h"
28 #include "map.h"
29 #include "vm.h"
30 #include "ioctl.h"
31 #include "mtio.h"
32 #include "cmap.h"
33 #include "uio.h"
34 #include "kernel.h"
35 #include "tty.h"
36 #include "syslog.h"
37 
38 #include "machine/pte.h"
39 #include "../vax/cpu.h"
40 #include "ubareg.h"
41 #include "ubavar.h"
42 #include "tmreg.h"
43 
44 /*
45  * There is a ctmbuf per tape controller.
46  * It is used as the token to pass to the internal routines
47  * to execute tape ioctls, and also acts as a lock on the slaves
48  * on the controller, since there is only one per controller.
49  * In particular, when the tape is rewinding on close we release
50  * the user process but any further attempts to use the tape drive
51  * before the rewind completes will hang waiting for ctmbuf.
52  */
53 struct	buf	ctmbuf[NTM];
54 
55 /*
56  * Driver unibus interface routines and variables.
57  */
58 int	tmprobe(), tmslave(), tmattach(), tmdgo(), tmintr();
59 struct	uba_ctlr *tmminfo[NTM];
60 struct	uba_device *tedinfo[NTE];
61 struct	buf teutab[NTE];
62 short	tetotm[NTE];
63 u_short	tmstd[] = { 0772520, 0 };
64 struct	uba_driver tmdriver =
65  { tmprobe, tmslave, tmattach, tmdgo, tmstd, "te", tedinfo, "tm", tmminfo, 0 };
66 
67 /* bits in minor device */
68 #define	TEUNIT(dev)	(minor(dev)&03)
69 #define	TMUNIT(dev)	(tetotm[TEUNIT(dev)])
70 #define	T_NOREWIND	04
71 #define	T_1600BPI	0x8
72 
73 #define	INF	(daddr_t)1000000L
74 
75 /*
76  * Software state per tape transport.
77  *
78  * 1. A tape drive is a unique-open device; we refuse opens when it is already.
79  * 2. We keep track of the current position on a block tape and seek
80  *    before operations by forward/back spacing if necessary.
81  * 3. We remember if the last operation was a write on a tape, so if a tape
82  *    is open read write and the last thing done is a write we can
83  *    write a standard end of tape mark (two eofs).
84  * 4. We remember the status registers after the last command, using
85  *    then internally and returning them to the SENSE ioctl.
86  * 5. We remember the last density the tape was used at.  If it is
87  *    not a BOT when we start using it and we are writing, we don't
88  *    let the density be changed.
89  */
90 struct	te_softc {
91 	char	sc_openf;	/* lock against multiple opens */
92 	char	sc_lastiow;	/* last op was a write */
93 	daddr_t	sc_blkno;	/* block number, for block device tape */
94 	daddr_t	sc_nxrec;	/* position of end of tape, if known */
95 	u_short	sc_erreg;	/* copy of last erreg */
96 	u_short	sc_ioerreg;	/* copy of last erreg for I/O command */
97 	u_short	sc_dsreg;	/* copy of last dsreg */
98 	short	sc_resid;	/* copy of last bc */
99 #ifdef unneeded
100 	short	sc_lastcmd;	/* last command to handle direction changes */
101 #endif
102 	u_short	sc_dens;	/* prototype command with density info */
103 	short	sc_tact;	/* timeout is active */
104 	daddr_t	sc_timo;	/* time until timeout expires */
105 	int	sc_blks;	/* number of I/O operations since open */
106 	int	sc_softerrs;	/* number of soft I/O errors since open */
107 	caddr_t	sc_ctty;	/* users controlling terminal (vnode) */
108 } te_softc[NTE];
109 #ifdef unneeded
110 int	tmgapsdcnt;		/* DEBUG */
111 #endif
112 
113 /*
114  * States for um->um_tab.b_active, the per controller state flag.
115  * This is used to sequence control in the driver.
116  */
117 #define	SSEEK	1		/* seeking */
118 #define	SIO	2		/* doing seq i/o */
119 #define	SCOM	3		/* sending control command */
120 #define	SREW	4		/* sending a drive rewind */
121 
122 /*
123  * Determine if there is a controller for
124  * a tm at address reg.  Our goal is to make the
125  * device interrupt.
126  */
127 tmprobe(reg)
128 	caddr_t reg;
129 {
130 	register int br, cvec;		/* must be r11,r10; value-result */
131 
132 #ifdef lint
133 	br = 0; cvec = br; br = cvec;
134 	tmintr(0);
135 #endif
136 	((struct tmdevice *)reg)->tmcs = TM_IE;
137 	/*
138 	 * If this is a tm11, it ought to have interrupted
139 	 * by now, if it isn't (ie: it is a ts04) then we just
140 	 * hope that it didn't interrupt, so autoconf will ignore it.
141 	 * Just in case, we will reference one
142 	 * of the more distant registers, and hope for a machine
143 	 * check, or similar disaster if this is a ts.
144 	 *
145 	 * Note: on an 11/780, badaddr will just generate
146 	 * a uba error for a ts; but our caller will notice that
147 	 * so we won't check for it.
148 	 */
149 	if (badaddr((caddr_t)&((struct tmdevice *)reg)->tmrd, 2))
150 		return (0);
151 	return (sizeof (struct tmdevice));
152 }
153 
154 /*
155  * Due to a design flaw, we cannot ascertain if the tape
156  * exists or not unless it is on line - ie: unless a tape is
157  * mounted. This is too servere a restriction to bear,
158  * so all units are assumed to exist.
159  */
160 /*ARGSUSED*/
161 tmslave(ui, reg)
162 	struct uba_device *ui;
163 	caddr_t reg;
164 {
165 
166 	return (1);
167 }
168 
169 /*
170  * Record attachment of the unit to the controller.
171  */
172 /*ARGSUSED*/
173 tmattach(ui)
174 	struct uba_device *ui;
175 {
176 	/*
177 	 * Tetotm is used in TMUNIT to index the ctmbuf
178 	 * array given a te unit number.
179 	 */
180 	tetotm[ui->ui_unit] = ui->ui_mi->um_ctlr;
181 }
182 
183 int	tmtimer();
184 /*
185  * Open the device.  Tapes are unique open
186  * devices, so we refuse if it is already open.
187  * We also check that a tape is available, and
188  * don't block waiting here; if you want to wait
189  * for a tape you should timeout in user code.
190  */
191 
192 #ifdef AVIV
193 int tmdens[4] = { 0x6000, 0x0000, 0x2000, 0 };
194 #endif AVIV
195 int tmdiag;
196 
197 tmopen(dev, flag)
198 	dev_t dev;
199 	int flag;
200 {
201 	register int teunit;
202 	register struct uba_device *ui;
203 	register struct te_softc *sc;
204 	int olddens, dens, error;
205 	int s;
206 
207 	teunit = TEUNIT(dev);
208 	if (teunit>=NTE || (ui = tedinfo[teunit]) == 0 || ui->ui_alive == 0)
209 		return (ENXIO);
210 	if ((sc = &te_softc[teunit])->sc_openf)
211 		return (EBUSY);
212 	sc->sc_openf = 1;
213 	olddens = sc->sc_dens;
214 	dens = TM_IE | TM_GO | (ui->ui_slave << 8);
215 #ifndef AVIV
216 	if ((minor(dev) & T_1600BPI) == 0)
217 		dens |= TM_D800;
218 #else AVIV
219 	dens |= tmdens[(minor(dev)>>3)&03];
220 #endif AVIV
221 	sc->sc_dens = dens;
222 get:
223 	tmcommand(dev, TM_SENSE, 1);
224 	if (sc->sc_erreg&TMER_SDWN) {
225 		if (error = tsleep((caddr_t)&lbolt, (PZERO+1) | PCATCH,
226 		    devopn, 0))
227 			return (error);
228 		goto get;
229 	}
230 	sc->sc_dens = olddens;
231 	if ((sc->sc_erreg&(TMER_SELR|TMER_TUR)) != (TMER_SELR|TMER_TUR)) {
232 		uprintf("te%d: not online\n", teunit);
233 		sc->sc_openf = 0;
234 		return (EIO);
235 	}
236 	if ((flag&FWRITE) && (sc->sc_erreg&TMER_WRL)) {
237 		uprintf("te%d: no write ring\n", teunit);
238 		sc->sc_openf = 0;
239 		return (EIO);
240 	}
241 	if ((sc->sc_erreg&TMER_BOT) == 0 && (flag&FWRITE) &&
242 	    dens != sc->sc_dens) {
243 		uprintf("te%d: can't change density in mid-tape\n", teunit);
244 		sc->sc_openf = 0;
245 		return (EIO);
246 	}
247 	sc->sc_blkno = (daddr_t)0;
248 	sc->sc_nxrec = INF;
249 	sc->sc_lastiow = 0;
250 	sc->sc_dens = dens;
251 	sc->sc_blks = 0;
252 	sc->sc_softerrs = 0;
253 	sc->sc_ctty = (caddr_t)(u.u_procp->p_flag&SCTTY ?
254 			u.u_procp->p_session->s_ttyvp : 0);
255 	s = splclock();
256 	if (sc->sc_tact == 0) {
257 		sc->sc_timo = INF;
258 		sc->sc_tact = 1;
259 		timeout(tmtimer, (caddr_t)dev, 5*hz);
260 	}
261 	splx(s);
262 	return (0);
263 }
264 
265 /*
266  * Close tape device.
267  *
268  * If tape was open for writing or last operation was
269  * a write, then write two EOF's and backspace over the last one.
270  * Unless this is a non-rewinding special file, rewind the tape.
271  * Make the tape available to others.
272  */
273 tmclose(dev, flag)
274 	register dev_t dev;
275 	register flag;
276 {
277 	register struct te_softc *sc = &te_softc[TEUNIT(dev)];
278 
279 	if (flag == FWRITE || (flag&FWRITE) && sc->sc_lastiow) {
280 		tmcommand(dev, TM_WEOF, 1);
281 		tmcommand(dev, TM_WEOF, 1);
282 		tmcommand(dev, TM_SREV, 1);
283 	}
284 	if ((minor(dev)&T_NOREWIND) == 0)
285 		/*
286 		 * 0 count means don't hang waiting for rewind complete
287 		 * rather ctmbuf stays busy until the operation completes
288 		 * preventing further opens from completing by
289 		 * preventing a TM_SENSE from completing.
290 		 */
291 		tmcommand(dev, TM_REW, 0);
292 	if (sc->sc_blks > 100 && sc->sc_softerrs > sc->sc_blks / 100)
293 		log(LOG_INFO, "te%d: %d soft errors in %d blocks\n",
294 		    TEUNIT(dev), sc->sc_softerrs, sc->sc_blks);
295 	sc->sc_openf = 0;
296 	return (0);
297 }
298 
299 /*
300  * Execute a command on the tape drive
301  * a specified number of times.
302  */
303 tmcommand(dev, com, count)
304 	dev_t dev;
305 	int com, count;
306 {
307 	register struct buf *bp;
308 	register int s;
309 
310 	bp = &ctmbuf[TMUNIT(dev)];
311 	s = spl5();
312 	while (bp->b_flags&B_BUSY) {
313 		/*
314 		 * This special check is because B_BUSY never
315 		 * gets cleared in the non-waiting rewind case.
316 		 */
317 		if (bp->b_repcnt == 0 && (bp->b_flags&B_DONE))
318 			break;
319 		bp->b_flags |= B_WANTED;
320 		sleep((caddr_t)bp, PRIBIO);
321 	}
322 	bp->b_flags = B_BUSY|B_READ;
323 	splx(s);
324 	bp->b_dev = dev;
325 	bp->b_repcnt = -count;
326 	bp->b_command = com;
327 	bp->b_blkno = 0;
328 	tmstrategy(bp);
329 	/*
330 	 * In case of rewind from close, don't wait.
331 	 * This is the only case where count can be 0.
332 	 */
333 	if (count == 0)
334 		return;
335 	iowait(bp);
336 	if (bp->b_flags&B_WANTED)
337 		wakeup((caddr_t)bp);
338 	bp->b_flags &= B_ERROR;
339 }
340 
341 /*
342  * Queue a tape operation.
343  */
344 tmstrategy(bp)
345 	register struct buf *bp;
346 {
347 	int teunit = TEUNIT(bp->b_dev);
348 	register struct uba_ctlr *um;
349 	register struct buf *dp;
350 	int s;
351 
352 	/*
353 	 * Put transfer at end of unit queue
354 	 */
355 	dp = &teutab[teunit];
356 	bp->av_forw = NULL;
357 	s = spl5();
358 	um = tedinfo[teunit]->ui_mi;
359 	if (dp->b_actf == NULL) {
360 		dp->b_actf = bp;
361 		/*
362 		 * Transport not already active...
363 		 * put at end of controller queue.
364 		 */
365 		dp->b_forw = NULL;
366 		if (um->um_tab.b_actf == NULL)
367 			um->um_tab.b_actf = dp;
368 		else
369 			um->um_tab.b_actl->b_forw = dp;
370 		um->um_tab.b_actl = dp;
371 	} else
372 		dp->b_actl->av_forw = bp;
373 	dp->b_actl = bp;
374 	/*
375 	 * If the controller is not busy, get
376 	 * it going.
377 	 */
378 	if (um->um_tab.b_active == 0)
379 		tmstart(um);
380 	splx(s);
381 }
382 
383 /*
384  * Start activity on a tm controller.
385  */
386 tmstart(um)
387 	register struct uba_ctlr *um;
388 {
389 	register struct buf *bp, *dp;
390 	register struct tmdevice *addr = (struct tmdevice *)um->um_addr;
391 	register struct te_softc *sc;
392 	register struct uba_device *ui;
393 	int teunit, cmd;
394 	daddr_t blkno;
395 
396 	/*
397 	 * Look for an idle transport on the controller.
398 	 */
399 loop:
400 	if ((dp = um->um_tab.b_actf) == NULL)
401 		return;
402 	if ((bp = dp->b_actf) == NULL) {
403 		um->um_tab.b_actf = dp->b_forw;
404 		goto loop;
405 	}
406 	teunit = TEUNIT(bp->b_dev);
407 	ui = tedinfo[teunit];
408 	/*
409 	 * Record pre-transfer status (e.g. for TM_SENSE)
410 	 */
411 	sc = &te_softc[teunit];
412 	addr = (struct tmdevice *)um->um_addr;
413 	addr->tmcs = (ui->ui_slave << 8);
414 	sc->sc_dsreg = addr->tmcs;
415 	sc->sc_erreg = addr->tmer;
416 	sc->sc_resid = addr->tmbc;
417 	/*
418 	 * Default is that last command was NOT a write command;
419 	 * if we do a write command we will notice this in tmintr().
420 	 */
421 	sc->sc_lastiow = 0;
422 	if (sc->sc_openf < 0 || (addr->tmcs&TM_CUR) == 0) {
423 		/*
424 		 * Have had a hard error on a non-raw tape
425 		 * or the tape unit is now unavailable
426 		 * (e.g. taken off line).
427 		 */
428 		bp->b_flags |= B_ERROR;
429 		goto next;
430 	}
431 	if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) {
432 		/*
433 		 * Execute control operation with the specified count.
434 		 */
435 		if (bp->b_command == TM_SENSE)
436 			goto next;
437 		/*
438 		 * Set next state; give 5 minutes to complete
439 		 * rewind, or 10 seconds per iteration (minimum 60
440 		 * seconds and max 5 minutes) to complete other ops.
441 		 */
442 		if (bp->b_command == TM_REW) {
443 			um->um_tab.b_active = SREW;
444 			sc->sc_timo = 5 * 60;
445 		} else {
446 			um->um_tab.b_active = SCOM;
447 			sc->sc_timo =
448 			    imin(imax(10*(int)-bp->b_repcnt,60),5*60);
449 		}
450 		if (bp->b_command == TM_SFORW || bp->b_command == TM_SREV)
451 			addr->tmbc = bp->b_repcnt;
452 		goto dobpcmd;
453 	}
454 	/*
455 	 * For raw I/O, fudge the current block number
456 	 * so we don't seek except on a retry.
457 	 * For raw I/O, fudge the current block number
458 	 * so we don't seek except on a retry.
459 	 */
460 	if (bp->b_flags & B_RAW) {
461 		if (um->um_tab.b_errcnt == 0) {
462 			sc->sc_blkno = bdbtofsb(bp->b_blkno);
463 			sc->sc_nxrec = sc->sc_blkno + 1;
464 		}
465 	} else {
466 		/*
467 		 * Handle boundary cases for operation
468 		 * on non-raw tapes.
469 		 */
470 		if (bdbtofsb(bp->b_blkno) > sc->sc_nxrec) {
471 			/*
472 			 * Can't read past known end-of-file.
473 			 */
474 			bp->b_flags |= B_ERROR;
475 			bp->b_error = ENXIO;
476 			goto next;
477 		}
478 		if (bdbtofsb(bp->b_blkno) == sc->sc_nxrec &&
479 		    bp->b_flags&B_READ) {
480 			/*
481 			 * Reading at end of file returns 0 bytes.
482 			 */
483 			bp->b_resid = bp->b_bcount;
484 			clrbuf(bp);
485 			goto next;
486 		}
487 		if ((bp->b_flags&B_READ) == 0)
488 			/*
489 			 * Writing sets EOF
490 			 */
491 			sc->sc_nxrec = bdbtofsb(bp->b_blkno) + 1;
492 	}
493 	/*
494 	 * If the data transfer command is in the correct place,
495 	 * set up all the registers except the csr, and give
496 	 * control over to the UNIBUS adapter routines, to
497 	 * wait for resources to start the i/o.
498 	 */
499 	if ((blkno = sc->sc_blkno) == bdbtofsb(bp->b_blkno)) {
500 		addr->tmbc = -bp->b_bcount;
501 		if ((bp->b_flags&B_READ) == 0) {
502 			if (um->um_tab.b_errcnt &&
503 			    (sc->sc_ioerreg&(TMER_HARD|TMER_SOFT)) != TMER_BGL)
504 				cmd = TM_WIRG;
505 			else
506 				cmd = TM_WCOM;
507 		} else
508 			cmd = TM_RCOM;
509 		um->um_tab.b_active = SIO;
510 		um->um_cmd = sc->sc_dens|cmd;
511 #ifdef notdef
512 		if (tmreverseop(sc->sc_lastcmd))
513 			while (addr->tmer & TMER_SDWN)
514 				DELAY(10),tmgapsdcnt++;
515 		sc->sc_lastcmd = TM_RCOM;		/* will serve */
516 #endif
517 		sc->sc_timo = 60;	/* premature, but should serve */
518 		(void) ubago(ui);
519 		return;
520 	}
521 	/*
522 	 * Tape positioned incorrectly;
523 	 * set to seek forwards or backwards to the correct spot.
524 	 * This happens for raw tapes only on error retries.
525 	 */
526 	um->um_tab.b_active = SSEEK;
527 	if (blkno < bdbtofsb(bp->b_blkno)) {
528 		bp->b_command = TM_SFORW;
529 		addr->tmbc = blkno - bdbtofsb(bp->b_blkno);
530 	} else {
531 		bp->b_command = TM_SREV;
532 		addr->tmbc = bdbtofsb(bp->b_blkno) - blkno;
533 	}
534 	sc->sc_timo = imin(imax(10 * -addr->tmbc, 60), 5 * 60);
535 dobpcmd:
536 #ifdef notdef
537 	/*
538 	 * It is strictly necessary to wait for the tape
539 	 * to stop before changing directions, but the TC11
540 	 * handles this for us.
541 	 */
542 	if (tmreverseop(sc->sc_lastcmd) != tmreverseop(bp->b_command))
543 		while (addr->tmer & TM_SDWN)
544 			DELAY(10),tmgapsdcnt++;
545 	sc->sc_lastcmd = bp->b_command;
546 #endif
547 	/*
548 	 * Do the command in bp.
549 	 */
550 	addr->tmcs = (sc->sc_dens | bp->b_command);
551 	return;
552 
553 next:
554 	/*
555 	 * Done with this operation due to error or
556 	 * the fact that it doesn't do anything.
557 	 * Release UBA resources (if any), dequeue
558 	 * the transfer and continue processing this slave.
559 	 */
560 	if (um->um_ubinfo)
561 		ubadone(um);
562 	um->um_tab.b_errcnt = 0;
563 	dp->b_actf = bp->av_forw;
564 	iodone(bp);
565 	goto loop;
566 }
567 
568 /*
569  * The UNIBUS resources we needed have been
570  * allocated to us; start the device.
571  */
572 tmdgo(um)
573 	register struct uba_ctlr *um;
574 {
575 	register struct tmdevice *addr = (struct tmdevice *)um->um_addr;
576 
577 	addr->tmba = um->um_ubinfo;
578 	addr->tmcs = um->um_cmd | ((um->um_ubinfo >> 12) & 0x30);
579 }
580 
581 /*
582  * Tm interrupt routine.
583  */
584 /*ARGSUSED*/
585 tmintr(tm11)
586 	int tm11;
587 {
588 	struct buf *dp;
589 	register struct buf *bp;
590 	register struct uba_ctlr *um = tmminfo[tm11];
591 	register struct tmdevice *addr;
592 	register struct te_softc *sc;
593 	int teunit;
594 	register state;
595 
596 	if ((dp = um->um_tab.b_actf) == NULL)
597 		return;
598 	bp = dp->b_actf;
599 	teunit = TEUNIT(bp->b_dev);
600 	addr = (struct tmdevice *)tedinfo[teunit]->ui_addr;
601 	sc = &te_softc[teunit];
602 	/*
603 	 * If last command was a rewind, and tape is still
604 	 * rewinding, wait for the rewind complete interrupt.
605 	 */
606 	if (um->um_tab.b_active == SREW) {
607 		um->um_tab.b_active = SCOM;
608 		if (addr->tmer&TMER_RWS) {
609 			sc->sc_timo = 5*60;		/* 5 minutes */
610 			return;
611 		}
612 	}
613 	/*
614 	 * An operation completed... record status
615 	 */
616 	sc->sc_timo = INF;
617 	if (um->um_tab.b_active == SIO)
618 	if (um->um_tab.b_active == SIO)
619 		sc->sc_ioerreg = addr->tmer;
620 	sc->sc_dsreg = addr->tmcs;
621 	sc->sc_erreg = addr->tmer;
622 	sc->sc_resid = addr->tmbc;
623 	if ((bp->b_flags & B_READ) == 0)
624 		sc->sc_lastiow = 1;
625 	state = um->um_tab.b_active;
626 	um->um_tab.b_active = 0;
627 	/*
628 	 * Check for errors.
629 	 */
630 	if (addr->tmcs&TM_ERR) {
631 		while (addr->tmer & TMER_SDWN)
632 			DELAY(10);			/* await settle down */
633 		/*
634 		 * If we hit the end of the tape file, update our position.
635 		 */
636 		if (addr->tmer&TMER_EOF) {
637 			tmseteof(bp);		/* set blkno and nxrec */
638 			state = SCOM;		/* force completion */
639 			/*
640 			 * Stuff bc so it will be unstuffed correctly
641 			 * later to get resid.
642 			 */
643 			addr->tmbc = -bp->b_bcount;
644 			goto opdone;
645 		}
646 		/*
647 		 * If we were reading raw tape and the only error was that the
648 		 * record was too long, then we don't consider this an error.
649 		 */
650 		if ((bp->b_flags & (B_READ|B_RAW)) == (B_READ|B_RAW) &&
651 		    (addr->tmer&(TMER_HARD|TMER_SOFT)) == TMER_RLE)
652 			goto ignoreerr;
653 		/*
654 		 * If error is not hard, and this was an i/o operation
655 		 * retry up to 8 times.
656 		 */
657 		if ((addr->tmer&TMER_HARD)==0 && state==SIO) {
658 			if (um->um_tab.b_errcnt++ < 8) {
659 				if (tmdiag)
660 					log(LOG_DEBUG,
661 					    "te%d: soft error bn%d er=%b\n",
662 					    minor(bp->b_dev)&03,
663 					    bp->b_blkno, sc->sc_erreg,
664 					    TMER_BITS);
665 				sc->sc_blkno++;		/* force backspace */
666 				ubadone(um);
667 				goto opcont;
668 			}
669 		} else
670 			/*
671 			 * Hard or non-i/o errors on non-raw tape
672 			 * cause it to close.
673 			 */
674 			if ((bp->b_flags&B_RAW) == 0 && sc->sc_openf > 0)
675 				sc->sc_openf = -1;
676 		/*
677 		 * Couldn't recover error
678 		 */
679 		tprintf(sc->sc_ctty,
680 		    "te%d: hard error bn%d er=%b\n", minor(bp->b_dev)&03,
681 		    bp->b_blkno, sc->sc_erreg, TMER_BITS);
682 #ifdef	AVIV
683 		if (tmdiag) {
684 			addr->tmmr = DAB;
685 			printf("reject code 0%o", addr->tmmr & DAB_MASK);
686 			addr->tmmr = DTS;
687 			if (addr->tmmr & DTS_MASK)
688 			    printf(", dead track 0%o", addr->tmmr & DTS_MASK);
689 			addr->tmmr = RWERR;
690 			printf(", read/write errors %b\n",
691 			    addr->tmmr & RWERR_MASK,
692 			    RWERR_BITS);
693 			addr->tmmr = DRSENSE;
694 			printf("drive sense %b, ", addr->tmmr & DRSENSE_MASK,
695 			    DRSENSE_BITS);
696 			printf("fsr %b\n", addr->tmfsr, FSR_BITS);
697 		}
698 #endif AVIV
699 		bp->b_flags |= B_ERROR;
700 		goto opdone;
701 	}
702 	/*
703 	 * Advance tape control FSM.
704 	 */
705 ignoreerr:
706 	switch (state) {
707 
708 	case SIO:
709 		/*
710 		 * Read/write increments tape block number
711 		 */
712 		sc->sc_blkno++;
713 		sc->sc_blks++;
714 		if (um->um_tab.b_errcnt)
715 			sc->sc_softerrs++;
716 		goto opdone;
717 
718 	case SCOM:
719 		/*
720 		 * For forward/backward space record update current position.
721 		 */
722 		if (bp == &ctmbuf[TMUNIT(bp->b_dev)])
723 		switch ((int)bp->b_command) {
724 
725 		case TM_SFORW:
726 			sc->sc_blkno -= bp->b_repcnt;
727 			break;
728 
729 		case TM_SREV:
730 			sc->sc_blkno += bp->b_repcnt;
731 			break;
732 		}
733 		goto opdone;
734 
735 	case SSEEK:
736 		sc->sc_blkno = bdbtofsb(bp->b_blkno);
737 		goto opcont;
738 
739 	default:
740 		panic("tmintr");
741 	}
742 opdone:
743 	/*
744 	 * Reset error count and remove
745 	 * from device queue.
746 	 */
747 	um->um_tab.b_errcnt = 0;
748 	dp->b_actf = bp->av_forw;
749 	/*
750 	 * Check resid; watch out for resid >32767 (tmbc not negative).
751 	 */
752 	bp->b_resid = ((int) -addr->tmbc) & 0xffff;
753 	ubadone(um);
754 	iodone(bp);
755 	/*
756 	 * Circulate slave to end of controller
757 	 * queue to give other slaves a chance.
758 	 */
759 	um->um_tab.b_actf = dp->b_forw;
760 	if (dp->b_actf) {
761 		dp->b_forw = NULL;
762 		if (um->um_tab.b_actf == NULL)
763 			um->um_tab.b_actf = dp;
764 		else
765 			um->um_tab.b_actl->b_forw = dp;
766 		um->um_tab.b_actl = dp;
767 	}
768 	if (um->um_tab.b_actf == 0)
769 		return;
770 opcont:
771 	tmstart(um);
772 }
773 
774 tmtimer(dev)
775 	int dev;
776 {
777 	register struct te_softc *sc = &te_softc[TEUNIT(dev)];
778 	register short x;
779 
780 	if (sc->sc_timo != INF && (sc->sc_timo -= 5) < 0) {
781 		printf("te%d: lost interrupt\n", TEUNIT(dev));
782 		sc->sc_timo = INF;
783 		x = spl5();
784 		tmintr(TMUNIT(dev));
785 		(void) splx(x);
786 	}
787 	timeout(tmtimer, (caddr_t)dev, 5*hz);
788 }
789 
790 tmseteof(bp)
791 	register struct buf *bp;
792 {
793 	register int teunit = TEUNIT(bp->b_dev);
794 	register struct tmdevice *addr =
795 	    (struct tmdevice *)tedinfo[teunit]->ui_addr;
796 	register struct te_softc *sc = &te_softc[teunit];
797 
798 	if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) {
799 		if (sc->sc_blkno > bdbtofsb(bp->b_blkno)) {
800 			/* reversing */
801 			sc->sc_nxrec = bdbtofsb(bp->b_blkno) - addr->tmbc;
802 			sc->sc_blkno = sc->sc_nxrec;
803 		} else {
804 			/* spacing forward */
805 			sc->sc_blkno = bdbtofsb(bp->b_blkno) + addr->tmbc;
806 			sc->sc_nxrec = sc->sc_blkno - 1;
807 		}
808 		return;
809 	}
810 	/* eof on read */
811 	sc->sc_nxrec = bdbtofsb(bp->b_blkno);
812 }
813 
814 tmreset(uban)
815 	int uban;
816 {
817 	register struct uba_ctlr *um;
818 	register tm11, teunit;
819 	register struct uba_device *ui;
820 	register struct buf *dp;
821 
822 	for (tm11 = 0; tm11 < NTM; tm11++) {
823 		if ((um = tmminfo[tm11]) == 0 || um->um_alive == 0 ||
824 		   um->um_ubanum != uban)
825 			continue;
826 		printf(" tm%d", tm11);
827 		um->um_tab.b_active = 0;
828 		um->um_tab.b_actf = um->um_tab.b_actl = 0;
829 		if (um->um_ubinfo) {
830 			printf("<%d>", (um->um_ubinfo>>28)&0xf);
831 			um->um_ubinfo = 0;
832 		}
833 		((struct tmdevice *)(um->um_addr))->tmcs = TM_DCLR;
834 		for (teunit = 0; teunit < NTE; teunit++) {
835 			if ((ui = tedinfo[teunit]) == 0 || ui->ui_mi != um ||
836 			    ui->ui_alive == 0)
837 				continue;
838 			dp = &teutab[teunit];
839 			dp->b_active = 0;
840 			dp->b_forw = 0;
841 			if (um->um_tab.b_actf == NULL)
842 				um->um_tab.b_actf = dp;
843 			else
844 				um->um_tab.b_actl->b_forw = dp;
845 			um->um_tab.b_actl = dp;
846 			if (te_softc[teunit].sc_openf > 0)
847 				te_softc[teunit].sc_openf = -1;
848 		}
849 		tmstart(um);
850 	}
851 }
852 
853 /*ARGSUSED*/
854 tmioctl(dev, cmd, data, flag)
855 	caddr_t data;
856 	dev_t dev;
857 {
858 	int teunit = TEUNIT(dev);
859 	register struct te_softc *sc = &te_softc[teunit];
860 	register struct buf *bp = &ctmbuf[TMUNIT(dev)];
861 	register callcount;
862 	int fcount;
863 	struct mtop *mtop;
864 	struct mtget *mtget;
865 	/* we depend of the values and order of the MT codes here */
866 	static tmops[] =
867 	   {TM_WEOF,TM_SFORW,TM_SREV,TM_SFORW,TM_SREV,TM_REW,TM_OFFL,TM_SENSE};
868 
869 	switch (cmd) {
870 
871 	case MTIOCTOP:	/* tape operation */
872 		mtop = (struct mtop *)data;
873 		switch (mtop->mt_op) {
874 
875 		case MTWEOF:
876 			callcount = mtop->mt_count;
877 			fcount = 1;
878 			break;
879 
880 		case MTFSF: case MTBSF:
881 			callcount = mtop->mt_count;
882 			fcount = INF;
883 			break;
884 
885 		case MTFSR: case MTBSR:
886 			callcount = 1;
887 			fcount = mtop->mt_count;
888 			break;
889 
890 		case MTREW: case MTOFFL: case MTNOP:
891 			callcount = 1;
892 			fcount = 1;
893 			break;
894 
895 		default:
896 			return (ENXIO);
897 		}
898 		if (callcount <= 0 || fcount <= 0)
899 			return (EINVAL);
900 		while (--callcount >= 0) {
901 			tmcommand(dev, tmops[mtop->mt_op], fcount);
902 			if ((mtop->mt_op == MTFSR || mtop->mt_op == MTBSR) &&
903 			    bp->b_resid)
904 				return (EIO);
905 			if ((bp->b_flags&B_ERROR) || sc->sc_erreg&TMER_BOT)
906 				break;
907 		}
908 		return (geterror(bp));
909 
910 	case MTIOCGET:
911 		mtget = (struct mtget *)data;
912 		mtget->mt_dsreg = sc->sc_dsreg;
913 		mtget->mt_erreg = sc->sc_erreg;
914 		mtget->mt_resid = sc->sc_resid;
915 		mtget->mt_type = MT_ISTM;
916 		break;
917 
918 	default:
919 		return (ENXIO);
920 	}
921 	return (0);
922 }
923 
924 #define	DBSIZE	20
925 
926 tmdump()
927 {
928 	register struct uba_device *ui;
929 	register struct uba_regs *up;
930 	register struct tmdevice *addr;
931 	int blk, num;
932 	int start;
933 
934 	start = 0;
935 	num = maxfree;
936 #define	phys(a,b)	((b)((int)(a)&0x7fffffff))
937 	if (tedinfo[0] == 0)
938 		return (ENXIO);
939 	ui = phys(tedinfo[0], struct uba_device *);
940 	up = phys(ui->ui_hd, struct uba_hd *)->uh_physuba;
941 	ubainit(up);
942 	DELAY(1000000);
943 	addr = (struct tmdevice *)ui->ui_physaddr;
944 	tmwait(addr);
945 	addr->tmcs = TM_DCLR | TM_GO;
946 	while (num > 0) {
947 		blk = num > DBSIZE ? DBSIZE : num;
948 		tmdwrite(start, blk, addr, up);
949 		start += blk;
950 		num -= blk;
951 	}
952 	tmeof(addr);
953 	tmeof(addr);
954 	tmwait(addr);
955 	if (addr->tmcs&TM_ERR)
956 		return (EIO);
957 	addr->tmcs = TM_REW | TM_GO;
958 	tmwait(addr);
959 	return (0);
960 }
961 
962 tmdwrite(dbuf, num, addr, up)
963 	register dbuf, num;
964 	register struct tmdevice *addr;
965 	struct uba_regs *up;
966 {
967 	register struct pte *io;
968 	register int npf;
969 
970 	tmwait(addr);
971 	io = up->uba_map;
972 	npf = num+1;
973 	while (--npf != 0)
974 		 *(int *)io++ = (dbuf++ | (1<<UBAMR_DPSHIFT) | UBAMR_MRV);
975 	*(int *)io = 0;
976 	addr->tmbc = -(num*NBPG);
977 	addr->tmba = 0;
978 	addr->tmcs = TM_WCOM | TM_GO;
979 }
980 
981 tmwait(addr)
982 	register struct tmdevice *addr;
983 {
984 	register s;
985 
986 	do
987 		s = addr->tmcs;
988 	while ((s & TM_CUR) == 0);
989 }
990 
991 tmeof(addr)
992 	struct tmdevice *addr;
993 {
994 
995 	tmwait(addr);
996 	addr->tmcs = TM_WEOF | TM_GO;
997 }
998 #endif
999