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