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