Home | History | Annotate | Line # | Download | only in ic
lance.c revision 1.12.4.1
      1 /*	$NetBSD: lance.c,v 1.12.4.1 2000/12/31 20:14:57 jhawk 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 	struct ether_header *eh;
    471 
    472 	if (len <= sizeof(struct ether_header) ||
    473 	    len > ((sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) ?
    474 		ETHER_VLAN_ENCAP_LEN + ETHERMTU + sizeof(struct ether_header) :
    475 		ETHERMTU + sizeof(struct ether_header))) {
    476 #ifdef LEDEBUG
    477 		printf("%s: invalid packet size %d; dropping\n",
    478 		    sc->sc_dev.dv_xname, len);
    479 #endif
    480 		ifp->if_ierrors++;
    481 		return;
    482 	}
    483 
    484 	/* Pull packet off interface. */
    485 	m = lance_get(sc, boff, len);
    486 	if (m == 0) {
    487 		ifp->if_ierrors++;
    488 		return;
    489 	}
    490 
    491 	ifp->if_ipackets++;
    492 
    493 	/* We assume that the header fit entirely in one mbuf. */
    494 	eh = mtod(m, struct ether_header *);
    495 
    496 #if NBPFILTER > 0
    497 	/*
    498 	 * Check if there's a BPF listener on this interface.
    499 	 * If so, hand off the raw packet to BPF.
    500 	 */
    501 	if (ifp->if_bpf) {
    502 		bpf_mtap(ifp->if_bpf, m);
    503 
    504 #ifndef LANCE_REVC_BUG
    505 		/*
    506 		 * Note that the interface cannot be in promiscuous mode if
    507 		 * there are no BPF listeners.  And if we are in promiscuous
    508 		 * mode, we have to check if this packet is really ours.
    509 		 */
    510 		if ((ifp->if_flags & IFF_PROMISC) != 0 &&
    511 		    (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
    512 		    ETHER_CMP(eh->ether_dhost, sc->sc_enaddr)) {
    513 			m_freem(m);
    514 			return;
    515 		}
    516 #endif
    517 	}
    518 #endif
    519 
    520 #ifdef LANCE_REVC_BUG
    521 	/*
    522 	 * The old LANCE (Rev. C) chips have a bug which causes
    523 	 * garbage to be inserted in front of the received packet.
    524 	 * The work-around is to ignore packets with an invalid
    525 	 * destination address (garbage will usually not match).
    526 	 * Of course, this precludes multicast support...
    527 	 */
    528 	if (ETHER_CMP(eh->ether_dhost, sc->sc_enaddr) &&
    529 	    ETHER_CMP(eh->ether_dhost, bcast_enaddr)) {
    530 		m_freem(m);
    531 		return;
    532 	}
    533 #endif
    534 
    535 	/* Pass the packet up. */
    536 	(*ifp->if_input)(ifp, m);
    537 }
    538 
    539 #undef	ifp
    540 
    541 void
    542 lance_watchdog(ifp)
    543 	struct ifnet *ifp;
    544 {
    545 	struct lance_softc *sc = ifp->if_softc;
    546 
    547 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
    548 	++ifp->if_oerrors;
    549 
    550 	lance_reset(sc);
    551 }
    552 
    553 int
    554 lance_mediachange(ifp)
    555 	struct ifnet *ifp;
    556 {
    557 	struct lance_softc *sc = ifp->if_softc;
    558 
    559 	if (sc->sc_mediachange)
    560 		return ((*sc->sc_mediachange)(sc));
    561 	return (0);
    562 }
    563 
    564 void
    565 lance_mediastatus(ifp, ifmr)
    566 	struct ifnet *ifp;
    567 	struct ifmediareq *ifmr;
    568 {
    569 	struct lance_softc *sc = ifp->if_softc;
    570 
    571 	if ((ifp->if_flags & IFF_UP) == 0)
    572 		return;
    573 
    574 	ifmr->ifm_status = IFM_AVALID;
    575 	if (sc->sc_havecarrier)
    576 		ifmr->ifm_status |= IFM_ACTIVE;
    577 
    578 	if (sc->sc_mediastatus)
    579 		(*sc->sc_mediastatus)(sc, ifmr);
    580 }
    581 
    582 /*
    583  * Process an ioctl request.
    584  */
    585 int
    586 lance_ioctl(ifp, cmd, data)
    587 	struct ifnet *ifp;
    588 	u_long cmd;
    589 	caddr_t data;
    590 {
    591 	struct lance_softc *sc = ifp->if_softc;
    592 	struct ifaddr *ifa = (struct ifaddr *)data;
    593 	struct ifreq *ifr = (struct ifreq *)data;
    594 	int s, error = 0;
    595 
    596 	s = splnet();
    597 
    598 	switch (cmd) {
    599 
    600 	case SIOCSIFADDR:
    601 		ifp->if_flags |= IFF_UP;
    602 
    603 		switch (ifa->ifa_addr->sa_family) {
    604 #ifdef INET
    605 		case AF_INET:
    606 			lance_init(sc);
    607 			arp_ifinit(ifp, ifa);
    608 			break;
    609 #endif
    610 #ifdef NS
    611 		case AF_NS:
    612 		    {
    613 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
    614 
    615 			if (ns_nullhost(*ina))
    616 				ina->x_host =
    617 				    *(union ns_host *)LLADDR(ifp->if_sadl);
    618 			else {
    619 				bcopy(ina->x_host.c_host,
    620 				    LLADDR(ifp->if_sadl),
    621 				    sizeof(sc->sc_enaddr));
    622 			}
    623 			/* Set new address. */
    624 			lance_init(sc);
    625 			break;
    626 		    }
    627 #endif
    628 		default:
    629 			lance_init(sc);
    630 			break;
    631 		}
    632 		break;
    633 
    634 #if defined(CCITT) && defined(LLC)
    635 	case SIOCSIFCONF_X25:
    636 		ifp->if_flags |= IFF_UP;
    637 		ifa->ifa_rtrequest = cons_rtrequest; /* XXX */
    638 		error = x25_llcglue(PRC_IFUP, ifa->ifa_addr);
    639 		if (error == 0)
    640 			lance_init(sc);
    641 		break;
    642 #endif /* CCITT && LLC */
    643 
    644 	case SIOCSIFFLAGS:
    645 		if ((ifp->if_flags & IFF_UP) == 0 &&
    646 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    647 			/*
    648 			 * If interface is marked down and it is running, then
    649 			 * stop it.
    650 			 */
    651 			lance_stop(sc);
    652 			ifp->if_flags &= ~IFF_RUNNING;
    653 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
    654 		    	   (ifp->if_flags & IFF_RUNNING) == 0) {
    655 			/*
    656 			 * If interface is marked up and it is stopped, then
    657 			 * start it.
    658 			 */
    659 			lance_init(sc);
    660 		} else if ((ifp->if_flags & IFF_UP) != 0) {
    661 			/*
    662 			 * Reset the interface to pick up changes in any other
    663 			 * flags that affect hardware registers.
    664 			 */
    665 			/*lance_stop(sc);*/
    666 			lance_init(sc);
    667 		}
    668 #ifdef LEDEBUG
    669 		if (ifp->if_flags & IFF_DEBUG)
    670 			sc->sc_debug = 1;
    671 		else
    672 			sc->sc_debug = 0;
    673 #endif
    674 		break;
    675 
    676 	case SIOCADDMULTI:
    677 	case SIOCDELMULTI:
    678 		error = (cmd == SIOCADDMULTI) ?
    679 		    ether_addmulti(ifr, &sc->sc_ethercom) :
    680 		    ether_delmulti(ifr, &sc->sc_ethercom);
    681 
    682 		if (error == ENETRESET) {
    683 			/*
    684 			 * Multicast list has changed; set the hardware filter
    685 			 * accordingly.
    686 			 */
    687 			lance_reset(sc);
    688 			error = 0;
    689 		}
    690 		break;
    691 
    692 	case SIOCGIFMEDIA:
    693 	case SIOCSIFMEDIA:
    694 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
    695 		break;
    696 
    697 	default:
    698 		error = EINVAL;
    699 		break;
    700 	}
    701 
    702 	splx(s);
    703 	return (error);
    704 }
    705 
    706 hide void
    707 lance_shutdown(arg)
    708 	void *arg;
    709 {
    710 
    711 	lance_stop((struct lance_softc *)arg);
    712 }
    713 
    714 /*
    715  * Set up the logical address filter.
    716  */
    717 void
    718 lance_setladrf(ac, af)
    719 	struct ethercom *ac;
    720 	u_int16_t *af;
    721 {
    722 	struct ifnet *ifp = &ac->ec_if;
    723 	struct ether_multi *enm;
    724 	u_int32_t crc;
    725 	struct ether_multistep step;
    726 
    727 	/*
    728 	 * Set up multicast address filter by passing all multicast addresses
    729 	 * through a crc generator, and then using the high order 6 bits as an
    730 	 * index into the 64 bit logical address filter.  The high order bit
    731 	 * selects the word, while the rest of the bits select the bit within
    732 	 * the word.
    733 	 */
    734 
    735 	if (ifp->if_flags & IFF_PROMISC)
    736 		goto allmulti;
    737 
    738 	af[0] = af[1] = af[2] = af[3] = 0x0000;
    739 	ETHER_FIRST_MULTI(step, ac, enm);
    740 	while (enm != NULL) {
    741 		if (ETHER_CMP(enm->enm_addrlo, enm->enm_addrhi)) {
    742 			/*
    743 			 * We must listen to a range of multicast addresses.
    744 			 * For now, just accept all multicasts, rather than
    745 			 * trying to set only those filter bits needed to match
    746 			 * the range.  (At this time, the only use of address
    747 			 * ranges is for IP multicast routing, for which the
    748 			 * range is big enough to require all bits set.)
    749 			 */
    750 			goto allmulti;
    751 		}
    752 
    753 		crc = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
    754 
    755 		/* Just want the 6 most significant bits. */
    756 		crc >>= 26;
    757 
    758 		/* Set the corresponding bit in the filter. */
    759 		af[crc >> 4] |= 1 << (crc & 0xf);
    760 
    761 		ETHER_NEXT_MULTI(step, enm);
    762 	}
    763 	ifp->if_flags &= ~IFF_ALLMULTI;
    764 	return;
    765 
    766 allmulti:
    767 	ifp->if_flags |= IFF_ALLMULTI;
    768 	af[0] = af[1] = af[2] = af[3] = 0xffff;
    769 }
    770 
    771 /*
    772  * Routines for accessing the transmit and receive buffers.
    773  * The various CPU and adapter configurations supported by this
    774  * driver require three different access methods for buffers
    775  * and descriptors:
    776  *	(1) contig (contiguous data; no padding),
    777  *	(2) gap2 (two bytes of data followed by two bytes of padding),
    778  *	(3) gap16 (16 bytes of data followed by 16 bytes of padding).
    779  */
    780 
    781 /*
    782  * contig: contiguous data with no padding.
    783  *
    784  * Buffers may have any alignment.
    785  */
    786 
    787 void
    788 lance_copytobuf_contig(sc, from, boff, len)
    789 	struct lance_softc *sc;
    790 	void *from;
    791 	int boff, len;
    792 {
    793 	volatile caddr_t buf = sc->sc_mem;
    794 
    795 	/*
    796 	 * Just call bcopy() to do the work.
    797 	 */
    798 	bcopy(from, buf + boff, len);
    799 }
    800 
    801 void
    802 lance_copyfrombuf_contig(sc, to, boff, len)
    803 	struct lance_softc *sc;
    804 	void *to;
    805 	int boff, len;
    806 {
    807 	volatile caddr_t buf = sc->sc_mem;
    808 
    809 	/*
    810 	 * Just call bcopy() to do the work.
    811 	 */
    812 	bcopy(buf + boff, to, len);
    813 }
    814 
    815 void
    816 lance_zerobuf_contig(sc, boff, len)
    817 	struct lance_softc *sc;
    818 	int boff, len;
    819 {
    820 	volatile caddr_t buf = sc->sc_mem;
    821 
    822 	/*
    823 	 * Just let bzero() do the work
    824 	 */
    825 	bzero(buf + boff, len);
    826 }
    827 
    828 #if 0
    829 /*
    830  * Examples only; duplicate these and tweak (if necessary) in
    831  * machine-specific front-ends.
    832  */
    833 
    834 /*
    835  * gap2: two bytes of data followed by two bytes of pad.
    836  *
    837  * Buffers must be 4-byte aligned.  The code doesn't worry about
    838  * doing an extra byte.
    839  */
    840 
    841 void
    842 lance_copytobuf_gap2(sc, fromv, boff, len)
    843 	struct lance_softc *sc;
    844 	void *fromv;
    845 	int boff;
    846 	int len;
    847 {
    848 	volatile caddr_t buf = sc->sc_mem;
    849 	caddr_t from = fromv;
    850 	volatile u_int16_t *bptr;
    851 
    852 	if (boff & 0x1) {
    853 		/* handle unaligned first byte */
    854 		bptr = ((volatile u_int16_t *)buf) + (boff - 1);
    855 		*bptr = (*from++ << 8) | (*bptr & 0xff);
    856 		bptr += 2;
    857 		len--;
    858 	} else
    859 		bptr = ((volatile u_int16_t *)buf) + boff;
    860 	while (len > 1) {
    861 		*bptr = (from[1] << 8) | (from[0] & 0xff);
    862 		bptr += 2;
    863 		from += 2;
    864 		len -= 2;
    865 	}
    866 	if (len == 1)
    867 		*bptr = (u_int16_t)*from;
    868 }
    869 
    870 void
    871 lance_copyfrombuf_gap2(sc, tov, boff, len)
    872 	struct lance_softc *sc;
    873 	void *tov;
    874 	int boff, len;
    875 {
    876 	volatile caddr_t buf = sc->sc_mem;
    877 	caddr_t to = tov;
    878 	volatile u_int16_t *bptr;
    879 	u_int16_t tmp;
    880 
    881 	if (boff & 0x1) {
    882 		/* handle unaligned first byte */
    883 		bptr = ((volatile u_int16_t *)buf) + (boff - 1);
    884 		*to++ = (*bptr >> 8) & 0xff;
    885 		bptr += 2;
    886 		len--;
    887 	} else
    888 		bptr = ((volatile u_int16_t *)buf) + boff;
    889 	while (len > 1) {
    890 		tmp = *bptr;
    891 		*to++ = tmp & 0xff;
    892 		*to++ = (tmp >> 8) & 0xff;
    893 		bptr += 2;
    894 		len -= 2;
    895 	}
    896 	if (len == 1)
    897 		*to = *bptr & 0xff;
    898 }
    899 
    900 void
    901 lance_zerobuf_gap2(sc, boff, len)
    902 	struct lance_softc *sc;
    903 	int boff, len;
    904 {
    905 	volatile caddr_t buf = sc->sc_mem;
    906 	volatile u_int16_t *bptr;
    907 
    908 	if ((unsigned)boff & 0x1) {
    909 		bptr = ((volatile u_int16_t *)buf) + (boff - 1);
    910 		*bptr &= 0xff;
    911 		bptr += 2;
    912 		len--;
    913 	} else
    914 		bptr = ((volatile u_int16_t *)buf) + boff;
    915 	while (len > 0) {
    916 		*bptr = 0;
    917 		bptr += 2;
    918 		len -= 2;
    919 	}
    920 }
    921 
    922 /*
    923  * gap16: 16 bytes of data followed by 16 bytes of pad.
    924  *
    925  * Buffers must be 32-byte aligned.
    926  */
    927 
    928 void
    929 lance_copytobuf_gap16(sc, fromv, boff, len)
    930 	struct lance_softc *sc;
    931 	void *fromv;
    932 	int boff;
    933 	int len;
    934 {
    935 	volatile caddr_t buf = sc->sc_mem;
    936 	caddr_t from = fromv;
    937 	caddr_t bptr;
    938 	int xfer;
    939 
    940 	bptr = buf + ((boff << 1) & ~0x1f);
    941 	boff &= 0xf;
    942 	xfer = min(len, 16 - boff);
    943 	while (len > 0) {
    944 		bcopy(from, bptr + boff, xfer);
    945 		from += xfer;
    946 		bptr += 32;
    947 		boff = 0;
    948 		len -= xfer;
    949 		xfer = min(len, 16);
    950 	}
    951 }
    952 
    953 void
    954 lance_copyfrombuf_gap16(sc, tov, boff, len)
    955 	struct lance_softc *sc;
    956 	void *tov;
    957 	int boff, len;
    958 {
    959 	volatile caddr_t buf = sc->sc_mem;
    960 	caddr_t to = tov;
    961 	caddr_t bptr;
    962 	int xfer;
    963 
    964 	bptr = buf + ((boff << 1) & ~0x1f);
    965 	boff &= 0xf;
    966 	xfer = min(len, 16 - boff);
    967 	while (len > 0) {
    968 		bcopy(bptr + boff, to, xfer);
    969 		to += xfer;
    970 		bptr += 32;
    971 		boff = 0;
    972 		len -= xfer;
    973 		xfer = min(len, 16);
    974 	}
    975 }
    976 
    977 void
    978 lance_zerobuf_gap16(sc, boff, len)
    979 	struct lance_softc *sc;
    980 	int boff, len;
    981 {
    982 	volatile caddr_t buf = sc->sc_mem;
    983 	caddr_t bptr;
    984 	int xfer;
    985 
    986 	bptr = buf + ((boff << 1) & ~0x1f);
    987 	boff &= 0xf;
    988 	xfer = min(len, 16 - boff);
    989 	while (len > 0) {
    990 		bzero(bptr + boff, xfer);
    991 		bptr += 32;
    992 		boff = 0;
    993 		len -= xfer;
    994 		xfer = min(len, 16);
    995 	}
    996 }
    997 #endif /* Example only */
    998