Home | History | Annotate | Line # | Download | only in dev
if_le.c revision 1.8
      1 /*
      2  * Copyright (c) 1982, 1990 The Regents of the University of California.
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  *
     33  *	from: @(#)if_le.c	7.6 (Berkeley) 5/8/91
     34  *	$Id: if_le.c,v 1.8 1994/02/06 00:46:02 mycroft Exp $
     35  */
     36 
     37 #include "le.h"
     38 #if NLE > 0
     39 
     40 #include "bpfilter.h"
     41 
     42 /*
     43  * AMD 7990 LANCE
     44  */
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/kernel.h>
     48 #include <sys/mbuf.h>
     49 #include <sys/buf.h>
     50 #include <sys/socket.h>
     51 #include <sys/syslog.h>
     52 #include <sys/ioctl.h>
     53 #include <sys/malloc.h>
     54 #include <sys/errno.h>
     55 
     56 #include <net/if.h>
     57 #include <net/netisr.h>
     58 #include <net/route.h>
     59 #if NBPFILTER > 0
     60 #include <net/bpf.h>
     61 #include <net/bpfdesc.h>
     62 #endif
     63 
     64 #ifdef INET
     65 #include <netinet/in.h>
     66 #include <netinet/in_systm.h>
     67 #include <netinet/in_var.h>
     68 #include <netinet/ip.h>
     69 #include <netinet/if_ether.h>
     70 #endif
     71 
     72 #ifdef NS
     73 #include <netns/ns.h>
     74 #include <netns/ns_if.h>
     75 #endif
     76 
     77 #include <machine/cpu.h>
     78 #include <hp300/hp300/isr.h>
     79 #include <machine/mtpr.h>
     80 
     81 #include <hp300/dev/device.h>
     82 #include <hp300/dev/if_lereg.h>
     83 
     84 /* offsets for:	   ID,   REGS,    MEM,  NVRAM */
     85 int	lestd[] = { 0, 0x4000, 0x8000, 0xC008 };
     86 
     87 struct	isr le_isr[NLE];
     88 int	ledebug = 0;		/* console error messages */
     89 
     90 /*
     91  * Ethernet software status per interface.
     92  *
     93  * Each interface is referenced by a network interface structure,
     94  * le_if, which the routing code uses to locate the interface.
     95  * This structure contains the output queue for the interface, its address, ...
     96  */
     97 struct	le_softc {
     98 	struct	arpcom sc_ac;	/* common Ethernet structures */
     99 #define	sc_if	sc_ac.ac_if	/* network-visible interface */
    100 #define	sc_addr	sc_ac.ac_enaddr	/* hardware Ethernet address */
    101 	struct	lereg0 *sc_r0;	/* DIO registers */
    102 	struct	lereg1 *sc_r1;	/* LANCE registers */
    103 	struct	lereg2 *sc_r2;	/* dual-port RAM */
    104 	int	sc_rmd;		/* predicted next rmd to process */
    105 	int	sc_tmd;		/* next available tmd */
    106 	int	sc_txcnt;	/* # of transmit buffers in use */
    107 	/* stats */
    108 	int	sc_runt;
    109 	int	sc_jab;
    110 	int	sc_merr;
    111 	int	sc_babl;
    112 	int	sc_cerr;
    113 	int	sc_miss;
    114 	int	sc_rown;
    115 	int	sc_xint;
    116 	int	sc_xown;
    117 	int	sc_xown2;
    118 	int	sc_uflo;
    119 	int	sc_rxlen;
    120 	int	sc_rxoff;
    121 	int	sc_txoff;
    122 	int	sc_busy;
    123 	short	sc_iflags;
    124 #if NBPFILTER > 0
    125 	caddr_t sc_bpf;
    126 #endif
    127 } le_softc[NLE];
    128 
    129 /* access LANCE registers */
    130 #define	LERDWR(cntl, src, dst) \
    131 	do { \
    132 		(dst) = (src); \
    133 	} while (((cntl)->ler0_status & LE_ACK) == 0);
    134 
    135 int leattach __P((struct hp_device *));
    136 void lesetladrf __P((struct le_softc *));
    137 void ledrinit __P((struct lereg2 *));
    138 void lereset __P((struct le_softc *));
    139 void leinit __P((int));
    140 int lestart __P((struct ifnet *));
    141 int leintr __P((int));
    142 void lexint __P((struct le_softc *));
    143 void lerint __P((struct le_softc *));
    144 void leread __P((struct le_softc *, char *, int));
    145 int leput __P((char *, struct mbuf *));
    146 struct mbuf *leget __P((char *, int, int, struct ifnet *));
    147 int leioctl __P((struct ifnet *, int, caddr_t));
    148 void leerror __P((struct le_softc *, int));
    149 void lererror __P((struct le_softc *, char *));
    150 void lexerror __P((struct le_softc *));
    151 int ether_output();
    152 
    153 struct	driver ledriver = {
    154 	leattach, "le",
    155 };
    156 
    157 /*
    158  * Interface exists: make available by filling in network interface
    159  * record.  System will initialize the interface when it is ready
    160  * to accept packets.
    161  */
    162 int
    163 leattach(hd)
    164 	struct hp_device *hd;
    165 {
    166 	register struct lereg0 *ler0;
    167 	register struct lereg2 *ler2;
    168 	struct lereg2 *lemem = 0;
    169 	struct le_softc *sc = &le_softc[hd->hp_unit];
    170 	struct ifnet *ifp = &sc->sc_if;
    171 	char *cp;
    172 	int i;
    173 
    174 	ler0 = sc->sc_r0 = (struct lereg0 *)(lestd[0] + (int)hd->hp_addr);
    175 	sc->sc_r1 = (struct lereg1 *)(lestd[1] + (int)hd->hp_addr);
    176 	ler2 = sc->sc_r2 = (struct lereg2 *)(lestd[2] + (int)hd->hp_addr);
    177 	if (ler0->ler0_id != LEID)
    178 		return(0);
    179 	le_isr[hd->hp_unit].isr_intr = leintr;
    180 	hd->hp_ipl = le_isr[hd->hp_unit].isr_ipl = LE_IPL(ler0->ler0_status);
    181 	le_isr[hd->hp_unit].isr_arg = hd->hp_unit;
    182 	ler0->ler0_id = 0xFF;
    183 	DELAY(100);
    184 
    185 	/*
    186 	 * Read the ethernet address off the board, one nibble at a time.
    187 	 */
    188 	cp = (char *)(lestd[3] + (int)hd->hp_addr);
    189 	for (i = 0; i < sizeof(sc->sc_addr); i++) {
    190 		sc->sc_addr[i] = (*++cp & 0xF) << 4;
    191 		cp++;
    192 		sc->sc_addr[i] |= *++cp & 0xF;
    193 		cp++;
    194 	}
    195 	printf("le%d: hardware address %s\n", hd->hp_unit,
    196 		ether_sprintf(sc->sc_addr));
    197 
    198 	/*
    199 	 * Setup for transmit/receive
    200 	 */
    201 	ler2->ler2_mode = LE_MODE;
    202 	ler2->ler2_rlen = LE_RLEN;
    203 	ler2->ler2_rdra = (int)lemem->ler2_rmd;
    204 	ler2->ler2_tlen = LE_TLEN;
    205 	ler2->ler2_tdra = (int)lemem->ler2_tmd;
    206 	isrlink(&le_isr[hd->hp_unit]);
    207 	ler0->ler0_status = LE_IE;
    208 
    209 	ifp->if_unit = hd->hp_unit;
    210 	ifp->if_name = "le";
    211 	ifp->if_mtu = ETHERMTU;
    212 	ifp->if_ioctl = leioctl;
    213 	ifp->if_output = ether_output;
    214 	ifp->if_start = lestart;
    215 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST |
    216 			IFF_NOTRAILERS;
    217 #if NBPFILTER > 0
    218 	bpfattach(&sc->sc_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
    219 #endif
    220 	if_attach(ifp);
    221 	return (1);
    222 }
    223 
    224 /*
    225  * Set up the logical address filter
    226  */
    227 void
    228 lesetladrf(sc)
    229 	struct le_softc *sc;
    230 {
    231 	struct lereg2 *ler2 = sc->sc_r2;
    232 	struct ifnet *ifp = &sc->sc_if;
    233 	struct ether_multi *enm;
    234 	register u_char *cp, c;
    235 	register u_long crc;
    236 	register int i, len;
    237 	struct ether_multistep step;
    238 
    239 	/*
    240 	 * Set up multicast address filter by passing all multicast
    241 	 * addresses through a crc generator, and then using the high
    242 	 * order 6 bits as a index into the 64 bit logical address
    243 	 * filter. The high order two bits select the word, while the
    244 	 * rest of the bits select the bit within the word.
    245 	 */
    246 
    247 	ler2->ler2_ladrf[0] = 0;
    248 	ler2->ler2_ladrf[1] = 0;
    249 	ifp->if_flags &= ~IFF_ALLMULTI;
    250 	ETHER_FIRST_MULTI(step, &sc->sc_ac, enm);
    251 	while (enm != NULL) {
    252 		if (bcmp((caddr_t)&enm->enm_addrlo,
    253 		    (caddr_t)&enm->enm_addrhi, sizeof(enm->enm_addrlo)) != 0) {
    254 			/*
    255 			 * We must listen to a range of multicast
    256 			 * addresses. For now, just accept all
    257 			 * multicasts, rather than trying to set only
    258 			 * those filter bits needed to match the range.
    259 			 * (At this time, the only use of address
    260 			 * ranges is for IP multicast routing, for
    261 			 * which the range is big enough to require all
    262 			 * bits set.)
    263 			 */
    264 			ler2->ler2_ladrf[0] = 0xffffffff;
    265 			ler2->ler2_ladrf[1] = 0xffffffff;
    266 			ifp->if_flags |= IFF_ALLMULTI;
    267 			return;
    268 		}
    269 
    270 		/*
    271 		 * One would think, given the AM7990 document's polynomial
    272 		 * of 0x04c11db6, that this should be 0x6db88320 (the bit
    273 		 * reversal of the AMD value), but that is not right.  See
    274 		 * the BASIC listing: bit 0 (our bit 31) must then be set.
    275 		 */
    276 		cp = (unsigned char *)&enm->enm_addrlo;
    277 		crc = 0xffffffff;
    278 		for (len = 6; --len >= 0;) {
    279 			c = *cp++;
    280 			for (i = 8; --i >= 0;) {
    281 				if ((c & 0x01) ^ (crc & 0x01)) {
    282 					crc >>= 1;
    283 					crc = crc ^ 0xedb88320;
    284 				} else
    285 					crc >>= 1;
    286 				c >>= 1;
    287 			}
    288 		}
    289 		/* Just want the 6 most significant bits. */
    290 		crc = crc >> 26;
    291 
    292 		/* Turn on the corresponding bit in the filter. */
    293 		ler2->ler2_ladrf[crc >> 5] |= 1 << (crc & 0x1f);
    294 
    295 		ETHER_NEXT_MULTI(step, enm);
    296 	}
    297 }
    298 
    299 void
    300 ledrinit(ler2)
    301 	register struct lereg2 *ler2;
    302 {
    303 	register struct lereg2 *lemem = 0;
    304 	register int i;
    305 
    306 	for (i = 0; i < LERBUF; i++) {
    307 		ler2->ler2_rmd[i].rmd0 = (int)lemem->ler2_rbuf[i];
    308 		ler2->ler2_rmd[i].rmd1 = LE_OWN;
    309 		ler2->ler2_rmd[i].rmd2 = -LEMTU;
    310 		ler2->ler2_rmd[i].rmd3 = 0;
    311 	}
    312 	for (i = 0; i < LETBUF; i++) {
    313 		ler2->ler2_tmd[i].tmd0 = (int)lemem->ler2_tbuf[i];
    314 		ler2->ler2_tmd[i].tmd1 = 0;
    315 		ler2->ler2_tmd[i].tmd2 = 0;
    316 		ler2->ler2_tmd[i].tmd3 = 0;
    317 	}
    318 }
    319 
    320 void
    321 lereset(sc)
    322 	register struct le_softc *sc;
    323 {
    324 	register struct lereg0 *ler0 = sc->sc_r0;
    325 	register struct lereg1 *ler1 = sc->sc_r1;
    326 	register struct lereg2 *ler2 = sc->sc_r2;
    327 	struct lereg2 *lemem = 0;
    328 	register int timo, stat;
    329 
    330 #if NBPFILTER > 0
    331 	if (sc->sc_if.if_flags & IFF_PROMISC)
    332 		/* set the promiscuous bit */
    333 		ler2->ler2_mode = LE_MODE|0x8000;
    334 	else
    335 #endif
    336 		ler2->ler2_mode = LE_MODE;
    337 	LERDWR(ler0, LE_CSR0, ler1->ler1_rap);
    338 	LERDWR(ler0, LE_STOP, ler1->ler1_rdp);
    339 
    340 	ler2->ler2_padr[0] = sc->sc_addr[1];
    341 	ler2->ler2_padr[1] = sc->sc_addr[0];
    342 	ler2->ler2_padr[2] = sc->sc_addr[3];
    343 	ler2->ler2_padr[3] = sc->sc_addr[2];
    344 	ler2->ler2_padr[4] = sc->sc_addr[5];
    345 	ler2->ler2_padr[5] = sc->sc_addr[4];
    346 	lesetladrf(sc);
    347 	ledrinit(ler2);
    348 	sc->sc_rmd = sc->sc_tmd = sc->sc_txcnt = 0;
    349 
    350 	LERDWR(ler0, LE_CSR1, ler1->ler1_rap);
    351 	LERDWR(ler0, (int)&lemem->ler2_mode, ler1->ler1_rdp);
    352 	LERDWR(ler0, LE_CSR2, ler1->ler1_rap);
    353 	LERDWR(ler0, 0, ler1->ler1_rdp);
    354 	LERDWR(ler0, LE_CSR3, ler1->ler1_rap);
    355 	LERDWR(ler0, LE_BSWP, ler1->ler1_rdp);
    356 	LERDWR(ler0, LE_CSR0, ler1->ler1_rap);
    357 	LERDWR(ler0, LE_INIT, ler1->ler1_rdp);
    358 	timo = 100000;
    359 	do {
    360 		if (--timo == 0) {
    361 			printf("le%d: init timeout, stat=0x%x\n",
    362 			    sc->sc_if.if_unit, stat);
    363 			break;
    364 		}
    365 		LERDWR(ler0, ler1->ler1_rdp, stat);
    366 	} while ((stat & (LE_IDON | LE_ERR)) == 0);
    367 	if (stat & LE_ERR)
    368 		printf("le%d: init failed, stat=0x%x\n",
    369 		    sc->sc_if.if_unit, stat);
    370 	else
    371 		LERDWR(ler0, LE_IDON, ler1->ler1_rdp);
    372 	sc->sc_if.if_flags &= ~IFF_OACTIVE;
    373 	LERDWR(ler0, LE_STRT | LE_INEA, ler1->ler1_rdp);
    374 }
    375 
    376 /*
    377  * Initialization of interface
    378  */
    379 void
    380 leinit(unit)
    381 	int unit;
    382 {
    383 	struct le_softc *sc = &le_softc[unit];
    384 	register struct ifnet *ifp = &sc->sc_if;
    385 	int s;
    386 
    387 	/* not yet, if address still unknown */
    388 	if (ifp->if_addrlist == (struct ifaddr *)0)
    389 		return;
    390 	if ((ifp->if_flags & IFF_RUNNING) == 0) {
    391 		s = splimp();
    392 		ifp->if_flags |= IFF_RUNNING;
    393 		lereset(sc);
    394 		(void) lestart(ifp);
    395 		splx(s);
    396 	}
    397 }
    398 
    399 #define	LENEXTTMP \
    400 	if (++bix == LETBUF) bix = 0, tmd = sc->sc_r2->ler2_tmd; else ++tmd
    401 
    402 /*
    403  * Start output on interface.  Get another datagram to send
    404  * off of the interface queue, and copy it to the interface
    405  * before starting the output.
    406  */
    407 int
    408 lestart(ifp)
    409 	struct ifnet *ifp;
    410 {
    411 	register struct le_softc *sc = &le_softc[ifp->if_unit];
    412 	register int bix;
    413 	register struct letmd *tmd;
    414 	register struct mbuf *m;
    415 	int len, gotone = 0;
    416 
    417 	if ((sc->sc_if.if_flags & IFF_RUNNING) == 0)
    418 		return (0);
    419 	bix = sc->sc_tmd;
    420 	tmd = &sc->sc_r2->ler2_tmd[bix];
    421 	do {
    422 		if (tmd->tmd1 & LE_OWN) {
    423 			if (gotone)
    424 				break;
    425 			sc->sc_xown2++;
    426 			return (0);
    427 		}
    428 		IF_DEQUEUE(&sc->sc_if.if_snd, m);
    429 		if (m == 0) {
    430 			if (gotone)
    431 				break;
    432 			return (0);
    433 		}
    434 		len = leput(sc->sc_r2->ler2_tbuf[bix], m);
    435 #if NBPFILTER > 0
    436 		/*
    437 		 * If bpf is listening on this interface, let it
    438 		 * see the packet before we commit it to the wire.
    439 		 */
    440 		if (sc->sc_bpf)
    441 			bpf_tap(sc->sc_bpf, sc->sc_r2->ler2_tbuf[bix], len);
    442 #endif
    443 		tmd->tmd3 = 0;
    444 		tmd->tmd2 = -len;
    445 		tmd->tmd1 = LE_OWN | LE_STP | LE_ENP;
    446 		LENEXTTMP;
    447 		gotone++;
    448 	} while (++sc->sc_txcnt < LETBUF);
    449 	sc->sc_tmd = bix;
    450 	sc->sc_if.if_flags |= IFF_OACTIVE;
    451 	/* transmit as soon as possible */
    452 	LERDWR(sc->sc_r0, LE_INEA|LE_TDMD, sc->sc_r1->ler1_rdp);
    453 	return (0);
    454 }
    455 
    456 int
    457 leintr(unit)
    458 	register int unit;
    459 {
    460 	register struct le_softc *sc = &le_softc[unit];
    461 	register struct lereg0 *ler0 = sc->sc_r0;
    462 	register struct lereg1 *ler1;
    463 	register int stat;
    464 
    465 	if ((ler0->ler0_status & LE_IR) == 0)
    466 		return(0);
    467 	if (ler0->ler0_status & LE_JAB) {
    468 		sc->sc_jab++;
    469 		lereset(sc);
    470 		return(1);
    471 	}
    472 	ler1 = sc->sc_r1;
    473 	LERDWR(ler0, ler1->ler1_rdp, stat);
    474 	if (stat & LE_SERR) {
    475 		leerror(sc, stat);
    476 		if (stat & LE_MERR) {
    477 			sc->sc_merr++;
    478 			lereset(sc);
    479 			return(1);
    480 		}
    481 		if (stat & LE_BABL)
    482 			sc->sc_babl++;
    483 		if (stat & LE_CERR)
    484 			sc->sc_cerr++;
    485 		if (stat & LE_MISS)
    486 			sc->sc_miss++;
    487 		LERDWR(ler0, LE_BABL|LE_CERR|LE_MISS|LE_INEA, ler1->ler1_rdp);
    488 	}
    489 	if ((stat & LE_RXON) == 0) {
    490 		sc->sc_rxoff++;
    491 		lereset(sc);
    492 		return(1);
    493 	}
    494 	if ((stat & LE_TXON) == 0) {
    495 		sc->sc_txoff++;
    496 		lereset(sc);
    497 		return(1);
    498 	}
    499 	if (stat & LE_RINT)
    500 		lerint(sc);
    501 	if (stat & LE_TINT)
    502 		lexint(sc);
    503 	return(1);
    504 }
    505 
    506 /*
    507  * Ethernet interface transmitter interrupt.
    508  * Start another output if more data to send.
    509  */
    510 void
    511 lexint(sc)
    512 	register struct le_softc *sc;
    513 {
    514 	register struct letmd *tmd;
    515 	int bix, gotone = 0;
    516 
    517 	if ((sc->sc_if.if_flags & IFF_OACTIVE) == 0) {
    518 		sc->sc_xint++;
    519 		return;
    520 	}
    521 	if ((bix = sc->sc_tmd - sc->sc_txcnt) < 0)
    522 		bix += LETBUF;
    523 	tmd = &sc->sc_r2->ler2_tmd[bix];
    524 	do {
    525 		if (tmd->tmd1 & LE_OWN) {
    526 			if (gotone)
    527 				break;
    528 			sc->sc_xown++;
    529 			return;
    530 		}
    531 
    532 		/* clear interrupt */
    533 		LERDWR(sc->sc_r0, LE_TINT|LE_INEA, sc->sc_r1->ler1_rdp);
    534 
    535 		/* XXX documentation says BUFF not included in ERR */
    536 		if ((tmd->tmd1 & LE_ERR) || (tmd->tmd3 & LE_TBUFF)) {
    537 			lexerror(sc);
    538 			sc->sc_if.if_oerrors++;
    539 			if (tmd->tmd3 & (LE_TBUFF|LE_UFLO)) {
    540 				sc->sc_uflo++;
    541 				lereset(sc);
    542 			} else if (tmd->tmd3 & LE_LCOL)
    543 				sc->sc_if.if_collisions++;
    544 			else if (tmd->tmd3 & LE_RTRY)
    545 				sc->sc_if.if_collisions += 16;
    546 		}
    547 		else if (tmd->tmd1 & LE_ONE)
    548 			sc->sc_if.if_collisions++;
    549 		else if (tmd->tmd1 & LE_MORE)
    550 			/* what is the real number? */
    551 			sc->sc_if.if_collisions += 2;
    552 		else
    553 			sc->sc_if.if_opackets++;
    554 		LENEXTTMP;
    555 		gotone++;
    556 	} while (--sc->sc_txcnt > 0);
    557 	sc->sc_if.if_flags &= ~IFF_OACTIVE;
    558 	(void) lestart(&sc->sc_if);
    559 }
    560 
    561 #define	LENEXTRMP \
    562 	if (++bix == LERBUF) bix = 0, rmd = sc->sc_r2->ler2_rmd; else ++rmd
    563 
    564 /*
    565  * Ethernet interface receiver interrupt.
    566  * If input error just drop packet.
    567  * Decapsulate packet based on type and pass to type specific
    568  * higher-level input routine.
    569  */
    570 void
    571 lerint(sc)
    572 	register struct le_softc *sc;
    573 {
    574 	register int bix = sc->sc_rmd;
    575 	register struct lermd *rmd = &sc->sc_r2->ler2_rmd[bix];
    576 
    577 	/*
    578 	 * Out of sync with hardware, should never happen?
    579 	 */
    580 	if (rmd->rmd1 & LE_OWN) {
    581 		sc->sc_rown++;
    582 		do {
    583 			LENEXTRMP;
    584 		} while ((rmd->rmd1 & LE_OWN) && bix != sc->sc_rmd);
    585 		if (bix == sc->sc_rmd) {
    586 			printf("le%d: rint with no buffer\n",
    587 			    sc->sc_if.if_unit);
    588 			LERDWR(sc->sc_r0, LE_RINT|LE_INEA, sc->sc_r1->ler1_rdp);
    589 			return;
    590 		}
    591 	}
    592 
    593 	/*
    594 	 * Process all buffers with valid data
    595 	 */
    596 	while ((rmd->rmd1 & LE_OWN) == 0) {
    597 		int len = rmd->rmd3;
    598 
    599 		/* Clear interrupt to avoid race condition */
    600 		LERDWR(sc->sc_r0, LE_RINT|LE_INEA, sc->sc_r1->ler1_rdp);
    601 
    602 		if (rmd->rmd1 & LE_ERR) {
    603 			sc->sc_rmd = bix;
    604 			lererror(sc, "bad packet");
    605 			sc->sc_if.if_ierrors++;
    606 		} else if ((rmd->rmd1 & (LE_STP|LE_ENP)) != (LE_STP|LE_ENP)) {
    607 			/*
    608 			 * Find the end of the packet so we can see how long
    609 			 * it was.  We still throw it away.
    610 			 */
    611 			do {
    612 				LERDWR(sc->sc_r0, LE_RINT|LE_INEA,
    613 				       sc->sc_r1->ler1_rdp);
    614 				rmd->rmd3 = 0;
    615 				rmd->rmd1 = LE_OWN;
    616 				LENEXTRMP;
    617 			} while (!(rmd->rmd1 & (LE_OWN|LE_ERR|LE_STP|LE_ENP)));
    618 			sc->sc_rmd = bix;
    619 			lererror(sc, "chained buffer");
    620 			sc->sc_rxlen++;
    621 			/*
    622 			 * If search terminated without successful completion
    623 			 * we reset the hardware (conservative).
    624 			 */
    625 			if ((rmd->rmd1 & (LE_OWN|LE_ERR|LE_STP|LE_ENP)) !=
    626 			    LE_ENP) {
    627 				lereset(sc);
    628 				return;
    629 			}
    630 		} else
    631 			leread(sc, sc->sc_r2->ler2_rbuf[bix], len);
    632 		rmd->rmd3 = 0;
    633 		rmd->rmd1 = LE_OWN;
    634 		LENEXTRMP;
    635 	}
    636 	sc->sc_rmd = bix;
    637 }
    638 
    639 void
    640 leread(sc, buf, len)
    641 	register struct le_softc *sc;
    642 	char *buf;
    643 	int len;
    644 {
    645 	register struct ether_header *et;
    646 	register struct ifnet *ifp = &sc->sc_if;
    647     	struct mbuf *m;
    648 
    649 	ifp->if_ipackets++;
    650 	et = (struct ether_header *)buf;
    651 	/* adjust input length to account for header and CRC */
    652 	len -= sizeof(struct ether_header) + 4;
    653 
    654 	if (len <= 0) {
    655 		if (ledebug)
    656 			log(LOG_WARNING,
    657 			    "le%d: ierror(runt packet): from %s: len=%d\n",
    658 			    sc->sc_if.if_unit, ether_sprintf(et->ether_shost),
    659 			    len);
    660 		sc->sc_runt++;
    661 		ifp->if_ierrors++;
    662 		return;
    663 	}
    664 
    665 #if NBPFILTER > 0
    666 	/*
    667 	 * Check if there's a bpf filter listening on this interface.
    668 	 * If so, hand off the raw packet to bpf, then discard things
    669 	 * not destined for us (but be sure to keep broadcast/multicast).
    670 	 */
    671 	if (sc->sc_bpf) {
    672 		bpf_tap(sc->sc_bpf, buf, len + sizeof(struct ether_header));
    673 		if ((ifp->if_flags & IFF_PROMISC) &&
    674 		    (et->ether_dhost[0] & 1) == 0 &&
    675 		    bcmp(et->ether_dhost, sc->sc_addr,
    676 			    sizeof(et->ether_dhost)) != 0 &&
    677 		    bcmp(et->ether_dhost, etherbroadcastaddr,
    678 			    sizeof(et->ether_dhost)) != 0)
    679 			return;
    680 	}
    681 #endif
    682 
    683 	m = leget(buf, len, 0, ifp);
    684 	if (m == 0)
    685 		return;
    686 
    687 	ether_input(ifp, et, m);
    688 }
    689 
    690 /*
    691  * Routine to copy from mbuf chain to transmit
    692  * buffer in board local memory.
    693  */
    694 int
    695 leput(lebuf, m)
    696 	register char *lebuf;
    697 	register struct mbuf *m;
    698 {
    699 	register struct mbuf *mp;
    700 	register int len, tlen = 0;
    701 
    702 	for (mp = m; mp; mp = mp->m_next) {
    703 		len = mp->m_len;
    704 		if (len == 0)
    705 			continue;
    706 		tlen += len;
    707 		bcopy(mtod(mp, char *), lebuf, len);
    708 		lebuf += len;
    709 	}
    710 	m_freem(m);
    711 	if (tlen < LEMINSIZE) {
    712 		bzero(lebuf, LEMINSIZE - tlen);
    713 		tlen = LEMINSIZE;
    714 	}
    715 	return(tlen);
    716 }
    717 
    718 /*
    719  * Routine to copy from board local memory into mbufs.
    720  */
    721 struct mbuf *
    722 leget(lebuf, totlen, off0, ifp)
    723 	char *lebuf;
    724 	int totlen, off0;
    725 	struct ifnet *ifp;
    726 {
    727 	register struct mbuf *m;
    728 	struct mbuf *top = 0, **mp = &top;
    729 	register int off = off0, len;
    730 	register char *cp;
    731 	char *epkt;
    732 
    733 	lebuf += sizeof (struct ether_header);
    734 	cp = lebuf;
    735 	epkt = cp + totlen;
    736 	if (off) {
    737 		cp += off + 2 * sizeof(u_short);
    738 		totlen -= 2 * sizeof(u_short);
    739 	}
    740 
    741 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    742 	if (m == 0)
    743 		return (0);
    744 	m->m_pkthdr.rcvif = ifp;
    745 	m->m_pkthdr.len = totlen;
    746 	m->m_len = MHLEN;
    747 
    748 	while (totlen > 0) {
    749 		if (top) {
    750 			MGET(m, M_DONTWAIT, MT_DATA);
    751 			if (m == 0) {
    752 				m_freem(top);
    753 				return (0);
    754 			}
    755 			m->m_len = MLEN;
    756 		}
    757 		len = min(totlen, epkt - cp);
    758 		if (len >= MINCLSIZE) {
    759 			MCLGET(m, M_DONTWAIT);
    760 			if (m->m_flags & M_EXT)
    761 				m->m_len = len = min(len, MCLBYTES);
    762 			else
    763 				len = m->m_len;
    764 		} else {
    765 			/*
    766 			 * Place initial small packet/header at end of mbuf.
    767 			 */
    768 			if (len < m->m_len) {
    769 				if (top == 0 && len + max_linkhdr <= m->m_len)
    770 					m->m_data += max_linkhdr;
    771 				m->m_len = len;
    772 			} else
    773 				len = m->m_len;
    774 		}
    775 		bcopy(cp, mtod(m, caddr_t), (unsigned)len);
    776 		cp += len;
    777 		*mp = m;
    778 		mp = &m->m_next;
    779 		totlen -= len;
    780 		if (cp == epkt)
    781 			cp = lebuf;
    782 	}
    783 	return (top);
    784 }
    785 
    786 /*
    787  * Process an ioctl request.
    788  */
    789 int
    790 leioctl(ifp, cmd, data)
    791 	register struct ifnet *ifp;
    792 	int cmd;
    793 	caddr_t data;
    794 {
    795 	register struct ifaddr *ifa;
    796 	struct le_softc *sc = &le_softc[ifp->if_unit];
    797 	int s = splimp(), error = 0;
    798 
    799 	switch (cmd) {
    800 
    801 	case SIOCSIFADDR:
    802 		ifa = (struct ifaddr *)data;
    803 		ifp->if_flags |= IFF_UP;
    804 		switch (ifa->ifa_addr->sa_family) {
    805 #ifdef INET
    806 		case AF_INET:
    807 			leinit(ifp->if_unit);	/* before arpwhohas */
    808 			((struct arpcom *)ifp)->ac_ipaddr =
    809 				IA_SIN(ifa)->sin_addr;
    810 			arpwhohas((struct arpcom *)ifp, &IA_SIN(ifa)->sin_addr);
    811 			break;
    812 #endif
    813 #ifdef NS
    814 		case AF_NS:
    815 		    {
    816 			register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
    817 
    818 			if (ns_nullhost(*ina))
    819 				ina->x_host = *(union ns_host *)(sc->sc_addr);
    820 			else {
    821 				/*
    822 				 * The manual says we can't change the address
    823 				 * while the receiver is armed,
    824 				 * so reset everything
    825 				 */
    826 				ifp->if_flags &= ~IFF_RUNNING;
    827 				LERDWR(sc->sc_r0, LE_STOP, sc->sc_r1->ler1_rdp);
    828 				bcopy((caddr_t)ina->x_host.c_host,
    829 				    (caddr_t)sc->sc_addr, sizeof(sc->sc_addr));
    830 			}
    831 			leinit(ifp->if_unit); /* does le_setaddr() */
    832 			break;
    833 		    }
    834 #endif
    835 		default:
    836 			leinit(ifp->if_unit);
    837 			break;
    838 		}
    839 		break;
    840 
    841 	case SIOCSIFFLAGS:
    842 		if ((ifp->if_flags & IFF_UP) == 0 &&
    843 		    ifp->if_flags & IFF_RUNNING) {
    844 			ifp->if_flags &= ~IFF_RUNNING;
    845 			LERDWR(sc->sc_r0, LE_STOP, sc->sc_r1->ler1_rdp);
    846 		} else if (ifp->if_flags & IFF_UP &&
    847 		    (ifp->if_flags & IFF_RUNNING) == 0)
    848 			leinit(ifp->if_unit);
    849 		/*
    850 		 * If the state of the promiscuous bit changes, the interface
    851 		 * must be reset to effect the change.
    852 		 */
    853 		if (((ifp->if_flags ^ sc->sc_iflags) & IFF_PROMISC) &&
    854 		    (ifp->if_flags & IFF_RUNNING)) {
    855 			sc->sc_iflags = ifp->if_flags;
    856 			lereset(sc);
    857 			lestart(ifp);
    858 		}
    859 		break;
    860 
    861 	case SIOCADDMULTI:
    862 		error = ether_addmulti((struct ifreq *)data, &sc->sc_ac);
    863 		goto update_multicast;
    864 
    865 	case SIOCDELMULTI:
    866 		error = ether_delmulti((struct ifreq *)data, &sc->sc_ac);
    867 	update_multicast:
    868 		if (error == ENETRESET) {
    869 			/*
    870 			 * Multicast list has changed; set the hardware
    871 			 * filter accordingly.
    872 			 */
    873 			lereset(sc);
    874 			error = 0;
    875 		}
    876 		break;
    877 
    878 	default:
    879 		error = EINVAL;
    880 	}
    881 	splx(s);
    882 	return (error);
    883 }
    884 
    885 void
    886 leerror(sc, stat)
    887 	register struct le_softc *sc;
    888 	int stat;
    889 {
    890 
    891 	if (!ledebug)
    892 		return;
    893 
    894 	/*
    895 	 * Not all transceivers implement heartbeat
    896 	 * so we only log CERR once.
    897 	 */
    898 	if ((stat & LE_CERR) && sc->sc_cerr)
    899 		return;
    900 	log(LOG_WARNING,
    901 	    "le%d: error: stat=%b\n", sc->sc_if.if_unit, stat,
    902 	    "\20\20ERR\17BABL\16CERR\15MISS\14MERR\13RINT\12TINT\11IDON\10INTR\07INEA\06RXON\05TXON\04TDMD\03STOP\02STRT\01INIT");
    903 }
    904 
    905 void
    906 lererror(sc, msg)
    907 	register struct le_softc *sc;
    908 	char *msg;
    909 {
    910 	register struct lermd *rmd;
    911 	int len;
    912 
    913 	if (!ledebug)
    914 		return;
    915 
    916 	rmd = &sc->sc_r2->ler2_rmd[sc->sc_rmd];
    917 	len = rmd->rmd3;
    918 	log(LOG_WARNING,
    919 	    "le%d: ierror(%s): from %s: buf=%d, len=%d, rmd1=%b\n",
    920 	    sc->sc_if.if_unit, msg,
    921 	    len > 11 ? ether_sprintf(&sc->sc_r2->ler2_rbuf[sc->sc_rmd][6]) : "unknown",
    922 	    sc->sc_rmd, len, rmd->rmd1,
    923 	    "\20\20OWN\17ERR\16FRAM\15OFLO\14CRC\13RBUF\12STP\11ENP");
    924 }
    925 
    926 void
    927 lexerror(sc)
    928 	register struct le_softc *sc;
    929 {
    930 	register struct letmd *tmd;
    931 	register int len;
    932 
    933 	if (!ledebug)
    934 		return;
    935 
    936 	tmd = sc->sc_r2->ler2_tmd;
    937 	len = -tmd->tmd2;
    938 	log(LOG_WARNING,
    939 	    "le%d: oerror: to %s: buf=%d, len=%d, tmd1=%b, tmd3=%b\n",
    940 	    sc->sc_if.if_unit,
    941 	    len > 5 ? ether_sprintf(&sc->sc_r2->ler2_tbuf[0][0]) : "unknown",
    942 	    0, len, tmd->tmd1,
    943 	    "\20\20OWN\17ERR\16RES\15MORE\14ONE\13DEF\12STP\11ENP",
    944 	    tmd->tmd3,
    945 	    "\20\20BUFF\17UFLO\16RES\15LCOL\14LCAR\13RTRY");
    946 }
    947 #endif
    948