xref: /csrg-svn/sys/vax/uba/tm.c (revision 3524)
1*3524Swnj /*	tm.c	4.36	81/04/15	*/
21919Swnj 
32709Swnj #include "te.h"
43519Sroot #include "ts.h"
52630Swnj #if NTM > 0
61919Swnj /*
72630Swnj  * TM11/TE10 tape driver
82471Swnj  *
93095Swnj  * TODO:
103095Swnj  *	test driver with more than one slave
113095Swnj  *	test driver with more than one controller
123095Swnj  *	test reset code
133095Swnj  *	what happens if you offline tape during rewind?
143095Swnj  *	test using file system on tape
151919Swnj  */
161919Swnj #include "../h/param.h"
173141Swnj #include "../h/systm.h"
181919Swnj #include "../h/buf.h"
191919Swnj #include "../h/dir.h"
201919Swnj #include "../h/conf.h"
211919Swnj #include "../h/user.h"
221919Swnj #include "../h/file.h"
231919Swnj #include "../h/map.h"
241919Swnj #include "../h/pte.h"
252574Swnj #include "../h/vm.h"
262982Swnj #include "../h/ubareg.h"
272982Swnj #include "../h/ubavar.h"
281919Swnj #include "../h/mtio.h"
291919Swnj #include "../h/ioctl.h"
302363Swnj #include "../h/cmap.h"
312396Swnj #include "../h/cpu.h"
321919Swnj 
332396Swnj #include "../h/tmreg.h"
341919Swnj 
353095Swnj /*
363095Swnj  * There is a ctmbuf per tape controller.
373095Swnj  * It is used as the token to pass to the internal routines
383095Swnj  * to execute tape ioctls, and also acts as a lock on the slaves
393095Swnj  * on the controller, since there is only one per controller.
403095Swnj  * In particular, when the tape is rewinding on close we release
413095Swnj  * the user process but any further attempts to use the tape drive
423095Swnj  * before the rewind completes will hang waiting for ctmbuf.
433095Swnj  */
443095Swnj struct	buf	ctmbuf[NTM];
451919Swnj 
463095Swnj /*
473095Swnj  * Raw tape operations use rtmbuf.  The driver
483095Swnj  * notices when rtmbuf is being used and allows the user
493095Swnj  * program to continue after errors and read records
503095Swnj  * not of the standard length (BSIZE).
513095Swnj  */
523095Swnj struct	buf	rtmbuf[NTM];
533095Swnj 
543095Swnj /*
553095Swnj  * Driver unibus interface routines and variables.
563095Swnj  */
572608Swnj int	tmprobe(), tmslave(), tmattach(), tmdgo(), tmintr();
582982Swnj struct	uba_ctlr *tmminfo[NTM];
593095Swnj struct	uba_device *tedinfo[NTE];
603095Swnj struct	buf teutab[NTE];
613095Swnj short	tetotm[NTE];
622458Swnj u_short	tmstd[] = { 0772520, 0 };
632396Swnj struct	uba_driver tmdriver =
643095Swnj  { tmprobe, tmslave, tmattach, tmdgo, tmstd, "te", tedinfo, "tm", tmminfo, 0 };
651919Swnj 
661919Swnj /* bits in minor device */
673095Swnj #define	TEUNIT(dev)	(minor(dev)&03)
683095Swnj #define	TMUNIT(dev)	(tetotm[TEUNIT(dev)])
691919Swnj #define	T_NOREWIND	04
701919Swnj #define	T_1600BPI	08
711919Swnj 
721919Swnj #define	INF	(daddr_t)1000000L
731919Swnj 
742608Swnj /*
752608Swnj  * Software state per tape transport.
763095Swnj  *
773095Swnj  * 1. A tape drive is a unique-open device; we refuse opens when it is already.
783095Swnj  * 2. We keep track of the current position on a block tape and seek
793095Swnj  *    before operations by forward/back spacing if necessary.
803095Swnj  * 3. We remember if the last operation was a write on a tape, so if a tape
813095Swnj  *    is open read write and the last thing done is a write we can
823095Swnj  *    write a standard end of tape mark (two eofs).
833095Swnj  * 4. We remember the status registers after the last command, using
843095Swnj  *    then internally and returning them to the SENSE ioctl.
853095Swnj  * 5. We remember the last density the tape was used at.  If it is
863095Swnj  *    not a BOT when we start using it and we are writing, we don't
873095Swnj  *    let the density be changed.
882608Swnj  */
893095Swnj struct	te_softc {
902608Swnj 	char	sc_openf;	/* lock against multiple opens */
912608Swnj 	char	sc_lastiow;	/* last op was a write */
922608Swnj 	daddr_t	sc_blkno;	/* block number, for block device tape */
933095Swnj 	daddr_t	sc_nxrec;	/* position of end of tape, if known */
942608Swnj 	u_short	sc_erreg;	/* copy of last erreg */
952608Swnj 	u_short	sc_dsreg;	/* copy of last dsreg */
962608Swnj 	short	sc_resid;	/* copy of last bc */
973105Swnj #ifdef unneeded
982670Swnj 	short	sc_lastcmd;	/* last command to handle direction changes */
992928Swnj #endif
1003095Swnj 	u_short	sc_dens;	/* prototype command with density info */
1013495Sroot 	daddr_t	sc_timo;	/* time until timeout expires */
1023495Sroot 	short	sc_tact;	/* timeout is active */
1033095Swnj } te_softc[NTM];
1043105Swnj #ifdef unneeded
1053105Swnj int	tmgapsdcnt;		/* DEBUG */
1063105Swnj #endif
1071919Swnj 
1082608Swnj /*
1093095Swnj  * States for um->um_tab.b_active, the per controller state flag.
1103095Swnj  * This is used to sequence control in the driver.
1112608Swnj  */
1121919Swnj #define	SSEEK	1		/* seeking */
1131919Swnj #define	SIO	2		/* doing seq i/o */
1141919Swnj #define	SCOM	3		/* sending control command */
1152608Swnj #define	SREW	4		/* sending a drive rewind */
1161919Swnj 
1173519Sroot #if NTS > 0
1182426Skre /*
1193519Sroot  * Kludge to get around fact that we don't really
1203519Sroot  * check if a ts is there... if there are both tm's and ts's
1213519Sroot  * declared in the system, then this driver sets havetm to 1
1223519Sroot  * if it finds a tm, and ts just pretends there isn't a ts.
1233519Sroot  */
1243519Sroot int	havetm = 0;
1253519Sroot #endif
1263519Sroot /*
1272426Skre  * Determine if there is a controller for
1282426Skre  * a tm at address reg.  Our goal is to make the
1292426Skre  * device interrupt.
1302426Skre  */
1312608Swnj tmprobe(reg)
1322396Swnj 	caddr_t reg;
1332396Swnj {
1343095Swnj 	register int br, cvec;		/* must be r11,r10; value-result */
1352426Skre 
1362608Swnj #ifdef lint
1373105Swnj 	br = 0; cvec = br; br = cvec;
1382608Swnj #endif
1392608Swnj 	((struct device *)reg)->tmcs = TM_IE;
1402396Swnj 	/*
1412630Swnj 	 * If this is a tm11, it ought to have interrupted
1422396Swnj 	 * by now, if it isn't (ie: it is a ts04) then we just
1432458Swnj 	 * hope that it didn't interrupt, so autoconf will ignore it.
1442458Swnj 	 * Just in case, we will reference one
1452396Swnj 	 * of the more distant registers, and hope for a machine
1462458Swnj 	 * check, or similar disaster if this is a ts.
1472471Swnj 	 *
1482471Swnj 	 * Note: on an 11/780, badaddr will just generate
1492471Swnj 	 * a uba error for a ts; but our caller will notice that
1502471Swnj 	 * so we won't check for it.
1512396Swnj 	 */
1523105Swnj 	if (badaddr((caddr_t)&((struct device *)reg)->tmrd, 2))
1532458Swnj 		return (0);
1542458Swnj 	return (1);
1552396Swnj }
1562396Swnj 
1572608Swnj /*
1582608Swnj  * Due to a design flaw, we cannot ascertain if the tape
1592608Swnj  * exists or not unless it is on line - ie: unless a tape is
1602608Swnj  * mounted. This is too servere a restriction to bear,
1612608Swnj  * so all units are assumed to exist.
1622608Swnj  */
1632608Swnj /*ARGSUSED*/
1642574Swnj tmslave(ui, reg)
1652982Swnj 	struct uba_device *ui;
1662396Swnj 	caddr_t reg;
1672396Swnj {
1682458Swnj 
1692458Swnj 	return (1);
1702396Swnj }
1712396Swnj 
1722608Swnj /*
1733095Swnj  * Record attachment of the unit to the controller.
1742608Swnj  */
1752608Swnj /*ARGSUSED*/
1762608Swnj tmattach(ui)
1772982Swnj 	struct uba_device *ui;
1782608Swnj {
1792608Swnj 
1803519Sroot #if NTS > 0
1813519Sroot 	havetm = 1;
1823519Sroot #endif
1833095Swnj 	/*
1843095Swnj 	 * Tetotm is used in TMUNIT to index the ctmbuf and rtmbuf
1853095Swnj 	 * arrays given a te unit number.
1863095Swnj 	 */
1873095Swnj 	tetotm[ui->ui_unit] = ui->ui_mi->um_ctlr;
1882608Swnj }
1892608Swnj 
1903495Sroot int	tmtimer();
1912608Swnj /*
1922608Swnj  * Open the device.  Tapes are unique open
1932608Swnj  * devices, so we refuse if it is already open.
1942608Swnj  * We also check that a tape is available, and
1953095Swnj  * don't block waiting here; if you want to wait
1963095Swnj  * for a tape you should timeout in user code.
1972608Swnj  */
1981919Swnj tmopen(dev, flag)
1991919Swnj 	dev_t dev;
2001919Swnj 	int flag;
2011919Swnj {
2023095Swnj 	register int teunit;
2032982Swnj 	register struct uba_device *ui;
2043095Swnj 	register struct te_softc *sc;
2053209Swnj 	int olddens, dens;
2061919Swnj 
2073095Swnj 	teunit = TEUNIT(dev);
2083095Swnj 	if (teunit>=NTE || (sc = &te_softc[teunit])->sc_openf ||
2093095Swnj 	    (ui = tedinfo[teunit]) == 0 || ui->ui_alive == 0) {
2102608Swnj 		u.u_error = ENXIO;
2111919Swnj 		return;
2121919Swnj 	}
2133209Swnj 	olddens = sc->sc_dens;
2143209Swnj 	dens = TM_IE | TM_GO | (ui->ui_slave << 8);
2153209Swnj 	if ((minor(dev) & T_1600BPI) == 0)
2163209Swnj 		dens |= TM_D800;
2173209Swnj 	sc->sc_dens = dens;
2183141Swnj get:
2192608Swnj 	tmcommand(dev, TM_SENSE, 1);
2203141Swnj 	if (sc->sc_erreg&TMER_SDWN) {
2213141Swnj 		sleep((caddr_t)&lbolt, PZERO+1);
2223141Swnj 		goto get;
2233141Swnj 	}
2243209Swnj 	sc->sc_dens = olddens;
2253095Swnj 	if ((sc->sc_erreg&(TMER_SELR|TMER_TUR)) != (TMER_SELR|TMER_TUR) ||
2263174Swnj 	    (flag&FWRITE) && (sc->sc_erreg&TMER_WRL) ||
2273095Swnj 	    (sc->sc_erreg&TMER_BOT) == 0 && (flag&FWRITE) &&
2283174Swnj 		dens != sc->sc_dens) {
2293095Swnj 		/*
2303095Swnj 		 * Not online or density switch in mid-tape or write locked.
2313095Swnj 		 */
2322471Swnj 		u.u_error = EIO;
2332608Swnj 		return;
2341919Swnj 	}
2352608Swnj 	sc->sc_openf = 1;
2362471Swnj 	sc->sc_blkno = (daddr_t)0;
2372471Swnj 	sc->sc_nxrec = INF;
2382608Swnj 	sc->sc_lastiow = 0;
2393095Swnj 	sc->sc_dens = dens;
2403495Sroot 	(void) spl6();
2413495Sroot 	if (sc->sc_tact == 0) {
2423495Sroot 		sc->sc_timo = INF;
2433495Sroot 		sc->sc_tact = 1;
2443495Sroot 		timeout(tmtimer, dev, 5*hz);
2453495Sroot 	}
2463495Sroot 	(void) spl0();
2471919Swnj }
2481919Swnj 
2492608Swnj /*
2502608Swnj  * Close tape device.
2512608Swnj  *
2522608Swnj  * If tape was open for writing or last operation was
2532608Swnj  * a write, then write two EOF's and backspace over the last one.
2542608Swnj  * Unless this is a non-rewinding special file, rewind the tape.
2552608Swnj  * Make the tape available to others.
2562608Swnj  */
2571919Swnj tmclose(dev, flag)
2581919Swnj 	register dev_t dev;
2591919Swnj 	register flag;
2601919Swnj {
2613095Swnj 	register struct te_softc *sc = &te_softc[TEUNIT(dev)];
2621919Swnj 
2632608Swnj 	if (flag == FWRITE || (flag&FWRITE) && sc->sc_lastiow) {
2642608Swnj 		tmcommand(dev, TM_WEOF, 1);
2652608Swnj 		tmcommand(dev, TM_WEOF, 1);
2662608Swnj 		tmcommand(dev, TM_SREV, 1);
2671919Swnj 	}
2681919Swnj 	if ((minor(dev)&T_NOREWIND) == 0)
2693095Swnj 		/*
2703095Swnj 		 * 0 count means don't hang waiting for rewind complete
2713095Swnj 		 * rather ctmbuf stays busy until the operation completes
2723095Swnj 		 * preventing further opens from completing by
2733095Swnj 		 * preventing a TM_SENSE from completing.
2743095Swnj 		 */
2753095Swnj 		tmcommand(dev, TM_REW, 0);
2762471Swnj 	sc->sc_openf = 0;
2771919Swnj }
2781919Swnj 
2792608Swnj /*
2802608Swnj  * Execute a command on the tape drive
2812608Swnj  * a specified number of times.
2822608Swnj  */
2832574Swnj tmcommand(dev, com, count)
2841919Swnj 	dev_t dev;
2851919Swnj 	int com, count;
2861919Swnj {
2871919Swnj 	register struct buf *bp;
2881919Swnj 
2892608Swnj 	bp = &ctmbuf[TMUNIT(dev)];
2901919Swnj 	(void) spl5();
2911919Swnj 	while (bp->b_flags&B_BUSY) {
2923095Swnj 		/*
2933095Swnj 		 * This special check is because B_BUSY never
2943095Swnj 		 * gets cleared in the non-waiting rewind case.
2953095Swnj 		 */
2963141Swnj 		if (bp->b_repcnt == 0 && (bp->b_flags&B_DONE))
2973095Swnj 			break;
2981919Swnj 		bp->b_flags |= B_WANTED;
2991919Swnj 		sleep((caddr_t)bp, PRIBIO);
3001919Swnj 	}
3011919Swnj 	bp->b_flags = B_BUSY|B_READ;
3021919Swnj 	(void) spl0();
3031919Swnj 	bp->b_dev = dev;
3041919Swnj 	bp->b_repcnt = -count;
3051919Swnj 	bp->b_command = com;
3061919Swnj 	bp->b_blkno = 0;
3071919Swnj 	tmstrategy(bp);
3083095Swnj 	/*
3093095Swnj 	 * In case of rewind from close, don't wait.
3103095Swnj 	 * This is the only case where count can be 0.
3113095Swnj 	 */
3123095Swnj 	if (count == 0)
3133095Swnj 		return;
3141919Swnj 	iowait(bp);
3151919Swnj 	if (bp->b_flags&B_WANTED)
3161919Swnj 		wakeup((caddr_t)bp);
3171919Swnj 	bp->b_flags &= B_ERROR;
3181919Swnj }
3191919Swnj 
3202608Swnj /*
3213095Swnj  * Queue a tape operation.
3222608Swnj  */
3231919Swnj tmstrategy(bp)
3241919Swnj 	register struct buf *bp;
3251919Swnj {
3263095Swnj 	int teunit = TEUNIT(bp->b_dev);
3272982Swnj 	register struct uba_ctlr *um;
3282608Swnj 	register struct buf *dp;
3291919Swnj 
3302608Swnj 	/*
3312608Swnj 	 * Put transfer at end of unit queue
3322608Swnj 	 */
3333095Swnj 	dp = &teutab[teunit];
3341919Swnj 	bp->av_forw = NULL;
3351919Swnj 	(void) spl5();
3362608Swnj 	if (dp->b_actf == NULL) {
3372608Swnj 		dp->b_actf = bp;
3382608Swnj 		/*
3392608Swnj 		 * Transport not already active...
3402608Swnj 		 * put at end of controller queue.
3412608Swnj 		 */
3422608Swnj 		dp->b_forw = NULL;
3433095Swnj 		um = tedinfo[teunit]->ui_mi;
3442608Swnj 		if (um->um_tab.b_actf == NULL)
3452608Swnj 			um->um_tab.b_actf = dp;
3462608Swnj 		else
3472608Swnj 			um->um_tab.b_actl->b_forw = dp;
3482608Swnj 		um->um_tab.b_actl = dp;
3492608Swnj 	} else
3502608Swnj 		dp->b_actl->av_forw = bp;
3512608Swnj 	dp->b_actl = bp;
3522608Swnj 	/*
3532608Swnj 	 * If the controller is not busy, get
3542608Swnj 	 * it going.
3552608Swnj 	 */
3562608Swnj 	if (um->um_tab.b_active == 0)
3572608Swnj 		tmstart(um);
3581919Swnj 	(void) spl0();
3591919Swnj }
3601919Swnj 
3612608Swnj /*
3622608Swnj  * Start activity on a tm controller.
3632608Swnj  */
3642608Swnj tmstart(um)
3652982Swnj 	register struct uba_ctlr *um;
3661919Swnj {
3672608Swnj 	register struct buf *bp, *dp;
3682608Swnj 	register struct device *addr = (struct device *)um->um_addr;
3693095Swnj 	register struct te_softc *sc;
3702982Swnj 	register struct uba_device *ui;
3713095Swnj 	int teunit, cmd;
3722471Swnj 	daddr_t blkno;
3731919Swnj 
3742608Swnj 	/*
3752608Swnj 	 * Look for an idle transport on the controller.
3762608Swnj 	 */
3771919Swnj loop:
3782608Swnj 	if ((dp = um->um_tab.b_actf) == NULL)
3791919Swnj 		return;
3802608Swnj 	if ((bp = dp->b_actf) == NULL) {
3812608Swnj 		um->um_tab.b_actf = dp->b_forw;
3822608Swnj 		goto loop;
3832608Swnj 	}
3843095Swnj 	teunit = TEUNIT(bp->b_dev);
3853095Swnj 	ui = tedinfo[teunit];
3862608Swnj 	/*
3872608Swnj 	 * Record pre-transfer status (e.g. for TM_SENSE)
3882608Swnj 	 */
3893095Swnj 	sc = &te_softc[teunit];
3902608Swnj 	addr = (struct device *)um->um_addr;
3912608Swnj 	addr->tmcs = (ui->ui_slave << 8);
3922471Swnj 	sc->sc_dsreg = addr->tmcs;
3932471Swnj 	sc->sc_erreg = addr->tmer;
3942471Swnj 	sc->sc_resid = addr->tmbc;
3952608Swnj 	/*
3962608Swnj 	 * Default is that last command was NOT a write command;
3972608Swnj 	 * if we do a write command we will notice this in tmintr().
3982608Swnj 	 */
3993493Sroot 	sc->sc_lastiow = 0;
4002608Swnj 	if (sc->sc_openf < 0 || (addr->tmcs&TM_CUR) == 0) {
4012608Swnj 		/*
4023095Swnj 		 * Have had a hard error on a non-raw tape
4033095Swnj 		 * or the tape unit is now unavailable
4043095Swnj 		 * (e.g. taken off line).
4052608Swnj 		 */
4062608Swnj 		bp->b_flags |= B_ERROR;
4071919Swnj 		goto next;
4081919Swnj 	}
4093095Swnj 	if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) {
4103095Swnj 		/*
4113095Swnj 		 * Execute control operation with the specified count.
4123095Swnj 		 */
4132608Swnj 		if (bp->b_command == TM_SENSE)
4142608Swnj 			goto next;
4153495Sroot 		/*
4163495Sroot 		 * Set next state; give 5 minutes to complete
4173495Sroot 		 * rewind, or 10 seconds per iteration (minimum 60
4183495Sroot 		 * seconds and max 5 minute) to complete other ops.
4193495Sroot 		 */
4203495Sroot 		if (bp->b_command == TM_REW) {
4213495Sroot 			um->um_tab.b_active = SREW;
4223495Sroot 			sc->sc_timo = 5 * 60;
4233495Sroot 		} else {
4243495Sroot 			um->um_tab.b_active = SCOM;
4253495Sroot 			sc->sc_timo = min(max(10 * bp->b_repcnt, 60), 5 * 60);
4263495Sroot 		}
4272608Swnj 		if (bp->b_command == TM_SFORW || bp->b_command == TM_SREV)
4282608Swnj 			addr->tmbc = bp->b_repcnt;
4292670Swnj 		goto dobpcmd;
4302608Swnj 	}
4312608Swnj 	/*
4323095Swnj 	 * The following checks handle boundary cases for operation
4333095Swnj 	 * on non-raw tapes.  On raw tapes the initialization of
4343095Swnj 	 * sc->sc_nxrec by tmphys causes them to be skipped normally
4353095Swnj 	 * (except in the case of retries).
4363095Swnj 	 */
4373095Swnj 	if (dbtofsb(bp->b_blkno) > sc->sc_nxrec) {
4383095Swnj 		/*
4393095Swnj 		 * Can't read past known end-of-file.
4403095Swnj 		 */
4413095Swnj 		bp->b_flags |= B_ERROR;
4423095Swnj 		bp->b_error = ENXIO;
4433095Swnj 		goto next;
4443095Swnj 	}
4453095Swnj 	if (dbtofsb(bp->b_blkno) == sc->sc_nxrec &&
4463095Swnj 	    bp->b_flags&B_READ) {
4473095Swnj 		/*
4483095Swnj 		 * Reading at end of file returns 0 bytes.
4493095Swnj 		 */
4503095Swnj 		bp->b_resid = bp->b_bcount;
4513095Swnj 		clrbuf(bp);
4523095Swnj 		goto next;
4533095Swnj 	}
4543095Swnj 	if ((bp->b_flags&B_READ) == 0)
4553095Swnj 		/*
4563095Swnj 		 * Writing sets EOF
4573095Swnj 		 */
4583095Swnj 		sc->sc_nxrec = dbtofsb(bp->b_blkno) + 1;
4593095Swnj 	/*
4602608Swnj 	 * If the data transfer command is in the correct place,
4612608Swnj 	 * set up all the registers except the csr, and give
4622608Swnj 	 * control over to the UNIBUS adapter routines, to
4632608Swnj 	 * wait for resources to start the i/o.
4642608Swnj 	 */
4652471Swnj 	if ((blkno = sc->sc_blkno) == dbtofsb(bp->b_blkno)) {
4662396Swnj 		addr->tmbc = -bp->b_bcount;
4671919Swnj 		if ((bp->b_flags&B_READ) == 0) {
4682471Swnj 			if (um->um_tab.b_errcnt)
4693095Swnj 				cmd = TM_WIRG;
4701919Swnj 			else
4713095Swnj 				cmd = TM_WCOM;
4721919Swnj 		} else
4733095Swnj 			cmd = TM_RCOM;
4742471Swnj 		um->um_tab.b_active = SIO;
4753095Swnj 		um->um_cmd = sc->sc_dens|cmd;
4762928Swnj #ifdef notdef
4772670Swnj 		if (tmreverseop(sc->sc_lastcmd))
4783095Swnj 			while (addr->tmer & TMER_SDWN)
4792670Swnj 				tmgapsdcnt++;
4802670Swnj 		sc->sc_lastcmd = TM_RCOM;		/* will serve */
4812928Swnj #endif
4823495Sroot 		sc->sc_timo = 60;	/* premature, but should serve */
4833105Swnj 		(void) ubago(ui);
4841919Swnj 		return;
4851919Swnj 	}
4862608Swnj 	/*
4873095Swnj 	 * Tape positioned incorrectly;
4883095Swnj 	 * set to seek forwards or backwards to the correct spot.
4893095Swnj 	 * This happens for raw tapes only on error retries.
4902608Swnj 	 */
4912471Swnj 	um->um_tab.b_active = SSEEK;
4921919Swnj 	if (blkno < dbtofsb(bp->b_blkno)) {
4932670Swnj 		bp->b_command = TM_SFORW;
4942396Swnj 		addr->tmbc = blkno - dbtofsb(bp->b_blkno);
4951919Swnj 	} else {
4962670Swnj 		bp->b_command = TM_SREV;
4972396Swnj 		addr->tmbc = dbtofsb(bp->b_blkno) - blkno;
4981919Swnj 	}
4993495Sroot 	sc->sc_timo = min(max(10 * -addr->tmbc, 60), 5 * 60);
5002670Swnj dobpcmd:
5012928Swnj #ifdef notdef
5023095Swnj 	/*
5033095Swnj 	 * It is strictly necessary to wait for the tape
5043095Swnj 	 * to stop before changing directions, but the TC11
5053095Swnj 	 * handles this for us.
5063095Swnj 	 */
5072670Swnj 	if (tmreverseop(sc->sc_lastcmd) != tmreverseop(bp->b_command))
5082670Swnj 		while (addr->tmer & TM_SDWN)
5092670Swnj 			tmgapsdcnt++;
5102670Swnj 	sc->sc_lastcmd = bp->b_command;
5112928Swnj #endif
5123095Swnj 	/*
5133095Swnj 	 * Do the command in bp.
5143095Swnj 	 */
5153095Swnj 	addr->tmcs = (sc->sc_dens | bp->b_command);
5161919Swnj 	return;
5171919Swnj 
5181919Swnj next:
5192608Swnj 	/*
5202608Swnj 	 * Done with this operation due to error or
5212608Swnj 	 * the fact that it doesn't do anything.
5222608Swnj 	 * Release UBA resources (if any), dequeue
5232608Swnj 	 * the transfer and continue processing this slave.
5242608Swnj 	 */
5252608Swnj 	if (um->um_ubinfo)
5262617Swnj 		ubadone(um);
5272608Swnj 	um->um_tab.b_errcnt = 0;
5282608Swnj 	dp->b_actf = bp->av_forw;
5291919Swnj 	iodone(bp);
5301919Swnj 	goto loop;
5311919Swnj }
5321919Swnj 
5332608Swnj /*
5342608Swnj  * The UNIBUS resources we needed have been
5352608Swnj  * allocated to us; start the device.
5362608Swnj  */
5372574Swnj tmdgo(um)
5382982Swnj 	register struct uba_ctlr *um;
5391919Swnj {
5402574Swnj 	register struct device *addr = (struct device *)um->um_addr;
5412471Swnj 
5422574Swnj 	addr->tmba = um->um_ubinfo;
5432574Swnj 	addr->tmcs = um->um_cmd | ((um->um_ubinfo >> 12) & 0x30);
5442396Swnj }
5452396Swnj 
5462608Swnj /*
5472608Swnj  * Tm interrupt routine.
5482608Swnj  */
5492471Swnj /*ARGSUSED*/
5502630Swnj tmintr(tm11)
5512630Swnj 	int tm11;
5522396Swnj {
5532608Swnj 	struct buf *dp;
5541919Swnj 	register struct buf *bp;
5552982Swnj 	register struct uba_ctlr *um = tmminfo[tm11];
5563095Swnj 	register struct device *addr;
5573095Swnj 	register struct te_softc *sc;
5583095Swnj 	int teunit;
5591919Swnj 	register state;
5601919Swnj 
5613095Swnj 	if ((dp = um->um_tab.b_actf) == NULL)
5623095Swnj 		return;
5633095Swnj 	bp = dp->b_actf;
5643095Swnj 	teunit = TEUNIT(bp->b_dev);
5653095Swnj 	addr = (struct device *)tedinfo[teunit]->ui_addr;
566*3524Swnj 	sc = &te_softc[teunit];
5672608Swnj 	/*
5682608Swnj 	 * If last command was a rewind, and tape is still
5692608Swnj 	 * rewinding, wait for the rewind complete interrupt.
5702608Swnj 	 */
5712608Swnj 	if (um->um_tab.b_active == SREW) {
5722608Swnj 		um->um_tab.b_active = SCOM;
573*3524Swnj 		if (addr->tmer&TMER_RWS) {
574*3524Swnj 			sc->sc_timo = 5*60;		/* 5 minutes */
5752608Swnj 			return;
576*3524Swnj 		}
5771919Swnj 	}
5782608Swnj 	/*
5792608Swnj 	 * An operation completed... record status
5802608Swnj 	 */
5813495Sroot 	sc->sc_timo = INF;
5822471Swnj 	sc->sc_dsreg = addr->tmcs;
5832471Swnj 	sc->sc_erreg = addr->tmer;
5842471Swnj 	sc->sc_resid = addr->tmbc;
5851919Swnj 	if ((bp->b_flags & B_READ) == 0)
5862608Swnj 		sc->sc_lastiow = 1;
5872471Swnj 	state = um->um_tab.b_active;
5882471Swnj 	um->um_tab.b_active = 0;
5892608Swnj 	/*
5902608Swnj 	 * Check for errors.
5912608Swnj 	 */
5922608Swnj 	if (addr->tmcs&TM_ERR) {
5933095Swnj 		while (addr->tmer & TMER_SDWN)
5941919Swnj 			;			/* await settle down */
5952608Swnj 		/*
5963095Swnj 		 * If we hit the end of the tape file, update our position.
5972608Swnj 		 */
5983095Swnj 		if (addr->tmer&TMER_EOF) {
5992608Swnj 			tmseteof(bp);		/* set blkno and nxrec */
6002608Swnj 			state = SCOM;		/* force completion */
6012608Swnj 			/*
6022608Swnj 			 * Stuff bc so it will be unstuffed correctly
6032608Swnj 			 * later to get resid.
6042608Swnj 			 */
6052396Swnj 			addr->tmbc = -bp->b_bcount;
6062608Swnj 			goto opdone;
6071919Swnj 		}
6082608Swnj 		/*
6093095Swnj 		 * If we were reading raw tape and the only error was that the
6103095Swnj 		 * record was too long, then we don't consider this an error.
6112608Swnj 		 */
6123095Swnj 		if (bp == &rtmbuf[TMUNIT(bp->b_dev)] && (bp->b_flags&B_READ) &&
6133095Swnj 		    (addr->tmer&(TMER_HARD|TMER_SOFT)) == TMER_RLE)
6142608Swnj 			goto ignoreerr;
6152608Swnj 		/*
6162608Swnj 		 * If error is not hard, and this was an i/o operation
6172608Swnj 		 * retry up to 8 times.
6182608Swnj 		 */
6193095Swnj 		if ((addr->tmer&TMER_HARD)==0 && state==SIO) {
6202471Swnj 			if (++um->um_tab.b_errcnt < 7) {
6212471Swnj 				sc->sc_blkno++;
6222617Swnj 				ubadone(um);
6232608Swnj 				goto opcont;
6241919Swnj 			}
6252608Swnj 		} else
6262608Swnj 			/*
6272608Swnj 			 * Hard or non-i/o errors on non-raw tape
6282608Swnj 			 * cause it to close.
6292608Swnj 			 */
6303095Swnj 			if (sc->sc_openf>0 && bp != &rtmbuf[TMUNIT(bp->b_dev)])
6312608Swnj 				sc->sc_openf = -1;
6322608Swnj 		/*
6332608Swnj 		 * Couldn't recover error
6342608Swnj 		 */
6352928Swnj 		printf("te%d: hard error bn%d er=%b\n", minor(bp->b_dev)&03,
6363095Swnj 		    bp->b_blkno, sc->sc_erreg, TMER_BITS);
6371919Swnj 		bp->b_flags |= B_ERROR;
6382608Swnj 		goto opdone;
6391919Swnj 	}
6402608Swnj 	/*
6412608Swnj 	 * Advance tape control FSM.
6422608Swnj 	 */
6432608Swnj ignoreerr:
6441919Swnj 	switch (state) {
6451919Swnj 
6461919Swnj 	case SIO:
6472608Swnj 		/*
6482608Swnj 		 * Read/write increments tape block number
6492608Swnj 		 */
6502471Swnj 		sc->sc_blkno++;
6512608Swnj 		goto opdone;
6521919Swnj 
6531919Swnj 	case SCOM:
6542608Swnj 		/*
6553095Swnj 		 * For forward/backward space record update current position.
6562608Swnj 		 */
6573095Swnj 		if (bp == &ctmbuf[TMUNIT(bp->b_dev)])
6582608Swnj 		switch (bp->b_command) {
6591919Swnj 
6602608Swnj 		case TM_SFORW:
6612608Swnj 			sc->sc_blkno -= bp->b_repcnt;
6623095Swnj 			break;
6631919Swnj 
6642608Swnj 		case TM_SREV:
6652608Swnj 			sc->sc_blkno += bp->b_repcnt;
6663095Swnj 			break;
6671919Swnj 		}
6683095Swnj 		goto opdone;
6691919Swnj 
6701919Swnj 	case SSEEK:
6712471Swnj 		sc->sc_blkno = dbtofsb(bp->b_blkno);
6722608Swnj 		goto opcont;
6731919Swnj 
6741919Swnj 	default:
6752608Swnj 		panic("tmintr");
6762608Swnj 	}
6772608Swnj opdone:
6782608Swnj 	/*
6792608Swnj 	 * Reset error count and remove
6802608Swnj 	 * from device queue.
6812608Swnj 	 */
6822608Swnj 	um->um_tab.b_errcnt = 0;
6832608Swnj 	dp->b_actf = bp->av_forw;
6842608Swnj 	bp->b_resid = -addr->tmbc;
6852617Swnj 	ubadone(um);
6862608Swnj 	iodone(bp);
6872608Swnj 	/*
6882608Swnj 	 * Circulate slave to end of controller
6892608Swnj 	 * queue to give other slaves a chance.
6902608Swnj 	 */
6912608Swnj 	um->um_tab.b_actf = dp->b_forw;
6922608Swnj 	if (dp->b_actf) {
6932608Swnj 		dp->b_forw = NULL;
6942608Swnj 		if (um->um_tab.b_actf == NULL)
6952608Swnj 			um->um_tab.b_actf = dp;
6962608Swnj 		else
6972608Swnj 			um->um_tab.b_actl->b_forw = dp;
6982608Swnj 		um->um_tab.b_actl = dp;
6992608Swnj 	}
7002608Swnj 	if (um->um_tab.b_actf == 0)
7011919Swnj 		return;
7022608Swnj opcont:
7032608Swnj 	tmstart(um);
7041919Swnj }
7051919Swnj 
7063495Sroot tmtimer(dev)
7073495Sroot 	int dev;
7083495Sroot {
7093495Sroot 	register struct te_softc *sc = &te_softc[TEUNIT(dev)];
7103495Sroot 
7113495Sroot 	if (sc->sc_timo != INF && (sc->sc_timo -= 5) < 0) {
7123495Sroot 		printf("te%d: lost interrupt\n");
7133495Sroot 		sc->sc_timo = INF;
7143495Sroot 		(void) spl5();
7153495Sroot 		tmintr(TMUNIT(dev));
7163495Sroot 		(void) spl0();
7173495Sroot 	}
7183495Sroot 	timeout(tmtimer, dev, 5*hz);
7193495Sroot }
7203495Sroot 
7211919Swnj tmseteof(bp)
7221919Swnj 	register struct buf *bp;
7231919Swnj {
7243095Swnj 	register int teunit = TEUNIT(bp->b_dev);
7252396Swnj 	register struct device *addr =
7263095Swnj 	    (struct device *)tedinfo[teunit]->ui_addr;
7273095Swnj 	register struct te_softc *sc = &te_softc[teunit];
7281919Swnj 
7293095Swnj 	if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) {
7302471Swnj 		if (sc->sc_blkno > dbtofsb(bp->b_blkno)) {
7311919Swnj 			/* reversing */
7322471Swnj 			sc->sc_nxrec = dbtofsb(bp->b_blkno) - addr->tmbc;
7332471Swnj 			sc->sc_blkno = sc->sc_nxrec;
7341919Swnj 		} else {
7351919Swnj 			/* spacing forward */
7362471Swnj 			sc->sc_blkno = dbtofsb(bp->b_blkno) + addr->tmbc;
7372471Swnj 			sc->sc_nxrec = sc->sc_blkno - 1;
7381919Swnj 		}
7391919Swnj 		return;
7401919Swnj 	}
7411919Swnj 	/* eof on read */
7422471Swnj 	sc->sc_nxrec = dbtofsb(bp->b_blkno);
7431919Swnj }
7441919Swnj 
7451919Swnj tmread(dev)
7462608Swnj 	dev_t dev;
7471919Swnj {
7481919Swnj 
7491919Swnj 	tmphys(dev);
7502982Swnj 	if (u.u_error)
7512982Swnj 		return;
7522608Swnj 	physio(tmstrategy, &rtmbuf[TMUNIT(dev)], dev, B_READ, minphys);
7531919Swnj }
7541919Swnj 
7551919Swnj tmwrite(dev)
7562608Swnj 	dev_t dev;
7571919Swnj {
7581919Swnj 
7591919Swnj 	tmphys(dev);
7602982Swnj 	if (u.u_error)
7612982Swnj 		return;
7622608Swnj 	physio(tmstrategy, &rtmbuf[TMUNIT(dev)], dev, B_WRITE, minphys);
7631919Swnj }
7641919Swnj 
7653095Swnj /*
7663095Swnj  * Check that a raw device exists.
7673095Swnj  * If it does, set up sc_blkno and sc_nxrec
7683095Swnj  * so that the tape will appear positioned correctly.
7693095Swnj  */
7701919Swnj tmphys(dev)
7712608Swnj 	dev_t dev;
7721919Swnj {
7733095Swnj 	register int teunit = TEUNIT(dev);
7741919Swnj 	register daddr_t a;
7753095Swnj 	register struct te_softc *sc;
7763095Swnj 	register struct uba_device *ui;
7771919Swnj 
7783095Swnj 	if (teunit >= NTE || (ui=tedinfo[teunit]) == 0 || ui->ui_alive == 0) {
7792982Swnj 		u.u_error = ENXIO;
7802982Swnj 		return;
7812982Swnj 	}
7823095Swnj 	sc = &te_softc[teunit];
7831919Swnj 	a = dbtofsb(u.u_offset >> 9);
7842471Swnj 	sc->sc_blkno = a;
7852471Swnj 	sc->sc_nxrec = a + 1;
7861919Swnj }
7871919Swnj 
7882608Swnj tmreset(uban)
7892608Swnj 	int uban;
7902608Swnj {
7912982Swnj 	register struct uba_ctlr *um;
7923095Swnj 	register tm11, teunit;
7932982Swnj 	register struct uba_device *ui;
7942608Swnj 	register struct buf *dp;
7952608Swnj 
7962630Swnj 	for (tm11 = 0; tm11 < NTM; tm11++) {
7972630Swnj 		if ((um = tmminfo[tm11]) == 0 || um->um_alive == 0 ||
7982608Swnj 		   um->um_ubanum != uban)
7992608Swnj 			continue;
8002928Swnj 		printf(" tm%d", tm11);
8012608Swnj 		um->um_tab.b_active = 0;
8022608Swnj 		um->um_tab.b_actf = um->um_tab.b_actl = 0;
8032608Swnj 		if (um->um_ubinfo) {
8042608Swnj 			printf("<%d>", (um->um_ubinfo>>28)&0xf);
8052617Swnj 			ubadone(um);
8062608Swnj 		}
8072608Swnj 		((struct device *)(um->um_addr))->tmcs = TM_DCLR;
8083095Swnj 		for (teunit = 0; teunit < NTE; teunit++) {
8093095Swnj 			if ((ui = tedinfo[teunit]) == 0 || ui->ui_mi != um ||
8103095Swnj 			    ui->ui_alive == 0)
8112608Swnj 				continue;
8123095Swnj 			dp = &teutab[teunit];
8132608Swnj 			dp->b_active = 0;
8142608Swnj 			dp->b_forw = 0;
8152608Swnj 			if (um->um_tab.b_actf == NULL)
8162608Swnj 				um->um_tab.b_actf = dp;
8172608Swnj 			else
8182608Swnj 				um->um_tab.b_actl->b_forw = dp;
8192608Swnj 			um->um_tab.b_actl = dp;
8203495Sroot 			if (te_softc[teunit].sc_openf > 0)
8213495Sroot 				te_softc[teunit].sc_openf = -1;
8222608Swnj 		}
8232608Swnj 		tmstart(um);
8242608Swnj 	}
8252608Swnj }
8262608Swnj 
8271919Swnj /*ARGSUSED*/
8281919Swnj tmioctl(dev, cmd, addr, flag)
8291919Swnj 	caddr_t addr;
8301919Swnj 	dev_t dev;
8311919Swnj {
8323095Swnj 	int teunit = TEUNIT(dev);
8333095Swnj 	register struct te_softc *sc = &te_softc[teunit];
8343095Swnj 	register struct buf *bp = &ctmbuf[TMUNIT(dev)];
8351919Swnj 	register callcount;
8361919Swnj 	int fcount;
8371919Swnj 	struct mtop mtop;
8381919Swnj 	struct mtget mtget;
8391919Swnj 	/* we depend of the values and order of the MT codes here */
8402608Swnj 	static tmops[] =
8412608Swnj 	   {TM_WEOF,TM_SFORW,TM_SREV,TM_SFORW,TM_SREV,TM_REW,TM_OFFL,TM_SENSE};
8421919Swnj 
8432608Swnj 	switch (cmd) {
8441919Swnj 		case MTIOCTOP:	/* tape operation */
8451919Swnj 		if (copyin((caddr_t)addr, (caddr_t)&mtop, sizeof(mtop))) {
8461919Swnj 			u.u_error = EFAULT;
8471919Swnj 			return;
8481919Swnj 		}
8491919Swnj 		switch(mtop.mt_op) {
8502608Swnj 		case MTWEOF:
8511919Swnj 			callcount = mtop.mt_count;
8522608Swnj 			fcount = 1;
8532608Swnj 			break;
8542608Swnj 		case MTFSF: case MTBSF:
8552608Swnj 			callcount = mtop.mt_count;
8561919Swnj 			fcount = INF;
8571919Swnj 			break;
8581919Swnj 		case MTFSR: case MTBSR:
8591919Swnj 			callcount = 1;
8601919Swnj 			fcount = mtop.mt_count;
8611919Swnj 			break;
8622324Skre 		case MTREW: case MTOFFL: case MTNOP:
8631919Swnj 			callcount = 1;
8641919Swnj 			fcount = 1;
8651919Swnj 			break;
8661919Swnj 		default:
8671919Swnj 			u.u_error = ENXIO;
8681919Swnj 			return;
8691919Swnj 		}
8702608Swnj 		if (callcount <= 0 || fcount <= 0) {
8711919Swnj 			u.u_error = ENXIO;
8722608Swnj 			return;
8732608Swnj 		}
8742608Swnj 		while (--callcount >= 0) {
8752574Swnj 			tmcommand(dev, tmops[mtop.mt_op], fcount);
8761919Swnj 			if ((mtop.mt_op == MTFSR || mtop.mt_op == MTBSR) &&
8772608Swnj 			    bp->b_resid) {
8781919Swnj 				u.u_error = EIO;
8791919Swnj 				break;
8801919Swnj 			}
8813095Swnj 			if ((bp->b_flags&B_ERROR) || sc->sc_erreg&TMER_BOT)
8821919Swnj 				break;
8831919Swnj 		}
8842608Swnj 		geterror(bp);
8851919Swnj 		return;
8861919Swnj 	case MTIOCGET:
8872471Swnj 		mtget.mt_dsreg = sc->sc_dsreg;
8882471Swnj 		mtget.mt_erreg = sc->sc_erreg;
8892471Swnj 		mtget.mt_resid = sc->sc_resid;
8903482Sroot 		mtget.mt_type = MT_ISTM;
8911919Swnj 		if (copyout((caddr_t)&mtget, addr, sizeof(mtget)))
8921919Swnj 			u.u_error = EFAULT;
8931919Swnj 		return;
8941919Swnj 	default:
8951919Swnj 		u.u_error = ENXIO;
8961919Swnj 	}
8971919Swnj }
8981919Swnj 
8991919Swnj #define	DBSIZE	20
9001919Swnj 
9012363Swnj tmdump()
9022363Swnj {
9032982Swnj 	register struct uba_device *ui;
9042396Swnj 	register struct uba_regs *up;
9052396Swnj 	register struct device *addr;
9062426Skre 	int blk, num;
9072426Skre 	int start;
9081919Swnj 
9092426Skre 	start = 0;
9102426Skre 	num = maxfree;
9112426Skre #define	phys(a,b)	((b)((int)(a)&0x7fffffff))
9123095Swnj 	if (tedinfo[0] == 0)
9132887Swnj 		return (ENXIO);
9143095Swnj 	ui = phys(tedinfo[0], struct uba_device *);
9152396Swnj 	up = phys(ui->ui_hd, struct uba_hd *)->uh_physuba;
9163331Swnj 	ubainit(up);
9172324Skre 	DELAY(1000000);
9182396Swnj 	addr = (struct device *)ui->ui_physaddr;
9192396Swnj 	tmwait(addr);
9202608Swnj 	addr->tmcs = TM_DCLR | TM_GO;
9211919Swnj 	while (num > 0) {
9221919Swnj 		blk = num > DBSIZE ? DBSIZE : num;
9232396Swnj 		tmdwrite(start, blk, addr, up);
9241919Swnj 		start += blk;
9251919Swnj 		num -= blk;
9261919Swnj 	}
9272426Skre 	tmeof(addr);
9282426Skre 	tmeof(addr);
9292426Skre 	tmwait(addr);
9302887Swnj 	if (addr->tmcs&TM_ERR)
9312887Swnj 		return (EIO);
9322608Swnj 	addr->tmcs = TM_REW | TM_GO;
9332471Swnj 	tmwait(addr);
9342363Swnj 	return (0);
9351919Swnj }
9361919Swnj 
9372608Swnj tmdwrite(dbuf, num, addr, up)
9382608Swnj 	register dbuf, num;
9392396Swnj 	register struct device *addr;
9402396Swnj 	struct uba_regs *up;
9411919Swnj {
9422396Swnj 	register struct pte *io;
9432396Swnj 	register int npf;
9441928Swnj 
9452396Swnj 	tmwait(addr);
9462396Swnj 	io = up->uba_map;
9471919Swnj 	npf = num+1;
9481928Swnj 	while (--npf != 0)
9492982Swnj 		 *(int *)io++ = (dbuf++ | (1<<UBAMR_DPSHIFT) | UBAMR_MRV);
9502396Swnj 	*(int *)io = 0;
9512396Swnj 	addr->tmbc = -(num*NBPG);
9522396Swnj 	addr->tmba = 0;
9532608Swnj 	addr->tmcs = TM_WCOM | TM_GO;
9541919Swnj }
9551919Swnj 
9562396Swnj tmwait(addr)
9572396Swnj 	register struct device *addr;
9581919Swnj {
9591928Swnj 	register s;
9601919Swnj 
9611919Swnj 	do
9622396Swnj 		s = addr->tmcs;
9632608Swnj 	while ((s & TM_CUR) == 0);
9641919Swnj }
9651919Swnj 
9662396Swnj tmeof(addr)
9672396Swnj 	struct device *addr;
9681919Swnj {
9691919Swnj 
9702396Swnj 	tmwait(addr);
9712608Swnj 	addr->tmcs = TM_WEOF | TM_GO;
9721919Swnj }
9731919Swnj #endif
974