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