xref: /csrg-svn/sys/vax/uba/tm.c (revision 17079)
1*17079Sbloom /*	tm.c	6.4	84/08/29	*/
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 
18*17079Sbloom #include "param.h"
19*17079Sbloom #include "systm.h"
20*17079Sbloom #include "buf.h"
21*17079Sbloom #include "dir.h"
22*17079Sbloom #include "conf.h"
23*17079Sbloom #include "user.h"
24*17079Sbloom #include "file.h"
25*17079Sbloom #include "map.h"
26*17079Sbloom #include "vm.h"
27*17079Sbloom #include "ioctl.h"
28*17079Sbloom #include "mtio.h"
29*17079Sbloom #include "cmap.h"
30*17079Sbloom #include "uio.h"
31*17079Sbloom #include "kernel.h"
321919Swnj 
338479Sroot #include "../vax/cpu.h"
34*17079Sbloom #include "ubareg.h"
35*17079Sbloom #include "ubavar.h"
36*17079Sbloom #include "tmreg.h"
371919Swnj 
383095Swnj /*
393095Swnj  * There is a ctmbuf per tape controller.
403095Swnj  * It is used as the token to pass to the internal routines
413095Swnj  * to execute tape ioctls, and also acts as a lock on the slaves
423095Swnj  * on the controller, since there is only one per controller.
433095Swnj  * In particular, when the tape is rewinding on close we release
443095Swnj  * the user process but any further attempts to use the tape drive
453095Swnj  * before the rewind completes will hang waiting for ctmbuf.
463095Swnj  */
473095Swnj struct	buf	ctmbuf[NTM];
481919Swnj 
493095Swnj /*
503095Swnj  * Raw tape operations use rtmbuf.  The driver
513095Swnj  * notices when rtmbuf is being used and allows the user
523095Swnj  * program to continue after errors and read records
533095Swnj  * not of the standard length (BSIZE).
543095Swnj  */
553095Swnj struct	buf	rtmbuf[NTM];
563095Swnj 
573095Swnj /*
583095Swnj  * Driver unibus interface routines and variables.
593095Swnj  */
602608Swnj int	tmprobe(), tmslave(), tmattach(), tmdgo(), tmintr();
612982Swnj struct	uba_ctlr *tmminfo[NTM];
623095Swnj struct	uba_device *tedinfo[NTE];
633095Swnj struct	buf teutab[NTE];
643095Swnj short	tetotm[NTE];
652458Swnj u_short	tmstd[] = { 0772520, 0 };
662396Swnj struct	uba_driver tmdriver =
673095Swnj  { tmprobe, tmslave, tmattach, tmdgo, tmstd, "te", tedinfo, "tm", tmminfo, 0 };
681919Swnj 
691919Swnj /* bits in minor device */
703095Swnj #define	TEUNIT(dev)	(minor(dev)&03)
713095Swnj #define	TMUNIT(dev)	(tetotm[TEUNIT(dev)])
721919Swnj #define	T_NOREWIND	04
731919Swnj #define	T_1600BPI	08
741919Swnj 
751919Swnj #define	INF	(daddr_t)1000000L
761919Swnj 
772608Swnj /*
782608Swnj  * Software state per tape transport.
793095Swnj  *
803095Swnj  * 1. A tape drive is a unique-open device; we refuse opens when it is already.
813095Swnj  * 2. We keep track of the current position on a block tape and seek
823095Swnj  *    before operations by forward/back spacing if necessary.
833095Swnj  * 3. We remember if the last operation was a write on a tape, so if a tape
843095Swnj  *    is open read write and the last thing done is a write we can
853095Swnj  *    write a standard end of tape mark (two eofs).
863095Swnj  * 4. We remember the status registers after the last command, using
873095Swnj  *    then internally and returning them to the SENSE ioctl.
883095Swnj  * 5. We remember the last density the tape was used at.  If it is
893095Swnj  *    not a BOT when we start using it and we are writing, we don't
903095Swnj  *    let the density be changed.
912608Swnj  */
923095Swnj struct	te_softc {
932608Swnj 	char	sc_openf;	/* lock against multiple opens */
942608Swnj 	char	sc_lastiow;	/* last op was a write */
952608Swnj 	daddr_t	sc_blkno;	/* block number, for block device tape */
963095Swnj 	daddr_t	sc_nxrec;	/* position of end of tape, if known */
972608Swnj 	u_short	sc_erreg;	/* copy of last erreg */
982608Swnj 	u_short	sc_dsreg;	/* copy of last dsreg */
992608Swnj 	short	sc_resid;	/* copy of last bc */
1003105Swnj #ifdef unneeded
1012670Swnj 	short	sc_lastcmd;	/* last command to handle direction changes */
1022928Swnj #endif
1033095Swnj 	u_short	sc_dens;	/* prototype command with density info */
1043495Sroot 	daddr_t	sc_timo;	/* time until timeout expires */
1053495Sroot 	short	sc_tact;	/* timeout is active */
1066952Swnj } te_softc[NTE];
1073105Swnj #ifdef unneeded
1083105Swnj int	tmgapsdcnt;		/* DEBUG */
1093105Swnj #endif
1101919Swnj 
1112608Swnj /*
1123095Swnj  * States for um->um_tab.b_active, the per controller state flag.
1133095Swnj  * This is used to sequence control in the driver.
1142608Swnj  */
1151919Swnj #define	SSEEK	1		/* seeking */
1161919Swnj #define	SIO	2		/* doing seq i/o */
1171919Swnj #define	SCOM	3		/* sending control command */
1182608Swnj #define	SREW	4		/* sending a drive rewind */
1191919Swnj 
1202426Skre /*
1212426Skre  * Determine if there is a controller for
1222426Skre  * a tm at address reg.  Our goal is to make the
1232426Skre  * device interrupt.
1242426Skre  */
1252608Swnj tmprobe(reg)
1262396Swnj 	caddr_t reg;
1272396Swnj {
1283095Swnj 	register int br, cvec;		/* must be r11,r10; value-result */
1292426Skre 
1302608Swnj #ifdef lint
1313105Swnj 	br = 0; cvec = br; br = cvec;
1324936Swnj 	tmintr(0);
1332608Swnj #endif
1345692Sroot 	((struct tmdevice *)reg)->tmcs = TM_IE;
1352396Swnj 	/*
1362630Swnj 	 * If this is a tm11, it ought to have interrupted
1372396Swnj 	 * by now, if it isn't (ie: it is a ts04) then we just
1382458Swnj 	 * hope that it didn't interrupt, so autoconf will ignore it.
1392458Swnj 	 * Just in case, we will reference one
1402396Swnj 	 * of the more distant registers, and hope for a machine
1412458Swnj 	 * check, or similar disaster if this is a ts.
1422471Swnj 	 *
1432471Swnj 	 * Note: on an 11/780, badaddr will just generate
1442471Swnj 	 * a uba error for a ts; but our caller will notice that
1452471Swnj 	 * so we won't check for it.
1462396Swnj 	 */
1475692Sroot 	if (badaddr((caddr_t)&((struct tmdevice *)reg)->tmrd, 2))
1482458Swnj 		return (0);
1497404Skre 	return (sizeof (struct tmdevice));
1502396Swnj }
1512396Swnj 
1522608Swnj /*
1532608Swnj  * Due to a design flaw, we cannot ascertain if the tape
1542608Swnj  * exists or not unless it is on line - ie: unless a tape is
1552608Swnj  * mounted. This is too servere a restriction to bear,
1562608Swnj  * so all units are assumed to exist.
1572608Swnj  */
1582608Swnj /*ARGSUSED*/
1592574Swnj tmslave(ui, reg)
1602982Swnj 	struct uba_device *ui;
1612396Swnj 	caddr_t reg;
1622396Swnj {
1632458Swnj 
1642458Swnj 	return (1);
1652396Swnj }
1662396Swnj 
1672608Swnj /*
1683095Swnj  * Record attachment of the unit to the controller.
1692608Swnj  */
1702608Swnj /*ARGSUSED*/
1712608Swnj tmattach(ui)
1722982Swnj 	struct uba_device *ui;
1732608Swnj {
1743095Swnj 	/*
1753095Swnj 	 * Tetotm is used in TMUNIT to index the ctmbuf and rtmbuf
1763095Swnj 	 * arrays given a te unit number.
1773095Swnj 	 */
1783095Swnj 	tetotm[ui->ui_unit] = ui->ui_mi->um_ctlr;
1792608Swnj }
1802608Swnj 
1813495Sroot int	tmtimer();
1822608Swnj /*
1832608Swnj  * Open the device.  Tapes are unique open
1842608Swnj  * devices, so we refuse if it is already open.
1852608Swnj  * We also check that a tape is available, and
1863095Swnj  * don't block waiting here; if you want to wait
1873095Swnj  * for a tape you should timeout in user code.
1882608Swnj  */
1891919Swnj tmopen(dev, flag)
1901919Swnj 	dev_t dev;
1911919Swnj 	int flag;
1921919Swnj {
1933095Swnj 	register int teunit;
1942982Swnj 	register struct uba_device *ui;
1953095Swnj 	register struct te_softc *sc;
1963209Swnj 	int olddens, dens;
1975437Sroot 	int s;
1981919Swnj 
1993095Swnj 	teunit = TEUNIT(dev);
2003095Swnj 	if (teunit>=NTE || (sc = &te_softc[teunit])->sc_openf ||
2018574Sroot 	    (ui = tedinfo[teunit]) == 0 || ui->ui_alive == 0)
2028574Sroot 		return (ENXIO);
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);
2178574Sroot 		return (EIO);
2181919Swnj 	}
2193710Sroot 	if ((flag&FWRITE) && (sc->sc_erreg&TMER_WRL)) {
2203710Sroot 		uprintf("te%d: no write ring\n", teunit);
2218574Sroot 		return (EIO);
2223710Sroot 	}
2233710Sroot 	if ((sc->sc_erreg&TMER_BOT) == 0 && (flag&FWRITE) &&
2243710Sroot 	    dens != sc->sc_dens) {
2253710Sroot 		uprintf("te%d: can't change density in mid-tape\n", teunit);
2268574Sroot 		return (EIO);
2273710Sroot 	}
2282608Swnj 	sc->sc_openf = 1;
2292471Swnj 	sc->sc_blkno = (daddr_t)0;
2302471Swnj 	sc->sc_nxrec = INF;
2312608Swnj 	sc->sc_lastiow = 0;
2323095Swnj 	sc->sc_dens = dens;
2335437Sroot 	s = spl6();
2343495Sroot 	if (sc->sc_tact == 0) {
2353495Sroot 		sc->sc_timo = INF;
2363495Sroot 		sc->sc_tact = 1;
2373629Sroot 		timeout(tmtimer, (caddr_t)dev, 5*hz);
2383495Sroot 	}
2395437Sroot 	splx(s);
2408574Sroot 	return (0);
2411919Swnj }
2421919Swnj 
2432608Swnj /*
2442608Swnj  * Close tape device.
2452608Swnj  *
2462608Swnj  * If tape was open for writing or last operation was
2472608Swnj  * a write, then write two EOF's and backspace over the last one.
2482608Swnj  * Unless this is a non-rewinding special file, rewind the tape.
2492608Swnj  * Make the tape available to others.
2502608Swnj  */
2511919Swnj tmclose(dev, flag)
2521919Swnj 	register dev_t dev;
2531919Swnj 	register flag;
2541919Swnj {
2553095Swnj 	register struct te_softc *sc = &te_softc[TEUNIT(dev)];
2561919Swnj 
2572608Swnj 	if (flag == FWRITE || (flag&FWRITE) && sc->sc_lastiow) {
2582608Swnj 		tmcommand(dev, TM_WEOF, 1);
2592608Swnj 		tmcommand(dev, TM_WEOF, 1);
2602608Swnj 		tmcommand(dev, TM_SREV, 1);
2611919Swnj 	}
2621919Swnj 	if ((minor(dev)&T_NOREWIND) == 0)
2633095Swnj 		/*
2643095Swnj 		 * 0 count means don't hang waiting for rewind complete
2653095Swnj 		 * rather ctmbuf stays busy until the operation completes
2663095Swnj 		 * preventing further opens from completing by
2673095Swnj 		 * preventing a TM_SENSE from completing.
2683095Swnj 		 */
2693095Swnj 		tmcommand(dev, TM_REW, 0);
2702471Swnj 	sc->sc_openf = 0;
2711919Swnj }
2721919Swnj 
2732608Swnj /*
2742608Swnj  * Execute a command on the tape drive
2752608Swnj  * a specified number of times.
2762608Swnj  */
2772574Swnj tmcommand(dev, com, count)
2781919Swnj 	dev_t dev;
2791919Swnj 	int com, count;
2801919Swnj {
2811919Swnj 	register struct buf *bp;
2825437Sroot 	register int s;
2831919Swnj 
2842608Swnj 	bp = &ctmbuf[TMUNIT(dev)];
2855437Sroot 	s = spl5();
2861919Swnj 	while (bp->b_flags&B_BUSY) {
2873095Swnj 		/*
2883095Swnj 		 * This special check is because B_BUSY never
2893095Swnj 		 * gets cleared in the non-waiting rewind case.
2903095Swnj 		 */
2913141Swnj 		if (bp->b_repcnt == 0 && (bp->b_flags&B_DONE))
2923095Swnj 			break;
2931919Swnj 		bp->b_flags |= B_WANTED;
2941919Swnj 		sleep((caddr_t)bp, PRIBIO);
2951919Swnj 	}
2961919Swnj 	bp->b_flags = B_BUSY|B_READ;
2975437Sroot 	splx(s);
2981919Swnj 	bp->b_dev = dev;
2991919Swnj 	bp->b_repcnt = -count;
3001919Swnj 	bp->b_command = com;
3011919Swnj 	bp->b_blkno = 0;
3021919Swnj 	tmstrategy(bp);
3033095Swnj 	/*
3043095Swnj 	 * In case of rewind from close, don't wait.
3053095Swnj 	 * This is the only case where count can be 0.
3063095Swnj 	 */
3073095Swnj 	if (count == 0)
3083095Swnj 		return;
3091919Swnj 	iowait(bp);
3101919Swnj 	if (bp->b_flags&B_WANTED)
3111919Swnj 		wakeup((caddr_t)bp);
3121919Swnj 	bp->b_flags &= B_ERROR;
3131919Swnj }
3141919Swnj 
3152608Swnj /*
3163095Swnj  * Queue a tape operation.
3172608Swnj  */
3181919Swnj tmstrategy(bp)
3191919Swnj 	register struct buf *bp;
3201919Swnj {
3213095Swnj 	int teunit = TEUNIT(bp->b_dev);
3222982Swnj 	register struct uba_ctlr *um;
3232608Swnj 	register struct buf *dp;
3245437Sroot 	int s;
3251919Swnj 
3262608Swnj 	/*
3272608Swnj 	 * Put transfer at end of unit queue
3282608Swnj 	 */
3293095Swnj 	dp = &teutab[teunit];
3301919Swnj 	bp->av_forw = NULL;
3315437Sroot 	s = spl5();
3323939Sbugs 	um = tedinfo[teunit]->ui_mi;
3332608Swnj 	if (dp->b_actf == NULL) {
3342608Swnj 		dp->b_actf = bp;
3352608Swnj 		/*
3362608Swnj 		 * Transport not already active...
3372608Swnj 		 * put at end of controller queue.
3382608Swnj 		 */
3392608Swnj 		dp->b_forw = NULL;
3402608Swnj 		if (um->um_tab.b_actf == NULL)
3412608Swnj 			um->um_tab.b_actf = dp;
3422608Swnj 		else
3432608Swnj 			um->um_tab.b_actl->b_forw = dp;
3442608Swnj 		um->um_tab.b_actl = dp;
3452608Swnj 	} else
3462608Swnj 		dp->b_actl->av_forw = bp;
3472608Swnj 	dp->b_actl = bp;
3482608Swnj 	/*
3492608Swnj 	 * If the controller is not busy, get
3502608Swnj 	 * it going.
3512608Swnj 	 */
3522608Swnj 	if (um->um_tab.b_active == 0)
3532608Swnj 		tmstart(um);
3545437Sroot 	splx(s);
3551919Swnj }
3561919Swnj 
3572608Swnj /*
3582608Swnj  * Start activity on a tm controller.
3592608Swnj  */
3602608Swnj tmstart(um)
3612982Swnj 	register struct uba_ctlr *um;
3621919Swnj {
3632608Swnj 	register struct buf *bp, *dp;
3645692Sroot 	register struct tmdevice *addr = (struct tmdevice *)um->um_addr;
3653095Swnj 	register struct te_softc *sc;
3662982Swnj 	register struct uba_device *ui;
3673095Swnj 	int teunit, cmd;
3682471Swnj 	daddr_t blkno;
3691919Swnj 
3702608Swnj 	/*
3712608Swnj 	 * Look for an idle transport on the controller.
3722608Swnj 	 */
3731919Swnj loop:
3742608Swnj 	if ((dp = um->um_tab.b_actf) == NULL)
3751919Swnj 		return;
3762608Swnj 	if ((bp = dp->b_actf) == NULL) {
3772608Swnj 		um->um_tab.b_actf = dp->b_forw;
3782608Swnj 		goto loop;
3792608Swnj 	}
3803095Swnj 	teunit = TEUNIT(bp->b_dev);
3813095Swnj 	ui = tedinfo[teunit];
3822608Swnj 	/*
3832608Swnj 	 * Record pre-transfer status (e.g. for TM_SENSE)
3842608Swnj 	 */
3853095Swnj 	sc = &te_softc[teunit];
3865692Sroot 	addr = (struct tmdevice *)um->um_addr;
3872608Swnj 	addr->tmcs = (ui->ui_slave << 8);
3882471Swnj 	sc->sc_dsreg = addr->tmcs;
3892471Swnj 	sc->sc_erreg = addr->tmer;
3902471Swnj 	sc->sc_resid = addr->tmbc;
3912608Swnj 	/*
3922608Swnj 	 * Default is that last command was NOT a write command;
3932608Swnj 	 * if we do a write command we will notice this in tmintr().
3942608Swnj 	 */
3953493Sroot 	sc->sc_lastiow = 0;
3962608Swnj 	if (sc->sc_openf < 0 || (addr->tmcs&TM_CUR) == 0) {
3972608Swnj 		/*
3983095Swnj 		 * Have had a hard error on a non-raw tape
3993095Swnj 		 * or the tape unit is now unavailable
4003095Swnj 		 * (e.g. taken off line).
4012608Swnj 		 */
4022608Swnj 		bp->b_flags |= B_ERROR;
4031919Swnj 		goto next;
4041919Swnj 	}
4053095Swnj 	if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) {
4063095Swnj 		/*
4073095Swnj 		 * Execute control operation with the specified count.
4083095Swnj 		 */
4092608Swnj 		if (bp->b_command == TM_SENSE)
4102608Swnj 			goto next;
4113495Sroot 		/*
4123495Sroot 		 * Set next state; give 5 minutes to complete
4133495Sroot 		 * rewind, or 10 seconds per iteration (minimum 60
4143629Sroot 		 * seconds and max 5 minutes) to complete other ops.
4153495Sroot 		 */
4163495Sroot 		if (bp->b_command == TM_REW) {
4173495Sroot 			um->um_tab.b_active = SREW;
4183495Sroot 			sc->sc_timo = 5 * 60;
4193495Sroot 		} else {
4203495Sroot 			um->um_tab.b_active = SCOM;
4214266Swnj 			sc->sc_timo =
4224266Swnj 			    imin(imax(10*(int)-bp->b_repcnt,60),5*60);
4233495Sroot 		}
4242608Swnj 		if (bp->b_command == TM_SFORW || bp->b_command == TM_SREV)
4252608Swnj 			addr->tmbc = bp->b_repcnt;
4262670Swnj 		goto dobpcmd;
4272608Swnj 	}
4282608Swnj 	/*
4293095Swnj 	 * The following checks handle boundary cases for operation
4303095Swnj 	 * on non-raw tapes.  On raw tapes the initialization of
4313095Swnj 	 * sc->sc_nxrec by tmphys causes them to be skipped normally
4323095Swnj 	 * (except in the case of retries).
4333095Swnj 	 */
4347381Ssam 	if (bdbtofsb(bp->b_blkno) > sc->sc_nxrec) {
4353095Swnj 		/*
4363095Swnj 		 * Can't read past known end-of-file.
4373095Swnj 		 */
4383095Swnj 		bp->b_flags |= B_ERROR;
4393095Swnj 		bp->b_error = ENXIO;
4403095Swnj 		goto next;
4413095Swnj 	}
4427381Ssam 	if (bdbtofsb(bp->b_blkno) == sc->sc_nxrec &&
4433095Swnj 	    bp->b_flags&B_READ) {
4443095Swnj 		/*
4453095Swnj 		 * Reading at end of file returns 0 bytes.
4463095Swnj 		 */
4473095Swnj 		bp->b_resid = bp->b_bcount;
4483095Swnj 		clrbuf(bp);
4493095Swnj 		goto next;
4503095Swnj 	}
4513095Swnj 	if ((bp->b_flags&B_READ) == 0)
4523095Swnj 		/*
4533095Swnj 		 * Writing sets EOF
4543095Swnj 		 */
4557381Ssam 		sc->sc_nxrec = bdbtofsb(bp->b_blkno) + 1;
4563095Swnj 	/*
4572608Swnj 	 * If the data transfer command is in the correct place,
4582608Swnj 	 * set up all the registers except the csr, and give
4592608Swnj 	 * control over to the UNIBUS adapter routines, to
4602608Swnj 	 * wait for resources to start the i/o.
4612608Swnj 	 */
4627381Ssam 	if ((blkno = sc->sc_blkno) == bdbtofsb(bp->b_blkno)) {
4632396Swnj 		addr->tmbc = -bp->b_bcount;
4641919Swnj 		if ((bp->b_flags&B_READ) == 0) {
4652471Swnj 			if (um->um_tab.b_errcnt)
4663095Swnj 				cmd = TM_WIRG;
4671919Swnj 			else
4683095Swnj 				cmd = TM_WCOM;
4691919Swnj 		} else
4703095Swnj 			cmd = TM_RCOM;
4712471Swnj 		um->um_tab.b_active = SIO;
4723095Swnj 		um->um_cmd = sc->sc_dens|cmd;
4732928Swnj #ifdef notdef
4742670Swnj 		if (tmreverseop(sc->sc_lastcmd))
4753095Swnj 			while (addr->tmer & TMER_SDWN)
4762670Swnj 				tmgapsdcnt++;
4772670Swnj 		sc->sc_lastcmd = TM_RCOM;		/* will serve */
4782928Swnj #endif
4793495Sroot 		sc->sc_timo = 60;	/* premature, but should serve */
4803105Swnj 		(void) ubago(ui);
4811919Swnj 		return;
4821919Swnj 	}
4832608Swnj 	/*
4843095Swnj 	 * Tape positioned incorrectly;
4853095Swnj 	 * set to seek forwards or backwards to the correct spot.
4863095Swnj 	 * This happens for raw tapes only on error retries.
4872608Swnj 	 */
4882471Swnj 	um->um_tab.b_active = SSEEK;
4897381Ssam 	if (blkno < bdbtofsb(bp->b_blkno)) {
4902670Swnj 		bp->b_command = TM_SFORW;
4917381Ssam 		addr->tmbc = blkno - bdbtofsb(bp->b_blkno);
4921919Swnj 	} else {
4932670Swnj 		bp->b_command = TM_SREV;
4947381Ssam 		addr->tmbc = bdbtofsb(bp->b_blkno) - blkno;
4951919Swnj 	}
4963629Sroot 	sc->sc_timo = imin(imax(10 * -addr->tmbc, 60), 5 * 60);
4972670Swnj dobpcmd:
4982928Swnj #ifdef notdef
4993095Swnj 	/*
5003095Swnj 	 * It is strictly necessary to wait for the tape
5013095Swnj 	 * to stop before changing directions, but the TC11
5023095Swnj 	 * handles this for us.
5033095Swnj 	 */
5042670Swnj 	if (tmreverseop(sc->sc_lastcmd) != tmreverseop(bp->b_command))
5052670Swnj 		while (addr->tmer & TM_SDWN)
5062670Swnj 			tmgapsdcnt++;
5072670Swnj 	sc->sc_lastcmd = bp->b_command;
5082928Swnj #endif
5093095Swnj 	/*
5103095Swnj 	 * Do the command in bp.
5113095Swnj 	 */
5123095Swnj 	addr->tmcs = (sc->sc_dens | bp->b_command);
5131919Swnj 	return;
5141919Swnj 
5151919Swnj next:
5162608Swnj 	/*
5172608Swnj 	 * Done with this operation due to error or
5182608Swnj 	 * the fact that it doesn't do anything.
5192608Swnj 	 * Release UBA resources (if any), dequeue
5202608Swnj 	 * the transfer and continue processing this slave.
5212608Swnj 	 */
5222608Swnj 	if (um->um_ubinfo)
5232617Swnj 		ubadone(um);
5242608Swnj 	um->um_tab.b_errcnt = 0;
5252608Swnj 	dp->b_actf = bp->av_forw;
5261919Swnj 	iodone(bp);
5271919Swnj 	goto loop;
5281919Swnj }
5291919Swnj 
5302608Swnj /*
5312608Swnj  * The UNIBUS resources we needed have been
5322608Swnj  * allocated to us; start the device.
5332608Swnj  */
5342574Swnj tmdgo(um)
5352982Swnj 	register struct uba_ctlr *um;
5361919Swnj {
5375692Sroot 	register struct tmdevice *addr = (struct tmdevice *)um->um_addr;
5382471Swnj 
5392574Swnj 	addr->tmba = um->um_ubinfo;
5402574Swnj 	addr->tmcs = um->um_cmd | ((um->um_ubinfo >> 12) & 0x30);
5412396Swnj }
5422396Swnj 
5432608Swnj /*
5442608Swnj  * Tm interrupt routine.
5452608Swnj  */
5462471Swnj /*ARGSUSED*/
5472630Swnj tmintr(tm11)
5482630Swnj 	int tm11;
5492396Swnj {
5502608Swnj 	struct buf *dp;
5511919Swnj 	register struct buf *bp;
5522982Swnj 	register struct uba_ctlr *um = tmminfo[tm11];
5535692Sroot 	register struct tmdevice *addr;
5543095Swnj 	register struct te_softc *sc;
5553095Swnj 	int teunit;
5561919Swnj 	register state;
5571919Swnj 
5583095Swnj 	if ((dp = um->um_tab.b_actf) == NULL)
5593095Swnj 		return;
5603095Swnj 	bp = dp->b_actf;
5613095Swnj 	teunit = TEUNIT(bp->b_dev);
5625692Sroot 	addr = (struct tmdevice *)tedinfo[teunit]->ui_addr;
5633524Swnj 	sc = &te_softc[teunit];
5642608Swnj 	/*
5652608Swnj 	 * If last command was a rewind, and tape is still
5662608Swnj 	 * rewinding, wait for the rewind complete interrupt.
5672608Swnj 	 */
5682608Swnj 	if (um->um_tab.b_active == SREW) {
5692608Swnj 		um->um_tab.b_active = SCOM;
5703524Swnj 		if (addr->tmer&TMER_RWS) {
5713524Swnj 			sc->sc_timo = 5*60;		/* 5 minutes */
5722608Swnj 			return;
5733524Swnj 		}
5741919Swnj 	}
5752608Swnj 	/*
5762608Swnj 	 * An operation completed... record status
5772608Swnj 	 */
5783495Sroot 	sc->sc_timo = INF;
5792471Swnj 	sc->sc_dsreg = addr->tmcs;
5802471Swnj 	sc->sc_erreg = addr->tmer;
5812471Swnj 	sc->sc_resid = addr->tmbc;
5821919Swnj 	if ((bp->b_flags & B_READ) == 0)
5832608Swnj 		sc->sc_lastiow = 1;
5842471Swnj 	state = um->um_tab.b_active;
5852471Swnj 	um->um_tab.b_active = 0;
5862608Swnj 	/*
5872608Swnj 	 * Check for errors.
5882608Swnj 	 */
5892608Swnj 	if (addr->tmcs&TM_ERR) {
5903095Swnj 		while (addr->tmer & TMER_SDWN)
5911919Swnj 			;			/* await settle down */
5922608Swnj 		/*
5933095Swnj 		 * If we hit the end of the tape file, update our position.
5942608Swnj 		 */
5953095Swnj 		if (addr->tmer&TMER_EOF) {
5962608Swnj 			tmseteof(bp);		/* set blkno and nxrec */
5972608Swnj 			state = SCOM;		/* force completion */
5982608Swnj 			/*
5992608Swnj 			 * Stuff bc so it will be unstuffed correctly
6002608Swnj 			 * later to get resid.
6012608Swnj 			 */
6022396Swnj 			addr->tmbc = -bp->b_bcount;
6032608Swnj 			goto opdone;
6041919Swnj 		}
6052608Swnj 		/*
6063095Swnj 		 * If we were reading raw tape and the only error was that the
6073095Swnj 		 * record was too long, then we don't consider this an error.
6082608Swnj 		 */
6093095Swnj 		if (bp == &rtmbuf[TMUNIT(bp->b_dev)] && (bp->b_flags&B_READ) &&
6103095Swnj 		    (addr->tmer&(TMER_HARD|TMER_SOFT)) == TMER_RLE)
6112608Swnj 			goto ignoreerr;
6122608Swnj 		/*
6132608Swnj 		 * If error is not hard, and this was an i/o operation
6142608Swnj 		 * retry up to 8 times.
6152608Swnj 		 */
6163095Swnj 		if ((addr->tmer&TMER_HARD)==0 && state==SIO) {
6172471Swnj 			if (++um->um_tab.b_errcnt < 7) {
6182471Swnj 				sc->sc_blkno++;
6192617Swnj 				ubadone(um);
6202608Swnj 				goto opcont;
6211919Swnj 			}
6222608Swnj 		} else
6232608Swnj 			/*
6242608Swnj 			 * Hard or non-i/o errors on non-raw tape
6252608Swnj 			 * cause it to close.
6262608Swnj 			 */
6273095Swnj 			if (sc->sc_openf>0 && bp != &rtmbuf[TMUNIT(bp->b_dev)])
6282608Swnj 				sc->sc_openf = -1;
6292608Swnj 		/*
6302608Swnj 		 * Couldn't recover error
6312608Swnj 		 */
6322928Swnj 		printf("te%d: hard error bn%d er=%b\n", minor(bp->b_dev)&03,
6333095Swnj 		    bp->b_blkno, sc->sc_erreg, TMER_BITS);
6341919Swnj 		bp->b_flags |= B_ERROR;
6352608Swnj 		goto opdone;
6361919Swnj 	}
6372608Swnj 	/*
6382608Swnj 	 * Advance tape control FSM.
6392608Swnj 	 */
6402608Swnj ignoreerr:
6411919Swnj 	switch (state) {
6421919Swnj 
6431919Swnj 	case SIO:
6442608Swnj 		/*
6452608Swnj 		 * Read/write increments tape block number
6462608Swnj 		 */
6472471Swnj 		sc->sc_blkno++;
6482608Swnj 		goto opdone;
6491919Swnj 
6501919Swnj 	case SCOM:
6512608Swnj 		/*
6523095Swnj 		 * For forward/backward space record update current position.
6532608Swnj 		 */
6543095Swnj 		if (bp == &ctmbuf[TMUNIT(bp->b_dev)])
6552608Swnj 		switch (bp->b_command) {
6561919Swnj 
6572608Swnj 		case TM_SFORW:
6582608Swnj 			sc->sc_blkno -= bp->b_repcnt;
6593095Swnj 			break;
6601919Swnj 
6612608Swnj 		case TM_SREV:
6622608Swnj 			sc->sc_blkno += bp->b_repcnt;
6633095Swnj 			break;
6641919Swnj 		}
6653095Swnj 		goto opdone;
6661919Swnj 
6671919Swnj 	case SSEEK:
6687381Ssam 		sc->sc_blkno = bdbtofsb(bp->b_blkno);
6692608Swnj 		goto opcont;
6701919Swnj 
6711919Swnj 	default:
6722608Swnj 		panic("tmintr");
6732608Swnj 	}
6742608Swnj opdone:
6752608Swnj 	/*
6762608Swnj 	 * Reset error count and remove
6772608Swnj 	 * from device queue.
6782608Swnj 	 */
6792608Swnj 	um->um_tab.b_errcnt = 0;
6802608Swnj 	dp->b_actf = bp->av_forw;
68114929Skarels 	/*
68214929Skarels 	 * Check resid; watch out for resid >32767 (tmbc not negative).
68314929Skarels 	 */
68415081Skarels 	bp->b_resid = ((int) -addr->tmbc) & 0xffff;
6852617Swnj 	ubadone(um);
6862608Swnj 	iodone(bp);
6872608Swnj 	/*
6882608Swnj 	 * Circulate slave to end of controller
6892608Swnj 	 * queue to give other slaves a chance.
6902608Swnj 	 */
6912608Swnj 	um->um_tab.b_actf = dp->b_forw;
6922608Swnj 	if (dp->b_actf) {
6932608Swnj 		dp->b_forw = NULL;
6942608Swnj 		if (um->um_tab.b_actf == NULL)
6952608Swnj 			um->um_tab.b_actf = dp;
6962608Swnj 		else
6972608Swnj 			um->um_tab.b_actl->b_forw = dp;
6982608Swnj 		um->um_tab.b_actl = dp;
6992608Swnj 	}
7002608Swnj 	if (um->um_tab.b_actf == 0)
7011919Swnj 		return;
7022608Swnj opcont:
7032608Swnj 	tmstart(um);
7041919Swnj }
7051919Swnj 
7063495Sroot tmtimer(dev)
7073495Sroot 	int dev;
7083495Sroot {
7093495Sroot 	register struct te_softc *sc = &te_softc[TEUNIT(dev)];
7104847Sroot 	register short x;
7113495Sroot 
7123495Sroot 	if (sc->sc_timo != INF && (sc->sc_timo -= 5) < 0) {
7134278Sroot 		printf("te%d: lost interrupt\n", TEUNIT(dev));
7143495Sroot 		sc->sc_timo = INF;
7154847Sroot 		x = spl5();
7163495Sroot 		tmintr(TMUNIT(dev));
7174847Sroot 		(void) splx(x);
7183495Sroot 	}
7193629Sroot 	timeout(tmtimer, (caddr_t)dev, 5*hz);
7203495Sroot }
7213495Sroot 
7221919Swnj tmseteof(bp)
7231919Swnj 	register struct buf *bp;
7241919Swnj {
7253095Swnj 	register int teunit = TEUNIT(bp->b_dev);
7265692Sroot 	register struct tmdevice *addr =
7275692Sroot 	    (struct tmdevice *)tedinfo[teunit]->ui_addr;
7283095Swnj 	register struct te_softc *sc = &te_softc[teunit];
7291919Swnj 
7303095Swnj 	if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) {
7317381Ssam 		if (sc->sc_blkno > bdbtofsb(bp->b_blkno)) {
7321919Swnj 			/* reversing */
7337381Ssam 			sc->sc_nxrec = bdbtofsb(bp->b_blkno) - addr->tmbc;
7342471Swnj 			sc->sc_blkno = sc->sc_nxrec;
7351919Swnj 		} else {
7361919Swnj 			/* spacing forward */
7377381Ssam 			sc->sc_blkno = bdbtofsb(bp->b_blkno) + addr->tmbc;
7382471Swnj 			sc->sc_nxrec = sc->sc_blkno - 1;
7391919Swnj 		}
7401919Swnj 		return;
7411919Swnj 	}
7421919Swnj 	/* eof on read */
7437381Ssam 	sc->sc_nxrec = bdbtofsb(bp->b_blkno);
7441919Swnj }
7451919Swnj 
7467732Sroot tmread(dev, uio)
7472608Swnj 	dev_t dev;
7487732Sroot 	struct uio *uio;
7491919Swnj {
7508163Sroot 	int errno;
7511919Swnj 
7528163Sroot 	errno = tmphys(dev, uio);
7538163Sroot 	if (errno)
7548163Sroot 		return (errno);
7558163Sroot 	return (physio(tmstrategy, &rtmbuf[TMUNIT(dev)], dev, B_READ, minphys, uio));
7561919Swnj }
7571919Swnj 
7587838Sroot tmwrite(dev, uio)
7592608Swnj 	dev_t dev;
7607838Sroot 	struct uio *uio;
7611919Swnj {
7628163Sroot 	int errno;
7631919Swnj 
7648163Sroot 	errno = tmphys(dev, uio);
7658163Sroot 	if (errno)
7668163Sroot 		return (errno);
7678163Sroot 	return (physio(tmstrategy, &rtmbuf[TMUNIT(dev)], dev, B_WRITE, minphys, uio));
7681919Swnj }
7691919Swnj 
7703095Swnj /*
7713095Swnj  * Check that a raw device exists.
7723095Swnj  * If it does, set up sc_blkno and sc_nxrec
7733095Swnj  * so that the tape will appear positioned correctly.
7743095Swnj  */
7757732Sroot tmphys(dev, uio)
7762608Swnj 	dev_t dev;
7777732Sroot 	struct uio *uio;
7781919Swnj {
7793095Swnj 	register int teunit = TEUNIT(dev);
7801919Swnj 	register daddr_t a;
7813095Swnj 	register struct te_softc *sc;
7823095Swnj 	register struct uba_device *ui;
7831919Swnj 
7847838Sroot 	if (teunit >= NTE || (ui=tedinfo[teunit]) == 0 || ui->ui_alive == 0)
7857732Sroot 		return (ENXIO);
7863095Swnj 	sc = &te_softc[teunit];
7877838Sroot 	a = bdbtofsb(uio->uio_offset >> 9);
7882471Swnj 	sc->sc_blkno = a;
7892471Swnj 	sc->sc_nxrec = a + 1;
7907732Sroot 	return (0);
7911919Swnj }
7921919Swnj 
7932608Swnj tmreset(uban)
7942608Swnj 	int uban;
7952608Swnj {
7962982Swnj 	register struct uba_ctlr *um;
7973095Swnj 	register tm11, teunit;
7982982Swnj 	register struct uba_device *ui;
7992608Swnj 	register struct buf *dp;
8002608Swnj 
8012630Swnj 	for (tm11 = 0; tm11 < NTM; tm11++) {
8022630Swnj 		if ((um = tmminfo[tm11]) == 0 || um->um_alive == 0 ||
8032608Swnj 		   um->um_ubanum != uban)
8042608Swnj 			continue;
8052928Swnj 		printf(" tm%d", tm11);
8062608Swnj 		um->um_tab.b_active = 0;
8072608Swnj 		um->um_tab.b_actf = um->um_tab.b_actl = 0;
8082608Swnj 		if (um->um_ubinfo) {
8092608Swnj 			printf("<%d>", (um->um_ubinfo>>28)&0xf);
8109355Ssam 			um->um_ubinfo = 0;
8112608Swnj 		}
8125692Sroot 		((struct tmdevice *)(um->um_addr))->tmcs = TM_DCLR;
8133095Swnj 		for (teunit = 0; teunit < NTE; teunit++) {
8143095Swnj 			if ((ui = tedinfo[teunit]) == 0 || ui->ui_mi != um ||
8153095Swnj 			    ui->ui_alive == 0)
8162608Swnj 				continue;
8173095Swnj 			dp = &teutab[teunit];
8182608Swnj 			dp->b_active = 0;
8192608Swnj 			dp->b_forw = 0;
8202608Swnj 			if (um->um_tab.b_actf == NULL)
8212608Swnj 				um->um_tab.b_actf = dp;
8222608Swnj 			else
8232608Swnj 				um->um_tab.b_actl->b_forw = dp;
8242608Swnj 			um->um_tab.b_actl = dp;
8253495Sroot 			if (te_softc[teunit].sc_openf > 0)
8263495Sroot 				te_softc[teunit].sc_openf = -1;
8272608Swnj 		}
8282608Swnj 		tmstart(um);
8292608Swnj 	}
8302608Swnj }
8312608Swnj 
8321919Swnj /*ARGSUSED*/
8337632Ssam tmioctl(dev, cmd, data, flag)
8347632Ssam 	caddr_t data;
8351919Swnj 	dev_t dev;
8361919Swnj {
8373095Swnj 	int teunit = TEUNIT(dev);
8383095Swnj 	register struct te_softc *sc = &te_softc[teunit];
8393095Swnj 	register struct buf *bp = &ctmbuf[TMUNIT(dev)];
8401919Swnj 	register callcount;
8411919Swnj 	int fcount;
8427632Ssam 	struct mtop *mtop;
8437632Ssam 	struct mtget *mtget;
8441919Swnj 	/* we depend of the values and order of the MT codes here */
8452608Swnj 	static tmops[] =
8462608Swnj 	   {TM_WEOF,TM_SFORW,TM_SREV,TM_SFORW,TM_SREV,TM_REW,TM_OFFL,TM_SENSE};
8471919Swnj 
8482608Swnj 	switch (cmd) {
8497632Ssam 
8507632Ssam 	case MTIOCTOP:	/* tape operation */
8517632Ssam 		mtop = (struct mtop *)data;
8528574Sroot 		switch (mtop->mt_op) {
8537632Ssam 
8542608Swnj 		case MTWEOF:
8557632Ssam 			callcount = mtop->mt_count;
8562608Swnj 			fcount = 1;
8572608Swnj 			break;
8587632Ssam 
8592608Swnj 		case MTFSF: case MTBSF:
8607632Ssam 			callcount = mtop->mt_count;
8611919Swnj 			fcount = INF;
8621919Swnj 			break;
8637632Ssam 
8641919Swnj 		case MTFSR: case MTBSR:
8651919Swnj 			callcount = 1;
8667632Ssam 			fcount = mtop->mt_count;
8671919Swnj 			break;
8687632Ssam 
8692324Skre 		case MTREW: case MTOFFL: case MTNOP:
8701919Swnj 			callcount = 1;
8711919Swnj 			fcount = 1;
8721919Swnj 			break;
8737632Ssam 
8741919Swnj 		default:
8758574Sroot 			return (ENXIO);
8761919Swnj 		}
8778574Sroot 		if (callcount <= 0 || fcount <= 0)
8788574Sroot 			return (EINVAL);
8792608Swnj 		while (--callcount >= 0) {
8807632Ssam 			tmcommand(dev, tmops[mtop->mt_op], fcount);
8817632Ssam 			if ((mtop->mt_op == MTFSR || mtop->mt_op == MTBSR) &&
8828574Sroot 			    bp->b_resid)
8838574Sroot 				return (EIO);
8843095Swnj 			if ((bp->b_flags&B_ERROR) || sc->sc_erreg&TMER_BOT)
8851919Swnj 				break;
8861919Swnj 		}
8878648Sroot 		return (geterror(bp));
8887632Ssam 
8891919Swnj 	case MTIOCGET:
8907632Ssam 		mtget = (struct mtget *)data;
8917632Ssam 		mtget->mt_dsreg = sc->sc_dsreg;
8927632Ssam 		mtget->mt_erreg = sc->sc_erreg;
8937632Ssam 		mtget->mt_resid = sc->sc_resid;
8947632Ssam 		mtget->mt_type = MT_ISTM;
8958574Sroot 		break;
8967632Ssam 
8971919Swnj 	default:
8988574Sroot 		return (ENXIO);
8991919Swnj 	}
9008574Sroot 	return (0);
9011919Swnj }
9021919Swnj 
9031919Swnj #define	DBSIZE	20
9041919Swnj 
9052363Swnj tmdump()
9062363Swnj {
9072982Swnj 	register struct uba_device *ui;
9082396Swnj 	register struct uba_regs *up;
9095692Sroot 	register struct tmdevice *addr;
9102426Skre 	int blk, num;
9112426Skre 	int start;
9121919Swnj 
9132426Skre 	start = 0;
9142426Skre 	num = maxfree;
9152426Skre #define	phys(a,b)	((b)((int)(a)&0x7fffffff))
9163095Swnj 	if (tedinfo[0] == 0)
9172887Swnj 		return (ENXIO);
9183095Swnj 	ui = phys(tedinfo[0], struct uba_device *);
9192396Swnj 	up = phys(ui->ui_hd, struct uba_hd *)->uh_physuba;
9203331Swnj 	ubainit(up);
9212324Skre 	DELAY(1000000);
9225692Sroot 	addr = (struct tmdevice *)ui->ui_physaddr;
9232396Swnj 	tmwait(addr);
9242608Swnj 	addr->tmcs = TM_DCLR | TM_GO;
9251919Swnj 	while (num > 0) {
9261919Swnj 		blk = num > DBSIZE ? DBSIZE : num;
9272396Swnj 		tmdwrite(start, blk, addr, up);
9281919Swnj 		start += blk;
9291919Swnj 		num -= blk;
9301919Swnj 	}
9312426Skre 	tmeof(addr);
9322426Skre 	tmeof(addr);
9332426Skre 	tmwait(addr);
9342887Swnj 	if (addr->tmcs&TM_ERR)
9352887Swnj 		return (EIO);
9362608Swnj 	addr->tmcs = TM_REW | TM_GO;
9372471Swnj 	tmwait(addr);
9382363Swnj 	return (0);
9391919Swnj }
9401919Swnj 
9412608Swnj tmdwrite(dbuf, num, addr, up)
9422608Swnj 	register dbuf, num;
9435692Sroot 	register struct tmdevice *addr;
9442396Swnj 	struct uba_regs *up;
9451919Swnj {
9462396Swnj 	register struct pte *io;
9472396Swnj 	register int npf;
9481928Swnj 
9492396Swnj 	tmwait(addr);
9502396Swnj 	io = up->uba_map;
9511919Swnj 	npf = num+1;
9521928Swnj 	while (--npf != 0)
9532982Swnj 		 *(int *)io++ = (dbuf++ | (1<<UBAMR_DPSHIFT) | UBAMR_MRV);
9542396Swnj 	*(int *)io = 0;
9552396Swnj 	addr->tmbc = -(num*NBPG);
9562396Swnj 	addr->tmba = 0;
9572608Swnj 	addr->tmcs = TM_WCOM | TM_GO;
9581919Swnj }
9591919Swnj 
9602396Swnj tmwait(addr)
9615692Sroot 	register struct tmdevice *addr;
9621919Swnj {
9631928Swnj 	register s;
9641919Swnj 
9651919Swnj 	do
9662396Swnj 		s = addr->tmcs;
9672608Swnj 	while ((s & TM_CUR) == 0);
9681919Swnj }
9691919Swnj 
9702396Swnj tmeof(addr)
9715692Sroot 	struct tmdevice *addr;
9721919Swnj {
9731919Swnj 
9742396Swnj 	tmwait(addr);
9752608Swnj 	addr->tmcs = TM_WEOF | TM_GO;
9761919Swnj }
9771919Swnj #endif
978