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