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