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