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