xref: /csrg-svn/sys/tahoe/vba/cy.c (revision 30439)
1*30439Skarels /*	cy.c	1.10	87/01/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 #define	CYMAXIO	(32*NBPG)		/* max i/o size */
7825979Ssam 
7924000Ssam /*
8025979Ssam  * Software state and shared command areas per controller.
8125979Ssam  *
8225979Ssam  * The i/o buffer must be defined statically to insure
8325979Ssam  * it's address will fit in 20-bits (YECH!!!!!!!!!!!!!!)
8424000Ssam  */
8525979Ssam struct cy_softc {
8625979Ssam 	struct	pte *cy_map;	/* pte's for mapped buffer i/o */
8725979Ssam 	caddr_t	cy_utl;		/* mapped virtual address */
8825979Ssam 	int	cy_bs;		/* controller's buffer size */
8925979Ssam 	struct	cyscp *cy_scp;	/* system configuration block address */
9025979Ssam 	struct	cyccb cy_ccb;	/* channel control block */
9125979Ssam 	struct	cyscb cy_scb;	/* system configuration block */
9225979Ssam 	struct	cytpb cy_tpb;	/* tape parameter block */
9325979Ssam 	struct	cytpb cy_nop;	/* nop parameter block for cyintr */
9430371Skarels 	char	cy_buf[CYMAXIO];/* intermediate buffer */
9525979Ssam } cy_softc[NCY];
9624000Ssam 
9725979Ssam /*
9825979Ssam  * Software state per tape transport.
9925979Ssam  */
10025979Ssam struct	yc_softc {
10125979Ssam 	char	yc_openf;	/* lock against multiple opens */
10225979Ssam 	char	yc_lastiow;	/* last operation was a write */
10325979Ssam 	short	yc_tact;	/* timeout is active */
10425979Ssam 	long	yc_timo;	/* time until timeout expires */
10525979Ssam 	u_short	yc_control;	/* copy of last tpcb.tpcontrol */
10625979Ssam 	u_short	yc_status;	/* copy of last tpcb.tpstatus */
10725979Ssam 	u_short	yc_resid;	/* copy of last bc */
10825979Ssam 	u_short	yc_dens;	/* prototype control word with density info */
10925979Ssam 	struct	tty *yc_ttyp;	/* user's tty for errors */
11025979Ssam 	daddr_t	yc_blkno;	/* block number, for block device tape */
11125979Ssam 	daddr_t	yc_nxrec;	/* position of end of tape, if known */
11230371Skarels 	int	yc_blksize;	/* current tape blocksize estimate */
11330371Skarels 	int	yc_blks;	/* number of I/O operations since open */
11430371Skarels 	int	yc_softerrs;	/* number of soft I/O errors since open */
11525979Ssam } yc_softc[NYC];
11624000Ssam 
11724000Ssam /*
11825979Ssam  * States for vm->um_tab.b_active, the per controller state flag.
11925979Ssam  * This is used to sequence control in the driver.
12024000Ssam  */
12125979Ssam #define	SSEEK	1		/* seeking */
12225979Ssam #define	SIO	2		/* doing seq i/o */
12325979Ssam #define	SCOM	3		/* sending control command */
12425979Ssam #define	SREW	4		/* sending a rewind */
12525979Ssam #define	SERASE	5		/* erase inter-record gap */
12625979Ssam #define	SERASED	6		/* erased inter-record gap */
12724000Ssam 
12825979Ssam /* there's no way to figure these out dynamically? -- yech */
12925979Ssam struct	cyscp *cyscp[] =
13025979Ssam     { (struct cyscp *)0xc0000c06, (struct cyscp *)0xc0000c16 };
13125979Ssam #define	NCYSCP	(sizeof (cyscp) / sizeof (cyscp[0]))
13225979Ssam 
13325857Ssam cyprobe(reg, vm)
13425857Ssam 	caddr_t reg;
13525857Ssam 	struct vba_ctlr *vm;
13625675Ssam {
13725857Ssam 	register br, cvec;			/* must be r12, r11 */
13830371Skarels 	register struct cy_softc *cy;
13930371Skarels 	int ctlr = vm->um_ctlr;
14025675Ssam 
14130294Ssam #ifdef lint
14230294Ssam 	br = 0; cvec = br; br = cvec;
14330294Ssam 	cyintr(0);
14430294Ssam #endif
14525857Ssam 	if (badcyaddr(reg+1))
14625675Ssam 		return (0);
14730371Skarels 	if (ctlr > NCYSCP || cyscp[ctlr] == 0)		/* XXX */
14830371Skarels 		return (0);
14930371Skarels 	cy = &cy_softc[ctlr];
15030371Skarels 	cy->cy_scp = cyscp[ctlr];			/* XXX */
15125979Ssam 	/*
15225979Ssam 	 * Tapemaster controller must have interrupt handler
15325979Ssam 	 * disable interrupt, so we'll just kludge things
15425979Ssam 	 * (stupid multibus non-vectored interrupt crud).
15525979Ssam 	 */
15630371Skarels 	if (cyinit(ctlr, reg)) {
15730371Skarels 		uncache(&cy->cy_tpb.tpcount);
15830371Skarels 		cy->cy_bs = htoms(cy->cy_tpb.tpcount);
15930371Skarels 		/*
16030371Skarels 		 * Setup nop parameter block for clearing interrupts.
16130371Skarels 		 */
16230371Skarels 		cy->cy_nop.tpcmd = CY_NOP;
16330371Skarels 		cy->cy_nop.tpcontrol = 0;
16430371Skarels 		/*
16530371Skarels 		 * Allocate page tables.
16630371Skarels 		 */
16730371Skarels 		vbmapalloc(btoc(CYMAXIO)+1, &cy->cy_map, &cy->cy_utl);
16830371Skarels 
16930371Skarels 		br = 0x13, cvec = 0x80;			/* XXX */
17030371Skarels 		return (sizeof (struct cyccb));
17130371Skarels 	} else
17230371Skarels 		return (0);
17325675Ssam }
17425675Ssam 
17524000Ssam /*
17625857Ssam  * Check to see if a drive is attached to a controller.
17725857Ssam  * Since we can only tell that a drive is there if a tape is loaded and
17825857Ssam  * the drive is placed online, we always indicate the slave is present.
17924000Ssam  */
18025857Ssam cyslave(vi, addr)
18125857Ssam 	struct vba_device *vi;
18225857Ssam 	caddr_t addr;
18324000Ssam {
18425857Ssam 
18525857Ssam #ifdef lint
18625857Ssam 	vi = vi; addr = addr;
18725857Ssam #endif
18825857Ssam 	return (1);
18925857Ssam }
19025857Ssam 
19125857Ssam cyattach(vi)
19225857Ssam 	struct vba_device *vi;
19325857Ssam {
19425979Ssam 	register struct cy_softc *cy;
19525979Ssam 	int ctlr = vi->ui_mi->um_ctlr;
19625857Ssam 
19725979Ssam 	yctocy[vi->ui_unit] = ctlr;
19825979Ssam 	cy = &cy_softc[ctlr];
19930371Skarels 	if (vi->ui_slave == 0 && cy->cy_bs)
20030371Skarels 		printf("; %dkb buffer", cy->cy_bs/1024);
20125857Ssam }
20225857Ssam 
20325857Ssam /*
20425857Ssam  * Initialize the controller after a controller reset or
20525857Ssam  * during autoconfigure.  All of the system control blocks
20625857Ssam  * are initialized and the controller is asked to configure
20725857Ssam  * itself for later use.
20825857Ssam  */
20930371Skarels cyinit(ctlr, addr)
21025979Ssam 	int ctlr;
21130371Skarels 	register caddr_t addr;
21225857Ssam {
21325979Ssam 	register struct cy_softc *cy = &cy_softc[ctlr];
21425675Ssam 	register int *pte;
21524000Ssam 
21624000Ssam 	/*
21725675Ssam 	 * Initialize the system configuration pointer.
21824000Ssam 	 */
21925675Ssam 	/* make kernel writable */
22025979Ssam 	pte = (int *)vtopte((struct proc *)0, btop(cy->cy_scp));
22125675Ssam 	*pte &= ~PG_PROT; *pte |= PG_KW;
22225979Ssam 	mtpr(TBIS, cy->cy_scp);
22325675Ssam 	/* load the correct values in the scp */
22425979Ssam 	cy->cy_scp->csp_buswidth = CSP_16BITS;
22525979Ssam 	cyldmba(cy->cy_scp->csp_scb, (caddr_t)&cy->cy_scb);
22625675Ssam 	/* put it back to read-only */
22725675Ssam 	*pte &= ~PG_PROT; *pte |= PG_KR;
22825979Ssam 	mtpr(TBIS, cy->cy_scp);
22925675Ssam 
23024000Ssam 	/*
23125675Ssam 	 * Init system configuration block.
23224000Ssam 	 */
23330371Skarels 	cy->cy_scb.csb_fixed = CSB_FIXED;
23425675Ssam 	/* set pointer to the channel control block */
23525979Ssam 	cyldmba(cy->cy_scb.csb_ccb, (caddr_t)&cy->cy_ccb);
23625675Ssam 
23724000Ssam 	/*
23825675Ssam 	 * Initialize the chanel control block.
23924000Ssam 	 */
24025979Ssam 	cy->cy_ccb.cbcw = CBCW_CLRINT;
24125979Ssam 	cy->cy_ccb.cbgate = GATE_OPEN;
24225675Ssam 	/* set pointer to the tape parameter block */
24325979Ssam 	cyldmba(cy->cy_ccb.cbtpb, (caddr_t)&cy->cy_tpb);
24425675Ssam 
24524000Ssam 	/*
24625979Ssam 	 * Issue a nop cmd and get the internal buffer size for buffered i/o.
24724000Ssam 	 */
24825979Ssam 	cy->cy_tpb.tpcmd = CY_NOP;
24925979Ssam 	cy->cy_tpb.tpcontrol = CYCW_16BITS;
25025979Ssam 	cy->cy_ccb.cbgate = GATE_CLOSED;
25125979Ssam 	CY_GO(addr);
25225979Ssam 	if (cywait(&cy->cy_ccb) || (cy->cy_tpb.tpstatus&CYS_ERR)) {
25325979Ssam 		uncache(&cy->cy_tpb.tpstatus);
25425979Ssam 		printf("cy%d: timeout or err during init, status=%b\n", ctlr,
25525979Ssam 		    cy->cy_tpb.tpstatus, CYS_BITS);
25625675Ssam 		return (0);
25725675Ssam 	}
25825979Ssam 	cy->cy_tpb.tpcmd = CY_CONFIG;
25925979Ssam 	cy->cy_tpb.tpcontrol = CYCW_16BITS;
26025979Ssam 	cy->cy_ccb.cbgate = GATE_CLOSED;
26125979Ssam 	CY_GO(addr);
26225979Ssam 	if (cywait(&cy->cy_ccb) || (cy->cy_tpb.tpstatus&CYS_ERR)) {
26325979Ssam 		uncache(&cy->cy_tpb.tpstatus);
26425979Ssam 		printf("cy%d: configuration failure, status=%b\n", ctlr,
26525979Ssam 		    cy->cy_tpb.tpstatus, CYS_BITS);
26625675Ssam 		return (0);
26725675Ssam 	}
26825675Ssam 	return (1);
26924000Ssam }
27024000Ssam 
27125979Ssam int	cytimer();
27225979Ssam /*
27325979Ssam  * Open the device.  Tapes are unique open
27425979Ssam  * devices, so we refuse if it is already open.
27525979Ssam  * We also check that a tape is available, and
27625979Ssam  * don't block waiting here; if you want to wait
27725979Ssam  * for a tape you should timeout in user code.
27825979Ssam  */
27925675Ssam cyopen(dev, flag)
28025979Ssam 	dev_t dev;
28125675Ssam 	register int flag;
28225675Ssam {
28325979Ssam 	register int ycunit;
28425979Ssam 	register struct vba_device *vi;
28525979Ssam 	register struct yc_softc *yc;
28625979Ssam 	int s;
28725675Ssam 
28825979Ssam 	ycunit = YCUNIT(dev);
28925979Ssam 	if (ycunit >= NYC || (vi = ycdinfo[ycunit]) == 0 || vi->ui_alive == 0)
29025675Ssam 		return (ENXIO);
29125979Ssam 	if ((yc = &yc_softc[ycunit])->yc_openf)
29225979Ssam 		return (EBUSY);
29330371Skarels 	yc->yc_openf = 1;
29425979Ssam #define	PACKUNIT(vi) \
29525979Ssam     (((vi->ui_slave&1)<<11)|((vi->ui_slave&2)<<9)|((vi->ui_slave&4)>>2))
29625979Ssam 	/* no way to select density */
29725979Ssam 	yc->yc_dens = PACKUNIT(vi)|CYCW_IE|CYCW_16BITS;
29830371Skarels 	if (yc->yc_tact == 0) {
29930371Skarels 		yc->yc_timo = INF;
30030371Skarels 		yc->yc_tact = 1;
30130371Skarels 		timeout(cytimer, (caddr_t)dev, 5*hz);
30230371Skarels 	}
30325979Ssam 	cycommand(dev, CY_SENSE, 1);
30425979Ssam 	if ((yc->yc_status&CYS_OL) == 0) {	/* not on-line */
30525979Ssam 		uprintf("yc%d: not online\n", ycunit);
306*30439Skarels 		yc->yc_openf = 0;
30725675Ssam 		return (ENXIO);
30825675Ssam 	}
30925979Ssam 	if ((flag&FWRITE) && (yc->yc_status&CYS_WP)) {
31025979Ssam 		uprintf("yc%d: no write ring\n", ycunit);
311*30439Skarels 		yc->yc_openf = 0;
31225675Ssam 		return (ENXIO);
31325675Ssam 	}
31425979Ssam 	yc->yc_blkno = (daddr_t)0;
31525979Ssam 	yc->yc_nxrec = INF;
31625979Ssam 	yc->yc_lastiow = 0;
31730371Skarels 	yc->yc_blksize = 1024;		/* guess > 0 */
31830371Skarels 	yc->yc_blks = 0;
31930371Skarels 	yc->yc_softerrs = 0;
32025979Ssam 	yc->yc_ttyp = u.u_ttyp;
32125675Ssam 	return (0);
32225675Ssam }
32325675Ssam 
32425979Ssam /*
32525979Ssam  * Close tape device.
32625979Ssam  *
32725979Ssam  * If tape was open for writing or last operation was a write,
32825979Ssam  * then write two EOF's and backspace over the last one.
32925979Ssam  * Unless this is a non-rewinding special file, rewind the tape.
33025979Ssam  * Make the tape available to others.
33125979Ssam  */
33225675Ssam cyclose(dev, flag)
33325979Ssam 	dev_t dev;
33430371Skarels 	int flag;
33525675Ssam {
33630371Skarels 	struct yc_softc *yc = &yc_softc[YCUNIT(dev)];
33725675Ssam 
33825979Ssam 	if (flag == FWRITE || (flag&FWRITE) && yc->yc_lastiow) {
33925979Ssam 		cycommand(dev, CY_WEOF, 2);
34025979Ssam 		cycommand(dev, CY_SREV, 1);
34125675Ssam 	}
34225979Ssam 	if ((minor(dev)&T_NOREWIND) == 0)
34325979Ssam 		/*
34425979Ssam 		 * 0 count means don't hang waiting for rewind complete
34525979Ssam 		 * rather ccybuf stays busy until the operation completes
34625979Ssam 		 * preventing further opens from completing by preventing
34725979Ssam 		 * a CY_SENSE from completing.
34825979Ssam 		 */
34925979Ssam 		cycommand(dev, CY_REW, 0);
35030371Skarels 	if (yc->yc_blks > 10 && yc->yc_softerrs > yc->yc_blks / 10)
35130371Skarels 		log(LOG_INFO, "yc%d: %d soft errors in %d blocks\n",
35230371Skarels 		    YCUNIT(dev), yc->yc_softerrs, yc->yc_blks);
35330371Skarels 	dlog((LOG_INFO, "%d soft errors in %d blocks\n",
35430371Skarels 	    yc->yc_softerrs, yc->yc_blks));
35525979Ssam 	yc->yc_openf = 0;
35625675Ssam }
35725675Ssam 
35824000Ssam /*
35925979Ssam  * Execute a command on the tape drive a specified number of times.
36024000Ssam  */
36125979Ssam cycommand(dev, com, count)
36225979Ssam 	dev_t dev;
36325979Ssam 	int com, count;
36424000Ssam {
36525979Ssam 	register struct buf *bp;
36625675Ssam 	int s;
36725675Ssam 
36825979Ssam 	bp = &ccybuf[CYUNIT(dev)];
36925675Ssam 	s = spl3();
37030371Skarels 	dlog((LOG_INFO, "cycommand(%o, %x, %d), b_flags %x\n",
37130371Skarels 	    dev, com, count, bp->b_flags));
37225979Ssam 	while (bp->b_flags&B_BUSY) {
37325979Ssam 		/*
37425979Ssam 		 * This special check is because B_BUSY never
37525979Ssam 		 * gets cleared in the non-waiting rewind case.
37625979Ssam 		 */
37725979Ssam 		if (bp->b_repcnt == 0 && (bp->b_flags&B_DONE))
37825979Ssam 			break;
37925979Ssam 		bp->b_flags |= B_WANTED;
38025979Ssam 		sleep((caddr_t)bp, PRIBIO);
38125675Ssam 	}
38225979Ssam 	bp->b_flags = B_BUSY|B_READ;
38325675Ssam 	splx(s);
38425979Ssam 	bp->b_dev = dev;
38525979Ssam 	bp->b_repcnt = count;
38625979Ssam 	bp->b_command = com;
38725979Ssam 	bp->b_blkno = 0;
38825979Ssam 	cystrategy(bp);
38925979Ssam 	/*
39025979Ssam 	 * In case of rewind from close; don't wait.
39125979Ssam 	 * This is the only case where count can be 0.
39225979Ssam 	 */
39325979Ssam 	if (count == 0)
39425979Ssam 		return;
39530371Skarels 	biowait(bp);
39625979Ssam 	if (bp->b_flags&B_WANTED)
39725979Ssam 		wakeup((caddr_t)bp);
39825979Ssam 	bp->b_flags &= B_ERROR;
39924000Ssam }
40024000Ssam 
40125675Ssam cystrategy(bp)
40225675Ssam 	register struct buf *bp;
40325675Ssam {
40425979Ssam 	int ycunit = YCUNIT(bp->b_dev);
40525979Ssam 	register struct vba_ctlr *vm;
40625979Ssam 	register struct buf *dp;
40725675Ssam 	int s;
40825675Ssam 
40925979Ssam 	/*
41025979Ssam 	 * Put transfer at end of unit queue.
41125979Ssam 	 */
41230371Skarels 	dlog((LOG_INFO, "cystrategy(%o, %x)\n", bp->b_dev, bp->b_command));
41325979Ssam 	dp = &ycutab[ycunit];
41425675Ssam 	bp->av_forw = NULL;
41530371Skarels 	bp->b_errcnt = 0;
41625979Ssam 	vm = ycdinfo[ycunit]->ui_mi;
41725979Ssam 	/* BEGIN GROT */
41825979Ssam 	if (bp == &rcybuf[CYUNIT(bp->b_dev)]) {
41925979Ssam 		if (bp->b_bcount > CYMAXIO) {
42025979Ssam 			uprintf("cy%d: i/o size too large\n", vm->um_ctlr);
42125979Ssam 			bp->b_error = EIO;
42225979Ssam 			bp->b_resid = bp->b_bcount;
42325979Ssam 			bp->b_flags |= B_ERROR;
42430371Skarels 			biodone(bp);
42525675Ssam 			return;
42625675Ssam 		}
42725979Ssam 		vbasetup(bp, CYMAXIO);
42824000Ssam 	}
42925979Ssam 	/* END GROT */
43025675Ssam 	s = spl3();
43125979Ssam 	if (dp->b_actf == NULL) {
43225979Ssam 		dp->b_actf = bp;
43325979Ssam 		/*
43425979Ssam 		 * Transport not already active...
43525979Ssam 		 * put at end of controller queue.
43625979Ssam 		 */
43725979Ssam 		 dp->b_forw = NULL;
43825979Ssam 		 if (vm->um_tab.b_actf == NULL)
43925979Ssam 			vm->um_tab.b_actf = dp;
44025979Ssam 		else
44125979Ssam 			vm->um_tab.b_actl->b_forw = dp;
44225979Ssam 	} else
44325979Ssam 		dp->b_actl->av_forw = bp;
44425979Ssam 	dp->b_actl = bp;
44525979Ssam 	/*
44625979Ssam 	 * If the controller is not busy, get it going.
44725979Ssam 	 */
44825979Ssam 	if (vm->um_tab.b_active == 0)
44925979Ssam 		cystart(vm);
45024000Ssam 	splx(s);
45124000Ssam }
45224000Ssam 
45324000Ssam /*
45425979Ssam  * Start activity on a cy controller.
45524000Ssam  */
45625979Ssam cystart(vm)
45725979Ssam 	register struct vba_ctlr *vm;
45824000Ssam {
45925979Ssam 	register struct buf *bp, *dp;
46025979Ssam 	register struct yc_softc *yc;
46125979Ssam 	register struct cy_softc *cy;
46225979Ssam 	int ycunit;
46325979Ssam 	daddr_t blkno;
46424000Ssam 
46530371Skarels 	dlog((LOG_INFO, "cystart()\n"));
46625979Ssam 	/*
46725979Ssam 	 * Look for an idle transport on the controller.
46825979Ssam 	 */
46925979Ssam loop:
47025979Ssam 	if ((dp = vm->um_tab.b_actf) == NULL)
47125675Ssam 		return;
47225979Ssam 	if ((bp = dp->b_actf) == NULL) {
47325979Ssam 		vm->um_tab.b_actf = dp->b_forw;
47425979Ssam 		goto loop;
47525675Ssam 	}
47625979Ssam 	ycunit = YCUNIT(bp->b_dev);
47725979Ssam 	yc = &yc_softc[ycunit];
47825979Ssam 	cy = &cy_softc[CYUNIT(bp->b_dev)];
47925979Ssam 	/*
48025979Ssam 	 * Default is that last command was NOT a write command;
48125979Ssam 	 * if we do a write command we will notice this in cyintr().
48225979Ssam 	 */
48325979Ssam 	yc->yc_lastiow = 0;
48425979Ssam 	if (yc->yc_openf < 0 ||
48525979Ssam 	    (bp->b_command != CY_SENSE && (cy->cy_tpb.tpstatus&CYS_OL) == 0)) {
48625979Ssam 		/*
48725979Ssam 		 * Have had a hard error on a non-raw tape
48825979Ssam 		 * or the tape unit is now unavailable (e.g.
48925979Ssam 		 * taken off line).
49025979Ssam 		 */
49130371Skarels 		dlog((LOG_INFO, "openf %d command %x status %b\n",
49230371Skarels 		   yc->yc_openf, bp->b_command, cy->cy_tpb.tpstatus, CYS_BITS));
49325979Ssam 		bp->b_flags |= B_ERROR;
49425979Ssam 		goto next;
49525675Ssam 	}
49625979Ssam 	if (bp == &ccybuf[CYUNIT(bp->b_dev)]) {
49725979Ssam 		/*
49825979Ssam 		 * Execute control operation with the specified count.
49925979Ssam 		 *
50025979Ssam 		 * Set next state; give 5 minutes to complete
50125979Ssam 		 * rewind or file mark search, or 10 seconds per
50225979Ssam 		 * iteration (minimum 60 seconds and max 5 minutes)
50325979Ssam 		 * to complete other ops.
50425979Ssam 		 */
50525979Ssam 		if (bp->b_command == CY_REW) {
50625979Ssam 			vm->um_tab.b_active = SREW;
50725979Ssam 			yc->yc_timo = 5*60;
50825979Ssam 		} else {
50925979Ssam 			vm->um_tab.b_active = SCOM;
51025979Ssam 			yc->yc_timo = imin(imax(10*(int)bp->b_repcnt,60),5*60);
51125979Ssam 		}
51225979Ssam 		cy->cy_tpb.tprec = htoms(bp->b_repcnt);
51325979Ssam 		goto dobpcmd;
51424000Ssam 	}
51525979Ssam 	/*
51625979Ssam 	 * The following checks handle boundary cases for operation
51725979Ssam 	 * on no-raw tapes.  On raw tapes the initialization of
51825979Ssam 	 * yc->yc_nxrec by cyphys causes them to be skipped normally
51925979Ssam 	 * (except in the case of retries).
52025979Ssam 	 */
52125979Ssam 	if (bdbtofsb(bp->b_blkno) > yc->yc_nxrec) {
52225979Ssam 		/*
52325979Ssam 		 * Can't read past known end-of-file.
52425979Ssam 		 */
52525979Ssam 		bp->b_flags |= B_ERROR;
52625979Ssam 		bp->b_error = ENXIO;
52725979Ssam 		goto next;
52824000Ssam 	}
52925979Ssam 	if (bdbtofsb(bp->b_blkno) == yc->yc_nxrec && bp->b_flags&B_READ) {
53025979Ssam 		/*
53125979Ssam 		 * Reading at end of file returns 0 bytes.
53225979Ssam 		 */
53325979Ssam 		bp->b_resid = bp->b_bcount;
53425979Ssam 		clrbuf(bp);
53525979Ssam 		goto next;
53624000Ssam 	}
53725979Ssam 	if ((bp->b_flags&B_READ) == 0)
53825979Ssam 		/*
53925979Ssam 		 * Writing sets EOF.
54025979Ssam 		 */
54125979Ssam 		yc->yc_nxrec = bdbtofsb(bp->b_blkno) + 1;
54225979Ssam 	if ((blkno = yc->yc_blkno) == bdbtofsb(bp->b_blkno)) {
54325979Ssam 		caddr_t addr;
54425979Ssam 		int cmd;
54525675Ssam 
54625979Ssam 		/*
54725979Ssam 		 * Choose the appropriate i/o command based on the
54830371Skarels 		 * transfer size, the estimated block size,
54930371Skarels 		 * and the controller's internal buffer size.
55025979Ssam 		 * If we're retrying a read on a raw device because
55125979Ssam 		 * the original try was a buffer request which failed
55225979Ssam 		 * due to a record length error, then we force the use
55325979Ssam 		 * of the raw controller read (YECH!!!!).
55425979Ssam 		 */
55525979Ssam 		if (bp->b_flags&B_READ) {
55630371Skarels 			if ((bp->b_bcount > cy->cy_bs &&
55730371Skarels 			    yc->yc_blksize > cy->cy_bs) || bp->b_errcnt)
55825979Ssam 				cmd = CY_RCOM;
55925979Ssam 			else
56025979Ssam 				cmd = CY_BRCOM;
56125979Ssam 		} else {
56225979Ssam 			/*
56325979Ssam 			 * On write error retries erase the
56425979Ssam 			 * inter-record gap before rewriting.
56525979Ssam 			 */
56625979Ssam 			if (vm->um_tab.b_errcnt &&
56725979Ssam 			    vm->um_tab.b_active != SERASED) {
56825979Ssam 				vm->um_tab.b_active = SERASE;
56925979Ssam 				bp->b_command = CY_ERASE;
57025979Ssam 				yc->yc_timo = 60;
57125979Ssam 				goto dobpcmd;
57225675Ssam 			}
57325979Ssam 			cmd = (bp->b_bcount > cy->cy_bs) ? CY_WCOM : CY_BWCOM;
57425675Ssam 		}
57525979Ssam 		vm->um_tab.b_active = SIO;
57625979Ssam 		addr = (caddr_t)vbastart(bp, cy->cy_buf,
57725979Ssam 		    (long *)cy->cy_map, cy->cy_utl);
57825979Ssam 		cy->cy_tpb.tpcmd = cmd;
57925979Ssam 		cy->cy_tpb.tpcontrol = yc->yc_dens;
58025979Ssam 		if (cmd == CY_RCOM || cmd == CY_WCOM)
58125979Ssam 			cy->cy_tpb.tpcontrol |= CYCW_LOCK;
58225979Ssam 		cy->cy_tpb.tpstatus = 0;
58325979Ssam 		cy->cy_tpb.tpcount = 0;
58425979Ssam 		cyldmba(cy->cy_tpb.tpdata, (caddr_t)addr);
58525979Ssam 		cy->cy_tpb.tprec = 0;
58630371Skarels 		if (cmd == CY_BRCOM && bp->b_bcount > cy->cy_bs)
58730371Skarels 			cy->cy_tpb.tpsize = htoms(cy->cy_bs);
58830371Skarels 		else
58930371Skarels 			cy->cy_tpb.tpsize = htoms(bp->b_bcount);
59025979Ssam 		cyldmba(cy->cy_tpb.tplink, (caddr_t)0);
59125979Ssam 		do
59225979Ssam 			uncache(&cy->cy_ccb.cbgate);
59325979Ssam 		while (cy->cy_ccb.cbgate == GATE_CLOSED);
59425979Ssam 		cyldmba(cy->cy_ccb.cbtpb, (caddr_t)&cy->cy_tpb);
59525979Ssam 		cy->cy_ccb.cbcw = CBCW_IE;
59625979Ssam 		cy->cy_ccb.cbgate = GATE_CLOSED;
59730371Skarels 		dlog((LOG_INFO, "CY_GO(%x) cmd %x control %x size %d\n",
59825979Ssam 		    vm->um_addr, cy->cy_tpb.tpcmd, cy->cy_tpb.tpcontrol,
59930371Skarels 		    htoms(cy->cy_tpb.tpsize)));
60025979Ssam 		CY_GO(vm->um_addr);
60125979Ssam 		return;
60224000Ssam 	}
60325979Ssam 	/*
60425979Ssam 	 * Tape positioned incorrectly; set to seek forwards
60525979Ssam 	 * or backwards to the correct spot.  This happens
60625979Ssam 	 * for raw tapes only on error retries.
60725979Ssam 	 */
60825979Ssam 	vm->um_tab.b_active = SSEEK;
60925979Ssam 	if (blkno < bdbtofsb(bp->b_blkno)) {
61025979Ssam 		bp->b_command = CY_SFORW;
61125979Ssam 		cy->cy_tpb.tprec = htoms(bdbtofsb(bp->b_blkno) - blkno);
61225979Ssam 	} else {
61325979Ssam 		bp->b_command = CY_SREV;
61425979Ssam 		cy->cy_tpb.tprec = htoms(blkno - bdbtofsb(bp->b_blkno));
61524000Ssam 	}
61625979Ssam 	yc->yc_timo = imin(imax(10 * htoms(cy->cy_tpb.tprec), 60), 5*60);
61725979Ssam dobpcmd:
61825979Ssam 	/*
61925979Ssam 	 * Do the command in bp.  Reverse direction commands
62025979Ssam 	 * are indicated by having CYCW_REV or'd into their
62125979Ssam 	 * value.  For these we must set the appropriate bit
62225979Ssam 	 * in the control field.
62325979Ssam 	 */
62425979Ssam 	if (bp->b_command&CYCW_REV) {
62525979Ssam 		cy->cy_tpb.tpcmd = bp->b_command &~ CYCW_REV;
62625979Ssam 		cy->cy_tpb.tpcontrol = yc->yc_dens | CYCW_REV;
62725979Ssam 	} else {
62825979Ssam 		cy->cy_tpb.tpcmd = bp->b_command;
62925979Ssam 		cy->cy_tpb.tpcontrol = yc->yc_dens;
63024000Ssam 	}
63125979Ssam 	cy->cy_tpb.tpstatus = 0;
63225979Ssam 	cy->cy_tpb.tpcount = 0;
63325979Ssam 	cyldmba(cy->cy_tpb.tplink, (caddr_t)0);
63425979Ssam 	do
63525979Ssam 		uncache(&cy->cy_ccb.cbgate);
63625979Ssam 	while (cy->cy_ccb.cbgate == GATE_CLOSED);
63725979Ssam 	cyldmba(cy->cy_ccb.cbtpb, (caddr_t)&cy->cy_tpb);
63825979Ssam 	cy->cy_ccb.cbcw = CBCW_IE;
63925979Ssam 	cy->cy_ccb.cbgate = GATE_CLOSED;
64030371Skarels 	dlog((LOG_INFO, "CY_GO(%x) cmd %x control %x rec %d\n",
64125979Ssam 	    vm->um_addr, cy->cy_tpb.tpcmd, cy->cy_tpb.tpcontrol,
64230371Skarels 	    htoms(cy->cy_tpb.tprec)));
64325979Ssam 	CY_GO(vm->um_addr);
64425979Ssam 	return;
64525979Ssam next:
64625979Ssam 	/*
64725979Ssam 	 * Done with this operation due to error or the
64825979Ssam 	 * fact that it doesn't do anything.  Release VERSAbus
64925979Ssam 	 * resource (if any), dequeue the transfer and continue
65025979Ssam 	 * processing this slave.
65125979Ssam 	 */
65225979Ssam 	if (bp == &rcybuf[CYUNIT(bp->b_dev)])
65325979Ssam 		vbadone(bp, cy->cy_buf, (long *)cy->cy_map, cy->cy_utl);
65425979Ssam 	vm->um_tab.b_errcnt = 0;
65525979Ssam 	dp->b_actf = bp->av_forw;
65630371Skarels 	biodone(bp);
65725979Ssam 	goto loop;
65825675Ssam }
65925675Ssam 
66025675Ssam /*
66125979Ssam  * Cy interrupt routine.
66225675Ssam  */
66325979Ssam cyintr(cipher)
66425979Ssam 	int cipher;
66525675Ssam {
66625979Ssam 	struct buf *dp;
66724000Ssam 	register struct buf *bp;
66825979Ssam 	register struct vba_ctlr *vm = cyminfo[cipher];
66925979Ssam 	register struct cy_softc *cy;
67025979Ssam 	register struct yc_softc *yc;
67125979Ssam 	int cyunit, err;
67225979Ssam 	register state;
67324000Ssam 
67430371Skarels 	dlog((LOG_INFO, "cyintr(%d)\n", cipher));
67525979Ssam 	/*
67625979Ssam 	 * First, turn off the interrupt from the controller
67725979Ssam 	 * (device uses Multibus non-vectored interrupts...yech).
67825979Ssam 	 */
67925979Ssam 	cy = &cy_softc[vm->um_ctlr];
68025979Ssam 	cy->cy_ccb.cbcw = CBCW_CLRINT;
68130294Ssam 	cyldmba(cy->cy_ccb.cbtpb, (caddr_t)&cy->cy_nop);
68225979Ssam 	cy->cy_ccb.cbgate = GATE_CLOSED;
68325979Ssam 	CY_GO(vm->um_addr);
68425979Ssam 	if ((dp = vm->um_tab.b_actf) == NULL) {
68530371Skarels 		dlog((LOG_ERR, "cy%d: stray interrupt", vm->um_ctlr));
68624000Ssam 		return;
68724000Ssam 	}
68825979Ssam 	bp = dp->b_actf;
68925979Ssam 	cyunit = CYUNIT(bp->b_dev);
69025979Ssam 	cy = &cy_softc[cyunit];
69125979Ssam 	cyuncachetpb(cy);
69230294Ssam 	yc = &yc_softc[YCUNIT(bp->b_dev)];
69325979Ssam 	/*
69425984Ssam 	 * If last command was a rewind and tape is
69525984Ssam 	 * still moving, wait for the operation to complete.
69625979Ssam 	 */
69725979Ssam 	if (vm->um_tab.b_active == SREW) {
69825979Ssam 		vm->um_tab.b_active = SCOM;
69925979Ssam 		if ((cy->cy_tpb.tpstatus&CYS_RDY) == 0) {
70025979Ssam 			yc->yc_timo = 5*60;	/* 5 minutes */
70125979Ssam 			return;
70224000Ssam 		}
70324000Ssam 	}
70425979Ssam 	/*
70525979Ssam 	 * An operation completed...record status.
70625979Ssam 	 */
70725979Ssam 	yc->yc_timo = INF;
70825979Ssam 	yc->yc_control = cy->cy_tpb.tpcontrol;
70925979Ssam 	yc->yc_status = cy->cy_tpb.tpstatus;
71025979Ssam 	yc->yc_resid = bp->b_bcount - htoms(cy->cy_tpb.tpcount);
71130371Skarels 	dlog((LOG_INFO, "cmd %x control %b status %b resid %d\n",
71225979Ssam 	    cy->cy_tpb.tpcmd, yc->yc_control, CYCW_BITS,
71330371Skarels 	    yc->yc_status, CYS_BITS, yc->yc_resid));
71425979Ssam 	if ((bp->b_flags&B_READ) == 0)
71525979Ssam 		yc->yc_lastiow = 1;
71625979Ssam 	state = vm->um_tab.b_active;
71725979Ssam 	vm->um_tab.b_active = 0;
71825979Ssam 	/*
71925979Ssam 	 * Check for errors.
72025979Ssam 	 */
72125979Ssam 	if (cy->cy_tpb.tpstatus&CYS_ERR) {
72225979Ssam 		err = cy->cy_tpb.tpstatus&CYS_ERR;
72330371Skarels 		dlog((LOG_INFO, "error %d\n", err));
72425979Ssam 		/*
72525979Ssam 		 * If we hit the end of tape file, update our position.
72625979Ssam 		 */
72725979Ssam 		if (err == CYER_FM) {
72825979Ssam 			yc->yc_status |= CYS_FM;
72925979Ssam 			state = SCOM;		/* force completion */
73025979Ssam 			cyseteof(bp);		/* set blkno and nxrec */
73125979Ssam 			goto opdone;
73225979Ssam 		}
73325979Ssam 		/*
73425979Ssam 		 * Fix up errors which occur due to backspacing over
73525979Ssam 		 * the beginning of the tape.
73625979Ssam 		 */
73725979Ssam 		if (err == CYER_BOT && cy->cy_tpb.tpcontrol&CYCW_REV) {
73825979Ssam 			yc->yc_status |= CYS_BOT;
73925979Ssam 			goto ignoreerr;
74025979Ssam 		}
74125979Ssam 		/*
74225979Ssam 		 * If we were reading raw tape and the only error was that the
74325979Ssam 		 * record was too long, then we don't consider this an error.
74425979Ssam 		 */
74525979Ssam 		if (bp == &rcybuf[cyunit] && (bp->b_flags&B_READ) &&
74625979Ssam 		    err == CYER_STROBE) {
74725979Ssam 			/*
74830371Skarels 			 * Retry reads with the command changed to
74930371Skarels 			 * a raw read if necessary.  Setting b_errcnt
75025979Ssam 			 * here causes cystart (above) to force a CY_RCOM.
75125979Ssam 			 */
75230371Skarels 			if (htoms(cy->cy_tpb.tprec) > cy->cy_bs &&
75330371Skarels 			    bp->b_bcount > cy->cy_bs &&
75430371Skarels 			    yc->yc_blksize <= cy->cy_bs &&
75530371Skarels 			    bp->b_errcnt++ == 0) {
75630371Skarels 				yc->yc_blkno++;
75730371Skarels 				goto opcont;
75830371Skarels 			} else
75925979Ssam 				goto ignoreerr;
76025979Ssam 		}
76125979Ssam 		/*
76225979Ssam 		 * If error is not hard, and this was an i/o operation
76325979Ssam 		 * retry up to 8 times.
76425979Ssam 		 */
76525984Ssam 		if (((1<<err)&CYER_SOFT) && state == SIO) {
76625979Ssam 			if (++vm->um_tab.b_errcnt < 7) {
76725979Ssam 				yc->yc_blkno++;
76825979Ssam 				goto opcont;
76925979Ssam 			}
77025979Ssam 		} else
77125979Ssam 			/*
77225979Ssam 			 * Hard or non-i/o errors on non-raw tape
77325979Ssam 			 * cause it to close.
77425979Ssam 			 */
77530371Skarels 			if (yc->yc_openf > 0 && bp != &rcybuf[cyunit])
77625979Ssam 				yc->yc_openf = -1;
77725979Ssam 		/*
77825979Ssam 		 * Couldn't recover from error.
77925979Ssam 		 */
78025979Ssam 		tprintf(yc->yc_ttyp,
78130371Skarels 		    "yc%d: hard error bn%d status=%b, %s\n", YCUNIT(bp->b_dev),
78230371Skarels 		    bp->b_blkno, yc->yc_status, CYS_BITS,
78330371Skarels 		    (err < NCYERROR) ? cyerror[err] : "");
78425979Ssam 		bp->b_flags |= B_ERROR;
78525979Ssam 		goto opdone;
78624000Ssam 	}
78725979Ssam 	/*
78825979Ssam 	 * Advance tape control FSM.
78925979Ssam 	 */
79025979Ssam ignoreerr:
79125979Ssam 	/*
79225979Ssam 	 * If we hit a tape mark update our position.
79325979Ssam 	 */
79425979Ssam 	if (yc->yc_status&CYS_FM && bp->b_flags&B_READ) {
79525979Ssam 		cyseteof(bp);
79625979Ssam 		goto opdone;
79725675Ssam 	}
79825979Ssam 	switch (state) {
79924000Ssam 
80025979Ssam 	case SIO:
80125979Ssam 		/*
80225979Ssam 		 * Read/write increments tape block number.
80325979Ssam 		 */
80425979Ssam 		yc->yc_blkno++;
80530371Skarels 		yc->yc_blks++;
80630371Skarels 		if (vm->um_tab.b_errcnt || yc->yc_status & CYS_CR)
80730371Skarels 			yc->yc_softerrs++;
80830371Skarels 		yc->yc_blksize = htoms(cy->cy_tpb.tpcount);
80930371Skarels 		dlog((LOG_ERR, "blocksize %d", yc->yc_blksize));
81025979Ssam 		goto opdone;
81124000Ssam 
81225979Ssam 	case SCOM:
81325979Ssam 		/*
81425979Ssam 		 * For forward/backward space record update current position.
81525979Ssam 		 */
81630294Ssam 		if (bp == &ccybuf[CYUNIT(bp->b_dev)])
81730294Ssam 			switch ((int)bp->b_command) {
81824000Ssam 
81930294Ssam 			case CY_SFORW:
82030294Ssam 				yc->yc_blkno -= bp->b_repcnt;
82130294Ssam 				break;
82224000Ssam 
82330294Ssam 			case CY_SREV:
82430294Ssam 				yc->yc_blkno += bp->b_repcnt;
82530294Ssam 				break;
82630294Ssam 			}
82725979Ssam 		goto opdone;
82825979Ssam 
82925979Ssam 	case SSEEK:
83025979Ssam 		yc->yc_blkno = bdbtofsb(bp->b_blkno);
83125979Ssam 		goto opcont;
83224000Ssam 
83325979Ssam 	case SERASE:
83425979Ssam 		/*
83525979Ssam 		 * Completed erase of the inter-record gap due to a
83625979Ssam 		 * write error; now retry the write operation.
83725979Ssam 		 */
83825979Ssam 		vm->um_tab.b_active = SERASED;
83925979Ssam 		goto opcont;
84024000Ssam 	}
84125675Ssam 
84225979Ssam opdone:
84325979Ssam 	/*
84425979Ssam 	 * Reset error count and remove from device queue.
84525979Ssam 	 */
84625979Ssam 	vm->um_tab.b_errcnt = 0;
84725979Ssam 	dp->b_actf = bp->av_forw;
84825979Ssam 	/*
84925979Ssam 	 * Save resid and release resources.
85025979Ssam 	 */
85125979Ssam 	bp->b_resid = bp->b_bcount - htoms(cy->cy_tpb.tpcount);
85225979Ssam 	if (bp == &rcybuf[CYUNIT(bp->b_dev)])
85325979Ssam 		vbadone(bp, cy->cy_buf, (long *)cy->cy_map, cy->cy_utl);
85430371Skarels 	biodone(bp);
85525979Ssam 	/*
85625979Ssam 	 * Circulate slave to end of controller
85725979Ssam 	 * queue to give other slaves a chance.
85825979Ssam 	 */
85925979Ssam 	vm->um_tab.b_actf = dp->b_forw;
86025979Ssam 	if (dp->b_actf) {
86125979Ssam 		dp->b_forw = NULL;
86225979Ssam 		if (vm->um_tab.b_actf == NULL)
86325979Ssam 			vm->um_tab.b_actf = dp;
86425979Ssam 		else
86525979Ssam 			vm->um_tab.b_actl->b_forw = dp;
86624000Ssam 	}
86725979Ssam 	if (vm->um_tab.b_actf == 0)
86824000Ssam 		return;
86925979Ssam opcont:
87025979Ssam 	cystart(vm);
87124000Ssam }
87224000Ssam 
87325979Ssam cytimer(dev)
87425979Ssam 	int dev;
87524000Ssam {
87625979Ssam 	register struct yc_softc *yc = &yc_softc[YCUNIT(dev)];
87725979Ssam 	int s;
87824000Ssam 
87930371Skarels 	if (yc->yc_openf == 0 && yc->yc_timo == INF) {
88030371Skarels 		yc->yc_tact = 0;
88130371Skarels 		return;
88230371Skarels 	}
88325979Ssam 	if (yc->yc_timo != INF && (yc->yc_timo -= 5) < 0) {
88425979Ssam 		printf("yc%d: lost interrupt\n", YCUNIT(dev));
88525979Ssam 		yc->yc_timo = INF;
88625979Ssam 		s = spl3();
88725979Ssam 		cyintr(CYUNIT(dev));
88825979Ssam 		splx(s);
88924000Ssam 	}
89025979Ssam 	timeout(cytimer, (caddr_t)dev, 5*hz);
89124000Ssam }
89224000Ssam 
89325979Ssam cyseteof(bp)
89425979Ssam 	register struct buf *bp;
89524000Ssam {
89625979Ssam 	register int cyunit = CYUNIT(bp->b_dev);
89725979Ssam 	register struct cy_softc *cy = &cy_softc[cyunit];
89825979Ssam 	register struct yc_softc *yc = &yc_softc[YCUNIT(bp->b_dev)];
89924000Ssam 
90025979Ssam 	if (bp == &ccybuf[cyunit]) {
90125979Ssam 		if (yc->yc_blkno > bdbtofsb(bp->b_blkno)) {
90225979Ssam 			/* reversing */
90325979Ssam 			yc->yc_nxrec = bdbtofsb(bp->b_blkno) -
90425979Ssam 			    htoms(cy->cy_tpb.tpcount);
90525979Ssam 			yc->yc_blkno = yc->yc_nxrec;
90625979Ssam 		} else {
90725979Ssam 			yc->yc_blkno = bdbtofsb(bp->b_blkno) +
90825979Ssam 			    htoms(cy->cy_tpb.tpcount);
90925979Ssam 			yc->yc_nxrec = yc->yc_blkno - 1;
91024000Ssam 		}
91125675Ssam 		return;
91225675Ssam 	}
91325979Ssam 	/* eof on read */
91425979Ssam 	yc->yc_nxrec = bdbtofsb(bp->b_blkno);
91524000Ssam }
91624000Ssam 
91725979Ssam cyread(dev, uio)
91825979Ssam 	dev_t dev;
91925979Ssam 	struct uio *uio;
92025675Ssam {
92125979Ssam 	int errno;
92225675Ssam 
92325979Ssam 	errno = cyphys(dev, uio);
92425979Ssam 	if (errno)
92525979Ssam 		return (errno);
92625979Ssam 	return (physio(cystrategy, &rcybuf[CYUNIT(dev)], dev, B_READ, minphys, uio));
92725675Ssam }
92825675Ssam 
92925979Ssam cywrite(dev, uio)
93025979Ssam 	dev_t dev;
93125979Ssam 	struct uio *uio;
93224000Ssam {
93325979Ssam 	int errno;
93424000Ssam 
93525979Ssam 	errno = cyphys(dev, uio);
93625979Ssam 	if (errno)
93725979Ssam 		return (errno);
93825979Ssam 	return (physio(cystrategy, &rcybuf[CYUNIT(dev)], dev, B_WRITE, minphys, uio));
93924000Ssam }
94024000Ssam 
94124000Ssam /*
94225979Ssam  * Check that a raw device exits.
94325979Ssam  * If it does, set up the yc_blkno and yc_nxrec
94425979Ssam  * so that the tape will appear positioned correctly.
94525979Ssam  */
94625979Ssam cyphys(dev, uio)
94725675Ssam 	dev_t dev;
94825675Ssam 	struct uio *uio;
94925675Ssam {
95025979Ssam 	register int ycunit = YCUNIT(dev);
95125979Ssam 	register daddr_t a;
95225979Ssam 	register struct yc_softc *yc;
95325979Ssam 	register struct vba_device *vi;
95425675Ssam 
95525979Ssam 	if (ycunit >= NYC || (vi = ycdinfo[ycunit]) == 0 || vi->ui_alive == 0)
95625979Ssam 		return (ENXIO);
95725979Ssam 	yc = &yc_softc[ycunit];
95825979Ssam 	a = bdbtofsb(uio->uio_offset >> DEV_BSHIFT);
95925979Ssam 	yc->yc_blkno = a;
96025979Ssam 	yc->yc_nxrec = a + 1;
96125979Ssam 	return (0);
96225675Ssam }
96325675Ssam 
96425675Ssam /*ARGSUSED*/
96525675Ssam cyioctl(dev, cmd, data, flag)
96625979Ssam 	caddr_t data;
96725675Ssam 	dev_t dev;
96825675Ssam {
96925979Ssam 	int ycunit = YCUNIT(dev);
97025979Ssam 	register struct yc_softc *yc = &yc_softc[ycunit];
97125979Ssam 	register struct buf *bp = &ccybuf[CYUNIT(dev)];
97225979Ssam 	register callcount;
97325979Ssam 	int fcount, op;
97425979Ssam 	struct mtop *mtop;
97525979Ssam 	struct mtget *mtget;
97625979Ssam 	/* we depend of the values and order of the MT codes here */
97725979Ssam 	static cyops[] =
97830371Skarels 	{CY_WEOF,CY_FSF,CY_BSF,CY_SFORW,CY_SREV,CY_REW,CY_OFFL,CY_SENSE};
97925675Ssam 
98025675Ssam 	switch (cmd) {
98125675Ssam 
98225979Ssam 	case MTIOCTOP:	/* tape operation */
98325979Ssam 		mtop = (struct mtop *)data;
98425979Ssam 		switch (op = mtop->mt_op) {
98525675Ssam 
98625979Ssam 		case MTWEOF:
98730371Skarels 			callcount = mtop->mt_count;
98830371Skarels 			fcount = 1;
98930371Skarels 			break;
99030371Skarels 
99125979Ssam 		case MTFSR: case MTBSR:
99230371Skarels 			callcount = 1;
99330371Skarels 			fcount = mtop->mt_count;
99430371Skarels 			break;
99530371Skarels 
99625979Ssam 		case MTFSF: case MTBSF:
99725979Ssam 			callcount = mtop->mt_count;
99825979Ssam 			fcount = 1;
99925979Ssam 			break;
100025675Ssam 
100125979Ssam 		case MTREW: case MTOFFL: case MTNOP:
100225979Ssam 			callcount = 1;
100325979Ssam 			fcount = 1;
100425979Ssam 			break;
100525675Ssam 
100625979Ssam 		default:
100725979Ssam 			return (ENXIO);
100825979Ssam 		}
100925979Ssam 		if (callcount <= 0 || fcount <= 0)
101025979Ssam 			return (EINVAL);
101125979Ssam 		while (--callcount >= 0) {
101230371Skarels #ifdef notdef
101325979Ssam 			/*
101425979Ssam 			 * Gagh, this controller is the pits...
101525979Ssam 			 */
101625979Ssam 			if (op == MTFSF || op == MTBSF) {
101725979Ssam 				do
101825979Ssam 					cycommand(dev, cyops[op], 1);
101925979Ssam 				while ((bp->b_flags&B_ERROR) == 0 &&
102025979Ssam 				 (yc->yc_status&(CYS_EOT|CYS_BOT|CYS_FM)) == 0);
102125979Ssam 			} else
102230371Skarels #endif
102325979Ssam 				cycommand(dev, cyops[op], fcount);
102430371Skarels 			dlog((LOG_INFO,
102530371Skarels 			    "cyioctl: status %x, b_flags %x, resid %d\n",
102630371Skarels 			    yc->yc_status, bp->b_flags, bp->b_resid));
102725979Ssam 			if ((bp->b_flags&B_ERROR) ||
102825979Ssam 			    (yc->yc_status&(CYS_BOT|CYS_EOT)))
102925979Ssam 				break;
103025979Ssam 		}
103125979Ssam 		bp->b_resid = callcount + 1;
103225979Ssam 		return (geterror(bp));
103325979Ssam 
103425979Ssam 	case MTIOCGET:
103525979Ssam 		cycommand(dev, CY_SENSE, 1);
103625979Ssam 		mtget = (struct mtget *)data;
103725979Ssam 		mtget->mt_dsreg = yc->yc_status;
103825979Ssam 		mtget->mt_erreg = yc->yc_control;
103925979Ssam 		mtget->mt_resid = yc->yc_resid;
104025979Ssam 		mtget->mt_type = MT_ISCY;
104125675Ssam 		break;
104225675Ssam 
104325675Ssam 	default:
104425675Ssam 		return (ENXIO);
104525675Ssam 	}
104625675Ssam 	return (0);
104725675Ssam }
104825675Ssam 
104925675Ssam /*
105025675Ssam  * Poll until the controller is ready.
105125675Ssam  */
105225675Ssam cywait(cp)
105325979Ssam 	register struct cyccb *cp;
105424000Ssam {
105525675Ssam 	register int i = 5000;
105624000Ssam 
105725979Ssam 	uncache(&cp->cbgate);
105825979Ssam 	while (i-- > 0 && cp->cbgate == GATE_CLOSED) {
105924000Ssam 		DELAY(1000);
106025979Ssam 		uncache(&cp->cbgate);
106124000Ssam 	}
106225675Ssam 	return (i <= 0);
106324000Ssam }
106424000Ssam 
106525675Ssam /*
106630371Skarels  * Load a 20 bit pointer into a Tapemaster pointer.
106725675Ssam  */
106830371Skarels cyldmba(reg, value)
106930371Skarels 	register caddr_t reg;
107025979Ssam 	caddr_t value;
107124000Ssam {
107225979Ssam 	register int v = (int)value;
107325675Ssam 
107425979Ssam 	*reg++ = v;
107525979Ssam 	*reg++ = v >> 8;
107625979Ssam 	*reg++ = 0;
107725979Ssam 	*reg = (v&0xf0000) >> 12;
107824000Ssam }
107924000Ssam 
108025675Ssam /*
108125675Ssam  * Unconditionally reset all controllers to their initial state.
108225675Ssam  */
108325675Ssam cyreset(vba)
108425675Ssam 	int vba;
108524000Ssam {
108625675Ssam 	register caddr_t addr;
108725675Ssam 	register int ctlr;
108824000Ssam 
108925675Ssam 	for (ctlr = 0; ctlr < NCY; ctlr++)
109025675Ssam 		if (cyminfo[ctlr] && cyminfo[ctlr]->um_vbanum == vba) {
109125675Ssam 			addr = cyminfo[ctlr]->um_addr;
109225675Ssam 			CY_RESET(addr);
109330371Skarels 			if (!cyinit(ctlr, addr)) {
109425675Ssam 				printf("cy%d: reset failed\n", ctlr);
109525675Ssam 				cyminfo[ctlr] = NULL;
109625675Ssam 			}
109725675Ssam 		}
109824000Ssam }
109925979Ssam 
110025979Ssam cyuncachetpb(cy)
110125979Ssam 	struct cy_softc *cy;
110225979Ssam {
110325979Ssam 	register long *lp = (long *)&cy->cy_tpb;
110425979Ssam 	register int i;
110525979Ssam 
110625979Ssam 	for (i = 0; i < howmany(sizeof (struct cytpb), sizeof (long)); i++)
110725979Ssam 		uncache(lp++);
110825979Ssam }
110925979Ssam 
111025979Ssam /*
111125979Ssam  * Dump routine.
111225979Ssam  */
111325979Ssam cydump(dev)
111425979Ssam 	dev_t dev;
111525979Ssam {
111625979Ssam 	register struct cy_softc *cy;
111725979Ssam 	register int bs, num, start;
111825979Ssam 	register caddr_t addr;
111930294Ssam 	int unit = CYUNIT(dev), error;
112025979Ssam 
112125979Ssam 	if (unit >= NCY || cyminfo[unit] == 0 ||
112225979Ssam 	    (cy = &cy_softc[unit])->cy_bs == 0 || YCUNIT(dev) >= NYC)
112325979Ssam 		return (ENXIO);
112425979Ssam 	if (cywait(&cy->cy_ccb))
112525979Ssam 		return (EFAULT);
112625979Ssam #define	phys(a)	((caddr_t)((int)(a)&~0xc0000000))
112730294Ssam 	addr = phys(cyminfo[unit]->um_addr);
112825979Ssam 	num = maxfree, start = NBPG*2;
112925979Ssam 	while (num > 0) {
113025979Ssam 		bs = num > btoc(CYMAXIO) ? btoc(CYMAXIO) : num;
113125979Ssam 		error = cydwrite(cy, start, bs, addr);
113225979Ssam 		if (error)
113325979Ssam 			return (error);
113425979Ssam 		start += bs, num -= bs;
113525979Ssam 	}
113625979Ssam 	cyweof(cy, addr);
113725979Ssam 	cyweof(cy, addr);
113825979Ssam 	uncache(&cy->cy_tpb);
113925979Ssam 	if (cy->cy_tpb.tpstatus&CYS_ERR)
114025979Ssam 		return (EIO);
114125979Ssam 	cyrewind(cy, addr);
114225979Ssam 	return (0);
114325979Ssam }
114425979Ssam 
114525979Ssam cydwrite(cy, pf, npf, addr)
114625979Ssam 	register struct cy_softc *cy;
114725979Ssam 	int pf, npf;
114825979Ssam 	caddr_t addr;
114925979Ssam {
115025979Ssam 
115125979Ssam 	cy->cy_tpb.tpcmd = CY_WCOM;
115225979Ssam 	cy->cy_tpb.tpcontrol = CYCW_LOCK|CYCW_25IPS|CYCW_16BITS;
115325979Ssam 	cy->cy_tpb.tpstatus = 0;
115425979Ssam 	cy->cy_tpb.tpsize = htoms(npf*NBPG);
115525979Ssam 	cyldmba(cy->cy_tpb.tplink, (caddr_t)0);
115625979Ssam 	cyldmba(cy->cy_tpb.tpdata, (caddr_t)(pf*NBPG));
115725979Ssam 	cyldmba(cy->cy_ccb.cbtpb, (caddr_t)&cy->cy_tpb);
115825979Ssam 	cy->cy_ccb.cbgate = GATE_CLOSED;
115925979Ssam 	CY_GO(addr);
116025979Ssam 	if (cywait(&cy->cy_ccb))
116125979Ssam 		return (EFAULT);
116225979Ssam 	uncache(&cy->cy_tpb);
116325979Ssam 	if (cy->cy_tpb.tpstatus&CYS_ERR)
116425979Ssam 		return (EIO);
116525979Ssam 	return (0);
116625979Ssam }
116725979Ssam 
116825979Ssam cyweof(cy, addr)
116925979Ssam 	register struct cy_softc *cy;
117025979Ssam 	caddr_t addr;
117125979Ssam {
117225979Ssam 
117325979Ssam 	cy->cy_tpb.tpcmd = CY_WEOF;
117425979Ssam 	cy->cy_tpb.tpcount = htoms(1);
117525979Ssam 	cy->cy_ccb.cbgate = GATE_CLOSED;
117625979Ssam 	CY_GO(addr);
117725979Ssam 	(void) cywait(&cy->cy_ccb);
117825979Ssam }
117925979Ssam 
118025979Ssam cyrewind(cy, addr)
118125979Ssam 	register struct cy_softc *cy;
118225979Ssam 	caddr_t addr;
118325979Ssam {
118425979Ssam 
118525979Ssam 	cy->cy_tpb.tpcmd = CY_REW;
118625979Ssam 	cy->cy_tpb.tpcount = htoms(1);
118725979Ssam 	cy->cy_ccb.cbgate = GATE_CLOSED;
118825979Ssam 	CY_GO(addr);
118925979Ssam 	(void) cywait(&cy->cy_ccb);
119025979Ssam }
119124000Ssam #endif
1192