xref: /csrg-svn/sys/vax/uba/ts.c (revision 23349)
1*23349Smckusick /*
2*23349Smckusick  * Copyright (c) 1982 Regents of the University of California.
3*23349Smckusick  * All rights reserved.  The Berkeley software License Agreement
4*23349Smckusick  * specifies the terms and conditions for redistribution.
5*23349Smckusick  *
6*23349Smckusick  *	@(#)ts.c	6.5 (Berkeley) 06/08/85
7*23349Smckusick  */
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);
1823244Swnj 	if (tsunit>=NTS || (sc = &ts_softc[tsunit])->sc_openf ||
1838575Sroot 	    (ui = tsdinfo[tsunit]) == 0 || ui->ui_alive == 0)
1848575Sroot 		return (ENXIO);
1858575Sroot 	if (tsinit(tsunit))
1868575Sroot 		return (ENXIO);
1873244Swnj 	tscommand(dev, TS_SENSE, 1);
1883711Sroot 	if ((sc->sc_sts.s_xs0&TS_ONL) == 0) {
1893711Sroot 		uprintf("ts%d: not online\n", tsunit);
1908575Sroot 		return (EIO);
1911900Swnj 	}
1923718Sroot 	if ((flag&(FREAD|FWRITE)) == FWRITE && (sc->sc_sts.s_xs0&TS_WLK)) {
1933711Sroot 		uprintf("ts%d: no write ring\n", tsunit);
1948575Sroot 		return (EIO);
1953711Sroot 	}
1963244Swnj 	sc->sc_openf = 1;
1973244Swnj 	sc->sc_blkno = (daddr_t)0;
1983244Swnj 	sc->sc_nxrec = INF;
1993244Swnj 	sc->sc_lastiow = 0;
20018322Sralph 	sc->sc_ttyp = u.u_ttyp;
2018575Sroot 	return (0);
2021900Swnj }
2031900Swnj 
2043244Swnj /*
2053244Swnj  * Close tape device.
2063244Swnj  *
2073244Swnj  * If tape was open for writing or last operation was
2083244Swnj  * a write, then write two EOF's and backspace over the last one.
2093244Swnj  * Unless this is a non-rewinding special file, rewind the tape.
2103244Swnj  * Make the tape available to others.
2113244Swnj  */
2121900Swnj tsclose(dev, flag)
2133244Swnj 	register dev_t dev;
2143244Swnj 	register flag;
2151900Swnj {
2163244Swnj 	register struct ts_softc *sc = &ts_softc[TSUNIT(dev)];
2171900Swnj 
2183244Swnj 	if (flag == FWRITE || (flag&FWRITE) && sc->sc_lastiow) {
2193244Swnj 		tscommand(dev, TS_WEOF, 1);
2203244Swnj 		tscommand(dev, TS_WEOF, 1);
2213244Swnj 		tscommand(dev, TS_SREV, 1);
2221900Swnj 	}
2233244Swnj 	if ((minor(dev)&T_NOREWIND) == 0)
2243244Swnj 		/*
2253244Swnj 		 * 0 count means don't hang waiting for rewind complete
2263244Swnj 		 * rather ctsbuf stays busy until the operation completes
2273244Swnj 		 * preventing further opens from completing by
2283244Swnj 		 * preventing a TS_SENSE from completing.
2293244Swnj 		 */
2303244Swnj 		tscommand(dev, TS_REW, 0);
2313244Swnj 	sc->sc_openf = 0;
2321900Swnj }
2331900Swnj 
2343244Swnj /*
2353244Swnj  * Initialize the TS11.  Set up Unibus mapping for command
2363244Swnj  * packets and set device characteristics.
2373244Swnj  */
2383244Swnj tsinit(unit)
2393244Swnj 	register int unit;
2401900Swnj {
2413244Swnj 	register struct ts_softc *sc = &ts_softc[unit];
2423244Swnj 	register struct uba_ctlr *um = tsminfo[unit];
2435693Sroot 	register struct tsdevice *addr = (struct tsdevice *)um->um_addr;
2443244Swnj 	register int i;
2453244Swnj 
2463244Swnj 	/*
2473244Swnj 	 * Map the command and message packets into Unibus
2483244Swnj 	 * address space.  We do all the command and message
2493244Swnj 	 * packets at once to minimize the amount of Unibus
2503244Swnj 	 * mapping necessary.
2513244Swnj 	 */
2525693Sroot 	if (sc->sc_mapped == 0) {
2535693Sroot 		ctsbuf[unit].b_un.b_addr = (caddr_t)sc;
2545693Sroot 		ctsbuf[unit].b_bcount = sizeof(*sc);
2553244Swnj 		i = ubasetup(um->um_ubanum, &ctsbuf[unit], 0);
2563244Swnj 		i &= 0777777;
2575693Sroot 		sc->sc_ubaddr = (struct ts_softc *)i;
2585693Sroot 		sc->sc_mapped++;
2593244Swnj 	}
2603244Swnj 	/*
2613244Swnj 	 * Now initialize the TS11 controller.
2623244Swnj 	 * Set the characteristics.
2633244Swnj 	 */
2643668Swnj 	if (addr->tssr & (TS_NBA|TS_OFL)) {
2653244Swnj 		addr->tssr = 0;		/* subsystem initialize */
2663244Swnj 		tswait(addr);
2675693Sroot 		i = (int)&sc->sc_ubaddr->sc_cmd;	/* Unibus addr of cmd */
2683244Swnj 		sc->sc_uba = (u_short)(i + ((i>>16)&3));
2695693Sroot 		sc->sc_char.char_addr = (int)&sc->sc_ubaddr->sc_sts;
2703244Swnj 		sc->sc_char.char_size = sizeof(struct ts_sts);
2713244Swnj 		sc->sc_char.char_mode = TS_ESS;
2723244Swnj 		sc->sc_cmd.c_cmd = TS_ACK | TS_SETCHR;
2735693Sroot 		i = (int)&sc->sc_ubaddr->sc_char;
2743327Swnj 		sc->sc_cmd.c_loba = i;
2753327Swnj 		sc->sc_cmd.c_hiba = (i>>16)&3;
2763244Swnj 		sc->sc_cmd.c_size = sizeof(struct ts_char);
2773244Swnj 		addr->tsdb = sc->sc_uba;
2783244Swnj 		tswait(addr);
2793327Swnj 		if (addr->tssr & TS_NBA)
2803327Swnj 			return(1);
2813244Swnj 	}
2823244Swnj 	return(0);
2833244Swnj }
2843244Swnj 
2853244Swnj /*
2863244Swnj  * Execute a command on the tape drive
2873244Swnj  * a specified number of times.
2883244Swnj  */
2893244Swnj tscommand(dev, com, count)
2903244Swnj 	dev_t dev;
2913244Swnj 	int com, count;
2923244Swnj {
2931900Swnj 	register struct buf *bp;
2945438Sroot 	register int s;
2951900Swnj 
2963244Swnj 	bp = &ctsbuf[TSUNIT(dev)];
2975438Sroot 	s = spl5();
2983244Swnj 	while (bp->b_flags&B_BUSY) {
2993244Swnj 		/*
3003244Swnj 		 * This special check is because B_BUSY never
3013244Swnj 		 * gets cleared in the non-waiting rewind case.
3023244Swnj 		 */
3033244Swnj 		if (bp->b_repcnt == 0 && (bp->b_flags&B_DONE))
3043244Swnj 			break;
3051900Swnj 		bp->b_flags |= B_WANTED;
3061900Swnj 		sleep((caddr_t)bp, PRIBIO);
3071900Swnj 	}
3083244Swnj 	bp->b_flags = B_BUSY|B_READ;
3095438Sroot 	splx(s);
3103244Swnj 	bp->b_dev = dev;
3113244Swnj 	bp->b_repcnt = count;
3123244Swnj 	bp->b_command = com;
3131900Swnj 	bp->b_blkno = 0;
3141900Swnj 	tsstrategy(bp);
3153244Swnj 	/*
3163244Swnj 	 * In case of rewind from close, don't wait.
3173244Swnj 	 * This is the only case where count can be 0.
3183244Swnj 	 */
3193244Swnj 	if (count == 0)
3203244Swnj 		return;
3211900Swnj 	iowait(bp);
3223244Swnj 	if (bp->b_flags&B_WANTED)
3231900Swnj 		wakeup((caddr_t)bp);
3243244Swnj 	bp->b_flags &= B_ERROR;
3251900Swnj }
3261900Swnj 
3273244Swnj /*
3283244Swnj  * Queue a tape operation.
3293244Swnj  */
3301900Swnj tsstrategy(bp)
3313244Swnj 	register struct buf *bp;
3321900Swnj {
3333244Swnj 	int tsunit = TSUNIT(bp->b_dev);
3343244Swnj 	register struct uba_ctlr *um;
3353327Swnj 	register struct buf *dp;
3365438Sroot 	register int s;
3371900Swnj 
3383244Swnj 	/*
3393244Swnj 	 * Put transfer at end of controller queue
3403244Swnj 	 */
3411900Swnj 	bp->av_forw = NULL;
3423244Swnj 	um = tsdinfo[tsunit]->ui_mi;
3435438Sroot 	s = spl5();
3445693Sroot 	dp = &tsutab[tsunit];
3453327Swnj 	if (dp->b_actf == NULL)
3463327Swnj 		dp->b_actf = bp;
3471900Swnj 	else
3483327Swnj 		dp->b_actl->av_forw = bp;
3493327Swnj 	dp->b_actl = bp;
3503327Swnj 	um->um_tab.b_actf = um->um_tab.b_actl = dp;
3513244Swnj 	/*
3523244Swnj 	 * If the controller is not busy, get
3533244Swnj 	 * it going.
3543244Swnj 	 */
3553244Swnj 	if (um->um_tab.b_active == 0)
3563244Swnj 		tsstart(um);
3575438Sroot 	splx(s);
3581900Swnj }
3591900Swnj 
3603244Swnj /*
3613244Swnj  * Start activity on a ts controller.
3623244Swnj  */
3633244Swnj tsstart(um)
3643244Swnj 	register struct uba_ctlr *um;
3651900Swnj {
3661900Swnj 	register struct buf *bp;
3675693Sroot 	register struct tsdevice *addr = (struct tsdevice *)um->um_addr;
3683244Swnj 	register struct ts_softc *sc;
3693244Swnj 	register struct ts_cmd *tc;
3703244Swnj 	register struct uba_device *ui;
3713244Swnj 	int tsunit, cmd;
3721900Swnj 	daddr_t blkno;
3731900Swnj 
3743244Swnj 	/*
3753244Swnj 	 * Start the controller if there is something for it to do.
3763244Swnj 	 */
3773244Swnj loop:
3783327Swnj 	if ((bp = um->um_tab.b_actf->b_actf) == NULL)
3791900Swnj 		return;
3803244Swnj 	tsunit = TSUNIT(bp->b_dev);
3813244Swnj 	ui = tsdinfo[tsunit];
3823244Swnj 	sc = &ts_softc[tsunit];
3833244Swnj 	tc = &sc->sc_cmd;
3843244Swnj 	/*
3853244Swnj 	 * Default is that last command was NOT a write command;
3863244Swnj 	 * if we do a write command we will notice this in tsintr().
3873244Swnj 	 */
3883656Swnj 	sc->sc_lastiow = 0;
3893244Swnj 	if (sc->sc_openf < 0 || (addr->tssr&TS_OFL)) {
3903244Swnj 		/*
3913244Swnj 		 * Have had a hard error on a non-raw tape
3923244Swnj 		 * or the tape unit is now unavailable
3933244Swnj 		 * (e.g. taken off line).
3943244Swnj 		 */
3953244Swnj 		bp->b_flags |= B_ERROR;
3963244Swnj 		goto next;
3973244Swnj 	}
3983244Swnj 	if (bp == &ctsbuf[TSUNIT(bp->b_dev)]) {
3993244Swnj 		/*
4003244Swnj 		 * Execute control operation with the specified count.
4013244Swnj 		 */
4023244Swnj 		um->um_tab.b_active =
4033244Swnj 		    bp->b_command == TS_REW ? SREW : SCOM;
4043244Swnj 		tc->c_repcnt = bp->b_repcnt;
4053244Swnj 		goto dobpcmd;
4063244Swnj 	}
4073244Swnj 	/*
4083244Swnj 	 * The following checks handle boundary cases for operation
4093244Swnj 	 * on non-raw tapes.  On raw tapes the initialization of
4103244Swnj 	 * sc->sc_nxrec by tsphys causes them to be skipped normally
4113244Swnj 	 * (except in the case of retries).
4123244Swnj 	 */
4137383Ssam 	if (bdbtofsb(bp->b_blkno) > sc->sc_nxrec) {
4143244Swnj 		/*
4153244Swnj 		 * Can't read past known end-of-file.
4163244Swnj 		 */
4173244Swnj 		bp->b_flags |= B_ERROR;
4183244Swnj 		bp->b_error = ENXIO;
4193244Swnj 		goto next;
4203244Swnj 	}
4217383Ssam 	if (bdbtofsb(bp->b_blkno) == sc->sc_nxrec &&
4223244Swnj 	    bp->b_flags&B_READ) {
4233244Swnj 		/*
4243244Swnj 		 * Reading at end of file returns 0 bytes.
4253244Swnj 		 */
4263244Swnj 		bp->b_resid = bp->b_bcount;
4273244Swnj 		clrbuf(bp);
4283244Swnj 		goto next;
4293244Swnj 	}
4303244Swnj 	if ((bp->b_flags&B_READ) == 0)
4313244Swnj 		/*
4323244Swnj 		 * Writing sets EOF
4333244Swnj 		 */
4347383Ssam 		sc->sc_nxrec = bdbtofsb(bp->b_blkno) + 1;
4353244Swnj 	/*
4363244Swnj 	 * If the data transfer command is in the correct place,
4373244Swnj 	 * set up all the registers except the csr, and give
4383244Swnj 	 * control over to the UNIBUS adapter routines, to
4393244Swnj 	 * wait for resources to start the i/o.
4403244Swnj 	 */
4417383Ssam 	if ((blkno = sc->sc_blkno) == bdbtofsb(bp->b_blkno)) {
4423244Swnj 		tc->c_size = bp->b_bcount;
4433244Swnj 		if ((bp->b_flags&B_READ) == 0)
4443244Swnj 			cmd = TS_WCOM;
4451900Swnj 		else
4463244Swnj 			cmd = TS_RCOM;
4473244Swnj 		if (um->um_tab.b_errcnt)
4483244Swnj 			cmd |= TS_RETRY;
4493244Swnj 		um->um_tab.b_active = SIO;
4503327Swnj 		tc->c_cmd = TS_ACK | TS_CVC | TS_IE | cmd;
4513244Swnj 		(void) ubago(ui);
4523244Swnj 		return;
4533244Swnj 	}
4543244Swnj 	/*
4553244Swnj 	 * Tape positioned incorrectly;
4563244Swnj 	 * set to seek forwards or backwards to the correct spot.
4573244Swnj 	 * This happens for raw tapes only on error retries.
4583244Swnj 	 */
4593244Swnj 	um->um_tab.b_active = SSEEK;
4607383Ssam 	if (blkno < bdbtofsb(bp->b_blkno)) {
4613244Swnj 		bp->b_command = TS_SFORW;
4627383Ssam 		tc->c_repcnt = bdbtofsb(bp->b_blkno) - blkno;
4631900Swnj 	} else {
4643244Swnj 		bp->b_command = TS_SREV;
4657383Ssam 		tc->c_repcnt = blkno - bdbtofsb(bp->b_blkno);
4661900Swnj 	}
4673244Swnj dobpcmd:
4683244Swnj 	/*
4693244Swnj 	 * Do the command in bp.
4703244Swnj 	 */
4713327Swnj 	tc->c_cmd = TS_ACK | TS_CVC | TS_IE | bp->b_command;
4723244Swnj 	addr->tsdb = sc->sc_uba;
4731900Swnj 	return;
4741900Swnj 
4753244Swnj next:
4763244Swnj 	/*
4773244Swnj 	 * Done with this operation due to error or
4783244Swnj 	 * the fact that it doesn't do anything.
4793244Swnj 	 * Release UBA resources (if any), dequeue
4803244Swnj 	 * the transfer and continue processing this slave.
4813244Swnj 	 */
4823244Swnj 	if (um->um_ubinfo)
4833244Swnj 		ubadone(um);
4843244Swnj 	um->um_tab.b_errcnt = 0;
4853327Swnj 	um->um_tab.b_actf->b_actf = bp->av_forw;
4861900Swnj 	iodone(bp);
4871900Swnj 	goto loop;
4881900Swnj }
4891900Swnj 
4903244Swnj /*
4913244Swnj  * The UNIBUS resources we needed have been
4923244Swnj  * allocated to us; start the device.
4933244Swnj  */
4943244Swnj tsdgo(um)
4953244Swnj 	register struct uba_ctlr *um;
4961900Swnj {
4975693Sroot 	register struct tsdevice *addr = (struct tsdevice *)um->um_addr;
4983244Swnj 	register struct ts_softc *sc = &ts_softc[um->um_ctlr];
4993327Swnj 	register int i;
5003244Swnj 
5013327Swnj 	i = um->um_ubinfo & 0777777;
5023327Swnj 	sc->sc_cmd.c_loba = i;
5033327Swnj 	sc->sc_cmd.c_hiba = (i>>16)&3;
5043244Swnj 	addr->tsdb = sc->sc_uba;
5053244Swnj }
5063244Swnj 
5073244Swnj /*
5083244Swnj  * Ts interrupt routine.
5093244Swnj  */
5103244Swnj /*ARGSUSED*/
5113244Swnj tsintr(ts11)
5123244Swnj 	int ts11;
5133244Swnj {
5141900Swnj 	register struct buf *bp;
5153244Swnj 	register struct uba_ctlr *um = tsminfo[ts11];
5165693Sroot 	register struct tsdevice *addr;
5173244Swnj 	register struct ts_softc *sc;
5183244Swnj 	int tsunit;
5193244Swnj 	register state;
5201900Swnj 
5213327Swnj 	if ((bp = um->um_tab.b_actf->b_actf) == NULL)
5221900Swnj 		return;
5233244Swnj 	tsunit = TSUNIT(bp->b_dev);
5245693Sroot 	addr = (struct tsdevice *)tsdinfo[tsunit]->ui_addr;
5253244Swnj 	/*
5263244Swnj 	 * If last command was a rewind, and tape is still
5273244Swnj 	 * rewinding, wait for the rewind complete interrupt.
5283244Swnj 	 *
5293244Swnj 	 * SHOULD NEVER GET AN INTERRUPT IN THIS STATE.
5303244Swnj 	 */
5313244Swnj 	if (um->um_tab.b_active == SREW) {
5323244Swnj 		um->um_tab.b_active = SCOM;
5333244Swnj 		if ((addr->tssr&TS_SSR) == 0)
5343244Swnj 			return;
5353244Swnj 	}
5363244Swnj 	/*
5373244Swnj 	 * An operation completed... record status
5383244Swnj 	 */
5393244Swnj 	sc = &ts_softc[tsunit];
5403244Swnj 	if ((bp->b_flags & B_READ) == 0)
5413244Swnj 		sc->sc_lastiow = 1;
5423244Swnj 	state = um->um_tab.b_active;
5433244Swnj 	um->um_tab.b_active = 0;
5443244Swnj 	/*
5453244Swnj 	 * Check for errors.
5463244Swnj 	 */
5473244Swnj 	if (addr->tssr&TS_SC) {
5483244Swnj 		switch (addr->tssr & TS_TC) {
5493244Swnj 		case TS_UNREC:		/* unrecoverable */
5503244Swnj 		case TS_FATAL:		/* fatal error */
5513244Swnj 		case TS_ATTN:		/* attention (shouldn't happen) */
5523244Swnj 		case TS_RECNM:		/* recoverable, no motion */
5533244Swnj 			break;
5541900Swnj 
5553244Swnj 		case TS_SUCC:		/* success termination */
5563244Swnj 			printf("ts%d: success\n", TSUNIT(minor(bp->b_dev)));
5573244Swnj 			goto ignoreerr;
5581900Swnj 
5593244Swnj 		case TS_ALERT:		/* tape status alert */
5603244Swnj 			/*
5613244Swnj 			 * If we hit the end of the tape file,
5623244Swnj 			 * update our position.
5633244Swnj 			 */
5643244Swnj 			if (sc->sc_sts.s_xs0 & (TS_TMK|TS_EOT)) {
5653244Swnj 				tsseteof(bp);		/* set blkno and nxrec */
5663244Swnj 				state = SCOM;		/* force completion */
5673244Swnj 				/*
5683244Swnj 				 * Stuff bc so it will be unstuffed correctly
5693244Swnj 				 * later to get resid.
5703244Swnj 				 */
5713244Swnj 				sc->sc_sts.s_rbpcr = bp->b_bcount;
5723244Swnj 				goto opdone;
5733244Swnj 			}
5743244Swnj 			/*
5753244Swnj 			 * If we were reading raw tape and the record was too long
5763244Swnj 			 * or too short, then we don't consider this an error.
5773244Swnj 			 */
5783244Swnj 			if (bp == &rtsbuf[TSUNIT(bp->b_dev)] && (bp->b_flags&B_READ) &&
5793244Swnj 			    sc->sc_sts.s_xs0&(TS_RLS|TS_RLL))
5803244Swnj 				goto ignoreerr;
5813244Swnj 		case TS_RECOV:		/* recoverable, tape moved */
5823244Swnj 			/*
5833244Swnj 			 * If this was an i/o operation retry up to 8 times.
5843244Swnj 			 */
5853244Swnj 			if (state==SIO) {
5863244Swnj 				if (++um->um_tab.b_errcnt < 7) {
5873244Swnj 					ubadone(um);
5883244Swnj 					goto opcont;
5893244Swnj 				} else
5903244Swnj 					sc->sc_blkno++;
5913244Swnj 			} else {
5923244Swnj 				/*
5933244Swnj 				 * Non-i/o errors on non-raw tape
5943244Swnj 				 * cause it to close.
5953244Swnj 				 */
5963244Swnj 				if (sc->sc_openf>0 && bp != &rtsbuf[TSUNIT(bp->b_dev)])
5973244Swnj 					sc->sc_openf = -1;
5983244Swnj 			}
5991900Swnj 			break;
6003244Swnj 
6013244Swnj 		case TS_REJECT:		/* function reject */
6023244Swnj 			if (state == SIO && sc->sc_sts.s_xs0 & TS_WLE)
60318322Sralph 				tprintf(sc->sc_ttyp, "ts%d: write locked\n",
60418322Sralph 				    TSUNIT(bp->b_dev));
6053244Swnj 			if ((sc->sc_sts.s_xs0 & TS_ONL) == 0)
60618322Sralph 				tprintf(sc->sc_ttyp, "ts%d: offline\n",
60718322Sralph 				    TSUNIT(bp->b_dev));
6081900Swnj 			break;
6091900Swnj 		}
6103244Swnj 		/*
6113244Swnj 		 * Couldn't recover error
6123244Swnj 		 */
61318322Sralph 		tprintf(sc->sc_ttyp, "ts%d: hard error bn%d xs0=%b",
61418322Sralph 		    TSUNIT(bp->b_dev), bp->b_blkno, sc->sc_sts.s_xs0, TSXS0_BITS);
6153647Swnj 		if (sc->sc_sts.s_xs1)
61618322Sralph 			tprintf(sc->sc_ttyp, " xs1=%b", sc->sc_sts.s_xs1,
61718322Sralph 			    TSXS1_BITS);
6183647Swnj 		if (sc->sc_sts.s_xs2)
61918322Sralph 			tprintf(sc->sc_ttyp, " xs2=%b", sc->sc_sts.s_xs2,
62018322Sralph 			    TSXS2_BITS);
6213647Swnj 		if (sc->sc_sts.s_xs3)
62218322Sralph 			tprintf(sc->sc_ttyp, " xs3=%b", sc->sc_sts.s_xs3,
62318322Sralph 			    TSXS3_BITS);
62418322Sralph 		tprintf(sc->sc_ttyp, "\n");
6253244Swnj 		bp->b_flags |= B_ERROR;
6263244Swnj 		goto opdone;
6273244Swnj 	}
6283244Swnj 	/*
6293244Swnj 	 * Advance tape control FSM.
6303244Swnj 	 */
6313244Swnj ignoreerr:
6323244Swnj 	switch (state) {
6331900Swnj 
6343244Swnj 	case SIO:
6353244Swnj 		/*
6363244Swnj 		 * Read/write increments tape block number
6373244Swnj 		 */
6383244Swnj 		sc->sc_blkno++;
6393244Swnj 		goto opdone;
6401900Swnj 
6411900Swnj 	case SCOM:
6423244Swnj 		/*
6433244Swnj 		 * For forward/backward space record update current position.
6443244Swnj 		 */
6453244Swnj 		if (bp == &ctsbuf[TSUNIT(bp->b_dev)])
6463244Swnj 		switch (bp->b_command) {
6471900Swnj 
6483244Swnj 		case TS_SFORW:
6493244Swnj 			sc->sc_blkno += bp->b_repcnt;
6503244Swnj 			break;
6511900Swnj 
6523244Swnj 		case TS_SREV:
6533244Swnj 			sc->sc_blkno -= bp->b_repcnt;
6543244Swnj 			break;
6553244Swnj 		}
6563244Swnj 		goto opdone;
6573244Swnj 
6583244Swnj 	case SSEEK:
6597383Ssam 		sc->sc_blkno = bdbtofsb(bp->b_blkno);
6603244Swnj 		goto opcont;
6613244Swnj 
6621900Swnj 	default:
6633244Swnj 		panic("tsintr");
6641900Swnj 	}
6653244Swnj opdone:
6663244Swnj 	/*
6673244Swnj 	 * Reset error count and remove
6683244Swnj 	 * from device queue.
6693244Swnj 	 */
6703244Swnj 	um->um_tab.b_errcnt = 0;
6713327Swnj 	um->um_tab.b_actf->b_actf = bp->av_forw;
6723244Swnj 	bp->b_resid = sc->sc_sts.s_rbpcr;
6733244Swnj 	ubadone(um);
6743244Swnj 	iodone(bp);
6753327Swnj 	if (um->um_tab.b_actf->b_actf == 0)
6763244Swnj 		return;
6773244Swnj opcont:
6783244Swnj 	tsstart(um);
6793244Swnj }
6803244Swnj 
6813244Swnj tsseteof(bp)
6823244Swnj 	register struct buf *bp;
6833244Swnj {
6843244Swnj 	register int tsunit = TSUNIT(bp->b_dev);
6853244Swnj 	register struct ts_softc *sc = &ts_softc[tsunit];
6863244Swnj 
6873244Swnj 	if (bp == &ctsbuf[TSUNIT(bp->b_dev)]) {
6887383Ssam 		if (sc->sc_blkno > bdbtofsb(bp->b_blkno)) {
6893244Swnj 			/* reversing */
6907383Ssam 			sc->sc_nxrec = bdbtofsb(bp->b_blkno) - sc->sc_sts.s_rbpcr;
6913244Swnj 			sc->sc_blkno = sc->sc_nxrec;
6923244Swnj 		} else {
6933244Swnj 			/* spacing forward */
6947383Ssam 			sc->sc_blkno = bdbtofsb(bp->b_blkno) + sc->sc_sts.s_rbpcr;
6953244Swnj 			sc->sc_nxrec = sc->sc_blkno - 1;
6961900Swnj 		}
6973244Swnj 		return;
6983244Swnj 	}
6993244Swnj 	/* eof on read */
7007383Ssam 	sc->sc_nxrec = bdbtofsb(bp->b_blkno);
7011900Swnj }
7021900Swnj 
7037733Sroot tsread(dev, uio)
7043244Swnj 	dev_t dev;
7057733Sroot 	struct uio *uio;
7061900Swnj {
7078164Sroot 	int errno;
7083244Swnj 
7098164Sroot 	errno = tsphys(dev, uio);
7108164Sroot 	if (errno)
7118164Sroot 		return (errno);
7128164Sroot 	return (physio(tsstrategy, &rtsbuf[TSUNIT(dev)], dev, B_READ, minphys, uio));
7131900Swnj }
7141900Swnj 
7157841Sroot tswrite(dev, uio)
7163244Swnj 	dev_t dev;
7177841Sroot 	struct uio *uio;
7181900Swnj {
7198164Sroot 	int errno;
7203244Swnj 
7218164Sroot 	errno = tsphys(dev, uio);
7228164Sroot 	if (errno)
7238164Sroot 		return (errno);
7248164Sroot 	return (physio(tsstrategy, &rtsbuf[TSUNIT(dev)], dev, B_WRITE, minphys, uio));
7251900Swnj }
7261900Swnj 
7273244Swnj /*
7283244Swnj  * Check that a raw device exists.
7293244Swnj  * If it does, set up sc_blkno and sc_nxrec
7303244Swnj  * so that the tape will appear positioned correctly.
7313244Swnj  */
7327733Sroot tsphys(dev, uio)
7333244Swnj 	dev_t dev;
7347733Sroot 	struct uio *uio;
7351900Swnj {
7363244Swnj 	register int tsunit = TSUNIT(dev);
7373244Swnj 	register daddr_t a;
7383244Swnj 	register struct ts_softc *sc;
7393244Swnj 	register struct uba_device *ui;
7401900Swnj 
7417841Sroot 	if (tsunit >= NTS || (ui=tsdinfo[tsunit]) == 0 || ui->ui_alive == 0)
7427733Sroot 		return (ENXIO);
7433244Swnj 	sc = &ts_softc[tsunit];
7447841Sroot 	a = bdbtofsb(uio->uio_offset >> 9);
7453244Swnj 	sc->sc_blkno = a;
7463244Swnj 	sc->sc_nxrec = a + 1;
7477733Sroot 	return (0);
7481900Swnj }
7491918Swnj 
7503244Swnj tsreset(uban)
7513244Swnj 	int uban;
7521918Swnj {
7533244Swnj 	register struct uba_ctlr *um;
7545693Sroot 	register struct uba_device *ui;
7555693Sroot 	register struct buf *dp;
7563244Swnj 	register ts11;
7571918Swnj 
7583244Swnj 	for (ts11 = 0; ts11 < NTS; ts11++) {
7593244Swnj 		if ((um = tsminfo[ts11]) == 0 || um->um_alive == 0 ||
7603244Swnj 		   um->um_ubanum != uban)
7613244Swnj 			continue;
7623244Swnj 		printf(" ts%d", ts11);
7633244Swnj 		um->um_tab.b_active = 0;
7643244Swnj 		um->um_tab.b_actf = um->um_tab.b_actl = 0;
7655693Sroot 		if (ts_softc[ts11].sc_openf > 0)
7665693Sroot 			ts_softc[ts11].sc_openf = -1;
7673244Swnj 		if (um->um_ubinfo) {
7683244Swnj 			printf("<%d>", (um->um_ubinfo>>28)&0xf);
7699356Ssam 			um->um_ubinfo = 0;
7703244Swnj 		}
7715693Sroot 		if ((ui = tsdinfo[ts11]) && ui->ui_mi == um && ui->ui_alive) {
7725693Sroot 			dp = &tsutab[ts11];
7735693Sroot 			dp->b_active = 0;
7745693Sroot 			dp->b_forw = 0;
7755693Sroot 			if (um->um_tab.b_actf == NULL)
7765693Sroot 				um->um_tab.b_actf = dp;
7775693Sroot 			else
7785693Sroot 				um->um_tab.b_actl->b_forw = dp;
7795693Sroot 			um->um_tab.b_actl = dp;
7805693Sroot 		}
78117432Skarels 		ts_softc[ts11].sc_mapped = 0;
7823989Sroot 		(void) tsinit(ts11);
7833244Swnj 		tsstart(um);
7841918Swnj 	}
7851918Swnj }
7861918Swnj 
7873244Swnj /*ARGSUSED*/
7887633Ssam tsioctl(dev, cmd, data, flag)
7897633Ssam 	caddr_t data;
7903244Swnj 	dev_t dev;
7911918Swnj {
7923244Swnj 	int tsunit = TSUNIT(dev);
7933244Swnj 	register struct ts_softc *sc = &ts_softc[tsunit];
7943244Swnj 	register struct buf *bp = &ctsbuf[TSUNIT(dev)];
7953244Swnj 	register callcount;
7963244Swnj 	int fcount;
7977633Ssam 	struct mtop *mtop;
7987633Ssam 	struct mtget *mtget;
7993244Swnj 	/* we depend of the values and order of the MT codes here */
8003244Swnj 	static tsops[] =
8013656Swnj 	 {TS_WEOF,TS_SFORWF,TS_SREVF,TS_SFORW,TS_SREV,TS_REW,TS_OFFL,TS_SENSE};
8021918Swnj 
8033244Swnj 	switch (cmd) {
8047633Ssam 
8053244Swnj 	case MTIOCTOP:	/* tape operation */
8067633Ssam 		mtop = (struct mtop *)data;
8078575Sroot 		switch (mtop->mt_op) {
8087633Ssam 
8093244Swnj 		case MTWEOF:
8107633Ssam 			callcount = mtop->mt_count;
8113244Swnj 			fcount = 1;
8123244Swnj 			break;
8137633Ssam 
8143244Swnj 		case MTFSF: case MTBSF:
8153244Swnj 		case MTFSR: case MTBSR:
8163244Swnj 			callcount = 1;
8177633Ssam 			fcount = mtop->mt_count;
8183244Swnj 			break;
8197633Ssam 
8203244Swnj 		case MTREW: case MTOFFL: case MTNOP:
8213244Swnj 			callcount = 1;
8223244Swnj 			fcount = 1;
8233244Swnj 			break;
8247633Ssam 
8253244Swnj 		default:
8268575Sroot 			return (ENXIO);
8273244Swnj 		}
8288575Sroot 		if (callcount <= 0 || fcount <= 0)
8298575Sroot 			return (EINVAL);
8303244Swnj 		while (--callcount >= 0) {
8317633Ssam 			tscommand(dev, tsops[mtop->mt_op], fcount);
8327633Ssam 			if ((mtop->mt_op == MTFSR || mtop->mt_op == MTBSR) &&
8338611Sroot 			    bp->b_resid)
8348575Sroot 				return (EIO);
8353244Swnj 			if ((bp->b_flags&B_ERROR) || sc->sc_sts.s_xs0&TS_BOT)
8363244Swnj 				break;
8373244Swnj 		}
8388649Sroot 		return (geterror(bp));
8397633Ssam 
8403244Swnj 	case MTIOCGET:
8417633Ssam 		mtget = (struct mtget *)data;
8427633Ssam 		mtget->mt_dsreg = 0;
8437633Ssam 		mtget->mt_erreg = sc->sc_sts.s_xs0;
8447633Ssam 		mtget->mt_resid = sc->sc_resid;
8457633Ssam 		mtget->mt_type = MT_ISTS;
8468575Sroot 		break;
8477633Ssam 
8483244Swnj 	default:
8498575Sroot 		return (ENXIO);
8503244Swnj 	}
8518575Sroot 	return (0);
8521918Swnj }
8531918Swnj 
8543244Swnj #define	DBSIZE	20
8553244Swnj 
8563244Swnj tsdump()
8571918Swnj {
8583244Swnj 	register struct uba_device *ui;
8593244Swnj 	register struct uba_regs *up;
8605693Sroot 	register struct tsdevice *addr;
8613244Swnj 	int blk, num;
8623244Swnj 	int start;
8631918Swnj 
8643244Swnj 	start = 0;
8653244Swnj 	num = maxfree;
8663244Swnj #define	phys(a,b)	((b)((int)(a)&0x7fffffff))
8673244Swnj 	if (tsdinfo[0] == 0)
8683244Swnj 		return (ENXIO);
8693244Swnj 	ui = phys(tsdinfo[0], struct uba_device *);
8703244Swnj 	up = phys(ui->ui_hd, struct uba_hd *)->uh_physuba;
8718703Sroot 	ubainit(up);
8723244Swnj 	DELAY(1000000);
8735693Sroot 	addr = (struct tsdevice *)ui->ui_physaddr;
8743244Swnj 	addr->tssr = 0;
8753244Swnj 	tswait(addr);
8763244Swnj 	while (num > 0) {
8773244Swnj 		blk = num > DBSIZE ? DBSIZE : num;
8783244Swnj 		tsdwrite(start, blk, addr, up);
8793244Swnj 		start += blk;
8803244Swnj 		num -= blk;
8813244Swnj 	}
8823244Swnj 	tseof(addr);
8833244Swnj 	tseof(addr);
8843244Swnj 	tswait(addr);
8853244Swnj 	if (addr->tssr&TS_SC)
8863244Swnj 		return (EIO);
8873244Swnj 	addr->tssr = 0;
8883244Swnj 	tswait(addr);
8893244Swnj 	return (0);
8901918Swnj }
8911918Swnj 
8923244Swnj tsdwrite(dbuf, num, addr, up)
8938635Sroot 	register int dbuf, num;
8945693Sroot 	register struct tsdevice *addr;
8953244Swnj 	struct uba_regs *up;
8961918Swnj {
8973244Swnj 	register struct pte *io;
8983244Swnj 	register int npf;
8991918Swnj 
9003244Swnj 	tswait(addr);
9013244Swnj 	io = up->uba_map;
9023244Swnj 	npf = num+1;
9033244Swnj 	while (--npf != 0)
9043244Swnj 		 *(int *)io++ = (dbuf++ | (1<<UBAMR_DPSHIFT) | UBAMR_MRV);
9053244Swnj 	*(int *)io = 0;
9063244Swnj #ifdef notyet
9073244Swnj 	addr->tsbc = -(num*NBPG);
9083244Swnj 	addr->tsba = 0;
9093244Swnj 	addr->tscs = TS_WCOM | TM_GO;
9103244Swnj #endif
9111918Swnj }
9121918Swnj 
9133244Swnj tswait(addr)
9145693Sroot 	register struct tsdevice *addr;
9151918Swnj {
9163244Swnj 	register s;
9171918Swnj 
9183244Swnj 	do
9193244Swnj 		s = addr->tssr;
9203244Swnj 	while ((s & TS_SSR) == 0);
9211918Swnj }
9221918Swnj 
9233244Swnj tseof(addr)
9245693Sroot 	struct tsdevice *addr;
9251918Swnj {
9261918Swnj 
9273244Swnj 	tswait(addr);
9283244Swnj #ifdef notyet
9293244Swnj 	addr->tscs = TS_WEOF | TM_GO;
9303244Swnj #endif
9311918Swnj }
9321900Swnj #endif
933