xref: /csrg-svn/sys/vax/if/if_ec.c (revision 9745)
1*9745Ssam /*	if_ec.c	4.31	82/12/16	*/
26520Sfeldman 
36520Sfeldman #include "ec.h"
46520Sfeldman 
56520Sfeldman /*
66520Sfeldman  * 3Com Ethernet Controller interface
76520Sfeldman  */
86520Sfeldman 
96520Sfeldman #include "../h/param.h"
106520Sfeldman #include "../h/systm.h"
116520Sfeldman #include "../h/mbuf.h"
126520Sfeldman #include "../h/pte.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"
268417Swnj #include "../netpup/pup.h"
276520Sfeldman 
288461Sroot #include "../vax/cpu.h"
298461Sroot #include "../vax/mtpr.h"
30*9745Ssam #include "../vaxif/if_ether.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 };
436892Sfeldman u_char	ec_iltop[3] = { 0x02, 0x07, 0x01 };
44*9745Ssam u_char	ecbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
456520Sfeldman #define	ECUNIT(x)	minor(x)
466520Sfeldman 
476520Sfeldman int	ecinit(),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 {
656520Sfeldman 	struct	ifnet es_if;		/* network-visible interface */
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 	u_char	es_enaddr[6];		/* board's ethernet address */
716520Sfeldman } ec_softc[NEC];
726520Sfeldman 
736520Sfeldman /*
746520Sfeldman  * Do output DMA to determine interface presence and
756520Sfeldman  * interrupt vector.  DMA is too short to disturb other hosts.
766520Sfeldman  */
776520Sfeldman ecprobe(reg)
786520Sfeldman 	caddr_t reg;
796520Sfeldman {
806520Sfeldman 	register int br, cvec;		/* r11, r10 value-result */
816520Sfeldman 	register struct ecdevice *addr = (struct ecdevice *)reg;
827470Sfeldman 	register caddr_t ecbuf = (caddr_t) &umem[numuba][ECMEM];
836520Sfeldman 
846520Sfeldman #ifdef lint
856520Sfeldman 	br = 0; cvec = br; br = cvec;
866520Sfeldman 	ecrint(0); ecxint(0); eccollide(0);
876520Sfeldman #endif
886520Sfeldman 	/*
896637Sfeldman 	 * Make sure memory is turned on
906637Sfeldman 	 */
916637Sfeldman 	addr->ec_rcr = EC_AROM;
926637Sfeldman 	/*
937470Sfeldman 	 * Disable map registers for ec unibus space,
947470Sfeldman 	 * but don't allocate yet.
957470Sfeldman 	 */
968773Sroot 	(void) ubamem(numuba, ECMEM, 32*2, 0);
977470Sfeldman 	/*
986520Sfeldman 	 * Check for existence of buffers on Unibus.
996520Sfeldman 	 */
1008773Sroot 	if (badaddr((caddr_t)ecbuf, 2)) {
1017470Sfeldman 	bad1:
1027470Sfeldman 		printf("ec: buffer mem not found\n");
1037470Sfeldman 	bad2:
1048773Sroot 		(void) ubamem(numuba, 0, 0, 0);	/* reenable map (780 only) */
1057470Sfeldman 		addr->ec_rcr = EC_MDISAB;	/* disable memory */
1066520Sfeldman 		return (0);
1076520Sfeldman 	}
1087470Sfeldman #if VAX780
1097470Sfeldman 	if (cpu == VAX_780 && uba_hd[numuba].uh_uba->uba_sr) {
1107470Sfeldman 		uba_hd[numuba].uh_uba->uba_sr = uba_hd[numuba].uh_uba->uba_sr;
1117470Sfeldman 		goto bad1;
1127470Sfeldman 	}
1137470Sfeldman #endif
1146520Sfeldman 
1156520Sfeldman 	/*
1166520Sfeldman 	 * Tell the system that the board has memory here, so it won't
1176520Sfeldman 	 * attempt to allocate the addresses later.
1186520Sfeldman 	 */
1197470Sfeldman 	if (ubamem(numuba, ECMEM, 32*2, 1) == 0) {
1207470Sfeldman 		printf("ecprobe: cannot reserve uba addresses\n");
1217470Sfeldman 		goto bad2;
1227470Sfeldman 	}
1236520Sfeldman 
1246520Sfeldman 	/*
1256520Sfeldman 	 * Make a one byte packet in what should be buffer #0.
1266520Sfeldman 	 * Submit it for sending.  This whould cause an xmit interrupt.
1276520Sfeldman 	 * The xmit interrupt vector is 8 bytes after the receive vector,
1286520Sfeldman 	 * so adjust for this before returning.
1296520Sfeldman 	 */
1306520Sfeldman 	*(u_short *)ecbuf = (u_short) 03777;
1316520Sfeldman 	ecbuf[03777] = '\0';
1326520Sfeldman 	addr->ec_xcr = EC_XINTEN|EC_XWBN;
1336520Sfeldman 	DELAY(100000);
1346520Sfeldman 	addr->ec_xcr = EC_XCLR;
1357216Ssam 	if (cvec > 0 && cvec != 0x200) {
1367470Sfeldman 		if (cvec & 04) {	/* collision interrupt */
1377470Sfeldman 			cvec -= 04;
1387470Sfeldman 			br += 1;		/* rcv is collision + 1 */
1397470Sfeldman 		} else {		/* xmit interrupt */
1407470Sfeldman 			cvec -= 010;
1417470Sfeldman 			br += 2;		/* rcv is xmit + 2 */
1427470Sfeldman 		}
1437216Ssam 	}
1446520Sfeldman 	return (1);
1456520Sfeldman }
1466520Sfeldman 
1476520Sfeldman /*
1486520Sfeldman  * Interface exists: make available by filling in network interface
1496520Sfeldman  * record.  System will initialize the interface when it is ready
1506520Sfeldman  * to accept packets.
1516520Sfeldman  */
1526520Sfeldman ecattach(ui)
1536520Sfeldman 	struct uba_device *ui;
1546520Sfeldman {
1557216Ssam 	struct ec_softc *es = &ec_softc[ui->ui_unit];
1567216Ssam 	register struct ifnet *ifp = &es->es_if;
1576520Sfeldman 	register struct ecdevice *addr = (struct ecdevice *)ui->ui_addr;
1587216Ssam 	struct sockaddr_in *sin;
1597216Ssam 	int i, j;
1607216Ssam 	u_char *cp;
1616520Sfeldman 
1627216Ssam 	ifp->if_unit = ui->ui_unit;
1637216Ssam 	ifp->if_name = "ec";
164*9745Ssam 	ifp->if_mtu = ETHERMTU;
1657216Ssam 	ifp->if_net = ui->ui_flags;
1666520Sfeldman 
1676520Sfeldman 	/*
1687216Ssam 	 * Read the ethernet address off the board, one nibble at a time.
1696520Sfeldman 	 */
1706520Sfeldman 	addr->ec_xcr = EC_UECLR;
1716520Sfeldman 	addr->ec_rcr = EC_AROM;
1726520Sfeldman 	cp = es->es_enaddr;
1737216Ssam #define	NEXTBIT	addr->ec_rcr = EC_AROM|EC_ASTEP; addr->ec_rcr = EC_AROM
1746520Sfeldman 	for (i=0; i<6; i++) {
1756520Sfeldman 		*cp = 0;
1766520Sfeldman 		for (j=0; j<=4; j+=4) {
1776520Sfeldman 			*cp |= ((addr->ec_rcr >> 8) & 0xf) << j;
1787216Ssam 			NEXTBIT; NEXTBIT; NEXTBIT; NEXTBIT;
1796520Sfeldman 		}
1806520Sfeldman 		cp++;
1816520Sfeldman 	}
1827216Ssam #ifdef notdef
1836520Sfeldman 	printf("ec%d: addr=%x:%x:%x:%x:%x:%x\n", ui->ui_unit,
1846520Sfeldman 		es->es_enaddr[0]&0xff, es->es_enaddr[1]&0xff,
1856520Sfeldman 		es->es_enaddr[2]&0xff, es->es_enaddr[3]&0xff,
1866520Sfeldman 		es->es_enaddr[4]&0xff, es->es_enaddr[5]&0xff);
1877216Ssam #endif
1887216Ssam 	ifp->if_host[0] = ((es->es_enaddr[3]&0xff)<<16) |
1896520Sfeldman 	    ((es->es_enaddr[4]&0xff)<<8) | (es->es_enaddr[5]&0xff);
1906520Sfeldman 	sin = (struct sockaddr_in *)&es->es_if.if_addr;
1916520Sfeldman 	sin->sin_family = AF_INET;
1927216Ssam 	sin->sin_addr = if_makeaddr(ifp->if_net, ifp->if_host[0]);
1936520Sfeldman 
1947216Ssam 	sin = (struct sockaddr_in *)&ifp->if_broadaddr;
1956520Sfeldman 	sin->sin_family = AF_INET;
1967216Ssam 	sin->sin_addr = if_makeaddr(ifp->if_net, INADDR_ANY);
1977216Ssam 	ifp->if_flags = IFF_BROADCAST;
1986520Sfeldman 
1997216Ssam 	ifp->if_init = ecinit;
2007216Ssam 	ifp->if_output = ecoutput;
2018977Sroot 	ifp->if_reset = ecreset;
2026520Sfeldman 	for (i=0; i<16; i++)
2038773Sroot 		es->es_buf[i] = (u_char *)&umem[ui->ui_ubanum][ECMEM+2048*i];
2047216Ssam 	if_attach(ifp);
2056520Sfeldman }
2066520Sfeldman 
2076520Sfeldman /*
2086520Sfeldman  * Reset of interface after UNIBUS reset.
2096520Sfeldman  * If interface is on specified uba, reset its state.
2106520Sfeldman  */
2116520Sfeldman ecreset(unit, uban)
2126520Sfeldman 	int unit, uban;
2136520Sfeldman {
2146520Sfeldman 	register struct uba_device *ui;
2156520Sfeldman 
2166520Sfeldman 	if (unit >= NEC || (ui = ecinfo[unit]) == 0 || ui->ui_alive == 0 ||
2176520Sfeldman 	    ui->ui_ubanum != uban)
2186520Sfeldman 		return;
2196520Sfeldman 	printf(" ec%d", unit);
2208773Sroot 	(void) ubamem(uban, ECMEM, 32*2, 0);	/* mr disable (no alloc) */
2216520Sfeldman 	ecinit(unit);
2226520Sfeldman }
2236520Sfeldman 
2246520Sfeldman /*
2256520Sfeldman  * Initialization of interface; clear recorded pending
2266520Sfeldman  * operations, and reinitialize UNIBUS usage.
2276520Sfeldman  */
2286520Sfeldman ecinit(unit)
2296520Sfeldman 	int unit;
2306520Sfeldman {
2317216Ssam 	struct ec_softc *es = &ec_softc[unit];
2327216Ssam 	struct ecdevice *addr;
2337216Ssam 	int i, s;
2346520Sfeldman 
2356520Sfeldman 	/*
2367217Sfeldman 	 * Hang receive buffers and start any pending writes.
2376637Sfeldman 	 * Writing into the rcr also makes sure the memory
2386637Sfeldman 	 * is turned on.
2396520Sfeldman 	 */
2407216Ssam 	addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
2416520Sfeldman 	s = splimp();
2426520Sfeldman 	for (i=ECRHBF; i>=ECRLBF; i--)
2436520Sfeldman 		addr->ec_rcr = EC_READ|i;
2447217Sfeldman 	es->es_oactive = 0;
2457217Sfeldman 	es->es_mask = ~0;
2466520Sfeldman 	es->es_if.if_flags |= IFF_UP;
2477217Sfeldman 	if (es->es_if.if_snd.ifq_head)
2487217Sfeldman 		ecstart(unit);
2496520Sfeldman 	splx(s);
2507150Swnj 	if_rtinit(&es->es_if, RTF_UP);
2516520Sfeldman }
2526520Sfeldman 
2536520Sfeldman /*
2546520Sfeldman  * Start or restart output on interface.
2556520Sfeldman  * If interface is already active, then this is a retransmit
2566545Sfeldman  * after a collision, and just restuff registers.
2576520Sfeldman  * If interface is not already active, get another datagram
2586520Sfeldman  * to send off of the interface queue, and map it to the interface
2596520Sfeldman  * before starting the output.
2606520Sfeldman  */
2616520Sfeldman ecstart(dev)
2626520Sfeldman 	dev_t dev;
2636520Sfeldman {
2648773Sroot         int unit = ECUNIT(dev);
2657216Ssam 	struct ec_softc *es = &ec_softc[unit];
2667216Ssam 	struct ecdevice *addr;
2676520Sfeldman 	struct mbuf *m;
2686520Sfeldman 
2696520Sfeldman 	if (es->es_oactive)
2706520Sfeldman 		goto restart;
2716520Sfeldman 
2726520Sfeldman 	IF_DEQUEUE(&es->es_if.if_snd, m);
2736520Sfeldman 	if (m == 0) {
2746520Sfeldman 		es->es_oactive = 0;
2756520Sfeldman 		return;
2766520Sfeldman 	}
2776520Sfeldman 	ecput(es->es_buf[ECTBF], m);
2786520Sfeldman 
2796520Sfeldman restart:
2807216Ssam 	addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
2816520Sfeldman 	addr->ec_xcr = EC_WRITE|ECTBF;
2826520Sfeldman 	es->es_oactive = 1;
2836520Sfeldman }
2846520Sfeldman 
2856520Sfeldman /*
2866520Sfeldman  * Ethernet interface transmitter interrupt.
2876520Sfeldman  * Start another output if more data to send.
2886520Sfeldman  */
2896520Sfeldman ecxint(unit)
2906520Sfeldman 	int unit;
2916520Sfeldman {
2926520Sfeldman 	register struct ec_softc *es = &ec_softc[unit];
2937216Ssam 	register struct ecdevice *addr =
2947216Ssam 		(struct ecdevice *)ecinfo[unit]->ui_addr;
2956520Sfeldman 
2966520Sfeldman 	if (es->es_oactive == 0)
2976520Sfeldman 		return;
2987216Ssam 	if ((addr->ec_xcr&EC_XDONE) == 0 || (addr->ec_xcr&EC_XBN) != ECTBF) {
2997216Ssam 		printf("ec%d: stray xmit interrupt, xcr=%b\n", unit,
3007216Ssam 			addr->ec_xcr, EC_XBITS);
3017216Ssam 		es->es_oactive = 0;
3027216Ssam 		addr->ec_xcr = EC_XCLR;
3037216Ssam 		return;
3047216Ssam 	}
3056520Sfeldman 	es->es_if.if_opackets++;
3066520Sfeldman 	es->es_oactive = 0;
3076520Sfeldman 	es->es_mask = ~0;
3086520Sfeldman 	addr->ec_xcr = EC_XCLR;
3097216Ssam 	if (es->es_if.if_snd.ifq_head)
3107216Ssam 		ecstart(unit);
3116520Sfeldman }
3126520Sfeldman 
3136520Sfeldman /*
3146520Sfeldman  * Collision on ethernet interface.  Do exponential
3156520Sfeldman  * backoff, and retransmit.  If have backed off all
3166520Sfeldman  * the way print warning diagnostic, and drop packet.
3176520Sfeldman  */
3186520Sfeldman eccollide(unit)
3196520Sfeldman 	int unit;
3206520Sfeldman {
3216520Sfeldman 	struct ec_softc *es = &ec_softc[unit];
3226520Sfeldman 
3236520Sfeldman 	es->es_if.if_collisions++;
3247216Ssam 	if (es->es_oactive)
3257216Ssam 		ecdocoll(unit);
3266520Sfeldman }
3276520Sfeldman 
3286520Sfeldman ecdocoll(unit)
3296520Sfeldman 	int unit;
3306520Sfeldman {
3316520Sfeldman 	register struct ec_softc *es = &ec_softc[unit];
3326545Sfeldman 	register struct ecdevice *addr =
3336545Sfeldman 	    (struct ecdevice *)ecinfo[unit]->ui_addr;
3346545Sfeldman 	register i;
3356545Sfeldman 	int delay;
3366520Sfeldman 
3376520Sfeldman 	/*
3386520Sfeldman 	 * Es_mask is a 16 bit number with n low zero bits, with
3396520Sfeldman 	 * n the number of backoffs.  When es_mask is 0 we have
3406520Sfeldman 	 * backed off 16 times, and give up.
3416520Sfeldman 	 */
3426520Sfeldman 	if (es->es_mask == 0) {
3436545Sfeldman 		es->es_if.if_oerrors++;
3446520Sfeldman 		printf("ec%d: send error\n", unit);
3456520Sfeldman 		/*
3466545Sfeldman 		 * Reset interface, then requeue rcv buffers.
3476545Sfeldman 		 * Some incoming packets may be lost, but that
3486545Sfeldman 		 * can't be helped.
3496520Sfeldman 		 */
3506545Sfeldman 		addr->ec_xcr = EC_UECLR;
3516545Sfeldman 		for (i=ECRHBF; i>=ECRLBF; i--)
3526545Sfeldman 			addr->ec_rcr = EC_READ|i;
3536545Sfeldman 		/*
3546545Sfeldman 		 * Reset and transmit next packet (if any).
3556545Sfeldman 		 */
3566545Sfeldman 		es->es_oactive = 0;
3576545Sfeldman 		es->es_mask = ~0;
3586545Sfeldman 		if (es->es_if.if_snd.ifq_head)
3596545Sfeldman 			ecstart(unit);
3606520Sfeldman 		return;
3616520Sfeldman 	}
3626520Sfeldman 	/*
3636545Sfeldman 	 * Do exponential backoff.  Compute delay based on low bits
3646545Sfeldman 	 * of the interval timer.  Then delay for that number of
3656545Sfeldman 	 * slot times.  A slot time is 51.2 microseconds (rounded to 51).
3666545Sfeldman 	 * This does not take into account the time already used to
3676545Sfeldman 	 * process the interrupt.
3686520Sfeldman 	 */
3696520Sfeldman 	es->es_mask <<= 1;
3706545Sfeldman 	delay = mfpr(ICR) &~ es->es_mask;
3716545Sfeldman 	DELAY(delay * 51);
3726520Sfeldman 	/*
3736545Sfeldman 	 * Clear the controller's collision flag, thus enabling retransmit.
3746520Sfeldman 	 */
3757470Sfeldman 	addr->ec_xcr = EC_CLEAR;
3766520Sfeldman }
3776520Sfeldman 
3786520Sfeldman /*
3796520Sfeldman  * Ethernet interface receiver interrupt.
3806520Sfeldman  * If input error just drop packet.
3816520Sfeldman  * Otherwise purge input buffered data path and examine
3826520Sfeldman  * packet to determine type.  If can't determine length
3836520Sfeldman  * from type, then have to drop packet.  Othewise decapsulate
3846520Sfeldman  * packet based on type and pass to type specific higher-level
3856520Sfeldman  * input routine.
3866520Sfeldman  */
3876520Sfeldman ecrint(unit)
3886520Sfeldman 	int unit;
3896520Sfeldman {
3906520Sfeldman 	struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
3916520Sfeldman 
3926520Sfeldman 	while (addr->ec_rcr & EC_RDONE)
3936520Sfeldman 		ecread(unit);
3946520Sfeldman }
3956520Sfeldman 
3966520Sfeldman ecread(unit)
3976520Sfeldman 	int unit;
3986520Sfeldman {
3996520Sfeldman 	register struct ec_softc *es = &ec_softc[unit];
4006520Sfeldman 	struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
401*9745Ssam 	register struct ether_header *ec;
4026520Sfeldman     	struct mbuf *m;
4038773Sroot 	int len, off, resid, ecoff, rbuf;
4046520Sfeldman 	register struct ifqueue *inq;
4058773Sroot 	u_char *ecbuf;
4066520Sfeldman 
4076520Sfeldman 	es->es_if.if_ipackets++;
4088773Sroot 	rbuf = addr->ec_rcr & EC_RBN;
4098773Sroot 	if (rbuf < ECRLBF || rbuf > ECRHBF)
4106520Sfeldman 		panic("ecrint");
4118773Sroot 	ecbuf = es->es_buf[rbuf];
4126520Sfeldman 	ecoff = *(short *)ecbuf;
4136545Sfeldman 	if (ecoff <= ECRDOFF || ecoff > 2046) {
4146520Sfeldman 		es->es_if.if_ierrors++;
4156520Sfeldman #ifdef notdef
4166520Sfeldman 		if (es->es_if.if_ierrors % 100 == 0)
4176520Sfeldman 			printf("ec%d: += 100 input errors\n", unit);
4186520Sfeldman #endif
4196520Sfeldman 		goto setup;
4206520Sfeldman 	}
4216520Sfeldman 
4226520Sfeldman 	/*
4236520Sfeldman 	 * Get input data length.
4246520Sfeldman 	 * Get pointer to ethernet header (in input buffer).
4256520Sfeldman 	 * Deal with trailer protocol: if type is PUP trailer
4266520Sfeldman 	 * get true type from first 16-bit word past data.
4276520Sfeldman 	 * Remember that type was trailer by setting off.
4286520Sfeldman 	 */
429*9745Ssam 	len = ecoff - ECRDOFF - sizeof (struct ether_header);
430*9745Ssam 	ec = (struct ether_header *)(ecbuf + ECRDOFF);
431*9745Ssam 	ec->ether_type = ntohs((u_short)ec->ether_type);
4326520Sfeldman #define	ecdataaddr(ec, off, type)	((type)(((caddr_t)((ec)+1)+(off))))
433*9745Ssam 	if (ec->ether_type >= ETHERPUP_TRAIL &&
434*9745Ssam 	    ec->ether_type < ETHERPUP_TRAIL+ETHERPUP_NTRAILER) {
435*9745Ssam 		off = (ec->ether_type - ETHERPUP_TRAIL) * 512;
436*9745Ssam 		if (off >= ETHERMTU)
4376520Sfeldman 			goto setup;		/* sanity */
438*9745Ssam 		ec->ether_type = ntohs(*ecdataaddr(ec, off, u_short *));
439*9745Ssam 		resid = ntohs(*(ecdataaddr(ec, off+2, u_short *)));
4406520Sfeldman 		if (off + resid > len)
4416520Sfeldman 			goto setup;		/* sanity */
4426520Sfeldman 		len = off + resid;
4436520Sfeldman 	} else
4446520Sfeldman 		off = 0;
4456520Sfeldman 	if (len == 0)
4466520Sfeldman 		goto setup;
4476520Sfeldman 
4486520Sfeldman 	/*
4496520Sfeldman 	 * Pull packet off interface.  Off is nonzero if packet
4506520Sfeldman 	 * has trailing header; ecget will then force this header
4516520Sfeldman 	 * information to be at the front, but we still have to drop
4526520Sfeldman 	 * the type and length which are at the front of any trailer data.
4536520Sfeldman 	 */
4546520Sfeldman 	m = ecget(ecbuf, len, off);
4556520Sfeldman 	if (m == 0)
4566520Sfeldman 		goto setup;
4576520Sfeldman 	if (off) {
4586520Sfeldman 		m->m_off += 2 * sizeof (u_short);
4596520Sfeldman 		m->m_len -= 2 * sizeof (u_short);
4606520Sfeldman 	}
461*9745Ssam 	switch (ec->ether_type) {
4626520Sfeldman 
4636520Sfeldman #ifdef INET
464*9745Ssam 	case ETHERPUP_IPTYPE:
4656520Sfeldman 		schednetisr(NETISR_IP);
4666520Sfeldman 		inq = &ipintrq;
4676520Sfeldman 		break;
4686520Sfeldman #endif
4696520Sfeldman 	default:
4706520Sfeldman 		m_freem(m);
4716520Sfeldman 		goto setup;
4726520Sfeldman 	}
4736520Sfeldman 
4746520Sfeldman 	if (IF_QFULL(inq)) {
4756520Sfeldman 		IF_DROP(inq);
4766520Sfeldman 		m_freem(m);
4777216Ssam 		goto setup;
4787216Ssam 	}
4797216Ssam 	IF_ENQUEUE(inq, m);
4806520Sfeldman 
4816520Sfeldman setup:
4826520Sfeldman 	/*
4836520Sfeldman 	 * Reset for next packet.
4846520Sfeldman 	 */
4858773Sroot 	addr->ec_rcr = EC_READ|EC_RCLR|rbuf;
4866520Sfeldman }
4876520Sfeldman 
4886520Sfeldman /*
4896520Sfeldman  * Ethernet output routine.
4906520Sfeldman  * Encapsulate a packet of type family for the local net.
4916520Sfeldman  * Use trailer local net encapsulation if enough data in first
4926520Sfeldman  * packet leaves a multiple of 512 bytes of data in remainder.
4936545Sfeldman  * If destination is this address or broadcast, send packet to
4946545Sfeldman  * loop device to kludge around the fact that 3com interfaces can't
4956545Sfeldman  * talk to themselves.
4966520Sfeldman  */
4976520Sfeldman ecoutput(ifp, m0, dst)
4986520Sfeldman 	struct ifnet *ifp;
4996520Sfeldman 	struct mbuf *m0;
5006520Sfeldman 	struct sockaddr *dst;
5016520Sfeldman {
5026520Sfeldman 	int type, dest, s, error;
5036520Sfeldman 	register struct ec_softc *es = &ec_softc[ifp->if_unit];
5046520Sfeldman 	register struct mbuf *m = m0;
505*9745Ssam 	register struct ether_header *ec;
5067216Ssam 	register int off, i;
5076545Sfeldman 	struct mbuf *mcopy = (struct mbuf *) 0;		/* Null */
5086520Sfeldman 
5096520Sfeldman 	switch (dst->sa_family) {
5106520Sfeldman 
5116520Sfeldman #ifdef INET
5126520Sfeldman 	case AF_INET:
5136520Sfeldman 		dest = ((struct sockaddr_in *)dst)->sin_addr.s_addr;
5146545Sfeldman 		if ((dest &~ 0xff) == 0)
5158838Sroot 			mcopy = m_copy(m, 0, (int)M_COPYALL);
5166545Sfeldman 		else if (dest == ((struct sockaddr_in *)&es->es_if.if_addr)->
5176545Sfeldman 		    sin_addr.s_addr) {
5186545Sfeldman 			mcopy = m;
5196545Sfeldman 			goto gotlocal;
5206545Sfeldman 		}
5216520Sfeldman 		off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len;
5226520Sfeldman 		if (off > 0 && (off & 0x1ff) == 0 &&
5236520Sfeldman 		    m->m_off >= MMINOFF + 2 * sizeof (u_short)) {
524*9745Ssam 			type = ETHERPUP_TRAIL + (off>>9);
5256520Sfeldman 			m->m_off -= 2 * sizeof (u_short);
5266520Sfeldman 			m->m_len += 2 * sizeof (u_short);
527*9745Ssam 			*mtod(m, u_short *) = ntohs((u_short)ETHERPUP_IPTYPE);
528*9745Ssam 			*(mtod(m, u_short *) + 1) = ntohs((u_short)m->m_len);
5296520Sfeldman 			goto gottrailertype;
5306520Sfeldman 		}
531*9745Ssam 		type = ETHERPUP_IPTYPE;
5326520Sfeldman 		off = 0;
5336520Sfeldman 		goto gottype;
5346520Sfeldman #endif
5356520Sfeldman 
5366520Sfeldman 	default:
5376520Sfeldman 		printf("ec%d: can't handle af%d\n", ifp->if_unit,
5386520Sfeldman 			dst->sa_family);
5396520Sfeldman 		error = EAFNOSUPPORT;
5406520Sfeldman 		goto bad;
5416520Sfeldman 	}
5426520Sfeldman 
5436520Sfeldman gottrailertype:
5446520Sfeldman 	/*
5456520Sfeldman 	 * Packet to be sent as trailer: move first packet
5466520Sfeldman 	 * (control information) to end of chain.
5476520Sfeldman 	 */
5486520Sfeldman 	while (m->m_next)
5496520Sfeldman 		m = m->m_next;
5506520Sfeldman 	m->m_next = m0;
5516520Sfeldman 	m = m0->m_next;
5526520Sfeldman 	m0->m_next = 0;
5536520Sfeldman 	m0 = m;
5546520Sfeldman 
5556520Sfeldman gottype:
5566520Sfeldman 	/*
5576520Sfeldman 	 * Add local net header.  If no space in first mbuf,
5586520Sfeldman 	 * allocate another.
5596520Sfeldman 	 */
5606520Sfeldman 	if (m->m_off > MMAXOFF ||
561*9745Ssam 	    MMINOFF + sizeof (struct ether_header) > m->m_off) {
5629648Ssam 		m = m_get(M_DONTWAIT, MT_HEADER);
5636520Sfeldman 		if (m == 0) {
5646520Sfeldman 			error = ENOBUFS;
5656520Sfeldman 			goto bad;
5666520Sfeldman 		}
5676520Sfeldman 		m->m_next = m0;
5686520Sfeldman 		m->m_off = MMINOFF;
569*9745Ssam 		m->m_len = sizeof (struct ether_header);
5706520Sfeldman 	} else {
571*9745Ssam 		m->m_off -= sizeof (struct ether_header);
572*9745Ssam 		m->m_len += sizeof (struct ether_header);
5736520Sfeldman 	}
574*9745Ssam 	ec = mtod(m, struct ether_header *);
575*9745Ssam 	bcopy((caddr_t)es->es_enaddr, (caddr_t)ec->ether_shost, 6);
5766525Sfeldman 	if ((dest &~ 0xff) == 0)
577*9745Ssam 		bcopy((caddr_t)ecbroadcastaddr, (caddr_t)ec->ether_dhost, 6);
5786520Sfeldman 	else {
579*9745Ssam 		u_char *to = dest & 0x8000 ? ec_iltop : es->es_enaddr;
580*9745Ssam 
581*9745Ssam 		bcopy((caddr_t)to, (caddr_t)ec->ether_dhost, 3);
582*9745Ssam 		ec->ether_dhost[3] = (dest>>8) & 0x7f;
583*9745Ssam 		ec->ether_dhost[4] = (dest>>16) & 0xff;
584*9745Ssam 		ec->ether_dhost[5] = (dest>>24) & 0xff;
5856520Sfeldman 	}
586*9745Ssam 	ec->ether_type = htons((u_short)type);
5876520Sfeldman 
5886520Sfeldman 	/*
5896520Sfeldman 	 * Queue message on interface, and start output if interface
5906520Sfeldman 	 * not yet active.
5916520Sfeldman 	 */
5926520Sfeldman 	s = splimp();
5936520Sfeldman 	if (IF_QFULL(&ifp->if_snd)) {
5946520Sfeldman 		IF_DROP(&ifp->if_snd);
5956520Sfeldman 		error = ENOBUFS;
5966520Sfeldman 		goto qfull;
5976520Sfeldman 	}
5986520Sfeldman 	IF_ENQUEUE(&ifp->if_snd, m);
5996520Sfeldman 	if (es->es_oactive == 0)
6006520Sfeldman 		ecstart(ifp->if_unit);
6016520Sfeldman 	splx(s);
6027216Ssam 
6036545Sfeldman gotlocal:
6047216Ssam 	return(mcopy ? looutput(&loif, mcopy, dst) : 0);
6057216Ssam 
6066520Sfeldman qfull:
6076520Sfeldman 	m0 = m;
6086520Sfeldman 	splx(s);
6096520Sfeldman bad:
6106520Sfeldman 	m_freem(m0);
6116520Sfeldman 	return(error);
6126520Sfeldman }
6136520Sfeldman 
6146520Sfeldman /*
6159177Ssam  * Routine to copy from mbuf chain to transmit
6167216Ssam  * buffer in UNIBUS memory.
6179177Ssam  * If packet size is less than the minimum legal size,
6189177Ssam  * the buffer is expanded.  We probably should zero out the extra
6199177Ssam  * bytes for security, but that would slow things down.
6206520Sfeldman  */
6216520Sfeldman ecput(ecbuf, m)
6227216Ssam 	u_char *ecbuf;
6236520Sfeldman 	struct mbuf *m;
6246520Sfeldman {
6256520Sfeldman 	register struct mbuf *mp;
6267216Ssam 	register int off;
6277263Ssam 	u_char *bp;
6286520Sfeldman 
6297216Ssam 	for (off = 2048, mp = m; mp; mp = mp->m_next)
6307216Ssam 		off -= mp->m_len;
631*9745Ssam 	if (2048 - off < ETHERMIN + sizeof (struct ether_header))
632*9745Ssam 		off = 2048 - ETHERMIN - sizeof (struct ether_header);
6337216Ssam 	*(u_short *)ecbuf = off;
6347216Ssam 	bp = (u_char *)(ecbuf + off);
6357263Ssam 	for (mp = m; mp; mp = mp->m_next) {
6367263Ssam 		register unsigned len = mp->m_len;
6377263Ssam 		u_char *mcp;
6387216Ssam 
6397216Ssam 		if (len == 0)
6407216Ssam 			continue;
6417216Ssam 		mcp = mtod(mp, u_char *);
6427216Ssam 		if ((unsigned)bp & 01) {
6437032Swnj 			*bp++ = *mcp++;
6447216Ssam 			len--;
6457032Swnj 		}
6467263Ssam 		if (off = (len >> 1)) {
6477263Ssam 			register u_short *to, *from;
6487263Ssam 
6497263Ssam 			to = (u_short *)bp;
6507263Ssam 			from = (u_short *)mcp;
6517263Ssam 			do
6527263Ssam 				*to++ = *from++;
6537263Ssam 			while (--off > 0);
6547263Ssam 			bp = (u_char *)to,
6557263Ssam 			mcp = (u_char *)from;
6567032Swnj 		}
6577263Ssam 		if (len & 01)
6586520Sfeldman 			*bp++ = *mcp++;
6596520Sfeldman 	}
6607263Ssam 	m_freem(m);
6616520Sfeldman }
6626520Sfeldman 
6636520Sfeldman /*
6646520Sfeldman  * Routine to copy from UNIBUS memory into mbufs.
6656520Sfeldman  * Similar in spirit to if_rubaget.
6667032Swnj  *
6677032Swnj  * Warning: This makes the fairly safe assumption that
6687032Swnj  * mbufs have even lengths.
6696520Sfeldman  */
6706520Sfeldman struct mbuf *
6716520Sfeldman ecget(ecbuf, totlen, off0)
6727263Ssam 	u_char *ecbuf;
6736520Sfeldman 	int totlen, off0;
6746520Sfeldman {
6757263Ssam 	register struct mbuf *m;
6767263Ssam 	struct mbuf *top = 0, **mp = &top;
6777263Ssam 	register int off = off0, len;
6787263Ssam 	u_char *cp;
6796520Sfeldman 
680*9745Ssam 	cp = ecbuf + ECRDOFF + sizeof (struct ether_header);
6816520Sfeldman 	while (totlen > 0) {
6827263Ssam 		register int words;
6837263Ssam 		u_char *mcp;
6847263Ssam 
6859648Ssam 		MGET(m, M_DONTWAIT, MT_DATA);
6866520Sfeldman 		if (m == 0)
6876520Sfeldman 			goto bad;
6886520Sfeldman 		if (off) {
6896520Sfeldman 			len = totlen - off;
690*9745Ssam 			cp = ecbuf + ECRDOFF +
691*9745Ssam 				sizeof (struct ether_header) + off;
6926520Sfeldman 		} else
6936520Sfeldman 			len = totlen;
6946520Sfeldman 		if (len >= CLBYTES) {
6956520Sfeldman 			struct mbuf *p;
6966520Sfeldman 
6976520Sfeldman 			MCLGET(p, 1);
6986520Sfeldman 			if (p != 0) {
6996520Sfeldman 				m->m_len = len = CLBYTES;
7006520Sfeldman 				m->m_off = (int)p - (int)m;
7016520Sfeldman 			} else {
7026520Sfeldman 				m->m_len = len = MIN(MLEN, len);
7036520Sfeldman 				m->m_off = MMINOFF;
7046520Sfeldman 			}
7056520Sfeldman 		} else {
7066520Sfeldman 			m->m_len = len = MIN(MLEN, len);
7076520Sfeldman 			m->m_off = MMINOFF;
7086520Sfeldman 		}
7097263Ssam 		mcp = mtod(m, u_char *);
7107263Ssam 		if (words = (len >> 1)) {
7117263Ssam 			register u_short *to, *from;
7127263Ssam 
7137263Ssam 			to = (u_short *)mcp;
7147263Ssam 			from = (u_short *)cp;
7157263Ssam 			do
7167263Ssam 				*to++ = *from++;
7177263Ssam 			while (--words > 0);
7187263Ssam 			mcp = (u_char *)to;
7197263Ssam 			cp = (u_char *)from;
7207032Swnj 		}
7217216Ssam 		if (len & 01)
7226520Sfeldman 			*mcp++ = *cp++;
7236520Sfeldman 		*mp = m;
7246520Sfeldman 		mp = &m->m_next;
7257263Ssam 		if (off == 0) {
7266520Sfeldman 			totlen -= len;
7277263Ssam 			continue;
7287263Ssam 		}
7297263Ssam 		off += len;
7307263Ssam 		if (off == totlen) {
731*9745Ssam 			cp = ecbuf + ECRDOFF + sizeof (struct ether_header);
7327263Ssam 			off = 0;
7337263Ssam 			totlen = off0;
7347263Ssam 		}
7356520Sfeldman 	}
7366520Sfeldman 	return (top);
7376520Sfeldman bad:
7386520Sfeldman 	m_freem(top);
7396520Sfeldman 	return (0);
7406520Sfeldman }
741