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