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