xref: /csrg-svn/sys/vax/if/if_ec.c (revision 8773)
1*8773Sroot /*	if_ec.c	4.25	82/10/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/vmmac.h"
178461Sroot #include <errno.h>
188461Sroot 
198461Sroot #include "../net/if.h"
208461Sroot #include "../net/netisr.h"
218461Sroot #include "../net/route.h"
228417Swnj #include "../netinet/in.h"
238417Swnj #include "../netinet/in_systm.h"
248417Swnj #include "../netinet/ip.h"
258417Swnj #include "../netinet/ip_var.h"
268417Swnj #include "../netpup/pup.h"
276520Sfeldman 
288461Sroot #include "../vax/cpu.h"
298461Sroot #include "../vax/mtpr.h"
308461Sroot #include "../vaxif/if_ec.h"
318461Sroot #include "../vaxif/if_ecreg.h"
328461Sroot #include "../vaxif/if_uba.h"
338461Sroot #include "../vaxuba/ubareg.h"
348461Sroot #include "../vaxuba/ubavar.h"
358461Sroot 
366520Sfeldman #define	ECMTU	1500
377470Sfeldman #define	ECMEM	0000000
386520Sfeldman 
396520Sfeldman int	ecprobe(), ecattach(), ecrint(), ecxint(), eccollide();
406520Sfeldman struct	uba_device *ecinfo[NEC];
416520Sfeldman u_short ecstd[] = { 0 };
426520Sfeldman struct	uba_driver ecdriver =
436520Sfeldman 	{ ecprobe, 0, ecattach, 0, ecstd, "ec", ecinfo };
446892Sfeldman u_char	ec_iltop[3] = { 0x02, 0x07, 0x01 };
456520Sfeldman #define	ECUNIT(x)	minor(x)
466520Sfeldman 
476520Sfeldman int	ecinit(),ecoutput(),ecreset();
48*8773Sroot struct	mbuf *ecget();
496520Sfeldman 
506545Sfeldman extern struct ifnet loif;
516545Sfeldman 
526520Sfeldman /*
536520Sfeldman  * Ethernet software status per interface.
546520Sfeldman  *
556520Sfeldman  * Each interface is referenced by a network interface structure,
566520Sfeldman  * es_if, which the routing code uses to locate the interface.
576520Sfeldman  * This structure contains the output queue for the interface, its address, ...
586520Sfeldman  * We also have, for each interface, a UBA interface structure, which
596520Sfeldman  * contains information about the UNIBUS resources held by the interface:
606520Sfeldman  * map registers, buffered data paths, etc.  Information is cached in this
616520Sfeldman  * structure for use by the if_uba.c routines in running the interface
626520Sfeldman  * efficiently.
636520Sfeldman  */
646520Sfeldman struct	ec_softc {
656520Sfeldman 	struct	ifnet es_if;		/* network-visible interface */
666520Sfeldman 	struct	ifuba es_ifuba;		/* UNIBUS resources */
676520Sfeldman 	short	es_mask;		/* mask for current output delay */
686520Sfeldman 	short	es_oactive;		/* is output active? */
69*8773Sroot 	u_char	*es_buf[16];		/* virtual addresses of buffers */
706520Sfeldman 	u_char	es_enaddr[6];		/* board's ethernet address */
716520Sfeldman } ec_softc[NEC];
726520Sfeldman 
736520Sfeldman /*
746520Sfeldman  * Do output DMA to determine interface presence and
756520Sfeldman  * interrupt vector.  DMA is too short to disturb other hosts.
766520Sfeldman  */
776520Sfeldman ecprobe(reg)
786520Sfeldman 	caddr_t reg;
796520Sfeldman {
806520Sfeldman 	register int br, cvec;		/* r11, r10 value-result */
816520Sfeldman 	register struct ecdevice *addr = (struct ecdevice *)reg;
827470Sfeldman 	register caddr_t ecbuf = (caddr_t) &umem[numuba][ECMEM];
836520Sfeldman 
846520Sfeldman #ifdef lint
856520Sfeldman 	br = 0; cvec = br; br = cvec;
866520Sfeldman 	ecrint(0); ecxint(0); eccollide(0);
876520Sfeldman #endif
886520Sfeldman 	/*
896637Sfeldman 	 * Make sure memory is turned on
906637Sfeldman 	 */
916637Sfeldman 	addr->ec_rcr = EC_AROM;
926637Sfeldman 	/*
937470Sfeldman 	 * Disable map registers for ec unibus space,
947470Sfeldman 	 * but don't allocate yet.
957470Sfeldman 	 */
96*8773Sroot 	(void) ubamem(numuba, ECMEM, 32*2, 0);
977470Sfeldman 	/*
986520Sfeldman 	 * Check for existence of buffers on Unibus.
996520Sfeldman 	 */
100*8773Sroot 	if (badaddr((caddr_t)ecbuf, 2)) {
1017470Sfeldman 	bad1:
1027470Sfeldman 		printf("ec: buffer mem not found\n");
1037470Sfeldman 	bad2:
104*8773Sroot 		(void) ubamem(numuba, 0, 0, 0);	/* reenable map (780 only) */
1057470Sfeldman 		addr->ec_rcr = EC_MDISAB;	/* disable memory */
1066520Sfeldman 		return (0);
1076520Sfeldman 	}
1087470Sfeldman #if VAX780
1097470Sfeldman 	if (cpu == VAX_780 && uba_hd[numuba].uh_uba->uba_sr) {
1107470Sfeldman 		uba_hd[numuba].uh_uba->uba_sr = uba_hd[numuba].uh_uba->uba_sr;
1117470Sfeldman 		goto bad1;
1127470Sfeldman 	}
1137470Sfeldman #endif
1146520Sfeldman 
1156520Sfeldman 	/*
1166520Sfeldman 	 * Tell the system that the board has memory here, so it won't
1176520Sfeldman 	 * attempt to allocate the addresses later.
1186520Sfeldman 	 */
1197470Sfeldman 	if (ubamem(numuba, ECMEM, 32*2, 1) == 0) {
1207470Sfeldman 		printf("ecprobe: cannot reserve uba addresses\n");
1217470Sfeldman 		goto bad2;
1227470Sfeldman 	}
1236520Sfeldman 
1246520Sfeldman 	/*
1256520Sfeldman 	 * Make a one byte packet in what should be buffer #0.
1266520Sfeldman 	 * Submit it for sending.  This whould cause an xmit interrupt.
1276520Sfeldman 	 * The xmit interrupt vector is 8 bytes after the receive vector,
1286520Sfeldman 	 * so adjust for this before returning.
1296520Sfeldman 	 */
1306520Sfeldman 	*(u_short *)ecbuf = (u_short) 03777;
1316520Sfeldman 	ecbuf[03777] = '\0';
1326520Sfeldman 	addr->ec_xcr = EC_XINTEN|EC_XWBN;
1336520Sfeldman 	DELAY(100000);
1346520Sfeldman 	addr->ec_xcr = EC_XCLR;
1357216Ssam 	if (cvec > 0 && cvec != 0x200) {
1367470Sfeldman 		if (cvec & 04) {	/* collision interrupt */
1377470Sfeldman 			cvec -= 04;
1387470Sfeldman 			br += 1;		/* rcv is collision + 1 */
1397470Sfeldman 		} else {		/* xmit interrupt */
1407470Sfeldman 			cvec -= 010;
1417470Sfeldman 			br += 2;		/* rcv is xmit + 2 */
1427470Sfeldman 		}
1437216Ssam 	}
1446520Sfeldman 	return (1);
1456520Sfeldman }
1466520Sfeldman 
1476520Sfeldman /*
1486520Sfeldman  * Interface exists: make available by filling in network interface
1496520Sfeldman  * record.  System will initialize the interface when it is ready
1506520Sfeldman  * to accept packets.
1516520Sfeldman  */
1526520Sfeldman ecattach(ui)
1536520Sfeldman 	struct uba_device *ui;
1546520Sfeldman {
1557216Ssam 	struct ec_softc *es = &ec_softc[ui->ui_unit];
1567216Ssam 	register struct ifnet *ifp = &es->es_if;
1576520Sfeldman 	register struct ecdevice *addr = (struct ecdevice *)ui->ui_addr;
1587216Ssam 	struct sockaddr_in *sin;
1597216Ssam 	int i, j;
1607216Ssam 	u_char *cp;
1616520Sfeldman 
1627216Ssam 	ifp->if_unit = ui->ui_unit;
1637216Ssam 	ifp->if_name = "ec";
1647216Ssam 	ifp->if_mtu = ECMTU;
1657216Ssam 	ifp->if_net = ui->ui_flags;
1666520Sfeldman 
1676520Sfeldman 	/*
1687216Ssam 	 * Read the ethernet address off the board, one nibble at a time.
1696520Sfeldman 	 */
1706520Sfeldman 	addr->ec_xcr = EC_UECLR;
1716520Sfeldman 	addr->ec_rcr = EC_AROM;
1726520Sfeldman 	cp = es->es_enaddr;
1737216Ssam #define	NEXTBIT	addr->ec_rcr = EC_AROM|EC_ASTEP; addr->ec_rcr = EC_AROM
1746520Sfeldman 	for (i=0; i<6; i++) {
1756520Sfeldman 		*cp = 0;
1766520Sfeldman 		for (j=0; j<=4; j+=4) {
1776520Sfeldman 			*cp |= ((addr->ec_rcr >> 8) & 0xf) << j;
1787216Ssam 			NEXTBIT; NEXTBIT; NEXTBIT; NEXTBIT;
1796520Sfeldman 		}
1806520Sfeldman 		cp++;
1816520Sfeldman 	}
1827216Ssam #ifdef notdef
1836520Sfeldman 	printf("ec%d: addr=%x:%x:%x:%x:%x:%x\n", ui->ui_unit,
1846520Sfeldman 		es->es_enaddr[0]&0xff, es->es_enaddr[1]&0xff,
1856520Sfeldman 		es->es_enaddr[2]&0xff, es->es_enaddr[3]&0xff,
1866520Sfeldman 		es->es_enaddr[4]&0xff, es->es_enaddr[5]&0xff);
1877216Ssam #endif
1887216Ssam 	ifp->if_host[0] = ((es->es_enaddr[3]&0xff)<<16) |
1896520Sfeldman 	    ((es->es_enaddr[4]&0xff)<<8) | (es->es_enaddr[5]&0xff);
1906520Sfeldman 	sin = (struct sockaddr_in *)&es->es_if.if_addr;
1916520Sfeldman 	sin->sin_family = AF_INET;
1927216Ssam 	sin->sin_addr = if_makeaddr(ifp->if_net, ifp->if_host[0]);
1936520Sfeldman 
1947216Ssam 	sin = (struct sockaddr_in *)&ifp->if_broadaddr;
1956520Sfeldman 	sin->sin_family = AF_INET;
1967216Ssam 	sin->sin_addr = if_makeaddr(ifp->if_net, INADDR_ANY);
1977216Ssam 	ifp->if_flags = IFF_BROADCAST;
1986520Sfeldman 
1997216Ssam 	ifp->if_init = ecinit;
2007216Ssam 	ifp->if_output = ecoutput;
2017216Ssam 	ifp->if_ubareset = ecreset;
2026520Sfeldman 	for (i=0; i<16; i++)
203*8773Sroot 		es->es_buf[i] = (u_char *)&umem[ui->ui_ubanum][ECMEM+2048*i];
2047216Ssam 	if_attach(ifp);
2056520Sfeldman }
2066520Sfeldman 
2076520Sfeldman /*
2086520Sfeldman  * Reset of interface after UNIBUS reset.
2096520Sfeldman  * If interface is on specified uba, reset its state.
2106520Sfeldman  */
2116520Sfeldman ecreset(unit, uban)
2126520Sfeldman 	int unit, uban;
2136520Sfeldman {
2146520Sfeldman 	register struct uba_device *ui;
2156520Sfeldman 
2166520Sfeldman 	if (unit >= NEC || (ui = ecinfo[unit]) == 0 || ui->ui_alive == 0 ||
2176520Sfeldman 	    ui->ui_ubanum != uban)
2186520Sfeldman 		return;
2196520Sfeldman 	printf(" ec%d", unit);
220*8773Sroot 	(void) ubamem(uban, ECMEM, 32*2, 0);	/* mr disable (no alloc) */
2216520Sfeldman 	ecinit(unit);
2226520Sfeldman }
2236520Sfeldman 
2246520Sfeldman /*
2256520Sfeldman  * Initialization of interface; clear recorded pending
2266520Sfeldman  * operations, and reinitialize UNIBUS usage.
2276520Sfeldman  */
2286520Sfeldman ecinit(unit)
2296520Sfeldman 	int unit;
2306520Sfeldman {
2317216Ssam 	struct ec_softc *es = &ec_softc[unit];
2327216Ssam 	struct ecdevice *addr;
2337216Ssam 	int i, s;
2346520Sfeldman 
2356520Sfeldman 	/*
2367217Sfeldman 	 * Hang receive buffers and start any pending writes.
2376637Sfeldman 	 * Writing into the rcr also makes sure the memory
2386637Sfeldman 	 * is turned on.
2396520Sfeldman 	 */
2407216Ssam 	addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
2416520Sfeldman 	s = splimp();
2426520Sfeldman 	for (i=ECRHBF; i>=ECRLBF; i--)
2436520Sfeldman 		addr->ec_rcr = EC_READ|i;
2447217Sfeldman 	es->es_oactive = 0;
2457217Sfeldman 	es->es_mask = ~0;
2466520Sfeldman 	es->es_if.if_flags |= IFF_UP;
2477217Sfeldman 	if (es->es_if.if_snd.ifq_head)
2487217Sfeldman 		ecstart(unit);
2496520Sfeldman 	splx(s);
2507150Swnj 	if_rtinit(&es->es_if, RTF_UP);
2516520Sfeldman }
2526520Sfeldman 
2536520Sfeldman /*
2546520Sfeldman  * Start or restart output on interface.
2556520Sfeldman  * If interface is already active, then this is a retransmit
2566545Sfeldman  * after a collision, and just restuff registers.
2576520Sfeldman  * If interface is not already active, get another datagram
2586520Sfeldman  * to send off of the interface queue, and map it to the interface
2596520Sfeldman  * before starting the output.
2606520Sfeldman  */
2616520Sfeldman ecstart(dev)
2626520Sfeldman 	dev_t dev;
2636520Sfeldman {
264*8773Sroot         int unit = ECUNIT(dev);
2657216Ssam 	struct ec_softc *es = &ec_softc[unit];
2667216Ssam 	struct ecdevice *addr;
2676520Sfeldman 	struct mbuf *m;
2686520Sfeldman 
2696520Sfeldman 	if (es->es_oactive)
2706520Sfeldman 		goto restart;
2716520Sfeldman 
2726520Sfeldman 	IF_DEQUEUE(&es->es_if.if_snd, m);
2736520Sfeldman 	if (m == 0) {
2746520Sfeldman 		es->es_oactive = 0;
2756520Sfeldman 		return;
2766520Sfeldman 	}
2776520Sfeldman 	ecput(es->es_buf[ECTBF], m);
2786520Sfeldman 
2796520Sfeldman restart:
2807216Ssam 	addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
2816520Sfeldman 	addr->ec_xcr = EC_WRITE|ECTBF;
2826520Sfeldman 	es->es_oactive = 1;
2836520Sfeldman }
2846520Sfeldman 
2856520Sfeldman /*
2866520Sfeldman  * Ethernet interface transmitter interrupt.
2876520Sfeldman  * Start another output if more data to send.
2886520Sfeldman  */
2896520Sfeldman ecxint(unit)
2906520Sfeldman 	int unit;
2916520Sfeldman {
2926520Sfeldman 	register struct ec_softc *es = &ec_softc[unit];
2937216Ssam 	register struct ecdevice *addr =
2947216Ssam 		(struct ecdevice *)ecinfo[unit]->ui_addr;
2956520Sfeldman 
2966520Sfeldman 	if (es->es_oactive == 0)
2976520Sfeldman 		return;
2987216Ssam 	if ((addr->ec_xcr&EC_XDONE) == 0 || (addr->ec_xcr&EC_XBN) != ECTBF) {
2997216Ssam 		printf("ec%d: stray xmit interrupt, xcr=%b\n", unit,
3007216Ssam 			addr->ec_xcr, EC_XBITS);
3017216Ssam 		es->es_oactive = 0;
3027216Ssam 		addr->ec_xcr = EC_XCLR;
3037216Ssam 		return;
3047216Ssam 	}
3056520Sfeldman 	es->es_if.if_opackets++;
3066520Sfeldman 	es->es_oactive = 0;
3076520Sfeldman 	es->es_mask = ~0;
3086520Sfeldman 	addr->ec_xcr = EC_XCLR;
3097216Ssam 	if (es->es_if.if_snd.ifq_head)
3107216Ssam 		ecstart(unit);
3116520Sfeldman }
3126520Sfeldman 
3136520Sfeldman /*
3146520Sfeldman  * Collision on ethernet interface.  Do exponential
3156520Sfeldman  * backoff, and retransmit.  If have backed off all
3166520Sfeldman  * the way print warning diagnostic, and drop packet.
3176520Sfeldman  */
3186520Sfeldman eccollide(unit)
3196520Sfeldman 	int unit;
3206520Sfeldman {
3216520Sfeldman 	struct ec_softc *es = &ec_softc[unit];
3226520Sfeldman 
3236520Sfeldman 	es->es_if.if_collisions++;
3247216Ssam 	if (es->es_oactive)
3257216Ssam 		ecdocoll(unit);
3266520Sfeldman }
3276520Sfeldman 
3286520Sfeldman ecdocoll(unit)
3296520Sfeldman 	int unit;
3306520Sfeldman {
3316520Sfeldman 	register struct ec_softc *es = &ec_softc[unit];
3326545Sfeldman 	register struct ecdevice *addr =
3336545Sfeldman 	    (struct ecdevice *)ecinfo[unit]->ui_addr;
3346545Sfeldman 	register i;
3356545Sfeldman 	int delay;
3366520Sfeldman 
3376520Sfeldman 	/*
3386520Sfeldman 	 * Es_mask is a 16 bit number with n low zero bits, with
3396520Sfeldman 	 * n the number of backoffs.  When es_mask is 0 we have
3406520Sfeldman 	 * backed off 16 times, and give up.
3416520Sfeldman 	 */
3426520Sfeldman 	if (es->es_mask == 0) {
3436545Sfeldman 		es->es_if.if_oerrors++;
3446520Sfeldman 		printf("ec%d: send error\n", unit);
3456520Sfeldman 		/*
3466545Sfeldman 		 * Reset interface, then requeue rcv buffers.
3476545Sfeldman 		 * Some incoming packets may be lost, but that
3486545Sfeldman 		 * can't be helped.
3496520Sfeldman 		 */
3506545Sfeldman 		addr->ec_xcr = EC_UECLR;
3516545Sfeldman 		for (i=ECRHBF; i>=ECRLBF; i--)
3526545Sfeldman 			addr->ec_rcr = EC_READ|i;
3536545Sfeldman 		/*
3546545Sfeldman 		 * Reset and transmit next packet (if any).
3556545Sfeldman 		 */
3566545Sfeldman 		es->es_oactive = 0;
3576545Sfeldman 		es->es_mask = ~0;
3586545Sfeldman 		if (es->es_if.if_snd.ifq_head)
3596545Sfeldman 			ecstart(unit);
3606520Sfeldman 		return;
3616520Sfeldman 	}
3626520Sfeldman 	/*
3636545Sfeldman 	 * Do exponential backoff.  Compute delay based on low bits
3646545Sfeldman 	 * of the interval timer.  Then delay for that number of
3656545Sfeldman 	 * slot times.  A slot time is 51.2 microseconds (rounded to 51).
3666545Sfeldman 	 * This does not take into account the time already used to
3676545Sfeldman 	 * process the interrupt.
3686520Sfeldman 	 */
3696520Sfeldman 	es->es_mask <<= 1;
3706545Sfeldman 	delay = mfpr(ICR) &~ es->es_mask;
3716545Sfeldman 	DELAY(delay * 51);
3726520Sfeldman 	/*
3736545Sfeldman 	 * Clear the controller's collision flag, thus enabling retransmit.
3746520Sfeldman 	 */
3757470Sfeldman 	addr->ec_xcr = EC_CLEAR;
3766520Sfeldman }
3776520Sfeldman 
3786520Sfeldman /*
3796520Sfeldman  * Ethernet interface receiver interrupt.
3806520Sfeldman  * If input error just drop packet.
3816520Sfeldman  * Otherwise purge input buffered data path and examine
3826520Sfeldman  * packet to determine type.  If can't determine length
3836520Sfeldman  * from type, then have to drop packet.  Othewise decapsulate
3846520Sfeldman  * packet based on type and pass to type specific higher-level
3856520Sfeldman  * input routine.
3866520Sfeldman  */
3876520Sfeldman ecrint(unit)
3886520Sfeldman 	int unit;
3896520Sfeldman {
3906520Sfeldman 	struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
3916520Sfeldman 
3926520Sfeldman 	while (addr->ec_rcr & EC_RDONE)
3936520Sfeldman 		ecread(unit);
3946520Sfeldman }
3956520Sfeldman 
3966520Sfeldman ecread(unit)
3976520Sfeldman 	int unit;
3986520Sfeldman {
3996520Sfeldman 	register struct ec_softc *es = &ec_softc[unit];
4006520Sfeldman 	struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
4016520Sfeldman 	register struct ec_header *ec;
4026520Sfeldman     	struct mbuf *m;
403*8773Sroot 	int len, off, resid, ecoff, rbuf;
4046520Sfeldman 	register struct ifqueue *inq;
405*8773Sroot 	u_char *ecbuf;
4066520Sfeldman 
4076520Sfeldman 	es->es_if.if_ipackets++;
408*8773Sroot 	rbuf = addr->ec_rcr & EC_RBN;
409*8773Sroot 	if (rbuf < ECRLBF || rbuf > ECRHBF)
4106520Sfeldman 		panic("ecrint");
411*8773Sroot 	ecbuf = es->es_buf[rbuf];
4126520Sfeldman 	ecoff = *(short *)ecbuf;
4136545Sfeldman 	if (ecoff <= ECRDOFF || ecoff > 2046) {
4146520Sfeldman 		es->es_if.if_ierrors++;
4156520Sfeldman #ifdef notdef
4166520Sfeldman 		if (es->es_if.if_ierrors % 100 == 0)
4176520Sfeldman 			printf("ec%d: += 100 input errors\n", unit);
4186520Sfeldman #endif
4196520Sfeldman 		goto setup;
4206520Sfeldman 	}
4216520Sfeldman 
4226520Sfeldman 	/*
4236520Sfeldman 	 * Get input data length.
4246520Sfeldman 	 * Get pointer to ethernet header (in input buffer).
4256520Sfeldman 	 * Deal with trailer protocol: if type is PUP trailer
4266520Sfeldman 	 * get true type from first 16-bit word past data.
4276520Sfeldman 	 * Remember that type was trailer by setting off.
4286520Sfeldman 	 */
4296520Sfeldman 	len = ecoff - ECRDOFF - sizeof (struct ec_header);
4306520Sfeldman 	ec = (struct ec_header *)(ecbuf + ECRDOFF);
4316520Sfeldman #define	ecdataaddr(ec, off, type)	((type)(((caddr_t)((ec)+1)+(off))))
4326520Sfeldman 	if (ec->ec_type >= ECPUP_TRAIL &&
4336520Sfeldman 	    ec->ec_type < ECPUP_TRAIL+ECPUP_NTRAILER) {
4346520Sfeldman 		off = (ec->ec_type - ECPUP_TRAIL) * 512;
4356520Sfeldman 		if (off >= ECMTU)
4366520Sfeldman 			goto setup;		/* sanity */
4376520Sfeldman 		ec->ec_type = *ecdataaddr(ec, off, u_short *);
4386520Sfeldman 		resid = *(ecdataaddr(ec, off+2, u_short *));
4396520Sfeldman 		if (off + resid > len)
4406520Sfeldman 			goto setup;		/* sanity */
4416520Sfeldman 		len = off + resid;
4426520Sfeldman 	} else
4436520Sfeldman 		off = 0;
4446520Sfeldman 	if (len == 0)
4456520Sfeldman 		goto setup;
4466520Sfeldman 
4476520Sfeldman 	/*
4486520Sfeldman 	 * Pull packet off interface.  Off is nonzero if packet
4496520Sfeldman 	 * has trailing header; ecget will then force this header
4506520Sfeldman 	 * information to be at the front, but we still have to drop
4516520Sfeldman 	 * the type and length which are at the front of any trailer data.
4526520Sfeldman 	 */
4536520Sfeldman 	m = ecget(ecbuf, len, off);
4546520Sfeldman 	if (m == 0)
4556520Sfeldman 		goto setup;
4566520Sfeldman 	if (off) {
4576520Sfeldman 		m->m_off += 2 * sizeof (u_short);
4586520Sfeldman 		m->m_len -= 2 * sizeof (u_short);
4596520Sfeldman 	}
4606520Sfeldman 	switch (ec->ec_type) {
4616520Sfeldman 
4626520Sfeldman #ifdef INET
4636520Sfeldman 	case ECPUP_IPTYPE:
4646520Sfeldman 		schednetisr(NETISR_IP);
4656520Sfeldman 		inq = &ipintrq;
4666520Sfeldman 		break;
4676520Sfeldman #endif
4686520Sfeldman 	default:
4696520Sfeldman 		m_freem(m);
4706520Sfeldman 		goto setup;
4716520Sfeldman 	}
4726520Sfeldman 
4736520Sfeldman 	if (IF_QFULL(inq)) {
4746520Sfeldman 		IF_DROP(inq);
4756520Sfeldman 		m_freem(m);
4767216Ssam 		goto setup;
4777216Ssam 	}
4787216Ssam 	IF_ENQUEUE(inq, m);
4796520Sfeldman 
4806520Sfeldman setup:
4816520Sfeldman 	/*
4826520Sfeldman 	 * Reset for next packet.
4836520Sfeldman 	 */
484*8773Sroot 	addr->ec_rcr = EC_READ|EC_RCLR|rbuf;
4856520Sfeldman }
4866520Sfeldman 
4876520Sfeldman /*
4886520Sfeldman  * Ethernet output routine.
4896520Sfeldman  * Encapsulate a packet of type family for the local net.
4906520Sfeldman  * Use trailer local net encapsulation if enough data in first
4916520Sfeldman  * packet leaves a multiple of 512 bytes of data in remainder.
4926545Sfeldman  * If destination is this address or broadcast, send packet to
4936545Sfeldman  * loop device to kludge around the fact that 3com interfaces can't
4946545Sfeldman  * talk to themselves.
4956520Sfeldman  */
4966520Sfeldman ecoutput(ifp, m0, dst)
4976520Sfeldman 	struct ifnet *ifp;
4986520Sfeldman 	struct mbuf *m0;
4996520Sfeldman 	struct sockaddr *dst;
5006520Sfeldman {
5016520Sfeldman 	int type, dest, s, error;
5026520Sfeldman 	register struct ec_softc *es = &ec_softc[ifp->if_unit];
5036520Sfeldman 	register struct mbuf *m = m0;
5046520Sfeldman 	register struct ec_header *ec;
5057216Ssam 	register int off, i;
5066545Sfeldman 	struct mbuf *mcopy = (struct mbuf *) 0;		/* Null */
5076520Sfeldman 
5086520Sfeldman 	switch (dst->sa_family) {
5096520Sfeldman 
5106520Sfeldman #ifdef INET
5116520Sfeldman 	case AF_INET:
5126520Sfeldman 		dest = ((struct sockaddr_in *)dst)->sin_addr.s_addr;
5136545Sfeldman 		if ((dest &~ 0xff) == 0)
514*8773Sroot 			mcopy = m_copy(m, 0, (long)M_COPYALL);
5156545Sfeldman 		else if (dest == ((struct sockaddr_in *)&es->es_if.if_addr)->
5166545Sfeldman 		    sin_addr.s_addr) {
5176545Sfeldman 			mcopy = m;
5186545Sfeldman 			goto gotlocal;
5196545Sfeldman 		}
5206520Sfeldman 		off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len;
5216520Sfeldman 		if (off > 0 && (off & 0x1ff) == 0 &&
5226520Sfeldman 		    m->m_off >= MMINOFF + 2 * sizeof (u_short)) {
5236520Sfeldman 			type = ECPUP_TRAIL + (off>>9);
5246520Sfeldman 			m->m_off -= 2 * sizeof (u_short);
5256520Sfeldman 			m->m_len += 2 * sizeof (u_short);
5266520Sfeldman 			*mtod(m, u_short *) = ECPUP_IPTYPE;
5276520Sfeldman 			*(mtod(m, u_short *) + 1) = m->m_len;
5286520Sfeldman 			goto gottrailertype;
5296520Sfeldman 		}
5306520Sfeldman 		type = ECPUP_IPTYPE;
5316520Sfeldman 		off = 0;
5326520Sfeldman 		goto gottype;
5336520Sfeldman #endif
5346520Sfeldman 
5356520Sfeldman 	default:
5366520Sfeldman 		printf("ec%d: can't handle af%d\n", ifp->if_unit,
5376520Sfeldman 			dst->sa_family);
5386520Sfeldman 		error = EAFNOSUPPORT;
5396520Sfeldman 		goto bad;
5406520Sfeldman 	}
5416520Sfeldman 
5426520Sfeldman gottrailertype:
5436520Sfeldman 	/*
5446520Sfeldman 	 * Packet to be sent as trailer: move first packet
5456520Sfeldman 	 * (control information) to end of chain.
5466520Sfeldman 	 */
5476520Sfeldman 	while (m->m_next)
5486520Sfeldman 		m = m->m_next;
5496520Sfeldman 	m->m_next = m0;
5506520Sfeldman 	m = m0->m_next;
5516520Sfeldman 	m0->m_next = 0;
5526520Sfeldman 	m0 = m;
5536520Sfeldman 
5546520Sfeldman gottype:
5556520Sfeldman 	/*
5566520Sfeldman 	 * Add local net header.  If no space in first mbuf,
5576520Sfeldman 	 * allocate another.
5586520Sfeldman 	 */
5596520Sfeldman 	if (m->m_off > MMAXOFF ||
5606520Sfeldman 	    MMINOFF + sizeof (struct ec_header) > m->m_off) {
5616520Sfeldman 		m = m_get(M_DONTWAIT);
5626520Sfeldman 		if (m == 0) {
5636520Sfeldman 			error = ENOBUFS;
5646520Sfeldman 			goto bad;
5656520Sfeldman 		}
5666520Sfeldman 		m->m_next = m0;
5676520Sfeldman 		m->m_off = MMINOFF;
5686520Sfeldman 		m->m_len = sizeof (struct ec_header);
5696520Sfeldman 	} else {
5706520Sfeldman 		m->m_off -= sizeof (struct ec_header);
5716520Sfeldman 		m->m_len += sizeof (struct ec_header);
5726520Sfeldman 	}
5736520Sfeldman 	ec = mtod(m, struct ec_header *);
5746520Sfeldman 	for (i=0; i<6; i++)
5756520Sfeldman 		ec->ec_shost[i] = es->es_enaddr[i];
5766525Sfeldman 	if ((dest &~ 0xff) == 0)
5777216Ssam 		/* broadcast address */
5786520Sfeldman 		for (i=0; i<6; i++)
5796520Sfeldman 			ec->ec_dhost[i] = 0xff;
5806520Sfeldman 	else {
5816892Sfeldman 		if (dest & 0x8000) {
5826892Sfeldman 			ec->ec_dhost[0] = ec_iltop[0];
5836892Sfeldman 			ec->ec_dhost[1] = ec_iltop[1];
5846892Sfeldman 			ec->ec_dhost[2] = ec_iltop[2];
5856892Sfeldman 		} else {
5866892Sfeldman 			ec->ec_dhost[0] = es->es_enaddr[0];
5876892Sfeldman 			ec->ec_dhost[1] = es->es_enaddr[1];
5886892Sfeldman 			ec->ec_dhost[2] = es->es_enaddr[2];
5896892Sfeldman 		}
5906892Sfeldman 		ec->ec_dhost[3] = (dest>>8) & 0x7f;
5916520Sfeldman 		ec->ec_dhost[4] = (dest>>16) & 0xff;
5926520Sfeldman 		ec->ec_dhost[5] = (dest>>24) & 0xff;
5936520Sfeldman 	}
5946520Sfeldman 	ec->ec_type = type;
5956520Sfeldman 
5966520Sfeldman 	/*
5976520Sfeldman 	 * Queue message on interface, and start output if interface
5986520Sfeldman 	 * not yet active.
5996520Sfeldman 	 */
6006520Sfeldman 	s = splimp();
6016520Sfeldman 	if (IF_QFULL(&ifp->if_snd)) {
6026520Sfeldman 		IF_DROP(&ifp->if_snd);
6036520Sfeldman 		error = ENOBUFS;
6046520Sfeldman 		goto qfull;
6056520Sfeldman 	}
6066520Sfeldman 	IF_ENQUEUE(&ifp->if_snd, m);
6076520Sfeldman 	if (es->es_oactive == 0)
6086520Sfeldman 		ecstart(ifp->if_unit);
6096520Sfeldman 	splx(s);
6107216Ssam 
6116545Sfeldman gotlocal:
6127216Ssam 	return(mcopy ? looutput(&loif, mcopy, dst) : 0);
6137216Ssam 
6146520Sfeldman qfull:
6156520Sfeldman 	m0 = m;
6166520Sfeldman 	splx(s);
6176520Sfeldman bad:
6186520Sfeldman 	m_freem(m0);
6196520Sfeldman 	return(error);
6206520Sfeldman }
6216520Sfeldman 
6226520Sfeldman /*
6237216Ssam  * Routine to copy from mbuf chain to transmitter
6247216Ssam  * buffer in UNIBUS memory.
6256520Sfeldman  */
6266520Sfeldman ecput(ecbuf, m)
6277216Ssam 	u_char *ecbuf;
6286520Sfeldman 	struct mbuf *m;
6296520Sfeldman {
6306520Sfeldman 	register struct mbuf *mp;
6317216Ssam 	register int off;
6327263Ssam 	u_char *bp;
6336520Sfeldman 
6347216Ssam 	for (off = 2048, mp = m; mp; mp = mp->m_next)
6357216Ssam 		off -= mp->m_len;
6367216Ssam 	*(u_short *)ecbuf = off;
6377216Ssam 	bp = (u_char *)(ecbuf + off);
6387263Ssam 	for (mp = m; mp; mp = mp->m_next) {
6397263Ssam 		register unsigned len = mp->m_len;
6407263Ssam 		u_char *mcp;
6417216Ssam 
6427216Ssam 		if (len == 0)
6437216Ssam 			continue;
6447216Ssam 		mcp = mtod(mp, u_char *);
6457216Ssam 		if ((unsigned)bp & 01) {
6467032Swnj 			*bp++ = *mcp++;
6477216Ssam 			len--;
6487032Swnj 		}
6497263Ssam 		if (off = (len >> 1)) {
6507263Ssam 			register u_short *to, *from;
6517263Ssam 
6527263Ssam 			to = (u_short *)bp;
6537263Ssam 			from = (u_short *)mcp;
6547263Ssam 			do
6557263Ssam 				*to++ = *from++;
6567263Ssam 			while (--off > 0);
6577263Ssam 			bp = (u_char *)to,
6587263Ssam 			mcp = (u_char *)from;
6597032Swnj 		}
6607263Ssam 		if (len & 01)
6616520Sfeldman 			*bp++ = *mcp++;
6626520Sfeldman 	}
6637216Ssam #ifdef notdef
6647216Ssam 	if (bp - ecbuf != 2048)
6657216Ssam 		printf("ec: bad ecput, diff=%d\n", bp-ecbuf);
6667216Ssam #endif
6677263Ssam 	m_freem(m);
6686520Sfeldman }
6696520Sfeldman 
6706520Sfeldman /*
6716520Sfeldman  * Routine to copy from UNIBUS memory into mbufs.
6726520Sfeldman  * Similar in spirit to if_rubaget.
6737032Swnj  *
6747032Swnj  * Warning: This makes the fairly safe assumption that
6757032Swnj  * mbufs have even lengths.
6766520Sfeldman  */
6776520Sfeldman struct mbuf *
6786520Sfeldman ecget(ecbuf, totlen, off0)
6797263Ssam 	u_char *ecbuf;
6806520Sfeldman 	int totlen, off0;
6816520Sfeldman {
6827263Ssam 	register struct mbuf *m;
6837263Ssam 	struct mbuf *top = 0, **mp = &top;
6847263Ssam 	register int off = off0, len;
6857263Ssam 	u_char *cp;
6866520Sfeldman 
6877216Ssam 	cp = ecbuf + ECRDOFF + sizeof (struct ec_header);
6886520Sfeldman 	while (totlen > 0) {
6897263Ssam 		register int words;
6907263Ssam 		u_char *mcp;
6917263Ssam 
6926520Sfeldman 		MGET(m, 0);
6936520Sfeldman 		if (m == 0)
6946520Sfeldman 			goto bad;
6956520Sfeldman 		if (off) {
6966520Sfeldman 			len = totlen - off;
6976520Sfeldman 			cp = ecbuf + ECRDOFF + sizeof (struct ec_header) + off;
6986520Sfeldman 		} else
6996520Sfeldman 			len = totlen;
7006520Sfeldman 		if (len >= CLBYTES) {
7016520Sfeldman 			struct mbuf *p;
7026520Sfeldman 
7036520Sfeldman 			MCLGET(p, 1);
7046520Sfeldman 			if (p != 0) {
7056520Sfeldman 				m->m_len = len = CLBYTES;
7066520Sfeldman 				m->m_off = (int)p - (int)m;
7076520Sfeldman 			} else {
7086520Sfeldman 				m->m_len = len = MIN(MLEN, len);
7096520Sfeldman 				m->m_off = MMINOFF;
7106520Sfeldman 			}
7116520Sfeldman 		} else {
7126520Sfeldman 			m->m_len = len = MIN(MLEN, len);
7136520Sfeldman 			m->m_off = MMINOFF;
7146520Sfeldman 		}
7157263Ssam 		mcp = mtod(m, u_char *);
7167263Ssam 		if (words = (len >> 1)) {
7177263Ssam 			register u_short *to, *from;
7187263Ssam 
7197263Ssam 			to = (u_short *)mcp;
7207263Ssam 			from = (u_short *)cp;
7217263Ssam 			do
7227263Ssam 				*to++ = *from++;
7237263Ssam 			while (--words > 0);
7247263Ssam 			mcp = (u_char *)to;
7257263Ssam 			cp = (u_char *)from;
7267032Swnj 		}
7277216Ssam 		if (len & 01)
7286520Sfeldman 			*mcp++ = *cp++;
7296520Sfeldman 		*mp = m;
7306520Sfeldman 		mp = &m->m_next;
7317263Ssam 		if (off == 0) {
7326520Sfeldman 			totlen -= len;
7337263Ssam 			continue;
7347263Ssam 		}
7357263Ssam 		off += len;
7367263Ssam 		if (off == totlen) {
7377263Ssam 			cp = ecbuf + ECRDOFF + sizeof (struct ec_header);
7387263Ssam 			off = 0;
7397263Ssam 			totlen = off0;
7407263Ssam 		}
7416520Sfeldman 	}
7426520Sfeldman 	return (top);
7436520Sfeldman bad:
7446520Sfeldman 	m_freem(top);
7456520Sfeldman 	return (0);
7466520Sfeldman }
747