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