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