xref: /csrg-svn/sys/vax/uba/tm.c (revision 3331)
1*3331Swnj /*	tm.c	4.30	81/03/21	*/
21919Swnj 
32709Swnj #include "te.h"
42630Swnj #if NTM > 0
51919Swnj /*
62630Swnj  * TM11/TE10 tape driver
72471Swnj  *
83095Swnj  * TODO:
93095Swnj  *	test driver with more than one slave
103095Swnj  *	test driver with more than one controller
113095Swnj  *	test reset code
123095Swnj  *	what happens if you offline tape during rewind?
133095Swnj  *	test using file system on tape
141919Swnj  */
151919Swnj #include "../h/param.h"
163141Swnj #include "../h/systm.h"
171919Swnj #include "../h/buf.h"
181919Swnj #include "../h/dir.h"
191919Swnj #include "../h/conf.h"
201919Swnj #include "../h/user.h"
211919Swnj #include "../h/file.h"
221919Swnj #include "../h/map.h"
231919Swnj #include "../h/pte.h"
242574Swnj #include "../h/vm.h"
252982Swnj #include "../h/ubareg.h"
262982Swnj #include "../h/ubavar.h"
271919Swnj #include "../h/mtio.h"
281919Swnj #include "../h/ioctl.h"
292363Swnj #include "../h/cmap.h"
302396Swnj #include "../h/cpu.h"
311919Swnj 
322396Swnj #include "../h/tmreg.h"
331919Swnj 
343095Swnj /*
353095Swnj  * There is a ctmbuf per tape controller.
363095Swnj  * It is used as the token to pass to the internal routines
373095Swnj  * to execute tape ioctls, and also acts as a lock on the slaves
383095Swnj  * on the controller, since there is only one per controller.
393095Swnj  * In particular, when the tape is rewinding on close we release
403095Swnj  * the user process but any further attempts to use the tape drive
413095Swnj  * before the rewind completes will hang waiting for ctmbuf.
423095Swnj  */
433095Swnj struct	buf	ctmbuf[NTM];
441919Swnj 
453095Swnj /*
463095Swnj  * Raw tape operations use rtmbuf.  The driver
473095Swnj  * notices when rtmbuf is being used and allows the user
483095Swnj  * program to continue after errors and read records
493095Swnj  * not of the standard length (BSIZE).
503095Swnj  */
513095Swnj struct	buf	rtmbuf[NTM];
523095Swnj 
533095Swnj /*
543095Swnj  * Driver unibus interface routines and variables.
553095Swnj  */
562608Swnj int	tmprobe(), tmslave(), tmattach(), tmdgo(), tmintr();
572982Swnj struct	uba_ctlr *tmminfo[NTM];
583095Swnj struct	uba_device *tedinfo[NTE];
593095Swnj struct	buf teutab[NTE];
603095Swnj short	tetotm[NTE];
612458Swnj u_short	tmstd[] = { 0772520, 0 };
622396Swnj struct	uba_driver tmdriver =
633095Swnj  { tmprobe, tmslave, tmattach, tmdgo, tmstd, "te", tedinfo, "tm", tmminfo, 0 };
641919Swnj 
651919Swnj /* bits in minor device */
663095Swnj #define	TEUNIT(dev)	(minor(dev)&03)
673095Swnj #define	TMUNIT(dev)	(tetotm[TEUNIT(dev)])
681919Swnj #define	T_NOREWIND	04
691919Swnj #define	T_1600BPI	08
701919Swnj 
711919Swnj #define	INF	(daddr_t)1000000L
721919Swnj 
732608Swnj /*
742608Swnj  * Software state per tape transport.
753095Swnj  *
763095Swnj  * 1. A tape drive is a unique-open device; we refuse opens when it is already.
773095Swnj  * 2. We keep track of the current position on a block tape and seek
783095Swnj  *    before operations by forward/back spacing if necessary.
793095Swnj  * 3. We remember if the last operation was a write on a tape, so if a tape
803095Swnj  *    is open read write and the last thing done is a write we can
813095Swnj  *    write a standard end of tape mark (two eofs).
823095Swnj  * 4. We remember the status registers after the last command, using
833095Swnj  *    then internally and returning them to the SENSE ioctl.
843095Swnj  * 5. We remember the last density the tape was used at.  If it is
853095Swnj  *    not a BOT when we start using it and we are writing, we don't
863095Swnj  *    let the density be changed.
872608Swnj  */
883095Swnj struct	te_softc {
892608Swnj 	char	sc_openf;	/* lock against multiple opens */
902608Swnj 	char	sc_lastiow;	/* last op was a write */
912608Swnj 	daddr_t	sc_blkno;	/* block number, for block device tape */
923095Swnj 	daddr_t	sc_nxrec;	/* position of end of tape, if known */
932608Swnj 	u_short	sc_erreg;	/* copy of last erreg */
942608Swnj 	u_short	sc_dsreg;	/* copy of last dsreg */
952608Swnj 	short	sc_resid;	/* copy of last bc */
963105Swnj #ifdef unneeded
972670Swnj 	short	sc_lastcmd;	/* last command to handle direction changes */
982928Swnj #endif
993095Swnj 	u_short	sc_dens;	/* prototype command with density info */
1003095Swnj } te_softc[NTM];
1013105Swnj #ifdef unneeded
1023105Swnj int	tmgapsdcnt;		/* DEBUG */
1033105Swnj #endif
1041919Swnj 
1052608Swnj /*
1063095Swnj  * States for um->um_tab.b_active, the per controller state flag.
1073095Swnj  * This is used to sequence control in the driver.
1082608Swnj  */
1091919Swnj #define	SSEEK	1		/* seeking */
1101919Swnj #define	SIO	2		/* doing seq i/o */
1111919Swnj #define	SCOM	3		/* sending control command */
1122608Swnj #define	SREW	4		/* sending a drive rewind */
1131919Swnj 
1142426Skre /*
1152426Skre  * Determine if there is a controller for
1162426Skre  * a tm at address reg.  Our goal is to make the
1172426Skre  * device interrupt.
1182426Skre  */
1192608Swnj tmprobe(reg)
1202396Swnj 	caddr_t reg;
1212396Swnj {
1223095Swnj 	register int br, cvec;		/* must be r11,r10; value-result */
1232426Skre 
1242608Swnj #ifdef lint
1253105Swnj 	br = 0; cvec = br; br = cvec;
1262608Swnj #endif
1272608Swnj 	((struct device *)reg)->tmcs = TM_IE;
1282396Swnj 	/*
1292630Swnj 	 * If this is a tm11, it ought to have interrupted
1302396Swnj 	 * by now, if it isn't (ie: it is a ts04) then we just
1312458Swnj 	 * hope that it didn't interrupt, so autoconf will ignore it.
1322458Swnj 	 * Just in case, we will reference one
1332396Swnj 	 * of the more distant registers, and hope for a machine
1342458Swnj 	 * check, or similar disaster if this is a ts.
1352471Swnj 	 *
1362471Swnj 	 * Note: on an 11/780, badaddr will just generate
1372471Swnj 	 * a uba error for a ts; but our caller will notice that
1382471Swnj 	 * so we won't check for it.
1392396Swnj 	 */
1403105Swnj 	if (badaddr((caddr_t)&((struct device *)reg)->tmrd, 2))
1412458Swnj 		return (0);
1422458Swnj 	return (1);
1432396Swnj }
1442396Swnj 
1452608Swnj /*
1462608Swnj  * Due to a design flaw, we cannot ascertain if the tape
1472608Swnj  * exists or not unless it is on line - ie: unless a tape is
1482608Swnj  * mounted. This is too servere a restriction to bear,
1492608Swnj  * so all units are assumed to exist.
1502608Swnj  */
1512608Swnj /*ARGSUSED*/
1522574Swnj tmslave(ui, reg)
1532982Swnj 	struct uba_device *ui;
1542396Swnj 	caddr_t reg;
1552396Swnj {
1562458Swnj 
1572458Swnj 	return (1);
1582396Swnj }
1592396Swnj 
1602608Swnj /*
1613095Swnj  * Record attachment of the unit to the controller.
1622608Swnj  */
1632608Swnj /*ARGSUSED*/
1642608Swnj tmattach(ui)
1652982Swnj 	struct uba_device *ui;
1662608Swnj {
1672608Swnj 
1683095Swnj 	/*
1693095Swnj 	 * Tetotm is used in TMUNIT to index the ctmbuf and rtmbuf
1703095Swnj 	 * arrays given a te unit number.
1713095Swnj 	 */
1723095Swnj 	tetotm[ui->ui_unit] = ui->ui_mi->um_ctlr;
1732608Swnj }
1742608Swnj 
1752608Swnj /*
1762608Swnj  * Open the device.  Tapes are unique open
1772608Swnj  * devices, so we refuse if it is already open.
1782608Swnj  * We also check that a tape is available, and
1793095Swnj  * don't block waiting here; if you want to wait
1803095Swnj  * for a tape you should timeout in user code.
1812608Swnj  */
1821919Swnj tmopen(dev, flag)
1831919Swnj 	dev_t dev;
1841919Swnj 	int flag;
1851919Swnj {
1863095Swnj 	register int teunit;
1872982Swnj 	register struct uba_device *ui;
1883095Swnj 	register struct te_softc *sc;
1893209Swnj 	int olddens, dens;
1901919Swnj 
1913095Swnj 	teunit = TEUNIT(dev);
1923095Swnj 	if (teunit>=NTE || (sc = &te_softc[teunit])->sc_openf ||
1933095Swnj 	    (ui = tedinfo[teunit]) == 0 || ui->ui_alive == 0) {
1942608Swnj 		u.u_error = ENXIO;
1951919Swnj 		return;
1961919Swnj 	}
1973209Swnj 	olddens = sc->sc_dens;
1983209Swnj 	dens = TM_IE | TM_GO | (ui->ui_slave << 8);
1993209Swnj 	if ((minor(dev) & T_1600BPI) == 0)
2003209Swnj 		dens |= TM_D800;
2013209Swnj 	sc->sc_dens = dens;
2023141Swnj get:
2032608Swnj 	tmcommand(dev, TM_SENSE, 1);
2043141Swnj 	if (sc->sc_erreg&TMER_SDWN) {
2053141Swnj 		sleep((caddr_t)&lbolt, PZERO+1);
2063141Swnj 		goto get;
2073141Swnj 	}
2083209Swnj 	sc->sc_dens = olddens;
2093095Swnj 	if ((sc->sc_erreg&(TMER_SELR|TMER_TUR)) != (TMER_SELR|TMER_TUR) ||
2103174Swnj 	    (flag&FWRITE) && (sc->sc_erreg&TMER_WRL) ||
2113095Swnj 	    (sc->sc_erreg&TMER_BOT) == 0 && (flag&FWRITE) &&
2123174Swnj 		dens != sc->sc_dens) {
2133095Swnj 		/*
2143095Swnj 		 * Not online or density switch in mid-tape or write locked.
2153095Swnj 		 */
2162471Swnj 		u.u_error = EIO;
2172608Swnj 		return;
2181919Swnj 	}
2192608Swnj 	sc->sc_openf = 1;
2202471Swnj 	sc->sc_blkno = (daddr_t)0;
2212471Swnj 	sc->sc_nxrec = INF;
2222608Swnj 	sc->sc_lastiow = 0;
2233095Swnj 	sc->sc_dens = dens;
2241919Swnj }
2251919Swnj 
2262608Swnj /*
2272608Swnj  * Close tape device.
2282608Swnj  *
2292608Swnj  * If tape was open for writing or last operation was
2302608Swnj  * a write, then write two EOF's and backspace over the last one.
2312608Swnj  * Unless this is a non-rewinding special file, rewind the tape.
2322608Swnj  * Make the tape available to others.
2332608Swnj  */
2341919Swnj tmclose(dev, flag)
2351919Swnj 	register dev_t dev;
2361919Swnj 	register flag;
2371919Swnj {
2383095Swnj 	register struct te_softc *sc = &te_softc[TEUNIT(dev)];
2391919Swnj 
2402608Swnj 	if (flag == FWRITE || (flag&FWRITE) && sc->sc_lastiow) {
2412608Swnj 		tmcommand(dev, TM_WEOF, 1);
2422608Swnj 		tmcommand(dev, TM_WEOF, 1);
2432608Swnj 		tmcommand(dev, TM_SREV, 1);
2441919Swnj 	}
2451919Swnj 	if ((minor(dev)&T_NOREWIND) == 0)
2463095Swnj 		/*
2473095Swnj 		 * 0 count means don't hang waiting for rewind complete
2483095Swnj 		 * rather ctmbuf stays busy until the operation completes
2493095Swnj 		 * preventing further opens from completing by
2503095Swnj 		 * preventing a TM_SENSE from completing.
2513095Swnj 		 */
2523095Swnj 		tmcommand(dev, TM_REW, 0);
2532471Swnj 	sc->sc_openf = 0;
2541919Swnj }
2551919Swnj 
2562608Swnj /*
2572608Swnj  * Execute a command on the tape drive
2582608Swnj  * a specified number of times.
2592608Swnj  */
2602574Swnj tmcommand(dev, com, count)
2611919Swnj 	dev_t dev;
2621919Swnj 	int com, count;
2631919Swnj {
2641919Swnj 	register struct buf *bp;
2651919Swnj 
2662608Swnj 	bp = &ctmbuf[TMUNIT(dev)];
2671919Swnj 	(void) spl5();
2681919Swnj 	while (bp->b_flags&B_BUSY) {
2693095Swnj 		/*
2703095Swnj 		 * This special check is because B_BUSY never
2713095Swnj 		 * gets cleared in the non-waiting rewind case.
2723095Swnj 		 */
2733141Swnj 		if (bp->b_repcnt == 0 && (bp->b_flags&B_DONE))
2743095Swnj 			break;
2751919Swnj 		bp->b_flags |= B_WANTED;
2761919Swnj 		sleep((caddr_t)bp, PRIBIO);
2771919Swnj 	}
2781919Swnj 	bp->b_flags = B_BUSY|B_READ;
2791919Swnj 	(void) spl0();
2801919Swnj 	bp->b_dev = dev;
2811919Swnj 	bp->b_repcnt = -count;
2821919Swnj 	bp->b_command = com;
2831919Swnj 	bp->b_blkno = 0;
2841919Swnj 	tmstrategy(bp);
2853095Swnj 	/*
2863095Swnj 	 * In case of rewind from close, don't wait.
2873095Swnj 	 * This is the only case where count can be 0.
2883095Swnj 	 */
2893095Swnj 	if (count == 0)
2903095Swnj 		return;
2911919Swnj 	iowait(bp);
2921919Swnj 	if (bp->b_flags&B_WANTED)
2931919Swnj 		wakeup((caddr_t)bp);
2941919Swnj 	bp->b_flags &= B_ERROR;
2951919Swnj }
2961919Swnj 
2972608Swnj /*
2983095Swnj  * Queue a tape operation.
2992608Swnj  */
3001919Swnj tmstrategy(bp)
3011919Swnj 	register struct buf *bp;
3021919Swnj {
3033095Swnj 	int teunit = TEUNIT(bp->b_dev);
3042982Swnj 	register struct uba_ctlr *um;
3052608Swnj 	register struct buf *dp;
3061919Swnj 
3072608Swnj 	/*
3082608Swnj 	 * Put transfer at end of unit queue
3092608Swnj 	 */
3103095Swnj 	dp = &teutab[teunit];
3111919Swnj 	bp->av_forw = NULL;
3121919Swnj 	(void) spl5();
3132608Swnj 	if (dp->b_actf == NULL) {
3142608Swnj 		dp->b_actf = bp;
3152608Swnj 		/*
3162608Swnj 		 * Transport not already active...
3172608Swnj 		 * put at end of controller queue.
3182608Swnj 		 */
3192608Swnj 		dp->b_forw = NULL;
3203095Swnj 		um = tedinfo[teunit]->ui_mi;
3212608Swnj 		if (um->um_tab.b_actf == NULL)
3222608Swnj 			um->um_tab.b_actf = dp;
3232608Swnj 		else
3242608Swnj 			um->um_tab.b_actl->b_forw = dp;
3252608Swnj 		um->um_tab.b_actl = dp;
3262608Swnj 	} else
3272608Swnj 		dp->b_actl->av_forw = bp;
3282608Swnj 	dp->b_actl = bp;
3292608Swnj 	/*
3302608Swnj 	 * If the controller is not busy, get
3312608Swnj 	 * it going.
3322608Swnj 	 */
3332608Swnj 	if (um->um_tab.b_active == 0)
3342608Swnj 		tmstart(um);
3351919Swnj 	(void) spl0();
3361919Swnj }
3371919Swnj 
3382608Swnj /*
3392608Swnj  * Start activity on a tm controller.
3402608Swnj  */
3412608Swnj tmstart(um)
3422982Swnj 	register struct uba_ctlr *um;
3431919Swnj {
3442608Swnj 	register struct buf *bp, *dp;
3452608Swnj 	register struct device *addr = (struct device *)um->um_addr;
3463095Swnj 	register struct te_softc *sc;
3472982Swnj 	register struct uba_device *ui;
3483095Swnj 	int teunit, cmd;
3492471Swnj 	daddr_t blkno;
3501919Swnj 
3512608Swnj 	/*
3522608Swnj 	 * Look for an idle transport on the controller.
3532608Swnj 	 */
3541919Swnj loop:
3552608Swnj 	if ((dp = um->um_tab.b_actf) == NULL)
3561919Swnj 		return;
3572608Swnj 	if ((bp = dp->b_actf) == NULL) {
3582608Swnj 		um->um_tab.b_actf = dp->b_forw;
3592608Swnj 		goto loop;
3602608Swnj 	}
3613095Swnj 	teunit = TEUNIT(bp->b_dev);
3623095Swnj 	ui = tedinfo[teunit];
3632608Swnj 	/*
3642608Swnj 	 * Record pre-transfer status (e.g. for TM_SENSE)
3652608Swnj 	 */
3663095Swnj 	sc = &te_softc[teunit];
3672608Swnj 	addr = (struct device *)um->um_addr;
3682608Swnj 	addr->tmcs = (ui->ui_slave << 8);
3692471Swnj 	sc->sc_dsreg = addr->tmcs;
3702471Swnj 	sc->sc_erreg = addr->tmer;
3712471Swnj 	sc->sc_resid = addr->tmbc;
3722608Swnj 	/*
3732608Swnj 	 * Default is that last command was NOT a write command;
3742608Swnj 	 * if we do a write command we will notice this in tmintr().
3752608Swnj 	 */
3762608Swnj 	sc->sc_lastiow = 1;
3772608Swnj 	if (sc->sc_openf < 0 || (addr->tmcs&TM_CUR) == 0) {
3782608Swnj 		/*
3793095Swnj 		 * Have had a hard error on a non-raw tape
3803095Swnj 		 * or the tape unit is now unavailable
3813095Swnj 		 * (e.g. taken off line).
3822608Swnj 		 */
3832608Swnj 		bp->b_flags |= B_ERROR;
3841919Swnj 		goto next;
3851919Swnj 	}
3863095Swnj 	if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) {
3873095Swnj 		/*
3883095Swnj 		 * Execute control operation with the specified count.
3893095Swnj 		 */
3902608Swnj 		if (bp->b_command == TM_SENSE)
3912608Swnj 			goto next;
3922608Swnj 		um->um_tab.b_active =
3932608Swnj 		    bp->b_command == TM_REW ? SREW : SCOM;
3942608Swnj 		if (bp->b_command == TM_SFORW || bp->b_command == TM_SREV)
3952608Swnj 			addr->tmbc = bp->b_repcnt;
3962670Swnj 		goto dobpcmd;
3972608Swnj 	}
3982608Swnj 	/*
3993095Swnj 	 * The following checks handle boundary cases for operation
4003095Swnj 	 * on non-raw tapes.  On raw tapes the initialization of
4013095Swnj 	 * sc->sc_nxrec by tmphys causes them to be skipped normally
4023095Swnj 	 * (except in the case of retries).
4033095Swnj 	 */
4043095Swnj 	if (dbtofsb(bp->b_blkno) > sc->sc_nxrec) {
4053095Swnj 		/*
4063095Swnj 		 * Can't read past known end-of-file.
4073095Swnj 		 */
4083095Swnj 		bp->b_flags |= B_ERROR;
4093095Swnj 		bp->b_error = ENXIO;
4103095Swnj 		goto next;
4113095Swnj 	}
4123095Swnj 	if (dbtofsb(bp->b_blkno) == sc->sc_nxrec &&
4133095Swnj 	    bp->b_flags&B_READ) {
4143095Swnj 		/*
4153095Swnj 		 * Reading at end of file returns 0 bytes.
4163095Swnj 		 */
4173095Swnj 		bp->b_resid = bp->b_bcount;
4183095Swnj 		clrbuf(bp);
4193095Swnj 		goto next;
4203095Swnj 	}
4213095Swnj 	if ((bp->b_flags&B_READ) == 0)
4223095Swnj 		/*
4233095Swnj 		 * Writing sets EOF
4243095Swnj 		 */
4253095Swnj 		sc->sc_nxrec = dbtofsb(bp->b_blkno) + 1;
4263095Swnj 	/*
4272608Swnj 	 * If the data transfer command is in the correct place,
4282608Swnj 	 * set up all the registers except the csr, and give
4292608Swnj 	 * control over to the UNIBUS adapter routines, to
4302608Swnj 	 * wait for resources to start the i/o.
4312608Swnj 	 */
4322471Swnj 	if ((blkno = sc->sc_blkno) == dbtofsb(bp->b_blkno)) {
4332396Swnj 		addr->tmbc = -bp->b_bcount;
4341919Swnj 		if ((bp->b_flags&B_READ) == 0) {
4352471Swnj 			if (um->um_tab.b_errcnt)
4363095Swnj 				cmd = TM_WIRG;
4371919Swnj 			else
4383095Swnj 				cmd = TM_WCOM;
4391919Swnj 		} else
4403095Swnj 			cmd = TM_RCOM;
4412471Swnj 		um->um_tab.b_active = SIO;
4423095Swnj 		um->um_cmd = sc->sc_dens|cmd;
4432928Swnj #ifdef notdef
4442670Swnj 		if (tmreverseop(sc->sc_lastcmd))
4453095Swnj 			while (addr->tmer & TMER_SDWN)
4462670Swnj 				tmgapsdcnt++;
4472670Swnj 		sc->sc_lastcmd = TM_RCOM;		/* will serve */
4482928Swnj #endif
4493105Swnj 		(void) ubago(ui);
4501919Swnj 		return;
4511919Swnj 	}
4522608Swnj 	/*
4533095Swnj 	 * Tape positioned incorrectly;
4543095Swnj 	 * set to seek forwards or backwards to the correct spot.
4553095Swnj 	 * This happens for raw tapes only on error retries.
4562608Swnj 	 */
4572471Swnj 	um->um_tab.b_active = SSEEK;
4581919Swnj 	if (blkno < dbtofsb(bp->b_blkno)) {
4592670Swnj 		bp->b_command = TM_SFORW;
4602396Swnj 		addr->tmbc = blkno - dbtofsb(bp->b_blkno);
4611919Swnj 	} else {
4622670Swnj 		bp->b_command = TM_SREV;
4632396Swnj 		addr->tmbc = dbtofsb(bp->b_blkno) - blkno;
4641919Swnj 	}
4652670Swnj dobpcmd:
4662928Swnj #ifdef notdef
4673095Swnj 	/*
4683095Swnj 	 * It is strictly necessary to wait for the tape
4693095Swnj 	 * to stop before changing directions, but the TC11
4703095Swnj 	 * handles this for us.
4713095Swnj 	 */
4722670Swnj 	if (tmreverseop(sc->sc_lastcmd) != tmreverseop(bp->b_command))
4732670Swnj 		while (addr->tmer & TM_SDWN)
4742670Swnj 			tmgapsdcnt++;
4752670Swnj 	sc->sc_lastcmd = bp->b_command;
4762928Swnj #endif
4773095Swnj 	/*
4783095Swnj 	 * Do the command in bp.
4793095Swnj 	 */
4803095Swnj 	addr->tmcs = (sc->sc_dens | bp->b_command);
4811919Swnj 	return;
4821919Swnj 
4831919Swnj next:
4842608Swnj 	/*
4852608Swnj 	 * Done with this operation due to error or
4862608Swnj 	 * the fact that it doesn't do anything.
4872608Swnj 	 * Release UBA resources (if any), dequeue
4882608Swnj 	 * the transfer and continue processing this slave.
4892608Swnj 	 */
4902608Swnj 	if (um->um_ubinfo)
4912617Swnj 		ubadone(um);
4922608Swnj 	um->um_tab.b_errcnt = 0;
4932608Swnj 	dp->b_actf = bp->av_forw;
4941919Swnj 	iodone(bp);
4951919Swnj 	goto loop;
4961919Swnj }
4971919Swnj 
4982608Swnj /*
4992608Swnj  * The UNIBUS resources we needed have been
5002608Swnj  * allocated to us; start the device.
5012608Swnj  */
5022574Swnj tmdgo(um)
5032982Swnj 	register struct uba_ctlr *um;
5041919Swnj {
5052574Swnj 	register struct device *addr = (struct device *)um->um_addr;
5062471Swnj 
5072574Swnj 	addr->tmba = um->um_ubinfo;
5082574Swnj 	addr->tmcs = um->um_cmd | ((um->um_ubinfo >> 12) & 0x30);
5092396Swnj }
5102396Swnj 
5112608Swnj /*
5122608Swnj  * Tm interrupt routine.
5132608Swnj  */
5142471Swnj /*ARGSUSED*/
5152630Swnj tmintr(tm11)
5162630Swnj 	int tm11;
5172396Swnj {
5182608Swnj 	struct buf *dp;
5191919Swnj 	register struct buf *bp;
5202982Swnj 	register struct uba_ctlr *um = tmminfo[tm11];
5213095Swnj 	register struct device *addr;
5223095Swnj 	register struct te_softc *sc;
5233095Swnj 	int teunit;
5241919Swnj 	register state;
5251919Swnj 
5263095Swnj 	if ((dp = um->um_tab.b_actf) == NULL)
5273095Swnj 		return;
5283095Swnj 	bp = dp->b_actf;
5293095Swnj 	teunit = TEUNIT(bp->b_dev);
5303095Swnj 	addr = (struct device *)tedinfo[teunit]->ui_addr;
5312608Swnj 	/*
5322608Swnj 	 * If last command was a rewind, and tape is still
5332608Swnj 	 * rewinding, wait for the rewind complete interrupt.
5342608Swnj 	 */
5352608Swnj 	if (um->um_tab.b_active == SREW) {
5362608Swnj 		um->um_tab.b_active = SCOM;
5373095Swnj 		if (addr->tmer&TMER_RWS)
5382608Swnj 			return;
5391919Swnj 	}
5402608Swnj 	/*
5412608Swnj 	 * An operation completed... record status
5422608Swnj 	 */
5433095Swnj 	sc = &te_softc[teunit];
5442471Swnj 	sc->sc_dsreg = addr->tmcs;
5452471Swnj 	sc->sc_erreg = addr->tmer;
5462471Swnj 	sc->sc_resid = addr->tmbc;
5471919Swnj 	if ((bp->b_flags & B_READ) == 0)
5482608Swnj 		sc->sc_lastiow = 1;
5492471Swnj 	state = um->um_tab.b_active;
5502471Swnj 	um->um_tab.b_active = 0;
5512608Swnj 	/*
5522608Swnj 	 * Check for errors.
5532608Swnj 	 */
5542608Swnj 	if (addr->tmcs&TM_ERR) {
5553095Swnj 		while (addr->tmer & TMER_SDWN)
5561919Swnj 			;			/* await settle down */
5572608Swnj 		/*
5583095Swnj 		 * If we hit the end of the tape file, update our position.
5592608Swnj 		 */
5603095Swnj 		if (addr->tmer&TMER_EOF) {
5612608Swnj 			tmseteof(bp);		/* set blkno and nxrec */
5622608Swnj 			state = SCOM;		/* force completion */
5632608Swnj 			/*
5642608Swnj 			 * Stuff bc so it will be unstuffed correctly
5652608Swnj 			 * later to get resid.
5662608Swnj 			 */
5672396Swnj 			addr->tmbc = -bp->b_bcount;
5682608Swnj 			goto opdone;
5691919Swnj 		}
5702608Swnj 		/*
5713095Swnj 		 * If we were reading raw tape and the only error was that the
5723095Swnj 		 * record was too long, then we don't consider this an error.
5732608Swnj 		 */
5743095Swnj 		if (bp == &rtmbuf[TMUNIT(bp->b_dev)] && (bp->b_flags&B_READ) &&
5753095Swnj 		    (addr->tmer&(TMER_HARD|TMER_SOFT)) == TMER_RLE)
5762608Swnj 			goto ignoreerr;
5772608Swnj 		/*
5782608Swnj 		 * If error is not hard, and this was an i/o operation
5792608Swnj 		 * retry up to 8 times.
5802608Swnj 		 */
5813095Swnj 		if ((addr->tmer&TMER_HARD)==0 && state==SIO) {
5822471Swnj 			if (++um->um_tab.b_errcnt < 7) {
5832471Swnj 				sc->sc_blkno++;
5842617Swnj 				ubadone(um);
5852608Swnj 				goto opcont;
5861919Swnj 			}
5872608Swnj 		} else
5882608Swnj 			/*
5892608Swnj 			 * Hard or non-i/o errors on non-raw tape
5902608Swnj 			 * cause it to close.
5912608Swnj 			 */
5923095Swnj 			if (sc->sc_openf>0 && bp != &rtmbuf[TMUNIT(bp->b_dev)])
5932608Swnj 				sc->sc_openf = -1;
5942608Swnj 		/*
5952608Swnj 		 * Couldn't recover error
5962608Swnj 		 */
5972928Swnj 		printf("te%d: hard error bn%d er=%b\n", minor(bp->b_dev)&03,
5983095Swnj 		    bp->b_blkno, sc->sc_erreg, TMER_BITS);
5991919Swnj 		bp->b_flags |= B_ERROR;
6002608Swnj 		goto opdone;
6011919Swnj 	}
6022608Swnj 	/*
6032608Swnj 	 * Advance tape control FSM.
6042608Swnj 	 */
6052608Swnj ignoreerr:
6061919Swnj 	switch (state) {
6071919Swnj 
6081919Swnj 	case SIO:
6092608Swnj 		/*
6102608Swnj 		 * Read/write increments tape block number
6112608Swnj 		 */
6122471Swnj 		sc->sc_blkno++;
6132608Swnj 		goto opdone;
6141919Swnj 
6151919Swnj 	case SCOM:
6162608Swnj 		/*
6173095Swnj 		 * For forward/backward space record update current position.
6182608Swnj 		 */
6193095Swnj 		if (bp == &ctmbuf[TMUNIT(bp->b_dev)])
6202608Swnj 		switch (bp->b_command) {
6211919Swnj 
6222608Swnj 		case TM_SFORW:
6232608Swnj 			sc->sc_blkno -= bp->b_repcnt;
6243095Swnj 			break;
6251919Swnj 
6262608Swnj 		case TM_SREV:
6272608Swnj 			sc->sc_blkno += bp->b_repcnt;
6283095Swnj 			break;
6291919Swnj 		}
6303095Swnj 		goto opdone;
6311919Swnj 
6321919Swnj 	case SSEEK:
6332471Swnj 		sc->sc_blkno = dbtofsb(bp->b_blkno);
6342608Swnj 		goto opcont;
6351919Swnj 
6361919Swnj 	default:
6372608Swnj 		panic("tmintr");
6382608Swnj 	}
6392608Swnj opdone:
6402608Swnj 	/*
6412608Swnj 	 * Reset error count and remove
6422608Swnj 	 * from device queue.
6432608Swnj 	 */
6442608Swnj 	um->um_tab.b_errcnt = 0;
6452608Swnj 	dp->b_actf = bp->av_forw;
6462608Swnj 	bp->b_resid = -addr->tmbc;
6472617Swnj 	ubadone(um);
6482608Swnj 	iodone(bp);
6492608Swnj 	/*
6502608Swnj 	 * Circulate slave to end of controller
6512608Swnj 	 * queue to give other slaves a chance.
6522608Swnj 	 */
6532608Swnj 	um->um_tab.b_actf = dp->b_forw;
6542608Swnj 	if (dp->b_actf) {
6552608Swnj 		dp->b_forw = NULL;
6562608Swnj 		if (um->um_tab.b_actf == NULL)
6572608Swnj 			um->um_tab.b_actf = dp;
6582608Swnj 		else
6592608Swnj 			um->um_tab.b_actl->b_forw = dp;
6602608Swnj 		um->um_tab.b_actl = dp;
6612608Swnj 	}
6622608Swnj 	if (um->um_tab.b_actf == 0)
6631919Swnj 		return;
6642608Swnj opcont:
6652608Swnj 	tmstart(um);
6661919Swnj }
6671919Swnj 
6681919Swnj tmseteof(bp)
6691919Swnj 	register struct buf *bp;
6701919Swnj {
6713095Swnj 	register int teunit = TEUNIT(bp->b_dev);
6722396Swnj 	register struct device *addr =
6733095Swnj 	    (struct device *)tedinfo[teunit]->ui_addr;
6743095Swnj 	register struct te_softc *sc = &te_softc[teunit];
6751919Swnj 
6763095Swnj 	if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) {
6772471Swnj 		if (sc->sc_blkno > dbtofsb(bp->b_blkno)) {
6781919Swnj 			/* reversing */
6792471Swnj 			sc->sc_nxrec = dbtofsb(bp->b_blkno) - addr->tmbc;
6802471Swnj 			sc->sc_blkno = sc->sc_nxrec;
6811919Swnj 		} else {
6821919Swnj 			/* spacing forward */
6832471Swnj 			sc->sc_blkno = dbtofsb(bp->b_blkno) + addr->tmbc;
6842471Swnj 			sc->sc_nxrec = sc->sc_blkno - 1;
6851919Swnj 		}
6861919Swnj 		return;
6871919Swnj 	}
6881919Swnj 	/* eof on read */
6892471Swnj 	sc->sc_nxrec = dbtofsb(bp->b_blkno);
6901919Swnj }
6911919Swnj 
6921919Swnj tmread(dev)
6932608Swnj 	dev_t dev;
6941919Swnj {
6951919Swnj 
6961919Swnj 	tmphys(dev);
6972982Swnj 	if (u.u_error)
6982982Swnj 		return;
6992608Swnj 	physio(tmstrategy, &rtmbuf[TMUNIT(dev)], dev, B_READ, minphys);
7001919Swnj }
7011919Swnj 
7021919Swnj tmwrite(dev)
7032608Swnj 	dev_t dev;
7041919Swnj {
7051919Swnj 
7061919Swnj 	tmphys(dev);
7072982Swnj 	if (u.u_error)
7082982Swnj 		return;
7092608Swnj 	physio(tmstrategy, &rtmbuf[TMUNIT(dev)], dev, B_WRITE, minphys);
7101919Swnj }
7111919Swnj 
7123095Swnj /*
7133095Swnj  * Check that a raw device exists.
7143095Swnj  * If it does, set up sc_blkno and sc_nxrec
7153095Swnj  * so that the tape will appear positioned correctly.
7163095Swnj  */
7171919Swnj tmphys(dev)
7182608Swnj 	dev_t dev;
7191919Swnj {
7203095Swnj 	register int teunit = TEUNIT(dev);
7211919Swnj 	register daddr_t a;
7223095Swnj 	register struct te_softc *sc;
7233095Swnj 	register struct uba_device *ui;
7241919Swnj 
7253095Swnj 	if (teunit >= NTE || (ui=tedinfo[teunit]) == 0 || ui->ui_alive == 0) {
7262982Swnj 		u.u_error = ENXIO;
7272982Swnj 		return;
7282982Swnj 	}
7293095Swnj 	sc = &te_softc[teunit];
7301919Swnj 	a = dbtofsb(u.u_offset >> 9);
7312471Swnj 	sc->sc_blkno = a;
7322471Swnj 	sc->sc_nxrec = a + 1;
7331919Swnj }
7341919Swnj 
7352608Swnj tmreset(uban)
7362608Swnj 	int uban;
7372608Swnj {
7382982Swnj 	register struct uba_ctlr *um;
7393095Swnj 	register tm11, teunit;
7402982Swnj 	register struct uba_device *ui;
7412608Swnj 	register struct buf *dp;
7422608Swnj 
7432630Swnj 	for (tm11 = 0; tm11 < NTM; tm11++) {
7442630Swnj 		if ((um = tmminfo[tm11]) == 0 || um->um_alive == 0 ||
7452608Swnj 		   um->um_ubanum != uban)
7462608Swnj 			continue;
7472928Swnj 		printf(" tm%d", tm11);
7482608Swnj 		um->um_tab.b_active = 0;
7492608Swnj 		um->um_tab.b_actf = um->um_tab.b_actl = 0;
7502608Swnj 		if (um->um_ubinfo) {
7512608Swnj 			printf("<%d>", (um->um_ubinfo>>28)&0xf);
7522617Swnj 			ubadone(um);
7532608Swnj 		}
7542608Swnj 		((struct device *)(um->um_addr))->tmcs = TM_DCLR;
7553095Swnj 		for (teunit = 0; teunit < NTE; teunit++) {
7563095Swnj 			if ((ui = tedinfo[teunit]) == 0 || ui->ui_mi != um ||
7573095Swnj 			    ui->ui_alive == 0)
7582608Swnj 				continue;
7593095Swnj 			dp = &teutab[teunit];
7602608Swnj 			dp->b_active = 0;
7612608Swnj 			dp->b_forw = 0;
7622608Swnj 			if (um->um_tab.b_actf == NULL)
7632608Swnj 				um->um_tab.b_actf = dp;
7642608Swnj 			else
7652608Swnj 				um->um_tab.b_actl->b_forw = dp;
7662608Swnj 			um->um_tab.b_actl = dp;
7673095Swnj 			te_softc[teunit].sc_openf = -1;
7682608Swnj 		}
7692608Swnj 		tmstart(um);
7702608Swnj 	}
7712608Swnj }
7722608Swnj 
7731919Swnj /*ARGSUSED*/
7741919Swnj tmioctl(dev, cmd, addr, flag)
7751919Swnj 	caddr_t addr;
7761919Swnj 	dev_t dev;
7771919Swnj {
7783095Swnj 	int teunit = TEUNIT(dev);
7793095Swnj 	register struct te_softc *sc = &te_softc[teunit];
7803095Swnj 	register struct buf *bp = &ctmbuf[TMUNIT(dev)];
7811919Swnj 	register callcount;
7821919Swnj 	int fcount;
7831919Swnj 	struct mtop mtop;
7841919Swnj 	struct mtget mtget;
7851919Swnj 	/* we depend of the values and order of the MT codes here */
7862608Swnj 	static tmops[] =
7872608Swnj 	   {TM_WEOF,TM_SFORW,TM_SREV,TM_SFORW,TM_SREV,TM_REW,TM_OFFL,TM_SENSE};
7881919Swnj 
7892608Swnj 	switch (cmd) {
7901919Swnj 		case MTIOCTOP:	/* tape operation */
7911919Swnj 		if (copyin((caddr_t)addr, (caddr_t)&mtop, sizeof(mtop))) {
7921919Swnj 			u.u_error = EFAULT;
7931919Swnj 			return;
7941919Swnj 		}
7951919Swnj 		switch(mtop.mt_op) {
7962608Swnj 		case MTWEOF:
7971919Swnj 			callcount = mtop.mt_count;
7982608Swnj 			fcount = 1;
7992608Swnj 			break;
8002608Swnj 		case MTFSF: case MTBSF:
8012608Swnj 			callcount = mtop.mt_count;
8021919Swnj 			fcount = INF;
8031919Swnj 			break;
8041919Swnj 		case MTFSR: case MTBSR:
8051919Swnj 			callcount = 1;
8061919Swnj 			fcount = mtop.mt_count;
8071919Swnj 			break;
8082324Skre 		case MTREW: case MTOFFL: case MTNOP:
8091919Swnj 			callcount = 1;
8101919Swnj 			fcount = 1;
8111919Swnj 			break;
8121919Swnj 		default:
8131919Swnj 			u.u_error = ENXIO;
8141919Swnj 			return;
8151919Swnj 		}
8162608Swnj 		if (callcount <= 0 || fcount <= 0) {
8171919Swnj 			u.u_error = ENXIO;
8182608Swnj 			return;
8192608Swnj 		}
8202608Swnj 		while (--callcount >= 0) {
8212574Swnj 			tmcommand(dev, tmops[mtop.mt_op], fcount);
8221919Swnj 			if ((mtop.mt_op == MTFSR || mtop.mt_op == MTBSR) &&
8232608Swnj 			    bp->b_resid) {
8241919Swnj 				u.u_error = EIO;
8251919Swnj 				break;
8261919Swnj 			}
8273095Swnj 			if ((bp->b_flags&B_ERROR) || sc->sc_erreg&TMER_BOT)
8281919Swnj 				break;
8291919Swnj 		}
8302608Swnj 		geterror(bp);
8311919Swnj 		return;
8321919Swnj 	case MTIOCGET:
8332471Swnj 		mtget.mt_dsreg = sc->sc_dsreg;
8342471Swnj 		mtget.mt_erreg = sc->sc_erreg;
8352471Swnj 		mtget.mt_resid = sc->sc_resid;
8361919Swnj 		if (copyout((caddr_t)&mtget, addr, sizeof(mtget)))
8371919Swnj 			u.u_error = EFAULT;
8381919Swnj 		return;
8391919Swnj 	default:
8401919Swnj 		u.u_error = ENXIO;
8411919Swnj 	}
8421919Swnj }
8431919Swnj 
8441919Swnj #define	DBSIZE	20
8451919Swnj 
8462363Swnj tmdump()
8472363Swnj {
8482982Swnj 	register struct uba_device *ui;
8492396Swnj 	register struct uba_regs *up;
8502396Swnj 	register struct device *addr;
8512426Skre 	int blk, num;
8522426Skre 	int start;
8531919Swnj 
8542426Skre 	start = 0;
8552426Skre 	num = maxfree;
8562426Skre #define	phys(a,b)	((b)((int)(a)&0x7fffffff))
8573095Swnj 	if (tedinfo[0] == 0)
8582887Swnj 		return (ENXIO);
8593095Swnj 	ui = phys(tedinfo[0], struct uba_device *);
8602396Swnj 	up = phys(ui->ui_hd, struct uba_hd *)->uh_physuba;
861*3331Swnj 	ubainit(up);
8622324Skre 	DELAY(1000000);
8632396Swnj 	addr = (struct device *)ui->ui_physaddr;
8642396Swnj 	tmwait(addr);
8652608Swnj 	addr->tmcs = TM_DCLR | TM_GO;
8661919Swnj 	while (num > 0) {
8671919Swnj 		blk = num > DBSIZE ? DBSIZE : num;
8682396Swnj 		tmdwrite(start, blk, addr, up);
8691919Swnj 		start += blk;
8701919Swnj 		num -= blk;
8711919Swnj 	}
8722426Skre 	tmeof(addr);
8732426Skre 	tmeof(addr);
8742426Skre 	tmwait(addr);
8752887Swnj 	if (addr->tmcs&TM_ERR)
8762887Swnj 		return (EIO);
8772608Swnj 	addr->tmcs = TM_REW | TM_GO;
8782471Swnj 	tmwait(addr);
8792363Swnj 	return (0);
8801919Swnj }
8811919Swnj 
8822608Swnj tmdwrite(dbuf, num, addr, up)
8832608Swnj 	register dbuf, num;
8842396Swnj 	register struct device *addr;
8852396Swnj 	struct uba_regs *up;
8861919Swnj {
8872396Swnj 	register struct pte *io;
8882396Swnj 	register int npf;
8891928Swnj 
8902396Swnj 	tmwait(addr);
8912396Swnj 	io = up->uba_map;
8921919Swnj 	npf = num+1;
8931928Swnj 	while (--npf != 0)
8942982Swnj 		 *(int *)io++ = (dbuf++ | (1<<UBAMR_DPSHIFT) | UBAMR_MRV);
8952396Swnj 	*(int *)io = 0;
8962396Swnj 	addr->tmbc = -(num*NBPG);
8972396Swnj 	addr->tmba = 0;
8982608Swnj 	addr->tmcs = TM_WCOM | TM_GO;
8991919Swnj }
9001919Swnj 
9012396Swnj tmwait(addr)
9022396Swnj 	register struct device *addr;
9031919Swnj {
9041928Swnj 	register s;
9051919Swnj 
9061919Swnj 	do
9072396Swnj 		s = addr->tmcs;
9082608Swnj 	while ((s & TM_CUR) == 0);
9091919Swnj }
9101919Swnj 
9112396Swnj tmeof(addr)
9122396Swnj 	struct device *addr;
9131919Swnj {
9141919Swnj 
9152396Swnj 	tmwait(addr);
9162608Swnj 	addr->tmcs = TM_WEOF | TM_GO;
9171919Swnj }
9181919Swnj #endif
919