xref: /csrg-svn/sys/vax/uba/ts.c (revision 25054)
123349Smckusick /*
223349Smckusick  * Copyright (c) 1982 Regents of the University of California.
323349Smckusick  * All rights reserved.  The Berkeley software License Agreement
423349Smckusick  * specifies the terms and conditions for redistribution.
523349Smckusick  *
6*25054Skarels  *	@(#)ts.c	6.7 (Berkeley) 10/01/85
723349Smckusick  */
81900Swnj 
91941Swnj #include "ts.h"
101900Swnj #if NTS > 0
111900Swnj /*
121900Swnj  * TS11 tape driver
133244Swnj  *
143244Swnj  * TODO:
155693Sroot  *	write dump code
161900Swnj  */
179779Ssam #include "../machine/pte.h"
189779Ssam 
1917080Sbloom #include "param.h"
2017080Sbloom #include "systm.h"
2117080Sbloom #include "buf.h"
2217080Sbloom #include "dir.h"
2317080Sbloom #include "conf.h"
2417080Sbloom #include "user.h"
2517080Sbloom #include "file.h"
2617080Sbloom #include "map.h"
2717080Sbloom #include "vm.h"
2817080Sbloom #include "ioctl.h"
2917080Sbloom #include "mtio.h"
3017080Sbloom #include "cmap.h"
3117080Sbloom #include "uio.h"
3218322Sralph #include "tty.h"
331900Swnj 
348480Sroot #include "../vax/cpu.h"
3517080Sbloom #include "ubareg.h"
3617080Sbloom #include "ubavar.h"
3717080Sbloom #include "tsreg.h"
381900Swnj 
393244Swnj /*
403244Swnj  * There is a ctsbuf per tape controller.
413244Swnj  * It is used as the token to pass to the internal routines
423244Swnj  * to execute tape ioctls.
433244Swnj  * In particular, when the tape is rewinding on close we release
443244Swnj  * the user process but any further attempts to use the tape drive
453244Swnj  * before the rewind completes will hang waiting for ctsbuf.
463244Swnj  */
473244Swnj struct	buf	ctsbuf[NTS];
481900Swnj 
493244Swnj /*
503244Swnj  * Raw tape operations use rtsbuf.  The driver
513244Swnj  * notices when rtsbuf is being used and allows the user
523244Swnj  * program to continue after errors and read records
533244Swnj  * not of the standard length (BSIZE).
543244Swnj  */
553244Swnj struct	buf	rtsbuf[NTS];
561900Swnj 
573244Swnj /*
583244Swnj  * Driver unibus interface routines and variables.
593244Swnj  */
603244Swnj int	tsprobe(), tsslave(), tsattach(), tsdgo(), tsintr();
613244Swnj struct	uba_ctlr *tsminfo[NTS];
623244Swnj struct	uba_device *tsdinfo[NTS];
635693Sroot struct buf	tsutab[NTS];
643244Swnj u_short	tsstd[] = { 0772520, 0 };
653244Swnj /*** PROBABLY DON'T NEED ALL THESE SINCE CONTROLLER == DRIVE ***/
663244Swnj struct	uba_driver zsdriver =
673327Swnj  { tsprobe, tsslave, tsattach, tsdgo, tsstd, "ts", tsdinfo, "zs", tsminfo, 0 };
681900Swnj 
693244Swnj /* bits in minor device */
703244Swnj #define	TSUNIT(dev)	(minor(dev)&03)
713244Swnj #define	T_NOREWIND	04
721900Swnj 
733244Swnj #define	INF	(daddr_t)1000000L
741900Swnj 
753244Swnj /*
763244Swnj  * Software state per tape transport.
773244Swnj  * Also contains hardware state in message packets.
783244Swnj  *
793244Swnj  * 1. A tape drive is a unique-open device; we refuse opens when it is already.
803244Swnj  * 2. We keep track of the current position on a block tape and seek
813244Swnj  *    before operations by forward/back spacing if necessary.
823244Swnj  * 3. We remember if the last operation was a write on a tape, so if a tape
833244Swnj  *    is open read write and the last thing done is a write we can
843244Swnj  *    write a standard end of tape mark (two eofs).
853244Swnj  * 4. We remember the status registers after the last command, using
863244Swnj  *    then internally and returning them to the SENSE ioctl.
873244Swnj  */
883244Swnj struct	ts_softc {
893244Swnj 	char	sc_openf;	/* lock against multiple opens */
903244Swnj 	char	sc_lastiow;	/* last op was a write */
913327Swnj 	short	sc_resid;	/* copy of last bc */
923244Swnj 	daddr_t	sc_blkno;	/* block number, for block device tape */
933244Swnj 	daddr_t	sc_nxrec;	/* position of end of tape, if known */
945693Sroot 	struct	ts_cmd sc_cmd;	/* the command packet */
955693Sroot 	struct	ts_sts sc_sts;	/* status packet, for returned status */
965693Sroot 	struct	ts_char sc_char; /* characteristics packet */
975693Sroot 	struct	ts_softc *sc_ubaddr; /* Unibus address of ts_softc structure */
985697Sroot 	u_short	sc_uba;		/* Unibus addr of cmd pkt for tsdb */
995693Sroot 	short	sc_mapped;	/* is ts_sfotc mapped in Unibus space? */
10018322Sralph 	struct	tty *sc_ttyp;	/* record user's tty for errors */
1013244Swnj } ts_softc[NTS];
1021900Swnj 
1033244Swnj /*
1043244Swnj  * States for um->um_tab.b_active, the per controller state flag.
1053244Swnj  * This is used to sequence control in the driver.
1063244Swnj  */
1073244Swnj #define	SSEEK	1		/* seeking */
1083244Swnj #define	SIO	2		/* doing seq i/o */
1093244Swnj #define	SCOM	3		/* sending control command */
1103244Swnj #define	SREW	4		/* sending a drive rewind */
1111900Swnj 
1123244Swnj /*
1133244Swnj  * Determine if there is a controller for
1143244Swnj  * a ts at address reg.  Our goal is to make the
1153244Swnj  * device interrupt.
1163244Swnj  */
1173985Sroot /*ARGSUSED*/
1183244Swnj tsprobe(reg)
1193244Swnj 	caddr_t reg;
1203244Swnj {
1213244Swnj 	register int br, cvec;		/* must be r11,r10; value-result */
1221900Swnj 
1233244Swnj #ifdef lint
1243244Swnj 	br = 0; cvec = br; br = cvec;
1254937Swnj 	tsintr(0);
1263244Swnj #endif
1275693Sroot 	((struct tsdevice *)reg)->tssr = 0;
1285693Sroot 	DELAY(100);
1295693Sroot 	if ((((struct tsdevice *)reg)->tssr & TS_NBA) == 0)
1305693Sroot 		return(0);
1315693Sroot 	/* IT'S TOO HARD TO MAKE THIS THING INTERRUPT JUST TO FIND ITS VECTOR */
1325693Sroot 	cvec = ((unsigned)reg) & 07 ? 0260 : 0224;
1333244Swnj 	br = 0x15;
1347407Skre 	return (sizeof (struct tsdevice));
1353244Swnj }
1361900Swnj 
1373244Swnj /*
1383244Swnj  * TS11 only supports one drive per controller;
1393244Swnj  * check for ui_slave == 0.
1403244Swnj  *
1413244Swnj  * DO WE REALLY NEED THIS ROUTINE???
1423244Swnj  */
1433244Swnj /*ARGSUSED*/
1443244Swnj tsslave(ui, reg)
1453244Swnj 	struct uba_device *ui;
1463244Swnj 	caddr_t reg;
1473244Swnj {
1481900Swnj 
1493244Swnj 	if (ui->ui_slave)	/* non-zero slave not allowed */
1503244Swnj 		return(0);
1513244Swnj 	return (1);
1523244Swnj }
1531900Swnj 
1543244Swnj /*
1553244Swnj  * Record attachment of the unit to the controller.
1563244Swnj  *
1573244Swnj  * SHOULD THIS ROUTINE DO ANYTHING???
1583244Swnj  */
1593244Swnj /*ARGSUSED*/
1603244Swnj tsattach(ui)
1613244Swnj 	struct uba_device *ui;
1623244Swnj {
1631900Swnj 
1643244Swnj }
1651900Swnj 
1663244Swnj /*
1673244Swnj  * Open the device.  Tapes are unique open
1683244Swnj  * devices, so we refuse if it is already open.
1693244Swnj  * We also check that a tape is available, and
1703244Swnj  * don't block waiting here; if you want to wait
1713244Swnj  * for a tape you should timeout in user code.
1723244Swnj  */
1731900Swnj tsopen(dev, flag)
1743244Swnj 	dev_t dev;
1753244Swnj 	int flag;
1761900Swnj {
1773244Swnj 	register int tsunit;
1783244Swnj 	register struct uba_device *ui;
1793244Swnj 	register struct ts_softc *sc;
1801900Swnj 
1813244Swnj 	tsunit = TSUNIT(dev);
182*25054Skarels 	if (tsunit>=NTS || (ui = tsdinfo[tsunit]) == 0 || ui->ui_alive == 0)
1838575Sroot 		return (ENXIO);
184*25054Skarels 	if ((sc = &ts_softc[tsunit])->sc_openf)
185*25054Skarels 		return (EBUSY);
1868575Sroot 	if (tsinit(tsunit))
1878575Sroot 		return (ENXIO);
1883244Swnj 	tscommand(dev, TS_SENSE, 1);
1893711Sroot 	if ((sc->sc_sts.s_xs0&TS_ONL) == 0) {
1903711Sroot 		uprintf("ts%d: not online\n", tsunit);
1918575Sroot 		return (EIO);
1921900Swnj 	}
19324267Sbloom 	if ((flag&FWRITE) && (sc->sc_sts.s_xs0&TS_WLK)) {
1943711Sroot 		uprintf("ts%d: no write ring\n", tsunit);
1958575Sroot 		return (EIO);
1963711Sroot 	}
1973244Swnj 	sc->sc_openf = 1;
1983244Swnj 	sc->sc_blkno = (daddr_t)0;
1993244Swnj 	sc->sc_nxrec = INF;
2003244Swnj 	sc->sc_lastiow = 0;
20118322Sralph 	sc->sc_ttyp = u.u_ttyp;
2028575Sroot 	return (0);
2031900Swnj }
2041900Swnj 
2053244Swnj /*
2063244Swnj  * Close tape device.
2073244Swnj  *
2083244Swnj  * If tape was open for writing or last operation was
2093244Swnj  * a write, then write two EOF's and backspace over the last one.
2103244Swnj  * Unless this is a non-rewinding special file, rewind the tape.
2113244Swnj  * Make the tape available to others.
2123244Swnj  */
2131900Swnj tsclose(dev, flag)
2143244Swnj 	register dev_t dev;
2153244Swnj 	register flag;
2161900Swnj {
2173244Swnj 	register struct ts_softc *sc = &ts_softc[TSUNIT(dev)];
2181900Swnj 
2193244Swnj 	if (flag == FWRITE || (flag&FWRITE) && sc->sc_lastiow) {
2203244Swnj 		tscommand(dev, TS_WEOF, 1);
2213244Swnj 		tscommand(dev, TS_WEOF, 1);
2223244Swnj 		tscommand(dev, TS_SREV, 1);
2231900Swnj 	}
2243244Swnj 	if ((minor(dev)&T_NOREWIND) == 0)
2253244Swnj 		/*
2263244Swnj 		 * 0 count means don't hang waiting for rewind complete
2273244Swnj 		 * rather ctsbuf stays busy until the operation completes
2283244Swnj 		 * preventing further opens from completing by
2293244Swnj 		 * preventing a TS_SENSE from completing.
2303244Swnj 		 */
2313244Swnj 		tscommand(dev, TS_REW, 0);
2323244Swnj 	sc->sc_openf = 0;
2331900Swnj }
2341900Swnj 
2353244Swnj /*
2363244Swnj  * Initialize the TS11.  Set up Unibus mapping for command
2373244Swnj  * packets and set device characteristics.
2383244Swnj  */
2393244Swnj tsinit(unit)
2403244Swnj 	register int unit;
2411900Swnj {
2423244Swnj 	register struct ts_softc *sc = &ts_softc[unit];
2433244Swnj 	register struct uba_ctlr *um = tsminfo[unit];
2445693Sroot 	register struct tsdevice *addr = (struct tsdevice *)um->um_addr;
2453244Swnj 	register int i;
2463244Swnj 
2473244Swnj 	/*
2483244Swnj 	 * Map the command and message packets into Unibus
2493244Swnj 	 * address space.  We do all the command and message
2503244Swnj 	 * packets at once to minimize the amount of Unibus
2513244Swnj 	 * mapping necessary.
2523244Swnj 	 */
2535693Sroot 	if (sc->sc_mapped == 0) {
2545693Sroot 		ctsbuf[unit].b_un.b_addr = (caddr_t)sc;
2555693Sroot 		ctsbuf[unit].b_bcount = sizeof(*sc);
2563244Swnj 		i = ubasetup(um->um_ubanum, &ctsbuf[unit], 0);
2573244Swnj 		i &= 0777777;
2585693Sroot 		sc->sc_ubaddr = (struct ts_softc *)i;
2595693Sroot 		sc->sc_mapped++;
2603244Swnj 	}
2613244Swnj 	/*
2623244Swnj 	 * Now initialize the TS11 controller.
2633244Swnj 	 * Set the characteristics.
2643244Swnj 	 */
2653668Swnj 	if (addr->tssr & (TS_NBA|TS_OFL)) {
2663244Swnj 		addr->tssr = 0;		/* subsystem initialize */
2673244Swnj 		tswait(addr);
2685693Sroot 		i = (int)&sc->sc_ubaddr->sc_cmd;	/* Unibus addr of cmd */
2693244Swnj 		sc->sc_uba = (u_short)(i + ((i>>16)&3));
2705693Sroot 		sc->sc_char.char_addr = (int)&sc->sc_ubaddr->sc_sts;
2713244Swnj 		sc->sc_char.char_size = sizeof(struct ts_sts);
2723244Swnj 		sc->sc_char.char_mode = TS_ESS;
2733244Swnj 		sc->sc_cmd.c_cmd = TS_ACK | TS_SETCHR;
2745693Sroot 		i = (int)&sc->sc_ubaddr->sc_char;
2753327Swnj 		sc->sc_cmd.c_loba = i;
2763327Swnj 		sc->sc_cmd.c_hiba = (i>>16)&3;
2773244Swnj 		sc->sc_cmd.c_size = sizeof(struct ts_char);
2783244Swnj 		addr->tsdb = sc->sc_uba;
2793244Swnj 		tswait(addr);
2803327Swnj 		if (addr->tssr & TS_NBA)
2813327Swnj 			return(1);
2823244Swnj 	}
2833244Swnj 	return(0);
2843244Swnj }
2853244Swnj 
2863244Swnj /*
2873244Swnj  * Execute a command on the tape drive
2883244Swnj  * a specified number of times.
2893244Swnj  */
2903244Swnj tscommand(dev, com, count)
2913244Swnj 	dev_t dev;
2923244Swnj 	int com, count;
2933244Swnj {
2941900Swnj 	register struct buf *bp;
2955438Sroot 	register int s;
2961900Swnj 
2973244Swnj 	bp = &ctsbuf[TSUNIT(dev)];
2985438Sroot 	s = spl5();
2993244Swnj 	while (bp->b_flags&B_BUSY) {
3003244Swnj 		/*
3013244Swnj 		 * This special check is because B_BUSY never
3023244Swnj 		 * gets cleared in the non-waiting rewind case.
3033244Swnj 		 */
3043244Swnj 		if (bp->b_repcnt == 0 && (bp->b_flags&B_DONE))
3053244Swnj 			break;
3061900Swnj 		bp->b_flags |= B_WANTED;
3071900Swnj 		sleep((caddr_t)bp, PRIBIO);
3081900Swnj 	}
3093244Swnj 	bp->b_flags = B_BUSY|B_READ;
3105438Sroot 	splx(s);
3113244Swnj 	bp->b_dev = dev;
3123244Swnj 	bp->b_repcnt = count;
3133244Swnj 	bp->b_command = com;
3141900Swnj 	bp->b_blkno = 0;
3151900Swnj 	tsstrategy(bp);
3163244Swnj 	/*
3173244Swnj 	 * In case of rewind from close, don't wait.
3183244Swnj 	 * This is the only case where count can be 0.
3193244Swnj 	 */
3203244Swnj 	if (count == 0)
3213244Swnj 		return;
3221900Swnj 	iowait(bp);
3233244Swnj 	if (bp->b_flags&B_WANTED)
3241900Swnj 		wakeup((caddr_t)bp);
3253244Swnj 	bp->b_flags &= B_ERROR;
3261900Swnj }
3271900Swnj 
3283244Swnj /*
3293244Swnj  * Queue a tape operation.
3303244Swnj  */
3311900Swnj tsstrategy(bp)
3323244Swnj 	register struct buf *bp;
3331900Swnj {
3343244Swnj 	int tsunit = TSUNIT(bp->b_dev);
3353244Swnj 	register struct uba_ctlr *um;
3363327Swnj 	register struct buf *dp;
3375438Sroot 	register int s;
3381900Swnj 
3393244Swnj 	/*
3403244Swnj 	 * Put transfer at end of controller queue
3413244Swnj 	 */
3421900Swnj 	bp->av_forw = NULL;
3433244Swnj 	um = tsdinfo[tsunit]->ui_mi;
3445438Sroot 	s = spl5();
3455693Sroot 	dp = &tsutab[tsunit];
3463327Swnj 	if (dp->b_actf == NULL)
3473327Swnj 		dp->b_actf = bp;
3481900Swnj 	else
3493327Swnj 		dp->b_actl->av_forw = bp;
3503327Swnj 	dp->b_actl = bp;
3513327Swnj 	um->um_tab.b_actf = um->um_tab.b_actl = dp;
3523244Swnj 	/*
3533244Swnj 	 * If the controller is not busy, get
3543244Swnj 	 * it going.
3553244Swnj 	 */
3563244Swnj 	if (um->um_tab.b_active == 0)
3573244Swnj 		tsstart(um);
3585438Sroot 	splx(s);
3591900Swnj }
3601900Swnj 
3613244Swnj /*
3623244Swnj  * Start activity on a ts controller.
3633244Swnj  */
3643244Swnj tsstart(um)
3653244Swnj 	register struct uba_ctlr *um;
3661900Swnj {
3671900Swnj 	register struct buf *bp;
3685693Sroot 	register struct tsdevice *addr = (struct tsdevice *)um->um_addr;
3693244Swnj 	register struct ts_softc *sc;
3703244Swnj 	register struct ts_cmd *tc;
3713244Swnj 	register struct uba_device *ui;
3723244Swnj 	int tsunit, cmd;
3731900Swnj 	daddr_t blkno;
3741900Swnj 
3753244Swnj 	/*
3763244Swnj 	 * Start the controller if there is something for it to do.
3773244Swnj 	 */
3783244Swnj loop:
3793327Swnj 	if ((bp = um->um_tab.b_actf->b_actf) == NULL)
3801900Swnj 		return;
3813244Swnj 	tsunit = TSUNIT(bp->b_dev);
3823244Swnj 	ui = tsdinfo[tsunit];
3833244Swnj 	sc = &ts_softc[tsunit];
3843244Swnj 	tc = &sc->sc_cmd;
3853244Swnj 	/*
3863244Swnj 	 * Default is that last command was NOT a write command;
3873244Swnj 	 * if we do a write command we will notice this in tsintr().
3883244Swnj 	 */
3893656Swnj 	sc->sc_lastiow = 0;
3903244Swnj 	if (sc->sc_openf < 0 || (addr->tssr&TS_OFL)) {
3913244Swnj 		/*
3923244Swnj 		 * Have had a hard error on a non-raw tape
3933244Swnj 		 * or the tape unit is now unavailable
3943244Swnj 		 * (e.g. taken off line).
3953244Swnj 		 */
3963244Swnj 		bp->b_flags |= B_ERROR;
3973244Swnj 		goto next;
3983244Swnj 	}
3993244Swnj 	if (bp == &ctsbuf[TSUNIT(bp->b_dev)]) {
4003244Swnj 		/*
4013244Swnj 		 * Execute control operation with the specified count.
4023244Swnj 		 */
4033244Swnj 		um->um_tab.b_active =
4043244Swnj 		    bp->b_command == TS_REW ? SREW : SCOM;
4053244Swnj 		tc->c_repcnt = bp->b_repcnt;
4063244Swnj 		goto dobpcmd;
4073244Swnj 	}
4083244Swnj 	/*
4093244Swnj 	 * The following checks handle boundary cases for operation
4103244Swnj 	 * on non-raw tapes.  On raw tapes the initialization of
4113244Swnj 	 * sc->sc_nxrec by tsphys causes them to be skipped normally
4123244Swnj 	 * (except in the case of retries).
4133244Swnj 	 */
4147383Ssam 	if (bdbtofsb(bp->b_blkno) > sc->sc_nxrec) {
4153244Swnj 		/*
4163244Swnj 		 * Can't read past known end-of-file.
4173244Swnj 		 */
4183244Swnj 		bp->b_flags |= B_ERROR;
4193244Swnj 		bp->b_error = ENXIO;
4203244Swnj 		goto next;
4213244Swnj 	}
4227383Ssam 	if (bdbtofsb(bp->b_blkno) == sc->sc_nxrec &&
4233244Swnj 	    bp->b_flags&B_READ) {
4243244Swnj 		/*
4253244Swnj 		 * Reading at end of file returns 0 bytes.
4263244Swnj 		 */
4273244Swnj 		bp->b_resid = bp->b_bcount;
4283244Swnj 		clrbuf(bp);
4293244Swnj 		goto next;
4303244Swnj 	}
4313244Swnj 	if ((bp->b_flags&B_READ) == 0)
4323244Swnj 		/*
4333244Swnj 		 * Writing sets EOF
4343244Swnj 		 */
4357383Ssam 		sc->sc_nxrec = bdbtofsb(bp->b_blkno) + 1;
4363244Swnj 	/*
4373244Swnj 	 * If the data transfer command is in the correct place,
4383244Swnj 	 * set up all the registers except the csr, and give
4393244Swnj 	 * control over to the UNIBUS adapter routines, to
4403244Swnj 	 * wait for resources to start the i/o.
4413244Swnj 	 */
4427383Ssam 	if ((blkno = sc->sc_blkno) == bdbtofsb(bp->b_blkno)) {
4433244Swnj 		tc->c_size = bp->b_bcount;
4443244Swnj 		if ((bp->b_flags&B_READ) == 0)
4453244Swnj 			cmd = TS_WCOM;
4461900Swnj 		else
4473244Swnj 			cmd = TS_RCOM;
4483244Swnj 		if (um->um_tab.b_errcnt)
4493244Swnj 			cmd |= TS_RETRY;
4503244Swnj 		um->um_tab.b_active = SIO;
4513327Swnj 		tc->c_cmd = TS_ACK | TS_CVC | TS_IE | cmd;
4523244Swnj 		(void) ubago(ui);
4533244Swnj 		return;
4543244Swnj 	}
4553244Swnj 	/*
4563244Swnj 	 * Tape positioned incorrectly;
4573244Swnj 	 * set to seek forwards or backwards to the correct spot.
4583244Swnj 	 * This happens for raw tapes only on error retries.
4593244Swnj 	 */
4603244Swnj 	um->um_tab.b_active = SSEEK;
4617383Ssam 	if (blkno < bdbtofsb(bp->b_blkno)) {
4623244Swnj 		bp->b_command = TS_SFORW;
4637383Ssam 		tc->c_repcnt = bdbtofsb(bp->b_blkno) - blkno;
4641900Swnj 	} else {
4653244Swnj 		bp->b_command = TS_SREV;
4667383Ssam 		tc->c_repcnt = blkno - bdbtofsb(bp->b_blkno);
4671900Swnj 	}
4683244Swnj dobpcmd:
4693244Swnj 	/*
4703244Swnj 	 * Do the command in bp.
4713244Swnj 	 */
4723327Swnj 	tc->c_cmd = TS_ACK | TS_CVC | TS_IE | bp->b_command;
4733244Swnj 	addr->tsdb = sc->sc_uba;
4741900Swnj 	return;
4751900Swnj 
4763244Swnj next:
4773244Swnj 	/*
4783244Swnj 	 * Done with this operation due to error or
4793244Swnj 	 * the fact that it doesn't do anything.
4803244Swnj 	 * Release UBA resources (if any), dequeue
4813244Swnj 	 * the transfer and continue processing this slave.
4823244Swnj 	 */
4833244Swnj 	if (um->um_ubinfo)
4843244Swnj 		ubadone(um);
4853244Swnj 	um->um_tab.b_errcnt = 0;
4863327Swnj 	um->um_tab.b_actf->b_actf = bp->av_forw;
4871900Swnj 	iodone(bp);
4881900Swnj 	goto loop;
4891900Swnj }
4901900Swnj 
4913244Swnj /*
4923244Swnj  * The UNIBUS resources we needed have been
4933244Swnj  * allocated to us; start the device.
4943244Swnj  */
4953244Swnj tsdgo(um)
4963244Swnj 	register struct uba_ctlr *um;
4971900Swnj {
4985693Sroot 	register struct tsdevice *addr = (struct tsdevice *)um->um_addr;
4993244Swnj 	register struct ts_softc *sc = &ts_softc[um->um_ctlr];
5003327Swnj 	register int i;
5013244Swnj 
5023327Swnj 	i = um->um_ubinfo & 0777777;
5033327Swnj 	sc->sc_cmd.c_loba = i;
5043327Swnj 	sc->sc_cmd.c_hiba = (i>>16)&3;
5053244Swnj 	addr->tsdb = sc->sc_uba;
5063244Swnj }
5073244Swnj 
5083244Swnj /*
5093244Swnj  * Ts interrupt routine.
5103244Swnj  */
5113244Swnj /*ARGSUSED*/
5123244Swnj tsintr(ts11)
5133244Swnj 	int ts11;
5143244Swnj {
5151900Swnj 	register struct buf *bp;
5163244Swnj 	register struct uba_ctlr *um = tsminfo[ts11];
5175693Sroot 	register struct tsdevice *addr;
5183244Swnj 	register struct ts_softc *sc;
5193244Swnj 	int tsunit;
5203244Swnj 	register state;
5211900Swnj 
5223327Swnj 	if ((bp = um->um_tab.b_actf->b_actf) == NULL)
5231900Swnj 		return;
5243244Swnj 	tsunit = TSUNIT(bp->b_dev);
5255693Sroot 	addr = (struct tsdevice *)tsdinfo[tsunit]->ui_addr;
5263244Swnj 	/*
5273244Swnj 	 * If last command was a rewind, and tape is still
5283244Swnj 	 * rewinding, wait for the rewind complete interrupt.
5293244Swnj 	 *
5303244Swnj 	 * SHOULD NEVER GET AN INTERRUPT IN THIS STATE.
5313244Swnj 	 */
5323244Swnj 	if (um->um_tab.b_active == SREW) {
5333244Swnj 		um->um_tab.b_active = SCOM;
5343244Swnj 		if ((addr->tssr&TS_SSR) == 0)
5353244Swnj 			return;
5363244Swnj 	}
5373244Swnj 	/*
5383244Swnj 	 * An operation completed... record status
5393244Swnj 	 */
5403244Swnj 	sc = &ts_softc[tsunit];
5413244Swnj 	if ((bp->b_flags & B_READ) == 0)
5423244Swnj 		sc->sc_lastiow = 1;
5433244Swnj 	state = um->um_tab.b_active;
5443244Swnj 	um->um_tab.b_active = 0;
5453244Swnj 	/*
5463244Swnj 	 * Check for errors.
5473244Swnj 	 */
5483244Swnj 	if (addr->tssr&TS_SC) {
5493244Swnj 		switch (addr->tssr & TS_TC) {
5503244Swnj 		case TS_UNREC:		/* unrecoverable */
5513244Swnj 		case TS_FATAL:		/* fatal error */
5523244Swnj 		case TS_ATTN:		/* attention (shouldn't happen) */
5533244Swnj 		case TS_RECNM:		/* recoverable, no motion */
5543244Swnj 			break;
5551900Swnj 
5563244Swnj 		case TS_SUCC:		/* success termination */
5573244Swnj 			printf("ts%d: success\n", TSUNIT(minor(bp->b_dev)));
5583244Swnj 			goto ignoreerr;
5591900Swnj 
5603244Swnj 		case TS_ALERT:		/* tape status alert */
5613244Swnj 			/*
5623244Swnj 			 * If we hit the end of the tape file,
5633244Swnj 			 * update our position.
5643244Swnj 			 */
5653244Swnj 			if (sc->sc_sts.s_xs0 & (TS_TMK|TS_EOT)) {
5663244Swnj 				tsseteof(bp);		/* set blkno and nxrec */
5673244Swnj 				state = SCOM;		/* force completion */
5683244Swnj 				/*
5693244Swnj 				 * Stuff bc so it will be unstuffed correctly
5703244Swnj 				 * later to get resid.
5713244Swnj 				 */
5723244Swnj 				sc->sc_sts.s_rbpcr = bp->b_bcount;
5733244Swnj 				goto opdone;
5743244Swnj 			}
5753244Swnj 			/*
5763244Swnj 			 * If we were reading raw tape and the record was too long
5773244Swnj 			 * or too short, then we don't consider this an error.
5783244Swnj 			 */
5793244Swnj 			if (bp == &rtsbuf[TSUNIT(bp->b_dev)] && (bp->b_flags&B_READ) &&
5803244Swnj 			    sc->sc_sts.s_xs0&(TS_RLS|TS_RLL))
5813244Swnj 				goto ignoreerr;
5823244Swnj 		case TS_RECOV:		/* recoverable, tape moved */
5833244Swnj 			/*
5843244Swnj 			 * If this was an i/o operation retry up to 8 times.
5853244Swnj 			 */
5863244Swnj 			if (state==SIO) {
5873244Swnj 				if (++um->um_tab.b_errcnt < 7) {
5883244Swnj 					ubadone(um);
5893244Swnj 					goto opcont;
5903244Swnj 				} else
5913244Swnj 					sc->sc_blkno++;
5923244Swnj 			} else {
5933244Swnj 				/*
5943244Swnj 				 * Non-i/o errors on non-raw tape
5953244Swnj 				 * cause it to close.
5963244Swnj 				 */
5973244Swnj 				if (sc->sc_openf>0 && bp != &rtsbuf[TSUNIT(bp->b_dev)])
5983244Swnj 					sc->sc_openf = -1;
5993244Swnj 			}
6001900Swnj 			break;
6013244Swnj 
6023244Swnj 		case TS_REJECT:		/* function reject */
6033244Swnj 			if (state == SIO && sc->sc_sts.s_xs0 & TS_WLE)
60418322Sralph 				tprintf(sc->sc_ttyp, "ts%d: write locked\n",
60518322Sralph 				    TSUNIT(bp->b_dev));
6063244Swnj 			if ((sc->sc_sts.s_xs0 & TS_ONL) == 0)
60718322Sralph 				tprintf(sc->sc_ttyp, "ts%d: offline\n",
60818322Sralph 				    TSUNIT(bp->b_dev));
6091900Swnj 			break;
6101900Swnj 		}
6113244Swnj 		/*
6123244Swnj 		 * Couldn't recover error
6133244Swnj 		 */
61418322Sralph 		tprintf(sc->sc_ttyp, "ts%d: hard error bn%d xs0=%b",
61518322Sralph 		    TSUNIT(bp->b_dev), bp->b_blkno, sc->sc_sts.s_xs0, TSXS0_BITS);
6163647Swnj 		if (sc->sc_sts.s_xs1)
61718322Sralph 			tprintf(sc->sc_ttyp, " xs1=%b", sc->sc_sts.s_xs1,
61818322Sralph 			    TSXS1_BITS);
6193647Swnj 		if (sc->sc_sts.s_xs2)
62018322Sralph 			tprintf(sc->sc_ttyp, " xs2=%b", sc->sc_sts.s_xs2,
62118322Sralph 			    TSXS2_BITS);
6223647Swnj 		if (sc->sc_sts.s_xs3)
62318322Sralph 			tprintf(sc->sc_ttyp, " xs3=%b", sc->sc_sts.s_xs3,
62418322Sralph 			    TSXS3_BITS);
62518322Sralph 		tprintf(sc->sc_ttyp, "\n");
6263244Swnj 		bp->b_flags |= B_ERROR;
6273244Swnj 		goto opdone;
6283244Swnj 	}
6293244Swnj 	/*
6303244Swnj 	 * Advance tape control FSM.
6313244Swnj 	 */
6323244Swnj ignoreerr:
6333244Swnj 	switch (state) {
6341900Swnj 
6353244Swnj 	case SIO:
6363244Swnj 		/*
6373244Swnj 		 * Read/write increments tape block number
6383244Swnj 		 */
6393244Swnj 		sc->sc_blkno++;
6403244Swnj 		goto opdone;
6411900Swnj 
6421900Swnj 	case SCOM:
6433244Swnj 		/*
6443244Swnj 		 * For forward/backward space record update current position.
6453244Swnj 		 */
6463244Swnj 		if (bp == &ctsbuf[TSUNIT(bp->b_dev)])
6473244Swnj 		switch (bp->b_command) {
6481900Swnj 
6493244Swnj 		case TS_SFORW:
6503244Swnj 			sc->sc_blkno += bp->b_repcnt;
6513244Swnj 			break;
6521900Swnj 
6533244Swnj 		case TS_SREV:
6543244Swnj 			sc->sc_blkno -= bp->b_repcnt;
6553244Swnj 			break;
6563244Swnj 		}
6573244Swnj 		goto opdone;
6583244Swnj 
6593244Swnj 	case SSEEK:
6607383Ssam 		sc->sc_blkno = bdbtofsb(bp->b_blkno);
6613244Swnj 		goto opcont;
6623244Swnj 
6631900Swnj 	default:
6643244Swnj 		panic("tsintr");
6651900Swnj 	}
6663244Swnj opdone:
6673244Swnj 	/*
6683244Swnj 	 * Reset error count and remove
6693244Swnj 	 * from device queue.
6703244Swnj 	 */
6713244Swnj 	um->um_tab.b_errcnt = 0;
6723327Swnj 	um->um_tab.b_actf->b_actf = bp->av_forw;
6733244Swnj 	bp->b_resid = sc->sc_sts.s_rbpcr;
6743244Swnj 	ubadone(um);
6753244Swnj 	iodone(bp);
6763327Swnj 	if (um->um_tab.b_actf->b_actf == 0)
6773244Swnj 		return;
6783244Swnj opcont:
6793244Swnj 	tsstart(um);
6803244Swnj }
6813244Swnj 
6823244Swnj tsseteof(bp)
6833244Swnj 	register struct buf *bp;
6843244Swnj {
6853244Swnj 	register int tsunit = TSUNIT(bp->b_dev);
6863244Swnj 	register struct ts_softc *sc = &ts_softc[tsunit];
6873244Swnj 
6883244Swnj 	if (bp == &ctsbuf[TSUNIT(bp->b_dev)]) {
6897383Ssam 		if (sc->sc_blkno > bdbtofsb(bp->b_blkno)) {
6903244Swnj 			/* reversing */
6917383Ssam 			sc->sc_nxrec = bdbtofsb(bp->b_blkno) - sc->sc_sts.s_rbpcr;
6923244Swnj 			sc->sc_blkno = sc->sc_nxrec;
6933244Swnj 		} else {
6943244Swnj 			/* spacing forward */
6957383Ssam 			sc->sc_blkno = bdbtofsb(bp->b_blkno) + sc->sc_sts.s_rbpcr;
6963244Swnj 			sc->sc_nxrec = sc->sc_blkno - 1;
6971900Swnj 		}
6983244Swnj 		return;
6993244Swnj 	}
7003244Swnj 	/* eof on read */
7017383Ssam 	sc->sc_nxrec = bdbtofsb(bp->b_blkno);
7021900Swnj }
7031900Swnj 
7047733Sroot tsread(dev, uio)
7053244Swnj 	dev_t dev;
7067733Sroot 	struct uio *uio;
7071900Swnj {
7088164Sroot 	int errno;
7093244Swnj 
7108164Sroot 	errno = tsphys(dev, uio);
7118164Sroot 	if (errno)
7128164Sroot 		return (errno);
7138164Sroot 	return (physio(tsstrategy, &rtsbuf[TSUNIT(dev)], dev, B_READ, minphys, uio));
7141900Swnj }
7151900Swnj 
7167841Sroot tswrite(dev, uio)
7173244Swnj 	dev_t dev;
7187841Sroot 	struct uio *uio;
7191900Swnj {
7208164Sroot 	int errno;
7213244Swnj 
7228164Sroot 	errno = tsphys(dev, uio);
7238164Sroot 	if (errno)
7248164Sroot 		return (errno);
7258164Sroot 	return (physio(tsstrategy, &rtsbuf[TSUNIT(dev)], dev, B_WRITE, minphys, uio));
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  */
7337733Sroot tsphys(dev, uio)
7343244Swnj 	dev_t dev;
7357733Sroot 	struct uio *uio;
7361900Swnj {
7373244Swnj 	register int tsunit = TSUNIT(dev);
7383244Swnj 	register daddr_t a;
7393244Swnj 	register struct ts_softc *sc;
7403244Swnj 	register struct uba_device *ui;
7411900Swnj 
7427841Sroot 	if (tsunit >= NTS || (ui=tsdinfo[tsunit]) == 0 || ui->ui_alive == 0)
7437733Sroot 		return (ENXIO);
7443244Swnj 	sc = &ts_softc[tsunit];
7457841Sroot 	a = bdbtofsb(uio->uio_offset >> 9);
7463244Swnj 	sc->sc_blkno = a;
7473244Swnj 	sc->sc_nxrec = a + 1;
7487733Sroot 	return (0);
7491900Swnj }
7501918Swnj 
7513244Swnj tsreset(uban)
7523244Swnj 	int uban;
7531918Swnj {
7543244Swnj 	register struct uba_ctlr *um;
7555693Sroot 	register struct uba_device *ui;
7565693Sroot 	register struct buf *dp;
7573244Swnj 	register ts11;
7581918Swnj 
7593244Swnj 	for (ts11 = 0; ts11 < NTS; ts11++) {
7603244Swnj 		if ((um = tsminfo[ts11]) == 0 || um->um_alive == 0 ||
7613244Swnj 		   um->um_ubanum != uban)
7623244Swnj 			continue;
7633244Swnj 		printf(" ts%d", ts11);
7643244Swnj 		um->um_tab.b_active = 0;
7653244Swnj 		um->um_tab.b_actf = um->um_tab.b_actl = 0;
7665693Sroot 		if (ts_softc[ts11].sc_openf > 0)
7675693Sroot 			ts_softc[ts11].sc_openf = -1;
7683244Swnj 		if (um->um_ubinfo) {
7693244Swnj 			printf("<%d>", (um->um_ubinfo>>28)&0xf);
7709356Ssam 			um->um_ubinfo = 0;
7713244Swnj 		}
7725693Sroot 		if ((ui = tsdinfo[ts11]) && ui->ui_mi == um && ui->ui_alive) {
7735693Sroot 			dp = &tsutab[ts11];
7745693Sroot 			dp->b_active = 0;
7755693Sroot 			dp->b_forw = 0;
7765693Sroot 			if (um->um_tab.b_actf == NULL)
7775693Sroot 				um->um_tab.b_actf = dp;
7785693Sroot 			else
7795693Sroot 				um->um_tab.b_actl->b_forw = dp;
7805693Sroot 			um->um_tab.b_actl = dp;
7815693Sroot 		}
78217432Skarels 		ts_softc[ts11].sc_mapped = 0;
7833989Sroot 		(void) tsinit(ts11);
7843244Swnj 		tsstart(um);
7851918Swnj 	}
7861918Swnj }
7871918Swnj 
7883244Swnj /*ARGSUSED*/
7897633Ssam tsioctl(dev, cmd, data, flag)
7907633Ssam 	caddr_t data;
7913244Swnj 	dev_t dev;
7921918Swnj {
7933244Swnj 	int tsunit = TSUNIT(dev);
7943244Swnj 	register struct ts_softc *sc = &ts_softc[tsunit];
7953244Swnj 	register struct buf *bp = &ctsbuf[TSUNIT(dev)];
7963244Swnj 	register callcount;
7973244Swnj 	int fcount;
7987633Ssam 	struct mtop *mtop;
7997633Ssam 	struct mtget *mtget;
8003244Swnj 	/* we depend of the values and order of the MT codes here */
8013244Swnj 	static tsops[] =
8023656Swnj 	 {TS_WEOF,TS_SFORWF,TS_SREVF,TS_SFORW,TS_SREV,TS_REW,TS_OFFL,TS_SENSE};
8031918Swnj 
8043244Swnj 	switch (cmd) {
8057633Ssam 
8063244Swnj 	case MTIOCTOP:	/* tape operation */
8077633Ssam 		mtop = (struct mtop *)data;
8088575Sroot 		switch (mtop->mt_op) {
8097633Ssam 
8103244Swnj 		case MTWEOF:
8117633Ssam 			callcount = mtop->mt_count;
8123244Swnj 			fcount = 1;
8133244Swnj 			break;
8147633Ssam 
8153244Swnj 		case MTFSF: case MTBSF:
8163244Swnj 		case MTFSR: case MTBSR:
8173244Swnj 			callcount = 1;
8187633Ssam 			fcount = mtop->mt_count;
8193244Swnj 			break;
8207633Ssam 
8213244Swnj 		case MTREW: case MTOFFL: case MTNOP:
8223244Swnj 			callcount = 1;
8233244Swnj 			fcount = 1;
8243244Swnj 			break;
8257633Ssam 
8263244Swnj 		default:
8278575Sroot 			return (ENXIO);
8283244Swnj 		}
8298575Sroot 		if (callcount <= 0 || fcount <= 0)
8308575Sroot 			return (EINVAL);
8313244Swnj 		while (--callcount >= 0) {
8327633Ssam 			tscommand(dev, tsops[mtop->mt_op], fcount);
8337633Ssam 			if ((mtop->mt_op == MTFSR || mtop->mt_op == MTBSR) &&
8348611Sroot 			    bp->b_resid)
8358575Sroot 				return (EIO);
8363244Swnj 			if ((bp->b_flags&B_ERROR) || sc->sc_sts.s_xs0&TS_BOT)
8373244Swnj 				break;
8383244Swnj 		}
8398649Sroot 		return (geterror(bp));
8407633Ssam 
8413244Swnj 	case MTIOCGET:
8427633Ssam 		mtget = (struct mtget *)data;
8437633Ssam 		mtget->mt_dsreg = 0;
8447633Ssam 		mtget->mt_erreg = sc->sc_sts.s_xs0;
8457633Ssam 		mtget->mt_resid = sc->sc_resid;
8467633Ssam 		mtget->mt_type = MT_ISTS;
8478575Sroot 		break;
8487633Ssam 
8493244Swnj 	default:
8508575Sroot 		return (ENXIO);
8513244Swnj 	}
8528575Sroot 	return (0);
8531918Swnj }
8541918Swnj 
8553244Swnj #define	DBSIZE	20
8563244Swnj 
8573244Swnj tsdump()
8581918Swnj {
8593244Swnj 	register struct uba_device *ui;
8603244Swnj 	register struct uba_regs *up;
8615693Sroot 	register struct tsdevice *addr;
8623244Swnj 	int blk, num;
8633244Swnj 	int start;
8641918Swnj 
8653244Swnj 	start = 0;
8663244Swnj 	num = maxfree;
8673244Swnj #define	phys(a,b)	((b)((int)(a)&0x7fffffff))
8683244Swnj 	if (tsdinfo[0] == 0)
8693244Swnj 		return (ENXIO);
8703244Swnj 	ui = phys(tsdinfo[0], struct uba_device *);
8713244Swnj 	up = phys(ui->ui_hd, struct uba_hd *)->uh_physuba;
8728703Sroot 	ubainit(up);
8733244Swnj 	DELAY(1000000);
8745693Sroot 	addr = (struct tsdevice *)ui->ui_physaddr;
8753244Swnj 	addr->tssr = 0;
8763244Swnj 	tswait(addr);
8773244Swnj 	while (num > 0) {
8783244Swnj 		blk = num > DBSIZE ? DBSIZE : num;
8793244Swnj 		tsdwrite(start, blk, addr, up);
8803244Swnj 		start += blk;
8813244Swnj 		num -= blk;
8823244Swnj 	}
8833244Swnj 	tseof(addr);
8843244Swnj 	tseof(addr);
8853244Swnj 	tswait(addr);
8863244Swnj 	if (addr->tssr&TS_SC)
8873244Swnj 		return (EIO);
8883244Swnj 	addr->tssr = 0;
8893244Swnj 	tswait(addr);
8903244Swnj 	return (0);
8911918Swnj }
8921918Swnj 
8933244Swnj tsdwrite(dbuf, num, addr, up)
8948635Sroot 	register int dbuf, num;
8955693Sroot 	register struct tsdevice *addr;
8963244Swnj 	struct uba_regs *up;
8971918Swnj {
8983244Swnj 	register struct pte *io;
8993244Swnj 	register int npf;
9001918Swnj 
9013244Swnj 	tswait(addr);
9023244Swnj 	io = up->uba_map;
9033244Swnj 	npf = num+1;
9043244Swnj 	while (--npf != 0)
9053244Swnj 		 *(int *)io++ = (dbuf++ | (1<<UBAMR_DPSHIFT) | UBAMR_MRV);
9063244Swnj 	*(int *)io = 0;
9073244Swnj #ifdef notyet
9083244Swnj 	addr->tsbc = -(num*NBPG);
9093244Swnj 	addr->tsba = 0;
9103244Swnj 	addr->tscs = TS_WCOM | TM_GO;
9113244Swnj #endif
9121918Swnj }
9131918Swnj 
9143244Swnj tswait(addr)
9155693Sroot 	register struct tsdevice *addr;
9161918Swnj {
9173244Swnj 	register s;
9181918Swnj 
9193244Swnj 	do
9203244Swnj 		s = addr->tssr;
9213244Swnj 	while ((s & TS_SSR) == 0);
9221918Swnj }
9231918Swnj 
9243244Swnj tseof(addr)
9255693Sroot 	struct tsdevice *addr;
9261918Swnj {
9271918Swnj 
9283244Swnj 	tswait(addr);
9293244Swnj #ifdef notyet
9303244Swnj 	addr->tscs = TS_WEOF | TM_GO;
9313244Swnj #endif
9321918Swnj }
9331900Swnj #endif
934