xref: /csrg-svn/sys/vax/uba/tm.c (revision 33477)
123344Smckusick /*
229242Smckusick  * Copyright (c) 1982, 1986 Regents of the University of California.
323344Smckusick  * All rights reserved.  The Berkeley software License Agreement
423344Smckusick  * specifies the terms and conditions for redistribution.
523344Smckusick  *
6*33477Skarels  *	@(#)tm.c	7.3 (Berkeley) 02/08/88
723344Smckusick  */
81919Swnj 
92709Swnj #include "te.h"
103519Sroot #include "ts.h"
116343Swnj #if NTE > 0
121919Swnj /*
132630Swnj  * TM11/TE10 tape driver
142471Swnj  *
153095Swnj  * TODO:
163095Swnj  *	test driver with more than one controller
173095Swnj  *	test reset code
183095Swnj  *	what happens if you offline tape during rewind?
193095Swnj  *	test using file system on tape
201919Swnj  */
2117079Sbloom #include "param.h"
2217079Sbloom #include "systm.h"
2317079Sbloom #include "buf.h"
2417079Sbloom #include "dir.h"
2517079Sbloom #include "conf.h"
2617079Sbloom #include "user.h"
2717079Sbloom #include "file.h"
2817079Sbloom #include "map.h"
2917079Sbloom #include "vm.h"
3017079Sbloom #include "ioctl.h"
3117079Sbloom #include "mtio.h"
3217079Sbloom #include "cmap.h"
3317079Sbloom #include "uio.h"
3417079Sbloom #include "kernel.h"
3518321Sralph #include "tty.h"
3630917Skarels #include "syslog.h"
371919Swnj 
3830917Skarels #include "../machine/pte.h"
398479Sroot #include "../vax/cpu.h"
4017079Sbloom #include "ubareg.h"
4117079Sbloom #include "ubavar.h"
4217079Sbloom #include "tmreg.h"
431919Swnj 
443095Swnj /*
453095Swnj  * There is a ctmbuf per tape controller.
463095Swnj  * It is used as the token to pass to the internal routines
473095Swnj  * to execute tape ioctls, and also acts as a lock on the slaves
483095Swnj  * on the controller, since there is only one per controller.
493095Swnj  * In particular, when the tape is rewinding on close we release
503095Swnj  * the user process but any further attempts to use the tape drive
513095Swnj  * before the rewind completes will hang waiting for ctmbuf.
523095Swnj  */
533095Swnj struct	buf	ctmbuf[NTM];
541919Swnj 
553095Swnj /*
563095Swnj  * Raw tape operations use rtmbuf.  The driver
573095Swnj  * notices when rtmbuf is being used and allows the user
583095Swnj  * program to continue after errors and read records
593095Swnj  * not of the standard length (BSIZE).
603095Swnj  */
613095Swnj struct	buf	rtmbuf[NTM];
623095Swnj 
633095Swnj /*
643095Swnj  * Driver unibus interface routines and variables.
653095Swnj  */
662608Swnj int	tmprobe(), tmslave(), tmattach(), tmdgo(), tmintr();
672982Swnj struct	uba_ctlr *tmminfo[NTM];
683095Swnj struct	uba_device *tedinfo[NTE];
693095Swnj struct	buf teutab[NTE];
703095Swnj short	tetotm[NTE];
712458Swnj u_short	tmstd[] = { 0772520, 0 };
722396Swnj struct	uba_driver tmdriver =
733095Swnj  { tmprobe, tmslave, tmattach, tmdgo, tmstd, "te", tedinfo, "tm", tmminfo, 0 };
741919Swnj 
751919Swnj /* bits in minor device */
763095Swnj #define	TEUNIT(dev)	(minor(dev)&03)
773095Swnj #define	TMUNIT(dev)	(tetotm[TEUNIT(dev)])
781919Swnj #define	T_NOREWIND	04
7924974Smckusick #define	T_1600BPI	0x8
801919Swnj 
811919Swnj #define	INF	(daddr_t)1000000L
821919Swnj 
832608Swnj /*
842608Swnj  * Software state per tape transport.
853095Swnj  *
863095Swnj  * 1. A tape drive is a unique-open device; we refuse opens when it is already.
873095Swnj  * 2. We keep track of the current position on a block tape and seek
883095Swnj  *    before operations by forward/back spacing if necessary.
893095Swnj  * 3. We remember if the last operation was a write on a tape, so if a tape
903095Swnj  *    is open read write and the last thing done is a write we can
913095Swnj  *    write a standard end of tape mark (two eofs).
923095Swnj  * 4. We remember the status registers after the last command, using
933095Swnj  *    then internally and returning them to the SENSE ioctl.
943095Swnj  * 5. We remember the last density the tape was used at.  If it is
953095Swnj  *    not a BOT when we start using it and we are writing, we don't
963095Swnj  *    let the density be changed.
972608Swnj  */
983095Swnj struct	te_softc {
992608Swnj 	char	sc_openf;	/* lock against multiple opens */
1002608Swnj 	char	sc_lastiow;	/* last op was a write */
1012608Swnj 	daddr_t	sc_blkno;	/* block number, for block device tape */
1023095Swnj 	daddr_t	sc_nxrec;	/* position of end of tape, if known */
1032608Swnj 	u_short	sc_erreg;	/* copy of last erreg */
104*33477Skarels 	u_short	sc_ioerreg;	/* copy of last erreg for I/O command */
1052608Swnj 	u_short	sc_dsreg;	/* copy of last dsreg */
1062608Swnj 	short	sc_resid;	/* copy of last bc */
1073105Swnj #ifdef unneeded
1082670Swnj 	short	sc_lastcmd;	/* last command to handle direction changes */
1092928Swnj #endif
1103095Swnj 	u_short	sc_dens;	/* prototype command with density info */
11118321Sralph 	short	sc_tact;	/* timeout is active */
1123495Sroot 	daddr_t	sc_timo;	/* time until timeout expires */
11330917Skarels 	int	sc_blks;	/* number of I/O operations since open */
11430917Skarels 	int	sc_softerrs;	/* number of soft I/O errors since open */
11518321Sralph 	struct	tty *sc_ttyp;	/* record user's tty for errors */
1166952Swnj } te_softc[NTE];
1173105Swnj #ifdef unneeded
1183105Swnj int	tmgapsdcnt;		/* DEBUG */
1193105Swnj #endif
1201919Swnj 
1212608Swnj /*
1223095Swnj  * States for um->um_tab.b_active, the per controller state flag.
1233095Swnj  * This is used to sequence control in the driver.
1242608Swnj  */
1251919Swnj #define	SSEEK	1		/* seeking */
1261919Swnj #define	SIO	2		/* doing seq i/o */
1271919Swnj #define	SCOM	3		/* sending control command */
1282608Swnj #define	SREW	4		/* sending a drive rewind */
1291919Swnj 
1302426Skre /*
1312426Skre  * Determine if there is a controller for
1322426Skre  * a tm at address reg.  Our goal is to make the
1332426Skre  * device interrupt.
1342426Skre  */
1352608Swnj tmprobe(reg)
1362396Swnj 	caddr_t reg;
1372396Swnj {
1383095Swnj 	register int br, cvec;		/* must be r11,r10; value-result */
1392426Skre 
1402608Swnj #ifdef lint
1413105Swnj 	br = 0; cvec = br; br = cvec;
1424936Swnj 	tmintr(0);
1432608Swnj #endif
1445692Sroot 	((struct tmdevice *)reg)->tmcs = TM_IE;
1452396Swnj 	/*
1462630Swnj 	 * If this is a tm11, it ought to have interrupted
1472396Swnj 	 * by now, if it isn't (ie: it is a ts04) then we just
1482458Swnj 	 * hope that it didn't interrupt, so autoconf will ignore it.
1492458Swnj 	 * Just in case, we will reference one
1502396Swnj 	 * of the more distant registers, and hope for a machine
1512458Swnj 	 * check, or similar disaster if this is a ts.
1522471Swnj 	 *
1532471Swnj 	 * Note: on an 11/780, badaddr will just generate
1542471Swnj 	 * a uba error for a ts; but our caller will notice that
1552471Swnj 	 * so we won't check for it.
1562396Swnj 	 */
1575692Sroot 	if (badaddr((caddr_t)&((struct tmdevice *)reg)->tmrd, 2))
1582458Swnj 		return (0);
1597404Skre 	return (sizeof (struct tmdevice));
1602396Swnj }
1612396Swnj 
1622608Swnj /*
1632608Swnj  * Due to a design flaw, we cannot ascertain if the tape
1642608Swnj  * exists or not unless it is on line - ie: unless a tape is
1652608Swnj  * mounted. This is too servere a restriction to bear,
1662608Swnj  * so all units are assumed to exist.
1672608Swnj  */
1682608Swnj /*ARGSUSED*/
1692574Swnj tmslave(ui, reg)
1702982Swnj 	struct uba_device *ui;
1712396Swnj 	caddr_t reg;
1722396Swnj {
1732458Swnj 
1742458Swnj 	return (1);
1752396Swnj }
1762396Swnj 
1772608Swnj /*
1783095Swnj  * Record attachment of the unit to the controller.
1792608Swnj  */
1802608Swnj /*ARGSUSED*/
1812608Swnj tmattach(ui)
1822982Swnj 	struct uba_device *ui;
1832608Swnj {
1843095Swnj 	/*
1853095Swnj 	 * Tetotm is used in TMUNIT to index the ctmbuf and rtmbuf
1863095Swnj 	 * arrays given a te unit number.
1873095Swnj 	 */
1883095Swnj 	tetotm[ui->ui_unit] = ui->ui_mi->um_ctlr;
1892608Swnj }
1902608Swnj 
1913495Sroot int	tmtimer();
1922608Swnj /*
1932608Swnj  * Open the device.  Tapes are unique open
1942608Swnj  * devices, so we refuse if it is already open.
1952608Swnj  * We also check that a tape is available, and
1963095Swnj  * don't block waiting here; if you want to wait
1973095Swnj  * for a tape you should timeout in user code.
1982608Swnj  */
19924974Smckusick 
20024974Smckusick #ifdef AVIV
20124974Smckusick int tmdens[4] = { 0x6000, 0x0000, 0x2000, 0 };
20230917Skarels #endif AVIV
20324974Smckusick int tmdiag;
20424974Smckusick 
2051919Swnj tmopen(dev, flag)
2061919Swnj 	dev_t dev;
2071919Swnj 	int flag;
2081919Swnj {
2093095Swnj 	register int teunit;
2102982Swnj 	register struct uba_device *ui;
2113095Swnj 	register struct te_softc *sc;
2123209Swnj 	int olddens, dens;
2135437Sroot 	int s;
2141919Swnj 
2153095Swnj 	teunit = TEUNIT(dev);
21625051Skarels 	if (teunit>=NTE || (ui = tedinfo[teunit]) == 0 || ui->ui_alive == 0)
2178574Sroot 		return (ENXIO);
21825051Skarels 	if ((sc = &te_softc[teunit])->sc_openf)
21925051Skarels 		return (EBUSY);
22030917Skarels 	sc->sc_openf = 1;
2213209Swnj 	olddens = sc->sc_dens;
2223209Swnj 	dens = TM_IE | TM_GO | (ui->ui_slave << 8);
22324974Smckusick #ifndef AVIV
2243209Swnj 	if ((minor(dev) & T_1600BPI) == 0)
2253209Swnj 		dens |= TM_D800;
22624974Smckusick #else AVIV
22724974Smckusick 	dens |= tmdens[(minor(dev)>>3)&03];
22824974Smckusick #endif AVIV
2293209Swnj 	sc->sc_dens = dens;
2303141Swnj get:
2312608Swnj 	tmcommand(dev, TM_SENSE, 1);
2323141Swnj 	if (sc->sc_erreg&TMER_SDWN) {
2333141Swnj 		sleep((caddr_t)&lbolt, PZERO+1);
2343141Swnj 		goto get;
2353141Swnj 	}
2363209Swnj 	sc->sc_dens = olddens;
2373710Sroot 	if ((sc->sc_erreg&(TMER_SELR|TMER_TUR)) != (TMER_SELR|TMER_TUR)) {
2383710Sroot 		uprintf("te%d: not online\n", teunit);
23930917Skarels 		sc->sc_openf = 0;
2408574Sroot 		return (EIO);
2411919Swnj 	}
2423710Sroot 	if ((flag&FWRITE) && (sc->sc_erreg&TMER_WRL)) {
2433710Sroot 		uprintf("te%d: no write ring\n", teunit);
24430917Skarels 		sc->sc_openf = 0;
2458574Sroot 		return (EIO);
2463710Sroot 	}
2473710Sroot 	if ((sc->sc_erreg&TMER_BOT) == 0 && (flag&FWRITE) &&
2483710Sroot 	    dens != sc->sc_dens) {
2493710Sroot 		uprintf("te%d: can't change density in mid-tape\n", teunit);
25030917Skarels 		sc->sc_openf = 0;
2518574Sroot 		return (EIO);
2523710Sroot 	}
2532471Swnj 	sc->sc_blkno = (daddr_t)0;
2542471Swnj 	sc->sc_nxrec = INF;
2552608Swnj 	sc->sc_lastiow = 0;
2563095Swnj 	sc->sc_dens = dens;
25730917Skarels 	sc->sc_blks = 0;
25830917Skarels 	sc->sc_softerrs = 0;
25918321Sralph 	sc->sc_ttyp = u.u_ttyp;
26026369Skarels 	s = splclock();
2613495Sroot 	if (sc->sc_tact == 0) {
2623495Sroot 		sc->sc_timo = INF;
2633495Sroot 		sc->sc_tact = 1;
2643629Sroot 		timeout(tmtimer, (caddr_t)dev, 5*hz);
2653495Sroot 	}
2665437Sroot 	splx(s);
2678574Sroot 	return (0);
2681919Swnj }
2691919Swnj 
2702608Swnj /*
2712608Swnj  * Close tape device.
2722608Swnj  *
2732608Swnj  * If tape was open for writing or last operation was
2742608Swnj  * a write, then write two EOF's and backspace over the last one.
2752608Swnj  * Unless this is a non-rewinding special file, rewind the tape.
2762608Swnj  * Make the tape available to others.
2772608Swnj  */
2781919Swnj tmclose(dev, flag)
2791919Swnj 	register dev_t dev;
2801919Swnj 	register flag;
2811919Swnj {
2823095Swnj 	register struct te_softc *sc = &te_softc[TEUNIT(dev)];
2831919Swnj 
2842608Swnj 	if (flag == FWRITE || (flag&FWRITE) && sc->sc_lastiow) {
2852608Swnj 		tmcommand(dev, TM_WEOF, 1);
2862608Swnj 		tmcommand(dev, TM_WEOF, 1);
2872608Swnj 		tmcommand(dev, TM_SREV, 1);
2881919Swnj 	}
2891919Swnj 	if ((minor(dev)&T_NOREWIND) == 0)
2903095Swnj 		/*
2913095Swnj 		 * 0 count means don't hang waiting for rewind complete
2923095Swnj 		 * rather ctmbuf stays busy until the operation completes
2933095Swnj 		 * preventing further opens from completing by
2943095Swnj 		 * preventing a TM_SENSE from completing.
2953095Swnj 		 */
2963095Swnj 		tmcommand(dev, TM_REW, 0);
29730917Skarels 	if (sc->sc_blks > 100 && sc->sc_softerrs > sc->sc_blks / 100)
29830917Skarels 		log(LOG_INFO, "te%d: %d soft errors in %d blocks\n",
29930917Skarels 		    TEUNIT(dev), sc->sc_softerrs, sc->sc_blks);
3002471Swnj 	sc->sc_openf = 0;
30130917Skarels 	return (0);
3021919Swnj }
3031919Swnj 
3042608Swnj /*
3052608Swnj  * Execute a command on the tape drive
3062608Swnj  * a specified number of times.
3072608Swnj  */
3082574Swnj tmcommand(dev, com, count)
3091919Swnj 	dev_t dev;
3101919Swnj 	int com, count;
3111919Swnj {
3121919Swnj 	register struct buf *bp;
3135437Sroot 	register int s;
3141919Swnj 
3152608Swnj 	bp = &ctmbuf[TMUNIT(dev)];
3165437Sroot 	s = spl5();
3171919Swnj 	while (bp->b_flags&B_BUSY) {
3183095Swnj 		/*
3193095Swnj 		 * This special check is because B_BUSY never
3203095Swnj 		 * gets cleared in the non-waiting rewind case.
3213095Swnj 		 */
3223141Swnj 		if (bp->b_repcnt == 0 && (bp->b_flags&B_DONE))
3233095Swnj 			break;
3241919Swnj 		bp->b_flags |= B_WANTED;
3251919Swnj 		sleep((caddr_t)bp, PRIBIO);
3261919Swnj 	}
3271919Swnj 	bp->b_flags = B_BUSY|B_READ;
3285437Sroot 	splx(s);
3291919Swnj 	bp->b_dev = dev;
3301919Swnj 	bp->b_repcnt = -count;
3311919Swnj 	bp->b_command = com;
3321919Swnj 	bp->b_blkno = 0;
3331919Swnj 	tmstrategy(bp);
3343095Swnj 	/*
3353095Swnj 	 * In case of rewind from close, don't wait.
3363095Swnj 	 * This is the only case where count can be 0.
3373095Swnj 	 */
3383095Swnj 	if (count == 0)
3393095Swnj 		return;
3401919Swnj 	iowait(bp);
3411919Swnj 	if (bp->b_flags&B_WANTED)
3421919Swnj 		wakeup((caddr_t)bp);
3431919Swnj 	bp->b_flags &= B_ERROR;
3441919Swnj }
3451919Swnj 
3462608Swnj /*
3473095Swnj  * Queue a tape operation.
3482608Swnj  */
3491919Swnj tmstrategy(bp)
3501919Swnj 	register struct buf *bp;
3511919Swnj {
3523095Swnj 	int teunit = TEUNIT(bp->b_dev);
3532982Swnj 	register struct uba_ctlr *um;
3542608Swnj 	register struct buf *dp;
3555437Sroot 	int s;
3561919Swnj 
3572608Swnj 	/*
3582608Swnj 	 * Put transfer at end of unit queue
3592608Swnj 	 */
3603095Swnj 	dp = &teutab[teunit];
3611919Swnj 	bp->av_forw = NULL;
3625437Sroot 	s = spl5();
3633939Sbugs 	um = tedinfo[teunit]->ui_mi;
3642608Swnj 	if (dp->b_actf == NULL) {
3652608Swnj 		dp->b_actf = bp;
3662608Swnj 		/*
3672608Swnj 		 * Transport not already active...
3682608Swnj 		 * put at end of controller queue.
3692608Swnj 		 */
3702608Swnj 		dp->b_forw = NULL;
3712608Swnj 		if (um->um_tab.b_actf == NULL)
3722608Swnj 			um->um_tab.b_actf = dp;
3732608Swnj 		else
3742608Swnj 			um->um_tab.b_actl->b_forw = dp;
3752608Swnj 		um->um_tab.b_actl = dp;
3762608Swnj 	} else
3772608Swnj 		dp->b_actl->av_forw = bp;
3782608Swnj 	dp->b_actl = bp;
3792608Swnj 	/*
3802608Swnj 	 * If the controller is not busy, get
3812608Swnj 	 * it going.
3822608Swnj 	 */
3832608Swnj 	if (um->um_tab.b_active == 0)
3842608Swnj 		tmstart(um);
3855437Sroot 	splx(s);
3861919Swnj }
3871919Swnj 
3882608Swnj /*
3892608Swnj  * Start activity on a tm controller.
3902608Swnj  */
3912608Swnj tmstart(um)
3922982Swnj 	register struct uba_ctlr *um;
3931919Swnj {
3942608Swnj 	register struct buf *bp, *dp;
3955692Sroot 	register struct tmdevice *addr = (struct tmdevice *)um->um_addr;
3963095Swnj 	register struct te_softc *sc;
3972982Swnj 	register struct uba_device *ui;
3983095Swnj 	int teunit, cmd;
3992471Swnj 	daddr_t blkno;
4001919Swnj 
4012608Swnj 	/*
4022608Swnj 	 * Look for an idle transport on the controller.
4032608Swnj 	 */
4041919Swnj loop:
4052608Swnj 	if ((dp = um->um_tab.b_actf) == NULL)
4061919Swnj 		return;
4072608Swnj 	if ((bp = dp->b_actf) == NULL) {
4082608Swnj 		um->um_tab.b_actf = dp->b_forw;
4092608Swnj 		goto loop;
4102608Swnj 	}
4113095Swnj 	teunit = TEUNIT(bp->b_dev);
4123095Swnj 	ui = tedinfo[teunit];
4132608Swnj 	/*
4142608Swnj 	 * Record pre-transfer status (e.g. for TM_SENSE)
4152608Swnj 	 */
4163095Swnj 	sc = &te_softc[teunit];
4175692Sroot 	addr = (struct tmdevice *)um->um_addr;
4182608Swnj 	addr->tmcs = (ui->ui_slave << 8);
4192471Swnj 	sc->sc_dsreg = addr->tmcs;
4202471Swnj 	sc->sc_erreg = addr->tmer;
4212471Swnj 	sc->sc_resid = addr->tmbc;
4222608Swnj 	/*
4232608Swnj 	 * Default is that last command was NOT a write command;
4242608Swnj 	 * if we do a write command we will notice this in tmintr().
4252608Swnj 	 */
4263493Sroot 	sc->sc_lastiow = 0;
4272608Swnj 	if (sc->sc_openf < 0 || (addr->tmcs&TM_CUR) == 0) {
4282608Swnj 		/*
4293095Swnj 		 * Have had a hard error on a non-raw tape
4303095Swnj 		 * or the tape unit is now unavailable
4313095Swnj 		 * (e.g. taken off line).
4322608Swnj 		 */
4332608Swnj 		bp->b_flags |= B_ERROR;
4341919Swnj 		goto next;
4351919Swnj 	}
4363095Swnj 	if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) {
4373095Swnj 		/*
4383095Swnj 		 * Execute control operation with the specified count.
4393095Swnj 		 */
4402608Swnj 		if (bp->b_command == TM_SENSE)
4412608Swnj 			goto next;
4423495Sroot 		/*
4433495Sroot 		 * Set next state; give 5 minutes to complete
4443495Sroot 		 * rewind, or 10 seconds per iteration (minimum 60
4453629Sroot 		 * seconds and max 5 minutes) to complete other ops.
4463495Sroot 		 */
4473495Sroot 		if (bp->b_command == TM_REW) {
4483495Sroot 			um->um_tab.b_active = SREW;
4493495Sroot 			sc->sc_timo = 5 * 60;
4503495Sroot 		} else {
4513495Sroot 			um->um_tab.b_active = SCOM;
4524266Swnj 			sc->sc_timo =
4534266Swnj 			    imin(imax(10*(int)-bp->b_repcnt,60),5*60);
4543495Sroot 		}
4552608Swnj 		if (bp->b_command == TM_SFORW || bp->b_command == TM_SREV)
4562608Swnj 			addr->tmbc = bp->b_repcnt;
4572670Swnj 		goto dobpcmd;
4582608Swnj 	}
4592608Swnj 	/*
4603095Swnj 	 * The following checks handle boundary cases for operation
4613095Swnj 	 * on non-raw tapes.  On raw tapes the initialization of
4623095Swnj 	 * sc->sc_nxrec by tmphys causes them to be skipped normally
4633095Swnj 	 * (except in the case of retries).
4643095Swnj 	 */
4657381Ssam 	if (bdbtofsb(bp->b_blkno) > sc->sc_nxrec) {
4663095Swnj 		/*
4673095Swnj 		 * Can't read past known end-of-file.
4683095Swnj 		 */
4693095Swnj 		bp->b_flags |= B_ERROR;
4703095Swnj 		bp->b_error = ENXIO;
4713095Swnj 		goto next;
4723095Swnj 	}
4737381Ssam 	if (bdbtofsb(bp->b_blkno) == sc->sc_nxrec &&
4743095Swnj 	    bp->b_flags&B_READ) {
4753095Swnj 		/*
4763095Swnj 		 * Reading at end of file returns 0 bytes.
4773095Swnj 		 */
4783095Swnj 		bp->b_resid = bp->b_bcount;
4793095Swnj 		clrbuf(bp);
4803095Swnj 		goto next;
4813095Swnj 	}
4823095Swnj 	if ((bp->b_flags&B_READ) == 0)
4833095Swnj 		/*
4843095Swnj 		 * Writing sets EOF
4853095Swnj 		 */
4867381Ssam 		sc->sc_nxrec = bdbtofsb(bp->b_blkno) + 1;
4873095Swnj 	/*
4882608Swnj 	 * If the data transfer command is in the correct place,
4892608Swnj 	 * set up all the registers except the csr, and give
4902608Swnj 	 * control over to the UNIBUS adapter routines, to
4912608Swnj 	 * wait for resources to start the i/o.
4922608Swnj 	 */
4937381Ssam 	if ((blkno = sc->sc_blkno) == bdbtofsb(bp->b_blkno)) {
4942396Swnj 		addr->tmbc = -bp->b_bcount;
4951919Swnj 		if ((bp->b_flags&B_READ) == 0) {
49630917Skarels 			if (um->um_tab.b_errcnt &&
497*33477Skarels 			    (sc->sc_ioerreg&(TMER_HARD|TMER_SOFT)) != TMER_BGL)
4983095Swnj 				cmd = TM_WIRG;
4991919Swnj 			else
5003095Swnj 				cmd = TM_WCOM;
5011919Swnj 		} else
5023095Swnj 			cmd = TM_RCOM;
5032471Swnj 		um->um_tab.b_active = SIO;
5043095Swnj 		um->um_cmd = sc->sc_dens|cmd;
5052928Swnj #ifdef notdef
5062670Swnj 		if (tmreverseop(sc->sc_lastcmd))
5073095Swnj 			while (addr->tmer & TMER_SDWN)
50824974Smckusick 				DELAY(10),tmgapsdcnt++;
5092670Swnj 		sc->sc_lastcmd = TM_RCOM;		/* will serve */
5102928Swnj #endif
5113495Sroot 		sc->sc_timo = 60;	/* premature, but should serve */
5123105Swnj 		(void) ubago(ui);
5131919Swnj 		return;
5141919Swnj 	}
5152608Swnj 	/*
5163095Swnj 	 * Tape positioned incorrectly;
5173095Swnj 	 * set to seek forwards or backwards to the correct spot.
5183095Swnj 	 * This happens for raw tapes only on error retries.
5192608Swnj 	 */
5202471Swnj 	um->um_tab.b_active = SSEEK;
5217381Ssam 	if (blkno < bdbtofsb(bp->b_blkno)) {
5222670Swnj 		bp->b_command = TM_SFORW;
5237381Ssam 		addr->tmbc = blkno - bdbtofsb(bp->b_blkno);
5241919Swnj 	} else {
5252670Swnj 		bp->b_command = TM_SREV;
5267381Ssam 		addr->tmbc = bdbtofsb(bp->b_blkno) - blkno;
5271919Swnj 	}
5283629Sroot 	sc->sc_timo = imin(imax(10 * -addr->tmbc, 60), 5 * 60);
5292670Swnj dobpcmd:
5302928Swnj #ifdef notdef
5313095Swnj 	/*
5323095Swnj 	 * It is strictly necessary to wait for the tape
5333095Swnj 	 * to stop before changing directions, but the TC11
5343095Swnj 	 * handles this for us.
5353095Swnj 	 */
5362670Swnj 	if (tmreverseop(sc->sc_lastcmd) != tmreverseop(bp->b_command))
5372670Swnj 		while (addr->tmer & TM_SDWN)
53824974Smckusick 			DELAY(10),tmgapsdcnt++;
5392670Swnj 	sc->sc_lastcmd = bp->b_command;
5402928Swnj #endif
5413095Swnj 	/*
5423095Swnj 	 * Do the command in bp.
5433095Swnj 	 */
5443095Swnj 	addr->tmcs = (sc->sc_dens | bp->b_command);
5451919Swnj 	return;
5461919Swnj 
5471919Swnj next:
5482608Swnj 	/*
5492608Swnj 	 * Done with this operation due to error or
5502608Swnj 	 * the fact that it doesn't do anything.
5512608Swnj 	 * Release UBA resources (if any), dequeue
5522608Swnj 	 * the transfer and continue processing this slave.
5532608Swnj 	 */
5542608Swnj 	if (um->um_ubinfo)
5552617Swnj 		ubadone(um);
5562608Swnj 	um->um_tab.b_errcnt = 0;
5572608Swnj 	dp->b_actf = bp->av_forw;
5581919Swnj 	iodone(bp);
5591919Swnj 	goto loop;
5601919Swnj }
5611919Swnj 
5622608Swnj /*
5632608Swnj  * The UNIBUS resources we needed have been
5642608Swnj  * allocated to us; start the device.
5652608Swnj  */
5662574Swnj tmdgo(um)
5672982Swnj 	register struct uba_ctlr *um;
5681919Swnj {
5695692Sroot 	register struct tmdevice *addr = (struct tmdevice *)um->um_addr;
5702471Swnj 
5712574Swnj 	addr->tmba = um->um_ubinfo;
5722574Swnj 	addr->tmcs = um->um_cmd | ((um->um_ubinfo >> 12) & 0x30);
5732396Swnj }
5742396Swnj 
5752608Swnj /*
5762608Swnj  * Tm interrupt routine.
5772608Swnj  */
5782471Swnj /*ARGSUSED*/
5792630Swnj tmintr(tm11)
5802630Swnj 	int tm11;
5812396Swnj {
5822608Swnj 	struct buf *dp;
5831919Swnj 	register struct buf *bp;
5842982Swnj 	register struct uba_ctlr *um = tmminfo[tm11];
5855692Sroot 	register struct tmdevice *addr;
5863095Swnj 	register struct te_softc *sc;
5873095Swnj 	int teunit;
5881919Swnj 	register state;
5891919Swnj 
5903095Swnj 	if ((dp = um->um_tab.b_actf) == NULL)
5913095Swnj 		return;
5923095Swnj 	bp = dp->b_actf;
5933095Swnj 	teunit = TEUNIT(bp->b_dev);
5945692Sroot 	addr = (struct tmdevice *)tedinfo[teunit]->ui_addr;
5953524Swnj 	sc = &te_softc[teunit];
5962608Swnj 	/*
5972608Swnj 	 * If last command was a rewind, and tape is still
5982608Swnj 	 * rewinding, wait for the rewind complete interrupt.
5992608Swnj 	 */
6002608Swnj 	if (um->um_tab.b_active == SREW) {
6012608Swnj 		um->um_tab.b_active = SCOM;
6023524Swnj 		if (addr->tmer&TMER_RWS) {
6033524Swnj 			sc->sc_timo = 5*60;		/* 5 minutes */
6042608Swnj 			return;
6053524Swnj 		}
6061919Swnj 	}
6072608Swnj 	/*
6082608Swnj 	 * An operation completed... record status
6092608Swnj 	 */
6103495Sroot 	sc->sc_timo = INF;
611*33477Skarels 	if (um->um_tab.b_active = SIO)
612*33477Skarels 		sc->sc_ioerreg = addr->tmer;
6132471Swnj 	sc->sc_dsreg = addr->tmcs;
6142471Swnj 	sc->sc_erreg = addr->tmer;
6152471Swnj 	sc->sc_resid = addr->tmbc;
6161919Swnj 	if ((bp->b_flags & B_READ) == 0)
6172608Swnj 		sc->sc_lastiow = 1;
6182471Swnj 	state = um->um_tab.b_active;
6192471Swnj 	um->um_tab.b_active = 0;
6202608Swnj 	/*
6212608Swnj 	 * Check for errors.
6222608Swnj 	 */
6232608Swnj 	if (addr->tmcs&TM_ERR) {
6243095Swnj 		while (addr->tmer & TMER_SDWN)
62524974Smckusick 			DELAY(10);			/* await settle down */
6262608Swnj 		/*
6273095Swnj 		 * If we hit the end of the tape file, update our position.
6282608Swnj 		 */
6293095Swnj 		if (addr->tmer&TMER_EOF) {
6302608Swnj 			tmseteof(bp);		/* set blkno and nxrec */
6312608Swnj 			state = SCOM;		/* force completion */
6322608Swnj 			/*
6332608Swnj 			 * Stuff bc so it will be unstuffed correctly
6342608Swnj 			 * later to get resid.
6352608Swnj 			 */
6362396Swnj 			addr->tmbc = -bp->b_bcount;
6372608Swnj 			goto opdone;
6381919Swnj 		}
6392608Swnj 		/*
6403095Swnj 		 * If we were reading raw tape and the only error was that the
6413095Swnj 		 * record was too long, then we don't consider this an error.
6422608Swnj 		 */
6433095Swnj 		if (bp == &rtmbuf[TMUNIT(bp->b_dev)] && (bp->b_flags&B_READ) &&
6443095Swnj 		    (addr->tmer&(TMER_HARD|TMER_SOFT)) == TMER_RLE)
6452608Swnj 			goto ignoreerr;
6462608Swnj 		/*
6472608Swnj 		 * If error is not hard, and this was an i/o operation
6482608Swnj 		 * retry up to 8 times.
6492608Swnj 		 */
6503095Swnj 		if ((addr->tmer&TMER_HARD)==0 && state==SIO) {
6512471Swnj 			if (++um->um_tab.b_errcnt < 7) {
65230917Skarels 				if (tmdiag)
65330917Skarels 					log(LOG_DEBUG,
65430917Skarels 					    "te%d: soft error bn%d er=%b\n",
65530917Skarels 					    minor(bp->b_dev)&03,
65630917Skarels 					    bp->b_blkno, sc->sc_erreg,
65730917Skarels 					    TMER_BITS);
6582471Swnj 				sc->sc_blkno++;
6592617Swnj 				ubadone(um);
6602608Swnj 				goto opcont;
6611919Swnj 			}
6622608Swnj 		} else
6632608Swnj 			/*
6642608Swnj 			 * Hard or non-i/o errors on non-raw tape
6652608Swnj 			 * cause it to close.
6662608Swnj 			 */
6673095Swnj 			if (sc->sc_openf>0 && bp != &rtmbuf[TMUNIT(bp->b_dev)])
6682608Swnj 				sc->sc_openf = -1;
6692608Swnj 		/*
6702608Swnj 		 * Couldn't recover error
6712608Swnj 		 */
67218321Sralph 		tprintf(sc->sc_ttyp,
67318321Sralph 		    "te%d: hard error bn%d er=%b\n", minor(bp->b_dev)&03,
6743095Swnj 		    bp->b_blkno, sc->sc_erreg, TMER_BITS);
67524974Smckusick #ifdef	AVIV
67624974Smckusick 		if (tmdiag) {
67724974Smckusick 			addr->tmmr = DAB;
67824974Smckusick 			printf("reject code 0%o", addr->tmmr & DAB_MASK);
67924974Smckusick 			addr->tmmr = DTS;
68024974Smckusick 			if (addr->tmmr & DTS_MASK)
68124974Smckusick 			    printf(", dead track 0%o", addr->tmmr & DTS_MASK);
68224974Smckusick 			addr->tmmr = RWERR;
68324974Smckusick 			printf(", read/write errors %b\n",
68424974Smckusick 			    addr->tmmr & RWERR_MASK,
68524974Smckusick 			    RWERR_BITS);
68624974Smckusick 			addr->tmmr = DRSENSE;
68724974Smckusick 			printf("drive sense %b, ", addr->tmmr & DRSENSE_MASK,
68824974Smckusick 			    DRSENSE_BITS);
68924974Smckusick 			printf("fsr %b\n", addr->tmfsr, FSR_BITS);
69024974Smckusick 		}
69124974Smckusick #endif AVIV
6921919Swnj 		bp->b_flags |= B_ERROR;
6932608Swnj 		goto opdone;
6941919Swnj 	}
6952608Swnj 	/*
6962608Swnj 	 * Advance tape control FSM.
6972608Swnj 	 */
6982608Swnj ignoreerr:
6991919Swnj 	switch (state) {
7001919Swnj 
7011919Swnj 	case SIO:
7022608Swnj 		/*
7032608Swnj 		 * Read/write increments tape block number
7042608Swnj 		 */
7052471Swnj 		sc->sc_blkno++;
70630917Skarels 		sc->sc_blks++;
70730917Skarels 		if (um->um_tab.b_errcnt)
70830917Skarels 			sc->sc_softerrs++;
7092608Swnj 		goto opdone;
7101919Swnj 
7111919Swnj 	case SCOM:
7122608Swnj 		/*
7133095Swnj 		 * For forward/backward space record update current position.
7142608Swnj 		 */
7153095Swnj 		if (bp == &ctmbuf[TMUNIT(bp->b_dev)])
71626292Skarels 		switch ((int)bp->b_command) {
7171919Swnj 
7182608Swnj 		case TM_SFORW:
7192608Swnj 			sc->sc_blkno -= bp->b_repcnt;
7203095Swnj 			break;
7211919Swnj 
7222608Swnj 		case TM_SREV:
7232608Swnj 			sc->sc_blkno += bp->b_repcnt;
7243095Swnj 			break;
7251919Swnj 		}
7263095Swnj 		goto opdone;
7271919Swnj 
7281919Swnj 	case SSEEK:
7297381Ssam 		sc->sc_blkno = bdbtofsb(bp->b_blkno);
7302608Swnj 		goto opcont;
7311919Swnj 
7321919Swnj 	default:
7332608Swnj 		panic("tmintr");
7342608Swnj 	}
7352608Swnj opdone:
7362608Swnj 	/*
7372608Swnj 	 * Reset error count and remove
7382608Swnj 	 * from device queue.
7392608Swnj 	 */
7402608Swnj 	um->um_tab.b_errcnt = 0;
7412608Swnj 	dp->b_actf = bp->av_forw;
74214929Skarels 	/*
74314929Skarels 	 * Check resid; watch out for resid >32767 (tmbc not negative).
74414929Skarels 	 */
74515081Skarels 	bp->b_resid = ((int) -addr->tmbc) & 0xffff;
7462617Swnj 	ubadone(um);
7472608Swnj 	iodone(bp);
7482608Swnj 	/*
7492608Swnj 	 * Circulate slave to end of controller
7502608Swnj 	 * queue to give other slaves a chance.
7512608Swnj 	 */
7522608Swnj 	um->um_tab.b_actf = dp->b_forw;
7532608Swnj 	if (dp->b_actf) {
7542608Swnj 		dp->b_forw = NULL;
7552608Swnj 		if (um->um_tab.b_actf == NULL)
7562608Swnj 			um->um_tab.b_actf = dp;
7572608Swnj 		else
7582608Swnj 			um->um_tab.b_actl->b_forw = dp;
7592608Swnj 		um->um_tab.b_actl = dp;
7602608Swnj 	}
7612608Swnj 	if (um->um_tab.b_actf == 0)
7621919Swnj 		return;
7632608Swnj opcont:
7642608Swnj 	tmstart(um);
7651919Swnj }
7661919Swnj 
7673495Sroot tmtimer(dev)
7683495Sroot 	int dev;
7693495Sroot {
7703495Sroot 	register struct te_softc *sc = &te_softc[TEUNIT(dev)];
7714847Sroot 	register short x;
7723495Sroot 
7733495Sroot 	if (sc->sc_timo != INF && (sc->sc_timo -= 5) < 0) {
7744278Sroot 		printf("te%d: lost interrupt\n", TEUNIT(dev));
7753495Sroot 		sc->sc_timo = INF;
7764847Sroot 		x = spl5();
7773495Sroot 		tmintr(TMUNIT(dev));
7784847Sroot 		(void) splx(x);
7793495Sroot 	}
7803629Sroot 	timeout(tmtimer, (caddr_t)dev, 5*hz);
7813495Sroot }
7823495Sroot 
7831919Swnj tmseteof(bp)
7841919Swnj 	register struct buf *bp;
7851919Swnj {
7863095Swnj 	register int teunit = TEUNIT(bp->b_dev);
7875692Sroot 	register struct tmdevice *addr =
7885692Sroot 	    (struct tmdevice *)tedinfo[teunit]->ui_addr;
7893095Swnj 	register struct te_softc *sc = &te_softc[teunit];
7901919Swnj 
7913095Swnj 	if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) {
7927381Ssam 		if (sc->sc_blkno > bdbtofsb(bp->b_blkno)) {
7931919Swnj 			/* reversing */
7947381Ssam 			sc->sc_nxrec = bdbtofsb(bp->b_blkno) - addr->tmbc;
7952471Swnj 			sc->sc_blkno = sc->sc_nxrec;
7961919Swnj 		} else {
7971919Swnj 			/* spacing forward */
7987381Ssam 			sc->sc_blkno = bdbtofsb(bp->b_blkno) + addr->tmbc;
7992471Swnj 			sc->sc_nxrec = sc->sc_blkno - 1;
8001919Swnj 		}
8011919Swnj 		return;
8021919Swnj 	}
8031919Swnj 	/* eof on read */
8047381Ssam 	sc->sc_nxrec = bdbtofsb(bp->b_blkno);
8051919Swnj }
8061919Swnj 
8077732Sroot tmread(dev, uio)
8082608Swnj 	dev_t dev;
8097732Sroot 	struct uio *uio;
8101919Swnj {
8118163Sroot 	int errno;
8121919Swnj 
8138163Sroot 	errno = tmphys(dev, uio);
8148163Sroot 	if (errno)
8158163Sroot 		return (errno);
8168163Sroot 	return (physio(tmstrategy, &rtmbuf[TMUNIT(dev)], dev, B_READ, minphys, uio));
8171919Swnj }
8181919Swnj 
8197838Sroot tmwrite(dev, uio)
8202608Swnj 	dev_t dev;
8217838Sroot 	struct uio *uio;
8221919Swnj {
8238163Sroot 	int errno;
8241919Swnj 
8258163Sroot 	errno = tmphys(dev, uio);
8268163Sroot 	if (errno)
8278163Sroot 		return (errno);
8288163Sroot 	return (physio(tmstrategy, &rtmbuf[TMUNIT(dev)], dev, B_WRITE, minphys, uio));
8291919Swnj }
8301919Swnj 
8313095Swnj /*
8323095Swnj  * Check that a raw device exists.
8333095Swnj  * If it does, set up sc_blkno and sc_nxrec
8343095Swnj  * so that the tape will appear positioned correctly.
8353095Swnj  */
8367732Sroot tmphys(dev, uio)
8372608Swnj 	dev_t dev;
8387732Sroot 	struct uio *uio;
8391919Swnj {
8403095Swnj 	register int teunit = TEUNIT(dev);
8411919Swnj 	register daddr_t a;
8423095Swnj 	register struct te_softc *sc;
8433095Swnj 	register struct uba_device *ui;
8441919Swnj 
8457838Sroot 	if (teunit >= NTE || (ui=tedinfo[teunit]) == 0 || ui->ui_alive == 0)
8467732Sroot 		return (ENXIO);
8473095Swnj 	sc = &te_softc[teunit];
8487838Sroot 	a = bdbtofsb(uio->uio_offset >> 9);
8492471Swnj 	sc->sc_blkno = a;
8502471Swnj 	sc->sc_nxrec = a + 1;
8517732Sroot 	return (0);
8521919Swnj }
8531919Swnj 
8542608Swnj tmreset(uban)
8552608Swnj 	int uban;
8562608Swnj {
8572982Swnj 	register struct uba_ctlr *um;
8583095Swnj 	register tm11, teunit;
8592982Swnj 	register struct uba_device *ui;
8602608Swnj 	register struct buf *dp;
8612608Swnj 
8622630Swnj 	for (tm11 = 0; tm11 < NTM; tm11++) {
8632630Swnj 		if ((um = tmminfo[tm11]) == 0 || um->um_alive == 0 ||
8642608Swnj 		   um->um_ubanum != uban)
8652608Swnj 			continue;
8662928Swnj 		printf(" tm%d", tm11);
8672608Swnj 		um->um_tab.b_active = 0;
8682608Swnj 		um->um_tab.b_actf = um->um_tab.b_actl = 0;
8692608Swnj 		if (um->um_ubinfo) {
8702608Swnj 			printf("<%d>", (um->um_ubinfo>>28)&0xf);
8719355Ssam 			um->um_ubinfo = 0;
8722608Swnj 		}
8735692Sroot 		((struct tmdevice *)(um->um_addr))->tmcs = TM_DCLR;
8743095Swnj 		for (teunit = 0; teunit < NTE; teunit++) {
8753095Swnj 			if ((ui = tedinfo[teunit]) == 0 || ui->ui_mi != um ||
8763095Swnj 			    ui->ui_alive == 0)
8772608Swnj 				continue;
8783095Swnj 			dp = &teutab[teunit];
8792608Swnj 			dp->b_active = 0;
8802608Swnj 			dp->b_forw = 0;
8812608Swnj 			if (um->um_tab.b_actf == NULL)
8822608Swnj 				um->um_tab.b_actf = dp;
8832608Swnj 			else
8842608Swnj 				um->um_tab.b_actl->b_forw = dp;
8852608Swnj 			um->um_tab.b_actl = dp;
8863495Sroot 			if (te_softc[teunit].sc_openf > 0)
8873495Sroot 				te_softc[teunit].sc_openf = -1;
8882608Swnj 		}
8892608Swnj 		tmstart(um);
8902608Swnj 	}
8912608Swnj }
8922608Swnj 
8931919Swnj /*ARGSUSED*/
8947632Ssam tmioctl(dev, cmd, data, flag)
8957632Ssam 	caddr_t data;
8961919Swnj 	dev_t dev;
8971919Swnj {
8983095Swnj 	int teunit = TEUNIT(dev);
8993095Swnj 	register struct te_softc *sc = &te_softc[teunit];
9003095Swnj 	register struct buf *bp = &ctmbuf[TMUNIT(dev)];
9011919Swnj 	register callcount;
9021919Swnj 	int fcount;
9037632Ssam 	struct mtop *mtop;
9047632Ssam 	struct mtget *mtget;
9051919Swnj 	/* we depend of the values and order of the MT codes here */
9062608Swnj 	static tmops[] =
9072608Swnj 	   {TM_WEOF,TM_SFORW,TM_SREV,TM_SFORW,TM_SREV,TM_REW,TM_OFFL,TM_SENSE};
9081919Swnj 
9092608Swnj 	switch (cmd) {
9107632Ssam 
9117632Ssam 	case MTIOCTOP:	/* tape operation */
9127632Ssam 		mtop = (struct mtop *)data;
9138574Sroot 		switch (mtop->mt_op) {
9147632Ssam 
9152608Swnj 		case MTWEOF:
9167632Ssam 			callcount = mtop->mt_count;
9172608Swnj 			fcount = 1;
9182608Swnj 			break;
9197632Ssam 
9202608Swnj 		case MTFSF: case MTBSF:
9217632Ssam 			callcount = mtop->mt_count;
9221919Swnj 			fcount = INF;
9231919Swnj 			break;
9247632Ssam 
9251919Swnj 		case MTFSR: case MTBSR:
9261919Swnj 			callcount = 1;
9277632Ssam 			fcount = mtop->mt_count;
9281919Swnj 			break;
9297632Ssam 
9302324Skre 		case MTREW: case MTOFFL: case MTNOP:
9311919Swnj 			callcount = 1;
9321919Swnj 			fcount = 1;
9331919Swnj 			break;
9347632Ssam 
9351919Swnj 		default:
9368574Sroot 			return (ENXIO);
9371919Swnj 		}
9388574Sroot 		if (callcount <= 0 || fcount <= 0)
9398574Sroot 			return (EINVAL);
9402608Swnj 		while (--callcount >= 0) {
9417632Ssam 			tmcommand(dev, tmops[mtop->mt_op], fcount);
9427632Ssam 			if ((mtop->mt_op == MTFSR || mtop->mt_op == MTBSR) &&
9438574Sroot 			    bp->b_resid)
9448574Sroot 				return (EIO);
9453095Swnj 			if ((bp->b_flags&B_ERROR) || sc->sc_erreg&TMER_BOT)
9461919Swnj 				break;
9471919Swnj 		}
9488648Sroot 		return (geterror(bp));
9497632Ssam 
9501919Swnj 	case MTIOCGET:
9517632Ssam 		mtget = (struct mtget *)data;
9527632Ssam 		mtget->mt_dsreg = sc->sc_dsreg;
9537632Ssam 		mtget->mt_erreg = sc->sc_erreg;
9547632Ssam 		mtget->mt_resid = sc->sc_resid;
9557632Ssam 		mtget->mt_type = MT_ISTM;
9568574Sroot 		break;
9577632Ssam 
9581919Swnj 	default:
9598574Sroot 		return (ENXIO);
9601919Swnj 	}
9618574Sroot 	return (0);
9621919Swnj }
9631919Swnj 
9641919Swnj #define	DBSIZE	20
9651919Swnj 
9662363Swnj tmdump()
9672363Swnj {
9682982Swnj 	register struct uba_device *ui;
9692396Swnj 	register struct uba_regs *up;
9705692Sroot 	register struct tmdevice *addr;
9712426Skre 	int blk, num;
9722426Skre 	int start;
9731919Swnj 
9742426Skre 	start = 0;
9752426Skre 	num = maxfree;
9762426Skre #define	phys(a,b)	((b)((int)(a)&0x7fffffff))
9773095Swnj 	if (tedinfo[0] == 0)
9782887Swnj 		return (ENXIO);
9793095Swnj 	ui = phys(tedinfo[0], struct uba_device *);
9802396Swnj 	up = phys(ui->ui_hd, struct uba_hd *)->uh_physuba;
9813331Swnj 	ubainit(up);
9822324Skre 	DELAY(1000000);
9835692Sroot 	addr = (struct tmdevice *)ui->ui_physaddr;
9842396Swnj 	tmwait(addr);
9852608Swnj 	addr->tmcs = TM_DCLR | TM_GO;
9861919Swnj 	while (num > 0) {
9871919Swnj 		blk = num > DBSIZE ? DBSIZE : num;
9882396Swnj 		tmdwrite(start, blk, addr, up);
9891919Swnj 		start += blk;
9901919Swnj 		num -= blk;
9911919Swnj 	}
9922426Skre 	tmeof(addr);
9932426Skre 	tmeof(addr);
9942426Skre 	tmwait(addr);
9952887Swnj 	if (addr->tmcs&TM_ERR)
9962887Swnj 		return (EIO);
9972608Swnj 	addr->tmcs = TM_REW | TM_GO;
9982471Swnj 	tmwait(addr);
9992363Swnj 	return (0);
10001919Swnj }
10011919Swnj 
10022608Swnj tmdwrite(dbuf, num, addr, up)
10032608Swnj 	register dbuf, num;
10045692Sroot 	register struct tmdevice *addr;
10052396Swnj 	struct uba_regs *up;
10061919Swnj {
10072396Swnj 	register struct pte *io;
10082396Swnj 	register int npf;
10091928Swnj 
10102396Swnj 	tmwait(addr);
10112396Swnj 	io = up->uba_map;
10121919Swnj 	npf = num+1;
10131928Swnj 	while (--npf != 0)
10142982Swnj 		 *(int *)io++ = (dbuf++ | (1<<UBAMR_DPSHIFT) | UBAMR_MRV);
10152396Swnj 	*(int *)io = 0;
10162396Swnj 	addr->tmbc = -(num*NBPG);
10172396Swnj 	addr->tmba = 0;
10182608Swnj 	addr->tmcs = TM_WCOM | TM_GO;
10191919Swnj }
10201919Swnj 
10212396Swnj tmwait(addr)
10225692Sroot 	register struct tmdevice *addr;
10231919Swnj {
10241928Swnj 	register s;
10251919Swnj 
10261919Swnj 	do
10272396Swnj 		s = addr->tmcs;
10282608Swnj 	while ((s & TM_CUR) == 0);
10291919Swnj }
10301919Swnj 
10312396Swnj tmeof(addr)
10325692Sroot 	struct tmdevice *addr;
10331919Swnj {
10341919Swnj 
10352396Swnj 	tmwait(addr);
10362608Swnj 	addr->tmcs = TM_WEOF | TM_GO;
10371919Swnj }
10381919Swnj #endif
1039