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