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