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