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