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