xref: /csrg-svn/sys/vax/if/if_en.c (revision 16380)
1*16380Skarels /*	if_en.c	6.2	84/04/12	*/
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"
2813458Ssam #include "../netpup/ether.h"
294686Swnj 
308462Sroot #include "../vax/cpu.h"
318462Sroot #include "../vax/mtpr.h"
328462Sroot #include "../vaxif/if_en.h"
338462Sroot #include "../vaxif/if_enreg.h"
348462Sroot #include "../vaxif/if_uba.h"
358462Sroot #include "../vaxuba/ubareg.h"
368462Sroot #include "../vaxuba/ubavar.h"
378462Sroot 
385213Swnj #define	ENMTU	(1024+512)
396925Swnj #define	ENMRU	(1024+512+16)		/* 16 is enough to receive trailer */
405083Swnj 
414686Swnj int	enprobe(), enattach(), enrint(), enxint(), encollide();
424686Swnj struct	uba_device *eninfo[NEN];
434686Swnj u_short enstd[] = { 0 };
444686Swnj struct	uba_driver endriver =
455171Swnj 	{ enprobe, 0, enattach, 0, enstd, "en", eninfo };
464686Swnj #define	ENUNIT(x)	minor(x)
474686Swnj 
4813054Ssam int	eninit(),enoutput(),enreset(),enioctl();
495105Swnj 
5013293Ssam #ifdef notdef
515105Swnj /*
5213293Ssam  * If you need to byte swap IP's in the system, define
5313293Ssam  * this and do a SIOCSIFFLAGS at boot time.
5413293Ssam  */
5513293Ssam #define	ENF_SWABIPS	0x100
5613293Ssam #endif
5713293Ssam 
5813293Ssam /*
595105Swnj  * Ethernet software status per interface.
605105Swnj  *
615105Swnj  * Each interface is referenced by a network interface structure,
625105Swnj  * es_if, which the routing code uses to locate the interface.
635105Swnj  * This structure contains the output queue for the interface, its address, ...
645105Swnj  * We also have, for each interface, a UBA interface structure, which
655105Swnj  * contains information about the UNIBUS resources held by the interface:
665105Swnj  * map registers, buffered data paths, etc.  Information is cached in this
675105Swnj  * structure for use by the if_uba.c routines in running the interface
685105Swnj  * efficiently.
695105Swnj  */
705083Swnj struct	en_softc {
715105Swnj 	struct	ifnet es_if;		/* network-visible interface */
725105Swnj 	struct	ifuba es_ifuba;		/* UNIBUS resources */
73*16380Skarels 	short	es_host;		/* hardware host number */
745105Swnj 	short	es_delay;		/* current output delay */
755105Swnj 	short	es_mask;		/* mask for current output delay */
766471Sroot 	short	es_lastx;		/* host last transmitted to */
775105Swnj 	short	es_oactive;		/* is output active? */
785105Swnj 	short	es_olen;		/* length of last output */
795083Swnj } en_softc[NEN];
804686Swnj 
815105Swnj /*
825105Swnj  * Do output DMA to determine interface presence and
835105Swnj  * interrupt vector.  DMA is too short to disturb other hosts.
845105Swnj  */
854686Swnj enprobe(reg)
864686Swnj 	caddr_t reg;
874686Swnj {
885171Swnj 	register int br, cvec;		/* r11, r10 value-result */
894686Swnj 	register struct endevice *addr = (struct endevice *)reg;
904686Swnj 
914686Swnj #ifdef lint
924686Swnj 	br = 0; cvec = br; br = cvec;
934922Swnj 	enrint(0); enxint(0); encollide(0);
944686Swnj #endif
954686Swnj 	addr->en_istat = 0;
964686Swnj 	addr->en_owc = -1;
974686Swnj 	addr->en_oba = 0;
984771Swnj 	addr->en_ostat = EN_IEN|EN_GO;
994686Swnj 	DELAY(100000);
1004686Swnj 	addr->en_ostat = 0;
1014686Swnj 	return (1);
1024686Swnj }
1034686Swnj 
1045105Swnj /*
1055105Swnj  * Interface exists: make available by filling in network interface
1065105Swnj  * record.  System will initialize the interface when it is ready
1075105Swnj  * to accept packets.
1085105Swnj  */
1094686Swnj enattach(ui)
1104686Swnj 	struct uba_device *ui;
1114686Swnj {
1125105Swnj 	register struct en_softc *es = &en_softc[ui->ui_unit];
1134688Swnj 
1145105Swnj 	es->es_if.if_unit = ui->ui_unit;
1155171Swnj 	es->es_if.if_name = "en";
1165105Swnj 	es->es_if.if_mtu = ENMTU;
1175171Swnj 	es->es_if.if_init = eninit;
1185105Swnj 	es->es_if.if_output = enoutput;
11913054Ssam 	es->es_if.if_ioctl = enioctl;
1208978Sroot 	es->es_if.if_reset = enreset;
1216554Ssam 	es->es_ifuba.ifu_flags = UBA_NEEDBDP | UBA_NEED16 | UBA_CANTWAIT;
1229271Ssam #if defined(VAX750)
1239271Ssam 	/* don't chew up 750 bdp's */
1249271Ssam 	if (cpu == VAX_750 && ui->ui_unit > 0)
1259271Ssam 		es->es_ifuba.ifu_flags &= ~UBA_NEEDBDP;
1269271Ssam #endif
1275160Swnj 	if_attach(&es->es_if);
1284686Swnj }
1294686Swnj 
1305105Swnj /*
1315105Swnj  * Reset of interface after UNIBUS reset.
1325105Swnj  * If interface is on specified uba, reset its state.
1335105Swnj  */
1345105Swnj enreset(unit, uban)
1355105Swnj 	int unit, uban;
1365105Swnj {
1375105Swnj 	register struct uba_device *ui;
1385105Swnj 
1395171Swnj 	if (unit >= NEN || (ui = eninfo[unit]) == 0 || ui->ui_alive == 0 ||
1405171Swnj 	    ui->ui_ubanum != uban)
1415105Swnj 		return;
1425171Swnj 	printf(" en%d", unit);
1435105Swnj 	eninit(unit);
1445105Swnj }
1455105Swnj 
1465105Swnj /*
1475105Swnj  * Initialization of interface; clear recorded pending
1485105Swnj  * operations, and reinitialize UNIBUS usage.
1495105Swnj  */
1504688Swnj eninit(unit)
1514686Swnj 	int unit;
1525083Swnj {
1535171Swnj 	register struct en_softc *es = &en_softc[unit];
1545171Swnj 	register struct uba_device *ui = eninfo[unit];
1554686Swnj 	register struct endevice *addr;
15612349Ssam 	struct sockaddr_in *sin = (struct sockaddr_in *)&es->es_if.if_addr;
15713054Ssam 	int s;
1584686Swnj 
15913054Ssam 	if (in_netof(sin->sin_addr) == 0)
16012349Ssam 		return;
1615105Swnj 	if (if_ubainit(&es->es_ifuba, ui->ui_ubanum,
1626925Swnj 	    sizeof (struct en_header), (int)btoc(ENMRU)) == 0) {
1635171Swnj 		printf("en%d: can't initialize\n", unit);
1646335Ssam 		es->es_if.if_flags &= ~IFF_UP;
1655083Swnj 		return;
1664686Swnj 	}
1674686Swnj 	addr = (struct endevice *)ui->ui_addr;
1685083Swnj 	addr->en_istat = addr->en_ostat = 0;
1694686Swnj 
1705105Swnj 	/*
1715171Swnj 	 * Hang a receive and start any
1725171Swnj 	 * pending writes by faking a transmit complete.
1735105Swnj 	 */
1745105Swnj 	s = splimp();
1755171Swnj 	addr->en_iba = es->es_ifuba.ifu_r.ifrw_info;
1766925Swnj 	addr->en_iwc = -(sizeof (struct en_header) + ENMRU) >> 1;
1775171Swnj 	addr->en_istat = EN_IEN|EN_GO;
1785171Swnj 	es->es_oactive = 1;
17913054Ssam 	es->es_if.if_flags |= IFF_UP|IFF_RUNNING;
1805105Swnj 	enxint(unit);
1815105Swnj 	splx(s);
1827151Swnj 	if_rtinit(&es->es_if, RTF_UP);
1834686Swnj }
1844686Swnj 
1856471Sroot int	enalldelay = 0;
1866951Swnj int	enlastdel = 50;
1876471Sroot int	enlastmask = (~0) << 5;
1885083Swnj 
1895105Swnj /*
1905105Swnj  * Start or restart output on interface.
1915105Swnj  * If interface is already active, then this is a retransmit
1925105Swnj  * after a collision, and just restuff registers and delay.
1935105Swnj  * If interface is not already active, get another datagram
1945105Swnj  * to send off of the interface queue, and map it to the interface
1955105Swnj  * before starting the output.
1965105Swnj  */
1974688Swnj enstart(dev)
1984686Swnj 	dev_t dev;
1994686Swnj {
2005171Swnj         int unit = ENUNIT(dev);
2015171Swnj 	struct uba_device *ui = eninfo[unit];
2025171Swnj 	register struct en_softc *es = &en_softc[unit];
2035083Swnj 	register struct endevice *addr;
204*16380Skarels 	register struct en_header *en;
2055083Swnj 	struct mbuf *m;
2065083Swnj 	int dest;
2074686Swnj 
2085083Swnj 	if (es->es_oactive)
2095083Swnj 		goto restart;
2105105Swnj 
2115105Swnj 	/*
2125105Swnj 	 * Not already active: dequeue another request
2135105Swnj 	 * and map it to the UNIBUS.  If no more requests,
2145105Swnj 	 * just return.
2155105Swnj 	 */
2165105Swnj 	IF_DEQUEUE(&es->es_if.if_snd, m);
2175083Swnj 	if (m == 0) {
2185083Swnj 		es->es_oactive = 0;
2194686Swnj 		return;
2204686Swnj 	}
221*16380Skarels 	en = mtod(m, struct en_header *);
222*16380Skarels 	dest = en->en_dhost;
223*16380Skarels 	en->en_shost = es->es_host;
2245105Swnj 	es->es_olen = if_wubaput(&es->es_ifuba, m);
22513293Ssam #ifdef ENF_SWABIPS
22613293Ssam 	/*
22713293Ssam 	 * The Xerox interface does word at a time DMA, so
22813293Ssam 	 * someone must do byte swapping of user data if high
22913293Ssam 	 * and low ender machines are to communicate.  It doesn't
23013293Ssam 	 * belong here, but certain people depend on it, so...
23113293Ssam 	 *
23213293Ssam 	 * Should swab everybody, but this is a kludge anyway.
23313293Ssam 	 */
23413293Ssam 	if (es->es_if.if_flags & ENF_SWABIPS) {
23513293Ssam 		en = (struct en_header *)es->es_ifuba.ifu_w.ifrw_addr;
23613293Ssam 		if (en->en_type == ENTYPE_IP)
23713293Ssam 			enswab((caddr_t)(en + 1), (caddr_t)(en + 1),
23813293Ssam 			    es->es_olen - sizeof (struct en_header) + 1);
23913293Ssam 	}
24013293Ssam #endif
24113293Ssam 
2425105Swnj 	/*
2435105Swnj 	 * Ethernet cannot take back-to-back packets (no
2446471Sroot 	 * buffering in interface.  To help avoid overrunning
2456471Sroot 	 * receivers, enforce a small delay (about 1ms) in interface:
2466471Sroot 	 *	* between all packets when enalldelay
2476471Sroot 	 *	* whenever last packet was broadcast
2486471Sroot 	 *	* whenever this packet is to same host as last packet
2495105Swnj 	 */
2506471Sroot 	if (enalldelay || es->es_lastx == 0 || es->es_lastx == dest) {
2515083Swnj 		es->es_delay = enlastdel;
2526471Sroot 		es->es_mask = enlastmask;
2536471Sroot 	}
2546471Sroot 	es->es_lastx = dest;
2555105Swnj 
2565083Swnj restart:
2575105Swnj 	/*
2585105Swnj 	 * Have request mapped to UNIBUS for transmission.
2595105Swnj 	 * Purge any stale data from this BDP, and start the otput.
2605105Swnj 	 */
2616335Ssam 	if (es->es_ifuba.ifu_flags & UBA_NEEDBDP)
2626335Ssam 		UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_w.ifrw_bdp);
2634686Swnj 	addr = (struct endevice *)ui->ui_addr;
2645160Swnj 	addr->en_oba = (int)es->es_ifuba.ifu_w.ifrw_info;
2655083Swnj 	addr->en_odelay = es->es_delay;
2665083Swnj 	addr->en_owc = -((es->es_olen + 1) >> 1);
2674771Swnj 	addr->en_ostat = EN_IEN|EN_GO;
2685083Swnj 	es->es_oactive = 1;
2694686Swnj }
2704686Swnj 
2715105Swnj /*
2725105Swnj  * Ethernet interface transmitter interrupt.
2735105Swnj  * Start another output if more data to send.
2745105Swnj  */
2754686Swnj enxint(unit)
2764686Swnj 	int unit;
2774686Swnj {
2785171Swnj 	register struct uba_device *ui = eninfo[unit];
2795171Swnj 	register struct en_softc *es = &en_softc[unit];
2806242Sroot 	register struct endevice *addr = (struct endevice *)ui->ui_addr;
2814686Swnj 
2825083Swnj 	if (es->es_oactive == 0)
2835083Swnj 		return;
2846242Sroot 	if (es->es_mask && (addr->en_ostat&EN_OERROR)) {
2856242Sroot 		es->es_if.if_oerrors++;
2866242Sroot 		endocoll(unit);
2876242Sroot 		return;
2886242Sroot 	}
2895171Swnj 	es->es_if.if_opackets++;
2905083Swnj 	es->es_oactive = 0;
2915083Swnj 	es->es_delay = 0;
2925083Swnj 	es->es_mask = ~0;
2935696Sroot 	if (es->es_ifuba.ifu_xtofree) {
2945696Sroot 		m_freem(es->es_ifuba.ifu_xtofree);
2955696Sroot 		es->es_ifuba.ifu_xtofree = 0;
2965696Sroot 	}
2975105Swnj 	if (es->es_if.if_snd.ifq_head == 0) {
2986471Sroot 		es->es_lastx = 256;		/* putatively illegal */
2994686Swnj 		return;
3004686Swnj 	}
3015083Swnj 	enstart(unit);
3024686Swnj }
3034686Swnj 
3045105Swnj /*
3055105Swnj  * Collision on ethernet interface.  Do exponential
3065105Swnj  * backoff, and retransmit.  If have backed off all
3076335Ssam  * the way print warning diagnostic, and drop packet.
3085105Swnj  */
3094686Swnj encollide(unit)
3104686Swnj 	int unit;
3114686Swnj {
3126242Sroot 	struct en_softc *es = &en_softc[unit];
3134686Swnj 
3145105Swnj 	es->es_if.if_collisions++;
3155083Swnj 	if (es->es_oactive == 0)
3164686Swnj 		return;
3176242Sroot 	endocoll(unit);
3186242Sroot }
3196242Sroot 
3206242Sroot endocoll(unit)
3216242Sroot 	int unit;
3226242Sroot {
3236242Sroot 	register struct en_softc *es = &en_softc[unit];
3246242Sroot 
3255171Swnj 	/*
3265171Swnj 	 * Es_mask is a 16 bit number with n low zero bits, with
3275171Swnj 	 * n the number of backoffs.  When es_mask is 0 we have
3285171Swnj 	 * backed off 16 times, and give up.
3295171Swnj 	 */
3305083Swnj 	if (es->es_mask == 0) {
3315213Swnj 		printf("en%d: send error\n", unit);
3325083Swnj 		enxint(unit);
3335171Swnj 		return;
3344686Swnj 	}
3355171Swnj 	/*
3365171Swnj 	 * Another backoff.  Restart with delay based on n low bits
3375171Swnj 	 * of the interval timer.
3385171Swnj 	 */
3395171Swnj 	es->es_mask <<= 1;
3405171Swnj 	es->es_delay = mfpr(ICR) &~ es->es_mask;
3415171Swnj 	enstart(unit);
3424686Swnj }
3434686Swnj 
34413458Ssam #ifdef notdef
34513458Ssam struct	sockproto enproto = { AF_ETHERLINK };
34613458Ssam struct	sockaddr_en endst = { AF_ETHERLINK };
34713458Ssam struct	sockaddr_en ensrc = { AF_ETHERLINK };
34813458Ssam #endif
3495105Swnj /*
3505105Swnj  * Ethernet interface receiver interrupt.
3515105Swnj  * If input error just drop packet.
3525105Swnj  * Otherwise purge input buffered data path and examine
3535105Swnj  * packet to determine type.  If can't determine length
3545105Swnj  * from type, then have to drop packet.  Othewise decapsulate
3555105Swnj  * packet based on type and pass to type specific higher-level
3565105Swnj  * input routine.
3575105Swnj  */
3584686Swnj enrint(unit)
3594686Swnj 	int unit;
3604686Swnj {
3615171Swnj 	register struct en_softc *es = &en_softc[unit];
3625171Swnj 	struct endevice *addr = (struct endevice *)eninfo[unit]->ui_addr;
3635171Swnj 	register struct en_header *en;
3645083Swnj     	struct mbuf *m;
3658603Sroot 	int len; short resid;
3665171Swnj 	register struct ifqueue *inq;
3675083Swnj 	int off;
3684686Swnj 
3695171Swnj 	es->es_if.if_ipackets++;
3705105Swnj 
3715105Swnj 	/*
3725171Swnj 	 * Purge BDP; drop if input error indicated.
3735105Swnj 	 */
3746335Ssam 	if (es->es_ifuba.ifu_flags & UBA_NEEDBDP)
3756335Ssam 		UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_r.ifrw_bdp);
3764771Swnj 	if (addr->en_istat&EN_IERROR) {
3775105Swnj 		es->es_if.if_ierrors++;
3785083Swnj 		goto setup;
3794686Swnj 	}
3805105Swnj 
3815105Swnj 	/*
3826471Sroot 	 * Calculate input data length.
3835105Swnj 	 * Get pointer to ethernet header (in input buffer).
3845105Swnj 	 * Deal with trailer protocol: if type is PUP trailer
3855105Swnj 	 * get true type from first 16-bit word past data.
3865105Swnj 	 * Remember that type was trailer by setting off.
3875105Swnj 	 */
3886471Sroot 	resid = addr->en_iwc;
3896471Sroot 	if (resid)
3906471Sroot 		resid |= 0176000;
3916925Swnj 	len = (((sizeof (struct en_header) + ENMRU) >> 1) + resid) << 1;
3926471Sroot 	len -= sizeof (struct en_header);
3936925Swnj 	if (len > ENMRU)
3946471Sroot 		goto setup;			/* sanity */
3955105Swnj 	en = (struct en_header *)(es->es_ifuba.ifu_r.ifrw_addr);
39612352Ssam 	en->en_type = ntohs(en->en_type);
3975083Swnj #define	endataaddr(en, off, type)	((type)(((caddr_t)((en)+1)+(off))))
39812352Ssam 	if (en->en_type >= ENTYPE_TRAIL &&
39912352Ssam 	    en->en_type < ENTYPE_TRAIL+ENTYPE_NTRAILER) {
40012352Ssam 		off = (en->en_type - ENTYPE_TRAIL) * 512;
4016925Swnj 		if (off > ENMTU)
4025171Swnj 			goto setup;		/* sanity */
40312352Ssam 		en->en_type = ntohs(*endataaddr(en, off, u_short *));
40412352Ssam 		resid = ntohs(*(endataaddr(en, off+2, u_short *)));
4056471Sroot 		if (off + resid > len)
4066471Sroot 			goto setup;		/* sanity */
4076471Sroot 		len = off + resid;
4085083Swnj 	} else
4095083Swnj 		off = 0;
4105083Swnj 	if (len == 0)
4115083Swnj 		goto setup;
41213293Ssam #ifdef ENF_SWABIPS
41313293Ssam 	if (es->es_if.if_flags & ENF_SWABIPS && en->en_type == ENTYPE_IP)
41413293Ssam 		enswab((caddr_t)(en + 1), (caddr_t)(en + 1), len);
41513293Ssam #endif
4165105Swnj 	/*
4175105Swnj 	 * Pull packet off interface.  Off is nonzero if packet
4185105Swnj 	 * has trailing header; if_rubaget will then force this header
4195105Swnj 	 * information to be at the front, but we still have to drop
4206471Sroot 	 * the type and length which are at the front of any trailer data.
4215105Swnj 	 */
4225105Swnj 	m = if_rubaget(&es->es_ifuba, len, off);
4235213Swnj 	if (m == 0)
4245213Swnj 		goto setup;
4255105Swnj 	if (off) {
4266471Sroot 		m->m_off += 2 * sizeof (u_short);
4276471Sroot 		m->m_len -= 2 * sizeof (u_short);
4285105Swnj 	}
4296027Ssam 	switch (en->en_type) {
4306027Ssam 
4316027Ssam #ifdef INET
43212352Ssam 	case ENTYPE_IP:
4336260Swnj 		schednetisr(NETISR_IP);
4346027Ssam 		inq = &ipintrq;
4356027Ssam 		break;
4366027Ssam #endif
4376335Ssam #ifdef PUP
43813458Ssam 	case ENTYPE_PUP:
43913458Ssam 		rpup_input(m);
4406027Ssam 		goto setup;
4416335Ssam #endif
4426476Swnj 	default:
44313458Ssam #ifdef notdef
44413458Ssam 		enproto.sp_protocol = en->en_type;
44513458Ssam 		endst.sen_host = en->en_dhost;
44613458Ssam 		endst.sen_net = ensrc.sen_net = es->es_if.if_net;
44713458Ssam 		ensrc.sen_host = en->en_shost;
44813458Ssam 		raw_input(m, &enproto,
44913458Ssam 		    (struct sockaddr *)&ensrc, (struct sockaddr *)&endst);
45013458Ssam #else
4516476Swnj 		m_freem(m);
45213458Ssam #endif
4536476Swnj 		goto setup;
4546335Ssam 	}
4556335Ssam 
4566207Swnj 	if (IF_QFULL(inq)) {
4576207Swnj 		IF_DROP(inq);
4586335Ssam 		m_freem(m);
4596207Swnj 	} else
4606207Swnj 		IF_ENQUEUE(inq, m);
4615105Swnj 
4624688Swnj setup:
4635105Swnj 	/*
4645105Swnj 	 * Reset for next packet.
4655105Swnj 	 */
4665105Swnj 	addr->en_iba = es->es_ifuba.ifu_r.ifrw_info;
4676925Swnj 	addr->en_iwc = -(sizeof (struct en_header) + ENMRU) >> 1;
4684771Swnj 	addr->en_istat = EN_IEN|EN_GO;
4694688Swnj }
4704686Swnj 
4715083Swnj /*
4725083Swnj  * Ethernet output routine.
4735083Swnj  * Encapsulate a packet of type family for the local net.
4745105Swnj  * Use trailer local net encapsulation if enough data in first
4755105Swnj  * packet leaves a multiple of 512 bytes of data in remainder.
4765083Swnj  */
4776335Ssam enoutput(ifp, m0, dst)
4785083Swnj 	struct ifnet *ifp;
4795083Swnj 	struct mbuf *m0;
4806335Ssam 	struct sockaddr *dst;
4814686Swnj {
4826503Ssam 	int type, dest, s, error;
4835105Swnj 	register struct mbuf *m = m0;
4845083Swnj 	register struct en_header *en;
4856335Ssam 	register int off;
4864686Swnj 
4876335Ssam 	switch (dst->sa_family) {
4885083Swnj 
4895083Swnj #ifdef INET
4906335Ssam 	case AF_INET:
4916484Swnj 		dest = ((struct sockaddr_in *)dst)->sin_addr.s_addr;
4929178Ssam 		if (in_lnaof(*((struct in_addr *)&dest)) >= 0x100) {
4936503Ssam 			error = EPERM;		/* ??? */
4946486Swnj 			goto bad;
4956503Ssam 		}
4966484Swnj 		dest = (dest >> 24) & 0xff;
4976335Ssam 		off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len;
49813054Ssam 		/* need per host negotiation */
49913054Ssam 		if ((ifp->if_flags & IFF_NOTRAILERS) == 0)
5006335Ssam 		if (off > 0 && (off & 0x1ff) == 0 &&
5016471Sroot 		    m->m_off >= MMINOFF + 2 * sizeof (u_short)) {
50212352Ssam 			type = ENTYPE_TRAIL + (off>>9);
5036471Sroot 			m->m_off -= 2 * sizeof (u_short);
5046471Sroot 			m->m_len += 2 * sizeof (u_short);
50512352Ssam 			*mtod(m, u_short *) = htons((u_short)ENTYPE_IP);
50612352Ssam 			*(mtod(m, u_short *) + 1) = ntohs((u_short)m->m_len);
5075105Swnj 			goto gottrailertype;
5085083Swnj 		}
50912352Ssam 		type = ENTYPE_IP;
5105105Swnj 		off = 0;
5115105Swnj 		goto gottype;
5125083Swnj #endif
5136027Ssam #ifdef PUP
5146335Ssam 	case AF_PUP:
51512833Ssam 		dest = ((struct sockaddr_pup *)dst)->spup_host;
51612352Ssam 		type = ENTYPE_PUP;
5176027Ssam 		off = 0;
5186027Ssam 		goto gottype;
5196027Ssam #endif
5206027Ssam 
52113458Ssam #ifdef notdef
52213458Ssam 	case AF_ETHERLINK:
52313458Ssam 		goto gotheader;
52413458Ssam #endif
52513458Ssam 
5265083Swnj 	default:
5276335Ssam 		printf("en%d: can't handle af%d\n", ifp->if_unit,
5286335Ssam 			dst->sa_family);
5296503Ssam 		error = EAFNOSUPPORT;
5306503Ssam 		goto bad;
5314686Swnj 	}
5325105Swnj 
5335171Swnj gottrailertype:
5345105Swnj 	/*
5355105Swnj 	 * Packet to be sent as trailer: move first packet
5365105Swnj 	 * (control information) to end of chain.
5375105Swnj 	 */
5385105Swnj 	while (m->m_next)
5395105Swnj 		m = m->m_next;
5405105Swnj 	m->m_next = m0;
5415105Swnj 	m = m0->m_next;
5425105Swnj 	m0->m_next = 0;
5435171Swnj 	m0 = m;
5445105Swnj 
5455171Swnj gottype:
5465105Swnj 	/*
5475105Swnj 	 * Add local net header.  If no space in first mbuf,
5485105Swnj 	 * allocate another.
5495105Swnj 	 */
5505213Swnj 	if (m->m_off > MMAXOFF ||
5515213Swnj 	    MMINOFF + sizeof (struct en_header) > m->m_off) {
5529649Ssam 		m = m_get(M_DONTWAIT, MT_HEADER);
5535083Swnj 		if (m == 0) {
5546503Ssam 			error = ENOBUFS;
5556503Ssam 			goto bad;
5565083Swnj 		}
5575083Swnj 		m->m_next = m0;
5585083Swnj 		m->m_off = MMINOFF;
5595083Swnj 		m->m_len = sizeof (struct en_header);
5605083Swnj 	} else {
5615083Swnj 		m->m_off -= sizeof (struct en_header);
5625083Swnj 		m->m_len += sizeof (struct en_header);
5635083Swnj 	}
5645083Swnj 	en = mtod(m, struct en_header *);
565*16380Skarels 	/* add en_shost later */
5665083Swnj 	en->en_dhost = dest;
56712352Ssam 	en->en_type = htons((u_short)type);
5685105Swnj 
56913458Ssam gotheader:
5705105Swnj 	/*
5715105Swnj 	 * Queue message on interface, and start output if interface
5725105Swnj 	 * not yet active.
5735105Swnj 	 */
5745083Swnj 	s = splimp();
5756207Swnj 	if (IF_QFULL(&ifp->if_snd)) {
5766207Swnj 		IF_DROP(&ifp->if_snd);
5776503Ssam 		error = ENOBUFS;
5786503Ssam 		goto qfull;
5796207Swnj 	}
5805083Swnj 	IF_ENQUEUE(&ifp->if_snd, m);
5815083Swnj 	if (en_softc[ifp->if_unit].es_oactive == 0)
5825083Swnj 		enstart(ifp->if_unit);
5835275Swnj 	splx(s);
5846503Ssam 	return (0);
5856503Ssam qfull:
5866503Ssam 	m0 = m;
5876503Ssam 	splx(s);
5886484Swnj bad:
5896503Ssam 	m_freem(m0);
5906503Ssam 	return (error);
5914686Swnj }
59213054Ssam 
59313054Ssam /*
59413054Ssam  * Process an ioctl request.
59513054Ssam  */
59613054Ssam enioctl(ifp, cmd, data)
59713054Ssam 	register struct ifnet *ifp;
59813054Ssam 	int cmd;
59913054Ssam 	caddr_t data;
60013054Ssam {
60113054Ssam 	struct ifreq *ifr = (struct ifreq *)data;
60213054Ssam 	int s = splimp(), error = 0;
60313054Ssam 
60413054Ssam 	switch (cmd) {
60513054Ssam 
60613054Ssam 	case SIOCSIFADDR:
60713054Ssam 		if (ifp->if_flags & IFF_RUNNING)
60813054Ssam 			if_rtinit(ifp, -1);	/* delete previous route */
60913054Ssam 		ensetaddr(ifp, (struct sockaddr_in *)&ifr->ifr_addr);
61013054Ssam 		if (ifp->if_flags & IFF_RUNNING)
61113054Ssam 			if_rtinit(ifp, RTF_UP);
61213054Ssam 		else
61313054Ssam 			eninit(ifp->if_unit);
61413054Ssam 		break;
61513054Ssam 
61613054Ssam 	default:
61713054Ssam 		error = EINVAL;
61813054Ssam 	}
61913054Ssam 	splx(s);
62013054Ssam 	return (error);
62113054Ssam }
62213054Ssam 
62313054Ssam ensetaddr(ifp, sin)
62413054Ssam 	register struct ifnet *ifp;
62513054Ssam 	register struct sockaddr_in *sin;
62613054Ssam {
62713054Ssam 	struct endevice *enaddr;
62813054Ssam 
629*16380Skarels 	/* set address once for in_netof, so subnets will be recognized */
630*16380Skarels 	ifp->if_addr = *(struct sockaddr *)sin;
63113054Ssam 	ifp->if_net = in_netof(sin->sin_addr);
63213054Ssam 	enaddr = (struct endevice *)eninfo[ifp->if_unit]->ui_addr;
633*16380Skarels 	((struct en_softc *) ifp)->es_host = (~enaddr->en_addr) & 0xff;
63413054Ssam 	sin = (struct sockaddr_in *)&ifp->if_addr;
63513054Ssam 	sin->sin_family = AF_INET;
636*16380Skarels 	sin->sin_addr = if_makeaddr(ifp->if_net,
637*16380Skarels 	    ((struct en_softc *)ifp)->es_host);
63813054Ssam 	sin = (struct sockaddr_in *)&ifp->if_broadaddr;
63913054Ssam 	sin->sin_family = AF_INET;
64013054Ssam 	sin->sin_addr = if_makeaddr(ifp->if_net, INADDR_ANY);
64113054Ssam 	ifp->if_flags |= IFF_BROADCAST;
64213054Ssam }
64313293Ssam 
64413293Ssam #ifdef ENF_SWABIPS
64513293Ssam /*
64613293Ssam  * Swab bytes
64713293Ssam  * Jeffrey Mogul, Stanford
64813293Ssam  */
64913293Ssam enswab(from, to, n)
65013293Ssam 	register caddr_t *from, *to;
65113293Ssam 	register int n;
65213293Ssam {
65313293Ssam 	register unsigned long temp;
65413293Ssam 
65513293Ssam 	n >>= 1; n++;
65613293Ssam #define	STEP	temp = *from++,*to++ = *from++,*to++ = temp
65713293Ssam 	/* round to multiple of 8 */
65813293Ssam 	while ((--n) & 07)
65913293Ssam 		STEP;
66013293Ssam 	n >>= 3;
66113293Ssam 	while (--n >= 0) {
66213293Ssam 		STEP; STEP; STEP; STEP;
66313293Ssam 		STEP; STEP; STEP; STEP;
66413293Ssam 	}
66513293Ssam }
66613293Ssam #endif
667