xref: /csrg-svn/sys/vax/uba/tm.c (revision 7732)
1*7732Sroot /*	tm.c	4.52	82/08/13	*/
21919Swnj 
32709Swnj #include "te.h"
43519Sroot #include "ts.h"
56343Swnj #if NTE > 0
61919Swnj /*
72630Swnj  * TM11/TE10 tape driver
82471Swnj  *
93095Swnj  * TODO:
103095Swnj  *	test driver with more than one slave
113095Swnj  *	test driver with more than one controller
123095Swnj  *	test reset code
133095Swnj  *	what happens if you offline tape during rewind?
143095Swnj  *	test using file system on tape
151919Swnj  */
161919Swnj #include "../h/param.h"
173141Swnj #include "../h/systm.h"
181919Swnj #include "../h/buf.h"
191919Swnj #include "../h/dir.h"
201919Swnj #include "../h/conf.h"
211919Swnj #include "../h/user.h"
221919Swnj #include "../h/file.h"
231919Swnj #include "../h/map.h"
241919Swnj #include "../h/pte.h"
252574Swnj #include "../h/vm.h"
262982Swnj #include "../h/ubareg.h"
272982Swnj #include "../h/ubavar.h"
287632Ssam #include "../h/ioctl.h"
291919Swnj #include "../h/mtio.h"
302363Swnj #include "../h/cmap.h"
312396Swnj #include "../h/cpu.h"
32*7732Sroot #include "../h/uio.h"
331919Swnj 
342396Swnj #include "../h/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 ||
1993095Swnj 	    (ui = tedinfo[teunit]) == 0 || ui->ui_alive == 0) {
2002608Swnj 		u.u_error = ENXIO;
2011919Swnj 		return;
2021919Swnj 	}
2033209Swnj 	olddens = sc->sc_dens;
2043209Swnj 	dens = TM_IE | TM_GO | (ui->ui_slave << 8);
2053209Swnj 	if ((minor(dev) & T_1600BPI) == 0)
2063209Swnj 		dens |= TM_D800;
2073209Swnj 	sc->sc_dens = dens;
2083141Swnj get:
2092608Swnj 	tmcommand(dev, TM_SENSE, 1);
2103141Swnj 	if (sc->sc_erreg&TMER_SDWN) {
2113141Swnj 		sleep((caddr_t)&lbolt, PZERO+1);
2123141Swnj 		goto get;
2133141Swnj 	}
2143209Swnj 	sc->sc_dens = olddens;
2153710Sroot 	if ((sc->sc_erreg&(TMER_SELR|TMER_TUR)) != (TMER_SELR|TMER_TUR)) {
2163710Sroot 		uprintf("te%d: not online\n", teunit);
2172471Swnj 		u.u_error = EIO;
2182608Swnj 		return;
2191919Swnj 	}
2203710Sroot 	if ((flag&FWRITE) && (sc->sc_erreg&TMER_WRL)) {
2213710Sroot 		uprintf("te%d: no write ring\n", teunit);
2223710Sroot 		u.u_error = EIO;
2233710Sroot 		return;
2243710Sroot 	}
2253710Sroot 	if ((sc->sc_erreg&TMER_BOT) == 0 && (flag&FWRITE) &&
2263710Sroot 	    dens != sc->sc_dens) {
2273710Sroot 		uprintf("te%d: can't change density in mid-tape\n", teunit);
2283710Sroot 		u.u_error = EIO;
2293710Sroot 		return;
2303710Sroot 	}
2312608Swnj 	sc->sc_openf = 1;
2322471Swnj 	sc->sc_blkno = (daddr_t)0;
2332471Swnj 	sc->sc_nxrec = INF;
2342608Swnj 	sc->sc_lastiow = 0;
2353095Swnj 	sc->sc_dens = dens;
2365437Sroot 	s = spl6();
2373495Sroot 	if (sc->sc_tact == 0) {
2383495Sroot 		sc->sc_timo = INF;
2393495Sroot 		sc->sc_tact = 1;
2403629Sroot 		timeout(tmtimer, (caddr_t)dev, 5*hz);
2413495Sroot 	}
2425437Sroot 	splx(s);
2431919Swnj }
2441919Swnj 
2452608Swnj /*
2462608Swnj  * Close tape device.
2472608Swnj  *
2482608Swnj  * If tape was open for writing or last operation was
2492608Swnj  * a write, then write two EOF's and backspace over the last one.
2502608Swnj  * Unless this is a non-rewinding special file, rewind the tape.
2512608Swnj  * Make the tape available to others.
2522608Swnj  */
2531919Swnj tmclose(dev, flag)
2541919Swnj 	register dev_t dev;
2551919Swnj 	register flag;
2561919Swnj {
2573095Swnj 	register struct te_softc *sc = &te_softc[TEUNIT(dev)];
2581919Swnj 
2592608Swnj 	if (flag == FWRITE || (flag&FWRITE) && sc->sc_lastiow) {
2602608Swnj 		tmcommand(dev, TM_WEOF, 1);
2612608Swnj 		tmcommand(dev, TM_WEOF, 1);
2622608Swnj 		tmcommand(dev, TM_SREV, 1);
2631919Swnj 	}
2641919Swnj 	if ((minor(dev)&T_NOREWIND) == 0)
2653095Swnj 		/*
2663095Swnj 		 * 0 count means don't hang waiting for rewind complete
2673095Swnj 		 * rather ctmbuf stays busy until the operation completes
2683095Swnj 		 * preventing further opens from completing by
2693095Swnj 		 * preventing a TM_SENSE from completing.
2703095Swnj 		 */
2713095Swnj 		tmcommand(dev, TM_REW, 0);
2722471Swnj 	sc->sc_openf = 0;
2731919Swnj }
2741919Swnj 
2752608Swnj /*
2762608Swnj  * Execute a command on the tape drive
2772608Swnj  * a specified number of times.
2782608Swnj  */
2792574Swnj tmcommand(dev, com, count)
2801919Swnj 	dev_t dev;
2811919Swnj 	int com, count;
2821919Swnj {
2831919Swnj 	register struct buf *bp;
2845437Sroot 	register int s;
2851919Swnj 
2862608Swnj 	bp = &ctmbuf[TMUNIT(dev)];
2875437Sroot 	s = spl5();
2881919Swnj 	while (bp->b_flags&B_BUSY) {
2893095Swnj 		/*
2903095Swnj 		 * This special check is because B_BUSY never
2913095Swnj 		 * gets cleared in the non-waiting rewind case.
2923095Swnj 		 */
2933141Swnj 		if (bp->b_repcnt == 0 && (bp->b_flags&B_DONE))
2943095Swnj 			break;
2951919Swnj 		bp->b_flags |= B_WANTED;
2961919Swnj 		sleep((caddr_t)bp, PRIBIO);
2971919Swnj 	}
2981919Swnj 	bp->b_flags = B_BUSY|B_READ;
2995437Sroot 	splx(s);
3001919Swnj 	bp->b_dev = dev;
3011919Swnj 	bp->b_repcnt = -count;
3021919Swnj 	bp->b_command = com;
3031919Swnj 	bp->b_blkno = 0;
3041919Swnj 	tmstrategy(bp);
3053095Swnj 	/*
3063095Swnj 	 * In case of rewind from close, don't wait.
3073095Swnj 	 * This is the only case where count can be 0.
3083095Swnj 	 */
3093095Swnj 	if (count == 0)
3103095Swnj 		return;
3111919Swnj 	iowait(bp);
3121919Swnj 	if (bp->b_flags&B_WANTED)
3131919Swnj 		wakeup((caddr_t)bp);
3141919Swnj 	bp->b_flags &= B_ERROR;
3151919Swnj }
3161919Swnj 
3172608Swnj /*
3183095Swnj  * Queue a tape operation.
3192608Swnj  */
3201919Swnj tmstrategy(bp)
3211919Swnj 	register struct buf *bp;
3221919Swnj {
3233095Swnj 	int teunit = TEUNIT(bp->b_dev);
3242982Swnj 	register struct uba_ctlr *um;
3252608Swnj 	register struct buf *dp;
3265437Sroot 	int s;
3271919Swnj 
3282608Swnj 	/*
3292608Swnj 	 * Put transfer at end of unit queue
3302608Swnj 	 */
3313095Swnj 	dp = &teutab[teunit];
3321919Swnj 	bp->av_forw = NULL;
3335437Sroot 	s = spl5();
3343939Sbugs 	um = tedinfo[teunit]->ui_mi;
3352608Swnj 	if (dp->b_actf == NULL) {
3362608Swnj 		dp->b_actf = bp;
3372608Swnj 		/*
3382608Swnj 		 * Transport not already active...
3392608Swnj 		 * put at end of controller queue.
3402608Swnj 		 */
3412608Swnj 		dp->b_forw = NULL;
3422608Swnj 		if (um->um_tab.b_actf == NULL)
3432608Swnj 			um->um_tab.b_actf = dp;
3442608Swnj 		else
3452608Swnj 			um->um_tab.b_actl->b_forw = dp;
3462608Swnj 		um->um_tab.b_actl = dp;
3472608Swnj 	} else
3482608Swnj 		dp->b_actl->av_forw = bp;
3492608Swnj 	dp->b_actl = bp;
3502608Swnj 	/*
3512608Swnj 	 * If the controller is not busy, get
3522608Swnj 	 * it going.
3532608Swnj 	 */
3542608Swnj 	if (um->um_tab.b_active == 0)
3552608Swnj 		tmstart(um);
3565437Sroot 	splx(s);
3571919Swnj }
3581919Swnj 
3592608Swnj /*
3602608Swnj  * Start activity on a tm controller.
3612608Swnj  */
3622608Swnj tmstart(um)
3632982Swnj 	register struct uba_ctlr *um;
3641919Swnj {
3652608Swnj 	register struct buf *bp, *dp;
3665692Sroot 	register struct tmdevice *addr = (struct tmdevice *)um->um_addr;
3673095Swnj 	register struct te_softc *sc;
3682982Swnj 	register struct uba_device *ui;
3693095Swnj 	int teunit, cmd;
3702471Swnj 	daddr_t blkno;
3711919Swnj 
3722608Swnj 	/*
3732608Swnj 	 * Look for an idle transport on the controller.
3742608Swnj 	 */
3751919Swnj loop:
3762608Swnj 	if ((dp = um->um_tab.b_actf) == NULL)
3771919Swnj 		return;
3782608Swnj 	if ((bp = dp->b_actf) == NULL) {
3792608Swnj 		um->um_tab.b_actf = dp->b_forw;
3802608Swnj 		goto loop;
3812608Swnj 	}
3823095Swnj 	teunit = TEUNIT(bp->b_dev);
3833095Swnj 	ui = tedinfo[teunit];
3842608Swnj 	/*
3852608Swnj 	 * Record pre-transfer status (e.g. for TM_SENSE)
3862608Swnj 	 */
3873095Swnj 	sc = &te_softc[teunit];
3885692Sroot 	addr = (struct tmdevice *)um->um_addr;
3892608Swnj 	addr->tmcs = (ui->ui_slave << 8);
3902471Swnj 	sc->sc_dsreg = addr->tmcs;
3912471Swnj 	sc->sc_erreg = addr->tmer;
3922471Swnj 	sc->sc_resid = addr->tmbc;
3932608Swnj 	/*
3942608Swnj 	 * Default is that last command was NOT a write command;
3952608Swnj 	 * if we do a write command we will notice this in tmintr().
3962608Swnj 	 */
3973493Sroot 	sc->sc_lastiow = 0;
3982608Swnj 	if (sc->sc_openf < 0 || (addr->tmcs&TM_CUR) == 0) {
3992608Swnj 		/*
4003095Swnj 		 * Have had a hard error on a non-raw tape
4013095Swnj 		 * or the tape unit is now unavailable
4023095Swnj 		 * (e.g. taken off line).
4032608Swnj 		 */
4042608Swnj 		bp->b_flags |= B_ERROR;
4051919Swnj 		goto next;
4061919Swnj 	}
4073095Swnj 	if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) {
4083095Swnj 		/*
4093095Swnj 		 * Execute control operation with the specified count.
4103095Swnj 		 */
4112608Swnj 		if (bp->b_command == TM_SENSE)
4122608Swnj 			goto next;
4133495Sroot 		/*
4143495Sroot 		 * Set next state; give 5 minutes to complete
4153495Sroot 		 * rewind, or 10 seconds per iteration (minimum 60
4163629Sroot 		 * seconds and max 5 minutes) to complete other ops.
4173495Sroot 		 */
4183495Sroot 		if (bp->b_command == TM_REW) {
4193495Sroot 			um->um_tab.b_active = SREW;
4203495Sroot 			sc->sc_timo = 5 * 60;
4213495Sroot 		} else {
4223495Sroot 			um->um_tab.b_active = SCOM;
4234266Swnj 			sc->sc_timo =
4244266Swnj 			    imin(imax(10*(int)-bp->b_repcnt,60),5*60);
4253495Sroot 		}
4262608Swnj 		if (bp->b_command == TM_SFORW || bp->b_command == TM_SREV)
4272608Swnj 			addr->tmbc = bp->b_repcnt;
4282670Swnj 		goto dobpcmd;
4292608Swnj 	}
4302608Swnj 	/*
4313095Swnj 	 * The following checks handle boundary cases for operation
4323095Swnj 	 * on non-raw tapes.  On raw tapes the initialization of
4333095Swnj 	 * sc->sc_nxrec by tmphys causes them to be skipped normally
4343095Swnj 	 * (except in the case of retries).
4353095Swnj 	 */
4367381Ssam 	if (bdbtofsb(bp->b_blkno) > sc->sc_nxrec) {
4373095Swnj 		/*
4383095Swnj 		 * Can't read past known end-of-file.
4393095Swnj 		 */
4403095Swnj 		bp->b_flags |= B_ERROR;
4413095Swnj 		bp->b_error = ENXIO;
4423095Swnj 		goto next;
4433095Swnj 	}
4447381Ssam 	if (bdbtofsb(bp->b_blkno) == sc->sc_nxrec &&
4453095Swnj 	    bp->b_flags&B_READ) {
4463095Swnj 		/*
4473095Swnj 		 * Reading at end of file returns 0 bytes.
4483095Swnj 		 */
4493095Swnj 		bp->b_resid = bp->b_bcount;
4503095Swnj 		clrbuf(bp);
4513095Swnj 		goto next;
4523095Swnj 	}
4533095Swnj 	if ((bp->b_flags&B_READ) == 0)
4543095Swnj 		/*
4553095Swnj 		 * Writing sets EOF
4563095Swnj 		 */
4577381Ssam 		sc->sc_nxrec = bdbtofsb(bp->b_blkno) + 1;
4583095Swnj 	/*
4592608Swnj 	 * If the data transfer command is in the correct place,
4602608Swnj 	 * set up all the registers except the csr, and give
4612608Swnj 	 * control over to the UNIBUS adapter routines, to
4622608Swnj 	 * wait for resources to start the i/o.
4632608Swnj 	 */
4647381Ssam 	if ((blkno = sc->sc_blkno) == bdbtofsb(bp->b_blkno)) {
4652396Swnj 		addr->tmbc = -bp->b_bcount;
4661919Swnj 		if ((bp->b_flags&B_READ) == 0) {
4672471Swnj 			if (um->um_tab.b_errcnt)
4683095Swnj 				cmd = TM_WIRG;
4691919Swnj 			else
4703095Swnj 				cmd = TM_WCOM;
4711919Swnj 		} else
4723095Swnj 			cmd = TM_RCOM;
4732471Swnj 		um->um_tab.b_active = SIO;
4743095Swnj 		um->um_cmd = sc->sc_dens|cmd;
4752928Swnj #ifdef notdef
4762670Swnj 		if (tmreverseop(sc->sc_lastcmd))
4773095Swnj 			while (addr->tmer & TMER_SDWN)
4782670Swnj 				tmgapsdcnt++;
4792670Swnj 		sc->sc_lastcmd = TM_RCOM;		/* will serve */
4802928Swnj #endif
4813495Sroot 		sc->sc_timo = 60;	/* premature, but should serve */
4823105Swnj 		(void) ubago(ui);
4831919Swnj 		return;
4841919Swnj 	}
4852608Swnj 	/*
4863095Swnj 	 * Tape positioned incorrectly;
4873095Swnj 	 * set to seek forwards or backwards to the correct spot.
4883095Swnj 	 * This happens for raw tapes only on error retries.
4892608Swnj 	 */
4902471Swnj 	um->um_tab.b_active = SSEEK;
4917381Ssam 	if (blkno < bdbtofsb(bp->b_blkno)) {
4922670Swnj 		bp->b_command = TM_SFORW;
4937381Ssam 		addr->tmbc = blkno - bdbtofsb(bp->b_blkno);
4941919Swnj 	} else {
4952670Swnj 		bp->b_command = TM_SREV;
4967381Ssam 		addr->tmbc = bdbtofsb(bp->b_blkno) - blkno;
4971919Swnj 	}
4983629Sroot 	sc->sc_timo = imin(imax(10 * -addr->tmbc, 60), 5 * 60);
4992670Swnj dobpcmd:
5002928Swnj #ifdef notdef
5013095Swnj 	/*
5023095Swnj 	 * It is strictly necessary to wait for the tape
5033095Swnj 	 * to stop before changing directions, but the TC11
5043095Swnj 	 * handles this for us.
5053095Swnj 	 */
5062670Swnj 	if (tmreverseop(sc->sc_lastcmd) != tmreverseop(bp->b_command))
5072670Swnj 		while (addr->tmer & TM_SDWN)
5082670Swnj 			tmgapsdcnt++;
5092670Swnj 	sc->sc_lastcmd = bp->b_command;
5102928Swnj #endif
5113095Swnj 	/*
5123095Swnj 	 * Do the command in bp.
5133095Swnj 	 */
5143095Swnj 	addr->tmcs = (sc->sc_dens | bp->b_command);
5151919Swnj 	return;
5161919Swnj 
5171919Swnj next:
5182608Swnj 	/*
5192608Swnj 	 * Done with this operation due to error or
5202608Swnj 	 * the fact that it doesn't do anything.
5212608Swnj 	 * Release UBA resources (if any), dequeue
5222608Swnj 	 * the transfer and continue processing this slave.
5232608Swnj 	 */
5242608Swnj 	if (um->um_ubinfo)
5252617Swnj 		ubadone(um);
5262608Swnj 	um->um_tab.b_errcnt = 0;
5272608Swnj 	dp->b_actf = bp->av_forw;
5281919Swnj 	iodone(bp);
5291919Swnj 	goto loop;
5301919Swnj }
5311919Swnj 
5322608Swnj /*
5332608Swnj  * The UNIBUS resources we needed have been
5342608Swnj  * allocated to us; start the device.
5352608Swnj  */
5362574Swnj tmdgo(um)
5372982Swnj 	register struct uba_ctlr *um;
5381919Swnj {
5395692Sroot 	register struct tmdevice *addr = (struct tmdevice *)um->um_addr;
5402471Swnj 
5412574Swnj 	addr->tmba = um->um_ubinfo;
5422574Swnj 	addr->tmcs = um->um_cmd | ((um->um_ubinfo >> 12) & 0x30);
5432396Swnj }
5442396Swnj 
5452608Swnj /*
5462608Swnj  * Tm interrupt routine.
5472608Swnj  */
5482471Swnj /*ARGSUSED*/
5492630Swnj tmintr(tm11)
5502630Swnj 	int tm11;
5512396Swnj {
5522608Swnj 	struct buf *dp;
5531919Swnj 	register struct buf *bp;
5542982Swnj 	register struct uba_ctlr *um = tmminfo[tm11];
5555692Sroot 	register struct tmdevice *addr;
5563095Swnj 	register struct te_softc *sc;
5573095Swnj 	int teunit;
5581919Swnj 	register state;
5591919Swnj 
5603095Swnj 	if ((dp = um->um_tab.b_actf) == NULL)
5613095Swnj 		return;
5623095Swnj 	bp = dp->b_actf;
5633095Swnj 	teunit = TEUNIT(bp->b_dev);
5645692Sroot 	addr = (struct tmdevice *)tedinfo[teunit]->ui_addr;
5653524Swnj 	sc = &te_softc[teunit];
5662608Swnj 	/*
5672608Swnj 	 * If last command was a rewind, and tape is still
5682608Swnj 	 * rewinding, wait for the rewind complete interrupt.
5692608Swnj 	 */
5702608Swnj 	if (um->um_tab.b_active == SREW) {
5712608Swnj 		um->um_tab.b_active = SCOM;
5723524Swnj 		if (addr->tmer&TMER_RWS) {
5733524Swnj 			sc->sc_timo = 5*60;		/* 5 minutes */
5742608Swnj 			return;
5753524Swnj 		}
5761919Swnj 	}
5772608Swnj 	/*
5782608Swnj 	 * An operation completed... record status
5792608Swnj 	 */
5803495Sroot 	sc->sc_timo = INF;
5812471Swnj 	sc->sc_dsreg = addr->tmcs;
5822471Swnj 	sc->sc_erreg = addr->tmer;
5832471Swnj 	sc->sc_resid = addr->tmbc;
5841919Swnj 	if ((bp->b_flags & B_READ) == 0)
5852608Swnj 		sc->sc_lastiow = 1;
5862471Swnj 	state = um->um_tab.b_active;
5872471Swnj 	um->um_tab.b_active = 0;
5882608Swnj 	/*
5892608Swnj 	 * Check for errors.
5902608Swnj 	 */
5912608Swnj 	if (addr->tmcs&TM_ERR) {
5923095Swnj 		while (addr->tmer & TMER_SDWN)
5931919Swnj 			;			/* await settle down */
5942608Swnj 		/*
5953095Swnj 		 * If we hit the end of the tape file, update our position.
5962608Swnj 		 */
5973095Swnj 		if (addr->tmer&TMER_EOF) {
5982608Swnj 			tmseteof(bp);		/* set blkno and nxrec */
5992608Swnj 			state = SCOM;		/* force completion */
6002608Swnj 			/*
6012608Swnj 			 * Stuff bc so it will be unstuffed correctly
6022608Swnj 			 * later to get resid.
6032608Swnj 			 */
6042396Swnj 			addr->tmbc = -bp->b_bcount;
6052608Swnj 			goto opdone;
6061919Swnj 		}
6072608Swnj 		/*
6083095Swnj 		 * If we were reading raw tape and the only error was that the
6093095Swnj 		 * record was too long, then we don't consider this an error.
6102608Swnj 		 */
6113095Swnj 		if (bp == &rtmbuf[TMUNIT(bp->b_dev)] && (bp->b_flags&B_READ) &&
6123095Swnj 		    (addr->tmer&(TMER_HARD|TMER_SOFT)) == TMER_RLE)
6132608Swnj 			goto ignoreerr;
6142608Swnj 		/*
6152608Swnj 		 * If error is not hard, and this was an i/o operation
6162608Swnj 		 * retry up to 8 times.
6172608Swnj 		 */
6183095Swnj 		if ((addr->tmer&TMER_HARD)==0 && state==SIO) {
6192471Swnj 			if (++um->um_tab.b_errcnt < 7) {
6202471Swnj 				sc->sc_blkno++;
6212617Swnj 				ubadone(um);
6222608Swnj 				goto opcont;
6231919Swnj 			}
6242608Swnj 		} else
6252608Swnj 			/*
6262608Swnj 			 * Hard or non-i/o errors on non-raw tape
6272608Swnj 			 * cause it to close.
6282608Swnj 			 */
6293095Swnj 			if (sc->sc_openf>0 && bp != &rtmbuf[TMUNIT(bp->b_dev)])
6302608Swnj 				sc->sc_openf = -1;
6312608Swnj 		/*
6322608Swnj 		 * Couldn't recover error
6332608Swnj 		 */
6342928Swnj 		printf("te%d: hard error bn%d er=%b\n", minor(bp->b_dev)&03,
6353095Swnj 		    bp->b_blkno, sc->sc_erreg, TMER_BITS);
6361919Swnj 		bp->b_flags |= B_ERROR;
6372608Swnj 		goto opdone;
6381919Swnj 	}
6392608Swnj 	/*
6402608Swnj 	 * Advance tape control FSM.
6412608Swnj 	 */
6422608Swnj ignoreerr:
6431919Swnj 	switch (state) {
6441919Swnj 
6451919Swnj 	case SIO:
6462608Swnj 		/*
6472608Swnj 		 * Read/write increments tape block number
6482608Swnj 		 */
6492471Swnj 		sc->sc_blkno++;
6502608Swnj 		goto opdone;
6511919Swnj 
6521919Swnj 	case SCOM:
6532608Swnj 		/*
6543095Swnj 		 * For forward/backward space record update current position.
6552608Swnj 		 */
6563095Swnj 		if (bp == &ctmbuf[TMUNIT(bp->b_dev)])
6572608Swnj 		switch (bp->b_command) {
6581919Swnj 
6592608Swnj 		case TM_SFORW:
6602608Swnj 			sc->sc_blkno -= bp->b_repcnt;
6613095Swnj 			break;
6621919Swnj 
6632608Swnj 		case TM_SREV:
6642608Swnj 			sc->sc_blkno += bp->b_repcnt;
6653095Swnj 			break;
6661919Swnj 		}
6673095Swnj 		goto opdone;
6681919Swnj 
6691919Swnj 	case SSEEK:
6707381Ssam 		sc->sc_blkno = bdbtofsb(bp->b_blkno);
6712608Swnj 		goto opcont;
6721919Swnj 
6731919Swnj 	default:
6742608Swnj 		panic("tmintr");
6752608Swnj 	}
6762608Swnj opdone:
6772608Swnj 	/*
6782608Swnj 	 * Reset error count and remove
6792608Swnj 	 * from device queue.
6802608Swnj 	 */
6812608Swnj 	um->um_tab.b_errcnt = 0;
6822608Swnj 	dp->b_actf = bp->av_forw;
6832608Swnj 	bp->b_resid = -addr->tmbc;
6842617Swnj 	ubadone(um);
6852608Swnj 	iodone(bp);
6862608Swnj 	/*
6872608Swnj 	 * Circulate slave to end of controller
6882608Swnj 	 * queue to give other slaves a chance.
6892608Swnj 	 */
6902608Swnj 	um->um_tab.b_actf = dp->b_forw;
6912608Swnj 	if (dp->b_actf) {
6922608Swnj 		dp->b_forw = NULL;
6932608Swnj 		if (um->um_tab.b_actf == NULL)
6942608Swnj 			um->um_tab.b_actf = dp;
6952608Swnj 		else
6962608Swnj 			um->um_tab.b_actl->b_forw = dp;
6972608Swnj 		um->um_tab.b_actl = dp;
6982608Swnj 	}
6992608Swnj 	if (um->um_tab.b_actf == 0)
7001919Swnj 		return;
7012608Swnj opcont:
7022608Swnj 	tmstart(um);
7031919Swnj }
7041919Swnj 
7053495Sroot tmtimer(dev)
7063495Sroot 	int dev;
7073495Sroot {
7083495Sroot 	register struct te_softc *sc = &te_softc[TEUNIT(dev)];
7094847Sroot 	register short x;
7103495Sroot 
7113495Sroot 	if (sc->sc_timo != INF && (sc->sc_timo -= 5) < 0) {
7124278Sroot 		printf("te%d: lost interrupt\n", TEUNIT(dev));
7133495Sroot 		sc->sc_timo = INF;
7144847Sroot 		x = spl5();
7153495Sroot 		tmintr(TMUNIT(dev));
7164847Sroot 		(void) splx(x);
7173495Sroot 	}
7183629Sroot 	timeout(tmtimer, (caddr_t)dev, 5*hz);
7193495Sroot }
7203495Sroot 
7211919Swnj tmseteof(bp)
7221919Swnj 	register struct buf *bp;
7231919Swnj {
7243095Swnj 	register int teunit = TEUNIT(bp->b_dev);
7255692Sroot 	register struct tmdevice *addr =
7265692Sroot 	    (struct tmdevice *)tedinfo[teunit]->ui_addr;
7273095Swnj 	register struct te_softc *sc = &te_softc[teunit];
7281919Swnj 
7293095Swnj 	if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) {
7307381Ssam 		if (sc->sc_blkno > bdbtofsb(bp->b_blkno)) {
7311919Swnj 			/* reversing */
7327381Ssam 			sc->sc_nxrec = bdbtofsb(bp->b_blkno) - addr->tmbc;
7332471Swnj 			sc->sc_blkno = sc->sc_nxrec;
7341919Swnj 		} else {
7351919Swnj 			/* spacing forward */
7367381Ssam 			sc->sc_blkno = bdbtofsb(bp->b_blkno) + addr->tmbc;
7372471Swnj 			sc->sc_nxrec = sc->sc_blkno - 1;
7381919Swnj 		}
7391919Swnj 		return;
7401919Swnj 	}
7411919Swnj 	/* eof on read */
7427381Ssam 	sc->sc_nxrec = bdbtofsb(bp->b_blkno);
7431919Swnj }
7441919Swnj 
745*7732Sroot tmread(dev, uio)
7462608Swnj 	dev_t dev;
747*7732Sroot 	struct uio *uio;
7481919Swnj {
7491919Swnj 
750*7732Sroot 	u.u_error = tmphys(dev, uio);
7512982Swnj 	if (u.u_error)
7522982Swnj 		return;
753*7732Sroot 	physio(tmstrategy, &rtmbuf[TMUNIT(dev)], dev, B_READ, minphys, uio);
7541919Swnj }
7551919Swnj 
7561919Swnj tmwrite(dev)
7572608Swnj 	dev_t dev;
7581919Swnj {
7591919Swnj 
760*7732Sroot 	tmphys(dev, 0);
7612982Swnj 	if (u.u_error)
7622982Swnj 		return;
763*7732Sroot 	physio(tmstrategy, &rtmbuf[TMUNIT(dev)], dev, B_WRITE, minphys, 0);
7641919Swnj }
7651919Swnj 
7663095Swnj /*
7673095Swnj  * Check that a raw device exists.
7683095Swnj  * If it does, set up sc_blkno and sc_nxrec
7693095Swnj  * so that the tape will appear positioned correctly.
7703095Swnj  */
771*7732Sroot tmphys(dev, uio)
7722608Swnj 	dev_t dev;
773*7732Sroot 	struct uio *uio;
7741919Swnj {
7753095Swnj 	register int teunit = TEUNIT(dev);
7761919Swnj 	register daddr_t a;
7773095Swnj 	register struct te_softc *sc;
7783095Swnj 	register struct uba_device *ui;
7791919Swnj 
7803095Swnj 	if (teunit >= NTE || (ui=tedinfo[teunit]) == 0 || ui->ui_alive == 0) {
7812982Swnj 		u.u_error = ENXIO;
782*7732Sroot 		return (ENXIO);
7832982Swnj 	}
7843095Swnj 	sc = &te_softc[teunit];
785*7732Sroot 	if (uio)
786*7732Sroot 		a = bdbtofsb(uio->uio_offset >> 9);
787*7732Sroot 	else
788*7732Sroot 		a = bdbtofsb(u.u_offset >> 9);
7892471Swnj 	sc->sc_blkno = a;
7902471Swnj 	sc->sc_nxrec = a + 1;
791*7732Sroot 	return (0);
7921919Swnj }
7931919Swnj 
7942608Swnj tmreset(uban)
7952608Swnj 	int uban;
7962608Swnj {
7972982Swnj 	register struct uba_ctlr *um;
7983095Swnj 	register tm11, teunit;
7992982Swnj 	register struct uba_device *ui;
8002608Swnj 	register struct buf *dp;
8012608Swnj 
8022630Swnj 	for (tm11 = 0; tm11 < NTM; tm11++) {
8032630Swnj 		if ((um = tmminfo[tm11]) == 0 || um->um_alive == 0 ||
8042608Swnj 		   um->um_ubanum != uban)
8052608Swnj 			continue;
8062928Swnj 		printf(" tm%d", tm11);
8072608Swnj 		um->um_tab.b_active = 0;
8082608Swnj 		um->um_tab.b_actf = um->um_tab.b_actl = 0;
8092608Swnj 		if (um->um_ubinfo) {
8102608Swnj 			printf("<%d>", (um->um_ubinfo>>28)&0xf);
8112617Swnj 			ubadone(um);
8122608Swnj 		}
8135692Sroot 		((struct tmdevice *)(um->um_addr))->tmcs = TM_DCLR;
8143095Swnj 		for (teunit = 0; teunit < NTE; teunit++) {
8153095Swnj 			if ((ui = tedinfo[teunit]) == 0 || ui->ui_mi != um ||
8163095Swnj 			    ui->ui_alive == 0)
8172608Swnj 				continue;
8183095Swnj 			dp = &teutab[teunit];
8192608Swnj 			dp->b_active = 0;
8202608Swnj 			dp->b_forw = 0;
8212608Swnj 			if (um->um_tab.b_actf == NULL)
8222608Swnj 				um->um_tab.b_actf = dp;
8232608Swnj 			else
8242608Swnj 				um->um_tab.b_actl->b_forw = dp;
8252608Swnj 			um->um_tab.b_actl = dp;
8263495Sroot 			if (te_softc[teunit].sc_openf > 0)
8273495Sroot 				te_softc[teunit].sc_openf = -1;
8282608Swnj 		}
8292608Swnj 		tmstart(um);
8302608Swnj 	}
8312608Swnj }
8322608Swnj 
8331919Swnj /*ARGSUSED*/
8347632Ssam tmioctl(dev, cmd, data, flag)
8357632Ssam 	caddr_t data;
8361919Swnj 	dev_t dev;
8371919Swnj {
8383095Swnj 	int teunit = TEUNIT(dev);
8393095Swnj 	register struct te_softc *sc = &te_softc[teunit];
8403095Swnj 	register struct buf *bp = &ctmbuf[TMUNIT(dev)];
8411919Swnj 	register callcount;
8421919Swnj 	int fcount;
8437632Ssam 	struct mtop *mtop;
8447632Ssam 	struct mtget *mtget;
8451919Swnj 	/* we depend of the values and order of the MT codes here */
8462608Swnj 	static tmops[] =
8472608Swnj 	   {TM_WEOF,TM_SFORW,TM_SREV,TM_SFORW,TM_SREV,TM_REW,TM_OFFL,TM_SENSE};
8481919Swnj 
8492608Swnj 	switch (cmd) {
8507632Ssam 
8517632Ssam 	case MTIOCTOP:	/* tape operation */
8527632Ssam 		mtop = (struct mtop *)data;
8537632Ssam 		switch(mtop->mt_op) {
8547632Ssam 
8552608Swnj 		case MTWEOF:
8567632Ssam 			callcount = mtop->mt_count;
8572608Swnj 			fcount = 1;
8582608Swnj 			break;
8597632Ssam 
8602608Swnj 		case MTFSF: case MTBSF:
8617632Ssam 			callcount = mtop->mt_count;
8621919Swnj 			fcount = INF;
8631919Swnj 			break;
8647632Ssam 
8651919Swnj 		case MTFSR: case MTBSR:
8661919Swnj 			callcount = 1;
8677632Ssam 			fcount = mtop->mt_count;
8681919Swnj 			break;
8697632Ssam 
8702324Skre 		case MTREW: case MTOFFL: case MTNOP:
8711919Swnj 			callcount = 1;
8721919Swnj 			fcount = 1;
8731919Swnj 			break;
8747632Ssam 
8751919Swnj 		default:
8761919Swnj 			u.u_error = ENXIO;
8771919Swnj 			return;
8781919Swnj 		}
8792608Swnj 		if (callcount <= 0 || fcount <= 0) {
8801919Swnj 			u.u_error = ENXIO;
8812608Swnj 			return;
8822608Swnj 		}
8832608Swnj 		while (--callcount >= 0) {
8847632Ssam 			tmcommand(dev, tmops[mtop->mt_op], fcount);
8857632Ssam 			if ((mtop->mt_op == MTFSR || mtop->mt_op == MTBSR) &&
8862608Swnj 			    bp->b_resid) {
8871919Swnj 				u.u_error = EIO;
8881919Swnj 				break;
8891919Swnj 			}
8903095Swnj 			if ((bp->b_flags&B_ERROR) || sc->sc_erreg&TMER_BOT)
8911919Swnj 				break;
8921919Swnj 		}
8932608Swnj 		geterror(bp);
8941919Swnj 		return;
8957632Ssam 
8961919Swnj 	case MTIOCGET:
8977632Ssam 		mtget = (struct mtget *)data;
8987632Ssam 		mtget->mt_dsreg = sc->sc_dsreg;
8997632Ssam 		mtget->mt_erreg = sc->sc_erreg;
9007632Ssam 		mtget->mt_resid = sc->sc_resid;
9017632Ssam 		mtget->mt_type = MT_ISTM;
9021919Swnj 		return;
9037632Ssam 
9041919Swnj 	default:
9051919Swnj 		u.u_error = ENXIO;
9061919Swnj 	}
9071919Swnj }
9081919Swnj 
9091919Swnj #define	DBSIZE	20
9101919Swnj 
9112363Swnj tmdump()
9122363Swnj {
9132982Swnj 	register struct uba_device *ui;
9142396Swnj 	register struct uba_regs *up;
9155692Sroot 	register struct tmdevice *addr;
9162426Skre 	int blk, num;
9172426Skre 	int start;
9181919Swnj 
9192426Skre 	start = 0;
9202426Skre 	num = maxfree;
9212426Skre #define	phys(a,b)	((b)((int)(a)&0x7fffffff))
9223095Swnj 	if (tedinfo[0] == 0)
9232887Swnj 		return (ENXIO);
9243095Swnj 	ui = phys(tedinfo[0], struct uba_device *);
9252396Swnj 	up = phys(ui->ui_hd, struct uba_hd *)->uh_physuba;
9263331Swnj 	ubainit(up);
9272324Skre 	DELAY(1000000);
9285692Sroot 	addr = (struct tmdevice *)ui->ui_physaddr;
9292396Swnj 	tmwait(addr);
9302608Swnj 	addr->tmcs = TM_DCLR | TM_GO;
9311919Swnj 	while (num > 0) {
9321919Swnj 		blk = num > DBSIZE ? DBSIZE : num;
9332396Swnj 		tmdwrite(start, blk, addr, up);
9341919Swnj 		start += blk;
9351919Swnj 		num -= blk;
9361919Swnj 	}
9372426Skre 	tmeof(addr);
9382426Skre 	tmeof(addr);
9392426Skre 	tmwait(addr);
9402887Swnj 	if (addr->tmcs&TM_ERR)
9412887Swnj 		return (EIO);
9422608Swnj 	addr->tmcs = TM_REW | TM_GO;
9432471Swnj 	tmwait(addr);
9442363Swnj 	return (0);
9451919Swnj }
9461919Swnj 
9472608Swnj tmdwrite(dbuf, num, addr, up)
9482608Swnj 	register dbuf, num;
9495692Sroot 	register struct tmdevice *addr;
9502396Swnj 	struct uba_regs *up;
9511919Swnj {
9522396Swnj 	register struct pte *io;
9532396Swnj 	register int npf;
9541928Swnj 
9552396Swnj 	tmwait(addr);
9562396Swnj 	io = up->uba_map;
9571919Swnj 	npf = num+1;
9581928Swnj 	while (--npf != 0)
9592982Swnj 		 *(int *)io++ = (dbuf++ | (1<<UBAMR_DPSHIFT) | UBAMR_MRV);
9602396Swnj 	*(int *)io = 0;
9612396Swnj 	addr->tmbc = -(num*NBPG);
9622396Swnj 	addr->tmba = 0;
9632608Swnj 	addr->tmcs = TM_WCOM | TM_GO;
9641919Swnj }
9651919Swnj 
9662396Swnj tmwait(addr)
9675692Sroot 	register struct tmdevice *addr;
9681919Swnj {
9691928Swnj 	register s;
9701919Swnj 
9711919Swnj 	do
9722396Swnj 		s = addr->tmcs;
9732608Swnj 	while ((s & TM_CUR) == 0);
9741919Swnj }
9751919Swnj 
9762396Swnj tmeof(addr)
9775692Sroot 	struct tmdevice *addr;
9781919Swnj {
9791919Swnj 
9802396Swnj 	tmwait(addr);
9812608Swnj 	addr->tmcs = TM_WEOF | TM_GO;
9821919Swnj }
9831919Swnj #endif
984