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