xref: /csrg-svn/sys/tahoe/vba/cy.c (revision 31737)
1*31737Skarels /*	cy.c	1.14	87/06/30	*/
224000Ssam 
325979Ssam #include "yc.h"
425675Ssam #if NCY > 0
524000Ssam /*
625675Ssam  * Cipher Tapemaster driver.
724000Ssam  */
830371Skarels #define CYDEBUG
930371Skarels #ifdef	CYDEBUG
1025675Ssam int	cydebug = 0;
1130371Skarels #define	dlog(params)	if (cydebug) log params
1230371Skarels #else
1330371Skarels #define dlog(params)	/* */
1430371Skarels #endif
1524000Ssam 
1625675Ssam #include "param.h"
1725675Ssam #include "systm.h"
1825675Ssam #include "vm.h"
1925675Ssam #include "buf.h"
2025675Ssam #include "file.h"
2125675Ssam #include "dir.h"
2225675Ssam #include "user.h"
2325675Ssam #include "proc.h"
2425675Ssam #include "signal.h"
2525675Ssam #include "uio.h"
2625675Ssam #include "ioctl.h"
2725675Ssam #include "mtio.h"
2825675Ssam #include "errno.h"
2925675Ssam #include "cmap.h"
3025979Ssam #include "kernel.h"
3125979Ssam #include "syslog.h"
3230294Ssam #include "tty.h"
3324000Ssam 
3429952Skarels #include "../tahoe/cpu.h"
3529952Skarels #include "../tahoe/mtpr.h"
3629952Skarels #include "../tahoe/pte.h"
3729952Skarels 
3825675Ssam #include "../tahoevba/vbavar.h"
3925979Ssam #define	CYERROR
4025675Ssam #include "../tahoevba/cyreg.h"
4124000Ssam 
4225979Ssam /*
4325979Ssam  * There is a ccybuf per tape controller.
4425979Ssam  * It is used as the token to pass to the internal routines
4525979Ssam  * to execute tape ioctls, and also acts as a lock on the slaves
4625979Ssam  * on the controller, since there is only one per controller.
4725979Ssam  * In particular, when the tape is rewinding on close we release
4825979Ssam  * the user process but any further attempts to use the tape drive
4925979Ssam  * before the rewind completes will hang waiting for ccybuf.
5025979Ssam  */
5125979Ssam struct	buf ccybuf[NCY];
5224000Ssam 
5325979Ssam /*
5425979Ssam  * Raw tape operations use rcybuf.  The driver notices when
5525979Ssam  * rcybuf is being used and allows the user program to contine
5625979Ssam  * after errors and read records not of the standard length.
5725979Ssam  */
5825979Ssam struct	buf rcybuf[NCY];
5924000Ssam 
6025979Ssam int	cyprobe(), cyslave(), cyattach();
6125979Ssam struct	buf ycutab[NYC];
6225979Ssam short	yctocy[NYC];
6325675Ssam struct	vba_ctlr *cyminfo[NCY];
6425979Ssam struct	vba_device *ycdinfo[NYC];
6525857Ssam long	cystd[] = { 0 };
6625857Ssam struct	vba_driver cydriver =
6725979Ssam    { cyprobe, cyslave, cyattach, 0, cystd, "yc", ycdinfo, "cy", cyminfo };
6824000Ssam 
6925979Ssam /* bits in minor device */
7025979Ssam #define	YCUNIT(dev)	(minor(dev)&03)
7125979Ssam #define	CYUNIT(dev)	(yctocy[YCUNIT(dev)])
7225979Ssam #define	T_NOREWIND	0x04
7330371Skarels #define	T_1600BPI	0x00		/* pseudo */
7430371Skarels #define	T_3200BPI	0x08		/* unused */
7525979Ssam 
7625979Ssam #define	INF	1000000L		/* close to infinity */
7725979Ssam 
7824000Ssam /*
7925979Ssam  * Software state and shared command areas per controller.
8025979Ssam  *
8130719Skarels  * The i/o intermediate buffer must be allocated in startup()
8230719Skarels  * so its address will fit in 20-bits (YECH!!!!!!!!!!!!!!).
8324000Ssam  */
8425979Ssam struct cy_softc {
8525979Ssam 	int	cy_bs;		/* controller's buffer size */
8625979Ssam 	struct	cyscp *cy_scp;	/* system configuration block address */
8725979Ssam 	struct	cyccb cy_ccb;	/* channel control block */
8825979Ssam 	struct	cyscb cy_scb;	/* system configuration block */
8925979Ssam 	struct	cytpb cy_tpb;	/* tape parameter block */
9025979Ssam 	struct	cytpb cy_nop;	/* nop parameter block for cyintr */
9130719Skarels 	struct	vb_buf cy_rbuf;	/* vba resources */
9225979Ssam } cy_softc[NCY];
9324000Ssam 
9425979Ssam /*
9525979Ssam  * Software state per tape transport.
9625979Ssam  */
9725979Ssam struct	yc_softc {
9825979Ssam 	char	yc_openf;	/* lock against multiple opens */
9925979Ssam 	char	yc_lastiow;	/* last operation was a write */
10025979Ssam 	short	yc_tact;	/* timeout is active */
10125979Ssam 	long	yc_timo;	/* time until timeout expires */
10225979Ssam 	u_short	yc_control;	/* copy of last tpcb.tpcontrol */
10325979Ssam 	u_short	yc_status;	/* copy of last tpcb.tpstatus */
10425979Ssam 	u_short	yc_resid;	/* copy of last bc */
10525979Ssam 	u_short	yc_dens;	/* prototype control word with density info */
10625979Ssam 	struct	tty *yc_ttyp;	/* user's tty for errors */
10725979Ssam 	daddr_t	yc_blkno;	/* block number, for block device tape */
10825979Ssam 	daddr_t	yc_nxrec;	/* position of end of tape, if known */
10930371Skarels 	int	yc_blksize;	/* current tape blocksize estimate */
11030371Skarels 	int	yc_blks;	/* number of I/O operations since open */
11130371Skarels 	int	yc_softerrs;	/* number of soft I/O errors since open */
11225979Ssam } yc_softc[NYC];
11324000Ssam 
11424000Ssam /*
11525979Ssam  * States for vm->um_tab.b_active, the per controller state flag.
11625979Ssam  * This is used to sequence control in the driver.
11724000Ssam  */
11825979Ssam #define	SSEEK	1		/* seeking */
11925979Ssam #define	SIO	2		/* doing seq i/o */
12025979Ssam #define	SCOM	3		/* sending control command */
12125979Ssam #define	SREW	4		/* sending a rewind */
12225979Ssam #define	SERASE	5		/* erase inter-record gap */
12325979Ssam #define	SERASED	6		/* erased inter-record gap */
12424000Ssam 
12525979Ssam /* there's no way to figure these out dynamically? -- yech */
12625979Ssam struct	cyscp *cyscp[] =
12725979Ssam     { (struct cyscp *)0xc0000c06, (struct cyscp *)0xc0000c16 };
12825979Ssam #define	NCYSCP	(sizeof (cyscp) / sizeof (cyscp[0]))
12925979Ssam 
13025857Ssam cyprobe(reg, vm)
13125857Ssam 	caddr_t reg;
13225857Ssam 	struct vba_ctlr *vm;
13325675Ssam {
13425857Ssam 	register br, cvec;			/* must be r12, r11 */
13530371Skarels 	register struct cy_softc *cy;
13630371Skarels 	int ctlr = vm->um_ctlr;
13725675Ssam 
13830294Ssam #ifdef lint
13930294Ssam 	br = 0; cvec = br; br = cvec;
14030294Ssam 	cyintr(0);
14130294Ssam #endif
14225857Ssam 	if (badcyaddr(reg+1))
14325675Ssam 		return (0);
14430371Skarels 	if (ctlr > NCYSCP || cyscp[ctlr] == 0)		/* XXX */
14530371Skarels 		return (0);
14630371Skarels 	cy = &cy_softc[ctlr];
14730371Skarels 	cy->cy_scp = cyscp[ctlr];			/* XXX */
14825979Ssam 	/*
14925979Ssam 	 * Tapemaster controller must have interrupt handler
15025979Ssam 	 * disable interrupt, so we'll just kludge things
15125979Ssam 	 * (stupid multibus non-vectored interrupt crud).
15225979Ssam 	 */
15330371Skarels 	if (cyinit(ctlr, reg)) {
15430371Skarels 		uncache(&cy->cy_tpb.tpcount);
15530371Skarels 		cy->cy_bs = htoms(cy->cy_tpb.tpcount);
15630371Skarels 		/*
15730371Skarels 		 * Setup nop parameter block for clearing interrupts.
15830371Skarels 		 */
15930371Skarels 		cy->cy_nop.tpcmd = CY_NOP;
16030371Skarels 		cy->cy_nop.tpcontrol = 0;
16130371Skarels 		/*
16230371Skarels 		 * Allocate page tables.
16330371Skarels 		 */
16430719Skarels 		if (cybuf == 0) {
16530719Skarels 			printf("no cy buffer!!!\n");
16630719Skarels 			return (0);
16730719Skarels 		}
16830719Skarels 		cy->cy_rbuf.vb_rawbuf = cybuf + ctlr * CYMAXIO;
169*31737Skarels 		if (vbainit(&cy->cy_rbuf, CYMAXIO, VB_20BIT) == 0) {
170*31737Skarels 			printf("cy%d: vbainit failed\n", ctlr);
171*31737Skarels 			return (0);
172*31737Skarels 		}
17330371Skarels 
17430371Skarels 		br = 0x13, cvec = 0x80;			/* XXX */
17530371Skarels 		return (sizeof (struct cyccb));
17630371Skarels 	} else
17730371Skarels 		return (0);
17825675Ssam }
17925675Ssam 
18024000Ssam /*
18125857Ssam  * Check to see if a drive is attached to a controller.
18225857Ssam  * Since we can only tell that a drive is there if a tape is loaded and
18325857Ssam  * the drive is placed online, we always indicate the slave is present.
18424000Ssam  */
18525857Ssam cyslave(vi, addr)
18625857Ssam 	struct vba_device *vi;
18725857Ssam 	caddr_t addr;
18824000Ssam {
18925857Ssam 
19025857Ssam #ifdef lint
19125857Ssam 	vi = vi; addr = addr;
19225857Ssam #endif
19325857Ssam 	return (1);
19425857Ssam }
19525857Ssam 
19625857Ssam cyattach(vi)
19725857Ssam 	struct vba_device *vi;
19825857Ssam {
19925979Ssam 	register struct cy_softc *cy;
20025979Ssam 	int ctlr = vi->ui_mi->um_ctlr;
20125857Ssam 
20225979Ssam 	yctocy[vi->ui_unit] = ctlr;
20325979Ssam 	cy = &cy_softc[ctlr];
20430371Skarels 	if (vi->ui_slave == 0 && cy->cy_bs)
20530371Skarels 		printf("; %dkb buffer", cy->cy_bs/1024);
20625857Ssam }
20725857Ssam 
20825857Ssam /*
20925857Ssam  * Initialize the controller after a controller reset or
21025857Ssam  * during autoconfigure.  All of the system control blocks
21125857Ssam  * are initialized and the controller is asked to configure
21225857Ssam  * itself for later use.
21325857Ssam  */
21430371Skarels cyinit(ctlr, addr)
21525979Ssam 	int ctlr;
21630371Skarels 	register caddr_t addr;
21725857Ssam {
21825979Ssam 	register struct cy_softc *cy = &cy_softc[ctlr];
21925675Ssam 	register int *pte;
22024000Ssam 
22124000Ssam 	/*
22225675Ssam 	 * Initialize the system configuration pointer.
22324000Ssam 	 */
22425675Ssam 	/* make kernel writable */
22530719Skarels 	pte = (int *)&Sysmap[btop((int)cy->cy_scp &~ KERNBASE)];
22625675Ssam 	*pte &= ~PG_PROT; *pte |= PG_KW;
22725979Ssam 	mtpr(TBIS, cy->cy_scp);
22825675Ssam 	/* load the correct values in the scp */
22925979Ssam 	cy->cy_scp->csp_buswidth = CSP_16BITS;
23025979Ssam 	cyldmba(cy->cy_scp->csp_scb, (caddr_t)&cy->cy_scb);
23125675Ssam 	/* put it back to read-only */
23225675Ssam 	*pte &= ~PG_PROT; *pte |= PG_KR;
23325979Ssam 	mtpr(TBIS, cy->cy_scp);
23425675Ssam 
23524000Ssam 	/*
23625675Ssam 	 * Init system configuration block.
23724000Ssam 	 */
23830371Skarels 	cy->cy_scb.csb_fixed = CSB_FIXED;
23925675Ssam 	/* set pointer to the channel control block */
24025979Ssam 	cyldmba(cy->cy_scb.csb_ccb, (caddr_t)&cy->cy_ccb);
24125675Ssam 
24224000Ssam 	/*
24325675Ssam 	 * Initialize the chanel control block.
24424000Ssam 	 */
24525979Ssam 	cy->cy_ccb.cbcw = CBCW_CLRINT;
24625979Ssam 	cy->cy_ccb.cbgate = GATE_OPEN;
24725675Ssam 	/* set pointer to the tape parameter block */
24825979Ssam 	cyldmba(cy->cy_ccb.cbtpb, (caddr_t)&cy->cy_tpb);
24925675Ssam 
25024000Ssam 	/*
25125979Ssam 	 * Issue a nop cmd and get the internal buffer size for buffered i/o.
25224000Ssam 	 */
25325979Ssam 	cy->cy_tpb.tpcmd = CY_NOP;
25425979Ssam 	cy->cy_tpb.tpcontrol = CYCW_16BITS;
25525979Ssam 	cy->cy_ccb.cbgate = GATE_CLOSED;
25625979Ssam 	CY_GO(addr);
25725979Ssam 	if (cywait(&cy->cy_ccb) || (cy->cy_tpb.tpstatus&CYS_ERR)) {
25825979Ssam 		uncache(&cy->cy_tpb.tpstatus);
25925979Ssam 		printf("cy%d: timeout or err during init, status=%b\n", ctlr,
26025979Ssam 		    cy->cy_tpb.tpstatus, CYS_BITS);
26125675Ssam 		return (0);
26225675Ssam 	}
26325979Ssam 	cy->cy_tpb.tpcmd = CY_CONFIG;
26425979Ssam 	cy->cy_tpb.tpcontrol = CYCW_16BITS;
26525979Ssam 	cy->cy_ccb.cbgate = GATE_CLOSED;
26625979Ssam 	CY_GO(addr);
26725979Ssam 	if (cywait(&cy->cy_ccb) || (cy->cy_tpb.tpstatus&CYS_ERR)) {
26825979Ssam 		uncache(&cy->cy_tpb.tpstatus);
26925979Ssam 		printf("cy%d: configuration failure, status=%b\n", ctlr,
27025979Ssam 		    cy->cy_tpb.tpstatus, CYS_BITS);
27125675Ssam 		return (0);
27225675Ssam 	}
27325675Ssam 	return (1);
27424000Ssam }
27524000Ssam 
27625979Ssam int	cytimer();
27725979Ssam /*
27825979Ssam  * Open the device.  Tapes are unique open
27925979Ssam  * devices, so we refuse if it is already open.
28025979Ssam  * We also check that a tape is available, and
28125979Ssam  * don't block waiting here; if you want to wait
28225979Ssam  * for a tape you should timeout in user code.
28325979Ssam  */
28425675Ssam cyopen(dev, flag)
28525979Ssam 	dev_t dev;
28625675Ssam 	register int flag;
28725675Ssam {
28825979Ssam 	register int ycunit;
28925979Ssam 	register struct vba_device *vi;
29025979Ssam 	register struct yc_softc *yc;
29125979Ssam 	int s;
29225675Ssam 
29325979Ssam 	ycunit = YCUNIT(dev);
29425979Ssam 	if (ycunit >= NYC || (vi = ycdinfo[ycunit]) == 0 || vi->ui_alive == 0)
29525675Ssam 		return (ENXIO);
29625979Ssam 	if ((yc = &yc_softc[ycunit])->yc_openf)
29725979Ssam 		return (EBUSY);
29830371Skarels 	yc->yc_openf = 1;
29925979Ssam #define	PACKUNIT(vi) \
30025979Ssam     (((vi->ui_slave&1)<<11)|((vi->ui_slave&2)<<9)|((vi->ui_slave&4)>>2))
30125979Ssam 	/* no way to select density */
30225979Ssam 	yc->yc_dens = PACKUNIT(vi)|CYCW_IE|CYCW_16BITS;
30330371Skarels 	if (yc->yc_tact == 0) {
30430371Skarels 		yc->yc_timo = INF;
30530371Skarels 		yc->yc_tact = 1;
30630371Skarels 		timeout(cytimer, (caddr_t)dev, 5*hz);
30730371Skarels 	}
30825979Ssam 	cycommand(dev, CY_SENSE, 1);
30925979Ssam 	if ((yc->yc_status&CYS_OL) == 0) {	/* not on-line */
31025979Ssam 		uprintf("yc%d: not online\n", ycunit);
31130439Skarels 		yc->yc_openf = 0;
31230872Skarels 		return (EIO);
31325675Ssam 	}
31425979Ssam 	if ((flag&FWRITE) && (yc->yc_status&CYS_WP)) {
31525979Ssam 		uprintf("yc%d: no write ring\n", ycunit);
31630439Skarels 		yc->yc_openf = 0;
31730872Skarels 		return (EIO);
31825675Ssam 	}
31925979Ssam 	yc->yc_blkno = (daddr_t)0;
32025979Ssam 	yc->yc_nxrec = INF;
32125979Ssam 	yc->yc_lastiow = 0;
32230869Skarels 	yc->yc_blksize = CYMAXIO;		/* guess > 0 */
32330371Skarels 	yc->yc_blks = 0;
32430371Skarels 	yc->yc_softerrs = 0;
32525979Ssam 	yc->yc_ttyp = u.u_ttyp;
32625675Ssam 	return (0);
32725675Ssam }
32825675Ssam 
32925979Ssam /*
33025979Ssam  * Close tape device.
33125979Ssam  *
33225979Ssam  * If tape was open for writing or last operation was a write,
33325979Ssam  * then write two EOF's and backspace over the last one.
33425979Ssam  * Unless this is a non-rewinding special file, rewind the tape.
33525979Ssam  * Make the tape available to others.
33625979Ssam  */
33725675Ssam cyclose(dev, flag)
33825979Ssam 	dev_t dev;
33930371Skarels 	int flag;
34025675Ssam {
34130371Skarels 	struct yc_softc *yc = &yc_softc[YCUNIT(dev)];
34225675Ssam 
34325979Ssam 	if (flag == FWRITE || (flag&FWRITE) && yc->yc_lastiow) {
34425979Ssam 		cycommand(dev, CY_WEOF, 2);
34525979Ssam 		cycommand(dev, CY_SREV, 1);
34625675Ssam 	}
34725979Ssam 	if ((minor(dev)&T_NOREWIND) == 0)
34825979Ssam 		/*
34925979Ssam 		 * 0 count means don't hang waiting for rewind complete
35025979Ssam 		 * rather ccybuf stays busy until the operation completes
35125979Ssam 		 * preventing further opens from completing by preventing
35225979Ssam 		 * a CY_SENSE from completing.
35325979Ssam 		 */
35425979Ssam 		cycommand(dev, CY_REW, 0);
35530371Skarels 	if (yc->yc_blks > 10 && yc->yc_softerrs > yc->yc_blks / 10)
35630371Skarels 		log(LOG_INFO, "yc%d: %d soft errors in %d blocks\n",
35730371Skarels 		    YCUNIT(dev), yc->yc_softerrs, yc->yc_blks);
35830371Skarels 	dlog((LOG_INFO, "%d soft errors in %d blocks\n",
35930371Skarels 	    yc->yc_softerrs, yc->yc_blks));
36025979Ssam 	yc->yc_openf = 0;
36130719Skarels 	return (0);
36225675Ssam }
36325675Ssam 
36424000Ssam /*
36525979Ssam  * Execute a command on the tape drive a specified number of times.
36624000Ssam  */
36725979Ssam cycommand(dev, com, count)
36825979Ssam 	dev_t dev;
36925979Ssam 	int com, count;
37024000Ssam {
37125979Ssam 	register struct buf *bp;
37225675Ssam 	int s;
37325675Ssam 
37425979Ssam 	bp = &ccybuf[CYUNIT(dev)];
37525675Ssam 	s = spl3();
37630371Skarels 	dlog((LOG_INFO, "cycommand(%o, %x, %d), b_flags %x\n",
37730371Skarels 	    dev, com, count, bp->b_flags));
37825979Ssam 	while (bp->b_flags&B_BUSY) {
37925979Ssam 		/*
38025979Ssam 		 * This special check is because B_BUSY never
38125979Ssam 		 * gets cleared in the non-waiting rewind case.
38225979Ssam 		 */
38325979Ssam 		if (bp->b_repcnt == 0 && (bp->b_flags&B_DONE))
38425979Ssam 			break;
38525979Ssam 		bp->b_flags |= B_WANTED;
38625979Ssam 		sleep((caddr_t)bp, PRIBIO);
38725675Ssam 	}
38825979Ssam 	bp->b_flags = B_BUSY|B_READ;
38925675Ssam 	splx(s);
39025979Ssam 	bp->b_dev = dev;
39125979Ssam 	bp->b_repcnt = count;
39225979Ssam 	bp->b_command = com;
39325979Ssam 	bp->b_blkno = 0;
39425979Ssam 	cystrategy(bp);
39525979Ssam 	/*
39625979Ssam 	 * In case of rewind from close; don't wait.
39725979Ssam 	 * This is the only case where count can be 0.
39825979Ssam 	 */
39925979Ssam 	if (count == 0)
40025979Ssam 		return;
40130371Skarels 	biowait(bp);
40225979Ssam 	if (bp->b_flags&B_WANTED)
40325979Ssam 		wakeup((caddr_t)bp);
40425979Ssam 	bp->b_flags &= B_ERROR;
40524000Ssam }
40624000Ssam 
40725675Ssam cystrategy(bp)
40825675Ssam 	register struct buf *bp;
40925675Ssam {
41025979Ssam 	int ycunit = YCUNIT(bp->b_dev);
41125979Ssam 	register struct vba_ctlr *vm;
41225979Ssam 	register struct buf *dp;
41325675Ssam 	int s;
41425675Ssam 
41525979Ssam 	/*
41625979Ssam 	 * Put transfer at end of unit queue.
41725979Ssam 	 */
41830371Skarels 	dlog((LOG_INFO, "cystrategy(%o, %x)\n", bp->b_dev, bp->b_command));
41925979Ssam 	dp = &ycutab[ycunit];
42025675Ssam 	bp->av_forw = NULL;
42125979Ssam 	vm = ycdinfo[ycunit]->ui_mi;
42225979Ssam 	/* BEGIN GROT */
42325979Ssam 	if (bp == &rcybuf[CYUNIT(bp->b_dev)]) {
42430869Skarels 		if (bp->b_bcount >= CYMAXIO) {
42525979Ssam 			uprintf("cy%d: i/o size too large\n", vm->um_ctlr);
42630869Skarels 			bp->b_error = EINVAL;
42725979Ssam 			bp->b_resid = bp->b_bcount;
42825979Ssam 			bp->b_flags |= B_ERROR;
42930371Skarels 			biodone(bp);
43025675Ssam 			return;
43125675Ssam 		}
43224000Ssam 	}
43325979Ssam 	/* END GROT */
43425675Ssam 	s = spl3();
43525979Ssam 	if (dp->b_actf == NULL) {
43625979Ssam 		dp->b_actf = bp;
43725979Ssam 		/*
43825979Ssam 		 * Transport not already active...
43925979Ssam 		 * put at end of controller queue.
44025979Ssam 		 */
44125979Ssam 		 dp->b_forw = NULL;
44225979Ssam 		 if (vm->um_tab.b_actf == NULL)
44325979Ssam 			vm->um_tab.b_actf = dp;
44425979Ssam 		else
44525979Ssam 			vm->um_tab.b_actl->b_forw = dp;
44625979Ssam 	} else
44725979Ssam 		dp->b_actl->av_forw = bp;
44825979Ssam 	dp->b_actl = bp;
44925979Ssam 	/*
45025979Ssam 	 * If the controller is not busy, get it going.
45125979Ssam 	 */
45225979Ssam 	if (vm->um_tab.b_active == 0)
45325979Ssam 		cystart(vm);
45424000Ssam 	splx(s);
45524000Ssam }
45624000Ssam 
45724000Ssam /*
45825979Ssam  * Start activity on a cy controller.
45924000Ssam  */
46025979Ssam cystart(vm)
46125979Ssam 	register struct vba_ctlr *vm;
46224000Ssam {
46325979Ssam 	register struct buf *bp, *dp;
46425979Ssam 	register struct yc_softc *yc;
46525979Ssam 	register struct cy_softc *cy;
46625979Ssam 	int ycunit;
46725979Ssam 	daddr_t blkno;
46824000Ssam 
46930371Skarels 	dlog((LOG_INFO, "cystart()\n"));
47025979Ssam 	/*
47125979Ssam 	 * Look for an idle transport on the controller.
47225979Ssam 	 */
47325979Ssam loop:
47425979Ssam 	if ((dp = vm->um_tab.b_actf) == NULL)
47525675Ssam 		return;
47625979Ssam 	if ((bp = dp->b_actf) == NULL) {
47725979Ssam 		vm->um_tab.b_actf = dp->b_forw;
47825979Ssam 		goto loop;
47925675Ssam 	}
48025979Ssam 	ycunit = YCUNIT(bp->b_dev);
48125979Ssam 	yc = &yc_softc[ycunit];
48225979Ssam 	cy = &cy_softc[CYUNIT(bp->b_dev)];
48325979Ssam 	/*
48425979Ssam 	 * Default is that last command was NOT a write command;
48525979Ssam 	 * if we do a write command we will notice this in cyintr().
48625979Ssam 	 */
48725979Ssam 	yc->yc_lastiow = 0;
48825979Ssam 	if (yc->yc_openf < 0 ||
48925979Ssam 	    (bp->b_command != CY_SENSE && (cy->cy_tpb.tpstatus&CYS_OL) == 0)) {
49025979Ssam 		/*
49125979Ssam 		 * Have had a hard error on a non-raw tape
49225979Ssam 		 * or the tape unit is now unavailable (e.g.
49325979Ssam 		 * taken off line).
49425979Ssam 		 */
49530371Skarels 		dlog((LOG_INFO, "openf %d command %x status %b\n",
49630371Skarels 		   yc->yc_openf, bp->b_command, cy->cy_tpb.tpstatus, CYS_BITS));
49725979Ssam 		bp->b_flags |= B_ERROR;
49825979Ssam 		goto next;
49925675Ssam 	}
50025979Ssam 	if (bp == &ccybuf[CYUNIT(bp->b_dev)]) {
50125979Ssam 		/*
50225979Ssam 		 * Execute control operation with the specified count.
50325979Ssam 		 *
50425979Ssam 		 * Set next state; give 5 minutes to complete
50525979Ssam 		 * rewind or file mark search, or 10 seconds per
50625979Ssam 		 * iteration (minimum 60 seconds and max 5 minutes)
50725979Ssam 		 * to complete other ops.
50825979Ssam 		 */
50925979Ssam 		if (bp->b_command == CY_REW) {
51025979Ssam 			vm->um_tab.b_active = SREW;
51125979Ssam 			yc->yc_timo = 5*60;
51230869Skarels 		} else if (bp->b_command == CY_FSF ||
51330869Skarels 		    bp->b_command == CY_BSF) {
51430869Skarels 			vm->um_tab.b_active = SCOM;
51530869Skarels 			yc->yc_timo = 5*60;
51625979Ssam 		} else {
51725979Ssam 			vm->um_tab.b_active = SCOM;
51825979Ssam 			yc->yc_timo = imin(imax(10*(int)bp->b_repcnt,60),5*60);
51925979Ssam 		}
52025979Ssam 		cy->cy_tpb.tprec = htoms(bp->b_repcnt);
52130719Skarels 		dlog((LOG_INFO, "bpcmd "));
52225979Ssam 		goto dobpcmd;
52324000Ssam 	}
52425979Ssam 	/*
52525979Ssam 	 * The following checks handle boundary cases for operation
52625979Ssam 	 * on no-raw tapes.  On raw tapes the initialization of
52725979Ssam 	 * yc->yc_nxrec by cyphys causes them to be skipped normally
52825979Ssam 	 * (except in the case of retries).
52925979Ssam 	 */
53030719Skarels 	if (bp->b_blkno > yc->yc_nxrec) {
53125979Ssam 		/*
53225979Ssam 		 * Can't read past known end-of-file.
53325979Ssam 		 */
53425979Ssam 		bp->b_flags |= B_ERROR;
53525979Ssam 		bp->b_error = ENXIO;
53625979Ssam 		goto next;
53724000Ssam 	}
53830719Skarels 	if (bp->b_blkno == yc->yc_nxrec && bp->b_flags&B_READ) {
53925979Ssam 		/*
54025979Ssam 		 * Reading at end of file returns 0 bytes.
54125979Ssam 		 */
54225979Ssam 		bp->b_resid = bp->b_bcount;
54325979Ssam 		clrbuf(bp);
54425979Ssam 		goto next;
54524000Ssam 	}
54625979Ssam 	if ((bp->b_flags&B_READ) == 0)
54725979Ssam 		/*
54825979Ssam 		 * Writing sets EOF.
54925979Ssam 		 */
55030719Skarels 		yc->yc_nxrec = bp->b_blkno + 1;
55130719Skarels 	if ((blkno = yc->yc_blkno) == bp->b_blkno) {
55225979Ssam 		caddr_t addr;
55325979Ssam 		int cmd;
55425675Ssam 
55525979Ssam 		/*
55625979Ssam 		 * Choose the appropriate i/o command based on the
55730371Skarels 		 * transfer size, the estimated block size,
55830371Skarels 		 * and the controller's internal buffer size.
55930869Skarels 		 * If the request length is longer than the tape
56030869Skarels 		 * block length, a buffered read will fail,
56130869Skarels 		 * thus, we request at most the size that we expect.
56230869Skarels 		 * We then check for larger records when the read completes.
56325979Ssam 		 * If we're retrying a read on a raw device because
56425979Ssam 		 * the original try was a buffer request which failed
56525979Ssam 		 * due to a record length error, then we force the use
56625979Ssam 		 * of the raw controller read (YECH!!!!).
56725979Ssam 		 */
56825979Ssam 		if (bp->b_flags&B_READ) {
56930869Skarels 			if (yc->yc_blksize <= cy->cy_bs &&
57030869Skarels 			    vm->um_tab.b_errcnt == 0)
57130869Skarels 				cmd = CY_BRCOM;
57230869Skarels 			else
57325979Ssam 				cmd = CY_RCOM;
57425979Ssam 		} else {
57525979Ssam 			/*
57625979Ssam 			 * On write error retries erase the
57725979Ssam 			 * inter-record gap before rewriting.
57825979Ssam 			 */
57925979Ssam 			if (vm->um_tab.b_errcnt &&
58025979Ssam 			    vm->um_tab.b_active != SERASED) {
58125979Ssam 				vm->um_tab.b_active = SERASE;
58225979Ssam 				bp->b_command = CY_ERASE;
58325979Ssam 				yc->yc_timo = 60;
58425979Ssam 				goto dobpcmd;
58525675Ssam 			}
58625979Ssam 			cmd = (bp->b_bcount > cy->cy_bs) ? CY_WCOM : CY_BWCOM;
58725675Ssam 		}
58825979Ssam 		vm->um_tab.b_active = SIO;
58930719Skarels 		addr = (caddr_t)vbasetup(bp, &cy->cy_rbuf, 1);
59025979Ssam 		cy->cy_tpb.tpcmd = cmd;
59125979Ssam 		cy->cy_tpb.tpcontrol = yc->yc_dens;
59225979Ssam 		if (cmd == CY_RCOM || cmd == CY_WCOM)
59325979Ssam 			cy->cy_tpb.tpcontrol |= CYCW_LOCK;
59425979Ssam 		cy->cy_tpb.tpstatus = 0;
59525979Ssam 		cy->cy_tpb.tpcount = 0;
59625979Ssam 		cyldmba(cy->cy_tpb.tpdata, (caddr_t)addr);
59725979Ssam 		cy->cy_tpb.tprec = 0;
59830869Skarels 		if (cmd == CY_BRCOM)
59930869Skarels 			cy->cy_tpb.tpsize = htoms(min(yc->yc_blksize,
60030869Skarels 			    bp->b_bcount));
60130371Skarels 		else
60230371Skarels 			cy->cy_tpb.tpsize = htoms(bp->b_bcount);
60325979Ssam 		cyldmba(cy->cy_tpb.tplink, (caddr_t)0);
60425979Ssam 		do
60525979Ssam 			uncache(&cy->cy_ccb.cbgate);
60625979Ssam 		while (cy->cy_ccb.cbgate == GATE_CLOSED);
60725979Ssam 		cyldmba(cy->cy_ccb.cbtpb, (caddr_t)&cy->cy_tpb);
60825979Ssam 		cy->cy_ccb.cbcw = CBCW_IE;
60925979Ssam 		cy->cy_ccb.cbgate = GATE_CLOSED;
61030371Skarels 		dlog((LOG_INFO, "CY_GO(%x) cmd %x control %x size %d\n",
61125979Ssam 		    vm->um_addr, cy->cy_tpb.tpcmd, cy->cy_tpb.tpcontrol,
61230371Skarels 		    htoms(cy->cy_tpb.tpsize)));
61325979Ssam 		CY_GO(vm->um_addr);
61425979Ssam 		return;
61524000Ssam 	}
61625979Ssam 	/*
61725979Ssam 	 * Tape positioned incorrectly; set to seek forwards
61825979Ssam 	 * or backwards to the correct spot.  This happens
61925979Ssam 	 * for raw tapes only on error retries.
62025979Ssam 	 */
62125979Ssam 	vm->um_tab.b_active = SSEEK;
62230719Skarels 	if (blkno < bp->b_blkno) {
62325979Ssam 		bp->b_command = CY_SFORW;
62430719Skarels 		cy->cy_tpb.tprec = htoms(bp->b_blkno - blkno);
62525979Ssam 	} else {
62625979Ssam 		bp->b_command = CY_SREV;
62730719Skarels 		cy->cy_tpb.tprec = htoms(blkno - bp->b_blkno);
62824000Ssam 	}
62925979Ssam 	yc->yc_timo = imin(imax(10 * htoms(cy->cy_tpb.tprec), 60), 5*60);
63025979Ssam dobpcmd:
63125979Ssam 	/*
63225979Ssam 	 * Do the command in bp.  Reverse direction commands
63325979Ssam 	 * are indicated by having CYCW_REV or'd into their
63425979Ssam 	 * value.  For these we must set the appropriate bit
63525979Ssam 	 * in the control field.
63625979Ssam 	 */
63725979Ssam 	if (bp->b_command&CYCW_REV) {
63825979Ssam 		cy->cy_tpb.tpcmd = bp->b_command &~ CYCW_REV;
63925979Ssam 		cy->cy_tpb.tpcontrol = yc->yc_dens | CYCW_REV;
64030719Skarels dlog((LOG_INFO, "cmd %x control %x\n", cy->cy_tpb.tpcmd, cy->cy_tpb.tpcontrol));
64125979Ssam 	} else {
64225979Ssam 		cy->cy_tpb.tpcmd = bp->b_command;
64325979Ssam 		cy->cy_tpb.tpcontrol = yc->yc_dens;
64430719Skarels dlog((LOG_INFO, "cmd %x control %x\n", cy->cy_tpb.tpcmd, cy->cy_tpb.tpcontrol));
64524000Ssam 	}
64625979Ssam 	cy->cy_tpb.tpstatus = 0;
64725979Ssam 	cy->cy_tpb.tpcount = 0;
64825979Ssam 	cyldmba(cy->cy_tpb.tplink, (caddr_t)0);
64925979Ssam 	do
65025979Ssam 		uncache(&cy->cy_ccb.cbgate);
65125979Ssam 	while (cy->cy_ccb.cbgate == GATE_CLOSED);
65225979Ssam 	cyldmba(cy->cy_ccb.cbtpb, (caddr_t)&cy->cy_tpb);
65325979Ssam 	cy->cy_ccb.cbcw = CBCW_IE;
65425979Ssam 	cy->cy_ccb.cbgate = GATE_CLOSED;
65530371Skarels 	dlog((LOG_INFO, "CY_GO(%x) cmd %x control %x rec %d\n",
65625979Ssam 	    vm->um_addr, cy->cy_tpb.tpcmd, cy->cy_tpb.tpcontrol,
65730371Skarels 	    htoms(cy->cy_tpb.tprec)));
65825979Ssam 	CY_GO(vm->um_addr);
65925979Ssam 	return;
66025979Ssam next:
66125979Ssam 	/*
66225979Ssam 	 * Done with this operation due to error or the
66330719Skarels 	 * fact that it doesn't do anything.
66430719Skarels 	 * Dequeue the transfer and continue
66525979Ssam 	 * processing this slave.
66625979Ssam 	 */
66725979Ssam 	vm->um_tab.b_errcnt = 0;
66825979Ssam 	dp->b_actf = bp->av_forw;
66930371Skarels 	biodone(bp);
67025979Ssam 	goto loop;
67125675Ssam }
67225675Ssam 
67325675Ssam /*
67425979Ssam  * Cy interrupt routine.
67525675Ssam  */
67630719Skarels cyintr(cyunit)
67730719Skarels 	int cyunit;
67825675Ssam {
67925979Ssam 	struct buf *dp;
68024000Ssam 	register struct buf *bp;
68130719Skarels 	register struct vba_ctlr *vm = cyminfo[cyunit];
68225979Ssam 	register struct cy_softc *cy;
68325979Ssam 	register struct yc_softc *yc;
68430719Skarels 	int err;
68525979Ssam 	register state;
68624000Ssam 
68730719Skarels 	dlog((LOG_INFO, "cyintr(%d)\n", cyunit));
68825979Ssam 	/*
68925979Ssam 	 * First, turn off the interrupt from the controller
69025979Ssam 	 * (device uses Multibus non-vectored interrupts...yech).
69125979Ssam 	 */
69225979Ssam 	cy = &cy_softc[vm->um_ctlr];
69325979Ssam 	cy->cy_ccb.cbcw = CBCW_CLRINT;
69430294Ssam 	cyldmba(cy->cy_ccb.cbtpb, (caddr_t)&cy->cy_nop);
69525979Ssam 	cy->cy_ccb.cbgate = GATE_CLOSED;
69625979Ssam 	CY_GO(vm->um_addr);
69725979Ssam 	if ((dp = vm->um_tab.b_actf) == NULL) {
69830371Skarels 		dlog((LOG_ERR, "cy%d: stray interrupt", vm->um_ctlr));
69924000Ssam 		return;
70024000Ssam 	}
70125979Ssam 	bp = dp->b_actf;
70225979Ssam 	cy = &cy_softc[cyunit];
70325979Ssam 	cyuncachetpb(cy);
70430294Ssam 	yc = &yc_softc[YCUNIT(bp->b_dev)];
70525979Ssam 	/*
70625984Ssam 	 * If last command was a rewind and tape is
70725984Ssam 	 * still moving, wait for the operation to complete.
70825979Ssam 	 */
70925979Ssam 	if (vm->um_tab.b_active == SREW) {
71025979Ssam 		vm->um_tab.b_active = SCOM;
71125979Ssam 		if ((cy->cy_tpb.tpstatus&CYS_RDY) == 0) {
71225979Ssam 			yc->yc_timo = 5*60;	/* 5 minutes */
71325979Ssam 			return;
71424000Ssam 		}
71524000Ssam 	}
71625979Ssam 	/*
71725979Ssam 	 * An operation completed...record status.
71825979Ssam 	 */
71925979Ssam 	yc->yc_timo = INF;
72025979Ssam 	yc->yc_control = cy->cy_tpb.tpcontrol;
72125979Ssam 	yc->yc_status = cy->cy_tpb.tpstatus;
72225979Ssam 	yc->yc_resid = bp->b_bcount - htoms(cy->cy_tpb.tpcount);
72330371Skarels 	dlog((LOG_INFO, "cmd %x control %b status %b resid %d\n",
72425979Ssam 	    cy->cy_tpb.tpcmd, yc->yc_control, CYCW_BITS,
72530371Skarels 	    yc->yc_status, CYS_BITS, yc->yc_resid));
72625979Ssam 	if ((bp->b_flags&B_READ) == 0)
72725979Ssam 		yc->yc_lastiow = 1;
72825979Ssam 	state = vm->um_tab.b_active;
72925979Ssam 	vm->um_tab.b_active = 0;
73025979Ssam 	/*
73125979Ssam 	 * Check for errors.
73225979Ssam 	 */
73325979Ssam 	if (cy->cy_tpb.tpstatus&CYS_ERR) {
73425979Ssam 		err = cy->cy_tpb.tpstatus&CYS_ERR;
73530371Skarels 		dlog((LOG_INFO, "error %d\n", err));
73625979Ssam 		/*
73725979Ssam 		 * If we hit the end of tape file, update our position.
73825979Ssam 		 */
73925979Ssam 		if (err == CYER_FM) {
74025979Ssam 			yc->yc_status |= CYS_FM;
74125979Ssam 			state = SCOM;		/* force completion */
74225979Ssam 			cyseteof(bp);		/* set blkno and nxrec */
74325979Ssam 			goto opdone;
74425979Ssam 		}
74525979Ssam 		/*
74625979Ssam 		 * Fix up errors which occur due to backspacing over
74725979Ssam 		 * the beginning of the tape.
74825979Ssam 		 */
74925979Ssam 		if (err == CYER_BOT && cy->cy_tpb.tpcontrol&CYCW_REV) {
75025979Ssam 			yc->yc_status |= CYS_BOT;
75125979Ssam 			goto ignoreerr;
75225979Ssam 		}
75325979Ssam 		/*
75425979Ssam 		 * If we were reading raw tape and the only error was that the
75525979Ssam 		 * record was too long, then we don't consider this an error.
75625979Ssam 		 */
75725979Ssam 		if (bp == &rcybuf[cyunit] && (bp->b_flags&B_READ) &&
75825979Ssam 		    err == CYER_STROBE) {
75925979Ssam 			/*
76030371Skarels 			 * Retry reads with the command changed to
76130371Skarels 			 * a raw read if necessary.  Setting b_errcnt
76225979Ssam 			 * here causes cystart (above) to force a CY_RCOM.
76325979Ssam 			 */
76430869Skarels 			if (cy->cy_tpb.tpcmd == CY_BRCOM &&
76530719Skarels 			    vm->um_tab.b_errcnt++ == 0) {
76630371Skarels 				yc->yc_blkno++;
76730371Skarels 				goto opcont;
76830371Skarels 			} else
76925979Ssam 				goto ignoreerr;
77025979Ssam 		}
77125979Ssam 		/*
77225979Ssam 		 * If error is not hard, and this was an i/o operation
77325979Ssam 		 * retry up to 8 times.
77425979Ssam 		 */
77525984Ssam 		if (((1<<err)&CYER_SOFT) && state == SIO) {
77625979Ssam 			if (++vm->um_tab.b_errcnt < 7) {
77725979Ssam 				yc->yc_blkno++;
77825979Ssam 				goto opcont;
77925979Ssam 			}
78025979Ssam 		} else
78125979Ssam 			/*
78225979Ssam 			 * Hard or non-i/o errors on non-raw tape
78325979Ssam 			 * cause it to close.
78425979Ssam 			 */
78530371Skarels 			if (yc->yc_openf > 0 && bp != &rcybuf[cyunit])
78625979Ssam 				yc->yc_openf = -1;
78725979Ssam 		/*
78825979Ssam 		 * Couldn't recover from error.
78925979Ssam 		 */
79025979Ssam 		tprintf(yc->yc_ttyp,
79130371Skarels 		    "yc%d: hard error bn%d status=%b, %s\n", YCUNIT(bp->b_dev),
79230371Skarels 		    bp->b_blkno, yc->yc_status, CYS_BITS,
79330371Skarels 		    (err < NCYERROR) ? cyerror[err] : "");
79425979Ssam 		bp->b_flags |= B_ERROR;
79525979Ssam 		goto opdone;
79630869Skarels 	} else if (cy->cy_tpb.tpcmd == CY_BRCOM) {
79730869Skarels 		int reclen = htoms(cy->cy_tpb.tprec);
79830869Skarels 
79930869Skarels 		/*
80030869Skarels 		 * If we did a buffered read, check whether the read
80130869Skarels 		 * was long enough.  If we asked the controller for less
80230869Skarels 		 * than the user asked for because the previous record
80330869Skarels 		 * was shorter, update our notion of record size
80430869Skarels 		 * and retry.  If the record is longer than the buffer,
80530869Skarels 		 * bump the errcnt so the retry will use direct read.
80630869Skarels 		 */
80730869Skarels 		if (reclen > yc->yc_blksize && bp->b_bcount > yc->yc_blksize) {
80830869Skarels 			yc->yc_blksize = reclen;
80930869Skarels 			if (reclen > cy->cy_bs)
81030869Skarels 				vm->um_tab.b_errcnt++;
81130869Skarels 			yc->yc_blkno++;
81230869Skarels 			goto opcont;
81330869Skarels 		}
81424000Ssam 	}
81525979Ssam 	/*
81625979Ssam 	 * Advance tape control FSM.
81725979Ssam 	 */
81825979Ssam ignoreerr:
81925979Ssam 	/*
82025979Ssam 	 * If we hit a tape mark update our position.
82125979Ssam 	 */
82225979Ssam 	if (yc->yc_status&CYS_FM && bp->b_flags&B_READ) {
82325979Ssam 		cyseteof(bp);
82425979Ssam 		goto opdone;
82525675Ssam 	}
82625979Ssam 	switch (state) {
82724000Ssam 
82825979Ssam 	case SIO:
82925979Ssam 		/*
83025979Ssam 		 * Read/write increments tape block number.
83125979Ssam 		 */
83225979Ssam 		yc->yc_blkno++;
83330371Skarels 		yc->yc_blks++;
83430371Skarels 		if (vm->um_tab.b_errcnt || yc->yc_status & CYS_CR)
83530371Skarels 			yc->yc_softerrs++;
83630371Skarels 		yc->yc_blksize = htoms(cy->cy_tpb.tpcount);
83730371Skarels 		dlog((LOG_ERR, "blocksize %d", yc->yc_blksize));
83825979Ssam 		goto opdone;
83924000Ssam 
84025979Ssam 	case SCOM:
84125979Ssam 		/*
84225979Ssam 		 * For forward/backward space record update current position.
84325979Ssam 		 */
84430294Ssam 		if (bp == &ccybuf[CYUNIT(bp->b_dev)])
84530294Ssam 			switch ((int)bp->b_command) {
84624000Ssam 
84730294Ssam 			case CY_SFORW:
84830294Ssam 				yc->yc_blkno -= bp->b_repcnt;
84930294Ssam 				break;
85024000Ssam 
85130294Ssam 			case CY_SREV:
85230294Ssam 				yc->yc_blkno += bp->b_repcnt;
85330294Ssam 				break;
85430294Ssam 			}
85525979Ssam 		goto opdone;
85625979Ssam 
85725979Ssam 	case SSEEK:
85830719Skarels 		yc->yc_blkno = bp->b_blkno;
85925979Ssam 		goto opcont;
86024000Ssam 
86125979Ssam 	case SERASE:
86225979Ssam 		/*
86325979Ssam 		 * Completed erase of the inter-record gap due to a
86425979Ssam 		 * write error; now retry the write operation.
86525979Ssam 		 */
86625979Ssam 		vm->um_tab.b_active = SERASED;
86725979Ssam 		goto opcont;
86824000Ssam 	}
86925675Ssam 
87025979Ssam opdone:
87125979Ssam 	/*
87225979Ssam 	 * Reset error count and remove from device queue.
87325979Ssam 	 */
87425979Ssam 	vm->um_tab.b_errcnt = 0;
87525979Ssam 	dp->b_actf = bp->av_forw;
87625979Ssam 	/*
87725979Ssam 	 * Save resid and release resources.
87825979Ssam 	 */
87925979Ssam 	bp->b_resid = bp->b_bcount - htoms(cy->cy_tpb.tpcount);
88030719Skarels 	if (bp != &ccybuf[cyunit])
88130719Skarels 		vbadone(bp, &cy->cy_rbuf);
88230371Skarels 	biodone(bp);
88325979Ssam 	/*
88425979Ssam 	 * Circulate slave to end of controller
88525979Ssam 	 * queue to give other slaves a chance.
88625979Ssam 	 */
88725979Ssam 	vm->um_tab.b_actf = dp->b_forw;
88825979Ssam 	if (dp->b_actf) {
88925979Ssam 		dp->b_forw = NULL;
89025979Ssam 		if (vm->um_tab.b_actf == NULL)
89125979Ssam 			vm->um_tab.b_actf = dp;
89225979Ssam 		else
89325979Ssam 			vm->um_tab.b_actl->b_forw = dp;
89424000Ssam 	}
89525979Ssam 	if (vm->um_tab.b_actf == 0)
89624000Ssam 		return;
89725979Ssam opcont:
89825979Ssam 	cystart(vm);
89924000Ssam }
90024000Ssam 
90125979Ssam cytimer(dev)
90225979Ssam 	int dev;
90324000Ssam {
90425979Ssam 	register struct yc_softc *yc = &yc_softc[YCUNIT(dev)];
90525979Ssam 	int s;
90624000Ssam 
90730371Skarels 	if (yc->yc_openf == 0 && yc->yc_timo == INF) {
90830371Skarels 		yc->yc_tact = 0;
90930371Skarels 		return;
91030371Skarels 	}
91125979Ssam 	if (yc->yc_timo != INF && (yc->yc_timo -= 5) < 0) {
91225979Ssam 		printf("yc%d: lost interrupt\n", YCUNIT(dev));
91325979Ssam 		yc->yc_timo = INF;
91425979Ssam 		s = spl3();
91525979Ssam 		cyintr(CYUNIT(dev));
91625979Ssam 		splx(s);
91724000Ssam 	}
91825979Ssam 	timeout(cytimer, (caddr_t)dev, 5*hz);
91924000Ssam }
92024000Ssam 
92125979Ssam cyseteof(bp)
92225979Ssam 	register struct buf *bp;
92324000Ssam {
92425979Ssam 	register int cyunit = CYUNIT(bp->b_dev);
92525979Ssam 	register struct cy_softc *cy = &cy_softc[cyunit];
92625979Ssam 	register struct yc_softc *yc = &yc_softc[YCUNIT(bp->b_dev)];
92724000Ssam 
92825979Ssam 	if (bp == &ccybuf[cyunit]) {
92930719Skarels 		if (yc->yc_blkno > bp->b_blkno) {
93025979Ssam 			/* reversing */
93130719Skarels 			yc->yc_nxrec = bp->b_blkno - htoms(cy->cy_tpb.tpcount);
93225979Ssam 			yc->yc_blkno = yc->yc_nxrec;
93325979Ssam 		} else {
93430719Skarels 			yc->yc_blkno = bp->b_blkno + htoms(cy->cy_tpb.tpcount);
93525979Ssam 			yc->yc_nxrec = yc->yc_blkno - 1;
93624000Ssam 		}
93725675Ssam 		return;
93825675Ssam 	}
93925979Ssam 	/* eof on read */
94030719Skarels 	yc->yc_nxrec = bp->b_blkno;
94124000Ssam }
94224000Ssam 
94325979Ssam cyread(dev, uio)
94425979Ssam 	dev_t dev;
94525979Ssam 	struct uio *uio;
94625675Ssam {
94725979Ssam 	int errno;
94825675Ssam 
94925979Ssam 	errno = cyphys(dev, uio);
95025979Ssam 	if (errno)
95125979Ssam 		return (errno);
95225979Ssam 	return (physio(cystrategy, &rcybuf[CYUNIT(dev)], dev, B_READ, minphys, uio));
95325675Ssam }
95425675Ssam 
95525979Ssam cywrite(dev, uio)
95625979Ssam 	dev_t dev;
95725979Ssam 	struct uio *uio;
95824000Ssam {
95925979Ssam 	int errno;
96024000Ssam 
96125979Ssam 	errno = cyphys(dev, uio);
96225979Ssam 	if (errno)
96325979Ssam 		return (errno);
96425979Ssam 	return (physio(cystrategy, &rcybuf[CYUNIT(dev)], dev, B_WRITE, minphys, uio));
96524000Ssam }
96624000Ssam 
96724000Ssam /*
96825979Ssam  * Check that a raw device exits.
96925979Ssam  * If it does, set up the yc_blkno and yc_nxrec
97025979Ssam  * so that the tape will appear positioned correctly.
97125979Ssam  */
97225979Ssam cyphys(dev, uio)
97325675Ssam 	dev_t dev;
97425675Ssam 	struct uio *uio;
97525675Ssam {
97625979Ssam 	register int ycunit = YCUNIT(dev);
97725979Ssam 	register daddr_t a;
97825979Ssam 	register struct yc_softc *yc;
97925979Ssam 	register struct vba_device *vi;
98025675Ssam 
98125979Ssam 	if (ycunit >= NYC || (vi = ycdinfo[ycunit]) == 0 || vi->ui_alive == 0)
98225979Ssam 		return (ENXIO);
98325979Ssam 	yc = &yc_softc[ycunit];
98430719Skarels 	a = uio->uio_offset >> DEV_BSHIFT;
98525979Ssam 	yc->yc_blkno = a;
98625979Ssam 	yc->yc_nxrec = a + 1;
98725979Ssam 	return (0);
98825675Ssam }
98925675Ssam 
99025675Ssam /*ARGSUSED*/
99125675Ssam cyioctl(dev, cmd, data, flag)
99225979Ssam 	caddr_t data;
99325675Ssam 	dev_t dev;
99425675Ssam {
99525979Ssam 	int ycunit = YCUNIT(dev);
99625979Ssam 	register struct yc_softc *yc = &yc_softc[ycunit];
99725979Ssam 	register struct buf *bp = &ccybuf[CYUNIT(dev)];
99825979Ssam 	register callcount;
99925979Ssam 	int fcount, op;
100025979Ssam 	struct mtop *mtop;
100125979Ssam 	struct mtget *mtget;
100225979Ssam 	/* we depend of the values and order of the MT codes here */
100325979Ssam 	static cyops[] =
100430371Skarels 	{CY_WEOF,CY_FSF,CY_BSF,CY_SFORW,CY_SREV,CY_REW,CY_OFFL,CY_SENSE};
100525675Ssam 
100625675Ssam 	switch (cmd) {
100725675Ssam 
100825979Ssam 	case MTIOCTOP:	/* tape operation */
100925979Ssam 		mtop = (struct mtop *)data;
101025979Ssam 		switch (op = mtop->mt_op) {
101125675Ssam 
101225979Ssam 		case MTWEOF:
101330371Skarels 			callcount = mtop->mt_count;
101430371Skarels 			fcount = 1;
101530371Skarels 			break;
101630371Skarels 
101725979Ssam 		case MTFSR: case MTBSR:
101830371Skarels 			callcount = 1;
101930371Skarels 			fcount = mtop->mt_count;
102030371Skarels 			break;
102130371Skarels 
102225979Ssam 		case MTFSF: case MTBSF:
102325979Ssam 			callcount = mtop->mt_count;
102425979Ssam 			fcount = 1;
102525979Ssam 			break;
102625675Ssam 
102725979Ssam 		case MTREW: case MTOFFL: case MTNOP:
102825979Ssam 			callcount = 1;
102925979Ssam 			fcount = 1;
103025979Ssam 			break;
103125675Ssam 
103225979Ssam 		default:
103325979Ssam 			return (ENXIO);
103425979Ssam 		}
103525979Ssam 		if (callcount <= 0 || fcount <= 0)
103625979Ssam 			return (EINVAL);
103725979Ssam 		while (--callcount >= 0) {
103830371Skarels #ifdef notdef
103925979Ssam 			/*
104025979Ssam 			 * Gagh, this controller is the pits...
104125979Ssam 			 */
104225979Ssam 			if (op == MTFSF || op == MTBSF) {
104325979Ssam 				do
104425979Ssam 					cycommand(dev, cyops[op], 1);
104525979Ssam 				while ((bp->b_flags&B_ERROR) == 0 &&
104625979Ssam 				 (yc->yc_status&(CYS_EOT|CYS_BOT|CYS_FM)) == 0);
104725979Ssam 			} else
104830371Skarels #endif
104925979Ssam 				cycommand(dev, cyops[op], fcount);
105030371Skarels 			dlog((LOG_INFO,
105130371Skarels 			    "cyioctl: status %x, b_flags %x, resid %d\n",
105230371Skarels 			    yc->yc_status, bp->b_flags, bp->b_resid));
105325979Ssam 			if ((bp->b_flags&B_ERROR) ||
105425979Ssam 			    (yc->yc_status&(CYS_BOT|CYS_EOT)))
105525979Ssam 				break;
105625979Ssam 		}
105725979Ssam 		bp->b_resid = callcount + 1;
105825979Ssam 		return (geterror(bp));
105925979Ssam 
106025979Ssam 	case MTIOCGET:
106125979Ssam 		cycommand(dev, CY_SENSE, 1);
106225979Ssam 		mtget = (struct mtget *)data;
106325979Ssam 		mtget->mt_dsreg = yc->yc_status;
106425979Ssam 		mtget->mt_erreg = yc->yc_control;
106525979Ssam 		mtget->mt_resid = yc->yc_resid;
106625979Ssam 		mtget->mt_type = MT_ISCY;
106725675Ssam 		break;
106825675Ssam 
106925675Ssam 	default:
107025675Ssam 		return (ENXIO);
107125675Ssam 	}
107225675Ssam 	return (0);
107325675Ssam }
107425675Ssam 
107525675Ssam /*
107625675Ssam  * Poll until the controller is ready.
107725675Ssam  */
107825675Ssam cywait(cp)
107925979Ssam 	register struct cyccb *cp;
108024000Ssam {
108125675Ssam 	register int i = 5000;
108224000Ssam 
108325979Ssam 	uncache(&cp->cbgate);
108425979Ssam 	while (i-- > 0 && cp->cbgate == GATE_CLOSED) {
108524000Ssam 		DELAY(1000);
108625979Ssam 		uncache(&cp->cbgate);
108724000Ssam 	}
108825675Ssam 	return (i <= 0);
108924000Ssam }
109024000Ssam 
109125675Ssam /*
109230371Skarels  * Load a 20 bit pointer into a Tapemaster pointer.
109325675Ssam  */
109430371Skarels cyldmba(reg, value)
109530371Skarels 	register caddr_t reg;
109625979Ssam 	caddr_t value;
109724000Ssam {
109825979Ssam 	register int v = (int)value;
109925675Ssam 
110025979Ssam 	*reg++ = v;
110125979Ssam 	*reg++ = v >> 8;
110225979Ssam 	*reg++ = 0;
110325979Ssam 	*reg = (v&0xf0000) >> 12;
110424000Ssam }
110524000Ssam 
110625675Ssam /*
110725675Ssam  * Unconditionally reset all controllers to their initial state.
110825675Ssam  */
110925675Ssam cyreset(vba)
111025675Ssam 	int vba;
111124000Ssam {
111225675Ssam 	register caddr_t addr;
111325675Ssam 	register int ctlr;
111424000Ssam 
111525675Ssam 	for (ctlr = 0; ctlr < NCY; ctlr++)
111625675Ssam 		if (cyminfo[ctlr] && cyminfo[ctlr]->um_vbanum == vba) {
111725675Ssam 			addr = cyminfo[ctlr]->um_addr;
111825675Ssam 			CY_RESET(addr);
111930371Skarels 			if (!cyinit(ctlr, addr)) {
112025675Ssam 				printf("cy%d: reset failed\n", ctlr);
112125675Ssam 				cyminfo[ctlr] = NULL;
112225675Ssam 			}
112325675Ssam 		}
112424000Ssam }
112525979Ssam 
112625979Ssam cyuncachetpb(cy)
112725979Ssam 	struct cy_softc *cy;
112825979Ssam {
112925979Ssam 	register long *lp = (long *)&cy->cy_tpb;
113025979Ssam 	register int i;
113125979Ssam 
113225979Ssam 	for (i = 0; i < howmany(sizeof (struct cytpb), sizeof (long)); i++)
113325979Ssam 		uncache(lp++);
113425979Ssam }
113525979Ssam 
113625979Ssam /*
113725979Ssam  * Dump routine.
113825979Ssam  */
113930869Skarels #define	DUMPREC	(32*1024)
114025979Ssam cydump(dev)
114125979Ssam 	dev_t dev;
114225979Ssam {
114325979Ssam 	register struct cy_softc *cy;
114425979Ssam 	register int bs, num, start;
114525979Ssam 	register caddr_t addr;
114630294Ssam 	int unit = CYUNIT(dev), error;
114725979Ssam 
114825979Ssam 	if (unit >= NCY || cyminfo[unit] == 0 ||
114925979Ssam 	    (cy = &cy_softc[unit])->cy_bs == 0 || YCUNIT(dev) >= NYC)
115025979Ssam 		return (ENXIO);
115125979Ssam 	if (cywait(&cy->cy_ccb))
115225979Ssam 		return (EFAULT);
115325979Ssam #define	phys(a)	((caddr_t)((int)(a)&~0xc0000000))
115430294Ssam 	addr = phys(cyminfo[unit]->um_addr);
115525979Ssam 	num = maxfree, start = NBPG*2;
115625979Ssam 	while (num > 0) {
115730869Skarels 		bs = num > btoc(DUMPREC) ? btoc(DUMPREC) : num;
115825979Ssam 		error = cydwrite(cy, start, bs, addr);
115925979Ssam 		if (error)
116025979Ssam 			return (error);
116125979Ssam 		start += bs, num -= bs;
116225979Ssam 	}
116325979Ssam 	cyweof(cy, addr);
116425979Ssam 	cyweof(cy, addr);
116525979Ssam 	uncache(&cy->cy_tpb);
116625979Ssam 	if (cy->cy_tpb.tpstatus&CYS_ERR)
116725979Ssam 		return (EIO);
116825979Ssam 	cyrewind(cy, addr);
116925979Ssam 	return (0);
117025979Ssam }
117125979Ssam 
117225979Ssam cydwrite(cy, pf, npf, addr)
117325979Ssam 	register struct cy_softc *cy;
117425979Ssam 	int pf, npf;
117525979Ssam 	caddr_t addr;
117625979Ssam {
117725979Ssam 
117825979Ssam 	cy->cy_tpb.tpcmd = CY_WCOM;
117925979Ssam 	cy->cy_tpb.tpcontrol = CYCW_LOCK|CYCW_25IPS|CYCW_16BITS;
118025979Ssam 	cy->cy_tpb.tpstatus = 0;
118125979Ssam 	cy->cy_tpb.tpsize = htoms(npf*NBPG);
118225979Ssam 	cyldmba(cy->cy_tpb.tplink, (caddr_t)0);
118325979Ssam 	cyldmba(cy->cy_tpb.tpdata, (caddr_t)(pf*NBPG));
118425979Ssam 	cyldmba(cy->cy_ccb.cbtpb, (caddr_t)&cy->cy_tpb);
118525979Ssam 	cy->cy_ccb.cbgate = GATE_CLOSED;
118625979Ssam 	CY_GO(addr);
118725979Ssam 	if (cywait(&cy->cy_ccb))
118825979Ssam 		return (EFAULT);
118925979Ssam 	uncache(&cy->cy_tpb);
119025979Ssam 	if (cy->cy_tpb.tpstatus&CYS_ERR)
119125979Ssam 		return (EIO);
119225979Ssam 	return (0);
119325979Ssam }
119425979Ssam 
119525979Ssam cyweof(cy, addr)
119625979Ssam 	register struct cy_softc *cy;
119725979Ssam 	caddr_t addr;
119825979Ssam {
119925979Ssam 
120025979Ssam 	cy->cy_tpb.tpcmd = CY_WEOF;
120125979Ssam 	cy->cy_tpb.tpcount = htoms(1);
120225979Ssam 	cy->cy_ccb.cbgate = GATE_CLOSED;
120325979Ssam 	CY_GO(addr);
120425979Ssam 	(void) cywait(&cy->cy_ccb);
120525979Ssam }
120625979Ssam 
120725979Ssam cyrewind(cy, addr)
120825979Ssam 	register struct cy_softc *cy;
120925979Ssam 	caddr_t addr;
121025979Ssam {
121125979Ssam 
121225979Ssam 	cy->cy_tpb.tpcmd = CY_REW;
121325979Ssam 	cy->cy_tpb.tpcount = htoms(1);
121425979Ssam 	cy->cy_ccb.cbgate = GATE_CLOSED;
121525979Ssam 	CY_GO(addr);
121625979Ssam 	(void) cywait(&cy->cy_ccb);
121725979Ssam }
121824000Ssam #endif
1219