xref: /csrg-svn/sys/hp300/dev/if_le.c (revision 47567)
141480Smckusick /*
241480Smckusick  * Copyright (c) 1982, 1990 The Regents of the University of California.
341480Smckusick  * All rights reserved.
441480Smckusick  *
541480Smckusick  * %sccs.include.redist.c%
641480Smckusick  *
7*47567Smccanne  *	@(#)if_le.c	7.3 (Berkeley) 03/19/91
841480Smckusick  */
941480Smckusick 
1041480Smckusick #include "le.h"
1141480Smckusick #if NLE > 0
1241480Smckusick 
13*47567Smccanne #include "bpfilter.h"
14*47567Smccanne 
1541480Smckusick /*
1641480Smckusick  * AMD 7990 LANCE
1741480Smckusick  *
1841480Smckusick  * This driver will generate and accept tailer encapsulated packets even
1941480Smckusick  * though it buys us nothing.  The motivation was to avoid incompatibilities
2041480Smckusick  * with VAXen, SUNs, and others that handle and benefit from them.
2141480Smckusick  * This reasoning is dubious.
2241480Smckusick  */
2345788Sbostic #include "sys/param.h"
2445788Sbostic #include "sys/systm.h"
2545788Sbostic #include "sys/mbuf.h"
2645788Sbostic #include "sys/buf.h"
2745788Sbostic #include "sys/protosw.h"
2845788Sbostic #include "sys/socket.h"
2945788Sbostic #include "sys/syslog.h"
3045788Sbostic #include "sys/ioctl.h"
3145788Sbostic #include "sys/errno.h"
3241480Smckusick 
3345788Sbostic #include "net/if.h"
3445788Sbostic #include "net/netisr.h"
3545788Sbostic #include "net/route.h"
3641480Smckusick 
3741480Smckusick #ifdef INET
3845788Sbostic #include "netinet/in.h"
3945788Sbostic #include "netinet/in_systm.h"
4045788Sbostic #include "netinet/in_var.h"
4145788Sbostic #include "netinet/ip.h"
4245788Sbostic #include "netinet/if_ether.h"
4341480Smckusick #endif
4441480Smckusick 
4541480Smckusick #ifdef NS
4645788Sbostic #include "netns/ns.h"
4745788Sbostic #include "netns/ns_if.h"
4841480Smckusick #endif
4941480Smckusick 
5041480Smckusick #ifdef RMP
5145788Sbostic #include "netrmp/rmp.h"
5245788Sbostic #include "netrmp/rmp_var.h"
5341480Smckusick #endif
5441480Smckusick 
5545788Sbostic #include "../include/cpu.h"
5645788Sbostic #include "../hp300/isr.h"
5745788Sbostic #include "../include/mtpr.h"
5841480Smckusick #include "device.h"
5941480Smckusick #include "if_lereg.h"
6041480Smckusick 
61*47567Smccanne #if NBPFILTER > 0
62*47567Smccanne #include "../net/bpf.h"
63*47567Smccanne #include "../net/bpfdesc.h"
64*47567Smccanne #endif
65*47567Smccanne 
6641480Smckusick /* offsets for:	   ID,   REGS,    MEM,  NVRAM */
6741480Smckusick int	lestd[] = { 0, 0x4000, 0x8000, 0xC008 };
6841480Smckusick 
6941480Smckusick int	leattach();
7041480Smckusick struct	driver ledriver = {
7141480Smckusick 	leattach, "le",
7241480Smckusick };
7341480Smckusick 
7441480Smckusick struct	isr le_isr[NLE];
7541480Smckusick int	ledebug = 0;		/* console error messages */
7641480Smckusick 
7741480Smckusick int	leintr(), leinit(), leioctl(), lestart(), ether_output();
7841480Smckusick struct	mbuf *leget();
7941480Smckusick extern	struct ifnet loif;
8041480Smckusick 
8141480Smckusick /*
8241480Smckusick  * Ethernet software status per interface.
8341480Smckusick  *
8441480Smckusick  * Each interface is referenced by a network interface structure,
8541480Smckusick  * le_if, which the routing code uses to locate the interface.
8641480Smckusick  * This structure contains the output queue for the interface, its address, ...
8741480Smckusick  */
8841480Smckusick struct	le_softc {
8941480Smckusick 	struct	arpcom sc_ac;	/* common Ethernet structures */
9041480Smckusick #define	sc_if	sc_ac.ac_if	/* network-visible interface */
9141480Smckusick #define	sc_addr	sc_ac.ac_enaddr	/* hardware Ethernet address */
9241480Smckusick 	struct	lereg0 *sc_r0;	/* DIO registers */
9341480Smckusick 	struct	lereg1 *sc_r1;	/* LANCE registers */
9441480Smckusick 	struct	lereg2 *sc_r2;	/* dual-port RAM */
9541480Smckusick 	int	sc_rmd;		/* predicted next rmd to process */
9641480Smckusick 	int	sc_runt;
9741480Smckusick 	int	sc_jab;
9841480Smckusick 	int	sc_merr;
9941480Smckusick 	int	sc_babl;
10041480Smckusick 	int	sc_cerr;
10141480Smckusick 	int	sc_miss;
10241480Smckusick 	int	sc_xint;
10341480Smckusick 	int	sc_xown;
10441480Smckusick 	int	sc_uflo;
10541480Smckusick 	int	sc_rxlen;
10641480Smckusick 	int	sc_rxoff;
10741480Smckusick 	int	sc_txoff;
10841480Smckusick 	int	sc_busy;
109*47567Smccanne #if NBPFILTER > 0
110*47567Smccanne 	caddr_t sc_bpf;
111*47567Smccanne #endif
11241480Smckusick } le_softc[NLE];
11341480Smckusick 
11441480Smckusick /* access LANCE registers */
11541480Smckusick #define	LERDWR(cntl, src, dst) \
11641480Smckusick 	do { \
11741480Smckusick 		(dst) = (src); \
11841480Smckusick 	} while (((cntl)->ler0_status & LE_ACK) == 0);
11941480Smckusick 
12041480Smckusick /*
12141480Smckusick  * Interface exists: make available by filling in network interface
12241480Smckusick  * record.  System will initialize the interface when it is ready
12341480Smckusick  * to accept packets.
12441480Smckusick  */
12541480Smckusick leattach(hd)
12641480Smckusick 	struct hp_device *hd;
12741480Smckusick {
12841480Smckusick 	register struct lereg0 *ler0;
12941480Smckusick 	register struct lereg2 *ler2;
13041480Smckusick 	struct lereg2 *lemem = 0;
13141480Smckusick 	struct le_softc *le = &le_softc[hd->hp_unit];
13241480Smckusick 	struct ifnet *ifp = &le->sc_if;
13341480Smckusick 	char *cp;
13441480Smckusick 	int i;
13541480Smckusick 
13641480Smckusick 	ler0 = le->sc_r0 = (struct lereg0 *)(lestd[0] + (int)hd->hp_addr);
13741480Smckusick 	le->sc_r1 = (struct lereg1 *)(lestd[1] + (int)hd->hp_addr);
13841480Smckusick 	ler2 = le->sc_r2 = (struct lereg2 *)(lestd[2] + (int)hd->hp_addr);
13941480Smckusick 	if (ler0->ler0_id != LEID)
14041480Smckusick 		return(0);
14141480Smckusick 	le_isr[hd->hp_unit].isr_intr = leintr;
14241480Smckusick 	hd->hp_ipl = le_isr[hd->hp_unit].isr_ipl = LE_IPL(ler0->ler0_status);
14341480Smckusick 	le_isr[hd->hp_unit].isr_arg = hd->hp_unit;
14441480Smckusick 	ler0->ler0_id = 0xFF;
14541480Smckusick 	DELAY(100);
14641480Smckusick 
14741480Smckusick 	/*
14841480Smckusick 	 * Read the ethernet address off the board, one nibble at a time.
14941480Smckusick 	 */
15041480Smckusick 	cp = (char *)(lestd[3] + (int)hd->hp_addr);
15141480Smckusick 	for (i = 0; i < sizeof(le->sc_addr); i++) {
15241480Smckusick 		le->sc_addr[i] = (*++cp & 0xF) << 4;
15341480Smckusick 		cp++;
15441480Smckusick 		le->sc_addr[i] |= *++cp & 0xF;
15541480Smckusick 		cp++;
15641480Smckusick 	}
15741480Smckusick 	printf("le%d: hardware address %s\n", hd->hp_unit,
15841480Smckusick 		ether_sprintf(le->sc_addr));
15941480Smckusick 
16041480Smckusick 	/*
16141480Smckusick 	 * Setup for transmit/receive
16241480Smckusick 	 */
16341480Smckusick 	ler2->ler2_mode = LE_MODE;
16441480Smckusick 	ler2->ler2_padr[0] = le->sc_addr[1];
16541480Smckusick 	ler2->ler2_padr[1] = le->sc_addr[0];
16641480Smckusick 	ler2->ler2_padr[2] = le->sc_addr[3];
16741480Smckusick 	ler2->ler2_padr[3] = le->sc_addr[2];
16841480Smckusick 	ler2->ler2_padr[4] = le->sc_addr[5];
16941480Smckusick 	ler2->ler2_padr[5] = le->sc_addr[4];
17041480Smckusick #ifdef RMP
17141480Smckusick 	/*
17241480Smckusick 	 * Set up logical addr filter to accept multicast 9:0:9:0:0:4
17341480Smckusick 	 * This should be an ioctl() to the driver.  (XXX)
17441480Smckusick 	 */
17541480Smckusick 	ler2->ler2_ladrf0 = 0x00100000;
17641480Smckusick 	ler2->ler2_ladrf1 = 0x0;
17741480Smckusick #else
17841480Smckusick 	ler2->ler2_ladrf0 = 0;
17941480Smckusick 	ler2->ler2_ladrf1 = 0;
18041480Smckusick #endif
18141480Smckusick 	ler2->ler2_rlen = LE_RLEN;
18241480Smckusick 	ler2->ler2_rdra = (int)lemem->ler2_rmd;
18341480Smckusick 	ler2->ler2_tlen = LE_TLEN;
18441480Smckusick 	ler2->ler2_tdra = (int)lemem->ler2_tmd;
18541480Smckusick 	isrlink(&le_isr[hd->hp_unit]);
18641480Smckusick 	ler0->ler0_status = LE_IE;
18741480Smckusick 
18841480Smckusick 	ifp->if_unit = hd->hp_unit;
18941480Smckusick 	ifp->if_name = "le";
19041480Smckusick 	ifp->if_mtu = ETHERMTU;
19141480Smckusick 	ifp->if_init = leinit;
19241480Smckusick 	ifp->if_ioctl = leioctl;
19341480Smckusick 	ifp->if_output = ether_output;
19441480Smckusick 	ifp->if_start = lestart;
19541480Smckusick 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX;
196*47567Smccanne #if NBPFILTER > 0
197*47567Smccanne 	{
198*47567Smccanne 		static struct bpf_devp dev =
199*47567Smccanne 			{ DLT_EN10MB, sizeof(struct ether_header) };
200*47567Smccanne 
201*47567Smccanne 		bpfattach(&le->sc_bpf, ifp, &dev);
202*47567Smccanne         }
203*47567Smccanne #endif
20441480Smckusick 	if_attach(ifp);
20541480Smckusick 	return (1);
20641480Smckusick }
20741480Smckusick 
20841480Smckusick ledrinit(ler2)
20941480Smckusick 	register struct lereg2 *ler2;
21041480Smckusick {
21141480Smckusick 	register struct lereg2 *lemem = 0;
21241480Smckusick 	register int i;
21341480Smckusick 
21441480Smckusick 	for (i = 0; i < LERBUF; i++) {
21541480Smckusick 		ler2->ler2_rmd[i].rmd0 = (int)lemem->ler2_rbuf[i];
21641480Smckusick 		ler2->ler2_rmd[i].rmd1 = LE_OWN;
21741480Smckusick 		ler2->ler2_rmd[i].rmd2 = -LEMTU;
21841480Smckusick 		ler2->ler2_rmd[i].rmd3 = 0;
21941480Smckusick 	}
22041480Smckusick 	for (i = 0; i < LETBUF; i++) {
22141480Smckusick 		ler2->ler2_tmd[i].tmd0 = (int)lemem->ler2_tbuf[i];
22241480Smckusick 		ler2->ler2_tmd[i].tmd1 = 0;
22341480Smckusick 		ler2->ler2_tmd[i].tmd2 = 0;
22441480Smckusick 		ler2->ler2_tmd[i].tmd3 = 0;
22541480Smckusick 	}
22641480Smckusick }
22741480Smckusick 
22841480Smckusick lereset(unit)
22941480Smckusick 	register int unit;
23041480Smckusick {
23141480Smckusick 	register struct le_softc *le = &le_softc[unit];
23241480Smckusick 	register struct lereg0 *ler0 = le->sc_r0;
23341480Smckusick 	register struct lereg1 *ler1 = le->sc_r1;
23441480Smckusick 	register struct lereg2 *lemem = 0;
23541480Smckusick 	register int timo = 100000;
23641480Smckusick 	register int stat;
23741480Smckusick 
23841480Smckusick #ifdef lint
23941480Smckusick 	stat = unit;
24041480Smckusick #endif
241*47567Smccanne #if NBPFILTER > 0
242*47567Smccanne 	if (le->sc_if.if_flags & IFF_PROMISC)
243*47567Smccanne 		/* set the promiscuous bit */
244*47567Smccanne 		le->sc_r2->ler2_mode = LE_MODE|0x8000;
245*47567Smccanne 	else
246*47567Smccanne 		le->sc_r2->ler2_mode = LE_MODE;
247*47567Smccanne #endif
24841480Smckusick 	LERDWR(ler0, LE_CSR0, ler1->ler1_rap);
24941480Smckusick 	LERDWR(ler0, LE_STOP, ler1->ler1_rdp);
25041480Smckusick 	ledrinit(le->sc_r2);
25141480Smckusick 	le->sc_rmd = 0;
25241480Smckusick 	LERDWR(ler0, LE_CSR1, ler1->ler1_rap);
25341480Smckusick 	LERDWR(ler0, (int)&lemem->ler2_mode, ler1->ler1_rdp);
25441480Smckusick 	LERDWR(ler0, LE_CSR2, ler1->ler1_rap);
25541480Smckusick 	LERDWR(ler0, 0, ler1->ler1_rdp);
25641480Smckusick 	LERDWR(ler0, LE_CSR0, ler1->ler1_rap);
25741480Smckusick 	LERDWR(ler0, LE_INIT, ler1->ler1_rdp);
25841480Smckusick 	do {
25941480Smckusick 		if (--timo == 0) {
26041480Smckusick 			printf("le%d: init timeout, stat = 0x%x\n",
26141480Smckusick 			       unit, stat);
26241480Smckusick 			break;
26341480Smckusick 		}
26441480Smckusick 		LERDWR(ler0, ler1->ler1_rdp, stat);
26541480Smckusick 	} while ((stat & LE_IDON) == 0);
26641480Smckusick 	LERDWR(ler0, LE_STOP, ler1->ler1_rdp);
26741480Smckusick 	LERDWR(ler0, LE_CSR3, ler1->ler1_rap);
26841480Smckusick 	LERDWR(ler0, LE_BSWP, ler1->ler1_rdp);
26941480Smckusick 	LERDWR(ler0, LE_CSR0, ler1->ler1_rap);
27041480Smckusick 	LERDWR(ler0, LE_STRT | LE_INEA, ler1->ler1_rdp);
27141480Smckusick 	le->sc_if.if_flags &= ~IFF_OACTIVE;
27241480Smckusick }
27341480Smckusick 
27441480Smckusick /*
27541480Smckusick  * Initialization of interface
27641480Smckusick  */
27741480Smckusick leinit(unit)
27841480Smckusick 	int unit;
27941480Smckusick {
28041480Smckusick 	struct le_softc *le = &le_softc[unit];
28141480Smckusick 	register struct ifnet *ifp = &le->sc_if;
28241480Smckusick 	int s;
28341480Smckusick 
28441480Smckusick 	/* not yet, if address still unknown */
28541480Smckusick 	if (ifp->if_addrlist == (struct ifaddr *)0)
28641480Smckusick 		return;
28741480Smckusick 	if ((ifp->if_flags & IFF_RUNNING) == 0) {
28841480Smckusick 		s = splimp();
28941480Smckusick 		ifp->if_flags |= IFF_RUNNING;
29041480Smckusick 		lereset(unit);
29141480Smckusick 	        (void) lestart(ifp);
29241480Smckusick 		splx(s);
29341480Smckusick 	}
29441480Smckusick }
29541480Smckusick 
29641480Smckusick /*
29741480Smckusick  * Start output on interface.  Get another datagram to send
29841480Smckusick  * off of the interface queue, and copy it to the interface
29941480Smckusick  * before starting the output.
30041480Smckusick  */
30141480Smckusick lestart(ifp)
30241480Smckusick 	struct ifnet *ifp;
30341480Smckusick {
30441480Smckusick 	register struct le_softc *le = &le_softc[ifp->if_unit];
30541480Smckusick 	register struct letmd *tmd;
30641480Smckusick 	register struct mbuf *m;
30741480Smckusick 	int len;
30841480Smckusick 
30941480Smckusick 	if ((le->sc_if.if_flags & IFF_RUNNING) == 0)
31041480Smckusick 		return (0);
31141480Smckusick 	IF_DEQUEUE(&le->sc_if.if_snd, m);
31241480Smckusick 	if (m == 0)
31341480Smckusick 		return (0);
314*47567Smccanne #if NBPFILTER > 0
315*47567Smccanne 	/*
316*47567Smccanne 	 * If bpf is listening on this interface, let it
317*47567Smccanne 	 * see the packet before we commit it to the wire.
318*47567Smccanne 	 */
319*47567Smccanne 	if (le->sc_bpf)
320*47567Smccanne                 bpf_tap(le->sc_bpf, le->sc_r2->ler2_tbuf[0], len);
321*47567Smccanne #endif
32241480Smckusick 	len = leput(le->sc_r2->ler2_tbuf[0], m);
32341480Smckusick 	tmd = le->sc_r2->ler2_tmd;
32441480Smckusick 	tmd->tmd3 = 0;
32541480Smckusick 	tmd->tmd2 = -len;
32641480Smckusick 	tmd->tmd1 = LE_OWN | LE_STP | LE_ENP;
32741480Smckusick 	le->sc_if.if_flags |= IFF_OACTIVE;
32841480Smckusick 	return (0);
32941480Smckusick }
33041480Smckusick 
33141480Smckusick leintr(unit)
33241480Smckusick 	register int unit;
33341480Smckusick {
33441480Smckusick 	register struct le_softc *le = &le_softc[unit];
33541480Smckusick 	register struct lereg0 *ler0 = le->sc_r0;
33641480Smckusick 	register struct lereg1 *ler1;
33741480Smckusick 	register int stat;
33841480Smckusick 
33941480Smckusick 	if ((ler0->ler0_status & LE_IR) == 0)
34041480Smckusick 		return(0);
34141480Smckusick 	if (ler0->ler0_status & LE_JAB) {
34241480Smckusick 		le->sc_jab++;
34341480Smckusick 		lereset(unit);
34441480Smckusick 		return(1);
34541480Smckusick 	}
34641480Smckusick 	ler1 = le->sc_r1;
34741480Smckusick 	LERDWR(ler0, ler1->ler1_rdp, stat);
34841480Smckusick 	if (stat & LE_SERR) {
34941480Smckusick 		leerror(unit, stat);
35041480Smckusick 		if (stat & LE_MERR) {
35141480Smckusick 			le->sc_merr++;
35241480Smckusick 			lereset(unit);
35341480Smckusick 			return(1);
35441480Smckusick 		}
35541480Smckusick 		if (stat & LE_BABL)
35641480Smckusick 			le->sc_babl++;
35741480Smckusick 		if (stat & LE_CERR)
35841480Smckusick 			le->sc_cerr++;
35941480Smckusick 		if (stat & LE_MISS)
36041480Smckusick 			le->sc_miss++;
36141480Smckusick 		LERDWR(ler0, LE_BABL|LE_CERR|LE_MISS|LE_INEA, ler1->ler1_rdp);
36241480Smckusick 	}
36341480Smckusick 	if ((stat & LE_RXON) == 0) {
36441480Smckusick 		le->sc_rxoff++;
36541480Smckusick 		lereset(unit);
36641480Smckusick 		return(1);
36741480Smckusick 	}
36841480Smckusick 	if ((stat & LE_TXON) == 0) {
36941480Smckusick 		le->sc_txoff++;
37041480Smckusick 		lereset(unit);
37141480Smckusick 		return(1);
37241480Smckusick 	}
37341480Smckusick 	if (stat & LE_RINT) {
37441480Smckusick 		/* interrupt is cleared in lerint */
37541480Smckusick 		lerint(unit);
37641480Smckusick 	}
37741480Smckusick 	if (stat & LE_TINT) {
37841480Smckusick 		LERDWR(ler0, LE_TINT|LE_INEA, ler1->ler1_rdp);
37941480Smckusick 		lexint(unit);
38041480Smckusick 	}
38141480Smckusick 	return(1);
38241480Smckusick }
38341480Smckusick 
38441480Smckusick /*
38541480Smckusick  * Ethernet interface transmitter interrupt.
38641480Smckusick  * Start another output if more data to send.
38741480Smckusick  */
38841480Smckusick lexint(unit)
38941480Smckusick 	register int unit;
39041480Smckusick {
39141480Smckusick 	register struct le_softc *le = &le_softc[unit];
39241480Smckusick 	register struct letmd *tmd = le->sc_r2->ler2_tmd;
39341480Smckusick 
39441480Smckusick 	if ((le->sc_if.if_flags & IFF_OACTIVE) == 0) {
39541480Smckusick 		le->sc_xint++;
39641480Smckusick 		return;
39741480Smckusick 	}
39841480Smckusick 	if (tmd->tmd1 & LE_OWN) {
39941480Smckusick 		le->sc_xown++;
40041480Smckusick 		return;
40141480Smckusick 	}
40241480Smckusick 	if (tmd->tmd1 & LE_ERR) {
40341480Smckusick err:
40441480Smckusick 		lexerror(unit);
40541480Smckusick 		le->sc_if.if_oerrors++;
40641480Smckusick 		if (tmd->tmd3 & (LE_TBUFF|LE_UFLO)) {
40741480Smckusick 			le->sc_uflo++;
40841480Smckusick 			lereset(unit);
40941480Smckusick 		}
41041480Smckusick 		else if (tmd->tmd3 & LE_LCOL)
41141480Smckusick 			le->sc_if.if_collisions++;
41241480Smckusick 		else if (tmd->tmd3 & LE_RTRY)
41341480Smckusick 			le->sc_if.if_collisions += 16;
41441480Smckusick 	}
41541480Smckusick 	else if (tmd->tmd3 & LE_TBUFF)
41641480Smckusick 		/* XXX documentation says BUFF not included in ERR */
41741480Smckusick 		goto err;
41841480Smckusick 	else if (tmd->tmd1 & LE_ONE)
41941480Smckusick 		le->sc_if.if_collisions++;
42041480Smckusick 	else if (tmd->tmd1 & LE_MORE)
42141480Smckusick 		/* what is the real number? */
42241480Smckusick 		le->sc_if.if_collisions += 2;
42341480Smckusick 	else
42441480Smckusick 		le->sc_if.if_opackets++;
42541480Smckusick 	le->sc_if.if_flags &= ~IFF_OACTIVE;
42641480Smckusick 	(void) lestart(&le->sc_if);
42741480Smckusick }
42841480Smckusick 
42941480Smckusick #define	LENEXTRMP \
43041480Smckusick 	if (++bix == LERBUF) bix = 0, rmd = le->sc_r2->ler2_rmd; else ++rmd
43141480Smckusick 
43241480Smckusick /*
43341480Smckusick  * Ethernet interface receiver interrupt.
43441480Smckusick  * If input error just drop packet.
43541480Smckusick  * Decapsulate packet based on type and pass to type specific
43641480Smckusick  * higher-level input routine.
43741480Smckusick  */
43841480Smckusick lerint(unit)
43941480Smckusick 	int unit;
44041480Smckusick {
44141480Smckusick 	register struct le_softc *le = &le_softc[unit];
44241480Smckusick 	register int bix = le->sc_rmd;
44341480Smckusick 	register struct lermd *rmd = &le->sc_r2->ler2_rmd[bix];
44441480Smckusick 
44541480Smckusick 	/*
44641480Smckusick 	 * Out of sync with hardware, should never happen?
44741480Smckusick 	 */
44841480Smckusick 	if (rmd->rmd1 & LE_OWN) {
44941480Smckusick 		LERDWR(le->sc_r0, LE_RINT|LE_INEA, le->sc_r1->ler1_rdp);
45041480Smckusick 		return;
45141480Smckusick 	}
45241480Smckusick 
45341480Smckusick 	/*
45441480Smckusick 	 * Process all buffers with valid data
45541480Smckusick 	 */
45641480Smckusick 	while ((rmd->rmd1 & LE_OWN) == 0) {
45741480Smckusick 		int len = rmd->rmd3;
45841480Smckusick 
45941480Smckusick 		/* Clear interrupt to avoid race condition */
46041480Smckusick 		LERDWR(le->sc_r0, LE_RINT|LE_INEA, le->sc_r1->ler1_rdp);
46141480Smckusick 
46241480Smckusick 		if (rmd->rmd1 & LE_ERR) {
46341480Smckusick 			le->sc_rmd = bix;
46441480Smckusick 			lererror(unit, "bad packet");
46541480Smckusick 			le->sc_if.if_ierrors++;
46641480Smckusick 		} else if ((rmd->rmd1 & (LE_STP|LE_ENP)) != (LE_STP|LE_ENP)) {
46741480Smckusick 			/*
46841480Smckusick 			 * Find the end of the packet so we can see how long
46941480Smckusick 			 * it was.  We still throw it away.
47041480Smckusick 			 */
47141480Smckusick 			do {
47241480Smckusick 				LERDWR(le->sc_r0, LE_RINT|LE_INEA,
47341480Smckusick 				       le->sc_r1->ler1_rdp);
47441480Smckusick 				rmd->rmd3 = 0;
47541480Smckusick 				rmd->rmd1 = LE_OWN;
47641480Smckusick 				LENEXTRMP;
47741480Smckusick 			} while (!(rmd->rmd1 & (LE_OWN|LE_ERR|LE_STP|LE_ENP)));
47841480Smckusick 			le->sc_rmd = bix;
47941480Smckusick 			lererror(unit, "chained buffer");
48041480Smckusick 			le->sc_rxlen++;
48141480Smckusick 			/*
48241480Smckusick 			 * If search terminated without successful completion
48341480Smckusick 			 * we reset the hardware (conservative).
48441480Smckusick 			 */
48541480Smckusick 			if ((rmd->rmd1 & (LE_OWN|LE_ERR|LE_STP|LE_ENP)) !=
48641480Smckusick 			    LE_ENP) {
48741480Smckusick 				lereset(unit);
48841480Smckusick 				return;
48941480Smckusick 			}
49041480Smckusick 		} else
49141480Smckusick 			leread(unit, le->sc_r2->ler2_rbuf[bix], len);
49241480Smckusick 		rmd->rmd3 = 0;
49341480Smckusick 		rmd->rmd1 = LE_OWN;
49441480Smckusick 		LENEXTRMP;
49541480Smckusick 	}
49641480Smckusick 	le->sc_rmd = bix;
49741480Smckusick }
49841480Smckusick 
49941480Smckusick leread(unit, buf, len)
50041480Smckusick 	int unit;
50141480Smckusick 	char *buf;
50241480Smckusick 	int len;
50341480Smckusick {
50441480Smckusick 	register struct le_softc *le = &le_softc[unit];
50541480Smckusick 	register struct ether_header *et;
50641480Smckusick     	struct mbuf *m;
50741480Smckusick 	int off, resid;
50841480Smckusick 
50941480Smckusick 	le->sc_if.if_ipackets++;
51041480Smckusick 	et = (struct ether_header *)buf;
51141480Smckusick 	et->ether_type = ntohs((u_short)et->ether_type);
51241480Smckusick 	/* adjust input length to account for header and CRC */
51341480Smckusick 	len = len - sizeof(struct ether_header) - 4;
51441480Smckusick 
51541480Smckusick #ifdef RMP
51641480Smckusick 	/*  (XXX)
51741480Smckusick 	 *
51841480Smckusick 	 *  If Ethernet Type field is < MaxPacketSize, we probably have
51941480Smckusick 	 *  a IEEE802 packet here.  Make sure that the size is at least
52041480Smckusick 	 *  that of the HP LLC.  Also do sanity checks on length of LLC
52141480Smckusick 	 *  (old Ethernet Type field) and packet length.
52241480Smckusick 	 *
52341480Smckusick 	 *  Provided the above checks succeed, change `len' to reflect
52441480Smckusick 	 *  the length of the LLC (i.e. et->ether_type) and change the
52541480Smckusick 	 *  type field to ETHERTYPE_IEEE so we can switch() on it later.
52641480Smckusick 	 *  Yes, this is a hack and will eventually be done "right".
52741480Smckusick 	 */
52841480Smckusick 	if (et->ether_type <= IEEE802LEN_MAX && len >= sizeof(struct hp_llc) &&
52941480Smckusick 	    len >= et->ether_type && len >= IEEE802LEN_MIN) {
53041480Smckusick 		len = et->ether_type;
53141480Smckusick 		et->ether_type = ETHERTYPE_IEEE;	/* hack! */
53241480Smckusick 	}
53341480Smckusick #endif
53441480Smckusick 
53541480Smckusick #define	ledataaddr(et, off, type)	((type)(((caddr_t)((et)+1)+(off))))
53641480Smckusick 	if (et->ether_type >= ETHERTYPE_TRAIL &&
53741480Smckusick 	    et->ether_type < ETHERTYPE_TRAIL+ETHERTYPE_NTRAILER) {
53841480Smckusick 		off = (et->ether_type - ETHERTYPE_TRAIL) * 512;
53941480Smckusick 		if (off >= ETHERMTU)
54041480Smckusick 			return;		/* sanity */
54141480Smckusick 		et->ether_type = ntohs(*ledataaddr(et, off, u_short *));
54241480Smckusick 		resid = ntohs(*(ledataaddr(et, off+2, u_short *)));
54341480Smckusick 		if (off + resid > len)
54441480Smckusick 			return;		/* sanity */
54541480Smckusick 		len = off + resid;
54641480Smckusick 	} else
54741480Smckusick 		off = 0;
54841480Smckusick 
54941480Smckusick 	if (len <= 0) {
55041480Smckusick 		if (ledebug)
55141480Smckusick 			log(LOG_WARNING,
55241480Smckusick 			    "le%d: ierror(runt packet): from %s: len=%d\n",
55341480Smckusick 			    unit, ether_sprintf(et->ether_shost), len);
55441480Smckusick 		le->sc_runt++;
55541480Smckusick 		le->sc_if.if_ierrors++;
55641480Smckusick 		return;
55741480Smckusick 	}
558*47567Smccanne #if NBPFILTER > 0
559*47567Smccanne 	/*
560*47567Smccanne 	 * Check if there's a bpf filter listening on this interface.
561*47567Smccanne 	 * If so, hand off the raw packet to bpf, which must deal with
562*47567Smccanne 	 * trailers in its own way.
563*47567Smccanne 	 */
564*47567Smccanne 	if (le->sc_bpf) {
565*47567Smccanne 		bpf_tap(le->sc_bpf, buf, len + sizeof(struct ether_header));
56641480Smckusick 
567*47567Smccanne 		/*
568*47567Smccanne 		 * Note that the interface cannot be in promiscuous mode if
569*47567Smccanne 		 * there are no bpf listeners.  And if we are in promiscuous
570*47567Smccanne 		 * mode, we have to check if this packet is really ours.
571*47567Smccanne 		 *
572*47567Smccanne 		 * XXX This test does not support multicasts.
573*47567Smccanne 		 */
574*47567Smccanne 		if ((le->sc_if.if_flags & IFF_PROMISC)
575*47567Smccanne 		    && bcmp(et->ether_dhost, le->sc_addr,
576*47567Smccanne 			    sizeof(et->ether_dhost)) != 0
577*47567Smccanne 		    && bcmp(et->ether_dhost, etherbroadcastaddr,
578*47567Smccanne 			    sizeof(et->ether_dhost)) != 0)
579*47567Smccanne 			return;
580*47567Smccanne 	}
581*47567Smccanne #endif
58241480Smckusick 	/*
58341480Smckusick 	 * Pull packet off interface.  Off is nonzero if packet
58441480Smckusick 	 * has trailing header; leget will then force this header
58541480Smckusick 	 * information to be at the front, but we still have to drop
58641480Smckusick 	 * the type and length which are at the front of any trailer data.
58741480Smckusick 	 */
58841480Smckusick 	m = leget(buf, len, off, &le->sc_if);
58941480Smckusick 	if (m == 0)
59041480Smckusick 		return;
59141480Smckusick #ifdef RMP
59241480Smckusick 	/*
59341480Smckusick 	 * (XXX)
59441480Smckusick 	 * This needs to be integrated with the ISO stuff in ether_input()
59541480Smckusick 	 */
59641480Smckusick 	if (et->ether_type == ETHERTYPE_IEEE) {
59741480Smckusick 		/*
59841480Smckusick 		 *  Snag the Logical Link Control header (IEEE 802.2).
59941480Smckusick 		 */
60041480Smckusick 		struct hp_llc *llc = &(mtod(m, struct rmp_packet *)->hp_llc);
60141480Smckusick 
60241480Smckusick 		/*
60341480Smckusick 		 *  If the DSAP (and HP's extended DXSAP) indicate this
60441480Smckusick 		 *  is an RMP packet, hand it to the raw input routine.
60541480Smckusick 		 */
60641480Smckusick 		if (llc->dsap == IEEE_DSAP_HP && llc->dxsap == HPEXT_DXSAP) {
60741480Smckusick 			static struct sockproto rmp_sp = {AF_RMP,RMPPROTO_BOOT};
60841480Smckusick 			static struct sockaddr rmp_src = {AF_RMP};
60941480Smckusick 			static struct sockaddr rmp_dst = {AF_RMP};
61041480Smckusick 
61141480Smckusick 			bcopy(et->ether_shost, rmp_src.sa_data,
61241480Smckusick 			      sizeof(et->ether_shost));
61341480Smckusick 			bcopy(et->ether_dhost, rmp_dst.sa_data,
61441480Smckusick 			      sizeof(et->ether_dhost));
61541480Smckusick 
61641480Smckusick 			raw_input(m, &rmp_sp, &rmp_src, &rmp_dst);
61741480Smckusick 			return;
61841480Smckusick 		}
61941480Smckusick 	}
62041480Smckusick #endif
62141480Smckusick 	ether_input(&le->sc_if, et, m);
62241480Smckusick }
62341480Smckusick 
62441480Smckusick /*
62541480Smckusick  * Routine to copy from mbuf chain to transmit
62641480Smckusick  * buffer in board local memory.
62741480Smckusick  */
62841480Smckusick leput(lebuf, m)
62941480Smckusick 	register char *lebuf;
63041480Smckusick 	register struct mbuf *m;
63141480Smckusick {
63241480Smckusick 	register struct mbuf *mp;
63341480Smckusick 	register int len, tlen = 0;
63441480Smckusick 
63541480Smckusick 	for (mp = m; mp; mp = mp->m_next) {
63641480Smckusick 		len = mp->m_len;
63741480Smckusick 		if (len == 0)
63841480Smckusick 			continue;
63941480Smckusick 		tlen += len;
64041480Smckusick 		bcopy(mtod(mp, char *), lebuf, len);
64141480Smckusick 		lebuf += len;
64241480Smckusick 	}
64341480Smckusick 	m_freem(m);
64441480Smckusick 	if (tlen < LEMINSIZE) {
64541480Smckusick 		bzero(lebuf, LEMINSIZE - tlen);
64641480Smckusick 		tlen = LEMINSIZE;
64741480Smckusick 	}
64841480Smckusick 	return(tlen);
64941480Smckusick }
65041480Smckusick 
65141480Smckusick /*
65241480Smckusick  * Routine to copy from board local memory into mbufs.
65341480Smckusick  */
65441480Smckusick struct mbuf *
65541480Smckusick leget(lebuf, totlen, off0, ifp)
65641480Smckusick 	char *lebuf;
65741480Smckusick 	int totlen, off0;
65841480Smckusick 	struct ifnet *ifp;
65941480Smckusick {
66041480Smckusick 	register struct mbuf *m;
66141480Smckusick 	struct mbuf *top = 0, **mp = &top;
66241480Smckusick 	register int off = off0, len;
66341480Smckusick 	register char *cp;
66441480Smckusick 	char *epkt;
66541480Smckusick 
66641480Smckusick 	lebuf += sizeof (struct ether_header);
66741480Smckusick 	cp = lebuf;
66841480Smckusick 	epkt = cp + totlen;
66941480Smckusick 	if (off) {
67041480Smckusick 		cp += off + 2 * sizeof(u_short);
67141480Smckusick 		totlen -= 2 * sizeof(u_short);
67241480Smckusick 	}
67341480Smckusick 
67441480Smckusick 	MGETHDR(m, M_DONTWAIT, MT_DATA);
67541480Smckusick 	if (m == 0)
67641480Smckusick 		return (0);
67741480Smckusick 	m->m_pkthdr.rcvif = ifp;
67841480Smckusick 	m->m_pkthdr.len = totlen;
67941480Smckusick 	m->m_len = MHLEN;
68041480Smckusick 
68141480Smckusick 	while (totlen > 0) {
68241480Smckusick 		if (top) {
68341480Smckusick 			MGET(m, M_DONTWAIT, MT_DATA);
68441480Smckusick 			if (m == 0) {
68541480Smckusick 				m_freem(top);
68641480Smckusick 				return (0);
68741480Smckusick 			}
68841480Smckusick 			m->m_len = MLEN;
68941480Smckusick 		}
69041480Smckusick 		len = min(totlen, epkt - cp);
69141480Smckusick 		if (len >= MINCLSIZE) {
69241480Smckusick 			MCLGET(m, M_DONTWAIT);
69341480Smckusick 			if (m->m_flags & M_EXT)
69441480Smckusick 				m->m_len = len = min(len, MCLBYTES);
69541480Smckusick 			else
69641480Smckusick 				len = m->m_len;
69741480Smckusick 		} else {
69841480Smckusick 			/*
69941480Smckusick 			 * Place initial small packet/header at end of mbuf.
70041480Smckusick 			 */
70141480Smckusick 			if (len < m->m_len) {
70241480Smckusick 				if (top == 0 && len + max_linkhdr <= m->m_len)
70341480Smckusick 					m->m_data += max_linkhdr;
70441480Smckusick 				m->m_len = len;
70541480Smckusick 			} else
70641480Smckusick 				len = m->m_len;
70741480Smckusick 		}
70841480Smckusick 		bcopy(cp, mtod(m, caddr_t), (unsigned)len);
70941480Smckusick 		cp += len;
71041480Smckusick 		*mp = m;
71141480Smckusick 		mp = &m->m_next;
71241480Smckusick 		totlen -= len;
71341480Smckusick 		if (cp == epkt)
71441480Smckusick 			cp = lebuf;
71541480Smckusick 	}
71641480Smckusick 	return (top);
71741480Smckusick }
71841480Smckusick 
71941480Smckusick /*
72041480Smckusick  * Process an ioctl request.
72141480Smckusick  */
72241480Smckusick leioctl(ifp, cmd, data)
72341480Smckusick 	register struct ifnet *ifp;
72441480Smckusick 	int cmd;
72541480Smckusick 	caddr_t data;
72641480Smckusick {
72741480Smckusick 	register struct ifaddr *ifa = (struct ifaddr *)data;
72841480Smckusick 	struct le_softc *le = &le_softc[ifp->if_unit];
72941480Smckusick 	struct lereg1 *ler1 = le->sc_r1;
73041480Smckusick 	int s = splimp(), error = 0;
73141480Smckusick 
73241480Smckusick 	switch (cmd) {
73341480Smckusick 
73441480Smckusick 	case SIOCSIFADDR:
73541480Smckusick 		ifp->if_flags |= IFF_UP;
73641480Smckusick 		switch (ifa->ifa_addr->sa_family) {
73741480Smckusick #ifdef INET
73841480Smckusick 		case AF_INET:
73941480Smckusick 			leinit(ifp->if_unit);	/* before arpwhohas */
74041480Smckusick 			((struct arpcom *)ifp)->ac_ipaddr =
74141480Smckusick 				IA_SIN(ifa)->sin_addr;
74241480Smckusick 			arpwhohas((struct arpcom *)ifp, &IA_SIN(ifa)->sin_addr);
74341480Smckusick 			break;
74441480Smckusick #endif
74541480Smckusick #ifdef NS
74641480Smckusick 		case AF_NS:
74741480Smckusick 		    {
74841480Smckusick 			register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
74941480Smckusick 
75041480Smckusick 			if (ns_nullhost(*ina))
75141480Smckusick 				ina->x_host = *(union ns_host *)(le->sc_addr);
75241480Smckusick 			else {
75341480Smckusick 				/*
75441480Smckusick 				 * The manual says we can't change the address
75541480Smckusick 				 * while the receiver is armed,
75641480Smckusick 				 * so reset everything
75741480Smckusick 				 */
75841480Smckusick 				ifp->if_flags &= ~IFF_RUNNING;
75941480Smckusick 				bcopy((caddr_t)ina->x_host.c_host,
76041480Smckusick 				    (caddr_t)le->sc_addr, sizeof(le->sc_addr));
76141480Smckusick 			}
76241480Smckusick 			leinit(ifp->if_unit); /* does le_setaddr() */
76341480Smckusick 			break;
76441480Smckusick 		    }
76541480Smckusick #endif
76641480Smckusick 		default:
76741480Smckusick 			leinit(ifp->if_unit);
76841480Smckusick 			break;
76941480Smckusick 		}
77041480Smckusick 		break;
77141480Smckusick 
77241480Smckusick 	case SIOCSIFFLAGS:
77341480Smckusick 		if ((ifp->if_flags & IFF_UP) == 0 &&
77441480Smckusick 		    ifp->if_flags & IFF_RUNNING) {
77541480Smckusick 			LERDWR(le->sc_r0, LE_STOP, ler1->ler1_rdp);
77641480Smckusick 			ifp->if_flags &= ~IFF_RUNNING;
77741480Smckusick 		} else if (ifp->if_flags & IFF_UP &&
77841480Smckusick 		    (ifp->if_flags & IFF_RUNNING) == 0)
77941480Smckusick 			leinit(ifp->if_unit);
78041480Smckusick 		break;
78141480Smckusick 
78241480Smckusick 	default:
78341480Smckusick 		error = EINVAL;
78441480Smckusick 	}
78541480Smckusick 	splx(s);
78641480Smckusick 	return (error);
78741480Smckusick }
78841480Smckusick 
78941480Smckusick leerror(unit, stat)
79041480Smckusick 	int unit;
79141480Smckusick 	int stat;
79241480Smckusick {
79341480Smckusick 	if (!ledebug)
79441480Smckusick 		return;
79541480Smckusick 
79641480Smckusick 	/*
79741480Smckusick 	 * Not all transceivers implement heartbeat
79841480Smckusick 	 * so we only log CERR once.
79941480Smckusick 	 */
80041480Smckusick 	if ((stat & LE_CERR) && le_softc[unit].sc_cerr)
80141480Smckusick 		return;
80241480Smckusick 	log(LOG_WARNING,
80341480Smckusick 	    "le%d: error: stat=%b\n", unit,
80441480Smckusick 	    stat,
80541480Smckusick 	    "\20\20ERR\17BABL\16CERR\15MISS\14MERR\13RINT\12TINT\11IDON\10INTR\07INEA\06RXON\05TXON\04TDMD\03STOP\02STRT\01INIT");
80641480Smckusick }
80741480Smckusick 
80841480Smckusick lererror(unit, msg)
80941480Smckusick 	int unit;
81041480Smckusick 	char *msg;
81141480Smckusick {
81241480Smckusick 	register struct le_softc *le = &le_softc[unit];
81341480Smckusick 	register struct lermd *rmd;
81441480Smckusick 	int len;
81541480Smckusick 
81641480Smckusick 	if (!ledebug)
81741480Smckusick 		return;
81841480Smckusick 
81941480Smckusick 	rmd = &le->sc_r2->ler2_rmd[le->sc_rmd];
82041480Smckusick 	len = rmd->rmd3;
82141480Smckusick 	log(LOG_WARNING,
82241480Smckusick 	    "le%d: ierror(%s): from %s: buf=%d, len=%d, rmd1=%b\n",
82341480Smckusick 	    unit, msg,
82441480Smckusick 	    len > 11 ? ether_sprintf(&le->sc_r2->ler2_rbuf[le->sc_rmd][6]) : "unknown",
82541480Smckusick 	    le->sc_rmd, len,
82641480Smckusick 	    rmd->rmd1,
82741480Smckusick 	    "\20\20OWN\17ERR\16FRAM\15OFLO\14CRC\13RBUF\12STP\11ENP");
82841480Smckusick }
82941480Smckusick 
83041480Smckusick lexerror(unit)
83141480Smckusick 	int unit;
83241480Smckusick {
83341480Smckusick 	register struct le_softc *le = &le_softc[unit];
83441480Smckusick 	register struct letmd *tmd;
83541480Smckusick 	int len;
83641480Smckusick 
83741480Smckusick 	if (!ledebug)
83841480Smckusick 		return;
83941480Smckusick 
84041480Smckusick 	tmd = le->sc_r2->ler2_tmd;
84141480Smckusick 	len = -tmd->tmd2;
84241480Smckusick 	log(LOG_WARNING,
84341480Smckusick 	    "le%d: oerror: to %s: buf=%d, len=%d, tmd1=%b, tmd3=%b\n",
84441480Smckusick 	    unit,
84541480Smckusick 	    len > 5 ? ether_sprintf(&le->sc_r2->ler2_tbuf[0][0]) : "unknown",
84641480Smckusick 	    0, len,
84741480Smckusick 	    tmd->tmd1,
84841480Smckusick 	    "\20\20OWN\17ERR\16RES\15MORE\14ONE\13DEF\12STP\11ENP",
84941480Smckusick 	    tmd->tmd3,
85041480Smckusick 	    "\20\20BUFF\17UFLO\16RES\15LCOL\14LCAR\13RTRY");
85141480Smckusick }
85241480Smckusick #endif
853