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