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