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