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