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