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