xref: /csrg-svn/sys/vax/if/if_ec.c (revision 16207)
1*16207Skarels /*	if_ec.c	6.2	84/03/20	*/
26520Sfeldman 
36520Sfeldman #include "ec.h"
46520Sfeldman 
56520Sfeldman /*
66520Sfeldman  * 3Com Ethernet Controller interface
76520Sfeldman  */
89795Ssam #include "../machine/pte.h"
96520Sfeldman 
106520Sfeldman #include "../h/param.h"
116520Sfeldman #include "../h/systm.h"
126520Sfeldman #include "../h/mbuf.h"
136520Sfeldman #include "../h/buf.h"
146520Sfeldman #include "../h/protosw.h"
156520Sfeldman #include "../h/socket.h"
166520Sfeldman #include "../h/vmmac.h"
1713055Ssam #include "../h/ioctl.h"
1813055Ssam #include "../h/errno.h"
198461Sroot 
208461Sroot #include "../net/if.h"
218461Sroot #include "../net/netisr.h"
228461Sroot #include "../net/route.h"
238417Swnj #include "../netinet/in.h"
248417Swnj #include "../netinet/in_systm.h"
258417Swnj #include "../netinet/ip.h"
268417Swnj #include "../netinet/ip_var.h"
2711574Ssam #include "../netinet/if_ether.h"
288417Swnj #include "../netpup/pup.h"
296520Sfeldman 
308461Sroot #include "../vax/cpu.h"
318461Sroot #include "../vax/mtpr.h"
328461Sroot #include "../vaxif/if_ecreg.h"
338461Sroot #include "../vaxif/if_uba.h"
348461Sroot #include "../vaxuba/ubareg.h"
358461Sroot #include "../vaxuba/ubavar.h"
368461Sroot 
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 };
446520Sfeldman #define	ECUNIT(x)	minor(x)
456520Sfeldman 
4613055Ssam int	ecinit(),ecioctl(),ecoutput(),ecreset();
478773Sroot struct	mbuf *ecget();
486520Sfeldman 
496545Sfeldman extern struct ifnet loif;
506545Sfeldman 
516520Sfeldman /*
526520Sfeldman  * Ethernet software status per interface.
536520Sfeldman  *
546520Sfeldman  * Each interface is referenced by a network interface structure,
556520Sfeldman  * es_if, which the routing code uses to locate the interface.
566520Sfeldman  * This structure contains the output queue for the interface, its address, ...
576520Sfeldman  * We also have, for each interface, a UBA interface structure, which
586520Sfeldman  * contains information about the UNIBUS resources held by the interface:
596520Sfeldman  * map registers, buffered data paths, etc.  Information is cached in this
606520Sfeldman  * structure for use by the if_uba.c routines in running the interface
616520Sfeldman  * efficiently.
626520Sfeldman  */
636520Sfeldman struct	ec_softc {
6411574Ssam 	struct	arpcom es_ac;		/* common Ethernet structures */
6511574Ssam #define	es_if	es_ac.ac_if		/* network-visible interface */
6611574Ssam #define	es_addr	es_ac.ac_enaddr		/* hardware Ethernet address */
676520Sfeldman 	struct	ifuba es_ifuba;		/* UNIBUS resources */
686520Sfeldman 	short	es_mask;		/* mask for current output delay */
696520Sfeldman 	short	es_oactive;		/* is output active? */
708773Sroot 	u_char	*es_buf[16];		/* virtual addresses of buffers */
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 	 */
968773Sroot 	(void) ubamem(numuba, ECMEM, 32*2, 0);
977470Sfeldman 	/*
986520Sfeldman 	 * Check for existence of buffers on Unibus.
996520Sfeldman 	 */
1008773Sroot 	if (badaddr((caddr_t)ecbuf, 2)) {
1017470Sfeldman 	bad1:
1027470Sfeldman 		printf("ec: buffer mem not found\n");
1037470Sfeldman 	bad2:
1048773Sroot 		(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";
1649745Ssam 	ifp->if_mtu = ETHERMTU;
1656520Sfeldman 
1666520Sfeldman 	/*
1677216Ssam 	 * Read the ethernet address off the board, one nibble at a time.
1686520Sfeldman 	 */
1696520Sfeldman 	addr->ec_xcr = EC_UECLR;
1706520Sfeldman 	addr->ec_rcr = EC_AROM;
17111574Ssam 	cp = es->es_addr;
1727216Ssam #define	NEXTBIT	addr->ec_rcr = EC_AROM|EC_ASTEP; addr->ec_rcr = EC_AROM
1736520Sfeldman 	for (i=0; i<6; i++) {
1746520Sfeldman 		*cp = 0;
1756520Sfeldman 		for (j=0; j<=4; j+=4) {
1766520Sfeldman 			*cp |= ((addr->ec_rcr >> 8) & 0xf) << j;
1777216Ssam 			NEXTBIT; NEXTBIT; NEXTBIT; NEXTBIT;
1786520Sfeldman 		}
1796520Sfeldman 		cp++;
1806520Sfeldman 	}
1816520Sfeldman 	sin = (struct sockaddr_in *)&es->es_if.if_addr;
1826520Sfeldman 	sin->sin_family = AF_INET;
18312771Ssam 	sin->sin_addr = arpmyaddr((struct arpcom *)0);
1847216Ssam 	ifp->if_init = ecinit;
18513055Ssam 	ifp->if_ioctl = ecioctl;
1867216Ssam 	ifp->if_output = ecoutput;
1878977Sroot 	ifp->if_reset = ecreset;
1886520Sfeldman 	for (i=0; i<16; i++)
1898773Sroot 		es->es_buf[i] = (u_char *)&umem[ui->ui_ubanum][ECMEM+2048*i];
1907216Ssam 	if_attach(ifp);
1916520Sfeldman }
1926520Sfeldman 
1936520Sfeldman /*
1946520Sfeldman  * Reset of interface after UNIBUS reset.
1956520Sfeldman  * If interface is on specified uba, reset its state.
1966520Sfeldman  */
1976520Sfeldman ecreset(unit, uban)
1986520Sfeldman 	int unit, uban;
1996520Sfeldman {
2006520Sfeldman 	register struct uba_device *ui;
2016520Sfeldman 
2026520Sfeldman 	if (unit >= NEC || (ui = ecinfo[unit]) == 0 || ui->ui_alive == 0 ||
2036520Sfeldman 	    ui->ui_ubanum != uban)
2046520Sfeldman 		return;
2056520Sfeldman 	printf(" ec%d", unit);
2068773Sroot 	(void) ubamem(uban, ECMEM, 32*2, 0);	/* mr disable (no alloc) */
207*16207Skarels 	ec_softc[unit].es_if.if_flags &= ~IFF_RUNNING;
2086520Sfeldman 	ecinit(unit);
2096520Sfeldman }
2106520Sfeldman 
2116520Sfeldman /*
2126520Sfeldman  * Initialization of interface; clear recorded pending
2136520Sfeldman  * operations, and reinitialize UNIBUS usage.
2146520Sfeldman  */
2156520Sfeldman ecinit(unit)
2166520Sfeldman 	int unit;
2176520Sfeldman {
2187216Ssam 	struct ec_softc *es = &ec_softc[unit];
2197216Ssam 	struct ecdevice *addr;
22011574Ssam 	register struct ifnet *ifp = &es->es_if;
22113086Ssam 	register struct sockaddr_in *sin;
22213055Ssam 	int i, s;
2236520Sfeldman 
22411574Ssam 	sin = (struct sockaddr_in *)&ifp->if_addr;
22513055Ssam 	if (sin->sin_addr.s_addr == 0)		/* address still unknown */
22611574Ssam 		return;
22711574Ssam 
2286520Sfeldman 	/*
2297217Sfeldman 	 * Hang receive buffers and start any pending writes.
2306637Sfeldman 	 * Writing into the rcr also makes sure the memory
2316637Sfeldman 	 * is turned on.
2326520Sfeldman 	 */
23313055Ssam 	if ((es->es_if.if_flags & IFF_RUNNING) == 0) {
23413055Ssam 		addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
23513055Ssam 		s = splimp();
23613055Ssam 		for (i = ECRHBF; i >= ECRLBF; i--)
23713055Ssam 			addr->ec_rcr = EC_READ | i;
23813055Ssam 		es->es_oactive = 0;
23913055Ssam 		es->es_mask = ~0;
24013055Ssam 		es->es_if.if_flags |= IFF_UP|IFF_RUNNING;
24113055Ssam 		if (es->es_if.if_snd.ifq_head)
24213055Ssam 			ecstart(unit);
24313055Ssam 		splx(s);
24413055Ssam 	}
2457150Swnj 	if_rtinit(&es->es_if, RTF_UP);
24611574Ssam 	arpattach(&es->es_ac);
24711574Ssam 	arpwhohas(&es->es_ac, &sin->sin_addr);
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 {
2618773Sroot         int unit = ECUNIT(dev);
2627216Ssam 	struct ec_softc *es = &ec_softc[unit];
2637216Ssam 	struct ecdevice *addr;
2646520Sfeldman 	struct mbuf *m;
2656520Sfeldman 
2666520Sfeldman 	if (es->es_oactive)
2676520Sfeldman 		goto restart;
2686520Sfeldman 
2696520Sfeldman 	IF_DEQUEUE(&es->es_if.if_snd, m);
2706520Sfeldman 	if (m == 0) {
2716520Sfeldman 		es->es_oactive = 0;
2726520Sfeldman 		return;
2736520Sfeldman 	}
2746520Sfeldman 	ecput(es->es_buf[ECTBF], m);
2756520Sfeldman 
2766520Sfeldman restart:
2777216Ssam 	addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
2786520Sfeldman 	addr->ec_xcr = EC_WRITE|ECTBF;
2796520Sfeldman 	es->es_oactive = 1;
2806520Sfeldman }
2816520Sfeldman 
2826520Sfeldman /*
2836520Sfeldman  * Ethernet interface transmitter interrupt.
2846520Sfeldman  * Start another output if more data to send.
2856520Sfeldman  */
2866520Sfeldman ecxint(unit)
2876520Sfeldman 	int unit;
2886520Sfeldman {
2896520Sfeldman 	register struct ec_softc *es = &ec_softc[unit];
2907216Ssam 	register struct ecdevice *addr =
2917216Ssam 		(struct ecdevice *)ecinfo[unit]->ui_addr;
2926520Sfeldman 
2936520Sfeldman 	if (es->es_oactive == 0)
2946520Sfeldman 		return;
2957216Ssam 	if ((addr->ec_xcr&EC_XDONE) == 0 || (addr->ec_xcr&EC_XBN) != ECTBF) {
2967216Ssam 		printf("ec%d: stray xmit interrupt, xcr=%b\n", unit,
2977216Ssam 			addr->ec_xcr, EC_XBITS);
2987216Ssam 		es->es_oactive = 0;
2997216Ssam 		addr->ec_xcr = EC_XCLR;
3007216Ssam 		return;
3017216Ssam 	}
3026520Sfeldman 	es->es_if.if_opackets++;
3036520Sfeldman 	es->es_oactive = 0;
3046520Sfeldman 	es->es_mask = ~0;
3056520Sfeldman 	addr->ec_xcr = EC_XCLR;
3067216Ssam 	if (es->es_if.if_snd.ifq_head)
3077216Ssam 		ecstart(unit);
3086520Sfeldman }
3096520Sfeldman 
3106520Sfeldman /*
3116520Sfeldman  * Collision on ethernet interface.  Do exponential
3126520Sfeldman  * backoff, and retransmit.  If have backed off all
3136520Sfeldman  * the way print warning diagnostic, and drop packet.
3146520Sfeldman  */
3156520Sfeldman eccollide(unit)
3166520Sfeldman 	int unit;
3176520Sfeldman {
3186520Sfeldman 	struct ec_softc *es = &ec_softc[unit];
3196520Sfeldman 
3206520Sfeldman 	es->es_if.if_collisions++;
3217216Ssam 	if (es->es_oactive)
3227216Ssam 		ecdocoll(unit);
3236520Sfeldman }
3246520Sfeldman 
3256520Sfeldman ecdocoll(unit)
3266520Sfeldman 	int unit;
3276520Sfeldman {
3286520Sfeldman 	register struct ec_softc *es = &ec_softc[unit];
3296545Sfeldman 	register struct ecdevice *addr =
3306545Sfeldman 	    (struct ecdevice *)ecinfo[unit]->ui_addr;
3316545Sfeldman 	register i;
3326545Sfeldman 	int delay;
3336520Sfeldman 
3346520Sfeldman 	/*
3356520Sfeldman 	 * Es_mask is a 16 bit number with n low zero bits, with
3366520Sfeldman 	 * n the number of backoffs.  When es_mask is 0 we have
3376520Sfeldman 	 * backed off 16 times, and give up.
3386520Sfeldman 	 */
3396520Sfeldman 	if (es->es_mask == 0) {
3406545Sfeldman 		es->es_if.if_oerrors++;
3416520Sfeldman 		printf("ec%d: send error\n", unit);
3426520Sfeldman 		/*
3436545Sfeldman 		 * Reset interface, then requeue rcv buffers.
3446545Sfeldman 		 * Some incoming packets may be lost, but that
3456545Sfeldman 		 * can't be helped.
3466520Sfeldman 		 */
3476545Sfeldman 		addr->ec_xcr = EC_UECLR;
3486545Sfeldman 		for (i=ECRHBF; i>=ECRLBF; i--)
3496545Sfeldman 			addr->ec_rcr = EC_READ|i;
3506545Sfeldman 		/*
3516545Sfeldman 		 * Reset and transmit next packet (if any).
3526545Sfeldman 		 */
3536545Sfeldman 		es->es_oactive = 0;
3546545Sfeldman 		es->es_mask = ~0;
3556545Sfeldman 		if (es->es_if.if_snd.ifq_head)
3566545Sfeldman 			ecstart(unit);
3576520Sfeldman 		return;
3586520Sfeldman 	}
3596520Sfeldman 	/*
3606545Sfeldman 	 * Do exponential backoff.  Compute delay based on low bits
3616545Sfeldman 	 * of the interval timer.  Then delay for that number of
3626545Sfeldman 	 * slot times.  A slot time is 51.2 microseconds (rounded to 51).
3636545Sfeldman 	 * This does not take into account the time already used to
3646545Sfeldman 	 * process the interrupt.
3656520Sfeldman 	 */
3666520Sfeldman 	es->es_mask <<= 1;
3676545Sfeldman 	delay = mfpr(ICR) &~ es->es_mask;
3686545Sfeldman 	DELAY(delay * 51);
3696520Sfeldman 	/*
3706545Sfeldman 	 * Clear the controller's collision flag, thus enabling retransmit.
3716520Sfeldman 	 */
3727470Sfeldman 	addr->ec_xcr = EC_CLEAR;
3736520Sfeldman }
3746520Sfeldman 
3756520Sfeldman /*
3766520Sfeldman  * Ethernet interface receiver interrupt.
3776520Sfeldman  * If input error just drop packet.
3786520Sfeldman  * Otherwise purge input buffered data path and examine
3796520Sfeldman  * packet to determine type.  If can't determine length
3806520Sfeldman  * from type, then have to drop packet.  Othewise decapsulate
3816520Sfeldman  * packet based on type and pass to type specific higher-level
3826520Sfeldman  * input routine.
3836520Sfeldman  */
3846520Sfeldman ecrint(unit)
3856520Sfeldman 	int unit;
3866520Sfeldman {
3876520Sfeldman 	struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
3886520Sfeldman 
3896520Sfeldman 	while (addr->ec_rcr & EC_RDONE)
3906520Sfeldman 		ecread(unit);
3916520Sfeldman }
3926520Sfeldman 
3936520Sfeldman ecread(unit)
3946520Sfeldman 	int unit;
3956520Sfeldman {
3966520Sfeldman 	register struct ec_softc *es = &ec_softc[unit];
3976520Sfeldman 	struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
3989745Ssam 	register struct ether_header *ec;
3996520Sfeldman     	struct mbuf *m;
4008773Sroot 	int len, off, resid, ecoff, rbuf;
4016520Sfeldman 	register struct ifqueue *inq;
4028773Sroot 	u_char *ecbuf;
4036520Sfeldman 
4046520Sfeldman 	es->es_if.if_ipackets++;
4058773Sroot 	rbuf = addr->ec_rcr & EC_RBN;
4068773Sroot 	if (rbuf < ECRLBF || rbuf > ECRHBF)
4076520Sfeldman 		panic("ecrint");
4088773Sroot 	ecbuf = es->es_buf[rbuf];
4096520Sfeldman 	ecoff = *(short *)ecbuf;
4106545Sfeldman 	if (ecoff <= ECRDOFF || ecoff > 2046) {
4116520Sfeldman 		es->es_if.if_ierrors++;
4126520Sfeldman #ifdef notdef
4136520Sfeldman 		if (es->es_if.if_ierrors % 100 == 0)
4146520Sfeldman 			printf("ec%d: += 100 input errors\n", unit);
4156520Sfeldman #endif
4166520Sfeldman 		goto setup;
4176520Sfeldman 	}
4186520Sfeldman 
4196520Sfeldman 	/*
4206520Sfeldman 	 * Get input data length.
4216520Sfeldman 	 * Get pointer to ethernet header (in input buffer).
4226520Sfeldman 	 * Deal with trailer protocol: if type is PUP trailer
4236520Sfeldman 	 * get true type from first 16-bit word past data.
4246520Sfeldman 	 * Remember that type was trailer by setting off.
4256520Sfeldman 	 */
4269745Ssam 	len = ecoff - ECRDOFF - sizeof (struct ether_header);
4279745Ssam 	ec = (struct ether_header *)(ecbuf + ECRDOFF);
4289745Ssam 	ec->ether_type = ntohs((u_short)ec->ether_type);
4296520Sfeldman #define	ecdataaddr(ec, off, type)	((type)(((caddr_t)((ec)+1)+(off))))
4309745Ssam 	if (ec->ether_type >= ETHERPUP_TRAIL &&
4319745Ssam 	    ec->ether_type < ETHERPUP_TRAIL+ETHERPUP_NTRAILER) {
4329745Ssam 		off = (ec->ether_type - ETHERPUP_TRAIL) * 512;
4339745Ssam 		if (off >= ETHERMTU)
4346520Sfeldman 			goto setup;		/* sanity */
4359745Ssam 		ec->ether_type = ntohs(*ecdataaddr(ec, off, u_short *));
4369745Ssam 		resid = ntohs(*(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 	}
4589745Ssam 	switch (ec->ether_type) {
4596520Sfeldman 
4606520Sfeldman #ifdef INET
4619745Ssam 	case ETHERPUP_IPTYPE:
4626520Sfeldman 		schednetisr(NETISR_IP);
4636520Sfeldman 		inq = &ipintrq;
4646520Sfeldman 		break;
46511574Ssam 
46611574Ssam 	case ETHERPUP_ARPTYPE:
46711574Ssam 		arpinput(&es->es_ac, m);
46813987Ssam 		goto setup;
4696520Sfeldman #endif
4706520Sfeldman 	default:
4716520Sfeldman 		m_freem(m);
4726520Sfeldman 		goto setup;
4736520Sfeldman 	}
4746520Sfeldman 
4756520Sfeldman 	if (IF_QFULL(inq)) {
4766520Sfeldman 		IF_DROP(inq);
4776520Sfeldman 		m_freem(m);
4787216Ssam 		goto setup;
4797216Ssam 	}
4807216Ssam 	IF_ENQUEUE(inq, m);
4816520Sfeldman 
4826520Sfeldman setup:
4836520Sfeldman 	/*
4846520Sfeldman 	 * Reset for next packet.
4856520Sfeldman 	 */
4868773Sroot 	addr->ec_rcr = EC_READ|EC_RCLR|rbuf;
4876520Sfeldman }
4886520Sfeldman 
4896520Sfeldman /*
4906520Sfeldman  * Ethernet output routine.
4916520Sfeldman  * Encapsulate a packet of type family for the local net.
4926520Sfeldman  * Use trailer local net encapsulation if enough data in first
4936520Sfeldman  * packet leaves a multiple of 512 bytes of data in remainder.
4946545Sfeldman  * If destination is this address or broadcast, send packet to
4956545Sfeldman  * loop device to kludge around the fact that 3com interfaces can't
4966545Sfeldman  * talk to themselves.
4976520Sfeldman  */
4986520Sfeldman ecoutput(ifp, m0, dst)
4996520Sfeldman 	struct ifnet *ifp;
5006520Sfeldman 	struct mbuf *m0;
5016520Sfeldman 	struct sockaddr *dst;
5026520Sfeldman {
50311574Ssam 	int type, s, error;
50411574Ssam 	u_char edst[6];
50511574Ssam 	struct in_addr idst;
5066520Sfeldman 	register struct ec_softc *es = &ec_softc[ifp->if_unit];
5076520Sfeldman 	register struct mbuf *m = m0;
5089745Ssam 	register struct ether_header *ec;
50912771Ssam 	register int off;
51011574Ssam 	struct mbuf *mcopy = (struct mbuf *)0;
5116520Sfeldman 
5126520Sfeldman 	switch (dst->sa_family) {
5136520Sfeldman 
5146520Sfeldman #ifdef INET
5156520Sfeldman 	case AF_INET:
51611574Ssam 		idst = ((struct sockaddr_in *)dst)->sin_addr;
51711574Ssam 		if (!arpresolve(&es->es_ac, m, &idst, edst))
51811574Ssam 			return (0);	/* if not yet resolved */
51911574Ssam 		if (in_lnaof(idst) == INADDR_ANY)
5208838Sroot 			mcopy = m_copy(m, 0, (int)M_COPYALL);
5216520Sfeldman 		off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len;
52213055Ssam 		/* need per host negotiation */
52313055Ssam 		if ((ifp->if_flags & IFF_NOTRAILERS) == 0)
5246520Sfeldman 		if (off > 0 && (off & 0x1ff) == 0 &&
5256520Sfeldman 		    m->m_off >= MMINOFF + 2 * sizeof (u_short)) {
5269745Ssam 			type = ETHERPUP_TRAIL + (off>>9);
5276520Sfeldman 			m->m_off -= 2 * sizeof (u_short);
5286520Sfeldman 			m->m_len += 2 * sizeof (u_short);
5299745Ssam 			*mtod(m, u_short *) = ntohs((u_short)ETHERPUP_IPTYPE);
5309745Ssam 			*(mtod(m, u_short *) + 1) = ntohs((u_short)m->m_len);
5316520Sfeldman 			goto gottrailertype;
5326520Sfeldman 		}
5339745Ssam 		type = ETHERPUP_IPTYPE;
5346520Sfeldman 		off = 0;
5356520Sfeldman 		goto gottype;
5366520Sfeldman #endif
5376520Sfeldman 
53811574Ssam 	case AF_UNSPEC:
53911574Ssam 		ec = (struct ether_header *)dst->sa_data;
54012771Ssam 		bcopy((caddr_t)ec->ether_dhost, (caddr_t)edst, sizeof (edst));
54111574Ssam 		type = ec->ether_type;
54211574Ssam 		goto gottype;
54311574Ssam 
5446520Sfeldman 	default:
5456520Sfeldman 		printf("ec%d: can't handle af%d\n", ifp->if_unit,
5466520Sfeldman 			dst->sa_family);
5476520Sfeldman 		error = EAFNOSUPPORT;
5486520Sfeldman 		goto bad;
5496520Sfeldman 	}
5506520Sfeldman 
5516520Sfeldman gottrailertype:
5526520Sfeldman 	/*
5536520Sfeldman 	 * Packet to be sent as trailer: move first packet
5546520Sfeldman 	 * (control information) to end of chain.
5556520Sfeldman 	 */
5566520Sfeldman 	while (m->m_next)
5576520Sfeldman 		m = m->m_next;
5586520Sfeldman 	m->m_next = m0;
5596520Sfeldman 	m = m0->m_next;
5606520Sfeldman 	m0->m_next = 0;
5616520Sfeldman 	m0 = m;
5626520Sfeldman 
5636520Sfeldman gottype:
5646520Sfeldman 	/*
5656520Sfeldman 	 * Add local net header.  If no space in first mbuf,
5666520Sfeldman 	 * allocate another.
5676520Sfeldman 	 */
5686520Sfeldman 	if (m->m_off > MMAXOFF ||
5699745Ssam 	    MMINOFF + sizeof (struct ether_header) > m->m_off) {
5709648Ssam 		m = m_get(M_DONTWAIT, MT_HEADER);
5716520Sfeldman 		if (m == 0) {
5726520Sfeldman 			error = ENOBUFS;
5736520Sfeldman 			goto bad;
5746520Sfeldman 		}
5756520Sfeldman 		m->m_next = m0;
5766520Sfeldman 		m->m_off = MMINOFF;
5779745Ssam 		m->m_len = sizeof (struct ether_header);
5786520Sfeldman 	} else {
5799745Ssam 		m->m_off -= sizeof (struct ether_header);
5809745Ssam 		m->m_len += sizeof (struct ether_header);
5816520Sfeldman 	}
5829745Ssam 	ec = mtod(m, struct ether_header *);
58312771Ssam 	bcopy((caddr_t)edst, (caddr_t)ec->ether_dhost, sizeof (edst));
5849745Ssam 	ec->ether_type = htons((u_short)type);
58511574Ssam 	bcopy((caddr_t)es->es_addr, (caddr_t)ec->ether_shost, 6);
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);
60112771Ssam 	return (mcopy ? looutput(&loif, mcopy, dst) : 0);
6027216Ssam 
6036520Sfeldman qfull:
6046520Sfeldman 	m0 = m;
6056520Sfeldman 	splx(s);
6066520Sfeldman bad:
6076520Sfeldman 	m_freem(m0);
608*16207Skarels 	if (mcopy)
609*16207Skarels 		m_freem(mcopy);
61012771Ssam 	return (error);
6116520Sfeldman }
6126520Sfeldman 
6136520Sfeldman /*
6149177Ssam  * Routine to copy from mbuf chain to transmit
6157216Ssam  * buffer in UNIBUS memory.
6169177Ssam  * If packet size is less than the minimum legal size,
6179177Ssam  * the buffer is expanded.  We probably should zero out the extra
6189177Ssam  * bytes for security, but that would slow things down.
6196520Sfeldman  */
6206520Sfeldman ecput(ecbuf, m)
6217216Ssam 	u_char *ecbuf;
6226520Sfeldman 	struct mbuf *m;
6236520Sfeldman {
6246520Sfeldman 	register struct mbuf *mp;
6257216Ssam 	register int off;
6267263Ssam 	u_char *bp;
6276520Sfeldman 
6287216Ssam 	for (off = 2048, mp = m; mp; mp = mp->m_next)
6297216Ssam 		off -= mp->m_len;
6309745Ssam 	if (2048 - off < ETHERMIN + sizeof (struct ether_header))
6319745Ssam 		off = 2048 - ETHERMIN - sizeof (struct ether_header);
6327216Ssam 	*(u_short *)ecbuf = off;
6337216Ssam 	bp = (u_char *)(ecbuf + off);
6347263Ssam 	for (mp = m; mp; mp = mp->m_next) {
6357263Ssam 		register unsigned len = mp->m_len;
6367263Ssam 		u_char *mcp;
6377216Ssam 
6387216Ssam 		if (len == 0)
6397216Ssam 			continue;
6407216Ssam 		mcp = mtod(mp, u_char *);
6417216Ssam 		if ((unsigned)bp & 01) {
6427032Swnj 			*bp++ = *mcp++;
6437216Ssam 			len--;
6447032Swnj 		}
6457263Ssam 		if (off = (len >> 1)) {
6467263Ssam 			register u_short *to, *from;
6477263Ssam 
6487263Ssam 			to = (u_short *)bp;
6497263Ssam 			from = (u_short *)mcp;
6507263Ssam 			do
6517263Ssam 				*to++ = *from++;
6527263Ssam 			while (--off > 0);
6537263Ssam 			bp = (u_char *)to,
6547263Ssam 			mcp = (u_char *)from;
6557032Swnj 		}
6567263Ssam 		if (len & 01)
6576520Sfeldman 			*bp++ = *mcp++;
6586520Sfeldman 	}
6597263Ssam 	m_freem(m);
6606520Sfeldman }
6616520Sfeldman 
6626520Sfeldman /*
6636520Sfeldman  * Routine to copy from UNIBUS memory into mbufs.
6646520Sfeldman  * Similar in spirit to if_rubaget.
6657032Swnj  *
6667032Swnj  * Warning: This makes the fairly safe assumption that
6677032Swnj  * mbufs have even lengths.
6686520Sfeldman  */
6696520Sfeldman struct mbuf *
6706520Sfeldman ecget(ecbuf, totlen, off0)
6717263Ssam 	u_char *ecbuf;
6726520Sfeldman 	int totlen, off0;
6736520Sfeldman {
6747263Ssam 	register struct mbuf *m;
6757263Ssam 	struct mbuf *top = 0, **mp = &top;
6767263Ssam 	register int off = off0, len;
6777263Ssam 	u_char *cp;
6786520Sfeldman 
6799745Ssam 	cp = ecbuf + ECRDOFF + sizeof (struct ether_header);
6806520Sfeldman 	while (totlen > 0) {
6817263Ssam 		register int words;
6827263Ssam 		u_char *mcp;
6837263Ssam 
6849648Ssam 		MGET(m, M_DONTWAIT, MT_DATA);
6856520Sfeldman 		if (m == 0)
6866520Sfeldman 			goto bad;
6876520Sfeldman 		if (off) {
6886520Sfeldman 			len = totlen - off;
6899745Ssam 			cp = ecbuf + ECRDOFF +
6909745Ssam 				sizeof (struct ether_header) + off;
6916520Sfeldman 		} else
6926520Sfeldman 			len = totlen;
6936520Sfeldman 		if (len >= CLBYTES) {
6946520Sfeldman 			struct mbuf *p;
6956520Sfeldman 
6966520Sfeldman 			MCLGET(p, 1);
6976520Sfeldman 			if (p != 0) {
6986520Sfeldman 				m->m_len = len = CLBYTES;
6996520Sfeldman 				m->m_off = (int)p - (int)m;
7006520Sfeldman 			} else {
7016520Sfeldman 				m->m_len = len = MIN(MLEN, len);
7026520Sfeldman 				m->m_off = MMINOFF;
7036520Sfeldman 			}
7046520Sfeldman 		} else {
7056520Sfeldman 			m->m_len = len = MIN(MLEN, len);
7066520Sfeldman 			m->m_off = MMINOFF;
7076520Sfeldman 		}
7087263Ssam 		mcp = mtod(m, u_char *);
7097263Ssam 		if (words = (len >> 1)) {
7107263Ssam 			register u_short *to, *from;
7117263Ssam 
7127263Ssam 			to = (u_short *)mcp;
7137263Ssam 			from = (u_short *)cp;
7147263Ssam 			do
7157263Ssam 				*to++ = *from++;
7167263Ssam 			while (--words > 0);
7177263Ssam 			mcp = (u_char *)to;
7187263Ssam 			cp = (u_char *)from;
7197032Swnj 		}
7207216Ssam 		if (len & 01)
7216520Sfeldman 			*mcp++ = *cp++;
7226520Sfeldman 		*mp = m;
7236520Sfeldman 		mp = &m->m_next;
7247263Ssam 		if (off == 0) {
7256520Sfeldman 			totlen -= len;
7267263Ssam 			continue;
7277263Ssam 		}
7287263Ssam 		off += len;
7297263Ssam 		if (off == totlen) {
7309745Ssam 			cp = ecbuf + ECRDOFF + sizeof (struct ether_header);
7317263Ssam 			off = 0;
7327263Ssam 			totlen = off0;
7337263Ssam 		}
7346520Sfeldman 	}
7356520Sfeldman 	return (top);
7366520Sfeldman bad:
7376520Sfeldman 	m_freem(top);
7386520Sfeldman 	return (0);
7396520Sfeldman }
74013055Ssam 
74113055Ssam /*
74213055Ssam  * Process an ioctl request.
74313055Ssam  */
74413055Ssam ecioctl(ifp, cmd, data)
74513055Ssam 	register struct ifnet *ifp;
74613055Ssam 	int cmd;
74713055Ssam 	caddr_t data;
74813055Ssam {
74913055Ssam 	register struct ifreq *ifr = (struct ifreq *)data;
75013055Ssam 	int s = splimp(), error = 0;
75113055Ssam 
75213055Ssam 	switch (cmd) {
75313055Ssam 
75413055Ssam 	case SIOCSIFADDR:
75513055Ssam 		if (ifp->if_flags & IFF_RUNNING)
75613055Ssam 			if_rtinit(ifp, -1);	/* delete previous route */
75713062Ssam 		ecsetaddr(ifp, (struct sockaddr_in *)&ifr->ifr_addr);
75813055Ssam 		ecinit(ifp->if_unit);
75913055Ssam 		break;
76013055Ssam 
76113055Ssam 	default:
76213055Ssam 		error = EINVAL;
76313055Ssam 	}
76413055Ssam 	splx(s);
76513055Ssam 	return (error);
76613055Ssam }
76713062Ssam 
76813062Ssam ecsetaddr(ifp, sin)
76913062Ssam 	register struct ifnet *ifp;
77013062Ssam 	register struct sockaddr_in *sin;
77113062Ssam {
77213062Ssam 
77313062Ssam 	ifp->if_addr = *(struct sockaddr *)sin;
77413062Ssam 	ifp->if_net = in_netof(sin->sin_addr);
77513062Ssam 	ifp->if_host[0] = in_lnaof(sin->sin_addr);
77613062Ssam 	sin = (struct sockaddr_in *)&ifp->if_broadaddr;
77713062Ssam 	sin->sin_family = AF_INET;
77813062Ssam 	sin->sin_addr = if_makeaddr(ifp->if_net, INADDR_ANY);
77913062Ssam 	ifp->if_flags |= IFF_BROADCAST;
78013062Ssam }
781