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