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