xref: /csrg-svn/sys/vax/uba/ts.c (revision 3480)
1*3480Stoy /*	ts.c	4.9	81/04/03	*/
21900Swnj 
31941Swnj #include "ts.h"
41900Swnj #if NTS > 0
53327Swnj #define printd if(tsdebug)printf
63327Swnj int tsdebug;
71900Swnj /*
81900Swnj  * TS11 tape driver
93244Swnj  *
103244Swnj  * TODO:
113244Swnj  *	test driver with more than one controller
123244Swnj  *	test reset code
133244Swnj  *	test dump code
143244Swnj  *	test rewinds without hanging in driver
153244Swnj  *	what happens if you offline tape during rewind?
163244Swnj  *	test using file system on tape
171900Swnj  */
181900Swnj #include "../h/param.h"
191900Swnj #include "../h/systm.h"
201900Swnj #include "../h/buf.h"
213244Swnj #include "../h/dir.h"
221900Swnj #include "../h/conf.h"
233244Swnj #include "../h/user.h"
241900Swnj #include "../h/file.h"
253244Swnj #include "../h/map.h"
261900Swnj #include "../h/pte.h"
271947Swnj #include "../h/vm.h"
283244Swnj #include "../h/ubareg.h"
293244Swnj #include "../h/ubavar.h"
303244Swnj #include "../h/mtio.h"
313244Swnj #include "../h/ioctl.h"
323244Swnj #include "../h/cmap.h"
333244Swnj #include "../h/cpu.h"
341900Swnj 
353244Swnj #include "../h/tsreg.h"
361900Swnj 
373244Swnj /*
383244Swnj  * There is a ctsbuf per tape controller.
393244Swnj  * It is used as the token to pass to the internal routines
403244Swnj  * to execute tape ioctls.
413244Swnj  * In particular, when the tape is rewinding on close we release
423244Swnj  * the user process but any further attempts to use the tape drive
433244Swnj  * before the rewind completes will hang waiting for ctsbuf.
443244Swnj  */
453244Swnj struct	buf	ctsbuf[NTS];
461900Swnj 
473244Swnj /*
483244Swnj  * Raw tape operations use rtsbuf.  The driver
493244Swnj  * notices when rtsbuf is being used and allows the user
503244Swnj  * program to continue after errors and read records
513244Swnj  * not of the standard length (BSIZE).
523244Swnj  */
533244Swnj struct	buf	rtsbuf[NTS];
541900Swnj 
553244Swnj /*
563244Swnj  * Driver unibus interface routines and variables.
573244Swnj  */
583244Swnj int	tsprobe(), tsslave(), tsattach(), tsdgo(), tsintr();
593244Swnj struct	uba_ctlr *tsminfo[NTS];
603244Swnj struct	uba_device *tsdinfo[NTS];
613327Swnj struct buf	tsbuf[NTS];
623244Swnj u_short	tsstd[] = { 0772520, 0 };
633244Swnj /*** PROBABLY DON'T NEED ALL THESE SINCE CONTROLLER == DRIVE ***/
643244Swnj struct	uba_driver zsdriver =
653327Swnj  { tsprobe, tsslave, tsattach, tsdgo, tsstd, "ts", tsdinfo, "zs", tsminfo, 0 };
661900Swnj 
673244Swnj /* bits in minor device */
683244Swnj #define	TSUNIT(dev)	(minor(dev)&03)
693244Swnj #define	T_NOREWIND	04
701900Swnj 
713244Swnj #define	INF	(daddr_t)1000000L
721900Swnj 
733244Swnj /*
743244Swnj  * Software state per tape transport.
753244Swnj  * Also contains hardware state in message packets.
763244Swnj  *
773244Swnj  * 1. A tape drive is a unique-open device; we refuse opens when it is already.
783244Swnj  * 2. We keep track of the current position on a block tape and seek
793244Swnj  *    before operations by forward/back spacing if necessary.
803244Swnj  * 3. We remember if the last operation was a write on a tape, so if a tape
813244Swnj  *    is open read write and the last thing done is a write we can
823244Swnj  *    write a standard end of tape mark (two eofs).
833244Swnj  * 4. We remember the status registers after the last command, using
843244Swnj  *    then internally and returning them to the SENSE ioctl.
853244Swnj  */
863244Swnj struct	ts_softc {
873244Swnj 	char	sc_openf;	/* lock against multiple opens */
883244Swnj 	char	sc_lastiow;	/* last op was a write */
893327Swnj 	short	sc_resid;	/* copy of last bc */
903244Swnj 	daddr_t	sc_blkno;	/* block number, for block device tape */
913244Swnj 	daddr_t	sc_nxrec;	/* position of end of tape, if known */
923244Swnj 	struct ts_cmd sc_cmd;	/* the command packet - ADDR MUST BE 0 MOD 4 */
933244Swnj 	struct ts_sts sc_sts;	/* status packet, for returned status */
943244Swnj 	struct ts_char sc_char;	/* characteristics packet */
953244Swnj 	u_short	sc_uba;		/* Unibus addr of cmd pkt for tsdb */
963244Swnj } ts_softc[NTS];
971900Swnj 
983244Swnj struct ts_softc *ts_ubaddr;	/* Unibus address of ts_softc */
991900Swnj 
1003244Swnj /*
1013244Swnj  * States for um->um_tab.b_active, the per controller state flag.
1023244Swnj  * This is used to sequence control in the driver.
1033244Swnj  */
1043244Swnj #define	SSEEK	1		/* seeking */
1053244Swnj #define	SIO	2		/* doing seq i/o */
1063244Swnj #define	SCOM	3		/* sending control command */
1073244Swnj #define	SREW	4		/* sending a drive rewind */
1081900Swnj 
1093244Swnj /*
1103244Swnj  * Determine if there is a controller for
1113244Swnj  * a ts at address reg.  Our goal is to make the
1123244Swnj  * device interrupt.
1133244Swnj  */
1143244Swnj tsprobe(reg)
1153244Swnj 	caddr_t reg;
1163244Swnj {
1173244Swnj 	register int br, cvec;		/* must be r11,r10; value-result */
1181900Swnj 
1193244Swnj #ifdef lint
1203244Swnj 	br = 0; cvec = br; br = cvec;
1213244Swnj #endif
1223244Swnj 	/****************/
1233244Swnj 	/*		*/
1243244Swnj 	/*  K L U D G E */
1253244Swnj 	/*		*/
1263244Swnj 	/****************/
1271900Swnj 
1283244Swnj 	/* IT'S TOO HARD TO MAKE THIS THING INTERRUPT
1293244Swnj 	   JUST TO FIND ITS VECTOR */
1303244Swnj 	cvec = 0224;
1313244Swnj 	br = 0x15;
1323244Swnj }
1331900Swnj 
1343244Swnj /*
1353244Swnj  * TS11 only supports one drive per controller;
1363244Swnj  * check for ui_slave == 0.
1373244Swnj  *
1383244Swnj  * DO WE REALLY NEED THIS ROUTINE???
1393244Swnj  */
1403244Swnj /*ARGSUSED*/
1413244Swnj tsslave(ui, reg)
1423244Swnj 	struct uba_device *ui;
1433244Swnj 	caddr_t reg;
1443244Swnj {
1451900Swnj 
1463244Swnj 	if (ui->ui_slave)	/* non-zero slave not allowed */
1473244Swnj 		return(0);
1483244Swnj 	return (1);
1493244Swnj }
1501900Swnj 
1513244Swnj /*
1523244Swnj  * Record attachment of the unit to the controller.
1533244Swnj  *
1543244Swnj  * SHOULD THIS ROUTINE DO ANYTHING???
1553244Swnj  */
1563244Swnj /*ARGSUSED*/
1573244Swnj tsattach(ui)
1583244Swnj 	struct uba_device *ui;
1593244Swnj {
1601900Swnj 
1613244Swnj }
1621900Swnj 
1633244Swnj /*
1643244Swnj  * Open the device.  Tapes are unique open
1653244Swnj  * devices, so we refuse if it is already open.
1663244Swnj  * We also check that a tape is available, and
1673244Swnj  * don't block waiting here; if you want to wait
1683244Swnj  * for a tape you should timeout in user code.
1693244Swnj  */
1701900Swnj tsopen(dev, flag)
1713244Swnj 	dev_t dev;
1723244Swnj 	int flag;
1731900Swnj {
1743244Swnj 	register int tsunit;
1753244Swnj 	register struct uba_device *ui;
1763244Swnj 	register struct ts_softc *sc;
1771900Swnj 
1783244Swnj 	tsunit = TSUNIT(dev);
1793244Swnj 	if (tsunit>=NTS || (sc = &ts_softc[tsunit])->sc_openf ||
1803244Swnj 	    (ui = tsdinfo[tsunit]) == 0 || ui->ui_alive == 0) {
1811900Swnj 		u.u_error = ENXIO;
1821900Swnj 		return;
1831900Swnj 	}
1843244Swnj 	if (tsinit(tsunit)) {
1851900Swnj 		u.u_error = ENXIO;
1863327Swnj 		printd("init failed\n");
1871900Swnj 		return;
1881900Swnj 	}
1893327Swnj 	printd("init ok\n");
1903244Swnj 	tscommand(dev, TS_SENSE, 1);
1913327Swnj 	printd("sense xs0 %o\n", sc->sc_sts.s_xs0);
1923244Swnj 	if ((sc->sc_sts.s_xs0&TS_ONL) == 0 || ((flag&(FREAD|FWRITE)) ==
1933244Swnj 	    FWRITE && (sc->sc_sts.s_xs0&TS_WLK))) {
1943244Swnj 		/*
1953244Swnj 		 * Not online or write locked.
1963244Swnj 		 */
1973244Swnj 		u.u_error = EIO;
1983244Swnj 		return;
1991900Swnj 	}
2003244Swnj 	sc->sc_openf = 1;
2013244Swnj 	sc->sc_blkno = (daddr_t)0;
2023244Swnj 	sc->sc_nxrec = INF;
2033244Swnj 	sc->sc_lastiow = 0;
2041900Swnj }
2051900Swnj 
2063244Swnj /*
2073244Swnj  * Close tape device.
2083244Swnj  *
2093244Swnj  * If tape was open for writing or last operation was
2103244Swnj  * a write, then write two EOF's and backspace over the last one.
2113244Swnj  * Unless this is a non-rewinding special file, rewind the tape.
2123244Swnj  * Make the tape available to others.
2133244Swnj  */
2141900Swnj tsclose(dev, flag)
2153244Swnj 	register dev_t dev;
2163244Swnj 	register flag;
2171900Swnj {
2183244Swnj 	register struct ts_softc *sc = &ts_softc[TSUNIT(dev)];
2191900Swnj 
2203244Swnj 	if (flag == FWRITE || (flag&FWRITE) && sc->sc_lastiow) {
2213244Swnj 		tscommand(dev, TS_WEOF, 1);
2223244Swnj 		tscommand(dev, TS_WEOF, 1);
2233244Swnj 		tscommand(dev, TS_SREV, 1);
2241900Swnj 	}
2253244Swnj 	if ((minor(dev)&T_NOREWIND) == 0)
2263244Swnj 		/*
2273244Swnj 		 * 0 count means don't hang waiting for rewind complete
2283244Swnj 		 * rather ctsbuf stays busy until the operation completes
2293244Swnj 		 * preventing further opens from completing by
2303244Swnj 		 * preventing a TS_SENSE from completing.
2313244Swnj 		 */
2323244Swnj 		tscommand(dev, TS_REW, 0);
2333244Swnj 	sc->sc_openf = 0;
2341900Swnj }
2351900Swnj 
2363244Swnj /*
2373244Swnj  * Initialize the TS11.  Set up Unibus mapping for command
2383244Swnj  * packets and set device characteristics.
2393244Swnj  */
2403244Swnj tsinit(unit)
2413244Swnj 	register int unit;
2421900Swnj {
2433244Swnj 	register struct ts_softc *sc = &ts_softc[unit];
2443244Swnj 	register struct uba_ctlr *um = tsminfo[unit];
2453244Swnj 	register struct device *addr = (struct device *)um->um_addr;
2463244Swnj 	register int i;
2473244Swnj 
2483244Swnj 	/*
2493244Swnj 	 * Map the command and message packets into Unibus
2503244Swnj 	 * address space.  We do all the command and message
2513244Swnj 	 * packets at once to minimize the amount of Unibus
2523244Swnj 	 * mapping necessary.
2533244Swnj 	 */
2543244Swnj 	if (ts_ubaddr == 0) {
2553244Swnj 		ctsbuf[unit].b_un.b_addr = (caddr_t)ts_softc;
2563244Swnj 		ctsbuf[unit].b_bcount = sizeof(ts_softc);
2573244Swnj 		i = ubasetup(um->um_ubanum, &ctsbuf[unit], 0);
2583244Swnj 		i &= 0777777;
2593244Swnj 		ts_ubaddr = (struct ts_softc *)i;
2603244Swnj 		/* MAKE SURE WE DON'T GET UNIBUS ADDRESS ZERO */
2613244Swnj 		if (ts_ubaddr == 0)
2623244Swnj 			printf("ts%d: zero ubaddr\n", unit);
2633244Swnj 	}
2643244Swnj 	/*
2653244Swnj 	 * Now initialize the TS11 controller.
2663244Swnj 	 * Set the characteristics.
2673244Swnj 	 */
2683244Swnj 	if (addr->tssr & TS_NBA) {
2693244Swnj 		addr->tssr = 0;		/* subsystem initialize */
2703244Swnj 		tswait(addr);
2713244Swnj 		i = (int)&ts_ubaddr[unit].sc_cmd;	/* Unibus addr of cmd */
2723327Swnj 		if (i&3) {
2733327Swnj 			printf("addr mod 4 != 0\n");
2743327Swnj 			return(1);
2753327Swnj 		}
2763244Swnj 		sc->sc_uba = (u_short)(i + ((i>>16)&3));
2773244Swnj 		sc->sc_char.char_addr = (int)&ts_ubaddr[unit].sc_sts;
2783244Swnj 		sc->sc_char.char_size = sizeof(struct ts_sts);
2793244Swnj 		sc->sc_char.char_mode = TS_ESS;
2803244Swnj 		sc->sc_cmd.c_cmd = TS_ACK | TS_SETCHR;
2813327Swnj 		i = (int)&ts_ubaddr[unit].sc_char;
2823327Swnj 		sc->sc_cmd.c_loba = i;
2833327Swnj 		sc->sc_cmd.c_hiba = (i>>16)&3;
2843244Swnj 		sc->sc_cmd.c_size = sizeof(struct ts_char);
2853244Swnj 		addr->tsdb = sc->sc_uba;
2863244Swnj 		tswait(addr);
2873327Swnj /*
2883327Swnj 		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);
2893327Swnj */
2903327Swnj 		if (addr->tssr & TS_NBA)
2913327Swnj 			return(1);
2923244Swnj 	}
2933244Swnj 	return(0);
2943244Swnj }
2953244Swnj 
2963244Swnj /*
2973244Swnj  * Execute a command on the tape drive
2983244Swnj  * a specified number of times.
2993244Swnj  */
3003244Swnj tscommand(dev, com, count)
3013244Swnj 	dev_t dev;
3023244Swnj 	int com, count;
3033244Swnj {
3041900Swnj 	register struct buf *bp;
3051900Swnj 
3063244Swnj 	bp = &ctsbuf[TSUNIT(dev)];
3073244Swnj 	(void) spl5();
3083244Swnj 	while (bp->b_flags&B_BUSY) {
3093244Swnj 		/*
3103244Swnj 		 * This special check is because B_BUSY never
3113244Swnj 		 * gets cleared in the non-waiting rewind case.
3123244Swnj 		 */
3133244Swnj 		if (bp->b_repcnt == 0 && (bp->b_flags&B_DONE))
3143244Swnj 			break;
3151900Swnj 		bp->b_flags |= B_WANTED;
3161900Swnj 		sleep((caddr_t)bp, PRIBIO);
3171900Swnj 	}
3183244Swnj 	bp->b_flags = B_BUSY|B_READ;
3193244Swnj 	(void) spl0();
3203327Swnj 	printd("command %o dev %x count %d\n", com, dev, count);
3213244Swnj 	bp->b_dev = dev;
3223244Swnj 	bp->b_repcnt = count;
3233244Swnj 	bp->b_command = com;
3241900Swnj 	bp->b_blkno = 0;
3251900Swnj 	tsstrategy(bp);
3263244Swnj 	/*
3273244Swnj 	 * In case of rewind from close, don't wait.
3283244Swnj 	 * This is the only case where count can be 0.
3293244Swnj 	 */
3303244Swnj 	if (count == 0)
3313244Swnj 		return;
3321900Swnj 	iowait(bp);
3333244Swnj 	if (bp->b_flags&B_WANTED)
3341900Swnj 		wakeup((caddr_t)bp);
3353244Swnj 	bp->b_flags &= B_ERROR;
3361900Swnj }
3371900Swnj 
3383244Swnj /*
3393244Swnj  * Queue a tape operation.
3403244Swnj  */
3411900Swnj tsstrategy(bp)
3423244Swnj 	register struct buf *bp;
3431900Swnj {
3443244Swnj 	int tsunit = TSUNIT(bp->b_dev);
3453244Swnj 	register struct uba_ctlr *um;
3463327Swnj 	register struct buf *dp;
3471900Swnj 
3483244Swnj 	/*
3493244Swnj 	 * Put transfer at end of controller queue
3503244Swnj 	 */
3511900Swnj 	bp->av_forw = NULL;
3523244Swnj 	um = tsdinfo[tsunit]->ui_mi;
3533327Swnj 	dp = &tsbuf[tsunit];
3543244Swnj 	(void) spl5();
3553327Swnj 	if (dp->b_actf == NULL)
3563327Swnj 		dp->b_actf = bp;
3571900Swnj 	else
3583327Swnj 		dp->b_actl->av_forw = bp;
3593327Swnj 	dp->b_actl = bp;
3603327Swnj 	um->um_tab.b_actf = um->um_tab.b_actl = dp;
3613244Swnj 	/*
3623244Swnj 	 * If the controller is not busy, get
3633244Swnj 	 * it going.
3643244Swnj 	 */
3653244Swnj 	if (um->um_tab.b_active == 0)
3663244Swnj 		tsstart(um);
3673244Swnj 	(void) spl0();
3681900Swnj }
3691900Swnj 
3703244Swnj /*
3713244Swnj  * Start activity on a ts controller.
3723244Swnj  */
3733244Swnj tsstart(um)
3743244Swnj 	register struct uba_ctlr *um;
3751900Swnj {
3761900Swnj 	register struct buf *bp;
3773244Swnj 	register struct device *addr = (struct device *)um->um_addr;
3783244Swnj 	register struct ts_softc *sc;
3793244Swnj 	register struct ts_cmd *tc;
3803244Swnj 	register struct uba_device *ui;
3813244Swnj 	int tsunit, cmd;
3821900Swnj 	daddr_t blkno;
3831900Swnj 
3843244Swnj 	/*
3853244Swnj 	 * Start the controller if there is something for it to do.
3863244Swnj 	 */
3873244Swnj loop:
3883327Swnj 	if ((bp = um->um_tab.b_actf->b_actf) == NULL)
3891900Swnj 		return;
3903244Swnj 	tsunit = TSUNIT(bp->b_dev);
3913244Swnj 	ui = tsdinfo[tsunit];
3923244Swnj 	sc = &ts_softc[tsunit];
3933244Swnj 	tc = &sc->sc_cmd;
3943244Swnj 	/*
3953244Swnj 	 * Default is that last command was NOT a write command;
3963244Swnj 	 * if we do a write command we will notice this in tsintr().
3973244Swnj 	 */
3983244Swnj 	sc->sc_lastiow = 1;
3993244Swnj 	if (sc->sc_openf < 0 || (addr->tssr&TS_OFL)) {
4003244Swnj 		/*
4013244Swnj 		 * Have had a hard error on a non-raw tape
4023244Swnj 		 * or the tape unit is now unavailable
4033244Swnj 		 * (e.g. taken off line).
4043244Swnj 		 */
4053244Swnj 		bp->b_flags |= B_ERROR;
4063244Swnj 		goto next;
4073244Swnj 	}
4083244Swnj 	if (bp == &ctsbuf[TSUNIT(bp->b_dev)]) {
4093244Swnj 		/*
4103244Swnj 		 * Execute control operation with the specified count.
4113244Swnj 		 */
4123244Swnj 		um->um_tab.b_active =
4133244Swnj 		    bp->b_command == TS_REW ? SREW : SCOM;
4143244Swnj 		tc->c_repcnt = bp->b_repcnt;
4153327Swnj 		printd("strat: do cmd\n");
4163244Swnj 		goto dobpcmd;
4173244Swnj 	}
4183244Swnj 	/*
4193244Swnj 	 * The following checks handle boundary cases for operation
4203244Swnj 	 * on non-raw tapes.  On raw tapes the initialization of
4213244Swnj 	 * sc->sc_nxrec by tsphys causes them to be skipped normally
4223244Swnj 	 * (except in the case of retries).
4233244Swnj 	 */
4243244Swnj 	if (dbtofsb(bp->b_blkno) > sc->sc_nxrec) {
4253244Swnj 		/*
4263244Swnj 		 * Can't read past known end-of-file.
4273244Swnj 		 */
4283244Swnj 		bp->b_flags |= B_ERROR;
4293244Swnj 		bp->b_error = ENXIO;
4303244Swnj 		goto next;
4313244Swnj 	}
4323244Swnj 	if (dbtofsb(bp->b_blkno) == sc->sc_nxrec &&
4333244Swnj 	    bp->b_flags&B_READ) {
4343244Swnj 		/*
4353244Swnj 		 * Reading at end of file returns 0 bytes.
4363244Swnj 		 */
4373244Swnj 		bp->b_resid = bp->b_bcount;
4383244Swnj 		clrbuf(bp);
4393244Swnj 		goto next;
4403244Swnj 	}
4413244Swnj 	if ((bp->b_flags&B_READ) == 0)
4423244Swnj 		/*
4433244Swnj 		 * Writing sets EOF
4443244Swnj 		 */
4453244Swnj 		sc->sc_nxrec = dbtofsb(bp->b_blkno) + 1;
4463244Swnj 	/*
4473244Swnj 	 * If the data transfer command is in the correct place,
4483244Swnj 	 * set up all the registers except the csr, and give
4493244Swnj 	 * control over to the UNIBUS adapter routines, to
4503244Swnj 	 * wait for resources to start the i/o.
4513244Swnj 	 */
4523244Swnj 	if ((blkno = sc->sc_blkno) == dbtofsb(bp->b_blkno)) {
4533244Swnj 		tc->c_size = bp->b_bcount;
4543244Swnj 		if ((bp->b_flags&B_READ) == 0)
4553244Swnj 			cmd = TS_WCOM;
4561900Swnj 		else
4573244Swnj 			cmd = TS_RCOM;
4583244Swnj 		if (um->um_tab.b_errcnt)
4593244Swnj 			cmd |= TS_RETRY;
4603244Swnj 		um->um_tab.b_active = SIO;
4613327Swnj 		tc->c_cmd = TS_ACK | TS_CVC | TS_IE | cmd;
4623327Swnj 		printd("r/w %o size %d\n", tc->c_cmd, tc->c_size);
4633244Swnj 		(void) ubago(ui);
4643244Swnj 		return;
4653244Swnj 	}
4663244Swnj 	/*
4673244Swnj 	 * Tape positioned incorrectly;
4683244Swnj 	 * set to seek forwards or backwards to the correct spot.
4693244Swnj 	 * This happens for raw tapes only on error retries.
4703244Swnj 	 */
4713244Swnj 	um->um_tab.b_active = SSEEK;
4723327Swnj 	printd("seek blkno %d b_blkno %d\n", blkno, bp->b_blkno);
4733244Swnj 	if (blkno < dbtofsb(bp->b_blkno)) {
4743244Swnj 		bp->b_command = TS_SFORW;
4753244Swnj 		tc->c_repcnt = dbtofsb(bp->b_blkno) - blkno;
4761900Swnj 	} else {
4773244Swnj 		bp->b_command = TS_SREV;
4783244Swnj 		tc->c_repcnt = blkno - dbtofsb(bp->b_blkno);
4791900Swnj 	}
4803244Swnj dobpcmd:
4813244Swnj 	/*
4823244Swnj 	 * Do the command in bp.
4833244Swnj 	 */
4843327Swnj 	tc->c_cmd = TS_ACK | TS_CVC | TS_IE | bp->b_command;
4853244Swnj 	addr->tsdb = sc->sc_uba;
4861900Swnj 	return;
4871900Swnj 
4883244Swnj next:
4893244Swnj 	/*
4903244Swnj 	 * Done with this operation due to error or
4913244Swnj 	 * the fact that it doesn't do anything.
4923244Swnj 	 * Release UBA resources (if any), dequeue
4933244Swnj 	 * the transfer and continue processing this slave.
4943244Swnj 	 */
4953244Swnj 	if (um->um_ubinfo)
4963244Swnj 		ubadone(um);
4973244Swnj 	um->um_tab.b_errcnt = 0;
4983327Swnj 	um->um_tab.b_actf->b_actf = bp->av_forw;
4991900Swnj 	iodone(bp);
5001900Swnj 	goto loop;
5011900Swnj }
5021900Swnj 
5033244Swnj /*
5043244Swnj  * The UNIBUS resources we needed have been
5053244Swnj  * allocated to us; start the device.
5063244Swnj  */
5073244Swnj tsdgo(um)
5083244Swnj 	register struct uba_ctlr *um;
5091900Swnj {
5103244Swnj 	register struct device *addr = (struct device *)um->um_addr;
5113244Swnj 	register struct ts_softc *sc = &ts_softc[um->um_ctlr];
5123327Swnj 	register int i;
5133244Swnj 
5143327Swnj 	i = um->um_ubinfo & 0777777;
5153327Swnj 	printd("dgo addr %o\n", i);
5163327Swnj 	sc->sc_cmd.c_loba = i;
5173327Swnj 	sc->sc_cmd.c_hiba = (i>>16)&3;
5183244Swnj 	addr->tsdb = sc->sc_uba;
5193244Swnj }
5203244Swnj 
5213244Swnj /*
5223244Swnj  * Ts interrupt routine.
5233244Swnj  */
5243244Swnj /*ARGSUSED*/
5253244Swnj tsintr(ts11)
5263244Swnj 	int ts11;
5273244Swnj {
5281900Swnj 	register struct buf *bp;
5293244Swnj 	register struct uba_ctlr *um = tsminfo[ts11];
5303244Swnj 	register struct device *addr;
5313244Swnj 	register struct ts_softc *sc;
5323244Swnj 	int tsunit;
5333244Swnj 	register state;
5341900Swnj 
5353327Swnj 	printd("intr\n");
5363327Swnj 	if ((bp = um->um_tab.b_actf->b_actf) == NULL)
5371900Swnj 		return;
5383244Swnj 	tsunit = TSUNIT(bp->b_dev);
5393244Swnj 	addr = (struct device *)tsdinfo[tsunit]->ui_addr;
5403244Swnj 	/*
5413244Swnj 	 * If last command was a rewind, and tape is still
5423244Swnj 	 * rewinding, wait for the rewind complete interrupt.
5433244Swnj 	 *
5443244Swnj 	 * SHOULD NEVER GET AN INTERRUPT IN THIS STATE.
5453244Swnj 	 */
5463244Swnj 	if (um->um_tab.b_active == SREW) {
5473244Swnj 		um->um_tab.b_active = SCOM;
5483244Swnj 		if ((addr->tssr&TS_SSR) == 0)
5493244Swnj 			return;
5503244Swnj 	}
5513244Swnj 	/*
5523244Swnj 	 * An operation completed... record status
5533244Swnj 	 */
5543327Swnj 	printd("  ok1\n");
5553244Swnj 	sc = &ts_softc[tsunit];
5563244Swnj 	if ((bp->b_flags & B_READ) == 0)
5573244Swnj 		sc->sc_lastiow = 1;
5583244Swnj 	state = um->um_tab.b_active;
5593244Swnj 	um->um_tab.b_active = 0;
5603244Swnj 	/*
5613244Swnj 	 * Check for errors.
5623244Swnj 	 */
5633244Swnj 	if (addr->tssr&TS_SC) {
5643244Swnj 		switch (addr->tssr & TS_TC) {
5653244Swnj 		case TS_UNREC:		/* unrecoverable */
5663244Swnj 		case TS_FATAL:		/* fatal error */
5673244Swnj 		case TS_ATTN:		/* attention (shouldn't happen) */
5683244Swnj 		case TS_RECNM:		/* recoverable, no motion */
5693244Swnj 			break;
5701900Swnj 
5713244Swnj 		case TS_SUCC:		/* success termination */
5723244Swnj 			printf("ts%d: success\n", TSUNIT(minor(bp->b_dev)));
5733244Swnj 			goto ignoreerr;
5741900Swnj 
5753244Swnj 		case TS_ALERT:		/* tape status alert */
5763244Swnj 			/*
5773244Swnj 			 * If we hit the end of the tape file,
5783244Swnj 			 * update our position.
5793244Swnj 			 */
5803244Swnj 			if (sc->sc_sts.s_xs0 & (TS_TMK|TS_EOT)) {
5813244Swnj 				tsseteof(bp);		/* set blkno and nxrec */
5823244Swnj 				state = SCOM;		/* force completion */
5833244Swnj 				/*
5843244Swnj 				 * Stuff bc so it will be unstuffed correctly
5853244Swnj 				 * later to get resid.
5863244Swnj 				 */
5873244Swnj 				sc->sc_sts.s_rbpcr = bp->b_bcount;
5883244Swnj 				goto opdone;
5893244Swnj 			}
5903244Swnj 			/*
5913244Swnj 			 * If we were reading raw tape and the record was too long
5923244Swnj 			 * or too short, then we don't consider this an error.
5933244Swnj 			 */
5943244Swnj 			if (bp == &rtsbuf[TSUNIT(bp->b_dev)] && (bp->b_flags&B_READ) &&
5953244Swnj 			    sc->sc_sts.s_xs0&(TS_RLS|TS_RLL))
5963244Swnj 				goto ignoreerr;
5973244Swnj 		case TS_RECOV:		/* recoverable, tape moved */
5983244Swnj 			/*
5993244Swnj 			 * If this was an i/o operation retry up to 8 times.
6003244Swnj 			 */
6013244Swnj 			if (state==SIO) {
6023244Swnj 				if (++um->um_tab.b_errcnt < 7) {
6033244Swnj 					ubadone(um);
6043244Swnj 					goto opcont;
6053244Swnj 				} else
6063244Swnj 					sc->sc_blkno++;
6073244Swnj 			} else {
6083244Swnj 				/*
6093244Swnj 				 * Non-i/o errors on non-raw tape
6103244Swnj 				 * cause it to close.
6113244Swnj 				 */
6123244Swnj 				if (sc->sc_openf>0 && bp != &rtsbuf[TSUNIT(bp->b_dev)])
6133244Swnj 					sc->sc_openf = -1;
6143244Swnj 			}
6151900Swnj 			break;
6163244Swnj 
6173244Swnj 		case TS_REJECT:		/* function reject */
6183244Swnj 			if (state == SIO && sc->sc_sts.s_xs0 & TS_WLE)
6193244Swnj 				printf("ts%d: write locked\n", TSUNIT(bp->b_dev));
6203244Swnj 			if ((sc->sc_sts.s_xs0 & TS_ONL) == 0)
6213244Swnj 				printf("ts%d: offline\n", TSUNIT(bp->b_dev));
6221900Swnj 			break;
6231900Swnj 		}
6243244Swnj 		/*
6253244Swnj 		 * Couldn't recover error
6263244Swnj 		 */
6273244Swnj 		printf("ts%d: hard error bn%d xs0=%b\n", TSUNIT(bp->b_dev),
6283244Swnj 		    bp->b_blkno, sc->sc_sts.s_xs0, TSXS0_BITS);
6293244Swnj 		bp->b_flags |= B_ERROR;
6303244Swnj 		goto opdone;
6313244Swnj 	}
6323244Swnj 	/*
6333244Swnj 	 * Advance tape control FSM.
6343244Swnj 	 */
6353244Swnj ignoreerr:
6363244Swnj 	switch (state) {
6371900Swnj 
6383244Swnj 	case SIO:
6393244Swnj 		/*
6403244Swnj 		 * Read/write increments tape block number
6413244Swnj 		 */
6423244Swnj 		sc->sc_blkno++;
6433244Swnj 		goto opdone;
6441900Swnj 
6451900Swnj 	case SCOM:
6463244Swnj 		/*
6473244Swnj 		 * For forward/backward space record update current position.
6483244Swnj 		 */
6493244Swnj 		if (bp == &ctsbuf[TSUNIT(bp->b_dev)])
6503244Swnj 		switch (bp->b_command) {
6511900Swnj 
6523244Swnj 		case TS_SFORW:
6533244Swnj 			sc->sc_blkno += bp->b_repcnt;
6543244Swnj 			break;
6551900Swnj 
6563244Swnj 		case TS_SREV:
6573244Swnj 			sc->sc_blkno -= bp->b_repcnt;
6583244Swnj 			break;
6593244Swnj 		}
6603244Swnj 		goto opdone;
6613244Swnj 
6623244Swnj 	case SSEEK:
6633244Swnj 		sc->sc_blkno = dbtofsb(bp->b_blkno);
6643244Swnj 		goto opcont;
6653244Swnj 
6661900Swnj 	default:
6673244Swnj 		panic("tsintr");
6681900Swnj 	}
6693244Swnj opdone:
6703244Swnj 	/*
6713244Swnj 	 * Reset error count and remove
6723244Swnj 	 * from device queue.
6733244Swnj 	 */
6743244Swnj 	um->um_tab.b_errcnt = 0;
6753327Swnj 	um->um_tab.b_actf->b_actf = bp->av_forw;
6763244Swnj 	bp->b_resid = sc->sc_sts.s_rbpcr;
6773244Swnj 	ubadone(um);
6783327Swnj 	printd("  iodone\n");
6793244Swnj 	iodone(bp);
6803327Swnj 	if (um->um_tab.b_actf->b_actf == 0)
6813244Swnj 		return;
6823244Swnj opcont:
6833244Swnj 	tsstart(um);
6843244Swnj }
6853244Swnj 
6863244Swnj tsseteof(bp)
6873244Swnj 	register struct buf *bp;
6883244Swnj {
6893244Swnj 	register int tsunit = TSUNIT(bp->b_dev);
6903244Swnj 	register struct ts_softc *sc = &ts_softc[tsunit];
6913244Swnj 
6923244Swnj 	if (bp == &ctsbuf[TSUNIT(bp->b_dev)]) {
6933244Swnj 		if (sc->sc_blkno > dbtofsb(bp->b_blkno)) {
6943244Swnj 			/* reversing */
6953244Swnj 			sc->sc_nxrec = dbtofsb(bp->b_blkno) - sc->sc_sts.s_rbpcr;
6963244Swnj 			sc->sc_blkno = sc->sc_nxrec;
6973244Swnj 		} else {
6983244Swnj 			/* spacing forward */
6993244Swnj 			sc->sc_blkno = dbtofsb(bp->b_blkno) + sc->sc_sts.s_rbpcr;
7003244Swnj 			sc->sc_nxrec = sc->sc_blkno - 1;
7011900Swnj 		}
7023244Swnj 		return;
7033244Swnj 	}
7043244Swnj 	/* eof on read */
7053244Swnj 	sc->sc_nxrec = dbtofsb(bp->b_blkno);
7061900Swnj }
7071900Swnj 
7081900Swnj tsread(dev)
7093244Swnj 	dev_t dev;
7101900Swnj {
7113244Swnj 
7121900Swnj 	tsphys(dev);
7133244Swnj 	if (u.u_error)
7143244Swnj 		return;
7153244Swnj 	physio(tsstrategy, &rtsbuf[TSUNIT(dev)], dev, B_READ, minphys);
7161900Swnj }
7171900Swnj 
7181900Swnj tswrite(dev)
7193244Swnj 	dev_t dev;
7201900Swnj {
7213244Swnj 
7221900Swnj 	tsphys(dev);
7233244Swnj 	if (u.u_error)
7243244Swnj 		return;
7253244Swnj 	physio(tsstrategy, &rtsbuf[TSUNIT(dev)], dev, B_WRITE, minphys);
7261900Swnj }
7271900Swnj 
7283244Swnj /*
7293244Swnj  * Check that a raw device exists.
7303244Swnj  * If it does, set up sc_blkno and sc_nxrec
7313244Swnj  * so that the tape will appear positioned correctly.
7323244Swnj  */
7331900Swnj tsphys(dev)
7343244Swnj 	dev_t dev;
7351900Swnj {
7363244Swnj 	register int tsunit = TSUNIT(dev);
7373244Swnj 	register daddr_t a;
7383244Swnj 	register struct ts_softc *sc;
7393244Swnj 	register struct uba_device *ui;
7401900Swnj 
7413244Swnj 	if (tsunit >= NTS || (ui=tsdinfo[tsunit]) == 0 || ui->ui_alive == 0) {
7423244Swnj 		u.u_error = ENXIO;
7433244Swnj 		return;
7443244Swnj 	}
7453244Swnj 	sc = &ts_softc[tsunit];
7463244Swnj 	a = dbtofsb(u.u_offset >> 9);
7473244Swnj 	sc->sc_blkno = a;
7483244Swnj 	sc->sc_nxrec = a + 1;
7491900Swnj }
7501918Swnj 
7513244Swnj tsreset(uban)
7523244Swnj 	int uban;
7531918Swnj {
7543244Swnj 	register struct uba_ctlr *um;
7553244Swnj 	register ts11;
7563244Swnj 	register struct buf *dp;
7571918Swnj 
7583244Swnj 	for (ts11 = 0; ts11 < NTS; ts11++) {
7593244Swnj 		if ((um = tsminfo[ts11]) == 0 || um->um_alive == 0 ||
7603244Swnj 		   um->um_ubanum != uban)
7613244Swnj 			continue;
7623244Swnj 		printf(" ts%d", ts11);
7633244Swnj 		um->um_tab.b_active = 0;
7643244Swnj 		um->um_tab.b_actf = um->um_tab.b_actl = 0;
7653244Swnj 		ts_softc[ts11].sc_openf = -1;
7663244Swnj 		if (um->um_ubinfo) {
7673244Swnj 			printf("<%d>", (um->um_ubinfo>>28)&0xf);
7683244Swnj 			ubadone(um);
7693244Swnj 		}
7703244Swnj 		tsinit(ts11);
7713244Swnj 		tsstart(um);
7721918Swnj 	}
7731918Swnj }
7741918Swnj 
7753244Swnj /*ARGSUSED*/
7763244Swnj tsioctl(dev, cmd, addr, flag)
7773244Swnj 	caddr_t addr;
7783244Swnj 	dev_t dev;
7791918Swnj {
7803244Swnj 	int tsunit = TSUNIT(dev);
7813244Swnj 	register struct ts_softc *sc = &ts_softc[tsunit];
7823244Swnj 	register struct buf *bp = &ctsbuf[TSUNIT(dev)];
7833244Swnj 	register callcount;
7843244Swnj 	int fcount;
7853244Swnj 	struct mtop mtop;
7863244Swnj 	struct mtget mtget;
7873244Swnj 	/* we depend of the values and order of the MT codes here */
7883244Swnj 	static tsops[] =
7893244Swnj 	 {TS_WEOF,TS_SFORW,TS_SREV,TS_SFORWF,TS_SREVF,TS_REW,TS_OFFL,TS_SENSE};
7901918Swnj 
7913244Swnj 	switch (cmd) {
7923244Swnj 	case MTIOCTOP:	/* tape operation */
7933244Swnj 		if (copyin((caddr_t)addr, (caddr_t)&mtop, sizeof(mtop))) {
7943244Swnj 			u.u_error = EFAULT;
7953244Swnj 			return;
7963244Swnj 		}
7973244Swnj 		switch(mtop.mt_op) {
7983244Swnj 		case MTWEOF:
7993244Swnj 			callcount = mtop.mt_count;
8003244Swnj 			fcount = 1;
8013244Swnj 			break;
8023244Swnj 		case MTFSF: case MTBSF:
8033244Swnj 		case MTFSR: case MTBSR:
8043244Swnj 			callcount = 1;
8053244Swnj 			fcount = mtop.mt_count;
8063244Swnj 			break;
8073244Swnj 		case MTREW: case MTOFFL: case MTNOP:
8083244Swnj 			callcount = 1;
8093244Swnj 			fcount = 1;
8103244Swnj 			break;
8113244Swnj 		default:
8123244Swnj 			u.u_error = ENXIO;
8133244Swnj 			return;
8143244Swnj 		}
8153244Swnj 		if (callcount <= 0 || fcount <= 0) {
8163244Swnj 			u.u_error = ENXIO;
8173244Swnj 			return;
8183244Swnj 		}
8193244Swnj 		while (--callcount >= 0) {
8203244Swnj 			tscommand(dev, tsops[mtop.mt_op], fcount);
8213244Swnj 			if ((mtop.mt_op == MTFSR || mtop.mt_op == MTBSR) &&
8223244Swnj 			    bp->b_resid) {
8233244Swnj 				u.u_error = EIO;
8243244Swnj 				break;
8253244Swnj 			}
8263244Swnj 			if ((bp->b_flags&B_ERROR) || sc->sc_sts.s_xs0&TS_BOT)
8273244Swnj 				break;
8283244Swnj 		}
8293244Swnj 		geterror(bp);
8303244Swnj 		return;
8313244Swnj 	case MTIOCGET:
8323244Swnj 		mtget.mt_dsreg = 0;
8333244Swnj 		mtget.mt_erreg = sc->sc_sts.s_xs0;
8343244Swnj 		mtget.mt_resid = sc->sc_resid;
835*3480Stoy 		mtget.mt_type = MT_ISTS;
8363244Swnj 		if (copyout((caddr_t)&mtget, addr, sizeof(mtget)))
8373244Swnj 			u.u_error = EFAULT;
8383244Swnj 		return;
8393244Swnj 	default:
8403244Swnj 		u.u_error = ENXIO;
8413244Swnj 	}
8421918Swnj }
8431918Swnj 
8443244Swnj #define	DBSIZE	20
8453244Swnj 
8463244Swnj tsdump()
8471918Swnj {
8483244Swnj 	register struct uba_device *ui;
8493244Swnj 	register struct uba_regs *up;
8503244Swnj 	register struct device *addr;
8513244Swnj 	int blk, num;
8523244Swnj 	int start;
8531918Swnj 
8543244Swnj 	start = 0;
8553244Swnj 	num = maxfree;
8563244Swnj #define	phys(a,b)	((b)((int)(a)&0x7fffffff))
8573244Swnj 	if (tsdinfo[0] == 0)
8583244Swnj 		return (ENXIO);
8593244Swnj 	ui = phys(tsdinfo[0], struct uba_device *);
8603244Swnj 	up = phys(ui->ui_hd, struct uba_hd *)->uh_physuba;
8613327Swnj 	ubainit(up);
8623244Swnj 	DELAY(1000000);
8633244Swnj 	addr = (struct device *)ui->ui_physaddr;
8643244Swnj 	addr->tssr = 0;
8653244Swnj 	tswait(addr);
8663244Swnj 	while (num > 0) {
8673244Swnj 		blk = num > DBSIZE ? DBSIZE : num;
8683244Swnj 		tsdwrite(start, blk, addr, up);
8693244Swnj 		start += blk;
8703244Swnj 		num -= blk;
8713244Swnj 	}
8723244Swnj 	tseof(addr);
8733244Swnj 	tseof(addr);
8743244Swnj 	tswait(addr);
8753244Swnj 	if (addr->tssr&TS_SC)
8763244Swnj 		return (EIO);
8773244Swnj 	addr->tssr = 0;
8783244Swnj 	tswait(addr);
8793244Swnj 	return (0);
8801918Swnj }
8811918Swnj 
8823244Swnj tsdwrite(dbuf, num, addr, up)
8833244Swnj 	register dbuf, num;
8843244Swnj 	register struct device *addr;
8853244Swnj 	struct uba_regs *up;
8861918Swnj {
8873244Swnj 	register struct pte *io;
8883244Swnj 	register int npf;
8891918Swnj 
8903244Swnj 	tswait(addr);
8913244Swnj 	io = up->uba_map;
8923244Swnj 	npf = num+1;
8933244Swnj 	while (--npf != 0)
8943244Swnj 		 *(int *)io++ = (dbuf++ | (1<<UBAMR_DPSHIFT) | UBAMR_MRV);
8953244Swnj 	*(int *)io = 0;
8963244Swnj #ifdef notyet
8973244Swnj 	addr->tsbc = -(num*NBPG);
8983244Swnj 	addr->tsba = 0;
8993244Swnj 	addr->tscs = TS_WCOM | TM_GO;
9003244Swnj #endif
9011918Swnj }
9021918Swnj 
9033244Swnj tswait(addr)
9043244Swnj 	register struct device *addr;
9051918Swnj {
9063244Swnj 	register s;
9071918Swnj 
9083244Swnj 	do
9093244Swnj 		s = addr->tssr;
9103244Swnj 	while ((s & TS_SSR) == 0);
9111918Swnj }
9121918Swnj 
9133244Swnj tseof(addr)
9143244Swnj 	struct device *addr;
9151918Swnj {
9161918Swnj 
9173244Swnj 	tswait(addr);
9183244Swnj #ifdef notyet
9193244Swnj 	addr->tscs = TS_WEOF | TM_GO;
9203244Swnj #endif
9211918Swnj }
9221900Swnj #endif
923