xref: /csrg-svn/sys/vax/uba/tm.c (revision 23344)
1*23344Smckusick /*
2*23344Smckusick  * Copyright (c) 1982 Regents of the University of California.
3*23344Smckusick  * All rights reserved.  The Berkeley software License Agreement
4*23344Smckusick  * specifies the terms and conditions for redistribution.
5*23344Smckusick  *
6*23344Smckusick  *	@(#)tm.c	6.6 (Berkeley) 06/08/85
7*23344Smckusick  */
81919Swnj 
92709Swnj #include "te.h"
103519Sroot #include "ts.h"
116343Swnj #if NTE > 0
121919Swnj /*
132630Swnj  * TM11/TE10 tape driver
142471Swnj  *
153095Swnj  * TODO:
163095Swnj  *	test driver with more than one slave
173095Swnj  *	test driver with more than one controller
183095Swnj  *	test reset code
193095Swnj  *	what happens if you offline tape during rewind?
203095Swnj  *	test using file system on tape
211919Swnj  */
229778Ssam #include "../machine/pte.h"
239778Ssam 
2417079Sbloom #include "param.h"
2517079Sbloom #include "systm.h"
2617079Sbloom #include "buf.h"
2717079Sbloom #include "dir.h"
2817079Sbloom #include "conf.h"
2917079Sbloom #include "user.h"
3017079Sbloom #include "file.h"
3117079Sbloom #include "map.h"
3217079Sbloom #include "vm.h"
3317079Sbloom #include "ioctl.h"
3417079Sbloom #include "mtio.h"
3517079Sbloom #include "cmap.h"
3617079Sbloom #include "uio.h"
3717079Sbloom #include "kernel.h"
3818321Sralph #include "tty.h"
391919Swnj 
408479Sroot #include "../vax/cpu.h"
4117079Sbloom #include "ubareg.h"
4217079Sbloom #include "ubavar.h"
4317079Sbloom #include "tmreg.h"
441919Swnj 
453095Swnj /*
463095Swnj  * There is a ctmbuf per tape controller.
473095Swnj  * It is used as the token to pass to the internal routines
483095Swnj  * to execute tape ioctls, and also acts as a lock on the slaves
493095Swnj  * on the controller, since there is only one per controller.
503095Swnj  * In particular, when the tape is rewinding on close we release
513095Swnj  * the user process but any further attempts to use the tape drive
523095Swnj  * before the rewind completes will hang waiting for ctmbuf.
533095Swnj  */
543095Swnj struct	buf	ctmbuf[NTM];
551919Swnj 
563095Swnj /*
573095Swnj  * Raw tape operations use rtmbuf.  The driver
583095Swnj  * notices when rtmbuf is being used and allows the user
593095Swnj  * program to continue after errors and read records
603095Swnj  * not of the standard length (BSIZE).
613095Swnj  */
623095Swnj struct	buf	rtmbuf[NTM];
633095Swnj 
643095Swnj /*
653095Swnj  * Driver unibus interface routines and variables.
663095Swnj  */
672608Swnj int	tmprobe(), tmslave(), tmattach(), tmdgo(), tmintr();
682982Swnj struct	uba_ctlr *tmminfo[NTM];
693095Swnj struct	uba_device *tedinfo[NTE];
703095Swnj struct	buf teutab[NTE];
713095Swnj short	tetotm[NTE];
722458Swnj u_short	tmstd[] = { 0772520, 0 };
732396Swnj struct	uba_driver tmdriver =
743095Swnj  { tmprobe, tmslave, tmattach, tmdgo, tmstd, "te", tedinfo, "tm", tmminfo, 0 };
751919Swnj 
761919Swnj /* bits in minor device */
773095Swnj #define	TEUNIT(dev)	(minor(dev)&03)
783095Swnj #define	TMUNIT(dev)	(tetotm[TEUNIT(dev)])
791919Swnj #define	T_NOREWIND	04
801919Swnj #define	T_1600BPI	08
811919Swnj 
821919Swnj #define	INF	(daddr_t)1000000L
831919Swnj 
842608Swnj /*
852608Swnj  * Software state per tape transport.
863095Swnj  *
873095Swnj  * 1. A tape drive is a unique-open device; we refuse opens when it is already.
883095Swnj  * 2. We keep track of the current position on a block tape and seek
893095Swnj  *    before operations by forward/back spacing if necessary.
903095Swnj  * 3. We remember if the last operation was a write on a tape, so if a tape
913095Swnj  *    is open read write and the last thing done is a write we can
923095Swnj  *    write a standard end of tape mark (two eofs).
933095Swnj  * 4. We remember the status registers after the last command, using
943095Swnj  *    then internally and returning them to the SENSE ioctl.
953095Swnj  * 5. We remember the last density the tape was used at.  If it is
963095Swnj  *    not a BOT when we start using it and we are writing, we don't
973095Swnj  *    let the density be changed.
982608Swnj  */
993095Swnj struct	te_softc {
1002608Swnj 	char	sc_openf;	/* lock against multiple opens */
1012608Swnj 	char	sc_lastiow;	/* last op was a write */
1022608Swnj 	daddr_t	sc_blkno;	/* block number, for block device tape */
1033095Swnj 	daddr_t	sc_nxrec;	/* position of end of tape, if known */
1042608Swnj 	u_short	sc_erreg;	/* copy of last erreg */
1052608Swnj 	u_short	sc_dsreg;	/* copy of last dsreg */
1062608Swnj 	short	sc_resid;	/* copy of last bc */
1073105Swnj #ifdef unneeded
1082670Swnj 	short	sc_lastcmd;	/* last command to handle direction changes */
1092928Swnj #endif
1103095Swnj 	u_short	sc_dens;	/* prototype command with density info */
11118321Sralph 	short	sc_tact;	/* timeout is active */
1123495Sroot 	daddr_t	sc_timo;	/* time until timeout expires */
11318321Sralph 	struct	tty *sc_ttyp;	/* record user's tty for errors */
1146952Swnj } te_softc[NTE];
1153105Swnj #ifdef unneeded
1163105Swnj int	tmgapsdcnt;		/* DEBUG */
1173105Swnj #endif
1181919Swnj 
1192608Swnj /*
1203095Swnj  * States for um->um_tab.b_active, the per controller state flag.
1213095Swnj  * This is used to sequence control in the driver.
1222608Swnj  */
1231919Swnj #define	SSEEK	1		/* seeking */
1241919Swnj #define	SIO	2		/* doing seq i/o */
1251919Swnj #define	SCOM	3		/* sending control command */
1262608Swnj #define	SREW	4		/* sending a drive rewind */
1271919Swnj 
1282426Skre /*
1292426Skre  * Determine if there is a controller for
1302426Skre  * a tm at address reg.  Our goal is to make the
1312426Skre  * device interrupt.
1322426Skre  */
1332608Swnj tmprobe(reg)
1342396Swnj 	caddr_t reg;
1352396Swnj {
1363095Swnj 	register int br, cvec;		/* must be r11,r10; value-result */
1372426Skre 
1382608Swnj #ifdef lint
1393105Swnj 	br = 0; cvec = br; br = cvec;
1404936Swnj 	tmintr(0);
1412608Swnj #endif
1425692Sroot 	((struct tmdevice *)reg)->tmcs = TM_IE;
1432396Swnj 	/*
1442630Swnj 	 * If this is a tm11, it ought to have interrupted
1452396Swnj 	 * by now, if it isn't (ie: it is a ts04) then we just
1462458Swnj 	 * hope that it didn't interrupt, so autoconf will ignore it.
1472458Swnj 	 * Just in case, we will reference one
1482396Swnj 	 * of the more distant registers, and hope for a machine
1492458Swnj 	 * check, or similar disaster if this is a ts.
1502471Swnj 	 *
1512471Swnj 	 * Note: on an 11/780, badaddr will just generate
1522471Swnj 	 * a uba error for a ts; but our caller will notice that
1532471Swnj 	 * so we won't check for it.
1542396Swnj 	 */
1555692Sroot 	if (badaddr((caddr_t)&((struct tmdevice *)reg)->tmrd, 2))
1562458Swnj 		return (0);
1577404Skre 	return (sizeof (struct tmdevice));
1582396Swnj }
1592396Swnj 
1602608Swnj /*
1612608Swnj  * Due to a design flaw, we cannot ascertain if the tape
1622608Swnj  * exists or not unless it is on line - ie: unless a tape is
1632608Swnj  * mounted. This is too servere a restriction to bear,
1642608Swnj  * so all units are assumed to exist.
1652608Swnj  */
1662608Swnj /*ARGSUSED*/
1672574Swnj tmslave(ui, reg)
1682982Swnj 	struct uba_device *ui;
1692396Swnj 	caddr_t reg;
1702396Swnj {
1712458Swnj 
1722458Swnj 	return (1);
1732396Swnj }
1742396Swnj 
1752608Swnj /*
1763095Swnj  * Record attachment of the unit to the controller.
1772608Swnj  */
1782608Swnj /*ARGSUSED*/
1792608Swnj tmattach(ui)
1802982Swnj 	struct uba_device *ui;
1812608Swnj {
1823095Swnj 	/*
1833095Swnj 	 * Tetotm is used in TMUNIT to index the ctmbuf and rtmbuf
1843095Swnj 	 * arrays given a te unit number.
1853095Swnj 	 */
1863095Swnj 	tetotm[ui->ui_unit] = ui->ui_mi->um_ctlr;
1872608Swnj }
1882608Swnj 
1893495Sroot int	tmtimer();
1902608Swnj /*
1912608Swnj  * Open the device.  Tapes are unique open
1922608Swnj  * devices, so we refuse if it is already open.
1932608Swnj  * We also check that a tape is available, and
1943095Swnj  * don't block waiting here; if you want to wait
1953095Swnj  * for a tape you should timeout in user code.
1962608Swnj  */
1971919Swnj tmopen(dev, flag)
1981919Swnj 	dev_t dev;
1991919Swnj 	int flag;
2001919Swnj {
2013095Swnj 	register int teunit;
2022982Swnj 	register struct uba_device *ui;
2033095Swnj 	register struct te_softc *sc;
2043209Swnj 	int olddens, dens;
2055437Sroot 	int s;
2061919Swnj 
2073095Swnj 	teunit = TEUNIT(dev);
2083095Swnj 	if (teunit>=NTE || (sc = &te_softc[teunit])->sc_openf ||
2098574Sroot 	    (ui = tedinfo[teunit]) == 0 || ui->ui_alive == 0)
2108574Sroot 		return (ENXIO);
2113209Swnj 	olddens = sc->sc_dens;
2123209Swnj 	dens = TM_IE | TM_GO | (ui->ui_slave << 8);
2133209Swnj 	if ((minor(dev) & T_1600BPI) == 0)
2143209Swnj 		dens |= TM_D800;
2153209Swnj 	sc->sc_dens = dens;
2163141Swnj get:
2172608Swnj 	tmcommand(dev, TM_SENSE, 1);
2183141Swnj 	if (sc->sc_erreg&TMER_SDWN) {
2193141Swnj 		sleep((caddr_t)&lbolt, PZERO+1);
2203141Swnj 		goto get;
2213141Swnj 	}
2223209Swnj 	sc->sc_dens = olddens;
2233710Sroot 	if ((sc->sc_erreg&(TMER_SELR|TMER_TUR)) != (TMER_SELR|TMER_TUR)) {
2243710Sroot 		uprintf("te%d: not online\n", teunit);
2258574Sroot 		return (EIO);
2261919Swnj 	}
2273710Sroot 	if ((flag&FWRITE) && (sc->sc_erreg&TMER_WRL)) {
2283710Sroot 		uprintf("te%d: no write ring\n", teunit);
2298574Sroot 		return (EIO);
2303710Sroot 	}
2313710Sroot 	if ((sc->sc_erreg&TMER_BOT) == 0 && (flag&FWRITE) &&
2323710Sroot 	    dens != sc->sc_dens) {
2333710Sroot 		uprintf("te%d: can't change density in mid-tape\n", teunit);
2348574Sroot 		return (EIO);
2353710Sroot 	}
2362608Swnj 	sc->sc_openf = 1;
2372471Swnj 	sc->sc_blkno = (daddr_t)0;
2382471Swnj 	sc->sc_nxrec = INF;
2392608Swnj 	sc->sc_lastiow = 0;
2403095Swnj 	sc->sc_dens = dens;
24118321Sralph 	sc->sc_ttyp = u.u_ttyp;
2425437Sroot 	s = spl6();
2433495Sroot 	if (sc->sc_tact == 0) {
2443495Sroot 		sc->sc_timo = INF;
2453495Sroot 		sc->sc_tact = 1;
2463629Sroot 		timeout(tmtimer, (caddr_t)dev, 5*hz);
2473495Sroot 	}
2485437Sroot 	splx(s);
2498574Sroot 	return (0);
2501919Swnj }
2511919Swnj 
2522608Swnj /*
2532608Swnj  * Close tape device.
2542608Swnj  *
2552608Swnj  * If tape was open for writing or last operation was
2562608Swnj  * a write, then write two EOF's and backspace over the last one.
2572608Swnj  * Unless this is a non-rewinding special file, rewind the tape.
2582608Swnj  * Make the tape available to others.
2592608Swnj  */
2601919Swnj tmclose(dev, flag)
2611919Swnj 	register dev_t dev;
2621919Swnj 	register flag;
2631919Swnj {
2643095Swnj 	register struct te_softc *sc = &te_softc[TEUNIT(dev)];
2651919Swnj 
2662608Swnj 	if (flag == FWRITE || (flag&FWRITE) && sc->sc_lastiow) {
2672608Swnj 		tmcommand(dev, TM_WEOF, 1);
2682608Swnj 		tmcommand(dev, TM_WEOF, 1);
2692608Swnj 		tmcommand(dev, TM_SREV, 1);
2701919Swnj 	}
2711919Swnj 	if ((minor(dev)&T_NOREWIND) == 0)
2723095Swnj 		/*
2733095Swnj 		 * 0 count means don't hang waiting for rewind complete
2743095Swnj 		 * rather ctmbuf stays busy until the operation completes
2753095Swnj 		 * preventing further opens from completing by
2763095Swnj 		 * preventing a TM_SENSE from completing.
2773095Swnj 		 */
2783095Swnj 		tmcommand(dev, TM_REW, 0);
2792471Swnj 	sc->sc_openf = 0;
2801919Swnj }
2811919Swnj 
2822608Swnj /*
2832608Swnj  * Execute a command on the tape drive
2842608Swnj  * a specified number of times.
2852608Swnj  */
2862574Swnj tmcommand(dev, com, count)
2871919Swnj 	dev_t dev;
2881919Swnj 	int com, count;
2891919Swnj {
2901919Swnj 	register struct buf *bp;
2915437Sroot 	register int s;
2921919Swnj 
2932608Swnj 	bp = &ctmbuf[TMUNIT(dev)];
2945437Sroot 	s = spl5();
2951919Swnj 	while (bp->b_flags&B_BUSY) {
2963095Swnj 		/*
2973095Swnj 		 * This special check is because B_BUSY never
2983095Swnj 		 * gets cleared in the non-waiting rewind case.
2993095Swnj 		 */
3003141Swnj 		if (bp->b_repcnt == 0 && (bp->b_flags&B_DONE))
3013095Swnj 			break;
3021919Swnj 		bp->b_flags |= B_WANTED;
3031919Swnj 		sleep((caddr_t)bp, PRIBIO);
3041919Swnj 	}
3051919Swnj 	bp->b_flags = B_BUSY|B_READ;
3065437Sroot 	splx(s);
3071919Swnj 	bp->b_dev = dev;
3081919Swnj 	bp->b_repcnt = -count;
3091919Swnj 	bp->b_command = com;
3101919Swnj 	bp->b_blkno = 0;
3111919Swnj 	tmstrategy(bp);
3123095Swnj 	/*
3133095Swnj 	 * In case of rewind from close, don't wait.
3143095Swnj 	 * This is the only case where count can be 0.
3153095Swnj 	 */
3163095Swnj 	if (count == 0)
3173095Swnj 		return;
3181919Swnj 	iowait(bp);
3191919Swnj 	if (bp->b_flags&B_WANTED)
3201919Swnj 		wakeup((caddr_t)bp);
3211919Swnj 	bp->b_flags &= B_ERROR;
3221919Swnj }
3231919Swnj 
3242608Swnj /*
3253095Swnj  * Queue a tape operation.
3262608Swnj  */
3271919Swnj tmstrategy(bp)
3281919Swnj 	register struct buf *bp;
3291919Swnj {
3303095Swnj 	int teunit = TEUNIT(bp->b_dev);
3312982Swnj 	register struct uba_ctlr *um;
3322608Swnj 	register struct buf *dp;
3335437Sroot 	int s;
3341919Swnj 
3352608Swnj 	/*
3362608Swnj 	 * Put transfer at end of unit queue
3372608Swnj 	 */
3383095Swnj 	dp = &teutab[teunit];
3391919Swnj 	bp->av_forw = NULL;
3405437Sroot 	s = spl5();
3413939Sbugs 	um = tedinfo[teunit]->ui_mi;
3422608Swnj 	if (dp->b_actf == NULL) {
3432608Swnj 		dp->b_actf = bp;
3442608Swnj 		/*
3452608Swnj 		 * Transport not already active...
3462608Swnj 		 * put at end of controller queue.
3472608Swnj 		 */
3482608Swnj 		dp->b_forw = NULL;
3492608Swnj 		if (um->um_tab.b_actf == NULL)
3502608Swnj 			um->um_tab.b_actf = dp;
3512608Swnj 		else
3522608Swnj 			um->um_tab.b_actl->b_forw = dp;
3532608Swnj 		um->um_tab.b_actl = dp;
3542608Swnj 	} else
3552608Swnj 		dp->b_actl->av_forw = bp;
3562608Swnj 	dp->b_actl = bp;
3572608Swnj 	/*
3582608Swnj 	 * If the controller is not busy, get
3592608Swnj 	 * it going.
3602608Swnj 	 */
3612608Swnj 	if (um->um_tab.b_active == 0)
3622608Swnj 		tmstart(um);
3635437Sroot 	splx(s);
3641919Swnj }
3651919Swnj 
3662608Swnj /*
3672608Swnj  * Start activity on a tm controller.
3682608Swnj  */
3692608Swnj tmstart(um)
3702982Swnj 	register struct uba_ctlr *um;
3711919Swnj {
3722608Swnj 	register struct buf *bp, *dp;
3735692Sroot 	register struct tmdevice *addr = (struct tmdevice *)um->um_addr;
3743095Swnj 	register struct te_softc *sc;
3752982Swnj 	register struct uba_device *ui;
3763095Swnj 	int teunit, cmd;
3772471Swnj 	daddr_t blkno;
3781919Swnj 
3792608Swnj 	/*
3802608Swnj 	 * Look for an idle transport on the controller.
3812608Swnj 	 */
3821919Swnj loop:
3832608Swnj 	if ((dp = um->um_tab.b_actf) == NULL)
3841919Swnj 		return;
3852608Swnj 	if ((bp = dp->b_actf) == NULL) {
3862608Swnj 		um->um_tab.b_actf = dp->b_forw;
3872608Swnj 		goto loop;
3882608Swnj 	}
3893095Swnj 	teunit = TEUNIT(bp->b_dev);
3903095Swnj 	ui = tedinfo[teunit];
3912608Swnj 	/*
3922608Swnj 	 * Record pre-transfer status (e.g. for TM_SENSE)
3932608Swnj 	 */
3943095Swnj 	sc = &te_softc[teunit];
3955692Sroot 	addr = (struct tmdevice *)um->um_addr;
3962608Swnj 	addr->tmcs = (ui->ui_slave << 8);
3972471Swnj 	sc->sc_dsreg = addr->tmcs;
3982471Swnj 	sc->sc_erreg = addr->tmer;
3992471Swnj 	sc->sc_resid = addr->tmbc;
4002608Swnj 	/*
4012608Swnj 	 * Default is that last command was NOT a write command;
4022608Swnj 	 * if we do a write command we will notice this in tmintr().
4032608Swnj 	 */
4043493Sroot 	sc->sc_lastiow = 0;
4052608Swnj 	if (sc->sc_openf < 0 || (addr->tmcs&TM_CUR) == 0) {
4062608Swnj 		/*
4073095Swnj 		 * Have had a hard error on a non-raw tape
4083095Swnj 		 * or the tape unit is now unavailable
4093095Swnj 		 * (e.g. taken off line).
4102608Swnj 		 */
4112608Swnj 		bp->b_flags |= B_ERROR;
4121919Swnj 		goto next;
4131919Swnj 	}
4143095Swnj 	if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) {
4153095Swnj 		/*
4163095Swnj 		 * Execute control operation with the specified count.
4173095Swnj 		 */
4182608Swnj 		if (bp->b_command == TM_SENSE)
4192608Swnj 			goto next;
4203495Sroot 		/*
4213495Sroot 		 * Set next state; give 5 minutes to complete
4223495Sroot 		 * rewind, or 10 seconds per iteration (minimum 60
4233629Sroot 		 * seconds and max 5 minutes) to complete other ops.
4243495Sroot 		 */
4253495Sroot 		if (bp->b_command == TM_REW) {
4263495Sroot 			um->um_tab.b_active = SREW;
4273495Sroot 			sc->sc_timo = 5 * 60;
4283495Sroot 		} else {
4293495Sroot 			um->um_tab.b_active = SCOM;
4304266Swnj 			sc->sc_timo =
4314266Swnj 			    imin(imax(10*(int)-bp->b_repcnt,60),5*60);
4323495Sroot 		}
4332608Swnj 		if (bp->b_command == TM_SFORW || bp->b_command == TM_SREV)
4342608Swnj 			addr->tmbc = bp->b_repcnt;
4352670Swnj 		goto dobpcmd;
4362608Swnj 	}
4372608Swnj 	/*
4383095Swnj 	 * The following checks handle boundary cases for operation
4393095Swnj 	 * on non-raw tapes.  On raw tapes the initialization of
4403095Swnj 	 * sc->sc_nxrec by tmphys causes them to be skipped normally
4413095Swnj 	 * (except in the case of retries).
4423095Swnj 	 */
4437381Ssam 	if (bdbtofsb(bp->b_blkno) > sc->sc_nxrec) {
4443095Swnj 		/*
4453095Swnj 		 * Can't read past known end-of-file.
4463095Swnj 		 */
4473095Swnj 		bp->b_flags |= B_ERROR;
4483095Swnj 		bp->b_error = ENXIO;
4493095Swnj 		goto next;
4503095Swnj 	}
4517381Ssam 	if (bdbtofsb(bp->b_blkno) == sc->sc_nxrec &&
4523095Swnj 	    bp->b_flags&B_READ) {
4533095Swnj 		/*
4543095Swnj 		 * Reading at end of file returns 0 bytes.
4553095Swnj 		 */
4563095Swnj 		bp->b_resid = bp->b_bcount;
4573095Swnj 		clrbuf(bp);
4583095Swnj 		goto next;
4593095Swnj 	}
4603095Swnj 	if ((bp->b_flags&B_READ) == 0)
4613095Swnj 		/*
4623095Swnj 		 * Writing sets EOF
4633095Swnj 		 */
4647381Ssam 		sc->sc_nxrec = bdbtofsb(bp->b_blkno) + 1;
4653095Swnj 	/*
4662608Swnj 	 * If the data transfer command is in the correct place,
4672608Swnj 	 * set up all the registers except the csr, and give
4682608Swnj 	 * control over to the UNIBUS adapter routines, to
4692608Swnj 	 * wait for resources to start the i/o.
4702608Swnj 	 */
4717381Ssam 	if ((blkno = sc->sc_blkno) == bdbtofsb(bp->b_blkno)) {
4722396Swnj 		addr->tmbc = -bp->b_bcount;
4731919Swnj 		if ((bp->b_flags&B_READ) == 0) {
4742471Swnj 			if (um->um_tab.b_errcnt)
4753095Swnj 				cmd = TM_WIRG;
4761919Swnj 			else
4773095Swnj 				cmd = TM_WCOM;
4781919Swnj 		} else
4793095Swnj 			cmd = TM_RCOM;
4802471Swnj 		um->um_tab.b_active = SIO;
4813095Swnj 		um->um_cmd = sc->sc_dens|cmd;
4822928Swnj #ifdef notdef
4832670Swnj 		if (tmreverseop(sc->sc_lastcmd))
4843095Swnj 			while (addr->tmer & TMER_SDWN)
4852670Swnj 				tmgapsdcnt++;
4862670Swnj 		sc->sc_lastcmd = TM_RCOM;		/* will serve */
4872928Swnj #endif
4883495Sroot 		sc->sc_timo = 60;	/* premature, but should serve */
4893105Swnj 		(void) ubago(ui);
4901919Swnj 		return;
4911919Swnj 	}
4922608Swnj 	/*
4933095Swnj 	 * Tape positioned incorrectly;
4943095Swnj 	 * set to seek forwards or backwards to the correct spot.
4953095Swnj 	 * This happens for raw tapes only on error retries.
4962608Swnj 	 */
4972471Swnj 	um->um_tab.b_active = SSEEK;
4987381Ssam 	if (blkno < bdbtofsb(bp->b_blkno)) {
4992670Swnj 		bp->b_command = TM_SFORW;
5007381Ssam 		addr->tmbc = blkno - bdbtofsb(bp->b_blkno);
5011919Swnj 	} else {
5022670Swnj 		bp->b_command = TM_SREV;
5037381Ssam 		addr->tmbc = bdbtofsb(bp->b_blkno) - blkno;
5041919Swnj 	}
5053629Sroot 	sc->sc_timo = imin(imax(10 * -addr->tmbc, 60), 5 * 60);
5062670Swnj dobpcmd:
5072928Swnj #ifdef notdef
5083095Swnj 	/*
5093095Swnj 	 * It is strictly necessary to wait for the tape
5103095Swnj 	 * to stop before changing directions, but the TC11
5113095Swnj 	 * handles this for us.
5123095Swnj 	 */
5132670Swnj 	if (tmreverseop(sc->sc_lastcmd) != tmreverseop(bp->b_command))
5142670Swnj 		while (addr->tmer & TM_SDWN)
5152670Swnj 			tmgapsdcnt++;
5162670Swnj 	sc->sc_lastcmd = bp->b_command;
5172928Swnj #endif
5183095Swnj 	/*
5193095Swnj 	 * Do the command in bp.
5203095Swnj 	 */
5213095Swnj 	addr->tmcs = (sc->sc_dens | bp->b_command);
5221919Swnj 	return;
5231919Swnj 
5241919Swnj next:
5252608Swnj 	/*
5262608Swnj 	 * Done with this operation due to error or
5272608Swnj 	 * the fact that it doesn't do anything.
5282608Swnj 	 * Release UBA resources (if any), dequeue
5292608Swnj 	 * the transfer and continue processing this slave.
5302608Swnj 	 */
5312608Swnj 	if (um->um_ubinfo)
5322617Swnj 		ubadone(um);
5332608Swnj 	um->um_tab.b_errcnt = 0;
5342608Swnj 	dp->b_actf = bp->av_forw;
5351919Swnj 	iodone(bp);
5361919Swnj 	goto loop;
5371919Swnj }
5381919Swnj 
5392608Swnj /*
5402608Swnj  * The UNIBUS resources we needed have been
5412608Swnj  * allocated to us; start the device.
5422608Swnj  */
5432574Swnj tmdgo(um)
5442982Swnj 	register struct uba_ctlr *um;
5451919Swnj {
5465692Sroot 	register struct tmdevice *addr = (struct tmdevice *)um->um_addr;
5472471Swnj 
5482574Swnj 	addr->tmba = um->um_ubinfo;
5492574Swnj 	addr->tmcs = um->um_cmd | ((um->um_ubinfo >> 12) & 0x30);
5502396Swnj }
5512396Swnj 
5522608Swnj /*
5532608Swnj  * Tm interrupt routine.
5542608Swnj  */
5552471Swnj /*ARGSUSED*/
5562630Swnj tmintr(tm11)
5572630Swnj 	int tm11;
5582396Swnj {
5592608Swnj 	struct buf *dp;
5601919Swnj 	register struct buf *bp;
5612982Swnj 	register struct uba_ctlr *um = tmminfo[tm11];
5625692Sroot 	register struct tmdevice *addr;
5633095Swnj 	register struct te_softc *sc;
5643095Swnj 	int teunit;
5651919Swnj 	register state;
5661919Swnj 
5673095Swnj 	if ((dp = um->um_tab.b_actf) == NULL)
5683095Swnj 		return;
5693095Swnj 	bp = dp->b_actf;
5703095Swnj 	teunit = TEUNIT(bp->b_dev);
5715692Sroot 	addr = (struct tmdevice *)tedinfo[teunit]->ui_addr;
5723524Swnj 	sc = &te_softc[teunit];
5732608Swnj 	/*
5742608Swnj 	 * If last command was a rewind, and tape is still
5752608Swnj 	 * rewinding, wait for the rewind complete interrupt.
5762608Swnj 	 */
5772608Swnj 	if (um->um_tab.b_active == SREW) {
5782608Swnj 		um->um_tab.b_active = SCOM;
5793524Swnj 		if (addr->tmer&TMER_RWS) {
5803524Swnj 			sc->sc_timo = 5*60;		/* 5 minutes */
5812608Swnj 			return;
5823524Swnj 		}
5831919Swnj 	}
5842608Swnj 	/*
5852608Swnj 	 * An operation completed... record status
5862608Swnj 	 */
5873495Sroot 	sc->sc_timo = INF;
5882471Swnj 	sc->sc_dsreg = addr->tmcs;
5892471Swnj 	sc->sc_erreg = addr->tmer;
5902471Swnj 	sc->sc_resid = addr->tmbc;
5911919Swnj 	if ((bp->b_flags & B_READ) == 0)
5922608Swnj 		sc->sc_lastiow = 1;
5932471Swnj 	state = um->um_tab.b_active;
5942471Swnj 	um->um_tab.b_active = 0;
5952608Swnj 	/*
5962608Swnj 	 * Check for errors.
5972608Swnj 	 */
5982608Swnj 	if (addr->tmcs&TM_ERR) {
5993095Swnj 		while (addr->tmer & TMER_SDWN)
6001919Swnj 			;			/* await settle down */
6012608Swnj 		/*
6023095Swnj 		 * If we hit the end of the tape file, update our position.
6032608Swnj 		 */
6043095Swnj 		if (addr->tmer&TMER_EOF) {
6052608Swnj 			tmseteof(bp);		/* set blkno and nxrec */
6062608Swnj 			state = SCOM;		/* force completion */
6072608Swnj 			/*
6082608Swnj 			 * Stuff bc so it will be unstuffed correctly
6092608Swnj 			 * later to get resid.
6102608Swnj 			 */
6112396Swnj 			addr->tmbc = -bp->b_bcount;
6122608Swnj 			goto opdone;
6131919Swnj 		}
6142608Swnj 		/*
6153095Swnj 		 * If we were reading raw tape and the only error was that the
6163095Swnj 		 * record was too long, then we don't consider this an error.
6172608Swnj 		 */
6183095Swnj 		if (bp == &rtmbuf[TMUNIT(bp->b_dev)] && (bp->b_flags&B_READ) &&
6193095Swnj 		    (addr->tmer&(TMER_HARD|TMER_SOFT)) == TMER_RLE)
6202608Swnj 			goto ignoreerr;
6212608Swnj 		/*
6222608Swnj 		 * If error is not hard, and this was an i/o operation
6232608Swnj 		 * retry up to 8 times.
6242608Swnj 		 */
6253095Swnj 		if ((addr->tmer&TMER_HARD)==0 && state==SIO) {
6262471Swnj 			if (++um->um_tab.b_errcnt < 7) {
6272471Swnj 				sc->sc_blkno++;
6282617Swnj 				ubadone(um);
6292608Swnj 				goto opcont;
6301919Swnj 			}
6312608Swnj 		} else
6322608Swnj 			/*
6332608Swnj 			 * Hard or non-i/o errors on non-raw tape
6342608Swnj 			 * cause it to close.
6352608Swnj 			 */
6363095Swnj 			if (sc->sc_openf>0 && bp != &rtmbuf[TMUNIT(bp->b_dev)])
6372608Swnj 				sc->sc_openf = -1;
6382608Swnj 		/*
6392608Swnj 		 * Couldn't recover error
6402608Swnj 		 */
64118321Sralph 		tprintf(sc->sc_ttyp,
64218321Sralph 		    "te%d: hard error bn%d er=%b\n", minor(bp->b_dev)&03,
6433095Swnj 		    bp->b_blkno, sc->sc_erreg, TMER_BITS);
6441919Swnj 		bp->b_flags |= B_ERROR;
6452608Swnj 		goto opdone;
6461919Swnj 	}
6472608Swnj 	/*
6482608Swnj 	 * Advance tape control FSM.
6492608Swnj 	 */
6502608Swnj ignoreerr:
6511919Swnj 	switch (state) {
6521919Swnj 
6531919Swnj 	case SIO:
6542608Swnj 		/*
6552608Swnj 		 * Read/write increments tape block number
6562608Swnj 		 */
6572471Swnj 		sc->sc_blkno++;
6582608Swnj 		goto opdone;
6591919Swnj 
6601919Swnj 	case SCOM:
6612608Swnj 		/*
6623095Swnj 		 * For forward/backward space record update current position.
6632608Swnj 		 */
6643095Swnj 		if (bp == &ctmbuf[TMUNIT(bp->b_dev)])
6652608Swnj 		switch (bp->b_command) {
6661919Swnj 
6672608Swnj 		case TM_SFORW:
6682608Swnj 			sc->sc_blkno -= bp->b_repcnt;
6693095Swnj 			break;
6701919Swnj 
6712608Swnj 		case TM_SREV:
6722608Swnj 			sc->sc_blkno += bp->b_repcnt;
6733095Swnj 			break;
6741919Swnj 		}
6753095Swnj 		goto opdone;
6761919Swnj 
6771919Swnj 	case SSEEK:
6787381Ssam 		sc->sc_blkno = bdbtofsb(bp->b_blkno);
6792608Swnj 		goto opcont;
6801919Swnj 
6811919Swnj 	default:
6822608Swnj 		panic("tmintr");
6832608Swnj 	}
6842608Swnj opdone:
6852608Swnj 	/*
6862608Swnj 	 * Reset error count and remove
6872608Swnj 	 * from device queue.
6882608Swnj 	 */
6892608Swnj 	um->um_tab.b_errcnt = 0;
6902608Swnj 	dp->b_actf = bp->av_forw;
69114929Skarels 	/*
69214929Skarels 	 * Check resid; watch out for resid >32767 (tmbc not negative).
69314929Skarels 	 */
69415081Skarels 	bp->b_resid = ((int) -addr->tmbc) & 0xffff;
6952617Swnj 	ubadone(um);
6962608Swnj 	iodone(bp);
6972608Swnj 	/*
6982608Swnj 	 * Circulate slave to end of controller
6992608Swnj 	 * queue to give other slaves a chance.
7002608Swnj 	 */
7012608Swnj 	um->um_tab.b_actf = dp->b_forw;
7022608Swnj 	if (dp->b_actf) {
7032608Swnj 		dp->b_forw = NULL;
7042608Swnj 		if (um->um_tab.b_actf == NULL)
7052608Swnj 			um->um_tab.b_actf = dp;
7062608Swnj 		else
7072608Swnj 			um->um_tab.b_actl->b_forw = dp;
7082608Swnj 		um->um_tab.b_actl = dp;
7092608Swnj 	}
7102608Swnj 	if (um->um_tab.b_actf == 0)
7111919Swnj 		return;
7122608Swnj opcont:
7132608Swnj 	tmstart(um);
7141919Swnj }
7151919Swnj 
7163495Sroot tmtimer(dev)
7173495Sroot 	int dev;
7183495Sroot {
7193495Sroot 	register struct te_softc *sc = &te_softc[TEUNIT(dev)];
7204847Sroot 	register short x;
7213495Sroot 
7223495Sroot 	if (sc->sc_timo != INF && (sc->sc_timo -= 5) < 0) {
7234278Sroot 		printf("te%d: lost interrupt\n", TEUNIT(dev));
7243495Sroot 		sc->sc_timo = INF;
7254847Sroot 		x = spl5();
7263495Sroot 		tmintr(TMUNIT(dev));
7274847Sroot 		(void) splx(x);
7283495Sroot 	}
7293629Sroot 	timeout(tmtimer, (caddr_t)dev, 5*hz);
7303495Sroot }
7313495Sroot 
7321919Swnj tmseteof(bp)
7331919Swnj 	register struct buf *bp;
7341919Swnj {
7353095Swnj 	register int teunit = TEUNIT(bp->b_dev);
7365692Sroot 	register struct tmdevice *addr =
7375692Sroot 	    (struct tmdevice *)tedinfo[teunit]->ui_addr;
7383095Swnj 	register struct te_softc *sc = &te_softc[teunit];
7391919Swnj 
7403095Swnj 	if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) {
7417381Ssam 		if (sc->sc_blkno > bdbtofsb(bp->b_blkno)) {
7421919Swnj 			/* reversing */
7437381Ssam 			sc->sc_nxrec = bdbtofsb(bp->b_blkno) - addr->tmbc;
7442471Swnj 			sc->sc_blkno = sc->sc_nxrec;
7451919Swnj 		} else {
7461919Swnj 			/* spacing forward */
7477381Ssam 			sc->sc_blkno = bdbtofsb(bp->b_blkno) + addr->tmbc;
7482471Swnj 			sc->sc_nxrec = sc->sc_blkno - 1;
7491919Swnj 		}
7501919Swnj 		return;
7511919Swnj 	}
7521919Swnj 	/* eof on read */
7537381Ssam 	sc->sc_nxrec = bdbtofsb(bp->b_blkno);
7541919Swnj }
7551919Swnj 
7567732Sroot tmread(dev, uio)
7572608Swnj 	dev_t dev;
7587732Sroot 	struct uio *uio;
7591919Swnj {
7608163Sroot 	int errno;
7611919Swnj 
7628163Sroot 	errno = tmphys(dev, uio);
7638163Sroot 	if (errno)
7648163Sroot 		return (errno);
7658163Sroot 	return (physio(tmstrategy, &rtmbuf[TMUNIT(dev)], dev, B_READ, minphys, uio));
7661919Swnj }
7671919Swnj 
7687838Sroot tmwrite(dev, uio)
7692608Swnj 	dev_t dev;
7707838Sroot 	struct uio *uio;
7711919Swnj {
7728163Sroot 	int errno;
7731919Swnj 
7748163Sroot 	errno = tmphys(dev, uio);
7758163Sroot 	if (errno)
7768163Sroot 		return (errno);
7778163Sroot 	return (physio(tmstrategy, &rtmbuf[TMUNIT(dev)], dev, B_WRITE, minphys, uio));
7781919Swnj }
7791919Swnj 
7803095Swnj /*
7813095Swnj  * Check that a raw device exists.
7823095Swnj  * If it does, set up sc_blkno and sc_nxrec
7833095Swnj  * so that the tape will appear positioned correctly.
7843095Swnj  */
7857732Sroot tmphys(dev, uio)
7862608Swnj 	dev_t dev;
7877732Sroot 	struct uio *uio;
7881919Swnj {
7893095Swnj 	register int teunit = TEUNIT(dev);
7901919Swnj 	register daddr_t a;
7913095Swnj 	register struct te_softc *sc;
7923095Swnj 	register struct uba_device *ui;
7931919Swnj 
7947838Sroot 	if (teunit >= NTE || (ui=tedinfo[teunit]) == 0 || ui->ui_alive == 0)
7957732Sroot 		return (ENXIO);
7963095Swnj 	sc = &te_softc[teunit];
7977838Sroot 	a = bdbtofsb(uio->uio_offset >> 9);
7982471Swnj 	sc->sc_blkno = a;
7992471Swnj 	sc->sc_nxrec = a + 1;
8007732Sroot 	return (0);
8011919Swnj }
8021919Swnj 
8032608Swnj tmreset(uban)
8042608Swnj 	int uban;
8052608Swnj {
8062982Swnj 	register struct uba_ctlr *um;
8073095Swnj 	register tm11, teunit;
8082982Swnj 	register struct uba_device *ui;
8092608Swnj 	register struct buf *dp;
8102608Swnj 
8112630Swnj 	for (tm11 = 0; tm11 < NTM; tm11++) {
8122630Swnj 		if ((um = tmminfo[tm11]) == 0 || um->um_alive == 0 ||
8132608Swnj 		   um->um_ubanum != uban)
8142608Swnj 			continue;
8152928Swnj 		printf(" tm%d", tm11);
8162608Swnj 		um->um_tab.b_active = 0;
8172608Swnj 		um->um_tab.b_actf = um->um_tab.b_actl = 0;
8182608Swnj 		if (um->um_ubinfo) {
8192608Swnj 			printf("<%d>", (um->um_ubinfo>>28)&0xf);
8209355Ssam 			um->um_ubinfo = 0;
8212608Swnj 		}
8225692Sroot 		((struct tmdevice *)(um->um_addr))->tmcs = TM_DCLR;
8233095Swnj 		for (teunit = 0; teunit < NTE; teunit++) {
8243095Swnj 			if ((ui = tedinfo[teunit]) == 0 || ui->ui_mi != um ||
8253095Swnj 			    ui->ui_alive == 0)
8262608Swnj 				continue;
8273095Swnj 			dp = &teutab[teunit];
8282608Swnj 			dp->b_active = 0;
8292608Swnj 			dp->b_forw = 0;
8302608Swnj 			if (um->um_tab.b_actf == NULL)
8312608Swnj 				um->um_tab.b_actf = dp;
8322608Swnj 			else
8332608Swnj 				um->um_tab.b_actl->b_forw = dp;
8342608Swnj 			um->um_tab.b_actl = dp;
8353495Sroot 			if (te_softc[teunit].sc_openf > 0)
8363495Sroot 				te_softc[teunit].sc_openf = -1;
8372608Swnj 		}
8382608Swnj 		tmstart(um);
8392608Swnj 	}
8402608Swnj }
8412608Swnj 
8421919Swnj /*ARGSUSED*/
8437632Ssam tmioctl(dev, cmd, data, flag)
8447632Ssam 	caddr_t data;
8451919Swnj 	dev_t dev;
8461919Swnj {
8473095Swnj 	int teunit = TEUNIT(dev);
8483095Swnj 	register struct te_softc *sc = &te_softc[teunit];
8493095Swnj 	register struct buf *bp = &ctmbuf[TMUNIT(dev)];
8501919Swnj 	register callcount;
8511919Swnj 	int fcount;
8527632Ssam 	struct mtop *mtop;
8537632Ssam 	struct mtget *mtget;
8541919Swnj 	/* we depend of the values and order of the MT codes here */
8552608Swnj 	static tmops[] =
8562608Swnj 	   {TM_WEOF,TM_SFORW,TM_SREV,TM_SFORW,TM_SREV,TM_REW,TM_OFFL,TM_SENSE};
8571919Swnj 
8582608Swnj 	switch (cmd) {
8597632Ssam 
8607632Ssam 	case MTIOCTOP:	/* tape operation */
8617632Ssam 		mtop = (struct mtop *)data;
8628574Sroot 		switch (mtop->mt_op) {
8637632Ssam 
8642608Swnj 		case MTWEOF:
8657632Ssam 			callcount = mtop->mt_count;
8662608Swnj 			fcount = 1;
8672608Swnj 			break;
8687632Ssam 
8692608Swnj 		case MTFSF: case MTBSF:
8707632Ssam 			callcount = mtop->mt_count;
8711919Swnj 			fcount = INF;
8721919Swnj 			break;
8737632Ssam 
8741919Swnj 		case MTFSR: case MTBSR:
8751919Swnj 			callcount = 1;
8767632Ssam 			fcount = mtop->mt_count;
8771919Swnj 			break;
8787632Ssam 
8792324Skre 		case MTREW: case MTOFFL: case MTNOP:
8801919Swnj 			callcount = 1;
8811919Swnj 			fcount = 1;
8821919Swnj 			break;
8837632Ssam 
8841919Swnj 		default:
8858574Sroot 			return (ENXIO);
8861919Swnj 		}
8878574Sroot 		if (callcount <= 0 || fcount <= 0)
8888574Sroot 			return (EINVAL);
8892608Swnj 		while (--callcount >= 0) {
8907632Ssam 			tmcommand(dev, tmops[mtop->mt_op], fcount);
8917632Ssam 			if ((mtop->mt_op == MTFSR || mtop->mt_op == MTBSR) &&
8928574Sroot 			    bp->b_resid)
8938574Sroot 				return (EIO);
8943095Swnj 			if ((bp->b_flags&B_ERROR) || sc->sc_erreg&TMER_BOT)
8951919Swnj 				break;
8961919Swnj 		}
8978648Sroot 		return (geterror(bp));
8987632Ssam 
8991919Swnj 	case MTIOCGET:
9007632Ssam 		mtget = (struct mtget *)data;
9017632Ssam 		mtget->mt_dsreg = sc->sc_dsreg;
9027632Ssam 		mtget->mt_erreg = sc->sc_erreg;
9037632Ssam 		mtget->mt_resid = sc->sc_resid;
9047632Ssam 		mtget->mt_type = MT_ISTM;
9058574Sroot 		break;
9067632Ssam 
9071919Swnj 	default:
9088574Sroot 		return (ENXIO);
9091919Swnj 	}
9108574Sroot 	return (0);
9111919Swnj }
9121919Swnj 
9131919Swnj #define	DBSIZE	20
9141919Swnj 
9152363Swnj tmdump()
9162363Swnj {
9172982Swnj 	register struct uba_device *ui;
9182396Swnj 	register struct uba_regs *up;
9195692Sroot 	register struct tmdevice *addr;
9202426Skre 	int blk, num;
9212426Skre 	int start;
9221919Swnj 
9232426Skre 	start = 0;
9242426Skre 	num = maxfree;
9252426Skre #define	phys(a,b)	((b)((int)(a)&0x7fffffff))
9263095Swnj 	if (tedinfo[0] == 0)
9272887Swnj 		return (ENXIO);
9283095Swnj 	ui = phys(tedinfo[0], struct uba_device *);
9292396Swnj 	up = phys(ui->ui_hd, struct uba_hd *)->uh_physuba;
9303331Swnj 	ubainit(up);
9312324Skre 	DELAY(1000000);
9325692Sroot 	addr = (struct tmdevice *)ui->ui_physaddr;
9332396Swnj 	tmwait(addr);
9342608Swnj 	addr->tmcs = TM_DCLR | TM_GO;
9351919Swnj 	while (num > 0) {
9361919Swnj 		blk = num > DBSIZE ? DBSIZE : num;
9372396Swnj 		tmdwrite(start, blk, addr, up);
9381919Swnj 		start += blk;
9391919Swnj 		num -= blk;
9401919Swnj 	}
9412426Skre 	tmeof(addr);
9422426Skre 	tmeof(addr);
9432426Skre 	tmwait(addr);
9442887Swnj 	if (addr->tmcs&TM_ERR)
9452887Swnj 		return (EIO);
9462608Swnj 	addr->tmcs = TM_REW | TM_GO;
9472471Swnj 	tmwait(addr);
9482363Swnj 	return (0);
9491919Swnj }
9501919Swnj 
9512608Swnj tmdwrite(dbuf, num, addr, up)
9522608Swnj 	register dbuf, num;
9535692Sroot 	register struct tmdevice *addr;
9542396Swnj 	struct uba_regs *up;
9551919Swnj {
9562396Swnj 	register struct pte *io;
9572396Swnj 	register int npf;
9581928Swnj 
9592396Swnj 	tmwait(addr);
9602396Swnj 	io = up->uba_map;
9611919Swnj 	npf = num+1;
9621928Swnj 	while (--npf != 0)
9632982Swnj 		 *(int *)io++ = (dbuf++ | (1<<UBAMR_DPSHIFT) | UBAMR_MRV);
9642396Swnj 	*(int *)io = 0;
9652396Swnj 	addr->tmbc = -(num*NBPG);
9662396Swnj 	addr->tmba = 0;
9672608Swnj 	addr->tmcs = TM_WCOM | TM_GO;
9681919Swnj }
9691919Swnj 
9702396Swnj tmwait(addr)
9715692Sroot 	register struct tmdevice *addr;
9721919Swnj {
9731928Swnj 	register s;
9741919Swnj 
9751919Swnj 	do
9762396Swnj 		s = addr->tmcs;
9772608Swnj 	while ((s & TM_CUR) == 0);
9781919Swnj }
9791919Swnj 
9802396Swnj tmeof(addr)
9815692Sroot 	struct tmdevice *addr;
9821919Swnj {
9831919Swnj 
9842396Swnj 	tmwait(addr);
9852608Swnj 	addr->tmcs = TM_WEOF | TM_GO;
9861919Swnj }
9871919Swnj #endif
988