xref: /csrg-svn/sys/vax/if/if_ec.c (revision 19951)
1*19951Sbloom /*	if_ec.c	6.8	85/05/04	*/
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"
2519863Skarels #include "../netinet/in_var.h"
268417Swnj #include "../netinet/ip.h"
278417Swnj #include "../netinet/ip_var.h"
2811574Ssam #include "../netinet/if_ether.h"
29*19951Sbloom #ifdef PUP
308417Swnj #include "../netpup/pup.h"
31*19951Sbloom #endif
326520Sfeldman 
338461Sroot #include "../vax/cpu.h"
348461Sroot #include "../vax/mtpr.h"
3517112Sbloom #include "if_ecreg.h"
3617112Sbloom #include "if_uba.h"
378461Sroot #include "../vaxuba/ubareg.h"
388461Sroot #include "../vaxuba/ubavar.h"
398461Sroot 
4017995Skarels #if CLSIZE == 2
4117995Skarels #define ECBUFSIZE	32		/* on-board memory, clusters */
4216749Sbloom #endif
4316749Sbloom 
4417995Skarels int	ecubamem(), ecprobe(), ecattach(), ecrint(), ecxint(), eccollide();
456520Sfeldman struct	uba_device *ecinfo[NEC];
466520Sfeldman u_short ecstd[] = { 0 };
476520Sfeldman struct	uba_driver ecdriver =
4817995Skarels 	{ ecprobe, 0, ecattach, 0, ecstd, "ec", ecinfo, 0, 0, 0, ecubamem };
496520Sfeldman 
5013055Ssam int	ecinit(),ecioctl(),ecoutput(),ecreset();
518773Sroot struct	mbuf *ecget();
526520Sfeldman 
536545Sfeldman extern struct ifnet loif;
546545Sfeldman 
556520Sfeldman /*
566520Sfeldman  * Ethernet software status per interface.
576520Sfeldman  *
586520Sfeldman  * Each interface is referenced by a network interface structure,
596520Sfeldman  * es_if, which the routing code uses to locate the interface.
606520Sfeldman  * This structure contains the output queue for the interface, its address, ...
616520Sfeldman  * We also have, for each interface, a UBA interface structure, which
626520Sfeldman  * contains information about the UNIBUS resources held by the interface:
636520Sfeldman  * map registers, buffered data paths, etc.  Information is cached in this
646520Sfeldman  * structure for use by the if_uba.c routines in running the interface
656520Sfeldman  * efficiently.
666520Sfeldman  */
676520Sfeldman struct	ec_softc {
6811574Ssam 	struct	arpcom es_ac;		/* common Ethernet structures */
6911574Ssam #define	es_if	es_ac.ac_if		/* network-visible interface */
7011574Ssam #define	es_addr	es_ac.ac_enaddr		/* hardware Ethernet address */
716520Sfeldman 	struct	ifuba es_ifuba;		/* UNIBUS resources */
726520Sfeldman 	short	es_mask;		/* mask for current output delay */
736520Sfeldman 	short	es_oactive;		/* is output active? */
748773Sroot 	u_char	*es_buf[16];		/* virtual addresses of buffers */
756520Sfeldman } ec_softc[NEC];
766520Sfeldman 
776520Sfeldman /*
7817995Skarels  * Configure on-board memory for an interface.
7917995Skarels  * Called from autoconfig and after a uba reset.
8017995Skarels  * The address of the memory on the uba is supplied in the device flags.
816520Sfeldman  */
8217995Skarels ecubamem(ui, uban)
8317995Skarels 	register struct uba_device *ui;
846520Sfeldman {
8517995Skarels 	register caddr_t ecbuf = (caddr_t) &umem[uban][ui->ui_flags];
8617995Skarels 	register struct ecdevice *addr = (struct ecdevice *)ui->ui_addr;
876520Sfeldman 
8817995Skarels 	/*
8917995Skarels 	 * Make sure csr is there (we run before ecprobe).
9017995Skarels 	 */
9117995Skarels 	if (badaddr((caddr_t)addr, 2))
9217995Skarels 		return (-1);
9317995Skarels #if VAX780
9417995Skarels 	if (cpu == VAX_780 && uba_hd[uban].uh_uba->uba_sr) {
9517995Skarels 		uba_hd[uban].uh_uba->uba_sr = uba_hd[uban].uh_uba->uba_sr;
9617995Skarels 		return (-1);
9717995Skarels 	}
986520Sfeldman #endif
996520Sfeldman 	/*
1006637Sfeldman 	 * Make sure memory is turned on
1016637Sfeldman 	 */
1026637Sfeldman 	addr->ec_rcr = EC_AROM;
1036637Sfeldman 	/*
10417995Skarels 	 * Tell the system that the board has memory here, so it won't
10517995Skarels 	 * attempt to allocate the addresses later.
1067470Sfeldman 	 */
10717995Skarels 	if (ubamem(uban, ui->ui_flags, ECBUFSIZE*CLSIZE, 1) == 0) {
10817995Skarels 		printf("ec%d: cannot reserve uba addresses\n", ui->ui_unit);
10917995Skarels 		addr->ec_rcr = EC_MDISAB;	/* disable memory */
11017995Skarels 		return (-1);
11117995Skarels 	}
1127470Sfeldman 	/*
1136520Sfeldman 	 * Check for existence of buffers on Unibus.
1146520Sfeldman 	 */
1158773Sroot 	if (badaddr((caddr_t)ecbuf, 2)) {
11617995Skarels bad:
11717995Skarels 		printf("ec%d: buffer mem not found\n", ui->ui_unit);
11817995Skarels 		(void) ubamem(uban, ui->ui_flags, ECBUFSIZE*2, 0);
1197470Sfeldman 		addr->ec_rcr = EC_MDISAB;	/* disable memory */
12017995Skarels 		return (-1);
1216520Sfeldman 	}
1227470Sfeldman #if VAX780
12317995Skarels 	if (cpu == VAX_780 && uba_hd[uban].uh_uba->uba_sr) {
12417995Skarels 		uba_hd[uban].uh_uba->uba_sr = uba_hd[uban].uh_uba->uba_sr;
12517995Skarels 		goto bad;
1267470Sfeldman 	}
1277470Sfeldman #endif
12817995Skarels 	if (ui->ui_alive == 0)		/* Only printf from autoconfig */
12917995Skarels 		printf("ec%d: mem %x-%x\n", ui->ui_unit,
13017995Skarels 			ui->ui_flags, ui->ui_flags + ECBUFSIZE*CLBYTES - 1);
13117995Skarels 	ui->ui_type = 1;		/* Memory on, allocated */
13217995Skarels 	return (0);
13317995Skarels }
1346520Sfeldman 
13517995Skarels /*
13617995Skarels  * Do output DMA to determine interface presence and
13717995Skarels  * interrupt vector.  DMA is too short to disturb other hosts.
13817995Skarels  */
13917995Skarels ecprobe(reg, ui)
14017995Skarels 	caddr_t reg;
14117995Skarels 	struct uba_device *ui;
14217995Skarels {
14317995Skarels 	register int br, cvec;		/* r11, r10 value-result */
14417995Skarels 	register struct ecdevice *addr = (struct ecdevice *)reg;
14517995Skarels 	register caddr_t ecbuf = (caddr_t) &umem[ui->ui_ubanum][ui->ui_flags];
14617995Skarels 
14717995Skarels #ifdef lint
14817995Skarels 	br = 0; cvec = br; br = cvec;
14917995Skarels 	ecrint(0); ecxint(0); eccollide(0);
15017995Skarels #endif
15117995Skarels 
1526520Sfeldman 	/*
15317995Skarels 	 * Check that buffer memory was found and enabled.
1546520Sfeldman 	 */
15517995Skarels 	if (ui->ui_type == 0)
15617995Skarels 		return(0);
1576520Sfeldman 	/*
1586520Sfeldman 	 * Make a one byte packet in what should be buffer #0.
15917995Skarels 	 * Submit it for sending.  This should 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 	}
1776520Sfeldman 	return (1);
1786520Sfeldman }
1796520Sfeldman 
1806520Sfeldman /*
1816520Sfeldman  * Interface exists: make available by filling in network interface
1826520Sfeldman  * record.  System will initialize the interface when it is ready
1836520Sfeldman  * to accept packets.
1846520Sfeldman  */
1856520Sfeldman ecattach(ui)
1866520Sfeldman 	struct uba_device *ui;
1876520Sfeldman {
1887216Ssam 	struct ec_softc *es = &ec_softc[ui->ui_unit];
1897216Ssam 	register struct ifnet *ifp = &es->es_if;
1906520Sfeldman 	register struct ecdevice *addr = (struct ecdevice *)ui->ui_addr;
1917216Ssam 	int i, j;
1927216Ssam 	u_char *cp;
1936520Sfeldman 
1947216Ssam 	ifp->if_unit = ui->ui_unit;
1957216Ssam 	ifp->if_name = "ec";
1969745Ssam 	ifp->if_mtu = ETHERMTU;
1976520Sfeldman 
1986520Sfeldman 	/*
1997216Ssam 	 * Read the ethernet address off the board, one nibble at a time.
2006520Sfeldman 	 */
2016520Sfeldman 	addr->ec_xcr = EC_UECLR;
2026520Sfeldman 	addr->ec_rcr = EC_AROM;
20319863Skarels 	cp = es->es_addr;
2047216Ssam #define	NEXTBIT	addr->ec_rcr = EC_AROM|EC_ASTEP; addr->ec_rcr = EC_AROM
20516217Skarels 	for (i=0; i < sizeof (es->es_addr); i++) {
2066520Sfeldman 		*cp = 0;
2076520Sfeldman 		for (j=0; j<=4; j+=4) {
2086520Sfeldman 			*cp |= ((addr->ec_rcr >> 8) & 0xf) << j;
2097216Ssam 			NEXTBIT; NEXTBIT; NEXTBIT; NEXTBIT;
2106520Sfeldman 		}
2116520Sfeldman 		cp++;
2126520Sfeldman 	}
2137216Ssam 	ifp->if_init = ecinit;
21413055Ssam 	ifp->if_ioctl = ecioctl;
2157216Ssam 	ifp->if_output = ecoutput;
2168977Sroot 	ifp->if_reset = ecreset;
21719863Skarels 	ifp->if_flags = IFF_BROADCAST;
2186520Sfeldman 	for (i=0; i<16; i++)
21916749Sbloom 		es->es_buf[i]
22017995Skarels 		    = (u_char *)&umem[ui->ui_ubanum][ui->ui_flags + 2048*i];
2217216Ssam 	if_attach(ifp);
2226520Sfeldman }
2236520Sfeldman 
2246520Sfeldman /*
2256520Sfeldman  * Reset of interface after UNIBUS reset.
2266520Sfeldman  * If interface is on specified uba, reset its state.
2276520Sfeldman  */
2286520Sfeldman ecreset(unit, uban)
2296520Sfeldman 	int unit, uban;
2306520Sfeldman {
2316520Sfeldman 	register struct uba_device *ui;
2326520Sfeldman 
2336520Sfeldman 	if (unit >= NEC || (ui = ecinfo[unit]) == 0 || ui->ui_alive == 0 ||
2346520Sfeldman 	    ui->ui_ubanum != uban)
2356520Sfeldman 		return;
2366520Sfeldman 	printf(" ec%d", unit);
23716207Skarels 	ec_softc[unit].es_if.if_flags &= ~IFF_RUNNING;
2386520Sfeldman 	ecinit(unit);
2396520Sfeldman }
2406520Sfeldman 
2416520Sfeldman /*
2426520Sfeldman  * Initialization of interface; clear recorded pending
2436520Sfeldman  * operations, and reinitialize UNIBUS usage.
2446520Sfeldman  */
2456520Sfeldman ecinit(unit)
2466520Sfeldman 	int unit;
2476520Sfeldman {
2487216Ssam 	struct ec_softc *es = &ec_softc[unit];
2497216Ssam 	struct ecdevice *addr;
25011574Ssam 	register struct ifnet *ifp = &es->es_if;
25113055Ssam 	int i, s;
2526520Sfeldman 
25319863Skarels 	/* not yet, if address still unknown */
25419863Skarels 	if (ifp->if_addrlist == (struct ifaddr *)0)
25511574Ssam 		return;
25611574Ssam 
2576520Sfeldman 	/*
2587217Sfeldman 	 * Hang receive buffers and start any pending writes.
2596637Sfeldman 	 * Writing into the rcr also makes sure the memory
2606637Sfeldman 	 * is turned on.
2616520Sfeldman 	 */
26219863Skarels 	if ((ifp->if_flags & IFF_RUNNING) == 0) {
26313055Ssam 		addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
26413055Ssam 		s = splimp();
26513055Ssam 		for (i = ECRHBF; i >= ECRLBF; i--)
26613055Ssam 			addr->ec_rcr = EC_READ | i;
26713055Ssam 		es->es_oactive = 0;
26813055Ssam 		es->es_mask = ~0;
26919863Skarels 		es->es_if.if_flags |= IFF_RUNNING;
27013055Ssam 		if (es->es_if.if_snd.ifq_head)
27113055Ssam 			ecstart(unit);
27213055Ssam 		splx(s);
27313055Ssam 	}
2746520Sfeldman }
2756520Sfeldman 
2766520Sfeldman /*
2776520Sfeldman  * Start or restart output on interface.
2786520Sfeldman  * If interface is already active, then this is a retransmit
2796545Sfeldman  * after a collision, and just restuff registers.
2806520Sfeldman  * If interface is not already active, get another datagram
2816520Sfeldman  * to send off of the interface queue, and map it to the interface
2826520Sfeldman  * before starting the output.
2836520Sfeldman  */
28417995Skarels ecstart(unit)
2856520Sfeldman {
2867216Ssam 	struct ec_softc *es = &ec_softc[unit];
2877216Ssam 	struct ecdevice *addr;
2886520Sfeldman 	struct mbuf *m;
2896520Sfeldman 
2906520Sfeldman 	if (es->es_oactive)
2916520Sfeldman 		goto restart;
2926520Sfeldman 
2936520Sfeldman 	IF_DEQUEUE(&es->es_if.if_snd, m);
2946520Sfeldman 	if (m == 0) {
2956520Sfeldman 		es->es_oactive = 0;
2966520Sfeldman 		return;
2976520Sfeldman 	}
2986520Sfeldman 	ecput(es->es_buf[ECTBF], m);
2996520Sfeldman 
3006520Sfeldman restart:
3017216Ssam 	addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
3026520Sfeldman 	addr->ec_xcr = EC_WRITE|ECTBF;
3036520Sfeldman 	es->es_oactive = 1;
3046520Sfeldman }
3056520Sfeldman 
3066520Sfeldman /*
3076520Sfeldman  * Ethernet interface transmitter interrupt.
3086520Sfeldman  * Start another output if more data to send.
3096520Sfeldman  */
3106520Sfeldman ecxint(unit)
3116520Sfeldman 	int unit;
3126520Sfeldman {
3136520Sfeldman 	register struct ec_softc *es = &ec_softc[unit];
3147216Ssam 	register struct ecdevice *addr =
3157216Ssam 		(struct ecdevice *)ecinfo[unit]->ui_addr;
3166520Sfeldman 
3176520Sfeldman 	if (es->es_oactive == 0)
3186520Sfeldman 		return;
3197216Ssam 	if ((addr->ec_xcr&EC_XDONE) == 0 || (addr->ec_xcr&EC_XBN) != ECTBF) {
3207216Ssam 		printf("ec%d: stray xmit interrupt, xcr=%b\n", unit,
3217216Ssam 			addr->ec_xcr, EC_XBITS);
3227216Ssam 		es->es_oactive = 0;
3237216Ssam 		addr->ec_xcr = EC_XCLR;
3247216Ssam 		return;
3257216Ssam 	}
3266520Sfeldman 	es->es_if.if_opackets++;
3276520Sfeldman 	es->es_oactive = 0;
3286520Sfeldman 	es->es_mask = ~0;
3296520Sfeldman 	addr->ec_xcr = EC_XCLR;
3307216Ssam 	if (es->es_if.if_snd.ifq_head)
3317216Ssam 		ecstart(unit);
3326520Sfeldman }
3336520Sfeldman 
3346520Sfeldman /*
3356520Sfeldman  * Collision on ethernet interface.  Do exponential
3366520Sfeldman  * backoff, and retransmit.  If have backed off all
3376520Sfeldman  * the way print warning diagnostic, and drop packet.
3386520Sfeldman  */
3396520Sfeldman eccollide(unit)
3406520Sfeldman 	int unit;
3416520Sfeldman {
3426520Sfeldman 	struct ec_softc *es = &ec_softc[unit];
3436520Sfeldman 
3446520Sfeldman 	es->es_if.if_collisions++;
3457216Ssam 	if (es->es_oactive)
3467216Ssam 		ecdocoll(unit);
3476520Sfeldman }
3486520Sfeldman 
3496520Sfeldman ecdocoll(unit)
3506520Sfeldman 	int unit;
3516520Sfeldman {
3526520Sfeldman 	register struct ec_softc *es = &ec_softc[unit];
3536545Sfeldman 	register struct ecdevice *addr =
3546545Sfeldman 	    (struct ecdevice *)ecinfo[unit]->ui_addr;
3556545Sfeldman 	register i;
3566545Sfeldman 	int delay;
3576520Sfeldman 
3586520Sfeldman 	/*
3596520Sfeldman 	 * Es_mask is a 16 bit number with n low zero bits, with
3606520Sfeldman 	 * n the number of backoffs.  When es_mask is 0 we have
3616520Sfeldman 	 * backed off 16 times, and give up.
3626520Sfeldman 	 */
3636520Sfeldman 	if (es->es_mask == 0) {
3646545Sfeldman 		es->es_if.if_oerrors++;
3656520Sfeldman 		printf("ec%d: send error\n", unit);
3666520Sfeldman 		/*
3676545Sfeldman 		 * Reset interface, then requeue rcv buffers.
3686545Sfeldman 		 * Some incoming packets may be lost, but that
3696545Sfeldman 		 * can't be helped.
3706520Sfeldman 		 */
3716545Sfeldman 		addr->ec_xcr = EC_UECLR;
3726545Sfeldman 		for (i=ECRHBF; i>=ECRLBF; i--)
3736545Sfeldman 			addr->ec_rcr = EC_READ|i;
3746545Sfeldman 		/*
3756545Sfeldman 		 * Reset and transmit next packet (if any).
3766545Sfeldman 		 */
3776545Sfeldman 		es->es_oactive = 0;
3786545Sfeldman 		es->es_mask = ~0;
3796545Sfeldman 		if (es->es_if.if_snd.ifq_head)
3806545Sfeldman 			ecstart(unit);
3816520Sfeldman 		return;
3826520Sfeldman 	}
3836520Sfeldman 	/*
3846545Sfeldman 	 * Do exponential backoff.  Compute delay based on low bits
3856545Sfeldman 	 * of the interval timer.  Then delay for that number of
3866545Sfeldman 	 * slot times.  A slot time is 51.2 microseconds (rounded to 51).
3876545Sfeldman 	 * This does not take into account the time already used to
3886545Sfeldman 	 * process the interrupt.
3896520Sfeldman 	 */
3906520Sfeldman 	es->es_mask <<= 1;
3916545Sfeldman 	delay = mfpr(ICR) &~ es->es_mask;
3926545Sfeldman 	DELAY(delay * 51);
3936520Sfeldman 	/*
3946545Sfeldman 	 * Clear the controller's collision flag, thus enabling retransmit.
3956520Sfeldman 	 */
3967470Sfeldman 	addr->ec_xcr = EC_CLEAR;
3976520Sfeldman }
3986520Sfeldman 
3996520Sfeldman /*
4006520Sfeldman  * Ethernet interface receiver interrupt.
4016520Sfeldman  * If input error just drop packet.
4026520Sfeldman  * Otherwise purge input buffered data path and examine
4036520Sfeldman  * packet to determine type.  If can't determine length
4046520Sfeldman  * from type, then have to drop packet.  Othewise decapsulate
4056520Sfeldman  * packet based on type and pass to type specific higher-level
4066520Sfeldman  * input routine.
4076520Sfeldman  */
4086520Sfeldman ecrint(unit)
4096520Sfeldman 	int unit;
4106520Sfeldman {
4116520Sfeldman 	struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
4126520Sfeldman 
4136520Sfeldman 	while (addr->ec_rcr & EC_RDONE)
4146520Sfeldman 		ecread(unit);
4156520Sfeldman }
4166520Sfeldman 
4176520Sfeldman ecread(unit)
4186520Sfeldman 	int unit;
4196520Sfeldman {
4206520Sfeldman 	register struct ec_softc *es = &ec_softc[unit];
4216520Sfeldman 	struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
4229745Ssam 	register struct ether_header *ec;
4236520Sfeldman     	struct mbuf *m;
4248773Sroot 	int len, off, resid, ecoff, rbuf;
4256520Sfeldman 	register struct ifqueue *inq;
4268773Sroot 	u_char *ecbuf;
4276520Sfeldman 
4286520Sfeldman 	es->es_if.if_ipackets++;
4298773Sroot 	rbuf = addr->ec_rcr & EC_RBN;
4308773Sroot 	if (rbuf < ECRLBF || rbuf > ECRHBF)
4316520Sfeldman 		panic("ecrint");
4328773Sroot 	ecbuf = es->es_buf[rbuf];
4336520Sfeldman 	ecoff = *(short *)ecbuf;
4346545Sfeldman 	if (ecoff <= ECRDOFF || ecoff > 2046) {
4356520Sfeldman 		es->es_if.if_ierrors++;
4366520Sfeldman #ifdef notdef
4376520Sfeldman 		if (es->es_if.if_ierrors % 100 == 0)
4386520Sfeldman 			printf("ec%d: += 100 input errors\n", unit);
4396520Sfeldman #endif
4406520Sfeldman 		goto setup;
4416520Sfeldman 	}
4426520Sfeldman 
4436520Sfeldman 	/*
4446520Sfeldman 	 * Get input data length.
4456520Sfeldman 	 * Get pointer to ethernet header (in input buffer).
44619863Skarels 	 * Deal with trailer protocol: if type is trailer type
4476520Sfeldman 	 * get true type from first 16-bit word past data.
4486520Sfeldman 	 * Remember that type was trailer by setting off.
4496520Sfeldman 	 */
4509745Ssam 	len = ecoff - ECRDOFF - sizeof (struct ether_header);
4519745Ssam 	ec = (struct ether_header *)(ecbuf + ECRDOFF);
4529745Ssam 	ec->ether_type = ntohs((u_short)ec->ether_type);
4536520Sfeldman #define	ecdataaddr(ec, off, type)	((type)(((caddr_t)((ec)+1)+(off))))
45419863Skarels 	if (ec->ether_type >= ETHERTYPE_TRAIL &&
45519863Skarels 	    ec->ether_type < ETHERTYPE_TRAIL+ETHERTYPE_NTRAILER) {
45619863Skarels 		off = (ec->ether_type - ETHERTYPE_TRAIL) * 512;
4579745Ssam 		if (off >= ETHERMTU)
4586520Sfeldman 			goto setup;		/* sanity */
4599745Ssam 		ec->ether_type = ntohs(*ecdataaddr(ec, off, u_short *));
4609745Ssam 		resid = ntohs(*(ecdataaddr(ec, off+2, u_short *)));
4616520Sfeldman 		if (off + resid > len)
4626520Sfeldman 			goto setup;		/* sanity */
4636520Sfeldman 		len = off + resid;
4646520Sfeldman 	} else
4656520Sfeldman 		off = 0;
4666520Sfeldman 	if (len == 0)
4676520Sfeldman 		goto setup;
4686520Sfeldman 
4696520Sfeldman 	/*
4706520Sfeldman 	 * Pull packet off interface.  Off is nonzero if packet
4716520Sfeldman 	 * has trailing header; ecget will then force this header
4726520Sfeldman 	 * information to be at the front, but we still have to drop
4736520Sfeldman 	 * the type and length which are at the front of any trailer data.
4746520Sfeldman 	 */
4756520Sfeldman 	m = ecget(ecbuf, len, off);
4766520Sfeldman 	if (m == 0)
4776520Sfeldman 		goto setup;
4786520Sfeldman 	if (off) {
4796520Sfeldman 		m->m_off += 2 * sizeof (u_short);
4806520Sfeldman 		m->m_len -= 2 * sizeof (u_short);
4816520Sfeldman 	}
4829745Ssam 	switch (ec->ether_type) {
4836520Sfeldman 
4846520Sfeldman #ifdef INET
48519863Skarels 	case ETHERTYPE_IP:
4866520Sfeldman 		schednetisr(NETISR_IP);
4876520Sfeldman 		inq = &ipintrq;
4886520Sfeldman 		break;
48911574Ssam 
49019863Skarels 	case ETHERTYPE_ARP:
49111574Ssam 		arpinput(&es->es_ac, m);
49213987Ssam 		goto setup;
4936520Sfeldman #endif
4946520Sfeldman 	default:
4956520Sfeldman 		m_freem(m);
4966520Sfeldman 		goto setup;
4976520Sfeldman 	}
4986520Sfeldman 
4996520Sfeldman 	if (IF_QFULL(inq)) {
5006520Sfeldman 		IF_DROP(inq);
5016520Sfeldman 		m_freem(m);
5027216Ssam 		goto setup;
5037216Ssam 	}
5047216Ssam 	IF_ENQUEUE(inq, m);
5056520Sfeldman 
5066520Sfeldman setup:
5076520Sfeldman 	/*
5086520Sfeldman 	 * Reset for next packet.
5096520Sfeldman 	 */
5108773Sroot 	addr->ec_rcr = EC_READ|EC_RCLR|rbuf;
5116520Sfeldman }
5126520Sfeldman 
5136520Sfeldman /*
5146520Sfeldman  * Ethernet output routine.
5156520Sfeldman  * Encapsulate a packet of type family for the local net.
5166520Sfeldman  * Use trailer local net encapsulation if enough data in first
5176520Sfeldman  * packet leaves a multiple of 512 bytes of data in remainder.
5186545Sfeldman  * If destination is this address or broadcast, send packet to
5196545Sfeldman  * loop device to kludge around the fact that 3com interfaces can't
5206545Sfeldman  * talk to themselves.
5216520Sfeldman  */
5226520Sfeldman ecoutput(ifp, m0, dst)
5236520Sfeldman 	struct ifnet *ifp;
5246520Sfeldman 	struct mbuf *m0;
5256520Sfeldman 	struct sockaddr *dst;
5266520Sfeldman {
52711574Ssam 	int type, s, error;
52819863Skarels  	u_char edst[6];
52911574Ssam 	struct in_addr idst;
5306520Sfeldman 	register struct ec_softc *es = &ec_softc[ifp->if_unit];
5316520Sfeldman 	register struct mbuf *m = m0;
5329745Ssam 	register struct ether_header *ec;
53312771Ssam 	register int off;
53411574Ssam 	struct mbuf *mcopy = (struct mbuf *)0;
5356520Sfeldman 
5366520Sfeldman 	switch (dst->sa_family) {
5376520Sfeldman 
5386520Sfeldman #ifdef INET
5396520Sfeldman 	case AF_INET:
54011574Ssam 		idst = ((struct sockaddr_in *)dst)->sin_addr;
54119863Skarels 		if (!arpresolve(&es->es_ac, m, &idst, edst))
54211574Ssam 			return (0);	/* if not yet resolved */
54319863Skarels 		if (!bcmp((caddr_t)edst, (caddr_t)etherbroadcastaddr,
54419863Skarels 		    sizeof(edst)))
5458838Sroot 			mcopy = m_copy(m, 0, (int)M_COPYALL);
5466520Sfeldman 		off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len;
54713055Ssam 		/* need per host negotiation */
54813055Ssam 		if ((ifp->if_flags & IFF_NOTRAILERS) == 0)
5496520Sfeldman 		if (off > 0 && (off & 0x1ff) == 0 &&
5506520Sfeldman 		    m->m_off >= MMINOFF + 2 * sizeof (u_short)) {
55119863Skarels 			type = ETHERTYPE_TRAIL + (off>>9);
5526520Sfeldman 			m->m_off -= 2 * sizeof (u_short);
5536520Sfeldman 			m->m_len += 2 * sizeof (u_short);
55419863Skarels 			*mtod(m, u_short *) = ntohs((u_short)ETHERTYPE_IP);
5559745Ssam 			*(mtod(m, u_short *) + 1) = ntohs((u_short)m->m_len);
5566520Sfeldman 			goto gottrailertype;
5576520Sfeldman 		}
55819863Skarels 		type = ETHERTYPE_IP;
5596520Sfeldman 		off = 0;
5606520Sfeldman 		goto gottype;
5616520Sfeldman #endif
5626520Sfeldman 
56311574Ssam 	case AF_UNSPEC:
56411574Ssam 		ec = (struct ether_header *)dst->sa_data;
56519863Skarels  		bcopy((caddr_t)ec->ether_dhost, (caddr_t)edst, sizeof (edst));
56611574Ssam 		type = ec->ether_type;
56711574Ssam 		goto gottype;
56811574Ssam 
5696520Sfeldman 	default:
5706520Sfeldman 		printf("ec%d: can't handle af%d\n", ifp->if_unit,
5716520Sfeldman 			dst->sa_family);
5726520Sfeldman 		error = EAFNOSUPPORT;
5736520Sfeldman 		goto bad;
5746520Sfeldman 	}
5756520Sfeldman 
5766520Sfeldman gottrailertype:
5776520Sfeldman 	/*
5786520Sfeldman 	 * Packet to be sent as trailer: move first packet
5796520Sfeldman 	 * (control information) to end of chain.
5806520Sfeldman 	 */
5816520Sfeldman 	while (m->m_next)
5826520Sfeldman 		m = m->m_next;
5836520Sfeldman 	m->m_next = m0;
5846520Sfeldman 	m = m0->m_next;
5856520Sfeldman 	m0->m_next = 0;
5866520Sfeldman 	m0 = m;
5876520Sfeldman 
5886520Sfeldman gottype:
5896520Sfeldman 	/*
5906520Sfeldman 	 * Add local net header.  If no space in first mbuf,
5916520Sfeldman 	 * allocate another.
5926520Sfeldman 	 */
5936520Sfeldman 	if (m->m_off > MMAXOFF ||
5949745Ssam 	    MMINOFF + sizeof (struct ether_header) > m->m_off) {
5959648Ssam 		m = m_get(M_DONTWAIT, MT_HEADER);
5966520Sfeldman 		if (m == 0) {
5976520Sfeldman 			error = ENOBUFS;
5986520Sfeldman 			goto bad;
5996520Sfeldman 		}
6006520Sfeldman 		m->m_next = m0;
6016520Sfeldman 		m->m_off = MMINOFF;
6029745Ssam 		m->m_len = sizeof (struct ether_header);
6036520Sfeldman 	} else {
6049745Ssam 		m->m_off -= sizeof (struct ether_header);
6059745Ssam 		m->m_len += sizeof (struct ether_header);
6066520Sfeldman 	}
6079745Ssam 	ec = mtod(m, struct ether_header *);
60819863Skarels  	bcopy((caddr_t)edst, (caddr_t)ec->ether_dhost, sizeof (edst));
60919863Skarels 	bcopy((caddr_t)es->es_addr, (caddr_t)ec->ether_shost,
61019863Skarels 	    sizeof(ec->ether_shost));
6119745Ssam 	ec->ether_type = htons((u_short)type);
6126520Sfeldman 
6136520Sfeldman 	/*
6146520Sfeldman 	 * Queue message on interface, and start output if interface
6156520Sfeldman 	 * not yet active.
6166520Sfeldman 	 */
6176520Sfeldman 	s = splimp();
6186520Sfeldman 	if (IF_QFULL(&ifp->if_snd)) {
6196520Sfeldman 		IF_DROP(&ifp->if_snd);
6206520Sfeldman 		error = ENOBUFS;
6216520Sfeldman 		goto qfull;
6226520Sfeldman 	}
6236520Sfeldman 	IF_ENQUEUE(&ifp->if_snd, m);
6246520Sfeldman 	if (es->es_oactive == 0)
6256520Sfeldman 		ecstart(ifp->if_unit);
6266520Sfeldman 	splx(s);
62712771Ssam 	return (mcopy ? looutput(&loif, mcopy, dst) : 0);
6287216Ssam 
6296520Sfeldman qfull:
6306520Sfeldman 	m0 = m;
6316520Sfeldman 	splx(s);
6326520Sfeldman bad:
6336520Sfeldman 	m_freem(m0);
63416207Skarels 	if (mcopy)
63516207Skarels 		m_freem(mcopy);
63612771Ssam 	return (error);
6376520Sfeldman }
6386520Sfeldman 
6396520Sfeldman /*
6409177Ssam  * Routine to copy from mbuf chain to transmit
6417216Ssam  * buffer in UNIBUS memory.
6429177Ssam  * If packet size is less than the minimum legal size,
6439177Ssam  * the buffer is expanded.  We probably should zero out the extra
6449177Ssam  * bytes for security, but that would slow things down.
6456520Sfeldman  */
6466520Sfeldman ecput(ecbuf, m)
6477216Ssam 	u_char *ecbuf;
6486520Sfeldman 	struct mbuf *m;
6496520Sfeldman {
6506520Sfeldman 	register struct mbuf *mp;
6517216Ssam 	register int off;
6527263Ssam 	u_char *bp;
6536520Sfeldman 
6547216Ssam 	for (off = 2048, mp = m; mp; mp = mp->m_next)
6557216Ssam 		off -= mp->m_len;
6569745Ssam 	if (2048 - off < ETHERMIN + sizeof (struct ether_header))
6579745Ssam 		off = 2048 - ETHERMIN - sizeof (struct ether_header);
6587216Ssam 	*(u_short *)ecbuf = off;
6597216Ssam 	bp = (u_char *)(ecbuf + off);
6607263Ssam 	for (mp = m; mp; mp = mp->m_next) {
6617263Ssam 		register unsigned len = mp->m_len;
6627263Ssam 		u_char *mcp;
6637216Ssam 
6647216Ssam 		if (len == 0)
6657216Ssam 			continue;
6667216Ssam 		mcp = mtod(mp, u_char *);
6677216Ssam 		if ((unsigned)bp & 01) {
6687032Swnj 			*bp++ = *mcp++;
6697216Ssam 			len--;
6707032Swnj 		}
6717263Ssam 		if (off = (len >> 1)) {
6727263Ssam 			register u_short *to, *from;
6737263Ssam 
6747263Ssam 			to = (u_short *)bp;
6757263Ssam 			from = (u_short *)mcp;
6767263Ssam 			do
6777263Ssam 				*to++ = *from++;
6787263Ssam 			while (--off > 0);
6797263Ssam 			bp = (u_char *)to,
6807263Ssam 			mcp = (u_char *)from;
6817032Swnj 		}
6827263Ssam 		if (len & 01)
6836520Sfeldman 			*bp++ = *mcp++;
6846520Sfeldman 	}
6857263Ssam 	m_freem(m);
6866520Sfeldman }
6876520Sfeldman 
6886520Sfeldman /*
6896520Sfeldman  * Routine to copy from UNIBUS memory into mbufs.
6906520Sfeldman  * Similar in spirit to if_rubaget.
6917032Swnj  *
6927032Swnj  * Warning: This makes the fairly safe assumption that
6937032Swnj  * mbufs have even lengths.
6946520Sfeldman  */
6956520Sfeldman struct mbuf *
6966520Sfeldman ecget(ecbuf, totlen, off0)
6977263Ssam 	u_char *ecbuf;
6986520Sfeldman 	int totlen, off0;
6996520Sfeldman {
7007263Ssam 	register struct mbuf *m;
7017263Ssam 	struct mbuf *top = 0, **mp = &top;
7027263Ssam 	register int off = off0, len;
7037263Ssam 	u_char *cp;
7046520Sfeldman 
7059745Ssam 	cp = ecbuf + ECRDOFF + sizeof (struct ether_header);
7066520Sfeldman 	while (totlen > 0) {
7077263Ssam 		register int words;
7087263Ssam 		u_char *mcp;
7097263Ssam 
7109648Ssam 		MGET(m, M_DONTWAIT, MT_DATA);
7116520Sfeldman 		if (m == 0)
7126520Sfeldman 			goto bad;
7136520Sfeldman 		if (off) {
7146520Sfeldman 			len = totlen - off;
7159745Ssam 			cp = ecbuf + ECRDOFF +
7169745Ssam 				sizeof (struct ether_header) + off;
7176520Sfeldman 		} else
7186520Sfeldman 			len = totlen;
7196520Sfeldman 		if (len >= CLBYTES) {
7206520Sfeldman 			struct mbuf *p;
7216520Sfeldman 
7226520Sfeldman 			MCLGET(p, 1);
7236520Sfeldman 			if (p != 0) {
7246520Sfeldman 				m->m_len = len = CLBYTES;
7256520Sfeldman 				m->m_off = (int)p - (int)m;
7266520Sfeldman 			} else {
7276520Sfeldman 				m->m_len = len = MIN(MLEN, len);
7286520Sfeldman 				m->m_off = MMINOFF;
7296520Sfeldman 			}
7306520Sfeldman 		} else {
7316520Sfeldman 			m->m_len = len = MIN(MLEN, len);
7326520Sfeldman 			m->m_off = MMINOFF;
7336520Sfeldman 		}
7347263Ssam 		mcp = mtod(m, u_char *);
7357263Ssam 		if (words = (len >> 1)) {
7367263Ssam 			register u_short *to, *from;
7377263Ssam 
7387263Ssam 			to = (u_short *)mcp;
7397263Ssam 			from = (u_short *)cp;
7407263Ssam 			do
7417263Ssam 				*to++ = *from++;
7427263Ssam 			while (--words > 0);
7437263Ssam 			mcp = (u_char *)to;
7447263Ssam 			cp = (u_char *)from;
7457032Swnj 		}
7467216Ssam 		if (len & 01)
7476520Sfeldman 			*mcp++ = *cp++;
7486520Sfeldman 		*mp = m;
7496520Sfeldman 		mp = &m->m_next;
7507263Ssam 		if (off == 0) {
7516520Sfeldman 			totlen -= len;
7527263Ssam 			continue;
7537263Ssam 		}
7547263Ssam 		off += len;
7557263Ssam 		if (off == totlen) {
7569745Ssam 			cp = ecbuf + ECRDOFF + sizeof (struct ether_header);
7577263Ssam 			off = 0;
7587263Ssam 			totlen = off0;
7597263Ssam 		}
7606520Sfeldman 	}
7616520Sfeldman 	return (top);
7626520Sfeldman bad:
7636520Sfeldman 	m_freem(top);
7646520Sfeldman 	return (0);
7656520Sfeldman }
76613055Ssam 
76713055Ssam /*
76813055Ssam  * Process an ioctl request.
76913055Ssam  */
77013055Ssam ecioctl(ifp, cmd, data)
77113055Ssam 	register struct ifnet *ifp;
77213055Ssam 	int cmd;
77313055Ssam 	caddr_t data;
77413055Ssam {
77519863Skarels 	register struct ifaddr *ifa = (struct ifaddr *)data;
77613055Ssam 	int s = splimp(), error = 0;
77713055Ssam 
77813055Ssam 	switch (cmd) {
77913055Ssam 
78013055Ssam 	case SIOCSIFADDR:
78119863Skarels 		ifp->if_flags |= IFF_UP;
78213055Ssam 		ecinit(ifp->if_unit);
78319863Skarels 
78419863Skarels 		switch (ifa->ifa_addr.sa_family) {
78519863Skarels 		case AF_INET:
78619863Skarels 			((struct arpcom *)ifp)->ac_ipaddr =
78719863Skarels 				IA_SIN(ifa)->sin_addr;
78819863Skarels 			arpwhohas((struct arpcom *)ifp, &IA_SIN(ifa)->sin_addr);
78919863Skarels 			break;
79019863Skarels 		}
79113055Ssam 		break;
79213055Ssam 
79313055Ssam 	default:
79413055Ssam 		error = EINVAL;
79513055Ssam 	}
79613055Ssam 	splx(s);
79713055Ssam 	return (error);
79813055Ssam }
799