xref: /csrg-svn/sys/vax/if/if_ec.c (revision 7217)
1*7217Sfeldman /*	if_ec.c	4.18	82/06/17	*/
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/ubareg.h"
176520Sfeldman #include "../h/ubavar.h"
186520Sfeldman #include "../h/ecreg.h"
196520Sfeldman #include "../h/cpu.h"
206520Sfeldman #include "../h/mtpr.h"
216520Sfeldman #include "../h/vmmac.h"
226520Sfeldman #include "../net/in.h"
236520Sfeldman #include "../net/in_systm.h"
246520Sfeldman #include "../net/if.h"
256520Sfeldman #include "../net/if_ec.h"
266520Sfeldman #include "../net/if_uba.h"
276520Sfeldman #include "../net/ip.h"
286520Sfeldman #include "../net/ip_var.h"
296520Sfeldman #include "../net/pup.h"
306520Sfeldman #include "../net/route.h"
316520Sfeldman #include <errno.h>
326520Sfeldman 
336520Sfeldman #define	ECMTU	1500
346520Sfeldman 
356520Sfeldman int	ecprobe(), ecattach(), ecrint(), ecxint(), eccollide();
366520Sfeldman struct	uba_device *ecinfo[NEC];
376520Sfeldman u_short ecstd[] = { 0 };
386520Sfeldman struct	uba_driver ecdriver =
396520Sfeldman 	{ ecprobe, 0, ecattach, 0, ecstd, "ec", ecinfo };
406892Sfeldman u_char	ec_iltop[3] = { 0x02, 0x07, 0x01 };
416520Sfeldman #define	ECUNIT(x)	minor(x)
426520Sfeldman 
436520Sfeldman int	ecinit(),ecoutput(),ecreset();
446520Sfeldman struct mbuf *ecget();
456520Sfeldman 
466545Sfeldman extern struct ifnet loif;
476545Sfeldman 
486520Sfeldman /*
496520Sfeldman  * Ethernet software status per interface.
506520Sfeldman  *
516520Sfeldman  * Each interface is referenced by a network interface structure,
526520Sfeldman  * es_if, which the routing code uses to locate the interface.
536520Sfeldman  * This structure contains the output queue for the interface, its address, ...
546520Sfeldman  * We also have, for each interface, a UBA interface structure, which
556520Sfeldman  * contains information about the UNIBUS resources held by the interface:
566520Sfeldman  * map registers, buffered data paths, etc.  Information is cached in this
576520Sfeldman  * structure for use by the if_uba.c routines in running the interface
586520Sfeldman  * efficiently.
596520Sfeldman  */
606520Sfeldman struct	ec_softc {
616520Sfeldman 	struct	ifnet es_if;		/* network-visible interface */
626520Sfeldman 	struct	ifuba es_ifuba;		/* UNIBUS resources */
636520Sfeldman 	short	es_mask;		/* mask for current output delay */
646520Sfeldman 	short	es_oactive;		/* is output active? */
656520Sfeldman 	caddr_t	es_buf[16];		/* virtual addresses of buffers */
666520Sfeldman 	u_char	es_enaddr[6];		/* board's ethernet address */
676520Sfeldman } ec_softc[NEC];
686520Sfeldman 
696520Sfeldman /*
706520Sfeldman  * Do output DMA to determine interface presence and
716520Sfeldman  * interrupt vector.  DMA is too short to disturb other hosts.
726520Sfeldman  */
736520Sfeldman ecprobe(reg)
746520Sfeldman 	caddr_t reg;
756520Sfeldman {
766520Sfeldman 	register int br, cvec;		/* r11, r10 value-result */
776520Sfeldman 	register struct ecdevice *addr = (struct ecdevice *)reg;
786520Sfeldman 	register caddr_t ecbuf = (caddr_t) &umem[0][0600000];
796520Sfeldman 
806520Sfeldman COUNT(ECPROBE);
816520Sfeldman #ifdef lint
826520Sfeldman 	br = 0; cvec = br; br = cvec;
836520Sfeldman 	ecrint(0); ecxint(0); eccollide(0);
846520Sfeldman #endif
856520Sfeldman 	/*
866637Sfeldman 	 * Make sure memory is turned on
876637Sfeldman 	 */
886637Sfeldman 	addr->ec_rcr = EC_AROM;
896637Sfeldman 	/*
906520Sfeldman 	 * Check for existence of buffers on Unibus.
916520Sfeldman 	 * This won't work on a 780 until more work is done.
926520Sfeldman 	 */
936520Sfeldman 	if (badaddr((caddr_t) ecbuf, 2)) {
946520Sfeldman 		printf("ec: buffer mem not found");
956520Sfeldman 		return (0);
966520Sfeldman 	}
976520Sfeldman 
986520Sfeldman 	/*
996520Sfeldman 	 * Tell the system that the board has memory here, so it won't
1006520Sfeldman 	 * attempt to allocate the addresses later.
1016520Sfeldman 	 */
1026520Sfeldman 	ubamem(0, 0600000, 32*2);
1036520Sfeldman 
1046520Sfeldman 	/*
1056520Sfeldman 	 * Make a one byte packet in what should be buffer #0.
1066520Sfeldman 	 * Submit it for sending.  This whould cause an xmit interrupt.
1076520Sfeldman 	 * The xmit interrupt vector is 8 bytes after the receive vector,
1086520Sfeldman 	 * so adjust for this before returning.
1096520Sfeldman 	 */
1106520Sfeldman 	*(u_short *)ecbuf = (u_short) 03777;
1116520Sfeldman 	ecbuf[03777] = '\0';
1126520Sfeldman 	addr->ec_xcr = EC_XINTEN|EC_XWBN;
1136520Sfeldman 	DELAY(100000);
1146520Sfeldman 	addr->ec_xcr = EC_XCLR;
1157216Ssam 	if (cvec > 0 && cvec != 0x200) {
1166520Sfeldman 		cvec -= 010;
1177216Ssam 		br += 2;		/* rcv is xmit + 2 */
1187216Ssam 	}
1196520Sfeldman 	return (1);
1206520Sfeldman }
1216520Sfeldman 
1226520Sfeldman /*
1236520Sfeldman  * Interface exists: make available by filling in network interface
1246520Sfeldman  * record.  System will initialize the interface when it is ready
1256520Sfeldman  * to accept packets.
1266520Sfeldman  */
1276520Sfeldman ecattach(ui)
1286520Sfeldman 	struct uba_device *ui;
1296520Sfeldman {
1307216Ssam 	struct ec_softc *es = &ec_softc[ui->ui_unit];
1317216Ssam 	register struct ifnet *ifp = &es->es_if;
1326520Sfeldman 	register struct ecdevice *addr = (struct ecdevice *)ui->ui_addr;
1337216Ssam 	struct sockaddr_in *sin;
1347216Ssam 	int i, j;
1357216Ssam 	u_char *cp;
1366520Sfeldman COUNT(ECATTACH);
1376520Sfeldman 
1387216Ssam 	ifp->if_unit = ui->ui_unit;
1397216Ssam 	ifp->if_name = "ec";
1407216Ssam 	ifp->if_mtu = ECMTU;
1417216Ssam 	ifp->if_net = ui->ui_flags;
1426520Sfeldman 
1436520Sfeldman 	/*
1447216Ssam 	 * Read the ethernet address off the board, one nibble at a time.
1456520Sfeldman 	 */
1466520Sfeldman 	addr->ec_xcr = EC_UECLR;
1476520Sfeldman 	addr->ec_rcr = EC_AROM;
1486520Sfeldman 	cp = es->es_enaddr;
1497216Ssam #define	NEXTBIT	addr->ec_rcr = EC_AROM|EC_ASTEP; addr->ec_rcr = EC_AROM
1506520Sfeldman 	for (i=0; i<6; i++) {
1516520Sfeldman 		*cp = 0;
1526520Sfeldman 		for (j=0; j<=4; j+=4) {
1536520Sfeldman 			*cp |= ((addr->ec_rcr >> 8) & 0xf) << j;
1547216Ssam 			NEXTBIT; NEXTBIT; NEXTBIT; NEXTBIT;
1556520Sfeldman 		}
1566520Sfeldman 		cp++;
1576520Sfeldman 	}
1587216Ssam #ifdef notdef
1596520Sfeldman 	printf("ec%d: addr=%x:%x:%x:%x:%x:%x\n", ui->ui_unit,
1606520Sfeldman 		es->es_enaddr[0]&0xff, es->es_enaddr[1]&0xff,
1616520Sfeldman 		es->es_enaddr[2]&0xff, es->es_enaddr[3]&0xff,
1626520Sfeldman 		es->es_enaddr[4]&0xff, es->es_enaddr[5]&0xff);
1637216Ssam #endif
1647216Ssam 	ifp->if_host[0] = ((es->es_enaddr[3]&0xff)<<16) |
1656520Sfeldman 	    ((es->es_enaddr[4]&0xff)<<8) | (es->es_enaddr[5]&0xff);
1666520Sfeldman 	sin = (struct sockaddr_in *)&es->es_if.if_addr;
1676520Sfeldman 	sin->sin_family = AF_INET;
1687216Ssam 	sin->sin_addr = if_makeaddr(ifp->if_net, ifp->if_host[0]);
1696520Sfeldman 
1707216Ssam 	sin = (struct sockaddr_in *)&ifp->if_broadaddr;
1716520Sfeldman 	sin->sin_family = AF_INET;
1727216Ssam 	sin->sin_addr = if_makeaddr(ifp->if_net, INADDR_ANY);
1737216Ssam 	ifp->if_flags = IFF_BROADCAST;
1746520Sfeldman 
1757216Ssam 	ifp->if_init = ecinit;
1767216Ssam 	ifp->if_output = ecoutput;
1777216Ssam 	ifp->if_ubareset = ecreset;
1786520Sfeldman 	for (i=0; i<16; i++)
1796520Sfeldman 		es->es_buf[i] = &umem[ui->ui_ubanum][0600000+2048*i];
1807216Ssam 	if_attach(ifp);
1816520Sfeldman }
1826520Sfeldman 
1836520Sfeldman /*
1846520Sfeldman  * Reset of interface after UNIBUS reset.
1856520Sfeldman  * If interface is on specified uba, reset its state.
1866520Sfeldman  */
1876520Sfeldman ecreset(unit, uban)
1886520Sfeldman 	int unit, uban;
1896520Sfeldman {
1906520Sfeldman 	register struct uba_device *ui;
1916520Sfeldman COUNT(ECRESET);
1926520Sfeldman 
1936520Sfeldman 	if (unit >= NEC || (ui = ecinfo[unit]) == 0 || ui->ui_alive == 0 ||
1946520Sfeldman 	    ui->ui_ubanum != uban)
1956520Sfeldman 		return;
1966520Sfeldman 	printf(" ec%d", unit);
1976520Sfeldman 	ecinit(unit);
1986520Sfeldman }
1996520Sfeldman 
2006520Sfeldman /*
2016520Sfeldman  * Initialization of interface; clear recorded pending
2026520Sfeldman  * operations, and reinitialize UNIBUS usage.
2036520Sfeldman  */
2046520Sfeldman ecinit(unit)
2056520Sfeldman 	int unit;
2066520Sfeldman {
2077216Ssam 	struct ec_softc *es = &ec_softc[unit];
2087216Ssam 	struct ecdevice *addr;
2097216Ssam 	int i, s;
2106520Sfeldman 
2116520Sfeldman 	/*
212*7217Sfeldman 	 * Hang receive buffers and start any pending writes.
2136637Sfeldman 	 * Writing into the rcr also makes sure the memory
2146637Sfeldman 	 * is turned on.
2156520Sfeldman 	 */
2167216Ssam 	addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
2176520Sfeldman 	s = splimp();
2186520Sfeldman 	for (i=ECRHBF; i>=ECRLBF; i--)
2196520Sfeldman 		addr->ec_rcr = EC_READ|i;
220*7217Sfeldman 	es->es_oactive = 0;
221*7217Sfeldman 	es->es_mask = ~0;
2226520Sfeldman 	es->es_if.if_flags |= IFF_UP;
223*7217Sfeldman 	if (es->es_if.if_snd.ifq_head)
224*7217Sfeldman 		ecstart(unit);
2256520Sfeldman 	splx(s);
2267150Swnj 	if_rtinit(&es->es_if, RTF_UP);
2276520Sfeldman }
2286520Sfeldman 
2296520Sfeldman /*
2306520Sfeldman  * Start or restart output on interface.
2316520Sfeldman  * If interface is already active, then this is a retransmit
2326545Sfeldman  * after a collision, and just restuff registers.
2336520Sfeldman  * If interface is not already active, get another datagram
2346520Sfeldman  * to send off of the interface queue, and map it to the interface
2356520Sfeldman  * before starting the output.
2366520Sfeldman  */
2376520Sfeldman ecstart(dev)
2386520Sfeldman 	dev_t dev;
2396520Sfeldman {
2407216Ssam         int unit = ECUNIT(dev), dest;
2417216Ssam 	struct ec_softc *es = &ec_softc[unit];
2427216Ssam 	struct ecdevice *addr;
2436520Sfeldman 	struct mbuf *m;
2446520Sfeldman 	caddr_t ecbuf;
2456520Sfeldman COUNT(ECSTART);
2466520Sfeldman 
2476520Sfeldman 	if (es->es_oactive)
2486520Sfeldman 		goto restart;
2496520Sfeldman 
2506520Sfeldman 	IF_DEQUEUE(&es->es_if.if_snd, m);
2516520Sfeldman 	if (m == 0) {
2526520Sfeldman 		es->es_oactive = 0;
2536520Sfeldman 		return;
2546520Sfeldman 	}
2556520Sfeldman 	ecput(es->es_buf[ECTBF], m);
2566520Sfeldman 
2576520Sfeldman restart:
2587216Ssam 	addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
2596520Sfeldman 	addr->ec_xcr = EC_WRITE|ECTBF;
2606520Sfeldman 	es->es_oactive = 1;
2616520Sfeldman }
2626520Sfeldman 
2636520Sfeldman /*
2646520Sfeldman  * Ethernet interface transmitter interrupt.
2656520Sfeldman  * Start another output if more data to send.
2666520Sfeldman  */
2676520Sfeldman ecxint(unit)
2686520Sfeldman 	int unit;
2696520Sfeldman {
2706520Sfeldman 	register struct ec_softc *es = &ec_softc[unit];
2717216Ssam 	register struct ecdevice *addr =
2727216Ssam 		(struct ecdevice *)ecinfo[unit]->ui_addr;
2736520Sfeldman COUNT(ECXINT);
2746520Sfeldman 
2756520Sfeldman 	if (es->es_oactive == 0)
2766520Sfeldman 		return;
2777216Ssam 	if ((addr->ec_xcr&EC_XDONE) == 0 || (addr->ec_xcr&EC_XBN) != ECTBF) {
2787216Ssam 		printf("ec%d: stray xmit interrupt, xcr=%b\n", unit,
2797216Ssam 			addr->ec_xcr, EC_XBITS);
2807216Ssam 		es->es_oactive = 0;
2817216Ssam 		addr->ec_xcr = EC_XCLR;
2827216Ssam 		return;
2837216Ssam 	}
2846520Sfeldman 	es->es_if.if_opackets++;
2856520Sfeldman 	es->es_oactive = 0;
2866520Sfeldman 	es->es_mask = ~0;
2876520Sfeldman 	addr->ec_xcr = EC_XCLR;
2886520Sfeldman 	/*
2896520Sfeldman 	 * There shouldn't ever be any mbuf's to free, but just in case...
2906520Sfeldman 	 */
2916520Sfeldman 	if (es->es_ifuba.ifu_xtofree) {
2926520Sfeldman 		m_freem(es->es_ifuba.ifu_xtofree);
2936520Sfeldman 		es->es_ifuba.ifu_xtofree = 0;
2946520Sfeldman 	}
2957216Ssam 	if (es->es_if.if_snd.ifq_head)
2967216Ssam 		ecstart(unit);
2976520Sfeldman }
2986520Sfeldman 
2996520Sfeldman /*
3006520Sfeldman  * Collision on ethernet interface.  Do exponential
3016520Sfeldman  * backoff, and retransmit.  If have backed off all
3026520Sfeldman  * the way print warning diagnostic, and drop packet.
3036520Sfeldman  */
3046520Sfeldman eccollide(unit)
3056520Sfeldman 	int unit;
3066520Sfeldman {
3076520Sfeldman 	struct ec_softc *es = &ec_softc[unit];
3086520Sfeldman COUNT(ECCOLLIDE);
3096520Sfeldman 
3106545Sfeldman 	printf("ec%d: collision\n", unit);
3116520Sfeldman 	es->es_if.if_collisions++;
3127216Ssam 	if (es->es_oactive)
3137216Ssam 		ecdocoll(unit);
3146520Sfeldman }
3156520Sfeldman 
3166520Sfeldman ecdocoll(unit)
3176520Sfeldman 	int unit;
3186520Sfeldman {
3196520Sfeldman 	register struct ec_softc *es = &ec_softc[unit];
3206545Sfeldman 	register struct ecdevice *addr =
3216545Sfeldman 	    (struct ecdevice *)ecinfo[unit]->ui_addr;
3226545Sfeldman 	register i;
3236545Sfeldman 	int delay;
3246520Sfeldman 
3256520Sfeldman 	/*
3266520Sfeldman 	 * Es_mask is a 16 bit number with n low zero bits, with
3276520Sfeldman 	 * n the number of backoffs.  When es_mask is 0 we have
3286520Sfeldman 	 * backed off 16 times, and give up.
3296520Sfeldman 	 */
3306520Sfeldman 	if (es->es_mask == 0) {
3316545Sfeldman 		es->es_if.if_oerrors++;
3326520Sfeldman 		printf("ec%d: send error\n", unit);
3336520Sfeldman 		/*
3346545Sfeldman 		 * Reset interface, then requeue rcv buffers.
3356545Sfeldman 		 * Some incoming packets may be lost, but that
3366545Sfeldman 		 * can't be helped.
3376520Sfeldman 		 */
3386545Sfeldman 		addr->ec_xcr = EC_UECLR;
3396545Sfeldman 		for (i=ECRHBF; i>=ECRLBF; i--)
3406545Sfeldman 			addr->ec_rcr = EC_READ|i;
3416545Sfeldman 		/*
3426545Sfeldman 		 * Reset and transmit next packet (if any).
3436545Sfeldman 		 */
3446545Sfeldman 		es->es_oactive = 0;
3456545Sfeldman 		es->es_mask = ~0;
3466545Sfeldman 		if (es->es_if.if_snd.ifq_head)
3476545Sfeldman 			ecstart(unit);
3486520Sfeldman 		return;
3496520Sfeldman 	}
3506520Sfeldman 	/*
3516545Sfeldman 	 * Do exponential backoff.  Compute delay based on low bits
3526545Sfeldman 	 * of the interval timer.  Then delay for that number of
3536545Sfeldman 	 * slot times.  A slot time is 51.2 microseconds (rounded to 51).
3546545Sfeldman 	 * This does not take into account the time already used to
3556545Sfeldman 	 * process the interrupt.
3566520Sfeldman 	 */
3576520Sfeldman 	es->es_mask <<= 1;
3586545Sfeldman 	delay = mfpr(ICR) &~ es->es_mask;
3596545Sfeldman 	DELAY(delay * 51);
3606520Sfeldman 	/*
3616545Sfeldman 	 * Clear the controller's collision flag, thus enabling retransmit.
3626520Sfeldman 	 */
3636717Sfeldman 	addr->ec_xcr = EC_JINTEN|EC_XINTEN|EC_JCLR;
3646520Sfeldman }
3656520Sfeldman 
3666520Sfeldman /*
3676520Sfeldman  * Ethernet interface receiver interrupt.
3686520Sfeldman  * If input error just drop packet.
3696520Sfeldman  * Otherwise purge input buffered data path and examine
3706520Sfeldman  * packet to determine type.  If can't determine length
3716520Sfeldman  * from type, then have to drop packet.  Othewise decapsulate
3726520Sfeldman  * packet based on type and pass to type specific higher-level
3736520Sfeldman  * input routine.
3746520Sfeldman  */
3756520Sfeldman ecrint(unit)
3766520Sfeldman 	int unit;
3776520Sfeldman {
3786520Sfeldman 	struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
3796520Sfeldman COUNT(ECRINT);
3806520Sfeldman 
3816520Sfeldman 	while (addr->ec_rcr & EC_RDONE)
3826520Sfeldman 		ecread(unit);
3836520Sfeldman }
3846520Sfeldman 
3856520Sfeldman ecread(unit)
3866520Sfeldman 	int unit;
3876520Sfeldman {
3886520Sfeldman 	register struct ec_softc *es = &ec_softc[unit];
3896520Sfeldman 	struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
3906520Sfeldman 	register struct ec_header *ec;
3916520Sfeldman     	struct mbuf *m;
3927216Ssam 	int len, off, resid, ecoff, buf;
3936520Sfeldman 	register struct ifqueue *inq;
3946520Sfeldman 	caddr_t ecbuf;
3956520Sfeldman COUNT(ECREAD);
3966520Sfeldman 
3976520Sfeldman 	es->es_if.if_ipackets++;
3986520Sfeldman 	buf = addr->ec_rcr & EC_RBN;
3996520Sfeldman 	if (buf < ECRLBF || buf > ECRHBF)
4006520Sfeldman 		panic("ecrint");
4016520Sfeldman 	ecbuf = es->es_buf[buf];
4026520Sfeldman 	ecoff = *(short *)ecbuf;
4036545Sfeldman 	if (ecoff <= ECRDOFF || ecoff > 2046) {
4046520Sfeldman 		es->es_if.if_ierrors++;
4056520Sfeldman #ifdef notdef
4066520Sfeldman 		if (es->es_if.if_ierrors % 100 == 0)
4076520Sfeldman 			printf("ec%d: += 100 input errors\n", unit);
4086520Sfeldman #endif
4096520Sfeldman 		printf("ec%d: input error (offset=%d)\n", unit, ecoff);
4106520Sfeldman 		goto setup;
4116520Sfeldman 	}
4126520Sfeldman 
4136520Sfeldman 	/*
4146520Sfeldman 	 * Get input data length.
4156520Sfeldman 	 * Get pointer to ethernet header (in input buffer).
4166520Sfeldman 	 * Deal with trailer protocol: if type is PUP trailer
4176520Sfeldman 	 * get true type from first 16-bit word past data.
4186520Sfeldman 	 * Remember that type was trailer by setting off.
4196520Sfeldman 	 */
4206520Sfeldman 	len = ecoff - ECRDOFF - sizeof (struct ec_header);
4216520Sfeldman 	ec = (struct ec_header *)(ecbuf + ECRDOFF);
4226520Sfeldman #define	ecdataaddr(ec, off, type)	((type)(((caddr_t)((ec)+1)+(off))))
4236520Sfeldman 	if (ec->ec_type >= ECPUP_TRAIL &&
4246520Sfeldman 	    ec->ec_type < ECPUP_TRAIL+ECPUP_NTRAILER) {
4256520Sfeldman 		off = (ec->ec_type - ECPUP_TRAIL) * 512;
4266520Sfeldman 		if (off >= ECMTU)
4276520Sfeldman 			goto setup;		/* sanity */
4286520Sfeldman 		ec->ec_type = *ecdataaddr(ec, off, u_short *);
4296520Sfeldman 		resid = *(ecdataaddr(ec, off+2, u_short *));
4306520Sfeldman 		if (off + resid > len)
4316520Sfeldman 			goto setup;		/* sanity */
4326520Sfeldman 		len = off + resid;
4336520Sfeldman 	} else
4346520Sfeldman 		off = 0;
4356520Sfeldman 	if (len == 0)
4366520Sfeldman 		goto setup;
4376520Sfeldman 
4386520Sfeldman 	/*
4396520Sfeldman 	 * Pull packet off interface.  Off is nonzero if packet
4406520Sfeldman 	 * has trailing header; ecget will then force this header
4416520Sfeldman 	 * information to be at the front, but we still have to drop
4426520Sfeldman 	 * the type and length which are at the front of any trailer data.
4436520Sfeldman 	 */
4446520Sfeldman 	m = ecget(ecbuf, len, off);
4456520Sfeldman 	if (m == 0)
4466520Sfeldman 		goto setup;
4476520Sfeldman 	if (off) {
4486520Sfeldman 		m->m_off += 2 * sizeof (u_short);
4496520Sfeldman 		m->m_len -= 2 * sizeof (u_short);
4506520Sfeldman 	}
4516520Sfeldman 	switch (ec->ec_type) {
4526520Sfeldman 
4536520Sfeldman #ifdef INET
4546520Sfeldman 	case ECPUP_IPTYPE:
4556520Sfeldman 		schednetisr(NETISR_IP);
4566520Sfeldman 		inq = &ipintrq;
4576520Sfeldman 		break;
4586520Sfeldman #endif
4596520Sfeldman 	default:
4606520Sfeldman 		m_freem(m);
4616520Sfeldman 		goto setup;
4626520Sfeldman 	}
4636520Sfeldman 
4646520Sfeldman 	if (IF_QFULL(inq)) {
4656520Sfeldman 		IF_DROP(inq);
4666520Sfeldman 		m_freem(m);
4677216Ssam 		goto setup;
4687216Ssam 	}
4697216Ssam 	IF_ENQUEUE(inq, m);
4706520Sfeldman 
4716520Sfeldman setup:
4726520Sfeldman 	/*
4736520Sfeldman 	 * Reset for next packet.
4746520Sfeldman 	 */
4756520Sfeldman 	addr->ec_rcr = EC_READ|EC_RCLR|buf;
4766520Sfeldman }
4776520Sfeldman 
4786520Sfeldman /*
4796520Sfeldman  * Ethernet output routine.
4806520Sfeldman  * Encapsulate a packet of type family for the local net.
4816520Sfeldman  * Use trailer local net encapsulation if enough data in first
4826520Sfeldman  * packet leaves a multiple of 512 bytes of data in remainder.
4836545Sfeldman  * If destination is this address or broadcast, send packet to
4846545Sfeldman  * loop device to kludge around the fact that 3com interfaces can't
4856545Sfeldman  * talk to themselves.
4866520Sfeldman  */
4876520Sfeldman ecoutput(ifp, m0, dst)
4886520Sfeldman 	struct ifnet *ifp;
4896520Sfeldman 	struct mbuf *m0;
4906520Sfeldman 	struct sockaddr *dst;
4916520Sfeldman {
4926520Sfeldman 	int type, dest, s, error;
4936520Sfeldman 	register struct ec_softc *es = &ec_softc[ifp->if_unit];
4946520Sfeldman 	register struct mbuf *m = m0;
4956520Sfeldman 	register struct ec_header *ec;
4967216Ssam 	register int off, i;
4976545Sfeldman 	struct mbuf *mcopy = (struct mbuf *) 0;		/* Null */
4986520Sfeldman 
4996520Sfeldman COUNT(ECOUTPUT);
5006520Sfeldman 	switch (dst->sa_family) {
5016520Sfeldman 
5026520Sfeldman #ifdef INET
5036520Sfeldman 	case AF_INET:
5046520Sfeldman 		dest = ((struct sockaddr_in *)dst)->sin_addr.s_addr;
5056545Sfeldman 		if ((dest &~ 0xff) == 0)
5066545Sfeldman 			mcopy = m_copy(m, 0, M_COPYALL);
5076545Sfeldman 		else if (dest == ((struct sockaddr_in *)&es->es_if.if_addr)->
5086545Sfeldman 		    sin_addr.s_addr) {
5096545Sfeldman 			mcopy = m;
5106545Sfeldman 			goto gotlocal;
5116545Sfeldman 		}
5126520Sfeldman 		off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len;
5136520Sfeldman 		if (off > 0 && (off & 0x1ff) == 0 &&
5146520Sfeldman 		    m->m_off >= MMINOFF + 2 * sizeof (u_short)) {
5156520Sfeldman 			type = ECPUP_TRAIL + (off>>9);
5166520Sfeldman 			m->m_off -= 2 * sizeof (u_short);
5176520Sfeldman 			m->m_len += 2 * sizeof (u_short);
5186520Sfeldman 			*mtod(m, u_short *) = ECPUP_IPTYPE;
5196520Sfeldman 			*(mtod(m, u_short *) + 1) = m->m_len;
5206520Sfeldman 			goto gottrailertype;
5216520Sfeldman 		}
5226520Sfeldman 		type = ECPUP_IPTYPE;
5236520Sfeldman 		off = 0;
5246520Sfeldman 		goto gottype;
5256520Sfeldman #endif
5266520Sfeldman 
5276520Sfeldman 	default:
5286520Sfeldman 		printf("ec%d: can't handle af%d\n", ifp->if_unit,
5296520Sfeldman 			dst->sa_family);
5306520Sfeldman 		error = EAFNOSUPPORT;
5316520Sfeldman 		goto bad;
5326520Sfeldman 	}
5336520Sfeldman 
5346520Sfeldman gottrailertype:
5356520Sfeldman 	/*
5366520Sfeldman 	 * Packet to be sent as trailer: move first packet
5376520Sfeldman 	 * (control information) to end of chain.
5386520Sfeldman 	 */
5396520Sfeldman 	while (m->m_next)
5406520Sfeldman 		m = m->m_next;
5416520Sfeldman 	m->m_next = m0;
5426520Sfeldman 	m = m0->m_next;
5436520Sfeldman 	m0->m_next = 0;
5446520Sfeldman 	m0 = m;
5456520Sfeldman 
5466520Sfeldman gottype:
5476520Sfeldman 	/*
5486520Sfeldman 	 * Add local net header.  If no space in first mbuf,
5496520Sfeldman 	 * allocate another.
5506520Sfeldman 	 */
5516520Sfeldman 	if (m->m_off > MMAXOFF ||
5526520Sfeldman 	    MMINOFF + sizeof (struct ec_header) > m->m_off) {
5536520Sfeldman 		m = m_get(M_DONTWAIT);
5546520Sfeldman 		if (m == 0) {
5556520Sfeldman 			error = ENOBUFS;
5566520Sfeldman 			goto bad;
5576520Sfeldman 		}
5586520Sfeldman 		m->m_next = m0;
5596520Sfeldman 		m->m_off = MMINOFF;
5606520Sfeldman 		m->m_len = sizeof (struct ec_header);
5616520Sfeldman 	} else {
5626520Sfeldman 		m->m_off -= sizeof (struct ec_header);
5636520Sfeldman 		m->m_len += sizeof (struct ec_header);
5646520Sfeldman 	}
5656520Sfeldman 	ec = mtod(m, struct ec_header *);
5666520Sfeldman 	for (i=0; i<6; i++)
5676520Sfeldman 		ec->ec_shost[i] = es->es_enaddr[i];
5686525Sfeldman 	if ((dest &~ 0xff) == 0)
5697216Ssam 		/* broadcast address */
5706520Sfeldman 		for (i=0; i<6; i++)
5716520Sfeldman 			ec->ec_dhost[i] = 0xff;
5726520Sfeldman 	else {
5736892Sfeldman 		if (dest & 0x8000) {
5746892Sfeldman 			ec->ec_dhost[0] = ec_iltop[0];
5756892Sfeldman 			ec->ec_dhost[1] = ec_iltop[1];
5766892Sfeldman 			ec->ec_dhost[2] = ec_iltop[2];
5776892Sfeldman 		} else {
5786892Sfeldman 			ec->ec_dhost[0] = es->es_enaddr[0];
5796892Sfeldman 			ec->ec_dhost[1] = es->es_enaddr[1];
5806892Sfeldman 			ec->ec_dhost[2] = es->es_enaddr[2];
5816892Sfeldman 		}
5826892Sfeldman 		ec->ec_dhost[3] = (dest>>8) & 0x7f;
5836520Sfeldman 		ec->ec_dhost[4] = (dest>>16) & 0xff;
5846520Sfeldman 		ec->ec_dhost[5] = (dest>>24) & 0xff;
5856520Sfeldman 	}
5866520Sfeldman 	ec->ec_type = 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 /*
6157216Ssam  * Routine to copy from mbuf chain to transmitter
6167216Ssam  * buffer in UNIBUS memory.
6176520Sfeldman  */
6186520Sfeldman ecput(ecbuf, m)
6197216Ssam 	u_char *ecbuf;
6206520Sfeldman 	struct mbuf *m;
6216520Sfeldman {
6226520Sfeldman 	register struct mbuf *mp;
6237216Ssam 	register u_char *bp;
6247216Ssam 	register int off;
6256520Sfeldman 
6266520Sfeldman COUNT(ECPUT);
6277216Ssam 	for (off = 2048, mp = m; mp; mp = mp->m_next)
6287216Ssam 		off -= mp->m_len;
6297216Ssam 	*(u_short *)ecbuf = off;
6307216Ssam 	bp = (u_char *)(ecbuf + off);
6317216Ssam 	for (mp = m; mp; mp = m_free(mp)) {
6327216Ssam 		register unsigned len;
6337216Ssam 		register u_char *mcp;
6347216Ssam 
6357216Ssam 		len = mp->m_len;
6367216Ssam 		if (len == 0)
6377216Ssam 			continue;
6387216Ssam 		mcp = mtod(mp, u_char *);
6397216Ssam 		if ((unsigned)bp & 01) {
6407032Swnj 			*bp++ = *mcp++;
6417216Ssam 			len--;
6427032Swnj 		}
6437216Ssam 		for (; len > 1; len -= sizeof (u_short)) {
6447216Ssam 			*(u_short *)bp = *(u_short *)mcp;
6457216Ssam 			bp += sizeof (u_short);
6467216Ssam 			mcp += sizeof (u_short);
6477032Swnj 		}
6487216Ssam 		if (len)
6496520Sfeldman 			*bp++ = *mcp++;
6506520Sfeldman 	}
6517216Ssam #ifdef notdef
6527216Ssam 	if (bp - ecbuf != 2048)
6537216Ssam 		printf("ec: bad ecput, diff=%d\n", bp-ecbuf);
6547216Ssam #endif
6556520Sfeldman }
6566520Sfeldman 
6576520Sfeldman /*
6586520Sfeldman  * Routine to copy from UNIBUS memory into mbufs.
6596520Sfeldman  * Similar in spirit to if_rubaget.
6607032Swnj  *
6617032Swnj  * Warning: This makes the fairly safe assumption that
6627032Swnj  * mbufs have even lengths.
6636520Sfeldman  */
6646520Sfeldman struct mbuf *
6656520Sfeldman ecget(ecbuf, totlen, off0)
6666520Sfeldman 	char *ecbuf;
6676520Sfeldman 	int totlen, off0;
6686520Sfeldman {
6696520Sfeldman 	struct mbuf *top, **mp, *m;
6707216Ssam 	int off = off0, len;
6717216Ssam 	register char *cp, *mcp;
6726520Sfeldman 	register int i;
6736520Sfeldman 
6746520Sfeldman COUNT(ECGET);
6756520Sfeldman 	top = 0;
6766520Sfeldman 	mp = &top;
6777216Ssam 	cp = ecbuf + ECRDOFF + sizeof (struct ec_header);
6786520Sfeldman 	while (totlen > 0) {
6796520Sfeldman 		MGET(m, 0);
6806520Sfeldman 		if (m == 0)
6816520Sfeldman 			goto bad;
6826520Sfeldman 		if (off) {
6836520Sfeldman 			len = totlen - off;
6846520Sfeldman 			cp = ecbuf + ECRDOFF + sizeof (struct ec_header) + off;
6856520Sfeldman 		} else
6866520Sfeldman 			len = totlen;
6876520Sfeldman 		if (len >= CLBYTES) {
6886520Sfeldman 			struct mbuf *p;
6896520Sfeldman 
6906520Sfeldman 			MCLGET(p, 1);
6916520Sfeldman 			if (p != 0) {
6926520Sfeldman 				m->m_len = len = CLBYTES;
6936520Sfeldman 				m->m_off = (int)p - (int)m;
6946520Sfeldman 			} else {
6956520Sfeldman 				m->m_len = len = MIN(MLEN, len);
6966520Sfeldman 				m->m_off = MMINOFF;
6976520Sfeldman 			}
6986520Sfeldman 		} else {
6996520Sfeldman 			m->m_len = len = MIN(MLEN, len);
7006520Sfeldman 			m->m_off = MMINOFF;
7016520Sfeldman 		}
7026520Sfeldman 		mcp = mtod(m, char *);
7037216Ssam 		for (i = 0; i < len; i += sizeof (short)) {
7047032Swnj 			*(short *)mcp = *(short *)cp;
7057216Ssam 			mcp += sizeof (short);
7067216Ssam 			cp += sizeof (short);
7077032Swnj 		}
7087216Ssam 		if (len & 01)
7096520Sfeldman 			*mcp++ = *cp++;
7106520Sfeldman 		*mp = m;
7116520Sfeldman 		mp = &m->m_next;
7126520Sfeldman 		if (off) {
7136520Sfeldman 			off += len;
7146520Sfeldman 			if (off == totlen) {
7156520Sfeldman 				cp = ecbuf + ECRDOFF +
7166520Sfeldman 				    sizeof (struct ec_header);
7176520Sfeldman 				off = 0;
7186520Sfeldman 				totlen = off0;
7196520Sfeldman 			}
7206520Sfeldman 		} else
7216520Sfeldman 			totlen -= len;
7226520Sfeldman 	}
7236520Sfeldman 	return (top);
7246520Sfeldman bad:
7256520Sfeldman 	m_freem(top);
7266520Sfeldman 	return (0);
7276520Sfeldman }
728