xref: /csrg-svn/sys/vax/if/if_ec.c (revision 19863)
1*19863Skarels /*	if_ec.c	6.7	85/05/01	*/
26520Sfeldman 
36520Sfeldman #include "ec.h"
46520Sfeldman 
56520Sfeldman /*
66520Sfeldman  * 3Com Ethernet Controller interface
76520Sfeldman  */
89795Ssam #include "../machine/pte.h"
96520Sfeldman 
1017112Sbloom #include "param.h"
1117112Sbloom #include "systm.h"
1217112Sbloom #include "mbuf.h"
1317112Sbloom #include "buf.h"
1417112Sbloom #include "protosw.h"
1517112Sbloom #include "socket.h"
1617112Sbloom #include "vmmac.h"
1717112Sbloom #include "ioctl.h"
1817112Sbloom #include "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"
25*19863Skarels #include "../netinet/in_var.h"
268417Swnj #include "../netinet/ip.h"
278417Swnj #include "../netinet/ip_var.h"
2811574Ssam #include "../netinet/if_ether.h"
298417Swnj #include "../netpup/pup.h"
306520Sfeldman 
318461Sroot #include "../vax/cpu.h"
328461Sroot #include "../vax/mtpr.h"
3317112Sbloom #include "if_ecreg.h"
3417112Sbloom #include "if_uba.h"
358461Sroot #include "../vaxuba/ubareg.h"
368461Sroot #include "../vaxuba/ubavar.h"
378461Sroot 
3817995Skarels #if CLSIZE == 2
3917995Skarels #define ECBUFSIZE	32		/* on-board memory, clusters */
4016749Sbloom #endif
4116749Sbloom 
4217995Skarels int	ecubamem(), ecprobe(), ecattach(), ecrint(), ecxint(), eccollide();
436520Sfeldman struct	uba_device *ecinfo[NEC];
446520Sfeldman u_short ecstd[] = { 0 };
456520Sfeldman struct	uba_driver ecdriver =
4617995Skarels 	{ ecprobe, 0, ecattach, 0, ecstd, "ec", ecinfo, 0, 0, 0, ecubamem };
476520Sfeldman 
4813055Ssam int	ecinit(),ecioctl(),ecoutput(),ecreset();
498773Sroot struct	mbuf *ecget();
506520Sfeldman 
516545Sfeldman extern struct ifnet loif;
526545Sfeldman 
536520Sfeldman /*
546520Sfeldman  * Ethernet software status per interface.
556520Sfeldman  *
566520Sfeldman  * Each interface is referenced by a network interface structure,
576520Sfeldman  * es_if, which the routing code uses to locate the interface.
586520Sfeldman  * This structure contains the output queue for the interface, its address, ...
596520Sfeldman  * We also have, for each interface, a UBA interface structure, which
606520Sfeldman  * contains information about the UNIBUS resources held by the interface:
616520Sfeldman  * map registers, buffered data paths, etc.  Information is cached in this
626520Sfeldman  * structure for use by the if_uba.c routines in running the interface
636520Sfeldman  * efficiently.
646520Sfeldman  */
656520Sfeldman struct	ec_softc {
6611574Ssam 	struct	arpcom es_ac;		/* common Ethernet structures */
6711574Ssam #define	es_if	es_ac.ac_if		/* network-visible interface */
6811574Ssam #define	es_addr	es_ac.ac_enaddr		/* hardware Ethernet address */
696520Sfeldman 	struct	ifuba es_ifuba;		/* UNIBUS resources */
706520Sfeldman 	short	es_mask;		/* mask for current output delay */
716520Sfeldman 	short	es_oactive;		/* is output active? */
728773Sroot 	u_char	*es_buf[16];		/* virtual addresses of buffers */
736520Sfeldman } ec_softc[NEC];
746520Sfeldman 
756520Sfeldman /*
7617995Skarels  * Configure on-board memory for an interface.
7717995Skarels  * Called from autoconfig and after a uba reset.
7817995Skarels  * The address of the memory on the uba is supplied in the device flags.
796520Sfeldman  */
8017995Skarels ecubamem(ui, uban)
8117995Skarels 	register struct uba_device *ui;
826520Sfeldman {
8317995Skarels 	register caddr_t ecbuf = (caddr_t) &umem[uban][ui->ui_flags];
8417995Skarels 	register struct ecdevice *addr = (struct ecdevice *)ui->ui_addr;
856520Sfeldman 
8617995Skarels 	/*
8717995Skarels 	 * Make sure csr is there (we run before ecprobe).
8817995Skarels 	 */
8917995Skarels 	if (badaddr((caddr_t)addr, 2))
9017995Skarels 		return (-1);
9117995Skarels #if VAX780
9217995Skarels 	if (cpu == VAX_780 && uba_hd[uban].uh_uba->uba_sr) {
9317995Skarels 		uba_hd[uban].uh_uba->uba_sr = uba_hd[uban].uh_uba->uba_sr;
9417995Skarels 		return (-1);
9517995Skarels 	}
966520Sfeldman #endif
976520Sfeldman 	/*
986637Sfeldman 	 * Make sure memory is turned on
996637Sfeldman 	 */
1006637Sfeldman 	addr->ec_rcr = EC_AROM;
1016637Sfeldman 	/*
10217995Skarels 	 * Tell the system that the board has memory here, so it won't
10317995Skarels 	 * attempt to allocate the addresses later.
1047470Sfeldman 	 */
10517995Skarels 	if (ubamem(uban, ui->ui_flags, ECBUFSIZE*CLSIZE, 1) == 0) {
10617995Skarels 		printf("ec%d: cannot reserve uba addresses\n", ui->ui_unit);
10717995Skarels 		addr->ec_rcr = EC_MDISAB;	/* disable memory */
10817995Skarels 		return (-1);
10917995Skarels 	}
1107470Sfeldman 	/*
1116520Sfeldman 	 * Check for existence of buffers on Unibus.
1126520Sfeldman 	 */
1138773Sroot 	if (badaddr((caddr_t)ecbuf, 2)) {
11417995Skarels bad:
11517995Skarels 		printf("ec%d: buffer mem not found\n", ui->ui_unit);
11617995Skarels 		(void) ubamem(uban, ui->ui_flags, ECBUFSIZE*2, 0);
1177470Sfeldman 		addr->ec_rcr = EC_MDISAB;	/* disable memory */
11817995Skarels 		return (-1);
1196520Sfeldman 	}
1207470Sfeldman #if VAX780
12117995Skarels 	if (cpu == VAX_780 && uba_hd[uban].uh_uba->uba_sr) {
12217995Skarels 		uba_hd[uban].uh_uba->uba_sr = uba_hd[uban].uh_uba->uba_sr;
12317995Skarels 		goto bad;
1247470Sfeldman 	}
1257470Sfeldman #endif
12617995Skarels 	if (ui->ui_alive == 0)		/* Only printf from autoconfig */
12717995Skarels 		printf("ec%d: mem %x-%x\n", ui->ui_unit,
12817995Skarels 			ui->ui_flags, ui->ui_flags + ECBUFSIZE*CLBYTES - 1);
12917995Skarels 	ui->ui_type = 1;		/* Memory on, allocated */
13017995Skarels 	return (0);
13117995Skarels }
1326520Sfeldman 
13317995Skarels /*
13417995Skarels  * Do output DMA to determine interface presence and
13517995Skarels  * interrupt vector.  DMA is too short to disturb other hosts.
13617995Skarels  */
13717995Skarels ecprobe(reg, ui)
13817995Skarels 	caddr_t reg;
13917995Skarels 	struct uba_device *ui;
14017995Skarels {
14117995Skarels 	register int br, cvec;		/* r11, r10 value-result */
14217995Skarels 	register struct ecdevice *addr = (struct ecdevice *)reg;
14317995Skarels 	register caddr_t ecbuf = (caddr_t) &umem[ui->ui_ubanum][ui->ui_flags];
14417995Skarels 
14517995Skarels #ifdef lint
14617995Skarels 	br = 0; cvec = br; br = cvec;
14717995Skarels 	ecrint(0); ecxint(0); eccollide(0);
14817995Skarels #endif
14917995Skarels 
1506520Sfeldman 	/*
15117995Skarels 	 * Check that buffer memory was found and enabled.
1526520Sfeldman 	 */
15317995Skarels 	if (ui->ui_type == 0)
15417995Skarels 		return(0);
1556520Sfeldman 	/*
1566520Sfeldman 	 * Make a one byte packet in what should be buffer #0.
15717995Skarels 	 * Submit it for sending.  This should cause an xmit interrupt.
1586520Sfeldman 	 * The xmit interrupt vector is 8 bytes after the receive vector,
1596520Sfeldman 	 * so adjust for this before returning.
1606520Sfeldman 	 */
1616520Sfeldman 	*(u_short *)ecbuf = (u_short) 03777;
1626520Sfeldman 	ecbuf[03777] = '\0';
1636520Sfeldman 	addr->ec_xcr = EC_XINTEN|EC_XWBN;
1646520Sfeldman 	DELAY(100000);
1656520Sfeldman 	addr->ec_xcr = EC_XCLR;
1667216Ssam 	if (cvec > 0 && cvec != 0x200) {
1677470Sfeldman 		if (cvec & 04) {	/* collision interrupt */
1687470Sfeldman 			cvec -= 04;
1697470Sfeldman 			br += 1;		/* rcv is collision + 1 */
1707470Sfeldman 		} else {		/* xmit interrupt */
1717470Sfeldman 			cvec -= 010;
1727470Sfeldman 			br += 2;		/* rcv is xmit + 2 */
1737470Sfeldman 		}
1747216Ssam 	}
1756520Sfeldman 	return (1);
1766520Sfeldman }
1776520Sfeldman 
1786520Sfeldman /*
1796520Sfeldman  * Interface exists: make available by filling in network interface
1806520Sfeldman  * record.  System will initialize the interface when it is ready
1816520Sfeldman  * to accept packets.
1826520Sfeldman  */
1836520Sfeldman ecattach(ui)
1846520Sfeldman 	struct uba_device *ui;
1856520Sfeldman {
1867216Ssam 	struct ec_softc *es = &ec_softc[ui->ui_unit];
1877216Ssam 	register struct ifnet *ifp = &es->es_if;
1886520Sfeldman 	register struct ecdevice *addr = (struct ecdevice *)ui->ui_addr;
1897216Ssam 	int i, j;
1907216Ssam 	u_char *cp;
1916520Sfeldman 
1927216Ssam 	ifp->if_unit = ui->ui_unit;
1937216Ssam 	ifp->if_name = "ec";
1949745Ssam 	ifp->if_mtu = ETHERMTU;
1956520Sfeldman 
1966520Sfeldman 	/*
1977216Ssam 	 * Read the ethernet address off the board, one nibble at a time.
1986520Sfeldman 	 */
1996520Sfeldman 	addr->ec_xcr = EC_UECLR;
2006520Sfeldman 	addr->ec_rcr = EC_AROM;
201*19863Skarels 	cp = es->es_addr;
2027216Ssam #define	NEXTBIT	addr->ec_rcr = EC_AROM|EC_ASTEP; addr->ec_rcr = EC_AROM
20316217Skarels 	for (i=0; i < sizeof (es->es_addr); i++) {
2046520Sfeldman 		*cp = 0;
2056520Sfeldman 		for (j=0; j<=4; j+=4) {
2066520Sfeldman 			*cp |= ((addr->ec_rcr >> 8) & 0xf) << j;
2077216Ssam 			NEXTBIT; NEXTBIT; NEXTBIT; NEXTBIT;
2086520Sfeldman 		}
2096520Sfeldman 		cp++;
2106520Sfeldman 	}
2117216Ssam 	ifp->if_init = ecinit;
21213055Ssam 	ifp->if_ioctl = ecioctl;
2137216Ssam 	ifp->if_output = ecoutput;
2148977Sroot 	ifp->if_reset = ecreset;
215*19863Skarels 	ifp->if_flags = IFF_BROADCAST;
2166520Sfeldman 	for (i=0; i<16; i++)
21716749Sbloom 		es->es_buf[i]
21817995Skarels 		    = (u_char *)&umem[ui->ui_ubanum][ui->ui_flags + 2048*i];
2197216Ssam 	if_attach(ifp);
2206520Sfeldman }
2216520Sfeldman 
2226520Sfeldman /*
2236520Sfeldman  * Reset of interface after UNIBUS reset.
2246520Sfeldman  * If interface is on specified uba, reset its state.
2256520Sfeldman  */
2266520Sfeldman ecreset(unit, uban)
2276520Sfeldman 	int unit, uban;
2286520Sfeldman {
2296520Sfeldman 	register struct uba_device *ui;
2306520Sfeldman 
2316520Sfeldman 	if (unit >= NEC || (ui = ecinfo[unit]) == 0 || ui->ui_alive == 0 ||
2326520Sfeldman 	    ui->ui_ubanum != uban)
2336520Sfeldman 		return;
2346520Sfeldman 	printf(" ec%d", unit);
23516207Skarels 	ec_softc[unit].es_if.if_flags &= ~IFF_RUNNING;
2366520Sfeldman 	ecinit(unit);
2376520Sfeldman }
2386520Sfeldman 
2396520Sfeldman /*
2406520Sfeldman  * Initialization of interface; clear recorded pending
2416520Sfeldman  * operations, and reinitialize UNIBUS usage.
2426520Sfeldman  */
2436520Sfeldman ecinit(unit)
2446520Sfeldman 	int unit;
2456520Sfeldman {
2467216Ssam 	struct ec_softc *es = &ec_softc[unit];
2477216Ssam 	struct ecdevice *addr;
24811574Ssam 	register struct ifnet *ifp = &es->es_if;
24913055Ssam 	int i, s;
2506520Sfeldman 
251*19863Skarels 	/* not yet, if address still unknown */
252*19863Skarels 	if (ifp->if_addrlist == (struct ifaddr *)0)
25311574Ssam 		return;
25411574Ssam 
2556520Sfeldman 	/*
2567217Sfeldman 	 * Hang receive buffers and start any pending writes.
2576637Sfeldman 	 * Writing into the rcr also makes sure the memory
2586637Sfeldman 	 * is turned on.
2596520Sfeldman 	 */
260*19863Skarels 	if ((ifp->if_flags & IFF_RUNNING) == 0) {
26113055Ssam 		addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
26213055Ssam 		s = splimp();
26313055Ssam 		for (i = ECRHBF; i >= ECRLBF; i--)
26413055Ssam 			addr->ec_rcr = EC_READ | i;
26513055Ssam 		es->es_oactive = 0;
26613055Ssam 		es->es_mask = ~0;
267*19863Skarels 		es->es_if.if_flags |= IFF_RUNNING;
26813055Ssam 		if (es->es_if.if_snd.ifq_head)
26913055Ssam 			ecstart(unit);
27013055Ssam 		splx(s);
27113055Ssam 	}
2726520Sfeldman }
2736520Sfeldman 
2746520Sfeldman /*
2756520Sfeldman  * Start or restart output on interface.
2766520Sfeldman  * If interface is already active, then this is a retransmit
2776545Sfeldman  * after a collision, and just restuff registers.
2786520Sfeldman  * If interface is not already active, get another datagram
2796520Sfeldman  * to send off of the interface queue, and map it to the interface
2806520Sfeldman  * before starting the output.
2816520Sfeldman  */
28217995Skarels ecstart(unit)
2836520Sfeldman {
2847216Ssam 	struct ec_softc *es = &ec_softc[unit];
2857216Ssam 	struct ecdevice *addr;
2866520Sfeldman 	struct mbuf *m;
2876520Sfeldman 
2886520Sfeldman 	if (es->es_oactive)
2896520Sfeldman 		goto restart;
2906520Sfeldman 
2916520Sfeldman 	IF_DEQUEUE(&es->es_if.if_snd, m);
2926520Sfeldman 	if (m == 0) {
2936520Sfeldman 		es->es_oactive = 0;
2946520Sfeldman 		return;
2956520Sfeldman 	}
2966520Sfeldman 	ecput(es->es_buf[ECTBF], m);
2976520Sfeldman 
2986520Sfeldman restart:
2997216Ssam 	addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
3006520Sfeldman 	addr->ec_xcr = EC_WRITE|ECTBF;
3016520Sfeldman 	es->es_oactive = 1;
3026520Sfeldman }
3036520Sfeldman 
3046520Sfeldman /*
3056520Sfeldman  * Ethernet interface transmitter interrupt.
3066520Sfeldman  * Start another output if more data to send.
3076520Sfeldman  */
3086520Sfeldman ecxint(unit)
3096520Sfeldman 	int unit;
3106520Sfeldman {
3116520Sfeldman 	register struct ec_softc *es = &ec_softc[unit];
3127216Ssam 	register struct ecdevice *addr =
3137216Ssam 		(struct ecdevice *)ecinfo[unit]->ui_addr;
3146520Sfeldman 
3156520Sfeldman 	if (es->es_oactive == 0)
3166520Sfeldman 		return;
3177216Ssam 	if ((addr->ec_xcr&EC_XDONE) == 0 || (addr->ec_xcr&EC_XBN) != ECTBF) {
3187216Ssam 		printf("ec%d: stray xmit interrupt, xcr=%b\n", unit,
3197216Ssam 			addr->ec_xcr, EC_XBITS);
3207216Ssam 		es->es_oactive = 0;
3217216Ssam 		addr->ec_xcr = EC_XCLR;
3227216Ssam 		return;
3237216Ssam 	}
3246520Sfeldman 	es->es_if.if_opackets++;
3256520Sfeldman 	es->es_oactive = 0;
3266520Sfeldman 	es->es_mask = ~0;
3276520Sfeldman 	addr->ec_xcr = EC_XCLR;
3287216Ssam 	if (es->es_if.if_snd.ifq_head)
3297216Ssam 		ecstart(unit);
3306520Sfeldman }
3316520Sfeldman 
3326520Sfeldman /*
3336520Sfeldman  * Collision on ethernet interface.  Do exponential
3346520Sfeldman  * backoff, and retransmit.  If have backed off all
3356520Sfeldman  * the way print warning diagnostic, and drop packet.
3366520Sfeldman  */
3376520Sfeldman eccollide(unit)
3386520Sfeldman 	int unit;
3396520Sfeldman {
3406520Sfeldman 	struct ec_softc *es = &ec_softc[unit];
3416520Sfeldman 
3426520Sfeldman 	es->es_if.if_collisions++;
3437216Ssam 	if (es->es_oactive)
3447216Ssam 		ecdocoll(unit);
3456520Sfeldman }
3466520Sfeldman 
3476520Sfeldman ecdocoll(unit)
3486520Sfeldman 	int unit;
3496520Sfeldman {
3506520Sfeldman 	register struct ec_softc *es = &ec_softc[unit];
3516545Sfeldman 	register struct ecdevice *addr =
3526545Sfeldman 	    (struct ecdevice *)ecinfo[unit]->ui_addr;
3536545Sfeldman 	register i;
3546545Sfeldman 	int delay;
3556520Sfeldman 
3566520Sfeldman 	/*
3576520Sfeldman 	 * Es_mask is a 16 bit number with n low zero bits, with
3586520Sfeldman 	 * n the number of backoffs.  When es_mask is 0 we have
3596520Sfeldman 	 * backed off 16 times, and give up.
3606520Sfeldman 	 */
3616520Sfeldman 	if (es->es_mask == 0) {
3626545Sfeldman 		es->es_if.if_oerrors++;
3636520Sfeldman 		printf("ec%d: send error\n", unit);
3646520Sfeldman 		/*
3656545Sfeldman 		 * Reset interface, then requeue rcv buffers.
3666545Sfeldman 		 * Some incoming packets may be lost, but that
3676545Sfeldman 		 * can't be helped.
3686520Sfeldman 		 */
3696545Sfeldman 		addr->ec_xcr = EC_UECLR;
3706545Sfeldman 		for (i=ECRHBF; i>=ECRLBF; i--)
3716545Sfeldman 			addr->ec_rcr = EC_READ|i;
3726545Sfeldman 		/*
3736545Sfeldman 		 * Reset and transmit next packet (if any).
3746545Sfeldman 		 */
3756545Sfeldman 		es->es_oactive = 0;
3766545Sfeldman 		es->es_mask = ~0;
3776545Sfeldman 		if (es->es_if.if_snd.ifq_head)
3786545Sfeldman 			ecstart(unit);
3796520Sfeldman 		return;
3806520Sfeldman 	}
3816520Sfeldman 	/*
3826545Sfeldman 	 * Do exponential backoff.  Compute delay based on low bits
3836545Sfeldman 	 * of the interval timer.  Then delay for that number of
3846545Sfeldman 	 * slot times.  A slot time is 51.2 microseconds (rounded to 51).
3856545Sfeldman 	 * This does not take into account the time already used to
3866545Sfeldman 	 * process the interrupt.
3876520Sfeldman 	 */
3886520Sfeldman 	es->es_mask <<= 1;
3896545Sfeldman 	delay = mfpr(ICR) &~ es->es_mask;
3906545Sfeldman 	DELAY(delay * 51);
3916520Sfeldman 	/*
3926545Sfeldman 	 * Clear the controller's collision flag, thus enabling retransmit.
3936520Sfeldman 	 */
3947470Sfeldman 	addr->ec_xcr = EC_CLEAR;
3956520Sfeldman }
3966520Sfeldman 
3976520Sfeldman /*
3986520Sfeldman  * Ethernet interface receiver interrupt.
3996520Sfeldman  * If input error just drop packet.
4006520Sfeldman  * Otherwise purge input buffered data path and examine
4016520Sfeldman  * packet to determine type.  If can't determine length
4026520Sfeldman  * from type, then have to drop packet.  Othewise decapsulate
4036520Sfeldman  * packet based on type and pass to type specific higher-level
4046520Sfeldman  * input routine.
4056520Sfeldman  */
4066520Sfeldman ecrint(unit)
4076520Sfeldman 	int unit;
4086520Sfeldman {
4096520Sfeldman 	struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
4106520Sfeldman 
4116520Sfeldman 	while (addr->ec_rcr & EC_RDONE)
4126520Sfeldman 		ecread(unit);
4136520Sfeldman }
4146520Sfeldman 
4156520Sfeldman ecread(unit)
4166520Sfeldman 	int unit;
4176520Sfeldman {
4186520Sfeldman 	register struct ec_softc *es = &ec_softc[unit];
4196520Sfeldman 	struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
4209745Ssam 	register struct ether_header *ec;
4216520Sfeldman     	struct mbuf *m;
4228773Sroot 	int len, off, resid, ecoff, rbuf;
4236520Sfeldman 	register struct ifqueue *inq;
4248773Sroot 	u_char *ecbuf;
4256520Sfeldman 
4266520Sfeldman 	es->es_if.if_ipackets++;
4278773Sroot 	rbuf = addr->ec_rcr & EC_RBN;
4288773Sroot 	if (rbuf < ECRLBF || rbuf > ECRHBF)
4296520Sfeldman 		panic("ecrint");
4308773Sroot 	ecbuf = es->es_buf[rbuf];
4316520Sfeldman 	ecoff = *(short *)ecbuf;
4326545Sfeldman 	if (ecoff <= ECRDOFF || ecoff > 2046) {
4336520Sfeldman 		es->es_if.if_ierrors++;
4346520Sfeldman #ifdef notdef
4356520Sfeldman 		if (es->es_if.if_ierrors % 100 == 0)
4366520Sfeldman 			printf("ec%d: += 100 input errors\n", unit);
4376520Sfeldman #endif
4386520Sfeldman 		goto setup;
4396520Sfeldman 	}
4406520Sfeldman 
4416520Sfeldman 	/*
4426520Sfeldman 	 * Get input data length.
4436520Sfeldman 	 * Get pointer to ethernet header (in input buffer).
444*19863Skarels 	 * Deal with trailer protocol: if type is trailer type
4456520Sfeldman 	 * get true type from first 16-bit word past data.
4466520Sfeldman 	 * Remember that type was trailer by setting off.
4476520Sfeldman 	 */
4489745Ssam 	len = ecoff - ECRDOFF - sizeof (struct ether_header);
4499745Ssam 	ec = (struct ether_header *)(ecbuf + ECRDOFF);
4509745Ssam 	ec->ether_type = ntohs((u_short)ec->ether_type);
4516520Sfeldman #define	ecdataaddr(ec, off, type)	((type)(((caddr_t)((ec)+1)+(off))))
452*19863Skarels 	if (ec->ether_type >= ETHERTYPE_TRAIL &&
453*19863Skarels 	    ec->ether_type < ETHERTYPE_TRAIL+ETHERTYPE_NTRAILER) {
454*19863Skarels 		off = (ec->ether_type - ETHERTYPE_TRAIL) * 512;
4559745Ssam 		if (off >= ETHERMTU)
4566520Sfeldman 			goto setup;		/* sanity */
4579745Ssam 		ec->ether_type = ntohs(*ecdataaddr(ec, off, u_short *));
4589745Ssam 		resid = ntohs(*(ecdataaddr(ec, off+2, u_short *)));
4596520Sfeldman 		if (off + resid > len)
4606520Sfeldman 			goto setup;		/* sanity */
4616520Sfeldman 		len = off + resid;
4626520Sfeldman 	} else
4636520Sfeldman 		off = 0;
4646520Sfeldman 	if (len == 0)
4656520Sfeldman 		goto setup;
4666520Sfeldman 
4676520Sfeldman 	/*
4686520Sfeldman 	 * Pull packet off interface.  Off is nonzero if packet
4696520Sfeldman 	 * has trailing header; ecget will then force this header
4706520Sfeldman 	 * information to be at the front, but we still have to drop
4716520Sfeldman 	 * the type and length which are at the front of any trailer data.
4726520Sfeldman 	 */
4736520Sfeldman 	m = ecget(ecbuf, len, off);
4746520Sfeldman 	if (m == 0)
4756520Sfeldman 		goto setup;
4766520Sfeldman 	if (off) {
4776520Sfeldman 		m->m_off += 2 * sizeof (u_short);
4786520Sfeldman 		m->m_len -= 2 * sizeof (u_short);
4796520Sfeldman 	}
4809745Ssam 	switch (ec->ether_type) {
4816520Sfeldman 
4826520Sfeldman #ifdef INET
483*19863Skarels 	case ETHERTYPE_IP:
4846520Sfeldman 		schednetisr(NETISR_IP);
4856520Sfeldman 		inq = &ipintrq;
4866520Sfeldman 		break;
48711574Ssam 
488*19863Skarels 	case ETHERTYPE_ARP:
48911574Ssam 		arpinput(&es->es_ac, m);
49013987Ssam 		goto setup;
4916520Sfeldman #endif
4926520Sfeldman 	default:
4936520Sfeldman 		m_freem(m);
4946520Sfeldman 		goto setup;
4956520Sfeldman 	}
4966520Sfeldman 
4976520Sfeldman 	if (IF_QFULL(inq)) {
4986520Sfeldman 		IF_DROP(inq);
4996520Sfeldman 		m_freem(m);
5007216Ssam 		goto setup;
5017216Ssam 	}
5027216Ssam 	IF_ENQUEUE(inq, m);
5036520Sfeldman 
5046520Sfeldman setup:
5056520Sfeldman 	/*
5066520Sfeldman 	 * Reset for next packet.
5076520Sfeldman 	 */
5088773Sroot 	addr->ec_rcr = EC_READ|EC_RCLR|rbuf;
5096520Sfeldman }
5106520Sfeldman 
5116520Sfeldman /*
5126520Sfeldman  * Ethernet output routine.
5136520Sfeldman  * Encapsulate a packet of type family for the local net.
5146520Sfeldman  * Use trailer local net encapsulation if enough data in first
5156520Sfeldman  * packet leaves a multiple of 512 bytes of data in remainder.
5166545Sfeldman  * If destination is this address or broadcast, send packet to
5176545Sfeldman  * loop device to kludge around the fact that 3com interfaces can't
5186545Sfeldman  * talk to themselves.
5196520Sfeldman  */
5206520Sfeldman ecoutput(ifp, m0, dst)
5216520Sfeldman 	struct ifnet *ifp;
5226520Sfeldman 	struct mbuf *m0;
5236520Sfeldman 	struct sockaddr *dst;
5246520Sfeldman {
52511574Ssam 	int type, s, error;
526*19863Skarels  	u_char edst[6];
52711574Ssam 	struct in_addr idst;
5286520Sfeldman 	register struct ec_softc *es = &ec_softc[ifp->if_unit];
5296520Sfeldman 	register struct mbuf *m = m0;
5309745Ssam 	register struct ether_header *ec;
53112771Ssam 	register int off;
53211574Ssam 	struct mbuf *mcopy = (struct mbuf *)0;
5336520Sfeldman 
5346520Sfeldman 	switch (dst->sa_family) {
5356520Sfeldman 
5366520Sfeldman #ifdef INET
5376520Sfeldman 	case AF_INET:
53811574Ssam 		idst = ((struct sockaddr_in *)dst)->sin_addr;
539*19863Skarels 		if (!arpresolve(&es->es_ac, m, &idst, edst))
54011574Ssam 			return (0);	/* if not yet resolved */
541*19863Skarels 		if (!bcmp((caddr_t)edst, (caddr_t)etherbroadcastaddr,
542*19863Skarels 		    sizeof(edst)))
5438838Sroot 			mcopy = m_copy(m, 0, (int)M_COPYALL);
5446520Sfeldman 		off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len;
54513055Ssam 		/* need per host negotiation */
54613055Ssam 		if ((ifp->if_flags & IFF_NOTRAILERS) == 0)
5476520Sfeldman 		if (off > 0 && (off & 0x1ff) == 0 &&
5486520Sfeldman 		    m->m_off >= MMINOFF + 2 * sizeof (u_short)) {
549*19863Skarels 			type = ETHERTYPE_TRAIL + (off>>9);
5506520Sfeldman 			m->m_off -= 2 * sizeof (u_short);
5516520Sfeldman 			m->m_len += 2 * sizeof (u_short);
552*19863Skarels 			*mtod(m, u_short *) = ntohs((u_short)ETHERTYPE_IP);
5539745Ssam 			*(mtod(m, u_short *) + 1) = ntohs((u_short)m->m_len);
5546520Sfeldman 			goto gottrailertype;
5556520Sfeldman 		}
556*19863Skarels 		type = ETHERTYPE_IP;
5576520Sfeldman 		off = 0;
5586520Sfeldman 		goto gottype;
5596520Sfeldman #endif
5606520Sfeldman 
56111574Ssam 	case AF_UNSPEC:
56211574Ssam 		ec = (struct ether_header *)dst->sa_data;
563*19863Skarels  		bcopy((caddr_t)ec->ether_dhost, (caddr_t)edst, sizeof (edst));
56411574Ssam 		type = ec->ether_type;
56511574Ssam 		goto gottype;
56611574Ssam 
5676520Sfeldman 	default:
5686520Sfeldman 		printf("ec%d: can't handle af%d\n", ifp->if_unit,
5696520Sfeldman 			dst->sa_family);
5706520Sfeldman 		error = EAFNOSUPPORT;
5716520Sfeldman 		goto bad;
5726520Sfeldman 	}
5736520Sfeldman 
5746520Sfeldman gottrailertype:
5756520Sfeldman 	/*
5766520Sfeldman 	 * Packet to be sent as trailer: move first packet
5776520Sfeldman 	 * (control information) to end of chain.
5786520Sfeldman 	 */
5796520Sfeldman 	while (m->m_next)
5806520Sfeldman 		m = m->m_next;
5816520Sfeldman 	m->m_next = m0;
5826520Sfeldman 	m = m0->m_next;
5836520Sfeldman 	m0->m_next = 0;
5846520Sfeldman 	m0 = m;
5856520Sfeldman 
5866520Sfeldman gottype:
5876520Sfeldman 	/*
5886520Sfeldman 	 * Add local net header.  If no space in first mbuf,
5896520Sfeldman 	 * allocate another.
5906520Sfeldman 	 */
5916520Sfeldman 	if (m->m_off > MMAXOFF ||
5929745Ssam 	    MMINOFF + sizeof (struct ether_header) > m->m_off) {
5939648Ssam 		m = m_get(M_DONTWAIT, MT_HEADER);
5946520Sfeldman 		if (m == 0) {
5956520Sfeldman 			error = ENOBUFS;
5966520Sfeldman 			goto bad;
5976520Sfeldman 		}
5986520Sfeldman 		m->m_next = m0;
5996520Sfeldman 		m->m_off = MMINOFF;
6009745Ssam 		m->m_len = sizeof (struct ether_header);
6016520Sfeldman 	} else {
6029745Ssam 		m->m_off -= sizeof (struct ether_header);
6039745Ssam 		m->m_len += sizeof (struct ether_header);
6046520Sfeldman 	}
6059745Ssam 	ec = mtod(m, struct ether_header *);
606*19863Skarels  	bcopy((caddr_t)edst, (caddr_t)ec->ether_dhost, sizeof (edst));
607*19863Skarels 	bcopy((caddr_t)es->es_addr, (caddr_t)ec->ether_shost,
608*19863Skarels 	    sizeof(ec->ether_shost));
6099745Ssam 	ec->ether_type = htons((u_short)type);
6106520Sfeldman 
6116520Sfeldman 	/*
6126520Sfeldman 	 * Queue message on interface, and start output if interface
6136520Sfeldman 	 * not yet active.
6146520Sfeldman 	 */
6156520Sfeldman 	s = splimp();
6166520Sfeldman 	if (IF_QFULL(&ifp->if_snd)) {
6176520Sfeldman 		IF_DROP(&ifp->if_snd);
6186520Sfeldman 		error = ENOBUFS;
6196520Sfeldman 		goto qfull;
6206520Sfeldman 	}
6216520Sfeldman 	IF_ENQUEUE(&ifp->if_snd, m);
6226520Sfeldman 	if (es->es_oactive == 0)
6236520Sfeldman 		ecstart(ifp->if_unit);
6246520Sfeldman 	splx(s);
62512771Ssam 	return (mcopy ? looutput(&loif, mcopy, dst) : 0);
6267216Ssam 
6276520Sfeldman qfull:
6286520Sfeldman 	m0 = m;
6296520Sfeldman 	splx(s);
6306520Sfeldman bad:
6316520Sfeldman 	m_freem(m0);
63216207Skarels 	if (mcopy)
63316207Skarels 		m_freem(mcopy);
63412771Ssam 	return (error);
6356520Sfeldman }
6366520Sfeldman 
6376520Sfeldman /*
6389177Ssam  * Routine to copy from mbuf chain to transmit
6397216Ssam  * buffer in UNIBUS memory.
6409177Ssam  * If packet size is less than the minimum legal size,
6419177Ssam  * the buffer is expanded.  We probably should zero out the extra
6429177Ssam  * bytes for security, but that would slow things down.
6436520Sfeldman  */
6446520Sfeldman ecput(ecbuf, m)
6457216Ssam 	u_char *ecbuf;
6466520Sfeldman 	struct mbuf *m;
6476520Sfeldman {
6486520Sfeldman 	register struct mbuf *mp;
6497216Ssam 	register int off;
6507263Ssam 	u_char *bp;
6516520Sfeldman 
6527216Ssam 	for (off = 2048, mp = m; mp; mp = mp->m_next)
6537216Ssam 		off -= mp->m_len;
6549745Ssam 	if (2048 - off < ETHERMIN + sizeof (struct ether_header))
6559745Ssam 		off = 2048 - ETHERMIN - sizeof (struct ether_header);
6567216Ssam 	*(u_short *)ecbuf = off;
6577216Ssam 	bp = (u_char *)(ecbuf + off);
6587263Ssam 	for (mp = m; mp; mp = mp->m_next) {
6597263Ssam 		register unsigned len = mp->m_len;
6607263Ssam 		u_char *mcp;
6617216Ssam 
6627216Ssam 		if (len == 0)
6637216Ssam 			continue;
6647216Ssam 		mcp = mtod(mp, u_char *);
6657216Ssam 		if ((unsigned)bp & 01) {
6667032Swnj 			*bp++ = *mcp++;
6677216Ssam 			len--;
6687032Swnj 		}
6697263Ssam 		if (off = (len >> 1)) {
6707263Ssam 			register u_short *to, *from;
6717263Ssam 
6727263Ssam 			to = (u_short *)bp;
6737263Ssam 			from = (u_short *)mcp;
6747263Ssam 			do
6757263Ssam 				*to++ = *from++;
6767263Ssam 			while (--off > 0);
6777263Ssam 			bp = (u_char *)to,
6787263Ssam 			mcp = (u_char *)from;
6797032Swnj 		}
6807263Ssam 		if (len & 01)
6816520Sfeldman 			*bp++ = *mcp++;
6826520Sfeldman 	}
6837263Ssam 	m_freem(m);
6846520Sfeldman }
6856520Sfeldman 
6866520Sfeldman /*
6876520Sfeldman  * Routine to copy from UNIBUS memory into mbufs.
6886520Sfeldman  * Similar in spirit to if_rubaget.
6897032Swnj  *
6907032Swnj  * Warning: This makes the fairly safe assumption that
6917032Swnj  * mbufs have even lengths.
6926520Sfeldman  */
6936520Sfeldman struct mbuf *
6946520Sfeldman ecget(ecbuf, totlen, off0)
6957263Ssam 	u_char *ecbuf;
6966520Sfeldman 	int totlen, off0;
6976520Sfeldman {
6987263Ssam 	register struct mbuf *m;
6997263Ssam 	struct mbuf *top = 0, **mp = &top;
7007263Ssam 	register int off = off0, len;
7017263Ssam 	u_char *cp;
7026520Sfeldman 
7039745Ssam 	cp = ecbuf + ECRDOFF + sizeof (struct ether_header);
7046520Sfeldman 	while (totlen > 0) {
7057263Ssam 		register int words;
7067263Ssam 		u_char *mcp;
7077263Ssam 
7089648Ssam 		MGET(m, M_DONTWAIT, MT_DATA);
7096520Sfeldman 		if (m == 0)
7106520Sfeldman 			goto bad;
7116520Sfeldman 		if (off) {
7126520Sfeldman 			len = totlen - off;
7139745Ssam 			cp = ecbuf + ECRDOFF +
7149745Ssam 				sizeof (struct ether_header) + off;
7156520Sfeldman 		} else
7166520Sfeldman 			len = totlen;
7176520Sfeldman 		if (len >= CLBYTES) {
7186520Sfeldman 			struct mbuf *p;
7196520Sfeldman 
7206520Sfeldman 			MCLGET(p, 1);
7216520Sfeldman 			if (p != 0) {
7226520Sfeldman 				m->m_len = len = CLBYTES;
7236520Sfeldman 				m->m_off = (int)p - (int)m;
7246520Sfeldman 			} else {
7256520Sfeldman 				m->m_len = len = MIN(MLEN, len);
7266520Sfeldman 				m->m_off = MMINOFF;
7276520Sfeldman 			}
7286520Sfeldman 		} else {
7296520Sfeldman 			m->m_len = len = MIN(MLEN, len);
7306520Sfeldman 			m->m_off = MMINOFF;
7316520Sfeldman 		}
7327263Ssam 		mcp = mtod(m, u_char *);
7337263Ssam 		if (words = (len >> 1)) {
7347263Ssam 			register u_short *to, *from;
7357263Ssam 
7367263Ssam 			to = (u_short *)mcp;
7377263Ssam 			from = (u_short *)cp;
7387263Ssam 			do
7397263Ssam 				*to++ = *from++;
7407263Ssam 			while (--words > 0);
7417263Ssam 			mcp = (u_char *)to;
7427263Ssam 			cp = (u_char *)from;
7437032Swnj 		}
7447216Ssam 		if (len & 01)
7456520Sfeldman 			*mcp++ = *cp++;
7466520Sfeldman 		*mp = m;
7476520Sfeldman 		mp = &m->m_next;
7487263Ssam 		if (off == 0) {
7496520Sfeldman 			totlen -= len;
7507263Ssam 			continue;
7517263Ssam 		}
7527263Ssam 		off += len;
7537263Ssam 		if (off == totlen) {
7549745Ssam 			cp = ecbuf + ECRDOFF + sizeof (struct ether_header);
7557263Ssam 			off = 0;
7567263Ssam 			totlen = off0;
7577263Ssam 		}
7586520Sfeldman 	}
7596520Sfeldman 	return (top);
7606520Sfeldman bad:
7616520Sfeldman 	m_freem(top);
7626520Sfeldman 	return (0);
7636520Sfeldman }
76413055Ssam 
76513055Ssam /*
76613055Ssam  * Process an ioctl request.
76713055Ssam  */
76813055Ssam ecioctl(ifp, cmd, data)
76913055Ssam 	register struct ifnet *ifp;
77013055Ssam 	int cmd;
77113055Ssam 	caddr_t data;
77213055Ssam {
773*19863Skarels 	register struct ifaddr *ifa = (struct ifaddr *)data;
77413055Ssam 	int s = splimp(), error = 0;
77513055Ssam 
77613055Ssam 	switch (cmd) {
77713055Ssam 
77813055Ssam 	case SIOCSIFADDR:
779*19863Skarels 		ifp->if_flags |= IFF_UP;
78013055Ssam 		ecinit(ifp->if_unit);
781*19863Skarels 
782*19863Skarels 		switch (ifa->ifa_addr.sa_family) {
783*19863Skarels 		case AF_INET:
784*19863Skarels 			((struct arpcom *)ifp)->ac_ipaddr =
785*19863Skarels 				IA_SIN(ifa)->sin_addr;
786*19863Skarels 			arpwhohas((struct arpcom *)ifp, &IA_SIN(ifa)->sin_addr);
787*19863Skarels 			break;
788*19863Skarels 		}
78913055Ssam 		break;
79013055Ssam 
79113055Ssam 	default:
79213055Ssam 		error = EINVAL;
79313055Ssam 	}
79413055Ssam 	splx(s);
79513055Ssam 	return (error);
79613055Ssam }
797