xref: /csrg-svn/sys/vax/if/if_vv.c (revision 9652)
1*9652Ssam /*	if_vv.c	4.10	82/12/14	*/
27024Ssam 
37024Ssam /*
47024Ssam  * Proteon 10 Meg Ring Driver.
57024Ssam  * This device is called "vv" because its "real name",
67024Ssam  * V2LNI won't work if shortened to the obvious "v2".
77024Ssam  * Hence the subterfuge.
87024Ssam  */
97024Ssam #include "../h/param.h"
107024Ssam #include "../h/systm.h"
117024Ssam #include "../h/mbuf.h"
127024Ssam #include "../h/pte.h"
137024Ssam #include "../h/buf.h"
147024Ssam #include "../h/protosw.h"
157024Ssam #include "../h/socket.h"
167024Ssam #include "../h/vmmac.h"
178465Sroot #include <errno.h>
188465Sroot 
198465Sroot #include "../net/if.h"
208465Sroot #include "../net/route.h"
218421Swnj #include "../netinet/in.h"
228421Swnj #include "../netinet/in_systm.h"
238421Swnj #include "../netinet/ip.h"
248421Swnj #include "../netinet/ip_var.h"
258465Sroot 
268465Sroot #include "../vax/cpu.h"
278465Sroot #include "../vax/mtpr.h"
288421Swnj #include "../vaxif/if_vv.h"
298421Swnj #include "../vaxif/if_uba.h"
308465Sroot #include "../vaxuba/ubareg.h"
318465Sroot #include "../vaxuba/ubavar.h"
327024Ssam 
337024Ssam #include "vv.h"
347024Ssam 
357024Ssam /*
367024Ssam  * N.B. - if WIRECENTER is defined wrong, it can well break
377024Ssam  * the hardware!!
387024Ssam  */
397640Ssam 
407024Ssam #define	WIRECENTER
417024Ssam 
427024Ssam #ifdef WIRECENTER
437024Ssam #define	VV_CONF	VV_HEN		/* drive wire center relay */
447024Ssam #else
457024Ssam #define	VV_CONF	VV_STE		/* allow operation without wire center */
467024Ssam #endif
477024Ssam 
487024Ssam #define	VVMTU	(1024+512)
497640Ssam #define VVMRU	(1024+512+16)	/* space for trailer */
507024Ssam 
517640Ssam int vv_dotrailer = 1,		/* so can do trailers selectively */
527640Ssam     vv_trace = 0;
537640Ssam 
547024Ssam int	vvprobe(), vvattach(), vvrint(), vvxint();
557024Ssam struct	uba_device *vvinfo[NVV];
567024Ssam u_short vvstd[] = { 0 };
577024Ssam struct	uba_driver vvdriver =
587024Ssam 	{ vvprobe, 0, vvattach, 0, vvstd, "vv", vvinfo };
597024Ssam #define	VVUNIT(x)	minor(x)
607024Ssam int	vvinit(),vvoutput(),vvreset();
617024Ssam 
627024Ssam /*
637024Ssam  * Software status of each interface.
647024Ssam  *
657024Ssam  * Each interface is referenced by a network interface structure,
667024Ssam  * vs_if, which the routing code uses to locate the interface.
677024Ssam  * This structure contains the output queue for the interface, its address, ...
687024Ssam  * We also have, for each interface, a UBA interface structure, which
697024Ssam  * contains information about the UNIBUS resources held by the interface:
707024Ssam  * map registers, buffered data paths, etc.  Information is cached in this
717024Ssam  * structure for use by the if_uba.c routines in running the interface
727024Ssam  * efficiently.
737024Ssam  */
747024Ssam struct	vv_softc {
757024Ssam 	struct	ifnet vs_if;		/* network-visible interface */
767024Ssam 	struct	ifuba vs_ifuba;		/* UNIBUS resources */
777024Ssam 	short	vs_oactive;		/* is output active? */
787024Ssam 	short	vs_olen;		/* length of last output */
797024Ssam 	u_short	vs_lastx;		/* last destination address */
807024Ssam 	short	vs_tries;		/* current retry count */
817024Ssam 	short	vs_init;		/* number of ring inits */
827024Ssam 	short	vs_flush;		/* number of flushed packets */
837024Ssam 	short	vs_nottaken;		/* number of packets refused */
847024Ssam } vv_softc[NVV];
857024Ssam 
867024Ssam vvprobe(reg)
877024Ssam 	caddr_t reg;
887024Ssam {
897024Ssam 	register int br, cvec;
907024Ssam 	register struct vvreg *addr = (struct vvreg *)reg;
917024Ssam 
927024Ssam #ifdef lint
937024Ssam 	br = 0; cvec = br; br = cvec;
947024Ssam #endif
957024Ssam 	/* reset interface, enable, and wait till dust settles */
967024Ssam 	addr->vvicsr = VV_RST;
977024Ssam 	addr->vvocsr = VV_RST;
987024Ssam 	DELAY(100000);
997024Ssam 	/* generate interrupt by doing 1 word DMA from 0 in uba space!! */
1007024Ssam 	addr->vvocsr = VV_IEN;		/* enable interrupt */
1017024Ssam 	addr->vvoba = 0;		/* low 16 bits */
1027024Ssam 	addr->vvoea = 0;		/* extended bits */
1037024Ssam 	addr->vvowc = -1;		/* for 1 word */
1047024Ssam 	addr->vvocsr |= VV_DEN;		/* start the DMA */
1057024Ssam 	DELAY(100000);
1067024Ssam 	addr->vvocsr = 0;
1077024Ssam 	if (cvec && cvec != 0x200)
1087024Ssam 		cvec -= 4;		/* backup so vector => recieve */
1097024Ssam 	return(1);
1107024Ssam }
1117024Ssam 
1127024Ssam /*
1137024Ssam  * Interface exists: make available by filling in network interface
1147024Ssam  * record.  System will initialize the interface when it is ready
1157024Ssam  * to accept packets.
1167024Ssam  */
1177024Ssam vvattach(ui)
1187024Ssam 	struct uba_device *ui;
1197024Ssam {
1207024Ssam 	register struct vv_softc *vs = &vv_softc[ui->ui_unit];
1217024Ssam 	register struct sockaddr_in *sin;
1227024Ssam 
1237024Ssam 	vs->vs_if.if_unit = ui->ui_unit;
1247024Ssam 	vs->vs_if.if_name = "vv";
1257024Ssam 	vs->vs_if.if_mtu = VVMTU;
1267024Ssam 	vs->vs_if.if_net = ui->ui_flags;
1277024Ssam 	vs->vs_if.if_host[0] = 0;	/* this will be reset in vvinit() */
1287024Ssam 
1297024Ssam 	sin = (struct sockaddr_in *)&vs->vs_if.if_addr;
1307024Ssam 	sin->sin_family = AF_INET;
1317024Ssam 	sin->sin_addr = if_makeaddr(vs->vs_if.if_net, vs->vs_if.if_host[0]);
1327024Ssam 
1337024Ssam 	sin = (struct sockaddr_in *)&vs->vs_if.if_broadaddr;
1347024Ssam 	sin->sin_family = AF_INET;
1357024Ssam 	sin->sin_addr = if_makeaddr(vs->vs_if.if_net, VV_BROADCAST);
1367024Ssam 	vs->vs_if.if_flags = IFF_BROADCAST;
1377024Ssam 
1387024Ssam 	vs->vs_if.if_init = vvinit;
1397024Ssam 	vs->vs_if.if_output = vvoutput;
1408980Sroot 	vs->vs_if.if_reset = vvreset;
1417640Ssam 	vs->vs_ifuba.ifu_flags = UBA_CANTWAIT | UBA_NEEDBDP | UBA_NEED16;
1427024Ssam 	if_attach(&vs->vs_if);
1437024Ssam }
1447024Ssam 
1457024Ssam /*
1467024Ssam  * Reset of interface after UNIBUS reset.
1477024Ssam  * If interface is on specified uba, reset its state.
1487024Ssam  */
1497024Ssam vvreset(unit, uban)
1507024Ssam 	int unit, uban;
1517024Ssam {
1527024Ssam 	register struct uba_device *ui;
1537024Ssam 
1547024Ssam 	if (unit >= NVV || (ui = vvinfo[unit]) == 0 || ui->ui_alive == 0 ||
1557024Ssam 	    ui->ui_ubanum != uban)
1567024Ssam 		return;
1577024Ssam 	printf(" vv%d", unit);
1587024Ssam 	vvinit(unit);
1597024Ssam }
1607024Ssam 
1617024Ssam /*
1627024Ssam  * Initialization of interface; clear recorded pending
1637024Ssam  * operations, and reinitialize UNIBUS usage.
1647024Ssam  */
1657024Ssam vvinit(unit)
1667024Ssam 	int unit;
1677024Ssam {
1687024Ssam 	register struct vv_softc *vs = &vv_softc[unit];
1697024Ssam 	register struct uba_device *ui = vvinfo[unit];
1707024Ssam 	register struct vvreg *addr;
1717024Ssam 	struct sockaddr_in *sin;
1727640Ssam 	int ubainfo, s;
1737024Ssam 
1747640Ssam 	addr = (struct vvreg *)ui->ui_addr;
1757024Ssam 	if (if_ubainit(&vs->vs_ifuba, ui->ui_ubanum,
1767024Ssam 	    sizeof (struct vv_header), (int)btoc(VVMTU)) == 0) {
1777640Ssam nogo:
1787024Ssam 		printf("vv%d: can't initialize\n", unit);
1797640Ssam 		vs->vs_if.if_flags &= ~IFF_UP;
1807024Ssam 		return;
1817024Ssam 	}
1827024Ssam 
1837024Ssam 	/*
1847640Ssam 	 * discover our host address and post it
1857640Ssam 	 */
1867640Ssam 
1877640Ssam 	vs->vs_if.if_host[0] = vvidentify(unit);
1887640Ssam 	if (vs->vs_if.if_host[0] == 0)
1897640Ssam 		goto nogo;
1907640Ssam 	printf("vv%d: host %d\n", unit, vs->vs_if.if_host[0]);
1917640Ssam 	sin = (struct sockaddr_in *)&vs->vs_if.if_addr;
1927640Ssam 	sin->sin_family = AF_INET;
1937640Ssam 	sin->sin_addr =
1947640Ssam 	    if_makeaddr(vs->vs_if.if_net, vs->vs_if.if_host[0]);
1957640Ssam 
1967640Ssam 	/*
1977640Ssam 	 * Reset the interface, and join the ring
1987640Ssam 	 */
1997640Ssam 	addr->vvocsr = VV_RST | VV_CPB;		/* clear packet buffer */
2007640Ssam 	addr->vvicsr = VV_RST | VV_CONF;	/* close logical relay */
2017640Ssam 	sleep((caddr_t)&lbolt, PZERO);		/* let contacts settle */
2027640Ssam 	vs->vs_init = 0;
2037640Ssam 	vs->vs_flush = 0;
2047640Ssam 	vs->vs_nottaken = 0;
2057640Ssam 
2067640Ssam 	/*
2077640Ssam 	 * Hang a receive and start any
2087640Ssam 	 * pending writes by faking a transmit complete.
2097640Ssam 	 */
2107640Ssam 	s = splimp();
2117640Ssam 	ubainfo = vs->vs_ifuba.ifu_r.ifrw_info;
2127640Ssam 	addr->vviba = (u_short) ubainfo;
2137640Ssam 	addr->vviea = (u_short) (ubainfo >> 16);
2147640Ssam 	addr->vviwc = -(sizeof (struct vv_header) + VVMTU) >> 1;
2157640Ssam 	addr->vvicsr = VV_IEN | VV_CONF | VV_DEN | VV_ENB;
2167640Ssam 	vs->vs_oactive = 1;
2177640Ssam 	vs->vs_if.if_flags |= IFF_UP;
2187640Ssam 	vvxint(unit);
2197640Ssam 	splx(s);
2207640Ssam 	if_rtinit(&vs->vs_if, RTF_UP);
2217640Ssam }
2227640Ssam 
2237640Ssam /*
2247640Ssam  * vvidentify() - return our host address
2257640Ssam  */
2267640Ssam vvidentify(unit)
2277640Ssam {
2287640Ssam 	register struct vv_softc *vs = &vv_softc[unit];
2297640Ssam 	register struct uba_device *ui = vvinfo[unit];
2307640Ssam 	register struct vvreg *addr;
2317640Ssam 	struct mbuf *m;
2327640Ssam 	struct vv_header *v;
2337640Ssam 	int ubainfo, retrying, attempts, waitcount, s;
2347640Ssam 
2357640Ssam 	/*
2367024Ssam 	 * Build a multicast message to identify our address
2377024Ssam 	 */
2387640Ssam 	addr = (struct vvreg *)ui->ui_addr;
2397024Ssam 	attempts = 0;		/* total attempts, including bad msg type */
2407024Ssam 	retrying = 0;		/* first time through */
241*9652Ssam 	m = m_get(M_DONTWAIT, MT_HEADER);
2427640Ssam 	if (m == 0) {
2437640Ssam 		printf("vvinit: can't get mbuf");
2447640Ssam 		return (0);
2457640Ssam 	}
2467024Ssam 	m->m_off = MMINOFF;
2477024Ssam 	m->m_len = sizeof(struct vv_header);
2487024Ssam 
2497024Ssam 	v = mtod(m, struct vv_header *);
2507024Ssam 	v->vh_dhost = 0;		/* multicast destination address */
2517024Ssam 	v->vh_shost = 0;		/* will be overwritten with ours */
2527024Ssam 	v->vh_version = RING_VERSION;
2537024Ssam 	v->vh_type = RING_WHOAMI;
2547024Ssam 	v->vh_info = 0;
2557640Ssam 	vs->vs_olen =  if_wubaput(&vs->vs_ifuba, m);
2567640Ssam 	if (vs->vs_ifuba.ifu_flags & UBA_NEEDBDP)
2577640Ssam 		UBAPURGE(vs->vs_ifuba.ifu_uba, vs->vs_ifuba.ifu_w.ifrw_bdp);
2587024Ssam 
2597024Ssam 	/*
2607024Ssam 	 * Reset interface, establish Digital Loopback Mode, and
2617024Ssam 	 * send the multicast (to myself) with Input Copy enabled.
2627024Ssam 	 */
2637024Ssam retry:
2647024Ssam 	ubainfo = vs->vs_ifuba.ifu_r.ifrw_info;
2657024Ssam 	addr->vvicsr = VV_RST;
2667024Ssam 	addr->vviba = (u_short) ubainfo;
2677024Ssam 	addr->vviea = (u_short) (ubainfo >> 16);
2687024Ssam 	addr->vviwc = -(sizeof (struct vv_header) + VVMTU) >> 1;
2697024Ssam 	addr->vvicsr = VV_STE | VV_DEN | VV_ENB | VV_LPB;
2707640Ssam 
2717640Ssam 	/* let flag timers fire so ring will initialize */
2727640Ssam 	sleep((caddr_t) &lbolt, PZERO);
2737640Ssam 	sleep((caddr_t) &lbolt, PZERO);
2747640Ssam 
2757024Ssam 	addr->vvocsr = VV_RST | VV_CPB;	/* clear packet buffer */
2767024Ssam 	ubainfo = vs->vs_ifuba.ifu_w.ifrw_info;
2777024Ssam 	addr->vvoba = (u_short) ubainfo;
2787024Ssam 	addr->vvoea = (u_short) (ubainfo >> 16);
2797024Ssam 	addr->vvowc = -((vs->vs_olen + 1) >> 1);
2807024Ssam 	addr->vvocsr = VV_CPB | VV_DEN | VV_INR | VV_ENB;
2817024Ssam 
2827024Ssam 	/*
2837024Ssam 	 * Wait for receive side to finish.
2847640Ssam 	 * Extract source address (which will be our own),
2857024Ssam 	 * and post to interface structure.
2867024Ssam 	 */
2877024Ssam 	DELAY(1000);
2887640Ssam 	for (waitcount = 0; (addr->vvicsr & VV_RDY) == 0; waitcount++)
2897024Ssam 		if (waitcount < 10)
2907024Ssam 			DELAY(1000);
2917024Ssam 		else {
2927640Ssam 			if (attempts++ < 10)
2937024Ssam 				goto retry;
2947024Ssam 			else {
2957024Ssam 				printf("vv%d: can't initialize\n", unit);
2967024Ssam 				printf("vvinit loopwait: icsr = %b\n",
2977024Ssam 					0xffff&(addr->vvicsr),VV_IBITS);
2987640Ssam 				vs->vs_if.if_flags &= ~IFF_UP;
2997640Ssam 				return (0);
3007024Ssam 			}
3017024Ssam 		}
3027024Ssam 	if (vs->vs_ifuba.ifu_flags & UBA_NEEDBDP)
3037024Ssam 		UBAPURGE(vs->vs_ifuba.ifu_uba, vs->vs_ifuba.ifu_w.ifrw_bdp);
3047024Ssam 	if (vs->vs_ifuba.ifu_xtofree)
3057024Ssam 		m_freem(vs->vs_ifuba.ifu_xtofree);
3067024Ssam 	if (vs->vs_ifuba.ifu_flags & UBA_NEEDBDP)
3077024Ssam 		UBAPURGE(vs->vs_ifuba.ifu_uba, vs->vs_ifuba.ifu_r.ifrw_bdp);
3087024Ssam 	m = if_rubaget(&vs->vs_ifuba, sizeof(struct vv_header), 0);
3097024Ssam 	if (m)
3107024Ssam 		m_freem(m);
3117024Ssam 	/*
3127024Ssam 	 * check message type before we believe the source host address
3137024Ssam 	 */
3147024Ssam 	v = (struct vv_header *)(vs->vs_ifuba.ifu_r.ifrw_addr);
3157640Ssam 	if (v->vh_type != RING_WHOAMI)
3167640Ssam 		goto retry;
3177640Ssam 	return (v->vh_shost);
3187024Ssam }
3197024Ssam 
3207024Ssam /*
3217024Ssam  * Start or restart output on interface.
3227024Ssam  * If interface is not already active, get another datagram
3237024Ssam  * to send off of the interface queue, and map it to the interface
3247024Ssam  * before starting the output.
3257024Ssam  */
3267024Ssam vvstart(dev)
3277024Ssam 	dev_t dev;
3287024Ssam {
3297024Ssam         int unit = VVUNIT(dev);
3307024Ssam 	struct uba_device *ui = vvinfo[unit];
3317024Ssam 	register struct vv_softc *vs = &vv_softc[unit];
3327024Ssam 	register struct vvreg *addr;
3337024Ssam 	struct mbuf *m;
3347640Ssam 	int ubainfo, dest;
3357024Ssam 
3367024Ssam 	if (vs->vs_oactive)
3377024Ssam 		goto restart;
3387024Ssam 	/*
3397024Ssam 	 * Not already active: dequeue another request
3407024Ssam 	 * and map it to the UNIBUS.  If no more requests,
3417024Ssam 	 * just return.
3427024Ssam 	 */
3437024Ssam 	IF_DEQUEUE(&vs->vs_if.if_snd, m);
3447024Ssam 	if (m == 0) {
3457024Ssam 		vs->vs_oactive = 0;
3467024Ssam 		return;
3477024Ssam 	}
3487024Ssam 	dest = mtod(m, struct vv_header *)->vh_dhost;
3497024Ssam 	vs->vs_olen = if_wubaput(&vs->vs_ifuba, m);
3507024Ssam 	vs->vs_lastx = dest;
3517024Ssam 
3527024Ssam restart:
3537024Ssam 	/*
3547024Ssam 	 * Have request mapped to UNIBUS for transmission.
3557024Ssam 	 * Purge any stale data from this BDP, and start the otput.
3567024Ssam 	 */
3577024Ssam 	if (vs->vs_ifuba.ifu_flags & UBA_NEEDBDP)
3587024Ssam 		UBAPURGE(vs->vs_ifuba.ifu_uba, vs->vs_ifuba.ifu_w.ifrw_bdp);
3597024Ssam 	addr = (struct vvreg *)ui->ui_addr;
3607024Ssam 	ubainfo = vs->vs_ifuba.ifu_w.ifrw_info;
3617024Ssam 	addr->vvoba = (u_short) ubainfo;
3627024Ssam 	addr->vvoea = (u_short) (ubainfo >> 16);
3637024Ssam 	addr->vvowc = -((vs->vs_olen + 1) >> 1);
3647024Ssam 	addr->vvocsr = VV_IEN | VV_CPB | VV_DEN | VV_INR | VV_ENB;
3657024Ssam 	vs->vs_oactive = 1;
3667024Ssam }
3677024Ssam 
3687024Ssam /*
3697024Ssam  * VVLNI transmit interrupt
3707024Ssam  * Start another output if more data to send.
3717024Ssam  */
3727024Ssam vvxint(unit)
3737024Ssam 	int unit;
3747024Ssam {
3757024Ssam 	register struct uba_device *ui = vvinfo[unit];
3767024Ssam 	register struct vv_softc *vs = &vv_softc[unit];
3777024Ssam 	register struct vvreg *addr;
3787024Ssam 	register int oc;
3797024Ssam 
3807024Ssam 	addr = (struct vvreg *)ui->ui_addr;
3817024Ssam 	oc = 0xffff & (addr->vvocsr);
3827024Ssam 	if (vs->vs_oactive == 0) {
3837640Ssam 		printf("vv%d: stray interrupt, vvocsr=%b\n", unit,
3847024Ssam 			oc, VV_OBITS);
3857024Ssam 		return;
3867024Ssam 	}
3877024Ssam 	if (oc &  (VV_OPT | VV_RFS)) {
3887640Ssam 		vs->vs_if.if_collisions++;
3897024Ssam 		if (++(vs->vs_tries) < VVRETRY) {
3907024Ssam 			if (oc & VV_OPT)
3917024Ssam 				vs->vs_init++;
3927024Ssam 			if (oc & VV_RFS)
3937024Ssam 				vs->vs_nottaken++;
3947024Ssam 			addr->vvocsr = VV_IEN | VV_ENB | VV_INR;
3957024Ssam 			return;
3967024Ssam 		}
3977024Ssam 		if (oc & VV_OPT)
3987024Ssam 			printf("vv%d: output timeout\n");
3997024Ssam 	}
4007024Ssam 	vs->vs_if.if_opackets++;
4017024Ssam 	vs->vs_oactive = 0;
4027024Ssam 	vs->vs_tries = 0;
4037024Ssam 	if (oc & VVXERR) {
4047024Ssam 		vs->vs_if.if_oerrors++;
4057640Ssam 		printf("vv%d: error, vvocsr=%b\n", unit, 0xffff & oc,
4067024Ssam 			VV_OBITS);
4077024Ssam 	}
4087024Ssam 	if (vs->vs_ifuba.ifu_xtofree) {
4097024Ssam 		m_freem(vs->vs_ifuba.ifu_xtofree);
4107024Ssam 		vs->vs_ifuba.ifu_xtofree = 0;
4117024Ssam 	}
4127024Ssam 	if (vs->vs_if.if_snd.ifq_head == 0) {
4137640Ssam 		vs->vs_lastx = 256;
4147024Ssam 		return;
4157024Ssam 	}
4167024Ssam 	vvstart(unit);
4177024Ssam }
4187024Ssam 
4197024Ssam /*
4207024Ssam  * V2lni interface receiver interrupt.
4217024Ssam  * If input error just drop packet.
4227024Ssam  * Otherwise purge input buffered data path and examine
4237024Ssam  * packet to determine type.  If can't determine length
4247024Ssam  * from type, then have to drop packet.  Othewise decapsulate
4257024Ssam  * packet based on type and pass to type specific higher-level
4267024Ssam  * input routine.
4277024Ssam  */
4287024Ssam vvrint(unit)
4297024Ssam 	int unit;
4307024Ssam {
4317024Ssam 	register struct vv_softc *vs = &vv_softc[unit];
4327024Ssam 	struct vvreg *addr = (struct vvreg *)vvinfo[unit]->ui_addr;
4337024Ssam 	register struct vv_header *vv;
4347024Ssam 	register struct ifqueue *inq;
4357024Ssam     	struct mbuf *m;
4367024Ssam 	int ubainfo, len, off;
4377640Ssam 	short resid;
4387024Ssam 
4397024Ssam 	vs->vs_if.if_ipackets++;
4407024Ssam 	/*
4417024Ssam 	 * Purge BDP; drop if input error indicated.
4427024Ssam 	 */
4437024Ssam 	if (vs->vs_ifuba.ifu_flags & UBA_NEEDBDP)
4447024Ssam 		UBAPURGE(vs->vs_ifuba.ifu_uba, vs->vs_ifuba.ifu_r.ifrw_bdp);
4457024Ssam 	if (addr->vvicsr & VVRERR) {
4467640Ssam 		/*
4477024Ssam 		printf("vv%d: error vvicsr = %b\n", unit,
4487024Ssam 			0xffff&(addr->vvicsr), VV_IBITS);
4497640Ssam 		*/
4507640Ssam 		goto dropit;
4517024Ssam 	}
4527640Ssam 
4537024Ssam 	/*
4547640Ssam 	 * Get packet length from word count residue
4557640Ssam 	 *
4567640Ssam 	 * Compute header offset if trailer protocol
4577640Ssam 	 *
4587640Ssam 	 * Pull packet off interface.  Off is nonzero if packet
4597640Ssam 	 * has trailing header; if_rubaget will then force this header
4607640Ssam 	 * information to be at the front.  The vh_info field
4617640Ssam 	 * carries the offset to the trailer data in trailer
4627640Ssam 	 * format packets.
4637024Ssam 	 */
4647640Ssam 	vv = (struct vv_header *)(vs->vs_ifuba.ifu_r.ifrw_addr);
4657640Ssam 	if (vv_trace)
4667640Ssam 		vvprt_hdr("vi", vv);
4677640Ssam 	resid = addr->vviwc;
4687640Ssam 	if (resid)
4697640Ssam 		resid |= 0176000;		/* ugly!!!! */
4707640Ssam 	len = (((sizeof (struct vv_header) + VVMRU) >> 1) + resid) << 1;
4717640Ssam 	len -= sizeof(struct vv_header);
4727640Ssam 	if (len > VVMRU)
4737640Ssam 		goto dropit;
4747640Ssam #define	vvdataaddr(vv, off, type)	((type)(((caddr_t)((vv)+1)+(off))))
4757640Ssam 	if (vv_dotrailer && vv->vh_type >= RING_IPTrailer &&
4767640Ssam 	     vv->vh_type < RING_IPTrailer+RING_IPNTrailer){
4777640Ssam 		off = (vv->vh_type - RING_IPTrailer) * 512;
4787640Ssam 		if (off > VVMTU)
4797640Ssam 			goto dropit;
4807640Ssam 		vv->vh_type = *vvdataaddr(vv, off, u_short *);
4817640Ssam 		resid = *(vvdataaddr(vv, off+2, u_short *));
4827640Ssam 		if (off + resid > len)
4837640Ssam 			goto dropit;
4847640Ssam 		len = off + resid;
4857640Ssam 	} else
4867640Ssam 		off = 0;
4877640Ssam 	if (len == 0)
4887640Ssam 		goto dropit;
4897640Ssam 	m = if_rubaget(&vs->vs_ifuba, len, off);
4907640Ssam 	if (m == 0)
4917640Ssam 		goto dropit;
4927640Ssam 	if (off) {
4937640Ssam 		m->m_off += 2 * sizeof(u_short);
4947640Ssam 		m->m_len -= 2 * sizeof(u_short);
4957640Ssam 	}
4967024Ssam 	switch (vv->vh_type) {
4977024Ssam #ifdef INET
4987024Ssam 	case RING_IP:
4997024Ssam 		schednetisr(NETISR_IP);
5007024Ssam 		inq = &ipintrq;
5017024Ssam 		break;
5027024Ssam #endif
5037024Ssam 	default:
5047024Ssam 		printf("vv%d: unknown pkt type 0x%x\n", unit, vv->vh_type);
5057640Ssam 		m_freem(m);
5067024Ssam 		goto setup;
5077024Ssam 	}
5087640Ssam 	if (IF_QFULL(inq)) {
5097640Ssam 		IF_DROP(inq);
5107640Ssam 		m_freem(m);
5117640Ssam 	} else
5127640Ssam 		IF_ENQUEUE(inq, m);
5137024Ssam 
5147024Ssam setup:
5157024Ssam 	/*
5167640Ssam 	 * Restart the read for next packet.
5177024Ssam 	 */
5187024Ssam 	ubainfo = vs->vs_ifuba.ifu_r.ifrw_info;
5197024Ssam 	addr->vviba = (u_short) ubainfo;
5207024Ssam 	addr->vviea = (u_short) (ubainfo >> 16);
5217024Ssam 	addr->vviwc = -(sizeof (struct vv_header) + VVMTU) >> 1;
5227024Ssam 	addr->vvicsr = VV_RST | VV_CONF;
5237024Ssam 	addr->vvicsr |= VV_IEN | VV_DEN | VV_ENB;
5247640Ssam 	return;
5257024Ssam 
5267640Ssam dropit:
5277640Ssam 	vs->vs_if.if_ierrors++;
5287640Ssam 	/*
5297640Ssam 	printf("vv%d: error vvicsr = %b\n", unit,
5307640Ssam 		0xffff&(addr->vvicsr), VV_IBITS);
5317640Ssam 	*/
5327640Ssam 	goto setup;
5337024Ssam }
5347024Ssam 
5357024Ssam /*
5367024Ssam  * V2lni output routine.
5377024Ssam  * Encapsulate a packet of type family for the local net.
5387024Ssam  * Use trailer local net encapsulation if enough data in first
5397024Ssam  * packet leaves a multiple of 512 bytes of data in remainder.
5407024Ssam  */
5417024Ssam vvoutput(ifp, m0, dst)
5427024Ssam 	struct ifnet *ifp;
5437024Ssam 	struct mbuf *m0;
5447024Ssam 	struct sockaddr *dst;
5457024Ssam {
5467024Ssam 	register struct mbuf *m = m0;
5477024Ssam 	register struct vv_header *vv;
5487640Ssam 	register int off;
5497640Ssam 	int type, dest, s, error;
5507024Ssam 
5517024Ssam 	switch (dst->sa_family) {
5527024Ssam #ifdef INET
5537024Ssam 	case AF_INET: {
5547640Ssam 		dest = ((struct sockaddr_in *)dst)->sin_addr.s_addr;
5557640Ssam 		if (dest & 0x00ffff00) {
5567640Ssam 			error = EPERM;
5577640Ssam 			goto bad;
5587640Ssam 		}
5597640Ssam 		dest = (dest >> 24) & 0xff;
5607640Ssam 		off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len;
5617640Ssam 		if (vv_dotrailer && off > 0 && (off & 0x1ff) == 0 &&
5627640Ssam 		    m->m_off >= MMINOFF + 2 * sizeof (u_short)) {
5637640Ssam 			type = RING_IPTrailer + (off>>9);
5647640Ssam 			m->m_off -= 2 * sizeof (u_short);
5657640Ssam 			m->m_len += 2 * sizeof (u_short);
5667640Ssam 			*mtod(m, u_short *) = RING_IP;
5677640Ssam 			*(mtod(m, u_short *) + 1) = m->m_len;
5687640Ssam 			goto gottrailertype;
5697640Ssam 		}
5707024Ssam 		type = RING_IP;
5717024Ssam 		off = 0;
5727024Ssam 		goto gottype;
5737024Ssam 		}
5747024Ssam #endif
5757024Ssam 	default:
5767024Ssam 		printf("vv%d: can't handle af%d\n", ifp->if_unit,
5777024Ssam 			dst->sa_family);
5787640Ssam 		error = EAFNOSUPPORT;
5797640Ssam 		goto bad;
5807024Ssam 	}
5817024Ssam 
5827024Ssam gottrailertype:
5837024Ssam 	/*
5847024Ssam 	 * Packet to be sent as trailer: move first packet
5857024Ssam 	 * (control information) to end of chain.
5867024Ssam 	 */
5877024Ssam 	while (m->m_next)
5887024Ssam 		m = m->m_next;
5897024Ssam 	m->m_next = m0;
5907024Ssam 	m = m0->m_next;
5917024Ssam 	m0->m_next = 0;
5927024Ssam 	m0 = m;
5937024Ssam 
5947024Ssam gottype:
5957024Ssam 	/*
5967024Ssam 	 * Add local net header.  If no space in first mbuf,
5977024Ssam 	 * allocate another.
5987024Ssam 	 */
5997024Ssam 	if (m->m_off > MMAXOFF ||
6007024Ssam 	    MMINOFF + sizeof (struct vv_header) > m->m_off) {
601*9652Ssam 		m = m_get(M_DONTWAIT, MT_HEADER);
6027024Ssam 		if (m == 0) {
6037640Ssam 			error = ENOBUFS;
6047640Ssam 			goto bad;
6057024Ssam 		}
6067024Ssam 		m->m_next = m0;
6077024Ssam 		m->m_off = MMINOFF;
6087024Ssam 		m->m_len = sizeof (struct vv_header);
6097024Ssam 	} else {
6107024Ssam 		m->m_off -= sizeof (struct vv_header);
6117024Ssam 		m->m_len += sizeof (struct vv_header);
6127024Ssam 	}
6137024Ssam 	vv = mtod(m, struct vv_header *);
6147024Ssam 	vv->vh_shost = ifp->if_host[0];
6157024Ssam 	vv->vh_dhost = dest;
6167024Ssam 	vv->vh_version = RING_VERSION;
6177024Ssam 	vv->vh_type = type;
6187640Ssam 	vv->vh_info = off;
6197640Ssam 	if (vv_trace)
6207640Ssam 		vvprt_hdr("vo", vv);
6217024Ssam 
6227024Ssam 	/*
6237024Ssam 	 * Queue message on interface, and start output if interface
6247024Ssam 	 * not yet active.
6257024Ssam 	 */
6267024Ssam 	s = splimp();
6277640Ssam 	if (IF_QFULL(&ifp->if_snd)) {
6287640Ssam 		IF_DROP(&ifp->if_snd);
6297640Ssam 		error = ENOBUFS;
6307640Ssam 		goto qfull;
6317640Ssam 	}
6327024Ssam 	IF_ENQUEUE(&ifp->if_snd, m);
6337024Ssam 	if (vv_softc[ifp->if_unit].vs_oactive == 0)
6347024Ssam 		vvstart(ifp->if_unit);
6357024Ssam 	splx(s);
6367640Ssam 	return (0);
6377640Ssam 
6387640Ssam qfull:
6397640Ssam 	m0 = m;
6407640Ssam 	splx(s);
6417640Ssam bad:
6427640Ssam 	m_freem(m0);
6437640Ssam 	return(error);
6447024Ssam }
6457024Ssam 
6467024Ssam /*
6477024Ssam  * vvprt_hdr(s, v) print the local net header in "v"
6487024Ssam  * 	with title is "s"
6497024Ssam  */
6507024Ssam vvprt_hdr(s, v)
6517024Ssam 	char *s;
6527024Ssam 	register struct vv_header *v;
6537024Ssam {
6547024Ssam 	printf("%s: dsvti: 0x%x 0x%x 0x%x 0x%x 0x%x\n",
6557024Ssam 		s,
6567024Ssam 		0xff & (int)(v->vh_dhost), 0xff & (int)(v->vh_shost),
6577024Ssam 		0xff & (int)(v->vh_version), 0xff & (int)(v->vh_type),
6587024Ssam 		0xffff & (int)(v->vh_info));
6597024Ssam }
6607024Ssam 
6617024Ssam /*
6627024Ssam  * print "l" hex bytes starting at "s"
6637024Ssam  */
6647024Ssam vvprt_hex(s, l)
6657024Ssam 	char *s;
6667024Ssam 	int l;
6677024Ssam {
6687024Ssam 	register int i;
6697024Ssam 	register int z;
6707024Ssam 
6717024Ssam 	for (i=0 ; i < l; i++) {
6727024Ssam 		z = 0xff & (int)(*(s + i));
6737024Ssam 		printf("%c%c ",
6747024Ssam 		"0123456789abcdef"[(z >> 4) & 0x0f],
6757024Ssam 		"0123456789abcdef"[z & 0x0f]
6767024Ssam 		);
6777024Ssam 	}
6787024Ssam }
679