xref: /csrg-svn/sys/tahoe/vba/cy.c (revision 44398)
134487Skarels /*
234487Skarels  * Copyright (c) 1988 Regents of the University of California.
334487Skarels  * All rights reserved.
434487Skarels  *
534487Skarels  * This code is derived from software contributed to Berkeley by
634487Skarels  * Computer Consoles Inc.
734487Skarels  *
834487Skarels  * Redistribution and use in source and binary forms are permitted
934866Sbostic  * provided that the above copyright notice and this paragraph are
1034866Sbostic  * duplicated in all such forms and that any documentation,
1134866Sbostic  * advertising materials, and other materials related to such
1234866Sbostic  * distribution and use acknowledge that the software was developed
1334866Sbostic  * by the University of California, Berkeley.  The name of the
1434866Sbostic  * University may not be used to endorse or promote products derived
1534866Sbostic  * from this software without specific prior written permission.
1634866Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1734866Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1834866Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1934487Skarels  *
20*44398Smarc  *	@(#)cy.c	7.4 (Berkeley) 5/5/89
2134487Skarels  */
2224000Ssam 
2325979Ssam #include "yc.h"
2425675Ssam #if NCY > 0
2524000Ssam /*
2625675Ssam  * Cipher Tapemaster driver.
2724000Ssam  */
2830371Skarels #define CYDEBUG
2930371Skarels #ifdef	CYDEBUG
3025675Ssam int	cydebug = 0;
3130371Skarels #define	dlog(params)	if (cydebug) log params
3230371Skarels #else
3330371Skarels #define dlog(params)	/* */
3430371Skarels #endif
3524000Ssam 
3625675Ssam #include "param.h"
3725675Ssam #include "systm.h"
3825675Ssam #include "vm.h"
3925675Ssam #include "buf.h"
4025675Ssam #include "file.h"
4125675Ssam #include "signal.h"
4225675Ssam #include "ioctl.h"
4325675Ssam #include "mtio.h"
4425675Ssam #include "errno.h"
4525675Ssam #include "cmap.h"
46*44398Smarc #include "time.h"
4725979Ssam #include "kernel.h"
4825979Ssam #include "syslog.h"
49*44398Smarc #include "tprintf.h"
5024000Ssam 
5129952Skarels #include "../tahoe/cpu.h"
5229952Skarels #include "../tahoe/mtpr.h"
5329952Skarels #include "../tahoe/pte.h"
5429952Skarels 
5525675Ssam #include "../tahoevba/vbavar.h"
5625979Ssam #define	CYERROR
5725675Ssam #include "../tahoevba/cyreg.h"
5824000Ssam 
5925979Ssam /*
6025979Ssam  * There is a ccybuf per tape controller.
6125979Ssam  * It is used as the token to pass to the internal routines
6225979Ssam  * to execute tape ioctls, and also acts as a lock on the slaves
6325979Ssam  * on the controller, since there is only one per controller.
6425979Ssam  * In particular, when the tape is rewinding on close we release
6525979Ssam  * the user process but any further attempts to use the tape drive
6625979Ssam  * before the rewind completes will hang waiting for ccybuf.
6725979Ssam  */
6825979Ssam struct	buf ccybuf[NCY];
6924000Ssam 
7025979Ssam int	cyprobe(), cyslave(), cyattach();
7125979Ssam struct	buf ycutab[NYC];
7225979Ssam short	yctocy[NYC];
7325675Ssam struct	vba_ctlr *cyminfo[NCY];
7425979Ssam struct	vba_device *ycdinfo[NYC];
7525857Ssam long	cystd[] = { 0 };
7625857Ssam struct	vba_driver cydriver =
7725979Ssam    { cyprobe, cyslave, cyattach, 0, cystd, "yc", ycdinfo, "cy", cyminfo };
7824000Ssam 
7925979Ssam /* bits in minor device */
8025979Ssam #define	YCUNIT(dev)	(minor(dev)&03)
8125979Ssam #define	CYUNIT(dev)	(yctocy[YCUNIT(dev)])
8225979Ssam #define	T_NOREWIND	0x04
8330371Skarels #define	T_1600BPI	0x00		/* pseudo */
8430371Skarels #define	T_3200BPI	0x08		/* unused */
8525979Ssam 
8625979Ssam #define	INF	1000000L		/* close to infinity */
8725979Ssam 
8824000Ssam /*
8925979Ssam  * Software state and shared command areas per controller.
9025979Ssam  *
9130719Skarels  * The i/o intermediate buffer must be allocated in startup()
9230719Skarels  * so its address will fit in 20-bits (YECH!!!!!!!!!!!!!!).
9324000Ssam  */
9425979Ssam struct cy_softc {
9525979Ssam 	int	cy_bs;		/* controller's buffer size */
9625979Ssam 	struct	cyscp *cy_scp;	/* system configuration block address */
9725979Ssam 	struct	cyccb cy_ccb;	/* channel control block */
9825979Ssam 	struct	cyscb cy_scb;	/* system configuration block */
9925979Ssam 	struct	cytpb cy_tpb;	/* tape parameter block */
10025979Ssam 	struct	cytpb cy_nop;	/* nop parameter block for cyintr */
10130719Skarels 	struct	vb_buf cy_rbuf;	/* vba resources */
10225979Ssam } cy_softc[NCY];
10324000Ssam 
10425979Ssam /*
10525979Ssam  * Software state per tape transport.
10625979Ssam  */
10725979Ssam struct	yc_softc {
10825979Ssam 	char	yc_openf;	/* lock against multiple opens */
10925979Ssam 	char	yc_lastiow;	/* last operation was a write */
11025979Ssam 	short	yc_tact;	/* timeout is active */
11125979Ssam 	long	yc_timo;	/* time until timeout expires */
11225979Ssam 	u_short	yc_control;	/* copy of last tpcb.tpcontrol */
11325979Ssam 	u_short	yc_status;	/* copy of last tpcb.tpstatus */
11425979Ssam 	u_short	yc_resid;	/* copy of last bc */
11525979Ssam 	u_short	yc_dens;	/* prototype control word with density info */
116*44398Smarc 	tpr_t	yc_tpr;		/* handle for tprintf */
11725979Ssam 	daddr_t	yc_blkno;	/* block number, for block device tape */
11825979Ssam 	daddr_t	yc_nxrec;	/* position of end of tape, if known */
11930371Skarels 	int	yc_blksize;	/* current tape blocksize estimate */
12030371Skarels 	int	yc_blks;	/* number of I/O operations since open */
12130371Skarels 	int	yc_softerrs;	/* number of soft I/O errors since open */
12225979Ssam } yc_softc[NYC];
12324000Ssam 
12424000Ssam /*
12525979Ssam  * States for vm->um_tab.b_active, the per controller state flag.
12625979Ssam  * This is used to sequence control in the driver.
12724000Ssam  */
12825979Ssam #define	SSEEK	1		/* seeking */
12925979Ssam #define	SIO	2		/* doing seq i/o */
13025979Ssam #define	SCOM	3		/* sending control command */
13125979Ssam #define	SREW	4		/* sending a rewind */
13225979Ssam #define	SERASE	5		/* erase inter-record gap */
13325979Ssam #define	SERASED	6		/* erased inter-record gap */
13424000Ssam 
13525979Ssam /* there's no way to figure these out dynamically? -- yech */
13625979Ssam struct	cyscp *cyscp[] =
13725979Ssam     { (struct cyscp *)0xc0000c06, (struct cyscp *)0xc0000c16 };
13825979Ssam #define	NCYSCP	(sizeof (cyscp) / sizeof (cyscp[0]))
13925979Ssam 
14025857Ssam cyprobe(reg, vm)
14125857Ssam 	caddr_t reg;
14225857Ssam 	struct vba_ctlr *vm;
14325675Ssam {
14425857Ssam 	register br, cvec;			/* must be r12, r11 */
14530371Skarels 	register struct cy_softc *cy;
14630371Skarels 	int ctlr = vm->um_ctlr;
14725675Ssam 
14830294Ssam #ifdef lint
14930294Ssam 	br = 0; cvec = br; br = cvec;
15030294Ssam 	cyintr(0);
15130294Ssam #endif
15225857Ssam 	if (badcyaddr(reg+1))
15325675Ssam 		return (0);
15430371Skarels 	if (ctlr > NCYSCP || cyscp[ctlr] == 0)		/* XXX */
15530371Skarels 		return (0);
15630371Skarels 	cy = &cy_softc[ctlr];
15730371Skarels 	cy->cy_scp = cyscp[ctlr];			/* XXX */
15825979Ssam 	/*
15925979Ssam 	 * Tapemaster controller must have interrupt handler
16025979Ssam 	 * disable interrupt, so we'll just kludge things
16125979Ssam 	 * (stupid multibus non-vectored interrupt crud).
16225979Ssam 	 */
16330371Skarels 	if (cyinit(ctlr, reg)) {
16430371Skarels 		uncache(&cy->cy_tpb.tpcount);
16530371Skarels 		cy->cy_bs = htoms(cy->cy_tpb.tpcount);
16630371Skarels 		/*
16730371Skarels 		 * Setup nop parameter block for clearing interrupts.
16830371Skarels 		 */
16930371Skarels 		cy->cy_nop.tpcmd = CY_NOP;
17030371Skarels 		cy->cy_nop.tpcontrol = 0;
17130371Skarels 		/*
17230371Skarels 		 * Allocate page tables.
17330371Skarels 		 */
17430719Skarels 		if (cybuf == 0) {
17530719Skarels 			printf("no cy buffer!!!\n");
17630719Skarels 			return (0);
17730719Skarels 		}
17830719Skarels 		cy->cy_rbuf.vb_rawbuf = cybuf + ctlr * CYMAXIO;
17931737Skarels 		if (vbainit(&cy->cy_rbuf, CYMAXIO, VB_20BIT) == 0) {
18031737Skarels 			printf("cy%d: vbainit failed\n", ctlr);
18131737Skarels 			return (0);
18231737Skarels 		}
18330371Skarels 
18430371Skarels 		br = 0x13, cvec = 0x80;			/* XXX */
18530371Skarels 		return (sizeof (struct cyccb));
18630371Skarels 	} else
18730371Skarels 		return (0);
18825675Ssam }
18925675Ssam 
19024000Ssam /*
19125857Ssam  * Check to see if a drive is attached to a controller.
19225857Ssam  * Since we can only tell that a drive is there if a tape is loaded and
19325857Ssam  * the drive is placed online, we always indicate the slave is present.
19424000Ssam  */
19525857Ssam cyslave(vi, addr)
19625857Ssam 	struct vba_device *vi;
19725857Ssam 	caddr_t addr;
19824000Ssam {
19925857Ssam 
20025857Ssam #ifdef lint
20125857Ssam 	vi = vi; addr = addr;
20225857Ssam #endif
20325857Ssam 	return (1);
20425857Ssam }
20525857Ssam 
20625857Ssam cyattach(vi)
20725857Ssam 	struct vba_device *vi;
20825857Ssam {
20925979Ssam 	register struct cy_softc *cy;
21025979Ssam 	int ctlr = vi->ui_mi->um_ctlr;
21125857Ssam 
21225979Ssam 	yctocy[vi->ui_unit] = ctlr;
21325979Ssam 	cy = &cy_softc[ctlr];
21430371Skarels 	if (vi->ui_slave == 0 && cy->cy_bs)
21530371Skarels 		printf("; %dkb buffer", cy->cy_bs/1024);
21625857Ssam }
21725857Ssam 
21825857Ssam /*
21925857Ssam  * Initialize the controller after a controller reset or
22025857Ssam  * during autoconfigure.  All of the system control blocks
22125857Ssam  * are initialized and the controller is asked to configure
22225857Ssam  * itself for later use.
22325857Ssam  */
22430371Skarels cyinit(ctlr, addr)
22525979Ssam 	int ctlr;
22630371Skarels 	register caddr_t addr;
22725857Ssam {
22825979Ssam 	register struct cy_softc *cy = &cy_softc[ctlr];
22925675Ssam 	register int *pte;
23024000Ssam 
23124000Ssam 	/*
23225675Ssam 	 * Initialize the system configuration pointer.
23324000Ssam 	 */
23425675Ssam 	/* make kernel writable */
23530719Skarels 	pte = (int *)&Sysmap[btop((int)cy->cy_scp &~ KERNBASE)];
23625675Ssam 	*pte &= ~PG_PROT; *pte |= PG_KW;
23725979Ssam 	mtpr(TBIS, cy->cy_scp);
23825675Ssam 	/* load the correct values in the scp */
23925979Ssam 	cy->cy_scp->csp_buswidth = CSP_16BITS;
24025979Ssam 	cyldmba(cy->cy_scp->csp_scb, (caddr_t)&cy->cy_scb);
24125675Ssam 	/* put it back to read-only */
24225675Ssam 	*pte &= ~PG_PROT; *pte |= PG_KR;
24325979Ssam 	mtpr(TBIS, cy->cy_scp);
24425675Ssam 
24524000Ssam 	/*
24625675Ssam 	 * Init system configuration block.
24724000Ssam 	 */
24830371Skarels 	cy->cy_scb.csb_fixed = CSB_FIXED;
24925675Ssam 	/* set pointer to the channel control block */
25025979Ssam 	cyldmba(cy->cy_scb.csb_ccb, (caddr_t)&cy->cy_ccb);
25125675Ssam 
25224000Ssam 	/*
25325675Ssam 	 * Initialize the chanel control block.
25424000Ssam 	 */
25525979Ssam 	cy->cy_ccb.cbcw = CBCW_CLRINT;
25625979Ssam 	cy->cy_ccb.cbgate = GATE_OPEN;
25725675Ssam 	/* set pointer to the tape parameter block */
25825979Ssam 	cyldmba(cy->cy_ccb.cbtpb, (caddr_t)&cy->cy_tpb);
25925675Ssam 
26024000Ssam 	/*
26125979Ssam 	 * Issue a nop cmd and get the internal buffer size for buffered i/o.
26224000Ssam 	 */
26325979Ssam 	cy->cy_tpb.tpcmd = CY_NOP;
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: timeout or err during init, status=%b\n", ctlr,
27025979Ssam 		    cy->cy_tpb.tpstatus, CYS_BITS);
27125675Ssam 		return (0);
27225675Ssam 	}
27325979Ssam 	cy->cy_tpb.tpcmd = CY_CONFIG;
27425979Ssam 	cy->cy_tpb.tpcontrol = CYCW_16BITS;
27525979Ssam 	cy->cy_ccb.cbgate = GATE_CLOSED;
27625979Ssam 	CY_GO(addr);
27725979Ssam 	if (cywait(&cy->cy_ccb) || (cy->cy_tpb.tpstatus&CYS_ERR)) {
27825979Ssam 		uncache(&cy->cy_tpb.tpstatus);
27925979Ssam 		printf("cy%d: configuration failure, status=%b\n", ctlr,
28025979Ssam 		    cy->cy_tpb.tpstatus, CYS_BITS);
28125675Ssam 		return (0);
28225675Ssam 	}
28325675Ssam 	return (1);
28424000Ssam }
28524000Ssam 
28625979Ssam int	cytimer();
28725979Ssam /*
28825979Ssam  * Open the device.  Tapes are unique open
28925979Ssam  * devices, so we refuse if it is already open.
29025979Ssam  * We also check that a tape is available, and
29125979Ssam  * don't block waiting here; if you want to wait
29225979Ssam  * for a tape you should timeout in user code.
29325979Ssam  */
29425675Ssam cyopen(dev, flag)
29525979Ssam 	dev_t dev;
29625675Ssam 	register int flag;
29725675Ssam {
29825979Ssam 	register int ycunit;
29925979Ssam 	register struct vba_device *vi;
30025979Ssam 	register struct yc_softc *yc;
30125675Ssam 
30225979Ssam 	ycunit = YCUNIT(dev);
30325979Ssam 	if (ycunit >= NYC || (vi = ycdinfo[ycunit]) == 0 || vi->ui_alive == 0)
30425675Ssam 		return (ENXIO);
30525979Ssam 	if ((yc = &yc_softc[ycunit])->yc_openf)
30625979Ssam 		return (EBUSY);
30730371Skarels 	yc->yc_openf = 1;
30825979Ssam #define	PACKUNIT(vi) \
30925979Ssam     (((vi->ui_slave&1)<<11)|((vi->ui_slave&2)<<9)|((vi->ui_slave&4)>>2))
31025979Ssam 	/* no way to select density */
31125979Ssam 	yc->yc_dens = PACKUNIT(vi)|CYCW_IE|CYCW_16BITS;
31230371Skarels 	if (yc->yc_tact == 0) {
31330371Skarels 		yc->yc_timo = INF;
31430371Skarels 		yc->yc_tact = 1;
31530371Skarels 		timeout(cytimer, (caddr_t)dev, 5*hz);
31630371Skarels 	}
31725979Ssam 	cycommand(dev, CY_SENSE, 1);
31825979Ssam 	if ((yc->yc_status&CYS_OL) == 0) {	/* not on-line */
31934285Skarels 		uprintf("cy%d: not online\n", ycunit);
32030439Skarels 		yc->yc_openf = 0;
32130872Skarels 		return (EIO);
32225675Ssam 	}
32325979Ssam 	if ((flag&FWRITE) && (yc->yc_status&CYS_WP)) {
32434285Skarels 		uprintf("cy%d: no write ring\n", ycunit);
32530439Skarels 		yc->yc_openf = 0;
32630872Skarels 		return (EIO);
32725675Ssam 	}
32825979Ssam 	yc->yc_blkno = (daddr_t)0;
32925979Ssam 	yc->yc_nxrec = INF;
33025979Ssam 	yc->yc_lastiow = 0;
33130869Skarels 	yc->yc_blksize = CYMAXIO;		/* guess > 0 */
33230371Skarels 	yc->yc_blks = 0;
33330371Skarels 	yc->yc_softerrs = 0;
334*44398Smarc 	yc->yc_tpr = tprintf_open();
33525675Ssam 	return (0);
33625675Ssam }
33725675Ssam 
33825979Ssam /*
33925979Ssam  * Close tape device.
34025979Ssam  *
34125979Ssam  * If tape was open for writing or last operation was a write,
34225979Ssam  * then write two EOF's and backspace over the last one.
34325979Ssam  * Unless this is a non-rewinding special file, rewind the tape.
34425979Ssam  * Make the tape available to others.
34525979Ssam  */
34625675Ssam cyclose(dev, flag)
34725979Ssam 	dev_t dev;
34830371Skarels 	int flag;
34925675Ssam {
35030371Skarels 	struct yc_softc *yc = &yc_softc[YCUNIT(dev)];
35125675Ssam 
35225979Ssam 	if (flag == FWRITE || (flag&FWRITE) && yc->yc_lastiow) {
35334285Skarels 		cycommand(dev, CY_WEOF, 1);	/* can't use count with WEOF */
35434285Skarels 		cycommand(dev, CY_WEOF, 1);
35525979Ssam 		cycommand(dev, CY_SREV, 1);
35625675Ssam 	}
35725979Ssam 	if ((minor(dev)&T_NOREWIND) == 0)
35825979Ssam 		/*
35925979Ssam 		 * 0 count means don't hang waiting for rewind complete
36025979Ssam 		 * rather ccybuf stays busy until the operation completes
36125979Ssam 		 * preventing further opens from completing by preventing
36225979Ssam 		 * a CY_SENSE from completing.
36325979Ssam 		 */
36425979Ssam 		cycommand(dev, CY_REW, 0);
36530371Skarels 	if (yc->yc_blks > 10 && yc->yc_softerrs > yc->yc_blks / 10)
36630371Skarels 		log(LOG_INFO, "yc%d: %d soft errors in %d blocks\n",
36730371Skarels 		    YCUNIT(dev), yc->yc_softerrs, yc->yc_blks);
36830371Skarels 	dlog((LOG_INFO, "%d soft errors in %d blocks\n",
36930371Skarels 	    yc->yc_softerrs, yc->yc_blks));
370*44398Smarc 	tprintf_close(yc->yc_tpr);
37125979Ssam 	yc->yc_openf = 0;
37230719Skarels 	return (0);
37325675Ssam }
37425675Ssam 
37524000Ssam /*
37625979Ssam  * Execute a command on the tape drive a specified number of times.
37724000Ssam  */
37825979Ssam cycommand(dev, com, count)
37925979Ssam 	dev_t dev;
38025979Ssam 	int com, count;
38124000Ssam {
38225979Ssam 	register struct buf *bp;
38325675Ssam 	int s;
38425675Ssam 
38525979Ssam 	bp = &ccybuf[CYUNIT(dev)];
38625675Ssam 	s = spl3();
38730371Skarels 	dlog((LOG_INFO, "cycommand(%o, %x, %d), b_flags %x\n",
38830371Skarels 	    dev, com, count, bp->b_flags));
38925979Ssam 	while (bp->b_flags&B_BUSY) {
39025979Ssam 		/*
39125979Ssam 		 * This special check is because B_BUSY never
39225979Ssam 		 * gets cleared in the non-waiting rewind case.
39325979Ssam 		 */
39425979Ssam 		if (bp->b_repcnt == 0 && (bp->b_flags&B_DONE))
39525979Ssam 			break;
39625979Ssam 		bp->b_flags |= B_WANTED;
39725979Ssam 		sleep((caddr_t)bp, PRIBIO);
39825675Ssam 	}
39925979Ssam 	bp->b_flags = B_BUSY|B_READ;
40025675Ssam 	splx(s);
40125979Ssam 	bp->b_dev = dev;
40225979Ssam 	bp->b_repcnt = count;
40325979Ssam 	bp->b_command = com;
40425979Ssam 	bp->b_blkno = 0;
40525979Ssam 	cystrategy(bp);
40625979Ssam 	/*
40725979Ssam 	 * In case of rewind from close; don't wait.
40825979Ssam 	 * This is the only case where count can be 0.
40925979Ssam 	 */
41025979Ssam 	if (count == 0)
41125979Ssam 		return;
41230371Skarels 	biowait(bp);
41325979Ssam 	if (bp->b_flags&B_WANTED)
41425979Ssam 		wakeup((caddr_t)bp);
41525979Ssam 	bp->b_flags &= B_ERROR;
41624000Ssam }
41724000Ssam 
41825675Ssam cystrategy(bp)
41925675Ssam 	register struct buf *bp;
42025675Ssam {
42125979Ssam 	int ycunit = YCUNIT(bp->b_dev);
42225979Ssam 	register struct vba_ctlr *vm;
42325979Ssam 	register struct buf *dp;
42425675Ssam 	int s;
42525675Ssam 
42625979Ssam 	/*
42725979Ssam 	 * Put transfer at end of unit queue.
42825979Ssam 	 */
42930371Skarels 	dlog((LOG_INFO, "cystrategy(%o, %x)\n", bp->b_dev, bp->b_command));
43025979Ssam 	dp = &ycutab[ycunit];
43125675Ssam 	bp->av_forw = NULL;
43225979Ssam 	vm = ycdinfo[ycunit]->ui_mi;
43325979Ssam 	/* BEGIN GROT */
43434507Skarels 	if (bp->b_flags & B_RAW) {
43530869Skarels 		if (bp->b_bcount >= CYMAXIO) {
43625979Ssam 			uprintf("cy%d: i/o size too large\n", vm->um_ctlr);
43730869Skarels 			bp->b_error = EINVAL;
43825979Ssam 			bp->b_resid = bp->b_bcount;
43925979Ssam 			bp->b_flags |= B_ERROR;
44030371Skarels 			biodone(bp);
44125675Ssam 			return;
44225675Ssam 		}
44324000Ssam 	}
44425979Ssam 	/* END GROT */
44525675Ssam 	s = spl3();
44625979Ssam 	if (dp->b_actf == NULL) {
44725979Ssam 		dp->b_actf = bp;
44825979Ssam 		/*
44925979Ssam 		 * Transport not already active...
45025979Ssam 		 * put at end of controller queue.
45125979Ssam 		 */
45225979Ssam 		 dp->b_forw = NULL;
45325979Ssam 		 if (vm->um_tab.b_actf == NULL)
45425979Ssam 			vm->um_tab.b_actf = dp;
45525979Ssam 		else
45625979Ssam 			vm->um_tab.b_actl->b_forw = dp;
45725979Ssam 	} else
45825979Ssam 		dp->b_actl->av_forw = bp;
45925979Ssam 	dp->b_actl = bp;
46025979Ssam 	/*
46125979Ssam 	 * If the controller is not busy, get it going.
46225979Ssam 	 */
46325979Ssam 	if (vm->um_tab.b_active == 0)
46425979Ssam 		cystart(vm);
46524000Ssam 	splx(s);
46624000Ssam }
46724000Ssam 
46824000Ssam /*
46925979Ssam  * Start activity on a cy controller.
47024000Ssam  */
47125979Ssam cystart(vm)
47225979Ssam 	register struct vba_ctlr *vm;
47324000Ssam {
47425979Ssam 	register struct buf *bp, *dp;
47525979Ssam 	register struct yc_softc *yc;
47625979Ssam 	register struct cy_softc *cy;
47725979Ssam 	int ycunit;
47825979Ssam 	daddr_t blkno;
47924000Ssam 
48030371Skarels 	dlog((LOG_INFO, "cystart()\n"));
48125979Ssam 	/*
48225979Ssam 	 * Look for an idle transport on the controller.
48325979Ssam 	 */
48425979Ssam loop:
48525979Ssam 	if ((dp = vm->um_tab.b_actf) == NULL)
48625675Ssam 		return;
48725979Ssam 	if ((bp = dp->b_actf) == NULL) {
48825979Ssam 		vm->um_tab.b_actf = dp->b_forw;
48925979Ssam 		goto loop;
49025675Ssam 	}
49125979Ssam 	ycunit = YCUNIT(bp->b_dev);
49225979Ssam 	yc = &yc_softc[ycunit];
49325979Ssam 	cy = &cy_softc[CYUNIT(bp->b_dev)];
49425979Ssam 	/*
49525979Ssam 	 * Default is that last command was NOT a write command;
49625979Ssam 	 * if we do a write command we will notice this in cyintr().
49725979Ssam 	 */
49825979Ssam 	yc->yc_lastiow = 0;
49925979Ssam 	if (yc->yc_openf < 0 ||
50025979Ssam 	    (bp->b_command != CY_SENSE && (cy->cy_tpb.tpstatus&CYS_OL) == 0)) {
50125979Ssam 		/*
50225979Ssam 		 * Have had a hard error on a non-raw tape
50325979Ssam 		 * or the tape unit is now unavailable (e.g.
50425979Ssam 		 * taken off line).
50525979Ssam 		 */
50630371Skarels 		dlog((LOG_INFO, "openf %d command %x status %b\n",
50730371Skarels 		   yc->yc_openf, bp->b_command, cy->cy_tpb.tpstatus, CYS_BITS));
50825979Ssam 		bp->b_flags |= B_ERROR;
50925979Ssam 		goto next;
51025675Ssam 	}
51125979Ssam 	if (bp == &ccybuf[CYUNIT(bp->b_dev)]) {
51225979Ssam 		/*
51325979Ssam 		 * Execute control operation with the specified count.
51425979Ssam 		 *
51525979Ssam 		 * Set next state; give 5 minutes to complete
51625979Ssam 		 * rewind or file mark search, or 10 seconds per
51725979Ssam 		 * iteration (minimum 60 seconds and max 5 minutes)
51825979Ssam 		 * to complete other ops.
51925979Ssam 		 */
52025979Ssam 		if (bp->b_command == CY_REW) {
52125979Ssam 			vm->um_tab.b_active = SREW;
52225979Ssam 			yc->yc_timo = 5*60;
52330869Skarels 		} else if (bp->b_command == CY_FSF ||
52430869Skarels 		    bp->b_command == CY_BSF) {
52530869Skarels 			vm->um_tab.b_active = SCOM;
52630869Skarels 			yc->yc_timo = 5*60;
52725979Ssam 		} else {
52825979Ssam 			vm->um_tab.b_active = SCOM;
52925979Ssam 			yc->yc_timo = imin(imax(10*(int)bp->b_repcnt,60),5*60);
53025979Ssam 		}
53125979Ssam 		cy->cy_tpb.tprec = htoms(bp->b_repcnt);
53230719Skarels 		dlog((LOG_INFO, "bpcmd "));
53325979Ssam 		goto dobpcmd;
53424000Ssam 	}
53525979Ssam 	/*
53634507Skarels 	 * For raw I/O, save the current block
53734507Skarels 	 * number in case we have to retry.
53825979Ssam 	 */
53934507Skarels 	if (bp->b_flags & B_RAW) {
54034507Skarels 		if (vm->um_tab.b_errcnt == 0) {
54134507Skarels 			yc->yc_blkno = bp->b_blkno;
54234507Skarels 			yc->yc_nxrec = yc->yc_blkno + 1;
54334507Skarels 		}
54434507Skarels 	} else {
54525979Ssam 		/*
54634507Skarels 		 * Handle boundary cases for operation
54734507Skarels 		 * on non-raw tapes.
54825979Ssam 		 */
54934507Skarels 		if (bp->b_blkno > yc->yc_nxrec) {
55034507Skarels 			/*
55134507Skarels 			 * Can't read past known end-of-file.
55234507Skarels 			 */
55334507Skarels 			bp->b_flags |= B_ERROR;
55434507Skarels 			bp->b_error = ENXIO;
55534507Skarels 			goto next;
55634507Skarels 		}
55734507Skarels 		if (bp->b_blkno == yc->yc_nxrec && bp->b_flags&B_READ) {
55834507Skarels 			/*
55934507Skarels 			 * Reading at end of file returns 0 bytes.
56034507Skarels 			 */
56134507Skarels 			bp->b_resid = bp->b_bcount;
56234507Skarels 			clrbuf(bp);
56334507Skarels 			goto next;
56434507Skarels 		}
56534507Skarels 		if ((bp->b_flags&B_READ) == 0)
56634507Skarels 			/*
56734507Skarels 			 * Writing sets EOF.
56834507Skarels 			 */
56934507Skarels 			yc->yc_nxrec = bp->b_blkno + 1;
57024000Ssam 	}
57130719Skarels 	if ((blkno = yc->yc_blkno) == bp->b_blkno) {
57225979Ssam 		caddr_t addr;
57325979Ssam 		int cmd;
57425675Ssam 
57525979Ssam 		/*
57625979Ssam 		 * Choose the appropriate i/o command based on the
57730371Skarels 		 * transfer size, the estimated block size,
57830371Skarels 		 * and the controller's internal buffer size.
57930869Skarels 		 * If the request length is longer than the tape
58030869Skarels 		 * block length, a buffered read will fail,
58130869Skarels 		 * thus, we request at most the size that we expect.
58230869Skarels 		 * We then check for larger records when the read completes.
58325979Ssam 		 * If we're retrying a read on a raw device because
58425979Ssam 		 * the original try was a buffer request which failed
58525979Ssam 		 * due to a record length error, then we force the use
58625979Ssam 		 * of the raw controller read (YECH!!!!).
58725979Ssam 		 */
58825979Ssam 		if (bp->b_flags&B_READ) {
58930869Skarels 			if (yc->yc_blksize <= cy->cy_bs &&
59030869Skarels 			    vm->um_tab.b_errcnt == 0)
59130869Skarels 				cmd = CY_BRCOM;
59230869Skarels 			else
59325979Ssam 				cmd = CY_RCOM;
59425979Ssam 		} else {
59525979Ssam 			/*
59625979Ssam 			 * On write error retries erase the
59725979Ssam 			 * inter-record gap before rewriting.
59825979Ssam 			 */
59925979Ssam 			if (vm->um_tab.b_errcnt &&
60025979Ssam 			    vm->um_tab.b_active != SERASED) {
60125979Ssam 				vm->um_tab.b_active = SERASE;
60225979Ssam 				bp->b_command = CY_ERASE;
60325979Ssam 				yc->yc_timo = 60;
60425979Ssam 				goto dobpcmd;
60525675Ssam 			}
60625979Ssam 			cmd = (bp->b_bcount > cy->cy_bs) ? CY_WCOM : CY_BWCOM;
60725675Ssam 		}
60825979Ssam 		vm->um_tab.b_active = SIO;
60930719Skarels 		addr = (caddr_t)vbasetup(bp, &cy->cy_rbuf, 1);
61025979Ssam 		cy->cy_tpb.tpcmd = cmd;
61125979Ssam 		cy->cy_tpb.tpcontrol = yc->yc_dens;
61225979Ssam 		if (cmd == CY_RCOM || cmd == CY_WCOM)
61325979Ssam 			cy->cy_tpb.tpcontrol |= CYCW_LOCK;
61425979Ssam 		cy->cy_tpb.tpstatus = 0;
61525979Ssam 		cy->cy_tpb.tpcount = 0;
61625979Ssam 		cyldmba(cy->cy_tpb.tpdata, (caddr_t)addr);
61725979Ssam 		cy->cy_tpb.tprec = 0;
61830869Skarels 		if (cmd == CY_BRCOM)
61934487Skarels 			cy->cy_tpb.tpsize = htoms(imin(yc->yc_blksize,
62034487Skarels 			    (int)bp->b_bcount));
62130371Skarels 		else
62230371Skarels 			cy->cy_tpb.tpsize = htoms(bp->b_bcount);
62325979Ssam 		cyldmba(cy->cy_tpb.tplink, (caddr_t)0);
62425979Ssam 		do
62525979Ssam 			uncache(&cy->cy_ccb.cbgate);
62625979Ssam 		while (cy->cy_ccb.cbgate == GATE_CLOSED);
62725979Ssam 		cyldmba(cy->cy_ccb.cbtpb, (caddr_t)&cy->cy_tpb);
62825979Ssam 		cy->cy_ccb.cbcw = CBCW_IE;
62925979Ssam 		cy->cy_ccb.cbgate = GATE_CLOSED;
63030371Skarels 		dlog((LOG_INFO, "CY_GO(%x) cmd %x control %x size %d\n",
63125979Ssam 		    vm->um_addr, cy->cy_tpb.tpcmd, cy->cy_tpb.tpcontrol,
63230371Skarels 		    htoms(cy->cy_tpb.tpsize)));
63325979Ssam 		CY_GO(vm->um_addr);
63425979Ssam 		return;
63524000Ssam 	}
63625979Ssam 	/*
63725979Ssam 	 * Tape positioned incorrectly; set to seek forwards
63825979Ssam 	 * or backwards to the correct spot.  This happens
63925979Ssam 	 * for raw tapes only on error retries.
64025979Ssam 	 */
64125979Ssam 	vm->um_tab.b_active = SSEEK;
64230719Skarels 	if (blkno < bp->b_blkno) {
64325979Ssam 		bp->b_command = CY_SFORW;
64430719Skarels 		cy->cy_tpb.tprec = htoms(bp->b_blkno - blkno);
64525979Ssam 	} else {
64625979Ssam 		bp->b_command = CY_SREV;
64730719Skarels 		cy->cy_tpb.tprec = htoms(blkno - bp->b_blkno);
64824000Ssam 	}
64934487Skarels 	yc->yc_timo = imin(imax((int)(10 * htoms(cy->cy_tpb.tprec)), 60), 5*60);
65025979Ssam dobpcmd:
65125979Ssam 	/*
65225979Ssam 	 * Do the command in bp.  Reverse direction commands
65325979Ssam 	 * are indicated by having CYCW_REV or'd into their
65425979Ssam 	 * value.  For these we must set the appropriate bit
65525979Ssam 	 * in the control field.
65625979Ssam 	 */
65725979Ssam 	if (bp->b_command&CYCW_REV) {
65825979Ssam 		cy->cy_tpb.tpcmd = bp->b_command &~ CYCW_REV;
65925979Ssam 		cy->cy_tpb.tpcontrol = yc->yc_dens | CYCW_REV;
66030719Skarels dlog((LOG_INFO, "cmd %x control %x\n", cy->cy_tpb.tpcmd, cy->cy_tpb.tpcontrol));
66125979Ssam 	} else {
66225979Ssam 		cy->cy_tpb.tpcmd = bp->b_command;
66325979Ssam 		cy->cy_tpb.tpcontrol = yc->yc_dens;
66430719Skarels dlog((LOG_INFO, "cmd %x control %x\n", cy->cy_tpb.tpcmd, cy->cy_tpb.tpcontrol));
66524000Ssam 	}
66625979Ssam 	cy->cy_tpb.tpstatus = 0;
66725979Ssam 	cy->cy_tpb.tpcount = 0;
66825979Ssam 	cyldmba(cy->cy_tpb.tplink, (caddr_t)0);
66925979Ssam 	do
67025979Ssam 		uncache(&cy->cy_ccb.cbgate);
67125979Ssam 	while (cy->cy_ccb.cbgate == GATE_CLOSED);
67225979Ssam 	cyldmba(cy->cy_ccb.cbtpb, (caddr_t)&cy->cy_tpb);
67325979Ssam 	cy->cy_ccb.cbcw = CBCW_IE;
67425979Ssam 	cy->cy_ccb.cbgate = GATE_CLOSED;
67530371Skarels 	dlog((LOG_INFO, "CY_GO(%x) cmd %x control %x rec %d\n",
67625979Ssam 	    vm->um_addr, cy->cy_tpb.tpcmd, cy->cy_tpb.tpcontrol,
67730371Skarels 	    htoms(cy->cy_tpb.tprec)));
67825979Ssam 	CY_GO(vm->um_addr);
67925979Ssam 	return;
68025979Ssam next:
68125979Ssam 	/*
68225979Ssam 	 * Done with this operation due to error or the
68330719Skarels 	 * fact that it doesn't do anything.
68430719Skarels 	 * Dequeue the transfer and continue
68525979Ssam 	 * processing this slave.
68625979Ssam 	 */
68725979Ssam 	vm->um_tab.b_errcnt = 0;
68825979Ssam 	dp->b_actf = bp->av_forw;
68930371Skarels 	biodone(bp);
69025979Ssam 	goto loop;
69125675Ssam }
69225675Ssam 
69325675Ssam /*
69425979Ssam  * Cy interrupt routine.
69525675Ssam  */
69630719Skarels cyintr(cyunit)
69730719Skarels 	int cyunit;
69825675Ssam {
69925979Ssam 	struct buf *dp;
70024000Ssam 	register struct buf *bp;
70130719Skarels 	register struct vba_ctlr *vm = cyminfo[cyunit];
70225979Ssam 	register struct cy_softc *cy;
70325979Ssam 	register struct yc_softc *yc;
70430719Skarels 	int err;
70525979Ssam 	register state;
70624000Ssam 
70730719Skarels 	dlog((LOG_INFO, "cyintr(%d)\n", cyunit));
70825979Ssam 	/*
70925979Ssam 	 * First, turn off the interrupt from the controller
71025979Ssam 	 * (device uses Multibus non-vectored interrupts...yech).
71125979Ssam 	 */
71225979Ssam 	cy = &cy_softc[vm->um_ctlr];
71325979Ssam 	cy->cy_ccb.cbcw = CBCW_CLRINT;
71430294Ssam 	cyldmba(cy->cy_ccb.cbtpb, (caddr_t)&cy->cy_nop);
71525979Ssam 	cy->cy_ccb.cbgate = GATE_CLOSED;
71625979Ssam 	CY_GO(vm->um_addr);
71725979Ssam 	if ((dp = vm->um_tab.b_actf) == NULL) {
71830371Skarels 		dlog((LOG_ERR, "cy%d: stray interrupt", vm->um_ctlr));
71924000Ssam 		return;
72024000Ssam 	}
72125979Ssam 	bp = dp->b_actf;
72225979Ssam 	cy = &cy_softc[cyunit];
72325979Ssam 	cyuncachetpb(cy);
72430294Ssam 	yc = &yc_softc[YCUNIT(bp->b_dev)];
72525979Ssam 	/*
72625984Ssam 	 * If last command was a rewind and tape is
72725984Ssam 	 * still moving, wait for the operation to complete.
72825979Ssam 	 */
72925979Ssam 	if (vm->um_tab.b_active == SREW) {
73025979Ssam 		vm->um_tab.b_active = SCOM;
73125979Ssam 		if ((cy->cy_tpb.tpstatus&CYS_RDY) == 0) {
73225979Ssam 			yc->yc_timo = 5*60;	/* 5 minutes */
73325979Ssam 			return;
73424000Ssam 		}
73524000Ssam 	}
73625979Ssam 	/*
73725979Ssam 	 * An operation completed...record status.
73825979Ssam 	 */
73925979Ssam 	yc->yc_timo = INF;
74025979Ssam 	yc->yc_control = cy->cy_tpb.tpcontrol;
74125979Ssam 	yc->yc_status = cy->cy_tpb.tpstatus;
74225979Ssam 	yc->yc_resid = bp->b_bcount - htoms(cy->cy_tpb.tpcount);
74330371Skarels 	dlog((LOG_INFO, "cmd %x control %b status %b resid %d\n",
74425979Ssam 	    cy->cy_tpb.tpcmd, yc->yc_control, CYCW_BITS,
74530371Skarels 	    yc->yc_status, CYS_BITS, yc->yc_resid));
74625979Ssam 	if ((bp->b_flags&B_READ) == 0)
74725979Ssam 		yc->yc_lastiow = 1;
74825979Ssam 	state = vm->um_tab.b_active;
74925979Ssam 	vm->um_tab.b_active = 0;
75025979Ssam 	/*
75125979Ssam 	 * Check for errors.
75225979Ssam 	 */
75325979Ssam 	if (cy->cy_tpb.tpstatus&CYS_ERR) {
75425979Ssam 		err = cy->cy_tpb.tpstatus&CYS_ERR;
75530371Skarels 		dlog((LOG_INFO, "error %d\n", err));
75625979Ssam 		/*
75725979Ssam 		 * If we hit the end of tape file, update our position.
75825979Ssam 		 */
75925979Ssam 		if (err == CYER_FM) {
76025979Ssam 			yc->yc_status |= CYS_FM;
76125979Ssam 			state = SCOM;		/* force completion */
76225979Ssam 			cyseteof(bp);		/* set blkno and nxrec */
76325979Ssam 			goto opdone;
76425979Ssam 		}
76525979Ssam 		/*
76625979Ssam 		 * Fix up errors which occur due to backspacing over
76725979Ssam 		 * the beginning of the tape.
76825979Ssam 		 */
76925979Ssam 		if (err == CYER_BOT && cy->cy_tpb.tpcontrol&CYCW_REV) {
77025979Ssam 			yc->yc_status |= CYS_BOT;
77125979Ssam 			goto ignoreerr;
77225979Ssam 		}
77325979Ssam 		/*
77425979Ssam 		 * If we were reading raw tape and the only error was that the
77525979Ssam 		 * record was too long, then we don't consider this an error.
77625979Ssam 		 */
77734507Skarels 		if ((bp->b_flags & (B_READ|B_RAW)) == (B_READ|B_RAW) &&
77825979Ssam 		    err == CYER_STROBE) {
77925979Ssam 			/*
78030371Skarels 			 * Retry reads with the command changed to
78130371Skarels 			 * a raw read if necessary.  Setting b_errcnt
78225979Ssam 			 * here causes cystart (above) to force a CY_RCOM.
78325979Ssam 			 */
78430869Skarels 			if (cy->cy_tpb.tpcmd == CY_BRCOM &&
78530719Skarels 			    vm->um_tab.b_errcnt++ == 0) {
78630371Skarels 				yc->yc_blkno++;
78730371Skarels 				goto opcont;
78830371Skarels 			} else
78925979Ssam 				goto ignoreerr;
79025979Ssam 		}
79125979Ssam 		/*
79225979Ssam 		 * If error is not hard, and this was an i/o operation
79325979Ssam 		 * retry up to 8 times.
79425979Ssam 		 */
79534285Skarels 		if (state == SIO && (CYMASK(err) &
79634285Skarels 		    ((bp->b_flags&B_READ) ? CYER_RSOFT : CYER_WSOFT))) {
79725979Ssam 			if (++vm->um_tab.b_errcnt < 7) {
79825979Ssam 				yc->yc_blkno++;
79925979Ssam 				goto opcont;
80025979Ssam 			}
80125979Ssam 		} else
80225979Ssam 			/*
80325979Ssam 			 * Hard or non-i/o errors on non-raw tape
80425979Ssam 			 * cause it to close.
80525979Ssam 			 */
80634507Skarels 			if ((bp->b_flags&B_RAW) == 0 &&
80734507Skarels 			    yc->yc_openf > 0)
80825979Ssam 				yc->yc_openf = -1;
80925979Ssam 		/*
81025979Ssam 		 * Couldn't recover from error.
81125979Ssam 		 */
812*44398Smarc 		tprintf(yc->yc_tpr,
81330371Skarels 		    "yc%d: hard error bn%d status=%b, %s\n", YCUNIT(bp->b_dev),
81430371Skarels 		    bp->b_blkno, yc->yc_status, CYS_BITS,
81530371Skarels 		    (err < NCYERROR) ? cyerror[err] : "");
81625979Ssam 		bp->b_flags |= B_ERROR;
81725979Ssam 		goto opdone;
81830869Skarels 	} else if (cy->cy_tpb.tpcmd == CY_BRCOM) {
81930869Skarels 		int reclen = htoms(cy->cy_tpb.tprec);
82030869Skarels 
82130869Skarels 		/*
82230869Skarels 		 * If we did a buffered read, check whether the read
82330869Skarels 		 * was long enough.  If we asked the controller for less
82430869Skarels 		 * than the user asked for because the previous record
82530869Skarels 		 * was shorter, update our notion of record size
82630869Skarels 		 * and retry.  If the record is longer than the buffer,
82730869Skarels 		 * bump the errcnt so the retry will use direct read.
82830869Skarels 		 */
82930869Skarels 		if (reclen > yc->yc_blksize && bp->b_bcount > yc->yc_blksize) {
83030869Skarels 			yc->yc_blksize = reclen;
83130869Skarels 			if (reclen > cy->cy_bs)
83230869Skarels 				vm->um_tab.b_errcnt++;
83330869Skarels 			yc->yc_blkno++;
83430869Skarels 			goto opcont;
83530869Skarels 		}
83624000Ssam 	}
83725979Ssam 	/*
83825979Ssam 	 * Advance tape control FSM.
83925979Ssam 	 */
84025979Ssam ignoreerr:
84125979Ssam 	/*
84225979Ssam 	 * If we hit a tape mark update our position.
84325979Ssam 	 */
84425979Ssam 	if (yc->yc_status&CYS_FM && bp->b_flags&B_READ) {
84525979Ssam 		cyseteof(bp);
84625979Ssam 		goto opdone;
84725675Ssam 	}
84825979Ssam 	switch (state) {
84924000Ssam 
85025979Ssam 	case SIO:
85125979Ssam 		/*
85225979Ssam 		 * Read/write increments tape block number.
85325979Ssam 		 */
85425979Ssam 		yc->yc_blkno++;
85530371Skarels 		yc->yc_blks++;
85630371Skarels 		if (vm->um_tab.b_errcnt || yc->yc_status & CYS_CR)
85730371Skarels 			yc->yc_softerrs++;
85830371Skarels 		yc->yc_blksize = htoms(cy->cy_tpb.tpcount);
85930371Skarels 		dlog((LOG_ERR, "blocksize %d", yc->yc_blksize));
86025979Ssam 		goto opdone;
86124000Ssam 
86225979Ssam 	case SCOM:
86325979Ssam 		/*
86425979Ssam 		 * For forward/backward space record update current position.
86525979Ssam 		 */
86630294Ssam 		if (bp == &ccybuf[CYUNIT(bp->b_dev)])
86730294Ssam 			switch ((int)bp->b_command) {
86824000Ssam 
86930294Ssam 			case CY_SFORW:
87030294Ssam 				yc->yc_blkno -= bp->b_repcnt;
87130294Ssam 				break;
87224000Ssam 
87330294Ssam 			case CY_SREV:
87430294Ssam 				yc->yc_blkno += bp->b_repcnt;
87530294Ssam 				break;
87630294Ssam 			}
87725979Ssam 		goto opdone;
87825979Ssam 
87925979Ssam 	case SSEEK:
88030719Skarels 		yc->yc_blkno = bp->b_blkno;
88125979Ssam 		goto opcont;
88224000Ssam 
88325979Ssam 	case SERASE:
88425979Ssam 		/*
88525979Ssam 		 * Completed erase of the inter-record gap due to a
88625979Ssam 		 * write error; now retry the write operation.
88725979Ssam 		 */
88825979Ssam 		vm->um_tab.b_active = SERASED;
88925979Ssam 		goto opcont;
89024000Ssam 	}
89125675Ssam 
89225979Ssam opdone:
89325979Ssam 	/*
89425979Ssam 	 * Reset error count and remove from device queue.
89525979Ssam 	 */
89625979Ssam 	vm->um_tab.b_errcnt = 0;
89725979Ssam 	dp->b_actf = bp->av_forw;
89825979Ssam 	/*
89925979Ssam 	 * Save resid and release resources.
90025979Ssam 	 */
90125979Ssam 	bp->b_resid = bp->b_bcount - htoms(cy->cy_tpb.tpcount);
90230719Skarels 	if (bp != &ccybuf[cyunit])
90330719Skarels 		vbadone(bp, &cy->cy_rbuf);
90430371Skarels 	biodone(bp);
90525979Ssam 	/*
90625979Ssam 	 * Circulate slave to end of controller
90725979Ssam 	 * queue to give other slaves a chance.
90825979Ssam 	 */
90925979Ssam 	vm->um_tab.b_actf = dp->b_forw;
91025979Ssam 	if (dp->b_actf) {
91125979Ssam 		dp->b_forw = NULL;
91225979Ssam 		if (vm->um_tab.b_actf == NULL)
91325979Ssam 			vm->um_tab.b_actf = dp;
91425979Ssam 		else
91525979Ssam 			vm->um_tab.b_actl->b_forw = dp;
91624000Ssam 	}
91725979Ssam 	if (vm->um_tab.b_actf == 0)
91824000Ssam 		return;
91925979Ssam opcont:
92025979Ssam 	cystart(vm);
92124000Ssam }
92224000Ssam 
92325979Ssam cytimer(dev)
92425979Ssam 	int dev;
92524000Ssam {
92625979Ssam 	register struct yc_softc *yc = &yc_softc[YCUNIT(dev)];
92725979Ssam 	int s;
92824000Ssam 
92930371Skarels 	if (yc->yc_openf == 0 && yc->yc_timo == INF) {
93030371Skarels 		yc->yc_tact = 0;
93130371Skarels 		return;
93230371Skarels 	}
93325979Ssam 	if (yc->yc_timo != INF && (yc->yc_timo -= 5) < 0) {
93425979Ssam 		printf("yc%d: lost interrupt\n", YCUNIT(dev));
93525979Ssam 		yc->yc_timo = INF;
93625979Ssam 		s = spl3();
93725979Ssam 		cyintr(CYUNIT(dev));
93825979Ssam 		splx(s);
93924000Ssam 	}
94025979Ssam 	timeout(cytimer, (caddr_t)dev, 5*hz);
94124000Ssam }
94224000Ssam 
94325979Ssam cyseteof(bp)
94425979Ssam 	register struct buf *bp;
94524000Ssam {
94625979Ssam 	register int cyunit = CYUNIT(bp->b_dev);
94725979Ssam 	register struct cy_softc *cy = &cy_softc[cyunit];
94825979Ssam 	register struct yc_softc *yc = &yc_softc[YCUNIT(bp->b_dev)];
94924000Ssam 
95025979Ssam 	if (bp == &ccybuf[cyunit]) {
95130719Skarels 		if (yc->yc_blkno > bp->b_blkno) {
95225979Ssam 			/* reversing */
95330719Skarels 			yc->yc_nxrec = bp->b_blkno - htoms(cy->cy_tpb.tpcount);
95425979Ssam 			yc->yc_blkno = yc->yc_nxrec;
95525979Ssam 		} else {
95630719Skarels 			yc->yc_blkno = bp->b_blkno + htoms(cy->cy_tpb.tpcount);
95725979Ssam 			yc->yc_nxrec = yc->yc_blkno - 1;
95824000Ssam 		}
95925675Ssam 		return;
96025675Ssam 	}
96125979Ssam 	/* eof on read */
96230719Skarels 	yc->yc_nxrec = bp->b_blkno;
96324000Ssam }
96424000Ssam 
96525675Ssam /*ARGSUSED*/
96625675Ssam cyioctl(dev, cmd, data, flag)
96725979Ssam 	caddr_t data;
96825675Ssam 	dev_t dev;
96925675Ssam {
97025979Ssam 	int ycunit = YCUNIT(dev);
97125979Ssam 	register struct yc_softc *yc = &yc_softc[ycunit];
97225979Ssam 	register struct buf *bp = &ccybuf[CYUNIT(dev)];
97325979Ssam 	register callcount;
97425979Ssam 	int fcount, op;
97525979Ssam 	struct mtop *mtop;
97625979Ssam 	struct mtget *mtget;
97725979Ssam 	/* we depend of the values and order of the MT codes here */
97825979Ssam 	static cyops[] =
97930371Skarels 	{CY_WEOF,CY_FSF,CY_BSF,CY_SFORW,CY_SREV,CY_REW,CY_OFFL,CY_SENSE};
98025675Ssam 
98125675Ssam 	switch (cmd) {
98225675Ssam 
98325979Ssam 	case MTIOCTOP:	/* tape operation */
98425979Ssam 		mtop = (struct mtop *)data;
98525979Ssam 		switch (op = mtop->mt_op) {
98625675Ssam 
98725979Ssam 		case MTWEOF:
98830371Skarels 			callcount = mtop->mt_count;
98930371Skarels 			fcount = 1;
99030371Skarels 			break;
99130371Skarels 
99225979Ssam 		case MTFSR: case MTBSR:
99330371Skarels 			callcount = 1;
99430371Skarels 			fcount = mtop->mt_count;
99530371Skarels 			break;
99630371Skarels 
99725979Ssam 		case MTFSF: case MTBSF:
99825979Ssam 			callcount = mtop->mt_count;
99925979Ssam 			fcount = 1;
100025979Ssam 			break;
100125675Ssam 
100225979Ssam 		case MTREW: case MTOFFL: case MTNOP:
100325979Ssam 			callcount = 1;
100425979Ssam 			fcount = 1;
100525979Ssam 			break;
100625675Ssam 
100725979Ssam 		default:
100825979Ssam 			return (ENXIO);
100925979Ssam 		}
101025979Ssam 		if (callcount <= 0 || fcount <= 0)
101125979Ssam 			return (EINVAL);
101225979Ssam 		while (--callcount >= 0) {
101330371Skarels #ifdef notdef
101425979Ssam 			/*
101525979Ssam 			 * Gagh, this controller is the pits...
101625979Ssam 			 */
101725979Ssam 			if (op == MTFSF || op == MTBSF) {
101825979Ssam 				do
101925979Ssam 					cycommand(dev, cyops[op], 1);
102025979Ssam 				while ((bp->b_flags&B_ERROR) == 0 &&
102125979Ssam 				 (yc->yc_status&(CYS_EOT|CYS_BOT|CYS_FM)) == 0);
102225979Ssam 			} else
102330371Skarels #endif
102425979Ssam 				cycommand(dev, cyops[op], fcount);
102530371Skarels 			dlog((LOG_INFO,
102630371Skarels 			    "cyioctl: status %x, b_flags %x, resid %d\n",
102730371Skarels 			    yc->yc_status, bp->b_flags, bp->b_resid));
102825979Ssam 			if ((bp->b_flags&B_ERROR) ||
102925979Ssam 			    (yc->yc_status&(CYS_BOT|CYS_EOT)))
103025979Ssam 				break;
103125979Ssam 		}
103225979Ssam 		bp->b_resid = callcount + 1;
103337638Smckusick 		/*
103437638Smckusick 		 * Pick up the device's error number and pass it
103537638Smckusick 		 * to the user; if there is an error but the number
103637638Smckusick 		 * is 0 set a generalized code.
103737638Smckusick 		 */
103837638Smckusick 		if ((bp->b_flags & B_ERROR) == 0)
103937638Smckusick 			return (0);
104037638Smckusick 		if (bp->b_error)
104137638Smckusick 			return (bp->b_error);
104237638Smckusick 		return (EIO);
104325979Ssam 
104425979Ssam 	case MTIOCGET:
104525979Ssam 		cycommand(dev, CY_SENSE, 1);
104625979Ssam 		mtget = (struct mtget *)data;
104725979Ssam 		mtget->mt_dsreg = yc->yc_status;
104825979Ssam 		mtget->mt_erreg = yc->yc_control;
104925979Ssam 		mtget->mt_resid = yc->yc_resid;
105025979Ssam 		mtget->mt_type = MT_ISCY;
105125675Ssam 		break;
105225675Ssam 
105325675Ssam 	default:
105425675Ssam 		return (ENXIO);
105525675Ssam 	}
105625675Ssam 	return (0);
105725675Ssam }
105825675Ssam 
105925675Ssam /*
106025675Ssam  * Poll until the controller is ready.
106125675Ssam  */
106225675Ssam cywait(cp)
106325979Ssam 	register struct cyccb *cp;
106424000Ssam {
106525675Ssam 	register int i = 5000;
106624000Ssam 
106725979Ssam 	uncache(&cp->cbgate);
106825979Ssam 	while (i-- > 0 && cp->cbgate == GATE_CLOSED) {
106924000Ssam 		DELAY(1000);
107025979Ssam 		uncache(&cp->cbgate);
107124000Ssam 	}
107225675Ssam 	return (i <= 0);
107324000Ssam }
107424000Ssam 
107525675Ssam /*
107630371Skarels  * Load a 20 bit pointer into a Tapemaster pointer.
107725675Ssam  */
107830371Skarels cyldmba(reg, value)
107934487Skarels 	register u_char *reg;
108025979Ssam 	caddr_t value;
108124000Ssam {
108225979Ssam 	register int v = (int)value;
108325675Ssam 
108425979Ssam 	*reg++ = v;
108525979Ssam 	*reg++ = v >> 8;
108625979Ssam 	*reg++ = 0;
108725979Ssam 	*reg = (v&0xf0000) >> 12;
108824000Ssam }
108924000Ssam 
109025675Ssam /*
109125675Ssam  * Unconditionally reset all controllers to their initial state.
109225675Ssam  */
109325675Ssam cyreset(vba)
109425675Ssam 	int vba;
109524000Ssam {
109625675Ssam 	register caddr_t addr;
109725675Ssam 	register int ctlr;
109824000Ssam 
109925675Ssam 	for (ctlr = 0; ctlr < NCY; ctlr++)
110025675Ssam 		if (cyminfo[ctlr] && cyminfo[ctlr]->um_vbanum == vba) {
110125675Ssam 			addr = cyminfo[ctlr]->um_addr;
110225675Ssam 			CY_RESET(addr);
110330371Skarels 			if (!cyinit(ctlr, addr)) {
110425675Ssam 				printf("cy%d: reset failed\n", ctlr);
110525675Ssam 				cyminfo[ctlr] = NULL;
110625675Ssam 			}
110725675Ssam 		}
110824000Ssam }
110925979Ssam 
111025979Ssam cyuncachetpb(cy)
111125979Ssam 	struct cy_softc *cy;
111225979Ssam {
111325979Ssam 	register long *lp = (long *)&cy->cy_tpb;
111425979Ssam 	register int i;
111525979Ssam 
111625979Ssam 	for (i = 0; i < howmany(sizeof (struct cytpb), sizeof (long)); i++)
111725979Ssam 		uncache(lp++);
111825979Ssam }
111925979Ssam 
112025979Ssam /*
112125979Ssam  * Dump routine.
112225979Ssam  */
112330869Skarels #define	DUMPREC	(32*1024)
112425979Ssam cydump(dev)
112525979Ssam 	dev_t dev;
112625979Ssam {
112725979Ssam 	register struct cy_softc *cy;
112825979Ssam 	register int bs, num, start;
112925979Ssam 	register caddr_t addr;
113030294Ssam 	int unit = CYUNIT(dev), error;
113125979Ssam 
113225979Ssam 	if (unit >= NCY || cyminfo[unit] == 0 ||
113325979Ssam 	    (cy = &cy_softc[unit])->cy_bs == 0 || YCUNIT(dev) >= NYC)
113425979Ssam 		return (ENXIO);
113525979Ssam 	if (cywait(&cy->cy_ccb))
113625979Ssam 		return (EFAULT);
113725979Ssam #define	phys(a)	((caddr_t)((int)(a)&~0xc0000000))
113830294Ssam 	addr = phys(cyminfo[unit]->um_addr);
113925979Ssam 	num = maxfree, start = NBPG*2;
114025979Ssam 	while (num > 0) {
114130869Skarels 		bs = num > btoc(DUMPREC) ? btoc(DUMPREC) : num;
114225979Ssam 		error = cydwrite(cy, start, bs, addr);
114325979Ssam 		if (error)
114425979Ssam 			return (error);
114525979Ssam 		start += bs, num -= bs;
114625979Ssam 	}
114725979Ssam 	cyweof(cy, addr);
114825979Ssam 	cyweof(cy, addr);
114925979Ssam 	uncache(&cy->cy_tpb);
115025979Ssam 	if (cy->cy_tpb.tpstatus&CYS_ERR)
115125979Ssam 		return (EIO);
115225979Ssam 	cyrewind(cy, addr);
115325979Ssam 	return (0);
115425979Ssam }
115525979Ssam 
115625979Ssam cydwrite(cy, pf, npf, addr)
115725979Ssam 	register struct cy_softc *cy;
115825979Ssam 	int pf, npf;
115925979Ssam 	caddr_t addr;
116025979Ssam {
116125979Ssam 
116225979Ssam 	cy->cy_tpb.tpcmd = CY_WCOM;
116325979Ssam 	cy->cy_tpb.tpcontrol = CYCW_LOCK|CYCW_25IPS|CYCW_16BITS;
116425979Ssam 	cy->cy_tpb.tpstatus = 0;
116525979Ssam 	cy->cy_tpb.tpsize = htoms(npf*NBPG);
116625979Ssam 	cyldmba(cy->cy_tpb.tplink, (caddr_t)0);
116725979Ssam 	cyldmba(cy->cy_tpb.tpdata, (caddr_t)(pf*NBPG));
116825979Ssam 	cyldmba(cy->cy_ccb.cbtpb, (caddr_t)&cy->cy_tpb);
116925979Ssam 	cy->cy_ccb.cbgate = GATE_CLOSED;
117025979Ssam 	CY_GO(addr);
117125979Ssam 	if (cywait(&cy->cy_ccb))
117225979Ssam 		return (EFAULT);
117325979Ssam 	uncache(&cy->cy_tpb);
117425979Ssam 	if (cy->cy_tpb.tpstatus&CYS_ERR)
117525979Ssam 		return (EIO);
117625979Ssam 	return (0);
117725979Ssam }
117825979Ssam 
117925979Ssam cyweof(cy, addr)
118025979Ssam 	register struct cy_softc *cy;
118125979Ssam 	caddr_t addr;
118225979Ssam {
118325979Ssam 
118425979Ssam 	cy->cy_tpb.tpcmd = CY_WEOF;
118525979Ssam 	cy->cy_tpb.tpcount = htoms(1);
118625979Ssam 	cy->cy_ccb.cbgate = GATE_CLOSED;
118725979Ssam 	CY_GO(addr);
118825979Ssam 	(void) cywait(&cy->cy_ccb);
118925979Ssam }
119025979Ssam 
119125979Ssam cyrewind(cy, addr)
119225979Ssam 	register struct cy_softc *cy;
119325979Ssam 	caddr_t addr;
119425979Ssam {
119525979Ssam 
119625979Ssam 	cy->cy_tpb.tpcmd = CY_REW;
119725979Ssam 	cy->cy_tpb.tpcount = htoms(1);
119825979Ssam 	cy->cy_ccb.cbgate = GATE_CLOSED;
119925979Ssam 	CY_GO(addr);
120025979Ssam 	(void) cywait(&cy->cy_ccb);
120125979Ssam }
120224000Ssam #endif
1203