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