xref: /csrg-svn/sys/hp300/dev/if_le.c (revision 49407)
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*49407Smccanne  *	@(#)if_le.c	7.6 (Berkeley) 05/08/91
841480Smckusick  */
941480Smckusick 
1041480Smckusick #include "le.h"
1141480Smckusick #if NLE > 0
1241480Smckusick 
1347567Smccanne #include "bpfilter.h"
1447567Smccanne 
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 
6147567Smccanne #if NBPFILTER > 0
6247567Smccanne #include "../net/bpf.h"
6347567Smccanne #include "../net/bpfdesc.h"
6447567Smccanne #endif
6547567Smccanne 
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;
10948771Smccanne 	short	sc_iflags;
11047567Smccanne #if NBPFILTER > 0
11147567Smccanne 	caddr_t sc_bpf;
11247567Smccanne #endif
11341480Smckusick } le_softc[NLE];
11441480Smckusick 
11541480Smckusick /* access LANCE registers */
11641480Smckusick #define	LERDWR(cntl, src, dst) \
11741480Smckusick 	do { \
11841480Smckusick 		(dst) = (src); \
11941480Smckusick 	} while (((cntl)->ler0_status & LE_ACK) == 0);
12041480Smckusick 
12141480Smckusick /*
12241480Smckusick  * Interface exists: make available by filling in network interface
12341480Smckusick  * record.  System will initialize the interface when it is ready
12441480Smckusick  * to accept packets.
12541480Smckusick  */
12641480Smckusick leattach(hd)
12741480Smckusick 	struct hp_device *hd;
12841480Smckusick {
12941480Smckusick 	register struct lereg0 *ler0;
13041480Smckusick 	register struct lereg2 *ler2;
13141480Smckusick 	struct lereg2 *lemem = 0;
13241480Smckusick 	struct le_softc *le = &le_softc[hd->hp_unit];
13341480Smckusick 	struct ifnet *ifp = &le->sc_if;
13441480Smckusick 	char *cp;
13541480Smckusick 	int i;
13641480Smckusick 
13741480Smckusick 	ler0 = le->sc_r0 = (struct lereg0 *)(lestd[0] + (int)hd->hp_addr);
13841480Smckusick 	le->sc_r1 = (struct lereg1 *)(lestd[1] + (int)hd->hp_addr);
13941480Smckusick 	ler2 = le->sc_r2 = (struct lereg2 *)(lestd[2] + (int)hd->hp_addr);
14041480Smckusick 	if (ler0->ler0_id != LEID)
14141480Smckusick 		return(0);
14241480Smckusick 	le_isr[hd->hp_unit].isr_intr = leintr;
14341480Smckusick 	hd->hp_ipl = le_isr[hd->hp_unit].isr_ipl = LE_IPL(ler0->ler0_status);
14441480Smckusick 	le_isr[hd->hp_unit].isr_arg = hd->hp_unit;
14541480Smckusick 	ler0->ler0_id = 0xFF;
14641480Smckusick 	DELAY(100);
14741480Smckusick 
14841480Smckusick 	/*
14941480Smckusick 	 * Read the ethernet address off the board, one nibble at a time.
15041480Smckusick 	 */
15141480Smckusick 	cp = (char *)(lestd[3] + (int)hd->hp_addr);
15241480Smckusick 	for (i = 0; i < sizeof(le->sc_addr); i++) {
15341480Smckusick 		le->sc_addr[i] = (*++cp & 0xF) << 4;
15441480Smckusick 		cp++;
15541480Smckusick 		le->sc_addr[i] |= *++cp & 0xF;
15641480Smckusick 		cp++;
15741480Smckusick 	}
15841480Smckusick 	printf("le%d: hardware address %s\n", hd->hp_unit,
15941480Smckusick 		ether_sprintf(le->sc_addr));
16041480Smckusick 
16141480Smckusick 	/*
16241480Smckusick 	 * Setup for transmit/receive
16341480Smckusick 	 */
16441480Smckusick 	ler2->ler2_mode = LE_MODE;
16541480Smckusick 	ler2->ler2_padr[0] = le->sc_addr[1];
16641480Smckusick 	ler2->ler2_padr[1] = le->sc_addr[0];
16741480Smckusick 	ler2->ler2_padr[2] = le->sc_addr[3];
16841480Smckusick 	ler2->ler2_padr[3] = le->sc_addr[2];
16941480Smckusick 	ler2->ler2_padr[4] = le->sc_addr[5];
17041480Smckusick 	ler2->ler2_padr[5] = le->sc_addr[4];
17141480Smckusick #ifdef RMP
17241480Smckusick 	/*
17341480Smckusick 	 * Set up logical addr filter to accept multicast 9:0:9:0:0:4
17441480Smckusick 	 * This should be an ioctl() to the driver.  (XXX)
17541480Smckusick 	 */
17641480Smckusick 	ler2->ler2_ladrf0 = 0x00100000;
17741480Smckusick 	ler2->ler2_ladrf1 = 0x0;
17841480Smckusick #else
17941480Smckusick 	ler2->ler2_ladrf0 = 0;
18041480Smckusick 	ler2->ler2_ladrf1 = 0;
18141480Smckusick #endif
18241480Smckusick 	ler2->ler2_rlen = LE_RLEN;
18341480Smckusick 	ler2->ler2_rdra = (int)lemem->ler2_rmd;
18441480Smckusick 	ler2->ler2_tlen = LE_TLEN;
18541480Smckusick 	ler2->ler2_tdra = (int)lemem->ler2_tmd;
18641480Smckusick 	isrlink(&le_isr[hd->hp_unit]);
18741480Smckusick 	ler0->ler0_status = LE_IE;
18841480Smckusick 
18941480Smckusick 	ifp->if_unit = hd->hp_unit;
19041480Smckusick 	ifp->if_name = "le";
19141480Smckusick 	ifp->if_mtu = ETHERMTU;
19241480Smckusick 	ifp->if_init = leinit;
19341480Smckusick 	ifp->if_ioctl = leioctl;
19441480Smckusick 	ifp->if_output = ether_output;
19541480Smckusick 	ifp->if_start = lestart;
19641480Smckusick 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX;
19747567Smccanne #if NBPFILTER > 0
198*49407Smccanne 	bpfattach(&le->sc_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
19947567Smccanne #endif
20041480Smckusick 	if_attach(ifp);
20141480Smckusick 	return (1);
20241480Smckusick }
20341480Smckusick 
20441480Smckusick ledrinit(ler2)
20541480Smckusick 	register struct lereg2 *ler2;
20641480Smckusick {
20741480Smckusick 	register struct lereg2 *lemem = 0;
20841480Smckusick 	register int i;
20941480Smckusick 
21041480Smckusick 	for (i = 0; i < LERBUF; i++) {
21141480Smckusick 		ler2->ler2_rmd[i].rmd0 = (int)lemem->ler2_rbuf[i];
21241480Smckusick 		ler2->ler2_rmd[i].rmd1 = LE_OWN;
21341480Smckusick 		ler2->ler2_rmd[i].rmd2 = -LEMTU;
21441480Smckusick 		ler2->ler2_rmd[i].rmd3 = 0;
21541480Smckusick 	}
21641480Smckusick 	for (i = 0; i < LETBUF; i++) {
21741480Smckusick 		ler2->ler2_tmd[i].tmd0 = (int)lemem->ler2_tbuf[i];
21841480Smckusick 		ler2->ler2_tmd[i].tmd1 = 0;
21941480Smckusick 		ler2->ler2_tmd[i].tmd2 = 0;
22041480Smckusick 		ler2->ler2_tmd[i].tmd3 = 0;
22141480Smckusick 	}
22241480Smckusick }
22341480Smckusick 
22441480Smckusick lereset(unit)
22541480Smckusick 	register int unit;
22641480Smckusick {
22741480Smckusick 	register struct le_softc *le = &le_softc[unit];
22841480Smckusick 	register struct lereg0 *ler0 = le->sc_r0;
22941480Smckusick 	register struct lereg1 *ler1 = le->sc_r1;
23041480Smckusick 	register struct lereg2 *lemem = 0;
23141480Smckusick 	register int timo = 100000;
23241480Smckusick 	register int stat;
23341480Smckusick 
23441480Smckusick #ifdef lint
23541480Smckusick 	stat = unit;
23641480Smckusick #endif
23747567Smccanne #if NBPFILTER > 0
23847567Smccanne 	if (le->sc_if.if_flags & IFF_PROMISC)
23947567Smccanne 		/* set the promiscuous bit */
24047567Smccanne 		le->sc_r2->ler2_mode = LE_MODE|0x8000;
24147567Smccanne 	else
24247567Smccanne 		le->sc_r2->ler2_mode = LE_MODE;
24347567Smccanne #endif
24441480Smckusick 	LERDWR(ler0, LE_CSR0, ler1->ler1_rap);
24541480Smckusick 	LERDWR(ler0, LE_STOP, ler1->ler1_rdp);
24641480Smckusick 	ledrinit(le->sc_r2);
24741480Smckusick 	le->sc_rmd = 0;
24841480Smckusick 	LERDWR(ler0, LE_CSR1, ler1->ler1_rap);
24941480Smckusick 	LERDWR(ler0, (int)&lemem->ler2_mode, ler1->ler1_rdp);
25041480Smckusick 	LERDWR(ler0, LE_CSR2, ler1->ler1_rap);
25141480Smckusick 	LERDWR(ler0, 0, ler1->ler1_rdp);
25241480Smckusick 	LERDWR(ler0, LE_CSR0, ler1->ler1_rap);
25341480Smckusick 	LERDWR(ler0, LE_INIT, ler1->ler1_rdp);
25441480Smckusick 	do {
25541480Smckusick 		if (--timo == 0) {
25641480Smckusick 			printf("le%d: init timeout, stat = 0x%x\n",
25741480Smckusick 			       unit, stat);
25841480Smckusick 			break;
25941480Smckusick 		}
26041480Smckusick 		LERDWR(ler0, ler1->ler1_rdp, stat);
26141480Smckusick 	} while ((stat & LE_IDON) == 0);
26241480Smckusick 	LERDWR(ler0, LE_STOP, ler1->ler1_rdp);
26341480Smckusick 	LERDWR(ler0, LE_CSR3, ler1->ler1_rap);
26441480Smckusick 	LERDWR(ler0, LE_BSWP, ler1->ler1_rdp);
26541480Smckusick 	LERDWR(ler0, LE_CSR0, ler1->ler1_rap);
26641480Smckusick 	LERDWR(ler0, LE_STRT | LE_INEA, ler1->ler1_rdp);
26741480Smckusick 	le->sc_if.if_flags &= ~IFF_OACTIVE;
26841480Smckusick }
26941480Smckusick 
27041480Smckusick /*
27141480Smckusick  * Initialization of interface
27241480Smckusick  */
27341480Smckusick leinit(unit)
27441480Smckusick 	int unit;
27541480Smckusick {
27641480Smckusick 	struct le_softc *le = &le_softc[unit];
27741480Smckusick 	register struct ifnet *ifp = &le->sc_if;
27841480Smckusick 	int s;
27941480Smckusick 
28041480Smckusick 	/* not yet, if address still unknown */
28141480Smckusick 	if (ifp->if_addrlist == (struct ifaddr *)0)
28241480Smckusick 		return;
28341480Smckusick 	if ((ifp->if_flags & IFF_RUNNING) == 0) {
28441480Smckusick 		s = splimp();
28541480Smckusick 		ifp->if_flags |= IFF_RUNNING;
28641480Smckusick 		lereset(unit);
28741480Smckusick 	        (void) lestart(ifp);
28841480Smckusick 		splx(s);
28941480Smckusick 	}
29041480Smckusick }
29141480Smckusick 
29241480Smckusick /*
29341480Smckusick  * Start output on interface.  Get another datagram to send
29441480Smckusick  * off of the interface queue, and copy it to the interface
29541480Smckusick  * before starting the output.
29641480Smckusick  */
29741480Smckusick lestart(ifp)
29841480Smckusick 	struct ifnet *ifp;
29941480Smckusick {
30041480Smckusick 	register struct le_softc *le = &le_softc[ifp->if_unit];
30141480Smckusick 	register struct letmd *tmd;
30241480Smckusick 	register struct mbuf *m;
30341480Smckusick 	int len;
30441480Smckusick 
30541480Smckusick 	if ((le->sc_if.if_flags & IFF_RUNNING) == 0)
30641480Smckusick 		return (0);
30741480Smckusick 	IF_DEQUEUE(&le->sc_if.if_snd, m);
30841480Smckusick 	if (m == 0)
30941480Smckusick 		return (0);
31048986Smccanne 	len = leput(le->sc_r2->ler2_tbuf[0], m);
31147567Smccanne #if NBPFILTER > 0
31247567Smccanne 	/*
31347567Smccanne 	 * If bpf is listening on this interface, let it
31447567Smccanne 	 * see the packet before we commit it to the wire.
31547567Smccanne 	 */
31647567Smccanne 	if (le->sc_bpf)
31747567Smccanne                 bpf_tap(le->sc_bpf, le->sc_r2->ler2_tbuf[0], len);
31847567Smccanne #endif
31941480Smckusick 	tmd = le->sc_r2->ler2_tmd;
32041480Smckusick 	tmd->tmd3 = 0;
32141480Smckusick 	tmd->tmd2 = -len;
32241480Smckusick 	tmd->tmd1 = LE_OWN | LE_STP | LE_ENP;
32341480Smckusick 	le->sc_if.if_flags |= IFF_OACTIVE;
32441480Smckusick 	return (0);
32541480Smckusick }
32641480Smckusick 
32741480Smckusick leintr(unit)
32841480Smckusick 	register int unit;
32941480Smckusick {
33041480Smckusick 	register struct le_softc *le = &le_softc[unit];
33141480Smckusick 	register struct lereg0 *ler0 = le->sc_r0;
33241480Smckusick 	register struct lereg1 *ler1;
33341480Smckusick 	register int stat;
33441480Smckusick 
33541480Smckusick 	if ((ler0->ler0_status & LE_IR) == 0)
33641480Smckusick 		return(0);
33741480Smckusick 	if (ler0->ler0_status & LE_JAB) {
33841480Smckusick 		le->sc_jab++;
33941480Smckusick 		lereset(unit);
34041480Smckusick 		return(1);
34141480Smckusick 	}
34241480Smckusick 	ler1 = le->sc_r1;
34341480Smckusick 	LERDWR(ler0, ler1->ler1_rdp, stat);
34441480Smckusick 	if (stat & LE_SERR) {
34541480Smckusick 		leerror(unit, stat);
34641480Smckusick 		if (stat & LE_MERR) {
34741480Smckusick 			le->sc_merr++;
34841480Smckusick 			lereset(unit);
34941480Smckusick 			return(1);
35041480Smckusick 		}
35141480Smckusick 		if (stat & LE_BABL)
35241480Smckusick 			le->sc_babl++;
35341480Smckusick 		if (stat & LE_CERR)
35441480Smckusick 			le->sc_cerr++;
35541480Smckusick 		if (stat & LE_MISS)
35641480Smckusick 			le->sc_miss++;
35741480Smckusick 		LERDWR(ler0, LE_BABL|LE_CERR|LE_MISS|LE_INEA, ler1->ler1_rdp);
35841480Smckusick 	}
35941480Smckusick 	if ((stat & LE_RXON) == 0) {
36041480Smckusick 		le->sc_rxoff++;
36141480Smckusick 		lereset(unit);
36241480Smckusick 		return(1);
36341480Smckusick 	}
36441480Smckusick 	if ((stat & LE_TXON) == 0) {
36541480Smckusick 		le->sc_txoff++;
36641480Smckusick 		lereset(unit);
36741480Smckusick 		return(1);
36841480Smckusick 	}
36941480Smckusick 	if (stat & LE_RINT) {
37041480Smckusick 		/* interrupt is cleared in lerint */
37141480Smckusick 		lerint(unit);
37241480Smckusick 	}
37341480Smckusick 	if (stat & LE_TINT) {
37441480Smckusick 		LERDWR(ler0, LE_TINT|LE_INEA, ler1->ler1_rdp);
37541480Smckusick 		lexint(unit);
37641480Smckusick 	}
37741480Smckusick 	return(1);
37841480Smckusick }
37941480Smckusick 
38041480Smckusick /*
38141480Smckusick  * Ethernet interface transmitter interrupt.
38241480Smckusick  * Start another output if more data to send.
38341480Smckusick  */
38441480Smckusick lexint(unit)
38541480Smckusick 	register int unit;
38641480Smckusick {
38741480Smckusick 	register struct le_softc *le = &le_softc[unit];
38841480Smckusick 	register struct letmd *tmd = le->sc_r2->ler2_tmd;
38941480Smckusick 
39041480Smckusick 	if ((le->sc_if.if_flags & IFF_OACTIVE) == 0) {
39141480Smckusick 		le->sc_xint++;
39241480Smckusick 		return;
39341480Smckusick 	}
39441480Smckusick 	if (tmd->tmd1 & LE_OWN) {
39541480Smckusick 		le->sc_xown++;
39641480Smckusick 		return;
39741480Smckusick 	}
39841480Smckusick 	if (tmd->tmd1 & LE_ERR) {
39941480Smckusick err:
40041480Smckusick 		lexerror(unit);
40141480Smckusick 		le->sc_if.if_oerrors++;
40241480Smckusick 		if (tmd->tmd3 & (LE_TBUFF|LE_UFLO)) {
40341480Smckusick 			le->sc_uflo++;
40441480Smckusick 			lereset(unit);
40541480Smckusick 		}
40641480Smckusick 		else if (tmd->tmd3 & LE_LCOL)
40741480Smckusick 			le->sc_if.if_collisions++;
40841480Smckusick 		else if (tmd->tmd3 & LE_RTRY)
40941480Smckusick 			le->sc_if.if_collisions += 16;
41041480Smckusick 	}
41141480Smckusick 	else if (tmd->tmd3 & LE_TBUFF)
41241480Smckusick 		/* XXX documentation says BUFF not included in ERR */
41341480Smckusick 		goto err;
41441480Smckusick 	else if (tmd->tmd1 & LE_ONE)
41541480Smckusick 		le->sc_if.if_collisions++;
41641480Smckusick 	else if (tmd->tmd1 & LE_MORE)
41741480Smckusick 		/* what is the real number? */
41841480Smckusick 		le->sc_if.if_collisions += 2;
41941480Smckusick 	else
42041480Smckusick 		le->sc_if.if_opackets++;
42141480Smckusick 	le->sc_if.if_flags &= ~IFF_OACTIVE;
42241480Smckusick 	(void) lestart(&le->sc_if);
42341480Smckusick }
42441480Smckusick 
42541480Smckusick #define	LENEXTRMP \
42641480Smckusick 	if (++bix == LERBUF) bix = 0, rmd = le->sc_r2->ler2_rmd; else ++rmd
42741480Smckusick 
42841480Smckusick /*
42941480Smckusick  * Ethernet interface receiver interrupt.
43041480Smckusick  * If input error just drop packet.
43141480Smckusick  * Decapsulate packet based on type and pass to type specific
43241480Smckusick  * higher-level input routine.
43341480Smckusick  */
43441480Smckusick lerint(unit)
43541480Smckusick 	int unit;
43641480Smckusick {
43741480Smckusick 	register struct le_softc *le = &le_softc[unit];
43841480Smckusick 	register int bix = le->sc_rmd;
43941480Smckusick 	register struct lermd *rmd = &le->sc_r2->ler2_rmd[bix];
44041480Smckusick 
44141480Smckusick 	/*
44241480Smckusick 	 * Out of sync with hardware, should never happen?
44341480Smckusick 	 */
44441480Smckusick 	if (rmd->rmd1 & LE_OWN) {
44541480Smckusick 		LERDWR(le->sc_r0, LE_RINT|LE_INEA, le->sc_r1->ler1_rdp);
44641480Smckusick 		return;
44741480Smckusick 	}
44841480Smckusick 
44941480Smckusick 	/*
45041480Smckusick 	 * Process all buffers with valid data
45141480Smckusick 	 */
45241480Smckusick 	while ((rmd->rmd1 & LE_OWN) == 0) {
45341480Smckusick 		int len = rmd->rmd3;
45441480Smckusick 
45541480Smckusick 		/* Clear interrupt to avoid race condition */
45641480Smckusick 		LERDWR(le->sc_r0, LE_RINT|LE_INEA, le->sc_r1->ler1_rdp);
45741480Smckusick 
45841480Smckusick 		if (rmd->rmd1 & LE_ERR) {
45941480Smckusick 			le->sc_rmd = bix;
46041480Smckusick 			lererror(unit, "bad packet");
46141480Smckusick 			le->sc_if.if_ierrors++;
46241480Smckusick 		} else if ((rmd->rmd1 & (LE_STP|LE_ENP)) != (LE_STP|LE_ENP)) {
46341480Smckusick 			/*
46441480Smckusick 			 * Find the end of the packet so we can see how long
46541480Smckusick 			 * it was.  We still throw it away.
46641480Smckusick 			 */
46741480Smckusick 			do {
46841480Smckusick 				LERDWR(le->sc_r0, LE_RINT|LE_INEA,
46941480Smckusick 				       le->sc_r1->ler1_rdp);
47041480Smckusick 				rmd->rmd3 = 0;
47141480Smckusick 				rmd->rmd1 = LE_OWN;
47241480Smckusick 				LENEXTRMP;
47341480Smckusick 			} while (!(rmd->rmd1 & (LE_OWN|LE_ERR|LE_STP|LE_ENP)));
47441480Smckusick 			le->sc_rmd = bix;
47541480Smckusick 			lererror(unit, "chained buffer");
47641480Smckusick 			le->sc_rxlen++;
47741480Smckusick 			/*
47841480Smckusick 			 * If search terminated without successful completion
47941480Smckusick 			 * we reset the hardware (conservative).
48041480Smckusick 			 */
48141480Smckusick 			if ((rmd->rmd1 & (LE_OWN|LE_ERR|LE_STP|LE_ENP)) !=
48241480Smckusick 			    LE_ENP) {
48341480Smckusick 				lereset(unit);
48441480Smckusick 				return;
48541480Smckusick 			}
48641480Smckusick 		} else
48741480Smckusick 			leread(unit, le->sc_r2->ler2_rbuf[bix], len);
48841480Smckusick 		rmd->rmd3 = 0;
48941480Smckusick 		rmd->rmd1 = LE_OWN;
49041480Smckusick 		LENEXTRMP;
49141480Smckusick 	}
49241480Smckusick 	le->sc_rmd = bix;
49341480Smckusick }
49441480Smckusick 
49541480Smckusick leread(unit, buf, len)
49641480Smckusick 	int unit;
49741480Smckusick 	char *buf;
49841480Smckusick 	int len;
49941480Smckusick {
50041480Smckusick 	register struct le_softc *le = &le_softc[unit];
50141480Smckusick 	register struct ether_header *et;
50241480Smckusick     	struct mbuf *m;
50341480Smckusick 	int off, resid;
50441480Smckusick 
50541480Smckusick 	le->sc_if.if_ipackets++;
50641480Smckusick 	et = (struct ether_header *)buf;
50741480Smckusick 	et->ether_type = ntohs((u_short)et->ether_type);
50841480Smckusick 	/* adjust input length to account for header and CRC */
50941480Smckusick 	len = len - sizeof(struct ether_header) - 4;
51041480Smckusick 
51141480Smckusick #ifdef RMP
51241480Smckusick 	/*  (XXX)
51341480Smckusick 	 *
51441480Smckusick 	 *  If Ethernet Type field is < MaxPacketSize, we probably have
51541480Smckusick 	 *  a IEEE802 packet here.  Make sure that the size is at least
51641480Smckusick 	 *  that of the HP LLC.  Also do sanity checks on length of LLC
51741480Smckusick 	 *  (old Ethernet Type field) and packet length.
51841480Smckusick 	 *
51941480Smckusick 	 *  Provided the above checks succeed, change `len' to reflect
52041480Smckusick 	 *  the length of the LLC (i.e. et->ether_type) and change the
52141480Smckusick 	 *  type field to ETHERTYPE_IEEE so we can switch() on it later.
52241480Smckusick 	 *  Yes, this is a hack and will eventually be done "right".
52341480Smckusick 	 */
52441480Smckusick 	if (et->ether_type <= IEEE802LEN_MAX && len >= sizeof(struct hp_llc) &&
52541480Smckusick 	    len >= et->ether_type && len >= IEEE802LEN_MIN) {
52641480Smckusick 		len = et->ether_type;
52741480Smckusick 		et->ether_type = ETHERTYPE_IEEE;	/* hack! */
52841480Smckusick 	}
52941480Smckusick #endif
53041480Smckusick 
53141480Smckusick #define	ledataaddr(et, off, type)	((type)(((caddr_t)((et)+1)+(off))))
53241480Smckusick 	if (et->ether_type >= ETHERTYPE_TRAIL &&
53341480Smckusick 	    et->ether_type < ETHERTYPE_TRAIL+ETHERTYPE_NTRAILER) {
53441480Smckusick 		off = (et->ether_type - ETHERTYPE_TRAIL) * 512;
53541480Smckusick 		if (off >= ETHERMTU)
53641480Smckusick 			return;		/* sanity */
53741480Smckusick 		et->ether_type = ntohs(*ledataaddr(et, off, u_short *));
53841480Smckusick 		resid = ntohs(*(ledataaddr(et, off+2, u_short *)));
53941480Smckusick 		if (off + resid > len)
54041480Smckusick 			return;		/* sanity */
54141480Smckusick 		len = off + resid;
54241480Smckusick 	} else
54341480Smckusick 		off = 0;
54441480Smckusick 
54541480Smckusick 	if (len <= 0) {
54641480Smckusick 		if (ledebug)
54741480Smckusick 			log(LOG_WARNING,
54841480Smckusick 			    "le%d: ierror(runt packet): from %s: len=%d\n",
54941480Smckusick 			    unit, ether_sprintf(et->ether_shost), len);
55041480Smckusick 		le->sc_runt++;
55141480Smckusick 		le->sc_if.if_ierrors++;
55241480Smckusick 		return;
55341480Smckusick 	}
55447567Smccanne #if NBPFILTER > 0
55547567Smccanne 	/*
55647567Smccanne 	 * Check if there's a bpf filter listening on this interface.
55747567Smccanne 	 * If so, hand off the raw packet to bpf, which must deal with
55847567Smccanne 	 * trailers in its own way.
55947567Smccanne 	 */
56047567Smccanne 	if (le->sc_bpf) {
56147567Smccanne 		bpf_tap(le->sc_bpf, buf, len + sizeof(struct ether_header));
56241480Smckusick 
56347567Smccanne 		/*
56447567Smccanne 		 * Note that the interface cannot be in promiscuous mode if
56547567Smccanne 		 * there are no bpf listeners.  And if we are in promiscuous
56647567Smccanne 		 * mode, we have to check if this packet is really ours.
56747567Smccanne 		 *
56847567Smccanne 		 * XXX This test does not support multicasts.
56947567Smccanne 		 */
57047567Smccanne 		if ((le->sc_if.if_flags & IFF_PROMISC)
57147567Smccanne 		    && bcmp(et->ether_dhost, le->sc_addr,
57247567Smccanne 			    sizeof(et->ether_dhost)) != 0
57347567Smccanne 		    && bcmp(et->ether_dhost, etherbroadcastaddr,
57447567Smccanne 			    sizeof(et->ether_dhost)) != 0)
57547567Smccanne 			return;
57647567Smccanne 	}
57747567Smccanne #endif
57841480Smckusick 	/*
57941480Smckusick 	 * Pull packet off interface.  Off is nonzero if packet
58041480Smckusick 	 * has trailing header; leget will then force this header
58141480Smckusick 	 * information to be at the front, but we still have to drop
58241480Smckusick 	 * the type and length which are at the front of any trailer data.
58341480Smckusick 	 */
58441480Smckusick 	m = leget(buf, len, off, &le->sc_if);
58541480Smckusick 	if (m == 0)
58641480Smckusick 		return;
58741480Smckusick #ifdef RMP
58841480Smckusick 	/*
58941480Smckusick 	 * (XXX)
59041480Smckusick 	 * This needs to be integrated with the ISO stuff in ether_input()
59141480Smckusick 	 */
59241480Smckusick 	if (et->ether_type == ETHERTYPE_IEEE) {
59341480Smckusick 		/*
59441480Smckusick 		 *  Snag the Logical Link Control header (IEEE 802.2).
59541480Smckusick 		 */
59641480Smckusick 		struct hp_llc *llc = &(mtod(m, struct rmp_packet *)->hp_llc);
59741480Smckusick 
59841480Smckusick 		/*
59941480Smckusick 		 *  If the DSAP (and HP's extended DXSAP) indicate this
60041480Smckusick 		 *  is an RMP packet, hand it to the raw input routine.
60141480Smckusick 		 */
60241480Smckusick 		if (llc->dsap == IEEE_DSAP_HP && llc->dxsap == HPEXT_DXSAP) {
60341480Smckusick 			static struct sockproto rmp_sp = {AF_RMP,RMPPROTO_BOOT};
60441480Smckusick 			static struct sockaddr rmp_src = {AF_RMP};
60541480Smckusick 			static struct sockaddr rmp_dst = {AF_RMP};
60641480Smckusick 
60741480Smckusick 			bcopy(et->ether_shost, rmp_src.sa_data,
60841480Smckusick 			      sizeof(et->ether_shost));
60941480Smckusick 			bcopy(et->ether_dhost, rmp_dst.sa_data,
61041480Smckusick 			      sizeof(et->ether_dhost));
61141480Smckusick 
61241480Smckusick 			raw_input(m, &rmp_sp, &rmp_src, &rmp_dst);
61341480Smckusick 			return;
61441480Smckusick 		}
61541480Smckusick 	}
61641480Smckusick #endif
61741480Smckusick 	ether_input(&le->sc_if, et, m);
61841480Smckusick }
61941480Smckusick 
62041480Smckusick /*
62141480Smckusick  * Routine to copy from mbuf chain to transmit
62241480Smckusick  * buffer in board local memory.
62341480Smckusick  */
62441480Smckusick leput(lebuf, m)
62541480Smckusick 	register char *lebuf;
62641480Smckusick 	register struct mbuf *m;
62741480Smckusick {
62841480Smckusick 	register struct mbuf *mp;
62941480Smckusick 	register int len, tlen = 0;
63041480Smckusick 
63141480Smckusick 	for (mp = m; mp; mp = mp->m_next) {
63241480Smckusick 		len = mp->m_len;
63341480Smckusick 		if (len == 0)
63441480Smckusick 			continue;
63541480Smckusick 		tlen += len;
63641480Smckusick 		bcopy(mtod(mp, char *), lebuf, len);
63741480Smckusick 		lebuf += len;
63841480Smckusick 	}
63941480Smckusick 	m_freem(m);
64041480Smckusick 	if (tlen < LEMINSIZE) {
64141480Smckusick 		bzero(lebuf, LEMINSIZE - tlen);
64241480Smckusick 		tlen = LEMINSIZE;
64341480Smckusick 	}
64441480Smckusick 	return(tlen);
64541480Smckusick }
64641480Smckusick 
64741480Smckusick /*
64841480Smckusick  * Routine to copy from board local memory into mbufs.
64941480Smckusick  */
65041480Smckusick struct mbuf *
65141480Smckusick leget(lebuf, totlen, off0, ifp)
65241480Smckusick 	char *lebuf;
65341480Smckusick 	int totlen, off0;
65441480Smckusick 	struct ifnet *ifp;
65541480Smckusick {
65641480Smckusick 	register struct mbuf *m;
65741480Smckusick 	struct mbuf *top = 0, **mp = &top;
65841480Smckusick 	register int off = off0, len;
65941480Smckusick 	register char *cp;
66041480Smckusick 	char *epkt;
66141480Smckusick 
66241480Smckusick 	lebuf += sizeof (struct ether_header);
66341480Smckusick 	cp = lebuf;
66441480Smckusick 	epkt = cp + totlen;
66541480Smckusick 	if (off) {
66641480Smckusick 		cp += off + 2 * sizeof(u_short);
66741480Smckusick 		totlen -= 2 * sizeof(u_short);
66841480Smckusick 	}
66941480Smckusick 
67041480Smckusick 	MGETHDR(m, M_DONTWAIT, MT_DATA);
67141480Smckusick 	if (m == 0)
67241480Smckusick 		return (0);
67341480Smckusick 	m->m_pkthdr.rcvif = ifp;
67441480Smckusick 	m->m_pkthdr.len = totlen;
67541480Smckusick 	m->m_len = MHLEN;
67641480Smckusick 
67741480Smckusick 	while (totlen > 0) {
67841480Smckusick 		if (top) {
67941480Smckusick 			MGET(m, M_DONTWAIT, MT_DATA);
68041480Smckusick 			if (m == 0) {
68141480Smckusick 				m_freem(top);
68241480Smckusick 				return (0);
68341480Smckusick 			}
68441480Smckusick 			m->m_len = MLEN;
68541480Smckusick 		}
68641480Smckusick 		len = min(totlen, epkt - cp);
68741480Smckusick 		if (len >= MINCLSIZE) {
68841480Smckusick 			MCLGET(m, M_DONTWAIT);
68941480Smckusick 			if (m->m_flags & M_EXT)
69041480Smckusick 				m->m_len = len = min(len, MCLBYTES);
69141480Smckusick 			else
69241480Smckusick 				len = m->m_len;
69341480Smckusick 		} else {
69441480Smckusick 			/*
69541480Smckusick 			 * Place initial small packet/header at end of mbuf.
69641480Smckusick 			 */
69741480Smckusick 			if (len < m->m_len) {
69841480Smckusick 				if (top == 0 && len + max_linkhdr <= m->m_len)
69941480Smckusick 					m->m_data += max_linkhdr;
70041480Smckusick 				m->m_len = len;
70141480Smckusick 			} else
70241480Smckusick 				len = m->m_len;
70341480Smckusick 		}
70441480Smckusick 		bcopy(cp, mtod(m, caddr_t), (unsigned)len);
70541480Smckusick 		cp += len;
70641480Smckusick 		*mp = m;
70741480Smckusick 		mp = &m->m_next;
70841480Smckusick 		totlen -= len;
70941480Smckusick 		if (cp == epkt)
71041480Smckusick 			cp = lebuf;
71141480Smckusick 	}
71241480Smckusick 	return (top);
71341480Smckusick }
71441480Smckusick 
71541480Smckusick /*
71641480Smckusick  * Process an ioctl request.
71741480Smckusick  */
71841480Smckusick leioctl(ifp, cmd, data)
71941480Smckusick 	register struct ifnet *ifp;
72041480Smckusick 	int cmd;
72141480Smckusick 	caddr_t data;
72241480Smckusick {
72341480Smckusick 	register struct ifaddr *ifa = (struct ifaddr *)data;
72441480Smckusick 	struct le_softc *le = &le_softc[ifp->if_unit];
72541480Smckusick 	struct lereg1 *ler1 = le->sc_r1;
72641480Smckusick 	int s = splimp(), error = 0;
72741480Smckusick 
72841480Smckusick 	switch (cmd) {
72941480Smckusick 
73041480Smckusick 	case SIOCSIFADDR:
73141480Smckusick 		ifp->if_flags |= IFF_UP;
73241480Smckusick 		switch (ifa->ifa_addr->sa_family) {
73341480Smckusick #ifdef INET
73441480Smckusick 		case AF_INET:
73541480Smckusick 			leinit(ifp->if_unit);	/* before arpwhohas */
73641480Smckusick 			((struct arpcom *)ifp)->ac_ipaddr =
73741480Smckusick 				IA_SIN(ifa)->sin_addr;
73841480Smckusick 			arpwhohas((struct arpcom *)ifp, &IA_SIN(ifa)->sin_addr);
73941480Smckusick 			break;
74041480Smckusick #endif
74141480Smckusick #ifdef NS
74241480Smckusick 		case AF_NS:
74341480Smckusick 		    {
74441480Smckusick 			register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
74541480Smckusick 
74641480Smckusick 			if (ns_nullhost(*ina))
74741480Smckusick 				ina->x_host = *(union ns_host *)(le->sc_addr);
74841480Smckusick 			else {
74941480Smckusick 				/*
75041480Smckusick 				 * The manual says we can't change the address
75141480Smckusick 				 * while the receiver is armed,
75241480Smckusick 				 * so reset everything
75341480Smckusick 				 */
75441480Smckusick 				ifp->if_flags &= ~IFF_RUNNING;
75541480Smckusick 				bcopy((caddr_t)ina->x_host.c_host,
75641480Smckusick 				    (caddr_t)le->sc_addr, sizeof(le->sc_addr));
75741480Smckusick 			}
75841480Smckusick 			leinit(ifp->if_unit); /* does le_setaddr() */
75941480Smckusick 			break;
76041480Smckusick 		    }
76141480Smckusick #endif
76241480Smckusick 		default:
76341480Smckusick 			leinit(ifp->if_unit);
76441480Smckusick 			break;
76541480Smckusick 		}
76641480Smckusick 		break;
76741480Smckusick 
76841480Smckusick 	case SIOCSIFFLAGS:
76941480Smckusick 		if ((ifp->if_flags & IFF_UP) == 0 &&
77041480Smckusick 		    ifp->if_flags & IFF_RUNNING) {
77141480Smckusick 			LERDWR(le->sc_r0, LE_STOP, ler1->ler1_rdp);
77241480Smckusick 			ifp->if_flags &= ~IFF_RUNNING;
77341480Smckusick 		} else if (ifp->if_flags & IFF_UP &&
77441480Smckusick 		    (ifp->if_flags & IFF_RUNNING) == 0)
77541480Smckusick 			leinit(ifp->if_unit);
77648771Smccanne 		/*
77748771Smccanne 		 * If the state of the promiscuous bit changes, the interface
77848771Smccanne 		 * must be reset to effect the change.
77948771Smccanne 		 */
78048771Smccanne 		if (((ifp->if_flags ^ le->sc_iflags) & IFF_PROMISC) &&
78148771Smccanne 		    (ifp->if_flags & IFF_RUNNING)) {
78248771Smccanne 			le->sc_iflags = ifp->if_flags;
78348771Smccanne 			lereset(ifp->if_unit);
78448771Smccanne 			lestart(ifp);
78548771Smccanne 		}
78641480Smckusick 		break;
78741480Smckusick 
78841480Smckusick 	default:
78941480Smckusick 		error = EINVAL;
79041480Smckusick 	}
79141480Smckusick 	splx(s);
79241480Smckusick 	return (error);
79341480Smckusick }
79441480Smckusick 
79541480Smckusick leerror(unit, stat)
79641480Smckusick 	int unit;
79741480Smckusick 	int stat;
79841480Smckusick {
79941480Smckusick 	if (!ledebug)
80041480Smckusick 		return;
80141480Smckusick 
80241480Smckusick 	/*
80341480Smckusick 	 * Not all transceivers implement heartbeat
80441480Smckusick 	 * so we only log CERR once.
80541480Smckusick 	 */
80641480Smckusick 	if ((stat & LE_CERR) && le_softc[unit].sc_cerr)
80741480Smckusick 		return;
80841480Smckusick 	log(LOG_WARNING,
80941480Smckusick 	    "le%d: error: stat=%b\n", unit,
81041480Smckusick 	    stat,
81141480Smckusick 	    "\20\20ERR\17BABL\16CERR\15MISS\14MERR\13RINT\12TINT\11IDON\10INTR\07INEA\06RXON\05TXON\04TDMD\03STOP\02STRT\01INIT");
81241480Smckusick }
81341480Smckusick 
81441480Smckusick lererror(unit, msg)
81541480Smckusick 	int unit;
81641480Smckusick 	char *msg;
81741480Smckusick {
81841480Smckusick 	register struct le_softc *le = &le_softc[unit];
81941480Smckusick 	register struct lermd *rmd;
82041480Smckusick 	int len;
82141480Smckusick 
82241480Smckusick 	if (!ledebug)
82341480Smckusick 		return;
82441480Smckusick 
82541480Smckusick 	rmd = &le->sc_r2->ler2_rmd[le->sc_rmd];
82641480Smckusick 	len = rmd->rmd3;
82741480Smckusick 	log(LOG_WARNING,
82841480Smckusick 	    "le%d: ierror(%s): from %s: buf=%d, len=%d, rmd1=%b\n",
82941480Smckusick 	    unit, msg,
83041480Smckusick 	    len > 11 ? ether_sprintf(&le->sc_r2->ler2_rbuf[le->sc_rmd][6]) : "unknown",
83141480Smckusick 	    le->sc_rmd, len,
83241480Smckusick 	    rmd->rmd1,
83341480Smckusick 	    "\20\20OWN\17ERR\16FRAM\15OFLO\14CRC\13RBUF\12STP\11ENP");
83441480Smckusick }
83541480Smckusick 
83641480Smckusick lexerror(unit)
83741480Smckusick 	int unit;
83841480Smckusick {
83941480Smckusick 	register struct le_softc *le = &le_softc[unit];
84041480Smckusick 	register struct letmd *tmd;
84141480Smckusick 	int len;
84241480Smckusick 
84341480Smckusick 	if (!ledebug)
84441480Smckusick 		return;
84541480Smckusick 
84641480Smckusick 	tmd = le->sc_r2->ler2_tmd;
84741480Smckusick 	len = -tmd->tmd2;
84841480Smckusick 	log(LOG_WARNING,
84941480Smckusick 	    "le%d: oerror: to %s: buf=%d, len=%d, tmd1=%b, tmd3=%b\n",
85041480Smckusick 	    unit,
85141480Smckusick 	    len > 5 ? ether_sprintf(&le->sc_r2->ler2_tbuf[0][0]) : "unknown",
85241480Smckusick 	    0, len,
85341480Smckusick 	    tmd->tmd1,
85441480Smckusick 	    "\20\20OWN\17ERR\16RES\15MORE\14ONE\13DEF\12STP\11ENP",
85541480Smckusick 	    tmd->tmd3,
85641480Smckusick 	    "\20\20BUFF\17UFLO\16RES\15LCOL\14LCAR\13RTRY");
85741480Smckusick }
85841480Smckusick #endif
859