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