xref: /netbsd-src/sys/arch/mac68k/dev/if_mc.c (revision 5e4c038a45edbc7d63b7c2daa76e29f88b64a4e3)
1 /*	$NetBSD: if_mc.c,v 1.19 2002/01/16 06:00:37 thorpej 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 <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 <uvm/uvm_extern.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_attach(ifp);
180 	ether_ifattach(ifp, lladdr);
181 
182 	return (0);
183 }
184 
185 hide int
186 mcioctl(ifp, cmd, data)
187 	struct ifnet *ifp;
188 	u_long cmd;
189 	caddr_t data;
190 {
191 	struct mc_softc *sc = ifp->if_softc;
192 	struct ifaddr *ifa;
193 	struct ifreq *ifr;
194 
195 	int	s = splnet(), err = 0;
196 	int	temp;
197 
198 	switch (cmd) {
199 
200 	case SIOCSIFADDR:
201 		ifa = (struct ifaddr *)data;
202 		ifp->if_flags |= IFF_UP;
203 		switch (ifa->ifa_addr->sa_family) {
204 #ifdef INET
205 		case AF_INET:
206 			mcinit(sc);
207 			arp_ifinit(ifp, ifa);
208 			break;
209 #endif
210 #ifdef NS
211 		case AF_NS:
212 		    {
213 			register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
214 
215 			if (ns_nullhost(*ina))
216 				ina->x_host =
217 				    *(union ns_host *)LLADDR(ifp->if_sadl);
218 			else {
219 				bcopy(ina->x_host.c_host,
220 				    LLADDR(ifp->if_sadl),
221 				    sizeof(sc->sc_enaddr));
222 			}
223 			/* Set new address. */
224 			mcinit(sc);
225 			break;
226 		    }
227 #endif
228 		default:
229 			mcinit(sc);
230 			break;
231 		}
232 		break;
233 
234 	case SIOCSIFFLAGS:
235 		if ((ifp->if_flags & IFF_UP) == 0 &&
236 		    (ifp->if_flags & IFF_RUNNING) != 0) {
237 			/*
238 			 * If interface is marked down and it is running,
239 			 * then stop it.
240 			 */
241 			mcstop(sc);
242 			ifp->if_flags &= ~IFF_RUNNING;
243 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
244 		    (ifp->if_flags & IFF_RUNNING) == 0) {
245 			/*
246 			 * If interface is marked up and it is stopped,
247 			 * then start it.
248 			 */
249 			(void)mcinit(sc);
250 		} else {
251 			/*
252 			 * reset the interface to pick up any other changes
253 			 * in flags
254 			 */
255 			temp = ifp->if_flags & IFF_UP;
256 			mcreset(sc);
257 			ifp->if_flags |= temp;
258 			mcstart(ifp);
259 		}
260 		break;
261 
262 	case SIOCADDMULTI:
263 	case SIOCDELMULTI:
264 		ifr = (struct ifreq *) data;
265 		err = (cmd == SIOCADDMULTI) ?
266 		    ether_addmulti(ifr, &sc->sc_ethercom) :
267 		    ether_delmulti(ifr, &sc->sc_ethercom);
268 
269 		if (err == ENETRESET) {
270 			/*
271 			 * Multicast list has changed; set the hardware
272 			 * filter accordingly. But remember UP flag!
273 			 */
274 			temp = ifp->if_flags & IFF_UP;
275 			mcreset(sc);
276 			ifp->if_flags |= temp;
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 | IFF_UP);
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 	int temp;
440 
441 	printf("mcwatchdog: resetting chip\n");
442 	temp = ifp->if_flags & IFF_UP;
443 	mcreset(sc);
444 	ifp->if_flags |= temp;
445 }
446 
447 /*
448  * stuff packet into MACE (at splnet)
449  */
450 integrate u_int
451 maceput(sc, m)
452 	struct mc_softc *sc;
453 	struct mbuf *m;
454 {
455 	struct mbuf *n;
456 	u_int len, totlen = 0;
457 	u_char *buff;
458 
459 	buff = sc->sc_txbuf;
460 
461 	for (; m; m = n) {
462 		u_char *data = mtod(m, u_char *);
463 		len = m->m_len;
464 		totlen += len;
465 		bcopy(data, buff, len);
466 		buff += len;
467 		MFREE(m, n);
468 	}
469 
470 	if (totlen > NBPG)
471 		panic("%s: maceput: packet overflow", sc->sc_dev.dv_xname);
472 
473 #if 0
474 	if (totlen < ETHERMIN + sizeof(struct ether_header)) {
475 		int pad = ETHERMIN + sizeof(struct ether_header) - totlen;
476 		bzero(sc->sc_txbuf + totlen, pad);
477 		totlen = ETHERMIN + sizeof(struct ether_header);
478 	}
479 #endif
480 
481 	(*sc->sc_putpacket)(sc, totlen);
482 
483 	sc->sc_if.if_timer = 5;	/* 5 seconds to watch for failing to transmit */
484 	return (totlen);
485 }
486 
487 void
488 mcintr(arg)
489 	void *arg;
490 {
491 struct mc_softc *sc = arg;
492 	u_int8_t ir;
493 
494 	ir = NIC_GET(sc, MACE_IR) & ~NIC_GET(sc, MACE_IMR);
495 	if (ir & JAB) {
496 #ifdef MCDEBUG
497 		printf("%s: jabber error\n", sc->sc_dev.dv_xname);
498 #endif
499 		sc->sc_if.if_oerrors++;
500 	}
501 
502 	if (ir & BABL) {
503 #ifdef MCDEBUG
504 		printf("%s: babble\n", sc->sc_dev.dv_xname);
505 #endif
506 		sc->sc_if.if_oerrors++;
507 	}
508 
509 	if (ir & CERR) {
510 #ifdef MCDEBUG
511 		printf("%s: collision error\n", sc->sc_dev.dv_xname);
512 #endif
513 		sc->sc_if.if_collisions++;
514 	}
515 
516 	/*
517 	 * Pretend we have carrier; if we don't this will be cleared
518 	 * shortly.
519 	 */
520 	sc->sc_havecarrier = 1;
521 
522 	if (ir & XMTINT)
523 		mc_tint(sc);
524 
525 	if (ir & RCVINT)
526 		mc_rint(sc);
527 }
528 
529 integrate void
530 mc_tint(sc)
531 	struct mc_softc *sc;
532 {
533 	u_int8_t xmtrc, xmtfs;
534 
535 	xmtrc = NIC_GET(sc, MACE_XMTRC);
536 	xmtfs = NIC_GET(sc, MACE_XMTFS);
537 
538 	if ((xmtfs & XMTSV) == 0)
539 		return;
540 
541 	if (xmtfs & UFLO) {
542 		printf("%s: underflow\n", sc->sc_dev.dv_xname);
543 		mcreset(sc);
544 		return;
545 	}
546 
547 	if (xmtfs & LCOL) {
548 		printf("%s: late collision\n", sc->sc_dev.dv_xname);
549 		sc->sc_if.if_oerrors++;
550 		sc->sc_if.if_collisions++;
551 	}
552 
553 	if (xmtfs & MORE)
554 		/* Real number is unknown. */
555 		sc->sc_if.if_collisions += 2;
556 	else if (xmtfs & ONE)
557 		sc->sc_if.if_collisions++;
558 	else if (xmtfs & RTRY) {
559 		printf("%s: excessive collisions\n", sc->sc_dev.dv_xname);
560 		sc->sc_if.if_collisions += 16;
561 		sc->sc_if.if_oerrors++;
562 	}
563 
564 	if (xmtfs & LCAR) {
565 		sc->sc_havecarrier = 0;
566 		printf("%s: lost carrier\n", sc->sc_dev.dv_xname);
567 		sc->sc_if.if_oerrors++;
568 	}
569 
570 	sc->sc_if.if_flags &= ~IFF_OACTIVE;
571 	sc->sc_if.if_timer = 0;
572 	mcstart(&sc->sc_if);
573 }
574 
575 void
576 mc_rint(sc)
577 	struct mc_softc *sc;
578 {
579 #define	rxf	sc->sc_rxframe
580 	u_int len;
581 
582 	len = (rxf.rx_rcvcnt | ((rxf.rx_rcvsts & 0xf) << 8)) - 4;
583 
584 #ifdef MCDEBUG
585 	if (rxf.rx_rcvsts & 0xf0)
586 		printf("%s: rcvcnt %02x rcvsts %02x rntpc 0x%02x rcvcc 0x%02x\n",
587 		    sc->sc_dev.dv_xname, rxf.rx_rcvcnt, rxf.rx_rcvsts,
588 		    rxf.rx_rntpc, rxf.rx_rcvcc);
589 #endif
590 
591 	if (rxf.rx_rcvsts & OFLO) {
592 		printf("%s: receive FIFO overflow\n", sc->sc_dev.dv_xname);
593 		sc->sc_if.if_ierrors++;
594 		return;
595 	}
596 
597 	if (rxf.rx_rcvsts & CLSN)
598 		sc->sc_if.if_collisions++;
599 
600 	if (rxf.rx_rcvsts & FRAM) {
601 #ifdef MCDEBUG
602 		printf("%s: framing error\n", sc->sc_dev.dv_xname);
603 #endif
604 		sc->sc_if.if_ierrors++;
605 		return;
606 	}
607 
608 	if (rxf.rx_rcvsts & FCS) {
609 #ifdef MCDEBUG
610 		printf("%s: frame control checksum error\n", sc->sc_dev.dv_xname);
611 #endif
612 		sc->sc_if.if_ierrors++;
613 		return;
614 	}
615 
616 	mace_read(sc, rxf.rx_frame, len);
617 #undef	rxf
618 }
619 
620 integrate void
621 mace_read(sc, pkt, len)
622 	struct mc_softc *sc;
623 	caddr_t pkt;
624 	int len;
625 {
626 	struct ifnet *ifp = &sc->sc_if;
627 	struct mbuf *m;
628 
629 	if (len <= sizeof(struct ether_header) ||
630 	    len > ETHERMTU + sizeof(struct ether_header)) {
631 #ifdef MCDEBUG
632 		printf("%s: invalid packet size %d; dropping\n",
633 		    sc->sc_dev.dv_xname, len);
634 #endif
635 		ifp->if_ierrors++;
636 		return;
637 	}
638 
639 	m = mace_get(sc, pkt, len);
640 	if (m == NULL) {
641 		ifp->if_ierrors++;
642 		return;
643 	}
644 
645 	ifp->if_ipackets++;
646 
647 #if NBPFILTER > 0
648 	/* Pass the packet to any BPF listeners. */
649 	if (ifp->if_bpf)
650 		bpf_mtap(ifp->if_bpf, m);
651 #endif
652 
653 	/* Pass the packet up. */
654 	(*ifp->if_input)(ifp, m);
655 }
656 
657 /*
658  * Pull data off an interface.
659  * Len is length of data, with local net header stripped.
660  * We copy the data into mbufs.  When full cluster sized units are present
661  * we copy into clusters.
662  */
663 integrate struct mbuf *
664 mace_get(sc, pkt, totlen)
665 	struct mc_softc *sc;
666 	caddr_t pkt;
667 	int totlen;
668 {
669 	register struct mbuf *m;
670 	struct mbuf *top, **mp;
671 	int len;
672 
673 	MGETHDR(m, M_DONTWAIT, MT_DATA);
674 	if (m == 0)
675 		return (0);
676 	m->m_pkthdr.rcvif = &sc->sc_if;
677 	m->m_pkthdr.len = totlen;
678 	len = MHLEN;
679 	top = 0;
680 	mp = &top;
681 
682 	while (totlen > 0) {
683 		if (top) {
684 			MGET(m, M_DONTWAIT, MT_DATA);
685 			if (m == 0) {
686 				m_freem(top);
687 				return 0;
688 			}
689 			len = MLEN;
690 		}
691 		if (totlen >= MINCLSIZE) {
692 			MCLGET(m, M_DONTWAIT);
693 			if ((m->m_flags & M_EXT) == 0) {
694 				m_free(m);
695 				m_freem(top);
696 				return 0;
697 			}
698 			len = MCLBYTES;
699 		}
700 		m->m_len = len = min(totlen, len);
701 		bcopy(pkt, mtod(m, caddr_t), len);
702 		pkt += len;
703 		totlen -= len;
704 		*mp = m;
705 		mp = &m->m_next;
706 	}
707 
708 	return (top);
709 }
710 
711 /*
712  * Go through the list of multicast addresses and calculate the logical
713  * address filter.
714  */
715 void
716 mace_calcladrf(ac, af)
717 	struct ethercom *ac;
718 	u_int8_t *af;
719 {
720 	struct ifnet *ifp = &ac->ec_if;
721 	struct ether_multi *enm;
722 	register u_char *cp;
723 	register u_int32_t crc;
724 	static const u_int32_t crctab[] = {
725 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
726 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
727 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
728 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
729 	};
730 	register int len;
731 	struct ether_multistep step;
732 
733 	/*
734 	 * Set up multicast address filter by passing all multicast addresses
735 	 * through a crc generator, and then using the high order 6 bits as an
736 	 * index into the 64 bit logical address filter.  The high order bit
737 	 * selects the word, while the rest of the bits select the bit within
738 	 * the word.
739 	 */
740 
741 	*((u_int32_t *)af) = *((u_int32_t *)af + 1) = 0;
742 	ETHER_FIRST_MULTI(step, ac, enm);
743 	while (enm != NULL) {
744 		if (ETHER_CMP(enm->enm_addrlo, enm->enm_addrhi)) {
745 			/*
746 			 * We must listen to a range of multicast addresses.
747 			 * For now, just accept all multicasts, rather than
748 			 * trying to set only those filter bits needed to match
749 			 * the range.  (At this time, the only use of address
750 			 * ranges is for IP multicast routing, for which the
751 			 * range is big enough to require all bits set.)
752 			 */
753 			goto allmulti;
754 		}
755 
756 		cp = enm->enm_addrlo;
757 		crc = 0xffffffff;
758 		for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
759 			crc ^= *cp++;
760 			crc = (crc >> 4) ^ crctab[crc & 0xf];
761 			crc = (crc >> 4) ^ crctab[crc & 0xf];
762 		}
763 		/* Just want the 6 most significant bits. */
764 		crc >>= 26;
765 
766 		/* Set the corresponding bit in the filter. */
767 		af[crc >> 3] |= 1 << (crc & 7);
768 
769 		ETHER_NEXT_MULTI(step, enm);
770 	}
771 	ifp->if_flags &= ~IFF_ALLMULTI;
772 	return;
773 
774 allmulti:
775 	ifp->if_flags |= IFF_ALLMULTI;
776 	*((u_int32_t *)af) = *((u_int32_t *)af + 1) = 0xffffffff;
777 }
778 
779 static u_char bbr4[] = {0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15};
780 #define bbr(v)  ((bbr4[(v)&0xf] << 4) | bbr4[((v)>>4) & 0xf])
781 
782 u_char
783 mc_get_enaddr(t, h, o, dst)
784 	bus_space_tag_t t;
785 	bus_space_handle_t h;
786 	bus_size_t o;
787 	u_char *dst;
788 {
789 	int	i;
790 	u_char	b, csum;
791 
792 	/*
793 	 * The XOR of the 8 bytes of the ROM must be 0xff for it to be
794 	 * valid
795 	*/
796 	for (i = 0, csum = 0; i < 8; i++) {
797 		b = bus_space_read_1(t, h, o+16*i);
798 		if (i < ETHER_ADDR_LEN)
799 			dst[i] = bbr(b);
800 		csum ^= b;
801 	}
802 
803 	return csum;
804 }
805