xref: /csrg-svn/sys/vax/if/if_ec.c (revision 13055)
1*13055Ssam /*	if_ec.c	4.35	83/06/12	*/
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"
17*13055Ssam #include "../h/ioctl.h"
18*13055Ssam #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 
46*13055Ssam 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;
185*13055Ssam 	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) */
2076520Sfeldman 	ecinit(unit);
2086520Sfeldman }
2096520Sfeldman 
2106520Sfeldman /*
2116520Sfeldman  * Initialization of interface; clear recorded pending
2126520Sfeldman  * operations, and reinitialize UNIBUS usage.
2136520Sfeldman  */
2146520Sfeldman ecinit(unit)
2156520Sfeldman 	int unit;
2166520Sfeldman {
2177216Ssam 	struct ec_softc *es = &ec_softc[unit];
2187216Ssam 	struct ecdevice *addr;
21911574Ssam 	register struct ifnet *ifp = &es->es_if;
22011574Ssam 	register struct sockaddr_in *sin, *sinb;
221*13055Ssam 	int i, s;
2226520Sfeldman 
22311574Ssam 	sin = (struct sockaddr_in *)&ifp->if_addr;
224*13055Ssam 	if (sin->sin_addr.s_addr == 0)		/* address still unknown */
22511574Ssam 		return;
22611574Ssam 
2276520Sfeldman 	/*
2287217Sfeldman 	 * Hang receive buffers and start any pending writes.
2296637Sfeldman 	 * Writing into the rcr also makes sure the memory
2306637Sfeldman 	 * is turned on.
2316520Sfeldman 	 */
232*13055Ssam 	if ((es->es_if.if_flags & IFF_RUNNING) == 0) {
233*13055Ssam 		addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
234*13055Ssam 		s = splimp();
235*13055Ssam 		for (i = ECRHBF; i >= ECRLBF; i--)
236*13055Ssam 			addr->ec_rcr = EC_READ | i;
237*13055Ssam 		es->es_oactive = 0;
238*13055Ssam 		es->es_mask = ~0;
239*13055Ssam 		es->es_if.if_flags |= IFF_UP|IFF_RUNNING;
240*13055Ssam 		if (es->es_if.if_snd.ifq_head)
241*13055Ssam 			ecstart(unit);
242*13055Ssam 		splx(s);
243*13055Ssam 	}
2447150Swnj 	if_rtinit(&es->es_if, RTF_UP);
24511574Ssam 	arpattach(&es->es_ac);
24611574Ssam 	arpwhohas(&es->es_ac, &sin->sin_addr);
2476520Sfeldman }
2486520Sfeldman 
2496520Sfeldman /*
2506520Sfeldman  * Start or restart output on interface.
2516520Sfeldman  * If interface is already active, then this is a retransmit
2526545Sfeldman  * after a collision, and just restuff registers.
2536520Sfeldman  * If interface is not already active, get another datagram
2546520Sfeldman  * to send off of the interface queue, and map it to the interface
2556520Sfeldman  * before starting the output.
2566520Sfeldman  */
2576520Sfeldman ecstart(dev)
2586520Sfeldman 	dev_t dev;
2596520Sfeldman {
2608773Sroot         int unit = ECUNIT(dev);
2617216Ssam 	struct ec_softc *es = &ec_softc[unit];
2627216Ssam 	struct ecdevice *addr;
2636520Sfeldman 	struct mbuf *m;
2646520Sfeldman 
2656520Sfeldman 	if (es->es_oactive)
2666520Sfeldman 		goto restart;
2676520Sfeldman 
2686520Sfeldman 	IF_DEQUEUE(&es->es_if.if_snd, m);
2696520Sfeldman 	if (m == 0) {
2706520Sfeldman 		es->es_oactive = 0;
2716520Sfeldman 		return;
2726520Sfeldman 	}
2736520Sfeldman 	ecput(es->es_buf[ECTBF], m);
2746520Sfeldman 
2756520Sfeldman restart:
2767216Ssam 	addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
2776520Sfeldman 	addr->ec_xcr = EC_WRITE|ECTBF;
2786520Sfeldman 	es->es_oactive = 1;
2796520Sfeldman }
2806520Sfeldman 
2816520Sfeldman /*
2826520Sfeldman  * Ethernet interface transmitter interrupt.
2836520Sfeldman  * Start another output if more data to send.
2846520Sfeldman  */
2856520Sfeldman ecxint(unit)
2866520Sfeldman 	int unit;
2876520Sfeldman {
2886520Sfeldman 	register struct ec_softc *es = &ec_softc[unit];
2897216Ssam 	register struct ecdevice *addr =
2907216Ssam 		(struct ecdevice *)ecinfo[unit]->ui_addr;
2916520Sfeldman 
2926520Sfeldman 	if (es->es_oactive == 0)
2936520Sfeldman 		return;
2947216Ssam 	if ((addr->ec_xcr&EC_XDONE) == 0 || (addr->ec_xcr&EC_XBN) != ECTBF) {
2957216Ssam 		printf("ec%d: stray xmit interrupt, xcr=%b\n", unit,
2967216Ssam 			addr->ec_xcr, EC_XBITS);
2977216Ssam 		es->es_oactive = 0;
2987216Ssam 		addr->ec_xcr = EC_XCLR;
2997216Ssam 		return;
3007216Ssam 	}
3016520Sfeldman 	es->es_if.if_opackets++;
3026520Sfeldman 	es->es_oactive = 0;
3036520Sfeldman 	es->es_mask = ~0;
3046520Sfeldman 	addr->ec_xcr = EC_XCLR;
3057216Ssam 	if (es->es_if.if_snd.ifq_head)
3067216Ssam 		ecstart(unit);
3076520Sfeldman }
3086520Sfeldman 
3096520Sfeldman /*
3106520Sfeldman  * Collision on ethernet interface.  Do exponential
3116520Sfeldman  * backoff, and retransmit.  If have backed off all
3126520Sfeldman  * the way print warning diagnostic, and drop packet.
3136520Sfeldman  */
3146520Sfeldman eccollide(unit)
3156520Sfeldman 	int unit;
3166520Sfeldman {
3176520Sfeldman 	struct ec_softc *es = &ec_softc[unit];
3186520Sfeldman 
3196520Sfeldman 	es->es_if.if_collisions++;
3207216Ssam 	if (es->es_oactive)
3217216Ssam 		ecdocoll(unit);
3226520Sfeldman }
3236520Sfeldman 
3246520Sfeldman ecdocoll(unit)
3256520Sfeldman 	int unit;
3266520Sfeldman {
3276520Sfeldman 	register struct ec_softc *es = &ec_softc[unit];
3286545Sfeldman 	register struct ecdevice *addr =
3296545Sfeldman 	    (struct ecdevice *)ecinfo[unit]->ui_addr;
3306545Sfeldman 	register i;
3316545Sfeldman 	int delay;
3326520Sfeldman 
3336520Sfeldman 	/*
3346520Sfeldman 	 * Es_mask is a 16 bit number with n low zero bits, with
3356520Sfeldman 	 * n the number of backoffs.  When es_mask is 0 we have
3366520Sfeldman 	 * backed off 16 times, and give up.
3376520Sfeldman 	 */
3386520Sfeldman 	if (es->es_mask == 0) {
3396545Sfeldman 		es->es_if.if_oerrors++;
3406520Sfeldman 		printf("ec%d: send error\n", unit);
3416520Sfeldman 		/*
3426545Sfeldman 		 * Reset interface, then requeue rcv buffers.
3436545Sfeldman 		 * Some incoming packets may be lost, but that
3446545Sfeldman 		 * can't be helped.
3456520Sfeldman 		 */
3466545Sfeldman 		addr->ec_xcr = EC_UECLR;
3476545Sfeldman 		for (i=ECRHBF; i>=ECRLBF; i--)
3486545Sfeldman 			addr->ec_rcr = EC_READ|i;
3496545Sfeldman 		/*
3506545Sfeldman 		 * Reset and transmit next packet (if any).
3516545Sfeldman 		 */
3526545Sfeldman 		es->es_oactive = 0;
3536545Sfeldman 		es->es_mask = ~0;
3546545Sfeldman 		if (es->es_if.if_snd.ifq_head)
3556545Sfeldman 			ecstart(unit);
3566520Sfeldman 		return;
3576520Sfeldman 	}
3586520Sfeldman 	/*
3596545Sfeldman 	 * Do exponential backoff.  Compute delay based on low bits
3606545Sfeldman 	 * of the interval timer.  Then delay for that number of
3616545Sfeldman 	 * slot times.  A slot time is 51.2 microseconds (rounded to 51).
3626545Sfeldman 	 * This does not take into account the time already used to
3636545Sfeldman 	 * process the interrupt.
3646520Sfeldman 	 */
3656520Sfeldman 	es->es_mask <<= 1;
3666545Sfeldman 	delay = mfpr(ICR) &~ es->es_mask;
3676545Sfeldman 	DELAY(delay * 51);
3686520Sfeldman 	/*
3696545Sfeldman 	 * Clear the controller's collision flag, thus enabling retransmit.
3706520Sfeldman 	 */
3717470Sfeldman 	addr->ec_xcr = EC_CLEAR;
3726520Sfeldman }
3736520Sfeldman 
3746520Sfeldman /*
3756520Sfeldman  * Ethernet interface receiver interrupt.
3766520Sfeldman  * If input error just drop packet.
3776520Sfeldman  * Otherwise purge input buffered data path and examine
3786520Sfeldman  * packet to determine type.  If can't determine length
3796520Sfeldman  * from type, then have to drop packet.  Othewise decapsulate
3806520Sfeldman  * packet based on type and pass to type specific higher-level
3816520Sfeldman  * input routine.
3826520Sfeldman  */
3836520Sfeldman ecrint(unit)
3846520Sfeldman 	int unit;
3856520Sfeldman {
3866520Sfeldman 	struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
3876520Sfeldman 
3886520Sfeldman 	while (addr->ec_rcr & EC_RDONE)
3896520Sfeldman 		ecread(unit);
3906520Sfeldman }
3916520Sfeldman 
3926520Sfeldman ecread(unit)
3936520Sfeldman 	int unit;
3946520Sfeldman {
3956520Sfeldman 	register struct ec_softc *es = &ec_softc[unit];
3966520Sfeldman 	struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
3979745Ssam 	register struct ether_header *ec;
3986520Sfeldman     	struct mbuf *m;
3998773Sroot 	int len, off, resid, ecoff, rbuf;
4006520Sfeldman 	register struct ifqueue *inq;
4018773Sroot 	u_char *ecbuf;
4026520Sfeldman 
4036520Sfeldman 	es->es_if.if_ipackets++;
4048773Sroot 	rbuf = addr->ec_rcr & EC_RBN;
4058773Sroot 	if (rbuf < ECRLBF || rbuf > ECRHBF)
4066520Sfeldman 		panic("ecrint");
4078773Sroot 	ecbuf = es->es_buf[rbuf];
4086520Sfeldman 	ecoff = *(short *)ecbuf;
4096545Sfeldman 	if (ecoff <= ECRDOFF || ecoff > 2046) {
4106520Sfeldman 		es->es_if.if_ierrors++;
4116520Sfeldman #ifdef notdef
4126520Sfeldman 		if (es->es_if.if_ierrors % 100 == 0)
4136520Sfeldman 			printf("ec%d: += 100 input errors\n", unit);
4146520Sfeldman #endif
4156520Sfeldman 		goto setup;
4166520Sfeldman 	}
4176520Sfeldman 
4186520Sfeldman 	/*
4196520Sfeldman 	 * Get input data length.
4206520Sfeldman 	 * Get pointer to ethernet header (in input buffer).
4216520Sfeldman 	 * Deal with trailer protocol: if type is PUP trailer
4226520Sfeldman 	 * get true type from first 16-bit word past data.
4236520Sfeldman 	 * Remember that type was trailer by setting off.
4246520Sfeldman 	 */
4259745Ssam 	len = ecoff - ECRDOFF - sizeof (struct ether_header);
4269745Ssam 	ec = (struct ether_header *)(ecbuf + ECRDOFF);
4279745Ssam 	ec->ether_type = ntohs((u_short)ec->ether_type);
4286520Sfeldman #define	ecdataaddr(ec, off, type)	((type)(((caddr_t)((ec)+1)+(off))))
4299745Ssam 	if (ec->ether_type >= ETHERPUP_TRAIL &&
4309745Ssam 	    ec->ether_type < ETHERPUP_TRAIL+ETHERPUP_NTRAILER) {
4319745Ssam 		off = (ec->ether_type - ETHERPUP_TRAIL) * 512;
4329745Ssam 		if (off >= ETHERMTU)
4336520Sfeldman 			goto setup;		/* sanity */
4349745Ssam 		ec->ether_type = ntohs(*ecdataaddr(ec, off, u_short *));
4359745Ssam 		resid = ntohs(*(ecdataaddr(ec, off+2, u_short *)));
4366520Sfeldman 		if (off + resid > len)
4376520Sfeldman 			goto setup;		/* sanity */
4386520Sfeldman 		len = off + resid;
4396520Sfeldman 	} else
4406520Sfeldman 		off = 0;
4416520Sfeldman 	if (len == 0)
4426520Sfeldman 		goto setup;
4436520Sfeldman 
4446520Sfeldman 	/*
4456520Sfeldman 	 * Pull packet off interface.  Off is nonzero if packet
4466520Sfeldman 	 * has trailing header; ecget will then force this header
4476520Sfeldman 	 * information to be at the front, but we still have to drop
4486520Sfeldman 	 * the type and length which are at the front of any trailer data.
4496520Sfeldman 	 */
4506520Sfeldman 	m = ecget(ecbuf, len, off);
4516520Sfeldman 	if (m == 0)
4526520Sfeldman 		goto setup;
4536520Sfeldman 	if (off) {
4546520Sfeldman 		m->m_off += 2 * sizeof (u_short);
4556520Sfeldman 		m->m_len -= 2 * sizeof (u_short);
4566520Sfeldman 	}
4579745Ssam 	switch (ec->ether_type) {
4586520Sfeldman 
4596520Sfeldman #ifdef INET
4609745Ssam 	case ETHERPUP_IPTYPE:
4616520Sfeldman 		schednetisr(NETISR_IP);
4626520Sfeldman 		inq = &ipintrq;
4636520Sfeldman 		break;
46411574Ssam 
46511574Ssam 	case ETHERPUP_ARPTYPE:
46611574Ssam 		arpinput(&es->es_ac, m);
46711574Ssam 		return;
4686520Sfeldman #endif
4696520Sfeldman 	default:
4706520Sfeldman 		m_freem(m);
4716520Sfeldman 		goto setup;
4726520Sfeldman 	}
4736520Sfeldman 
4746520Sfeldman 	if (IF_QFULL(inq)) {
4756520Sfeldman 		IF_DROP(inq);
4766520Sfeldman 		m_freem(m);
4777216Ssam 		goto setup;
4787216Ssam 	}
4797216Ssam 	IF_ENQUEUE(inq, m);
4806520Sfeldman 
4816520Sfeldman setup:
4826520Sfeldman 	/*
4836520Sfeldman 	 * Reset for next packet.
4846520Sfeldman 	 */
4858773Sroot 	addr->ec_rcr = EC_READ|EC_RCLR|rbuf;
4866520Sfeldman }
4876520Sfeldman 
4886520Sfeldman /*
4896520Sfeldman  * Ethernet output routine.
4906520Sfeldman  * Encapsulate a packet of type family for the local net.
4916520Sfeldman  * Use trailer local net encapsulation if enough data in first
4926520Sfeldman  * packet leaves a multiple of 512 bytes of data in remainder.
4936545Sfeldman  * If destination is this address or broadcast, send packet to
4946545Sfeldman  * loop device to kludge around the fact that 3com interfaces can't
4956545Sfeldman  * talk to themselves.
4966520Sfeldman  */
4976520Sfeldman ecoutput(ifp, m0, dst)
4986520Sfeldman 	struct ifnet *ifp;
4996520Sfeldman 	struct mbuf *m0;
5006520Sfeldman 	struct sockaddr *dst;
5016520Sfeldman {
50211574Ssam 	int type, s, error;
50311574Ssam 	u_char edst[6];
50411574Ssam 	struct in_addr idst;
5056520Sfeldman 	register struct ec_softc *es = &ec_softc[ifp->if_unit];
5066520Sfeldman 	register struct mbuf *m = m0;
5079745Ssam 	register struct ether_header *ec;
50812771Ssam 	register int off;
50911574Ssam 	struct mbuf *mcopy = (struct mbuf *)0;
5106520Sfeldman 
5116520Sfeldman 	switch (dst->sa_family) {
5126520Sfeldman 
5136520Sfeldman #ifdef INET
5146520Sfeldman 	case AF_INET:
51511574Ssam 		idst = ((struct sockaddr_in *)dst)->sin_addr;
51611574Ssam 		if (!arpresolve(&es->es_ac, m, &idst, edst))
51711574Ssam 			return (0);	/* if not yet resolved */
51811574Ssam 		if (in_lnaof(idst) == INADDR_ANY)
5198838Sroot 			mcopy = m_copy(m, 0, (int)M_COPYALL);
5206520Sfeldman 		off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len;
521*13055Ssam 		/* need per host negotiation */
522*13055Ssam 		if ((ifp->if_flags & IFF_NOTRAILERS) == 0)
5236520Sfeldman 		if (off > 0 && (off & 0x1ff) == 0 &&
5246520Sfeldman 		    m->m_off >= MMINOFF + 2 * sizeof (u_short)) {
5259745Ssam 			type = ETHERPUP_TRAIL + (off>>9);
5266520Sfeldman 			m->m_off -= 2 * sizeof (u_short);
5276520Sfeldman 			m->m_len += 2 * sizeof (u_short);
5289745Ssam 			*mtod(m, u_short *) = ntohs((u_short)ETHERPUP_IPTYPE);
5299745Ssam 			*(mtod(m, u_short *) + 1) = ntohs((u_short)m->m_len);
5306520Sfeldman 			goto gottrailertype;
5316520Sfeldman 		}
5329745Ssam 		type = ETHERPUP_IPTYPE;
5336520Sfeldman 		off = 0;
5346520Sfeldman 		goto gottype;
5356520Sfeldman #endif
5366520Sfeldman 
53711574Ssam 	case AF_UNSPEC:
53811574Ssam 		ec = (struct ether_header *)dst->sa_data;
53912771Ssam 		bcopy((caddr_t)ec->ether_dhost, (caddr_t)edst, sizeof (edst));
54011574Ssam 		type = ec->ether_type;
54111574Ssam 		goto gottype;
54211574Ssam 
5436520Sfeldman 	default:
5446520Sfeldman 		printf("ec%d: can't handle af%d\n", ifp->if_unit,
5456520Sfeldman 			dst->sa_family);
5466520Sfeldman 		error = EAFNOSUPPORT;
5476520Sfeldman 		goto bad;
5486520Sfeldman 	}
5496520Sfeldman 
5506520Sfeldman gottrailertype:
5516520Sfeldman 	/*
5526520Sfeldman 	 * Packet to be sent as trailer: move first packet
5536520Sfeldman 	 * (control information) to end of chain.
5546520Sfeldman 	 */
5556520Sfeldman 	while (m->m_next)
5566520Sfeldman 		m = m->m_next;
5576520Sfeldman 	m->m_next = m0;
5586520Sfeldman 	m = m0->m_next;
5596520Sfeldman 	m0->m_next = 0;
5606520Sfeldman 	m0 = m;
5616520Sfeldman 
5626520Sfeldman gottype:
5636520Sfeldman 	/*
5646520Sfeldman 	 * Add local net header.  If no space in first mbuf,
5656520Sfeldman 	 * allocate another.
5666520Sfeldman 	 */
5676520Sfeldman 	if (m->m_off > MMAXOFF ||
5689745Ssam 	    MMINOFF + sizeof (struct ether_header) > m->m_off) {
5699648Ssam 		m = m_get(M_DONTWAIT, MT_HEADER);
5706520Sfeldman 		if (m == 0) {
5716520Sfeldman 			error = ENOBUFS;
5726520Sfeldman 			goto bad;
5736520Sfeldman 		}
5746520Sfeldman 		m->m_next = m0;
5756520Sfeldman 		m->m_off = MMINOFF;
5769745Ssam 		m->m_len = sizeof (struct ether_header);
5776520Sfeldman 	} else {
5789745Ssam 		m->m_off -= sizeof (struct ether_header);
5799745Ssam 		m->m_len += sizeof (struct ether_header);
5806520Sfeldman 	}
5819745Ssam 	ec = mtod(m, struct ether_header *);
58212771Ssam 	bcopy((caddr_t)edst, (caddr_t)ec->ether_dhost, sizeof (edst));
5839745Ssam 	ec->ether_type = htons((u_short)type);
58411574Ssam 	bcopy((caddr_t)es->es_addr, (caddr_t)ec->ether_shost, 6);
5856520Sfeldman 
5866520Sfeldman 	/*
5876520Sfeldman 	 * Queue message on interface, and start output if interface
5886520Sfeldman 	 * not yet active.
5896520Sfeldman 	 */
5906520Sfeldman 	s = splimp();
5916520Sfeldman 	if (IF_QFULL(&ifp->if_snd)) {
5926520Sfeldman 		IF_DROP(&ifp->if_snd);
5936520Sfeldman 		error = ENOBUFS;
5946520Sfeldman 		goto qfull;
5956520Sfeldman 	}
5966520Sfeldman 	IF_ENQUEUE(&ifp->if_snd, m);
5976520Sfeldman 	if (es->es_oactive == 0)
5986520Sfeldman 		ecstart(ifp->if_unit);
5996520Sfeldman 	splx(s);
60012771Ssam 	return (mcopy ? looutput(&loif, mcopy, dst) : 0);
6017216Ssam 
6026520Sfeldman qfull:
6036520Sfeldman 	m0 = m;
6046520Sfeldman 	splx(s);
6056520Sfeldman bad:
6066520Sfeldman 	m_freem(m0);
60712771Ssam 	return (error);
6086520Sfeldman }
6096520Sfeldman 
6106520Sfeldman /*
6119177Ssam  * Routine to copy from mbuf chain to transmit
6127216Ssam  * buffer in UNIBUS memory.
6139177Ssam  * If packet size is less than the minimum legal size,
6149177Ssam  * the buffer is expanded.  We probably should zero out the extra
6159177Ssam  * bytes for security, but that would slow things down.
6166520Sfeldman  */
6176520Sfeldman ecput(ecbuf, m)
6187216Ssam 	u_char *ecbuf;
6196520Sfeldman 	struct mbuf *m;
6206520Sfeldman {
6216520Sfeldman 	register struct mbuf *mp;
6227216Ssam 	register int off;
6237263Ssam 	u_char *bp;
6246520Sfeldman 
6257216Ssam 	for (off = 2048, mp = m; mp; mp = mp->m_next)
6267216Ssam 		off -= mp->m_len;
6279745Ssam 	if (2048 - off < ETHERMIN + sizeof (struct ether_header))
6289745Ssam 		off = 2048 - ETHERMIN - sizeof (struct ether_header);
6297216Ssam 	*(u_short *)ecbuf = off;
6307216Ssam 	bp = (u_char *)(ecbuf + off);
6317263Ssam 	for (mp = m; mp; mp = mp->m_next) {
6327263Ssam 		register unsigned len = mp->m_len;
6337263Ssam 		u_char *mcp;
6347216Ssam 
6357216Ssam 		if (len == 0)
6367216Ssam 			continue;
6377216Ssam 		mcp = mtod(mp, u_char *);
6387216Ssam 		if ((unsigned)bp & 01) {
6397032Swnj 			*bp++ = *mcp++;
6407216Ssam 			len--;
6417032Swnj 		}
6427263Ssam 		if (off = (len >> 1)) {
6437263Ssam 			register u_short *to, *from;
6447263Ssam 
6457263Ssam 			to = (u_short *)bp;
6467263Ssam 			from = (u_short *)mcp;
6477263Ssam 			do
6487263Ssam 				*to++ = *from++;
6497263Ssam 			while (--off > 0);
6507263Ssam 			bp = (u_char *)to,
6517263Ssam 			mcp = (u_char *)from;
6527032Swnj 		}
6537263Ssam 		if (len & 01)
6546520Sfeldman 			*bp++ = *mcp++;
6556520Sfeldman 	}
6567263Ssam 	m_freem(m);
6576520Sfeldman }
6586520Sfeldman 
6596520Sfeldman /*
6606520Sfeldman  * Routine to copy from UNIBUS memory into mbufs.
6616520Sfeldman  * Similar in spirit to if_rubaget.
6627032Swnj  *
6637032Swnj  * Warning: This makes the fairly safe assumption that
6647032Swnj  * mbufs have even lengths.
6656520Sfeldman  */
6666520Sfeldman struct mbuf *
6676520Sfeldman ecget(ecbuf, totlen, off0)
6687263Ssam 	u_char *ecbuf;
6696520Sfeldman 	int totlen, off0;
6706520Sfeldman {
6717263Ssam 	register struct mbuf *m;
6727263Ssam 	struct mbuf *top = 0, **mp = &top;
6737263Ssam 	register int off = off0, len;
6747263Ssam 	u_char *cp;
6756520Sfeldman 
6769745Ssam 	cp = ecbuf + ECRDOFF + sizeof (struct ether_header);
6776520Sfeldman 	while (totlen > 0) {
6787263Ssam 		register int words;
6797263Ssam 		u_char *mcp;
6807263Ssam 
6819648Ssam 		MGET(m, M_DONTWAIT, MT_DATA);
6826520Sfeldman 		if (m == 0)
6836520Sfeldman 			goto bad;
6846520Sfeldman 		if (off) {
6856520Sfeldman 			len = totlen - off;
6869745Ssam 			cp = ecbuf + ECRDOFF +
6879745Ssam 				sizeof (struct ether_header) + off;
6886520Sfeldman 		} else
6896520Sfeldman 			len = totlen;
6906520Sfeldman 		if (len >= CLBYTES) {
6916520Sfeldman 			struct mbuf *p;
6926520Sfeldman 
6936520Sfeldman 			MCLGET(p, 1);
6946520Sfeldman 			if (p != 0) {
6956520Sfeldman 				m->m_len = len = CLBYTES;
6966520Sfeldman 				m->m_off = (int)p - (int)m;
6976520Sfeldman 			} else {
6986520Sfeldman 				m->m_len = len = MIN(MLEN, len);
6996520Sfeldman 				m->m_off = MMINOFF;
7006520Sfeldman 			}
7016520Sfeldman 		} else {
7026520Sfeldman 			m->m_len = len = MIN(MLEN, len);
7036520Sfeldman 			m->m_off = MMINOFF;
7046520Sfeldman 		}
7057263Ssam 		mcp = mtod(m, u_char *);
7067263Ssam 		if (words = (len >> 1)) {
7077263Ssam 			register u_short *to, *from;
7087263Ssam 
7097263Ssam 			to = (u_short *)mcp;
7107263Ssam 			from = (u_short *)cp;
7117263Ssam 			do
7127263Ssam 				*to++ = *from++;
7137263Ssam 			while (--words > 0);
7147263Ssam 			mcp = (u_char *)to;
7157263Ssam 			cp = (u_char *)from;
7167032Swnj 		}
7177216Ssam 		if (len & 01)
7186520Sfeldman 			*mcp++ = *cp++;
7196520Sfeldman 		*mp = m;
7206520Sfeldman 		mp = &m->m_next;
7217263Ssam 		if (off == 0) {
7226520Sfeldman 			totlen -= len;
7237263Ssam 			continue;
7247263Ssam 		}
7257263Ssam 		off += len;
7267263Ssam 		if (off == totlen) {
7279745Ssam 			cp = ecbuf + ECRDOFF + sizeof (struct ether_header);
7287263Ssam 			off = 0;
7297263Ssam 			totlen = off0;
7307263Ssam 		}
7316520Sfeldman 	}
7326520Sfeldman 	return (top);
7336520Sfeldman bad:
7346520Sfeldman 	m_freem(top);
7356520Sfeldman 	return (0);
7366520Sfeldman }
737*13055Ssam 
738*13055Ssam /*
739*13055Ssam  * Process an ioctl request.
740*13055Ssam  */
741*13055Ssam ecioctl(ifp, cmd, data)
742*13055Ssam 	register struct ifnet *ifp;
743*13055Ssam 	int cmd;
744*13055Ssam 	caddr_t data;
745*13055Ssam {
746*13055Ssam 	register struct ifreq *ifr = (struct ifreq *)data;
747*13055Ssam 	register struct sockaddr_in *sin;
748*13055Ssam 	int s = splimp(), error = 0;
749*13055Ssam 
750*13055Ssam 	switch (cmd) {
751*13055Ssam 
752*13055Ssam 	case SIOCSIFADDR:
753*13055Ssam 		if (ifp->if_flags & IFF_RUNNING)
754*13055Ssam 			if_rtinit(ifp, -1);	/* delete previous route */
755*13055Ssam 		ifp->if_addr = ifr->ifr_addr;
756*13055Ssam 		ifp->if_net = in_netof(ifr->ifr_addr);
757*13055Ssam 		ifp->if_host[0] = in_lnaof(ifr->ifr_addr);
758*13055Ssam 		sin = (struct sockaddr_in *)&ifp->if_broadaddr;
759*13055Ssam 		sin->sin_family = AF_INET;
760*13055Ssam 		sin->sin_addr = if_makeaddr(ifp->if_net, INADDR_ANY);
761*13055Ssam 		ifp->if_flags |= IFF_BROADCAST;
762*13055Ssam 		ecinit(ifp->if_unit);
763*13055Ssam 		break;
764*13055Ssam 
765*13055Ssam 	default:
766*13055Ssam 		error = EINVAL;
767*13055Ssam 	}
768*13055Ssam 	splx(s);
769*13055Ssam 	return (error);
770*13055Ssam }
771