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