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