Home | History | Annotate | Line # | Download | only in ic
am7990.c revision 1.44
      1 /*	$NetBSD: am7990.c,v 1.44 1998/06/07 08:08:20 fair Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * 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) 1995 Charles M. Hannum.  All rights reserved.
     42  * Copyright (c) 1992, 1993
     43  *	The Regents of the University of California.  All rights reserved.
     44  *
     45  * This code is derived from software contributed to Berkeley by
     46  * Ralph Campbell and Rick Macklem.
     47  *
     48  * Redistribution and use in source and binary forms, with or without
     49  * modification, are permitted provided that the following conditions
     50  * are met:
     51  * 1. Redistributions of source code must retain the above copyright
     52  *    notice, this list of conditions and the following disclaimer.
     53  * 2. Redistributions in binary form must reproduce the above copyright
     54  *    notice, this list of conditions and the following disclaimer in the
     55  *    documentation and/or other materials provided with the distribution.
     56  * 3. All advertising materials mentioning features or use of this software
     57  *    must display the following acknowledgement:
     58  *	This product includes software developed by the University of
     59  *	California, Berkeley and its contributors.
     60  * 4. Neither the name of the University nor the names of its contributors
     61  *    may be used to endorse or promote products derived from this software
     62  *    without specific prior written permission.
     63  *
     64  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     65  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     66  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     67  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     68  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     69  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     70  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     71  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     72  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     73  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     74  * SUCH DAMAGE.
     75  *
     76  *	@(#)if_le.c	8.2 (Berkeley) 11/16/93
     77  */
     78 
     79 #include "bpfilter.h"
     80 #include "rnd.h"
     81 
     82 #include <sys/param.h>
     83 #include <sys/systm.h>
     84 #include <sys/mbuf.h>
     85 #include <sys/syslog.h>
     86 #include <sys/socket.h>
     87 #include <sys/device.h>
     88 #include <sys/malloc.h>
     89 #include <sys/ioctl.h>
     90 #include <sys/errno.h>
     91 #if NRND > 0
     92 #include <sys/rnd.h>
     93 #endif
     94 
     95 #include <net/if.h>
     96 #include <net/if_dl.h>
     97 #include <net/if_ether.h>
     98 #include <net/if_media.h>
     99 
    100 #ifdef INET
    101 #include <netinet/in.h>
    102 #include <netinet/if_inarp.h>
    103 #include <netinet/in_systm.h>
    104 #include <netinet/in_var.h>
    105 #include <netinet/ip.h>
    106 #endif
    107 
    108 #ifdef NS
    109 #include <netns/ns.h>
    110 #include <netns/ns_if.h>
    111 #endif
    112 
    113 #if defined(CCITT) && defined(LLC)
    114 #include <sys/socketvar.h>
    115 #include <netccitt/x25.h>
    116 #include <netccitt/pk.h>
    117 #include <netccitt/pk_var.h>
    118 #include <netccitt/pk_extern.h>
    119 #endif
    120 
    121 #if NBPFILTER > 0
    122 #include <net/bpf.h>
    123 #include <net/bpfdesc.h>
    124 #endif
    125 
    126 #include <dev/ic/am7990reg.h>
    127 #include <dev/ic/am7990var.h>
    128 
    129 #ifdef LEDEBUG
    130 void am7990_recv_print __P((struct am7990_softc *, int));
    131 void am7990_xmit_print __P((struct am7990_softc *, int));
    132 #endif
    133 
    134 integrate void am7990_rint __P((struct am7990_softc *));
    135 integrate void am7990_tint __P((struct am7990_softc *));
    136 
    137 integrate int am7990_put __P((struct am7990_softc *, int, struct mbuf *));
    138 integrate struct mbuf *am7990_get __P((struct am7990_softc *, int, int));
    139 integrate void am7990_read __P((struct am7990_softc *, int, int));
    140 
    141 hide void am7990_shutdown __P((void *));
    142 
    143 int am7990_mediachange __P((struct ifnet *));
    144 void am7990_mediastatus __P((struct ifnet *, struct ifmediareq *));
    145 
    146 #define	ifp	(&sc->sc_ethercom.ec_if)
    147 
    148 static inline u_int16_t ether_cmp __P((void *, void *));
    149 
    150 /*
    151  * Compare two Ether/802 addresses for equality, inlined and
    152  * unrolled for speed.  Use this like bcmp().
    153  *
    154  * XXX: Add <machine/inlines.h> for stuff like this?
    155  * XXX: or maybe add it to libkern.h instead?
    156  *
    157  * "I'd love to have an inline assembler version of this."
    158  * XXX: Who wanted that? mycroft?  I wrote one, but this
    159  * version in C is as good as hand-coded assembly. -gwr
    160  *
    161  * Please do NOT tweak this without looking at the actual
    162  * assembly code generated before and after your tweaks!
    163  */
    164 static inline u_int16_t
    165 ether_cmp(one, two)
    166 	void *one, *two;
    167 {
    168 	register u_int16_t *a = (u_short *) one;
    169 	register u_int16_t *b = (u_short *) two;
    170 	register u_int16_t diff;
    171 
    172 #ifdef	m68k
    173 	/*
    174 	 * The post-increment-pointer form produces the best
    175 	 * machine code for m68k.  This was carefully tuned
    176 	 * so it compiles to just 8 short (2-byte) op-codes!
    177 	 */
    178 	diff  = *a++ - *b++;
    179 	diff |= *a++ - *b++;
    180 	diff |= *a++ - *b++;
    181 #else
    182 	/*
    183 	 * Most modern CPUs do better with a single expresion.
    184 	 * Note that short-cut evaluation is NOT helpful here,
    185 	 * because it just makes the code longer, not faster!
    186 	 */
    187 	diff = (a[0] - b[0]) | (a[1] - b[1]) | (a[2] - b[2]);
    188 #endif
    189 
    190 	return (diff);
    191 }
    192 
    193 #define ETHER_CMP	ether_cmp
    194 
    195 #ifdef LANCE_REVC_BUG
    196 /* Make sure this is short-aligned, for ether_cmp(). */
    197 static u_int16_t bcast_enaddr[3] = { ~0, ~0, ~0 };
    198 #endif
    199 
    200 void
    201 am7990_config(sc)
    202 	struct am7990_softc *sc;
    203 {
    204 	int mem, i;
    205 
    206 	/* Make sure the chip is stopped. */
    207 	am7990_stop(sc);
    208 
    209 	/* Initialize ifnet structure. */
    210 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    211 	ifp->if_softc = sc;
    212 	ifp->if_start = am7990_start;
    213 	ifp->if_ioctl = am7990_ioctl;
    214 	ifp->if_watchdog = am7990_watchdog;
    215 	ifp->if_flags =
    216 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    217 #ifdef LANCE_REVC_BUG
    218 	ifp->if_flags &= ~IFF_MULTICAST;
    219 #endif
    220 
    221 	/* Initialize ifmedia structures. */
    222 	ifmedia_init(&sc->sc_media, 0, am7990_mediachange, am7990_mediastatus);
    223 	if (sc->sc_supmedia != NULL) {
    224 		for (i = 0; i < sc->sc_nsupmedia; i++)
    225 			ifmedia_add(&sc->sc_media, sc->sc_supmedia[i],
    226 			   0, NULL);
    227 		ifmedia_set(&sc->sc_media, sc->sc_defaultmedia);
    228 	} else {
    229 		ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
    230 		ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
    231 	}
    232 
    233 	/* Attach the interface. */
    234 	if_attach(ifp);
    235 	ether_ifattach(ifp, sc->sc_enaddr);
    236 
    237 #if NBPFILTER > 0
    238 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
    239 #endif
    240 
    241 	switch (sc->sc_memsize) {
    242 	case 8192:
    243 		sc->sc_nrbuf = 4;
    244 		sc->sc_ntbuf = 1;
    245 		break;
    246 	case 16384:
    247 		sc->sc_nrbuf = 8;
    248 		sc->sc_ntbuf = 2;
    249 		break;
    250 	case 32768:
    251 		sc->sc_nrbuf = 16;
    252 		sc->sc_ntbuf = 4;
    253 		break;
    254 	case 65536:
    255 		sc->sc_nrbuf = 32;
    256 		sc->sc_ntbuf = 8;
    257 		break;
    258 	case 131072:
    259 		sc->sc_nrbuf = 64;
    260 		sc->sc_ntbuf = 16;
    261 		break;
    262 	default:
    263 		panic("am7990_config: weird memory size");
    264 	}
    265 
    266 	printf(": address %s\n", ether_sprintf(sc->sc_enaddr));
    267 	printf("%s: %d receive buffers, %d transmit buffers\n",
    268 	    sc->sc_dev.dv_xname, sc->sc_nrbuf, sc->sc_ntbuf);
    269 
    270 	sc->sc_sh = shutdownhook_establish(am7990_shutdown, sc);
    271 	if (sc->sc_sh == NULL)
    272 		panic("am7990_config: can't establish shutdownhook");
    273 	sc->sc_rbufaddr = malloc(sc->sc_nrbuf * sizeof(int), M_DEVBUF,
    274 					M_WAITOK);
    275 	sc->sc_tbufaddr = malloc(sc->sc_ntbuf * sizeof(int), M_DEVBUF,
    276 					M_WAITOK);
    277 
    278 	mem = 0;
    279 	sc->sc_initaddr = mem;
    280 	mem += sizeof(struct leinit);
    281 	sc->sc_rmdaddr = mem;
    282 	mem += sizeof(struct lermd) * sc->sc_nrbuf;
    283 	sc->sc_tmdaddr = mem;
    284 	mem += sizeof(struct letmd) * sc->sc_ntbuf;
    285 	for (i = 0; i < sc->sc_nrbuf; i++, mem += LEBLEN)
    286 		sc->sc_rbufaddr[i] = mem;
    287 	for (i = 0; i < sc->sc_ntbuf; i++, mem += LEBLEN)
    288 		sc->sc_tbufaddr[i] = mem;
    289 #ifdef notyet
    290 	if (mem > ...)
    291 		panic(...);
    292 #endif
    293 
    294 #if NRND > 0
    295 	rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
    296 			  RND_TYPE_NET);
    297 #endif
    298 }
    299 
    300 void
    301 am7990_reset(sc)
    302 	struct am7990_softc *sc;
    303 {
    304 	int s;
    305 
    306 	s = splimp();
    307 	am7990_init(sc);
    308 	splx(s);
    309 }
    310 
    311 /*
    312  * Set up the initialization block and the descriptor rings.
    313  */
    314 void
    315 am7990_meminit(sc)
    316 	register struct am7990_softc *sc;
    317 {
    318 	u_long a;
    319 	int bix;
    320 	struct leinit init;
    321 	struct lermd rmd;
    322 	struct letmd tmd;
    323 	u_int8_t *myaddr;
    324 
    325 #if NBPFILTER > 0
    326 	if (ifp->if_flags & IFF_PROMISC)
    327 		init.init_mode = LE_MODE_NORMAL | LE_MODE_PROM;
    328 	else
    329 #endif
    330 		init.init_mode = LE_MODE_NORMAL;
    331 	if (sc->sc_initmodemedia == 1)
    332 		init.init_mode |= LE_MODE_PSEL0;
    333 
    334 	/*
    335 	 * Update our private copy of the Ethernet address.
    336 	 * We NEED the copy so we can ensure its alignment!
    337 	 */
    338 	bcopy(LLADDR(ifp->if_sadl), sc->sc_enaddr, 6);
    339 	myaddr = sc->sc_enaddr;
    340 
    341 	init.init_padr[0] = (myaddr[1] << 8) | myaddr[0];
    342 	init.init_padr[1] = (myaddr[3] << 8) | myaddr[2];
    343 	init.init_padr[2] = (myaddr[5] << 8) | myaddr[4];
    344 	am7990_setladrf(&sc->sc_ethercom, init.init_ladrf);
    345 
    346 	sc->sc_last_rd = 0;
    347 	sc->sc_first_td = sc->sc_last_td = sc->sc_no_td = 0;
    348 
    349 	a = sc->sc_addr + LE_RMDADDR(sc, 0);
    350 	init.init_rdra = a;
    351 	init.init_rlen = (a >> 16) | ((ffs(sc->sc_nrbuf) - 1) << 13);
    352 
    353 	a = sc->sc_addr + LE_TMDADDR(sc, 0);
    354 	init.init_tdra = a;
    355 	init.init_tlen = (a >> 16) | ((ffs(sc->sc_ntbuf) - 1) << 13);
    356 
    357 	(*sc->sc_copytodesc)(sc, &init, LE_INITADDR(sc), sizeof(init));
    358 
    359 	/*
    360 	 * Set up receive ring descriptors.
    361 	 */
    362 	for (bix = 0; bix < sc->sc_nrbuf; bix++) {
    363 		a = sc->sc_addr + LE_RBUFADDR(sc, bix);
    364 		rmd.rmd0 = a;
    365 		rmd.rmd1_hadr = a >> 16;
    366 		rmd.rmd1_bits = LE_R1_OWN;
    367 		rmd.rmd2 = -LEBLEN | LE_XMD2_ONES;
    368 		rmd.rmd3 = 0;
    369 		(*sc->sc_copytodesc)(sc, &rmd, LE_RMDADDR(sc, bix),
    370 		    sizeof(rmd));
    371 	}
    372 
    373 	/*
    374 	 * Set up transmit ring descriptors.
    375 	 */
    376 	for (bix = 0; bix < sc->sc_ntbuf; bix++) {
    377 		a = sc->sc_addr + LE_TBUFADDR(sc, bix);
    378 		tmd.tmd0 = a;
    379 		tmd.tmd1_hadr = a >> 16;
    380 		tmd.tmd1_bits = 0;
    381 		tmd.tmd2 = 0 | LE_XMD2_ONES;
    382 		tmd.tmd3 = 0;
    383 		(*sc->sc_copytodesc)(sc, &tmd, LE_TMDADDR(sc, bix),
    384 		    sizeof(tmd));
    385 	}
    386 }
    387 
    388 void
    389 am7990_stop(sc)
    390 	struct am7990_softc *sc;
    391 {
    392 
    393 	(*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_STOP);
    394 }
    395 
    396 /*
    397  * Initialization of interface; set up initialization block
    398  * and transmit/receive descriptor rings.
    399  */
    400 void
    401 am7990_init(sc)
    402 	register struct am7990_softc *sc;
    403 {
    404 	register int timo;
    405 	u_long a;
    406 
    407 	(*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_STOP);
    408 	DELAY(100);
    409 
    410 	/* Newer LANCE chips have a reset register */
    411 	if (sc->sc_hwreset)
    412 		(*sc->sc_hwreset)(sc);
    413 
    414 	/* Set the correct byte swapping mode, etc. */
    415 	(*sc->sc_wrcsr)(sc, LE_CSR3, sc->sc_conf3);
    416 
    417 	/* Set up LANCE init block. */
    418 	am7990_meminit(sc);
    419 
    420 	/* Give LANCE the physical address of its init block. */
    421 	a = sc->sc_addr + LE_INITADDR(sc);
    422 	(*sc->sc_wrcsr)(sc, LE_CSR1, a);
    423 	(*sc->sc_wrcsr)(sc, LE_CSR2, a >> 16);
    424 
    425 	/* Try to initialize the LANCE. */
    426 	DELAY(100);
    427 	(*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INIT);
    428 
    429 	/* Wait for initialization to finish. */
    430 	for (timo = 100000; timo; timo--)
    431 		if ((*sc->sc_rdcsr)(sc, LE_CSR0) & LE_C0_IDON)
    432 			break;
    433 
    434 	if ((*sc->sc_rdcsr)(sc, LE_CSR0) & LE_C0_IDON) {
    435 		/* Start the LANCE. */
    436 		(*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA | LE_C0_STRT |
    437 		    LE_C0_IDON);
    438 		ifp->if_flags |= IFF_RUNNING;
    439 		ifp->if_flags &= ~IFF_OACTIVE;
    440 		ifp->if_timer = 0;
    441 		am7990_start(ifp);
    442 	} else
    443 		printf("%s: controller failed to initialize\n",
    444 			sc->sc_dev.dv_xname);
    445 	if (sc->sc_hwinit)
    446 		(*sc->sc_hwinit)(sc);
    447 }
    448 
    449 /*
    450  * Routine to copy from mbuf chain to transmit buffer in
    451  * network buffer memory.
    452  */
    453 integrate int
    454 am7990_put(sc, boff, m)
    455 	struct am7990_softc *sc;
    456 	int boff;
    457 	register struct mbuf *m;
    458 {
    459 	register struct mbuf *n;
    460 	register int len, tlen = 0;
    461 
    462 	for (; m; m = n) {
    463 		len = m->m_len;
    464 		if (len == 0) {
    465 			MFREE(m, n);
    466 			continue;
    467 		}
    468 		(*sc->sc_copytobuf)(sc, mtod(m, caddr_t), boff, len);
    469 		boff += len;
    470 		tlen += len;
    471 		MFREE(m, n);
    472 	}
    473 	if (tlen < LEMINSIZE) {
    474 		(*sc->sc_zerobuf)(sc, boff, LEMINSIZE - tlen);
    475 		tlen = LEMINSIZE;
    476 	}
    477 	return (tlen);
    478 }
    479 
    480 /*
    481  * Pull data off an interface.
    482  * Len is length of data, with local net header stripped.
    483  * We copy the data into mbufs.  When full cluster sized units are present
    484  * we copy into clusters.
    485  */
    486 integrate struct mbuf *
    487 am7990_get(sc, boff, totlen)
    488 	struct am7990_softc *sc;
    489 	int boff, totlen;
    490 {
    491 	register struct mbuf *m;
    492 	struct mbuf *top, **mp;
    493 	int len;
    494 
    495 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    496 	if (m == 0)
    497 		return (0);
    498 	m->m_pkthdr.rcvif = ifp;
    499 	m->m_pkthdr.len = totlen;
    500 	len = MHLEN;
    501 	top = 0;
    502 	mp = &top;
    503 
    504 	while (totlen > 0) {
    505 		if (top) {
    506 			MGET(m, M_DONTWAIT, MT_DATA);
    507 			if (m == 0) {
    508 				m_freem(top);
    509 				return 0;
    510 			}
    511 			len = MLEN;
    512 		}
    513 		if (totlen >= MINCLSIZE) {
    514 			MCLGET(m, M_DONTWAIT);
    515 			if ((m->m_flags & M_EXT) == 0) {
    516 				m_free(m);
    517 				m_freem(top);
    518 				return 0;
    519 			}
    520 			len = MCLBYTES;
    521 		}
    522 		if (!top) {
    523 			register int pad =
    524 			    ALIGN(sizeof(struct ether_header)) -
    525 			        sizeof(struct ether_header);
    526 			m->m_data += pad;
    527 			len -= pad;
    528 		}
    529 		m->m_len = len = min(totlen, len);
    530 		(*sc->sc_copyfrombuf)(sc, mtod(m, caddr_t), boff, len);
    531 		boff += len;
    532 		totlen -= len;
    533 		*mp = m;
    534 		mp = &m->m_next;
    535 	}
    536 
    537 	return (top);
    538 }
    539 
    540 /*
    541  * Pass a packet to the higher levels.
    542  */
    543 integrate void
    544 am7990_read(sc, boff, len)
    545 	register struct am7990_softc *sc;
    546 	int boff, len;
    547 {
    548 	struct mbuf *m;
    549 	struct ether_header *eh;
    550 
    551 	if (len <= sizeof(struct ether_header) ||
    552 	    len > ETHERMTU + sizeof(struct ether_header)) {
    553 #ifdef LEDEBUG
    554 		printf("%s: invalid packet size %d; dropping\n",
    555 		    sc->sc_dev.dv_xname, len);
    556 #endif
    557 		ifp->if_ierrors++;
    558 		return;
    559 	}
    560 
    561 	/* Pull packet off interface. */
    562 	m = am7990_get(sc, boff, len);
    563 	if (m == 0) {
    564 		ifp->if_ierrors++;
    565 		return;
    566 	}
    567 
    568 	ifp->if_ipackets++;
    569 
    570 	/* We assume that the header fit entirely in one mbuf. */
    571 	eh = mtod(m, struct ether_header *);
    572 
    573 #if NBPFILTER > 0
    574 	/*
    575 	 * Check if there's a BPF listener on this interface.
    576 	 * If so, hand off the raw packet to BPF.
    577 	 */
    578 	if (ifp->if_bpf) {
    579 		bpf_mtap(ifp->if_bpf, m);
    580 
    581 #ifndef LANCE_REVC_BUG
    582 		/*
    583 		 * Note that the interface cannot be in promiscuous mode if
    584 		 * there are no BPF listeners.  And if we are in promiscuous
    585 		 * mode, we have to check if this packet is really ours.
    586 		 */
    587 		if ((ifp->if_flags & IFF_PROMISC) != 0 &&
    588 		    (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
    589 		    ETHER_CMP(eh->ether_dhost, sc->sc_enaddr)) {
    590 			m_freem(m);
    591 			return;
    592 		}
    593 #endif
    594 	}
    595 #endif
    596 
    597 #ifdef LANCE_REVC_BUG
    598 	/*
    599 	 * The old LANCE (Rev. C) chips have a bug which causes
    600 	 * garbage to be inserted in front of the received packet.
    601 	 * The work-around is to ignore packets with an invalid
    602 	 * destination address (garbage will usually not match).
    603 	 * Of course, this precludes multicast support...
    604 	 */
    605 	if (ETHER_CMP(eh->ether_dhost, sc->sc_enaddr) &&
    606 	    ETHER_CMP(eh->ether_dhost, bcast_enaddr)) {
    607 		m_freem(m);
    608 		return;
    609 	}
    610 #endif
    611 
    612 	/* Pass the packet up, with the ether header sort-of removed. */
    613 	m_adj(m, sizeof(struct ether_header));
    614 	ether_input(ifp, eh, m);
    615 }
    616 
    617 integrate void
    618 am7990_rint(sc)
    619 	struct am7990_softc *sc;
    620 {
    621 	register int bix;
    622 	int rp;
    623 	struct lermd rmd;
    624 
    625 	bix = sc->sc_last_rd;
    626 
    627 	/* Process all buffers with valid data. */
    628 	for (;;) {
    629 		rp = LE_RMDADDR(sc, bix);
    630 		(*sc->sc_copyfromdesc)(sc, &rmd, rp, sizeof(rmd));
    631 
    632 		if (rmd.rmd1_bits & LE_R1_OWN)
    633 			break;
    634 
    635 		if (rmd.rmd1_bits & LE_R1_ERR) {
    636 			if (rmd.rmd1_bits & LE_R1_ENP) {
    637 #ifdef LEDEBUG
    638 				if ((rmd.rmd1_bits & LE_R1_OFLO) == 0) {
    639 					if (rmd.rmd1_bits & LE_R1_FRAM)
    640 						printf("%s: framing error\n",
    641 						    sc->sc_dev.dv_xname);
    642 					if (rmd.rmd1_bits & LE_R1_CRC)
    643 						printf("%s: crc mismatch\n",
    644 						    sc->sc_dev.dv_xname);
    645 				}
    646 #endif
    647 			} else {
    648 				if (rmd.rmd1_bits & LE_R1_OFLO)
    649 					printf("%s: overflow\n",
    650 					    sc->sc_dev.dv_xname);
    651 			}
    652 			if (rmd.rmd1_bits & LE_R1_BUFF)
    653 				printf("%s: receive buffer error\n",
    654 				    sc->sc_dev.dv_xname);
    655 			ifp->if_ierrors++;
    656 		} else if ((rmd.rmd1_bits & (LE_R1_STP | LE_R1_ENP)) !=
    657 		    (LE_R1_STP | LE_R1_ENP)) {
    658 			printf("%s: dropping chained buffer\n",
    659 			    sc->sc_dev.dv_xname);
    660 			ifp->if_ierrors++;
    661 		} else {
    662 #ifdef LEDEBUG
    663 			if (sc->sc_debug)
    664 				am7990_recv_print(sc, sc->sc_last_rd);
    665 #endif
    666 			am7990_read(sc, LE_RBUFADDR(sc, bix),
    667 			    (int)rmd.rmd3 - 4);
    668 		}
    669 
    670 		rmd.rmd1_bits = LE_R1_OWN;
    671 		rmd.rmd2 = -LEBLEN | LE_XMD2_ONES;
    672 		rmd.rmd3 = 0;
    673 		(*sc->sc_copytodesc)(sc, &rmd, rp, sizeof(rmd));
    674 
    675 #ifdef LEDEBUG
    676 		if (sc->sc_debug)
    677 			printf("sc->sc_last_rd = %x, rmd: "
    678 			       "ladr %04x, hadr %02x, flags %02x, "
    679 			       "bcnt %04x, mcnt %04x\n",
    680 				sc->sc_last_rd,
    681 				rmd.rmd0, rmd.rmd1_hadr, rmd.rmd1_bits,
    682 				rmd.rmd2, rmd.rmd3);
    683 #endif
    684 
    685 		if (++bix == sc->sc_nrbuf)
    686 			bix = 0;
    687 	}
    688 
    689 	sc->sc_last_rd = bix;
    690 }
    691 
    692 integrate void
    693 am7990_tint(sc)
    694 	register struct am7990_softc *sc;
    695 {
    696 	register int bix;
    697 	struct letmd tmd;
    698 
    699 	bix = sc->sc_first_td;
    700 
    701 	for (;;) {
    702 		if (sc->sc_no_td <= 0)
    703 			break;
    704 
    705 #ifdef LEDEBUG
    706 		if (sc->sc_debug)
    707 			printf("trans tmd: "
    708 			    "ladr %04x, hadr %02x, flags %02x, "
    709 			    "bcnt %04x, mcnt %04x\n",
    710 			    tmd.tmd0, tmd.tmd1_hadr, tmd.tmd1_bits,
    711 			    tmd.tmd2, tmd.tmd3);
    712 #endif
    713 
    714 		(*sc->sc_copyfromdesc)(sc, &tmd, LE_TMDADDR(sc, bix),
    715 		    sizeof(tmd));
    716 
    717 		if (tmd.tmd1_bits & LE_T1_OWN)
    718 			break;
    719 
    720 		ifp->if_flags &= ~IFF_OACTIVE;
    721 
    722 		if (tmd.tmd1_bits & LE_T1_ERR) {
    723 			if (tmd.tmd3 & LE_T3_BUFF)
    724 				printf("%s: transmit buffer error\n",
    725 				    sc->sc_dev.dv_xname);
    726 			else if (tmd.tmd3 & LE_T3_UFLO)
    727 				printf("%s: underflow\n", sc->sc_dev.dv_xname);
    728 			if (tmd.tmd3 & (LE_T3_BUFF | LE_T3_UFLO)) {
    729 				am7990_reset(sc);
    730 				return;
    731 			}
    732 			if (tmd.tmd3 & LE_T3_LCAR) {
    733 				sc->sc_havecarrier = 0;
    734 				if (sc->sc_nocarrier)
    735 					(*sc->sc_nocarrier)(sc);
    736 				else
    737 					printf("%s: lost carrier\n",
    738 					    sc->sc_dev.dv_xname);
    739 			}
    740 			if (tmd.tmd3 & LE_T3_LCOL)
    741 				ifp->if_collisions++;
    742 			if (tmd.tmd3 & LE_T3_RTRY) {
    743 				printf("%s: excessive collisions, tdr %d\n",
    744 				    sc->sc_dev.dv_xname,
    745 				    tmd.tmd3 & LE_T3_TDR_MASK);
    746 				ifp->if_collisions += 16;
    747 			}
    748 			ifp->if_oerrors++;
    749 		} else {
    750 			if (tmd.tmd1_bits & LE_T1_ONE)
    751 				ifp->if_collisions++;
    752 			else if (tmd.tmd1_bits & LE_T1_MORE)
    753 				/* Real number is unknown. */
    754 				ifp->if_collisions += 2;
    755 			ifp->if_opackets++;
    756 		}
    757 
    758 		if (++bix == sc->sc_ntbuf)
    759 			bix = 0;
    760 
    761 		--sc->sc_no_td;
    762 	}
    763 
    764 	sc->sc_first_td = bix;
    765 
    766 	am7990_start(ifp);
    767 
    768 	if (sc->sc_no_td == 0)
    769 		ifp->if_timer = 0;
    770 }
    771 
    772 /*
    773  * Controller interrupt.
    774  */
    775 int
    776 am7990_intr(arg)
    777 	register void *arg;
    778 {
    779 	register struct am7990_softc *sc = arg;
    780 	register u_int16_t isr;
    781 
    782 	isr = (*sc->sc_rdcsr)(sc, LE_CSR0) | sc->sc_saved_csr0;
    783 	sc->sc_saved_csr0 = 0;
    784 #ifdef LEDEBUG
    785 	if (sc->sc_debug)
    786 		printf("%s: am7990_intr entering with isr=%04x\n",
    787 		    sc->sc_dev.dv_xname, isr);
    788 #endif
    789 	if ((isr & LE_C0_INTR) == 0)
    790 		return (0);
    791 
    792 	(*sc->sc_wrcsr)(sc, LE_CSR0,
    793 	    isr & (LE_C0_INEA | LE_C0_BABL | LE_C0_MISS | LE_C0_MERR |
    794 		   LE_C0_RINT | LE_C0_TINT | LE_C0_IDON));
    795 	if (isr & LE_C0_ERR) {
    796 		if (isr & LE_C0_BABL) {
    797 #ifdef LEDEBUG
    798 			printf("%s: babble\n", sc->sc_dev.dv_xname);
    799 #endif
    800 			ifp->if_oerrors++;
    801 		}
    802 #if 0
    803 		if (isr & LE_C0_CERR) {
    804 			printf("%s: collision error\n", sc->sc_dev.dv_xname);
    805 			ifp->if_collisions++;
    806 		}
    807 #endif
    808 		if (isr & LE_C0_MISS) {
    809 #ifdef LEDEBUG
    810 			printf("%s: missed packet\n", sc->sc_dev.dv_xname);
    811 #endif
    812 			ifp->if_ierrors++;
    813 		}
    814 		if (isr & LE_C0_MERR) {
    815 			printf("%s: memory error\n", sc->sc_dev.dv_xname);
    816 			am7990_reset(sc);
    817 			return (1);
    818 		}
    819 	}
    820 
    821 	if ((isr & LE_C0_RXON) == 0) {
    822 		printf("%s: receiver disabled\n", sc->sc_dev.dv_xname);
    823 		ifp->if_ierrors++;
    824 		am7990_reset(sc);
    825 		return (1);
    826 	}
    827 	if ((isr & LE_C0_TXON) == 0) {
    828 		printf("%s: transmitter disabled\n", sc->sc_dev.dv_xname);
    829 		ifp->if_oerrors++;
    830 		am7990_reset(sc);
    831 		return (1);
    832 	}
    833 
    834 	/*
    835 	 * Pretend we have carrier; if we don't this will be cleared
    836 	 * shortly.
    837 	 */
    838 	sc->sc_havecarrier = 1;
    839 
    840 	if (isr & LE_C0_RINT)
    841 		am7990_rint(sc);
    842 	if (isr & LE_C0_TINT)
    843 		am7990_tint(sc);
    844 
    845 #if NRND > 0
    846 	rnd_add_uint32(&sc->rnd_source, isr);
    847 #endif
    848 
    849 	return (1);
    850 }
    851 
    852 #undef	ifp
    853 
    854 void
    855 am7990_watchdog(ifp)
    856 	struct ifnet *ifp;
    857 {
    858 	struct am7990_softc *sc = ifp->if_softc;
    859 
    860 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
    861 	++ifp->if_oerrors;
    862 
    863 	am7990_reset(sc);
    864 }
    865 
    866 int
    867 am7990_mediachange(ifp)
    868 	struct ifnet *ifp;
    869 {
    870 	struct am7990_softc *sc = ifp->if_softc;
    871 
    872 	if (sc->sc_mediachange)
    873 		return ((*sc->sc_mediachange)(sc));
    874 	return (EINVAL);
    875 }
    876 
    877 void
    878 am7990_mediastatus(ifp, ifmr)
    879 	struct ifnet *ifp;
    880 	struct ifmediareq *ifmr;
    881 {
    882 	struct am7990_softc *sc = ifp->if_softc;
    883 
    884 	if ((ifp->if_flags & IFF_UP) == 0)
    885 		return;
    886 
    887 	ifmr->ifm_status = IFM_AVALID;
    888 	if (sc->sc_havecarrier)
    889 		ifmr->ifm_status |= IFM_ACTIVE;
    890 
    891 	if (sc->sc_mediastatus)
    892 		(*sc->sc_mediastatus)(sc, ifmr);
    893 }
    894 
    895 /*
    896  * Setup output on interface.
    897  * Get another datagram to send off of the interface queue, and map it to the
    898  * interface before starting the output.
    899  * Called only at splimp or interrupt level.
    900  */
    901 void
    902 am7990_start(ifp)
    903 	register struct ifnet *ifp;
    904 {
    905 	register struct am7990_softc *sc = ifp->if_softc;
    906 	register int bix;
    907 	register struct mbuf *m;
    908 	struct letmd tmd;
    909 	int rp;
    910 	int len;
    911 
    912 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
    913 		return;
    914 
    915 	bix = sc->sc_last_td;
    916 
    917 	for (;;) {
    918 		rp = LE_TMDADDR(sc, bix);
    919 		(*sc->sc_copyfromdesc)(sc, &tmd, rp, sizeof(tmd));
    920 
    921 		if (tmd.tmd1_bits & LE_T1_OWN) {
    922 			ifp->if_flags |= IFF_OACTIVE;
    923 			printf("missing buffer, no_td = %d, last_td = %d\n",
    924 			    sc->sc_no_td, sc->sc_last_td);
    925 		}
    926 
    927 		IF_DEQUEUE(&ifp->if_snd, m);
    928 		if (m == 0)
    929 			break;
    930 
    931 #if NBPFILTER > 0
    932 		/*
    933 		 * If BPF is listening on this interface, let it see the packet
    934 		 * before we commit it to the wire.
    935 		 */
    936 		if (ifp->if_bpf)
    937 			bpf_mtap(ifp->if_bpf, m);
    938 #endif
    939 
    940 		/*
    941 		 * Copy the mbuf chain into the transmit buffer.
    942 		 */
    943 		len = am7990_put(sc, LE_TBUFADDR(sc, bix), m);
    944 
    945 #ifdef LEDEBUG
    946 		if (len > ETHERMTU + sizeof(struct ether_header))
    947 			printf("packet length %d\n", len);
    948 #endif
    949 
    950 		ifp->if_timer = 5;
    951 
    952 		/*
    953 		 * Init transmit registers, and set transmit start flag.
    954 		 */
    955 		tmd.tmd1_bits = LE_T1_OWN | LE_T1_STP | LE_T1_ENP;
    956 		tmd.tmd2 = -len | LE_XMD2_ONES;
    957 		tmd.tmd3 = 0;
    958 
    959 		(*sc->sc_copytodesc)(sc, &tmd, rp, sizeof(tmd));
    960 
    961 #ifdef LEDEBUG
    962 		if (sc->sc_debug)
    963 			am7990_xmit_print(sc, sc->sc_last_td);
    964 #endif
    965 
    966 		(*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA | LE_C0_TDMD);
    967 
    968 		if (++bix == sc->sc_ntbuf)
    969 			bix = 0;
    970 
    971 		if (++sc->sc_no_td == sc->sc_ntbuf) {
    972 			ifp->if_flags |= IFF_OACTIVE;
    973 			break;
    974 		}
    975 
    976 	}
    977 
    978 	sc->sc_last_td = bix;
    979 }
    980 
    981 /*
    982  * Process an ioctl request.
    983  */
    984 int
    985 am7990_ioctl(ifp, cmd, data)
    986 	register struct ifnet *ifp;
    987 	u_long cmd;
    988 	caddr_t data;
    989 {
    990 	register struct am7990_softc *sc = ifp->if_softc;
    991 	struct ifaddr *ifa = (struct ifaddr *)data;
    992 	struct ifreq *ifr = (struct ifreq *)data;
    993 	int s, error = 0;
    994 
    995 	s = splimp();
    996 
    997 	switch (cmd) {
    998 
    999 	case SIOCSIFADDR:
   1000 		ifp->if_flags |= IFF_UP;
   1001 
   1002 		switch (ifa->ifa_addr->sa_family) {
   1003 #ifdef INET
   1004 		case AF_INET:
   1005 			am7990_init(sc);
   1006 			arp_ifinit(ifp, ifa);
   1007 			break;
   1008 #endif
   1009 #ifdef NS
   1010 		case AF_NS:
   1011 		    {
   1012 			register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
   1013 
   1014 			if (ns_nullhost(*ina))
   1015 				ina->x_host =
   1016 				    *(union ns_host *)LLADDR(ifp->if_sadl);
   1017 			else {
   1018 				bcopy(ina->x_host.c_host,
   1019 				    LLADDR(ifp->if_sadl),
   1020 				    sizeof(sc->sc_enaddr));
   1021 			}
   1022 			/* Set new address. */
   1023 			am7990_init(sc);
   1024 			break;
   1025 		    }
   1026 #endif
   1027 		default:
   1028 			am7990_init(sc);
   1029 			break;
   1030 		}
   1031 		break;
   1032 
   1033 #if defined(CCITT) && defined(LLC)
   1034 	case SIOCSIFCONF_X25:
   1035 		ifp->if_flags |= IFF_UP;
   1036 		ifa->ifa_rtrequest = cons_rtrequest; /* XXX */
   1037 		error = x25_llcglue(PRC_IFUP, ifa->ifa_addr);
   1038 		if (error == 0)
   1039 			am7990_init(sc);
   1040 		break;
   1041 #endif /* CCITT && LLC */
   1042 
   1043 	case SIOCSIFFLAGS:
   1044 		if ((ifp->if_flags & IFF_UP) == 0 &&
   1045 		    (ifp->if_flags & IFF_RUNNING) != 0) {
   1046 			/*
   1047 			 * If interface is marked down and it is running, then
   1048 			 * stop it.
   1049 			 */
   1050 			am7990_stop(sc);
   1051 			ifp->if_flags &= ~IFF_RUNNING;
   1052 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
   1053 		    	   (ifp->if_flags & IFF_RUNNING) == 0) {
   1054 			/*
   1055 			 * If interface is marked up and it is stopped, then
   1056 			 * start it.
   1057 			 */
   1058 			am7990_init(sc);
   1059 		} else {
   1060 			/*
   1061 			 * Reset the interface to pick up changes in any other
   1062 			 * flags that affect hardware registers.
   1063 			 */
   1064 			/*am7990_stop(sc);*/
   1065 			am7990_init(sc);
   1066 		}
   1067 #ifdef LEDEBUG
   1068 		if (ifp->if_flags & IFF_DEBUG)
   1069 			sc->sc_debug = 1;
   1070 		else
   1071 			sc->sc_debug = 0;
   1072 #endif
   1073 		break;
   1074 
   1075 	case SIOCADDMULTI:
   1076 	case SIOCDELMULTI:
   1077 		error = (cmd == SIOCADDMULTI) ?
   1078 		    ether_addmulti(ifr, &sc->sc_ethercom) :
   1079 		    ether_delmulti(ifr, &sc->sc_ethercom);
   1080 
   1081 		if (error == ENETRESET) {
   1082 			/*
   1083 			 * Multicast list has changed; set the hardware filter
   1084 			 * accordingly.
   1085 			 */
   1086 			am7990_reset(sc);
   1087 			error = 0;
   1088 		}
   1089 		break;
   1090 
   1091 	case SIOCGIFMEDIA:
   1092 	case SIOCSIFMEDIA:
   1093 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
   1094 		break;
   1095 
   1096 	default:
   1097 		error = EINVAL;
   1098 		break;
   1099 	}
   1100 
   1101 	splx(s);
   1102 	return (error);
   1103 }
   1104 
   1105 hide void
   1106 am7990_shutdown(arg)
   1107 	void *arg;
   1108 {
   1109 
   1110 	am7990_stop((struct am7990_softc *)arg);
   1111 }
   1112 
   1113 #ifdef LEDEBUG
   1114 void
   1115 am7990_recv_print(sc, no)
   1116 	struct am7990_softc *sc;
   1117 	int no;
   1118 {
   1119 	struct lermd rmd;
   1120 	u_int16_t len;
   1121 	struct ether_header eh;
   1122 
   1123 	(*sc->sc_copyfromdesc)(sc, &rmd, LE_RMDADDR(sc, no), sizeof(rmd));
   1124 	len = rmd.rmd3;
   1125 	printf("%s: receive buffer %d, len = %d\n", sc->sc_dev.dv_xname, no,
   1126 	    len);
   1127 	printf("%s: status %04x\n", sc->sc_dev.dv_xname,
   1128 	    (*sc->sc_rdcsr)(sc, LE_CSR0));
   1129 	printf("%s: ladr %04x, hadr %02x, flags %02x, bcnt %04x, mcnt %04x\n",
   1130 	    sc->sc_dev.dv_xname,
   1131 	    rmd.rmd0, rmd.rmd1_hadr, rmd.rmd1_bits, rmd.rmd2, rmd.rmd3);
   1132 	if (len >= sizeof(eh)) {
   1133 		(*sc->sc_copyfrombuf)(sc, &eh, LE_RBUFADDR(sc, no), sizeof(eh));
   1134 		printf("%s: dst %s", sc->sc_dev.dv_xname,
   1135 			ether_sprintf(eh.ether_dhost));
   1136 		printf(" src %s type %04x\n", ether_sprintf(eh.ether_shost),
   1137 			ntohs(eh.ether_type));
   1138 	}
   1139 }
   1140 
   1141 void
   1142 am7990_xmit_print(sc, no)
   1143 	struct am7990_softc *sc;
   1144 	int no;
   1145 {
   1146 	struct letmd tmd;
   1147 	u_int16_t len;
   1148 	struct ether_header eh;
   1149 
   1150 	(*sc->sc_copyfromdesc)(sc, &tmd, LE_TMDADDR(sc, no), sizeof(tmd));
   1151 	len = -tmd.tmd2;
   1152 	printf("%s: transmit buffer %d, len = %d\n", sc->sc_dev.dv_xname, no,
   1153 	    len);
   1154 	printf("%s: status %04x\n", sc->sc_dev.dv_xname,
   1155 	    (*sc->sc_rdcsr)(sc, LE_CSR0));
   1156 	printf("%s: ladr %04x, hadr %02x, flags %02x, bcnt %04x, mcnt %04x\n",
   1157 	    sc->sc_dev.dv_xname,
   1158 	    tmd.tmd0, tmd.tmd1_hadr, tmd.tmd1_bits, tmd.tmd2, tmd.tmd3);
   1159 	if (len >= sizeof(eh)) {
   1160 		(*sc->sc_copyfrombuf)(sc, &eh, LE_TBUFADDR(sc, no), sizeof(eh));
   1161 		printf("%s: dst %s", sc->sc_dev.dv_xname,
   1162 			ether_sprintf(eh.ether_dhost));
   1163 		printf(" src %s type %04x\n", ether_sprintf(eh.ether_shost),
   1164 		    ntohs(eh.ether_type));
   1165 	}
   1166 }
   1167 #endif /* LEDEBUG */
   1168 
   1169 /*
   1170  * Set up the logical address filter.
   1171  */
   1172 void
   1173 am7990_setladrf(ac, af)
   1174 	struct ethercom *ac;
   1175 	u_int16_t *af;
   1176 {
   1177 	struct ifnet *ifp = &ac->ec_if;
   1178 	struct ether_multi *enm;
   1179 	register u_char *cp;
   1180 	register u_int32_t crc;
   1181 	static const u_int32_t crctab[] = {
   1182 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
   1183 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
   1184 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
   1185 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
   1186 	};
   1187 	register int len;
   1188 	struct ether_multistep step;
   1189 
   1190 	/*
   1191 	 * Set up multicast address filter by passing all multicast addresses
   1192 	 * through a crc generator, and then using the high order 6 bits as an
   1193 	 * index into the 64 bit logical address filter.  The high order bit
   1194 	 * selects the word, while the rest of the bits select the bit within
   1195 	 * the word.
   1196 	 */
   1197 
   1198 	if (ifp->if_flags & IFF_PROMISC)
   1199 		goto allmulti;
   1200 
   1201 	af[0] = af[1] = af[2] = af[3] = 0x0000;
   1202 	ETHER_FIRST_MULTI(step, ac, enm);
   1203 	while (enm != NULL) {
   1204 		if (ETHER_CMP(enm->enm_addrlo, enm->enm_addrhi)) {
   1205 			/*
   1206 			 * We must listen to a range of multicast addresses.
   1207 			 * For now, just accept all multicasts, rather than
   1208 			 * trying to set only those filter bits needed to match
   1209 			 * the range.  (At this time, the only use of address
   1210 			 * ranges is for IP multicast routing, for which the
   1211 			 * range is big enough to require all bits set.)
   1212 			 */
   1213 			goto allmulti;
   1214 		}
   1215 
   1216 		cp = enm->enm_addrlo;
   1217 		crc = 0xffffffff;
   1218 		for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
   1219 			crc ^= *cp++;
   1220 			crc = (crc >> 4) ^ crctab[crc & 0xf];
   1221 			crc = (crc >> 4) ^ crctab[crc & 0xf];
   1222 		}
   1223 		/* Just want the 6 most significant bits. */
   1224 		crc >>= 26;
   1225 
   1226 		/* Set the corresponding bit in the filter. */
   1227 		af[crc >> 4] |= 1 << (crc & 0xf);
   1228 
   1229 		ETHER_NEXT_MULTI(step, enm);
   1230 	}
   1231 	ifp->if_flags &= ~IFF_ALLMULTI;
   1232 	return;
   1233 
   1234 allmulti:
   1235 	ifp->if_flags |= IFF_ALLMULTI;
   1236 	af[0] = af[1] = af[2] = af[3] = 0xffff;
   1237 }
   1238 
   1239 
   1240 /*
   1241  * Routines for accessing the transmit and receive buffers.
   1242  * The various CPU and adapter configurations supported by this
   1243  * driver require three different access methods for buffers
   1244  * and descriptors:
   1245  *	(1) contig (contiguous data; no padding),
   1246  *	(2) gap2 (two bytes of data followed by two bytes of padding),
   1247  *	(3) gap16 (16 bytes of data followed by 16 bytes of padding).
   1248  */
   1249 
   1250 /*
   1251  * contig: contiguous data with no padding.
   1252  *
   1253  * Buffers may have any alignment.
   1254  */
   1255 
   1256 void
   1257 am7990_copytobuf_contig(sc, from, boff, len)
   1258 	struct am7990_softc *sc;
   1259 	void *from;
   1260 	int boff, len;
   1261 {
   1262 	volatile caddr_t buf = sc->sc_mem;
   1263 
   1264 	/*
   1265 	 * Just call bcopy() to do the work.
   1266 	 */
   1267 	bcopy(from, buf + boff, len);
   1268 }
   1269 
   1270 void
   1271 am7990_copyfrombuf_contig(sc, to, boff, len)
   1272 	struct am7990_softc *sc;
   1273 	void *to;
   1274 	int boff, len;
   1275 {
   1276 	volatile caddr_t buf = sc->sc_mem;
   1277 
   1278 	/*
   1279 	 * Just call bcopy() to do the work.
   1280 	 */
   1281 	bcopy(buf + boff, to, len);
   1282 }
   1283 
   1284 void
   1285 am7990_zerobuf_contig(sc, boff, len)
   1286 	struct am7990_softc *sc;
   1287 	int boff, len;
   1288 {
   1289 	volatile caddr_t buf = sc->sc_mem;
   1290 
   1291 	/*
   1292 	 * Just let bzero() do the work
   1293 	 */
   1294 	bzero(buf + boff, len);
   1295 }
   1296 
   1297 #if 0
   1298 /*
   1299  * Examples only; duplicate these and tweak (if necessary) in
   1300  * machine-specific front-ends.
   1301  */
   1302 
   1303 /*
   1304  * gap2: two bytes of data followed by two bytes of pad.
   1305  *
   1306  * Buffers must be 4-byte aligned.  The code doesn't worry about
   1307  * doing an extra byte.
   1308  */
   1309 
   1310 void
   1311 am7990_copytobuf_gap2(sc, fromv, boff, len)
   1312 	struct am7990_softc *sc;
   1313 	void *fromv;
   1314 	int boff;
   1315 	register int len;
   1316 {
   1317 	volatile caddr_t buf = sc->sc_mem;
   1318 	register caddr_t from = fromv;
   1319 	register volatile u_int16_t *bptr;
   1320 
   1321 	if (boff & 0x1) {
   1322 		/* handle unaligned first byte */
   1323 		bptr = ((volatile u_int16_t *)buf) + (boff - 1);
   1324 		*bptr = (*from++ << 8) | (*bptr & 0xff);
   1325 		bptr += 2;
   1326 		len--;
   1327 	} else
   1328 		bptr = ((volatile u_int16_t *)buf) + boff;
   1329 	while (len > 1) {
   1330 		*bptr = (from[1] << 8) | (from[0] & 0xff);
   1331 		bptr += 2;
   1332 		from += 2;
   1333 		len -= 2;
   1334 	}
   1335 	if (len == 1)
   1336 		*bptr = (u_int16_t)*from;
   1337 }
   1338 
   1339 void
   1340 am7990_copyfrombuf_gap2(sc, tov, boff, len)
   1341 	struct am7990_softc *sc;
   1342 	void *tov;
   1343 	int boff, len;
   1344 {
   1345 	volatile caddr_t buf = sc->sc_mem;
   1346 	register caddr_t to = tov;
   1347 	register volatile u_int16_t *bptr;
   1348 	register u_int16_t tmp;
   1349 
   1350 	if (boff & 0x1) {
   1351 		/* handle unaligned first byte */
   1352 		bptr = ((volatile u_int16_t *)buf) + (boff - 1);
   1353 		*to++ = (*bptr >> 8) & 0xff;
   1354 		bptr += 2;
   1355 		len--;
   1356 	} else
   1357 		bptr = ((volatile u_int16_t *)buf) + boff;
   1358 	while (len > 1) {
   1359 		tmp = *bptr;
   1360 		*to++ = tmp & 0xff;
   1361 		*to++ = (tmp >> 8) & 0xff;
   1362 		bptr += 2;
   1363 		len -= 2;
   1364 	}
   1365 	if (len == 1)
   1366 		*to = *bptr & 0xff;
   1367 }
   1368 
   1369 void
   1370 am7990_zerobuf_gap2(sc, boff, len)
   1371 	struct am7990_softc *sc;
   1372 	int boff, len;
   1373 {
   1374 	volatile caddr_t buf = sc->sc_mem;
   1375 	register volatile u_int16_t *bptr;
   1376 
   1377 	if ((unsigned)boff & 0x1) {
   1378 		bptr = ((volatile u_int16_t *)buf) + (boff - 1);
   1379 		*bptr &= 0xff;
   1380 		bptr += 2;
   1381 		len--;
   1382 	} else
   1383 		bptr = ((volatile u_int16_t *)buf) + boff;
   1384 	while (len > 0) {
   1385 		*bptr = 0;
   1386 		bptr += 2;
   1387 		len -= 2;
   1388 	}
   1389 }
   1390 
   1391 /*
   1392  * gap16: 16 bytes of data followed by 16 bytes of pad.
   1393  *
   1394  * Buffers must be 32-byte aligned.
   1395  */
   1396 
   1397 void
   1398 am7990_copytobuf_gap16(sc, fromv, boff, len)
   1399 	struct am7990_softc *sc;
   1400 	void *fromv;
   1401 	int boff;
   1402 	register int len;
   1403 {
   1404 	volatile caddr_t buf = sc->sc_mem;
   1405 	register caddr_t from = fromv;
   1406 	register caddr_t bptr;
   1407 	register int xfer;
   1408 
   1409 	bptr = buf + ((boff << 1) & ~0x1f);
   1410 	boff &= 0xf;
   1411 	xfer = min(len, 16 - boff);
   1412 	while (len > 0) {
   1413 		bcopy(from, bptr + boff, xfer);
   1414 		from += xfer;
   1415 		bptr += 32;
   1416 		boff = 0;
   1417 		len -= xfer;
   1418 		xfer = min(len, 16);
   1419 	}
   1420 }
   1421 
   1422 void
   1423 am7990_copyfrombuf_gap16(sc, tov, boff, len)
   1424 	struct am7990_softc *sc;
   1425 	void *tov;
   1426 	int boff, len;
   1427 {
   1428 	volatile caddr_t buf = sc->sc_mem;
   1429 	register caddr_t to = tov;
   1430 	register caddr_t bptr;
   1431 	register int xfer;
   1432 
   1433 	bptr = buf + ((boff << 1) & ~0x1f);
   1434 	boff &= 0xf;
   1435 	xfer = min(len, 16 - boff);
   1436 	while (len > 0) {
   1437 		bcopy(bptr + boff, to, xfer);
   1438 		to += xfer;
   1439 		bptr += 32;
   1440 		boff = 0;
   1441 		len -= xfer;
   1442 		xfer = min(len, 16);
   1443 	}
   1444 }
   1445 
   1446 void
   1447 am7990_zerobuf_gap16(sc, boff, len)
   1448 	struct am7990_softc *sc;
   1449 	int boff, len;
   1450 {
   1451 	volatile caddr_t buf = sc->sc_mem;
   1452 	register caddr_t bptr;
   1453 	register int xfer;
   1454 
   1455 	bptr = buf + ((boff << 1) & ~0x1f);
   1456 	boff &= 0xf;
   1457 	xfer = min(len, 16 - boff);
   1458 	while (len > 0) {
   1459 		bzero(bptr + boff, xfer);
   1460 		bptr += 32;
   1461 		boff = 0;
   1462 		len -= xfer;
   1463 		xfer = min(len, 16);
   1464 	}
   1465 }
   1466 #endif /* Example only */
   1467