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