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