xref: /csrg-svn/sys/vax/if/if_ec.c (revision 17995)
1*17995Skarels /*	if_ec.c	6.6	85/02/15	*/
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"
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"
3217112Sbloom #include "if_ecreg.h"
3317112Sbloom #include "if_uba.h"
348461Sroot #include "../vaxuba/ubareg.h"
358461Sroot #include "../vaxuba/ubavar.h"
368461Sroot 
37*17995Skarels #if CLSIZE == 2
38*17995Skarels #define ECBUFSIZE	32		/* on-board memory, clusters */
3916749Sbloom #endif
4016749Sbloom 
41*17995Skarels int	ecubamem(), ecprobe(), ecattach(), ecrint(), ecxint(), eccollide();
426520Sfeldman struct	uba_device *ecinfo[NEC];
436520Sfeldman u_short ecstd[] = { 0 };
446520Sfeldman struct	uba_driver ecdriver =
45*17995Skarels 	{ ecprobe, 0, ecattach, 0, ecstd, "ec", ecinfo, 0, 0, 0, ecubamem };
466520Sfeldman 
4713055Ssam int	ecinit(),ecioctl(),ecoutput(),ecreset();
488773Sroot struct	mbuf *ecget();
496520Sfeldman 
506545Sfeldman extern struct ifnet loif;
516545Sfeldman 
526520Sfeldman /*
536520Sfeldman  * Ethernet software status per interface.
546520Sfeldman  *
556520Sfeldman  * Each interface is referenced by a network interface structure,
566520Sfeldman  * es_if, which the routing code uses to locate the interface.
576520Sfeldman  * This structure contains the output queue for the interface, its address, ...
586520Sfeldman  * We also have, for each interface, a UBA interface structure, which
596520Sfeldman  * contains information about the UNIBUS resources held by the interface:
606520Sfeldman  * map registers, buffered data paths, etc.  Information is cached in this
616520Sfeldman  * structure for use by the if_uba.c routines in running the interface
626520Sfeldman  * efficiently.
636520Sfeldman  */
646520Sfeldman struct	ec_softc {
6511574Ssam 	struct	arpcom es_ac;		/* common Ethernet structures */
6611574Ssam #define	es_if	es_ac.ac_if		/* network-visible interface */
6711574Ssam #define	es_addr	es_ac.ac_enaddr		/* hardware Ethernet address */
686520Sfeldman 	struct	ifuba es_ifuba;		/* UNIBUS resources */
696520Sfeldman 	short	es_mask;		/* mask for current output delay */
706520Sfeldman 	short	es_oactive;		/* is output active? */
718773Sroot 	u_char	*es_buf[16];		/* virtual addresses of buffers */
726520Sfeldman } ec_softc[NEC];
736520Sfeldman 
746520Sfeldman /*
75*17995Skarels  * Configure on-board memory for an interface.
76*17995Skarels  * Called from autoconfig and after a uba reset.
77*17995Skarels  * The address of the memory on the uba is supplied in the device flags.
786520Sfeldman  */
79*17995Skarels ecubamem(ui, uban)
80*17995Skarels 	register struct uba_device *ui;
816520Sfeldman {
82*17995Skarels 	register caddr_t ecbuf = (caddr_t) &umem[uban][ui->ui_flags];
83*17995Skarels 	register struct ecdevice *addr = (struct ecdevice *)ui->ui_addr;
846520Sfeldman 
85*17995Skarels 	/*
86*17995Skarels 	 * Make sure csr is there (we run before ecprobe).
87*17995Skarels 	 */
88*17995Skarels 	if (badaddr((caddr_t)addr, 2))
89*17995Skarels 		return (-1);
90*17995Skarels #if VAX780
91*17995Skarels 	if (cpu == VAX_780 && uba_hd[uban].uh_uba->uba_sr) {
92*17995Skarels 		uba_hd[uban].uh_uba->uba_sr = uba_hd[uban].uh_uba->uba_sr;
93*17995Skarels 		return (-1);
94*17995Skarels 	}
956520Sfeldman #endif
966520Sfeldman 	/*
976637Sfeldman 	 * Make sure memory is turned on
986637Sfeldman 	 */
996637Sfeldman 	addr->ec_rcr = EC_AROM;
1006637Sfeldman 	/*
101*17995Skarels 	 * Tell the system that the board has memory here, so it won't
102*17995Skarels 	 * attempt to allocate the addresses later.
1037470Sfeldman 	 */
104*17995Skarels 	if (ubamem(uban, ui->ui_flags, ECBUFSIZE*CLSIZE, 1) == 0) {
105*17995Skarels 		printf("ec%d: cannot reserve uba addresses\n", ui->ui_unit);
106*17995Skarels 		addr->ec_rcr = EC_MDISAB;	/* disable memory */
107*17995Skarels 		return (-1);
108*17995Skarels 	}
1097470Sfeldman 	/*
1106520Sfeldman 	 * Check for existence of buffers on Unibus.
1116520Sfeldman 	 */
1128773Sroot 	if (badaddr((caddr_t)ecbuf, 2)) {
113*17995Skarels bad:
114*17995Skarels 		printf("ec%d: buffer mem not found\n", ui->ui_unit);
115*17995Skarels 		(void) ubamem(uban, ui->ui_flags, ECBUFSIZE*2, 0);
1167470Sfeldman 		addr->ec_rcr = EC_MDISAB;	/* disable memory */
117*17995Skarels 		return (-1);
1186520Sfeldman 	}
1197470Sfeldman #if VAX780
120*17995Skarels 	if (cpu == VAX_780 && uba_hd[uban].uh_uba->uba_sr) {
121*17995Skarels 		uba_hd[uban].uh_uba->uba_sr = uba_hd[uban].uh_uba->uba_sr;
122*17995Skarels 		goto bad;
1237470Sfeldman 	}
1247470Sfeldman #endif
125*17995Skarels 	if (ui->ui_alive == 0)		/* Only printf from autoconfig */
126*17995Skarels 		printf("ec%d: mem %x-%x\n", ui->ui_unit,
127*17995Skarels 			ui->ui_flags, ui->ui_flags + ECBUFSIZE*CLBYTES - 1);
128*17995Skarels 	ui->ui_type = 1;		/* Memory on, allocated */
129*17995Skarels 	return (0);
130*17995Skarels }
1316520Sfeldman 
132*17995Skarels /*
133*17995Skarels  * Do output DMA to determine interface presence and
134*17995Skarels  * interrupt vector.  DMA is too short to disturb other hosts.
135*17995Skarels  */
136*17995Skarels ecprobe(reg, ui)
137*17995Skarels 	caddr_t reg;
138*17995Skarels 	struct uba_device *ui;
139*17995Skarels {
140*17995Skarels 	register int br, cvec;		/* r11, r10 value-result */
141*17995Skarels 	register struct ecdevice *addr = (struct ecdevice *)reg;
142*17995Skarels 	register caddr_t ecbuf = (caddr_t) &umem[ui->ui_ubanum][ui->ui_flags];
143*17995Skarels 
144*17995Skarels #ifdef lint
145*17995Skarels 	br = 0; cvec = br; br = cvec;
146*17995Skarels 	ecrint(0); ecxint(0); eccollide(0);
147*17995Skarels #endif
148*17995Skarels 
1496520Sfeldman 	/*
150*17995Skarels 	 * Check that buffer memory was found and enabled.
1516520Sfeldman 	 */
152*17995Skarels 	if (ui->ui_type == 0)
153*17995Skarels 		return(0);
1546520Sfeldman 	/*
1556520Sfeldman 	 * Make a one byte packet in what should be buffer #0.
156*17995Skarels 	 * Submit it for sending.  This should cause an xmit interrupt.
1576520Sfeldman 	 * The xmit interrupt vector is 8 bytes after the receive vector,
1586520Sfeldman 	 * so adjust for this before returning.
1596520Sfeldman 	 */
1606520Sfeldman 	*(u_short *)ecbuf = (u_short) 03777;
1616520Sfeldman 	ecbuf[03777] = '\0';
1626520Sfeldman 	addr->ec_xcr = EC_XINTEN|EC_XWBN;
1636520Sfeldman 	DELAY(100000);
1646520Sfeldman 	addr->ec_xcr = EC_XCLR;
1657216Ssam 	if (cvec > 0 && cvec != 0x200) {
1667470Sfeldman 		if (cvec & 04) {	/* collision interrupt */
1677470Sfeldman 			cvec -= 04;
1687470Sfeldman 			br += 1;		/* rcv is collision + 1 */
1697470Sfeldman 		} else {		/* xmit interrupt */
1707470Sfeldman 			cvec -= 010;
1717470Sfeldman 			br += 2;		/* rcv is xmit + 2 */
1727470Sfeldman 		}
1737216Ssam 	}
1746520Sfeldman 	return (1);
1756520Sfeldman }
1766520Sfeldman 
1776520Sfeldman /*
1786520Sfeldman  * Interface exists: make available by filling in network interface
1796520Sfeldman  * record.  System will initialize the interface when it is ready
1806520Sfeldman  * to accept packets.
1816520Sfeldman  */
1826520Sfeldman ecattach(ui)
1836520Sfeldman 	struct uba_device *ui;
1846520Sfeldman {
1857216Ssam 	struct ec_softc *es = &ec_softc[ui->ui_unit];
1867216Ssam 	register struct ifnet *ifp = &es->es_if;
1876520Sfeldman 	register struct ecdevice *addr = (struct ecdevice *)ui->ui_addr;
1887216Ssam 	struct sockaddr_in *sin;
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;
20116217Skarels 	cp = (u_char *) &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 	}
2116520Sfeldman 	sin = (struct sockaddr_in *)&es->es_if.if_addr;
2126520Sfeldman 	sin->sin_family = AF_INET;
2137216Ssam 	ifp->if_init = ecinit;
21413055Ssam 	ifp->if_ioctl = ecioctl;
2157216Ssam 	ifp->if_output = ecoutput;
2168977Sroot 	ifp->if_reset = ecreset;
2176520Sfeldman 	for (i=0; i<16; i++)
21816749Sbloom 		es->es_buf[i]
219*17995Skarels 		    = (u_char *)&umem[ui->ui_ubanum][ui->ui_flags + 2048*i];
2207216Ssam 	if_attach(ifp);
2216520Sfeldman }
2226520Sfeldman 
2236520Sfeldman /*
2246520Sfeldman  * Reset of interface after UNIBUS reset.
2256520Sfeldman  * If interface is on specified uba, reset its state.
2266520Sfeldman  */
2276520Sfeldman ecreset(unit, uban)
2286520Sfeldman 	int unit, uban;
2296520Sfeldman {
2306520Sfeldman 	register struct uba_device *ui;
2316520Sfeldman 
2326520Sfeldman 	if (unit >= NEC || (ui = ecinfo[unit]) == 0 || ui->ui_alive == 0 ||
2336520Sfeldman 	    ui->ui_ubanum != uban)
2346520Sfeldman 		return;
2356520Sfeldman 	printf(" ec%d", unit);
23616207Skarels 	ec_softc[unit].es_if.if_flags &= ~IFF_RUNNING;
2376520Sfeldman 	ecinit(unit);
2386520Sfeldman }
2396520Sfeldman 
2406520Sfeldman /*
2416520Sfeldman  * Initialization of interface; clear recorded pending
2426520Sfeldman  * operations, and reinitialize UNIBUS usage.
2436520Sfeldman  */
2446520Sfeldman ecinit(unit)
2456520Sfeldman 	int unit;
2466520Sfeldman {
2477216Ssam 	struct ec_softc *es = &ec_softc[unit];
2487216Ssam 	struct ecdevice *addr;
24911574Ssam 	register struct ifnet *ifp = &es->es_if;
25013086Ssam 	register struct sockaddr_in *sin;
25113055Ssam 	int i, s;
2526520Sfeldman 
25311574Ssam 	sin = (struct sockaddr_in *)&ifp->if_addr;
25413055Ssam 	if (sin->sin_addr.s_addr == 0)		/* address still unknown */
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 	 */
26213055Ssam 	if ((es->es_if.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;
26913055Ssam 		es->es_if.if_flags |= IFF_UP|IFF_RUNNING;
27013055Ssam 		if (es->es_if.if_snd.ifq_head)
27113055Ssam 			ecstart(unit);
27213055Ssam 		splx(s);
27313055Ssam 	}
2747150Swnj 	if_rtinit(&es->es_if, RTF_UP);
27511574Ssam 	arpwhohas(&es->es_ac, &sin->sin_addr);
2766520Sfeldman }
2776520Sfeldman 
2786520Sfeldman /*
2796520Sfeldman  * Start or restart output on interface.
2806520Sfeldman  * If interface is already active, then this is a retransmit
2816545Sfeldman  * after a collision, and just restuff registers.
2826520Sfeldman  * If interface is not already active, get another datagram
2836520Sfeldman  * to send off of the interface queue, and map it to the interface
2846520Sfeldman  * before starting the output.
2856520Sfeldman  */
286*17995Skarels ecstart(unit)
2876520Sfeldman {
2887216Ssam 	struct ec_softc *es = &ec_softc[unit];
2897216Ssam 	struct ecdevice *addr;
2906520Sfeldman 	struct mbuf *m;
2916520Sfeldman 
2926520Sfeldman 	if (es->es_oactive)
2936520Sfeldman 		goto restart;
2946520Sfeldman 
2956520Sfeldman 	IF_DEQUEUE(&es->es_if.if_snd, m);
2966520Sfeldman 	if (m == 0) {
2976520Sfeldman 		es->es_oactive = 0;
2986520Sfeldman 		return;
2996520Sfeldman 	}
3006520Sfeldman 	ecput(es->es_buf[ECTBF], m);
3016520Sfeldman 
3026520Sfeldman restart:
3037216Ssam 	addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
3046520Sfeldman 	addr->ec_xcr = EC_WRITE|ECTBF;
3056520Sfeldman 	es->es_oactive = 1;
3066520Sfeldman }
3076520Sfeldman 
3086520Sfeldman /*
3096520Sfeldman  * Ethernet interface transmitter interrupt.
3106520Sfeldman  * Start another output if more data to send.
3116520Sfeldman  */
3126520Sfeldman ecxint(unit)
3136520Sfeldman 	int unit;
3146520Sfeldman {
3156520Sfeldman 	register struct ec_softc *es = &ec_softc[unit];
3167216Ssam 	register struct ecdevice *addr =
3177216Ssam 		(struct ecdevice *)ecinfo[unit]->ui_addr;
3186520Sfeldman 
3196520Sfeldman 	if (es->es_oactive == 0)
3206520Sfeldman 		return;
3217216Ssam 	if ((addr->ec_xcr&EC_XDONE) == 0 || (addr->ec_xcr&EC_XBN) != ECTBF) {
3227216Ssam 		printf("ec%d: stray xmit interrupt, xcr=%b\n", unit,
3237216Ssam 			addr->ec_xcr, EC_XBITS);
3247216Ssam 		es->es_oactive = 0;
3257216Ssam 		addr->ec_xcr = EC_XCLR;
3267216Ssam 		return;
3277216Ssam 	}
3286520Sfeldman 	es->es_if.if_opackets++;
3296520Sfeldman 	es->es_oactive = 0;
3306520Sfeldman 	es->es_mask = ~0;
3316520Sfeldman 	addr->ec_xcr = EC_XCLR;
3327216Ssam 	if (es->es_if.if_snd.ifq_head)
3337216Ssam 		ecstart(unit);
3346520Sfeldman }
3356520Sfeldman 
3366520Sfeldman /*
3376520Sfeldman  * Collision on ethernet interface.  Do exponential
3386520Sfeldman  * backoff, and retransmit.  If have backed off all
3396520Sfeldman  * the way print warning diagnostic, and drop packet.
3406520Sfeldman  */
3416520Sfeldman eccollide(unit)
3426520Sfeldman 	int unit;
3436520Sfeldman {
3446520Sfeldman 	struct ec_softc *es = &ec_softc[unit];
3456520Sfeldman 
3466520Sfeldman 	es->es_if.if_collisions++;
3477216Ssam 	if (es->es_oactive)
3487216Ssam 		ecdocoll(unit);
3496520Sfeldman }
3506520Sfeldman 
3516520Sfeldman ecdocoll(unit)
3526520Sfeldman 	int unit;
3536520Sfeldman {
3546520Sfeldman 	register struct ec_softc *es = &ec_softc[unit];
3556545Sfeldman 	register struct ecdevice *addr =
3566545Sfeldman 	    (struct ecdevice *)ecinfo[unit]->ui_addr;
3576545Sfeldman 	register i;
3586545Sfeldman 	int delay;
3596520Sfeldman 
3606520Sfeldman 	/*
3616520Sfeldman 	 * Es_mask is a 16 bit number with n low zero bits, with
3626520Sfeldman 	 * n the number of backoffs.  When es_mask is 0 we have
3636520Sfeldman 	 * backed off 16 times, and give up.
3646520Sfeldman 	 */
3656520Sfeldman 	if (es->es_mask == 0) {
3666545Sfeldman 		es->es_if.if_oerrors++;
3676520Sfeldman 		printf("ec%d: send error\n", unit);
3686520Sfeldman 		/*
3696545Sfeldman 		 * Reset interface, then requeue rcv buffers.
3706545Sfeldman 		 * Some incoming packets may be lost, but that
3716545Sfeldman 		 * can't be helped.
3726520Sfeldman 		 */
3736545Sfeldman 		addr->ec_xcr = EC_UECLR;
3746545Sfeldman 		for (i=ECRHBF; i>=ECRLBF; i--)
3756545Sfeldman 			addr->ec_rcr = EC_READ|i;
3766545Sfeldman 		/*
3776545Sfeldman 		 * Reset and transmit next packet (if any).
3786545Sfeldman 		 */
3796545Sfeldman 		es->es_oactive = 0;
3806545Sfeldman 		es->es_mask = ~0;
3816545Sfeldman 		if (es->es_if.if_snd.ifq_head)
3826545Sfeldman 			ecstart(unit);
3836520Sfeldman 		return;
3846520Sfeldman 	}
3856520Sfeldman 	/*
3866545Sfeldman 	 * Do exponential backoff.  Compute delay based on low bits
3876545Sfeldman 	 * of the interval timer.  Then delay for that number of
3886545Sfeldman 	 * slot times.  A slot time is 51.2 microseconds (rounded to 51).
3896545Sfeldman 	 * This does not take into account the time already used to
3906545Sfeldman 	 * process the interrupt.
3916520Sfeldman 	 */
3926520Sfeldman 	es->es_mask <<= 1;
3936545Sfeldman 	delay = mfpr(ICR) &~ es->es_mask;
3946545Sfeldman 	DELAY(delay * 51);
3956520Sfeldman 	/*
3966545Sfeldman 	 * Clear the controller's collision flag, thus enabling retransmit.
3976520Sfeldman 	 */
3987470Sfeldman 	addr->ec_xcr = EC_CLEAR;
3996520Sfeldman }
4006520Sfeldman 
4016520Sfeldman /*
4026520Sfeldman  * Ethernet interface receiver interrupt.
4036520Sfeldman  * If input error just drop packet.
4046520Sfeldman  * Otherwise purge input buffered data path and examine
4056520Sfeldman  * packet to determine type.  If can't determine length
4066520Sfeldman  * from type, then have to drop packet.  Othewise decapsulate
4076520Sfeldman  * packet based on type and pass to type specific higher-level
4086520Sfeldman  * input routine.
4096520Sfeldman  */
4106520Sfeldman ecrint(unit)
4116520Sfeldman 	int unit;
4126520Sfeldman {
4136520Sfeldman 	struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
4146520Sfeldman 
4156520Sfeldman 	while (addr->ec_rcr & EC_RDONE)
4166520Sfeldman 		ecread(unit);
4176520Sfeldman }
4186520Sfeldman 
4196520Sfeldman ecread(unit)
4206520Sfeldman 	int unit;
4216520Sfeldman {
4226520Sfeldman 	register struct ec_softc *es = &ec_softc[unit];
4236520Sfeldman 	struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
4249745Ssam 	register struct ether_header *ec;
4256520Sfeldman     	struct mbuf *m;
4268773Sroot 	int len, off, resid, ecoff, rbuf;
4276520Sfeldman 	register struct ifqueue *inq;
4288773Sroot 	u_char *ecbuf;
4296520Sfeldman 
4306520Sfeldman 	es->es_if.if_ipackets++;
4318773Sroot 	rbuf = addr->ec_rcr & EC_RBN;
4328773Sroot 	if (rbuf < ECRLBF || rbuf > ECRHBF)
4336520Sfeldman 		panic("ecrint");
4348773Sroot 	ecbuf = es->es_buf[rbuf];
4356520Sfeldman 	ecoff = *(short *)ecbuf;
4366545Sfeldman 	if (ecoff <= ECRDOFF || ecoff > 2046) {
4376520Sfeldman 		es->es_if.if_ierrors++;
4386520Sfeldman #ifdef notdef
4396520Sfeldman 		if (es->es_if.if_ierrors % 100 == 0)
4406520Sfeldman 			printf("ec%d: += 100 input errors\n", unit);
4416520Sfeldman #endif
4426520Sfeldman 		goto setup;
4436520Sfeldman 	}
4446520Sfeldman 
4456520Sfeldman 	/*
4466520Sfeldman 	 * Get input data length.
4476520Sfeldman 	 * Get pointer to ethernet header (in input buffer).
4486520Sfeldman 	 * Deal with trailer protocol: if type is PUP trailer
4496520Sfeldman 	 * get true type from first 16-bit word past data.
4506520Sfeldman 	 * Remember that type was trailer by setting off.
4516520Sfeldman 	 */
4529745Ssam 	len = ecoff - ECRDOFF - sizeof (struct ether_header);
4539745Ssam 	ec = (struct ether_header *)(ecbuf + ECRDOFF);
4549745Ssam 	ec->ether_type = ntohs((u_short)ec->ether_type);
4556520Sfeldman #define	ecdataaddr(ec, off, type)	((type)(((caddr_t)((ec)+1)+(off))))
4569745Ssam 	if (ec->ether_type >= ETHERPUP_TRAIL &&
4579745Ssam 	    ec->ether_type < ETHERPUP_TRAIL+ETHERPUP_NTRAILER) {
4589745Ssam 		off = (ec->ether_type - ETHERPUP_TRAIL) * 512;
4599745Ssam 		if (off >= ETHERMTU)
4606520Sfeldman 			goto setup;		/* sanity */
4619745Ssam 		ec->ether_type = ntohs(*ecdataaddr(ec, off, u_short *));
4629745Ssam 		resid = ntohs(*(ecdataaddr(ec, off+2, u_short *)));
4636520Sfeldman 		if (off + resid > len)
4646520Sfeldman 			goto setup;		/* sanity */
4656520Sfeldman 		len = off + resid;
4666520Sfeldman 	} else
4676520Sfeldman 		off = 0;
4686520Sfeldman 	if (len == 0)
4696520Sfeldman 		goto setup;
4706520Sfeldman 
4716520Sfeldman 	/*
4726520Sfeldman 	 * Pull packet off interface.  Off is nonzero if packet
4736520Sfeldman 	 * has trailing header; ecget will then force this header
4746520Sfeldman 	 * information to be at the front, but we still have to drop
4756520Sfeldman 	 * the type and length which are at the front of any trailer data.
4766520Sfeldman 	 */
4776520Sfeldman 	m = ecget(ecbuf, len, off);
4786520Sfeldman 	if (m == 0)
4796520Sfeldman 		goto setup;
4806520Sfeldman 	if (off) {
4816520Sfeldman 		m->m_off += 2 * sizeof (u_short);
4826520Sfeldman 		m->m_len -= 2 * sizeof (u_short);
4836520Sfeldman 	}
4849745Ssam 	switch (ec->ether_type) {
4856520Sfeldman 
4866520Sfeldman #ifdef INET
4879745Ssam 	case ETHERPUP_IPTYPE:
4886520Sfeldman 		schednetisr(NETISR_IP);
4896520Sfeldman 		inq = &ipintrq;
4906520Sfeldman 		break;
49111574Ssam 
49211574Ssam 	case ETHERPUP_ARPTYPE:
49311574Ssam 		arpinput(&es->es_ac, m);
49413987Ssam 		goto setup;
4956520Sfeldman #endif
4966520Sfeldman 	default:
4976520Sfeldman 		m_freem(m);
4986520Sfeldman 		goto setup;
4996520Sfeldman 	}
5006520Sfeldman 
5016520Sfeldman 	if (IF_QFULL(inq)) {
5026520Sfeldman 		IF_DROP(inq);
5036520Sfeldman 		m_freem(m);
5047216Ssam 		goto setup;
5057216Ssam 	}
5067216Ssam 	IF_ENQUEUE(inq, m);
5076520Sfeldman 
5086520Sfeldman setup:
5096520Sfeldman 	/*
5106520Sfeldman 	 * Reset for next packet.
5116520Sfeldman 	 */
5128773Sroot 	addr->ec_rcr = EC_READ|EC_RCLR|rbuf;
5136520Sfeldman }
5146520Sfeldman 
5156520Sfeldman /*
5166520Sfeldman  * Ethernet output routine.
5176520Sfeldman  * Encapsulate a packet of type family for the local net.
5186520Sfeldman  * Use trailer local net encapsulation if enough data in first
5196520Sfeldman  * packet leaves a multiple of 512 bytes of data in remainder.
5206545Sfeldman  * If destination is this address or broadcast, send packet to
5216545Sfeldman  * loop device to kludge around the fact that 3com interfaces can't
5226545Sfeldman  * talk to themselves.
5236520Sfeldman  */
5246520Sfeldman ecoutput(ifp, m0, dst)
5256520Sfeldman 	struct ifnet *ifp;
5266520Sfeldman 	struct mbuf *m0;
5276520Sfeldman 	struct sockaddr *dst;
5286520Sfeldman {
52911574Ssam 	int type, s, error;
53016217Skarels 	struct ether_addr edst;
53111574Ssam 	struct in_addr idst;
5326520Sfeldman 	register struct ec_softc *es = &ec_softc[ifp->if_unit];
5336520Sfeldman 	register struct mbuf *m = m0;
5349745Ssam 	register struct ether_header *ec;
53512771Ssam 	register int off;
53611574Ssam 	struct mbuf *mcopy = (struct mbuf *)0;
5376520Sfeldman 
5386520Sfeldman 	switch (dst->sa_family) {
5396520Sfeldman 
5406520Sfeldman #ifdef INET
5416520Sfeldman 	case AF_INET:
54211574Ssam 		idst = ((struct sockaddr_in *)dst)->sin_addr;
54316217Skarels 		if (!arpresolve(&es->es_ac, m, &idst, &edst))
54411574Ssam 			return (0);	/* if not yet resolved */
54511574Ssam 		if (in_lnaof(idst) == INADDR_ANY)
5468838Sroot 			mcopy = m_copy(m, 0, (int)M_COPYALL);
5476520Sfeldman 		off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len;
54813055Ssam 		/* need per host negotiation */
54913055Ssam 		if ((ifp->if_flags & IFF_NOTRAILERS) == 0)
5506520Sfeldman 		if (off > 0 && (off & 0x1ff) == 0 &&
5516520Sfeldman 		    m->m_off >= MMINOFF + 2 * sizeof (u_short)) {
5529745Ssam 			type = ETHERPUP_TRAIL + (off>>9);
5536520Sfeldman 			m->m_off -= 2 * sizeof (u_short);
5546520Sfeldman 			m->m_len += 2 * sizeof (u_short);
5559745Ssam 			*mtod(m, u_short *) = ntohs((u_short)ETHERPUP_IPTYPE);
5569745Ssam 			*(mtod(m, u_short *) + 1) = ntohs((u_short)m->m_len);
5576520Sfeldman 			goto gottrailertype;
5586520Sfeldman 		}
5599745Ssam 		type = ETHERPUP_IPTYPE;
5606520Sfeldman 		off = 0;
5616520Sfeldman 		goto gottype;
5626520Sfeldman #endif
5636520Sfeldman 
56411574Ssam 	case AF_UNSPEC:
56511574Ssam 		ec = (struct ether_header *)dst->sa_data;
56616217Skarels 		edst = ec->ether_dhost;
56711574Ssam 		type = ec->ether_type;
56811574Ssam 		goto gottype;
56911574Ssam 
5706520Sfeldman 	default:
5716520Sfeldman 		printf("ec%d: can't handle af%d\n", ifp->if_unit,
5726520Sfeldman 			dst->sa_family);
5736520Sfeldman 		error = EAFNOSUPPORT;
5746520Sfeldman 		goto bad;
5756520Sfeldman 	}
5766520Sfeldman 
5776520Sfeldman gottrailertype:
5786520Sfeldman 	/*
5796520Sfeldman 	 * Packet to be sent as trailer: move first packet
5806520Sfeldman 	 * (control information) to end of chain.
5816520Sfeldman 	 */
5826520Sfeldman 	while (m->m_next)
5836520Sfeldman 		m = m->m_next;
5846520Sfeldman 	m->m_next = m0;
5856520Sfeldman 	m = m0->m_next;
5866520Sfeldman 	m0->m_next = 0;
5876520Sfeldman 	m0 = m;
5886520Sfeldman 
5896520Sfeldman gottype:
5906520Sfeldman 	/*
5916520Sfeldman 	 * Add local net header.  If no space in first mbuf,
5926520Sfeldman 	 * allocate another.
5936520Sfeldman 	 */
5946520Sfeldman 	if (m->m_off > MMAXOFF ||
5959745Ssam 	    MMINOFF + sizeof (struct ether_header) > m->m_off) {
5969648Ssam 		m = m_get(M_DONTWAIT, MT_HEADER);
5976520Sfeldman 		if (m == 0) {
5986520Sfeldman 			error = ENOBUFS;
5996520Sfeldman 			goto bad;
6006520Sfeldman 		}
6016520Sfeldman 		m->m_next = m0;
6026520Sfeldman 		m->m_off = MMINOFF;
6039745Ssam 		m->m_len = sizeof (struct ether_header);
6046520Sfeldman 	} else {
6059745Ssam 		m->m_off -= sizeof (struct ether_header);
6069745Ssam 		m->m_len += sizeof (struct ether_header);
6076520Sfeldman 	}
6089745Ssam 	ec = mtod(m, struct ether_header *);
60916217Skarels 	ec->ether_dhost = edst;
61016217Skarels 	ec->ether_shost = es->es_addr;
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 {
77513055Ssam 	register struct ifreq *ifr = (struct ifreq *)data;
77613055Ssam 	int s = splimp(), error = 0;
77713055Ssam 
77813055Ssam 	switch (cmd) {
77913055Ssam 
78013055Ssam 	case SIOCSIFADDR:
78113055Ssam 		if (ifp->if_flags & IFF_RUNNING)
78213055Ssam 			if_rtinit(ifp, -1);	/* delete previous route */
78313062Ssam 		ecsetaddr(ifp, (struct sockaddr_in *)&ifr->ifr_addr);
78413055Ssam 		ecinit(ifp->if_unit);
78513055Ssam 		break;
78613055Ssam 
78713055Ssam 	default:
78813055Ssam 		error = EINVAL;
78913055Ssam 	}
79013055Ssam 	splx(s);
79113055Ssam 	return (error);
79213055Ssam }
79313062Ssam 
79413062Ssam ecsetaddr(ifp, sin)
79513062Ssam 	register struct ifnet *ifp;
79613062Ssam 	register struct sockaddr_in *sin;
79713062Ssam {
79813062Ssam 
79913062Ssam 	ifp->if_addr = *(struct sockaddr *)sin;
80013062Ssam 	ifp->if_net = in_netof(sin->sin_addr);
80113062Ssam 	ifp->if_host[0] = in_lnaof(sin->sin_addr);
80213062Ssam 	sin = (struct sockaddr_in *)&ifp->if_broadaddr;
80313062Ssam 	sin->sin_family = AF_INET;
80413062Ssam 	sin->sin_addr = if_makeaddr(ifp->if_net, INADDR_ANY);
80513062Ssam 	ifp->if_flags |= IFF_BROADCAST;
80613062Ssam }
807