xref: /csrg-svn/sys/vax/uba/ts.c (revision 7407)
1*7407Skre /*	ts.c	4.25	82/07/15	*/
21900Swnj 
31941Swnj #include "ts.h"
41900Swnj #if NTS > 0
51900Swnj /*
61900Swnj  * TS11 tape driver
73244Swnj  *
83244Swnj  * TODO:
95693Sroot  *	write dump code
101900Swnj  */
111900Swnj #include "../h/param.h"
121900Swnj #include "../h/systm.h"
131900Swnj #include "../h/buf.h"
143244Swnj #include "../h/dir.h"
151900Swnj #include "../h/conf.h"
163244Swnj #include "../h/user.h"
171900Swnj #include "../h/file.h"
183244Swnj #include "../h/map.h"
191900Swnj #include "../h/pte.h"
201947Swnj #include "../h/vm.h"
213244Swnj #include "../h/ubareg.h"
223244Swnj #include "../h/ubavar.h"
233244Swnj #include "../h/mtio.h"
243244Swnj #include "../h/ioctl.h"
253244Swnj #include "../h/cmap.h"
263244Swnj #include "../h/cpu.h"
271900Swnj 
283244Swnj #include "../h/tsreg.h"
291900Swnj 
303244Swnj /*
313244Swnj  * There is a ctsbuf per tape controller.
323244Swnj  * It is used as the token to pass to the internal routines
333244Swnj  * to execute tape ioctls.
343244Swnj  * In particular, when the tape is rewinding on close we release
353244Swnj  * the user process but any further attempts to use the tape drive
363244Swnj  * before the rewind completes will hang waiting for ctsbuf.
373244Swnj  */
383244Swnj struct	buf	ctsbuf[NTS];
391900Swnj 
403244Swnj /*
413244Swnj  * Raw tape operations use rtsbuf.  The driver
423244Swnj  * notices when rtsbuf is being used and allows the user
433244Swnj  * program to continue after errors and read records
443244Swnj  * not of the standard length (BSIZE).
453244Swnj  */
463244Swnj struct	buf	rtsbuf[NTS];
471900Swnj 
483244Swnj /*
493244Swnj  * Driver unibus interface routines and variables.
503244Swnj  */
513244Swnj int	tsprobe(), tsslave(), tsattach(), tsdgo(), tsintr();
523244Swnj struct	uba_ctlr *tsminfo[NTS];
533244Swnj struct	uba_device *tsdinfo[NTS];
545693Sroot struct buf	tsutab[NTS];
553244Swnj u_short	tsstd[] = { 0772520, 0 };
563244Swnj /*** PROBABLY DON'T NEED ALL THESE SINCE CONTROLLER == DRIVE ***/
573244Swnj struct	uba_driver zsdriver =
583327Swnj  { tsprobe, tsslave, tsattach, tsdgo, tsstd, "ts", tsdinfo, "zs", tsminfo, 0 };
591900Swnj 
603244Swnj /* bits in minor device */
613244Swnj #define	TSUNIT(dev)	(minor(dev)&03)
623244Swnj #define	T_NOREWIND	04
631900Swnj 
643244Swnj #define	INF	(daddr_t)1000000L
651900Swnj 
663244Swnj /*
673244Swnj  * Software state per tape transport.
683244Swnj  * Also contains hardware state in message packets.
693244Swnj  *
703244Swnj  * 1. A tape drive is a unique-open device; we refuse opens when it is already.
713244Swnj  * 2. We keep track of the current position on a block tape and seek
723244Swnj  *    before operations by forward/back spacing if necessary.
733244Swnj  * 3. We remember if the last operation was a write on a tape, so if a tape
743244Swnj  *    is open read write and the last thing done is a write we can
753244Swnj  *    write a standard end of tape mark (two eofs).
763244Swnj  * 4. We remember the status registers after the last command, using
773244Swnj  *    then internally and returning them to the SENSE ioctl.
783244Swnj  */
793244Swnj struct	ts_softc {
803244Swnj 	char	sc_openf;	/* lock against multiple opens */
813244Swnj 	char	sc_lastiow;	/* last op was a write */
823327Swnj 	short	sc_resid;	/* copy of last bc */
833244Swnj 	daddr_t	sc_blkno;	/* block number, for block device tape */
843244Swnj 	daddr_t	sc_nxrec;	/* position of end of tape, if known */
855693Sroot 	struct	ts_cmd sc_cmd;	/* the command packet */
865693Sroot 	struct	ts_sts sc_sts;	/* status packet, for returned status */
875693Sroot 	struct	ts_char sc_char; /* characteristics packet */
885693Sroot 	struct	ts_softc *sc_ubaddr; /* Unibus address of ts_softc structure */
895697Sroot 	u_short	sc_uba;		/* Unibus addr of cmd pkt for tsdb */
905693Sroot 	short	sc_mapped;	/* is ts_sfotc mapped in Unibus space? */
913244Swnj } ts_softc[NTS];
921900Swnj 
933244Swnj /*
943244Swnj  * States for um->um_tab.b_active, the per controller state flag.
953244Swnj  * This is used to sequence control in the driver.
963244Swnj  */
973244Swnj #define	SSEEK	1		/* seeking */
983244Swnj #define	SIO	2		/* doing seq i/o */
993244Swnj #define	SCOM	3		/* sending control command */
1003244Swnj #define	SREW	4		/* sending a drive rewind */
1011900Swnj 
1023244Swnj /*
1033244Swnj  * Determine if there is a controller for
1043244Swnj  * a ts at address reg.  Our goal is to make the
1053244Swnj  * device interrupt.
1063244Swnj  */
1073985Sroot /*ARGSUSED*/
1083244Swnj tsprobe(reg)
1093244Swnj 	caddr_t reg;
1103244Swnj {
1113244Swnj 	register int br, cvec;		/* must be r11,r10; value-result */
1121900Swnj 
1133244Swnj #ifdef lint
1143244Swnj 	br = 0; cvec = br; br = cvec;
1154937Swnj 	tsintr(0);
1163244Swnj #endif
1175693Sroot 	((struct tsdevice *)reg)->tssr = 0;
1185693Sroot 	DELAY(100);
1195693Sroot 	if ((((struct tsdevice *)reg)->tssr & TS_NBA) == 0)
1205693Sroot 		return(0);
1215693Sroot 	/* IT'S TOO HARD TO MAKE THIS THING INTERRUPT JUST TO FIND ITS VECTOR */
1225693Sroot 	cvec = ((unsigned)reg) & 07 ? 0260 : 0224;
1233244Swnj 	br = 0x15;
124*7407Skre 	return (sizeof (struct tsdevice));
1253244Swnj }
1261900Swnj 
1273244Swnj /*
1283244Swnj  * TS11 only supports one drive per controller;
1293244Swnj  * check for ui_slave == 0.
1303244Swnj  *
1313244Swnj  * DO WE REALLY NEED THIS ROUTINE???
1323244Swnj  */
1333244Swnj /*ARGSUSED*/
1343244Swnj tsslave(ui, reg)
1353244Swnj 	struct uba_device *ui;
1363244Swnj 	caddr_t reg;
1373244Swnj {
1381900Swnj 
1393244Swnj 	if (ui->ui_slave)	/* non-zero slave not allowed */
1403244Swnj 		return(0);
1413244Swnj 	return (1);
1423244Swnj }
1431900Swnj 
1443244Swnj /*
1453244Swnj  * Record attachment of the unit to the controller.
1463244Swnj  *
1473244Swnj  * SHOULD THIS ROUTINE DO ANYTHING???
1483244Swnj  */
1493244Swnj /*ARGSUSED*/
1503244Swnj tsattach(ui)
1513244Swnj 	struct uba_device *ui;
1523244Swnj {
1531900Swnj 
1543244Swnj }
1551900Swnj 
1563244Swnj /*
1573244Swnj  * Open the device.  Tapes are unique open
1583244Swnj  * devices, so we refuse if it is already open.
1593244Swnj  * We also check that a tape is available, and
1603244Swnj  * don't block waiting here; if you want to wait
1613244Swnj  * for a tape you should timeout in user code.
1623244Swnj  */
1631900Swnj tsopen(dev, flag)
1643244Swnj 	dev_t dev;
1653244Swnj 	int flag;
1661900Swnj {
1673244Swnj 	register int tsunit;
1683244Swnj 	register struct uba_device *ui;
1693244Swnj 	register struct ts_softc *sc;
1701900Swnj 
1713244Swnj 	tsunit = TSUNIT(dev);
1723244Swnj 	if (tsunit>=NTS || (sc = &ts_softc[tsunit])->sc_openf ||
1733244Swnj 	    (ui = tsdinfo[tsunit]) == 0 || ui->ui_alive == 0) {
1741900Swnj 		u.u_error = ENXIO;
1751900Swnj 		return;
1761900Swnj 	}
1773244Swnj 	if (tsinit(tsunit)) {
1781900Swnj 		u.u_error = ENXIO;
1791900Swnj 		return;
1801900Swnj 	}
1813244Swnj 	tscommand(dev, TS_SENSE, 1);
1823711Sroot 	if ((sc->sc_sts.s_xs0&TS_ONL) == 0) {
1833711Sroot 		uprintf("ts%d: not online\n", tsunit);
1843244Swnj 		u.u_error = EIO;
1853244Swnj 		return;
1861900Swnj 	}
1873718Sroot 	if ((flag&(FREAD|FWRITE)) == FWRITE && (sc->sc_sts.s_xs0&TS_WLK)) {
1883711Sroot 		uprintf("ts%d: no write ring\n", tsunit);
1893711Sroot 		u.u_error = EIO;
1903711Sroot 		return;
1913711Sroot 	}
1923244Swnj 	sc->sc_openf = 1;
1933244Swnj 	sc->sc_blkno = (daddr_t)0;
1943244Swnj 	sc->sc_nxrec = INF;
1953244Swnj 	sc->sc_lastiow = 0;
1961900Swnj }
1971900Swnj 
1983244Swnj /*
1993244Swnj  * Close tape device.
2003244Swnj  *
2013244Swnj  * If tape was open for writing or last operation was
2023244Swnj  * a write, then write two EOF's and backspace over the last one.
2033244Swnj  * Unless this is a non-rewinding special file, rewind the tape.
2043244Swnj  * Make the tape available to others.
2053244Swnj  */
2061900Swnj tsclose(dev, flag)
2073244Swnj 	register dev_t dev;
2083244Swnj 	register flag;
2091900Swnj {
2103244Swnj 	register struct ts_softc *sc = &ts_softc[TSUNIT(dev)];
2111900Swnj 
2123244Swnj 	if (flag == FWRITE || (flag&FWRITE) && sc->sc_lastiow) {
2133244Swnj 		tscommand(dev, TS_WEOF, 1);
2143244Swnj 		tscommand(dev, TS_WEOF, 1);
2153244Swnj 		tscommand(dev, TS_SREV, 1);
2161900Swnj 	}
2173244Swnj 	if ((minor(dev)&T_NOREWIND) == 0)
2183244Swnj 		/*
2193244Swnj 		 * 0 count means don't hang waiting for rewind complete
2203244Swnj 		 * rather ctsbuf stays busy until the operation completes
2213244Swnj 		 * preventing further opens from completing by
2223244Swnj 		 * preventing a TS_SENSE from completing.
2233244Swnj 		 */
2243244Swnj 		tscommand(dev, TS_REW, 0);
2253244Swnj 	sc->sc_openf = 0;
2261900Swnj }
2271900Swnj 
2283244Swnj /*
2293244Swnj  * Initialize the TS11.  Set up Unibus mapping for command
2303244Swnj  * packets and set device characteristics.
2313244Swnj  */
2323244Swnj tsinit(unit)
2333244Swnj 	register int unit;
2341900Swnj {
2353244Swnj 	register struct ts_softc *sc = &ts_softc[unit];
2363244Swnj 	register struct uba_ctlr *um = tsminfo[unit];
2375693Sroot 	register struct tsdevice *addr = (struct tsdevice *)um->um_addr;
2383244Swnj 	register int i;
2393244Swnj 
2403244Swnj 	/*
2413244Swnj 	 * Map the command and message packets into Unibus
2423244Swnj 	 * address space.  We do all the command and message
2433244Swnj 	 * packets at once to minimize the amount of Unibus
2443244Swnj 	 * mapping necessary.
2453244Swnj 	 */
2465693Sroot 	if (sc->sc_mapped == 0) {
2475693Sroot 		ctsbuf[unit].b_un.b_addr = (caddr_t)sc;
2485693Sroot 		ctsbuf[unit].b_bcount = sizeof(*sc);
2493244Swnj 		i = ubasetup(um->um_ubanum, &ctsbuf[unit], 0);
2503244Swnj 		i &= 0777777;
2515693Sroot 		sc->sc_ubaddr = (struct ts_softc *)i;
2525693Sroot 		sc->sc_mapped++;
2533244Swnj 	}
2543244Swnj 	/*
2553244Swnj 	 * Now initialize the TS11 controller.
2563244Swnj 	 * Set the characteristics.
2573244Swnj 	 */
2583668Swnj 	if (addr->tssr & (TS_NBA|TS_OFL)) {
2593244Swnj 		addr->tssr = 0;		/* subsystem initialize */
2603244Swnj 		tswait(addr);
2615693Sroot 		i = (int)&sc->sc_ubaddr->sc_cmd;	/* Unibus addr of cmd */
2623244Swnj 		sc->sc_uba = (u_short)(i + ((i>>16)&3));
2635693Sroot 		sc->sc_char.char_addr = (int)&sc->sc_ubaddr->sc_sts;
2643244Swnj 		sc->sc_char.char_size = sizeof(struct ts_sts);
2653244Swnj 		sc->sc_char.char_mode = TS_ESS;
2663244Swnj 		sc->sc_cmd.c_cmd = TS_ACK | TS_SETCHR;
2675693Sroot 		i = (int)&sc->sc_ubaddr->sc_char;
2683327Swnj 		sc->sc_cmd.c_loba = i;
2693327Swnj 		sc->sc_cmd.c_hiba = (i>>16)&3;
2703244Swnj 		sc->sc_cmd.c_size = sizeof(struct ts_char);
2713244Swnj 		addr->tsdb = sc->sc_uba;
2723244Swnj 		tswait(addr);
2733327Swnj 		if (addr->tssr & TS_NBA)
2743327Swnj 			return(1);
2753244Swnj 	}
2763244Swnj 	return(0);
2773244Swnj }
2783244Swnj 
2793244Swnj /*
2803244Swnj  * Execute a command on the tape drive
2813244Swnj  * a specified number of times.
2823244Swnj  */
2833244Swnj tscommand(dev, com, count)
2843244Swnj 	dev_t dev;
2853244Swnj 	int com, count;
2863244Swnj {
2871900Swnj 	register struct buf *bp;
2885438Sroot 	register int s;
2891900Swnj 
2903244Swnj 	bp = &ctsbuf[TSUNIT(dev)];
2915438Sroot 	s = spl5();
2923244Swnj 	while (bp->b_flags&B_BUSY) {
2933244Swnj 		/*
2943244Swnj 		 * This special check is because B_BUSY never
2953244Swnj 		 * gets cleared in the non-waiting rewind case.
2963244Swnj 		 */
2973244Swnj 		if (bp->b_repcnt == 0 && (bp->b_flags&B_DONE))
2983244Swnj 			break;
2991900Swnj 		bp->b_flags |= B_WANTED;
3001900Swnj 		sleep((caddr_t)bp, PRIBIO);
3011900Swnj 	}
3023244Swnj 	bp->b_flags = B_BUSY|B_READ;
3035438Sroot 	splx(s);
3043244Swnj 	bp->b_dev = dev;
3053244Swnj 	bp->b_repcnt = count;
3063244Swnj 	bp->b_command = com;
3071900Swnj 	bp->b_blkno = 0;
3081900Swnj 	tsstrategy(bp);
3093244Swnj 	/*
3103244Swnj 	 * In case of rewind from close, don't wait.
3113244Swnj 	 * This is the only case where count can be 0.
3123244Swnj 	 */
3133244Swnj 	if (count == 0)
3143244Swnj 		return;
3151900Swnj 	iowait(bp);
3163244Swnj 	if (bp->b_flags&B_WANTED)
3171900Swnj 		wakeup((caddr_t)bp);
3183244Swnj 	bp->b_flags &= B_ERROR;
3191900Swnj }
3201900Swnj 
3213244Swnj /*
3223244Swnj  * Queue a tape operation.
3233244Swnj  */
3241900Swnj tsstrategy(bp)
3253244Swnj 	register struct buf *bp;
3261900Swnj {
3273244Swnj 	int tsunit = TSUNIT(bp->b_dev);
3283244Swnj 	register struct uba_ctlr *um;
3293327Swnj 	register struct buf *dp;
3305438Sroot 	register int s;
3311900Swnj 
3323244Swnj 	/*
3333244Swnj 	 * Put transfer at end of controller queue
3343244Swnj 	 */
3351900Swnj 	bp->av_forw = NULL;
3363244Swnj 	um = tsdinfo[tsunit]->ui_mi;
3375438Sroot 	s = spl5();
3385693Sroot 	dp = &tsutab[tsunit];
3393327Swnj 	if (dp->b_actf == NULL)
3403327Swnj 		dp->b_actf = bp;
3411900Swnj 	else
3423327Swnj 		dp->b_actl->av_forw = bp;
3433327Swnj 	dp->b_actl = bp;
3443327Swnj 	um->um_tab.b_actf = um->um_tab.b_actl = dp;
3453244Swnj 	/*
3463244Swnj 	 * If the controller is not busy, get
3473244Swnj 	 * it going.
3483244Swnj 	 */
3493244Swnj 	if (um->um_tab.b_active == 0)
3503244Swnj 		tsstart(um);
3515438Sroot 	splx(s);
3521900Swnj }
3531900Swnj 
3543244Swnj /*
3553244Swnj  * Start activity on a ts controller.
3563244Swnj  */
3573244Swnj tsstart(um)
3583244Swnj 	register struct uba_ctlr *um;
3591900Swnj {
3601900Swnj 	register struct buf *bp;
3615693Sroot 	register struct tsdevice *addr = (struct tsdevice *)um->um_addr;
3623244Swnj 	register struct ts_softc *sc;
3633244Swnj 	register struct ts_cmd *tc;
3643244Swnj 	register struct uba_device *ui;
3653244Swnj 	int tsunit, cmd;
3661900Swnj 	daddr_t blkno;
3671900Swnj 
3683244Swnj 	/*
3693244Swnj 	 * Start the controller if there is something for it to do.
3703244Swnj 	 */
3713244Swnj loop:
3723327Swnj 	if ((bp = um->um_tab.b_actf->b_actf) == NULL)
3731900Swnj 		return;
3743244Swnj 	tsunit = TSUNIT(bp->b_dev);
3753244Swnj 	ui = tsdinfo[tsunit];
3763244Swnj 	sc = &ts_softc[tsunit];
3773244Swnj 	tc = &sc->sc_cmd;
3783244Swnj 	/*
3793244Swnj 	 * Default is that last command was NOT a write command;
3803244Swnj 	 * if we do a write command we will notice this in tsintr().
3813244Swnj 	 */
3823656Swnj 	sc->sc_lastiow = 0;
3833244Swnj 	if (sc->sc_openf < 0 || (addr->tssr&TS_OFL)) {
3843244Swnj 		/*
3853244Swnj 		 * Have had a hard error on a non-raw tape
3863244Swnj 		 * or the tape unit is now unavailable
3873244Swnj 		 * (e.g. taken off line).
3883244Swnj 		 */
3893244Swnj 		bp->b_flags |= B_ERROR;
3903244Swnj 		goto next;
3913244Swnj 	}
3923244Swnj 	if (bp == &ctsbuf[TSUNIT(bp->b_dev)]) {
3933244Swnj 		/*
3943244Swnj 		 * Execute control operation with the specified count.
3953244Swnj 		 */
3963244Swnj 		um->um_tab.b_active =
3973244Swnj 		    bp->b_command == TS_REW ? SREW : SCOM;
3983244Swnj 		tc->c_repcnt = bp->b_repcnt;
3993244Swnj 		goto dobpcmd;
4003244Swnj 	}
4013244Swnj 	/*
4023244Swnj 	 * The following checks handle boundary cases for operation
4033244Swnj 	 * on non-raw tapes.  On raw tapes the initialization of
4043244Swnj 	 * sc->sc_nxrec by tsphys causes them to be skipped normally
4053244Swnj 	 * (except in the case of retries).
4063244Swnj 	 */
4077383Ssam 	if (bdbtofsb(bp->b_blkno) > sc->sc_nxrec) {
4083244Swnj 		/*
4093244Swnj 		 * Can't read past known end-of-file.
4103244Swnj 		 */
4113244Swnj 		bp->b_flags |= B_ERROR;
4123244Swnj 		bp->b_error = ENXIO;
4133244Swnj 		goto next;
4143244Swnj 	}
4157383Ssam 	if (bdbtofsb(bp->b_blkno) == sc->sc_nxrec &&
4163244Swnj 	    bp->b_flags&B_READ) {
4173244Swnj 		/*
4183244Swnj 		 * Reading at end of file returns 0 bytes.
4193244Swnj 		 */
4203244Swnj 		bp->b_resid = bp->b_bcount;
4213244Swnj 		clrbuf(bp);
4223244Swnj 		goto next;
4233244Swnj 	}
4243244Swnj 	if ((bp->b_flags&B_READ) == 0)
4253244Swnj 		/*
4263244Swnj 		 * Writing sets EOF
4273244Swnj 		 */
4287383Ssam 		sc->sc_nxrec = bdbtofsb(bp->b_blkno) + 1;
4293244Swnj 	/*
4303244Swnj 	 * If the data transfer command is in the correct place,
4313244Swnj 	 * set up all the registers except the csr, and give
4323244Swnj 	 * control over to the UNIBUS adapter routines, to
4333244Swnj 	 * wait for resources to start the i/o.
4343244Swnj 	 */
4357383Ssam 	if ((blkno = sc->sc_blkno) == bdbtofsb(bp->b_blkno)) {
4363244Swnj 		tc->c_size = bp->b_bcount;
4373244Swnj 		if ((bp->b_flags&B_READ) == 0)
4383244Swnj 			cmd = TS_WCOM;
4391900Swnj 		else
4403244Swnj 			cmd = TS_RCOM;
4413244Swnj 		if (um->um_tab.b_errcnt)
4423244Swnj 			cmd |= TS_RETRY;
4433244Swnj 		um->um_tab.b_active = SIO;
4443327Swnj 		tc->c_cmd = TS_ACK | TS_CVC | TS_IE | cmd;
4453244Swnj 		(void) ubago(ui);
4463244Swnj 		return;
4473244Swnj 	}
4483244Swnj 	/*
4493244Swnj 	 * Tape positioned incorrectly;
4503244Swnj 	 * set to seek forwards or backwards to the correct spot.
4513244Swnj 	 * This happens for raw tapes only on error retries.
4523244Swnj 	 */
4533244Swnj 	um->um_tab.b_active = SSEEK;
4547383Ssam 	if (blkno < bdbtofsb(bp->b_blkno)) {
4553244Swnj 		bp->b_command = TS_SFORW;
4567383Ssam 		tc->c_repcnt = bdbtofsb(bp->b_blkno) - blkno;
4571900Swnj 	} else {
4583244Swnj 		bp->b_command = TS_SREV;
4597383Ssam 		tc->c_repcnt = blkno - bdbtofsb(bp->b_blkno);
4601900Swnj 	}
4613244Swnj dobpcmd:
4623244Swnj 	/*
4633244Swnj 	 * Do the command in bp.
4643244Swnj 	 */
4653327Swnj 	tc->c_cmd = TS_ACK | TS_CVC | TS_IE | bp->b_command;
4663244Swnj 	addr->tsdb = sc->sc_uba;
4671900Swnj 	return;
4681900Swnj 
4693244Swnj next:
4703244Swnj 	/*
4713244Swnj 	 * Done with this operation due to error or
4723244Swnj 	 * the fact that it doesn't do anything.
4733244Swnj 	 * Release UBA resources (if any), dequeue
4743244Swnj 	 * the transfer and continue processing this slave.
4753244Swnj 	 */
4763244Swnj 	if (um->um_ubinfo)
4773244Swnj 		ubadone(um);
4783244Swnj 	um->um_tab.b_errcnt = 0;
4793327Swnj 	um->um_tab.b_actf->b_actf = bp->av_forw;
4801900Swnj 	iodone(bp);
4811900Swnj 	goto loop;
4821900Swnj }
4831900Swnj 
4843244Swnj /*
4853244Swnj  * The UNIBUS resources we needed have been
4863244Swnj  * allocated to us; start the device.
4873244Swnj  */
4883244Swnj tsdgo(um)
4893244Swnj 	register struct uba_ctlr *um;
4901900Swnj {
4915693Sroot 	register struct tsdevice *addr = (struct tsdevice *)um->um_addr;
4923244Swnj 	register struct ts_softc *sc = &ts_softc[um->um_ctlr];
4933327Swnj 	register int i;
4943244Swnj 
4953327Swnj 	i = um->um_ubinfo & 0777777;
4963327Swnj 	sc->sc_cmd.c_loba = i;
4973327Swnj 	sc->sc_cmd.c_hiba = (i>>16)&3;
4983244Swnj 	addr->tsdb = sc->sc_uba;
4993244Swnj }
5003244Swnj 
5013244Swnj /*
5023244Swnj  * Ts interrupt routine.
5033244Swnj  */
5043244Swnj /*ARGSUSED*/
5053244Swnj tsintr(ts11)
5063244Swnj 	int ts11;
5073244Swnj {
5081900Swnj 	register struct buf *bp;
5093244Swnj 	register struct uba_ctlr *um = tsminfo[ts11];
5105693Sroot 	register struct tsdevice *addr;
5113244Swnj 	register struct ts_softc *sc;
5123244Swnj 	int tsunit;
5133244Swnj 	register state;
5141900Swnj 
5153327Swnj 	if ((bp = um->um_tab.b_actf->b_actf) == NULL)
5161900Swnj 		return;
5173244Swnj 	tsunit = TSUNIT(bp->b_dev);
5185693Sroot 	addr = (struct tsdevice *)tsdinfo[tsunit]->ui_addr;
5193244Swnj 	/*
5203244Swnj 	 * If last command was a rewind, and tape is still
5213244Swnj 	 * rewinding, wait for the rewind complete interrupt.
5223244Swnj 	 *
5233244Swnj 	 * SHOULD NEVER GET AN INTERRUPT IN THIS STATE.
5243244Swnj 	 */
5253244Swnj 	if (um->um_tab.b_active == SREW) {
5263244Swnj 		um->um_tab.b_active = SCOM;
5273244Swnj 		if ((addr->tssr&TS_SSR) == 0)
5283244Swnj 			return;
5293244Swnj 	}
5303244Swnj 	/*
5313244Swnj 	 * An operation completed... record status
5323244Swnj 	 */
5333244Swnj 	sc = &ts_softc[tsunit];
5343244Swnj 	if ((bp->b_flags & B_READ) == 0)
5353244Swnj 		sc->sc_lastiow = 1;
5363244Swnj 	state = um->um_tab.b_active;
5373244Swnj 	um->um_tab.b_active = 0;
5383244Swnj 	/*
5393244Swnj 	 * Check for errors.
5403244Swnj 	 */
5413244Swnj 	if (addr->tssr&TS_SC) {
5423244Swnj 		switch (addr->tssr & TS_TC) {
5433244Swnj 		case TS_UNREC:		/* unrecoverable */
5443244Swnj 		case TS_FATAL:		/* fatal error */
5453244Swnj 		case TS_ATTN:		/* attention (shouldn't happen) */
5463244Swnj 		case TS_RECNM:		/* recoverable, no motion */
5473244Swnj 			break;
5481900Swnj 
5493244Swnj 		case TS_SUCC:		/* success termination */
5503244Swnj 			printf("ts%d: success\n", TSUNIT(minor(bp->b_dev)));
5513244Swnj 			goto ignoreerr;
5521900Swnj 
5533244Swnj 		case TS_ALERT:		/* tape status alert */
5543244Swnj 			/*
5553244Swnj 			 * If we hit the end of the tape file,
5563244Swnj 			 * update our position.
5573244Swnj 			 */
5583244Swnj 			if (sc->sc_sts.s_xs0 & (TS_TMK|TS_EOT)) {
5593244Swnj 				tsseteof(bp);		/* set blkno and nxrec */
5603244Swnj 				state = SCOM;		/* force completion */
5613244Swnj 				/*
5623244Swnj 				 * Stuff bc so it will be unstuffed correctly
5633244Swnj 				 * later to get resid.
5643244Swnj 				 */
5653244Swnj 				sc->sc_sts.s_rbpcr = bp->b_bcount;
5663244Swnj 				goto opdone;
5673244Swnj 			}
5683244Swnj 			/*
5693244Swnj 			 * If we were reading raw tape and the record was too long
5703244Swnj 			 * or too short, then we don't consider this an error.
5713244Swnj 			 */
5723244Swnj 			if (bp == &rtsbuf[TSUNIT(bp->b_dev)] && (bp->b_flags&B_READ) &&
5733244Swnj 			    sc->sc_sts.s_xs0&(TS_RLS|TS_RLL))
5743244Swnj 				goto ignoreerr;
5753244Swnj 		case TS_RECOV:		/* recoverable, tape moved */
5763244Swnj 			/*
5773244Swnj 			 * If this was an i/o operation retry up to 8 times.
5783244Swnj 			 */
5793244Swnj 			if (state==SIO) {
5803244Swnj 				if (++um->um_tab.b_errcnt < 7) {
5813244Swnj 					ubadone(um);
5823244Swnj 					goto opcont;
5833244Swnj 				} else
5843244Swnj 					sc->sc_blkno++;
5853244Swnj 			} else {
5863244Swnj 				/*
5873244Swnj 				 * Non-i/o errors on non-raw tape
5883244Swnj 				 * cause it to close.
5893244Swnj 				 */
5903244Swnj 				if (sc->sc_openf>0 && bp != &rtsbuf[TSUNIT(bp->b_dev)])
5913244Swnj 					sc->sc_openf = -1;
5923244Swnj 			}
5931900Swnj 			break;
5943244Swnj 
5953244Swnj 		case TS_REJECT:		/* function reject */
5963244Swnj 			if (state == SIO && sc->sc_sts.s_xs0 & TS_WLE)
5973244Swnj 				printf("ts%d: write locked\n", TSUNIT(bp->b_dev));
5983244Swnj 			if ((sc->sc_sts.s_xs0 & TS_ONL) == 0)
5993244Swnj 				printf("ts%d: offline\n", TSUNIT(bp->b_dev));
6001900Swnj 			break;
6011900Swnj 		}
6023244Swnj 		/*
6033244Swnj 		 * Couldn't recover error
6043244Swnj 		 */
6053647Swnj 		printf("ts%d: hard error bn%d xs0=%b", TSUNIT(bp->b_dev),
6063244Swnj 		    bp->b_blkno, sc->sc_sts.s_xs0, TSXS0_BITS);
6073647Swnj 		if (sc->sc_sts.s_xs1)
6083647Swnj 			printf(" xs1=%b", sc->sc_sts.s_xs1, TSXS1_BITS);
6093647Swnj 		if (sc->sc_sts.s_xs2)
6103647Swnj 			printf(" xs2=%b", sc->sc_sts.s_xs2, TSXS2_BITS);
6113647Swnj 		if (sc->sc_sts.s_xs3)
6123647Swnj 			printf(" xs3=%b", sc->sc_sts.s_xs3, TSXS3_BITS);
6133647Swnj 		printf("\n");
6143244Swnj 		bp->b_flags |= B_ERROR;
6153244Swnj 		goto opdone;
6163244Swnj 	}
6173244Swnj 	/*
6183244Swnj 	 * Advance tape control FSM.
6193244Swnj 	 */
6203244Swnj ignoreerr:
6213244Swnj 	switch (state) {
6221900Swnj 
6233244Swnj 	case SIO:
6243244Swnj 		/*
6253244Swnj 		 * Read/write increments tape block number
6263244Swnj 		 */
6273244Swnj 		sc->sc_blkno++;
6283244Swnj 		goto opdone;
6291900Swnj 
6301900Swnj 	case SCOM:
6313244Swnj 		/*
6323244Swnj 		 * For forward/backward space record update current position.
6333244Swnj 		 */
6343244Swnj 		if (bp == &ctsbuf[TSUNIT(bp->b_dev)])
6353244Swnj 		switch (bp->b_command) {
6361900Swnj 
6373244Swnj 		case TS_SFORW:
6383244Swnj 			sc->sc_blkno += bp->b_repcnt;
6393244Swnj 			break;
6401900Swnj 
6413244Swnj 		case TS_SREV:
6423244Swnj 			sc->sc_blkno -= bp->b_repcnt;
6433244Swnj 			break;
6443244Swnj 		}
6453244Swnj 		goto opdone;
6463244Swnj 
6473244Swnj 	case SSEEK:
6487383Ssam 		sc->sc_blkno = bdbtofsb(bp->b_blkno);
6493244Swnj 		goto opcont;
6503244Swnj 
6511900Swnj 	default:
6523244Swnj 		panic("tsintr");
6531900Swnj 	}
6543244Swnj opdone:
6553244Swnj 	/*
6563244Swnj 	 * Reset error count and remove
6573244Swnj 	 * from device queue.
6583244Swnj 	 */
6593244Swnj 	um->um_tab.b_errcnt = 0;
6603327Swnj 	um->um_tab.b_actf->b_actf = bp->av_forw;
6613244Swnj 	bp->b_resid = sc->sc_sts.s_rbpcr;
6623244Swnj 	ubadone(um);
6633244Swnj 	iodone(bp);
6643327Swnj 	if (um->um_tab.b_actf->b_actf == 0)
6653244Swnj 		return;
6663244Swnj opcont:
6673244Swnj 	tsstart(um);
6683244Swnj }
6693244Swnj 
6703244Swnj tsseteof(bp)
6713244Swnj 	register struct buf *bp;
6723244Swnj {
6733244Swnj 	register int tsunit = TSUNIT(bp->b_dev);
6743244Swnj 	register struct ts_softc *sc = &ts_softc[tsunit];
6753244Swnj 
6763244Swnj 	if (bp == &ctsbuf[TSUNIT(bp->b_dev)]) {
6777383Ssam 		if (sc->sc_blkno > bdbtofsb(bp->b_blkno)) {
6783244Swnj 			/* reversing */
6797383Ssam 			sc->sc_nxrec = bdbtofsb(bp->b_blkno) - sc->sc_sts.s_rbpcr;
6803244Swnj 			sc->sc_blkno = sc->sc_nxrec;
6813244Swnj 		} else {
6823244Swnj 			/* spacing forward */
6837383Ssam 			sc->sc_blkno = bdbtofsb(bp->b_blkno) + sc->sc_sts.s_rbpcr;
6843244Swnj 			sc->sc_nxrec = sc->sc_blkno - 1;
6851900Swnj 		}
6863244Swnj 		return;
6873244Swnj 	}
6883244Swnj 	/* eof on read */
6897383Ssam 	sc->sc_nxrec = bdbtofsb(bp->b_blkno);
6901900Swnj }
6911900Swnj 
6921900Swnj tsread(dev)
6933244Swnj 	dev_t dev;
6941900Swnj {
6953244Swnj 
6961900Swnj 	tsphys(dev);
6973244Swnj 	if (u.u_error)
6983244Swnj 		return;
6993244Swnj 	physio(tsstrategy, &rtsbuf[TSUNIT(dev)], dev, B_READ, minphys);
7001900Swnj }
7011900Swnj 
7021900Swnj tswrite(dev)
7033244Swnj 	dev_t dev;
7041900Swnj {
7053244Swnj 
7061900Swnj 	tsphys(dev);
7073244Swnj 	if (u.u_error)
7083244Swnj 		return;
7093244Swnj 	physio(tsstrategy, &rtsbuf[TSUNIT(dev)], dev, B_WRITE, minphys);
7101900Swnj }
7111900Swnj 
7123244Swnj /*
7133244Swnj  * Check that a raw device exists.
7143244Swnj  * If it does, set up sc_blkno and sc_nxrec
7153244Swnj  * so that the tape will appear positioned correctly.
7163244Swnj  */
7171900Swnj tsphys(dev)
7183244Swnj 	dev_t dev;
7191900Swnj {
7203244Swnj 	register int tsunit = TSUNIT(dev);
7213244Swnj 	register daddr_t a;
7223244Swnj 	register struct ts_softc *sc;
7233244Swnj 	register struct uba_device *ui;
7241900Swnj 
7253244Swnj 	if (tsunit >= NTS || (ui=tsdinfo[tsunit]) == 0 || ui->ui_alive == 0) {
7263244Swnj 		u.u_error = ENXIO;
7273244Swnj 		return;
7283244Swnj 	}
7293244Swnj 	sc = &ts_softc[tsunit];
7307383Ssam 	a = bdbtofsb(u.u_offset >> 9);
7313244Swnj 	sc->sc_blkno = a;
7323244Swnj 	sc->sc_nxrec = a + 1;
7331900Swnj }
7341918Swnj 
7353244Swnj tsreset(uban)
7363244Swnj 	int uban;
7371918Swnj {
7383244Swnj 	register struct uba_ctlr *um;
7395693Sroot 	register struct uba_device *ui;
7405693Sroot 	register struct buf *dp;
7413244Swnj 	register ts11;
7421918Swnj 
7433244Swnj 	for (ts11 = 0; ts11 < NTS; ts11++) {
7443244Swnj 		if ((um = tsminfo[ts11]) == 0 || um->um_alive == 0 ||
7453244Swnj 		   um->um_ubanum != uban)
7463244Swnj 			continue;
7473244Swnj 		printf(" ts%d", ts11);
7483244Swnj 		um->um_tab.b_active = 0;
7493244Swnj 		um->um_tab.b_actf = um->um_tab.b_actl = 0;
7505693Sroot 		if (ts_softc[ts11].sc_openf > 0)
7515693Sroot 			ts_softc[ts11].sc_openf = -1;
7523244Swnj 		if (um->um_ubinfo) {
7533244Swnj 			printf("<%d>", (um->um_ubinfo>>28)&0xf);
7543244Swnj 			ubadone(um);
7553244Swnj 		}
7565693Sroot 		if ((ui = tsdinfo[ts11]) && ui->ui_mi == um && ui->ui_alive) {
7575693Sroot 			dp = &tsutab[ts11];
7585693Sroot 			dp->b_active = 0;
7595693Sroot 			dp->b_forw = 0;
7605693Sroot 			if (um->um_tab.b_actf == NULL)
7615693Sroot 				um->um_tab.b_actf = dp;
7625693Sroot 			else
7635693Sroot 				um->um_tab.b_actl->b_forw = dp;
7645693Sroot 			um->um_tab.b_actl = dp;
7655693Sroot 		}
7663989Sroot 		(void) tsinit(ts11);
7673244Swnj 		tsstart(um);
7681918Swnj 	}
7691918Swnj }
7701918Swnj 
7713244Swnj /*ARGSUSED*/
7723244Swnj tsioctl(dev, cmd, addr, flag)
7733244Swnj 	caddr_t addr;
7743244Swnj 	dev_t dev;
7751918Swnj {
7763244Swnj 	int tsunit = TSUNIT(dev);
7773244Swnj 	register struct ts_softc *sc = &ts_softc[tsunit];
7783244Swnj 	register struct buf *bp = &ctsbuf[TSUNIT(dev)];
7793244Swnj 	register callcount;
7803244Swnj 	int fcount;
7813244Swnj 	struct mtop mtop;
7823244Swnj 	struct mtget mtget;
7833244Swnj 	/* we depend of the values and order of the MT codes here */
7843244Swnj 	static tsops[] =
7853656Swnj 	 {TS_WEOF,TS_SFORWF,TS_SREVF,TS_SFORW,TS_SREV,TS_REW,TS_OFFL,TS_SENSE};
7861918Swnj 
7873244Swnj 	switch (cmd) {
7883244Swnj 	case MTIOCTOP:	/* tape operation */
7893244Swnj 		if (copyin((caddr_t)addr, (caddr_t)&mtop, sizeof(mtop))) {
7903244Swnj 			u.u_error = EFAULT;
7913244Swnj 			return;
7923244Swnj 		}
7933244Swnj 		switch(mtop.mt_op) {
7943244Swnj 		case MTWEOF:
7953244Swnj 			callcount = mtop.mt_count;
7963244Swnj 			fcount = 1;
7973244Swnj 			break;
7983244Swnj 		case MTFSF: case MTBSF:
7993244Swnj 		case MTFSR: case MTBSR:
8003244Swnj 			callcount = 1;
8013244Swnj 			fcount = mtop.mt_count;
8023244Swnj 			break;
8033244Swnj 		case MTREW: case MTOFFL: case MTNOP:
8043244Swnj 			callcount = 1;
8053244Swnj 			fcount = 1;
8063244Swnj 			break;
8073244Swnj 		default:
8083244Swnj 			u.u_error = ENXIO;
8093244Swnj 			return;
8103244Swnj 		}
8113244Swnj 		if (callcount <= 0 || fcount <= 0) {
8123244Swnj 			u.u_error = ENXIO;
8133244Swnj 			return;
8143244Swnj 		}
8153244Swnj 		while (--callcount >= 0) {
8163244Swnj 			tscommand(dev, tsops[mtop.mt_op], fcount);
8173244Swnj 			if ((mtop.mt_op == MTFSR || mtop.mt_op == MTBSR) &&
8183244Swnj 			    bp->b_resid) {
8193244Swnj 				u.u_error = EIO;
8203244Swnj 				break;
8213244Swnj 			}
8223244Swnj 			if ((bp->b_flags&B_ERROR) || sc->sc_sts.s_xs0&TS_BOT)
8233244Swnj 				break;
8243244Swnj 		}
8253244Swnj 		geterror(bp);
8263244Swnj 		return;
8273244Swnj 	case MTIOCGET:
8283244Swnj 		mtget.mt_dsreg = 0;
8293244Swnj 		mtget.mt_erreg = sc->sc_sts.s_xs0;
8303244Swnj 		mtget.mt_resid = sc->sc_resid;
8313480Stoy 		mtget.mt_type = MT_ISTS;
8323244Swnj 		if (copyout((caddr_t)&mtget, addr, sizeof(mtget)))
8333244Swnj 			u.u_error = EFAULT;
8343244Swnj 		return;
8353244Swnj 	default:
8363244Swnj 		u.u_error = ENXIO;
8373244Swnj 	}
8381918Swnj }
8391918Swnj 
8403244Swnj #define	DBSIZE	20
8413244Swnj 
8423244Swnj tsdump()
8431918Swnj {
8443244Swnj 	register struct uba_device *ui;
8453244Swnj 	register struct uba_regs *up;
8465693Sroot 	register struct tsdevice *addr;
8473244Swnj 	int blk, num;
8483244Swnj 	int start;
8491918Swnj 
8503244Swnj 	start = 0;
8513244Swnj 	num = maxfree;
8523244Swnj #define	phys(a,b)	((b)((int)(a)&0x7fffffff))
8533244Swnj 	if (tsdinfo[0] == 0)
8543244Swnj 		return (ENXIO);
8553244Swnj 	ui = phys(tsdinfo[0], struct uba_device *);
8563244Swnj 	up = phys(ui->ui_hd, struct uba_hd *)->uh_physuba;
8573327Swnj 	ubainit(up);
8583244Swnj 	DELAY(1000000);
8595693Sroot 	addr = (struct tsdevice *)ui->ui_physaddr;
8603244Swnj 	addr->tssr = 0;
8613244Swnj 	tswait(addr);
8623244Swnj 	while (num > 0) {
8633244Swnj 		blk = num > DBSIZE ? DBSIZE : num;
8643244Swnj 		tsdwrite(start, blk, addr, up);
8653244Swnj 		start += blk;
8663244Swnj 		num -= blk;
8673244Swnj 	}
8683244Swnj 	tseof(addr);
8693244Swnj 	tseof(addr);
8703244Swnj 	tswait(addr);
8713244Swnj 	if (addr->tssr&TS_SC)
8723244Swnj 		return (EIO);
8733244Swnj 	addr->tssr = 0;
8743244Swnj 	tswait(addr);
8753244Swnj 	return (0);
8761918Swnj }
8771918Swnj 
8783244Swnj tsdwrite(dbuf, num, addr, up)
8793244Swnj 	register dbuf, num;
8805693Sroot 	register struct tsdevice *addr;
8813244Swnj 	struct uba_regs *up;
8821918Swnj {
8833244Swnj 	register struct pte *io;
8843244Swnj 	register int npf;
8851918Swnj 
8863244Swnj 	tswait(addr);
8873244Swnj 	io = up->uba_map;
8883244Swnj 	npf = num+1;
8893244Swnj 	while (--npf != 0)
8903244Swnj 		 *(int *)io++ = (dbuf++ | (1<<UBAMR_DPSHIFT) | UBAMR_MRV);
8913244Swnj 	*(int *)io = 0;
8923244Swnj #ifdef notyet
8933244Swnj 	addr->tsbc = -(num*NBPG);
8943244Swnj 	addr->tsba = 0;
8953244Swnj 	addr->tscs = TS_WCOM | TM_GO;
8963244Swnj #endif
8971918Swnj }
8981918Swnj 
8993244Swnj tswait(addr)
9005693Sroot 	register struct tsdevice *addr;
9011918Swnj {
9023244Swnj 	register s;
9031918Swnj 
9043244Swnj 	do
9053244Swnj 		s = addr->tssr;
9063244Swnj 	while ((s & TS_SSR) == 0);
9071918Swnj }
9081918Swnj 
9093244Swnj tseof(addr)
9105693Sroot 	struct tsdevice *addr;
9111918Swnj {
9121918Swnj 
9133244Swnj 	tswait(addr);
9143244Swnj #ifdef notyet
9153244Swnj 	addr->tscs = TS_WEOF | TM_GO;
9163244Swnj #endif
9171918Swnj }
9181900Swnj #endif
919