xref: /freebsd/sys/dev/le/lance.c (revision a7ee7a7d6aa723ce89a6853d54056dfec8d403d9)
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>
81a7ee7a7dSMarius Strobl #include <sys/mbuf.h>
82a7ee7a7dSMarius Strobl #include <sys/mutex.h>
83a7ee7a7dSMarius Strobl #include <sys/socket.h>
84a7ee7a7dSMarius Strobl #include <sys/sockio.h>
85a7ee7a7dSMarius Strobl 
86a7ee7a7dSMarius Strobl #include <net/ethernet.h>
87a7ee7a7dSMarius Strobl #include <net/if.h>
88a7ee7a7dSMarius Strobl #include <net/if_arp.h>
89a7ee7a7dSMarius Strobl #include <net/if_dl.h>
90a7ee7a7dSMarius Strobl #include <net/if_media.h>
91a7ee7a7dSMarius Strobl #include <net/if_types.h>
92a7ee7a7dSMarius Strobl #include <net/if_vlan_var.h>
93a7ee7a7dSMarius Strobl 
94a7ee7a7dSMarius Strobl #include <dev/le/lancereg.h>
95a7ee7a7dSMarius Strobl #include <dev/le/lancevar.h>
96a7ee7a7dSMarius Strobl 
97a7ee7a7dSMarius Strobl devclass_t le_devclass;
98a7ee7a7dSMarius Strobl 
99a7ee7a7dSMarius Strobl static inline uint16_t ether_cmp(void *, void *);
100a7ee7a7dSMarius Strobl 
101a7ee7a7dSMarius Strobl static void lance_start(struct ifnet *);
102a7ee7a7dSMarius Strobl static void lance_stop(struct lance_softc *);
103a7ee7a7dSMarius Strobl static void lance_init(void *);
104a7ee7a7dSMarius Strobl static struct mbuf *lance_get(struct lance_softc *, int, int);
105a7ee7a7dSMarius Strobl static void lance_watchdog(struct ifnet *);
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 /*
111a7ee7a7dSMarius Strobl  * Compare two Ether/802 addresses for equality, inlined and
112a7ee7a7dSMarius Strobl  * unrolled for speed.  Use this like memcmp().
113a7ee7a7dSMarius Strobl  *
114a7ee7a7dSMarius Strobl  * XXX: Add <machine/inlines.h> for stuff like this?
115a7ee7a7dSMarius Strobl  * XXX: or maybe add it to libkern.h instead?
116a7ee7a7dSMarius Strobl  *
117a7ee7a7dSMarius Strobl  * "I'd love to have an inline assembler version of this."
118a7ee7a7dSMarius Strobl  * XXX: Who wanted that? mycroft?  I wrote one, but this
119a7ee7a7dSMarius Strobl  * version in C is as good as hand-coded assembly. -gwr
120a7ee7a7dSMarius Strobl  *
121a7ee7a7dSMarius Strobl  * Please do NOT tweak this without looking at the actual
122a7ee7a7dSMarius Strobl  * assembly code generated before and after your tweaks!
123a7ee7a7dSMarius Strobl  */
124a7ee7a7dSMarius Strobl static inline uint16_t
125a7ee7a7dSMarius Strobl ether_cmp(void *one, void *two)
126a7ee7a7dSMarius Strobl {
127a7ee7a7dSMarius Strobl 	uint16_t *a = (u_short *)one;
128a7ee7a7dSMarius Strobl 	uint16_t *b = (u_short *)two;
129a7ee7a7dSMarius Strobl 	uint16_t diff;
130a7ee7a7dSMarius Strobl 
131a7ee7a7dSMarius Strobl #ifdef	m68k
132a7ee7a7dSMarius Strobl 	/*
133a7ee7a7dSMarius Strobl 	 * The post-increment-pointer form produces the best
134a7ee7a7dSMarius Strobl 	 * machine code for m68k.  This was carefully tuned
135a7ee7a7dSMarius Strobl 	 * so it compiles to just 8 short (2-byte) op-codes!
136a7ee7a7dSMarius Strobl 	 */
137a7ee7a7dSMarius Strobl 	diff  = *a++ - *b++;
138a7ee7a7dSMarius Strobl 	diff |= *a++ - *b++;
139a7ee7a7dSMarius Strobl 	diff |= *a++ - *b++;
140a7ee7a7dSMarius Strobl #else
141a7ee7a7dSMarius Strobl 	/*
142a7ee7a7dSMarius Strobl 	 * Most modern CPUs do better with a single expresion.
143a7ee7a7dSMarius Strobl 	 * Note that short-cut evaluation is NOT helpful here,
144a7ee7a7dSMarius Strobl 	 * because it just makes the code longer, not faster!
145a7ee7a7dSMarius Strobl 	 */
146a7ee7a7dSMarius Strobl 	diff = (a[0] - b[0]) | (a[1] - b[1]) | (a[2] - b[2]);
147a7ee7a7dSMarius Strobl #endif
148a7ee7a7dSMarius Strobl 
149a7ee7a7dSMarius Strobl 	return (diff);
150a7ee7a7dSMarius Strobl }
151a7ee7a7dSMarius Strobl 
152a7ee7a7dSMarius Strobl #define ETHER_CMP	ether_cmp
153a7ee7a7dSMarius Strobl 
154a7ee7a7dSMarius Strobl #ifdef LANCE_REVC_BUG
155a7ee7a7dSMarius Strobl /* Make sure this is short-aligned, for ether_cmp(). */
156a7ee7a7dSMarius Strobl static uint16_t bcast_enaddr[3] = { ~0, ~0, ~0 };
157a7ee7a7dSMarius Strobl #endif
158a7ee7a7dSMarius Strobl 
159a7ee7a7dSMarius Strobl int
160a7ee7a7dSMarius Strobl lance_config(struct lance_softc *sc, const char* name, int unit)
161a7ee7a7dSMarius Strobl {
162a7ee7a7dSMarius Strobl 	struct ifnet *ifp;
163a7ee7a7dSMarius Strobl 	int i, nbuf;
164a7ee7a7dSMarius Strobl 
165a7ee7a7dSMarius Strobl 	if (LE_LOCK_INITIALIZED(sc) == 0)
166a7ee7a7dSMarius Strobl 		return (ENXIO);
167a7ee7a7dSMarius Strobl 
168a7ee7a7dSMarius Strobl 	ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
169a7ee7a7dSMarius Strobl 	if (ifp == NULL)
170a7ee7a7dSMarius Strobl 		return (ENOSPC);
171a7ee7a7dSMarius Strobl 
172a7ee7a7dSMarius Strobl 	/* Initialize ifnet structure. */
173a7ee7a7dSMarius Strobl 	ifp->if_softc = sc;
174a7ee7a7dSMarius Strobl 	if_initname(ifp, name, unit);
175a7ee7a7dSMarius Strobl 	ifp->if_start = lance_start;
176a7ee7a7dSMarius Strobl 	ifp->if_ioctl = lance_ioctl;
177a7ee7a7dSMarius Strobl 	ifp->if_watchdog = lance_watchdog;
178a7ee7a7dSMarius Strobl 	ifp->if_init = lance_init;
179a7ee7a7dSMarius Strobl 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
180a7ee7a7dSMarius Strobl #ifdef LANCE_REVC_BUG
181a7ee7a7dSMarius Strobl 	ifp->if_flags &= ~IFF_MULTICAST;
182a7ee7a7dSMarius Strobl #endif
183a7ee7a7dSMarius Strobl 	ifp->if_baudrate = IF_Mbps(10);
184a7ee7a7dSMarius Strobl 	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
185a7ee7a7dSMarius Strobl 	ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
186a7ee7a7dSMarius Strobl 	IFQ_SET_READY(&ifp->if_snd);
187a7ee7a7dSMarius Strobl 
188a7ee7a7dSMarius Strobl 	/* Initialize ifmedia structures. */
189a7ee7a7dSMarius Strobl 	ifmedia_init(&sc->sc_media, 0, lance_mediachange, lance_mediastatus);
190a7ee7a7dSMarius Strobl 	if (sc->sc_supmedia != NULL) {
191a7ee7a7dSMarius Strobl 		for (i = 0; i < sc->sc_nsupmedia; i++)
192a7ee7a7dSMarius Strobl 			ifmedia_add(&sc->sc_media, sc->sc_supmedia[i], 0, NULL);
193a7ee7a7dSMarius Strobl 		ifmedia_set(&sc->sc_media, sc->sc_defaultmedia);
194a7ee7a7dSMarius Strobl 	} else {
195a7ee7a7dSMarius Strobl 		ifmedia_add(&sc->sc_media,
196a7ee7a7dSMarius Strobl 		    IFM_MAKEWORD(IFM_ETHER, IFM_MANUAL, 0, 0), 0, NULL);
197a7ee7a7dSMarius Strobl 		ifmedia_set(&sc->sc_media,
198a7ee7a7dSMarius Strobl 		    IFM_MAKEWORD(IFM_ETHER, IFM_MANUAL, 0, 0));
199a7ee7a7dSMarius Strobl 	}
200a7ee7a7dSMarius Strobl 
201a7ee7a7dSMarius Strobl 	switch (sc->sc_memsize) {
202a7ee7a7dSMarius Strobl 	case 8192:
203a7ee7a7dSMarius Strobl 		sc->sc_nrbuf = 4;
204a7ee7a7dSMarius Strobl 		sc->sc_ntbuf = 1;
205a7ee7a7dSMarius Strobl 		break;
206a7ee7a7dSMarius Strobl 	case 16384:
207a7ee7a7dSMarius Strobl 		sc->sc_nrbuf = 8;
208a7ee7a7dSMarius Strobl 		sc->sc_ntbuf = 2;
209a7ee7a7dSMarius Strobl 		break;
210a7ee7a7dSMarius Strobl 	case 32768:
211a7ee7a7dSMarius Strobl 		sc->sc_nrbuf = 16;
212a7ee7a7dSMarius Strobl 		sc->sc_ntbuf = 4;
213a7ee7a7dSMarius Strobl 		break;
214a7ee7a7dSMarius Strobl 	case 65536:
215a7ee7a7dSMarius Strobl 		sc->sc_nrbuf = 32;
216a7ee7a7dSMarius Strobl 		sc->sc_ntbuf = 8;
217a7ee7a7dSMarius Strobl 		break;
218a7ee7a7dSMarius Strobl 	case 131072:
219a7ee7a7dSMarius Strobl 		sc->sc_nrbuf = 64;
220a7ee7a7dSMarius Strobl 		sc->sc_ntbuf = 16;
221a7ee7a7dSMarius Strobl 		break;
222a7ee7a7dSMarius Strobl 	case 262144:
223a7ee7a7dSMarius Strobl 		sc->sc_nrbuf = 128;
224a7ee7a7dSMarius Strobl 		sc->sc_ntbuf = 32;
225a7ee7a7dSMarius Strobl 		break;
226a7ee7a7dSMarius Strobl 	default:
227a7ee7a7dSMarius Strobl 		/* weird memory size; cope with it */
228a7ee7a7dSMarius Strobl 		nbuf = sc->sc_memsize / LEBLEN;
229a7ee7a7dSMarius Strobl 		sc->sc_ntbuf = nbuf / 5;
230a7ee7a7dSMarius Strobl 		sc->sc_nrbuf = nbuf - sc->sc_ntbuf;
231a7ee7a7dSMarius Strobl 	}
232a7ee7a7dSMarius Strobl 
233a7ee7a7dSMarius Strobl 	if_printf(ifp, "%d receive buffers, %d transmit buffers\n",
234a7ee7a7dSMarius Strobl 	    sc->sc_nrbuf, sc->sc_ntbuf);
235a7ee7a7dSMarius Strobl 
236a7ee7a7dSMarius Strobl 	/* Make sure the chip is stopped. */
237a7ee7a7dSMarius Strobl 	LE_LOCK(sc);
238a7ee7a7dSMarius Strobl 	lance_stop(sc);
239a7ee7a7dSMarius Strobl 	LE_UNLOCK(sc);
240a7ee7a7dSMarius Strobl 
241a7ee7a7dSMarius Strobl 	return (0);
242a7ee7a7dSMarius Strobl }
243a7ee7a7dSMarius Strobl 
244a7ee7a7dSMarius Strobl void
245a7ee7a7dSMarius Strobl lance_attach(struct lance_softc *sc)
246a7ee7a7dSMarius Strobl {
247a7ee7a7dSMarius Strobl 	struct ifnet *ifp = sc->sc_ifp;
248a7ee7a7dSMarius Strobl 
249a7ee7a7dSMarius Strobl 	/* Attach the interface. */
250a7ee7a7dSMarius Strobl 	ether_ifattach(ifp, sc->sc_enaddr);
251a7ee7a7dSMarius Strobl 
252a7ee7a7dSMarius Strobl 	/* Claim 802.1q capability. */
253a7ee7a7dSMarius Strobl 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
254a7ee7a7dSMarius Strobl 	ifp->if_capabilities |= IFCAP_VLAN_MTU;
255a7ee7a7dSMarius Strobl 	ifp->if_capenable |= IFCAP_VLAN_MTU;
256a7ee7a7dSMarius Strobl }
257a7ee7a7dSMarius Strobl 
258a7ee7a7dSMarius Strobl void
259a7ee7a7dSMarius Strobl lance_detach(struct lance_softc *sc)
260a7ee7a7dSMarius Strobl {
261a7ee7a7dSMarius Strobl 	struct ifnet *ifp = sc->sc_ifp;
262a7ee7a7dSMarius Strobl 
263a7ee7a7dSMarius Strobl 	LE_LOCK(sc);
264a7ee7a7dSMarius Strobl 	lance_stop(sc);
265a7ee7a7dSMarius Strobl 	LE_UNLOCK(sc);
266a7ee7a7dSMarius Strobl 	ether_ifdetach(ifp);
267a7ee7a7dSMarius Strobl 	if_free(ifp);
268a7ee7a7dSMarius Strobl }
269a7ee7a7dSMarius Strobl 
270a7ee7a7dSMarius Strobl void
271a7ee7a7dSMarius Strobl lance_suspend(struct lance_softc *sc)
272a7ee7a7dSMarius Strobl {
273a7ee7a7dSMarius Strobl 
274a7ee7a7dSMarius Strobl 	LE_LOCK(sc);
275a7ee7a7dSMarius Strobl 	lance_stop(sc);
276a7ee7a7dSMarius Strobl 	LE_UNLOCK(sc);
277a7ee7a7dSMarius Strobl }
278a7ee7a7dSMarius Strobl 
279a7ee7a7dSMarius Strobl void
280a7ee7a7dSMarius Strobl lance_resume(struct lance_softc *sc)
281a7ee7a7dSMarius Strobl {
282a7ee7a7dSMarius Strobl 
283a7ee7a7dSMarius Strobl 	LE_LOCK(sc);
284a7ee7a7dSMarius Strobl 	if (sc->sc_ifp->if_flags & IFF_UP)
285a7ee7a7dSMarius Strobl 		lance_init_locked(sc);
286a7ee7a7dSMarius Strobl 	LE_UNLOCK(sc);
287a7ee7a7dSMarius Strobl }
288a7ee7a7dSMarius Strobl 
289a7ee7a7dSMarius Strobl static void
290a7ee7a7dSMarius Strobl lance_start(struct ifnet *ifp)
291a7ee7a7dSMarius Strobl {
292a7ee7a7dSMarius Strobl 	struct lance_softc *sc = ifp->if_softc;
293a7ee7a7dSMarius Strobl 
294a7ee7a7dSMarius Strobl 	LE_LOCK(sc);
295a7ee7a7dSMarius Strobl 	(*sc->sc_start_locked)(sc);
296a7ee7a7dSMarius Strobl 	LE_UNLOCK(sc);
297a7ee7a7dSMarius Strobl }
298a7ee7a7dSMarius Strobl 
299a7ee7a7dSMarius Strobl static void
300a7ee7a7dSMarius Strobl lance_stop(struct lance_softc *sc)
301a7ee7a7dSMarius Strobl {
302a7ee7a7dSMarius Strobl 	struct ifnet *ifp = sc->sc_ifp;
303a7ee7a7dSMarius Strobl 
304a7ee7a7dSMarius Strobl 	LE_LOCK_ASSERT(sc, MA_OWNED);
305a7ee7a7dSMarius Strobl 
306a7ee7a7dSMarius Strobl 	/*
307a7ee7a7dSMarius Strobl 	 * Mark the interface down and cancel the watchdog timer.
308a7ee7a7dSMarius Strobl 	 */
309a7ee7a7dSMarius Strobl 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
310a7ee7a7dSMarius Strobl 	ifp->if_timer = 0;
311a7ee7a7dSMarius Strobl 
312a7ee7a7dSMarius Strobl 	(*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_STOP);
313a7ee7a7dSMarius Strobl }
314a7ee7a7dSMarius Strobl 
315a7ee7a7dSMarius Strobl static void
316a7ee7a7dSMarius Strobl lance_init(void *xsc)
317a7ee7a7dSMarius Strobl {
318a7ee7a7dSMarius Strobl 	struct lance_softc *sc = (struct lance_softc *)xsc;
319a7ee7a7dSMarius Strobl 
320a7ee7a7dSMarius Strobl 	LE_LOCK(sc);
321a7ee7a7dSMarius Strobl 	lance_init_locked(sc);
322a7ee7a7dSMarius Strobl 	LE_UNLOCK(sc);
323a7ee7a7dSMarius Strobl }
324a7ee7a7dSMarius Strobl 
325a7ee7a7dSMarius Strobl /*
326a7ee7a7dSMarius Strobl  * Initialization of interface; set up initialization block
327a7ee7a7dSMarius Strobl  * and transmit/receive descriptor rings.
328a7ee7a7dSMarius Strobl  */
329a7ee7a7dSMarius Strobl void
330a7ee7a7dSMarius Strobl lance_init_locked(struct lance_softc *sc)
331a7ee7a7dSMarius Strobl {
332a7ee7a7dSMarius Strobl 	struct ifnet *ifp = sc->sc_ifp;
333a7ee7a7dSMarius Strobl 	u_long a;
334a7ee7a7dSMarius Strobl 	int timo;
335a7ee7a7dSMarius Strobl 
336a7ee7a7dSMarius Strobl 	LE_LOCK_ASSERT(sc, MA_OWNED);
337a7ee7a7dSMarius Strobl 
338a7ee7a7dSMarius Strobl 	(*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_STOP);
339a7ee7a7dSMarius Strobl 	DELAY(100);
340a7ee7a7dSMarius Strobl 
341a7ee7a7dSMarius Strobl 	/* Newer LANCE chips have a reset register. */
342a7ee7a7dSMarius Strobl 	if (sc->sc_hwreset)
343a7ee7a7dSMarius Strobl 		(*sc->sc_hwreset)(sc);
344a7ee7a7dSMarius Strobl 
345a7ee7a7dSMarius Strobl 	/* Set the correct byte swapping mode, etc. */
346a7ee7a7dSMarius Strobl 	(*sc->sc_wrcsr)(sc, LE_CSR3, sc->sc_conf3);
347a7ee7a7dSMarius Strobl 
348a7ee7a7dSMarius Strobl 	/*
349a7ee7a7dSMarius Strobl 	 * Update our private copy of the Ethernet address.
350a7ee7a7dSMarius Strobl 	 * We NEED the copy so we can ensure its alignment!
351a7ee7a7dSMarius Strobl 	 */
352a7ee7a7dSMarius Strobl 	memcpy(sc->sc_enaddr, IF_LLADDR(ifp), ETHER_ADDR_LEN);
353a7ee7a7dSMarius Strobl 
354a7ee7a7dSMarius Strobl 	/* Set up LANCE init block. */
355a7ee7a7dSMarius Strobl 	(*sc->sc_meminit)(sc);
356a7ee7a7dSMarius Strobl 
357a7ee7a7dSMarius Strobl 	/* Give LANCE the physical address of its init block. */
358a7ee7a7dSMarius Strobl 	a = sc->sc_addr + LE_INITADDR(sc);
359a7ee7a7dSMarius Strobl 	(*sc->sc_wrcsr)(sc, LE_CSR1, a & 0xffff);
360a7ee7a7dSMarius Strobl 	(*sc->sc_wrcsr)(sc, LE_CSR2, a >> 16);
361a7ee7a7dSMarius Strobl 
362a7ee7a7dSMarius Strobl 	/* Try to initialize the LANCE. */
363a7ee7a7dSMarius Strobl 	DELAY(100);
364a7ee7a7dSMarius Strobl 	(*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INIT);
365a7ee7a7dSMarius Strobl 
366a7ee7a7dSMarius Strobl 	/* Wait for initialization to finish. */
367a7ee7a7dSMarius Strobl 	for (timo = 100000; timo; timo--)
368a7ee7a7dSMarius Strobl 		if ((*sc->sc_rdcsr)(sc, LE_CSR0) & LE_C0_IDON)
369a7ee7a7dSMarius Strobl 			break;
370a7ee7a7dSMarius Strobl 
371a7ee7a7dSMarius Strobl 	/* Set the current media. */
372a7ee7a7dSMarius Strobl 	if (sc->sc_mediachange)
373a7ee7a7dSMarius Strobl 		(void)(*sc->sc_mediachange)(sc);
374a7ee7a7dSMarius Strobl 
375a7ee7a7dSMarius Strobl 	if ((*sc->sc_rdcsr)(sc, LE_CSR0) & LE_C0_IDON) {
376a7ee7a7dSMarius Strobl 		/* Start the LANCE. */
377a7ee7a7dSMarius Strobl 		(*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA | LE_C0_STRT);
378a7ee7a7dSMarius Strobl 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
379a7ee7a7dSMarius Strobl 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
380a7ee7a7dSMarius Strobl 		ifp->if_timer = 0;
381a7ee7a7dSMarius Strobl 		(*sc->sc_start_locked)(sc);
382a7ee7a7dSMarius Strobl 	} else
383a7ee7a7dSMarius Strobl 		if_printf(ifp, "controller failed to initialize\n");
384a7ee7a7dSMarius Strobl 
385a7ee7a7dSMarius Strobl 	if (sc->sc_hwinit)
386a7ee7a7dSMarius Strobl 		(*sc->sc_hwinit)(sc);
387a7ee7a7dSMarius Strobl }
388a7ee7a7dSMarius Strobl 
389a7ee7a7dSMarius Strobl /*
390a7ee7a7dSMarius Strobl  * Routine to copy from mbuf chain to transmit buffer in
391a7ee7a7dSMarius Strobl  * network buffer memory.
392a7ee7a7dSMarius Strobl  */
393a7ee7a7dSMarius Strobl int
394a7ee7a7dSMarius Strobl lance_put(struct lance_softc *sc, int boff, struct mbuf *m)
395a7ee7a7dSMarius Strobl {
396a7ee7a7dSMarius Strobl 	struct mbuf *n;
397a7ee7a7dSMarius Strobl 	int len, tlen = 0;
398a7ee7a7dSMarius Strobl 
399a7ee7a7dSMarius Strobl 	LE_LOCK_ASSERT(sc, MA_OWNED);
400a7ee7a7dSMarius Strobl 
401a7ee7a7dSMarius Strobl 	for (; m; m = n) {
402a7ee7a7dSMarius Strobl 		len = m->m_len;
403a7ee7a7dSMarius Strobl 		if (len == 0) {
404a7ee7a7dSMarius Strobl 			n = m_free(m);
405a7ee7a7dSMarius Strobl 			m = NULL;
406a7ee7a7dSMarius Strobl 			continue;
407a7ee7a7dSMarius Strobl 		}
408a7ee7a7dSMarius Strobl 		(*sc->sc_copytobuf)(sc, mtod(m, caddr_t), boff, len);
409a7ee7a7dSMarius Strobl 		boff += len;
410a7ee7a7dSMarius Strobl 		tlen += len;
411a7ee7a7dSMarius Strobl 		n = m_free(m);
412a7ee7a7dSMarius Strobl 		m = NULL;
413a7ee7a7dSMarius Strobl 	}
414a7ee7a7dSMarius Strobl 	if (tlen < LEMINSIZE) {
415a7ee7a7dSMarius Strobl 		(*sc->sc_zerobuf)(sc, boff, LEMINSIZE - tlen);
416a7ee7a7dSMarius Strobl 		tlen = LEMINSIZE;
417a7ee7a7dSMarius Strobl 	}
418a7ee7a7dSMarius Strobl 	return (tlen);
419a7ee7a7dSMarius Strobl }
420a7ee7a7dSMarius Strobl 
421a7ee7a7dSMarius Strobl /*
422a7ee7a7dSMarius Strobl  * Pull data off an interface.
423a7ee7a7dSMarius Strobl  * Len is length of data, with local net header stripped.
424a7ee7a7dSMarius Strobl  * We copy the data into mbufs.  When full cluster sized units are present
425a7ee7a7dSMarius Strobl  * we copy into clusters.
426a7ee7a7dSMarius Strobl  */
427a7ee7a7dSMarius Strobl static struct mbuf *
428a7ee7a7dSMarius Strobl lance_get(struct lance_softc *sc, int boff, int totlen)
429a7ee7a7dSMarius Strobl {
430a7ee7a7dSMarius Strobl 	struct ifnet *ifp = sc->sc_ifp;
431a7ee7a7dSMarius Strobl 	struct mbuf *m, *m0, *newm;
432a7ee7a7dSMarius Strobl 	caddr_t newdata;
433a7ee7a7dSMarius Strobl 	int len;
434a7ee7a7dSMarius Strobl 
435a7ee7a7dSMarius Strobl 	MGETHDR(m0, M_DONTWAIT, MT_DATA);
436a7ee7a7dSMarius Strobl 	if (m0 == 0)
437a7ee7a7dSMarius Strobl 		return (0);
438a7ee7a7dSMarius Strobl 	m0->m_pkthdr.rcvif = ifp;
439a7ee7a7dSMarius Strobl 	m0->m_pkthdr.len = totlen;
440a7ee7a7dSMarius Strobl 	len = MHLEN;
441a7ee7a7dSMarius Strobl 	m = m0;
442a7ee7a7dSMarius Strobl 
443a7ee7a7dSMarius Strobl 	while (totlen > 0) {
444a7ee7a7dSMarius Strobl 		if (totlen >= MINCLSIZE) {
445a7ee7a7dSMarius Strobl 			MCLGET(m, M_DONTWAIT);
446a7ee7a7dSMarius Strobl 			if ((m->m_flags & M_EXT) == 0)
447a7ee7a7dSMarius Strobl 				goto bad;
448a7ee7a7dSMarius Strobl 			len = MCLBYTES;
449a7ee7a7dSMarius Strobl 		}
450a7ee7a7dSMarius Strobl 
451a7ee7a7dSMarius Strobl 		if (m == m0) {
452a7ee7a7dSMarius Strobl 			newdata = (caddr_t)
453a7ee7a7dSMarius Strobl 			    ALIGN(m->m_data + ETHER_HDR_LEN) - ETHER_HDR_LEN;
454a7ee7a7dSMarius Strobl 			len -= newdata - m->m_data;
455a7ee7a7dSMarius Strobl 			m->m_data = newdata;
456a7ee7a7dSMarius Strobl 		}
457a7ee7a7dSMarius Strobl 
458a7ee7a7dSMarius Strobl 		m->m_len = len = min(totlen, len);
459a7ee7a7dSMarius Strobl 		(*sc->sc_copyfrombuf)(sc, mtod(m, caddr_t), boff, len);
460a7ee7a7dSMarius Strobl 		boff += len;
461a7ee7a7dSMarius Strobl 
462a7ee7a7dSMarius Strobl 		totlen -= len;
463a7ee7a7dSMarius Strobl 		if (totlen > 0) {
464a7ee7a7dSMarius Strobl 			MGET(newm, M_DONTWAIT, MT_DATA);
465a7ee7a7dSMarius Strobl 			if (newm == 0)
466a7ee7a7dSMarius Strobl 				goto bad;
467a7ee7a7dSMarius Strobl 			len = MLEN;
468a7ee7a7dSMarius Strobl 			m = m->m_next = newm;
469a7ee7a7dSMarius Strobl 		}
470a7ee7a7dSMarius Strobl 	}
471a7ee7a7dSMarius Strobl 
472a7ee7a7dSMarius Strobl 	return (m0);
473a7ee7a7dSMarius Strobl 
474a7ee7a7dSMarius Strobl  bad:
475a7ee7a7dSMarius Strobl 	m_freem(m0);
476a7ee7a7dSMarius Strobl 	return (0);
477a7ee7a7dSMarius Strobl }
478a7ee7a7dSMarius Strobl 
479a7ee7a7dSMarius Strobl /*
480a7ee7a7dSMarius Strobl  * Pass a packet to the higher levels.
481a7ee7a7dSMarius Strobl  */
482a7ee7a7dSMarius Strobl void
483a7ee7a7dSMarius Strobl lance_read(struct lance_softc *sc, int boff, int len)
484a7ee7a7dSMarius Strobl {
485a7ee7a7dSMarius Strobl 	struct ifnet *ifp = sc->sc_ifp;
486a7ee7a7dSMarius Strobl 	struct ether_header *eh;
487a7ee7a7dSMarius Strobl 	struct mbuf *m;
488a7ee7a7dSMarius Strobl 
489a7ee7a7dSMarius Strobl 	LE_LOCK_ASSERT(sc, MA_OWNED);
490a7ee7a7dSMarius Strobl 
491a7ee7a7dSMarius Strobl 	if (len <= ETHER_HDR_LEN || len > LEBLEN - ETHER_CRC_LEN) {
492a7ee7a7dSMarius Strobl #ifdef LEDEBUG
493a7ee7a7dSMarius Strobl 		if_printf(ifp, "invalid packet size %d; dropping\n", len);
494a7ee7a7dSMarius Strobl #endif
495a7ee7a7dSMarius Strobl 		ifp->if_ierrors++;
496a7ee7a7dSMarius Strobl 		return;
497a7ee7a7dSMarius Strobl 	}
498a7ee7a7dSMarius Strobl 
499a7ee7a7dSMarius Strobl 	/* Pull packet off interface. */
500a7ee7a7dSMarius Strobl 	m = lance_get(sc, boff, len);
501a7ee7a7dSMarius Strobl 	if (m == 0) {
502a7ee7a7dSMarius Strobl 		ifp->if_ierrors++;
503a7ee7a7dSMarius Strobl 		return;
504a7ee7a7dSMarius Strobl 	}
505a7ee7a7dSMarius Strobl 
506a7ee7a7dSMarius Strobl 	ifp->if_ipackets++;
507a7ee7a7dSMarius Strobl 
508a7ee7a7dSMarius Strobl 	eh = mtod(m, struct ether_header *);
509a7ee7a7dSMarius Strobl 
510a7ee7a7dSMarius Strobl #ifdef LANCE_REVC_BUG
511a7ee7a7dSMarius Strobl 	/*
512a7ee7a7dSMarius Strobl 	 * The old LANCE (Rev. C) chips have a bug which causes
513a7ee7a7dSMarius Strobl 	 * garbage to be inserted in front of the received packet.
514a7ee7a7dSMarius Strobl 	 * The work-around is to ignore packets with an invalid
515a7ee7a7dSMarius Strobl 	 * destination address (garbage will usually not match).
516a7ee7a7dSMarius Strobl 	 * Of course, this precludes multicast support...
517a7ee7a7dSMarius Strobl 	 */
518a7ee7a7dSMarius Strobl 	if (ETHER_CMP(eh->ether_dhost, sc->sc_enaddr) &&
519a7ee7a7dSMarius Strobl 	    ETHER_CMP(eh->ether_dhost, bcast_enaddr)) {
520a7ee7a7dSMarius Strobl 		m_freem(m);
521a7ee7a7dSMarius Strobl 		return;
522a7ee7a7dSMarius Strobl 	}
523a7ee7a7dSMarius Strobl #endif
524a7ee7a7dSMarius Strobl 
525a7ee7a7dSMarius Strobl 	/*
526a7ee7a7dSMarius Strobl 	 * Some lance device does not present IFF_SIMPLEX behavior on multicast
527a7ee7a7dSMarius Strobl 	 * packets.  Make sure to drop it if it is from ourselves.
528a7ee7a7dSMarius Strobl 	 */
529a7ee7a7dSMarius Strobl 	if (!ETHER_CMP(eh->ether_shost, sc->sc_enaddr)) {
530a7ee7a7dSMarius Strobl 		m_freem(m);
531a7ee7a7dSMarius Strobl 		return;
532a7ee7a7dSMarius Strobl 	}
533a7ee7a7dSMarius Strobl 
534a7ee7a7dSMarius Strobl 	/* Pass the packet up. */
535a7ee7a7dSMarius Strobl 	LE_UNLOCK(sc);
536a7ee7a7dSMarius Strobl 	(*ifp->if_input)(ifp, m);
537a7ee7a7dSMarius Strobl 	LE_LOCK(sc);
538a7ee7a7dSMarius Strobl }
539a7ee7a7dSMarius Strobl 
540a7ee7a7dSMarius Strobl static void
541a7ee7a7dSMarius Strobl lance_watchdog(struct ifnet *ifp)
542a7ee7a7dSMarius Strobl {
543a7ee7a7dSMarius Strobl 	struct lance_softc *sc = ifp->if_softc;
544a7ee7a7dSMarius Strobl 
545a7ee7a7dSMarius Strobl 	LE_LOCK(sc);
546a7ee7a7dSMarius Strobl 	if_printf(ifp, "device timeout\n");
547a7ee7a7dSMarius Strobl 	++ifp->if_oerrors;
548a7ee7a7dSMarius Strobl 	lance_init_locked(sc);
549a7ee7a7dSMarius Strobl 	LE_UNLOCK(sc);
550a7ee7a7dSMarius Strobl }
551a7ee7a7dSMarius Strobl 
552a7ee7a7dSMarius Strobl static int
553a7ee7a7dSMarius Strobl lance_mediachange(struct ifnet *ifp)
554a7ee7a7dSMarius Strobl {
555a7ee7a7dSMarius Strobl 	struct lance_softc *sc = ifp->if_softc;
556a7ee7a7dSMarius Strobl 	int error;
557a7ee7a7dSMarius Strobl 
558a7ee7a7dSMarius Strobl 	if (sc->sc_mediachange) {
559a7ee7a7dSMarius Strobl 		LE_LOCK(sc);
560a7ee7a7dSMarius Strobl 		error = (*sc->sc_mediachange)(sc);
561a7ee7a7dSMarius Strobl 		LE_UNLOCK(sc);
562a7ee7a7dSMarius Strobl 		return (error);
563a7ee7a7dSMarius Strobl 	}
564a7ee7a7dSMarius Strobl 	return (0);
565a7ee7a7dSMarius Strobl }
566a7ee7a7dSMarius Strobl 
567a7ee7a7dSMarius Strobl static void
568a7ee7a7dSMarius Strobl lance_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
569a7ee7a7dSMarius Strobl {
570a7ee7a7dSMarius Strobl 	struct lance_softc *sc = ifp->if_softc;
571a7ee7a7dSMarius Strobl 
572a7ee7a7dSMarius Strobl 	LE_LOCK(sc);
573a7ee7a7dSMarius Strobl 	if (!(ifp->if_flags & IFF_UP)) {
574a7ee7a7dSMarius Strobl 		LE_UNLOCK(sc);
575a7ee7a7dSMarius Strobl 		return;
576a7ee7a7dSMarius Strobl 	}
577a7ee7a7dSMarius Strobl 
578a7ee7a7dSMarius Strobl 	ifmr->ifm_status = IFM_AVALID;
579a7ee7a7dSMarius Strobl 	if (sc->sc_flags & LE_CARRIER)
580a7ee7a7dSMarius Strobl 		ifmr->ifm_status |= IFM_ACTIVE;
581a7ee7a7dSMarius Strobl 
582a7ee7a7dSMarius Strobl 	if (sc->sc_mediastatus)
583a7ee7a7dSMarius Strobl 		(*sc->sc_mediastatus)(sc, ifmr);
584a7ee7a7dSMarius Strobl 	LE_UNLOCK(sc);
585a7ee7a7dSMarius Strobl }
586a7ee7a7dSMarius Strobl 
587a7ee7a7dSMarius Strobl /*
588a7ee7a7dSMarius Strobl  * Process an ioctl request.
589a7ee7a7dSMarius Strobl  */
590a7ee7a7dSMarius Strobl static int
591a7ee7a7dSMarius Strobl lance_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
592a7ee7a7dSMarius Strobl {
593a7ee7a7dSMarius Strobl 	struct lance_softc *sc = ifp->if_softc;
594a7ee7a7dSMarius Strobl 	struct ifreq *ifr = (struct ifreq *)data;
595a7ee7a7dSMarius Strobl 	int error = 0;
596a7ee7a7dSMarius Strobl 
597a7ee7a7dSMarius Strobl 	switch (cmd) {
598a7ee7a7dSMarius Strobl 	case SIOCSIFFLAGS:
599a7ee7a7dSMarius Strobl 		LE_LOCK(sc);
600a7ee7a7dSMarius Strobl 		if (ifp->if_flags & IFF_PROMISC) {
601a7ee7a7dSMarius Strobl 			if (!(sc->sc_flags & LE_PROMISC)) {
602a7ee7a7dSMarius Strobl 				sc->sc_flags |= LE_PROMISC;
603a7ee7a7dSMarius Strobl 				lance_init_locked(sc);
604a7ee7a7dSMarius Strobl 			}
605a7ee7a7dSMarius Strobl 		} else if (sc->sc_flags & LE_PROMISC) {
606a7ee7a7dSMarius Strobl 			sc->sc_flags &= ~LE_PROMISC;
607a7ee7a7dSMarius Strobl 			lance_init_locked(sc);
608a7ee7a7dSMarius Strobl 		}
609a7ee7a7dSMarius Strobl 
610a7ee7a7dSMarius Strobl 		if ((ifp->if_flags & IFF_ALLMULTI) &&
611a7ee7a7dSMarius Strobl 		    !(sc->sc_flags & LE_ALLMULTI)) {
612a7ee7a7dSMarius Strobl 			sc->sc_flags |= LE_ALLMULTI;
613a7ee7a7dSMarius Strobl 			lance_init_locked(sc);
614a7ee7a7dSMarius Strobl 		} else if (!(ifp->if_flags & IFF_ALLMULTI) &&
615a7ee7a7dSMarius Strobl 		    (sc->sc_flags & LE_ALLMULTI)) {
616a7ee7a7dSMarius Strobl 			sc->sc_flags &= ~LE_ALLMULTI;
617a7ee7a7dSMarius Strobl 			lance_init_locked(sc);
618a7ee7a7dSMarius Strobl 		}
619a7ee7a7dSMarius Strobl 
620a7ee7a7dSMarius Strobl 		if (!(ifp->if_flags & IFF_UP) &&
621a7ee7a7dSMarius Strobl 		    ifp->if_drv_flags & IFF_DRV_RUNNING) {
622a7ee7a7dSMarius Strobl 			/*
623a7ee7a7dSMarius Strobl 			 * If interface is marked down and it is running, then
624a7ee7a7dSMarius Strobl 			 * stop it.
625a7ee7a7dSMarius Strobl 			 */
626a7ee7a7dSMarius Strobl 			lance_stop(sc);
627a7ee7a7dSMarius Strobl 		} else if (ifp->if_flags & IFF_UP &&
628a7ee7a7dSMarius Strobl 		    !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
629a7ee7a7dSMarius Strobl 			/*
630a7ee7a7dSMarius Strobl 			 * If interface is marked up and it is stopped, then
631a7ee7a7dSMarius Strobl 			 * start it.
632a7ee7a7dSMarius Strobl 			 */
633a7ee7a7dSMarius Strobl 			lance_init_locked(sc);
634a7ee7a7dSMarius Strobl 		}
635a7ee7a7dSMarius Strobl #ifdef LEDEBUG
636a7ee7a7dSMarius Strobl 		if (ifp->if_flags & IFF_DEBUG)
637a7ee7a7dSMarius Strobl 			sc->sc_flags |= LE_DEBUG;
638a7ee7a7dSMarius Strobl 		else
639a7ee7a7dSMarius Strobl 			sc->sc_flags &= ~LE_DEBUG;
640a7ee7a7dSMarius Strobl #endif
641a7ee7a7dSMarius Strobl 		LE_UNLOCK(sc);
642a7ee7a7dSMarius Strobl 		break;
643a7ee7a7dSMarius Strobl 
644a7ee7a7dSMarius Strobl 	case SIOCADDMULTI:
645a7ee7a7dSMarius Strobl 	case SIOCDELMULTI:
646a7ee7a7dSMarius Strobl 		/*
647a7ee7a7dSMarius Strobl 		 * Multicast list has changed; set the hardware filter
648a7ee7a7dSMarius Strobl 		 * accordingly.
649a7ee7a7dSMarius Strobl 		 */
650a7ee7a7dSMarius Strobl 		LE_LOCK(sc);
651a7ee7a7dSMarius Strobl 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
652a7ee7a7dSMarius Strobl 			lance_init_locked(sc);
653a7ee7a7dSMarius Strobl 		LE_UNLOCK(sc);
654a7ee7a7dSMarius Strobl 		break;
655a7ee7a7dSMarius Strobl 
656a7ee7a7dSMarius Strobl 	case SIOCGIFMEDIA:
657a7ee7a7dSMarius Strobl 	case SIOCSIFMEDIA:
658a7ee7a7dSMarius Strobl 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
659a7ee7a7dSMarius Strobl 		break;
660a7ee7a7dSMarius Strobl 
661a7ee7a7dSMarius Strobl 	default:
662a7ee7a7dSMarius Strobl 		error = ether_ioctl(ifp, cmd, data);
663a7ee7a7dSMarius Strobl 		break;
664a7ee7a7dSMarius Strobl 	}
665a7ee7a7dSMarius Strobl 
666a7ee7a7dSMarius Strobl 	return (error);
667a7ee7a7dSMarius Strobl }
668a7ee7a7dSMarius Strobl 
669a7ee7a7dSMarius Strobl /*
670a7ee7a7dSMarius Strobl  * Set up the logical address filter.
671a7ee7a7dSMarius Strobl  */
672a7ee7a7dSMarius Strobl void
673a7ee7a7dSMarius Strobl lance_setladrf(struct lance_softc *sc, uint16_t *af)
674a7ee7a7dSMarius Strobl {
675a7ee7a7dSMarius Strobl 	struct ifnet *ifp = sc->sc_ifp;
676a7ee7a7dSMarius Strobl 	struct ifmultiaddr *ifma;
677a7ee7a7dSMarius Strobl 	uint32_t crc;
678a7ee7a7dSMarius Strobl 
679a7ee7a7dSMarius Strobl 	/*
680a7ee7a7dSMarius Strobl 	 * Set up multicast address filter by passing all multicast addresses
681a7ee7a7dSMarius Strobl 	 * through a crc generator, and then using the high order 6 bits as an
682a7ee7a7dSMarius Strobl 	 * index into the 64 bit logical address filter.  The high order bit
683a7ee7a7dSMarius Strobl 	 * selects the word, while the rest of the bits select the bit within
684a7ee7a7dSMarius Strobl 	 * the word.
685a7ee7a7dSMarius Strobl 	 */
686a7ee7a7dSMarius Strobl 
687a7ee7a7dSMarius Strobl 	if (ifp->if_flags & IFF_PROMISC || sc->sc_flags & LE_ALLMULTI) {
688a7ee7a7dSMarius Strobl 		af[0] = af[1] = af[2] = af[3] = 0xffff;
689a7ee7a7dSMarius Strobl 		return;
690a7ee7a7dSMarius Strobl 	}
691a7ee7a7dSMarius Strobl 
692a7ee7a7dSMarius Strobl 	af[0] = af[1] = af[2] = af[3] = 0x0000;
693a7ee7a7dSMarius Strobl 	IF_ADDR_LOCK(ifp);
694a7ee7a7dSMarius Strobl 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
695a7ee7a7dSMarius Strobl 		if (ifma->ifma_addr->sa_family != AF_LINK)
696a7ee7a7dSMarius Strobl 			continue;
697a7ee7a7dSMarius Strobl 
698a7ee7a7dSMarius Strobl 		crc = ether_crc32_le(LLADDR((struct sockaddr_dl *)
699a7ee7a7dSMarius Strobl 		    ifma->ifma_addr), ETHER_ADDR_LEN);
700a7ee7a7dSMarius Strobl 
701a7ee7a7dSMarius Strobl 		/* Just want the 6 most significant bits. */
702a7ee7a7dSMarius Strobl 		crc >>= 26;
703a7ee7a7dSMarius Strobl 
704a7ee7a7dSMarius Strobl 		/* Set the corresponding bit in the filter. */
705a7ee7a7dSMarius Strobl 		af[crc >> 4] |= LE_HTOLE16(1U << (crc & 0xf));
706a7ee7a7dSMarius Strobl 	}
707a7ee7a7dSMarius Strobl 	IF_ADDR_UNLOCK(ifp);
708a7ee7a7dSMarius Strobl }
709a7ee7a7dSMarius Strobl 
710a7ee7a7dSMarius Strobl /*
711a7ee7a7dSMarius Strobl  * Routines for accessing the transmit and receive buffers.
712a7ee7a7dSMarius Strobl  * The various CPU and adapter configurations supported by this
713a7ee7a7dSMarius Strobl  * driver require three different access methods for buffers
714a7ee7a7dSMarius Strobl  * and descriptors:
715a7ee7a7dSMarius Strobl  *	(1) contig (contiguous data; no padding),
716a7ee7a7dSMarius Strobl  *	(2) gap2 (two bytes of data followed by two bytes of padding),
717a7ee7a7dSMarius Strobl  *	(3) gap16 (16 bytes of data followed by 16 bytes of padding).
718a7ee7a7dSMarius Strobl  */
719a7ee7a7dSMarius Strobl 
720a7ee7a7dSMarius Strobl /*
721a7ee7a7dSMarius Strobl  * contig: contiguous data with no padding.
722a7ee7a7dSMarius Strobl  *
723a7ee7a7dSMarius Strobl  * Buffers may have any alignment.
724a7ee7a7dSMarius Strobl  */
725a7ee7a7dSMarius Strobl 
726a7ee7a7dSMarius Strobl void
727a7ee7a7dSMarius Strobl lance_copytobuf_contig(struct lance_softc *sc, void *from, int boff, int len)
728a7ee7a7dSMarius Strobl {
729a7ee7a7dSMarius Strobl 	volatile caddr_t buf = sc->sc_mem;
730a7ee7a7dSMarius Strobl 
731a7ee7a7dSMarius Strobl 	/*
732a7ee7a7dSMarius Strobl 	 * Just call memcpy() to do the work.
733a7ee7a7dSMarius Strobl 	 */
734a7ee7a7dSMarius Strobl 	memcpy(buf + boff, from, len);
735a7ee7a7dSMarius Strobl }
736a7ee7a7dSMarius Strobl 
737a7ee7a7dSMarius Strobl void
738a7ee7a7dSMarius Strobl lance_copyfrombuf_contig(struct lance_softc *sc, void *to, int boff, int len)
739a7ee7a7dSMarius Strobl {
740a7ee7a7dSMarius Strobl 	volatile caddr_t buf = sc->sc_mem;
741a7ee7a7dSMarius Strobl 
742a7ee7a7dSMarius Strobl 	/*
743a7ee7a7dSMarius Strobl 	 * Just call memcpy() to do the work.
744a7ee7a7dSMarius Strobl 	 */
745a7ee7a7dSMarius Strobl 	memcpy(to, buf + boff, len);
746a7ee7a7dSMarius Strobl }
747a7ee7a7dSMarius Strobl 
748a7ee7a7dSMarius Strobl void
749a7ee7a7dSMarius Strobl lance_zerobuf_contig(struct lance_softc *sc, int boff, int len)
750a7ee7a7dSMarius Strobl {
751a7ee7a7dSMarius Strobl 	volatile caddr_t buf = sc->sc_mem;
752a7ee7a7dSMarius Strobl 
753a7ee7a7dSMarius Strobl 	/*
754a7ee7a7dSMarius Strobl 	 * Just let memset() do the work
755a7ee7a7dSMarius Strobl 	 */
756a7ee7a7dSMarius Strobl 	memset(buf + boff, 0, len);
757a7ee7a7dSMarius Strobl }
758a7ee7a7dSMarius Strobl 
759a7ee7a7dSMarius Strobl #if 0
760a7ee7a7dSMarius Strobl /*
761a7ee7a7dSMarius Strobl  * Examples only; duplicate these and tweak (if necessary) in
762a7ee7a7dSMarius Strobl  * machine-specific front-ends.
763a7ee7a7dSMarius Strobl  */
764a7ee7a7dSMarius Strobl 
765a7ee7a7dSMarius Strobl /*
766a7ee7a7dSMarius Strobl  * gap2: two bytes of data followed by two bytes of pad.
767a7ee7a7dSMarius Strobl  *
768a7ee7a7dSMarius Strobl  * Buffers must be 4-byte aligned.  The code doesn't worry about
769a7ee7a7dSMarius Strobl  * doing an extra byte.
770a7ee7a7dSMarius Strobl  */
771a7ee7a7dSMarius Strobl 
772a7ee7a7dSMarius Strobl static void
773a7ee7a7dSMarius Strobl lance_copytobuf_gap2(struct lance_softc *sc, void *fromv, int boff, int len)
774a7ee7a7dSMarius Strobl {
775a7ee7a7dSMarius Strobl 	volatile caddr_t buf = sc->sc_mem;
776a7ee7a7dSMarius Strobl 	caddr_t from = fromv;
777a7ee7a7dSMarius Strobl 	volatile uint16_t *bptr;
778a7ee7a7dSMarius Strobl 
779a7ee7a7dSMarius Strobl 	if (boff & 0x1) {
780a7ee7a7dSMarius Strobl 		/* Handle unaligned first byte. */
781a7ee7a7dSMarius Strobl 		bptr = ((volatile uint16_t *)buf) + (boff - 1);
782a7ee7a7dSMarius Strobl 		*bptr = (*from++ << 8) | (*bptr & 0xff);
783a7ee7a7dSMarius Strobl 		bptr += 2;
784a7ee7a7dSMarius Strobl 		len--;
785a7ee7a7dSMarius Strobl 	} else
786a7ee7a7dSMarius Strobl 		bptr = ((volatile uint16_t *)buf) + boff;
787a7ee7a7dSMarius Strobl 	while (len > 1) {
788a7ee7a7dSMarius Strobl 		*bptr = (from[1] << 8) | (from[0] & 0xff);
789a7ee7a7dSMarius Strobl 		bptr += 2;
790a7ee7a7dSMarius Strobl 		from += 2;
791a7ee7a7dSMarius Strobl 		len -= 2;
792a7ee7a7dSMarius Strobl 	}
793a7ee7a7dSMarius Strobl 	if (len == 1)
794a7ee7a7dSMarius Strobl 		*bptr = (uint16_t)*from;
795a7ee7a7dSMarius Strobl }
796a7ee7a7dSMarius Strobl 
797a7ee7a7dSMarius Strobl static void
798a7ee7a7dSMarius Strobl lance_copyfrombuf_gap2(struct lance_softc *sc, void *tov, int boff, int len)
799a7ee7a7dSMarius Strobl {
800a7ee7a7dSMarius Strobl 	volatile caddr_t buf = sc->sc_mem;
801a7ee7a7dSMarius Strobl 	caddr_t to = tov;
802a7ee7a7dSMarius Strobl 	volatile uint16_t *bptr;
803a7ee7a7dSMarius Strobl 	uint16_t tmp;
804a7ee7a7dSMarius Strobl 
805a7ee7a7dSMarius Strobl 	if (boff & 0x1) {
806a7ee7a7dSMarius Strobl 		/* Handle unaligned first byte. */
807a7ee7a7dSMarius Strobl 		bptr = ((volatile uint16_t *)buf) + (boff - 1);
808a7ee7a7dSMarius Strobl 		*to++ = (*bptr >> 8) & 0xff;
809a7ee7a7dSMarius Strobl 		bptr += 2;
810a7ee7a7dSMarius Strobl 		len--;
811a7ee7a7dSMarius Strobl 	} else
812a7ee7a7dSMarius Strobl 		bptr = ((volatile uint16_t *)buf) + boff;
813a7ee7a7dSMarius Strobl 	while (len > 1) {
814a7ee7a7dSMarius Strobl 		tmp = *bptr;
815a7ee7a7dSMarius Strobl 		*to++ = tmp & 0xff;
816a7ee7a7dSMarius Strobl 		*to++ = (tmp >> 8) & 0xff;
817a7ee7a7dSMarius Strobl 		bptr += 2;
818a7ee7a7dSMarius Strobl 		len -= 2;
819a7ee7a7dSMarius Strobl 	}
820a7ee7a7dSMarius Strobl 	if (len == 1)
821a7ee7a7dSMarius Strobl 		*to = *bptr & 0xff;
822a7ee7a7dSMarius Strobl }
823a7ee7a7dSMarius Strobl 
824a7ee7a7dSMarius Strobl static void
825a7ee7a7dSMarius Strobl lance_zerobuf_gap2(struct lance_softc *sc, int boff, int len)
826a7ee7a7dSMarius Strobl {
827a7ee7a7dSMarius Strobl 	volatile caddr_t buf = sc->sc_mem;
828a7ee7a7dSMarius Strobl 	volatile uint16_t *bptr;
829a7ee7a7dSMarius Strobl 
830a7ee7a7dSMarius Strobl 	if ((unsigned)boff & 0x1) {
831a7ee7a7dSMarius Strobl 		bptr = ((volatile uint16_t *)buf) + (boff - 1);
832a7ee7a7dSMarius Strobl 		*bptr &= 0xff;
833a7ee7a7dSMarius Strobl 		bptr += 2;
834a7ee7a7dSMarius Strobl 		len--;
835a7ee7a7dSMarius Strobl 	} else
836a7ee7a7dSMarius Strobl 		bptr = ((volatile uint16_t *)buf) + boff;
837a7ee7a7dSMarius Strobl 	while (len > 0) {
838a7ee7a7dSMarius Strobl 		*bptr = 0;
839a7ee7a7dSMarius Strobl 		bptr += 2;
840a7ee7a7dSMarius Strobl 		len -= 2;
841a7ee7a7dSMarius Strobl 	}
842a7ee7a7dSMarius Strobl }
843a7ee7a7dSMarius Strobl 
844a7ee7a7dSMarius Strobl /*
845a7ee7a7dSMarius Strobl  * gap16: 16 bytes of data followed by 16 bytes of pad.
846a7ee7a7dSMarius Strobl  *
847a7ee7a7dSMarius Strobl  * Buffers must be 32-byte aligned.
848a7ee7a7dSMarius Strobl  */
849a7ee7a7dSMarius Strobl 
850a7ee7a7dSMarius Strobl static void
851a7ee7a7dSMarius Strobl lance_copytobuf_gap16(struct lance_softc *sc, void *fromv, int boff, int len)
852a7ee7a7dSMarius Strobl {
853a7ee7a7dSMarius Strobl 	volatile caddr_t buf = sc->sc_mem;
854a7ee7a7dSMarius Strobl 	caddr_t bptr, from = fromv;
855a7ee7a7dSMarius Strobl 	int xfer;
856a7ee7a7dSMarius Strobl 
857a7ee7a7dSMarius Strobl 	bptr = buf + ((boff << 1) & ~0x1f);
858a7ee7a7dSMarius Strobl 	boff &= 0xf;
859a7ee7a7dSMarius Strobl 	xfer = min(len, 16 - boff);
860a7ee7a7dSMarius Strobl 	while (len > 0) {
861a7ee7a7dSMarius Strobl 		memcpy(bptr + boff, from, xfer);
862a7ee7a7dSMarius Strobl 		from += xfer;
863a7ee7a7dSMarius Strobl 		bptr += 32;
864a7ee7a7dSMarius Strobl 		boff = 0;
865a7ee7a7dSMarius Strobl 		len -= xfer;
866a7ee7a7dSMarius Strobl 		xfer = min(len, 16);
867a7ee7a7dSMarius Strobl 	}
868a7ee7a7dSMarius Strobl }
869a7ee7a7dSMarius Strobl 
870a7ee7a7dSMarius Strobl static void
871a7ee7a7dSMarius Strobl lance_copyfrombuf_gap16(struct lance_softc *sc, void *tov, int boff, int len)
872a7ee7a7dSMarius Strobl {
873a7ee7a7dSMarius Strobl 	volatile caddr_t buf = sc->sc_mem;
874a7ee7a7dSMarius Strobl 	caddr_t bptr, to = tov;
875a7ee7a7dSMarius Strobl 	int xfer;
876a7ee7a7dSMarius Strobl 
877a7ee7a7dSMarius Strobl 	bptr = buf + ((boff << 1) & ~0x1f);
878a7ee7a7dSMarius Strobl 	boff &= 0xf;
879a7ee7a7dSMarius Strobl 	xfer = min(len, 16 - boff);
880a7ee7a7dSMarius Strobl 	while (len > 0) {
881a7ee7a7dSMarius Strobl 		memcpy(to, bptr + boff, xfer);
882a7ee7a7dSMarius Strobl 		to += xfer;
883a7ee7a7dSMarius Strobl 		bptr += 32;
884a7ee7a7dSMarius Strobl 		boff = 0;
885a7ee7a7dSMarius Strobl 		len -= xfer;
886a7ee7a7dSMarius Strobl 		xfer = min(len, 16);
887a7ee7a7dSMarius Strobl 	}
888a7ee7a7dSMarius Strobl }
889a7ee7a7dSMarius Strobl 
890a7ee7a7dSMarius Strobl static void
891a7ee7a7dSMarius Strobl lance_zerobuf_gap16(struct lance_softc *sc, int boff, int len)
892a7ee7a7dSMarius Strobl {
893a7ee7a7dSMarius Strobl 	volatile caddr_t buf = sc->sc_mem;
894a7ee7a7dSMarius Strobl 	caddr_t bptr;
895a7ee7a7dSMarius Strobl 	int xfer;
896a7ee7a7dSMarius Strobl 
897a7ee7a7dSMarius Strobl 	bptr = buf + ((boff << 1) & ~0x1f);
898a7ee7a7dSMarius Strobl 	boff &= 0xf;
899a7ee7a7dSMarius Strobl 	xfer = min(len, 16 - boff);
900a7ee7a7dSMarius Strobl 	while (len > 0) {
901a7ee7a7dSMarius Strobl 		memset(bptr + boff, 0, xfer);
902a7ee7a7dSMarius Strobl 		bptr += 32;
903a7ee7a7dSMarius Strobl 		boff = 0;
904a7ee7a7dSMarius Strobl 		len -= xfer;
905a7ee7a7dSMarius Strobl 		xfer = min(len, 16);
906a7ee7a7dSMarius Strobl 	}
907a7ee7a7dSMarius Strobl }
908a7ee7a7dSMarius Strobl #endif /* Example only */
909