xref: /csrg-svn/sys/vax/uba/tm.c (revision 18321)
1*18321Sralph /*	tm.c	6.5	85/03/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  */
169778Ssam #include "../machine/pte.h"
179778Ssam 
1817079Sbloom #include "param.h"
1917079Sbloom #include "systm.h"
2017079Sbloom #include "buf.h"
2117079Sbloom #include "dir.h"
2217079Sbloom #include "conf.h"
2317079Sbloom #include "user.h"
2417079Sbloom #include "file.h"
2517079Sbloom #include "map.h"
2617079Sbloom #include "vm.h"
2717079Sbloom #include "ioctl.h"
2817079Sbloom #include "mtio.h"
2917079Sbloom #include "cmap.h"
3017079Sbloom #include "uio.h"
3117079Sbloom #include "kernel.h"
32*18321Sralph #include "tty.h"
331919Swnj 
348479Sroot #include "../vax/cpu.h"
3517079Sbloom #include "ubareg.h"
3617079Sbloom #include "ubavar.h"
3717079Sbloom #include "tmreg.h"
381919Swnj 
393095Swnj /*
403095Swnj  * There is a ctmbuf per tape controller.
413095Swnj  * It is used as the token to pass to the internal routines
423095Swnj  * to execute tape ioctls, and also acts as a lock on the slaves
433095Swnj  * on the controller, since there is only one per controller.
443095Swnj  * In particular, when the tape is rewinding on close we release
453095Swnj  * the user process but any further attempts to use the tape drive
463095Swnj  * before the rewind completes will hang waiting for ctmbuf.
473095Swnj  */
483095Swnj struct	buf	ctmbuf[NTM];
491919Swnj 
503095Swnj /*
513095Swnj  * Raw tape operations use rtmbuf.  The driver
523095Swnj  * notices when rtmbuf is being used and allows the user
533095Swnj  * program to continue after errors and read records
543095Swnj  * not of the standard length (BSIZE).
553095Swnj  */
563095Swnj struct	buf	rtmbuf[NTM];
573095Swnj 
583095Swnj /*
593095Swnj  * Driver unibus interface routines and variables.
603095Swnj  */
612608Swnj int	tmprobe(), tmslave(), tmattach(), tmdgo(), tmintr();
622982Swnj struct	uba_ctlr *tmminfo[NTM];
633095Swnj struct	uba_device *tedinfo[NTE];
643095Swnj struct	buf teutab[NTE];
653095Swnj short	tetotm[NTE];
662458Swnj u_short	tmstd[] = { 0772520, 0 };
672396Swnj struct	uba_driver tmdriver =
683095Swnj  { tmprobe, tmslave, tmattach, tmdgo, tmstd, "te", tedinfo, "tm", tmminfo, 0 };
691919Swnj 
701919Swnj /* bits in minor device */
713095Swnj #define	TEUNIT(dev)	(minor(dev)&03)
723095Swnj #define	TMUNIT(dev)	(tetotm[TEUNIT(dev)])
731919Swnj #define	T_NOREWIND	04
741919Swnj #define	T_1600BPI	08
751919Swnj 
761919Swnj #define	INF	(daddr_t)1000000L
771919Swnj 
782608Swnj /*
792608Swnj  * Software state per tape transport.
803095Swnj  *
813095Swnj  * 1. A tape drive is a unique-open device; we refuse opens when it is already.
823095Swnj  * 2. We keep track of the current position on a block tape and seek
833095Swnj  *    before operations by forward/back spacing if necessary.
843095Swnj  * 3. We remember if the last operation was a write on a tape, so if a tape
853095Swnj  *    is open read write and the last thing done is a write we can
863095Swnj  *    write a standard end of tape mark (two eofs).
873095Swnj  * 4. We remember the status registers after the last command, using
883095Swnj  *    then internally and returning them to the SENSE ioctl.
893095Swnj  * 5. We remember the last density the tape was used at.  If it is
903095Swnj  *    not a BOT when we start using it and we are writing, we don't
913095Swnj  *    let the density be changed.
922608Swnj  */
933095Swnj struct	te_softc {
942608Swnj 	char	sc_openf;	/* lock against multiple opens */
952608Swnj 	char	sc_lastiow;	/* last op was a write */
962608Swnj 	daddr_t	sc_blkno;	/* block number, for block device tape */
973095Swnj 	daddr_t	sc_nxrec;	/* position of end of tape, if known */
982608Swnj 	u_short	sc_erreg;	/* copy of last erreg */
992608Swnj 	u_short	sc_dsreg;	/* copy of last dsreg */
1002608Swnj 	short	sc_resid;	/* copy of last bc */
1013105Swnj #ifdef unneeded
1022670Swnj 	short	sc_lastcmd;	/* last command to handle direction changes */
1032928Swnj #endif
1043095Swnj 	u_short	sc_dens;	/* prototype command with density info */
105*18321Sralph 	short	sc_tact;	/* timeout is active */
1063495Sroot 	daddr_t	sc_timo;	/* time until timeout expires */
107*18321Sralph 	struct	tty *sc_ttyp;	/* record user's tty for errors */
1086952Swnj } te_softc[NTE];
1093105Swnj #ifdef unneeded
1103105Swnj int	tmgapsdcnt;		/* DEBUG */
1113105Swnj #endif
1121919Swnj 
1132608Swnj /*
1143095Swnj  * States for um->um_tab.b_active, the per controller state flag.
1153095Swnj  * This is used to sequence control in the driver.
1162608Swnj  */
1171919Swnj #define	SSEEK	1		/* seeking */
1181919Swnj #define	SIO	2		/* doing seq i/o */
1191919Swnj #define	SCOM	3		/* sending control command */
1202608Swnj #define	SREW	4		/* sending a drive rewind */
1211919Swnj 
1222426Skre /*
1232426Skre  * Determine if there is a controller for
1242426Skre  * a tm at address reg.  Our goal is to make the
1252426Skre  * device interrupt.
1262426Skre  */
1272608Swnj tmprobe(reg)
1282396Swnj 	caddr_t reg;
1292396Swnj {
1303095Swnj 	register int br, cvec;		/* must be r11,r10; value-result */
1312426Skre 
1322608Swnj #ifdef lint
1333105Swnj 	br = 0; cvec = br; br = cvec;
1344936Swnj 	tmintr(0);
1352608Swnj #endif
1365692Sroot 	((struct tmdevice *)reg)->tmcs = TM_IE;
1372396Swnj 	/*
1382630Swnj 	 * If this is a tm11, it ought to have interrupted
1392396Swnj 	 * by now, if it isn't (ie: it is a ts04) then we just
1402458Swnj 	 * hope that it didn't interrupt, so autoconf will ignore it.
1412458Swnj 	 * Just in case, we will reference one
1422396Swnj 	 * of the more distant registers, and hope for a machine
1432458Swnj 	 * check, or similar disaster if this is a ts.
1442471Swnj 	 *
1452471Swnj 	 * Note: on an 11/780, badaddr will just generate
1462471Swnj 	 * a uba error for a ts; but our caller will notice that
1472471Swnj 	 * so we won't check for it.
1482396Swnj 	 */
1495692Sroot 	if (badaddr((caddr_t)&((struct tmdevice *)reg)->tmrd, 2))
1502458Swnj 		return (0);
1517404Skre 	return (sizeof (struct tmdevice));
1522396Swnj }
1532396Swnj 
1542608Swnj /*
1552608Swnj  * Due to a design flaw, we cannot ascertain if the tape
1562608Swnj  * exists or not unless it is on line - ie: unless a tape is
1572608Swnj  * mounted. This is too servere a restriction to bear,
1582608Swnj  * so all units are assumed to exist.
1592608Swnj  */
1602608Swnj /*ARGSUSED*/
1612574Swnj tmslave(ui, reg)
1622982Swnj 	struct uba_device *ui;
1632396Swnj 	caddr_t reg;
1642396Swnj {
1652458Swnj 
1662458Swnj 	return (1);
1672396Swnj }
1682396Swnj 
1692608Swnj /*
1703095Swnj  * Record attachment of the unit to the controller.
1712608Swnj  */
1722608Swnj /*ARGSUSED*/
1732608Swnj tmattach(ui)
1742982Swnj 	struct uba_device *ui;
1752608Swnj {
1763095Swnj 	/*
1773095Swnj 	 * Tetotm is used in TMUNIT to index the ctmbuf and rtmbuf
1783095Swnj 	 * arrays given a te unit number.
1793095Swnj 	 */
1803095Swnj 	tetotm[ui->ui_unit] = ui->ui_mi->um_ctlr;
1812608Swnj }
1822608Swnj 
1833495Sroot int	tmtimer();
1842608Swnj /*
1852608Swnj  * Open the device.  Tapes are unique open
1862608Swnj  * devices, so we refuse if it is already open.
1872608Swnj  * We also check that a tape is available, and
1883095Swnj  * don't block waiting here; if you want to wait
1893095Swnj  * for a tape you should timeout in user code.
1902608Swnj  */
1911919Swnj tmopen(dev, flag)
1921919Swnj 	dev_t dev;
1931919Swnj 	int flag;
1941919Swnj {
1953095Swnj 	register int teunit;
1962982Swnj 	register struct uba_device *ui;
1973095Swnj 	register struct te_softc *sc;
1983209Swnj 	int olddens, dens;
1995437Sroot 	int s;
2001919Swnj 
2013095Swnj 	teunit = TEUNIT(dev);
2023095Swnj 	if (teunit>=NTE || (sc = &te_softc[teunit])->sc_openf ||
2038574Sroot 	    (ui = tedinfo[teunit]) == 0 || ui->ui_alive == 0)
2048574Sroot 		return (ENXIO);
2053209Swnj 	olddens = sc->sc_dens;
2063209Swnj 	dens = TM_IE | TM_GO | (ui->ui_slave << 8);
2073209Swnj 	if ((minor(dev) & T_1600BPI) == 0)
2083209Swnj 		dens |= TM_D800;
2093209Swnj 	sc->sc_dens = dens;
2103141Swnj get:
2112608Swnj 	tmcommand(dev, TM_SENSE, 1);
2123141Swnj 	if (sc->sc_erreg&TMER_SDWN) {
2133141Swnj 		sleep((caddr_t)&lbolt, PZERO+1);
2143141Swnj 		goto get;
2153141Swnj 	}
2163209Swnj 	sc->sc_dens = olddens;
2173710Sroot 	if ((sc->sc_erreg&(TMER_SELR|TMER_TUR)) != (TMER_SELR|TMER_TUR)) {
2183710Sroot 		uprintf("te%d: not online\n", teunit);
2198574Sroot 		return (EIO);
2201919Swnj 	}
2213710Sroot 	if ((flag&FWRITE) && (sc->sc_erreg&TMER_WRL)) {
2223710Sroot 		uprintf("te%d: no write ring\n", teunit);
2238574Sroot 		return (EIO);
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);
2288574Sroot 		return (EIO);
2293710Sroot 	}
2302608Swnj 	sc->sc_openf = 1;
2312471Swnj 	sc->sc_blkno = (daddr_t)0;
2322471Swnj 	sc->sc_nxrec = INF;
2332608Swnj 	sc->sc_lastiow = 0;
2343095Swnj 	sc->sc_dens = dens;
235*18321Sralph 	sc->sc_ttyp = u.u_ttyp;
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);
2438574Sroot 	return (0);
2441919Swnj }
2451919Swnj 
2462608Swnj /*
2472608Swnj  * Close tape device.
2482608Swnj  *
2492608Swnj  * If tape was open for writing or last operation was
2502608Swnj  * a write, then write two EOF's and backspace over the last one.
2512608Swnj  * Unless this is a non-rewinding special file, rewind the tape.
2522608Swnj  * Make the tape available to others.
2532608Swnj  */
2541919Swnj tmclose(dev, flag)
2551919Swnj 	register dev_t dev;
2561919Swnj 	register flag;
2571919Swnj {
2583095Swnj 	register struct te_softc *sc = &te_softc[TEUNIT(dev)];
2591919Swnj 
2602608Swnj 	if (flag == FWRITE || (flag&FWRITE) && sc->sc_lastiow) {
2612608Swnj 		tmcommand(dev, TM_WEOF, 1);
2622608Swnj 		tmcommand(dev, TM_WEOF, 1);
2632608Swnj 		tmcommand(dev, TM_SREV, 1);
2641919Swnj 	}
2651919Swnj 	if ((minor(dev)&T_NOREWIND) == 0)
2663095Swnj 		/*
2673095Swnj 		 * 0 count means don't hang waiting for rewind complete
2683095Swnj 		 * rather ctmbuf stays busy until the operation completes
2693095Swnj 		 * preventing further opens from completing by
2703095Swnj 		 * preventing a TM_SENSE from completing.
2713095Swnj 		 */
2723095Swnj 		tmcommand(dev, TM_REW, 0);
2732471Swnj 	sc->sc_openf = 0;
2741919Swnj }
2751919Swnj 
2762608Swnj /*
2772608Swnj  * Execute a command on the tape drive
2782608Swnj  * a specified number of times.
2792608Swnj  */
2802574Swnj tmcommand(dev, com, count)
2811919Swnj 	dev_t dev;
2821919Swnj 	int com, count;
2831919Swnj {
2841919Swnj 	register struct buf *bp;
2855437Sroot 	register int s;
2861919Swnj 
2872608Swnj 	bp = &ctmbuf[TMUNIT(dev)];
2885437Sroot 	s = spl5();
2891919Swnj 	while (bp->b_flags&B_BUSY) {
2903095Swnj 		/*
2913095Swnj 		 * This special check is because B_BUSY never
2923095Swnj 		 * gets cleared in the non-waiting rewind case.
2933095Swnj 		 */
2943141Swnj 		if (bp->b_repcnt == 0 && (bp->b_flags&B_DONE))
2953095Swnj 			break;
2961919Swnj 		bp->b_flags |= B_WANTED;
2971919Swnj 		sleep((caddr_t)bp, PRIBIO);
2981919Swnj 	}
2991919Swnj 	bp->b_flags = B_BUSY|B_READ;
3005437Sroot 	splx(s);
3011919Swnj 	bp->b_dev = dev;
3021919Swnj 	bp->b_repcnt = -count;
3031919Swnj 	bp->b_command = com;
3041919Swnj 	bp->b_blkno = 0;
3051919Swnj 	tmstrategy(bp);
3063095Swnj 	/*
3073095Swnj 	 * In case of rewind from close, don't wait.
3083095Swnj 	 * This is the only case where count can be 0.
3093095Swnj 	 */
3103095Swnj 	if (count == 0)
3113095Swnj 		return;
3121919Swnj 	iowait(bp);
3131919Swnj 	if (bp->b_flags&B_WANTED)
3141919Swnj 		wakeup((caddr_t)bp);
3151919Swnj 	bp->b_flags &= B_ERROR;
3161919Swnj }
3171919Swnj 
3182608Swnj /*
3193095Swnj  * Queue a tape operation.
3202608Swnj  */
3211919Swnj tmstrategy(bp)
3221919Swnj 	register struct buf *bp;
3231919Swnj {
3243095Swnj 	int teunit = TEUNIT(bp->b_dev);
3252982Swnj 	register struct uba_ctlr *um;
3262608Swnj 	register struct buf *dp;
3275437Sroot 	int s;
3281919Swnj 
3292608Swnj 	/*
3302608Swnj 	 * Put transfer at end of unit queue
3312608Swnj 	 */
3323095Swnj 	dp = &teutab[teunit];
3331919Swnj 	bp->av_forw = NULL;
3345437Sroot 	s = spl5();
3353939Sbugs 	um = tedinfo[teunit]->ui_mi;
3362608Swnj 	if (dp->b_actf == NULL) {
3372608Swnj 		dp->b_actf = bp;
3382608Swnj 		/*
3392608Swnj 		 * Transport not already active...
3402608Swnj 		 * put at end of controller queue.
3412608Swnj 		 */
3422608Swnj 		dp->b_forw = NULL;
3432608Swnj 		if (um->um_tab.b_actf == NULL)
3442608Swnj 			um->um_tab.b_actf = dp;
3452608Swnj 		else
3462608Swnj 			um->um_tab.b_actl->b_forw = dp;
3472608Swnj 		um->um_tab.b_actl = dp;
3482608Swnj 	} else
3492608Swnj 		dp->b_actl->av_forw = bp;
3502608Swnj 	dp->b_actl = bp;
3512608Swnj 	/*
3522608Swnj 	 * If the controller is not busy, get
3532608Swnj 	 * it going.
3542608Swnj 	 */
3552608Swnj 	if (um->um_tab.b_active == 0)
3562608Swnj 		tmstart(um);
3575437Sroot 	splx(s);
3581919Swnj }
3591919Swnj 
3602608Swnj /*
3612608Swnj  * Start activity on a tm controller.
3622608Swnj  */
3632608Swnj tmstart(um)
3642982Swnj 	register struct uba_ctlr *um;
3651919Swnj {
3662608Swnj 	register struct buf *bp, *dp;
3675692Sroot 	register struct tmdevice *addr = (struct tmdevice *)um->um_addr;
3683095Swnj 	register struct te_softc *sc;
3692982Swnj 	register struct uba_device *ui;
3703095Swnj 	int teunit, cmd;
3712471Swnj 	daddr_t blkno;
3721919Swnj 
3732608Swnj 	/*
3742608Swnj 	 * Look for an idle transport on the controller.
3752608Swnj 	 */
3761919Swnj loop:
3772608Swnj 	if ((dp = um->um_tab.b_actf) == NULL)
3781919Swnj 		return;
3792608Swnj 	if ((bp = dp->b_actf) == NULL) {
3802608Swnj 		um->um_tab.b_actf = dp->b_forw;
3812608Swnj 		goto loop;
3822608Swnj 	}
3833095Swnj 	teunit = TEUNIT(bp->b_dev);
3843095Swnj 	ui = tedinfo[teunit];
3852608Swnj 	/*
3862608Swnj 	 * Record pre-transfer status (e.g. for TM_SENSE)
3872608Swnj 	 */
3883095Swnj 	sc = &te_softc[teunit];
3895692Sroot 	addr = (struct tmdevice *)um->um_addr;
3902608Swnj 	addr->tmcs = (ui->ui_slave << 8);
3912471Swnj 	sc->sc_dsreg = addr->tmcs;
3922471Swnj 	sc->sc_erreg = addr->tmer;
3932471Swnj 	sc->sc_resid = addr->tmbc;
3942608Swnj 	/*
3952608Swnj 	 * Default is that last command was NOT a write command;
3962608Swnj 	 * if we do a write command we will notice this in tmintr().
3972608Swnj 	 */
3983493Sroot 	sc->sc_lastiow = 0;
3992608Swnj 	if (sc->sc_openf < 0 || (addr->tmcs&TM_CUR) == 0) {
4002608Swnj 		/*
4013095Swnj 		 * Have had a hard error on a non-raw tape
4023095Swnj 		 * or the tape unit is now unavailable
4033095Swnj 		 * (e.g. taken off line).
4042608Swnj 		 */
4052608Swnj 		bp->b_flags |= B_ERROR;
4061919Swnj 		goto next;
4071919Swnj 	}
4083095Swnj 	if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) {
4093095Swnj 		/*
4103095Swnj 		 * Execute control operation with the specified count.
4113095Swnj 		 */
4122608Swnj 		if (bp->b_command == TM_SENSE)
4132608Swnj 			goto next;
4143495Sroot 		/*
4153495Sroot 		 * Set next state; give 5 minutes to complete
4163495Sroot 		 * rewind, or 10 seconds per iteration (minimum 60
4173629Sroot 		 * seconds and max 5 minutes) to complete other ops.
4183495Sroot 		 */
4193495Sroot 		if (bp->b_command == TM_REW) {
4203495Sroot 			um->um_tab.b_active = SREW;
4213495Sroot 			sc->sc_timo = 5 * 60;
4223495Sroot 		} else {
4233495Sroot 			um->um_tab.b_active = SCOM;
4244266Swnj 			sc->sc_timo =
4254266Swnj 			    imin(imax(10*(int)-bp->b_repcnt,60),5*60);
4263495Sroot 		}
4272608Swnj 		if (bp->b_command == TM_SFORW || bp->b_command == TM_SREV)
4282608Swnj 			addr->tmbc = bp->b_repcnt;
4292670Swnj 		goto dobpcmd;
4302608Swnj 	}
4312608Swnj 	/*
4323095Swnj 	 * The following checks handle boundary cases for operation
4333095Swnj 	 * on non-raw tapes.  On raw tapes the initialization of
4343095Swnj 	 * sc->sc_nxrec by tmphys causes them to be skipped normally
4353095Swnj 	 * (except in the case of retries).
4363095Swnj 	 */
4377381Ssam 	if (bdbtofsb(bp->b_blkno) > sc->sc_nxrec) {
4383095Swnj 		/*
4393095Swnj 		 * Can't read past known end-of-file.
4403095Swnj 		 */
4413095Swnj 		bp->b_flags |= B_ERROR;
4423095Swnj 		bp->b_error = ENXIO;
4433095Swnj 		goto next;
4443095Swnj 	}
4457381Ssam 	if (bdbtofsb(bp->b_blkno) == sc->sc_nxrec &&
4463095Swnj 	    bp->b_flags&B_READ) {
4473095Swnj 		/*
4483095Swnj 		 * Reading at end of file returns 0 bytes.
4493095Swnj 		 */
4503095Swnj 		bp->b_resid = bp->b_bcount;
4513095Swnj 		clrbuf(bp);
4523095Swnj 		goto next;
4533095Swnj 	}
4543095Swnj 	if ((bp->b_flags&B_READ) == 0)
4553095Swnj 		/*
4563095Swnj 		 * Writing sets EOF
4573095Swnj 		 */
4587381Ssam 		sc->sc_nxrec = bdbtofsb(bp->b_blkno) + 1;
4593095Swnj 	/*
4602608Swnj 	 * If the data transfer command is in the correct place,
4612608Swnj 	 * set up all the registers except the csr, and give
4622608Swnj 	 * control over to the UNIBUS adapter routines, to
4632608Swnj 	 * wait for resources to start the i/o.
4642608Swnj 	 */
4657381Ssam 	if ((blkno = sc->sc_blkno) == bdbtofsb(bp->b_blkno)) {
4662396Swnj 		addr->tmbc = -bp->b_bcount;
4671919Swnj 		if ((bp->b_flags&B_READ) == 0) {
4682471Swnj 			if (um->um_tab.b_errcnt)
4693095Swnj 				cmd = TM_WIRG;
4701919Swnj 			else
4713095Swnj 				cmd = TM_WCOM;
4721919Swnj 		} else
4733095Swnj 			cmd = TM_RCOM;
4742471Swnj 		um->um_tab.b_active = SIO;
4753095Swnj 		um->um_cmd = sc->sc_dens|cmd;
4762928Swnj #ifdef notdef
4772670Swnj 		if (tmreverseop(sc->sc_lastcmd))
4783095Swnj 			while (addr->tmer & TMER_SDWN)
4792670Swnj 				tmgapsdcnt++;
4802670Swnj 		sc->sc_lastcmd = TM_RCOM;		/* will serve */
4812928Swnj #endif
4823495Sroot 		sc->sc_timo = 60;	/* premature, but should serve */
4833105Swnj 		(void) ubago(ui);
4841919Swnj 		return;
4851919Swnj 	}
4862608Swnj 	/*
4873095Swnj 	 * Tape positioned incorrectly;
4883095Swnj 	 * set to seek forwards or backwards to the correct spot.
4893095Swnj 	 * This happens for raw tapes only on error retries.
4902608Swnj 	 */
4912471Swnj 	um->um_tab.b_active = SSEEK;
4927381Ssam 	if (blkno < bdbtofsb(bp->b_blkno)) {
4932670Swnj 		bp->b_command = TM_SFORW;
4947381Ssam 		addr->tmbc = blkno - bdbtofsb(bp->b_blkno);
4951919Swnj 	} else {
4962670Swnj 		bp->b_command = TM_SREV;
4977381Ssam 		addr->tmbc = bdbtofsb(bp->b_blkno) - blkno;
4981919Swnj 	}
4993629Sroot 	sc->sc_timo = imin(imax(10 * -addr->tmbc, 60), 5 * 60);
5002670Swnj dobpcmd:
5012928Swnj #ifdef notdef
5023095Swnj 	/*
5033095Swnj 	 * It is strictly necessary to wait for the tape
5043095Swnj 	 * to stop before changing directions, but the TC11
5053095Swnj 	 * handles this for us.
5063095Swnj 	 */
5072670Swnj 	if (tmreverseop(sc->sc_lastcmd) != tmreverseop(bp->b_command))
5082670Swnj 		while (addr->tmer & TM_SDWN)
5092670Swnj 			tmgapsdcnt++;
5102670Swnj 	sc->sc_lastcmd = bp->b_command;
5112928Swnj #endif
5123095Swnj 	/*
5133095Swnj 	 * Do the command in bp.
5143095Swnj 	 */
5153095Swnj 	addr->tmcs = (sc->sc_dens | bp->b_command);
5161919Swnj 	return;
5171919Swnj 
5181919Swnj next:
5192608Swnj 	/*
5202608Swnj 	 * Done with this operation due to error or
5212608Swnj 	 * the fact that it doesn't do anything.
5222608Swnj 	 * Release UBA resources (if any), dequeue
5232608Swnj 	 * the transfer and continue processing this slave.
5242608Swnj 	 */
5252608Swnj 	if (um->um_ubinfo)
5262617Swnj 		ubadone(um);
5272608Swnj 	um->um_tab.b_errcnt = 0;
5282608Swnj 	dp->b_actf = bp->av_forw;
5291919Swnj 	iodone(bp);
5301919Swnj 	goto loop;
5311919Swnj }
5321919Swnj 
5332608Swnj /*
5342608Swnj  * The UNIBUS resources we needed have been
5352608Swnj  * allocated to us; start the device.
5362608Swnj  */
5372574Swnj tmdgo(um)
5382982Swnj 	register struct uba_ctlr *um;
5391919Swnj {
5405692Sroot 	register struct tmdevice *addr = (struct tmdevice *)um->um_addr;
5412471Swnj 
5422574Swnj 	addr->tmba = um->um_ubinfo;
5432574Swnj 	addr->tmcs = um->um_cmd | ((um->um_ubinfo >> 12) & 0x30);
5442396Swnj }
5452396Swnj 
5462608Swnj /*
5472608Swnj  * Tm interrupt routine.
5482608Swnj  */
5492471Swnj /*ARGSUSED*/
5502630Swnj tmintr(tm11)
5512630Swnj 	int tm11;
5522396Swnj {
5532608Swnj 	struct buf *dp;
5541919Swnj 	register struct buf *bp;
5552982Swnj 	register struct uba_ctlr *um = tmminfo[tm11];
5565692Sroot 	register struct tmdevice *addr;
5573095Swnj 	register struct te_softc *sc;
5583095Swnj 	int teunit;
5591919Swnj 	register state;
5601919Swnj 
5613095Swnj 	if ((dp = um->um_tab.b_actf) == NULL)
5623095Swnj 		return;
5633095Swnj 	bp = dp->b_actf;
5643095Swnj 	teunit = TEUNIT(bp->b_dev);
5655692Sroot 	addr = (struct tmdevice *)tedinfo[teunit]->ui_addr;
5663524Swnj 	sc = &te_softc[teunit];
5672608Swnj 	/*
5682608Swnj 	 * If last command was a rewind, and tape is still
5692608Swnj 	 * rewinding, wait for the rewind complete interrupt.
5702608Swnj 	 */
5712608Swnj 	if (um->um_tab.b_active == SREW) {
5722608Swnj 		um->um_tab.b_active = SCOM;
5733524Swnj 		if (addr->tmer&TMER_RWS) {
5743524Swnj 			sc->sc_timo = 5*60;		/* 5 minutes */
5752608Swnj 			return;
5763524Swnj 		}
5771919Swnj 	}
5782608Swnj 	/*
5792608Swnj 	 * An operation completed... record status
5802608Swnj 	 */
5813495Sroot 	sc->sc_timo = INF;
5822471Swnj 	sc->sc_dsreg = addr->tmcs;
5832471Swnj 	sc->sc_erreg = addr->tmer;
5842471Swnj 	sc->sc_resid = addr->tmbc;
5851919Swnj 	if ((bp->b_flags & B_READ) == 0)
5862608Swnj 		sc->sc_lastiow = 1;
5872471Swnj 	state = um->um_tab.b_active;
5882471Swnj 	um->um_tab.b_active = 0;
5892608Swnj 	/*
5902608Swnj 	 * Check for errors.
5912608Swnj 	 */
5922608Swnj 	if (addr->tmcs&TM_ERR) {
5933095Swnj 		while (addr->tmer & TMER_SDWN)
5941919Swnj 			;			/* await settle down */
5952608Swnj 		/*
5963095Swnj 		 * If we hit the end of the tape file, update our position.
5972608Swnj 		 */
5983095Swnj 		if (addr->tmer&TMER_EOF) {
5992608Swnj 			tmseteof(bp);		/* set blkno and nxrec */
6002608Swnj 			state = SCOM;		/* force completion */
6012608Swnj 			/*
6022608Swnj 			 * Stuff bc so it will be unstuffed correctly
6032608Swnj 			 * later to get resid.
6042608Swnj 			 */
6052396Swnj 			addr->tmbc = -bp->b_bcount;
6062608Swnj 			goto opdone;
6071919Swnj 		}
6082608Swnj 		/*
6093095Swnj 		 * If we were reading raw tape and the only error was that the
6103095Swnj 		 * record was too long, then we don't consider this an error.
6112608Swnj 		 */
6123095Swnj 		if (bp == &rtmbuf[TMUNIT(bp->b_dev)] && (bp->b_flags&B_READ) &&
6133095Swnj 		    (addr->tmer&(TMER_HARD|TMER_SOFT)) == TMER_RLE)
6142608Swnj 			goto ignoreerr;
6152608Swnj 		/*
6162608Swnj 		 * If error is not hard, and this was an i/o operation
6172608Swnj 		 * retry up to 8 times.
6182608Swnj 		 */
6193095Swnj 		if ((addr->tmer&TMER_HARD)==0 && state==SIO) {
6202471Swnj 			if (++um->um_tab.b_errcnt < 7) {
6212471Swnj 				sc->sc_blkno++;
6222617Swnj 				ubadone(um);
6232608Swnj 				goto opcont;
6241919Swnj 			}
6252608Swnj 		} else
6262608Swnj 			/*
6272608Swnj 			 * Hard or non-i/o errors on non-raw tape
6282608Swnj 			 * cause it to close.
6292608Swnj 			 */
6303095Swnj 			if (sc->sc_openf>0 && bp != &rtmbuf[TMUNIT(bp->b_dev)])
6312608Swnj 				sc->sc_openf = -1;
6322608Swnj 		/*
6332608Swnj 		 * Couldn't recover error
6342608Swnj 		 */
635*18321Sralph 		tprintf(sc->sc_ttyp,
636*18321Sralph 		    "te%d: hard error bn%d er=%b\n", minor(bp->b_dev)&03,
6373095Swnj 		    bp->b_blkno, sc->sc_erreg, TMER_BITS);
6381919Swnj 		bp->b_flags |= B_ERROR;
6392608Swnj 		goto opdone;
6401919Swnj 	}
6412608Swnj 	/*
6422608Swnj 	 * Advance tape control FSM.
6432608Swnj 	 */
6442608Swnj ignoreerr:
6451919Swnj 	switch (state) {
6461919Swnj 
6471919Swnj 	case SIO:
6482608Swnj 		/*
6492608Swnj 		 * Read/write increments tape block number
6502608Swnj 		 */
6512471Swnj 		sc->sc_blkno++;
6522608Swnj 		goto opdone;
6531919Swnj 
6541919Swnj 	case SCOM:
6552608Swnj 		/*
6563095Swnj 		 * For forward/backward space record update current position.
6572608Swnj 		 */
6583095Swnj 		if (bp == &ctmbuf[TMUNIT(bp->b_dev)])
6592608Swnj 		switch (bp->b_command) {
6601919Swnj 
6612608Swnj 		case TM_SFORW:
6622608Swnj 			sc->sc_blkno -= bp->b_repcnt;
6633095Swnj 			break;
6641919Swnj 
6652608Swnj 		case TM_SREV:
6662608Swnj 			sc->sc_blkno += bp->b_repcnt;
6673095Swnj 			break;
6681919Swnj 		}
6693095Swnj 		goto opdone;
6701919Swnj 
6711919Swnj 	case SSEEK:
6727381Ssam 		sc->sc_blkno = bdbtofsb(bp->b_blkno);
6732608Swnj 		goto opcont;
6741919Swnj 
6751919Swnj 	default:
6762608Swnj 		panic("tmintr");
6772608Swnj 	}
6782608Swnj opdone:
6792608Swnj 	/*
6802608Swnj 	 * Reset error count and remove
6812608Swnj 	 * from device queue.
6822608Swnj 	 */
6832608Swnj 	um->um_tab.b_errcnt = 0;
6842608Swnj 	dp->b_actf = bp->av_forw;
68514929Skarels 	/*
68614929Skarels 	 * Check resid; watch out for resid >32767 (tmbc not negative).
68714929Skarels 	 */
68815081Skarels 	bp->b_resid = ((int) -addr->tmbc) & 0xffff;
6892617Swnj 	ubadone(um);
6902608Swnj 	iodone(bp);
6912608Swnj 	/*
6922608Swnj 	 * Circulate slave to end of controller
6932608Swnj 	 * queue to give other slaves a chance.
6942608Swnj 	 */
6952608Swnj 	um->um_tab.b_actf = dp->b_forw;
6962608Swnj 	if (dp->b_actf) {
6972608Swnj 		dp->b_forw = NULL;
6982608Swnj 		if (um->um_tab.b_actf == NULL)
6992608Swnj 			um->um_tab.b_actf = dp;
7002608Swnj 		else
7012608Swnj 			um->um_tab.b_actl->b_forw = dp;
7022608Swnj 		um->um_tab.b_actl = dp;
7032608Swnj 	}
7042608Swnj 	if (um->um_tab.b_actf == 0)
7051919Swnj 		return;
7062608Swnj opcont:
7072608Swnj 	tmstart(um);
7081919Swnj }
7091919Swnj 
7103495Sroot tmtimer(dev)
7113495Sroot 	int dev;
7123495Sroot {
7133495Sroot 	register struct te_softc *sc = &te_softc[TEUNIT(dev)];
7144847Sroot 	register short x;
7153495Sroot 
7163495Sroot 	if (sc->sc_timo != INF && (sc->sc_timo -= 5) < 0) {
7174278Sroot 		printf("te%d: lost interrupt\n", TEUNIT(dev));
7183495Sroot 		sc->sc_timo = INF;
7194847Sroot 		x = spl5();
7203495Sroot 		tmintr(TMUNIT(dev));
7214847Sroot 		(void) splx(x);
7223495Sroot 	}
7233629Sroot 	timeout(tmtimer, (caddr_t)dev, 5*hz);
7243495Sroot }
7253495Sroot 
7261919Swnj tmseteof(bp)
7271919Swnj 	register struct buf *bp;
7281919Swnj {
7293095Swnj 	register int teunit = TEUNIT(bp->b_dev);
7305692Sroot 	register struct tmdevice *addr =
7315692Sroot 	    (struct tmdevice *)tedinfo[teunit]->ui_addr;
7323095Swnj 	register struct te_softc *sc = &te_softc[teunit];
7331919Swnj 
7343095Swnj 	if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) {
7357381Ssam 		if (sc->sc_blkno > bdbtofsb(bp->b_blkno)) {
7361919Swnj 			/* reversing */
7377381Ssam 			sc->sc_nxrec = bdbtofsb(bp->b_blkno) - addr->tmbc;
7382471Swnj 			sc->sc_blkno = sc->sc_nxrec;
7391919Swnj 		} else {
7401919Swnj 			/* spacing forward */
7417381Ssam 			sc->sc_blkno = bdbtofsb(bp->b_blkno) + addr->tmbc;
7422471Swnj 			sc->sc_nxrec = sc->sc_blkno - 1;
7431919Swnj 		}
7441919Swnj 		return;
7451919Swnj 	}
7461919Swnj 	/* eof on read */
7477381Ssam 	sc->sc_nxrec = bdbtofsb(bp->b_blkno);
7481919Swnj }
7491919Swnj 
7507732Sroot tmread(dev, uio)
7512608Swnj 	dev_t dev;
7527732Sroot 	struct uio *uio;
7531919Swnj {
7548163Sroot 	int errno;
7551919Swnj 
7568163Sroot 	errno = tmphys(dev, uio);
7578163Sroot 	if (errno)
7588163Sroot 		return (errno);
7598163Sroot 	return (physio(tmstrategy, &rtmbuf[TMUNIT(dev)], dev, B_READ, minphys, uio));
7601919Swnj }
7611919Swnj 
7627838Sroot tmwrite(dev, uio)
7632608Swnj 	dev_t dev;
7647838Sroot 	struct uio *uio;
7651919Swnj {
7668163Sroot 	int errno;
7671919Swnj 
7688163Sroot 	errno = tmphys(dev, uio);
7698163Sroot 	if (errno)
7708163Sroot 		return (errno);
7718163Sroot 	return (physio(tmstrategy, &rtmbuf[TMUNIT(dev)], dev, B_WRITE, minphys, uio));
7721919Swnj }
7731919Swnj 
7743095Swnj /*
7753095Swnj  * Check that a raw device exists.
7763095Swnj  * If it does, set up sc_blkno and sc_nxrec
7773095Swnj  * so that the tape will appear positioned correctly.
7783095Swnj  */
7797732Sroot tmphys(dev, uio)
7802608Swnj 	dev_t dev;
7817732Sroot 	struct uio *uio;
7821919Swnj {
7833095Swnj 	register int teunit = TEUNIT(dev);
7841919Swnj 	register daddr_t a;
7853095Swnj 	register struct te_softc *sc;
7863095Swnj 	register struct uba_device *ui;
7871919Swnj 
7887838Sroot 	if (teunit >= NTE || (ui=tedinfo[teunit]) == 0 || ui->ui_alive == 0)
7897732Sroot 		return (ENXIO);
7903095Swnj 	sc = &te_softc[teunit];
7917838Sroot 	a = bdbtofsb(uio->uio_offset >> 9);
7922471Swnj 	sc->sc_blkno = a;
7932471Swnj 	sc->sc_nxrec = a + 1;
7947732Sroot 	return (0);
7951919Swnj }
7961919Swnj 
7972608Swnj tmreset(uban)
7982608Swnj 	int uban;
7992608Swnj {
8002982Swnj 	register struct uba_ctlr *um;
8013095Swnj 	register tm11, teunit;
8022982Swnj 	register struct uba_device *ui;
8032608Swnj 	register struct buf *dp;
8042608Swnj 
8052630Swnj 	for (tm11 = 0; tm11 < NTM; tm11++) {
8062630Swnj 		if ((um = tmminfo[tm11]) == 0 || um->um_alive == 0 ||
8072608Swnj 		   um->um_ubanum != uban)
8082608Swnj 			continue;
8092928Swnj 		printf(" tm%d", tm11);
8102608Swnj 		um->um_tab.b_active = 0;
8112608Swnj 		um->um_tab.b_actf = um->um_tab.b_actl = 0;
8122608Swnj 		if (um->um_ubinfo) {
8132608Swnj 			printf("<%d>", (um->um_ubinfo>>28)&0xf);
8149355Ssam 			um->um_ubinfo = 0;
8152608Swnj 		}
8165692Sroot 		((struct tmdevice *)(um->um_addr))->tmcs = TM_DCLR;
8173095Swnj 		for (teunit = 0; teunit < NTE; teunit++) {
8183095Swnj 			if ((ui = tedinfo[teunit]) == 0 || ui->ui_mi != um ||
8193095Swnj 			    ui->ui_alive == 0)
8202608Swnj 				continue;
8213095Swnj 			dp = &teutab[teunit];
8222608Swnj 			dp->b_active = 0;
8232608Swnj 			dp->b_forw = 0;
8242608Swnj 			if (um->um_tab.b_actf == NULL)
8252608Swnj 				um->um_tab.b_actf = dp;
8262608Swnj 			else
8272608Swnj 				um->um_tab.b_actl->b_forw = dp;
8282608Swnj 			um->um_tab.b_actl = dp;
8293495Sroot 			if (te_softc[teunit].sc_openf > 0)
8303495Sroot 				te_softc[teunit].sc_openf = -1;
8312608Swnj 		}
8322608Swnj 		tmstart(um);
8332608Swnj 	}
8342608Swnj }
8352608Swnj 
8361919Swnj /*ARGSUSED*/
8377632Ssam tmioctl(dev, cmd, data, flag)
8387632Ssam 	caddr_t data;
8391919Swnj 	dev_t dev;
8401919Swnj {
8413095Swnj 	int teunit = TEUNIT(dev);
8423095Swnj 	register struct te_softc *sc = &te_softc[teunit];
8433095Swnj 	register struct buf *bp = &ctmbuf[TMUNIT(dev)];
8441919Swnj 	register callcount;
8451919Swnj 	int fcount;
8467632Ssam 	struct mtop *mtop;
8477632Ssam 	struct mtget *mtget;
8481919Swnj 	/* we depend of the values and order of the MT codes here */
8492608Swnj 	static tmops[] =
8502608Swnj 	   {TM_WEOF,TM_SFORW,TM_SREV,TM_SFORW,TM_SREV,TM_REW,TM_OFFL,TM_SENSE};
8511919Swnj 
8522608Swnj 	switch (cmd) {
8537632Ssam 
8547632Ssam 	case MTIOCTOP:	/* tape operation */
8557632Ssam 		mtop = (struct mtop *)data;
8568574Sroot 		switch (mtop->mt_op) {
8577632Ssam 
8582608Swnj 		case MTWEOF:
8597632Ssam 			callcount = mtop->mt_count;
8602608Swnj 			fcount = 1;
8612608Swnj 			break;
8627632Ssam 
8632608Swnj 		case MTFSF: case MTBSF:
8647632Ssam 			callcount = mtop->mt_count;
8651919Swnj 			fcount = INF;
8661919Swnj 			break;
8677632Ssam 
8681919Swnj 		case MTFSR: case MTBSR:
8691919Swnj 			callcount = 1;
8707632Ssam 			fcount = mtop->mt_count;
8711919Swnj 			break;
8727632Ssam 
8732324Skre 		case MTREW: case MTOFFL: case MTNOP:
8741919Swnj 			callcount = 1;
8751919Swnj 			fcount = 1;
8761919Swnj 			break;
8777632Ssam 
8781919Swnj 		default:
8798574Sroot 			return (ENXIO);
8801919Swnj 		}
8818574Sroot 		if (callcount <= 0 || fcount <= 0)
8828574Sroot 			return (EINVAL);
8832608Swnj 		while (--callcount >= 0) {
8847632Ssam 			tmcommand(dev, tmops[mtop->mt_op], fcount);
8857632Ssam 			if ((mtop->mt_op == MTFSR || mtop->mt_op == MTBSR) &&
8868574Sroot 			    bp->b_resid)
8878574Sroot 				return (EIO);
8883095Swnj 			if ((bp->b_flags&B_ERROR) || sc->sc_erreg&TMER_BOT)
8891919Swnj 				break;
8901919Swnj 		}
8918648Sroot 		return (geterror(bp));
8927632Ssam 
8931919Swnj 	case MTIOCGET:
8947632Ssam 		mtget = (struct mtget *)data;
8957632Ssam 		mtget->mt_dsreg = sc->sc_dsreg;
8967632Ssam 		mtget->mt_erreg = sc->sc_erreg;
8977632Ssam 		mtget->mt_resid = sc->sc_resid;
8987632Ssam 		mtget->mt_type = MT_ISTM;
8998574Sroot 		break;
9007632Ssam 
9011919Swnj 	default:
9028574Sroot 		return (ENXIO);
9031919Swnj 	}
9048574Sroot 	return (0);
9051919Swnj }
9061919Swnj 
9071919Swnj #define	DBSIZE	20
9081919Swnj 
9092363Swnj tmdump()
9102363Swnj {
9112982Swnj 	register struct uba_device *ui;
9122396Swnj 	register struct uba_regs *up;
9135692Sroot 	register struct tmdevice *addr;
9142426Skre 	int blk, num;
9152426Skre 	int start;
9161919Swnj 
9172426Skre 	start = 0;
9182426Skre 	num = maxfree;
9192426Skre #define	phys(a,b)	((b)((int)(a)&0x7fffffff))
9203095Swnj 	if (tedinfo[0] == 0)
9212887Swnj 		return (ENXIO);
9223095Swnj 	ui = phys(tedinfo[0], struct uba_device *);
9232396Swnj 	up = phys(ui->ui_hd, struct uba_hd *)->uh_physuba;
9243331Swnj 	ubainit(up);
9252324Skre 	DELAY(1000000);
9265692Sroot 	addr = (struct tmdevice *)ui->ui_physaddr;
9272396Swnj 	tmwait(addr);
9282608Swnj 	addr->tmcs = TM_DCLR | TM_GO;
9291919Swnj 	while (num > 0) {
9301919Swnj 		blk = num > DBSIZE ? DBSIZE : num;
9312396Swnj 		tmdwrite(start, blk, addr, up);
9321919Swnj 		start += blk;
9331919Swnj 		num -= blk;
9341919Swnj 	}
9352426Skre 	tmeof(addr);
9362426Skre 	tmeof(addr);
9372426Skre 	tmwait(addr);
9382887Swnj 	if (addr->tmcs&TM_ERR)
9392887Swnj 		return (EIO);
9402608Swnj 	addr->tmcs = TM_REW | TM_GO;
9412471Swnj 	tmwait(addr);
9422363Swnj 	return (0);
9431919Swnj }
9441919Swnj 
9452608Swnj tmdwrite(dbuf, num, addr, up)
9462608Swnj 	register dbuf, num;
9475692Sroot 	register struct tmdevice *addr;
9482396Swnj 	struct uba_regs *up;
9491919Swnj {
9502396Swnj 	register struct pte *io;
9512396Swnj 	register int npf;
9521928Swnj 
9532396Swnj 	tmwait(addr);
9542396Swnj 	io = up->uba_map;
9551919Swnj 	npf = num+1;
9561928Swnj 	while (--npf != 0)
9572982Swnj 		 *(int *)io++ = (dbuf++ | (1<<UBAMR_DPSHIFT) | UBAMR_MRV);
9582396Swnj 	*(int *)io = 0;
9592396Swnj 	addr->tmbc = -(num*NBPG);
9602396Swnj 	addr->tmba = 0;
9612608Swnj 	addr->tmcs = TM_WCOM | TM_GO;
9621919Swnj }
9631919Swnj 
9642396Swnj tmwait(addr)
9655692Sroot 	register struct tmdevice *addr;
9661919Swnj {
9671928Swnj 	register s;
9681919Swnj 
9691919Swnj 	do
9702396Swnj 		s = addr->tmcs;
9712608Swnj 	while ((s & TM_CUR) == 0);
9721919Swnj }
9731919Swnj 
9742396Swnj tmeof(addr)
9755692Sroot 	struct tmdevice *addr;
9761919Swnj {
9771919Swnj 
9782396Swnj 	tmwait(addr);
9792608Swnj 	addr->tmcs = TM_WEOF | TM_GO;
9801919Swnj }
9811919Swnj #endif
982