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