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