xref: /csrg-svn/sys/vax/if/if_ec.c (revision 7263)
1*7263Ssam /*	if_ec.c	4.20	82/06/23	*/
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 #ifdef lint
816520Sfeldman 	br = 0; cvec = br; br = cvec;
826520Sfeldman 	ecrint(0); ecxint(0); eccollide(0);
836520Sfeldman #endif
846520Sfeldman 	/*
856637Sfeldman 	 * Make sure memory is turned on
866637Sfeldman 	 */
876637Sfeldman 	addr->ec_rcr = EC_AROM;
886637Sfeldman 	/*
896520Sfeldman 	 * Check for existence of buffers on Unibus.
906520Sfeldman 	 * This won't work on a 780 until more work is done.
916520Sfeldman 	 */
926520Sfeldman 	if (badaddr((caddr_t) ecbuf, 2)) {
936520Sfeldman 		printf("ec: buffer mem not found");
946520Sfeldman 		return (0);
956520Sfeldman 	}
966520Sfeldman 
976520Sfeldman 	/*
986520Sfeldman 	 * Tell the system that the board has memory here, so it won't
996520Sfeldman 	 * attempt to allocate the addresses later.
1006520Sfeldman 	 */
1016520Sfeldman 	ubamem(0, 0600000, 32*2);
1026520Sfeldman 
1036520Sfeldman 	/*
1046520Sfeldman 	 * Make a one byte packet in what should be buffer #0.
1056520Sfeldman 	 * Submit it for sending.  This whould cause an xmit interrupt.
1066520Sfeldman 	 * The xmit interrupt vector is 8 bytes after the receive vector,
1076520Sfeldman 	 * so adjust for this before returning.
1086520Sfeldman 	 */
1096520Sfeldman 	*(u_short *)ecbuf = (u_short) 03777;
1106520Sfeldman 	ecbuf[03777] = '\0';
1116520Sfeldman 	addr->ec_xcr = EC_XINTEN|EC_XWBN;
1126520Sfeldman 	DELAY(100000);
1136520Sfeldman 	addr->ec_xcr = EC_XCLR;
1147216Ssam 	if (cvec > 0 && cvec != 0x200) {
1156520Sfeldman 		cvec -= 010;
1167216Ssam 		br += 2;		/* rcv is xmit + 2 */
1177216Ssam 	}
1186520Sfeldman 	return (1);
1196520Sfeldman }
1206520Sfeldman 
1216520Sfeldman /*
1226520Sfeldman  * Interface exists: make available by filling in network interface
1236520Sfeldman  * record.  System will initialize the interface when it is ready
1246520Sfeldman  * to accept packets.
1256520Sfeldman  */
1266520Sfeldman ecattach(ui)
1276520Sfeldman 	struct uba_device *ui;
1286520Sfeldman {
1297216Ssam 	struct ec_softc *es = &ec_softc[ui->ui_unit];
1307216Ssam 	register struct ifnet *ifp = &es->es_if;
1316520Sfeldman 	register struct ecdevice *addr = (struct ecdevice *)ui->ui_addr;
1327216Ssam 	struct sockaddr_in *sin;
1337216Ssam 	int i, j;
1347216Ssam 	u_char *cp;
1356520Sfeldman 
1367216Ssam 	ifp->if_unit = ui->ui_unit;
1377216Ssam 	ifp->if_name = "ec";
1387216Ssam 	ifp->if_mtu = ECMTU;
1397216Ssam 	ifp->if_net = ui->ui_flags;
1406520Sfeldman 
1416520Sfeldman 	/*
1427216Ssam 	 * Read the ethernet address off the board, one nibble at a time.
1436520Sfeldman 	 */
1446520Sfeldman 	addr->ec_xcr = EC_UECLR;
1456520Sfeldman 	addr->ec_rcr = EC_AROM;
1466520Sfeldman 	cp = es->es_enaddr;
1477216Ssam #define	NEXTBIT	addr->ec_rcr = EC_AROM|EC_ASTEP; addr->ec_rcr = EC_AROM
1486520Sfeldman 	for (i=0; i<6; i++) {
1496520Sfeldman 		*cp = 0;
1506520Sfeldman 		for (j=0; j<=4; j+=4) {
1516520Sfeldman 			*cp |= ((addr->ec_rcr >> 8) & 0xf) << j;
1527216Ssam 			NEXTBIT; NEXTBIT; NEXTBIT; NEXTBIT;
1536520Sfeldman 		}
1546520Sfeldman 		cp++;
1556520Sfeldman 	}
1567216Ssam #ifdef notdef
1576520Sfeldman 	printf("ec%d: addr=%x:%x:%x:%x:%x:%x\n", ui->ui_unit,
1586520Sfeldman 		es->es_enaddr[0]&0xff, es->es_enaddr[1]&0xff,
1596520Sfeldman 		es->es_enaddr[2]&0xff, es->es_enaddr[3]&0xff,
1606520Sfeldman 		es->es_enaddr[4]&0xff, es->es_enaddr[5]&0xff);
1617216Ssam #endif
1627216Ssam 	ifp->if_host[0] = ((es->es_enaddr[3]&0xff)<<16) |
1636520Sfeldman 	    ((es->es_enaddr[4]&0xff)<<8) | (es->es_enaddr[5]&0xff);
1646520Sfeldman 	sin = (struct sockaddr_in *)&es->es_if.if_addr;
1656520Sfeldman 	sin->sin_family = AF_INET;
1667216Ssam 	sin->sin_addr = if_makeaddr(ifp->if_net, ifp->if_host[0]);
1676520Sfeldman 
1687216Ssam 	sin = (struct sockaddr_in *)&ifp->if_broadaddr;
1696520Sfeldman 	sin->sin_family = AF_INET;
1707216Ssam 	sin->sin_addr = if_makeaddr(ifp->if_net, INADDR_ANY);
1717216Ssam 	ifp->if_flags = IFF_BROADCAST;
1726520Sfeldman 
1737216Ssam 	ifp->if_init = ecinit;
1747216Ssam 	ifp->if_output = ecoutput;
1757216Ssam 	ifp->if_ubareset = ecreset;
1766520Sfeldman 	for (i=0; i<16; i++)
1776520Sfeldman 		es->es_buf[i] = &umem[ui->ui_ubanum][0600000+2048*i];
1787216Ssam 	if_attach(ifp);
1796520Sfeldman }
1806520Sfeldman 
1816520Sfeldman /*
1826520Sfeldman  * Reset of interface after UNIBUS reset.
1836520Sfeldman  * If interface is on specified uba, reset its state.
1846520Sfeldman  */
1856520Sfeldman ecreset(unit, uban)
1866520Sfeldman 	int unit, uban;
1876520Sfeldman {
1886520Sfeldman 	register struct uba_device *ui;
1896520Sfeldman 
1906520Sfeldman 	if (unit >= NEC || (ui = ecinfo[unit]) == 0 || ui->ui_alive == 0 ||
1916520Sfeldman 	    ui->ui_ubanum != uban)
1926520Sfeldman 		return;
1936520Sfeldman 	printf(" ec%d", unit);
1946520Sfeldman 	ecinit(unit);
1956520Sfeldman }
1966520Sfeldman 
1976520Sfeldman /*
1986520Sfeldman  * Initialization of interface; clear recorded pending
1996520Sfeldman  * operations, and reinitialize UNIBUS usage.
2006520Sfeldman  */
2016520Sfeldman ecinit(unit)
2026520Sfeldman 	int unit;
2036520Sfeldman {
2047216Ssam 	struct ec_softc *es = &ec_softc[unit];
2057216Ssam 	struct ecdevice *addr;
2067216Ssam 	int i, s;
2076520Sfeldman 
2086520Sfeldman 	/*
2097217Sfeldman 	 * Hang receive buffers and start any pending writes.
2106637Sfeldman 	 * Writing into the rcr also makes sure the memory
2116637Sfeldman 	 * is turned on.
2126520Sfeldman 	 */
2137216Ssam 	addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
2146520Sfeldman 	s = splimp();
2156520Sfeldman 	for (i=ECRHBF; i>=ECRLBF; i--)
2166520Sfeldman 		addr->ec_rcr = EC_READ|i;
2177217Sfeldman 	es->es_oactive = 0;
2187217Sfeldman 	es->es_mask = ~0;
2196520Sfeldman 	es->es_if.if_flags |= IFF_UP;
2207217Sfeldman 	if (es->es_if.if_snd.ifq_head)
2217217Sfeldman 		ecstart(unit);
2226520Sfeldman 	splx(s);
2237150Swnj 	if_rtinit(&es->es_if, RTF_UP);
2246520Sfeldman }
2256520Sfeldman 
2266520Sfeldman /*
2276520Sfeldman  * Start or restart output on interface.
2286520Sfeldman  * If interface is already active, then this is a retransmit
2296545Sfeldman  * after a collision, and just restuff registers.
2306520Sfeldman  * If interface is not already active, get another datagram
2316520Sfeldman  * to send off of the interface queue, and map it to the interface
2326520Sfeldman  * before starting the output.
2336520Sfeldman  */
2346520Sfeldman ecstart(dev)
2356520Sfeldman 	dev_t dev;
2366520Sfeldman {
2377216Ssam         int unit = ECUNIT(dev), dest;
2387216Ssam 	struct ec_softc *es = &ec_softc[unit];
2397216Ssam 	struct ecdevice *addr;
2406520Sfeldman 	struct mbuf *m;
2416520Sfeldman 	caddr_t ecbuf;
2426520Sfeldman 
2436520Sfeldman 	if (es->es_oactive)
2446520Sfeldman 		goto restart;
2456520Sfeldman 
2466520Sfeldman 	IF_DEQUEUE(&es->es_if.if_snd, m);
2476520Sfeldman 	if (m == 0) {
2486520Sfeldman 		es->es_oactive = 0;
2496520Sfeldman 		return;
2506520Sfeldman 	}
2516520Sfeldman 	ecput(es->es_buf[ECTBF], m);
2526520Sfeldman 
2536520Sfeldman restart:
2547216Ssam 	addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
2556520Sfeldman 	addr->ec_xcr = EC_WRITE|ECTBF;
2566520Sfeldman 	es->es_oactive = 1;
2576520Sfeldman }
2586520Sfeldman 
2596520Sfeldman /*
2606520Sfeldman  * Ethernet interface transmitter interrupt.
2616520Sfeldman  * Start another output if more data to send.
2626520Sfeldman  */
2636520Sfeldman ecxint(unit)
2646520Sfeldman 	int unit;
2656520Sfeldman {
2666520Sfeldman 	register struct ec_softc *es = &ec_softc[unit];
2677216Ssam 	register struct ecdevice *addr =
2687216Ssam 		(struct ecdevice *)ecinfo[unit]->ui_addr;
2696520Sfeldman 
2706520Sfeldman 	if (es->es_oactive == 0)
2716520Sfeldman 		return;
2727216Ssam 	if ((addr->ec_xcr&EC_XDONE) == 0 || (addr->ec_xcr&EC_XBN) != ECTBF) {
2737216Ssam 		printf("ec%d: stray xmit interrupt, xcr=%b\n", unit,
2747216Ssam 			addr->ec_xcr, EC_XBITS);
2757216Ssam 		es->es_oactive = 0;
2767216Ssam 		addr->ec_xcr = EC_XCLR;
2777216Ssam 		return;
2787216Ssam 	}
2796520Sfeldman 	es->es_if.if_opackets++;
2806520Sfeldman 	es->es_oactive = 0;
2816520Sfeldman 	es->es_mask = ~0;
2826520Sfeldman 	addr->ec_xcr = EC_XCLR;
2837216Ssam 	if (es->es_if.if_snd.ifq_head)
2847216Ssam 		ecstart(unit);
2856520Sfeldman }
2866520Sfeldman 
2876520Sfeldman /*
2886520Sfeldman  * Collision on ethernet interface.  Do exponential
2896520Sfeldman  * backoff, and retransmit.  If have backed off all
2906520Sfeldman  * the way print warning diagnostic, and drop packet.
2916520Sfeldman  */
2926520Sfeldman eccollide(unit)
2936520Sfeldman 	int unit;
2946520Sfeldman {
2956520Sfeldman 	struct ec_softc *es = &ec_softc[unit];
2966520Sfeldman 
2976520Sfeldman 	es->es_if.if_collisions++;
2987216Ssam 	if (es->es_oactive)
2997216Ssam 		ecdocoll(unit);
3006520Sfeldman }
3016520Sfeldman 
3026520Sfeldman ecdocoll(unit)
3036520Sfeldman 	int unit;
3046520Sfeldman {
3056520Sfeldman 	register struct ec_softc *es = &ec_softc[unit];
3066545Sfeldman 	register struct ecdevice *addr =
3076545Sfeldman 	    (struct ecdevice *)ecinfo[unit]->ui_addr;
3086545Sfeldman 	register i;
3096545Sfeldman 	int delay;
3106520Sfeldman 
3116520Sfeldman 	/*
3126520Sfeldman 	 * Es_mask is a 16 bit number with n low zero bits, with
3136520Sfeldman 	 * n the number of backoffs.  When es_mask is 0 we have
3146520Sfeldman 	 * backed off 16 times, and give up.
3156520Sfeldman 	 */
3166520Sfeldman 	if (es->es_mask == 0) {
3176545Sfeldman 		es->es_if.if_oerrors++;
3186520Sfeldman 		printf("ec%d: send error\n", unit);
3196520Sfeldman 		/*
3206545Sfeldman 		 * Reset interface, then requeue rcv buffers.
3216545Sfeldman 		 * Some incoming packets may be lost, but that
3226545Sfeldman 		 * can't be helped.
3236520Sfeldman 		 */
3246545Sfeldman 		addr->ec_xcr = EC_UECLR;
3256545Sfeldman 		for (i=ECRHBF; i>=ECRLBF; i--)
3266545Sfeldman 			addr->ec_rcr = EC_READ|i;
3276545Sfeldman 		/*
3286545Sfeldman 		 * Reset and transmit next packet (if any).
3296545Sfeldman 		 */
3306545Sfeldman 		es->es_oactive = 0;
3316545Sfeldman 		es->es_mask = ~0;
3326545Sfeldman 		if (es->es_if.if_snd.ifq_head)
3336545Sfeldman 			ecstart(unit);
3346520Sfeldman 		return;
3356520Sfeldman 	}
3366520Sfeldman 	/*
3376545Sfeldman 	 * Do exponential backoff.  Compute delay based on low bits
3386545Sfeldman 	 * of the interval timer.  Then delay for that number of
3396545Sfeldman 	 * slot times.  A slot time is 51.2 microseconds (rounded to 51).
3406545Sfeldman 	 * This does not take into account the time already used to
3416545Sfeldman 	 * process the interrupt.
3426520Sfeldman 	 */
3436520Sfeldman 	es->es_mask <<= 1;
3446545Sfeldman 	delay = mfpr(ICR) &~ es->es_mask;
3456545Sfeldman 	DELAY(delay * 51);
3466520Sfeldman 	/*
3476545Sfeldman 	 * Clear the controller's collision flag, thus enabling retransmit.
3486520Sfeldman 	 */
3496717Sfeldman 	addr->ec_xcr = EC_JINTEN|EC_XINTEN|EC_JCLR;
3506520Sfeldman }
3516520Sfeldman 
3526520Sfeldman /*
3536520Sfeldman  * Ethernet interface receiver interrupt.
3546520Sfeldman  * If input error just drop packet.
3556520Sfeldman  * Otherwise purge input buffered data path and examine
3566520Sfeldman  * packet to determine type.  If can't determine length
3576520Sfeldman  * from type, then have to drop packet.  Othewise decapsulate
3586520Sfeldman  * packet based on type and pass to type specific higher-level
3596520Sfeldman  * input routine.
3606520Sfeldman  */
3616520Sfeldman ecrint(unit)
3626520Sfeldman 	int unit;
3636520Sfeldman {
3646520Sfeldman 	struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
3656520Sfeldman 
3666520Sfeldman 	while (addr->ec_rcr & EC_RDONE)
3676520Sfeldman 		ecread(unit);
3686520Sfeldman }
3696520Sfeldman 
3706520Sfeldman ecread(unit)
3716520Sfeldman 	int unit;
3726520Sfeldman {
3736520Sfeldman 	register struct ec_softc *es = &ec_softc[unit];
3746520Sfeldman 	struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
3756520Sfeldman 	register struct ec_header *ec;
3766520Sfeldman     	struct mbuf *m;
3777216Ssam 	int len, off, resid, ecoff, buf;
3786520Sfeldman 	register struct ifqueue *inq;
3796520Sfeldman 	caddr_t ecbuf;
3806520Sfeldman 
3816520Sfeldman 	es->es_if.if_ipackets++;
3826520Sfeldman 	buf = addr->ec_rcr & EC_RBN;
3836520Sfeldman 	if (buf < ECRLBF || buf > ECRHBF)
3846520Sfeldman 		panic("ecrint");
3856520Sfeldman 	ecbuf = es->es_buf[buf];
3866520Sfeldman 	ecoff = *(short *)ecbuf;
3876545Sfeldman 	if (ecoff <= ECRDOFF || ecoff > 2046) {
3886520Sfeldman 		es->es_if.if_ierrors++;
3896520Sfeldman #ifdef notdef
3906520Sfeldman 		if (es->es_if.if_ierrors % 100 == 0)
3916520Sfeldman 			printf("ec%d: += 100 input errors\n", unit);
3926520Sfeldman #endif
3936520Sfeldman 		goto setup;
3946520Sfeldman 	}
3956520Sfeldman 
3966520Sfeldman 	/*
3976520Sfeldman 	 * Get input data length.
3986520Sfeldman 	 * Get pointer to ethernet header (in input buffer).
3996520Sfeldman 	 * Deal with trailer protocol: if type is PUP trailer
4006520Sfeldman 	 * get true type from first 16-bit word past data.
4016520Sfeldman 	 * Remember that type was trailer by setting off.
4026520Sfeldman 	 */
4036520Sfeldman 	len = ecoff - ECRDOFF - sizeof (struct ec_header);
4046520Sfeldman 	ec = (struct ec_header *)(ecbuf + ECRDOFF);
4056520Sfeldman #define	ecdataaddr(ec, off, type)	((type)(((caddr_t)((ec)+1)+(off))))
4066520Sfeldman 	if (ec->ec_type >= ECPUP_TRAIL &&
4076520Sfeldman 	    ec->ec_type < ECPUP_TRAIL+ECPUP_NTRAILER) {
4086520Sfeldman 		off = (ec->ec_type - ECPUP_TRAIL) * 512;
4096520Sfeldman 		if (off >= ECMTU)
4106520Sfeldman 			goto setup;		/* sanity */
4116520Sfeldman 		ec->ec_type = *ecdataaddr(ec, off, u_short *);
4126520Sfeldman 		resid = *(ecdataaddr(ec, off+2, u_short *));
4136520Sfeldman 		if (off + resid > len)
4146520Sfeldman 			goto setup;		/* sanity */
4156520Sfeldman 		len = off + resid;
4166520Sfeldman 	} else
4176520Sfeldman 		off = 0;
4186520Sfeldman 	if (len == 0)
4196520Sfeldman 		goto setup;
4206520Sfeldman 
4216520Sfeldman 	/*
4226520Sfeldman 	 * Pull packet off interface.  Off is nonzero if packet
4236520Sfeldman 	 * has trailing header; ecget will then force this header
4246520Sfeldman 	 * information to be at the front, but we still have to drop
4256520Sfeldman 	 * the type and length which are at the front of any trailer data.
4266520Sfeldman 	 */
4276520Sfeldman 	m = ecget(ecbuf, len, off);
4286520Sfeldman 	if (m == 0)
4296520Sfeldman 		goto setup;
4306520Sfeldman 	if (off) {
4316520Sfeldman 		m->m_off += 2 * sizeof (u_short);
4326520Sfeldman 		m->m_len -= 2 * sizeof (u_short);
4336520Sfeldman 	}
4346520Sfeldman 	switch (ec->ec_type) {
4356520Sfeldman 
4366520Sfeldman #ifdef INET
4376520Sfeldman 	case ECPUP_IPTYPE:
4386520Sfeldman 		schednetisr(NETISR_IP);
4396520Sfeldman 		inq = &ipintrq;
4406520Sfeldman 		break;
4416520Sfeldman #endif
4426520Sfeldman 	default:
4436520Sfeldman 		m_freem(m);
4446520Sfeldman 		goto setup;
4456520Sfeldman 	}
4466520Sfeldman 
4476520Sfeldman 	if (IF_QFULL(inq)) {
4486520Sfeldman 		IF_DROP(inq);
4496520Sfeldman 		m_freem(m);
4507216Ssam 		goto setup;
4517216Ssam 	}
4527216Ssam 	IF_ENQUEUE(inq, m);
4536520Sfeldman 
4546520Sfeldman setup:
4556520Sfeldman 	/*
4566520Sfeldman 	 * Reset for next packet.
4576520Sfeldman 	 */
4586520Sfeldman 	addr->ec_rcr = EC_READ|EC_RCLR|buf;
4596520Sfeldman }
4606520Sfeldman 
4616520Sfeldman /*
4626520Sfeldman  * Ethernet output routine.
4636520Sfeldman  * Encapsulate a packet of type family for the local net.
4646520Sfeldman  * Use trailer local net encapsulation if enough data in first
4656520Sfeldman  * packet leaves a multiple of 512 bytes of data in remainder.
4666545Sfeldman  * If destination is this address or broadcast, send packet to
4676545Sfeldman  * loop device to kludge around the fact that 3com interfaces can't
4686545Sfeldman  * talk to themselves.
4696520Sfeldman  */
4706520Sfeldman ecoutput(ifp, m0, dst)
4716520Sfeldman 	struct ifnet *ifp;
4726520Sfeldman 	struct mbuf *m0;
4736520Sfeldman 	struct sockaddr *dst;
4746520Sfeldman {
4756520Sfeldman 	int type, dest, s, error;
4766520Sfeldman 	register struct ec_softc *es = &ec_softc[ifp->if_unit];
4776520Sfeldman 	register struct mbuf *m = m0;
4786520Sfeldman 	register struct ec_header *ec;
4797216Ssam 	register int off, i;
4806545Sfeldman 	struct mbuf *mcopy = (struct mbuf *) 0;		/* Null */
4816520Sfeldman 
4826520Sfeldman 	switch (dst->sa_family) {
4836520Sfeldman 
4846520Sfeldman #ifdef INET
4856520Sfeldman 	case AF_INET:
4866520Sfeldman 		dest = ((struct sockaddr_in *)dst)->sin_addr.s_addr;
4876545Sfeldman 		if ((dest &~ 0xff) == 0)
4886545Sfeldman 			mcopy = m_copy(m, 0, M_COPYALL);
4896545Sfeldman 		else if (dest == ((struct sockaddr_in *)&es->es_if.if_addr)->
4906545Sfeldman 		    sin_addr.s_addr) {
4916545Sfeldman 			mcopy = m;
4926545Sfeldman 			goto gotlocal;
4936545Sfeldman 		}
4946520Sfeldman 		off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len;
4956520Sfeldman 		if (off > 0 && (off & 0x1ff) == 0 &&
4966520Sfeldman 		    m->m_off >= MMINOFF + 2 * sizeof (u_short)) {
4976520Sfeldman 			type = ECPUP_TRAIL + (off>>9);
4986520Sfeldman 			m->m_off -= 2 * sizeof (u_short);
4996520Sfeldman 			m->m_len += 2 * sizeof (u_short);
5006520Sfeldman 			*mtod(m, u_short *) = ECPUP_IPTYPE;
5016520Sfeldman 			*(mtod(m, u_short *) + 1) = m->m_len;
5026520Sfeldman 			goto gottrailertype;
5036520Sfeldman 		}
5046520Sfeldman 		type = ECPUP_IPTYPE;
5056520Sfeldman 		off = 0;
5066520Sfeldman 		goto gottype;
5076520Sfeldman #endif
5086520Sfeldman 
5096520Sfeldman 	default:
5106520Sfeldman 		printf("ec%d: can't handle af%d\n", ifp->if_unit,
5116520Sfeldman 			dst->sa_family);
5126520Sfeldman 		error = EAFNOSUPPORT;
5136520Sfeldman 		goto bad;
5146520Sfeldman 	}
5156520Sfeldman 
5166520Sfeldman gottrailertype:
5176520Sfeldman 	/*
5186520Sfeldman 	 * Packet to be sent as trailer: move first packet
5196520Sfeldman 	 * (control information) to end of chain.
5206520Sfeldman 	 */
5216520Sfeldman 	while (m->m_next)
5226520Sfeldman 		m = m->m_next;
5236520Sfeldman 	m->m_next = m0;
5246520Sfeldman 	m = m0->m_next;
5256520Sfeldman 	m0->m_next = 0;
5266520Sfeldman 	m0 = m;
5276520Sfeldman 
5286520Sfeldman gottype:
5296520Sfeldman 	/*
5306520Sfeldman 	 * Add local net header.  If no space in first mbuf,
5316520Sfeldman 	 * allocate another.
5326520Sfeldman 	 */
5336520Sfeldman 	if (m->m_off > MMAXOFF ||
5346520Sfeldman 	    MMINOFF + sizeof (struct ec_header) > m->m_off) {
5356520Sfeldman 		m = m_get(M_DONTWAIT);
5366520Sfeldman 		if (m == 0) {
5376520Sfeldman 			error = ENOBUFS;
5386520Sfeldman 			goto bad;
5396520Sfeldman 		}
5406520Sfeldman 		m->m_next = m0;
5416520Sfeldman 		m->m_off = MMINOFF;
5426520Sfeldman 		m->m_len = sizeof (struct ec_header);
5436520Sfeldman 	} else {
5446520Sfeldman 		m->m_off -= sizeof (struct ec_header);
5456520Sfeldman 		m->m_len += sizeof (struct ec_header);
5466520Sfeldman 	}
5476520Sfeldman 	ec = mtod(m, struct ec_header *);
5486520Sfeldman 	for (i=0; i<6; i++)
5496520Sfeldman 		ec->ec_shost[i] = es->es_enaddr[i];
5506525Sfeldman 	if ((dest &~ 0xff) == 0)
5517216Ssam 		/* broadcast address */
5526520Sfeldman 		for (i=0; i<6; i++)
5536520Sfeldman 			ec->ec_dhost[i] = 0xff;
5546520Sfeldman 	else {
5556892Sfeldman 		if (dest & 0x8000) {
5566892Sfeldman 			ec->ec_dhost[0] = ec_iltop[0];
5576892Sfeldman 			ec->ec_dhost[1] = ec_iltop[1];
5586892Sfeldman 			ec->ec_dhost[2] = ec_iltop[2];
5596892Sfeldman 		} else {
5606892Sfeldman 			ec->ec_dhost[0] = es->es_enaddr[0];
5616892Sfeldman 			ec->ec_dhost[1] = es->es_enaddr[1];
5626892Sfeldman 			ec->ec_dhost[2] = es->es_enaddr[2];
5636892Sfeldman 		}
5646892Sfeldman 		ec->ec_dhost[3] = (dest>>8) & 0x7f;
5656520Sfeldman 		ec->ec_dhost[4] = (dest>>16) & 0xff;
5666520Sfeldman 		ec->ec_dhost[5] = (dest>>24) & 0xff;
5676520Sfeldman 	}
5686520Sfeldman 	ec->ec_type = type;
5696520Sfeldman 
5706520Sfeldman 	/*
5716520Sfeldman 	 * Queue message on interface, and start output if interface
5726520Sfeldman 	 * not yet active.
5736520Sfeldman 	 */
5746520Sfeldman 	s = splimp();
5756520Sfeldman 	if (IF_QFULL(&ifp->if_snd)) {
5766520Sfeldman 		IF_DROP(&ifp->if_snd);
5776520Sfeldman 		error = ENOBUFS;
5786520Sfeldman 		goto qfull;
5796520Sfeldman 	}
5806520Sfeldman 	IF_ENQUEUE(&ifp->if_snd, m);
5816520Sfeldman 	if (es->es_oactive == 0)
5826520Sfeldman 		ecstart(ifp->if_unit);
5836520Sfeldman 	splx(s);
5847216Ssam 
5856545Sfeldman gotlocal:
5867216Ssam 	return(mcopy ? looutput(&loif, mcopy, dst) : 0);
5877216Ssam 
5886520Sfeldman qfull:
5896520Sfeldman 	m0 = m;
5906520Sfeldman 	splx(s);
5916520Sfeldman bad:
5926520Sfeldman 	m_freem(m0);
5936520Sfeldman 	return(error);
5946520Sfeldman }
5956520Sfeldman 
5966520Sfeldman /*
5977216Ssam  * Routine to copy from mbuf chain to transmitter
5987216Ssam  * buffer in UNIBUS memory.
5996520Sfeldman  */
6006520Sfeldman ecput(ecbuf, m)
6017216Ssam 	u_char *ecbuf;
6026520Sfeldman 	struct mbuf *m;
6036520Sfeldman {
6046520Sfeldman 	register struct mbuf *mp;
6057216Ssam 	register int off;
606*7263Ssam 	u_char *bp;
6076520Sfeldman 
6087216Ssam 	for (off = 2048, mp = m; mp; mp = mp->m_next)
6097216Ssam 		off -= mp->m_len;
6107216Ssam 	*(u_short *)ecbuf = off;
6117216Ssam 	bp = (u_char *)(ecbuf + off);
612*7263Ssam 	for (mp = m; mp; mp = mp->m_next) {
613*7263Ssam 		register unsigned len = mp->m_len;
614*7263Ssam 		u_char *mcp;
6157216Ssam 
6167216Ssam 		if (len == 0)
6177216Ssam 			continue;
6187216Ssam 		mcp = mtod(mp, u_char *);
6197216Ssam 		if ((unsigned)bp & 01) {
6207032Swnj 			*bp++ = *mcp++;
6217216Ssam 			len--;
6227032Swnj 		}
623*7263Ssam 		if (off = (len >> 1)) {
624*7263Ssam 			register u_short *to, *from;
625*7263Ssam 
626*7263Ssam 			to = (u_short *)bp;
627*7263Ssam 			from = (u_short *)mcp;
628*7263Ssam 			do
629*7263Ssam 				*to++ = *from++;
630*7263Ssam 			while (--off > 0);
631*7263Ssam 			bp = (u_char *)to,
632*7263Ssam 			mcp = (u_char *)from;
6337032Swnj 		}
634*7263Ssam 		if (len & 01)
6356520Sfeldman 			*bp++ = *mcp++;
6366520Sfeldman 	}
6377216Ssam #ifdef notdef
6387216Ssam 	if (bp - ecbuf != 2048)
6397216Ssam 		printf("ec: bad ecput, diff=%d\n", bp-ecbuf);
6407216Ssam #endif
641*7263Ssam 	m_freem(m);
6426520Sfeldman }
6436520Sfeldman 
6446520Sfeldman /*
6456520Sfeldman  * Routine to copy from UNIBUS memory into mbufs.
6466520Sfeldman  * Similar in spirit to if_rubaget.
6477032Swnj  *
6487032Swnj  * Warning: This makes the fairly safe assumption that
6497032Swnj  * mbufs have even lengths.
6506520Sfeldman  */
6516520Sfeldman struct mbuf *
6526520Sfeldman ecget(ecbuf, totlen, off0)
653*7263Ssam 	u_char *ecbuf;
6546520Sfeldman 	int totlen, off0;
6556520Sfeldman {
656*7263Ssam 	register struct mbuf *m;
657*7263Ssam 	struct mbuf *top = 0, **mp = &top;
658*7263Ssam 	register int off = off0, len;
659*7263Ssam 	u_char *cp;
6606520Sfeldman 
6617216Ssam 	cp = ecbuf + ECRDOFF + sizeof (struct ec_header);
6626520Sfeldman 	while (totlen > 0) {
663*7263Ssam 		register int words;
664*7263Ssam 		u_char *mcp;
665*7263Ssam 
6666520Sfeldman 		MGET(m, 0);
6676520Sfeldman 		if (m == 0)
6686520Sfeldman 			goto bad;
6696520Sfeldman 		if (off) {
6706520Sfeldman 			len = totlen - off;
6716520Sfeldman 			cp = ecbuf + ECRDOFF + sizeof (struct ec_header) + off;
6726520Sfeldman 		} else
6736520Sfeldman 			len = totlen;
6746520Sfeldman 		if (len >= CLBYTES) {
6756520Sfeldman 			struct mbuf *p;
6766520Sfeldman 
6776520Sfeldman 			MCLGET(p, 1);
6786520Sfeldman 			if (p != 0) {
6796520Sfeldman 				m->m_len = len = CLBYTES;
6806520Sfeldman 				m->m_off = (int)p - (int)m;
6816520Sfeldman 			} else {
6826520Sfeldman 				m->m_len = len = MIN(MLEN, len);
6836520Sfeldman 				m->m_off = MMINOFF;
6846520Sfeldman 			}
6856520Sfeldman 		} else {
6866520Sfeldman 			m->m_len = len = MIN(MLEN, len);
6876520Sfeldman 			m->m_off = MMINOFF;
6886520Sfeldman 		}
689*7263Ssam 		mcp = mtod(m, u_char *);
690*7263Ssam 		if (words = (len >> 1)) {
691*7263Ssam 			register u_short *to, *from;
692*7263Ssam 
693*7263Ssam 			to = (u_short *)mcp;
694*7263Ssam 			from = (u_short *)cp;
695*7263Ssam 			do
696*7263Ssam 				*to++ = *from++;
697*7263Ssam 			while (--words > 0);
698*7263Ssam 			mcp = (u_char *)to;
699*7263Ssam 			cp = (u_char *)from;
7007032Swnj 		}
7017216Ssam 		if (len & 01)
7026520Sfeldman 			*mcp++ = *cp++;
7036520Sfeldman 		*mp = m;
7046520Sfeldman 		mp = &m->m_next;
705*7263Ssam 		if (off == 0) {
7066520Sfeldman 			totlen -= len;
707*7263Ssam 			continue;
708*7263Ssam 		}
709*7263Ssam 		off += len;
710*7263Ssam 		if (off == totlen) {
711*7263Ssam 			cp = ecbuf + ECRDOFF + sizeof (struct ec_header);
712*7263Ssam 			off = 0;
713*7263Ssam 			totlen = off0;
714*7263Ssam 		}
7156520Sfeldman 	}
7166520Sfeldman 	return (top);
7176520Sfeldman bad:
7186520Sfeldman 	m_freem(top);
7196520Sfeldman 	return (0);
7206520Sfeldman }
721