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