xref: /csrg-svn/sys/vax/if/if_vv.c (revision 9799)
1*9799Ssam /*	if_vv.c	4.11	82/12/17	*/
27024Ssam 
3*9799Ssam #include "vv.h"
4*9799Ssam #if NVV > 0
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.
107024Ssam  */
11*9799Ssam #include "../machine/pte.h"
12*9799Ssam 
137024Ssam #include "../h/param.h"
147024Ssam #include "../h/systm.h"
157024Ssam #include "../h/mbuf.h"
167024Ssam #include "../h/buf.h"
177024Ssam #include "../h/protosw.h"
187024Ssam #include "../h/socket.h"
197024Ssam #include "../h/vmmac.h"
208465Sroot #include <errno.h>
218465Sroot 
228465Sroot #include "../net/if.h"
238465Sroot #include "../net/route.h"
248421Swnj #include "../netinet/in.h"
258421Swnj #include "../netinet/in_systm.h"
268421Swnj #include "../netinet/ip.h"
278421Swnj #include "../netinet/ip_var.h"
288465Sroot 
298465Sroot #include "../vax/cpu.h"
308465Sroot #include "../vax/mtpr.h"
318421Swnj #include "../vaxif/if_vv.h"
328421Swnj #include "../vaxif/if_uba.h"
338465Sroot #include "../vaxuba/ubareg.h"
348465Sroot #include "../vaxuba/ubavar.h"
357024Ssam 
367024Ssam #include "vv.h"
377024Ssam 
387024Ssam /*
397024Ssam  * N.B. - if WIRECENTER is defined wrong, it can well break
407024Ssam  * the hardware!!
417024Ssam  */
427640Ssam 
437024Ssam #define	WIRECENTER
447024Ssam 
457024Ssam #ifdef WIRECENTER
467024Ssam #define	VV_CONF	VV_HEN		/* drive wire center relay */
477024Ssam #else
487024Ssam #define	VV_CONF	VV_STE		/* allow operation without wire center */
497024Ssam #endif
507024Ssam 
517024Ssam #define	VVMTU	(1024+512)
527640Ssam #define VVMRU	(1024+512+16)	/* space for trailer */
537024Ssam 
547640Ssam int vv_dotrailer = 1,		/* so can do trailers selectively */
557640Ssam     vv_trace = 0;
567640Ssam 
577024Ssam int	vvprobe(), vvattach(), vvrint(), vvxint();
587024Ssam struct	uba_device *vvinfo[NVV];
597024Ssam u_short vvstd[] = { 0 };
607024Ssam struct	uba_driver vvdriver =
617024Ssam 	{ vvprobe, 0, vvattach, 0, vvstd, "vv", vvinfo };
627024Ssam #define	VVUNIT(x)	minor(x)
637024Ssam int	vvinit(),vvoutput(),vvreset();
647024Ssam 
657024Ssam /*
667024Ssam  * Software status of each interface.
677024Ssam  *
687024Ssam  * Each interface is referenced by a network interface structure,
697024Ssam  * vs_if, which the routing code uses to locate the interface.
707024Ssam  * This structure contains the output queue for the interface, its address, ...
717024Ssam  * We also have, for each interface, a UBA interface structure, which
727024Ssam  * contains information about the UNIBUS resources held by the interface:
737024Ssam  * map registers, buffered data paths, etc.  Information is cached in this
747024Ssam  * structure for use by the if_uba.c routines in running the interface
757024Ssam  * efficiently.
767024Ssam  */
777024Ssam struct	vv_softc {
787024Ssam 	struct	ifnet vs_if;		/* network-visible interface */
797024Ssam 	struct	ifuba vs_ifuba;		/* UNIBUS resources */
807024Ssam 	short	vs_oactive;		/* is output active? */
817024Ssam 	short	vs_olen;		/* length of last output */
827024Ssam 	u_short	vs_lastx;		/* last destination address */
837024Ssam 	short	vs_tries;		/* current retry count */
847024Ssam 	short	vs_init;		/* number of ring inits */
857024Ssam 	short	vs_flush;		/* number of flushed packets */
867024Ssam 	short	vs_nottaken;		/* number of packets refused */
877024Ssam } vv_softc[NVV];
887024Ssam 
897024Ssam vvprobe(reg)
907024Ssam 	caddr_t reg;
917024Ssam {
927024Ssam 	register int br, cvec;
937024Ssam 	register struct vvreg *addr = (struct vvreg *)reg;
947024Ssam 
957024Ssam #ifdef lint
967024Ssam 	br = 0; cvec = br; br = cvec;
977024Ssam #endif
987024Ssam 	/* reset interface, enable, and wait till dust settles */
997024Ssam 	addr->vvicsr = VV_RST;
1007024Ssam 	addr->vvocsr = VV_RST;
1017024Ssam 	DELAY(100000);
1027024Ssam 	/* generate interrupt by doing 1 word DMA from 0 in uba space!! */
1037024Ssam 	addr->vvocsr = VV_IEN;		/* enable interrupt */
1047024Ssam 	addr->vvoba = 0;		/* low 16 bits */
1057024Ssam 	addr->vvoea = 0;		/* extended bits */
1067024Ssam 	addr->vvowc = -1;		/* for 1 word */
1077024Ssam 	addr->vvocsr |= VV_DEN;		/* start the DMA */
1087024Ssam 	DELAY(100000);
1097024Ssam 	addr->vvocsr = 0;
1107024Ssam 	if (cvec && cvec != 0x200)
1117024Ssam 		cvec -= 4;		/* backup so vector => recieve */
1127024Ssam 	return(1);
1137024Ssam }
1147024Ssam 
1157024Ssam /*
1167024Ssam  * Interface exists: make available by filling in network interface
1177024Ssam  * record.  System will initialize the interface when it is ready
1187024Ssam  * to accept packets.
1197024Ssam  */
1207024Ssam vvattach(ui)
1217024Ssam 	struct uba_device *ui;
1227024Ssam {
1237024Ssam 	register struct vv_softc *vs = &vv_softc[ui->ui_unit];
1247024Ssam 	register struct sockaddr_in *sin;
1257024Ssam 
1267024Ssam 	vs->vs_if.if_unit = ui->ui_unit;
1277024Ssam 	vs->vs_if.if_name = "vv";
1287024Ssam 	vs->vs_if.if_mtu = VVMTU;
1297024Ssam 	vs->vs_if.if_net = ui->ui_flags;
1307024Ssam 	vs->vs_if.if_host[0] = 0;	/* this will be reset in vvinit() */
1317024Ssam 
1327024Ssam 	sin = (struct sockaddr_in *)&vs->vs_if.if_addr;
1337024Ssam 	sin->sin_family = AF_INET;
1347024Ssam 	sin->sin_addr = if_makeaddr(vs->vs_if.if_net, vs->vs_if.if_host[0]);
1357024Ssam 
1367024Ssam 	sin = (struct sockaddr_in *)&vs->vs_if.if_broadaddr;
1377024Ssam 	sin->sin_family = AF_INET;
1387024Ssam 	sin->sin_addr = if_makeaddr(vs->vs_if.if_net, VV_BROADCAST);
1397024Ssam 	vs->vs_if.if_flags = IFF_BROADCAST;
1407024Ssam 
1417024Ssam 	vs->vs_if.if_init = vvinit;
1427024Ssam 	vs->vs_if.if_output = vvoutput;
1438980Sroot 	vs->vs_if.if_reset = vvreset;
1447640Ssam 	vs->vs_ifuba.ifu_flags = UBA_CANTWAIT | UBA_NEEDBDP | UBA_NEED16;
1457024Ssam 	if_attach(&vs->vs_if);
1467024Ssam }
1477024Ssam 
1487024Ssam /*
1497024Ssam  * Reset of interface after UNIBUS reset.
1507024Ssam  * If interface is on specified uba, reset its state.
1517024Ssam  */
1527024Ssam vvreset(unit, uban)
1537024Ssam 	int unit, uban;
1547024Ssam {
1557024Ssam 	register struct uba_device *ui;
1567024Ssam 
1577024Ssam 	if (unit >= NVV || (ui = vvinfo[unit]) == 0 || ui->ui_alive == 0 ||
1587024Ssam 	    ui->ui_ubanum != uban)
1597024Ssam 		return;
1607024Ssam 	printf(" vv%d", unit);
1617024Ssam 	vvinit(unit);
1627024Ssam }
1637024Ssam 
1647024Ssam /*
1657024Ssam  * Initialization of interface; clear recorded pending
1667024Ssam  * operations, and reinitialize UNIBUS usage.
1677024Ssam  */
1687024Ssam vvinit(unit)
1697024Ssam 	int unit;
1707024Ssam {
1717024Ssam 	register struct vv_softc *vs = &vv_softc[unit];
1727024Ssam 	register struct uba_device *ui = vvinfo[unit];
1737024Ssam 	register struct vvreg *addr;
1747024Ssam 	struct sockaddr_in *sin;
1757640Ssam 	int ubainfo, s;
1767024Ssam 
1777640Ssam 	addr = (struct vvreg *)ui->ui_addr;
1787024Ssam 	if (if_ubainit(&vs->vs_ifuba, ui->ui_ubanum,
1797024Ssam 	    sizeof (struct vv_header), (int)btoc(VVMTU)) == 0) {
1807640Ssam nogo:
1817024Ssam 		printf("vv%d: can't initialize\n", unit);
1827640Ssam 		vs->vs_if.if_flags &= ~IFF_UP;
1837024Ssam 		return;
1847024Ssam 	}
1857024Ssam 
1867024Ssam 	/*
1877640Ssam 	 * discover our host address and post it
1887640Ssam 	 */
1897640Ssam 
1907640Ssam 	vs->vs_if.if_host[0] = vvidentify(unit);
1917640Ssam 	if (vs->vs_if.if_host[0] == 0)
1927640Ssam 		goto nogo;
1937640Ssam 	printf("vv%d: host %d\n", unit, vs->vs_if.if_host[0]);
1947640Ssam 	sin = (struct sockaddr_in *)&vs->vs_if.if_addr;
1957640Ssam 	sin->sin_family = AF_INET;
1967640Ssam 	sin->sin_addr =
1977640Ssam 	    if_makeaddr(vs->vs_if.if_net, vs->vs_if.if_host[0]);
1987640Ssam 
1997640Ssam 	/*
2007640Ssam 	 * Reset the interface, and join the ring
2017640Ssam 	 */
2027640Ssam 	addr->vvocsr = VV_RST | VV_CPB;		/* clear packet buffer */
2037640Ssam 	addr->vvicsr = VV_RST | VV_CONF;	/* close logical relay */
2047640Ssam 	sleep((caddr_t)&lbolt, PZERO);		/* let contacts settle */
2057640Ssam 	vs->vs_init = 0;
2067640Ssam 	vs->vs_flush = 0;
2077640Ssam 	vs->vs_nottaken = 0;
2087640Ssam 
2097640Ssam 	/*
2107640Ssam 	 * Hang a receive and start any
2117640Ssam 	 * pending writes by faking a transmit complete.
2127640Ssam 	 */
2137640Ssam 	s = splimp();
2147640Ssam 	ubainfo = vs->vs_ifuba.ifu_r.ifrw_info;
2157640Ssam 	addr->vviba = (u_short) ubainfo;
2167640Ssam 	addr->vviea = (u_short) (ubainfo >> 16);
2177640Ssam 	addr->vviwc = -(sizeof (struct vv_header) + VVMTU) >> 1;
2187640Ssam 	addr->vvicsr = VV_IEN | VV_CONF | VV_DEN | VV_ENB;
2197640Ssam 	vs->vs_oactive = 1;
2207640Ssam 	vs->vs_if.if_flags |= IFF_UP;
2217640Ssam 	vvxint(unit);
2227640Ssam 	splx(s);
2237640Ssam 	if_rtinit(&vs->vs_if, RTF_UP);
2247640Ssam }
2257640Ssam 
2267640Ssam /*
2277640Ssam  * vvidentify() - return our host address
2287640Ssam  */
2297640Ssam vvidentify(unit)
2307640Ssam {
2317640Ssam 	register struct vv_softc *vs = &vv_softc[unit];
2327640Ssam 	register struct uba_device *ui = vvinfo[unit];
2337640Ssam 	register struct vvreg *addr;
2347640Ssam 	struct mbuf *m;
2357640Ssam 	struct vv_header *v;
2367640Ssam 	int ubainfo, retrying, attempts, waitcount, s;
2377640Ssam 
2387640Ssam 	/*
2397024Ssam 	 * Build a multicast message to identify our address
2407024Ssam 	 */
2417640Ssam 	addr = (struct vvreg *)ui->ui_addr;
2427024Ssam 	attempts = 0;		/* total attempts, including bad msg type */
2437024Ssam 	retrying = 0;		/* first time through */
2449652Ssam 	m = m_get(M_DONTWAIT, MT_HEADER);
2457640Ssam 	if (m == 0) {
2467640Ssam 		printf("vvinit: can't get mbuf");
2477640Ssam 		return (0);
2487640Ssam 	}
2497024Ssam 	m->m_off = MMINOFF;
2507024Ssam 	m->m_len = sizeof(struct vv_header);
2517024Ssam 
2527024Ssam 	v = mtod(m, struct vv_header *);
2537024Ssam 	v->vh_dhost = 0;		/* multicast destination address */
2547024Ssam 	v->vh_shost = 0;		/* will be overwritten with ours */
2557024Ssam 	v->vh_version = RING_VERSION;
2567024Ssam 	v->vh_type = RING_WHOAMI;
2577024Ssam 	v->vh_info = 0;
2587640Ssam 	vs->vs_olen =  if_wubaput(&vs->vs_ifuba, m);
2597640Ssam 	if (vs->vs_ifuba.ifu_flags & UBA_NEEDBDP)
2607640Ssam 		UBAPURGE(vs->vs_ifuba.ifu_uba, vs->vs_ifuba.ifu_w.ifrw_bdp);
2617024Ssam 
2627024Ssam 	/*
2637024Ssam 	 * Reset interface, establish Digital Loopback Mode, and
2647024Ssam 	 * send the multicast (to myself) with Input Copy enabled.
2657024Ssam 	 */
2667024Ssam retry:
2677024Ssam 	ubainfo = vs->vs_ifuba.ifu_r.ifrw_info;
2687024Ssam 	addr->vvicsr = VV_RST;
2697024Ssam 	addr->vviba = (u_short) ubainfo;
2707024Ssam 	addr->vviea = (u_short) (ubainfo >> 16);
2717024Ssam 	addr->vviwc = -(sizeof (struct vv_header) + VVMTU) >> 1;
2727024Ssam 	addr->vvicsr = VV_STE | VV_DEN | VV_ENB | VV_LPB;
2737640Ssam 
2747640Ssam 	/* let flag timers fire so ring will initialize */
2757640Ssam 	sleep((caddr_t) &lbolt, PZERO);
2767640Ssam 	sleep((caddr_t) &lbolt, PZERO);
2777640Ssam 
2787024Ssam 	addr->vvocsr = VV_RST | VV_CPB;	/* clear packet buffer */
2797024Ssam 	ubainfo = vs->vs_ifuba.ifu_w.ifrw_info;
2807024Ssam 	addr->vvoba = (u_short) ubainfo;
2817024Ssam 	addr->vvoea = (u_short) (ubainfo >> 16);
2827024Ssam 	addr->vvowc = -((vs->vs_olen + 1) >> 1);
2837024Ssam 	addr->vvocsr = VV_CPB | VV_DEN | VV_INR | VV_ENB;
2847024Ssam 
2857024Ssam 	/*
2867024Ssam 	 * Wait for receive side to finish.
2877640Ssam 	 * Extract source address (which will be our own),
2887024Ssam 	 * and post to interface structure.
2897024Ssam 	 */
2907024Ssam 	DELAY(1000);
2917640Ssam 	for (waitcount = 0; (addr->vvicsr & VV_RDY) == 0; waitcount++)
2927024Ssam 		if (waitcount < 10)
2937024Ssam 			DELAY(1000);
2947024Ssam 		else {
2957640Ssam 			if (attempts++ < 10)
2967024Ssam 				goto retry;
2977024Ssam 			else {
2987024Ssam 				printf("vv%d: can't initialize\n", unit);
2997024Ssam 				printf("vvinit loopwait: icsr = %b\n",
3007024Ssam 					0xffff&(addr->vvicsr),VV_IBITS);
3017640Ssam 				vs->vs_if.if_flags &= ~IFF_UP;
3027640Ssam 				return (0);
3037024Ssam 			}
3047024Ssam 		}
3057024Ssam 	if (vs->vs_ifuba.ifu_flags & UBA_NEEDBDP)
3067024Ssam 		UBAPURGE(vs->vs_ifuba.ifu_uba, vs->vs_ifuba.ifu_w.ifrw_bdp);
3077024Ssam 	if (vs->vs_ifuba.ifu_xtofree)
3087024Ssam 		m_freem(vs->vs_ifuba.ifu_xtofree);
3097024Ssam 	if (vs->vs_ifuba.ifu_flags & UBA_NEEDBDP)
3107024Ssam 		UBAPURGE(vs->vs_ifuba.ifu_uba, vs->vs_ifuba.ifu_r.ifrw_bdp);
3117024Ssam 	m = if_rubaget(&vs->vs_ifuba, sizeof(struct vv_header), 0);
3127024Ssam 	if (m)
3137024Ssam 		m_freem(m);
3147024Ssam 	/*
3157024Ssam 	 * check message type before we believe the source host address
3167024Ssam 	 */
3177024Ssam 	v = (struct vv_header *)(vs->vs_ifuba.ifu_r.ifrw_addr);
3187640Ssam 	if (v->vh_type != RING_WHOAMI)
3197640Ssam 		goto retry;
3207640Ssam 	return (v->vh_shost);
3217024Ssam }
3227024Ssam 
3237024Ssam /*
3247024Ssam  * Start or restart output on interface.
3257024Ssam  * If interface is not already active, get another datagram
3267024Ssam  * to send off of the interface queue, and map it to the interface
3277024Ssam  * before starting the output.
3287024Ssam  */
3297024Ssam vvstart(dev)
3307024Ssam 	dev_t dev;
3317024Ssam {
3327024Ssam         int unit = VVUNIT(dev);
3337024Ssam 	struct uba_device *ui = vvinfo[unit];
3347024Ssam 	register struct vv_softc *vs = &vv_softc[unit];
3357024Ssam 	register struct vvreg *addr;
3367024Ssam 	struct mbuf *m;
3377640Ssam 	int ubainfo, dest;
3387024Ssam 
3397024Ssam 	if (vs->vs_oactive)
3407024Ssam 		goto restart;
3417024Ssam 	/*
3427024Ssam 	 * Not already active: dequeue another request
3437024Ssam 	 * and map it to the UNIBUS.  If no more requests,
3447024Ssam 	 * just return.
3457024Ssam 	 */
3467024Ssam 	IF_DEQUEUE(&vs->vs_if.if_snd, m);
3477024Ssam 	if (m == 0) {
3487024Ssam 		vs->vs_oactive = 0;
3497024Ssam 		return;
3507024Ssam 	}
3517024Ssam 	dest = mtod(m, struct vv_header *)->vh_dhost;
3527024Ssam 	vs->vs_olen = if_wubaput(&vs->vs_ifuba, m);
3537024Ssam 	vs->vs_lastx = dest;
3547024Ssam 
3557024Ssam restart:
3567024Ssam 	/*
3577024Ssam 	 * Have request mapped to UNIBUS for transmission.
3587024Ssam 	 * Purge any stale data from this BDP, and start the otput.
3597024Ssam 	 */
3607024Ssam 	if (vs->vs_ifuba.ifu_flags & UBA_NEEDBDP)
3617024Ssam 		UBAPURGE(vs->vs_ifuba.ifu_uba, vs->vs_ifuba.ifu_w.ifrw_bdp);
3627024Ssam 	addr = (struct vvreg *)ui->ui_addr;
3637024Ssam 	ubainfo = vs->vs_ifuba.ifu_w.ifrw_info;
3647024Ssam 	addr->vvoba = (u_short) ubainfo;
3657024Ssam 	addr->vvoea = (u_short) (ubainfo >> 16);
3667024Ssam 	addr->vvowc = -((vs->vs_olen + 1) >> 1);
3677024Ssam 	addr->vvocsr = VV_IEN | VV_CPB | VV_DEN | VV_INR | VV_ENB;
3687024Ssam 	vs->vs_oactive = 1;
3697024Ssam }
3707024Ssam 
3717024Ssam /*
3727024Ssam  * VVLNI transmit interrupt
3737024Ssam  * Start another output if more data to send.
3747024Ssam  */
3757024Ssam vvxint(unit)
3767024Ssam 	int unit;
3777024Ssam {
3787024Ssam 	register struct uba_device *ui = vvinfo[unit];
3797024Ssam 	register struct vv_softc *vs = &vv_softc[unit];
3807024Ssam 	register struct vvreg *addr;
3817024Ssam 	register int oc;
3827024Ssam 
3837024Ssam 	addr = (struct vvreg *)ui->ui_addr;
3847024Ssam 	oc = 0xffff & (addr->vvocsr);
3857024Ssam 	if (vs->vs_oactive == 0) {
3867640Ssam 		printf("vv%d: stray interrupt, vvocsr=%b\n", unit,
3877024Ssam 			oc, VV_OBITS);
3887024Ssam 		return;
3897024Ssam 	}
3907024Ssam 	if (oc &  (VV_OPT | VV_RFS)) {
3917640Ssam 		vs->vs_if.if_collisions++;
3927024Ssam 		if (++(vs->vs_tries) < VVRETRY) {
3937024Ssam 			if (oc & VV_OPT)
3947024Ssam 				vs->vs_init++;
3957024Ssam 			if (oc & VV_RFS)
3967024Ssam 				vs->vs_nottaken++;
3977024Ssam 			addr->vvocsr = VV_IEN | VV_ENB | VV_INR;
3987024Ssam 			return;
3997024Ssam 		}
4007024Ssam 		if (oc & VV_OPT)
4017024Ssam 			printf("vv%d: output timeout\n");
4027024Ssam 	}
4037024Ssam 	vs->vs_if.if_opackets++;
4047024Ssam 	vs->vs_oactive = 0;
4057024Ssam 	vs->vs_tries = 0;
4067024Ssam 	if (oc & VVXERR) {
4077024Ssam 		vs->vs_if.if_oerrors++;
4087640Ssam 		printf("vv%d: error, vvocsr=%b\n", unit, 0xffff & oc,
4097024Ssam 			VV_OBITS);
4107024Ssam 	}
4117024Ssam 	if (vs->vs_ifuba.ifu_xtofree) {
4127024Ssam 		m_freem(vs->vs_ifuba.ifu_xtofree);
4137024Ssam 		vs->vs_ifuba.ifu_xtofree = 0;
4147024Ssam 	}
4157024Ssam 	if (vs->vs_if.if_snd.ifq_head == 0) {
4167640Ssam 		vs->vs_lastx = 256;
4177024Ssam 		return;
4187024Ssam 	}
4197024Ssam 	vvstart(unit);
4207024Ssam }
4217024Ssam 
4227024Ssam /*
4237024Ssam  * V2lni interface receiver interrupt.
4247024Ssam  * If input error just drop packet.
4257024Ssam  * Otherwise purge input buffered data path and examine
4267024Ssam  * packet to determine type.  If can't determine length
4277024Ssam  * from type, then have to drop packet.  Othewise decapsulate
4287024Ssam  * packet based on type and pass to type specific higher-level
4297024Ssam  * input routine.
4307024Ssam  */
4317024Ssam vvrint(unit)
4327024Ssam 	int unit;
4337024Ssam {
4347024Ssam 	register struct vv_softc *vs = &vv_softc[unit];
4357024Ssam 	struct vvreg *addr = (struct vvreg *)vvinfo[unit]->ui_addr;
4367024Ssam 	register struct vv_header *vv;
4377024Ssam 	register struct ifqueue *inq;
4387024Ssam     	struct mbuf *m;
4397024Ssam 	int ubainfo, len, off;
4407640Ssam 	short resid;
4417024Ssam 
4427024Ssam 	vs->vs_if.if_ipackets++;
4437024Ssam 	/*
4447024Ssam 	 * Purge BDP; drop if input error indicated.
4457024Ssam 	 */
4467024Ssam 	if (vs->vs_ifuba.ifu_flags & UBA_NEEDBDP)
4477024Ssam 		UBAPURGE(vs->vs_ifuba.ifu_uba, vs->vs_ifuba.ifu_r.ifrw_bdp);
4487024Ssam 	if (addr->vvicsr & VVRERR) {
4497640Ssam 		/*
4507024Ssam 		printf("vv%d: error vvicsr = %b\n", unit,
4517024Ssam 			0xffff&(addr->vvicsr), VV_IBITS);
4527640Ssam 		*/
4537640Ssam 		goto dropit;
4547024Ssam 	}
4557640Ssam 
4567024Ssam 	/*
4577640Ssam 	 * Get packet length from word count residue
4587640Ssam 	 *
4597640Ssam 	 * Compute header offset if trailer protocol
4607640Ssam 	 *
4617640Ssam 	 * Pull packet off interface.  Off is nonzero if packet
4627640Ssam 	 * has trailing header; if_rubaget will then force this header
4637640Ssam 	 * information to be at the front.  The vh_info field
4647640Ssam 	 * carries the offset to the trailer data in trailer
4657640Ssam 	 * format packets.
4667024Ssam 	 */
4677640Ssam 	vv = (struct vv_header *)(vs->vs_ifuba.ifu_r.ifrw_addr);
4687640Ssam 	if (vv_trace)
4697640Ssam 		vvprt_hdr("vi", vv);
4707640Ssam 	resid = addr->vviwc;
4717640Ssam 	if (resid)
4727640Ssam 		resid |= 0176000;		/* ugly!!!! */
4737640Ssam 	len = (((sizeof (struct vv_header) + VVMRU) >> 1) + resid) << 1;
4747640Ssam 	len -= sizeof(struct vv_header);
4757640Ssam 	if (len > VVMRU)
4767640Ssam 		goto dropit;
4777640Ssam #define	vvdataaddr(vv, off, type)	((type)(((caddr_t)((vv)+1)+(off))))
4787640Ssam 	if (vv_dotrailer && vv->vh_type >= RING_IPTrailer &&
4797640Ssam 	     vv->vh_type < RING_IPTrailer+RING_IPNTrailer){
4807640Ssam 		off = (vv->vh_type - RING_IPTrailer) * 512;
4817640Ssam 		if (off > VVMTU)
4827640Ssam 			goto dropit;
4837640Ssam 		vv->vh_type = *vvdataaddr(vv, off, u_short *);
4847640Ssam 		resid = *(vvdataaddr(vv, off+2, u_short *));
4857640Ssam 		if (off + resid > len)
4867640Ssam 			goto dropit;
4877640Ssam 		len = off + resid;
4887640Ssam 	} else
4897640Ssam 		off = 0;
4907640Ssam 	if (len == 0)
4917640Ssam 		goto dropit;
4927640Ssam 	m = if_rubaget(&vs->vs_ifuba, len, off);
4937640Ssam 	if (m == 0)
4947640Ssam 		goto dropit;
4957640Ssam 	if (off) {
4967640Ssam 		m->m_off += 2 * sizeof(u_short);
4977640Ssam 		m->m_len -= 2 * sizeof(u_short);
4987640Ssam 	}
4997024Ssam 	switch (vv->vh_type) {
5007024Ssam #ifdef INET
5017024Ssam 	case RING_IP:
5027024Ssam 		schednetisr(NETISR_IP);
5037024Ssam 		inq = &ipintrq;
5047024Ssam 		break;
5057024Ssam #endif
5067024Ssam 	default:
5077024Ssam 		printf("vv%d: unknown pkt type 0x%x\n", unit, vv->vh_type);
5087640Ssam 		m_freem(m);
5097024Ssam 		goto setup;
5107024Ssam 	}
5117640Ssam 	if (IF_QFULL(inq)) {
5127640Ssam 		IF_DROP(inq);
5137640Ssam 		m_freem(m);
5147640Ssam 	} else
5157640Ssam 		IF_ENQUEUE(inq, m);
5167024Ssam 
5177024Ssam setup:
5187024Ssam 	/*
5197640Ssam 	 * Restart the read for next packet.
5207024Ssam 	 */
5217024Ssam 	ubainfo = vs->vs_ifuba.ifu_r.ifrw_info;
5227024Ssam 	addr->vviba = (u_short) ubainfo;
5237024Ssam 	addr->vviea = (u_short) (ubainfo >> 16);
5247024Ssam 	addr->vviwc = -(sizeof (struct vv_header) + VVMTU) >> 1;
5257024Ssam 	addr->vvicsr = VV_RST | VV_CONF;
5267024Ssam 	addr->vvicsr |= VV_IEN | VV_DEN | VV_ENB;
5277640Ssam 	return;
5287024Ssam 
5297640Ssam dropit:
5307640Ssam 	vs->vs_if.if_ierrors++;
5317640Ssam 	/*
5327640Ssam 	printf("vv%d: error vvicsr = %b\n", unit,
5337640Ssam 		0xffff&(addr->vvicsr), VV_IBITS);
5347640Ssam 	*/
5357640Ssam 	goto setup;
5367024Ssam }
5377024Ssam 
5387024Ssam /*
5397024Ssam  * V2lni output routine.
5407024Ssam  * Encapsulate a packet of type family for the local net.
5417024Ssam  * Use trailer local net encapsulation if enough data in first
5427024Ssam  * packet leaves a multiple of 512 bytes of data in remainder.
5437024Ssam  */
5447024Ssam vvoutput(ifp, m0, dst)
5457024Ssam 	struct ifnet *ifp;
5467024Ssam 	struct mbuf *m0;
5477024Ssam 	struct sockaddr *dst;
5487024Ssam {
5497024Ssam 	register struct mbuf *m = m0;
5507024Ssam 	register struct vv_header *vv;
5517640Ssam 	register int off;
5527640Ssam 	int type, dest, s, error;
5537024Ssam 
5547024Ssam 	switch (dst->sa_family) {
5557024Ssam #ifdef INET
5567024Ssam 	case AF_INET: {
5577640Ssam 		dest = ((struct sockaddr_in *)dst)->sin_addr.s_addr;
5587640Ssam 		if (dest & 0x00ffff00) {
5597640Ssam 			error = EPERM;
5607640Ssam 			goto bad;
5617640Ssam 		}
5627640Ssam 		dest = (dest >> 24) & 0xff;
5637640Ssam 		off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len;
5647640Ssam 		if (vv_dotrailer && off > 0 && (off & 0x1ff) == 0 &&
5657640Ssam 		    m->m_off >= MMINOFF + 2 * sizeof (u_short)) {
5667640Ssam 			type = RING_IPTrailer + (off>>9);
5677640Ssam 			m->m_off -= 2 * sizeof (u_short);
5687640Ssam 			m->m_len += 2 * sizeof (u_short);
5697640Ssam 			*mtod(m, u_short *) = RING_IP;
5707640Ssam 			*(mtod(m, u_short *) + 1) = m->m_len;
5717640Ssam 			goto gottrailertype;
5727640Ssam 		}
5737024Ssam 		type = RING_IP;
5747024Ssam 		off = 0;
5757024Ssam 		goto gottype;
5767024Ssam 		}
5777024Ssam #endif
5787024Ssam 	default:
5797024Ssam 		printf("vv%d: can't handle af%d\n", ifp->if_unit,
5807024Ssam 			dst->sa_family);
5817640Ssam 		error = EAFNOSUPPORT;
5827640Ssam 		goto bad;
5837024Ssam 	}
5847024Ssam 
5857024Ssam gottrailertype:
5867024Ssam 	/*
5877024Ssam 	 * Packet to be sent as trailer: move first packet
5887024Ssam 	 * (control information) to end of chain.
5897024Ssam 	 */
5907024Ssam 	while (m->m_next)
5917024Ssam 		m = m->m_next;
5927024Ssam 	m->m_next = m0;
5937024Ssam 	m = m0->m_next;
5947024Ssam 	m0->m_next = 0;
5957024Ssam 	m0 = m;
5967024Ssam 
5977024Ssam gottype:
5987024Ssam 	/*
5997024Ssam 	 * Add local net header.  If no space in first mbuf,
6007024Ssam 	 * allocate another.
6017024Ssam 	 */
6027024Ssam 	if (m->m_off > MMAXOFF ||
6037024Ssam 	    MMINOFF + sizeof (struct vv_header) > m->m_off) {
6049652Ssam 		m = m_get(M_DONTWAIT, MT_HEADER);
6057024Ssam 		if (m == 0) {
6067640Ssam 			error = ENOBUFS;
6077640Ssam 			goto bad;
6087024Ssam 		}
6097024Ssam 		m->m_next = m0;
6107024Ssam 		m->m_off = MMINOFF;
6117024Ssam 		m->m_len = sizeof (struct vv_header);
6127024Ssam 	} else {
6137024Ssam 		m->m_off -= sizeof (struct vv_header);
6147024Ssam 		m->m_len += sizeof (struct vv_header);
6157024Ssam 	}
6167024Ssam 	vv = mtod(m, struct vv_header *);
6177024Ssam 	vv->vh_shost = ifp->if_host[0];
6187024Ssam 	vv->vh_dhost = dest;
6197024Ssam 	vv->vh_version = RING_VERSION;
6207024Ssam 	vv->vh_type = type;
6217640Ssam 	vv->vh_info = off;
6227640Ssam 	if (vv_trace)
6237640Ssam 		vvprt_hdr("vo", vv);
6247024Ssam 
6257024Ssam 	/*
6267024Ssam 	 * Queue message on interface, and start output if interface
6277024Ssam 	 * not yet active.
6287024Ssam 	 */
6297024Ssam 	s = splimp();
6307640Ssam 	if (IF_QFULL(&ifp->if_snd)) {
6317640Ssam 		IF_DROP(&ifp->if_snd);
6327640Ssam 		error = ENOBUFS;
6337640Ssam 		goto qfull;
6347640Ssam 	}
6357024Ssam 	IF_ENQUEUE(&ifp->if_snd, m);
6367024Ssam 	if (vv_softc[ifp->if_unit].vs_oactive == 0)
6377024Ssam 		vvstart(ifp->if_unit);
6387024Ssam 	splx(s);
6397640Ssam 	return (0);
6407640Ssam 
6417640Ssam qfull:
6427640Ssam 	m0 = m;
6437640Ssam 	splx(s);
6447640Ssam bad:
6457640Ssam 	m_freem(m0);
6467640Ssam 	return(error);
6477024Ssam }
6487024Ssam 
6497024Ssam /*
6507024Ssam  * vvprt_hdr(s, v) print the local net header in "v"
6517024Ssam  * 	with title is "s"
6527024Ssam  */
6537024Ssam vvprt_hdr(s, v)
6547024Ssam 	char *s;
6557024Ssam 	register struct vv_header *v;
6567024Ssam {
6577024Ssam 	printf("%s: dsvti: 0x%x 0x%x 0x%x 0x%x 0x%x\n",
6587024Ssam 		s,
6597024Ssam 		0xff & (int)(v->vh_dhost), 0xff & (int)(v->vh_shost),
6607024Ssam 		0xff & (int)(v->vh_version), 0xff & (int)(v->vh_type),
6617024Ssam 		0xffff & (int)(v->vh_info));
6627024Ssam }
6637024Ssam 
6647024Ssam /*
6657024Ssam  * print "l" hex bytes starting at "s"
6667024Ssam  */
6677024Ssam vvprt_hex(s, l)
6687024Ssam 	char *s;
6697024Ssam 	int l;
6707024Ssam {
6717024Ssam 	register int i;
6727024Ssam 	register int z;
6737024Ssam 
6747024Ssam 	for (i=0 ; i < l; i++) {
6757024Ssam 		z = 0xff & (int)(*(s + i));
6767024Ssam 		printf("%c%c ",
6777024Ssam 		"0123456789abcdef"[(z >> 4) & 0x0f],
6787024Ssam 		"0123456789abcdef"[z & 0x0f]
6797024Ssam 		);
6807024Ssam 	}
6817024Ssam }
682*9799Ssam #endif
683