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