xref: /csrg-svn/sys/vax/uba/tm.c (revision 8648)
1*8648Sroot /*	tm.c	4.58	82/10/17	*/
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"
267632Ssam #include "../h/ioctl.h"
271919Swnj #include "../h/mtio.h"
282363Swnj #include "../h/cmap.h"
297732Sroot #include "../h/uio.h"
308610Sroot #include "../h/kernel.h"
311919Swnj 
328479Sroot #include "../vax/cpu.h"
338479Sroot #include "../vaxuba/ubareg.h"
348479Sroot #include "../vaxuba/ubavar.h"
358479Sroot #include "../vaxuba/tmreg.h"
361919Swnj 
373095Swnj /*
383095Swnj  * There is a ctmbuf per tape controller.
393095Swnj  * It is used as the token to pass to the internal routines
403095Swnj  * to execute tape ioctls, and also acts as a lock on the slaves
413095Swnj  * on the controller, since there is only one per controller.
423095Swnj  * In particular, when the tape is rewinding on close we release
433095Swnj  * the user process but any further attempts to use the tape drive
443095Swnj  * before the rewind completes will hang waiting for ctmbuf.
453095Swnj  */
463095Swnj struct	buf	ctmbuf[NTM];
471919Swnj 
483095Swnj /*
493095Swnj  * Raw tape operations use rtmbuf.  The driver
503095Swnj  * notices when rtmbuf is being used and allows the user
513095Swnj  * program to continue after errors and read records
523095Swnj  * not of the standard length (BSIZE).
533095Swnj  */
543095Swnj struct	buf	rtmbuf[NTM];
553095Swnj 
563095Swnj /*
573095Swnj  * Driver unibus interface routines and variables.
583095Swnj  */
592608Swnj int	tmprobe(), tmslave(), tmattach(), tmdgo(), tmintr();
602982Swnj struct	uba_ctlr *tmminfo[NTM];
613095Swnj struct	uba_device *tedinfo[NTE];
623095Swnj struct	buf teutab[NTE];
633095Swnj short	tetotm[NTE];
642458Swnj u_short	tmstd[] = { 0772520, 0 };
652396Swnj struct	uba_driver tmdriver =
663095Swnj  { tmprobe, tmslave, tmattach, tmdgo, tmstd, "te", tedinfo, "tm", tmminfo, 0 };
671919Swnj 
681919Swnj /* bits in minor device */
693095Swnj #define	TEUNIT(dev)	(minor(dev)&03)
703095Swnj #define	TMUNIT(dev)	(tetotm[TEUNIT(dev)])
711919Swnj #define	T_NOREWIND	04
721919Swnj #define	T_1600BPI	08
731919Swnj 
741919Swnj #define	INF	(daddr_t)1000000L
751919Swnj 
762608Swnj /*
772608Swnj  * Software state per tape transport.
783095Swnj  *
793095Swnj  * 1. A tape drive is a unique-open device; we refuse opens when it is already.
803095Swnj  * 2. We keep track of the current position on a block tape and seek
813095Swnj  *    before operations by forward/back spacing if necessary.
823095Swnj  * 3. We remember if the last operation was a write on a tape, so if a tape
833095Swnj  *    is open read write and the last thing done is a write we can
843095Swnj  *    write a standard end of tape mark (two eofs).
853095Swnj  * 4. We remember the status registers after the last command, using
863095Swnj  *    then internally and returning them to the SENSE ioctl.
873095Swnj  * 5. We remember the last density the tape was used at.  If it is
883095Swnj  *    not a BOT when we start using it and we are writing, we don't
893095Swnj  *    let the density be changed.
902608Swnj  */
913095Swnj struct	te_softc {
922608Swnj 	char	sc_openf;	/* lock against multiple opens */
932608Swnj 	char	sc_lastiow;	/* last op was a write */
942608Swnj 	daddr_t	sc_blkno;	/* block number, for block device tape */
953095Swnj 	daddr_t	sc_nxrec;	/* position of end of tape, if known */
962608Swnj 	u_short	sc_erreg;	/* copy of last erreg */
972608Swnj 	u_short	sc_dsreg;	/* copy of last dsreg */
982608Swnj 	short	sc_resid;	/* copy of last bc */
993105Swnj #ifdef unneeded
1002670Swnj 	short	sc_lastcmd;	/* last command to handle direction changes */
1012928Swnj #endif
1023095Swnj 	u_short	sc_dens;	/* prototype command with density info */
1033495Sroot 	daddr_t	sc_timo;	/* time until timeout expires */
1043495Sroot 	short	sc_tact;	/* timeout is active */
1056952Swnj } te_softc[NTE];
1063105Swnj #ifdef unneeded
1073105Swnj int	tmgapsdcnt;		/* DEBUG */
1083105Swnj #endif
1091919Swnj 
1102608Swnj /*
1113095Swnj  * States for um->um_tab.b_active, the per controller state flag.
1123095Swnj  * This is used to sequence control in the driver.
1132608Swnj  */
1141919Swnj #define	SSEEK	1		/* seeking */
1151919Swnj #define	SIO	2		/* doing seq i/o */
1161919Swnj #define	SCOM	3		/* sending control command */
1172608Swnj #define	SREW	4		/* sending a drive rewind */
1181919Swnj 
1192426Skre /*
1202426Skre  * Determine if there is a controller for
1212426Skre  * a tm at address reg.  Our goal is to make the
1222426Skre  * device interrupt.
1232426Skre  */
1242608Swnj tmprobe(reg)
1252396Swnj 	caddr_t reg;
1262396Swnj {
1273095Swnj 	register int br, cvec;		/* must be r11,r10; value-result */
1282426Skre 
1292608Swnj #ifdef lint
1303105Swnj 	br = 0; cvec = br; br = cvec;
1314936Swnj 	tmintr(0);
1322608Swnj #endif
1335692Sroot 	((struct tmdevice *)reg)->tmcs = TM_IE;
1342396Swnj 	/*
1352630Swnj 	 * If this is a tm11, it ought to have interrupted
1362396Swnj 	 * by now, if it isn't (ie: it is a ts04) then we just
1372458Swnj 	 * hope that it didn't interrupt, so autoconf will ignore it.
1382458Swnj 	 * Just in case, we will reference one
1392396Swnj 	 * of the more distant registers, and hope for a machine
1402458Swnj 	 * check, or similar disaster if this is a ts.
1412471Swnj 	 *
1422471Swnj 	 * Note: on an 11/780, badaddr will just generate
1432471Swnj 	 * a uba error for a ts; but our caller will notice that
1442471Swnj 	 * so we won't check for it.
1452396Swnj 	 */
1465692Sroot 	if (badaddr((caddr_t)&((struct tmdevice *)reg)->tmrd, 2))
1472458Swnj 		return (0);
1487404Skre 	return (sizeof (struct tmdevice));
1492396Swnj }
1502396Swnj 
1512608Swnj /*
1522608Swnj  * Due to a design flaw, we cannot ascertain if the tape
1532608Swnj  * exists or not unless it is on line - ie: unless a tape is
1542608Swnj  * mounted. This is too servere a restriction to bear,
1552608Swnj  * so all units are assumed to exist.
1562608Swnj  */
1572608Swnj /*ARGSUSED*/
1582574Swnj tmslave(ui, reg)
1592982Swnj 	struct uba_device *ui;
1602396Swnj 	caddr_t reg;
1612396Swnj {
1622458Swnj 
1632458Swnj 	return (1);
1642396Swnj }
1652396Swnj 
1662608Swnj /*
1673095Swnj  * Record attachment of the unit to the controller.
1682608Swnj  */
1692608Swnj /*ARGSUSED*/
1702608Swnj tmattach(ui)
1712982Swnj 	struct uba_device *ui;
1722608Swnj {
1733095Swnj 	/*
1743095Swnj 	 * Tetotm is used in TMUNIT to index the ctmbuf and rtmbuf
1753095Swnj 	 * arrays given a te unit number.
1763095Swnj 	 */
1773095Swnj 	tetotm[ui->ui_unit] = ui->ui_mi->um_ctlr;
1782608Swnj }
1792608Swnj 
1803495Sroot int	tmtimer();
1812608Swnj /*
1822608Swnj  * Open the device.  Tapes are unique open
1832608Swnj  * devices, so we refuse if it is already open.
1842608Swnj  * We also check that a tape is available, and
1853095Swnj  * don't block waiting here; if you want to wait
1863095Swnj  * for a tape you should timeout in user code.
1872608Swnj  */
1881919Swnj tmopen(dev, flag)
1891919Swnj 	dev_t dev;
1901919Swnj 	int flag;
1911919Swnj {
1923095Swnj 	register int teunit;
1932982Swnj 	register struct uba_device *ui;
1943095Swnj 	register struct te_softc *sc;
1953209Swnj 	int olddens, dens;
1965437Sroot 	int s;
1971919Swnj 
1983095Swnj 	teunit = TEUNIT(dev);
1993095Swnj 	if (teunit>=NTE || (sc = &te_softc[teunit])->sc_openf ||
2008574Sroot 	    (ui = tedinfo[teunit]) == 0 || ui->ui_alive == 0)
2018574Sroot 		return (ENXIO);
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);
2168574Sroot 		return (EIO);
2171919Swnj 	}
2183710Sroot 	if ((flag&FWRITE) && (sc->sc_erreg&TMER_WRL)) {
2193710Sroot 		uprintf("te%d: no write ring\n", teunit);
2208574Sroot 		return (EIO);
2213710Sroot 	}
2223710Sroot 	if ((sc->sc_erreg&TMER_BOT) == 0 && (flag&FWRITE) &&
2233710Sroot 	    dens != sc->sc_dens) {
2243710Sroot 		uprintf("te%d: can't change density in mid-tape\n", teunit);
2258574Sroot 		return (EIO);
2263710Sroot 	}
2272608Swnj 	sc->sc_openf = 1;
2282471Swnj 	sc->sc_blkno = (daddr_t)0;
2292471Swnj 	sc->sc_nxrec = INF;
2302608Swnj 	sc->sc_lastiow = 0;
2313095Swnj 	sc->sc_dens = dens;
2325437Sroot 	s = spl6();
2333495Sroot 	if (sc->sc_tact == 0) {
2343495Sroot 		sc->sc_timo = INF;
2353495Sroot 		sc->sc_tact = 1;
2363629Sroot 		timeout(tmtimer, (caddr_t)dev, 5*hz);
2373495Sroot 	}
2385437Sroot 	splx(s);
2398574Sroot 	return (0);
2401919Swnj }
2411919Swnj 
2422608Swnj /*
2432608Swnj  * Close tape device.
2442608Swnj  *
2452608Swnj  * If tape was open for writing or last operation was
2462608Swnj  * a write, then write two EOF's and backspace over the last one.
2472608Swnj  * Unless this is a non-rewinding special file, rewind the tape.
2482608Swnj  * Make the tape available to others.
2492608Swnj  */
2501919Swnj tmclose(dev, flag)
2511919Swnj 	register dev_t dev;
2521919Swnj 	register flag;
2531919Swnj {
2543095Swnj 	register struct te_softc *sc = &te_softc[TEUNIT(dev)];
2551919Swnj 
2562608Swnj 	if (flag == FWRITE || (flag&FWRITE) && sc->sc_lastiow) {
2572608Swnj 		tmcommand(dev, TM_WEOF, 1);
2582608Swnj 		tmcommand(dev, TM_WEOF, 1);
2592608Swnj 		tmcommand(dev, TM_SREV, 1);
2601919Swnj 	}
2611919Swnj 	if ((minor(dev)&T_NOREWIND) == 0)
2623095Swnj 		/*
2633095Swnj 		 * 0 count means don't hang waiting for rewind complete
2643095Swnj 		 * rather ctmbuf stays busy until the operation completes
2653095Swnj 		 * preventing further opens from completing by
2663095Swnj 		 * preventing a TM_SENSE from completing.
2673095Swnj 		 */
2683095Swnj 		tmcommand(dev, TM_REW, 0);
2692471Swnj 	sc->sc_openf = 0;
2701919Swnj }
2711919Swnj 
2722608Swnj /*
2732608Swnj  * Execute a command on the tape drive
2742608Swnj  * a specified number of times.
2752608Swnj  */
2762574Swnj tmcommand(dev, com, count)
2771919Swnj 	dev_t dev;
2781919Swnj 	int com, count;
2791919Swnj {
2801919Swnj 	register struct buf *bp;
2815437Sroot 	register int s;
2821919Swnj 
2832608Swnj 	bp = &ctmbuf[TMUNIT(dev)];
2845437Sroot 	s = spl5();
2851919Swnj 	while (bp->b_flags&B_BUSY) {
2863095Swnj 		/*
2873095Swnj 		 * This special check is because B_BUSY never
2883095Swnj 		 * gets cleared in the non-waiting rewind case.
2893095Swnj 		 */
2903141Swnj 		if (bp->b_repcnt == 0 && (bp->b_flags&B_DONE))
2913095Swnj 			break;
2921919Swnj 		bp->b_flags |= B_WANTED;
2931919Swnj 		sleep((caddr_t)bp, PRIBIO);
2941919Swnj 	}
2951919Swnj 	bp->b_flags = B_BUSY|B_READ;
2965437Sroot 	splx(s);
2971919Swnj 	bp->b_dev = dev;
2981919Swnj 	bp->b_repcnt = -count;
2991919Swnj 	bp->b_command = com;
3001919Swnj 	bp->b_blkno = 0;
3011919Swnj 	tmstrategy(bp);
3023095Swnj 	/*
3033095Swnj 	 * In case of rewind from close, don't wait.
3043095Swnj 	 * This is the only case where count can be 0.
3053095Swnj 	 */
3063095Swnj 	if (count == 0)
3073095Swnj 		return;
3081919Swnj 	iowait(bp);
3091919Swnj 	if (bp->b_flags&B_WANTED)
3101919Swnj 		wakeup((caddr_t)bp);
3111919Swnj 	bp->b_flags &= B_ERROR;
3121919Swnj }
3131919Swnj 
3142608Swnj /*
3153095Swnj  * Queue a tape operation.
3162608Swnj  */
3171919Swnj tmstrategy(bp)
3181919Swnj 	register struct buf *bp;
3191919Swnj {
3203095Swnj 	int teunit = TEUNIT(bp->b_dev);
3212982Swnj 	register struct uba_ctlr *um;
3222608Swnj 	register struct buf *dp;
3235437Sroot 	int s;
3241919Swnj 
3252608Swnj 	/*
3262608Swnj 	 * Put transfer at end of unit queue
3272608Swnj 	 */
3283095Swnj 	dp = &teutab[teunit];
3291919Swnj 	bp->av_forw = NULL;
3305437Sroot 	s = spl5();
3313939Sbugs 	um = tedinfo[teunit]->ui_mi;
3322608Swnj 	if (dp->b_actf == NULL) {
3332608Swnj 		dp->b_actf = bp;
3342608Swnj 		/*
3352608Swnj 		 * Transport not already active...
3362608Swnj 		 * put at end of controller queue.
3372608Swnj 		 */
3382608Swnj 		dp->b_forw = NULL;
3392608Swnj 		if (um->um_tab.b_actf == NULL)
3402608Swnj 			um->um_tab.b_actf = dp;
3412608Swnj 		else
3422608Swnj 			um->um_tab.b_actl->b_forw = dp;
3432608Swnj 		um->um_tab.b_actl = dp;
3442608Swnj 	} else
3452608Swnj 		dp->b_actl->av_forw = bp;
3462608Swnj 	dp->b_actl = bp;
3472608Swnj 	/*
3482608Swnj 	 * If the controller is not busy, get
3492608Swnj 	 * it going.
3502608Swnj 	 */
3512608Swnj 	if (um->um_tab.b_active == 0)
3522608Swnj 		tmstart(um);
3535437Sroot 	splx(s);
3541919Swnj }
3551919Swnj 
3562608Swnj /*
3572608Swnj  * Start activity on a tm controller.
3582608Swnj  */
3592608Swnj tmstart(um)
3602982Swnj 	register struct uba_ctlr *um;
3611919Swnj {
3622608Swnj 	register struct buf *bp, *dp;
3635692Sroot 	register struct tmdevice *addr = (struct tmdevice *)um->um_addr;
3643095Swnj 	register struct te_softc *sc;
3652982Swnj 	register struct uba_device *ui;
3663095Swnj 	int teunit, cmd;
3672471Swnj 	daddr_t blkno;
3681919Swnj 
3692608Swnj 	/*
3702608Swnj 	 * Look for an idle transport on the controller.
3712608Swnj 	 */
3721919Swnj loop:
3732608Swnj 	if ((dp = um->um_tab.b_actf) == NULL)
3741919Swnj 		return;
3752608Swnj 	if ((bp = dp->b_actf) == NULL) {
3762608Swnj 		um->um_tab.b_actf = dp->b_forw;
3772608Swnj 		goto loop;
3782608Swnj 	}
3793095Swnj 	teunit = TEUNIT(bp->b_dev);
3803095Swnj 	ui = tedinfo[teunit];
3812608Swnj 	/*
3822608Swnj 	 * Record pre-transfer status (e.g. for TM_SENSE)
3832608Swnj 	 */
3843095Swnj 	sc = &te_softc[teunit];
3855692Sroot 	addr = (struct tmdevice *)um->um_addr;
3862608Swnj 	addr->tmcs = (ui->ui_slave << 8);
3872471Swnj 	sc->sc_dsreg = addr->tmcs;
3882471Swnj 	sc->sc_erreg = addr->tmer;
3892471Swnj 	sc->sc_resid = addr->tmbc;
3902608Swnj 	/*
3912608Swnj 	 * Default is that last command was NOT a write command;
3922608Swnj 	 * if we do a write command we will notice this in tmintr().
3932608Swnj 	 */
3943493Sroot 	sc->sc_lastiow = 0;
3952608Swnj 	if (sc->sc_openf < 0 || (addr->tmcs&TM_CUR) == 0) {
3962608Swnj 		/*
3973095Swnj 		 * Have had a hard error on a non-raw tape
3983095Swnj 		 * or the tape unit is now unavailable
3993095Swnj 		 * (e.g. taken off line).
4002608Swnj 		 */
4012608Swnj 		bp->b_flags |= B_ERROR;
4021919Swnj 		goto next;
4031919Swnj 	}
4043095Swnj 	if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) {
4053095Swnj 		/*
4063095Swnj 		 * Execute control operation with the specified count.
4073095Swnj 		 */
4082608Swnj 		if (bp->b_command == TM_SENSE)
4092608Swnj 			goto next;
4103495Sroot 		/*
4113495Sroot 		 * Set next state; give 5 minutes to complete
4123495Sroot 		 * rewind, or 10 seconds per iteration (minimum 60
4133629Sroot 		 * seconds and max 5 minutes) to complete other ops.
4143495Sroot 		 */
4153495Sroot 		if (bp->b_command == TM_REW) {
4163495Sroot 			um->um_tab.b_active = SREW;
4173495Sroot 			sc->sc_timo = 5 * 60;
4183495Sroot 		} else {
4193495Sroot 			um->um_tab.b_active = SCOM;
4204266Swnj 			sc->sc_timo =
4214266Swnj 			    imin(imax(10*(int)-bp->b_repcnt,60),5*60);
4223495Sroot 		}
4232608Swnj 		if (bp->b_command == TM_SFORW || bp->b_command == TM_SREV)
4242608Swnj 			addr->tmbc = bp->b_repcnt;
4252670Swnj 		goto dobpcmd;
4262608Swnj 	}
4272608Swnj 	/*
4283095Swnj 	 * The following checks handle boundary cases for operation
4293095Swnj 	 * on non-raw tapes.  On raw tapes the initialization of
4303095Swnj 	 * sc->sc_nxrec by tmphys causes them to be skipped normally
4313095Swnj 	 * (except in the case of retries).
4323095Swnj 	 */
4337381Ssam 	if (bdbtofsb(bp->b_blkno) > sc->sc_nxrec) {
4343095Swnj 		/*
4353095Swnj 		 * Can't read past known end-of-file.
4363095Swnj 		 */
4373095Swnj 		bp->b_flags |= B_ERROR;
4383095Swnj 		bp->b_error = ENXIO;
4393095Swnj 		goto next;
4403095Swnj 	}
4417381Ssam 	if (bdbtofsb(bp->b_blkno) == sc->sc_nxrec &&
4423095Swnj 	    bp->b_flags&B_READ) {
4433095Swnj 		/*
4443095Swnj 		 * Reading at end of file returns 0 bytes.
4453095Swnj 		 */
4463095Swnj 		bp->b_resid = bp->b_bcount;
4473095Swnj 		clrbuf(bp);
4483095Swnj 		goto next;
4493095Swnj 	}
4503095Swnj 	if ((bp->b_flags&B_READ) == 0)
4513095Swnj 		/*
4523095Swnj 		 * Writing sets EOF
4533095Swnj 		 */
4547381Ssam 		sc->sc_nxrec = bdbtofsb(bp->b_blkno) + 1;
4553095Swnj 	/*
4562608Swnj 	 * If the data transfer command is in the correct place,
4572608Swnj 	 * set up all the registers except the csr, and give
4582608Swnj 	 * control over to the UNIBUS adapter routines, to
4592608Swnj 	 * wait for resources to start the i/o.
4602608Swnj 	 */
4617381Ssam 	if ((blkno = sc->sc_blkno) == bdbtofsb(bp->b_blkno)) {
4622396Swnj 		addr->tmbc = -bp->b_bcount;
4631919Swnj 		if ((bp->b_flags&B_READ) == 0) {
4642471Swnj 			if (um->um_tab.b_errcnt)
4653095Swnj 				cmd = TM_WIRG;
4661919Swnj 			else
4673095Swnj 				cmd = TM_WCOM;
4681919Swnj 		} else
4693095Swnj 			cmd = TM_RCOM;
4702471Swnj 		um->um_tab.b_active = SIO;
4713095Swnj 		um->um_cmd = sc->sc_dens|cmd;
4722928Swnj #ifdef notdef
4732670Swnj 		if (tmreverseop(sc->sc_lastcmd))
4743095Swnj 			while (addr->tmer & TMER_SDWN)
4752670Swnj 				tmgapsdcnt++;
4762670Swnj 		sc->sc_lastcmd = TM_RCOM;		/* will serve */
4772928Swnj #endif
4783495Sroot 		sc->sc_timo = 60;	/* premature, but should serve */
4793105Swnj 		(void) ubago(ui);
4801919Swnj 		return;
4811919Swnj 	}
4822608Swnj 	/*
4833095Swnj 	 * Tape positioned incorrectly;
4843095Swnj 	 * set to seek forwards or backwards to the correct spot.
4853095Swnj 	 * This happens for raw tapes only on error retries.
4862608Swnj 	 */
4872471Swnj 	um->um_tab.b_active = SSEEK;
4887381Ssam 	if (blkno < bdbtofsb(bp->b_blkno)) {
4892670Swnj 		bp->b_command = TM_SFORW;
4907381Ssam 		addr->tmbc = blkno - bdbtofsb(bp->b_blkno);
4911919Swnj 	} else {
4922670Swnj 		bp->b_command = TM_SREV;
4937381Ssam 		addr->tmbc = bdbtofsb(bp->b_blkno) - blkno;
4941919Swnj 	}
4953629Sroot 	sc->sc_timo = imin(imax(10 * -addr->tmbc, 60), 5 * 60);
4962670Swnj dobpcmd:
4972928Swnj #ifdef notdef
4983095Swnj 	/*
4993095Swnj 	 * It is strictly necessary to wait for the tape
5003095Swnj 	 * to stop before changing directions, but the TC11
5013095Swnj 	 * handles this for us.
5023095Swnj 	 */
5032670Swnj 	if (tmreverseop(sc->sc_lastcmd) != tmreverseop(bp->b_command))
5042670Swnj 		while (addr->tmer & TM_SDWN)
5052670Swnj 			tmgapsdcnt++;
5062670Swnj 	sc->sc_lastcmd = bp->b_command;
5072928Swnj #endif
5083095Swnj 	/*
5093095Swnj 	 * Do the command in bp.
5103095Swnj 	 */
5113095Swnj 	addr->tmcs = (sc->sc_dens | bp->b_command);
5121919Swnj 	return;
5131919Swnj 
5141919Swnj next:
5152608Swnj 	/*
5162608Swnj 	 * Done with this operation due to error or
5172608Swnj 	 * the fact that it doesn't do anything.
5182608Swnj 	 * Release UBA resources (if any), dequeue
5192608Swnj 	 * the transfer and continue processing this slave.
5202608Swnj 	 */
5212608Swnj 	if (um->um_ubinfo)
5222617Swnj 		ubadone(um);
5232608Swnj 	um->um_tab.b_errcnt = 0;
5242608Swnj 	dp->b_actf = bp->av_forw;
5251919Swnj 	iodone(bp);
5261919Swnj 	goto loop;
5271919Swnj }
5281919Swnj 
5292608Swnj /*
5302608Swnj  * The UNIBUS resources we needed have been
5312608Swnj  * allocated to us; start the device.
5322608Swnj  */
5332574Swnj tmdgo(um)
5342982Swnj 	register struct uba_ctlr *um;
5351919Swnj {
5365692Sroot 	register struct tmdevice *addr = (struct tmdevice *)um->um_addr;
5372471Swnj 
5382574Swnj 	addr->tmba = um->um_ubinfo;
5392574Swnj 	addr->tmcs = um->um_cmd | ((um->um_ubinfo >> 12) & 0x30);
5402396Swnj }
5412396Swnj 
5422608Swnj /*
5432608Swnj  * Tm interrupt routine.
5442608Swnj  */
5452471Swnj /*ARGSUSED*/
5462630Swnj tmintr(tm11)
5472630Swnj 	int tm11;
5482396Swnj {
5492608Swnj 	struct buf *dp;
5501919Swnj 	register struct buf *bp;
5512982Swnj 	register struct uba_ctlr *um = tmminfo[tm11];
5525692Sroot 	register struct tmdevice *addr;
5533095Swnj 	register struct te_softc *sc;
5543095Swnj 	int teunit;
5551919Swnj 	register state;
5561919Swnj 
5573095Swnj 	if ((dp = um->um_tab.b_actf) == NULL)
5583095Swnj 		return;
5593095Swnj 	bp = dp->b_actf;
5603095Swnj 	teunit = TEUNIT(bp->b_dev);
5615692Sroot 	addr = (struct tmdevice *)tedinfo[teunit]->ui_addr;
5623524Swnj 	sc = &te_softc[teunit];
5632608Swnj 	/*
5642608Swnj 	 * If last command was a rewind, and tape is still
5652608Swnj 	 * rewinding, wait for the rewind complete interrupt.
5662608Swnj 	 */
5672608Swnj 	if (um->um_tab.b_active == SREW) {
5682608Swnj 		um->um_tab.b_active = SCOM;
5693524Swnj 		if (addr->tmer&TMER_RWS) {
5703524Swnj 			sc->sc_timo = 5*60;		/* 5 minutes */
5712608Swnj 			return;
5723524Swnj 		}
5731919Swnj 	}
5742608Swnj 	/*
5752608Swnj 	 * An operation completed... record status
5762608Swnj 	 */
5773495Sroot 	sc->sc_timo = INF;
5782471Swnj 	sc->sc_dsreg = addr->tmcs;
5792471Swnj 	sc->sc_erreg = addr->tmer;
5802471Swnj 	sc->sc_resid = addr->tmbc;
5811919Swnj 	if ((bp->b_flags & B_READ) == 0)
5822608Swnj 		sc->sc_lastiow = 1;
5832471Swnj 	state = um->um_tab.b_active;
5842471Swnj 	um->um_tab.b_active = 0;
5852608Swnj 	/*
5862608Swnj 	 * Check for errors.
5872608Swnj 	 */
5882608Swnj 	if (addr->tmcs&TM_ERR) {
5893095Swnj 		while (addr->tmer & TMER_SDWN)
5901919Swnj 			;			/* await settle down */
5912608Swnj 		/*
5923095Swnj 		 * If we hit the end of the tape file, update our position.
5932608Swnj 		 */
5943095Swnj 		if (addr->tmer&TMER_EOF) {
5952608Swnj 			tmseteof(bp);		/* set blkno and nxrec */
5962608Swnj 			state = SCOM;		/* force completion */
5972608Swnj 			/*
5982608Swnj 			 * Stuff bc so it will be unstuffed correctly
5992608Swnj 			 * later to get resid.
6002608Swnj 			 */
6012396Swnj 			addr->tmbc = -bp->b_bcount;
6022608Swnj 			goto opdone;
6031919Swnj 		}
6042608Swnj 		/*
6053095Swnj 		 * If we were reading raw tape and the only error was that the
6063095Swnj 		 * record was too long, then we don't consider this an error.
6072608Swnj 		 */
6083095Swnj 		if (bp == &rtmbuf[TMUNIT(bp->b_dev)] && (bp->b_flags&B_READ) &&
6093095Swnj 		    (addr->tmer&(TMER_HARD|TMER_SOFT)) == TMER_RLE)
6102608Swnj 			goto ignoreerr;
6112608Swnj 		/*
6122608Swnj 		 * If error is not hard, and this was an i/o operation
6132608Swnj 		 * retry up to 8 times.
6142608Swnj 		 */
6153095Swnj 		if ((addr->tmer&TMER_HARD)==0 && state==SIO) {
6162471Swnj 			if (++um->um_tab.b_errcnt < 7) {
6172471Swnj 				sc->sc_blkno++;
6182617Swnj 				ubadone(um);
6192608Swnj 				goto opcont;
6201919Swnj 			}
6212608Swnj 		} else
6222608Swnj 			/*
6232608Swnj 			 * Hard or non-i/o errors on non-raw tape
6242608Swnj 			 * cause it to close.
6252608Swnj 			 */
6263095Swnj 			if (sc->sc_openf>0 && bp != &rtmbuf[TMUNIT(bp->b_dev)])
6272608Swnj 				sc->sc_openf = -1;
6282608Swnj 		/*
6292608Swnj 		 * Couldn't recover error
6302608Swnj 		 */
6312928Swnj 		printf("te%d: hard error bn%d er=%b\n", minor(bp->b_dev)&03,
6323095Swnj 		    bp->b_blkno, sc->sc_erreg, TMER_BITS);
6331919Swnj 		bp->b_flags |= B_ERROR;
6342608Swnj 		goto opdone;
6351919Swnj 	}
6362608Swnj 	/*
6372608Swnj 	 * Advance tape control FSM.
6382608Swnj 	 */
6392608Swnj ignoreerr:
6401919Swnj 	switch (state) {
6411919Swnj 
6421919Swnj 	case SIO:
6432608Swnj 		/*
6442608Swnj 		 * Read/write increments tape block number
6452608Swnj 		 */
6462471Swnj 		sc->sc_blkno++;
6472608Swnj 		goto opdone;
6481919Swnj 
6491919Swnj 	case SCOM:
6502608Swnj 		/*
6513095Swnj 		 * For forward/backward space record update current position.
6522608Swnj 		 */
6533095Swnj 		if (bp == &ctmbuf[TMUNIT(bp->b_dev)])
6542608Swnj 		switch (bp->b_command) {
6551919Swnj 
6562608Swnj 		case TM_SFORW:
6572608Swnj 			sc->sc_blkno -= bp->b_repcnt;
6583095Swnj 			break;
6591919Swnj 
6602608Swnj 		case TM_SREV:
6612608Swnj 			sc->sc_blkno += bp->b_repcnt;
6623095Swnj 			break;
6631919Swnj 		}
6643095Swnj 		goto opdone;
6651919Swnj 
6661919Swnj 	case SSEEK:
6677381Ssam 		sc->sc_blkno = bdbtofsb(bp->b_blkno);
6682608Swnj 		goto opcont;
6691919Swnj 
6701919Swnj 	default:
6712608Swnj 		panic("tmintr");
6722608Swnj 	}
6732608Swnj opdone:
6742608Swnj 	/*
6752608Swnj 	 * Reset error count and remove
6762608Swnj 	 * from device queue.
6772608Swnj 	 */
6782608Swnj 	um->um_tab.b_errcnt = 0;
6792608Swnj 	dp->b_actf = bp->av_forw;
6802608Swnj 	bp->b_resid = -addr->tmbc;
6812617Swnj 	ubadone(um);
6822608Swnj 	iodone(bp);
6832608Swnj 	/*
6842608Swnj 	 * Circulate slave to end of controller
6852608Swnj 	 * queue to give other slaves a chance.
6862608Swnj 	 */
6872608Swnj 	um->um_tab.b_actf = dp->b_forw;
6882608Swnj 	if (dp->b_actf) {
6892608Swnj 		dp->b_forw = NULL;
6902608Swnj 		if (um->um_tab.b_actf == NULL)
6912608Swnj 			um->um_tab.b_actf = dp;
6922608Swnj 		else
6932608Swnj 			um->um_tab.b_actl->b_forw = dp;
6942608Swnj 		um->um_tab.b_actl = dp;
6952608Swnj 	}
6962608Swnj 	if (um->um_tab.b_actf == 0)
6971919Swnj 		return;
6982608Swnj opcont:
6992608Swnj 	tmstart(um);
7001919Swnj }
7011919Swnj 
7023495Sroot tmtimer(dev)
7033495Sroot 	int dev;
7043495Sroot {
7053495Sroot 	register struct te_softc *sc = &te_softc[TEUNIT(dev)];
7064847Sroot 	register short x;
7073495Sroot 
7083495Sroot 	if (sc->sc_timo != INF && (sc->sc_timo -= 5) < 0) {
7094278Sroot 		printf("te%d: lost interrupt\n", TEUNIT(dev));
7103495Sroot 		sc->sc_timo = INF;
7114847Sroot 		x = spl5();
7123495Sroot 		tmintr(TMUNIT(dev));
7134847Sroot 		(void) splx(x);
7143495Sroot 	}
7153629Sroot 	timeout(tmtimer, (caddr_t)dev, 5*hz);
7163495Sroot }
7173495Sroot 
7181919Swnj tmseteof(bp)
7191919Swnj 	register struct buf *bp;
7201919Swnj {
7213095Swnj 	register int teunit = TEUNIT(bp->b_dev);
7225692Sroot 	register struct tmdevice *addr =
7235692Sroot 	    (struct tmdevice *)tedinfo[teunit]->ui_addr;
7243095Swnj 	register struct te_softc *sc = &te_softc[teunit];
7251919Swnj 
7263095Swnj 	if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) {
7277381Ssam 		if (sc->sc_blkno > bdbtofsb(bp->b_blkno)) {
7281919Swnj 			/* reversing */
7297381Ssam 			sc->sc_nxrec = bdbtofsb(bp->b_blkno) - addr->tmbc;
7302471Swnj 			sc->sc_blkno = sc->sc_nxrec;
7311919Swnj 		} else {
7321919Swnj 			/* spacing forward */
7337381Ssam 			sc->sc_blkno = bdbtofsb(bp->b_blkno) + addr->tmbc;
7342471Swnj 			sc->sc_nxrec = sc->sc_blkno - 1;
7351919Swnj 		}
7361919Swnj 		return;
7371919Swnj 	}
7381919Swnj 	/* eof on read */
7397381Ssam 	sc->sc_nxrec = bdbtofsb(bp->b_blkno);
7401919Swnj }
7411919Swnj 
7427732Sroot tmread(dev, uio)
7432608Swnj 	dev_t dev;
7447732Sroot 	struct uio *uio;
7451919Swnj {
7468163Sroot 	int errno;
7471919Swnj 
7488163Sroot 	errno = tmphys(dev, uio);
7498163Sroot 	if (errno)
7508163Sroot 		return (errno);
7518163Sroot 	return (physio(tmstrategy, &rtmbuf[TMUNIT(dev)], dev, B_READ, minphys, uio));
7521919Swnj }
7531919Swnj 
7547838Sroot tmwrite(dev, uio)
7552608Swnj 	dev_t dev;
7567838Sroot 	struct uio *uio;
7571919Swnj {
7588163Sroot 	int errno;
7591919Swnj 
7608163Sroot 	errno = tmphys(dev, uio);
7618163Sroot 	if (errno)
7628163Sroot 		return (errno);
7638163Sroot 	return (physio(tmstrategy, &rtmbuf[TMUNIT(dev)], dev, B_WRITE, minphys, uio));
7641919Swnj }
7651919Swnj 
7663095Swnj /*
7673095Swnj  * Check that a raw device exists.
7683095Swnj  * If it does, set up sc_blkno and sc_nxrec
7693095Swnj  * so that the tape will appear positioned correctly.
7703095Swnj  */
7717732Sroot tmphys(dev, uio)
7722608Swnj 	dev_t dev;
7737732Sroot 	struct uio *uio;
7741919Swnj {
7753095Swnj 	register int teunit = TEUNIT(dev);
7761919Swnj 	register daddr_t a;
7773095Swnj 	register struct te_softc *sc;
7783095Swnj 	register struct uba_device *ui;
7791919Swnj 
7807838Sroot 	if (teunit >= NTE || (ui=tedinfo[teunit]) == 0 || ui->ui_alive == 0)
7817732Sroot 		return (ENXIO);
7823095Swnj 	sc = &te_softc[teunit];
7837838Sroot 	a = bdbtofsb(uio->uio_offset >> 9);
7842471Swnj 	sc->sc_blkno = a;
7852471Swnj 	sc->sc_nxrec = a + 1;
7867732Sroot 	return (0);
7871919Swnj }
7881919Swnj 
7892608Swnj tmreset(uban)
7902608Swnj 	int uban;
7912608Swnj {
7922982Swnj 	register struct uba_ctlr *um;
7933095Swnj 	register tm11, teunit;
7942982Swnj 	register struct uba_device *ui;
7952608Swnj 	register struct buf *dp;
7962608Swnj 
7972630Swnj 	for (tm11 = 0; tm11 < NTM; tm11++) {
7982630Swnj 		if ((um = tmminfo[tm11]) == 0 || um->um_alive == 0 ||
7992608Swnj 		   um->um_ubanum != uban)
8002608Swnj 			continue;
8012928Swnj 		printf(" tm%d", tm11);
8022608Swnj 		um->um_tab.b_active = 0;
8032608Swnj 		um->um_tab.b_actf = um->um_tab.b_actl = 0;
8042608Swnj 		if (um->um_ubinfo) {
8052608Swnj 			printf("<%d>", (um->um_ubinfo>>28)&0xf);
8062617Swnj 			ubadone(um);
8072608Swnj 		}
8085692Sroot 		((struct tmdevice *)(um->um_addr))->tmcs = TM_DCLR;
8093095Swnj 		for (teunit = 0; teunit < NTE; teunit++) {
8103095Swnj 			if ((ui = tedinfo[teunit]) == 0 || ui->ui_mi != um ||
8113095Swnj 			    ui->ui_alive == 0)
8122608Swnj 				continue;
8133095Swnj 			dp = &teutab[teunit];
8142608Swnj 			dp->b_active = 0;
8152608Swnj 			dp->b_forw = 0;
8162608Swnj 			if (um->um_tab.b_actf == NULL)
8172608Swnj 				um->um_tab.b_actf = dp;
8182608Swnj 			else
8192608Swnj 				um->um_tab.b_actl->b_forw = dp;
8202608Swnj 			um->um_tab.b_actl = dp;
8213495Sroot 			if (te_softc[teunit].sc_openf > 0)
8223495Sroot 				te_softc[teunit].sc_openf = -1;
8232608Swnj 		}
8242608Swnj 		tmstart(um);
8252608Swnj 	}
8262608Swnj }
8272608Swnj 
8281919Swnj /*ARGSUSED*/
8297632Ssam tmioctl(dev, cmd, data, flag)
8307632Ssam 	caddr_t data;
8311919Swnj 	dev_t dev;
8321919Swnj {
8333095Swnj 	int teunit = TEUNIT(dev);
8343095Swnj 	register struct te_softc *sc = &te_softc[teunit];
8353095Swnj 	register struct buf *bp = &ctmbuf[TMUNIT(dev)];
8361919Swnj 	register callcount;
8371919Swnj 	int fcount;
8387632Ssam 	struct mtop *mtop;
8397632Ssam 	struct mtget *mtget;
8401919Swnj 	/* we depend of the values and order of the MT codes here */
8412608Swnj 	static tmops[] =
8422608Swnj 	   {TM_WEOF,TM_SFORW,TM_SREV,TM_SFORW,TM_SREV,TM_REW,TM_OFFL,TM_SENSE};
8431919Swnj 
8442608Swnj 	switch (cmd) {
8457632Ssam 
8467632Ssam 	case MTIOCTOP:	/* tape operation */
8477632Ssam 		mtop = (struct mtop *)data;
8488574Sroot 		switch (mtop->mt_op) {
8497632Ssam 
8502608Swnj 		case MTWEOF:
8517632Ssam 			callcount = mtop->mt_count;
8522608Swnj 			fcount = 1;
8532608Swnj 			break;
8547632Ssam 
8552608Swnj 		case MTFSF: case MTBSF:
8567632Ssam 			callcount = mtop->mt_count;
8571919Swnj 			fcount = INF;
8581919Swnj 			break;
8597632Ssam 
8601919Swnj 		case MTFSR: case MTBSR:
8611919Swnj 			callcount = 1;
8627632Ssam 			fcount = mtop->mt_count;
8631919Swnj 			break;
8647632Ssam 
8652324Skre 		case MTREW: case MTOFFL: case MTNOP:
8661919Swnj 			callcount = 1;
8671919Swnj 			fcount = 1;
8681919Swnj 			break;
8697632Ssam 
8701919Swnj 		default:
8718574Sroot 			return (ENXIO);
8721919Swnj 		}
8738574Sroot 		if (callcount <= 0 || fcount <= 0)
8748574Sroot 			return (EINVAL);
8752608Swnj 		while (--callcount >= 0) {
8767632Ssam 			tmcommand(dev, tmops[mtop->mt_op], fcount);
8777632Ssam 			if ((mtop->mt_op == MTFSR || mtop->mt_op == MTBSR) &&
8788574Sroot 			    bp->b_resid)
8798574Sroot 				return (EIO);
8803095Swnj 			if ((bp->b_flags&B_ERROR) || sc->sc_erreg&TMER_BOT)
8811919Swnj 				break;
8821919Swnj 		}
883*8648Sroot 		return (geterror(bp));
8847632Ssam 
8851919Swnj 	case MTIOCGET:
8867632Ssam 		mtget = (struct mtget *)data;
8877632Ssam 		mtget->mt_dsreg = sc->sc_dsreg;
8887632Ssam 		mtget->mt_erreg = sc->sc_erreg;
8897632Ssam 		mtget->mt_resid = sc->sc_resid;
8907632Ssam 		mtget->mt_type = MT_ISTM;
8918574Sroot 		break;
8927632Ssam 
8931919Swnj 	default:
8948574Sroot 		return (ENXIO);
8951919Swnj 	}
8968574Sroot 	return (0);
8971919Swnj }
8981919Swnj 
8991919Swnj #define	DBSIZE	20
9001919Swnj 
9012363Swnj tmdump()
9022363Swnj {
9032982Swnj 	register struct uba_device *ui;
9042396Swnj 	register struct uba_regs *up;
9055692Sroot 	register struct tmdevice *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);
9185692Sroot 	addr = (struct tmdevice *)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;
9395692Sroot 	register struct tmdevice *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)
9575692Sroot 	register struct tmdevice *addr;
9581919Swnj {
9591928Swnj 	register s;
9601919Swnj 
9611919Swnj 	do
9622396Swnj 		s = addr->tmcs;
9632608Swnj 	while ((s & TM_CUR) == 0);
9641919Swnj }
9651919Swnj 
9662396Swnj tmeof(addr)
9675692Sroot 	struct tmdevice *addr;
9681919Swnj {
9691919Swnj 
9702396Swnj 	tmwait(addr);
9712608Swnj 	addr->tmcs = TM_WEOF | TM_GO;
9721919Swnj }
9731919Swnj #endif
974