xref: /csrg-svn/sys/vax/if/if_ec.c (revision 23558)
1*23558Ssklower /*
2*23558Ssklower  * Copyright (c) 1982 Regents of the University of California.
3*23558Ssklower  * All rights reserved.  The Berkeley software License Agreement
4*23558Ssklower  * specifies the terms and conditions for redistribution.
5*23558Ssklower  *
6*23558Ssklower  *	@(#)if_ec.c	6.9 (Berkeley) 06/19/85
7*23558Ssklower  */
86520Sfeldman 
96520Sfeldman #include "ec.h"
106520Sfeldman 
116520Sfeldman /*
126520Sfeldman  * 3Com Ethernet Controller interface
136520Sfeldman  */
149795Ssam #include "../machine/pte.h"
156520Sfeldman 
1617112Sbloom #include "param.h"
1717112Sbloom #include "systm.h"
1817112Sbloom #include "mbuf.h"
1917112Sbloom #include "buf.h"
2017112Sbloom #include "protosw.h"
2117112Sbloom #include "socket.h"
2217112Sbloom #include "vmmac.h"
2317112Sbloom #include "ioctl.h"
2417112Sbloom #include "errno.h"
258461Sroot 
268461Sroot #include "../net/if.h"
278461Sroot #include "../net/netisr.h"
288461Sroot #include "../net/route.h"
29*23558Ssklower 
30*23558Ssklower #ifdef INET
318417Swnj #include "../netinet/in.h"
328417Swnj #include "../netinet/in_systm.h"
3319863Skarels #include "../netinet/in_var.h"
348417Swnj #include "../netinet/ip.h"
358417Swnj #include "../netinet/ip_var.h"
3611574Ssam #include "../netinet/if_ether.h"
37*23558Ssklower #endif
38*23558Ssklower 
3919951Sbloom #ifdef PUP
408417Swnj #include "../netpup/pup.h"
41*23558Ssklower #endif PUP
42*23558Ssklower 
43*23558Ssklower #ifdef NS
44*23558Ssklower #include "../netns/ns.h"
45*23558Ssklower #include "../netns/ns_if.h"
4619951Sbloom #endif
476520Sfeldman 
488461Sroot #include "../vax/cpu.h"
498461Sroot #include "../vax/mtpr.h"
5017112Sbloom #include "if_ecreg.h"
5117112Sbloom #include "if_uba.h"
528461Sroot #include "../vaxuba/ubareg.h"
538461Sroot #include "../vaxuba/ubavar.h"
548461Sroot 
5517995Skarels #if CLSIZE == 2
5617995Skarels #define ECBUFSIZE	32		/* on-board memory, clusters */
5716749Sbloom #endif
5816749Sbloom 
5917995Skarels int	ecubamem(), ecprobe(), ecattach(), ecrint(), ecxint(), eccollide();
606520Sfeldman struct	uba_device *ecinfo[NEC];
616520Sfeldman u_short ecstd[] = { 0 };
626520Sfeldman struct	uba_driver ecdriver =
6317995Skarels 	{ ecprobe, 0, ecattach, 0, ecstd, "ec", ecinfo, 0, 0, 0, ecubamem };
646520Sfeldman 
6513055Ssam int	ecinit(),ecioctl(),ecoutput(),ecreset();
668773Sroot struct	mbuf *ecget();
676520Sfeldman 
686545Sfeldman extern struct ifnet loif;
696545Sfeldman 
706520Sfeldman /*
716520Sfeldman  * Ethernet software status per interface.
726520Sfeldman  *
736520Sfeldman  * Each interface is referenced by a network interface structure,
746520Sfeldman  * es_if, which the routing code uses to locate the interface.
756520Sfeldman  * This structure contains the output queue for the interface, its address, ...
766520Sfeldman  * We also have, for each interface, a UBA interface structure, which
776520Sfeldman  * contains information about the UNIBUS resources held by the interface:
786520Sfeldman  * map registers, buffered data paths, etc.  Information is cached in this
796520Sfeldman  * structure for use by the if_uba.c routines in running the interface
806520Sfeldman  * efficiently.
816520Sfeldman  */
826520Sfeldman struct	ec_softc {
8311574Ssam 	struct	arpcom es_ac;		/* common Ethernet structures */
8411574Ssam #define	es_if	es_ac.ac_if		/* network-visible interface */
8511574Ssam #define	es_addr	es_ac.ac_enaddr		/* hardware Ethernet address */
866520Sfeldman 	struct	ifuba es_ifuba;		/* UNIBUS resources */
876520Sfeldman 	short	es_mask;		/* mask for current output delay */
886520Sfeldman 	short	es_oactive;		/* is output active? */
898773Sroot 	u_char	*es_buf[16];		/* virtual addresses of buffers */
906520Sfeldman } ec_softc[NEC];
916520Sfeldman 
926520Sfeldman /*
9317995Skarels  * Configure on-board memory for an interface.
9417995Skarels  * Called from autoconfig and after a uba reset.
9517995Skarels  * The address of the memory on the uba is supplied in the device flags.
966520Sfeldman  */
9717995Skarels ecubamem(ui, uban)
9817995Skarels 	register struct uba_device *ui;
996520Sfeldman {
10017995Skarels 	register caddr_t ecbuf = (caddr_t) &umem[uban][ui->ui_flags];
10117995Skarels 	register struct ecdevice *addr = (struct ecdevice *)ui->ui_addr;
1026520Sfeldman 
10317995Skarels 	/*
10417995Skarels 	 * Make sure csr is there (we run before ecprobe).
10517995Skarels 	 */
10617995Skarels 	if (badaddr((caddr_t)addr, 2))
10717995Skarels 		return (-1);
10817995Skarels #if VAX780
10917995Skarels 	if (cpu == VAX_780 && uba_hd[uban].uh_uba->uba_sr) {
11017995Skarels 		uba_hd[uban].uh_uba->uba_sr = uba_hd[uban].uh_uba->uba_sr;
11117995Skarels 		return (-1);
11217995Skarels 	}
1136520Sfeldman #endif
1146520Sfeldman 	/*
1156637Sfeldman 	 * Make sure memory is turned on
1166637Sfeldman 	 */
1176637Sfeldman 	addr->ec_rcr = EC_AROM;
1186637Sfeldman 	/*
11917995Skarels 	 * Tell the system that the board has memory here, so it won't
12017995Skarels 	 * attempt to allocate the addresses later.
1217470Sfeldman 	 */
12217995Skarels 	if (ubamem(uban, ui->ui_flags, ECBUFSIZE*CLSIZE, 1) == 0) {
12317995Skarels 		printf("ec%d: cannot reserve uba addresses\n", ui->ui_unit);
12417995Skarels 		addr->ec_rcr = EC_MDISAB;	/* disable memory */
12517995Skarels 		return (-1);
12617995Skarels 	}
1277470Sfeldman 	/*
1286520Sfeldman 	 * Check for existence of buffers on Unibus.
1296520Sfeldman 	 */
1308773Sroot 	if (badaddr((caddr_t)ecbuf, 2)) {
13117995Skarels bad:
13217995Skarels 		printf("ec%d: buffer mem not found\n", ui->ui_unit);
13317995Skarels 		(void) ubamem(uban, ui->ui_flags, ECBUFSIZE*2, 0);
1347470Sfeldman 		addr->ec_rcr = EC_MDISAB;	/* disable memory */
13517995Skarels 		return (-1);
1366520Sfeldman 	}
1377470Sfeldman #if VAX780
13817995Skarels 	if (cpu == VAX_780 && uba_hd[uban].uh_uba->uba_sr) {
13917995Skarels 		uba_hd[uban].uh_uba->uba_sr = uba_hd[uban].uh_uba->uba_sr;
14017995Skarels 		goto bad;
1417470Sfeldman 	}
1427470Sfeldman #endif
14317995Skarels 	if (ui->ui_alive == 0)		/* Only printf from autoconfig */
14417995Skarels 		printf("ec%d: mem %x-%x\n", ui->ui_unit,
14517995Skarels 			ui->ui_flags, ui->ui_flags + ECBUFSIZE*CLBYTES - 1);
14617995Skarels 	ui->ui_type = 1;		/* Memory on, allocated */
14717995Skarels 	return (0);
14817995Skarels }
1496520Sfeldman 
15017995Skarels /*
15117995Skarels  * Do output DMA to determine interface presence and
15217995Skarels  * interrupt vector.  DMA is too short to disturb other hosts.
15317995Skarels  */
15417995Skarels ecprobe(reg, ui)
15517995Skarels 	caddr_t reg;
15617995Skarels 	struct uba_device *ui;
15717995Skarels {
15817995Skarels 	register int br, cvec;		/* r11, r10 value-result */
15917995Skarels 	register struct ecdevice *addr = (struct ecdevice *)reg;
16017995Skarels 	register caddr_t ecbuf = (caddr_t) &umem[ui->ui_ubanum][ui->ui_flags];
16117995Skarels 
16217995Skarels #ifdef lint
16317995Skarels 	br = 0; cvec = br; br = cvec;
16417995Skarels 	ecrint(0); ecxint(0); eccollide(0);
16517995Skarels #endif
16617995Skarels 
1676520Sfeldman 	/*
16817995Skarels 	 * Check that buffer memory was found and enabled.
1696520Sfeldman 	 */
17017995Skarels 	if (ui->ui_type == 0)
17117995Skarels 		return(0);
1726520Sfeldman 	/*
1736520Sfeldman 	 * Make a one byte packet in what should be buffer #0.
17417995Skarels 	 * Submit it for sending.  This should cause an xmit interrupt.
1756520Sfeldman 	 * The xmit interrupt vector is 8 bytes after the receive vector,
1766520Sfeldman 	 * so adjust for this before returning.
1776520Sfeldman 	 */
1786520Sfeldman 	*(u_short *)ecbuf = (u_short) 03777;
1796520Sfeldman 	ecbuf[03777] = '\0';
1806520Sfeldman 	addr->ec_xcr = EC_XINTEN|EC_XWBN;
1816520Sfeldman 	DELAY(100000);
1826520Sfeldman 	addr->ec_xcr = EC_XCLR;
1837216Ssam 	if (cvec > 0 && cvec != 0x200) {
1847470Sfeldman 		if (cvec & 04) {	/* collision interrupt */
1857470Sfeldman 			cvec -= 04;
1867470Sfeldman 			br += 1;		/* rcv is collision + 1 */
1877470Sfeldman 		} else {		/* xmit interrupt */
1887470Sfeldman 			cvec -= 010;
1897470Sfeldman 			br += 2;		/* rcv is xmit + 2 */
1907470Sfeldman 		}
1917216Ssam 	}
1926520Sfeldman 	return (1);
1936520Sfeldman }
1946520Sfeldman 
1956520Sfeldman /*
1966520Sfeldman  * Interface exists: make available by filling in network interface
1976520Sfeldman  * record.  System will initialize the interface when it is ready
1986520Sfeldman  * to accept packets.
1996520Sfeldman  */
2006520Sfeldman ecattach(ui)
2016520Sfeldman 	struct uba_device *ui;
2026520Sfeldman {
2037216Ssam 	struct ec_softc *es = &ec_softc[ui->ui_unit];
2047216Ssam 	register struct ifnet *ifp = &es->es_if;
2056520Sfeldman 	register struct ecdevice *addr = (struct ecdevice *)ui->ui_addr;
2067216Ssam 	int i, j;
2077216Ssam 	u_char *cp;
2086520Sfeldman 
2097216Ssam 	ifp->if_unit = ui->ui_unit;
2107216Ssam 	ifp->if_name = "ec";
2119745Ssam 	ifp->if_mtu = ETHERMTU;
2126520Sfeldman 
2136520Sfeldman 	/*
2147216Ssam 	 * Read the ethernet address off the board, one nibble at a time.
2156520Sfeldman 	 */
2166520Sfeldman 	addr->ec_xcr = EC_UECLR;
2176520Sfeldman 	addr->ec_rcr = EC_AROM;
21819863Skarels 	cp = es->es_addr;
2197216Ssam #define	NEXTBIT	addr->ec_rcr = EC_AROM|EC_ASTEP; addr->ec_rcr = EC_AROM
22016217Skarels 	for (i=0; i < sizeof (es->es_addr); i++) {
2216520Sfeldman 		*cp = 0;
2226520Sfeldman 		for (j=0; j<=4; j+=4) {
2236520Sfeldman 			*cp |= ((addr->ec_rcr >> 8) & 0xf) << j;
2247216Ssam 			NEXTBIT; NEXTBIT; NEXTBIT; NEXTBIT;
2256520Sfeldman 		}
2266520Sfeldman 		cp++;
2276520Sfeldman 	}
2287216Ssam 	ifp->if_init = ecinit;
22913055Ssam 	ifp->if_ioctl = ecioctl;
2307216Ssam 	ifp->if_output = ecoutput;
2318977Sroot 	ifp->if_reset = ecreset;
23219863Skarels 	ifp->if_flags = IFF_BROADCAST;
2336520Sfeldman 	for (i=0; i<16; i++)
23416749Sbloom 		es->es_buf[i]
23517995Skarels 		    = (u_char *)&umem[ui->ui_ubanum][ui->ui_flags + 2048*i];
2367216Ssam 	if_attach(ifp);
2376520Sfeldman }
2386520Sfeldman 
2396520Sfeldman /*
2406520Sfeldman  * Reset of interface after UNIBUS reset.
2416520Sfeldman  * If interface is on specified uba, reset its state.
2426520Sfeldman  */
2436520Sfeldman ecreset(unit, uban)
2446520Sfeldman 	int unit, uban;
2456520Sfeldman {
2466520Sfeldman 	register struct uba_device *ui;
2476520Sfeldman 
2486520Sfeldman 	if (unit >= NEC || (ui = ecinfo[unit]) == 0 || ui->ui_alive == 0 ||
2496520Sfeldman 	    ui->ui_ubanum != uban)
2506520Sfeldman 		return;
2516520Sfeldman 	printf(" ec%d", unit);
25216207Skarels 	ec_softc[unit].es_if.if_flags &= ~IFF_RUNNING;
2536520Sfeldman 	ecinit(unit);
2546520Sfeldman }
2556520Sfeldman 
2566520Sfeldman /*
2576520Sfeldman  * Initialization of interface; clear recorded pending
2586520Sfeldman  * operations, and reinitialize UNIBUS usage.
2596520Sfeldman  */
2606520Sfeldman ecinit(unit)
2616520Sfeldman 	int unit;
2626520Sfeldman {
2637216Ssam 	struct ec_softc *es = &ec_softc[unit];
2647216Ssam 	struct ecdevice *addr;
26511574Ssam 	register struct ifnet *ifp = &es->es_if;
26613055Ssam 	int i, s;
2676520Sfeldman 
26819863Skarels 	/* not yet, if address still unknown */
26919863Skarels 	if (ifp->if_addrlist == (struct ifaddr *)0)
27011574Ssam 		return;
27111574Ssam 
2726520Sfeldman 	/*
2737217Sfeldman 	 * Hang receive buffers and start any pending writes.
2746637Sfeldman 	 * Writing into the rcr also makes sure the memory
2756637Sfeldman 	 * is turned on.
2766520Sfeldman 	 */
27719863Skarels 	if ((ifp->if_flags & IFF_RUNNING) == 0) {
27813055Ssam 		addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
27913055Ssam 		s = splimp();
28013055Ssam 		for (i = ECRHBF; i >= ECRLBF; i--)
28113055Ssam 			addr->ec_rcr = EC_READ | i;
28213055Ssam 		es->es_oactive = 0;
28313055Ssam 		es->es_mask = ~0;
28419863Skarels 		es->es_if.if_flags |= IFF_RUNNING;
28513055Ssam 		if (es->es_if.if_snd.ifq_head)
28613055Ssam 			ecstart(unit);
28713055Ssam 		splx(s);
28813055Ssam 	}
2896520Sfeldman }
2906520Sfeldman 
2916520Sfeldman /*
2926520Sfeldman  * Start or restart output on interface.
2936520Sfeldman  * If interface is already active, then this is a retransmit
2946545Sfeldman  * after a collision, and just restuff registers.
2956520Sfeldman  * If interface is not already active, get another datagram
2966520Sfeldman  * to send off of the interface queue, and map it to the interface
2976520Sfeldman  * before starting the output.
2986520Sfeldman  */
29917995Skarels ecstart(unit)
3006520Sfeldman {
3017216Ssam 	struct ec_softc *es = &ec_softc[unit];
3027216Ssam 	struct ecdevice *addr;
3036520Sfeldman 	struct mbuf *m;
3046520Sfeldman 
3056520Sfeldman 	if (es->es_oactive)
3066520Sfeldman 		goto restart;
3076520Sfeldman 
3086520Sfeldman 	IF_DEQUEUE(&es->es_if.if_snd, m);
3096520Sfeldman 	if (m == 0) {
3106520Sfeldman 		es->es_oactive = 0;
3116520Sfeldman 		return;
3126520Sfeldman 	}
3136520Sfeldman 	ecput(es->es_buf[ECTBF], m);
3146520Sfeldman 
3156520Sfeldman restart:
3167216Ssam 	addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
3176520Sfeldman 	addr->ec_xcr = EC_WRITE|ECTBF;
3186520Sfeldman 	es->es_oactive = 1;
3196520Sfeldman }
3206520Sfeldman 
3216520Sfeldman /*
3226520Sfeldman  * Ethernet interface transmitter interrupt.
3236520Sfeldman  * Start another output if more data to send.
3246520Sfeldman  */
3256520Sfeldman ecxint(unit)
3266520Sfeldman 	int unit;
3276520Sfeldman {
3286520Sfeldman 	register struct ec_softc *es = &ec_softc[unit];
3297216Ssam 	register struct ecdevice *addr =
3307216Ssam 		(struct ecdevice *)ecinfo[unit]->ui_addr;
3316520Sfeldman 
3326520Sfeldman 	if (es->es_oactive == 0)
3336520Sfeldman 		return;
3347216Ssam 	if ((addr->ec_xcr&EC_XDONE) == 0 || (addr->ec_xcr&EC_XBN) != ECTBF) {
3357216Ssam 		printf("ec%d: stray xmit interrupt, xcr=%b\n", unit,
3367216Ssam 			addr->ec_xcr, EC_XBITS);
3377216Ssam 		es->es_oactive = 0;
3387216Ssam 		addr->ec_xcr = EC_XCLR;
3397216Ssam 		return;
3407216Ssam 	}
3416520Sfeldman 	es->es_if.if_opackets++;
3426520Sfeldman 	es->es_oactive = 0;
3436520Sfeldman 	es->es_mask = ~0;
3446520Sfeldman 	addr->ec_xcr = EC_XCLR;
3457216Ssam 	if (es->es_if.if_snd.ifq_head)
3467216Ssam 		ecstart(unit);
3476520Sfeldman }
3486520Sfeldman 
3496520Sfeldman /*
3506520Sfeldman  * Collision on ethernet interface.  Do exponential
3516520Sfeldman  * backoff, and retransmit.  If have backed off all
3526520Sfeldman  * the way print warning diagnostic, and drop packet.
3536520Sfeldman  */
3546520Sfeldman eccollide(unit)
3556520Sfeldman 	int unit;
3566520Sfeldman {
3576520Sfeldman 	struct ec_softc *es = &ec_softc[unit];
3586520Sfeldman 
3596520Sfeldman 	es->es_if.if_collisions++;
3607216Ssam 	if (es->es_oactive)
3617216Ssam 		ecdocoll(unit);
3626520Sfeldman }
3636520Sfeldman 
3646520Sfeldman ecdocoll(unit)
3656520Sfeldman 	int unit;
3666520Sfeldman {
3676520Sfeldman 	register struct ec_softc *es = &ec_softc[unit];
3686545Sfeldman 	register struct ecdevice *addr =
3696545Sfeldman 	    (struct ecdevice *)ecinfo[unit]->ui_addr;
3706545Sfeldman 	register i;
3716545Sfeldman 	int delay;
3726520Sfeldman 
3736520Sfeldman 	/*
3746520Sfeldman 	 * Es_mask is a 16 bit number with n low zero bits, with
3756520Sfeldman 	 * n the number of backoffs.  When es_mask is 0 we have
3766520Sfeldman 	 * backed off 16 times, and give up.
3776520Sfeldman 	 */
3786520Sfeldman 	if (es->es_mask == 0) {
3796545Sfeldman 		es->es_if.if_oerrors++;
3806520Sfeldman 		printf("ec%d: send error\n", unit);
3816520Sfeldman 		/*
3826545Sfeldman 		 * Reset interface, then requeue rcv buffers.
3836545Sfeldman 		 * Some incoming packets may be lost, but that
3846545Sfeldman 		 * can't be helped.
3856520Sfeldman 		 */
3866545Sfeldman 		addr->ec_xcr = EC_UECLR;
3876545Sfeldman 		for (i=ECRHBF; i>=ECRLBF; i--)
3886545Sfeldman 			addr->ec_rcr = EC_READ|i;
3896545Sfeldman 		/*
3906545Sfeldman 		 * Reset and transmit next packet (if any).
3916545Sfeldman 		 */
3926545Sfeldman 		es->es_oactive = 0;
3936545Sfeldman 		es->es_mask = ~0;
3946545Sfeldman 		if (es->es_if.if_snd.ifq_head)
3956545Sfeldman 			ecstart(unit);
3966520Sfeldman 		return;
3976520Sfeldman 	}
3986520Sfeldman 	/*
3996545Sfeldman 	 * Do exponential backoff.  Compute delay based on low bits
4006545Sfeldman 	 * of the interval timer.  Then delay for that number of
4016545Sfeldman 	 * slot times.  A slot time is 51.2 microseconds (rounded to 51).
4026545Sfeldman 	 * This does not take into account the time already used to
4036545Sfeldman 	 * process the interrupt.
4046520Sfeldman 	 */
4056520Sfeldman 	es->es_mask <<= 1;
4066545Sfeldman 	delay = mfpr(ICR) &~ es->es_mask;
4076545Sfeldman 	DELAY(delay * 51);
4086520Sfeldman 	/*
4096545Sfeldman 	 * Clear the controller's collision flag, thus enabling retransmit.
4106520Sfeldman 	 */
4117470Sfeldman 	addr->ec_xcr = EC_CLEAR;
4126520Sfeldman }
4136520Sfeldman 
4146520Sfeldman /*
4156520Sfeldman  * Ethernet interface receiver interrupt.
4166520Sfeldman  * If input error just drop packet.
4176520Sfeldman  * Otherwise purge input buffered data path and examine
4186520Sfeldman  * packet to determine type.  If can't determine length
4196520Sfeldman  * from type, then have to drop packet.  Othewise decapsulate
4206520Sfeldman  * packet based on type and pass to type specific higher-level
4216520Sfeldman  * input routine.
4226520Sfeldman  */
4236520Sfeldman ecrint(unit)
4246520Sfeldman 	int unit;
4256520Sfeldman {
4266520Sfeldman 	struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
4276520Sfeldman 
4286520Sfeldman 	while (addr->ec_rcr & EC_RDONE)
4296520Sfeldman 		ecread(unit);
4306520Sfeldman }
4316520Sfeldman 
4326520Sfeldman ecread(unit)
4336520Sfeldman 	int unit;
4346520Sfeldman {
4356520Sfeldman 	register struct ec_softc *es = &ec_softc[unit];
4366520Sfeldman 	struct ecdevice *addr = (struct ecdevice *)ecinfo[unit]->ui_addr;
4379745Ssam 	register struct ether_header *ec;
4386520Sfeldman     	struct mbuf *m;
4398773Sroot 	int len, off, resid, ecoff, rbuf;
4406520Sfeldman 	register struct ifqueue *inq;
4418773Sroot 	u_char *ecbuf;
4426520Sfeldman 
4436520Sfeldman 	es->es_if.if_ipackets++;
4448773Sroot 	rbuf = addr->ec_rcr & EC_RBN;
4458773Sroot 	if (rbuf < ECRLBF || rbuf > ECRHBF)
4466520Sfeldman 		panic("ecrint");
4478773Sroot 	ecbuf = es->es_buf[rbuf];
4486520Sfeldman 	ecoff = *(short *)ecbuf;
4496545Sfeldman 	if (ecoff <= ECRDOFF || ecoff > 2046) {
4506520Sfeldman 		es->es_if.if_ierrors++;
4516520Sfeldman #ifdef notdef
4526520Sfeldman 		if (es->es_if.if_ierrors % 100 == 0)
4536520Sfeldman 			printf("ec%d: += 100 input errors\n", unit);
4546520Sfeldman #endif
4556520Sfeldman 		goto setup;
4566520Sfeldman 	}
4576520Sfeldman 
4586520Sfeldman 	/*
4596520Sfeldman 	 * Get input data length.
4606520Sfeldman 	 * Get pointer to ethernet header (in input buffer).
46119863Skarels 	 * Deal with trailer protocol: if type is trailer type
4626520Sfeldman 	 * get true type from first 16-bit word past data.
4636520Sfeldman 	 * Remember that type was trailer by setting off.
4646520Sfeldman 	 */
4659745Ssam 	len = ecoff - ECRDOFF - sizeof (struct ether_header);
4669745Ssam 	ec = (struct ether_header *)(ecbuf + ECRDOFF);
4679745Ssam 	ec->ether_type = ntohs((u_short)ec->ether_type);
4686520Sfeldman #define	ecdataaddr(ec, off, type)	((type)(((caddr_t)((ec)+1)+(off))))
46919863Skarels 	if (ec->ether_type >= ETHERTYPE_TRAIL &&
47019863Skarels 	    ec->ether_type < ETHERTYPE_TRAIL+ETHERTYPE_NTRAILER) {
47119863Skarels 		off = (ec->ether_type - ETHERTYPE_TRAIL) * 512;
4729745Ssam 		if (off >= ETHERMTU)
4736520Sfeldman 			goto setup;		/* sanity */
4749745Ssam 		ec->ether_type = ntohs(*ecdataaddr(ec, off, u_short *));
4759745Ssam 		resid = ntohs(*(ecdataaddr(ec, off+2, u_short *)));
4766520Sfeldman 		if (off + resid > len)
4776520Sfeldman 			goto setup;		/* sanity */
4786520Sfeldman 		len = off + resid;
4796520Sfeldman 	} else
4806520Sfeldman 		off = 0;
4816520Sfeldman 	if (len == 0)
4826520Sfeldman 		goto setup;
4836520Sfeldman 
4846520Sfeldman 	/*
4856520Sfeldman 	 * Pull packet off interface.  Off is nonzero if packet
4866520Sfeldman 	 * has trailing header; ecget will then force this header
4876520Sfeldman 	 * information to be at the front, but we still have to drop
4886520Sfeldman 	 * the type and length which are at the front of any trailer data.
4896520Sfeldman 	 */
4906520Sfeldman 	m = ecget(ecbuf, len, off);
4916520Sfeldman 	if (m == 0)
4926520Sfeldman 		goto setup;
4936520Sfeldman 	if (off) {
4946520Sfeldman 		m->m_off += 2 * sizeof (u_short);
4956520Sfeldman 		m->m_len -= 2 * sizeof (u_short);
4966520Sfeldman 	}
4979745Ssam 	switch (ec->ether_type) {
4986520Sfeldman 
4996520Sfeldman #ifdef INET
50019863Skarels 	case ETHERTYPE_IP:
5016520Sfeldman 		schednetisr(NETISR_IP);
5026520Sfeldman 		inq = &ipintrq;
5036520Sfeldman 		break;
50411574Ssam 
50519863Skarels 	case ETHERTYPE_ARP:
50611574Ssam 		arpinput(&es->es_ac, m);
50713987Ssam 		goto setup;
5086520Sfeldman #endif
509*23558Ssklower #ifdef NS
510*23558Ssklower 	case ETHERTYPE_NS:
511*23558Ssklower 		schednetisr(NETISR_NS);
512*23558Ssklower 		inq = &nsintrq;
513*23558Ssklower 		break;
514*23558Ssklower 
515*23558Ssklower #endif
5166520Sfeldman 	default:
5176520Sfeldman 		m_freem(m);
5186520Sfeldman 		goto setup;
5196520Sfeldman 	}
5206520Sfeldman 
5216520Sfeldman 	if (IF_QFULL(inq)) {
5226520Sfeldman 		IF_DROP(inq);
5236520Sfeldman 		m_freem(m);
5247216Ssam 		goto setup;
5257216Ssam 	}
5267216Ssam 	IF_ENQUEUE(inq, m);
5276520Sfeldman 
5286520Sfeldman setup:
5296520Sfeldman 	/*
5306520Sfeldman 	 * Reset for next packet.
5316520Sfeldman 	 */
5328773Sroot 	addr->ec_rcr = EC_READ|EC_RCLR|rbuf;
5336520Sfeldman }
5346520Sfeldman 
5356520Sfeldman /*
5366520Sfeldman  * Ethernet output routine.
5376520Sfeldman  * Encapsulate a packet of type family for the local net.
5386520Sfeldman  * Use trailer local net encapsulation if enough data in first
5396520Sfeldman  * packet leaves a multiple of 512 bytes of data in remainder.
5406545Sfeldman  * If destination is this address or broadcast, send packet to
5416545Sfeldman  * loop device to kludge around the fact that 3com interfaces can't
5426545Sfeldman  * talk to themselves.
5436520Sfeldman  */
5446520Sfeldman ecoutput(ifp, m0, dst)
5456520Sfeldman 	struct ifnet *ifp;
5466520Sfeldman 	struct mbuf *m0;
5476520Sfeldman 	struct sockaddr *dst;
5486520Sfeldman {
54911574Ssam 	int type, s, error;
55019863Skarels  	u_char edst[6];
55111574Ssam 	struct in_addr idst;
5526520Sfeldman 	register struct ec_softc *es = &ec_softc[ifp->if_unit];
5536520Sfeldman 	register struct mbuf *m = m0;
5549745Ssam 	register struct ether_header *ec;
55512771Ssam 	register int off;
55611574Ssam 	struct mbuf *mcopy = (struct mbuf *)0;
5576520Sfeldman 
5586520Sfeldman 	switch (dst->sa_family) {
5596520Sfeldman 
5606520Sfeldman #ifdef INET
5616520Sfeldman 	case AF_INET:
56211574Ssam 		idst = ((struct sockaddr_in *)dst)->sin_addr;
56319863Skarels 		if (!arpresolve(&es->es_ac, m, &idst, edst))
56411574Ssam 			return (0);	/* if not yet resolved */
56519863Skarels 		if (!bcmp((caddr_t)edst, (caddr_t)etherbroadcastaddr,
56619863Skarels 		    sizeof(edst)))
5678838Sroot 			mcopy = m_copy(m, 0, (int)M_COPYALL);
5686520Sfeldman 		off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len;
56913055Ssam 		/* need per host negotiation */
57013055Ssam 		if ((ifp->if_flags & IFF_NOTRAILERS) == 0)
5716520Sfeldman 		if (off > 0 && (off & 0x1ff) == 0 &&
5726520Sfeldman 		    m->m_off >= MMINOFF + 2 * sizeof (u_short)) {
57319863Skarels 			type = ETHERTYPE_TRAIL + (off>>9);
5746520Sfeldman 			m->m_off -= 2 * sizeof (u_short);
5756520Sfeldman 			m->m_len += 2 * sizeof (u_short);
57619863Skarels 			*mtod(m, u_short *) = ntohs((u_short)ETHERTYPE_IP);
5779745Ssam 			*(mtod(m, u_short *) + 1) = ntohs((u_short)m->m_len);
5786520Sfeldman 			goto gottrailertype;
5796520Sfeldman 		}
58019863Skarels 		type = ETHERTYPE_IP;
5816520Sfeldman 		off = 0;
5826520Sfeldman 		goto gottype;
5836520Sfeldman #endif
584*23558Ssklower #ifdef NS
585*23558Ssklower 	case AF_NS:
586*23558Ssklower  		bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
587*23558Ssklower 		    (caddr_t)edst, sizeof (edst));
5886520Sfeldman 
589*23558Ssklower 		if (!bcmp((caddr_t)edst, (caddr_t)&ns_broadhost,
590*23558Ssklower 			sizeof(edst))) {
591*23558Ssklower 
592*23558Ssklower 				mcopy = m_copy(m, 0, (int)M_COPYALL);
593*23558Ssklower 		} else if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost,
594*23558Ssklower 			sizeof(edst))) {
595*23558Ssklower 
596*23558Ssklower 				return(looutput(&loif, m, dst));
597*23558Ssklower 		}
598*23558Ssklower 		type = ETHERTYPE_NS;
599*23558Ssklower 		off = 0;
600*23558Ssklower 		goto gottype;
601*23558Ssklower #endif
602*23558Ssklower 
60311574Ssam 	case AF_UNSPEC:
60411574Ssam 		ec = (struct ether_header *)dst->sa_data;
60519863Skarels  		bcopy((caddr_t)ec->ether_dhost, (caddr_t)edst, sizeof (edst));
60611574Ssam 		type = ec->ether_type;
60711574Ssam 		goto gottype;
60811574Ssam 
6096520Sfeldman 	default:
6106520Sfeldman 		printf("ec%d: can't handle af%d\n", ifp->if_unit,
6116520Sfeldman 			dst->sa_family);
6126520Sfeldman 		error = EAFNOSUPPORT;
6136520Sfeldman 		goto bad;
6146520Sfeldman 	}
6156520Sfeldman 
6166520Sfeldman gottrailertype:
6176520Sfeldman 	/*
6186520Sfeldman 	 * Packet to be sent as trailer: move first packet
6196520Sfeldman 	 * (control information) to end of chain.
6206520Sfeldman 	 */
6216520Sfeldman 	while (m->m_next)
6226520Sfeldman 		m = m->m_next;
6236520Sfeldman 	m->m_next = m0;
6246520Sfeldman 	m = m0->m_next;
6256520Sfeldman 	m0->m_next = 0;
6266520Sfeldman 	m0 = m;
6276520Sfeldman 
6286520Sfeldman gottype:
6296520Sfeldman 	/*
6306520Sfeldman 	 * Add local net header.  If no space in first mbuf,
6316520Sfeldman 	 * allocate another.
6326520Sfeldman 	 */
6336520Sfeldman 	if (m->m_off > MMAXOFF ||
6349745Ssam 	    MMINOFF + sizeof (struct ether_header) > m->m_off) {
6359648Ssam 		m = m_get(M_DONTWAIT, MT_HEADER);
6366520Sfeldman 		if (m == 0) {
6376520Sfeldman 			error = ENOBUFS;
6386520Sfeldman 			goto bad;
6396520Sfeldman 		}
6406520Sfeldman 		m->m_next = m0;
6416520Sfeldman 		m->m_off = MMINOFF;
6429745Ssam 		m->m_len = sizeof (struct ether_header);
6436520Sfeldman 	} else {
6449745Ssam 		m->m_off -= sizeof (struct ether_header);
6459745Ssam 		m->m_len += sizeof (struct ether_header);
6466520Sfeldman 	}
6479745Ssam 	ec = mtod(m, struct ether_header *);
64819863Skarels  	bcopy((caddr_t)edst, (caddr_t)ec->ether_dhost, sizeof (edst));
64919863Skarels 	bcopy((caddr_t)es->es_addr, (caddr_t)ec->ether_shost,
65019863Skarels 	    sizeof(ec->ether_shost));
6519745Ssam 	ec->ether_type = htons((u_short)type);
6526520Sfeldman 
6536520Sfeldman 	/*
6546520Sfeldman 	 * Queue message on interface, and start output if interface
6556520Sfeldman 	 * not yet active.
6566520Sfeldman 	 */
6576520Sfeldman 	s = splimp();
6586520Sfeldman 	if (IF_QFULL(&ifp->if_snd)) {
6596520Sfeldman 		IF_DROP(&ifp->if_snd);
6606520Sfeldman 		error = ENOBUFS;
6616520Sfeldman 		goto qfull;
6626520Sfeldman 	}
6636520Sfeldman 	IF_ENQUEUE(&ifp->if_snd, m);
6646520Sfeldman 	if (es->es_oactive == 0)
6656520Sfeldman 		ecstart(ifp->if_unit);
6666520Sfeldman 	splx(s);
66712771Ssam 	return (mcopy ? looutput(&loif, mcopy, dst) : 0);
6687216Ssam 
6696520Sfeldman qfull:
6706520Sfeldman 	m0 = m;
6716520Sfeldman 	splx(s);
6726520Sfeldman bad:
6736520Sfeldman 	m_freem(m0);
67416207Skarels 	if (mcopy)
67516207Skarels 		m_freem(mcopy);
67612771Ssam 	return (error);
6776520Sfeldman }
6786520Sfeldman 
6796520Sfeldman /*
6809177Ssam  * Routine to copy from mbuf chain to transmit
6817216Ssam  * buffer in UNIBUS memory.
6829177Ssam  * If packet size is less than the minimum legal size,
6839177Ssam  * the buffer is expanded.  We probably should zero out the extra
6849177Ssam  * bytes for security, but that would slow things down.
6856520Sfeldman  */
6866520Sfeldman ecput(ecbuf, m)
6877216Ssam 	u_char *ecbuf;
6886520Sfeldman 	struct mbuf *m;
6896520Sfeldman {
6906520Sfeldman 	register struct mbuf *mp;
6917216Ssam 	register int off;
6927263Ssam 	u_char *bp;
6936520Sfeldman 
6947216Ssam 	for (off = 2048, mp = m; mp; mp = mp->m_next)
6957216Ssam 		off -= mp->m_len;
6969745Ssam 	if (2048 - off < ETHERMIN + sizeof (struct ether_header))
6979745Ssam 		off = 2048 - ETHERMIN - sizeof (struct ether_header);
6987216Ssam 	*(u_short *)ecbuf = off;
6997216Ssam 	bp = (u_char *)(ecbuf + off);
7007263Ssam 	for (mp = m; mp; mp = mp->m_next) {
7017263Ssam 		register unsigned len = mp->m_len;
7027263Ssam 		u_char *mcp;
7037216Ssam 
7047216Ssam 		if (len == 0)
7057216Ssam 			continue;
7067216Ssam 		mcp = mtod(mp, u_char *);
7077216Ssam 		if ((unsigned)bp & 01) {
7087032Swnj 			*bp++ = *mcp++;
7097216Ssam 			len--;
7107032Swnj 		}
7117263Ssam 		if (off = (len >> 1)) {
7127263Ssam 			register u_short *to, *from;
7137263Ssam 
7147263Ssam 			to = (u_short *)bp;
7157263Ssam 			from = (u_short *)mcp;
7167263Ssam 			do
7177263Ssam 				*to++ = *from++;
7187263Ssam 			while (--off > 0);
7197263Ssam 			bp = (u_char *)to,
7207263Ssam 			mcp = (u_char *)from;
7217032Swnj 		}
7227263Ssam 		if (len & 01)
7236520Sfeldman 			*bp++ = *mcp++;
7246520Sfeldman 	}
7257263Ssam 	m_freem(m);
7266520Sfeldman }
7276520Sfeldman 
7286520Sfeldman /*
7296520Sfeldman  * Routine to copy from UNIBUS memory into mbufs.
7306520Sfeldman  * Similar in spirit to if_rubaget.
7317032Swnj  *
7327032Swnj  * Warning: This makes the fairly safe assumption that
7337032Swnj  * mbufs have even lengths.
7346520Sfeldman  */
7356520Sfeldman struct mbuf *
7366520Sfeldman ecget(ecbuf, totlen, off0)
7377263Ssam 	u_char *ecbuf;
7386520Sfeldman 	int totlen, off0;
7396520Sfeldman {
7407263Ssam 	register struct mbuf *m;
7417263Ssam 	struct mbuf *top = 0, **mp = &top;
7427263Ssam 	register int off = off0, len;
7437263Ssam 	u_char *cp;
7446520Sfeldman 
7459745Ssam 	cp = ecbuf + ECRDOFF + sizeof (struct ether_header);
7466520Sfeldman 	while (totlen > 0) {
7477263Ssam 		register int words;
7487263Ssam 		u_char *mcp;
7497263Ssam 
7509648Ssam 		MGET(m, M_DONTWAIT, MT_DATA);
7516520Sfeldman 		if (m == 0)
7526520Sfeldman 			goto bad;
7536520Sfeldman 		if (off) {
7546520Sfeldman 			len = totlen - off;
7559745Ssam 			cp = ecbuf + ECRDOFF +
7569745Ssam 				sizeof (struct ether_header) + off;
7576520Sfeldman 		} else
7586520Sfeldman 			len = totlen;
7596520Sfeldman 		if (len >= CLBYTES) {
7606520Sfeldman 			struct mbuf *p;
7616520Sfeldman 
7626520Sfeldman 			MCLGET(p, 1);
7636520Sfeldman 			if (p != 0) {
7646520Sfeldman 				m->m_len = len = CLBYTES;
7656520Sfeldman 				m->m_off = (int)p - (int)m;
7666520Sfeldman 			} else {
7676520Sfeldman 				m->m_len = len = MIN(MLEN, len);
7686520Sfeldman 				m->m_off = MMINOFF;
7696520Sfeldman 			}
7706520Sfeldman 		} else {
7716520Sfeldman 			m->m_len = len = MIN(MLEN, len);
7726520Sfeldman 			m->m_off = MMINOFF;
7736520Sfeldman 		}
7747263Ssam 		mcp = mtod(m, u_char *);
7757263Ssam 		if (words = (len >> 1)) {
7767263Ssam 			register u_short *to, *from;
7777263Ssam 
7787263Ssam 			to = (u_short *)mcp;
7797263Ssam 			from = (u_short *)cp;
7807263Ssam 			do
7817263Ssam 				*to++ = *from++;
7827263Ssam 			while (--words > 0);
7837263Ssam 			mcp = (u_char *)to;
7847263Ssam 			cp = (u_char *)from;
7857032Swnj 		}
7867216Ssam 		if (len & 01)
7876520Sfeldman 			*mcp++ = *cp++;
7886520Sfeldman 		*mp = m;
7896520Sfeldman 		mp = &m->m_next;
7907263Ssam 		if (off == 0) {
7916520Sfeldman 			totlen -= len;
7927263Ssam 			continue;
7937263Ssam 		}
7947263Ssam 		off += len;
7957263Ssam 		if (off == totlen) {
7969745Ssam 			cp = ecbuf + ECRDOFF + sizeof (struct ether_header);
7977263Ssam 			off = 0;
7987263Ssam 			totlen = off0;
7997263Ssam 		}
8006520Sfeldman 	}
8016520Sfeldman 	return (top);
8026520Sfeldman bad:
8036520Sfeldman 	m_freem(top);
8046520Sfeldman 	return (0);
8056520Sfeldman }
80613055Ssam 
80713055Ssam /*
80813055Ssam  * Process an ioctl request.
80913055Ssam  */
81013055Ssam ecioctl(ifp, cmd, data)
81113055Ssam 	register struct ifnet *ifp;
81213055Ssam 	int cmd;
81313055Ssam 	caddr_t data;
81413055Ssam {
81519863Skarels 	register struct ifaddr *ifa = (struct ifaddr *)data;
81613055Ssam 	int s = splimp(), error = 0;
81713055Ssam 
81813055Ssam 	switch (cmd) {
81913055Ssam 
82013055Ssam 	case SIOCSIFADDR:
82119863Skarels 		ifp->if_flags |= IFF_UP;
82213055Ssam 		ecinit(ifp->if_unit);
82319863Skarels 
82419863Skarels 		switch (ifa->ifa_addr.sa_family) {
825*23558Ssklower #ifdef INET
82619863Skarels 		case AF_INET:
82719863Skarels 			((struct arpcom *)ifp)->ac_ipaddr =
82819863Skarels 				IA_SIN(ifa)->sin_addr;
82919863Skarels 			arpwhohas((struct arpcom *)ifp, &IA_SIN(ifa)->sin_addr);
83019863Skarels 			break;
831*23558Ssklower #endif
832*23558Ssklower #ifdef NS
833*23558Ssklower 		case AF_NS:
834*23558Ssklower 		    {
835*23558Ssklower 			register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
836*23558Ssklower 
837*23558Ssklower 			if (ns_nullhost(*ina)) {
838*23558Ssklower 				ina->x_host = * (union ns_host *)
839*23558Ssklower 				     (ec_softc[ifp->if_unit].es_addr);
840*23558Ssklower 			} else {
841*23558Ssklower 				ec_setaddr(ina->x_host.c_host,ifp->if_unit);
842*23558Ssklower 			}
843*23558Ssklower 			break;
844*23558Ssklower 		    }
845*23558Ssklower #endif
84619863Skarels 		}
84713055Ssam 		break;
84813055Ssam 
84913055Ssam 	default:
85013055Ssam 		error = EINVAL;
85113055Ssam 	}
85213055Ssam 	splx(s);
85313055Ssam 	return (error);
85413055Ssam }
855*23558Ssklower 
856*23558Ssklower ec_setaddr(physaddr,unit)
857*23558Ssklower u_char *physaddr;
858*23558Ssklower int unit;
859*23558Ssklower {
860*23558Ssklower 	struct ec_softc *es = &ec_softc[unit];
861*23558Ssklower 	struct uba_device *ui = ecinfo[unit];
862*23558Ssklower 	register struct ecdevice *addr = (struct ecdevice *)ui->ui_addr;
863*23558Ssklower 	register char nibble;
864*23558Ssklower 	register int i, j;
865*23558Ssklower 	/*
866*23558Ssklower 	 * Use the ethernet address supplied
867*23558Ssklower 	 * Routine Courtesy Bill Nesheim, Cornell University.
868*23558Ssklower 	 */
869*23558Ssklower 
870*23558Ssklower 	addr->ec_rcr = 0;
871*23558Ssklower 	/* load address of first controller */
872*23558Ssklower 	for (i = 0; i < 6; i++) { /* 6 bytes of address */
873*23558Ssklower 	    es->es_addr[i] = physaddr[i];
874*23558Ssklower 	    nibble = physaddr[i] & 0xf; /* lower nibble */
875*23558Ssklower 	    addr->ec_rcr = (nibble << 8);
876*23558Ssklower 	    addr->ec_rcr = (nibble << 8) + EC_ASTEP; /* latch nibble */
877*23558Ssklower 	    addr->ec_rcr = (nibble << 8);
878*23558Ssklower 	    for (j=0; j < 4; j++) {
879*23558Ssklower 		addr->ec_rcr = 0;
880*23558Ssklower 		addr->ec_rcr = EC_ASTEP; /* step counter */
881*23558Ssklower 		addr->ec_rcr = 0;
882*23558Ssklower 	    }
883*23558Ssklower 	    nibble = (physaddr[i] >> 4) & 0xf; /* upper nibble */
884*23558Ssklower 	    addr->ec_rcr = (nibble << 8);
885*23558Ssklower 	    addr->ec_rcr = (nibble << 8) + EC_ASTEP; /* latch nibble */
886*23558Ssklower 	    addr->ec_rcr = (nibble << 8);
887*23558Ssklower 	    for (j=0; j < 4; j++) {
888*23558Ssklower 		addr->ec_rcr = 0;
889*23558Ssklower 		addr->ec_rcr = EC_ASTEP; /* step counter */
890*23558Ssklower 		addr->ec_rcr = 0;
891*23558Ssklower 	    }
892*23558Ssklower 	}
893*23558Ssklower }
894