xref: /csrg-svn/sys/vax/uba/tm.c (revision 7632)
1*7632Ssam /*	tm.c	4.51	82/08/01	*/
21919Swnj 
32709Swnj #include "te.h"
43519Sroot #include "ts.h"
56343Swnj #if NTE > 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"
28*7632Ssam #include "../h/ioctl.h"
291919Swnj #include "../h/mtio.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 */
1036952Swnj } te_softc[NTE];
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 
1172426Skre /*
1182426Skre  * Determine if there is a controller for
1192426Skre  * a tm at address reg.  Our goal is to make the
1202426Skre  * device interrupt.
1212426Skre  */
1222608Swnj tmprobe(reg)
1232396Swnj 	caddr_t reg;
1242396Swnj {
1253095Swnj 	register int br, cvec;		/* must be r11,r10; value-result */
1262426Skre 
1272608Swnj #ifdef lint
1283105Swnj 	br = 0; cvec = br; br = cvec;
1294936Swnj 	tmintr(0);
1302608Swnj #endif
1315692Sroot 	((struct tmdevice *)reg)->tmcs = TM_IE;
1322396Swnj 	/*
1332630Swnj 	 * If this is a tm11, it ought to have interrupted
1342396Swnj 	 * by now, if it isn't (ie: it is a ts04) then we just
1352458Swnj 	 * hope that it didn't interrupt, so autoconf will ignore it.
1362458Swnj 	 * Just in case, we will reference one
1372396Swnj 	 * of the more distant registers, and hope for a machine
1382458Swnj 	 * check, or similar disaster if this is a ts.
1392471Swnj 	 *
1402471Swnj 	 * Note: on an 11/780, badaddr will just generate
1412471Swnj 	 * a uba error for a ts; but our caller will notice that
1422471Swnj 	 * so we won't check for it.
1432396Swnj 	 */
1445692Sroot 	if (badaddr((caddr_t)&((struct tmdevice *)reg)->tmrd, 2))
1452458Swnj 		return (0);
1467404Skre 	return (sizeof (struct tmdevice));
1472396Swnj }
1482396Swnj 
1492608Swnj /*
1502608Swnj  * Due to a design flaw, we cannot ascertain if the tape
1512608Swnj  * exists or not unless it is on line - ie: unless a tape is
1522608Swnj  * mounted. This is too servere a restriction to bear,
1532608Swnj  * so all units are assumed to exist.
1542608Swnj  */
1552608Swnj /*ARGSUSED*/
1562574Swnj tmslave(ui, reg)
1572982Swnj 	struct uba_device *ui;
1582396Swnj 	caddr_t reg;
1592396Swnj {
1602458Swnj 
1612458Swnj 	return (1);
1622396Swnj }
1632396Swnj 
1642608Swnj /*
1653095Swnj  * Record attachment of the unit to the controller.
1662608Swnj  */
1672608Swnj /*ARGSUSED*/
1682608Swnj tmattach(ui)
1692982Swnj 	struct uba_device *ui;
1702608Swnj {
1713095Swnj 	/*
1723095Swnj 	 * Tetotm is used in TMUNIT to index the ctmbuf and rtmbuf
1733095Swnj 	 * arrays given a te unit number.
1743095Swnj 	 */
1753095Swnj 	tetotm[ui->ui_unit] = ui->ui_mi->um_ctlr;
1762608Swnj }
1772608Swnj 
1783495Sroot int	tmtimer();
1792608Swnj /*
1802608Swnj  * Open the device.  Tapes are unique open
1812608Swnj  * devices, so we refuse if it is already open.
1822608Swnj  * We also check that a tape is available, and
1833095Swnj  * don't block waiting here; if you want to wait
1843095Swnj  * for a tape you should timeout in user code.
1852608Swnj  */
1861919Swnj tmopen(dev, flag)
1871919Swnj 	dev_t dev;
1881919Swnj 	int flag;
1891919Swnj {
1903095Swnj 	register int teunit;
1912982Swnj 	register struct uba_device *ui;
1923095Swnj 	register struct te_softc *sc;
1933209Swnj 	int olddens, dens;
1945437Sroot 	int s;
1951919Swnj 
1963095Swnj 	teunit = TEUNIT(dev);
1973095Swnj 	if (teunit>=NTE || (sc = &te_softc[teunit])->sc_openf ||
1983095Swnj 	    (ui = tedinfo[teunit]) == 0 || ui->ui_alive == 0) {
1992608Swnj 		u.u_error = ENXIO;
2001919Swnj 		return;
2011919Swnj 	}
2023209Swnj 	olddens = sc->sc_dens;
2033209Swnj 	dens = TM_IE | TM_GO | (ui->ui_slave << 8);
2043209Swnj 	if ((minor(dev) & T_1600BPI) == 0)
2053209Swnj 		dens |= TM_D800;
2063209Swnj 	sc->sc_dens = dens;
2073141Swnj get:
2082608Swnj 	tmcommand(dev, TM_SENSE, 1);
2093141Swnj 	if (sc->sc_erreg&TMER_SDWN) {
2103141Swnj 		sleep((caddr_t)&lbolt, PZERO+1);
2113141Swnj 		goto get;
2123141Swnj 	}
2133209Swnj 	sc->sc_dens = olddens;
2143710Sroot 	if ((sc->sc_erreg&(TMER_SELR|TMER_TUR)) != (TMER_SELR|TMER_TUR)) {
2153710Sroot 		uprintf("te%d: not online\n", teunit);
2162471Swnj 		u.u_error = EIO;
2172608Swnj 		return;
2181919Swnj 	}
2193710Sroot 	if ((flag&FWRITE) && (sc->sc_erreg&TMER_WRL)) {
2203710Sroot 		uprintf("te%d: no write ring\n", teunit);
2213710Sroot 		u.u_error = EIO;
2223710Sroot 		return;
2233710Sroot 	}
2243710Sroot 	if ((sc->sc_erreg&TMER_BOT) == 0 && (flag&FWRITE) &&
2253710Sroot 	    dens != sc->sc_dens) {
2263710Sroot 		uprintf("te%d: can't change density in mid-tape\n", teunit);
2273710Sroot 		u.u_error = EIO;
2283710Sroot 		return;
2293710Sroot 	}
2302608Swnj 	sc->sc_openf = 1;
2312471Swnj 	sc->sc_blkno = (daddr_t)0;
2322471Swnj 	sc->sc_nxrec = INF;
2332608Swnj 	sc->sc_lastiow = 0;
2343095Swnj 	sc->sc_dens = dens;
2355437Sroot 	s = spl6();
2363495Sroot 	if (sc->sc_tact == 0) {
2373495Sroot 		sc->sc_timo = INF;
2383495Sroot 		sc->sc_tact = 1;
2393629Sroot 		timeout(tmtimer, (caddr_t)dev, 5*hz);
2403495Sroot 	}
2415437Sroot 	splx(s);
2421919Swnj }
2431919Swnj 
2442608Swnj /*
2452608Swnj  * Close tape device.
2462608Swnj  *
2472608Swnj  * If tape was open for writing or last operation was
2482608Swnj  * a write, then write two EOF's and backspace over the last one.
2492608Swnj  * Unless this is a non-rewinding special file, rewind the tape.
2502608Swnj  * Make the tape available to others.
2512608Swnj  */
2521919Swnj tmclose(dev, flag)
2531919Swnj 	register dev_t dev;
2541919Swnj 	register flag;
2551919Swnj {
2563095Swnj 	register struct te_softc *sc = &te_softc[TEUNIT(dev)];
2571919Swnj 
2582608Swnj 	if (flag == FWRITE || (flag&FWRITE) && sc->sc_lastiow) {
2592608Swnj 		tmcommand(dev, TM_WEOF, 1);
2602608Swnj 		tmcommand(dev, TM_WEOF, 1);
2612608Swnj 		tmcommand(dev, TM_SREV, 1);
2621919Swnj 	}
2631919Swnj 	if ((minor(dev)&T_NOREWIND) == 0)
2643095Swnj 		/*
2653095Swnj 		 * 0 count means don't hang waiting for rewind complete
2663095Swnj 		 * rather ctmbuf stays busy until the operation completes
2673095Swnj 		 * preventing further opens from completing by
2683095Swnj 		 * preventing a TM_SENSE from completing.
2693095Swnj 		 */
2703095Swnj 		tmcommand(dev, TM_REW, 0);
2712471Swnj 	sc->sc_openf = 0;
2721919Swnj }
2731919Swnj 
2742608Swnj /*
2752608Swnj  * Execute a command on the tape drive
2762608Swnj  * a specified number of times.
2772608Swnj  */
2782574Swnj tmcommand(dev, com, count)
2791919Swnj 	dev_t dev;
2801919Swnj 	int com, count;
2811919Swnj {
2821919Swnj 	register struct buf *bp;
2835437Sroot 	register int s;
2841919Swnj 
2852608Swnj 	bp = &ctmbuf[TMUNIT(dev)];
2865437Sroot 	s = spl5();
2871919Swnj 	while (bp->b_flags&B_BUSY) {
2883095Swnj 		/*
2893095Swnj 		 * This special check is because B_BUSY never
2903095Swnj 		 * gets cleared in the non-waiting rewind case.
2913095Swnj 		 */
2923141Swnj 		if (bp->b_repcnt == 0 && (bp->b_flags&B_DONE))
2933095Swnj 			break;
2941919Swnj 		bp->b_flags |= B_WANTED;
2951919Swnj 		sleep((caddr_t)bp, PRIBIO);
2961919Swnj 	}
2971919Swnj 	bp->b_flags = B_BUSY|B_READ;
2985437Sroot 	splx(s);
2991919Swnj 	bp->b_dev = dev;
3001919Swnj 	bp->b_repcnt = -count;
3011919Swnj 	bp->b_command = com;
3021919Swnj 	bp->b_blkno = 0;
3031919Swnj 	tmstrategy(bp);
3043095Swnj 	/*
3053095Swnj 	 * In case of rewind from close, don't wait.
3063095Swnj 	 * This is the only case where count can be 0.
3073095Swnj 	 */
3083095Swnj 	if (count == 0)
3093095Swnj 		return;
3101919Swnj 	iowait(bp);
3111919Swnj 	if (bp->b_flags&B_WANTED)
3121919Swnj 		wakeup((caddr_t)bp);
3131919Swnj 	bp->b_flags &= B_ERROR;
3141919Swnj }
3151919Swnj 
3162608Swnj /*
3173095Swnj  * Queue a tape operation.
3182608Swnj  */
3191919Swnj tmstrategy(bp)
3201919Swnj 	register struct buf *bp;
3211919Swnj {
3223095Swnj 	int teunit = TEUNIT(bp->b_dev);
3232982Swnj 	register struct uba_ctlr *um;
3242608Swnj 	register struct buf *dp;
3255437Sroot 	int s;
3261919Swnj 
3272608Swnj 	/*
3282608Swnj 	 * Put transfer at end of unit queue
3292608Swnj 	 */
3303095Swnj 	dp = &teutab[teunit];
3311919Swnj 	bp->av_forw = NULL;
3325437Sroot 	s = spl5();
3333939Sbugs 	um = tedinfo[teunit]->ui_mi;
3342608Swnj 	if (dp->b_actf == NULL) {
3352608Swnj 		dp->b_actf = bp;
3362608Swnj 		/*
3372608Swnj 		 * Transport not already active...
3382608Swnj 		 * put at end of controller queue.
3392608Swnj 		 */
3402608Swnj 		dp->b_forw = NULL;
3412608Swnj 		if (um->um_tab.b_actf == NULL)
3422608Swnj 			um->um_tab.b_actf = dp;
3432608Swnj 		else
3442608Swnj 			um->um_tab.b_actl->b_forw = dp;
3452608Swnj 		um->um_tab.b_actl = dp;
3462608Swnj 	} else
3472608Swnj 		dp->b_actl->av_forw = bp;
3482608Swnj 	dp->b_actl = bp;
3492608Swnj 	/*
3502608Swnj 	 * If the controller is not busy, get
3512608Swnj 	 * it going.
3522608Swnj 	 */
3532608Swnj 	if (um->um_tab.b_active == 0)
3542608Swnj 		tmstart(um);
3555437Sroot 	splx(s);
3561919Swnj }
3571919Swnj 
3582608Swnj /*
3592608Swnj  * Start activity on a tm controller.
3602608Swnj  */
3612608Swnj tmstart(um)
3622982Swnj 	register struct uba_ctlr *um;
3631919Swnj {
3642608Swnj 	register struct buf *bp, *dp;
3655692Sroot 	register struct tmdevice *addr = (struct tmdevice *)um->um_addr;
3663095Swnj 	register struct te_softc *sc;
3672982Swnj 	register struct uba_device *ui;
3683095Swnj 	int teunit, cmd;
3692471Swnj 	daddr_t blkno;
3701919Swnj 
3712608Swnj 	/*
3722608Swnj 	 * Look for an idle transport on the controller.
3732608Swnj 	 */
3741919Swnj loop:
3752608Swnj 	if ((dp = um->um_tab.b_actf) == NULL)
3761919Swnj 		return;
3772608Swnj 	if ((bp = dp->b_actf) == NULL) {
3782608Swnj 		um->um_tab.b_actf = dp->b_forw;
3792608Swnj 		goto loop;
3802608Swnj 	}
3813095Swnj 	teunit = TEUNIT(bp->b_dev);
3823095Swnj 	ui = tedinfo[teunit];
3832608Swnj 	/*
3842608Swnj 	 * Record pre-transfer status (e.g. for TM_SENSE)
3852608Swnj 	 */
3863095Swnj 	sc = &te_softc[teunit];
3875692Sroot 	addr = (struct tmdevice *)um->um_addr;
3882608Swnj 	addr->tmcs = (ui->ui_slave << 8);
3892471Swnj 	sc->sc_dsreg = addr->tmcs;
3902471Swnj 	sc->sc_erreg = addr->tmer;
3912471Swnj 	sc->sc_resid = addr->tmbc;
3922608Swnj 	/*
3932608Swnj 	 * Default is that last command was NOT a write command;
3942608Swnj 	 * if we do a write command we will notice this in tmintr().
3952608Swnj 	 */
3963493Sroot 	sc->sc_lastiow = 0;
3972608Swnj 	if (sc->sc_openf < 0 || (addr->tmcs&TM_CUR) == 0) {
3982608Swnj 		/*
3993095Swnj 		 * Have had a hard error on a non-raw tape
4003095Swnj 		 * or the tape unit is now unavailable
4013095Swnj 		 * (e.g. taken off line).
4022608Swnj 		 */
4032608Swnj 		bp->b_flags |= B_ERROR;
4041919Swnj 		goto next;
4051919Swnj 	}
4063095Swnj 	if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) {
4073095Swnj 		/*
4083095Swnj 		 * Execute control operation with the specified count.
4093095Swnj 		 */
4102608Swnj 		if (bp->b_command == TM_SENSE)
4112608Swnj 			goto next;
4123495Sroot 		/*
4133495Sroot 		 * Set next state; give 5 minutes to complete
4143495Sroot 		 * rewind, or 10 seconds per iteration (minimum 60
4153629Sroot 		 * seconds and max 5 minutes) to complete other ops.
4163495Sroot 		 */
4173495Sroot 		if (bp->b_command == TM_REW) {
4183495Sroot 			um->um_tab.b_active = SREW;
4193495Sroot 			sc->sc_timo = 5 * 60;
4203495Sroot 		} else {
4213495Sroot 			um->um_tab.b_active = SCOM;
4224266Swnj 			sc->sc_timo =
4234266Swnj 			    imin(imax(10*(int)-bp->b_repcnt,60),5*60);
4243495Sroot 		}
4252608Swnj 		if (bp->b_command == TM_SFORW || bp->b_command == TM_SREV)
4262608Swnj 			addr->tmbc = bp->b_repcnt;
4272670Swnj 		goto dobpcmd;
4282608Swnj 	}
4292608Swnj 	/*
4303095Swnj 	 * The following checks handle boundary cases for operation
4313095Swnj 	 * on non-raw tapes.  On raw tapes the initialization of
4323095Swnj 	 * sc->sc_nxrec by tmphys causes them to be skipped normally
4333095Swnj 	 * (except in the case of retries).
4343095Swnj 	 */
4357381Ssam 	if (bdbtofsb(bp->b_blkno) > sc->sc_nxrec) {
4363095Swnj 		/*
4373095Swnj 		 * Can't read past known end-of-file.
4383095Swnj 		 */
4393095Swnj 		bp->b_flags |= B_ERROR;
4403095Swnj 		bp->b_error = ENXIO;
4413095Swnj 		goto next;
4423095Swnj 	}
4437381Ssam 	if (bdbtofsb(bp->b_blkno) == sc->sc_nxrec &&
4443095Swnj 	    bp->b_flags&B_READ) {
4453095Swnj 		/*
4463095Swnj 		 * Reading at end of file returns 0 bytes.
4473095Swnj 		 */
4483095Swnj 		bp->b_resid = bp->b_bcount;
4493095Swnj 		clrbuf(bp);
4503095Swnj 		goto next;
4513095Swnj 	}
4523095Swnj 	if ((bp->b_flags&B_READ) == 0)
4533095Swnj 		/*
4543095Swnj 		 * Writing sets EOF
4553095Swnj 		 */
4567381Ssam 		sc->sc_nxrec = bdbtofsb(bp->b_blkno) + 1;
4573095Swnj 	/*
4582608Swnj 	 * If the data transfer command is in the correct place,
4592608Swnj 	 * set up all the registers except the csr, and give
4602608Swnj 	 * control over to the UNIBUS adapter routines, to
4612608Swnj 	 * wait for resources to start the i/o.
4622608Swnj 	 */
4637381Ssam 	if ((blkno = sc->sc_blkno) == bdbtofsb(bp->b_blkno)) {
4642396Swnj 		addr->tmbc = -bp->b_bcount;
4651919Swnj 		if ((bp->b_flags&B_READ) == 0) {
4662471Swnj 			if (um->um_tab.b_errcnt)
4673095Swnj 				cmd = TM_WIRG;
4681919Swnj 			else
4693095Swnj 				cmd = TM_WCOM;
4701919Swnj 		} else
4713095Swnj 			cmd = TM_RCOM;
4722471Swnj 		um->um_tab.b_active = SIO;
4733095Swnj 		um->um_cmd = sc->sc_dens|cmd;
4742928Swnj #ifdef notdef
4752670Swnj 		if (tmreverseop(sc->sc_lastcmd))
4763095Swnj 			while (addr->tmer & TMER_SDWN)
4772670Swnj 				tmgapsdcnt++;
4782670Swnj 		sc->sc_lastcmd = TM_RCOM;		/* will serve */
4792928Swnj #endif
4803495Sroot 		sc->sc_timo = 60;	/* premature, but should serve */
4813105Swnj 		(void) ubago(ui);
4821919Swnj 		return;
4831919Swnj 	}
4842608Swnj 	/*
4853095Swnj 	 * Tape positioned incorrectly;
4863095Swnj 	 * set to seek forwards or backwards to the correct spot.
4873095Swnj 	 * This happens for raw tapes only on error retries.
4882608Swnj 	 */
4892471Swnj 	um->um_tab.b_active = SSEEK;
4907381Ssam 	if (blkno < bdbtofsb(bp->b_blkno)) {
4912670Swnj 		bp->b_command = TM_SFORW;
4927381Ssam 		addr->tmbc = blkno - bdbtofsb(bp->b_blkno);
4931919Swnj 	} else {
4942670Swnj 		bp->b_command = TM_SREV;
4957381Ssam 		addr->tmbc = bdbtofsb(bp->b_blkno) - blkno;
4961919Swnj 	}
4973629Sroot 	sc->sc_timo = imin(imax(10 * -addr->tmbc, 60), 5 * 60);
4982670Swnj dobpcmd:
4992928Swnj #ifdef notdef
5003095Swnj 	/*
5013095Swnj 	 * It is strictly necessary to wait for the tape
5023095Swnj 	 * to stop before changing directions, but the TC11
5033095Swnj 	 * handles this for us.
5043095Swnj 	 */
5052670Swnj 	if (tmreverseop(sc->sc_lastcmd) != tmreverseop(bp->b_command))
5062670Swnj 		while (addr->tmer & TM_SDWN)
5072670Swnj 			tmgapsdcnt++;
5082670Swnj 	sc->sc_lastcmd = bp->b_command;
5092928Swnj #endif
5103095Swnj 	/*
5113095Swnj 	 * Do the command in bp.
5123095Swnj 	 */
5133095Swnj 	addr->tmcs = (sc->sc_dens | bp->b_command);
5141919Swnj 	return;
5151919Swnj 
5161919Swnj next:
5172608Swnj 	/*
5182608Swnj 	 * Done with this operation due to error or
5192608Swnj 	 * the fact that it doesn't do anything.
5202608Swnj 	 * Release UBA resources (if any), dequeue
5212608Swnj 	 * the transfer and continue processing this slave.
5222608Swnj 	 */
5232608Swnj 	if (um->um_ubinfo)
5242617Swnj 		ubadone(um);
5252608Swnj 	um->um_tab.b_errcnt = 0;
5262608Swnj 	dp->b_actf = bp->av_forw;
5271919Swnj 	iodone(bp);
5281919Swnj 	goto loop;
5291919Swnj }
5301919Swnj 
5312608Swnj /*
5322608Swnj  * The UNIBUS resources we needed have been
5332608Swnj  * allocated to us; start the device.
5342608Swnj  */
5352574Swnj tmdgo(um)
5362982Swnj 	register struct uba_ctlr *um;
5371919Swnj {
5385692Sroot 	register struct tmdevice *addr = (struct tmdevice *)um->um_addr;
5392471Swnj 
5402574Swnj 	addr->tmba = um->um_ubinfo;
5412574Swnj 	addr->tmcs = um->um_cmd | ((um->um_ubinfo >> 12) & 0x30);
5422396Swnj }
5432396Swnj 
5442608Swnj /*
5452608Swnj  * Tm interrupt routine.
5462608Swnj  */
5472471Swnj /*ARGSUSED*/
5482630Swnj tmintr(tm11)
5492630Swnj 	int tm11;
5502396Swnj {
5512608Swnj 	struct buf *dp;
5521919Swnj 	register struct buf *bp;
5532982Swnj 	register struct uba_ctlr *um = tmminfo[tm11];
5545692Sroot 	register struct tmdevice *addr;
5553095Swnj 	register struct te_softc *sc;
5563095Swnj 	int teunit;
5571919Swnj 	register state;
5581919Swnj 
5593095Swnj 	if ((dp = um->um_tab.b_actf) == NULL)
5603095Swnj 		return;
5613095Swnj 	bp = dp->b_actf;
5623095Swnj 	teunit = TEUNIT(bp->b_dev);
5635692Sroot 	addr = (struct tmdevice *)tedinfo[teunit]->ui_addr;
5643524Swnj 	sc = &te_softc[teunit];
5652608Swnj 	/*
5662608Swnj 	 * If last command was a rewind, and tape is still
5672608Swnj 	 * rewinding, wait for the rewind complete interrupt.
5682608Swnj 	 */
5692608Swnj 	if (um->um_tab.b_active == SREW) {
5702608Swnj 		um->um_tab.b_active = SCOM;
5713524Swnj 		if (addr->tmer&TMER_RWS) {
5723524Swnj 			sc->sc_timo = 5*60;		/* 5 minutes */
5732608Swnj 			return;
5743524Swnj 		}
5751919Swnj 	}
5762608Swnj 	/*
5772608Swnj 	 * An operation completed... record status
5782608Swnj 	 */
5793495Sroot 	sc->sc_timo = INF;
5802471Swnj 	sc->sc_dsreg = addr->tmcs;
5812471Swnj 	sc->sc_erreg = addr->tmer;
5822471Swnj 	sc->sc_resid = addr->tmbc;
5831919Swnj 	if ((bp->b_flags & B_READ) == 0)
5842608Swnj 		sc->sc_lastiow = 1;
5852471Swnj 	state = um->um_tab.b_active;
5862471Swnj 	um->um_tab.b_active = 0;
5872608Swnj 	/*
5882608Swnj 	 * Check for errors.
5892608Swnj 	 */
5902608Swnj 	if (addr->tmcs&TM_ERR) {
5913095Swnj 		while (addr->tmer & TMER_SDWN)
5921919Swnj 			;			/* await settle down */
5932608Swnj 		/*
5943095Swnj 		 * If we hit the end of the tape file, update our position.
5952608Swnj 		 */
5963095Swnj 		if (addr->tmer&TMER_EOF) {
5972608Swnj 			tmseteof(bp);		/* set blkno and nxrec */
5982608Swnj 			state = SCOM;		/* force completion */
5992608Swnj 			/*
6002608Swnj 			 * Stuff bc so it will be unstuffed correctly
6012608Swnj 			 * later to get resid.
6022608Swnj 			 */
6032396Swnj 			addr->tmbc = -bp->b_bcount;
6042608Swnj 			goto opdone;
6051919Swnj 		}
6062608Swnj 		/*
6073095Swnj 		 * If we were reading raw tape and the only error was that the
6083095Swnj 		 * record was too long, then we don't consider this an error.
6092608Swnj 		 */
6103095Swnj 		if (bp == &rtmbuf[TMUNIT(bp->b_dev)] && (bp->b_flags&B_READ) &&
6113095Swnj 		    (addr->tmer&(TMER_HARD|TMER_SOFT)) == TMER_RLE)
6122608Swnj 			goto ignoreerr;
6132608Swnj 		/*
6142608Swnj 		 * If error is not hard, and this was an i/o operation
6152608Swnj 		 * retry up to 8 times.
6162608Swnj 		 */
6173095Swnj 		if ((addr->tmer&TMER_HARD)==0 && state==SIO) {
6182471Swnj 			if (++um->um_tab.b_errcnt < 7) {
6192471Swnj 				sc->sc_blkno++;
6202617Swnj 				ubadone(um);
6212608Swnj 				goto opcont;
6221919Swnj 			}
6232608Swnj 		} else
6242608Swnj 			/*
6252608Swnj 			 * Hard or non-i/o errors on non-raw tape
6262608Swnj 			 * cause it to close.
6272608Swnj 			 */
6283095Swnj 			if (sc->sc_openf>0 && bp != &rtmbuf[TMUNIT(bp->b_dev)])
6292608Swnj 				sc->sc_openf = -1;
6302608Swnj 		/*
6312608Swnj 		 * Couldn't recover error
6322608Swnj 		 */
6332928Swnj 		printf("te%d: hard error bn%d er=%b\n", minor(bp->b_dev)&03,
6343095Swnj 		    bp->b_blkno, sc->sc_erreg, TMER_BITS);
6351919Swnj 		bp->b_flags |= B_ERROR;
6362608Swnj 		goto opdone;
6371919Swnj 	}
6382608Swnj 	/*
6392608Swnj 	 * Advance tape control FSM.
6402608Swnj 	 */
6412608Swnj ignoreerr:
6421919Swnj 	switch (state) {
6431919Swnj 
6441919Swnj 	case SIO:
6452608Swnj 		/*
6462608Swnj 		 * Read/write increments tape block number
6472608Swnj 		 */
6482471Swnj 		sc->sc_blkno++;
6492608Swnj 		goto opdone;
6501919Swnj 
6511919Swnj 	case SCOM:
6522608Swnj 		/*
6533095Swnj 		 * For forward/backward space record update current position.
6542608Swnj 		 */
6553095Swnj 		if (bp == &ctmbuf[TMUNIT(bp->b_dev)])
6562608Swnj 		switch (bp->b_command) {
6571919Swnj 
6582608Swnj 		case TM_SFORW:
6592608Swnj 			sc->sc_blkno -= bp->b_repcnt;
6603095Swnj 			break;
6611919Swnj 
6622608Swnj 		case TM_SREV:
6632608Swnj 			sc->sc_blkno += bp->b_repcnt;
6643095Swnj 			break;
6651919Swnj 		}
6663095Swnj 		goto opdone;
6671919Swnj 
6681919Swnj 	case SSEEK:
6697381Ssam 		sc->sc_blkno = bdbtofsb(bp->b_blkno);
6702608Swnj 		goto opcont;
6711919Swnj 
6721919Swnj 	default:
6732608Swnj 		panic("tmintr");
6742608Swnj 	}
6752608Swnj opdone:
6762608Swnj 	/*
6772608Swnj 	 * Reset error count and remove
6782608Swnj 	 * from device queue.
6792608Swnj 	 */
6802608Swnj 	um->um_tab.b_errcnt = 0;
6812608Swnj 	dp->b_actf = bp->av_forw;
6822608Swnj 	bp->b_resid = -addr->tmbc;
6832617Swnj 	ubadone(um);
6842608Swnj 	iodone(bp);
6852608Swnj 	/*
6862608Swnj 	 * Circulate slave to end of controller
6872608Swnj 	 * queue to give other slaves a chance.
6882608Swnj 	 */
6892608Swnj 	um->um_tab.b_actf = dp->b_forw;
6902608Swnj 	if (dp->b_actf) {
6912608Swnj 		dp->b_forw = NULL;
6922608Swnj 		if (um->um_tab.b_actf == NULL)
6932608Swnj 			um->um_tab.b_actf = dp;
6942608Swnj 		else
6952608Swnj 			um->um_tab.b_actl->b_forw = dp;
6962608Swnj 		um->um_tab.b_actl = dp;
6972608Swnj 	}
6982608Swnj 	if (um->um_tab.b_actf == 0)
6991919Swnj 		return;
7002608Swnj opcont:
7012608Swnj 	tmstart(um);
7021919Swnj }
7031919Swnj 
7043495Sroot tmtimer(dev)
7053495Sroot 	int dev;
7063495Sroot {
7073495Sroot 	register struct te_softc *sc = &te_softc[TEUNIT(dev)];
7084847Sroot 	register short x;
7093495Sroot 
7103495Sroot 	if (sc->sc_timo != INF && (sc->sc_timo -= 5) < 0) {
7114278Sroot 		printf("te%d: lost interrupt\n", TEUNIT(dev));
7123495Sroot 		sc->sc_timo = INF;
7134847Sroot 		x = spl5();
7143495Sroot 		tmintr(TMUNIT(dev));
7154847Sroot 		(void) splx(x);
7163495Sroot 	}
7173629Sroot 	timeout(tmtimer, (caddr_t)dev, 5*hz);
7183495Sroot }
7193495Sroot 
7201919Swnj tmseteof(bp)
7211919Swnj 	register struct buf *bp;
7221919Swnj {
7233095Swnj 	register int teunit = TEUNIT(bp->b_dev);
7245692Sroot 	register struct tmdevice *addr =
7255692Sroot 	    (struct tmdevice *)tedinfo[teunit]->ui_addr;
7263095Swnj 	register struct te_softc *sc = &te_softc[teunit];
7271919Swnj 
7283095Swnj 	if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) {
7297381Ssam 		if (sc->sc_blkno > bdbtofsb(bp->b_blkno)) {
7301919Swnj 			/* reversing */
7317381Ssam 			sc->sc_nxrec = bdbtofsb(bp->b_blkno) - addr->tmbc;
7322471Swnj 			sc->sc_blkno = sc->sc_nxrec;
7331919Swnj 		} else {
7341919Swnj 			/* spacing forward */
7357381Ssam 			sc->sc_blkno = bdbtofsb(bp->b_blkno) + addr->tmbc;
7362471Swnj 			sc->sc_nxrec = sc->sc_blkno - 1;
7371919Swnj 		}
7381919Swnj 		return;
7391919Swnj 	}
7401919Swnj 	/* eof on read */
7417381Ssam 	sc->sc_nxrec = bdbtofsb(bp->b_blkno);
7421919Swnj }
7431919Swnj 
7441919Swnj tmread(dev)
7452608Swnj 	dev_t dev;
7461919Swnj {
7471919Swnj 
7481919Swnj 	tmphys(dev);
7492982Swnj 	if (u.u_error)
7502982Swnj 		return;
7512608Swnj 	physio(tmstrategy, &rtmbuf[TMUNIT(dev)], dev, B_READ, minphys);
7521919Swnj }
7531919Swnj 
7541919Swnj tmwrite(dev)
7552608Swnj 	dev_t dev;
7561919Swnj {
7571919Swnj 
7581919Swnj 	tmphys(dev);
7592982Swnj 	if (u.u_error)
7602982Swnj 		return;
7612608Swnj 	physio(tmstrategy, &rtmbuf[TMUNIT(dev)], dev, B_WRITE, minphys);
7621919Swnj }
7631919Swnj 
7643095Swnj /*
7653095Swnj  * Check that a raw device exists.
7663095Swnj  * If it does, set up sc_blkno and sc_nxrec
7673095Swnj  * so that the tape will appear positioned correctly.
7683095Swnj  */
7691919Swnj tmphys(dev)
7702608Swnj 	dev_t dev;
7711919Swnj {
7723095Swnj 	register int teunit = TEUNIT(dev);
7731919Swnj 	register daddr_t a;
7743095Swnj 	register struct te_softc *sc;
7753095Swnj 	register struct uba_device *ui;
7761919Swnj 
7773095Swnj 	if (teunit >= NTE || (ui=tedinfo[teunit]) == 0 || ui->ui_alive == 0) {
7782982Swnj 		u.u_error = ENXIO;
7792982Swnj 		return;
7802982Swnj 	}
7813095Swnj 	sc = &te_softc[teunit];
7827381Ssam 	a = bdbtofsb(u.u_offset >> 9);
7832471Swnj 	sc->sc_blkno = a;
7842471Swnj 	sc->sc_nxrec = a + 1;
7851919Swnj }
7861919Swnj 
7872608Swnj tmreset(uban)
7882608Swnj 	int uban;
7892608Swnj {
7902982Swnj 	register struct uba_ctlr *um;
7913095Swnj 	register tm11, teunit;
7922982Swnj 	register struct uba_device *ui;
7932608Swnj 	register struct buf *dp;
7942608Swnj 
7952630Swnj 	for (tm11 = 0; tm11 < NTM; tm11++) {
7962630Swnj 		if ((um = tmminfo[tm11]) == 0 || um->um_alive == 0 ||
7972608Swnj 		   um->um_ubanum != uban)
7982608Swnj 			continue;
7992928Swnj 		printf(" tm%d", tm11);
8002608Swnj 		um->um_tab.b_active = 0;
8012608Swnj 		um->um_tab.b_actf = um->um_tab.b_actl = 0;
8022608Swnj 		if (um->um_ubinfo) {
8032608Swnj 			printf("<%d>", (um->um_ubinfo>>28)&0xf);
8042617Swnj 			ubadone(um);
8052608Swnj 		}
8065692Sroot 		((struct tmdevice *)(um->um_addr))->tmcs = TM_DCLR;
8073095Swnj 		for (teunit = 0; teunit < NTE; teunit++) {
8083095Swnj 			if ((ui = tedinfo[teunit]) == 0 || ui->ui_mi != um ||
8093095Swnj 			    ui->ui_alive == 0)
8102608Swnj 				continue;
8113095Swnj 			dp = &teutab[teunit];
8122608Swnj 			dp->b_active = 0;
8132608Swnj 			dp->b_forw = 0;
8142608Swnj 			if (um->um_tab.b_actf == NULL)
8152608Swnj 				um->um_tab.b_actf = dp;
8162608Swnj 			else
8172608Swnj 				um->um_tab.b_actl->b_forw = dp;
8182608Swnj 			um->um_tab.b_actl = dp;
8193495Sroot 			if (te_softc[teunit].sc_openf > 0)
8203495Sroot 				te_softc[teunit].sc_openf = -1;
8212608Swnj 		}
8222608Swnj 		tmstart(um);
8232608Swnj 	}
8242608Swnj }
8252608Swnj 
8261919Swnj /*ARGSUSED*/
827*7632Ssam tmioctl(dev, cmd, data, flag)
828*7632Ssam 	caddr_t data;
8291919Swnj 	dev_t dev;
8301919Swnj {
8313095Swnj 	int teunit = TEUNIT(dev);
8323095Swnj 	register struct te_softc *sc = &te_softc[teunit];
8333095Swnj 	register struct buf *bp = &ctmbuf[TMUNIT(dev)];
8341919Swnj 	register callcount;
8351919Swnj 	int fcount;
836*7632Ssam 	struct mtop *mtop;
837*7632Ssam 	struct mtget *mtget;
8381919Swnj 	/* we depend of the values and order of the MT codes here */
8392608Swnj 	static tmops[] =
8402608Swnj 	   {TM_WEOF,TM_SFORW,TM_SREV,TM_SFORW,TM_SREV,TM_REW,TM_OFFL,TM_SENSE};
8411919Swnj 
8422608Swnj 	switch (cmd) {
843*7632Ssam 
844*7632Ssam 	case MTIOCTOP:	/* tape operation */
845*7632Ssam 		mtop = (struct mtop *)data;
846*7632Ssam 		switch(mtop->mt_op) {
847*7632Ssam 
8482608Swnj 		case MTWEOF:
849*7632Ssam 			callcount = mtop->mt_count;
8502608Swnj 			fcount = 1;
8512608Swnj 			break;
852*7632Ssam 
8532608Swnj 		case MTFSF: case MTBSF:
854*7632Ssam 			callcount = mtop->mt_count;
8551919Swnj 			fcount = INF;
8561919Swnj 			break;
857*7632Ssam 
8581919Swnj 		case MTFSR: case MTBSR:
8591919Swnj 			callcount = 1;
860*7632Ssam 			fcount = mtop->mt_count;
8611919Swnj 			break;
862*7632Ssam 
8632324Skre 		case MTREW: case MTOFFL: case MTNOP:
8641919Swnj 			callcount = 1;
8651919Swnj 			fcount = 1;
8661919Swnj 			break;
867*7632Ssam 
8681919Swnj 		default:
8691919Swnj 			u.u_error = ENXIO;
8701919Swnj 			return;
8711919Swnj 		}
8722608Swnj 		if (callcount <= 0 || fcount <= 0) {
8731919Swnj 			u.u_error = ENXIO;
8742608Swnj 			return;
8752608Swnj 		}
8762608Swnj 		while (--callcount >= 0) {
877*7632Ssam 			tmcommand(dev, tmops[mtop->mt_op], fcount);
878*7632Ssam 			if ((mtop->mt_op == MTFSR || mtop->mt_op == MTBSR) &&
8792608Swnj 			    bp->b_resid) {
8801919Swnj 				u.u_error = EIO;
8811919Swnj 				break;
8821919Swnj 			}
8833095Swnj 			if ((bp->b_flags&B_ERROR) || sc->sc_erreg&TMER_BOT)
8841919Swnj 				break;
8851919Swnj 		}
8862608Swnj 		geterror(bp);
8871919Swnj 		return;
888*7632Ssam 
8891919Swnj 	case MTIOCGET:
890*7632Ssam 		mtget = (struct mtget *)data;
891*7632Ssam 		mtget->mt_dsreg = sc->sc_dsreg;
892*7632Ssam 		mtget->mt_erreg = sc->sc_erreg;
893*7632Ssam 		mtget->mt_resid = sc->sc_resid;
894*7632Ssam 		mtget->mt_type = MT_ISTM;
8951919Swnj 		return;
896*7632Ssam 
8971919Swnj 	default:
8981919Swnj 		u.u_error = ENXIO;
8991919Swnj 	}
9001919Swnj }
9011919Swnj 
9021919Swnj #define	DBSIZE	20
9031919Swnj 
9042363Swnj tmdump()
9052363Swnj {
9062982Swnj 	register struct uba_device *ui;
9072396Swnj 	register struct uba_regs *up;
9085692Sroot 	register struct tmdevice *addr;
9092426Skre 	int blk, num;
9102426Skre 	int start;
9111919Swnj 
9122426Skre 	start = 0;
9132426Skre 	num = maxfree;
9142426Skre #define	phys(a,b)	((b)((int)(a)&0x7fffffff))
9153095Swnj 	if (tedinfo[0] == 0)
9162887Swnj 		return (ENXIO);
9173095Swnj 	ui = phys(tedinfo[0], struct uba_device *);
9182396Swnj 	up = phys(ui->ui_hd, struct uba_hd *)->uh_physuba;
9193331Swnj 	ubainit(up);
9202324Skre 	DELAY(1000000);
9215692Sroot 	addr = (struct tmdevice *)ui->ui_physaddr;
9222396Swnj 	tmwait(addr);
9232608Swnj 	addr->tmcs = TM_DCLR | TM_GO;
9241919Swnj 	while (num > 0) {
9251919Swnj 		blk = num > DBSIZE ? DBSIZE : num;
9262396Swnj 		tmdwrite(start, blk, addr, up);
9271919Swnj 		start += blk;
9281919Swnj 		num -= blk;
9291919Swnj 	}
9302426Skre 	tmeof(addr);
9312426Skre 	tmeof(addr);
9322426Skre 	tmwait(addr);
9332887Swnj 	if (addr->tmcs&TM_ERR)
9342887Swnj 		return (EIO);
9352608Swnj 	addr->tmcs = TM_REW | TM_GO;
9362471Swnj 	tmwait(addr);
9372363Swnj 	return (0);
9381919Swnj }
9391919Swnj 
9402608Swnj tmdwrite(dbuf, num, addr, up)
9412608Swnj 	register dbuf, num;
9425692Sroot 	register struct tmdevice *addr;
9432396Swnj 	struct uba_regs *up;
9441919Swnj {
9452396Swnj 	register struct pte *io;
9462396Swnj 	register int npf;
9471928Swnj 
9482396Swnj 	tmwait(addr);
9492396Swnj 	io = up->uba_map;
9501919Swnj 	npf = num+1;
9511928Swnj 	while (--npf != 0)
9522982Swnj 		 *(int *)io++ = (dbuf++ | (1<<UBAMR_DPSHIFT) | UBAMR_MRV);
9532396Swnj 	*(int *)io = 0;
9542396Swnj 	addr->tmbc = -(num*NBPG);
9552396Swnj 	addr->tmba = 0;
9562608Swnj 	addr->tmcs = TM_WCOM | TM_GO;
9571919Swnj }
9581919Swnj 
9592396Swnj tmwait(addr)
9605692Sroot 	register struct tmdevice *addr;
9611919Swnj {
9621928Swnj 	register s;
9631919Swnj 
9641919Swnj 	do
9652396Swnj 		s = addr->tmcs;
9662608Swnj 	while ((s & TM_CUR) == 0);
9671919Swnj }
9681919Swnj 
9692396Swnj tmeof(addr)
9705692Sroot 	struct tmdevice *addr;
9711919Swnj {
9721919Swnj 
9732396Swnj 	tmwait(addr);
9742608Swnj 	addr->tmcs = TM_WEOF | TM_GO;
9751919Swnj }
9761919Swnj #endif
977