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