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