Home | History | Annotate | Line # | Download | only in ic
lance.c revision 1.45
      1 /*	$NetBSD: lance.c,v 1.45 2010/04/05 07:19:35 joerg Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
      9  * Simulation Facility, NASA Ames Research Center.
     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. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*-
     34  * Copyright (c) 1992, 1993
     35  *	The Regents of the University of California.  All rights reserved.
     36  *
     37  * This code is derived from software contributed to Berkeley by
     38  * Ralph Campbell and Rick Macklem.
     39  *
     40  * Redistribution and use in source and binary forms, with or without
     41  * modification, are permitted provided that the following conditions
     42  * are met:
     43  * 1. Redistributions of source code must retain the above copyright
     44  *    notice, this list of conditions and the following disclaimer.
     45  * 2. Redistributions in binary form must reproduce the above copyright
     46  *    notice, this list of conditions and the following disclaimer in the
     47  *    documentation and/or other materials provided with the distribution.
     48  * 3. Neither the name of the University nor the names of its contributors
     49  *    may be used to endorse or promote products derived from this software
     50  *    without specific prior written permission.
     51  *
     52  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     62  * SUCH DAMAGE.
     63  *
     64  *	@(#)if_le.c	8.2 (Berkeley) 11/16/93
     65  */
     66 
     67 #include <sys/cdefs.h>
     68 __KERNEL_RCSID(0, "$NetBSD: lance.c,v 1.45 2010/04/05 07:19:35 joerg Exp $");
     69 
     70 #include "rnd.h"
     71 
     72 #include <sys/param.h>
     73 #include <sys/systm.h>
     74 #include <sys/mbuf.h>
     75 #include <sys/syslog.h>
     76 #include <sys/socket.h>
     77 #include <sys/device.h>
     78 #include <sys/malloc.h>
     79 #include <sys/ioctl.h>
     80 #include <sys/errno.h>
     81 #if NRND > 0
     82 #include <sys/rnd.h>
     83 #endif
     84 
     85 #include <net/if.h>
     86 #include <net/if_dl.h>
     87 #include <net/if_ether.h>
     88 #include <net/if_media.h>
     89 
     90 
     91 #include <net/bpf.h>
     92 #include <net/bpfdesc.h>
     93 
     94 #include <dev/ic/lancereg.h>
     95 #include <dev/ic/lancevar.h>
     96 
     97 #if defined(_KERNEL_OPT)
     98 #include "opt_ddb.h"
     99 #endif
    100 
    101 #ifdef DDB
    102 #define	integrate
    103 #define hide
    104 #else
    105 #define	integrate	static inline
    106 #define hide		static
    107 #endif
    108 
    109 integrate struct mbuf *lance_get(struct lance_softc *, int, int);
    110 
    111 hide bool lance_shutdown(device_t, int);
    112 
    113 int lance_mediachange(struct ifnet *);
    114 void lance_mediastatus(struct ifnet *, struct ifmediareq *);
    115 
    116 static inline u_int16_t ether_cmp(void *, void *);
    117 
    118 void lance_stop(struct ifnet *, int);
    119 int lance_ioctl(struct ifnet *, u_long, void *);
    120 void lance_watchdog(struct ifnet *);
    121 
    122 /*
    123  * Compare two Ether/802 addresses for equality, inlined and
    124  * unrolled for speed.  Use this like memcmp().
    125  *
    126  * XXX: Add <machine/inlines.h> for stuff like this?
    127  * XXX: or maybe add it to libkern.h instead?
    128  *
    129  * "I'd love to have an inline assembler version of this."
    130  * XXX: Who wanted that? mycroft?  I wrote one, but this
    131  * version in C is as good as hand-coded assembly. -gwr
    132  *
    133  * Please do NOT tweak this without looking at the actual
    134  * assembly code generated before and after your tweaks!
    135  */
    136 static inline uint16_t
    137 ether_cmp(void *one, void *two)
    138 {
    139 	uint16_t *a = (uint16_t *)one;
    140 	uint16_t *b = (uint16_t *)two;
    141 	uint16_t diff;
    142 
    143 #ifdef	m68k
    144 	/*
    145 	 * The post-increment-pointer form produces the best
    146 	 * machine code for m68k.  This was carefully tuned
    147 	 * so it compiles to just 8 short (2-byte) op-codes!
    148 	 */
    149 	diff  = *a++ - *b++;
    150 	diff |= *a++ - *b++;
    151 	diff |= *a++ - *b++;
    152 #else
    153 	/*
    154 	 * Most modern CPUs do better with a single expresion.
    155 	 * Note that short-cut evaluation is NOT helpful here,
    156 	 * because it just makes the code longer, not faster!
    157 	 */
    158 	diff = (a[0] - b[0]) | (a[1] - b[1]) | (a[2] - b[2]);
    159 #endif
    160 
    161 	return (diff);
    162 }
    163 
    164 #define ETHER_CMP	ether_cmp
    165 
    166 #ifdef LANCE_REVC_BUG
    167 /* Make sure this is short-aligned, for ether_cmp(). */
    168 static uint16_t bcast_enaddr[3] = { ~0, ~0, ~0 };
    169 #endif
    170 
    171 void
    172 lance_config(struct lance_softc *sc)
    173 {
    174 	int i, nbuf;
    175 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    176 
    177 	/* Initialize ifnet structure. */
    178 	strcpy(ifp->if_xname, device_xname(sc->sc_dev));
    179 	ifp->if_softc = sc;
    180 	ifp->if_start = sc->sc_start;
    181 	ifp->if_ioctl = lance_ioctl;
    182 	ifp->if_watchdog = lance_watchdog;
    183 	ifp->if_init = lance_init;
    184 	ifp->if_stop = lance_stop;
    185 	ifp->if_flags =
    186 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    187 #ifdef LANCE_REVC_BUG
    188 	ifp->if_flags &= ~IFF_MULTICAST;
    189 #endif
    190 	IFQ_SET_READY(&ifp->if_snd);
    191 
    192 	/* Initialize ifmedia structures. */
    193 	ifmedia_init(&sc->sc_media, 0, lance_mediachange, lance_mediastatus);
    194 	if (sc->sc_supmedia != NULL) {
    195 		for (i = 0; i < sc->sc_nsupmedia; i++)
    196 			ifmedia_add(&sc->sc_media, sc->sc_supmedia[i],
    197 			   0, NULL);
    198 		ifmedia_set(&sc->sc_media, sc->sc_defaultmedia);
    199 	} else {
    200 		ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
    201 		ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
    202 	}
    203 
    204 	switch (sc->sc_memsize) {
    205 	case 8192:
    206 		sc->sc_nrbuf = 4;
    207 		sc->sc_ntbuf = 1;
    208 		break;
    209 	case 16384:
    210 		sc->sc_nrbuf = 8;
    211 		sc->sc_ntbuf = 2;
    212 		break;
    213 	case 32768:
    214 		sc->sc_nrbuf = 16;
    215 		sc->sc_ntbuf = 4;
    216 		break;
    217 	case 65536:
    218 		sc->sc_nrbuf = 32;
    219 		sc->sc_ntbuf = 8;
    220 		break;
    221 	case 131072:
    222 		sc->sc_nrbuf = 64;
    223 		sc->sc_ntbuf = 16;
    224 		break;
    225 	case 262144:
    226 		sc->sc_nrbuf = 128;
    227 		sc->sc_ntbuf = 32;
    228 		break;
    229 	default:
    230 		/* weird memory size; cope with it */
    231 		nbuf = sc->sc_memsize / LEBLEN;
    232 		sc->sc_ntbuf = nbuf / 5;
    233 		sc->sc_nrbuf = nbuf - sc->sc_ntbuf;
    234 	}
    235 
    236 	aprint_normal(": address %s\n", ether_sprintf(sc->sc_enaddr));
    237 	aprint_normal_dev(sc->sc_dev,
    238 	    "%d receive buffers, %d transmit buffers\n",
    239 	    sc->sc_nrbuf, sc->sc_ntbuf);
    240 
    241 	/* Make sure the chip is stopped. */
    242 	lance_stop(ifp, 0);
    243 
    244 	/* claim 802.1q capability */
    245 	sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
    246 	/* Attach the interface. */
    247 	if_attach(ifp);
    248 	ether_ifattach(ifp, sc->sc_enaddr);
    249 
    250 	if (pmf_device_register1(sc->sc_dev, NULL, NULL, lance_shutdown))
    251 		pmf_class_network_register(sc->sc_dev, ifp);
    252 	else
    253 		aprint_error_dev(sc->sc_dev,
    254 		    "couldn't establish power handler\n");
    255 
    256 	sc->sc_rbufaddr = malloc(sc->sc_nrbuf * sizeof(int), M_DEVBUF,
    257 					M_WAITOK);
    258 	sc->sc_tbufaddr = malloc(sc->sc_ntbuf * sizeof(int), M_DEVBUF,
    259 					M_WAITOK);
    260 
    261 #if NRND > 0
    262 	rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
    263 			  RND_TYPE_NET, 0);
    264 #endif
    265 }
    266 
    267 void
    268 lance_reset(struct lance_softc *sc)
    269 {
    270 	int s;
    271 
    272 	s = splnet();
    273 	lance_init(&sc->sc_ethercom.ec_if);
    274 	splx(s);
    275 }
    276 
    277 void
    278 lance_stop(struct ifnet *ifp, int disable)
    279 {
    280 	struct lance_softc *sc = ifp->if_softc;
    281 
    282 	(*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_STOP);
    283 }
    284 
    285 /*
    286  * Initialization of interface; set up initialization block
    287  * and transmit/receive descriptor rings.
    288  */
    289 int
    290 lance_init(struct ifnet *ifp)
    291 {
    292 	struct lance_softc *sc = ifp->if_softc;
    293 	int timo;
    294 	u_long a;
    295 
    296 	(*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_STOP);
    297 	DELAY(100);
    298 
    299 	/* Newer LANCE chips have a reset register */
    300 	if (sc->sc_hwreset)
    301 		(*sc->sc_hwreset)(sc);
    302 
    303 	/* Set the correct byte swapping mode, etc. */
    304 	(*sc->sc_wrcsr)(sc, LE_CSR3, sc->sc_conf3);
    305 
    306 	/* Set up LANCE init block. */
    307 	(*sc->sc_meminit)(sc);
    308 
    309 	/* Give LANCE the physical address of its init block. */
    310 	a = sc->sc_addr + LE_INITADDR(sc);
    311 	(*sc->sc_wrcsr)(sc, LE_CSR1, a);
    312 	(*sc->sc_wrcsr)(sc, LE_CSR2, a >> 16);
    313 
    314 	/* Try to initialize the LANCE. */
    315 	DELAY(100);
    316 	(*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INIT);
    317 
    318 	/* Wait for initialization to finish. */
    319 	for (timo = 100000; timo; timo--)
    320 		if ((*sc->sc_rdcsr)(sc, LE_CSR0) & LE_C0_IDON)
    321 			break;
    322 
    323 	if ((*sc->sc_rdcsr)(sc, LE_CSR0) & LE_C0_IDON) {
    324 		/* Start the LANCE. */
    325 		(*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA | LE_C0_STRT);
    326 		ifp->if_flags |= IFF_RUNNING;
    327 		ifp->if_flags &= ~IFF_OACTIVE;
    328 		ifp->if_timer = 0;
    329 		(*sc->sc_start)(ifp);
    330 	} else
    331 		printf("%s: controller failed to initialize\n",
    332 			device_xname(sc->sc_dev));
    333 	if (sc->sc_hwinit)
    334 		(*sc->sc_hwinit)(sc);
    335 
    336 	return (0);
    337 }
    338 
    339 /*
    340  * Routine to copy from mbuf chain to transmit buffer in
    341  * network buffer memory.
    342  */
    343 int
    344 lance_put(struct lance_softc *sc, int boff, struct mbuf *m)
    345 {
    346 	struct mbuf *n;
    347 	int len, tlen = 0;
    348 
    349 	for (; m; m = n) {
    350 		len = m->m_len;
    351 		if (len == 0) {
    352 			MFREE(m, n);
    353 			continue;
    354 		}
    355 		(*sc->sc_copytobuf)(sc, mtod(m, void *), boff, len);
    356 		boff += len;
    357 		tlen += len;
    358 		MFREE(m, n);
    359 	}
    360 	if (tlen < LEMINSIZE) {
    361 		(*sc->sc_zerobuf)(sc, boff, LEMINSIZE - tlen);
    362 		tlen = LEMINSIZE;
    363 	}
    364 	return (tlen);
    365 }
    366 
    367 /*
    368  * Pull data off an interface.
    369  * Len is length of data, with local net header stripped.
    370  * We copy the data into mbufs.  When full cluster sized units are present
    371  * we copy into clusters.
    372  */
    373 integrate struct mbuf *
    374 lance_get(struct lance_softc *sc, int boff, int totlen)
    375 {
    376 	struct mbuf *m, *m0, *newm;
    377 	int len;
    378 
    379 	MGETHDR(m0, M_DONTWAIT, MT_DATA);
    380 	if (m0 == 0)
    381 		return (0);
    382 	m0->m_pkthdr.rcvif = &sc->sc_ethercom.ec_if;
    383 	m0->m_pkthdr.len = totlen;
    384 	len = MHLEN;
    385 	m = m0;
    386 
    387 	while (totlen > 0) {
    388 		if (totlen >= MINCLSIZE) {
    389 			MCLGET(m, M_DONTWAIT);
    390 			if ((m->m_flags & M_EXT) == 0)
    391 				goto bad;
    392 			len = MCLBYTES;
    393 		}
    394 
    395 		if (m == m0) {
    396 			char *newdata = (char *)
    397 			    ALIGN(m->m_data + sizeof(struct ether_header)) -
    398 			    sizeof(struct ether_header);
    399 			len -= newdata - m->m_data;
    400 			m->m_data = newdata;
    401 		}
    402 
    403 		m->m_len = len = min(totlen, len);
    404 		(*sc->sc_copyfrombuf)(sc, mtod(m, void *), boff, len);
    405 		boff += len;
    406 
    407 		totlen -= len;
    408 		if (totlen > 0) {
    409 			MGET(newm, M_DONTWAIT, MT_DATA);
    410 			if (newm == 0)
    411 				goto bad;
    412 			len = MLEN;
    413 			m = m->m_next = newm;
    414 		}
    415 	}
    416 
    417 	return (m0);
    418 
    419 bad:
    420 	m_freem(m0);
    421 	return (0);
    422 }
    423 
    424 /*
    425  * Pass a packet to the higher levels.
    426  */
    427 void
    428 lance_read(struct lance_softc *sc, int boff, int len)
    429 {
    430 	struct mbuf *m;
    431 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    432 	struct ether_header *eh;
    433 
    434 	if (len <= sizeof(struct ether_header) ||
    435 	    len > ((sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) ?
    436 		ETHER_VLAN_ENCAP_LEN + ETHERMTU + sizeof(struct ether_header) :
    437 		ETHERMTU + sizeof(struct ether_header))) {
    438 #ifdef LEDEBUG
    439 		printf("%s: invalid packet size %d; dropping\n",
    440 		    device_xname(sc->sc_dev), len);
    441 #endif
    442 		ifp->if_ierrors++;
    443 		return;
    444 	}
    445 
    446 	/* Pull packet off interface. */
    447 	m = lance_get(sc, boff, len);
    448 	if (m == 0) {
    449 		ifp->if_ierrors++;
    450 		return;
    451 	}
    452 
    453 	ifp->if_ipackets++;
    454 
    455 	eh = mtod(m, struct ether_header *);
    456 
    457 #ifdef LANCE_REVC_BUG
    458 	/*
    459 	 * The old LANCE (Rev. C) chips have a bug which causes
    460 	 * garbage to be inserted in front of the received packet.
    461 	 * The work-around is to ignore packets with an invalid
    462 	 * destination address (garbage will usually not match).
    463 	 * Of course, this precludes multicast support...
    464 	 */
    465 	if (ETHER_CMP(eh->ether_dhost, sc->sc_enaddr) &&
    466 	    ETHER_CMP(eh->ether_dhost, bcast_enaddr)) {
    467 		m_freem(m);
    468 		return;
    469 	}
    470 #endif
    471 
    472 	/*
    473 	 * Some lance device does not present IFF_SIMPLEX behavior on multicast
    474 	 * packets.  Make sure to drop it if it is from ourselves.
    475 	 */
    476 	if (!ETHER_CMP(eh->ether_shost, sc->sc_enaddr)) {
    477 		m_freem(m);
    478 		return;
    479 	}
    480 
    481 	/*
    482 	 * Check if there's a BPF listener on this interface.
    483 	 * If so, hand off the raw packet to BPF.
    484 	 */
    485 	bpf_mtap(ifp, m);
    486 
    487 	/* Pass the packet up. */
    488 	(*ifp->if_input)(ifp, m);
    489 }
    490 
    491 #undef	ifp
    492 
    493 void
    494 lance_watchdog(struct ifnet *ifp)
    495 {
    496 	struct lance_softc *sc = ifp->if_softc;
    497 
    498 	log(LOG_ERR, "%s: device timeout\n", device_xname(sc->sc_dev));
    499 	++ifp->if_oerrors;
    500 
    501 	lance_reset(sc);
    502 }
    503 
    504 int
    505 lance_mediachange(struct ifnet *ifp)
    506 {
    507 	struct lance_softc *sc = ifp->if_softc;
    508 
    509 	if (sc->sc_mediachange)
    510 		return ((*sc->sc_mediachange)(sc));
    511 	return (0);
    512 }
    513 
    514 void
    515 lance_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
    516 {
    517 	struct lance_softc *sc = ifp->if_softc;
    518 
    519 	if ((ifp->if_flags & IFF_UP) == 0)
    520 		return;
    521 
    522 	ifmr->ifm_status = IFM_AVALID;
    523 	if (sc->sc_havecarrier)
    524 		ifmr->ifm_status |= IFM_ACTIVE;
    525 
    526 	if (sc->sc_mediastatus)
    527 		(*sc->sc_mediastatus)(sc, ifmr);
    528 }
    529 
    530 /*
    531  * Process an ioctl request.
    532  */
    533 int
    534 lance_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    535 {
    536 	struct lance_softc *sc = ifp->if_softc;
    537 	struct ifreq *ifr = (struct ifreq *)data;
    538 	int s, error = 0;
    539 
    540 	s = splnet();
    541 
    542 	switch (cmd) {
    543 	case SIOCGIFMEDIA:
    544 	case SIOCSIFMEDIA:
    545 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
    546 		break;
    547 	default:
    548 		if ((error = ether_ioctl(ifp, cmd, data)) != ENETRESET)
    549 			break;
    550 		error = 0;
    551 		if (cmd != SIOCADDMULTI && cmd != SIOCDELMULTI)
    552 			break;
    553 		if (ifp->if_flags & IFF_RUNNING) {
    554 			/*
    555 			 * Multicast list has changed; set the hardware filter
    556 			 * accordingly.
    557 			 */
    558 			lance_reset(sc);
    559 		}
    560 		break;
    561 
    562 	}
    563 
    564 	splx(s);
    565 	return (error);
    566 }
    567 
    568 hide bool
    569 lance_shutdown(device_t self, int howto)
    570 {
    571 	struct lance_softc *sc = device_private(self);
    572 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    573 
    574 	lance_stop(ifp, 0);
    575 
    576 	return true;
    577 }
    578 
    579 /*
    580  * Set up the logical address filter.
    581  */
    582 void
    583 lance_setladrf(struct ethercom *ac, uint16_t *af)
    584 {
    585 	struct ifnet *ifp = &ac->ec_if;
    586 	struct ether_multi *enm;
    587 	uint32_t crc;
    588 	struct ether_multistep step;
    589 
    590 	/*
    591 	 * Set up multicast address filter by passing all multicast addresses
    592 	 * through a crc generator, and then using the high order 6 bits as an
    593 	 * index into the 64 bit logical address filter.  The high order bit
    594 	 * selects the word, while the rest of the bits select the bit within
    595 	 * the word.
    596 	 */
    597 
    598 	if (ifp->if_flags & IFF_PROMISC)
    599 		goto allmulti;
    600 
    601 	af[0] = af[1] = af[2] = af[3] = 0x0000;
    602 	ETHER_FIRST_MULTI(step, ac, enm);
    603 	while (enm != NULL) {
    604 		if (ETHER_CMP(enm->enm_addrlo, enm->enm_addrhi)) {
    605 			/*
    606 			 * We must listen to a range of multicast addresses.
    607 			 * For now, just accept all multicasts, rather than
    608 			 * trying to set only those filter bits needed to match
    609 			 * the range.  (At this time, the only use of address
    610 			 * ranges is for IP multicast routing, for which the
    611 			 * range is big enough to require all bits set.)
    612 			 */
    613 			goto allmulti;
    614 		}
    615 
    616 		crc = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
    617 
    618 		/* Just want the 6 most significant bits. */
    619 		crc >>= 26;
    620 
    621 		/* Set the corresponding bit in the filter. */
    622 		af[crc >> 4] |= 1 << (crc & 0xf);
    623 
    624 		ETHER_NEXT_MULTI(step, enm);
    625 	}
    626 	ifp->if_flags &= ~IFF_ALLMULTI;
    627 	return;
    628 
    629 allmulti:
    630 	ifp->if_flags |= IFF_ALLMULTI;
    631 	af[0] = af[1] = af[2] = af[3] = 0xffff;
    632 }
    633 
    634 /*
    635  * Routines for accessing the transmit and receive buffers.
    636  * The various CPU and adapter configurations supported by this
    637  * driver require three different access methods for buffers
    638  * and descriptors:
    639  *	(1) contig (contiguous data; no padding),
    640  *	(2) gap2 (two bytes of data followed by two bytes of padding),
    641  *	(3) gap16 (16 bytes of data followed by 16 bytes of padding).
    642  */
    643 
    644 /*
    645  * contig: contiguous data with no padding.
    646  *
    647  * Buffers may have any alignment.
    648  */
    649 
    650 void
    651 lance_copytobuf_contig(struct lance_softc *sc, void *from, int boff, int len)
    652 {
    653 	uint8_t *buf = sc->sc_mem;
    654 
    655 	/*
    656 	 * Just call memcpy() to do the work.
    657 	 */
    658 	memcpy(buf + boff, from, len);
    659 }
    660 
    661 void
    662 lance_copyfrombuf_contig(struct lance_softc *sc, void *to, int boff, int len)
    663 {
    664 	uint8_t *buf = sc->sc_mem;
    665 
    666 	/*
    667 	 * Just call memcpy() to do the work.
    668 	 */
    669 	memcpy(to, buf + boff, len);
    670 }
    671 
    672 void
    673 lance_zerobuf_contig(struct lance_softc *sc, int boff, int len)
    674 {
    675 	uint8_t *buf = sc->sc_mem;
    676 
    677 	/*
    678 	 * Just let memset() do the work
    679 	 */
    680 	memset(buf + boff, 0, len);
    681 }
    682 
    683 #if 0
    684 /*
    685  * Examples only; duplicate these and tweak (if necessary) in
    686  * machine-specific front-ends.
    687  */
    688 
    689 /*
    690  * gap2: two bytes of data followed by two bytes of pad.
    691  *
    692  * Buffers must be 4-byte aligned.  The code doesn't worry about
    693  * doing an extra byte.
    694  */
    695 
    696 void
    697 lance_copytobuf_gap2(struct lance_softc *sc, void *fromv, int boff, int len)
    698 {
    699 	volatile void *buf = sc->sc_mem;
    700 	void *from = fromv;
    701 	volatile uint16_t *bptr;
    702 
    703 	if (boff & 0x1) {
    704 		/* handle unaligned first byte */
    705 		bptr = ((volatile uint16_t *)buf) + (boff - 1);
    706 		*bptr = (*from++ << 8) | (*bptr & 0xff);
    707 		bptr += 2;
    708 		len--;
    709 	} else
    710 		bptr = ((volatile uint16_t *)buf) + boff;
    711 	while (len > 1) {
    712 		*bptr = (from[1] << 8) | (from[0] & 0xff);
    713 		bptr += 2;
    714 		from += 2;
    715 		len -= 2;
    716 	}
    717 	if (len == 1)
    718 		*bptr = (uint16_t)*from;
    719 }
    720 
    721 void
    722 lance_copyfrombuf_gap2(struct lance_softc *sc, void *tov, int boff, int len)
    723 {
    724 	volatile void *buf = sc->sc_mem;
    725 	void *to = tov;
    726 	volatile uint16_t *bptr;
    727 	uint16_t tmp;
    728 
    729 	if (boff & 0x1) {
    730 		/* handle unaligned first byte */
    731 		bptr = ((volatile uint16_t *)buf) + (boff - 1);
    732 		*to++ = (*bptr >> 8) & 0xff;
    733 		bptr += 2;
    734 		len--;
    735 	} else
    736 		bptr = ((volatile uint16_t *)buf) + boff;
    737 	while (len > 1) {
    738 		tmp = *bptr;
    739 		*to++ = tmp & 0xff;
    740 		*to++ = (tmp >> 8) & 0xff;
    741 		bptr += 2;
    742 		len -= 2;
    743 	}
    744 	if (len == 1)
    745 		*to = *bptr & 0xff;
    746 }
    747 
    748 void
    749 lance_zerobuf_gap2(struct lance_softc *sc, int boff, int len)
    750 {
    751 	volatile void *buf = sc->sc_mem;
    752 	volatile uint16_t *bptr;
    753 
    754 	if ((unsigned int)boff & 0x1) {
    755 		bptr = ((volatile uint16_t *)buf) + (boff - 1);
    756 		*bptr &= 0xff;
    757 		bptr += 2;
    758 		len--;
    759 	} else
    760 		bptr = ((volatile uint16_t *)buf) + boff;
    761 	while (len > 0) {
    762 		*bptr = 0;
    763 		bptr += 2;
    764 		len -= 2;
    765 	}
    766 }
    767 
    768 /*
    769  * gap16: 16 bytes of data followed by 16 bytes of pad.
    770  *
    771  * Buffers must be 32-byte aligned.
    772  */
    773 
    774 void
    775 lance_copytobuf_gap16(struct lance_softc *sc, void *fromv, int boff, int len)
    776 {
    777 	volatile uint8_t *buf = sc->sc_mem;
    778 	void *from = fromv;
    779 	uint8_t *bptr;
    780 	int xfer;
    781 
    782 	bptr = buf + ((boff << 1) & ~0x1f);
    783 	boff &= 0xf;
    784 	xfer = min(len, 16 - boff);
    785 	while (len > 0) {
    786 		memcpy(bptr + boff, from, xfer);
    787 		from += xfer;
    788 		bptr += 32;
    789 		boff = 0;
    790 		len -= xfer;
    791 		xfer = min(len, 16);
    792 	}
    793 }
    794 
    795 void
    796 lance_copyfrombuf_gap16(struct lance_softc *sc, void *tov, int boff, int len)
    797 {
    798 	volatile uint8_t *buf = sc->sc_mem;
    799 	void *to = tov;
    800 	uint8_t *bptr;
    801 	int xfer;
    802 
    803 	bptr = buf + ((boff << 1) & ~0x1f);
    804 	boff &= 0xf;
    805 	xfer = min(len, 16 - boff);
    806 	while (len > 0) {
    807 		memcpy(to, bptr + boff, xfer);
    808 		to += xfer;
    809 		bptr += 32;
    810 		boff = 0;
    811 		len -= xfer;
    812 		xfer = min(len, 16);
    813 	}
    814 }
    815 
    816 void
    817 lance_zerobuf_gap16(struct lance_softc *sc, int boff, int len)
    818 {
    819 	volatile uint8_t *buf = sc->sc_mem;
    820 	uint8_t *bptr;
    821 	int xfer;
    822 
    823 	bptr = buf + ((boff << 1) & ~0x1f);
    824 	boff &= 0xf;
    825 	xfer = min(len, 16 - boff);
    826 	while (len > 0) {
    827 		memset(bptr + boff, 0, xfer);
    828 		bptr += 32;
    829 		boff = 0;
    830 		len -= xfer;
    831 		xfer = min(len, 16);
    832 	}
    833 }
    834 #endif /* Example only */
    835