xref: /csrg-svn/sys/vax/uba/ts.c (revision 3668)
1*3668Swnj /*	ts.c	4.14	81/05/08	*/
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  */
1213244Swnj tsprobe(reg)
1223244Swnj 	caddr_t reg;
1233244Swnj {
1243244Swnj 	register int br, cvec;		/* must be r11,r10; value-result */
1251900Swnj 
1263244Swnj #ifdef lint
1273244Swnj 	br = 0; cvec = br; br = cvec;
1283244Swnj #endif
1293244Swnj 	/****************/
1303244Swnj 	/*		*/
1313244Swnj 	/*  K L U D G E */
1323244Swnj 	/*		*/
1333244Swnj 	/****************/
1341900Swnj 
1353520Sroot #if NTM > 0
1363520Sroot 	if (havetm)
1373520Sroot 		return (0);
1383520Sroot #endif
1393244Swnj 	/* IT'S TOO HARD TO MAKE THIS THING INTERRUPT
1403244Swnj 	   JUST TO FIND ITS VECTOR */
1413244Swnj 	cvec = 0224;
1423244Swnj 	br = 0x15;
1433244Swnj }
1441900Swnj 
1453244Swnj /*
1463244Swnj  * TS11 only supports one drive per controller;
1473244Swnj  * check for ui_slave == 0.
1483244Swnj  *
1493244Swnj  * DO WE REALLY NEED THIS ROUTINE???
1503244Swnj  */
1513244Swnj /*ARGSUSED*/
1523244Swnj tsslave(ui, reg)
1533244Swnj 	struct uba_device *ui;
1543244Swnj 	caddr_t reg;
1553244Swnj {
1561900Swnj 
1573244Swnj 	if (ui->ui_slave)	/* non-zero slave not allowed */
1583244Swnj 		return(0);
1593244Swnj 	return (1);
1603244Swnj }
1611900Swnj 
1623244Swnj /*
1633244Swnj  * Record attachment of the unit to the controller.
1643244Swnj  *
1653244Swnj  * SHOULD THIS ROUTINE DO ANYTHING???
1663244Swnj  */
1673244Swnj /*ARGSUSED*/
1683244Swnj tsattach(ui)
1693244Swnj 	struct uba_device *ui;
1703244Swnj {
1711900Swnj 
1723244Swnj }
1731900Swnj 
1743244Swnj /*
1753244Swnj  * Open the device.  Tapes are unique open
1763244Swnj  * devices, so we refuse if it is already open.
1773244Swnj  * We also check that a tape is available, and
1783244Swnj  * don't block waiting here; if you want to wait
1793244Swnj  * for a tape you should timeout in user code.
1803244Swnj  */
1811900Swnj tsopen(dev, flag)
1823244Swnj 	dev_t dev;
1833244Swnj 	int flag;
1841900Swnj {
1853244Swnj 	register int tsunit;
1863244Swnj 	register struct uba_device *ui;
1873244Swnj 	register struct ts_softc *sc;
1881900Swnj 
1893244Swnj 	tsunit = TSUNIT(dev);
1903244Swnj 	if (tsunit>=NTS || (sc = &ts_softc[tsunit])->sc_openf ||
1913244Swnj 	    (ui = tsdinfo[tsunit]) == 0 || ui->ui_alive == 0) {
1921900Swnj 		u.u_error = ENXIO;
1931900Swnj 		return;
1941900Swnj 	}
1953244Swnj 	if (tsinit(tsunit)) {
1961900Swnj 		u.u_error = ENXIO;
1973647Swnj #ifdef TSDEBUG
1983327Swnj 		printd("init failed\n");
1993647Swnj #endif
2001900Swnj 		return;
2011900Swnj 	}
2023647Swnj #ifdef TSDEBUG
2033327Swnj 	printd("init ok\n");
2043647Swnj #endif
2053244Swnj 	tscommand(dev, TS_SENSE, 1);
2063647Swnj #ifdef TSDEBUG
2073327Swnj 	printd("sense xs0 %o\n", sc->sc_sts.s_xs0);
2083647Swnj #endif
2093244Swnj 	if ((sc->sc_sts.s_xs0&TS_ONL) == 0 || ((flag&(FREAD|FWRITE)) ==
2103244Swnj 	    FWRITE && (sc->sc_sts.s_xs0&TS_WLK))) {
2113244Swnj 		/*
2123244Swnj 		 * Not online or write locked.
2133244Swnj 		 */
2143244Swnj 		u.u_error = EIO;
2153244Swnj 		return;
2161900Swnj 	}
2173244Swnj 	sc->sc_openf = 1;
2183244Swnj 	sc->sc_blkno = (daddr_t)0;
2193244Swnj 	sc->sc_nxrec = INF;
2203244Swnj 	sc->sc_lastiow = 0;
2211900Swnj }
2221900Swnj 
2233244Swnj /*
2243244Swnj  * Close tape device.
2253244Swnj  *
2263244Swnj  * If tape was open for writing or last operation was
2273244Swnj  * a write, then write two EOF's and backspace over the last one.
2283244Swnj  * Unless this is a non-rewinding special file, rewind the tape.
2293244Swnj  * Make the tape available to others.
2303244Swnj  */
2311900Swnj tsclose(dev, flag)
2323244Swnj 	register dev_t dev;
2333244Swnj 	register flag;
2341900Swnj {
2353244Swnj 	register struct ts_softc *sc = &ts_softc[TSUNIT(dev)];
2361900Swnj 
2373244Swnj 	if (flag == FWRITE || (flag&FWRITE) && sc->sc_lastiow) {
2383244Swnj 		tscommand(dev, TS_WEOF, 1);
2393244Swnj 		tscommand(dev, TS_WEOF, 1);
2403244Swnj 		tscommand(dev, TS_SREV, 1);
2411900Swnj 	}
2423244Swnj 	if ((minor(dev)&T_NOREWIND) == 0)
2433244Swnj 		/*
2443244Swnj 		 * 0 count means don't hang waiting for rewind complete
2453244Swnj 		 * rather ctsbuf stays busy until the operation completes
2463244Swnj 		 * preventing further opens from completing by
2473244Swnj 		 * preventing a TS_SENSE from completing.
2483244Swnj 		 */
2493244Swnj 		tscommand(dev, TS_REW, 0);
2503244Swnj 	sc->sc_openf = 0;
2511900Swnj }
2521900Swnj 
2533244Swnj /*
2543244Swnj  * Initialize the TS11.  Set up Unibus mapping for command
2553244Swnj  * packets and set device characteristics.
2563244Swnj  */
2573244Swnj tsinit(unit)
2583244Swnj 	register int unit;
2591900Swnj {
2603244Swnj 	register struct ts_softc *sc = &ts_softc[unit];
2613244Swnj 	register struct uba_ctlr *um = tsminfo[unit];
2623244Swnj 	register struct device *addr = (struct device *)um->um_addr;
2633244Swnj 	register int i;
2643244Swnj 
2653244Swnj 	/*
2663244Swnj 	 * Map the command and message packets into Unibus
2673244Swnj 	 * address space.  We do all the command and message
2683244Swnj 	 * packets at once to minimize the amount of Unibus
2693244Swnj 	 * mapping necessary.
2703244Swnj 	 */
2713244Swnj 	if (ts_ubaddr == 0) {
2723244Swnj 		ctsbuf[unit].b_un.b_addr = (caddr_t)ts_softc;
2733244Swnj 		ctsbuf[unit].b_bcount = sizeof(ts_softc);
2743244Swnj 		i = ubasetup(um->um_ubanum, &ctsbuf[unit], 0);
2753244Swnj 		i &= 0777777;
2763244Swnj 		ts_ubaddr = (struct ts_softc *)i;
2773244Swnj 		/* MAKE SURE WE DON'T GET UNIBUS ADDRESS ZERO */
2783244Swnj 		if (ts_ubaddr == 0)
2793244Swnj 			printf("ts%d: zero ubaddr\n", unit);
2803244Swnj 	}
2813244Swnj 	/*
2823244Swnj 	 * Now initialize the TS11 controller.
2833244Swnj 	 * Set the characteristics.
2843244Swnj 	 */
285*3668Swnj 	if (addr->tssr & (TS_NBA|TS_OFL)) {
2863244Swnj 		addr->tssr = 0;		/* subsystem initialize */
2873244Swnj 		tswait(addr);
2883244Swnj 		i = (int)&ts_ubaddr[unit].sc_cmd;	/* Unibus addr of cmd */
2893327Swnj 		if (i&3) {
2903327Swnj 			printf("addr mod 4 != 0\n");
2913327Swnj 			return(1);
2923327Swnj 		}
2933244Swnj 		sc->sc_uba = (u_short)(i + ((i>>16)&3));
2943244Swnj 		sc->sc_char.char_addr = (int)&ts_ubaddr[unit].sc_sts;
2953244Swnj 		sc->sc_char.char_size = sizeof(struct ts_sts);
2963244Swnj 		sc->sc_char.char_mode = TS_ESS;
2973244Swnj 		sc->sc_cmd.c_cmd = TS_ACK | TS_SETCHR;
2983327Swnj 		i = (int)&ts_ubaddr[unit].sc_char;
2993327Swnj 		sc->sc_cmd.c_loba = i;
3003327Swnj 		sc->sc_cmd.c_hiba = (i>>16)&3;
3013244Swnj 		sc->sc_cmd.c_size = sizeof(struct ts_char);
3023244Swnj 		addr->tsdb = sc->sc_uba;
3033244Swnj 		tswait(addr);
3043327Swnj /*
3053327Swnj 		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);
3063327Swnj */
3073327Swnj 		if (addr->tssr & TS_NBA)
3083327Swnj 			return(1);
3093244Swnj 	}
3103244Swnj 	return(0);
3113244Swnj }
3123244Swnj 
3133244Swnj /*
3143244Swnj  * Execute a command on the tape drive
3153244Swnj  * a specified number of times.
3163244Swnj  */
3173244Swnj tscommand(dev, com, count)
3183244Swnj 	dev_t dev;
3193244Swnj 	int com, count;
3203244Swnj {
3211900Swnj 	register struct buf *bp;
3221900Swnj 
3233244Swnj 	bp = &ctsbuf[TSUNIT(dev)];
3243244Swnj 	(void) spl5();
3253244Swnj 	while (bp->b_flags&B_BUSY) {
3263244Swnj 		/*
3273244Swnj 		 * This special check is because B_BUSY never
3283244Swnj 		 * gets cleared in the non-waiting rewind case.
3293244Swnj 		 */
3303244Swnj 		if (bp->b_repcnt == 0 && (bp->b_flags&B_DONE))
3313244Swnj 			break;
3321900Swnj 		bp->b_flags |= B_WANTED;
3331900Swnj 		sleep((caddr_t)bp, PRIBIO);
3341900Swnj 	}
3353244Swnj 	bp->b_flags = B_BUSY|B_READ;
3363244Swnj 	(void) spl0();
3373647Swnj #ifdef TSDEBUG
3383327Swnj 	printd("command %o dev %x count %d\n", com, dev, count);
3393647Swnj #endif
3403244Swnj 	bp->b_dev = dev;
3413244Swnj 	bp->b_repcnt = count;
3423244Swnj 	bp->b_command = com;
3431900Swnj 	bp->b_blkno = 0;
3441900Swnj 	tsstrategy(bp);
3453244Swnj 	/*
3463244Swnj 	 * In case of rewind from close, don't wait.
3473244Swnj 	 * This is the only case where count can be 0.
3483244Swnj 	 */
3493244Swnj 	if (count == 0)
3503244Swnj 		return;
3511900Swnj 	iowait(bp);
3523244Swnj 	if (bp->b_flags&B_WANTED)
3531900Swnj 		wakeup((caddr_t)bp);
3543244Swnj 	bp->b_flags &= B_ERROR;
3551900Swnj }
3561900Swnj 
3573244Swnj /*
3583244Swnj  * Queue a tape operation.
3593244Swnj  */
3601900Swnj tsstrategy(bp)
3613244Swnj 	register struct buf *bp;
3621900Swnj {
3633244Swnj 	int tsunit = TSUNIT(bp->b_dev);
3643244Swnj 	register struct uba_ctlr *um;
3653327Swnj 	register struct buf *dp;
3661900Swnj 
3673244Swnj 	/*
3683244Swnj 	 * Put transfer at end of controller queue
3693244Swnj 	 */
3701900Swnj 	bp->av_forw = NULL;
3713244Swnj 	um = tsdinfo[tsunit]->ui_mi;
3723327Swnj 	dp = &tsbuf[tsunit];
3733244Swnj 	(void) spl5();
3743327Swnj 	if (dp->b_actf == NULL)
3753327Swnj 		dp->b_actf = bp;
3761900Swnj 	else
3773327Swnj 		dp->b_actl->av_forw = bp;
3783327Swnj 	dp->b_actl = bp;
3793327Swnj 	um->um_tab.b_actf = um->um_tab.b_actl = dp;
3803244Swnj 	/*
3813244Swnj 	 * If the controller is not busy, get
3823244Swnj 	 * it going.
3833244Swnj 	 */
3843244Swnj 	if (um->um_tab.b_active == 0)
3853244Swnj 		tsstart(um);
3863244Swnj 	(void) spl0();
3871900Swnj }
3881900Swnj 
3893244Swnj /*
3903244Swnj  * Start activity on a ts controller.
3913244Swnj  */
3923244Swnj tsstart(um)
3933244Swnj 	register struct uba_ctlr *um;
3941900Swnj {
3951900Swnj 	register struct buf *bp;
3963244Swnj 	register struct device *addr = (struct device *)um->um_addr;
3973244Swnj 	register struct ts_softc *sc;
3983244Swnj 	register struct ts_cmd *tc;
3993244Swnj 	register struct uba_device *ui;
4003244Swnj 	int tsunit, cmd;
4011900Swnj 	daddr_t blkno;
4021900Swnj 
4033244Swnj 	/*
4043244Swnj 	 * Start the controller if there is something for it to do.
4053244Swnj 	 */
4063244Swnj loop:
4073327Swnj 	if ((bp = um->um_tab.b_actf->b_actf) == NULL)
4081900Swnj 		return;
4093244Swnj 	tsunit = TSUNIT(bp->b_dev);
4103244Swnj 	ui = tsdinfo[tsunit];
4113244Swnj 	sc = &ts_softc[tsunit];
4123244Swnj 	tc = &sc->sc_cmd;
4133244Swnj 	/*
4143244Swnj 	 * Default is that last command was NOT a write command;
4153244Swnj 	 * if we do a write command we will notice this in tsintr().
4163244Swnj 	 */
4173656Swnj 	sc->sc_lastiow = 0;
4183244Swnj 	if (sc->sc_openf < 0 || (addr->tssr&TS_OFL)) {
4193244Swnj 		/*
4203244Swnj 		 * Have had a hard error on a non-raw tape
4213244Swnj 		 * or the tape unit is now unavailable
4223244Swnj 		 * (e.g. taken off line).
4233244Swnj 		 */
4243244Swnj 		bp->b_flags |= B_ERROR;
4253244Swnj 		goto next;
4263244Swnj 	}
4273244Swnj 	if (bp == &ctsbuf[TSUNIT(bp->b_dev)]) {
4283244Swnj 		/*
4293244Swnj 		 * Execute control operation with the specified count.
4303244Swnj 		 */
4313244Swnj 		um->um_tab.b_active =
4323244Swnj 		    bp->b_command == TS_REW ? SREW : SCOM;
4333244Swnj 		tc->c_repcnt = bp->b_repcnt;
4343647Swnj #ifdef TSDEBUG
4353327Swnj 		printd("strat: do cmd\n");
4363647Swnj #endif
4373244Swnj 		goto dobpcmd;
4383244Swnj 	}
4393244Swnj 	/*
4403244Swnj 	 * The following checks handle boundary cases for operation
4413244Swnj 	 * on non-raw tapes.  On raw tapes the initialization of
4423244Swnj 	 * sc->sc_nxrec by tsphys causes them to be skipped normally
4433244Swnj 	 * (except in the case of retries).
4443244Swnj 	 */
4453244Swnj 	if (dbtofsb(bp->b_blkno) > sc->sc_nxrec) {
4463244Swnj 		/*
4473244Swnj 		 * Can't read past known end-of-file.
4483244Swnj 		 */
4493244Swnj 		bp->b_flags |= B_ERROR;
4503244Swnj 		bp->b_error = ENXIO;
4513244Swnj 		goto next;
4523244Swnj 	}
4533244Swnj 	if (dbtofsb(bp->b_blkno) == sc->sc_nxrec &&
4543244Swnj 	    bp->b_flags&B_READ) {
4553244Swnj 		/*
4563244Swnj 		 * Reading at end of file returns 0 bytes.
4573244Swnj 		 */
4583244Swnj 		bp->b_resid = bp->b_bcount;
4593244Swnj 		clrbuf(bp);
4603244Swnj 		goto next;
4613244Swnj 	}
4623244Swnj 	if ((bp->b_flags&B_READ) == 0)
4633244Swnj 		/*
4643244Swnj 		 * Writing sets EOF
4653244Swnj 		 */
4663244Swnj 		sc->sc_nxrec = dbtofsb(bp->b_blkno) + 1;
4673244Swnj 	/*
4683244Swnj 	 * If the data transfer command is in the correct place,
4693244Swnj 	 * set up all the registers except the csr, and give
4703244Swnj 	 * control over to the UNIBUS adapter routines, to
4713244Swnj 	 * wait for resources to start the i/o.
4723244Swnj 	 */
4733244Swnj 	if ((blkno = sc->sc_blkno) == dbtofsb(bp->b_blkno)) {
4743244Swnj 		tc->c_size = bp->b_bcount;
4753244Swnj 		if ((bp->b_flags&B_READ) == 0)
4763244Swnj 			cmd = TS_WCOM;
4771900Swnj 		else
4783244Swnj 			cmd = TS_RCOM;
4793244Swnj 		if (um->um_tab.b_errcnt)
4803244Swnj 			cmd |= TS_RETRY;
4813244Swnj 		um->um_tab.b_active = SIO;
4823327Swnj 		tc->c_cmd = TS_ACK | TS_CVC | TS_IE | cmd;
4833647Swnj #ifdef TSDEBUG
4843327Swnj 		printd("r/w %o size %d\n", tc->c_cmd, tc->c_size);
4853647Swnj #endif
4863244Swnj 		(void) ubago(ui);
4873244Swnj 		return;
4883244Swnj 	}
4893244Swnj 	/*
4903244Swnj 	 * Tape positioned incorrectly;
4913244Swnj 	 * set to seek forwards or backwards to the correct spot.
4923244Swnj 	 * This happens for raw tapes only on error retries.
4933244Swnj 	 */
4943244Swnj 	um->um_tab.b_active = SSEEK;
4953647Swnj #ifdef TSDEBUG
4963327Swnj 	printd("seek blkno %d b_blkno %d\n", blkno, bp->b_blkno);
4973647Swnj #endif
4983244Swnj 	if (blkno < dbtofsb(bp->b_blkno)) {
4993244Swnj 		bp->b_command = TS_SFORW;
5003244Swnj 		tc->c_repcnt = dbtofsb(bp->b_blkno) - blkno;
5011900Swnj 	} else {
5023244Swnj 		bp->b_command = TS_SREV;
5033244Swnj 		tc->c_repcnt = blkno - dbtofsb(bp->b_blkno);
5041900Swnj 	}
5053244Swnj dobpcmd:
5063244Swnj 	/*
5073244Swnj 	 * Do the command in bp.
5083244Swnj 	 */
5093327Swnj 	tc->c_cmd = TS_ACK | TS_CVC | TS_IE | bp->b_command;
5103244Swnj 	addr->tsdb = sc->sc_uba;
5111900Swnj 	return;
5121900Swnj 
5133244Swnj next:
5143244Swnj 	/*
5153244Swnj 	 * Done with this operation due to error or
5163244Swnj 	 * the fact that it doesn't do anything.
5173244Swnj 	 * Release UBA resources (if any), dequeue
5183244Swnj 	 * the transfer and continue processing this slave.
5193244Swnj 	 */
5203244Swnj 	if (um->um_ubinfo)
5213244Swnj 		ubadone(um);
5223244Swnj 	um->um_tab.b_errcnt = 0;
5233327Swnj 	um->um_tab.b_actf->b_actf = bp->av_forw;
5241900Swnj 	iodone(bp);
5251900Swnj 	goto loop;
5261900Swnj }
5271900Swnj 
5283244Swnj /*
5293244Swnj  * The UNIBUS resources we needed have been
5303244Swnj  * allocated to us; start the device.
5313244Swnj  */
5323244Swnj tsdgo(um)
5333244Swnj 	register struct uba_ctlr *um;
5341900Swnj {
5353244Swnj 	register struct device *addr = (struct device *)um->um_addr;
5363244Swnj 	register struct ts_softc *sc = &ts_softc[um->um_ctlr];
5373327Swnj 	register int i;
5383244Swnj 
5393327Swnj 	i = um->um_ubinfo & 0777777;
5403647Swnj #ifdef TSDEBUG
5413327Swnj 	printd("dgo addr %o\n", i);
5423647Swnj #endif
5433327Swnj 	sc->sc_cmd.c_loba = i;
5443327Swnj 	sc->sc_cmd.c_hiba = (i>>16)&3;
5453244Swnj 	addr->tsdb = sc->sc_uba;
5463244Swnj }
5473244Swnj 
5483244Swnj /*
5493244Swnj  * Ts interrupt routine.
5503244Swnj  */
5513244Swnj /*ARGSUSED*/
5523244Swnj tsintr(ts11)
5533244Swnj 	int ts11;
5543244Swnj {
5551900Swnj 	register struct buf *bp;
5563244Swnj 	register struct uba_ctlr *um = tsminfo[ts11];
5573244Swnj 	register struct device *addr;
5583244Swnj 	register struct ts_softc *sc;
5593244Swnj 	int tsunit;
5603244Swnj 	register state;
5611900Swnj 
5623647Swnj #ifdef TSDEBUG
5633327Swnj 	printd("intr\n");
5643647Swnj #endif
5653327Swnj 	if ((bp = um->um_tab.b_actf->b_actf) == NULL)
5661900Swnj 		return;
5673244Swnj 	tsunit = TSUNIT(bp->b_dev);
5683244Swnj 	addr = (struct device *)tsdinfo[tsunit]->ui_addr;
5693244Swnj 	/*
5703244Swnj 	 * If last command was a rewind, and tape is still
5713244Swnj 	 * rewinding, wait for the rewind complete interrupt.
5723244Swnj 	 *
5733244Swnj 	 * SHOULD NEVER GET AN INTERRUPT IN THIS STATE.
5743244Swnj 	 */
5753244Swnj 	if (um->um_tab.b_active == SREW) {
5763244Swnj 		um->um_tab.b_active = SCOM;
5773244Swnj 		if ((addr->tssr&TS_SSR) == 0)
5783244Swnj 			return;
5793244Swnj 	}
5803244Swnj 	/*
5813244Swnj 	 * An operation completed... record status
5823244Swnj 	 */
5833647Swnj #ifdef TSDEBUG
5843327Swnj 	printd("  ok1\n");
5853647Swnj #endif
5863244Swnj 	sc = &ts_softc[tsunit];
5873244Swnj 	if ((bp->b_flags & B_READ) == 0)
5883244Swnj 		sc->sc_lastiow = 1;
5893244Swnj 	state = um->um_tab.b_active;
5903244Swnj 	um->um_tab.b_active = 0;
5913244Swnj 	/*
5923244Swnj 	 * Check for errors.
5933244Swnj 	 */
5943244Swnj 	if (addr->tssr&TS_SC) {
5953244Swnj 		switch (addr->tssr & TS_TC) {
5963244Swnj 		case TS_UNREC:		/* unrecoverable */
5973244Swnj 		case TS_FATAL:		/* fatal error */
5983244Swnj 		case TS_ATTN:		/* attention (shouldn't happen) */
5993244Swnj 		case TS_RECNM:		/* recoverable, no motion */
6003244Swnj 			break;
6011900Swnj 
6023244Swnj 		case TS_SUCC:		/* success termination */
6033244Swnj 			printf("ts%d: success\n", TSUNIT(minor(bp->b_dev)));
6043244Swnj 			goto ignoreerr;
6051900Swnj 
6063244Swnj 		case TS_ALERT:		/* tape status alert */
6073244Swnj 			/*
6083244Swnj 			 * If we hit the end of the tape file,
6093244Swnj 			 * update our position.
6103244Swnj 			 */
6113244Swnj 			if (sc->sc_sts.s_xs0 & (TS_TMK|TS_EOT)) {
6123244Swnj 				tsseteof(bp);		/* set blkno and nxrec */
6133244Swnj 				state = SCOM;		/* force completion */
6143244Swnj 				/*
6153244Swnj 				 * Stuff bc so it will be unstuffed correctly
6163244Swnj 				 * later to get resid.
6173244Swnj 				 */
6183244Swnj 				sc->sc_sts.s_rbpcr = bp->b_bcount;
6193244Swnj 				goto opdone;
6203244Swnj 			}
6213244Swnj 			/*
6223244Swnj 			 * If we were reading raw tape and the record was too long
6233244Swnj 			 * or too short, then we don't consider this an error.
6243244Swnj 			 */
6253244Swnj 			if (bp == &rtsbuf[TSUNIT(bp->b_dev)] && (bp->b_flags&B_READ) &&
6263244Swnj 			    sc->sc_sts.s_xs0&(TS_RLS|TS_RLL))
6273244Swnj 				goto ignoreerr;
6283244Swnj 		case TS_RECOV:		/* recoverable, tape moved */
6293244Swnj 			/*
6303244Swnj 			 * If this was an i/o operation retry up to 8 times.
6313244Swnj 			 */
6323244Swnj 			if (state==SIO) {
6333244Swnj 				if (++um->um_tab.b_errcnt < 7) {
6343244Swnj 					ubadone(um);
6353244Swnj 					goto opcont;
6363244Swnj 				} else
6373244Swnj 					sc->sc_blkno++;
6383244Swnj 			} else {
6393244Swnj 				/*
6403244Swnj 				 * Non-i/o errors on non-raw tape
6413244Swnj 				 * cause it to close.
6423244Swnj 				 */
6433244Swnj 				if (sc->sc_openf>0 && bp != &rtsbuf[TSUNIT(bp->b_dev)])
6443244Swnj 					sc->sc_openf = -1;
6453244Swnj 			}
6461900Swnj 			break;
6473244Swnj 
6483244Swnj 		case TS_REJECT:		/* function reject */
6493244Swnj 			if (state == SIO && sc->sc_sts.s_xs0 & TS_WLE)
6503244Swnj 				printf("ts%d: write locked\n", TSUNIT(bp->b_dev));
6513244Swnj 			if ((sc->sc_sts.s_xs0 & TS_ONL) == 0)
6523244Swnj 				printf("ts%d: offline\n", TSUNIT(bp->b_dev));
6531900Swnj 			break;
6541900Swnj 		}
6553244Swnj 		/*
6563244Swnj 		 * Couldn't recover error
6573244Swnj 		 */
6583647Swnj 		printf("ts%d: hard error bn%d xs0=%b", TSUNIT(bp->b_dev),
6593244Swnj 		    bp->b_blkno, sc->sc_sts.s_xs0, TSXS0_BITS);
6603647Swnj 		if (sc->sc_sts.s_xs1)
6613647Swnj 			printf(" xs1=%b", sc->sc_sts.s_xs1, TSXS1_BITS);
6623647Swnj 		if (sc->sc_sts.s_xs2)
6633647Swnj 			printf(" xs2=%b", sc->sc_sts.s_xs2, TSXS2_BITS);
6643647Swnj 		if (sc->sc_sts.s_xs3)
6653647Swnj 			printf(" xs3=%b", sc->sc_sts.s_xs3, TSXS3_BITS);
6663647Swnj 		printf("\n");
6673244Swnj 		bp->b_flags |= B_ERROR;
6683244Swnj 		goto opdone;
6693244Swnj 	}
6703244Swnj 	/*
6713244Swnj 	 * Advance tape control FSM.
6723244Swnj 	 */
6733244Swnj ignoreerr:
6743244Swnj 	switch (state) {
6751900Swnj 
6763244Swnj 	case SIO:
6773244Swnj 		/*
6783244Swnj 		 * Read/write increments tape block number
6793244Swnj 		 */
6803244Swnj 		sc->sc_blkno++;
6813244Swnj 		goto opdone;
6821900Swnj 
6831900Swnj 	case SCOM:
6843244Swnj 		/*
6853244Swnj 		 * For forward/backward space record update current position.
6863244Swnj 		 */
6873244Swnj 		if (bp == &ctsbuf[TSUNIT(bp->b_dev)])
6883244Swnj 		switch (bp->b_command) {
6891900Swnj 
6903244Swnj 		case TS_SFORW:
6913244Swnj 			sc->sc_blkno += bp->b_repcnt;
6923244Swnj 			break;
6931900Swnj 
6943244Swnj 		case TS_SREV:
6953244Swnj 			sc->sc_blkno -= bp->b_repcnt;
6963244Swnj 			break;
6973244Swnj 		}
6983244Swnj 		goto opdone;
6993244Swnj 
7003244Swnj 	case SSEEK:
7013244Swnj 		sc->sc_blkno = dbtofsb(bp->b_blkno);
7023244Swnj 		goto opcont;
7033244Swnj 
7041900Swnj 	default:
7053244Swnj 		panic("tsintr");
7061900Swnj 	}
7073244Swnj opdone:
7083244Swnj 	/*
7093244Swnj 	 * Reset error count and remove
7103244Swnj 	 * from device queue.
7113244Swnj 	 */
7123244Swnj 	um->um_tab.b_errcnt = 0;
7133327Swnj 	um->um_tab.b_actf->b_actf = bp->av_forw;
7143244Swnj 	bp->b_resid = sc->sc_sts.s_rbpcr;
7153244Swnj 	ubadone(um);
7163647Swnj #ifdef TSDEBUG
7173327Swnj 	printd("  iodone\n");
7183647Swnj #endif
7193244Swnj 	iodone(bp);
7203327Swnj 	if (um->um_tab.b_actf->b_actf == 0)
7213244Swnj 		return;
7223244Swnj opcont:
7233244Swnj 	tsstart(um);
7243244Swnj }
7253244Swnj 
7263244Swnj tsseteof(bp)
7273244Swnj 	register struct buf *bp;
7283244Swnj {
7293244Swnj 	register int tsunit = TSUNIT(bp->b_dev);
7303244Swnj 	register struct ts_softc *sc = &ts_softc[tsunit];
7313244Swnj 
7323244Swnj 	if (bp == &ctsbuf[TSUNIT(bp->b_dev)]) {
7333244Swnj 		if (sc->sc_blkno > dbtofsb(bp->b_blkno)) {
7343244Swnj 			/* reversing */
7353244Swnj 			sc->sc_nxrec = dbtofsb(bp->b_blkno) - sc->sc_sts.s_rbpcr;
7363244Swnj 			sc->sc_blkno = sc->sc_nxrec;
7373244Swnj 		} else {
7383244Swnj 			/* spacing forward */
7393244Swnj 			sc->sc_blkno = dbtofsb(bp->b_blkno) + sc->sc_sts.s_rbpcr;
7403244Swnj 			sc->sc_nxrec = sc->sc_blkno - 1;
7411900Swnj 		}
7423244Swnj 		return;
7433244Swnj 	}
7443244Swnj 	/* eof on read */
7453244Swnj 	sc->sc_nxrec = dbtofsb(bp->b_blkno);
7461900Swnj }
7471900Swnj 
7481900Swnj tsread(dev)
7493244Swnj 	dev_t dev;
7501900Swnj {
7513244Swnj 
7521900Swnj 	tsphys(dev);
7533244Swnj 	if (u.u_error)
7543244Swnj 		return;
7553244Swnj 	physio(tsstrategy, &rtsbuf[TSUNIT(dev)], dev, B_READ, minphys);
7561900Swnj }
7571900Swnj 
7581900Swnj tswrite(dev)
7593244Swnj 	dev_t dev;
7601900Swnj {
7613244Swnj 
7621900Swnj 	tsphys(dev);
7633244Swnj 	if (u.u_error)
7643244Swnj 		return;
7653244Swnj 	physio(tsstrategy, &rtsbuf[TSUNIT(dev)], dev, B_WRITE, minphys);
7661900Swnj }
7671900Swnj 
7683244Swnj /*
7693244Swnj  * Check that a raw device exists.
7703244Swnj  * If it does, set up sc_blkno and sc_nxrec
7713244Swnj  * so that the tape will appear positioned correctly.
7723244Swnj  */
7731900Swnj tsphys(dev)
7743244Swnj 	dev_t dev;
7751900Swnj {
7763244Swnj 	register int tsunit = TSUNIT(dev);
7773244Swnj 	register daddr_t a;
7783244Swnj 	register struct ts_softc *sc;
7793244Swnj 	register struct uba_device *ui;
7801900Swnj 
7813244Swnj 	if (tsunit >= NTS || (ui=tsdinfo[tsunit]) == 0 || ui->ui_alive == 0) {
7823244Swnj 		u.u_error = ENXIO;
7833244Swnj 		return;
7843244Swnj 	}
7853244Swnj 	sc = &ts_softc[tsunit];
7863244Swnj 	a = dbtofsb(u.u_offset >> 9);
7873244Swnj 	sc->sc_blkno = a;
7883244Swnj 	sc->sc_nxrec = a + 1;
7891900Swnj }
7901918Swnj 
7913244Swnj tsreset(uban)
7923244Swnj 	int uban;
7931918Swnj {
7943244Swnj 	register struct uba_ctlr *um;
7953244Swnj 	register ts11;
7963244Swnj 	register struct buf *dp;
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 		}
8103244Swnj 		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