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