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