xref: /csrg-svn/sys/vax/if/if_ec.c (revision 7216)
1*7216Ssam /*	if_ec.c	4.17	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;
115*7216Ssam 	if (cvec > 0 && cvec != 0x200) {
1166520Sfeldman 		cvec -= 010;
117*7216Ssam 		br += 2;		/* rcv is xmit + 2 */
118*7216Ssam 	}
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 {
130*7216Ssam 	struct ec_softc *es = &ec_softc[ui->ui_unit];
131*7216Ssam 	register struct ifnet *ifp = &es->es_if;
1326520Sfeldman 	register struct ecdevice *addr = (struct ecdevice *)ui->ui_addr;
133*7216Ssam 	struct sockaddr_in *sin;
134*7216Ssam 	int i, j;
135*7216Ssam 	u_char *cp;
1366520Sfeldman COUNT(ECATTACH);
1376520Sfeldman 
138*7216Ssam 	ifp->if_unit = ui->ui_unit;
139*7216Ssam 	ifp->if_name = "ec";
140*7216Ssam 	ifp->if_mtu = ECMTU;
141*7216Ssam 	ifp->if_net = ui->ui_flags;
1426520Sfeldman 
1436520Sfeldman 	/*
144*7216Ssam 	 * 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;
149*7216Ssam #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;
154*7216Ssam 			NEXTBIT; NEXTBIT; NEXTBIT; NEXTBIT;
1556520Sfeldman 		}
1566520Sfeldman 		cp++;
1576520Sfeldman 	}
158*7216Ssam #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);
163*7216Ssam #endif
164*7216Ssam 	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;
168*7216Ssam 	sin->sin_addr = if_makeaddr(ifp->if_net, ifp->if_host[0]);
1696520Sfeldman 
170*7216Ssam 	sin = (struct sockaddr_in *)&ifp->if_broadaddr;
1716520Sfeldman 	sin->sin_family = AF_INET;
172*7216Ssam 	sin->sin_addr = if_makeaddr(ifp->if_net, INADDR_ANY);
173*7216Ssam 	ifp->if_flags = IFF_BROADCAST;
1746520Sfeldman 
175*7216Ssam 	ifp->if_init = ecinit;
176*7216Ssam 	ifp->if_output = ecoutput;
177*7216Ssam 	ifp->if_ubareset = ecreset;
1786520Sfeldman 	for (i=0; i<16; i++)
1796520Sfeldman 		es->es_buf[i] = &umem[ui->ui_ubanum][0600000+2048*i];
180*7216Ssam 	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 {
207*7216Ssam 	struct ec_softc *es = &ec_softc[unit];
208*7216Ssam 	struct ecdevice *addr;
209*7216Ssam 	int i, s;
2106520Sfeldman 
2116520Sfeldman 	/*
2126520Sfeldman 	 * Hang receive buffers and start any pending
2136520Sfeldman 	 * writes by faking a transmit complete.
2146637Sfeldman 	 * Writing into the rcr also makes sure the memory
2156637Sfeldman 	 * is turned on.
2166520Sfeldman 	 */
217*7216Ssam 	addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
2186520Sfeldman 	s = splimp();
2196520Sfeldman 	for (i=ECRHBF; i>=ECRLBF; i--)
2206520Sfeldman 		addr->ec_rcr = EC_READ|i;
2216520Sfeldman 	es->es_oactive = 1;
2226520Sfeldman 	es->es_if.if_flags |= IFF_UP;
2236520Sfeldman 	ecxint(unit);
2246520Sfeldman 	splx(s);
2257150Swnj 	if_rtinit(&es->es_if, RTF_UP);
2266520Sfeldman }
2276520Sfeldman 
2286520Sfeldman /*
2296520Sfeldman  * Start or restart output on interface.
2306520Sfeldman  * If interface is already active, then this is a retransmit
2316545Sfeldman  * after a collision, and just restuff registers.
2326520Sfeldman  * If interface is not already active, get another datagram
2336520Sfeldman  * to send off of the interface queue, and map it to the interface
2346520Sfeldman  * before starting the output.
2356520Sfeldman  */
2366520Sfeldman ecstart(dev)
2376520Sfeldman 	dev_t dev;
2386520Sfeldman {
239*7216Ssam         int unit = ECUNIT(dev), dest;
240*7216Ssam 	struct ec_softc *es = &ec_softc[unit];
241*7216Ssam 	struct ecdevice *addr;
2426520Sfeldman 	struct mbuf *m;
2436520Sfeldman 	caddr_t ecbuf;
2446520Sfeldman COUNT(ECSTART);
2456520Sfeldman 
2466520Sfeldman 	if (es->es_oactive)
2476520Sfeldman 		goto restart;
2486520Sfeldman 
2496520Sfeldman 	IF_DEQUEUE(&es->es_if.if_snd, m);
2506520Sfeldman 	if (m == 0) {
2516520Sfeldman 		es->es_oactive = 0;
2526520Sfeldman 		return;
2536520Sfeldman 	}
2546520Sfeldman 	ecput(es->es_buf[ECTBF], m);
2556520Sfeldman 
2566520Sfeldman restart:
257*7216Ssam 	addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
2586520Sfeldman 	addr->ec_xcr = EC_WRITE|ECTBF;
2596520Sfeldman 	es->es_oactive = 1;
2606520Sfeldman }
2616520Sfeldman 
2626520Sfeldman /*
2636520Sfeldman  * Ethernet interface transmitter interrupt.
2646520Sfeldman  * Start another output if more data to send.
2656520Sfeldman  */
2666520Sfeldman ecxint(unit)
2676520Sfeldman 	int unit;
2686520Sfeldman {
2696520Sfeldman 	register struct ec_softc *es = &ec_softc[unit];
270*7216Ssam 	register struct ecdevice *addr =
271*7216Ssam 		(struct ecdevice *)ecinfo[unit]->ui_addr;
2726520Sfeldman COUNT(ECXINT);
2736520Sfeldman 
2746520Sfeldman 	if (es->es_oactive == 0)
2756520Sfeldman 		return;
276*7216Ssam 	if ((addr->ec_xcr&EC_XDONE) == 0 || (addr->ec_xcr&EC_XBN) != ECTBF) {
277*7216Ssam 		printf("ec%d: stray xmit interrupt, xcr=%b\n", unit,
278*7216Ssam 			addr->ec_xcr, EC_XBITS);
279*7216Ssam 		es->es_oactive = 0;
280*7216Ssam 		addr->ec_xcr = EC_XCLR;
281*7216Ssam 		return;
282*7216Ssam 	}
2836520Sfeldman 	es->es_if.if_opackets++;
2846520Sfeldman 	es->es_oactive = 0;
2856520Sfeldman 	es->es_mask = ~0;
2866520Sfeldman 	addr->ec_xcr = EC_XCLR;
2876520Sfeldman 	/*
2886520Sfeldman 	 * There shouldn't ever be any mbuf's to free, but just in case...
2896520Sfeldman 	 */
2906520Sfeldman 	if (es->es_ifuba.ifu_xtofree) {
2916520Sfeldman 		m_freem(es->es_ifuba.ifu_xtofree);
2926520Sfeldman 		es->es_ifuba.ifu_xtofree = 0;
2936520Sfeldman 	}
294*7216Ssam 	if (es->es_if.if_snd.ifq_head)
295*7216Ssam 		ecstart(unit);
2966520Sfeldman }
2976520Sfeldman 
2986520Sfeldman /*
2996520Sfeldman  * Collision on ethernet interface.  Do exponential
3006520Sfeldman  * backoff, and retransmit.  If have backed off all
3016520Sfeldman  * the way print warning diagnostic, and drop packet.
3026520Sfeldman  */
3036520Sfeldman eccollide(unit)
3046520Sfeldman 	int unit;
3056520Sfeldman {
3066520Sfeldman 	struct ec_softc *es = &ec_softc[unit];
3076520Sfeldman COUNT(ECCOLLIDE);
3086520Sfeldman 
3096545Sfeldman 	printf("ec%d: collision\n", unit);
3106520Sfeldman 	es->es_if.if_collisions++;
311*7216Ssam 	if (es->es_oactive)
312*7216Ssam 		ecdocoll(unit);
3136520Sfeldman }
3146520Sfeldman 
3156520Sfeldman ecdocoll(unit)
3166520Sfeldman 	int unit;
3176520Sfeldman {
3186520Sfeldman 	register struct ec_softc *es = &ec_softc[unit];
3196545Sfeldman 	register struct ecdevice *addr =
3206545Sfeldman 	    (struct ecdevice *)ecinfo[unit]->ui_addr;
3216545Sfeldman 	register i;
3226545Sfeldman 	int delay;
3236520Sfeldman 
3246520Sfeldman 	/*
3256520Sfeldman 	 * Es_mask is a 16 bit number with n low zero bits, with
3266520Sfeldman 	 * n the number of backoffs.  When es_mask is 0 we have
3276520Sfeldman 	 * backed off 16 times, and give up.
3286520Sfeldman 	 */
3296520Sfeldman 	if (es->es_mask == 0) {
3306545Sfeldman 		es->es_if.if_oerrors++;
3316520Sfeldman 		printf("ec%d: send error\n", unit);
3326520Sfeldman 		/*
3336545Sfeldman 		 * Reset interface, then requeue rcv buffers.
3346545Sfeldman 		 * Some incoming packets may be lost, but that
3356545Sfeldman 		 * can't be helped.
3366520Sfeldman 		 */
3376545Sfeldman 		addr->ec_xcr = EC_UECLR;
3386545Sfeldman 		for (i=ECRHBF; i>=ECRLBF; i--)
3396545Sfeldman 			addr->ec_rcr = EC_READ|i;
3406545Sfeldman 		/*
3416545Sfeldman 		 * Reset and transmit next packet (if any).
3426545Sfeldman 		 */
3436545Sfeldman 		es->es_oactive = 0;
3446545Sfeldman 		es->es_mask = ~0;
3456545Sfeldman 		if (es->es_if.if_snd.ifq_head)
3466545Sfeldman 			ecstart(unit);
3476520Sfeldman 		return;
3486520Sfeldman 	}
3496520Sfeldman 	/*
3506545Sfeldman 	 * Do exponential backoff.  Compute delay based on low bits
3516545Sfeldman 	 * of the interval timer.  Then delay for that number of
3526545Sfeldman 	 * slot times.  A slot time is 51.2 microseconds (rounded to 51).
3536545Sfeldman 	 * This does not take into account the time already used to
3546545Sfeldman 	 * process the interrupt.
3556520Sfeldman 	 */
3566520Sfeldman 	es->es_mask <<= 1;
3576545Sfeldman 	delay = mfpr(ICR) &~ es->es_mask;
3586545Sfeldman 	DELAY(delay * 51);
3596520Sfeldman 	/*
3606545Sfeldman 	 * Clear the controller's collision flag, thus enabling retransmit.
3616520Sfeldman 	 */
3626717Sfeldman 	addr->ec_xcr = EC_JINTEN|EC_XINTEN|EC_JCLR;
3636520Sfeldman }
3646520Sfeldman 
3656520Sfeldman /*
3666520Sfeldman  * Ethernet interface receiver interrupt.
3676520Sfeldman  * If input error just drop packet.
3686520Sfeldman  * Otherwise purge input buffered data path and examine
3696520Sfeldman  * packet to determine type.  If can't determine length
3706520Sfeldman  * from type, then have to drop packet.  Othewise decapsulate
3716520Sfeldman  * packet based on type and pass to type specific higher-level
3726520Sfeldman  * input routine.
3736520Sfeldman  */
3746520Sfeldman ecrint(unit)
3756520Sfeldman 	int unit;
3766520Sfeldman {
3776520Sfeldman 	struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
3786520Sfeldman COUNT(ECRINT);
3796520Sfeldman 
3806520Sfeldman 	while (addr->ec_rcr & EC_RDONE)
3816520Sfeldman 		ecread(unit);
3826520Sfeldman }
3836520Sfeldman 
3846520Sfeldman ecread(unit)
3856520Sfeldman 	int unit;
3866520Sfeldman {
3876520Sfeldman 	register struct ec_softc *es = &ec_softc[unit];
3886520Sfeldman 	struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
3896520Sfeldman 	register struct ec_header *ec;
3906520Sfeldman     	struct mbuf *m;
391*7216Ssam 	int len, off, resid, ecoff, buf;
3926520Sfeldman 	register struct ifqueue *inq;
3936520Sfeldman 	caddr_t ecbuf;
3946520Sfeldman COUNT(ECREAD);
3956520Sfeldman 
3966520Sfeldman 	es->es_if.if_ipackets++;
3976520Sfeldman 	buf = addr->ec_rcr & EC_RBN;
3986520Sfeldman 	if (buf < ECRLBF || buf > ECRHBF)
3996520Sfeldman 		panic("ecrint");
4006520Sfeldman 	ecbuf = es->es_buf[buf];
4016520Sfeldman 	ecoff = *(short *)ecbuf;
4026545Sfeldman 	if (ecoff <= ECRDOFF || ecoff > 2046) {
4036520Sfeldman 		es->es_if.if_ierrors++;
4046520Sfeldman #ifdef notdef
4056520Sfeldman 		if (es->es_if.if_ierrors % 100 == 0)
4066520Sfeldman 			printf("ec%d: += 100 input errors\n", unit);
4076520Sfeldman #endif
4086520Sfeldman 		printf("ec%d: input error (offset=%d)\n", unit, ecoff);
4096520Sfeldman 		goto setup;
4106520Sfeldman 	}
4116520Sfeldman 
4126520Sfeldman 	/*
4136520Sfeldman 	 * Get input data length.
4146520Sfeldman 	 * Get pointer to ethernet header (in input buffer).
4156520Sfeldman 	 * Deal with trailer protocol: if type is PUP trailer
4166520Sfeldman 	 * get true type from first 16-bit word past data.
4176520Sfeldman 	 * Remember that type was trailer by setting off.
4186520Sfeldman 	 */
4196520Sfeldman 	len = ecoff - ECRDOFF - sizeof (struct ec_header);
4206520Sfeldman 	ec = (struct ec_header *)(ecbuf + ECRDOFF);
4216520Sfeldman #define	ecdataaddr(ec, off, type)	((type)(((caddr_t)((ec)+1)+(off))))
4226520Sfeldman 	if (ec->ec_type >= ECPUP_TRAIL &&
4236520Sfeldman 	    ec->ec_type < ECPUP_TRAIL+ECPUP_NTRAILER) {
4246520Sfeldman 		off = (ec->ec_type - ECPUP_TRAIL) * 512;
4256520Sfeldman 		if (off >= ECMTU)
4266520Sfeldman 			goto setup;		/* sanity */
4276520Sfeldman 		ec->ec_type = *ecdataaddr(ec, off, u_short *);
4286520Sfeldman 		resid = *(ecdataaddr(ec, off+2, u_short *));
4296520Sfeldman 		if (off + resid > len)
4306520Sfeldman 			goto setup;		/* sanity */
4316520Sfeldman 		len = off + resid;
4326520Sfeldman 	} else
4336520Sfeldman 		off = 0;
4346520Sfeldman 	if (len == 0)
4356520Sfeldman 		goto setup;
4366520Sfeldman 
4376520Sfeldman 	/*
4386520Sfeldman 	 * Pull packet off interface.  Off is nonzero if packet
4396520Sfeldman 	 * has trailing header; ecget will then force this header
4406520Sfeldman 	 * information to be at the front, but we still have to drop
4416520Sfeldman 	 * the type and length which are at the front of any trailer data.
4426520Sfeldman 	 */
4436520Sfeldman 	m = ecget(ecbuf, len, off);
4446520Sfeldman 	if (m == 0)
4456520Sfeldman 		goto setup;
4466520Sfeldman 	if (off) {
4476520Sfeldman 		m->m_off += 2 * sizeof (u_short);
4486520Sfeldman 		m->m_len -= 2 * sizeof (u_short);
4496520Sfeldman 	}
4506520Sfeldman 	switch (ec->ec_type) {
4516520Sfeldman 
4526520Sfeldman #ifdef INET
4536520Sfeldman 	case ECPUP_IPTYPE:
4546520Sfeldman 		schednetisr(NETISR_IP);
4556520Sfeldman 		inq = &ipintrq;
4566520Sfeldman 		break;
4576520Sfeldman #endif
4586520Sfeldman 	default:
4596520Sfeldman 		m_freem(m);
4606520Sfeldman 		goto setup;
4616520Sfeldman 	}
4626520Sfeldman 
4636520Sfeldman 	if (IF_QFULL(inq)) {
4646520Sfeldman 		IF_DROP(inq);
4656520Sfeldman 		m_freem(m);
466*7216Ssam 		goto setup;
467*7216Ssam 	}
468*7216Ssam 	IF_ENQUEUE(inq, m);
4696520Sfeldman 
4706520Sfeldman setup:
4716520Sfeldman 	/*
4726520Sfeldman 	 * Reset for next packet.
4736520Sfeldman 	 */
4746520Sfeldman 	addr->ec_rcr = EC_READ|EC_RCLR|buf;
4756520Sfeldman }
4766520Sfeldman 
4776520Sfeldman /*
4786520Sfeldman  * Ethernet output routine.
4796520Sfeldman  * Encapsulate a packet of type family for the local net.
4806520Sfeldman  * Use trailer local net encapsulation if enough data in first
4816520Sfeldman  * packet leaves a multiple of 512 bytes of data in remainder.
4826545Sfeldman  * If destination is this address or broadcast, send packet to
4836545Sfeldman  * loop device to kludge around the fact that 3com interfaces can't
4846545Sfeldman  * talk to themselves.
4856520Sfeldman  */
4866520Sfeldman ecoutput(ifp, m0, dst)
4876520Sfeldman 	struct ifnet *ifp;
4886520Sfeldman 	struct mbuf *m0;
4896520Sfeldman 	struct sockaddr *dst;
4906520Sfeldman {
4916520Sfeldman 	int type, dest, s, error;
4926520Sfeldman 	register struct ec_softc *es = &ec_softc[ifp->if_unit];
4936520Sfeldman 	register struct mbuf *m = m0;
4946520Sfeldman 	register struct ec_header *ec;
495*7216Ssam 	register int off, i;
4966545Sfeldman 	struct mbuf *mcopy = (struct mbuf *) 0;		/* Null */
4976520Sfeldman 
4986520Sfeldman COUNT(ECOUTPUT);
4996520Sfeldman 	switch (dst->sa_family) {
5006520Sfeldman 
5016520Sfeldman #ifdef INET
5026520Sfeldman 	case AF_INET:
5036520Sfeldman 		dest = ((struct sockaddr_in *)dst)->sin_addr.s_addr;
5046545Sfeldman 		if ((dest &~ 0xff) == 0)
5056545Sfeldman 			mcopy = m_copy(m, 0, M_COPYALL);
5066545Sfeldman 		else if (dest == ((struct sockaddr_in *)&es->es_if.if_addr)->
5076545Sfeldman 		    sin_addr.s_addr) {
5086545Sfeldman 			mcopy = m;
5096545Sfeldman 			goto gotlocal;
5106545Sfeldman 		}
5116520Sfeldman 		off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len;
5126520Sfeldman 		if (off > 0 && (off & 0x1ff) == 0 &&
5136520Sfeldman 		    m->m_off >= MMINOFF + 2 * sizeof (u_short)) {
5146520Sfeldman 			type = ECPUP_TRAIL + (off>>9);
5156520Sfeldman 			m->m_off -= 2 * sizeof (u_short);
5166520Sfeldman 			m->m_len += 2 * sizeof (u_short);
5176520Sfeldman 			*mtod(m, u_short *) = ECPUP_IPTYPE;
5186520Sfeldman 			*(mtod(m, u_short *) + 1) = m->m_len;
5196520Sfeldman 			goto gottrailertype;
5206520Sfeldman 		}
5216520Sfeldman 		type = ECPUP_IPTYPE;
5226520Sfeldman 		off = 0;
5236520Sfeldman 		goto gottype;
5246520Sfeldman #endif
5256520Sfeldman 
5266520Sfeldman 	default:
5276520Sfeldman 		printf("ec%d: can't handle af%d\n", ifp->if_unit,
5286520Sfeldman 			dst->sa_family);
5296520Sfeldman 		error = EAFNOSUPPORT;
5306520Sfeldman 		goto bad;
5316520Sfeldman 	}
5326520Sfeldman 
5336520Sfeldman gottrailertype:
5346520Sfeldman 	/*
5356520Sfeldman 	 * Packet to be sent as trailer: move first packet
5366520Sfeldman 	 * (control information) to end of chain.
5376520Sfeldman 	 */
5386520Sfeldman 	while (m->m_next)
5396520Sfeldman 		m = m->m_next;
5406520Sfeldman 	m->m_next = m0;
5416520Sfeldman 	m = m0->m_next;
5426520Sfeldman 	m0->m_next = 0;
5436520Sfeldman 	m0 = m;
5446520Sfeldman 
5456520Sfeldman gottype:
5466520Sfeldman 	/*
5476520Sfeldman 	 * Add local net header.  If no space in first mbuf,
5486520Sfeldman 	 * allocate another.
5496520Sfeldman 	 */
5506520Sfeldman 	if (m->m_off > MMAXOFF ||
5516520Sfeldman 	    MMINOFF + sizeof (struct ec_header) > m->m_off) {
5526520Sfeldman 		m = m_get(M_DONTWAIT);
5536520Sfeldman 		if (m == 0) {
5546520Sfeldman 			error = ENOBUFS;
5556520Sfeldman 			goto bad;
5566520Sfeldman 		}
5576520Sfeldman 		m->m_next = m0;
5586520Sfeldman 		m->m_off = MMINOFF;
5596520Sfeldman 		m->m_len = sizeof (struct ec_header);
5606520Sfeldman 	} else {
5616520Sfeldman 		m->m_off -= sizeof (struct ec_header);
5626520Sfeldman 		m->m_len += sizeof (struct ec_header);
5636520Sfeldman 	}
5646520Sfeldman 	ec = mtod(m, struct ec_header *);
5656520Sfeldman 	for (i=0; i<6; i++)
5666520Sfeldman 		ec->ec_shost[i] = es->es_enaddr[i];
5676525Sfeldman 	if ((dest &~ 0xff) == 0)
568*7216Ssam 		/* broadcast address */
5696520Sfeldman 		for (i=0; i<6; i++)
5706520Sfeldman 			ec->ec_dhost[i] = 0xff;
5716520Sfeldman 	else {
5726892Sfeldman 		if (dest & 0x8000) {
5736892Sfeldman 			ec->ec_dhost[0] = ec_iltop[0];
5746892Sfeldman 			ec->ec_dhost[1] = ec_iltop[1];
5756892Sfeldman 			ec->ec_dhost[2] = ec_iltop[2];
5766892Sfeldman 		} else {
5776892Sfeldman 			ec->ec_dhost[0] = es->es_enaddr[0];
5786892Sfeldman 			ec->ec_dhost[1] = es->es_enaddr[1];
5796892Sfeldman 			ec->ec_dhost[2] = es->es_enaddr[2];
5806892Sfeldman 		}
5816892Sfeldman 		ec->ec_dhost[3] = (dest>>8) & 0x7f;
5826520Sfeldman 		ec->ec_dhost[4] = (dest>>16) & 0xff;
5836520Sfeldman 		ec->ec_dhost[5] = (dest>>24) & 0xff;
5846520Sfeldman 	}
5856520Sfeldman 	ec->ec_type = type;
5866520Sfeldman 
5876520Sfeldman 	/*
5886520Sfeldman 	 * Queue message on interface, and start output if interface
5896520Sfeldman 	 * not yet active.
5906520Sfeldman 	 */
5916520Sfeldman 	s = splimp();
5926520Sfeldman 	if (IF_QFULL(&ifp->if_snd)) {
5936520Sfeldman 		IF_DROP(&ifp->if_snd);
5946520Sfeldman 		error = ENOBUFS;
5956520Sfeldman 		goto qfull;
5966520Sfeldman 	}
5976520Sfeldman 	IF_ENQUEUE(&ifp->if_snd, m);
5986520Sfeldman 	if (es->es_oactive == 0)
5996520Sfeldman 		ecstart(ifp->if_unit);
6006520Sfeldman 	splx(s);
601*7216Ssam 
6026545Sfeldman gotlocal:
603*7216Ssam 	return(mcopy ? looutput(&loif, mcopy, dst) : 0);
604*7216Ssam 
6056520Sfeldman qfull:
6066520Sfeldman 	m0 = m;
6076520Sfeldman 	splx(s);
6086520Sfeldman bad:
6096520Sfeldman 	m_freem(m0);
6106520Sfeldman 	return(error);
6116520Sfeldman }
6126520Sfeldman 
6136520Sfeldman /*
614*7216Ssam  * Routine to copy from mbuf chain to transmitter
615*7216Ssam  * buffer in UNIBUS memory.
6166520Sfeldman  */
6176520Sfeldman ecput(ecbuf, m)
618*7216Ssam 	u_char *ecbuf;
6196520Sfeldman 	struct mbuf *m;
6206520Sfeldman {
6216520Sfeldman 	register struct mbuf *mp;
622*7216Ssam 	register u_char *bp;
623*7216Ssam 	register int off;
6246520Sfeldman 
6256520Sfeldman COUNT(ECPUT);
626*7216Ssam 	for (off = 2048, mp = m; mp; mp = mp->m_next)
627*7216Ssam 		off -= mp->m_len;
628*7216Ssam 	*(u_short *)ecbuf = off;
629*7216Ssam 	bp = (u_char *)(ecbuf + off);
630*7216Ssam 	for (mp = m; mp; mp = m_free(mp)) {
631*7216Ssam 		register unsigned len;
632*7216Ssam 		register u_char *mcp;
633*7216Ssam 
634*7216Ssam 		len = mp->m_len;
635*7216Ssam 		if (len == 0)
636*7216Ssam 			continue;
637*7216Ssam 		mcp = mtod(mp, u_char *);
638*7216Ssam 		if ((unsigned)bp & 01) {
6397032Swnj 			*bp++ = *mcp++;
640*7216Ssam 			len--;
6417032Swnj 		}
642*7216Ssam 		for (; len > 1; len -= sizeof (u_short)) {
643*7216Ssam 			*(u_short *)bp = *(u_short *)mcp;
644*7216Ssam 			bp += sizeof (u_short);
645*7216Ssam 			mcp += sizeof (u_short);
6467032Swnj 		}
647*7216Ssam 		if (len)
6486520Sfeldman 			*bp++ = *mcp++;
6496520Sfeldman 	}
650*7216Ssam #ifdef notdef
651*7216Ssam 	if (bp - ecbuf != 2048)
652*7216Ssam 		printf("ec: bad ecput, diff=%d\n", bp-ecbuf);
653*7216Ssam #endif
6546520Sfeldman }
6556520Sfeldman 
6566520Sfeldman /*
6576520Sfeldman  * Routine to copy from UNIBUS memory into mbufs.
6586520Sfeldman  * Similar in spirit to if_rubaget.
6597032Swnj  *
6607032Swnj  * Warning: This makes the fairly safe assumption that
6617032Swnj  * mbufs have even lengths.
6626520Sfeldman  */
6636520Sfeldman struct mbuf *
6646520Sfeldman ecget(ecbuf, totlen, off0)
6656520Sfeldman 	char *ecbuf;
6666520Sfeldman 	int totlen, off0;
6676520Sfeldman {
6686520Sfeldman 	struct mbuf *top, **mp, *m;
669*7216Ssam 	int off = off0, len;
670*7216Ssam 	register char *cp, *mcp;
6716520Sfeldman 	register int i;
6726520Sfeldman 
6736520Sfeldman COUNT(ECGET);
6746520Sfeldman 	top = 0;
6756520Sfeldman 	mp = &top;
676*7216Ssam 	cp = ecbuf + ECRDOFF + sizeof (struct ec_header);
6776520Sfeldman 	while (totlen > 0) {
6786520Sfeldman 		MGET(m, 0);
6796520Sfeldman 		if (m == 0)
6806520Sfeldman 			goto bad;
6816520Sfeldman 		if (off) {
6826520Sfeldman 			len = totlen - off;
6836520Sfeldman 			cp = ecbuf + ECRDOFF + sizeof (struct ec_header) + off;
6846520Sfeldman 		} else
6856520Sfeldman 			len = totlen;
6866520Sfeldman 		if (len >= CLBYTES) {
6876520Sfeldman 			struct mbuf *p;
6886520Sfeldman 
6896520Sfeldman 			MCLGET(p, 1);
6906520Sfeldman 			if (p != 0) {
6916520Sfeldman 				m->m_len = len = CLBYTES;
6926520Sfeldman 				m->m_off = (int)p - (int)m;
6936520Sfeldman 			} else {
6946520Sfeldman 				m->m_len = len = MIN(MLEN, len);
6956520Sfeldman 				m->m_off = MMINOFF;
6966520Sfeldman 			}
6976520Sfeldman 		} else {
6986520Sfeldman 			m->m_len = len = MIN(MLEN, len);
6996520Sfeldman 			m->m_off = MMINOFF;
7006520Sfeldman 		}
7016520Sfeldman 		mcp = mtod(m, char *);
702*7216Ssam 		for (i = 0; i < len; i += sizeof (short)) {
7037032Swnj 			*(short *)mcp = *(short *)cp;
704*7216Ssam 			mcp += sizeof (short);
705*7216Ssam 			cp += sizeof (short);
7067032Swnj 		}
707*7216Ssam 		if (len & 01)
7086520Sfeldman 			*mcp++ = *cp++;
7096520Sfeldman 		*mp = m;
7106520Sfeldman 		mp = &m->m_next;
7116520Sfeldman 		if (off) {
7126520Sfeldman 			off += len;
7136520Sfeldman 			if (off == totlen) {
7146520Sfeldman 				cp = ecbuf + ECRDOFF +
7156520Sfeldman 				    sizeof (struct ec_header);
7166520Sfeldman 				off = 0;
7176520Sfeldman 				totlen = off0;
7186520Sfeldman 			}
7196520Sfeldman 		} else
7206520Sfeldman 			totlen -= len;
7216520Sfeldman 	}
7226520Sfeldman 	return (top);
7236520Sfeldman bad:
7246520Sfeldman 	m_freem(top);
7256520Sfeldman 	return (0);
7266520Sfeldman }
727