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