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