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