xref: /original-bsd/sys/vax/uba/tm.c (revision dfa2e9e4)
14f2dfb3aSmckusick /*
25ed26d82Smckusick  * Copyright (c) 1982, 1986 Regents of the University of California.
34f2dfb3aSmckusick  * All rights reserved.  The Berkeley software License Agreement
44f2dfb3aSmckusick  * specifies the terms and conditions for redistribution.
54f2dfb3aSmckusick  *
6*dfa2e9e4Sbostic  *	@(#)tm.c	7.14 (Berkeley) 12/16/90
74f2dfb3aSmckusick  */
81aa546d4Swnj 
9587c5f67Swnj #include "te.h"
10038e0bd0Sroot #include "ts.h"
11781c10e9Swnj #if NTE > 0
121aa546d4Swnj /*
1365061276Swnj  * TM11/TE10 tape driver
14f6e77a22Swnj  *
15905d868dSwnj  * TODO:
16905d868dSwnj  *	test driver with more than one controller
17905d868dSwnj  *	test reset code
18905d868dSwnj  *	what happens if you offline tape during rewind?
19905d868dSwnj  *	test using file system on tape
201aa546d4Swnj  */
21*dfa2e9e4Sbostic #include "sys/param.h"
22*dfa2e9e4Sbostic #include "sys/systm.h"
23*dfa2e9e4Sbostic #include "sys/buf.h"
24*dfa2e9e4Sbostic #include "sys/conf.h"
25*dfa2e9e4Sbostic #include "sys/user.h"
26*dfa2e9e4Sbostic #include "sys/file.h"
27*dfa2e9e4Sbostic #include "sys/map.h"
28*dfa2e9e4Sbostic #include "sys/vm.h"
29*dfa2e9e4Sbostic #include "sys/ioctl.h"
30*dfa2e9e4Sbostic #include "sys/mtio.h"
31*dfa2e9e4Sbostic #include "sys/cmap.h"
32*dfa2e9e4Sbostic #include "sys/uio.h"
33*dfa2e9e4Sbostic #include "sys/kernel.h"
34*dfa2e9e4Sbostic #include "sys/syslog.h"
35*dfa2e9e4Sbostic #include "sys/tprintf.h"
361aa546d4Swnj 
37*dfa2e9e4Sbostic #include "../include/pte.h"
38*dfa2e9e4Sbostic #include "../include/cpu.h"
398ab1e19fSbloom #include "ubareg.h"
408ab1e19fSbloom #include "ubavar.h"
418ab1e19fSbloom #include "tmreg.h"
421aa546d4Swnj 
43905d868dSwnj /*
44905d868dSwnj  * There is a ctmbuf per tape controller.
45905d868dSwnj  * It is used as the token to pass to the internal routines
46905d868dSwnj  * to execute tape ioctls, and also acts as a lock on the slaves
47905d868dSwnj  * on the controller, since there is only one per controller.
48905d868dSwnj  * In particular, when the tape is rewinding on close we release
49905d868dSwnj  * the user process but any further attempts to use the tape drive
50905d868dSwnj  * before the rewind completes will hang waiting for ctmbuf.
51905d868dSwnj  */
52905d868dSwnj struct	buf	ctmbuf[NTM];
531aa546d4Swnj 
54905d868dSwnj /*
55905d868dSwnj  * Driver unibus interface routines and variables.
56905d868dSwnj  */
57884ec674Swnj int	tmprobe(), tmslave(), tmattach(), tmdgo(), tmintr();
58b28bc7fcSwnj struct	uba_ctlr *tmminfo[NTM];
59905d868dSwnj struct	uba_device *tedinfo[NTE];
60905d868dSwnj struct	buf teutab[NTE];
61905d868dSwnj short	tetotm[NTE];
621a2c0962Swnj u_short	tmstd[] = { 0772520, 0 };
6309e5bb10Swnj struct	uba_driver tmdriver =
64905d868dSwnj  { tmprobe, tmslave, tmattach, tmdgo, tmstd, "te", tedinfo, "tm", tmminfo, 0 };
651aa546d4Swnj 
661aa546d4Swnj /* bits in minor device */
67905d868dSwnj #define	TEUNIT(dev)	(minor(dev)&03)
68905d868dSwnj #define	TMUNIT(dev)	(tetotm[TEUNIT(dev)])
691aa546d4Swnj #define	T_NOREWIND	04
702bdeb6ecSmckusick #define	T_1600BPI	0x8
711aa546d4Swnj 
721aa546d4Swnj #define	INF	(daddr_t)1000000L
731aa546d4Swnj 
74884ec674Swnj /*
75884ec674Swnj  * Software state per tape transport.
76905d868dSwnj  *
77905d868dSwnj  * 1. A tape drive is a unique-open device; we refuse opens when it is already.
78905d868dSwnj  * 2. We keep track of the current position on a block tape and seek
79905d868dSwnj  *    before operations by forward/back spacing if necessary.
80905d868dSwnj  * 3. We remember if the last operation was a write on a tape, so if a tape
81905d868dSwnj  *    is open read write and the last thing done is a write we can
82905d868dSwnj  *    write a standard end of tape mark (two eofs).
83905d868dSwnj  * 4. We remember the status registers after the last command, using
84905d868dSwnj  *    then internally and returning them to the SENSE ioctl.
85905d868dSwnj  * 5. We remember the last density the tape was used at.  If it is
86905d868dSwnj  *    not a BOT when we start using it and we are writing, we don't
87905d868dSwnj  *    let the density be changed.
88884ec674Swnj  */
89905d868dSwnj struct	te_softc {
90884ec674Swnj 	char	sc_openf;	/* lock against multiple opens */
91884ec674Swnj 	char	sc_lastiow;	/* last op was a write */
92884ec674Swnj 	daddr_t	sc_blkno;	/* block number, for block device tape */
93905d868dSwnj 	daddr_t	sc_nxrec;	/* position of end of tape, if known */
94884ec674Swnj 	u_short	sc_erreg;	/* copy of last erreg */
9535277208Skarels 	u_short	sc_ioerreg;	/* copy of last erreg for I/O command */
96884ec674Swnj 	u_short	sc_dsreg;	/* copy of last dsreg */
97884ec674Swnj 	short	sc_resid;	/* copy of last bc */
98d1dce5b3Swnj #ifdef unneeded
995c653392Swnj 	short	sc_lastcmd;	/* last command to handle direction changes */
10006eb541fSwnj #endif
101905d868dSwnj 	u_short	sc_dens;	/* prototype command with density info */
10249868192Sroot 	short	sc_tact;	/* timeout is active */
1039fa410e8Sralph 	daddr_t	sc_timo;	/* time until timeout expires */
104edb110f3Skarels 	int	sc_blks;	/* number of I/O operations since open */
105edb110f3Skarels 	int	sc_softerrs;	/* number of soft I/O errors since open */
1060f129637Smarc 	tpr_t	sc_tpr;		/* tprintf handle */
107cef3f809Swnj } te_softc[NTE];
108d1dce5b3Swnj #ifdef unneeded
109d1dce5b3Swnj int	tmgapsdcnt;		/* DEBUG */
110d1dce5b3Swnj #endif
1111aa546d4Swnj 
112884ec674Swnj /*
113905d868dSwnj  * States for um->um_tab.b_active, the per controller state flag.
114905d868dSwnj  * This is used to sequence control in the driver.
115884ec674Swnj  */
1161aa546d4Swnj #define	SSEEK	1		/* seeking */
1171aa546d4Swnj #define	SIO	2		/* doing seq i/o */
1181aa546d4Swnj #define	SCOM	3		/* sending control command */
119884ec674Swnj #define	SREW	4		/* sending a drive rewind */
1201aa546d4Swnj 
1216a858426Skre /*
1226a858426Skre  * Determine if there is a controller for
1236a858426Skre  * a tm at address reg.  Our goal is to make the
1246a858426Skre  * device interrupt.
1256a858426Skre  */
tmprobe(reg)126884ec674Swnj tmprobe(reg)
12709e5bb10Swnj 	caddr_t reg;
12809e5bb10Swnj {
129905d868dSwnj 	register int br, cvec;		/* must be r11,r10; value-result */
1306a858426Skre 
131884ec674Swnj #ifdef lint
132d1dce5b3Swnj 	br = 0; cvec = br; br = cvec;
133317990f6Swnj 	tmintr(0);
134884ec674Swnj #endif
13581a5e293Sroot 	((struct tmdevice *)reg)->tmcs = TM_IE;
13609e5bb10Swnj 	/*
13765061276Swnj 	 * If this is a tm11, it ought to have interrupted
13809e5bb10Swnj 	 * by now, if it isn't (ie: it is a ts04) then we just
1391a2c0962Swnj 	 * hope that it didn't interrupt, so autoconf will ignore it.
1401a2c0962Swnj 	 * Just in case, we will reference one
14109e5bb10Swnj 	 * of the more distant registers, and hope for a machine
1421a2c0962Swnj 	 * check, or similar disaster if this is a ts.
143f6e77a22Swnj 	 *
144f6e77a22Swnj 	 * Note: on an 11/780, badaddr will just generate
145f6e77a22Swnj 	 * a uba error for a ts; but our caller will notice that
146f6e77a22Swnj 	 * so we won't check for it.
14709e5bb10Swnj 	 */
14881a5e293Sroot 	if (badaddr((caddr_t)&((struct tmdevice *)reg)->tmrd, 2))
14909e5bb10Swnj 		return (0);
150d8a337adSkre 	return (sizeof (struct tmdevice));
15109e5bb10Swnj }
15209e5bb10Swnj 
153884ec674Swnj /*
154884ec674Swnj  * Due to a design flaw, we cannot ascertain if the tape
155884ec674Swnj  * exists or not unless it is on line - ie: unless a tape is
156884ec674Swnj  * mounted. This is too servere a restriction to bear,
157884ec674Swnj  * so all units are assumed to exist.
158884ec674Swnj  */
159884ec674Swnj /*ARGSUSED*/
1608aea9ba3Swnj tmslave(ui, reg)
161b28bc7fcSwnj 	struct uba_device *ui;
16209e5bb10Swnj 	caddr_t reg;
16309e5bb10Swnj {
1641a2c0962Swnj 
16509e5bb10Swnj 	return (1);
16609e5bb10Swnj }
16709e5bb10Swnj 
168884ec674Swnj /*
169905d868dSwnj  * Record attachment of the unit to the controller.
170884ec674Swnj  */
171884ec674Swnj /*ARGSUSED*/
172884ec674Swnj tmattach(ui)
173b28bc7fcSwnj 	struct uba_device *ui;
174884ec674Swnj {
175905d868dSwnj 	/*
176de98104dSbostic 	 * Tetotm is used in TMUNIT to index the ctmbuf
177de98104dSbostic 	 * array given a te unit number.
178905d868dSwnj 	 */
179905d868dSwnj 	tetotm[ui->ui_unit] = ui->ui_mi->um_ctlr;
180884ec674Swnj }
181884ec674Swnj 
18249868192Sroot int	tmtimer();
183884ec674Swnj /*
184884ec674Swnj  * Open the device.  Tapes are unique open
185884ec674Swnj  * devices, so we refuse if it is already open.
186884ec674Swnj  * We also check that a tape is available, and
187905d868dSwnj  * don't block waiting here; if you want to wait
188905d868dSwnj  * for a tape you should timeout in user code.
189884ec674Swnj  */
1902bdeb6ecSmckusick 
1912bdeb6ecSmckusick #ifdef AVIV
1922bdeb6ecSmckusick int tmdens[4] = { 0x6000, 0x0000, 0x2000, 0 };
1932bdeb6ecSmckusick #endif AVIV
194edb110f3Skarels int tmdiag;
1952bdeb6ecSmckusick 
tmopen(dev,flag)1961aa546d4Swnj tmopen(dev, flag)
1971aa546d4Swnj 	dev_t dev;
1981aa546d4Swnj 	int flag;
1991aa546d4Swnj {
200905d868dSwnj 	register int teunit;
201b28bc7fcSwnj 	register struct uba_device *ui;
202905d868dSwnj 	register struct te_softc *sc;
203265c83f6Skarels 	int olddens, dens, error;
2045e4e38baSroot 	int s;
2051aa546d4Swnj 
206905d868dSwnj 	teunit = TEUNIT(dev);
2071d69cd4bSkarels 	if (teunit>=NTE || (ui = tedinfo[teunit]) == 0 || ui->ui_alive == 0)
208ae3ac2f8Sroot 		return (ENXIO);
2091d69cd4bSkarels 	if ((sc = &te_softc[teunit])->sc_openf)
2101d69cd4bSkarels 		return (EBUSY);
211edb110f3Skarels 	sc->sc_openf = 1;
212dd6b2376Swnj 	olddens = sc->sc_dens;
213dd6b2376Swnj 	dens = TM_IE | TM_GO | (ui->ui_slave << 8);
2142bdeb6ecSmckusick #ifndef AVIV
215dd6b2376Swnj 	if ((minor(dev) & T_1600BPI) == 0)
216dd6b2376Swnj 		dens |= TM_D800;
2172bdeb6ecSmckusick #else AVIV
2182bdeb6ecSmckusick 	dens |= tmdens[(minor(dev)>>3)&03];
2192bdeb6ecSmckusick #endif AVIV
220dd6b2376Swnj 	sc->sc_dens = dens;
2217b5c0749Swnj get:
222884ec674Swnj 	tmcommand(dev, TM_SENSE, 1);
2237b5c0749Swnj 	if (sc->sc_erreg&TMER_SDWN) {
224265c83f6Skarels 		if (error = tsleep((caddr_t)&lbolt, (PZERO+1) | PCATCH,
225265c83f6Skarels 		    devopn, 0))
226265c83f6Skarels 			return (error);
2277b5c0749Swnj 		goto get;
2287b5c0749Swnj 	}
229dd6b2376Swnj 	sc->sc_dens = olddens;
230ce732b68Sroot 	if ((sc->sc_erreg&(TMER_SELR|TMER_TUR)) != (TMER_SELR|TMER_TUR)) {
231ce732b68Sroot 		uprintf("te%d: not online\n", teunit);
232edb110f3Skarels 		sc->sc_openf = 0;
233ae3ac2f8Sroot 		return (EIO);
234ce732b68Sroot 	}
235ce732b68Sroot 	if ((flag&FWRITE) && (sc->sc_erreg&TMER_WRL)) {
236ce732b68Sroot 		uprintf("te%d: no write ring\n", teunit);
237edb110f3Skarels 		sc->sc_openf = 0;
238ae3ac2f8Sroot 		return (EIO);
239ce732b68Sroot 	}
240ce732b68Sroot 	if ((sc->sc_erreg&TMER_BOT) == 0 && (flag&FWRITE) &&
241c812fa8cSwnj 	    dens != sc->sc_dens) {
242ce732b68Sroot 		uprintf("te%d: can't change density in mid-tape\n", teunit);
243edb110f3Skarels 		sc->sc_openf = 0;
244ae3ac2f8Sroot 		return (EIO);
2451aa546d4Swnj 	}
246f6e77a22Swnj 	sc->sc_blkno = (daddr_t)0;
247f6e77a22Swnj 	sc->sc_nxrec = INF;
248884ec674Swnj 	sc->sc_lastiow = 0;
249905d868dSwnj 	sc->sc_dens = dens;
250edb110f3Skarels 	sc->sc_blks = 0;
251edb110f3Skarels 	sc->sc_softerrs = 0;
2520f129637Smarc 	sc->sc_tpr = tprintf_open();
2533cbcb8dfSkarels 	s = splclock();
25449868192Sroot 	if (sc->sc_tact == 0) {
25549868192Sroot 		sc->sc_timo = INF;
25649868192Sroot 		sc->sc_tact = 1;
257c5d20ef9Sroot 		timeout(tmtimer, (caddr_t)dev, 5*hz);
25849868192Sroot 	}
2595e4e38baSroot 	splx(s);
260ae3ac2f8Sroot 	return (0);
2611aa546d4Swnj }
2621aa546d4Swnj 
263884ec674Swnj /*
264884ec674Swnj  * Close tape device.
265884ec674Swnj  *
266884ec674Swnj  * If tape was open for writing or last operation was
267884ec674Swnj  * a write, then write two EOF's and backspace over the last one.
268884ec674Swnj  * Unless this is a non-rewinding special file, rewind the tape.
269884ec674Swnj  * Make the tape available to others.
270884ec674Swnj  */
tmclose(dev,flag)2711aa546d4Swnj tmclose(dev, flag)
2721aa546d4Swnj 	register dev_t dev;
2731aa546d4Swnj 	register flag;
2741aa546d4Swnj {
275905d868dSwnj 	register struct te_softc *sc = &te_softc[TEUNIT(dev)];
2761aa546d4Swnj 
277884ec674Swnj 	if (flag == FWRITE || (flag&FWRITE) && sc->sc_lastiow) {
278884ec674Swnj 		tmcommand(dev, TM_WEOF, 1);
279884ec674Swnj 		tmcommand(dev, TM_WEOF, 1);
280884ec674Swnj 		tmcommand(dev, TM_SREV, 1);
2811aa546d4Swnj 	}
2821aa546d4Swnj 	if ((minor(dev)&T_NOREWIND) == 0)
283905d868dSwnj 		/*
284905d868dSwnj 		 * 0 count means don't hang waiting for rewind complete
285905d868dSwnj 		 * rather ctmbuf stays busy until the operation completes
286905d868dSwnj 		 * preventing further opens from completing by
287905d868dSwnj 		 * preventing a TM_SENSE from completing.
288905d868dSwnj 		 */
289905d868dSwnj 		tmcommand(dev, TM_REW, 0);
290edb110f3Skarels 	if (sc->sc_blks > 100 && sc->sc_softerrs > sc->sc_blks / 100)
291edb110f3Skarels 		log(LOG_INFO, "te%d: %d soft errors in %d blocks\n",
292edb110f3Skarels 		    TEUNIT(dev), sc->sc_softerrs, sc->sc_blks);
2930f129637Smarc 	tprintf_close(sc->sc_tpr);
294f6e77a22Swnj 	sc->sc_openf = 0;
295edb110f3Skarels 	return (0);
2961aa546d4Swnj }
2971aa546d4Swnj 
298884ec674Swnj /*
299884ec674Swnj  * Execute a command on the tape drive
300884ec674Swnj  * a specified number of times.
301884ec674Swnj  */
tmcommand(dev,com,count)3028aea9ba3Swnj tmcommand(dev, com, count)
3031aa546d4Swnj 	dev_t dev;
3041aa546d4Swnj 	int com, count;
3051aa546d4Swnj {
3061aa546d4Swnj 	register struct buf *bp;
3075e4e38baSroot 	register int s;
3081aa546d4Swnj 
309884ec674Swnj 	bp = &ctmbuf[TMUNIT(dev)];
3105e4e38baSroot 	s = spl5();
3111aa546d4Swnj 	while (bp->b_flags&B_BUSY) {
312905d868dSwnj 		/*
313905d868dSwnj 		 * This special check is because B_BUSY never
314905d868dSwnj 		 * gets cleared in the non-waiting rewind case.
315905d868dSwnj 		 */
3167b5c0749Swnj 		if (bp->b_repcnt == 0 && (bp->b_flags&B_DONE))
317905d868dSwnj 			break;
3181aa546d4Swnj 		bp->b_flags |= B_WANTED;
3191aa546d4Swnj 		sleep((caddr_t)bp, PRIBIO);
3201aa546d4Swnj 	}
3211aa546d4Swnj 	bp->b_flags = B_BUSY|B_READ;
3225e4e38baSroot 	splx(s);
3231aa546d4Swnj 	bp->b_dev = dev;
3241aa546d4Swnj 	bp->b_repcnt = -count;
3251aa546d4Swnj 	bp->b_command = com;
3261aa546d4Swnj 	bp->b_blkno = 0;
3271aa546d4Swnj 	tmstrategy(bp);
328905d868dSwnj 	/*
329905d868dSwnj 	 * In case of rewind from close, don't wait.
330905d868dSwnj 	 * This is the only case where count can be 0.
331905d868dSwnj 	 */
332905d868dSwnj 	if (count == 0)
333905d868dSwnj 		return;
3341aa546d4Swnj 	iowait(bp);
3351aa546d4Swnj 	if (bp->b_flags&B_WANTED)
3361aa546d4Swnj 		wakeup((caddr_t)bp);
3371aa546d4Swnj 	bp->b_flags &= B_ERROR;
3381aa546d4Swnj }
3391aa546d4Swnj 
340884ec674Swnj /*
341905d868dSwnj  * Queue a tape operation.
342884ec674Swnj  */
tmstrategy(bp)3431aa546d4Swnj tmstrategy(bp)
3441aa546d4Swnj 	register struct buf *bp;
3451aa546d4Swnj {
346905d868dSwnj 	int teunit = TEUNIT(bp->b_dev);
347b28bc7fcSwnj 	register struct uba_ctlr *um;
348884ec674Swnj 	register struct buf *dp;
3495e4e38baSroot 	int s;
3501aa546d4Swnj 
351884ec674Swnj 	/*
352884ec674Swnj 	 * Put transfer at end of unit queue
353884ec674Swnj 	 */
354905d868dSwnj 	dp = &teutab[teunit];
3551aa546d4Swnj 	bp->av_forw = NULL;
3565e4e38baSroot 	s = spl5();
35704964253Sbugs 	um = tedinfo[teunit]->ui_mi;
358884ec674Swnj 	if (dp->b_actf == NULL) {
359884ec674Swnj 		dp->b_actf = bp;
360884ec674Swnj 		/*
361884ec674Swnj 		 * Transport not already active...
362884ec674Swnj 		 * put at end of controller queue.
363884ec674Swnj 		 */
364884ec674Swnj 		dp->b_forw = NULL;
365884ec674Swnj 		if (um->um_tab.b_actf == NULL)
366884ec674Swnj 			um->um_tab.b_actf = dp;
3671aa546d4Swnj 		else
368884ec674Swnj 			um->um_tab.b_actl->b_forw = dp;
369884ec674Swnj 		um->um_tab.b_actl = dp;
370884ec674Swnj 	} else
371884ec674Swnj 		dp->b_actl->av_forw = bp;
372884ec674Swnj 	dp->b_actl = bp;
373884ec674Swnj 	/*
374884ec674Swnj 	 * If the controller is not busy, get
375884ec674Swnj 	 * it going.
376884ec674Swnj 	 */
377884ec674Swnj 	if (um->um_tab.b_active == 0)
378884ec674Swnj 		tmstart(um);
3795e4e38baSroot 	splx(s);
3801aa546d4Swnj }
3811aa546d4Swnj 
382884ec674Swnj /*
383884ec674Swnj  * Start activity on a tm controller.
384884ec674Swnj  */
tmstart(um)385884ec674Swnj tmstart(um)
386b28bc7fcSwnj 	register struct uba_ctlr *um;
3871aa546d4Swnj {
388884ec674Swnj 	register struct buf *bp, *dp;
38981a5e293Sroot 	register struct tmdevice *addr = (struct tmdevice *)um->um_addr;
390905d868dSwnj 	register struct te_softc *sc;
391b28bc7fcSwnj 	register struct uba_device *ui;
392905d868dSwnj 	int teunit, cmd;
393f6e77a22Swnj 	daddr_t blkno;
3941aa546d4Swnj 
395884ec674Swnj 	/*
396884ec674Swnj 	 * Look for an idle transport on the controller.
397884ec674Swnj 	 */
3981aa546d4Swnj loop:
399884ec674Swnj 	if ((dp = um->um_tab.b_actf) == NULL)
4001aa546d4Swnj 		return;
401884ec674Swnj 	if ((bp = dp->b_actf) == NULL) {
402884ec674Swnj 		um->um_tab.b_actf = dp->b_forw;
403884ec674Swnj 		goto loop;
404884ec674Swnj 	}
405905d868dSwnj 	teunit = TEUNIT(bp->b_dev);
406905d868dSwnj 	ui = tedinfo[teunit];
407884ec674Swnj 	/*
408884ec674Swnj 	 * Record pre-transfer status (e.g. for TM_SENSE)
409884ec674Swnj 	 */
410905d868dSwnj 	sc = &te_softc[teunit];
41181a5e293Sroot 	addr = (struct tmdevice *)um->um_addr;
412884ec674Swnj 	addr->tmcs = (ui->ui_slave << 8);
413f6e77a22Swnj 	sc->sc_dsreg = addr->tmcs;
414f6e77a22Swnj 	sc->sc_erreg = addr->tmer;
415f6e77a22Swnj 	sc->sc_resid = addr->tmbc;
416884ec674Swnj 	/*
417884ec674Swnj 	 * Default is that last command was NOT a write command;
418884ec674Swnj 	 * if we do a write command we will notice this in tmintr().
419884ec674Swnj 	 */
420b45170acSroot 	sc->sc_lastiow = 0;
421884ec674Swnj 	if (sc->sc_openf < 0 || (addr->tmcs&TM_CUR) == 0) {
422884ec674Swnj 		/*
423905d868dSwnj 		 * Have had a hard error on a non-raw tape
424905d868dSwnj 		 * or the tape unit is now unavailable
425905d868dSwnj 		 * (e.g. taken off line).
426884ec674Swnj 		 */
427884ec674Swnj 		bp->b_flags |= B_ERROR;
4281aa546d4Swnj 		goto next;
4291aa546d4Swnj 	}
430905d868dSwnj 	if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) {
431884ec674Swnj 		/*
432905d868dSwnj 		 * Execute control operation with the specified count.
433884ec674Swnj 		 */
434884ec674Swnj 		if (bp->b_command == TM_SENSE)
435884ec674Swnj 			goto next;
43649868192Sroot 		/*
43749868192Sroot 		 * Set next state; give 5 minutes to complete
43849868192Sroot 		 * rewind, or 10 seconds per iteration (minimum 60
439c5d20ef9Sroot 		 * seconds and max 5 minutes) to complete other ops.
44049868192Sroot 		 */
44149868192Sroot 		if (bp->b_command == TM_REW) {
44249868192Sroot 			um->um_tab.b_active = SREW;
44349868192Sroot 			sc->sc_timo = 5 * 60;
44449868192Sroot 		} else {
44549868192Sroot 			um->um_tab.b_active = SCOM;
446c8b47d32Swnj 			sc->sc_timo =
447c8b47d32Swnj 			    imin(imax(10*(int)-bp->b_repcnt,60),5*60);
44849868192Sroot 		}
449884ec674Swnj 		if (bp->b_command == TM_SFORW || bp->b_command == TM_SREV)
45009e5bb10Swnj 			addr->tmbc = bp->b_repcnt;
4515c653392Swnj 		goto dobpcmd;
4521aa546d4Swnj 	}
453884ec674Swnj 	/*
454cb8c8d5fSkarels 	 * For raw I/O, fudge the current block number
455cb8c8d5fSkarels 	 * so we don't seek except on a retry.
456ba3a7840Smarc 	 * For raw I/O, fudge the current block number
457ba3a7840Smarc 	 * so we don't seek except on a retry.
458de98104dSbostic 	 */
459de98104dSbostic 	if (bp->b_flags & B_RAW) {
460de98104dSbostic 		if (um->um_tab.b_errcnt == 0) {
461de98104dSbostic 			sc->sc_blkno = bdbtofsb(bp->b_blkno);
462de98104dSbostic 			sc->sc_nxrec = sc->sc_blkno + 1;
463de98104dSbostic 		}
464cb8c8d5fSkarels 	} else {
465de98104dSbostic 		/*
466de98104dSbostic 		 * Handle boundary cases for operation
467de98104dSbostic 		 * on non-raw tapes.
468905d868dSwnj 		 */
4698cc29115Ssam 		if (bdbtofsb(bp->b_blkno) > sc->sc_nxrec) {
470905d868dSwnj 			/*
471905d868dSwnj 			 * Can't read past known end-of-file.
472905d868dSwnj 			 */
473905d868dSwnj 			bp->b_flags |= B_ERROR;
474905d868dSwnj 			bp->b_error = ENXIO;
475905d868dSwnj 			goto next;
476905d868dSwnj 		}
4778cc29115Ssam 		if (bdbtofsb(bp->b_blkno) == sc->sc_nxrec &&
478905d868dSwnj 		    bp->b_flags&B_READ) {
479905d868dSwnj 			/*
480905d868dSwnj 			 * Reading at end of file returns 0 bytes.
481905d868dSwnj 			 */
482905d868dSwnj 			bp->b_resid = bp->b_bcount;
483905d868dSwnj 			clrbuf(bp);
484905d868dSwnj 			goto next;
485905d868dSwnj 		}
486905d868dSwnj 		if ((bp->b_flags&B_READ) == 0)
487905d868dSwnj 			/*
488905d868dSwnj 			 * Writing sets EOF
489905d868dSwnj 			 */
4908cc29115Ssam 			sc->sc_nxrec = bdbtofsb(bp->b_blkno) + 1;
491de98104dSbostic 	}
492905d868dSwnj 	/*
493884ec674Swnj 	 * If the data transfer command is in the correct place,
494884ec674Swnj 	 * set up all the registers except the csr, and give
495884ec674Swnj 	 * control over to the UNIBUS adapter routines, to
496884ec674Swnj 	 * wait for resources to start the i/o.
497884ec674Swnj 	 */
4988cc29115Ssam 	if ((blkno = sc->sc_blkno) == bdbtofsb(bp->b_blkno)) {
49909e5bb10Swnj 		addr->tmbc = -bp->b_bcount;
5001aa546d4Swnj 		if ((bp->b_flags&B_READ) == 0) {
501edb110f3Skarels 			if (um->um_tab.b_errcnt &&
50235277208Skarels 			    (sc->sc_ioerreg&(TMER_HARD|TMER_SOFT)) != TMER_BGL)
503905d868dSwnj 				cmd = TM_WIRG;
5041aa546d4Swnj 			else
505905d868dSwnj 				cmd = TM_WCOM;
5061aa546d4Swnj 		} else
507905d868dSwnj 			cmd = TM_RCOM;
508f6e77a22Swnj 		um->um_tab.b_active = SIO;
509905d868dSwnj 		um->um_cmd = sc->sc_dens|cmd;
51006eb541fSwnj #ifdef notdef
5115c653392Swnj 		if (tmreverseop(sc->sc_lastcmd))
512905d868dSwnj 			while (addr->tmer & TMER_SDWN)
5132bdeb6ecSmckusick 				DELAY(10),tmgapsdcnt++;
5145c653392Swnj 		sc->sc_lastcmd = TM_RCOM;		/* will serve */
51506eb541fSwnj #endif
51649868192Sroot 		sc->sc_timo = 60;	/* premature, but should serve */
517d1dce5b3Swnj 		(void) ubago(ui);
5181aa546d4Swnj 		return;
5191aa546d4Swnj 	}
520884ec674Swnj 	/*
521905d868dSwnj 	 * Tape positioned incorrectly;
522905d868dSwnj 	 * set to seek forwards or backwards to the correct spot.
523905d868dSwnj 	 * This happens for raw tapes only on error retries.
524884ec674Swnj 	 */
525f6e77a22Swnj 	um->um_tab.b_active = SSEEK;
5268cc29115Ssam 	if (blkno < bdbtofsb(bp->b_blkno)) {
5275c653392Swnj 		bp->b_command = TM_SFORW;
5288cc29115Ssam 		addr->tmbc = blkno - bdbtofsb(bp->b_blkno);
5291aa546d4Swnj 	} else {
5305c653392Swnj 		bp->b_command = TM_SREV;
5318cc29115Ssam 		addr->tmbc = bdbtofsb(bp->b_blkno) - blkno;
5321aa546d4Swnj 	}
533c5d20ef9Sroot 	sc->sc_timo = imin(imax(10 * -addr->tmbc, 60), 5 * 60);
5345c653392Swnj dobpcmd:
53506eb541fSwnj #ifdef notdef
536905d868dSwnj 	/*
537905d868dSwnj 	 * It is strictly necessary to wait for the tape
538905d868dSwnj 	 * to stop before changing directions, but the TC11
539905d868dSwnj 	 * handles this for us.
540905d868dSwnj 	 */
5415c653392Swnj 	if (tmreverseop(sc->sc_lastcmd) != tmreverseop(bp->b_command))
5425c653392Swnj 		while (addr->tmer & TM_SDWN)
5432bdeb6ecSmckusick 			DELAY(10),tmgapsdcnt++;
5445c653392Swnj 	sc->sc_lastcmd = bp->b_command;
54506eb541fSwnj #endif
546905d868dSwnj 	/*
547905d868dSwnj 	 * Do the command in bp.
548905d868dSwnj 	 */
549905d868dSwnj 	addr->tmcs = (sc->sc_dens | bp->b_command);
5501aa546d4Swnj 	return;
5511aa546d4Swnj 
5521aa546d4Swnj next:
553884ec674Swnj 	/*
554884ec674Swnj 	 * Done with this operation due to error or
555884ec674Swnj 	 * the fact that it doesn't do anything.
556884ec674Swnj 	 * Release UBA resources (if any), dequeue
557884ec674Swnj 	 * the transfer and continue processing this slave.
558884ec674Swnj 	 */
559884ec674Swnj 	if (um->um_ubinfo)
560c5a2c486Swnj 		ubadone(um);
561884ec674Swnj 	um->um_tab.b_errcnt = 0;
562884ec674Swnj 	dp->b_actf = bp->av_forw;
5631aa546d4Swnj 	iodone(bp);
5641aa546d4Swnj 	goto loop;
5651aa546d4Swnj }
5661aa546d4Swnj 
567884ec674Swnj /*
568884ec674Swnj  * The UNIBUS resources we needed have been
569884ec674Swnj  * allocated to us; start the device.
570884ec674Swnj  */
tmdgo(um)5718aea9ba3Swnj tmdgo(um)
572b28bc7fcSwnj 	register struct uba_ctlr *um;
57309e5bb10Swnj {
57481a5e293Sroot 	register struct tmdevice *addr = (struct tmdevice *)um->um_addr;
575f6e77a22Swnj 
5768aea9ba3Swnj 	addr->tmba = um->um_ubinfo;
5778aea9ba3Swnj 	addr->tmcs = um->um_cmd | ((um->um_ubinfo >> 12) & 0x30);
57809e5bb10Swnj }
57909e5bb10Swnj 
580884ec674Swnj /*
581884ec674Swnj  * Tm interrupt routine.
582884ec674Swnj  */
583f6e77a22Swnj /*ARGSUSED*/
tmintr(tm11)58465061276Swnj tmintr(tm11)
58565061276Swnj 	int tm11;
5861aa546d4Swnj {
587884ec674Swnj 	struct buf *dp;
5881aa546d4Swnj 	register struct buf *bp;
589b28bc7fcSwnj 	register struct uba_ctlr *um = tmminfo[tm11];
59081a5e293Sroot 	register struct tmdevice *addr;
591905d868dSwnj 	register struct te_softc *sc;
592905d868dSwnj 	int teunit;
5931aa546d4Swnj 	register state;
5941aa546d4Swnj 
595905d868dSwnj 	if ((dp = um->um_tab.b_actf) == NULL)
596905d868dSwnj 		return;
597905d868dSwnj 	bp = dp->b_actf;
598905d868dSwnj 	teunit = TEUNIT(bp->b_dev);
59981a5e293Sroot 	addr = (struct tmdevice *)tedinfo[teunit]->ui_addr;
600e7bec9e7Swnj 	sc = &te_softc[teunit];
601884ec674Swnj 	/*
602884ec674Swnj 	 * If last command was a rewind, and tape is still
603884ec674Swnj 	 * rewinding, wait for the rewind complete interrupt.
604884ec674Swnj 	 */
605884ec674Swnj 	if (um->um_tab.b_active == SREW) {
606884ec674Swnj 		um->um_tab.b_active = SCOM;
607e7bec9e7Swnj 		if (addr->tmer&TMER_RWS) {
608e7bec9e7Swnj 			sc->sc_timo = 5*60;		/* 5 minutes */
6091aa546d4Swnj 			return;
610884ec674Swnj 		}
611e7bec9e7Swnj 	}
612884ec674Swnj 	/*
613884ec674Swnj 	 * An operation completed... record status
614884ec674Swnj 	 */
61549868192Sroot 	sc->sc_timo = INF;
616cb8c8d5fSkarels 	if (um->um_tab.b_active == SIO)
617ba3a7840Smarc 	if (um->um_tab.b_active == SIO)
61835277208Skarels 		sc->sc_ioerreg = addr->tmer;
619f6e77a22Swnj 	sc->sc_dsreg = addr->tmcs;
620f6e77a22Swnj 	sc->sc_erreg = addr->tmer;
621f6e77a22Swnj 	sc->sc_resid = addr->tmbc;
6221aa546d4Swnj 	if ((bp->b_flags & B_READ) == 0)
623884ec674Swnj 		sc->sc_lastiow = 1;
624f6e77a22Swnj 	state = um->um_tab.b_active;
625f6e77a22Swnj 	um->um_tab.b_active = 0;
626884ec674Swnj 	/*
627884ec674Swnj 	 * Check for errors.
628884ec674Swnj 	 */
629884ec674Swnj 	if (addr->tmcs&TM_ERR) {
630905d868dSwnj 		while (addr->tmer & TMER_SDWN)
6312bdeb6ecSmckusick 			DELAY(10);			/* await settle down */
632884ec674Swnj 		/*
633905d868dSwnj 		 * If we hit the end of the tape file, update our position.
634884ec674Swnj 		 */
635905d868dSwnj 		if (addr->tmer&TMER_EOF) {
6361aa546d4Swnj 			tmseteof(bp);		/* set blkno and nxrec */
637884ec674Swnj 			state = SCOM;		/* force completion */
638884ec674Swnj 			/*
639884ec674Swnj 			 * Stuff bc so it will be unstuffed correctly
640884ec674Swnj 			 * later to get resid.
641884ec674Swnj 			 */
64209e5bb10Swnj 			addr->tmbc = -bp->b_bcount;
643884ec674Swnj 			goto opdone;
6441aa546d4Swnj 		}
645884ec674Swnj 		/*
646905d868dSwnj 		 * If we were reading raw tape and the only error was that the
647905d868dSwnj 		 * record was too long, then we don't consider this an error.
648884ec674Swnj 		 */
649de98104dSbostic 		if ((bp->b_flags & (B_READ|B_RAW)) == (B_READ|B_RAW) &&
650905d868dSwnj 		    (addr->tmer&(TMER_HARD|TMER_SOFT)) == TMER_RLE)
651884ec674Swnj 			goto ignoreerr;
652884ec674Swnj 		/*
653884ec674Swnj 		 * If error is not hard, and this was an i/o operation
654884ec674Swnj 		 * retry up to 8 times.
655884ec674Swnj 		 */
656905d868dSwnj 		if ((addr->tmer&TMER_HARD)==0 && state==SIO) {
657ba3a7840Smarc 			if (um->um_tab.b_errcnt++ < 8) {
658edb110f3Skarels 				if (tmdiag)
659edb110f3Skarels 					log(LOG_DEBUG,
660edb110f3Skarels 					    "te%d: soft error bn%d er=%b\n",
661edb110f3Skarels 					    minor(bp->b_dev)&03,
662edb110f3Skarels 					    bp->b_blkno, sc->sc_erreg,
663edb110f3Skarels 					    TMER_BITS);
664ba3a7840Smarc 				sc->sc_blkno++;		/* force backspace */
665c5a2c486Swnj 				ubadone(um);
666884ec674Swnj 				goto opcont;
6671aa546d4Swnj 			}
668884ec674Swnj 		} else
669884ec674Swnj 			/*
670884ec674Swnj 			 * Hard or non-i/o errors on non-raw tape
671884ec674Swnj 			 * cause it to close.
672884ec674Swnj 			 */
673de98104dSbostic 			if ((bp->b_flags&B_RAW) == 0 && sc->sc_openf > 0)
674f6e77a22Swnj 				sc->sc_openf = -1;
675884ec674Swnj 		/*
676884ec674Swnj 		 * Couldn't recover error
677884ec674Swnj 		 */
6780f129637Smarc 		tprintf(sc->sc_tpr,
6799fa410e8Sralph 		    "te%d: hard error bn%d er=%b\n", minor(bp->b_dev)&03,
680905d868dSwnj 		    bp->b_blkno, sc->sc_erreg, TMER_BITS);
6812bdeb6ecSmckusick #ifdef	AVIV
6822bdeb6ecSmckusick 		if (tmdiag) {
6832bdeb6ecSmckusick 			addr->tmmr = DAB;
6842bdeb6ecSmckusick 			printf("reject code 0%o", addr->tmmr & DAB_MASK);
6852bdeb6ecSmckusick 			addr->tmmr = DTS;
6862bdeb6ecSmckusick 			if (addr->tmmr & DTS_MASK)
6872bdeb6ecSmckusick 			    printf(", dead track 0%o", addr->tmmr & DTS_MASK);
6882bdeb6ecSmckusick 			addr->tmmr = RWERR;
6892bdeb6ecSmckusick 			printf(", read/write errors %b\n",
6902bdeb6ecSmckusick 			    addr->tmmr & RWERR_MASK,
6912bdeb6ecSmckusick 			    RWERR_BITS);
6922bdeb6ecSmckusick 			addr->tmmr = DRSENSE;
6932bdeb6ecSmckusick 			printf("drive sense %b, ", addr->tmmr & DRSENSE_MASK,
6942bdeb6ecSmckusick 			    DRSENSE_BITS);
6952bdeb6ecSmckusick 			printf("fsr %b\n", addr->tmfsr, FSR_BITS);
6962bdeb6ecSmckusick 		}
6972bdeb6ecSmckusick #endif AVIV
6981aa546d4Swnj 		bp->b_flags |= B_ERROR;
699884ec674Swnj 		goto opdone;
7001aa546d4Swnj 	}
701884ec674Swnj 	/*
702884ec674Swnj 	 * Advance tape control FSM.
703884ec674Swnj 	 */
704884ec674Swnj ignoreerr:
7051aa546d4Swnj 	switch (state) {
7061aa546d4Swnj 
7071aa546d4Swnj 	case SIO:
708884ec674Swnj 		/*
709884ec674Swnj 		 * Read/write increments tape block number
710884ec674Swnj 		 */
711f6e77a22Swnj 		sc->sc_blkno++;
712edb110f3Skarels 		sc->sc_blks++;
713edb110f3Skarels 		if (um->um_tab.b_errcnt)
714edb110f3Skarels 			sc->sc_softerrs++;
715884ec674Swnj 		goto opdone;
7161aa546d4Swnj 
7171aa546d4Swnj 	case SCOM:
718884ec674Swnj 		/*
719905d868dSwnj 		 * For forward/backward space record update current position.
720884ec674Swnj 		 */
721905d868dSwnj 		if (bp == &ctmbuf[TMUNIT(bp->b_dev)])
722e06aa688Skarels 		switch ((int)bp->b_command) {
7231aa546d4Swnj 
724884ec674Swnj 		case TM_SFORW:
725884ec674Swnj 			sc->sc_blkno -= bp->b_repcnt;
726905d868dSwnj 			break;
727884ec674Swnj 
728884ec674Swnj 		case TM_SREV:
729f6e77a22Swnj 			sc->sc_blkno += bp->b_repcnt;
730905d868dSwnj 			break;
7311aa546d4Swnj 		}
732905d868dSwnj 		goto opdone;
7331aa546d4Swnj 
7341aa546d4Swnj 	case SSEEK:
7358cc29115Ssam 		sc->sc_blkno = bdbtofsb(bp->b_blkno);
736884ec674Swnj 		goto opcont;
7371aa546d4Swnj 
7381aa546d4Swnj 	default:
739884ec674Swnj 		panic("tmintr");
7401aa546d4Swnj 	}
741884ec674Swnj opdone:
742884ec674Swnj 	/*
743884ec674Swnj 	 * Reset error count and remove
744884ec674Swnj 	 * from device queue.
745884ec674Swnj 	 */
746884ec674Swnj 	um->um_tab.b_errcnt = 0;
747884ec674Swnj 	dp->b_actf = bp->av_forw;
748435dbbafSkarels 	/*
749435dbbafSkarels 	 * Check resid; watch out for resid >32767 (tmbc not negative).
750435dbbafSkarels 	 */
7510b1c5df3Skarels 	bp->b_resid = ((int) -addr->tmbc) & 0xffff;
752c5a2c486Swnj 	ubadone(um);
753884ec674Swnj 	iodone(bp);
754884ec674Swnj 	/*
755884ec674Swnj 	 * Circulate slave to end of controller
756884ec674Swnj 	 * queue to give other slaves a chance.
757884ec674Swnj 	 */
758884ec674Swnj 	um->um_tab.b_actf = dp->b_forw;
759884ec674Swnj 	if (dp->b_actf) {
760884ec674Swnj 		dp->b_forw = NULL;
761884ec674Swnj 		if (um->um_tab.b_actf == NULL)
762884ec674Swnj 			um->um_tab.b_actf = dp;
763884ec674Swnj 		else
764884ec674Swnj 			um->um_tab.b_actl->b_forw = dp;
765884ec674Swnj 		um->um_tab.b_actl = dp;
766884ec674Swnj 	}
767884ec674Swnj 	if (um->um_tab.b_actf == 0)
768884ec674Swnj 		return;
769884ec674Swnj opcont:
770884ec674Swnj 	tmstart(um);
7711aa546d4Swnj }
7721aa546d4Swnj 
tmtimer(dev)77349868192Sroot tmtimer(dev)
77449868192Sroot 	int dev;
77549868192Sroot {
77649868192Sroot 	register struct te_softc *sc = &te_softc[TEUNIT(dev)];
7778b4d5c75Sroot 	register short x;
77849868192Sroot 
77949868192Sroot 	if (sc->sc_timo != INF && (sc->sc_timo -= 5) < 0) {
78080b07d7fSroot 		printf("te%d: lost interrupt\n", TEUNIT(dev));
78149868192Sroot 		sc->sc_timo = INF;
7828b4d5c75Sroot 		x = spl5();
78349868192Sroot 		tmintr(TMUNIT(dev));
7848b4d5c75Sroot 		(void) splx(x);
78549868192Sroot 	}
786c5d20ef9Sroot 	timeout(tmtimer, (caddr_t)dev, 5*hz);
78749868192Sroot }
78849868192Sroot 
tmseteof(bp)7891aa546d4Swnj tmseteof(bp)
7901aa546d4Swnj 	register struct buf *bp;
7911aa546d4Swnj {
792905d868dSwnj 	register int teunit = TEUNIT(bp->b_dev);
79381a5e293Sroot 	register struct tmdevice *addr =
79481a5e293Sroot 	    (struct tmdevice *)tedinfo[teunit]->ui_addr;
795905d868dSwnj 	register struct te_softc *sc = &te_softc[teunit];
7961aa546d4Swnj 
797905d868dSwnj 	if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) {
7988cc29115Ssam 		if (sc->sc_blkno > bdbtofsb(bp->b_blkno)) {
7991aa546d4Swnj 			/* reversing */
8008cc29115Ssam 			sc->sc_nxrec = bdbtofsb(bp->b_blkno) - addr->tmbc;
801f6e77a22Swnj 			sc->sc_blkno = sc->sc_nxrec;
8021aa546d4Swnj 		} else {
8031aa546d4Swnj 			/* spacing forward */
8048cc29115Ssam 			sc->sc_blkno = bdbtofsb(bp->b_blkno) + addr->tmbc;
805f6e77a22Swnj 			sc->sc_nxrec = sc->sc_blkno - 1;
8061aa546d4Swnj 		}
8071aa546d4Swnj 		return;
8081aa546d4Swnj 	}
8091aa546d4Swnj 	/* eof on read */
8108cc29115Ssam 	sc->sc_nxrec = bdbtofsb(bp->b_blkno);
8111aa546d4Swnj }
8121aa546d4Swnj 
tmreset(uban)813884ec674Swnj tmreset(uban)
814884ec674Swnj 	int uban;
815884ec674Swnj {
816b28bc7fcSwnj 	register struct uba_ctlr *um;
817905d868dSwnj 	register tm11, teunit;
818b28bc7fcSwnj 	register struct uba_device *ui;
819884ec674Swnj 	register struct buf *dp;
820884ec674Swnj 
82165061276Swnj 	for (tm11 = 0; tm11 < NTM; tm11++) {
82265061276Swnj 		if ((um = tmminfo[tm11]) == 0 || um->um_alive == 0 ||
823884ec674Swnj 		   um->um_ubanum != uban)
824884ec674Swnj 			continue;
82506eb541fSwnj 		printf(" tm%d", tm11);
826884ec674Swnj 		um->um_tab.b_active = 0;
827884ec674Swnj 		um->um_tab.b_actf = um->um_tab.b_actl = 0;
828884ec674Swnj 		if (um->um_ubinfo) {
829884ec674Swnj 			printf("<%d>", (um->um_ubinfo>>28)&0xf);
83061831adaSsam 			um->um_ubinfo = 0;
831884ec674Swnj 		}
83281a5e293Sroot 		((struct tmdevice *)(um->um_addr))->tmcs = TM_DCLR;
833905d868dSwnj 		for (teunit = 0; teunit < NTE; teunit++) {
834905d868dSwnj 			if ((ui = tedinfo[teunit]) == 0 || ui->ui_mi != um ||
835905d868dSwnj 			    ui->ui_alive == 0)
836884ec674Swnj 				continue;
837905d868dSwnj 			dp = &teutab[teunit];
838884ec674Swnj 			dp->b_active = 0;
839884ec674Swnj 			dp->b_forw = 0;
840884ec674Swnj 			if (um->um_tab.b_actf == NULL)
841884ec674Swnj 				um->um_tab.b_actf = dp;
842884ec674Swnj 			else
843884ec674Swnj 				um->um_tab.b_actl->b_forw = dp;
844884ec674Swnj 			um->um_tab.b_actl = dp;
84549868192Sroot 			if (te_softc[teunit].sc_openf > 0)
846905d868dSwnj 				te_softc[teunit].sc_openf = -1;
847884ec674Swnj 		}
848884ec674Swnj 		tmstart(um);
849884ec674Swnj 	}
850884ec674Swnj }
851884ec674Swnj 
8521aa546d4Swnj /*ARGSUSED*/
tmioctl(dev,cmd,data,flag)853b5e757a2Ssam tmioctl(dev, cmd, data, flag)
854b5e757a2Ssam 	caddr_t data;
8551aa546d4Swnj 	dev_t dev;
8561aa546d4Swnj {
857905d868dSwnj 	int teunit = TEUNIT(dev);
858905d868dSwnj 	register struct te_softc *sc = &te_softc[teunit];
859905d868dSwnj 	register struct buf *bp = &ctmbuf[TMUNIT(dev)];
8601aa546d4Swnj 	register callcount;
8611ba79fc0Ssklower 	int fcount, error = 0;
862b5e757a2Ssam 	struct mtop *mtop;
863b5e757a2Ssam 	struct mtget *mtget;
8641aa546d4Swnj 	/* we depend of the values and order of the MT codes here */
865884ec674Swnj 	static tmops[] =
866884ec674Swnj 	   {TM_WEOF,TM_SFORW,TM_SREV,TM_SFORW,TM_SREV,TM_REW,TM_OFFL,TM_SENSE};
8671aa546d4Swnj 
8681aa546d4Swnj 	switch (cmd) {
869b5e757a2Ssam 
8701aa546d4Swnj 	case MTIOCTOP:	/* tape operation */
871b5e757a2Ssam 		mtop = (struct mtop *)data;
872b5e757a2Ssam 		switch (mtop->mt_op) {
873b5e757a2Ssam 
874884ec674Swnj 		case MTWEOF:
875b5e757a2Ssam 			callcount = mtop->mt_count;
876884ec674Swnj 			fcount = 1;
877884ec674Swnj 			break;
878b5e757a2Ssam 
879884ec674Swnj 		case MTFSF: case MTBSF:
880b5e757a2Ssam 			callcount = mtop->mt_count;
8811aa546d4Swnj 			fcount = INF;
8821aa546d4Swnj 			break;
883b5e757a2Ssam 
8841aa546d4Swnj 		case MTFSR: case MTBSR:
8851aa546d4Swnj 			callcount = 1;
886b5e757a2Ssam 			fcount = mtop->mt_count;
8871aa546d4Swnj 			break;
888b5e757a2Ssam 
889f35ef42fSkre 		case MTREW: case MTOFFL: case MTNOP:
8901aa546d4Swnj 			callcount = 1;
8911aa546d4Swnj 			fcount = 1;
8921aa546d4Swnj 			break;
893b5e757a2Ssam 
8941aa546d4Swnj 		default:
895ae3ac2f8Sroot 			return (ENXIO);
8961aa546d4Swnj 		}
897ae3ac2f8Sroot 		if (callcount <= 0 || fcount <= 0)
898ae3ac2f8Sroot 			return (EINVAL);
899884ec674Swnj 		while (--callcount >= 0) {
900b5e757a2Ssam 			tmcommand(dev, tmops[mtop->mt_op], fcount);
901b5e757a2Ssam 			if ((mtop->mt_op == MTFSR || mtop->mt_op == MTBSR) &&
902ae3ac2f8Sroot 			    bp->b_resid)
903ae3ac2f8Sroot 				return (EIO);
904905d868dSwnj 			if ((bp->b_flags&B_ERROR) || sc->sc_erreg&TMER_BOT)
9051aa546d4Swnj 				break;
9061aa546d4Swnj 		}
9071ba79fc0Ssklower 		if (bp->b_flags&B_ERROR)
9081ba79fc0Ssklower 			if ((error = bp->b_error)==0)
9091ba79fc0Ssklower 				return (EIO);
9101ba79fc0Ssklower 		return (error);
911b5e757a2Ssam 
9121aa546d4Swnj 	case MTIOCGET:
913b5e757a2Ssam 		mtget = (struct mtget *)data;
914b5e757a2Ssam 		mtget->mt_dsreg = sc->sc_dsreg;
915b5e757a2Ssam 		mtget->mt_erreg = sc->sc_erreg;
916b5e757a2Ssam 		mtget->mt_resid = sc->sc_resid;
917b5e757a2Ssam 		mtget->mt_type = MT_ISTM;
918ae3ac2f8Sroot 		break;
919b5e757a2Ssam 
9201aa546d4Swnj 	default:
921ae3ac2f8Sroot 		return (ENXIO);
9221aa546d4Swnj 	}
923ae3ac2f8Sroot 	return (0);
9241aa546d4Swnj }
9251aa546d4Swnj 
9261aa546d4Swnj #define	DBSIZE	20
9271aa546d4Swnj 
tmdump()928f906751dSwnj tmdump()
929f906751dSwnj {
930b28bc7fcSwnj 	register struct uba_device *ui;
93109e5bb10Swnj 	register struct uba_regs *up;
93281a5e293Sroot 	register struct tmdevice *addr;
9336a858426Skre 	int blk, num;
9346a858426Skre 	int start;
9351aa546d4Swnj 
9366a858426Skre 	start = 0;
9376a858426Skre 	num = maxfree;
9386a858426Skre #define	phys(a,b)	((b)((int)(a)&0x7fffffff))
939905d868dSwnj 	if (tedinfo[0] == 0)
94010d3a9f7Swnj 		return (ENXIO);
941905d868dSwnj 	ui = phys(tedinfo[0], struct uba_device *);
94209e5bb10Swnj 	up = phys(ui->ui_hd, struct uba_hd *)->uh_physuba;
94309e5bb10Swnj 	ubainit(up);
944f35ef42fSkre 	DELAY(1000000);
94581a5e293Sroot 	addr = (struct tmdevice *)ui->ui_physaddr;
94609e5bb10Swnj 	tmwait(addr);
947884ec674Swnj 	addr->tmcs = TM_DCLR | TM_GO;
9481aa546d4Swnj 	while (num > 0) {
9491aa546d4Swnj 		blk = num > DBSIZE ? DBSIZE : num;
95009e5bb10Swnj 		tmdwrite(start, blk, addr, up);
9511aa546d4Swnj 		start += blk;
9521aa546d4Swnj 		num -= blk;
9531aa546d4Swnj 	}
954f6e77a22Swnj 	tmeof(addr);
955f6e77a22Swnj 	tmeof(addr);
9566a858426Skre 	tmwait(addr);
95710d3a9f7Swnj 	if (addr->tmcs&TM_ERR)
95810d3a9f7Swnj 		return (EIO);
959884ec674Swnj 	addr->tmcs = TM_REW | TM_GO;
9606a858426Skre 	tmwait(addr);
961f906751dSwnj 	return (0);
9621aa546d4Swnj }
9631aa546d4Swnj 
tmdwrite(dbuf,num,addr,up)964884ec674Swnj tmdwrite(dbuf, num, addr, up)
965884ec674Swnj 	register dbuf, num;
96681a5e293Sroot 	register struct tmdevice *addr;
96709e5bb10Swnj 	struct uba_regs *up;
9681aa546d4Swnj {
96909e5bb10Swnj 	register struct pte *io;
97009e5bb10Swnj 	register int npf;
9711f8db90aSwnj 
97209e5bb10Swnj 	tmwait(addr);
97309e5bb10Swnj 	io = up->uba_map;
9741aa546d4Swnj 	npf = num+1;
9751aa546d4Swnj 	while (--npf != 0)
976b28bc7fcSwnj 		 *(int *)io++ = (dbuf++ | (1<<UBAMR_DPSHIFT) | UBAMR_MRV);
97709e5bb10Swnj 	*(int *)io = 0;
97809e5bb10Swnj 	addr->tmbc = -(num*NBPG);
97909e5bb10Swnj 	addr->tmba = 0;
980884ec674Swnj 	addr->tmcs = TM_WCOM | TM_GO;
9811aa546d4Swnj }
9821aa546d4Swnj 
tmwait(addr)98309e5bb10Swnj tmwait(addr)
98481a5e293Sroot 	register struct tmdevice *addr;
9851aa546d4Swnj {
9861f8db90aSwnj 	register s;
9871aa546d4Swnj 
9881aa546d4Swnj 	do
98909e5bb10Swnj 		s = addr->tmcs;
990884ec674Swnj 	while ((s & TM_CUR) == 0);
9911aa546d4Swnj }
9921aa546d4Swnj 
99309e5bb10Swnj tmeof(addr)
99481a5e293Sroot 	struct tmdevice *addr;
9951aa546d4Swnj {
9961aa546d4Swnj 
99709e5bb10Swnj 	tmwait(addr);
998884ec674Swnj 	addr->tmcs = TM_WEOF | TM_GO;
9991aa546d4Swnj }
10001aa546d4Swnj #endif
1001