Home | History | Annotate | Line # | Download | only in dev
am79c950.c revision 1.16
      1 /*	$NetBSD: am79c950.c,v 1.16 2004/10/30 18:08:34 thorpej 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/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: am79c950.c,v 1.16 2004/10/30 18:08:34 thorpej Exp $");
     39 
     40 #include "opt_inet.h"
     41 #include "opt_ccitt.h"
     42 #include "opt_llc.h"
     43 #include "opt_ns.h"
     44 
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/mbuf.h>
     48 #include <sys/buf.h>
     49 #include <sys/protosw.h>
     50 #include <sys/socket.h>
     51 #include <sys/syslog.h>
     52 #include <sys/ioctl.h>
     53 #include <sys/errno.h>
     54 #include <sys/device.h>
     55 
     56 #include <uvm/uvm_extern.h>
     57 
     58 #include <net/if.h>
     59 #include <net/if_dl.h>
     60 #include <net/if_ether.h>
     61 #include <net/if_media.h>
     62 
     63 #ifdef INET
     64 #include <netinet/in.h>
     65 #include <netinet/if_inarp.h>
     66 #include <netinet/in_systm.h>
     67 #include <netinet/in_var.h>
     68 #include <netinet/ip.h>
     69 #endif
     70 
     71 #ifdef NS
     72 #include <netns/ns.h>
     73 #include <netns/ns_if.h>
     74 #endif
     75 
     76 #if defined(CCITT) && defined(LLC)
     77 #include <sys/socketvar.h>
     78 #include <netccitt/x25.h>
     79 #include <netccitt/pk.h>
     80 #include <netccitt/pk_var.h>
     81 #include <netccitt/pk_extern.h>
     82 #endif
     83 
     84 #include <uvm/uvm_extern.h>
     85 
     86 #include "bpfilter.h"
     87 #if NBPFILTER > 0
     88 #include <net/bpf.h>
     89 #include <net/bpfdesc.h>
     90 #endif
     91 
     92 #include <machine/pio.h>
     93 #include <machine/bus.h>
     94 
     95 #include <macppc/dev/am79c950reg.h>
     96 #include <macppc/dev/if_mcvar.h>
     97 
     98 hide void	mcwatchdog __P((struct ifnet *));
     99 hide int	mcinit __P((struct mc_softc *sc));
    100 hide int	mcstop __P((struct mc_softc *sc));
    101 hide int	mcioctl __P((struct ifnet *ifp, u_long cmd, caddr_t data));
    102 hide void	mcstart __P((struct ifnet *ifp));
    103 hide void	mcreset __P((struct mc_softc *sc));
    104 
    105 integrate u_int	maceput __P((struct mc_softc *sc, struct mbuf *m0));
    106 integrate void	mc_tint __P((struct mc_softc *sc));
    107 integrate void	mace_read __P((struct mc_softc *, caddr_t, int));
    108 integrate struct mbuf *mace_get __P((struct mc_softc *, caddr_t, int));
    109 static void mace_calcladrf __P((struct ethercom *ac, u_int8_t *af));
    110 static inline u_int16_t ether_cmp __P((void *, void *));
    111 static int mc_mediachange __P((struct ifnet *));
    112 static void mc_mediastatus __P((struct ifnet *, struct ifmediareq *));
    113 
    114 /*
    115  * Compare two Ether/802 addresses for equality, inlined and
    116  * unrolled for speed.  Use this like memcmp().
    117  *
    118  * XXX: Add <machine/inlines.h> for stuff like this?
    119  * XXX: or maybe add it to libkern.h instead?
    120  *
    121  * "I'd love to have an inline assembler version of this."
    122  * XXX: Who wanted that? mycroft?  I wrote one, but this
    123  * version in C is as good as hand-coded assembly. -gwr
    124  *
    125  * Please do NOT tweak this without looking at the actual
    126  * assembly code generated before and after your tweaks!
    127  */
    128 static inline u_int16_t
    129 ether_cmp(one, two)
    130 	void *one, *two;
    131 {
    132 	register u_int16_t *a = (u_short *) one;
    133 	register u_int16_t *b = (u_short *) two;
    134 	register u_int16_t diff;
    135 
    136 #ifdef	m68k
    137 	/*
    138 	 * The post-increment-pointer form produces the best
    139 	 * machine code for m68k.  This was carefully tuned
    140 	 * so it compiles to just 8 short (2-byte) op-codes!
    141 	 */
    142 	diff  = *a++ - *b++;
    143 	diff |= *a++ - *b++;
    144 	diff |= *a++ - *b++;
    145 #else
    146 	/*
    147 	 * Most modern CPUs do better with a single expresion.
    148 	 * Note that short-cut evaluation is NOT helpful here,
    149 	 * because it just makes the code longer, not faster!
    150 	 */
    151 	diff = (a[0] - b[0]) | (a[1] - b[1]) | (a[2] - b[2]);
    152 #endif
    153 
    154 	return (diff);
    155 }
    156 
    157 #define ETHER_CMP	ether_cmp
    158 
    159 /*
    160  * Interface exists: make available by filling in network interface
    161  * record.  System will initialize the interface when it is ready
    162  * to accept packets.
    163  */
    164 int
    165 mcsetup(sc, lladdr)
    166 	struct mc_softc	*sc;
    167 	u_int8_t *lladdr;
    168 {
    169 	struct ifnet *ifp = &sc->sc_if;
    170 
    171 	/* reset the chip and disable all interrupts */
    172 	NIC_PUT(sc, MACE_BIUCC, SWRST);
    173 	DELAY(100);
    174 	NIC_PUT(sc, MACE_IMR, ~0);
    175 
    176 	memcpy(sc->sc_enaddr, lladdr, ETHER_ADDR_LEN);
    177 	printf(": address %s\n", ether_sprintf(lladdr));
    178 
    179 	memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
    180 	ifp->if_softc = sc;
    181 	ifp->if_ioctl = mcioctl;
    182 	ifp->if_start = mcstart;
    183 	ifp->if_flags =
    184 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    185 	ifp->if_watchdog = mcwatchdog;
    186 
    187 	/* initialize ifmedia structures */
    188 	ifmedia_init(&sc->sc_media, 0, mc_mediachange, mc_mediastatus);
    189 	ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
    190 	ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
    191 
    192 	if_attach(ifp);
    193 	ether_ifattach(ifp, lladdr);
    194 
    195 	return (0);
    196 }
    197 
    198 hide int
    199 mcioctl(ifp, cmd, data)
    200 	struct ifnet *ifp;
    201 	u_long cmd;
    202 	caddr_t data;
    203 {
    204 	struct mc_softc *sc = ifp->if_softc;
    205 	struct ifaddr *ifa;
    206 	struct ifreq *ifr;
    207 
    208 	int	s = splnet(), err = 0;
    209 
    210 	switch (cmd) {
    211 
    212 	case SIOCSIFADDR:
    213 		ifa = (struct ifaddr *)data;
    214 		ifp->if_flags |= IFF_UP;
    215 		switch (ifa->ifa_addr->sa_family) {
    216 #ifdef INET
    217 		case AF_INET:
    218 			mcinit(sc);
    219 			arp_ifinit(ifp, ifa);
    220 			break;
    221 #endif
    222 #ifdef NS
    223 		case AF_NS:
    224 		    {
    225 			register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
    226 
    227 			if (ns_nullhost(*ina))
    228 				ina->x_host =
    229 				    *(union ns_host *)LLADDR(ifp->if_sadl);
    230 			else {
    231 				memcpy(LLADDR(ifp->if_sadl),
    232 				    ina->x_host.c_host,
    233 				    sizeof(sc->sc_enaddr));
    234 			}
    235 			/* Set new address. */
    236 			mcinit(sc);
    237 			break;
    238 		    }
    239 #endif
    240 		default:
    241 			mcinit(sc);
    242 			break;
    243 		}
    244 		break;
    245 
    246 	case SIOCSIFFLAGS:
    247 		if ((ifp->if_flags & IFF_UP) == 0 &&
    248 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    249 			/*
    250 			 * If interface is marked down and it is running,
    251 			 * then stop it.
    252 			 */
    253 			mcstop(sc);
    254 			ifp->if_flags &= ~IFF_RUNNING;
    255 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
    256 		    (ifp->if_flags & IFF_RUNNING) == 0) {
    257 			/*
    258 			 * If interface is marked up and it is stopped,
    259 			 * then start it.
    260 			 */
    261 			(void)mcinit(sc);
    262 		} else {
    263 			/*
    264 			 * reset the interface to pick up any other changes
    265 			 * in flags
    266 			 */
    267 			mcreset(sc);
    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 			if (ifp->if_flags & IFF_RUNNING)
    285 				mcreset(sc);
    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;
    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 
    456 	printf("mcwatchdog: resetting chip\n");
    457 	mcreset(sc);
    458 }
    459 
    460 /*
    461  * stuff packet into MACE (at splnet)
    462  */
    463 integrate u_int
    464 maceput(sc, m)
    465 	struct mc_softc *sc;
    466 	struct mbuf *m;
    467 {
    468 	struct mbuf *n;
    469 	u_int len, totlen = 0;
    470 	u_char *buff;
    471 
    472 	buff = sc->sc_txbuf;
    473 
    474 	for (; m; m = n) {
    475 		u_char *data = mtod(m, u_char *);
    476 		len = m->m_len;
    477 		totlen += len;
    478 		memcpy(buff, data, len);
    479 		buff += len;
    480 		MFREE(m, n);
    481 	}
    482 
    483 	if (totlen > PAGE_SIZE)
    484 		panic("%s: maceput: packet overflow", sc->sc_dev.dv_xname);
    485 
    486 #if 0
    487 	if (totlen < ETHERMIN + sizeof(struct ether_header)) {
    488 		int pad = ETHERMIN + sizeof(struct ether_header) - totlen;
    489 		memset(sc->sc_txbuf + totlen, 0, pad);
    490 		totlen = ETHERMIN + sizeof(struct ether_header);
    491 	}
    492 #endif
    493 
    494 	(*sc->sc_putpacket)(sc, totlen);
    495 
    496 	sc->sc_if.if_timer = 5;	/* 5 seconds to watch for failing to transmit */
    497 	return (totlen);
    498 }
    499 
    500 int
    501 mcintr(arg)
    502 	void *arg;
    503 {
    504 	struct mc_softc *sc = arg;
    505 	u_int8_t ir;
    506 
    507 	ir = NIC_GET(sc, MACE_IR) & ~NIC_GET(sc, MACE_IMR);
    508 	if (ir == 0)
    509 		return 0;
    510 
    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 	return 1;
    543 }
    544 
    545 integrate void
    546 mc_tint(sc)
    547 	struct mc_softc *sc;
    548 {
    549 	u_int8_t xmtrc, xmtfs;
    550 
    551 	xmtrc = NIC_GET(sc, MACE_XMTRC);
    552 	xmtfs = NIC_GET(sc, MACE_XMTFS);
    553 
    554 	if ((xmtfs & XMTSV) == 0)
    555 		return;
    556 
    557 	if (xmtfs & UFLO) {
    558 		printf("%s: underflow\n", sc->sc_dev.dv_xname);
    559 		mcreset(sc);
    560 		return;
    561 	}
    562 
    563 	if (xmtfs & LCOL) {
    564 		printf("%s: late collision\n", sc->sc_dev.dv_xname);
    565 		sc->sc_if.if_oerrors++;
    566 		sc->sc_if.if_collisions++;
    567 	}
    568 
    569 	if (xmtfs & MORE)
    570 		/* Real number is unknown. */
    571 		sc->sc_if.if_collisions += 2;
    572 	else if (xmtfs & ONE)
    573 		sc->sc_if.if_collisions++;
    574 	else if (xmtfs & RTRY) {
    575 		sc->sc_if.if_collisions += 16;
    576 		sc->sc_if.if_oerrors++;
    577 	}
    578 
    579 	if (xmtfs & LCAR) {
    580 		sc->sc_havecarrier = 0;
    581 		printf("%s: lost carrier\n", sc->sc_dev.dv_xname);
    582 		sc->sc_if.if_oerrors++;
    583 	}
    584 
    585 	sc->sc_if.if_flags &= ~IFF_OACTIVE;
    586 	sc->sc_if.if_timer = 0;
    587 	mcstart(&sc->sc_if);
    588 }
    589 
    590 void
    591 mc_rint(sc)
    592 	struct mc_softc *sc;
    593 {
    594 #define	rxf	sc->sc_rxframe
    595 	u_int len;
    596 
    597 	len = (rxf.rx_rcvcnt | ((rxf.rx_rcvsts & 0xf) << 8)) - 4;
    598 
    599 #ifdef MCDEBUG
    600 	if (rxf.rx_rcvsts & 0xf0)
    601 		printf("%s: rcvcnt %02x rcvsts %02x rntpc 0x%02x rcvcc 0x%02x\n",
    602 		    sc->sc_dev.dv_xname, rxf.rx_rcvcnt, rxf.rx_rcvsts,
    603 		    rxf.rx_rntpc, rxf.rx_rcvcc);
    604 #endif
    605 
    606 	if (rxf.rx_rcvsts & OFLO) {
    607 		printf("%s: receive FIFO overflow\n", sc->sc_dev.dv_xname);
    608 		sc->sc_if.if_ierrors++;
    609 		return;
    610 	}
    611 
    612 	if (rxf.rx_rcvsts & CLSN)
    613 		sc->sc_if.if_collisions++;
    614 
    615 	if (rxf.rx_rcvsts & FRAM) {
    616 #ifdef MCDEBUG
    617 		printf("%s: framing error\n", sc->sc_dev.dv_xname);
    618 #endif
    619 		sc->sc_if.if_ierrors++;
    620 		return;
    621 	}
    622 
    623 	if (rxf.rx_rcvsts & FCS) {
    624 #ifdef MCDEBUG
    625 		printf("%s: frame control checksum error\n", sc->sc_dev.dv_xname);
    626 #endif
    627 		sc->sc_if.if_ierrors++;
    628 		return;
    629 	}
    630 
    631 	mace_read(sc, rxf.rx_frame, len);
    632 #undef	rxf
    633 }
    634 
    635 integrate void
    636 mace_read(sc, pkt, len)
    637 	struct mc_softc *sc;
    638 	caddr_t pkt;
    639 	int len;
    640 {
    641 	struct ifnet *ifp = &sc->sc_if;
    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 	m = mace_get(sc, pkt, len);
    655 	if (m == NULL) {
    656 		ifp->if_ierrors++;
    657 		return;
    658 	}
    659 
    660 	ifp->if_ipackets++;
    661 
    662 #if NBPFILTER > 0
    663 	/* Pass this up to any BPF listeners. */
    664 	if (ifp->if_bpf)
    665 		bpf_mtap(ifp->if_bpf, m);
    666 #endif
    667 
    668 	/* Pass the packet up. */
    669 	(*ifp->if_input)(ifp, m);
    670 }
    671 
    672 /*
    673  * Pull data off an interface.
    674  * Len is length of data, with local net header stripped.
    675  * We copy the data into mbufs.  When full cluster sized units are present
    676  * we copy into clusters.
    677  */
    678 integrate struct mbuf *
    679 mace_get(sc, pkt, totlen)
    680 	struct mc_softc *sc;
    681 	caddr_t pkt;
    682 	int totlen;
    683 {
    684 	register struct mbuf *m;
    685 	struct mbuf *top, **mp;
    686 	int len;
    687 
    688 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    689 	if (m == 0)
    690 		return (0);
    691 	m->m_pkthdr.rcvif = &sc->sc_if;
    692 	m->m_pkthdr.len = totlen;
    693 	len = MHLEN;
    694 	top = 0;
    695 	mp = &top;
    696 
    697 	while (totlen > 0) {
    698 		if (top) {
    699 			MGET(m, M_DONTWAIT, MT_DATA);
    700 			if (m == 0) {
    701 				m_freem(top);
    702 				return 0;
    703 			}
    704 			len = MLEN;
    705 		}
    706 		if (totlen >= MINCLSIZE) {
    707 			MCLGET(m, M_DONTWAIT);
    708 			if ((m->m_flags & M_EXT) == 0) {
    709 				m_free(m);
    710 				m_freem(top);
    711 				return 0;
    712 			}
    713 			len = MCLBYTES;
    714 		}
    715 		m->m_len = len = min(totlen, len);
    716 		memcpy(mtod(m, caddr_t), pkt, len);
    717 		pkt += len;
    718 		totlen -= len;
    719 		*mp = m;
    720 		mp = &m->m_next;
    721 	}
    722 
    723 	return (top);
    724 }
    725 
    726 /*
    727  * Go through the list of multicast addresses and calculate the logical
    728  * address filter.
    729  */
    730 void
    731 mace_calcladrf(ac, af)
    732 	struct ethercom *ac;
    733 	u_int8_t *af;
    734 {
    735 	struct ifnet *ifp = &ac->ec_if;
    736 	struct ether_multi *enm;
    737 	register u_char *cp, c;
    738 	register u_int32_t crc;
    739 	register int i, len;
    740 	struct ether_multistep step;
    741 
    742 	/*
    743 	 * Set up multicast address filter by passing all multicast addresses
    744 	 * through a crc generator, and then using the high order 6 bits as an
    745 	 * index into the 64 bit logical address filter.  The high order bit
    746 	 * selects the word, while the rest of the bits select the bit within
    747 	 * the word.
    748 	 */
    749 
    750 	*((u_int32_t *)af) = *((u_int32_t *)af + 1) = 0;
    751 
    752 	ETHER_FIRST_MULTI(step, ac, enm);
    753 	while (enm != NULL) {
    754 		if (ETHER_CMP(enm->enm_addrlo, enm->enm_addrhi)) {
    755 			/*
    756 			 * We must listen to a range of multicast addresses.
    757 			 * For now, just accept all multicasts, rather than
    758 			 * trying to set only those filter bits needed to match
    759 			 * the range.  (At this time, the only use of address
    760 			 * ranges is for IP multicast routing, for which the
    761 			 * range is big enough to require all bits set.)
    762 			 */
    763 			goto allmulti;
    764 		}
    765 
    766 		cp = enm->enm_addrlo;
    767 		crc = 0xffffffff;
    768 		for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
    769 			c = *cp++;
    770 			for (i = 8; --i >= 0;) {
    771 				if ((crc & 0x01) ^ (c & 0x01)) {
    772 					crc >>= 1;
    773 					crc ^= 0xedb88320;
    774 				} else
    775 					crc >>= 1;
    776 				c >>= 1;
    777 			}
    778 		}
    779 		/* Just want the 6 most significant bits. */
    780 		crc >>= 26;
    781 
    782 		/* Set the corresponding bit in the filter. */
    783 		af[crc >> 3] |= 1 << (crc & 7);
    784 
    785 		ETHER_NEXT_MULTI(step, enm);
    786 	}
    787 	ifp->if_flags &= ~IFF_ALLMULTI;
    788 	return;
    789 
    790 allmulti:
    791 	ifp->if_flags |= IFF_ALLMULTI;
    792 	*((u_int32_t *)af) = *((u_int32_t *)af + 1) = 0xffffffff;
    793 }
    794 
    795 int
    796 mc_mediachange(ifp)
    797 	struct ifnet *ifp;
    798 {
    799 	return EINVAL;
    800 }
    801 
    802 void
    803 mc_mediastatus(ifp, ifmr)
    804 	struct ifnet *ifp;
    805 	struct ifmediareq *ifmr;
    806 {
    807 	struct mc_softc *sc = ifp->if_softc;
    808 
    809 	if ((ifp->if_flags & IFF_UP) == 0)
    810 		return;
    811 
    812 	if (sc->sc_havecarrier)
    813 		ifmr->ifm_status |= IFM_ACTIVE;
    814 }
    815