xref: /csrg-svn/sys/vax/uba/tm.c (revision 8574)
1*8574Sroot /*	tm.c	4.56	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"
301919Swnj 
318479Sroot #include "../vax/cpu.h"
328479Sroot #include "../vaxuba/ubareg.h"
338479Sroot #include "../vaxuba/ubavar.h"
348479Sroot #include "../vaxuba/tmreg.h"
351919Swnj 
363095Swnj /*
373095Swnj  * There is a ctmbuf per tape controller.
383095Swnj  * It is used as the token to pass to the internal routines
393095Swnj  * to execute tape ioctls, and also acts as a lock on the slaves
403095Swnj  * on the controller, since there is only one per controller.
413095Swnj  * In particular, when the tape is rewinding on close we release
423095Swnj  * the user process but any further attempts to use the tape drive
433095Swnj  * before the rewind completes will hang waiting for ctmbuf.
443095Swnj  */
453095Swnj struct	buf	ctmbuf[NTM];
461919Swnj 
473095Swnj /*
483095Swnj  * Raw tape operations use rtmbuf.  The driver
493095Swnj  * notices when rtmbuf is being used and allows the user
503095Swnj  * program to continue after errors and read records
513095Swnj  * not of the standard length (BSIZE).
523095Swnj  */
533095Swnj struct	buf	rtmbuf[NTM];
543095Swnj 
553095Swnj /*
563095Swnj  * Driver unibus interface routines and variables.
573095Swnj  */
582608Swnj int	tmprobe(), tmslave(), tmattach(), tmdgo(), tmintr();
592982Swnj struct	uba_ctlr *tmminfo[NTM];
603095Swnj struct	uba_device *tedinfo[NTE];
613095Swnj struct	buf teutab[NTE];
623095Swnj short	tetotm[NTE];
632458Swnj u_short	tmstd[] = { 0772520, 0 };
642396Swnj struct	uba_driver tmdriver =
653095Swnj  { tmprobe, tmslave, tmattach, tmdgo, tmstd, "te", tedinfo, "tm", tmminfo, 0 };
661919Swnj 
671919Swnj /* bits in minor device */
683095Swnj #define	TEUNIT(dev)	(minor(dev)&03)
693095Swnj #define	TMUNIT(dev)	(tetotm[TEUNIT(dev)])
701919Swnj #define	T_NOREWIND	04
711919Swnj #define	T_1600BPI	08
721919Swnj 
731919Swnj #define	INF	(daddr_t)1000000L
741919Swnj 
752608Swnj /*
762608Swnj  * Software state per tape transport.
773095Swnj  *
783095Swnj  * 1. A tape drive is a unique-open device; we refuse opens when it is already.
793095Swnj  * 2. We keep track of the current position on a block tape and seek
803095Swnj  *    before operations by forward/back spacing if necessary.
813095Swnj  * 3. We remember if the last operation was a write on a tape, so if a tape
823095Swnj  *    is open read write and the last thing done is a write we can
833095Swnj  *    write a standard end of tape mark (two eofs).
843095Swnj  * 4. We remember the status registers after the last command, using
853095Swnj  *    then internally and returning them to the SENSE ioctl.
863095Swnj  * 5. We remember the last density the tape was used at.  If it is
873095Swnj  *    not a BOT when we start using it and we are writing, we don't
883095Swnj  *    let the density be changed.
892608Swnj  */
903095Swnj struct	te_softc {
912608Swnj 	char	sc_openf;	/* lock against multiple opens */
922608Swnj 	char	sc_lastiow;	/* last op was a write */
932608Swnj 	daddr_t	sc_blkno;	/* block number, for block device tape */
943095Swnj 	daddr_t	sc_nxrec;	/* position of end of tape, if known */
952608Swnj 	u_short	sc_erreg;	/* copy of last erreg */
962608Swnj 	u_short	sc_dsreg;	/* copy of last dsreg */
972608Swnj 	short	sc_resid;	/* copy of last bc */
983105Swnj #ifdef unneeded
992670Swnj 	short	sc_lastcmd;	/* last command to handle direction changes */
1002928Swnj #endif
1013095Swnj 	u_short	sc_dens;	/* prototype command with density info */
1023495Sroot 	daddr_t	sc_timo;	/* time until timeout expires */
1033495Sroot 	short	sc_tact;	/* timeout is active */
1046952Swnj } te_softc[NTE];
1053105Swnj #ifdef unneeded
1063105Swnj int	tmgapsdcnt;		/* DEBUG */
1073105Swnj #endif
1081919Swnj 
1092608Swnj /*
1103095Swnj  * States for um->um_tab.b_active, the per controller state flag.
1113095Swnj  * This is used to sequence control in the driver.
1122608Swnj  */
1131919Swnj #define	SSEEK	1		/* seeking */
1141919Swnj #define	SIO	2		/* doing seq i/o */
1151919Swnj #define	SCOM	3		/* sending control command */
1162608Swnj #define	SREW	4		/* sending a drive rewind */
1171919Swnj 
1182426Skre /*
1192426Skre  * Determine if there is a controller for
1202426Skre  * a tm at address reg.  Our goal is to make the
1212426Skre  * device interrupt.
1222426Skre  */
1232608Swnj tmprobe(reg)
1242396Swnj 	caddr_t reg;
1252396Swnj {
1263095Swnj 	register int br, cvec;		/* must be r11,r10; value-result */
1272426Skre 
1282608Swnj #ifdef lint
1293105Swnj 	br = 0; cvec = br; br = cvec;
1304936Swnj 	tmintr(0);
1312608Swnj #endif
1325692Sroot 	((struct tmdevice *)reg)->tmcs = TM_IE;
1332396Swnj 	/*
1342630Swnj 	 * If this is a tm11, it ought to have interrupted
1352396Swnj 	 * by now, if it isn't (ie: it is a ts04) then we just
1362458Swnj 	 * hope that it didn't interrupt, so autoconf will ignore it.
1372458Swnj 	 * Just in case, we will reference one
1382396Swnj 	 * of the more distant registers, and hope for a machine
1392458Swnj 	 * check, or similar disaster if this is a ts.
1402471Swnj 	 *
1412471Swnj 	 * Note: on an 11/780, badaddr will just generate
1422471Swnj 	 * a uba error for a ts; but our caller will notice that
1432471Swnj 	 * so we won't check for it.
1442396Swnj 	 */
1455692Sroot 	if (badaddr((caddr_t)&((struct tmdevice *)reg)->tmrd, 2))
1462458Swnj 		return (0);
1477404Skre 	return (sizeof (struct tmdevice));
1482396Swnj }
1492396Swnj 
1502608Swnj /*
1512608Swnj  * Due to a design flaw, we cannot ascertain if the tape
1522608Swnj  * exists or not unless it is on line - ie: unless a tape is
1532608Swnj  * mounted. This is too servere a restriction to bear,
1542608Swnj  * so all units are assumed to exist.
1552608Swnj  */
1562608Swnj /*ARGSUSED*/
1572574Swnj tmslave(ui, reg)
1582982Swnj 	struct uba_device *ui;
1592396Swnj 	caddr_t reg;
1602396Swnj {
1612458Swnj 
1622458Swnj 	return (1);
1632396Swnj }
1642396Swnj 
1652608Swnj /*
1663095Swnj  * Record attachment of the unit to the controller.
1672608Swnj  */
1682608Swnj /*ARGSUSED*/
1692608Swnj tmattach(ui)
1702982Swnj 	struct uba_device *ui;
1712608Swnj {
1723095Swnj 	/*
1733095Swnj 	 * Tetotm is used in TMUNIT to index the ctmbuf and rtmbuf
1743095Swnj 	 * arrays given a te unit number.
1753095Swnj 	 */
1763095Swnj 	tetotm[ui->ui_unit] = ui->ui_mi->um_ctlr;
1772608Swnj }
1782608Swnj 
1793495Sroot int	tmtimer();
1802608Swnj /*
1812608Swnj  * Open the device.  Tapes are unique open
1822608Swnj  * devices, so we refuse if it is already open.
1832608Swnj  * We also check that a tape is available, and
1843095Swnj  * don't block waiting here; if you want to wait
1853095Swnj  * for a tape you should timeout in user code.
1862608Swnj  */
1871919Swnj tmopen(dev, flag)
1881919Swnj 	dev_t dev;
1891919Swnj 	int flag;
1901919Swnj {
1913095Swnj 	register int teunit;
1922982Swnj 	register struct uba_device *ui;
1933095Swnj 	register struct te_softc *sc;
1943209Swnj 	int olddens, dens;
1955437Sroot 	int s;
1961919Swnj 
1973095Swnj 	teunit = TEUNIT(dev);
1983095Swnj 	if (teunit>=NTE || (sc = &te_softc[teunit])->sc_openf ||
199*8574Sroot 	    (ui = tedinfo[teunit]) == 0 || ui->ui_alive == 0)
200*8574Sroot 		return (ENXIO);
2013209Swnj 	olddens = sc->sc_dens;
2023209Swnj 	dens = TM_IE | TM_GO | (ui->ui_slave << 8);
2033209Swnj 	if ((minor(dev) & T_1600BPI) == 0)
2043209Swnj 		dens |= TM_D800;
2053209Swnj 	sc->sc_dens = dens;
2063141Swnj get:
2072608Swnj 	tmcommand(dev, TM_SENSE, 1);
2083141Swnj 	if (sc->sc_erreg&TMER_SDWN) {
2093141Swnj 		sleep((caddr_t)&lbolt, PZERO+1);
2103141Swnj 		goto get;
2113141Swnj 	}
2123209Swnj 	sc->sc_dens = olddens;
2133710Sroot 	if ((sc->sc_erreg&(TMER_SELR|TMER_TUR)) != (TMER_SELR|TMER_TUR)) {
2143710Sroot 		uprintf("te%d: not online\n", teunit);
215*8574Sroot 		return (EIO);
2161919Swnj 	}
2173710Sroot 	if ((flag&FWRITE) && (sc->sc_erreg&TMER_WRL)) {
2183710Sroot 		uprintf("te%d: no write ring\n", teunit);
219*8574Sroot 		return (EIO);
2203710Sroot 	}
2213710Sroot 	if ((sc->sc_erreg&TMER_BOT) == 0 && (flag&FWRITE) &&
2223710Sroot 	    dens != sc->sc_dens) {
2233710Sroot 		uprintf("te%d: can't change density in mid-tape\n", teunit);
224*8574Sroot 		return (EIO);
2253710Sroot 	}
2262608Swnj 	sc->sc_openf = 1;
2272471Swnj 	sc->sc_blkno = (daddr_t)0;
2282471Swnj 	sc->sc_nxrec = INF;
2292608Swnj 	sc->sc_lastiow = 0;
2303095Swnj 	sc->sc_dens = dens;
2315437Sroot 	s = spl6();
2323495Sroot 	if (sc->sc_tact == 0) {
2333495Sroot 		sc->sc_timo = INF;
2343495Sroot 		sc->sc_tact = 1;
2353629Sroot 		timeout(tmtimer, (caddr_t)dev, 5*hz);
2363495Sroot 	}
2375437Sroot 	splx(s);
238*8574Sroot 	return (0);
2391919Swnj }
2401919Swnj 
2412608Swnj /*
2422608Swnj  * Close tape device.
2432608Swnj  *
2442608Swnj  * If tape was open for writing or last operation was
2452608Swnj  * a write, then write two EOF's and backspace over the last one.
2462608Swnj  * Unless this is a non-rewinding special file, rewind the tape.
2472608Swnj  * Make the tape available to others.
2482608Swnj  */
2491919Swnj tmclose(dev, flag)
2501919Swnj 	register dev_t dev;
2511919Swnj 	register flag;
2521919Swnj {
2533095Swnj 	register struct te_softc *sc = &te_softc[TEUNIT(dev)];
2541919Swnj 
2552608Swnj 	if (flag == FWRITE || (flag&FWRITE) && sc->sc_lastiow) {
2562608Swnj 		tmcommand(dev, TM_WEOF, 1);
2572608Swnj 		tmcommand(dev, TM_WEOF, 1);
2582608Swnj 		tmcommand(dev, TM_SREV, 1);
2591919Swnj 	}
2601919Swnj 	if ((minor(dev)&T_NOREWIND) == 0)
2613095Swnj 		/*
2623095Swnj 		 * 0 count means don't hang waiting for rewind complete
2633095Swnj 		 * rather ctmbuf stays busy until the operation completes
2643095Swnj 		 * preventing further opens from completing by
2653095Swnj 		 * preventing a TM_SENSE from completing.
2663095Swnj 		 */
2673095Swnj 		tmcommand(dev, TM_REW, 0);
2682471Swnj 	sc->sc_openf = 0;
2691919Swnj }
2701919Swnj 
2712608Swnj /*
2722608Swnj  * Execute a command on the tape drive
2732608Swnj  * a specified number of times.
2742608Swnj  */
2752574Swnj tmcommand(dev, com, count)
2761919Swnj 	dev_t dev;
2771919Swnj 	int com, count;
2781919Swnj {
2791919Swnj 	register struct buf *bp;
2805437Sroot 	register int s;
2811919Swnj 
2822608Swnj 	bp = &ctmbuf[TMUNIT(dev)];
2835437Sroot 	s = spl5();
2841919Swnj 	while (bp->b_flags&B_BUSY) {
2853095Swnj 		/*
2863095Swnj 		 * This special check is because B_BUSY never
2873095Swnj 		 * gets cleared in the non-waiting rewind case.
2883095Swnj 		 */
2893141Swnj 		if (bp->b_repcnt == 0 && (bp->b_flags&B_DONE))
2903095Swnj 			break;
2911919Swnj 		bp->b_flags |= B_WANTED;
2921919Swnj 		sleep((caddr_t)bp, PRIBIO);
2931919Swnj 	}
2941919Swnj 	bp->b_flags = B_BUSY|B_READ;
2955437Sroot 	splx(s);
2961919Swnj 	bp->b_dev = dev;
2971919Swnj 	bp->b_repcnt = -count;
2981919Swnj 	bp->b_command = com;
2991919Swnj 	bp->b_blkno = 0;
3001919Swnj 	tmstrategy(bp);
3013095Swnj 	/*
3023095Swnj 	 * In case of rewind from close, don't wait.
3033095Swnj 	 * This is the only case where count can be 0.
3043095Swnj 	 */
3053095Swnj 	if (count == 0)
3063095Swnj 		return;
3071919Swnj 	iowait(bp);
3081919Swnj 	if (bp->b_flags&B_WANTED)
3091919Swnj 		wakeup((caddr_t)bp);
3101919Swnj 	bp->b_flags &= B_ERROR;
3111919Swnj }
3121919Swnj 
3132608Swnj /*
3143095Swnj  * Queue a tape operation.
3152608Swnj  */
3161919Swnj tmstrategy(bp)
3171919Swnj 	register struct buf *bp;
3181919Swnj {
3193095Swnj 	int teunit = TEUNIT(bp->b_dev);
3202982Swnj 	register struct uba_ctlr *um;
3212608Swnj 	register struct buf *dp;
3225437Sroot 	int s;
3231919Swnj 
3242608Swnj 	/*
3252608Swnj 	 * Put transfer at end of unit queue
3262608Swnj 	 */
3273095Swnj 	dp = &teutab[teunit];
3281919Swnj 	bp->av_forw = NULL;
3295437Sroot 	s = spl5();
3303939Sbugs 	um = tedinfo[teunit]->ui_mi;
3312608Swnj 	if (dp->b_actf == NULL) {
3322608Swnj 		dp->b_actf = bp;
3332608Swnj 		/*
3342608Swnj 		 * Transport not already active...
3352608Swnj 		 * put at end of controller queue.
3362608Swnj 		 */
3372608Swnj 		dp->b_forw = NULL;
3382608Swnj 		if (um->um_tab.b_actf == NULL)
3392608Swnj 			um->um_tab.b_actf = dp;
3402608Swnj 		else
3412608Swnj 			um->um_tab.b_actl->b_forw = dp;
3422608Swnj 		um->um_tab.b_actl = dp;
3432608Swnj 	} else
3442608Swnj 		dp->b_actl->av_forw = bp;
3452608Swnj 	dp->b_actl = bp;
3462608Swnj 	/*
3472608Swnj 	 * If the controller is not busy, get
3482608Swnj 	 * it going.
3492608Swnj 	 */
3502608Swnj 	if (um->um_tab.b_active == 0)
3512608Swnj 		tmstart(um);
3525437Sroot 	splx(s);
3531919Swnj }
3541919Swnj 
3552608Swnj /*
3562608Swnj  * Start activity on a tm controller.
3572608Swnj  */
3582608Swnj tmstart(um)
3592982Swnj 	register struct uba_ctlr *um;
3601919Swnj {
3612608Swnj 	register struct buf *bp, *dp;
3625692Sroot 	register struct tmdevice *addr = (struct tmdevice *)um->um_addr;
3633095Swnj 	register struct te_softc *sc;
3642982Swnj 	register struct uba_device *ui;
3653095Swnj 	int teunit, cmd;
3662471Swnj 	daddr_t blkno;
3671919Swnj 
3682608Swnj 	/*
3692608Swnj 	 * Look for an idle transport on the controller.
3702608Swnj 	 */
3711919Swnj loop:
3722608Swnj 	if ((dp = um->um_tab.b_actf) == NULL)
3731919Swnj 		return;
3742608Swnj 	if ((bp = dp->b_actf) == NULL) {
3752608Swnj 		um->um_tab.b_actf = dp->b_forw;
3762608Swnj 		goto loop;
3772608Swnj 	}
3783095Swnj 	teunit = TEUNIT(bp->b_dev);
3793095Swnj 	ui = tedinfo[teunit];
3802608Swnj 	/*
3812608Swnj 	 * Record pre-transfer status (e.g. for TM_SENSE)
3822608Swnj 	 */
3833095Swnj 	sc = &te_softc[teunit];
3845692Sroot 	addr = (struct tmdevice *)um->um_addr;
3852608Swnj 	addr->tmcs = (ui->ui_slave << 8);
3862471Swnj 	sc->sc_dsreg = addr->tmcs;
3872471Swnj 	sc->sc_erreg = addr->tmer;
3882471Swnj 	sc->sc_resid = addr->tmbc;
3892608Swnj 	/*
3902608Swnj 	 * Default is that last command was NOT a write command;
3912608Swnj 	 * if we do a write command we will notice this in tmintr().
3922608Swnj 	 */
3933493Sroot 	sc->sc_lastiow = 0;
3942608Swnj 	if (sc->sc_openf < 0 || (addr->tmcs&TM_CUR) == 0) {
3952608Swnj 		/*
3963095Swnj 		 * Have had a hard error on a non-raw tape
3973095Swnj 		 * or the tape unit is now unavailable
3983095Swnj 		 * (e.g. taken off line).
3992608Swnj 		 */
4002608Swnj 		bp->b_flags |= B_ERROR;
4011919Swnj 		goto next;
4021919Swnj 	}
4033095Swnj 	if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) {
4043095Swnj 		/*
4053095Swnj 		 * Execute control operation with the specified count.
4063095Swnj 		 */
4072608Swnj 		if (bp->b_command == TM_SENSE)
4082608Swnj 			goto next;
4093495Sroot 		/*
4103495Sroot 		 * Set next state; give 5 minutes to complete
4113495Sroot 		 * rewind, or 10 seconds per iteration (minimum 60
4123629Sroot 		 * seconds and max 5 minutes) to complete other ops.
4133495Sroot 		 */
4143495Sroot 		if (bp->b_command == TM_REW) {
4153495Sroot 			um->um_tab.b_active = SREW;
4163495Sroot 			sc->sc_timo = 5 * 60;
4173495Sroot 		} else {
4183495Sroot 			um->um_tab.b_active = SCOM;
4194266Swnj 			sc->sc_timo =
4204266Swnj 			    imin(imax(10*(int)-bp->b_repcnt,60),5*60);
4213495Sroot 		}
4222608Swnj 		if (bp->b_command == TM_SFORW || bp->b_command == TM_SREV)
4232608Swnj 			addr->tmbc = bp->b_repcnt;
4242670Swnj 		goto dobpcmd;
4252608Swnj 	}
4262608Swnj 	/*
4273095Swnj 	 * The following checks handle boundary cases for operation
4283095Swnj 	 * on non-raw tapes.  On raw tapes the initialization of
4293095Swnj 	 * sc->sc_nxrec by tmphys causes them to be skipped normally
4303095Swnj 	 * (except in the case of retries).
4313095Swnj 	 */
4327381Ssam 	if (bdbtofsb(bp->b_blkno) > sc->sc_nxrec) {
4333095Swnj 		/*
4343095Swnj 		 * Can't read past known end-of-file.
4353095Swnj 		 */
4363095Swnj 		bp->b_flags |= B_ERROR;
4373095Swnj 		bp->b_error = ENXIO;
4383095Swnj 		goto next;
4393095Swnj 	}
4407381Ssam 	if (bdbtofsb(bp->b_blkno) == sc->sc_nxrec &&
4413095Swnj 	    bp->b_flags&B_READ) {
4423095Swnj 		/*
4433095Swnj 		 * Reading at end of file returns 0 bytes.
4443095Swnj 		 */
4453095Swnj 		bp->b_resid = bp->b_bcount;
4463095Swnj 		clrbuf(bp);
4473095Swnj 		goto next;
4483095Swnj 	}
4493095Swnj 	if ((bp->b_flags&B_READ) == 0)
4503095Swnj 		/*
4513095Swnj 		 * Writing sets EOF
4523095Swnj 		 */
4537381Ssam 		sc->sc_nxrec = bdbtofsb(bp->b_blkno) + 1;
4543095Swnj 	/*
4552608Swnj 	 * If the data transfer command is in the correct place,
4562608Swnj 	 * set up all the registers except the csr, and give
4572608Swnj 	 * control over to the UNIBUS adapter routines, to
4582608Swnj 	 * wait for resources to start the i/o.
4592608Swnj 	 */
4607381Ssam 	if ((blkno = sc->sc_blkno) == bdbtofsb(bp->b_blkno)) {
4612396Swnj 		addr->tmbc = -bp->b_bcount;
4621919Swnj 		if ((bp->b_flags&B_READ) == 0) {
4632471Swnj 			if (um->um_tab.b_errcnt)
4643095Swnj 				cmd = TM_WIRG;
4651919Swnj 			else
4663095Swnj 				cmd = TM_WCOM;
4671919Swnj 		} else
4683095Swnj 			cmd = TM_RCOM;
4692471Swnj 		um->um_tab.b_active = SIO;
4703095Swnj 		um->um_cmd = sc->sc_dens|cmd;
4712928Swnj #ifdef notdef
4722670Swnj 		if (tmreverseop(sc->sc_lastcmd))
4733095Swnj 			while (addr->tmer & TMER_SDWN)
4742670Swnj 				tmgapsdcnt++;
4752670Swnj 		sc->sc_lastcmd = TM_RCOM;		/* will serve */
4762928Swnj #endif
4773495Sroot 		sc->sc_timo = 60;	/* premature, but should serve */
4783105Swnj 		(void) ubago(ui);
4791919Swnj 		return;
4801919Swnj 	}
4812608Swnj 	/*
4823095Swnj 	 * Tape positioned incorrectly;
4833095Swnj 	 * set to seek forwards or backwards to the correct spot.
4843095Swnj 	 * This happens for raw tapes only on error retries.
4852608Swnj 	 */
4862471Swnj 	um->um_tab.b_active = SSEEK;
4877381Ssam 	if (blkno < bdbtofsb(bp->b_blkno)) {
4882670Swnj 		bp->b_command = TM_SFORW;
4897381Ssam 		addr->tmbc = blkno - bdbtofsb(bp->b_blkno);
4901919Swnj 	} else {
4912670Swnj 		bp->b_command = TM_SREV;
4927381Ssam 		addr->tmbc = bdbtofsb(bp->b_blkno) - blkno;
4931919Swnj 	}
4943629Sroot 	sc->sc_timo = imin(imax(10 * -addr->tmbc, 60), 5 * 60);
4952670Swnj dobpcmd:
4962928Swnj #ifdef notdef
4973095Swnj 	/*
4983095Swnj 	 * It is strictly necessary to wait for the tape
4993095Swnj 	 * to stop before changing directions, but the TC11
5003095Swnj 	 * handles this for us.
5013095Swnj 	 */
5022670Swnj 	if (tmreverseop(sc->sc_lastcmd) != tmreverseop(bp->b_command))
5032670Swnj 		while (addr->tmer & TM_SDWN)
5042670Swnj 			tmgapsdcnt++;
5052670Swnj 	sc->sc_lastcmd = bp->b_command;
5062928Swnj #endif
5073095Swnj 	/*
5083095Swnj 	 * Do the command in bp.
5093095Swnj 	 */
5103095Swnj 	addr->tmcs = (sc->sc_dens | bp->b_command);
5111919Swnj 	return;
5121919Swnj 
5131919Swnj next:
5142608Swnj 	/*
5152608Swnj 	 * Done with this operation due to error or
5162608Swnj 	 * the fact that it doesn't do anything.
5172608Swnj 	 * Release UBA resources (if any), dequeue
5182608Swnj 	 * the transfer and continue processing this slave.
5192608Swnj 	 */
5202608Swnj 	if (um->um_ubinfo)
5212617Swnj 		ubadone(um);
5222608Swnj 	um->um_tab.b_errcnt = 0;
5232608Swnj 	dp->b_actf = bp->av_forw;
5241919Swnj 	iodone(bp);
5251919Swnj 	goto loop;
5261919Swnj }
5271919Swnj 
5282608Swnj /*
5292608Swnj  * The UNIBUS resources we needed have been
5302608Swnj  * allocated to us; start the device.
5312608Swnj  */
5322574Swnj tmdgo(um)
5332982Swnj 	register struct uba_ctlr *um;
5341919Swnj {
5355692Sroot 	register struct tmdevice *addr = (struct tmdevice *)um->um_addr;
5362471Swnj 
5372574Swnj 	addr->tmba = um->um_ubinfo;
5382574Swnj 	addr->tmcs = um->um_cmd | ((um->um_ubinfo >> 12) & 0x30);
5392396Swnj }
5402396Swnj 
5412608Swnj /*
5422608Swnj  * Tm interrupt routine.
5432608Swnj  */
5442471Swnj /*ARGSUSED*/
5452630Swnj tmintr(tm11)
5462630Swnj 	int tm11;
5472396Swnj {
5482608Swnj 	struct buf *dp;
5491919Swnj 	register struct buf *bp;
5502982Swnj 	register struct uba_ctlr *um = tmminfo[tm11];
5515692Sroot 	register struct tmdevice *addr;
5523095Swnj 	register struct te_softc *sc;
5533095Swnj 	int teunit;
5541919Swnj 	register state;
5551919Swnj 
5563095Swnj 	if ((dp = um->um_tab.b_actf) == NULL)
5573095Swnj 		return;
5583095Swnj 	bp = dp->b_actf;
5593095Swnj 	teunit = TEUNIT(bp->b_dev);
5605692Sroot 	addr = (struct tmdevice *)tedinfo[teunit]->ui_addr;
5613524Swnj 	sc = &te_softc[teunit];
5622608Swnj 	/*
5632608Swnj 	 * If last command was a rewind, and tape is still
5642608Swnj 	 * rewinding, wait for the rewind complete interrupt.
5652608Swnj 	 */
5662608Swnj 	if (um->um_tab.b_active == SREW) {
5672608Swnj 		um->um_tab.b_active = SCOM;
5683524Swnj 		if (addr->tmer&TMER_RWS) {
5693524Swnj 			sc->sc_timo = 5*60;		/* 5 minutes */
5702608Swnj 			return;
5713524Swnj 		}
5721919Swnj 	}
5732608Swnj 	/*
5742608Swnj 	 * An operation completed... record status
5752608Swnj 	 */
5763495Sroot 	sc->sc_timo = INF;
5772471Swnj 	sc->sc_dsreg = addr->tmcs;
5782471Swnj 	sc->sc_erreg = addr->tmer;
5792471Swnj 	sc->sc_resid = addr->tmbc;
5801919Swnj 	if ((bp->b_flags & B_READ) == 0)
5812608Swnj 		sc->sc_lastiow = 1;
5822471Swnj 	state = um->um_tab.b_active;
5832471Swnj 	um->um_tab.b_active = 0;
5842608Swnj 	/*
5852608Swnj 	 * Check for errors.
5862608Swnj 	 */
5872608Swnj 	if (addr->tmcs&TM_ERR) {
5883095Swnj 		while (addr->tmer & TMER_SDWN)
5891919Swnj 			;			/* await settle down */
5902608Swnj 		/*
5913095Swnj 		 * If we hit the end of the tape file, update our position.
5922608Swnj 		 */
5933095Swnj 		if (addr->tmer&TMER_EOF) {
5942608Swnj 			tmseteof(bp);		/* set blkno and nxrec */
5952608Swnj 			state = SCOM;		/* force completion */
5962608Swnj 			/*
5972608Swnj 			 * Stuff bc so it will be unstuffed correctly
5982608Swnj 			 * later to get resid.
5992608Swnj 			 */
6002396Swnj 			addr->tmbc = -bp->b_bcount;
6012608Swnj 			goto opdone;
6021919Swnj 		}
6032608Swnj 		/*
6043095Swnj 		 * If we were reading raw tape and the only error was that the
6053095Swnj 		 * record was too long, then we don't consider this an error.
6062608Swnj 		 */
6073095Swnj 		if (bp == &rtmbuf[TMUNIT(bp->b_dev)] && (bp->b_flags&B_READ) &&
6083095Swnj 		    (addr->tmer&(TMER_HARD|TMER_SOFT)) == TMER_RLE)
6092608Swnj 			goto ignoreerr;
6102608Swnj 		/*
6112608Swnj 		 * If error is not hard, and this was an i/o operation
6122608Swnj 		 * retry up to 8 times.
6132608Swnj 		 */
6143095Swnj 		if ((addr->tmer&TMER_HARD)==0 && state==SIO) {
6152471Swnj 			if (++um->um_tab.b_errcnt < 7) {
6162471Swnj 				sc->sc_blkno++;
6172617Swnj 				ubadone(um);
6182608Swnj 				goto opcont;
6191919Swnj 			}
6202608Swnj 		} else
6212608Swnj 			/*
6222608Swnj 			 * Hard or non-i/o errors on non-raw tape
6232608Swnj 			 * cause it to close.
6242608Swnj 			 */
6253095Swnj 			if (sc->sc_openf>0 && bp != &rtmbuf[TMUNIT(bp->b_dev)])
6262608Swnj 				sc->sc_openf = -1;
6272608Swnj 		/*
6282608Swnj 		 * Couldn't recover error
6292608Swnj 		 */
6302928Swnj 		printf("te%d: hard error bn%d er=%b\n", minor(bp->b_dev)&03,
6313095Swnj 		    bp->b_blkno, sc->sc_erreg, TMER_BITS);
6321919Swnj 		bp->b_flags |= B_ERROR;
6332608Swnj 		goto opdone;
6341919Swnj 	}
6352608Swnj 	/*
6362608Swnj 	 * Advance tape control FSM.
6372608Swnj 	 */
6382608Swnj ignoreerr:
6391919Swnj 	switch (state) {
6401919Swnj 
6411919Swnj 	case SIO:
6422608Swnj 		/*
6432608Swnj 		 * Read/write increments tape block number
6442608Swnj 		 */
6452471Swnj 		sc->sc_blkno++;
6462608Swnj 		goto opdone;
6471919Swnj 
6481919Swnj 	case SCOM:
6492608Swnj 		/*
6503095Swnj 		 * For forward/backward space record update current position.
6512608Swnj 		 */
6523095Swnj 		if (bp == &ctmbuf[TMUNIT(bp->b_dev)])
6532608Swnj 		switch (bp->b_command) {
6541919Swnj 
6552608Swnj 		case TM_SFORW:
6562608Swnj 			sc->sc_blkno -= bp->b_repcnt;
6573095Swnj 			break;
6581919Swnj 
6592608Swnj 		case TM_SREV:
6602608Swnj 			sc->sc_blkno += bp->b_repcnt;
6613095Swnj 			break;
6621919Swnj 		}
6633095Swnj 		goto opdone;
6641919Swnj 
6651919Swnj 	case SSEEK:
6667381Ssam 		sc->sc_blkno = bdbtofsb(bp->b_blkno);
6672608Swnj 		goto opcont;
6681919Swnj 
6691919Swnj 	default:
6702608Swnj 		panic("tmintr");
6712608Swnj 	}
6722608Swnj opdone:
6732608Swnj 	/*
6742608Swnj 	 * Reset error count and remove
6752608Swnj 	 * from device queue.
6762608Swnj 	 */
6772608Swnj 	um->um_tab.b_errcnt = 0;
6782608Swnj 	dp->b_actf = bp->av_forw;
6792608Swnj 	bp->b_resid = -addr->tmbc;
6802617Swnj 	ubadone(um);
6812608Swnj 	iodone(bp);
6822608Swnj 	/*
6832608Swnj 	 * Circulate slave to end of controller
6842608Swnj 	 * queue to give other slaves a chance.
6852608Swnj 	 */
6862608Swnj 	um->um_tab.b_actf = dp->b_forw;
6872608Swnj 	if (dp->b_actf) {
6882608Swnj 		dp->b_forw = NULL;
6892608Swnj 		if (um->um_tab.b_actf == NULL)
6902608Swnj 			um->um_tab.b_actf = dp;
6912608Swnj 		else
6922608Swnj 			um->um_tab.b_actl->b_forw = dp;
6932608Swnj 		um->um_tab.b_actl = dp;
6942608Swnj 	}
6952608Swnj 	if (um->um_tab.b_actf == 0)
6961919Swnj 		return;
6972608Swnj opcont:
6982608Swnj 	tmstart(um);
6991919Swnj }
7001919Swnj 
7013495Sroot tmtimer(dev)
7023495Sroot 	int dev;
7033495Sroot {
7043495Sroot 	register struct te_softc *sc = &te_softc[TEUNIT(dev)];
7054847Sroot 	register short x;
7063495Sroot 
7073495Sroot 	if (sc->sc_timo != INF && (sc->sc_timo -= 5) < 0) {
7084278Sroot 		printf("te%d: lost interrupt\n", TEUNIT(dev));
7093495Sroot 		sc->sc_timo = INF;
7104847Sroot 		x = spl5();
7113495Sroot 		tmintr(TMUNIT(dev));
7124847Sroot 		(void) splx(x);
7133495Sroot 	}
7143629Sroot 	timeout(tmtimer, (caddr_t)dev, 5*hz);
7153495Sroot }
7163495Sroot 
7171919Swnj tmseteof(bp)
7181919Swnj 	register struct buf *bp;
7191919Swnj {
7203095Swnj 	register int teunit = TEUNIT(bp->b_dev);
7215692Sroot 	register struct tmdevice *addr =
7225692Sroot 	    (struct tmdevice *)tedinfo[teunit]->ui_addr;
7233095Swnj 	register struct te_softc *sc = &te_softc[teunit];
7241919Swnj 
7253095Swnj 	if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) {
7267381Ssam 		if (sc->sc_blkno > bdbtofsb(bp->b_blkno)) {
7271919Swnj 			/* reversing */
7287381Ssam 			sc->sc_nxrec = bdbtofsb(bp->b_blkno) - addr->tmbc;
7292471Swnj 			sc->sc_blkno = sc->sc_nxrec;
7301919Swnj 		} else {
7311919Swnj 			/* spacing forward */
7327381Ssam 			sc->sc_blkno = bdbtofsb(bp->b_blkno) + addr->tmbc;
7332471Swnj 			sc->sc_nxrec = sc->sc_blkno - 1;
7341919Swnj 		}
7351919Swnj 		return;
7361919Swnj 	}
7371919Swnj 	/* eof on read */
7387381Ssam 	sc->sc_nxrec = bdbtofsb(bp->b_blkno);
7391919Swnj }
7401919Swnj 
7417732Sroot tmread(dev, uio)
7422608Swnj 	dev_t dev;
7437732Sroot 	struct uio *uio;
7441919Swnj {
7458163Sroot 	int errno;
7461919Swnj 
7478163Sroot 	errno = tmphys(dev, uio);
7488163Sroot 	if (errno)
7498163Sroot 		return (errno);
7508163Sroot 	return (physio(tmstrategy, &rtmbuf[TMUNIT(dev)], dev, B_READ, minphys, uio));
7511919Swnj }
7521919Swnj 
7537838Sroot tmwrite(dev, uio)
7542608Swnj 	dev_t dev;
7557838Sroot 	struct uio *uio;
7561919Swnj {
7578163Sroot 	int errno;
7581919Swnj 
7598163Sroot 	errno = tmphys(dev, uio);
7608163Sroot 	if (errno)
7618163Sroot 		return (errno);
7628163Sroot 	return (physio(tmstrategy, &rtmbuf[TMUNIT(dev)], dev, B_WRITE, minphys, uio));
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  */
7707732Sroot tmphys(dev, uio)
7712608Swnj 	dev_t dev;
7727732Sroot 	struct uio *uio;
7731919Swnj {
7743095Swnj 	register int teunit = TEUNIT(dev);
7751919Swnj 	register daddr_t a;
7763095Swnj 	register struct te_softc *sc;
7773095Swnj 	register struct uba_device *ui;
7781919Swnj 
7797838Sroot 	if (teunit >= NTE || (ui=tedinfo[teunit]) == 0 || ui->ui_alive == 0)
7807732Sroot 		return (ENXIO);
7813095Swnj 	sc = &te_softc[teunit];
7827838Sroot 	a = bdbtofsb(uio->uio_offset >> 9);
7832471Swnj 	sc->sc_blkno = a;
7842471Swnj 	sc->sc_nxrec = a + 1;
7857732Sroot 	return (0);
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 		}
8075692Sroot 		((struct tmdevice *)(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*/
8287632Ssam tmioctl(dev, cmd, data, flag)
8297632Ssam 	caddr_t data;
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;
8377632Ssam 	struct mtop *mtop;
8387632Ssam 	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) {
8447632Ssam 
8457632Ssam 	case MTIOCTOP:	/* tape operation */
8467632Ssam 		mtop = (struct mtop *)data;
847*8574Sroot 		switch (mtop->mt_op) {
8487632Ssam 
8492608Swnj 		case MTWEOF:
8507632Ssam 			callcount = mtop->mt_count;
8512608Swnj 			fcount = 1;
8522608Swnj 			break;
8537632Ssam 
8542608Swnj 		case MTFSF: case MTBSF:
8557632Ssam 			callcount = mtop->mt_count;
8561919Swnj 			fcount = INF;
8571919Swnj 			break;
8587632Ssam 
8591919Swnj 		case MTFSR: case MTBSR:
8601919Swnj 			callcount = 1;
8617632Ssam 			fcount = mtop->mt_count;
8621919Swnj 			break;
8637632Ssam 
8642324Skre 		case MTREW: case MTOFFL: case MTNOP:
8651919Swnj 			callcount = 1;
8661919Swnj 			fcount = 1;
8671919Swnj 			break;
8687632Ssam 
8691919Swnj 		default:
870*8574Sroot 			return (ENXIO);
8711919Swnj 		}
872*8574Sroot 		if (callcount <= 0 || fcount <= 0)
873*8574Sroot 			return (EINVAL);
8742608Swnj 		while (--callcount >= 0) {
8757632Ssam 			tmcommand(dev, tmops[mtop->mt_op], fcount);
8767632Ssam 			if ((mtop->mt_op == MTFSR || mtop->mt_op == MTBSR) &&
877*8574Sroot 			    bp->b_resid)
878*8574Sroot 				return (EIO);
8793095Swnj 			if ((bp->b_flags&B_ERROR) || sc->sc_erreg&TMER_BOT)
8801919Swnj 				break;
8811919Swnj 		}
882*8574Sroot 		geterror(bp);		/* XXX */
883*8574Sroot 		return (u.u_error);	/* XXX */
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;
891*8574Sroot 		break;
8927632Ssam 
8931919Swnj 	default:
894*8574Sroot 		return (ENXIO);
8951919Swnj 	}
896*8574Sroot 	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