elink3.c revision 1.60 1 1.60 enami /* $NetBSD: elink3.c,v 1.60 1999/10/20 16:34:22 enami Exp $ */
2 1.41 thorpej
3 1.41 thorpej /*-
4 1.41 thorpej * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 1.41 thorpej * All rights reserved.
6 1.41 thorpej *
7 1.41 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.41 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.41 thorpej * NASA Ames Research Center.
10 1.41 thorpej *
11 1.41 thorpej * Redistribution and use in source and binary forms, with or without
12 1.41 thorpej * modification, are permitted provided that the following conditions
13 1.41 thorpej * are met:
14 1.41 thorpej * 1. Redistributions of source code must retain the above copyright
15 1.41 thorpej * notice, this list of conditions and the following disclaimer.
16 1.41 thorpej * 2. Redistributions in binary form must reproduce the above copyright
17 1.41 thorpej * notice, this list of conditions and the following disclaimer in the
18 1.41 thorpej * documentation and/or other materials provided with the distribution.
19 1.41 thorpej * 3. All advertising materials mentioning features or use of this software
20 1.41 thorpej * must display the following acknowledgement:
21 1.41 thorpej * This product includes software developed by the NetBSD
22 1.41 thorpej * Foundation, Inc. and its contributors.
23 1.41 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.41 thorpej * contributors may be used to endorse or promote products derived
25 1.41 thorpej * from this software without specific prior written permission.
26 1.41 thorpej *
27 1.41 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.41 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.41 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.41 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.41 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.41 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.41 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.41 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.41 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.41 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.41 thorpej * POSSIBILITY OF SUCH DAMAGE.
38 1.41 thorpej */
39 1.1 thorpej
40 1.1 thorpej /*
41 1.19 jonathan * Copyright (c) 1996, 1997 Jonathan Stone <jonathan (at) NetBSD.org>
42 1.6 thorpej * Copyright (c) 1994 Herb Peyerl <hpeyerl (at) beer.org>
43 1.1 thorpej * All rights reserved.
44 1.1 thorpej *
45 1.1 thorpej * Redistribution and use in source and binary forms, with or without
46 1.1 thorpej * modification, are permitted provided that the following conditions
47 1.1 thorpej * are met:
48 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
49 1.1 thorpej * notice, this list of conditions and the following disclaimer.
50 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
51 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
52 1.1 thorpej * documentation and/or other materials provided with the distribution.
53 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
54 1.1 thorpej * must display the following acknowledgement:
55 1.1 thorpej * This product includes software developed by Herb Peyerl.
56 1.1 thorpej * 4. The name of Herb Peyerl may not be used to endorse or promote products
57 1.1 thorpej * derived from this software without specific prior written permission.
58 1.1 thorpej *
59 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
60 1.1 thorpej * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
61 1.1 thorpej * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
62 1.1 thorpej * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
63 1.1 thorpej * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
64 1.1 thorpej * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
65 1.1 thorpej * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
66 1.1 thorpej * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
67 1.1 thorpej * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
68 1.1 thorpej * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
69 1.1 thorpej */
70 1.1 thorpej
71 1.39 jonathan #include "opt_inet.h"
72 1.40 jonathan #include "opt_ns.h"
73 1.1 thorpej #include "bpfilter.h"
74 1.35 explorer #include "rnd.h"
75 1.1 thorpej
76 1.1 thorpej #include <sys/param.h>
77 1.3 christos #include <sys/systm.h>
78 1.41 thorpej #include <sys/kernel.h>
79 1.1 thorpej #include <sys/mbuf.h>
80 1.1 thorpej #include <sys/socket.h>
81 1.1 thorpej #include <sys/ioctl.h>
82 1.1 thorpej #include <sys/errno.h>
83 1.1 thorpej #include <sys/syslog.h>
84 1.1 thorpej #include <sys/select.h>
85 1.1 thorpej #include <sys/device.h>
86 1.35 explorer #if NRND > 0
87 1.35 explorer #include <sys/rnd.h>
88 1.35 explorer #endif
89 1.1 thorpej
90 1.1 thorpej #include <net/if.h>
91 1.1 thorpej #include <net/if_dl.h>
92 1.21 is #include <net/if_ether.h>
93 1.22 jonathan #include <net/if_media.h>
94 1.1 thorpej
95 1.1 thorpej #ifdef INET
96 1.1 thorpej #include <netinet/in.h>
97 1.1 thorpej #include <netinet/in_systm.h>
98 1.1 thorpej #include <netinet/in_var.h>
99 1.1 thorpej #include <netinet/ip.h>
100 1.21 is #include <netinet/if_inarp.h>
101 1.1 thorpej #endif
102 1.1 thorpej
103 1.1 thorpej #ifdef NS
104 1.1 thorpej #include <netns/ns.h>
105 1.1 thorpej #include <netns/ns_if.h>
106 1.1 thorpej #endif
107 1.1 thorpej
108 1.1 thorpej #if NBPFILTER > 0
109 1.1 thorpej #include <net/bpf.h>
110 1.1 thorpej #include <net/bpfdesc.h>
111 1.1 thorpej #endif
112 1.1 thorpej
113 1.1 thorpej #include <machine/cpu.h>
114 1.2 thorpej #include <machine/bus.h>
115 1.7 thorpej #include <machine/intr.h>
116 1.1 thorpej
117 1.41 thorpej #include <dev/mii/mii.h>
118 1.41 thorpej #include <dev/mii/miivar.h>
119 1.41 thorpej
120 1.1 thorpej #include <dev/ic/elink3var.h>
121 1.1 thorpej #include <dev/ic/elink3reg.h>
122 1.1 thorpej
123 1.36 jonathan #ifdef DEBUG
124 1.36 jonathan int epdebug = 0;
125 1.36 jonathan #endif
126 1.36 jonathan
127 1.23 jonathan /*
128 1.55 jonathan * XXX endian workaround for big-endian CPUs with pcmcia:
129 1.55 jonathan * if stream methods for bus_space_multi are not provided, define them
130 1.55 jonathan * using non-stream bus_space_{read,write}_multi_.
131 1.55 jonathan * Assumes host CPU is same endian-ness as bus.
132 1.55 jonathan */
133 1.55 jonathan #ifndef __BUS_SPACE_HAS_STREAM_METHODS
134 1.55 jonathan #define bus_space_read_multi_stream_2 bus_space_read_multi_2
135 1.55 jonathan #define bus_space_read_multi_stream_4 bus_space_read_multi_4
136 1.55 jonathan #define bus_space_write_multi_stream_2 bus_space_write_multi_2
137 1.55 jonathan #define bus_space_write_multi_stream_4 bus_space_write_multi_4
138 1.55 jonathan #endif /* __BUS_SPACE_HAS_STREAM_METHODS */
139 1.55 jonathan
140 1.55 jonathan /*
141 1.41 thorpej * Structure to map media-present bits in boards to ifmedia codes and
142 1.41 thorpej * printable media names. Used for table-driven ifmedia initialization.
143 1.23 jonathan */
144 1.23 jonathan struct ep_media {
145 1.41 thorpej int epm_mpbit; /* media present bit */
146 1.41 thorpej const char *epm_name; /* name of medium */
147 1.23 jonathan int epm_ifmedia; /* ifmedia word for medium */
148 1.47 fvdl int epm_epmedia; /* ELINKMEDIA_* constant */
149 1.23 jonathan };
150 1.23 jonathan
151 1.23 jonathan /*
152 1.41 thorpej * Media table for the Demon/Vortex/Boomerang chipsets.
153 1.41 thorpej *
154 1.41 thorpej * Note that MII on the Demon and Vortex (3c59x) indicates an external
155 1.41 thorpej * MII connector (for connecting an external PHY) ... I think. Treat
156 1.41 thorpej * it as `manual' on these chips.
157 1.23 jonathan *
158 1.41 thorpej * Any Boomerang (3c90x) chips with MII really do have an internal
159 1.41 thorpej * MII and real PHYs attached; no `native' media.
160 1.23 jonathan */
161 1.41 thorpej struct ep_media ep_vortex_media[] = {
162 1.47 fvdl { ELINK_PCI_10BASE_T, "10baseT", IFM_ETHER|IFM_10_T,
163 1.47 fvdl ELINKMEDIA_10BASE_T },
164 1.47 fvdl { ELINK_PCI_10BASE_T, "10baseT-FDX", IFM_ETHER|IFM_10_T|IFM_FDX,
165 1.47 fvdl ELINKMEDIA_10BASE_T },
166 1.48 thorpej { ELINK_PCI_AUI, "10base5", IFM_ETHER|IFM_10_5,
167 1.47 fvdl ELINKMEDIA_AUI },
168 1.48 thorpej { ELINK_PCI_BNC, "10base2", IFM_ETHER|IFM_10_2,
169 1.47 fvdl ELINKMEDIA_10BASE_2 },
170 1.47 fvdl { ELINK_PCI_100BASE_TX, "100baseTX", IFM_ETHER|IFM_100_TX,
171 1.47 fvdl ELINKMEDIA_100BASE_TX },
172 1.47 fvdl { ELINK_PCI_100BASE_TX, "100baseTX-FDX",IFM_ETHER|IFM_100_TX|IFM_FDX,
173 1.47 fvdl ELINKMEDIA_100BASE_TX },
174 1.47 fvdl { ELINK_PCI_100BASE_FX, "100baseFX", IFM_ETHER|IFM_100_FX,
175 1.47 fvdl ELINKMEDIA_100BASE_FX },
176 1.48 thorpej { ELINK_PCI_100BASE_MII,"manual", IFM_ETHER|IFM_MANUAL,
177 1.47 fvdl ELINKMEDIA_MII },
178 1.47 fvdl { ELINK_PCI_100BASE_T4, "100baseT4", IFM_ETHER|IFM_100_T4,
179 1.47 fvdl ELINKMEDIA_100BASE_T4 },
180 1.41 thorpej { 0, NULL, 0,
181 1.41 thorpej 0 },
182 1.23 jonathan };
183 1.23 jonathan
184 1.23 jonathan /*
185 1.41 thorpej * Media table for the older 3Com Etherlink III chipset, used
186 1.41 thorpej * in the 3c509, 3c579, and 3c589.
187 1.23 jonathan */
188 1.41 thorpej struct ep_media ep_509_media[] = {
189 1.48 thorpej { ELINK_W0_CC_UTP, "10baseT", IFM_ETHER|IFM_10_T,
190 1.47 fvdl ELINKMEDIA_10BASE_T },
191 1.48 thorpej { ELINK_W0_CC_AUI, "10base5", IFM_ETHER|IFM_10_5,
192 1.47 fvdl ELINKMEDIA_AUI },
193 1.48 thorpej { ELINK_W0_CC_BNC, "10base2", IFM_ETHER|IFM_10_2,
194 1.47 fvdl ELINKMEDIA_10BASE_2 },
195 1.41 thorpej { 0, NULL, 0,
196 1.41 thorpej 0 },
197 1.23 jonathan };
198 1.23 jonathan
199 1.15 jonathan void ep_internalconfig __P((struct ep_softc *sc));
200 1.20 jonathan void ep_vortex_probemedia __P((struct ep_softc *sc));
201 1.41 thorpej void ep_509_probemedia __P((struct ep_softc *sc));
202 1.20 jonathan
203 1.3 christos static void eptxstat __P((struct ep_softc *));
204 1.1 thorpej static int epstatus __P((struct ep_softc *));
205 1.60 enami void epinit __P((struct ep_softc *));
206 1.60 enami int epioctl __P((struct ifnet *, u_long, caddr_t));
207 1.60 enami void epstart __P((struct ifnet *));
208 1.60 enami void epwatchdog __P((struct ifnet *));
209 1.60 enami void epreset __P((struct ep_softc *));
210 1.16 jonathan static void epshutdown __P((void *));
211 1.23 jonathan void epread __P((struct ep_softc *));
212 1.1 thorpej struct mbuf *epget __P((struct ep_softc *, int));
213 1.23 jonathan void epmbuffill __P((void *));
214 1.23 jonathan void epmbufempty __P((struct ep_softc *));
215 1.23 jonathan void epsetfilter __P((struct ep_softc *));
216 1.59 thorpej void ep_roadrunner_mii_enable __P((struct ep_softc *));
217 1.41 thorpej void epsetmedia __P((struct ep_softc *));
218 1.23 jonathan
219 1.23 jonathan /* ifmedia callbacks */
220 1.23 jonathan int ep_media_change __P((struct ifnet *ifp));
221 1.23 jonathan void ep_media_status __P((struct ifnet *ifp, struct ifmediareq *req));
222 1.1 thorpej
223 1.41 thorpej /* MII callbacks */
224 1.41 thorpej int ep_mii_readreg __P((struct device *, int, int));
225 1.41 thorpej void ep_mii_writereg __P((struct device *, int, int, int));
226 1.41 thorpej void ep_statchg __P((struct device *));
227 1.41 thorpej
228 1.41 thorpej void ep_tick __P((void *));
229 1.41 thorpej
230 1.41 thorpej void ep_mii_setbit __P((struct ep_softc *, u_int16_t));
231 1.41 thorpej void ep_mii_clrbit __P((struct ep_softc *, u_int16_t));
232 1.41 thorpej u_int16_t ep_mii_readbit __P((struct ep_softc *, u_int16_t));
233 1.41 thorpej void ep_mii_sync __P((struct ep_softc *));
234 1.41 thorpej void ep_mii_sendbits __P((struct ep_softc *, u_int32_t, int));
235 1.41 thorpej
236 1.1 thorpej static int epbusyeeprom __P((struct ep_softc *));
237 1.59 thorpej u_int16_t ep_read_eeprom __P((struct ep_softc *, u_int16_t));
238 1.56 jonathan static inline void ep_reset_cmd __P((struct ep_softc *sc,
239 1.19 jonathan u_int cmd, u_int arg));
240 1.56 jonathan static inline void ep_finish_reset __P((bus_space_tag_t, bus_space_handle_t));
241 1.56 jonathan static inline void ep_discard_rxtop __P((bus_space_tag_t, bus_space_handle_t));
242 1.42 thorpej static __inline int ep_w1_reg __P((struct ep_softc *, int));
243 1.19 jonathan
244 1.42 thorpej /*
245 1.42 thorpej * Some chips (3c515 [Corkscrew] and 3c574 [RoadRunner]) have
246 1.42 thorpej * Window 1 registers offset!
247 1.42 thorpej */
248 1.42 thorpej static __inline int
249 1.42 thorpej ep_w1_reg(sc, reg)
250 1.42 thorpej struct ep_softc *sc;
251 1.42 thorpej int reg;
252 1.42 thorpej {
253 1.42 thorpej
254 1.42 thorpej switch (sc->ep_chipset) {
255 1.47 fvdl case ELINK_CHIPSET_CORKSCREW:
256 1.42 thorpej return (reg + 0x10);
257 1.42 thorpej
258 1.47 fvdl case ELINK_CHIPSET_ROADRUNNER:
259 1.42 thorpej switch (reg) {
260 1.47 fvdl case ELINK_W1_FREE_TX:
261 1.47 fvdl case ELINK_W1_RUNNER_RDCTL:
262 1.47 fvdl case ELINK_W1_RUNNER_WRCTL:
263 1.42 thorpej return (reg);
264 1.42 thorpej }
265 1.42 thorpej return (reg + 0x10);
266 1.42 thorpej }
267 1.42 thorpej
268 1.42 thorpej return (reg);
269 1.42 thorpej }
270 1.19 jonathan
271 1.19 jonathan /*
272 1.56 jonathan * Wait for any pending reset to complete.
273 1.19 jonathan * On newer hardware we could poll SC_COMMAND_IN_PROGRESS,
274 1.19 jonathan * but older hardware doesn't implement it and we must delay.
275 1.19 jonathan */
276 1.19 jonathan static inline void
277 1.56 jonathan ep_finish_reset(iot, ioh)
278 1.56 jonathan bus_space_tag_t iot;
279 1.56 jonathan bus_space_handle_t ioh;
280 1.56 jonathan
281 1.56 jonathan {
282 1.57 jonathan int i;
283 1.56 jonathan
284 1.57 jonathan for (i = 0; i < 10000; i++) {
285 1.60 enami if ((bus_space_read_2(iot, ioh, ELINK_STATUS) &
286 1.60 enami S_COMMAND_IN_PROGRESS) == 0)
287 1.57 jonathan break;
288 1.57 jonathan DELAY(10);
289 1.56 jonathan }
290 1.56 jonathan }
291 1.56 jonathan
292 1.56 jonathan /*
293 1.56 jonathan * Issue a (reset) command, and be sure it has completed.
294 1.56 jonathan * Used for global reset, TX_RESET, RX_RESET.
295 1.56 jonathan */
296 1.56 jonathan static inline void
297 1.56 jonathan ep_reset_cmd(sc, cmd, arg)
298 1.19 jonathan struct ep_softc *sc;
299 1.19 jonathan u_int cmd, arg;
300 1.19 jonathan {
301 1.19 jonathan register bus_space_tag_t iot = sc->sc_iot;
302 1.19 jonathan register bus_space_handle_t ioh = sc->sc_ioh;
303 1.19 jonathan
304 1.19 jonathan bus_space_write_2(iot, ioh, cmd, arg);
305 1.56 jonathan ep_finish_reset(iot, ioh);
306 1.56 jonathan }
307 1.56 jonathan
308 1.56 jonathan
309 1.56 jonathan static inline void
310 1.56 jonathan ep_discard_rxtop(iot, ioh)
311 1.56 jonathan register bus_space_tag_t iot;
312 1.56 jonathan register bus_space_handle_t ioh;
313 1.56 jonathan {
314 1.57 jonathan int i;
315 1.56 jonathan
316 1.56 jonathan bus_space_write_2(iot, ioh, ELINK_COMMAND, RX_DISCARD_TOP_PACK);
317 1.57 jonathan
318 1.57 jonathan /*
319 1.57 jonathan * Spin for about 1 msec, to avoid forcing a DELAY() between
320 1.57 jonathan * every received packet (adding latency and limiting pkt-recv rate).
321 1.57 jonathan * On PCI, at 4 30-nsec PCI bus cycles for a read, 8000 iterations
322 1.57 jonathan * is about right.
323 1.57 jonathan */
324 1.57 jonathan for (i = 0; i < 8000; i++) {
325 1.60 enami if ((bus_space_read_2(iot, ioh, ELINK_STATUS) &
326 1.60 enami S_COMMAND_IN_PROGRESS) == 0)
327 1.57 jonathan return;
328 1.57 jonathan }
329 1.57 jonathan
330 1.57 jonathan /* Didn't complete in a hurry. Do DELAY()s. */
331 1.56 jonathan ep_finish_reset(iot, ioh);
332 1.19 jonathan }
333 1.19 jonathan
334 1.20 jonathan /*
335 1.20 jonathan * Back-end attach and configure.
336 1.20 jonathan */
337 1.1 thorpej void
338 1.34 thorpej epconfig(sc, chipset, enaddr)
339 1.1 thorpej struct ep_softc *sc;
340 1.20 jonathan u_short chipset;
341 1.34 thorpej u_int8_t *enaddr;
342 1.1 thorpej {
343 1.21 is struct ifnet *ifp = &sc->sc_ethercom.ec_if;
344 1.11 thorpej bus_space_tag_t iot = sc->sc_iot;
345 1.11 thorpej bus_space_handle_t ioh = sc->sc_ioh;
346 1.7 thorpej u_int16_t i;
347 1.21 is u_int8_t myla[6];
348 1.1 thorpej
349 1.20 jonathan sc->ep_chipset = chipset;
350 1.32 thorpej
351 1.32 thorpej /*
352 1.32 thorpej * We could have been groveling around in other register
353 1.32 thorpej * windows in the front-end; make sure we're in window 0
354 1.32 thorpej * to read the EEPROM.
355 1.32 thorpej */
356 1.32 thorpej GO_WINDOW(0);
357 1.1 thorpej
358 1.34 thorpej if (enaddr == NULL) {
359 1.34 thorpej /*
360 1.59 thorpej * Read the station address from the eeprom.
361 1.34 thorpej */
362 1.34 thorpej for (i = 0; i < 3; i++) {
363 1.59 thorpej u_int16_t x = ep_read_eeprom(sc, i);
364 1.34 thorpej myla[(i << 1)] = x >> 8;
365 1.34 thorpej myla[(i << 1) + 1] = x;
366 1.34 thorpej }
367 1.34 thorpej enaddr = myla;
368 1.1 thorpej }
369 1.1 thorpej
370 1.12 jonathan /*
371 1.41 thorpej * Vortex-based (3c59x pci,eisa) and Boomerang (3c900) cards
372 1.23 jonathan * allow FDDI-sized (4500) byte packets. Commands only take an
373 1.23 jonathan * 11-bit parameter, and 11 bits isn't enough to hold a full-size
374 1.23 jonathan * packet length.
375 1.12 jonathan * Commands to these cards implicitly upshift a packet size
376 1.12 jonathan * or threshold by 2 bits.
377 1.12 jonathan * To detect cards with large-packet support, we probe by setting
378 1.12 jonathan * the transmit threshold register, then change windows and
379 1.12 jonathan * read back the threshold register directly, and see if the
380 1.12 jonathan * threshold value was shifted or not.
381 1.12 jonathan */
382 1.47 fvdl bus_space_write_2(iot, ioh, ELINK_COMMAND,
383 1.60 enami SET_TX_AVAIL_THRESH | ELINK_LARGEWIN_PROBE);
384 1.12 jonathan GO_WINDOW(5);
385 1.47 fvdl i = bus_space_read_2(iot, ioh, ELINK_W5_TX_AVAIL_THRESH);
386 1.12 jonathan GO_WINDOW(1);
387 1.12 jonathan switch (i) {
388 1.47 fvdl case ELINK_LARGEWIN_PROBE:
389 1.47 fvdl case (ELINK_LARGEWIN_PROBE & ELINK_LARGEWIN_MASK):
390 1.12 jonathan sc->ep_pktlenshift = 0;
391 1.12 jonathan break;
392 1.12 jonathan
393 1.47 fvdl case (ELINK_LARGEWIN_PROBE << 2):
394 1.12 jonathan sc->ep_pktlenshift = 2;
395 1.12 jonathan break;
396 1.12 jonathan
397 1.12 jonathan default:
398 1.34 thorpej printf("%s: wrote 0x%x to TX_AVAIL_THRESH, read back 0x%x. "
399 1.14 cjs "Interface disabled\n",
400 1.47 fvdl sc->sc_dev.dv_xname, ELINK_LARGEWIN_PROBE, (int) i);
401 1.12 jonathan return;
402 1.12 jonathan }
403 1.20 jonathan
404 1.12 jonathan /*
405 1.12 jonathan * Ensure Tx-available interrupts are enabled for
406 1.12 jonathan * start the interface.
407 1.23 jonathan * XXX should be in epinit()?
408 1.12 jonathan */
409 1.47 fvdl bus_space_write_2(iot, ioh, ELINK_COMMAND,
410 1.12 jonathan SET_TX_AVAIL_THRESH | (1600 >> sc->ep_pktlenshift));
411 1.12 jonathan
412 1.23 jonathan bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
413 1.23 jonathan ifp->if_softc = sc;
414 1.23 jonathan ifp->if_start = epstart;
415 1.23 jonathan ifp->if_ioctl = epioctl;
416 1.23 jonathan ifp->if_watchdog = epwatchdog;
417 1.23 jonathan ifp->if_flags =
418 1.23 jonathan IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
419 1.23 jonathan
420 1.23 jonathan if_attach(ifp);
421 1.34 thorpej ether_ifattach(ifp, enaddr);
422 1.23 jonathan
423 1.23 jonathan /*
424 1.23 jonathan * Finish configuration:
425 1.23 jonathan * determine chipset if the front-end couldn't do so,
426 1.23 jonathan * show board details, set media.
427 1.23 jonathan */
428 1.23 jonathan
429 1.41 thorpej /*
430 1.41 thorpej * Print RAM size. We also print the Ethernet address in here.
431 1.41 thorpej * It's extracted from the ifp, so we have to make sure it's
432 1.41 thorpej * been attached first.
433 1.41 thorpej */
434 1.23 jonathan ep_internalconfig(sc);
435 1.23 jonathan GO_WINDOW(0);
436 1.23 jonathan
437 1.41 thorpej /*
438 1.44 thorpej * Display some additional information, if pertinent.
439 1.44 thorpej */
440 1.47 fvdl if (sc->ep_flags & ELINK_FLAGS_USEFIFOBUFFER)
441 1.44 thorpej printf("%s: RoadRunner FIFO buffer enabled\n",
442 1.44 thorpej sc->sc_dev.dv_xname);
443 1.44 thorpej
444 1.44 thorpej /*
445 1.41 thorpej * Initialize our media structures and MII info. We'll
446 1.41 thorpej * probe the MII if we discover that we have one.
447 1.20 jonathan */
448 1.41 thorpej sc->sc_mii.mii_ifp = ifp;
449 1.41 thorpej sc->sc_mii.mii_readreg = ep_mii_readreg;
450 1.41 thorpej sc->sc_mii.mii_writereg = ep_mii_writereg;
451 1.41 thorpej sc->sc_mii.mii_statchg = ep_statchg;
452 1.41 thorpej ifmedia_init(&sc->sc_mii.mii_media, 0, ep_media_change,
453 1.41 thorpej ep_media_status);
454 1.20 jonathan
455 1.20 jonathan /*
456 1.41 thorpej * Now, determine which media we have.
457 1.20 jonathan */
458 1.20 jonathan switch (sc->ep_chipset) {
459 1.59 thorpej case ELINK_CHIPSET_ROADRUNNER:
460 1.59 thorpej if (sc->ep_flags & ELINK_FLAGS_MII) {
461 1.59 thorpej ep_roadrunner_mii_enable(sc);
462 1.59 thorpej GO_WINDOW(0);
463 1.59 thorpej }
464 1.59 thorpej /* FALLTHROUGH */
465 1.59 thorpej
466 1.47 fvdl case ELINK_CHIPSET_BOOMERANG:
467 1.41 thorpej /*
468 1.41 thorpej * If the device has MII, probe it. We won't be using
469 1.41 thorpej * any `native' media in this case, only PHYs. If
470 1.41 thorpej * we don't, just treat the Boomerang like the Vortex.
471 1.41 thorpej */
472 1.47 fvdl if (sc->ep_flags & ELINK_FLAGS_MII) {
473 1.41 thorpej mii_phy_probe(&sc->sc_dev, &sc->sc_mii, 0xffffffff);
474 1.41 thorpej if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
475 1.41 thorpej ifmedia_add(&sc->sc_mii.mii_media,
476 1.41 thorpej IFM_ETHER|IFM_NONE, 0, NULL);
477 1.41 thorpej ifmedia_set(&sc->sc_mii.mii_media,
478 1.41 thorpej IFM_ETHER|IFM_NONE);
479 1.41 thorpej } else {
480 1.41 thorpej ifmedia_set(&sc->sc_mii.mii_media,
481 1.41 thorpej IFM_ETHER|IFM_AUTO);
482 1.41 thorpej }
483 1.41 thorpej break;
484 1.41 thorpej }
485 1.41 thorpej /* FALLTHROUGH */
486 1.41 thorpej
487 1.47 fvdl case ELINK_CHIPSET_VORTEX:
488 1.20 jonathan ep_vortex_probemedia(sc);
489 1.20 jonathan break;
490 1.20 jonathan
491 1.20 jonathan default:
492 1.41 thorpej ep_509_probemedia(sc);
493 1.20 jonathan break;
494 1.20 jonathan }
495 1.23 jonathan
496 1.20 jonathan GO_WINDOW(1); /* Window 1 is operating window */
497 1.20 jonathan
498 1.1 thorpej #if NBPFILTER > 0
499 1.60 enami bpfattach(ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
500 1.1 thorpej #endif
501 1.1 thorpej
502 1.35 explorer #if NRND > 0
503 1.53 explorer rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
504 1.60 enami RND_TYPE_NET, 0);
505 1.35 explorer #endif
506 1.35 explorer
507 1.1 thorpej sc->tx_start_thresh = 20; /* probably a good starting point. */
508 1.12 jonathan
509 1.16 jonathan /* Establish callback to reset card when we reboot. */
510 1.16 jonathan shutdownhook_establish(epshutdown, sc);
511 1.16 jonathan
512 1.56 jonathan ep_reset_cmd(sc, ELINK_COMMAND, RX_RESET);
513 1.56 jonathan ep_reset_cmd(sc, ELINK_COMMAND, TX_RESET);
514 1.1 thorpej }
515 1.1 thorpej
516 1.23 jonathan
517 1.1 thorpej /*
518 1.15 jonathan * Show interface-model-independent info from window 3
519 1.15 jonathan * internal-configuration register.
520 1.15 jonathan */
521 1.15 jonathan void
522 1.15 jonathan ep_internalconfig(sc)
523 1.15 jonathan struct ep_softc *sc;
524 1.15 jonathan {
525 1.15 jonathan bus_space_tag_t iot = sc->sc_iot;
526 1.15 jonathan bus_space_handle_t ioh = sc->sc_ioh;
527 1.15 jonathan
528 1.15 jonathan u_int config0;
529 1.15 jonathan u_int config1;
530 1.15 jonathan
531 1.15 jonathan int ram_size, ram_width, ram_speed, rom_size, ram_split;
532 1.15 jonathan /*
533 1.15 jonathan * NVRAM buffer Rx:Tx config names for busmastering cards
534 1.15 jonathan * (Demon, Vortex, and later).
535 1.15 jonathan */
536 1.15 jonathan const char *onboard_ram_config[] = {
537 1.38 augustss "5:3", "3:1", "1:1", "3:5" };
538 1.15 jonathan
539 1.15 jonathan GO_WINDOW(3);
540 1.47 fvdl config0 = (u_int)bus_space_read_2(iot, ioh, ELINK_W3_INTERNAL_CONFIG);
541 1.60 enami config1 = (u_int)bus_space_read_2(iot, ioh,
542 1.60 enami ELINK_W3_INTERNAL_CONFIG + 2);
543 1.15 jonathan GO_WINDOW(0);
544 1.15 jonathan
545 1.15 jonathan ram_size = (config0 & CONFIG_RAMSIZE) >> CONFIG_RAMSIZE_SHIFT;
546 1.15 jonathan ram_width = (config0 & CONFIG_RAMWIDTH) >> CONFIG_RAMWIDTH_SHIFT;
547 1.15 jonathan ram_speed = (config0 & CONFIG_RAMSPEED) >> CONFIG_RAMSPEED_SHIFT;
548 1.15 jonathan rom_size = (config0 & CONFIG_ROMSIZE) >> CONFIG_ROMSIZE_SHIFT;
549 1.15 jonathan
550 1.15 jonathan ram_split = (config1 & CONFIG_RAMSPLIT) >> CONFIG_RAMSPLIT_SHIFT;
551 1.15 jonathan
552 1.41 thorpej printf("%s: address %s, %dKB %s-wide FIFO, %s Rx:Tx split\n",
553 1.23 jonathan sc->sc_dev.dv_xname,
554 1.41 thorpej ether_sprintf(LLADDR(sc->sc_ethercom.ec_if.if_sadl)),
555 1.23 jonathan 8 << ram_size,
556 1.23 jonathan (ram_width) ? "word" : "byte",
557 1.23 jonathan onboard_ram_config[ram_split]);
558 1.15 jonathan }
559 1.15 jonathan
560 1.23 jonathan
561 1.20 jonathan /*
562 1.23 jonathan * Find supported media on 3c509-generation hardware that doesn't have
563 1.20 jonathan * a "reset_options" register in window 3.
564 1.23 jonathan * Use the config_cntrl register in window 0 instead.
565 1.23 jonathan * Used on original, 10Mbit ISA (3c509), 3c509B, and pre-Demon EISA cards
566 1.23 jonathan * that implement CONFIG_CTRL. We don't have a good way to set the
567 1.23 jonathan * default active mediuim; punt to ifconfig instead.
568 1.20 jonathan */
569 1.20 jonathan void
570 1.41 thorpej ep_509_probemedia(sc)
571 1.20 jonathan struct ep_softc *sc;
572 1.20 jonathan {
573 1.20 jonathan bus_space_tag_t iot = sc->sc_iot;
574 1.20 jonathan bus_space_handle_t ioh = sc->sc_ioh;
575 1.41 thorpej struct ifmedia *ifm = &sc->sc_mii.mii_media;
576 1.23 jonathan u_int16_t ep_w0_config, port;
577 1.41 thorpej struct ep_media *epm;
578 1.41 thorpej const char *sep = "", *defmedianame = NULL;
579 1.41 thorpej int defmedia = 0;
580 1.23 jonathan
581 1.20 jonathan GO_WINDOW(0);
582 1.47 fvdl ep_w0_config = bus_space_read_2(iot, ioh, ELINK_W0_CONFIG_CTRL);
583 1.23 jonathan
584 1.41 thorpej printf("%s: ", sc->sc_dev.dv_xname);
585 1.23 jonathan
586 1.41 thorpej /* Sanity check that there are any media! */
587 1.47 fvdl if ((ep_w0_config & ELINK_W0_CC_MEDIAMASK) == 0) {
588 1.41 thorpej printf("no media present!\n");
589 1.41 thorpej ifmedia_add(ifm, IFM_ETHER|IFM_NONE, 0, NULL);
590 1.41 thorpej ifmedia_set(ifm, IFM_ETHER|IFM_NONE);
591 1.41 thorpej return;
592 1.23 jonathan }
593 1.23 jonathan
594 1.41 thorpej /*
595 1.41 thorpej * Get the default media from the EEPROM.
596 1.41 thorpej */
597 1.59 thorpej port = ep_read_eeprom(sc, EEPROM_ADDR_CFG) >> 14;
598 1.23 jonathan
599 1.41 thorpej #define PRINT(s) printf("%s%s", sep, s); sep = ", "
600 1.23 jonathan
601 1.41 thorpej for (epm = ep_509_media; epm->epm_name != NULL; epm++) {
602 1.41 thorpej if (ep_w0_config & epm->epm_mpbit) {
603 1.46 thorpej /*
604 1.46 thorpej * This simple test works because 509 chipsets
605 1.46 thorpej * don't do full-duplex.
606 1.46 thorpej */
607 1.41 thorpej if (epm->epm_epmedia == port || defmedia == 0) {
608 1.41 thorpej defmedia = epm->epm_ifmedia;
609 1.41 thorpej defmedianame = epm->epm_name;
610 1.41 thorpej }
611 1.41 thorpej ifmedia_add(ifm, epm->epm_ifmedia, epm->epm_epmedia,
612 1.41 thorpej NULL);
613 1.41 thorpej PRINT(epm->epm_name);
614 1.41 thorpej }
615 1.41 thorpej }
616 1.41 thorpej
617 1.41 thorpej #undef PRINT
618 1.41 thorpej
619 1.41 thorpej #ifdef DIAGNOSTIC
620 1.41 thorpej if (defmedia == 0)
621 1.41 thorpej panic("ep_509_probemedia: impossible");
622 1.41 thorpej #endif
623 1.41 thorpej
624 1.41 thorpej printf(" (default %s)\n", defmedianame);
625 1.41 thorpej ifmedia_set(ifm, defmedia);
626 1.20 jonathan }
627 1.20 jonathan
628 1.15 jonathan /*
629 1.23 jonathan * Find media present on large-packet-capable elink3 devices.
630 1.23 jonathan * Show onboard configuration of large-packet-capable elink3 devices
631 1.23 jonathan * (Demon, Vortex, Boomerang), which do not implement CONFIG_CTRL in window 0.
632 1.23 jonathan * Use media and card-version info in window 3 instead.
633 1.15 jonathan */
634 1.15 jonathan void
635 1.20 jonathan ep_vortex_probemedia(sc)
636 1.15 jonathan struct ep_softc *sc;
637 1.15 jonathan {
638 1.15 jonathan bus_space_tag_t iot = sc->sc_iot;
639 1.15 jonathan bus_space_handle_t ioh = sc->sc_ioh;
640 1.41 thorpej struct ifmedia *ifm = &sc->sc_mii.mii_media;
641 1.41 thorpej struct ep_media *epm;
642 1.41 thorpej u_int config1;
643 1.15 jonathan int reset_options;
644 1.28 veego int default_media; /* 3-bit encoding of default (EEPROM) media */
645 1.41 thorpej int defmedia = 0;
646 1.41 thorpej const char *sep = "", *defmedianame = NULL;
647 1.15 jonathan
648 1.15 jonathan GO_WINDOW(3);
649 1.60 enami config1 = (u_int)bus_space_read_2(iot, ioh,
650 1.60 enami ELINK_W3_INTERNAL_CONFIG + 2);
651 1.47 fvdl reset_options = (int)bus_space_read_1(iot, ioh, ELINK_W3_RESET_OPTIONS);
652 1.15 jonathan GO_WINDOW(0);
653 1.15 jonathan
654 1.23 jonathan default_media = (config1 & CONFIG_MEDIAMASK) >> CONFIG_MEDIAMASK_SHIFT;
655 1.15 jonathan
656 1.41 thorpej printf("%s: ", sc->sc_dev.dv_xname);
657 1.41 thorpej
658 1.41 thorpej /* Sanity check that there are any media! */
659 1.47 fvdl if ((reset_options & ELINK_PCI_MEDIAMASK) == 0) {
660 1.41 thorpej printf("no media present!\n");
661 1.41 thorpej ifmedia_add(ifm, IFM_ETHER|IFM_NONE, 0, NULL);
662 1.41 thorpej ifmedia_set(ifm, IFM_ETHER|IFM_NONE);
663 1.41 thorpej return;
664 1.41 thorpej }
665 1.41 thorpej
666 1.41 thorpej #define PRINT(s) printf("%s%s", sep, s); sep = ", "
667 1.23 jonathan
668 1.41 thorpej for (epm = ep_vortex_media; epm->epm_name != NULL; epm++) {
669 1.41 thorpej if (reset_options & epm->epm_mpbit) {
670 1.46 thorpej /*
671 1.46 thorpej * Default media is a little more complicated
672 1.46 thorpej * on the Vortex. We support full-duplex which
673 1.46 thorpej * uses the same reset options bit.
674 1.46 thorpej *
675 1.46 thorpej * XXX Check EEPROM for default to FDX?
676 1.46 thorpej */
677 1.46 thorpej if (epm->epm_epmedia == default_media) {
678 1.46 thorpej if ((epm->epm_ifmedia & IFM_FDX) == 0) {
679 1.46 thorpej defmedia = epm->epm_ifmedia;
680 1.46 thorpej defmedianame = epm->epm_name;
681 1.46 thorpej }
682 1.46 thorpej } else if (defmedia == 0) {
683 1.41 thorpej defmedia = epm->epm_ifmedia;
684 1.41 thorpej defmedianame = epm->epm_name;
685 1.41 thorpej }
686 1.41 thorpej ifmedia_add(ifm, epm->epm_ifmedia, epm->epm_epmedia,
687 1.41 thorpej NULL);
688 1.41 thorpej PRINT(epm->epm_name);
689 1.23 jonathan }
690 1.23 jonathan }
691 1.15 jonathan
692 1.41 thorpej #undef PRINT
693 1.41 thorpej
694 1.41 thorpej #ifdef DIAGNOSTIC
695 1.41 thorpej if (defmedia == 0)
696 1.41 thorpej panic("ep_vortex_probemedia: impossible");
697 1.41 thorpej #endif
698 1.41 thorpej
699 1.41 thorpej printf(" (default %s)\n", defmedianame);
700 1.41 thorpej ifmedia_set(ifm, defmedia);
701 1.41 thorpej }
702 1.41 thorpej
703 1.41 thorpej /*
704 1.41 thorpej * One second timer, used to tick the MII.
705 1.41 thorpej */
706 1.41 thorpej void
707 1.41 thorpej ep_tick(arg)
708 1.41 thorpej void *arg;
709 1.41 thorpej {
710 1.41 thorpej struct ep_softc *sc = arg;
711 1.41 thorpej int s;
712 1.15 jonathan
713 1.41 thorpej #ifdef DIAGNOSTIC
714 1.47 fvdl if ((sc->ep_flags & ELINK_FLAGS_MII) == 0)
715 1.41 thorpej panic("ep_tick");
716 1.41 thorpej #endif
717 1.31 jonathan
718 1.41 thorpej s = splnet();
719 1.41 thorpej mii_tick(&sc->sc_mii);
720 1.41 thorpej splx(s);
721 1.15 jonathan
722 1.41 thorpej timeout(ep_tick, sc, hz);
723 1.15 jonathan }
724 1.15 jonathan
725 1.15 jonathan /*
726 1.20 jonathan * Bring device up.
727 1.20 jonathan *
728 1.1 thorpej * The order in here seems important. Otherwise we may not receive
729 1.1 thorpej * interrupts. ?!
730 1.1 thorpej */
731 1.1 thorpej void
732 1.1 thorpej epinit(sc)
733 1.1 thorpej register struct ep_softc *sc;
734 1.1 thorpej {
735 1.21 is register struct ifnet *ifp = &sc->sc_ethercom.ec_if;
736 1.11 thorpej bus_space_tag_t iot = sc->sc_iot;
737 1.11 thorpej bus_space_handle_t ioh = sc->sc_ioh;
738 1.1 thorpej int i;
739 1.1 thorpej
740 1.56 jonathan /* Make sure any pending reset has completed before touching board. */
741 1.56 jonathan ep_finish_reset(iot, ioh);
742 1.56 jonathan
743 1.1 thorpej
744 1.47 fvdl if (sc->bustype != ELINK_BUS_PCI) {
745 1.1 thorpej GO_WINDOW(0);
746 1.47 fvdl bus_space_write_2(iot, ioh, ELINK_W0_CONFIG_CTRL, 0);
747 1.60 enami bus_space_write_2(iot, ioh, ELINK_W0_CONFIG_CTRL,
748 1.60 enami ENABLE_DRQ_IRQ);
749 1.1 thorpej }
750 1.1 thorpej
751 1.47 fvdl if (sc->bustype == ELINK_BUS_PCMCIA) {
752 1.47 fvdl bus_space_write_2(iot, ioh, ELINK_W0_RESOURCE_CFG, 0x3f00);
753 1.1 thorpej }
754 1.1 thorpej
755 1.1 thorpej GO_WINDOW(2);
756 1.1 thorpej for (i = 0; i < 6; i++) /* Reload the ether_addr. */
757 1.47 fvdl bus_space_write_1(iot, ioh, ELINK_W2_ADDR_0 + i,
758 1.21 is LLADDR(ifp->if_sadl)[i]);
759 1.8 christos
760 1.12 jonathan /*
761 1.12 jonathan * Reset the station-address receive filter.
762 1.41 thorpej * A bug workaround for busmastering (Vortex, Demon) cards.
763 1.12 jonathan */
764 1.12 jonathan for (i = 0; i < 6; i++)
765 1.47 fvdl bus_space_write_1(iot, ioh, ELINK_W2_RECVMASK_0 + i, 0);
766 1.1 thorpej
767 1.56 jonathan ep_reset_cmd(sc, ELINK_COMMAND, RX_RESET);
768 1.56 jonathan ep_reset_cmd(sc, ELINK_COMMAND, TX_RESET);
769 1.1 thorpej
770 1.1 thorpej GO_WINDOW(1); /* Window 1 is operating window */
771 1.1 thorpej for (i = 0; i < 31; i++)
772 1.47 fvdl bus_space_read_1(iot, ioh, ep_w1_reg(sc, ELINK_W1_TX_STATUS));
773 1.31 jonathan
774 1.31 jonathan /* Set threshhold for for Tx-space avaiable interrupt. */
775 1.47 fvdl bus_space_write_2(iot, ioh, ELINK_COMMAND,
776 1.31 jonathan SET_TX_AVAIL_THRESH | (1600 >> sc->ep_pktlenshift));
777 1.1 thorpej
778 1.47 fvdl if (sc->ep_chipset == ELINK_CHIPSET_ROADRUNNER) {
779 1.44 thorpej /*
780 1.44 thorpej * Enable options in the PCMCIA LAN COR register, via
781 1.44 thorpej * RoadRunner Window 1.
782 1.44 thorpej *
783 1.44 thorpej * XXX MAGIC CONSTANTS!
784 1.44 thorpej */
785 1.44 thorpej u_int16_t cor;
786 1.44 thorpej
787 1.47 fvdl bus_space_write_2(iot, ioh, ELINK_W1_RUNNER_RDCTL, (1 << 11));
788 1.44 thorpej
789 1.44 thorpej cor = bus_space_read_2(iot, ioh, 0) & ~0x30;
790 1.47 fvdl if (sc->ep_flags & ELINK_FLAGS_USESHAREDMEM)
791 1.44 thorpej cor |= 0x10;
792 1.47 fvdl if (sc->ep_flags & ELINK_FLAGS_FORCENOWAIT)
793 1.44 thorpej cor |= 0x20;
794 1.44 thorpej bus_space_write_2(iot, ioh, 0, cor);
795 1.44 thorpej
796 1.47 fvdl bus_space_write_2(iot, ioh, ELINK_W1_RUNNER_WRCTL, 0);
797 1.47 fvdl bus_space_write_2(iot, ioh, ELINK_W1_RUNNER_RDCTL, 0);
798 1.59 thorpej
799 1.59 thorpej if (sc->ep_flags & ELINK_FLAGS_MII) {
800 1.59 thorpej ep_roadrunner_mii_enable(sc);
801 1.59 thorpej GO_WINDOW(1);
802 1.59 thorpej }
803 1.44 thorpej }
804 1.44 thorpej
805 1.18 jonathan /* Enable interrupts. */
806 1.60 enami bus_space_write_2(iot, ioh, ELINK_COMMAND,
807 1.60 enami SET_RD_0_MASK | S_CARD_FAILURE | S_RX_COMPLETE | S_TX_COMPLETE |
808 1.60 enami S_TX_AVAIL);
809 1.60 enami bus_space_write_2(iot, ioh, ELINK_COMMAND,
810 1.60 enami SET_INTR_MASK | S_CARD_FAILURE | S_RX_COMPLETE | S_TX_COMPLETE |
811 1.60 enami S_TX_AVAIL);
812 1.1 thorpej
813 1.1 thorpej /*
814 1.1 thorpej * Attempt to get rid of any stray interrupts that occured during
815 1.1 thorpej * configuration. On the i386 this isn't possible because one may
816 1.1 thorpej * already be queued. However, a single stray interrupt is
817 1.1 thorpej * unimportant.
818 1.1 thorpej */
819 1.47 fvdl bus_space_write_2(iot, ioh, ELINK_COMMAND, ACK_INTR | 0xff);
820 1.1 thorpej
821 1.1 thorpej epsetfilter(sc);
822 1.41 thorpej epsetmedia(sc);
823 1.1 thorpej
824 1.47 fvdl bus_space_write_2(iot, ioh, ELINK_COMMAND, RX_ENABLE);
825 1.47 fvdl bus_space_write_2(iot, ioh, ELINK_COMMAND, TX_ENABLE);
826 1.1 thorpej
827 1.1 thorpej epmbuffill(sc);
828 1.1 thorpej
829 1.1 thorpej /* Interface is now `running', with no output active. */
830 1.1 thorpej ifp->if_flags |= IFF_RUNNING;
831 1.1 thorpej ifp->if_flags &= ~IFF_OACTIVE;
832 1.1 thorpej
833 1.47 fvdl if (sc->ep_flags & ELINK_FLAGS_MII) {
834 1.41 thorpej /* Start the one second clock. */
835 1.41 thorpej timeout(ep_tick, sc, hz);
836 1.41 thorpej }
837 1.41 thorpej
838 1.1 thorpej /* Attempt to start output, if any. */
839 1.1 thorpej epstart(ifp);
840 1.1 thorpej }
841 1.1 thorpej
842 1.20 jonathan
843 1.20 jonathan /*
844 1.20 jonathan * Set multicast receive filter.
845 1.20 jonathan * elink3 hardware has no selective multicast filter in hardware.
846 1.20 jonathan * Enable reception of all multicasts and filter in software.
847 1.20 jonathan */
848 1.1 thorpej void
849 1.1 thorpej epsetfilter(sc)
850 1.1 thorpej register struct ep_softc *sc;
851 1.1 thorpej {
852 1.21 is register struct ifnet *ifp = &sc->sc_ethercom.ec_if;
853 1.1 thorpej
854 1.1 thorpej GO_WINDOW(1); /* Window 1 is operating window */
855 1.60 enami bus_space_write_2(sc->sc_iot, sc->sc_ioh, ELINK_COMMAND,
856 1.60 enami SET_RX_FILTER | FIL_INDIVIDUAL | FIL_BRDCST |
857 1.60 enami ((ifp->if_flags & IFF_MULTICAST) ? FIL_MULTICAST : 0) |
858 1.60 enami ((ifp->if_flags & IFF_PROMISC) ? FIL_PROMISC : 0));
859 1.1 thorpej }
860 1.1 thorpej
861 1.23 jonathan int
862 1.23 jonathan ep_media_change(ifp)
863 1.23 jonathan struct ifnet *ifp;
864 1.23 jonathan {
865 1.23 jonathan register struct ep_softc *sc = ifp->if_softc;
866 1.23 jonathan
867 1.41 thorpej if (sc->enabled && (ifp->if_flags & IFF_UP) != 0)
868 1.41 thorpej epreset(sc);
869 1.34 thorpej
870 1.34 thorpej return (0);
871 1.23 jonathan }
872 1.23 jonathan
873 1.15 jonathan /*
874 1.59 thorpej * Reset and enable the MII on the RoadRunner.
875 1.59 thorpej */
876 1.59 thorpej void
877 1.59 thorpej ep_roadrunner_mii_enable(sc)
878 1.59 thorpej struct ep_softc *sc;
879 1.59 thorpej {
880 1.59 thorpej bus_space_tag_t iot = sc->sc_iot;
881 1.59 thorpej bus_space_handle_t ioh = sc->sc_ioh;
882 1.59 thorpej
883 1.59 thorpej GO_WINDOW(3);
884 1.59 thorpej bus_space_write_2(iot, ioh, ELINK_W3_RESET_OPTIONS,
885 1.59 thorpej ELINK_PCI_100BASE_MII|ELINK_RUNNER_ENABLE_MII);
886 1.59 thorpej delay(1000);
887 1.59 thorpej bus_space_write_2(iot, ioh, ELINK_W3_RESET_OPTIONS,
888 1.59 thorpej ELINK_PCI_100BASE_MII|ELINK_RUNNER_MII_RESET|
889 1.59 thorpej ELINK_RUNNER_ENABLE_MII);
890 1.59 thorpej ep_reset_cmd(sc, ELINK_COMMAND, TX_RESET);
891 1.59 thorpej ep_reset_cmd(sc, ELINK_COMMAND, RX_RESET);
892 1.59 thorpej delay(1000);
893 1.59 thorpej bus_space_write_2(iot, ioh, ELINK_W3_RESET_OPTIONS,
894 1.59 thorpej ELINK_PCI_100BASE_MII|ELINK_RUNNER_ENABLE_MII);
895 1.59 thorpej }
896 1.59 thorpej
897 1.59 thorpej /*
898 1.41 thorpej * Set the card to use the specified media.
899 1.15 jonathan */
900 1.34 thorpej void
901 1.41 thorpej epsetmedia(sc)
902 1.41 thorpej struct ep_softc *sc;
903 1.1 thorpej {
904 1.11 thorpej bus_space_tag_t iot = sc->sc_iot;
905 1.11 thorpej bus_space_handle_t ioh = sc->sc_ioh;
906 1.23 jonathan
907 1.41 thorpej /* Turn everything off. First turn off linkbeat and UTP. */
908 1.1 thorpej GO_WINDOW(4);
909 1.47 fvdl bus_space_write_2(iot, ioh, ELINK_W4_MEDIA_TYPE, 0x0);
910 1.23 jonathan
911 1.23 jonathan /* Turn off coax */
912 1.47 fvdl bus_space_write_2(iot, ioh, ELINK_COMMAND, STOP_TRANSCEIVER);
913 1.23 jonathan delay(1000);
914 1.23 jonathan
915 1.29 jonathan /*
916 1.41 thorpej * If the device has MII, select it, and then tell the
917 1.41 thorpej * PHY which media to use.
918 1.41 thorpej */
919 1.47 fvdl if (sc->ep_flags & ELINK_FLAGS_MII) {
920 1.41 thorpej int config0, config1;
921 1.41 thorpej
922 1.41 thorpej GO_WINDOW(3);
923 1.44 thorpej
924 1.59 thorpej #if 0
925 1.47 fvdl if (sc->ep_chipset == ELINK_CHIPSET_ROADRUNNER) {
926 1.44 thorpej int resopt;
927 1.44 thorpej
928 1.44 thorpej resopt = bus_space_read_2(iot, ioh,
929 1.47 fvdl ELINK_W3_RESET_OPTIONS);
930 1.60 enami bus_space_write_2(iot, ioh, ELINK_W3_RESET_OPTIONS,
931 1.60 enami resopt | ELINK_RUNNER_ENABLE_MII);
932 1.44 thorpej }
933 1.59 thorpej #endif
934 1.44 thorpej
935 1.41 thorpej config0 = (u_int)bus_space_read_2(iot, ioh,
936 1.47 fvdl ELINK_W3_INTERNAL_CONFIG);
937 1.41 thorpej config1 = (u_int)bus_space_read_2(iot, ioh,
938 1.47 fvdl ELINK_W3_INTERNAL_CONFIG + 2);
939 1.41 thorpej
940 1.41 thorpej config1 = config1 & ~CONFIG_MEDIAMASK;
941 1.47 fvdl config1 |= (ELINKMEDIA_MII << CONFIG_MEDIAMASK_SHIFT);
942 1.41 thorpej
943 1.47 fvdl bus_space_write_2(iot, ioh, ELINK_W3_INTERNAL_CONFIG, config0);
944 1.60 enami bus_space_write_2(iot, ioh, ELINK_W3_INTERNAL_CONFIG + 2,
945 1.60 enami config1);
946 1.41 thorpej GO_WINDOW(1); /* back to operating window */
947 1.41 thorpej
948 1.41 thorpej mii_mediachg(&sc->sc_mii);
949 1.41 thorpej return;
950 1.41 thorpej }
951 1.41 thorpej
952 1.41 thorpej /*
953 1.29 jonathan * Now turn on the selected media/transceiver.
954 1.29 jonathan */
955 1.29 jonathan GO_WINDOW(4);
956 1.41 thorpej switch (IFM_SUBTYPE(sc->sc_mii.mii_media.ifm_cur->ifm_media)) {
957 1.41 thorpej case IFM_10_T:
958 1.47 fvdl bus_space_write_2(iot, ioh, ELINK_W4_MEDIA_TYPE,
959 1.41 thorpej JABBER_GUARD_ENABLE|LINKBEAT_ENABLE);
960 1.23 jonathan break;
961 1.23 jonathan
962 1.41 thorpej case IFM_10_2:
963 1.47 fvdl bus_space_write_2(iot, ioh, ELINK_COMMAND, START_TRANSCEIVER);
964 1.23 jonathan DELAY(1000); /* 50ms not enmough? */
965 1.23 jonathan break;
966 1.23 jonathan
967 1.41 thorpej case IFM_100_TX:
968 1.41 thorpej case IFM_100_FX:
969 1.41 thorpej case IFM_100_T4: /* XXX check documentation */
970 1.47 fvdl bus_space_write_2(iot, ioh, ELINK_W4_MEDIA_TYPE,
971 1.41 thorpej LINKBEAT_ENABLE);
972 1.23 jonathan DELAY(1000); /* not strictly necessary? */
973 1.23 jonathan break;
974 1.23 jonathan
975 1.41 thorpej case IFM_10_5:
976 1.47 fvdl bus_space_write_2(iot, ioh, ELINK_W4_MEDIA_TYPE,
977 1.41 thorpej SQE_ENABLE);
978 1.41 thorpej DELAY(1000); /* not strictly necessary? */
979 1.41 thorpej break;
980 1.41 thorpej
981 1.41 thorpej case IFM_MANUAL:
982 1.41 thorpej /*
983 1.41 thorpej * Nothing to do here; we are actually enabling the
984 1.41 thorpej * external PHY on the MII port.
985 1.41 thorpej */
986 1.23 jonathan break;
987 1.41 thorpej
988 1.41 thorpej case IFM_NONE:
989 1.41 thorpej printf("%s: interface disabled\n", sc->sc_dev.dv_xname);
990 1.41 thorpej return;
991 1.41 thorpej
992 1.23 jonathan default:
993 1.41 thorpej panic("epsetmedia: impossible");
994 1.1 thorpej }
995 1.23 jonathan
996 1.23 jonathan /*
997 1.41 thorpej * Tell the chip which port to use.
998 1.23 jonathan */
999 1.41 thorpej switch (sc->ep_chipset) {
1000 1.47 fvdl case ELINK_CHIPSET_VORTEX:
1001 1.47 fvdl case ELINK_CHIPSET_BOOMERANG:
1002 1.41 thorpej {
1003 1.45 thorpej int mctl, config0, config1;
1004 1.23 jonathan
1005 1.23 jonathan GO_WINDOW(3);
1006 1.23 jonathan config0 = (u_int)bus_space_read_2(iot, ioh,
1007 1.47 fvdl ELINK_W3_INTERNAL_CONFIG);
1008 1.23 jonathan config1 = (u_int)bus_space_read_2(iot, ioh,
1009 1.47 fvdl ELINK_W3_INTERNAL_CONFIG + 2);
1010 1.23 jonathan
1011 1.23 jonathan config1 = config1 & ~CONFIG_MEDIAMASK;
1012 1.41 thorpej config1 |= (sc->sc_mii.mii_media.ifm_cur->ifm_data <<
1013 1.41 thorpej CONFIG_MEDIAMASK_SHIFT);
1014 1.41 thorpej
1015 1.47 fvdl bus_space_write_2(iot, ioh, ELINK_W3_INTERNAL_CONFIG, config0);
1016 1.47 fvdl bus_space_write_2(iot, ioh, ELINK_W3_INTERNAL_CONFIG + 2,
1017 1.47 fvdl config1);
1018 1.45 thorpej
1019 1.47 fvdl mctl = bus_space_read_2(iot, ioh, ELINK_W3_MAC_CONTROL);
1020 1.45 thorpej if (sc->sc_mii.mii_media.ifm_cur->ifm_media & IFM_FDX)
1021 1.45 thorpej mctl |= MAC_CONTROL_FDX;
1022 1.45 thorpej else
1023 1.45 thorpej mctl &= ~MAC_CONTROL_FDX;
1024 1.47 fvdl bus_space_write_2(iot, ioh, ELINK_W3_MAC_CONTROL, mctl);
1025 1.41 thorpej break;
1026 1.41 thorpej }
1027 1.41 thorpej default:
1028 1.41 thorpej {
1029 1.41 thorpej int w0_addr_cfg;
1030 1.28 veego
1031 1.28 veego GO_WINDOW(0);
1032 1.47 fvdl w0_addr_cfg = bus_space_read_2(iot, ioh, ELINK_W0_ADDRESS_CFG);
1033 1.29 jonathan w0_addr_cfg &= 0x3fff;
1034 1.47 fvdl bus_space_write_2(iot, ioh, ELINK_W0_ADDRESS_CFG, w0_addr_cfg |
1035 1.41 thorpej (sc->sc_mii.mii_media.ifm_cur->ifm_data << 14));
1036 1.28 veego DELAY(1000);
1037 1.41 thorpej break;
1038 1.41 thorpej }
1039 1.23 jonathan }
1040 1.23 jonathan
1041 1.23 jonathan GO_WINDOW(1); /* Window 1 is operating window */
1042 1.23 jonathan }
1043 1.23 jonathan
1044 1.23 jonathan /*
1045 1.23 jonathan * Get currently-selected media from card.
1046 1.23 jonathan * (if_media callback, may be called before interface is brought up).
1047 1.23 jonathan */
1048 1.23 jonathan void
1049 1.23 jonathan ep_media_status(ifp, req)
1050 1.23 jonathan struct ifnet *ifp;
1051 1.23 jonathan struct ifmediareq *req;
1052 1.23 jonathan {
1053 1.23 jonathan register struct ep_softc *sc = ifp->if_softc;
1054 1.23 jonathan bus_space_tag_t iot = sc->sc_iot;
1055 1.23 jonathan bus_space_handle_t ioh = sc->sc_ioh;
1056 1.23 jonathan
1057 1.34 thorpej if (sc->enabled == 0) {
1058 1.34 thorpej req->ifm_active = IFM_ETHER|IFM_NONE;
1059 1.34 thorpej req->ifm_status = 0;
1060 1.34 thorpej return;
1061 1.34 thorpej }
1062 1.34 thorpej
1063 1.41 thorpej /*
1064 1.41 thorpej * If we have MII, go ask the PHY what's going on.
1065 1.41 thorpej */
1066 1.47 fvdl if (sc->ep_flags & ELINK_FLAGS_MII) {
1067 1.41 thorpej mii_pollstat(&sc->sc_mii);
1068 1.41 thorpej req->ifm_active = sc->sc_mii.mii_media_active;
1069 1.41 thorpej req->ifm_status = sc->sc_mii.mii_media_status;
1070 1.41 thorpej return;
1071 1.41 thorpej }
1072 1.41 thorpej
1073 1.41 thorpej /*
1074 1.41 thorpej * Ok, at this point we claim that our active media is
1075 1.41 thorpej * the currently selected media. We'll update our status
1076 1.41 thorpej * if our chipset allows us to detect link.
1077 1.41 thorpej */
1078 1.41 thorpej req->ifm_active = sc->sc_mii.mii_media.ifm_cur->ifm_media;
1079 1.41 thorpej req->ifm_status = 0;
1080 1.41 thorpej
1081 1.23 jonathan switch (sc->ep_chipset) {
1082 1.47 fvdl case ELINK_CHIPSET_VORTEX:
1083 1.47 fvdl case ELINK_CHIPSET_BOOMERANG:
1084 1.23 jonathan GO_WINDOW(4);
1085 1.41 thorpej req->ifm_status = IFM_AVALID;
1086 1.47 fvdl if (bus_space_read_2(iot, ioh, ELINK_W4_MEDIA_TYPE) &
1087 1.41 thorpej LINKBEAT_DETECT)
1088 1.41 thorpej req->ifm_status |= IFM_ACTIVE;
1089 1.41 thorpej GO_WINDOW(1); /* back to operating window */
1090 1.23 jonathan break;
1091 1.1 thorpej }
1092 1.1 thorpej }
1093 1.1 thorpej
1094 1.23 jonathan
1095 1.23 jonathan
1096 1.1 thorpej /*
1097 1.1 thorpej * Start outputting on the interface.
1098 1.1 thorpej * Always called as splnet().
1099 1.1 thorpej */
1100 1.1 thorpej void
1101 1.1 thorpej epstart(ifp)
1102 1.1 thorpej struct ifnet *ifp;
1103 1.1 thorpej {
1104 1.5 thorpej register struct ep_softc *sc = ifp->if_softc;
1105 1.11 thorpej bus_space_tag_t iot = sc->sc_iot;
1106 1.11 thorpej bus_space_handle_t ioh = sc->sc_ioh;
1107 1.1 thorpej struct mbuf *m, *m0;
1108 1.1 thorpej int sh, len, pad;
1109 1.42 thorpej bus_addr_t txreg;
1110 1.1 thorpej
1111 1.1 thorpej /* Don't transmit if interface is busy or not running */
1112 1.28 veego if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
1113 1.1 thorpej return;
1114 1.1 thorpej
1115 1.1 thorpej startagain:
1116 1.1 thorpej /* Sneak a peek at the next packet */
1117 1.1 thorpej m0 = ifp->if_snd.ifq_head;
1118 1.1 thorpej if (m0 == 0)
1119 1.1 thorpej return;
1120 1.1 thorpej
1121 1.1 thorpej /* We need to use m->m_pkthdr.len, so require the header */
1122 1.1 thorpej if ((m0->m_flags & M_PKTHDR) == 0)
1123 1.1 thorpej panic("epstart: no header mbuf");
1124 1.1 thorpej len = m0->m_pkthdr.len;
1125 1.1 thorpej
1126 1.1 thorpej pad = (4 - len) & 3;
1127 1.1 thorpej
1128 1.1 thorpej /*
1129 1.1 thorpej * The 3c509 automatically pads short packets to minimum ethernet
1130 1.1 thorpej * length, but we drop packets that are too large. Perhaps we should
1131 1.1 thorpej * truncate them instead?
1132 1.1 thorpej */
1133 1.1 thorpej if (len + pad > ETHER_MAX_LEN) {
1134 1.1 thorpej /* packet is obviously too large: toss it */
1135 1.1 thorpej ++ifp->if_oerrors;
1136 1.1 thorpej IF_DEQUEUE(&ifp->if_snd, m0);
1137 1.1 thorpej m_freem(m0);
1138 1.1 thorpej goto readcheck;
1139 1.1 thorpej }
1140 1.1 thorpej
1141 1.47 fvdl if (bus_space_read_2(iot, ioh, ep_w1_reg(sc, ELINK_W1_FREE_TX)) <
1142 1.42 thorpej len + pad + 4) {
1143 1.47 fvdl bus_space_write_2(iot, ioh, ELINK_COMMAND,
1144 1.12 jonathan SET_TX_AVAIL_THRESH |
1145 1.12 jonathan ((len + pad + 4) >> sc->ep_pktlenshift));
1146 1.1 thorpej /* not enough room in FIFO */
1147 1.1 thorpej ifp->if_flags |= IFF_OACTIVE;
1148 1.1 thorpej return;
1149 1.1 thorpej } else {
1150 1.47 fvdl bus_space_write_2(iot, ioh, ELINK_COMMAND,
1151 1.60 enami SET_TX_AVAIL_THRESH | ELINK_THRESH_DISABLE);
1152 1.1 thorpej }
1153 1.1 thorpej
1154 1.1 thorpej IF_DEQUEUE(&ifp->if_snd, m0);
1155 1.1 thorpej if (m0 == 0) /* not really needed */
1156 1.1 thorpej return;
1157 1.1 thorpej
1158 1.47 fvdl bus_space_write_2(iot, ioh, ELINK_COMMAND, SET_TX_START_THRESH |
1159 1.60 enami ((len / 4 + sc->tx_start_thresh) /* >> sc->ep_pktlenshift*/));
1160 1.1 thorpej
1161 1.1 thorpej #if NBPFILTER > 0
1162 1.1 thorpej if (ifp->if_bpf)
1163 1.1 thorpej bpf_mtap(ifp->if_bpf, m0);
1164 1.1 thorpej #endif
1165 1.1 thorpej
1166 1.1 thorpej /*
1167 1.1 thorpej * Do the output at splhigh() so that an interrupt from another device
1168 1.1 thorpej * won't cause a FIFO underrun.
1169 1.1 thorpej */
1170 1.1 thorpej sh = splhigh();
1171 1.1 thorpej
1172 1.47 fvdl txreg = ep_w1_reg(sc, ELINK_W1_TX_PIO_WR_1);
1173 1.42 thorpej
1174 1.47 fvdl if (sc->ep_flags & ELINK_FLAGS_USEFIFOBUFFER) {
1175 1.44 thorpej /*
1176 1.44 thorpej * Prime the FIFO buffer counter (number of 16-bit
1177 1.44 thorpej * words about to be written to the FIFO).
1178 1.44 thorpej *
1179 1.44 thorpej * NOTE: NO OTHER ACCESS CAN BE PERFORMED WHILE THIS
1180 1.44 thorpej * COUNTER IS NON-ZERO!
1181 1.44 thorpej */
1182 1.47 fvdl bus_space_write_2(iot, ioh, ELINK_W1_RUNNER_WRCTL,
1183 1.44 thorpej (len + pad) >> 1);
1184 1.44 thorpej }
1185 1.44 thorpej
1186 1.42 thorpej bus_space_write_2(iot, ioh, txreg, len);
1187 1.42 thorpej bus_space_write_2(iot, ioh, txreg, 0xffff); /* Second is meaningless */
1188 1.47 fvdl if (ELINK_IS_BUS_32(sc->bustype)) {
1189 1.60 enami for (m = m0; m;) {
1190 1.14 cjs if (m->m_len > 3) {
1191 1.14 cjs /* align our reads from core */
1192 1.14 cjs if (mtod(m, u_long) & 3) {
1193 1.14 cjs u_long count =
1194 1.14 cjs 4 - (mtod(m, u_long) & 3);
1195 1.14 cjs bus_space_write_multi_1(iot, ioh,
1196 1.42 thorpej txreg, mtod(m, u_int8_t *), count);
1197 1.14 cjs m->m_data =
1198 1.14 cjs (void *)(mtod(m, u_long) + count);
1199 1.14 cjs m->m_len -= count;
1200 1.14 cjs }
1201 1.55 jonathan bus_space_write_multi_stream_4(iot, ioh,
1202 1.42 thorpej txreg, mtod(m, u_int32_t *), m->m_len >> 2);
1203 1.14 cjs m->m_data = (void *)(mtod(m, u_long) +
1204 1.14 cjs (u_long)(m->m_len & ~3));
1205 1.14 cjs m->m_len -= m->m_len & ~3;
1206 1.14 cjs }
1207 1.14 cjs if (m->m_len) {
1208 1.11 thorpej bus_space_write_multi_1(iot, ioh,
1209 1.42 thorpej txreg, mtod(m, u_int8_t *), m->m_len);
1210 1.14 cjs }
1211 1.1 thorpej MFREE(m, m0);
1212 1.1 thorpej m = m0;
1213 1.1 thorpej }
1214 1.1 thorpej } else {
1215 1.60 enami for (m = m0; m;) {
1216 1.14 cjs if (m->m_len > 1) {
1217 1.14 cjs if (mtod(m, u_long) & 1) {
1218 1.14 cjs bus_space_write_1(iot, ioh,
1219 1.42 thorpej txreg, *(mtod(m, u_int8_t *)));
1220 1.14 cjs m->m_data =
1221 1.14 cjs (void *)(mtod(m, u_long) + 1);
1222 1.14 cjs m->m_len -= 1;
1223 1.14 cjs }
1224 1.55 jonathan bus_space_write_multi_stream_2(iot, ioh,
1225 1.42 thorpej txreg, mtod(m, u_int16_t *),
1226 1.14 cjs m->m_len >> 1);
1227 1.14 cjs }
1228 1.14 cjs if (m->m_len & 1) {
1229 1.42 thorpej bus_space_write_1(iot, ioh, txreg,
1230 1.2 thorpej *(mtod(m, u_int8_t *) + m->m_len - 1));
1231 1.14 cjs }
1232 1.1 thorpej MFREE(m, m0);
1233 1.1 thorpej m = m0;
1234 1.1 thorpej }
1235 1.1 thorpej }
1236 1.1 thorpej while (pad--)
1237 1.42 thorpej bus_space_write_1(iot, ioh, txreg, 0);
1238 1.1 thorpej
1239 1.1 thorpej splx(sh);
1240 1.1 thorpej
1241 1.1 thorpej ++ifp->if_opackets;
1242 1.1 thorpej
1243 1.1 thorpej readcheck:
1244 1.47 fvdl if ((bus_space_read_2(iot, ioh, ep_w1_reg(sc, ELINK_W1_RX_STATUS)) &
1245 1.42 thorpej ERR_INCOMPLETE) == 0) {
1246 1.1 thorpej /* We received a complete packet. */
1247 1.47 fvdl u_int16_t status = bus_space_read_2(iot, ioh, ELINK_STATUS);
1248 1.1 thorpej
1249 1.1 thorpej if ((status & S_INTR_LATCH) == 0) {
1250 1.1 thorpej /*
1251 1.1 thorpej * No interrupt, read the packet and continue
1252 1.1 thorpej * Is this supposed to happen? Is my motherboard
1253 1.1 thorpej * completely busted?
1254 1.1 thorpej */
1255 1.1 thorpej epread(sc);
1256 1.28 veego } else {
1257 1.1 thorpej /* Got an interrupt, return so that it gets serviced. */
1258 1.1 thorpej return;
1259 1.28 veego }
1260 1.28 veego } else {
1261 1.1 thorpej /* Check if we are stuck and reset [see XXX comment] */
1262 1.1 thorpej if (epstatus(sc)) {
1263 1.1 thorpej if (ifp->if_flags & IFF_DEBUG)
1264 1.10 christos printf("%s: adapter reset\n",
1265 1.9 christos sc->sc_dev.dv_xname);
1266 1.1 thorpej epreset(sc);
1267 1.1 thorpej }
1268 1.1 thorpej }
1269 1.1 thorpej
1270 1.1 thorpej goto startagain;
1271 1.1 thorpej }
1272 1.1 thorpej
1273 1.1 thorpej
1274 1.1 thorpej /*
1275 1.1 thorpej * XXX: The 3c509 card can get in a mode where both the fifo status bit
1276 1.1 thorpej * FIFOS_RX_OVERRUN and the status bit ERR_INCOMPLETE are set
1277 1.1 thorpej * We detect this situation and we reset the adapter.
1278 1.1 thorpej * It happens at times when there is a lot of broadcast traffic
1279 1.1 thorpej * on the cable (once in a blue moon).
1280 1.1 thorpej */
1281 1.1 thorpej static int
1282 1.1 thorpej epstatus(sc)
1283 1.1 thorpej register struct ep_softc *sc;
1284 1.1 thorpej {
1285 1.11 thorpej bus_space_tag_t iot = sc->sc_iot;
1286 1.11 thorpej bus_space_handle_t ioh = sc->sc_ioh;
1287 1.7 thorpej u_int16_t fifost;
1288 1.1 thorpej
1289 1.1 thorpej /*
1290 1.1 thorpej * Check the FIFO status and act accordingly
1291 1.1 thorpej */
1292 1.1 thorpej GO_WINDOW(4);
1293 1.47 fvdl fifost = bus_space_read_2(iot, ioh, ELINK_W4_FIFO_DIAG);
1294 1.1 thorpej GO_WINDOW(1);
1295 1.1 thorpej
1296 1.1 thorpej if (fifost & FIFOS_RX_UNDERRUN) {
1297 1.21 is if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
1298 1.10 christos printf("%s: RX underrun\n", sc->sc_dev.dv_xname);
1299 1.1 thorpej epreset(sc);
1300 1.1 thorpej return 0;
1301 1.1 thorpej }
1302 1.1 thorpej
1303 1.1 thorpej if (fifost & FIFOS_RX_STATUS_OVERRUN) {
1304 1.21 is if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
1305 1.10 christos printf("%s: RX Status overrun\n", sc->sc_dev.dv_xname);
1306 1.1 thorpej return 1;
1307 1.1 thorpej }
1308 1.1 thorpej
1309 1.1 thorpej if (fifost & FIFOS_RX_OVERRUN) {
1310 1.21 is if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
1311 1.10 christos printf("%s: RX overrun\n", sc->sc_dev.dv_xname);
1312 1.1 thorpej return 1;
1313 1.1 thorpej }
1314 1.1 thorpej
1315 1.1 thorpej if (fifost & FIFOS_TX_OVERRUN) {
1316 1.21 is if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
1317 1.10 christos printf("%s: TX overrun\n", sc->sc_dev.dv_xname);
1318 1.1 thorpej epreset(sc);
1319 1.1 thorpej return 0;
1320 1.1 thorpej }
1321 1.1 thorpej
1322 1.1 thorpej return 0;
1323 1.1 thorpej }
1324 1.1 thorpej
1325 1.1 thorpej
1326 1.1 thorpej static void
1327 1.1 thorpej eptxstat(sc)
1328 1.1 thorpej register struct ep_softc *sc;
1329 1.1 thorpej {
1330 1.11 thorpej bus_space_tag_t iot = sc->sc_iot;
1331 1.11 thorpej bus_space_handle_t ioh = sc->sc_ioh;
1332 1.1 thorpej int i;
1333 1.1 thorpej
1334 1.1 thorpej /*
1335 1.1 thorpej * We need to read+write TX_STATUS until we get a 0 status
1336 1.1 thorpej * in order to turn off the interrupt flag.
1337 1.1 thorpej */
1338 1.60 enami while ((i = bus_space_read_1(iot, ioh,
1339 1.60 enami ep_w1_reg(sc, ELINK_W1_TX_STATUS))) & TXS_COMPLETE) {
1340 1.47 fvdl bus_space_write_1(iot, ioh, ep_w1_reg(sc, ELINK_W1_TX_STATUS),
1341 1.42 thorpej 0x0);
1342 1.1 thorpej
1343 1.1 thorpej if (i & TXS_JABBER) {
1344 1.21 is ++sc->sc_ethercom.ec_if.if_oerrors;
1345 1.21 is if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
1346 1.10 christos printf("%s: jabber (%x)\n",
1347 1.1 thorpej sc->sc_dev.dv_xname, i);
1348 1.59 thorpej #if 1
1349 1.59 thorpej ep_reset_cmd(sc, ELINK_COMMAND, TX_RESET);
1350 1.59 thorpej #else
1351 1.1 thorpej epreset(sc);
1352 1.59 thorpej #endif
1353 1.1 thorpej } else if (i & TXS_UNDERRUN) {
1354 1.21 is ++sc->sc_ethercom.ec_if.if_oerrors;
1355 1.21 is if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
1356 1.10 christos printf("%s: fifo underrun (%x) @%d\n",
1357 1.1 thorpej sc->sc_dev.dv_xname, i,
1358 1.1 thorpej sc->tx_start_thresh);
1359 1.1 thorpej if (sc->tx_succ_ok < 100)
1360 1.1 thorpej sc->tx_start_thresh = min(ETHER_MAX_LEN,
1361 1.1 thorpej sc->tx_start_thresh + 20);
1362 1.1 thorpej sc->tx_succ_ok = 0;
1363 1.59 thorpej #if 1
1364 1.59 thorpej ep_reset_cmd(sc, ELINK_COMMAND, TX_RESET);
1365 1.59 thorpej #else
1366 1.1 thorpej epreset(sc);
1367 1.59 thorpej #endif
1368 1.1 thorpej } else if (i & TXS_MAX_COLLISION) {
1369 1.21 is ++sc->sc_ethercom.ec_if.if_collisions;
1370 1.47 fvdl bus_space_write_2(iot, ioh, ELINK_COMMAND, TX_ENABLE);
1371 1.21 is sc->sc_ethercom.ec_if.if_flags &= ~IFF_OACTIVE;
1372 1.1 thorpej } else
1373 1.1 thorpej sc->tx_succ_ok = (sc->tx_succ_ok+1) & 127;
1374 1.1 thorpej }
1375 1.1 thorpej }
1376 1.1 thorpej
1377 1.1 thorpej int
1378 1.1 thorpej epintr(arg)
1379 1.1 thorpej void *arg;
1380 1.1 thorpej {
1381 1.1 thorpej register struct ep_softc *sc = arg;
1382 1.11 thorpej bus_space_tag_t iot = sc->sc_iot;
1383 1.11 thorpej bus_space_handle_t ioh = sc->sc_ioh;
1384 1.21 is struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1385 1.7 thorpej u_int16_t status;
1386 1.1 thorpej int ret = 0;
1387 1.35 explorer int addrandom = 0;
1388 1.1 thorpej
1389 1.34 thorpej if (sc->enabled == 0)
1390 1.34 thorpej return (0);
1391 1.34 thorpej
1392 1.1 thorpej for (;;) {
1393 1.47 fvdl bus_space_write_2(iot, ioh, ELINK_COMMAND, C_INTR_LATCH);
1394 1.1 thorpej
1395 1.47 fvdl status = bus_space_read_2(iot, ioh, ELINK_STATUS);
1396 1.1 thorpej
1397 1.1 thorpej if ((status & (S_TX_COMPLETE | S_TX_AVAIL |
1398 1.34 thorpej S_RX_COMPLETE | S_CARD_FAILURE)) == 0) {
1399 1.34 thorpej if ((status & S_INTR_LATCH) == 0) {
1400 1.34 thorpej #if 0
1401 1.34 thorpej printf("%s: intr latch cleared\n",
1402 1.34 thorpej sc->sc_dev.dv_xname);
1403 1.34 thorpej #endif
1404 1.34 thorpej break;
1405 1.34 thorpej }
1406 1.34 thorpej }
1407 1.1 thorpej
1408 1.1 thorpej ret = 1;
1409 1.1 thorpej
1410 1.1 thorpej /*
1411 1.1 thorpej * Acknowledge any interrupts. It's important that we do this
1412 1.1 thorpej * first, since there would otherwise be a race condition.
1413 1.1 thorpej * Due to the i386 interrupt queueing, we may get spurious
1414 1.1 thorpej * interrupts occasionally.
1415 1.1 thorpej */
1416 1.47 fvdl bus_space_write_2(iot, ioh, ELINK_COMMAND, ACK_INTR |
1417 1.34 thorpej (status & (C_INTR_LATCH |
1418 1.34 thorpej C_CARD_FAILURE |
1419 1.34 thorpej C_TX_COMPLETE |
1420 1.34 thorpej C_TX_AVAIL |
1421 1.34 thorpej C_RX_COMPLETE |
1422 1.34 thorpej C_RX_EARLY |
1423 1.34 thorpej C_INT_RQD |
1424 1.34 thorpej C_UPD_STATS)));
1425 1.34 thorpej
1426 1.34 thorpej #if 0
1427 1.47 fvdl status = bus_space_read_2(iot, ioh, ELINK_STATUS);
1428 1.34 thorpej
1429 1.34 thorpej printf("%s: intr%s%s%s%s\n", sc->sc_dev.dv_xname,
1430 1.34 thorpej (status & S_RX_COMPLETE)?" RX_COMPLETE":"",
1431 1.34 thorpej (status & S_TX_COMPLETE)?" TX_COMPLETE":"",
1432 1.34 thorpej (status & S_TX_AVAIL)?" TX_AVAIL":"",
1433 1.34 thorpej (status & S_CARD_FAILURE)?" CARD_FAILURE":"");
1434 1.34 thorpej #endif
1435 1.1 thorpej
1436 1.35 explorer if (status & S_RX_COMPLETE) {
1437 1.1 thorpej epread(sc);
1438 1.35 explorer addrandom = 1;
1439 1.35 explorer }
1440 1.1 thorpej if (status & S_TX_AVAIL) {
1441 1.21 is sc->sc_ethercom.ec_if.if_flags &= ~IFF_OACTIVE;
1442 1.21 is epstart(&sc->sc_ethercom.ec_if);
1443 1.35 explorer addrandom = 1;
1444 1.1 thorpej }
1445 1.1 thorpej if (status & S_CARD_FAILURE) {
1446 1.10 christos printf("%s: adapter failure (%x)\n",
1447 1.9 christos sc->sc_dev.dv_xname, status);
1448 1.59 thorpej #if 1
1449 1.59 thorpej epinit(sc);
1450 1.59 thorpej #else
1451 1.1 thorpej epreset(sc);
1452 1.59 thorpej #endif
1453 1.1 thorpej return (1);
1454 1.1 thorpej }
1455 1.1 thorpej if (status & S_TX_COMPLETE) {
1456 1.1 thorpej eptxstat(sc);
1457 1.1 thorpej epstart(ifp);
1458 1.35 explorer addrandom = 1;
1459 1.1 thorpej }
1460 1.35 explorer
1461 1.35 explorer #if NRND > 0
1462 1.35 explorer if (status)
1463 1.35 explorer rnd_add_uint32(&sc->rnd_source, status);
1464 1.35 explorer #endif
1465 1.1 thorpej }
1466 1.1 thorpej
1467 1.1 thorpej /* no more interrupts */
1468 1.1 thorpej return (ret);
1469 1.1 thorpej }
1470 1.1 thorpej
1471 1.1 thorpej void
1472 1.1 thorpej epread(sc)
1473 1.1 thorpej register struct ep_softc *sc;
1474 1.1 thorpej {
1475 1.11 thorpej bus_space_tag_t iot = sc->sc_iot;
1476 1.11 thorpej bus_space_handle_t ioh = sc->sc_ioh;
1477 1.21 is struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1478 1.1 thorpej struct mbuf *m;
1479 1.1 thorpej struct ether_header *eh;
1480 1.1 thorpej int len;
1481 1.1 thorpej
1482 1.47 fvdl len = bus_space_read_2(iot, ioh, ep_w1_reg(sc, ELINK_W1_RX_STATUS));
1483 1.1 thorpej
1484 1.1 thorpej again:
1485 1.1 thorpej if (ifp->if_flags & IFF_DEBUG) {
1486 1.1 thorpej int err = len & ERR_MASK;
1487 1.1 thorpej char *s = NULL;
1488 1.1 thorpej
1489 1.1 thorpej if (len & ERR_INCOMPLETE)
1490 1.1 thorpej s = "incomplete packet";
1491 1.1 thorpej else if (err == ERR_OVERRUN)
1492 1.1 thorpej s = "packet overrun";
1493 1.1 thorpej else if (err == ERR_RUNT)
1494 1.1 thorpej s = "runt packet";
1495 1.1 thorpej else if (err == ERR_ALIGNMENT)
1496 1.1 thorpej s = "bad alignment";
1497 1.1 thorpej else if (err == ERR_CRC)
1498 1.1 thorpej s = "bad crc";
1499 1.1 thorpej else if (err == ERR_OVERSIZE)
1500 1.1 thorpej s = "oversized packet";
1501 1.1 thorpej else if (err == ERR_DRIBBLE)
1502 1.1 thorpej s = "dribble bits";
1503 1.1 thorpej
1504 1.1 thorpej if (s)
1505 1.10 christos printf("%s: %s\n", sc->sc_dev.dv_xname, s);
1506 1.1 thorpej }
1507 1.1 thorpej
1508 1.1 thorpej if (len & ERR_INCOMPLETE)
1509 1.1 thorpej return;
1510 1.1 thorpej
1511 1.1 thorpej if (len & ERR_RX) {
1512 1.1 thorpej ++ifp->if_ierrors;
1513 1.1 thorpej goto abort;
1514 1.1 thorpej }
1515 1.1 thorpej
1516 1.1 thorpej len &= RX_BYTES_MASK; /* Lower 11 bits = RX bytes. */
1517 1.1 thorpej
1518 1.1 thorpej /* Pull packet off interface. */
1519 1.1 thorpej m = epget(sc, len);
1520 1.1 thorpej if (m == 0) {
1521 1.1 thorpej ifp->if_ierrors++;
1522 1.1 thorpej goto abort;
1523 1.1 thorpej }
1524 1.1 thorpej
1525 1.1 thorpej ++ifp->if_ipackets;
1526 1.1 thorpej
1527 1.1 thorpej /* We assume the header fit entirely in one mbuf. */
1528 1.1 thorpej eh = mtod(m, struct ether_header *);
1529 1.1 thorpej
1530 1.1 thorpej #if NBPFILTER > 0
1531 1.1 thorpej /*
1532 1.1 thorpej * Check if there's a BPF listener on this interface.
1533 1.1 thorpej * If so, hand off the raw packet to BPF.
1534 1.1 thorpej */
1535 1.1 thorpej if (ifp->if_bpf) {
1536 1.1 thorpej bpf_mtap(ifp->if_bpf, m);
1537 1.1 thorpej
1538 1.1 thorpej /*
1539 1.1 thorpej * Note that the interface cannot be in promiscuous mode if
1540 1.1 thorpej * there are no BPF listeners. And if we are in promiscuous
1541 1.1 thorpej * mode, we have to check if this packet is really ours.
1542 1.1 thorpej */
1543 1.1 thorpej if ((ifp->if_flags & IFF_PROMISC) &&
1544 1.1 thorpej (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
1545 1.21 is bcmp(eh->ether_dhost, LLADDR(sc->sc_ethercom.ec_if.if_sadl),
1546 1.60 enami sizeof(eh->ether_dhost)) != 0) {
1547 1.1 thorpej m_freem(m);
1548 1.1 thorpej return;
1549 1.1 thorpej }
1550 1.1 thorpej }
1551 1.1 thorpej #endif
1552 1.58 thorpej (*ifp->if_input)(ifp, m);
1553 1.1 thorpej
1554 1.1 thorpej /*
1555 1.1 thorpej * In periods of high traffic we can actually receive enough
1556 1.1 thorpej * packets so that the fifo overrun bit will be set at this point,
1557 1.1 thorpej * even though we just read a packet. In this case we
1558 1.1 thorpej * are not going to receive any more interrupts. We check for
1559 1.1 thorpej * this condition and read again until the fifo is not full.
1560 1.1 thorpej * We could simplify this test by not using epstatus(), but
1561 1.1 thorpej * rechecking the RX_STATUS register directly. This test could
1562 1.1 thorpej * result in unnecessary looping in cases where there is a new
1563 1.1 thorpej * packet but the fifo is not full, but it will not fix the
1564 1.1 thorpej * stuck behavior.
1565 1.1 thorpej *
1566 1.1 thorpej * Even with this improvement, we still get packet overrun errors
1567 1.1 thorpej * which are hurting performance. Maybe when I get some more time
1568 1.1 thorpej * I'll modify epread() so that it can handle RX_EARLY interrupts.
1569 1.1 thorpej */
1570 1.1 thorpej if (epstatus(sc)) {
1571 1.42 thorpej len = bus_space_read_2(iot, ioh,
1572 1.47 fvdl ep_w1_reg(sc, ELINK_W1_RX_STATUS));
1573 1.1 thorpej /* Check if we are stuck and reset [see XXX comment] */
1574 1.1 thorpej if (len & ERR_INCOMPLETE) {
1575 1.1 thorpej if (ifp->if_flags & IFF_DEBUG)
1576 1.10 christos printf("%s: adapter reset\n",
1577 1.9 christos sc->sc_dev.dv_xname);
1578 1.1 thorpej epreset(sc);
1579 1.1 thorpej return;
1580 1.1 thorpej }
1581 1.1 thorpej goto again;
1582 1.1 thorpej }
1583 1.1 thorpej
1584 1.1 thorpej return;
1585 1.1 thorpej
1586 1.1 thorpej abort:
1587 1.56 jonathan ep_discard_rxtop(iot, ioh);
1588 1.56 jonathan
1589 1.1 thorpej }
1590 1.1 thorpej
1591 1.1 thorpej struct mbuf *
1592 1.1 thorpej epget(sc, totlen)
1593 1.1 thorpej struct ep_softc *sc;
1594 1.1 thorpej int totlen;
1595 1.1 thorpej {
1596 1.11 thorpej bus_space_tag_t iot = sc->sc_iot;
1597 1.11 thorpej bus_space_handle_t ioh = sc->sc_ioh;
1598 1.21 is struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1599 1.44 thorpej struct mbuf *top, **mp, *m, *rv = NULL;
1600 1.42 thorpej bus_addr_t rxreg;
1601 1.14 cjs int len, remaining;
1602 1.1 thorpej int sh;
1603 1.1 thorpej
1604 1.1 thorpej m = sc->mb[sc->next_mb];
1605 1.1 thorpej sc->mb[sc->next_mb] = 0;
1606 1.1 thorpej if (m == 0) {
1607 1.1 thorpej MGETHDR(m, M_DONTWAIT, MT_DATA);
1608 1.1 thorpej if (m == 0)
1609 1.1 thorpej return 0;
1610 1.1 thorpej } else {
1611 1.1 thorpej /* If the queue is no longer full, refill. */
1612 1.1 thorpej if (sc->last_mb == sc->next_mb)
1613 1.1 thorpej timeout(epmbuffill, sc, 1);
1614 1.1 thorpej /* Convert one of our saved mbuf's. */
1615 1.1 thorpej sc->next_mb = (sc->next_mb + 1) % MAX_MBS;
1616 1.1 thorpej m->m_data = m->m_pktdat;
1617 1.1 thorpej m->m_flags = M_PKTHDR;
1618 1.1 thorpej }
1619 1.1 thorpej m->m_pkthdr.rcvif = ifp;
1620 1.1 thorpej m->m_pkthdr.len = totlen;
1621 1.1 thorpej len = MHLEN;
1622 1.1 thorpej top = 0;
1623 1.1 thorpej mp = ⊤
1624 1.1 thorpej
1625 1.1 thorpej /*
1626 1.1 thorpej * We read the packet at splhigh() so that an interrupt from another
1627 1.1 thorpej * device doesn't cause the card's buffer to overflow while we're
1628 1.1 thorpej * reading it. We may still lose packets at other times.
1629 1.1 thorpej */
1630 1.1 thorpej sh = splhigh();
1631 1.1 thorpej
1632 1.47 fvdl rxreg = ep_w1_reg(sc, ELINK_W1_RX_PIO_RD_1);
1633 1.42 thorpej
1634 1.47 fvdl if (sc->ep_flags & ELINK_FLAGS_USEFIFOBUFFER) {
1635 1.44 thorpej /*
1636 1.44 thorpej * Prime the FIFO buffer counter (number of 16-bit
1637 1.44 thorpej * words about to be read from the FIFO).
1638 1.44 thorpej *
1639 1.44 thorpej * NOTE: NO OTHER ACCESS CAN BE PERFORMED WHILE THIS
1640 1.44 thorpej * COUNTER IS NON-ZERO!
1641 1.44 thorpej */
1642 1.47 fvdl bus_space_write_2(iot, ioh, ELINK_W1_RUNNER_RDCTL, totlen >> 1);
1643 1.44 thorpej }
1644 1.44 thorpej
1645 1.1 thorpej while (totlen > 0) {
1646 1.1 thorpej if (top) {
1647 1.1 thorpej m = sc->mb[sc->next_mb];
1648 1.1 thorpej sc->mb[sc->next_mb] = 0;
1649 1.1 thorpej if (m == 0) {
1650 1.1 thorpej MGET(m, M_DONTWAIT, MT_DATA);
1651 1.1 thorpej if (m == 0) {
1652 1.1 thorpej m_freem(top);
1653 1.44 thorpej goto out;
1654 1.1 thorpej }
1655 1.1 thorpej } else {
1656 1.1 thorpej sc->next_mb = (sc->next_mb + 1) % MAX_MBS;
1657 1.1 thorpej }
1658 1.1 thorpej len = MLEN;
1659 1.1 thorpej }
1660 1.1 thorpej if (totlen >= MINCLSIZE) {
1661 1.1 thorpej MCLGET(m, M_DONTWAIT);
1662 1.27 mycroft if ((m->m_flags & M_EXT) == 0) {
1663 1.30 mycroft m_free(m);
1664 1.26 mycroft m_freem(top);
1665 1.44 thorpej goto out;
1666 1.26 mycroft }
1667 1.26 mycroft len = MCLBYTES;
1668 1.1 thorpej }
1669 1.24 cjs if (top == 0) {
1670 1.25 cjs /* align the struct ip header */
1671 1.25 cjs caddr_t newdata = (caddr_t)
1672 1.25 cjs ALIGN(m->m_data + sizeof(struct ether_header))
1673 1.25 cjs - sizeof(struct ether_header);
1674 1.25 cjs len -= newdata - m->m_data;
1675 1.25 cjs m->m_data = newdata;
1676 1.14 cjs }
1677 1.14 cjs remaining = len = min(totlen, len);
1678 1.47 fvdl if (ELINK_IS_BUS_32(sc->bustype)) {
1679 1.14 cjs u_long offset = mtod(m, u_long);
1680 1.14 cjs /*
1681 1.14 cjs * Read bytes up to the point where we are aligned.
1682 1.14 cjs * (We can align to 4 bytes, rather than ALIGNBYTES,
1683 1.14 cjs * here because we're later reading 4-byte chunks.)
1684 1.14 cjs */
1685 1.14 cjs if ((remaining > 3) && (offset & 3)) {
1686 1.14 cjs int count = (4 - (offset & 3));
1687 1.14 cjs bus_space_read_multi_1(iot, ioh,
1688 1.42 thorpej rxreg, (u_int8_t *) offset, count);
1689 1.14 cjs offset += count;
1690 1.14 cjs remaining -= count;
1691 1.14 cjs }
1692 1.14 cjs if (remaining > 3) {
1693 1.55 jonathan bus_space_read_multi_stream_4(iot, ioh,
1694 1.42 thorpej rxreg, (u_int32_t *) offset,
1695 1.42 thorpej remaining >> 2);
1696 1.14 cjs offset += remaining & ~3;
1697 1.14 cjs remaining &= 3;
1698 1.14 cjs }
1699 1.14 cjs if (remaining) {
1700 1.11 thorpej bus_space_read_multi_1(iot, ioh,
1701 1.42 thorpej rxreg, (u_int8_t *) offset, remaining);
1702 1.14 cjs }
1703 1.1 thorpej } else {
1704 1.14 cjs u_long offset = mtod(m, u_long);
1705 1.14 cjs if ((remaining > 1) && (offset & 1)) {
1706 1.14 cjs bus_space_read_multi_1(iot, ioh,
1707 1.42 thorpej rxreg, (u_int8_t *) offset, 1);
1708 1.14 cjs remaining -= 1;
1709 1.14 cjs offset += 1;
1710 1.14 cjs }
1711 1.14 cjs if (remaining > 1) {
1712 1.55 jonathan bus_space_read_multi_stream_2(iot, ioh,
1713 1.42 thorpej rxreg, (u_int16_t *) offset,
1714 1.42 thorpej remaining >> 1);
1715 1.14 cjs offset += remaining & ~1;
1716 1.14 cjs }
1717 1.14 cjs if (remaining & 1) {
1718 1.14 cjs bus_space_read_multi_1(iot, ioh,
1719 1.42 thorpej rxreg, (u_int8_t *) offset, remaining & 1);
1720 1.14 cjs }
1721 1.1 thorpej }
1722 1.1 thorpej m->m_len = len;
1723 1.1 thorpej totlen -= len;
1724 1.1 thorpej *mp = m;
1725 1.1 thorpej mp = &m->m_next;
1726 1.1 thorpej }
1727 1.1 thorpej
1728 1.44 thorpej rv = top;
1729 1.44 thorpej
1730 1.56 jonathan ep_discard_rxtop(iot, ioh);
1731 1.1 thorpej
1732 1.44 thorpej out:
1733 1.47 fvdl if (sc->ep_flags & ELINK_FLAGS_USEFIFOBUFFER)
1734 1.47 fvdl bus_space_write_2(iot, ioh, ELINK_W1_RUNNER_RDCTL, 0);
1735 1.1 thorpej splx(sh);
1736 1.1 thorpej
1737 1.44 thorpej return rv;
1738 1.1 thorpej }
1739 1.1 thorpej
1740 1.1 thorpej int
1741 1.1 thorpej epioctl(ifp, cmd, data)
1742 1.1 thorpej register struct ifnet *ifp;
1743 1.1 thorpej u_long cmd;
1744 1.1 thorpej caddr_t data;
1745 1.1 thorpej {
1746 1.5 thorpej struct ep_softc *sc = ifp->if_softc;
1747 1.1 thorpej struct ifaddr *ifa = (struct ifaddr *)data;
1748 1.1 thorpej struct ifreq *ifr = (struct ifreq *)data;
1749 1.1 thorpej int s, error = 0;
1750 1.1 thorpej
1751 1.1 thorpej s = splnet();
1752 1.1 thorpej
1753 1.1 thorpej switch (cmd) {
1754 1.1 thorpej
1755 1.1 thorpej case SIOCSIFADDR:
1756 1.34 thorpej if ((error = epenable(sc)) != 0)
1757 1.34 thorpej break;
1758 1.34 thorpej /* epinit is called just below */
1759 1.1 thorpej ifp->if_flags |= IFF_UP;
1760 1.1 thorpej switch (ifa->ifa_addr->sa_family) {
1761 1.1 thorpej #ifdef INET
1762 1.1 thorpej case AF_INET:
1763 1.1 thorpej epinit(sc);
1764 1.21 is arp_ifinit(&sc->sc_ethercom.ec_if, ifa);
1765 1.1 thorpej break;
1766 1.1 thorpej #endif
1767 1.1 thorpej #ifdef NS
1768 1.1 thorpej case AF_NS:
1769 1.1 thorpej {
1770 1.1 thorpej register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1771 1.1 thorpej
1772 1.1 thorpej if (ns_nullhost(*ina))
1773 1.21 is ina->x_host = *(union ns_host *)
1774 1.21 is LLADDR(ifp->if_sadl);
1775 1.1 thorpej else
1776 1.1 thorpej bcopy(ina->x_host.c_host,
1777 1.21 is LLADDR(ifp->if_sadl),
1778 1.21 is ifp->if_addrlen);
1779 1.1 thorpej /* Set new address. */
1780 1.1 thorpej epinit(sc);
1781 1.1 thorpej break;
1782 1.1 thorpej }
1783 1.1 thorpej #endif
1784 1.1 thorpej default:
1785 1.1 thorpej epinit(sc);
1786 1.1 thorpej break;
1787 1.1 thorpej }
1788 1.1 thorpej break;
1789 1.1 thorpej
1790 1.23 jonathan case SIOCSIFMEDIA:
1791 1.23 jonathan case SIOCGIFMEDIA:
1792 1.41 thorpej error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
1793 1.23 jonathan break;
1794 1.23 jonathan
1795 1.1 thorpej case SIOCSIFFLAGS:
1796 1.1 thorpej if ((ifp->if_flags & IFF_UP) == 0 &&
1797 1.1 thorpej (ifp->if_flags & IFF_RUNNING) != 0) {
1798 1.1 thorpej /*
1799 1.1 thorpej * If interface is marked down and it is running, then
1800 1.1 thorpej * stop it.
1801 1.1 thorpej */
1802 1.1 thorpej epstop(sc);
1803 1.1 thorpej ifp->if_flags &= ~IFF_RUNNING;
1804 1.34 thorpej epdisable(sc);
1805 1.1 thorpej } else if ((ifp->if_flags & IFF_UP) != 0 &&
1806 1.1 thorpej (ifp->if_flags & IFF_RUNNING) == 0) {
1807 1.1 thorpej /*
1808 1.1 thorpej * If interface is marked up and it is stopped, then
1809 1.1 thorpej * start it.
1810 1.1 thorpej */
1811 1.34 thorpej if ((error = epenable(sc)) != 0)
1812 1.34 thorpej break;
1813 1.1 thorpej epinit(sc);
1814 1.52 thorpej } else if ((ifp->if_flags & IFF_UP) != 0) {
1815 1.1 thorpej /*
1816 1.1 thorpej * deal with flags changes:
1817 1.23 jonathan * IFF_MULTICAST, IFF_PROMISC.
1818 1.1 thorpej */
1819 1.1 thorpej epsetfilter(sc);
1820 1.1 thorpej }
1821 1.1 thorpej break;
1822 1.1 thorpej
1823 1.1 thorpej case SIOCADDMULTI:
1824 1.1 thorpej case SIOCDELMULTI:
1825 1.34 thorpej if (sc->enabled == 0) {
1826 1.34 thorpej error = EIO;
1827 1.34 thorpej break;
1828 1.34 thorpej }
1829 1.34 thorpej
1830 1.1 thorpej error = (cmd == SIOCADDMULTI) ?
1831 1.21 is ether_addmulti(ifr, &sc->sc_ethercom) :
1832 1.21 is ether_delmulti(ifr, &sc->sc_ethercom);
1833 1.1 thorpej
1834 1.1 thorpej if (error == ENETRESET) {
1835 1.1 thorpej /*
1836 1.1 thorpej * Multicast list has changed; set the hardware filter
1837 1.1 thorpej * accordingly.
1838 1.1 thorpej */
1839 1.1 thorpej epreset(sc);
1840 1.1 thorpej error = 0;
1841 1.1 thorpej }
1842 1.1 thorpej break;
1843 1.1 thorpej
1844 1.1 thorpej default:
1845 1.1 thorpej error = EINVAL;
1846 1.1 thorpej break;
1847 1.1 thorpej }
1848 1.1 thorpej
1849 1.1 thorpej splx(s);
1850 1.1 thorpej return (error);
1851 1.1 thorpej }
1852 1.1 thorpej
1853 1.1 thorpej void
1854 1.1 thorpej epreset(sc)
1855 1.1 thorpej struct ep_softc *sc;
1856 1.1 thorpej {
1857 1.1 thorpej int s;
1858 1.1 thorpej
1859 1.1 thorpej s = splnet();
1860 1.1 thorpej epstop(sc);
1861 1.1 thorpej epinit(sc);
1862 1.1 thorpej splx(s);
1863 1.1 thorpej }
1864 1.1 thorpej
1865 1.1 thorpej void
1866 1.5 thorpej epwatchdog(ifp)
1867 1.5 thorpej struct ifnet *ifp;
1868 1.1 thorpej {
1869 1.5 thorpej struct ep_softc *sc = ifp->if_softc;
1870 1.1 thorpej
1871 1.1 thorpej log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
1872 1.21 is ++sc->sc_ethercom.ec_if.if_oerrors;
1873 1.1 thorpej
1874 1.1 thorpej epreset(sc);
1875 1.1 thorpej }
1876 1.1 thorpej
1877 1.1 thorpej void
1878 1.1 thorpej epstop(sc)
1879 1.1 thorpej register struct ep_softc *sc;
1880 1.1 thorpej {
1881 1.11 thorpej bus_space_tag_t iot = sc->sc_iot;
1882 1.11 thorpej bus_space_handle_t ioh = sc->sc_ioh;
1883 1.1 thorpej
1884 1.47 fvdl if (sc->ep_flags & ELINK_FLAGS_MII) {
1885 1.41 thorpej /* Stop the one second clock. */
1886 1.41 thorpej untimeout(ep_tick, sc);
1887 1.44 thorpej }
1888 1.44 thorpej
1889 1.47 fvdl if (sc->ep_chipset == ELINK_CHIPSET_ROADRUNNER) {
1890 1.44 thorpej /*
1891 1.44 thorpej * Clear the FIFO buffer count, thus halting
1892 1.44 thorpej * any currently-running transactions.
1893 1.44 thorpej */
1894 1.44 thorpej GO_WINDOW(1); /* sanity */
1895 1.47 fvdl bus_space_write_2(iot, ioh, ELINK_W1_RUNNER_WRCTL, 0);
1896 1.47 fvdl bus_space_write_2(iot, ioh, ELINK_W1_RUNNER_RDCTL, 0);
1897 1.41 thorpej }
1898 1.41 thorpej
1899 1.47 fvdl bus_space_write_2(iot, ioh, ELINK_COMMAND, RX_DISABLE);
1900 1.56 jonathan ep_discard_rxtop(iot, ioh);
1901 1.56 jonathan
1902 1.47 fvdl bus_space_write_2(iot, ioh, ELINK_COMMAND, TX_DISABLE);
1903 1.47 fvdl bus_space_write_2(iot, ioh, ELINK_COMMAND, STOP_TRANSCEIVER);
1904 1.18 jonathan
1905 1.56 jonathan ep_reset_cmd(sc, ELINK_COMMAND, RX_RESET);
1906 1.56 jonathan ep_reset_cmd(sc, ELINK_COMMAND, TX_RESET);
1907 1.18 jonathan
1908 1.47 fvdl bus_space_write_2(iot, ioh, ELINK_COMMAND, C_INTR_LATCH);
1909 1.47 fvdl bus_space_write_2(iot, ioh, ELINK_COMMAND, SET_RD_0_MASK);
1910 1.47 fvdl bus_space_write_2(iot, ioh, ELINK_COMMAND, SET_INTR_MASK);
1911 1.47 fvdl bus_space_write_2(iot, ioh, ELINK_COMMAND, SET_RX_FILTER);
1912 1.1 thorpej
1913 1.1 thorpej epmbufempty(sc);
1914 1.1 thorpej }
1915 1.16 jonathan
1916 1.16 jonathan
1917 1.16 jonathan /*
1918 1.16 jonathan * Before reboots, reset card completely.
1919 1.16 jonathan */
1920 1.16 jonathan static void
1921 1.16 jonathan epshutdown(arg)
1922 1.16 jonathan void *arg;
1923 1.16 jonathan {
1924 1.16 jonathan register struct ep_softc *sc = arg;
1925 1.56 jonathan int s = splnet();
1926 1.16 jonathan
1927 1.34 thorpej if (sc->enabled) {
1928 1.34 thorpej epstop(sc);
1929 1.56 jonathan ep_reset_cmd(sc, ELINK_COMMAND, GLOBAL_RESET);
1930 1.56 jonathan sc->enabled = 0;
1931 1.34 thorpej }
1932 1.56 jonathan splx(s);
1933 1.16 jonathan }
1934 1.1 thorpej
1935 1.1 thorpej /*
1936 1.1 thorpej * We get eeprom data from the id_port given an offset into the
1937 1.1 thorpej * eeprom. Basically; after the ID_sequence is sent to all of
1938 1.1 thorpej * the cards; they enter the ID_CMD state where they will accept
1939 1.1 thorpej * command requests. 0x80-0xbf loads the eeprom data. We then
1940 1.1 thorpej * read the port 16 times and with every read; the cards check
1941 1.1 thorpej * for contention (ie: if one card writes a 0 bit and another
1942 1.1 thorpej * writes a 1 bit then the host sees a 0. At the end of the cycle;
1943 1.1 thorpej * each card compares the data on the bus; if there is a difference
1944 1.1 thorpej * then that card goes into ID_WAIT state again). In the meantime;
1945 1.1 thorpej * one bit of data is returned in the AX register which is conveniently
1946 1.11 thorpej * returned to us by bus_space_read_1(). Hence; we read 16 times getting one
1947 1.1 thorpej * bit of data with each read.
1948 1.2 thorpej *
1949 1.2 thorpej * NOTE: the caller must provide an i/o handle for ELINK_ID_PORT!
1950 1.1 thorpej */
1951 1.2 thorpej u_int16_t
1952 1.11 thorpej epreadeeprom(iot, ioh, offset)
1953 1.11 thorpej bus_space_tag_t iot;
1954 1.11 thorpej bus_space_handle_t ioh;
1955 1.2 thorpej int offset;
1956 1.1 thorpej {
1957 1.2 thorpej u_int16_t data = 0;
1958 1.2 thorpej int i;
1959 1.1 thorpej
1960 1.11 thorpej bus_space_write_1(iot, ioh, 0, 0x80 + offset);
1961 1.1 thorpej delay(1000);
1962 1.1 thorpej for (i = 0; i < 16; i++)
1963 1.11 thorpej data = (data << 1) | (bus_space_read_2(iot, ioh, 0) & 1);
1964 1.1 thorpej return (data);
1965 1.1 thorpej }
1966 1.1 thorpej
1967 1.1 thorpej static int
1968 1.1 thorpej epbusyeeprom(sc)
1969 1.1 thorpej struct ep_softc *sc;
1970 1.1 thorpej {
1971 1.11 thorpej bus_space_tag_t iot = sc->sc_iot;
1972 1.11 thorpej bus_space_handle_t ioh = sc->sc_ioh;
1973 1.1 thorpej int i = 100, j;
1974 1.1 thorpej
1975 1.47 fvdl if (sc->bustype == ELINK_BUS_PCMCIA) {
1976 1.1 thorpej delay(1000);
1977 1.1 thorpej return 0;
1978 1.1 thorpej }
1979 1.1 thorpej
1980 1.33 jonathan j = 0; /* bad GCC flow analysis */
1981 1.1 thorpej while (i--) {
1982 1.47 fvdl j = bus_space_read_2(iot, ioh, ELINK_W0_EEPROM_COMMAND);
1983 1.1 thorpej if (j & EEPROM_BUSY)
1984 1.1 thorpej delay(100);
1985 1.1 thorpej else
1986 1.1 thorpej break;
1987 1.1 thorpej }
1988 1.1 thorpej if (!i) {
1989 1.10 christos printf("\n%s: eeprom failed to come ready\n",
1990 1.1 thorpej sc->sc_dev.dv_xname);
1991 1.1 thorpej return (1);
1992 1.1 thorpej }
1993 1.1 thorpej if (j & EEPROM_TST_MODE) {
1994 1.29 jonathan /* XXX PnP mode? */
1995 1.28 veego printf("\n%s: erase pencil mark!\n", sc->sc_dev.dv_xname);
1996 1.1 thorpej return (1);
1997 1.1 thorpej }
1998 1.1 thorpej return (0);
1999 1.59 thorpej }
2000 1.59 thorpej
2001 1.59 thorpej u_int16_t
2002 1.59 thorpej ep_read_eeprom(sc, offset)
2003 1.59 thorpej struct ep_softc *sc;
2004 1.59 thorpej u_int16_t offset;
2005 1.59 thorpej {
2006 1.59 thorpej u_int16_t readcmd;
2007 1.59 thorpej
2008 1.59 thorpej /*
2009 1.59 thorpej * RoadRunner has a larger EEPROM, so a different read command
2010 1.59 thorpej * is required.
2011 1.59 thorpej */
2012 1.59 thorpej if (sc->ep_chipset == ELINK_CHIPSET_ROADRUNNER)
2013 1.59 thorpej readcmd = READ_EEPROM_RR;
2014 1.59 thorpej else
2015 1.59 thorpej readcmd = READ_EEPROM;
2016 1.59 thorpej
2017 1.59 thorpej if (epbusyeeprom(sc))
2018 1.59 thorpej return (0); /* XXX why is eeprom busy? */
2019 1.59 thorpej bus_space_write_2(sc->sc_iot, sc->sc_ioh, ELINK_W0_EEPROM_COMMAND,
2020 1.59 thorpej readcmd | offset);
2021 1.59 thorpej if (epbusyeeprom(sc))
2022 1.59 thorpej return (0); /* XXX why is eeprom busy? */
2023 1.59 thorpej return (bus_space_read_2(sc->sc_iot, sc->sc_ioh, ELINK_W0_EEPROM_DATA));
2024 1.1 thorpej }
2025 1.1 thorpej
2026 1.1 thorpej void
2027 1.3 christos epmbuffill(v)
2028 1.3 christos void *v;
2029 1.1 thorpej {
2030 1.3 christos struct ep_softc *sc = v;
2031 1.51 mycroft struct mbuf *m;
2032 1.1 thorpej int s, i;
2033 1.1 thorpej
2034 1.1 thorpej s = splnet();
2035 1.1 thorpej i = sc->last_mb;
2036 1.1 thorpej do {
2037 1.51 mycroft if (sc->mb[i] == 0) {
2038 1.51 mycroft MGET(m, M_DONTWAIT, MT_DATA);
2039 1.51 mycroft if (m == 0)
2040 1.51 mycroft break;
2041 1.51 mycroft sc->mb[i] = m;
2042 1.51 mycroft }
2043 1.1 thorpej i = (i + 1) % MAX_MBS;
2044 1.1 thorpej } while (i != sc->next_mb);
2045 1.1 thorpej sc->last_mb = i;
2046 1.1 thorpej /* If the queue was not filled, try again. */
2047 1.1 thorpej if (sc->last_mb != sc->next_mb)
2048 1.1 thorpej timeout(epmbuffill, sc, 1);
2049 1.1 thorpej splx(s);
2050 1.1 thorpej }
2051 1.1 thorpej
2052 1.1 thorpej void
2053 1.1 thorpej epmbufempty(sc)
2054 1.1 thorpej struct ep_softc *sc;
2055 1.1 thorpej {
2056 1.1 thorpej int s, i;
2057 1.1 thorpej
2058 1.1 thorpej s = splnet();
2059 1.1 thorpej for (i = 0; i<MAX_MBS; i++) {
2060 1.1 thorpej if (sc->mb[i]) {
2061 1.1 thorpej m_freem(sc->mb[i]);
2062 1.1 thorpej sc->mb[i] = NULL;
2063 1.1 thorpej }
2064 1.1 thorpej }
2065 1.1 thorpej sc->last_mb = sc->next_mb = 0;
2066 1.1 thorpej untimeout(epmbuffill, sc);
2067 1.1 thorpej splx(s);
2068 1.34 thorpej }
2069 1.34 thorpej
2070 1.34 thorpej int
2071 1.34 thorpej epenable(sc)
2072 1.34 thorpej struct ep_softc *sc;
2073 1.34 thorpej {
2074 1.34 thorpej
2075 1.34 thorpej if (sc->enabled == 0 && sc->enable != NULL) {
2076 1.34 thorpej if ((*sc->enable)(sc) != 0) {
2077 1.34 thorpej printf("%s: device enable failed\n",
2078 1.34 thorpej sc->sc_dev.dv_xname);
2079 1.34 thorpej return (EIO);
2080 1.34 thorpej }
2081 1.34 thorpej }
2082 1.34 thorpej
2083 1.34 thorpej sc->enabled = 1;
2084 1.34 thorpej return (0);
2085 1.34 thorpej }
2086 1.34 thorpej
2087 1.34 thorpej void
2088 1.34 thorpej epdisable(sc)
2089 1.34 thorpej struct ep_softc *sc;
2090 1.34 thorpej {
2091 1.34 thorpej
2092 1.34 thorpej if (sc->enabled != 0 && sc->disable != NULL) {
2093 1.34 thorpej (*sc->disable)(sc);
2094 1.34 thorpej sc->enabled = 0;
2095 1.34 thorpej }
2096 1.50 thorpej }
2097 1.50 thorpej
2098 1.50 thorpej int
2099 1.50 thorpej ep_activate(self, act)
2100 1.50 thorpej struct device *self;
2101 1.50 thorpej enum devact act;
2102 1.50 thorpej {
2103 1.50 thorpej struct ep_softc *sc = (struct ep_softc *)self;
2104 1.50 thorpej int rv = 0, s;
2105 1.50 thorpej
2106 1.50 thorpej s = splnet();
2107 1.50 thorpej switch (act) {
2108 1.50 thorpej case DVACT_ACTIVATE:
2109 1.50 thorpej rv = EOPNOTSUPP;
2110 1.50 thorpej break;
2111 1.50 thorpej
2112 1.50 thorpej case DVACT_DEACTIVATE:
2113 1.50 thorpej #ifdef notyet
2114 1.50 thorpej /* First, kill off the interface. */
2115 1.50 thorpej if_detach(sc->sc_ethercom.ec_if);
2116 1.50 thorpej #endif
2117 1.50 thorpej
2118 1.50 thorpej /* Now disable the interface. */
2119 1.50 thorpej epdisable(sc);
2120 1.50 thorpej break;
2121 1.50 thorpej }
2122 1.50 thorpej splx(s);
2123 1.50 thorpej return (rv);
2124 1.41 thorpej }
2125 1.41 thorpej
2126 1.41 thorpej void
2127 1.41 thorpej ep_mii_setbit(sc, bit)
2128 1.41 thorpej struct ep_softc *sc;
2129 1.41 thorpej u_int16_t bit;
2130 1.41 thorpej {
2131 1.41 thorpej u_int16_t val;
2132 1.41 thorpej
2133 1.41 thorpej /* We assume we're already in Window 4 */
2134 1.47 fvdl val = bus_space_read_2(sc->sc_iot, sc->sc_ioh, ELINK_W4_BOOM_PHYSMGMT);
2135 1.47 fvdl bus_space_write_2(sc->sc_iot, sc->sc_ioh, ELINK_W4_BOOM_PHYSMGMT,
2136 1.41 thorpej val | bit);
2137 1.41 thorpej }
2138 1.41 thorpej
2139 1.41 thorpej void
2140 1.41 thorpej ep_mii_clrbit(sc, bit)
2141 1.41 thorpej struct ep_softc *sc;
2142 1.41 thorpej u_int16_t bit;
2143 1.41 thorpej {
2144 1.41 thorpej u_int16_t val;
2145 1.41 thorpej
2146 1.41 thorpej /* We assume we're already in Window 4 */
2147 1.47 fvdl val = bus_space_read_2(sc->sc_iot, sc->sc_ioh, ELINK_W4_BOOM_PHYSMGMT);
2148 1.47 fvdl bus_space_write_2(sc->sc_iot, sc->sc_ioh, ELINK_W4_BOOM_PHYSMGMT,
2149 1.41 thorpej val & ~bit);
2150 1.41 thorpej }
2151 1.41 thorpej
2152 1.41 thorpej u_int16_t
2153 1.41 thorpej ep_mii_readbit(sc, bit)
2154 1.41 thorpej struct ep_softc *sc;
2155 1.41 thorpej u_int16_t bit;
2156 1.41 thorpej {
2157 1.41 thorpej
2158 1.41 thorpej /* We assume we're already in Window 4 */
2159 1.47 fvdl return (bus_space_read_2(sc->sc_iot, sc->sc_ioh, ELINK_W4_BOOM_PHYSMGMT) &
2160 1.41 thorpej bit);
2161 1.41 thorpej }
2162 1.41 thorpej
2163 1.41 thorpej void
2164 1.41 thorpej ep_mii_sync(sc)
2165 1.41 thorpej struct ep_softc *sc;
2166 1.41 thorpej {
2167 1.41 thorpej int i;
2168 1.41 thorpej
2169 1.41 thorpej /* We assume we're already in Window 4 */
2170 1.41 thorpej ep_mii_clrbit(sc, PHYSMGMT_DIR);
2171 1.41 thorpej for (i = 0; i < 32; i++) {
2172 1.41 thorpej ep_mii_clrbit(sc, PHYSMGMT_CLK);
2173 1.41 thorpej ep_mii_setbit(sc, PHYSMGMT_CLK);
2174 1.41 thorpej }
2175 1.41 thorpej }
2176 1.41 thorpej
2177 1.41 thorpej void
2178 1.41 thorpej ep_mii_sendbits(sc, data, nbits)
2179 1.41 thorpej struct ep_softc *sc;
2180 1.41 thorpej u_int32_t data;
2181 1.41 thorpej int nbits;
2182 1.41 thorpej {
2183 1.41 thorpej int i;
2184 1.41 thorpej
2185 1.41 thorpej /* We assume we're already in Window 4 */
2186 1.41 thorpej ep_mii_setbit(sc, PHYSMGMT_DIR);
2187 1.41 thorpej for (i = 1 << (nbits - 1); i; i = i >> 1) {
2188 1.41 thorpej ep_mii_clrbit(sc, PHYSMGMT_CLK);
2189 1.41 thorpej ep_mii_readbit(sc, PHYSMGMT_CLK);
2190 1.41 thorpej if (data & i)
2191 1.41 thorpej ep_mii_setbit(sc, PHYSMGMT_DATA);
2192 1.41 thorpej else
2193 1.41 thorpej ep_mii_clrbit(sc, PHYSMGMT_DATA);
2194 1.41 thorpej ep_mii_setbit(sc, PHYSMGMT_CLK);
2195 1.41 thorpej ep_mii_readbit(sc, PHYSMGMT_CLK);
2196 1.41 thorpej }
2197 1.41 thorpej }
2198 1.41 thorpej
2199 1.41 thorpej int
2200 1.41 thorpej ep_mii_readreg(self, phy, reg)
2201 1.41 thorpej struct device *self;
2202 1.41 thorpej int phy, reg;
2203 1.41 thorpej {
2204 1.41 thorpej struct ep_softc *sc = (struct ep_softc *)self;
2205 1.41 thorpej int val = 0, i, err;
2206 1.41 thorpej
2207 1.41 thorpej /*
2208 1.41 thorpej * Read the PHY register by manually driving the MII control lines.
2209 1.41 thorpej */
2210 1.41 thorpej
2211 1.41 thorpej GO_WINDOW(4);
2212 1.41 thorpej
2213 1.47 fvdl bus_space_write_2(sc->sc_iot, sc->sc_ioh, ELINK_W4_BOOM_PHYSMGMT, 0);
2214 1.41 thorpej
2215 1.41 thorpej ep_mii_sync(sc);
2216 1.41 thorpej ep_mii_sendbits(sc, MII_COMMAND_START, 2);
2217 1.41 thorpej ep_mii_sendbits(sc, MII_COMMAND_READ, 2);
2218 1.41 thorpej ep_mii_sendbits(sc, phy, 5);
2219 1.41 thorpej ep_mii_sendbits(sc, reg, 5);
2220 1.41 thorpej
2221 1.41 thorpej ep_mii_clrbit(sc, PHYSMGMT_DIR);
2222 1.41 thorpej ep_mii_clrbit(sc, PHYSMGMT_CLK);
2223 1.41 thorpej ep_mii_setbit(sc, PHYSMGMT_CLK);
2224 1.41 thorpej ep_mii_clrbit(sc, PHYSMGMT_CLK);
2225 1.41 thorpej
2226 1.41 thorpej err = ep_mii_readbit(sc, PHYSMGMT_DATA);
2227 1.41 thorpej ep_mii_setbit(sc, PHYSMGMT_CLK);
2228 1.41 thorpej
2229 1.41 thorpej /* Even if an error occurs, must still clock out the cycle. */
2230 1.41 thorpej for (i = 0; i < 16; i++) {
2231 1.41 thorpej val <<= 1;
2232 1.41 thorpej ep_mii_clrbit(sc, PHYSMGMT_CLK);
2233 1.41 thorpej if (err == 0 && ep_mii_readbit(sc, PHYSMGMT_DATA))
2234 1.41 thorpej val |= 1;
2235 1.41 thorpej ep_mii_setbit(sc, PHYSMGMT_CLK);
2236 1.41 thorpej }
2237 1.41 thorpej ep_mii_clrbit(sc, PHYSMGMT_CLK);
2238 1.41 thorpej ep_mii_setbit(sc, PHYSMGMT_CLK);
2239 1.41 thorpej
2240 1.41 thorpej GO_WINDOW(1); /* back to operating window */
2241 1.41 thorpej
2242 1.41 thorpej return (err ? 0 : val);
2243 1.41 thorpej }
2244 1.41 thorpej
2245 1.41 thorpej void
2246 1.41 thorpej ep_mii_writereg(self, phy, reg, val)
2247 1.41 thorpej struct device *self;
2248 1.41 thorpej int phy, reg, val;
2249 1.41 thorpej {
2250 1.41 thorpej struct ep_softc *sc = (struct ep_softc *)self;
2251 1.41 thorpej
2252 1.41 thorpej /*
2253 1.41 thorpej * Write the PHY register by manually driving the MII control lines.
2254 1.41 thorpej */
2255 1.41 thorpej
2256 1.41 thorpej GO_WINDOW(4);
2257 1.41 thorpej
2258 1.41 thorpej ep_mii_sync(sc);
2259 1.41 thorpej ep_mii_sendbits(sc, MII_COMMAND_START, 2);
2260 1.41 thorpej ep_mii_sendbits(sc, MII_COMMAND_WRITE, 2);
2261 1.41 thorpej ep_mii_sendbits(sc, phy, 5);
2262 1.41 thorpej ep_mii_sendbits(sc, reg, 5);
2263 1.41 thorpej ep_mii_sendbits(sc, MII_COMMAND_ACK, 2);
2264 1.41 thorpej ep_mii_sendbits(sc, val, 16);
2265 1.41 thorpej
2266 1.41 thorpej ep_mii_clrbit(sc, PHYSMGMT_CLK);
2267 1.41 thorpej ep_mii_setbit(sc, PHYSMGMT_CLK);
2268 1.41 thorpej
2269 1.41 thorpej GO_WINDOW(1); /* back to operating window */
2270 1.41 thorpej }
2271 1.41 thorpej
2272 1.41 thorpej void
2273 1.41 thorpej ep_statchg(self)
2274 1.41 thorpej struct device *self;
2275 1.41 thorpej {
2276 1.45 thorpej struct ep_softc *sc = (struct ep_softc *)self;
2277 1.45 thorpej bus_space_tag_t iot = sc->sc_iot;
2278 1.45 thorpej bus_space_handle_t ioh = sc->sc_ioh;
2279 1.45 thorpej int mctl;
2280 1.41 thorpej
2281 1.41 thorpej /* XXX Update ifp->if_baudrate */
2282 1.45 thorpej
2283 1.45 thorpej GO_WINDOW(3);
2284 1.47 fvdl mctl = bus_space_read_2(iot, ioh, ELINK_W3_MAC_CONTROL);
2285 1.45 thorpej if (sc->sc_mii.mii_media_active & IFM_FDX)
2286 1.45 thorpej mctl |= MAC_CONTROL_FDX;
2287 1.45 thorpej else
2288 1.45 thorpej mctl &= ~MAC_CONTROL_FDX;
2289 1.47 fvdl bus_space_write_2(iot, ioh, ELINK_W3_MAC_CONTROL, mctl);
2290 1.45 thorpej GO_WINDOW(1); /* back to operating window */
2291 1.1 thorpej }
2292