xref: /csrg-svn/sys/vax/if/if_en.c (revision 45801)
123290Smckusick /*
229362Smckusick  * Copyright (c) 1982, 1986 Regents of the University of California.
335319Sbostic  * All rights reserved.
423290Smckusick  *
544558Sbostic  * %sccs.include.redist.c%
635319Sbostic  *
7*45801Sbostic  *	@(#)if_en.c	7.7 (Berkeley) 12/16/90
823290Smckusick  */
94686Swnj 
104686Swnj #include "en.h"
1125272Sbloom #if NEN > 0
125105Swnj 
134686Swnj /*
145105Swnj  * Xerox prototype (3 Mb) Ethernet interface driver.
154686Swnj  */
16*45801Sbostic #include "../include/pte.h"
174686Swnj 
18*45801Sbostic #include "sys/param.h"
19*45801Sbostic #include "sys/systm.h"
20*45801Sbostic #include "sys/mbuf.h"
21*45801Sbostic #include "sys/buf.h"
22*45801Sbostic #include "sys/protosw.h"
23*45801Sbostic #include "sys/socket.h"
24*45801Sbostic #include "sys/vmmac.h"
25*45801Sbostic #include "sys/errno.h"
26*45801Sbostic #include "sys/ioctl.h"
278462Sroot 
28*45801Sbostic #include "net/if.h"
29*45801Sbostic #include "net/netisr.h"
30*45801Sbostic #include "net/route.h"
3124790Skarels 
3224790Skarels #ifdef	INET
33*45801Sbostic #include "netinet/in.h"
34*45801Sbostic #include "netinet/in_systm.h"
35*45801Sbostic #include "netinet/in_var.h"
36*45801Sbostic #include "netinet/ip.h"
3724790Skarels #endif
3824790Skarels 
3919950Sbloom #ifdef PUP
40*45801Sbostic #include "netpup/pup.h"
41*45801Sbostic #include "netpup/ether.h"
4219950Sbloom #endif
434686Swnj 
4425610Ssklower #ifdef NS
45*45801Sbostic #include "netns/ns.h"
46*45801Sbostic #include "netns/ns_if.h"
4725610Ssklower #endif
4825610Ssklower 
49*45801Sbostic #include "../include/cpu.h"
50*45801Sbostic #include "../include/mtpr.h"
5117113Sbloom #include "if_en.h"
5217113Sbloom #include "if_enreg.h"
5317113Sbloom #include "if_uba.h"
54*45801Sbostic #include "../uba/ubareg.h"
55*45801Sbostic #include "../uba/ubavar.h"
568462Sroot 
575213Swnj #define	ENMTU	(1024+512)
586925Swnj #define	ENMRU	(1024+512+16)		/* 16 is enough to receive trailer */
595083Swnj 
604686Swnj int	enprobe(), enattach(), enrint(), enxint(), encollide();
614686Swnj struct	uba_device *eninfo[NEN];
624686Swnj u_short enstd[] = { 0 };
634686Swnj struct	uba_driver endriver =
645171Swnj 	{ enprobe, 0, enattach, 0, enstd, "en", eninfo };
654686Swnj #define	ENUNIT(x)	minor(x)
664686Swnj 
6735801Ssklower int	eninit(),oldenoutput(),enreset(),enioctl(), enstart();
685105Swnj 
6913293Ssam #ifdef notdef
705105Swnj /*
7113293Ssam  * If you need to byte swap IP's in the system, define
7213293Ssam  * this and do a SIOCSIFFLAGS at boot time.
7313293Ssam  */
7419864Skarels #define	ENF_SWABIPS	0x1000
7513293Ssam #endif
7613293Ssam 
7713293Ssam /*
785105Swnj  * Ethernet software status per interface.
795105Swnj  *
805105Swnj  * Each interface is referenced by a network interface structure,
815105Swnj  * es_if, which the routing code uses to locate the interface.
825105Swnj  * This structure contains the output queue for the interface, its address, ...
835105Swnj  * We also have, for each interface, a UBA interface structure, which
845105Swnj  * contains information about the UNIBUS resources held by the interface:
855105Swnj  * map registers, buffered data paths, etc.  Information is cached in this
865105Swnj  * structure for use by the if_uba.c routines in running the interface
875105Swnj  * efficiently.
885105Swnj  */
895083Swnj struct	en_softc {
905105Swnj 	struct	ifnet es_if;		/* network-visible interface */
915105Swnj 	struct	ifuba es_ifuba;		/* UNIBUS resources */
9216380Skarels 	short	es_host;		/* hardware host number */
935105Swnj 	short	es_delay;		/* current output delay */
945105Swnj 	short	es_mask;		/* mask for current output delay */
956471Sroot 	short	es_lastx;		/* host last transmitted to */
965105Swnj 	short	es_oactive;		/* is output active? */
975105Swnj 	short	es_olen;		/* length of last output */
9825610Ssklower 	short	es_nsactive;		/* is interface enabled for ns? */
995083Swnj } en_softc[NEN];
1004686Swnj 
1015105Swnj /*
1025105Swnj  * Do output DMA to determine interface presence and
1035105Swnj  * interrupt vector.  DMA is too short to disturb other hosts.
1045105Swnj  */
enprobe(reg)1054686Swnj enprobe(reg)
1064686Swnj 	caddr_t reg;
1074686Swnj {
1085171Swnj 	register int br, cvec;		/* r11, r10 value-result */
1094686Swnj 	register struct endevice *addr = (struct endevice *)reg;
1104686Swnj 
1114686Swnj #ifdef lint
1124686Swnj 	br = 0; cvec = br; br = cvec;
1134922Swnj 	enrint(0); enxint(0); encollide(0);
1144686Swnj #endif
1154686Swnj 	addr->en_istat = 0;
1164686Swnj 	addr->en_owc = -1;
1174686Swnj 	addr->en_oba = 0;
1184771Swnj 	addr->en_ostat = EN_IEN|EN_GO;
1194686Swnj 	DELAY(100000);
1204686Swnj 	addr->en_ostat = 0;
1214686Swnj 	return (1);
1224686Swnj }
1234686Swnj 
1245105Swnj /*
1255105Swnj  * Interface exists: make available by filling in network interface
1265105Swnj  * record.  System will initialize the interface when it is ready
1275105Swnj  * to accept packets.
1285105Swnj  */
1294686Swnj enattach(ui)
1304686Swnj 	struct uba_device *ui;
1314686Swnj {
1325105Swnj 	register struct en_softc *es = &en_softc[ui->ui_unit];
1334688Swnj 
1345105Swnj 	es->es_if.if_unit = ui->ui_unit;
1355171Swnj 	es->es_if.if_name = "en";
1365105Swnj 	es->es_if.if_mtu = ENMTU;
13719864Skarels 	es->es_if.if_flags = IFF_BROADCAST;
1385171Swnj 	es->es_if.if_init = eninit;
13935801Ssklower 	es->es_if.if_output = oldenoutput;
14035801Ssklower 	es->es_if.if_start = enstart;
14113054Ssam 	es->es_if.if_ioctl = enioctl;
1428978Sroot 	es->es_if.if_reset = enreset;
1436554Ssam 	es->es_ifuba.ifu_flags = UBA_NEEDBDP | UBA_NEED16 | UBA_CANTWAIT;
1449271Ssam #if defined(VAX750)
1459271Ssam 	/* don't chew up 750 bdp's */
1469271Ssam 	if (cpu == VAX_750 && ui->ui_unit > 0)
1479271Ssam 		es->es_ifuba.ifu_flags &= ~UBA_NEEDBDP;
1489271Ssam #endif
1495160Swnj 	if_attach(&es->es_if);
1504686Swnj }
1514686Swnj 
1525105Swnj /*
1535105Swnj  * Reset of interface after UNIBUS reset.
1545105Swnj  * If interface is on specified uba, reset its state.
1555105Swnj  */
enreset(unit,uban)1565105Swnj enreset(unit, uban)
1575105Swnj 	int unit, uban;
1585105Swnj {
1595105Swnj 	register struct uba_device *ui;
1605105Swnj 
1615171Swnj 	if (unit >= NEN || (ui = eninfo[unit]) == 0 || ui->ui_alive == 0 ||
1625171Swnj 	    ui->ui_ubanum != uban)
1635105Swnj 		return;
1645171Swnj 	printf(" en%d", unit);
1655105Swnj 	eninit(unit);
1665105Swnj }
1675105Swnj 
1685105Swnj /*
1695105Swnj  * Initialization of interface; clear recorded pending
1705105Swnj  * operations, and reinitialize UNIBUS usage.
1715105Swnj  */
eninit(unit)1724688Swnj eninit(unit)
1734686Swnj 	int unit;
1745083Swnj {
1755171Swnj 	register struct en_softc *es = &en_softc[unit];
1765171Swnj 	register struct uba_device *ui = eninfo[unit];
1774686Swnj 	register struct endevice *addr;
17813054Ssam 	int s;
1794686Swnj 
18019864Skarels 	if (es->es_if.if_addrlist == (struct ifaddr *)0)
18112349Ssam 		return;
1825105Swnj 	if (if_ubainit(&es->es_ifuba, ui->ui_ubanum,
1836925Swnj 	    sizeof (struct en_header), (int)btoc(ENMRU)) == 0) {
1845171Swnj 		printf("en%d: can't initialize\n", unit);
1856335Ssam 		es->es_if.if_flags &= ~IFF_UP;
1865083Swnj 		return;
1874686Swnj 	}
1884686Swnj 	addr = (struct endevice *)ui->ui_addr;
1895083Swnj 	addr->en_istat = addr->en_ostat = 0;
1904686Swnj 
1915105Swnj 	/*
1925171Swnj 	 * Hang a receive and start any
1935171Swnj 	 * pending writes by faking a transmit complete.
1945105Swnj 	 */
1955105Swnj 	s = splimp();
1965171Swnj 	addr->en_iba = es->es_ifuba.ifu_r.ifrw_info;
1976925Swnj 	addr->en_iwc = -(sizeof (struct en_header) + ENMRU) >> 1;
1985171Swnj 	addr->en_istat = EN_IEN|EN_GO;
1995171Swnj 	es->es_oactive = 1;
20019864Skarels 	es->es_if.if_flags |= IFF_RUNNING;
2015105Swnj 	enxint(unit);
2025105Swnj 	splx(s);
2034686Swnj }
2044686Swnj 
2056471Sroot int	enalldelay = 0;
2066951Swnj int	enlastdel = 50;
2076471Sroot int	enlastmask = (~0) << 5;
2085083Swnj 
2095105Swnj /*
2105105Swnj  * Start or restart output on interface.
2115105Swnj  * If interface is already active, then this is a retransmit
2125105Swnj  * after a collision, and just restuff registers and delay.
2135105Swnj  * If interface is not already active, get another datagram
2145105Swnj  * to send off of the interface queue, and map it to the interface
2155105Swnj  * before starting the output.
2165105Swnj  */
enstart(dev)2174688Swnj enstart(dev)
2184686Swnj 	dev_t dev;
2194686Swnj {
2205171Swnj         int unit = ENUNIT(dev);
2215171Swnj 	struct uba_device *ui = eninfo[unit];
2225171Swnj 	register struct en_softc *es = &en_softc[unit];
2235083Swnj 	register struct endevice *addr;
22416380Skarels 	register struct en_header *en;
2255083Swnj 	struct mbuf *m;
2265083Swnj 	int dest;
2274686Swnj 
2285083Swnj 	if (es->es_oactive)
2295083Swnj 		goto restart;
2305105Swnj 
2315105Swnj 	/*
2325105Swnj 	 * Not already active: dequeue another request
2335105Swnj 	 * and map it to the UNIBUS.  If no more requests,
2345105Swnj 	 * just return.
2355105Swnj 	 */
2365105Swnj 	IF_DEQUEUE(&es->es_if.if_snd, m);
2375083Swnj 	if (m == 0) {
2385083Swnj 		es->es_oactive = 0;
2394686Swnj 		return;
2404686Swnj 	}
24116380Skarels 	en = mtod(m, struct en_header *);
24216380Skarels 	dest = en->en_dhost;
24316380Skarels 	en->en_shost = es->es_host;
2445105Swnj 	es->es_olen = if_wubaput(&es->es_ifuba, m);
24513293Ssam #ifdef ENF_SWABIPS
24613293Ssam 	/*
24713293Ssam 	 * The Xerox interface does word at a time DMA, so
24813293Ssam 	 * someone must do byte swapping of user data if high
24913293Ssam 	 * and low ender machines are to communicate.  It doesn't
25013293Ssam 	 * belong here, but certain people depend on it, so...
25113293Ssam 	 *
25213293Ssam 	 * Should swab everybody, but this is a kludge anyway.
25313293Ssam 	 */
25413293Ssam 	if (es->es_if.if_flags & ENF_SWABIPS) {
25513293Ssam 		en = (struct en_header *)es->es_ifuba.ifu_w.ifrw_addr;
25613293Ssam 		if (en->en_type == ENTYPE_IP)
25713293Ssam 			enswab((caddr_t)(en + 1), (caddr_t)(en + 1),
25813293Ssam 			    es->es_olen - sizeof (struct en_header) + 1);
25913293Ssam 	}
26013293Ssam #endif
26113293Ssam 
2625105Swnj 	/*
2635105Swnj 	 * Ethernet cannot take back-to-back packets (no
2646471Sroot 	 * buffering in interface.  To help avoid overrunning
2656471Sroot 	 * receivers, enforce a small delay (about 1ms) in interface:
2666471Sroot 	 *	* between all packets when enalldelay
2676471Sroot 	 *	* whenever last packet was broadcast
2686471Sroot 	 *	* whenever this packet is to same host as last packet
2695105Swnj 	 */
2706471Sroot 	if (enalldelay || es->es_lastx == 0 || es->es_lastx == dest) {
2715083Swnj 		es->es_delay = enlastdel;
2726471Sroot 		es->es_mask = enlastmask;
2736471Sroot 	}
2746471Sroot 	es->es_lastx = dest;
2755105Swnj 
2765083Swnj restart:
2775105Swnj 	/*
2785105Swnj 	 * Have request mapped to UNIBUS for transmission.
2795105Swnj 	 * Purge any stale data from this BDP, and start the otput.
2805105Swnj 	 */
2816335Ssam 	if (es->es_ifuba.ifu_flags & UBA_NEEDBDP)
2826335Ssam 		UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_w.ifrw_bdp);
2834686Swnj 	addr = (struct endevice *)ui->ui_addr;
2845160Swnj 	addr->en_oba = (int)es->es_ifuba.ifu_w.ifrw_info;
2855083Swnj 	addr->en_odelay = es->es_delay;
2865083Swnj 	addr->en_owc = -((es->es_olen + 1) >> 1);
2874771Swnj 	addr->en_ostat = EN_IEN|EN_GO;
2885083Swnj 	es->es_oactive = 1;
2894686Swnj }
2904686Swnj 
2915105Swnj /*
2925105Swnj  * Ethernet interface transmitter interrupt.
2935105Swnj  * Start another output if more data to send.
2945105Swnj  */
enxint(unit)2954686Swnj enxint(unit)
2964686Swnj 	int unit;
2974686Swnj {
2985171Swnj 	register struct uba_device *ui = eninfo[unit];
2995171Swnj 	register struct en_softc *es = &en_softc[unit];
3006242Sroot 	register struct endevice *addr = (struct endevice *)ui->ui_addr;
3014686Swnj 
3025083Swnj 	if (es->es_oactive == 0)
3035083Swnj 		return;
3046242Sroot 	if (es->es_mask && (addr->en_ostat&EN_OERROR)) {
3056242Sroot 		es->es_if.if_oerrors++;
3066242Sroot 		endocoll(unit);
3076242Sroot 		return;
3086242Sroot 	}
3095171Swnj 	es->es_if.if_opackets++;
3105083Swnj 	es->es_oactive = 0;
3115083Swnj 	es->es_delay = 0;
3125083Swnj 	es->es_mask = ~0;
3135696Sroot 	if (es->es_ifuba.ifu_xtofree) {
3145696Sroot 		m_freem(es->es_ifuba.ifu_xtofree);
3155696Sroot 		es->es_ifuba.ifu_xtofree = 0;
3165696Sroot 	}
3175105Swnj 	if (es->es_if.if_snd.ifq_head == 0) {
3186471Sroot 		es->es_lastx = 256;		/* putatively illegal */
3194686Swnj 		return;
3204686Swnj 	}
3215083Swnj 	enstart(unit);
3224686Swnj }
3234686Swnj 
3245105Swnj /*
3255105Swnj  * Collision on ethernet interface.  Do exponential
3265105Swnj  * backoff, and retransmit.  If have backed off all
3276335Ssam  * the way print warning diagnostic, and drop packet.
3285105Swnj  */
encollide(unit)3294686Swnj encollide(unit)
3304686Swnj 	int unit;
3314686Swnj {
3326242Sroot 	struct en_softc *es = &en_softc[unit];
3334686Swnj 
3345105Swnj 	es->es_if.if_collisions++;
3355083Swnj 	if (es->es_oactive == 0)
3364686Swnj 		return;
3376242Sroot 	endocoll(unit);
3386242Sroot }
3396242Sroot 
endocoll(unit)3406242Sroot endocoll(unit)
3416242Sroot 	int unit;
3426242Sroot {
3436242Sroot 	register struct en_softc *es = &en_softc[unit];
3446242Sroot 
3455171Swnj 	/*
3465171Swnj 	 * Es_mask is a 16 bit number with n low zero bits, with
3475171Swnj 	 * n the number of backoffs.  When es_mask is 0 we have
3485171Swnj 	 * backed off 16 times, and give up.
3495171Swnj 	 */
3505083Swnj 	if (es->es_mask == 0) {
3515213Swnj 		printf("en%d: send error\n", unit);
3525083Swnj 		enxint(unit);
3535171Swnj 		return;
3544686Swnj 	}
3555171Swnj 	/*
3565171Swnj 	 * Another backoff.  Restart with delay based on n low bits
3575171Swnj 	 * of the interval timer.
3585171Swnj 	 */
3595171Swnj 	es->es_mask <<= 1;
3605171Swnj 	es->es_delay = mfpr(ICR) &~ es->es_mask;
3615171Swnj 	enstart(unit);
3624686Swnj }
3634686Swnj 
36413458Ssam #ifdef notdef
36513458Ssam struct	sockproto enproto = { AF_ETHERLINK };
36637476Ssklower struct	sockaddr_en endst = { sizeof(endst), AF_ETHERLINK };
36737476Ssklower struct	sockaddr_en ensrc = { sizeof(ensrc), AF_ETHERLINK };
36813458Ssam #endif
3695105Swnj /*
3705105Swnj  * Ethernet interface receiver interrupt.
3715105Swnj  * If input error just drop packet.
3725105Swnj  * Otherwise purge input buffered data path and examine
3735105Swnj  * packet to determine type.  If can't determine length
3745105Swnj  * from type, then have to drop packet.  Othewise decapsulate
3755105Swnj  * packet based on type and pass to type specific higher-level
3765105Swnj  * input routine.
3775105Swnj  */
enrint(unit)3784686Swnj enrint(unit)
3794686Swnj 	int unit;
3804686Swnj {
3815171Swnj 	register struct en_softc *es = &en_softc[unit];
3825171Swnj 	struct endevice *addr = (struct endevice *)eninfo[unit]->ui_addr;
3835171Swnj 	register struct en_header *en;
3845083Swnj     	struct mbuf *m;
3858603Sroot 	int len; short resid;
3865171Swnj 	register struct ifqueue *inq;
38716511Skarels 	int off, s;
3884686Swnj 
3895171Swnj 	es->es_if.if_ipackets++;
3905105Swnj 
3915105Swnj 	/*
3925171Swnj 	 * Purge BDP; drop if input error indicated.
3935105Swnj 	 */
3946335Ssam 	if (es->es_ifuba.ifu_flags & UBA_NEEDBDP)
3956335Ssam 		UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_r.ifrw_bdp);
3964771Swnj 	if (addr->en_istat&EN_IERROR) {
3975105Swnj 		es->es_if.if_ierrors++;
3985083Swnj 		goto setup;
3994686Swnj 	}
4005105Swnj 
4015105Swnj 	/*
4026471Sroot 	 * Calculate input data length.
4035105Swnj 	 * Get pointer to ethernet header (in input buffer).
4045105Swnj 	 * Deal with trailer protocol: if type is PUP trailer
4055105Swnj 	 * get true type from first 16-bit word past data.
4065105Swnj 	 * Remember that type was trailer by setting off.
4075105Swnj 	 */
4086471Sroot 	resid = addr->en_iwc;
4096471Sroot 	if (resid)
4106471Sroot 		resid |= 0176000;
4116925Swnj 	len = (((sizeof (struct en_header) + ENMRU) >> 1) + resid) << 1;
4126471Sroot 	len -= sizeof (struct en_header);
4136925Swnj 	if (len > ENMRU)
4146471Sroot 		goto setup;			/* sanity */
4155105Swnj 	en = (struct en_header *)(es->es_ifuba.ifu_r.ifrw_addr);
41612352Ssam 	en->en_type = ntohs(en->en_type);
4175083Swnj #define	endataaddr(en, off, type)	((type)(((caddr_t)((en)+1)+(off))))
41812352Ssam 	if (en->en_type >= ENTYPE_TRAIL &&
41912352Ssam 	    en->en_type < ENTYPE_TRAIL+ENTYPE_NTRAILER) {
42012352Ssam 		off = (en->en_type - ENTYPE_TRAIL) * 512;
4216925Swnj 		if (off > ENMTU)
4225171Swnj 			goto setup;		/* sanity */
42312352Ssam 		en->en_type = ntohs(*endataaddr(en, off, u_short *));
42412352Ssam 		resid = ntohs(*(endataaddr(en, off+2, u_short *)));
4256471Sroot 		if (off + resid > len)
4266471Sroot 			goto setup;		/* sanity */
4276471Sroot 		len = off + resid;
4285083Swnj 	} else
4295083Swnj 		off = 0;
4305083Swnj 	if (len == 0)
4315083Swnj 		goto setup;
43213293Ssam #ifdef ENF_SWABIPS
43313293Ssam 	if (es->es_if.if_flags & ENF_SWABIPS && en->en_type == ENTYPE_IP)
43413293Ssam 		enswab((caddr_t)(en + 1), (caddr_t)(en + 1), len);
43513293Ssam #endif
4365105Swnj 	/*
4375105Swnj 	 * Pull packet off interface.  Off is nonzero if packet
4385105Swnj 	 * has trailing header; if_rubaget will then force this header
4395105Swnj 	 * information to be at the front, but we still have to drop
4406471Sroot 	 * the type and length which are at the front of any trailer data.
4415105Swnj 	 */
44224790Skarels 	m = if_rubaget(&es->es_ifuba, len, off, &es->es_if);
4435213Swnj 	if (m == 0)
4445213Swnj 		goto setup;
4456027Ssam 	switch (en->en_type) {
4466027Ssam 
4476027Ssam #ifdef INET
44812352Ssam 	case ENTYPE_IP:
4496260Swnj 		schednetisr(NETISR_IP);
4506027Ssam 		inq = &ipintrq;
4516027Ssam 		break;
4526027Ssam #endif
4536335Ssam #ifdef PUP
45413458Ssam 	case ENTYPE_PUP:
45513458Ssam 		rpup_input(m);
4566027Ssam 		goto setup;
4576335Ssam #endif
45825610Ssklower #ifdef NS
45925610Ssklower 	case ETHERTYPE_NS:
46025610Ssklower 		if (es->es_nsactive) {
46125610Ssklower 			schednetisr(NETISR_NS);
46225610Ssklower 			inq = &nsintrq;
46325610Ssklower 		} else {
46425610Ssklower 			m_freem(m);
46525610Ssklower 			goto setup;
46625610Ssklower 		}
46725610Ssklower 		break;
46825610Ssklower #endif
46925610Ssklower 
4706476Swnj 	default:
47113458Ssam #ifdef notdef
47213458Ssam 		enproto.sp_protocol = en->en_type;
47313458Ssam 		endst.sen_host = en->en_dhost;
47413458Ssam 		endst.sen_net = ensrc.sen_net = es->es_if.if_net;
47513458Ssam 		ensrc.sen_host = en->en_shost;
47613458Ssam 		raw_input(m, &enproto,
47713458Ssam 		    (struct sockaddr *)&ensrc, (struct sockaddr *)&endst);
47813458Ssam #else
4796476Swnj 		m_freem(m);
48013458Ssam #endif
4816476Swnj 		goto setup;
4826335Ssam 	}
4836335Ssam 
48416511Skarels 	s = splimp();
4856207Swnj 	if (IF_QFULL(inq)) {
4866207Swnj 		IF_DROP(inq);
4876335Ssam 		m_freem(m);
4886207Swnj 	} else
4896207Swnj 		IF_ENQUEUE(inq, m);
49016511Skarels 	splx(s);
4915105Swnj 
4924688Swnj setup:
4935105Swnj 	/*
4945105Swnj 	 * Reset for next packet.
4955105Swnj 	 */
4965105Swnj 	addr->en_iba = es->es_ifuba.ifu_r.ifrw_info;
4976925Swnj 	addr->en_iwc = -(sizeof (struct en_header) + ENMRU) >> 1;
4984771Swnj 	addr->en_istat = EN_IEN|EN_GO;
4994688Swnj }
5004686Swnj 
5015083Swnj /*
5025083Swnj  * Ethernet output routine.
5035083Swnj  * Encapsulate a packet of type family for the local net.
5045105Swnj  * Use trailer local net encapsulation if enough data in first
5055105Swnj  * packet leaves a multiple of 512 bytes of data in remainder.
5065083Swnj  */
50735801Ssklower oldenoutput(ifp, m0, dst)
5085083Swnj 	struct ifnet *ifp;
5095083Swnj 	struct mbuf *m0;
5106335Ssam 	struct sockaddr *dst;
5114686Swnj {
5126503Ssam 	int type, dest, s, error;
5135105Swnj 	register struct mbuf *m = m0;
5145083Swnj 	register struct en_header *en;
5156335Ssam 	register int off;
5164686Swnj 
51725446Skarels 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
51825446Skarels 		error = ENETDOWN;
51925446Skarels 		goto bad;
52025446Skarels 	}
5216335Ssam 	switch (dst->sa_family) {
5225083Swnj 
5235083Swnj #ifdef INET
5246335Ssam 	case AF_INET:
52519864Skarels 		{
52619864Skarels 		struct in_addr in;
52719864Skarels 
52819864Skarels 		in = ((struct sockaddr_in *)dst)->sin_addr;
52919864Skarels 		if (in_broadcast(in))
53019864Skarels 			dest = EN_BROADCAST;
53119864Skarels 		else
53219864Skarels 			dest = in_lnaof(in);
53319864Skarels 		}
53419864Skarels 		if (dest >= 0x100) {
5356503Ssam 			error = EPERM;		/* ??? */
5366486Swnj 			goto bad;
5376503Ssam 		}
53835801Ssklower 		off = m->m_pkthdr.len - m->m_len;
53913054Ssam 		/* need per host negotiation */
54013054Ssam 		if ((ifp->if_flags & IFF_NOTRAILERS) == 0)
5416335Ssam 		if (off > 0 && (off & 0x1ff) == 0 &&
54235801Ssklower 		    (m->m_flags & M_EXT) == 0 &&
54335801Ssklower 		    m->m_data >= m->m_pktdat + 2 * sizeof (u_short)) {
54412352Ssam 			type = ENTYPE_TRAIL + (off>>9);
54535801Ssklower 			m->m_data -= 2 * sizeof (u_short);
5466471Sroot 			m->m_len += 2 * sizeof (u_short);
54712352Ssam 			*mtod(m, u_short *) = htons((u_short)ENTYPE_IP);
54812352Ssam 			*(mtod(m, u_short *) + 1) = ntohs((u_short)m->m_len);
5495105Swnj 			goto gottrailertype;
5505083Swnj 		}
55112352Ssam 		type = ENTYPE_IP;
5525105Swnj 		off = 0;
5535105Swnj 		goto gottype;
5545083Swnj #endif
55525610Ssklower #ifdef NS
55625610Ssklower 	case AF_NS:
55725610Ssklower 	{
55825610Ssklower 		u_char *up;
55925610Ssklower 
56025610Ssklower 		type = ETHERTYPE_NS;
56125610Ssklower 		up = ((struct sockaddr_ns *)dst)->sns_addr.x_host.c_host;
56225610Ssklower 		if (*up & 1)
56325610Ssklower 			dest = EN_BROADCAST;
56425610Ssklower 		else
56525610Ssklower 			dest = up[5];
56625610Ssklower 
56725610Ssklower 		off = 0;
56825610Ssklower 		goto gottype;
56925610Ssklower 	}
57025610Ssklower #endif
5716027Ssam #ifdef PUP
5726335Ssam 	case AF_PUP:
57312833Ssam 		dest = ((struct sockaddr_pup *)dst)->spup_host;
57412352Ssam 		type = ENTYPE_PUP;
5756027Ssam 		off = 0;
5766027Ssam 		goto gottype;
5776027Ssam #endif
5786027Ssam 
57913458Ssam #ifdef notdef
58013458Ssam 	case AF_ETHERLINK:
58113458Ssam 		goto gotheader;
58213458Ssam #endif
58313458Ssam 
5845083Swnj 	default:
5856335Ssam 		printf("en%d: can't handle af%d\n", ifp->if_unit,
5866335Ssam 			dst->sa_family);
5876503Ssam 		error = EAFNOSUPPORT;
5886503Ssam 		goto bad;
5894686Swnj 	}
5905105Swnj 
5915171Swnj gottrailertype:
5925105Swnj 	/*
5935105Swnj 	 * Packet to be sent as trailer: move first packet
5945105Swnj 	 * (control information) to end of chain.
5955105Swnj 	 */
5965105Swnj 	while (m->m_next)
5975105Swnj 		m = m->m_next;
5985105Swnj 	m->m_next = m0;
5995105Swnj 	m = m0->m_next;
6005105Swnj 	m0->m_next = 0;
6015171Swnj 	m0 = m;
6025105Swnj 
6035171Swnj gottype:
6045105Swnj 	/*
6055105Swnj 	 * Add local net header.  If no space in first mbuf,
6065105Swnj 	 * allocate another.
6075105Swnj 	 */
60835801Ssklower 	M_PREPEND(m, sizeof (struct en_header), M_DONTWAIT);
60935801Ssklower 	if (m == NULL)
61035801Ssklower 		return (ENOBUFS);
6115083Swnj 	en = mtod(m, struct en_header *);
61216380Skarels 	/* add en_shost later */
6135083Swnj 	en->en_dhost = dest;
61412352Ssam 	en->en_type = htons((u_short)type);
6155105Swnj 
61624790Skarels #ifdef notdef
61713458Ssam gotheader:
61824790Skarels #endif
6195105Swnj 	/*
6205105Swnj 	 * Queue message on interface, and start output if interface
6215105Swnj 	 * not yet active.
6225105Swnj 	 */
6235083Swnj 	s = splimp();
6246207Swnj 	if (IF_QFULL(&ifp->if_snd)) {
6256207Swnj 		IF_DROP(&ifp->if_snd);
6266503Ssam 		error = ENOBUFS;
6276503Ssam 		goto qfull;
6286207Swnj 	}
6295083Swnj 	IF_ENQUEUE(&ifp->if_snd, m);
6305083Swnj 	if (en_softc[ifp->if_unit].es_oactive == 0)
6315083Swnj 		enstart(ifp->if_unit);
6325275Swnj 	splx(s);
6336503Ssam 	return (0);
6346503Ssam qfull:
6356503Ssam 	m0 = m;
6366503Ssam 	splx(s);
6376484Swnj bad:
6386503Ssam 	m_freem(m0);
6396503Ssam 	return (error);
6404686Swnj }
64113054Ssam 
64213054Ssam /*
64313054Ssam  * Process an ioctl request.
64413054Ssam  */
enioctl(ifp,cmd,data)64513054Ssam enioctl(ifp, cmd, data)
64613054Ssam 	register struct ifnet *ifp;
64713054Ssam 	int cmd;
64813054Ssam 	caddr_t data;
64913054Ssam {
65019864Skarels 	register struct en_softc *es = ((struct en_softc *)ifp);
65119864Skarels 	struct ifaddr *ifa = (struct ifaddr *) data;
65213054Ssam 	int s = splimp(), error = 0;
65319864Skarels 	struct endevice *enaddr;
65413054Ssam 
65513054Ssam 	switch (cmd) {
65613054Ssam 
65713054Ssam 	case SIOCSIFADDR:
65819864Skarels 		enaddr = (struct endevice *)eninfo[ifp->if_unit]->ui_addr;
65919864Skarels 		es->es_host = (~enaddr->en_addr) & 0xff;
66019864Skarels 		/*
66119864Skarels 		 * Attempt to check agreement of protocol address
66219864Skarels 		 * and board address.
66319864Skarels 		 */
66437476Ssklower 		switch (ifa->ifa_addr->sa_family) {
66519864Skarels 		case AF_INET:
66619864Skarels 			if (in_lnaof(IA_SIN(ifa)->sin_addr) != es->es_host)
66719864Skarels 				return (EADDRNOTAVAIL);
66819864Skarels 			break;
66925610Ssklower #ifdef NS
67025610Ssklower 		case AF_NS:
67125610Ssklower 			if (IA_SNS(ifa)->sns_addr.x_host.c_host[5]
67225610Ssklower 							!= es->es_host)
67325610Ssklower 				return (EADDRNOTAVAIL);
67425610Ssklower 			es->es_nsactive = 1;
67525610Ssklower 			break;
67625610Ssklower #endif
67719864Skarels 		}
67819864Skarels 		ifp->if_flags |= IFF_UP;
67919864Skarels 		if ((ifp->if_flags & IFF_RUNNING) == 0)
68013054Ssam 			eninit(ifp->if_unit);
68113054Ssam 		break;
68213054Ssam 
68313054Ssam 	default:
68413054Ssam 		error = EINVAL;
68524790Skarels 		break;
68613054Ssam 	}
68713054Ssam 	splx(s);
68813054Ssam 	return (error);
68913054Ssam }
69013054Ssam 
69113293Ssam #ifdef ENF_SWABIPS
69213293Ssam /*
69313293Ssam  * Swab bytes
69413293Ssam  * Jeffrey Mogul, Stanford
69513293Ssam  */
enswab(from,to,n)69613293Ssam enswab(from, to, n)
69719864Skarels 	register unsigned char *from, *to;
69813293Ssam 	register int n;
69913293Ssam {
70013293Ssam 	register unsigned long temp;
70119864Skarels 
70219864Skarels 	if ((n <= 0) || (n > 0xFFFF)) {
70319864Skarels 		printf("enswab: bad len %d\n", n);
70419864Skarels 		return;
70519864Skarels 	}
70613293Ssam 
70713293Ssam 	n >>= 1; n++;
70819864Skarels #define	STEP	{temp = *from++;*to++ = *from++;*to++ = temp;}
70913293Ssam 	/* round to multiple of 8 */
71013293Ssam 	while ((--n) & 07)
71113293Ssam 		STEP;
71213293Ssam 	n >>= 3;
71313293Ssam 	while (--n >= 0) {
71413293Ssam 		STEP; STEP; STEP; STEP;
71513293Ssam 		STEP; STEP; STEP; STEP;
71613293Ssam 	}
71713293Ssam }
71813293Ssam #endif
71925272Sbloom #endif
720