xref: /netbsd-src/sys/arch/mac68k/dev/if_mc.c (revision 627f7eb200a4419d89b531d55fccd2ee3ffdcde0)
1 /*	$NetBSD: if_mc.c,v 1.57 2021/01/24 05:20:23 rin Exp $	*/
2 
3 /*-
4  * Copyright (c) 1997 David Huang <khym@azeotrope.org>
5  * All rights reserved.
6  *
7  * Portions of this code are based on code by Denton Gentry <denny1@home.com>,
8  * Charles M. Hannum, Yanagisawa Takeshi <yanagisw@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.57 2021/01/24 05:20:23 rin 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 <sys/rndsource.h>
55 
56 #include <uvm/uvm_extern.h>
57 
58 #include <net/if.h>
59 #include <net/if_dl.h>
60 #include <net/if_ether.h>
61 #include <net/bpf.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 #include <machine/bus.h>
72 #include <mac68k/dev/if_mcreg.h>
73 #include <mac68k/dev/if_mcvar.h>
74 
75 hide void	mcwatchdog(struct ifnet *);
76 hide int	mcinit(struct mc_softc *);
77 hide int	mcstop(struct mc_softc *);
78 hide int	mcioctl(struct ifnet *, u_long, void *);
79 hide void	mcstart(struct ifnet *);
80 hide void	mcreset(struct mc_softc *);
81 
82 integrate u_int	maceput(struct mc_softc *, struct mbuf *);
83 integrate void	mc_tint(struct mc_softc *);
84 integrate void	mace_read(struct mc_softc *, void *, int);
85 integrate struct mbuf *mace_get(struct mc_softc *, void *, int);
86 static void mace_calcladrf(struct ethercom *, uint8_t *);
87 static inline uint16_t ether_cmp(void *, void *);
88 
89 
90 /*
91  * Compare two Ether/802 addresses for equality, inlined and
92  * unrolled for speed.  Use this like memcmp().
93  *
94  * XXX: Add <machine/inlines.h> for stuff like this?
95  * XXX: or maybe add it to libkern.h instead?
96  *
97  * "I'd love to have an inline assembler version of this."
98  * XXX: Who wanted that? mycroft?  I wrote one, but this
99  * version in C is as good as hand-coded assembly. -gwr
100  *
101  * Please do NOT tweak this without looking at the actual
102  * assembly code generated before and after your tweaks!
103  */
104 static inline uint16_t
105 ether_cmp(void *one, void *two)
106 {
107 	uint16_t *a = (u_short *) one;
108 	uint16_t *b = (u_short *) two;
109 	uint16_t diff;
110 
111 #ifdef	m68k
112 	/*
113 	 * The post-increment-pointer form produces the best
114 	 * machine code for m68k.  This was carefully tuned
115 	 * so it compiles to just 8 short (2-byte) op-codes!
116 	 */
117 	diff  = *a++ - *b++;
118 	diff |= *a++ - *b++;
119 	diff |= *a++ - *b++;
120 #else
121 	/*
122 	 * Most modern CPUs do better with a single expression.
123 	 * Note that short-cut evaluation is NOT helpful here,
124 	 * because it just makes the code longer, not faster!
125 	 */
126 	diff = (a[0] - b[0]) | (a[1] - b[1]) | (a[2] - b[2]);
127 #endif
128 
129 	return diff;
130 }
131 
132 #define ETHER_CMP	ether_cmp
133 
134 /*
135  * Interface exists: make available by filling in network interface
136  * record.  System will initialize the interface when it is ready
137  * to accept packets.
138  */
139 int
140 mcsetup(struct mc_softc	*sc, uint8_t *lladdr)
141 {
142 	struct ifnet *ifp = &sc->sc_if;
143 
144 	/* reset the chip and disable all interrupts */
145 	NIC_PUT(sc, MACE_BIUCC, SWRST);
146 	DELAY(100);
147 	NIC_PUT(sc, MACE_IMR, ~0);
148 
149 	memcpy(sc->sc_enaddr, lladdr, ETHER_ADDR_LEN);
150 	printf(": address %s\n", ether_sprintf(lladdr));
151 
152 	memcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
153 	ifp->if_softc = sc;
154 	ifp->if_ioctl = mcioctl;
155 	ifp->if_start = mcstart;
156 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
157 	ifp->if_watchdog = mcwatchdog;
158 
159 	if_attach(ifp);
160 	if_deferred_start_init(ifp, NULL);
161 	ether_ifattach(ifp, lladdr);
162 
163 	rnd_attach_source(&sc->rnd_source, ifp->if_xname, RND_TYPE_NET,
164 	    RND_FLAG_DEFAULT);
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, BPF_D_OUT);
267 
268 		/*
269 		 * Copy the mbuf chain into the transmit buffer.
270 		 */
271 		ifp->if_flags |= IFF_OACTIVE;
272 		maceput(sc, m);
273 
274 		if_statinc(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 	uint8_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 	uint8_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 		if_statinc(&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 		if_statinc(&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 		if_statinc(&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 	const int ocarrier = sc->sc_havecarrier;
464 	sc->sc_havecarrier = 1;
465 
466 	if (ir & XMTINT)
467 		mc_tint(sc);
468 
469 	if (ir & RCVINT)
470 		mc_rint(sc);
471 
472 	if (sc->sc_havecarrier != ocarrier)
473 		if_link_state_change(&sc->sc_if,
474 		    sc->sc_havecarrier ? LINK_STATE_UP : LINK_STATE_DOWN);
475 }
476 
477 integrate void
478 mc_tint(struct mc_softc *sc)
479 {
480 	uint8_t /* xmtrc,*/ xmtfs;
481 
482 	/* xmtrc = */ NIC_GET(sc, MACE_XMTRC);
483 	xmtfs = NIC_GET(sc, MACE_XMTFS);
484 
485 	if ((xmtfs & XMTSV) == 0)
486 		return;
487 
488 	if (xmtfs & UFLO) {
489 		printf("%s: underflow\n", device_xname(sc->sc_dev));
490 		mcreset(sc);
491 		return;
492 	}
493 
494 	net_stat_ref_t nsr = IF_STAT_GETREF(&sc->sc_if);
495 
496 	if (xmtfs & LCOL) {
497 		printf("%s: late collision\n", device_xname(sc->sc_dev));
498 		if_statinc_ref(nsr, if_oerrors);
499 		if_statinc_ref(nsr, if_collisions);
500 	}
501 
502 	if (xmtfs & MORE)
503 		/* Real number is unknown. */
504 		if_statadd_ref(nsr, if_collisions, 2);
505 	else if (xmtfs & ONE)
506 		if_statinc_ref(nsr, if_collisions);
507 	else if (xmtfs & RTRY) {
508 		printf("%s: excessive collisions\n", device_xname(sc->sc_dev));
509 		if_statadd_ref(nsr, if_collisions, 16);
510 		if_statinc_ref(nsr, if_oerrors);
511 	}
512 
513 	if (xmtfs & LCAR) {
514 		sc->sc_havecarrier = 0;
515 		printf("%s: lost carrier\n", device_xname(sc->sc_dev));
516 		if_statinc_ref(nsr, if_oerrors);
517 	}
518 
519 	IF_STAT_PUTREF(&sc->sc_if);
520 
521 	sc->sc_if.if_flags &= ~IFF_OACTIVE;
522 	sc->sc_if.if_timer = 0;
523 	if_schedule_deferred_start(&sc->sc_if);
524 
525 	rnd_add_uint32(&sc->rnd_source, xmtfs);
526 }
527 
528 void
529 mc_rint(struct mc_softc *sc)
530 {
531 #define	rxf	sc->sc_rxframe
532 	u_int len;
533 
534 	len = (rxf.rx_rcvcnt | ((rxf.rx_rcvsts & 0xf) << 8)) - 4;
535 
536 #ifdef MCDEBUG
537 	if (rxf.rx_rcvsts & 0xf0)
538 		printf("%s: rcvcnt %02x rcvsts %02x rntpc 0x%02x rcvcc 0x%02x\n",
539 		    device_xname(sc->sc_dev), rxf.rx_rcvcnt, rxf.rx_rcvsts,
540 		    rxf.rx_rntpc, rxf.rx_rcvcc);
541 #endif
542 
543 	if (rxf.rx_rcvsts & OFLO) {
544 		printf("%s: receive FIFO overflow\n", device_xname(sc->sc_dev));
545 		if_statinc(&sc->sc_if, if_ierrors);
546 		return;
547 	}
548 
549 	if (rxf.rx_rcvsts & CLSN)
550 		if_statinc(&sc->sc_if, if_collisions);
551 
552 	if (rxf.rx_rcvsts & FRAM) {
553 #ifdef MCDEBUG
554 		printf("%s: framing error\n", device_xname(sc->sc_dev));
555 #endif
556 		if_statinc(&sc->sc_if, if_ierrors);
557 		return;
558 	}
559 
560 	if (rxf.rx_rcvsts & FCS) {
561 #ifdef MCDEBUG
562 		printf("%s: frame control checksum error\n", device_xname(sc->sc_dev));
563 #endif
564 		if_statinc(&sc->sc_if, if_ierrors);
565 		return;
566 	}
567 
568 	mace_read(sc, rxf.rx_frame, len);
569 
570 	rnd_add_uint32(&sc->rnd_source, rxf.rx_rcvsts);
571 #undef	rxf
572 }
573 
574 integrate void
575 mace_read(struct mc_softc *sc, void *pkt, int len)
576 {
577 	struct ifnet *ifp = &sc->sc_if;
578 	struct mbuf *m;
579 
580 	if (len <= sizeof(struct ether_header) ||
581 	    len > ETHERMTU + sizeof(struct ether_header)) {
582 #ifdef MCDEBUG
583 		printf("%s: invalid packet size %d; dropping\n",
584 		    device_xname(sc->sc_dev), len);
585 #endif
586 		if_statinc(ifp, if_ierrors);
587 		return;
588 	}
589 
590 	m = mace_get(sc, pkt, len);
591 	if (m == NULL) {
592 		if_statinc(ifp, if_ierrors);
593 		return;
594 	}
595 
596 	/* Pass the packet up. */
597 	if_percpuq_enqueue(ifp->if_percpuq, m);
598 }
599 
600 /*
601  * Pull data off an interface.
602  * Len is length of data, with local net header stripped.
603  * We copy the data into mbufs.  When full cluster sized units are present
604  * we copy into clusters.
605  */
606 integrate struct mbuf *
607 mace_get(struct mc_softc *sc, void *pkt, int totlen)
608 {
609 	struct mbuf *m;
610 	struct mbuf *top, **mp;
611 	int len;
612 
613 	MGETHDR(m, M_DONTWAIT, MT_DATA);
614 	if (m == 0)
615 		return 0;
616 	m_set_rcvif(m, &sc->sc_if);
617 	m->m_pkthdr.len = totlen;
618 	len = MHLEN;
619 	top = 0;
620 	mp = &top;
621 
622 	while (totlen > 0) {
623 		if (top) {
624 			MGET(m, M_DONTWAIT, MT_DATA);
625 			if (m == 0) {
626 				m_freem(top);
627 				return 0;
628 			}
629 			len = MLEN;
630 		}
631 		if (totlen >= MINCLSIZE) {
632 			MCLGET(m, M_DONTWAIT);
633 			if ((m->m_flags & M_EXT) == 0) {
634 				m_free(m);
635 				m_freem(top);
636 				return 0;
637 			}
638 			len = MCLBYTES;
639 		}
640 		m->m_len = len = uimin(totlen, len);
641 		memcpy(mtod(m, void *), pkt, len);
642 		pkt = (char*)pkt + len;
643 		totlen -= len;
644 		*mp = m;
645 		mp = &m->m_next;
646 	}
647 
648 	return top;
649 }
650 
651 /*
652  * Go through the list of multicast addresses and calculate the logical
653  * address filter.
654  */
655 void
656 mace_calcladrf(struct ethercom *ec, uint8_t *af)
657 {
658 	struct ifnet *ifp = &ec->ec_if;
659 	struct ether_multi *enm;
660 	u_char *cp;
661 	uint32_t crc;
662 	static const uint32_t crctab[] = {
663 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
664 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
665 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
666 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
667 	};
668 	int len;
669 	struct ether_multistep step;
670 
671 	/*
672 	 * Set up multicast address filter by passing all multicast addresses
673 	 * through a crc generator, and then using the high order 6 bits as an
674 	 * index into the 64 bit logical address filter.  The high order bit
675 	 * selects the word, while the rest of the bits select the bit within
676 	 * the word.
677 	 */
678 
679 	*((uint32_t *)af) = *((uint32_t *)af + 1) = 0;
680 	ETHER_LOCK(ec);
681 	ETHER_FIRST_MULTI(step, ec, enm);
682 	while (enm != NULL) {
683 		if (ETHER_CMP(enm->enm_addrlo, enm->enm_addrhi)) {
684 			/*
685 			 * We must listen to a range of multicast addresses.
686 			 * For now, just accept all multicasts, rather than
687 			 * trying to set only those filter bits needed to match
688 			 * the range.  (At this time, the only use of address
689 			 * ranges is for IP multicast routing, for which the
690 			 * range is big enough to require all bits set.)
691 			 */
692 			ETHER_UNLOCK(ec);
693 			goto allmulti;
694 		}
695 
696 		cp = enm->enm_addrlo;
697 		crc = 0xffffffff;
698 		for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
699 			crc ^= *cp++;
700 			crc = (crc >> 4) ^ crctab[crc & 0xf];
701 			crc = (crc >> 4) ^ crctab[crc & 0xf];
702 		}
703 		/* Just want the 6 most significant bits. */
704 		crc >>= 26;
705 
706 		/* Set the corresponding bit in the filter. */
707 		af[crc >> 3] |= 1 << (crc & 7);
708 
709 		ETHER_NEXT_MULTI(step, enm);
710 	}
711 	ETHER_UNLOCK(ec);
712 	ifp->if_flags &= ~IFF_ALLMULTI;
713 	return;
714 
715 allmulti:
716 	ifp->if_flags |= IFF_ALLMULTI;
717 	*((uint32_t *)af) = *((uint32_t *)af + 1) = 0xffffffff;
718 }
719 
720 static u_char bbr4[] = {0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15};
721 #define bbr(v)  ((bbr4[(v)&0xf] << 4) | bbr4[((v)>>4) & 0xf])
722 
723 u_char
724 mc_get_enaddr(bus_space_tag_t t, bus_space_handle_t h, bus_size_t o,
725     u_char *dst)
726 {
727 	int	i;
728 	u_char	b, csum;
729 
730 	/*
731 	 * The XOR of the 8 bytes of the ROM must be 0xff for it to be
732 	 * valid
733 	*/
734 	for (i = 0, csum = 0; i < 8; i++) {
735 		b = bus_space_read_1(t, h, o+16*i);
736 		if (i < ETHER_ADDR_LEN)
737 			dst[i] = bbr(b);
738 		csum ^= b;
739 	}
740 
741 	return csum;
742 }
743