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