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