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