xref: /csrg-svn/sys/vax/if/if_ec.c (revision 16749)
1*16749Sbloom /*	if_ec.c	6.4	84/07/23	*/
26520Sfeldman 
36520Sfeldman #include "ec.h"
46520Sfeldman 
56520Sfeldman /*
66520Sfeldman  * 3Com Ethernet Controller interface
76520Sfeldman  */
89795Ssam #include "../machine/pte.h"
96520Sfeldman 
106520Sfeldman #include "../h/param.h"
116520Sfeldman #include "../h/systm.h"
126520Sfeldman #include "../h/mbuf.h"
136520Sfeldman #include "../h/buf.h"
146520Sfeldman #include "../h/protosw.h"
156520Sfeldman #include "../h/socket.h"
166520Sfeldman #include "../h/vmmac.h"
1713055Ssam #include "../h/ioctl.h"
1813055Ssam #include "../h/errno.h"
198461Sroot 
208461Sroot #include "../net/if.h"
218461Sroot #include "../net/netisr.h"
228461Sroot #include "../net/route.h"
238417Swnj #include "../netinet/in.h"
248417Swnj #include "../netinet/in_systm.h"
258417Swnj #include "../netinet/ip.h"
268417Swnj #include "../netinet/ip_var.h"
2711574Ssam #include "../netinet/if_ether.h"
288417Swnj #include "../netpup/pup.h"
296520Sfeldman 
308461Sroot #include "../vax/cpu.h"
318461Sroot #include "../vax/mtpr.h"
328461Sroot #include "../vaxif/if_ecreg.h"
338461Sroot #include "../vaxif/if_uba.h"
348461Sroot #include "../vaxuba/ubareg.h"
358461Sroot #include "../vaxuba/ubavar.h"
368461Sroot 
37*16749Sbloom /*
38*16749Sbloom  * The memory address must be consecutive.  Any allocations
39*16749Sbloom  * not in order will cause problems on a VAX 780.
40*16749Sbloom  */
416520Sfeldman 
42*16749Sbloom int ecmem[] = {
43*16749Sbloom #if NEC > 0
44*16749Sbloom 	0000000,
45*16749Sbloom #endif
46*16749Sbloom #if NEC > 1
47*16749Sbloom 	0100000,
48*16749Sbloom #endif
49*16749Sbloom #if NEC > 2
50*16749Sbloom 	0200000,
51*16749Sbloom #endif
52*16749Sbloom #if NEC > 3
53*16749Sbloom 	0300000,
54*16749Sbloom #endif
55*16749Sbloom #if NEC > 4
56*16749Sbloom 	0400000,
57*16749Sbloom #endif
58*16749Sbloom #if NEC > 5
59*16749Sbloom 	0500000,
60*16749Sbloom #endif
61*16749Sbloom #if NEC > 6
62*16749Sbloom 	0600000,
63*16749Sbloom #endif
64*16749Sbloom #if NEC > 7
65*16749Sbloom 	0700000,
66*16749Sbloom #endif
67*16749Sbloom 	};
68*16749Sbloom 
69*16749Sbloom int necmem = 0;			/* count of current memory number for ecprobe */
70*16749Sbloom 
716520Sfeldman int	ecprobe(), ecattach(), ecrint(), ecxint(), eccollide();
726520Sfeldman struct	uba_device *ecinfo[NEC];
736520Sfeldman u_short ecstd[] = { 0 };
746520Sfeldman struct	uba_driver ecdriver =
756520Sfeldman 	{ ecprobe, 0, ecattach, 0, ecstd, "ec", ecinfo };
766520Sfeldman #define	ECUNIT(x)	minor(x)
776520Sfeldman 
7813055Ssam int	ecinit(),ecioctl(),ecoutput(),ecreset();
798773Sroot struct	mbuf *ecget();
806520Sfeldman 
816545Sfeldman extern struct ifnet loif;
826545Sfeldman 
836520Sfeldman /*
846520Sfeldman  * Ethernet software status per interface.
856520Sfeldman  *
866520Sfeldman  * Each interface is referenced by a network interface structure,
876520Sfeldman  * es_if, which the routing code uses to locate the interface.
886520Sfeldman  * This structure contains the output queue for the interface, its address, ...
896520Sfeldman  * We also have, for each interface, a UBA interface structure, which
906520Sfeldman  * contains information about the UNIBUS resources held by the interface:
916520Sfeldman  * map registers, buffered data paths, etc.  Information is cached in this
926520Sfeldman  * structure for use by the if_uba.c routines in running the interface
936520Sfeldman  * efficiently.
946520Sfeldman  */
956520Sfeldman struct	ec_softc {
9611574Ssam 	struct	arpcom es_ac;		/* common Ethernet structures */
9711574Ssam #define	es_if	es_ac.ac_if		/* network-visible interface */
9811574Ssam #define	es_addr	es_ac.ac_enaddr		/* hardware Ethernet address */
996520Sfeldman 	struct	ifuba es_ifuba;		/* UNIBUS resources */
1006520Sfeldman 	short	es_mask;		/* mask for current output delay */
1016520Sfeldman 	short	es_oactive;		/* is output active? */
1028773Sroot 	u_char	*es_buf[16];		/* virtual addresses of buffers */
1036520Sfeldman } ec_softc[NEC];
1046520Sfeldman 
1056520Sfeldman /*
1066520Sfeldman  * Do output DMA to determine interface presence and
1076520Sfeldman  * interrupt vector.  DMA is too short to disturb other hosts.
1086520Sfeldman  */
1096520Sfeldman ecprobe(reg)
1106520Sfeldman 	caddr_t reg;
1116520Sfeldman {
1126520Sfeldman 	register int br, cvec;		/* r11, r10 value-result */
1136520Sfeldman 	register struct ecdevice *addr = (struct ecdevice *)reg;
114*16749Sbloom 	register caddr_t ecbuf = (caddr_t) &umem[numuba][ecmem[necmem]];
1156520Sfeldman 
1166520Sfeldman #ifdef lint
1176520Sfeldman 	br = 0; cvec = br; br = cvec;
1186520Sfeldman 	ecrint(0); ecxint(0); eccollide(0);
1196520Sfeldman #endif
1206520Sfeldman 	/*
1216637Sfeldman 	 * Make sure memory is turned on
1226637Sfeldman 	 */
1236637Sfeldman 	addr->ec_rcr = EC_AROM;
1246637Sfeldman 	/*
1257470Sfeldman 	 * Disable map registers for ec unibus space,
1267470Sfeldman 	 * but don't allocate yet.
1277470Sfeldman 	 */
128*16749Sbloom 	(void) ubamem(numuba, ecmem[necmem], 32*2, 0);
1297470Sfeldman 	/*
1306520Sfeldman 	 * Check for existence of buffers on Unibus.
1316520Sfeldman 	 */
1328773Sroot 	if (badaddr((caddr_t)ecbuf, 2)) {
1337470Sfeldman 	bad1:
1347470Sfeldman 		printf("ec: buffer mem not found\n");
1357470Sfeldman 	bad2:
1368773Sroot 		(void) ubamem(numuba, 0, 0, 0);	/* reenable map (780 only) */
1377470Sfeldman 		addr->ec_rcr = EC_MDISAB;	/* disable memory */
138*16749Sbloom 		necmem++;
1396520Sfeldman 		return (0);
1406520Sfeldman 	}
1417470Sfeldman #if VAX780
1427470Sfeldman 	if (cpu == VAX_780 && uba_hd[numuba].uh_uba->uba_sr) {
1437470Sfeldman 		uba_hd[numuba].uh_uba->uba_sr = uba_hd[numuba].uh_uba->uba_sr;
1447470Sfeldman 		goto bad1;
1457470Sfeldman 	}
1467470Sfeldman #endif
1476520Sfeldman 
1486520Sfeldman 	/*
1496520Sfeldman 	 * Tell the system that the board has memory here, so it won't
1506520Sfeldman 	 * attempt to allocate the addresses later.
1516520Sfeldman 	 */
152*16749Sbloom 	if (ubamem(numuba, ecmem[necmem], 32*2, 1) == 0) {
1537470Sfeldman 		printf("ecprobe: cannot reserve uba addresses\n");
1547470Sfeldman 		goto bad2;
1557470Sfeldman 	}
1566520Sfeldman 
1576520Sfeldman 	/*
1586520Sfeldman 	 * Make a one byte packet in what should be buffer #0.
1596520Sfeldman 	 * Submit it for sending.  This whould cause an xmit interrupt.
1606520Sfeldman 	 * The xmit interrupt vector is 8 bytes after the receive vector,
1616520Sfeldman 	 * so adjust for this before returning.
1626520Sfeldman 	 */
1636520Sfeldman 	*(u_short *)ecbuf = (u_short) 03777;
1646520Sfeldman 	ecbuf[03777] = '\0';
1656520Sfeldman 	addr->ec_xcr = EC_XINTEN|EC_XWBN;
1666520Sfeldman 	DELAY(100000);
1676520Sfeldman 	addr->ec_xcr = EC_XCLR;
1687216Ssam 	if (cvec > 0 && cvec != 0x200) {
1697470Sfeldman 		if (cvec & 04) {	/* collision interrupt */
1707470Sfeldman 			cvec -= 04;
1717470Sfeldman 			br += 1;		/* rcv is collision + 1 */
1727470Sfeldman 		} else {		/* xmit interrupt */
1737470Sfeldman 			cvec -= 010;
1747470Sfeldman 			br += 2;		/* rcv is xmit + 2 */
1757470Sfeldman 		}
1767216Ssam 	}
177*16749Sbloom 	necmem++;
1786520Sfeldman 	return (1);
1796520Sfeldman }
1806520Sfeldman 
1816520Sfeldman /*
1826520Sfeldman  * Interface exists: make available by filling in network interface
1836520Sfeldman  * record.  System will initialize the interface when it is ready
1846520Sfeldman  * to accept packets.
1856520Sfeldman  */
1866520Sfeldman ecattach(ui)
1876520Sfeldman 	struct uba_device *ui;
1886520Sfeldman {
1897216Ssam 	struct ec_softc *es = &ec_softc[ui->ui_unit];
1907216Ssam 	register struct ifnet *ifp = &es->es_if;
1916520Sfeldman 	register struct ecdevice *addr = (struct ecdevice *)ui->ui_addr;
1927216Ssam 	struct sockaddr_in *sin;
1937216Ssam 	int i, j;
1947216Ssam 	u_char *cp;
1956520Sfeldman 
1967216Ssam 	ifp->if_unit = ui->ui_unit;
1977216Ssam 	ifp->if_name = "ec";
1989745Ssam 	ifp->if_mtu = ETHERMTU;
1996520Sfeldman 
2006520Sfeldman 	/*
2017216Ssam 	 * Read the ethernet address off the board, one nibble at a time.
2026520Sfeldman 	 */
2036520Sfeldman 	addr->ec_xcr = EC_UECLR;
2046520Sfeldman 	addr->ec_rcr = EC_AROM;
20516217Skarels 	cp = (u_char *) &es->es_addr;
2067216Ssam #define	NEXTBIT	addr->ec_rcr = EC_AROM|EC_ASTEP; addr->ec_rcr = EC_AROM
20716217Skarels 	for (i=0; i < sizeof (es->es_addr); i++) {
2086520Sfeldman 		*cp = 0;
2096520Sfeldman 		for (j=0; j<=4; j+=4) {
2106520Sfeldman 			*cp |= ((addr->ec_rcr >> 8) & 0xf) << j;
2117216Ssam 			NEXTBIT; NEXTBIT; NEXTBIT; NEXTBIT;
2126520Sfeldman 		}
2136520Sfeldman 		cp++;
2146520Sfeldman 	}
2156520Sfeldman 	sin = (struct sockaddr_in *)&es->es_if.if_addr;
2166520Sfeldman 	sin->sin_family = AF_INET;
2177216Ssam 	ifp->if_init = ecinit;
21813055Ssam 	ifp->if_ioctl = ecioctl;
2197216Ssam 	ifp->if_output = ecoutput;
2208977Sroot 	ifp->if_reset = ecreset;
2216520Sfeldman 	for (i=0; i<16; i++)
222*16749Sbloom 		es->es_buf[i]
223*16749Sbloom 		    = (u_char *)&umem[ui->ui_ubanum][ecmem[ifp->if_unit]+2048*i];
2247216Ssam 	if_attach(ifp);
2256520Sfeldman }
2266520Sfeldman 
2276520Sfeldman /*
2286520Sfeldman  * Reset of interface after UNIBUS reset.
2296520Sfeldman  * If interface is on specified uba, reset its state.
2306520Sfeldman  */
2316520Sfeldman ecreset(unit, uban)
2326520Sfeldman 	int unit, uban;
2336520Sfeldman {
2346520Sfeldman 	register struct uba_device *ui;
2356520Sfeldman 
2366520Sfeldman 	if (unit >= NEC || (ui = ecinfo[unit]) == 0 || ui->ui_alive == 0 ||
2376520Sfeldman 	    ui->ui_ubanum != uban)
2386520Sfeldman 		return;
2396520Sfeldman 	printf(" ec%d", unit);
240*16749Sbloom 	(void) ubamem(uban, ecmem[unit], 32*2, 0);	/* mr disable (no alloc) */
24116207Skarels 	ec_softc[unit].es_if.if_flags &= ~IFF_RUNNING;
2426520Sfeldman 	ecinit(unit);
2436520Sfeldman }
2446520Sfeldman 
2456520Sfeldman /*
2466520Sfeldman  * Initialization of interface; clear recorded pending
2476520Sfeldman  * operations, and reinitialize UNIBUS usage.
2486520Sfeldman  */
2496520Sfeldman ecinit(unit)
2506520Sfeldman 	int unit;
2516520Sfeldman {
2527216Ssam 	struct ec_softc *es = &ec_softc[unit];
2537216Ssam 	struct ecdevice *addr;
25411574Ssam 	register struct ifnet *ifp = &es->es_if;
25513086Ssam 	register struct sockaddr_in *sin;
25613055Ssam 	int i, s;
2576520Sfeldman 
25811574Ssam 	sin = (struct sockaddr_in *)&ifp->if_addr;
25913055Ssam 	if (sin->sin_addr.s_addr == 0)		/* address still unknown */
26011574Ssam 		return;
26111574Ssam 
2626520Sfeldman 	/*
2637217Sfeldman 	 * Hang receive buffers and start any pending writes.
2646637Sfeldman 	 * Writing into the rcr also makes sure the memory
2656637Sfeldman 	 * is turned on.
2666520Sfeldman 	 */
26713055Ssam 	if ((es->es_if.if_flags & IFF_RUNNING) == 0) {
26813055Ssam 		addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
26913055Ssam 		s = splimp();
27013055Ssam 		for (i = ECRHBF; i >= ECRLBF; i--)
27113055Ssam 			addr->ec_rcr = EC_READ | i;
27213055Ssam 		es->es_oactive = 0;
27313055Ssam 		es->es_mask = ~0;
27413055Ssam 		es->es_if.if_flags |= IFF_UP|IFF_RUNNING;
27513055Ssam 		if (es->es_if.if_snd.ifq_head)
27613055Ssam 			ecstart(unit);
27713055Ssam 		splx(s);
27813055Ssam 	}
2797150Swnj 	if_rtinit(&es->es_if, RTF_UP);
28011574Ssam 	arpwhohas(&es->es_ac, &sin->sin_addr);
2816520Sfeldman }
2826520Sfeldman 
2836520Sfeldman /*
2846520Sfeldman  * Start or restart output on interface.
2856520Sfeldman  * If interface is already active, then this is a retransmit
2866545Sfeldman  * after a collision, and just restuff registers.
2876520Sfeldman  * If interface is not already active, get another datagram
2886520Sfeldman  * to send off of the interface queue, and map it to the interface
2896520Sfeldman  * before starting the output.
2906520Sfeldman  */
2916520Sfeldman ecstart(dev)
2926520Sfeldman 	dev_t dev;
2936520Sfeldman {
2948773Sroot         int unit = ECUNIT(dev);
2957216Ssam 	struct ec_softc *es = &ec_softc[unit];
2967216Ssam 	struct ecdevice *addr;
2976520Sfeldman 	struct mbuf *m;
2986520Sfeldman 
2996520Sfeldman 	if (es->es_oactive)
3006520Sfeldman 		goto restart;
3016520Sfeldman 
3026520Sfeldman 	IF_DEQUEUE(&es->es_if.if_snd, m);
3036520Sfeldman 	if (m == 0) {
3046520Sfeldman 		es->es_oactive = 0;
3056520Sfeldman 		return;
3066520Sfeldman 	}
3076520Sfeldman 	ecput(es->es_buf[ECTBF], m);
3086520Sfeldman 
3096520Sfeldman restart:
3107216Ssam 	addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
3116520Sfeldman 	addr->ec_xcr = EC_WRITE|ECTBF;
3126520Sfeldman 	es->es_oactive = 1;
3136520Sfeldman }
3146520Sfeldman 
3156520Sfeldman /*
3166520Sfeldman  * Ethernet interface transmitter interrupt.
3176520Sfeldman  * Start another output if more data to send.
3186520Sfeldman  */
3196520Sfeldman ecxint(unit)
3206520Sfeldman 	int unit;
3216520Sfeldman {
3226520Sfeldman 	register struct ec_softc *es = &ec_softc[unit];
3237216Ssam 	register struct ecdevice *addr =
3247216Ssam 		(struct ecdevice *)ecinfo[unit]->ui_addr;
3256520Sfeldman 
3266520Sfeldman 	if (es->es_oactive == 0)
3276520Sfeldman 		return;
3287216Ssam 	if ((addr->ec_xcr&EC_XDONE) == 0 || (addr->ec_xcr&EC_XBN) != ECTBF) {
3297216Ssam 		printf("ec%d: stray xmit interrupt, xcr=%b\n", unit,
3307216Ssam 			addr->ec_xcr, EC_XBITS);
3317216Ssam 		es->es_oactive = 0;
3327216Ssam 		addr->ec_xcr = EC_XCLR;
3337216Ssam 		return;
3347216Ssam 	}
3356520Sfeldman 	es->es_if.if_opackets++;
3366520Sfeldman 	es->es_oactive = 0;
3376520Sfeldman 	es->es_mask = ~0;
3386520Sfeldman 	addr->ec_xcr = EC_XCLR;
3397216Ssam 	if (es->es_if.if_snd.ifq_head)
3407216Ssam 		ecstart(unit);
3416520Sfeldman }
3426520Sfeldman 
3436520Sfeldman /*
3446520Sfeldman  * Collision on ethernet interface.  Do exponential
3456520Sfeldman  * backoff, and retransmit.  If have backed off all
3466520Sfeldman  * the way print warning diagnostic, and drop packet.
3476520Sfeldman  */
3486520Sfeldman eccollide(unit)
3496520Sfeldman 	int unit;
3506520Sfeldman {
3516520Sfeldman 	struct ec_softc *es = &ec_softc[unit];
3526520Sfeldman 
3536520Sfeldman 	es->es_if.if_collisions++;
3547216Ssam 	if (es->es_oactive)
3557216Ssam 		ecdocoll(unit);
3566520Sfeldman }
3576520Sfeldman 
3586520Sfeldman ecdocoll(unit)
3596520Sfeldman 	int unit;
3606520Sfeldman {
3616520Sfeldman 	register struct ec_softc *es = &ec_softc[unit];
3626545Sfeldman 	register struct ecdevice *addr =
3636545Sfeldman 	    (struct ecdevice *)ecinfo[unit]->ui_addr;
3646545Sfeldman 	register i;
3656545Sfeldman 	int delay;
3666520Sfeldman 
3676520Sfeldman 	/*
3686520Sfeldman 	 * Es_mask is a 16 bit number with n low zero bits, with
3696520Sfeldman 	 * n the number of backoffs.  When es_mask is 0 we have
3706520Sfeldman 	 * backed off 16 times, and give up.
3716520Sfeldman 	 */
3726520Sfeldman 	if (es->es_mask == 0) {
3736545Sfeldman 		es->es_if.if_oerrors++;
3746520Sfeldman 		printf("ec%d: send error\n", unit);
3756520Sfeldman 		/*
3766545Sfeldman 		 * Reset interface, then requeue rcv buffers.
3776545Sfeldman 		 * Some incoming packets may be lost, but that
3786545Sfeldman 		 * can't be helped.
3796520Sfeldman 		 */
3806545Sfeldman 		addr->ec_xcr = EC_UECLR;
3816545Sfeldman 		for (i=ECRHBF; i>=ECRLBF; i--)
3826545Sfeldman 			addr->ec_rcr = EC_READ|i;
3836545Sfeldman 		/*
3846545Sfeldman 		 * Reset and transmit next packet (if any).
3856545Sfeldman 		 */
3866545Sfeldman 		es->es_oactive = 0;
3876545Sfeldman 		es->es_mask = ~0;
3886545Sfeldman 		if (es->es_if.if_snd.ifq_head)
3896545Sfeldman 			ecstart(unit);
3906520Sfeldman 		return;
3916520Sfeldman 	}
3926520Sfeldman 	/*
3936545Sfeldman 	 * Do exponential backoff.  Compute delay based on low bits
3946545Sfeldman 	 * of the interval timer.  Then delay for that number of
3956545Sfeldman 	 * slot times.  A slot time is 51.2 microseconds (rounded to 51).
3966545Sfeldman 	 * This does not take into account the time already used to
3976545Sfeldman 	 * process the interrupt.
3986520Sfeldman 	 */
3996520Sfeldman 	es->es_mask <<= 1;
4006545Sfeldman 	delay = mfpr(ICR) &~ es->es_mask;
4016545Sfeldman 	DELAY(delay * 51);
4026520Sfeldman 	/*
4036545Sfeldman 	 * Clear the controller's collision flag, thus enabling retransmit.
4046520Sfeldman 	 */
4057470Sfeldman 	addr->ec_xcr = EC_CLEAR;
4066520Sfeldman }
4076520Sfeldman 
4086520Sfeldman /*
4096520Sfeldman  * Ethernet interface receiver interrupt.
4106520Sfeldman  * If input error just drop packet.
4116520Sfeldman  * Otherwise purge input buffered data path and examine
4126520Sfeldman  * packet to determine type.  If can't determine length
4136520Sfeldman  * from type, then have to drop packet.  Othewise decapsulate
4146520Sfeldman  * packet based on type and pass to type specific higher-level
4156520Sfeldman  * input routine.
4166520Sfeldman  */
4176520Sfeldman ecrint(unit)
4186520Sfeldman 	int unit;
4196520Sfeldman {
4206520Sfeldman 	struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
4216520Sfeldman 
4226520Sfeldman 	while (addr->ec_rcr & EC_RDONE)
4236520Sfeldman 		ecread(unit);
4246520Sfeldman }
4256520Sfeldman 
4266520Sfeldman ecread(unit)
4276520Sfeldman 	int unit;
4286520Sfeldman {
4296520Sfeldman 	register struct ec_softc *es = &ec_softc[unit];
4306520Sfeldman 	struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
4319745Ssam 	register struct ether_header *ec;
4326520Sfeldman     	struct mbuf *m;
4338773Sroot 	int len, off, resid, ecoff, rbuf;
4346520Sfeldman 	register struct ifqueue *inq;
4358773Sroot 	u_char *ecbuf;
4366520Sfeldman 
4376520Sfeldman 	es->es_if.if_ipackets++;
4388773Sroot 	rbuf = addr->ec_rcr & EC_RBN;
4398773Sroot 	if (rbuf < ECRLBF || rbuf > ECRHBF)
4406520Sfeldman 		panic("ecrint");
4418773Sroot 	ecbuf = es->es_buf[rbuf];
4426520Sfeldman 	ecoff = *(short *)ecbuf;
4436545Sfeldman 	if (ecoff <= ECRDOFF || ecoff > 2046) {
4446520Sfeldman 		es->es_if.if_ierrors++;
4456520Sfeldman #ifdef notdef
4466520Sfeldman 		if (es->es_if.if_ierrors % 100 == 0)
4476520Sfeldman 			printf("ec%d: += 100 input errors\n", unit);
4486520Sfeldman #endif
4496520Sfeldman 		goto setup;
4506520Sfeldman 	}
4516520Sfeldman 
4526520Sfeldman 	/*
4536520Sfeldman 	 * Get input data length.
4546520Sfeldman 	 * Get pointer to ethernet header (in input buffer).
4556520Sfeldman 	 * Deal with trailer protocol: if type is PUP trailer
4566520Sfeldman 	 * get true type from first 16-bit word past data.
4576520Sfeldman 	 * Remember that type was trailer by setting off.
4586520Sfeldman 	 */
4599745Ssam 	len = ecoff - ECRDOFF - sizeof (struct ether_header);
4609745Ssam 	ec = (struct ether_header *)(ecbuf + ECRDOFF);
4619745Ssam 	ec->ether_type = ntohs((u_short)ec->ether_type);
4626520Sfeldman #define	ecdataaddr(ec, off, type)	((type)(((caddr_t)((ec)+1)+(off))))
4639745Ssam 	if (ec->ether_type >= ETHERPUP_TRAIL &&
4649745Ssam 	    ec->ether_type < ETHERPUP_TRAIL+ETHERPUP_NTRAILER) {
4659745Ssam 		off = (ec->ether_type - ETHERPUP_TRAIL) * 512;
4669745Ssam 		if (off >= ETHERMTU)
4676520Sfeldman 			goto setup;		/* sanity */
4689745Ssam 		ec->ether_type = ntohs(*ecdataaddr(ec, off, u_short *));
4699745Ssam 		resid = ntohs(*(ecdataaddr(ec, off+2, u_short *)));
4706520Sfeldman 		if (off + resid > len)
4716520Sfeldman 			goto setup;		/* sanity */
4726520Sfeldman 		len = off + resid;
4736520Sfeldman 	} else
4746520Sfeldman 		off = 0;
4756520Sfeldman 	if (len == 0)
4766520Sfeldman 		goto setup;
4776520Sfeldman 
4786520Sfeldman 	/*
4796520Sfeldman 	 * Pull packet off interface.  Off is nonzero if packet
4806520Sfeldman 	 * has trailing header; ecget will then force this header
4816520Sfeldman 	 * information to be at the front, but we still have to drop
4826520Sfeldman 	 * the type and length which are at the front of any trailer data.
4836520Sfeldman 	 */
4846520Sfeldman 	m = ecget(ecbuf, len, off);
4856520Sfeldman 	if (m == 0)
4866520Sfeldman 		goto setup;
4876520Sfeldman 	if (off) {
4886520Sfeldman 		m->m_off += 2 * sizeof (u_short);
4896520Sfeldman 		m->m_len -= 2 * sizeof (u_short);
4906520Sfeldman 	}
4919745Ssam 	switch (ec->ether_type) {
4926520Sfeldman 
4936520Sfeldman #ifdef INET
4949745Ssam 	case ETHERPUP_IPTYPE:
4956520Sfeldman 		schednetisr(NETISR_IP);
4966520Sfeldman 		inq = &ipintrq;
4976520Sfeldman 		break;
49811574Ssam 
49911574Ssam 	case ETHERPUP_ARPTYPE:
50011574Ssam 		arpinput(&es->es_ac, m);
50113987Ssam 		goto setup;
5026520Sfeldman #endif
5036520Sfeldman 	default:
5046520Sfeldman 		m_freem(m);
5056520Sfeldman 		goto setup;
5066520Sfeldman 	}
5076520Sfeldman 
5086520Sfeldman 	if (IF_QFULL(inq)) {
5096520Sfeldman 		IF_DROP(inq);
5106520Sfeldman 		m_freem(m);
5117216Ssam 		goto setup;
5127216Ssam 	}
5137216Ssam 	IF_ENQUEUE(inq, m);
5146520Sfeldman 
5156520Sfeldman setup:
5166520Sfeldman 	/*
5176520Sfeldman 	 * Reset for next packet.
5186520Sfeldman 	 */
5198773Sroot 	addr->ec_rcr = EC_READ|EC_RCLR|rbuf;
5206520Sfeldman }
5216520Sfeldman 
5226520Sfeldman /*
5236520Sfeldman  * Ethernet output routine.
5246520Sfeldman  * Encapsulate a packet of type family for the local net.
5256520Sfeldman  * Use trailer local net encapsulation if enough data in first
5266520Sfeldman  * packet leaves a multiple of 512 bytes of data in remainder.
5276545Sfeldman  * If destination is this address or broadcast, send packet to
5286545Sfeldman  * loop device to kludge around the fact that 3com interfaces can't
5296545Sfeldman  * talk to themselves.
5306520Sfeldman  */
5316520Sfeldman ecoutput(ifp, m0, dst)
5326520Sfeldman 	struct ifnet *ifp;
5336520Sfeldman 	struct mbuf *m0;
5346520Sfeldman 	struct sockaddr *dst;
5356520Sfeldman {
53611574Ssam 	int type, s, error;
53716217Skarels 	struct ether_addr edst;
53811574Ssam 	struct in_addr idst;
5396520Sfeldman 	register struct ec_softc *es = &ec_softc[ifp->if_unit];
5406520Sfeldman 	register struct mbuf *m = m0;
5419745Ssam 	register struct ether_header *ec;
54212771Ssam 	register int off;
54311574Ssam 	struct mbuf *mcopy = (struct mbuf *)0;
5446520Sfeldman 
5456520Sfeldman 	switch (dst->sa_family) {
5466520Sfeldman 
5476520Sfeldman #ifdef INET
5486520Sfeldman 	case AF_INET:
54911574Ssam 		idst = ((struct sockaddr_in *)dst)->sin_addr;
55016217Skarels 		if (!arpresolve(&es->es_ac, m, &idst, &edst))
55111574Ssam 			return (0);	/* if not yet resolved */
55211574Ssam 		if (in_lnaof(idst) == INADDR_ANY)
5538838Sroot 			mcopy = m_copy(m, 0, (int)M_COPYALL);
5546520Sfeldman 		off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len;
55513055Ssam 		/* need per host negotiation */
55613055Ssam 		if ((ifp->if_flags & IFF_NOTRAILERS) == 0)
5576520Sfeldman 		if (off > 0 && (off & 0x1ff) == 0 &&
5586520Sfeldman 		    m->m_off >= MMINOFF + 2 * sizeof (u_short)) {
5599745Ssam 			type = ETHERPUP_TRAIL + (off>>9);
5606520Sfeldman 			m->m_off -= 2 * sizeof (u_short);
5616520Sfeldman 			m->m_len += 2 * sizeof (u_short);
5629745Ssam 			*mtod(m, u_short *) = ntohs((u_short)ETHERPUP_IPTYPE);
5639745Ssam 			*(mtod(m, u_short *) + 1) = ntohs((u_short)m->m_len);
5646520Sfeldman 			goto gottrailertype;
5656520Sfeldman 		}
5669745Ssam 		type = ETHERPUP_IPTYPE;
5676520Sfeldman 		off = 0;
5686520Sfeldman 		goto gottype;
5696520Sfeldman #endif
5706520Sfeldman 
57111574Ssam 	case AF_UNSPEC:
57211574Ssam 		ec = (struct ether_header *)dst->sa_data;
57316217Skarels 		edst = ec->ether_dhost;
57411574Ssam 		type = ec->ether_type;
57511574Ssam 		goto gottype;
57611574Ssam 
5776520Sfeldman 	default:
5786520Sfeldman 		printf("ec%d: can't handle af%d\n", ifp->if_unit,
5796520Sfeldman 			dst->sa_family);
5806520Sfeldman 		error = EAFNOSUPPORT;
5816520Sfeldman 		goto bad;
5826520Sfeldman 	}
5836520Sfeldman 
5846520Sfeldman gottrailertype:
5856520Sfeldman 	/*
5866520Sfeldman 	 * Packet to be sent as trailer: move first packet
5876520Sfeldman 	 * (control information) to end of chain.
5886520Sfeldman 	 */
5896520Sfeldman 	while (m->m_next)
5906520Sfeldman 		m = m->m_next;
5916520Sfeldman 	m->m_next = m0;
5926520Sfeldman 	m = m0->m_next;
5936520Sfeldman 	m0->m_next = 0;
5946520Sfeldman 	m0 = m;
5956520Sfeldman 
5966520Sfeldman gottype:
5976520Sfeldman 	/*
5986520Sfeldman 	 * Add local net header.  If no space in first mbuf,
5996520Sfeldman 	 * allocate another.
6006520Sfeldman 	 */
6016520Sfeldman 	if (m->m_off > MMAXOFF ||
6029745Ssam 	    MMINOFF + sizeof (struct ether_header) > m->m_off) {
6039648Ssam 		m = m_get(M_DONTWAIT, MT_HEADER);
6046520Sfeldman 		if (m == 0) {
6056520Sfeldman 			error = ENOBUFS;
6066520Sfeldman 			goto bad;
6076520Sfeldman 		}
6086520Sfeldman 		m->m_next = m0;
6096520Sfeldman 		m->m_off = MMINOFF;
6109745Ssam 		m->m_len = sizeof (struct ether_header);
6116520Sfeldman 	} else {
6129745Ssam 		m->m_off -= sizeof (struct ether_header);
6139745Ssam 		m->m_len += sizeof (struct ether_header);
6146520Sfeldman 	}
6159745Ssam 	ec = mtod(m, struct ether_header *);
61616217Skarels 	ec->ether_dhost = edst;
61716217Skarels 	ec->ether_shost = es->es_addr;
6189745Ssam 	ec->ether_type = htons((u_short)type);
6196520Sfeldman 
6206520Sfeldman 	/*
6216520Sfeldman 	 * Queue message on interface, and start output if interface
6226520Sfeldman 	 * not yet active.
6236520Sfeldman 	 */
6246520Sfeldman 	s = splimp();
6256520Sfeldman 	if (IF_QFULL(&ifp->if_snd)) {
6266520Sfeldman 		IF_DROP(&ifp->if_snd);
6276520Sfeldman 		error = ENOBUFS;
6286520Sfeldman 		goto qfull;
6296520Sfeldman 	}
6306520Sfeldman 	IF_ENQUEUE(&ifp->if_snd, m);
6316520Sfeldman 	if (es->es_oactive == 0)
6326520Sfeldman 		ecstart(ifp->if_unit);
6336520Sfeldman 	splx(s);
63412771Ssam 	return (mcopy ? looutput(&loif, mcopy, dst) : 0);
6357216Ssam 
6366520Sfeldman qfull:
6376520Sfeldman 	m0 = m;
6386520Sfeldman 	splx(s);
6396520Sfeldman bad:
6406520Sfeldman 	m_freem(m0);
64116207Skarels 	if (mcopy)
64216207Skarels 		m_freem(mcopy);
64312771Ssam 	return (error);
6446520Sfeldman }
6456520Sfeldman 
6466520Sfeldman /*
6479177Ssam  * Routine to copy from mbuf chain to transmit
6487216Ssam  * buffer in UNIBUS memory.
6499177Ssam  * If packet size is less than the minimum legal size,
6509177Ssam  * the buffer is expanded.  We probably should zero out the extra
6519177Ssam  * bytes for security, but that would slow things down.
6526520Sfeldman  */
6536520Sfeldman ecput(ecbuf, m)
6547216Ssam 	u_char *ecbuf;
6556520Sfeldman 	struct mbuf *m;
6566520Sfeldman {
6576520Sfeldman 	register struct mbuf *mp;
6587216Ssam 	register int off;
6597263Ssam 	u_char *bp;
6606520Sfeldman 
6617216Ssam 	for (off = 2048, mp = m; mp; mp = mp->m_next)
6627216Ssam 		off -= mp->m_len;
6639745Ssam 	if (2048 - off < ETHERMIN + sizeof (struct ether_header))
6649745Ssam 		off = 2048 - ETHERMIN - sizeof (struct ether_header);
6657216Ssam 	*(u_short *)ecbuf = off;
6667216Ssam 	bp = (u_char *)(ecbuf + off);
6677263Ssam 	for (mp = m; mp; mp = mp->m_next) {
6687263Ssam 		register unsigned len = mp->m_len;
6697263Ssam 		u_char *mcp;
6707216Ssam 
6717216Ssam 		if (len == 0)
6727216Ssam 			continue;
6737216Ssam 		mcp = mtod(mp, u_char *);
6747216Ssam 		if ((unsigned)bp & 01) {
6757032Swnj 			*bp++ = *mcp++;
6767216Ssam 			len--;
6777032Swnj 		}
6787263Ssam 		if (off = (len >> 1)) {
6797263Ssam 			register u_short *to, *from;
6807263Ssam 
6817263Ssam 			to = (u_short *)bp;
6827263Ssam 			from = (u_short *)mcp;
6837263Ssam 			do
6847263Ssam 				*to++ = *from++;
6857263Ssam 			while (--off > 0);
6867263Ssam 			bp = (u_char *)to,
6877263Ssam 			mcp = (u_char *)from;
6887032Swnj 		}
6897263Ssam 		if (len & 01)
6906520Sfeldman 			*bp++ = *mcp++;
6916520Sfeldman 	}
6927263Ssam 	m_freem(m);
6936520Sfeldman }
6946520Sfeldman 
6956520Sfeldman /*
6966520Sfeldman  * Routine to copy from UNIBUS memory into mbufs.
6976520Sfeldman  * Similar in spirit to if_rubaget.
6987032Swnj  *
6997032Swnj  * Warning: This makes the fairly safe assumption that
7007032Swnj  * mbufs have even lengths.
7016520Sfeldman  */
7026520Sfeldman struct mbuf *
7036520Sfeldman ecget(ecbuf, totlen, off0)
7047263Ssam 	u_char *ecbuf;
7056520Sfeldman 	int totlen, off0;
7066520Sfeldman {
7077263Ssam 	register struct mbuf *m;
7087263Ssam 	struct mbuf *top = 0, **mp = &top;
7097263Ssam 	register int off = off0, len;
7107263Ssam 	u_char *cp;
7116520Sfeldman 
7129745Ssam 	cp = ecbuf + ECRDOFF + sizeof (struct ether_header);
7136520Sfeldman 	while (totlen > 0) {
7147263Ssam 		register int words;
7157263Ssam 		u_char *mcp;
7167263Ssam 
7179648Ssam 		MGET(m, M_DONTWAIT, MT_DATA);
7186520Sfeldman 		if (m == 0)
7196520Sfeldman 			goto bad;
7206520Sfeldman 		if (off) {
7216520Sfeldman 			len = totlen - off;
7229745Ssam 			cp = ecbuf + ECRDOFF +
7239745Ssam 				sizeof (struct ether_header) + off;
7246520Sfeldman 		} else
7256520Sfeldman 			len = totlen;
7266520Sfeldman 		if (len >= CLBYTES) {
7276520Sfeldman 			struct mbuf *p;
7286520Sfeldman 
7296520Sfeldman 			MCLGET(p, 1);
7306520Sfeldman 			if (p != 0) {
7316520Sfeldman 				m->m_len = len = CLBYTES;
7326520Sfeldman 				m->m_off = (int)p - (int)m;
7336520Sfeldman 			} else {
7346520Sfeldman 				m->m_len = len = MIN(MLEN, len);
7356520Sfeldman 				m->m_off = MMINOFF;
7366520Sfeldman 			}
7376520Sfeldman 		} else {
7386520Sfeldman 			m->m_len = len = MIN(MLEN, len);
7396520Sfeldman 			m->m_off = MMINOFF;
7406520Sfeldman 		}
7417263Ssam 		mcp = mtod(m, u_char *);
7427263Ssam 		if (words = (len >> 1)) {
7437263Ssam 			register u_short *to, *from;
7447263Ssam 
7457263Ssam 			to = (u_short *)mcp;
7467263Ssam 			from = (u_short *)cp;
7477263Ssam 			do
7487263Ssam 				*to++ = *from++;
7497263Ssam 			while (--words > 0);
7507263Ssam 			mcp = (u_char *)to;
7517263Ssam 			cp = (u_char *)from;
7527032Swnj 		}
7537216Ssam 		if (len & 01)
7546520Sfeldman 			*mcp++ = *cp++;
7556520Sfeldman 		*mp = m;
7566520Sfeldman 		mp = &m->m_next;
7577263Ssam 		if (off == 0) {
7586520Sfeldman 			totlen -= len;
7597263Ssam 			continue;
7607263Ssam 		}
7617263Ssam 		off += len;
7627263Ssam 		if (off == totlen) {
7639745Ssam 			cp = ecbuf + ECRDOFF + sizeof (struct ether_header);
7647263Ssam 			off = 0;
7657263Ssam 			totlen = off0;
7667263Ssam 		}
7676520Sfeldman 	}
7686520Sfeldman 	return (top);
7696520Sfeldman bad:
7706520Sfeldman 	m_freem(top);
7716520Sfeldman 	return (0);
7726520Sfeldman }
77313055Ssam 
77413055Ssam /*
77513055Ssam  * Process an ioctl request.
77613055Ssam  */
77713055Ssam ecioctl(ifp, cmd, data)
77813055Ssam 	register struct ifnet *ifp;
77913055Ssam 	int cmd;
78013055Ssam 	caddr_t data;
78113055Ssam {
78213055Ssam 	register struct ifreq *ifr = (struct ifreq *)data;
78313055Ssam 	int s = splimp(), error = 0;
78413055Ssam 
78513055Ssam 	switch (cmd) {
78613055Ssam 
78713055Ssam 	case SIOCSIFADDR:
78813055Ssam 		if (ifp->if_flags & IFF_RUNNING)
78913055Ssam 			if_rtinit(ifp, -1);	/* delete previous route */
79013062Ssam 		ecsetaddr(ifp, (struct sockaddr_in *)&ifr->ifr_addr);
79113055Ssam 		ecinit(ifp->if_unit);
79213055Ssam 		break;
79313055Ssam 
79413055Ssam 	default:
79513055Ssam 		error = EINVAL;
79613055Ssam 	}
79713055Ssam 	splx(s);
79813055Ssam 	return (error);
79913055Ssam }
80013062Ssam 
80113062Ssam ecsetaddr(ifp, sin)
80213062Ssam 	register struct ifnet *ifp;
80313062Ssam 	register struct sockaddr_in *sin;
80413062Ssam {
80513062Ssam 
80613062Ssam 	ifp->if_addr = *(struct sockaddr *)sin;
80713062Ssam 	ifp->if_net = in_netof(sin->sin_addr);
80813062Ssam 	ifp->if_host[0] = in_lnaof(sin->sin_addr);
80913062Ssam 	sin = (struct sockaddr_in *)&ifp->if_broadaddr;
81013062Ssam 	sin->sin_family = AF_INET;
81113062Ssam 	sin->sin_addr = if_makeaddr(ifp->if_net, INADDR_ANY);
81213062Ssam 	ifp->if_flags |= IFF_BROADCAST;
81313062Ssam }
814