Home | History | Annotate | Line # | Download | only in dev
if_le.c revision 1.18
      1 /*	$NetBSD: if_le.c,v 1.18 1995/04/13 21:54:55 gwr Exp $	*/
      2 
      3 /*
      4  * LANCE Ethernet driver
      5  *
      6  * Copyright (c) 1995 Gordon W. Ross
      7  * Copyright (c) 1994 Charles Hannum.
      8  *
      9  * Copyright (C) 1993, Paul Richards. This software may be used, modified,
     10  *   copied, distributed, and sold, in both source and binary form provided
     11  *   that the above copyright and these terms are retained. Under no
     12  *   circumstances is the author responsible for the proper functioning
     13  *   of this software, nor does the author assume any responsibility
     14  *   for damages incurred with its use.
     15  */
     16 
     17 #include "bpfilter.h"
     18 
     19 #include <sys/param.h>
     20 #include <sys/systm.h>
     21 #include <sys/errno.h>
     22 #include <sys/ioctl.h>
     23 #include <sys/mbuf.h>
     24 #include <sys/socket.h>
     25 #include <sys/syslog.h>
     26 #include <sys/device.h>
     27 
     28 #include <net/if.h>
     29 #include <net/if_dl.h>
     30 #include <net/if_types.h>
     31 #include <net/netisr.h>
     32 
     33 #ifdef INET
     34 #include <netinet/in.h>
     35 #include <netinet/in_systm.h>
     36 #include <netinet/in_var.h>
     37 #include <netinet/ip.h>
     38 #include <netinet/if_ether.h>
     39 #endif
     40 
     41 #ifdef NS
     42 #include <netns/ns.h>
     43 #include <netns/ns_if.h>
     44 #endif
     45 
     46 #if NBPFILTER > 0
     47 #include <net/bpf.h>
     48 #include <net/bpfdesc.h>
     49 #endif
     50 
     51 #include <machine/autoconf.h>
     52 #include <machine/cpu.h>
     53 
     54 /* #define	LEDEBUG	1 */
     55 
     56 #include "if_lereg.h"
     57 #include "if_le.h"
     58 #include "if_le_subr.h"
     59 
     60 #define	ETHER_MIN_LEN	64
     61 #define	ETHER_MAX_LEN	1518
     62 
     63 /*
     64  * The lance has only 24 address lines.  When it accesses memory,
     65  * the high address lines are hard-wired to 0xFF, so we must:
     66  * (1) put what we want the LANCE to see above 0xFF000000, and
     67  * (2) mask our CPU addresses down to 24 bits for the LANCE.
     68  */
     69 #define	LANCE_ADDR(sc,x)	((u_int)(x) & 0xFFffff)
     70 
     71 #ifdef PACKETSTATS
     72 long	lexpacketsizes[LEMTU+1];
     73 long	lerpacketsizes[LEMTU+1];
     74 #endif
     75 
     76 /* autoconfiguration driver */
     77 void	le_attach(struct device *, struct device *, void *);
     78 
     79 struct	cfdriver lecd = {
     80 	NULL, "le", le_md_match, le_attach,
     81 	DV_IFNET, sizeof(struct le_softc),
     82 };
     83 
     84 int leioctl __P((struct ifnet *, u_long, caddr_t));
     85 void lestart __P((struct ifnet *));
     86 void lewatchdog __P((/* short */));
     87 static inline void lewrcsr __P((/* struct le_softc *, u_short, u_short */));
     88 static inline u_short lerdcsr __P((/* struct le_softc *, u_short */));
     89 void leinit __P((struct le_softc *));
     90 void lememinit __P((struct le_softc *));
     91 void lereset __P((struct le_softc *));
     92 void lestop __P((struct le_softc *));
     93 void letint __P((struct le_softc *));
     94 void lerint __P((struct le_softc *));
     95 void leread __P((struct le_softc *, u_char *, int));
     96 struct mbuf *leget __P((u_char *, int, struct ifnet *));
     97 void lesetladrf __P((struct arpcom *, u_long *));
     98 #ifdef LEDEBUG
     99 void recv_print __P((struct le_softc *, int));
    100 void xmit_print __P((struct le_softc *, int));
    101 #endif
    102 
    103 /*
    104  * Inline routines to read and write the LANCE registers.
    105  */
    106 
    107 static inline void
    108 lewrcsr(sc, regnum, value)
    109 	struct le_softc *sc;
    110 	u_short regnum;
    111 	u_short value;
    112 {
    113 	volatile struct le_regs *regs = sc->sc_regs;
    114 
    115 	regs->lereg_addr = regnum;
    116 	regs->lereg_data = value;
    117 }
    118 
    119 static inline u_short
    120 lerdcsr(sc, regnum)
    121 	struct le_softc *sc;
    122 	u_short regnum;
    123 {
    124 	volatile struct le_regs *regs = sc->sc_regs;
    125 	u_short value;
    126 
    127 	regs->lereg_addr = regnum;
    128 	value = regs->lereg_data;
    129 
    130 	return (value);
    131 }
    132 
    133 /*
    134  * The probe is done in if_le_subr.c:if_md_match()
    135  */
    136 
    137 /*
    138  * Interface exists: make available by filling in network interface
    139  * record.  System will initialize the interface when it is ready
    140  * to accept packets.  We get the ethernet address here.
    141  */
    142 void
    143 le_attach(parent, self, aux)
    144 	struct device *parent, *self;
    145 	void *aux;
    146 {
    147 	struct le_softc *sc = (void *)self;
    148 	struct confargs *ca = aux;
    149 	struct ifnet *ifp = &sc->sc_if;
    150 	int pri;
    151 	u_int a;
    152 
    153 	le_md_attach(parent, self, aux);
    154 	printf(" hwaddr %s\n", ether_sprintf(sc->sc_enaddr));
    155 
    156 	/*
    157 	 * Initialize and attach S/W interface
    158 	 */
    159 	ifp->if_unit = sc->sc_dev.dv_unit;
    160 	ifp->if_name = lecd.cd_name;
    161 	ifp->if_start = lestart;
    162 	ifp->if_ioctl = leioctl;
    163 	ifp->if_watchdog = lewatchdog;
    164 	ifp->if_flags =
    165 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    166 
    167 	/* Attach the interface. */
    168 	if_attach(ifp);
    169 	ether_ifattach(ifp);
    170 #if NBPFILTER > 0
    171 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
    172 #endif
    173 }
    174 
    175 void
    176 lereset(sc)
    177 	struct le_softc *sc;
    178 {
    179 
    180 	leinit(sc);
    181 }
    182 
    183 void
    184 lewatchdog(unit)
    185 	short unit;
    186 {
    187 	struct le_softc *sc = lecd.cd_devs[unit];
    188 
    189 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
    190 	++sc->sc_if.if_oerrors;
    191 	lereset(sc);
    192 }
    193 
    194 /* LANCE initialization block set up. */
    195 void
    196 lememinit(sc)
    197 	register struct le_softc *sc;
    198 {
    199 	struct ifnet *ifp = &sc->sc_if;
    200 	int i;
    201 	void *mem;
    202 	u_long a;
    203 
    204 	/*
    205 	 * At this point we assume that the memory allocated to the Lance is
    206 	 * quadword aligned.  If it isn't then the initialisation is going
    207 	 * fail later on.
    208 	 */
    209 	mem = sc->sc_mem;
    210 
    211 	sc->sc_init = mem;
    212 #if NBPFILTER > 0
    213 	if (ifp->if_flags & IFF_PROMISC)
    214 		sc->sc_init->mode = LE_NORMAL | LE_PROM;
    215 	else
    216 #endif
    217 		sc->sc_init->mode = LE_NORMAL;
    218 
    219 	/* Set the Ethernet address (have to byte-swap) */
    220 	for (i = 0; i < 6; i += 2) {
    221 		sc->sc_init->padr[i] = sc->sc_enaddr[i+1];
    222 		sc->sc_init->padr[i+1] = sc->sc_enaddr[i];
    223 	}
    224 	lesetladrf(&sc->sc_ac, sc->sc_init->ladrf);
    225 	mem += sizeof(struct init_block);
    226 
    227 	sc->sc_rd = mem;
    228 	a = LANCE_ADDR(sc, mem);
    229 	sc->sc_init->rdra = a;
    230 	sc->sc_init->rlen = ((a >> 16) & 0xff) | (RLEN << 13);
    231 	mem += NRBUF * sizeof(struct mds);
    232 
    233 	sc->sc_td = mem;
    234 	a = LANCE_ADDR(sc, mem);
    235 	sc->sc_init->tdra = a;
    236 	sc->sc_init->tlen = ((a >> 16) & 0xff) | (TLEN << 13);
    237 	mem += NTBUF * sizeof(struct mds);
    238 
    239 	/*
    240 	 * Set up receive ring descriptors.
    241 	 */
    242 	sc->sc_rbuf = mem;
    243 	for (i = 0; i < NRBUF; i++) {
    244 		a = LANCE_ADDR(sc, mem);
    245 		sc->sc_rd[i].addr = a;
    246 		sc->sc_rd[i].flags = ((a >> 16) & 0xff) | LE_OWN;
    247 		sc->sc_rd[i].bcnt = -BUFSIZE;
    248 		sc->sc_rd[i].mcnt = 0;
    249 		mem += BUFSIZE;
    250 	}
    251 
    252 	/*
    253 	 * Set up transmit ring descriptors.
    254 	 */
    255 	sc->sc_tbuf = mem;
    256 	for (i = 0; i < NTBUF; i++) {
    257 		a = LANCE_ADDR(sc, mem);
    258 		sc->sc_td[i].addr = a;
    259 		sc->sc_td[i].flags= ((a >> 16) & 0xff);
    260 		sc->sc_td[i].bcnt = 0xf000;
    261 		sc->sc_td[i].mcnt = 0;
    262 		mem += BUFSIZE;
    263 	}
    264 
    265 #ifdef	DIAGNOSTIC
    266 	if (mem > (sc->sc_mem + MEMSIZE))
    267 		panic("lememinit: used 0x%x\n", mem - sc->sc_mem);
    268 #endif
    269 }
    270 
    271 void
    272 lestop(sc)
    273 	struct le_softc *sc;
    274 {
    275 
    276 	lewrcsr(sc, 0, LE_STOP);
    277 }
    278 
    279 /*
    280  * Initialization of interface; set up initialization block
    281  * and transmit/receive descriptor rings.
    282  */
    283 void
    284 leinit(sc)
    285 	register struct le_softc *sc;
    286 {
    287 	struct ifnet *ifp = &sc->sc_if;
    288 	int s;
    289 	register int timo;
    290 	u_long a;
    291 
    292 	/* Address not known. */
    293 	if (!ifp->if_addrlist)
    294 		return;
    295 
    296 	s = splimp();
    297 
    298 	/* Don't want to get in a weird state. */
    299 	lewrcsr(sc, 0, LE_STOP);
    300 	delay(100);
    301 
    302 	sc->sc_last_rd = sc->sc_last_td = sc->sc_no_td = 0;
    303 
    304 	/* Set up LANCE init block. */
    305 	lememinit(sc);
    306 
    307 	/* Set byte swapping etc. */
    308 	lewrcsr(sc, 3, LE_CONF3);
    309 
    310 	/* Give LANCE the physical address of its init block. */
    311 	a = LANCE_ADDR(sc, sc->sc_init);
    312 	lewrcsr(sc, 1, a);
    313 	lewrcsr(sc, 2, (a >> 16) & 0xff);
    314 
    315 	/* Try to initialize the LANCE. */
    316 	delay(100);
    317 	lewrcsr(sc, 0, LE_INIT);
    318 
    319 	/* Wait for initialization to finish. */
    320 	for (timo = 1000; timo; timo--)
    321 		if (lerdcsr(sc, 0) & LE_IDON)
    322 			break;
    323 
    324 	if (lerdcsr(sc, 0) & LE_IDON) {
    325 		/* Start the LANCE. */
    326 		lewrcsr(sc, 0, LE_INEA | LE_STRT | LE_IDON);
    327 		ifp->if_flags |= IFF_RUNNING;
    328 		ifp->if_flags &= ~IFF_OACTIVE;
    329 		lestart(ifp);
    330 	} else
    331 		printf("%s: card failed to initialize\n", sc->sc_dev.dv_xname);
    332 
    333 	(void) splx(s);
    334 }
    335 
    336 /*
    337  * Controller interrupt.
    338  */
    339 int
    340 leintr(vsc)
    341 	void *vsc;
    342 {
    343 	register struct le_softc *sc = vsc;
    344 	register u_short isr;
    345 
    346 	isr = lerdcsr(sc, 0);
    347 #ifdef LEDEBUG
    348 	if (sc->sc_debug)
    349 		printf("%s: leintr entering with isr=%04x\n",
    350 		    sc->sc_dev.dv_xname, isr);
    351 #endif
    352 	if ((isr & LE_INTR) == 0)
    353 		return 0;
    354 
    355 	do {
    356 		lewrcsr(sc, 0,
    357 		    isr & (LE_INEA | LE_BABL | LE_MISS | LE_MERR |
    358 			   LE_RINT | LE_TINT | LE_IDON));
    359 		if (isr & (LE_BABL | LE_CERR | LE_MISS | LE_MERR)) {
    360 			if (isr & LE_BABL) {
    361 				printf("%s: babble\n", sc->sc_dev.dv_xname);
    362 				sc->sc_if.if_oerrors++;
    363 			}
    364 #if 0
    365 			if (isr & LE_CERR) {
    366 				printf("%s: collision error\n", sc->sc_dev.dv_xname);
    367 				sc->sc_if.if_collisions++;
    368 			}
    369 #endif
    370 			if (isr & LE_MISS) {
    371 #if 0
    372 				printf("%s: missed packet\n", sc->sc_dev.dv_xname);
    373 #endif
    374 				sc->sc_if.if_ierrors++;
    375 			}
    376 			if (isr & LE_MERR) {
    377 				printf("%s: memory error\n", sc->sc_dev.dv_xname);
    378 				lereset(sc);
    379 				goto out;
    380 			}
    381 		}
    382 
    383 		if ((isr & LE_RXON) == 0) {
    384 			printf("%s: receiver disabled\n", sc->sc_dev.dv_xname);
    385 			sc->sc_if.if_ierrors++;
    386 			lereset(sc);
    387 			goto out;
    388 		}
    389 		if ((isr & LE_TXON) == 0) {
    390 			printf("%s: transmitter disabled\n", sc->sc_dev.dv_xname);
    391 			sc->sc_if.if_oerrors++;
    392 			lereset(sc);
    393 			goto out;
    394 		}
    395 
    396 		if (isr & LE_RINT) {
    397 			/* Reset watchdog timer. */
    398 			sc->sc_if.if_timer = 0;
    399 			lerint(sc);
    400 		}
    401 		if (isr & LE_TINT) {
    402 			/* Reset watchdog timer. */
    403 			sc->sc_if.if_timer = 0;
    404 			letint(sc);
    405 		}
    406 
    407 		isr = lerdcsr(sc, 0);
    408 	} while ((isr & LE_INTR) != 0);
    409 
    410 #ifdef LEDEBUG
    411 	if (sc->sc_debug)
    412 		printf("%s: leintr returning with isr=%04x\n",
    413 		    sc->sc_dev.dv_xname, isr);
    414 #endif
    415 
    416 out:
    417 	return 1;
    418 }
    419 
    420 #define NEXTTDS \
    421 	if (++tmd == NTBUF) tmd=0, cdm=sc->sc_td; else ++cdm
    422 
    423 /*
    424  * Setup output on interface.
    425  * Get another datagram to send off of the interface queue, and map it to the
    426  * interface before starting the output.
    427  * Called only at splimp or interrupt level.
    428  */
    429 void
    430 lestart(ifp)
    431 	struct ifnet *ifp;
    432 {
    433 	register struct le_softc *sc = lecd.cd_devs[ifp->if_unit];
    434 	register int tmd;
    435 	volatile struct mds *cdm;
    436 	struct mbuf *m0, *m;
    437 	u_char *buffer;
    438 	int len;
    439 
    440 	if ((sc->sc_if.if_flags & (IFF_RUNNING | IFF_OACTIVE)) !=
    441 	    IFF_RUNNING)
    442 		return;
    443 
    444 	tmd = sc->sc_last_td;
    445 	cdm = &sc->sc_td[tmd];
    446 
    447 	for (;;) {
    448 		if (sc->sc_no_td >= NTBUF) {
    449 			sc->sc_if.if_flags |= IFF_OACTIVE;
    450 #ifdef LEDEBUG
    451 			if (sc->sc_debug)
    452 				printf("no_td = %d, last_td = %d\n", sc->sc_no_td,
    453 				    sc->sc_last_td);
    454 #endif
    455 			break;
    456 		}
    457 
    458 #ifdef LEDEBUG
    459 		if (cdm->flags & LE_OWN) {
    460 			sc->sc_if.if_flags |= IFF_OACTIVE;
    461 			printf("missing buffer, no_td = %d, last_td = %d\n",
    462 			    sc->sc_no_td, sc->sc_last_td);
    463 		}
    464 #endif
    465 
    466 		IF_DEQUEUE(&sc->sc_if.if_snd, m);
    467 		if (!m)
    468 			break;
    469 
    470 		++sc->sc_no_td;
    471 
    472 		/*
    473 		 * Copy the mbuf chain into the transmit buffer.
    474 		 */
    475 		buffer = sc->sc_tbuf + (BUFSIZE * sc->sc_last_td);
    476 		len = 0;
    477 		for (m0 = m; m; m = m->m_next) {
    478 			bcopy(mtod(m, caddr_t), buffer, m->m_len);
    479 			buffer += m->m_len;
    480 			len += m->m_len;
    481 		}
    482 
    483 #ifdef LEDEBUG
    484 		if (len > ETHER_MAX_LEN)
    485 			printf("packet length %d\n", len);
    486 #endif
    487 
    488 #if NBPFILTER > 0
    489 		if (sc->sc_if.if_bpf)
    490 			bpf_mtap(sc->sc_if.if_bpf, m0);
    491 #endif
    492 
    493 		m_freem(m0);
    494 		len = max(len, ETHER_MIN_LEN);
    495 
    496 		/*
    497 		 * Init transmit registers, and set transmit start flag.
    498 		 */
    499 		cdm->bcnt = -len;
    500 		cdm->mcnt = 0;
    501 		cdm->flags |= LE_OWN | LE_STP | LE_ENP;
    502 
    503 #ifdef LEDEBUG
    504 		if (sc->sc_debug)
    505 			xmit_print(sc, sc->sc_last_td);
    506 #endif
    507 
    508 		lewrcsr(sc, 0, LE_INEA | LE_TDMD);
    509 
    510 		NEXTTDS;
    511 	}
    512 
    513 	sc->sc_last_td = tmd;
    514 }
    515 
    516 void
    517 letint(sc)
    518 	struct le_softc *sc;
    519 {
    520 	register int tmd = (sc->sc_last_td - sc->sc_no_td + NTBUF) % NTBUF;
    521 	volatile struct mds *cdm;
    522 
    523 	cdm = &sc->sc_td[tmd];
    524 	if (cdm->flags & LE_OWN) {
    525 		/* Race condition with loop below. */
    526 #ifdef LEDEBUG
    527 		if (sc->sc_debug)
    528 			printf("%s: extra tint\n", sc->sc_dev.dv_xname);
    529 #endif
    530 		return;
    531 	}
    532 
    533 	sc->sc_if.if_flags &= ~IFF_OACTIVE;
    534 
    535 	do {
    536 		if (sc->sc_no_td <= 0)
    537 			break;
    538 #ifdef LEDEBUG
    539 		if (sc->sc_debug)
    540 			printf("trans cdm = %x\n", cdm);
    541 #endif
    542 		sc->sc_if.if_opackets++;
    543 		--sc->sc_no_td;
    544 		if (cdm->mcnt & (LE_TBUFF | LE_UFLO | LE_LCOL | LE_LCAR | LE_RTRY)) {
    545 			if (cdm->mcnt & LE_TBUFF)
    546 				printf("%s: transmit buffer error\n", sc->sc_dev.dv_xname);
    547 			if ((cdm->mcnt & (LE_TBUFF | LE_UFLO)) == LE_UFLO)
    548 				printf("%s: underflow\n", sc->sc_dev.dv_xname);
    549 			if (cdm->mcnt & LE_UFLO) {
    550 				lereset(sc);
    551 				return;
    552 			}
    553 #if 0
    554 			if (cdm->mcnt & LE_LCOL) {
    555 				printf("%s: late collision\n", sc->sc_dev.dv_xname);
    556 				sc->sc_if.if_collisions++;
    557 			}
    558 			if (cdm->mcnt & LE_LCAR)
    559 				printf("%s: lost carrier\n", sc->sc_dev.dv_xname);
    560 			if (cdm->mcnt & LE_RTRY) {
    561 				printf("%s: excessive collisions, tdr %d\n",
    562 				    sc->sc_dev.dv_xname, cdm->flags & 0x1ff);
    563 				sc->sc_if.if_collisions += 16;
    564 			}
    565 #endif
    566 		} else if (cdm->flags & LE_ONE)
    567 			sc->sc_if.if_collisions++;
    568 		else if (cdm->flags & LE_MORE)
    569 			/* Real number is unknown. */
    570 			sc->sc_if.if_collisions += 2;
    571 		NEXTTDS;
    572 	} while ((cdm->flags & LE_OWN) == 0);
    573 
    574 	lestart(&sc->sc_if);
    575 }
    576 
    577 #define NEXTRDS \
    578 	if (++rmd == NRBUF) rmd=0, cdm=sc->sc_rd; else ++cdm
    579 
    580 /* only called from one place, so may as well integrate */
    581 void
    582 lerint(sc)
    583 	struct le_softc *sc;
    584 {
    585 	register int rmd = sc->sc_last_rd;
    586 	volatile struct mds *cdm;
    587 
    588 	cdm = &sc->sc_rd[rmd];
    589 	if (cdm->flags & LE_OWN) {
    590 		/* Race condition with loop below. */
    591 #ifdef LEDEBUG
    592 		if (sc->sc_debug)
    593 			printf("%s: extra rint\n", sc->sc_dev.dv_xname);
    594 #endif
    595 		return;
    596 	}
    597 
    598 	/* Process all buffers with valid data. */
    599 	do {
    600 		if (cdm->flags & (LE_FRAM | LE_OFLO | LE_CRC | LE_RBUFF)) {
    601 			if ((cdm->flags & (LE_FRAM | LE_OFLO | LE_ENP)) == (LE_FRAM | LE_ENP))
    602 				printf("%s: framing error\n", sc->sc_dev.dv_xname);
    603 			if ((cdm->flags & (LE_OFLO | LE_ENP)) == LE_OFLO)
    604 				printf("%s: overflow\n", sc->sc_dev.dv_xname);
    605 			if ((cdm->flags & (LE_CRC | LE_OFLO | LE_ENP)) == (LE_CRC | LE_ENP))
    606 				printf("%s: crc mismatch\n", sc->sc_dev.dv_xname);
    607 			if (cdm->flags & LE_RBUFF)
    608 				printf("%s: receive buffer error\n", sc->sc_dev.dv_xname);
    609 		} else if (cdm->flags & (LE_STP | LE_ENP) != (LE_STP | LE_ENP)) {
    610 			do {
    611 				cdm->mcnt = 0;
    612 				cdm->flags |= LE_OWN;
    613 				NEXTRDS;
    614 			} while ((cdm->flags & (LE_OWN | LE_ERR | LE_STP | LE_ENP)) == 0);
    615 			sc->sc_last_rd = rmd;
    616 			printf("%s: chained buffer\n", sc->sc_dev.dv_xname);
    617 			if ((cdm->flags & (LE_OWN | LE_ERR | LE_STP | LE_ENP)) != LE_ENP) {
    618 				lereset(sc);
    619 				return;
    620 			}
    621 		} else {
    622 #ifdef LEDEBUG
    623 			if (sc->sc_debug)
    624 				recv_print(sc, sc->sc_last_rd);
    625 #endif
    626 			leread(sc, sc->sc_rbuf + (BUFSIZE * rmd),
    627 			    (int)cdm->mcnt);
    628 			sc->sc_if.if_ipackets++;
    629 		}
    630 
    631 		cdm->bcnt = -BUFSIZE;
    632 		cdm->mcnt = 0;
    633 		cdm->flags |= LE_OWN;
    634 		NEXTRDS;
    635 #ifdef LEDEBUG
    636 		if (sc->sc_debug)
    637 			printf("sc->sc_last_rd = %x, cdm = %x\n",
    638 			    sc->sc_last_rd, cdm);
    639 #endif
    640 	} while ((cdm->flags & LE_OWN) == 0);
    641 
    642 	sc->sc_last_rd = rmd;
    643 }
    644 
    645 /*
    646  * Pass a packet to the higher levels.
    647  */
    648 void
    649 leread(sc, buf, len)
    650 	register struct le_softc *sc;
    651 	u_char *buf;
    652 	int len;
    653 {
    654 	struct ifnet *ifp;
    655 	struct mbuf *m;
    656 	struct ether_header *eh;
    657 
    658 	len -= 4;
    659 	if (len <= 0)
    660 		return;
    661 
    662 	/* Pull packet off interface. */
    663 	ifp = &sc->sc_if;
    664 	m = leget(buf, len, ifp);
    665 	if (m == 0)
    666 		return;
    667 
    668 	/* We assume that the header fit entirely in one mbuf. */
    669 	eh = mtod(m, struct ether_header *);
    670 
    671 #if NBPFILTER > 0
    672 	/*
    673 	 * Check if there's a BPF listener on this interface.
    674 	 * If so, hand off the raw packet to BPF.
    675 	 */
    676 	if (ifp->if_bpf) {
    677 		bpf_mtap(ifp->if_bpf, m);
    678 
    679 		/*
    680 		 * Note that the interface cannot be in promiscuous mode if
    681 		 * there are no BPF listeners.  And if we are in promiscuous
    682 		 * mode, we have to check if this packet is really ours.
    683 		 */
    684 		if ((ifp->if_flags & IFF_PROMISC) &&
    685 		    (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
    686 		    bcmp(eh->ether_dhost, sc->sc_enaddr,
    687 			    sizeof(eh->ether_dhost)) != 0) {
    688 			m_freem(m);
    689 			return;
    690 		}
    691 	}
    692 #endif
    693 
    694 	/* We assume that the header fit entirely in one mbuf. */
    695 	m->m_pkthdr.len -= sizeof(*eh);
    696 	m->m_len -= sizeof(*eh);
    697 	m->m_data += sizeof(*eh);
    698 
    699 	ether_input(ifp, eh, m);
    700 }
    701 
    702 /*
    703  * Supporting routines
    704  */
    705 
    706 /*
    707  * Pull data off an interface.
    708  * Len is length of data, with local net header stripped.
    709  * We copy the data into mbufs.  When full cluster sized units are present
    710  * we copy into clusters.
    711  */
    712 struct mbuf *
    713 leget(buf, totlen, ifp)
    714 	u_char *buf;
    715 	int totlen;
    716 	struct ifnet *ifp;
    717 {
    718 	struct mbuf *top, **mp, *m;
    719 	int len;
    720 
    721 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    722 	if (m == 0)
    723 		return 0;
    724 	m->m_pkthdr.rcvif = ifp;
    725 	m->m_pkthdr.len = totlen;
    726 	len = MHLEN;
    727 	top = 0;
    728 	mp = &top;
    729 
    730 	while (totlen > 0) {
    731 		if (top) {
    732 			MGET(m, M_DONTWAIT, MT_DATA);
    733 			if (m == 0) {
    734 				m_freem(top);
    735 				return 0;
    736 			}
    737 			len = MLEN;
    738 		}
    739 		if (totlen >= MINCLSIZE) {
    740 			MCLGET(m, M_DONTWAIT);
    741 			if (m->m_flags & M_EXT)
    742 				len = MCLBYTES;
    743 		}
    744 		m->m_len = len = min(totlen, len);
    745 		bcopy((caddr_t)buf, mtod(m, caddr_t), len);
    746 		buf += len;
    747 		totlen -= len;
    748 		*mp = m;
    749 		mp = &m->m_next;
    750 	}
    751 
    752 	return top;
    753 }
    754 
    755 /*
    756  * Process an ioctl request.
    757  */
    758 int
    759 leioctl(ifp, cmd, data)
    760 	register struct ifnet *ifp;
    761 	u_long cmd;
    762 	caddr_t data;
    763 {
    764 	struct le_softc *sc = lecd.cd_devs[ifp->if_unit];
    765 	struct ifaddr *ifa = (struct ifaddr *)data;
    766 	struct ifreq *ifr = (struct ifreq *)data;
    767 	int s, error = 0;
    768 
    769 	s = splimp();
    770 
    771 	switch (cmd) {
    772 
    773 	case SIOCSIFADDR:
    774 		ifp->if_flags |= IFF_UP;
    775 
    776 		switch (ifa->ifa_addr->sa_family) {
    777 #ifdef INET
    778 		case AF_INET:
    779 			leinit(sc);
    780 			arp_ifinit(&sc->sc_ac, ifa);
    781 			break;
    782 #endif
    783 #ifdef NS
    784 		/* XXX - This code is probably wrong. */
    785 		case AF_NS:
    786 		    {
    787 			register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
    788 
    789 			if (ns_nullhost(*ina))
    790 				ina->x_host =
    791 				    *(union ns_host *)(sc->sc_enaddr);
    792 			else
    793 				bcopy(ina->x_host.c_host,
    794 				    sc->sc_enaddr,
    795 				    sizeof(sc->sc_enaddr));
    796 			/* Set new address. */
    797 			leinit(sc);
    798 			break;
    799 		    }
    800 #endif
    801 		default:
    802 			leinit(sc);
    803 			break;
    804 		}
    805 		break;
    806 
    807 	case SIOCSIFFLAGS:
    808 		/*
    809 		 * If interface is marked down and it is running, then stop it
    810 		 */
    811 		if ((ifp->if_flags & IFF_UP) == 0 &&
    812 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    813 			/*
    814 			 * If interface is marked down and it is running, then
    815 			 * stop it.
    816 			 */
    817 			lestop(sc);
    818 			ifp->if_flags &= ~IFF_RUNNING;
    819 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
    820 		    	   (ifp->if_flags & IFF_RUNNING) == 0) {
    821 			/*
    822 			 * If interface is marked up and it is stopped, then
    823 			 * start it.
    824 			 */
    825 			leinit(sc);
    826 		} else {
    827 			/*
    828 			 * Reset the interface to pick up changes in any other
    829 			 * flags that affect hardware registers.
    830 			 */
    831 			/*lestop(sc);*/
    832 			leinit(sc);
    833 		}
    834 #ifdef LEDEBUG
    835 		if (ifp->if_flags & IFF_DEBUG)
    836 			sc->sc_debug = 1;
    837 		else
    838 			sc->sc_debug = 0;
    839 #endif
    840 		break;
    841 
    842 	case SIOCADDMULTI:
    843 	case SIOCDELMULTI:
    844 		error = (cmd == SIOCADDMULTI) ?
    845 		    ether_addmulti(ifr, &sc->sc_ac):
    846 		    ether_delmulti(ifr, &sc->sc_ac);
    847 
    848 		if (error == ENETRESET) {
    849 			/*
    850 			 * Multicast list has changed; set the hardware filter
    851 			 * accordingly.
    852 			 */
    853 			leinit(sc);
    854 			error = 0;
    855 		}
    856 		break;
    857 
    858 	default:
    859 		error = EINVAL;
    860 	}
    861 	(void) splx(s);
    862 	return error;
    863 }
    864 
    865 #ifdef LEDEBUG
    866 void
    867 recv_print(sc, no)
    868 	struct le_softc *sc;
    869 	int no;
    870 {
    871 	struct mds *rmd;
    872 	int i, printed = 0;
    873 	u_short len;
    874 
    875 	rmd = &sc->sc_rd[no];
    876 	len = rmd->mcnt;
    877 	printf("%s: receive buffer %d, len = %d\n", sc->sc_dev.dv_xname, no,
    878 	    len);
    879 	printf("%s: status %x\n", sc->sc_dev.dv_xname, lerdcsr(sc, 0));
    880 	for (i = 0; i < len; i++) {
    881 		if (!printed) {
    882 			printed = 1;
    883 			printf("%s: data: ", sc->sc_dev.dv_xname);
    884 		}
    885 		printf("%x ", *(sc->sc_rbuf + (BUFSIZE*no) + i));
    886 	}
    887 	if (printed)
    888 		printf("\n");
    889 }
    890 
    891 void
    892 xmit_print(sc, no)
    893 	struct le_softc *sc;
    894 	int no;
    895 {
    896 	struct mds *rmd;
    897 	int i, printed=0;
    898 	u_short len;
    899 
    900 	rmd = &sc->sc_td[no];
    901 	len = -rmd->bcnt;
    902 	printf("%s: transmit buffer %d, len = %d\n", sc->sc_dev.dv_xname, no,
    903 	    len);
    904 	printf("%s: status %x\n", sc->sc_dev.dv_xname, lerdcsr(sc, 0));
    905 	printf("%s: addr %x, flags %x, bcnt %x, mcnt %x\n",
    906 	    sc->sc_dev.dv_xname, rmd->addr, rmd->flags, rmd->bcnt, rmd->mcnt);
    907 	for (i = 0; i < len; i++)  {
    908 		if (!printed) {
    909 			printed = 1;
    910 			printf("%s: data: ", sc->sc_dev.dv_xname);
    911 		}
    912 		printf("%x ", *(sc->sc_tbuf + (BUFSIZE*no) + i));
    913 	}
    914 	if (printed)
    915 		printf("\n");
    916 }
    917 #endif /* LEDEBUG */
    918 
    919 /*
    920  * Set up the logical address filter.
    921  */
    922 void
    923 lesetladrf(ac, af)
    924 	struct arpcom *ac;
    925 	u_long *af;
    926 {
    927 	struct ifnet *ifp = &ac->ac_if;
    928 	struct ether_multi *enm;
    929 	register u_char *cp, c;
    930 	register u_long crc;
    931 	register int i, len;
    932 	struct ether_multistep step;
    933 
    934 	/*
    935 	 * Set up multicast address filter by passing all multicast addresses
    936 	 * through a crc generator, and then using the high order 6 bits as an
    937 	 * index into the 64 bit logical address filter.  The high order bit
    938 	 * selects the word, while the rest of the bits select the bit within
    939 	 * the word.
    940 	 */
    941 
    942 	if (ifp->if_flags & IFF_PROMISC) {
    943 		ifp->if_flags |= IFF_ALLMULTI;
    944 		af[0] = af[1] = 0xffffffff;
    945 		return;
    946 	}
    947 
    948 	af[0] = af[1] = 0;
    949 	ETHER_FIRST_MULTI(step, ac, enm);
    950 	while (enm != NULL) {
    951 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi,
    952 		    sizeof(enm->enm_addrlo)) != 0) {
    953 			/*
    954 			 * We must listen to a range of multicast addresses.
    955 			 * For now, just accept all multicasts, rather than
    956 			 * trying to set only those filter bits needed to match
    957 			 * the range.  (At this time, the only use of address
    958 			 * ranges is for IP multicast routing, for which the
    959 			 * range is big enough to require all bits set.)
    960 			 */
    961 			ifp->if_flags |= IFF_ALLMULTI;
    962 			af[0] = af[1] = 0xffffffff;
    963 			return;
    964 		}
    965 
    966 		cp = enm->enm_addrlo;
    967 		crc = 0xffffffff;
    968 		for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
    969 			c = *cp++;
    970 			for (i = 8; --i >= 0;) {
    971 				if ((crc & 0x01) ^ (c & 0x01)) {
    972 					crc >>= 1;
    973 					crc ^= 0x6db88320 | 0x80000000;
    974 				} else
    975 					crc >>= 1;
    976 				c >>= 1;
    977 			}
    978 		}
    979 		/* Just want the 6 most significant bits. */
    980 		crc >>= 26;
    981 
    982 		/* Turn on the corresponding bit in the filter. */
    983 		af[crc >> 5] |= 1 << ((crc & 0x1f) ^ 0);
    984 
    985 		ETHER_NEXT_MULTI(step, enm);
    986 	}
    987 	ifp->if_flags &= ~IFF_ALLMULTI;
    988 }
    989