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