if_mc.c revision 1.11.6.1 1 /* $NetBSD: if_mc.c,v 1.11.6.1 1998/12/11 04:52:56 kenh 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;
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 ifp = ifalloc();
172 sc->sc_if = ifp;
173 ifp->if_ifcom = &sc->sc_ethercom;
174 bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
175 ifp->if_softc = sc;
176 ifp->if_ioctl = mcioctl;
177 ifp->if_start = mcstart;
178 ifp->if_flags =
179 IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
180 ifp->if_watchdog = mcwatchdog;
181
182 #if NBPFILTER > 0
183 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
184 #endif
185 if_attach(ifp);
186 ether_ifattach(ifp, lladdr);
187
188 return (0);
189 }
190
191 hide int
192 mcioctl(ifp, cmd, data)
193 struct ifnet *ifp;
194 u_long cmd;
195 caddr_t data;
196 {
197 struct mc_softc *sc = ifp->if_softc;
198 struct ifaddr *ifa;
199 struct ifreq *ifr;
200
201 int s = splnet(), err = 0;
202 int temp;
203
204 switch (cmd) {
205
206 case SIOCSIFADDR:
207 ifa = (struct ifaddr *)data;
208 ifp->if_flags |= IFF_UP;
209 switch (ifa->ifa_addr->sa_family) {
210 #ifdef INET
211 case AF_INET:
212 mcinit(sc);
213 arp_ifinit(ifp, ifa);
214 break;
215 #endif
216 #ifdef NS
217 case AF_NS:
218 {
219 register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
220
221 if (ns_nullhost(*ina))
222 ina->x_host =
223 *(union ns_host *)LLADDR(ifp->if_sadl);
224 else {
225 bcopy(ina->x_host.c_host,
226 LLADDR(ifp->if_sadl),
227 sizeof(sc->sc_enaddr));
228 }
229 /* Set new address. */
230 mcinit(sc);
231 break;
232 }
233 #endif
234 default:
235 mcinit(sc);
236 break;
237 }
238 break;
239
240 case SIOCSIFFLAGS:
241 if ((ifp->if_flags & IFF_UP) == 0 &&
242 (ifp->if_flags & IFF_RUNNING) != 0) {
243 /*
244 * If interface is marked down and it is running,
245 * then stop it.
246 */
247 mcstop(sc);
248 ifp->if_flags &= ~IFF_RUNNING;
249 } else if ((ifp->if_flags & IFF_UP) != 0 &&
250 (ifp->if_flags & IFF_RUNNING) == 0) {
251 /*
252 * If interface is marked up and it is stopped,
253 * then start it.
254 */
255 (void)mcinit(sc);
256 } else {
257 /*
258 * reset the interface to pick up any other changes
259 * in flags
260 */
261 temp = ifp->if_flags & IFF_UP;
262 mcreset(sc);
263 ifp->if_flags |= temp;
264 mcstart(ifp);
265 }
266 break;
267
268 case SIOCADDMULTI:
269 case SIOCDELMULTI:
270 ifr = (struct ifreq *) data;
271 err = (cmd == SIOCADDMULTI) ?
272 ether_addmulti(ifr, &sc->sc_ethercom) :
273 ether_delmulti(ifr, &sc->sc_ethercom);
274
275 if (err == ENETRESET) {
276 /*
277 * Multicast list has changed; set the hardware
278 * filter accordingly. But remember UP flag!
279 */
280 temp = ifp->if_flags & IFF_UP;
281 mcreset(sc);
282 ifp->if_flags |= temp;
283 err = 0;
284 }
285 break;
286 default:
287 err = EINVAL;
288 }
289 splx(s);
290 return (err);
291 }
292
293 /*
294 * Encapsulate a packet of type family for the local net.
295 */
296 hide void
297 mcstart(ifp)
298 struct ifnet *ifp;
299 {
300 struct mc_softc *sc = ifp->if_softc;
301 struct mbuf *m;
302
303 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
304 return;
305
306 while (1) {
307 if (ifp->if_flags & IFF_OACTIVE)
308 return;
309
310 IF_DEQUEUE(&ifp->if_snd, m);
311 if (m == 0)
312 return;
313
314 #if NBPFILTER > 0
315 /*
316 * If bpf is listening on this interface, let it
317 * see the packet before we commit it to the wire.
318 */
319 if (ifp->if_bpf)
320 bpf_mtap(ifp->if_bpf, m);
321 #endif
322
323 /*
324 * Copy the mbuf chain into the transmit buffer.
325 */
326 ifp->if_flags |= IFF_OACTIVE;
327 maceput(sc, m);
328
329 ifp->if_opackets++; /* # of pkts */
330 }
331 }
332
333 /*
334 * reset and restart the MACE. Called in case of fatal
335 * hardware/software errors.
336 */
337 hide void
338 mcreset(sc)
339 struct mc_softc *sc;
340 {
341 mcstop(sc);
342 mcinit(sc);
343 }
344
345 hide int
346 mcinit(sc)
347 struct mc_softc *sc;
348 {
349 int s;
350 u_int8_t maccc, ladrf[8];
351
352 if (sc->sc_if->if_flags & IFF_RUNNING)
353 /* already running */
354 return (0);
355
356 s = splnet();
357
358 NIC_PUT(sc, MACE_BIUCC, sc->sc_biucc);
359 NIC_PUT(sc, MACE_FIFOCC, sc->sc_fifocc);
360 NIC_PUT(sc, MACE_IMR, ~0); /* disable all interrupts */
361 NIC_PUT(sc, MACE_PLSCC, sc->sc_plscc);
362
363 NIC_PUT(sc, MACE_UTR, RTRD); /* disable reserved test registers */
364
365 /* set MAC address */
366 NIC_PUT(sc, MACE_IAC, ADDRCHG);
367 while (NIC_GET(sc, MACE_IAC) & ADDRCHG)
368 ;
369 NIC_PUT(sc, MACE_IAC, PHYADDR);
370 bus_space_write_multi_1(sc->sc_regt, sc->sc_regh, MACE_REG(MACE_PADR),
371 sc->sc_enaddr, ETHER_ADDR_LEN);
372
373 /* set logical address filter */
374 mace_calcladrf(&sc->sc_ethercom, ladrf);
375
376 NIC_PUT(sc, MACE_IAC, ADDRCHG);
377 while (NIC_GET(sc, MACE_IAC) & ADDRCHG)
378 ;
379 NIC_PUT(sc, MACE_IAC, LOGADDR);
380 bus_space_write_multi_1(sc->sc_regt, sc->sc_regh, MACE_REG(MACE_LADRF),
381 ladrf, 8);
382
383 NIC_PUT(sc, MACE_XMTFC, APADXMT);
384 /*
385 * No need to autostrip padding on receive... Ethernet frames
386 * don't have a length field, unlike 802.3 frames, so the MACE
387 * can't figure out the length of the packet anyways.
388 */
389 NIC_PUT(sc, MACE_RCVFC, 0);
390
391 maccc = ENXMT | ENRCV;
392 if (sc->sc_if->if_flags & IFF_PROMISC)
393 maccc |= PROM;
394
395 NIC_PUT(sc, MACE_MACCC, maccc);
396
397 if (sc->sc_bus_init)
398 (*sc->sc_bus_init)(sc);
399
400 /*
401 * Enable all interrupts except receive, since we use the DMA
402 * completion interrupt for that.
403 */
404 NIC_PUT(sc, MACE_IMR, RCVINTM);
405
406 /* flag interface as "running" */
407 sc->sc_if->if_flags |= IFF_RUNNING;
408 sc->sc_if->if_flags &= ~IFF_OACTIVE;
409
410 splx(s);
411 return (0);
412 }
413
414 /*
415 * close down an interface and free its buffers
416 * Called on final close of device, or if mcinit() fails
417 * part way through.
418 */
419 hide int
420 mcstop(sc)
421 struct mc_softc *sc;
422 {
423 int s = splnet();
424
425 NIC_PUT(sc, MACE_BIUCC, SWRST);
426 DELAY(100);
427
428 sc->sc_if->if_timer = 0;
429 sc->sc_if->if_flags &= ~(IFF_RUNNING | IFF_UP);
430
431 splx(s);
432 return (0);
433 }
434
435 /*
436 * Called if any Tx packets remain unsent after 5 seconds,
437 * In all cases we just reset the chip, and any retransmission
438 * will be handled by higher level protocol timeouts.
439 */
440 hide void
441 mcwatchdog(ifp)
442 struct ifnet *ifp;
443 {
444 struct mc_softc *sc = ifp->if_softc;
445 int temp;
446
447 printf("mcwatchdog: resetting chip\n");
448 temp = ifp->if_flags & IFF_UP;
449 mcreset(sc);
450 ifp->if_flags |= temp;
451 }
452
453 /*
454 * stuff packet into MACE (at splnet)
455 */
456 integrate u_int
457 maceput(sc, m)
458 struct mc_softc *sc;
459 struct mbuf *m;
460 {
461 struct mbuf *n;
462 u_int len, totlen = 0;
463 u_char *buff;
464
465 buff = sc->sc_txbuf;
466
467 for (; m; m = n) {
468 u_char *data = mtod(m, u_char *);
469 len = m->m_len;
470 totlen += len;
471 bcopy(data, buff, len);
472 buff += len;
473 MFREE(m, n);
474 }
475
476 if (totlen > NBPG)
477 panic("%s: maceput: packet overflow", sc->sc_dev.dv_xname);
478
479 #if 0
480 if (totlen < ETHERMIN + sizeof(struct ether_header)) {
481 int pad = ETHERMIN + sizeof(struct ether_header) - totlen;
482 bzero(sc->sc_txbuf + totlen, pad);
483 totlen = ETHERMIN + sizeof(struct ether_header);
484 }
485 #endif
486
487 (*sc->sc_putpacket)(sc, totlen);
488
489 sc->sc_if->if_timer = 5; /* 5 seconds to watch for failing to transmit */
490 return (totlen);
491 }
492
493 void
494 mcintr(arg)
495 void *arg;
496 {
497 struct mc_softc *sc = arg;
498 u_int8_t ir;
499
500 ir = NIC_GET(sc, MACE_IR) & ~NIC_GET(sc, MACE_IMR);
501 if (ir & JAB) {
502 #ifdef MCDEBUG
503 printf("%s: jabber error\n", sc->sc_dev.dv_xname);
504 #endif
505 sc->sc_if->if_oerrors++;
506 }
507
508 if (ir & BABL) {
509 #ifdef MCDEBUG
510 printf("%s: babble\n", sc->sc_dev.dv_xname);
511 #endif
512 sc->sc_if->if_oerrors++;
513 }
514
515 if (ir & CERR) {
516 printf("%s: collision error\n", sc->sc_dev.dv_xname);
517 sc->sc_if->if_collisions++;
518 }
519
520 /*
521 * Pretend we have carrier; if we don't this will be cleared
522 * shortly.
523 */
524 sc->sc_havecarrier = 1;
525
526 if (ir & XMTINT)
527 mc_tint(sc);
528
529 if (ir & RCVINT)
530 mc_rint(sc);
531 }
532
533 integrate void
534 mc_tint(sc)
535 struct mc_softc *sc;
536 {
537 u_int8_t xmtrc, xmtfs;
538
539 xmtrc = NIC_GET(sc, MACE_XMTRC);
540 xmtfs = NIC_GET(sc, MACE_XMTFS);
541
542 if ((xmtfs & XMTSV) == 0)
543 return;
544
545 if (xmtfs & UFLO) {
546 printf("%s: underflow\n", sc->sc_dev.dv_xname);
547 mcreset(sc);
548 return;
549 }
550
551 if (xmtfs & LCOL) {
552 printf("%s: late collision\n", sc->sc_dev.dv_xname);
553 sc->sc_if->if_oerrors++;
554 sc->sc_if->if_collisions++;
555 }
556
557 if (xmtfs & MORE)
558 /* Real number is unknown. */
559 sc->sc_if->if_collisions += 2;
560 else if (xmtfs & ONE)
561 sc->sc_if->if_collisions++;
562 else if (xmtfs & RTRY) {
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 struct ether_header *eh = (struct ether_header *)pkt;
631 struct mbuf *m;
632
633 if (len <= sizeof(struct ether_header) ||
634 len > ETHERMTU + sizeof(struct ether_header)) {
635 #ifdef MCDEBUG
636 printf("%s: invalid packet size %d; dropping\n",
637 sc->sc_dev.dv_xname, len);
638 #endif
639 ifp->if_ierrors++;
640 return;
641 }
642
643 #if NBPFILTER > 0
644 /*
645 * Check if there's a bpf filter listening on this interface.
646 * If so, hand off the raw packet to enet, then discard things
647 * not destined for us (but be sure to keep broadcast/multicast).
648 */
649 if (ifp->if_bpf) {
650 bpf_tap(ifp->if_bpf, pkt, len);
651 if ((ifp->if_flags & IFF_PROMISC) != 0 &&
652 (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
653 ETHER_CMP(eh->ether_dhost, sc->sc_enaddr))
654 return;
655 }
656 #endif
657 m = mace_get(sc, pkt, len);
658 if (m == NULL) {
659 ifp->if_ierrors++;
660 return;
661 }
662
663 ifp->if_ipackets++;
664
665 /* Pass the packet up, with the ether header sort-of removed. */
666 m_adj(m, sizeof(struct ether_header));
667 ether_input(ifp, eh, m);
668 }
669
670 /*
671 * Pull data off an interface.
672 * Len is length of data, with local net header stripped.
673 * We copy the data into mbufs. When full cluster sized units are present
674 * we copy into clusters.
675 */
676 integrate struct mbuf *
677 mace_get(sc, pkt, totlen)
678 struct mc_softc *sc;
679 caddr_t pkt;
680 int totlen;
681 {
682 register struct mbuf *m;
683 struct mbuf *top, **mp;
684 int len;
685
686 MGETHDR(m, M_DONTWAIT, MT_DATA);
687 if (m == 0)
688 return (0);
689 m->m_pkthdr.rcvif = sc->sc_if;
690 if_addref(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 vm_offset_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