xref: /csrg-svn/sys/vax/if/if_il.c (revision 25444)
123557Ssklower /*
223557Ssklower  * Copyright (c) 1982 Regents of the University of California.
323557Ssklower  * All rights reserved.  The Berkeley software License Agreement
423557Ssklower  * specifies the terms and conditions for redistribution.
523557Ssklower  *
6*25444Skarels  *	@(#)if_il.c	6.11 (Berkeley) 11/08/85
723557Ssklower  */
86893Sfeldman 
96893Sfeldman #include "il.h"
1025274Sbloom #if NIL > 0
116893Sfeldman 
126893Sfeldman /*
136893Sfeldman  * Interlan Ethernet Communications Controller interface
146893Sfeldman  */
159797Ssam #include "../machine/pte.h"
169797Ssam 
1717115Sbloom #include "param.h"
1817115Sbloom #include "systm.h"
1917115Sbloom #include "mbuf.h"
2017115Sbloom #include "buf.h"
2117115Sbloom #include "protosw.h"
2217115Sbloom #include "socket.h"
2317115Sbloom #include "vmmac.h"
2417115Sbloom #include "ioctl.h"
2517115Sbloom #include "errno.h"
268463Sroot 
278463Sroot #include "../net/if.h"
288463Sroot #include "../net/netisr.h"
298463Sroot #include "../net/route.h"
3023557Ssklower 
3124791Skarels #ifdef	BBNNET
3224791Skarels #define	INET
3324791Skarels #endif
3423557Ssklower #ifdef INET
358419Swnj #include "../netinet/in.h"
368419Swnj #include "../netinet/in_systm.h"
3719865Skarels #include "../netinet/in_var.h"
388419Swnj #include "../netinet/ip.h"
3911575Ssam #include "../netinet/if_ether.h"
4023557Ssklower #endif
4123557Ssklower 
4223557Ssklower #ifdef NS
4323557Ssklower #include "../netns/ns.h"
4423557Ssklower #include "../netns/ns_if.h"
4523557Ssklower #endif
4623557Ssklower 
478463Sroot #include "../vax/cpu.h"
488463Sroot #include "../vax/mtpr.h"
4917115Sbloom #include "if_il.h"
5017115Sbloom #include "if_ilreg.h"
5117115Sbloom #include "if_uba.h"
528463Sroot #include "../vaxuba/ubareg.h"
538463Sroot #include "../vaxuba/ubavar.h"
548463Sroot 
556893Sfeldman int	ilprobe(), ilattach(), ilrint(), ilcint();
566893Sfeldman struct	uba_device *ilinfo[NIL];
576893Sfeldman u_short ilstd[] = { 0 };
586893Sfeldman struct	uba_driver ildriver =
596893Sfeldman 	{ ilprobe, 0, ilattach, 0, ilstd, "il", ilinfo };
606893Sfeldman #define	ILUNIT(x)	minor(x)
6113056Ssam int	ilinit(),iloutput(),ilioctl(),ilreset(),ilwatch();
626893Sfeldman 
636893Sfeldman /*
646893Sfeldman  * Ethernet software status per interface.
656893Sfeldman  *
666893Sfeldman  * Each interface is referenced by a network interface structure,
676893Sfeldman  * is_if, which the routing code uses to locate the interface.
686893Sfeldman  * This structure contains the output queue for the interface, its address, ...
696893Sfeldman  * We also have, for each interface, a UBA interface structure, which
706893Sfeldman  * contains information about the UNIBUS resources held by the interface:
716893Sfeldman  * map registers, buffered data paths, etc.  Information is cached in this
726893Sfeldman  * structure for use by the if_uba.c routines in running the interface
736893Sfeldman  * efficiently.
746893Sfeldman  */
756893Sfeldman struct	il_softc {
7611575Ssam 	struct	arpcom is_ac;		/* Ethernet common part */
7711575Ssam #define	is_if	is_ac.ac_if		/* network-visible interface */
7811575Ssam #define	is_addr	is_ac.ac_enaddr		/* hardware Ethernet address */
796893Sfeldman 	struct	ifuba is_ifuba;		/* UNIBUS resources */
807261Ssam 	int	is_flags;
817261Ssam #define	ILF_OACTIVE	0x1		/* output is active */
827261Ssam #define	ILF_RCVPENDING	0x2		/* start rcv in ilcint */
837261Ssam #define	ILF_STATPENDING	0x4		/* stat cmd pending */
84*25444Skarels #define	ILF_RUNNING	0x8		/* board is running */
857261Ssam 	short	is_lastcmd;		/* can't read csr, so must save it */
867261Ssam 	short	is_scaninterval;	/* interval of stat collection */
877261Ssam #define	ILWATCHINTERVAL	60		/* once every 60 seconds */
887261Ssam 	struct	il_stats is_stats;	/* holds on-board statistics */
897261Ssam 	struct	il_stats is_sum;	/* summation over time */
907261Ssam 	int	is_ubaddr;		/* mapping registers of is_stats */
916893Sfeldman } il_softc[NIL];
926893Sfeldman 
936893Sfeldman ilprobe(reg)
946893Sfeldman 	caddr_t reg;
956893Sfeldman {
966893Sfeldman 	register int br, cvec;		/* r11, r10 value-result */
976893Sfeldman 	register struct ildevice *addr = (struct ildevice *)reg;
986893Sfeldman 	register i;
996893Sfeldman 
1006893Sfeldman #ifdef lint
1016893Sfeldman 	br = 0; cvec = br; br = cvec;
1029179Ssam 	i = 0; ilrint(i); ilcint(i); ilwatch(i);
1036893Sfeldman #endif
1046893Sfeldman 
1056893Sfeldman 	addr->il_csr = ILC_OFFLINE|IL_CIE;
1066893Sfeldman 	DELAY(100000);
1077261Ssam 	i = addr->il_csr;		/* clear CDONE */
1086893Sfeldman 	if (cvec > 0 && cvec != 0x200)
1096893Sfeldman 		cvec -= 4;
1106893Sfeldman 	return (1);
1116893Sfeldman }
1126893Sfeldman 
1136893Sfeldman /*
1146893Sfeldman  * Interface exists: make available by filling in network interface
1156893Sfeldman  * record.  System will initialize the interface when it is ready
1166893Sfeldman  * to accept packets.  A STATUS command is done to get the ethernet
1176893Sfeldman  * address and other interesting data.
1186893Sfeldman  */
1196893Sfeldman ilattach(ui)
1206893Sfeldman 	struct uba_device *ui;
1216893Sfeldman {
1226893Sfeldman 	register struct il_softc *is = &il_softc[ui->ui_unit];
1237220Ssam 	register struct ifnet *ifp = &is->is_if;
1246893Sfeldman 	register struct ildevice *addr = (struct ildevice *)ui->ui_addr;
1256893Sfeldman 
1267220Ssam 	ifp->if_unit = ui->ui_unit;
1277220Ssam 	ifp->if_name = "il";
1289746Ssam 	ifp->if_mtu = ETHERMTU;
12919865Skarels 	ifp->if_flags = IFF_BROADCAST;
1306893Sfeldman 
1316893Sfeldman 	/*
1327261Ssam 	 * Reset the board and map the statistics
1337261Ssam 	 * buffer onto the Unibus.
1346893Sfeldman 	 */
1357261Ssam 	addr->il_csr = ILC_RESET;
1367261Ssam 	while ((addr->il_csr&IL_CDONE) == 0)
1377261Ssam 		;
1387261Ssam 	if (addr->il_csr&IL_STATUS)
1397261Ssam 		printf("il%d: reset failed, csr=%b\n", ui->ui_unit,
1407261Ssam 			addr->il_csr, IL_BITS);
1416893Sfeldman 
1429179Ssam 	is->is_ubaddr = uballoc(ui->ui_ubanum, (caddr_t)&is->is_stats,
14313056Ssam 	    sizeof (struct il_stats), 0);
1447261Ssam 	addr->il_bar = is->is_ubaddr & 0xffff;
1457261Ssam 	addr->il_bcr = sizeof (struct il_stats);
1467261Ssam 	addr->il_csr = ((is->is_ubaddr >> 2) & IL_EUA)|ILC_STAT;
1477261Ssam 	while ((addr->il_csr&IL_CDONE) == 0)
1487261Ssam 		;
1497261Ssam 	if (addr->il_csr&IL_STATUS)
1507261Ssam 		printf("il%d: status failed, csr=%b\n", ui->ui_unit,
1517261Ssam 			addr->il_csr, IL_BITS);
1527261Ssam 	ubarelse(ui->ui_ubanum, &is->is_ubaddr);
15311575Ssam #ifdef notdef
1546893Sfeldman 	printf("il%d: addr=%x:%x:%x:%x:%x:%x module=%s firmware=%s\n",
1556893Sfeldman 		ui->ui_unit,
1567261Ssam 		is->is_stats.ils_addr[0]&0xff, is->is_stats.ils_addr[1]&0xff,
1577261Ssam 		is->is_stats.ils_addr[2]&0xff, is->is_stats.ils_addr[3]&0xff,
1587261Ssam 		is->is_stats.ils_addr[4]&0xff, is->is_stats.ils_addr[5]&0xff,
1597261Ssam 		is->is_stats.ils_module, is->is_stats.ils_firmware);
16011575Ssam #endif
16119865Skarels  	bcopy((caddr_t)is->is_stats.ils_addr, (caddr_t)is->is_addr,
16219865Skarels  	    sizeof (is->is_addr));
1637220Ssam 	ifp->if_init = ilinit;
1647220Ssam 	ifp->if_output = iloutput;
16513056Ssam 	ifp->if_ioctl = ilioctl;
1668979Sroot 	ifp->if_reset = ilreset;
1676893Sfeldman 	is->is_ifuba.ifu_flags = UBA_CANTWAIT;
1687220Ssam #ifdef notdef
1697220Ssam 	is->is_ifuba.ifu_flags |= UBA_NEEDBDP;
1707220Ssam #endif
1717220Ssam 	if_attach(ifp);
1726893Sfeldman }
1736893Sfeldman 
1746893Sfeldman /*
1756893Sfeldman  * Reset of interface after UNIBUS reset.
1766893Sfeldman  * If interface is on specified uba, reset its state.
1776893Sfeldman  */
1786893Sfeldman ilreset(unit, uban)
1796893Sfeldman 	int unit, uban;
1806893Sfeldman {
1816893Sfeldman 	register struct uba_device *ui;
1826893Sfeldman 
1836893Sfeldman 	if (unit >= NIL || (ui = ilinfo[unit]) == 0 || ui->ui_alive == 0 ||
1846893Sfeldman 	    ui->ui_ubanum != uban)
1856893Sfeldman 		return;
1866893Sfeldman 	printf(" il%d", unit);
187*25444Skarels 	il_softc[unit].is_if.if_flags &= ~IFF_RUNNING;
188*25444Skarels 	il_softc[unit].is_flags &= ~ILF_RUNNING;
1896893Sfeldman 	ilinit(unit);
1906893Sfeldman }
1916893Sfeldman 
1926893Sfeldman /*
1936893Sfeldman  * Initialization of interface; clear recorded pending
1946893Sfeldman  * operations, and reinitialize UNIBUS usage.
1956893Sfeldman  */
1966893Sfeldman ilinit(unit)
1976893Sfeldman 	int unit;
1986893Sfeldman {
1996893Sfeldman 	register struct il_softc *is = &il_softc[unit];
2006893Sfeldman 	register struct uba_device *ui = ilinfo[unit];
2016893Sfeldman 	register struct ildevice *addr;
20213056Ssam 	register struct ifnet *ifp = &is->is_if;
2037261Ssam 	int s;
2046893Sfeldman 
20519865Skarels 	/* not yet, if address still unknown */
20619865Skarels 	if (ifp->if_addrlist == (struct ifaddr *)0)
20711575Ssam 		return;
208*25444Skarels 	if (is->is_flags & ILF_RUNNING)
209*25444Skarels 		return;
21011575Ssam 
211*25444Skarels 	if ((ifp->if_flags & IFF_RUNNING) == 0) {
212*25444Skarels 		if (if_ubainit(&is->is_ifuba, ui->ui_ubanum,
213*25444Skarels 		    sizeof (struct il_rheader), (int)btoc(ETHERMTU)) == 0) {
214*25444Skarels 			printf("il%d: can't initialize\n", unit);
215*25444Skarels 			is->is_if.if_flags &= ~IFF_UP;
216*25444Skarels 			return;
217*25444Skarels 		}
218*25444Skarels 		is->is_ubaddr = uballoc(ui->ui_ubanum, (caddr_t)&is->is_stats,
219*25444Skarels 		    sizeof (struct il_stats), 0);
2206893Sfeldman 	}
22115071Skarels 	ifp->if_watchdog = ilwatch;
22215071Skarels 	is->is_scaninterval = ILWATCHINTERVAL;
22315071Skarels 	ifp->if_timer = is->is_scaninterval;
2246893Sfeldman 	addr = (struct ildevice *)ui->ui_addr;
2256893Sfeldman 
2266893Sfeldman 	/*
2279179Ssam 	 * Turn off source address insertion (it's faster this way),
22812488Ssam 	 * and set board online.  Former doesn't work if board is
22912488Ssam 	 * already online (happens on ubareset), so we put it offline
23012488Ssam 	 * first.
2319179Ssam 	 */
2329179Ssam 	s = splimp();
233*25444Skarels 	addr->il_csr = ILC_RESET;
2349179Ssam 	while ((addr->il_csr & IL_CDONE) == 0)
2359179Ssam 		;
236*25444Skarels  	if (addr->il_csr & IL_STATUS) {
237*25444Skarels  		printf("il%d failed hardware diag 0x%X\n", unit,
238*25444Skarels  		   addr->il_csr & 0xffff);
239*25444Skarels  		is->is_if.if_flags &= ~IFF_UP;
240*25444Skarels  		splx(s);
241*25444Skarels  		return;
242*25444Skarels  	}
24312488Ssam 	addr->il_csr = ILC_CISA;
2449179Ssam 	while ((addr->il_csr & IL_CDONE) == 0)
2459179Ssam 		;
2469179Ssam 	/*
2476893Sfeldman 	 * Set board online.
2486893Sfeldman 	 * Hang receive buffer and start any pending
2496893Sfeldman 	 * writes by faking a transmit complete.
250*25444Skarels 	 * Receive bcr is not a multiple of 8 so buffer
2516893Sfeldman 	 * chaining can't happen.
2526893Sfeldman 	 */
2536893Sfeldman 	addr->il_csr = ILC_ONLINE;
2547220Ssam 	while ((addr->il_csr & IL_CDONE) == 0)
2557220Ssam 		;
2566893Sfeldman 	addr->il_bar = is->is_ifuba.ifu_r.ifrw_info & 0xffff;
2579746Ssam 	addr->il_bcr = sizeof(struct il_rheader) + ETHERMTU + 6;
2587261Ssam 	addr->il_csr =
25913056Ssam 	    ((is->is_ifuba.ifu_r.ifrw_info >> 2) & IL_EUA)|ILC_RCV|IL_RIE;
2607220Ssam 	while ((addr->il_csr & IL_CDONE) == 0)
2617220Ssam 		;
2627261Ssam 	is->is_flags = ILF_OACTIVE;
26319865Skarels 	is->is_if.if_flags |= IFF_RUNNING;
264*25444Skarels 	is->is_flags |= ILF_RUNNING;
2657261Ssam 	is->is_lastcmd = 0;
2666893Sfeldman 	ilcint(unit);
2676893Sfeldman 	splx(s);
2686893Sfeldman }
2696893Sfeldman 
2706893Sfeldman /*
2716893Sfeldman  * Start output on interface.
2726893Sfeldman  * Get another datagram to send off of the interface queue,
2736893Sfeldman  * and map it to the interface before starting the output.
2746893Sfeldman  */
2756893Sfeldman ilstart(dev)
2766893Sfeldman 	dev_t dev;
2776893Sfeldman {
2789179Ssam         int unit = ILUNIT(dev), len;
2796893Sfeldman 	struct uba_device *ui = ilinfo[unit];
2806893Sfeldman 	register struct il_softc *is = &il_softc[unit];
2816893Sfeldman 	register struct ildevice *addr;
2826893Sfeldman 	struct mbuf *m;
2837220Ssam 	short csr;
2846893Sfeldman 
2856893Sfeldman 	IF_DEQUEUE(&is->is_if.if_snd, m);
2867261Ssam 	addr = (struct ildevice *)ui->ui_addr;
2877261Ssam 	if (m == 0) {
2887261Ssam 		if ((is->is_flags & ILF_STATPENDING) == 0)
2897261Ssam 			return;
2909179Ssam 		addr->il_bar = is->is_ubaddr & 0xffff;
2917261Ssam 		addr->il_bcr = sizeof (struct il_stats);
2927261Ssam 		csr = ((is->is_ubaddr >> 2) & IL_EUA)|ILC_STAT|IL_RIE|IL_CIE;
2937261Ssam 		is->is_flags &= ~ILF_STATPENDING;
2947261Ssam 		goto startcmd;
2957261Ssam 	}
2966893Sfeldman 	len = if_wubaput(&is->is_ifuba, m);
2979179Ssam 	/*
2989179Ssam 	 * Ensure minimum packet length.
2999179Ssam 	 * This makes the safe assumtion that there are no virtual holes
3009179Ssam 	 * after the data.
3019179Ssam 	 * For security, it might be wise to zero out the added bytes,
3029179Ssam 	 * but we're mainly interested in speed at the moment.
3039179Ssam 	 */
3049746Ssam 	if (len - sizeof(struct ether_header) < ETHERMIN)
3059746Ssam 		len = ETHERMIN + sizeof(struct ether_header);
3066893Sfeldman 	if (is->is_ifuba.ifu_flags & UBA_NEEDBDP)
3076893Sfeldman 		UBAPURGE(is->is_ifuba.ifu_uba, is->is_ifuba.ifu_w.ifrw_bdp);
3086893Sfeldman 	addr->il_bar = is->is_ifuba.ifu_w.ifrw_info & 0xffff;
3096893Sfeldman 	addr->il_bcr = len;
3107261Ssam 	csr =
3117261Ssam 	  ((is->is_ifuba.ifu_w.ifrw_info >> 2) & IL_EUA)|ILC_XMIT|IL_CIE|IL_RIE;
3127261Ssam 
3137261Ssam startcmd:
3147261Ssam 	is->is_lastcmd = csr & IL_CMD;
3157220Ssam 	addr->il_csr = csr;
3167261Ssam 	is->is_flags |= ILF_OACTIVE;
3176893Sfeldman }
3186893Sfeldman 
3196893Sfeldman /*
3206893Sfeldman  * Command done interrupt.
3216893Sfeldman  */
3226893Sfeldman ilcint(unit)
3236893Sfeldman 	int unit;
3246893Sfeldman {
3256893Sfeldman 	register struct il_softc *is = &il_softc[unit];
3267220Ssam 	struct uba_device *ui = ilinfo[unit];
3276893Sfeldman 	register struct ildevice *addr = (struct ildevice *)ui->ui_addr;
3287266Ssam 	short csr;
3296893Sfeldman 
3307261Ssam 	if ((is->is_flags & ILF_OACTIVE) == 0) {
3317220Ssam 		printf("il%d: stray xmit interrupt, csr=%b\n", unit,
3327261Ssam 			addr->il_csr, IL_BITS);
3336893Sfeldman 		return;
3346893Sfeldman 	}
3357220Ssam 
3367266Ssam 	csr = addr->il_csr;
3376893Sfeldman 	/*
3387261Ssam 	 * Hang receive buffer if it couldn't
3397261Ssam 	 * be done earlier (in ilrint).
3406893Sfeldman 	 */
3417261Ssam 	if (is->is_flags & ILF_RCVPENDING) {
342*25444Skarels 		int s;
343*25444Skarels 
3446893Sfeldman 		addr->il_bar = is->is_ifuba.ifu_r.ifrw_info & 0xffff;
3459746Ssam 		addr->il_bcr = sizeof(struct il_rheader) + ETHERMTU + 6;
3467261Ssam 		addr->il_csr =
3477261Ssam 		  ((is->is_ifuba.ifu_r.ifrw_info >> 2) & IL_EUA)|ILC_RCV|IL_RIE;
348*25444Skarels 		s = splhigh();
3497220Ssam 		while ((addr->il_csr & IL_CDONE) == 0)
3507220Ssam 			;
351*25444Skarels 		splx(s);
3527261Ssam 		is->is_flags &= ~ILF_RCVPENDING;
3536893Sfeldman 	}
3547261Ssam 	is->is_flags &= ~ILF_OACTIVE;
3557266Ssam 	csr &= IL_STATUS;
3567261Ssam 	switch (is->is_lastcmd) {
3577261Ssam 
3587261Ssam 	case ILC_XMIT:
3597261Ssam 		is->is_if.if_opackets++;
3607266Ssam 		if (csr > ILERR_RETRIES)
3617261Ssam 			is->is_if.if_oerrors++;
3627261Ssam 		break;
3637261Ssam 
3647261Ssam 	case ILC_STAT:
3657266Ssam 		if (csr == ILERR_SUCCESS)
3667261Ssam 			iltotal(is);
3677261Ssam 		break;
3687261Ssam 	}
3696893Sfeldman 	if (is->is_ifuba.ifu_xtofree) {
3706893Sfeldman 		m_freem(is->is_ifuba.ifu_xtofree);
3716893Sfeldman 		is->is_ifuba.ifu_xtofree = 0;
3726893Sfeldman 	}
3737261Ssam 	ilstart(unit);
3746893Sfeldman }
3756893Sfeldman 
3766893Sfeldman /*
3776893Sfeldman  * Ethernet interface receiver interrupt.
3786893Sfeldman  * If input error just drop packet.
3796893Sfeldman  * Otherwise purge input buffered data path and examine
3806893Sfeldman  * packet to determine type.  If can't determine length
3816893Sfeldman  * from type, then have to drop packet.  Othewise decapsulate
3826893Sfeldman  * packet based on type and pass to type specific higher-level
3836893Sfeldman  * input routine.
3846893Sfeldman  */
3856893Sfeldman ilrint(unit)
3866893Sfeldman 	int unit;
3876893Sfeldman {
3886893Sfeldman 	register struct il_softc *is = &il_softc[unit];
3896893Sfeldman 	struct ildevice *addr = (struct ildevice *)ilinfo[unit]->ui_addr;
3906893Sfeldman 	register struct il_rheader *il;
3916893Sfeldman     	struct mbuf *m;
39215787Sleres 	int len, off, resid, s;
3936893Sfeldman 	register struct ifqueue *inq;
3946893Sfeldman 
3956893Sfeldman 	is->is_if.if_ipackets++;
3966893Sfeldman 	if (is->is_ifuba.ifu_flags & UBA_NEEDBDP)
3976893Sfeldman 		UBAPURGE(is->is_ifuba.ifu_uba, is->is_ifuba.ifu_r.ifrw_bdp);
3986893Sfeldman 	il = (struct il_rheader *)(is->is_ifuba.ifu_r.ifrw_addr);
3996893Sfeldman 	len = il->ilr_length - sizeof(struct il_rheader);
4009746Ssam 	if ((il->ilr_status&(ILFSTAT_A|ILFSTAT_C)) || len < 46 ||
4019746Ssam 	    len > ETHERMTU) {
4026893Sfeldman 		is->is_if.if_ierrors++;
4036893Sfeldman #ifdef notdef
4046893Sfeldman 		if (is->is_if.if_ierrors % 100 == 0)
4056893Sfeldman 			printf("il%d: += 100 input errors\n", unit);
4066893Sfeldman #endif
4076893Sfeldman 		goto setup;
4086893Sfeldman 	}
4096893Sfeldman 
4106893Sfeldman 	/*
41119865Skarels 	 * Deal with trailer protocol: if type is trailer type
4126893Sfeldman 	 * get true type from first 16-bit word past data.
4136893Sfeldman 	 * Remember that type was trailer by setting off.
4146893Sfeldman 	 */
4159746Ssam 	il->ilr_type = ntohs((u_short)il->ilr_type);
4166893Sfeldman #define	ildataaddr(il, off, type)	((type)(((caddr_t)((il)+1)+(off))))
41719865Skarels 	if (il->ilr_type >= ETHERTYPE_TRAIL &&
41819865Skarels 	    il->ilr_type < ETHERTYPE_TRAIL+ETHERTYPE_NTRAILER) {
41919865Skarels 		off = (il->ilr_type - ETHERTYPE_TRAIL) * 512;
4209746Ssam 		if (off >= ETHERMTU)
4216893Sfeldman 			goto setup;		/* sanity */
4229746Ssam 		il->ilr_type = ntohs(*ildataaddr(il, off, u_short *));
4239746Ssam 		resid = ntohs(*(ildataaddr(il, off+2, u_short *)));
4246893Sfeldman 		if (off + resid > len)
4256893Sfeldman 			goto setup;		/* sanity */
4266893Sfeldman 		len = off + resid;
4276893Sfeldman 	} else
4286893Sfeldman 		off = 0;
4296893Sfeldman 	if (len == 0)
4306893Sfeldman 		goto setup;
4316893Sfeldman 
4326893Sfeldman 	/*
4336893Sfeldman 	 * Pull packet off interface.  Off is nonzero if packet
4346893Sfeldman 	 * has trailing header; ilget will then force this header
4356893Sfeldman 	 * information to be at the front, but we still have to drop
4366893Sfeldman 	 * the type and length which are at the front of any trailer data.
4376893Sfeldman 	 */
43824791Skarels 	m = if_rubaget(&is->is_ifuba, len, off, &is->is_if);
4396893Sfeldman 	if (m == 0)
4406893Sfeldman 		goto setup;
4416893Sfeldman 	if (off) {
44224791Skarels 		struct ifnet *ifp;
44324791Skarels 
44424791Skarels 		ifp = *(mtod(m, struct ifnet **));
4456893Sfeldman 		m->m_off += 2 * sizeof (u_short);
4466893Sfeldman 		m->m_len -= 2 * sizeof (u_short);
44724791Skarels 		*(mtod(m, struct ifnet **)) = ifp;
4486893Sfeldman 	}
4496893Sfeldman 	switch (il->ilr_type) {
4506893Sfeldman 
4516893Sfeldman #ifdef INET
45219865Skarels 	case ETHERTYPE_IP:
4536893Sfeldman 		schednetisr(NETISR_IP);
4546893Sfeldman 		inq = &ipintrq;
4556893Sfeldman 		break;
45611575Ssam 
45719865Skarels 	case ETHERTYPE_ARP:
45811575Ssam 		arpinput(&is->is_ac, m);
45913988Ssam 		goto setup;
4606893Sfeldman #endif
46123557Ssklower #ifdef NS
46223557Ssklower 	case ETHERTYPE_NS:
46323557Ssklower 		schednetisr(NETISR_NS);
46423557Ssklower 		inq = &nsintrq;
46523557Ssklower 		break;
46623557Ssklower 
46723557Ssklower #endif
4686893Sfeldman 	default:
4696893Sfeldman 		m_freem(m);
4706893Sfeldman 		goto setup;
4716893Sfeldman 	}
4726893Sfeldman 
47315787Sleres 	s = splimp();
4746893Sfeldman 	if (IF_QFULL(inq)) {
4756893Sfeldman 		IF_DROP(inq);
4766893Sfeldman 		m_freem(m);
47715787Sleres 	} else
47815787Sleres 		IF_ENQUEUE(inq, m);
47915787Sleres 	splx(s);
4806893Sfeldman 
4816893Sfeldman setup:
4826893Sfeldman 	/*
4836893Sfeldman 	 * Reset for next packet if possible.
4846893Sfeldman 	 * If waiting for transmit command completion, set flag
4856893Sfeldman 	 * and wait until command completes.
4866893Sfeldman 	 */
4877261Ssam 	if (is->is_flags & ILF_OACTIVE) {
4887261Ssam 		is->is_flags |= ILF_RCVPENDING;
4897220Ssam 		return;
4907220Ssam 	}
4917220Ssam 	addr->il_bar = is->is_ifuba.ifu_r.ifrw_info & 0xffff;
4929746Ssam 	addr->il_bcr = sizeof(struct il_rheader) + ETHERMTU + 6;
4937261Ssam 	addr->il_csr =
4947261Ssam 		((is->is_ifuba.ifu_r.ifrw_info >> 2) & IL_EUA)|ILC_RCV|IL_RIE;
495*25444Skarels 	s = splhigh();
4967220Ssam 	while ((addr->il_csr & IL_CDONE) == 0)
4977220Ssam 		;
498*25444Skarels 	splx(s);
4996893Sfeldman }
5006893Sfeldman 
5016893Sfeldman /*
5026893Sfeldman  * Ethernet output routine.
5036893Sfeldman  * Encapsulate a packet of type family for the local net.
5046893Sfeldman  * Use trailer local net encapsulation if enough data in first
5056893Sfeldman  * packet leaves a multiple of 512 bytes of data in remainder.
5066893Sfeldman  */
5076893Sfeldman iloutput(ifp, m0, dst)
5086893Sfeldman 	struct ifnet *ifp;
5096893Sfeldman 	struct mbuf *m0;
5106893Sfeldman 	struct sockaddr *dst;
5116893Sfeldman {
51211575Ssam 	int type, s, error;
51319865Skarels  	u_char edst[6];
51411575Ssam 	struct in_addr idst;
5156893Sfeldman 	register struct il_softc *is = &il_softc[ifp->if_unit];
5166893Sfeldman 	register struct mbuf *m = m0;
5179746Ssam 	register struct ether_header *il;
5189179Ssam 	register int off;
5196893Sfeldman 
520*25444Skarels 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
521*25444Skarels 		error = ENETDOWN;
522*25444Skarels 		goto bad;
523*25444Skarels 	}
5246893Sfeldman 	switch (dst->sa_family) {
5256893Sfeldman 
5266893Sfeldman #ifdef INET
5276893Sfeldman 	case AF_INET:
52811575Ssam 		idst = ((struct sockaddr_in *)dst)->sin_addr;
52919865Skarels  		if (!arpresolve(&is->is_ac, m, &idst, edst))
53011575Ssam 			return (0);	/* if not yet resolved */
5316893Sfeldman 		off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len;
53213056Ssam 		/* need per host negotiation */
53313056Ssam 		if ((ifp->if_flags & IFF_NOTRAILERS) == 0)
5346893Sfeldman 		if (off > 0 && (off & 0x1ff) == 0 &&
5356893Sfeldman 		    m->m_off >= MMINOFF + 2 * sizeof (u_short)) {
53619865Skarels 			type = ETHERTYPE_TRAIL + (off>>9);
5376893Sfeldman 			m->m_off -= 2 * sizeof (u_short);
5386893Sfeldman 			m->m_len += 2 * sizeof (u_short);
53919865Skarels 			*mtod(m, u_short *) = htons((u_short)ETHERTYPE_IP);
5409746Ssam 			*(mtod(m, u_short *) + 1) = htons((u_short)m->m_len);
5416893Sfeldman 			goto gottrailertype;
5426893Sfeldman 		}
54319865Skarels 		type = ETHERTYPE_IP;
5446893Sfeldman 		off = 0;
5456893Sfeldman 		goto gottype;
5466893Sfeldman #endif
54723557Ssklower #ifdef NS
54823557Ssklower 	case AF_NS:
54923557Ssklower 		type = ETHERTYPE_NS;
55023557Ssklower  		bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
55123557Ssklower 		(caddr_t)edst, sizeof (edst));
55223557Ssklower 		off = 0;
55323557Ssklower 		goto gottype;
55423557Ssklower #endif
5556893Sfeldman 
55611575Ssam 	case AF_UNSPEC:
55711575Ssam 		il = (struct ether_header *)dst->sa_data;
55819865Skarels  		bcopy((caddr_t)il->ether_dhost, (caddr_t)edst, sizeof (edst));
55911575Ssam 		type = il->ether_type;
56011575Ssam 		goto gottype;
56111575Ssam 
5626893Sfeldman 	default:
5636893Sfeldman 		printf("il%d: can't handle af%d\n", ifp->if_unit,
5646893Sfeldman 			dst->sa_family);
5656893Sfeldman 		error = EAFNOSUPPORT;
5666893Sfeldman 		goto bad;
5676893Sfeldman 	}
5686893Sfeldman 
5696893Sfeldman gottrailertype:
5706893Sfeldman 	/*
5716893Sfeldman 	 * Packet to be sent as trailer: move first packet
5726893Sfeldman 	 * (control information) to end of chain.
5736893Sfeldman 	 */
5746893Sfeldman 	while (m->m_next)
5756893Sfeldman 		m = m->m_next;
5766893Sfeldman 	m->m_next = m0;
5776893Sfeldman 	m = m0->m_next;
5786893Sfeldman 	m0->m_next = 0;
5796893Sfeldman 	m0 = m;
5806893Sfeldman 
5816893Sfeldman gottype:
5826893Sfeldman 	/*
5836893Sfeldman 	 * Add local net header.  If no space in first mbuf,
5846893Sfeldman 	 * allocate another.
5856893Sfeldman 	 */
5866893Sfeldman 	if (m->m_off > MMAXOFF ||
5879746Ssam 	    MMINOFF + sizeof (struct ether_header) > m->m_off) {
5889650Ssam 		m = m_get(M_DONTWAIT, MT_HEADER);
5896893Sfeldman 		if (m == 0) {
5906893Sfeldman 			error = ENOBUFS;
5916893Sfeldman 			goto bad;
5926893Sfeldman 		}
5936893Sfeldman 		m->m_next = m0;
5946893Sfeldman 		m->m_off = MMINOFF;
5959746Ssam 		m->m_len = sizeof (struct ether_header);
5966893Sfeldman 	} else {
5979746Ssam 		m->m_off -= sizeof (struct ether_header);
5989746Ssam 		m->m_len += sizeof (struct ether_header);
5996893Sfeldman 	}
6009746Ssam 	il = mtod(m, struct ether_header *);
6019746Ssam 	il->ether_type = htons((u_short)type);
60219865Skarels  	bcopy((caddr_t)edst, (caddr_t)il->ether_dhost, sizeof (edst));
60319865Skarels  	bcopy((caddr_t)is->is_addr, (caddr_t)il->ether_shost,
60419865Skarels 	    sizeof(il->ether_shost));
6056893Sfeldman 
6066893Sfeldman 	/*
6076893Sfeldman 	 * Queue message on interface, and start output if interface
6086893Sfeldman 	 * not yet active.
6096893Sfeldman 	 */
6106893Sfeldman 	s = splimp();
6116893Sfeldman 	if (IF_QFULL(&ifp->if_snd)) {
6126893Sfeldman 		IF_DROP(&ifp->if_snd);
6137220Ssam 		splx(s);
6147220Ssam 		m_freem(m);
6157220Ssam 		return (ENOBUFS);
6166893Sfeldman 	}
6176893Sfeldman 	IF_ENQUEUE(&ifp->if_snd, m);
6187261Ssam 	if ((is->is_flags & ILF_OACTIVE) == 0)
6196893Sfeldman 		ilstart(ifp->if_unit);
6206893Sfeldman 	splx(s);
6216893Sfeldman 	return (0);
6227220Ssam 
6236893Sfeldman bad:
6246893Sfeldman 	m_freem(m0);
6257220Ssam 	return (error);
6266893Sfeldman }
6277261Ssam 
6287261Ssam /*
6297261Ssam  * Watchdog routine, request statistics from board.
6307261Ssam  */
6317261Ssam ilwatch(unit)
6327261Ssam 	int unit;
6337261Ssam {
6347261Ssam 	register struct il_softc *is = &il_softc[unit];
6357261Ssam 	register struct ifnet *ifp = &is->is_if;
6367261Ssam 	int s;
6377261Ssam 
6387261Ssam 	if (is->is_flags & ILF_STATPENDING) {
6397261Ssam 		ifp->if_timer = is->is_scaninterval;
6407261Ssam 		return;
6417261Ssam 	}
6427261Ssam 	s = splimp();
6437261Ssam 	is->is_flags |= ILF_STATPENDING;
6447261Ssam 	if ((is->is_flags & ILF_OACTIVE) == 0)
6457261Ssam 		ilstart(ifp->if_unit);
6467261Ssam 	splx(s);
6477261Ssam 	ifp->if_timer = is->is_scaninterval;
6487261Ssam }
6497261Ssam 
6507261Ssam /*
6517261Ssam  * Total up the on-board statistics.
6527261Ssam  */
6537261Ssam iltotal(is)
6547261Ssam 	register struct il_softc *is;
6557261Ssam {
6567261Ssam 	register u_short *interval, *sum, *end;
6577261Ssam 
6587261Ssam 	interval = &is->is_stats.ils_frames;
6597261Ssam 	sum = &is->is_sum.ils_frames;
6607261Ssam 	end = is->is_sum.ils_fill2;
6617261Ssam 	while (sum < end)
6627261Ssam 		*sum++ += *interval++;
6637261Ssam 	is->is_if.if_collisions = is->is_sum.ils_collis;
6647261Ssam }
66513056Ssam 
66613056Ssam /*
66713056Ssam  * Process an ioctl request.
66813056Ssam  */
66913056Ssam ilioctl(ifp, cmd, data)
67013056Ssam 	register struct ifnet *ifp;
67113056Ssam 	int cmd;
67213056Ssam 	caddr_t data;
67313056Ssam {
67419865Skarels 	register struct ifaddr *ifa = (struct ifaddr *)data;
675*25444Skarels 	register struct il_softc *is = &il_softc[ifp->if_unit];
67613056Ssam 	int s = splimp(), error = 0;
67713056Ssam 
67813056Ssam 	switch (cmd) {
67913056Ssam 
68013056Ssam 	case SIOCSIFADDR:
68119865Skarels 		ifp->if_flags |= IFF_UP;
68213056Ssam 		ilinit(ifp->if_unit);
68319865Skarels 
68419865Skarels 		switch (ifa->ifa_addr.sa_family) {
68523557Ssklower #ifdef INET
68619865Skarels 		case AF_INET:
68719865Skarels 			((struct arpcom *)ifp)->ac_ipaddr =
68819865Skarels 				IA_SIN(ifa)->sin_addr;
68919865Skarels 			arpwhohas((struct arpcom *)ifp, &IA_SIN(ifa)->sin_addr);
69019865Skarels 			break;
69123557Ssklower #endif
69223557Ssklower #ifdef NS
69323557Ssklower 		case AF_NS:
69423557Ssklower 			IA_SNS(ifa)->sns_addr.x_host =
69523557Ssklower 				* (union ns_host *)
696*25444Skarels 				     (is->is_addr);
69723557Ssklower 			break;
69823557Ssklower #endif
69919865Skarels 		}
70013056Ssam 		break;
70113056Ssam 
702*25444Skarels 	case SIOCSIFFLAGS:
703*25444Skarels 		if ((ifp->if_flags & IFF_UP) == 0 &&
704*25444Skarels 		    is->is_flags & ILF_RUNNING) {
705*25444Skarels 			((struct ildevice *)
706*25444Skarels 			   (ilinfo[ifp->if_unit]->ui_addr))->il_csr = ILC_RESET;
707*25444Skarels 			is->is_flags &= ~ILF_RUNNING;
708*25444Skarels 		} else if (ifp->if_flags & IFF_UP &&
709*25444Skarels 		    (is->is_flags & ILF_RUNNING) == 0)
710*25444Skarels 			ilinit(ifp->if_unit);
711*25444Skarels 		break;
712*25444Skarels 
71313056Ssam 	default:
71413056Ssam 		error = EINVAL;
71513056Ssam 	}
71613056Ssam 	splx(s);
71713056Ssam 	return (error);
71813056Ssam }
71925274Sbloom #endif
720