xref: /csrg-svn/sys/vax/if/if_ec.c (revision 7470)
1*7470Sfeldman /*	if_ec.c	4.22	82/07/21	*/
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
34*7470Sfeldman #define	ECMEM	0000000
356520Sfeldman 
366520Sfeldman int	ecprobe(), ecattach(), ecrint(), ecxint(), eccollide();
376520Sfeldman struct	uba_device *ecinfo[NEC];
386520Sfeldman u_short ecstd[] = { 0 };
396520Sfeldman struct	uba_driver ecdriver =
406520Sfeldman 	{ ecprobe, 0, ecattach, 0, ecstd, "ec", ecinfo };
416892Sfeldman u_char	ec_iltop[3] = { 0x02, 0x07, 0x01 };
426520Sfeldman #define	ECUNIT(x)	minor(x)
436520Sfeldman 
446520Sfeldman int	ecinit(),ecoutput(),ecreset();
456520Sfeldman struct mbuf *ecget();
466520Sfeldman 
476545Sfeldman extern struct ifnet loif;
486545Sfeldman 
496520Sfeldman /*
506520Sfeldman  * Ethernet software status per interface.
516520Sfeldman  *
526520Sfeldman  * Each interface is referenced by a network interface structure,
536520Sfeldman  * es_if, which the routing code uses to locate the interface.
546520Sfeldman  * This structure contains the output queue for the interface, its address, ...
556520Sfeldman  * We also have, for each interface, a UBA interface structure, which
566520Sfeldman  * contains information about the UNIBUS resources held by the interface:
576520Sfeldman  * map registers, buffered data paths, etc.  Information is cached in this
586520Sfeldman  * structure for use by the if_uba.c routines in running the interface
596520Sfeldman  * efficiently.
606520Sfeldman  */
616520Sfeldman struct	ec_softc {
626520Sfeldman 	struct	ifnet es_if;		/* network-visible interface */
636520Sfeldman 	struct	ifuba es_ifuba;		/* UNIBUS resources */
646520Sfeldman 	short	es_mask;		/* mask for current output delay */
656520Sfeldman 	short	es_oactive;		/* is output active? */
666520Sfeldman 	caddr_t	es_buf[16];		/* virtual addresses of buffers */
676520Sfeldman 	u_char	es_enaddr[6];		/* board's ethernet address */
686520Sfeldman } ec_softc[NEC];
696520Sfeldman 
706520Sfeldman /*
716520Sfeldman  * Do output DMA to determine interface presence and
726520Sfeldman  * interrupt vector.  DMA is too short to disturb other hosts.
736520Sfeldman  */
746520Sfeldman ecprobe(reg)
756520Sfeldman 	caddr_t reg;
766520Sfeldman {
776520Sfeldman 	register int br, cvec;		/* r11, r10 value-result */
786520Sfeldman 	register struct ecdevice *addr = (struct ecdevice *)reg;
79*7470Sfeldman 	register caddr_t ecbuf = (caddr_t) &umem[numuba][ECMEM];
806520Sfeldman 
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 	/*
90*7470Sfeldman 	 * Disable map registers for ec unibus space,
91*7470Sfeldman 	 * but don't allocate yet.
92*7470Sfeldman 	 */
93*7470Sfeldman 	ubamem(numuba, ECMEM, 32*2, 0);
94*7470Sfeldman 	/*
956520Sfeldman 	 * Check for existence of buffers on Unibus.
966520Sfeldman 	 */
976520Sfeldman 	if (badaddr((caddr_t) ecbuf, 2)) {
98*7470Sfeldman 	bad1:
99*7470Sfeldman 		printf("ec: buffer mem not found\n");
100*7470Sfeldman 	bad2:
101*7470Sfeldman 		ubamem(numuba, 0, 0, 0);	/* reenable map (780 only) */
102*7470Sfeldman 		addr->ec_rcr = EC_MDISAB;	/* disable memory */
1036520Sfeldman 		return (0);
1046520Sfeldman 	}
105*7470Sfeldman #if VAX780
106*7470Sfeldman 	if (cpu == VAX_780 && uba_hd[numuba].uh_uba->uba_sr) {
107*7470Sfeldman 		uba_hd[numuba].uh_uba->uba_sr = uba_hd[numuba].uh_uba->uba_sr;
108*7470Sfeldman 		goto bad1;
109*7470Sfeldman 	}
110*7470Sfeldman #endif
1116520Sfeldman 
1126520Sfeldman 	/*
1136520Sfeldman 	 * Tell the system that the board has memory here, so it won't
1146520Sfeldman 	 * attempt to allocate the addresses later.
1156520Sfeldman 	 */
116*7470Sfeldman 	if (ubamem(numuba, ECMEM, 32*2, 1) == 0) {
117*7470Sfeldman 		printf("ecprobe: cannot reserve uba addresses\n");
118*7470Sfeldman 		goto bad2;
119*7470Sfeldman 	}
1206520Sfeldman 
1216520Sfeldman 	/*
1226520Sfeldman 	 * Make a one byte packet in what should be buffer #0.
1236520Sfeldman 	 * Submit it for sending.  This whould cause an xmit interrupt.
1246520Sfeldman 	 * The xmit interrupt vector is 8 bytes after the receive vector,
1256520Sfeldman 	 * so adjust for this before returning.
1266520Sfeldman 	 */
1276520Sfeldman 	*(u_short *)ecbuf = (u_short) 03777;
1286520Sfeldman 	ecbuf[03777] = '\0';
1296520Sfeldman 	addr->ec_xcr = EC_XINTEN|EC_XWBN;
1306520Sfeldman 	DELAY(100000);
1316520Sfeldman 	addr->ec_xcr = EC_XCLR;
1327216Ssam 	if (cvec > 0 && cvec != 0x200) {
133*7470Sfeldman 		if (cvec & 04) {	/* collision interrupt */
134*7470Sfeldman 			cvec -= 04;
135*7470Sfeldman 			br += 1;		/* rcv is collision + 1 */
136*7470Sfeldman 		} else {		/* xmit interrupt */
137*7470Sfeldman 			cvec -= 010;
138*7470Sfeldman 			br += 2;		/* rcv is xmit + 2 */
139*7470Sfeldman 		}
1407216Ssam 	}
1416520Sfeldman 	return (1);
1426520Sfeldman }
1436520Sfeldman 
1446520Sfeldman /*
1456520Sfeldman  * Interface exists: make available by filling in network interface
1466520Sfeldman  * record.  System will initialize the interface when it is ready
1476520Sfeldman  * to accept packets.
1486520Sfeldman  */
1496520Sfeldman ecattach(ui)
1506520Sfeldman 	struct uba_device *ui;
1516520Sfeldman {
1527216Ssam 	struct ec_softc *es = &ec_softc[ui->ui_unit];
1537216Ssam 	register struct ifnet *ifp = &es->es_if;
1546520Sfeldman 	register struct ecdevice *addr = (struct ecdevice *)ui->ui_addr;
1557216Ssam 	struct sockaddr_in *sin;
1567216Ssam 	int i, j;
1577216Ssam 	u_char *cp;
1586520Sfeldman 
1597216Ssam 	ifp->if_unit = ui->ui_unit;
1607216Ssam 	ifp->if_name = "ec";
1617216Ssam 	ifp->if_mtu = ECMTU;
1627216Ssam 	ifp->if_net = ui->ui_flags;
1636520Sfeldman 
1646520Sfeldman 	/*
1657216Ssam 	 * Read the ethernet address off the board, one nibble at a time.
1666520Sfeldman 	 */
1676520Sfeldman 	addr->ec_xcr = EC_UECLR;
1686520Sfeldman 	addr->ec_rcr = EC_AROM;
1696520Sfeldman 	cp = es->es_enaddr;
1707216Ssam #define	NEXTBIT	addr->ec_rcr = EC_AROM|EC_ASTEP; addr->ec_rcr = EC_AROM
1716520Sfeldman 	for (i=0; i<6; i++) {
1726520Sfeldman 		*cp = 0;
1736520Sfeldman 		for (j=0; j<=4; j+=4) {
1746520Sfeldman 			*cp |= ((addr->ec_rcr >> 8) & 0xf) << j;
1757216Ssam 			NEXTBIT; NEXTBIT; NEXTBIT; NEXTBIT;
1766520Sfeldman 		}
1776520Sfeldman 		cp++;
1786520Sfeldman 	}
1797216Ssam #ifdef notdef
1806520Sfeldman 	printf("ec%d: addr=%x:%x:%x:%x:%x:%x\n", ui->ui_unit,
1816520Sfeldman 		es->es_enaddr[0]&0xff, es->es_enaddr[1]&0xff,
1826520Sfeldman 		es->es_enaddr[2]&0xff, es->es_enaddr[3]&0xff,
1836520Sfeldman 		es->es_enaddr[4]&0xff, es->es_enaddr[5]&0xff);
1847216Ssam #endif
1857216Ssam 	ifp->if_host[0] = ((es->es_enaddr[3]&0xff)<<16) |
1866520Sfeldman 	    ((es->es_enaddr[4]&0xff)<<8) | (es->es_enaddr[5]&0xff);
1876520Sfeldman 	sin = (struct sockaddr_in *)&es->es_if.if_addr;
1886520Sfeldman 	sin->sin_family = AF_INET;
1897216Ssam 	sin->sin_addr = if_makeaddr(ifp->if_net, ifp->if_host[0]);
1906520Sfeldman 
1917216Ssam 	sin = (struct sockaddr_in *)&ifp->if_broadaddr;
1926520Sfeldman 	sin->sin_family = AF_INET;
1937216Ssam 	sin->sin_addr = if_makeaddr(ifp->if_net, INADDR_ANY);
1947216Ssam 	ifp->if_flags = IFF_BROADCAST;
1956520Sfeldman 
1967216Ssam 	ifp->if_init = ecinit;
1977216Ssam 	ifp->if_output = ecoutput;
1987216Ssam 	ifp->if_ubareset = ecreset;
1996520Sfeldman 	for (i=0; i<16; i++)
200*7470Sfeldman 		es->es_buf[i] = &umem[ui->ui_ubanum][ECMEM+2048*i];
2017216Ssam 	if_attach(ifp);
2026520Sfeldman }
2036520Sfeldman 
2046520Sfeldman /*
2056520Sfeldman  * Reset of interface after UNIBUS reset.
2066520Sfeldman  * If interface is on specified uba, reset its state.
2076520Sfeldman  */
2086520Sfeldman ecreset(unit, uban)
2096520Sfeldman 	int unit, uban;
2106520Sfeldman {
2116520Sfeldman 	register struct uba_device *ui;
2126520Sfeldman 
2136520Sfeldman 	if (unit >= NEC || (ui = ecinfo[unit]) == 0 || ui->ui_alive == 0 ||
2146520Sfeldman 	    ui->ui_ubanum != uban)
2156520Sfeldman 		return;
2166520Sfeldman 	printf(" ec%d", unit);
217*7470Sfeldman 	ubamem(uban, ECMEM, 32*2, 0);	/* map register disable (no alloc) */
2186520Sfeldman 	ecinit(unit);
2196520Sfeldman }
2206520Sfeldman 
2216520Sfeldman /*
2226520Sfeldman  * Initialization of interface; clear recorded pending
2236520Sfeldman  * operations, and reinitialize UNIBUS usage.
2246520Sfeldman  */
2256520Sfeldman ecinit(unit)
2266520Sfeldman 	int unit;
2276520Sfeldman {
2287216Ssam 	struct ec_softc *es = &ec_softc[unit];
2297216Ssam 	struct ecdevice *addr;
2307216Ssam 	int i, s;
2316520Sfeldman 
2326520Sfeldman 	/*
2337217Sfeldman 	 * Hang receive buffers and start any pending writes.
2346637Sfeldman 	 * Writing into the rcr also makes sure the memory
2356637Sfeldman 	 * is turned on.
2366520Sfeldman 	 */
2377216Ssam 	addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
2386520Sfeldman 	s = splimp();
2396520Sfeldman 	for (i=ECRHBF; i>=ECRLBF; i--)
2406520Sfeldman 		addr->ec_rcr = EC_READ|i;
2417217Sfeldman 	es->es_oactive = 0;
2427217Sfeldman 	es->es_mask = ~0;
2436520Sfeldman 	es->es_if.if_flags |= IFF_UP;
2447217Sfeldman 	if (es->es_if.if_snd.ifq_head)
2457217Sfeldman 		ecstart(unit);
2466520Sfeldman 	splx(s);
2477150Swnj 	if_rtinit(&es->es_if, RTF_UP);
2486520Sfeldman }
2496520Sfeldman 
2506520Sfeldman /*
2516520Sfeldman  * Start or restart output on interface.
2526520Sfeldman  * If interface is already active, then this is a retransmit
2536545Sfeldman  * after a collision, and just restuff registers.
2546520Sfeldman  * If interface is not already active, get another datagram
2556520Sfeldman  * to send off of the interface queue, and map it to the interface
2566520Sfeldman  * before starting the output.
2576520Sfeldman  */
2586520Sfeldman ecstart(dev)
2596520Sfeldman 	dev_t dev;
2606520Sfeldman {
2617216Ssam         int unit = ECUNIT(dev), dest;
2627216Ssam 	struct ec_softc *es = &ec_softc[unit];
2637216Ssam 	struct ecdevice *addr;
2646520Sfeldman 	struct mbuf *m;
2656520Sfeldman 	caddr_t ecbuf;
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 	 */
373*7470Sfeldman 	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;
3996520Sfeldman 	register struct ec_header *ec;
4006520Sfeldman     	struct mbuf *m;
4017216Ssam 	int len, off, resid, ecoff, buf;
4026520Sfeldman 	register struct ifqueue *inq;
4036520Sfeldman 	caddr_t ecbuf;
4046520Sfeldman 
4056520Sfeldman 	es->es_if.if_ipackets++;
4066520Sfeldman 	buf = addr->ec_rcr & EC_RBN;
4076520Sfeldman 	if (buf < ECRLBF || buf > ECRHBF)
4086520Sfeldman 		panic("ecrint");
4096520Sfeldman 	ecbuf = es->es_buf[buf];
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 	 */
4276520Sfeldman 	len = ecoff - ECRDOFF - sizeof (struct ec_header);
4286520Sfeldman 	ec = (struct ec_header *)(ecbuf + ECRDOFF);
4296520Sfeldman #define	ecdataaddr(ec, off, type)	((type)(((caddr_t)((ec)+1)+(off))))
4306520Sfeldman 	if (ec->ec_type >= ECPUP_TRAIL &&
4316520Sfeldman 	    ec->ec_type < ECPUP_TRAIL+ECPUP_NTRAILER) {
4326520Sfeldman 		off = (ec->ec_type - ECPUP_TRAIL) * 512;
4336520Sfeldman 		if (off >= ECMTU)
4346520Sfeldman 			goto setup;		/* sanity */
4356520Sfeldman 		ec->ec_type = *ecdataaddr(ec, off, u_short *);
4366520Sfeldman 		resid = *(ecdataaddr(ec, off+2, u_short *));
4376520Sfeldman 		if (off + resid > len)
4386520Sfeldman 			goto setup;		/* sanity */
4396520Sfeldman 		len = off + resid;
4406520Sfeldman 	} else
4416520Sfeldman 		off = 0;
4426520Sfeldman 	if (len == 0)
4436520Sfeldman 		goto setup;
4446520Sfeldman 
4456520Sfeldman 	/*
4466520Sfeldman 	 * Pull packet off interface.  Off is nonzero if packet
4476520Sfeldman 	 * has trailing header; ecget will then force this header
4486520Sfeldman 	 * information to be at the front, but we still have to drop
4496520Sfeldman 	 * the type and length which are at the front of any trailer data.
4506520Sfeldman 	 */
4516520Sfeldman 	m = ecget(ecbuf, len, off);
4526520Sfeldman 	if (m == 0)
4536520Sfeldman 		goto setup;
4546520Sfeldman 	if (off) {
4556520Sfeldman 		m->m_off += 2 * sizeof (u_short);
4566520Sfeldman 		m->m_len -= 2 * sizeof (u_short);
4576520Sfeldman 	}
4586520Sfeldman 	switch (ec->ec_type) {
4596520Sfeldman 
4606520Sfeldman #ifdef INET
4616520Sfeldman 	case ECPUP_IPTYPE:
4626520Sfeldman 		schednetisr(NETISR_IP);
4636520Sfeldman 		inq = &ipintrq;
4646520Sfeldman 		break;
4656520Sfeldman #endif
4666520Sfeldman 	default:
4676520Sfeldman 		m_freem(m);
4686520Sfeldman 		goto setup;
4696520Sfeldman 	}
4706520Sfeldman 
4716520Sfeldman 	if (IF_QFULL(inq)) {
4726520Sfeldman 		IF_DROP(inq);
4736520Sfeldman 		m_freem(m);
4747216Ssam 		goto setup;
4757216Ssam 	}
4767216Ssam 	IF_ENQUEUE(inq, m);
4776520Sfeldman 
4786520Sfeldman setup:
4796520Sfeldman 	/*
4806520Sfeldman 	 * Reset for next packet.
4816520Sfeldman 	 */
4826520Sfeldman 	addr->ec_rcr = EC_READ|EC_RCLR|buf;
4836520Sfeldman }
4846520Sfeldman 
4856520Sfeldman /*
4866520Sfeldman  * Ethernet output routine.
4876520Sfeldman  * Encapsulate a packet of type family for the local net.
4886520Sfeldman  * Use trailer local net encapsulation if enough data in first
4896520Sfeldman  * packet leaves a multiple of 512 bytes of data in remainder.
4906545Sfeldman  * If destination is this address or broadcast, send packet to
4916545Sfeldman  * loop device to kludge around the fact that 3com interfaces can't
4926545Sfeldman  * talk to themselves.
4936520Sfeldman  */
4946520Sfeldman ecoutput(ifp, m0, dst)
4956520Sfeldman 	struct ifnet *ifp;
4966520Sfeldman 	struct mbuf *m0;
4976520Sfeldman 	struct sockaddr *dst;
4986520Sfeldman {
4996520Sfeldman 	int type, dest, s, error;
5006520Sfeldman 	register struct ec_softc *es = &ec_softc[ifp->if_unit];
5016520Sfeldman 	register struct mbuf *m = m0;
5026520Sfeldman 	register struct ec_header *ec;
5037216Ssam 	register int off, i;
5046545Sfeldman 	struct mbuf *mcopy = (struct mbuf *) 0;		/* Null */
5056520Sfeldman 
5066520Sfeldman 	switch (dst->sa_family) {
5076520Sfeldman 
5086520Sfeldman #ifdef INET
5096520Sfeldman 	case AF_INET:
5106520Sfeldman 		dest = ((struct sockaddr_in *)dst)->sin_addr.s_addr;
5116545Sfeldman 		if ((dest &~ 0xff) == 0)
5126545Sfeldman 			mcopy = m_copy(m, 0, M_COPYALL);
5136545Sfeldman 		else if (dest == ((struct sockaddr_in *)&es->es_if.if_addr)->
5146545Sfeldman 		    sin_addr.s_addr) {
5156545Sfeldman 			mcopy = m;
5166545Sfeldman 			goto gotlocal;
5176545Sfeldman 		}
5186520Sfeldman 		off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len;
5196520Sfeldman 		if (off > 0 && (off & 0x1ff) == 0 &&
5206520Sfeldman 		    m->m_off >= MMINOFF + 2 * sizeof (u_short)) {
5216520Sfeldman 			type = ECPUP_TRAIL + (off>>9);
5226520Sfeldman 			m->m_off -= 2 * sizeof (u_short);
5236520Sfeldman 			m->m_len += 2 * sizeof (u_short);
5246520Sfeldman 			*mtod(m, u_short *) = ECPUP_IPTYPE;
5256520Sfeldman 			*(mtod(m, u_short *) + 1) = m->m_len;
5266520Sfeldman 			goto gottrailertype;
5276520Sfeldman 		}
5286520Sfeldman 		type = ECPUP_IPTYPE;
5296520Sfeldman 		off = 0;
5306520Sfeldman 		goto gottype;
5316520Sfeldman #endif
5326520Sfeldman 
5336520Sfeldman 	default:
5346520Sfeldman 		printf("ec%d: can't handle af%d\n", ifp->if_unit,
5356520Sfeldman 			dst->sa_family);
5366520Sfeldman 		error = EAFNOSUPPORT;
5376520Sfeldman 		goto bad;
5386520Sfeldman 	}
5396520Sfeldman 
5406520Sfeldman gottrailertype:
5416520Sfeldman 	/*
5426520Sfeldman 	 * Packet to be sent as trailer: move first packet
5436520Sfeldman 	 * (control information) to end of chain.
5446520Sfeldman 	 */
5456520Sfeldman 	while (m->m_next)
5466520Sfeldman 		m = m->m_next;
5476520Sfeldman 	m->m_next = m0;
5486520Sfeldman 	m = m0->m_next;
5496520Sfeldman 	m0->m_next = 0;
5506520Sfeldman 	m0 = m;
5516520Sfeldman 
5526520Sfeldman gottype:
5536520Sfeldman 	/*
5546520Sfeldman 	 * Add local net header.  If no space in first mbuf,
5556520Sfeldman 	 * allocate another.
5566520Sfeldman 	 */
5576520Sfeldman 	if (m->m_off > MMAXOFF ||
5586520Sfeldman 	    MMINOFF + sizeof (struct ec_header) > m->m_off) {
5596520Sfeldman 		m = m_get(M_DONTWAIT);
5606520Sfeldman 		if (m == 0) {
5616520Sfeldman 			error = ENOBUFS;
5626520Sfeldman 			goto bad;
5636520Sfeldman 		}
5646520Sfeldman 		m->m_next = m0;
5656520Sfeldman 		m->m_off = MMINOFF;
5666520Sfeldman 		m->m_len = sizeof (struct ec_header);
5676520Sfeldman 	} else {
5686520Sfeldman 		m->m_off -= sizeof (struct ec_header);
5696520Sfeldman 		m->m_len += sizeof (struct ec_header);
5706520Sfeldman 	}
5716520Sfeldman 	ec = mtod(m, struct ec_header *);
5726520Sfeldman 	for (i=0; i<6; i++)
5736520Sfeldman 		ec->ec_shost[i] = es->es_enaddr[i];
5746525Sfeldman 	if ((dest &~ 0xff) == 0)
5757216Ssam 		/* broadcast address */
5766520Sfeldman 		for (i=0; i<6; i++)
5776520Sfeldman 			ec->ec_dhost[i] = 0xff;
5786520Sfeldman 	else {
5796892Sfeldman 		if (dest & 0x8000) {
5806892Sfeldman 			ec->ec_dhost[0] = ec_iltop[0];
5816892Sfeldman 			ec->ec_dhost[1] = ec_iltop[1];
5826892Sfeldman 			ec->ec_dhost[2] = ec_iltop[2];
5836892Sfeldman 		} else {
5846892Sfeldman 			ec->ec_dhost[0] = es->es_enaddr[0];
5856892Sfeldman 			ec->ec_dhost[1] = es->es_enaddr[1];
5866892Sfeldman 			ec->ec_dhost[2] = es->es_enaddr[2];
5876892Sfeldman 		}
5886892Sfeldman 		ec->ec_dhost[3] = (dest>>8) & 0x7f;
5896520Sfeldman 		ec->ec_dhost[4] = (dest>>16) & 0xff;
5906520Sfeldman 		ec->ec_dhost[5] = (dest>>24) & 0xff;
5916520Sfeldman 	}
5926520Sfeldman 	ec->ec_type = type;
5936520Sfeldman 
5946520Sfeldman 	/*
5956520Sfeldman 	 * Queue message on interface, and start output if interface
5966520Sfeldman 	 * not yet active.
5976520Sfeldman 	 */
5986520Sfeldman 	s = splimp();
5996520Sfeldman 	if (IF_QFULL(&ifp->if_snd)) {
6006520Sfeldman 		IF_DROP(&ifp->if_snd);
6016520Sfeldman 		error = ENOBUFS;
6026520Sfeldman 		goto qfull;
6036520Sfeldman 	}
6046520Sfeldman 	IF_ENQUEUE(&ifp->if_snd, m);
6056520Sfeldman 	if (es->es_oactive == 0)
6066520Sfeldman 		ecstart(ifp->if_unit);
6076520Sfeldman 	splx(s);
6087216Ssam 
6096545Sfeldman gotlocal:
6107216Ssam 	return(mcopy ? looutput(&loif, mcopy, dst) : 0);
6117216Ssam 
6126520Sfeldman qfull:
6136520Sfeldman 	m0 = m;
6146520Sfeldman 	splx(s);
6156520Sfeldman bad:
6166520Sfeldman 	m_freem(m0);
6176520Sfeldman 	return(error);
6186520Sfeldman }
6196520Sfeldman 
6206520Sfeldman /*
6217216Ssam  * Routine to copy from mbuf chain to transmitter
6227216Ssam  * buffer in UNIBUS memory.
6236520Sfeldman  */
6246520Sfeldman ecput(ecbuf, m)
6257216Ssam 	u_char *ecbuf;
6266520Sfeldman 	struct mbuf *m;
6276520Sfeldman {
6286520Sfeldman 	register struct mbuf *mp;
6297216Ssam 	register int off;
6307263Ssam 	u_char *bp;
6316520Sfeldman 
6327216Ssam 	for (off = 2048, mp = m; mp; mp = mp->m_next)
6337216Ssam 		off -= mp->m_len;
6347216Ssam 	*(u_short *)ecbuf = off;
6357216Ssam 	bp = (u_char *)(ecbuf + off);
6367263Ssam 	for (mp = m; mp; mp = mp->m_next) {
6377263Ssam 		register unsigned len = mp->m_len;
6387263Ssam 		u_char *mcp;
6397216Ssam 
6407216Ssam 		if (len == 0)
6417216Ssam 			continue;
6427216Ssam 		mcp = mtod(mp, u_char *);
6437216Ssam 		if ((unsigned)bp & 01) {
6447032Swnj 			*bp++ = *mcp++;
6457216Ssam 			len--;
6467032Swnj 		}
6477263Ssam 		if (off = (len >> 1)) {
6487263Ssam 			register u_short *to, *from;
6497263Ssam 
6507263Ssam 			to = (u_short *)bp;
6517263Ssam 			from = (u_short *)mcp;
6527263Ssam 			do
6537263Ssam 				*to++ = *from++;
6547263Ssam 			while (--off > 0);
6557263Ssam 			bp = (u_char *)to,
6567263Ssam 			mcp = (u_char *)from;
6577032Swnj 		}
6587263Ssam 		if (len & 01)
6596520Sfeldman 			*bp++ = *mcp++;
6606520Sfeldman 	}
6617216Ssam #ifdef notdef
6627216Ssam 	if (bp - ecbuf != 2048)
6637216Ssam 		printf("ec: bad ecput, diff=%d\n", bp-ecbuf);
6647216Ssam #endif
6657263Ssam 	m_freem(m);
6666520Sfeldman }
6676520Sfeldman 
6686520Sfeldman /*
6696520Sfeldman  * Routine to copy from UNIBUS memory into mbufs.
6706520Sfeldman  * Similar in spirit to if_rubaget.
6717032Swnj  *
6727032Swnj  * Warning: This makes the fairly safe assumption that
6737032Swnj  * mbufs have even lengths.
6746520Sfeldman  */
6756520Sfeldman struct mbuf *
6766520Sfeldman ecget(ecbuf, totlen, off0)
6777263Ssam 	u_char *ecbuf;
6786520Sfeldman 	int totlen, off0;
6796520Sfeldman {
6807263Ssam 	register struct mbuf *m;
6817263Ssam 	struct mbuf *top = 0, **mp = &top;
6827263Ssam 	register int off = off0, len;
6837263Ssam 	u_char *cp;
6846520Sfeldman 
6857216Ssam 	cp = ecbuf + ECRDOFF + sizeof (struct ec_header);
6866520Sfeldman 	while (totlen > 0) {
6877263Ssam 		register int words;
6887263Ssam 		u_char *mcp;
6897263Ssam 
6906520Sfeldman 		MGET(m, 0);
6916520Sfeldman 		if (m == 0)
6926520Sfeldman 			goto bad;
6936520Sfeldman 		if (off) {
6946520Sfeldman 			len = totlen - off;
6956520Sfeldman 			cp = ecbuf + ECRDOFF + sizeof (struct ec_header) + off;
6966520Sfeldman 		} else
6976520Sfeldman 			len = totlen;
6986520Sfeldman 		if (len >= CLBYTES) {
6996520Sfeldman 			struct mbuf *p;
7006520Sfeldman 
7016520Sfeldman 			MCLGET(p, 1);
7026520Sfeldman 			if (p != 0) {
7036520Sfeldman 				m->m_len = len = CLBYTES;
7046520Sfeldman 				m->m_off = (int)p - (int)m;
7056520Sfeldman 			} else {
7066520Sfeldman 				m->m_len = len = MIN(MLEN, len);
7076520Sfeldman 				m->m_off = MMINOFF;
7086520Sfeldman 			}
7096520Sfeldman 		} else {
7106520Sfeldman 			m->m_len = len = MIN(MLEN, len);
7116520Sfeldman 			m->m_off = MMINOFF;
7126520Sfeldman 		}
7137263Ssam 		mcp = mtod(m, u_char *);
7147263Ssam 		if (words = (len >> 1)) {
7157263Ssam 			register u_short *to, *from;
7167263Ssam 
7177263Ssam 			to = (u_short *)mcp;
7187263Ssam 			from = (u_short *)cp;
7197263Ssam 			do
7207263Ssam 				*to++ = *from++;
7217263Ssam 			while (--words > 0);
7227263Ssam 			mcp = (u_char *)to;
7237263Ssam 			cp = (u_char *)from;
7247032Swnj 		}
7257216Ssam 		if (len & 01)
7266520Sfeldman 			*mcp++ = *cp++;
7276520Sfeldman 		*mp = m;
7286520Sfeldman 		mp = &m->m_next;
7297263Ssam 		if (off == 0) {
7306520Sfeldman 			totlen -= len;
7317263Ssam 			continue;
7327263Ssam 		}
7337263Ssam 		off += len;
7347263Ssam 		if (off == totlen) {
7357263Ssam 			cp = ecbuf + ECRDOFF + sizeof (struct ec_header);
7367263Ssam 			off = 0;
7377263Ssam 			totlen = off0;
7387263Ssam 		}
7396520Sfeldman 	}
7406520Sfeldman 	return (top);
7416520Sfeldman bad:
7426520Sfeldman 	m_freem(top);
7436520Sfeldman 	return (0);
7446520Sfeldman }
745