xref: /csrg-svn/sys/vax/uba/ts.c (revision 4937)
1*4937Swnj /*	ts.c	4.20	81/11/18	*/
21900Swnj 
31941Swnj #include "ts.h"
43528Swnj #include "te.h"
51900Swnj #if NTS > 0
63647Swnj #if TSDEBUG
73327Swnj #define printd if(tsdebug)printf
83327Swnj int tsdebug;
93647Swnj #endif
101900Swnj /*
111900Swnj  * TS11 tape driver
123244Swnj  *
133244Swnj  * TODO:
143244Swnj  *	test driver with more than one controller
153244Swnj  *	test reset code
163244Swnj  *	test dump code
173244Swnj  *	test rewinds without hanging in driver
183244Swnj  *	what happens if you offline tape during rewind?
193244Swnj  *	test using file system on tape
201900Swnj  */
211900Swnj #include "../h/param.h"
221900Swnj #include "../h/systm.h"
231900Swnj #include "../h/buf.h"
243244Swnj #include "../h/dir.h"
251900Swnj #include "../h/conf.h"
263244Swnj #include "../h/user.h"
271900Swnj #include "../h/file.h"
283244Swnj #include "../h/map.h"
291900Swnj #include "../h/pte.h"
301947Swnj #include "../h/vm.h"
313244Swnj #include "../h/ubareg.h"
323244Swnj #include "../h/ubavar.h"
333244Swnj #include "../h/mtio.h"
343244Swnj #include "../h/ioctl.h"
353244Swnj #include "../h/cmap.h"
363244Swnj #include "../h/cpu.h"
371900Swnj 
383244Swnj #include "../h/tsreg.h"
391900Swnj 
403244Swnj /*
413244Swnj  * There is a ctsbuf per tape controller.
423244Swnj  * It is used as the token to pass to the internal routines
433244Swnj  * to execute tape ioctls.
443244Swnj  * In particular, when the tape is rewinding on close we release
453244Swnj  * the user process but any further attempts to use the tape drive
463244Swnj  * before the rewind completes will hang waiting for ctsbuf.
473244Swnj  */
483244Swnj struct	buf	ctsbuf[NTS];
491900Swnj 
503244Swnj /*
513244Swnj  * Raw tape operations use rtsbuf.  The driver
523244Swnj  * notices when rtsbuf is being used and allows the user
533244Swnj  * program to continue after errors and read records
543244Swnj  * not of the standard length (BSIZE).
553244Swnj  */
563244Swnj struct	buf	rtsbuf[NTS];
571900Swnj 
583244Swnj /*
593244Swnj  * Driver unibus interface routines and variables.
603244Swnj  */
613244Swnj int	tsprobe(), tsslave(), tsattach(), tsdgo(), tsintr();
623244Swnj struct	uba_ctlr *tsminfo[NTS];
633244Swnj struct	uba_device *tsdinfo[NTS];
643327Swnj struct buf	tsbuf[NTS];
653244Swnj u_short	tsstd[] = { 0772520, 0 };
663244Swnj /*** PROBABLY DON'T NEED ALL THESE SINCE CONTROLLER == DRIVE ***/
673244Swnj struct	uba_driver zsdriver =
683327Swnj  { tsprobe, tsslave, tsattach, tsdgo, tsstd, "ts", tsdinfo, "zs", tsminfo, 0 };
691900Swnj 
703244Swnj /* bits in minor device */
713244Swnj #define	TSUNIT(dev)	(minor(dev)&03)
723244Swnj #define	T_NOREWIND	04
731900Swnj 
743244Swnj #define	INF	(daddr_t)1000000L
751900Swnj 
763244Swnj /*
773244Swnj  * Software state per tape transport.
783244Swnj  * Also contains hardware state in message packets.
793244Swnj  *
803244Swnj  * 1. A tape drive is a unique-open device; we refuse opens when it is already.
813244Swnj  * 2. We keep track of the current position on a block tape and seek
823244Swnj  *    before operations by forward/back spacing if necessary.
833244Swnj  * 3. We remember if the last operation was a write on a tape, so if a tape
843244Swnj  *    is open read write and the last thing done is a write we can
853244Swnj  *    write a standard end of tape mark (two eofs).
863244Swnj  * 4. We remember the status registers after the last command, using
873244Swnj  *    then internally and returning them to the SENSE ioctl.
883244Swnj  */
893244Swnj struct	ts_softc {
903244Swnj 	char	sc_openf;	/* lock against multiple opens */
913244Swnj 	char	sc_lastiow;	/* last op was a write */
923327Swnj 	short	sc_resid;	/* copy of last bc */
933244Swnj 	daddr_t	sc_blkno;	/* block number, for block device tape */
943244Swnj 	daddr_t	sc_nxrec;	/* position of end of tape, if known */
953244Swnj 	struct ts_cmd sc_cmd;	/* the command packet - ADDR MUST BE 0 MOD 4 */
963244Swnj 	struct ts_sts sc_sts;	/* status packet, for returned status */
973244Swnj 	struct ts_char sc_char;	/* characteristics packet */
983244Swnj 	u_short	sc_uba;		/* Unibus addr of cmd pkt for tsdb */
993244Swnj } ts_softc[NTS];
1001900Swnj 
1013244Swnj struct ts_softc *ts_ubaddr;	/* Unibus address of ts_softc */
1021900Swnj 
1033244Swnj /*
1043244Swnj  * States for um->um_tab.b_active, the per controller state flag.
1053244Swnj  * This is used to sequence control in the driver.
1063244Swnj  */
1073244Swnj #define	SSEEK	1		/* seeking */
1083244Swnj #define	SIO	2		/* doing seq i/o */
1093244Swnj #define	SCOM	3		/* sending control command */
1103244Swnj #define	SREW	4		/* sending a drive rewind */
1111900Swnj 
1123520Sroot #if NTM > 0
1133520Sroot /* kludge... see tm.c */
1143520Sroot extern	havetm;
1153520Sroot #endif
1163244Swnj /*
1173244Swnj  * Determine if there is a controller for
1183244Swnj  * a ts at address reg.  Our goal is to make the
1193244Swnj  * device interrupt.
1203244Swnj  */
1213985Sroot /*ARGSUSED*/
1223244Swnj tsprobe(reg)
1233244Swnj 	caddr_t reg;
1243244Swnj {
1253244Swnj 	register int br, cvec;		/* must be r11,r10; value-result */
1261900Swnj 
1273244Swnj #ifdef lint
1283244Swnj 	br = 0; cvec = br; br = cvec;
129*4937Swnj 	tsintr(0);
1303244Swnj #endif
1311900Swnj 
132*4937Swnj 	/*
133*4937Swnj 	 * Too hard to make it interrupt; don't try.
134*4937Swnj 	 */
1353520Sroot #if NTM > 0
1363520Sroot 	if (havetm)
1373520Sroot 		return (0);
1383520Sroot #endif
1393244Swnj 	cvec = 0224;
1403244Swnj 	br = 0x15;
1413982Sroot 	return (1);
1423244Swnj }
1431900Swnj 
1443244Swnj /*
1453244Swnj  * TS11 only supports one drive per controller;
1463244Swnj  * check for ui_slave == 0.
1473244Swnj  *
1483244Swnj  * DO WE REALLY NEED THIS ROUTINE???
1493244Swnj  */
1503244Swnj /*ARGSUSED*/
1513244Swnj tsslave(ui, reg)
1523244Swnj 	struct uba_device *ui;
1533244Swnj 	caddr_t reg;
1543244Swnj {
1551900Swnj 
1563244Swnj 	if (ui->ui_slave)	/* non-zero slave not allowed */
1573244Swnj 		return(0);
1583244Swnj 	return (1);
1593244Swnj }
1601900Swnj 
1613244Swnj /*
1623244Swnj  * Record attachment of the unit to the controller.
1633244Swnj  *
1643244Swnj  * SHOULD THIS ROUTINE DO ANYTHING???
1653244Swnj  */
1663244Swnj /*ARGSUSED*/
1673244Swnj tsattach(ui)
1683244Swnj 	struct uba_device *ui;
1693244Swnj {
1701900Swnj 
1713244Swnj }
1721900Swnj 
1733244Swnj /*
1743244Swnj  * Open the device.  Tapes are unique open
1753244Swnj  * devices, so we refuse if it is already open.
1763244Swnj  * We also check that a tape is available, and
1773244Swnj  * don't block waiting here; if you want to wait
1783244Swnj  * for a tape you should timeout in user code.
1793244Swnj  */
1801900Swnj tsopen(dev, flag)
1813244Swnj 	dev_t dev;
1823244Swnj 	int flag;
1831900Swnj {
1843244Swnj 	register int tsunit;
1853244Swnj 	register struct uba_device *ui;
1863244Swnj 	register struct ts_softc *sc;
1871900Swnj 
1883244Swnj 	tsunit = TSUNIT(dev);
1893244Swnj 	if (tsunit>=NTS || (sc = &ts_softc[tsunit])->sc_openf ||
1903244Swnj 	    (ui = tsdinfo[tsunit]) == 0 || ui->ui_alive == 0) {
1911900Swnj 		u.u_error = ENXIO;
1921900Swnj 		return;
1931900Swnj 	}
1943244Swnj 	if (tsinit(tsunit)) {
1951900Swnj 		u.u_error = ENXIO;
1963647Swnj #ifdef TSDEBUG
1973327Swnj 		printd("init failed\n");
1983647Swnj #endif
1991900Swnj 		return;
2001900Swnj 	}
2013647Swnj #ifdef TSDEBUG
2023327Swnj 	printd("init ok\n");
2033647Swnj #endif
2043244Swnj 	tscommand(dev, TS_SENSE, 1);
2053647Swnj #ifdef TSDEBUG
2063327Swnj 	printd("sense xs0 %o\n", sc->sc_sts.s_xs0);
2073647Swnj #endif
2083711Sroot 	if ((sc->sc_sts.s_xs0&TS_ONL) == 0) {
2093711Sroot 		uprintf("ts%d: not online\n", tsunit);
2103244Swnj 		u.u_error = EIO;
2113244Swnj 		return;
2121900Swnj 	}
2133718Sroot 	if ((flag&(FREAD|FWRITE)) == FWRITE && (sc->sc_sts.s_xs0&TS_WLK)) {
2143711Sroot 		uprintf("ts%d: no write ring\n", tsunit);
2153711Sroot 		u.u_error = EIO;
2163711Sroot 		return;
2173711Sroot 	}
2183244Swnj 	sc->sc_openf = 1;
2193244Swnj 	sc->sc_blkno = (daddr_t)0;
2203244Swnj 	sc->sc_nxrec = INF;
2213244Swnj 	sc->sc_lastiow = 0;
2221900Swnj }
2231900Swnj 
2243244Swnj /*
2253244Swnj  * Close tape device.
2263244Swnj  *
2273244Swnj  * If tape was open for writing or last operation was
2283244Swnj  * a write, then write two EOF's and backspace over the last one.
2293244Swnj  * Unless this is a non-rewinding special file, rewind the tape.
2303244Swnj  * Make the tape available to others.
2313244Swnj  */
2321900Swnj tsclose(dev, flag)
2333244Swnj 	register dev_t dev;
2343244Swnj 	register flag;
2351900Swnj {
2363244Swnj 	register struct ts_softc *sc = &ts_softc[TSUNIT(dev)];
2371900Swnj 
2383244Swnj 	if (flag == FWRITE || (flag&FWRITE) && sc->sc_lastiow) {
2393244Swnj 		tscommand(dev, TS_WEOF, 1);
2403244Swnj 		tscommand(dev, TS_WEOF, 1);
2413244Swnj 		tscommand(dev, TS_SREV, 1);
2421900Swnj 	}
2433244Swnj 	if ((minor(dev)&T_NOREWIND) == 0)
2443244Swnj 		/*
2453244Swnj 		 * 0 count means don't hang waiting for rewind complete
2463244Swnj 		 * rather ctsbuf stays busy until the operation completes
2473244Swnj 		 * preventing further opens from completing by
2483244Swnj 		 * preventing a TS_SENSE from completing.
2493244Swnj 		 */
2503244Swnj 		tscommand(dev, TS_REW, 0);
2513244Swnj 	sc->sc_openf = 0;
2521900Swnj }
2531900Swnj 
2543244Swnj /*
2553244Swnj  * Initialize the TS11.  Set up Unibus mapping for command
2563244Swnj  * packets and set device characteristics.
2573244Swnj  */
2583244Swnj tsinit(unit)
2593244Swnj 	register int unit;
2601900Swnj {
2613244Swnj 	register struct ts_softc *sc = &ts_softc[unit];
2623244Swnj 	register struct uba_ctlr *um = tsminfo[unit];
2633244Swnj 	register struct device *addr = (struct device *)um->um_addr;
2643244Swnj 	register int i;
2653244Swnj 
2663244Swnj 	/*
2673244Swnj 	 * Map the command and message packets into Unibus
2683244Swnj 	 * address space.  We do all the command and message
2693244Swnj 	 * packets at once to minimize the amount of Unibus
2703244Swnj 	 * mapping necessary.
2713244Swnj 	 */
2723244Swnj 	if (ts_ubaddr == 0) {
2733244Swnj 		ctsbuf[unit].b_un.b_addr = (caddr_t)ts_softc;
2743244Swnj 		ctsbuf[unit].b_bcount = sizeof(ts_softc);
2753244Swnj 		i = ubasetup(um->um_ubanum, &ctsbuf[unit], 0);
2763244Swnj 		i &= 0777777;
2773244Swnj 		ts_ubaddr = (struct ts_softc *)i;
2783244Swnj 		/* MAKE SURE WE DON'T GET UNIBUS ADDRESS ZERO */
2793244Swnj 		if (ts_ubaddr == 0)
2803244Swnj 			printf("ts%d: zero ubaddr\n", unit);
2813244Swnj 	}
2823244Swnj 	/*
2833244Swnj 	 * Now initialize the TS11 controller.
2843244Swnj 	 * Set the characteristics.
2853244Swnj 	 */
2863668Swnj 	if (addr->tssr & (TS_NBA|TS_OFL)) {
2873244Swnj 		addr->tssr = 0;		/* subsystem initialize */
2883244Swnj 		tswait(addr);
2893244Swnj 		i = (int)&ts_ubaddr[unit].sc_cmd;	/* Unibus addr of cmd */
2903327Swnj 		if (i&3) {
2913327Swnj 			printf("addr mod 4 != 0\n");
2923327Swnj 			return(1);
2933327Swnj 		}
2943244Swnj 		sc->sc_uba = (u_short)(i + ((i>>16)&3));
2953244Swnj 		sc->sc_char.char_addr = (int)&ts_ubaddr[unit].sc_sts;
2963244Swnj 		sc->sc_char.char_size = sizeof(struct ts_sts);
2973244Swnj 		sc->sc_char.char_mode = TS_ESS;
2983244Swnj 		sc->sc_cmd.c_cmd = TS_ACK | TS_SETCHR;
2993327Swnj 		i = (int)&ts_ubaddr[unit].sc_char;
3003327Swnj 		sc->sc_cmd.c_loba = i;
3013327Swnj 		sc->sc_cmd.c_hiba = (i>>16)&3;
3023244Swnj 		sc->sc_cmd.c_size = sizeof(struct ts_char);
3033244Swnj 		addr->tsdb = sc->sc_uba;
3043244Swnj 		tswait(addr);
3053327Swnj /*
3063327Swnj 		printd("%o %o %o %o %o %o %o %o\n", addr->tssr, sc->sc_sts.s_sts, sc->sc_sts.s_len, sc->sc_sts.s_rbpcr, sc->sc_sts.s_xs0, sc->sc_sts.s_xs1,sc->sc_sts.s_xs1,sc->sc_sts.s_xs2,sc->sc_sts.s_xs3);
3073327Swnj */
3083327Swnj 		if (addr->tssr & TS_NBA)
3093327Swnj 			return(1);
3103244Swnj 	}
3113244Swnj 	return(0);
3123244Swnj }
3133244Swnj 
3143244Swnj /*
3153244Swnj  * Execute a command on the tape drive
3163244Swnj  * a specified number of times.
3173244Swnj  */
3183244Swnj tscommand(dev, com, count)
3193244Swnj 	dev_t dev;
3203244Swnj 	int com, count;
3213244Swnj {
3221900Swnj 	register struct buf *bp;
3231900Swnj 
3243244Swnj 	bp = &ctsbuf[TSUNIT(dev)];
3253244Swnj 	(void) spl5();
3263244Swnj 	while (bp->b_flags&B_BUSY) {
3273244Swnj 		/*
3283244Swnj 		 * This special check is because B_BUSY never
3293244Swnj 		 * gets cleared in the non-waiting rewind case.
3303244Swnj 		 */
3313244Swnj 		if (bp->b_repcnt == 0 && (bp->b_flags&B_DONE))
3323244Swnj 			break;
3331900Swnj 		bp->b_flags |= B_WANTED;
3341900Swnj 		sleep((caddr_t)bp, PRIBIO);
3351900Swnj 	}
3363244Swnj 	bp->b_flags = B_BUSY|B_READ;
3373244Swnj 	(void) spl0();
3383647Swnj #ifdef TSDEBUG
3393327Swnj 	printd("command %o dev %x count %d\n", com, dev, count);
3403647Swnj #endif
3413244Swnj 	bp->b_dev = dev;
3423244Swnj 	bp->b_repcnt = count;
3433244Swnj 	bp->b_command = com;
3441900Swnj 	bp->b_blkno = 0;
3451900Swnj 	tsstrategy(bp);
3463244Swnj 	/*
3473244Swnj 	 * In case of rewind from close, don't wait.
3483244Swnj 	 * This is the only case where count can be 0.
3493244Swnj 	 */
3503244Swnj 	if (count == 0)
3513244Swnj 		return;
3521900Swnj 	iowait(bp);
3533244Swnj 	if (bp->b_flags&B_WANTED)
3541900Swnj 		wakeup((caddr_t)bp);
3553244Swnj 	bp->b_flags &= B_ERROR;
3561900Swnj }
3571900Swnj 
3583244Swnj /*
3593244Swnj  * Queue a tape operation.
3603244Swnj  */
3611900Swnj tsstrategy(bp)
3623244Swnj 	register struct buf *bp;
3631900Swnj {
3643244Swnj 	int tsunit = TSUNIT(bp->b_dev);
3653244Swnj 	register struct uba_ctlr *um;
3663327Swnj 	register struct buf *dp;
3671900Swnj 
3683244Swnj 	/*
3693244Swnj 	 * Put transfer at end of controller queue
3703244Swnj 	 */
3711900Swnj 	bp->av_forw = NULL;
3723244Swnj 	um = tsdinfo[tsunit]->ui_mi;
3733327Swnj 	dp = &tsbuf[tsunit];
3743244Swnj 	(void) spl5();
3753327Swnj 	if (dp->b_actf == NULL)
3763327Swnj 		dp->b_actf = bp;
3771900Swnj 	else
3783327Swnj 		dp->b_actl->av_forw = bp;
3793327Swnj 	dp->b_actl = bp;
3803327Swnj 	um->um_tab.b_actf = um->um_tab.b_actl = dp;
3813244Swnj 	/*
3823244Swnj 	 * If the controller is not busy, get
3833244Swnj 	 * it going.
3843244Swnj 	 */
3853244Swnj 	if (um->um_tab.b_active == 0)
3863244Swnj 		tsstart(um);
3873244Swnj 	(void) spl0();
3881900Swnj }
3891900Swnj 
3903244Swnj /*
3913244Swnj  * Start activity on a ts controller.
3923244Swnj  */
3933244Swnj tsstart(um)
3943244Swnj 	register struct uba_ctlr *um;
3951900Swnj {
3961900Swnj 	register struct buf *bp;
3973244Swnj 	register struct device *addr = (struct device *)um->um_addr;
3983244Swnj 	register struct ts_softc *sc;
3993244Swnj 	register struct ts_cmd *tc;
4003244Swnj 	register struct uba_device *ui;
4013244Swnj 	int tsunit, cmd;
4021900Swnj 	daddr_t blkno;
4031900Swnj 
4043244Swnj 	/*
4053244Swnj 	 * Start the controller if there is something for it to do.
4063244Swnj 	 */
4073244Swnj loop:
4083327Swnj 	if ((bp = um->um_tab.b_actf->b_actf) == NULL)
4091900Swnj 		return;
4103244Swnj 	tsunit = TSUNIT(bp->b_dev);
4113244Swnj 	ui = tsdinfo[tsunit];
4123244Swnj 	sc = &ts_softc[tsunit];
4133244Swnj 	tc = &sc->sc_cmd;
4143244Swnj 	/*
4153244Swnj 	 * Default is that last command was NOT a write command;
4163244Swnj 	 * if we do a write command we will notice this in tsintr().
4173244Swnj 	 */
4183656Swnj 	sc->sc_lastiow = 0;
4193244Swnj 	if (sc->sc_openf < 0 || (addr->tssr&TS_OFL)) {
4203244Swnj 		/*
4213244Swnj 		 * Have had a hard error on a non-raw tape
4223244Swnj 		 * or the tape unit is now unavailable
4233244Swnj 		 * (e.g. taken off line).
4243244Swnj 		 */
4253244Swnj 		bp->b_flags |= B_ERROR;
4263244Swnj 		goto next;
4273244Swnj 	}
4283244Swnj 	if (bp == &ctsbuf[TSUNIT(bp->b_dev)]) {
4293244Swnj 		/*
4303244Swnj 		 * Execute control operation with the specified count.
4313244Swnj 		 */
4323244Swnj 		um->um_tab.b_active =
4333244Swnj 		    bp->b_command == TS_REW ? SREW : SCOM;
4343244Swnj 		tc->c_repcnt = bp->b_repcnt;
4353647Swnj #ifdef TSDEBUG
4363327Swnj 		printd("strat: do cmd\n");
4373647Swnj #endif
4383244Swnj 		goto dobpcmd;
4393244Swnj 	}
4403244Swnj 	/*
4413244Swnj 	 * The following checks handle boundary cases for operation
4423244Swnj 	 * on non-raw tapes.  On raw tapes the initialization of
4433244Swnj 	 * sc->sc_nxrec by tsphys causes them to be skipped normally
4443244Swnj 	 * (except in the case of retries).
4453244Swnj 	 */
4463244Swnj 	if (dbtofsb(bp->b_blkno) > sc->sc_nxrec) {
4473244Swnj 		/*
4483244Swnj 		 * Can't read past known end-of-file.
4493244Swnj 		 */
4503244Swnj 		bp->b_flags |= B_ERROR;
4513244Swnj 		bp->b_error = ENXIO;
4523244Swnj 		goto next;
4533244Swnj 	}
4543244Swnj 	if (dbtofsb(bp->b_blkno) == sc->sc_nxrec &&
4553244Swnj 	    bp->b_flags&B_READ) {
4563244Swnj 		/*
4573244Swnj 		 * Reading at end of file returns 0 bytes.
4583244Swnj 		 */
4593244Swnj 		bp->b_resid = bp->b_bcount;
4603244Swnj 		clrbuf(bp);
4613244Swnj 		goto next;
4623244Swnj 	}
4633244Swnj 	if ((bp->b_flags&B_READ) == 0)
4643244Swnj 		/*
4653244Swnj 		 * Writing sets EOF
4663244Swnj 		 */
4673244Swnj 		sc->sc_nxrec = dbtofsb(bp->b_blkno) + 1;
4683244Swnj 	/*
4693244Swnj 	 * If the data transfer command is in the correct place,
4703244Swnj 	 * set up all the registers except the csr, and give
4713244Swnj 	 * control over to the UNIBUS adapter routines, to
4723244Swnj 	 * wait for resources to start the i/o.
4733244Swnj 	 */
4743244Swnj 	if ((blkno = sc->sc_blkno) == dbtofsb(bp->b_blkno)) {
4753244Swnj 		tc->c_size = bp->b_bcount;
4763244Swnj 		if ((bp->b_flags&B_READ) == 0)
4773244Swnj 			cmd = TS_WCOM;
4781900Swnj 		else
4793244Swnj 			cmd = TS_RCOM;
4803244Swnj 		if (um->um_tab.b_errcnt)
4813244Swnj 			cmd |= TS_RETRY;
4823244Swnj 		um->um_tab.b_active = SIO;
4833327Swnj 		tc->c_cmd = TS_ACK | TS_CVC | TS_IE | cmd;
4843647Swnj #ifdef TSDEBUG
4853327Swnj 		printd("r/w %o size %d\n", tc->c_cmd, tc->c_size);
4863647Swnj #endif
4873244Swnj 		(void) ubago(ui);
4883244Swnj 		return;
4893244Swnj 	}
4903244Swnj 	/*
4913244Swnj 	 * Tape positioned incorrectly;
4923244Swnj 	 * set to seek forwards or backwards to the correct spot.
4933244Swnj 	 * This happens for raw tapes only on error retries.
4943244Swnj 	 */
4953244Swnj 	um->um_tab.b_active = SSEEK;
4963647Swnj #ifdef TSDEBUG
4973327Swnj 	printd("seek blkno %d b_blkno %d\n", blkno, bp->b_blkno);
4983647Swnj #endif
4993244Swnj 	if (blkno < dbtofsb(bp->b_blkno)) {
5003244Swnj 		bp->b_command = TS_SFORW;
5013244Swnj 		tc->c_repcnt = dbtofsb(bp->b_blkno) - blkno;
5021900Swnj 	} else {
5033244Swnj 		bp->b_command = TS_SREV;
5043244Swnj 		tc->c_repcnt = blkno - dbtofsb(bp->b_blkno);
5051900Swnj 	}
5063244Swnj dobpcmd:
5073244Swnj 	/*
5083244Swnj 	 * Do the command in bp.
5093244Swnj 	 */
5103327Swnj 	tc->c_cmd = TS_ACK | TS_CVC | TS_IE | bp->b_command;
5113244Swnj 	addr->tsdb = sc->sc_uba;
5121900Swnj 	return;
5131900Swnj 
5143244Swnj next:
5153244Swnj 	/*
5163244Swnj 	 * Done with this operation due to error or
5173244Swnj 	 * the fact that it doesn't do anything.
5183244Swnj 	 * Release UBA resources (if any), dequeue
5193244Swnj 	 * the transfer and continue processing this slave.
5203244Swnj 	 */
5213244Swnj 	if (um->um_ubinfo)
5223244Swnj 		ubadone(um);
5233244Swnj 	um->um_tab.b_errcnt = 0;
5243327Swnj 	um->um_tab.b_actf->b_actf = bp->av_forw;
5251900Swnj 	iodone(bp);
5261900Swnj 	goto loop;
5271900Swnj }
5281900Swnj 
5293244Swnj /*
5303244Swnj  * The UNIBUS resources we needed have been
5313244Swnj  * allocated to us; start the device.
5323244Swnj  */
5333244Swnj tsdgo(um)
5343244Swnj 	register struct uba_ctlr *um;
5351900Swnj {
5363244Swnj 	register struct device *addr = (struct device *)um->um_addr;
5373244Swnj 	register struct ts_softc *sc = &ts_softc[um->um_ctlr];
5383327Swnj 	register int i;
5393244Swnj 
5403327Swnj 	i = um->um_ubinfo & 0777777;
5413647Swnj #ifdef TSDEBUG
5423327Swnj 	printd("dgo addr %o\n", i);
5433647Swnj #endif
5443327Swnj 	sc->sc_cmd.c_loba = i;
5453327Swnj 	sc->sc_cmd.c_hiba = (i>>16)&3;
5463244Swnj 	addr->tsdb = sc->sc_uba;
5473244Swnj }
5483244Swnj 
5493244Swnj /*
5503244Swnj  * Ts interrupt routine.
5513244Swnj  */
5523244Swnj /*ARGSUSED*/
5533244Swnj tsintr(ts11)
5543244Swnj 	int ts11;
5553244Swnj {
5561900Swnj 	register struct buf *bp;
5573244Swnj 	register struct uba_ctlr *um = tsminfo[ts11];
5583244Swnj 	register struct device *addr;
5593244Swnj 	register struct ts_softc *sc;
5603244Swnj 	int tsunit;
5613244Swnj 	register state;
5621900Swnj 
5633647Swnj #ifdef TSDEBUG
5643327Swnj 	printd("intr\n");
5653647Swnj #endif
5663327Swnj 	if ((bp = um->um_tab.b_actf->b_actf) == NULL)
5671900Swnj 		return;
5683244Swnj 	tsunit = TSUNIT(bp->b_dev);
5693244Swnj 	addr = (struct device *)tsdinfo[tsunit]->ui_addr;
5703244Swnj 	/*
5713244Swnj 	 * If last command was a rewind, and tape is still
5723244Swnj 	 * rewinding, wait for the rewind complete interrupt.
5733244Swnj 	 *
5743244Swnj 	 * SHOULD NEVER GET AN INTERRUPT IN THIS STATE.
5753244Swnj 	 */
5763244Swnj 	if (um->um_tab.b_active == SREW) {
5773244Swnj 		um->um_tab.b_active = SCOM;
5783244Swnj 		if ((addr->tssr&TS_SSR) == 0)
5793244Swnj 			return;
5803244Swnj 	}
5813244Swnj 	/*
5823244Swnj 	 * An operation completed... record status
5833244Swnj 	 */
5843647Swnj #ifdef TSDEBUG
5853327Swnj 	printd("  ok1\n");
5863647Swnj #endif
5873244Swnj 	sc = &ts_softc[tsunit];
5883244Swnj 	if ((bp->b_flags & B_READ) == 0)
5893244Swnj 		sc->sc_lastiow = 1;
5903244Swnj 	state = um->um_tab.b_active;
5913244Swnj 	um->um_tab.b_active = 0;
5923244Swnj 	/*
5933244Swnj 	 * Check for errors.
5943244Swnj 	 */
5953244Swnj 	if (addr->tssr&TS_SC) {
5963244Swnj 		switch (addr->tssr & TS_TC) {
5973244Swnj 		case TS_UNREC:		/* unrecoverable */
5983244Swnj 		case TS_FATAL:		/* fatal error */
5993244Swnj 		case TS_ATTN:		/* attention (shouldn't happen) */
6003244Swnj 		case TS_RECNM:		/* recoverable, no motion */
6013244Swnj 			break;
6021900Swnj 
6033244Swnj 		case TS_SUCC:		/* success termination */
6043244Swnj 			printf("ts%d: success\n", TSUNIT(minor(bp->b_dev)));
6053244Swnj 			goto ignoreerr;
6061900Swnj 
6073244Swnj 		case TS_ALERT:		/* tape status alert */
6083244Swnj 			/*
6093244Swnj 			 * If we hit the end of the tape file,
6103244Swnj 			 * update our position.
6113244Swnj 			 */
6123244Swnj 			if (sc->sc_sts.s_xs0 & (TS_TMK|TS_EOT)) {
6133244Swnj 				tsseteof(bp);		/* set blkno and nxrec */
6143244Swnj 				state = SCOM;		/* force completion */
6153244Swnj 				/*
6163244Swnj 				 * Stuff bc so it will be unstuffed correctly
6173244Swnj 				 * later to get resid.
6183244Swnj 				 */
6193244Swnj 				sc->sc_sts.s_rbpcr = bp->b_bcount;
6203244Swnj 				goto opdone;
6213244Swnj 			}
6223244Swnj 			/*
6233244Swnj 			 * If we were reading raw tape and the record was too long
6243244Swnj 			 * or too short, then we don't consider this an error.
6253244Swnj 			 */
6263244Swnj 			if (bp == &rtsbuf[TSUNIT(bp->b_dev)] && (bp->b_flags&B_READ) &&
6273244Swnj 			    sc->sc_sts.s_xs0&(TS_RLS|TS_RLL))
6283244Swnj 				goto ignoreerr;
6293244Swnj 		case TS_RECOV:		/* recoverable, tape moved */
6303244Swnj 			/*
6313244Swnj 			 * If this was an i/o operation retry up to 8 times.
6323244Swnj 			 */
6333244Swnj 			if (state==SIO) {
6343244Swnj 				if (++um->um_tab.b_errcnt < 7) {
6353244Swnj 					ubadone(um);
6363244Swnj 					goto opcont;
6373244Swnj 				} else
6383244Swnj 					sc->sc_blkno++;
6393244Swnj 			} else {
6403244Swnj 				/*
6413244Swnj 				 * Non-i/o errors on non-raw tape
6423244Swnj 				 * cause it to close.
6433244Swnj 				 */
6443244Swnj 				if (sc->sc_openf>0 && bp != &rtsbuf[TSUNIT(bp->b_dev)])
6453244Swnj 					sc->sc_openf = -1;
6463244Swnj 			}
6471900Swnj 			break;
6483244Swnj 
6493244Swnj 		case TS_REJECT:		/* function reject */
6503244Swnj 			if (state == SIO && sc->sc_sts.s_xs0 & TS_WLE)
6513244Swnj 				printf("ts%d: write locked\n", TSUNIT(bp->b_dev));
6523244Swnj 			if ((sc->sc_sts.s_xs0 & TS_ONL) == 0)
6533244Swnj 				printf("ts%d: offline\n", TSUNIT(bp->b_dev));
6541900Swnj 			break;
6551900Swnj 		}
6563244Swnj 		/*
6573244Swnj 		 * Couldn't recover error
6583244Swnj 		 */
6593647Swnj 		printf("ts%d: hard error bn%d xs0=%b", TSUNIT(bp->b_dev),
6603244Swnj 		    bp->b_blkno, sc->sc_sts.s_xs0, TSXS0_BITS);
6613647Swnj 		if (sc->sc_sts.s_xs1)
6623647Swnj 			printf(" xs1=%b", sc->sc_sts.s_xs1, TSXS1_BITS);
6633647Swnj 		if (sc->sc_sts.s_xs2)
6643647Swnj 			printf(" xs2=%b", sc->sc_sts.s_xs2, TSXS2_BITS);
6653647Swnj 		if (sc->sc_sts.s_xs3)
6663647Swnj 			printf(" xs3=%b", sc->sc_sts.s_xs3, TSXS3_BITS);
6673647Swnj 		printf("\n");
6683244Swnj 		bp->b_flags |= B_ERROR;
6693244Swnj 		goto opdone;
6703244Swnj 	}
6713244Swnj 	/*
6723244Swnj 	 * Advance tape control FSM.
6733244Swnj 	 */
6743244Swnj ignoreerr:
6753244Swnj 	switch (state) {
6761900Swnj 
6773244Swnj 	case SIO:
6783244Swnj 		/*
6793244Swnj 		 * Read/write increments tape block number
6803244Swnj 		 */
6813244Swnj 		sc->sc_blkno++;
6823244Swnj 		goto opdone;
6831900Swnj 
6841900Swnj 	case SCOM:
6853244Swnj 		/*
6863244Swnj 		 * For forward/backward space record update current position.
6873244Swnj 		 */
6883244Swnj 		if (bp == &ctsbuf[TSUNIT(bp->b_dev)])
6893244Swnj 		switch (bp->b_command) {
6901900Swnj 
6913244Swnj 		case TS_SFORW:
6923244Swnj 			sc->sc_blkno += bp->b_repcnt;
6933244Swnj 			break;
6941900Swnj 
6953244Swnj 		case TS_SREV:
6963244Swnj 			sc->sc_blkno -= bp->b_repcnt;
6973244Swnj 			break;
6983244Swnj 		}
6993244Swnj 		goto opdone;
7003244Swnj 
7013244Swnj 	case SSEEK:
7023244Swnj 		sc->sc_blkno = dbtofsb(bp->b_blkno);
7033244Swnj 		goto opcont;
7043244Swnj 
7051900Swnj 	default:
7063244Swnj 		panic("tsintr");
7071900Swnj 	}
7083244Swnj opdone:
7093244Swnj 	/*
7103244Swnj 	 * Reset error count and remove
7113244Swnj 	 * from device queue.
7123244Swnj 	 */
7133244Swnj 	um->um_tab.b_errcnt = 0;
7143327Swnj 	um->um_tab.b_actf->b_actf = bp->av_forw;
7153244Swnj 	bp->b_resid = sc->sc_sts.s_rbpcr;
7163244Swnj 	ubadone(um);
7173647Swnj #ifdef TSDEBUG
7183327Swnj 	printd("  iodone\n");
7193647Swnj #endif
7203244Swnj 	iodone(bp);
7213327Swnj 	if (um->um_tab.b_actf->b_actf == 0)
7223244Swnj 		return;
7233244Swnj opcont:
7243244Swnj 	tsstart(um);
7253244Swnj }
7263244Swnj 
7273244Swnj tsseteof(bp)
7283244Swnj 	register struct buf *bp;
7293244Swnj {
7303244Swnj 	register int tsunit = TSUNIT(bp->b_dev);
7313244Swnj 	register struct ts_softc *sc = &ts_softc[tsunit];
7323244Swnj 
7333244Swnj 	if (bp == &ctsbuf[TSUNIT(bp->b_dev)]) {
7343244Swnj 		if (sc->sc_blkno > dbtofsb(bp->b_blkno)) {
7353244Swnj 			/* reversing */
7363244Swnj 			sc->sc_nxrec = dbtofsb(bp->b_blkno) - sc->sc_sts.s_rbpcr;
7373244Swnj 			sc->sc_blkno = sc->sc_nxrec;
7383244Swnj 		} else {
7393244Swnj 			/* spacing forward */
7403244Swnj 			sc->sc_blkno = dbtofsb(bp->b_blkno) + sc->sc_sts.s_rbpcr;
7413244Swnj 			sc->sc_nxrec = sc->sc_blkno - 1;
7421900Swnj 		}
7433244Swnj 		return;
7443244Swnj 	}
7453244Swnj 	/* eof on read */
7463244Swnj 	sc->sc_nxrec = dbtofsb(bp->b_blkno);
7471900Swnj }
7481900Swnj 
7491900Swnj tsread(dev)
7503244Swnj 	dev_t dev;
7511900Swnj {
7523244Swnj 
7531900Swnj 	tsphys(dev);
7543244Swnj 	if (u.u_error)
7553244Swnj 		return;
7563244Swnj 	physio(tsstrategy, &rtsbuf[TSUNIT(dev)], dev, B_READ, minphys);
7571900Swnj }
7581900Swnj 
7591900Swnj tswrite(dev)
7603244Swnj 	dev_t dev;
7611900Swnj {
7623244Swnj 
7631900Swnj 	tsphys(dev);
7643244Swnj 	if (u.u_error)
7653244Swnj 		return;
7663244Swnj 	physio(tsstrategy, &rtsbuf[TSUNIT(dev)], dev, B_WRITE, minphys);
7671900Swnj }
7681900Swnj 
7693244Swnj /*
7703244Swnj  * Check that a raw device exists.
7713244Swnj  * If it does, set up sc_blkno and sc_nxrec
7723244Swnj  * so that the tape will appear positioned correctly.
7733244Swnj  */
7741900Swnj tsphys(dev)
7753244Swnj 	dev_t dev;
7761900Swnj {
7773244Swnj 	register int tsunit = TSUNIT(dev);
7783244Swnj 	register daddr_t a;
7793244Swnj 	register struct ts_softc *sc;
7803244Swnj 	register struct uba_device *ui;
7811900Swnj 
7823244Swnj 	if (tsunit >= NTS || (ui=tsdinfo[tsunit]) == 0 || ui->ui_alive == 0) {
7833244Swnj 		u.u_error = ENXIO;
7843244Swnj 		return;
7853244Swnj 	}
7863244Swnj 	sc = &ts_softc[tsunit];
7873244Swnj 	a = dbtofsb(u.u_offset >> 9);
7883244Swnj 	sc->sc_blkno = a;
7893244Swnj 	sc->sc_nxrec = a + 1;
7901900Swnj }
7911918Swnj 
7923244Swnj tsreset(uban)
7933244Swnj 	int uban;
7941918Swnj {
7953244Swnj 	register struct uba_ctlr *um;
7963244Swnj 	register ts11;
7971918Swnj 
7983244Swnj 	for (ts11 = 0; ts11 < NTS; ts11++) {
7993244Swnj 		if ((um = tsminfo[ts11]) == 0 || um->um_alive == 0 ||
8003244Swnj 		   um->um_ubanum != uban)
8013244Swnj 			continue;
8023244Swnj 		printf(" ts%d", ts11);
8033244Swnj 		um->um_tab.b_active = 0;
8043244Swnj 		um->um_tab.b_actf = um->um_tab.b_actl = 0;
8053244Swnj 		ts_softc[ts11].sc_openf = -1;
8063244Swnj 		if (um->um_ubinfo) {
8073244Swnj 			printf("<%d>", (um->um_ubinfo>>28)&0xf);
8083244Swnj 			ubadone(um);
8093244Swnj 		}
8103989Sroot 		(void) tsinit(ts11);
8113244Swnj 		tsstart(um);
8121918Swnj 	}
8131918Swnj }
8141918Swnj 
8153244Swnj /*ARGSUSED*/
8163244Swnj tsioctl(dev, cmd, addr, flag)
8173244Swnj 	caddr_t addr;
8183244Swnj 	dev_t dev;
8191918Swnj {
8203244Swnj 	int tsunit = TSUNIT(dev);
8213244Swnj 	register struct ts_softc *sc = &ts_softc[tsunit];
8223244Swnj 	register struct buf *bp = &ctsbuf[TSUNIT(dev)];
8233244Swnj 	register callcount;
8243244Swnj 	int fcount;
8253244Swnj 	struct mtop mtop;
8263244Swnj 	struct mtget mtget;
8273244Swnj 	/* we depend of the values and order of the MT codes here */
8283244Swnj 	static tsops[] =
8293656Swnj 	 {TS_WEOF,TS_SFORWF,TS_SREVF,TS_SFORW,TS_SREV,TS_REW,TS_OFFL,TS_SENSE};
8301918Swnj 
8313244Swnj 	switch (cmd) {
8323244Swnj 	case MTIOCTOP:	/* tape operation */
8333244Swnj 		if (copyin((caddr_t)addr, (caddr_t)&mtop, sizeof(mtop))) {
8343244Swnj 			u.u_error = EFAULT;
8353244Swnj 			return;
8363244Swnj 		}
8373244Swnj 		switch(mtop.mt_op) {
8383244Swnj 		case MTWEOF:
8393244Swnj 			callcount = mtop.mt_count;
8403244Swnj 			fcount = 1;
8413244Swnj 			break;
8423244Swnj 		case MTFSF: case MTBSF:
8433244Swnj 		case MTFSR: case MTBSR:
8443244Swnj 			callcount = 1;
8453244Swnj 			fcount = mtop.mt_count;
8463244Swnj 			break;
8473244Swnj 		case MTREW: case MTOFFL: case MTNOP:
8483244Swnj 			callcount = 1;
8493244Swnj 			fcount = 1;
8503244Swnj 			break;
8513244Swnj 		default:
8523244Swnj 			u.u_error = ENXIO;
8533244Swnj 			return;
8543244Swnj 		}
8553244Swnj 		if (callcount <= 0 || fcount <= 0) {
8563244Swnj 			u.u_error = ENXIO;
8573244Swnj 			return;
8583244Swnj 		}
8593244Swnj 		while (--callcount >= 0) {
8603244Swnj 			tscommand(dev, tsops[mtop.mt_op], fcount);
8613244Swnj 			if ((mtop.mt_op == MTFSR || mtop.mt_op == MTBSR) &&
8623244Swnj 			    bp->b_resid) {
8633244Swnj 				u.u_error = EIO;
8643244Swnj 				break;
8653244Swnj 			}
8663244Swnj 			if ((bp->b_flags&B_ERROR) || sc->sc_sts.s_xs0&TS_BOT)
8673244Swnj 				break;
8683244Swnj 		}
8693244Swnj 		geterror(bp);
8703244Swnj 		return;
8713244Swnj 	case MTIOCGET:
8723244Swnj 		mtget.mt_dsreg = 0;
8733244Swnj 		mtget.mt_erreg = sc->sc_sts.s_xs0;
8743244Swnj 		mtget.mt_resid = sc->sc_resid;
8753480Stoy 		mtget.mt_type = MT_ISTS;
8763244Swnj 		if (copyout((caddr_t)&mtget, addr, sizeof(mtget)))
8773244Swnj 			u.u_error = EFAULT;
8783244Swnj 		return;
8793244Swnj 	default:
8803244Swnj 		u.u_error = ENXIO;
8813244Swnj 	}
8821918Swnj }
8831918Swnj 
8843244Swnj #define	DBSIZE	20
8853244Swnj 
8863244Swnj tsdump()
8871918Swnj {
8883244Swnj 	register struct uba_device *ui;
8893244Swnj 	register struct uba_regs *up;
8903244Swnj 	register struct device *addr;
8913244Swnj 	int blk, num;
8923244Swnj 	int start;
8931918Swnj 
8943244Swnj 	start = 0;
8953244Swnj 	num = maxfree;
8963244Swnj #define	phys(a,b)	((b)((int)(a)&0x7fffffff))
8973244Swnj 	if (tsdinfo[0] == 0)
8983244Swnj 		return (ENXIO);
8993244Swnj 	ui = phys(tsdinfo[0], struct uba_device *);
9003244Swnj 	up = phys(ui->ui_hd, struct uba_hd *)->uh_physuba;
9013327Swnj 	ubainit(up);
9023244Swnj 	DELAY(1000000);
9033244Swnj 	addr = (struct device *)ui->ui_physaddr;
9043244Swnj 	addr->tssr = 0;
9053244Swnj 	tswait(addr);
9063244Swnj 	while (num > 0) {
9073244Swnj 		blk = num > DBSIZE ? DBSIZE : num;
9083244Swnj 		tsdwrite(start, blk, addr, up);
9093244Swnj 		start += blk;
9103244Swnj 		num -= blk;
9113244Swnj 	}
9123244Swnj 	tseof(addr);
9133244Swnj 	tseof(addr);
9143244Swnj 	tswait(addr);
9153244Swnj 	if (addr->tssr&TS_SC)
9163244Swnj 		return (EIO);
9173244Swnj 	addr->tssr = 0;
9183244Swnj 	tswait(addr);
9193244Swnj 	return (0);
9201918Swnj }
9211918Swnj 
9223244Swnj tsdwrite(dbuf, num, addr, up)
9233244Swnj 	register dbuf, num;
9243244Swnj 	register struct device *addr;
9253244Swnj 	struct uba_regs *up;
9261918Swnj {
9273244Swnj 	register struct pte *io;
9283244Swnj 	register int npf;
9291918Swnj 
9303244Swnj 	tswait(addr);
9313244Swnj 	io = up->uba_map;
9323244Swnj 	npf = num+1;
9333244Swnj 	while (--npf != 0)
9343244Swnj 		 *(int *)io++ = (dbuf++ | (1<<UBAMR_DPSHIFT) | UBAMR_MRV);
9353244Swnj 	*(int *)io = 0;
9363244Swnj #ifdef notyet
9373244Swnj 	addr->tsbc = -(num*NBPG);
9383244Swnj 	addr->tsba = 0;
9393244Swnj 	addr->tscs = TS_WCOM | TM_GO;
9403244Swnj #endif
9411918Swnj }
9421918Swnj 
9433244Swnj tswait(addr)
9443244Swnj 	register struct device *addr;
9451918Swnj {
9463244Swnj 	register s;
9471918Swnj 
9483244Swnj 	do
9493244Swnj 		s = addr->tssr;
9503244Swnj 	while ((s & TS_SSR) == 0);
9511918Swnj }
9521918Swnj 
9533244Swnj tseof(addr)
9543244Swnj 	struct device *addr;
9551918Swnj {
9561918Swnj 
9573244Swnj 	tswait(addr);
9583244Swnj #ifdef notyet
9593244Swnj 	addr->tscs = TS_WEOF | TM_GO;
9603244Swnj #endif
9611918Swnj }
9621900Swnj #endif
963