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