Home | History | Annotate | Line # | Download | only in dev
if_le.c revision 1.12
      1 /*	$NetBSD: if_le.c,v 1.12 1994/10/26 09:08:46 cgd Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1982, 1992, 1993
      5  *	The Regents of the University of California.  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  * from: Header: if_le.c,v 1.25 93/10/31 04:47:50 leres Locked
     36  *
     37  *	@(#)if_le.c	8.2 (Berkeley) 10/30/93
     38  */
     39 
     40 #include "bpfilter.h"
     41 
     42 /*
     43  * AMD 7990 LANCE
     44  */
     45 #include <sys/param.h>
     46 #include <sys/device.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 
     61 #if NBPFILTER > 0
     62 #include <sys/select.h>
     63 #include <net/bpf.h>
     64 #include <net/bpfdesc.h>
     65 #endif
     66 
     67 #ifdef INET
     68 #include <netinet/in.h>
     69 #include <netinet/in_systm.h>
     70 #include <netinet/in_var.h>
     71 #include <netinet/ip.h>
     72 #include <netinet/if_ether.h>
     73 #endif
     74 
     75 #ifdef NS
     76 #include <netns/ns.h>
     77 #include <netns/ns_if.h>
     78 #endif
     79 
     80 #ifdef APPLETALK
     81 #include <netddp/atalk.h>
     82 #endif
     83 
     84 #include <machine/autoconf.h>
     85 #include <machine/cpu.h>
     86 
     87 #include "if_lereg.h"
     88 #include "if_le.h"
     89 #include "if_le_subr.h"
     90 
     91 /*
     92  * The lance has only 24 address lines.  When it accesses memory,
     93  * the high address lines are hard-wired to 0xFF, so we must:
     94  * (1) put what we want the LANCE to see above 0xFF000000, and
     95  * (2) mask our CPU addresses down to 24 bits for the LANCE.
     96  */
     97 #define	LANCE_ADDR(x)	((u_int)(x) & 0xFFffff)
     98 #define ISQUADALIGN(a) (((a) & 0x3) == 0)
     99 
    100 /* console error messages */
    101 int	ledebug = 0;
    102 
    103 #ifdef PACKETSTATS
    104 long	lexpacketsizes[LEMTU+1];
    105 long	lerpacketsizes[LEMTU+1];
    106 #endif
    107 
    108 /* autoconfiguration driver */
    109 void	leattach(struct device *, struct device *, void *);
    110 int 	le_md_match(struct device *, struct cfdata *, void *args);
    111 
    112 struct	cfdriver lecd = {
    113 	NULL, "le",
    114 	le_md_match, leattach,
    115 	DV_IFNET, sizeof(struct le_softc),
    116 };
    117 
    118 /* Forwards */
    119 void	lesetladrf(struct le_softc *);
    120 void	lereset(struct device *);
    121 int 	leinit(int);
    122 int 	lestart(struct ifnet *);
    123 int 	leintr(void *);
    124 void	lexint(struct le_softc *);
    125 void	lerint(struct le_softc *);
    126 void	leread(struct le_softc *, char *, int);
    127 int 	leput(char *, struct mbuf *);
    128 struct mbuf *leget(char *, int, int, struct ifnet *);
    129 int 	leioctl(struct ifnet *, int, caddr_t);
    130 void	leerror(struct le_softc *, int);
    131 void	lererror(struct le_softc *, char *);
    132 void	lexerror(struct le_softc *);
    133 int 	lewatchdog(int);	/* XXX */
    134 
    135 /*
    136  * Interface exists: make available by filling in network interface
    137  * record.  System will initialize the interface when it is ready
    138  * to accept packets.
    139  */
    140 void
    141 leattach(parent, self, args)
    142 	struct device *parent;
    143 	struct device *self;
    144 	void *args;
    145 {
    146 	struct le_softc *sc = (struct le_softc *)self;
    147 	volatile struct lereg2 *ler2;
    148 	struct ifnet *ifp = &sc->sc_if;
    149 	int pri;
    150 	u_int a;
    151 
    152 	le_md_attach(parent, self, args);
    153 	printf(": ether address %s\n", ether_sprintf(sc->sc_addr));
    154 
    155 	/*
    156 	 * Setup for transmit/receive
    157 	 *
    158 	 * According to Van, some versions of the Lance only use this
    159 	 * address to receive packets; it doesn't put them in
    160 	 * output packets. We'll want to make sure that lestart()
    161 	 * installs the address.
    162 	 */
    163 	ler2 = sc->sc_r2;
    164 	ler2->ler2_padr[0] = sc->sc_addr[1];
    165 	ler2->ler2_padr[1] = sc->sc_addr[0];
    166 	ler2->ler2_padr[2] = sc->sc_addr[3];
    167 	ler2->ler2_padr[3] = sc->sc_addr[2];
    168 	ler2->ler2_padr[4] = sc->sc_addr[5];
    169 	ler2->ler2_padr[5] = sc->sc_addr[4];
    170 	a = LANCE_ADDR(ler2->ler2_rmd);
    171 #ifdef	DIAGNOSTIC
    172 	if (!ISQUADALIGN(a))
    173 	    panic("rdra not quad aligned");
    174 #endif
    175 	ler2->ler2_rlen = LE_RLEN | (a >> 16);
    176 	ler2->ler2_rdra = a;
    177 	a = LANCE_ADDR(ler2->ler2_tmd);
    178 #ifdef	DIAGNOSTIC
    179 	if (!ISQUADALIGN(a))
    180 	    panic("tdra not quad aligned");
    181 #endif
    182 	ler2->ler2_tlen = LE_TLEN | (a >> 16);
    183 	ler2->ler2_tdra = a;
    184 
    185 	/*
    186 	 * Set up event counters.
    187 	 */
    188 	evcnt_attach(&sc->sc_dev, "intr", &sc->sc_intrcnt);
    189 	evcnt_attach(&sc->sc_dev, "errs", &sc->sc_errcnt);
    190 
    191 	ifp->if_unit = sc->sc_dev.dv_unit;
    192 	ifp->if_name = "le";
    193 	ifp->if_ioctl = leioctl;
    194 	ifp->if_output = ether_output;
    195 	ifp->if_start = lestart;
    196 	ifp->if_watchdog = lewatchdog;	/* XXX */
    197 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    198 #ifdef IFF_NOTRAILERS
    199 	/* XXX still compile when the blasted things are gone... */
    200 	ifp->if_flags |= IFF_NOTRAILERS;
    201 #endif
    202 #if NBPFILTER > 0
    203 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
    204 #endif
    205 	if_attach(ifp);
    206 	ether_ifattach(ifp);
    207 }
    208 
    209 /*
    210  * Setup the logical address filter
    211  */
    212 void
    213 lesetladrf(sc)
    214 	register struct le_softc *sc;
    215 {
    216 	register volatile struct lereg2 *ler2 = sc->sc_r2;
    217 	register struct ifnet *ifp = &sc->sc_if;
    218 	register struct ether_multi *enm;
    219 	register u_char *cp, c;
    220 	register u_long crc;
    221 	register int i, len;
    222 	struct ether_multistep step;
    223 
    224 	/*
    225 	 * Set up multicast address filter by passing all multicast
    226 	 * addresses through a crc generator, and then using the high
    227 	 * order 6 bits as a index into the 64 bit logical address
    228 	 * filter. The high order two bits select the word, while the
    229 	 * rest of the bits select the bit within the word.
    230 	 */
    231 
    232 	ler2->ler2_ladrf[0] = 0;
    233 	ler2->ler2_ladrf[1] = 0;
    234 	ler2->ler2_ladrf[2] = 0;
    235 	ler2->ler2_ladrf[3] = 0;
    236 	ifp->if_flags &= ~IFF_ALLMULTI;
    237 	ETHER_FIRST_MULTI(step, &sc->sc_ac, enm);
    238 	while (enm != NULL) {
    239 		if (bcmp((caddr_t)&enm->enm_addrlo,
    240 		    (caddr_t)&enm->enm_addrhi, sizeof(enm->enm_addrlo)) != 0) {
    241 			/*
    242 			 * We must listen to a range of multicast
    243 			 * addresses. For now, just accept all
    244 			 * multicasts, rather than trying to set only
    245 			 * those filter bits needed to match the range.
    246 			 * (At this time, the only use of address
    247 			 * ranges is for IP multicast routing, for
    248 			 * which the range is big enough to require all
    249 			 * bits set.)
    250 			 */
    251 			ler2->ler2_ladrf[0] = 0xffff;
    252 			ler2->ler2_ladrf[1] = 0xffff;
    253 			ler2->ler2_ladrf[2] = 0xffff;
    254 			ler2->ler2_ladrf[3] = 0xffff;
    255 			ifp->if_flags |= IFF_ALLMULTI;
    256 			return;
    257 		}
    258 
    259 		/*
    260 		 * One would think, given the AM7990 document's polynomial
    261 		 * of 0x04c11db6, that this should be 0x6db88320 (the bit
    262 		 * reversal of the AMD value), but that is not right.  See
    263 		 * the BASIC listing: bit 0 (our bit 31) must then be set.
    264 		 */
    265 		cp = (unsigned char *)&enm->enm_addrlo;
    266 		crc = 0xffffffff;
    267 		for (len = 6; --len >= 0;) {
    268 			c = *cp++;
    269 			for (i = 0; i < 8; i++) {
    270 				if ((c & 0x01) ^ (crc & 0x01)) {
    271 					crc >>= 1;
    272 					crc = crc ^ 0xedb88320;
    273 				} else
    274 					crc >>= 1;
    275 				c >>= 1;
    276 			}
    277 		}
    278 		/* Just want the 6 most significant bits. */
    279 		crc = crc >> 26;
    280 
    281 		/* Turn on the corresponding bit in the filter. */
    282 		ler2->ler2_ladrf[crc >> 4] |= 1 << (crc & 0xf);
    283 
    284 		ETHER_NEXT_MULTI(step, enm);
    285 	}
    286 }
    287 
    288 void
    289 lereset(dev)
    290 	struct device *dev;
    291 {
    292 	struct le_softc *sc = (struct le_softc *)dev;
    293 	volatile struct lereg1 *ler1 = sc->sc_r1;
    294 	volatile struct lereg2 *ler2 = sc->sc_r2;
    295 	int i, timo, stat;
    296 	u_int a;
    297 
    298 	if (ledebug)
    299 	    printf("%s: resetting, reg %x, ram %x\n",
    300 			   sc->sc_dev.dv_xname, sc->sc_r1, sc->sc_r2);
    301 
    302 #ifdef	DIAGNOSTIC
    303 	i = getsr();
    304 	if ((i & PSL_IPL) < PSL_IPL3)
    305 		panic("lereset at low ipl, sr=%x", i);
    306 #endif
    307 
    308 #if NBPFILTER > 0
    309 	if (sc->sc_if.if_flags & IFF_PROMISC)
    310 		ler2->ler2_mode = LE_MODE_NORMAL | LE_MODE_PROM;
    311 	else
    312 #endif
    313 		ler2->ler2_mode = LE_MODE_NORMAL;
    314 	ler1->ler1_rap = LE_CSR0;
    315 	ler1->ler1_rdp = LE_C0_STOP;
    316 
    317 	/* Setup the logical address filter */
    318 	lesetladrf(sc);
    319 
    320 	/* init receive and transmit rings */
    321 	for (i = 0; i < LERBUF; i++) {
    322 		a = LANCE_ADDR(&ler2->ler2_rbuf[i][0]);
    323 		ler2->ler2_rmd[i].rmd0 = a;
    324 		ler2->ler2_rmd[i].rmd1_hadr = a >> 16;
    325 		ler2->ler2_rmd[i].rmd1_bits = LE_R1_OWN;
    326 		ler2->ler2_rmd[i].rmd2 = -LEMTU | LE_XMD2_ONES;
    327 		ler2->ler2_rmd[i].rmd3 = 0;
    328 	}
    329 	for (i = 0; i < LETBUF; i++) {
    330 		a = LANCE_ADDR(&ler2->ler2_tbuf[i][0]);
    331 		ler2->ler2_tmd[i].tmd0 = a;
    332 		ler2->ler2_tmd[i].tmd1_hadr = a >> 16;
    333 		ler2->ler2_tmd[i].tmd1_bits = 0;
    334 		ler2->ler2_tmd[i].tmd2 = LE_XMD2_ONES;
    335 		ler2->ler2_tmd[i].tmd3 = 0;
    336 	}
    337 
    338 	bzero(&ler2->ler2_rbuf[0][0], (LERBUF + LETBUF) * LEMTU);
    339 
    340 	/* lance will stuff packet into receive buffer 0 next */
    341 	sc->sc_rmd = 0;
    342 
    343 	/*
    344 	 * Tell the chip where to find the initialization block.
    345 	 * Note that CSR1, CSR2, and CSR3 may only be accessed
    346 	 * while the STOP bit is set in CSR0.
    347 	 */
    348 	a = LANCE_ADDR(&ler2->ler2_mode);
    349 	ler1->ler1_rap = LE_CSR1;
    350 	ler1->ler1_rdp = a;
    351 	ler1->ler1_rap = LE_CSR2;
    352 	ler1->ler1_rdp = a >> 16;
    353 	ler1->ler1_rap = LE_CSR3;
    354 	ler1->ler1_rdp = LE_C3_CONFIG;
    355 	ler1->ler1_rap = LE_CSR0;
    356 	ler1->ler1_rdp = LE_C0_INIT;
    357 	timo = 10000;
    358 	while (((stat = ler1->ler1_rdp) & (LE_C0_ERR | LE_C0_IDON)) == 0) {
    359 		delay(100); 	/* XXX */
    360 		if (--timo == 0) {
    361 			printf("%s: init timeout, stat=%b\n",
    362 			    sc->sc_dev.dv_xname, stat, LE_C0_BITS);
    363 			break;
    364 		}
    365 	}
    366 	if (stat & LE_C0_ERR) {
    367 		printf("%s: init failed, stat=%b\n",
    368 		    sc->sc_dev.dv_xname, stat, LE_C0_BITS);
    369 		sc->sc_if.if_flags &= ~IFF_RUNNING; 	/* XXX */
    370 		return;
    371 	}
    372 	ler1->ler1_rdp = LE_C0_IDON;	/* clear IDON */
    373 	ler1->ler1_rdp = LE_C0_STRT | LE_C0_INEA;
    374 	sc->sc_if.if_flags &= ~IFF_OACTIVE;
    375 	delay(100);		/* XXX */
    376 }
    377 
    378 /*
    379  * Device timeout/watchdog routine.  Entered if the device neglects to
    380  * generate an interrupt after a transmit has been started on it.
    381  */
    382 int
    383 lewatchdog(unit)
    384 	int unit;
    385 {
    386 	struct le_softc *sc = lecd.cd_devs[unit];
    387 	struct ifnet *ifp = &sc->sc_if;
    388 	int s;
    389 
    390 	printf("%s: watchdog timeout\n", sc->sc_dev.dv_xname);
    391 	sc->sc_if.if_oerrors++;
    392 
    393 #ifdef	DIAGNOSTIC
    394 	s = getsr();
    395 	if ((s & PSL_IPL) > PSL_IPL3)
    396 		panic("lewatchdog would lower spl, sr=%x", s);
    397 #endif
    398 
    399 	s = splimp();	/* XXX - Can this lower the IPL? */
    400 	lereset(&sc->sc_dev);
    401 	lestart(&sc->sc_if);
    402 	splx(s);
    403 }
    404 
    405 /*
    406  * Initialization of interface
    407  */
    408 int
    409 leinit(unit)
    410 	int unit;
    411 {
    412 	struct le_softc *sc = lecd.cd_devs[unit];
    413 	struct ifnet *ifp = &sc->sc_if;
    414 	int s;
    415 
    416 	/* not yet, if address still unknown */
    417 	if (ifp->if_addrlist == (struct ifaddr *)0) {
    418 		if (ledebug)
    419 			printf("leinit: no address yet\n");
    420 		return (0);
    421 	}
    422 	if ((ifp->if_flags & IFF_RUNNING) == 0) {
    423 		s = splimp();
    424 		if (ledebug)
    425 		    printf("le: initializing unit %d, reg %x, ram %x\n",
    426 				   unit, sc->sc_r1, sc->sc_r2);
    427 		ifp->if_flags |= IFF_RUNNING;
    428 		lereset(&sc->sc_dev);
    429 		lestart(ifp);		/* XXX */
    430 		splx(s);
    431 	}
    432 	return (0);
    433 }
    434 
    435 /*
    436  * Start output on interface.  Get another datagram to send
    437  * off of the interface queue, and copy it to the interface
    438  * before starting the output.
    439  */
    440 int
    441 lestart(ifp)
    442 	register struct ifnet *ifp;
    443 {
    444 	register struct le_softc *sc = lecd.cd_devs[ifp->if_unit];
    445 	register volatile struct letmd *tmd;
    446 	register struct mbuf *m;
    447 	register int len;
    448 
    449 #ifdef	DIAGNOSTIC
    450 	int s = getsr();
    451 	if ((s & PSL_IPL) < PSL_IPL3)
    452 		panic("lestart at low ipl, sr=%x", s);
    453 #endif
    454 
    455 	if ((sc->sc_if.if_flags & IFF_RUNNING) == 0) {
    456 		if (ledebug)
    457 			printf("lestart: not running\n");
    458 		return (0);
    459 	}
    460 	IF_DEQUEUE(&sc->sc_if.if_snd, m);
    461 	if (m == 0) {
    462 		if (ledebug & 2)
    463 			printf("lestart: send queue empty\n");
    464 		return (0);
    465 	}
    466 	len = leput(sc->sc_r2->ler2_tbuf[0], m);
    467 #if NBPFILTER > 0
    468 	/*
    469 	 * If bpf is listening on this interface, let it
    470 	 * see the packet before we commit it to the wire.
    471 	 */
    472 	if (sc->sc_if.if_bpf)
    473 		bpf_tap(sc->sc_if.if_bpf, sc->sc_r2->ler2_tbuf[0], len);
    474 #endif
    475 
    476 #ifdef PACKETSTATS
    477 	if (len <= LEMTU)
    478 		lexpacketsizes[len]++;
    479 #endif
    480 	tmd = sc->sc_r2->ler2_tmd;
    481 	tmd->tmd3 = 0;
    482 	tmd->tmd2 = -len | LE_XMD2_ONES;
    483 	tmd->tmd1_bits = LE_T1_OWN | LE_T1_STP | LE_T1_ENP;
    484 	sc->sc_if.if_flags |= IFF_OACTIVE;
    485 
    486 	/* Set a timer just in case we never hear from the board again. */
    487 	ifp->if_timer = 2;
    488 
    489 	return (0);
    490 }
    491 
    492 int
    493 leintr(dev)
    494 	register void *dev;
    495 {
    496 	register struct le_softc *sc = dev;
    497 	register volatile struct lereg1 *ler1 = sc->sc_r1;
    498 	register int csr0;
    499 
    500 	csr0 = ler1->ler1_rdp;
    501 	if (ledebug & 2)
    502 	    printf("[%s: intr, stat %b]\n",
    503 			   sc->sc_dev.dv_xname, csr0, LE_C0_BITS);
    504 
    505 	if ((csr0 & LE_C0_INTR) == 0)
    506 		return (0);
    507 	sc->sc_intrcnt.ev_count++;
    508 
    509 	if (csr0 & LE_C0_ERR) {
    510 		sc->sc_errcnt.ev_count++;
    511 		leerror(sc, csr0);
    512 		if (csr0 & LE_C0_MERR) {
    513 			sc->sc_merr++;
    514 			lereset(&sc->sc_dev);
    515 			return (1);
    516 		}
    517 		if (csr0 & LE_C0_BABL)
    518 			sc->sc_babl++;
    519 		if (csr0 & LE_C0_CERR)
    520 			sc->sc_cerr++;
    521 		if (csr0 & LE_C0_MISS)
    522 			sc->sc_miss++;
    523 		ler1->ler1_rdp = LE_C0_BABL|LE_C0_CERR|LE_C0_MISS|LE_C0_INEA;
    524 	}
    525 	if ((csr0 & LE_C0_RXON) == 0) {
    526 		sc->sc_rxoff++;
    527 		lereset(&sc->sc_dev);
    528 		return (1);
    529 	}
    530 	if ((csr0 & LE_C0_TXON) == 0) {
    531 		sc->sc_txoff++;
    532 		lereset(&sc->sc_dev);
    533 		return (1);
    534 	}
    535 	if (csr0 & LE_C0_RINT) {
    536 		/* interrupt is cleared in lerint */
    537 		lerint(sc);
    538 	}
    539 	if (csr0 & LE_C0_TINT) {
    540 		ler1->ler1_rdp = LE_C0_TINT|LE_C0_INEA;
    541 		lexint(sc);
    542 	}
    543 	return (1);
    544 }
    545 
    546 /*
    547  * Ethernet interface transmitter interrupt.
    548  * Start another output if more data to send.
    549  */
    550 void
    551 lexint(sc)
    552 	register struct le_softc *sc;
    553 {
    554 	register volatile struct letmd *tmd = sc->sc_r2->ler2_tmd;
    555 
    556 	sc->sc_lestats.lexints++;
    557 	if ((sc->sc_if.if_flags & IFF_OACTIVE) == 0) {
    558 		sc->sc_xint++;
    559 		return;
    560 	}
    561 	if (tmd->tmd1_bits & LE_T1_OWN) {
    562 		sc->sc_xown++;
    563 		return;
    564 	}
    565 	if (tmd->tmd1_bits & LE_T1_ERR) {
    566 err:
    567 		lexerror(sc);
    568 		sc->sc_if.if_oerrors++;
    569 		if (tmd->tmd3 & (LE_T3_BUFF|LE_T3_UFLO)) {
    570 			sc->sc_uflo++;
    571 			lereset(&sc->sc_dev);
    572 		} else if (tmd->tmd3 & LE_T3_LCOL)
    573 			sc->sc_if.if_collisions++;
    574 		else if (tmd->tmd3 & LE_T3_RTRY)
    575 			sc->sc_if.if_collisions += 16;
    576 	}
    577 	else if (tmd->tmd3 & LE_T3_BUFF)
    578 		/* XXX documentation says BUFF not included in ERR */
    579 		goto err;
    580 	else if (tmd->tmd1_bits & LE_T1_ONE)
    581 		sc->sc_if.if_collisions++;
    582 	else if (tmd->tmd1_bits & LE_T1_MORE)
    583 		/* what is the real number? */
    584 		sc->sc_if.if_collisions += 2;
    585 	else
    586 		sc->sc_if.if_opackets++;
    587 	sc->sc_if.if_flags &= ~IFF_OACTIVE;
    588 	sc->sc_if.if_timer = 0;		/* XXX */
    589 	lestart(&sc->sc_if);
    590 }
    591 
    592 #define	LENEXTRMP \
    593 	if (++bix == LERBUF) bix = 0, rmd = sc->sc_r2->ler2_rmd; else ++rmd
    594 
    595 /*
    596  * Ethernet interface receiver interrupt.
    597  * If input error just drop packet.
    598  * Decapsulate packet based on type and pass to type specific
    599  * higher-level input routine.
    600  */
    601 void
    602 lerint(sc)
    603 	register struct le_softc *sc;
    604 {
    605 	register int bix = sc->sc_rmd;
    606 	register volatile struct lermd *rmd = &sc->sc_r2->ler2_rmd[bix];
    607 
    608 	sc->sc_lestats.lerints++;
    609 	/*
    610 	 * Out of sync with hardware, should never happen?
    611 	 */
    612 	if (rmd->rmd1_bits & LE_R1_OWN) {
    613 		do {
    614 			sc->sc_lestats.lerscans++;
    615 			LENEXTRMP;
    616 		} while ((rmd->rmd1_bits & LE_R1_OWN) && bix != sc->sc_rmd);
    617 		if (bix == sc->sc_rmd)
    618 			printf("%s: RINT with no buffer\n",
    619 			    sc->sc_dev.dv_xname);
    620 	} else
    621 		sc->sc_lestats.lerhits++;
    622 
    623 	/*
    624 	 * Process all buffers with valid data
    625 	 */
    626 	while ((rmd->rmd1_bits & LE_R1_OWN) == 0) {
    627 		int len = rmd->rmd3;
    628 
    629 		/* Clear interrupt to avoid race condition */
    630 		sc->sc_r1->ler1_rdp = LE_C0_RINT|LE_C0_INEA;
    631 
    632 		if (rmd->rmd1_bits & LE_R1_ERR) {
    633 			sc->sc_rmd = bix;
    634 			lererror(sc, "bad packet");
    635 			sc->sc_if.if_ierrors++;
    636 		} else if ((rmd->rmd1_bits & (LE_R1_STP|LE_R1_ENP)) !=
    637 		    (LE_R1_STP|LE_R1_ENP)) {
    638 			/* XXX make a define for LE_R1_STP|LE_R1_ENP? */
    639 			/*
    640 			 * Find the end of the packet so we can see how long
    641 			 * it was.  We still throw it away.
    642 			 */
    643 			do {
    644 				sc->sc_r1->ler1_rdp = LE_C0_RINT|LE_C0_INEA;
    645 				rmd->rmd3 = 0;
    646 				rmd->rmd1_bits = LE_R1_OWN;
    647 				LENEXTRMP;
    648 			} while (!(rmd->rmd1_bits &
    649 			    (LE_R1_OWN|LE_R1_ERR|LE_R1_STP|LE_R1_ENP)));
    650 			sc->sc_rmd = bix;
    651 			lererror(sc, "chained buffer");
    652 			sc->sc_rxlen++;
    653 			/*
    654 			 * If search terminated without successful completion
    655 			 * we reset the hardware (conservative).
    656 			 */
    657 			if ((rmd->rmd1_bits &
    658 			    (LE_R1_OWN|LE_R1_ERR|LE_R1_STP|LE_R1_ENP)) !=
    659 			    LE_R1_ENP) {
    660 				lereset(&sc->sc_dev);
    661 				return;
    662 			}
    663 		} else {
    664 			leread(sc, sc->sc_r2->ler2_rbuf[bix], len);
    665 #ifdef PACKETSTATS
    666 			lerpacketsizes[len]++;
    667 #endif
    668 			sc->sc_lestats.lerbufs++;
    669 		}
    670 		rmd->rmd3 = 0;
    671 		rmd->rmd1_bits = LE_R1_OWN;
    672 		LENEXTRMP;
    673 	}
    674 	sc->sc_rmd = bix;
    675 }
    676 
    677 void
    678 leread(sc, pkt, len)
    679 	register struct le_softc *sc;
    680 	char *pkt;
    681 	int len;
    682 {
    683 	register struct ether_header *et;
    684 	register struct ifnet *ifp = &sc->sc_if;
    685 	struct mbuf *m;
    686 	struct ifqueue *inq;
    687 	int flags;
    688 
    689 	ifp->if_ipackets++;
    690 	et = (struct ether_header *)pkt;
    691 	et->ether_type = ntohs((u_short)et->ether_type);
    692 	/* adjust input length to account for header and CRC */
    693 	len -= sizeof(struct ether_header) + 4;
    694 
    695 	if (len <= 0) {
    696 		if (ledebug)
    697 			log(LOG_WARNING,
    698 			    "%s: ierror(runt packet): from %s: len=%d\n",
    699 			    sc->sc_dev.dv_xname,
    700 			    ether_sprintf(et->ether_shost), len);
    701 		sc->sc_runt++;
    702 		ifp->if_ierrors++;
    703 		return;
    704 	}
    705 
    706 	/* Setup mbuf flags we'll need later */
    707 	flags = 0;
    708 	if (bcmp((caddr_t)etherbroadcastaddr,
    709 	    (caddr_t)et->ether_dhost, sizeof(etherbroadcastaddr)) == 0)
    710 		flags |= M_BCAST;
    711 	if (et->ether_dhost[0] & 1)
    712 		flags |= M_MCAST;
    713 
    714 #if NBPFILTER > 0
    715 	/*
    716 	 * Check if there's a bpf filter listening on this interface.
    717 	 * If so, hand off the raw packet to enet, then discard things
    718 	 * not destined for us (but be sure to keep broadcast/multicast).
    719 	 */
    720 	if (ifp->if_bpf) {
    721 		bpf_tap(ifp->if_bpf, pkt,
    722 		    len + sizeof(struct ether_header));
    723 		if ((flags & (M_BCAST | M_MCAST)) == 0 &&
    724 		    bcmp(et->ether_dhost, sc->sc_addr,
    725 			    sizeof(et->ether_dhost)) != 0)
    726 			return;
    727 	}
    728 #endif
    729 	m = leget(pkt, len, 0, ifp);
    730 	if (m == 0)
    731 		return;
    732 
    733 	ether_input(ifp, et, m);
    734 }
    735 
    736 /*
    737  * Routine to copy from mbuf chain to transmit
    738  * buffer in board local memory.
    739  *
    740  * ### this can be done by remapping in some cases
    741  */
    742 int
    743 leput(lebuf, m)
    744 	register char *lebuf;
    745 	register struct mbuf *m;
    746 {
    747 	register struct mbuf *mp;
    748 	register int len, tlen = 0;
    749 
    750 	for (mp = m; mp; mp = mp->m_next) {
    751 		len = mp->m_len;
    752 		if (len == 0)
    753 			continue;
    754 		tlen += len;
    755 		bcopy(mtod(mp, char *), lebuf, len);
    756 		lebuf += len;
    757 	}
    758 	m_freem(m);
    759 	if (tlen < LEMINSIZE) {
    760 		bzero(lebuf, LEMINSIZE - tlen);
    761 		tlen = LEMINSIZE;
    762 	}
    763 	return (tlen);
    764 }
    765 
    766 /*
    767  * Routine to copy from board local memory into mbufs.
    768  */
    769 struct mbuf *
    770 leget(lebuf, totlen, off0, ifp)
    771 	char *lebuf;
    772 	int totlen, off0;
    773 	struct ifnet *ifp;
    774 {
    775 	register struct mbuf *m;
    776 	struct mbuf *top = 0, **mp = &top;
    777 	register int off = off0, len;
    778 	register char *cp;
    779 	char *epkt;
    780 
    781 	lebuf += sizeof(struct ether_header);
    782 	cp = lebuf;
    783 	epkt = cp + totlen;
    784 	if (off) {
    785 		cp += off + 2 * sizeof(u_short);
    786 		totlen -= 2 * sizeof(u_short);
    787 	}
    788 
    789 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    790 	if (m == 0)
    791 		return (0);
    792 	m->m_pkthdr.rcvif = ifp;
    793 	m->m_pkthdr.len = totlen;
    794 	m->m_len = MHLEN;
    795 
    796 	while (totlen > 0) {
    797 		if (top) {
    798 			MGET(m, M_DONTWAIT, MT_DATA);
    799 			if (m == 0) {
    800 				m_freem(top);
    801 				return (0);
    802 			}
    803 			m->m_len = MLEN;
    804 		}
    805 		len = min(totlen, epkt - cp);
    806 		if (len >= MINCLSIZE) {
    807 			MCLGET(m, M_DONTWAIT);
    808 			if (m->m_flags & M_EXT)
    809 				m->m_len = len = min(len, MCLBYTES);
    810 			else
    811 				len = m->m_len;
    812 		} else {
    813 			/*
    814 			 * Place initial small packet/header at end of mbuf.
    815 			 */
    816 			if (len < m->m_len) {
    817 				if (top == 0 && len + max_linkhdr <= m->m_len)
    818 					m->m_data += max_linkhdr;
    819 				m->m_len = len;
    820 			} else
    821 				len = m->m_len;
    822 		}
    823 		bcopy(cp, mtod(m, caddr_t), (unsigned)len);
    824 		cp += len;
    825 		*mp = m;
    826 		mp = &m->m_next;
    827 		totlen -= len;
    828 		if (cp == epkt)
    829 			cp = lebuf;
    830 	}
    831 	return (top);
    832 }
    833 
    834 /*
    835  * Process an ioctl request.
    836  */
    837 int
    838 leioctl(ifp, cmd, data)
    839 	register struct ifnet *ifp;
    840 	int cmd;
    841 	caddr_t data;
    842 {
    843 	register struct ifaddr *ifa;
    844 	register struct le_softc *sc = lecd.cd_devs[ifp->if_unit];
    845 	register volatile struct lereg1 *ler1;
    846 	int s, error;
    847 
    848 	/* Make sure attach was called. */
    849 	if (sc->sc_r1 == NULL)
    850 		return (ENXIO);
    851 
    852 	error = 0;
    853 	s = splimp();
    854 	switch (cmd) {
    855 
    856 	case SIOCSIFADDR:
    857 		ifa = (struct ifaddr *)data;
    858 		ifp->if_flags |= IFF_UP;
    859 		switch (ifa->ifa_addr->sa_family) {
    860 #ifdef INET
    861 		case AF_INET:
    862 			/* before arpwhohas */
    863 		    if ((ifp->if_flags & IFF_RUNNING) == 0) 	/* XXX */
    864 				(void)leinit(ifp->if_unit);
    865 			((struct arpcom *)ifp)->ac_ipaddr =
    866 				IA_SIN(ifa)->sin_addr;
    867 			arpwhohas((struct arpcom *)ifp, &IA_SIN(ifa)->sin_addr);
    868 			break;
    869 #endif
    870 #ifdef NS
    871 		case AF_NS:
    872 		    {
    873 			register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
    874 
    875 			if (ns_nullhost(*ina))
    876 				ina->x_host = *(union ns_host *)(sc->sc_addr);
    877 			else {
    878 				/*
    879 				 * The manual says we can't change the address
    880 				 * while the receiver is armed,
    881 				 * so reset everything
    882 				 */
    883 				ifp->if_flags &= ~IFF_RUNNING;
    884 				bcopy((caddr_t)ina->x_host.c_host,
    885 				    (caddr_t)sc->sc_addr, sizeof(sc->sc_addr));
    886 			}
    887 			(void)leinit(ifp->if_unit);	/* does le_setaddr() */
    888 			break;
    889 		    }
    890 #endif
    891 		default:
    892 			(void)leinit(ifp->if_unit);
    893 			break;
    894 		}
    895 		break;
    896 
    897 	case SIOCSIFFLAGS:
    898 		ler1 = sc->sc_r1;
    899 		if ((ifp->if_flags & IFF_UP) == 0 &&
    900 		    ifp->if_flags & IFF_RUNNING) {
    901 			ler1->ler1_rdp = LE_C0_STOP;
    902 			ifp->if_flags &= ~IFF_RUNNING;
    903 		} else if (ifp->if_flags & IFF_UP &&
    904 		    (ifp->if_flags & IFF_RUNNING) == 0)
    905 			(void)leinit(ifp->if_unit);
    906 		/*
    907 		 * If the state of the promiscuous bit changes, the interface
    908 		 * must be reset to effect the change.
    909 		 */
    910 		if (((ifp->if_flags ^ sc->sc_iflags) & IFF_PROMISC) &&
    911 		    (ifp->if_flags & IFF_RUNNING)) {
    912 			sc->sc_iflags = ifp->if_flags;
    913 			lereset(&sc->sc_dev);
    914 			lestart(ifp);
    915 		}
    916 		break;
    917 
    918 	case SIOCADDMULTI:
    919 		error = ether_addmulti((struct ifreq *)data, &sc->sc_ac);
    920 		goto update_multicast;
    921 
    922 	case SIOCDELMULTI:
    923 		error = ether_delmulti((struct ifreq *)data, &sc->sc_ac);
    924 	update_multicast:
    925 		if (error == ENETRESET) {
    926 			/*
    927 			 * Multicast list has changed; set the hardware
    928 			 * filter accordingly.
    929 			 */
    930 			lereset(&sc->sc_dev);
    931 			lestart(ifp);			/* XXX */
    932 			error = 0;
    933 		}
    934 		break;
    935 
    936 	default:
    937 		error = EINVAL;
    938 	}
    939 	splx(s);
    940 	return (error);
    941 }
    942 
    943 void
    944 leerror(sc, stat)
    945 	register struct le_softc *sc;
    946 	int stat;
    947 {
    948 	if (!ledebug)
    949 		return;
    950 
    951 	/*
    952 	 * Not all transceivers implement heartbeat
    953 	 * so we only log CERR once.
    954 	 */
    955 	if ((stat & LE_C0_CERR) && sc->sc_cerr)
    956 		return;
    957 	log(LOG_WARNING, "%s: error: stat=%b\n",
    958 	    sc->sc_dev.dv_xname, stat, LE_C0_BITS);
    959 }
    960 
    961 void
    962 lererror(sc, msg)
    963 	register struct le_softc *sc;
    964 	char *msg;
    965 {
    966 	register volatile struct lermd *rmd;
    967 	int len;
    968 
    969 	if (!ledebug)
    970 		return;
    971 
    972 	rmd = &sc->sc_r2->ler2_rmd[sc->sc_rmd];
    973 	len = rmd->rmd3;
    974 	log(LOG_WARNING, "%s: ierror(%s): from %s: buf=%d, len=%d, rmd1=%b\n",
    975 	    sc->sc_dev.dv_xname, msg, len > 11 ?
    976 	    ether_sprintf((u_char *)&sc->sc_r2->ler2_rbuf[sc->sc_rmd][6]) :
    977 	    "unknown",
    978 	    sc->sc_rmd, len, rmd->rmd1_bits, LE_R1_BITS);
    979 }
    980 
    981 void
    982 lexerror(sc)
    983 	register struct le_softc *sc;
    984 {
    985 	register volatile struct letmd *tmd;
    986 	register int len, tmd3, tdr;
    987 
    988 	if (!ledebug)
    989 		return;
    990 
    991 	tmd = sc->sc_r2->ler2_tmd;
    992 	tmd3 = tmd->tmd3;
    993 	tdr = tmd3 & LE_T3_TDR_MASK;
    994 	len = -(tmd->tmd2 & ~LE_XMD2_ONES);
    995 	log(LOG_WARNING,
    996     "%s: oerror: to %s: buf=%d, len=%d, tmd1=%b, tmd3=%b, tdr=%d (%d nsecs)\n",
    997 	    sc->sc_dev.dv_xname, len > 5 ?
    998 	    ether_sprintf((u_char *)&sc->sc_r2->ler2_tbuf[0][0]) : "unknown",
    999 	    0, len,
   1000 	    tmd->tmd1_bits, LE_T1_BITS,
   1001 	    tmd3, LE_T3_BITS, tdr, tdr * 100);
   1002 }
   1003