xref: /freebsd/sys/dev/le/lance.c (revision aa3860851b9f6a6002d135b1cac7736e0995eedc)
1a7ee7a7dSMarius Strobl /*	$NetBSD: lance.c,v 1.34 2005/12/24 20:27:30 perry Exp $	*/
2a7ee7a7dSMarius Strobl 
3a7ee7a7dSMarius Strobl /*-
4b61a5730SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
5718cf2ccSPedro F. Giffuni  *
6a7ee7a7dSMarius Strobl  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
7a7ee7a7dSMarius Strobl  * All rights reserved.
8a7ee7a7dSMarius Strobl  *
9a7ee7a7dSMarius Strobl  * This code is derived from software contributed to The NetBSD Foundation
10a7ee7a7dSMarius Strobl  * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
11a7ee7a7dSMarius Strobl  * Simulation Facility, NASA Ames Research Center.
12a7ee7a7dSMarius Strobl  *
13a7ee7a7dSMarius Strobl  * Redistribution and use in source and binary forms, with or without
14a7ee7a7dSMarius Strobl  * modification, are permitted provided that the following conditions
15a7ee7a7dSMarius Strobl  * are met:
16a7ee7a7dSMarius Strobl  * 1. Redistributions of source code must retain the above copyright
17a7ee7a7dSMarius Strobl  *    notice, this list of conditions and the following disclaimer.
18a7ee7a7dSMarius Strobl  * 2. Redistributions in binary form must reproduce the above copyright
19a7ee7a7dSMarius Strobl  *    notice, this list of conditions and the following disclaimer in the
20a7ee7a7dSMarius Strobl  *    documentation and/or other materials provided with the distribution.
21a7ee7a7dSMarius Strobl  *
22a7ee7a7dSMarius Strobl  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23a7ee7a7dSMarius Strobl  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24a7ee7a7dSMarius Strobl  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25a7ee7a7dSMarius Strobl  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26a7ee7a7dSMarius Strobl  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27a7ee7a7dSMarius Strobl  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28a7ee7a7dSMarius Strobl  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29a7ee7a7dSMarius Strobl  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30a7ee7a7dSMarius Strobl  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31a7ee7a7dSMarius Strobl  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32a7ee7a7dSMarius Strobl  * POSSIBILITY OF SUCH DAMAGE.
33a7ee7a7dSMarius Strobl  */
34a7ee7a7dSMarius Strobl 
35a7ee7a7dSMarius Strobl /*-
36a7ee7a7dSMarius Strobl  * Copyright (c) 1992, 1993
37a7ee7a7dSMarius Strobl  *	The Regents of the University of California.  All rights reserved.
38a7ee7a7dSMarius Strobl  *
39a7ee7a7dSMarius Strobl  * This code is derived from software contributed to Berkeley by
40a7ee7a7dSMarius Strobl  * Ralph Campbell and Rick Macklem.
41a7ee7a7dSMarius Strobl  *
42a7ee7a7dSMarius Strobl  * Redistribution and use in source and binary forms, with or without
43a7ee7a7dSMarius Strobl  * modification, are permitted provided that the following conditions
44a7ee7a7dSMarius Strobl  * are met:
45a7ee7a7dSMarius Strobl  * 1. Redistributions of source code must retain the above copyright
46a7ee7a7dSMarius Strobl  *    notice, this list of conditions and the following disclaimer.
47a7ee7a7dSMarius Strobl  * 2. Redistributions in binary form must reproduce the above copyright
48a7ee7a7dSMarius Strobl  *    notice, this list of conditions and the following disclaimer in the
49a7ee7a7dSMarius Strobl  *    documentation and/or other materials provided with the distribution.
50a7ee7a7dSMarius Strobl  * 3. Neither the name of the University nor the names of its contributors
51a7ee7a7dSMarius Strobl  *    may be used to endorse or promote products derived from this software
52a7ee7a7dSMarius Strobl  *    without specific prior written permission.
53a7ee7a7dSMarius Strobl  *
54a7ee7a7dSMarius Strobl  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55a7ee7a7dSMarius Strobl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56a7ee7a7dSMarius Strobl  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57a7ee7a7dSMarius Strobl  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58a7ee7a7dSMarius Strobl  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59a7ee7a7dSMarius Strobl  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60a7ee7a7dSMarius Strobl  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61a7ee7a7dSMarius Strobl  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62a7ee7a7dSMarius Strobl  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63a7ee7a7dSMarius Strobl  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64a7ee7a7dSMarius Strobl  * SUCH DAMAGE.
65a7ee7a7dSMarius Strobl  */
66a7ee7a7dSMarius Strobl 
67a7ee7a7dSMarius Strobl #include <sys/param.h>
68a7ee7a7dSMarius Strobl #include <sys/bus.h>
69a7ee7a7dSMarius Strobl #include <sys/endian.h>
70a7ee7a7dSMarius Strobl #include <sys/lock.h>
71fc64bae4SMarius Strobl #include <sys/kernel.h>
728ec07310SGleb Smirnoff #include <sys/malloc.h>
73a7ee7a7dSMarius Strobl #include <sys/mbuf.h>
74a7ee7a7dSMarius Strobl #include <sys/mutex.h>
75a7ee7a7dSMarius Strobl #include <sys/socket.h>
76a7ee7a7dSMarius Strobl #include <sys/sockio.h>
77a7ee7a7dSMarius Strobl 
78a7ee7a7dSMarius Strobl #include <net/ethernet.h>
79a7ee7a7dSMarius Strobl #include <net/if.h>
8076039bc8SGleb Smirnoff #include <net/if_var.h>
81a7ee7a7dSMarius Strobl #include <net/if_arp.h>
82a7ee7a7dSMarius Strobl #include <net/if_dl.h>
83a7ee7a7dSMarius Strobl #include <net/if_media.h>
84a7ee7a7dSMarius Strobl #include <net/if_types.h>
85a7ee7a7dSMarius Strobl #include <net/if_vlan_var.h>
86a7ee7a7dSMarius Strobl 
8760c430f5SMarius Strobl #include <machine/bus.h>
8860c430f5SMarius Strobl 
89a7ee7a7dSMarius Strobl #include <dev/le/lancereg.h>
90a7ee7a7dSMarius Strobl #include <dev/le/lancevar.h>
91a7ee7a7dSMarius Strobl 
92df40efe1SJustin Hibbits static void lance_start(if_t);
93a7ee7a7dSMarius Strobl static void lance_stop(struct lance_softc *);
94a7ee7a7dSMarius Strobl static void lance_init(void *);
95fc64bae4SMarius Strobl static void lance_watchdog(void *s);
96df40efe1SJustin Hibbits static int lance_mediachange(if_t);
97df40efe1SJustin Hibbits static void lance_mediastatus(if_t, struct ifmediareq *);
98df40efe1SJustin Hibbits static int lance_ioctl(if_t, u_long, caddr_t);
99a7ee7a7dSMarius Strobl 
100a7ee7a7dSMarius Strobl int
lance_config(struct lance_softc * sc,const char * name,int unit)101a7ee7a7dSMarius Strobl lance_config(struct lance_softc *sc, const char* name, int unit)
102a7ee7a7dSMarius Strobl {
103df40efe1SJustin Hibbits 	if_t ifp;
104a7ee7a7dSMarius Strobl 	int i, nbuf;
105a7ee7a7dSMarius Strobl 
106a7ee7a7dSMarius Strobl 	if (LE_LOCK_INITIALIZED(sc) == 0)
107a7ee7a7dSMarius Strobl 		return (ENXIO);
108a7ee7a7dSMarius Strobl 
109a7ee7a7dSMarius Strobl 	ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
110a7ee7a7dSMarius Strobl 
111fc64bae4SMarius Strobl 	callout_init_mtx(&sc->sc_wdog_ch, &sc->sc_mtx, 0);
112fc64bae4SMarius Strobl 
113a7ee7a7dSMarius Strobl 	/* Initialize ifnet structure. */
114df40efe1SJustin Hibbits 	if_setsoftc(ifp, sc);
115a7ee7a7dSMarius Strobl 	if_initname(ifp, name, unit);
116df40efe1SJustin Hibbits 	if_setstartfn(ifp, lance_start);
117df40efe1SJustin Hibbits 	if_setioctlfn(ifp, lance_ioctl);
118df40efe1SJustin Hibbits 	if_setinitfn(ifp, lance_init);
119df40efe1SJustin Hibbits 	if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
120a7ee7a7dSMarius Strobl #ifdef LANCE_REVC_BUG
121df40efe1SJustin Hibbits 	if_setflagsbit(ifp, 0, IFF_MULTICAST);
122a7ee7a7dSMarius Strobl #endif
123df40efe1SJustin Hibbits 	if_setbaudrate(ifp, IF_Mbps(10));
124df40efe1SJustin Hibbits 	if_setsendqlen(ifp, ifqmaxlen);
125df40efe1SJustin Hibbits 	if_setsendqready(ifp);
126a7ee7a7dSMarius Strobl 
127a7ee7a7dSMarius Strobl 	/* Initialize ifmedia structures. */
128a7ee7a7dSMarius Strobl 	ifmedia_init(&sc->sc_media, 0, lance_mediachange, lance_mediastatus);
129a7ee7a7dSMarius Strobl 	if (sc->sc_supmedia != NULL) {
130a7ee7a7dSMarius Strobl 		for (i = 0; i < sc->sc_nsupmedia; i++)
131a7ee7a7dSMarius Strobl 			ifmedia_add(&sc->sc_media, sc->sc_supmedia[i], 0, NULL);
132a7ee7a7dSMarius Strobl 		ifmedia_set(&sc->sc_media, sc->sc_defaultmedia);
133a7ee7a7dSMarius Strobl 	} else {
134a7ee7a7dSMarius Strobl 		ifmedia_add(&sc->sc_media,
135a7ee7a7dSMarius Strobl 		    IFM_MAKEWORD(IFM_ETHER, IFM_MANUAL, 0, 0), 0, NULL);
136a7ee7a7dSMarius Strobl 		ifmedia_set(&sc->sc_media,
137a7ee7a7dSMarius Strobl 		    IFM_MAKEWORD(IFM_ETHER, IFM_MANUAL, 0, 0));
138a7ee7a7dSMarius Strobl 	}
139a7ee7a7dSMarius Strobl 
140a7ee7a7dSMarius Strobl 	switch (sc->sc_memsize) {
141a7ee7a7dSMarius Strobl 	case 8192:
142a7ee7a7dSMarius Strobl 		sc->sc_nrbuf = 4;
143a7ee7a7dSMarius Strobl 		sc->sc_ntbuf = 1;
144a7ee7a7dSMarius Strobl 		break;
145a7ee7a7dSMarius Strobl 	case 16384:
146a7ee7a7dSMarius Strobl 		sc->sc_nrbuf = 8;
147a7ee7a7dSMarius Strobl 		sc->sc_ntbuf = 2;
148a7ee7a7dSMarius Strobl 		break;
149a7ee7a7dSMarius Strobl 	case 32768:
150a7ee7a7dSMarius Strobl 		sc->sc_nrbuf = 16;
151a7ee7a7dSMarius Strobl 		sc->sc_ntbuf = 4;
152a7ee7a7dSMarius Strobl 		break;
153a7ee7a7dSMarius Strobl 	case 65536:
154a7ee7a7dSMarius Strobl 		sc->sc_nrbuf = 32;
155a7ee7a7dSMarius Strobl 		sc->sc_ntbuf = 8;
156a7ee7a7dSMarius Strobl 		break;
157a7ee7a7dSMarius Strobl 	case 131072:
158a7ee7a7dSMarius Strobl 		sc->sc_nrbuf = 64;
159a7ee7a7dSMarius Strobl 		sc->sc_ntbuf = 16;
160a7ee7a7dSMarius Strobl 		break;
161a7ee7a7dSMarius Strobl 	case 262144:
162a7ee7a7dSMarius Strobl 		sc->sc_nrbuf = 128;
163a7ee7a7dSMarius Strobl 		sc->sc_ntbuf = 32;
164a7ee7a7dSMarius Strobl 		break;
165a7ee7a7dSMarius Strobl 	default:
166a7ee7a7dSMarius Strobl 		/* weird memory size; cope with it */
167a7ee7a7dSMarius Strobl 		nbuf = sc->sc_memsize / LEBLEN;
168a7ee7a7dSMarius Strobl 		sc->sc_ntbuf = nbuf / 5;
169a7ee7a7dSMarius Strobl 		sc->sc_nrbuf = nbuf - sc->sc_ntbuf;
170a7ee7a7dSMarius Strobl 	}
171a7ee7a7dSMarius Strobl 
172a7ee7a7dSMarius Strobl 	if_printf(ifp, "%d receive buffers, %d transmit buffers\n",
173a7ee7a7dSMarius Strobl 	    sc->sc_nrbuf, sc->sc_ntbuf);
174a7ee7a7dSMarius Strobl 
175a7ee7a7dSMarius Strobl 	/* Make sure the chip is stopped. */
176a7ee7a7dSMarius Strobl 	LE_LOCK(sc);
177a7ee7a7dSMarius Strobl 	lance_stop(sc);
178a7ee7a7dSMarius Strobl 	LE_UNLOCK(sc);
179a7ee7a7dSMarius Strobl 
180a7ee7a7dSMarius Strobl 	return (0);
181a7ee7a7dSMarius Strobl }
182a7ee7a7dSMarius Strobl 
183a7ee7a7dSMarius Strobl void
lance_attach(struct lance_softc * sc)184a7ee7a7dSMarius Strobl lance_attach(struct lance_softc *sc)
185a7ee7a7dSMarius Strobl {
186df40efe1SJustin Hibbits 	if_t ifp = sc->sc_ifp;
187a7ee7a7dSMarius Strobl 
188a7ee7a7dSMarius Strobl 	/* Attach the interface. */
189a7ee7a7dSMarius Strobl 	ether_ifattach(ifp, sc->sc_enaddr);
190a7ee7a7dSMarius Strobl 
191a7ee7a7dSMarius Strobl 	/* Claim 802.1q capability. */
192df40efe1SJustin Hibbits 	if_setifheaderlen(ifp, sizeof(struct ether_vlan_header));
193df40efe1SJustin Hibbits 	if_setcapabilitiesbit(ifp, IFCAP_VLAN_MTU, 0);
194df40efe1SJustin Hibbits 	if_setcapenablebit(ifp, IFCAP_VLAN_MTU, 0);
195*b95d2237SBrooks Davis 
196*b95d2237SBrooks Davis 	gone_in(15, "le: 10/100 NIC no longer needed for Qemu/MIPS");
197a7ee7a7dSMarius Strobl }
198a7ee7a7dSMarius Strobl 
199a7ee7a7dSMarius Strobl void
lance_detach(struct lance_softc * sc)200a7ee7a7dSMarius Strobl lance_detach(struct lance_softc *sc)
201a7ee7a7dSMarius Strobl {
202df40efe1SJustin Hibbits 	if_t ifp = sc->sc_ifp;
203a7ee7a7dSMarius Strobl 
204a7ee7a7dSMarius Strobl 	LE_LOCK(sc);
205a7ee7a7dSMarius Strobl 	lance_stop(sc);
206a7ee7a7dSMarius Strobl 	LE_UNLOCK(sc);
207fc64bae4SMarius Strobl 	callout_drain(&sc->sc_wdog_ch);
208a7ee7a7dSMarius Strobl 	ether_ifdetach(ifp);
209a7ee7a7dSMarius Strobl 	if_free(ifp);
210a7ee7a7dSMarius Strobl }
211a7ee7a7dSMarius Strobl 
212a7ee7a7dSMarius Strobl void
lance_suspend(struct lance_softc * sc)213a7ee7a7dSMarius Strobl lance_suspend(struct lance_softc *sc)
214a7ee7a7dSMarius Strobl {
215a7ee7a7dSMarius Strobl 
216a7ee7a7dSMarius Strobl 	LE_LOCK(sc);
217a7ee7a7dSMarius Strobl 	lance_stop(sc);
218a7ee7a7dSMarius Strobl 	LE_UNLOCK(sc);
219a7ee7a7dSMarius Strobl }
220a7ee7a7dSMarius Strobl 
221a7ee7a7dSMarius Strobl void
lance_resume(struct lance_softc * sc)222a7ee7a7dSMarius Strobl lance_resume(struct lance_softc *sc)
223a7ee7a7dSMarius Strobl {
224a7ee7a7dSMarius Strobl 
225a7ee7a7dSMarius Strobl 	LE_LOCK(sc);
226df40efe1SJustin Hibbits 	if (if_getflags(sc->sc_ifp) & IFF_UP)
227a7ee7a7dSMarius Strobl 		lance_init_locked(sc);
228a7ee7a7dSMarius Strobl 	LE_UNLOCK(sc);
229a7ee7a7dSMarius Strobl }
230a7ee7a7dSMarius Strobl 
231a7ee7a7dSMarius Strobl static void
lance_start(if_t ifp)232df40efe1SJustin Hibbits lance_start(if_t ifp)
233a7ee7a7dSMarius Strobl {
234df40efe1SJustin Hibbits 	struct lance_softc *sc = if_getsoftc(ifp);
235a7ee7a7dSMarius Strobl 
236a7ee7a7dSMarius Strobl 	LE_LOCK(sc);
237a7ee7a7dSMarius Strobl 	(*sc->sc_start_locked)(sc);
238a7ee7a7dSMarius Strobl 	LE_UNLOCK(sc);
239a7ee7a7dSMarius Strobl }
240a7ee7a7dSMarius Strobl 
241a7ee7a7dSMarius Strobl static void
lance_stop(struct lance_softc * sc)242a7ee7a7dSMarius Strobl lance_stop(struct lance_softc *sc)
243a7ee7a7dSMarius Strobl {
244df40efe1SJustin Hibbits 	if_t ifp = sc->sc_ifp;
245a7ee7a7dSMarius Strobl 
246a7ee7a7dSMarius Strobl 	LE_LOCK_ASSERT(sc, MA_OWNED);
247a7ee7a7dSMarius Strobl 
248a7ee7a7dSMarius Strobl 	/*
249a7ee7a7dSMarius Strobl 	 * Mark the interface down and cancel the watchdog timer.
250a7ee7a7dSMarius Strobl 	 */
251df40efe1SJustin Hibbits 	if_setdrvflagbits(ifp, 0, (IFF_DRV_RUNNING | IFF_DRV_OACTIVE));
252fc64bae4SMarius Strobl 	callout_stop(&sc->sc_wdog_ch);
253fc64bae4SMarius Strobl 	sc->sc_wdog_timer = 0;
254a7ee7a7dSMarius Strobl 
255a7ee7a7dSMarius Strobl 	(*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_STOP);
256a7ee7a7dSMarius Strobl }
257a7ee7a7dSMarius Strobl 
258a7ee7a7dSMarius Strobl static void
lance_init(void * xsc)259a7ee7a7dSMarius Strobl lance_init(void *xsc)
260a7ee7a7dSMarius Strobl {
261a7ee7a7dSMarius Strobl 	struct lance_softc *sc = (struct lance_softc *)xsc;
262a7ee7a7dSMarius Strobl 
263a7ee7a7dSMarius Strobl 	LE_LOCK(sc);
264a7ee7a7dSMarius Strobl 	lance_init_locked(sc);
265a7ee7a7dSMarius Strobl 	LE_UNLOCK(sc);
266a7ee7a7dSMarius Strobl }
267a7ee7a7dSMarius Strobl 
268a7ee7a7dSMarius Strobl /*
269a7ee7a7dSMarius Strobl  * Initialization of interface; set up initialization block
270a7ee7a7dSMarius Strobl  * and transmit/receive descriptor rings.
271a7ee7a7dSMarius Strobl  */
272a7ee7a7dSMarius Strobl void
lance_init_locked(struct lance_softc * sc)273a7ee7a7dSMarius Strobl lance_init_locked(struct lance_softc *sc)
274a7ee7a7dSMarius Strobl {
275df40efe1SJustin Hibbits 	if_t ifp = sc->sc_ifp;
276a7ee7a7dSMarius Strobl 	u_long a;
277a7ee7a7dSMarius Strobl 	int timo;
278a7ee7a7dSMarius Strobl 
279a7ee7a7dSMarius Strobl 	LE_LOCK_ASSERT(sc, MA_OWNED);
280a7ee7a7dSMarius Strobl 
281a7ee7a7dSMarius Strobl 	(*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_STOP);
282a7ee7a7dSMarius Strobl 	DELAY(100);
283a7ee7a7dSMarius Strobl 
284a7ee7a7dSMarius Strobl 	/* Newer LANCE chips have a reset register. */
285a7ee7a7dSMarius Strobl 	if (sc->sc_hwreset)
286a7ee7a7dSMarius Strobl 		(*sc->sc_hwreset)(sc);
287a7ee7a7dSMarius Strobl 
288a7ee7a7dSMarius Strobl 	/* Set the correct byte swapping mode, etc. */
289a7ee7a7dSMarius Strobl 	(*sc->sc_wrcsr)(sc, LE_CSR3, sc->sc_conf3);
290a7ee7a7dSMarius Strobl 
29117792f45SMarius Strobl 	/* Set the current media. This may require the chip to be stopped. */
29217792f45SMarius Strobl 	if (sc->sc_mediachange)
29317792f45SMarius Strobl 		(void)(*sc->sc_mediachange)(sc);
29417792f45SMarius Strobl 
295a7ee7a7dSMarius Strobl 	/*
296a7ee7a7dSMarius Strobl 	 * Update our private copy of the Ethernet address.
297a7ee7a7dSMarius Strobl 	 * We NEED the copy so we can ensure its alignment!
298a7ee7a7dSMarius Strobl 	 */
299df40efe1SJustin Hibbits 	memcpy(sc->sc_enaddr, if_getlladdr(ifp), ETHER_ADDR_LEN);
300a7ee7a7dSMarius Strobl 
301a7ee7a7dSMarius Strobl 	/* Set up LANCE init block. */
302a7ee7a7dSMarius Strobl 	(*sc->sc_meminit)(sc);
303a7ee7a7dSMarius Strobl 
304a7ee7a7dSMarius Strobl 	/* Give LANCE the physical address of its init block. */
305a7ee7a7dSMarius Strobl 	a = sc->sc_addr + LE_INITADDR(sc);
306a7ee7a7dSMarius Strobl 	(*sc->sc_wrcsr)(sc, LE_CSR1, a & 0xffff);
307a7ee7a7dSMarius Strobl 	(*sc->sc_wrcsr)(sc, LE_CSR2, a >> 16);
308a7ee7a7dSMarius Strobl 
309a7ee7a7dSMarius Strobl 	/* Try to initialize the LANCE. */
310a7ee7a7dSMarius Strobl 	DELAY(100);
311a7ee7a7dSMarius Strobl 	(*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INIT);
312a7ee7a7dSMarius Strobl 
313a7ee7a7dSMarius Strobl 	/* Wait for initialization to finish. */
314a7ee7a7dSMarius Strobl 	for (timo = 100000; timo; timo--)
315a7ee7a7dSMarius Strobl 		if ((*sc->sc_rdcsr)(sc, LE_CSR0) & LE_C0_IDON)
316a7ee7a7dSMarius Strobl 			break;
317a7ee7a7dSMarius Strobl 
318a7ee7a7dSMarius Strobl 	if ((*sc->sc_rdcsr)(sc, LE_CSR0) & LE_C0_IDON) {
319a7ee7a7dSMarius Strobl 		/* Start the LANCE. */
320a7ee7a7dSMarius Strobl 		(*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA | LE_C0_STRT);
321df40efe1SJustin Hibbits 		if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
322df40efe1SJustin Hibbits 		if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
323fc64bae4SMarius Strobl 		sc->sc_wdog_timer = 0;
324fc64bae4SMarius Strobl 		callout_reset(&sc->sc_wdog_ch, hz, lance_watchdog, sc);
325a7ee7a7dSMarius Strobl 		(*sc->sc_start_locked)(sc);
326a7ee7a7dSMarius Strobl 	} else
327a7ee7a7dSMarius Strobl 		if_printf(ifp, "controller failed to initialize\n");
328a7ee7a7dSMarius Strobl 
329a7ee7a7dSMarius Strobl 	if (sc->sc_hwinit)
330a7ee7a7dSMarius Strobl 		(*sc->sc_hwinit)(sc);
331a7ee7a7dSMarius Strobl }
332a7ee7a7dSMarius Strobl 
333a7ee7a7dSMarius Strobl /*
334a7ee7a7dSMarius Strobl  * Routine to copy from mbuf chain to transmit buffer in
335a7ee7a7dSMarius Strobl  * network buffer memory.
336a7ee7a7dSMarius Strobl  */
337a7ee7a7dSMarius Strobl int
lance_put(struct lance_softc * sc,int boff,struct mbuf * m)338a7ee7a7dSMarius Strobl lance_put(struct lance_softc *sc, int boff, struct mbuf *m)
339a7ee7a7dSMarius Strobl {
340a7ee7a7dSMarius Strobl 	struct mbuf *n;
341a7ee7a7dSMarius Strobl 	int len, tlen = 0;
342a7ee7a7dSMarius Strobl 
343a7ee7a7dSMarius Strobl 	LE_LOCK_ASSERT(sc, MA_OWNED);
344a7ee7a7dSMarius Strobl 
345a7ee7a7dSMarius Strobl 	for (; m; m = n) {
346a7ee7a7dSMarius Strobl 		len = m->m_len;
347a7ee7a7dSMarius Strobl 		if (len == 0) {
348a7ee7a7dSMarius Strobl 			n = m_free(m);
349a7ee7a7dSMarius Strobl 			m = NULL;
350a7ee7a7dSMarius Strobl 			continue;
351a7ee7a7dSMarius Strobl 		}
352a7ee7a7dSMarius Strobl 		(*sc->sc_copytobuf)(sc, mtod(m, caddr_t), boff, len);
353a7ee7a7dSMarius Strobl 		boff += len;
354a7ee7a7dSMarius Strobl 		tlen += len;
355a7ee7a7dSMarius Strobl 		n = m_free(m);
356a7ee7a7dSMarius Strobl 		m = NULL;
357a7ee7a7dSMarius Strobl 	}
358a7ee7a7dSMarius Strobl 	if (tlen < LEMINSIZE) {
359a7ee7a7dSMarius Strobl 		(*sc->sc_zerobuf)(sc, boff, LEMINSIZE - tlen);
360a7ee7a7dSMarius Strobl 		tlen = LEMINSIZE;
361a7ee7a7dSMarius Strobl 	}
362a7ee7a7dSMarius Strobl 	return (tlen);
363a7ee7a7dSMarius Strobl }
364a7ee7a7dSMarius Strobl 
365a7ee7a7dSMarius Strobl /*
366a7ee7a7dSMarius Strobl  * Pull data off an interface.
367a7ee7a7dSMarius Strobl  * Len is length of data, with local net header stripped.
368a7ee7a7dSMarius Strobl  * We copy the data into mbufs.  When full cluster sized units are present
369a7ee7a7dSMarius Strobl  * we copy into clusters.
370a7ee7a7dSMarius Strobl  */
37160c430f5SMarius Strobl struct mbuf *
lance_get(struct lance_softc * sc,int boff,int totlen)372a7ee7a7dSMarius Strobl lance_get(struct lance_softc *sc, int boff, int totlen)
373a7ee7a7dSMarius Strobl {
374df40efe1SJustin Hibbits 	if_t ifp = sc->sc_ifp;
375a7ee7a7dSMarius Strobl 	struct mbuf *m, *m0, *newm;
376a7ee7a7dSMarius Strobl 	caddr_t newdata;
377a7ee7a7dSMarius Strobl 	int len;
378a7ee7a7dSMarius Strobl 
37960c430f5SMarius Strobl 	if (totlen <= ETHER_HDR_LEN || totlen > LEBLEN - ETHER_CRC_LEN) {
38060c430f5SMarius Strobl #ifdef LEDEBUG
38160c430f5SMarius Strobl 		if_printf(ifp, "invalid packet size %d; dropping\n", totlen);
38260c430f5SMarius Strobl #endif
38360c430f5SMarius Strobl 		return (NULL);
38460c430f5SMarius Strobl 	}
38560c430f5SMarius Strobl 
386c6499eccSGleb Smirnoff 	MGETHDR(m0, M_NOWAIT, MT_DATA);
38760c430f5SMarius Strobl 	if (m0 == NULL)
38860c430f5SMarius Strobl 		return (NULL);
389a7ee7a7dSMarius Strobl 	m0->m_pkthdr.rcvif = ifp;
390a7ee7a7dSMarius Strobl 	m0->m_pkthdr.len = totlen;
391a7ee7a7dSMarius Strobl 	len = MHLEN;
392a7ee7a7dSMarius Strobl 	m = m0;
393a7ee7a7dSMarius Strobl 
394a7ee7a7dSMarius Strobl 	while (totlen > 0) {
395a7ee7a7dSMarius Strobl 		if (totlen >= MINCLSIZE) {
3962a8c860fSRobert Watson 			if (!(MCLGET(m, M_NOWAIT)))
397a7ee7a7dSMarius Strobl 				goto bad;
398a7ee7a7dSMarius Strobl 			len = MCLBYTES;
399a7ee7a7dSMarius Strobl 		}
400a7ee7a7dSMarius Strobl 
401a7ee7a7dSMarius Strobl 		if (m == m0) {
402a7ee7a7dSMarius Strobl 			newdata = (caddr_t)
403a7ee7a7dSMarius Strobl 			    ALIGN(m->m_data + ETHER_HDR_LEN) - ETHER_HDR_LEN;
404a7ee7a7dSMarius Strobl 			len -= newdata - m->m_data;
405a7ee7a7dSMarius Strobl 			m->m_data = newdata;
406a7ee7a7dSMarius Strobl 		}
407a7ee7a7dSMarius Strobl 
408a7ee7a7dSMarius Strobl 		m->m_len = len = min(totlen, len);
409a7ee7a7dSMarius Strobl 		(*sc->sc_copyfrombuf)(sc, mtod(m, caddr_t), boff, len);
410a7ee7a7dSMarius Strobl 		boff += len;
411a7ee7a7dSMarius Strobl 
412a7ee7a7dSMarius Strobl 		totlen -= len;
413a7ee7a7dSMarius Strobl 		if (totlen > 0) {
414c6499eccSGleb Smirnoff 			MGET(newm, M_NOWAIT, MT_DATA);
4154d24901aSPedro F. Giffuni 			if (newm == NULL)
416a7ee7a7dSMarius Strobl 				goto bad;
417a7ee7a7dSMarius Strobl 			len = MLEN;
418a7ee7a7dSMarius Strobl 			m = m->m_next = newm;
419a7ee7a7dSMarius Strobl 		}
420a7ee7a7dSMarius Strobl 	}
421a7ee7a7dSMarius Strobl 
422a7ee7a7dSMarius Strobl 	return (m0);
423a7ee7a7dSMarius Strobl 
424a7ee7a7dSMarius Strobl  bad:
425a7ee7a7dSMarius Strobl 	m_freem(m0);
42660c430f5SMarius Strobl 	return (NULL);
427a7ee7a7dSMarius Strobl }
428a7ee7a7dSMarius Strobl 
429a7ee7a7dSMarius Strobl static void
lance_watchdog(void * xsc)430fc64bae4SMarius Strobl lance_watchdog(void *xsc)
431a7ee7a7dSMarius Strobl {
432fc64bae4SMarius Strobl 	struct lance_softc *sc = (struct lance_softc *)xsc;
433df40efe1SJustin Hibbits 	if_t ifp = sc->sc_ifp;
434a7ee7a7dSMarius Strobl 
435fc64bae4SMarius Strobl 	LE_LOCK_ASSERT(sc, MA_OWNED);
436fc64bae4SMarius Strobl 
437fc64bae4SMarius Strobl 	if (sc->sc_wdog_timer == 0 || --sc->sc_wdog_timer != 0) {
438fc64bae4SMarius Strobl 		callout_reset(&sc->sc_wdog_ch, hz, lance_watchdog, sc);
439fc64bae4SMarius Strobl 		return;
440fc64bae4SMarius Strobl 	}
441fc64bae4SMarius Strobl 
442a7ee7a7dSMarius Strobl 	if_printf(ifp, "device timeout\n");
443c8dfaf38SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
444a7ee7a7dSMarius Strobl 	lance_init_locked(sc);
445a7ee7a7dSMarius Strobl }
446a7ee7a7dSMarius Strobl 
447a7ee7a7dSMarius Strobl static int
lance_mediachange(if_t ifp)448df40efe1SJustin Hibbits lance_mediachange(if_t ifp)
449a7ee7a7dSMarius Strobl {
450df40efe1SJustin Hibbits 	struct lance_softc *sc = if_getsoftc(ifp);
451a7ee7a7dSMarius Strobl 
452a7ee7a7dSMarius Strobl 	if (sc->sc_mediachange) {
45317792f45SMarius Strobl 		/*
45417792f45SMarius Strobl 		 * For setting the port in LE_CSR15 the PCnet chips must
45517792f45SMarius Strobl 		 * be powered down or stopped and unlike documented may
45617792f45SMarius Strobl 		 * not take effect without an initialization. So don't
45717792f45SMarius Strobl 		 * invoke (*sc_mediachange) directly here but go through
45817792f45SMarius Strobl 		 * lance_init_locked().
45917792f45SMarius Strobl 		 */
460a7ee7a7dSMarius Strobl 		LE_LOCK(sc);
46117792f45SMarius Strobl 		lance_stop(sc);
46217792f45SMarius Strobl 		lance_init_locked(sc);
463df40efe1SJustin Hibbits 		if (!if_sendq_empty(ifp))
46417792f45SMarius Strobl 			(*sc->sc_start_locked)(sc);
465a7ee7a7dSMarius Strobl 		LE_UNLOCK(sc);
466a7ee7a7dSMarius Strobl 	}
467a7ee7a7dSMarius Strobl 	return (0);
468a7ee7a7dSMarius Strobl }
469a7ee7a7dSMarius Strobl 
470a7ee7a7dSMarius Strobl static void
lance_mediastatus(if_t ifp,struct ifmediareq * ifmr)471df40efe1SJustin Hibbits lance_mediastatus(if_t ifp, struct ifmediareq *ifmr)
472a7ee7a7dSMarius Strobl {
473df40efe1SJustin Hibbits 	struct lance_softc *sc = if_getsoftc(ifp);
474a7ee7a7dSMarius Strobl 
475a7ee7a7dSMarius Strobl 	LE_LOCK(sc);
476df40efe1SJustin Hibbits 	if (!(if_getflags(ifp) & IFF_UP)) {
477a7ee7a7dSMarius Strobl 		LE_UNLOCK(sc);
478a7ee7a7dSMarius Strobl 		return;
479a7ee7a7dSMarius Strobl 	}
480a7ee7a7dSMarius Strobl 
481a7ee7a7dSMarius Strobl 	ifmr->ifm_status = IFM_AVALID;
482a7ee7a7dSMarius Strobl 	if (sc->sc_flags & LE_CARRIER)
483a7ee7a7dSMarius Strobl 		ifmr->ifm_status |= IFM_ACTIVE;
484a7ee7a7dSMarius Strobl 
485a7ee7a7dSMarius Strobl 	if (sc->sc_mediastatus)
486a7ee7a7dSMarius Strobl 		(*sc->sc_mediastatus)(sc, ifmr);
487a7ee7a7dSMarius Strobl 	LE_UNLOCK(sc);
488a7ee7a7dSMarius Strobl }
489a7ee7a7dSMarius Strobl 
490a7ee7a7dSMarius Strobl /*
491a7ee7a7dSMarius Strobl  * Process an ioctl request.
492a7ee7a7dSMarius Strobl  */
493a7ee7a7dSMarius Strobl static int
lance_ioctl(if_t ifp,u_long cmd,caddr_t data)494df40efe1SJustin Hibbits lance_ioctl(if_t ifp, u_long cmd, caddr_t data)
495a7ee7a7dSMarius Strobl {
496df40efe1SJustin Hibbits 	struct lance_softc *sc = if_getsoftc(ifp);
497a7ee7a7dSMarius Strobl 	struct ifreq *ifr = (struct ifreq *)data;
498a7ee7a7dSMarius Strobl 	int error = 0;
499a7ee7a7dSMarius Strobl 
500a7ee7a7dSMarius Strobl 	switch (cmd) {
501a7ee7a7dSMarius Strobl 	case SIOCSIFFLAGS:
502a7ee7a7dSMarius Strobl 		LE_LOCK(sc);
503df40efe1SJustin Hibbits 		if (if_getflags(ifp) & IFF_PROMISC) {
504a7ee7a7dSMarius Strobl 			if (!(sc->sc_flags & LE_PROMISC)) {
505a7ee7a7dSMarius Strobl 				sc->sc_flags |= LE_PROMISC;
506a7ee7a7dSMarius Strobl 				lance_init_locked(sc);
507a7ee7a7dSMarius Strobl 			}
508a7ee7a7dSMarius Strobl 		} else if (sc->sc_flags & LE_PROMISC) {
509a7ee7a7dSMarius Strobl 			sc->sc_flags &= ~LE_PROMISC;
510a7ee7a7dSMarius Strobl 			lance_init_locked(sc);
511a7ee7a7dSMarius Strobl 		}
512a7ee7a7dSMarius Strobl 
513df40efe1SJustin Hibbits 		if ((if_getflags(ifp) & IFF_ALLMULTI) &&
514a7ee7a7dSMarius Strobl 		    !(sc->sc_flags & LE_ALLMULTI)) {
515a7ee7a7dSMarius Strobl 			sc->sc_flags |= LE_ALLMULTI;
516a7ee7a7dSMarius Strobl 			lance_init_locked(sc);
517df40efe1SJustin Hibbits 		} else if (!(if_getflags(ifp) & IFF_ALLMULTI) &&
518a7ee7a7dSMarius Strobl 		    (sc->sc_flags & LE_ALLMULTI)) {
519a7ee7a7dSMarius Strobl 			sc->sc_flags &= ~LE_ALLMULTI;
520a7ee7a7dSMarius Strobl 			lance_init_locked(sc);
521a7ee7a7dSMarius Strobl 		}
522a7ee7a7dSMarius Strobl 
523df40efe1SJustin Hibbits 		if (!(if_getflags(ifp) & IFF_UP) &&
524df40efe1SJustin Hibbits 		    if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
525a7ee7a7dSMarius Strobl 			/*
526a7ee7a7dSMarius Strobl 			 * If interface is marked down and it is running, then
527a7ee7a7dSMarius Strobl 			 * stop it.
528a7ee7a7dSMarius Strobl 			 */
529a7ee7a7dSMarius Strobl 			lance_stop(sc);
530df40efe1SJustin Hibbits 		} else if (if_getflags(ifp) & IFF_UP &&
531df40efe1SJustin Hibbits 		    !(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
532a7ee7a7dSMarius Strobl 			/*
533a7ee7a7dSMarius Strobl 			 * If interface is marked up and it is stopped, then
534a7ee7a7dSMarius Strobl 			 * start it.
535a7ee7a7dSMarius Strobl 			 */
536a7ee7a7dSMarius Strobl 			lance_init_locked(sc);
537a7ee7a7dSMarius Strobl 		}
538a7ee7a7dSMarius Strobl #ifdef LEDEBUG
539df40efe1SJustin Hibbits 		if (if_getflags(ifp) & IFF_DEBUG)
540a7ee7a7dSMarius Strobl 			sc->sc_flags |= LE_DEBUG;
541a7ee7a7dSMarius Strobl 		else
542a7ee7a7dSMarius Strobl 			sc->sc_flags &= ~LE_DEBUG;
543a7ee7a7dSMarius Strobl #endif
544a7ee7a7dSMarius Strobl 		LE_UNLOCK(sc);
545a7ee7a7dSMarius Strobl 		break;
546a7ee7a7dSMarius Strobl 
547a7ee7a7dSMarius Strobl 	case SIOCADDMULTI:
548a7ee7a7dSMarius Strobl 	case SIOCDELMULTI:
549a7ee7a7dSMarius Strobl 		/*
550a7ee7a7dSMarius Strobl 		 * Multicast list has changed; set the hardware filter
551a7ee7a7dSMarius Strobl 		 * accordingly.
552a7ee7a7dSMarius Strobl 		 */
553a7ee7a7dSMarius Strobl 		LE_LOCK(sc);
554df40efe1SJustin Hibbits 		if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
555a7ee7a7dSMarius Strobl 			lance_init_locked(sc);
556a7ee7a7dSMarius Strobl 		LE_UNLOCK(sc);
557a7ee7a7dSMarius Strobl 		break;
558a7ee7a7dSMarius Strobl 
559a7ee7a7dSMarius Strobl 	case SIOCGIFMEDIA:
560a7ee7a7dSMarius Strobl 	case SIOCSIFMEDIA:
561a7ee7a7dSMarius Strobl 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
562a7ee7a7dSMarius Strobl 		break;
563a7ee7a7dSMarius Strobl 
564a7ee7a7dSMarius Strobl 	default:
565a7ee7a7dSMarius Strobl 		error = ether_ioctl(ifp, cmd, data);
566a7ee7a7dSMarius Strobl 		break;
567a7ee7a7dSMarius Strobl 	}
568a7ee7a7dSMarius Strobl 
569a7ee7a7dSMarius Strobl 	return (error);
570a7ee7a7dSMarius Strobl }
571a7ee7a7dSMarius Strobl 
572208af059SGleb Smirnoff struct lance_hash_maddr_ctx {
573208af059SGleb Smirnoff 	struct lance_softc *sc;
574208af059SGleb Smirnoff 	uint16_t *af;
575208af059SGleb Smirnoff };
576208af059SGleb Smirnoff 
577208af059SGleb Smirnoff static u_int
lance_hash_maddr(void * arg,struct sockaddr_dl * sdl,u_int cnt)578208af059SGleb Smirnoff lance_hash_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
579208af059SGleb Smirnoff {
580208af059SGleb Smirnoff 	struct lance_hash_maddr_ctx *ctx = arg;
581208af059SGleb Smirnoff 	struct lance_softc *sc = ctx->sc;
582208af059SGleb Smirnoff 	uint32_t crc;
583208af059SGleb Smirnoff 
584208af059SGleb Smirnoff 	crc = ether_crc32_le(LLADDR(sdl), ETHER_ADDR_LEN);
585208af059SGleb Smirnoff 	/* Just want the 6 most significant bits. */
586208af059SGleb Smirnoff 	crc >>= 26;
587208af059SGleb Smirnoff 	/* Set the corresponding bit in the filter. */
588208af059SGleb Smirnoff 	ctx->af[crc >> 4] |= LE_HTOLE16(1 << (crc & 0xf));
589208af059SGleb Smirnoff 
590208af059SGleb Smirnoff 	return (1);
591208af059SGleb Smirnoff }
592208af059SGleb Smirnoff 
593a7ee7a7dSMarius Strobl /*
594a7ee7a7dSMarius Strobl  * Set up the logical address filter.
595a7ee7a7dSMarius Strobl  */
596a7ee7a7dSMarius Strobl void
lance_setladrf(struct lance_softc * sc,uint16_t * af)597a7ee7a7dSMarius Strobl lance_setladrf(struct lance_softc *sc, uint16_t *af)
598a7ee7a7dSMarius Strobl {
599df40efe1SJustin Hibbits 	if_t ifp = sc->sc_ifp;
600208af059SGleb Smirnoff 	struct lance_hash_maddr_ctx ctx = { sc, af };
601a7ee7a7dSMarius Strobl 
602a7ee7a7dSMarius Strobl 	/*
603a7ee7a7dSMarius Strobl 	 * Set up multicast address filter by passing all multicast addresses
604a7ee7a7dSMarius Strobl 	 * through a crc generator, and then using the high order 6 bits as an
605a7ee7a7dSMarius Strobl 	 * index into the 64 bit logical address filter.  The high order bit
606a7ee7a7dSMarius Strobl 	 * selects the word, while the rest of the bits select the bit within
607a7ee7a7dSMarius Strobl 	 * the word.
608a7ee7a7dSMarius Strobl 	 */
609a7ee7a7dSMarius Strobl 
610df40efe1SJustin Hibbits 	if (if_getflags(ifp) & IFF_PROMISC || sc->sc_flags & LE_ALLMULTI) {
611a7ee7a7dSMarius Strobl 		af[0] = af[1] = af[2] = af[3] = 0xffff;
612a7ee7a7dSMarius Strobl 		return;
613a7ee7a7dSMarius Strobl 	}
614a7ee7a7dSMarius Strobl 
615a7ee7a7dSMarius Strobl 	af[0] = af[1] = af[2] = af[3] = 0x0000;
616208af059SGleb Smirnoff 	if_foreach_llmaddr(ifp, lance_hash_maddr, &ctx);
617a7ee7a7dSMarius Strobl }
618a7ee7a7dSMarius Strobl 
619a7ee7a7dSMarius Strobl /*
620a7ee7a7dSMarius Strobl  * Routines for accessing the transmit and receive buffers.
621a7ee7a7dSMarius Strobl  * The various CPU and adapter configurations supported by this
622a7ee7a7dSMarius Strobl  * driver require three different access methods for buffers
623a7ee7a7dSMarius Strobl  * and descriptors:
624a7ee7a7dSMarius Strobl  *	(1) contig (contiguous data; no padding),
625a7ee7a7dSMarius Strobl  *	(2) gap2 (two bytes of data followed by two bytes of padding),
626a7ee7a7dSMarius Strobl  *	(3) gap16 (16 bytes of data followed by 16 bytes of padding).
627a7ee7a7dSMarius Strobl  */
628a7ee7a7dSMarius Strobl 
629a7ee7a7dSMarius Strobl /*
630a7ee7a7dSMarius Strobl  * contig: contiguous data with no padding.
631a7ee7a7dSMarius Strobl  *
632a7ee7a7dSMarius Strobl  * Buffers may have any alignment.
633a7ee7a7dSMarius Strobl  */
634a7ee7a7dSMarius Strobl 
635a7ee7a7dSMarius Strobl void
lance_copytobuf_contig(struct lance_softc * sc,void * from,int boff,int len)636a7ee7a7dSMarius Strobl lance_copytobuf_contig(struct lance_softc *sc, void *from, int boff, int len)
637a7ee7a7dSMarius Strobl {
638a7ee7a7dSMarius Strobl 	volatile caddr_t buf = sc->sc_mem;
639a7ee7a7dSMarius Strobl 
640a7ee7a7dSMarius Strobl 	/*
641a7ee7a7dSMarius Strobl 	 * Just call memcpy() to do the work.
642a7ee7a7dSMarius Strobl 	 */
643a7ee7a7dSMarius Strobl 	memcpy(buf + boff, from, len);
644a7ee7a7dSMarius Strobl }
645a7ee7a7dSMarius Strobl 
646a7ee7a7dSMarius Strobl void
lance_copyfrombuf_contig(struct lance_softc * sc,void * to,int boff,int len)647a7ee7a7dSMarius Strobl lance_copyfrombuf_contig(struct lance_softc *sc, void *to, int boff, int len)
648a7ee7a7dSMarius Strobl {
649a7ee7a7dSMarius Strobl 	volatile caddr_t buf = sc->sc_mem;
650a7ee7a7dSMarius Strobl 
651a7ee7a7dSMarius Strobl 	/*
652a7ee7a7dSMarius Strobl 	 * Just call memcpy() to do the work.
653a7ee7a7dSMarius Strobl 	 */
654a7ee7a7dSMarius Strobl 	memcpy(to, buf + boff, len);
655a7ee7a7dSMarius Strobl }
656a7ee7a7dSMarius Strobl 
657a7ee7a7dSMarius Strobl void
lance_zerobuf_contig(struct lance_softc * sc,int boff,int len)658a7ee7a7dSMarius Strobl lance_zerobuf_contig(struct lance_softc *sc, int boff, int len)
659a7ee7a7dSMarius Strobl {
660a7ee7a7dSMarius Strobl 	volatile caddr_t buf = sc->sc_mem;
661a7ee7a7dSMarius Strobl 
662a7ee7a7dSMarius Strobl 	/*
663a7ee7a7dSMarius Strobl 	 * Just let memset() do the work
664a7ee7a7dSMarius Strobl 	 */
665a7ee7a7dSMarius Strobl 	memset(buf + boff, 0, len);
666a7ee7a7dSMarius Strobl }
667a7ee7a7dSMarius Strobl 
668a7ee7a7dSMarius Strobl #if 0
669a7ee7a7dSMarius Strobl /*
670a7ee7a7dSMarius Strobl  * Examples only; duplicate these and tweak (if necessary) in
671a7ee7a7dSMarius Strobl  * machine-specific front-ends.
672a7ee7a7dSMarius Strobl  */
673a7ee7a7dSMarius Strobl 
674a7ee7a7dSMarius Strobl /*
675a7ee7a7dSMarius Strobl  * gap2: two bytes of data followed by two bytes of pad.
676a7ee7a7dSMarius Strobl  *
677a7ee7a7dSMarius Strobl  * Buffers must be 4-byte aligned.  The code doesn't worry about
678a7ee7a7dSMarius Strobl  * doing an extra byte.
679a7ee7a7dSMarius Strobl  */
680a7ee7a7dSMarius Strobl 
681a7ee7a7dSMarius Strobl static void
682a7ee7a7dSMarius Strobl lance_copytobuf_gap2(struct lance_softc *sc, void *fromv, int boff, int len)
683a7ee7a7dSMarius Strobl {
684a7ee7a7dSMarius Strobl 	volatile caddr_t buf = sc->sc_mem;
685a7ee7a7dSMarius Strobl 	caddr_t from = fromv;
686a7ee7a7dSMarius Strobl 	volatile uint16_t *bptr;
687a7ee7a7dSMarius Strobl 
688a7ee7a7dSMarius Strobl 	if (boff & 0x1) {
689a7ee7a7dSMarius Strobl 		/* Handle unaligned first byte. */
690a7ee7a7dSMarius Strobl 		bptr = ((volatile uint16_t *)buf) + (boff - 1);
691a7ee7a7dSMarius Strobl 		*bptr = (*from++ << 8) | (*bptr & 0xff);
692a7ee7a7dSMarius Strobl 		bptr += 2;
693a7ee7a7dSMarius Strobl 		len--;
694a7ee7a7dSMarius Strobl 	} else
695a7ee7a7dSMarius Strobl 		bptr = ((volatile uint16_t *)buf) + boff;
696a7ee7a7dSMarius Strobl 	while (len > 1) {
697a7ee7a7dSMarius Strobl 		*bptr = (from[1] << 8) | (from[0] & 0xff);
698a7ee7a7dSMarius Strobl 		bptr += 2;
699a7ee7a7dSMarius Strobl 		from += 2;
700a7ee7a7dSMarius Strobl 		len -= 2;
701a7ee7a7dSMarius Strobl 	}
702a7ee7a7dSMarius Strobl 	if (len == 1)
703a7ee7a7dSMarius Strobl 		*bptr = (uint16_t)*from;
704a7ee7a7dSMarius Strobl }
705a7ee7a7dSMarius Strobl 
706a7ee7a7dSMarius Strobl static void
707a7ee7a7dSMarius Strobl lance_copyfrombuf_gap2(struct lance_softc *sc, void *tov, int boff, int len)
708a7ee7a7dSMarius Strobl {
709a7ee7a7dSMarius Strobl 	volatile caddr_t buf = sc->sc_mem;
710a7ee7a7dSMarius Strobl 	caddr_t to = tov;
711a7ee7a7dSMarius Strobl 	volatile uint16_t *bptr;
712a7ee7a7dSMarius Strobl 	uint16_t tmp;
713a7ee7a7dSMarius Strobl 
714a7ee7a7dSMarius Strobl 	if (boff & 0x1) {
715a7ee7a7dSMarius Strobl 		/* Handle unaligned first byte. */
716a7ee7a7dSMarius Strobl 		bptr = ((volatile uint16_t *)buf) + (boff - 1);
717a7ee7a7dSMarius Strobl 		*to++ = (*bptr >> 8) & 0xff;
718a7ee7a7dSMarius Strobl 		bptr += 2;
719a7ee7a7dSMarius Strobl 		len--;
720a7ee7a7dSMarius Strobl 	} else
721a7ee7a7dSMarius Strobl 		bptr = ((volatile uint16_t *)buf) + boff;
722a7ee7a7dSMarius Strobl 	while (len > 1) {
723a7ee7a7dSMarius Strobl 		tmp = *bptr;
724a7ee7a7dSMarius Strobl 		*to++ = tmp & 0xff;
725a7ee7a7dSMarius Strobl 		*to++ = (tmp >> 8) & 0xff;
726a7ee7a7dSMarius Strobl 		bptr += 2;
727a7ee7a7dSMarius Strobl 		len -= 2;
728a7ee7a7dSMarius Strobl 	}
729a7ee7a7dSMarius Strobl 	if (len == 1)
730a7ee7a7dSMarius Strobl 		*to = *bptr & 0xff;
731a7ee7a7dSMarius Strobl }
732a7ee7a7dSMarius Strobl 
733a7ee7a7dSMarius Strobl static void
734a7ee7a7dSMarius Strobl lance_zerobuf_gap2(struct lance_softc *sc, int boff, int len)
735a7ee7a7dSMarius Strobl {
736a7ee7a7dSMarius Strobl 	volatile caddr_t buf = sc->sc_mem;
737a7ee7a7dSMarius Strobl 	volatile uint16_t *bptr;
738a7ee7a7dSMarius Strobl 
739a7ee7a7dSMarius Strobl 	if ((unsigned)boff & 0x1) {
740a7ee7a7dSMarius Strobl 		bptr = ((volatile uint16_t *)buf) + (boff - 1);
741a7ee7a7dSMarius Strobl 		*bptr &= 0xff;
742a7ee7a7dSMarius Strobl 		bptr += 2;
743a7ee7a7dSMarius Strobl 		len--;
744a7ee7a7dSMarius Strobl 	} else
745a7ee7a7dSMarius Strobl 		bptr = ((volatile uint16_t *)buf) + boff;
746a7ee7a7dSMarius Strobl 	while (len > 0) {
747a7ee7a7dSMarius Strobl 		*bptr = 0;
748a7ee7a7dSMarius Strobl 		bptr += 2;
749a7ee7a7dSMarius Strobl 		len -= 2;
750a7ee7a7dSMarius Strobl 	}
751a7ee7a7dSMarius Strobl }
752a7ee7a7dSMarius Strobl 
753a7ee7a7dSMarius Strobl /*
754a7ee7a7dSMarius Strobl  * gap16: 16 bytes of data followed by 16 bytes of pad.
755a7ee7a7dSMarius Strobl  *
756a7ee7a7dSMarius Strobl  * Buffers must be 32-byte aligned.
757a7ee7a7dSMarius Strobl  */
758a7ee7a7dSMarius Strobl 
759a7ee7a7dSMarius Strobl static void
760a7ee7a7dSMarius Strobl lance_copytobuf_gap16(struct lance_softc *sc, void *fromv, int boff, int len)
761a7ee7a7dSMarius Strobl {
762a7ee7a7dSMarius Strobl 	volatile caddr_t buf = sc->sc_mem;
763a7ee7a7dSMarius Strobl 	caddr_t bptr, from = fromv;
764a7ee7a7dSMarius Strobl 	int xfer;
765a7ee7a7dSMarius Strobl 
766a7ee7a7dSMarius Strobl 	bptr = buf + ((boff << 1) & ~0x1f);
767a7ee7a7dSMarius Strobl 	boff &= 0xf;
768a7ee7a7dSMarius Strobl 	xfer = min(len, 16 - boff);
769a7ee7a7dSMarius Strobl 	while (len > 0) {
770a7ee7a7dSMarius Strobl 		memcpy(bptr + boff, from, xfer);
771a7ee7a7dSMarius Strobl 		from += xfer;
772a7ee7a7dSMarius Strobl 		bptr += 32;
773a7ee7a7dSMarius Strobl 		boff = 0;
774a7ee7a7dSMarius Strobl 		len -= xfer;
775a7ee7a7dSMarius Strobl 		xfer = min(len, 16);
776a7ee7a7dSMarius Strobl 	}
777a7ee7a7dSMarius Strobl }
778a7ee7a7dSMarius Strobl 
779a7ee7a7dSMarius Strobl static void
780a7ee7a7dSMarius Strobl lance_copyfrombuf_gap16(struct lance_softc *sc, void *tov, int boff, int len)
781a7ee7a7dSMarius Strobl {
782a7ee7a7dSMarius Strobl 	volatile caddr_t buf = sc->sc_mem;
783a7ee7a7dSMarius Strobl 	caddr_t bptr, to = tov;
784a7ee7a7dSMarius Strobl 	int xfer;
785a7ee7a7dSMarius Strobl 
786a7ee7a7dSMarius Strobl 	bptr = buf + ((boff << 1) & ~0x1f);
787a7ee7a7dSMarius Strobl 	boff &= 0xf;
788a7ee7a7dSMarius Strobl 	xfer = min(len, 16 - boff);
789a7ee7a7dSMarius Strobl 	while (len > 0) {
790a7ee7a7dSMarius Strobl 		memcpy(to, bptr + boff, xfer);
791a7ee7a7dSMarius Strobl 		to += xfer;
792a7ee7a7dSMarius Strobl 		bptr += 32;
793a7ee7a7dSMarius Strobl 		boff = 0;
794a7ee7a7dSMarius Strobl 		len -= xfer;
795a7ee7a7dSMarius Strobl 		xfer = min(len, 16);
796a7ee7a7dSMarius Strobl 	}
797a7ee7a7dSMarius Strobl }
798a7ee7a7dSMarius Strobl 
799a7ee7a7dSMarius Strobl static void
800a7ee7a7dSMarius Strobl lance_zerobuf_gap16(struct lance_softc *sc, int boff, int len)
801a7ee7a7dSMarius Strobl {
802a7ee7a7dSMarius Strobl 	volatile caddr_t buf = sc->sc_mem;
803a7ee7a7dSMarius Strobl 	caddr_t bptr;
804a7ee7a7dSMarius Strobl 	int xfer;
805a7ee7a7dSMarius Strobl 
806a7ee7a7dSMarius Strobl 	bptr = buf + ((boff << 1) & ~0x1f);
807a7ee7a7dSMarius Strobl 	boff &= 0xf;
808a7ee7a7dSMarius Strobl 	xfer = min(len, 16 - boff);
809a7ee7a7dSMarius Strobl 	while (len > 0) {
810a7ee7a7dSMarius Strobl 		memset(bptr + boff, 0, xfer);
811a7ee7a7dSMarius Strobl 		bptr += 32;
812a7ee7a7dSMarius Strobl 		boff = 0;
813a7ee7a7dSMarius Strobl 		len -= xfer;
814a7ee7a7dSMarius Strobl 		xfer = min(len, 16);
815a7ee7a7dSMarius Strobl 	}
816a7ee7a7dSMarius Strobl }
817a7ee7a7dSMarius Strobl #endif /* Example only */
818