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