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