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