xref: /csrg-svn/sys/vax/if/if_il.c (revision 25274)
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*25274Sbloom  *	@(#)if_il.c	6.10 (Berkeley) 10/24/85
723557Ssklower  */
86893Sfeldman 
96893Sfeldman #include "il.h"
10*25274Sbloom #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 
4219949Sbloom #ifdef PUP
438419Swnj #include "../netpup/pup.h"
4419949Sbloom #endif
456893Sfeldman 
4623557Ssklower #ifdef NS
4723557Ssklower #include "../netns/ns.h"
4823557Ssklower #include "../netns/ns_if.h"
4923557Ssklower #endif
5023557Ssklower 
518463Sroot #include "../vax/cpu.h"
528463Sroot #include "../vax/mtpr.h"
5317115Sbloom #include "if_il.h"
5417115Sbloom #include "if_ilreg.h"
5517115Sbloom #include "if_uba.h"
568463Sroot #include "../vaxuba/ubareg.h"
578463Sroot #include "../vaxuba/ubavar.h"
588463Sroot 
596893Sfeldman int	ilprobe(), ilattach(), ilrint(), ilcint();
606893Sfeldman struct	uba_device *ilinfo[NIL];
616893Sfeldman u_short ilstd[] = { 0 };
626893Sfeldman struct	uba_driver ildriver =
636893Sfeldman 	{ ilprobe, 0, ilattach, 0, ilstd, "il", ilinfo };
646893Sfeldman #define	ILUNIT(x)	minor(x)
6513056Ssam int	ilinit(),iloutput(),ilioctl(),ilreset(),ilwatch();
666893Sfeldman 
676893Sfeldman /*
686893Sfeldman  * Ethernet software status per interface.
696893Sfeldman  *
706893Sfeldman  * Each interface is referenced by a network interface structure,
716893Sfeldman  * is_if, which the routing code uses to locate the interface.
726893Sfeldman  * This structure contains the output queue for the interface, its address, ...
736893Sfeldman  * We also have, for each interface, a UBA interface structure, which
746893Sfeldman  * contains information about the UNIBUS resources held by the interface:
756893Sfeldman  * map registers, buffered data paths, etc.  Information is cached in this
766893Sfeldman  * structure for use by the if_uba.c routines in running the interface
776893Sfeldman  * efficiently.
786893Sfeldman  */
796893Sfeldman struct	il_softc {
8011575Ssam 	struct	arpcom is_ac;		/* Ethernet common part */
8111575Ssam #define	is_if	is_ac.ac_if		/* network-visible interface */
8211575Ssam #define	is_addr	is_ac.ac_enaddr		/* hardware Ethernet address */
836893Sfeldman 	struct	ifuba is_ifuba;		/* UNIBUS resources */
847261Ssam 	int	is_flags;
857261Ssam #define	ILF_OACTIVE	0x1		/* output is active */
867261Ssam #define	ILF_RCVPENDING	0x2		/* start rcv in ilcint */
877261Ssam #define	ILF_STATPENDING	0x4		/* stat cmd pending */
887261Ssam 	short	is_lastcmd;		/* can't read csr, so must save it */
897261Ssam 	short	is_scaninterval;	/* interval of stat collection */
907261Ssam #define	ILWATCHINTERVAL	60		/* once every 60 seconds */
917261Ssam 	struct	il_stats is_stats;	/* holds on-board statistics */
927261Ssam 	struct	il_stats is_sum;	/* summation over time */
937261Ssam 	int	is_ubaddr;		/* mapping registers of is_stats */
946893Sfeldman } il_softc[NIL];
956893Sfeldman 
966893Sfeldman ilprobe(reg)
976893Sfeldman 	caddr_t reg;
986893Sfeldman {
996893Sfeldman 	register int br, cvec;		/* r11, r10 value-result */
1006893Sfeldman 	register struct ildevice *addr = (struct ildevice *)reg;
1016893Sfeldman 	register i;
1026893Sfeldman 
1036893Sfeldman #ifdef lint
1046893Sfeldman 	br = 0; cvec = br; br = cvec;
1059179Ssam 	i = 0; ilrint(i); ilcint(i); ilwatch(i);
1066893Sfeldman #endif
1076893Sfeldman 
1086893Sfeldman 	addr->il_csr = ILC_OFFLINE|IL_CIE;
1096893Sfeldman 	DELAY(100000);
1107261Ssam 	i = addr->il_csr;		/* clear CDONE */
1116893Sfeldman 	if (cvec > 0 && cvec != 0x200)
1126893Sfeldman 		cvec -= 4;
1136893Sfeldman 	return (1);
1146893Sfeldman }
1156893Sfeldman 
1166893Sfeldman /*
1176893Sfeldman  * Interface exists: make available by filling in network interface
1186893Sfeldman  * record.  System will initialize the interface when it is ready
1196893Sfeldman  * to accept packets.  A STATUS command is done to get the ethernet
1206893Sfeldman  * address and other interesting data.
1216893Sfeldman  */
1226893Sfeldman ilattach(ui)
1236893Sfeldman 	struct uba_device *ui;
1246893Sfeldman {
1256893Sfeldman 	register struct il_softc *is = &il_softc[ui->ui_unit];
1267220Ssam 	register struct ifnet *ifp = &is->is_if;
1276893Sfeldman 	register struct ildevice *addr = (struct ildevice *)ui->ui_addr;
1286893Sfeldman 
1297220Ssam 	ifp->if_unit = ui->ui_unit;
1307220Ssam 	ifp->if_name = "il";
1319746Ssam 	ifp->if_mtu = ETHERMTU;
13219865Skarels 	ifp->if_flags = IFF_BROADCAST;
1336893Sfeldman 
1346893Sfeldman 	/*
1357261Ssam 	 * Reset the board and map the statistics
1367261Ssam 	 * buffer onto the Unibus.
1376893Sfeldman 	 */
1387261Ssam 	addr->il_csr = ILC_RESET;
1397261Ssam 	while ((addr->il_csr&IL_CDONE) == 0)
1407261Ssam 		;
1417261Ssam 	if (addr->il_csr&IL_STATUS)
1427261Ssam 		printf("il%d: reset failed, csr=%b\n", ui->ui_unit,
1437261Ssam 			addr->il_csr, IL_BITS);
1446893Sfeldman 
1459179Ssam 	is->is_ubaddr = uballoc(ui->ui_ubanum, (caddr_t)&is->is_stats,
14613056Ssam 	    sizeof (struct il_stats), 0);
1477261Ssam 	addr->il_bar = is->is_ubaddr & 0xffff;
1487261Ssam 	addr->il_bcr = sizeof (struct il_stats);
1497261Ssam 	addr->il_csr = ((is->is_ubaddr >> 2) & IL_EUA)|ILC_STAT;
1507261Ssam 	while ((addr->il_csr&IL_CDONE) == 0)
1517261Ssam 		;
1527261Ssam 	if (addr->il_csr&IL_STATUS)
1537261Ssam 		printf("il%d: status failed, csr=%b\n", ui->ui_unit,
1547261Ssam 			addr->il_csr, IL_BITS);
1557261Ssam 	ubarelse(ui->ui_ubanum, &is->is_ubaddr);
15611575Ssam #ifdef notdef
1576893Sfeldman 	printf("il%d: addr=%x:%x:%x:%x:%x:%x module=%s firmware=%s\n",
1586893Sfeldman 		ui->ui_unit,
1597261Ssam 		is->is_stats.ils_addr[0]&0xff, is->is_stats.ils_addr[1]&0xff,
1607261Ssam 		is->is_stats.ils_addr[2]&0xff, is->is_stats.ils_addr[3]&0xff,
1617261Ssam 		is->is_stats.ils_addr[4]&0xff, is->is_stats.ils_addr[5]&0xff,
1627261Ssam 		is->is_stats.ils_module, is->is_stats.ils_firmware);
16311575Ssam #endif
16419865Skarels  	bcopy((caddr_t)is->is_stats.ils_addr, (caddr_t)is->is_addr,
16519865Skarels  	    sizeof (is->is_addr));
1667220Ssam 	ifp->if_init = ilinit;
1677220Ssam 	ifp->if_output = iloutput;
16813056Ssam 	ifp->if_ioctl = ilioctl;
1698979Sroot 	ifp->if_reset = ilreset;
1706893Sfeldman 	is->is_ifuba.ifu_flags = UBA_CANTWAIT;
1717220Ssam #ifdef notdef
1727220Ssam 	is->is_ifuba.ifu_flags |= UBA_NEEDBDP;
1737220Ssam #endif
1747220Ssam 	if_attach(ifp);
1756893Sfeldman }
1766893Sfeldman 
1776893Sfeldman /*
1786893Sfeldman  * Reset of interface after UNIBUS reset.
1796893Sfeldman  * If interface is on specified uba, reset its state.
1806893Sfeldman  */
1816893Sfeldman ilreset(unit, uban)
1826893Sfeldman 	int unit, uban;
1836893Sfeldman {
1846893Sfeldman 	register struct uba_device *ui;
1856893Sfeldman 
1866893Sfeldman 	if (unit >= NIL || (ui = ilinfo[unit]) == 0 || ui->ui_alive == 0 ||
1876893Sfeldman 	    ui->ui_ubanum != uban)
1886893Sfeldman 		return;
1896893Sfeldman 	printf(" il%d", unit);
1906893Sfeldman 	ilinit(unit);
1916893Sfeldman }
1926893Sfeldman 
1936893Sfeldman /*
1946893Sfeldman  * Initialization of interface; clear recorded pending
1956893Sfeldman  * operations, and reinitialize UNIBUS usage.
1966893Sfeldman  */
1976893Sfeldman ilinit(unit)
1986893Sfeldman 	int unit;
1996893Sfeldman {
2006893Sfeldman 	register struct il_softc *is = &il_softc[unit];
2016893Sfeldman 	register struct uba_device *ui = ilinfo[unit];
2026893Sfeldman 	register struct ildevice *addr;
20313056Ssam 	register struct ifnet *ifp = &is->is_if;
2047261Ssam 	int s;
2056893Sfeldman 
20619865Skarels 	/* not yet, if address still unknown */
20719865Skarels 	if (ifp->if_addrlist == (struct ifaddr *)0)
20811575Ssam 		return;
20911575Ssam 
21013056Ssam 	if (ifp->if_flags & IFF_RUNNING)
21119865Skarels 		return;
2126893Sfeldman 	if (if_ubainit(&is->is_ifuba, ui->ui_ubanum,
2139746Ssam 	    sizeof (struct il_rheader), (int)btoc(ETHERMTU)) == 0) {
2146893Sfeldman 		printf("il%d: can't initialize\n", unit);
2156893Sfeldman 		is->is_if.if_flags &= ~IFF_UP;
2166893Sfeldman 		return;
2176893Sfeldman 	}
2189179Ssam 	is->is_ubaddr = uballoc(ui->ui_ubanum, (caddr_t)&is->is_stats,
21913056Ssam 	    sizeof (struct il_stats), 0);
22015071Skarels 	ifp->if_watchdog = ilwatch;
22115071Skarels 	is->is_scaninterval = ILWATCHINTERVAL;
22215071Skarels 	ifp->if_timer = is->is_scaninterval;
2236893Sfeldman 	addr = (struct ildevice *)ui->ui_addr;
2246893Sfeldman 
2256893Sfeldman 	/*
2269179Ssam 	 * Turn off source address insertion (it's faster this way),
22712488Ssam 	 * and set board online.  Former doesn't work if board is
22812488Ssam 	 * already online (happens on ubareset), so we put it offline
22912488Ssam 	 * first.
2309179Ssam 	 */
2319179Ssam 	s = splimp();
23212488Ssam 	addr->il_csr = ILC_OFFLINE;
2339179Ssam 	while ((addr->il_csr & IL_CDONE) == 0)
2349179Ssam 		;
23512488Ssam 	addr->il_csr = ILC_CISA;
2369179Ssam 	while ((addr->il_csr & IL_CDONE) == 0)
2379179Ssam 		;
2389179Ssam 	/*
2396893Sfeldman 	 * Set board online.
2406893Sfeldman 	 * Hang receive buffer and start any pending
2416893Sfeldman 	 * writes by faking a transmit complete.
2426893Sfeldman 	 * Receive bcr is not a muliple of 4 so buffer
2436893Sfeldman 	 * chaining can't happen.
2446893Sfeldman 	 */
2456893Sfeldman 	addr->il_csr = ILC_ONLINE;
2467220Ssam 	while ((addr->il_csr & IL_CDONE) == 0)
2477220Ssam 		;
2486893Sfeldman 	addr->il_bar = is->is_ifuba.ifu_r.ifrw_info & 0xffff;
2499746Ssam 	addr->il_bcr = sizeof(struct il_rheader) + ETHERMTU + 6;
2507261Ssam 	addr->il_csr =
25113056Ssam 	    ((is->is_ifuba.ifu_r.ifrw_info >> 2) & IL_EUA)|ILC_RCV|IL_RIE;
2527220Ssam 	while ((addr->il_csr & IL_CDONE) == 0)
2537220Ssam 		;
2547261Ssam 	is->is_flags = ILF_OACTIVE;
25519865Skarels 	is->is_if.if_flags |= IFF_RUNNING;
2567261Ssam 	is->is_lastcmd = 0;
2576893Sfeldman 	ilcint(unit);
2586893Sfeldman 	splx(s);
2596893Sfeldman }
2606893Sfeldman 
2616893Sfeldman /*
2626893Sfeldman  * Start output on interface.
2636893Sfeldman  * Get another datagram to send off of the interface queue,
2646893Sfeldman  * and map it to the interface before starting the output.
2656893Sfeldman  */
2666893Sfeldman ilstart(dev)
2676893Sfeldman 	dev_t dev;
2686893Sfeldman {
2699179Ssam         int unit = ILUNIT(dev), len;
2706893Sfeldman 	struct uba_device *ui = ilinfo[unit];
2716893Sfeldman 	register struct il_softc *is = &il_softc[unit];
2726893Sfeldman 	register struct ildevice *addr;
2736893Sfeldman 	struct mbuf *m;
2747220Ssam 	short csr;
2756893Sfeldman 
2766893Sfeldman 	IF_DEQUEUE(&is->is_if.if_snd, m);
2777261Ssam 	addr = (struct ildevice *)ui->ui_addr;
2787261Ssam 	if (m == 0) {
2797261Ssam 		if ((is->is_flags & ILF_STATPENDING) == 0)
2807261Ssam 			return;
2819179Ssam 		addr->il_bar = is->is_ubaddr & 0xffff;
2827261Ssam 		addr->il_bcr = sizeof (struct il_stats);
2837261Ssam 		csr = ((is->is_ubaddr >> 2) & IL_EUA)|ILC_STAT|IL_RIE|IL_CIE;
2847261Ssam 		is->is_flags &= ~ILF_STATPENDING;
2857261Ssam 		goto startcmd;
2867261Ssam 	}
2876893Sfeldman 	len = if_wubaput(&is->is_ifuba, m);
2889179Ssam 	/*
2899179Ssam 	 * Ensure minimum packet length.
2909179Ssam 	 * This makes the safe assumtion that there are no virtual holes
2919179Ssam 	 * after the data.
2929179Ssam 	 * For security, it might be wise to zero out the added bytes,
2939179Ssam 	 * but we're mainly interested in speed at the moment.
2949179Ssam 	 */
2959746Ssam 	if (len - sizeof(struct ether_header) < ETHERMIN)
2969746Ssam 		len = ETHERMIN + sizeof(struct ether_header);
2976893Sfeldman 	if (is->is_ifuba.ifu_flags & UBA_NEEDBDP)
2986893Sfeldman 		UBAPURGE(is->is_ifuba.ifu_uba, is->is_ifuba.ifu_w.ifrw_bdp);
2996893Sfeldman 	addr->il_bar = is->is_ifuba.ifu_w.ifrw_info & 0xffff;
3006893Sfeldman 	addr->il_bcr = len;
3017261Ssam 	csr =
3027261Ssam 	  ((is->is_ifuba.ifu_w.ifrw_info >> 2) & IL_EUA)|ILC_XMIT|IL_CIE|IL_RIE;
3037261Ssam 
3047261Ssam startcmd:
3057261Ssam 	is->is_lastcmd = csr & IL_CMD;
3067220Ssam 	addr->il_csr = csr;
3077261Ssam 	is->is_flags |= ILF_OACTIVE;
3086893Sfeldman }
3096893Sfeldman 
3106893Sfeldman /*
3116893Sfeldman  * Command done interrupt.
3126893Sfeldman  */
3136893Sfeldman ilcint(unit)
3146893Sfeldman 	int unit;
3156893Sfeldman {
3166893Sfeldman 	register struct il_softc *is = &il_softc[unit];
3177220Ssam 	struct uba_device *ui = ilinfo[unit];
3186893Sfeldman 	register struct ildevice *addr = (struct ildevice *)ui->ui_addr;
3197266Ssam 	short csr;
3206893Sfeldman 
3217261Ssam 	if ((is->is_flags & ILF_OACTIVE) == 0) {
3227220Ssam 		printf("il%d: stray xmit interrupt, csr=%b\n", unit,
3237261Ssam 			addr->il_csr, IL_BITS);
3246893Sfeldman 		return;
3256893Sfeldman 	}
3267220Ssam 
3277266Ssam 	csr = addr->il_csr;
3286893Sfeldman 	/*
3297261Ssam 	 * Hang receive buffer if it couldn't
3307261Ssam 	 * be done earlier (in ilrint).
3316893Sfeldman 	 */
3327261Ssam 	if (is->is_flags & ILF_RCVPENDING) {
3336893Sfeldman 		addr->il_bar = is->is_ifuba.ifu_r.ifrw_info & 0xffff;
3349746Ssam 		addr->il_bcr = sizeof(struct il_rheader) + ETHERMTU + 6;
3357261Ssam 		addr->il_csr =
3367261Ssam 		  ((is->is_ifuba.ifu_r.ifrw_info >> 2) & IL_EUA)|ILC_RCV|IL_RIE;
3377220Ssam 		while ((addr->il_csr & IL_CDONE) == 0)
3387220Ssam 			;
3397261Ssam 		is->is_flags &= ~ILF_RCVPENDING;
3406893Sfeldman 	}
3417261Ssam 	is->is_flags &= ~ILF_OACTIVE;
3427266Ssam 	csr &= IL_STATUS;
3437261Ssam 	switch (is->is_lastcmd) {
3447261Ssam 
3457261Ssam 	case ILC_XMIT:
3467261Ssam 		is->is_if.if_opackets++;
3477266Ssam 		if (csr > ILERR_RETRIES)
3487261Ssam 			is->is_if.if_oerrors++;
3497261Ssam 		break;
3507261Ssam 
3517261Ssam 	case ILC_STAT:
3527266Ssam 		if (csr == ILERR_SUCCESS)
3537261Ssam 			iltotal(is);
3547261Ssam 		break;
3557261Ssam 	}
3566893Sfeldman 	if (is->is_ifuba.ifu_xtofree) {
3576893Sfeldman 		m_freem(is->is_ifuba.ifu_xtofree);
3586893Sfeldman 		is->is_ifuba.ifu_xtofree = 0;
3596893Sfeldman 	}
3607261Ssam 	ilstart(unit);
3616893Sfeldman }
3626893Sfeldman 
3636893Sfeldman /*
3646893Sfeldman  * Ethernet interface receiver interrupt.
3656893Sfeldman  * If input error just drop packet.
3666893Sfeldman  * Otherwise purge input buffered data path and examine
3676893Sfeldman  * packet to determine type.  If can't determine length
3686893Sfeldman  * from type, then have to drop packet.  Othewise decapsulate
3696893Sfeldman  * packet based on type and pass to type specific higher-level
3706893Sfeldman  * input routine.
3716893Sfeldman  */
3726893Sfeldman ilrint(unit)
3736893Sfeldman 	int unit;
3746893Sfeldman {
3756893Sfeldman 	register struct il_softc *is = &il_softc[unit];
3766893Sfeldman 	struct ildevice *addr = (struct ildevice *)ilinfo[unit]->ui_addr;
3776893Sfeldman 	register struct il_rheader *il;
3786893Sfeldman     	struct mbuf *m;
37915787Sleres 	int len, off, resid, s;
3806893Sfeldman 	register struct ifqueue *inq;
3816893Sfeldman 
3826893Sfeldman 	is->is_if.if_ipackets++;
3836893Sfeldman 	if (is->is_ifuba.ifu_flags & UBA_NEEDBDP)
3846893Sfeldman 		UBAPURGE(is->is_ifuba.ifu_uba, is->is_ifuba.ifu_r.ifrw_bdp);
3856893Sfeldman 	il = (struct il_rheader *)(is->is_ifuba.ifu_r.ifrw_addr);
3866893Sfeldman 	len = il->ilr_length - sizeof(struct il_rheader);
3879746Ssam 	if ((il->ilr_status&(ILFSTAT_A|ILFSTAT_C)) || len < 46 ||
3889746Ssam 	    len > ETHERMTU) {
3896893Sfeldman 		is->is_if.if_ierrors++;
3906893Sfeldman #ifdef notdef
3916893Sfeldman 		if (is->is_if.if_ierrors % 100 == 0)
3926893Sfeldman 			printf("il%d: += 100 input errors\n", unit);
3936893Sfeldman #endif
3946893Sfeldman 		goto setup;
3956893Sfeldman 	}
3966893Sfeldman 
3976893Sfeldman 	/*
39819865Skarels 	 * Deal with trailer protocol: if type is trailer type
3996893Sfeldman 	 * get true type from first 16-bit word past data.
4006893Sfeldman 	 * Remember that type was trailer by setting off.
4016893Sfeldman 	 */
4029746Ssam 	il->ilr_type = ntohs((u_short)il->ilr_type);
4036893Sfeldman #define	ildataaddr(il, off, type)	((type)(((caddr_t)((il)+1)+(off))))
40419865Skarels 	if (il->ilr_type >= ETHERTYPE_TRAIL &&
40519865Skarels 	    il->ilr_type < ETHERTYPE_TRAIL+ETHERTYPE_NTRAILER) {
40619865Skarels 		off = (il->ilr_type - ETHERTYPE_TRAIL) * 512;
4079746Ssam 		if (off >= ETHERMTU)
4086893Sfeldman 			goto setup;		/* sanity */
4099746Ssam 		il->ilr_type = ntohs(*ildataaddr(il, off, u_short *));
4109746Ssam 		resid = ntohs(*(ildataaddr(il, off+2, u_short *)));
4116893Sfeldman 		if (off + resid > len)
4126893Sfeldman 			goto setup;		/* sanity */
4136893Sfeldman 		len = off + resid;
4146893Sfeldman 	} else
4156893Sfeldman 		off = 0;
4166893Sfeldman 	if (len == 0)
4176893Sfeldman 		goto setup;
4186893Sfeldman 
4196893Sfeldman 	/*
4206893Sfeldman 	 * Pull packet off interface.  Off is nonzero if packet
4216893Sfeldman 	 * has trailing header; ilget will then force this header
4226893Sfeldman 	 * information to be at the front, but we still have to drop
4236893Sfeldman 	 * the type and length which are at the front of any trailer data.
4246893Sfeldman 	 */
42524791Skarels 	m = if_rubaget(&is->is_ifuba, len, off, &is->is_if);
4266893Sfeldman 	if (m == 0)
4276893Sfeldman 		goto setup;
4286893Sfeldman 	if (off) {
42924791Skarels 		struct ifnet *ifp;
43024791Skarels 
43124791Skarels 		ifp = *(mtod(m, struct ifnet **));
4326893Sfeldman 		m->m_off += 2 * sizeof (u_short);
4336893Sfeldman 		m->m_len -= 2 * sizeof (u_short);
43424791Skarels 		*(mtod(m, struct ifnet **)) = ifp;
4356893Sfeldman 	}
4366893Sfeldman 	switch (il->ilr_type) {
4376893Sfeldman 
4386893Sfeldman #ifdef INET
43919865Skarels 	case ETHERTYPE_IP:
4406893Sfeldman 		schednetisr(NETISR_IP);
4416893Sfeldman 		inq = &ipintrq;
4426893Sfeldman 		break;
44311575Ssam 
44419865Skarels 	case ETHERTYPE_ARP:
44511575Ssam 		arpinput(&is->is_ac, m);
44613988Ssam 		goto setup;
4476893Sfeldman #endif
44823557Ssklower #ifdef NS
44923557Ssklower 	case ETHERTYPE_NS:
45023557Ssklower 		schednetisr(NETISR_NS);
45123557Ssklower 		inq = &nsintrq;
45223557Ssklower 		break;
45323557Ssklower 
45423557Ssklower #endif
4556893Sfeldman 	default:
4566893Sfeldman 		m_freem(m);
4576893Sfeldman 		goto setup;
4586893Sfeldman 	}
4596893Sfeldman 
46015787Sleres 	s = splimp();
4616893Sfeldman 	if (IF_QFULL(inq)) {
4626893Sfeldman 		IF_DROP(inq);
4636893Sfeldman 		m_freem(m);
46415787Sleres 	} else
46515787Sleres 		IF_ENQUEUE(inq, m);
46615787Sleres 	splx(s);
4676893Sfeldman 
4686893Sfeldman setup:
4696893Sfeldman 	/*
4706893Sfeldman 	 * Reset for next packet if possible.
4716893Sfeldman 	 * If waiting for transmit command completion, set flag
4726893Sfeldman 	 * and wait until command completes.
4736893Sfeldman 	 */
4747261Ssam 	if (is->is_flags & ILF_OACTIVE) {
4757261Ssam 		is->is_flags |= ILF_RCVPENDING;
4767220Ssam 		return;
4777220Ssam 	}
4787220Ssam 	addr->il_bar = is->is_ifuba.ifu_r.ifrw_info & 0xffff;
4799746Ssam 	addr->il_bcr = sizeof(struct il_rheader) + ETHERMTU + 6;
4807261Ssam 	addr->il_csr =
4817261Ssam 		((is->is_ifuba.ifu_r.ifrw_info >> 2) & IL_EUA)|ILC_RCV|IL_RIE;
4827220Ssam 	while ((addr->il_csr & IL_CDONE) == 0)
4837220Ssam 		;
4846893Sfeldman }
4856893Sfeldman 
4866893Sfeldman /*
4876893Sfeldman  * Ethernet output routine.
4886893Sfeldman  * Encapsulate a packet of type family for the local net.
4896893Sfeldman  * Use trailer local net encapsulation if enough data in first
4906893Sfeldman  * packet leaves a multiple of 512 bytes of data in remainder.
4916893Sfeldman  */
4926893Sfeldman iloutput(ifp, m0, dst)
4936893Sfeldman 	struct ifnet *ifp;
4946893Sfeldman 	struct mbuf *m0;
4956893Sfeldman 	struct sockaddr *dst;
4966893Sfeldman {
49711575Ssam 	int type, s, error;
49819865Skarels  	u_char edst[6];
49911575Ssam 	struct in_addr idst;
5006893Sfeldman 	register struct il_softc *is = &il_softc[ifp->if_unit];
5016893Sfeldman 	register struct mbuf *m = m0;
5029746Ssam 	register struct ether_header *il;
5039179Ssam 	register int off;
5046893Sfeldman 
5056893Sfeldman 	switch (dst->sa_family) {
5066893Sfeldman 
5076893Sfeldman #ifdef INET
5086893Sfeldman 	case AF_INET:
50911575Ssam 		idst = ((struct sockaddr_in *)dst)->sin_addr;
51019865Skarels  		if (!arpresolve(&is->is_ac, m, &idst, edst))
51111575Ssam 			return (0);	/* if not yet resolved */
5126893Sfeldman 		off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len;
51313056Ssam 		/* need per host negotiation */
51413056Ssam 		if ((ifp->if_flags & IFF_NOTRAILERS) == 0)
5156893Sfeldman 		if (off > 0 && (off & 0x1ff) == 0 &&
5166893Sfeldman 		    m->m_off >= MMINOFF + 2 * sizeof (u_short)) {
51719865Skarels 			type = ETHERTYPE_TRAIL + (off>>9);
5186893Sfeldman 			m->m_off -= 2 * sizeof (u_short);
5196893Sfeldman 			m->m_len += 2 * sizeof (u_short);
52019865Skarels 			*mtod(m, u_short *) = htons((u_short)ETHERTYPE_IP);
5219746Ssam 			*(mtod(m, u_short *) + 1) = htons((u_short)m->m_len);
5226893Sfeldman 			goto gottrailertype;
5236893Sfeldman 		}
52419865Skarels 		type = ETHERTYPE_IP;
5256893Sfeldman 		off = 0;
5266893Sfeldman 		goto gottype;
5276893Sfeldman #endif
52823557Ssklower #ifdef NS
52923557Ssklower 	case AF_NS:
53023557Ssklower 		type = ETHERTYPE_NS;
53123557Ssklower  		bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
53223557Ssklower 		(caddr_t)edst, sizeof (edst));
53323557Ssklower 		off = 0;
53423557Ssklower 		goto gottype;
53523557Ssklower #endif
5366893Sfeldman 
53711575Ssam 	case AF_UNSPEC:
53811575Ssam 		il = (struct ether_header *)dst->sa_data;
53919865Skarels  		bcopy((caddr_t)il->ether_dhost, (caddr_t)edst, sizeof (edst));
54011575Ssam 		type = il->ether_type;
54111575Ssam 		goto gottype;
54211575Ssam 
5436893Sfeldman 	default:
5446893Sfeldman 		printf("il%d: can't handle af%d\n", ifp->if_unit,
5456893Sfeldman 			dst->sa_family);
5466893Sfeldman 		error = EAFNOSUPPORT;
5476893Sfeldman 		goto bad;
5486893Sfeldman 	}
5496893Sfeldman 
5506893Sfeldman gottrailertype:
5516893Sfeldman 	/*
5526893Sfeldman 	 * Packet to be sent as trailer: move first packet
5536893Sfeldman 	 * (control information) to end of chain.
5546893Sfeldman 	 */
5556893Sfeldman 	while (m->m_next)
5566893Sfeldman 		m = m->m_next;
5576893Sfeldman 	m->m_next = m0;
5586893Sfeldman 	m = m0->m_next;
5596893Sfeldman 	m0->m_next = 0;
5606893Sfeldman 	m0 = m;
5616893Sfeldman 
5626893Sfeldman gottype:
5636893Sfeldman 	/*
5646893Sfeldman 	 * Add local net header.  If no space in first mbuf,
5656893Sfeldman 	 * allocate another.
5666893Sfeldman 	 */
5676893Sfeldman 	if (m->m_off > MMAXOFF ||
5689746Ssam 	    MMINOFF + sizeof (struct ether_header) > m->m_off) {
5699650Ssam 		m = m_get(M_DONTWAIT, MT_HEADER);
5706893Sfeldman 		if (m == 0) {
5716893Sfeldman 			error = ENOBUFS;
5726893Sfeldman 			goto bad;
5736893Sfeldman 		}
5746893Sfeldman 		m->m_next = m0;
5756893Sfeldman 		m->m_off = MMINOFF;
5769746Ssam 		m->m_len = sizeof (struct ether_header);
5776893Sfeldman 	} else {
5789746Ssam 		m->m_off -= sizeof (struct ether_header);
5799746Ssam 		m->m_len += sizeof (struct ether_header);
5806893Sfeldman 	}
5819746Ssam 	il = mtod(m, struct ether_header *);
5829746Ssam 	il->ether_type = htons((u_short)type);
58319865Skarels  	bcopy((caddr_t)edst, (caddr_t)il->ether_dhost, sizeof (edst));
58419865Skarels  	bcopy((caddr_t)is->is_addr, (caddr_t)il->ether_shost,
58519865Skarels 	    sizeof(il->ether_shost));
5866893Sfeldman 
5876893Sfeldman 	/*
5886893Sfeldman 	 * Queue message on interface, and start output if interface
5896893Sfeldman 	 * not yet active.
5906893Sfeldman 	 */
5916893Sfeldman 	s = splimp();
5926893Sfeldman 	if (IF_QFULL(&ifp->if_snd)) {
5936893Sfeldman 		IF_DROP(&ifp->if_snd);
5947220Ssam 		splx(s);
5957220Ssam 		m_freem(m);
5967220Ssam 		return (ENOBUFS);
5976893Sfeldman 	}
5986893Sfeldman 	IF_ENQUEUE(&ifp->if_snd, m);
5997261Ssam 	if ((is->is_flags & ILF_OACTIVE) == 0)
6006893Sfeldman 		ilstart(ifp->if_unit);
6016893Sfeldman 	splx(s);
6026893Sfeldman 	return (0);
6037220Ssam 
6046893Sfeldman bad:
6056893Sfeldman 	m_freem(m0);
6067220Ssam 	return (error);
6076893Sfeldman }
6087261Ssam 
6097261Ssam /*
6107261Ssam  * Watchdog routine, request statistics from board.
6117261Ssam  */
6127261Ssam ilwatch(unit)
6137261Ssam 	int unit;
6147261Ssam {
6157261Ssam 	register struct il_softc *is = &il_softc[unit];
6167261Ssam 	register struct ifnet *ifp = &is->is_if;
6177261Ssam 	int s;
6187261Ssam 
6197261Ssam 	if (is->is_flags & ILF_STATPENDING) {
6207261Ssam 		ifp->if_timer = is->is_scaninterval;
6217261Ssam 		return;
6227261Ssam 	}
6237261Ssam 	s = splimp();
6247261Ssam 	is->is_flags |= ILF_STATPENDING;
6257261Ssam 	if ((is->is_flags & ILF_OACTIVE) == 0)
6267261Ssam 		ilstart(ifp->if_unit);
6277261Ssam 	splx(s);
6287261Ssam 	ifp->if_timer = is->is_scaninterval;
6297261Ssam }
6307261Ssam 
6317261Ssam /*
6327261Ssam  * Total up the on-board statistics.
6337261Ssam  */
6347261Ssam iltotal(is)
6357261Ssam 	register struct il_softc *is;
6367261Ssam {
6377261Ssam 	register u_short *interval, *sum, *end;
6387261Ssam 
6397261Ssam 	interval = &is->is_stats.ils_frames;
6407261Ssam 	sum = &is->is_sum.ils_frames;
6417261Ssam 	end = is->is_sum.ils_fill2;
6427261Ssam 	while (sum < end)
6437261Ssam 		*sum++ += *interval++;
6447261Ssam 	is->is_if.if_collisions = is->is_sum.ils_collis;
6457261Ssam }
64613056Ssam 
64713056Ssam /*
64813056Ssam  * Process an ioctl request.
64913056Ssam  */
65013056Ssam ilioctl(ifp, cmd, data)
65113056Ssam 	register struct ifnet *ifp;
65213056Ssam 	int cmd;
65313056Ssam 	caddr_t data;
65413056Ssam {
65519865Skarels 	register struct ifaddr *ifa = (struct ifaddr *)data;
65613056Ssam 	int s = splimp(), error = 0;
65713056Ssam 
65813056Ssam 	switch (cmd) {
65913056Ssam 
66013056Ssam 	case SIOCSIFADDR:
66119865Skarels 		ifp->if_flags |= IFF_UP;
66213056Ssam 		ilinit(ifp->if_unit);
66319865Skarels 
66419865Skarels 		switch (ifa->ifa_addr.sa_family) {
66523557Ssklower #ifdef INET
66619865Skarels 		case AF_INET:
66719865Skarels 			((struct arpcom *)ifp)->ac_ipaddr =
66819865Skarels 				IA_SIN(ifa)->sin_addr;
66919865Skarels 			arpwhohas((struct arpcom *)ifp, &IA_SIN(ifa)->sin_addr);
67019865Skarels 			break;
67123557Ssklower #endif
67223557Ssklower #ifdef NS
67323557Ssklower 		case AF_NS:
67423557Ssklower 			IA_SNS(ifa)->sns_addr.x_host =
67523557Ssklower 				* (union ns_host *)
67623557Ssklower 				     (il_softc[ifp->if_unit].is_addr);
67723557Ssklower 			break;
67823557Ssklower #endif
67919865Skarels 		}
68013056Ssam 		break;
68113056Ssam 
68213056Ssam 	default:
68313056Ssam 		error = EINVAL;
68413056Ssam 	}
68513056Ssam 	splx(s);
68613056Ssam 	return (error);
68713056Ssam }
688*25274Sbloom #endif
689