xref: /csrg-svn/sys/vax/if/if_en.c (revision 19864)
1*19864Skarels /*	if_en.c	6.6	85/05/01	*/
24686Swnj 
34686Swnj #include "en.h"
45105Swnj 
54686Swnj /*
65105Swnj  * Xerox prototype (3 Mb) Ethernet interface driver.
74686Swnj  */
89796Ssam #include "../machine/pte.h"
94686Swnj 
1017113Sbloom #include "param.h"
1117113Sbloom #include "systm.h"
1217113Sbloom #include "mbuf.h"
1317113Sbloom #include "buf.h"
1417113Sbloom #include "protosw.h"
1517113Sbloom #include "socket.h"
1617113Sbloom #include "vmmac.h"
1717113Sbloom #include "errno.h"
1817113Sbloom #include "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"
25*19864Skarels #include "../netinet/in_var.h"
268418Swnj #include "../netinet/ip.h"
278418Swnj #include "../netinet/ip_var.h"
288418Swnj #include "../netpup/pup.h"
2913458Ssam #include "../netpup/ether.h"
304686Swnj 
318462Sroot #include "../vax/cpu.h"
328462Sroot #include "../vax/mtpr.h"
3317113Sbloom #include "if_en.h"
3417113Sbloom #include "if_enreg.h"
3517113Sbloom #include "if_uba.h"
368462Sroot #include "../vaxuba/ubareg.h"
378462Sroot #include "../vaxuba/ubavar.h"
388462Sroot 
395213Swnj #define	ENMTU	(1024+512)
406925Swnj #define	ENMRU	(1024+512+16)		/* 16 is enough to receive trailer */
415083Swnj 
424686Swnj int	enprobe(), enattach(), enrint(), enxint(), encollide();
434686Swnj struct	uba_device *eninfo[NEN];
444686Swnj u_short enstd[] = { 0 };
454686Swnj struct	uba_driver endriver =
465171Swnj 	{ enprobe, 0, enattach, 0, enstd, "en", eninfo };
474686Swnj #define	ENUNIT(x)	minor(x)
484686Swnj 
4913054Ssam int	eninit(),enoutput(),enreset(),enioctl();
505105Swnj 
5113293Ssam #ifdef notdef
525105Swnj /*
5313293Ssam  * If you need to byte swap IP's in the system, define
5413293Ssam  * this and do a SIOCSIFFLAGS at boot time.
5513293Ssam  */
56*19864Skarels #define	ENF_SWABIPS	0x1000
5713293Ssam #endif
5813293Ssam 
5913293Ssam /*
605105Swnj  * Ethernet software status per interface.
615105Swnj  *
625105Swnj  * Each interface is referenced by a network interface structure,
635105Swnj  * es_if, which the routing code uses to locate the interface.
645105Swnj  * This structure contains the output queue for the interface, its address, ...
655105Swnj  * We also have, for each interface, a UBA interface structure, which
665105Swnj  * contains information about the UNIBUS resources held by the interface:
675105Swnj  * map registers, buffered data paths, etc.  Information is cached in this
685105Swnj  * structure for use by the if_uba.c routines in running the interface
695105Swnj  * efficiently.
705105Swnj  */
715083Swnj struct	en_softc {
725105Swnj 	struct	ifnet es_if;		/* network-visible interface */
735105Swnj 	struct	ifuba es_ifuba;		/* UNIBUS resources */
7416380Skarels 	short	es_host;		/* hardware host number */
755105Swnj 	short	es_delay;		/* current output delay */
765105Swnj 	short	es_mask;		/* mask for current output delay */
776471Sroot 	short	es_lastx;		/* host last transmitted to */
785105Swnj 	short	es_oactive;		/* is output active? */
795105Swnj 	short	es_olen;		/* length of last output */
805083Swnj } en_softc[NEN];
814686Swnj 
825105Swnj /*
835105Swnj  * Do output DMA to determine interface presence and
845105Swnj  * interrupt vector.  DMA is too short to disturb other hosts.
855105Swnj  */
864686Swnj enprobe(reg)
874686Swnj 	caddr_t reg;
884686Swnj {
895171Swnj 	register int br, cvec;		/* r11, r10 value-result */
904686Swnj 	register struct endevice *addr = (struct endevice *)reg;
914686Swnj 
924686Swnj #ifdef lint
934686Swnj 	br = 0; cvec = br; br = cvec;
944922Swnj 	enrint(0); enxint(0); encollide(0);
954686Swnj #endif
964686Swnj 	addr->en_istat = 0;
974686Swnj 	addr->en_owc = -1;
984686Swnj 	addr->en_oba = 0;
994771Swnj 	addr->en_ostat = EN_IEN|EN_GO;
1004686Swnj 	DELAY(100000);
1014686Swnj 	addr->en_ostat = 0;
1024686Swnj 	return (1);
1034686Swnj }
1044686Swnj 
1055105Swnj /*
1065105Swnj  * Interface exists: make available by filling in network interface
1075105Swnj  * record.  System will initialize the interface when it is ready
1085105Swnj  * to accept packets.
1095105Swnj  */
1104686Swnj enattach(ui)
1114686Swnj 	struct uba_device *ui;
1124686Swnj {
1135105Swnj 	register struct en_softc *es = &en_softc[ui->ui_unit];
1144688Swnj 
1155105Swnj 	es->es_if.if_unit = ui->ui_unit;
1165171Swnj 	es->es_if.if_name = "en";
1175105Swnj 	es->es_if.if_mtu = ENMTU;
118*19864Skarels 	es->es_if.if_flags = IFF_BROADCAST;
1195171Swnj 	es->es_if.if_init = eninit;
1205105Swnj 	es->es_if.if_output = enoutput;
12113054Ssam 	es->es_if.if_ioctl = enioctl;
1228978Sroot 	es->es_if.if_reset = enreset;
1236554Ssam 	es->es_ifuba.ifu_flags = UBA_NEEDBDP | UBA_NEED16 | UBA_CANTWAIT;
1249271Ssam #if defined(VAX750)
1259271Ssam 	/* don't chew up 750 bdp's */
1269271Ssam 	if (cpu == VAX_750 && ui->ui_unit > 0)
1279271Ssam 		es->es_ifuba.ifu_flags &= ~UBA_NEEDBDP;
1289271Ssam #endif
1295160Swnj 	if_attach(&es->es_if);
1304686Swnj }
1314686Swnj 
1325105Swnj /*
1335105Swnj  * Reset of interface after UNIBUS reset.
1345105Swnj  * If interface is on specified uba, reset its state.
1355105Swnj  */
1365105Swnj enreset(unit, uban)
1375105Swnj 	int unit, uban;
1385105Swnj {
1395105Swnj 	register struct uba_device *ui;
1405105Swnj 
1415171Swnj 	if (unit >= NEN || (ui = eninfo[unit]) == 0 || ui->ui_alive == 0 ||
1425171Swnj 	    ui->ui_ubanum != uban)
1435105Swnj 		return;
1445171Swnj 	printf(" en%d", unit);
1455105Swnj 	eninit(unit);
1465105Swnj }
1475105Swnj 
1485105Swnj /*
1495105Swnj  * Initialization of interface; clear recorded pending
1505105Swnj  * operations, and reinitialize UNIBUS usage.
1515105Swnj  */
1524688Swnj eninit(unit)
1534686Swnj 	int unit;
1545083Swnj {
1555171Swnj 	register struct en_softc *es = &en_softc[unit];
1565171Swnj 	register struct uba_device *ui = eninfo[unit];
1574686Swnj 	register struct endevice *addr;
15813054Ssam 	int s;
1594686Swnj 
160*19864Skarels 	if (es->es_if.if_addrlist == (struct ifaddr *)0)
16112349Ssam 		return;
1625105Swnj 	if (if_ubainit(&es->es_ifuba, ui->ui_ubanum,
1636925Swnj 	    sizeof (struct en_header), (int)btoc(ENMRU)) == 0) {
1645171Swnj 		printf("en%d: can't initialize\n", unit);
1656335Ssam 		es->es_if.if_flags &= ~IFF_UP;
1665083Swnj 		return;
1674686Swnj 	}
1684686Swnj 	addr = (struct endevice *)ui->ui_addr;
1695083Swnj 	addr->en_istat = addr->en_ostat = 0;
1704686Swnj 
1715105Swnj 	/*
1725171Swnj 	 * Hang a receive and start any
1735171Swnj 	 * pending writes by faking a transmit complete.
1745105Swnj 	 */
1755105Swnj 	s = splimp();
1765171Swnj 	addr->en_iba = es->es_ifuba.ifu_r.ifrw_info;
1776925Swnj 	addr->en_iwc = -(sizeof (struct en_header) + ENMRU) >> 1;
1785171Swnj 	addr->en_istat = EN_IEN|EN_GO;
1795171Swnj 	es->es_oactive = 1;
180*19864Skarels 	es->es_if.if_flags |= IFF_RUNNING;
1815105Swnj 	enxint(unit);
1825105Swnj 	splx(s);
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;
20416380Skarels 	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 	}
22116380Skarels 	en = mtod(m, struct en_header *);
22216380Skarels 	dest = en->en_dhost;
22316380Skarels 	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;
36716511Skarels 	int off, s;
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 
45616511Skarels 	s = splimp();
4576207Swnj 	if (IF_QFULL(inq)) {
4586207Swnj 		IF_DROP(inq);
4596335Ssam 		m_freem(m);
4606207Swnj 	} else
4616207Swnj 		IF_ENQUEUE(inq, m);
46216511Skarels 	splx(s);
4635105Swnj 
4644688Swnj setup:
4655105Swnj 	/*
4665105Swnj 	 * Reset for next packet.
4675105Swnj 	 */
4685105Swnj 	addr->en_iba = es->es_ifuba.ifu_r.ifrw_info;
4696925Swnj 	addr->en_iwc = -(sizeof (struct en_header) + ENMRU) >> 1;
4704771Swnj 	addr->en_istat = EN_IEN|EN_GO;
4714688Swnj }
4724686Swnj 
4735083Swnj /*
4745083Swnj  * Ethernet output routine.
4755083Swnj  * Encapsulate a packet of type family for the local net.
4765105Swnj  * Use trailer local net encapsulation if enough data in first
4775105Swnj  * packet leaves a multiple of 512 bytes of data in remainder.
4785083Swnj  */
4796335Ssam enoutput(ifp, m0, dst)
4805083Swnj 	struct ifnet *ifp;
4815083Swnj 	struct mbuf *m0;
4826335Ssam 	struct sockaddr *dst;
4834686Swnj {
4846503Ssam 	int type, dest, s, error;
4855105Swnj 	register struct mbuf *m = m0;
4865083Swnj 	register struct en_header *en;
4876335Ssam 	register int off;
4884686Swnj 
4896335Ssam 	switch (dst->sa_family) {
4905083Swnj 
4915083Swnj #ifdef INET
4926335Ssam 	case AF_INET:
493*19864Skarels 		{
494*19864Skarels 		struct in_addr in;
495*19864Skarels 
496*19864Skarels 		in = ((struct sockaddr_in *)dst)->sin_addr;
497*19864Skarels 		if (in_broadcast(in))
498*19864Skarels 			dest = EN_BROADCAST;
499*19864Skarels 		else
500*19864Skarels 			dest = in_lnaof(in);
501*19864Skarels 		}
502*19864Skarels 		if (dest >= 0x100) {
5036503Ssam 			error = EPERM;		/* ??? */
5046486Swnj 			goto bad;
5056503Ssam 		}
5066335Ssam 		off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len;
50713054Ssam 		/* need per host negotiation */
50813054Ssam 		if ((ifp->if_flags & IFF_NOTRAILERS) == 0)
5096335Ssam 		if (off > 0 && (off & 0x1ff) == 0 &&
5106471Sroot 		    m->m_off >= MMINOFF + 2 * sizeof (u_short)) {
51112352Ssam 			type = ENTYPE_TRAIL + (off>>9);
5126471Sroot 			m->m_off -= 2 * sizeof (u_short);
5136471Sroot 			m->m_len += 2 * sizeof (u_short);
51412352Ssam 			*mtod(m, u_short *) = htons((u_short)ENTYPE_IP);
51512352Ssam 			*(mtod(m, u_short *) + 1) = ntohs((u_short)m->m_len);
5165105Swnj 			goto gottrailertype;
5175083Swnj 		}
51812352Ssam 		type = ENTYPE_IP;
5195105Swnj 		off = 0;
5205105Swnj 		goto gottype;
5215083Swnj #endif
5226027Ssam #ifdef PUP
5236335Ssam 	case AF_PUP:
52412833Ssam 		dest = ((struct sockaddr_pup *)dst)->spup_host;
52512352Ssam 		type = ENTYPE_PUP;
5266027Ssam 		off = 0;
5276027Ssam 		goto gottype;
5286027Ssam #endif
5296027Ssam 
53013458Ssam #ifdef notdef
53113458Ssam 	case AF_ETHERLINK:
53213458Ssam 		goto gotheader;
53313458Ssam #endif
53413458Ssam 
5355083Swnj 	default:
5366335Ssam 		printf("en%d: can't handle af%d\n", ifp->if_unit,
5376335Ssam 			dst->sa_family);
5386503Ssam 		error = EAFNOSUPPORT;
5396503Ssam 		goto bad;
5404686Swnj 	}
5415105Swnj 
5425171Swnj gottrailertype:
5435105Swnj 	/*
5445105Swnj 	 * Packet to be sent as trailer: move first packet
5455105Swnj 	 * (control information) to end of chain.
5465105Swnj 	 */
5475105Swnj 	while (m->m_next)
5485105Swnj 		m = m->m_next;
5495105Swnj 	m->m_next = m0;
5505105Swnj 	m = m0->m_next;
5515105Swnj 	m0->m_next = 0;
5525171Swnj 	m0 = m;
5535105Swnj 
5545171Swnj gottype:
5555105Swnj 	/*
5565105Swnj 	 * Add local net header.  If no space in first mbuf,
5575105Swnj 	 * allocate another.
5585105Swnj 	 */
5595213Swnj 	if (m->m_off > MMAXOFF ||
5605213Swnj 	    MMINOFF + sizeof (struct en_header) > m->m_off) {
56117559Skarels 		MGET(m, M_DONTWAIT, MT_HEADER);
5625083Swnj 		if (m == 0) {
5636503Ssam 			error = ENOBUFS;
5646503Ssam 			goto bad;
5655083Swnj 		}
5665083Swnj 		m->m_next = m0;
5675083Swnj 		m->m_off = MMINOFF;
5685083Swnj 		m->m_len = sizeof (struct en_header);
5695083Swnj 	} else {
5705083Swnj 		m->m_off -= sizeof (struct en_header);
5715083Swnj 		m->m_len += sizeof (struct en_header);
5725083Swnj 	}
5735083Swnj 	en = mtod(m, struct en_header *);
57416380Skarels 	/* add en_shost later */
5755083Swnj 	en->en_dhost = dest;
57612352Ssam 	en->en_type = htons((u_short)type);
5775105Swnj 
57813458Ssam gotheader:
5795105Swnj 	/*
5805105Swnj 	 * Queue message on interface, and start output if interface
5815105Swnj 	 * not yet active.
5825105Swnj 	 */
5835083Swnj 	s = splimp();
5846207Swnj 	if (IF_QFULL(&ifp->if_snd)) {
5856207Swnj 		IF_DROP(&ifp->if_snd);
5866503Ssam 		error = ENOBUFS;
5876503Ssam 		goto qfull;
5886207Swnj 	}
5895083Swnj 	IF_ENQUEUE(&ifp->if_snd, m);
5905083Swnj 	if (en_softc[ifp->if_unit].es_oactive == 0)
5915083Swnj 		enstart(ifp->if_unit);
5925275Swnj 	splx(s);
5936503Ssam 	return (0);
5946503Ssam qfull:
5956503Ssam 	m0 = m;
5966503Ssam 	splx(s);
5976484Swnj bad:
5986503Ssam 	m_freem(m0);
5996503Ssam 	return (error);
6004686Swnj }
60113054Ssam 
60213054Ssam /*
60313054Ssam  * Process an ioctl request.
60413054Ssam  */
60513054Ssam enioctl(ifp, cmd, data)
60613054Ssam 	register struct ifnet *ifp;
60713054Ssam 	int cmd;
60813054Ssam 	caddr_t data;
60913054Ssam {
610*19864Skarels 	register struct en_softc *es = ((struct en_softc *)ifp);
611*19864Skarels 	struct ifaddr *ifa = (struct ifaddr *) data;
61213054Ssam 	int s = splimp(), error = 0;
613*19864Skarels 	struct endevice *enaddr;
61413054Ssam 
61513054Ssam 	switch (cmd) {
61613054Ssam 
61713054Ssam 	case SIOCSIFADDR:
618*19864Skarels 		enaddr = (struct endevice *)eninfo[ifp->if_unit]->ui_addr;
619*19864Skarels 		es->es_host = (~enaddr->en_addr) & 0xff;
620*19864Skarels 		/*
621*19864Skarels 		 * Attempt to check agreement of protocol address
622*19864Skarels 		 * and board address.
623*19864Skarels 		 */
624*19864Skarels 		switch (ifa->ifa_addr.sa_family) {
625*19864Skarels 		case AF_INET:
626*19864Skarels 			if (in_lnaof(IA_SIN(ifa)->sin_addr) != es->es_host)
627*19864Skarels 				return (EADDRNOTAVAIL);
628*19864Skarels 			break;
629*19864Skarels 		}
630*19864Skarels 		ifp->if_flags |= IFF_UP;
631*19864Skarels 		if ((ifp->if_flags & IFF_RUNNING) == 0)
63213054Ssam 			eninit(ifp->if_unit);
63313054Ssam 		break;
63413054Ssam 
63513054Ssam 	default:
63613054Ssam 		error = EINVAL;
63713054Ssam 	}
63813054Ssam 	splx(s);
63913054Ssam 	return (error);
64013054Ssam }
64113054Ssam 
64213293Ssam #ifdef ENF_SWABIPS
64313293Ssam /*
64413293Ssam  * Swab bytes
64513293Ssam  * Jeffrey Mogul, Stanford
64613293Ssam  */
64713293Ssam enswab(from, to, n)
648*19864Skarels 	register unsigned char *from, *to;
64913293Ssam 	register int n;
65013293Ssam {
65113293Ssam 	register unsigned long temp;
652*19864Skarels 
653*19864Skarels 	if ((n <= 0) || (n > 0xFFFF)) {
654*19864Skarels 		printf("enswab: bad len %d\n", n);
655*19864Skarels 		return;
656*19864Skarels 	}
65713293Ssam 
65813293Ssam 	n >>= 1; n++;
659*19864Skarels #define	STEP	{temp = *from++;*to++ = *from++;*to++ = temp;}
66013293Ssam 	/* round to multiple of 8 */
66113293Ssam 	while ((--n) & 07)
66213293Ssam 		STEP;
66313293Ssam 	n >>= 3;
66413293Ssam 	while (--n >= 0) {
66513293Ssam 		STEP; STEP; STEP; STEP;
66613293Ssam 		STEP; STEP; STEP; STEP;
66713293Ssam 	}
66813293Ssam }
66913293Ssam #endif
670