xref: /openbsd-src/sys/dev/ic/lance.c (revision 1e1858b66c07b2325844bd60ebf172f05b3364bb)
1*1e1858b6Smpi /*	$OpenBSD: lance.c,v 1.13 2016/04/13 10:49:26 mpi Exp $	*/
20ec3b04cSmiod /*	$NetBSD: lance.c,v 1.46 2012/02/02 19:43:03 tls Exp $	*/
30ec3b04cSmiod 
40ec3b04cSmiod /*-
50ec3b04cSmiod  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
60ec3b04cSmiod  * All rights reserved.
70ec3b04cSmiod  *
80ec3b04cSmiod  * This code is derived from software contributed to The NetBSD Foundation
90ec3b04cSmiod  * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
100ec3b04cSmiod  * Simulation Facility, NASA Ames Research Center.
110ec3b04cSmiod  *
120ec3b04cSmiod  * Redistribution and use in source and binary forms, with or without
130ec3b04cSmiod  * modification, are permitted provided that the following conditions
140ec3b04cSmiod  * are met:
150ec3b04cSmiod  * 1. Redistributions of source code must retain the above copyright
160ec3b04cSmiod  *    notice, this list of conditions and the following disclaimer.
170ec3b04cSmiod  * 2. Redistributions in binary form must reproduce the above copyright
180ec3b04cSmiod  *    notice, this list of conditions and the following disclaimer in the
190ec3b04cSmiod  *    documentation and/or other materials provided with the distribution.
200ec3b04cSmiod  *
210ec3b04cSmiod  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
220ec3b04cSmiod  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
230ec3b04cSmiod  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
240ec3b04cSmiod  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
250ec3b04cSmiod  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
260ec3b04cSmiod  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
270ec3b04cSmiod  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
280ec3b04cSmiod  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
290ec3b04cSmiod  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
300ec3b04cSmiod  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
310ec3b04cSmiod  * POSSIBILITY OF SUCH DAMAGE.
320ec3b04cSmiod  */
330ec3b04cSmiod 
340ec3b04cSmiod /*-
350ec3b04cSmiod  * Copyright (c) 1992, 1993
360ec3b04cSmiod  *	The Regents of the University of California.  All rights reserved.
370ec3b04cSmiod  *
380ec3b04cSmiod  * This code is derived from software contributed to Berkeley by
390ec3b04cSmiod  * Ralph Campbell and Rick Macklem.
400ec3b04cSmiod  *
410ec3b04cSmiod  * Redistribution and use in source and binary forms, with or without
420ec3b04cSmiod  * modification, are permitted provided that the following conditions
430ec3b04cSmiod  * are met:
440ec3b04cSmiod  * 1. Redistributions of source code must retain the above copyright
450ec3b04cSmiod  *    notice, this list of conditions and the following disclaimer.
460ec3b04cSmiod  * 2. Redistributions in binary form must reproduce the above copyright
470ec3b04cSmiod  *    notice, this list of conditions and the following disclaimer in the
480ec3b04cSmiod  *    documentation and/or other materials provided with the distribution.
490ec3b04cSmiod  * 3. Neither the name of the University nor the names of its contributors
500ec3b04cSmiod  *    may be used to endorse or promote products derived from this software
510ec3b04cSmiod  *    without specific prior written permission.
520ec3b04cSmiod  *
530ec3b04cSmiod  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
540ec3b04cSmiod  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
550ec3b04cSmiod  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
560ec3b04cSmiod  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
570ec3b04cSmiod  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
580ec3b04cSmiod  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
590ec3b04cSmiod  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
600ec3b04cSmiod  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
610ec3b04cSmiod  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
620ec3b04cSmiod  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
630ec3b04cSmiod  * SUCH DAMAGE.
640ec3b04cSmiod  *
650ec3b04cSmiod  *	@(#)if_le.c	8.2 (Berkeley) 11/16/93
660ec3b04cSmiod  */
670ec3b04cSmiod 
680ec3b04cSmiod #include "bpfilter.h"
690ec3b04cSmiod 
700ec3b04cSmiod #include <sys/param.h>
710ec3b04cSmiod #include <sys/systm.h>
720ec3b04cSmiod #include <sys/mbuf.h>
730ec3b04cSmiod #include <sys/syslog.h>
740ec3b04cSmiod #include <sys/socket.h>
750ec3b04cSmiod #include <sys/device.h>
760ec3b04cSmiod #include <sys/malloc.h>
770ec3b04cSmiod #include <sys/ioctl.h>
780ec3b04cSmiod #include <sys/errno.h>
790ec3b04cSmiod 
800ec3b04cSmiod #include <net/if.h>
810ec3b04cSmiod #include <net/if_media.h>
820ec3b04cSmiod 
830ec3b04cSmiod #include <netinet/in.h>
840ec3b04cSmiod #include <netinet/if_ether.h>
850ec3b04cSmiod 
860ec3b04cSmiod #if NBPFILTER > 0
870ec3b04cSmiod #include <net/bpf.h>
880ec3b04cSmiod #endif
890ec3b04cSmiod 
900ec3b04cSmiod #include <dev/ic/lancereg.h>
910ec3b04cSmiod #include <dev/ic/lancevar.h>
920ec3b04cSmiod 
930ec3b04cSmiod #ifdef DDB
940ec3b04cSmiod #define	integrate
950ec3b04cSmiod #define hide
960ec3b04cSmiod #else
970ec3b04cSmiod #define	integrate	static inline
980ec3b04cSmiod #define hide		static
990ec3b04cSmiod #endif
1000ec3b04cSmiod 
1010ec3b04cSmiod integrate struct mbuf *lance_get(struct lance_softc *, int, int);
1020ec3b04cSmiod 
1030ec3b04cSmiod int lance_mediachange(struct ifnet *);
1040ec3b04cSmiod void lance_mediastatus(struct ifnet *, struct ifmediareq *);
1050ec3b04cSmiod 
1060ec3b04cSmiod static inline u_int16_t ether_cmp(void *, void *);
1070ec3b04cSmiod 
1080ec3b04cSmiod void lance_stop(struct ifnet *, int);
1090ec3b04cSmiod int lance_ioctl(struct ifnet *, u_long, caddr_t);
1100ec3b04cSmiod void lance_watchdog(struct ifnet *);
1110ec3b04cSmiod 
1120ec3b04cSmiod struct cfdriver le_cd = {
1130ec3b04cSmiod 	NULL, "le", DV_IFNET
1140ec3b04cSmiod };
1150ec3b04cSmiod 
1160ec3b04cSmiod /*
1170ec3b04cSmiod  * Compare two Ether/802 addresses for equality, inlined and
1180ec3b04cSmiod  * unrolled for speed.  Use this like memcmp().
1190ec3b04cSmiod  *
1200ec3b04cSmiod  * XXX: Add <machine/inlines.h> for stuff like this?
1210ec3b04cSmiod  * XXX: or maybe add it to libkern.h instead?
1220ec3b04cSmiod  *
1230ec3b04cSmiod  * "I'd love to have an inline assembler version of this."
1240ec3b04cSmiod  * XXX: Who wanted that? mycroft?  I wrote one, but this
1250ec3b04cSmiod  * version in C is as good as hand-coded assembly. -gwr
1260ec3b04cSmiod  *
1270ec3b04cSmiod  * Please do NOT tweak this without looking at the actual
1280ec3b04cSmiod  * assembly code generated before and after your tweaks!
1290ec3b04cSmiod  */
1300ec3b04cSmiod static inline uint16_t
ether_cmp(void * one,void * two)1310ec3b04cSmiod ether_cmp(void *one, void *two)
1320ec3b04cSmiod {
1330ec3b04cSmiod 	uint16_t *a = (uint16_t *)one;
1340ec3b04cSmiod 	uint16_t *b = (uint16_t *)two;
1350ec3b04cSmiod 	uint16_t diff;
1360ec3b04cSmiod 
1370ec3b04cSmiod #ifdef	__m68k__
1380ec3b04cSmiod 	/*
1390ec3b04cSmiod 	 * The post-increment-pointer form produces the best
1400ec3b04cSmiod 	 * machine code for m68k.  This was carefully tuned
1410ec3b04cSmiod 	 * so it compiles to just 8 short (2-byte) op-codes!
1420ec3b04cSmiod 	 */
1430ec3b04cSmiod 	diff  = *a++ - *b++;
1440ec3b04cSmiod 	diff |= *a++ - *b++;
1450ec3b04cSmiod 	diff |= *a++ - *b++;
1460ec3b04cSmiod #else
1470ec3b04cSmiod 	/*
1482c83f2c7Smmcc 	 * Most modern CPUs do better with a single expression.
1490ec3b04cSmiod 	 * Note that short-cut evaluation is NOT helpful here,
1500ec3b04cSmiod 	 * because it just makes the code longer, not faster!
1510ec3b04cSmiod 	 */
1520ec3b04cSmiod 	diff = (a[0] - b[0]) | (a[1] - b[1]) | (a[2] - b[2]);
1530ec3b04cSmiod #endif
1540ec3b04cSmiod 
1550ec3b04cSmiod 	return (diff);
1560ec3b04cSmiod }
1570ec3b04cSmiod 
1580ec3b04cSmiod #define ETHER_CMP	ether_cmp
1590ec3b04cSmiod 
1600ec3b04cSmiod #ifdef LANCE_REVC_BUG
1610ec3b04cSmiod /* Make sure this is short-aligned, for ether_cmp(). */
1620ec3b04cSmiod static uint16_t bcast_enaddr[3] = { ~0, ~0, ~0 };
1630ec3b04cSmiod #endif
1640ec3b04cSmiod 
1650ec3b04cSmiod void
lance_config(struct lance_softc * sc)1660ec3b04cSmiod lance_config(struct lance_softc *sc)
1670ec3b04cSmiod {
1680ec3b04cSmiod 	int i, nbuf;
1690ec3b04cSmiod 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
1700ec3b04cSmiod 
1710ec3b04cSmiod 	/* Initialize ifnet structure. */
1720ec3b04cSmiod 	strlcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
1730ec3b04cSmiod 	ifp->if_softc = sc;
1740ec3b04cSmiod 	ifp->if_start = sc->sc_start;
1750ec3b04cSmiod 	ifp->if_ioctl = lance_ioctl;
1760ec3b04cSmiod 	ifp->if_watchdog = lance_watchdog;
1770ec3b04cSmiod 	ifp->if_flags =
1789d022f3eStedu 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1790ec3b04cSmiod #ifdef LANCE_REVC_BUG
1800ec3b04cSmiod 	ifp->if_flags &= ~IFF_MULTICAST;
1810ec3b04cSmiod #endif
1820ec3b04cSmiod 	ifp->if_baudrate = IF_Mbps(10);
1830ec3b04cSmiod 
1840ec3b04cSmiod 	/* Initialize ifmedia structures. */
1850ec3b04cSmiod 	ifmedia_init(&sc->sc_ifmedia, 0, lance_mediachange, lance_mediastatus);
1860ec3b04cSmiod 	if (sc->sc_supmedia != NULL) {
1870ec3b04cSmiod 		for (i = 0; i < sc->sc_nsupmedia; i++)
1880ec3b04cSmiod 			ifmedia_add(&sc->sc_ifmedia, sc->sc_supmedia[i],
1890ec3b04cSmiod 			   0, NULL);
1900ec3b04cSmiod 		ifmedia_set(&sc->sc_ifmedia, sc->sc_defaultmedia);
1910ec3b04cSmiod 	} else {
1920ec3b04cSmiod 		ifmedia_add(&sc->sc_ifmedia, IFM_ETHER|IFM_MANUAL, 0, NULL);
1930ec3b04cSmiod 		ifmedia_set(&sc->sc_ifmedia, IFM_ETHER|IFM_MANUAL);
1940ec3b04cSmiod 	}
1950ec3b04cSmiod 
1960ec3b04cSmiod 	if (sc->sc_memsize > 262144)
1970ec3b04cSmiod 		sc->sc_memsize = 262144;
1980ec3b04cSmiod 
1990ec3b04cSmiod 	switch (sc->sc_memsize) {
2000ec3b04cSmiod 	case 8192:
2010ec3b04cSmiod 		sc->sc_nrbuf = 4;
2020ec3b04cSmiod 		sc->sc_ntbuf = 1;
2030ec3b04cSmiod 		break;
2040ec3b04cSmiod 	case 16384:
2050ec3b04cSmiod 		sc->sc_nrbuf = 8;
2060ec3b04cSmiod 		sc->sc_ntbuf = 2;
2070ec3b04cSmiod 		break;
2080ec3b04cSmiod 	case 32768:
2090ec3b04cSmiod 		sc->sc_nrbuf = 16;
2100ec3b04cSmiod 		sc->sc_ntbuf = 4;
2110ec3b04cSmiod 		break;
2120ec3b04cSmiod 	case 65536:
2130ec3b04cSmiod 		sc->sc_nrbuf = 32;
2140ec3b04cSmiod 		sc->sc_ntbuf = 8;
2150ec3b04cSmiod 		break;
2160ec3b04cSmiod 	case 131072:
2170ec3b04cSmiod 		sc->sc_nrbuf = 64;
2180ec3b04cSmiod 		sc->sc_ntbuf = 16;
2190ec3b04cSmiod 		break;
2200ec3b04cSmiod 	case 262144:
2210ec3b04cSmiod 		sc->sc_nrbuf = 128;
2220ec3b04cSmiod 		sc->sc_ntbuf = 32;
2230ec3b04cSmiod 		break;
2240ec3b04cSmiod 	default:
2250ec3b04cSmiod 		/* weird memory size; cope with it */
2260ec3b04cSmiod 		nbuf = sc->sc_memsize / LEBLEN;
2270ec3b04cSmiod 		sc->sc_ntbuf = nbuf / 5;
2280ec3b04cSmiod 		sc->sc_nrbuf = nbuf - sc->sc_ntbuf;
2290ec3b04cSmiod 	}
2300ec3b04cSmiod 
2310ec3b04cSmiod 	printf(": address %s\n", ether_sprintf(sc->sc_arpcom.ac_enaddr));
2320ec3b04cSmiod 	printf("%s: %d receive buffers, %d transmit buffers\n",
2330ec3b04cSmiod 	    sc->sc_dev.dv_xname, sc->sc_nrbuf, sc->sc_ntbuf);
2340ec3b04cSmiod 
2350ec3b04cSmiod 	/* Make sure the chip is stopped. */
2360ec3b04cSmiod 	lance_stop(ifp, 0);
2370ec3b04cSmiod 
2380ec3b04cSmiod 	/* claim 802.1q capability */
2390ec3b04cSmiod 	ifp->if_capabilities = IFCAP_VLAN_MTU;
2400ec3b04cSmiod 
2410ec3b04cSmiod 	/* Attach the interface. */
2420ec3b04cSmiod 	if_attach(ifp);
2430ec3b04cSmiod 	ether_ifattach(ifp);
2440ec3b04cSmiod 
2459f6fb5c7Sderaadt 	sc->sc_rbufaddr = mallocarray(sc->sc_nrbuf, sizeof(int), M_DEVBUF,
2460ec3b04cSmiod 	    M_WAITOK);
2479f6fb5c7Sderaadt 	sc->sc_tbufaddr = mallocarray(sc->sc_ntbuf, sizeof(int), M_DEVBUF,
2480ec3b04cSmiod 	    M_WAITOK);
2490ec3b04cSmiod }
2500ec3b04cSmiod 
2510ec3b04cSmiod void
lance_reset(struct lance_softc * sc)2520ec3b04cSmiod lance_reset(struct lance_softc *sc)
2530ec3b04cSmiod {
2540ec3b04cSmiod 	int s;
2550ec3b04cSmiod 
2560ec3b04cSmiod 	s = splnet();
2570ec3b04cSmiod 	lance_init(sc);
2580ec3b04cSmiod 	splx(s);
2590ec3b04cSmiod }
2600ec3b04cSmiod 
2610ec3b04cSmiod void
lance_stop(struct ifnet * ifp,int disable)2620ec3b04cSmiod lance_stop(struct ifnet *ifp, int disable)
2630ec3b04cSmiod {
2640ec3b04cSmiod 	struct lance_softc *sc = ifp->if_softc;
2650ec3b04cSmiod 
2660ec3b04cSmiod 	(*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_STOP);
2670ec3b04cSmiod }
2680ec3b04cSmiod 
2690ec3b04cSmiod /*
2700ec3b04cSmiod  * Initialization of interface; set up initialization block
2710ec3b04cSmiod  * and transmit/receive descriptor rings.
2720ec3b04cSmiod  */
2730ec3b04cSmiod int
lance_init(struct lance_softc * sc)2740ec3b04cSmiod lance_init(struct lance_softc *sc)
2750ec3b04cSmiod {
2760ec3b04cSmiod 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
2770ec3b04cSmiod 	int timo;
2780ec3b04cSmiod 	u_long a;
2790ec3b04cSmiod 
2800ec3b04cSmiod 	(*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_STOP);
2810ec3b04cSmiod 	DELAY(100);
2820ec3b04cSmiod 
2830ec3b04cSmiod 	/* Newer LANCE chips have a reset register */
2840ec3b04cSmiod 	if (sc->sc_hwreset)
2850ec3b04cSmiod 		(*sc->sc_hwreset)(sc);
2860ec3b04cSmiod 
2870ec3b04cSmiod 	/* Set the correct byte swapping mode, etc. */
2880ec3b04cSmiod 	(*sc->sc_wrcsr)(sc, LE_CSR3, sc->sc_conf3);
2890ec3b04cSmiod 
2900ec3b04cSmiod 	/* Set up LANCE init block. */
2910ec3b04cSmiod 	(*sc->sc_meminit)(sc);
2920ec3b04cSmiod 
2930ec3b04cSmiod 	/* Give LANCE the physical address of its init block. */
2940ec3b04cSmiod 	a = sc->sc_addr + LE_INITADDR(sc);
2950ec3b04cSmiod 	(*sc->sc_wrcsr)(sc, LE_CSR1, a);
2960ec3b04cSmiod 	(*sc->sc_wrcsr)(sc, LE_CSR2, a >> 16);
2970ec3b04cSmiod 
2980ec3b04cSmiod 	/* Try to initialize the LANCE. */
2990ec3b04cSmiod 	DELAY(100);
3000ec3b04cSmiod 	(*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INIT);
3010ec3b04cSmiod 
3020ec3b04cSmiod 	/* Wait for initialization to finish. */
3030ec3b04cSmiod 	for (timo = 100000; timo; timo--)
3040ec3b04cSmiod 		if ((*sc->sc_rdcsr)(sc, LE_CSR0) & LE_C0_IDON)
3050ec3b04cSmiod 			break;
3060ec3b04cSmiod 
3070ec3b04cSmiod 	if ((*sc->sc_rdcsr)(sc, LE_CSR0) & LE_C0_IDON) {
3080ec3b04cSmiod 		/* Start the LANCE. */
3090ec3b04cSmiod 		(*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA | LE_C0_STRT);
3100ec3b04cSmiod 		ifp->if_flags |= IFF_RUNNING;
311de6cd8fbSdlg 		ifq_clr_oactive(&ifp->if_snd);
3120ec3b04cSmiod 		ifp->if_timer = 0;
3130ec3b04cSmiod 		(*sc->sc_start)(ifp);
3140ec3b04cSmiod 	} else
3150ec3b04cSmiod 		printf("%s: controller failed to initialize\n",
3160ec3b04cSmiod 		     sc->sc_dev.dv_xname);
3170ec3b04cSmiod 	if (sc->sc_hwinit)
3180ec3b04cSmiod 		(*sc->sc_hwinit)(sc);
3190ec3b04cSmiod 
3200ec3b04cSmiod 	return (0);
3210ec3b04cSmiod }
3220ec3b04cSmiod 
3230ec3b04cSmiod /*
3240ec3b04cSmiod  * Routine to copy from mbuf chain to transmit buffer in
3250ec3b04cSmiod  * network buffer memory.
3260ec3b04cSmiod  */
3270ec3b04cSmiod int
lance_put(struct lance_softc * sc,int boff,struct mbuf * m)3280ec3b04cSmiod lance_put(struct lance_softc *sc, int boff, struct mbuf *m)
3290ec3b04cSmiod {
3300ec3b04cSmiod 	struct mbuf *n;
3310ec3b04cSmiod 	int len, tlen = 0;
3320ec3b04cSmiod 
3330ec3b04cSmiod 	for (; m; m = n) {
3340ec3b04cSmiod 		len = m->m_len;
3350ec3b04cSmiod 		if (len == 0) {
336822e8206Smpi 			n = m_free(m);
3370ec3b04cSmiod 			continue;
3380ec3b04cSmiod 		}
3390ec3b04cSmiod 		(*sc->sc_copytobuf)(sc, mtod(m, void *), boff, len);
3400ec3b04cSmiod 		boff += len;
3410ec3b04cSmiod 		tlen += len;
342822e8206Smpi 		n = m_free(m);
3430ec3b04cSmiod 	}
3440ec3b04cSmiod 	if (tlen < LEMINSIZE) {
3450ec3b04cSmiod 		(*sc->sc_zerobuf)(sc, boff, LEMINSIZE - tlen);
3460ec3b04cSmiod 		tlen = LEMINSIZE;
3470ec3b04cSmiod 	}
3480ec3b04cSmiod 	return (tlen);
3490ec3b04cSmiod }
3500ec3b04cSmiod 
3510ec3b04cSmiod /*
3520ec3b04cSmiod  * Pull data off an interface.
3530ec3b04cSmiod  * Len is length of data, with local net header stripped.
3540ec3b04cSmiod  * We copy the data into mbufs.  When full cluster sized units are present
3550ec3b04cSmiod  * we copy into clusters.
3560ec3b04cSmiod  */
3570ec3b04cSmiod integrate struct mbuf *
lance_get(struct lance_softc * sc,int boff,int totlen)3580ec3b04cSmiod lance_get(struct lance_softc *sc, int boff, int totlen)
3590ec3b04cSmiod {
3600ec3b04cSmiod 	struct mbuf *m, *top, **mp;
3610ec3b04cSmiod 	int len, pad;
3620ec3b04cSmiod 
3630ec3b04cSmiod 	MGETHDR(m, M_DONTWAIT, MT_DATA);
3640ec3b04cSmiod 	if (m == NULL)
3650ec3b04cSmiod 		return (NULL);
3660ec3b04cSmiod 	m->m_pkthdr.len = totlen;
3670ec3b04cSmiod 	pad = ALIGN(sizeof(struct ether_header)) - sizeof(struct ether_header);
3680ec3b04cSmiod 	m->m_data += pad;
3690ec3b04cSmiod 	len = MHLEN - pad;
3700ec3b04cSmiod 	top = NULL;
3710ec3b04cSmiod 	mp = &top;
3720ec3b04cSmiod 
3730ec3b04cSmiod 	while (totlen > 0) {
3740ec3b04cSmiod 		if (top) {
3750ec3b04cSmiod 			MGET(m, M_DONTWAIT, MT_DATA);
3760ec3b04cSmiod 			if (m == NULL) {
3770ec3b04cSmiod 				m_freem(top);
3780ec3b04cSmiod 				return NULL;
3790ec3b04cSmiod 			}
3800ec3b04cSmiod 			len = MLEN;
3810ec3b04cSmiod 		}
3820ec3b04cSmiod 		if (totlen >= MINCLSIZE) {
3830ec3b04cSmiod 			MCLGET(m, M_DONTWAIT);
3840ec3b04cSmiod 			if (m->m_flags & M_EXT) {
3850ec3b04cSmiod 				len = MCLBYTES;
3860ec3b04cSmiod 				if (!top) {
3870ec3b04cSmiod 					m->m_data += pad;
3880ec3b04cSmiod 					len -= pad;
3890ec3b04cSmiod 				}
3900ec3b04cSmiod 			}
3910ec3b04cSmiod 		}
3920ec3b04cSmiod 		m->m_len = len = min(totlen, len);
3930ec3b04cSmiod 		(*sc->sc_copyfrombuf)(sc, mtod(m, caddr_t), boff, len);
3940ec3b04cSmiod 		boff += len;
3950ec3b04cSmiod 		totlen -= len;
3960ec3b04cSmiod 		*mp = m;
3970ec3b04cSmiod 		mp = &m->m_next;
3980ec3b04cSmiod 	}
3990ec3b04cSmiod 
4000ec3b04cSmiod 	return (top);
4010ec3b04cSmiod }
4020ec3b04cSmiod 
4030d918b5eSmpi struct mbuf *
lance_read(struct lance_softc * sc,int boff,int len)4040ec3b04cSmiod lance_read(struct lance_softc *sc, int boff, int len)
4050ec3b04cSmiod {
4060ec3b04cSmiod 	struct mbuf *m;
4070ec3b04cSmiod 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
4080ec3b04cSmiod 	struct ether_header *eh;
4090ec3b04cSmiod 
4100ec3b04cSmiod 	if (len <= sizeof(struct ether_header) ||
4110ec3b04cSmiod 	    len > ((ifp->if_capabilities & IFCAP_VLAN_MTU) ?
4120ec3b04cSmiod 		ETHER_VLAN_ENCAP_LEN + ETHERMTU + sizeof(struct ether_header) :
4130ec3b04cSmiod 		ETHERMTU + sizeof(struct ether_header))) {
4140ec3b04cSmiod #ifdef LEDEBUG
4150ec3b04cSmiod 		printf("%s: invalid packet size %d; dropping\n",
4160ec3b04cSmiod 		    sc->sc_dev.dv_xnam, len);
4170ec3b04cSmiod #endif
4180ec3b04cSmiod 		ifp->if_ierrors++;
4190d918b5eSmpi 		return (NULL);
4200ec3b04cSmiod 	}
4210ec3b04cSmiod 
4220ec3b04cSmiod 	/* Pull packet off interface. */
4230ec3b04cSmiod 	m = lance_get(sc, boff, len);
4240ec3b04cSmiod 	if (m == NULL) {
4250ec3b04cSmiod 		ifp->if_ierrors++;
4260d918b5eSmpi 		return (NULL);
4270ec3b04cSmiod 	}
4280ec3b04cSmiod 
4290ec3b04cSmiod 	eh = mtod(m, struct ether_header *);
4300ec3b04cSmiod 
4310ec3b04cSmiod #ifdef LANCE_REVC_BUG
4320ec3b04cSmiod 	/*
4330ec3b04cSmiod 	 * The old LANCE (Rev. C) chips have a bug which causes
4340ec3b04cSmiod 	 * garbage to be inserted in front of the received packet.
4350ec3b04cSmiod 	 * The work-around is to ignore packets with an invalid
4360ec3b04cSmiod 	 * destination address (garbage will usually not match).
4370ec3b04cSmiod 	 * Of course, this precludes multicast support...
4380ec3b04cSmiod 	 */
4390ec3b04cSmiod 	if (ETHER_CMP(eh->ether_dhost, sc->sc_arpcom.ac_enaddr) &&
4400ec3b04cSmiod 	    ETHER_CMP(eh->ether_dhost, bcast_enaddr)) {
4410ec3b04cSmiod 		m_freem(m);
4420d918b5eSmpi 		return (NULL);
4430ec3b04cSmiod 	}
4440ec3b04cSmiod #endif
4450ec3b04cSmiod 
4460ec3b04cSmiod 	/*
4470ec3b04cSmiod 	 * Some lance device does not present IFF_SIMPLEX behavior on multicast
4480ec3b04cSmiod 	 * packets.  Make sure to drop it if it is from ourselves.
4490ec3b04cSmiod 	 */
4500ec3b04cSmiod 	if (!ETHER_CMP(eh->ether_shost, sc->sc_arpcom.ac_enaddr)) {
4510ec3b04cSmiod 		m_freem(m);
4520d918b5eSmpi 		return (NULL);
4530ec3b04cSmiod 	}
4540ec3b04cSmiod 
4550d918b5eSmpi 	return (m);
4560ec3b04cSmiod }
4570ec3b04cSmiod 
4580ec3b04cSmiod void
lance_watchdog(struct ifnet * ifp)4590ec3b04cSmiod lance_watchdog(struct ifnet *ifp)
4600ec3b04cSmiod {
4610ec3b04cSmiod 	struct lance_softc *sc = ifp->if_softc;
4620ec3b04cSmiod 
4630ec3b04cSmiod 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
4640ec3b04cSmiod 	++ifp->if_oerrors;
4650ec3b04cSmiod 
4660ec3b04cSmiod 	lance_reset(sc);
4670ec3b04cSmiod }
4680ec3b04cSmiod 
4690ec3b04cSmiod int
lance_mediachange(struct ifnet * ifp)4700ec3b04cSmiod lance_mediachange(struct ifnet *ifp)
4710ec3b04cSmiod {
4720ec3b04cSmiod 	struct lance_softc *sc = ifp->if_softc;
4730ec3b04cSmiod 
4740ec3b04cSmiod 	if (sc->sc_mediachange)
4750ec3b04cSmiod 		return ((*sc->sc_mediachange)(sc));
4760ec3b04cSmiod 	return (0);
4770ec3b04cSmiod }
4780ec3b04cSmiod 
4790ec3b04cSmiod void
lance_mediastatus(struct ifnet * ifp,struct ifmediareq * ifmr)4800ec3b04cSmiod lance_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
4810ec3b04cSmiod {
4820ec3b04cSmiod 	struct lance_softc *sc = ifp->if_softc;
4830ec3b04cSmiod 
4840ec3b04cSmiod 	if ((ifp->if_flags & IFF_UP) == 0)
4850ec3b04cSmiod 		return;
4860ec3b04cSmiod 
4870ec3b04cSmiod 	ifmr->ifm_status = IFM_AVALID;
4880ec3b04cSmiod 	if (sc->sc_havecarrier)
4890ec3b04cSmiod 		ifmr->ifm_status |= IFM_ACTIVE;
4900ec3b04cSmiod 
4910ec3b04cSmiod 	if (sc->sc_mediastatus)
4920ec3b04cSmiod 		(*sc->sc_mediastatus)(sc, ifmr);
4930ec3b04cSmiod }
4940ec3b04cSmiod 
4950ec3b04cSmiod /*
4960ec3b04cSmiod  * Process an ioctl request.
4970ec3b04cSmiod  */
4980ec3b04cSmiod int
lance_ioctl(struct ifnet * ifp,u_long cmd,caddr_t data)4990ec3b04cSmiod lance_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
5000ec3b04cSmiod {
5010ec3b04cSmiod 	struct lance_softc *sc = ifp->if_softc;
5020ec3b04cSmiod 	struct ifreq *ifr = (struct ifreq *)data;
5030ec3b04cSmiod 	int s, error = 0;
5040ec3b04cSmiod 
5050ec3b04cSmiod 	s = splnet();
5060ec3b04cSmiod 
5070ec3b04cSmiod 	switch (cmd) {
5080ec3b04cSmiod 	case SIOCSIFADDR:
5090ec3b04cSmiod 		ifp->if_flags |= IFF_UP;
5100ec3b04cSmiod 		if (!(ifp->if_flags & IFF_RUNNING))
5110ec3b04cSmiod 			lance_init(sc);
5120ec3b04cSmiod 		break;
5130ec3b04cSmiod 
5140ec3b04cSmiod 	case SIOCSIFFLAGS:
5150ec3b04cSmiod 		if (ifp->if_flags & IFF_UP) {
5160ec3b04cSmiod 			if (ifp->if_flags & IFF_RUNNING)
5170ec3b04cSmiod 				error = ENETRESET;
5180ec3b04cSmiod 			else
5190ec3b04cSmiod 				lance_init(sc);
5200ec3b04cSmiod 		} else {
5210ec3b04cSmiod 			if (ifp->if_flags & IFF_RUNNING) {
5220ec3b04cSmiod 				lance_stop(ifp, 0);
5230ec3b04cSmiod 				ifp->if_flags &= ~IFF_RUNNING;
5240ec3b04cSmiod 			}
5250ec3b04cSmiod 		}
5260ec3b04cSmiod 
5270ec3b04cSmiod #ifdef LEDEBUG
5280ec3b04cSmiod 		if (ifp->if_flags & IFF_DEBUG)
5290ec3b04cSmiod 			sc->sc_debug = 1;
5300ec3b04cSmiod 		else
5310ec3b04cSmiod 			sc->sc_debug = 0;
5320ec3b04cSmiod #endif
5330ec3b04cSmiod 		break;
5340ec3b04cSmiod 
5350ec3b04cSmiod 	case SIOCGIFMEDIA:
5360ec3b04cSmiod 	case SIOCSIFMEDIA:
5370ec3b04cSmiod 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_ifmedia, cmd);
5380ec3b04cSmiod 		break;
5390ec3b04cSmiod 
5400ec3b04cSmiod 	default:
5410ec3b04cSmiod 		error = ether_ioctl(ifp, &sc->sc_arpcom, cmd, data);
5420ec3b04cSmiod 		break;
5430ec3b04cSmiod 	}
5440ec3b04cSmiod 
5450ec3b04cSmiod 	if (error == ENETRESET) {
5460ec3b04cSmiod 		if (ifp->if_flags & IFF_RUNNING) {
5470ec3b04cSmiod 			/*
5480ec3b04cSmiod 			 * Multicast list has changed; set the hardware filter
5490ec3b04cSmiod 			 * accordingly.
5500ec3b04cSmiod 			 */
5510ec3b04cSmiod 			lance_reset(sc);
5520ec3b04cSmiod 		}
5530ec3b04cSmiod 		error = 0;
5540ec3b04cSmiod 	}
5550ec3b04cSmiod 
5560ec3b04cSmiod 	splx(s);
5570ec3b04cSmiod 	return (error);
5580ec3b04cSmiod }
5590ec3b04cSmiod 
5600ec3b04cSmiod /*
5610ec3b04cSmiod  * Set up the logical address filter.
5620ec3b04cSmiod  */
5630ec3b04cSmiod void
lance_setladrf(struct arpcom * ac,uint16_t * af)5640ec3b04cSmiod lance_setladrf(struct arpcom *ac, uint16_t *af)
5650ec3b04cSmiod {
5660ec3b04cSmiod 	struct ifnet *ifp = &ac->ac_if;
5670ec3b04cSmiod 	struct ether_multi *enm;
5680ec3b04cSmiod 	uint32_t crc;
5690ec3b04cSmiod 	struct ether_multistep step;
5700ec3b04cSmiod 
5710ec3b04cSmiod 	/*
5720ec3b04cSmiod 	 * Set up multicast address filter by passing all multicast addresses
5730ec3b04cSmiod 	 * through a crc generator, and then using the high order 6 bits as an
5740ec3b04cSmiod 	 * index into the 64 bit logical address filter.  The high order bit
5750ec3b04cSmiod 	 * selects the word, while the rest of the bits select the bit within
5760ec3b04cSmiod 	 * the word.
5770ec3b04cSmiod 	 */
5780ec3b04cSmiod 
579f2e23e59Smpi 	if (ifp->if_flags & IFF_PROMISC || ac->ac_multirangecnt > 0)
5800ec3b04cSmiod 		goto allmulti;
5810ec3b04cSmiod 
5820ec3b04cSmiod 	af[0] = af[1] = af[2] = af[3] = 0x0000;
5830ec3b04cSmiod 	ETHER_FIRST_MULTI(step, ac, enm);
5840ec3b04cSmiod 	while (enm != NULL) {
5850ec3b04cSmiod 		crc = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
5860ec3b04cSmiod 
5870ec3b04cSmiod 		/* Just want the 6 most significant bits. */
5880ec3b04cSmiod 		crc >>= 26;
5890ec3b04cSmiod 
5900ec3b04cSmiod 		/* Set the corresponding bit in the filter. */
5910ec3b04cSmiod 		af[crc >> 4] |= 1 << (crc & 0xf);
5920ec3b04cSmiod 
5930ec3b04cSmiod 		ETHER_NEXT_MULTI(step, enm);
5940ec3b04cSmiod 	}
5950ec3b04cSmiod 	ifp->if_flags &= ~IFF_ALLMULTI;
5960ec3b04cSmiod 	return;
5970ec3b04cSmiod 
5980ec3b04cSmiod allmulti:
5990ec3b04cSmiod 	ifp->if_flags |= IFF_ALLMULTI;
6000ec3b04cSmiod 	af[0] = af[1] = af[2] = af[3] = 0xffff;
6010ec3b04cSmiod }
6020ec3b04cSmiod 
6030ec3b04cSmiod /*
6040ec3b04cSmiod  * Routines for accessing the transmit and receive buffers.
6050ec3b04cSmiod  * The various CPU and adapter configurations supported by this
6060ec3b04cSmiod  * driver require three different access methods for buffers
6070ec3b04cSmiod  * and descriptors:
6080ec3b04cSmiod  *	(1) contig (contiguous data; no padding),
6090ec3b04cSmiod  *	(2) gap2 (two bytes of data followed by two bytes of padding),
6100ec3b04cSmiod  *	(3) gap16 (16 bytes of data followed by 16 bytes of padding).
6110ec3b04cSmiod  */
6120ec3b04cSmiod 
6130ec3b04cSmiod /*
6140ec3b04cSmiod  * contig: contiguous data with no padding.
6150ec3b04cSmiod  *
6160ec3b04cSmiod  * Buffers may have any alignment.
6170ec3b04cSmiod  */
6180ec3b04cSmiod 
6190ec3b04cSmiod void
lance_copytobuf_contig(struct lance_softc * sc,void * from,int boff,int len)6200ec3b04cSmiod lance_copytobuf_contig(struct lance_softc *sc, void *from, int boff, int len)
6210ec3b04cSmiod {
6220ec3b04cSmiod 	uint8_t *buf = sc->sc_mem;
6230ec3b04cSmiod 
6240ec3b04cSmiod 	/*
6250ec3b04cSmiod 	 * Just call memcpy() to do the work.
6260ec3b04cSmiod 	 */
6270ec3b04cSmiod 	memcpy(buf + boff, from, len);
6280ec3b04cSmiod }
6290ec3b04cSmiod 
6300ec3b04cSmiod void
lance_copyfrombuf_contig(struct lance_softc * sc,void * to,int boff,int len)6310ec3b04cSmiod lance_copyfrombuf_contig(struct lance_softc *sc, void *to, int boff, int len)
6320ec3b04cSmiod {
6330ec3b04cSmiod 	uint8_t *buf = sc->sc_mem;
6340ec3b04cSmiod 
6350ec3b04cSmiod 	/*
6360ec3b04cSmiod 	 * Just call memcpy() to do the work.
6370ec3b04cSmiod 	 */
6380ec3b04cSmiod 	memcpy(to, buf + boff, len);
6390ec3b04cSmiod }
6400ec3b04cSmiod 
6410ec3b04cSmiod void
lance_zerobuf_contig(struct lance_softc * sc,int boff,int len)6420ec3b04cSmiod lance_zerobuf_contig(struct lance_softc *sc, int boff, int len)
6430ec3b04cSmiod {
6440ec3b04cSmiod 	uint8_t *buf = sc->sc_mem;
6450ec3b04cSmiod 
6460ec3b04cSmiod 	/*
6470ec3b04cSmiod 	 * Just let memset() do the work
6480ec3b04cSmiod 	 */
6490ec3b04cSmiod 	memset(buf + boff, 0, len);
6500ec3b04cSmiod }
6510ec3b04cSmiod 
6520ec3b04cSmiod #if 0
6530ec3b04cSmiod /*
6540ec3b04cSmiod  * Examples only; duplicate these and tweak (if necessary) in
6550ec3b04cSmiod  * machine-specific front-ends.
6560ec3b04cSmiod  */
6570ec3b04cSmiod 
6580ec3b04cSmiod /*
6590ec3b04cSmiod  * gap2: two bytes of data followed by two bytes of pad.
6600ec3b04cSmiod  *
6610ec3b04cSmiod  * Buffers must be 4-byte aligned.  The code doesn't worry about
6620ec3b04cSmiod  * doing an extra byte.
6630ec3b04cSmiod  */
6640ec3b04cSmiod 
6650ec3b04cSmiod void
6660ec3b04cSmiod lance_copytobuf_gap2(struct lance_softc *sc, void *fromv, int boff, int len)
6670ec3b04cSmiod {
6680ec3b04cSmiod 	volatile void *buf = sc->sc_mem;
6690ec3b04cSmiod 	void *from = fromv;
6700ec3b04cSmiod 	volatile uint16_t *bptr;
6710ec3b04cSmiod 
6720ec3b04cSmiod 	if (boff & 0x1) {
6730ec3b04cSmiod 		/* handle unaligned first byte */
6740ec3b04cSmiod 		bptr = ((volatile uint16_t *)buf) + (boff - 1);
6750ec3b04cSmiod 		*bptr = (*from++ << 8) | (*bptr & 0xff);
6760ec3b04cSmiod 		bptr += 2;
6770ec3b04cSmiod 		len--;
6780ec3b04cSmiod 	} else
6790ec3b04cSmiod 		bptr = ((volatile uint16_t *)buf) + boff;
6800ec3b04cSmiod 	while (len > 1) {
6810ec3b04cSmiod 		*bptr = (from[1] << 8) | (from[0] & 0xff);
6820ec3b04cSmiod 		bptr += 2;
6830ec3b04cSmiod 		from += 2;
6840ec3b04cSmiod 		len -= 2;
6850ec3b04cSmiod 	}
6860ec3b04cSmiod 	if (len == 1)
6870ec3b04cSmiod 		*bptr = (uint16_t)*from;
6880ec3b04cSmiod }
6890ec3b04cSmiod 
6900ec3b04cSmiod void
6910ec3b04cSmiod lance_copyfrombuf_gap2(struct lance_softc *sc, void *tov, int boff, int len)
6920ec3b04cSmiod {
6930ec3b04cSmiod 	volatile void *buf = sc->sc_mem;
6940ec3b04cSmiod 	void *to = tov;
6950ec3b04cSmiod 	volatile uint16_t *bptr;
6960ec3b04cSmiod 	uint16_t tmp;
6970ec3b04cSmiod 
6980ec3b04cSmiod 	if (boff & 0x1) {
6990ec3b04cSmiod 		/* handle unaligned first byte */
7000ec3b04cSmiod 		bptr = ((volatile uint16_t *)buf) + (boff - 1);
7010ec3b04cSmiod 		*to++ = (*bptr >> 8) & 0xff;
7020ec3b04cSmiod 		bptr += 2;
7030ec3b04cSmiod 		len--;
7040ec3b04cSmiod 	} else
7050ec3b04cSmiod 		bptr = ((volatile uint16_t *)buf) + boff;
7060ec3b04cSmiod 	while (len > 1) {
7070ec3b04cSmiod 		tmp = *bptr;
7080ec3b04cSmiod 		*to++ = tmp & 0xff;
7090ec3b04cSmiod 		*to++ = (tmp >> 8) & 0xff;
7100ec3b04cSmiod 		bptr += 2;
7110ec3b04cSmiod 		len -= 2;
7120ec3b04cSmiod 	}
7130ec3b04cSmiod 	if (len == 1)
7140ec3b04cSmiod 		*to = *bptr & 0xff;
7150ec3b04cSmiod }
7160ec3b04cSmiod 
7170ec3b04cSmiod void
7180ec3b04cSmiod lance_zerobuf_gap2(struct lance_softc *sc, int boff, int len)
7190ec3b04cSmiod {
7200ec3b04cSmiod 	volatile void *buf = sc->sc_mem;
7210ec3b04cSmiod 	volatile uint16_t *bptr;
7220ec3b04cSmiod 
7230ec3b04cSmiod 	if ((unsigned int)boff & 0x1) {
7240ec3b04cSmiod 		bptr = ((volatile uint16_t *)buf) + (boff - 1);
7250ec3b04cSmiod 		*bptr &= 0xff;
7260ec3b04cSmiod 		bptr += 2;
7270ec3b04cSmiod 		len--;
7280ec3b04cSmiod 	} else
7290ec3b04cSmiod 		bptr = ((volatile uint16_t *)buf) + boff;
7300ec3b04cSmiod 	while (len > 0) {
7310ec3b04cSmiod 		*bptr = 0;
7320ec3b04cSmiod 		bptr += 2;
7330ec3b04cSmiod 		len -= 2;
7340ec3b04cSmiod 	}
7350ec3b04cSmiod }
7360ec3b04cSmiod 
7370ec3b04cSmiod /*
7380ec3b04cSmiod  * gap16: 16 bytes of data followed by 16 bytes of pad.
7390ec3b04cSmiod  *
7400ec3b04cSmiod  * Buffers must be 32-byte aligned.
7410ec3b04cSmiod  */
7420ec3b04cSmiod 
7430ec3b04cSmiod void
7440ec3b04cSmiod lance_copytobuf_gap16(struct lance_softc *sc, void *fromv, int boff, int len)
7450ec3b04cSmiod {
7460ec3b04cSmiod 	volatile uint8_t *buf = sc->sc_mem;
7470ec3b04cSmiod 	void *from = fromv;
7480ec3b04cSmiod 	uint8_t *bptr;
7490ec3b04cSmiod 	int xfer;
7500ec3b04cSmiod 
7510ec3b04cSmiod 	bptr = buf + ((boff << 1) & ~0x1f);
7520ec3b04cSmiod 	boff &= 0xf;
7530ec3b04cSmiod 	xfer = min(len, 16 - boff);
7540ec3b04cSmiod 	while (len > 0) {
7550ec3b04cSmiod 		memcpy(bptr + boff, from, xfer);
7560ec3b04cSmiod 		from += xfer;
7570ec3b04cSmiod 		bptr += 32;
7580ec3b04cSmiod 		boff = 0;
7590ec3b04cSmiod 		len -= xfer;
7600ec3b04cSmiod 		xfer = min(len, 16);
7610ec3b04cSmiod 	}
7620ec3b04cSmiod }
7630ec3b04cSmiod 
7640ec3b04cSmiod void
7650ec3b04cSmiod lance_copyfrombuf_gap16(struct lance_softc *sc, void *tov, int boff, int len)
7660ec3b04cSmiod {
7670ec3b04cSmiod 	volatile uint8_t *buf = sc->sc_mem;
7680ec3b04cSmiod 	void *to = tov;
7690ec3b04cSmiod 	uint8_t *bptr;
7700ec3b04cSmiod 	int xfer;
7710ec3b04cSmiod 
7720ec3b04cSmiod 	bptr = buf + ((boff << 1) & ~0x1f);
7730ec3b04cSmiod 	boff &= 0xf;
7740ec3b04cSmiod 	xfer = min(len, 16 - boff);
7750ec3b04cSmiod 	while (len > 0) {
7760ec3b04cSmiod 		memcpy(to, bptr + boff, xfer);
7770ec3b04cSmiod 		to += xfer;
7780ec3b04cSmiod 		bptr += 32;
7790ec3b04cSmiod 		boff = 0;
7800ec3b04cSmiod 		len -= xfer;
7810ec3b04cSmiod 		xfer = min(len, 16);
7820ec3b04cSmiod 	}
7830ec3b04cSmiod }
7840ec3b04cSmiod 
7850ec3b04cSmiod void
7860ec3b04cSmiod lance_zerobuf_gap16(struct lance_softc *sc, int boff, int len)
7870ec3b04cSmiod {
7880ec3b04cSmiod 	volatile uint8_t *buf = sc->sc_mem;
7890ec3b04cSmiod 	uint8_t *bptr;
7900ec3b04cSmiod 	int xfer;
7910ec3b04cSmiod 
7920ec3b04cSmiod 	bptr = buf + ((boff << 1) & ~0x1f);
7930ec3b04cSmiod 	boff &= 0xf;
7940ec3b04cSmiod 	xfer = min(len, 16 - boff);
7950ec3b04cSmiod 	while (len > 0) {
7960ec3b04cSmiod 		memset(bptr + boff, 0, xfer);
7970ec3b04cSmiod 		bptr += 32;
7980ec3b04cSmiod 		boff = 0;
7990ec3b04cSmiod 		len -= xfer;
8000ec3b04cSmiod 		xfer = min(len, 16);
8010ec3b04cSmiod 	}
8020ec3b04cSmiod }
8030ec3b04cSmiod #endif /* Example only */
804