Home | History | Annotate | Line # | Download | only in dev
if_mc.c revision 1.9
      1 /*	$NetBSD: if_mc.c,v 1.9 1998/07/05 03:14:41 jonathan Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997 David Huang <khym (at) bga.com>
      5  * All rights reserved.
      6  *
      7  * Portions of this code are based on code by Denton Gentry <denny1 (at) home.com>,
      8  * Charles M. Hannum, Yanagisawa Takeshi <yanagisw (at) aa.ap.titech.ac.jp>, and
      9  * Jason R. Thorpe.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. The name of the author may not be used to endorse or promote products
     17  *    derived from this software without specific prior written permission
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  *
     30  */
     31 
     32 /*
     33  * Driver for the AMD Am79C940 (MACE) ethernet chip, used for onboard
     34  * ethernet on the Centris/Quadra 660av and Quadra 840av.
     35  */
     36 
     37 #include "opt_ddb.h"
     38 #include "opt_inet.h"
     39 #include "opt_ccitt.h"
     40 #include "opt_llc.h"
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/mbuf.h>
     45 #include <sys/buf.h>
     46 #include <sys/protosw.h>
     47 #include <sys/socket.h>
     48 #include <sys/syslog.h>
     49 #include <sys/ioctl.h>
     50 #include <sys/errno.h>
     51 #include <sys/device.h>
     52 
     53 #include <net/if.h>
     54 #include <net/if_dl.h>
     55 #include <net/if_ether.h>
     56 
     57 #ifdef INET
     58 #include <netinet/in.h>
     59 #include <netinet/if_inarp.h>
     60 #include <netinet/in_systm.h>
     61 #include <netinet/in_var.h>
     62 #include <netinet/ip.h>
     63 #endif
     64 
     65 #ifdef NS
     66 #include <netns/ns.h>
     67 #include <netns/ns_if.h>
     68 #endif
     69 
     70 #if defined(CCITT) && defined(LLC)
     71 #include <sys/socketvar.h>
     72 #include <netccitt/x25.h>
     73 #include <netccitt/pk.h>
     74 #include <netccitt/pk_var.h>
     75 #include <netccitt/pk_extern.h>
     76 #endif
     77 
     78 #include <vm/vm.h>
     79 
     80 #include "bpfilter.h"
     81 #if NBPFILTER > 0
     82 #include <net/bpf.h>
     83 #include <net/bpfdesc.h>
     84 #endif
     85 
     86 #include <machine/bus.h>
     87 #include <mac68k/dev/if_mcreg.h>
     88 #include <mac68k/dev/if_mcvar.h>
     89 
     90 #ifdef DDB
     91 #define	integrate
     92 #define hide
     93 #else
     94 #define	integrate	static __inline
     95 #define hide		static
     96 #endif
     97 
     98 hide void	mcwatchdog __P((struct ifnet *));
     99 hide int	mcinit __P((struct mc_softc *sc));
    100 hide int	mcstop __P((struct mc_softc *sc));
    101 hide int	mcioctl __P((struct ifnet *ifp, u_long cmd, caddr_t data));
    102 hide void	mcstart __P((struct ifnet *ifp));
    103 hide void	mcreset __P((struct mc_softc *sc));
    104 
    105 integrate u_int	maceput __P((struct mc_softc *sc, struct mbuf *m0));
    106 integrate void	mc_tint __P((struct mc_softc *sc));
    107 integrate void	mace_read __P((struct mc_softc *, caddr_t, int));
    108 integrate struct mbuf *mace_get __P((struct mc_softc *, caddr_t, int));
    109 static void mace_calcladrf __P((struct ethercom *ac, u_int8_t *af));
    110 static inline u_int16_t ether_cmp __P((void *, void *));
    111 
    112 
    113 /*
    114  * Compare two Ether/802 addresses for equality, inlined and
    115  * unrolled for speed.  Use this like bcmp().
    116  *
    117  * XXX: Add <machine/inlines.h> for stuff like this?
    118  * XXX: or maybe add it to libkern.h instead?
    119  *
    120  * "I'd love to have an inline assembler version of this."
    121  * XXX: Who wanted that? mycroft?  I wrote one, but this
    122  * version in C is as good as hand-coded assembly. -gwr
    123  *
    124  * Please do NOT tweak this without looking at the actual
    125  * assembly code generated before and after your tweaks!
    126  */
    127 static inline u_int16_t
    128 ether_cmp(one, two)
    129 	void *one, *two;
    130 {
    131 	register u_int16_t *a = (u_short *) one;
    132 	register u_int16_t *b = (u_short *) two;
    133 	register u_int16_t diff;
    134 
    135 #ifdef	m68k
    136 	/*
    137 	 * The post-increment-pointer form produces the best
    138 	 * machine code for m68k.  This was carefully tuned
    139 	 * so it compiles to just 8 short (2-byte) op-codes!
    140 	 */
    141 	diff  = *a++ - *b++;
    142 	diff |= *a++ - *b++;
    143 	diff |= *a++ - *b++;
    144 #else
    145 	/*
    146 	 * Most modern CPUs do better with a single expresion.
    147 	 * Note that short-cut evaluation is NOT helpful here,
    148 	 * because it just makes the code longer, not faster!
    149 	 */
    150 	diff = (a[0] - b[0]) | (a[1] - b[1]) | (a[2] - b[2]);
    151 #endif
    152 
    153 	return (diff);
    154 }
    155 
    156 #define ETHER_CMP	ether_cmp
    157 
    158 /*
    159  * Interface exists: make available by filling in network interface
    160  * record.  System will initialize the interface when it is ready
    161  * to accept packets.
    162  */
    163 int
    164 mcsetup(sc, lladdr)
    165 	struct mc_softc	*sc;
    166 	u_int8_t *lladdr;
    167 {
    168 	struct ifnet *ifp = &sc->sc_if;
    169 
    170 	/* reset the chip and disable all interrupts */
    171 	NIC_PUT(sc, MACE_BIUCC, SWRST);
    172 	DELAY(100);
    173 	NIC_PUT(sc, MACE_IMR, ~0);
    174 
    175 	bcopy(lladdr, sc->sc_enaddr, ETHER_ADDR_LEN);
    176 	printf(": address %s\n", ether_sprintf(lladdr));
    177 
    178 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    179 	ifp->if_softc = sc;
    180 	ifp->if_ioctl = mcioctl;
    181 	ifp->if_start = mcstart;
    182 	ifp->if_flags =
    183 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    184 	ifp->if_watchdog = mcwatchdog;
    185 
    186 #if NBPFILTER > 0
    187 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
    188 #endif
    189 	if_attach(ifp);
    190 	ether_ifattach(ifp, lladdr);
    191 
    192 	return (0);
    193 }
    194 
    195 hide int
    196 mcioctl(ifp, cmd, data)
    197 	struct ifnet *ifp;
    198 	u_long cmd;
    199 	caddr_t data;
    200 {
    201 	struct mc_softc *sc = ifp->if_softc;
    202 	struct ifaddr *ifa;
    203 	struct ifreq *ifr;
    204 
    205 	int	s = splnet(), err = 0;
    206 	int	temp;
    207 
    208 	switch (cmd) {
    209 
    210 	case SIOCSIFADDR:
    211 		ifa = (struct ifaddr *)data;
    212 		ifp->if_flags |= IFF_UP;
    213 		switch (ifa->ifa_addr->sa_family) {
    214 #ifdef INET
    215 		case AF_INET:
    216 			mcinit(sc);
    217 			arp_ifinit(ifp, ifa);
    218 			break;
    219 #endif
    220 #ifdef NS
    221 		case AF_NS:
    222 		    {
    223 			register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
    224 
    225 			if (ns_nullhost(*ina))
    226 				ina->x_host =
    227 				    *(union ns_host *)LLADDR(ifp->if_sadl);
    228 			else {
    229 				bcopy(ina->x_host.c_host,
    230 				    LLADDR(ifp->if_sadl),
    231 				    sizeof(sc->sc_enaddr));
    232 			}
    233 			/* Set new address. */
    234 			mcinit(sc);
    235 			break;
    236 		    }
    237 #endif
    238 		default:
    239 			mcinit(sc);
    240 			break;
    241 		}
    242 		break;
    243 
    244 	case SIOCSIFFLAGS:
    245 		if ((ifp->if_flags & IFF_UP) == 0 &&
    246 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    247 			/*
    248 			 * If interface is marked down and it is running,
    249 			 * then stop it.
    250 			 */
    251 			mcstop(sc);
    252 			ifp->if_flags &= ~IFF_RUNNING;
    253 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
    254 		    (ifp->if_flags & IFF_RUNNING) == 0) {
    255 			/*
    256 			 * If interface is marked up and it is stopped,
    257 			 * then start it.
    258 			 */
    259 			(void)mcinit(sc);
    260 		} else {
    261 			/*
    262 			 * reset the interface to pick up any other changes
    263 			 * in flags
    264 			 */
    265 			temp = ifp->if_flags & IFF_UP;
    266 			mcreset(sc);
    267 			ifp->if_flags |= temp;
    268 			mcstart(ifp);
    269 		}
    270 		break;
    271 
    272 	case SIOCADDMULTI:
    273 	case SIOCDELMULTI:
    274 		ifr = (struct ifreq *) data;
    275 		err = (cmd == SIOCADDMULTI) ?
    276 		    ether_addmulti(ifr, &sc->sc_ethercom) :
    277 		    ether_delmulti(ifr, &sc->sc_ethercom);
    278 
    279 		if (err == ENETRESET) {
    280 			/*
    281 			 * Multicast list has changed; set the hardware
    282 			 * filter accordingly. But remember UP flag!
    283 			 */
    284 			temp = ifp->if_flags & IFF_UP;
    285 			mcreset(sc);
    286 			ifp->if_flags |= temp;
    287 			err = 0;
    288 		}
    289 		break;
    290 	default:
    291 		err = EINVAL;
    292 	}
    293 	splx(s);
    294 	return (err);
    295 }
    296 
    297 /*
    298  * Encapsulate a packet of type family for the local net.
    299  */
    300 hide void
    301 mcstart(ifp)
    302 	struct ifnet *ifp;
    303 {
    304 	struct mc_softc	*sc = ifp->if_softc;
    305 	struct mbuf	*m;
    306 
    307 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
    308 		return;
    309 
    310 	while (1) {
    311 		if (ifp->if_flags & IFF_OACTIVE)
    312 			return;
    313 
    314 		IF_DEQUEUE(&ifp->if_snd, m);
    315 		if (m == 0)
    316 			return;
    317 
    318 #if NBPFILTER > 0
    319 		/*
    320 		 * If bpf is listening on this interface, let it
    321 		 * see the packet before we commit it to the wire.
    322 		 */
    323 		if (ifp->if_bpf)
    324 			bpf_mtap(ifp->if_bpf, m);
    325 #endif
    326 
    327 		/*
    328 		 * Copy the mbuf chain into the transmit buffer.
    329 		 */
    330 		ifp->if_flags |= IFF_OACTIVE;
    331 		maceput(sc, m);
    332 
    333 		ifp->if_opackets++;		/* # of pkts */
    334 	}
    335 }
    336 
    337 /*
    338  * reset and restart the MACE.  Called in case of fatal
    339  * hardware/software errors.
    340  */
    341 hide void
    342 mcreset(sc)
    343 	struct mc_softc *sc;
    344 {
    345 	mcstop(sc);
    346 	mcinit(sc);
    347 }
    348 
    349 hide int
    350 mcinit(sc)
    351 	struct mc_softc *sc;
    352 {
    353 	int s;
    354 	u_int8_t maccc, ladrf[8];
    355 
    356 	if (sc->sc_if.if_flags & IFF_RUNNING)
    357 		/* already running */
    358 		return (0);
    359 
    360 	s = splnet();
    361 
    362 	NIC_PUT(sc, MACE_BIUCC, sc->sc_biucc);
    363 	NIC_PUT(sc, MACE_FIFOCC, sc->sc_fifocc);
    364 	NIC_PUT(sc, MACE_IMR, ~0); /* disable all interrupts */
    365 	NIC_PUT(sc, MACE_PLSCC, sc->sc_plscc);
    366 
    367 	NIC_PUT(sc, MACE_UTR, RTRD); /* disable reserved test registers */
    368 
    369 	/* set MAC address */
    370 	NIC_PUT(sc, MACE_IAC, ADDRCHG);
    371 	while (NIC_GET(sc, MACE_IAC) & ADDRCHG)
    372 		;
    373 	NIC_PUT(sc, MACE_IAC, PHYADDR);
    374 	bus_space_write_multi_1(sc->sc_regt, sc->sc_regh, MACE_REG(MACE_PADR),
    375 	    sc->sc_enaddr, ETHER_ADDR_LEN);
    376 
    377 	/* set logical address filter */
    378 	mace_calcladrf(&sc->sc_ethercom, ladrf);
    379 
    380 	NIC_PUT(sc, MACE_IAC, ADDRCHG);
    381 	while (NIC_GET(sc, MACE_IAC) & ADDRCHG)
    382 		;
    383 	NIC_PUT(sc, MACE_IAC, LOGADDR);
    384 	bus_space_write_multi_1(sc->sc_regt, sc->sc_regh, MACE_REG(MACE_LADRF),
    385 	    ladrf, 8);
    386 
    387 	NIC_PUT(sc, MACE_XMTFC, APADXMT);
    388 	/*
    389 	 * No need to autostrip padding on receive... Ethernet frames
    390 	 * don't have a length field, unlike 802.3 frames, so the MACE
    391 	 * can't figure out the length of the packet anyways.
    392 	 */
    393 	NIC_PUT(sc, MACE_RCVFC, 0);
    394 
    395 	maccc = ENXMT | ENRCV;
    396 	if (sc->sc_if.if_flags & IFF_PROMISC)
    397 		maccc |= PROM;
    398 
    399 	NIC_PUT(sc, MACE_MACCC, maccc);
    400 
    401 	if (sc->sc_bus_init)
    402 		(*sc->sc_bus_init)(sc);
    403 
    404 	/*
    405 	 * Enable all interrupts except receive, since we use the DMA
    406 	 * completion interrupt for that.
    407 	 */
    408 	NIC_PUT(sc, MACE_IMR, RCVINTM);
    409 
    410 	/* flag interface as "running" */
    411 	sc->sc_if.if_flags |= IFF_RUNNING;
    412 	sc->sc_if.if_flags &= ~IFF_OACTIVE;
    413 
    414 	splx(s);
    415 	return (0);
    416 }
    417 
    418 /*
    419  * close down an interface and free its buffers
    420  * Called on final close of device, or if mcinit() fails
    421  * part way through.
    422  */
    423 hide int
    424 mcstop(sc)
    425 	struct mc_softc *sc;
    426 {
    427 	int	s = splnet();
    428 
    429 	NIC_PUT(sc, MACE_BIUCC, SWRST);
    430 	DELAY(100);
    431 
    432 	sc->sc_if.if_timer = 0;
    433 	sc->sc_if.if_flags &= ~(IFF_RUNNING | IFF_UP);
    434 
    435 	splx(s);
    436 	return (0);
    437 }
    438 
    439 /*
    440  * Called if any Tx packets remain unsent after 5 seconds,
    441  * In all cases we just reset the chip, and any retransmission
    442  * will be handled by higher level protocol timeouts.
    443  */
    444 hide void
    445 mcwatchdog(ifp)
    446 	struct ifnet *ifp;
    447 {
    448 	struct mc_softc *sc = ifp->if_softc;
    449 	int temp;
    450 
    451 	printf("mcwatchdog: resetting chip\n");
    452 	temp = ifp->if_flags & IFF_UP;
    453 	mcreset(sc);
    454 	ifp->if_flags |= temp;
    455 }
    456 
    457 /*
    458  * stuff packet into MACE (at splnet)
    459  */
    460 integrate u_int
    461 maceput(sc, m)
    462 	struct mc_softc *sc;
    463 	struct mbuf *m;
    464 {
    465 	struct mbuf *n;
    466 	u_int len, totlen = 0;
    467 	u_char *buff;
    468 
    469 	buff = sc->sc_txbuf;
    470 
    471 	for (; m; m = n) {
    472 		u_char *data = mtod(m, u_char *);
    473 		len = m->m_len;
    474 		totlen += len;
    475 		bcopy(data, buff, len);
    476 		buff += len;
    477 		MFREE(m, n);
    478 	}
    479 
    480 	if (totlen > NBPG)
    481 		panic("%s: maceput: packet overflow", sc->sc_dev.dv_xname);
    482 
    483 #if 0
    484 	if (totlen < ETHERMIN + sizeof(struct ether_header)) {
    485 		int pad = ETHERMIN + sizeof(struct ether_header) - totlen;
    486 		bzero(sc->sc_txbuf + totlen, pad);
    487 		totlen = ETHERMIN + sizeof(struct ether_header);
    488 	}
    489 #endif
    490 
    491 	(*sc->sc_putpacket)(sc, totlen);
    492 
    493 	sc->sc_if.if_timer = 5;	/* 5 seconds to watch for failing to transmit */
    494 	return (totlen);
    495 }
    496 
    497 void
    498 mcintr(arg)
    499 	void *arg;
    500 {
    501 struct mc_softc *sc = arg;
    502 	u_int8_t ir;
    503 
    504 	ir = NIC_GET(sc, MACE_IR) & ~NIC_GET(sc, MACE_IMR);
    505 	if (ir & JAB) {
    506 #ifdef MCDEBUG
    507 		printf("%s: jabber error\n", sc->sc_dev.dv_xname);
    508 #endif
    509 		sc->sc_if.if_oerrors++;
    510 	}
    511 
    512 	if (ir & BABL) {
    513 #ifdef MCDEBUG
    514 		printf("%s: babble\n", sc->sc_dev.dv_xname);
    515 #endif
    516 		sc->sc_if.if_oerrors++;
    517 	}
    518 
    519 	if (ir & CERR) {
    520 		printf("%s: collision error\n", sc->sc_dev.dv_xname);
    521 		sc->sc_if.if_collisions++;
    522 	}
    523 
    524 	/*
    525 	 * Pretend we have carrier; if we don't this will be cleared
    526 	 * shortly.
    527 	 */
    528 	sc->sc_havecarrier = 1;
    529 
    530 	if (ir & XMTINT)
    531 		mc_tint(sc);
    532 
    533 	if (ir & RCVINT)
    534 		mc_rint(sc);
    535 }
    536 
    537 integrate void
    538 mc_tint(sc)
    539 	struct mc_softc *sc;
    540 {
    541 	u_int8_t xmtrc, xmtfs;
    542 
    543 	xmtrc = NIC_GET(sc, MACE_XMTRC);
    544 	xmtfs = NIC_GET(sc, MACE_XMTFS);
    545 
    546 	if ((xmtfs & XMTSV) == 0)
    547 		return;
    548 
    549 	if (xmtfs & UFLO) {
    550 		printf("%s: underflow\n", sc->sc_dev.dv_xname);
    551 		mcreset(sc);
    552 		return;
    553 	}
    554 
    555 	if (xmtfs & LCOL) {
    556 		printf("%s: late collision\n", sc->sc_dev.dv_xname);
    557 		sc->sc_if.if_oerrors++;
    558 		sc->sc_if.if_collisions++;
    559 	}
    560 
    561 	if (xmtfs & MORE)
    562 		/* Real number is unknown. */
    563 		sc->sc_if.if_collisions += 2;
    564 	else if (xmtfs & ONE)
    565 		sc->sc_if.if_collisions++;
    566 	else if (xmtfs & RTRY) {
    567 		sc->sc_if.if_collisions += 16;
    568 		sc->sc_if.if_oerrors++;
    569 	}
    570 
    571 	if (xmtfs & LCAR) {
    572 		sc->sc_havecarrier = 0;
    573 		printf("%s: lost carrier\n", sc->sc_dev.dv_xname);
    574 		sc->sc_if.if_oerrors++;
    575 	}
    576 
    577 	sc->sc_if.if_flags &= ~IFF_OACTIVE;
    578 	sc->sc_if.if_timer = 0;
    579 	mcstart(&sc->sc_if);
    580 }
    581 
    582 void
    583 mc_rint(sc)
    584 	struct mc_softc *sc;
    585 {
    586 #define	rxf	sc->sc_rxframe
    587 	u_int len;
    588 
    589 	len = (rxf.rx_rcvcnt | ((rxf.rx_rcvsts & 0xf) << 8)) - 4;
    590 
    591 #ifdef MCDEBUG
    592 	if (rxf.rx_rcvsts & 0xf0)
    593 		printf("%s: rcvcnt %02x rcvsts %02x rntpc 0x%02x rcvcc 0x%02x\n",
    594 		    sc->sc_dev.dv_xname, rxf.rx_rcvcnt, rxf.rx_rcvsts,
    595 		    rxf.rx_rntpc, rxf.rx_rcvcc);
    596 #endif
    597 
    598 	if (rxf.rx_rcvsts & OFLO) {
    599 		printf("%s: receive FIFO overflow\n", sc->sc_dev.dv_xname);
    600 		sc->sc_if.if_ierrors++;
    601 		return;
    602 	}
    603 
    604 	if (rxf.rx_rcvsts & CLSN)
    605 		sc->sc_if.if_collisions++;
    606 
    607 	if (rxf.rx_rcvsts & FRAM) {
    608 #ifdef MCDEBUG
    609 		printf("%s: framing error\n", sc->sc_dev.dv_xname);
    610 #endif
    611 		sc->sc_if.if_ierrors++;
    612 		return;
    613 	}
    614 
    615 	if (rxf.rx_rcvsts & FCS) {
    616 #ifdef MCDEBUG
    617 		printf("%s: frame control checksum error\n", sc->sc_dev.dv_xname);
    618 #endif
    619 		sc->sc_if.if_ierrors++;
    620 		return;
    621 	}
    622 
    623 	mace_read(sc, rxf.rx_frame, len);
    624 #undef	rxf
    625 }
    626 
    627 integrate void
    628 mace_read(sc, pkt, len)
    629 	struct mc_softc *sc;
    630 	caddr_t pkt;
    631 	int len;
    632 {
    633 	struct ifnet *ifp = &sc->sc_if;
    634 	struct ether_header *eh = (struct ether_header *)pkt;
    635 	struct mbuf *m;
    636 
    637 	if (len <= sizeof(struct ether_header) ||
    638 	    len > ETHERMTU + sizeof(struct ether_header)) {
    639 #ifdef MCDEBUG
    640 		printf("%s: invalid packet size %d; dropping\n",
    641 		    sc->sc_dev.dv_xname, len);
    642 #endif
    643 		ifp->if_ierrors++;
    644 		return;
    645 	}
    646 
    647 #if NBPFILTER > 0
    648 	/*
    649 	 * Check if there's a bpf filter listening on this interface.
    650 	 * If so, hand off the raw packet to enet, then discard things
    651 	 * not destined for us (but be sure to keep broadcast/multicast).
    652 	 */
    653 	if (ifp->if_bpf) {
    654 		bpf_tap(ifp->if_bpf, pkt, len);
    655 		if ((ifp->if_flags & IFF_PROMISC) != 0 &&
    656 		    (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
    657 		    ETHER_CMP(eh->ether_dhost, sc->sc_enaddr))
    658 			return;
    659 	}
    660 #endif
    661 	m = mace_get(sc, pkt, len);
    662 	if (m == NULL) {
    663 		ifp->if_ierrors++;
    664 		return;
    665 	}
    666 
    667 	ifp->if_ipackets++;
    668 
    669 	/* Pass the packet up, with the ether header sort-of removed. */
    670 	m_adj(m, sizeof(struct ether_header));
    671 	ether_input(ifp, eh, m);
    672 }
    673 
    674 /*
    675  * Pull data off an interface.
    676  * Len is length of data, with local net header stripped.
    677  * We copy the data into mbufs.  When full cluster sized units are present
    678  * we copy into clusters.
    679  */
    680 integrate struct mbuf *
    681 mace_get(sc, pkt, totlen)
    682 	struct mc_softc *sc;
    683 	caddr_t pkt;
    684 	int totlen;
    685 {
    686 	register struct mbuf *m;
    687 	struct mbuf *top, **mp;
    688 	int len;
    689 
    690 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    691 	if (m == 0)
    692 		return (0);
    693 	m->m_pkthdr.rcvif = &sc->sc_if;
    694 	m->m_pkthdr.len = totlen;
    695 	len = MHLEN;
    696 	top = 0;
    697 	mp = &top;
    698 
    699 	while (totlen > 0) {
    700 		if (top) {
    701 			MGET(m, M_DONTWAIT, MT_DATA);
    702 			if (m == 0) {
    703 				m_freem(top);
    704 				return 0;
    705 			}
    706 			len = MLEN;
    707 		}
    708 		if (totlen >= MINCLSIZE) {
    709 			MCLGET(m, M_DONTWAIT);
    710 			if ((m->m_flags & M_EXT) == 0) {
    711 				m_free(m);
    712 				m_freem(top);
    713 				return 0;
    714 			}
    715 			len = MCLBYTES;
    716 		}
    717 		m->m_len = len = min(totlen, len);
    718 		bcopy(pkt, mtod(m, caddr_t), len);
    719 		pkt += len;
    720 		totlen -= len;
    721 		*mp = m;
    722 		mp = &m->m_next;
    723 	}
    724 
    725 	return (top);
    726 }
    727 
    728 /*
    729  * Go through the list of multicast addresses and calculate the logical
    730  * address filter.
    731  */
    732 void
    733 mace_calcladrf(ac, af)
    734 	struct ethercom *ac;
    735 	u_int8_t *af;
    736 {
    737 	struct ifnet *ifp = &ac->ec_if;
    738 	struct ether_multi *enm;
    739 	register u_char *cp;
    740 	register u_int32_t crc;
    741 	static const u_int32_t crctab[] = {
    742 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
    743 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
    744 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
    745 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
    746 	};
    747 	register int len;
    748 	struct ether_multistep step;
    749 
    750 	/*
    751 	 * Set up multicast address filter by passing all multicast addresses
    752 	 * through a crc generator, and then using the high order 6 bits as an
    753 	 * index into the 64 bit logical address filter.  The high order bit
    754 	 * selects the word, while the rest of the bits select the bit within
    755 	 * the word.
    756 	 */
    757 
    758 	*((u_int32_t *)af) = *((u_int32_t *)af + 1) = 0;
    759 	ETHER_FIRST_MULTI(step, ac, enm);
    760 	while (enm != NULL) {
    761 		if (ETHER_CMP(enm->enm_addrlo, enm->enm_addrhi)) {
    762 			/*
    763 			 * We must listen to a range of multicast addresses.
    764 			 * For now, just accept all multicasts, rather than
    765 			 * trying to set only those filter bits needed to match
    766 			 * the range.  (At this time, the only use of address
    767 			 * ranges is for IP multicast routing, for which the
    768 			 * range is big enough to require all bits set.)
    769 			 */
    770 			goto allmulti;
    771 		}
    772 
    773 		cp = enm->enm_addrlo;
    774 		crc = 0xffffffff;
    775 		for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
    776 			crc ^= *cp++;
    777 			crc = (crc >> 4) ^ crctab[crc & 0xf];
    778 			crc = (crc >> 4) ^ crctab[crc & 0xf];
    779 		}
    780 		/* Just want the 6 most significant bits. */
    781 		crc >>= 26;
    782 
    783 		/* Set the corresponding bit in the filter. */
    784 		af[crc >> 3] |= 1 << (crc & 7);
    785 
    786 		ETHER_NEXT_MULTI(step, enm);
    787 	}
    788 	ifp->if_flags &= ~IFF_ALLMULTI;
    789 	return;
    790 
    791 allmulti:
    792 	ifp->if_flags |= IFF_ALLMULTI;
    793 	*((u_int32_t *)af) = *((u_int32_t *)af + 1) = 0xffffffff;
    794 }
    795 
    796 static u_char bbr4[] = {0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15};
    797 #define bbr(v)  ((bbr4[(v)&0xf] << 4) | bbr4[((v)>>4) & 0xf])
    798 
    799 u_char
    800 mc_get_enaddr(t, h, o, dst)
    801 	bus_space_tag_t t;
    802 	bus_space_handle_t h;
    803 	vm_offset_t o;
    804 	u_char *dst;
    805 {
    806 	int	i;
    807 	u_char	b, csum;
    808 
    809 	/*
    810 	 * The XOR of the 8 bytes of the ROM must be 0xff for it to be
    811 	 * valid
    812 	*/
    813 	for (i = 0, csum = 0; i < 8; i++) {
    814 		b = bus_space_read_1(t, h, o+16*i);
    815 		if (i < ETHER_ADDR_LEN)
    816 			dst[i] = bbr(b);
    817 		csum ^= b;
    818 	}
    819 
    820 	return csum;
    821 }
    822