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