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