elinkxl.c revision 1.51 1 /* $NetBSD: elinkxl.c,v 1.51 2001/06/12 22:32:50 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 Frank van der Linden.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include "bpfilter.h"
40 #include "rnd.h"
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/callout.h>
45 #include <sys/kernel.h>
46 #include <sys/mbuf.h>
47 #include <sys/socket.h>
48 #include <sys/ioctl.h>
49 #include <sys/errno.h>
50 #include <sys/syslog.h>
51 #include <sys/select.h>
52 #include <sys/device.h>
53 #if NRND > 0
54 #include <sys/rnd.h>
55 #endif
56
57 #include <uvm/uvm_extern.h>
58
59 #include <net/if.h>
60 #include <net/if_dl.h>
61 #include <net/if_ether.h>
62 #include <net/if_media.h>
63
64 #if NBPFILTER > 0
65 #include <net/bpf.h>
66 #include <net/bpfdesc.h>
67 #endif
68
69 #include <machine/cpu.h>
70 #include <machine/bus.h>
71 #include <machine/intr.h>
72 #include <machine/endian.h>
73
74 #include <dev/mii/miivar.h>
75 #include <dev/mii/mii.h>
76 #include <dev/mii/mii_bitbang.h>
77
78 #include <dev/ic/elink3reg.h>
79 /* #include <dev/ic/elink3var.h> */
80 #include <dev/ic/elinkxlreg.h>
81 #include <dev/ic/elinkxlvar.h>
82
83 #ifdef DEBUG
84 int exdebug = 0;
85 #endif
86
87 /* ifmedia callbacks */
88 int ex_media_chg __P((struct ifnet *ifp));
89 void ex_media_stat __P((struct ifnet *ifp, struct ifmediareq *req));
90
91 void ex_probe_media __P((struct ex_softc *));
92 void ex_set_filter __P((struct ex_softc *));
93 void ex_set_media __P((struct ex_softc *));
94 struct mbuf *ex_get __P((struct ex_softc *, int));
95 u_int16_t ex_read_eeprom __P((struct ex_softc *, int));
96 int ex_init __P((struct ifnet *));
97 void ex_read __P((struct ex_softc *));
98 void ex_reset __P((struct ex_softc *));
99 void ex_set_mc __P((struct ex_softc *));
100 void ex_getstats __P((struct ex_softc *));
101 void ex_printstats __P((struct ex_softc *));
102 void ex_tick __P((void *));
103
104 int ex_enable __P((struct ex_softc *));
105 void ex_disable __P((struct ex_softc *));
106 void ex_power __P((int, void *));
107
108 static int ex_eeprom_busy __P((struct ex_softc *));
109 static int ex_add_rxbuf __P((struct ex_softc *, struct ex_rxdesc *));
110 static void ex_init_txdescs __P((struct ex_softc *));
111
112 static void ex_shutdown __P((void *));
113 static void ex_start __P((struct ifnet *));
114 static void ex_txstat __P((struct ex_softc *));
115
116 int ex_mii_readreg __P((struct device *, int, int));
117 void ex_mii_writereg __P((struct device *, int, int, int));
118 void ex_mii_statchg __P((struct device *));
119
120 void ex_probemedia __P((struct ex_softc *));
121
122 /*
123 * Structure to map media-present bits in boards to ifmedia codes and
124 * printable media names. Used for table-driven ifmedia initialization.
125 */
126 struct ex_media {
127 int exm_mpbit; /* media present bit */
128 const char *exm_name; /* name of medium */
129 int exm_ifmedia; /* ifmedia word for medium */
130 int exm_epmedia; /* ELINKMEDIA_* constant */
131 };
132
133 /*
134 * Media table for 3c90x chips. Note that chips with MII have no
135 * `native' media.
136 */
137 struct ex_media ex_native_media[] = {
138 { ELINK_PCI_10BASE_T, "10baseT", IFM_ETHER|IFM_10_T,
139 ELINKMEDIA_10BASE_T },
140 { ELINK_PCI_10BASE_T, "10baseT-FDX", IFM_ETHER|IFM_10_T|IFM_FDX,
141 ELINKMEDIA_10BASE_T },
142 { ELINK_PCI_AUI, "10base5", IFM_ETHER|IFM_10_5,
143 ELINKMEDIA_AUI },
144 { ELINK_PCI_BNC, "10base2", IFM_ETHER|IFM_10_2,
145 ELINKMEDIA_10BASE_2 },
146 { ELINK_PCI_100BASE_TX, "100baseTX", IFM_ETHER|IFM_100_TX,
147 ELINKMEDIA_100BASE_TX },
148 { ELINK_PCI_100BASE_TX, "100baseTX-FDX",IFM_ETHER|IFM_100_TX|IFM_FDX,
149 ELINKMEDIA_100BASE_TX },
150 { ELINK_PCI_100BASE_FX, "100baseFX", IFM_ETHER|IFM_100_FX,
151 ELINKMEDIA_100BASE_FX },
152 { ELINK_PCI_100BASE_MII,"manual", IFM_ETHER|IFM_MANUAL,
153 ELINKMEDIA_MII },
154 { ELINK_PCI_100BASE_T4, "100baseT4", IFM_ETHER|IFM_100_T4,
155 ELINKMEDIA_100BASE_T4 },
156 { 0, NULL, 0,
157 0 },
158 };
159
160 /*
161 * MII bit-bang glue.
162 */
163 u_int32_t ex_mii_bitbang_read __P((struct device *));
164 void ex_mii_bitbang_write __P((struct device *, u_int32_t));
165
166 const struct mii_bitbang_ops ex_mii_bitbang_ops = {
167 ex_mii_bitbang_read,
168 ex_mii_bitbang_write,
169 {
170 ELINK_PHY_DATA, /* MII_BIT_MDO */
171 ELINK_PHY_DATA, /* MII_BIT_MDI */
172 ELINK_PHY_CLK, /* MII_BIT_MDC */
173 ELINK_PHY_DIR, /* MII_BIT_DIR_HOST_PHY */
174 0, /* MII_BIT_DIR_PHY_HOST */
175 }
176 };
177
178 /*
179 * Back-end attach and configure.
180 */
181 void
182 ex_config(sc)
183 struct ex_softc *sc;
184 {
185 struct ifnet *ifp;
186 u_int16_t val;
187 u_int8_t macaddr[ETHER_ADDR_LEN] = {0};
188 bus_space_tag_t iot = sc->sc_iot;
189 bus_space_handle_t ioh = sc->sc_ioh;
190 int i, error, attach_stage;
191
192 callout_init(&sc->ex_mii_callout);
193
194 ex_reset(sc);
195
196 val = ex_read_eeprom(sc, EEPROM_OEM_ADDR0);
197 macaddr[0] = val >> 8;
198 macaddr[1] = val & 0xff;
199 val = ex_read_eeprom(sc, EEPROM_OEM_ADDR1);
200 macaddr[2] = val >> 8;
201 macaddr[3] = val & 0xff;
202 val = ex_read_eeprom(sc, EEPROM_OEM_ADDR2);
203 macaddr[4] = val >> 8;
204 macaddr[5] = val & 0xff;
205
206 printf("%s: MAC address %s\n", sc->sc_dev.dv_xname,
207 ether_sprintf(macaddr));
208
209 if (sc->ex_conf & (EX_CONF_INV_LED_POLARITY|EX_CONF_PHY_POWER)) {
210 GO_WINDOW(2);
211 val = bus_space_read_2(iot, ioh, ELINK_W2_RESET_OPTIONS);
212 if (sc->ex_conf & EX_CONF_INV_LED_POLARITY)
213 val |= ELINK_RESET_OPT_LEDPOLAR;
214 if (sc->ex_conf & EX_CONF_PHY_POWER)
215 val |= ELINK_RESET_OPT_PHYPOWER;
216 bus_space_write_2(iot, ioh, ELINK_W2_RESET_OPTIONS, val);
217 }
218
219 attach_stage = 0;
220
221 /*
222 * Allocate the upload descriptors, and create and load the DMA
223 * map for them.
224 */
225 if ((error = bus_dmamem_alloc(sc->sc_dmat,
226 EX_NUPD * sizeof (struct ex_upd), PAGE_SIZE, 0, &sc->sc_useg, 1,
227 &sc->sc_urseg, BUS_DMA_NOWAIT)) != 0) {
228 printf("%s: can't allocate upload descriptors, error = %d\n",
229 sc->sc_dev.dv_xname, error);
230 goto fail;
231 }
232
233 attach_stage = 1;
234
235 if ((error = bus_dmamem_map(sc->sc_dmat, &sc->sc_useg, sc->sc_urseg,
236 EX_NUPD * sizeof (struct ex_upd), (caddr_t *)&sc->sc_upd,
237 BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
238 printf("%s: can't map upload descriptors, error = %d\n",
239 sc->sc_dev.dv_xname, error);
240 goto fail;
241 }
242
243 attach_stage = 2;
244
245 if ((error = bus_dmamap_create(sc->sc_dmat,
246 EX_NUPD * sizeof (struct ex_upd), 1,
247 EX_NUPD * sizeof (struct ex_upd), 0, BUS_DMA_NOWAIT,
248 &sc->sc_upd_dmamap)) != 0) {
249 printf("%s: can't create upload desc. DMA map, error = %d\n",
250 sc->sc_dev.dv_xname, error);
251 goto fail;
252 }
253
254 attach_stage = 3;
255
256 if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_upd_dmamap,
257 sc->sc_upd, EX_NUPD * sizeof (struct ex_upd), NULL,
258 BUS_DMA_NOWAIT)) != 0) {
259 printf("%s: can't load upload desc. DMA map, error = %d\n",
260 sc->sc_dev.dv_xname, error);
261 goto fail;
262 }
263
264 attach_stage = 4;
265
266 /*
267 * Allocate the download descriptors, and create and load the DMA
268 * map for them.
269 */
270 if ((error = bus_dmamem_alloc(sc->sc_dmat,
271 EX_NDPD * sizeof (struct ex_dpd), PAGE_SIZE, 0, &sc->sc_dseg, 1,
272 &sc->sc_drseg, BUS_DMA_NOWAIT)) != 0) {
273 printf("%s: can't allocate download descriptors, error = %d\n",
274 sc->sc_dev.dv_xname, error);
275 goto fail;
276 }
277
278 attach_stage = 5;
279
280 if ((error = bus_dmamem_map(sc->sc_dmat, &sc->sc_dseg, sc->sc_drseg,
281 EX_NDPD * sizeof (struct ex_dpd), (caddr_t *)&sc->sc_dpd,
282 BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
283 printf("%s: can't map download descriptors, error = %d\n",
284 sc->sc_dev.dv_xname, error);
285 goto fail;
286 }
287 bzero(sc->sc_dpd, EX_NDPD * sizeof (struct ex_dpd));
288
289 attach_stage = 6;
290
291 if ((error = bus_dmamap_create(sc->sc_dmat,
292 EX_NDPD * sizeof (struct ex_dpd), 1,
293 EX_NDPD * sizeof (struct ex_dpd), 0, BUS_DMA_NOWAIT,
294 &sc->sc_dpd_dmamap)) != 0) {
295 printf("%s: can't create download desc. DMA map, error = %d\n",
296 sc->sc_dev.dv_xname, error);
297 goto fail;
298 }
299
300 attach_stage = 7;
301
302 if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dpd_dmamap,
303 sc->sc_dpd, EX_NDPD * sizeof (struct ex_dpd), NULL,
304 BUS_DMA_NOWAIT)) != 0) {
305 printf("%s: can't load download desc. DMA map, error = %d\n",
306 sc->sc_dev.dv_xname, error);
307 goto fail;
308 }
309
310 attach_stage = 8;
311
312
313 /*
314 * Create the transmit buffer DMA maps.
315 */
316 for (i = 0; i < EX_NDPD; i++) {
317 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
318 EX_NTFRAGS, MCLBYTES, 0, BUS_DMA_NOWAIT,
319 &sc->sc_tx_dmamaps[i])) != 0) {
320 printf("%s: can't create tx DMA map %d, error = %d\n",
321 sc->sc_dev.dv_xname, i, error);
322 goto fail;
323 }
324 }
325
326 attach_stage = 9;
327
328 /*
329 * Create the receive buffer DMA maps.
330 */
331 for (i = 0; i < EX_NUPD; i++) {
332 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
333 EX_NRFRAGS, MCLBYTES, 0, BUS_DMA_NOWAIT,
334 &sc->sc_rx_dmamaps[i])) != 0) {
335 printf("%s: can't create rx DMA map %d, error = %d\n",
336 sc->sc_dev.dv_xname, i, error);
337 goto fail;
338 }
339 }
340
341 attach_stage = 10;
342
343 /*
344 * Create ring of upload descriptors, only once. The DMA engine
345 * will loop over this when receiving packets, stalling if it
346 * hits an UPD with a finished receive.
347 */
348 for (i = 0; i < EX_NUPD; i++) {
349 sc->sc_rxdescs[i].rx_dmamap = sc->sc_rx_dmamaps[i];
350 sc->sc_rxdescs[i].rx_upd = &sc->sc_upd[i];
351 sc->sc_upd[i].upd_frags[0].fr_len =
352 htole32((MCLBYTES - 2) | EX_FR_LAST);
353 if (ex_add_rxbuf(sc, &sc->sc_rxdescs[i]) != 0) {
354 printf("%s: can't allocate or map rx buffers\n",
355 sc->sc_dev.dv_xname);
356 goto fail;
357 }
358 }
359
360 bus_dmamap_sync(sc->sc_dmat, sc->sc_upd_dmamap, 0,
361 EX_NUPD * sizeof (struct ex_upd),
362 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
363
364 ex_init_txdescs(sc);
365
366 attach_stage = 11;
367
368
369 GO_WINDOW(3);
370 val = bus_space_read_2(iot, ioh, ELINK_W3_RESET_OPTIONS);
371 if (val & ELINK_MEDIACAP_MII)
372 sc->ex_conf |= EX_CONF_MII;
373
374 ifp = &sc->sc_ethercom.ec_if;
375
376 /*
377 * Initialize our media structures and MII info. We'll
378 * probe the MII if we discover that we have one.
379 */
380 sc->ex_mii.mii_ifp = ifp;
381 sc->ex_mii.mii_readreg = ex_mii_readreg;
382 sc->ex_mii.mii_writereg = ex_mii_writereg;
383 sc->ex_mii.mii_statchg = ex_mii_statchg;
384 ifmedia_init(&sc->ex_mii.mii_media, 0, ex_media_chg,
385 ex_media_stat);
386
387 if (sc->ex_conf & EX_CONF_MII) {
388 /*
389 * Find PHY, extract media information from it.
390 * First, select the right transceiver.
391 */
392 u_int32_t icfg;
393
394 GO_WINDOW(3);
395 icfg = bus_space_read_4(iot, ioh, ELINK_W3_INTERNAL_CONFIG);
396 icfg &= ~(CONFIG_XCVR_SEL << 16);
397 if (val & (ELINK_MEDIACAP_MII | ELINK_MEDIACAP_100BASET4))
398 icfg |= ELINKMEDIA_MII << (CONFIG_XCVR_SEL_SHIFT + 16);
399 if (val & ELINK_MEDIACAP_100BASETX)
400 icfg |= ELINKMEDIA_AUTO << (CONFIG_XCVR_SEL_SHIFT + 16);
401 if (val & ELINK_MEDIACAP_100BASEFX)
402 icfg |= ELINKMEDIA_100BASE_FX
403 << (CONFIG_XCVR_SEL_SHIFT + 16);
404 bus_space_write_4(iot, ioh, ELINK_W3_INTERNAL_CONFIG, icfg);
405
406 mii_attach(&sc->sc_dev, &sc->ex_mii, 0xffffffff,
407 MII_PHY_ANY, MII_OFFSET_ANY, 0);
408 if (LIST_FIRST(&sc->ex_mii.mii_phys) == NULL) {
409 ifmedia_add(&sc->ex_mii.mii_media, IFM_ETHER|IFM_NONE,
410 0, NULL);
411 ifmedia_set(&sc->ex_mii.mii_media, IFM_ETHER|IFM_NONE);
412 } else {
413 ifmedia_set(&sc->ex_mii.mii_media, IFM_ETHER|IFM_AUTO);
414 }
415 } else
416 ex_probemedia(sc);
417
418 bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
419 ifp->if_softc = sc;
420 ifp->if_start = ex_start;
421 ifp->if_ioctl = ex_ioctl;
422 ifp->if_watchdog = ex_watchdog;
423 ifp->if_init = ex_init;
424 ifp->if_stop = ex_stop;
425 ifp->if_flags =
426 IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
427 IFQ_SET_READY(&ifp->if_snd);
428
429 /*
430 * We can support 802.1Q VLAN-sized frames.
431 */
432 sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
433
434 /*
435 * The 3c90xB has hardware IPv4/TCPv4/UDPv4 checksum support.
436 */
437 if (sc->ex_conf & EX_CONF_90XB)
438 sc->sc_ethercom.ec_if.if_capabilities |= IFCAP_CSUM_IPv4 |
439 IFCAP_CSUM_TCPv4 | IFCAP_CSUM_UDPv4;
440
441 if_attach(ifp);
442 ether_ifattach(ifp, macaddr);
443
444 GO_WINDOW(1);
445
446 sc->tx_start_thresh = 20;
447 sc->tx_succ_ok = 0;
448
449 /* TODO: set queues to 0 */
450
451 #if NRND > 0
452 rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
453 RND_TYPE_NET, 0);
454 #endif
455
456 /* Establish callback to reset card when we reboot. */
457 sc->sc_sdhook = shutdownhook_establish(ex_shutdown, sc);
458 if (sc->sc_sdhook == NULL)
459 printf("%s: WARNING: unable to establish shutdown hook\n",
460 sc->sc_dev.dv_xname);
461
462 /* Add a suspend hook to make sure we come back up after a resume. */
463 sc->sc_powerhook = powerhook_establish(ex_power, sc);
464 if (sc->sc_powerhook == NULL)
465 printf("%s: WARNING: unable to establish power hook\n",
466 sc->sc_dev.dv_xname);
467
468 /* The attach is successful. */
469 sc->ex_flags |= EX_FLAGS_ATTACHED;
470 return;
471
472 fail:
473 /*
474 * Free any resources we've allocated during the failed attach
475 * attempt. Do this in reverse order and fall though.
476 */
477 switch (attach_stage) {
478 case 11:
479 {
480 struct ex_rxdesc *rxd;
481
482 for (i = 0; i < EX_NUPD; i++) {
483 rxd = &sc->sc_rxdescs[i];
484 if (rxd->rx_mbhead != NULL) {
485 bus_dmamap_unload(sc->sc_dmat, rxd->rx_dmamap);
486 m_freem(rxd->rx_mbhead);
487 }
488 }
489 }
490 /* FALLTHROUGH */
491
492 case 10:
493 for (i = 0; i < EX_NUPD; i++)
494 bus_dmamap_destroy(sc->sc_dmat, sc->sc_rx_dmamaps[i]);
495 /* FALLTHROUGH */
496
497 case 9:
498 for (i = 0; i < EX_NDPD; i++)
499 bus_dmamap_destroy(sc->sc_dmat, sc->sc_tx_dmamaps[i]);
500 /* FALLTHROUGH */
501 case 8:
502 bus_dmamap_unload(sc->sc_dmat, sc->sc_dpd_dmamap);
503 /* FALLTHROUGH */
504
505 case 7:
506 bus_dmamap_destroy(sc->sc_dmat, sc->sc_dpd_dmamap);
507 /* FALLTHROUGH */
508
509 case 6:
510 bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_dpd,
511 EX_NDPD * sizeof (struct ex_dpd));
512 /* FALLTHROUGH */
513
514 case 5:
515 bus_dmamem_free(sc->sc_dmat, &sc->sc_dseg, sc->sc_drseg);
516 break;
517
518 case 4:
519 bus_dmamap_unload(sc->sc_dmat, sc->sc_upd_dmamap);
520 /* FALLTHROUGH */
521
522 case 3:
523 bus_dmamap_destroy(sc->sc_dmat, sc->sc_upd_dmamap);
524 /* FALLTHROUGH */
525
526 case 2:
527 bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_upd,
528 EX_NUPD * sizeof (struct ex_upd));
529 /* FALLTHROUGH */
530
531 case 1:
532 bus_dmamem_free(sc->sc_dmat, &sc->sc_useg, sc->sc_urseg);
533 break;
534 }
535
536 }
537
538 /*
539 * Find the media present on non-MII chips.
540 */
541 void
542 ex_probemedia(sc)
543 struct ex_softc *sc;
544 {
545 bus_space_tag_t iot = sc->sc_iot;
546 bus_space_handle_t ioh = sc->sc_ioh;
547 struct ifmedia *ifm = &sc->ex_mii.mii_media;
548 struct ex_media *exm;
549 u_int16_t config1, reset_options, default_media;
550 int defmedia = 0;
551 const char *sep = "", *defmedianame = NULL;
552
553 GO_WINDOW(3);
554 config1 = bus_space_read_2(iot, ioh, ELINK_W3_INTERNAL_CONFIG + 2);
555 reset_options = bus_space_read_1(iot, ioh, ELINK_W3_RESET_OPTIONS);
556 GO_WINDOW(0);
557
558 default_media = (config1 & CONFIG_MEDIAMASK) >> CONFIG_MEDIAMASK_SHIFT;
559
560 printf("%s: ", sc->sc_dev.dv_xname);
561
562 /* Sanity check that there are any media! */
563 if ((reset_options & ELINK_PCI_MEDIAMASK) == 0) {
564 printf("no media present!\n");
565 ifmedia_add(ifm, IFM_ETHER|IFM_NONE, 0, NULL);
566 ifmedia_set(ifm, IFM_ETHER|IFM_NONE);
567 return;
568 }
569
570 #define PRINT(s) printf("%s%s", sep, s); sep = ", "
571
572 for (exm = ex_native_media; exm->exm_name != NULL; exm++) {
573 if (reset_options & exm->exm_mpbit) {
574 /*
575 * Default media is a little complicated. We
576 * support full-duplex which uses the same
577 * reset options bit.
578 *
579 * XXX Check EEPROM for default to FDX?
580 */
581 if (exm->exm_epmedia == default_media) {
582 if ((exm->exm_ifmedia & IFM_FDX) == 0) {
583 defmedia = exm->exm_ifmedia;
584 defmedianame = exm->exm_name;
585 }
586 } else if (defmedia == 0) {
587 defmedia = exm->exm_ifmedia;
588 defmedianame = exm->exm_name;
589 }
590 ifmedia_add(ifm, exm->exm_ifmedia, exm->exm_epmedia,
591 NULL);
592 PRINT(exm->exm_name);
593 }
594 }
595
596 #undef PRINT
597
598 #ifdef DIAGNOSTIC
599 if (defmedia == 0)
600 panic("ex_probemedia: impossible");
601 #endif
602
603 printf(", default %s\n", defmedianame);
604 ifmedia_set(ifm, defmedia);
605 }
606
607 /*
608 * Bring device up.
609 */
610 int
611 ex_init(ifp)
612 struct ifnet *ifp;
613 {
614 struct ex_softc *sc = ifp->if_softc;
615 bus_space_tag_t iot = sc->sc_iot;
616 bus_space_handle_t ioh = sc->sc_ioh;
617 int i;
618 int error = 0;
619
620 if ((error = ex_enable(sc)) != 0)
621 goto out;
622
623 ex_waitcmd(sc);
624 ex_stop(ifp, 0);
625
626 /*
627 * Set the station address and clear the station mask. The latter
628 * is needed for 90x cards, 0 is the default for 90xB cards.
629 */
630 GO_WINDOW(2);
631 for (i = 0; i < ETHER_ADDR_LEN; i++) {
632 bus_space_write_1(iot, ioh, ELINK_W2_ADDR_0 + i,
633 LLADDR(ifp->if_sadl)[i]);
634 bus_space_write_1(iot, ioh, ELINK_W2_RECVMASK_0 + i, 0);
635 }
636
637 GO_WINDOW(3);
638
639 bus_space_write_2(iot, ioh, ELINK_COMMAND, RX_RESET);
640 ex_waitcmd(sc);
641 bus_space_write_2(iot, ioh, ELINK_COMMAND, TX_RESET);
642 ex_waitcmd(sc);
643
644 /*
645 * Disable reclaim threshold for 90xB, set free threshold to
646 * 6 * 256 = 1536 for 90x.
647 */
648 if (sc->ex_conf & EX_CONF_90XB)
649 bus_space_write_2(iot, ioh, ELINK_COMMAND,
650 ELINK_TXRECLTHRESH | 255);
651 else
652 bus_space_write_1(iot, ioh, ELINK_TXFREETHRESH, 6);
653
654 bus_space_write_2(iot, ioh, ELINK_COMMAND,
655 SET_RX_EARLY_THRESH | ELINK_THRESH_DISABLE);
656
657 bus_space_write_4(iot, ioh, ELINK_DMACTRL,
658 bus_space_read_4(iot, ioh, ELINK_DMACTRL) | ELINK_DMAC_UPRXEAREN);
659
660 bus_space_write_2(iot, ioh, ELINK_COMMAND, SET_RD_0_MASK | S_MASK);
661 bus_space_write_2(iot, ioh, ELINK_COMMAND, SET_INTR_MASK | S_MASK);
662
663 bus_space_write_2(iot, ioh, ELINK_COMMAND, ACK_INTR | 0xff);
664 if (sc->intr_ack)
665 (* sc->intr_ack)(sc);
666 ex_set_media(sc);
667 ex_set_mc(sc);
668
669
670 bus_space_write_2(iot, ioh, ELINK_COMMAND, STATS_ENABLE);
671 bus_space_write_2(iot, ioh, ELINK_COMMAND, TX_ENABLE);
672 bus_space_write_4(iot, ioh, ELINK_UPLISTPTR, sc->sc_upddma);
673 bus_space_write_2(iot, ioh, ELINK_COMMAND, RX_ENABLE);
674 bus_space_write_2(iot, ioh, ELINK_COMMAND, ELINK_UPUNSTALL);
675
676 if (sc->ex_conf & (EX_CONF_PHY_POWER | EX_CONF_INV_LED_POLARITY)) {
677 u_int16_t cbcard_config;
678
679 GO_WINDOW(2);
680 cbcard_config = bus_space_read_2(sc->sc_iot, sc->sc_ioh, 0x0c);
681 if (sc->ex_conf & EX_CONF_PHY_POWER) {
682 cbcard_config |= 0x4000; /* turn on PHY power */
683 }
684 if (sc->ex_conf & EX_CONF_INV_LED_POLARITY) {
685 cbcard_config |= 0x0010; /* invert LED polarity */
686 }
687 bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x0c, cbcard_config);
688
689 GO_WINDOW(3);
690 }
691
692 ifp->if_flags |= IFF_RUNNING;
693 ifp->if_flags &= ~IFF_OACTIVE;
694 ex_start(ifp);
695
696 GO_WINDOW(1);
697
698 callout_reset(&sc->ex_mii_callout, hz, ex_tick, sc);
699
700 out:
701 if (error) {
702 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
703 ifp->if_timer = 0;
704 printf("%s: interface not running\n", sc->sc_dev.dv_xname);
705 }
706 return (error);
707 }
708
709 #define ex_mchash(addr) (ether_crc32_be((addr), ETHER_ADDR_LEN) & 0xff)
710
711 /*
712 * Set multicast receive filter. Also take care of promiscuous mode
713 * here (XXX).
714 */
715 void
716 ex_set_mc(sc)
717 struct ex_softc *sc;
718 {
719 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
720 struct ethercom *ec = &sc->sc_ethercom;
721 struct ether_multi *enm;
722 struct ether_multistep estep;
723 int i;
724 u_int16_t mask = FIL_INDIVIDUAL | FIL_BRDCST;
725
726 if (ifp->if_flags & IFF_PROMISC)
727 mask |= FIL_PROMISC;
728
729 if (!(ifp->if_flags & IFF_MULTICAST))
730 goto out;
731
732 if (!(sc->ex_conf & EX_CONF_90XB) || ifp->if_flags & IFF_ALLMULTI) {
733 mask |= (ifp->if_flags & IFF_MULTICAST) ? FIL_MULTICAST : 0;
734 } else {
735 ETHER_FIRST_MULTI(estep, ec, enm);
736 while (enm != NULL) {
737 if (bcmp(enm->enm_addrlo, enm->enm_addrhi,
738 ETHER_ADDR_LEN) != 0)
739 goto out;
740 i = ex_mchash(enm->enm_addrlo);
741 bus_space_write_2(sc->sc_iot, sc->sc_ioh,
742 ELINK_COMMAND, ELINK_SETHASHFILBIT | i);
743 ETHER_NEXT_MULTI(estep, enm);
744 }
745 mask |= FIL_MULTIHASH;
746 }
747 out:
748 bus_space_write_2(sc->sc_iot, sc->sc_ioh, ELINK_COMMAND,
749 SET_RX_FILTER | mask);
750 }
751
752
753 static void
754 ex_txstat(sc)
755 struct ex_softc *sc;
756 {
757 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
758 bus_space_tag_t iot = sc->sc_iot;
759 bus_space_handle_t ioh = sc->sc_ioh;
760 int i;
761
762 /*
763 * We need to read+write TX_STATUS until we get a 0 status
764 * in order to turn off the interrupt flag.
765 */
766 while ((i = bus_space_read_1(iot, ioh, ELINK_TXSTATUS)) & TXS_COMPLETE) {
767 bus_space_write_1(iot, ioh, ELINK_TXSTATUS, 0x0);
768
769 if (i & TXS_JABBER) {
770 ++sc->sc_ethercom.ec_if.if_oerrors;
771 if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
772 printf("%s: jabber (%x)\n",
773 sc->sc_dev.dv_xname, i);
774 ex_init(ifp);
775 /* TODO: be more subtle here */
776 } else if (i & TXS_UNDERRUN) {
777 ++sc->sc_ethercom.ec_if.if_oerrors;
778 if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
779 printf("%s: fifo underrun (%x) @%d\n",
780 sc->sc_dev.dv_xname, i,
781 sc->tx_start_thresh);
782 if (sc->tx_succ_ok < 100)
783 sc->tx_start_thresh = min(ETHER_MAX_LEN,
784 sc->tx_start_thresh + 20);
785 sc->tx_succ_ok = 0;
786 ex_init(ifp);
787 /* TODO: be more subtle here */
788 } else if (i & TXS_MAX_COLLISION) {
789 ++sc->sc_ethercom.ec_if.if_collisions;
790 bus_space_write_2(iot, ioh, ELINK_COMMAND, TX_ENABLE);
791 sc->sc_ethercom.ec_if.if_flags &= ~IFF_OACTIVE;
792 } else
793 sc->tx_succ_ok = (sc->tx_succ_ok+1) & 127;
794 }
795 }
796
797 int
798 ex_media_chg(ifp)
799 struct ifnet *ifp;
800 {
801
802 if (ifp->if_flags & IFF_UP)
803 ex_init(ifp);
804 return 0;
805 }
806
807 void
808 ex_set_media(sc)
809 struct ex_softc *sc;
810 {
811 bus_space_tag_t iot = sc->sc_iot;
812 bus_space_handle_t ioh = sc->sc_ioh;
813 u_int32_t configreg;
814
815 if (((sc->ex_conf & EX_CONF_MII) &&
816 (sc->ex_mii.mii_media_active & IFM_FDX))
817 || (!(sc->ex_conf & EX_CONF_MII) &&
818 (sc->ex_mii.mii_media.ifm_media & IFM_FDX))) {
819 bus_space_write_2(iot, ioh, ELINK_W3_MAC_CONTROL,
820 MAC_CONTROL_FDX);
821 } else {
822 bus_space_write_2(iot, ioh, ELINK_W3_MAC_CONTROL, 0);
823 }
824
825 /*
826 * If the device has MII, select it, and then tell the
827 * PHY which media to use.
828 */
829 if (sc->ex_conf & EX_CONF_MII) {
830 GO_WINDOW(3);
831
832 configreg = bus_space_read_4(iot, ioh, ELINK_W3_INTERNAL_CONFIG);
833
834 configreg &= ~(CONFIG_MEDIAMASK << 16);
835 configreg |= (ELINKMEDIA_MII << (CONFIG_MEDIAMASK_SHIFT + 16));
836
837 bus_space_write_4(iot, ioh, ELINK_W3_INTERNAL_CONFIG, configreg);
838 mii_mediachg(&sc->ex_mii);
839 return;
840 }
841
842 GO_WINDOW(4);
843 bus_space_write_2(iot, ioh, ELINK_W4_MEDIA_TYPE, 0);
844 bus_space_write_2(iot, ioh, ELINK_COMMAND, STOP_TRANSCEIVER);
845 delay(800);
846
847 /*
848 * Now turn on the selected media/transceiver.
849 */
850 switch (IFM_SUBTYPE(sc->ex_mii.mii_media.ifm_cur->ifm_media)) {
851 case IFM_10_T:
852 bus_space_write_2(iot, ioh, ELINK_W4_MEDIA_TYPE,
853 JABBER_GUARD_ENABLE|LINKBEAT_ENABLE);
854 break;
855
856 case IFM_10_2:
857 bus_space_write_2(iot, ioh, ELINK_COMMAND, START_TRANSCEIVER);
858 DELAY(800);
859 break;
860
861 case IFM_100_TX:
862 case IFM_100_FX:
863 bus_space_write_2(iot, ioh, ELINK_W4_MEDIA_TYPE,
864 LINKBEAT_ENABLE);
865 DELAY(800);
866 break;
867
868 case IFM_10_5:
869 bus_space_write_2(iot, ioh, ELINK_W4_MEDIA_TYPE,
870 SQE_ENABLE);
871 DELAY(800);
872 break;
873
874 case IFM_MANUAL:
875 break;
876
877 case IFM_NONE:
878 return;
879
880 default:
881 panic("ex_set_media: impossible");
882 }
883
884 GO_WINDOW(3);
885 configreg = bus_space_read_4(iot, ioh, ELINK_W3_INTERNAL_CONFIG);
886
887 configreg &= ~(CONFIG_MEDIAMASK << 16);
888 configreg |= (sc->ex_mii.mii_media.ifm_cur->ifm_data <<
889 (CONFIG_MEDIAMASK_SHIFT + 16));
890
891 bus_space_write_4(iot, ioh, ELINK_W3_INTERNAL_CONFIG, configreg);
892 }
893
894 /*
895 * Get currently-selected media from card.
896 * (if_media callback, may be called before interface is brought up).
897 */
898 void
899 ex_media_stat(ifp, req)
900 struct ifnet *ifp;
901 struct ifmediareq *req;
902 {
903 struct ex_softc *sc = ifp->if_softc;
904
905 if (sc->ex_conf & EX_CONF_MII) {
906 mii_pollstat(&sc->ex_mii);
907 req->ifm_status = sc->ex_mii.mii_media_status;
908 req->ifm_active = sc->ex_mii.mii_media_active;
909 } else {
910 GO_WINDOW(4);
911 req->ifm_status = IFM_AVALID;
912 req->ifm_active = sc->ex_mii.mii_media.ifm_cur->ifm_media;
913 if (bus_space_read_2(sc->sc_iot, sc->sc_ioh,
914 ELINK_W4_MEDIA_TYPE) & LINKBEAT_DETECT)
915 req->ifm_status |= IFM_ACTIVE;
916 GO_WINDOW(1);
917 }
918 }
919
920
921
922 /*
923 * Start outputting on the interface.
924 */
925 static void
926 ex_start(ifp)
927 struct ifnet *ifp;
928 {
929 struct ex_softc *sc = ifp->if_softc;
930 bus_space_tag_t iot = sc->sc_iot;
931 bus_space_handle_t ioh = sc->sc_ioh;
932 volatile struct ex_fraghdr *fr = NULL;
933 volatile struct ex_dpd *dpd = NULL, *prevdpd = NULL;
934 struct ex_txdesc *txp;
935 struct mbuf *mb_head;
936 bus_dmamap_t dmamap;
937 int offset, totlen, segment, error;
938 u_int32_t csum_flags;
939
940 if (sc->tx_head || sc->tx_free == NULL)
941 return;
942
943 txp = NULL;
944
945 /*
946 * We're finished if there is nothing more to add to the list or if
947 * we're all filled up with buffers to transmit.
948 */
949 while (sc->tx_free != NULL) {
950 /*
951 * Grab a packet to transmit.
952 */
953 IFQ_DEQUEUE(&ifp->if_snd, mb_head);
954 if (mb_head == NULL)
955 break;
956
957 /*
958 * Get pointer to next available tx desc.
959 */
960 txp = sc->tx_free;
961 sc->tx_free = txp->tx_next;
962 txp->tx_next = NULL;
963 dmamap = txp->tx_dmamap;
964
965 /*
966 * Go through each of the mbufs in the chain and initialize
967 * the transmit buffer descriptors with the physical address
968 * and size of the mbuf.
969 */
970 reload:
971 error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap,
972 mb_head, BUS_DMA_NOWAIT);
973 switch (error) {
974 case 0:
975 /* Success. */
976 break;
977
978 case EFBIG:
979 {
980 struct mbuf *mn;
981
982 /*
983 * We ran out of segments. We have to recopy this
984 * mbuf chain first. Bail out if we can't get the
985 * new buffers.
986 */
987 printf("%s: too many segments, ", sc->sc_dev.dv_xname);
988
989 MGETHDR(mn, M_DONTWAIT, MT_DATA);
990 if (mn == NULL) {
991 m_freem(mb_head);
992 printf("aborting\n");
993 goto out;
994 }
995 if (mb_head->m_pkthdr.len > MHLEN) {
996 MCLGET(mn, M_DONTWAIT);
997 if ((mn->m_flags & M_EXT) == 0) {
998 m_freem(mn);
999 m_freem(mb_head);
1000 printf("aborting\n");
1001 goto out;
1002 }
1003 }
1004 m_copydata(mb_head, 0, mb_head->m_pkthdr.len,
1005 mtod(mn, caddr_t));
1006 mn->m_pkthdr.len = mn->m_len = mb_head->m_pkthdr.len;
1007 m_freem(mb_head);
1008 mb_head = mn;
1009 printf("retrying\n");
1010 goto reload;
1011 }
1012
1013 default:
1014 /*
1015 * Some other problem; report it.
1016 */
1017 printf("%s: can't load mbuf chain, error = %d\n",
1018 sc->sc_dev.dv_xname, error);
1019 m_freem(mb_head);
1020 goto out;
1021 }
1022
1023 fr = &txp->tx_dpd->dpd_frags[0];
1024 totlen = 0;
1025 for (segment = 0; segment < dmamap->dm_nsegs; segment++, fr++) {
1026 fr->fr_addr = htole32(dmamap->dm_segs[segment].ds_addr);
1027 fr->fr_len = htole32(dmamap->dm_segs[segment].ds_len);
1028 totlen += dmamap->dm_segs[segment].ds_len;
1029 }
1030 fr--;
1031 fr->fr_len |= htole32(EX_FR_LAST);
1032 txp->tx_mbhead = mb_head;
1033
1034 bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
1035 BUS_DMASYNC_PREWRITE);
1036
1037 dpd = txp->tx_dpd;
1038 dpd->dpd_nextptr = 0;
1039 dpd->dpd_fsh = htole32(totlen);
1040
1041 /* Byte-swap constants to compiler can optimize. */
1042
1043 if (sc->ex_conf & EX_CONF_90XB) {
1044 csum_flags = 0;
1045
1046 if (mb_head->m_pkthdr.csum_flags & M_CSUM_IPv4)
1047 csum_flags |= htole32(EX_DPD_IPCKSUM);
1048
1049 if (mb_head->m_pkthdr.csum_flags & M_CSUM_TCPv4)
1050 csum_flags |= htole32(EX_DPD_TCPCKSUM);
1051 else if (mb_head->m_pkthdr.csum_flags & M_CSUM_UDPv4)
1052 csum_flags |= htole32(EX_DPD_UDPCKSUM);
1053
1054 dpd->dpd_fsh |= csum_flags;
1055 } else {
1056 KDASSERT((mb_head->m_pkthdr.csum_flags &
1057 (M_CSUM_IPv4|M_CSUM_TCPv4|M_CSUM_UDPv4)) == 0);
1058 }
1059
1060 bus_dmamap_sync(sc->sc_dmat, sc->sc_dpd_dmamap,
1061 ((caddr_t)dpd - (caddr_t)sc->sc_dpd),
1062 sizeof (struct ex_dpd),
1063 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1064
1065 /*
1066 * No need to stall the download engine, we know it's
1067 * not busy right now.
1068 *
1069 * Fix up pointers in both the "soft" tx and the physical
1070 * tx list.
1071 */
1072 if (sc->tx_head != NULL) {
1073 prevdpd = sc->tx_tail->tx_dpd;
1074 offset = ((caddr_t)prevdpd - (caddr_t)sc->sc_dpd);
1075 bus_dmamap_sync(sc->sc_dmat, sc->sc_dpd_dmamap,
1076 offset, sizeof (struct ex_dpd),
1077 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1078 prevdpd->dpd_nextptr = htole32(DPD_DMADDR(sc, txp));
1079 bus_dmamap_sync(sc->sc_dmat, sc->sc_dpd_dmamap,
1080 offset, sizeof (struct ex_dpd),
1081 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1082 sc->tx_tail->tx_next = txp;
1083 sc->tx_tail = txp;
1084 } else {
1085 sc->tx_tail = sc->tx_head = txp;
1086 }
1087
1088 #if NBPFILTER > 0
1089 /*
1090 * Pass packet to bpf if there is a listener.
1091 */
1092 if (ifp->if_bpf)
1093 bpf_mtap(ifp->if_bpf, mb_head);
1094 #endif
1095 }
1096 out:
1097 if (sc->tx_head) {
1098 sc->tx_tail->tx_dpd->dpd_fsh |= htole32(EX_DPD_DNIND);
1099 bus_dmamap_sync(sc->sc_dmat, sc->sc_dpd_dmamap,
1100 ((caddr_t)sc->tx_tail->tx_dpd - (caddr_t)sc->sc_dpd),
1101 sizeof (struct ex_dpd),
1102 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1103 ifp->if_flags |= IFF_OACTIVE;
1104 bus_space_write_2(iot, ioh, ELINK_COMMAND, ELINK_DNUNSTALL);
1105 bus_space_write_4(iot, ioh, ELINK_DNLISTPTR,
1106 DPD_DMADDR(sc, sc->tx_head));
1107
1108 /* trigger watchdog */
1109 ifp->if_timer = 5;
1110 }
1111 }
1112
1113
1114 int
1115 ex_intr(arg)
1116 void *arg;
1117 {
1118 struct ex_softc *sc = arg;
1119 bus_space_tag_t iot = sc->sc_iot;
1120 bus_space_handle_t ioh = sc->sc_ioh;
1121 u_int16_t stat;
1122 int ret = 0;
1123 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1124
1125 if ((ifp->if_flags & IFF_RUNNING) == 0 ||
1126 (sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
1127 return (0);
1128
1129 for (;;) {
1130 bus_space_write_2(iot, ioh, ELINK_COMMAND, C_INTR_LATCH);
1131
1132 stat = bus_space_read_2(iot, ioh, ELINK_STATUS);
1133
1134 if ((stat & S_MASK) == 0) {
1135 if ((stat & S_INTR_LATCH) == 0) {
1136 #if 0
1137 printf("%s: intr latch cleared\n",
1138 sc->sc_dev.dv_xname);
1139 #endif
1140 break;
1141 }
1142 }
1143
1144 ret = 1;
1145
1146 /*
1147 * Acknowledge interrupts.
1148 */
1149 bus_space_write_2(iot, ioh, ELINK_COMMAND, ACK_INTR |
1150 (stat & S_MASK));
1151 if (sc->intr_ack)
1152 (*sc->intr_ack)(sc);
1153
1154 if (stat & S_HOST_ERROR) {
1155 printf("%s: adapter failure (%x)\n",
1156 sc->sc_dev.dv_xname, stat);
1157 ex_reset(sc);
1158 ex_init(ifp);
1159 return 1;
1160 }
1161 if (stat & S_TX_COMPLETE) {
1162 ex_txstat(sc);
1163 }
1164 if (stat & S_UPD_STATS) {
1165 ex_getstats(sc);
1166 }
1167 if (stat & S_DN_COMPLETE) {
1168 struct ex_txdesc *txp, *ptxp = NULL;
1169 bus_dmamap_t txmap;
1170
1171 /* reset watchdog timer, was set in ex_start() */
1172 ifp->if_timer = 0;
1173
1174 for (txp = sc->tx_head; txp != NULL;
1175 txp = txp->tx_next) {
1176 bus_dmamap_sync(sc->sc_dmat,
1177 sc->sc_dpd_dmamap,
1178 (caddr_t)txp->tx_dpd - (caddr_t)sc->sc_dpd,
1179 sizeof (struct ex_dpd),
1180 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1181 if (txp->tx_mbhead != NULL) {
1182 txmap = txp->tx_dmamap;
1183 bus_dmamap_sync(sc->sc_dmat, txmap,
1184 0, txmap->dm_mapsize,
1185 BUS_DMASYNC_POSTWRITE);
1186 bus_dmamap_unload(sc->sc_dmat, txmap);
1187 m_freem(txp->tx_mbhead);
1188 txp->tx_mbhead = NULL;
1189 }
1190 ptxp = txp;
1191 }
1192
1193 /*
1194 * Move finished tx buffers back to the tx free list.
1195 */
1196 if (sc->tx_free) {
1197 sc->tx_ftail->tx_next = sc->tx_head;
1198 sc->tx_ftail = ptxp;
1199 } else
1200 sc->tx_ftail = sc->tx_free = sc->tx_head;
1201
1202 sc->tx_head = sc->tx_tail = NULL;
1203 ifp->if_flags &= ~IFF_OACTIVE;
1204 }
1205
1206 if (stat & S_UP_COMPLETE) {
1207 struct ex_rxdesc *rxd;
1208 struct mbuf *m;
1209 struct ex_upd *upd;
1210 bus_dmamap_t rxmap;
1211 u_int32_t pktstat;
1212
1213 rcvloop:
1214 rxd = sc->rx_head;
1215 rxmap = rxd->rx_dmamap;
1216 m = rxd->rx_mbhead;
1217 upd = rxd->rx_upd;
1218
1219 bus_dmamap_sync(sc->sc_dmat, rxmap, 0,
1220 rxmap->dm_mapsize,
1221 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1222 bus_dmamap_sync(sc->sc_dmat, sc->sc_upd_dmamap,
1223 ((caddr_t)upd - (caddr_t)sc->sc_upd),
1224 sizeof (struct ex_upd),
1225 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1226 pktstat = le32toh(upd->upd_pktstatus);
1227
1228 if (pktstat & EX_UPD_COMPLETE) {
1229 /*
1230 * Remove first packet from the chain.
1231 */
1232 sc->rx_head = rxd->rx_next;
1233 rxd->rx_next = NULL;
1234
1235 /*
1236 * Add a new buffer to the receive chain.
1237 * If this fails, the old buffer is recycled
1238 * instead.
1239 */
1240 if (ex_add_rxbuf(sc, rxd) == 0) {
1241 u_int16_t total_len;
1242
1243 if (pktstat &
1244 ((sc->sc_ethercom.ec_capenable &
1245 ETHERCAP_VLAN_MTU) ?
1246 EX_UPD_ERR_VLAN : EX_UPD_ERR)) {
1247 ifp->if_ierrors++;
1248 m_freem(m);
1249 goto rcvloop;
1250 }
1251
1252 total_len = pktstat & EX_UPD_PKTLENMASK;
1253 if (total_len <
1254 sizeof(struct ether_header)) {
1255 m_freem(m);
1256 goto rcvloop;
1257 }
1258 m->m_pkthdr.rcvif = ifp;
1259 m->m_pkthdr.len = m->m_len = total_len;
1260 #if NBPFILTER > 0
1261 if (ifp->if_bpf)
1262 bpf_mtap(ifp->if_bpf, m);
1263 #endif
1264 /*
1265 * Set the incoming checksum information for the packet.
1266 */
1267 if ((sc->ex_conf & EX_CONF_90XB) != 0 &&
1268 (pktstat & EX_UPD_IPCHECKED) != 0) {
1269 m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
1270 if (pktstat & EX_UPD_IPCKSUMERR)
1271 m->m_pkthdr.csum_flags |= M_CSUM_IPv4_BAD;
1272 if (pktstat & EX_UPD_TCPCHECKED) {
1273 m->m_pkthdr.csum_flags |= M_CSUM_TCPv4;
1274 if (pktstat & EX_UPD_TCPCKSUMERR)
1275 m->m_pkthdr.csum_flags |=
1276 M_CSUM_TCP_UDP_BAD;
1277 } else if (pktstat & EX_UPD_UDPCHECKED) {
1278 m->m_pkthdr.csum_flags |= M_CSUM_UDPv4;
1279 if (pktstat & EX_UPD_UDPCKSUMERR)
1280 m->m_pkthdr.csum_flags |=
1281 M_CSUM_TCP_UDP_BAD;
1282 }
1283 }
1284 (*ifp->if_input)(ifp, m);
1285 }
1286 goto rcvloop;
1287 }
1288 /*
1289 * Just in case we filled up all UPDs and the DMA engine
1290 * stalled. We could be more subtle about this.
1291 */
1292 if (bus_space_read_4(iot, ioh, ELINK_UPLISTPTR) == 0) {
1293 printf("%s: uplistptr was 0\n",
1294 sc->sc_dev.dv_xname);
1295 ex_init(ifp);
1296 } else if (bus_space_read_4(iot, ioh, ELINK_UPPKTSTATUS)
1297 & 0x2000) {
1298 printf("%s: receive stalled\n",
1299 sc->sc_dev.dv_xname);
1300 bus_space_write_2(iot, ioh, ELINK_COMMAND,
1301 ELINK_UPUNSTALL);
1302 }
1303 }
1304 }
1305
1306 /* no more interrupts */
1307 if (ret && IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1308 ex_start(ifp);
1309 return ret;
1310 }
1311
1312 int
1313 ex_ioctl(ifp, cmd, data)
1314 struct ifnet *ifp;
1315 u_long cmd;
1316 caddr_t data;
1317 {
1318 struct ex_softc *sc = ifp->if_softc;
1319 struct ifreq *ifr = (struct ifreq *)data;
1320 int s, error;
1321
1322 s = splnet();
1323
1324 switch (cmd) {
1325 case SIOCSIFMEDIA:
1326 case SIOCGIFMEDIA:
1327 error = ifmedia_ioctl(ifp, ifr, &sc->ex_mii.mii_media, cmd);
1328 break;
1329
1330 default:
1331 error = ether_ioctl(ifp, cmd, data);
1332 if (error == ENETRESET) {
1333 if (sc->enabled) {
1334 /*
1335 * Multicast list has changed; set the hardware filter
1336 * accordingly.
1337 */
1338 ex_set_mc(sc);
1339 }
1340 error = 0;
1341 }
1342 break;
1343 }
1344
1345 splx(s);
1346 return (error);
1347 }
1348
1349 void
1350 ex_getstats(sc)
1351 struct ex_softc *sc;
1352 {
1353 bus_space_handle_t ioh = sc->sc_ioh;
1354 bus_space_tag_t iot = sc->sc_iot;
1355 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1356 u_int8_t upperok;
1357
1358 GO_WINDOW(6);
1359 upperok = bus_space_read_1(iot, ioh, UPPER_FRAMES_OK);
1360 ifp->if_ipackets += bus_space_read_1(iot, ioh, RX_FRAMES_OK);
1361 ifp->if_ipackets += (upperok & 0x03) << 8;
1362 ifp->if_opackets += bus_space_read_1(iot, ioh, TX_FRAMES_OK);
1363 ifp->if_opackets += (upperok & 0x30) << 4;
1364 ifp->if_ierrors += bus_space_read_1(iot, ioh, RX_OVERRUNS);
1365 ifp->if_collisions += bus_space_read_1(iot, ioh, TX_COLLISIONS);
1366 /*
1367 * There seems to be no way to get the exact number of collisions,
1368 * this is the number that occured at the very least.
1369 */
1370 ifp->if_collisions += 2 * bus_space_read_1(iot, ioh,
1371 TX_AFTER_X_COLLISIONS);
1372 ifp->if_ibytes += bus_space_read_2(iot, ioh, RX_TOTAL_OK);
1373 ifp->if_obytes += bus_space_read_2(iot, ioh, TX_TOTAL_OK);
1374
1375 /*
1376 * Clear the following to avoid stats overflow interrupts
1377 */
1378 bus_space_read_1(iot, ioh, TX_DEFERRALS);
1379 bus_space_read_1(iot, ioh, TX_AFTER_1_COLLISION);
1380 bus_space_read_1(iot, ioh, TX_NO_SQE);
1381 bus_space_read_1(iot, ioh, TX_CD_LOST);
1382 GO_WINDOW(4);
1383 bus_space_read_1(iot, ioh, ELINK_W4_BADSSD);
1384 upperok = bus_space_read_1(iot, ioh, ELINK_W4_UBYTESOK);
1385 ifp->if_ibytes += (upperok & 0x0f) << 16;
1386 ifp->if_obytes += (upperok & 0xf0) << 12;
1387 GO_WINDOW(1);
1388 }
1389
1390 void
1391 ex_printstats(sc)
1392 struct ex_softc *sc;
1393 {
1394 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1395
1396 ex_getstats(sc);
1397 printf("in %llu out %llu ierror %llu oerror %llu ibytes %llu obytes "
1398 "%llu\n", (unsigned long long)ifp->if_ipackets,
1399 (unsigned long long)ifp->if_opackets,
1400 (unsigned long long)ifp->if_ierrors,
1401 (unsigned long long)ifp->if_oerrors,
1402 (unsigned long long)ifp->if_ibytes,
1403 (unsigned long long)ifp->if_obytes);
1404 }
1405
1406 void
1407 ex_tick(arg)
1408 void *arg;
1409 {
1410 struct ex_softc *sc = arg;
1411 int s;
1412
1413 if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
1414 return;
1415
1416 s = splnet();
1417
1418 if (sc->ex_conf & EX_CONF_MII)
1419 mii_tick(&sc->ex_mii);
1420
1421 if (!(bus_space_read_2((sc)->sc_iot, (sc)->sc_ioh, ELINK_STATUS)
1422 & S_COMMAND_IN_PROGRESS))
1423 ex_getstats(sc);
1424
1425 splx(s);
1426
1427 callout_reset(&sc->ex_mii_callout, hz, ex_tick, sc);
1428 }
1429
1430 void
1431 ex_reset(sc)
1432 struct ex_softc *sc;
1433 {
1434 u_int16_t val = GLOBAL_RESET;
1435
1436 if (sc->ex_conf & EX_CONF_RESETHACK)
1437 val |= 0x10;
1438 bus_space_write_2(sc->sc_iot, sc->sc_ioh, ELINK_COMMAND, val);
1439 /*
1440 * XXX apparently the command in progress bit can't be trusted
1441 * during a reset, so we just always wait this long. Fortunately
1442 * we normally only reset the chip during autoconfig.
1443 */
1444 delay(100000);
1445 ex_waitcmd(sc);
1446 }
1447
1448 void
1449 ex_watchdog(ifp)
1450 struct ifnet *ifp;
1451 {
1452 struct ex_softc *sc = ifp->if_softc;
1453
1454 log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
1455 ++sc->sc_ethercom.ec_if.if_oerrors;
1456
1457 ex_reset(sc);
1458 ex_init(ifp);
1459 }
1460
1461 void
1462 ex_stop(ifp, disable)
1463 struct ifnet *ifp;
1464 int disable;
1465 {
1466 struct ex_softc *sc = ifp->if_softc;
1467 bus_space_tag_t iot = sc->sc_iot;
1468 bus_space_handle_t ioh = sc->sc_ioh;
1469 struct ex_txdesc *tx;
1470 struct ex_rxdesc *rx;
1471 int i;
1472
1473 bus_space_write_2(iot, ioh, ELINK_COMMAND, RX_DISABLE);
1474 bus_space_write_2(iot, ioh, ELINK_COMMAND, TX_DISABLE);
1475 bus_space_write_2(iot, ioh, ELINK_COMMAND, STOP_TRANSCEIVER);
1476
1477 for (tx = sc->tx_head ; tx != NULL; tx = tx->tx_next) {
1478 if (tx->tx_mbhead == NULL)
1479 continue;
1480 m_freem(tx->tx_mbhead);
1481 tx->tx_mbhead = NULL;
1482 bus_dmamap_unload(sc->sc_dmat, tx->tx_dmamap);
1483 tx->tx_dpd->dpd_fsh = tx->tx_dpd->dpd_nextptr = 0;
1484 bus_dmamap_sync(sc->sc_dmat, sc->sc_dpd_dmamap,
1485 ((caddr_t)tx->tx_dpd - (caddr_t)sc->sc_dpd),
1486 sizeof (struct ex_dpd),
1487 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1488 }
1489 sc->tx_tail = sc->tx_head = NULL;
1490 ex_init_txdescs(sc);
1491
1492 sc->rx_tail = sc->rx_head = 0;
1493 for (i = 0; i < EX_NUPD; i++) {
1494 rx = &sc->sc_rxdescs[i];
1495 if (rx->rx_mbhead != NULL) {
1496 bus_dmamap_unload(sc->sc_dmat, rx->rx_dmamap);
1497 m_freem(rx->rx_mbhead);
1498 rx->rx_mbhead = NULL;
1499 }
1500 ex_add_rxbuf(sc, rx);
1501 }
1502
1503 bus_space_write_2(iot, ioh, ELINK_COMMAND, C_INTR_LATCH);
1504
1505 callout_stop(&sc->ex_mii_callout);
1506 if (sc->ex_conf & EX_CONF_MII)
1507 mii_down(&sc->ex_mii);
1508
1509 if (disable)
1510 ex_disable(sc);
1511
1512 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1513 ifp->if_timer = 0;
1514 }
1515
1516 static void
1517 ex_init_txdescs(sc)
1518 struct ex_softc *sc;
1519 {
1520 int i;
1521
1522 for (i = 0; i < EX_NDPD; i++) {
1523 sc->sc_txdescs[i].tx_dmamap = sc->sc_tx_dmamaps[i];
1524 sc->sc_txdescs[i].tx_dpd = &sc->sc_dpd[i];
1525 if (i < EX_NDPD - 1)
1526 sc->sc_txdescs[i].tx_next = &sc->sc_txdescs[i + 1];
1527 else
1528 sc->sc_txdescs[i].tx_next = NULL;
1529 }
1530 sc->tx_free = &sc->sc_txdescs[0];
1531 sc->tx_ftail = &sc->sc_txdescs[EX_NDPD-1];
1532 }
1533
1534
1535 int
1536 ex_activate(self, act)
1537 struct device *self;
1538 enum devact act;
1539 {
1540 struct ex_softc *sc = (void *) self;
1541 int s, error = 0;
1542
1543 s = splnet();
1544 switch (act) {
1545 case DVACT_ACTIVATE:
1546 error = EOPNOTSUPP;
1547 break;
1548
1549 case DVACT_DEACTIVATE:
1550 if (sc->ex_conf & EX_CONF_MII)
1551 mii_activate(&sc->ex_mii, act, MII_PHY_ANY,
1552 MII_OFFSET_ANY);
1553 if_deactivate(&sc->sc_ethercom.ec_if);
1554 break;
1555 }
1556 splx(s);
1557
1558 return (error);
1559 }
1560
1561 int
1562 ex_detach(sc)
1563 struct ex_softc *sc;
1564 {
1565 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1566 struct ex_rxdesc *rxd;
1567 int i;
1568
1569 /* Succeed now if there's no work to do. */
1570 if ((sc->ex_flags & EX_FLAGS_ATTACHED) == 0)
1571 return (0);
1572
1573 /* Unhook our tick handler. */
1574 callout_stop(&sc->ex_mii_callout);
1575
1576 if (sc->ex_conf & EX_CONF_MII) {
1577 /* Detach all PHYs */
1578 mii_detach(&sc->ex_mii, MII_PHY_ANY, MII_OFFSET_ANY);
1579 }
1580
1581 /* Delete all remaining media. */
1582 ifmedia_delete_instance(&sc->ex_mii.mii_media, IFM_INST_ANY);
1583
1584 #if NRND > 0
1585 rnd_detach_source(&sc->rnd_source);
1586 #endif
1587 ether_ifdetach(ifp);
1588 if_detach(ifp);
1589
1590 for (i = 0; i < EX_NUPD; i++) {
1591 rxd = &sc->sc_rxdescs[i];
1592 if (rxd->rx_mbhead != NULL) {
1593 bus_dmamap_unload(sc->sc_dmat, rxd->rx_dmamap);
1594 m_freem(rxd->rx_mbhead);
1595 rxd->rx_mbhead = NULL;
1596 }
1597 }
1598 for (i = 0; i < EX_NUPD; i++)
1599 bus_dmamap_destroy(sc->sc_dmat, sc->sc_rx_dmamaps[i]);
1600 for (i = 0; i < EX_NDPD; i++)
1601 bus_dmamap_destroy(sc->sc_dmat, sc->sc_tx_dmamaps[i]);
1602 bus_dmamap_unload(sc->sc_dmat, sc->sc_dpd_dmamap);
1603 bus_dmamap_destroy(sc->sc_dmat, sc->sc_dpd_dmamap);
1604 bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_dpd,
1605 EX_NDPD * sizeof (struct ex_dpd));
1606 bus_dmamem_free(sc->sc_dmat, &sc->sc_dseg, sc->sc_drseg);
1607 bus_dmamap_unload(sc->sc_dmat, sc->sc_upd_dmamap);
1608 bus_dmamap_destroy(sc->sc_dmat, sc->sc_upd_dmamap);
1609 bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_upd,
1610 EX_NUPD * sizeof (struct ex_upd));
1611 bus_dmamem_free(sc->sc_dmat, &sc->sc_useg, sc->sc_urseg);
1612
1613 shutdownhook_disestablish(sc->sc_sdhook);
1614 powerhook_disestablish(sc->sc_powerhook);
1615
1616 return (0);
1617 }
1618
1619 /*
1620 * Before reboots, reset card completely.
1621 */
1622 static void
1623 ex_shutdown(arg)
1624 void *arg;
1625 {
1626 struct ex_softc *sc = arg;
1627
1628 ex_stop(&sc->sc_ethercom.ec_if, 1);
1629 }
1630
1631 /*
1632 * Read EEPROM data.
1633 * XXX what to do if EEPROM doesn't unbusy?
1634 */
1635 u_int16_t
1636 ex_read_eeprom(sc, offset)
1637 struct ex_softc *sc;
1638 int offset;
1639 {
1640 bus_space_tag_t iot = sc->sc_iot;
1641 bus_space_handle_t ioh = sc->sc_ioh;
1642 u_int16_t data = 0, cmd = READ_EEPROM;
1643 int off;
1644
1645 off = sc->ex_conf & EX_CONF_EEPROM_OFF ? 0x30 : 0;
1646 cmd = sc->ex_conf & EX_CONF_EEPROM_8BIT ? READ_EEPROM8 : READ_EEPROM;
1647
1648 GO_WINDOW(0);
1649 if (ex_eeprom_busy(sc))
1650 goto out;
1651 bus_space_write_2(iot, ioh, ELINK_W0_EEPROM_COMMAND,
1652 cmd | (off + (offset & 0x3f)));
1653 if (ex_eeprom_busy(sc))
1654 goto out;
1655 data = bus_space_read_2(iot, ioh, ELINK_W0_EEPROM_DATA);
1656 out:
1657 return data;
1658 }
1659
1660 static int
1661 ex_eeprom_busy(sc)
1662 struct ex_softc *sc;
1663 {
1664 bus_space_tag_t iot = sc->sc_iot;
1665 bus_space_handle_t ioh = sc->sc_ioh;
1666 int i = 100;
1667
1668 while (i--) {
1669 if (!(bus_space_read_2(iot, ioh, ELINK_W0_EEPROM_COMMAND) &
1670 EEPROM_BUSY))
1671 return 0;
1672 delay(100);
1673 }
1674 printf("\n%s: eeprom stays busy.\n", sc->sc_dev.dv_xname);
1675 return (1);
1676 }
1677
1678 /*
1679 * Create a new rx buffer and add it to the 'soft' rx list.
1680 */
1681 static int
1682 ex_add_rxbuf(sc, rxd)
1683 struct ex_softc *sc;
1684 struct ex_rxdesc *rxd;
1685 {
1686 struct mbuf *m, *oldm;
1687 bus_dmamap_t rxmap;
1688 int error, rval = 0;
1689
1690 oldm = rxd->rx_mbhead;
1691 rxmap = rxd->rx_dmamap;
1692
1693 MGETHDR(m, M_DONTWAIT, MT_DATA);
1694 if (m != NULL) {
1695 MCLGET(m, M_DONTWAIT);
1696 if ((m->m_flags & M_EXT) == 0) {
1697 m_freem(m);
1698 if (oldm == NULL)
1699 return 1;
1700 m = oldm;
1701 m->m_data = m->m_ext.ext_buf;
1702 rval = 1;
1703 }
1704 } else {
1705 if (oldm == NULL)
1706 return 1;
1707 m = oldm;
1708 m->m_data = m->m_ext.ext_buf;
1709 rval = 1;
1710 }
1711
1712 /*
1713 * Setup the DMA map for this receive buffer.
1714 */
1715 if (m != oldm) {
1716 if (oldm != NULL)
1717 bus_dmamap_unload(sc->sc_dmat, rxmap);
1718 error = bus_dmamap_load(sc->sc_dmat, rxmap,
1719 m->m_ext.ext_buf, MCLBYTES, NULL, BUS_DMA_NOWAIT);
1720 if (error) {
1721 printf("%s: can't load rx buffer, error = %d\n",
1722 sc->sc_dev.dv_xname, error);
1723 panic("ex_add_rxbuf"); /* XXX */
1724 }
1725 }
1726
1727 /*
1728 * Align for data after 14 byte header.
1729 */
1730 m->m_data += 2;
1731
1732 rxd->rx_mbhead = m;
1733 rxd->rx_upd->upd_pktstatus = htole32(MCLBYTES - 2);
1734 rxd->rx_upd->upd_frags[0].fr_addr =
1735 htole32(rxmap->dm_segs[0].ds_addr + 2);
1736 rxd->rx_upd->upd_nextptr = 0;
1737
1738 /*
1739 * Attach it to the end of the list.
1740 */
1741 if (sc->rx_head != NULL) {
1742 sc->rx_tail->rx_next = rxd;
1743 sc->rx_tail->rx_upd->upd_nextptr = htole32(sc->sc_upddma +
1744 ((caddr_t)rxd->rx_upd - (caddr_t)sc->sc_upd));
1745 bus_dmamap_sync(sc->sc_dmat, sc->sc_upd_dmamap,
1746 (caddr_t)sc->rx_tail->rx_upd - (caddr_t)sc->sc_upd,
1747 sizeof (struct ex_upd),
1748 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1749 } else {
1750 sc->rx_head = rxd;
1751 }
1752 sc->rx_tail = rxd;
1753
1754 bus_dmamap_sync(sc->sc_dmat, rxmap, 0, rxmap->dm_mapsize,
1755 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1756 bus_dmamap_sync(sc->sc_dmat, sc->sc_upd_dmamap,
1757 ((caddr_t)rxd->rx_upd - (caddr_t)sc->sc_upd),
1758 sizeof (struct ex_upd), BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1759 return (rval);
1760 }
1761
1762 u_int32_t
1763 ex_mii_bitbang_read(self)
1764 struct device *self;
1765 {
1766 struct ex_softc *sc = (void *) self;
1767
1768 /* We're already in Window 4. */
1769 return (bus_space_read_2(sc->sc_iot, sc->sc_ioh, ELINK_W4_PHYSMGMT));
1770 }
1771
1772 void
1773 ex_mii_bitbang_write(self, val)
1774 struct device *self;
1775 u_int32_t val;
1776 {
1777 struct ex_softc *sc = (void *) self;
1778
1779 /* We're already in Window 4. */
1780 bus_space_write_2(sc->sc_iot, sc->sc_ioh, ELINK_W4_PHYSMGMT, val);
1781 }
1782
1783 int
1784 ex_mii_readreg(v, phy, reg)
1785 struct device *v;
1786 int phy, reg;
1787 {
1788 struct ex_softc *sc = (struct ex_softc *)v;
1789 int val;
1790
1791 if ((sc->ex_conf & EX_CONF_INTPHY) && phy != ELINK_INTPHY_ID)
1792 return 0;
1793
1794 GO_WINDOW(4);
1795
1796 val = mii_bitbang_readreg(v, &ex_mii_bitbang_ops, phy, reg);
1797
1798 GO_WINDOW(1);
1799
1800 return (val);
1801 }
1802
1803 void
1804 ex_mii_writereg(v, phy, reg, data)
1805 struct device *v;
1806 int phy;
1807 int reg;
1808 int data;
1809 {
1810 struct ex_softc *sc = (struct ex_softc *)v;
1811
1812 GO_WINDOW(4);
1813
1814 mii_bitbang_writereg(v, &ex_mii_bitbang_ops, phy, reg, data);
1815
1816 GO_WINDOW(1);
1817 }
1818
1819 void
1820 ex_mii_statchg(v)
1821 struct device *v;
1822 {
1823 struct ex_softc *sc = (struct ex_softc *)v;
1824 bus_space_tag_t iot = sc->sc_iot;
1825 bus_space_handle_t ioh = sc->sc_ioh;
1826 int mctl;
1827
1828 GO_WINDOW(3);
1829 mctl = bus_space_read_2(iot, ioh, ELINK_W3_MAC_CONTROL);
1830 if (sc->ex_mii.mii_media_active & IFM_FDX)
1831 mctl |= MAC_CONTROL_FDX;
1832 else
1833 mctl &= ~MAC_CONTROL_FDX;
1834 bus_space_write_2(iot, ioh, ELINK_W3_MAC_CONTROL, mctl);
1835 GO_WINDOW(1); /* back to operating window */
1836 }
1837
1838 int
1839 ex_enable(sc)
1840 struct ex_softc *sc;
1841 {
1842 if (sc->enabled == 0 && sc->enable != NULL) {
1843 if ((*sc->enable)(sc) != 0) {
1844 printf("%s: de/vice enable failed\n",
1845 sc->sc_dev.dv_xname);
1846 return (EIO);
1847 }
1848 sc->enabled = 1;
1849 }
1850 return (0);
1851 }
1852
1853 void
1854 ex_disable(sc)
1855 struct ex_softc *sc;
1856 {
1857 if (sc->enabled == 1 && sc->disable != NULL) {
1858 (*sc->disable)(sc);
1859 sc->enabled = 0;
1860 }
1861 }
1862
1863 void
1864 ex_power(why, arg)
1865 int why;
1866 void *arg;
1867 {
1868 struct ex_softc *sc = (void *)arg;
1869 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1870 int s;
1871
1872 s = splnet();
1873 switch (why) {
1874 case PWR_SUSPEND:
1875 case PWR_STANDBY:
1876 ex_stop(ifp, 0);
1877 if (sc->power != NULL)
1878 (*sc->power)(sc, why);
1879 break;
1880 case PWR_RESUME:
1881 if (ifp->if_flags & IFF_UP) {
1882 if (sc->power != NULL)
1883 (*sc->power)(sc, why);
1884 ex_init(ifp);
1885 }
1886 break;
1887 case PWR_SOFTSUSPEND:
1888 case PWR_SOFTSTANDBY:
1889 case PWR_SOFTRESUME:
1890 break;
1891 }
1892 splx(s);
1893 }
1894