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