Home | History | Annotate | Line # | Download | only in ic
lance.c revision 1.13
      1 /*	$NetBSD: lance.c,v 1.13 2000/10/01 23:32:42 thorpej 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 	/* Attach the interface. */
    276 	if_attach(ifp);
    277 	ether_ifattach(ifp, sc->sc_enaddr);
    278 
    279 #if NBPFILTER > 0
    280 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
    281 #endif
    282 
    283 	sc->sc_sh = shutdownhook_establish(lance_shutdown, sc);
    284 	if (sc->sc_sh == NULL)
    285 		panic("lance_config: can't establish shutdownhook");
    286 	sc->sc_rbufaddr = malloc(sc->sc_nrbuf * sizeof(int), M_DEVBUF,
    287 					M_WAITOK);
    288 	sc->sc_tbufaddr = malloc(sc->sc_ntbuf * sizeof(int), M_DEVBUF,
    289 					M_WAITOK);
    290 
    291 #if NRND > 0
    292 	rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
    293 			  RND_TYPE_NET, 0);
    294 #endif
    295 }
    296 
    297 void
    298 lance_reset(sc)
    299 	struct lance_softc *sc;
    300 {
    301 	int s;
    302 
    303 	s = splnet();
    304 	lance_init(sc);
    305 	splx(s);
    306 }
    307 
    308 void
    309 lance_stop(sc)
    310 	struct lance_softc *sc;
    311 {
    312 
    313 	(*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_STOP);
    314 }
    315 
    316 /*
    317  * Initialization of interface; set up initialization block
    318  * and transmit/receive descriptor rings.
    319  */
    320 void
    321 lance_init(sc)
    322 	struct lance_softc *sc;
    323 {
    324 	int timo;
    325 	u_long a;
    326 
    327 	(*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_STOP);
    328 	DELAY(100);
    329 
    330 	/* Newer LANCE chips have a reset register */
    331 	if (sc->sc_hwreset)
    332 		(*sc->sc_hwreset)(sc);
    333 
    334 	/* Set the correct byte swapping mode, etc. */
    335 	(*sc->sc_wrcsr)(sc, LE_CSR3, sc->sc_conf3);
    336 
    337 	/* Set up LANCE init block. */
    338 	(*sc->sc_meminit)(sc);
    339 
    340 	/* Give LANCE the physical address of its init block. */
    341 	a = sc->sc_addr + LE_INITADDR(sc);
    342 	(*sc->sc_wrcsr)(sc, LE_CSR1, a);
    343 	(*sc->sc_wrcsr)(sc, LE_CSR2, a >> 16);
    344 
    345 	/* Try to initialize the LANCE. */
    346 	DELAY(100);
    347 	(*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INIT);
    348 
    349 	/* Wait for initialization to finish. */
    350 	for (timo = 100000; timo; timo--)
    351 		if ((*sc->sc_rdcsr)(sc, LE_CSR0) & LE_C0_IDON)
    352 			break;
    353 
    354 	if ((*sc->sc_rdcsr)(sc, LE_CSR0) & LE_C0_IDON) {
    355 		/* Start the LANCE. */
    356 		(*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA | LE_C0_STRT |
    357 		    LE_C0_IDON);
    358 		ifp->if_flags |= IFF_RUNNING;
    359 		ifp->if_flags &= ~IFF_OACTIVE;
    360 		ifp->if_timer = 0;
    361 		(*sc->sc_start)(ifp);
    362 	} else
    363 		printf("%s: controller failed to initialize\n",
    364 			sc->sc_dev.dv_xname);
    365 	if (sc->sc_hwinit)
    366 		(*sc->sc_hwinit)(sc);
    367 }
    368 
    369 /*
    370  * Routine to copy from mbuf chain to transmit buffer in
    371  * network buffer memory.
    372  */
    373 int
    374 lance_put(sc, boff, m)
    375 	struct lance_softc *sc;
    376 	int boff;
    377 	struct mbuf *m;
    378 {
    379 	struct mbuf *n;
    380 	int len, tlen = 0;
    381 
    382 	for (; m; m = n) {
    383 		len = m->m_len;
    384 		if (len == 0) {
    385 			MFREE(m, n);
    386 			continue;
    387 		}
    388 		(*sc->sc_copytobuf)(sc, mtod(m, caddr_t), boff, len);
    389 		boff += len;
    390 		tlen += len;
    391 		MFREE(m, n);
    392 	}
    393 	if (tlen < LEMINSIZE) {
    394 		(*sc->sc_zerobuf)(sc, boff, LEMINSIZE - tlen);
    395 		tlen = LEMINSIZE;
    396 	}
    397 	return (tlen);
    398 }
    399 
    400 /*
    401  * Pull data off an interface.
    402  * Len is length of data, with local net header stripped.
    403  * We copy the data into mbufs.  When full cluster sized units are present
    404  * we copy into clusters.
    405  */
    406 integrate struct mbuf *
    407 lance_get(sc, boff, totlen)
    408 	struct lance_softc *sc;
    409 	int boff, totlen;
    410 {
    411 	struct mbuf *m, *m0, *newm;
    412 	int len;
    413 
    414 	MGETHDR(m0, M_DONTWAIT, MT_DATA);
    415 	if (m0 == 0)
    416 		return (0);
    417 	m0->m_pkthdr.rcvif = ifp;
    418 	m0->m_pkthdr.len = totlen;
    419 	len = MHLEN;
    420 	m = m0;
    421 
    422 	while (totlen > 0) {
    423 		if (totlen >= MINCLSIZE) {
    424 			MCLGET(m, M_DONTWAIT);
    425 			if ((m->m_flags & M_EXT) == 0)
    426 				goto bad;
    427 			len = MCLBYTES;
    428 		}
    429 
    430 		if (m == m0) {
    431 			caddr_t newdata = (caddr_t)
    432 			    ALIGN(m->m_data + sizeof(struct ether_header)) -
    433 			    sizeof(struct ether_header);
    434 			len -= newdata - m->m_data;
    435 			m->m_data = newdata;
    436 		}
    437 
    438 		m->m_len = len = min(totlen, len);
    439 		(*sc->sc_copyfrombuf)(sc, mtod(m, caddr_t), boff, len);
    440 		boff += len;
    441 
    442 		totlen -= len;
    443 		if (totlen > 0) {
    444 			MGET(newm, M_DONTWAIT, MT_DATA);
    445 			if (newm == 0)
    446 				goto bad;
    447 			len = MLEN;
    448 			m = m->m_next = newm;
    449 		}
    450 	}
    451 
    452 	return (m0);
    453 
    454 bad:
    455 	m_freem(m0);
    456 	return (0);
    457 }
    458 
    459 /*
    460  * Pass a packet to the higher levels.
    461  */
    462 void
    463 lance_read(sc, boff, len)
    464 	struct lance_softc *sc;
    465 	int boff, len;
    466 {
    467 	struct mbuf *m;
    468 
    469 	if (len <= sizeof(struct ether_header) ||
    470 	    len > 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 #if NBPFILTER > 0
    489 	/*
    490 	 * Check if there's a BPF listener on this interface.
    491 	 * If so, hand off the raw packet to BPF.
    492 	 */
    493 	if (ifp->if_bpf)
    494 		bpf_mtap(ifp->if_bpf, m);
    495 #endif
    496 
    497 #ifdef LANCE_REVC_BUG
    498 	/*
    499 	 * The old LANCE (Rev. C) chips have a bug which causes
    500 	 * garbage to be inserted in front of the received packet.
    501 	 * The work-around is to ignore packets with an invalid
    502 	 * destination address (garbage will usually not match).
    503 	 * Of course, this precludes multicast support...
    504 	 */
    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 		ifp->if_flags |= IFF_UP;
    579 
    580 		switch (ifa->ifa_addr->sa_family) {
    581 #ifdef INET
    582 		case AF_INET:
    583 			lance_init(sc);
    584 			arp_ifinit(ifp, ifa);
    585 			break;
    586 #endif
    587 #ifdef NS
    588 		case AF_NS:
    589 		    {
    590 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
    591 
    592 			if (ns_nullhost(*ina))
    593 				ina->x_host =
    594 				    *(union ns_host *)LLADDR(ifp->if_sadl);
    595 			else {
    596 				bcopy(ina->x_host.c_host,
    597 				    LLADDR(ifp->if_sadl),
    598 				    sizeof(sc->sc_enaddr));
    599 			}
    600 			/* Set new address. */
    601 			lance_init(sc);
    602 			break;
    603 		    }
    604 #endif
    605 		default:
    606 			lance_init(sc);
    607 			break;
    608 		}
    609 		break;
    610 
    611 #if defined(CCITT) && defined(LLC)
    612 	case SIOCSIFCONF_X25:
    613 		ifp->if_flags |= IFF_UP;
    614 		ifa->ifa_rtrequest = cons_rtrequest; /* XXX */
    615 		error = x25_llcglue(PRC_IFUP, ifa->ifa_addr);
    616 		if (error == 0)
    617 			lance_init(sc);
    618 		break;
    619 #endif /* CCITT && LLC */
    620 
    621 	case SIOCSIFFLAGS:
    622 		if ((ifp->if_flags & IFF_UP) == 0 &&
    623 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    624 			/*
    625 			 * If interface is marked down and it is running, then
    626 			 * stop it.
    627 			 */
    628 			lance_stop(sc);
    629 			ifp->if_flags &= ~IFF_RUNNING;
    630 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
    631 		    	   (ifp->if_flags & IFF_RUNNING) == 0) {
    632 			/*
    633 			 * If interface is marked up and it is stopped, then
    634 			 * start it.
    635 			 */
    636 			lance_init(sc);
    637 		} else if ((ifp->if_flags & IFF_UP) != 0) {
    638 			/*
    639 			 * Reset the interface to pick up changes in any other
    640 			 * flags that affect hardware registers.
    641 			 */
    642 			/*lance_stop(sc);*/
    643 			lance_init(sc);
    644 		}
    645 #ifdef LEDEBUG
    646 		if (ifp->if_flags & IFF_DEBUG)
    647 			sc->sc_debug = 1;
    648 		else
    649 			sc->sc_debug = 0;
    650 #endif
    651 		break;
    652 
    653 	case SIOCADDMULTI:
    654 	case SIOCDELMULTI:
    655 		error = (cmd == SIOCADDMULTI) ?
    656 		    ether_addmulti(ifr, &sc->sc_ethercom) :
    657 		    ether_delmulti(ifr, &sc->sc_ethercom);
    658 
    659 		if (error == ENETRESET) {
    660 			/*
    661 			 * Multicast list has changed; set the hardware filter
    662 			 * accordingly.
    663 			 */
    664 			lance_reset(sc);
    665 			error = 0;
    666 		}
    667 		break;
    668 
    669 	case SIOCGIFMEDIA:
    670 	case SIOCSIFMEDIA:
    671 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
    672 		break;
    673 
    674 	default:
    675 		error = EINVAL;
    676 		break;
    677 	}
    678 
    679 	splx(s);
    680 	return (error);
    681 }
    682 
    683 hide void
    684 lance_shutdown(arg)
    685 	void *arg;
    686 {
    687 
    688 	lance_stop((struct lance_softc *)arg);
    689 }
    690 
    691 /*
    692  * Set up the logical address filter.
    693  */
    694 void
    695 lance_setladrf(ac, af)
    696 	struct ethercom *ac;
    697 	u_int16_t *af;
    698 {
    699 	struct ifnet *ifp = &ac->ec_if;
    700 	struct ether_multi *enm;
    701 	u_int32_t crc;
    702 	struct ether_multistep step;
    703 
    704 	/*
    705 	 * Set up multicast address filter by passing all multicast addresses
    706 	 * through a crc generator, and then using the high order 6 bits as an
    707 	 * index into the 64 bit logical address filter.  The high order bit
    708 	 * selects the word, while the rest of the bits select the bit within
    709 	 * the word.
    710 	 */
    711 
    712 	if (ifp->if_flags & IFF_PROMISC)
    713 		goto allmulti;
    714 
    715 	af[0] = af[1] = af[2] = af[3] = 0x0000;
    716 	ETHER_FIRST_MULTI(step, ac, enm);
    717 	while (enm != NULL) {
    718 		if (ETHER_CMP(enm->enm_addrlo, enm->enm_addrhi)) {
    719 			/*
    720 			 * We must listen to a range of multicast addresses.
    721 			 * For now, just accept all multicasts, rather than
    722 			 * trying to set only those filter bits needed to match
    723 			 * the range.  (At this time, the only use of address
    724 			 * ranges is for IP multicast routing, for which the
    725 			 * range is big enough to require all bits set.)
    726 			 */
    727 			goto allmulti;
    728 		}
    729 
    730 		crc = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
    731 
    732 		/* Just want the 6 most significant bits. */
    733 		crc >>= 26;
    734 
    735 		/* Set the corresponding bit in the filter. */
    736 		af[crc >> 4] |= 1 << (crc & 0xf);
    737 
    738 		ETHER_NEXT_MULTI(step, enm);
    739 	}
    740 	ifp->if_flags &= ~IFF_ALLMULTI;
    741 	return;
    742 
    743 allmulti:
    744 	ifp->if_flags |= IFF_ALLMULTI;
    745 	af[0] = af[1] = af[2] = af[3] = 0xffff;
    746 }
    747 
    748 /*
    749  * Routines for accessing the transmit and receive buffers.
    750  * The various CPU and adapter configurations supported by this
    751  * driver require three different access methods for buffers
    752  * and descriptors:
    753  *	(1) contig (contiguous data; no padding),
    754  *	(2) gap2 (two bytes of data followed by two bytes of padding),
    755  *	(3) gap16 (16 bytes of data followed by 16 bytes of padding).
    756  */
    757 
    758 /*
    759  * contig: contiguous data with no padding.
    760  *
    761  * Buffers may have any alignment.
    762  */
    763 
    764 void
    765 lance_copytobuf_contig(sc, from, boff, len)
    766 	struct lance_softc *sc;
    767 	void *from;
    768 	int boff, len;
    769 {
    770 	volatile caddr_t buf = sc->sc_mem;
    771 
    772 	/*
    773 	 * Just call bcopy() to do the work.
    774 	 */
    775 	bcopy(from, buf + boff, len);
    776 }
    777 
    778 void
    779 lance_copyfrombuf_contig(sc, to, boff, len)
    780 	struct lance_softc *sc;
    781 	void *to;
    782 	int boff, len;
    783 {
    784 	volatile caddr_t buf = sc->sc_mem;
    785 
    786 	/*
    787 	 * Just call bcopy() to do the work.
    788 	 */
    789 	bcopy(buf + boff, to, len);
    790 }
    791 
    792 void
    793 lance_zerobuf_contig(sc, boff, len)
    794 	struct lance_softc *sc;
    795 	int boff, len;
    796 {
    797 	volatile caddr_t buf = sc->sc_mem;
    798 
    799 	/*
    800 	 * Just let bzero() do the work
    801 	 */
    802 	bzero(buf + boff, len);
    803 }
    804 
    805 #if 0
    806 /*
    807  * Examples only; duplicate these and tweak (if necessary) in
    808  * machine-specific front-ends.
    809  */
    810 
    811 /*
    812  * gap2: two bytes of data followed by two bytes of pad.
    813  *
    814  * Buffers must be 4-byte aligned.  The code doesn't worry about
    815  * doing an extra byte.
    816  */
    817 
    818 void
    819 lance_copytobuf_gap2(sc, fromv, boff, len)
    820 	struct lance_softc *sc;
    821 	void *fromv;
    822 	int boff;
    823 	int len;
    824 {
    825 	volatile caddr_t buf = sc->sc_mem;
    826 	caddr_t from = fromv;
    827 	volatile u_int16_t *bptr;
    828 
    829 	if (boff & 0x1) {
    830 		/* handle unaligned first byte */
    831 		bptr = ((volatile u_int16_t *)buf) + (boff - 1);
    832 		*bptr = (*from++ << 8) | (*bptr & 0xff);
    833 		bptr += 2;
    834 		len--;
    835 	} else
    836 		bptr = ((volatile u_int16_t *)buf) + boff;
    837 	while (len > 1) {
    838 		*bptr = (from[1] << 8) | (from[0] & 0xff);
    839 		bptr += 2;
    840 		from += 2;
    841 		len -= 2;
    842 	}
    843 	if (len == 1)
    844 		*bptr = (u_int16_t)*from;
    845 }
    846 
    847 void
    848 lance_copyfrombuf_gap2(sc, tov, boff, len)
    849 	struct lance_softc *sc;
    850 	void *tov;
    851 	int boff, len;
    852 {
    853 	volatile caddr_t buf = sc->sc_mem;
    854 	caddr_t to = tov;
    855 	volatile u_int16_t *bptr;
    856 	u_int16_t tmp;
    857 
    858 	if (boff & 0x1) {
    859 		/* handle unaligned first byte */
    860 		bptr = ((volatile u_int16_t *)buf) + (boff - 1);
    861 		*to++ = (*bptr >> 8) & 0xff;
    862 		bptr += 2;
    863 		len--;
    864 	} else
    865 		bptr = ((volatile u_int16_t *)buf) + boff;
    866 	while (len > 1) {
    867 		tmp = *bptr;
    868 		*to++ = tmp & 0xff;
    869 		*to++ = (tmp >> 8) & 0xff;
    870 		bptr += 2;
    871 		len -= 2;
    872 	}
    873 	if (len == 1)
    874 		*to = *bptr & 0xff;
    875 }
    876 
    877 void
    878 lance_zerobuf_gap2(sc, boff, len)
    879 	struct lance_softc *sc;
    880 	int boff, len;
    881 {
    882 	volatile caddr_t buf = sc->sc_mem;
    883 	volatile u_int16_t *bptr;
    884 
    885 	if ((unsigned)boff & 0x1) {
    886 		bptr = ((volatile u_int16_t *)buf) + (boff - 1);
    887 		*bptr &= 0xff;
    888 		bptr += 2;
    889 		len--;
    890 	} else
    891 		bptr = ((volatile u_int16_t *)buf) + boff;
    892 	while (len > 0) {
    893 		*bptr = 0;
    894 		bptr += 2;
    895 		len -= 2;
    896 	}
    897 }
    898 
    899 /*
    900  * gap16: 16 bytes of data followed by 16 bytes of pad.
    901  *
    902  * Buffers must be 32-byte aligned.
    903  */
    904 
    905 void
    906 lance_copytobuf_gap16(sc, fromv, boff, len)
    907 	struct lance_softc *sc;
    908 	void *fromv;
    909 	int boff;
    910 	int len;
    911 {
    912 	volatile caddr_t buf = sc->sc_mem;
    913 	caddr_t from = fromv;
    914 	caddr_t bptr;
    915 	int xfer;
    916 
    917 	bptr = buf + ((boff << 1) & ~0x1f);
    918 	boff &= 0xf;
    919 	xfer = min(len, 16 - boff);
    920 	while (len > 0) {
    921 		bcopy(from, bptr + boff, xfer);
    922 		from += xfer;
    923 		bptr += 32;
    924 		boff = 0;
    925 		len -= xfer;
    926 		xfer = min(len, 16);
    927 	}
    928 }
    929 
    930 void
    931 lance_copyfrombuf_gap16(sc, tov, boff, len)
    932 	struct lance_softc *sc;
    933 	void *tov;
    934 	int boff, len;
    935 {
    936 	volatile caddr_t buf = sc->sc_mem;
    937 	caddr_t to = tov;
    938 	caddr_t bptr;
    939 	int xfer;
    940 
    941 	bptr = buf + ((boff << 1) & ~0x1f);
    942 	boff &= 0xf;
    943 	xfer = min(len, 16 - boff);
    944 	while (len > 0) {
    945 		bcopy(bptr + boff, to, xfer);
    946 		to += xfer;
    947 		bptr += 32;
    948 		boff = 0;
    949 		len -= xfer;
    950 		xfer = min(len, 16);
    951 	}
    952 }
    953 
    954 void
    955 lance_zerobuf_gap16(sc, boff, len)
    956 	struct lance_softc *sc;
    957 	int boff, len;
    958 {
    959 	volatile caddr_t buf = sc->sc_mem;
    960 	caddr_t bptr;
    961 	int xfer;
    962 
    963 	bptr = buf + ((boff << 1) & ~0x1f);
    964 	boff &= 0xf;
    965 	xfer = min(len, 16 - boff);
    966 	while (len > 0) {
    967 		bzero(bptr + boff, xfer);
    968 		bptr += 32;
    969 		boff = 0;
    970 		len -= xfer;
    971 		xfer = min(len, 16);
    972 	}
    973 }
    974 #endif /* Example only */
    975