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