Home | History | Annotate | Line # | Download | only in dev
if_le.c revision 1.14
      1 /*	$NetBSD: if_le.c,v 1.14 1995/07/02 00:16:06 mycroft Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1990 The Regents of the University of California.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  *	@(#)if_le.c	7.6 (Berkeley) 5/8/91
     36  */
     37 
     38 #include "le.h"
     39 #if NLE > 0
     40 
     41 #include "bpfilter.h"
     42 
     43 /*
     44  * AMD 7990 LANCE
     45  *
     46  * This driver will generate and accept tailer encapsulated packets even
     47  * though it buys us nothing.  The motivation was to avoid incompatibilities
     48  * with VAXen, SUNs, and others that handle and benefit from them.
     49  * This reasoning is dubious.
     50  */
     51 #include <sys/param.h>
     52 #include <sys/systm.h>
     53 #include <sys/mbuf.h>
     54 #include <sys/buf.h>
     55 #include <sys/protosw.h>
     56 #include <sys/socket.h>
     57 #include <sys/syslog.h>
     58 #include <sys/ioctl.h>
     59 #include <sys/errno.h>
     60 #include <sys/device.h>
     61 
     62 #include <net/if.h>
     63 #include <net/netisr.h>
     64 #include <net/route.h>
     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 #include <machine/cpu.h>
     80 #include <machine/mtpr.h>
     81 #include <amiga/amiga/device.h>
     82 #include <amiga/amiga/isr.h>
     83 #include <amiga/dev/zbusvar.h>
     84 #include <amiga/dev/if_lereg.h>
     85 
     86 /*
     87  * Ethernet software status per interface.
     88  *
     89  * Each interface is referenced by a network interface structure,
     90  * le_if, which the routing code uses to locate the interface.
     91  * This structure contains the output queue for the interface, its address, ...
     92  */
     93 struct	le_softc {
     94 	struct	isr sc_isr;
     95 	struct	arpcom sc_ac;	/* common Ethernet structures */
     96 #define	sc_if	sc_ac.ac_if	/* network-visible interface */
     97 #define	sc_addr	sc_ac.ac_enaddr	/* hardware Ethernet address */
     98 	void	*sc_base;	/* base address of board */
     99 	struct	lereg1 *sc_r1;	/* LANCE registers */
    100 	struct	lereg2 *sc_r2;	/* dual-port RAM */
    101 	int	sc_rmd;		/* predicted next rmd to process */
    102 	int	sc_tmd;		/* next tmd to use */
    103 	int	sc_no_td;	/* number of tmds in use */
    104 	int	sc_runt;
    105 	int	sc_jab;
    106 	int	sc_merr;
    107 	int	sc_babl;
    108 	int	sc_cerr;
    109 	int	sc_miss;
    110 	int	sc_xint;
    111 	int	sc_xown;
    112 	int	sc_uflo;
    113 	int	sc_rxlen;
    114 	int	sc_rxoff;
    115 	int	sc_txoff;
    116 	int	sc_busy;
    117 	short	sc_iflags;
    118 #if NBPFILTER > 0
    119 	caddr_t sc_bpf;
    120 #endif
    121 } le_softc[NLE];
    122 
    123 #if NBPFILTER > 0
    124 #include <net/bpf.h>
    125 #include <net/bpfdesc.h>
    126 #endif
    127 
    128 /* offsets for:	   ID,   REGS,    MEM */
    129 int	lestd[] = { 0, 0x4000, 0x8000 };
    130 
    131 /* console error messages */
    132 int	ledebug = 0;
    133 
    134 int leioctl __P((struct ifnet *, u_long, caddr_t));
    135 int leintr __P((struct le_softc *));
    136 void lestart __P((struct ifnet *));
    137 void leinit __P((int));
    138 
    139 struct	mbuf *leget();
    140 extern	struct ifnet loif;
    141 
    142 void leattach __P((struct device *, struct device *, void *));
    143 int lematch __P((struct device *, struct cfdata *, void *args));
    144 
    145 struct cfdriver lecd = {
    146 	NULL, "le", (cfmatch_t)lematch, leattach, DV_IFNET,
    147 	sizeof(struct le_softc), NULL, 0};
    148 
    149 int
    150 lematch(pdp, cfp, auxp)
    151 	struct device *pdp;
    152 	struct cfdata *cfp;
    153 	void *auxp;
    154 {
    155 
    156 	struct zbus_args *zap;
    157 
    158 	zap = (struct zbus_args *)auxp;
    159 
    160 	/* Commodore ethernet card */
    161 	if ( zap->manid == 514 && zap->prodid == 112)
    162 		return(1);
    163 
    164 	/* Ameristar ethernet card */
    165 	if ( zap->manid == 1053 && zap->prodid == 1)
    166 		return(1);
    167 
    168 	return (0);
    169 }
    170 
    171 /*
    172  * Interface exists: make available by filling in network interface
    173  * record.  System will initialize the interface when it is ready
    174  * to accept packets.
    175  */
    176 void
    177 leattach(pdp, dp, auxp)
    178 	struct device *pdp, *dp;
    179 	void *auxp;
    180 {
    181 	register struct lereg0 *ler0;
    182 	register struct lereg2 *ler2;
    183 	struct zbus_args *zap;
    184 	struct lereg2 *lemem = (struct lereg2 *) 0x8000;
    185 	struct le_softc *le = &le_softc[dp->dv_unit];
    186 	struct ifnet *ifp = &le->sc_if;
    187 	char *cp;
    188 	int i;
    189 	unsigned long ser;
    190 	int s = splhigh ();
    191 
    192 	zap =(struct zbus_args *)auxp;
    193 
    194 	/*
    195 	 * Make config msgs look nicer.
    196 	 */
    197 	printf("\n");
    198 
    199 	ler0 = le->sc_base = zap->va;
    200 	le->sc_r1 = (struct lereg1 *)(lestd[1] + (int)zap->va);
    201 	ler2 = le->sc_r2 = (struct lereg2 *)(lestd[2] + (int)zap->va);
    202 
    203 	/*
    204 	 * Manufacturer decides the 3 first bytes, i.e. ethernet vendor ID.
    205 	 */
    206 	if ( zap->manid == 514 && zap->prodid == 112) {
    207 	    /* Commodore 2065 */
    208 	    le->sc_addr[0] = 0x00;
    209 	    le->sc_addr[1] = 0x80;
    210 	    le->sc_addr[2] = 0x10;
    211 	}
    212 	if ( zap->manid == 1053 && zap->prodid == 1) {
    213 	    le->sc_addr[0] = 0x00;
    214 	    le->sc_addr[1] = 0x00;
    215 	    le->sc_addr[2] = 0x9f;
    216 	}
    217 
    218 	/*
    219 	 * Serial number for board is used as host ID.
    220 	 */
    221 	ser = (unsigned long) zap->serno;
    222 
    223 	le->sc_addr[3] = (ser >> 16) & 0xff;
    224 	le->sc_addr[4] = (ser >>  8) & 0xff;
    225 	le->sc_addr[5] = (ser      ) & 0xff;
    226 
    227 #ifdef LE_USE_16K
    228 	printf("le%d: hardware address %s 16K\n",
    229 #else
    230 	printf("le%d: hardware address %s 32K\n",
    231 #endif
    232 	dp->dv_unit, ether_sprintf(le->sc_addr));
    233 
    234 	/*
    235 	 * Setup for transmit/receive
    236 	 */
    237 	ler2->ler2_mode = LE_MODE;
    238 	ler2->ler2_padr[0] = le->sc_addr[1];
    239 	ler2->ler2_padr[1] = le->sc_addr[0];
    240 	ler2->ler2_padr[2] = le->sc_addr[3];
    241 	ler2->ler2_padr[3] = le->sc_addr[2];
    242 	ler2->ler2_padr[4] = le->sc_addr[5];
    243 	ler2->ler2_padr[5] = le->sc_addr[4];
    244 	ler2->ler2_ladrf0 = 0;
    245 	ler2->ler2_ladrf1 = 0;
    246 	ler2->ler2_rlen = LE_RLEN;
    247 	ler2->ler2_rdra = (int)lemem->ler2_rmd;
    248 	ler2->ler2_tlen = LE_TLEN;
    249 	ler2->ler2_tdra = (int)lemem->ler2_tmd;
    250 
    251 	splx (s);
    252 
    253 	ifp->if_unit = dp->dv_unit;
    254 	ifp->if_name = "le";
    255 	ifp->if_mtu = ETHERMTU;
    256 	ifp->if_ioctl = leioctl;
    257 	ifp->if_output = ether_output;
    258 	ifp->if_start = lestart;
    259 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX;
    260 
    261 #if NBPFILTER > 0
    262 	bpfattach(&le->sc_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
    263 #endif
    264 	if_attach(ifp);
    265 	ether_ifattach(ifp);
    266 
    267 	le->sc_isr.isr_intr = leintr;
    268 	le->sc_isr.isr_arg = le;
    269 	le->sc_isr.isr_ipl = 2;
    270 	add_isr (&le->sc_isr);
    271 
    272 	return;
    273 }
    274 
    275 ledrinit(ler2)
    276 	register struct lereg2 *ler2;
    277 {
    278 	register struct lereg2 *lemem = (struct lereg2 *) 0x8000;
    279 	register int i;
    280 
    281 	for (i = 0; i < LERBUF; i++) {
    282 		ler2->ler2_rmd[i].rmd0 = (int)lemem->ler2_rbuf[i];
    283 		ler2->ler2_rmd[i].rmd1 = LE_OWN;
    284 		ler2->ler2_rmd[i].rmd2 = -LEMTU;
    285 		ler2->ler2_rmd[i].rmd3 = 0;
    286 	}
    287 
    288 	for (i = 0; i < LETBUF; i++) {
    289 		ler2->ler2_tmd[i].tmd0 = (int)lemem->ler2_tbuf[i];
    290 		ler2->ler2_tmd[i].tmd1 = 0;
    291 		ler2->ler2_tmd[i].tmd2 = 0;
    292 		ler2->ler2_tmd[i].tmd3 = 0;
    293 	}
    294 }
    295 
    296 void
    297 lereset(unit)
    298 	register int unit;
    299 {
    300 	register struct le_softc *le = &le_softc[unit];
    301 	register struct lereg1 *ler1 = le->sc_r1;
    302 	/*
    303 	 * This structure is referenced from the CARDS/LANCE point of
    304 	 * view, thus the 0x8000 address which is the buffer RAM area of
    305 	 * the Commodore and Ameristar cards. This pointer is manipulated
    306 	 * with the LANCE's view of memory and NOT the Amiga's. FYI.
    307 	 */
    308 	register struct lereg2 *lemem = (struct lereg2 *) 0x8000;
    309 
    310 	register int timo = 100000;
    311 	register int stat;
    312 
    313 #ifdef lint
    314 	stat = unit;
    315 #endif
    316 #if NBPFILTER > 0
    317 	if (le->sc_if.if_flags & IFF_PROMISC)
    318 		/* set the promiscuous bit */
    319 		le->sc_r2->ler2_mode = LE_MODE|0x8000;
    320 	else
    321 		le->sc_r2->ler2_mode = LE_MODE;
    322 #endif
    323 	 ler1->ler1_rap =  LE_CSR0;
    324 	 ler1->ler1_rdp =  LE_STOP;
    325 
    326 	 ledrinit(le->sc_r2);
    327 
    328 	 le->sc_rmd = le->sc_tmd = le->sc_no_td = 0;
    329 	 ler1->ler1_rap =  LE_CSR1;
    330 	 ler1->ler1_rdp =  (int)&lemem->ler2_mode;
    331 	 ler1->ler1_rap =  LE_CSR2;
    332 	 ler1->ler1_rdp =  0;
    333 	 ler1->ler1_rap =  LE_CSR0;
    334 	 ler1->ler1_rdp =  LE_INIT;
    335 
    336 	do {
    337 		if (--timo == 0) {
    338 			printf("le%d: init timeout, stat = 0x%x\n",
    339 			       unit, stat);
    340 			break;
    341 		}
    342 		 stat =  ler1->ler1_rdp;
    343 	} while ((stat & LE_IDON) == 0);
    344 
    345 	 ler1->ler1_rdp =  LE_STOP;
    346 	 ler1->ler1_rap =  LE_CSR3;
    347 	 ler1->ler1_rdp =  LE_BSWP;
    348 	 ler1->ler1_rap =  LE_CSR0;
    349 	 ler1->ler1_rdp =  LE_STRT | LE_INEA;
    350 	 le->sc_if.if_flags &= ~IFF_OACTIVE;
    351 
    352 	return;
    353 }
    354 
    355 /*
    356  * Initialization of interface
    357  */
    358 void
    359 leinit(unit)
    360 	int unit;
    361 {
    362 	struct le_softc *le = &le_softc[unit];
    363 	register struct ifnet *ifp = &le->sc_if;
    364 	int s;
    365 
    366 	if ((ifp->if_flags & IFF_RUNNING) == 0) {
    367 		s = splimp();
    368 		ifp->if_flags |= IFF_RUNNING;
    369 		lereset(unit);
    370 	        (void) lestart(ifp);
    371 		splx(s);
    372 	}
    373 
    374 	return;
    375 }
    376 
    377 #define LENEXTTMP \
    378 	if (++bix == LETBUF) bix = 0, tmd = le->sc_r2->ler2_tmd; else ++tmd
    379 
    380 /*
    381  * Start output on interface.  Get another datagram to send
    382  * off of the interface queue, and copy it to the interface
    383  * before starting the output.
    384  */
    385 void
    386 lestart(ifp)
    387 	struct ifnet *ifp;
    388 {
    389 	register struct le_softc *le = &le_softc[ifp->if_unit];
    390 	register int bix;
    391 	register struct letmd *tmd;
    392 	register struct mbuf *m;
    393 	int len;
    394 
    395 	if ((le->sc_if.if_flags & IFF_RUNNING) == 0)
    396 		return;
    397 
    398 	bix = le->sc_tmd;
    399 	tmd = &le->sc_r2->ler2_tmd[bix];
    400 
    401 	for (;;) {
    402 		if (le->sc_no_td >= LETBUF) {
    403 			le->sc_if.if_flags |= IFF_OACTIVE;
    404 			break;
    405 		}
    406 
    407 		IF_DEQUEUE(&le->sc_if.if_snd, m);
    408 		if (m == 0)
    409 			break;
    410 
    411 		++le->sc_no_td;
    412 
    413 		len = leput(le->sc_r2->ler2_tbuf[bix], m);
    414 
    415 #if NBPFILTER > 0
    416 		/*
    417 		 * If bpf is listening on this interface, let it
    418 		 * see the packet before we commit it to the wire.
    419 		 */
    420 		if (le->sc_bpf)
    421         	        bpf_tap(le->sc_bpf, le->sc_r2->ler2_tbuf[bix], len);
    422 #endif
    423 
    424 		tmd->tmd3 = 0;
    425 		tmd->tmd2 = -len;
    426 		tmd->tmd1 = LE_OWN | LE_STP | LE_ENP;
    427 
    428 		LENEXTTMP;
    429 	}
    430 
    431 	le->sc_tmd = bix;
    432 }
    433 
    434 int
    435 leintr(le)
    436 	struct le_softc *le;
    437 {
    438 #if 0
    439 	register struct le_softc *le = &le_softc[unit];
    440 #else
    441 	int unit = le->sc_if.if_unit;
    442 #endif
    443 	register struct lereg1 *ler1;
    444 	register int stat;
    445 
    446 	/* if not even initialized, don't do anything further.. */
    447 	if (! le->sc_base)
    448 	  return 0;
    449 
    450 	ler1 = le->sc_r1;
    451 	stat =  ler1->ler1_rdp;
    452 
    453 	if (! (stat & LE_INTR))
    454 	  return 0;
    455 
    456 	if (stat & LE_SERR) {
    457 		leerror(unit, stat);
    458 		if (stat & LE_MERR) {
    459 			le->sc_merr++;
    460 			lereset(unit);
    461 			return(1);
    462 		}
    463 		if (stat & LE_BABL)
    464 			le->sc_babl++;
    465 		if (stat & LE_CERR)
    466 			le->sc_cerr++;
    467 		if (stat & LE_MISS)
    468 			le->sc_miss++;
    469 		 ler1->ler1_rdp =  LE_BABL|LE_CERR|LE_MISS|LE_INEA;
    470 	}
    471 	if ((stat & LE_RXON) == 0) {
    472 		le->sc_rxoff++;
    473 		lereset(unit);
    474 		return(1);
    475 	}
    476 	if ((stat & LE_TXON) == 0) {
    477 		le->sc_txoff++;
    478 		lereset(unit);
    479 		return(1);
    480 	}
    481 	if (stat & LE_RINT) {
    482 		/* interrupt is cleared in lerint */
    483 		lerint(unit);
    484 	}
    485 	if (stat & LE_TINT) {
    486 		 ler1->ler1_rdp =  LE_TINT|LE_INEA;
    487 		lexint(unit);
    488 	}
    489 	return(1);
    490 }
    491 
    492 /*
    493  * Ethernet interface transmitter interrupt.
    494  * Start another output if more data to send.
    495  */
    496 lexint(unit)
    497 	register int unit;
    498 {
    499 	register struct le_softc *le = &le_softc[unit];
    500 	register int bix = (le->sc_tmd - le->sc_no_td + LETBUF) % LETBUF;
    501 	register struct letmd *tmd = &le->sc_r2->ler2_tmd[bix];
    502 
    503 	if ((le->sc_if.if_flags & IFF_OACTIVE) == 0) {
    504 		le->sc_xint++;
    505 		return;
    506 	}
    507 	if (tmd->tmd1 & LE_OWN) {
    508 printf("le%d: extra xint\n", unit);
    509 		le->sc_xown++;
    510 		return;
    511 	}
    512 	le->sc_if.if_flags &= ~IFF_OACTIVE;
    513 
    514 	do {
    515 		if (le->sc_no_td <= 0)
    516 			break;
    517 		--le->sc_no_td;
    518 
    519 		if (tmd->tmd1 & LE_ERR) {
    520 err:
    521 printf("le%d: xint error\n", unit);
    522 			lexerror(unit);
    523 			le->sc_if.if_oerrors++;
    524 			if (tmd->tmd3 & (LE_TBUFF|LE_UFLO)) {
    525 				le->sc_uflo++;
    526 				lereset(unit);
    527 				return;
    528 			}
    529 			else if (tmd->tmd3 & LE_LCOL)
    530 				le->sc_if.if_collisions++;
    531 			else if (tmd->tmd3 & LE_RTRY)
    532 				le->sc_if.if_collisions += 16;
    533 		}
    534 		else if (tmd->tmd3 & LE_TBUFF)
    535 			/* XXX documentation says BUFF not included in ERR */
    536 			goto err;
    537 		else if (tmd->tmd1 & LE_ONE)
    538 			le->sc_if.if_collisions++;
    539 		else if (tmd->tmd1 & LE_MORE)
    540 			/* what is the real number? */
    541 			le->sc_if.if_collisions += 2;
    542 		else
    543 			le->sc_if.if_opackets++;
    544 		LENEXTTMP;
    545 	} while ((tmd->tmd1 & LE_OWN) == 0);
    546 
    547 	(void) lestart(&le->sc_if);
    548 }
    549 
    550 #define	LENEXTRMP \
    551 	if (++bix == LERBUF) bix = 0, rmd = le->sc_r2->ler2_rmd; else ++rmd
    552 
    553 /*
    554  * Ethernet interface receiver interrupt.
    555  * If input error just drop packet.
    556  * Decapsulate packet based on type and pass to type specific
    557  * higher-level input routine.
    558  */
    559 lerint(unit)
    560 	int unit;
    561 {
    562 	register struct le_softc *le = &le_softc[unit];
    563 	register int bix = le->sc_rmd;
    564 	register struct lermd *rmd = &le->sc_r2->ler2_rmd[bix];
    565 
    566 	/*
    567 	 * Out of sync with hardware, should never happen?
    568 	 */
    569 	if (rmd->rmd1 & LE_OWN) {
    570 		 le->sc_r1->ler1_rdp =  LE_RINT|LE_INEA;
    571 		return;
    572 	}
    573 
    574 	/*
    575 	 * Process all buffers with valid data
    576 	 */
    577 	while ((rmd->rmd1 & LE_OWN) == 0) {
    578 		int len = rmd->rmd3;
    579 
    580 		/* Clear interrupt to avoid race condition */
    581 		 le->sc_r1->ler1_rdp =  LE_RINT|LE_INEA;
    582 
    583 		if (rmd->rmd1 & LE_ERR) {
    584 			le->sc_rmd = bix;
    585 			lererror(unit, "bad packet");
    586 			le->sc_if.if_ierrors++;
    587 		} else if ((rmd->rmd1 & (LE_STP|LE_ENP)) != (LE_STP|LE_ENP)) {
    588 			/*
    589 			 * Find the end of the packet so we can see how long
    590 			 * it was.  We still throw it away.
    591 			 */
    592 			do {
    593 			        le->sc_r1->ler1_rdp = LE_RINT|LE_INEA;
    594 				rmd->rmd3 = 0;
    595 				rmd->rmd1 = LE_OWN;
    596 				LENEXTRMP;
    597 			} while (!(rmd->rmd1 & (LE_OWN|LE_ERR|LE_STP|LE_ENP)));
    598 
    599 			le->sc_rmd = bix;
    600 			lererror(unit, "chained buffer");
    601 			le->sc_rxlen++;
    602 
    603 			/*
    604 			 * If search terminated without successful completion
    605 			 * we reset the hardware (conservative).
    606 			 */
    607 			if ((rmd->rmd1 & (LE_OWN|LE_ERR|LE_STP|LE_ENP)) != LE_ENP) {
    608 				lereset(unit);
    609 				return;
    610 			}
    611 		} else
    612 			leread(unit, le->sc_r2->ler2_rbuf[bix], len);
    613 
    614 		rmd->rmd3 = 0;
    615 		rmd->rmd1 = LE_OWN;
    616 		LENEXTRMP;
    617 
    618 	}
    619 
    620 	le->sc_rmd = bix;
    621 }
    622 
    623 leread(unit, buf, len)
    624 	int unit;
    625 	char *buf;
    626 	int len;
    627 {
    628 	register struct le_softc *le = &le_softc[unit];
    629 	register struct ether_header *et;
    630     	struct mbuf *m;
    631 	int off, resid;
    632 
    633 	le->sc_if.if_ipackets++;
    634 
    635 	et = (struct ether_header *)buf;
    636 	et->ether_type = ntohs((u_short)et->ether_type);
    637 
    638 	/* adjust input length to account for header and CRC */
    639 	len = len - sizeof(struct ether_header) - 4;
    640 
    641 #define	ledataaddr(et, off, type)	((type)(((caddr_t)((et)+1)+(off))))
    642 	if (et->ether_type >= ETHERTYPE_TRAIL &&
    643 	    et->ether_type < ETHERTYPE_TRAIL+ETHERTYPE_NTRAILER) {
    644 		off = (et->ether_type - ETHERTYPE_TRAIL) * 512;
    645 		if (off >= ETHERMTU)
    646 			return;		/* sanity */
    647 		et->ether_type = ntohs(*ledataaddr(et, off, u_short *));
    648 		resid = ntohs(*(ledataaddr(et, off+2, u_short *)));
    649 		if (off + resid > len)
    650 			return;		/* sanity */
    651 		len = off + resid;
    652 	} else
    653 		off = 0;
    654 
    655 	if (len <= 0) {
    656 		if (ledebug)
    657 			log(LOG_WARNING,
    658 			    "le%d: ierror(runt packet): from %s: len=%d\n",
    659 			    unit, ether_sprintf(et->ether_shost), len);
    660 		le->sc_runt++;
    661 		le->sc_if.if_ierrors++;
    662 		return;
    663 	}
    664 #if NBPFILTER > 0
    665 	/*
    666 	 * Check if there's a bpf filter listening on this interface.
    667 	 * If so, hand off the raw packet to bpf, which must deal with
    668 	 * trailers in its own way.
    669 	 */
    670 	if (le->sc_bpf) {
    671 		bpf_tap(le->sc_bpf, buf, len + sizeof(struct ether_header));
    672 
    673 		/*
    674 		 * Note that the interface cannot be in promiscuous mode if
    675 		 * there are no bpf listeners.  And if we are in promiscuous
    676 		 * mode, we have to check if this packet is really ours.
    677 		 *
    678 		 * XXX This test does not support multicasts.
    679 		 */
    680 		if ((le->sc_if.if_flags & IFF_PROMISC)
    681 		    && bcmp(et->ether_dhost, le->sc_addr,
    682 			    sizeof(et->ether_dhost)) != 0
    683 		    && bcmp(et->ether_dhost, etherbroadcastaddr,
    684 			    sizeof(et->ether_dhost)) != 0)
    685 			return;
    686 	}
    687 #endif
    688 	/*
    689 	 * Pull packet off interface.  Off is nonzero if packet
    690 	 * has trailing header; leget will then force this header
    691 	 * information to be at the front, but we still have to drop
    692 	 * the type and length which are at the front of any trailer data.
    693 	 */
    694 	m = leget(buf, len, off, &le->sc_if);
    695 	if (m == 0)
    696 		return;
    697 
    698 	ether_input(&le->sc_if, et, m);
    699 }
    700 
    701 /*
    702  * Routine to copy from mbuf chain to transmit
    703  * buffer in board local memory.
    704  */
    705 leput(lebuf, m)
    706 	register char *lebuf;
    707 	register struct mbuf *m;
    708 {
    709 	register struct mbuf *mp;
    710 	register int len, tlen = 0;
    711 
    712 	for (mp = m; mp; mp = mp->m_next) {
    713 		len = mp->m_len;
    714 		if (len == 0)
    715 			continue;
    716 		tlen += len;
    717 		bcopy(mtod(mp, char *), lebuf, len);
    718 		lebuf += len;
    719 	}
    720 
    721 	m_freem(m);
    722 
    723 	if (tlen < LEMINSIZE) {
    724 		bzero(lebuf, LEMINSIZE - tlen);
    725 		tlen = LEMINSIZE;
    726 	}
    727 
    728 	return(tlen);
    729 }
    730 
    731 /*
    732  * Routine to copy from board local memory into mbufs.
    733  */
    734 struct mbuf *
    735 leget(lebuf, totlen, off0, ifp)
    736 	char *lebuf;
    737 	int totlen, off0;
    738 	struct ifnet *ifp;
    739 {
    740 	register struct mbuf *m;
    741 	struct mbuf *top = 0, **mp = &top;
    742 	register int off = off0, len;
    743 	register char *cp;
    744 	char *epkt;
    745 
    746 	lebuf += sizeof (struct ether_header);
    747 	cp = lebuf;
    748 	epkt = cp + totlen;
    749 	if (off) {
    750 		cp += off + 2 * sizeof(u_short);
    751 		totlen -= 2 * sizeof(u_short);
    752 	}
    753 
    754 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    755 
    756 	if (m == 0)
    757 		return (0);
    758 
    759 	m->m_pkthdr.rcvif = ifp;
    760 	m->m_pkthdr.len = totlen;
    761 	m->m_len = MHLEN;
    762 
    763 	while (totlen > 0) {
    764 
    765 		if (top) {
    766 			MGET(m, M_DONTWAIT, MT_DATA);
    767 			if (m == 0) {
    768 				m_freem(top);
    769 				return (0);
    770 			}
    771 			m->m_len = MLEN;
    772 		}
    773 
    774 		len = min(totlen, epkt - cp);
    775 		if (len >= MINCLSIZE) {
    776 			MCLGET(m, M_DONTWAIT);
    777 			if (m->m_flags & M_EXT)
    778 				m->m_len = len = min(len, MCLBYTES);
    779 			else
    780 				len = m->m_len;
    781 		} else {
    782 			/*
    783 			 * Place initial small packet/header at end of mbuf.
    784 			 */
    785 			if (len < m->m_len) {
    786 				if (top == 0 && len + max_linkhdr <= m->m_len)
    787 					m->m_data += max_linkhdr;
    788 				m->m_len = len;
    789 			} else
    790 				len = m->m_len;
    791 		}
    792 
    793 		bcopy(cp, mtod(m, caddr_t), (unsigned)len);
    794 		cp += len;
    795 		*mp = m;
    796 		mp = &m->m_next;
    797 		totlen -= len;
    798 
    799 		if (cp == epkt)
    800 			cp = lebuf;
    801 
    802 	}
    803 
    804 	return (top);
    805 }
    806 
    807 /*
    808  * Process an ioctl request.
    809  */
    810 int
    811 leioctl(ifp, cmd, data)
    812 	register struct ifnet *ifp;
    813 	u_long cmd;
    814 	caddr_t data;
    815 {
    816 	register struct ifaddr *ifa = (struct ifaddr *)data;
    817 	struct le_softc *le = &le_softc[ifp->if_unit];
    818 	struct lereg1 *ler1 = le->sc_r1;
    819 	int s = splimp(), error = 0;
    820 
    821 	switch (cmd) {
    822 
    823 	case SIOCSIFADDR:
    824 		ifp->if_flags |= IFF_UP;
    825 		switch (ifa->ifa_addr->sa_family) {
    826 #ifdef INET
    827 		case AF_INET:
    828 			leinit(ifp->if_unit);
    829 			arp_ifinit(&le->sc_ac, ifa);
    830 			break;
    831 #endif
    832 #ifdef NS
    833 		case AF_NS:
    834 		    {
    835 			register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
    836 
    837 			if (ns_nullhost(*ina))
    838 				ina->x_host = *(union ns_host *)(le->sc_addr);
    839 			else {
    840 				/*
    841 				 * The manual says we can't change the address
    842 				 * while the receiver is armed,
    843 				 * so reset everything
    844 				 */
    845 				ifp->if_flags &= ~IFF_RUNNING;
    846 				bcopy((caddr_t)ina->x_host.c_host,
    847 				    (caddr_t)le->sc_addr, sizeof(le->sc_addr));
    848 			}
    849 			leinit(ifp->if_unit); /* does le_setaddr() */
    850 			break;
    851 		    }
    852 #endif
    853 		default:
    854 			leinit(ifp->if_unit);
    855 			break;
    856 		}
    857 		break;
    858 
    859 	case SIOCSIFFLAGS:
    860 		if ((ifp->if_flags & IFF_UP) == 0 &&
    861 		    ifp->if_flags & IFF_RUNNING) {
    862 			 ler1->ler1_rdp =  LE_STOP;
    863 			ifp->if_flags &= ~IFF_RUNNING;
    864 		} else if (ifp->if_flags & IFF_UP && (ifp->if_flags & IFF_RUNNING) == 0)
    865 			leinit(ifp->if_unit);
    866 
    867 		/*
    868 		 * If the state of the promiscuous bit changes, the interface
    869 		 * must be reset to effect the change.
    870 		 */
    871 		if (((ifp->if_flags ^ le->sc_iflags) & IFF_PROMISC) && (ifp->if_flags & IFF_RUNNING)) {
    872 			le->sc_iflags = ifp->if_flags;
    873 			lereset(ifp->if_unit);
    874 			lestart(ifp);
    875 		}
    876 		break;
    877 
    878 	default:
    879 		error = EINVAL;
    880 	}
    881 
    882 	splx(s);
    883 	return (error);
    884 }
    885 
    886 leerror(unit, stat)
    887 	int unit;
    888 	int stat;
    889 {
    890 	if (!ledebug)
    891 		return;
    892 
    893 	/*
    894 	 * Not all transceivers implement heartbeat
    895 	 * so we only log CERR once.
    896 	 */
    897 	if ((stat & LE_CERR) && le_softc[unit].sc_cerr)
    898 		return;
    899 
    900 	log(LOG_WARNING,
    901 	    "le%d: error: stat=%b\n", unit,
    902 	    stat,
    903 	    "\20\20ERR\17BABL\16CERR\15MISS\14MERR\13RINT\12TINT\11IDON\10INTR\07INEA\06RXON\05TXON\04TDMD\03STOP\02STRT\01INIT");
    904 }
    905 
    906 lererror(unit, msg)
    907 	int unit;
    908 	char *msg;
    909 {
    910 	register struct le_softc *le = &le_softc[unit];
    911 	register struct lermd *rmd;
    912 	int len;
    913 
    914 	if (!ledebug)
    915 		return;
    916 
    917 	rmd = &le->sc_r2->ler2_rmd[le->sc_rmd];
    918 	len = rmd->rmd3;
    919 
    920 	log(LOG_WARNING,
    921 	    "le%d: ierror(%s): from %s: buf=%d, len=%d, rmd1=%b\n",
    922 	    unit, msg,
    923 	    len > 11 ? ether_sprintf(&le->sc_r2->ler2_rbuf[le->sc_rmd][6]) : "unknown",
    924 	    le->sc_rmd, len,
    925 	    rmd->rmd1,
    926 	    "\20\20OWN\17ERR\16FRAM\15OFLO\14CRC\13RBUF\12STP\11ENP");
    927 }
    928 
    929 lexerror(unit)
    930 	int unit;
    931 {
    932 	register struct le_softc *le = &le_softc[unit];
    933 	int bix;
    934 	register struct letmd *tmd;
    935 	int len;
    936 
    937 	if (!ledebug)
    938 		return;
    939 
    940 	bix = (le->sc_tmd - le->sc_no_td + LETBUF) % LETBUF;
    941 	tmd = &le->sc_r2->ler2_tmd[bix];
    942 	len = -tmd->tmd2;
    943 
    944 	log(LOG_WARNING,
    945 	    "le%d: oerror: to %s: buf=%d, len=%d, tmd1=%b, tmd3=%b\n",
    946 	    unit,
    947 	    len > 5 ? ether_sprintf(&le->sc_r2->ler2_tbuf[0][0]) : "unknown",
    948 	    0, len,
    949 	    tmd->tmd1,
    950 	    "\20\20OWN\17ERR\16RES\15MORE\14ONE\13DEF\12STP\11ENP",
    951 	    tmd->tmd3,
    952 	    "\20\20BUFF\17UFLO\16RES\15LCOL\14LCAR\13RTRY");
    953 }
    954 
    955 #endif
    956 
    957 
    958