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