if_mvgbe.c revision 1.29 1 /* $NetBSD: if_mvgbe.c,v 1.29 2012/11/08 14:32:01 msaitoh Exp $ */
2 /*
3 * Copyright (c) 2007, 2008 KIYOHARA Takashi
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27 #include <sys/cdefs.h>
28 __KERNEL_RCSID(0, "$NetBSD: if_mvgbe.c,v 1.29 2012/11/08 14:32:01 msaitoh Exp $");
29
30 #include <sys/param.h>
31 #include <sys/bus.h>
32 #include <sys/callout.h>
33 #include <sys/device.h>
34 #include <sys/endian.h>
35 #include <sys/errno.h>
36 #include <sys/kernel.h>
37 #include <sys/kmem.h>
38 #include <sys/mutex.h>
39 #include <sys/sockio.h>
40 #include <sys/sysctl.h>
41
42 #include <dev/marvell/marvellreg.h>
43 #include <dev/marvell/marvellvar.h>
44 #include <dev/marvell/mvgbereg.h>
45
46 #include <net/if.h>
47 #include <net/if_ether.h>
48 #include <net/if_media.h>
49
50 #include <netinet/in.h>
51 #include <netinet/in_systm.h>
52 #include <netinet/ip.h>
53
54 #include <net/bpf.h>
55 #include <sys/rnd.h>
56
57 #include <dev/mii/mii.h>
58 #include <dev/mii/miivar.h>
59
60 #include "locators.h"
61
62 /* #define MVGBE_DEBUG 3 */
63 #ifdef MVGBE_DEBUG
64 #define DPRINTF(x) if (mvgbe_debug) printf x
65 #define DPRINTFN(n,x) if (mvgbe_debug >= (n)) printf x
66 int mvgbe_debug = MVGBE_DEBUG;
67 #else
68 #define DPRINTF(x)
69 #define DPRINTFN(n,x)
70 #endif
71
72
73 #define MVGBE_READ(sc, reg) \
74 bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg))
75 #define MVGBE_WRITE(sc, reg, val) \
76 bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
77 #define MVGBE_READ_FILTER(sc, reg, val, c) \
78 bus_space_read_region_4((sc)->sc_iot, (sc)->sc_dafh, (reg), (val), (c))
79 #define MVGBE_WRITE_FILTER(sc, reg, val, c) \
80 bus_space_write_region_4((sc)->sc_iot, (sc)->sc_dafh, (reg), (val), (c))
81
82 #define MVGBE_TX_RING_CNT 256
83 #define MVGBE_TX_RING_MSK (MVGBE_TX_RING_CNT - 1)
84 #define MVGBE_TX_RING_NEXT(x) (((x) + 1) & MVGBE_TX_RING_MSK)
85 #define MVGBE_RX_RING_CNT 256
86 #define MVGBE_RX_RING_MSK (MVGBE_RX_RING_CNT - 1)
87 #define MVGBE_RX_RING_NEXT(x) (((x) + 1) & MVGBE_RX_RING_MSK)
88
89 CTASSERT(MVGBE_TX_RING_CNT > 1 && MVGBE_TX_RING_NEXT(MVGBE_TX_RING_CNT) ==
90 (MVGBE_TX_RING_CNT + 1) % MVGBE_TX_RING_CNT);
91 CTASSERT(MVGBE_RX_RING_CNT > 1 && MVGBE_RX_RING_NEXT(MVGBE_RX_RING_CNT) ==
92 (MVGBE_RX_RING_CNT + 1) % MVGBE_RX_RING_CNT);
93
94 #define MVGBE_JSLOTS 384 /* XXXX */
95 #define MVGBE_JLEN \
96 ((MVGBE_MRU + MVGBE_RXBUF_ALIGN) & ~MVGBE_RXBUF_MASK)
97 #define MVGBE_NTXSEG 30
98 #define MVGBE_JPAGESZ PAGE_SIZE
99 #define MVGBE_RESID \
100 (MVGBE_JPAGESZ - (MVGBE_JLEN * MVGBE_JSLOTS) % MVGBE_JPAGESZ)
101 #define MVGBE_JMEM \
102 ((MVGBE_JLEN * MVGBE_JSLOTS) + MVGBE_RESID)
103
104 #define MVGBE_TX_RING_ADDR(sc, i) \
105 ((sc)->sc_ring_map->dm_segs[0].ds_addr + \
106 offsetof(struct mvgbe_ring_data, mvgbe_tx_ring[(i)]))
107
108 #define MVGBE_RX_RING_ADDR(sc, i) \
109 ((sc)->sc_ring_map->dm_segs[0].ds_addr + \
110 offsetof(struct mvgbe_ring_data, mvgbe_rx_ring[(i)]))
111
112 #define MVGBE_CDOFF(x) offsetof(struct mvgbe_ring_data, x)
113 #define MVGBE_CDTXOFF(x) MVGBE_CDOFF(mvgbe_tx_ring[(x)])
114 #define MVGBE_CDRXOFF(x) MVGBE_CDOFF(mvgbe_rx_ring[(x)])
115
116 #define MVGBE_CDTXSYNC(sc, x, n, ops) \
117 do { \
118 int __x, __n; \
119 const int __descsize = sizeof(struct mvgbe_tx_desc); \
120 \
121 __x = (x); \
122 __n = (n); \
123 \
124 /* If it will wrap around, sync to the end of the ring. */ \
125 if ((__x + __n) > MVGBE_TX_RING_CNT) { \
126 bus_dmamap_sync((sc)->sc_dmat, \
127 (sc)->sc_ring_map, MVGBE_CDTXOFF(__x), \
128 __descsize * (MVGBE_TX_RING_CNT - __x), (ops)); \
129 __n -= (MVGBE_TX_RING_CNT - __x); \
130 __x = 0; \
131 } \
132 \
133 /* Now sync whatever is left. */ \
134 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_ring_map, \
135 MVGBE_CDTXOFF((__x)), __descsize * __n, (ops)); \
136 } while (0 /*CONSTCOND*/)
137
138 #define MVGBE_CDRXSYNC(sc, x, ops) \
139 do { \
140 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_ring_map, \
141 MVGBE_CDRXOFF((x)), sizeof(struct mvgbe_rx_desc), (ops)); \
142 } while (/*CONSTCOND*/0)
143
144 #define MVGBE_IPGINTTX_DEFAULT 768
145 #define MVGBE_IPGINTRX_DEFAULT 768
146
147 struct mvgbe_jpool_entry {
148 int slot;
149 LIST_ENTRY(mvgbe_jpool_entry) jpool_entries;
150 };
151
152 struct mvgbe_chain {
153 void *mvgbe_desc;
154 struct mbuf *mvgbe_mbuf;
155 struct mvgbe_chain *mvgbe_next;
156 };
157
158 struct mvgbe_txmap_entry {
159 bus_dmamap_t dmamap;
160 SIMPLEQ_ENTRY(mvgbe_txmap_entry) link;
161 };
162
163 struct mvgbe_chain_data {
164 struct mvgbe_chain mvgbe_tx_chain[MVGBE_TX_RING_CNT];
165 struct mvgbe_txmap_entry *mvgbe_tx_map[MVGBE_TX_RING_CNT];
166 int mvgbe_tx_prod;
167 int mvgbe_tx_cons;
168 int mvgbe_tx_cnt;
169
170 struct mvgbe_chain mvgbe_rx_chain[MVGBE_RX_RING_CNT];
171 bus_dmamap_t mvgbe_rx_map[MVGBE_RX_RING_CNT];
172 bus_dmamap_t mvgbe_rx_jumbo_map;
173 int mvgbe_rx_prod;
174 int mvgbe_rx_cons;
175 int mvgbe_rx_cnt;
176
177 /* Stick the jumbo mem management stuff here too. */
178 void *mvgbe_jslots[MVGBE_JSLOTS];
179 void *mvgbe_jumbo_buf;
180 };
181
182 struct mvgbe_ring_data {
183 struct mvgbe_tx_desc mvgbe_tx_ring[MVGBE_TX_RING_CNT];
184 struct mvgbe_rx_desc mvgbe_rx_ring[MVGBE_RX_RING_CNT];
185 };
186
187 struct mvgbec_softc {
188 device_t sc_dev;
189
190 bus_space_tag_t sc_iot;
191 bus_space_handle_t sc_ioh;
192
193 kmutex_t sc_mtx;
194
195 int sc_flags;
196 };
197
198 struct mvgbe_softc {
199 device_t sc_dev;
200 int sc_port;
201
202 bus_space_tag_t sc_iot;
203 bus_space_handle_t sc_ioh;
204 bus_space_handle_t sc_dafh; /* dest address filter handle */
205 bus_dma_tag_t sc_dmat;
206
207 struct ethercom sc_ethercom;
208 struct mii_data sc_mii;
209 u_int8_t sc_enaddr[ETHER_ADDR_LEN]; /* station addr */
210
211 callout_t sc_tick_ch; /* tick callout */
212
213 struct mvgbe_chain_data sc_cdata;
214 struct mvgbe_ring_data *sc_rdata;
215 bus_dmamap_t sc_ring_map;
216 int sc_if_flags;
217 unsigned int sc_ipginttx;
218 unsigned int sc_ipgintrx;
219 int sc_wdogsoft;
220
221 LIST_HEAD(__mvgbe_jfreehead, mvgbe_jpool_entry) sc_jfree_listhead;
222 LIST_HEAD(__mvgbe_jinusehead, mvgbe_jpool_entry) sc_jinuse_listhead;
223 SIMPLEQ_HEAD(__mvgbe_txmaphead, mvgbe_txmap_entry) sc_txmap_head;
224
225 krndsource_t sc_rnd_source;
226 struct sysctllog *mvgbe_clog;
227 };
228
229
230 /* Gigabit Ethernet Unit Global part functions */
231
232 static int mvgbec_match(device_t, struct cfdata *, void *);
233 static void mvgbec_attach(device_t, device_t, void *);
234
235 static int mvgbec_print(void *, const char *);
236 static int mvgbec_search(device_t, cfdata_t, const int *, void *);
237
238 /* MII funcstions */
239 static int mvgbec_miibus_readreg(device_t, int, int);
240 static void mvgbec_miibus_writereg(device_t, int, int, int);
241 static void mvgbec_miibus_statchg(struct ifnet *);
242
243 static void mvgbec_wininit(struct mvgbec_softc *);
244
245 /* Gigabit Ethernet Port part functions */
246
247 static int mvgbe_match(device_t, struct cfdata *, void *);
248 static void mvgbe_attach(device_t, device_t, void *);
249
250 static void mvgbe_tick(void *);
251 static int mvgbe_intr(void *);
252
253 static void mvgbe_start(struct ifnet *);
254 static int mvgbe_ioctl(struct ifnet *, u_long, void *);
255 static int mvgbe_init(struct ifnet *);
256 static void mvgbe_stop(struct ifnet *, int);
257 static void mvgbe_watchdog(struct ifnet *);
258
259 static int mvgbe_ifflags_cb(struct ethercom *);
260
261 static int mvgbe_mediachange(struct ifnet *);
262 static void mvgbe_mediastatus(struct ifnet *, struct ifmediareq *);
263
264 static int mvgbe_init_rx_ring(struct mvgbe_softc *);
265 static int mvgbe_init_tx_ring(struct mvgbe_softc *);
266 static int mvgbe_newbuf(struct mvgbe_softc *, int, struct mbuf *, bus_dmamap_t);
267 static int mvgbe_alloc_jumbo_mem(struct mvgbe_softc *);
268 static void *mvgbe_jalloc(struct mvgbe_softc *);
269 static void mvgbe_jfree(struct mbuf *, void *, size_t, void *);
270 static int mvgbe_encap(struct mvgbe_softc *, struct mbuf *, uint32_t *);
271 static void mvgbe_rxeof(struct mvgbe_softc *);
272 static void mvgbe_txeof(struct mvgbe_softc *);
273 static uint8_t mvgbe_crc8(const uint8_t *, size_t);
274 static void mvgbe_filter_setup(struct mvgbe_softc *);
275 #ifdef MVGBE_DEBUG
276 static void mvgbe_dump_txdesc(struct mvgbe_tx_desc *, int);
277 #endif
278 static int mvgbe_ipginttx(struct mvgbec_softc *, struct mvgbe_softc *,
279 unsigned int);
280 static int mvgbe_ipgintrx(struct mvgbec_softc *, struct mvgbe_softc *,
281 unsigned int);
282 static void sysctl_mvgbe_init(struct mvgbe_softc *);
283 static int mvgbe_sysctl_ipginttx(SYSCTLFN_PROTO);
284 static int mvgbe_sysctl_ipgintrx(SYSCTLFN_PROTO);
285
286 CFATTACH_DECL_NEW(mvgbec_gt, sizeof(struct mvgbec_softc),
287 mvgbec_match, mvgbec_attach, NULL, NULL);
288 CFATTACH_DECL_NEW(mvgbec_mbus, sizeof(struct mvgbec_softc),
289 mvgbec_match, mvgbec_attach, NULL, NULL);
290
291 CFATTACH_DECL_NEW(mvgbe, sizeof(struct mvgbe_softc),
292 mvgbe_match, mvgbe_attach, NULL, NULL);
293
294 device_t mvgbec0 = NULL;
295 static int mvgbe_root_num;
296
297 struct mvgbe_port {
298 int model;
299 int unit;
300 int ports;
301 int irqs[3];
302 int flags;
303 #define FLAGS_FIX_TQTB (1 << 0)
304 #define FLAGS_FIX_MTU (1 << 1)
305 #define FLAGS_IPG1 (1 << 2)
306 #define FLAGS_IPG2 (1 << 3)
307 } mvgbe_ports[] = {
308 { MARVELL_DISCOVERY_II, 0, 3, { 32, 33, 34 }, 0 },
309 { MARVELL_DISCOVERY_III, 0, 3, { 32, 33, 34 }, 0 },
310 #if 0
311 { MARVELL_DISCOVERY_LT, 0, ?, { }, 0 },
312 { MARVELL_DISCOVERY_V, 0, ?, { }, 0 },
313 { MARVELL_DISCOVERY_VI, 0, ?, { }, 0 },
314 #endif
315 { MARVELL_ORION_1_88F5082, 0, 1, { 21 }, FLAGS_FIX_MTU },
316 { MARVELL_ORION_1_88F5180N, 0, 1, { 21 }, FLAGS_FIX_MTU },
317 { MARVELL_ORION_1_88F5181, 0, 1, { 21 }, FLAGS_FIX_MTU | FLAGS_IPG1 },
318 { MARVELL_ORION_1_88F5182, 0, 1, { 21 }, FLAGS_FIX_MTU | FLAGS_IPG1 },
319 { MARVELL_ORION_2_88F5281, 0, 1, { 21 }, FLAGS_FIX_MTU | FLAGS_IPG1 },
320 { MARVELL_ORION_1_88F6082, 0, 1, { 21 }, FLAGS_FIX_MTU },
321 { MARVELL_ORION_1_88W8660, 0, 1, { 21 }, FLAGS_FIX_MTU },
322
323 { MARVELL_KIRKWOOD_88F6180, 0, 1, { 11 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
324 { MARVELL_KIRKWOOD_88F6192, 0, 1, { 11 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
325 { MARVELL_KIRKWOOD_88F6192, 1, 1, { 15 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
326 { MARVELL_KIRKWOOD_88F6281, 0, 1, { 11 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
327 { MARVELL_KIRKWOOD_88F6281, 1, 1, { 15 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
328 { MARVELL_KIRKWOOD_88F6282, 0, 1, { 11 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
329 { MARVELL_KIRKWOOD_88F6282, 1, 1, { 15 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
330
331 { MARVELL_MV78XX0_MV78100, 0, 1, { 40 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
332 { MARVELL_MV78XX0_MV78100, 1, 1, { 44 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
333 { MARVELL_MV78XX0_MV78200, 0, 1, { 40 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
334 { MARVELL_MV78XX0_MV78200, 1, 1, { 44 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
335 { MARVELL_MV78XX0_MV78200, 2, 1, { 48 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
336 { MARVELL_MV78XX0_MV78200, 3, 1, { 52 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
337 };
338
339
340 /* ARGSUSED */
341 static int
342 mvgbec_match(device_t parent, cfdata_t match, void *aux)
343 {
344 struct marvell_attach_args *mva = aux;
345 int i;
346
347 if (strcmp(mva->mva_name, match->cf_name) != 0)
348 return 0;
349 if (mva->mva_offset == MVA_OFFSET_DEFAULT)
350 return 0;
351
352 for (i = 0; i < __arraycount(mvgbe_ports); i++)
353 if (mva->mva_model == mvgbe_ports[i].model) {
354 mva->mva_size = MVGBE_SIZE;
355 return 1;
356 }
357 return 0;
358 }
359
360 /* ARGSUSED */
361 static void
362 mvgbec_attach(device_t parent, device_t self, void *aux)
363 {
364 struct mvgbec_softc *csc = device_private(self);
365 struct marvell_attach_args *mva = aux, gbea;
366 struct mvgbe_softc *port;
367 struct mii_softc *mii;
368 device_t child;
369 uint32_t phyaddr;
370 int i, j;
371
372 aprint_naive("\n");
373 aprint_normal(": Marvell Gigabit Ethernet Controller\n");
374
375 csc->sc_dev = self;
376 csc->sc_iot = mva->mva_iot;
377 if (bus_space_subregion(mva->mva_iot, mva->mva_ioh, mva->mva_offset,
378 mva->mva_size, &csc->sc_ioh)) {
379 aprint_error_dev(self, "Cannot map registers\n");
380 return;
381 }
382
383 if (mvgbec0 == NULL)
384 mvgbec0 = self;
385
386 phyaddr = 0;
387 MVGBE_WRITE(csc, MVGBE_PHYADDR, phyaddr);
388
389 mutex_init(&csc->sc_mtx, MUTEX_DEFAULT, IPL_NET);
390
391 /* Disable and clear Gigabit Ethernet Unit interrupts */
392 MVGBE_WRITE(csc, MVGBE_EUIM, 0);
393 MVGBE_WRITE(csc, MVGBE_EUIC, 0);
394
395 mvgbec_wininit(csc);
396
397 memset(&gbea, 0, sizeof(gbea));
398 for (i = 0; i < __arraycount(mvgbe_ports); i++) {
399 if (mvgbe_ports[i].model != mva->mva_model ||
400 mvgbe_ports[i].unit != mva->mva_unit)
401 continue;
402
403 csc->sc_flags = mvgbe_ports[i].flags;
404
405 for (j = 0; j < mvgbe_ports[i].ports; j++) {
406 gbea.mva_name = "mvgbe";
407 gbea.mva_model = mva->mva_model;
408 gbea.mva_iot = csc->sc_iot;
409 gbea.mva_ioh = csc->sc_ioh;
410 gbea.mva_unit = j;
411 gbea.mva_dmat = mva->mva_dmat;
412 gbea.mva_irq = mvgbe_ports[i].irqs[j];
413 child = config_found_sm_loc(csc->sc_dev, "mvgbec", NULL,
414 &gbea, mvgbec_print, mvgbec_search);
415 if (child) {
416 port = device_private(child);
417 mii = LIST_FIRST(&port->sc_mii.mii_phys);
418 phyaddr |= MVGBE_PHYADDR_PHYAD(j, mii->mii_phy);
419 }
420 }
421 break;
422 }
423 MVGBE_WRITE(csc, MVGBE_PHYADDR, phyaddr);
424 }
425
426 static int
427 mvgbec_print(void *aux, const char *pnp)
428 {
429 struct marvell_attach_args *gbea = aux;
430
431 if (pnp)
432 aprint_normal("%s at %s port %d",
433 gbea->mva_name, pnp, gbea->mva_unit);
434 else {
435 if (gbea->mva_unit != MVGBECCF_PORT_DEFAULT)
436 aprint_normal(" port %d", gbea->mva_unit);
437 if (gbea->mva_irq != MVGBECCF_IRQ_DEFAULT)
438 aprint_normal(" irq %d", gbea->mva_irq);
439 }
440 return UNCONF;
441 }
442
443 /* ARGSUSED */
444 static int
445 mvgbec_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
446 {
447 struct marvell_attach_args *gbea = aux;
448
449 if (cf->cf_loc[MVGBECCF_PORT] == gbea->mva_unit &&
450 cf->cf_loc[MVGBECCF_IRQ] != MVGBECCF_IRQ_DEFAULT)
451 gbea->mva_irq = cf->cf_loc[MVGBECCF_IRQ];
452
453 return config_match(parent, cf, aux);
454 }
455
456 static int
457 mvgbec_miibus_readreg(device_t dev, int phy, int reg)
458 {
459 struct mvgbe_softc *sc = device_private(dev);
460 struct mvgbec_softc *csc;
461 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
462 uint32_t smi, val;
463 int i;
464
465 if (mvgbec0 == NULL) {
466 aprint_error_ifnet(ifp, "SMI mvgbec0 not found\n");
467 return -1;
468 }
469 csc = device_private(mvgbec0);
470
471 mutex_enter(&csc->sc_mtx);
472
473 for (i = 0; i < MVGBE_PHY_TIMEOUT; i++) {
474 DELAY(1);
475 if (!(MVGBE_READ(csc, MVGBE_SMI) & MVGBE_SMI_BUSY))
476 break;
477 }
478 if (i == MVGBE_PHY_TIMEOUT) {
479 aprint_error_ifnet(ifp, "SMI busy timeout\n");
480 mutex_exit(&csc->sc_mtx);
481 return -1;
482 }
483
484 smi =
485 MVGBE_SMI_PHYAD(phy) | MVGBE_SMI_REGAD(reg) | MVGBE_SMI_OPCODE_READ;
486 MVGBE_WRITE(csc, MVGBE_SMI, smi);
487
488 for (i = 0; i < MVGBE_PHY_TIMEOUT; i++) {
489 DELAY(1);
490 smi = MVGBE_READ(csc, MVGBE_SMI);
491 if (smi & MVGBE_SMI_READVALID)
492 break;
493 }
494
495 mutex_exit(&csc->sc_mtx);
496
497 DPRINTFN(9, ("mvgbec_miibus_readreg: i=%d, timeout=%d\n",
498 i, MVGBE_PHY_TIMEOUT));
499
500 val = smi & MVGBE_SMI_DATA_MASK;
501
502 DPRINTFN(9, ("mvgbec_miibus_readreg phy=%d, reg=%#x, val=%#x\n",
503 phy, reg, val));
504
505 return val;
506 }
507
508 static void
509 mvgbec_miibus_writereg(device_t dev, int phy, int reg, int val)
510 {
511 struct mvgbe_softc *sc = device_private(dev);
512 struct mvgbec_softc *csc;
513 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
514 uint32_t smi;
515 int i;
516
517 if (mvgbec0 == NULL) {
518 aprint_error_ifnet(ifp, "SMI mvgbec0 not found\n");
519 return;
520 }
521 csc = device_private(mvgbec0);
522
523 DPRINTFN(9, ("mvgbec_miibus_writereg phy=%d reg=%#x val=%#x\n",
524 phy, reg, val));
525
526 mutex_enter(&csc->sc_mtx);
527
528 for (i = 0; i < MVGBE_PHY_TIMEOUT; i++) {
529 DELAY(1);
530 if (!(MVGBE_READ(csc, MVGBE_SMI) & MVGBE_SMI_BUSY))
531 break;
532 }
533 if (i == MVGBE_PHY_TIMEOUT) {
534 aprint_error_ifnet(ifp, "SMI busy timeout\n");
535 mutex_exit(&csc->sc_mtx);
536 return;
537 }
538
539 smi = MVGBE_SMI_PHYAD(phy) | MVGBE_SMI_REGAD(reg) |
540 MVGBE_SMI_OPCODE_WRITE | (val & MVGBE_SMI_DATA_MASK);
541 MVGBE_WRITE(csc, MVGBE_SMI, smi);
542
543 for (i = 0; i < MVGBE_PHY_TIMEOUT; i++) {
544 DELAY(1);
545 if (!(MVGBE_READ(csc, MVGBE_SMI) & MVGBE_SMI_BUSY))
546 break;
547 }
548
549 mutex_exit(&csc->sc_mtx);
550
551 if (i == MVGBE_PHY_TIMEOUT)
552 aprint_error_ifnet(ifp, "phy write timed out\n");
553 }
554
555 static void
556 mvgbec_miibus_statchg(struct ifnet *ifp)
557 {
558
559 /* nothing to do */
560 }
561
562
563 static void
564 mvgbec_wininit(struct mvgbec_softc *sc)
565 {
566 device_t pdev = device_parent(sc->sc_dev);
567 uint64_t base;
568 uint32_t en, ac, size;
569 int window, target, attr, rv, i;
570 static int tags[] = {
571 MARVELL_TAG_SDRAM_CS0,
572 MARVELL_TAG_SDRAM_CS1,
573 MARVELL_TAG_SDRAM_CS2,
574 MARVELL_TAG_SDRAM_CS3,
575
576 MARVELL_TAG_UNDEFINED,
577 };
578
579 /* First disable all address decode windows */
580 en = MVGBE_BARE_EN_MASK;
581 MVGBE_WRITE(sc, MVGBE_BARE, en);
582
583 ac = 0;
584 for (window = 0, i = 0;
585 tags[i] != MARVELL_TAG_UNDEFINED && window < MVGBE_NWINDOW; i++) {
586 rv = marvell_winparams_by_tag(pdev, tags[i],
587 &target, &attr, &base, &size);
588 if (rv != 0 || size == 0)
589 continue;
590
591 if (base > 0xffffffffULL) {
592 if (window >= MVGBE_NREMAP) {
593 aprint_error_dev(sc->sc_dev,
594 "can't remap window %d\n", window);
595 continue;
596 }
597 MVGBE_WRITE(sc, MVGBE_HA(window),
598 (base >> 32) & 0xffffffff);
599 }
600
601 MVGBE_WRITE(sc, MVGBE_BASEADDR(window),
602 MVGBE_BASEADDR_TARGET(target) |
603 MVGBE_BASEADDR_ATTR(attr) |
604 MVGBE_BASEADDR_BASE(base));
605 MVGBE_WRITE(sc, MVGBE_S(window), MVGBE_S_SIZE(size));
606
607 en &= ~(1 << window);
608 /* set full access (r/w) */
609 ac |= MVGBE_EPAP_EPAR(window, MVGBE_EPAP_AC_FA);
610 window++;
611 }
612 /* allow to access decode window */
613 MVGBE_WRITE(sc, MVGBE_EPAP, ac);
614
615 MVGBE_WRITE(sc, MVGBE_BARE, en);
616 }
617
618
619 /* ARGSUSED */
620 static int
621 mvgbe_match(device_t parent, cfdata_t match, void *aux)
622 {
623 struct marvell_attach_args *mva = aux;
624 uint32_t pbase, maddrh, maddrl;
625
626 pbase = MVGBE_PORTR_BASE + mva->mva_unit * MVGBE_PORTR_SIZE;
627 maddrh =
628 bus_space_read_4(mva->mva_iot, mva->mva_ioh, pbase + MVGBE_MACAH);
629 maddrl =
630 bus_space_read_4(mva->mva_iot, mva->mva_ioh, pbase + MVGBE_MACAL);
631 if ((maddrh | maddrl) == 0)
632 return 0;
633
634 return 1;
635 }
636
637 /* ARGSUSED */
638 static void
639 mvgbe_attach(device_t parent, device_t self, void *aux)
640 {
641 struct mvgbe_softc *sc = device_private(self);
642 struct marvell_attach_args *mva = aux;
643 struct mvgbe_txmap_entry *entry;
644 struct ifnet *ifp;
645 bus_dma_segment_t seg;
646 bus_dmamap_t dmamap;
647 int rseg, i;
648 uint32_t maddrh, maddrl;
649 void *kva;
650
651 aprint_naive("\n");
652 aprint_normal("\n");
653
654 sc->sc_dev = self;
655 sc->sc_port = mva->mva_unit;
656 sc->sc_iot = mva->mva_iot;
657 callout_init(&sc->sc_tick_ch, 0);
658 callout_setfunc(&sc->sc_tick_ch, mvgbe_tick, sc);
659 if (bus_space_subregion(mva->mva_iot, mva->mva_ioh,
660 MVGBE_PORTR_BASE + mva->mva_unit * MVGBE_PORTR_SIZE,
661 MVGBE_PORTR_SIZE, &sc->sc_ioh)) {
662 aprint_error_dev(self, "Cannot map registers\n");
663 return;
664 }
665 if (bus_space_subregion(mva->mva_iot, mva->mva_ioh,
666 MVGBE_PORTDAFR_BASE + mva->mva_unit * MVGBE_PORTDAFR_SIZE,
667 MVGBE_PORTDAFR_SIZE, &sc->sc_dafh)) {
668 aprint_error_dev(self,
669 "Cannot map destination address filter registers\n");
670 return;
671 }
672 sc->sc_dmat = mva->mva_dmat;
673
674 maddrh = MVGBE_READ(sc, MVGBE_MACAH);
675 maddrl = MVGBE_READ(sc, MVGBE_MACAL);
676 sc->sc_enaddr[0] = maddrh >> 24;
677 sc->sc_enaddr[1] = maddrh >> 16;
678 sc->sc_enaddr[2] = maddrh >> 8;
679 sc->sc_enaddr[3] = maddrh >> 0;
680 sc->sc_enaddr[4] = maddrl >> 8;
681 sc->sc_enaddr[5] = maddrl >> 0;
682 aprint_normal_dev(self, "Ethernet address %s\n",
683 ether_sprintf(sc->sc_enaddr));
684
685 /* clear all ethernet port interrupts */
686 MVGBE_WRITE(sc, MVGBE_IC, 0);
687 MVGBE_WRITE(sc, MVGBE_ICE, 0);
688
689 marvell_intr_establish(mva->mva_irq, IPL_NET, mvgbe_intr, sc);
690
691 /* Allocate the descriptor queues. */
692 if (bus_dmamem_alloc(sc->sc_dmat, sizeof(struct mvgbe_ring_data),
693 PAGE_SIZE, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) {
694 aprint_error_dev(self, "can't alloc rx buffers\n");
695 return;
696 }
697 if (bus_dmamem_map(sc->sc_dmat, &seg, rseg,
698 sizeof(struct mvgbe_ring_data), &kva, BUS_DMA_NOWAIT)) {
699 aprint_error_dev(self, "can't map dma buffers (%lu bytes)\n",
700 (u_long)sizeof(struct mvgbe_ring_data));
701 goto fail1;
702 }
703 if (bus_dmamap_create(sc->sc_dmat, sizeof(struct mvgbe_ring_data), 1,
704 sizeof(struct mvgbe_ring_data), 0, BUS_DMA_NOWAIT,
705 &sc->sc_ring_map)) {
706 aprint_error_dev(self, "can't create dma map\n");
707 goto fail2;
708 }
709 if (bus_dmamap_load(sc->sc_dmat, sc->sc_ring_map, kva,
710 sizeof(struct mvgbe_ring_data), NULL, BUS_DMA_NOWAIT)) {
711 aprint_error_dev(self, "can't load dma map\n");
712 goto fail3;
713 }
714 for (i = 0; i < MVGBE_RX_RING_CNT; i++)
715 sc->sc_cdata.mvgbe_rx_chain[i].mvgbe_mbuf = NULL;
716
717 SIMPLEQ_INIT(&sc->sc_txmap_head);
718 for (i = 0; i < MVGBE_TX_RING_CNT; i++) {
719 sc->sc_cdata.mvgbe_tx_chain[i].mvgbe_mbuf = NULL;
720
721 if (bus_dmamap_create(sc->sc_dmat,
722 MVGBE_JLEN, MVGBE_NTXSEG, MVGBE_JLEN, 0,
723 BUS_DMA_NOWAIT, &dmamap)) {
724 aprint_error_dev(self, "Can't create TX dmamap\n");
725 goto fail4;
726 }
727
728 entry = kmem_alloc(sizeof(*entry), KM_SLEEP);
729 if (!entry) {
730 aprint_error_dev(self, "Can't alloc txmap entry\n");
731 bus_dmamap_destroy(sc->sc_dmat, dmamap);
732 goto fail4;
733 }
734 entry->dmamap = dmamap;
735 SIMPLEQ_INSERT_HEAD(&sc->sc_txmap_head, entry, link);
736 }
737
738 sc->sc_rdata = (struct mvgbe_ring_data *)kva;
739 memset(sc->sc_rdata, 0, sizeof(struct mvgbe_ring_data));
740
741 /*
742 * We can support 802.1Q VLAN-sized frames and jumbo
743 * Ethernet frames.
744 */
745 sc->sc_ethercom.ec_capabilities |=
746 ETHERCAP_VLAN_MTU | ETHERCAP_JUMBO_MTU;
747
748 /* Try to allocate memory for jumbo buffers. */
749 if (mvgbe_alloc_jumbo_mem(sc)) {
750 aprint_error_dev(self, "jumbo buffer allocation failed\n");
751 goto fail4;
752 }
753
754 ifp = &sc->sc_ethercom.ec_if;
755 ifp->if_softc = sc;
756 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
757 ifp->if_start = mvgbe_start;
758 ifp->if_ioctl = mvgbe_ioctl;
759 ifp->if_init = mvgbe_init;
760 ifp->if_stop = mvgbe_stop;
761 ifp->if_watchdog = mvgbe_watchdog;
762 /*
763 * We can do IPv4/TCPv4/UDPv4 checksums in hardware.
764 */
765 sc->sc_ethercom.ec_if.if_capabilities |=
766 IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx |
767 IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx |
768 IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx;
769 /*
770 * But, IPv6 packets in the stream can cause incorrect TCPv4 Tx sums.
771 */
772 sc->sc_ethercom.ec_if.if_capabilities &= ~IFCAP_CSUM_TCPv4_Tx;
773 IFQ_SET_MAXLEN(&ifp->if_snd, max(MVGBE_TX_RING_CNT - 1, IFQ_MAXLEN));
774 IFQ_SET_READY(&ifp->if_snd);
775 strcpy(ifp->if_xname, device_xname(sc->sc_dev));
776
777 mvgbe_stop(ifp, 0);
778
779 /*
780 * Do MII setup.
781 */
782 sc->sc_mii.mii_ifp = ifp;
783 sc->sc_mii.mii_readreg = mvgbec_miibus_readreg;
784 sc->sc_mii.mii_writereg = mvgbec_miibus_writereg;
785 sc->sc_mii.mii_statchg = mvgbec_miibus_statchg;
786
787 sc->sc_ethercom.ec_mii = &sc->sc_mii;
788 ifmedia_init(&sc->sc_mii.mii_media, 0,
789 mvgbe_mediachange, mvgbe_mediastatus);
790 mii_attach(self, &sc->sc_mii, 0xffffffff,
791 MII_PHY_ANY, parent == mvgbec0 ? 0 : 1, 0);
792 if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
793 aprint_error_dev(self, "no PHY found!\n");
794 ifmedia_add(&sc->sc_mii.mii_media,
795 IFM_ETHER|IFM_MANUAL, 0, NULL);
796 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_MANUAL);
797 } else
798 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
799
800 /*
801 * Call MI attach routines.
802 */
803 if_attach(ifp);
804
805 ether_ifattach(ifp, sc->sc_enaddr);
806 ether_set_ifflags_cb(&sc->sc_ethercom, mvgbe_ifflags_cb);
807
808 sysctl_mvgbe_init(sc);
809 rnd_attach_source(&sc->sc_rnd_source, device_xname(sc->sc_dev),
810 RND_TYPE_NET, 0);
811
812 return;
813
814 fail4:
815 while ((entry = SIMPLEQ_FIRST(&sc->sc_txmap_head)) != NULL) {
816 SIMPLEQ_REMOVE_HEAD(&sc->sc_txmap_head, link);
817 bus_dmamap_destroy(sc->sc_dmat, entry->dmamap);
818 }
819 bus_dmamap_unload(sc->sc_dmat, sc->sc_ring_map);
820 fail3:
821 bus_dmamap_destroy(sc->sc_dmat, sc->sc_ring_map);
822 fail2:
823 bus_dmamem_unmap(sc->sc_dmat, kva, sizeof(struct mvgbe_ring_data));
824 fail1:
825 bus_dmamem_free(sc->sc_dmat, &seg, rseg);
826 return;
827 }
828
829 static int
830 mvgbe_ipginttx(struct mvgbec_softc *csc, struct mvgbe_softc *sc,
831 unsigned int ipginttx)
832 {
833 uint32_t reg;
834 reg = MVGBE_READ(sc, MVGBE_PTFUT);
835
836 if (csc->sc_flags & FLAGS_IPG2) {
837 if (ipginttx > MVGBE_PTFUT_IPGINTTX_V2_MAX)
838 return -1;
839 reg &= ~MVGBE_PTFUT_IPGINTTX_V2_MASK;
840 reg |= MVGBE_PTFUT_IPGINTTX_V2(ipginttx);
841 } else if (csc->sc_flags & FLAGS_IPG1) {
842 if (ipginttx > MVGBE_PTFUT_IPGINTTX_V1_MAX)
843 return -1;
844 reg &= ~MVGBE_PTFUT_IPGINTTX_V1_MASK;
845 reg |= MVGBE_PTFUT_IPGINTTX_V1(ipginttx);
846 }
847 MVGBE_WRITE(sc, MVGBE_PTFUT, reg);
848
849 return 0;
850 }
851
852 static int
853 mvgbe_ipgintrx(struct mvgbec_softc *csc, struct mvgbe_softc *sc,
854 unsigned int ipgintrx)
855 {
856 uint32_t reg;
857 reg = MVGBE_READ(sc, MVGBE_SDC);
858
859 if (csc->sc_flags & FLAGS_IPG2) {
860 if (ipgintrx > MVGBE_SDC_IPGINTRX_V2_MAX)
861 return -1;
862 reg &= ~MVGBE_SDC_IPGINTRX_V2_MASK;
863 reg |= MVGBE_SDC_IPGINTRX_V2(ipgintrx);
864 } else if (csc->sc_flags & FLAGS_IPG1) {
865 if (ipgintrx > MVGBE_SDC_IPGINTRX_V1_MAX)
866 return -1;
867 reg &= ~MVGBE_SDC_IPGINTRX_V1_MASK;
868 reg |= MVGBE_SDC_IPGINTRX_V1(ipgintrx);
869 }
870 MVGBE_WRITE(sc, MVGBE_SDC, reg);
871
872 return 0;
873 }
874
875 static void
876 mvgbe_tick(void *arg)
877 {
878 struct mvgbe_softc *sc = arg;
879 struct mii_data *mii = &sc->sc_mii;
880 int s;
881
882 s = splnet();
883 mii_tick(mii);
884 /* Need more work */
885 splx(s);
886
887 callout_schedule(&sc->sc_tick_ch, hz);
888 }
889
890 static int
891 mvgbe_intr(void *arg)
892 {
893 struct mvgbe_softc *sc = arg;
894 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
895 uint32_t ic, ice, datum = 0;
896 int claimed = 0;
897
898 for (;;) {
899 ice = MVGBE_READ(sc, MVGBE_ICE);
900 ic = MVGBE_READ(sc, MVGBE_IC);
901
902 DPRINTFN(3, ("mvgbe_intr: ic=%#x, ice=%#x\n", ic, ice));
903 if (ic == 0 && ice == 0)
904 break;
905
906 datum = datum ^ ic ^ ice;
907
908 MVGBE_WRITE(sc, MVGBE_IC, ~ic);
909 MVGBE_WRITE(sc, MVGBE_ICE, ~ice);
910
911 claimed = 1;
912
913 if (!(ifp->if_flags & IFF_RUNNING))
914 break;
915
916 if (ice & MVGBE_ICE_LINKCHG) {
917 if (MVGBE_READ(sc, MVGBE_PS) & MVGBE_PS_LINKUP) {
918 /* Enable port RX and TX. */
919 MVGBE_WRITE(sc, MVGBE_RQC, MVGBE_RQC_ENQ(0));
920 MVGBE_WRITE(sc, MVGBE_TQC, MVGBE_TQC_ENQ);
921 } else {
922 MVGBE_WRITE(sc, MVGBE_RQC, MVGBE_RQC_DISQ(0));
923 MVGBE_WRITE(sc, MVGBE_TQC, MVGBE_TQC_DISQ);
924 }
925 }
926
927 if (ic & (MVGBE_IC_RXBUF | MVGBE_IC_RXERROR))
928 mvgbe_rxeof(sc);
929
930 if (ice & (MVGBE_ICE_TXBUF | MVGBE_ICE_TXERR))
931 mvgbe_txeof(sc);
932 }
933
934 if (!IFQ_IS_EMPTY(&ifp->if_snd))
935 mvgbe_start(ifp);
936
937 rnd_add_uint32(&sc->sc_rnd_source, datum);
938
939 return claimed;
940 }
941
942 static void
943 mvgbe_start(struct ifnet *ifp)
944 {
945 struct mvgbe_softc *sc = ifp->if_softc;
946 struct mbuf *m_head = NULL;
947 uint32_t idx = sc->sc_cdata.mvgbe_tx_prod;
948 int pkts = 0;
949
950 DPRINTFN(3, ("mvgbe_start (idx %d, tx_chain[idx] %p)\n", idx,
951 sc->sc_cdata.mvgbe_tx_chain[idx].mvgbe_mbuf));
952
953 if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
954 return;
955 /* If Link is DOWN, can't start TX */
956 if (!(MVGBE_READ(sc, MVGBE_PS) & MVGBE_PS_LINKUP))
957 return;
958
959 while (sc->sc_cdata.mvgbe_tx_chain[idx].mvgbe_mbuf == NULL) {
960 IFQ_POLL(&ifp->if_snd, m_head);
961 if (m_head == NULL)
962 break;
963
964 /*
965 * Pack the data into the transmit ring. If we
966 * don't have room, set the OACTIVE flag and wait
967 * for the NIC to drain the ring.
968 */
969 if (mvgbe_encap(sc, m_head, &idx)) {
970 ifp->if_flags |= IFF_OACTIVE;
971 break;
972 }
973
974 /* now we are committed to transmit the packet */
975 IFQ_DEQUEUE(&ifp->if_snd, m_head);
976 pkts++;
977
978 /*
979 * If there's a BPF listener, bounce a copy of this frame
980 * to him.
981 */
982 bpf_mtap(ifp, m_head);
983 }
984 if (pkts == 0)
985 return;
986
987 /* Transmit at Queue 0 */
988 if (idx != sc->sc_cdata.mvgbe_tx_prod) {
989 sc->sc_cdata.mvgbe_tx_prod = idx;
990 MVGBE_WRITE(sc, MVGBE_TQC, MVGBE_TQC_ENQ);
991
992 /*
993 * Set a timeout in case the chip goes out to lunch.
994 */
995 ifp->if_timer = 1;
996 sc->sc_wdogsoft = 1;
997 }
998 }
999
1000 static int
1001 mvgbe_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1002 {
1003 struct mvgbe_softc *sc = ifp->if_softc;
1004 struct ifreq *ifr = data;
1005 int s, error = 0;
1006
1007 s = splnet();
1008
1009 switch (cmd) {
1010 case SIOCGIFMEDIA:
1011 case SIOCSIFMEDIA:
1012 DPRINTFN(2, ("mvgbe_ioctl MEDIA\n"));
1013 error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
1014 break;
1015 default:
1016 DPRINTFN(2, ("mvgbe_ioctl ETHER\n"));
1017 error = ether_ioctl(ifp, cmd, data);
1018 if (error == ENETRESET) {
1019 if (ifp->if_flags & IFF_RUNNING) {
1020 mvgbe_filter_setup(sc);
1021 }
1022 error = 0;
1023 }
1024 break;
1025 }
1026
1027 splx(s);
1028
1029 return error;
1030 }
1031
1032 static int
1033 mvgbe_init(struct ifnet *ifp)
1034 {
1035 struct mvgbe_softc *sc = ifp->if_softc;
1036 struct mvgbec_softc *csc = device_private(device_parent(sc->sc_dev));
1037 struct mii_data *mii = &sc->sc_mii;
1038 uint32_t reg;
1039 int i;
1040
1041 DPRINTFN(2, ("mvgbe_init\n"));
1042
1043 /* Cancel pending I/O and free all RX/TX buffers. */
1044 mvgbe_stop(ifp, 0);
1045
1046 /* clear all ethernet port interrupts */
1047 MVGBE_WRITE(sc, MVGBE_IC, 0);
1048 MVGBE_WRITE(sc, MVGBE_ICE, 0);
1049
1050 /* Init TX/RX descriptors */
1051 if (mvgbe_init_tx_ring(sc) == ENOBUFS) {
1052 aprint_error_ifnet(ifp,
1053 "initialization failed: no memory for tx buffers\n");
1054 return ENOBUFS;
1055 }
1056 if (mvgbe_init_rx_ring(sc) == ENOBUFS) {
1057 aprint_error_ifnet(ifp,
1058 "initialization failed: no memory for rx buffers\n");
1059 return ENOBUFS;
1060 }
1061
1062 if ((csc->sc_flags & FLAGS_IPG1) || (csc->sc_flags & FLAGS_IPG2)) {
1063 sc->sc_ipginttx = MVGBE_IPGINTTX_DEFAULT;
1064 sc->sc_ipgintrx = MVGBE_IPGINTRX_DEFAULT;
1065 }
1066 if (csc->sc_flags & FLAGS_FIX_MTU)
1067 MVGBE_WRITE(sc, MVGBE_MTU, 0); /* hw reset value is wrong */
1068 MVGBE_WRITE(sc, MVGBE_PSC,
1069 MVGBE_PSC_ANFC | /* Enable Auto-Neg Flow Ctrl */
1070 MVGBE_PSC_RESERVED | /* Must be set to 1 */
1071 MVGBE_PSC_FLFAIL | /* Do NOT Force Link Fail */
1072 MVGBE_PSC_MRU(MVGBE_PSC_MRU_9022) | /* we want 9k */
1073 MVGBE_PSC_SETFULLDX); /* Set_FullDx */
1074 /* XXXX: mvgbe(4) always use RGMII. */
1075 MVGBE_WRITE(sc, MVGBE_PSC1,
1076 MVGBE_READ(sc, MVGBE_PSC1) | MVGBE_PSC1_RGMIIEN);
1077 /* XXXX: Also always Weighted Round-Robin Priority Mode */
1078 MVGBE_WRITE(sc, MVGBE_TQFPC, MVGBE_TQFPC_EN(0));
1079
1080 MVGBE_WRITE(sc, MVGBE_CRDP(0), MVGBE_RX_RING_ADDR(sc, 0));
1081 MVGBE_WRITE(sc, MVGBE_TCQDP, MVGBE_TX_RING_ADDR(sc, 0));
1082
1083 if (csc->sc_flags & FLAGS_FIX_TQTB) {
1084 /*
1085 * Queue 0 (offset 0x72700) must be programmed to 0x3fffffff.
1086 * And offset 0x72704 must be programmed to 0x03ffffff.
1087 * Queue 1 through 7 must be programmed to 0x0.
1088 */
1089 MVGBE_WRITE(sc, MVGBE_TQTBCOUNT(0), 0x3fffffff);
1090 MVGBE_WRITE(sc, MVGBE_TQTBCONFIG(0), 0x03ffffff);
1091 for (i = 1; i < 8; i++) {
1092 MVGBE_WRITE(sc, MVGBE_TQTBCOUNT(i), 0x0);
1093 MVGBE_WRITE(sc, MVGBE_TQTBCONFIG(i), 0x0);
1094 }
1095 } else
1096 for (i = 1; i < 8; i++) {
1097 MVGBE_WRITE(sc, MVGBE_TQTBCOUNT(i), 0x3fffffff);
1098 MVGBE_WRITE(sc, MVGBE_TQTBCONFIG(i), 0xffff7fff);
1099 MVGBE_WRITE(sc, MVGBE_TQAC(i), 0xfc0000ff);
1100 }
1101
1102 MVGBE_WRITE(sc, MVGBE_PXC, MVGBE_PXC_RXCS);
1103 MVGBE_WRITE(sc, MVGBE_PXCX, 0);
1104
1105 /* Set SDC register except IPGINT bits */
1106 MVGBE_WRITE(sc, MVGBE_SDC,
1107 MVGBE_SDC_RXBSZ_16_64BITWORDS |
1108 #if BYTE_ORDER == LITTLE_ENDIAN
1109 MVGBE_SDC_BLMR | /* Big/Little Endian Receive Mode: No swap */
1110 MVGBE_SDC_BLMT | /* Big/Little Endian Transmit Mode: No swap */
1111 #endif
1112 MVGBE_SDC_TXBSZ_16_64BITWORDS);
1113 /* And then set IPGINT bits */
1114 mvgbe_ipgintrx(csc, sc, sc->sc_ipgintrx);
1115
1116 /* Tx side */
1117 MVGBE_WRITE(sc, MVGBE_PTFUT, 0);
1118 mvgbe_ipginttx(csc, sc, sc->sc_ipginttx);
1119
1120 mvgbe_filter_setup(sc);
1121
1122 mii_mediachg(mii);
1123
1124 /* Enable port */
1125 reg = MVGBE_READ(sc, MVGBE_PSC);
1126 MVGBE_WRITE(sc, MVGBE_PSC, reg | MVGBE_PSC_PORTEN);
1127
1128 /* If Link is UP, Start RX and TX traffic */
1129 if (MVGBE_READ(sc, MVGBE_PS) & MVGBE_PS_LINKUP) {
1130 /* Enable port RX/TX. */
1131 MVGBE_WRITE(sc, MVGBE_RQC, MVGBE_RQC_ENQ(0));
1132 MVGBE_WRITE(sc, MVGBE_TQC, MVGBE_TQC_ENQ);
1133 }
1134
1135 /* Enable interrupt masks */
1136 MVGBE_WRITE(sc, MVGBE_PIM,
1137 MVGBE_IC_RXBUF |
1138 MVGBE_IC_EXTEND |
1139 MVGBE_IC_RXBUFQ_MASK |
1140 MVGBE_IC_RXERROR |
1141 MVGBE_IC_RXERRQ_MASK);
1142 MVGBE_WRITE(sc, MVGBE_PEIM,
1143 MVGBE_ICE_TXBUF |
1144 MVGBE_ICE_TXERR |
1145 MVGBE_ICE_LINKCHG);
1146
1147 callout_schedule(&sc->sc_tick_ch, hz);
1148
1149 ifp->if_flags |= IFF_RUNNING;
1150 ifp->if_flags &= ~IFF_OACTIVE;
1151
1152 return 0;
1153 }
1154
1155 /* ARGSUSED */
1156 static void
1157 mvgbe_stop(struct ifnet *ifp, int disable)
1158 {
1159 struct mvgbe_softc *sc = ifp->if_softc;
1160 struct mvgbec_softc *csc = device_private(device_parent(sc->sc_dev));
1161 struct mvgbe_chain_data *cdata = &sc->sc_cdata;
1162 uint32_t reg;
1163 int i, cnt;
1164
1165 DPRINTFN(2, ("mvgbe_stop\n"));
1166
1167 callout_stop(&sc->sc_tick_ch);
1168
1169 /* Stop Rx port activity. Check port Rx activity. */
1170 reg = MVGBE_READ(sc, MVGBE_RQC);
1171 if (reg & MVGBE_RQC_ENQ_MASK)
1172 /* Issue stop command for active channels only */
1173 MVGBE_WRITE(sc, MVGBE_RQC, MVGBE_RQC_DISQ_DISABLE(reg));
1174
1175 /* Stop Tx port activity. Check port Tx activity. */
1176 if (MVGBE_READ(sc, MVGBE_TQC) & MVGBE_TQC_ENQ)
1177 MVGBE_WRITE(sc, MVGBE_TQC, MVGBE_TQC_DISQ);
1178
1179 /* Force link down */
1180 reg = MVGBE_READ(sc, MVGBE_PSC);
1181 MVGBE_WRITE(sc, MVGBE_PSC, reg & ~MVGBE_PSC_FLFAIL);
1182
1183 #define RX_DISABLE_TIMEOUT 0x1000000
1184 #define TX_FIFO_EMPTY_TIMEOUT 0x1000000
1185 /* Wait for all Rx activity to terminate. */
1186 cnt = 0;
1187 do {
1188 if (cnt >= RX_DISABLE_TIMEOUT) {
1189 aprint_error_ifnet(ifp,
1190 "timeout for RX stopped. rqc 0x%x\n", reg);
1191 break;
1192 }
1193 cnt++;
1194
1195 /*
1196 * Check Receive Queue Command register that all Rx queues
1197 * are stopped
1198 */
1199 reg = MVGBE_READ(sc, MVGBE_RQC);
1200 } while (reg & 0xff);
1201
1202 /* Double check to verify that TX FIFO is empty */
1203 cnt = 0;
1204 while (1) {
1205 do {
1206 if (cnt >= TX_FIFO_EMPTY_TIMEOUT) {
1207 aprint_error_ifnet(ifp,
1208 "timeout for TX FIFO empty. status 0x%x\n",
1209 reg);
1210 break;
1211 }
1212 cnt++;
1213
1214 reg = MVGBE_READ(sc, MVGBE_PS);
1215 } while
1216 (!(reg & MVGBE_PS_TXFIFOEMP) || reg & MVGBE_PS_TXINPROG);
1217
1218 if (cnt >= TX_FIFO_EMPTY_TIMEOUT)
1219 break;
1220
1221 /* Double check */
1222 reg = MVGBE_READ(sc, MVGBE_PS);
1223 if (reg & MVGBE_PS_TXFIFOEMP && !(reg & MVGBE_PS_TXINPROG))
1224 break;
1225 else
1226 aprint_error_ifnet(ifp,
1227 "TX FIFO empty double check failed."
1228 " %d loops, status 0x%x\n", cnt, reg);
1229 }
1230
1231 /* Reset the Enable bit in the Port Serial Control Register */
1232 reg = MVGBE_READ(sc, MVGBE_PSC);
1233 MVGBE_WRITE(sc, MVGBE_PSC, reg & ~MVGBE_PSC_PORTEN);
1234
1235 /*
1236 * Disable and clear interrupts
1237 * 0) controller interrupt
1238 * 1) port interrupt cause
1239 * 2) port interrupt mask
1240 */
1241 MVGBE_WRITE(csc, MVGBE_EUIM, 0);
1242 MVGBE_WRITE(csc, MVGBE_EUIC, 0);
1243 MVGBE_WRITE(sc, MVGBE_IC, 0);
1244 MVGBE_WRITE(sc, MVGBE_ICE, 0);
1245 MVGBE_WRITE(sc, MVGBE_PIM, 0);
1246 MVGBE_WRITE(sc, MVGBE_PEIM, 0);
1247
1248 /* Free RX and TX mbufs still in the queues. */
1249 for (i = 0; i < MVGBE_RX_RING_CNT; i++) {
1250 if (cdata->mvgbe_rx_chain[i].mvgbe_mbuf != NULL) {
1251 m_freem(cdata->mvgbe_rx_chain[i].mvgbe_mbuf);
1252 cdata->mvgbe_rx_chain[i].mvgbe_mbuf = NULL;
1253 }
1254 }
1255 for (i = 0; i < MVGBE_TX_RING_CNT; i++) {
1256 if (cdata->mvgbe_tx_chain[i].mvgbe_mbuf != NULL) {
1257 m_freem(cdata->mvgbe_tx_chain[i].mvgbe_mbuf);
1258 cdata->mvgbe_tx_chain[i].mvgbe_mbuf = NULL;
1259 }
1260 }
1261
1262 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1263 }
1264
1265 static void
1266 mvgbe_watchdog(struct ifnet *ifp)
1267 {
1268 struct mvgbe_softc *sc = ifp->if_softc;
1269
1270 /*
1271 * Reclaim first as there is a possibility of losing Tx completion
1272 * interrupts.
1273 */
1274 mvgbe_txeof(sc);
1275 if (sc->sc_cdata.mvgbe_tx_cnt != 0) {
1276 if (sc->sc_wdogsoft) {
1277 /*
1278 * There is race condition between CPU and DMA
1279 * engine. When DMA engine encounters queue end,
1280 * it clears MVGBE_TQC_ENQ bit.
1281 */
1282 MVGBE_WRITE(sc, MVGBE_TQC, MVGBE_TQC_ENQ);
1283 ifp->if_timer = 5;
1284 sc->sc_wdogsoft = 0;
1285 } else {
1286 aprint_error_ifnet(ifp, "watchdog timeout\n");
1287
1288 ifp->if_oerrors++;
1289
1290 mvgbe_init(ifp);
1291 }
1292 }
1293 }
1294
1295 static int
1296 mvgbe_ifflags_cb(struct ethercom *ec)
1297 {
1298 struct ifnet *ifp = &ec->ec_if;
1299 struct mvgbe_softc *sc = ifp->if_softc;
1300 int change = ifp->if_flags ^ sc->sc_if_flags;
1301
1302 if (change != 0)
1303 sc->sc_if_flags = ifp->if_flags;
1304
1305 if ((change & ~(IFF_CANTCHANGE|IFF_DEBUG)) != 0)
1306 return ENETRESET;
1307
1308 if ((change & IFF_PROMISC) != 0)
1309 mvgbe_filter_setup(sc);
1310
1311 return 0;
1312 }
1313
1314 /*
1315 * Set media options.
1316 */
1317 static int
1318 mvgbe_mediachange(struct ifnet *ifp)
1319 {
1320 return ether_mediachange(ifp);
1321 }
1322
1323 /*
1324 * Report current media status.
1325 */
1326 static void
1327 mvgbe_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
1328 {
1329 ether_mediastatus(ifp, ifmr);
1330 }
1331
1332
1333 static int
1334 mvgbe_init_rx_ring(struct mvgbe_softc *sc)
1335 {
1336 struct mvgbe_chain_data *cd = &sc->sc_cdata;
1337 struct mvgbe_ring_data *rd = sc->sc_rdata;
1338 int i;
1339
1340 memset(rd->mvgbe_rx_ring, 0,
1341 sizeof(struct mvgbe_rx_desc) * MVGBE_RX_RING_CNT);
1342
1343 for (i = 0; i < MVGBE_RX_RING_CNT; i++) {
1344 cd->mvgbe_rx_chain[i].mvgbe_desc =
1345 &rd->mvgbe_rx_ring[i];
1346 if (i == MVGBE_RX_RING_CNT - 1) {
1347 cd->mvgbe_rx_chain[i].mvgbe_next =
1348 &cd->mvgbe_rx_chain[0];
1349 rd->mvgbe_rx_ring[i].nextdescptr =
1350 MVGBE_RX_RING_ADDR(sc, 0);
1351 } else {
1352 cd->mvgbe_rx_chain[i].mvgbe_next =
1353 &cd->mvgbe_rx_chain[i + 1];
1354 rd->mvgbe_rx_ring[i].nextdescptr =
1355 MVGBE_RX_RING_ADDR(sc, i + 1);
1356 }
1357 }
1358
1359 for (i = 0; i < MVGBE_RX_RING_CNT; i++) {
1360 if (mvgbe_newbuf(sc, i, NULL,
1361 sc->sc_cdata.mvgbe_rx_jumbo_map) == ENOBUFS) {
1362 aprint_error_ifnet(&sc->sc_ethercom.ec_if,
1363 "failed alloc of %dth mbuf\n", i);
1364 return ENOBUFS;
1365 }
1366 }
1367 sc->sc_cdata.mvgbe_rx_prod = 0;
1368 sc->sc_cdata.mvgbe_rx_cons = 0;
1369
1370 return 0;
1371 }
1372
1373 static int
1374 mvgbe_init_tx_ring(struct mvgbe_softc *sc)
1375 {
1376 struct mvgbe_chain_data *cd = &sc->sc_cdata;
1377 struct mvgbe_ring_data *rd = sc->sc_rdata;
1378 int i;
1379
1380 memset(sc->sc_rdata->mvgbe_tx_ring, 0,
1381 sizeof(struct mvgbe_tx_desc) * MVGBE_TX_RING_CNT);
1382
1383 for (i = 0; i < MVGBE_TX_RING_CNT; i++) {
1384 cd->mvgbe_tx_chain[i].mvgbe_desc =
1385 &rd->mvgbe_tx_ring[i];
1386 if (i == MVGBE_TX_RING_CNT - 1) {
1387 cd->mvgbe_tx_chain[i].mvgbe_next =
1388 &cd->mvgbe_tx_chain[0];
1389 rd->mvgbe_tx_ring[i].nextdescptr =
1390 MVGBE_TX_RING_ADDR(sc, 0);
1391 } else {
1392 cd->mvgbe_tx_chain[i].mvgbe_next =
1393 &cd->mvgbe_tx_chain[i + 1];
1394 rd->mvgbe_tx_ring[i].nextdescptr =
1395 MVGBE_TX_RING_ADDR(sc, i + 1);
1396 }
1397 rd->mvgbe_tx_ring[i].cmdsts = MVGBE_BUFFER_OWNED_BY_HOST;
1398 }
1399
1400 sc->sc_cdata.mvgbe_tx_prod = 0;
1401 sc->sc_cdata.mvgbe_tx_cons = 0;
1402 sc->sc_cdata.mvgbe_tx_cnt = 0;
1403
1404 MVGBE_CDTXSYNC(sc, 0, MVGBE_TX_RING_CNT,
1405 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1406
1407 return 0;
1408 }
1409
1410 static int
1411 mvgbe_newbuf(struct mvgbe_softc *sc, int i, struct mbuf *m,
1412 bus_dmamap_t dmamap)
1413 {
1414 struct mbuf *m_new = NULL;
1415 struct mvgbe_chain *c;
1416 struct mvgbe_rx_desc *r;
1417 int align;
1418 vaddr_t offset;
1419
1420 if (m == NULL) {
1421 void *buf = NULL;
1422
1423 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
1424 if (m_new == NULL) {
1425 aprint_error_ifnet(&sc->sc_ethercom.ec_if,
1426 "no memory for rx list -- packet dropped!\n");
1427 return ENOBUFS;
1428 }
1429
1430 /* Allocate the jumbo buffer */
1431 buf = mvgbe_jalloc(sc);
1432 if (buf == NULL) {
1433 m_freem(m_new);
1434 DPRINTFN(1, ("%s jumbo allocation failed -- packet "
1435 "dropped!\n", sc->sc_ethercom.ec_if.if_xname));
1436 return ENOBUFS;
1437 }
1438
1439 /* Attach the buffer to the mbuf */
1440 m_new->m_len = m_new->m_pkthdr.len = MVGBE_JLEN;
1441 MEXTADD(m_new, buf, MVGBE_JLEN, 0, mvgbe_jfree, sc);
1442 } else {
1443 /*
1444 * We're re-using a previously allocated mbuf;
1445 * be sure to re-init pointers and lengths to
1446 * default values.
1447 */
1448 m_new = m;
1449 m_new->m_len = m_new->m_pkthdr.len = MVGBE_JLEN;
1450 m_new->m_data = m_new->m_ext.ext_buf;
1451 }
1452 align = (u_long)m_new->m_data & MVGBE_RXBUF_MASK;
1453 if (align != 0) {
1454 DPRINTFN(1,("align = %d\n", align));
1455 m_adj(m_new, MVGBE_RXBUF_ALIGN - align);
1456 }
1457
1458 c = &sc->sc_cdata.mvgbe_rx_chain[i];
1459 r = c->mvgbe_desc;
1460 c->mvgbe_mbuf = m_new;
1461 offset = (vaddr_t)m_new->m_data - (vaddr_t)sc->sc_cdata.mvgbe_jumbo_buf;
1462 r->bufptr = dmamap->dm_segs[0].ds_addr + offset;
1463 r->bufsize = MVGBE_JLEN & ~MVGBE_RXBUF_MASK;
1464 r->cmdsts = MVGBE_BUFFER_OWNED_BY_DMA | MVGBE_RX_ENABLE_INTERRUPT;
1465
1466 /* Invalidate RX buffer */
1467 bus_dmamap_sync(sc->sc_dmat, dmamap, offset, r->bufsize,
1468 BUS_DMASYNC_PREREAD);
1469
1470 MVGBE_CDRXSYNC(sc, i, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1471
1472 return 0;
1473 }
1474
1475 /*
1476 * Memory management for jumbo frames.
1477 */
1478
1479 static int
1480 mvgbe_alloc_jumbo_mem(struct mvgbe_softc *sc)
1481 {
1482 char *ptr, *kva;
1483 bus_dma_segment_t seg;
1484 int i, rseg, state, error;
1485 struct mvgbe_jpool_entry *entry;
1486
1487 state = error = 0;
1488
1489 /* Grab a big chunk o' storage. */
1490 if (bus_dmamem_alloc(sc->sc_dmat, MVGBE_JMEM, PAGE_SIZE, 0,
1491 &seg, 1, &rseg, BUS_DMA_NOWAIT)) {
1492 aprint_error_dev(sc->sc_dev, "can't alloc rx buffers\n");
1493 return ENOBUFS;
1494 }
1495
1496 state = 1;
1497 if (bus_dmamem_map(sc->sc_dmat, &seg, rseg, MVGBE_JMEM,
1498 (void **)&kva, BUS_DMA_NOWAIT)) {
1499 aprint_error_dev(sc->sc_dev,
1500 "can't map dma buffers (%d bytes)\n", MVGBE_JMEM);
1501 error = ENOBUFS;
1502 goto out;
1503 }
1504
1505 state = 2;
1506 if (bus_dmamap_create(sc->sc_dmat, MVGBE_JMEM, 1, MVGBE_JMEM, 0,
1507 BUS_DMA_NOWAIT, &sc->sc_cdata.mvgbe_rx_jumbo_map)) {
1508 aprint_error_dev(sc->sc_dev, "can't create dma map\n");
1509 error = ENOBUFS;
1510 goto out;
1511 }
1512
1513 state = 3;
1514 if (bus_dmamap_load(sc->sc_dmat, sc->sc_cdata.mvgbe_rx_jumbo_map,
1515 kva, MVGBE_JMEM, NULL, BUS_DMA_NOWAIT)) {
1516 aprint_error_dev(sc->sc_dev, "can't load dma map\n");
1517 error = ENOBUFS;
1518 goto out;
1519 }
1520
1521 state = 4;
1522 sc->sc_cdata.mvgbe_jumbo_buf = (void *)kva;
1523 DPRINTFN(1,("mvgbe_jumbo_buf = %p\n", sc->sc_cdata.mvgbe_jumbo_buf));
1524
1525 LIST_INIT(&sc->sc_jfree_listhead);
1526 LIST_INIT(&sc->sc_jinuse_listhead);
1527
1528 /*
1529 * Now divide it up into 9K pieces and save the addresses
1530 * in an array.
1531 */
1532 ptr = sc->sc_cdata.mvgbe_jumbo_buf;
1533 for (i = 0; i < MVGBE_JSLOTS; i++) {
1534 sc->sc_cdata.mvgbe_jslots[i] = ptr;
1535 ptr += MVGBE_JLEN;
1536 entry = kmem_alloc(sizeof(struct mvgbe_jpool_entry), KM_SLEEP);
1537 if (entry == NULL) {
1538 aprint_error_dev(sc->sc_dev,
1539 "no memory for jumbo buffer queue!\n");
1540 error = ENOBUFS;
1541 goto out;
1542 }
1543 entry->slot = i;
1544 if (i)
1545 LIST_INSERT_HEAD(&sc->sc_jfree_listhead, entry,
1546 jpool_entries);
1547 else
1548 LIST_INSERT_HEAD(&sc->sc_jinuse_listhead, entry,
1549 jpool_entries);
1550 }
1551 out:
1552 if (error != 0) {
1553 switch (state) {
1554 case 4:
1555 bus_dmamap_unload(sc->sc_dmat,
1556 sc->sc_cdata.mvgbe_rx_jumbo_map);
1557 case 3:
1558 bus_dmamap_destroy(sc->sc_dmat,
1559 sc->sc_cdata.mvgbe_rx_jumbo_map);
1560 case 2:
1561 bus_dmamem_unmap(sc->sc_dmat, kva, MVGBE_JMEM);
1562 case 1:
1563 bus_dmamem_free(sc->sc_dmat, &seg, rseg);
1564 break;
1565 default:
1566 break;
1567 }
1568 }
1569
1570 return error;
1571 }
1572
1573 /*
1574 * Allocate a jumbo buffer.
1575 */
1576 static void *
1577 mvgbe_jalloc(struct mvgbe_softc *sc)
1578 {
1579 struct mvgbe_jpool_entry *entry;
1580
1581 entry = LIST_FIRST(&sc->sc_jfree_listhead);
1582
1583 if (entry == NULL)
1584 return NULL;
1585
1586 LIST_REMOVE(entry, jpool_entries);
1587 LIST_INSERT_HEAD(&sc->sc_jinuse_listhead, entry, jpool_entries);
1588 return sc->sc_cdata.mvgbe_jslots[entry->slot];
1589 }
1590
1591 /*
1592 * Release a jumbo buffer.
1593 */
1594 static void
1595 mvgbe_jfree(struct mbuf *m, void *buf, size_t size, void *arg)
1596 {
1597 struct mvgbe_jpool_entry *entry;
1598 struct mvgbe_softc *sc;
1599 int i, s;
1600
1601 /* Extract the softc struct pointer. */
1602 sc = (struct mvgbe_softc *)arg;
1603
1604 if (sc == NULL)
1605 panic("%s: can't find softc pointer!", __func__);
1606
1607 /* calculate the slot this buffer belongs to */
1608
1609 i = ((vaddr_t)buf - (vaddr_t)sc->sc_cdata.mvgbe_jumbo_buf) / MVGBE_JLEN;
1610
1611 if ((i < 0) || (i >= MVGBE_JSLOTS))
1612 panic("%s: asked to free buffer that we don't manage!",
1613 __func__);
1614
1615 s = splvm();
1616 entry = LIST_FIRST(&sc->sc_jinuse_listhead);
1617 if (entry == NULL)
1618 panic("%s: buffer not in use!", __func__);
1619 entry->slot = i;
1620 LIST_REMOVE(entry, jpool_entries);
1621 LIST_INSERT_HEAD(&sc->sc_jfree_listhead, entry, jpool_entries);
1622
1623 if (__predict_true(m != NULL))
1624 pool_cache_put(mb_cache, m);
1625 splx(s);
1626 }
1627
1628 static int
1629 mvgbe_encap(struct mvgbe_softc *sc, struct mbuf *m_head,
1630 uint32_t *txidx)
1631 {
1632 struct mvgbe_tx_desc *f = NULL;
1633 struct mvgbe_txmap_entry *entry;
1634 bus_dma_segment_t *txseg;
1635 bus_dmamap_t txmap;
1636 uint32_t first, current, last, cmdsts = 0;
1637 int m_csumflags, i;
1638 bool needs_defrag = false;
1639
1640 DPRINTFN(3, ("mvgbe_encap\n"));
1641
1642 entry = SIMPLEQ_FIRST(&sc->sc_txmap_head);
1643 if (entry == NULL) {
1644 DPRINTFN(2, ("mvgbe_encap: no txmap available\n"));
1645 return ENOBUFS;
1646 }
1647 txmap = entry->dmamap;
1648
1649 first = current = last = *txidx;
1650
1651 /*
1652 * Preserve m_pkthdr.csum_flags here since m_head might be
1653 * updated by m_defrag()
1654 */
1655 m_csumflags = m_head->m_pkthdr.csum_flags;
1656
1657 do_defrag:
1658 if (__predict_false(needs_defrag == true)) {
1659 /* A small unaligned segment was detected. */
1660 struct mbuf *m_new;
1661 m_new = m_defrag(m_head, M_DONTWAIT);
1662 if (m_new == NULL)
1663 return EFBIG;
1664 m_head = m_new;
1665 }
1666
1667 /*
1668 * Start packing the mbufs in this chain into
1669 * the fragment pointers. Stop when we run out
1670 * of fragments or hit the end of the mbuf chain.
1671 */
1672 if (bus_dmamap_load_mbuf(sc->sc_dmat, txmap, m_head, BUS_DMA_NOWAIT)) {
1673 DPRINTFN(1, ("mvgbe_encap: dmamap failed\n"));
1674 return ENOBUFS;
1675 }
1676
1677 txseg = txmap->dm_segs;
1678
1679 if (__predict_true(needs_defrag == false)) {
1680 /*
1681 * Detect rarely encountered DMA limitation.
1682 */
1683 for (i = 0; i < txmap->dm_nsegs; i++) {
1684 if (((txseg[i].ds_addr & 7) != 0) &&
1685 (txseg[i].ds_len <= 8) &&
1686 (txseg[i].ds_len >= 1)
1687 ) {
1688 txseg = NULL;
1689 bus_dmamap_unload(sc->sc_dmat, txmap);
1690 needs_defrag = true;
1691 goto do_defrag;
1692 }
1693 }
1694 }
1695
1696 /* Sync the DMA map. */
1697 bus_dmamap_sync(sc->sc_dmat, txmap, 0, txmap->dm_mapsize,
1698 BUS_DMASYNC_PREWRITE);
1699
1700 if (sc->sc_cdata.mvgbe_tx_cnt + txmap->dm_nsegs >=
1701 MVGBE_TX_RING_CNT) {
1702 DPRINTFN(2, ("mvgbe_encap: too few descriptors free\n"));
1703 bus_dmamap_unload(sc->sc_dmat, txmap);
1704 return ENOBUFS;
1705 }
1706
1707
1708 DPRINTFN(2, ("mvgbe_encap: dm_nsegs=%d\n", txmap->dm_nsegs));
1709
1710 for (i = 0; i < txmap->dm_nsegs; i++) {
1711 f = &sc->sc_rdata->mvgbe_tx_ring[current];
1712 f->bufptr = txseg[i].ds_addr;
1713 f->bytecnt = txseg[i].ds_len;
1714 if (i != 0)
1715 f->cmdsts = MVGBE_BUFFER_OWNED_BY_DMA;
1716 last = current;
1717 current = MVGBE_TX_RING_NEXT(current);
1718 }
1719
1720 if (m_csumflags & M_CSUM_IPv4)
1721 cmdsts |= MVGBE_TX_GENERATE_IP_CHKSUM;
1722 if (m_csumflags & M_CSUM_TCPv4)
1723 cmdsts |=
1724 MVGBE_TX_GENERATE_L4_CHKSUM | MVGBE_TX_L4_TYPE_TCP;
1725 if (m_csumflags & M_CSUM_UDPv4)
1726 cmdsts |=
1727 MVGBE_TX_GENERATE_L4_CHKSUM | MVGBE_TX_L4_TYPE_UDP;
1728 if (m_csumflags & (M_CSUM_IPv4 | M_CSUM_TCPv4 | M_CSUM_UDPv4)) {
1729 const int iphdr_unitlen = sizeof(struct ip) / sizeof(uint32_t);
1730
1731 cmdsts |= MVGBE_TX_IP_NO_FRAG |
1732 MVGBE_TX_IP_HEADER_LEN(iphdr_unitlen); /* unit is 4B */
1733 }
1734 if (txmap->dm_nsegs == 1)
1735 f->cmdsts = cmdsts |
1736 MVGBE_TX_GENERATE_CRC |
1737 MVGBE_TX_ENABLE_INTERRUPT |
1738 MVGBE_TX_ZERO_PADDING |
1739 MVGBE_TX_FIRST_DESC |
1740 MVGBE_TX_LAST_DESC;
1741 else {
1742 f = &sc->sc_rdata->mvgbe_tx_ring[first];
1743 f->cmdsts = cmdsts |
1744 MVGBE_TX_GENERATE_CRC |
1745 MVGBE_TX_FIRST_DESC;
1746
1747 f = &sc->sc_rdata->mvgbe_tx_ring[last];
1748 f->cmdsts =
1749 MVGBE_BUFFER_OWNED_BY_DMA |
1750 MVGBE_TX_ENABLE_INTERRUPT |
1751 MVGBE_TX_ZERO_PADDING |
1752 MVGBE_TX_LAST_DESC;
1753
1754 /* Sync descriptors except first */
1755 MVGBE_CDTXSYNC(sc,
1756 (MVGBE_TX_RING_CNT - 1 == *txidx) ? 0 : (*txidx) + 1,
1757 txmap->dm_nsegs - 1,
1758 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1759 }
1760
1761 sc->sc_cdata.mvgbe_tx_chain[last].mvgbe_mbuf = m_head;
1762 SIMPLEQ_REMOVE_HEAD(&sc->sc_txmap_head, link);
1763 sc->sc_cdata.mvgbe_tx_map[last] = entry;
1764
1765 /* Finally, sync first descriptor */
1766 sc->sc_rdata->mvgbe_tx_ring[first].cmdsts |=
1767 MVGBE_BUFFER_OWNED_BY_DMA;
1768 MVGBE_CDTXSYNC(sc, *txidx, 1,
1769 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1770
1771 sc->sc_cdata.mvgbe_tx_cnt += i;
1772 *txidx = current;
1773
1774 DPRINTFN(3, ("mvgbe_encap: completed successfully\n"));
1775
1776 return 0;
1777 }
1778
1779 static void
1780 mvgbe_rxeof(struct mvgbe_softc *sc)
1781 {
1782 struct mvgbe_chain_data *cdata = &sc->sc_cdata;
1783 struct mvgbe_rx_desc *cur_rx;
1784 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1785 struct mbuf *m;
1786 bus_dmamap_t dmamap;
1787 uint32_t rxstat;
1788 uint16_t bufsize;
1789 int idx, cur, total_len;
1790
1791 idx = sc->sc_cdata.mvgbe_rx_prod;
1792
1793 DPRINTFN(3, ("mvgbe_rxeof %d\n", idx));
1794
1795 for (;;) {
1796 cur = idx;
1797
1798 /* Sync the descriptor */
1799 MVGBE_CDRXSYNC(sc, idx,
1800 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1801
1802 cur_rx = &sc->sc_rdata->mvgbe_rx_ring[idx];
1803
1804 if ((cur_rx->cmdsts & MVGBE_BUFFER_OWNED_MASK) ==
1805 MVGBE_BUFFER_OWNED_BY_DMA) {
1806 /* Invalidate the descriptor -- it's not ready yet */
1807 MVGBE_CDRXSYNC(sc, idx, BUS_DMASYNC_PREREAD);
1808 sc->sc_cdata.mvgbe_rx_prod = idx;
1809 break;
1810 }
1811 #ifdef DIAGNOSTIC
1812 if ((cur_rx->cmdsts &
1813 (MVGBE_RX_LAST_DESC | MVGBE_RX_FIRST_DESC)) !=
1814 (MVGBE_RX_LAST_DESC | MVGBE_RX_FIRST_DESC))
1815 panic(
1816 "mvgbe_rxeof: buffer size is smaller than packet");
1817 #endif
1818
1819 dmamap = sc->sc_cdata.mvgbe_rx_jumbo_map;
1820
1821 bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
1822 BUS_DMASYNC_POSTREAD);
1823
1824 m = cdata->mvgbe_rx_chain[idx].mvgbe_mbuf;
1825 cdata->mvgbe_rx_chain[idx].mvgbe_mbuf = NULL;
1826 total_len = cur_rx->bytecnt - ETHER_CRC_LEN;
1827 rxstat = cur_rx->cmdsts;
1828 bufsize = cur_rx->bufsize;
1829
1830 cdata->mvgbe_rx_map[idx] = NULL;
1831
1832 idx = MVGBE_RX_RING_NEXT(idx);
1833
1834 if (rxstat & MVGBE_ERROR_SUMMARY) {
1835 #if 0
1836 int err = rxstat & MVGBE_RX_ERROR_CODE_MASK;
1837
1838 if (err == MVGBE_RX_CRC_ERROR)
1839 ifp->if_ierrors++;
1840 if (err == MVGBE_RX_OVERRUN_ERROR)
1841 ifp->if_ierrors++;
1842 if (err == MVGBE_RX_MAX_FRAME_LEN_ERROR)
1843 ifp->if_ierrors++;
1844 if (err == MVGBE_RX_RESOURCE_ERROR)
1845 ifp->if_ierrors++;
1846 #else
1847 ifp->if_ierrors++;
1848 #endif
1849 mvgbe_newbuf(sc, cur, m, dmamap);
1850 continue;
1851 }
1852
1853 if (rxstat & MVGBE_RX_IP_FRAME_TYPE) {
1854 int flgs = 0;
1855
1856 /* Check IPv4 header checksum */
1857 flgs |= M_CSUM_IPv4;
1858 if (!(rxstat & MVGBE_RX_IP_HEADER_OK))
1859 flgs |= M_CSUM_IPv4_BAD;
1860 else if ((bufsize & MVGBE_RX_IP_FRAGMENT) == 0) {
1861 /*
1862 * Check TCPv4/UDPv4 checksum for
1863 * non-fragmented packet only.
1864 *
1865 * It seemd that sometimes
1866 * MVGBE_RX_L4_CHECKSUM_OK bit was set to 0
1867 * even if the checksum is correct and the
1868 * packet was not fragmented. So we don't set
1869 * M_CSUM_TCP_UDP_BAD even if csum bit is 0.
1870 */
1871
1872 if (((rxstat & MVGBE_RX_L4_TYPE_MASK) ==
1873 MVGBE_RX_L4_TYPE_TCP) &&
1874 ((rxstat & MVGBE_RX_L4_CHECKSUM_OK) != 0))
1875 flgs |= M_CSUM_TCPv4;
1876 else if (((rxstat & MVGBE_RX_L4_TYPE_MASK) ==
1877 MVGBE_RX_L4_TYPE_UDP) &&
1878 ((rxstat & MVGBE_RX_L4_CHECKSUM_OK) != 0))
1879 flgs |= M_CSUM_UDPv4;
1880 }
1881 m->m_pkthdr.csum_flags = flgs;
1882 }
1883
1884 /*
1885 * Try to allocate a new jumbo buffer. If that
1886 * fails, copy the packet to mbufs and put the
1887 * jumbo buffer back in the ring so it can be
1888 * re-used. If allocating mbufs fails, then we
1889 * have to drop the packet.
1890 */
1891 if (mvgbe_newbuf(sc, cur, NULL, dmamap) == ENOBUFS) {
1892 struct mbuf *m0;
1893
1894 m0 = m_devget(mtod(m, char *), total_len, 0, ifp, NULL);
1895 mvgbe_newbuf(sc, cur, m, dmamap);
1896 if (m0 == NULL) {
1897 aprint_error_ifnet(ifp,
1898 "no receive buffers available --"
1899 " packet dropped!\n");
1900 ifp->if_ierrors++;
1901 continue;
1902 }
1903 m = m0;
1904 } else {
1905 m->m_pkthdr.rcvif = ifp;
1906 m->m_pkthdr.len = m->m_len = total_len;
1907 }
1908
1909 /* Skip on first 2byte (HW header) */
1910 m_adj(m, MVGBE_HWHEADER_SIZE);
1911
1912 ifp->if_ipackets++;
1913
1914 bpf_mtap(ifp, m);
1915
1916 /* pass it on. */
1917 (*ifp->if_input)(ifp, m);
1918 }
1919 }
1920
1921 static void
1922 mvgbe_txeof(struct mvgbe_softc *sc)
1923 {
1924 struct mvgbe_chain_data *cdata = &sc->sc_cdata;
1925 struct mvgbe_tx_desc *cur_tx;
1926 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1927 struct mvgbe_txmap_entry *entry;
1928 int idx;
1929
1930 DPRINTFN(3, ("mvgbe_txeof\n"));
1931
1932 /*
1933 * Go through our tx ring and free mbufs for those
1934 * frames that have been sent.
1935 */
1936 idx = cdata->mvgbe_tx_cons;
1937 while (idx != cdata->mvgbe_tx_prod) {
1938 MVGBE_CDTXSYNC(sc, idx, 1,
1939 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1940
1941 cur_tx = &sc->sc_rdata->mvgbe_tx_ring[idx];
1942 #ifdef MVGBE_DEBUG
1943 if (mvgbe_debug >= 3)
1944 mvgbe_dump_txdesc(cur_tx, idx);
1945 #endif
1946 if ((cur_tx->cmdsts & MVGBE_BUFFER_OWNED_MASK) ==
1947 MVGBE_BUFFER_OWNED_BY_DMA) {
1948 MVGBE_CDTXSYNC(sc, idx, 1, BUS_DMASYNC_PREREAD);
1949 break;
1950 }
1951 if (cur_tx->cmdsts & MVGBE_TX_LAST_DESC)
1952 ifp->if_opackets++;
1953 if (cur_tx->cmdsts & MVGBE_ERROR_SUMMARY) {
1954 int err = cur_tx->cmdsts & MVGBE_TX_ERROR_CODE_MASK;
1955
1956 if (err == MVGBE_TX_LATE_COLLISION_ERROR)
1957 ifp->if_collisions++;
1958 if (err == MVGBE_TX_UNDERRUN_ERROR)
1959 ifp->if_oerrors++;
1960 if (err == MVGBE_TX_EXCESSIVE_COLLISION_ERRO)
1961 ifp->if_collisions++;
1962 }
1963 if (cdata->mvgbe_tx_chain[idx].mvgbe_mbuf != NULL) {
1964 entry = cdata->mvgbe_tx_map[idx];
1965
1966 m_freem(cdata->mvgbe_tx_chain[idx].mvgbe_mbuf);
1967 cdata->mvgbe_tx_chain[idx].mvgbe_mbuf = NULL;
1968
1969 bus_dmamap_sync(sc->sc_dmat, entry->dmamap, 0,
1970 entry->dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1971
1972 bus_dmamap_unload(sc->sc_dmat, entry->dmamap);
1973 SIMPLEQ_INSERT_TAIL(&sc->sc_txmap_head, entry, link);
1974 cdata->mvgbe_tx_map[idx] = NULL;
1975 }
1976 cdata->mvgbe_tx_cnt--;
1977 idx = MVGBE_TX_RING_NEXT(idx);
1978 }
1979 if (cdata->mvgbe_tx_cnt == 0)
1980 ifp->if_timer = 0;
1981
1982 if (cdata->mvgbe_tx_cnt < MVGBE_TX_RING_CNT - 2)
1983 ifp->if_flags &= ~IFF_OACTIVE;
1984
1985 cdata->mvgbe_tx_cons = idx;
1986 }
1987
1988 static uint8_t
1989 mvgbe_crc8(const uint8_t *data, size_t size)
1990 {
1991 int bit;
1992 uint8_t byte;
1993 uint8_t crc = 0;
1994 const uint8_t poly = 0x07;
1995
1996 while(size--)
1997 for (byte = *data++, bit = NBBY-1; bit >= 0; bit--)
1998 crc = (crc << 1) ^ ((((crc >> 7) ^ (byte >> bit)) & 1) ? poly : 0);
1999
2000 return crc;
2001 }
2002
2003 CTASSERT(MVGBE_NDFSMT == MVGBE_NDFOMT);
2004
2005 static void
2006 mvgbe_filter_setup(struct mvgbe_softc *sc)
2007 {
2008 struct ethercom *ec = &sc->sc_ethercom;
2009 struct ifnet *ifp= &sc->sc_ethercom.ec_if;
2010 struct ether_multi *enm;
2011 struct ether_multistep step;
2012 uint32_t dfut[MVGBE_NDFUT], dfsmt[MVGBE_NDFSMT], dfomt[MVGBE_NDFOMT];
2013 uint32_t pxc;
2014 int i;
2015 const uint8_t special[ETHER_ADDR_LEN] = {0x01,0x00,0x5e,0x00,0x00,0x00};
2016
2017 memset(dfut, 0, sizeof(dfut));
2018 memset(dfsmt, 0, sizeof(dfsmt));
2019 memset(dfomt, 0, sizeof(dfomt));
2020
2021 if (ifp->if_flags & (IFF_ALLMULTI|IFF_PROMISC)) {
2022 goto allmulti;
2023 }
2024
2025 ETHER_FIRST_MULTI(step, ec, enm);
2026 while (enm != NULL) {
2027 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
2028 /* ranges are complex and somewhat rare */
2029 goto allmulti;
2030 }
2031 /* chip handles some IPv4 multicast specially */
2032 if (memcmp(enm->enm_addrlo, special, 5) == 0) {
2033 i = enm->enm_addrlo[5];
2034 dfsmt[i>>2] =
2035 MVGBE_DF(i&3, MVGBE_DF_QUEUE(0) | MVGBE_DF_PASS);
2036 } else {
2037 i = mvgbe_crc8(enm->enm_addrlo, ETHER_ADDR_LEN);
2038 dfomt[i>>2] =
2039 MVGBE_DF(i&3, MVGBE_DF_QUEUE(0) | MVGBE_DF_PASS);
2040 }
2041
2042 ETHER_NEXT_MULTI(step, enm);
2043 }
2044 goto set;
2045
2046 allmulti:
2047 if (ifp->if_flags & (IFF_ALLMULTI|IFF_PROMISC)) {
2048 for (i = 0; i < MVGBE_NDFSMT; i++) {
2049 dfsmt[i] = dfomt[i] =
2050 MVGBE_DF(0, MVGBE_DF_QUEUE(0) | MVGBE_DF_PASS) |
2051 MVGBE_DF(1, MVGBE_DF_QUEUE(0) | MVGBE_DF_PASS) |
2052 MVGBE_DF(2, MVGBE_DF_QUEUE(0) | MVGBE_DF_PASS) |
2053 MVGBE_DF(3, MVGBE_DF_QUEUE(0) | MVGBE_DF_PASS);
2054 }
2055 }
2056
2057 set:
2058 pxc = MVGBE_READ(sc, MVGBE_PXC);
2059 pxc &= ~MVGBE_PXC_UPM;
2060 pxc |= MVGBE_PXC_RB | MVGBE_PXC_RBIP | MVGBE_PXC_RBARP;
2061 if (ifp->if_flags & IFF_BROADCAST) {
2062 pxc &= ~(MVGBE_PXC_RB | MVGBE_PXC_RBIP | MVGBE_PXC_RBARP);
2063 }
2064 if (ifp->if_flags & IFF_PROMISC) {
2065 pxc |= MVGBE_PXC_UPM;
2066 }
2067 MVGBE_WRITE(sc, MVGBE_PXC, pxc);
2068
2069 /* Set Destination Address Filter Unicast Table */
2070 i = sc->sc_enaddr[5] & 0xf; /* last nibble */
2071 dfut[i>>2] = MVGBE_DF(i&3, MVGBE_DF_QUEUE(0) | MVGBE_DF_PASS);
2072 MVGBE_WRITE_FILTER(sc, MVGBE_DFUT, dfut, MVGBE_NDFUT);
2073
2074 /* Set Destination Address Filter Multicast Tables */
2075 MVGBE_WRITE_FILTER(sc, MVGBE_DFSMT, dfsmt, MVGBE_NDFSMT);
2076 MVGBE_WRITE_FILTER(sc, MVGBE_DFOMT, dfomt, MVGBE_NDFOMT);
2077 }
2078
2079 #ifdef MVGBE_DEBUG
2080 static void
2081 mvgbe_dump_txdesc(struct mvgbe_tx_desc *desc, int idx)
2082 {
2083 #define DESC_PRINT(X) \
2084 if (X) \
2085 printf("txdesc[%d]." #X "=%#x\n", idx, X);
2086
2087 #if BYTE_ORDER == BIG_ENDIAN
2088 DESC_PRINT(desc->bytecnt);
2089 DESC_PRINT(desc->l4ichk);
2090 DESC_PRINT(desc->cmdsts);
2091 DESC_PRINT(desc->nextdescptr);
2092 DESC_PRINT(desc->bufptr);
2093 #else /* LITTLE_ENDIAN */
2094 DESC_PRINT(desc->cmdsts);
2095 DESC_PRINT(desc->l4ichk);
2096 DESC_PRINT(desc->bytecnt);
2097 DESC_PRINT(desc->bufptr);
2098 DESC_PRINT(desc->nextdescptr);
2099 #endif
2100 #undef DESC_PRINT
2101 }
2102 #endif
2103
2104 SYSCTL_SETUP(sysctl_mvgbe, "sysctl mvgbe subtree setup")
2105 {
2106 int rc;
2107 const struct sysctlnode *node;
2108
2109 if ((rc = sysctl_createv(clog, 0, NULL, NULL,
2110 0, CTLTYPE_NODE, "hw", NULL,
2111 NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0) {
2112 goto err;
2113 }
2114
2115 if ((rc = sysctl_createv(clog, 0, NULL, &node,
2116 0, CTLTYPE_NODE, "mvgbe",
2117 SYSCTL_DESCR("mvgbe interface controls"),
2118 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) {
2119 goto err;
2120 }
2121
2122 mvgbe_root_num = node->sysctl_num;
2123 return;
2124
2125 err:
2126 aprint_error("%s: syctl_createv failed (rc = %d)\n", __func__, rc);
2127 }
2128
2129 static void
2130 sysctl_mvgbe_init(struct mvgbe_softc *sc)
2131 {
2132 const struct sysctlnode *node;
2133 int mvgbe_nodenum;
2134
2135 if (sysctl_createv(&sc->mvgbe_clog, 0, NULL, &node,
2136 0, CTLTYPE_NODE, device_xname(sc->sc_dev),
2137 SYSCTL_DESCR("mvgbe per-controller controls"),
2138 NULL, 0, NULL, 0, CTL_HW, mvgbe_root_num, CTL_CREATE,
2139 CTL_EOL) != 0) {
2140 aprint_normal_dev(sc->sc_dev, "couldn't create sysctl node\n");
2141 return;
2142 }
2143 mvgbe_nodenum = node->sysctl_num;
2144
2145 /* interrupt moderation sysctls */
2146 if (sysctl_createv(&sc->mvgbe_clog, 0, NULL, &node,
2147 CTLFLAG_READWRITE, CTLTYPE_INT, "ipginttx",
2148 SYSCTL_DESCR("mvgbe TX interrupt moderation timer"),
2149 mvgbe_sysctl_ipginttx, 0, (void *)sc,
2150 0, CTL_HW, mvgbe_root_num, mvgbe_nodenum, CTL_CREATE,
2151 CTL_EOL) != 0) {
2152 aprint_normal_dev(sc->sc_dev,
2153 "couldn't create ipginttx sysctl node\n");
2154 }
2155 if (sysctl_createv(&sc->mvgbe_clog, 0, NULL, &node,
2156 CTLFLAG_READWRITE, CTLTYPE_INT, "ipgintrx",
2157 SYSCTL_DESCR("mvgbe RX interrupt moderation timer"),
2158 mvgbe_sysctl_ipgintrx, 0, (void *)sc,
2159 0, CTL_HW, mvgbe_root_num, mvgbe_nodenum, CTL_CREATE,
2160 CTL_EOL) != 0) {
2161 aprint_normal_dev(sc->sc_dev,
2162 "couldn't create ipginttx sysctl node\n");
2163 }
2164 }
2165
2166 static int
2167 mvgbe_sysctl_ipginttx(SYSCTLFN_ARGS)
2168 {
2169 int error;
2170 unsigned int t;
2171 struct sysctlnode node;
2172 struct mvgbec_softc *csc;
2173 struct mvgbe_softc *sc;
2174
2175 node = *rnode;
2176 sc = node.sysctl_data;
2177 csc = device_private(device_parent(sc->sc_dev));
2178 t = sc->sc_ipginttx;
2179 node.sysctl_data = &t;
2180 error = sysctl_lookup(SYSCTLFN_CALL(&node));
2181 if (error || newp == NULL)
2182 return error;
2183
2184 if (mvgbe_ipginttx(csc, sc, t) < 0)
2185 return EINVAL;
2186 /*
2187 * update the softc with sysctl-changed value, and mark
2188 * for hardware update
2189 */
2190 sc->sc_ipginttx = t;
2191
2192 return 0;
2193 }
2194
2195 static int
2196 mvgbe_sysctl_ipgintrx(SYSCTLFN_ARGS)
2197 {
2198 int error;
2199 unsigned int t;
2200 struct sysctlnode node;
2201 struct mvgbec_softc *csc;
2202 struct mvgbe_softc *sc;
2203
2204 node = *rnode;
2205 sc = node.sysctl_data;
2206 csc = device_private(device_parent(sc->sc_dev));
2207 t = sc->sc_ipgintrx;
2208 node.sysctl_data = &t;
2209 error = sysctl_lookup(SYSCTLFN_CALL(&node));
2210 if (error || newp == NULL)
2211 return error;
2212
2213 if (mvgbe_ipgintrx(csc, sc, t) < 0)
2214 return EINVAL;
2215 /*
2216 * update the softc with sysctl-changed value, and mark
2217 * for hardware update
2218 */
2219 sc->sc_ipgintrx = t;
2220
2221 return 0;
2222 }
2223