if_gfe.c revision 1.50 1 /* $NetBSD: if_gfe.c,v 1.50 2019/01/22 03:42:27 msaitoh Exp $ */
2
3 /*
4 * Copyright (c) 2002 Allegro Networks, Inc., Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed for the NetBSD Project by
18 * Allegro Networks, Inc., and Wasabi Systems, Inc.
19 * 4. The name of Allegro Networks, Inc. may not be used to endorse
20 * or promote products derived from this software without specific prior
21 * written permission.
22 * 5. The name of Wasabi Systems, Inc. may not be used to endorse
23 * or promote products derived from this software without specific prior
24 * written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY ALLEGRO NETWORKS, INC. AND
27 * WASABI SYSTEMS, INC. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
28 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
29 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 * IN NO EVENT SHALL EITHER ALLEGRO NETWORKS, INC. OR WASABI SYSTEMS, INC.
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * if_gfe.c -- GT ethernet MAC driver
42 */
43
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: if_gfe.c,v 1.50 2019/01/22 03:42:27 msaitoh Exp $");
46
47 #include "opt_inet.h"
48
49 #include <sys/param.h>
50 #include <sys/bus.h>
51 #include <sys/callout.h>
52 #include <sys/device.h>
53 #include <sys/errno.h>
54 #include <sys/ioctl.h>
55 #include <sys/mbuf.h>
56 #include <sys/mutex.h>
57 #include <sys/socket.h>
58
59 #include <uvm/uvm.h>
60 #include <net/if.h>
61 #include <net/if_dl.h>
62 #include <net/if_ether.h>
63 #include <net/if_media.h>
64
65 #ifdef INET
66 #include <netinet/in.h>
67 #include <netinet/if_inarp.h>
68 #endif
69 #include <net/bpf.h>
70 #include <sys/rndsource.h>
71
72 #include <dev/mii/mii.h>
73 #include <dev/mii/miivar.h>
74
75 #include <dev/marvell/gtreg.h>
76 #include <dev/marvell/gtvar.h>
77 #include <dev/marvell/gtethreg.h>
78 #include <dev/marvell/if_gfevar.h>
79 #include <dev/marvell/marvellreg.h>
80 #include <dev/marvell/marvellvar.h>
81
82 #include <prop/proplib.h>
83
84 #include "locators.h"
85
86
87 #define GE_READ(sc, reg) \
88 bus_space_read_4((sc)->sc_memt, (sc)->sc_memh, (reg))
89 #define GE_WRITE(sc, reg, v) \
90 bus_space_write_4((sc)->sc_memt, (sc)->sc_memh, (reg), (v))
91
92 #define GE_DEBUG
93 #if 0
94 #define GE_NOHASH
95 #define GE_NORX
96 #endif
97
98 #ifdef GE_DEBUG
99 #define GE_DPRINTF(sc, a) \
100 do { \
101 if ((sc)->sc_ec.ec_if.if_flags & IFF_DEBUG) \
102 printf a; \
103 } while (0 /* CONSTCOND */)
104 #define GE_FUNC_ENTER(sc, func) GE_DPRINTF(sc, ("[" func))
105 #define GE_FUNC_EXIT(sc, str) GE_DPRINTF(sc, (str "]"))
106 #else
107 #define GE_DPRINTF(sc, a) do { } while (0)
108 #define GE_FUNC_ENTER(sc, func) do { } while (0)
109 #define GE_FUNC_EXIT(sc, str) do { } while (0)
110 #endif
111 enum gfe_whack_op {
112 GE_WHACK_START, GE_WHACK_RESTART,
113 GE_WHACK_CHANGE, GE_WHACK_STOP
114 };
115
116 enum gfe_hash_op {
117 GE_HASH_ADD, GE_HASH_REMOVE,
118 };
119
120 #if 1
121 #define htogt32(a) htobe32(a)
122 #define gt32toh(a) be32toh(a)
123 #else
124 #define htogt32(a) htole32(a)
125 #define gt32toh(a) le32toh(a)
126 #endif
127
128 #define GE_RXDSYNC(sc, rxq, n, ops) \
129 bus_dmamap_sync((sc)->sc_dmat, (rxq)->rxq_desc_mem.gdm_map, \
130 (n) * sizeof((rxq)->rxq_descs[0]), sizeof((rxq)->rxq_descs[0]), \
131 (ops))
132 #define GE_RXDPRESYNC(sc, rxq, n) \
133 GE_RXDSYNC(sc, rxq, n, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)
134 #define GE_RXDPOSTSYNC(sc, rxq, n) \
135 GE_RXDSYNC(sc, rxq, n, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)
136
137 #define GE_TXDSYNC(sc, txq, n, ops) \
138 bus_dmamap_sync((sc)->sc_dmat, (txq)->txq_desc_mem.gdm_map, \
139 (n) * sizeof((txq)->txq_descs[0]), sizeof((txq)->txq_descs[0]), \
140 (ops))
141 #define GE_TXDPRESYNC(sc, txq, n) \
142 GE_TXDSYNC(sc, txq, n, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)
143 #define GE_TXDPOSTSYNC(sc, txq, n) \
144 GE_TXDSYNC(sc, txq, n, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)
145
146 #define STATIC
147
148
149 STATIC int gfec_match(device_t, cfdata_t, void *);
150 STATIC void gfec_attach(device_t, device_t, void *);
151
152 STATIC int gfec_print(void *, const char *);
153 STATIC int gfec_search(device_t, cfdata_t, const int *, void *);
154
155 STATIC int gfec_enet_phy(device_t, int);
156 STATIC int gfec_mii_read(device_t, int, int, uint16_t *);
157 STATIC int gfec_mii_write(device_t, int, int, uint16_t);
158 STATIC void gfec_mii_statchg(struct ifnet *);
159
160 STATIC int gfe_match(device_t, cfdata_t, void *);
161 STATIC void gfe_attach(device_t, device_t, void *);
162
163 STATIC int gfe_dmamem_alloc(struct gfe_softc *, struct gfe_dmamem *, int,
164 size_t, int);
165 STATIC void gfe_dmamem_free(struct gfe_softc *, struct gfe_dmamem *);
166
167 STATIC int gfe_ifioctl(struct ifnet *, u_long, void *);
168 STATIC void gfe_ifstart(struct ifnet *);
169 STATIC void gfe_ifwatchdog(struct ifnet *);
170
171 STATIC void gfe_tick(void *arg);
172
173 STATIC void gfe_tx_restart(void *);
174 STATIC int gfe_tx_enqueue(struct gfe_softc *, enum gfe_txprio);
175 STATIC uint32_t gfe_tx_done(struct gfe_softc *, enum gfe_txprio, uint32_t);
176 STATIC void gfe_tx_cleanup(struct gfe_softc *, enum gfe_txprio, int);
177 STATIC int gfe_tx_txqalloc(struct gfe_softc *, enum gfe_txprio);
178 STATIC int gfe_tx_start(struct gfe_softc *, enum gfe_txprio);
179 STATIC void gfe_tx_stop(struct gfe_softc *, enum gfe_whack_op);
180
181 STATIC void gfe_rx_cleanup(struct gfe_softc *, enum gfe_rxprio);
182 STATIC void gfe_rx_get(struct gfe_softc *, enum gfe_rxprio);
183 STATIC int gfe_rx_prime(struct gfe_softc *);
184 STATIC uint32_t gfe_rx_process(struct gfe_softc *, uint32_t, uint32_t);
185 STATIC int gfe_rx_rxqalloc(struct gfe_softc *, enum gfe_rxprio);
186 STATIC int gfe_rx_rxqinit(struct gfe_softc *, enum gfe_rxprio);
187 STATIC void gfe_rx_stop(struct gfe_softc *, enum gfe_whack_op);
188
189 STATIC int gfe_intr(void *);
190
191 STATIC int gfe_whack(struct gfe_softc *, enum gfe_whack_op);
192
193 STATIC int gfe_hash_compute(struct gfe_softc *, const uint8_t [ETHER_ADDR_LEN]);
194 STATIC int gfe_hash_entry_op(struct gfe_softc *, enum gfe_hash_op,
195 enum gfe_rxprio, const uint8_t [ETHER_ADDR_LEN]);
196 STATIC int gfe_hash_multichg(struct ethercom *, const struct ether_multi *,
197 u_long);
198 STATIC int gfe_hash_fill(struct gfe_softc *);
199 STATIC int gfe_hash_alloc(struct gfe_softc *);
200
201
202 CFATTACH_DECL_NEW(gfec, sizeof(struct gfec_softc),
203 gfec_match, gfec_attach, NULL, NULL);
204 CFATTACH_DECL_NEW(gfe, sizeof(struct gfe_softc),
205 gfe_match, gfe_attach, NULL, NULL);
206
207
208 /* ARGSUSED */
209 int
210 gfec_match(device_t parent, cfdata_t cf, void *aux)
211 {
212 struct marvell_attach_args *mva = aux;
213
214 if (strcmp(mva->mva_name, cf->cf_name) != 0)
215 return 0;
216 if (mva->mva_offset == MVA_OFFSET_DEFAULT)
217 return 0;
218
219 mva->mva_size = ETHC_SIZE;
220 return 1;
221 }
222
223 /* ARGSUSED */
224 void
225 gfec_attach(device_t parent, device_t self, void *aux)
226 {
227 struct gfec_softc *sc = device_private(self);
228 struct marvell_attach_args *mva = aux, gfea;
229 static int gfe_irqs[] = { 32, 33, 34 };
230 int i;
231
232 aprint_naive("\n");
233 aprint_normal(": Ethernet Controller\n");
234
235 sc->sc_dev = self;
236 sc->sc_iot = mva->mva_iot;
237 if (bus_space_subregion(mva->mva_iot, mva->mva_ioh, mva->mva_offset,
238 mva->mva_size, &sc->sc_ioh)) {
239 aprint_error_dev(self, "Cannot map registers\n");
240 return;
241 }
242
243 mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_NET);
244
245 for (i = 0; i < ETH_NUM; i++) {
246 gfea.mva_name = "gfe";
247 gfea.mva_model = mva->mva_model;
248 gfea.mva_iot = sc->sc_iot;
249 gfea.mva_ioh = sc->sc_ioh;
250 gfea.mva_unit = i;
251 gfea.mva_dmat = mva->mva_dmat;
252 gfea.mva_irq = gfe_irqs[i];
253 config_found_sm_loc(sc->sc_dev, "gfec", NULL, &gfea,
254 gfec_print, gfec_search);
255 }
256 }
257
258 int
259 gfec_print(void *aux, const char *pnp)
260 {
261 struct marvell_attach_args *gfea = aux;
262
263 if (pnp)
264 aprint_normal("%s at %s port %d",
265 gfea->mva_name, pnp, gfea->mva_unit);
266 else {
267 if (gfea->mva_unit != GFECCF_PORT_DEFAULT)
268 aprint_normal(" port %d", gfea->mva_unit);
269 if (gfea->mva_irq != GFECCF_IRQ_DEFAULT)
270 aprint_normal(" irq %d", gfea->mva_irq);
271 }
272 return UNCONF;
273 }
274
275 /* ARGSUSED */
276 int
277 gfec_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
278 {
279 struct marvell_attach_args *gfea = aux;
280
281 if (cf->cf_loc[GFECCF_PORT] == gfea->mva_unit &&
282 cf->cf_loc[GFECCF_IRQ] != GFECCF_IRQ_DEFAULT)
283 gfea->mva_irq = cf->cf_loc[GFECCF_IRQ];
284
285 return config_match(parent, cf, aux);
286 }
287
288 int
289 gfec_enet_phy(device_t dev, int unit)
290 {
291 struct gfec_softc *sc = device_private(dev);
292 uint32_t epar;
293
294 epar = bus_space_read_4(sc->sc_iot, sc->sc_ioh, ETH_EPAR);
295 return ETH_EPAR_PhyAD_GET(epar, unit);
296 }
297
298 int
299 gfec_mii_read(device_t dev, int phy, int reg, uint16_t *val)
300 {
301 struct gfec_softc *csc = device_private(device_parent(dev));
302 uint32_t data;
303 int count = 10000;
304
305 mutex_enter(&csc->sc_mtx);
306
307 do {
308 DELAY(10);
309 data = bus_space_read_4(csc->sc_iot, csc->sc_ioh, ETH_ESMIR);
310 } while ((data & ETH_ESMIR_Busy) && count-- > 0);
311
312 if (count == 0) {
313 aprint_error_dev(dev,
314 "mii read for phy %d reg %d busied out\n", phy, reg);
315 mutex_exit(&csc->sc_mtx);
316 return ETIMEDOUT;
317 }
318
319 bus_space_write_4(csc->sc_iot, csc->sc_ioh, ETH_ESMIR,
320 ETH_ESMIR_READ(phy, reg));
321
322 count = 10000;
323 do {
324 DELAY(10);
325 data = bus_space_read_4(csc->sc_iot, csc->sc_ioh, ETH_ESMIR);
326 } while ((data & ETH_ESMIR_ReadValid) == 0 && count-- > 0);
327
328 mutex_exit(&csc->sc_mtx);
329
330 if (count == 0) {
331 aprint_error_dev(dev,
332 "mii read for phy %d reg %d timed out\n", phy, reg);
333 return ETIMEDOUT;
334 }
335 #if defined(GTMIIDEBUG)
336 aprint_normal_dev(dev, "mii_read(%d, %d): %#x data %#x\n",
337 phy, reg, data, ETH_ESMIR_Value_GET(data));
338 #endif
339 *val = ETH_ESMIR_Value_GET(data);
340 return 0;
341 }
342
343 int
344 gfec_mii_write(device_t dev, int phy, int reg, uint16_t value)
345 {
346 struct gfec_softc *csc = device_private(device_parent(dev));
347 uint32_t data;
348 int count = 10000;
349
350 mutex_enter(&csc->sc_mtx);
351
352 do {
353 DELAY(10);
354 data = bus_space_read_4(csc->sc_iot, csc->sc_ioh, ETH_ESMIR);
355 } while ((data & ETH_ESMIR_Busy) && count-- > 0);
356
357 if (count == 0) {
358 aprint_error_dev(dev,
359 "mii write for phy %d reg %d busied out (busy)\n",
360 phy, reg);
361 mutex_exit(&csc->sc_mtx);
362 return ETIMEDOUT;
363 }
364
365 bus_space_write_4(csc->sc_iot, csc->sc_ioh, ETH_ESMIR,
366 ETH_ESMIR_WRITE(phy, reg, value));
367
368 count = 10000;
369 do {
370 DELAY(10);
371 data = bus_space_read_4(csc->sc_iot, csc->sc_ioh, ETH_ESMIR);
372 } while ((data & ETH_ESMIR_Busy) && count-- > 0);
373
374 mutex_exit(&csc->sc_mtx);
375
376 if (count == 0) {
377 aprint_error_dev(dev,
378 "mii write for phy %d reg %d timed out\n", phy, reg);
379 return ETIMEDOUT;
380 }
381 #if defined(GTMIIDEBUG)
382 aprint_normal_dev(dev, "mii_write(%d, %d, %#hx)\n", phy, reg, value);
383 #endif
384 return 0;
385 }
386
387 void
388 gfec_mii_statchg(struct ifnet *ifp)
389 {
390 /* struct gfe_softc *sc = ifp->if_softc; */
391 /* do nothing? */
392 }
393
394 /* ARGSUSED */
395 int
396 gfe_match(device_t parent, cfdata_t cf, void *aux)
397 {
398
399 return 1;
400 }
401
402 /* ARGSUSED */
403 void
404 gfe_attach(device_t parent, device_t self, void *aux)
405 {
406 struct marvell_attach_args *mva = aux;
407 struct gfe_softc * const sc = device_private(self);
408 struct ifnet * const ifp = &sc->sc_ec.ec_if;
409 uint32_t sdcr;
410 int phyaddr, error;
411 prop_data_t ea;
412 uint8_t enaddr[6];
413
414 aprint_naive("\n");
415 aprint_normal(": Ethernet Controller\n");
416
417 if (bus_space_subregion(mva->mva_iot, mva->mva_ioh,
418 mva->mva_offset, mva->mva_size, &sc->sc_memh)) {
419 aprint_error_dev(self, "failed to map registers\n");
420 return;
421 }
422 sc->sc_dev = self;
423 sc->sc_memt = mva->mva_iot;
424 sc->sc_dmat = mva->mva_dmat;
425 sc->sc_macno = (mva->mva_offset == ETH_BASE(0)) ? 0 :
426 ((mva->mva_offset == ETH_BASE(1)) ? 1 : 2);
427
428 callout_init(&sc->sc_co, 0);
429
430 phyaddr = gfec_enet_phy(parent, sc->sc_macno);
431
432 ea = prop_dictionary_get(device_properties(sc->sc_dev), "mac-addr");
433 if (ea != NULL) {
434 KASSERT(prop_object_type(ea) == PROP_TYPE_DATA);
435 KASSERT(prop_data_size(ea) == ETHER_ADDR_LEN);
436 memcpy(enaddr, prop_data_data_nocopy(ea), ETHER_ADDR_LEN);
437 }
438
439 sc->sc_pcr = GE_READ(sc, ETH_EPCR);
440 sc->sc_pcxr = GE_READ(sc, ETH_EPCXR);
441 sc->sc_intrmask = GE_READ(sc, ETH_EIMR) | ETH_IR_MIIPhySTC;
442
443 aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(enaddr));
444
445 #if defined(DEBUG)
446 printf("pcr %#x, pcxr %#x\n", sc->sc_pcr, sc->sc_pcxr);
447 #endif
448
449 sc->sc_pcxr &= ~ETH_EPCXR_PRIOrx_Override;
450 if (device_cfdata(self)->cf_flags & 1) {
451 aprint_normal_dev(self, "phy %d (rmii)\n", phyaddr);
452 sc->sc_pcxr |= ETH_EPCXR_RMIIEn;
453 } else {
454 aprint_normal_dev(self, "phy %d (mii)\n", phyaddr);
455 sc->sc_pcxr &= ~ETH_EPCXR_RMIIEn;
456 }
457 if (device_cfdata(self)->cf_flags & 2)
458 sc->sc_flags |= GE_NOFREE;
459 /* Set Max Frame Length is 1536 */
460 sc->sc_pcxr &= ~ETH_EPCXR_MFL_SET(ETH_EPCXR_MFL_MASK);
461 sc->sc_pcxr |= ETH_EPCXR_MFL_SET(ETH_EPCXR_MFL_1536);
462 sc->sc_max_frame_length = 1536;
463
464 if (sc->sc_pcr & ETH_EPCR_EN) {
465 int tries = 1000;
466 /*
467 * Abort transmitter and receiver and wait for them to quiese
468 */
469 GE_WRITE(sc, ETH_ESDCMR, ETH_ESDCMR_AR | ETH_ESDCMR_AT);
470 do {
471 delay(100);
472 if (tries-- <= 0) {
473 aprint_error_dev(self, "Abort TX/RX failed\n");
474 break;
475 }
476 } while (GE_READ(sc, ETH_ESDCMR) &
477 (ETH_ESDCMR_AR | ETH_ESDCMR_AT));
478 }
479
480 sc->sc_pcr &=
481 ~(ETH_EPCR_EN | ETH_EPCR_RBM | ETH_EPCR_PM | ETH_EPCR_PBF);
482
483 #if defined(DEBUG)
484 printf("pcr %#x, pcxr %#x\n", sc->sc_pcr, sc->sc_pcxr);
485 #endif
486
487 /*
488 * Now turn off the GT. If it didn't quiese, too ***ing bad.
489 */
490 GE_WRITE(sc, ETH_EPCR, sc->sc_pcr);
491 GE_WRITE(sc, ETH_EIMR, sc->sc_intrmask);
492 sdcr = GE_READ(sc, ETH_ESDCR);
493 ETH_ESDCR_BSZ_SET(sdcr, ETH_ESDCR_BSZ_4);
494 sdcr |= ETH_ESDCR_RIFB;
495 GE_WRITE(sc, ETH_ESDCR, sdcr);
496
497 sc->sc_mii.mii_ifp = ifp;
498 sc->sc_mii.mii_readreg = gfec_mii_read;
499 sc->sc_mii.mii_writereg = gfec_mii_write;
500 sc->sc_mii.mii_statchg = gfec_mii_statchg;
501
502 sc->sc_ec.ec_mii = &sc->sc_mii;
503 ifmedia_init(&sc->sc_mii.mii_media, 0, ether_mediachange,
504 ether_mediastatus);
505
506 mii_attach(sc->sc_dev, &sc->sc_mii, 0xffffffff, phyaddr,
507 MII_OFFSET_ANY, MIIF_NOISOLATE);
508 if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
509 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
510 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
511 } else {
512 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
513 }
514
515 strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
516 ifp->if_softc = sc;
517 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
518 #if 0
519 ifp->if_flags |= IFF_DEBUG;
520 #endif
521 ifp->if_ioctl = gfe_ifioctl;
522 ifp->if_start = gfe_ifstart;
523 ifp->if_watchdog = gfe_ifwatchdog;
524
525 if (sc->sc_flags & GE_NOFREE) {
526 error = gfe_rx_rxqalloc(sc, GE_RXPRIO_HI);
527 if (!error)
528 error = gfe_rx_rxqalloc(sc, GE_RXPRIO_MEDHI);
529 if (!error)
530 error = gfe_rx_rxqalloc(sc, GE_RXPRIO_MEDLO);
531 if (!error)
532 error = gfe_rx_rxqalloc(sc, GE_RXPRIO_LO);
533 if (!error)
534 error = gfe_tx_txqalloc(sc, GE_TXPRIO_HI);
535 if (!error)
536 error = gfe_hash_alloc(sc);
537 if (error)
538 aprint_error_dev(self,
539 "failed to allocate resources: %d\n", error);
540 }
541
542 if_attach(ifp);
543 ether_ifattach(ifp, enaddr);
544 bpf_attach(ifp, DLT_EN10MB, sizeof(struct ether_header));
545 rnd_attach_source(&sc->sc_rnd_source, device_xname(self), RND_TYPE_NET,
546 RND_FLAG_DEFAULT);
547 marvell_intr_establish(mva->mva_irq, IPL_NET, gfe_intr, sc);
548 }
549
550 int
551 gfe_dmamem_alloc(struct gfe_softc *sc, struct gfe_dmamem *gdm, int maxsegs,
552 size_t size, int flags)
553 {
554 int error = 0;
555 GE_FUNC_ENTER(sc, "gfe_dmamem_alloc");
556
557 KASSERT(gdm->gdm_kva == NULL);
558 gdm->gdm_size = size;
559 gdm->gdm_maxsegs = maxsegs;
560
561 error = bus_dmamem_alloc(sc->sc_dmat, gdm->gdm_size, PAGE_SIZE,
562 gdm->gdm_size, gdm->gdm_segs, gdm->gdm_maxsegs, &gdm->gdm_nsegs,
563 BUS_DMA_NOWAIT);
564 if (error)
565 goto fail;
566
567 error = bus_dmamem_map(sc->sc_dmat, gdm->gdm_segs, gdm->gdm_nsegs,
568 gdm->gdm_size, &gdm->gdm_kva, flags | BUS_DMA_NOWAIT);
569 if (error)
570 goto fail;
571
572 error = bus_dmamap_create(sc->sc_dmat, gdm->gdm_size, gdm->gdm_nsegs,
573 gdm->gdm_size, 0, BUS_DMA_ALLOCNOW|BUS_DMA_NOWAIT, &gdm->gdm_map);
574 if (error)
575 goto fail;
576
577 error = bus_dmamap_load(sc->sc_dmat, gdm->gdm_map, gdm->gdm_kva,
578 gdm->gdm_size, NULL, BUS_DMA_NOWAIT);
579 if (error)
580 goto fail;
581
582 /* invalidate from cache */
583 bus_dmamap_sync(sc->sc_dmat, gdm->gdm_map, 0, gdm->gdm_size,
584 BUS_DMASYNC_PREREAD);
585 fail:
586 if (error) {
587 gfe_dmamem_free(sc, gdm);
588 GE_DPRINTF(sc, (":err=%d", error));
589 }
590 GE_DPRINTF(sc, (":kva=%p/%#x,map=%p,nsegs=%d,pa=%x/%x",
591 gdm->gdm_kva, gdm->gdm_size, gdm->gdm_map, gdm->gdm_map->dm_nsegs,
592 gdm->gdm_map->dm_segs->ds_addr, gdm->gdm_map->dm_segs->ds_len));
593 GE_FUNC_EXIT(sc, "");
594 return error;
595 }
596
597 void
598 gfe_dmamem_free(struct gfe_softc *sc, struct gfe_dmamem *gdm)
599 {
600 GE_FUNC_ENTER(sc, "gfe_dmamem_free");
601 if (gdm->gdm_map)
602 bus_dmamap_destroy(sc->sc_dmat, gdm->gdm_map);
603 if (gdm->gdm_kva)
604 bus_dmamem_unmap(sc->sc_dmat, gdm->gdm_kva, gdm->gdm_size);
605 if (gdm->gdm_nsegs > 0)
606 bus_dmamem_free(sc->sc_dmat, gdm->gdm_segs, gdm->gdm_nsegs);
607 gdm->gdm_map = NULL;
608 gdm->gdm_kva = NULL;
609 gdm->gdm_nsegs = 0;
610 GE_FUNC_EXIT(sc, "");
611 }
612
613 int
614 gfe_ifioctl(struct ifnet *ifp, u_long cmd, void *data)
615 {
616 struct gfe_softc * const sc = ifp->if_softc;
617 struct ifreq *ifr = (struct ifreq *) data;
618 struct ifaddr *ifa = (struct ifaddr *) data;
619 int s, error = 0;
620
621 GE_FUNC_ENTER(sc, "gfe_ifioctl");
622 s = splnet();
623
624 switch (cmd) {
625 case SIOCINITIFADDR:
626 ifp->if_flags |= IFF_UP;
627 error = gfe_whack(sc, GE_WHACK_START);
628 switch (ifa->ifa_addr->sa_family) {
629 #ifdef INET
630 case AF_INET:
631 if (error == 0)
632 arp_ifinit(ifp, ifa);
633 break;
634 #endif
635 default:
636 break;
637 }
638 break;
639
640 case SIOCSIFFLAGS:
641 if ((error = ifioctl_common(ifp, cmd, data)) != 0)
642 break;
643 /* XXX re-use ether_ioctl() */
644 switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
645 case IFF_UP|IFF_RUNNING:/* active->active, update */
646 error = gfe_whack(sc, GE_WHACK_CHANGE);
647 break;
648 case IFF_RUNNING: /* not up, so we stop */
649 error = gfe_whack(sc, GE_WHACK_STOP);
650 break;
651 case IFF_UP: /* not running, so we start */
652 error = gfe_whack(sc, GE_WHACK_START);
653 break;
654 case 0: /* idle->idle: do nothing */
655 break;
656 }
657 break;
658
659 case SIOCSIFMEDIA:
660 case SIOCGIFMEDIA:
661 case SIOCADDMULTI:
662 case SIOCDELMULTI:
663 if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
664 if (ifp->if_flags & IFF_RUNNING)
665 error = gfe_whack(sc, GE_WHACK_CHANGE);
666 else
667 error = 0;
668 }
669 break;
670
671 case SIOCSIFMTU:
672 if (ifr->ifr_mtu > ETHERMTU || ifr->ifr_mtu < ETHERMIN) {
673 error = EINVAL;
674 break;
675 }
676 if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
677 error = 0;
678 break;
679
680 default:
681 error = ether_ioctl(ifp, cmd, data);
682 break;
683 }
684 splx(s);
685 GE_FUNC_EXIT(sc, "");
686 return error;
687 }
688
689 void
690 gfe_ifstart(struct ifnet *ifp)
691 {
692 struct gfe_softc * const sc = ifp->if_softc;
693 struct mbuf *m;
694
695 GE_FUNC_ENTER(sc, "gfe_ifstart");
696
697 if ((ifp->if_flags & IFF_RUNNING) == 0) {
698 GE_FUNC_EXIT(sc, "$");
699 return;
700 }
701
702 for (;;) {
703 IF_DEQUEUE(&ifp->if_snd, m);
704 if (m == NULL) {
705 ifp->if_flags &= ~IFF_OACTIVE;
706 GE_FUNC_EXIT(sc, "");
707 return;
708 }
709
710 /*
711 * No space in the pending queue? try later.
712 */
713 if (IF_QFULL(&sc->sc_txq[GE_TXPRIO_HI].txq_pendq))
714 break;
715
716 /*
717 * Try to enqueue a mbuf to the device. If that fails, we
718 * can always try to map the next mbuf.
719 */
720 IF_ENQUEUE(&sc->sc_txq[GE_TXPRIO_HI].txq_pendq, m);
721 GE_DPRINTF(sc, (">"));
722 #ifndef GE_NOTX
723 (void) gfe_tx_enqueue(sc, GE_TXPRIO_HI);
724 #endif
725 }
726
727 /*
728 * Attempt to queue the mbuf for send failed.
729 */
730 IF_PREPEND(&ifp->if_snd, m);
731 ifp->if_flags |= IFF_OACTIVE;
732 GE_FUNC_EXIT(sc, "%%");
733 }
734
735 void
736 gfe_ifwatchdog(struct ifnet *ifp)
737 {
738 struct gfe_softc * const sc = ifp->if_softc;
739 struct gfe_txqueue * const txq = &sc->sc_txq[GE_TXPRIO_HI];
740
741 GE_FUNC_ENTER(sc, "gfe_ifwatchdog");
742 aprint_error_dev(sc->sc_dev, "device timeout");
743 if (ifp->if_flags & IFF_RUNNING) {
744 uint32_t curtxdnum;
745
746 curtxdnum = (GE_READ(sc, txq->txq_ectdp) -
747 txq->txq_desc_busaddr) / sizeof(txq->txq_descs[0]);
748 GE_TXDPOSTSYNC(sc, txq, txq->txq_fi);
749 GE_TXDPOSTSYNC(sc, txq, curtxdnum);
750 aprint_error(" (fi=%d(%#x),lo=%d,cur=%d(%#x),icm=%#x) ",
751 txq->txq_fi, txq->txq_descs[txq->txq_fi].ed_cmdsts,
752 txq->txq_lo, curtxdnum, txq->txq_descs[curtxdnum].ed_cmdsts,
753 GE_READ(sc, ETH_EICR));
754 GE_TXDPRESYNC(sc, txq, txq->txq_fi);
755 GE_TXDPRESYNC(sc, txq, curtxdnum);
756 }
757 aprint_error("\n");
758 ifp->if_oerrors++;
759 (void) gfe_whack(sc, GE_WHACK_RESTART);
760 GE_FUNC_EXIT(sc, "");
761 }
762
763 int
764 gfe_rx_rxqalloc(struct gfe_softc *sc, enum gfe_rxprio rxprio)
765 {
766 struct gfe_rxqueue * const rxq = &sc->sc_rxq[rxprio];
767 int error;
768
769 GE_FUNC_ENTER(sc, "gfe_rx_rxqalloc");
770 GE_DPRINTF(sc, ("(%d)", rxprio));
771
772 error = gfe_dmamem_alloc(sc, &rxq->rxq_desc_mem, 1,
773 GE_RXDESC_MEMSIZE, BUS_DMA_NOCACHE);
774 if (error) {
775 GE_FUNC_EXIT(sc, "!!");
776 return error;
777 }
778
779 error = gfe_dmamem_alloc(sc, &rxq->rxq_buf_mem, GE_RXBUF_NSEGS,
780 GE_RXBUF_MEMSIZE, 0);
781 if (error) {
782 GE_FUNC_EXIT(sc, "!!!");
783 return error;
784 }
785 GE_FUNC_EXIT(sc, "");
786 return error;
787 }
788
789 int
790 gfe_rx_rxqinit(struct gfe_softc *sc, enum gfe_rxprio rxprio)
791 {
792 struct gfe_rxqueue * const rxq = &sc->sc_rxq[rxprio];
793 volatile struct gt_eth_desc *rxd;
794 const bus_dma_segment_t *ds;
795 int idx;
796 bus_addr_t nxtaddr;
797 bus_size_t boff;
798
799 GE_FUNC_ENTER(sc, "gfe_rx_rxqinit");
800 GE_DPRINTF(sc, ("(%d)", rxprio));
801
802 if ((sc->sc_flags & GE_NOFREE) == 0) {
803 int error = gfe_rx_rxqalloc(sc, rxprio);
804 if (error) {
805 GE_FUNC_EXIT(sc, "!");
806 return error;
807 }
808 } else {
809 KASSERT(rxq->rxq_desc_mem.gdm_kva != NULL);
810 KASSERT(rxq->rxq_buf_mem.gdm_kva != NULL);
811 }
812
813 memset(rxq->rxq_desc_mem.gdm_kva, 0, GE_RXDESC_MEMSIZE);
814
815 rxq->rxq_descs =
816 (volatile struct gt_eth_desc *) rxq->rxq_desc_mem.gdm_kva;
817 rxq->rxq_desc_busaddr = rxq->rxq_desc_mem.gdm_map->dm_segs[0].ds_addr;
818 rxq->rxq_bufs = (struct gfe_rxbuf *) rxq->rxq_buf_mem.gdm_kva;
819 rxq->rxq_fi = 0;
820 rxq->rxq_active = GE_RXDESC_MAX;
821 boff = 0;
822 ds = rxq->rxq_buf_mem.gdm_map->dm_segs;
823 nxtaddr = rxq->rxq_desc_busaddr + sizeof(*rxd);
824 for (idx = 0, rxd = rxq->rxq_descs; idx < GE_RXDESC_MAX;
825 idx++, rxd++, nxtaddr += sizeof(*rxd)) {
826 rxd->ed_lencnt = htogt32(GE_RXBUF_SIZE << 16);
827 rxd->ed_cmdsts = htogt32(RX_CMD_F|RX_CMD_L|RX_CMD_O|RX_CMD_EI);
828 rxd->ed_bufptr = htogt32(ds->ds_addr + boff);
829 /*
830 * update the nxtptr to point to the next txd.
831 */
832 if (idx == GE_RXDESC_MAX - 1)
833 nxtaddr = rxq->rxq_desc_busaddr;
834 rxd->ed_nxtptr = htogt32(nxtaddr);
835 boff += GE_RXBUF_SIZE;
836 if (boff == ds->ds_len) {
837 ds++;
838 boff = 0;
839 }
840 }
841 bus_dmamap_sync(sc->sc_dmat, rxq->rxq_desc_mem.gdm_map, 0,
842 rxq->rxq_desc_mem.gdm_map->dm_mapsize,
843 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
844 bus_dmamap_sync(sc->sc_dmat, rxq->rxq_buf_mem.gdm_map, 0,
845 rxq->rxq_buf_mem.gdm_map->dm_mapsize,
846 BUS_DMASYNC_PREREAD);
847
848 rxq->rxq_intrbits = ETH_IR_RxBuffer|ETH_IR_RxError;
849 switch (rxprio) {
850 case GE_RXPRIO_HI:
851 rxq->rxq_intrbits |= ETH_IR_RxBuffer_3|ETH_IR_RxError_3;
852 rxq->rxq_efrdp = ETH_EFRDP3;
853 rxq->rxq_ecrdp = ETH_ECRDP3;
854 break;
855 case GE_RXPRIO_MEDHI:
856 rxq->rxq_intrbits |= ETH_IR_RxBuffer_2|ETH_IR_RxError_2;
857 rxq->rxq_efrdp = ETH_EFRDP2;
858 rxq->rxq_ecrdp = ETH_ECRDP2;
859 break;
860 case GE_RXPRIO_MEDLO:
861 rxq->rxq_intrbits |= ETH_IR_RxBuffer_1|ETH_IR_RxError_1;
862 rxq->rxq_efrdp = ETH_EFRDP1;
863 rxq->rxq_ecrdp = ETH_ECRDP1;
864 break;
865 case GE_RXPRIO_LO:
866 rxq->rxq_intrbits |= ETH_IR_RxBuffer_0|ETH_IR_RxError_0;
867 rxq->rxq_efrdp = ETH_EFRDP0;
868 rxq->rxq_ecrdp = ETH_ECRDP0;
869 break;
870 }
871 GE_FUNC_EXIT(sc, "");
872 return 0;
873 }
874
875 void
876 gfe_rx_get(struct gfe_softc *sc, enum gfe_rxprio rxprio)
877 {
878 struct ifnet * const ifp = &sc->sc_ec.ec_if;
879 struct gfe_rxqueue * const rxq = &sc->sc_rxq[rxprio];
880 struct mbuf *m = rxq->rxq_curpkt;
881
882 GE_FUNC_ENTER(sc, "gfe_rx_get");
883 GE_DPRINTF(sc, ("(%d)", rxprio));
884
885 while (rxq->rxq_active > 0) {
886 volatile struct gt_eth_desc *rxd = &rxq->rxq_descs[rxq->rxq_fi];
887 struct gfe_rxbuf *rxb = &rxq->rxq_bufs[rxq->rxq_fi];
888 const struct ether_header *eh;
889 unsigned int cmdsts;
890 size_t buflen;
891
892 GE_RXDPOSTSYNC(sc, rxq, rxq->rxq_fi);
893 cmdsts = gt32toh(rxd->ed_cmdsts);
894 GE_DPRINTF(sc, (":%d=%#x", rxq->rxq_fi, cmdsts));
895 rxq->rxq_cmdsts = cmdsts;
896 /*
897 * Sometimes the GE "forgets" to reset the ownership bit.
898 * But if the length has been rewritten, the packet is ours
899 * so pretend the O bit is set.
900 */
901 buflen = gt32toh(rxd->ed_lencnt) & 0xffff;
902 if ((cmdsts & RX_CMD_O) && buflen == 0) {
903 GE_RXDPRESYNC(sc, rxq, rxq->rxq_fi);
904 break;
905 }
906
907 /*
908 * If this is not a single buffer packet with no errors
909 * or for some reason it's bigger than our frame size,
910 * ignore it and go to the next packet.
911 */
912 if ((cmdsts & (RX_CMD_F|RX_CMD_L|RX_STS_ES)) !=
913 (RX_CMD_F|RX_CMD_L) ||
914 buflen > sc->sc_max_frame_length) {
915 GE_DPRINTF(sc, ("!"));
916 --rxq->rxq_active;
917 ifp->if_ipackets++;
918 ifp->if_ierrors++;
919 goto give_it_back;
920 }
921
922 /* CRC is included with the packet; trim it off. */
923 buflen -= ETHER_CRC_LEN;
924
925 if (m == NULL) {
926 MGETHDR(m, M_DONTWAIT, MT_DATA);
927 if (m == NULL) {
928 GE_DPRINTF(sc, ("?"));
929 break;
930 }
931 }
932 if ((m->m_flags & M_EXT) == 0 && buflen > MHLEN - 2) {
933 MCLGET(m, M_DONTWAIT);
934 if ((m->m_flags & M_EXT) == 0) {
935 GE_DPRINTF(sc, ("?"));
936 break;
937 }
938 }
939 m->m_data += 2;
940 m->m_len = 0;
941 m->m_pkthdr.len = 0;
942 m_set_rcvif(m, ifp);
943 rxq->rxq_cmdsts = cmdsts;
944 --rxq->rxq_active;
945
946 bus_dmamap_sync(sc->sc_dmat, rxq->rxq_buf_mem.gdm_map,
947 rxq->rxq_fi * sizeof(*rxb), buflen, BUS_DMASYNC_POSTREAD);
948
949 KASSERT(m->m_len == 0 && m->m_pkthdr.len == 0);
950 memcpy(m->m_data + m->m_len, rxb->rxb_data, buflen);
951 m->m_len = buflen;
952 m->m_pkthdr.len = buflen;
953
954 eh = (const struct ether_header *) m->m_data;
955 if ((ifp->if_flags & IFF_PROMISC) ||
956 (rxq->rxq_cmdsts & RX_STS_M) == 0 ||
957 (rxq->rxq_cmdsts & RX_STS_HE) ||
958 (eh->ether_dhost[0] & 1) != 0 ||
959 memcmp(eh->ether_dhost, CLLADDR(ifp->if_sadl),
960 ETHER_ADDR_LEN) == 0) {
961 if_percpuq_enqueue(ifp->if_percpuq, m);
962 m = NULL;
963 GE_DPRINTF(sc, (">"));
964 } else {
965 m->m_len = 0;
966 m->m_pkthdr.len = 0;
967 GE_DPRINTF(sc, ("+"));
968 }
969 rxq->rxq_cmdsts = 0;
970
971 give_it_back:
972 rxd->ed_lencnt &= ~0xffff; /* zero out length */
973 rxd->ed_cmdsts = htogt32(RX_CMD_F|RX_CMD_L|RX_CMD_O|RX_CMD_EI);
974 #if 0
975 GE_DPRINTF(sc, ("([%d]->%08lx.%08lx.%08lx.%08lx)",
976 rxq->rxq_fi,
977 ((unsigned long *)rxd)[0], ((unsigned long *)rxd)[1],
978 ((unsigned long *)rxd)[2], ((unsigned long *)rxd)[3]));
979 #endif
980 GE_RXDPRESYNC(sc, rxq, rxq->rxq_fi);
981 if (++rxq->rxq_fi == GE_RXDESC_MAX)
982 rxq->rxq_fi = 0;
983 rxq->rxq_active++;
984 }
985 rxq->rxq_curpkt = m;
986 GE_FUNC_EXIT(sc, "");
987 }
988
989 uint32_t
990 gfe_rx_process(struct gfe_softc *sc, uint32_t cause, uint32_t intrmask)
991 {
992 struct ifnet * const ifp = &sc->sc_ec.ec_if;
993 struct gfe_rxqueue *rxq;
994 uint32_t rxbits;
995 #define RXPRIO_DECODER 0xffffaa50
996 GE_FUNC_ENTER(sc, "gfe_rx_process");
997
998 rxbits = ETH_IR_RxBuffer_GET(cause);
999 while (rxbits) {
1000 enum gfe_rxprio rxprio = (RXPRIO_DECODER >> (rxbits * 2)) & 3;
1001 GE_DPRINTF(sc, ("%1x", rxbits));
1002 rxbits &= ~(1 << rxprio);
1003 gfe_rx_get(sc, rxprio);
1004 }
1005
1006 rxbits = ETH_IR_RxError_GET(cause);
1007 while (rxbits) {
1008 enum gfe_rxprio rxprio = (RXPRIO_DECODER >> (rxbits * 2)) & 3;
1009 uint32_t masks[(GE_RXDESC_MAX + 31) / 32];
1010 int idx;
1011 rxbits &= ~(1 << rxprio);
1012 rxq = &sc->sc_rxq[rxprio];
1013 sc->sc_idlemask |= (rxq->rxq_intrbits & ETH_IR_RxBits);
1014 intrmask &= ~(rxq->rxq_intrbits & ETH_IR_RxBits);
1015 if ((sc->sc_tickflags & GE_TICK_RX_RESTART) == 0) {
1016 sc->sc_tickflags |= GE_TICK_RX_RESTART;
1017 callout_reset(&sc->sc_co, 1, gfe_tick, sc);
1018 }
1019 ifp->if_ierrors++;
1020 GE_DPRINTF(sc, ("%s: rx queue %d filled at %u\n",
1021 device_xname(sc->sc_dev), rxprio, rxq->rxq_fi));
1022 memset(masks, 0, sizeof(masks));
1023 bus_dmamap_sync(sc->sc_dmat, rxq->rxq_desc_mem.gdm_map,
1024 0, rxq->rxq_desc_mem.gdm_size,
1025 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1026 for (idx = 0; idx < GE_RXDESC_MAX; idx++) {
1027 volatile struct gt_eth_desc *rxd = &rxq->rxq_descs[idx];
1028
1029 if (RX_CMD_O & gt32toh(rxd->ed_cmdsts))
1030 masks[idx/32] |= 1 << (idx & 31);
1031 }
1032 bus_dmamap_sync(sc->sc_dmat, rxq->rxq_desc_mem.gdm_map,
1033 0, rxq->rxq_desc_mem.gdm_size,
1034 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1035 #if defined(DEBUG)
1036 printf("%s: rx queue %d filled at %u=%#x(%#x/%#x)\n",
1037 device_xname(sc->sc_dev), rxprio, rxq->rxq_fi,
1038 rxq->rxq_cmdsts, masks[0], masks[1]);
1039 #endif
1040 }
1041 if ((intrmask & ETH_IR_RxBits) == 0)
1042 intrmask &= ~(ETH_IR_RxBuffer|ETH_IR_RxError);
1043
1044 GE_FUNC_EXIT(sc, "");
1045 return intrmask;
1046 }
1047
1048 int
1049 gfe_rx_prime(struct gfe_softc *sc)
1050 {
1051 struct gfe_rxqueue *rxq;
1052 int error;
1053
1054 GE_FUNC_ENTER(sc, "gfe_rx_prime");
1055
1056 error = gfe_rx_rxqinit(sc, GE_RXPRIO_HI);
1057 if (error)
1058 goto bail;
1059 rxq = &sc->sc_rxq[GE_RXPRIO_HI];
1060 if ((sc->sc_flags & GE_RXACTIVE) == 0) {
1061 GE_WRITE(sc, ETH_EFRDP3, rxq->rxq_desc_busaddr);
1062 GE_WRITE(sc, ETH_ECRDP3, rxq->rxq_desc_busaddr);
1063 }
1064 sc->sc_intrmask |= rxq->rxq_intrbits;
1065
1066 error = gfe_rx_rxqinit(sc, GE_RXPRIO_MEDHI);
1067 if (error)
1068 goto bail;
1069 if ((sc->sc_flags & GE_RXACTIVE) == 0) {
1070 rxq = &sc->sc_rxq[GE_RXPRIO_MEDHI];
1071 GE_WRITE(sc, ETH_EFRDP2, rxq->rxq_desc_busaddr);
1072 GE_WRITE(sc, ETH_ECRDP2, rxq->rxq_desc_busaddr);
1073 sc->sc_intrmask |= rxq->rxq_intrbits;
1074 }
1075
1076 error = gfe_rx_rxqinit(sc, GE_RXPRIO_MEDLO);
1077 if (error)
1078 goto bail;
1079 if ((sc->sc_flags & GE_RXACTIVE) == 0) {
1080 rxq = &sc->sc_rxq[GE_RXPRIO_MEDLO];
1081 GE_WRITE(sc, ETH_EFRDP1, rxq->rxq_desc_busaddr);
1082 GE_WRITE(sc, ETH_ECRDP1, rxq->rxq_desc_busaddr);
1083 sc->sc_intrmask |= rxq->rxq_intrbits;
1084 }
1085
1086 error = gfe_rx_rxqinit(sc, GE_RXPRIO_LO);
1087 if (error)
1088 goto bail;
1089 if ((sc->sc_flags & GE_RXACTIVE) == 0) {
1090 rxq = &sc->sc_rxq[GE_RXPRIO_LO];
1091 GE_WRITE(sc, ETH_EFRDP0, rxq->rxq_desc_busaddr);
1092 GE_WRITE(sc, ETH_ECRDP0, rxq->rxq_desc_busaddr);
1093 sc->sc_intrmask |= rxq->rxq_intrbits;
1094 }
1095
1096 bail:
1097 GE_FUNC_EXIT(sc, "");
1098 return error;
1099 }
1100
1101 void
1102 gfe_rx_cleanup(struct gfe_softc *sc, enum gfe_rxprio rxprio)
1103 {
1104 struct gfe_rxqueue *rxq = &sc->sc_rxq[rxprio];
1105 GE_FUNC_ENTER(sc, "gfe_rx_cleanup");
1106 if (rxq == NULL) {
1107 GE_FUNC_EXIT(sc, "");
1108 return;
1109 }
1110
1111 if (rxq->rxq_curpkt)
1112 m_freem(rxq->rxq_curpkt);
1113 if ((sc->sc_flags & GE_NOFREE) == 0) {
1114 gfe_dmamem_free(sc, &rxq->rxq_desc_mem);
1115 gfe_dmamem_free(sc, &rxq->rxq_buf_mem);
1116 }
1117 GE_FUNC_EXIT(sc, "");
1118 }
1119
1120 void
1121 gfe_rx_stop(struct gfe_softc *sc, enum gfe_whack_op op)
1122 {
1123 GE_FUNC_ENTER(sc, "gfe_rx_stop");
1124 sc->sc_flags &= ~GE_RXACTIVE;
1125 sc->sc_idlemask &= ~(ETH_IR_RxBits|ETH_IR_RxBuffer|ETH_IR_RxError);
1126 sc->sc_intrmask &= ~(ETH_IR_RxBits|ETH_IR_RxBuffer|ETH_IR_RxError);
1127 GE_WRITE(sc, ETH_EIMR, sc->sc_intrmask);
1128 GE_WRITE(sc, ETH_ESDCMR, ETH_ESDCMR_AR);
1129 do {
1130 delay(10);
1131 } while (GE_READ(sc, ETH_ESDCMR) & ETH_ESDCMR_AR);
1132 gfe_rx_cleanup(sc, GE_RXPRIO_HI);
1133 gfe_rx_cleanup(sc, GE_RXPRIO_MEDHI);
1134 gfe_rx_cleanup(sc, GE_RXPRIO_MEDLO);
1135 gfe_rx_cleanup(sc, GE_RXPRIO_LO);
1136 GE_FUNC_EXIT(sc, "");
1137 }
1138
1139 void
1140 gfe_tick(void *arg)
1141 {
1142 struct gfe_softc * const sc = arg;
1143 uint32_t intrmask;
1144 unsigned int tickflags;
1145 int s;
1146
1147 GE_FUNC_ENTER(sc, "gfe_tick");
1148
1149 s = splnet();
1150
1151 tickflags = sc->sc_tickflags;
1152 sc->sc_tickflags = 0;
1153 intrmask = sc->sc_intrmask;
1154 if (tickflags & GE_TICK_TX_IFSTART)
1155 gfe_ifstart(&sc->sc_ec.ec_if);
1156 if (tickflags & GE_TICK_RX_RESTART) {
1157 intrmask |= sc->sc_idlemask;
1158 if (sc->sc_idlemask & (ETH_IR_RxBuffer_3|ETH_IR_RxError_3)) {
1159 struct gfe_rxqueue *rxq = &sc->sc_rxq[GE_RXPRIO_HI];
1160 rxq->rxq_fi = 0;
1161 GE_WRITE(sc, ETH_EFRDP3, rxq->rxq_desc_busaddr);
1162 GE_WRITE(sc, ETH_ECRDP3, rxq->rxq_desc_busaddr);
1163 }
1164 if (sc->sc_idlemask & (ETH_IR_RxBuffer_2|ETH_IR_RxError_2)) {
1165 struct gfe_rxqueue *rxq = &sc->sc_rxq[GE_RXPRIO_MEDHI];
1166 rxq->rxq_fi = 0;
1167 GE_WRITE(sc, ETH_EFRDP2, rxq->rxq_desc_busaddr);
1168 GE_WRITE(sc, ETH_ECRDP2, rxq->rxq_desc_busaddr);
1169 }
1170 if (sc->sc_idlemask & (ETH_IR_RxBuffer_1|ETH_IR_RxError_1)) {
1171 struct gfe_rxqueue *rxq = &sc->sc_rxq[GE_RXPRIO_MEDLO];
1172 rxq->rxq_fi = 0;
1173 GE_WRITE(sc, ETH_EFRDP1, rxq->rxq_desc_busaddr);
1174 GE_WRITE(sc, ETH_ECRDP1, rxq->rxq_desc_busaddr);
1175 }
1176 if (sc->sc_idlemask & (ETH_IR_RxBuffer_0|ETH_IR_RxError_0)) {
1177 struct gfe_rxqueue *rxq = &sc->sc_rxq[GE_RXPRIO_LO];
1178 rxq->rxq_fi = 0;
1179 GE_WRITE(sc, ETH_EFRDP0, rxq->rxq_desc_busaddr);
1180 GE_WRITE(sc, ETH_ECRDP0, rxq->rxq_desc_busaddr);
1181 }
1182 sc->sc_idlemask = 0;
1183 }
1184 if (intrmask != sc->sc_intrmask) {
1185 sc->sc_intrmask = intrmask;
1186 GE_WRITE(sc, ETH_EIMR, sc->sc_intrmask);
1187 }
1188 gfe_intr(sc);
1189 splx(s);
1190
1191 GE_FUNC_EXIT(sc, "");
1192 }
1193
1194 int
1195 gfe_tx_enqueue(struct gfe_softc *sc, enum gfe_txprio txprio)
1196 {
1197 const int dcache_line_size = curcpu()->ci_ci.dcache_line_size;
1198 struct ifnet * const ifp = &sc->sc_ec.ec_if;
1199 struct gfe_txqueue * const txq = &sc->sc_txq[txprio];
1200 volatile struct gt_eth_desc * const txd = &txq->txq_descs[txq->txq_lo];
1201 uint32_t intrmask = sc->sc_intrmask;
1202 size_t buflen;
1203 struct mbuf *m;
1204
1205 GE_FUNC_ENTER(sc, "gfe_tx_enqueue");
1206
1207 /*
1208 * Anything in the pending queue to enqueue? if not, punt. Likewise
1209 * if the txq is not yet created.
1210 * otherwise grab its dmamap.
1211 */
1212 if (txq == NULL || (m = txq->txq_pendq.ifq_head) == NULL) {
1213 GE_FUNC_EXIT(sc, "-");
1214 return 0;
1215 }
1216
1217 /*
1218 * Have we [over]consumed our limit of descriptors?
1219 * Do we have enough free descriptors?
1220 */
1221 if (GE_TXDESC_MAX == txq->txq_nactive + 2) {
1222 volatile struct gt_eth_desc * const txd2 = &txq->txq_descs[txq->txq_fi];
1223 uint32_t cmdsts;
1224 size_t pktlen;
1225 GE_TXDPOSTSYNC(sc, txq, txq->txq_fi);
1226 cmdsts = gt32toh(txd2->ed_cmdsts);
1227 if (cmdsts & TX_CMD_O) {
1228 int nextin;
1229 /*
1230 * Sometime the Discovery forgets to update the
1231 * last descriptor. See if we own the descriptor
1232 * after it (since we know we've turned that to
1233 * the discovery and if we owned it, the Discovery
1234 * gave it back). If we do, we know the Discovery
1235 * gave back this one but forgot to mark it as ours.
1236 */
1237 nextin = txq->txq_fi + 1;
1238 if (nextin == GE_TXDESC_MAX)
1239 nextin = 0;
1240 GE_TXDPOSTSYNC(sc, txq, nextin);
1241 if (gt32toh(txq->txq_descs[nextin].ed_cmdsts) & TX_CMD_O) {
1242 GE_TXDPRESYNC(sc, txq, txq->txq_fi);
1243 GE_TXDPRESYNC(sc, txq, nextin);
1244 GE_FUNC_EXIT(sc, "@");
1245 return 0;
1246 }
1247 #ifdef DEBUG
1248 printf("%s: txenqueue: transmitter resynced at %d\n",
1249 device_xname(sc->sc_dev), txq->txq_fi);
1250 #endif
1251 }
1252 if (++txq->txq_fi == GE_TXDESC_MAX)
1253 txq->txq_fi = 0;
1254 txq->txq_inptr = gt32toh(txd2->ed_bufptr) - txq->txq_buf_busaddr;
1255 pktlen = (gt32toh(txd2->ed_lencnt) >> 16) & 0xffff;
1256 txq->txq_inptr += roundup(pktlen, dcache_line_size);
1257 txq->txq_nactive--;
1258
1259 /* statistics */
1260 ifp->if_opackets++;
1261 if (cmdsts & TX_STS_ES)
1262 ifp->if_oerrors++;
1263 GE_DPRINTF(sc, ("%%"));
1264 }
1265
1266 buflen = roundup(m->m_pkthdr.len, dcache_line_size);
1267
1268 /*
1269 * If this packet would wrap around the end of the buffer, reset back
1270 * to the beginning.
1271 */
1272 if (txq->txq_outptr + buflen > GE_TXBUF_SIZE) {
1273 txq->txq_ei_gapcount += GE_TXBUF_SIZE - txq->txq_outptr;
1274 txq->txq_outptr = 0;
1275 }
1276
1277 /*
1278 * Make sure the output packet doesn't run over the beginning of
1279 * what we've already given the GT.
1280 */
1281 if (txq->txq_nactive > 0 && txq->txq_outptr <= txq->txq_inptr &&
1282 txq->txq_outptr + buflen > txq->txq_inptr) {
1283 intrmask |= txq->txq_intrbits &
1284 (ETH_IR_TxBufferHigh|ETH_IR_TxBufferLow);
1285 if (sc->sc_intrmask != intrmask) {
1286 sc->sc_intrmask = intrmask;
1287 GE_WRITE(sc, ETH_EIMR, sc->sc_intrmask);
1288 }
1289 GE_FUNC_EXIT(sc, "#");
1290 return 0;
1291 }
1292
1293 /*
1294 * The end-of-list descriptor we put on last time is the starting point
1295 * for this packet. The GT is supposed to terminate list processing on
1296 * a NULL nxtptr but that currently is broken so a CPU-owned descriptor
1297 * must terminate the list.
1298 */
1299 intrmask = sc->sc_intrmask;
1300
1301 m_copydata(m, 0, m->m_pkthdr.len,
1302 (char *)txq->txq_buf_mem.gdm_kva + (int)txq->txq_outptr);
1303 bus_dmamap_sync(sc->sc_dmat, txq->txq_buf_mem.gdm_map,
1304 txq->txq_outptr, buflen, BUS_DMASYNC_PREWRITE);
1305 txd->ed_bufptr = htogt32(txq->txq_buf_busaddr + txq->txq_outptr);
1306 txd->ed_lencnt = htogt32(m->m_pkthdr.len << 16);
1307 GE_TXDPRESYNC(sc, txq, txq->txq_lo);
1308
1309 /*
1310 * Request a buffer interrupt every 2/3 of the way thru the transmit
1311 * buffer.
1312 */
1313 txq->txq_ei_gapcount += buflen;
1314 if (txq->txq_ei_gapcount > 2 * GE_TXBUF_SIZE / 3) {
1315 txd->ed_cmdsts = htogt32(TX_CMD_FIRST|TX_CMD_LAST|TX_CMD_EI);
1316 txq->txq_ei_gapcount = 0;
1317 } else {
1318 txd->ed_cmdsts = htogt32(TX_CMD_FIRST|TX_CMD_LAST);
1319 }
1320 #if 0
1321 GE_DPRINTF(sc, ("([%d]->%08lx.%08lx.%08lx.%08lx)", txq->txq_lo,
1322 ((unsigned long *)txd)[0], ((unsigned long *)txd)[1],
1323 ((unsigned long *)txd)[2], ((unsigned long *)txd)[3]));
1324 #endif
1325 GE_TXDPRESYNC(sc, txq, txq->txq_lo);
1326
1327 txq->txq_outptr += buflen;
1328 /*
1329 * Tell the SDMA engine to "Fetch!"
1330 */
1331 GE_WRITE(sc, ETH_ESDCMR,
1332 txq->txq_esdcmrbits & (ETH_ESDCMR_TXDH|ETH_ESDCMR_TXDL));
1333
1334 GE_DPRINTF(sc, ("(%d)", txq->txq_lo));
1335
1336 /*
1337 * Update the last out appropriately.
1338 */
1339 txq->txq_nactive++;
1340 if (++txq->txq_lo == GE_TXDESC_MAX)
1341 txq->txq_lo = 0;
1342
1343 /*
1344 * Move mbuf from the pending queue to the snd queue.
1345 */
1346 IF_DEQUEUE(&txq->txq_pendq, m);
1347 bpf_mtap(ifp, m, BPF_D_OUT);
1348 m_freem(m);
1349 ifp->if_flags &= ~IFF_OACTIVE;
1350
1351 /*
1352 * Since we have put an item into the packet queue, we now want
1353 * an interrupt when the transmit queue finishes processing the
1354 * list. But only update the mask if needs changing.
1355 */
1356 intrmask |= txq->txq_intrbits & (ETH_IR_TxEndHigh|ETH_IR_TxEndLow);
1357 if (sc->sc_intrmask != intrmask) {
1358 sc->sc_intrmask = intrmask;
1359 GE_WRITE(sc, ETH_EIMR, sc->sc_intrmask);
1360 }
1361 if (ifp->if_timer == 0)
1362 ifp->if_timer = 5;
1363 GE_FUNC_EXIT(sc, "*");
1364 return 1;
1365 }
1366
1367 uint32_t
1368 gfe_tx_done(struct gfe_softc *sc, enum gfe_txprio txprio, uint32_t intrmask)
1369 {
1370 struct gfe_txqueue * const txq = &sc->sc_txq[txprio];
1371 struct ifnet * const ifp = &sc->sc_ec.ec_if;
1372
1373 GE_FUNC_ENTER(sc, "gfe_tx_done");
1374
1375 if (txq == NULL) {
1376 GE_FUNC_EXIT(sc, "");
1377 return intrmask;
1378 }
1379
1380 while (txq->txq_nactive > 0) {
1381 const int dcache_line_size = curcpu()->ci_ci.dcache_line_size;
1382 volatile struct gt_eth_desc *txd = &txq->txq_descs[txq->txq_fi];
1383 uint32_t cmdsts;
1384 size_t pktlen;
1385
1386 GE_TXDPOSTSYNC(sc, txq, txq->txq_fi);
1387 if ((cmdsts = gt32toh(txd->ed_cmdsts)) & TX_CMD_O) {
1388 int nextin;
1389
1390 if (txq->txq_nactive == 1) {
1391 GE_TXDPRESYNC(sc, txq, txq->txq_fi);
1392 GE_FUNC_EXIT(sc, "");
1393 return intrmask;
1394 }
1395 /*
1396 * Sometimes the Discovery forgets to update the
1397 * ownership bit in the descriptor. See if we own the
1398 * descriptor after it (since we know we've turned
1399 * that to the Discovery and if we own it now then the
1400 * Discovery gave it back). If we do, we know the
1401 * Discovery gave back this one but forgot to mark it
1402 * as ours.
1403 */
1404 nextin = txq->txq_fi + 1;
1405 if (nextin == GE_TXDESC_MAX)
1406 nextin = 0;
1407 GE_TXDPOSTSYNC(sc, txq, nextin);
1408 if (gt32toh(txq->txq_descs[nextin].ed_cmdsts) & TX_CMD_O) {
1409 GE_TXDPRESYNC(sc, txq, txq->txq_fi);
1410 GE_TXDPRESYNC(sc, txq, nextin);
1411 GE_FUNC_EXIT(sc, "");
1412 return intrmask;
1413 }
1414 #ifdef DEBUG
1415 printf("%s: txdone: transmitter resynced at %d\n",
1416 device_xname(sc->sc_dev), txq->txq_fi);
1417 #endif
1418 }
1419 #if 0
1420 GE_DPRINTF(sc, ("([%d]<-%08lx.%08lx.%08lx.%08lx)",
1421 txq->txq_lo,
1422 ((unsigned long *)txd)[0], ((unsigned long *)txd)[1],
1423 ((unsigned long *)txd)[2], ((unsigned long *)txd)[3]));
1424 #endif
1425 GE_DPRINTF(sc, ("(%d)", txq->txq_fi));
1426 if (++txq->txq_fi == GE_TXDESC_MAX)
1427 txq->txq_fi = 0;
1428 txq->txq_inptr = gt32toh(txd->ed_bufptr) - txq->txq_buf_busaddr;
1429 pktlen = (gt32toh(txd->ed_lencnt) >> 16) & 0xffff;
1430 bus_dmamap_sync(sc->sc_dmat, txq->txq_buf_mem.gdm_map,
1431 txq->txq_inptr, pktlen, BUS_DMASYNC_POSTWRITE);
1432 txq->txq_inptr += roundup(pktlen, dcache_line_size);
1433
1434 /* statistics */
1435 ifp->if_opackets++;
1436 if (cmdsts & TX_STS_ES)
1437 ifp->if_oerrors++;
1438
1439 /* txd->ed_bufptr = 0; */
1440
1441 ifp->if_timer = 5;
1442 --txq->txq_nactive;
1443 }
1444 if (txq->txq_nactive != 0)
1445 panic("%s: transmit fifo%d empty but active count (%d) > 0!",
1446 device_xname(sc->sc_dev), txprio, txq->txq_nactive);
1447 ifp->if_timer = 0;
1448 intrmask &= ~(txq->txq_intrbits & (ETH_IR_TxEndHigh|ETH_IR_TxEndLow));
1449 intrmask &= ~(txq->txq_intrbits & (ETH_IR_TxBufferHigh|ETH_IR_TxBufferLow));
1450 GE_FUNC_EXIT(sc, "");
1451 return intrmask;
1452 }
1453
1454 int
1455 gfe_tx_txqalloc(struct gfe_softc *sc, enum gfe_txprio txprio)
1456 {
1457 struct gfe_txqueue * const txq = &sc->sc_txq[txprio];
1458 int error;
1459
1460 GE_FUNC_ENTER(sc, "gfe_tx_txqalloc");
1461
1462 error = gfe_dmamem_alloc(sc, &txq->txq_desc_mem, 1,
1463 GE_TXDESC_MEMSIZE, BUS_DMA_NOCACHE);
1464 if (error) {
1465 GE_FUNC_EXIT(sc, "");
1466 return error;
1467 }
1468 error = gfe_dmamem_alloc(sc, &txq->txq_buf_mem, 1, GE_TXBUF_SIZE, 0);
1469 if (error) {
1470 gfe_dmamem_free(sc, &txq->txq_desc_mem);
1471 GE_FUNC_EXIT(sc, "");
1472 return error;
1473 }
1474 GE_FUNC_EXIT(sc, "");
1475 return 0;
1476 }
1477
1478 int
1479 gfe_tx_start(struct gfe_softc *sc, enum gfe_txprio txprio)
1480 {
1481 struct gfe_txqueue * const txq = &sc->sc_txq[txprio];
1482 volatile struct gt_eth_desc *txd;
1483 unsigned int i;
1484 bus_addr_t addr;
1485
1486 GE_FUNC_ENTER(sc, "gfe_tx_start");
1487
1488 sc->sc_intrmask &=
1489 ~(ETH_IR_TxEndHigh |
1490 ETH_IR_TxBufferHigh |
1491 ETH_IR_TxEndLow |
1492 ETH_IR_TxBufferLow);
1493
1494 if (sc->sc_flags & GE_NOFREE) {
1495 KASSERT(txq->txq_desc_mem.gdm_kva != NULL);
1496 KASSERT(txq->txq_buf_mem.gdm_kva != NULL);
1497 } else {
1498 int error = gfe_tx_txqalloc(sc, txprio);
1499 if (error) {
1500 GE_FUNC_EXIT(sc, "!");
1501 return error;
1502 }
1503 }
1504
1505 txq->txq_descs =
1506 (volatile struct gt_eth_desc *) txq->txq_desc_mem.gdm_kva;
1507 txq->txq_desc_busaddr = txq->txq_desc_mem.gdm_map->dm_segs[0].ds_addr;
1508 txq->txq_buf_busaddr = txq->txq_buf_mem.gdm_map->dm_segs[0].ds_addr;
1509
1510 txq->txq_pendq.ifq_maxlen = 10;
1511 txq->txq_ei_gapcount = 0;
1512 txq->txq_nactive = 0;
1513 txq->txq_fi = 0;
1514 txq->txq_lo = 0;
1515 txq->txq_inptr = GE_TXBUF_SIZE;
1516 txq->txq_outptr = 0;
1517 for (i = 0, txd = txq->txq_descs,
1518 addr = txq->txq_desc_busaddr + sizeof(*txd);
1519 i < GE_TXDESC_MAX - 1; i++, txd++, addr += sizeof(*txd)) {
1520 /*
1521 * update the nxtptr to point to the next txd.
1522 */
1523 txd->ed_cmdsts = 0;
1524 txd->ed_nxtptr = htogt32(addr);
1525 }
1526 txq->txq_descs[GE_TXDESC_MAX-1].ed_nxtptr =
1527 htogt32(txq->txq_desc_busaddr);
1528 bus_dmamap_sync(sc->sc_dmat, txq->txq_desc_mem.gdm_map, 0,
1529 GE_TXDESC_MEMSIZE, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1530
1531 switch (txprio) {
1532 case GE_TXPRIO_HI:
1533 txq->txq_intrbits = ETH_IR_TxEndHigh|ETH_IR_TxBufferHigh;
1534 txq->txq_esdcmrbits = ETH_ESDCMR_TXDH;
1535 txq->txq_epsrbits = ETH_EPSR_TxHigh;
1536 txq->txq_ectdp = ETH_ECTDP1;
1537 GE_WRITE(sc, ETH_ECTDP1, txq->txq_desc_busaddr);
1538 break;
1539
1540 case GE_TXPRIO_LO:
1541 txq->txq_intrbits = ETH_IR_TxEndLow|ETH_IR_TxBufferLow;
1542 txq->txq_esdcmrbits = ETH_ESDCMR_TXDL;
1543 txq->txq_epsrbits = ETH_EPSR_TxLow;
1544 txq->txq_ectdp = ETH_ECTDP0;
1545 GE_WRITE(sc, ETH_ECTDP0, txq->txq_desc_busaddr);
1546 break;
1547
1548 case GE_TXPRIO_NONE:
1549 break;
1550 }
1551 #if 0
1552 GE_DPRINTF(sc, ("(ectdp=%#x", txq->txq_ectdp));
1553 GE_WRITE(sc->sc_dev, txq->txq_ectdp, txq->txq_desc_busaddr);
1554 GE_DPRINTF(sc, (")"));
1555 #endif
1556
1557 /*
1558 * If we are restarting, there may be packets in the pending queue
1559 * waiting to be enqueued. Try enqueuing packets from both priority
1560 * queues until the pending queue is empty or there no room for them
1561 * on the device.
1562 */
1563 while (gfe_tx_enqueue(sc, txprio))
1564 continue;
1565
1566 GE_FUNC_EXIT(sc, "");
1567 return 0;
1568 }
1569
1570 void
1571 gfe_tx_cleanup(struct gfe_softc *sc, enum gfe_txprio txprio, int flush)
1572 {
1573 struct gfe_txqueue * const txq = &sc->sc_txq[txprio];
1574
1575 GE_FUNC_ENTER(sc, "gfe_tx_cleanup");
1576 if (txq == NULL) {
1577 GE_FUNC_EXIT(sc, "");
1578 return;
1579 }
1580
1581 if (!flush) {
1582 GE_FUNC_EXIT(sc, "");
1583 return;
1584 }
1585
1586 if ((sc->sc_flags & GE_NOFREE) == 0) {
1587 gfe_dmamem_free(sc, &txq->txq_desc_mem);
1588 gfe_dmamem_free(sc, &txq->txq_buf_mem);
1589 }
1590 GE_FUNC_EXIT(sc, "-F");
1591 }
1592
1593 void
1594 gfe_tx_stop(struct gfe_softc *sc, enum gfe_whack_op op)
1595 {
1596 GE_FUNC_ENTER(sc, "gfe_tx_stop");
1597
1598 GE_WRITE(sc, ETH_ESDCMR, ETH_ESDCMR_STDH|ETH_ESDCMR_STDL);
1599
1600 sc->sc_intrmask = gfe_tx_done(sc, GE_TXPRIO_HI, sc->sc_intrmask);
1601 sc->sc_intrmask = gfe_tx_done(sc, GE_TXPRIO_LO, sc->sc_intrmask);
1602 sc->sc_intrmask &=
1603 ~(ETH_IR_TxEndHigh |
1604 ETH_IR_TxBufferHigh |
1605 ETH_IR_TxEndLow |
1606 ETH_IR_TxBufferLow);
1607
1608 gfe_tx_cleanup(sc, GE_TXPRIO_HI, op == GE_WHACK_STOP);
1609 gfe_tx_cleanup(sc, GE_TXPRIO_LO, op == GE_WHACK_STOP);
1610
1611 sc->sc_ec.ec_if.if_timer = 0;
1612 GE_FUNC_EXIT(sc, "");
1613 }
1614
1615 int
1616 gfe_intr(void *arg)
1617 {
1618 struct gfe_softc * const sc = arg;
1619 uint32_t cause;
1620 uint32_t intrmask = sc->sc_intrmask;
1621 int claim = 0;
1622 int cnt;
1623
1624 GE_FUNC_ENTER(sc, "gfe_intr");
1625
1626 for (cnt = 0; cnt < 4; cnt++) {
1627 if (sc->sc_intrmask != intrmask) {
1628 sc->sc_intrmask = intrmask;
1629 GE_WRITE(sc, ETH_EIMR, sc->sc_intrmask);
1630 }
1631 cause = GE_READ(sc, ETH_EICR);
1632 cause &= sc->sc_intrmask;
1633 GE_DPRINTF(sc, (".%#x", cause));
1634 if (cause == 0)
1635 break;
1636
1637 claim = 1;
1638
1639 GE_WRITE(sc, ETH_EICR, ~cause);
1640 #ifndef GE_NORX
1641 if (cause & (ETH_IR_RxBuffer|ETH_IR_RxError))
1642 intrmask = gfe_rx_process(sc, cause, intrmask);
1643 #endif
1644
1645 #ifndef GE_NOTX
1646 if (cause & (ETH_IR_TxBufferHigh|ETH_IR_TxEndHigh))
1647 intrmask = gfe_tx_done(sc, GE_TXPRIO_HI, intrmask);
1648 if (cause & (ETH_IR_TxBufferLow|ETH_IR_TxEndLow))
1649 intrmask = gfe_tx_done(sc, GE_TXPRIO_LO, intrmask);
1650 #endif
1651 if (cause & ETH_IR_MIIPhySTC) {
1652 sc->sc_flags |= GE_PHYSTSCHG;
1653 /* intrmask &= ~ETH_IR_MIIPhySTC; */
1654 }
1655 }
1656
1657 while (gfe_tx_enqueue(sc, GE_TXPRIO_HI))
1658 continue;
1659 while (gfe_tx_enqueue(sc, GE_TXPRIO_LO))
1660 continue;
1661
1662 GE_FUNC_EXIT(sc, "");
1663 return claim;
1664 }
1665
1666 int
1667 gfe_whack(struct gfe_softc *sc, enum gfe_whack_op op)
1668 {
1669 int error = 0;
1670 GE_FUNC_ENTER(sc, "gfe_whack");
1671
1672 switch (op) {
1673 case GE_WHACK_RESTART:
1674 #ifndef GE_NOTX
1675 gfe_tx_stop(sc, op);
1676 #endif
1677 /* sc->sc_ec.ec_if.if_flags &= ~IFF_RUNNING; */
1678 /* FALLTHROUGH */
1679 case GE_WHACK_START:
1680 #ifndef GE_NOHASH
1681 if (error == 0 && sc->sc_hashtable == NULL) {
1682 error = gfe_hash_alloc(sc);
1683 if (error)
1684 break;
1685 }
1686 if (op != GE_WHACK_RESTART)
1687 gfe_hash_fill(sc);
1688 #endif
1689 #ifndef GE_NORX
1690 if (op != GE_WHACK_RESTART) {
1691 error = gfe_rx_prime(sc);
1692 if (error)
1693 break;
1694 }
1695 #endif
1696 #ifndef GE_NOTX
1697 error = gfe_tx_start(sc, GE_TXPRIO_HI);
1698 if (error)
1699 break;
1700 #endif
1701 sc->sc_ec.ec_if.if_flags |= IFF_RUNNING;
1702 GE_WRITE(sc, ETH_EPCR, sc->sc_pcr | ETH_EPCR_EN);
1703 GE_WRITE(sc, ETH_EPCXR, sc->sc_pcxr);
1704 GE_WRITE(sc, ETH_EICR, 0);
1705 GE_WRITE(sc, ETH_EIMR, sc->sc_intrmask);
1706 #ifndef GE_NOHASH
1707 GE_WRITE(sc, ETH_EHTPR,
1708 sc->sc_hash_mem.gdm_map->dm_segs->ds_addr);
1709 #endif
1710 #ifndef GE_NORX
1711 GE_WRITE(sc, ETH_ESDCMR, ETH_ESDCMR_ERD);
1712 sc->sc_flags |= GE_RXACTIVE;
1713 #endif
1714 /* FALLTHROUGH */
1715 case GE_WHACK_CHANGE:
1716 GE_DPRINTF(sc, ("(pcr=%#x,imr=%#x)",
1717 GE_READ(sc, ETH_EPCR), GE_READ(sc, ETH_EIMR)));
1718 GE_WRITE(sc, ETH_EPCR, sc->sc_pcr | ETH_EPCR_EN);
1719 GE_WRITE(sc, ETH_EIMR, sc->sc_intrmask);
1720 gfe_ifstart(&sc->sc_ec.ec_if);
1721 GE_DPRINTF(sc, ("(ectdp0=%#x, ectdp1=%#x)",
1722 GE_READ(sc, ETH_ECTDP0), GE_READ(sc, ETH_ECTDP1)));
1723 GE_FUNC_EXIT(sc, "");
1724 return error;
1725 case GE_WHACK_STOP:
1726 break;
1727 }
1728
1729 #ifdef GE_DEBUG
1730 if (error)
1731 GE_DPRINTF(sc, (" failed: %d\n", error));
1732 #endif
1733 GE_WRITE(sc, ETH_EPCR, sc->sc_pcr);
1734 GE_WRITE(sc, ETH_EIMR, 0);
1735 sc->sc_ec.ec_if.if_flags &= ~IFF_RUNNING;
1736 #ifndef GE_NOTX
1737 gfe_tx_stop(sc, GE_WHACK_STOP);
1738 #endif
1739 #ifndef GE_NORX
1740 gfe_rx_stop(sc, GE_WHACK_STOP);
1741 #endif
1742 #ifndef GE_NOHASH
1743 if ((sc->sc_flags & GE_NOFREE) == 0) {
1744 gfe_dmamem_free(sc, &sc->sc_hash_mem);
1745 sc->sc_hashtable = NULL;
1746 }
1747 #endif
1748
1749 GE_FUNC_EXIT(sc, "");
1750 return error;
1751 }
1752
1753 int
1754 gfe_hash_compute(struct gfe_softc *sc, const uint8_t eaddr[ETHER_ADDR_LEN])
1755 {
1756 uint32_t w0, add0, add1;
1757 uint32_t result;
1758
1759 GE_FUNC_ENTER(sc, "gfe_hash_compute");
1760 add0 = ((uint32_t) eaddr[5] << 0) |
1761 ((uint32_t) eaddr[4] << 8) |
1762 ((uint32_t) eaddr[3] << 16);
1763
1764 add0 = ((add0 & 0x00f0f0f0) >> 4) | ((add0 & 0x000f0f0f) << 4);
1765 add0 = ((add0 & 0x00cccccc) >> 2) | ((add0 & 0x00333333) << 2);
1766 add0 = ((add0 & 0x00aaaaaa) >> 1) | ((add0 & 0x00555555) << 1);
1767
1768 add1 = ((uint32_t) eaddr[2] << 0) |
1769 ((uint32_t) eaddr[1] << 8) |
1770 ((uint32_t) eaddr[0] << 16);
1771
1772 add1 = ((add1 & 0x00f0f0f0) >> 4) | ((add1 & 0x000f0f0f) << 4);
1773 add1 = ((add1 & 0x00cccccc) >> 2) | ((add1 & 0x00333333) << 2);
1774 add1 = ((add1 & 0x00aaaaaa) >> 1) | ((add1 & 0x00555555) << 1);
1775
1776 GE_DPRINTF(sc, ("%s=", ether_sprintf(eaddr)));
1777 /*
1778 * hashResult is the 15 bits Hash entry address.
1779 * ethernetADD is a 48 bit number, which is derived from the Ethernet
1780 * MAC address, by nibble swapping in every byte (i.e MAC address
1781 * of 0x123456789abc translates to ethernetADD of 0x21436587a9cb).
1782 */
1783
1784 if ((sc->sc_pcr & ETH_EPCR_HM) == 0) {
1785 /*
1786 * hashResult[14:0] = hashFunc0(ethernetADD[47:0])
1787 *
1788 * hashFunc0 calculates the hashResult in the following manner:
1789 * hashResult[ 8:0] = ethernetADD[14:8,1,0]
1790 * XOR ethernetADD[23:15] XOR ethernetADD[32:24]
1791 */
1792 result = (add0 & 3) | ((add0 >> 6) & ~3);
1793 result ^= (add0 >> 15) ^ (add1 >> 0);
1794 result &= 0x1ff;
1795 /*
1796 * hashResult[14:9] = ethernetADD[7:2]
1797 */
1798 result |= (add0 & ~3) << 7; /* excess bits will be masked */
1799 GE_DPRINTF(sc, ("0(%#x)", result & 0x7fff));
1800 } else {
1801 #define TRIBITFLIP 073516240 /* yes its in octal */
1802 /*
1803 * hashResult[14:0] = hashFunc1(ethernetADD[47:0])
1804 *
1805 * hashFunc1 calculates the hashResult in the following manner:
1806 * hashResult[08:00] = ethernetADD[06:14]
1807 * XOR ethernetADD[15:23] XOR ethernetADD[24:32]
1808 */
1809 w0 = ((add0 >> 6) ^ (add0 >> 15) ^ (add1)) & 0x1ff;
1810 /*
1811 * Now bitswap those 9 bits
1812 */
1813 result = 0;
1814 result |= ((TRIBITFLIP >> (((w0 >> 0) & 7) * 3)) & 7) << 6;
1815 result |= ((TRIBITFLIP >> (((w0 >> 3) & 7) * 3)) & 7) << 3;
1816 result |= ((TRIBITFLIP >> (((w0 >> 6) & 7) * 3)) & 7) << 0;
1817
1818 /*
1819 * hashResult[14:09] = ethernetADD[00:05]
1820 */
1821 result |= ((TRIBITFLIP >> (((add0 >> 0) & 7) * 3)) & 7) << 12;
1822 result |= ((TRIBITFLIP >> (((add0 >> 3) & 7) * 3)) & 7) << 9;
1823 GE_DPRINTF(sc, ("1(%#x)", result));
1824 }
1825 GE_FUNC_EXIT(sc, "");
1826 return result & ((sc->sc_pcr & ETH_EPCR_HS_512) ? 0x7ff : 0x7fff);
1827 }
1828
1829 int
1830 gfe_hash_entry_op(struct gfe_softc *sc, enum gfe_hash_op op,
1831 enum gfe_rxprio prio, const uint8_t eaddr[ETHER_ADDR_LEN])
1832 {
1833 uint64_t he;
1834 uint64_t *maybe_he_p = NULL;
1835 int limit;
1836 int hash;
1837 int maybe_hash = 0;
1838
1839 GE_FUNC_ENTER(sc, "gfe_hash_entry_op");
1840
1841 hash = gfe_hash_compute(sc, eaddr);
1842
1843 if (sc->sc_hashtable == NULL) {
1844 panic("%s:%d: hashtable == NULL!", device_xname(sc->sc_dev),
1845 __LINE__);
1846 }
1847
1848 /*
1849 * Assume we are going to insert so create the hash entry we
1850 * are going to insert. We also use it to match entries we
1851 * will be removing.
1852 */
1853 he = ((uint64_t) eaddr[5] << 43) |
1854 ((uint64_t) eaddr[4] << 35) |
1855 ((uint64_t) eaddr[3] << 27) |
1856 ((uint64_t) eaddr[2] << 19) |
1857 ((uint64_t) eaddr[1] << 11) |
1858 ((uint64_t) eaddr[0] << 3) |
1859 HSH_PRIO_INS(prio) | HSH_V | HSH_R;
1860
1861 /*
1862 * The GT will search upto 12 entries for a hit, so we must mimic that.
1863 */
1864 hash &= sc->sc_hashmask / sizeof(he);
1865 for (limit = HSH_LIMIT; limit > 0 ; --limit) {
1866 /*
1867 * Does the GT wrap at the end, stop at the, or overrun the
1868 * end? Assume it wraps for now. Stash a copy of the
1869 * current hash entry.
1870 */
1871 uint64_t *he_p = &sc->sc_hashtable[hash];
1872 uint64_t thishe = *he_p;
1873
1874 /*
1875 * If the hash entry isn't valid, that break the chain. And
1876 * this entry a good candidate for reuse.
1877 */
1878 if ((thishe & HSH_V) == 0) {
1879 maybe_he_p = he_p;
1880 break;
1881 }
1882
1883 /*
1884 * If the hash entry has the same address we are looking for
1885 * then ... if we are removing and the skip bit is set, its
1886 * already been removed. if are adding and the skip bit is
1887 * clear, then its already added. In either return EBUSY
1888 * indicating the op has already been done. Otherwise flip
1889 * the skip bit and return 0.
1890 */
1891 if (((he ^ thishe) & HSH_ADDR_MASK) == 0) {
1892 if (((op == GE_HASH_REMOVE) && (thishe & HSH_S)) ||
1893 ((op == GE_HASH_ADD) && (thishe & HSH_S) == 0))
1894 return EBUSY;
1895 *he_p = thishe ^ HSH_S;
1896 bus_dmamap_sync(sc->sc_dmat, sc->sc_hash_mem.gdm_map,
1897 hash * sizeof(he), sizeof(he),
1898 BUS_DMASYNC_PREWRITE);
1899 GE_FUNC_EXIT(sc, "^");
1900 return 0;
1901 }
1902
1903 /*
1904 * If we haven't found a slot for the entry and this entry
1905 * is currently being skipped, return this entry.
1906 */
1907 if (maybe_he_p == NULL && (thishe & HSH_S)) {
1908 maybe_he_p = he_p;
1909 maybe_hash = hash;
1910 }
1911
1912 hash = (hash + 1) & (sc->sc_hashmask / sizeof(he));
1913 }
1914
1915 /*
1916 * If we got here, then there was no entry to remove.
1917 */
1918 if (op == GE_HASH_REMOVE) {
1919 GE_FUNC_EXIT(sc, "?");
1920 return ENOENT;
1921 }
1922
1923 /*
1924 * If we couldn't find a slot, return an error.
1925 */
1926 if (maybe_he_p == NULL) {
1927 GE_FUNC_EXIT(sc, "!");
1928 return ENOSPC;
1929 }
1930
1931 /* Update the entry.
1932 */
1933 *maybe_he_p = he;
1934 bus_dmamap_sync(sc->sc_dmat, sc->sc_hash_mem.gdm_map,
1935 maybe_hash * sizeof(he), sizeof(he), BUS_DMASYNC_PREWRITE);
1936 GE_FUNC_EXIT(sc, "+");
1937 return 0;
1938 }
1939
1940 int
1941 gfe_hash_multichg(struct ethercom *ec, const struct ether_multi *enm,
1942 u_long cmd)
1943 {
1944 struct gfe_softc *sc = ec->ec_if.if_softc;
1945 int error;
1946 enum gfe_hash_op op;
1947 enum gfe_rxprio prio;
1948
1949 GE_FUNC_ENTER(sc, "hash_multichg");
1950 /*
1951 * Is this a wildcard entry? If so and its being removed, recompute.
1952 */
1953 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN) != 0) {
1954 if (cmd == SIOCDELMULTI) {
1955 GE_FUNC_EXIT(sc, "");
1956 return ENETRESET;
1957 }
1958
1959 /*
1960 * Switch in
1961 */
1962 sc->sc_flags |= GE_ALLMULTI;
1963 if ((sc->sc_pcr & ETH_EPCR_PM) == 0) {
1964 sc->sc_pcr |= ETH_EPCR_PM;
1965 GE_WRITE(sc, ETH_EPCR, sc->sc_pcr);
1966 GE_FUNC_EXIT(sc, "");
1967 return 0;
1968 }
1969 GE_FUNC_EXIT(sc, "");
1970 return ENETRESET;
1971 }
1972
1973 prio = GE_RXPRIO_MEDLO;
1974 op = (cmd == SIOCDELMULTI ? GE_HASH_REMOVE : GE_HASH_ADD);
1975
1976 if (sc->sc_hashtable == NULL) {
1977 GE_FUNC_EXIT(sc, "");
1978 return 0;
1979 }
1980
1981 error = gfe_hash_entry_op(sc, op, prio, enm->enm_addrlo);
1982 if (error == EBUSY) {
1983 aprint_error_dev(sc->sc_dev, "multichg: tried to %s %s again\n",
1984 cmd == SIOCDELMULTI ? "remove" : "add",
1985 ether_sprintf(enm->enm_addrlo));
1986 GE_FUNC_EXIT(sc, "");
1987 return 0;
1988 }
1989
1990 if (error == ENOENT) {
1991 aprint_error_dev(sc->sc_dev,
1992 "multichg: failed to remove %s: not in table\n",
1993 ether_sprintf(enm->enm_addrlo));
1994 GE_FUNC_EXIT(sc, "");
1995 return 0;
1996 }
1997
1998 if (error == ENOSPC) {
1999 aprint_error_dev(sc->sc_dev, "multichg:"
2000 " failed to add %s: no space; regenerating table\n",
2001 ether_sprintf(enm->enm_addrlo));
2002 GE_FUNC_EXIT(sc, "");
2003 return ENETRESET;
2004 }
2005 GE_DPRINTF(sc, ("%s: multichg: %s: %s succeeded\n",
2006 device_xname(sc->sc_dev),
2007 cmd == SIOCDELMULTI ? "remove" : "add",
2008 ether_sprintf(enm->enm_addrlo)));
2009 GE_FUNC_EXIT(sc, "");
2010 return 0;
2011 }
2012
2013 int
2014 gfe_hash_fill(struct gfe_softc *sc)
2015 {
2016 struct ether_multistep step;
2017 struct ether_multi *enm;
2018 int error;
2019
2020 GE_FUNC_ENTER(sc, "gfe_hash_fill");
2021
2022 error = gfe_hash_entry_op(sc, GE_HASH_ADD, GE_RXPRIO_HI,
2023 CLLADDR(sc->sc_ec.ec_if.if_sadl));
2024 if (error) {
2025 GE_FUNC_EXIT(sc, "!");
2026 return error;
2027 }
2028
2029 sc->sc_flags &= ~GE_ALLMULTI;
2030 if ((sc->sc_ec.ec_if.if_flags & IFF_PROMISC) == 0)
2031 sc->sc_pcr &= ~ETH_EPCR_PM;
2032 ETHER_FIRST_MULTI(step, &sc->sc_ec, enm);
2033 while (enm != NULL) {
2034 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
2035 sc->sc_flags |= GE_ALLMULTI;
2036 sc->sc_pcr |= ETH_EPCR_PM;
2037 } else {
2038 error = gfe_hash_entry_op(sc, GE_HASH_ADD,
2039 GE_RXPRIO_MEDLO, enm->enm_addrlo);
2040 if (error == ENOSPC)
2041 break;
2042 }
2043 ETHER_NEXT_MULTI(step, enm);
2044 }
2045
2046 GE_FUNC_EXIT(sc, "");
2047 return error;
2048 }
2049
2050 int
2051 gfe_hash_alloc(struct gfe_softc *sc)
2052 {
2053 int error;
2054 GE_FUNC_ENTER(sc, "gfe_hash_alloc");
2055 sc->sc_hashmask = (sc->sc_pcr & ETH_EPCR_HS_512 ? 16 : 256)*1024 - 1;
2056 error = gfe_dmamem_alloc(sc, &sc->sc_hash_mem, 1, sc->sc_hashmask + 1,
2057 BUS_DMA_NOCACHE);
2058 if (error) {
2059 aprint_error_dev(sc->sc_dev,
2060 "failed to allocate %d bytes for hash table: %d\n",
2061 sc->sc_hashmask + 1, error);
2062 GE_FUNC_EXIT(sc, "");
2063 return error;
2064 }
2065 sc->sc_hashtable = (uint64_t *) sc->sc_hash_mem.gdm_kva;
2066 memset(sc->sc_hashtable, 0, sc->sc_hashmask + 1);
2067 bus_dmamap_sync(sc->sc_dmat, sc->sc_hash_mem.gdm_map,
2068 0, sc->sc_hashmask + 1, BUS_DMASYNC_PREWRITE);
2069 GE_FUNC_EXIT(sc, "");
2070 return 0;
2071 }
2072