xref: /csrg-svn/sys/vax/if/if_ec.c (revision 12771)
1*12771Ssam /*	if_ec.c	4.34	83/05/27	*/
26520Sfeldman 
36520Sfeldman #include "ec.h"
46520Sfeldman 
56520Sfeldman /*
66520Sfeldman  * 3Com Ethernet Controller interface
76520Sfeldman  */
89795Ssam #include "../machine/pte.h"
96520Sfeldman 
106520Sfeldman #include "../h/param.h"
116520Sfeldman #include "../h/systm.h"
126520Sfeldman #include "../h/mbuf.h"
136520Sfeldman #include "../h/buf.h"
146520Sfeldman #include "../h/protosw.h"
156520Sfeldman #include "../h/socket.h"
166520Sfeldman #include "../h/vmmac.h"
178461Sroot #include <errno.h>
188461Sroot 
198461Sroot #include "../net/if.h"
208461Sroot #include "../net/netisr.h"
218461Sroot #include "../net/route.h"
228417Swnj #include "../netinet/in.h"
238417Swnj #include "../netinet/in_systm.h"
248417Swnj #include "../netinet/ip.h"
258417Swnj #include "../netinet/ip_var.h"
2611574Ssam #include "../netinet/if_ether.h"
278417Swnj #include "../netpup/pup.h"
286520Sfeldman 
298461Sroot #include "../vax/cpu.h"
308461Sroot #include "../vax/mtpr.h"
318461Sroot #include "../vaxif/if_ecreg.h"
328461Sroot #include "../vaxif/if_uba.h"
338461Sroot #include "../vaxuba/ubareg.h"
348461Sroot #include "../vaxuba/ubavar.h"
358461Sroot 
367470Sfeldman #define	ECMEM	0000000
376520Sfeldman 
386520Sfeldman int	ecprobe(), ecattach(), ecrint(), ecxint(), eccollide();
396520Sfeldman struct	uba_device *ecinfo[NEC];
406520Sfeldman u_short ecstd[] = { 0 };
416520Sfeldman struct	uba_driver ecdriver =
426520Sfeldman 	{ ecprobe, 0, ecattach, 0, ecstd, "ec", ecinfo };
436520Sfeldman #define	ECUNIT(x)	minor(x)
446520Sfeldman 
456520Sfeldman int	ecinit(),ecoutput(),ecreset();
468773Sroot struct	mbuf *ecget();
476520Sfeldman 
486545Sfeldman extern struct ifnet loif;
496545Sfeldman 
506520Sfeldman /*
516520Sfeldman  * Ethernet software status per interface.
526520Sfeldman  *
536520Sfeldman  * Each interface is referenced by a network interface structure,
546520Sfeldman  * es_if, which the routing code uses to locate the interface.
556520Sfeldman  * This structure contains the output queue for the interface, its address, ...
566520Sfeldman  * We also have, for each interface, a UBA interface structure, which
576520Sfeldman  * contains information about the UNIBUS resources held by the interface:
586520Sfeldman  * map registers, buffered data paths, etc.  Information is cached in this
596520Sfeldman  * structure for use by the if_uba.c routines in running the interface
606520Sfeldman  * efficiently.
616520Sfeldman  */
626520Sfeldman struct	ec_softc {
6311574Ssam 	struct	arpcom es_ac;		/* common Ethernet structures */
6411574Ssam #define	es_if	es_ac.ac_if		/* network-visible interface */
6511574Ssam #define	es_addr	es_ac.ac_enaddr		/* hardware Ethernet address */
666520Sfeldman 	struct	ifuba es_ifuba;		/* UNIBUS resources */
676520Sfeldman 	short	es_mask;		/* mask for current output delay */
686520Sfeldman 	short	es_oactive;		/* is output active? */
698773Sroot 	u_char	*es_buf[16];		/* virtual addresses of buffers */
706520Sfeldman } ec_softc[NEC];
716520Sfeldman 
726520Sfeldman /*
736520Sfeldman  * Do output DMA to determine interface presence and
746520Sfeldman  * interrupt vector.  DMA is too short to disturb other hosts.
756520Sfeldman  */
766520Sfeldman ecprobe(reg)
776520Sfeldman 	caddr_t reg;
786520Sfeldman {
796520Sfeldman 	register int br, cvec;		/* r11, r10 value-result */
806520Sfeldman 	register struct ecdevice *addr = (struct ecdevice *)reg;
817470Sfeldman 	register caddr_t ecbuf = (caddr_t) &umem[numuba][ECMEM];
826520Sfeldman 
836520Sfeldman #ifdef lint
846520Sfeldman 	br = 0; cvec = br; br = cvec;
856520Sfeldman 	ecrint(0); ecxint(0); eccollide(0);
866520Sfeldman #endif
876520Sfeldman 	/*
886637Sfeldman 	 * Make sure memory is turned on
896637Sfeldman 	 */
906637Sfeldman 	addr->ec_rcr = EC_AROM;
916637Sfeldman 	/*
927470Sfeldman 	 * Disable map registers for ec unibus space,
937470Sfeldman 	 * but don't allocate yet.
947470Sfeldman 	 */
958773Sroot 	(void) ubamem(numuba, ECMEM, 32*2, 0);
967470Sfeldman 	/*
976520Sfeldman 	 * Check for existence of buffers on Unibus.
986520Sfeldman 	 */
998773Sroot 	if (badaddr((caddr_t)ecbuf, 2)) {
1007470Sfeldman 	bad1:
1017470Sfeldman 		printf("ec: buffer mem not found\n");
1027470Sfeldman 	bad2:
1038773Sroot 		(void) ubamem(numuba, 0, 0, 0);	/* reenable map (780 only) */
1047470Sfeldman 		addr->ec_rcr = EC_MDISAB;	/* disable memory */
1056520Sfeldman 		return (0);
1066520Sfeldman 	}
1077470Sfeldman #if VAX780
1087470Sfeldman 	if (cpu == VAX_780 && uba_hd[numuba].uh_uba->uba_sr) {
1097470Sfeldman 		uba_hd[numuba].uh_uba->uba_sr = uba_hd[numuba].uh_uba->uba_sr;
1107470Sfeldman 		goto bad1;
1117470Sfeldman 	}
1127470Sfeldman #endif
1136520Sfeldman 
1146520Sfeldman 	/*
1156520Sfeldman 	 * Tell the system that the board has memory here, so it won't
1166520Sfeldman 	 * attempt to allocate the addresses later.
1176520Sfeldman 	 */
1187470Sfeldman 	if (ubamem(numuba, ECMEM, 32*2, 1) == 0) {
1197470Sfeldman 		printf("ecprobe: cannot reserve uba addresses\n");
1207470Sfeldman 		goto bad2;
1217470Sfeldman 	}
1226520Sfeldman 
1236520Sfeldman 	/*
1246520Sfeldman 	 * Make a one byte packet in what should be buffer #0.
1256520Sfeldman 	 * Submit it for sending.  This whould cause an xmit interrupt.
1266520Sfeldman 	 * The xmit interrupt vector is 8 bytes after the receive vector,
1276520Sfeldman 	 * so adjust for this before returning.
1286520Sfeldman 	 */
1296520Sfeldman 	*(u_short *)ecbuf = (u_short) 03777;
1306520Sfeldman 	ecbuf[03777] = '\0';
1316520Sfeldman 	addr->ec_xcr = EC_XINTEN|EC_XWBN;
1326520Sfeldman 	DELAY(100000);
1336520Sfeldman 	addr->ec_xcr = EC_XCLR;
1347216Ssam 	if (cvec > 0 && cvec != 0x200) {
1357470Sfeldman 		if (cvec & 04) {	/* collision interrupt */
1367470Sfeldman 			cvec -= 04;
1377470Sfeldman 			br += 1;		/* rcv is collision + 1 */
1387470Sfeldman 		} else {		/* xmit interrupt */
1397470Sfeldman 			cvec -= 010;
1407470Sfeldman 			br += 2;		/* rcv is xmit + 2 */
1417470Sfeldman 		}
1427216Ssam 	}
1436520Sfeldman 	return (1);
1446520Sfeldman }
1456520Sfeldman 
1466520Sfeldman /*
1476520Sfeldman  * Interface exists: make available by filling in network interface
1486520Sfeldman  * record.  System will initialize the interface when it is ready
1496520Sfeldman  * to accept packets.
1506520Sfeldman  */
1516520Sfeldman ecattach(ui)
1526520Sfeldman 	struct uba_device *ui;
1536520Sfeldman {
1547216Ssam 	struct ec_softc *es = &ec_softc[ui->ui_unit];
1557216Ssam 	register struct ifnet *ifp = &es->es_if;
1566520Sfeldman 	register struct ecdevice *addr = (struct ecdevice *)ui->ui_addr;
1577216Ssam 	struct sockaddr_in *sin;
1587216Ssam 	int i, j;
1597216Ssam 	u_char *cp;
1606520Sfeldman 
1617216Ssam 	ifp->if_unit = ui->ui_unit;
1627216Ssam 	ifp->if_name = "ec";
1639745Ssam 	ifp->if_mtu = ETHERMTU;
1646520Sfeldman 
1656520Sfeldman 	/*
1667216Ssam 	 * Read the ethernet address off the board, one nibble at a time.
1676520Sfeldman 	 */
1686520Sfeldman 	addr->ec_xcr = EC_UECLR;
1696520Sfeldman 	addr->ec_rcr = EC_AROM;
17011574Ssam 	cp = es->es_addr;
1717216Ssam #define	NEXTBIT	addr->ec_rcr = EC_AROM|EC_ASTEP; addr->ec_rcr = EC_AROM
1726520Sfeldman 	for (i=0; i<6; i++) {
1736520Sfeldman 		*cp = 0;
1746520Sfeldman 		for (j=0; j<=4; j+=4) {
1756520Sfeldman 			*cp |= ((addr->ec_rcr >> 8) & 0xf) << j;
1767216Ssam 			NEXTBIT; NEXTBIT; NEXTBIT; NEXTBIT;
1776520Sfeldman 		}
1786520Sfeldman 		cp++;
1796520Sfeldman 	}
1806520Sfeldman 	sin = (struct sockaddr_in *)&es->es_if.if_addr;
1816520Sfeldman 	sin->sin_family = AF_INET;
182*12771Ssam 	sin->sin_addr = arpmyaddr((struct arpcom *)0);
1837216Ssam 	ifp->if_init = ecinit;
1847216Ssam 	ifp->if_output = ecoutput;
1858977Sroot 	ifp->if_reset = ecreset;
1866520Sfeldman 	for (i=0; i<16; i++)
1878773Sroot 		es->es_buf[i] = (u_char *)&umem[ui->ui_ubanum][ECMEM+2048*i];
1887216Ssam 	if_attach(ifp);
1896520Sfeldman }
1906520Sfeldman 
1916520Sfeldman /*
1926520Sfeldman  * Reset of interface after UNIBUS reset.
1936520Sfeldman  * If interface is on specified uba, reset its state.
1946520Sfeldman  */
1956520Sfeldman ecreset(unit, uban)
1966520Sfeldman 	int unit, uban;
1976520Sfeldman {
1986520Sfeldman 	register struct uba_device *ui;
1996520Sfeldman 
2006520Sfeldman 	if (unit >= NEC || (ui = ecinfo[unit]) == 0 || ui->ui_alive == 0 ||
2016520Sfeldman 	    ui->ui_ubanum != uban)
2026520Sfeldman 		return;
2036520Sfeldman 	printf(" ec%d", unit);
2048773Sroot 	(void) ubamem(uban, ECMEM, 32*2, 0);	/* mr disable (no alloc) */
2056520Sfeldman 	ecinit(unit);
2066520Sfeldman }
2076520Sfeldman 
2086520Sfeldman /*
2096520Sfeldman  * Initialization of interface; clear recorded pending
2106520Sfeldman  * operations, and reinitialize UNIBUS usage.
2116520Sfeldman  */
2126520Sfeldman ecinit(unit)
2136520Sfeldman 	int unit;
2146520Sfeldman {
2157216Ssam 	struct ec_softc *es = &ec_softc[unit];
2167216Ssam 	struct ecdevice *addr;
2177216Ssam 	int i, s;
21811574Ssam 	register struct ifnet *ifp = &es->es_if;
21911574Ssam 	register struct sockaddr_in *sin, *sinb;
2206520Sfeldman 
22111574Ssam 	sin = (struct sockaddr_in *)&ifp->if_addr;
22211574Ssam 	if (sin->sin_addr.s_addr == 0)	/* if address still unknown */
22311574Ssam 		return;
22411574Ssam 	ifp->if_net = in_netof(sin->sin_addr);
22511574Ssam 	ifp->if_host[0] = in_lnaof(sin->sin_addr);
22611574Ssam 	sinb = (struct sockaddr_in *)&ifp->if_broadaddr;
22711574Ssam 	sinb->sin_family = AF_INET;
22811574Ssam 	sinb->sin_addr = if_makeaddr(ifp->if_net, INADDR_ANY);
22911574Ssam 	ifp->if_flags = IFF_BROADCAST;
23011574Ssam 
2316520Sfeldman 	/*
2327217Sfeldman 	 * Hang receive buffers and start any pending writes.
2336637Sfeldman 	 * Writing into the rcr also makes sure the memory
2346637Sfeldman 	 * is turned on.
2356520Sfeldman 	 */
2367216Ssam 	addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
2376520Sfeldman 	s = splimp();
2386520Sfeldman 	for (i=ECRHBF; i>=ECRLBF; i--)
2396520Sfeldman 		addr->ec_rcr = EC_READ|i;
2407217Sfeldman 	es->es_oactive = 0;
2417217Sfeldman 	es->es_mask = ~0;
2426520Sfeldman 	es->es_if.if_flags |= IFF_UP;
2437217Sfeldman 	if (es->es_if.if_snd.ifq_head)
2447217Sfeldman 		ecstart(unit);
2456520Sfeldman 	splx(s);
2467150Swnj 	if_rtinit(&es->es_if, RTF_UP);
24711574Ssam 	arpattach(&es->es_ac);
24811574Ssam 	arpwhohas(&es->es_ac, &sin->sin_addr);
2496520Sfeldman }
2506520Sfeldman 
2516520Sfeldman /*
2526520Sfeldman  * Start or restart output on interface.
2536520Sfeldman  * If interface is already active, then this is a retransmit
2546545Sfeldman  * after a collision, and just restuff registers.
2556520Sfeldman  * If interface is not already active, get another datagram
2566520Sfeldman  * to send off of the interface queue, and map it to the interface
2576520Sfeldman  * before starting the output.
2586520Sfeldman  */
2596520Sfeldman ecstart(dev)
2606520Sfeldman 	dev_t dev;
2616520Sfeldman {
2628773Sroot         int unit = ECUNIT(dev);
2637216Ssam 	struct ec_softc *es = &ec_softc[unit];
2647216Ssam 	struct ecdevice *addr;
2656520Sfeldman 	struct mbuf *m;
2666520Sfeldman 
2676520Sfeldman 	if (es->es_oactive)
2686520Sfeldman 		goto restart;
2696520Sfeldman 
2706520Sfeldman 	IF_DEQUEUE(&es->es_if.if_snd, m);
2716520Sfeldman 	if (m == 0) {
2726520Sfeldman 		es->es_oactive = 0;
2736520Sfeldman 		return;
2746520Sfeldman 	}
2756520Sfeldman 	ecput(es->es_buf[ECTBF], m);
2766520Sfeldman 
2776520Sfeldman restart:
2787216Ssam 	addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
2796520Sfeldman 	addr->ec_xcr = EC_WRITE|ECTBF;
2806520Sfeldman 	es->es_oactive = 1;
2816520Sfeldman }
2826520Sfeldman 
2836520Sfeldman /*
2846520Sfeldman  * Ethernet interface transmitter interrupt.
2856520Sfeldman  * Start another output if more data to send.
2866520Sfeldman  */
2876520Sfeldman ecxint(unit)
2886520Sfeldman 	int unit;
2896520Sfeldman {
2906520Sfeldman 	register struct ec_softc *es = &ec_softc[unit];
2917216Ssam 	register struct ecdevice *addr =
2927216Ssam 		(struct ecdevice *)ecinfo[unit]->ui_addr;
2936520Sfeldman 
2946520Sfeldman 	if (es->es_oactive == 0)
2956520Sfeldman 		return;
2967216Ssam 	if ((addr->ec_xcr&EC_XDONE) == 0 || (addr->ec_xcr&EC_XBN) != ECTBF) {
2977216Ssam 		printf("ec%d: stray xmit interrupt, xcr=%b\n", unit,
2987216Ssam 			addr->ec_xcr, EC_XBITS);
2997216Ssam 		es->es_oactive = 0;
3007216Ssam 		addr->ec_xcr = EC_XCLR;
3017216Ssam 		return;
3027216Ssam 	}
3036520Sfeldman 	es->es_if.if_opackets++;
3046520Sfeldman 	es->es_oactive = 0;
3056520Sfeldman 	es->es_mask = ~0;
3066520Sfeldman 	addr->ec_xcr = EC_XCLR;
3077216Ssam 	if (es->es_if.if_snd.ifq_head)
3087216Ssam 		ecstart(unit);
3096520Sfeldman }
3106520Sfeldman 
3116520Sfeldman /*
3126520Sfeldman  * Collision on ethernet interface.  Do exponential
3136520Sfeldman  * backoff, and retransmit.  If have backed off all
3146520Sfeldman  * the way print warning diagnostic, and drop packet.
3156520Sfeldman  */
3166520Sfeldman eccollide(unit)
3176520Sfeldman 	int unit;
3186520Sfeldman {
3196520Sfeldman 	struct ec_softc *es = &ec_softc[unit];
3206520Sfeldman 
3216520Sfeldman 	es->es_if.if_collisions++;
3227216Ssam 	if (es->es_oactive)
3237216Ssam 		ecdocoll(unit);
3246520Sfeldman }
3256520Sfeldman 
3266520Sfeldman ecdocoll(unit)
3276520Sfeldman 	int unit;
3286520Sfeldman {
3296520Sfeldman 	register struct ec_softc *es = &ec_softc[unit];
3306545Sfeldman 	register struct ecdevice *addr =
3316545Sfeldman 	    (struct ecdevice *)ecinfo[unit]->ui_addr;
3326545Sfeldman 	register i;
3336545Sfeldman 	int delay;
3346520Sfeldman 
3356520Sfeldman 	/*
3366520Sfeldman 	 * Es_mask is a 16 bit number with n low zero bits, with
3376520Sfeldman 	 * n the number of backoffs.  When es_mask is 0 we have
3386520Sfeldman 	 * backed off 16 times, and give up.
3396520Sfeldman 	 */
3406520Sfeldman 	if (es->es_mask == 0) {
3416545Sfeldman 		es->es_if.if_oerrors++;
3426520Sfeldman 		printf("ec%d: send error\n", unit);
3436520Sfeldman 		/*
3446545Sfeldman 		 * Reset interface, then requeue rcv buffers.
3456545Sfeldman 		 * Some incoming packets may be lost, but that
3466545Sfeldman 		 * can't be helped.
3476520Sfeldman 		 */
3486545Sfeldman 		addr->ec_xcr = EC_UECLR;
3496545Sfeldman 		for (i=ECRHBF; i>=ECRLBF; i--)
3506545Sfeldman 			addr->ec_rcr = EC_READ|i;
3516545Sfeldman 		/*
3526545Sfeldman 		 * Reset and transmit next packet (if any).
3536545Sfeldman 		 */
3546545Sfeldman 		es->es_oactive = 0;
3556545Sfeldman 		es->es_mask = ~0;
3566545Sfeldman 		if (es->es_if.if_snd.ifq_head)
3576545Sfeldman 			ecstart(unit);
3586520Sfeldman 		return;
3596520Sfeldman 	}
3606520Sfeldman 	/*
3616545Sfeldman 	 * Do exponential backoff.  Compute delay based on low bits
3626545Sfeldman 	 * of the interval timer.  Then delay for that number of
3636545Sfeldman 	 * slot times.  A slot time is 51.2 microseconds (rounded to 51).
3646545Sfeldman 	 * This does not take into account the time already used to
3656545Sfeldman 	 * process the interrupt.
3666520Sfeldman 	 */
3676520Sfeldman 	es->es_mask <<= 1;
3686545Sfeldman 	delay = mfpr(ICR) &~ es->es_mask;
3696545Sfeldman 	DELAY(delay * 51);
3706520Sfeldman 	/*
3716545Sfeldman 	 * Clear the controller's collision flag, thus enabling retransmit.
3726520Sfeldman 	 */
3737470Sfeldman 	addr->ec_xcr = EC_CLEAR;
3746520Sfeldman }
3756520Sfeldman 
3766520Sfeldman /*
3776520Sfeldman  * Ethernet interface receiver interrupt.
3786520Sfeldman  * If input error just drop packet.
3796520Sfeldman  * Otherwise purge input buffered data path and examine
3806520Sfeldman  * packet to determine type.  If can't determine length
3816520Sfeldman  * from type, then have to drop packet.  Othewise decapsulate
3826520Sfeldman  * packet based on type and pass to type specific higher-level
3836520Sfeldman  * input routine.
3846520Sfeldman  */
3856520Sfeldman ecrint(unit)
3866520Sfeldman 	int unit;
3876520Sfeldman {
3886520Sfeldman 	struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
3896520Sfeldman 
3906520Sfeldman 	while (addr->ec_rcr & EC_RDONE)
3916520Sfeldman 		ecread(unit);
3926520Sfeldman }
3936520Sfeldman 
3946520Sfeldman ecread(unit)
3956520Sfeldman 	int unit;
3966520Sfeldman {
3976520Sfeldman 	register struct ec_softc *es = &ec_softc[unit];
3986520Sfeldman 	struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
3999745Ssam 	register struct ether_header *ec;
4006520Sfeldman     	struct mbuf *m;
4018773Sroot 	int len, off, resid, ecoff, rbuf;
4026520Sfeldman 	register struct ifqueue *inq;
4038773Sroot 	u_char *ecbuf;
4046520Sfeldman 
4056520Sfeldman 	es->es_if.if_ipackets++;
4068773Sroot 	rbuf = addr->ec_rcr & EC_RBN;
4078773Sroot 	if (rbuf < ECRLBF || rbuf > ECRHBF)
4086520Sfeldman 		panic("ecrint");
4098773Sroot 	ecbuf = es->es_buf[rbuf];
4106520Sfeldman 	ecoff = *(short *)ecbuf;
4116545Sfeldman 	if (ecoff <= ECRDOFF || ecoff > 2046) {
4126520Sfeldman 		es->es_if.if_ierrors++;
4136520Sfeldman #ifdef notdef
4146520Sfeldman 		if (es->es_if.if_ierrors % 100 == 0)
4156520Sfeldman 			printf("ec%d: += 100 input errors\n", unit);
4166520Sfeldman #endif
4176520Sfeldman 		goto setup;
4186520Sfeldman 	}
4196520Sfeldman 
4206520Sfeldman 	/*
4216520Sfeldman 	 * Get input data length.
4226520Sfeldman 	 * Get pointer to ethernet header (in input buffer).
4236520Sfeldman 	 * Deal with trailer protocol: if type is PUP trailer
4246520Sfeldman 	 * get true type from first 16-bit word past data.
4256520Sfeldman 	 * Remember that type was trailer by setting off.
4266520Sfeldman 	 */
4279745Ssam 	len = ecoff - ECRDOFF - sizeof (struct ether_header);
4289745Ssam 	ec = (struct ether_header *)(ecbuf + ECRDOFF);
4299745Ssam 	ec->ether_type = ntohs((u_short)ec->ether_type);
4306520Sfeldman #define	ecdataaddr(ec, off, type)	((type)(((caddr_t)((ec)+1)+(off))))
4319745Ssam 	if (ec->ether_type >= ETHERPUP_TRAIL &&
4329745Ssam 	    ec->ether_type < ETHERPUP_TRAIL+ETHERPUP_NTRAILER) {
4339745Ssam 		off = (ec->ether_type - ETHERPUP_TRAIL) * 512;
4349745Ssam 		if (off >= ETHERMTU)
4356520Sfeldman 			goto setup;		/* sanity */
4369745Ssam 		ec->ether_type = ntohs(*ecdataaddr(ec, off, u_short *));
4379745Ssam 		resid = ntohs(*(ecdataaddr(ec, off+2, u_short *)));
4386520Sfeldman 		if (off + resid > len)
4396520Sfeldman 			goto setup;		/* sanity */
4406520Sfeldman 		len = off + resid;
4416520Sfeldman 	} else
4426520Sfeldman 		off = 0;
4436520Sfeldman 	if (len == 0)
4446520Sfeldman 		goto setup;
4456520Sfeldman 
4466520Sfeldman 	/*
4476520Sfeldman 	 * Pull packet off interface.  Off is nonzero if packet
4486520Sfeldman 	 * has trailing header; ecget will then force this header
4496520Sfeldman 	 * information to be at the front, but we still have to drop
4506520Sfeldman 	 * the type and length which are at the front of any trailer data.
4516520Sfeldman 	 */
4526520Sfeldman 	m = ecget(ecbuf, len, off);
4536520Sfeldman 	if (m == 0)
4546520Sfeldman 		goto setup;
4556520Sfeldman 	if (off) {
4566520Sfeldman 		m->m_off += 2 * sizeof (u_short);
4576520Sfeldman 		m->m_len -= 2 * sizeof (u_short);
4586520Sfeldman 	}
4599745Ssam 	switch (ec->ether_type) {
4606520Sfeldman 
4616520Sfeldman #ifdef INET
4629745Ssam 	case ETHERPUP_IPTYPE:
4636520Sfeldman 		schednetisr(NETISR_IP);
4646520Sfeldman 		inq = &ipintrq;
4656520Sfeldman 		break;
46611574Ssam 
46711574Ssam 	case ETHERPUP_ARPTYPE:
46811574Ssam 		arpinput(&es->es_ac, m);
46911574Ssam 		return;
4706520Sfeldman #endif
4716520Sfeldman 	default:
4726520Sfeldman 		m_freem(m);
4736520Sfeldman 		goto setup;
4746520Sfeldman 	}
4756520Sfeldman 
4766520Sfeldman 	if (IF_QFULL(inq)) {
4776520Sfeldman 		IF_DROP(inq);
4786520Sfeldman 		m_freem(m);
4797216Ssam 		goto setup;
4807216Ssam 	}
4817216Ssam 	IF_ENQUEUE(inq, m);
4826520Sfeldman 
4836520Sfeldman setup:
4846520Sfeldman 	/*
4856520Sfeldman 	 * Reset for next packet.
4866520Sfeldman 	 */
4878773Sroot 	addr->ec_rcr = EC_READ|EC_RCLR|rbuf;
4886520Sfeldman }
4896520Sfeldman 
4906520Sfeldman /*
4916520Sfeldman  * Ethernet output routine.
4926520Sfeldman  * Encapsulate a packet of type family for the local net.
4936520Sfeldman  * Use trailer local net encapsulation if enough data in first
4946520Sfeldman  * packet leaves a multiple of 512 bytes of data in remainder.
4956545Sfeldman  * If destination is this address or broadcast, send packet to
4966545Sfeldman  * loop device to kludge around the fact that 3com interfaces can't
4976545Sfeldman  * talk to themselves.
4986520Sfeldman  */
4996520Sfeldman ecoutput(ifp, m0, dst)
5006520Sfeldman 	struct ifnet *ifp;
5016520Sfeldman 	struct mbuf *m0;
5026520Sfeldman 	struct sockaddr *dst;
5036520Sfeldman {
50411574Ssam 	int type, s, error;
50511574Ssam 	u_char edst[6];
50611574Ssam 	struct in_addr idst;
5076520Sfeldman 	register struct ec_softc *es = &ec_softc[ifp->if_unit];
5086520Sfeldman 	register struct mbuf *m = m0;
5099745Ssam 	register struct ether_header *ec;
510*12771Ssam 	register int off;
51111574Ssam 	struct mbuf *mcopy = (struct mbuf *)0;
5126520Sfeldman 
5136520Sfeldman 	switch (dst->sa_family) {
5146520Sfeldman 
5156520Sfeldman #ifdef INET
5166520Sfeldman 	case AF_INET:
51711574Ssam 		idst = ((struct sockaddr_in *)dst)->sin_addr;
51811574Ssam 		if (!arpresolve(&es->es_ac, m, &idst, edst))
51911574Ssam 			return (0);	/* if not yet resolved */
52011574Ssam 		if (in_lnaof(idst) == INADDR_ANY)
5218838Sroot 			mcopy = m_copy(m, 0, (int)M_COPYALL);
5226520Sfeldman 		off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len;
5236520Sfeldman 		if (off > 0 && (off & 0x1ff) == 0 &&
5246520Sfeldman 		    m->m_off >= MMINOFF + 2 * sizeof (u_short)) {
5259745Ssam 			type = ETHERPUP_TRAIL + (off>>9);
5266520Sfeldman 			m->m_off -= 2 * sizeof (u_short);
5276520Sfeldman 			m->m_len += 2 * sizeof (u_short);
5289745Ssam 			*mtod(m, u_short *) = ntohs((u_short)ETHERPUP_IPTYPE);
5299745Ssam 			*(mtod(m, u_short *) + 1) = ntohs((u_short)m->m_len);
5306520Sfeldman 			goto gottrailertype;
5316520Sfeldman 		}
5329745Ssam 		type = ETHERPUP_IPTYPE;
5336520Sfeldman 		off = 0;
5346520Sfeldman 		goto gottype;
5356520Sfeldman #endif
5366520Sfeldman 
53711574Ssam 	case AF_UNSPEC:
53811574Ssam 		ec = (struct ether_header *)dst->sa_data;
539*12771Ssam 		bcopy((caddr_t)ec->ether_dhost, (caddr_t)edst, sizeof (edst));
54011574Ssam 		type = ec->ether_type;
54111574Ssam 		goto gottype;
54211574Ssam 
5436520Sfeldman 	default:
5446520Sfeldman 		printf("ec%d: can't handle af%d\n", ifp->if_unit,
5456520Sfeldman 			dst->sa_family);
5466520Sfeldman 		error = EAFNOSUPPORT;
5476520Sfeldman 		goto bad;
5486520Sfeldman 	}
5496520Sfeldman 
5506520Sfeldman gottrailertype:
5516520Sfeldman 	/*
5526520Sfeldman 	 * Packet to be sent as trailer: move first packet
5536520Sfeldman 	 * (control information) to end of chain.
5546520Sfeldman 	 */
5556520Sfeldman 	while (m->m_next)
5566520Sfeldman 		m = m->m_next;
5576520Sfeldman 	m->m_next = m0;
5586520Sfeldman 	m = m0->m_next;
5596520Sfeldman 	m0->m_next = 0;
5606520Sfeldman 	m0 = m;
5616520Sfeldman 
5626520Sfeldman gottype:
5636520Sfeldman 	/*
5646520Sfeldman 	 * Add local net header.  If no space in first mbuf,
5656520Sfeldman 	 * allocate another.
5666520Sfeldman 	 */
5676520Sfeldman 	if (m->m_off > MMAXOFF ||
5689745Ssam 	    MMINOFF + sizeof (struct ether_header) > m->m_off) {
5699648Ssam 		m = m_get(M_DONTWAIT, MT_HEADER);
5706520Sfeldman 		if (m == 0) {
5716520Sfeldman 			error = ENOBUFS;
5726520Sfeldman 			goto bad;
5736520Sfeldman 		}
5746520Sfeldman 		m->m_next = m0;
5756520Sfeldman 		m->m_off = MMINOFF;
5769745Ssam 		m->m_len = sizeof (struct ether_header);
5776520Sfeldman 	} else {
5789745Ssam 		m->m_off -= sizeof (struct ether_header);
5799745Ssam 		m->m_len += sizeof (struct ether_header);
5806520Sfeldman 	}
5819745Ssam 	ec = mtod(m, struct ether_header *);
582*12771Ssam 	bcopy((caddr_t)edst, (caddr_t)ec->ether_dhost, sizeof (edst));
5839745Ssam 	ec->ether_type = htons((u_short)type);
58411574Ssam 	bcopy((caddr_t)es->es_addr, (caddr_t)ec->ether_shost, 6);
5856520Sfeldman 
5866520Sfeldman 	/*
5876520Sfeldman 	 * Queue message on interface, and start output if interface
5886520Sfeldman 	 * not yet active.
5896520Sfeldman 	 */
5906520Sfeldman 	s = splimp();
5916520Sfeldman 	if (IF_QFULL(&ifp->if_snd)) {
5926520Sfeldman 		IF_DROP(&ifp->if_snd);
5936520Sfeldman 		error = ENOBUFS;
5946520Sfeldman 		goto qfull;
5956520Sfeldman 	}
5966520Sfeldman 	IF_ENQUEUE(&ifp->if_snd, m);
5976520Sfeldman 	if (es->es_oactive == 0)
5986520Sfeldman 		ecstart(ifp->if_unit);
5996520Sfeldman 	splx(s);
600*12771Ssam 	return (mcopy ? looutput(&loif, mcopy, dst) : 0);
6017216Ssam 
6026520Sfeldman qfull:
6036520Sfeldman 	m0 = m;
6046520Sfeldman 	splx(s);
6056520Sfeldman bad:
6066520Sfeldman 	m_freem(m0);
607*12771Ssam 	return (error);
6086520Sfeldman }
6096520Sfeldman 
6106520Sfeldman /*
6119177Ssam  * Routine to copy from mbuf chain to transmit
6127216Ssam  * buffer in UNIBUS memory.
6139177Ssam  * If packet size is less than the minimum legal size,
6149177Ssam  * the buffer is expanded.  We probably should zero out the extra
6159177Ssam  * bytes for security, but that would slow things down.
6166520Sfeldman  */
6176520Sfeldman ecput(ecbuf, m)
6187216Ssam 	u_char *ecbuf;
6196520Sfeldman 	struct mbuf *m;
6206520Sfeldman {
6216520Sfeldman 	register struct mbuf *mp;
6227216Ssam 	register int off;
6237263Ssam 	u_char *bp;
6246520Sfeldman 
6257216Ssam 	for (off = 2048, mp = m; mp; mp = mp->m_next)
6267216Ssam 		off -= mp->m_len;
6279745Ssam 	if (2048 - off < ETHERMIN + sizeof (struct ether_header))
6289745Ssam 		off = 2048 - ETHERMIN - sizeof (struct ether_header);
6297216Ssam 	*(u_short *)ecbuf = off;
6307216Ssam 	bp = (u_char *)(ecbuf + off);
6317263Ssam 	for (mp = m; mp; mp = mp->m_next) {
6327263Ssam 		register unsigned len = mp->m_len;
6337263Ssam 		u_char *mcp;
6347216Ssam 
6357216Ssam 		if (len == 0)
6367216Ssam 			continue;
6377216Ssam 		mcp = mtod(mp, u_char *);
6387216Ssam 		if ((unsigned)bp & 01) {
6397032Swnj 			*bp++ = *mcp++;
6407216Ssam 			len--;
6417032Swnj 		}
6427263Ssam 		if (off = (len >> 1)) {
6437263Ssam 			register u_short *to, *from;
6447263Ssam 
6457263Ssam 			to = (u_short *)bp;
6467263Ssam 			from = (u_short *)mcp;
6477263Ssam 			do
6487263Ssam 				*to++ = *from++;
6497263Ssam 			while (--off > 0);
6507263Ssam 			bp = (u_char *)to,
6517263Ssam 			mcp = (u_char *)from;
6527032Swnj 		}
6537263Ssam 		if (len & 01)
6546520Sfeldman 			*bp++ = *mcp++;
6556520Sfeldman 	}
6567263Ssam 	m_freem(m);
6576520Sfeldman }
6586520Sfeldman 
6596520Sfeldman /*
6606520Sfeldman  * Routine to copy from UNIBUS memory into mbufs.
6616520Sfeldman  * Similar in spirit to if_rubaget.
6627032Swnj  *
6637032Swnj  * Warning: This makes the fairly safe assumption that
6647032Swnj  * mbufs have even lengths.
6656520Sfeldman  */
6666520Sfeldman struct mbuf *
6676520Sfeldman ecget(ecbuf, totlen, off0)
6687263Ssam 	u_char *ecbuf;
6696520Sfeldman 	int totlen, off0;
6706520Sfeldman {
6717263Ssam 	register struct mbuf *m;
6727263Ssam 	struct mbuf *top = 0, **mp = &top;
6737263Ssam 	register int off = off0, len;
6747263Ssam 	u_char *cp;
6756520Sfeldman 
6769745Ssam 	cp = ecbuf + ECRDOFF + sizeof (struct ether_header);
6776520Sfeldman 	while (totlen > 0) {
6787263Ssam 		register int words;
6797263Ssam 		u_char *mcp;
6807263Ssam 
6819648Ssam 		MGET(m, M_DONTWAIT, MT_DATA);
6826520Sfeldman 		if (m == 0)
6836520Sfeldman 			goto bad;
6846520Sfeldman 		if (off) {
6856520Sfeldman 			len = totlen - off;
6869745Ssam 			cp = ecbuf + ECRDOFF +
6879745Ssam 				sizeof (struct ether_header) + off;
6886520Sfeldman 		} else
6896520Sfeldman 			len = totlen;
6906520Sfeldman 		if (len >= CLBYTES) {
6916520Sfeldman 			struct mbuf *p;
6926520Sfeldman 
6936520Sfeldman 			MCLGET(p, 1);
6946520Sfeldman 			if (p != 0) {
6956520Sfeldman 				m->m_len = len = CLBYTES;
6966520Sfeldman 				m->m_off = (int)p - (int)m;
6976520Sfeldman 			} else {
6986520Sfeldman 				m->m_len = len = MIN(MLEN, len);
6996520Sfeldman 				m->m_off = MMINOFF;
7006520Sfeldman 			}
7016520Sfeldman 		} else {
7026520Sfeldman 			m->m_len = len = MIN(MLEN, len);
7036520Sfeldman 			m->m_off = MMINOFF;
7046520Sfeldman 		}
7057263Ssam 		mcp = mtod(m, u_char *);
7067263Ssam 		if (words = (len >> 1)) {
7077263Ssam 			register u_short *to, *from;
7087263Ssam 
7097263Ssam 			to = (u_short *)mcp;
7107263Ssam 			from = (u_short *)cp;
7117263Ssam 			do
7127263Ssam 				*to++ = *from++;
7137263Ssam 			while (--words > 0);
7147263Ssam 			mcp = (u_char *)to;
7157263Ssam 			cp = (u_char *)from;
7167032Swnj 		}
7177216Ssam 		if (len & 01)
7186520Sfeldman 			*mcp++ = *cp++;
7196520Sfeldman 		*mp = m;
7206520Sfeldman 		mp = &m->m_next;
7217263Ssam 		if (off == 0) {
7226520Sfeldman 			totlen -= len;
7237263Ssam 			continue;
7247263Ssam 		}
7257263Ssam 		off += len;
7267263Ssam 		if (off == totlen) {
7279745Ssam 			cp = ecbuf + ECRDOFF + sizeof (struct ether_header);
7287263Ssam 			off = 0;
7297263Ssam 			totlen = off0;
7307263Ssam 		}
7316520Sfeldman 	}
7326520Sfeldman 	return (top);
7336520Sfeldman bad:
7346520Sfeldman 	m_freem(top);
7356520Sfeldman 	return (0);
7366520Sfeldman }
737