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