Home | History | Annotate | Line # | Download | only in dev
if_mc.c revision 1.44.2.1
      1 /*	$NetBSD: if_mc.c,v 1.44.2.1 2017/04/21 16:53:30 bouyer 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.44.2.1 2017/04/21 16:53:30 bouyer 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 	if_deferred_start_init(ifp, NULL);
    164 	ether_ifattach(ifp, lladdr);
    165 
    166 	return (0);
    167 }
    168 
    169 hide int
    170 mcioctl(struct ifnet *ifp, u_long cmd, void *data)
    171 {
    172 	struct mc_softc *sc = ifp->if_softc;
    173 	struct ifaddr *ifa;
    174 
    175 	int	s = splnet(), err = 0;
    176 
    177 	switch (cmd) {
    178 
    179 	case SIOCINITIFADDR:
    180 		ifa = (struct ifaddr *)data;
    181 		ifp->if_flags |= IFF_UP;
    182 		mcinit(sc);
    183 		switch (ifa->ifa_addr->sa_family) {
    184 #ifdef INET
    185 		case AF_INET:
    186 			arp_ifinit(ifp, ifa);
    187 			break;
    188 #endif
    189 		default:
    190 			break;
    191 		}
    192 		break;
    193 
    194 	case SIOCSIFFLAGS:
    195 		if ((err = ifioctl_common(ifp, cmd, data)) != 0)
    196 			break;
    197 		/* XXX see the comment in ed_ioctl() about code re-use */
    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 = ether_ioctl(ifp, cmd, data);
    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 		/*
    263 		 * If bpf is listening on this interface, let it
    264 		 * see the packet before we commit it to the wire.
    265 		 */
    266 		bpf_mtap(ifp, m);
    267 
    268 		/*
    269 		 * Copy the mbuf chain into the transmit buffer.
    270 		 */
    271 		ifp->if_flags |= IFF_OACTIVE;
    272 		maceput(sc, m);
    273 
    274 		ifp->if_opackets++;		/* # of pkts */
    275 	}
    276 }
    277 
    278 /*
    279  * reset and restart the MACE.  Called in case of fatal
    280  * hardware/software errors.
    281  */
    282 hide void
    283 mcreset(struct mc_softc *sc)
    284 {
    285 	mcstop(sc);
    286 	mcinit(sc);
    287 }
    288 
    289 hide int
    290 mcinit(struct mc_softc *sc)
    291 {
    292 	int s;
    293 	u_int8_t maccc, ladrf[8];
    294 
    295 	if (sc->sc_if.if_flags & IFF_RUNNING)
    296 		/* already running */
    297 		return (0);
    298 
    299 	s = splnet();
    300 
    301 	NIC_PUT(sc, MACE_BIUCC, sc->sc_biucc);
    302 	NIC_PUT(sc, MACE_FIFOCC, sc->sc_fifocc);
    303 	NIC_PUT(sc, MACE_IMR, ~0); /* disable all interrupts */
    304 	NIC_PUT(sc, MACE_PLSCC, sc->sc_plscc);
    305 
    306 	NIC_PUT(sc, MACE_UTR, RTRD); /* disable reserved test registers */
    307 
    308 	/* set MAC address */
    309 	NIC_PUT(sc, MACE_IAC, ADDRCHG);
    310 	while (NIC_GET(sc, MACE_IAC) & ADDRCHG)
    311 		;
    312 	NIC_PUT(sc, MACE_IAC, PHYADDR);
    313 	bus_space_write_multi_1(sc->sc_regt, sc->sc_regh, MACE_REG(MACE_PADR),
    314 	    sc->sc_enaddr, ETHER_ADDR_LEN);
    315 
    316 	/* set logical address filter */
    317 	mace_calcladrf(&sc->sc_ethercom, ladrf);
    318 
    319 	NIC_PUT(sc, MACE_IAC, ADDRCHG);
    320 	while (NIC_GET(sc, MACE_IAC) & ADDRCHG)
    321 		;
    322 	NIC_PUT(sc, MACE_IAC, LOGADDR);
    323 	bus_space_write_multi_1(sc->sc_regt, sc->sc_regh, MACE_REG(MACE_LADRF),
    324 	    ladrf, 8);
    325 
    326 	NIC_PUT(sc, MACE_XMTFC, APADXMT);
    327 	/*
    328 	 * No need to autostrip padding on receive... Ethernet frames
    329 	 * don't have a length field, unlike 802.3 frames, so the MACE
    330 	 * can't figure out the length of the packet anyways.
    331 	 */
    332 	NIC_PUT(sc, MACE_RCVFC, 0);
    333 
    334 	maccc = ENXMT | ENRCV;
    335 	if (sc->sc_if.if_flags & IFF_PROMISC)
    336 		maccc |= PROM;
    337 
    338 	NIC_PUT(sc, MACE_MACCC, maccc);
    339 
    340 	if (sc->sc_bus_init)
    341 		(*sc->sc_bus_init)(sc);
    342 
    343 	/*
    344 	 * Enable all interrupts except receive, since we use the DMA
    345 	 * completion interrupt for that.
    346 	 */
    347 	NIC_PUT(sc, MACE_IMR, RCVINTM);
    348 
    349 	/* flag interface as "running" */
    350 	sc->sc_if.if_flags |= IFF_RUNNING;
    351 	sc->sc_if.if_flags &= ~IFF_OACTIVE;
    352 
    353 	splx(s);
    354 	return (0);
    355 }
    356 
    357 /*
    358  * close down an interface and free its buffers
    359  * Called on final close of device, or if mcinit() fails
    360  * part way through.
    361  */
    362 hide int
    363 mcstop(struct mc_softc *sc)
    364 {
    365 	int s;
    366 
    367 	s = splnet();
    368 
    369 	NIC_PUT(sc, MACE_BIUCC, SWRST);
    370 	DELAY(100);
    371 
    372 	sc->sc_if.if_timer = 0;
    373 	sc->sc_if.if_flags &= ~IFF_RUNNING;
    374 
    375 	splx(s);
    376 	return (0);
    377 }
    378 
    379 /*
    380  * Called if any Tx packets remain unsent after 5 seconds,
    381  * In all cases we just reset the chip, and any retransmission
    382  * will be handled by higher level protocol timeouts.
    383  */
    384 hide void
    385 mcwatchdog(struct ifnet *ifp)
    386 {
    387 	struct mc_softc *sc = ifp->if_softc;
    388 
    389 	printf("mcwatchdog: resetting chip\n");
    390 	mcreset(sc);
    391 }
    392 
    393 /*
    394  * stuff packet into MACE (at splnet)
    395  */
    396 integrate u_int
    397 maceput(struct mc_softc *sc, struct mbuf *m)
    398 {
    399 	struct mbuf *n;
    400 	u_int len, totlen = 0;
    401 	u_char *buff;
    402 
    403 	buff = (u_char*)sc->sc_txbuf + (sc->sc_txset == 0 ? 0 : 0x800);
    404 
    405 	for (; m; m = n) {
    406 		u_char *data = mtod(m, u_char *);
    407 		len = m->m_len;
    408 		totlen += len;
    409 		memcpy(buff, data, len);
    410 		buff += len;
    411 		n = m_free(m);
    412 	}
    413 
    414 	if (totlen > PAGE_SIZE)
    415 		panic("%s: maceput: packet overflow", device_xname(sc->sc_dev));
    416 
    417 #if 0
    418 	if (totlen < ETHERMIN + sizeof(struct ether_header)) {
    419 		int pad = ETHERMIN + sizeof(struct ether_header) - totlen;
    420 		memset(sc->sc_txbuf + totlen, 0, pad);
    421 		totlen = ETHERMIN + sizeof(struct ether_header);
    422 	}
    423 #endif
    424 
    425 	(*sc->sc_putpacket)(sc, totlen);
    426 
    427 	sc->sc_if.if_timer = 5;	/* 5 seconds to watch for failing to transmit */
    428 	return (totlen);
    429 }
    430 
    431 void
    432 mcintr(void *arg)
    433 {
    434 struct mc_softc *sc = arg;
    435 	u_int8_t ir;
    436 
    437 	ir = NIC_GET(sc, MACE_IR) & ~NIC_GET(sc, MACE_IMR);
    438 	if (ir & JAB) {
    439 #ifdef MCDEBUG
    440 		printf("%s: jabber error\n", device_xname(sc->sc_dev));
    441 #endif
    442 		sc->sc_if.if_oerrors++;
    443 	}
    444 
    445 	if (ir & BABL) {
    446 #ifdef MCDEBUG
    447 		printf("%s: babble\n", device_xname(sc->sc_dev));
    448 #endif
    449 		sc->sc_if.if_oerrors++;
    450 	}
    451 
    452 	if (ir & CERR) {
    453 #ifdef MCDEBUG
    454 		printf("%s: collision error\n", device_xname(sc->sc_dev));
    455 #endif
    456 		sc->sc_if.if_collisions++;
    457 	}
    458 
    459 	/*
    460 	 * Pretend we have carrier; if we don't this will be cleared
    461 	 * shortly.
    462 	 */
    463 	sc->sc_havecarrier = 1;
    464 
    465 	if (ir & XMTINT)
    466 		mc_tint(sc);
    467 
    468 	if (ir & RCVINT)
    469 		mc_rint(sc);
    470 }
    471 
    472 integrate void
    473 mc_tint(struct mc_softc *sc)
    474 {
    475 	u_int8_t /* xmtrc,*/ xmtfs;
    476 
    477 	/* xmtrc = */ NIC_GET(sc, MACE_XMTRC);
    478 	xmtfs = NIC_GET(sc, MACE_XMTFS);
    479 
    480 	if ((xmtfs & XMTSV) == 0)
    481 		return;
    482 
    483 	if (xmtfs & UFLO) {
    484 		printf("%s: underflow\n", device_xname(sc->sc_dev));
    485 		mcreset(sc);
    486 		return;
    487 	}
    488 
    489 	if (xmtfs & LCOL) {
    490 		printf("%s: late collision\n", device_xname(sc->sc_dev));
    491 		sc->sc_if.if_oerrors++;
    492 		sc->sc_if.if_collisions++;
    493 	}
    494 
    495 	if (xmtfs & MORE)
    496 		/* Real number is unknown. */
    497 		sc->sc_if.if_collisions += 2;
    498 	else if (xmtfs & ONE)
    499 		sc->sc_if.if_collisions++;
    500 	else if (xmtfs & RTRY) {
    501 		printf("%s: excessive collisions\n", device_xname(sc->sc_dev));
    502 		sc->sc_if.if_collisions += 16;
    503 		sc->sc_if.if_oerrors++;
    504 	}
    505 
    506 	if (xmtfs & LCAR) {
    507 		sc->sc_havecarrier = 0;
    508 		printf("%s: lost carrier\n", device_xname(sc->sc_dev));
    509 		sc->sc_if.if_oerrors++;
    510 	}
    511 
    512 	sc->sc_if.if_flags &= ~IFF_OACTIVE;
    513 	sc->sc_if.if_timer = 0;
    514 	if_schedule_deferred_start(&sc->sc_if);
    515 }
    516 
    517 void
    518 mc_rint(struct mc_softc *sc)
    519 {
    520 #define	rxf	sc->sc_rxframe
    521 	u_int len;
    522 
    523 	len = (rxf.rx_rcvcnt | ((rxf.rx_rcvsts & 0xf) << 8)) - 4;
    524 
    525 #ifdef MCDEBUG
    526 	if (rxf.rx_rcvsts & 0xf0)
    527 		printf("%s: rcvcnt %02x rcvsts %02x rntpc 0x%02x rcvcc 0x%02x\n",
    528 		    device_xname(sc->sc_dev), rxf.rx_rcvcnt, rxf.rx_rcvsts,
    529 		    rxf.rx_rntpc, rxf.rx_rcvcc);
    530 #endif
    531 
    532 	if (rxf.rx_rcvsts & OFLO) {
    533 		printf("%s: receive FIFO overflow\n", device_xname(sc->sc_dev));
    534 		sc->sc_if.if_ierrors++;
    535 		return;
    536 	}
    537 
    538 	if (rxf.rx_rcvsts & CLSN)
    539 		sc->sc_if.if_collisions++;
    540 
    541 	if (rxf.rx_rcvsts & FRAM) {
    542 #ifdef MCDEBUG
    543 		printf("%s: framing error\n", device_xname(sc->sc_dev));
    544 #endif
    545 		sc->sc_if.if_ierrors++;
    546 		return;
    547 	}
    548 
    549 	if (rxf.rx_rcvsts & FCS) {
    550 #ifdef MCDEBUG
    551 		printf("%s: frame control checksum error\n", device_xname(sc->sc_dev));
    552 #endif
    553 		sc->sc_if.if_ierrors++;
    554 		return;
    555 	}
    556 
    557 	mace_read(sc, rxf.rx_frame, len);
    558 #undef	rxf
    559 }
    560 
    561 integrate void
    562 mace_read(struct mc_softc *sc, void *pkt, int len)
    563 {
    564 	struct ifnet *ifp = &sc->sc_if;
    565 	struct mbuf *m;
    566 
    567 	if (len <= sizeof(struct ether_header) ||
    568 	    len > ETHERMTU + sizeof(struct ether_header)) {
    569 #ifdef MCDEBUG
    570 		printf("%s: invalid packet size %d; dropping\n",
    571 		    device_xname(sc->sc_dev), len);
    572 #endif
    573 		ifp->if_ierrors++;
    574 		return;
    575 	}
    576 
    577 	m = mace_get(sc, pkt, len);
    578 	if (m == NULL) {
    579 		ifp->if_ierrors++;
    580 		return;
    581 	}
    582 
    583 	/* Pass the packet up. */
    584 	if_percpuq_enqueue(ifp->if_percpuq, m);
    585 }
    586 
    587 /*
    588  * Pull data off an interface.
    589  * Len is length of data, with local net header stripped.
    590  * We copy the data into mbufs.  When full cluster sized units are present
    591  * we copy into clusters.
    592  */
    593 integrate struct mbuf *
    594 mace_get(struct mc_softc *sc, void *pkt, int totlen)
    595 {
    596 	struct mbuf *m;
    597 	struct mbuf *top, **mp;
    598 	int len;
    599 
    600 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    601 	if (m == 0)
    602 		return (0);
    603 	m_set_rcvif(m, &sc->sc_if);
    604 	m->m_pkthdr.len = totlen;
    605 	len = MHLEN;
    606 	top = 0;
    607 	mp = &top;
    608 
    609 	while (totlen > 0) {
    610 		if (top) {
    611 			MGET(m, M_DONTWAIT, MT_DATA);
    612 			if (m == 0) {
    613 				m_freem(top);
    614 				return 0;
    615 			}
    616 			len = MLEN;
    617 		}
    618 		if (totlen >= MINCLSIZE) {
    619 			MCLGET(m, M_DONTWAIT);
    620 			if ((m->m_flags & M_EXT) == 0) {
    621 				m_free(m);
    622 				m_freem(top);
    623 				return 0;
    624 			}
    625 			len = MCLBYTES;
    626 		}
    627 		m->m_len = len = min(totlen, len);
    628 		memcpy(mtod(m, void *), pkt, len);
    629 		pkt = (char*)pkt + len;
    630 		totlen -= len;
    631 		*mp = m;
    632 		mp = &m->m_next;
    633 	}
    634 
    635 	return (top);
    636 }
    637 
    638 /*
    639  * Go through the list of multicast addresses and calculate the logical
    640  * address filter.
    641  */
    642 void
    643 mace_calcladrf(struct ethercom *ac, u_int8_t *af)
    644 {
    645 	struct ifnet *ifp = &ac->ec_if;
    646 	struct ether_multi *enm;
    647 	u_char *cp;
    648 	u_int32_t crc;
    649 	static const u_int32_t crctab[] = {
    650 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
    651 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
    652 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
    653 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
    654 	};
    655 	int len;
    656 	struct ether_multistep step;
    657 
    658 	/*
    659 	 * Set up multicast address filter by passing all multicast addresses
    660 	 * through a crc generator, and then using the high order 6 bits as an
    661 	 * index into the 64 bit logical address filter.  The high order bit
    662 	 * selects the word, while the rest of the bits select the bit within
    663 	 * the word.
    664 	 */
    665 
    666 	*((u_int32_t *)af) = *((u_int32_t *)af + 1) = 0;
    667 	ETHER_FIRST_MULTI(step, ac, enm);
    668 	while (enm != NULL) {
    669 		if (ETHER_CMP(enm->enm_addrlo, enm->enm_addrhi)) {
    670 			/*
    671 			 * We must listen to a range of multicast addresses.
    672 			 * For now, just accept all multicasts, rather than
    673 			 * trying to set only those filter bits needed to match
    674 			 * the range.  (At this time, the only use of address
    675 			 * ranges is for IP multicast routing, for which the
    676 			 * range is big enough to require all bits set.)
    677 			 */
    678 			goto allmulti;
    679 		}
    680 
    681 		cp = enm->enm_addrlo;
    682 		crc = 0xffffffff;
    683 		for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
    684 			crc ^= *cp++;
    685 			crc = (crc >> 4) ^ crctab[crc & 0xf];
    686 			crc = (crc >> 4) ^ crctab[crc & 0xf];
    687 		}
    688 		/* Just want the 6 most significant bits. */
    689 		crc >>= 26;
    690 
    691 		/* Set the corresponding bit in the filter. */
    692 		af[crc >> 3] |= 1 << (crc & 7);
    693 
    694 		ETHER_NEXT_MULTI(step, enm);
    695 	}
    696 	ifp->if_flags &= ~IFF_ALLMULTI;
    697 	return;
    698 
    699 allmulti:
    700 	ifp->if_flags |= IFF_ALLMULTI;
    701 	*((u_int32_t *)af) = *((u_int32_t *)af + 1) = 0xffffffff;
    702 }
    703 
    704 static u_char bbr4[] = {0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15};
    705 #define bbr(v)  ((bbr4[(v)&0xf] << 4) | bbr4[((v)>>4) & 0xf])
    706 
    707 u_char
    708 mc_get_enaddr(bus_space_tag_t t, bus_space_handle_t h, bus_size_t o,
    709     u_char *dst)
    710 {
    711 	int	i;
    712 	u_char	b, csum;
    713 
    714 	/*
    715 	 * The XOR of the 8 bytes of the ROM must be 0xff for it to be
    716 	 * valid
    717 	*/
    718 	for (i = 0, csum = 0; i < 8; i++) {
    719 		b = bus_space_read_1(t, h, o+16*i);
    720 		if (i < ETHER_ADDR_LEN)
    721 			dst[i] = bbr(b);
    722 		csum ^= b;
    723 	}
    724 
    725 	return csum;
    726 }
    727