Home | History | Annotate | Line # | Download | only in dev
if_mc.c revision 1.15.4.1
      1 /*	$NetBSD: if_mc.c,v 1.15.4.1 2003/06/17 04:54:56 msaitoh 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 
    200 	switch (cmd) {
    201 
    202 	case SIOCSIFADDR:
    203 		ifa = (struct ifaddr *)data;
    204 		ifp->if_flags |= IFF_UP;
    205 		switch (ifa->ifa_addr->sa_family) {
    206 #ifdef INET
    207 		case AF_INET:
    208 			mcinit(sc);
    209 			arp_ifinit(ifp, ifa);
    210 			break;
    211 #endif
    212 #ifdef NS
    213 		case AF_NS:
    214 		    {
    215 			register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
    216 
    217 			if (ns_nullhost(*ina))
    218 				ina->x_host =
    219 				    *(union ns_host *)LLADDR(ifp->if_sadl);
    220 			else {
    221 				bcopy(ina->x_host.c_host,
    222 				    LLADDR(ifp->if_sadl),
    223 				    sizeof(sc->sc_enaddr));
    224 			}
    225 			/* Set new address. */
    226 			mcinit(sc);
    227 			break;
    228 		    }
    229 #endif
    230 		default:
    231 			mcinit(sc);
    232 			break;
    233 		}
    234 		break;
    235 
    236 	case SIOCSIFFLAGS:
    237 		if ((ifp->if_flags & IFF_UP) == 0 &&
    238 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    239 			/*
    240 			 * If interface is marked down and it is running,
    241 			 * then stop it.
    242 			 */
    243 			mcstop(sc);
    244 			ifp->if_flags &= ~IFF_RUNNING;
    245 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
    246 		    (ifp->if_flags & IFF_RUNNING) == 0) {
    247 			/*
    248 			 * If interface is marked up and it is stopped,
    249 			 * then start it.
    250 			 */
    251 			(void)mcinit(sc);
    252 		} else {
    253 			/*
    254 			 * reset the interface to pick up any other changes
    255 			 * in flags
    256 			 */
    257 			mcreset(sc);
    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 			mcreset(sc);
    275 			err = 0;
    276 		}
    277 		break;
    278 	default:
    279 		err = EINVAL;
    280 	}
    281 	splx(s);
    282 	return (err);
    283 }
    284 
    285 /*
    286  * Encapsulate a packet of type family for the local net.
    287  */
    288 hide void
    289 mcstart(ifp)
    290 	struct ifnet *ifp;
    291 {
    292 	struct mc_softc	*sc = ifp->if_softc;
    293 	struct mbuf	*m;
    294 
    295 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
    296 		return;
    297 
    298 	while (1) {
    299 		if (ifp->if_flags & IFF_OACTIVE)
    300 			return;
    301 
    302 		IF_DEQUEUE(&ifp->if_snd, m);
    303 		if (m == 0)
    304 			return;
    305 
    306 #if NBPFILTER > 0
    307 		/*
    308 		 * If bpf is listening on this interface, let it
    309 		 * see the packet before we commit it to the wire.
    310 		 */
    311 		if (ifp->if_bpf)
    312 			bpf_mtap(ifp->if_bpf, m);
    313 #endif
    314 
    315 		/*
    316 		 * Copy the mbuf chain into the transmit buffer.
    317 		 */
    318 		ifp->if_flags |= IFF_OACTIVE;
    319 		maceput(sc, m);
    320 
    321 		ifp->if_opackets++;		/* # of pkts */
    322 	}
    323 }
    324 
    325 /*
    326  * reset and restart the MACE.  Called in case of fatal
    327  * hardware/software errors.
    328  */
    329 hide void
    330 mcreset(sc)
    331 	struct mc_softc *sc;
    332 {
    333 	mcstop(sc);
    334 	mcinit(sc);
    335 }
    336 
    337 hide int
    338 mcinit(sc)
    339 	struct mc_softc *sc;
    340 {
    341 	int s;
    342 	u_int8_t maccc, ladrf[8];
    343 
    344 	if (sc->sc_if.if_flags & IFF_RUNNING)
    345 		/* already running */
    346 		return (0);
    347 
    348 	s = splnet();
    349 
    350 	NIC_PUT(sc, MACE_BIUCC, sc->sc_biucc);
    351 	NIC_PUT(sc, MACE_FIFOCC, sc->sc_fifocc);
    352 	NIC_PUT(sc, MACE_IMR, ~0); /* disable all interrupts */
    353 	NIC_PUT(sc, MACE_PLSCC, sc->sc_plscc);
    354 
    355 	NIC_PUT(sc, MACE_UTR, RTRD); /* disable reserved test registers */
    356 
    357 	/* set MAC address */
    358 	NIC_PUT(sc, MACE_IAC, ADDRCHG);
    359 	while (NIC_GET(sc, MACE_IAC) & ADDRCHG)
    360 		;
    361 	NIC_PUT(sc, MACE_IAC, PHYADDR);
    362 	bus_space_write_multi_1(sc->sc_regt, sc->sc_regh, MACE_REG(MACE_PADR),
    363 	    sc->sc_enaddr, ETHER_ADDR_LEN);
    364 
    365 	/* set logical address filter */
    366 	mace_calcladrf(&sc->sc_ethercom, ladrf);
    367 
    368 	NIC_PUT(sc, MACE_IAC, ADDRCHG);
    369 	while (NIC_GET(sc, MACE_IAC) & ADDRCHG)
    370 		;
    371 	NIC_PUT(sc, MACE_IAC, LOGADDR);
    372 	bus_space_write_multi_1(sc->sc_regt, sc->sc_regh, MACE_REG(MACE_LADRF),
    373 	    ladrf, 8);
    374 
    375 	NIC_PUT(sc, MACE_XMTFC, APADXMT);
    376 	/*
    377 	 * No need to autostrip padding on receive... Ethernet frames
    378 	 * don't have a length field, unlike 802.3 frames, so the MACE
    379 	 * can't figure out the length of the packet anyways.
    380 	 */
    381 	NIC_PUT(sc, MACE_RCVFC, 0);
    382 
    383 	maccc = ENXMT | ENRCV;
    384 	if (sc->sc_if.if_flags & IFF_PROMISC)
    385 		maccc |= PROM;
    386 
    387 	NIC_PUT(sc, MACE_MACCC, maccc);
    388 
    389 	if (sc->sc_bus_init)
    390 		(*sc->sc_bus_init)(sc);
    391 
    392 	/*
    393 	 * Enable all interrupts except receive, since we use the DMA
    394 	 * completion interrupt for that.
    395 	 */
    396 	NIC_PUT(sc, MACE_IMR, RCVINTM);
    397 
    398 	/* flag interface as "running" */
    399 	sc->sc_if.if_flags |= IFF_RUNNING;
    400 	sc->sc_if.if_flags &= ~IFF_OACTIVE;
    401 
    402 	splx(s);
    403 	return (0);
    404 }
    405 
    406 /*
    407  * close down an interface and free its buffers
    408  * Called on final close of device, or if mcinit() fails
    409  * part way through.
    410  */
    411 hide int
    412 mcstop(sc)
    413 	struct mc_softc *sc;
    414 {
    415 	int	s = splnet();
    416 
    417 	NIC_PUT(sc, MACE_BIUCC, SWRST);
    418 	DELAY(100);
    419 
    420 	sc->sc_if.if_timer = 0;
    421 	sc->sc_if.if_flags &= ~IFF_RUNNING;
    422 
    423 	splx(s);
    424 	return (0);
    425 }
    426 
    427 /*
    428  * Called if any Tx packets remain unsent after 5 seconds,
    429  * In all cases we just reset the chip, and any retransmission
    430  * will be handled by higher level protocol timeouts.
    431  */
    432 hide void
    433 mcwatchdog(ifp)
    434 	struct ifnet *ifp;
    435 {
    436 	struct mc_softc *sc = ifp->if_softc;
    437 
    438 	printf("mcwatchdog: resetting chip\n");
    439 	mcreset(sc);
    440 }
    441 
    442 /*
    443  * stuff packet into MACE (at splnet)
    444  */
    445 integrate u_int
    446 maceput(sc, m)
    447 	struct mc_softc *sc;
    448 	struct mbuf *m;
    449 {
    450 	struct mbuf *n;
    451 	u_int len, totlen = 0;
    452 	u_char *buff;
    453 
    454 	buff = sc->sc_txbuf;
    455 
    456 	for (; m; m = n) {
    457 		u_char *data = mtod(m, u_char *);
    458 		len = m->m_len;
    459 		totlen += len;
    460 		bcopy(data, buff, len);
    461 		buff += len;
    462 		MFREE(m, n);
    463 	}
    464 
    465 	if (totlen > NBPG)
    466 		panic("%s: maceput: packet overflow", sc->sc_dev.dv_xname);
    467 
    468 #if 0
    469 	if (totlen < ETHERMIN + sizeof(struct ether_header)) {
    470 		int pad = ETHERMIN + sizeof(struct ether_header) - totlen;
    471 		bzero(sc->sc_txbuf + totlen, pad);
    472 		totlen = ETHERMIN + sizeof(struct ether_header);
    473 	}
    474 #endif
    475 
    476 	(*sc->sc_putpacket)(sc, totlen);
    477 
    478 	sc->sc_if.if_timer = 5;	/* 5 seconds to watch for failing to transmit */
    479 	return (totlen);
    480 }
    481 
    482 void
    483 mcintr(arg)
    484 	void *arg;
    485 {
    486 struct mc_softc *sc = arg;
    487 	u_int8_t ir;
    488 
    489 	ir = NIC_GET(sc, MACE_IR) & ~NIC_GET(sc, MACE_IMR);
    490 	if (ir & JAB) {
    491 #ifdef MCDEBUG
    492 		printf("%s: jabber error\n", sc->sc_dev.dv_xname);
    493 #endif
    494 		sc->sc_if.if_oerrors++;
    495 	}
    496 
    497 	if (ir & BABL) {
    498 #ifdef MCDEBUG
    499 		printf("%s: babble\n", sc->sc_dev.dv_xname);
    500 #endif
    501 		sc->sc_if.if_oerrors++;
    502 	}
    503 
    504 	if (ir & CERR) {
    505 #ifdef MCDEBUG
    506 		printf("%s: collision error\n", sc->sc_dev.dv_xname);
    507 #endif
    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 		printf("%s: excessive collisions\n", sc->sc_dev.dv_xname);
    555 		sc->sc_if.if_collisions += 16;
    556 		sc->sc_if.if_oerrors++;
    557 	}
    558 
    559 	if (xmtfs & LCAR) {
    560 		sc->sc_havecarrier = 0;
    561 		printf("%s: lost carrier\n", sc->sc_dev.dv_xname);
    562 		sc->sc_if.if_oerrors++;
    563 	}
    564 
    565 	sc->sc_if.if_flags &= ~IFF_OACTIVE;
    566 	sc->sc_if.if_timer = 0;
    567 	mcstart(&sc->sc_if);
    568 }
    569 
    570 void
    571 mc_rint(sc)
    572 	struct mc_softc *sc;
    573 {
    574 #define	rxf	sc->sc_rxframe
    575 	u_int len;
    576 
    577 	len = (rxf.rx_rcvcnt | ((rxf.rx_rcvsts & 0xf) << 8)) - 4;
    578 
    579 #ifdef MCDEBUG
    580 	if (rxf.rx_rcvsts & 0xf0)
    581 		printf("%s: rcvcnt %02x rcvsts %02x rntpc 0x%02x rcvcc 0x%02x\n",
    582 		    sc->sc_dev.dv_xname, rxf.rx_rcvcnt, rxf.rx_rcvsts,
    583 		    rxf.rx_rntpc, rxf.rx_rcvcc);
    584 #endif
    585 
    586 	if (rxf.rx_rcvsts & OFLO) {
    587 		printf("%s: receive FIFO overflow\n", sc->sc_dev.dv_xname);
    588 		sc->sc_if.if_ierrors++;
    589 		return;
    590 	}
    591 
    592 	if (rxf.rx_rcvsts & CLSN)
    593 		sc->sc_if.if_collisions++;
    594 
    595 	if (rxf.rx_rcvsts & FRAM) {
    596 #ifdef MCDEBUG
    597 		printf("%s: framing error\n", sc->sc_dev.dv_xname);
    598 #endif
    599 		sc->sc_if.if_ierrors++;
    600 		return;
    601 	}
    602 
    603 	if (rxf.rx_rcvsts & FCS) {
    604 #ifdef MCDEBUG
    605 		printf("%s: frame control checksum error\n", sc->sc_dev.dv_xname);
    606 #endif
    607 		sc->sc_if.if_ierrors++;
    608 		return;
    609 	}
    610 
    611 	mace_read(sc, rxf.rx_frame, len);
    612 #undef	rxf
    613 }
    614 
    615 integrate void
    616 mace_read(sc, pkt, len)
    617 	struct mc_softc *sc;
    618 	caddr_t pkt;
    619 	int len;
    620 {
    621 	struct ifnet *ifp = &sc->sc_if;
    622 #if NBPFILTER > 0
    623 	struct ether_header *eh = (struct ether_header *)pkt;
    624 #endif
    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. */
    660 	(*ifp->if_input)(ifp, m);
    661 }
    662 
    663 /*
    664  * Pull data off an interface.
    665  * Len is length of data, with local net header stripped.
    666  * We copy the data into mbufs.  When full cluster sized units are present
    667  * we copy into clusters.
    668  */
    669 integrate struct mbuf *
    670 mace_get(sc, pkt, totlen)
    671 	struct mc_softc *sc;
    672 	caddr_t pkt;
    673 	int totlen;
    674 {
    675 	register struct mbuf *m;
    676 	struct mbuf *top, **mp;
    677 	int len;
    678 
    679 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    680 	if (m == 0)
    681 		return (0);
    682 	m->m_pkthdr.rcvif = &sc->sc_if;
    683 	m->m_pkthdr.len = totlen;
    684 	len = MHLEN;
    685 	top = 0;
    686 	mp = &top;
    687 
    688 	while (totlen > 0) {
    689 		if (top) {
    690 			MGET(m, M_DONTWAIT, MT_DATA);
    691 			if (m == 0) {
    692 				m_freem(top);
    693 				return 0;
    694 			}
    695 			len = MLEN;
    696 		}
    697 		if (totlen >= MINCLSIZE) {
    698 			MCLGET(m, M_DONTWAIT);
    699 			if ((m->m_flags & M_EXT) == 0) {
    700 				m_free(m);
    701 				m_freem(top);
    702 				return 0;
    703 			}
    704 			len = MCLBYTES;
    705 		}
    706 		m->m_len = len = min(totlen, len);
    707 		bcopy(pkt, mtod(m, caddr_t), len);
    708 		pkt += len;
    709 		totlen -= len;
    710 		*mp = m;
    711 		mp = &m->m_next;
    712 	}
    713 
    714 	return (top);
    715 }
    716 
    717 /*
    718  * Go through the list of multicast addresses and calculate the logical
    719  * address filter.
    720  */
    721 void
    722 mace_calcladrf(ac, af)
    723 	struct ethercom *ac;
    724 	u_int8_t *af;
    725 {
    726 	struct ifnet *ifp = &ac->ec_if;
    727 	struct ether_multi *enm;
    728 	register u_char *cp;
    729 	register u_int32_t crc;
    730 	static const u_int32_t crctab[] = {
    731 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
    732 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
    733 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
    734 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
    735 	};
    736 	register int len;
    737 	struct ether_multistep step;
    738 
    739 	/*
    740 	 * Set up multicast address filter by passing all multicast addresses
    741 	 * through a crc generator, and then using the high order 6 bits as an
    742 	 * index into the 64 bit logical address filter.  The high order bit
    743 	 * selects the word, while the rest of the bits select the bit within
    744 	 * the word.
    745 	 */
    746 
    747 	*((u_int32_t *)af) = *((u_int32_t *)af + 1) = 0;
    748 	ETHER_FIRST_MULTI(step, ac, enm);
    749 	while (enm != NULL) {
    750 		if (ETHER_CMP(enm->enm_addrlo, enm->enm_addrhi)) {
    751 			/*
    752 			 * We must listen to a range of multicast addresses.
    753 			 * For now, just accept all multicasts, rather than
    754 			 * trying to set only those filter bits needed to match
    755 			 * the range.  (At this time, the only use of address
    756 			 * ranges is for IP multicast routing, for which the
    757 			 * range is big enough to require all bits set.)
    758 			 */
    759 			goto allmulti;
    760 		}
    761 
    762 		cp = enm->enm_addrlo;
    763 		crc = 0xffffffff;
    764 		for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
    765 			crc ^= *cp++;
    766 			crc = (crc >> 4) ^ crctab[crc & 0xf];
    767 			crc = (crc >> 4) ^ crctab[crc & 0xf];
    768 		}
    769 		/* Just want the 6 most significant bits. */
    770 		crc >>= 26;
    771 
    772 		/* Set the corresponding bit in the filter. */
    773 		af[crc >> 3] |= 1 << (crc & 7);
    774 
    775 		ETHER_NEXT_MULTI(step, enm);
    776 	}
    777 	ifp->if_flags &= ~IFF_ALLMULTI;
    778 	return;
    779 
    780 allmulti:
    781 	ifp->if_flags |= IFF_ALLMULTI;
    782 	*((u_int32_t *)af) = *((u_int32_t *)af + 1) = 0xffffffff;
    783 }
    784 
    785 static u_char bbr4[] = {0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15};
    786 #define bbr(v)  ((bbr4[(v)&0xf] << 4) | bbr4[((v)>>4) & 0xf])
    787 
    788 u_char
    789 mc_get_enaddr(t, h, o, dst)
    790 	bus_space_tag_t t;
    791 	bus_space_handle_t h;
    792 	bus_size_t o;
    793 	u_char *dst;
    794 {
    795 	int	i;
    796 	u_char	b, csum;
    797 
    798 	/*
    799 	 * The XOR of the 8 bytes of the ROM must be 0xff for it to be
    800 	 * valid
    801 	*/
    802 	for (i = 0, csum = 0; i < 8; i++) {
    803 		b = bus_space_read_1(t, h, o+16*i);
    804 		if (i < ETHER_ADDR_LEN)
    805 			dst[i] = bbr(b);
    806 		csum ^= b;
    807 	}
    808 
    809 	return csum;
    810 }
    811