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