xref: /csrg-svn/sys/vax/if/if_vv.c (revision 12774)
1*12774Ssam /*	if_vv.c	4.20	83/05/27	*/
27024Ssam 
39799Ssam #include "vv.h"
411192Ssam 
57024Ssam /*
67024Ssam  * Proteon 10 Meg Ring Driver.
77024Ssam  * This device is called "vv" because its "real name",
87024Ssam  * V2LNI won't work if shortened to the obvious "v2".
97024Ssam  * Hence the subterfuge.
1011192Ssam  *
117024Ssam  */
1211209Ssam #include "../machine/pte.h"
139799Ssam 
147024Ssam #include "../h/param.h"
157024Ssam #include "../h/systm.h"
167024Ssam #include "../h/mbuf.h"
177024Ssam #include "../h/buf.h"
187024Ssam #include "../h/protosw.h"
197024Ssam #include "../h/socket.h"
207024Ssam #include "../h/vmmac.h"
2111192Ssam #include "../h/errno.h"
2211209Ssam #include "../h/time.h"
2311209Ssam #include "../h/kernel.h"
248465Sroot 
258465Sroot #include "../net/if.h"
2611209Ssam #include "../net/netisr.h"
278465Sroot #include "../net/route.h"
2811192Ssam 
298421Swnj #include "../netinet/in.h"
308421Swnj #include "../netinet/in_systm.h"
318421Swnj #include "../netinet/ip.h"
328421Swnj #include "../netinet/ip_var.h"
338465Sroot 
3411209Ssam #include "../vax/mtpr.h"
3511209Ssam #include "../vax/cpu.h"
3611192Ssam 
378465Sroot #include "../vaxuba/ubareg.h"
388465Sroot #include "../vaxuba/ubavar.h"
397024Ssam 
4011209Ssam #include "../vaxif/if_vv.h"
4111209Ssam #include "../vaxif/if_uba.h"
4211209Ssam 
437024Ssam /*
447024Ssam  * N.B. - if WIRECENTER is defined wrong, it can well break
457024Ssam  * the hardware!!
467024Ssam  */
477024Ssam #define	WIRECENTER
487024Ssam 
497024Ssam #ifdef WIRECENTER
507024Ssam #define	VV_CONF	VV_HEN		/* drive wire center relay */
517024Ssam #else
527024Ssam #define	VV_CONF	VV_STE		/* allow operation without wire center */
537024Ssam #endif
547024Ssam 
557024Ssam #define	VVMTU	(1024+512)
567640Ssam #define VVMRU	(1024+512+16)	/* space for trailer */
577024Ssam 
5811192Ssam int vv_dotrailer = 0,		/* 1 => do trailer protocol */
5911192Ssam     vv_tracehdr = 0,		/* 1 => trace headers (slowly!!) */
6011192Ssam     vv_tracetimeout = 1;	/* 1 => trace input error-rate limiting */
6111192Ssam     vv_logreaderrors = 0;	/* 1 => log all read errors */
627640Ssam 
6311192Ssam #define vvtracehdr	if (vv_tracehdr) vvprt_hdr
6411192Ssam #define	vvtrprintf	if (vv_tracetimeout) printf
6511192Ssam 
6611192Ssam int vv_ticking = 0;		/* error flywheel is running */
6711192Ssam 
6811209Ssam /*
6911209Ssam  * Interval in HZ - 50 msec.
7011209Ssam  * N.B. all times below are in units of flywheel ticks
7111209Ssam  */
7211209Ssam #define VV_FLYWHEEL		3
7311192Ssam #define	VV_ERRORTHRESHOLD	100	/* errors/flywheel-interval */
7411192Ssam #define	VV_MODE1ATTEMPTS	10	/* number mode 1 retries */
7511192Ssam #define	VV_MODE1DELAY		2	/* period interface is PAUSEd - 100ms */
7611192Ssam #define VV_MODE2DELAY		4	/* base interval host relay is off - 200ms */
7711192Ssam #define	VV_MAXDELAY		6400	/* max interval host relay is off - 2 minutes */
7811192Ssam 
797024Ssam int	vvprobe(), vvattach(), vvrint(), vvxint();
807024Ssam struct	uba_device *vvinfo[NVV];
817024Ssam u_short vvstd[] = { 0 };
827024Ssam struct	uba_driver vvdriver =
837024Ssam 	{ vvprobe, 0, vvattach, 0, vvstd, "vv", vvinfo };
847024Ssam #define	VVUNIT(x)	minor(x)
857024Ssam int	vvinit(),vvoutput(),vvreset();
867024Ssam 
877024Ssam /*
887024Ssam  * Software status of each interface.
897024Ssam  *
907024Ssam  * Each interface is referenced by a network interface structure,
917024Ssam  * vs_if, which the routing code uses to locate the interface.
927024Ssam  * This structure contains the output queue for the interface, its address, ...
937024Ssam  * We also have, for each interface, a UBA interface structure, which
947024Ssam  * contains information about the UNIBUS resources held by the interface:
957024Ssam  * map registers, buffered data paths, etc.  Information is cached in this
967024Ssam  * structure for use by the if_uba.c routines in running the interface
977024Ssam  * efficiently.
987024Ssam  */
997024Ssam struct	vv_softc {
1007024Ssam 	struct	ifnet vs_if;		/* network-visible interface */
1017024Ssam 	struct	ifuba vs_ifuba;		/* UNIBUS resources */
10211192Ssam 	short	vs_oactive;		/* is output active */
10311192Ssam 	short	vs_iactive;		/* is input active */
1047024Ssam 	short	vs_olen;		/* length of last output */
1057024Ssam 	u_short	vs_lastx;		/* last destination address */
10611192Ssam 	short	vs_tries;		/* transmit current retry count */
1077024Ssam 	short	vs_init;		/* number of ring inits */
1087024Ssam 	short	vs_nottaken;		/* number of packets refused */
10911192Ssam 	/* input error rate limiting state */
11011192Ssam 	short	vs_major;		/* recovery major state */
11111192Ssam 	short	vs_minor;		/* recovery minor state */
11211192Ssam 	short	vs_retry;		/* recovery retry count */
11311192Ssam 	short	vs_delayclock;		/* recovery delay clock */
11411192Ssam 	short	vs_delayrange;		/* increasing delay interval */
11511192Ssam 	short	vs_dropped;		/* number of packes tossed in last dt */
1167024Ssam } vv_softc[NVV];
1177024Ssam 
11811192Ssam /*
11911209Ssam  * States of vs_iactive.
12011192Ssam  */
12111192Ssam #define	ACTIVE	1		/* interface should post new receives */
12211192Ssam #define	PAUSE	0		/* interface should NOT post new receives */
12311192Ssam #define	OPEN	-1		/* PAUSE and open host relay */
12411192Ssam 
12511192Ssam /*
12611209Ssam  * Recovery major states.
12711192Ssam  */
12811192Ssam #define	MODE0	0		/* everything is wonderful */
12911192Ssam #define	MODE1	1		/* hopefully whatever will go away */
13011209Ssam #define	MODE2	2		/* drastic measures - open host relay for increasing intervals */
13111192Ssam 
1327024Ssam vvprobe(reg)
1337024Ssam 	caddr_t reg;
1347024Ssam {
1357024Ssam 	register int br, cvec;
1367024Ssam 	register struct vvreg *addr = (struct vvreg *)reg;
1377024Ssam 
1387024Ssam #ifdef lint
139*12774Ssam 	br = 0; cvec = br; br = cvec; vvrint(0);
1407024Ssam #endif
1417024Ssam 	/* reset interface, enable, and wait till dust settles */
1427024Ssam 	addr->vvicsr = VV_RST;
1437024Ssam 	addr->vvocsr = VV_RST;
14412351Smo 	DELAY(10000);
1457024Ssam 	/* generate interrupt by doing 1 word DMA from 0 in uba space!! */
1467024Ssam 	addr->vvocsr = VV_IEN;		/* enable interrupt */
1477024Ssam 	addr->vvoba = 0;		/* low 16 bits */
1487024Ssam 	addr->vvoea = 0;		/* extended bits */
1497024Ssam 	addr->vvowc = -1;		/* for 1 word */
1507024Ssam 	addr->vvocsr |= VV_DEN;		/* start the DMA */
1517024Ssam 	DELAY(100000);
1527024Ssam 	addr->vvocsr = 0;
1537024Ssam 	if (cvec && cvec != 0x200)
1547024Ssam 		cvec -= 4;		/* backup so vector => recieve */
1557024Ssam 	return(1);
1567024Ssam }
1577024Ssam 
1587024Ssam /*
1597024Ssam  * Interface exists: make available by filling in network interface
1607024Ssam  * record.  System will initialize the interface when it is ready
1617024Ssam  * to accept packets.
1627024Ssam  */
1637024Ssam vvattach(ui)
1647024Ssam 	struct uba_device *ui;
1657024Ssam {
1667024Ssam 	register struct vv_softc *vs = &vv_softc[ui->ui_unit];
1677024Ssam 	register struct sockaddr_in *sin;
1687024Ssam 
1697024Ssam 	vs->vs_if.if_unit = ui->ui_unit;
1707024Ssam 	vs->vs_if.if_name = "vv";
1717024Ssam 	vs->vs_if.if_mtu = VVMTU;
1727024Ssam 	vs->vs_if.if_net = ui->ui_flags;
1737024Ssam 	vs->vs_if.if_host[0] = 0;	/* this will be reset in vvinit() */
1747024Ssam 	sin = (struct sockaddr_in *)&vs->vs_if.if_addr;
1757024Ssam 	sin->sin_family = AF_INET;
1767024Ssam 	sin->sin_addr = if_makeaddr(vs->vs_if.if_net, vs->vs_if.if_host[0]);
1777024Ssam 	sin = (struct sockaddr_in *)&vs->vs_if.if_broadaddr;
1787024Ssam 	sin->sin_family = AF_INET;
1797024Ssam 	sin->sin_addr = if_makeaddr(vs->vs_if.if_net, VV_BROADCAST);
1807024Ssam 	vs->vs_if.if_flags = IFF_BROADCAST;
1817024Ssam 	vs->vs_if.if_init = vvinit;
1827024Ssam 	vs->vs_if.if_output = vvoutput;
18311209Ssam 	vs->vs_if.if_reset = vvreset;
1847640Ssam 	vs->vs_ifuba.ifu_flags = UBA_CANTWAIT | UBA_NEEDBDP | UBA_NEED16;
18512354Smo #if defined(VAX750)
18612354Smo 	/* don't chew up 750 bdp's */
18712354Smo 	if (cpu == VAX_750 && ui->ui_unit > 0)
18812354Smo 		vs->vs_ifuba.ifu_flags &= ~UBA_NEEDBDP;
18912354Smo #endif
1907024Ssam 	if_attach(&vs->vs_if);
1917024Ssam }
1927024Ssam 
1937024Ssam /*
1947024Ssam  * Reset of interface after UNIBUS reset.
1957024Ssam  * If interface is on specified uba, reset its state.
1967024Ssam  */
1977024Ssam vvreset(unit, uban)
1987024Ssam 	int unit, uban;
1997024Ssam {
2007024Ssam 	register struct uba_device *ui;
2017024Ssam 
2027024Ssam 	if (unit >= NVV || (ui = vvinfo[unit]) == 0 || ui->ui_alive == 0 ||
2037024Ssam 	    ui->ui_ubanum != uban)
2047024Ssam 		return;
2057024Ssam 	printf(" vv%d", unit);
2067024Ssam 	vvinit(unit);
2077024Ssam }
2087024Ssam 
2097024Ssam /*
2107024Ssam  * Initialization of interface; clear recorded pending
2117024Ssam  * operations, and reinitialize UNIBUS usage.
2127024Ssam  */
2137024Ssam vvinit(unit)
2147024Ssam 	int unit;
2157024Ssam {
2167024Ssam 	register struct vv_softc *vs = &vv_softc[unit];
2177024Ssam 	register struct uba_device *ui = vvinfo[unit];
2187024Ssam 	register struct vvreg *addr;
2197024Ssam 	struct sockaddr_in *sin;
2207640Ssam 	int ubainfo, s;
22111192Ssam 	int vvtimeout();
2227024Ssam 
2237640Ssam 	addr = (struct vvreg *)ui->ui_addr;
2247024Ssam 	if (if_ubainit(&vs->vs_ifuba, ui->ui_ubanum,
2257024Ssam 	    sizeof (struct vv_header), (int)btoc(VVMTU)) == 0) {
2267024Ssam 		printf("vv%d: can't initialize\n", unit);
2277640Ssam 		vs->vs_if.if_flags &= ~IFF_UP;
2287024Ssam 		return;
2297024Ssam 	}
23011209Ssam 	if (vv_ticking++ == 0)
23111209Ssam 		timeout(vvtimeout, (caddr_t) 0, VV_FLYWHEEL);
2327024Ssam 	/*
23311209Ssam 	 * Discover our host address and post it
2347640Ssam 	 */
2357640Ssam 	vs->vs_if.if_host[0] = vvidentify(unit);
2367640Ssam 	printf("vv%d: host %d\n", unit, vs->vs_if.if_host[0]);
2377640Ssam 	sin = (struct sockaddr_in *)&vs->vs_if.if_addr;
2387640Ssam 	sin->sin_family = AF_INET;
23911209Ssam 	sin->sin_addr = if_makeaddr(vs->vs_if.if_net, vs->vs_if.if_host[0]);
2407640Ssam 
2417640Ssam 	/*
2427640Ssam 	 * Reset the interface, and join the ring
2437640Ssam 	 */
2447640Ssam 	addr->vvocsr = VV_RST | VV_CPB;		/* clear packet buffer */
2457640Ssam 	addr->vvicsr = VV_RST | VV_CONF;	/* close logical relay */
24612351Smo 	DELAY(500000);				/* let contacts settle */
2477640Ssam 	vs->vs_init = 0;
24811192Ssam 	vs->vs_dropped = 0;
2497640Ssam 	vs->vs_nottaken = 0;
2507640Ssam 
2517640Ssam 	/*
2527640Ssam 	 * Hang a receive and start any
2537640Ssam 	 * pending writes by faking a transmit complete.
2547640Ssam 	 */
2557640Ssam 	s = splimp();
2567640Ssam 	ubainfo = vs->vs_ifuba.ifu_r.ifrw_info;
2577640Ssam 	addr->vviba = (u_short) ubainfo;
2587640Ssam 	addr->vviea = (u_short) (ubainfo >> 16);
2597640Ssam 	addr->vviwc = -(sizeof (struct vv_header) + VVMTU) >> 1;
2607640Ssam 	addr->vvicsr = VV_IEN | VV_CONF | VV_DEN | VV_ENB;
26111192Ssam 	vs->vs_iactive = ACTIVE;
2627640Ssam 	vs->vs_oactive = 1;
2637640Ssam 	vs->vs_if.if_flags |= IFF_UP;
2647640Ssam 	vvxint(unit);
2657640Ssam 	splx(s);
2667640Ssam 	if_rtinit(&vs->vs_if, RTF_UP);
2677640Ssam }
2687640Ssam 
2697640Ssam /*
2707640Ssam  * vvidentify() - return our host address
2717640Ssam  */
27211209Ssam vvidentify(unit)
27311209Ssam {
2747640Ssam 	register struct vv_softc *vs = &vv_softc[unit];
2757640Ssam 	register struct uba_device *ui = vvinfo[unit];
2767640Ssam 	register struct vvreg *addr;
2777640Ssam 	struct mbuf *m;
2787640Ssam 	struct vv_header *v;
279*12774Ssam 	int ubainfo, attempts, waitcount;
2807640Ssam 
2817640Ssam 	/*
2827024Ssam 	 * Build a multicast message to identify our address
2837024Ssam 	 */
2847640Ssam 	addr = (struct vvreg *)ui->ui_addr;
2857024Ssam 	attempts = 0;		/* total attempts, including bad msg type */
28611209Ssam 	m = m_get(M_DONTWAIT, MT_HEADER);
287*12774Ssam 	if (m == NULL)
28811192Ssam 		panic("vvinit: can't get mbuf");
28911192Ssam 	m->m_next = 0;
2907024Ssam 	m->m_off = MMINOFF;
2917024Ssam 	m->m_len = sizeof(struct vv_header);
2927024Ssam 	v = mtod(m, struct vv_header *);
29311192Ssam 	v->vh_dhost = VV_BROADCAST;	/* multicast destination address */
2947024Ssam 	v->vh_shost = 0;		/* will be overwritten with ours */
2957024Ssam 	v->vh_version = RING_VERSION;
2967024Ssam 	v->vh_type = RING_WHOAMI;
2977024Ssam 	v->vh_info = 0;
29811192Ssam 	/* map xmit message into uba */
2997640Ssam 	vs->vs_olen =  if_wubaput(&vs->vs_ifuba, m);
3007640Ssam 	if (vs->vs_ifuba.ifu_flags & UBA_NEEDBDP)
3017640Ssam 		UBAPURGE(vs->vs_ifuba.ifu_uba, vs->vs_ifuba.ifu_w.ifrw_bdp);
3027024Ssam 	/*
3037024Ssam 	 * Reset interface, establish Digital Loopback Mode, and
3047024Ssam 	 * send the multicast (to myself) with Input Copy enabled.
3057024Ssam 	 */
3067024Ssam retry:
3077024Ssam 	ubainfo = vs->vs_ifuba.ifu_r.ifrw_info;
3087024Ssam 	addr->vvicsr = VV_RST;
3097024Ssam 	addr->vviba = (u_short) ubainfo;
3107024Ssam 	addr->vviea = (u_short) (ubainfo >> 16);
3117024Ssam 	addr->vviwc = -(sizeof (struct vv_header) + VVMTU) >> 1;
3127024Ssam 	addr->vvicsr = VV_STE | VV_DEN | VV_ENB | VV_LPB;
3137640Ssam 
3147640Ssam 	/* let flag timers fire so ring will initialize */
31512164Ssam 	DELAY(2000000);
3167640Ssam 
3177024Ssam 	addr->vvocsr = VV_RST | VV_CPB;	/* clear packet buffer */
3187024Ssam 	ubainfo = vs->vs_ifuba.ifu_w.ifrw_info;
3197024Ssam 	addr->vvoba = (u_short) ubainfo;
3207024Ssam 	addr->vvoea = (u_short) (ubainfo >> 16);
3217024Ssam 	addr->vvowc = -((vs->vs_olen + 1) >> 1);
3227024Ssam 	addr->vvocsr = VV_CPB | VV_DEN | VV_INR | VV_ENB;
3237024Ssam 	/*
3247024Ssam 	 * Wait for receive side to finish.
32511192Ssam 	 * Extract source address (which will our own),
3267024Ssam 	 * and post to interface structure.
3277024Ssam 	 */
3287024Ssam 	DELAY(1000);
32911192Ssam 	for (waitcount = 0; (addr->vvicsr & VV_RDY) == 0; waitcount++) {
3309876Ssam 		if (waitcount < 10) {
3317024Ssam 			DELAY(1000);
33211209Ssam 			continue;
33311192Ssam 		}
33411209Ssam 		if (attempts++ >= 10) {
33511209Ssam 			printf("vv%d: can't initialize\n", unit);
33611209Ssam 			printf("vvinit loopwait: icsr = %b\n",
33711209Ssam 				0xffff&(addr->vvicsr), VV_IBITS);
33811209Ssam 			vs->vs_if.if_flags &= ~IFF_UP;
339*12774Ssam 			return (0);
3407024Ssam 		}
34111209Ssam 		goto retry;
34211192Ssam 	}
3437024Ssam 	if (vs->vs_ifuba.ifu_flags & UBA_NEEDBDP)
3447024Ssam 		UBAPURGE(vs->vs_ifuba.ifu_uba, vs->vs_ifuba.ifu_w.ifrw_bdp);
3457024Ssam 	if (vs->vs_ifuba.ifu_xtofree)
3467024Ssam 		m_freem(vs->vs_ifuba.ifu_xtofree);
3477024Ssam 	if (vs->vs_ifuba.ifu_flags & UBA_NEEDBDP)
3487024Ssam 		UBAPURGE(vs->vs_ifuba.ifu_uba, vs->vs_ifuba.ifu_r.ifrw_bdp);
3497024Ssam 	m = if_rubaget(&vs->vs_ifuba, sizeof(struct vv_header), 0);
35011209Ssam 	if (m != NULL)
3517024Ssam 		m_freem(m);
3527024Ssam 	/*
35311209Ssam 	 * Check message type before we believe the source host address
3547024Ssam 	 */
3557024Ssam 	v = (struct vv_header *)(vs->vs_ifuba.ifu_r.ifrw_addr);
35611209Ssam 	if (v->vh_type != RING_WHOAMI)
3577640Ssam 		goto retry;
35811209Ssam 	return(v->vh_shost);
3597024Ssam }
3607024Ssam 
3617024Ssam /*
36211192Ssam  * vvtimeout() - called by timer flywheel to monitor input packet
36311192Ssam  * discard rate.  Interfaces getting too many errors are shut
36411192Ssam  * down for a while.  If the condition persists, the interface
36511192Ssam  * is marked down.
36611192Ssam  */
367*12774Ssam /*ARGSUSED*/
36811192Ssam vvtimeout(junk)
36911209Ssam 	int junk;
37011192Ssam {
37111192Ssam 	register struct vv_softc *vs;
37211192Ssam 	register int i;
37311192Ssam 	register struct vvreg *addr;
37411192Ssam 	int ubainfo;
37511192Ssam 
37611192Ssam 	timeout(vvtimeout, (caddr_t) 0, VV_FLYWHEEL);
37711209Ssam 	for (i = 0; i < NVV; i++) {
37811192Ssam 		vs = &vv_softc[i];
37911192Ssam 		addr = (struct vvreg *)vvinfo[i]->ui_addr;
380*12774Ssam 		if ((vs->vs_if.if_flags & IFF_UP) == 0)
381*12774Ssam 			continue;
38211192Ssam 		switch (vs->vs_major) {
38311192Ssam 
38411192Ssam 		/*
38511192Ssam 		 * MODE0: generally OK, just check error rate
38611192Ssam 		 */
38711192Ssam 		case MODE0:
38811192Ssam 			if (vs->vs_dropped < VV_ERRORTHRESHOLD) {
38911192Ssam 				vs->vs_dropped = 0;
39011192Ssam 				continue;
39111192Ssam 			}
39211209Ssam 			/* suspend reads for a while */
39311209Ssam 			vvtrprintf("vv%d going MODE1 in vvtimeout\n",i);
39411209Ssam 			vs->vs_major = MODE1;
39511209Ssam 			vs->vs_iactive = PAUSE;	/* no new reads */
39611209Ssam 			vs->vs_retry = VV_MODE1ATTEMPTS;
39711209Ssam 			vs->vs_delayclock = VV_MODE1DELAY;
39811209Ssam 			vs->vs_minor = 0;
39911209Ssam 			continue;
40011192Ssam 
40111192Ssam 		/*
40211192Ssam 		 * MODE1: excessive error rate observed
40311192Ssam 		 * Scheme: try simply suspending reads for a
40411192Ssam 		 * short while a small number of times
40511192Ssam 		 */
40611192Ssam 		case MODE1:
40711192Ssam 			if (vs->vs_delayclock > 0) {
40811192Ssam 				vs->vs_delayclock--;
40911192Ssam 				continue;
41011192Ssam 			}
41111192Ssam 			switch (vs->vs_minor) {
41211209Ssam 
41311192Ssam 			case 0:				/* reenable reads */
41411192Ssam 				vvtrprintf("vv%d M1m0\n",i);
41511192Ssam 				vs->vs_dropped = 0;
41611192Ssam 				vs->vs_iactive = ACTIVE;
41711192Ssam 				vs->vs_minor = 1;	/* next state */
41811192Ssam 				ubainfo = vs->vs_ifuba.ifu_r.ifrw_info;
41911192Ssam 				addr->vviba = (u_short) ubainfo;
42011192Ssam 				addr->vviea = (u_short) (ubainfo >> 16);
42111209Ssam 				addr->vviwc =
42211209Ssam 				  -(sizeof (struct vv_header) + VVMTU) >> 1;
42311192Ssam 				addr->vvicsr = VV_RST | VV_CONF;
42411192Ssam 				addr->vvicsr |= VV_IEN | VV_DEN | VV_ENB;
42511192Ssam 				continue;
42611209Ssam 
42711192Ssam 			case 1:				/* see if it worked */
42811192Ssam 				vvtrprintf("vv%d M1m1\n",i);
42911192Ssam 				if (vs->vs_dropped < VV_ERRORTHRESHOLD) {
43011192Ssam 					vs->vs_dropped = 0;
43111192Ssam 					vs->vs_major = MODE0;	/* yeah!! */
43211192Ssam 					continue;
43311192Ssam 				}
43411209Ssam 				if (vs->vs_retry -- > 0) {
43511209Ssam 					vs->vs_dropped = 0;
43611209Ssam 					vs->vs_iactive = PAUSE;
43711209Ssam 					vs->vs_delayclock = VV_MODE1DELAY;
43811209Ssam 					vs->vs_minor = 0; /* recheck */
43911209Ssam 					continue;
44011192Ssam 				}
44111209Ssam 				vs->vs_major = MODE2;
44211209Ssam 				vs->vs_minor = 0;
44311209Ssam 				vs->vs_dropped = 0;
44411209Ssam 				vs->vs_iactive = OPEN;
44511209Ssam 				vs->vs_delayrange = VV_MODE2DELAY;
44611209Ssam 				vs->vs_delayclock = VV_MODE2DELAY;
44711209Ssam 				/* fall thru ... */
44811192Ssam 			}
44911192Ssam 
45011192Ssam 		/*
45111192Ssam 		 * MODE2: simply ignoring traffic didn't relieve condition
45211192Ssam 		 * Scheme: open host relay for intervals linearly
45311192Ssam 		 * increasing up to some maximum of a several minutes.
45411192Ssam 		 * This allows broken networks to return to operation
45511192Ssam 		 * without rebooting.
45611192Ssam 		 */
45711192Ssam 		case MODE2:
45811192Ssam 			if (vs->vs_delayclock > 0) {
45911192Ssam 				vs->vs_delayclock--;
46011192Ssam 				continue;
46111192Ssam 			}
46211192Ssam 			switch (vs->vs_minor) {
46311209Ssam 
46411192Ssam 			case 0:		/* close relay and reenable reads */
46511192Ssam 				vvtrprintf("vv%d M2m0\n",i);
46611192Ssam 				vs->vs_dropped = 0;
46711192Ssam 				vs->vs_iactive = ACTIVE;
46811192Ssam 				vs->vs_minor = 1;	/* next state */
46911192Ssam 				ubainfo = vs->vs_ifuba.ifu_r.ifrw_info;
47011192Ssam 				addr->vviba = (u_short) ubainfo;
47111192Ssam 				addr->vviea = (u_short) (ubainfo >> 16);
47211209Ssam 				addr->vviwc =
47311209Ssam 				  -(sizeof (struct vv_header) + VVMTU) >> 1;
47411192Ssam 				addr->vvicsr = VV_RST | VV_CONF;
47511192Ssam 				addr->vvicsr |= VV_IEN | VV_DEN | VV_ENB;
47611192Ssam 				continue;
47711209Ssam 
47811192Ssam 			case 1:				/* see if it worked */
47911192Ssam 				vvtrprintf("vv%d M2m1\n",i);
48011192Ssam 				if (vs->vs_dropped < VV_ERRORTHRESHOLD) {
48111192Ssam 					vs->vs_dropped = 0;
48211192Ssam 					vs->vs_major = MODE0;	/* yeah!! */
48311192Ssam 					continue;
48411192Ssam 				}
48511209Ssam 				vvtrprintf("vv%d M2m1 ++ delay\n",i);
48611209Ssam 				vs->vs_dropped = 0;
48711209Ssam 				vs->vs_iactive = OPEN;
48811209Ssam 				vs->vs_minor = 0;
48911209Ssam 				if (vs->vs_delayrange < VV_MAXDELAY)
49011209Ssam 					vs->vs_delayrange +=
49111209Ssam 					  (vs->vs_delayrange/2);
49211209Ssam 				vs->vs_delayclock = vs->vs_delayrange;
49311209Ssam 				continue;
49411192Ssam 			}
49511192Ssam 
49611192Ssam 		default:
49711192Ssam 			printf("vv%d: major state screwed\n", i);
49811192Ssam 			vs->vs_if.if_flags &= ~IFF_UP;
49911192Ssam 		}
50011192Ssam 	}
50111192Ssam }
50211192Ssam 
50311192Ssam /*
5047024Ssam  * Start or restart output on interface.
50511192Ssam  * If interface is active, this is a retransmit, so just
50611192Ssam  * restuff registers and go.
5077024Ssam  * If interface is not already active, get another datagram
5087024Ssam  * to send off of the interface queue, and map it to the interface
5097024Ssam  * before starting the output.
5107024Ssam  */
5117024Ssam vvstart(dev)
5127024Ssam 	dev_t dev;
5137024Ssam {
5147024Ssam         int unit = VVUNIT(dev);
5157024Ssam 	struct uba_device *ui = vvinfo[unit];
5167024Ssam 	register struct vv_softc *vs = &vv_softc[unit];
5177024Ssam 	register struct vvreg *addr;
5187024Ssam 	struct mbuf *m;
51911192Ssam 	int ubainfo;
52011192Ssam 	int dest;
5217024Ssam 
5227024Ssam 	if (vs->vs_oactive)
5237024Ssam 		goto restart;
5247024Ssam 	/*
5257024Ssam 	 * Not already active: dequeue another request
5267024Ssam 	 * and map it to the UNIBUS.  If no more requests,
5277024Ssam 	 * just return.
5287024Ssam 	 */
5297024Ssam 	IF_DEQUEUE(&vs->vs_if.if_snd, m);
53011209Ssam 	if (m == NULL) {
5317024Ssam 		vs->vs_oactive = 0;
5327024Ssam 		return;
5337024Ssam 	}
5347024Ssam 	dest = mtod(m, struct vv_header *)->vh_dhost;
5357024Ssam 	vs->vs_olen = if_wubaput(&vs->vs_ifuba, m);
5367024Ssam 	vs->vs_lastx = dest;
5377024Ssam restart:
5387024Ssam 	/*
5397024Ssam 	 * Have request mapped to UNIBUS for transmission.
5407024Ssam 	 * Purge any stale data from this BDP, and start the otput.
5417024Ssam 	 */
54212363Ssam 	if (vs->vs_olen > VVMTU + sizeof (struct vv_header)) {
54311192Ssam 		printf("vv%d vs_olen: %d > VVMTU\n", unit, vs->vs_olen);
54411192Ssam 		panic("vvdriver vs_olen botch");
54511192Ssam 	}
5467024Ssam 	if (vs->vs_ifuba.ifu_flags & UBA_NEEDBDP)
5477024Ssam 		UBAPURGE(vs->vs_ifuba.ifu_uba, vs->vs_ifuba.ifu_w.ifrw_bdp);
5487024Ssam 	addr = (struct vvreg *)ui->ui_addr;
5497024Ssam 	ubainfo = vs->vs_ifuba.ifu_w.ifrw_info;
5507024Ssam 	addr->vvoba = (u_short) ubainfo;
5517024Ssam 	addr->vvoea = (u_short) (ubainfo >> 16);
5527024Ssam 	addr->vvowc = -((vs->vs_olen + 1) >> 1);
5537024Ssam 	addr->vvocsr = VV_IEN | VV_CPB | VV_DEN | VV_INR | VV_ENB;
5547024Ssam 	vs->vs_oactive = 1;
5557024Ssam }
5567024Ssam 
5577024Ssam /*
5587024Ssam  * VVLNI transmit interrupt
5597024Ssam  * Start another output if more data to send.
5607024Ssam  */
5617024Ssam vvxint(unit)
5627024Ssam 	int unit;
5637024Ssam {
5647024Ssam 	register struct uba_device *ui = vvinfo[unit];
5657024Ssam 	register struct vv_softc *vs = &vv_softc[unit];
5667024Ssam 	register struct vvreg *addr;
5677024Ssam 	register int oc;
5687024Ssam 
5697024Ssam 	addr = (struct vvreg *)ui->ui_addr;
5707024Ssam 	oc = 0xffff & (addr->vvocsr);
5717024Ssam 	if (vs->vs_oactive == 0) {
57211192Ssam 		printf("vv%d: stray interrupt vvocsr = %b\n", unit,
5737024Ssam 			oc, VV_OBITS);
5747024Ssam 		return;
5757024Ssam 	}
5767024Ssam 	if (oc &  (VV_OPT | VV_RFS)) {
5777640Ssam 		vs->vs_if.if_collisions++;
57811192Ssam 		if (vs->vs_tries++ < VVRETRY) {
5797024Ssam 			if (oc & VV_OPT)
5807024Ssam 				vs->vs_init++;
5817024Ssam 			if (oc & VV_RFS)
5827024Ssam 				vs->vs_nottaken++;
58311192Ssam 			vvstart(unit);		/* restart this message */
5847024Ssam 			return;
5857024Ssam 		}
5867024Ssam 		if (oc & VV_OPT)
5877024Ssam 			printf("vv%d: output timeout\n");
5887024Ssam 	}
5897024Ssam 	vs->vs_if.if_opackets++;
5907024Ssam 	vs->vs_oactive = 0;
5917024Ssam 	vs->vs_tries = 0;
5927024Ssam 	if (oc & VVXERR) {
5937024Ssam 		vs->vs_if.if_oerrors++;
59411192Ssam 		printf("vv%d: error vvocsr = %b\n", unit, 0xffff & oc,
5957024Ssam 			VV_OBITS);
5967024Ssam 	}
5977024Ssam 	if (vs->vs_ifuba.ifu_xtofree) {
5987024Ssam 		m_freem(vs->vs_ifuba.ifu_xtofree);
5997024Ssam 		vs->vs_ifuba.ifu_xtofree = 0;
6007024Ssam 	}
6017024Ssam 	if (vs->vs_if.if_snd.ifq_head == 0) {
60211192Ssam 		vs->vs_lastx = 256;		/* an invalid address */
6037024Ssam 		return;
6047024Ssam 	}
6057024Ssam 	vvstart(unit);
6067024Ssam }
6077024Ssam 
6087024Ssam /*
6097024Ssam  * V2lni interface receiver interrupt.
6107024Ssam  * If input error just drop packet.
6117024Ssam  * Otherwise purge input buffered data path and examine
6127024Ssam  * packet to determine type.  If can't determine length
6137024Ssam  * from type, then have to drop packet.  Othewise decapsulate
6147024Ssam  * packet based on type and pass to type specific higher-level
6157024Ssam  * input routine.
6167024Ssam  */
6177024Ssam vvrint(unit)
6187024Ssam 	int unit;
6197024Ssam {
6207024Ssam 	register struct vv_softc *vs = &vv_softc[unit];
6217024Ssam 	struct vvreg *addr = (struct vvreg *)vvinfo[unit]->ui_addr;
6227024Ssam 	register struct vv_header *vv;
6237024Ssam 	register struct ifqueue *inq;
6247024Ssam     	struct mbuf *m;
6257024Ssam 	int ubainfo, len, off;
6267640Ssam 	short resid;
6277024Ssam 
6287024Ssam 	vs->vs_if.if_ipackets++;
6297024Ssam 	/*
6307024Ssam 	 * Purge BDP; drop if input error indicated.
6317024Ssam 	 */
6327024Ssam 	if (vs->vs_ifuba.ifu_flags & UBA_NEEDBDP)
6337024Ssam 		UBAPURGE(vs->vs_ifuba.ifu_uba, vs->vs_ifuba.ifu_r.ifrw_bdp);
6347024Ssam 	if (addr->vvicsr & VVRERR) {
63511192Ssam 		if (vv_logreaderrors)
63611192Ssam 			printf("vv%d: error vvicsr = %b\n", unit,
63711192Ssam 				0xffff&(addr->vvicsr), VV_IBITS);
6387640Ssam 		goto dropit;
6397024Ssam 	}
6407640Ssam 
6417024Ssam 	/*
6427640Ssam 	 * Get packet length from word count residue
6437640Ssam 	 *
6447640Ssam 	 * Compute header offset if trailer protocol
6457640Ssam 	 *
6467640Ssam 	 * Pull packet off interface.  Off is nonzero if packet
6477640Ssam 	 * has trailing header; if_rubaget will then force this header
6487640Ssam 	 * information to be at the front.  The vh_info field
6497640Ssam 	 * carries the offset to the trailer data in trailer
6507640Ssam 	 * format packets.
6517024Ssam 	 */
6527640Ssam 	vv = (struct vv_header *)(vs->vs_ifuba.ifu_r.ifrw_addr);
65311192Ssam 	vvtracehdr("vi", vv);
6547640Ssam 	resid = addr->vviwc;
6557640Ssam 	if (resid)
6567640Ssam 		resid |= 0176000;		/* ugly!!!! */
6577640Ssam 	len = (((sizeof (struct vv_header) + VVMRU) >> 1) + resid) << 1;
6587640Ssam 	len -= sizeof(struct vv_header);
65911192Ssam 	if (len > VVMRU || len <= 0)
6607640Ssam 		goto dropit;
6617640Ssam #define	vvdataaddr(vv, off, type)	((type)(((caddr_t)((vv)+1)+(off))))
6627640Ssam 	if (vv_dotrailer && vv->vh_type >= RING_IPTrailer &&
6637640Ssam 	     vv->vh_type < RING_IPTrailer+RING_IPNTrailer){
6647640Ssam 		off = (vv->vh_type - RING_IPTrailer) * 512;
6657640Ssam 		if (off > VVMTU)
6667640Ssam 			goto dropit;
6677640Ssam 		vv->vh_type = *vvdataaddr(vv, off, u_short *);
6687640Ssam 		resid = *(vvdataaddr(vv, off+2, u_short *));
6697640Ssam 		if (off + resid > len)
6707640Ssam 			goto dropit;
6717640Ssam 		len = off + resid;
67211209Ssam 	} else
6737640Ssam 		off = 0;
6747640Ssam 	if (len == 0)
6757640Ssam 		goto dropit;
6767640Ssam 	m = if_rubaget(&vs->vs_ifuba, len, off);
67711209Ssam 	if (m == NULL)
6787640Ssam 		goto dropit;
6797640Ssam 	if (off) {
6807640Ssam 		m->m_off += 2 * sizeof(u_short);
6817640Ssam 		m->m_len -= 2 * sizeof(u_short);
6827640Ssam 	}
68311192Ssam 
68411192Ssam 	/*
68511192Ssam 	 * Demultiplex on packet type
68611192Ssam 	 */
6877024Ssam 	switch (vv->vh_type) {
68811192Ssam 
6897024Ssam #ifdef INET
6907024Ssam 	case RING_IP:
6917024Ssam 		schednetisr(NETISR_IP);
6927024Ssam 		inq = &ipintrq;
6937024Ssam 		break;
6947024Ssam #endif
6957024Ssam 	default:
6967024Ssam 		printf("vv%d: unknown pkt type 0x%x\n", unit, vv->vh_type);
6977640Ssam 		m_freem(m);
6987024Ssam 		goto setup;
6997024Ssam 	}
7007640Ssam 	if (IF_QFULL(inq)) {
7017640Ssam 		IF_DROP(inq);
7027640Ssam 		m_freem(m);
70311209Ssam 	} else
7047640Ssam 		IF_ENQUEUE(inq, m);
7057024Ssam setup:
7067024Ssam 	/*
70711192Ssam 	 * Check the error rate and start recovery if needed
70811192Ssam 	 * this has to go here since the timer flywheel runs at
70911192Ssam 	 * a lower ipl and never gets a chance to change the mode
7107024Ssam 	 */
71111192Ssam 	if (vs->vs_major == MODE0 && vs->vs_dropped > VV_ERRORTHRESHOLD) {
71211192Ssam 		vvtrprintf("vv%d going MODE1 in vvrint\n",unit);
71311192Ssam 		vs->vs_major = MODE1;
71411192Ssam 		vs->vs_iactive = PAUSE;		/* no new reads */
71511192Ssam 		vs->vs_retry = VV_MODE1ATTEMPTS;
71611192Ssam 		vs->vs_delayclock = VV_MODE1DELAY;
71711192Ssam 		vs->vs_minor = 0;
71811192Ssam 		vs->vs_dropped = 0;
71911192Ssam 	}
72011192Ssam 	switch (vs->vs_iactive) {
72111192Ssam 
72211209Ssam 	case ACTIVE:		/* Restart the read for next packet */
72311192Ssam 		ubainfo = vs->vs_ifuba.ifu_r.ifrw_info;
72411192Ssam 		addr->vviba = (u_short) ubainfo;
72511192Ssam 		addr->vviea = (u_short) (ubainfo >> 16);
72611192Ssam 		addr->vviwc = -(sizeof (struct vv_header) + VVMTU) >> 1;
72711192Ssam 		addr->vvicsr = VV_RST | VV_CONF;
72811192Ssam 		addr->vvicsr |= VV_IEN | VV_DEN | VV_ENB;
72911192Ssam 		return;
73011192Ssam 
73111209Ssam 	case PAUSE:		/* requested to not start any new reads */
73211192Ssam 		vs->vs_dropped = 0;
73311192Ssam 		return;
73411192Ssam 
73511209Ssam 	case OPEN:		/* request to open host relay */
73611192Ssam 		vs->vs_dropped = 0;
73711192Ssam 		addr->vvicsr = 0;
73811192Ssam 		return;
73911192Ssam 
74011192Ssam 	default:
74111192Ssam 		printf("vv%d: vs_iactive = %d\n", unit, vs->vs_iactive);
74211192Ssam 		return;
74311192Ssam 	}
74411192Ssam 	/*
74511209Ssam 	 * Drop packet on floor -- count them!!
74611192Ssam 	 */
7477640Ssam dropit:
7487640Ssam 	vs->vs_if.if_ierrors++;
74911192Ssam 	vs->vs_dropped++;
7507640Ssam 	/*
7517640Ssam 	printf("vv%d: error vvicsr = %b\n", unit,
7527640Ssam 		0xffff&(addr->vvicsr), VV_IBITS);
7537640Ssam 	*/
7547640Ssam 	goto setup;
7557024Ssam }
7567024Ssam 
7577024Ssam /*
7587024Ssam  * V2lni output routine.
7597024Ssam  * Encapsulate a packet of type family for the local net.
7607024Ssam  * Use trailer local net encapsulation if enough data in first
7617024Ssam  * packet leaves a multiple of 512 bytes of data in remainder.
7627024Ssam  */
7637024Ssam vvoutput(ifp, m0, dst)
7647024Ssam 	struct ifnet *ifp;
7657024Ssam 	struct mbuf *m0;
7667024Ssam 	struct sockaddr *dst;
7677024Ssam {
7687024Ssam 	register struct mbuf *m = m0;
7697024Ssam 	register struct vv_header *vv;
7707640Ssam 	register int off;
7717640Ssam 	int type, dest, s, error;
7727024Ssam 
7737024Ssam 	switch (dst->sa_family) {
77411192Ssam 
7757024Ssam #ifdef INET
7767024Ssam 	case AF_INET: {
7777640Ssam 		dest = ((struct sockaddr_in *)dst)->sin_addr.s_addr;
77811192Ssam 		if ((dest = in_lnaof(*((struct in_addr *)&dest))) >= 0x100) {
7797640Ssam 			error = EPERM;
7807640Ssam 			goto bad;
7817640Ssam 		}
7827640Ssam 		off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len;
7837640Ssam 		if (vv_dotrailer && off > 0 && (off & 0x1ff) == 0 &&
7847640Ssam 		    m->m_off >= MMINOFF + 2 * sizeof (u_short)) {
7857640Ssam 			type = RING_IPTrailer + (off>>9);
7867640Ssam 			m->m_off -= 2 * sizeof (u_short);
7877640Ssam 			m->m_len += 2 * sizeof (u_short);
7887640Ssam 			*mtod(m, u_short *) = RING_IP;
7897640Ssam 			*(mtod(m, u_short *) + 1) = m->m_len;
7907640Ssam 			goto gottrailertype;
7917640Ssam 		}
7927024Ssam 		type = RING_IP;
7937024Ssam 		off = 0;
7947024Ssam 		goto gottype;
7957024Ssam 		}
7967024Ssam #endif
7977024Ssam 	default:
7987024Ssam 		printf("vv%d: can't handle af%d\n", ifp->if_unit,
7997024Ssam 			dst->sa_family);
8007640Ssam 		error = EAFNOSUPPORT;
8017640Ssam 		goto bad;
8027024Ssam 	}
8037024Ssam 
8047024Ssam gottrailertype:
8057024Ssam 	/*
8067024Ssam 	 * Packet to be sent as trailer: move first packet
8077024Ssam 	 * (control information) to end of chain.
8087024Ssam 	 */
8097024Ssam 	while (m->m_next)
8107024Ssam 		m = m->m_next;
8117024Ssam 	m->m_next = m0;
8127024Ssam 	m = m0->m_next;
8137024Ssam 	m0->m_next = 0;
8147024Ssam 	m0 = m;
8157024Ssam gottype:
8167024Ssam 	/*
8177024Ssam 	 * Add local net header.  If no space in first mbuf,
8187024Ssam 	 * allocate another.
8197024Ssam 	 */
8207024Ssam 	if (m->m_off > MMAXOFF ||
8217024Ssam 	    MMINOFF + sizeof (struct vv_header) > m->m_off) {
82211209Ssam 		m = m_get(M_DONTWAIT, MT_HEADER);
82311209Ssam 		if (m == NULL) {
8247640Ssam 			error = ENOBUFS;
8257640Ssam 			goto bad;
8267024Ssam 		}
8277024Ssam 		m->m_next = m0;
8287024Ssam 		m->m_off = MMINOFF;
8297024Ssam 		m->m_len = sizeof (struct vv_header);
8307024Ssam 	} else {
8317024Ssam 		m->m_off -= sizeof (struct vv_header);
8327024Ssam 		m->m_len += sizeof (struct vv_header);
8337024Ssam 	}
8347024Ssam 	vv = mtod(m, struct vv_header *);
8357024Ssam 	vv->vh_shost = ifp->if_host[0];
8367024Ssam 	vv->vh_dhost = dest;
8377024Ssam 	vv->vh_version = RING_VERSION;
8387024Ssam 	vv->vh_type = type;
8397640Ssam 	vv->vh_info = off;
84011192Ssam 	vvtracehdr("vo", vv);
8417024Ssam 
8427024Ssam 	/*
8437024Ssam 	 * Queue message on interface, and start output if interface
8447024Ssam 	 * not yet active.
8457024Ssam 	 */
8467024Ssam 	s = splimp();
8477640Ssam 	if (IF_QFULL(&ifp->if_snd)) {
8487640Ssam 		IF_DROP(&ifp->if_snd);
8497640Ssam 		error = ENOBUFS;
8507640Ssam 		goto qfull;
8517640Ssam 	}
8527024Ssam 	IF_ENQUEUE(&ifp->if_snd, m);
8537024Ssam 	if (vv_softc[ifp->if_unit].vs_oactive == 0)
8547024Ssam 		vvstart(ifp->if_unit);
8557024Ssam 	splx(s);
8567640Ssam 	return (0);
8577640Ssam qfull:
8587640Ssam 	m0 = m;
8597640Ssam 	splx(s);
8607640Ssam bad:
8617640Ssam 	m_freem(m0);
8627640Ssam 	return(error);
8637024Ssam }
8647024Ssam 
8657024Ssam /*
8667024Ssam  * vvprt_hdr(s, v) print the local net header in "v"
8677024Ssam  * 	with title is "s"
8687024Ssam  */
8697024Ssam vvprt_hdr(s, v)
8707024Ssam 	char *s;
8717024Ssam 	register struct vv_header *v;
8727024Ssam {
8737024Ssam 	printf("%s: dsvti: 0x%x 0x%x 0x%x 0x%x 0x%x\n",
8747024Ssam 		s,
8757024Ssam 		0xff & (int)(v->vh_dhost), 0xff & (int)(v->vh_shost),
8767024Ssam 		0xff & (int)(v->vh_version), 0xff & (int)(v->vh_type),
8777024Ssam 		0xffff & (int)(v->vh_info));
8787024Ssam }
8797024Ssam 
880*12774Ssam #ifdef notdef
8817024Ssam /*
8827024Ssam  * print "l" hex bytes starting at "s"
8837024Ssam  */
8847024Ssam vvprt_hex(s, l)
8857024Ssam 	char *s;
8867024Ssam 	int l;
8877024Ssam {
8887024Ssam 	register int i;
8897024Ssam 	register int z;
8907024Ssam 
8917024Ssam 	for (i=0 ; i < l; i++) {
8927024Ssam 		z = 0xff & (int)(*(s + i));
8937024Ssam 		printf("%c%c ",
8947024Ssam 		"0123456789abcdef"[(z >> 4) & 0x0f],
8957024Ssam 		"0123456789abcdef"[z & 0x0f]
8967024Ssam 		);
8977024Ssam 	}
8987024Ssam }
899*12774Ssam #endif
900