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