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