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