if_mvgbe.c revision 1.30 1 /* $NetBSD: if_mvgbe.c,v 1.30 2012/11/08 14:37:47 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.30 2012/11/08 14:37:47 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 /* Notify link change event to mii layer */
927 mii_pollstat(&sc->sc_mii);
928 }
929
930 if (ic & (MVGBE_IC_RXBUF | MVGBE_IC_RXERROR))
931 mvgbe_rxeof(sc);
932
933 if (ice & (MVGBE_ICE_TXBUF | MVGBE_ICE_TXERR))
934 mvgbe_txeof(sc);
935 }
936
937 if (!IFQ_IS_EMPTY(&ifp->if_snd))
938 mvgbe_start(ifp);
939
940 rnd_add_uint32(&sc->sc_rnd_source, datum);
941
942 return claimed;
943 }
944
945 static void
946 mvgbe_start(struct ifnet *ifp)
947 {
948 struct mvgbe_softc *sc = ifp->if_softc;
949 struct mbuf *m_head = NULL;
950 uint32_t idx = sc->sc_cdata.mvgbe_tx_prod;
951 int pkts = 0;
952
953 DPRINTFN(3, ("mvgbe_start (idx %d, tx_chain[idx] %p)\n", idx,
954 sc->sc_cdata.mvgbe_tx_chain[idx].mvgbe_mbuf));
955
956 if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
957 return;
958 /* If Link is DOWN, can't start TX */
959 if (!(MVGBE_READ(sc, MVGBE_PS) & MVGBE_PS_LINKUP))
960 return;
961
962 while (sc->sc_cdata.mvgbe_tx_chain[idx].mvgbe_mbuf == NULL) {
963 IFQ_POLL(&ifp->if_snd, m_head);
964 if (m_head == NULL)
965 break;
966
967 /*
968 * Pack the data into the transmit ring. If we
969 * don't have room, set the OACTIVE flag and wait
970 * for the NIC to drain the ring.
971 */
972 if (mvgbe_encap(sc, m_head, &idx)) {
973 ifp->if_flags |= IFF_OACTIVE;
974 break;
975 }
976
977 /* now we are committed to transmit the packet */
978 IFQ_DEQUEUE(&ifp->if_snd, m_head);
979 pkts++;
980
981 /*
982 * If there's a BPF listener, bounce a copy of this frame
983 * to him.
984 */
985 bpf_mtap(ifp, m_head);
986 }
987 if (pkts == 0)
988 return;
989
990 /* Transmit at Queue 0 */
991 if (idx != sc->sc_cdata.mvgbe_tx_prod) {
992 sc->sc_cdata.mvgbe_tx_prod = idx;
993 MVGBE_WRITE(sc, MVGBE_TQC, MVGBE_TQC_ENQ);
994
995 /*
996 * Set a timeout in case the chip goes out to lunch.
997 */
998 ifp->if_timer = 1;
999 sc->sc_wdogsoft = 1;
1000 }
1001 }
1002
1003 static int
1004 mvgbe_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1005 {
1006 struct mvgbe_softc *sc = ifp->if_softc;
1007 struct ifreq *ifr = data;
1008 int s, error = 0;
1009
1010 s = splnet();
1011
1012 switch (cmd) {
1013 case SIOCGIFMEDIA:
1014 case SIOCSIFMEDIA:
1015 DPRINTFN(2, ("mvgbe_ioctl MEDIA\n"));
1016 error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
1017 break;
1018 default:
1019 DPRINTFN(2, ("mvgbe_ioctl ETHER\n"));
1020 error = ether_ioctl(ifp, cmd, data);
1021 if (error == ENETRESET) {
1022 if (ifp->if_flags & IFF_RUNNING) {
1023 mvgbe_filter_setup(sc);
1024 }
1025 error = 0;
1026 }
1027 break;
1028 }
1029
1030 splx(s);
1031
1032 return error;
1033 }
1034
1035 static int
1036 mvgbe_init(struct ifnet *ifp)
1037 {
1038 struct mvgbe_softc *sc = ifp->if_softc;
1039 struct mvgbec_softc *csc = device_private(device_parent(sc->sc_dev));
1040 struct mii_data *mii = &sc->sc_mii;
1041 uint32_t reg;
1042 int i;
1043
1044 DPRINTFN(2, ("mvgbe_init\n"));
1045
1046 /* Cancel pending I/O and free all RX/TX buffers. */
1047 mvgbe_stop(ifp, 0);
1048
1049 /* clear all ethernet port interrupts */
1050 MVGBE_WRITE(sc, MVGBE_IC, 0);
1051 MVGBE_WRITE(sc, MVGBE_ICE, 0);
1052
1053 /* Init TX/RX descriptors */
1054 if (mvgbe_init_tx_ring(sc) == ENOBUFS) {
1055 aprint_error_ifnet(ifp,
1056 "initialization failed: no memory for tx buffers\n");
1057 return ENOBUFS;
1058 }
1059 if (mvgbe_init_rx_ring(sc) == ENOBUFS) {
1060 aprint_error_ifnet(ifp,
1061 "initialization failed: no memory for rx buffers\n");
1062 return ENOBUFS;
1063 }
1064
1065 if ((csc->sc_flags & FLAGS_IPG1) || (csc->sc_flags & FLAGS_IPG2)) {
1066 sc->sc_ipginttx = MVGBE_IPGINTTX_DEFAULT;
1067 sc->sc_ipgintrx = MVGBE_IPGINTRX_DEFAULT;
1068 }
1069 if (csc->sc_flags & FLAGS_FIX_MTU)
1070 MVGBE_WRITE(sc, MVGBE_MTU, 0); /* hw reset value is wrong */
1071 MVGBE_WRITE(sc, MVGBE_PSC,
1072 MVGBE_PSC_ANFC | /* Enable Auto-Neg Flow Ctrl */
1073 MVGBE_PSC_RESERVED | /* Must be set to 1 */
1074 MVGBE_PSC_FLFAIL | /* Do NOT Force Link Fail */
1075 MVGBE_PSC_MRU(MVGBE_PSC_MRU_9022) | /* we want 9k */
1076 MVGBE_PSC_SETFULLDX); /* Set_FullDx */
1077 /* XXXX: mvgbe(4) always use RGMII. */
1078 MVGBE_WRITE(sc, MVGBE_PSC1,
1079 MVGBE_READ(sc, MVGBE_PSC1) | MVGBE_PSC1_RGMIIEN);
1080 /* XXXX: Also always Weighted Round-Robin Priority Mode */
1081 MVGBE_WRITE(sc, MVGBE_TQFPC, MVGBE_TQFPC_EN(0));
1082
1083 MVGBE_WRITE(sc, MVGBE_CRDP(0), MVGBE_RX_RING_ADDR(sc, 0));
1084 MVGBE_WRITE(sc, MVGBE_TCQDP, MVGBE_TX_RING_ADDR(sc, 0));
1085
1086 if (csc->sc_flags & FLAGS_FIX_TQTB) {
1087 /*
1088 * Queue 0 (offset 0x72700) must be programmed to 0x3fffffff.
1089 * And offset 0x72704 must be programmed to 0x03ffffff.
1090 * Queue 1 through 7 must be programmed to 0x0.
1091 */
1092 MVGBE_WRITE(sc, MVGBE_TQTBCOUNT(0), 0x3fffffff);
1093 MVGBE_WRITE(sc, MVGBE_TQTBCONFIG(0), 0x03ffffff);
1094 for (i = 1; i < 8; i++) {
1095 MVGBE_WRITE(sc, MVGBE_TQTBCOUNT(i), 0x0);
1096 MVGBE_WRITE(sc, MVGBE_TQTBCONFIG(i), 0x0);
1097 }
1098 } else
1099 for (i = 1; i < 8; i++) {
1100 MVGBE_WRITE(sc, MVGBE_TQTBCOUNT(i), 0x3fffffff);
1101 MVGBE_WRITE(sc, MVGBE_TQTBCONFIG(i), 0xffff7fff);
1102 MVGBE_WRITE(sc, MVGBE_TQAC(i), 0xfc0000ff);
1103 }
1104
1105 MVGBE_WRITE(sc, MVGBE_PXC, MVGBE_PXC_RXCS);
1106 MVGBE_WRITE(sc, MVGBE_PXCX, 0);
1107
1108 /* Set SDC register except IPGINT bits */
1109 MVGBE_WRITE(sc, MVGBE_SDC,
1110 MVGBE_SDC_RXBSZ_16_64BITWORDS |
1111 #if BYTE_ORDER == LITTLE_ENDIAN
1112 MVGBE_SDC_BLMR | /* Big/Little Endian Receive Mode: No swap */
1113 MVGBE_SDC_BLMT | /* Big/Little Endian Transmit Mode: No swap */
1114 #endif
1115 MVGBE_SDC_TXBSZ_16_64BITWORDS);
1116 /* And then set IPGINT bits */
1117 mvgbe_ipgintrx(csc, sc, sc->sc_ipgintrx);
1118
1119 /* Tx side */
1120 MVGBE_WRITE(sc, MVGBE_PTFUT, 0);
1121 mvgbe_ipginttx(csc, sc, sc->sc_ipginttx);
1122
1123 mvgbe_filter_setup(sc);
1124
1125 mii_mediachg(mii);
1126
1127 /* Enable port */
1128 reg = MVGBE_READ(sc, MVGBE_PSC);
1129 MVGBE_WRITE(sc, MVGBE_PSC, reg | MVGBE_PSC_PORTEN);
1130
1131 /* If Link is UP, Start RX and TX traffic */
1132 if (MVGBE_READ(sc, MVGBE_PS) & MVGBE_PS_LINKUP) {
1133 /* Enable port RX/TX. */
1134 MVGBE_WRITE(sc, MVGBE_RQC, MVGBE_RQC_ENQ(0));
1135 MVGBE_WRITE(sc, MVGBE_TQC, MVGBE_TQC_ENQ);
1136 }
1137
1138 /* Enable interrupt masks */
1139 MVGBE_WRITE(sc, MVGBE_PIM,
1140 MVGBE_IC_RXBUF |
1141 MVGBE_IC_EXTEND |
1142 MVGBE_IC_RXBUFQ_MASK |
1143 MVGBE_IC_RXERROR |
1144 MVGBE_IC_RXERRQ_MASK);
1145 MVGBE_WRITE(sc, MVGBE_PEIM,
1146 MVGBE_ICE_TXBUF |
1147 MVGBE_ICE_TXERR |
1148 MVGBE_ICE_LINKCHG);
1149
1150 callout_schedule(&sc->sc_tick_ch, hz);
1151
1152 ifp->if_flags |= IFF_RUNNING;
1153 ifp->if_flags &= ~IFF_OACTIVE;
1154
1155 return 0;
1156 }
1157
1158 /* ARGSUSED */
1159 static void
1160 mvgbe_stop(struct ifnet *ifp, int disable)
1161 {
1162 struct mvgbe_softc *sc = ifp->if_softc;
1163 struct mvgbec_softc *csc = device_private(device_parent(sc->sc_dev));
1164 struct mvgbe_chain_data *cdata = &sc->sc_cdata;
1165 uint32_t reg;
1166 int i, cnt;
1167
1168 DPRINTFN(2, ("mvgbe_stop\n"));
1169
1170 callout_stop(&sc->sc_tick_ch);
1171
1172 /* Stop Rx port activity. Check port Rx activity. */
1173 reg = MVGBE_READ(sc, MVGBE_RQC);
1174 if (reg & MVGBE_RQC_ENQ_MASK)
1175 /* Issue stop command for active channels only */
1176 MVGBE_WRITE(sc, MVGBE_RQC, MVGBE_RQC_DISQ_DISABLE(reg));
1177
1178 /* Stop Tx port activity. Check port Tx activity. */
1179 if (MVGBE_READ(sc, MVGBE_TQC) & MVGBE_TQC_ENQ)
1180 MVGBE_WRITE(sc, MVGBE_TQC, MVGBE_TQC_DISQ);
1181
1182 /* Force link down */
1183 reg = MVGBE_READ(sc, MVGBE_PSC);
1184 MVGBE_WRITE(sc, MVGBE_PSC, reg & ~MVGBE_PSC_FLFAIL);
1185
1186 #define RX_DISABLE_TIMEOUT 0x1000000
1187 #define TX_FIFO_EMPTY_TIMEOUT 0x1000000
1188 /* Wait for all Rx activity to terminate. */
1189 cnt = 0;
1190 do {
1191 if (cnt >= RX_DISABLE_TIMEOUT) {
1192 aprint_error_ifnet(ifp,
1193 "timeout for RX stopped. rqc 0x%x\n", reg);
1194 break;
1195 }
1196 cnt++;
1197
1198 /*
1199 * Check Receive Queue Command register that all Rx queues
1200 * are stopped
1201 */
1202 reg = MVGBE_READ(sc, MVGBE_RQC);
1203 } while (reg & 0xff);
1204
1205 /* Double check to verify that TX FIFO is empty */
1206 cnt = 0;
1207 while (1) {
1208 do {
1209 if (cnt >= TX_FIFO_EMPTY_TIMEOUT) {
1210 aprint_error_ifnet(ifp,
1211 "timeout for TX FIFO empty. status 0x%x\n",
1212 reg);
1213 break;
1214 }
1215 cnt++;
1216
1217 reg = MVGBE_READ(sc, MVGBE_PS);
1218 } while
1219 (!(reg & MVGBE_PS_TXFIFOEMP) || reg & MVGBE_PS_TXINPROG);
1220
1221 if (cnt >= TX_FIFO_EMPTY_TIMEOUT)
1222 break;
1223
1224 /* Double check */
1225 reg = MVGBE_READ(sc, MVGBE_PS);
1226 if (reg & MVGBE_PS_TXFIFOEMP && !(reg & MVGBE_PS_TXINPROG))
1227 break;
1228 else
1229 aprint_error_ifnet(ifp,
1230 "TX FIFO empty double check failed."
1231 " %d loops, status 0x%x\n", cnt, reg);
1232 }
1233
1234 /* Reset the Enable bit in the Port Serial Control Register */
1235 reg = MVGBE_READ(sc, MVGBE_PSC);
1236 MVGBE_WRITE(sc, MVGBE_PSC, reg & ~MVGBE_PSC_PORTEN);
1237
1238 /*
1239 * Disable and clear interrupts
1240 * 0) controller interrupt
1241 * 1) port interrupt cause
1242 * 2) port interrupt mask
1243 */
1244 MVGBE_WRITE(csc, MVGBE_EUIM, 0);
1245 MVGBE_WRITE(csc, MVGBE_EUIC, 0);
1246 MVGBE_WRITE(sc, MVGBE_IC, 0);
1247 MVGBE_WRITE(sc, MVGBE_ICE, 0);
1248 MVGBE_WRITE(sc, MVGBE_PIM, 0);
1249 MVGBE_WRITE(sc, MVGBE_PEIM, 0);
1250
1251 /* Free RX and TX mbufs still in the queues. */
1252 for (i = 0; i < MVGBE_RX_RING_CNT; i++) {
1253 if (cdata->mvgbe_rx_chain[i].mvgbe_mbuf != NULL) {
1254 m_freem(cdata->mvgbe_rx_chain[i].mvgbe_mbuf);
1255 cdata->mvgbe_rx_chain[i].mvgbe_mbuf = NULL;
1256 }
1257 }
1258 for (i = 0; i < MVGBE_TX_RING_CNT; i++) {
1259 if (cdata->mvgbe_tx_chain[i].mvgbe_mbuf != NULL) {
1260 m_freem(cdata->mvgbe_tx_chain[i].mvgbe_mbuf);
1261 cdata->mvgbe_tx_chain[i].mvgbe_mbuf = NULL;
1262 }
1263 }
1264
1265 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1266 }
1267
1268 static void
1269 mvgbe_watchdog(struct ifnet *ifp)
1270 {
1271 struct mvgbe_softc *sc = ifp->if_softc;
1272
1273 /*
1274 * Reclaim first as there is a possibility of losing Tx completion
1275 * interrupts.
1276 */
1277 mvgbe_txeof(sc);
1278 if (sc->sc_cdata.mvgbe_tx_cnt != 0) {
1279 if (sc->sc_wdogsoft) {
1280 /*
1281 * There is race condition between CPU and DMA
1282 * engine. When DMA engine encounters queue end,
1283 * it clears MVGBE_TQC_ENQ bit.
1284 */
1285 MVGBE_WRITE(sc, MVGBE_TQC, MVGBE_TQC_ENQ);
1286 ifp->if_timer = 5;
1287 sc->sc_wdogsoft = 0;
1288 } else {
1289 aprint_error_ifnet(ifp, "watchdog timeout\n");
1290
1291 ifp->if_oerrors++;
1292
1293 mvgbe_init(ifp);
1294 }
1295 }
1296 }
1297
1298 static int
1299 mvgbe_ifflags_cb(struct ethercom *ec)
1300 {
1301 struct ifnet *ifp = &ec->ec_if;
1302 struct mvgbe_softc *sc = ifp->if_softc;
1303 int change = ifp->if_flags ^ sc->sc_if_flags;
1304
1305 if (change != 0)
1306 sc->sc_if_flags = ifp->if_flags;
1307
1308 if ((change & ~(IFF_CANTCHANGE|IFF_DEBUG)) != 0)
1309 return ENETRESET;
1310
1311 if ((change & IFF_PROMISC) != 0)
1312 mvgbe_filter_setup(sc);
1313
1314 return 0;
1315 }
1316
1317 /*
1318 * Set media options.
1319 */
1320 static int
1321 mvgbe_mediachange(struct ifnet *ifp)
1322 {
1323 return ether_mediachange(ifp);
1324 }
1325
1326 /*
1327 * Report current media status.
1328 */
1329 static void
1330 mvgbe_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
1331 {
1332 ether_mediastatus(ifp, ifmr);
1333 }
1334
1335
1336 static int
1337 mvgbe_init_rx_ring(struct mvgbe_softc *sc)
1338 {
1339 struct mvgbe_chain_data *cd = &sc->sc_cdata;
1340 struct mvgbe_ring_data *rd = sc->sc_rdata;
1341 int i;
1342
1343 memset(rd->mvgbe_rx_ring, 0,
1344 sizeof(struct mvgbe_rx_desc) * MVGBE_RX_RING_CNT);
1345
1346 for (i = 0; i < MVGBE_RX_RING_CNT; i++) {
1347 cd->mvgbe_rx_chain[i].mvgbe_desc =
1348 &rd->mvgbe_rx_ring[i];
1349 if (i == MVGBE_RX_RING_CNT - 1) {
1350 cd->mvgbe_rx_chain[i].mvgbe_next =
1351 &cd->mvgbe_rx_chain[0];
1352 rd->mvgbe_rx_ring[i].nextdescptr =
1353 MVGBE_RX_RING_ADDR(sc, 0);
1354 } else {
1355 cd->mvgbe_rx_chain[i].mvgbe_next =
1356 &cd->mvgbe_rx_chain[i + 1];
1357 rd->mvgbe_rx_ring[i].nextdescptr =
1358 MVGBE_RX_RING_ADDR(sc, i + 1);
1359 }
1360 }
1361
1362 for (i = 0; i < MVGBE_RX_RING_CNT; i++) {
1363 if (mvgbe_newbuf(sc, i, NULL,
1364 sc->sc_cdata.mvgbe_rx_jumbo_map) == ENOBUFS) {
1365 aprint_error_ifnet(&sc->sc_ethercom.ec_if,
1366 "failed alloc of %dth mbuf\n", i);
1367 return ENOBUFS;
1368 }
1369 }
1370 sc->sc_cdata.mvgbe_rx_prod = 0;
1371 sc->sc_cdata.mvgbe_rx_cons = 0;
1372
1373 return 0;
1374 }
1375
1376 static int
1377 mvgbe_init_tx_ring(struct mvgbe_softc *sc)
1378 {
1379 struct mvgbe_chain_data *cd = &sc->sc_cdata;
1380 struct mvgbe_ring_data *rd = sc->sc_rdata;
1381 int i;
1382
1383 memset(sc->sc_rdata->mvgbe_tx_ring, 0,
1384 sizeof(struct mvgbe_tx_desc) * MVGBE_TX_RING_CNT);
1385
1386 for (i = 0; i < MVGBE_TX_RING_CNT; i++) {
1387 cd->mvgbe_tx_chain[i].mvgbe_desc =
1388 &rd->mvgbe_tx_ring[i];
1389 if (i == MVGBE_TX_RING_CNT - 1) {
1390 cd->mvgbe_tx_chain[i].mvgbe_next =
1391 &cd->mvgbe_tx_chain[0];
1392 rd->mvgbe_tx_ring[i].nextdescptr =
1393 MVGBE_TX_RING_ADDR(sc, 0);
1394 } else {
1395 cd->mvgbe_tx_chain[i].mvgbe_next =
1396 &cd->mvgbe_tx_chain[i + 1];
1397 rd->mvgbe_tx_ring[i].nextdescptr =
1398 MVGBE_TX_RING_ADDR(sc, i + 1);
1399 }
1400 rd->mvgbe_tx_ring[i].cmdsts = MVGBE_BUFFER_OWNED_BY_HOST;
1401 }
1402
1403 sc->sc_cdata.mvgbe_tx_prod = 0;
1404 sc->sc_cdata.mvgbe_tx_cons = 0;
1405 sc->sc_cdata.mvgbe_tx_cnt = 0;
1406
1407 MVGBE_CDTXSYNC(sc, 0, MVGBE_TX_RING_CNT,
1408 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1409
1410 return 0;
1411 }
1412
1413 static int
1414 mvgbe_newbuf(struct mvgbe_softc *sc, int i, struct mbuf *m,
1415 bus_dmamap_t dmamap)
1416 {
1417 struct mbuf *m_new = NULL;
1418 struct mvgbe_chain *c;
1419 struct mvgbe_rx_desc *r;
1420 int align;
1421 vaddr_t offset;
1422
1423 if (m == NULL) {
1424 void *buf = NULL;
1425
1426 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
1427 if (m_new == NULL) {
1428 aprint_error_ifnet(&sc->sc_ethercom.ec_if,
1429 "no memory for rx list -- packet dropped!\n");
1430 return ENOBUFS;
1431 }
1432
1433 /* Allocate the jumbo buffer */
1434 buf = mvgbe_jalloc(sc);
1435 if (buf == NULL) {
1436 m_freem(m_new);
1437 DPRINTFN(1, ("%s jumbo allocation failed -- packet "
1438 "dropped!\n", sc->sc_ethercom.ec_if.if_xname));
1439 return ENOBUFS;
1440 }
1441
1442 /* Attach the buffer to the mbuf */
1443 m_new->m_len = m_new->m_pkthdr.len = MVGBE_JLEN;
1444 MEXTADD(m_new, buf, MVGBE_JLEN, 0, mvgbe_jfree, sc);
1445 } else {
1446 /*
1447 * We're re-using a previously allocated mbuf;
1448 * be sure to re-init pointers and lengths to
1449 * default values.
1450 */
1451 m_new = m;
1452 m_new->m_len = m_new->m_pkthdr.len = MVGBE_JLEN;
1453 m_new->m_data = m_new->m_ext.ext_buf;
1454 }
1455 align = (u_long)m_new->m_data & MVGBE_RXBUF_MASK;
1456 if (align != 0) {
1457 DPRINTFN(1,("align = %d\n", align));
1458 m_adj(m_new, MVGBE_RXBUF_ALIGN - align);
1459 }
1460
1461 c = &sc->sc_cdata.mvgbe_rx_chain[i];
1462 r = c->mvgbe_desc;
1463 c->mvgbe_mbuf = m_new;
1464 offset = (vaddr_t)m_new->m_data - (vaddr_t)sc->sc_cdata.mvgbe_jumbo_buf;
1465 r->bufptr = dmamap->dm_segs[0].ds_addr + offset;
1466 r->bufsize = MVGBE_JLEN & ~MVGBE_RXBUF_MASK;
1467 r->cmdsts = MVGBE_BUFFER_OWNED_BY_DMA | MVGBE_RX_ENABLE_INTERRUPT;
1468
1469 /* Invalidate RX buffer */
1470 bus_dmamap_sync(sc->sc_dmat, dmamap, offset, r->bufsize,
1471 BUS_DMASYNC_PREREAD);
1472
1473 MVGBE_CDRXSYNC(sc, i, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1474
1475 return 0;
1476 }
1477
1478 /*
1479 * Memory management for jumbo frames.
1480 */
1481
1482 static int
1483 mvgbe_alloc_jumbo_mem(struct mvgbe_softc *sc)
1484 {
1485 char *ptr, *kva;
1486 bus_dma_segment_t seg;
1487 int i, rseg, state, error;
1488 struct mvgbe_jpool_entry *entry;
1489
1490 state = error = 0;
1491
1492 /* Grab a big chunk o' storage. */
1493 if (bus_dmamem_alloc(sc->sc_dmat, MVGBE_JMEM, PAGE_SIZE, 0,
1494 &seg, 1, &rseg, BUS_DMA_NOWAIT)) {
1495 aprint_error_dev(sc->sc_dev, "can't alloc rx buffers\n");
1496 return ENOBUFS;
1497 }
1498
1499 state = 1;
1500 if (bus_dmamem_map(sc->sc_dmat, &seg, rseg, MVGBE_JMEM,
1501 (void **)&kva, BUS_DMA_NOWAIT)) {
1502 aprint_error_dev(sc->sc_dev,
1503 "can't map dma buffers (%d bytes)\n", MVGBE_JMEM);
1504 error = ENOBUFS;
1505 goto out;
1506 }
1507
1508 state = 2;
1509 if (bus_dmamap_create(sc->sc_dmat, MVGBE_JMEM, 1, MVGBE_JMEM, 0,
1510 BUS_DMA_NOWAIT, &sc->sc_cdata.mvgbe_rx_jumbo_map)) {
1511 aprint_error_dev(sc->sc_dev, "can't create dma map\n");
1512 error = ENOBUFS;
1513 goto out;
1514 }
1515
1516 state = 3;
1517 if (bus_dmamap_load(sc->sc_dmat, sc->sc_cdata.mvgbe_rx_jumbo_map,
1518 kva, MVGBE_JMEM, NULL, BUS_DMA_NOWAIT)) {
1519 aprint_error_dev(sc->sc_dev, "can't load dma map\n");
1520 error = ENOBUFS;
1521 goto out;
1522 }
1523
1524 state = 4;
1525 sc->sc_cdata.mvgbe_jumbo_buf = (void *)kva;
1526 DPRINTFN(1,("mvgbe_jumbo_buf = %p\n", sc->sc_cdata.mvgbe_jumbo_buf));
1527
1528 LIST_INIT(&sc->sc_jfree_listhead);
1529 LIST_INIT(&sc->sc_jinuse_listhead);
1530
1531 /*
1532 * Now divide it up into 9K pieces and save the addresses
1533 * in an array.
1534 */
1535 ptr = sc->sc_cdata.mvgbe_jumbo_buf;
1536 for (i = 0; i < MVGBE_JSLOTS; i++) {
1537 sc->sc_cdata.mvgbe_jslots[i] = ptr;
1538 ptr += MVGBE_JLEN;
1539 entry = kmem_alloc(sizeof(struct mvgbe_jpool_entry), KM_SLEEP);
1540 if (entry == NULL) {
1541 aprint_error_dev(sc->sc_dev,
1542 "no memory for jumbo buffer queue!\n");
1543 error = ENOBUFS;
1544 goto out;
1545 }
1546 entry->slot = i;
1547 if (i)
1548 LIST_INSERT_HEAD(&sc->sc_jfree_listhead, entry,
1549 jpool_entries);
1550 else
1551 LIST_INSERT_HEAD(&sc->sc_jinuse_listhead, entry,
1552 jpool_entries);
1553 }
1554 out:
1555 if (error != 0) {
1556 switch (state) {
1557 case 4:
1558 bus_dmamap_unload(sc->sc_dmat,
1559 sc->sc_cdata.mvgbe_rx_jumbo_map);
1560 case 3:
1561 bus_dmamap_destroy(sc->sc_dmat,
1562 sc->sc_cdata.mvgbe_rx_jumbo_map);
1563 case 2:
1564 bus_dmamem_unmap(sc->sc_dmat, kva, MVGBE_JMEM);
1565 case 1:
1566 bus_dmamem_free(sc->sc_dmat, &seg, rseg);
1567 break;
1568 default:
1569 break;
1570 }
1571 }
1572
1573 return error;
1574 }
1575
1576 /*
1577 * Allocate a jumbo buffer.
1578 */
1579 static void *
1580 mvgbe_jalloc(struct mvgbe_softc *sc)
1581 {
1582 struct mvgbe_jpool_entry *entry;
1583
1584 entry = LIST_FIRST(&sc->sc_jfree_listhead);
1585
1586 if (entry == NULL)
1587 return NULL;
1588
1589 LIST_REMOVE(entry, jpool_entries);
1590 LIST_INSERT_HEAD(&sc->sc_jinuse_listhead, entry, jpool_entries);
1591 return sc->sc_cdata.mvgbe_jslots[entry->slot];
1592 }
1593
1594 /*
1595 * Release a jumbo buffer.
1596 */
1597 static void
1598 mvgbe_jfree(struct mbuf *m, void *buf, size_t size, void *arg)
1599 {
1600 struct mvgbe_jpool_entry *entry;
1601 struct mvgbe_softc *sc;
1602 int i, s;
1603
1604 /* Extract the softc struct pointer. */
1605 sc = (struct mvgbe_softc *)arg;
1606
1607 if (sc == NULL)
1608 panic("%s: can't find softc pointer!", __func__);
1609
1610 /* calculate the slot this buffer belongs to */
1611
1612 i = ((vaddr_t)buf - (vaddr_t)sc->sc_cdata.mvgbe_jumbo_buf) / MVGBE_JLEN;
1613
1614 if ((i < 0) || (i >= MVGBE_JSLOTS))
1615 panic("%s: asked to free buffer that we don't manage!",
1616 __func__);
1617
1618 s = splvm();
1619 entry = LIST_FIRST(&sc->sc_jinuse_listhead);
1620 if (entry == NULL)
1621 panic("%s: buffer not in use!", __func__);
1622 entry->slot = i;
1623 LIST_REMOVE(entry, jpool_entries);
1624 LIST_INSERT_HEAD(&sc->sc_jfree_listhead, entry, jpool_entries);
1625
1626 if (__predict_true(m != NULL))
1627 pool_cache_put(mb_cache, m);
1628 splx(s);
1629 }
1630
1631 static int
1632 mvgbe_encap(struct mvgbe_softc *sc, struct mbuf *m_head,
1633 uint32_t *txidx)
1634 {
1635 struct mvgbe_tx_desc *f = NULL;
1636 struct mvgbe_txmap_entry *entry;
1637 bus_dma_segment_t *txseg;
1638 bus_dmamap_t txmap;
1639 uint32_t first, current, last, cmdsts = 0;
1640 int m_csumflags, i;
1641 bool needs_defrag = false;
1642
1643 DPRINTFN(3, ("mvgbe_encap\n"));
1644
1645 entry = SIMPLEQ_FIRST(&sc->sc_txmap_head);
1646 if (entry == NULL) {
1647 DPRINTFN(2, ("mvgbe_encap: no txmap available\n"));
1648 return ENOBUFS;
1649 }
1650 txmap = entry->dmamap;
1651
1652 first = current = last = *txidx;
1653
1654 /*
1655 * Preserve m_pkthdr.csum_flags here since m_head might be
1656 * updated by m_defrag()
1657 */
1658 m_csumflags = m_head->m_pkthdr.csum_flags;
1659
1660 do_defrag:
1661 if (__predict_false(needs_defrag == true)) {
1662 /* A small unaligned segment was detected. */
1663 struct mbuf *m_new;
1664 m_new = m_defrag(m_head, M_DONTWAIT);
1665 if (m_new == NULL)
1666 return EFBIG;
1667 m_head = m_new;
1668 }
1669
1670 /*
1671 * Start packing the mbufs in this chain into
1672 * the fragment pointers. Stop when we run out
1673 * of fragments or hit the end of the mbuf chain.
1674 */
1675 if (bus_dmamap_load_mbuf(sc->sc_dmat, txmap, m_head, BUS_DMA_NOWAIT)) {
1676 DPRINTFN(1, ("mvgbe_encap: dmamap failed\n"));
1677 return ENOBUFS;
1678 }
1679
1680 txseg = txmap->dm_segs;
1681
1682 if (__predict_true(needs_defrag == false)) {
1683 /*
1684 * Detect rarely encountered DMA limitation.
1685 */
1686 for (i = 0; i < txmap->dm_nsegs; i++) {
1687 if (((txseg[i].ds_addr & 7) != 0) &&
1688 (txseg[i].ds_len <= 8) &&
1689 (txseg[i].ds_len >= 1)
1690 ) {
1691 txseg = NULL;
1692 bus_dmamap_unload(sc->sc_dmat, txmap);
1693 needs_defrag = true;
1694 goto do_defrag;
1695 }
1696 }
1697 }
1698
1699 /* Sync the DMA map. */
1700 bus_dmamap_sync(sc->sc_dmat, txmap, 0, txmap->dm_mapsize,
1701 BUS_DMASYNC_PREWRITE);
1702
1703 if (sc->sc_cdata.mvgbe_tx_cnt + txmap->dm_nsegs >=
1704 MVGBE_TX_RING_CNT) {
1705 DPRINTFN(2, ("mvgbe_encap: too few descriptors free\n"));
1706 bus_dmamap_unload(sc->sc_dmat, txmap);
1707 return ENOBUFS;
1708 }
1709
1710
1711 DPRINTFN(2, ("mvgbe_encap: dm_nsegs=%d\n", txmap->dm_nsegs));
1712
1713 for (i = 0; i < txmap->dm_nsegs; i++) {
1714 f = &sc->sc_rdata->mvgbe_tx_ring[current];
1715 f->bufptr = txseg[i].ds_addr;
1716 f->bytecnt = txseg[i].ds_len;
1717 if (i != 0)
1718 f->cmdsts = MVGBE_BUFFER_OWNED_BY_DMA;
1719 last = current;
1720 current = MVGBE_TX_RING_NEXT(current);
1721 }
1722
1723 if (m_csumflags & M_CSUM_IPv4)
1724 cmdsts |= MVGBE_TX_GENERATE_IP_CHKSUM;
1725 if (m_csumflags & M_CSUM_TCPv4)
1726 cmdsts |=
1727 MVGBE_TX_GENERATE_L4_CHKSUM | MVGBE_TX_L4_TYPE_TCP;
1728 if (m_csumflags & M_CSUM_UDPv4)
1729 cmdsts |=
1730 MVGBE_TX_GENERATE_L4_CHKSUM | MVGBE_TX_L4_TYPE_UDP;
1731 if (m_csumflags & (M_CSUM_IPv4 | M_CSUM_TCPv4 | M_CSUM_UDPv4)) {
1732 const int iphdr_unitlen = sizeof(struct ip) / sizeof(uint32_t);
1733
1734 cmdsts |= MVGBE_TX_IP_NO_FRAG |
1735 MVGBE_TX_IP_HEADER_LEN(iphdr_unitlen); /* unit is 4B */
1736 }
1737 if (txmap->dm_nsegs == 1)
1738 f->cmdsts = cmdsts |
1739 MVGBE_TX_GENERATE_CRC |
1740 MVGBE_TX_ENABLE_INTERRUPT |
1741 MVGBE_TX_ZERO_PADDING |
1742 MVGBE_TX_FIRST_DESC |
1743 MVGBE_TX_LAST_DESC;
1744 else {
1745 f = &sc->sc_rdata->mvgbe_tx_ring[first];
1746 f->cmdsts = cmdsts |
1747 MVGBE_TX_GENERATE_CRC |
1748 MVGBE_TX_FIRST_DESC;
1749
1750 f = &sc->sc_rdata->mvgbe_tx_ring[last];
1751 f->cmdsts =
1752 MVGBE_BUFFER_OWNED_BY_DMA |
1753 MVGBE_TX_ENABLE_INTERRUPT |
1754 MVGBE_TX_ZERO_PADDING |
1755 MVGBE_TX_LAST_DESC;
1756
1757 /* Sync descriptors except first */
1758 MVGBE_CDTXSYNC(sc,
1759 (MVGBE_TX_RING_CNT - 1 == *txidx) ? 0 : (*txidx) + 1,
1760 txmap->dm_nsegs - 1,
1761 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1762 }
1763
1764 sc->sc_cdata.mvgbe_tx_chain[last].mvgbe_mbuf = m_head;
1765 SIMPLEQ_REMOVE_HEAD(&sc->sc_txmap_head, link);
1766 sc->sc_cdata.mvgbe_tx_map[last] = entry;
1767
1768 /* Finally, sync first descriptor */
1769 sc->sc_rdata->mvgbe_tx_ring[first].cmdsts |=
1770 MVGBE_BUFFER_OWNED_BY_DMA;
1771 MVGBE_CDTXSYNC(sc, *txidx, 1,
1772 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1773
1774 sc->sc_cdata.mvgbe_tx_cnt += i;
1775 *txidx = current;
1776
1777 DPRINTFN(3, ("mvgbe_encap: completed successfully\n"));
1778
1779 return 0;
1780 }
1781
1782 static void
1783 mvgbe_rxeof(struct mvgbe_softc *sc)
1784 {
1785 struct mvgbe_chain_data *cdata = &sc->sc_cdata;
1786 struct mvgbe_rx_desc *cur_rx;
1787 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1788 struct mbuf *m;
1789 bus_dmamap_t dmamap;
1790 uint32_t rxstat;
1791 uint16_t bufsize;
1792 int idx, cur, total_len;
1793
1794 idx = sc->sc_cdata.mvgbe_rx_prod;
1795
1796 DPRINTFN(3, ("mvgbe_rxeof %d\n", idx));
1797
1798 for (;;) {
1799 cur = idx;
1800
1801 /* Sync the descriptor */
1802 MVGBE_CDRXSYNC(sc, idx,
1803 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1804
1805 cur_rx = &sc->sc_rdata->mvgbe_rx_ring[idx];
1806
1807 if ((cur_rx->cmdsts & MVGBE_BUFFER_OWNED_MASK) ==
1808 MVGBE_BUFFER_OWNED_BY_DMA) {
1809 /* Invalidate the descriptor -- it's not ready yet */
1810 MVGBE_CDRXSYNC(sc, idx, BUS_DMASYNC_PREREAD);
1811 sc->sc_cdata.mvgbe_rx_prod = idx;
1812 break;
1813 }
1814 #ifdef DIAGNOSTIC
1815 if ((cur_rx->cmdsts &
1816 (MVGBE_RX_LAST_DESC | MVGBE_RX_FIRST_DESC)) !=
1817 (MVGBE_RX_LAST_DESC | MVGBE_RX_FIRST_DESC))
1818 panic(
1819 "mvgbe_rxeof: buffer size is smaller than packet");
1820 #endif
1821
1822 dmamap = sc->sc_cdata.mvgbe_rx_jumbo_map;
1823
1824 bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
1825 BUS_DMASYNC_POSTREAD);
1826
1827 m = cdata->mvgbe_rx_chain[idx].mvgbe_mbuf;
1828 cdata->mvgbe_rx_chain[idx].mvgbe_mbuf = NULL;
1829 total_len = cur_rx->bytecnt - ETHER_CRC_LEN;
1830 rxstat = cur_rx->cmdsts;
1831 bufsize = cur_rx->bufsize;
1832
1833 cdata->mvgbe_rx_map[idx] = NULL;
1834
1835 idx = MVGBE_RX_RING_NEXT(idx);
1836
1837 if (rxstat & MVGBE_ERROR_SUMMARY) {
1838 #if 0
1839 int err = rxstat & MVGBE_RX_ERROR_CODE_MASK;
1840
1841 if (err == MVGBE_RX_CRC_ERROR)
1842 ifp->if_ierrors++;
1843 if (err == MVGBE_RX_OVERRUN_ERROR)
1844 ifp->if_ierrors++;
1845 if (err == MVGBE_RX_MAX_FRAME_LEN_ERROR)
1846 ifp->if_ierrors++;
1847 if (err == MVGBE_RX_RESOURCE_ERROR)
1848 ifp->if_ierrors++;
1849 #else
1850 ifp->if_ierrors++;
1851 #endif
1852 mvgbe_newbuf(sc, cur, m, dmamap);
1853 continue;
1854 }
1855
1856 if (rxstat & MVGBE_RX_IP_FRAME_TYPE) {
1857 int flgs = 0;
1858
1859 /* Check IPv4 header checksum */
1860 flgs |= M_CSUM_IPv4;
1861 if (!(rxstat & MVGBE_RX_IP_HEADER_OK))
1862 flgs |= M_CSUM_IPv4_BAD;
1863 else if ((bufsize & MVGBE_RX_IP_FRAGMENT) == 0) {
1864 /*
1865 * Check TCPv4/UDPv4 checksum for
1866 * non-fragmented packet only.
1867 *
1868 * It seemd that sometimes
1869 * MVGBE_RX_L4_CHECKSUM_OK bit was set to 0
1870 * even if the checksum is correct and the
1871 * packet was not fragmented. So we don't set
1872 * M_CSUM_TCP_UDP_BAD even if csum bit is 0.
1873 */
1874
1875 if (((rxstat & MVGBE_RX_L4_TYPE_MASK) ==
1876 MVGBE_RX_L4_TYPE_TCP) &&
1877 ((rxstat & MVGBE_RX_L4_CHECKSUM_OK) != 0))
1878 flgs |= M_CSUM_TCPv4;
1879 else if (((rxstat & MVGBE_RX_L4_TYPE_MASK) ==
1880 MVGBE_RX_L4_TYPE_UDP) &&
1881 ((rxstat & MVGBE_RX_L4_CHECKSUM_OK) != 0))
1882 flgs |= M_CSUM_UDPv4;
1883 }
1884 m->m_pkthdr.csum_flags = flgs;
1885 }
1886
1887 /*
1888 * Try to allocate a new jumbo buffer. If that
1889 * fails, copy the packet to mbufs and put the
1890 * jumbo buffer back in the ring so it can be
1891 * re-used. If allocating mbufs fails, then we
1892 * have to drop the packet.
1893 */
1894 if (mvgbe_newbuf(sc, cur, NULL, dmamap) == ENOBUFS) {
1895 struct mbuf *m0;
1896
1897 m0 = m_devget(mtod(m, char *), total_len, 0, ifp, NULL);
1898 mvgbe_newbuf(sc, cur, m, dmamap);
1899 if (m0 == NULL) {
1900 aprint_error_ifnet(ifp,
1901 "no receive buffers available --"
1902 " packet dropped!\n");
1903 ifp->if_ierrors++;
1904 continue;
1905 }
1906 m = m0;
1907 } else {
1908 m->m_pkthdr.rcvif = ifp;
1909 m->m_pkthdr.len = m->m_len = total_len;
1910 }
1911
1912 /* Skip on first 2byte (HW header) */
1913 m_adj(m, MVGBE_HWHEADER_SIZE);
1914
1915 ifp->if_ipackets++;
1916
1917 bpf_mtap(ifp, m);
1918
1919 /* pass it on. */
1920 (*ifp->if_input)(ifp, m);
1921 }
1922 }
1923
1924 static void
1925 mvgbe_txeof(struct mvgbe_softc *sc)
1926 {
1927 struct mvgbe_chain_data *cdata = &sc->sc_cdata;
1928 struct mvgbe_tx_desc *cur_tx;
1929 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1930 struct mvgbe_txmap_entry *entry;
1931 int idx;
1932
1933 DPRINTFN(3, ("mvgbe_txeof\n"));
1934
1935 /*
1936 * Go through our tx ring and free mbufs for those
1937 * frames that have been sent.
1938 */
1939 idx = cdata->mvgbe_tx_cons;
1940 while (idx != cdata->mvgbe_tx_prod) {
1941 MVGBE_CDTXSYNC(sc, idx, 1,
1942 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1943
1944 cur_tx = &sc->sc_rdata->mvgbe_tx_ring[idx];
1945 #ifdef MVGBE_DEBUG
1946 if (mvgbe_debug >= 3)
1947 mvgbe_dump_txdesc(cur_tx, idx);
1948 #endif
1949 if ((cur_tx->cmdsts & MVGBE_BUFFER_OWNED_MASK) ==
1950 MVGBE_BUFFER_OWNED_BY_DMA) {
1951 MVGBE_CDTXSYNC(sc, idx, 1, BUS_DMASYNC_PREREAD);
1952 break;
1953 }
1954 if (cur_tx->cmdsts & MVGBE_TX_LAST_DESC)
1955 ifp->if_opackets++;
1956 if (cur_tx->cmdsts & MVGBE_ERROR_SUMMARY) {
1957 int err = cur_tx->cmdsts & MVGBE_TX_ERROR_CODE_MASK;
1958
1959 if (err == MVGBE_TX_LATE_COLLISION_ERROR)
1960 ifp->if_collisions++;
1961 if (err == MVGBE_TX_UNDERRUN_ERROR)
1962 ifp->if_oerrors++;
1963 if (err == MVGBE_TX_EXCESSIVE_COLLISION_ERRO)
1964 ifp->if_collisions++;
1965 }
1966 if (cdata->mvgbe_tx_chain[idx].mvgbe_mbuf != NULL) {
1967 entry = cdata->mvgbe_tx_map[idx];
1968
1969 m_freem(cdata->mvgbe_tx_chain[idx].mvgbe_mbuf);
1970 cdata->mvgbe_tx_chain[idx].mvgbe_mbuf = NULL;
1971
1972 bus_dmamap_sync(sc->sc_dmat, entry->dmamap, 0,
1973 entry->dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1974
1975 bus_dmamap_unload(sc->sc_dmat, entry->dmamap);
1976 SIMPLEQ_INSERT_TAIL(&sc->sc_txmap_head, entry, link);
1977 cdata->mvgbe_tx_map[idx] = NULL;
1978 }
1979 cdata->mvgbe_tx_cnt--;
1980 idx = MVGBE_TX_RING_NEXT(idx);
1981 }
1982 if (cdata->mvgbe_tx_cnt == 0)
1983 ifp->if_timer = 0;
1984
1985 if (cdata->mvgbe_tx_cnt < MVGBE_TX_RING_CNT - 2)
1986 ifp->if_flags &= ~IFF_OACTIVE;
1987
1988 cdata->mvgbe_tx_cons = idx;
1989 }
1990
1991 static uint8_t
1992 mvgbe_crc8(const uint8_t *data, size_t size)
1993 {
1994 int bit;
1995 uint8_t byte;
1996 uint8_t crc = 0;
1997 const uint8_t poly = 0x07;
1998
1999 while(size--)
2000 for (byte = *data++, bit = NBBY-1; bit >= 0; bit--)
2001 crc = (crc << 1) ^ ((((crc >> 7) ^ (byte >> bit)) & 1) ? poly : 0);
2002
2003 return crc;
2004 }
2005
2006 CTASSERT(MVGBE_NDFSMT == MVGBE_NDFOMT);
2007
2008 static void
2009 mvgbe_filter_setup(struct mvgbe_softc *sc)
2010 {
2011 struct ethercom *ec = &sc->sc_ethercom;
2012 struct ifnet *ifp= &sc->sc_ethercom.ec_if;
2013 struct ether_multi *enm;
2014 struct ether_multistep step;
2015 uint32_t dfut[MVGBE_NDFUT], dfsmt[MVGBE_NDFSMT], dfomt[MVGBE_NDFOMT];
2016 uint32_t pxc;
2017 int i;
2018 const uint8_t special[ETHER_ADDR_LEN] = {0x01,0x00,0x5e,0x00,0x00,0x00};
2019
2020 memset(dfut, 0, sizeof(dfut));
2021 memset(dfsmt, 0, sizeof(dfsmt));
2022 memset(dfomt, 0, sizeof(dfomt));
2023
2024 if (ifp->if_flags & (IFF_ALLMULTI|IFF_PROMISC)) {
2025 goto allmulti;
2026 }
2027
2028 ETHER_FIRST_MULTI(step, ec, enm);
2029 while (enm != NULL) {
2030 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
2031 /* ranges are complex and somewhat rare */
2032 goto allmulti;
2033 }
2034 /* chip handles some IPv4 multicast specially */
2035 if (memcmp(enm->enm_addrlo, special, 5) == 0) {
2036 i = enm->enm_addrlo[5];
2037 dfsmt[i>>2] =
2038 MVGBE_DF(i&3, MVGBE_DF_QUEUE(0) | MVGBE_DF_PASS);
2039 } else {
2040 i = mvgbe_crc8(enm->enm_addrlo, ETHER_ADDR_LEN);
2041 dfomt[i>>2] =
2042 MVGBE_DF(i&3, MVGBE_DF_QUEUE(0) | MVGBE_DF_PASS);
2043 }
2044
2045 ETHER_NEXT_MULTI(step, enm);
2046 }
2047 goto set;
2048
2049 allmulti:
2050 if (ifp->if_flags & (IFF_ALLMULTI|IFF_PROMISC)) {
2051 for (i = 0; i < MVGBE_NDFSMT; i++) {
2052 dfsmt[i] = dfomt[i] =
2053 MVGBE_DF(0, MVGBE_DF_QUEUE(0) | MVGBE_DF_PASS) |
2054 MVGBE_DF(1, MVGBE_DF_QUEUE(0) | MVGBE_DF_PASS) |
2055 MVGBE_DF(2, MVGBE_DF_QUEUE(0) | MVGBE_DF_PASS) |
2056 MVGBE_DF(3, MVGBE_DF_QUEUE(0) | MVGBE_DF_PASS);
2057 }
2058 }
2059
2060 set:
2061 pxc = MVGBE_READ(sc, MVGBE_PXC);
2062 pxc &= ~MVGBE_PXC_UPM;
2063 pxc |= MVGBE_PXC_RB | MVGBE_PXC_RBIP | MVGBE_PXC_RBARP;
2064 if (ifp->if_flags & IFF_BROADCAST) {
2065 pxc &= ~(MVGBE_PXC_RB | MVGBE_PXC_RBIP | MVGBE_PXC_RBARP);
2066 }
2067 if (ifp->if_flags & IFF_PROMISC) {
2068 pxc |= MVGBE_PXC_UPM;
2069 }
2070 MVGBE_WRITE(sc, MVGBE_PXC, pxc);
2071
2072 /* Set Destination Address Filter Unicast Table */
2073 i = sc->sc_enaddr[5] & 0xf; /* last nibble */
2074 dfut[i>>2] = MVGBE_DF(i&3, MVGBE_DF_QUEUE(0) | MVGBE_DF_PASS);
2075 MVGBE_WRITE_FILTER(sc, MVGBE_DFUT, dfut, MVGBE_NDFUT);
2076
2077 /* Set Destination Address Filter Multicast Tables */
2078 MVGBE_WRITE_FILTER(sc, MVGBE_DFSMT, dfsmt, MVGBE_NDFSMT);
2079 MVGBE_WRITE_FILTER(sc, MVGBE_DFOMT, dfomt, MVGBE_NDFOMT);
2080 }
2081
2082 #ifdef MVGBE_DEBUG
2083 static void
2084 mvgbe_dump_txdesc(struct mvgbe_tx_desc *desc, int idx)
2085 {
2086 #define DESC_PRINT(X) \
2087 if (X) \
2088 printf("txdesc[%d]." #X "=%#x\n", idx, X);
2089
2090 #if BYTE_ORDER == BIG_ENDIAN
2091 DESC_PRINT(desc->bytecnt);
2092 DESC_PRINT(desc->l4ichk);
2093 DESC_PRINT(desc->cmdsts);
2094 DESC_PRINT(desc->nextdescptr);
2095 DESC_PRINT(desc->bufptr);
2096 #else /* LITTLE_ENDIAN */
2097 DESC_PRINT(desc->cmdsts);
2098 DESC_PRINT(desc->l4ichk);
2099 DESC_PRINT(desc->bytecnt);
2100 DESC_PRINT(desc->bufptr);
2101 DESC_PRINT(desc->nextdescptr);
2102 #endif
2103 #undef DESC_PRINT
2104 }
2105 #endif
2106
2107 SYSCTL_SETUP(sysctl_mvgbe, "sysctl mvgbe subtree setup")
2108 {
2109 int rc;
2110 const struct sysctlnode *node;
2111
2112 if ((rc = sysctl_createv(clog, 0, NULL, NULL,
2113 0, CTLTYPE_NODE, "hw", NULL,
2114 NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0) {
2115 goto err;
2116 }
2117
2118 if ((rc = sysctl_createv(clog, 0, NULL, &node,
2119 0, CTLTYPE_NODE, "mvgbe",
2120 SYSCTL_DESCR("mvgbe interface controls"),
2121 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) {
2122 goto err;
2123 }
2124
2125 mvgbe_root_num = node->sysctl_num;
2126 return;
2127
2128 err:
2129 aprint_error("%s: syctl_createv failed (rc = %d)\n", __func__, rc);
2130 }
2131
2132 static void
2133 sysctl_mvgbe_init(struct mvgbe_softc *sc)
2134 {
2135 const struct sysctlnode *node;
2136 int mvgbe_nodenum;
2137
2138 if (sysctl_createv(&sc->mvgbe_clog, 0, NULL, &node,
2139 0, CTLTYPE_NODE, device_xname(sc->sc_dev),
2140 SYSCTL_DESCR("mvgbe per-controller controls"),
2141 NULL, 0, NULL, 0, CTL_HW, mvgbe_root_num, CTL_CREATE,
2142 CTL_EOL) != 0) {
2143 aprint_normal_dev(sc->sc_dev, "couldn't create sysctl node\n");
2144 return;
2145 }
2146 mvgbe_nodenum = node->sysctl_num;
2147
2148 /* interrupt moderation sysctls */
2149 if (sysctl_createv(&sc->mvgbe_clog, 0, NULL, &node,
2150 CTLFLAG_READWRITE, CTLTYPE_INT, "ipginttx",
2151 SYSCTL_DESCR("mvgbe TX interrupt moderation timer"),
2152 mvgbe_sysctl_ipginttx, 0, (void *)sc,
2153 0, CTL_HW, mvgbe_root_num, mvgbe_nodenum, CTL_CREATE,
2154 CTL_EOL) != 0) {
2155 aprint_normal_dev(sc->sc_dev,
2156 "couldn't create ipginttx sysctl node\n");
2157 }
2158 if (sysctl_createv(&sc->mvgbe_clog, 0, NULL, &node,
2159 CTLFLAG_READWRITE, CTLTYPE_INT, "ipgintrx",
2160 SYSCTL_DESCR("mvgbe RX interrupt moderation timer"),
2161 mvgbe_sysctl_ipgintrx, 0, (void *)sc,
2162 0, CTL_HW, mvgbe_root_num, mvgbe_nodenum, CTL_CREATE,
2163 CTL_EOL) != 0) {
2164 aprint_normal_dev(sc->sc_dev,
2165 "couldn't create ipginttx sysctl node\n");
2166 }
2167 }
2168
2169 static int
2170 mvgbe_sysctl_ipginttx(SYSCTLFN_ARGS)
2171 {
2172 int error;
2173 unsigned int t;
2174 struct sysctlnode node;
2175 struct mvgbec_softc *csc;
2176 struct mvgbe_softc *sc;
2177
2178 node = *rnode;
2179 sc = node.sysctl_data;
2180 csc = device_private(device_parent(sc->sc_dev));
2181 t = sc->sc_ipginttx;
2182 node.sysctl_data = &t;
2183 error = sysctl_lookup(SYSCTLFN_CALL(&node));
2184 if (error || newp == NULL)
2185 return error;
2186
2187 if (mvgbe_ipginttx(csc, sc, t) < 0)
2188 return EINVAL;
2189 /*
2190 * update the softc with sysctl-changed value, and mark
2191 * for hardware update
2192 */
2193 sc->sc_ipginttx = t;
2194
2195 return 0;
2196 }
2197
2198 static int
2199 mvgbe_sysctl_ipgintrx(SYSCTLFN_ARGS)
2200 {
2201 int error;
2202 unsigned int t;
2203 struct sysctlnode node;
2204 struct mvgbec_softc *csc;
2205 struct mvgbe_softc *sc;
2206
2207 node = *rnode;
2208 sc = node.sysctl_data;
2209 csc = device_private(device_parent(sc->sc_dev));
2210 t = sc->sc_ipgintrx;
2211 node.sysctl_data = &t;
2212 error = sysctl_lookup(SYSCTLFN_CALL(&node));
2213 if (error || newp == NULL)
2214 return error;
2215
2216 if (mvgbe_ipgintrx(csc, sc, t) < 0)
2217 return EINVAL;
2218 /*
2219 * update the softc with sysctl-changed value, and mark
2220 * for hardware update
2221 */
2222 sc->sc_ipgintrx = t;
2223
2224 return 0;
2225 }
2226