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