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