xref: /csrg-svn/sys/vax/if/if_en.c (revision 13293)
1*13293Ssam /*	if_en.c	4.81	83/06/26	*/
24686Swnj 
34686Swnj #include "en.h"
45105Swnj 
54686Swnj /*
65105Swnj  * Xerox prototype (3 Mb) Ethernet interface driver.
74686Swnj  */
89796Ssam #include "../machine/pte.h"
94686Swnj 
104686Swnj #include "../h/param.h"
114686Swnj #include "../h/systm.h"
124686Swnj #include "../h/mbuf.h"
134686Swnj #include "../h/buf.h"
145083Swnj #include "../h/protosw.h"
155083Swnj #include "../h/socket.h"
165083Swnj #include "../h/vmmac.h"
1713054Ssam #include "../h/errno.h"
1813054Ssam #include "../h/ioctl.h"
198462Sroot 
208462Sroot #include "../net/if.h"
218462Sroot #include "../net/netisr.h"
228462Sroot #include "../net/route.h"
238418Swnj #include "../netinet/in.h"
248418Swnj #include "../netinet/in_systm.h"
258418Swnj #include "../netinet/ip.h"
268418Swnj #include "../netinet/ip_var.h"
278418Swnj #include "../netpup/pup.h"
284686Swnj 
298462Sroot #include "../vax/cpu.h"
308462Sroot #include "../vax/mtpr.h"
318462Sroot #include "../vaxif/if_en.h"
328462Sroot #include "../vaxif/if_enreg.h"
338462Sroot #include "../vaxif/if_uba.h"
348462Sroot #include "../vaxuba/ubareg.h"
358462Sroot #include "../vaxuba/ubavar.h"
368462Sroot 
375213Swnj #define	ENMTU	(1024+512)
386925Swnj #define	ENMRU	(1024+512+16)		/* 16 is enough to receive trailer */
395083Swnj 
404686Swnj int	enprobe(), enattach(), enrint(), enxint(), encollide();
414686Swnj struct	uba_device *eninfo[NEN];
424686Swnj u_short enstd[] = { 0 };
434686Swnj struct	uba_driver endriver =
445171Swnj 	{ enprobe, 0, enattach, 0, enstd, "en", eninfo };
454686Swnj #define	ENUNIT(x)	minor(x)
464686Swnj 
4713054Ssam int	eninit(),enoutput(),enreset(),enioctl();
485105Swnj 
49*13293Ssam #ifdef notdef
505105Swnj /*
51*13293Ssam  * If you need to byte swap IP's in the system, define
52*13293Ssam  * this and do a SIOCSIFFLAGS at boot time.
53*13293Ssam  */
54*13293Ssam #define	ENF_SWABIPS	0x100
55*13293Ssam #endif
56*13293Ssam 
57*13293Ssam /*
585105Swnj  * Ethernet software status per interface.
595105Swnj  *
605105Swnj  * Each interface is referenced by a network interface structure,
615105Swnj  * es_if, which the routing code uses to locate the interface.
625105Swnj  * This structure contains the output queue for the interface, its address, ...
635105Swnj  * We also have, for each interface, a UBA interface structure, which
645105Swnj  * contains information about the UNIBUS resources held by the interface:
655105Swnj  * map registers, buffered data paths, etc.  Information is cached in this
665105Swnj  * structure for use by the if_uba.c routines in running the interface
675105Swnj  * efficiently.
685105Swnj  */
695083Swnj struct	en_softc {
705105Swnj 	struct	ifnet es_if;		/* network-visible interface */
715105Swnj 	struct	ifuba es_ifuba;		/* UNIBUS resources */
725105Swnj 	short	es_delay;		/* current output delay */
735105Swnj 	short	es_mask;		/* mask for current output delay */
746471Sroot 	short	es_lastx;		/* host last transmitted to */
755105Swnj 	short	es_oactive;		/* is output active? */
765105Swnj 	short	es_olen;		/* length of last output */
775083Swnj } en_softc[NEN];
784686Swnj 
795105Swnj /*
805105Swnj  * Do output DMA to determine interface presence and
815105Swnj  * interrupt vector.  DMA is too short to disturb other hosts.
825105Swnj  */
834686Swnj enprobe(reg)
844686Swnj 	caddr_t reg;
854686Swnj {
865171Swnj 	register int br, cvec;		/* r11, r10 value-result */
874686Swnj 	register struct endevice *addr = (struct endevice *)reg;
884686Swnj 
894686Swnj #ifdef lint
904686Swnj 	br = 0; cvec = br; br = cvec;
914922Swnj 	enrint(0); enxint(0); encollide(0);
924686Swnj #endif
934686Swnj 	addr->en_istat = 0;
944686Swnj 	addr->en_owc = -1;
954686Swnj 	addr->en_oba = 0;
964771Swnj 	addr->en_ostat = EN_IEN|EN_GO;
974686Swnj 	DELAY(100000);
984686Swnj 	addr->en_ostat = 0;
994686Swnj 	return (1);
1004686Swnj }
1014686Swnj 
1025105Swnj /*
1035105Swnj  * Interface exists: make available by filling in network interface
1045105Swnj  * record.  System will initialize the interface when it is ready
1055105Swnj  * to accept packets.
1065105Swnj  */
1074686Swnj enattach(ui)
1084686Swnj 	struct uba_device *ui;
1094686Swnj {
1105105Swnj 	register struct en_softc *es = &en_softc[ui->ui_unit];
1114688Swnj 
1125105Swnj 	es->es_if.if_unit = ui->ui_unit;
1135171Swnj 	es->es_if.if_name = "en";
1145105Swnj 	es->es_if.if_mtu = ENMTU;
1155171Swnj 	es->es_if.if_init = eninit;
1165105Swnj 	es->es_if.if_output = enoutput;
11713054Ssam 	es->es_if.if_ioctl = enioctl;
1188978Sroot 	es->es_if.if_reset = enreset;
1196554Ssam 	es->es_ifuba.ifu_flags = UBA_NEEDBDP | UBA_NEED16 | UBA_CANTWAIT;
1209271Ssam #if defined(VAX750)
1219271Ssam 	/* don't chew up 750 bdp's */
1229271Ssam 	if (cpu == VAX_750 && ui->ui_unit > 0)
1239271Ssam 		es->es_ifuba.ifu_flags &= ~UBA_NEEDBDP;
1249271Ssam #endif
1255160Swnj 	if_attach(&es->es_if);
1264686Swnj }
1274686Swnj 
1285105Swnj /*
1295105Swnj  * Reset of interface after UNIBUS reset.
1305105Swnj  * If interface is on specified uba, reset its state.
1315105Swnj  */
1325105Swnj enreset(unit, uban)
1335105Swnj 	int unit, uban;
1345105Swnj {
1355105Swnj 	register struct uba_device *ui;
1365105Swnj 
1375171Swnj 	if (unit >= NEN || (ui = eninfo[unit]) == 0 || ui->ui_alive == 0 ||
1385171Swnj 	    ui->ui_ubanum != uban)
1395105Swnj 		return;
1405171Swnj 	printf(" en%d", unit);
1415105Swnj 	eninit(unit);
1425105Swnj }
1435105Swnj 
1445105Swnj /*
1455105Swnj  * Initialization of interface; clear recorded pending
1465105Swnj  * operations, and reinitialize UNIBUS usage.
1475105Swnj  */
1484688Swnj eninit(unit)
1494686Swnj 	int unit;
1505083Swnj {
1515171Swnj 	register struct en_softc *es = &en_softc[unit];
1525171Swnj 	register struct uba_device *ui = eninfo[unit];
1534686Swnj 	register struct endevice *addr;
15412349Ssam 	struct sockaddr_in *sin = (struct sockaddr_in *)&es->es_if.if_addr;
15513054Ssam 	int s;
1564686Swnj 
15713054Ssam 	if (in_netof(sin->sin_addr) == 0)
15812349Ssam 		return;
1595105Swnj 	if (if_ubainit(&es->es_ifuba, ui->ui_ubanum,
1606925Swnj 	    sizeof (struct en_header), (int)btoc(ENMRU)) == 0) {
1615171Swnj 		printf("en%d: can't initialize\n", unit);
1626335Ssam 		es->es_if.if_flags &= ~IFF_UP;
1635083Swnj 		return;
1644686Swnj 	}
1654686Swnj 	addr = (struct endevice *)ui->ui_addr;
1665083Swnj 	addr->en_istat = addr->en_ostat = 0;
1674686Swnj 
1685105Swnj 	/*
1695171Swnj 	 * Hang a receive and start any
1705171Swnj 	 * pending writes by faking a transmit complete.
1715105Swnj 	 */
1725105Swnj 	s = splimp();
1735171Swnj 	addr->en_iba = es->es_ifuba.ifu_r.ifrw_info;
1746925Swnj 	addr->en_iwc = -(sizeof (struct en_header) + ENMRU) >> 1;
1755171Swnj 	addr->en_istat = EN_IEN|EN_GO;
1765171Swnj 	es->es_oactive = 1;
17713054Ssam 	es->es_if.if_flags |= IFF_UP|IFF_RUNNING;
1785105Swnj 	enxint(unit);
1795105Swnj 	splx(s);
1807151Swnj 	if_rtinit(&es->es_if, RTF_UP);
1814686Swnj }
1824686Swnj 
1836471Sroot int	enalldelay = 0;
1846951Swnj int	enlastdel = 50;
1856471Sroot int	enlastmask = (~0) << 5;
1865083Swnj 
1875105Swnj /*
1885105Swnj  * Start or restart output on interface.
1895105Swnj  * If interface is already active, then this is a retransmit
1905105Swnj  * after a collision, and just restuff registers and delay.
1915105Swnj  * If interface is not already active, get another datagram
1925105Swnj  * to send off of the interface queue, and map it to the interface
1935105Swnj  * before starting the output.
1945105Swnj  */
1954688Swnj enstart(dev)
1964686Swnj 	dev_t dev;
1974686Swnj {
1985171Swnj         int unit = ENUNIT(dev);
1995171Swnj 	struct uba_device *ui = eninfo[unit];
2005171Swnj 	register struct en_softc *es = &en_softc[unit];
2015083Swnj 	register struct endevice *addr;
2025083Swnj 	struct mbuf *m;
2035083Swnj 	int dest;
2044686Swnj 
2055083Swnj 	if (es->es_oactive)
2065083Swnj 		goto restart;
2075105Swnj 
2085105Swnj 	/*
2095105Swnj 	 * Not already active: dequeue another request
2105105Swnj 	 * and map it to the UNIBUS.  If no more requests,
2115105Swnj 	 * just return.
2125105Swnj 	 */
2135105Swnj 	IF_DEQUEUE(&es->es_if.if_snd, m);
2145083Swnj 	if (m == 0) {
2155083Swnj 		es->es_oactive = 0;
2164686Swnj 		return;
2174686Swnj 	}
2185213Swnj 	dest = mtod(m, struct en_header *)->en_dhost;
2195105Swnj 	es->es_olen = if_wubaput(&es->es_ifuba, m);
220*13293Ssam #ifdef ENF_SWABIPS
221*13293Ssam 	/*
222*13293Ssam 	 * The Xerox interface does word at a time DMA, so
223*13293Ssam 	 * someone must do byte swapping of user data if high
224*13293Ssam 	 * and low ender machines are to communicate.  It doesn't
225*13293Ssam 	 * belong here, but certain people depend on it, so...
226*13293Ssam 	 *
227*13293Ssam 	 * Should swab everybody, but this is a kludge anyway.
228*13293Ssam 	 */
229*13293Ssam 	if (es->es_if.if_flags & ENF_SWABIPS) {
230*13293Ssam 		register struct en_header *en;
2315105Swnj 
232*13293Ssam 		en = (struct en_header *)es->es_ifuba.ifu_w.ifrw_addr;
233*13293Ssam 		if (en->en_type == ENTYPE_IP)
234*13293Ssam 			enswab((caddr_t)(en + 1), (caddr_t)(en + 1),
235*13293Ssam 			    es->es_olen - sizeof (struct en_header) + 1);
236*13293Ssam 	}
237*13293Ssam #endif
238*13293Ssam 
2395105Swnj 	/*
2405105Swnj 	 * Ethernet cannot take back-to-back packets (no
2416471Sroot 	 * buffering in interface.  To help avoid overrunning
2426471Sroot 	 * receivers, enforce a small delay (about 1ms) in interface:
2436471Sroot 	 *	* between all packets when enalldelay
2446471Sroot 	 *	* whenever last packet was broadcast
2456471Sroot 	 *	* whenever this packet is to same host as last packet
2465105Swnj 	 */
2476471Sroot 	if (enalldelay || es->es_lastx == 0 || es->es_lastx == dest) {
2485083Swnj 		es->es_delay = enlastdel;
2496471Sroot 		es->es_mask = enlastmask;
2506471Sroot 	}
2516471Sroot 	es->es_lastx = dest;
2525105Swnj 
2535083Swnj restart:
2545105Swnj 	/*
2555105Swnj 	 * Have request mapped to UNIBUS for transmission.
2565105Swnj 	 * Purge any stale data from this BDP, and start the otput.
2575105Swnj 	 */
2586335Ssam 	if (es->es_ifuba.ifu_flags & UBA_NEEDBDP)
2596335Ssam 		UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_w.ifrw_bdp);
2604686Swnj 	addr = (struct endevice *)ui->ui_addr;
2615160Swnj 	addr->en_oba = (int)es->es_ifuba.ifu_w.ifrw_info;
2625083Swnj 	addr->en_odelay = es->es_delay;
2635083Swnj 	addr->en_owc = -((es->es_olen + 1) >> 1);
2644771Swnj 	addr->en_ostat = EN_IEN|EN_GO;
2655083Swnj 	es->es_oactive = 1;
2664686Swnj }
2674686Swnj 
2685105Swnj /*
2695105Swnj  * Ethernet interface transmitter interrupt.
2705105Swnj  * Start another output if more data to send.
2715105Swnj  */
2724686Swnj enxint(unit)
2734686Swnj 	int unit;
2744686Swnj {
2755171Swnj 	register struct uba_device *ui = eninfo[unit];
2765171Swnj 	register struct en_softc *es = &en_softc[unit];
2776242Sroot 	register struct endevice *addr = (struct endevice *)ui->ui_addr;
2784686Swnj 
2795083Swnj 	if (es->es_oactive == 0)
2805083Swnj 		return;
2816242Sroot 	if (es->es_mask && (addr->en_ostat&EN_OERROR)) {
2826242Sroot 		es->es_if.if_oerrors++;
2836242Sroot 		endocoll(unit);
2846242Sroot 		return;
2856242Sroot 	}
2865171Swnj 	es->es_if.if_opackets++;
2875083Swnj 	es->es_oactive = 0;
2885083Swnj 	es->es_delay = 0;
2895083Swnj 	es->es_mask = ~0;
2905696Sroot 	if (es->es_ifuba.ifu_xtofree) {
2915696Sroot 		m_freem(es->es_ifuba.ifu_xtofree);
2925696Sroot 		es->es_ifuba.ifu_xtofree = 0;
2935696Sroot 	}
2945105Swnj 	if (es->es_if.if_snd.ifq_head == 0) {
2956471Sroot 		es->es_lastx = 256;		/* putatively illegal */
2964686Swnj 		return;
2974686Swnj 	}
2985083Swnj 	enstart(unit);
2994686Swnj }
3004686Swnj 
3015105Swnj /*
3025105Swnj  * Collision on ethernet interface.  Do exponential
3035105Swnj  * backoff, and retransmit.  If have backed off all
3046335Ssam  * the way print warning diagnostic, and drop packet.
3055105Swnj  */
3064686Swnj encollide(unit)
3074686Swnj 	int unit;
3084686Swnj {
3096242Sroot 	struct en_softc *es = &en_softc[unit];
3104686Swnj 
3115105Swnj 	es->es_if.if_collisions++;
3125083Swnj 	if (es->es_oactive == 0)
3134686Swnj 		return;
3146242Sroot 	endocoll(unit);
3156242Sroot }
3166242Sroot 
3176242Sroot endocoll(unit)
3186242Sroot 	int unit;
3196242Sroot {
3206242Sroot 	register struct en_softc *es = &en_softc[unit];
3216242Sroot 
3225171Swnj 	/*
3235171Swnj 	 * Es_mask is a 16 bit number with n low zero bits, with
3245171Swnj 	 * n the number of backoffs.  When es_mask is 0 we have
3255171Swnj 	 * backed off 16 times, and give up.
3265171Swnj 	 */
3275083Swnj 	if (es->es_mask == 0) {
3285213Swnj 		printf("en%d: send error\n", unit);
3295083Swnj 		enxint(unit);
3305171Swnj 		return;
3314686Swnj 	}
3325171Swnj 	/*
3335171Swnj 	 * Another backoff.  Restart with delay based on n low bits
3345171Swnj 	 * of the interval timer.
3355171Swnj 	 */
3365171Swnj 	es->es_mask <<= 1;
3375171Swnj 	es->es_delay = mfpr(ICR) &~ es->es_mask;
3385171Swnj 	enstart(unit);
3394686Swnj }
3404686Swnj 
3416027Ssam struct	sockaddr_pup pupsrc = { AF_PUP };
3426027Ssam struct	sockaddr_pup pupdst = { AF_PUP };
3436027Ssam struct	sockproto pupproto = { PF_PUP };
3445105Swnj /*
3455105Swnj  * Ethernet interface receiver interrupt.
3465105Swnj  * If input error just drop packet.
3475105Swnj  * Otherwise purge input buffered data path and examine
3485105Swnj  * packet to determine type.  If can't determine length
3495105Swnj  * from type, then have to drop packet.  Othewise decapsulate
3505105Swnj  * packet based on type and pass to type specific higher-level
3515105Swnj  * input routine.
3525105Swnj  */
3534686Swnj enrint(unit)
3544686Swnj 	int unit;
3554686Swnj {
3565171Swnj 	register struct en_softc *es = &en_softc[unit];
3575171Swnj 	struct endevice *addr = (struct endevice *)eninfo[unit]->ui_addr;
3585171Swnj 	register struct en_header *en;
3595083Swnj     	struct mbuf *m;
3608603Sroot 	int len; short resid;
3615171Swnj 	register struct ifqueue *inq;
3625083Swnj 	int off;
3634686Swnj 
3645171Swnj 	es->es_if.if_ipackets++;
3655105Swnj 
3665105Swnj 	/*
3675171Swnj 	 * Purge BDP; drop if input error indicated.
3685105Swnj 	 */
3696335Ssam 	if (es->es_ifuba.ifu_flags & UBA_NEEDBDP)
3706335Ssam 		UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_r.ifrw_bdp);
3714771Swnj 	if (addr->en_istat&EN_IERROR) {
3725105Swnj 		es->es_if.if_ierrors++;
3735083Swnj 		goto setup;
3744686Swnj 	}
3755105Swnj 
3765105Swnj 	/*
3776471Sroot 	 * Calculate input data length.
3785105Swnj 	 * Get pointer to ethernet header (in input buffer).
3795105Swnj 	 * Deal with trailer protocol: if type is PUP trailer
3805105Swnj 	 * get true type from first 16-bit word past data.
3815105Swnj 	 * Remember that type was trailer by setting off.
3825105Swnj 	 */
3836471Sroot 	resid = addr->en_iwc;
3846471Sroot 	if (resid)
3856471Sroot 		resid |= 0176000;
3866925Swnj 	len = (((sizeof (struct en_header) + ENMRU) >> 1) + resid) << 1;
3876471Sroot 	len -= sizeof (struct en_header);
3886925Swnj 	if (len > ENMRU)
3896471Sroot 		goto setup;			/* sanity */
3905105Swnj 	en = (struct en_header *)(es->es_ifuba.ifu_r.ifrw_addr);
39112352Ssam 	en->en_type = ntohs(en->en_type);
3925083Swnj #define	endataaddr(en, off, type)	((type)(((caddr_t)((en)+1)+(off))))
39312352Ssam 	if (en->en_type >= ENTYPE_TRAIL &&
39412352Ssam 	    en->en_type < ENTYPE_TRAIL+ENTYPE_NTRAILER) {
39512352Ssam 		off = (en->en_type - ENTYPE_TRAIL) * 512;
3966925Swnj 		if (off > ENMTU)
3975171Swnj 			goto setup;		/* sanity */
39812352Ssam 		en->en_type = ntohs(*endataaddr(en, off, u_short *));
39912352Ssam 		resid = ntohs(*(endataaddr(en, off+2, u_short *)));
4006471Sroot 		if (off + resid > len)
4016471Sroot 			goto setup;		/* sanity */
4026471Sroot 		len = off + resid;
4035083Swnj 	} else
4045083Swnj 		off = 0;
4055083Swnj 	if (len == 0)
4065083Swnj 		goto setup;
407*13293Ssam #ifdef ENF_SWABIPS
408*13293Ssam 	if (es->es_if.if_flags & ENF_SWABIPS && en->en_type == ENTYPE_IP)
409*13293Ssam 		enswab((caddr_t)(en + 1), (caddr_t)(en + 1), len);
410*13293Ssam #endif
4115105Swnj 	/*
4125105Swnj 	 * Pull packet off interface.  Off is nonzero if packet
4135105Swnj 	 * has trailing header; if_rubaget will then force this header
4145105Swnj 	 * information to be at the front, but we still have to drop
4156471Sroot 	 * the type and length which are at the front of any trailer data.
4165105Swnj 	 */
4175105Swnj 	m = if_rubaget(&es->es_ifuba, len, off);
4185213Swnj 	if (m == 0)
4195213Swnj 		goto setup;
4205105Swnj 	if (off) {
4216471Sroot 		m->m_off += 2 * sizeof (u_short);
4226471Sroot 		m->m_len -= 2 * sizeof (u_short);
4235105Swnj 	}
4246027Ssam 	switch (en->en_type) {
4256027Ssam 
4266027Ssam #ifdef INET
42712352Ssam 	case ENTYPE_IP:
4286260Swnj 		schednetisr(NETISR_IP);
4296027Ssam 		inq = &ipintrq;
4306027Ssam 		break;
4316027Ssam #endif
4326335Ssam #ifdef PUP
43312352Ssam 	case ENTYPE_PUP: {
4346027Ssam 		struct pup_header *pup = mtod(m, struct pup_header *);
4356027Ssam 
4366027Ssam 		pupproto.sp_protocol = pup->pup_type;
43712833Ssam 		bcopy((caddr_t)pup->pup_dnet, (caddr_t)pupdst.spup_net,
43812833Ssam 		    sizeof (struct pupport));
43912833Ssam 		bcopy((caddr_t)pup->pup_snet, (caddr_t)pupsrc.spup_net,
44012833Ssam 		    sizeof (struct pupport));
4416526Ssam 		raw_input(m, &pupproto, (struct sockaddr *)&pupsrc,
4426526Ssam 		  (struct sockaddr *)&pupdst);
4436027Ssam 		goto setup;
4446027Ssam 	}
4456335Ssam #endif
4466476Swnj 	default:
4476476Swnj 		m_freem(m);
4486476Swnj 		goto setup;
4496335Ssam 	}
4506335Ssam 
4516207Swnj 	if (IF_QFULL(inq)) {
4526207Swnj 		IF_DROP(inq);
4536335Ssam 		m_freem(m);
4546207Swnj 	} else
4556207Swnj 		IF_ENQUEUE(inq, m);
4565105Swnj 
4574688Swnj setup:
4585105Swnj 	/*
4595105Swnj 	 * Reset for next packet.
4605105Swnj 	 */
4615105Swnj 	addr->en_iba = es->es_ifuba.ifu_r.ifrw_info;
4626925Swnj 	addr->en_iwc = -(sizeof (struct en_header) + ENMRU) >> 1;
4634771Swnj 	addr->en_istat = EN_IEN|EN_GO;
4644688Swnj }
4654686Swnj 
4665083Swnj /*
4675083Swnj  * Ethernet output routine.
4685083Swnj  * Encapsulate a packet of type family for the local net.
4695105Swnj  * Use trailer local net encapsulation if enough data in first
4705105Swnj  * packet leaves a multiple of 512 bytes of data in remainder.
4715083Swnj  */
4726335Ssam enoutput(ifp, m0, dst)
4735083Swnj 	struct ifnet *ifp;
4745083Swnj 	struct mbuf *m0;
4756335Ssam 	struct sockaddr *dst;
4764686Swnj {
4776503Ssam 	int type, dest, s, error;
4785105Swnj 	register struct mbuf *m = m0;
4795083Swnj 	register struct en_header *en;
4806335Ssam 	register int off;
4814686Swnj 
4826335Ssam 	switch (dst->sa_family) {
4835083Swnj 
4845083Swnj #ifdef INET
4856335Ssam 	case AF_INET:
4866484Swnj 		dest = ((struct sockaddr_in *)dst)->sin_addr.s_addr;
4879178Ssam 		if (in_lnaof(*((struct in_addr *)&dest)) >= 0x100) {
4886503Ssam 			error = EPERM;		/* ??? */
4896486Swnj 			goto bad;
4906503Ssam 		}
4916484Swnj 		dest = (dest >> 24) & 0xff;
4926335Ssam 		off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len;
49313054Ssam 		/* need per host negotiation */
49413054Ssam 		if ((ifp->if_flags & IFF_NOTRAILERS) == 0)
4956335Ssam 		if (off > 0 && (off & 0x1ff) == 0 &&
4966471Sroot 		    m->m_off >= MMINOFF + 2 * sizeof (u_short)) {
49712352Ssam 			type = ENTYPE_TRAIL + (off>>9);
4986471Sroot 			m->m_off -= 2 * sizeof (u_short);
4996471Sroot 			m->m_len += 2 * sizeof (u_short);
50012352Ssam 			*mtod(m, u_short *) = htons((u_short)ENTYPE_IP);
50112352Ssam 			*(mtod(m, u_short *) + 1) = ntohs((u_short)m->m_len);
5025105Swnj 			goto gottrailertype;
5035083Swnj 		}
50412352Ssam 		type = ENTYPE_IP;
5055105Swnj 		off = 0;
5065105Swnj 		goto gottype;
5075083Swnj #endif
5086027Ssam #ifdef PUP
5096335Ssam 	case AF_PUP:
51012833Ssam 		dest = ((struct sockaddr_pup *)dst)->spup_host;
51112352Ssam 		type = ENTYPE_PUP;
5126027Ssam 		off = 0;
5136027Ssam 		goto gottype;
5146027Ssam #endif
5156027Ssam 
5165083Swnj 	default:
5176335Ssam 		printf("en%d: can't handle af%d\n", ifp->if_unit,
5186335Ssam 			dst->sa_family);
5196503Ssam 		error = EAFNOSUPPORT;
5206503Ssam 		goto bad;
5214686Swnj 	}
5225105Swnj 
5235171Swnj gottrailertype:
5245105Swnj 	/*
5255105Swnj 	 * Packet to be sent as trailer: move first packet
5265105Swnj 	 * (control information) to end of chain.
5275105Swnj 	 */
5285105Swnj 	while (m->m_next)
5295105Swnj 		m = m->m_next;
5305105Swnj 	m->m_next = m0;
5315105Swnj 	m = m0->m_next;
5325105Swnj 	m0->m_next = 0;
5335171Swnj 	m0 = m;
5345105Swnj 
5355171Swnj gottype:
5365105Swnj 	/*
5375105Swnj 	 * Add local net header.  If no space in first mbuf,
5385105Swnj 	 * allocate another.
5395105Swnj 	 */
5405213Swnj 	if (m->m_off > MMAXOFF ||
5415213Swnj 	    MMINOFF + sizeof (struct en_header) > m->m_off) {
5429649Ssam 		m = m_get(M_DONTWAIT, MT_HEADER);
5435083Swnj 		if (m == 0) {
5446503Ssam 			error = ENOBUFS;
5456503Ssam 			goto bad;
5465083Swnj 		}
5475083Swnj 		m->m_next = m0;
5485083Swnj 		m->m_off = MMINOFF;
5495083Swnj 		m->m_len = sizeof (struct en_header);
5505083Swnj 	} else {
5515083Swnj 		m->m_off -= sizeof (struct en_header);
5525083Swnj 		m->m_len += sizeof (struct en_header);
5535083Swnj 	}
5545083Swnj 	en = mtod(m, struct en_header *);
5555083Swnj 	en->en_shost = ifp->if_host[0];
5565083Swnj 	en->en_dhost = dest;
55712352Ssam 	en->en_type = htons((u_short)type);
5585105Swnj 
5595105Swnj 	/*
5605105Swnj 	 * Queue message on interface, and start output if interface
5615105Swnj 	 * not yet active.
5625105Swnj 	 */
5635083Swnj 	s = splimp();
5646207Swnj 	if (IF_QFULL(&ifp->if_snd)) {
5656207Swnj 		IF_DROP(&ifp->if_snd);
5666503Ssam 		error = ENOBUFS;
5676503Ssam 		goto qfull;
5686207Swnj 	}
5695083Swnj 	IF_ENQUEUE(&ifp->if_snd, m);
5705083Swnj 	if (en_softc[ifp->if_unit].es_oactive == 0)
5715083Swnj 		enstart(ifp->if_unit);
5725275Swnj 	splx(s);
5736503Ssam 	return (0);
5746503Ssam qfull:
5756503Ssam 	m0 = m;
5766503Ssam 	splx(s);
5776484Swnj bad:
5786503Ssam 	m_freem(m0);
5796503Ssam 	return (error);
5804686Swnj }
58113054Ssam 
58213054Ssam /*
58313054Ssam  * Process an ioctl request.
58413054Ssam  */
58513054Ssam enioctl(ifp, cmd, data)
58613054Ssam 	register struct ifnet *ifp;
58713054Ssam 	int cmd;
58813054Ssam 	caddr_t data;
58913054Ssam {
59013054Ssam 	struct ifreq *ifr = (struct ifreq *)data;
59113054Ssam 	int s = splimp(), error = 0;
59213054Ssam 
59313054Ssam 	switch (cmd) {
59413054Ssam 
59513054Ssam 	case SIOCSIFADDR:
59613054Ssam 		if (ifp->if_flags & IFF_RUNNING)
59713054Ssam 			if_rtinit(ifp, -1);	/* delete previous route */
59813054Ssam 		ensetaddr(ifp, (struct sockaddr_in *)&ifr->ifr_addr);
59913054Ssam 		if (ifp->if_flags & IFF_RUNNING)
60013054Ssam 			if_rtinit(ifp, RTF_UP);
60113054Ssam 		else
60213054Ssam 			eninit(ifp->if_unit);
60313054Ssam 		break;
60413054Ssam 
60513054Ssam 	default:
60613054Ssam 		error = EINVAL;
60713054Ssam 	}
60813054Ssam 	splx(s);
60913054Ssam 	return (error);
61013054Ssam }
61113054Ssam 
61213054Ssam ensetaddr(ifp, sin)
61313054Ssam 	register struct ifnet *ifp;
61413054Ssam 	register struct sockaddr_in *sin;
61513054Ssam {
61613054Ssam 	struct endevice *enaddr;
61713054Ssam 
61813054Ssam 	ifp->if_net = in_netof(sin->sin_addr);
61913054Ssam 	enaddr = (struct endevice *)eninfo[ifp->if_unit]->ui_addr;
62013054Ssam 	ifp->if_host[0] = (~enaddr->en_addr) & 0xff;
62113054Ssam 	sin = (struct sockaddr_in *)&ifp->if_addr;
62213054Ssam 	sin->sin_family = AF_INET;
62313054Ssam 	sin->sin_addr = if_makeaddr(ifp->if_net, ifp->if_host[0]);
62413054Ssam 	sin = (struct sockaddr_in *)&ifp->if_broadaddr;
62513054Ssam 	sin->sin_family = AF_INET;
62613054Ssam 	sin->sin_addr = if_makeaddr(ifp->if_net, INADDR_ANY);
62713054Ssam 	ifp->if_flags |= IFF_BROADCAST;
62813054Ssam }
629*13293Ssam 
630*13293Ssam #ifdef ENF_SWABIPS
631*13293Ssam /*
632*13293Ssam  * Swab bytes
633*13293Ssam  * Jeffrey Mogul, Stanford
634*13293Ssam  */
635*13293Ssam enswab(from, to, n)
636*13293Ssam 	register caddr_t *from, *to;
637*13293Ssam 	register int n;
638*13293Ssam {
639*13293Ssam 	register unsigned long temp;
640*13293Ssam 
641*13293Ssam 	n >>= 1; n++;
642*13293Ssam #define	STEP	temp = *from++,*to++ = *from++,*to++ = temp
643*13293Ssam 	/* round to multiple of 8 */
644*13293Ssam 	while ((--n) & 07)
645*13293Ssam 		STEP;
646*13293Ssam 	n >>= 3;
647*13293Ssam 	while (--n >= 0) {
648*13293Ssam 		STEP; STEP; STEP; STEP;
649*13293Ssam 		STEP; STEP; STEP; STEP;
650*13293Ssam 	}
651*13293Ssam }
652*13293Ssam #endif
653