xref: /csrg-svn/sys/vax/if/if_en.c (revision 35801)
123290Smckusick /*
229362Smckusick  * Copyright (c) 1982, 1986 Regents of the University of California.
335319Sbostic  * All rights reserved.
423290Smckusick  *
535319Sbostic  * Redistribution and use in source and binary forms are permitted
635319Sbostic  * provided that the above copyright notice and this paragraph are
735319Sbostic  * duplicated in all such forms and that any documentation,
835319Sbostic  * advertising materials, and other materials related to such
935319Sbostic  * distribution and use acknowledge that the software was developed
1035319Sbostic  * by the University of California, Berkeley.  The name of the
1135319Sbostic  * University may not be used to endorse or promote products derived
1235319Sbostic  * from this software without specific prior written permission.
1335319Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1435319Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1535319Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1635319Sbostic  *
17*35801Ssklower  *	@(#)if_en.c	7.3 (Berkeley) 10/12/88
1823290Smckusick  */
194686Swnj 
204686Swnj #include "en.h"
2125272Sbloom #if NEN > 0
225105Swnj 
234686Swnj /*
245105Swnj  * Xerox prototype (3 Mb) Ethernet interface driver.
254686Swnj  */
269796Ssam #include "../machine/pte.h"
274686Swnj 
2817113Sbloom #include "param.h"
2917113Sbloom #include "systm.h"
3017113Sbloom #include "mbuf.h"
3117113Sbloom #include "buf.h"
3217113Sbloom #include "protosw.h"
3317113Sbloom #include "socket.h"
3417113Sbloom #include "vmmac.h"
3517113Sbloom #include "errno.h"
3617113Sbloom #include "ioctl.h"
378462Sroot 
388462Sroot #include "../net/if.h"
398462Sroot #include "../net/netisr.h"
408462Sroot #include "../net/route.h"
4124790Skarels 
4224790Skarels #ifdef	INET
438418Swnj #include "../netinet/in.h"
448418Swnj #include "../netinet/in_systm.h"
4519864Skarels #include "../netinet/in_var.h"
468418Swnj #include "../netinet/ip.h"
4724790Skarels #endif
4824790Skarels 
4919950Sbloom #ifdef PUP
508418Swnj #include "../netpup/pup.h"
5113458Ssam #include "../netpup/ether.h"
5219950Sbloom #endif
534686Swnj 
5425610Ssklower #ifdef NS
5525610Ssklower #include "../netns/ns.h"
5625610Ssklower #include "../netns/ns_if.h"
5725610Ssklower #endif
5825610Ssklower 
598462Sroot #include "../vax/cpu.h"
608462Sroot #include "../vax/mtpr.h"
6117113Sbloom #include "if_en.h"
6217113Sbloom #include "if_enreg.h"
6317113Sbloom #include "if_uba.h"
648462Sroot #include "../vaxuba/ubareg.h"
658462Sroot #include "../vaxuba/ubavar.h"
668462Sroot 
675213Swnj #define	ENMTU	(1024+512)
686925Swnj #define	ENMRU	(1024+512+16)		/* 16 is enough to receive trailer */
695083Swnj 
704686Swnj int	enprobe(), enattach(), enrint(), enxint(), encollide();
714686Swnj struct	uba_device *eninfo[NEN];
724686Swnj u_short enstd[] = { 0 };
734686Swnj struct	uba_driver endriver =
745171Swnj 	{ enprobe, 0, enattach, 0, enstd, "en", eninfo };
754686Swnj #define	ENUNIT(x)	minor(x)
764686Swnj 
77*35801Ssklower int	eninit(),oldenoutput(),enreset(),enioctl(), enstart();
785105Swnj 
7913293Ssam #ifdef notdef
805105Swnj /*
8113293Ssam  * If you need to byte swap IP's in the system, define
8213293Ssam  * this and do a SIOCSIFFLAGS at boot time.
8313293Ssam  */
8419864Skarels #define	ENF_SWABIPS	0x1000
8513293Ssam #endif
8613293Ssam 
8713293Ssam /*
885105Swnj  * Ethernet software status per interface.
895105Swnj  *
905105Swnj  * Each interface is referenced by a network interface structure,
915105Swnj  * es_if, which the routing code uses to locate the interface.
925105Swnj  * This structure contains the output queue for the interface, its address, ...
935105Swnj  * We also have, for each interface, a UBA interface structure, which
945105Swnj  * contains information about the UNIBUS resources held by the interface:
955105Swnj  * map registers, buffered data paths, etc.  Information is cached in this
965105Swnj  * structure for use by the if_uba.c routines in running the interface
975105Swnj  * efficiently.
985105Swnj  */
995083Swnj struct	en_softc {
1005105Swnj 	struct	ifnet es_if;		/* network-visible interface */
1015105Swnj 	struct	ifuba es_ifuba;		/* UNIBUS resources */
10216380Skarels 	short	es_host;		/* hardware host number */
1035105Swnj 	short	es_delay;		/* current output delay */
1045105Swnj 	short	es_mask;		/* mask for current output delay */
1056471Sroot 	short	es_lastx;		/* host last transmitted to */
1065105Swnj 	short	es_oactive;		/* is output active? */
1075105Swnj 	short	es_olen;		/* length of last output */
10825610Ssklower 	short	es_nsactive;		/* is interface enabled for ns? */
1095083Swnj } en_softc[NEN];
1104686Swnj 
1115105Swnj /*
1125105Swnj  * Do output DMA to determine interface presence and
1135105Swnj  * interrupt vector.  DMA is too short to disturb other hosts.
1145105Swnj  */
1154686Swnj enprobe(reg)
1164686Swnj 	caddr_t reg;
1174686Swnj {
1185171Swnj 	register int br, cvec;		/* r11, r10 value-result */
1194686Swnj 	register struct endevice *addr = (struct endevice *)reg;
1204686Swnj 
1214686Swnj #ifdef lint
1224686Swnj 	br = 0; cvec = br; br = cvec;
1234922Swnj 	enrint(0); enxint(0); encollide(0);
1244686Swnj #endif
1254686Swnj 	addr->en_istat = 0;
1264686Swnj 	addr->en_owc = -1;
1274686Swnj 	addr->en_oba = 0;
1284771Swnj 	addr->en_ostat = EN_IEN|EN_GO;
1294686Swnj 	DELAY(100000);
1304686Swnj 	addr->en_ostat = 0;
1314686Swnj 	return (1);
1324686Swnj }
1334686Swnj 
1345105Swnj /*
1355105Swnj  * Interface exists: make available by filling in network interface
1365105Swnj  * record.  System will initialize the interface when it is ready
1375105Swnj  * to accept packets.
1385105Swnj  */
1394686Swnj enattach(ui)
1404686Swnj 	struct uba_device *ui;
1414686Swnj {
1425105Swnj 	register struct en_softc *es = &en_softc[ui->ui_unit];
1434688Swnj 
1445105Swnj 	es->es_if.if_unit = ui->ui_unit;
1455171Swnj 	es->es_if.if_name = "en";
1465105Swnj 	es->es_if.if_mtu = ENMTU;
14719864Skarels 	es->es_if.if_flags = IFF_BROADCAST;
1485171Swnj 	es->es_if.if_init = eninit;
149*35801Ssklower 	es->es_if.if_output = oldenoutput;
150*35801Ssklower 	es->es_if.if_start = enstart;
15113054Ssam 	es->es_if.if_ioctl = enioctl;
1528978Sroot 	es->es_if.if_reset = enreset;
1536554Ssam 	es->es_ifuba.ifu_flags = UBA_NEEDBDP | UBA_NEED16 | UBA_CANTWAIT;
1549271Ssam #if defined(VAX750)
1559271Ssam 	/* don't chew up 750 bdp's */
1569271Ssam 	if (cpu == VAX_750 && ui->ui_unit > 0)
1579271Ssam 		es->es_ifuba.ifu_flags &= ~UBA_NEEDBDP;
1589271Ssam #endif
1595160Swnj 	if_attach(&es->es_if);
1604686Swnj }
1614686Swnj 
1625105Swnj /*
1635105Swnj  * Reset of interface after UNIBUS reset.
1645105Swnj  * If interface is on specified uba, reset its state.
1655105Swnj  */
1665105Swnj enreset(unit, uban)
1675105Swnj 	int unit, uban;
1685105Swnj {
1695105Swnj 	register struct uba_device *ui;
1705105Swnj 
1715171Swnj 	if (unit >= NEN || (ui = eninfo[unit]) == 0 || ui->ui_alive == 0 ||
1725171Swnj 	    ui->ui_ubanum != uban)
1735105Swnj 		return;
1745171Swnj 	printf(" en%d", unit);
1755105Swnj 	eninit(unit);
1765105Swnj }
1775105Swnj 
1785105Swnj /*
1795105Swnj  * Initialization of interface; clear recorded pending
1805105Swnj  * operations, and reinitialize UNIBUS usage.
1815105Swnj  */
1824688Swnj eninit(unit)
1834686Swnj 	int unit;
1845083Swnj {
1855171Swnj 	register struct en_softc *es = &en_softc[unit];
1865171Swnj 	register struct uba_device *ui = eninfo[unit];
1874686Swnj 	register struct endevice *addr;
18813054Ssam 	int s;
1894686Swnj 
19019864Skarels 	if (es->es_if.if_addrlist == (struct ifaddr *)0)
19112349Ssam 		return;
1925105Swnj 	if (if_ubainit(&es->es_ifuba, ui->ui_ubanum,
1936925Swnj 	    sizeof (struct en_header), (int)btoc(ENMRU)) == 0) {
1945171Swnj 		printf("en%d: can't initialize\n", unit);
1956335Ssam 		es->es_if.if_flags &= ~IFF_UP;
1965083Swnj 		return;
1974686Swnj 	}
1984686Swnj 	addr = (struct endevice *)ui->ui_addr;
1995083Swnj 	addr->en_istat = addr->en_ostat = 0;
2004686Swnj 
2015105Swnj 	/*
2025171Swnj 	 * Hang a receive and start any
2035171Swnj 	 * pending writes by faking a transmit complete.
2045105Swnj 	 */
2055105Swnj 	s = splimp();
2065171Swnj 	addr->en_iba = es->es_ifuba.ifu_r.ifrw_info;
2076925Swnj 	addr->en_iwc = -(sizeof (struct en_header) + ENMRU) >> 1;
2085171Swnj 	addr->en_istat = EN_IEN|EN_GO;
2095171Swnj 	es->es_oactive = 1;
21019864Skarels 	es->es_if.if_flags |= IFF_RUNNING;
2115105Swnj 	enxint(unit);
2125105Swnj 	splx(s);
2134686Swnj }
2144686Swnj 
2156471Sroot int	enalldelay = 0;
2166951Swnj int	enlastdel = 50;
2176471Sroot int	enlastmask = (~0) << 5;
2185083Swnj 
2195105Swnj /*
2205105Swnj  * Start or restart output on interface.
2215105Swnj  * If interface is already active, then this is a retransmit
2225105Swnj  * after a collision, and just restuff registers and delay.
2235105Swnj  * If interface is not already active, get another datagram
2245105Swnj  * to send off of the interface queue, and map it to the interface
2255105Swnj  * before starting the output.
2265105Swnj  */
2274688Swnj enstart(dev)
2284686Swnj 	dev_t dev;
2294686Swnj {
2305171Swnj         int unit = ENUNIT(dev);
2315171Swnj 	struct uba_device *ui = eninfo[unit];
2325171Swnj 	register struct en_softc *es = &en_softc[unit];
2335083Swnj 	register struct endevice *addr;
23416380Skarels 	register struct en_header *en;
2355083Swnj 	struct mbuf *m;
2365083Swnj 	int dest;
2374686Swnj 
2385083Swnj 	if (es->es_oactive)
2395083Swnj 		goto restart;
2405105Swnj 
2415105Swnj 	/*
2425105Swnj 	 * Not already active: dequeue another request
2435105Swnj 	 * and map it to the UNIBUS.  If no more requests,
2445105Swnj 	 * just return.
2455105Swnj 	 */
2465105Swnj 	IF_DEQUEUE(&es->es_if.if_snd, m);
2475083Swnj 	if (m == 0) {
2485083Swnj 		es->es_oactive = 0;
2494686Swnj 		return;
2504686Swnj 	}
25116380Skarels 	en = mtod(m, struct en_header *);
25216380Skarels 	dest = en->en_dhost;
25316380Skarels 	en->en_shost = es->es_host;
2545105Swnj 	es->es_olen = if_wubaput(&es->es_ifuba, m);
25513293Ssam #ifdef ENF_SWABIPS
25613293Ssam 	/*
25713293Ssam 	 * The Xerox interface does word at a time DMA, so
25813293Ssam 	 * someone must do byte swapping of user data if high
25913293Ssam 	 * and low ender machines are to communicate.  It doesn't
26013293Ssam 	 * belong here, but certain people depend on it, so...
26113293Ssam 	 *
26213293Ssam 	 * Should swab everybody, but this is a kludge anyway.
26313293Ssam 	 */
26413293Ssam 	if (es->es_if.if_flags & ENF_SWABIPS) {
26513293Ssam 		en = (struct en_header *)es->es_ifuba.ifu_w.ifrw_addr;
26613293Ssam 		if (en->en_type == ENTYPE_IP)
26713293Ssam 			enswab((caddr_t)(en + 1), (caddr_t)(en + 1),
26813293Ssam 			    es->es_olen - sizeof (struct en_header) + 1);
26913293Ssam 	}
27013293Ssam #endif
27113293Ssam 
2725105Swnj 	/*
2735105Swnj 	 * Ethernet cannot take back-to-back packets (no
2746471Sroot 	 * buffering in interface.  To help avoid overrunning
2756471Sroot 	 * receivers, enforce a small delay (about 1ms) in interface:
2766471Sroot 	 *	* between all packets when enalldelay
2776471Sroot 	 *	* whenever last packet was broadcast
2786471Sroot 	 *	* whenever this packet is to same host as last packet
2795105Swnj 	 */
2806471Sroot 	if (enalldelay || es->es_lastx == 0 || es->es_lastx == dest) {
2815083Swnj 		es->es_delay = enlastdel;
2826471Sroot 		es->es_mask = enlastmask;
2836471Sroot 	}
2846471Sroot 	es->es_lastx = dest;
2855105Swnj 
2865083Swnj restart:
2875105Swnj 	/*
2885105Swnj 	 * Have request mapped to UNIBUS for transmission.
2895105Swnj 	 * Purge any stale data from this BDP, and start the otput.
2905105Swnj 	 */
2916335Ssam 	if (es->es_ifuba.ifu_flags & UBA_NEEDBDP)
2926335Ssam 		UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_w.ifrw_bdp);
2934686Swnj 	addr = (struct endevice *)ui->ui_addr;
2945160Swnj 	addr->en_oba = (int)es->es_ifuba.ifu_w.ifrw_info;
2955083Swnj 	addr->en_odelay = es->es_delay;
2965083Swnj 	addr->en_owc = -((es->es_olen + 1) >> 1);
2974771Swnj 	addr->en_ostat = EN_IEN|EN_GO;
2985083Swnj 	es->es_oactive = 1;
2994686Swnj }
3004686Swnj 
3015105Swnj /*
3025105Swnj  * Ethernet interface transmitter interrupt.
3035105Swnj  * Start another output if more data to send.
3045105Swnj  */
3054686Swnj enxint(unit)
3064686Swnj 	int unit;
3074686Swnj {
3085171Swnj 	register struct uba_device *ui = eninfo[unit];
3095171Swnj 	register struct en_softc *es = &en_softc[unit];
3106242Sroot 	register struct endevice *addr = (struct endevice *)ui->ui_addr;
3114686Swnj 
3125083Swnj 	if (es->es_oactive == 0)
3135083Swnj 		return;
3146242Sroot 	if (es->es_mask && (addr->en_ostat&EN_OERROR)) {
3156242Sroot 		es->es_if.if_oerrors++;
3166242Sroot 		endocoll(unit);
3176242Sroot 		return;
3186242Sroot 	}
3195171Swnj 	es->es_if.if_opackets++;
3205083Swnj 	es->es_oactive = 0;
3215083Swnj 	es->es_delay = 0;
3225083Swnj 	es->es_mask = ~0;
3235696Sroot 	if (es->es_ifuba.ifu_xtofree) {
3245696Sroot 		m_freem(es->es_ifuba.ifu_xtofree);
3255696Sroot 		es->es_ifuba.ifu_xtofree = 0;
3265696Sroot 	}
3275105Swnj 	if (es->es_if.if_snd.ifq_head == 0) {
3286471Sroot 		es->es_lastx = 256;		/* putatively illegal */
3294686Swnj 		return;
3304686Swnj 	}
3315083Swnj 	enstart(unit);
3324686Swnj }
3334686Swnj 
3345105Swnj /*
3355105Swnj  * Collision on ethernet interface.  Do exponential
3365105Swnj  * backoff, and retransmit.  If have backed off all
3376335Ssam  * the way print warning diagnostic, and drop packet.
3385105Swnj  */
3394686Swnj encollide(unit)
3404686Swnj 	int unit;
3414686Swnj {
3426242Sroot 	struct en_softc *es = &en_softc[unit];
3434686Swnj 
3445105Swnj 	es->es_if.if_collisions++;
3455083Swnj 	if (es->es_oactive == 0)
3464686Swnj 		return;
3476242Sroot 	endocoll(unit);
3486242Sroot }
3496242Sroot 
3506242Sroot endocoll(unit)
3516242Sroot 	int unit;
3526242Sroot {
3536242Sroot 	register struct en_softc *es = &en_softc[unit];
3546242Sroot 
3555171Swnj 	/*
3565171Swnj 	 * Es_mask is a 16 bit number with n low zero bits, with
3575171Swnj 	 * n the number of backoffs.  When es_mask is 0 we have
3585171Swnj 	 * backed off 16 times, and give up.
3595171Swnj 	 */
3605083Swnj 	if (es->es_mask == 0) {
3615213Swnj 		printf("en%d: send error\n", unit);
3625083Swnj 		enxint(unit);
3635171Swnj 		return;
3644686Swnj 	}
3655171Swnj 	/*
3665171Swnj 	 * Another backoff.  Restart with delay based on n low bits
3675171Swnj 	 * of the interval timer.
3685171Swnj 	 */
3695171Swnj 	es->es_mask <<= 1;
3705171Swnj 	es->es_delay = mfpr(ICR) &~ es->es_mask;
3715171Swnj 	enstart(unit);
3724686Swnj }
3734686Swnj 
37413458Ssam #ifdef notdef
37513458Ssam struct	sockproto enproto = { AF_ETHERLINK };
37613458Ssam struct	sockaddr_en endst = { AF_ETHERLINK };
37713458Ssam struct	sockaddr_en ensrc = { AF_ETHERLINK };
37813458Ssam #endif
3795105Swnj /*
3805105Swnj  * Ethernet interface receiver interrupt.
3815105Swnj  * If input error just drop packet.
3825105Swnj  * Otherwise purge input buffered data path and examine
3835105Swnj  * packet to determine type.  If can't determine length
3845105Swnj  * from type, then have to drop packet.  Othewise decapsulate
3855105Swnj  * packet based on type and pass to type specific higher-level
3865105Swnj  * input routine.
3875105Swnj  */
3884686Swnj enrint(unit)
3894686Swnj 	int unit;
3904686Swnj {
3915171Swnj 	register struct en_softc *es = &en_softc[unit];
3925171Swnj 	struct endevice *addr = (struct endevice *)eninfo[unit]->ui_addr;
3935171Swnj 	register struct en_header *en;
3945083Swnj     	struct mbuf *m;
3958603Sroot 	int len; short resid;
3965171Swnj 	register struct ifqueue *inq;
39716511Skarels 	int off, s;
3984686Swnj 
3995171Swnj 	es->es_if.if_ipackets++;
4005105Swnj 
4015105Swnj 	/*
4025171Swnj 	 * Purge BDP; drop if input error indicated.
4035105Swnj 	 */
4046335Ssam 	if (es->es_ifuba.ifu_flags & UBA_NEEDBDP)
4056335Ssam 		UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_r.ifrw_bdp);
4064771Swnj 	if (addr->en_istat&EN_IERROR) {
4075105Swnj 		es->es_if.if_ierrors++;
4085083Swnj 		goto setup;
4094686Swnj 	}
4105105Swnj 
4115105Swnj 	/*
4126471Sroot 	 * Calculate input data length.
4135105Swnj 	 * Get pointer to ethernet header (in input buffer).
4145105Swnj 	 * Deal with trailer protocol: if type is PUP trailer
4155105Swnj 	 * get true type from first 16-bit word past data.
4165105Swnj 	 * Remember that type was trailer by setting off.
4175105Swnj 	 */
4186471Sroot 	resid = addr->en_iwc;
4196471Sroot 	if (resid)
4206471Sroot 		resid |= 0176000;
4216925Swnj 	len = (((sizeof (struct en_header) + ENMRU) >> 1) + resid) << 1;
4226471Sroot 	len -= sizeof (struct en_header);
4236925Swnj 	if (len > ENMRU)
4246471Sroot 		goto setup;			/* sanity */
4255105Swnj 	en = (struct en_header *)(es->es_ifuba.ifu_r.ifrw_addr);
42612352Ssam 	en->en_type = ntohs(en->en_type);
4275083Swnj #define	endataaddr(en, off, type)	((type)(((caddr_t)((en)+1)+(off))))
42812352Ssam 	if (en->en_type >= ENTYPE_TRAIL &&
42912352Ssam 	    en->en_type < ENTYPE_TRAIL+ENTYPE_NTRAILER) {
43012352Ssam 		off = (en->en_type - ENTYPE_TRAIL) * 512;
4316925Swnj 		if (off > ENMTU)
4325171Swnj 			goto setup;		/* sanity */
43312352Ssam 		en->en_type = ntohs(*endataaddr(en, off, u_short *));
43412352Ssam 		resid = ntohs(*(endataaddr(en, off+2, u_short *)));
4356471Sroot 		if (off + resid > len)
4366471Sroot 			goto setup;		/* sanity */
4376471Sroot 		len = off + resid;
4385083Swnj 	} else
4395083Swnj 		off = 0;
4405083Swnj 	if (len == 0)
4415083Swnj 		goto setup;
44213293Ssam #ifdef ENF_SWABIPS
44313293Ssam 	if (es->es_if.if_flags & ENF_SWABIPS && en->en_type == ENTYPE_IP)
44413293Ssam 		enswab((caddr_t)(en + 1), (caddr_t)(en + 1), len);
44513293Ssam #endif
4465105Swnj 	/*
4475105Swnj 	 * Pull packet off interface.  Off is nonzero if packet
4485105Swnj 	 * has trailing header; if_rubaget will then force this header
4495105Swnj 	 * information to be at the front, but we still have to drop
4506471Sroot 	 * the type and length which are at the front of any trailer data.
4515105Swnj 	 */
45224790Skarels 	m = if_rubaget(&es->es_ifuba, len, off, &es->es_if);
4535213Swnj 	if (m == 0)
4545213Swnj 		goto setup;
4556027Ssam 	switch (en->en_type) {
4566027Ssam 
4576027Ssam #ifdef INET
45812352Ssam 	case ENTYPE_IP:
4596260Swnj 		schednetisr(NETISR_IP);
4606027Ssam 		inq = &ipintrq;
4616027Ssam 		break;
4626027Ssam #endif
4636335Ssam #ifdef PUP
46413458Ssam 	case ENTYPE_PUP:
46513458Ssam 		rpup_input(m);
4666027Ssam 		goto setup;
4676335Ssam #endif
46825610Ssklower #ifdef NS
46925610Ssklower 	case ETHERTYPE_NS:
47025610Ssklower 		if (es->es_nsactive) {
47125610Ssklower 			schednetisr(NETISR_NS);
47225610Ssklower 			inq = &nsintrq;
47325610Ssklower 		} else {
47425610Ssklower 			m_freem(m);
47525610Ssklower 			goto setup;
47625610Ssklower 		}
47725610Ssklower 		break;
47825610Ssklower #endif
47925610Ssklower 
4806476Swnj 	default:
48113458Ssam #ifdef notdef
48213458Ssam 		enproto.sp_protocol = en->en_type;
48313458Ssam 		endst.sen_host = en->en_dhost;
48413458Ssam 		endst.sen_net = ensrc.sen_net = es->es_if.if_net;
48513458Ssam 		ensrc.sen_host = en->en_shost;
48613458Ssam 		raw_input(m, &enproto,
48713458Ssam 		    (struct sockaddr *)&ensrc, (struct sockaddr *)&endst);
48813458Ssam #else
4896476Swnj 		m_freem(m);
49013458Ssam #endif
4916476Swnj 		goto setup;
4926335Ssam 	}
4936335Ssam 
49416511Skarels 	s = splimp();
4956207Swnj 	if (IF_QFULL(inq)) {
4966207Swnj 		IF_DROP(inq);
4976335Ssam 		m_freem(m);
4986207Swnj 	} else
4996207Swnj 		IF_ENQUEUE(inq, m);
50016511Skarels 	splx(s);
5015105Swnj 
5024688Swnj setup:
5035105Swnj 	/*
5045105Swnj 	 * Reset for next packet.
5055105Swnj 	 */
5065105Swnj 	addr->en_iba = es->es_ifuba.ifu_r.ifrw_info;
5076925Swnj 	addr->en_iwc = -(sizeof (struct en_header) + ENMRU) >> 1;
5084771Swnj 	addr->en_istat = EN_IEN|EN_GO;
5094688Swnj }
5104686Swnj 
5115083Swnj /*
5125083Swnj  * Ethernet output routine.
5135083Swnj  * Encapsulate a packet of type family for the local net.
5145105Swnj  * Use trailer local net encapsulation if enough data in first
5155105Swnj  * packet leaves a multiple of 512 bytes of data in remainder.
5165083Swnj  */
517*35801Ssklower oldenoutput(ifp, m0, dst)
5185083Swnj 	struct ifnet *ifp;
5195083Swnj 	struct mbuf *m0;
5206335Ssam 	struct sockaddr *dst;
5214686Swnj {
5226503Ssam 	int type, dest, s, error;
5235105Swnj 	register struct mbuf *m = m0;
5245083Swnj 	register struct en_header *en;
5256335Ssam 	register int off;
5264686Swnj 
52725446Skarels 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
52825446Skarels 		error = ENETDOWN;
52925446Skarels 		goto bad;
53025446Skarels 	}
5316335Ssam 	switch (dst->sa_family) {
5325083Swnj 
5335083Swnj #ifdef INET
5346335Ssam 	case AF_INET:
53519864Skarels 		{
53619864Skarels 		struct in_addr in;
53719864Skarels 
53819864Skarels 		in = ((struct sockaddr_in *)dst)->sin_addr;
53919864Skarels 		if (in_broadcast(in))
54019864Skarels 			dest = EN_BROADCAST;
54119864Skarels 		else
54219864Skarels 			dest = in_lnaof(in);
54319864Skarels 		}
54419864Skarels 		if (dest >= 0x100) {
5456503Ssam 			error = EPERM;		/* ??? */
5466486Swnj 			goto bad;
5476503Ssam 		}
548*35801Ssklower 		off = m->m_pkthdr.len - m->m_len;
54913054Ssam 		/* need per host negotiation */
55013054Ssam 		if ((ifp->if_flags & IFF_NOTRAILERS) == 0)
5516335Ssam 		if (off > 0 && (off & 0x1ff) == 0 &&
552*35801Ssklower 		    (m->m_flags & M_EXT) == 0 &&
553*35801Ssklower 		    m->m_data >= m->m_pktdat + 2 * sizeof (u_short)) {
55412352Ssam 			type = ENTYPE_TRAIL + (off>>9);
555*35801Ssklower 			m->m_data -= 2 * sizeof (u_short);
5566471Sroot 			m->m_len += 2 * sizeof (u_short);
55712352Ssam 			*mtod(m, u_short *) = htons((u_short)ENTYPE_IP);
55812352Ssam 			*(mtod(m, u_short *) + 1) = ntohs((u_short)m->m_len);
5595105Swnj 			goto gottrailertype;
5605083Swnj 		}
56112352Ssam 		type = ENTYPE_IP;
5625105Swnj 		off = 0;
5635105Swnj 		goto gottype;
5645083Swnj #endif
56525610Ssklower #ifdef NS
56625610Ssklower 	case AF_NS:
56725610Ssklower 	{
56825610Ssklower 		u_char *up;
56925610Ssklower 
57025610Ssklower 		type = ETHERTYPE_NS;
57125610Ssklower 		up = ((struct sockaddr_ns *)dst)->sns_addr.x_host.c_host;
57225610Ssklower 		if (*up & 1)
57325610Ssklower 			dest = EN_BROADCAST;
57425610Ssklower 		else
57525610Ssklower 			dest = up[5];
57625610Ssklower 
57725610Ssklower 		off = 0;
57825610Ssklower 		goto gottype;
57925610Ssklower 	}
58025610Ssklower #endif
5816027Ssam #ifdef PUP
5826335Ssam 	case AF_PUP:
58312833Ssam 		dest = ((struct sockaddr_pup *)dst)->spup_host;
58412352Ssam 		type = ENTYPE_PUP;
5856027Ssam 		off = 0;
5866027Ssam 		goto gottype;
5876027Ssam #endif
5886027Ssam 
58913458Ssam #ifdef notdef
59013458Ssam 	case AF_ETHERLINK:
59113458Ssam 		goto gotheader;
59213458Ssam #endif
59313458Ssam 
5945083Swnj 	default:
5956335Ssam 		printf("en%d: can't handle af%d\n", ifp->if_unit,
5966335Ssam 			dst->sa_family);
5976503Ssam 		error = EAFNOSUPPORT;
5986503Ssam 		goto bad;
5994686Swnj 	}
6005105Swnj 
6015171Swnj gottrailertype:
6025105Swnj 	/*
6035105Swnj 	 * Packet to be sent as trailer: move first packet
6045105Swnj 	 * (control information) to end of chain.
6055105Swnj 	 */
6065105Swnj 	while (m->m_next)
6075105Swnj 		m = m->m_next;
6085105Swnj 	m->m_next = m0;
6095105Swnj 	m = m0->m_next;
6105105Swnj 	m0->m_next = 0;
6115171Swnj 	m0 = m;
6125105Swnj 
6135171Swnj gottype:
6145105Swnj 	/*
6155105Swnj 	 * Add local net header.  If no space in first mbuf,
6165105Swnj 	 * allocate another.
6175105Swnj 	 */
618*35801Ssklower 	M_PREPEND(m, sizeof (struct en_header), M_DONTWAIT);
619*35801Ssklower 	if (m == NULL)
620*35801Ssklower 		return (ENOBUFS);
6215083Swnj 	en = mtod(m, struct en_header *);
62216380Skarels 	/* add en_shost later */
6235083Swnj 	en->en_dhost = dest;
62412352Ssam 	en->en_type = htons((u_short)type);
6255105Swnj 
62624790Skarels #ifdef notdef
62713458Ssam gotheader:
62824790Skarels #endif
6295105Swnj 	/*
6305105Swnj 	 * Queue message on interface, and start output if interface
6315105Swnj 	 * not yet active.
6325105Swnj 	 */
6335083Swnj 	s = splimp();
6346207Swnj 	if (IF_QFULL(&ifp->if_snd)) {
6356207Swnj 		IF_DROP(&ifp->if_snd);
6366503Ssam 		error = ENOBUFS;
6376503Ssam 		goto qfull;
6386207Swnj 	}
6395083Swnj 	IF_ENQUEUE(&ifp->if_snd, m);
6405083Swnj 	if (en_softc[ifp->if_unit].es_oactive == 0)
6415083Swnj 		enstart(ifp->if_unit);
6425275Swnj 	splx(s);
6436503Ssam 	return (0);
6446503Ssam qfull:
6456503Ssam 	m0 = m;
6466503Ssam 	splx(s);
6476484Swnj bad:
6486503Ssam 	m_freem(m0);
6496503Ssam 	return (error);
6504686Swnj }
65113054Ssam 
65213054Ssam /*
65313054Ssam  * Process an ioctl request.
65413054Ssam  */
65513054Ssam enioctl(ifp, cmd, data)
65613054Ssam 	register struct ifnet *ifp;
65713054Ssam 	int cmd;
65813054Ssam 	caddr_t data;
65913054Ssam {
66019864Skarels 	register struct en_softc *es = ((struct en_softc *)ifp);
66119864Skarels 	struct ifaddr *ifa = (struct ifaddr *) data;
66213054Ssam 	int s = splimp(), error = 0;
66319864Skarels 	struct endevice *enaddr;
66413054Ssam 
66513054Ssam 	switch (cmd) {
66613054Ssam 
66713054Ssam 	case SIOCSIFADDR:
66819864Skarels 		enaddr = (struct endevice *)eninfo[ifp->if_unit]->ui_addr;
66919864Skarels 		es->es_host = (~enaddr->en_addr) & 0xff;
67019864Skarels 		/*
67119864Skarels 		 * Attempt to check agreement of protocol address
67219864Skarels 		 * and board address.
67319864Skarels 		 */
67419864Skarels 		switch (ifa->ifa_addr.sa_family) {
67519864Skarels 		case AF_INET:
67619864Skarels 			if (in_lnaof(IA_SIN(ifa)->sin_addr) != es->es_host)
67719864Skarels 				return (EADDRNOTAVAIL);
67819864Skarels 			break;
67925610Ssklower #ifdef NS
68025610Ssklower 		case AF_NS:
68125610Ssklower 			if (IA_SNS(ifa)->sns_addr.x_host.c_host[5]
68225610Ssklower 							!= es->es_host)
68325610Ssklower 				return (EADDRNOTAVAIL);
68425610Ssklower 			es->es_nsactive = 1;
68525610Ssklower 			break;
68625610Ssklower #endif
68719864Skarels 		}
68819864Skarels 		ifp->if_flags |= IFF_UP;
68919864Skarels 		if ((ifp->if_flags & IFF_RUNNING) == 0)
69013054Ssam 			eninit(ifp->if_unit);
69113054Ssam 		break;
69213054Ssam 
69313054Ssam 	default:
69413054Ssam 		error = EINVAL;
69524790Skarels 		break;
69613054Ssam 	}
69713054Ssam 	splx(s);
69813054Ssam 	return (error);
69913054Ssam }
70013054Ssam 
70113293Ssam #ifdef ENF_SWABIPS
70213293Ssam /*
70313293Ssam  * Swab bytes
70413293Ssam  * Jeffrey Mogul, Stanford
70513293Ssam  */
70613293Ssam enswab(from, to, n)
70719864Skarels 	register unsigned char *from, *to;
70813293Ssam 	register int n;
70913293Ssam {
71013293Ssam 	register unsigned long temp;
71119864Skarels 
71219864Skarels 	if ((n <= 0) || (n > 0xFFFF)) {
71319864Skarels 		printf("enswab: bad len %d\n", n);
71419864Skarels 		return;
71519864Skarels 	}
71613293Ssam 
71713293Ssam 	n >>= 1; n++;
71819864Skarels #define	STEP	{temp = *from++;*to++ = *from++;*to++ = temp;}
71913293Ssam 	/* round to multiple of 8 */
72013293Ssam 	while ((--n) & 07)
72113293Ssam 		STEP;
72213293Ssam 	n >>= 3;
72313293Ssam 	while (--n >= 0) {
72413293Ssam 		STEP; STEP; STEP; STEP;
72513293Ssam 		STEP; STEP; STEP; STEP;
72613293Ssam 	}
72713293Ssam }
72813293Ssam #endif
72925272Sbloom #endif
730