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