xref: /csrg-svn/sys/vax/if/if_il.c (revision 7220)
1*7220Ssam /*	if_il.c	4.7	82/06/18	*/
26893Sfeldman 
36893Sfeldman #include "il.h"
46893Sfeldman 
56893Sfeldman /*
66893Sfeldman  * Interlan Ethernet Communications Controller interface
76893Sfeldman  */
86893Sfeldman #include "../h/param.h"
96893Sfeldman #include "../h/systm.h"
106893Sfeldman #include "../h/mbuf.h"
116893Sfeldman #include "../h/pte.h"
126893Sfeldman #include "../h/buf.h"
136893Sfeldman #include "../h/protosw.h"
146893Sfeldman #include "../h/socket.h"
156893Sfeldman #include "../h/ubareg.h"
166893Sfeldman #include "../h/ubavar.h"
176893Sfeldman #include "../h/ilreg.h"
186893Sfeldman #include "../h/cpu.h"
196893Sfeldman #include "../h/mtpr.h"
206893Sfeldman #include "../h/vmmac.h"
216893Sfeldman #include "../net/in.h"
226893Sfeldman #include "../net/in_systm.h"
236893Sfeldman #include "../net/if.h"
246893Sfeldman #include "../net/if_il.h"
256893Sfeldman #include "../net/if_uba.h"
266893Sfeldman #include "../net/ip.h"
276893Sfeldman #include "../net/ip_var.h"
286893Sfeldman #include "../net/pup.h"
296893Sfeldman #include "../net/route.h"
306893Sfeldman #include <errno.h>
316893Sfeldman 
326893Sfeldman #define	ILMTU	1500
336893Sfeldman 
346893Sfeldman int	ilprobe(), ilattach(), ilrint(), ilcint();
356893Sfeldman struct	uba_device *ilinfo[NIL];
366893Sfeldman u_short ilstd[] = { 0 };
376893Sfeldman struct	uba_driver ildriver =
386893Sfeldman 	{ ilprobe, 0, ilattach, 0, ilstd, "il", ilinfo };
396893Sfeldman #define	ILUNIT(x)	minor(x)
406893Sfeldman int	ilinit(),iloutput(),ilreset();
416893Sfeldman 
42*7220Ssam u_char	il_ectop[3] = { 0x02, 0x60, 0x8c };
43*7220Ssam u_char	ilbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
44*7220Ssam 
456893Sfeldman /*
466893Sfeldman  * Ethernet software status per interface.
476893Sfeldman  *
486893Sfeldman  * Each interface is referenced by a network interface structure,
496893Sfeldman  * is_if, which the routing code uses to locate the interface.
506893Sfeldman  * This structure contains the output queue for the interface, its address, ...
516893Sfeldman  * We also have, for each interface, a UBA interface structure, which
526893Sfeldman  * contains information about the UNIBUS resources held by the interface:
536893Sfeldman  * map registers, buffered data paths, etc.  Information is cached in this
546893Sfeldman  * structure for use by the if_uba.c routines in running the interface
556893Sfeldman  * efficiently.
566893Sfeldman  */
576893Sfeldman struct	il_softc {
586893Sfeldman 	struct	ifnet is_if;		/* network-visible interface */
596893Sfeldman 	struct	ifuba is_ifuba;		/* UNIBUS resources */
606893Sfeldman 	short	is_oactive;		/* is output active? */
616893Sfeldman 	short	is_startrcv;		/* hang receive next chance */
626893Sfeldman 	u_char	is_enaddr[6];		/* board's ethernet address */
636893Sfeldman } il_softc[NIL];
646893Sfeldman 
656893Sfeldman /*
666893Sfeldman  * Do an OFFLINE command.  This will cause an interrupt for the
676893Sfeldman  * autoconfigure stuff.
686893Sfeldman  */
696893Sfeldman ilprobe(reg)
706893Sfeldman 	caddr_t reg;
716893Sfeldman {
726893Sfeldman 	register int br, cvec;		/* r11, r10 value-result */
736893Sfeldman 	register struct ildevice *addr = (struct ildevice *)reg;
746893Sfeldman 	register i;
756893Sfeldman 
766893Sfeldman COUNT(ILPROBE);
776893Sfeldman #ifdef lint
786893Sfeldman 	br = 0; cvec = br; br = cvec;
796893Sfeldman 	ilrint(0); ilcint(0);
806893Sfeldman #endif
816893Sfeldman 
826893Sfeldman 	addr->il_csr = ILC_OFFLINE|IL_CIE;
836893Sfeldman 	DELAY(100000);
846893Sfeldman 	i = addr->il_csr;		/* Clear CDONE */
856893Sfeldman 	if (cvec > 0 && cvec != 0x200)
866893Sfeldman 		cvec -= 4;
876893Sfeldman 	return (1);
886893Sfeldman }
896893Sfeldman 
906893Sfeldman struct il_stat ilbuf;
916893Sfeldman /*
926893Sfeldman  * Interface exists: make available by filling in network interface
936893Sfeldman  * record.  System will initialize the interface when it is ready
946893Sfeldman  * to accept packets.  A STATUS command is done to get the ethernet
956893Sfeldman  * address and other interesting data.
966893Sfeldman  */
976893Sfeldman ilattach(ui)
986893Sfeldman 	struct uba_device *ui;
996893Sfeldman {
1006893Sfeldman 	register struct il_softc *is = &il_softc[ui->ui_unit];
101*7220Ssam 	register struct ifnet *ifp = &is->is_if;
1026893Sfeldman 	register struct ildevice *addr = (struct ildevice *)ui->ui_addr;
103*7220Ssam 	register short csr;
104*7220Ssam 	struct sockaddr_in *sin;
105*7220Ssam 	int i, s, ubaddr;
1066893Sfeldman COUNT(ILATTACH);
1076893Sfeldman 
108*7220Ssam 	ifp->if_unit = ui->ui_unit;
109*7220Ssam 	ifp->if_name = "il";
110*7220Ssam 	ifp->if_mtu = ILMTU;
111*7220Ssam 	ifp->if_net = ui->ui_flags;
1126893Sfeldman 
1136893Sfeldman 	/*
1146893Sfeldman 	 * Reset the board
1156893Sfeldman 	 */
1166893Sfeldman 	s = splimp();
117*7220Ssam 	csr = ((ubaddr>>2)&0xc000)|ILC_RESET;
118*7220Ssam 	addr->il_csr = csr;
119*7220Ssam 	do
120*7220Ssam 		csr = addr->il_csr;
121*7220Ssam 	while ((csr&IL_CDONE) == 0);
122*7220Ssam 	if (csr &= IL_STATUS)
123*7220Ssam 		printf("il%d: %s\n", ui->ui_unit, ildiag[csr]);
1246893Sfeldman 	splx(s);
1256893Sfeldman 
1266893Sfeldman 	/*
1276893Sfeldman 	 * Map the status buffer to the Unibus, do the status command,
1286893Sfeldman 	 * and unmap the buffer.
1296893Sfeldman 	 */
1306893Sfeldman 	ubaddr = uballoc(ui->ui_ubanum, &ilbuf, sizeof(ilbuf), 0);
1316893Sfeldman 	s = splimp();
1326893Sfeldman 	addr->il_bar = ubaddr & 0xffff;
1336893Sfeldman 	addr->il_bcr = sizeof(ilbuf);
134*7220Ssam 	csr = ((ubaddr>>2)&0xc000)|ILC_STAT;
135*7220Ssam 	addr->il_csr = csr;
136*7220Ssam 	do
137*7220Ssam 		csr = addr->il_csr;
138*7220Ssam 	while ((csr&IL_CDONE) == 0);
139*7220Ssam 	if (csr &= IL_STATUS)
140*7220Ssam 		printf("il%d: %s\n", ui->ui_unit, ilerrs[csr]);
1416893Sfeldman 	splx(s);
1426893Sfeldman 	ubarelse(ui->ui_ubanum, &ubaddr);
1436893Sfeldman 	/*
1446893Sfeldman 	 * Fill in the Ethernet address from the status buffer
1456893Sfeldman 	 */
146*7220Ssam 	bcopy(ilbuf.ils_addr, is->is_enaddr, 6);
1476893Sfeldman 	printf("il%d: addr=%x:%x:%x:%x:%x:%x module=%s firmware=%s\n",
1486893Sfeldman 		ui->ui_unit,
1496893Sfeldman 		is->is_enaddr[0]&0xff, is->is_enaddr[1]&0xff,
1506893Sfeldman 		is->is_enaddr[2]&0xff, is->is_enaddr[3]&0xff,
1516893Sfeldman 		is->is_enaddr[4]&0xff, is->is_enaddr[5]&0xff,
1526893Sfeldman 		ilbuf.ils_module, ilbuf.ils_firmware);
153*7220Ssam 	ifp->if_host[0] = ((is->is_enaddr[3]&0xff)<<16) | 0x800000 |
1546893Sfeldman 	    ((is->is_enaddr[4]&0xff)<<8) | (is->is_enaddr[5]&0xff);
155*7220Ssam 	sin = (struct sockaddr_in *)&ifp->if_addr;
1566893Sfeldman 	sin->sin_family = AF_INET;
157*7220Ssam 	sin->sin_addr = if_makeaddr(ifp->if_net, ifp->if_host[0]);
1586893Sfeldman 
159*7220Ssam 	sin = (struct sockaddr_in *)&ifp->if_broadaddr;
1606893Sfeldman 	sin->sin_family = AF_INET;
161*7220Ssam 	sin->sin_addr = if_makeaddr(ifp->if_net, INADDR_ANY);
162*7220Ssam 	ifp->if_flags = IFF_BROADCAST;
1636893Sfeldman 
164*7220Ssam 	ifp->if_init = ilinit;
165*7220Ssam 	ifp->if_output = iloutput;
166*7220Ssam 	ifp->if_ubareset = ilreset;
1676893Sfeldman 	is->is_ifuba.ifu_flags = UBA_CANTWAIT;
168*7220Ssam #ifdef notdef
169*7220Ssam 	is->is_ifuba.ifu_flags |= UBA_NEEDBDP;
170*7220Ssam #endif
171*7220Ssam 	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 COUNT(ILRESET);
1836893Sfeldman 
1846893Sfeldman 	if (unit >= NIL || (ui = ilinfo[unit]) == 0 || ui->ui_alive == 0 ||
1856893Sfeldman 	    ui->ui_ubanum != uban)
1866893Sfeldman 		return;
1876893Sfeldman 	printf(" il%d", unit);
1886893Sfeldman 	ilinit(unit);
1896893Sfeldman }
1906893Sfeldman 
1916893Sfeldman /*
1926893Sfeldman  * Initialization of interface; clear recorded pending
1936893Sfeldman  * operations, and reinitialize UNIBUS usage.
1946893Sfeldman  */
1956893Sfeldman ilinit(unit)
1966893Sfeldman 	int unit;
1976893Sfeldman {
1986893Sfeldman 	register struct il_softc *is = &il_softc[unit];
1996893Sfeldman 	register struct uba_device *ui = ilinfo[unit];
2006893Sfeldman 	register struct ildevice *addr;
201*7220Ssam 	int i, s;
202*7220Ssam 	short csr;
2036893Sfeldman 
2046893Sfeldman 	if (if_ubainit(&is->is_ifuba, ui->ui_ubanum,
2056893Sfeldman 	    sizeof (struct il_rheader), (int)btoc(ILMTU)) == 0) {
2066893Sfeldman 		printf("il%d: can't initialize\n", unit);
2076893Sfeldman 		is->is_if.if_flags &= ~IFF_UP;
2086893Sfeldman 		return;
2096893Sfeldman 	}
2106893Sfeldman 	addr = (struct ildevice *)ui->ui_addr;
2116893Sfeldman 
2126893Sfeldman 	/*
2136893Sfeldman 	 * Set board online.
2146893Sfeldman 	 * Hang receive buffer and start any pending
2156893Sfeldman 	 * writes by faking a transmit complete.
2166893Sfeldman 	 * Receive bcr is not a muliple of 4 so buffer
2176893Sfeldman 	 * chaining can't happen.
2186893Sfeldman 	 */
2196893Sfeldman 	s = splimp();
2206893Sfeldman 	addr->il_csr = ILC_ONLINE;
221*7220Ssam 	while ((addr->il_csr & IL_CDONE) == 0)
222*7220Ssam 		;
2236893Sfeldman 	addr->il_bar = is->is_ifuba.ifu_r.ifrw_info & 0xffff;
2246893Sfeldman 	addr->il_bcr = sizeof(struct il_rheader) + ILMTU + 6;
225*7220Ssam 	csr = ((is->is_ifuba.ifu_r.ifrw_info>>2)&0xc000)|ILC_RCV|IL_RIE;
226*7220Ssam 	addr->il_csr = csr;
227*7220Ssam 	while ((addr->il_csr & IL_CDONE) == 0)
228*7220Ssam 		;
2296893Sfeldman 	is->is_startrcv = 0;
2306893Sfeldman 	is->is_oactive = 1;
2316893Sfeldman 	is->is_if.if_flags |= IFF_UP;
2326893Sfeldman 	ilcint(unit);
2336893Sfeldman 	splx(s);
2347152Swnj 	if_rtinit(&is->is_if, RTF_UP);
2356893Sfeldman }
2366893Sfeldman 
2376893Sfeldman /*
2386893Sfeldman  * Start output on interface.
2396893Sfeldman  * Get another datagram to send off of the interface queue,
2406893Sfeldman  * and map it to the interface before starting the output.
2416893Sfeldman  */
2426893Sfeldman ilstart(dev)
2436893Sfeldman 	dev_t dev;
2446893Sfeldman {
245*7220Ssam         int unit = ILUNIT(dev), dest, len;
2466893Sfeldman 	struct uba_device *ui = ilinfo[unit];
2476893Sfeldman 	register struct il_softc *is = &il_softc[unit];
2486893Sfeldman 	register struct ildevice *addr;
2496893Sfeldman 	struct mbuf *m;
250*7220Ssam 	short csr;
2516893Sfeldman COUNT(ILSTART);
2526893Sfeldman 
2536893Sfeldman 	IF_DEQUEUE(&is->is_if.if_snd, m);
2546893Sfeldman 	if (m == 0)
2556893Sfeldman 		return;
2566893Sfeldman 	len = if_wubaput(&is->is_ifuba, m);
2576893Sfeldman 	if (is->is_ifuba.ifu_flags & UBA_NEEDBDP)
2586893Sfeldman 		UBAPURGE(is->is_ifuba.ifu_uba, is->is_ifuba.ifu_w.ifrw_bdp);
2596893Sfeldman 	addr = (struct ildevice *)ui->ui_addr;
2606893Sfeldman 	addr->il_bar = is->is_ifuba.ifu_w.ifrw_info & 0xffff;
2616893Sfeldman 	addr->il_bcr = len;
262*7220Ssam 	csr = ((is->is_ifuba.ifu_w.ifrw_info>>2)&0xc000)|ILC_XMIT|IL_CIE|IL_RIE;
263*7220Ssam 	addr->il_csr = csr;
2646893Sfeldman 	is->is_oactive = 1;
2656893Sfeldman }
2666893Sfeldman 
2676893Sfeldman /*
2686893Sfeldman  * Command done interrupt.
2696893Sfeldman  * This should only happen after a transmit command,
2706893Sfeldman  * so it is equivalent to a transmit interrupt.
2716893Sfeldman  * Start another output if more data to send.
2726893Sfeldman  */
2736893Sfeldman ilcint(unit)
2746893Sfeldman 	int unit;
2756893Sfeldman {
2766893Sfeldman 	register struct il_softc *is = &il_softc[unit];
277*7220Ssam 	struct uba_device *ui = ilinfo[unit];
2786893Sfeldman 	register struct ildevice *addr = (struct ildevice *)ui->ui_addr;
279*7220Ssam 	short csr;
2806893Sfeldman COUNT(ILCINT);
2816893Sfeldman 
282*7220Ssam 	csr = addr->il_csr;
2836893Sfeldman 	if (is->is_oactive == 0) {
284*7220Ssam 		printf("il%d: stray xmit interrupt, csr=%b\n", unit,
285*7220Ssam 			csr, IL_BITS);
2866893Sfeldman 		return;
2876893Sfeldman 	}
2886893Sfeldman 	is->is_if.if_opackets++;
2896893Sfeldman 	is->is_oactive = 0;
290*7220Ssam 	if (csr &= IL_STATUS) {
2916893Sfeldman 		is->is_if.if_oerrors++;
292*7220Ssam 		printf("il%d: output error %d\n", unit, csr);
2936893Sfeldman 	}
294*7220Ssam 
2956893Sfeldman 	/*
2966893Sfeldman 	 * Hang receive buffer if it couldn't be done earlier (in ilrint).
2976893Sfeldman 	 */
2986893Sfeldman 	if (is->is_startrcv) {
2996893Sfeldman 		addr->il_bar = is->is_ifuba.ifu_r.ifrw_info & 0xffff;
3006893Sfeldman 		addr->il_bcr = sizeof(struct il_rheader) + ILMTU + 6;
301*7220Ssam 		csr = ((is->is_ifuba.ifu_r.ifrw_info>>2)&0xc000)|ILC_RCV|IL_RIE;
302*7220Ssam 		addr->il_csr = csr;
303*7220Ssam 		while ((addr->il_csr & IL_CDONE) == 0)
304*7220Ssam 			;
3056893Sfeldman 		is->is_startrcv = 0;
3066893Sfeldman 	}
3076893Sfeldman 	if (is->is_ifuba.ifu_xtofree) {
3086893Sfeldman 		m_freem(is->is_ifuba.ifu_xtofree);
3096893Sfeldman 		is->is_ifuba.ifu_xtofree = 0;
3106893Sfeldman 	}
311*7220Ssam 	if (is->is_if.if_snd.ifq_head)
312*7220Ssam 		ilstart(unit);
3136893Sfeldman }
3146893Sfeldman 
3156893Sfeldman /*
3166893Sfeldman  * Ethernet interface receiver interrupt.
3176893Sfeldman  * If input error just drop packet.
3186893Sfeldman  * Otherwise purge input buffered data path and examine
3196893Sfeldman  * packet to determine type.  If can't determine length
3206893Sfeldman  * from type, then have to drop packet.  Othewise decapsulate
3216893Sfeldman  * packet based on type and pass to type specific higher-level
3226893Sfeldman  * input routine.
3236893Sfeldman  */
3246893Sfeldman ilrint(unit)
3256893Sfeldman 	int unit;
3266893Sfeldman {
3276893Sfeldman 	register struct il_softc *is = &il_softc[unit];
3286893Sfeldman 	struct ildevice *addr = (struct ildevice *)ilinfo[unit]->ui_addr;
3296893Sfeldman 	register struct il_rheader *il;
3306893Sfeldman     	struct mbuf *m;
3316893Sfeldman 	int len, off, resid;
3326893Sfeldman 	register struct ifqueue *inq;
333*7220Ssam 	short csr;
3346893Sfeldman 
3356893Sfeldman COUNT(ILRINT);
3366893Sfeldman 	is->is_if.if_ipackets++;
3376893Sfeldman 	if (is->is_ifuba.ifu_flags & UBA_NEEDBDP)
3386893Sfeldman 		UBAPURGE(is->is_ifuba.ifu_uba, is->is_ifuba.ifu_r.ifrw_bdp);
3396893Sfeldman 	il = (struct il_rheader *)(is->is_ifuba.ifu_r.ifrw_addr);
3406893Sfeldman 	len = il->ilr_length - sizeof(struct il_rheader);
341*7220Ssam 	if ((il->ilr_status&0x3) || len < 46 || len > ILMTU) {
3426893Sfeldman 		is->is_if.if_ierrors++;
3436893Sfeldman #ifdef notdef
3446893Sfeldman 		if (is->is_if.if_ierrors % 100 == 0)
3456893Sfeldman 			printf("il%d: += 100 input errors\n", unit);
3466893Sfeldman #endif
3476893Sfeldman 		printf("il%d: input error (status=%x, len=%d)\n", unit,
3486893Sfeldman 		    il->ilr_status, len);
3496893Sfeldman 		goto setup;
3506893Sfeldman 	}
3516893Sfeldman 
3526893Sfeldman 	/*
3536893Sfeldman 	 * Deal with trailer protocol: if type is PUP trailer
3546893Sfeldman 	 * get true type from first 16-bit word past data.
3556893Sfeldman 	 * Remember that type was trailer by setting off.
3566893Sfeldman 	 */
3576893Sfeldman #define	ildataaddr(il, off, type)	((type)(((caddr_t)((il)+1)+(off))))
3586893Sfeldman 	if (il->ilr_type >= ILPUP_TRAIL &&
3596893Sfeldman 	    il->ilr_type < ILPUP_TRAIL+ILPUP_NTRAILER) {
3606893Sfeldman 		off = (il->ilr_type - ILPUP_TRAIL) * 512;
3616893Sfeldman 		if (off >= ILMTU)
3626893Sfeldman 			goto setup;		/* sanity */
3636893Sfeldman 		il->ilr_type = *ildataaddr(il, off, u_short *);
3646893Sfeldman 		resid = *(ildataaddr(il, off+2, u_short *));
3656893Sfeldman 		if (off + resid > len)
3666893Sfeldman 			goto setup;		/* sanity */
3676893Sfeldman 		len = off + resid;
3686893Sfeldman 	} else
3696893Sfeldman 		off = 0;
3706893Sfeldman 	if (len == 0)
3716893Sfeldman 		goto setup;
3726893Sfeldman 
3736893Sfeldman 	/*
3746893Sfeldman 	 * Pull packet off interface.  Off is nonzero if packet
3756893Sfeldman 	 * has trailing header; ilget will then force this header
3766893Sfeldman 	 * information to be at the front, but we still have to drop
3776893Sfeldman 	 * the type and length which are at the front of any trailer data.
3786893Sfeldman 	 */
3796893Sfeldman 	m = if_rubaget(&is->is_ifuba, len, off);
3806893Sfeldman 	if (m == 0)
3816893Sfeldman 		goto setup;
3826893Sfeldman 	if (off) {
3836893Sfeldman 		m->m_off += 2 * sizeof (u_short);
3846893Sfeldman 		m->m_len -= 2 * sizeof (u_short);
3856893Sfeldman 	}
3866893Sfeldman 	switch (il->ilr_type) {
3876893Sfeldman 
3886893Sfeldman #ifdef INET
3896893Sfeldman 	case ILPUP_IPTYPE:
3906893Sfeldman 		schednetisr(NETISR_IP);
3916893Sfeldman 		inq = &ipintrq;
3926893Sfeldman 		break;
3936893Sfeldman #endif
3946893Sfeldman 	default:
3956893Sfeldman 		m_freem(m);
3966893Sfeldman 		goto setup;
3976893Sfeldman 	}
3986893Sfeldman 
3996893Sfeldman 	if (IF_QFULL(inq)) {
4006893Sfeldman 		IF_DROP(inq);
4016893Sfeldman 		m_freem(m);
402*7220Ssam 		goto setup;
403*7220Ssam 	}
404*7220Ssam 	IF_ENQUEUE(inq, m);
4056893Sfeldman 
4066893Sfeldman setup:
4076893Sfeldman 	/*
4086893Sfeldman 	 * Reset for next packet if possible.
4096893Sfeldman 	 * If waiting for transmit command completion, set flag
4106893Sfeldman 	 * and wait until command completes.
4116893Sfeldman 	 */
412*7220Ssam 	if (is->is_oactive) {
4136893Sfeldman 		is->is_startrcv = 1;
414*7220Ssam 		return;
415*7220Ssam 	}
416*7220Ssam 	addr->il_bar = is->is_ifuba.ifu_r.ifrw_info & 0xffff;
417*7220Ssam 	addr->il_bcr = sizeof(struct il_rheader) + ILMTU + 6;
418*7220Ssam 	csr = ((is->is_ifuba.ifu_r.ifrw_info>>2)&0xc000)|ILC_RCV|IL_RIE;
419*7220Ssam 	addr->il_csr = csr;
420*7220Ssam 	while ((addr->il_csr & IL_CDONE) == 0)
421*7220Ssam 		;
4226893Sfeldman }
4236893Sfeldman 
4246893Sfeldman /*
4256893Sfeldman  * Ethernet output routine.
4266893Sfeldman  * Encapsulate a packet of type family for the local net.
4276893Sfeldman  * Use trailer local net encapsulation if enough data in first
4286893Sfeldman  * packet leaves a multiple of 512 bytes of data in remainder.
4296893Sfeldman  */
4306893Sfeldman iloutput(ifp, m0, dst)
4316893Sfeldman 	struct ifnet *ifp;
4326893Sfeldman 	struct mbuf *m0;
4336893Sfeldman 	struct sockaddr *dst;
4346893Sfeldman {
4356893Sfeldman 	int type, dest, s, error;
4366893Sfeldman 	register struct il_softc *is = &il_softc[ifp->if_unit];
4376893Sfeldman 	register struct mbuf *m = m0;
4386893Sfeldman 	register struct il_xheader *il;
439*7220Ssam 	register int off, i;
4406893Sfeldman 
4416893Sfeldman COUNT(ILOUTPUT);
4426893Sfeldman 	switch (dst->sa_family) {
4436893Sfeldman 
4446893Sfeldman #ifdef INET
4456893Sfeldman 	case AF_INET:
4466893Sfeldman 		dest = ((struct sockaddr_in *)dst)->sin_addr.s_addr;
4476893Sfeldman 		off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len;
4486893Sfeldman 		if (off > 0 && (off & 0x1ff) == 0 &&
4496893Sfeldman 		    m->m_off >= MMINOFF + 2 * sizeof (u_short)) {
4506893Sfeldman 			type = ILPUP_TRAIL + (off>>9);
4516893Sfeldman 			m->m_off -= 2 * sizeof (u_short);
4526893Sfeldman 			m->m_len += 2 * sizeof (u_short);
4536893Sfeldman 			*mtod(m, u_short *) = ILPUP_IPTYPE;
4546893Sfeldman 			*(mtod(m, u_short *) + 1) = m->m_len;
4556893Sfeldman 			goto gottrailertype;
4566893Sfeldman 		}
4576893Sfeldman 		type = ILPUP_IPTYPE;
4586893Sfeldman 		off = 0;
4596893Sfeldman 		goto gottype;
4606893Sfeldman #endif
4616893Sfeldman 
4626893Sfeldman 	default:
4636893Sfeldman 		printf("il%d: can't handle af%d\n", ifp->if_unit,
4646893Sfeldman 			dst->sa_family);
4656893Sfeldman 		error = EAFNOSUPPORT;
4666893Sfeldman 		goto bad;
4676893Sfeldman 	}
4686893Sfeldman 
4696893Sfeldman gottrailertype:
4706893Sfeldman 	/*
4716893Sfeldman 	 * Packet to be sent as trailer: move first packet
4726893Sfeldman 	 * (control information) to end of chain.
4736893Sfeldman 	 */
4746893Sfeldman 	while (m->m_next)
4756893Sfeldman 		m = m->m_next;
4766893Sfeldman 	m->m_next = m0;
4776893Sfeldman 	m = m0->m_next;
4786893Sfeldman 	m0->m_next = 0;
4796893Sfeldman 	m0 = m;
4806893Sfeldman 
4816893Sfeldman gottype:
4826893Sfeldman 	/*
4836893Sfeldman 	 * Add local net header.  If no space in first mbuf,
4846893Sfeldman 	 * allocate another.
4856893Sfeldman 	 */
4866893Sfeldman 	if (m->m_off > MMAXOFF ||
4876893Sfeldman 	    MMINOFF + sizeof (struct il_xheader) > m->m_off) {
4886893Sfeldman 		m = m_get(M_DONTWAIT);
4896893Sfeldman 		if (m == 0) {
4906893Sfeldman 			error = ENOBUFS;
4916893Sfeldman 			goto bad;
4926893Sfeldman 		}
4936893Sfeldman 		m->m_next = m0;
4946893Sfeldman 		m->m_off = MMINOFF;
4956893Sfeldman 		m->m_len = sizeof (struct il_xheader);
4966893Sfeldman 	} else {
4976893Sfeldman 		m->m_off -= sizeof (struct il_xheader);
4986893Sfeldman 		m->m_len += sizeof (struct il_xheader);
4996893Sfeldman 	}
5006893Sfeldman 	il = mtod(m, struct il_xheader *);
5016893Sfeldman 	if ((dest &~ 0xff) == 0)
502*7220Ssam 		bcopy(ilbroadcastaddr, il->ilx_dhost, 6);
5036893Sfeldman 	else {
504*7220Ssam 		u_char *to = dest & 0x8000 ? is->is_enaddr : il_ectop;
505*7220Ssam 
506*7220Ssam 		bcopy(to, il->ilx_dhost, 3);
5076893Sfeldman 		il->ilx_dhost[3] = (dest>>8) & 0x7f;
5086893Sfeldman 		il->ilx_dhost[4] = (dest>>16) & 0xff;
5096893Sfeldman 		il->ilx_dhost[5] = (dest>>24) & 0xff;
5106893Sfeldman 	}
5116893Sfeldman 	il->ilx_type = type;
5126893Sfeldman 
5136893Sfeldman 	/*
5146893Sfeldman 	 * Queue message on interface, and start output if interface
5156893Sfeldman 	 * not yet active.
5166893Sfeldman 	 */
5176893Sfeldman 	s = splimp();
5186893Sfeldman 	if (IF_QFULL(&ifp->if_snd)) {
5196893Sfeldman 		IF_DROP(&ifp->if_snd);
520*7220Ssam 		splx(s);
521*7220Ssam 		m_freem(m);
522*7220Ssam 		return (ENOBUFS);
5236893Sfeldman 	}
5246893Sfeldman 	IF_ENQUEUE(&ifp->if_snd, m);
5256893Sfeldman 	if (is->is_oactive == 0)
5266893Sfeldman 		ilstart(ifp->if_unit);
5276893Sfeldman 	splx(s);
5286893Sfeldman 	return (0);
529*7220Ssam 
5306893Sfeldman bad:
5316893Sfeldman 	m_freem(m0);
532*7220Ssam 	return (error);
5336893Sfeldman }
534