xref: /csrg-svn/sys/vax/uba/ts.c (revision 9356)
1*9356Ssam /*	ts.c	4.36	82/11/26	*/
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"
217633Ssam #include "../h/ioctl.h"
223244Swnj #include "../h/mtio.h"
233244Swnj #include "../h/cmap.h"
247733Sroot #include "../h/uio.h"
251900Swnj 
268480Sroot #include "../vax/cpu.h"
278480Sroot #include "../vaxuba/ubareg.h"
288480Sroot #include "../vaxuba/ubavar.h"
298480Sroot #include "../vaxuba/tsreg.h"
301900Swnj 
313244Swnj /*
323244Swnj  * There is a ctsbuf per tape controller.
333244Swnj  * It is used as the token to pass to the internal routines
343244Swnj  * to execute tape ioctls.
353244Swnj  * In particular, when the tape is rewinding on close we release
363244Swnj  * the user process but any further attempts to use the tape drive
373244Swnj  * before the rewind completes will hang waiting for ctsbuf.
383244Swnj  */
393244Swnj struct	buf	ctsbuf[NTS];
401900Swnj 
413244Swnj /*
423244Swnj  * Raw tape operations use rtsbuf.  The driver
433244Swnj  * notices when rtsbuf is being used and allows the user
443244Swnj  * program to continue after errors and read records
453244Swnj  * not of the standard length (BSIZE).
463244Swnj  */
473244Swnj struct	buf	rtsbuf[NTS];
481900Swnj 
493244Swnj /*
503244Swnj  * Driver unibus interface routines and variables.
513244Swnj  */
523244Swnj int	tsprobe(), tsslave(), tsattach(), tsdgo(), tsintr();
533244Swnj struct	uba_ctlr *tsminfo[NTS];
543244Swnj struct	uba_device *tsdinfo[NTS];
555693Sroot struct buf	tsutab[NTS];
563244Swnj u_short	tsstd[] = { 0772520, 0 };
573244Swnj /*** PROBABLY DON'T NEED ALL THESE SINCE CONTROLLER == DRIVE ***/
583244Swnj struct	uba_driver zsdriver =
593327Swnj  { tsprobe, tsslave, tsattach, tsdgo, tsstd, "ts", tsdinfo, "zs", tsminfo, 0 };
601900Swnj 
613244Swnj /* bits in minor device */
623244Swnj #define	TSUNIT(dev)	(minor(dev)&03)
633244Swnj #define	T_NOREWIND	04
641900Swnj 
653244Swnj #define	INF	(daddr_t)1000000L
661900Swnj 
673244Swnj /*
683244Swnj  * Software state per tape transport.
693244Swnj  * Also contains hardware state in message packets.
703244Swnj  *
713244Swnj  * 1. A tape drive is a unique-open device; we refuse opens when it is already.
723244Swnj  * 2. We keep track of the current position on a block tape and seek
733244Swnj  *    before operations by forward/back spacing if necessary.
743244Swnj  * 3. We remember if the last operation was a write on a tape, so if a tape
753244Swnj  *    is open read write and the last thing done is a write we can
763244Swnj  *    write a standard end of tape mark (two eofs).
773244Swnj  * 4. We remember the status registers after the last command, using
783244Swnj  *    then internally and returning them to the SENSE ioctl.
793244Swnj  */
803244Swnj struct	ts_softc {
813244Swnj 	char	sc_openf;	/* lock against multiple opens */
823244Swnj 	char	sc_lastiow;	/* last op was a write */
833327Swnj 	short	sc_resid;	/* copy of last bc */
843244Swnj 	daddr_t	sc_blkno;	/* block number, for block device tape */
853244Swnj 	daddr_t	sc_nxrec;	/* position of end of tape, if known */
865693Sroot 	struct	ts_cmd sc_cmd;	/* the command packet */
875693Sroot 	struct	ts_sts sc_sts;	/* status packet, for returned status */
885693Sroot 	struct	ts_char sc_char; /* characteristics packet */
895693Sroot 	struct	ts_softc *sc_ubaddr; /* Unibus address of ts_softc structure */
905697Sroot 	u_short	sc_uba;		/* Unibus addr of cmd pkt for tsdb */
915693Sroot 	short	sc_mapped;	/* is ts_sfotc mapped in Unibus space? */
923244Swnj } ts_softc[NTS];
931900Swnj 
943244Swnj /*
953244Swnj  * States for um->um_tab.b_active, the per controller state flag.
963244Swnj  * This is used to sequence control in the driver.
973244Swnj  */
983244Swnj #define	SSEEK	1		/* seeking */
993244Swnj #define	SIO	2		/* doing seq i/o */
1003244Swnj #define	SCOM	3		/* sending control command */
1013244Swnj #define	SREW	4		/* sending a drive rewind */
1021900Swnj 
1033244Swnj /*
1043244Swnj  * Determine if there is a controller for
1053244Swnj  * a ts at address reg.  Our goal is to make the
1063244Swnj  * device interrupt.
1073244Swnj  */
1083985Sroot /*ARGSUSED*/
1093244Swnj tsprobe(reg)
1103244Swnj 	caddr_t reg;
1113244Swnj {
1123244Swnj 	register int br, cvec;		/* must be r11,r10; value-result */
1131900Swnj 
1143244Swnj #ifdef lint
1153244Swnj 	br = 0; cvec = br; br = cvec;
1164937Swnj 	tsintr(0);
1173244Swnj #endif
1185693Sroot 	((struct tsdevice *)reg)->tssr = 0;
1195693Sroot 	DELAY(100);
1205693Sroot 	if ((((struct tsdevice *)reg)->tssr & TS_NBA) == 0)
1215693Sroot 		return(0);
1225693Sroot 	/* IT'S TOO HARD TO MAKE THIS THING INTERRUPT JUST TO FIND ITS VECTOR */
1235693Sroot 	cvec = ((unsigned)reg) & 07 ? 0260 : 0224;
1243244Swnj 	br = 0x15;
1257407Skre 	return (sizeof (struct tsdevice));
1263244Swnj }
1271900Swnj 
1283244Swnj /*
1293244Swnj  * TS11 only supports one drive per controller;
1303244Swnj  * check for ui_slave == 0.
1313244Swnj  *
1323244Swnj  * DO WE REALLY NEED THIS ROUTINE???
1333244Swnj  */
1343244Swnj /*ARGSUSED*/
1353244Swnj tsslave(ui, reg)
1363244Swnj 	struct uba_device *ui;
1373244Swnj 	caddr_t reg;
1383244Swnj {
1391900Swnj 
1403244Swnj 	if (ui->ui_slave)	/* non-zero slave not allowed */
1413244Swnj 		return(0);
1423244Swnj 	return (1);
1433244Swnj }
1441900Swnj 
1453244Swnj /*
1463244Swnj  * Record attachment of the unit to the controller.
1473244Swnj  *
1483244Swnj  * SHOULD THIS ROUTINE DO ANYTHING???
1493244Swnj  */
1503244Swnj /*ARGSUSED*/
1513244Swnj tsattach(ui)
1523244Swnj 	struct uba_device *ui;
1533244Swnj {
1541900Swnj 
1553244Swnj }
1561900Swnj 
1573244Swnj /*
1583244Swnj  * Open the device.  Tapes are unique open
1593244Swnj  * devices, so we refuse if it is already open.
1603244Swnj  * We also check that a tape is available, and
1613244Swnj  * don't block waiting here; if you want to wait
1623244Swnj  * for a tape you should timeout in user code.
1633244Swnj  */
1641900Swnj tsopen(dev, flag)
1653244Swnj 	dev_t dev;
1663244Swnj 	int flag;
1671900Swnj {
1683244Swnj 	register int tsunit;
1693244Swnj 	register struct uba_device *ui;
1703244Swnj 	register struct ts_softc *sc;
1711900Swnj 
1723244Swnj 	tsunit = TSUNIT(dev);
1733244Swnj 	if (tsunit>=NTS || (sc = &ts_softc[tsunit])->sc_openf ||
1748575Sroot 	    (ui = tsdinfo[tsunit]) == 0 || ui->ui_alive == 0)
1758575Sroot 		return (ENXIO);
1768575Sroot 	if (tsinit(tsunit))
1778575Sroot 		return (ENXIO);
1783244Swnj 	tscommand(dev, TS_SENSE, 1);
1793711Sroot 	if ((sc->sc_sts.s_xs0&TS_ONL) == 0) {
1803711Sroot 		uprintf("ts%d: not online\n", tsunit);
1818575Sroot 		return (EIO);
1821900Swnj 	}
1833718Sroot 	if ((flag&(FREAD|FWRITE)) == FWRITE && (sc->sc_sts.s_xs0&TS_WLK)) {
1843711Sroot 		uprintf("ts%d: no write ring\n", tsunit);
1858575Sroot 		return (EIO);
1863711Sroot 	}
1873244Swnj 	sc->sc_openf = 1;
1883244Swnj 	sc->sc_blkno = (daddr_t)0;
1893244Swnj 	sc->sc_nxrec = INF;
1903244Swnj 	sc->sc_lastiow = 0;
1918575Sroot 	return (0);
1921900Swnj }
1931900Swnj 
1943244Swnj /*
1953244Swnj  * Close tape device.
1963244Swnj  *
1973244Swnj  * If tape was open for writing or last operation was
1983244Swnj  * a write, then write two EOF's and backspace over the last one.
1993244Swnj  * Unless this is a non-rewinding special file, rewind the tape.
2003244Swnj  * Make the tape available to others.
2013244Swnj  */
2021900Swnj tsclose(dev, flag)
2033244Swnj 	register dev_t dev;
2043244Swnj 	register flag;
2051900Swnj {
2063244Swnj 	register struct ts_softc *sc = &ts_softc[TSUNIT(dev)];
2071900Swnj 
2083244Swnj 	if (flag == FWRITE || (flag&FWRITE) && sc->sc_lastiow) {
2093244Swnj 		tscommand(dev, TS_WEOF, 1);
2103244Swnj 		tscommand(dev, TS_WEOF, 1);
2113244Swnj 		tscommand(dev, TS_SREV, 1);
2121900Swnj 	}
2133244Swnj 	if ((minor(dev)&T_NOREWIND) == 0)
2143244Swnj 		/*
2153244Swnj 		 * 0 count means don't hang waiting for rewind complete
2163244Swnj 		 * rather ctsbuf stays busy until the operation completes
2173244Swnj 		 * preventing further opens from completing by
2183244Swnj 		 * preventing a TS_SENSE from completing.
2193244Swnj 		 */
2203244Swnj 		tscommand(dev, TS_REW, 0);
2213244Swnj 	sc->sc_openf = 0;
2221900Swnj }
2231900Swnj 
2243244Swnj /*
2253244Swnj  * Initialize the TS11.  Set up Unibus mapping for command
2263244Swnj  * packets and set device characteristics.
2273244Swnj  */
2283244Swnj tsinit(unit)
2293244Swnj 	register int unit;
2301900Swnj {
2313244Swnj 	register struct ts_softc *sc = &ts_softc[unit];
2323244Swnj 	register struct uba_ctlr *um = tsminfo[unit];
2335693Sroot 	register struct tsdevice *addr = (struct tsdevice *)um->um_addr;
2343244Swnj 	register int i;
2353244Swnj 
2363244Swnj 	/*
2373244Swnj 	 * Map the command and message packets into Unibus
2383244Swnj 	 * address space.  We do all the command and message
2393244Swnj 	 * packets at once to minimize the amount of Unibus
2403244Swnj 	 * mapping necessary.
2413244Swnj 	 */
2425693Sroot 	if (sc->sc_mapped == 0) {
2435693Sroot 		ctsbuf[unit].b_un.b_addr = (caddr_t)sc;
2445693Sroot 		ctsbuf[unit].b_bcount = sizeof(*sc);
2453244Swnj 		i = ubasetup(um->um_ubanum, &ctsbuf[unit], 0);
2463244Swnj 		i &= 0777777;
2475693Sroot 		sc->sc_ubaddr = (struct ts_softc *)i;
2485693Sroot 		sc->sc_mapped++;
2493244Swnj 	}
2503244Swnj 	/*
2513244Swnj 	 * Now initialize the TS11 controller.
2523244Swnj 	 * Set the characteristics.
2533244Swnj 	 */
2543668Swnj 	if (addr->tssr & (TS_NBA|TS_OFL)) {
2553244Swnj 		addr->tssr = 0;		/* subsystem initialize */
2563244Swnj 		tswait(addr);
2575693Sroot 		i = (int)&sc->sc_ubaddr->sc_cmd;	/* Unibus addr of cmd */
2583244Swnj 		sc->sc_uba = (u_short)(i + ((i>>16)&3));
2595693Sroot 		sc->sc_char.char_addr = (int)&sc->sc_ubaddr->sc_sts;
2603244Swnj 		sc->sc_char.char_size = sizeof(struct ts_sts);
2613244Swnj 		sc->sc_char.char_mode = TS_ESS;
2623244Swnj 		sc->sc_cmd.c_cmd = TS_ACK | TS_SETCHR;
2635693Sroot 		i = (int)&sc->sc_ubaddr->sc_char;
2643327Swnj 		sc->sc_cmd.c_loba = i;
2653327Swnj 		sc->sc_cmd.c_hiba = (i>>16)&3;
2663244Swnj 		sc->sc_cmd.c_size = sizeof(struct ts_char);
2673244Swnj 		addr->tsdb = sc->sc_uba;
2683244Swnj 		tswait(addr);
2693327Swnj 		if (addr->tssr & TS_NBA)
2703327Swnj 			return(1);
2713244Swnj 	}
2723244Swnj 	return(0);
2733244Swnj }
2743244Swnj 
2753244Swnj /*
2763244Swnj  * Execute a command on the tape drive
2773244Swnj  * a specified number of times.
2783244Swnj  */
2793244Swnj tscommand(dev, com, count)
2803244Swnj 	dev_t dev;
2813244Swnj 	int com, count;
2823244Swnj {
2831900Swnj 	register struct buf *bp;
2845438Sroot 	register int s;
2851900Swnj 
2863244Swnj 	bp = &ctsbuf[TSUNIT(dev)];
2875438Sroot 	s = spl5();
2883244Swnj 	while (bp->b_flags&B_BUSY) {
2893244Swnj 		/*
2903244Swnj 		 * This special check is because B_BUSY never
2913244Swnj 		 * gets cleared in the non-waiting rewind case.
2923244Swnj 		 */
2933244Swnj 		if (bp->b_repcnt == 0 && (bp->b_flags&B_DONE))
2943244Swnj 			break;
2951900Swnj 		bp->b_flags |= B_WANTED;
2961900Swnj 		sleep((caddr_t)bp, PRIBIO);
2971900Swnj 	}
2983244Swnj 	bp->b_flags = B_BUSY|B_READ;
2995438Sroot 	splx(s);
3003244Swnj 	bp->b_dev = dev;
3013244Swnj 	bp->b_repcnt = count;
3023244Swnj 	bp->b_command = com;
3031900Swnj 	bp->b_blkno = 0;
3041900Swnj 	tsstrategy(bp);
3053244Swnj 	/*
3063244Swnj 	 * In case of rewind from close, don't wait.
3073244Swnj 	 * This is the only case where count can be 0.
3083244Swnj 	 */
3093244Swnj 	if (count == 0)
3103244Swnj 		return;
3111900Swnj 	iowait(bp);
3123244Swnj 	if (bp->b_flags&B_WANTED)
3131900Swnj 		wakeup((caddr_t)bp);
3143244Swnj 	bp->b_flags &= B_ERROR;
3151900Swnj }
3161900Swnj 
3173244Swnj /*
3183244Swnj  * Queue a tape operation.
3193244Swnj  */
3201900Swnj tsstrategy(bp)
3213244Swnj 	register struct buf *bp;
3221900Swnj {
3233244Swnj 	int tsunit = TSUNIT(bp->b_dev);
3243244Swnj 	register struct uba_ctlr *um;
3253327Swnj 	register struct buf *dp;
3265438Sroot 	register int s;
3271900Swnj 
3283244Swnj 	/*
3293244Swnj 	 * Put transfer at end of controller queue
3303244Swnj 	 */
3311900Swnj 	bp->av_forw = NULL;
3323244Swnj 	um = tsdinfo[tsunit]->ui_mi;
3335438Sroot 	s = spl5();
3345693Sroot 	dp = &tsutab[tsunit];
3353327Swnj 	if (dp->b_actf == NULL)
3363327Swnj 		dp->b_actf = bp;
3371900Swnj 	else
3383327Swnj 		dp->b_actl->av_forw = bp;
3393327Swnj 	dp->b_actl = bp;
3403327Swnj 	um->um_tab.b_actf = um->um_tab.b_actl = dp;
3413244Swnj 	/*
3423244Swnj 	 * If the controller is not busy, get
3433244Swnj 	 * it going.
3443244Swnj 	 */
3453244Swnj 	if (um->um_tab.b_active == 0)
3463244Swnj 		tsstart(um);
3475438Sroot 	splx(s);
3481900Swnj }
3491900Swnj 
3503244Swnj /*
3513244Swnj  * Start activity on a ts controller.
3523244Swnj  */
3533244Swnj tsstart(um)
3543244Swnj 	register struct uba_ctlr *um;
3551900Swnj {
3561900Swnj 	register struct buf *bp;
3575693Sroot 	register struct tsdevice *addr = (struct tsdevice *)um->um_addr;
3583244Swnj 	register struct ts_softc *sc;
3593244Swnj 	register struct ts_cmd *tc;
3603244Swnj 	register struct uba_device *ui;
3613244Swnj 	int tsunit, cmd;
3621900Swnj 	daddr_t blkno;
3631900Swnj 
3643244Swnj 	/*
3653244Swnj 	 * Start the controller if there is something for it to do.
3663244Swnj 	 */
3673244Swnj loop:
3683327Swnj 	if ((bp = um->um_tab.b_actf->b_actf) == NULL)
3691900Swnj 		return;
3703244Swnj 	tsunit = TSUNIT(bp->b_dev);
3713244Swnj 	ui = tsdinfo[tsunit];
3723244Swnj 	sc = &ts_softc[tsunit];
3733244Swnj 	tc = &sc->sc_cmd;
3743244Swnj 	/*
3753244Swnj 	 * Default is that last command was NOT a write command;
3763244Swnj 	 * if we do a write command we will notice this in tsintr().
3773244Swnj 	 */
3783656Swnj 	sc->sc_lastiow = 0;
3793244Swnj 	if (sc->sc_openf < 0 || (addr->tssr&TS_OFL)) {
3803244Swnj 		/*
3813244Swnj 		 * Have had a hard error on a non-raw tape
3823244Swnj 		 * or the tape unit is now unavailable
3833244Swnj 		 * (e.g. taken off line).
3843244Swnj 		 */
3853244Swnj 		bp->b_flags |= B_ERROR;
3863244Swnj 		goto next;
3873244Swnj 	}
3883244Swnj 	if (bp == &ctsbuf[TSUNIT(bp->b_dev)]) {
3893244Swnj 		/*
3903244Swnj 		 * Execute control operation with the specified count.
3913244Swnj 		 */
3923244Swnj 		um->um_tab.b_active =
3933244Swnj 		    bp->b_command == TS_REW ? SREW : SCOM;
3943244Swnj 		tc->c_repcnt = bp->b_repcnt;
3953244Swnj 		goto dobpcmd;
3963244Swnj 	}
3973244Swnj 	/*
3983244Swnj 	 * The following checks handle boundary cases for operation
3993244Swnj 	 * on non-raw tapes.  On raw tapes the initialization of
4003244Swnj 	 * sc->sc_nxrec by tsphys causes them to be skipped normally
4013244Swnj 	 * (except in the case of retries).
4023244Swnj 	 */
4037383Ssam 	if (bdbtofsb(bp->b_blkno) > sc->sc_nxrec) {
4043244Swnj 		/*
4053244Swnj 		 * Can't read past known end-of-file.
4063244Swnj 		 */
4073244Swnj 		bp->b_flags |= B_ERROR;
4083244Swnj 		bp->b_error = ENXIO;
4093244Swnj 		goto next;
4103244Swnj 	}
4117383Ssam 	if (bdbtofsb(bp->b_blkno) == sc->sc_nxrec &&
4123244Swnj 	    bp->b_flags&B_READ) {
4133244Swnj 		/*
4143244Swnj 		 * Reading at end of file returns 0 bytes.
4153244Swnj 		 */
4163244Swnj 		bp->b_resid = bp->b_bcount;
4173244Swnj 		clrbuf(bp);
4183244Swnj 		goto next;
4193244Swnj 	}
4203244Swnj 	if ((bp->b_flags&B_READ) == 0)
4213244Swnj 		/*
4223244Swnj 		 * Writing sets EOF
4233244Swnj 		 */
4247383Ssam 		sc->sc_nxrec = bdbtofsb(bp->b_blkno) + 1;
4253244Swnj 	/*
4263244Swnj 	 * If the data transfer command is in the correct place,
4273244Swnj 	 * set up all the registers except the csr, and give
4283244Swnj 	 * control over to the UNIBUS adapter routines, to
4293244Swnj 	 * wait for resources to start the i/o.
4303244Swnj 	 */
4317383Ssam 	if ((blkno = sc->sc_blkno) == bdbtofsb(bp->b_blkno)) {
4323244Swnj 		tc->c_size = bp->b_bcount;
4333244Swnj 		if ((bp->b_flags&B_READ) == 0)
4343244Swnj 			cmd = TS_WCOM;
4351900Swnj 		else
4363244Swnj 			cmd = TS_RCOM;
4373244Swnj 		if (um->um_tab.b_errcnt)
4383244Swnj 			cmd |= TS_RETRY;
4393244Swnj 		um->um_tab.b_active = SIO;
4403327Swnj 		tc->c_cmd = TS_ACK | TS_CVC | TS_IE | cmd;
4413244Swnj 		(void) ubago(ui);
4423244Swnj 		return;
4433244Swnj 	}
4443244Swnj 	/*
4453244Swnj 	 * Tape positioned incorrectly;
4463244Swnj 	 * set to seek forwards or backwards to the correct spot.
4473244Swnj 	 * This happens for raw tapes only on error retries.
4483244Swnj 	 */
4493244Swnj 	um->um_tab.b_active = SSEEK;
4507383Ssam 	if (blkno < bdbtofsb(bp->b_blkno)) {
4513244Swnj 		bp->b_command = TS_SFORW;
4527383Ssam 		tc->c_repcnt = bdbtofsb(bp->b_blkno) - blkno;
4531900Swnj 	} else {
4543244Swnj 		bp->b_command = TS_SREV;
4557383Ssam 		tc->c_repcnt = blkno - bdbtofsb(bp->b_blkno);
4561900Swnj 	}
4573244Swnj dobpcmd:
4583244Swnj 	/*
4593244Swnj 	 * Do the command in bp.
4603244Swnj 	 */
4613327Swnj 	tc->c_cmd = TS_ACK | TS_CVC | TS_IE | bp->b_command;
4623244Swnj 	addr->tsdb = sc->sc_uba;
4631900Swnj 	return;
4641900Swnj 
4653244Swnj next:
4663244Swnj 	/*
4673244Swnj 	 * Done with this operation due to error or
4683244Swnj 	 * the fact that it doesn't do anything.
4693244Swnj 	 * Release UBA resources (if any), dequeue
4703244Swnj 	 * the transfer and continue processing this slave.
4713244Swnj 	 */
4723244Swnj 	if (um->um_ubinfo)
4733244Swnj 		ubadone(um);
4743244Swnj 	um->um_tab.b_errcnt = 0;
4753327Swnj 	um->um_tab.b_actf->b_actf = bp->av_forw;
4761900Swnj 	iodone(bp);
4771900Swnj 	goto loop;
4781900Swnj }
4791900Swnj 
4803244Swnj /*
4813244Swnj  * The UNIBUS resources we needed have been
4823244Swnj  * allocated to us; start the device.
4833244Swnj  */
4843244Swnj tsdgo(um)
4853244Swnj 	register struct uba_ctlr *um;
4861900Swnj {
4875693Sroot 	register struct tsdevice *addr = (struct tsdevice *)um->um_addr;
4883244Swnj 	register struct ts_softc *sc = &ts_softc[um->um_ctlr];
4893327Swnj 	register int i;
4903244Swnj 
4913327Swnj 	i = um->um_ubinfo & 0777777;
4923327Swnj 	sc->sc_cmd.c_loba = i;
4933327Swnj 	sc->sc_cmd.c_hiba = (i>>16)&3;
4943244Swnj 	addr->tsdb = sc->sc_uba;
4953244Swnj }
4963244Swnj 
4973244Swnj /*
4983244Swnj  * Ts interrupt routine.
4993244Swnj  */
5003244Swnj /*ARGSUSED*/
5013244Swnj tsintr(ts11)
5023244Swnj 	int ts11;
5033244Swnj {
5041900Swnj 	register struct buf *bp;
5053244Swnj 	register struct uba_ctlr *um = tsminfo[ts11];
5065693Sroot 	register struct tsdevice *addr;
5073244Swnj 	register struct ts_softc *sc;
5083244Swnj 	int tsunit;
5093244Swnj 	register state;
5101900Swnj 
5113327Swnj 	if ((bp = um->um_tab.b_actf->b_actf) == NULL)
5121900Swnj 		return;
5133244Swnj 	tsunit = TSUNIT(bp->b_dev);
5145693Sroot 	addr = (struct tsdevice *)tsdinfo[tsunit]->ui_addr;
5153244Swnj 	/*
5163244Swnj 	 * If last command was a rewind, and tape is still
5173244Swnj 	 * rewinding, wait for the rewind complete interrupt.
5183244Swnj 	 *
5193244Swnj 	 * SHOULD NEVER GET AN INTERRUPT IN THIS STATE.
5203244Swnj 	 */
5213244Swnj 	if (um->um_tab.b_active == SREW) {
5223244Swnj 		um->um_tab.b_active = SCOM;
5233244Swnj 		if ((addr->tssr&TS_SSR) == 0)
5243244Swnj 			return;
5253244Swnj 	}
5263244Swnj 	/*
5273244Swnj 	 * An operation completed... record status
5283244Swnj 	 */
5293244Swnj 	sc = &ts_softc[tsunit];
5303244Swnj 	if ((bp->b_flags & B_READ) == 0)
5313244Swnj 		sc->sc_lastiow = 1;
5323244Swnj 	state = um->um_tab.b_active;
5333244Swnj 	um->um_tab.b_active = 0;
5343244Swnj 	/*
5353244Swnj 	 * Check for errors.
5363244Swnj 	 */
5373244Swnj 	if (addr->tssr&TS_SC) {
5383244Swnj 		switch (addr->tssr & TS_TC) {
5393244Swnj 		case TS_UNREC:		/* unrecoverable */
5403244Swnj 		case TS_FATAL:		/* fatal error */
5413244Swnj 		case TS_ATTN:		/* attention (shouldn't happen) */
5423244Swnj 		case TS_RECNM:		/* recoverable, no motion */
5433244Swnj 			break;
5441900Swnj 
5453244Swnj 		case TS_SUCC:		/* success termination */
5463244Swnj 			printf("ts%d: success\n", TSUNIT(minor(bp->b_dev)));
5473244Swnj 			goto ignoreerr;
5481900Swnj 
5493244Swnj 		case TS_ALERT:		/* tape status alert */
5503244Swnj 			/*
5513244Swnj 			 * If we hit the end of the tape file,
5523244Swnj 			 * update our position.
5533244Swnj 			 */
5543244Swnj 			if (sc->sc_sts.s_xs0 & (TS_TMK|TS_EOT)) {
5553244Swnj 				tsseteof(bp);		/* set blkno and nxrec */
5563244Swnj 				state = SCOM;		/* force completion */
5573244Swnj 				/*
5583244Swnj 				 * Stuff bc so it will be unstuffed correctly
5593244Swnj 				 * later to get resid.
5603244Swnj 				 */
5613244Swnj 				sc->sc_sts.s_rbpcr = bp->b_bcount;
5623244Swnj 				goto opdone;
5633244Swnj 			}
5643244Swnj 			/*
5653244Swnj 			 * If we were reading raw tape and the record was too long
5663244Swnj 			 * or too short, then we don't consider this an error.
5673244Swnj 			 */
5683244Swnj 			if (bp == &rtsbuf[TSUNIT(bp->b_dev)] && (bp->b_flags&B_READ) &&
5693244Swnj 			    sc->sc_sts.s_xs0&(TS_RLS|TS_RLL))
5703244Swnj 				goto ignoreerr;
5713244Swnj 		case TS_RECOV:		/* recoverable, tape moved */
5723244Swnj 			/*
5733244Swnj 			 * If this was an i/o operation retry up to 8 times.
5743244Swnj 			 */
5753244Swnj 			if (state==SIO) {
5763244Swnj 				if (++um->um_tab.b_errcnt < 7) {
5773244Swnj 					ubadone(um);
5783244Swnj 					goto opcont;
5793244Swnj 				} else
5803244Swnj 					sc->sc_blkno++;
5813244Swnj 			} else {
5823244Swnj 				/*
5833244Swnj 				 * Non-i/o errors on non-raw tape
5843244Swnj 				 * cause it to close.
5853244Swnj 				 */
5863244Swnj 				if (sc->sc_openf>0 && bp != &rtsbuf[TSUNIT(bp->b_dev)])
5873244Swnj 					sc->sc_openf = -1;
5883244Swnj 			}
5891900Swnj 			break;
5903244Swnj 
5913244Swnj 		case TS_REJECT:		/* function reject */
5923244Swnj 			if (state == SIO && sc->sc_sts.s_xs0 & TS_WLE)
5933244Swnj 				printf("ts%d: write locked\n", TSUNIT(bp->b_dev));
5943244Swnj 			if ((sc->sc_sts.s_xs0 & TS_ONL) == 0)
5953244Swnj 				printf("ts%d: offline\n", TSUNIT(bp->b_dev));
5961900Swnj 			break;
5971900Swnj 		}
5983244Swnj 		/*
5993244Swnj 		 * Couldn't recover error
6003244Swnj 		 */
6013647Swnj 		printf("ts%d: hard error bn%d xs0=%b", TSUNIT(bp->b_dev),
6023244Swnj 		    bp->b_blkno, sc->sc_sts.s_xs0, TSXS0_BITS);
6033647Swnj 		if (sc->sc_sts.s_xs1)
6043647Swnj 			printf(" xs1=%b", sc->sc_sts.s_xs1, TSXS1_BITS);
6053647Swnj 		if (sc->sc_sts.s_xs2)
6063647Swnj 			printf(" xs2=%b", sc->sc_sts.s_xs2, TSXS2_BITS);
6073647Swnj 		if (sc->sc_sts.s_xs3)
6083647Swnj 			printf(" xs3=%b", sc->sc_sts.s_xs3, TSXS3_BITS);
6093647Swnj 		printf("\n");
6103244Swnj 		bp->b_flags |= B_ERROR;
6113244Swnj 		goto opdone;
6123244Swnj 	}
6133244Swnj 	/*
6143244Swnj 	 * Advance tape control FSM.
6153244Swnj 	 */
6163244Swnj ignoreerr:
6173244Swnj 	switch (state) {
6181900Swnj 
6193244Swnj 	case SIO:
6203244Swnj 		/*
6213244Swnj 		 * Read/write increments tape block number
6223244Swnj 		 */
6233244Swnj 		sc->sc_blkno++;
6243244Swnj 		goto opdone;
6251900Swnj 
6261900Swnj 	case SCOM:
6273244Swnj 		/*
6283244Swnj 		 * For forward/backward space record update current position.
6293244Swnj 		 */
6303244Swnj 		if (bp == &ctsbuf[TSUNIT(bp->b_dev)])
6313244Swnj 		switch (bp->b_command) {
6321900Swnj 
6333244Swnj 		case TS_SFORW:
6343244Swnj 			sc->sc_blkno += bp->b_repcnt;
6353244Swnj 			break;
6361900Swnj 
6373244Swnj 		case TS_SREV:
6383244Swnj 			sc->sc_blkno -= bp->b_repcnt;
6393244Swnj 			break;
6403244Swnj 		}
6413244Swnj 		goto opdone;
6423244Swnj 
6433244Swnj 	case SSEEK:
6447383Ssam 		sc->sc_blkno = bdbtofsb(bp->b_blkno);
6453244Swnj 		goto opcont;
6463244Swnj 
6471900Swnj 	default:
6483244Swnj 		panic("tsintr");
6491900Swnj 	}
6503244Swnj opdone:
6513244Swnj 	/*
6523244Swnj 	 * Reset error count and remove
6533244Swnj 	 * from device queue.
6543244Swnj 	 */
6553244Swnj 	um->um_tab.b_errcnt = 0;
6563327Swnj 	um->um_tab.b_actf->b_actf = bp->av_forw;
6573244Swnj 	bp->b_resid = sc->sc_sts.s_rbpcr;
6583244Swnj 	ubadone(um);
6593244Swnj 	iodone(bp);
6603327Swnj 	if (um->um_tab.b_actf->b_actf == 0)
6613244Swnj 		return;
6623244Swnj opcont:
6633244Swnj 	tsstart(um);
6643244Swnj }
6653244Swnj 
6663244Swnj tsseteof(bp)
6673244Swnj 	register struct buf *bp;
6683244Swnj {
6693244Swnj 	register int tsunit = TSUNIT(bp->b_dev);
6703244Swnj 	register struct ts_softc *sc = &ts_softc[tsunit];
6713244Swnj 
6723244Swnj 	if (bp == &ctsbuf[TSUNIT(bp->b_dev)]) {
6737383Ssam 		if (sc->sc_blkno > bdbtofsb(bp->b_blkno)) {
6743244Swnj 			/* reversing */
6757383Ssam 			sc->sc_nxrec = bdbtofsb(bp->b_blkno) - sc->sc_sts.s_rbpcr;
6763244Swnj 			sc->sc_blkno = sc->sc_nxrec;
6773244Swnj 		} else {
6783244Swnj 			/* spacing forward */
6797383Ssam 			sc->sc_blkno = bdbtofsb(bp->b_blkno) + sc->sc_sts.s_rbpcr;
6803244Swnj 			sc->sc_nxrec = sc->sc_blkno - 1;
6811900Swnj 		}
6823244Swnj 		return;
6833244Swnj 	}
6843244Swnj 	/* eof on read */
6857383Ssam 	sc->sc_nxrec = bdbtofsb(bp->b_blkno);
6861900Swnj }
6871900Swnj 
6887733Sroot tsread(dev, uio)
6893244Swnj 	dev_t dev;
6907733Sroot 	struct uio *uio;
6911900Swnj {
6928164Sroot 	int errno;
6933244Swnj 
6948164Sroot 	errno = tsphys(dev, uio);
6958164Sroot 	if (errno)
6968164Sroot 		return (errno);
6978164Sroot 	return (physio(tsstrategy, &rtsbuf[TSUNIT(dev)], dev, B_READ, minphys, uio));
6981900Swnj }
6991900Swnj 
7007841Sroot tswrite(dev, uio)
7013244Swnj 	dev_t dev;
7027841Sroot 	struct uio *uio;
7031900Swnj {
7048164Sroot 	int errno;
7053244Swnj 
7068164Sroot 	errno = tsphys(dev, uio);
7078164Sroot 	if (errno)
7088164Sroot 		return (errno);
7098164Sroot 	return (physio(tsstrategy, &rtsbuf[TSUNIT(dev)], dev, B_WRITE, minphys, uio));
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  */
7177733Sroot tsphys(dev, uio)
7183244Swnj 	dev_t dev;
7197733Sroot 	struct uio *uio;
7201900Swnj {
7213244Swnj 	register int tsunit = TSUNIT(dev);
7223244Swnj 	register daddr_t a;
7233244Swnj 	register struct ts_softc *sc;
7243244Swnj 	register struct uba_device *ui;
7251900Swnj 
7267841Sroot 	if (tsunit >= NTS || (ui=tsdinfo[tsunit]) == 0 || ui->ui_alive == 0)
7277733Sroot 		return (ENXIO);
7283244Swnj 	sc = &ts_softc[tsunit];
7297841Sroot 	a = bdbtofsb(uio->uio_offset >> 9);
7303244Swnj 	sc->sc_blkno = a;
7313244Swnj 	sc->sc_nxrec = a + 1;
7327733Sroot 	return (0);
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);
754*9356Ssam 			um->um_ubinfo = 0;
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*/
7727633Ssam tsioctl(dev, cmd, data, flag)
7737633Ssam 	caddr_t data;
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;
7817633Ssam 	struct mtop *mtop;
7827633Ssam 	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) {
7887633Ssam 
7893244Swnj 	case MTIOCTOP:	/* tape operation */
7907633Ssam 		mtop = (struct mtop *)data;
7918575Sroot 		switch (mtop->mt_op) {
7927633Ssam 
7933244Swnj 		case MTWEOF:
7947633Ssam 			callcount = mtop->mt_count;
7953244Swnj 			fcount = 1;
7963244Swnj 			break;
7977633Ssam 
7983244Swnj 		case MTFSF: case MTBSF:
7993244Swnj 		case MTFSR: case MTBSR:
8003244Swnj 			callcount = 1;
8017633Ssam 			fcount = mtop->mt_count;
8023244Swnj 			break;
8037633Ssam 
8043244Swnj 		case MTREW: case MTOFFL: case MTNOP:
8053244Swnj 			callcount = 1;
8063244Swnj 			fcount = 1;
8073244Swnj 			break;
8087633Ssam 
8093244Swnj 		default:
8108575Sroot 			return (ENXIO);
8113244Swnj 		}
8128575Sroot 		if (callcount <= 0 || fcount <= 0)
8138575Sroot 			return (EINVAL);
8143244Swnj 		while (--callcount >= 0) {
8157633Ssam 			tscommand(dev, tsops[mtop->mt_op], fcount);
8167633Ssam 			if ((mtop->mt_op == MTFSR || mtop->mt_op == MTBSR) &&
8178611Sroot 			    bp->b_resid)
8188575Sroot 				return (EIO);
8193244Swnj 			if ((bp->b_flags&B_ERROR) || sc->sc_sts.s_xs0&TS_BOT)
8203244Swnj 				break;
8213244Swnj 		}
8228649Sroot 		return (geterror(bp));
8237633Ssam 
8243244Swnj 	case MTIOCGET:
8257633Ssam 		mtget = (struct mtget *)data;
8267633Ssam 		mtget->mt_dsreg = 0;
8277633Ssam 		mtget->mt_erreg = sc->sc_sts.s_xs0;
8287633Ssam 		mtget->mt_resid = sc->sc_resid;
8297633Ssam 		mtget->mt_type = MT_ISTS;
8308575Sroot 		break;
8317633Ssam 
8323244Swnj 	default:
8338575Sroot 		return (ENXIO);
8343244Swnj 	}
8358575Sroot 	return (0);
8361918Swnj }
8371918Swnj 
8383244Swnj #define	DBSIZE	20
8393244Swnj 
8403244Swnj tsdump()
8411918Swnj {
8423244Swnj 	register struct uba_device *ui;
8433244Swnj 	register struct uba_regs *up;
8445693Sroot 	register struct tsdevice *addr;
8453244Swnj 	int blk, num;
8463244Swnj 	int start;
8471918Swnj 
8483244Swnj 	start = 0;
8493244Swnj 	num = maxfree;
8503244Swnj #define	phys(a,b)	((b)((int)(a)&0x7fffffff))
8513244Swnj 	if (tsdinfo[0] == 0)
8523244Swnj 		return (ENXIO);
8533244Swnj 	ui = phys(tsdinfo[0], struct uba_device *);
8543244Swnj 	up = phys(ui->ui_hd, struct uba_hd *)->uh_physuba;
8558703Sroot 	ubainit(up);
8563244Swnj 	DELAY(1000000);
8575693Sroot 	addr = (struct tsdevice *)ui->ui_physaddr;
8583244Swnj 	addr->tssr = 0;
8593244Swnj 	tswait(addr);
8603244Swnj 	while (num > 0) {
8613244Swnj 		blk = num > DBSIZE ? DBSIZE : num;
8623244Swnj 		tsdwrite(start, blk, addr, up);
8633244Swnj 		start += blk;
8643244Swnj 		num -= blk;
8653244Swnj 	}
8663244Swnj 	tseof(addr);
8673244Swnj 	tseof(addr);
8683244Swnj 	tswait(addr);
8693244Swnj 	if (addr->tssr&TS_SC)
8703244Swnj 		return (EIO);
8713244Swnj 	addr->tssr = 0;
8723244Swnj 	tswait(addr);
8733244Swnj 	return (0);
8741918Swnj }
8751918Swnj 
8763244Swnj tsdwrite(dbuf, num, addr, up)
8778635Sroot 	register int dbuf, num;
8785693Sroot 	register struct tsdevice *addr;
8793244Swnj 	struct uba_regs *up;
8801918Swnj {
8813244Swnj 	register struct pte *io;
8823244Swnj 	register int npf;
8831918Swnj 
8843244Swnj 	tswait(addr);
8853244Swnj 	io = up->uba_map;
8863244Swnj 	npf = num+1;
8873244Swnj 	while (--npf != 0)
8883244Swnj 		 *(int *)io++ = (dbuf++ | (1<<UBAMR_DPSHIFT) | UBAMR_MRV);
8893244Swnj 	*(int *)io = 0;
8903244Swnj #ifdef notyet
8913244Swnj 	addr->tsbc = -(num*NBPG);
8923244Swnj 	addr->tsba = 0;
8933244Swnj 	addr->tscs = TS_WCOM | TM_GO;
8943244Swnj #endif
8951918Swnj }
8961918Swnj 
8973244Swnj tswait(addr)
8985693Sroot 	register struct tsdevice *addr;
8991918Swnj {
9003244Swnj 	register s;
9011918Swnj 
9023244Swnj 	do
9033244Swnj 		s = addr->tssr;
9043244Swnj 	while ((s & TS_SSR) == 0);
9051918Swnj }
9061918Swnj 
9073244Swnj tseof(addr)
9085693Sroot 	struct tsdevice *addr;
9091918Swnj {
9101918Swnj 
9113244Swnj 	tswait(addr);
9123244Swnj #ifdef notyet
9133244Swnj 	addr->tscs = TS_WEOF | TM_GO;
9143244Swnj #endif
9151918Swnj }
9161900Swnj #endif
917