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