lance.c revision 1.37 1 1.37 christos /* $NetBSD: lance.c,v 1.37 2006/11/16 01:32:51 christos Exp $ */
2 1.1 drochner
3 1.1 drochner /*-
4 1.3 mycroft * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
5 1.1 drochner * All rights reserved.
6 1.1 drochner *
7 1.1 drochner * This code is derived from software contributed to The NetBSD Foundation
8 1.3 mycroft * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
9 1.3 mycroft * Simulation Facility, NASA Ames Research Center.
10 1.1 drochner *
11 1.1 drochner * Redistribution and use in source and binary forms, with or without
12 1.1 drochner * modification, are permitted provided that the following conditions
13 1.1 drochner * are met:
14 1.1 drochner * 1. Redistributions of source code must retain the above copyright
15 1.1 drochner * notice, this list of conditions and the following disclaimer.
16 1.1 drochner * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 drochner * notice, this list of conditions and the following disclaimer in the
18 1.1 drochner * documentation and/or other materials provided with the distribution.
19 1.1 drochner * 3. All advertising materials mentioning features or use of this software
20 1.1 drochner * must display the following acknowledgement:
21 1.1 drochner * This product includes software developed by the NetBSD
22 1.1 drochner * Foundation, Inc. and its contributors.
23 1.1 drochner * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.1 drochner * contributors may be used to endorse or promote products derived
25 1.1 drochner * from this software without specific prior written permission.
26 1.1 drochner *
27 1.1 drochner * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.1 drochner * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.1 drochner * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.1 drochner * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.1 drochner * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.1 drochner * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.1 drochner * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.1 drochner * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.1 drochner * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.1 drochner * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.1 drochner * POSSIBILITY OF SUCH DAMAGE.
38 1.1 drochner */
39 1.1 drochner
40 1.1 drochner /*-
41 1.1 drochner * Copyright (c) 1992, 1993
42 1.1 drochner * The Regents of the University of California. All rights reserved.
43 1.1 drochner *
44 1.1 drochner * This code is derived from software contributed to Berkeley by
45 1.1 drochner * Ralph Campbell and Rick Macklem.
46 1.1 drochner *
47 1.1 drochner * Redistribution and use in source and binary forms, with or without
48 1.1 drochner * modification, are permitted provided that the following conditions
49 1.1 drochner * are met:
50 1.1 drochner * 1. Redistributions of source code must retain the above copyright
51 1.1 drochner * notice, this list of conditions and the following disclaimer.
52 1.1 drochner * 2. Redistributions in binary form must reproduce the above copyright
53 1.1 drochner * notice, this list of conditions and the following disclaimer in the
54 1.1 drochner * documentation and/or other materials provided with the distribution.
55 1.29 agc * 3. Neither the name of the University nor the names of its contributors
56 1.1 drochner * may be used to endorse or promote products derived from this software
57 1.1 drochner * without specific prior written permission.
58 1.1 drochner *
59 1.1 drochner * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 1.1 drochner * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 1.1 drochner * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 1.1 drochner * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 1.1 drochner * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 1.1 drochner * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 1.1 drochner * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 1.1 drochner * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 1.1 drochner * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 1.1 drochner * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 1.1 drochner * SUCH DAMAGE.
70 1.1 drochner *
71 1.1 drochner * @(#)if_le.c 8.2 (Berkeley) 11/16/93
72 1.1 drochner */
73 1.26 lukem
74 1.26 lukem #include <sys/cdefs.h>
75 1.37 christos __KERNEL_RCSID(0, "$NetBSD: lance.c,v 1.37 2006/11/16 01:32:51 christos Exp $");
76 1.1 drochner
77 1.1 drochner #include "bpfilter.h"
78 1.1 drochner #include "rnd.h"
79 1.1 drochner
80 1.1 drochner #include <sys/param.h>
81 1.1 drochner #include <sys/systm.h>
82 1.32 perry #include <sys/mbuf.h>
83 1.1 drochner #include <sys/syslog.h>
84 1.1 drochner #include <sys/socket.h>
85 1.1 drochner #include <sys/device.h>
86 1.1 drochner #include <sys/malloc.h>
87 1.1 drochner #include <sys/ioctl.h>
88 1.1 drochner #include <sys/errno.h>
89 1.1 drochner #if NRND > 0
90 1.1 drochner #include <sys/rnd.h>
91 1.1 drochner #endif
92 1.1 drochner
93 1.1 drochner #include <net/if.h>
94 1.1 drochner #include <net/if_dl.h>
95 1.1 drochner #include <net/if_ether.h>
96 1.1 drochner #include <net/if_media.h>
97 1.1 drochner
98 1.1 drochner
99 1.1 drochner #if NBPFILTER > 0
100 1.1 drochner #include <net/bpf.h>
101 1.1 drochner #include <net/bpfdesc.h>
102 1.1 drochner #endif
103 1.1 drochner
104 1.1 drochner #include <dev/ic/lancereg.h>
105 1.1 drochner #include <dev/ic/lancevar.h>
106 1.1 drochner
107 1.19 mrg #if defined(_KERNEL_OPT)
108 1.1 drochner #include "opt_ddb.h"
109 1.1 drochner #endif
110 1.1 drochner
111 1.1 drochner #ifdef DDB
112 1.1 drochner #define integrate
113 1.1 drochner #define hide
114 1.1 drochner #else
115 1.34 perry #define integrate static inline
116 1.1 drochner #define hide static
117 1.1 drochner #endif
118 1.1 drochner
119 1.31 perry integrate struct mbuf *lance_get(struct lance_softc *, int, int);
120 1.1 drochner
121 1.31 perry hide void lance_shutdown(void *);
122 1.1 drochner
123 1.31 perry int lance_mediachange(struct ifnet *);
124 1.31 perry void lance_mediastatus(struct ifnet *, struct ifmediareq *);
125 1.1 drochner
126 1.31 perry static inline u_int16_t ether_cmp(void *, void *);
127 1.1 drochner
128 1.31 perry void lance_stop(struct ifnet *, int);
129 1.31 perry int lance_ioctl(struct ifnet *, u_long, caddr_t);
130 1.31 perry void lance_watchdog(struct ifnet *);
131 1.1 drochner
132 1.1 drochner /*
133 1.1 drochner * Compare two Ether/802 addresses for equality, inlined and
134 1.23 thorpej * unrolled for speed. Use this like memcmp().
135 1.1 drochner *
136 1.1 drochner * XXX: Add <machine/inlines.h> for stuff like this?
137 1.1 drochner * XXX: or maybe add it to libkern.h instead?
138 1.1 drochner *
139 1.1 drochner * "I'd love to have an inline assembler version of this."
140 1.1 drochner * XXX: Who wanted that? mycroft? I wrote one, but this
141 1.1 drochner * version in C is as good as hand-coded assembly. -gwr
142 1.1 drochner *
143 1.1 drochner * Please do NOT tweak this without looking at the actual
144 1.1 drochner * assembly code generated before and after your tweaks!
145 1.1 drochner */
146 1.1 drochner static inline u_int16_t
147 1.1 drochner ether_cmp(one, two)
148 1.1 drochner void *one, *two;
149 1.1 drochner {
150 1.11 augustss u_int16_t *a = (u_short *) one;
151 1.11 augustss u_int16_t *b = (u_short *) two;
152 1.11 augustss u_int16_t diff;
153 1.1 drochner
154 1.1 drochner #ifdef m68k
155 1.1 drochner /*
156 1.1 drochner * The post-increment-pointer form produces the best
157 1.1 drochner * machine code for m68k. This was carefully tuned
158 1.1 drochner * so it compiles to just 8 short (2-byte) op-codes!
159 1.1 drochner */
160 1.1 drochner diff = *a++ - *b++;
161 1.1 drochner diff |= *a++ - *b++;
162 1.1 drochner diff |= *a++ - *b++;
163 1.1 drochner #else
164 1.1 drochner /*
165 1.1 drochner * Most modern CPUs do better with a single expresion.
166 1.1 drochner * Note that short-cut evaluation is NOT helpful here,
167 1.1 drochner * because it just makes the code longer, not faster!
168 1.1 drochner */
169 1.1 drochner diff = (a[0] - b[0]) | (a[1] - b[1]) | (a[2] - b[2]);
170 1.1 drochner #endif
171 1.1 drochner
172 1.1 drochner return (diff);
173 1.1 drochner }
174 1.1 drochner
175 1.1 drochner #define ETHER_CMP ether_cmp
176 1.1 drochner
177 1.1 drochner #ifdef LANCE_REVC_BUG
178 1.1 drochner /* Make sure this is short-aligned, for ether_cmp(). */
179 1.1 drochner static u_int16_t bcast_enaddr[3] = { ~0, ~0, ~0 };
180 1.1 drochner #endif
181 1.1 drochner
182 1.1 drochner void
183 1.1 drochner lance_config(sc)
184 1.1 drochner struct lance_softc *sc;
185 1.1 drochner {
186 1.18 jdolecek int i, nbuf;
187 1.20 jdolecek struct ifnet *ifp = &sc->sc_ethercom.ec_if;
188 1.1 drochner
189 1.1 drochner /* Initialize ifnet structure. */
190 1.24 thorpej strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
191 1.1 drochner ifp->if_softc = sc;
192 1.1 drochner ifp->if_start = sc->sc_start;
193 1.1 drochner ifp->if_ioctl = lance_ioctl;
194 1.1 drochner ifp->if_watchdog = lance_watchdog;
195 1.20 jdolecek ifp->if_init = lance_init;
196 1.20 jdolecek ifp->if_stop = lance_stop;
197 1.1 drochner ifp->if_flags =
198 1.1 drochner IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
199 1.1 drochner #ifdef LANCE_REVC_BUG
200 1.1 drochner ifp->if_flags &= ~IFF_MULTICAST;
201 1.1 drochner #endif
202 1.17 thorpej IFQ_SET_READY(&ifp->if_snd);
203 1.1 drochner
204 1.1 drochner /* Initialize ifmedia structures. */
205 1.1 drochner ifmedia_init(&sc->sc_media, 0, lance_mediachange, lance_mediastatus);
206 1.1 drochner if (sc->sc_supmedia != NULL) {
207 1.1 drochner for (i = 0; i < sc->sc_nsupmedia; i++)
208 1.1 drochner ifmedia_add(&sc->sc_media, sc->sc_supmedia[i],
209 1.1 drochner 0, NULL);
210 1.1 drochner ifmedia_set(&sc->sc_media, sc->sc_defaultmedia);
211 1.1 drochner } else {
212 1.1 drochner ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
213 1.1 drochner ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
214 1.1 drochner }
215 1.1 drochner
216 1.1 drochner switch (sc->sc_memsize) {
217 1.1 drochner case 8192:
218 1.1 drochner sc->sc_nrbuf = 4;
219 1.1 drochner sc->sc_ntbuf = 1;
220 1.1 drochner break;
221 1.1 drochner case 16384:
222 1.1 drochner sc->sc_nrbuf = 8;
223 1.1 drochner sc->sc_ntbuf = 2;
224 1.1 drochner break;
225 1.1 drochner case 32768:
226 1.1 drochner sc->sc_nrbuf = 16;
227 1.1 drochner sc->sc_ntbuf = 4;
228 1.1 drochner break;
229 1.1 drochner case 65536:
230 1.1 drochner sc->sc_nrbuf = 32;
231 1.1 drochner sc->sc_ntbuf = 8;
232 1.1 drochner break;
233 1.1 drochner case 131072:
234 1.1 drochner sc->sc_nrbuf = 64;
235 1.1 drochner sc->sc_ntbuf = 16;
236 1.4 leo break;
237 1.4 leo case 262144:
238 1.4 leo sc->sc_nrbuf = 128;
239 1.4 leo sc->sc_ntbuf = 32;
240 1.1 drochner break;
241 1.1 drochner default:
242 1.18 jdolecek /* weird memory size; cope with it */
243 1.18 jdolecek nbuf = sc->sc_memsize / LEBLEN;
244 1.18 jdolecek sc->sc_ntbuf = nbuf / 5;
245 1.18 jdolecek sc->sc_nrbuf = nbuf - sc->sc_ntbuf;
246 1.1 drochner }
247 1.1 drochner
248 1.1 drochner printf(": address %s\n", ether_sprintf(sc->sc_enaddr));
249 1.1 drochner printf("%s: %d receive buffers, %d transmit buffers\n",
250 1.1 drochner sc->sc_dev.dv_xname, sc->sc_nrbuf, sc->sc_ntbuf);
251 1.10 simonb
252 1.20 jdolecek /* Make sure the chip is stopped. */
253 1.20 jdolecek lance_stop(ifp, 0);
254 1.20 jdolecek
255 1.14 bouyer /* claim 802.1q capability */
256 1.14 bouyer sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
257 1.10 simonb /* Attach the interface. */
258 1.10 simonb if_attach(ifp);
259 1.10 simonb ether_ifattach(ifp, sc->sc_enaddr);
260 1.1 drochner
261 1.20 jdolecek sc->sc_sh = shutdownhook_establish(lance_shutdown, ifp);
262 1.1 drochner if (sc->sc_sh == NULL)
263 1.1 drochner panic("lance_config: can't establish shutdownhook");
264 1.1 drochner sc->sc_rbufaddr = malloc(sc->sc_nrbuf * sizeof(int), M_DEVBUF,
265 1.1 drochner M_WAITOK);
266 1.1 drochner sc->sc_tbufaddr = malloc(sc->sc_ntbuf * sizeof(int), M_DEVBUF,
267 1.1 drochner M_WAITOK);
268 1.1 drochner
269 1.1 drochner #if NRND > 0
270 1.1 drochner rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
271 1.7 explorer RND_TYPE_NET, 0);
272 1.1 drochner #endif
273 1.1 drochner }
274 1.1 drochner
275 1.1 drochner void
276 1.1 drochner lance_reset(sc)
277 1.1 drochner struct lance_softc *sc;
278 1.1 drochner {
279 1.1 drochner int s;
280 1.1 drochner
281 1.2 mycroft s = splnet();
282 1.20 jdolecek lance_init(&sc->sc_ethercom.ec_if);
283 1.1 drochner splx(s);
284 1.1 drochner }
285 1.1 drochner
286 1.1 drochner void
287 1.37 christos lance_stop(struct ifnet *ifp, int disable)
288 1.1 drochner {
289 1.20 jdolecek struct lance_softc *sc = ifp->if_softc;
290 1.1 drochner
291 1.1 drochner (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_STOP);
292 1.1 drochner }
293 1.1 drochner
294 1.1 drochner /*
295 1.1 drochner * Initialization of interface; set up initialization block
296 1.1 drochner * and transmit/receive descriptor rings.
297 1.1 drochner */
298 1.20 jdolecek int
299 1.20 jdolecek lance_init(ifp)
300 1.20 jdolecek struct ifnet *ifp;
301 1.1 drochner {
302 1.20 jdolecek struct lance_softc *sc = ifp->if_softc;
303 1.11 augustss int timo;
304 1.1 drochner u_long a;
305 1.1 drochner
306 1.1 drochner (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_STOP);
307 1.1 drochner DELAY(100);
308 1.1 drochner
309 1.1 drochner /* Newer LANCE chips have a reset register */
310 1.1 drochner if (sc->sc_hwreset)
311 1.1 drochner (*sc->sc_hwreset)(sc);
312 1.1 drochner
313 1.1 drochner /* Set the correct byte swapping mode, etc. */
314 1.1 drochner (*sc->sc_wrcsr)(sc, LE_CSR3, sc->sc_conf3);
315 1.1 drochner
316 1.1 drochner /* Set up LANCE init block. */
317 1.1 drochner (*sc->sc_meminit)(sc);
318 1.1 drochner
319 1.1 drochner /* Give LANCE the physical address of its init block. */
320 1.1 drochner a = sc->sc_addr + LE_INITADDR(sc);
321 1.1 drochner (*sc->sc_wrcsr)(sc, LE_CSR1, a);
322 1.1 drochner (*sc->sc_wrcsr)(sc, LE_CSR2, a >> 16);
323 1.1 drochner
324 1.1 drochner /* Try to initialize the LANCE. */
325 1.1 drochner DELAY(100);
326 1.1 drochner (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INIT);
327 1.1 drochner
328 1.1 drochner /* Wait for initialization to finish. */
329 1.1 drochner for (timo = 100000; timo; timo--)
330 1.1 drochner if ((*sc->sc_rdcsr)(sc, LE_CSR0) & LE_C0_IDON)
331 1.1 drochner break;
332 1.1 drochner
333 1.1 drochner if ((*sc->sc_rdcsr)(sc, LE_CSR0) & LE_C0_IDON) {
334 1.1 drochner /* Start the LANCE. */
335 1.28 pk (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA | LE_C0_STRT);
336 1.1 drochner ifp->if_flags |= IFF_RUNNING;
337 1.1 drochner ifp->if_flags &= ~IFF_OACTIVE;
338 1.1 drochner ifp->if_timer = 0;
339 1.1 drochner (*sc->sc_start)(ifp);
340 1.1 drochner } else
341 1.1 drochner printf("%s: controller failed to initialize\n",
342 1.1 drochner sc->sc_dev.dv_xname);
343 1.1 drochner if (sc->sc_hwinit)
344 1.1 drochner (*sc->sc_hwinit)(sc);
345 1.20 jdolecek
346 1.20 jdolecek return (0);
347 1.1 drochner }
348 1.1 drochner
349 1.1 drochner /*
350 1.1 drochner * Routine to copy from mbuf chain to transmit buffer in
351 1.1 drochner * network buffer memory.
352 1.1 drochner */
353 1.1 drochner int
354 1.1 drochner lance_put(sc, boff, m)
355 1.1 drochner struct lance_softc *sc;
356 1.1 drochner int boff;
357 1.11 augustss struct mbuf *m;
358 1.1 drochner {
359 1.11 augustss struct mbuf *n;
360 1.11 augustss int len, tlen = 0;
361 1.1 drochner
362 1.1 drochner for (; m; m = n) {
363 1.1 drochner len = m->m_len;
364 1.1 drochner if (len == 0) {
365 1.1 drochner MFREE(m, n);
366 1.1 drochner continue;
367 1.1 drochner }
368 1.1 drochner (*sc->sc_copytobuf)(sc, mtod(m, caddr_t), boff, len);
369 1.1 drochner boff += len;
370 1.1 drochner tlen += len;
371 1.1 drochner MFREE(m, n);
372 1.1 drochner }
373 1.1 drochner if (tlen < LEMINSIZE) {
374 1.1 drochner (*sc->sc_zerobuf)(sc, boff, LEMINSIZE - tlen);
375 1.1 drochner tlen = LEMINSIZE;
376 1.1 drochner }
377 1.1 drochner return (tlen);
378 1.1 drochner }
379 1.1 drochner
380 1.1 drochner /*
381 1.1 drochner * Pull data off an interface.
382 1.1 drochner * Len is length of data, with local net header stripped.
383 1.1 drochner * We copy the data into mbufs. When full cluster sized units are present
384 1.1 drochner * we copy into clusters.
385 1.1 drochner */
386 1.1 drochner integrate struct mbuf *
387 1.1 drochner lance_get(sc, boff, totlen)
388 1.1 drochner struct lance_softc *sc;
389 1.1 drochner int boff, totlen;
390 1.1 drochner {
391 1.5 mycroft struct mbuf *m, *m0, *newm;
392 1.1 drochner int len;
393 1.1 drochner
394 1.5 mycroft MGETHDR(m0, M_DONTWAIT, MT_DATA);
395 1.5 mycroft if (m0 == 0)
396 1.1 drochner return (0);
397 1.20 jdolecek m0->m_pkthdr.rcvif = &sc->sc_ethercom.ec_if;
398 1.5 mycroft m0->m_pkthdr.len = totlen;
399 1.1 drochner len = MHLEN;
400 1.5 mycroft m = m0;
401 1.1 drochner
402 1.1 drochner while (totlen > 0) {
403 1.1 drochner if (totlen >= MINCLSIZE) {
404 1.1 drochner MCLGET(m, M_DONTWAIT);
405 1.5 mycroft if ((m->m_flags & M_EXT) == 0)
406 1.5 mycroft goto bad;
407 1.1 drochner len = MCLBYTES;
408 1.1 drochner }
409 1.5 mycroft
410 1.5 mycroft if (m == m0) {
411 1.5 mycroft caddr_t newdata = (caddr_t)
412 1.5 mycroft ALIGN(m->m_data + sizeof(struct ether_header)) -
413 1.5 mycroft sizeof(struct ether_header);
414 1.5 mycroft len -= newdata - m->m_data;
415 1.5 mycroft m->m_data = newdata;
416 1.1 drochner }
417 1.5 mycroft
418 1.1 drochner m->m_len = len = min(totlen, len);
419 1.1 drochner (*sc->sc_copyfrombuf)(sc, mtod(m, caddr_t), boff, len);
420 1.1 drochner boff += len;
421 1.5 mycroft
422 1.1 drochner totlen -= len;
423 1.5 mycroft if (totlen > 0) {
424 1.5 mycroft MGET(newm, M_DONTWAIT, MT_DATA);
425 1.5 mycroft if (newm == 0)
426 1.5 mycroft goto bad;
427 1.5 mycroft len = MLEN;
428 1.5 mycroft m = m->m_next = newm;
429 1.5 mycroft }
430 1.1 drochner }
431 1.1 drochner
432 1.5 mycroft return (m0);
433 1.5 mycroft
434 1.5 mycroft bad:
435 1.5 mycroft m_freem(m0);
436 1.5 mycroft return (0);
437 1.1 drochner }
438 1.1 drochner
439 1.1 drochner /*
440 1.1 drochner * Pass a packet to the higher levels.
441 1.1 drochner */
442 1.1 drochner void
443 1.1 drochner lance_read(sc, boff, len)
444 1.11 augustss struct lance_softc *sc;
445 1.1 drochner int boff, len;
446 1.1 drochner {
447 1.1 drochner struct mbuf *m;
448 1.20 jdolecek struct ifnet *ifp = &sc->sc_ethercom.ec_if;
449 1.15 tsutsui struct ether_header *eh;
450 1.1 drochner
451 1.1 drochner if (len <= sizeof(struct ether_header) ||
452 1.14 bouyer len > ((sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) ?
453 1.14 bouyer ETHER_VLAN_ENCAP_LEN + ETHERMTU + sizeof(struct ether_header) :
454 1.14 bouyer ETHERMTU + sizeof(struct ether_header))) {
455 1.1 drochner #ifdef LEDEBUG
456 1.1 drochner printf("%s: invalid packet size %d; dropping\n",
457 1.1 drochner sc->sc_dev.dv_xname, len);
458 1.1 drochner #endif
459 1.1 drochner ifp->if_ierrors++;
460 1.1 drochner return;
461 1.1 drochner }
462 1.1 drochner
463 1.1 drochner /* Pull packet off interface. */
464 1.1 drochner m = lance_get(sc, boff, len);
465 1.1 drochner if (m == 0) {
466 1.1 drochner ifp->if_ierrors++;
467 1.1 drochner return;
468 1.1 drochner }
469 1.1 drochner
470 1.1 drochner ifp->if_ipackets++;
471 1.1 drochner
472 1.27 itojun eh = mtod(m, struct ether_header *);
473 1.1 drochner
474 1.1 drochner #ifdef LANCE_REVC_BUG
475 1.1 drochner /*
476 1.1 drochner * The old LANCE (Rev. C) chips have a bug which causes
477 1.1 drochner * garbage to be inserted in front of the received packet.
478 1.1 drochner * The work-around is to ignore packets with an invalid
479 1.1 drochner * destination address (garbage will usually not match).
480 1.1 drochner * Of course, this precludes multicast support...
481 1.1 drochner */
482 1.1 drochner if (ETHER_CMP(eh->ether_dhost, sc->sc_enaddr) &&
483 1.1 drochner ETHER_CMP(eh->ether_dhost, bcast_enaddr)) {
484 1.1 drochner m_freem(m);
485 1.1 drochner return;
486 1.1 drochner }
487 1.27 itojun #endif
488 1.27 itojun
489 1.27 itojun /*
490 1.27 itojun * Some lance device does not present IFF_SIMPLEX behavior on multicast
491 1.27 itojun * packets. Make sure to drop it if it is from ourselves.
492 1.27 itojun */
493 1.27 itojun if (!ETHER_CMP(eh->ether_shost, sc->sc_enaddr)) {
494 1.27 itojun m_freem(m);
495 1.27 itojun return;
496 1.27 itojun }
497 1.27 itojun
498 1.27 itojun #if NBPFILTER > 0
499 1.27 itojun /*
500 1.27 itojun * Check if there's a BPF listener on this interface.
501 1.27 itojun * If so, hand off the raw packet to BPF.
502 1.27 itojun */
503 1.27 itojun if (ifp->if_bpf)
504 1.27 itojun bpf_mtap(ifp->if_bpf, m);
505 1.1 drochner #endif
506 1.1 drochner
507 1.9 thorpej /* Pass the packet up. */
508 1.9 thorpej (*ifp->if_input)(ifp, m);
509 1.1 drochner }
510 1.1 drochner
511 1.1 drochner #undef ifp
512 1.1 drochner
513 1.1 drochner void
514 1.1 drochner lance_watchdog(ifp)
515 1.1 drochner struct ifnet *ifp;
516 1.1 drochner {
517 1.1 drochner struct lance_softc *sc = ifp->if_softc;
518 1.1 drochner
519 1.1 drochner log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
520 1.1 drochner ++ifp->if_oerrors;
521 1.1 drochner
522 1.1 drochner lance_reset(sc);
523 1.1 drochner }
524 1.1 drochner
525 1.1 drochner int
526 1.1 drochner lance_mediachange(ifp)
527 1.1 drochner struct ifnet *ifp;
528 1.1 drochner {
529 1.1 drochner struct lance_softc *sc = ifp->if_softc;
530 1.1 drochner
531 1.1 drochner if (sc->sc_mediachange)
532 1.1 drochner return ((*sc->sc_mediachange)(sc));
533 1.8 thorpej return (0);
534 1.1 drochner }
535 1.1 drochner
536 1.1 drochner void
537 1.1 drochner lance_mediastatus(ifp, ifmr)
538 1.1 drochner struct ifnet *ifp;
539 1.1 drochner struct ifmediareq *ifmr;
540 1.1 drochner {
541 1.1 drochner struct lance_softc *sc = ifp->if_softc;
542 1.1 drochner
543 1.1 drochner if ((ifp->if_flags & IFF_UP) == 0)
544 1.1 drochner return;
545 1.1 drochner
546 1.1 drochner ifmr->ifm_status = IFM_AVALID;
547 1.1 drochner if (sc->sc_havecarrier)
548 1.1 drochner ifmr->ifm_status |= IFM_ACTIVE;
549 1.1 drochner
550 1.1 drochner if (sc->sc_mediastatus)
551 1.1 drochner (*sc->sc_mediastatus)(sc, ifmr);
552 1.1 drochner }
553 1.1 drochner
554 1.1 drochner /*
555 1.1 drochner * Process an ioctl request.
556 1.1 drochner */
557 1.1 drochner int
558 1.1 drochner lance_ioctl(ifp, cmd, data)
559 1.11 augustss struct ifnet *ifp;
560 1.1 drochner u_long cmd;
561 1.1 drochner caddr_t data;
562 1.1 drochner {
563 1.11 augustss struct lance_softc *sc = ifp->if_softc;
564 1.1 drochner struct ifreq *ifr = (struct ifreq *)data;
565 1.1 drochner int s, error = 0;
566 1.1 drochner
567 1.2 mycroft s = splnet();
568 1.1 drochner
569 1.1 drochner switch (cmd) {
570 1.1 drochner
571 1.1 drochner case SIOCSIFADDR:
572 1.20 jdolecek case SIOCSIFFLAGS:
573 1.20 jdolecek error = ether_ioctl(ifp, cmd, data);
574 1.1 drochner break;
575 1.1 drochner
576 1.1 drochner
577 1.1 drochner case SIOCADDMULTI:
578 1.1 drochner case SIOCDELMULTI:
579 1.1 drochner error = (cmd == SIOCADDMULTI) ?
580 1.1 drochner ether_addmulti(ifr, &sc->sc_ethercom) :
581 1.1 drochner ether_delmulti(ifr, &sc->sc_ethercom);
582 1.1 drochner
583 1.1 drochner if (error == ENETRESET) {
584 1.1 drochner /*
585 1.1 drochner * Multicast list has changed; set the hardware filter
586 1.1 drochner * accordingly.
587 1.1 drochner */
588 1.30 thorpej if (ifp->if_flags & IFF_RUNNING)
589 1.30 thorpej lance_reset(sc);
590 1.1 drochner error = 0;
591 1.1 drochner }
592 1.1 drochner break;
593 1.1 drochner
594 1.1 drochner case SIOCGIFMEDIA:
595 1.1 drochner case SIOCSIFMEDIA:
596 1.1 drochner error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
597 1.1 drochner break;
598 1.1 drochner
599 1.1 drochner default:
600 1.1 drochner error = EINVAL;
601 1.1 drochner break;
602 1.1 drochner }
603 1.1 drochner
604 1.1 drochner splx(s);
605 1.1 drochner return (error);
606 1.1 drochner }
607 1.1 drochner
608 1.1 drochner hide void
609 1.1 drochner lance_shutdown(arg)
610 1.1 drochner void *arg;
611 1.1 drochner {
612 1.1 drochner
613 1.20 jdolecek lance_stop((struct ifnet *)arg, 0);
614 1.1 drochner }
615 1.1 drochner
616 1.1 drochner /*
617 1.1 drochner * Set up the logical address filter.
618 1.1 drochner */
619 1.1 drochner void
620 1.1 drochner lance_setladrf(ac, af)
621 1.1 drochner struct ethercom *ac;
622 1.1 drochner u_int16_t *af;
623 1.1 drochner {
624 1.1 drochner struct ifnet *ifp = &ac->ec_if;
625 1.1 drochner struct ether_multi *enm;
626 1.11 augustss u_int32_t crc;
627 1.1 drochner struct ether_multistep step;
628 1.1 drochner
629 1.1 drochner /*
630 1.1 drochner * Set up multicast address filter by passing all multicast addresses
631 1.1 drochner * through a crc generator, and then using the high order 6 bits as an
632 1.1 drochner * index into the 64 bit logical address filter. The high order bit
633 1.1 drochner * selects the word, while the rest of the bits select the bit within
634 1.1 drochner * the word.
635 1.1 drochner */
636 1.1 drochner
637 1.1 drochner if (ifp->if_flags & IFF_PROMISC)
638 1.1 drochner goto allmulti;
639 1.1 drochner
640 1.1 drochner af[0] = af[1] = af[2] = af[3] = 0x0000;
641 1.1 drochner ETHER_FIRST_MULTI(step, ac, enm);
642 1.1 drochner while (enm != NULL) {
643 1.1 drochner if (ETHER_CMP(enm->enm_addrlo, enm->enm_addrhi)) {
644 1.1 drochner /*
645 1.1 drochner * We must listen to a range of multicast addresses.
646 1.1 drochner * For now, just accept all multicasts, rather than
647 1.1 drochner * trying to set only those filter bits needed to match
648 1.1 drochner * the range. (At this time, the only use of address
649 1.1 drochner * ranges is for IP multicast routing, for which the
650 1.1 drochner * range is big enough to require all bits set.)
651 1.1 drochner */
652 1.1 drochner goto allmulti;
653 1.1 drochner }
654 1.1 drochner
655 1.12 thorpej crc = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
656 1.12 thorpej
657 1.1 drochner /* Just want the 6 most significant bits. */
658 1.1 drochner crc >>= 26;
659 1.1 drochner
660 1.1 drochner /* Set the corresponding bit in the filter. */
661 1.1 drochner af[crc >> 4] |= 1 << (crc & 0xf);
662 1.1 drochner
663 1.1 drochner ETHER_NEXT_MULTI(step, enm);
664 1.1 drochner }
665 1.1 drochner ifp->if_flags &= ~IFF_ALLMULTI;
666 1.1 drochner return;
667 1.1 drochner
668 1.1 drochner allmulti:
669 1.1 drochner ifp->if_flags |= IFF_ALLMULTI;
670 1.1 drochner af[0] = af[1] = af[2] = af[3] = 0xffff;
671 1.1 drochner }
672 1.1 drochner
673 1.1 drochner /*
674 1.1 drochner * Routines for accessing the transmit and receive buffers.
675 1.1 drochner * The various CPU and adapter configurations supported by this
676 1.1 drochner * driver require three different access methods for buffers
677 1.1 drochner * and descriptors:
678 1.1 drochner * (1) contig (contiguous data; no padding),
679 1.1 drochner * (2) gap2 (two bytes of data followed by two bytes of padding),
680 1.1 drochner * (3) gap16 (16 bytes of data followed by 16 bytes of padding).
681 1.1 drochner */
682 1.1 drochner
683 1.1 drochner /*
684 1.1 drochner * contig: contiguous data with no padding.
685 1.1 drochner *
686 1.1 drochner * Buffers may have any alignment.
687 1.1 drochner */
688 1.1 drochner
689 1.1 drochner void
690 1.1 drochner lance_copytobuf_contig(sc, from, boff, len)
691 1.1 drochner struct lance_softc *sc;
692 1.1 drochner void *from;
693 1.1 drochner int boff, len;
694 1.1 drochner {
695 1.1 drochner volatile caddr_t buf = sc->sc_mem;
696 1.1 drochner
697 1.1 drochner /*
698 1.24 thorpej * Just call memcpy() to do the work.
699 1.1 drochner */
700 1.24 thorpej memcpy(buf + boff, from, len);
701 1.1 drochner }
702 1.1 drochner
703 1.1 drochner void
704 1.1 drochner lance_copyfrombuf_contig(sc, to, boff, len)
705 1.1 drochner struct lance_softc *sc;
706 1.1 drochner void *to;
707 1.1 drochner int boff, len;
708 1.1 drochner {
709 1.1 drochner volatile caddr_t buf = sc->sc_mem;
710 1.1 drochner
711 1.1 drochner /*
712 1.24 thorpej * Just call memcpy() to do the work.
713 1.1 drochner */
714 1.24 thorpej memcpy(to, buf + boff, len);
715 1.1 drochner }
716 1.1 drochner
717 1.1 drochner void
718 1.1 drochner lance_zerobuf_contig(sc, boff, len)
719 1.1 drochner struct lance_softc *sc;
720 1.1 drochner int boff, len;
721 1.1 drochner {
722 1.1 drochner volatile caddr_t buf = sc->sc_mem;
723 1.1 drochner
724 1.1 drochner /*
725 1.25 thorpej * Just let memset() do the work
726 1.1 drochner */
727 1.25 thorpej memset(buf + boff, 0, len);
728 1.1 drochner }
729 1.1 drochner
730 1.1 drochner #if 0
731 1.1 drochner /*
732 1.1 drochner * Examples only; duplicate these and tweak (if necessary) in
733 1.1 drochner * machine-specific front-ends.
734 1.1 drochner */
735 1.1 drochner
736 1.1 drochner /*
737 1.1 drochner * gap2: two bytes of data followed by two bytes of pad.
738 1.1 drochner *
739 1.1 drochner * Buffers must be 4-byte aligned. The code doesn't worry about
740 1.1 drochner * doing an extra byte.
741 1.1 drochner */
742 1.1 drochner
743 1.1 drochner void
744 1.1 drochner lance_copytobuf_gap2(sc, fromv, boff, len)
745 1.1 drochner struct lance_softc *sc;
746 1.1 drochner void *fromv;
747 1.1 drochner int boff;
748 1.11 augustss int len;
749 1.1 drochner {
750 1.1 drochner volatile caddr_t buf = sc->sc_mem;
751 1.11 augustss caddr_t from = fromv;
752 1.11 augustss volatile u_int16_t *bptr;
753 1.1 drochner
754 1.1 drochner if (boff & 0x1) {
755 1.1 drochner /* handle unaligned first byte */
756 1.1 drochner bptr = ((volatile u_int16_t *)buf) + (boff - 1);
757 1.1 drochner *bptr = (*from++ << 8) | (*bptr & 0xff);
758 1.1 drochner bptr += 2;
759 1.1 drochner len--;
760 1.1 drochner } else
761 1.1 drochner bptr = ((volatile u_int16_t *)buf) + boff;
762 1.1 drochner while (len > 1) {
763 1.1 drochner *bptr = (from[1] << 8) | (from[0] & 0xff);
764 1.1 drochner bptr += 2;
765 1.1 drochner from += 2;
766 1.1 drochner len -= 2;
767 1.1 drochner }
768 1.1 drochner if (len == 1)
769 1.1 drochner *bptr = (u_int16_t)*from;
770 1.1 drochner }
771 1.1 drochner
772 1.1 drochner void
773 1.1 drochner lance_copyfrombuf_gap2(sc, tov, boff, len)
774 1.1 drochner struct lance_softc *sc;
775 1.1 drochner void *tov;
776 1.1 drochner int boff, len;
777 1.1 drochner {
778 1.1 drochner volatile caddr_t buf = sc->sc_mem;
779 1.11 augustss caddr_t to = tov;
780 1.11 augustss volatile u_int16_t *bptr;
781 1.11 augustss u_int16_t tmp;
782 1.1 drochner
783 1.1 drochner if (boff & 0x1) {
784 1.1 drochner /* handle unaligned first byte */
785 1.1 drochner bptr = ((volatile u_int16_t *)buf) + (boff - 1);
786 1.1 drochner *to++ = (*bptr >> 8) & 0xff;
787 1.1 drochner bptr += 2;
788 1.1 drochner len--;
789 1.1 drochner } else
790 1.1 drochner bptr = ((volatile u_int16_t *)buf) + boff;
791 1.1 drochner while (len > 1) {
792 1.1 drochner tmp = *bptr;
793 1.1 drochner *to++ = tmp & 0xff;
794 1.1 drochner *to++ = (tmp >> 8) & 0xff;
795 1.1 drochner bptr += 2;
796 1.1 drochner len -= 2;
797 1.1 drochner }
798 1.1 drochner if (len == 1)
799 1.1 drochner *to = *bptr & 0xff;
800 1.1 drochner }
801 1.1 drochner
802 1.1 drochner void
803 1.1 drochner lance_zerobuf_gap2(sc, boff, len)
804 1.1 drochner struct lance_softc *sc;
805 1.1 drochner int boff, len;
806 1.1 drochner {
807 1.1 drochner volatile caddr_t buf = sc->sc_mem;
808 1.11 augustss volatile u_int16_t *bptr;
809 1.1 drochner
810 1.1 drochner if ((unsigned)boff & 0x1) {
811 1.1 drochner bptr = ((volatile u_int16_t *)buf) + (boff - 1);
812 1.1 drochner *bptr &= 0xff;
813 1.1 drochner bptr += 2;
814 1.1 drochner len--;
815 1.1 drochner } else
816 1.1 drochner bptr = ((volatile u_int16_t *)buf) + boff;
817 1.1 drochner while (len > 0) {
818 1.1 drochner *bptr = 0;
819 1.1 drochner bptr += 2;
820 1.1 drochner len -= 2;
821 1.1 drochner }
822 1.1 drochner }
823 1.1 drochner
824 1.1 drochner /*
825 1.1 drochner * gap16: 16 bytes of data followed by 16 bytes of pad.
826 1.1 drochner *
827 1.1 drochner * Buffers must be 32-byte aligned.
828 1.1 drochner */
829 1.1 drochner
830 1.1 drochner void
831 1.1 drochner lance_copytobuf_gap16(sc, fromv, boff, len)
832 1.1 drochner struct lance_softc *sc;
833 1.1 drochner void *fromv;
834 1.1 drochner int boff;
835 1.11 augustss int len;
836 1.1 drochner {
837 1.1 drochner volatile caddr_t buf = sc->sc_mem;
838 1.11 augustss caddr_t from = fromv;
839 1.11 augustss caddr_t bptr;
840 1.11 augustss int xfer;
841 1.1 drochner
842 1.1 drochner bptr = buf + ((boff << 1) & ~0x1f);
843 1.1 drochner boff &= 0xf;
844 1.1 drochner xfer = min(len, 16 - boff);
845 1.1 drochner while (len > 0) {
846 1.24 thorpej memcpy(bptr + boff, from, xfer);
847 1.1 drochner from += xfer;
848 1.1 drochner bptr += 32;
849 1.1 drochner boff = 0;
850 1.1 drochner len -= xfer;
851 1.1 drochner xfer = min(len, 16);
852 1.1 drochner }
853 1.1 drochner }
854 1.1 drochner
855 1.1 drochner void
856 1.1 drochner lance_copyfrombuf_gap16(sc, tov, boff, len)
857 1.1 drochner struct lance_softc *sc;
858 1.1 drochner void *tov;
859 1.1 drochner int boff, len;
860 1.1 drochner {
861 1.1 drochner volatile caddr_t buf = sc->sc_mem;
862 1.11 augustss caddr_t to = tov;
863 1.11 augustss caddr_t bptr;
864 1.11 augustss int xfer;
865 1.1 drochner
866 1.1 drochner bptr = buf + ((boff << 1) & ~0x1f);
867 1.1 drochner boff &= 0xf;
868 1.1 drochner xfer = min(len, 16 - boff);
869 1.1 drochner while (len > 0) {
870 1.24 thorpej memcpy(to, bptr + boff, xfer);
871 1.1 drochner to += xfer;
872 1.1 drochner bptr += 32;
873 1.1 drochner boff = 0;
874 1.1 drochner len -= xfer;
875 1.1 drochner xfer = min(len, 16);
876 1.1 drochner }
877 1.1 drochner }
878 1.1 drochner
879 1.1 drochner void
880 1.1 drochner lance_zerobuf_gap16(sc, boff, len)
881 1.1 drochner struct lance_softc *sc;
882 1.1 drochner int boff, len;
883 1.1 drochner {
884 1.1 drochner volatile caddr_t buf = sc->sc_mem;
885 1.11 augustss caddr_t bptr;
886 1.11 augustss int xfer;
887 1.1 drochner
888 1.1 drochner bptr = buf + ((boff << 1) & ~0x1f);
889 1.1 drochner boff &= 0xf;
890 1.1 drochner xfer = min(len, 16 - boff);
891 1.1 drochner while (len > 0) {
892 1.25 thorpej memset(bptr + boff, 0, xfer);
893 1.1 drochner bptr += 32;
894 1.1 drochner boff = 0;
895 1.1 drochner len -= xfer;
896 1.1 drochner xfer = min(len, 16);
897 1.1 drochner }
898 1.1 drochner }
899 1.1 drochner #endif /* Example only */
900