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