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