1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Fast Ethernet Controller (FEC) driver for Motorola MPC8xx.
4 * Copyright (c) 1997 Dan Malek (dmalek@jlc.net)
5 *
6 * Right now, I am very wasteful with the buffers. I allocate memory
7 * pages and then divide them into 2K frame buffers. This way I know I
8 * have buffers large enough to hold one frame within one buffer descriptor.
9 * Once I get this working, I will use 64 or 128 byte CPM buffers, which
10 * will be much more memory efficient and will easily handle lots of
11 * small packets.
12 *
13 * Much better multiple PHY support by Magnus Damm.
14 * Copyright (c) 2000 Ericsson Radio Systems AB.
15 *
16 * Support for FEC controller of ColdFire processors.
17 * Copyright (c) 2001-2005 Greg Ungerer (gerg@snapgear.com)
18 *
19 * Bug fixes and cleanup by Philippe De Muyter (phdm@macqel.be)
20 * Copyright (c) 2004-2006 Macq Electronique SA.
21 *
22 * Copyright (C) 2010-2011 Freescale Semiconductor, Inc.
23 */
24
25 #include <linux/bitops.h>
26 #include <linux/bpf.h>
27 #include <linux/bpf_trace.h>
28 #include <linux/cacheflush.h>
29 #include <linux/clk.h>
30 #include <linux/crc32.h>
31 #include <linux/delay.h>
32 #include <linux/errno.h>
33 #include <linux/etherdevice.h>
34 #include <linux/fec.h>
35 #include <linux/filter.h>
36 #include <linux/gpio/consumer.h>
37 #include <linux/icmp.h>
38 #include <linux/if_vlan.h>
39 #include <linux/in.h>
40 #include <linux/interrupt.h>
41 #include <linux/io.h>
42 #include <linux/ioport.h>
43 #include <linux/ip.h>
44 #include <linux/irq.h>
45 #include <linux/kernel.h>
46 #include <linux/mdio.h>
47 #include <linux/mfd/syscon.h>
48 #include <linux/module.h>
49 #include <linux/netdevice.h>
50 #include <linux/of.h>
51 #include <linux/of_mdio.h>
52 #include <linux/of_net.h>
53 #include <linux/phy.h>
54 #include <linux/pinctrl/consumer.h>
55 #include <linux/phy_fixed.h>
56 #include <linux/platform_device.h>
57 #include <linux/pm_runtime.h>
58 #include <linux/prefetch.h>
59 #include <linux/property.h>
60 #include <linux/ptrace.h>
61 #include <linux/regmap.h>
62 #include <linux/regulator/consumer.h>
63 #include <linux/skbuff.h>
64 #include <linux/slab.h>
65 #include <linux/spinlock.h>
66 #include <linux/string.h>
67 #include <linux/tcp.h>
68 #include <linux/udp.h>
69 #include <linux/workqueue.h>
70 #include <net/ip.h>
71 #include <net/page_pool/helpers.h>
72 #include <net/selftests.h>
73 #include <net/tso.h>
74 #include <net/xdp_sock_drv.h>
75 #include <soc/imx/cpuidle.h>
76
77 #include "fec.h"
78
79 static void set_multicast_list(struct net_device *ndev);
80 static void fec_enet_itr_coal_set(struct net_device *ndev);
81 static int fec_enet_xdp_tx_xmit(struct fec_enet_private *fep,
82 int cpu, struct xdp_buff *xdp,
83 u32 dma_sync_len, int queue);
84
85 #define DRIVER_NAME "fec"
86
87 static const u16 fec_enet_vlan_pri_to_queue[8] = {0, 0, 1, 1, 1, 2, 2, 2};
88
89 #define FEC_ENET_RSEM_V 0x84
90 #define FEC_ENET_RSFL_V 16
91 #define FEC_ENET_RAEM_V 0x8
92 #define FEC_ENET_RAFL_V 0x8
93 #define FEC_ENET_OPD_V 0xFFF0
94 #define FEC_MDIO_PM_TIMEOUT 100 /* ms */
95
96 #define FEC_ENET_XDP_PASS 0
97 #define FEC_ENET_XDP_CONSUMED BIT(0)
98 #define FEC_ENET_XDP_TX BIT(1)
99 #define FEC_ENET_XDP_REDIR BIT(2)
100
101 struct fec_devinfo {
102 u32 quirks;
103 };
104
105 static const struct fec_devinfo fec_imx25_info = {
106 .quirks = FEC_QUIRK_USE_GASKET | FEC_QUIRK_MIB_CLEAR |
107 FEC_QUIRK_HAS_FRREG | FEC_QUIRK_HAS_MDIO_C45,
108 };
109
110 static const struct fec_devinfo fec_imx27_info = {
111 .quirks = FEC_QUIRK_MIB_CLEAR | FEC_QUIRK_HAS_FRREG |
112 FEC_QUIRK_HAS_MDIO_C45,
113 };
114
115 static const struct fec_devinfo fec_imx28_info = {
116 .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME |
117 FEC_QUIRK_SINGLE_MDIO | FEC_QUIRK_HAS_RACC |
118 FEC_QUIRK_HAS_FRREG | FEC_QUIRK_CLEAR_SETUP_MII |
119 FEC_QUIRK_NO_HARD_RESET | FEC_QUIRK_HAS_MDIO_C45,
120 };
121
122 static const struct fec_devinfo fec_imx6q_info = {
123 .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
124 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
125 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR006358 |
126 FEC_QUIRK_HAS_RACC | FEC_QUIRK_CLEAR_SETUP_MII |
127 FEC_QUIRK_HAS_PMQOS | FEC_QUIRK_HAS_MDIO_C45,
128 };
129
130 static const struct fec_devinfo fec_mvf600_info = {
131 .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_RACC |
132 FEC_QUIRK_HAS_MDIO_C45,
133 };
134
135 static const struct fec_devinfo fec_imx6sx_info = {
136 .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
137 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
138 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB |
139 FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE |
140 FEC_QUIRK_HAS_RACC | FEC_QUIRK_HAS_COALESCE |
141 FEC_QUIRK_CLEAR_SETUP_MII | FEC_QUIRK_HAS_MULTI_QUEUES |
142 FEC_QUIRK_HAS_MDIO_C45,
143 };
144
145 static const struct fec_devinfo fec_imx6ul_info = {
146 .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
147 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
148 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR007885 |
149 FEC_QUIRK_BUG_CAPTURE | FEC_QUIRK_HAS_RACC |
150 FEC_QUIRK_HAS_COALESCE | FEC_QUIRK_CLEAR_SETUP_MII |
151 FEC_QUIRK_HAS_MDIO_C45,
152 };
153
154 static const struct fec_devinfo fec_imx8mq_info = {
155 .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
156 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
157 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB |
158 FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE |
159 FEC_QUIRK_HAS_RACC | FEC_QUIRK_HAS_COALESCE |
160 FEC_QUIRK_CLEAR_SETUP_MII | FEC_QUIRK_HAS_MULTI_QUEUES |
161 FEC_QUIRK_HAS_EEE | FEC_QUIRK_WAKEUP_FROM_INT2 |
162 FEC_QUIRK_HAS_MDIO_C45,
163 };
164
165 static const struct fec_devinfo fec_imx8qm_info = {
166 .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
167 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
168 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB |
169 FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE |
170 FEC_QUIRK_HAS_RACC | FEC_QUIRK_HAS_COALESCE |
171 FEC_QUIRK_CLEAR_SETUP_MII | FEC_QUIRK_HAS_MULTI_QUEUES |
172 FEC_QUIRK_DELAYED_CLKS_SUPPORT | FEC_QUIRK_HAS_MDIO_C45 |
173 FEC_QUIRK_JUMBO_FRAME,
174 };
175
176 static const struct fec_devinfo fec_s32v234_info = {
177 .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
178 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
179 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB |
180 FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE |
181 FEC_QUIRK_HAS_MDIO_C45,
182 };
183
184 static struct platform_device_id fec_devtype[] = {
185 {
186 /* keep it for coldfire */
187 .name = DRIVER_NAME,
188 .driver_data = 0,
189 }, {
190 /* sentinel */
191 }
192 };
193 MODULE_DEVICE_TABLE(platform, fec_devtype);
194
195 static const struct of_device_id fec_dt_ids[] = {
196 { .compatible = "fsl,imx25-fec", .data = &fec_imx25_info, },
197 { .compatible = "fsl,imx27-fec", .data = &fec_imx27_info, },
198 { .compatible = "fsl,imx28-fec", .data = &fec_imx28_info, },
199 { .compatible = "fsl,imx6q-fec", .data = &fec_imx6q_info, },
200 { .compatible = "fsl,mvf600-fec", .data = &fec_mvf600_info, },
201 { .compatible = "fsl,imx6sx-fec", .data = &fec_imx6sx_info, },
202 { .compatible = "fsl,imx6ul-fec", .data = &fec_imx6ul_info, },
203 { .compatible = "fsl,imx8mq-fec", .data = &fec_imx8mq_info, },
204 { .compatible = "fsl,imx8qm-fec", .data = &fec_imx8qm_info, },
205 { .compatible = "fsl,s32v234-fec", .data = &fec_s32v234_info, },
206 { /* sentinel */ }
207 };
208 MODULE_DEVICE_TABLE(of, fec_dt_ids);
209
210 static unsigned char macaddr[ETH_ALEN];
211 module_param_array(macaddr, byte, NULL, 0);
212 MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address");
213
214 #if defined(CONFIG_M5272)
215 /*
216 * Some hardware gets it MAC address out of local flash memory.
217 * if this is non-zero then assume it is the address to get MAC from.
218 */
219 #if defined(CONFIG_NETtel)
220 #define FEC_FLASHMAC 0xf0006006
221 #elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
222 #define FEC_FLASHMAC 0xf0006000
223 #elif defined(CONFIG_CANCam)
224 #define FEC_FLASHMAC 0xf0020000
225 #elif defined (CONFIG_M5272C3)
226 #define FEC_FLASHMAC (0xffe04000 + 4)
227 #elif defined(CONFIG_MOD5272)
228 #define FEC_FLASHMAC 0xffc0406b
229 #else
230 #define FEC_FLASHMAC 0
231 #endif
232 #endif /* CONFIG_M5272 */
233
234 /* The FEC stores dest/src/type/vlan, data, and checksum for receive packets.
235 *
236 * 2048 byte skbufs are allocated. However, alignment requirements
237 * varies between FEC variants. Worst case is 64, so round down by 64.
238 */
239 #define MAX_JUMBO_BUF_SIZE (round_down(16384 - FEC_DRV_RESERVE_SPACE - 64, 64))
240 #define PKT_MAXBUF_SIZE (round_down(2048 - 64, 64))
241 #define PKT_MINBUF_SIZE 64
242
243 /* FEC receive acceleration */
244 #define FEC_RACC_IPDIS BIT(1)
245 #define FEC_RACC_PRODIS BIT(2)
246 #define FEC_RACC_SHIFT16 BIT(7)
247 #define FEC_RACC_OPTIONS (FEC_RACC_IPDIS | FEC_RACC_PRODIS)
248
249 /* MIB Control Register */
250 #define FEC_MIB_CTRLSTAT_DISABLE BIT(31)
251
252 /*
253 * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
254 * size bits. Other FEC hardware does not, so we need to take that into
255 * account when setting it.
256 */
257 #ifndef CONFIG_M5272
258 #define OPT_ARCH_HAS_MAX_FL 1
259 #else
260 #define OPT_ARCH_HAS_MAX_FL 0
261 #endif
262
263 /* FEC MII MMFR bits definition */
264 #define FEC_MMFR_ST (1 << 30)
265 #define FEC_MMFR_ST_C45 (0)
266 #define FEC_MMFR_OP_READ (2 << 28)
267 #define FEC_MMFR_OP_READ_C45 (3 << 28)
268 #define FEC_MMFR_OP_WRITE (1 << 28)
269 #define FEC_MMFR_OP_ADDR_WRITE (0)
270 #define FEC_MMFR_PA(v) ((v & 0x1f) << 23)
271 #define FEC_MMFR_RA(v) ((v & 0x1f) << 18)
272 #define FEC_MMFR_TA (2 << 16)
273 #define FEC_MMFR_DATA(v) (v & 0xffff)
274 /* FEC ECR bits definition */
275 #define FEC_ECR_RESET BIT(0)
276 #define FEC_ECR_ETHEREN BIT(1)
277 #define FEC_ECR_MAGICEN BIT(2)
278 #define FEC_ECR_SLEEP BIT(3)
279 #define FEC_ECR_EN1588 BIT(4)
280 #define FEC_ECR_SPEED BIT(5)
281 #define FEC_ECR_BYTESWP BIT(8)
282 /* FEC RCR bits definition */
283 #define FEC_RCR_LOOP BIT(0)
284 #define FEC_RCR_DRT BIT(1)
285 #define FEC_RCR_MII BIT(2)
286 #define FEC_RCR_PROMISC BIT(3)
287 #define FEC_RCR_BC_REJ BIT(4)
288 #define FEC_RCR_FLOWCTL BIT(5)
289 #define FEC_RCR_RGMII BIT(6)
290 #define FEC_RCR_RMII BIT(8)
291 #define FEC_RCR_10BASET BIT(9)
292 #define FEC_RCR_NLC BIT(30)
293 /* TX WMARK bits */
294 #define FEC_TXWMRK_STRFWD BIT(8)
295
296 #define FEC_MII_TIMEOUT 30000 /* us */
297
298 /* Transmitter timeout */
299 #define TX_TIMEOUT (2 * HZ)
300
301 #define FEC_PAUSE_FLAG_AUTONEG 0x1
302 #define FEC_PAUSE_FLAG_ENABLE 0x2
303 #define FEC_WOL_HAS_MAGIC_PACKET (0x1 << 0)
304 #define FEC_WOL_FLAG_ENABLE (0x1 << 1)
305 #define FEC_WOL_FLAG_SLEEP_ON (0x1 << 2)
306
307 /* Max number of allowed TCP segments for software TSO */
308 #define FEC_MAX_TSO_SEGS 100
309 #define FEC_MAX_SKB_DESCS (FEC_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
310
311 #define IS_TSO_HEADER(txq, addr) \
312 ((addr >= txq->tso_hdrs_dma) && \
313 (addr < txq->tso_hdrs_dma + txq->bd.ring_size * TSO_HEADER_SIZE))
314
315 static int mii_cnt;
316
fec_enet_get_nextdesc(struct bufdesc * bdp,struct bufdesc_prop * bd)317 static struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp,
318 struct bufdesc_prop *bd)
319 {
320 return (bdp >= bd->last) ? bd->base
321 : (struct bufdesc *)(((void *)bdp) + bd->dsize);
322 }
323
fec_enet_get_prevdesc(struct bufdesc * bdp,struct bufdesc_prop * bd)324 static struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp,
325 struct bufdesc_prop *bd)
326 {
327 return (bdp <= bd->base) ? bd->last
328 : (struct bufdesc *)(((void *)bdp) - bd->dsize);
329 }
330
fec_enet_get_bd_index(struct bufdesc * bdp,struct bufdesc_prop * bd)331 static int fec_enet_get_bd_index(struct bufdesc *bdp,
332 struct bufdesc_prop *bd)
333 {
334 return ((const char *)bdp - (const char *)bd->base) >> bd->dsize_log2;
335 }
336
fec_enet_get_free_txdesc_num(struct fec_enet_priv_tx_q * txq)337 static int fec_enet_get_free_txdesc_num(struct fec_enet_priv_tx_q *txq)
338 {
339 int entries;
340
341 entries = (((const char *)txq->dirty_tx -
342 (const char *)txq->bd.cur) >> txq->bd.dsize_log2) - 1;
343
344 return entries >= 0 ? entries : entries + txq->bd.ring_size;
345 }
346
swap_buffer(void * bufaddr,int len)347 static void swap_buffer(void *bufaddr, int len)
348 {
349 int i;
350 unsigned int *buf = bufaddr;
351
352 for (i = 0; i < len; i += 4, buf++)
353 swab32s(buf);
354 }
355
fec_dump(struct net_device * ndev)356 static void fec_dump(struct net_device *ndev)
357 {
358 struct fec_enet_private *fep = netdev_priv(ndev);
359 struct bufdesc *bdp;
360 struct fec_enet_priv_tx_q *txq;
361 int index = 0;
362
363 netdev_info(ndev, "TX ring dump\n");
364 pr_info("Nr SC addr len SKB\n");
365
366 txq = fep->tx_queue[0];
367 bdp = txq->bd.base;
368
369 do {
370 pr_info("%3u %c%c 0x%04x 0x%08x %4u %p\n",
371 index,
372 bdp == txq->bd.cur ? 'S' : ' ',
373 bdp == txq->dirty_tx ? 'H' : ' ',
374 fec16_to_cpu(bdp->cbd_sc),
375 fec32_to_cpu(bdp->cbd_bufaddr),
376 fec16_to_cpu(bdp->cbd_datlen),
377 txq->tx_buf[index].buf_p);
378 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
379 index++;
380 } while (bdp != txq->bd.base);
381 }
382
383 /*
384 * Coldfire does not support DMA coherent allocations, and has historically used
385 * a band-aid with a manual flush in fec_enet_rx_queue.
386 */
387 #if defined(CONFIG_COLDFIRE) && !defined(CONFIG_COLDFIRE_COHERENT_DMA)
fec_dma_alloc(struct device * dev,size_t size,dma_addr_t * handle,gfp_t gfp)388 static void *fec_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
389 gfp_t gfp)
390 {
391 return dma_alloc_noncoherent(dev, size, handle, DMA_BIDIRECTIONAL, gfp);
392 }
393
fec_dma_free(struct device * dev,size_t size,void * cpu_addr,dma_addr_t handle)394 static void fec_dma_free(struct device *dev, size_t size, void *cpu_addr,
395 dma_addr_t handle)
396 {
397 dma_free_noncoherent(dev, size, cpu_addr, handle, DMA_BIDIRECTIONAL);
398 }
399 #else /* !CONFIG_COLDFIRE || CONFIG_COLDFIRE_COHERENT_DMA */
fec_dma_alloc(struct device * dev,size_t size,dma_addr_t * handle,gfp_t gfp)400 static void *fec_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
401 gfp_t gfp)
402 {
403 return dma_alloc_coherent(dev, size, handle, gfp);
404 }
405
fec_dma_free(struct device * dev,size_t size,void * cpu_addr,dma_addr_t handle)406 static void fec_dma_free(struct device *dev, size_t size, void *cpu_addr,
407 dma_addr_t handle)
408 {
409 dma_free_coherent(dev, size, cpu_addr, handle);
410 }
411 #endif /* !CONFIG_COLDFIRE || CONFIG_COLDFIRE_COHERENT_DMA */
412
413 struct fec_dma_devres {
414 size_t size;
415 void *vaddr;
416 dma_addr_t dma_handle;
417 };
418
fec_dmam_release(struct device * dev,void * res)419 static void fec_dmam_release(struct device *dev, void *res)
420 {
421 struct fec_dma_devres *this = res;
422
423 fec_dma_free(dev, this->size, this->vaddr, this->dma_handle);
424 }
425
fec_dmam_alloc(struct device * dev,size_t size,dma_addr_t * handle,gfp_t gfp)426 static void *fec_dmam_alloc(struct device *dev, size_t size, dma_addr_t *handle,
427 gfp_t gfp)
428 {
429 struct fec_dma_devres *dr;
430 void *vaddr;
431
432 dr = devres_alloc(fec_dmam_release, sizeof(*dr), gfp);
433 if (!dr)
434 return NULL;
435 vaddr = fec_dma_alloc(dev, size, handle, gfp);
436 if (!vaddr) {
437 devres_free(dr);
438 return NULL;
439 }
440 dr->vaddr = vaddr;
441 dr->dma_handle = *handle;
442 dr->size = size;
443 devres_add(dev, dr);
444 return vaddr;
445 }
446
is_ipv4_pkt(struct sk_buff * skb)447 static inline bool is_ipv4_pkt(struct sk_buff *skb)
448 {
449 return skb->protocol == htons(ETH_P_IP) && ip_hdr(skb)->version == 4;
450 }
451
452 static int
fec_enet_clear_csum(struct sk_buff * skb,struct net_device * ndev)453 fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev)
454 {
455 /* Only run for packets requiring a checksum. */
456 if (skb->ip_summed != CHECKSUM_PARTIAL)
457 return 0;
458
459 if (unlikely(skb_cow_head(skb, 0)))
460 return -1;
461
462 if (is_ipv4_pkt(skb))
463 ip_hdr(skb)->check = 0;
464 *(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0;
465
466 return 0;
467 }
468
469 static int
fec_enet_create_page_pool(struct fec_enet_private * fep,struct fec_enet_priv_rx_q * rxq)470 fec_enet_create_page_pool(struct fec_enet_private *fep,
471 struct fec_enet_priv_rx_q *rxq)
472 {
473 struct bpf_prog *xdp_prog = READ_ONCE(fep->xdp_prog);
474 struct page_pool_params pp_params = {
475 .order = fep->pagepool_order,
476 .flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV,
477 .pool_size = rxq->bd.ring_size,
478 .nid = dev_to_node(&fep->pdev->dev),
479 .dev = &fep->pdev->dev,
480 .dma_dir = xdp_prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE,
481 .offset = FEC_ENET_XDP_HEADROOM,
482 .max_len = fep->rx_frame_size,
483 };
484 int err;
485
486 rxq->page_pool = page_pool_create(&pp_params);
487 if (IS_ERR(rxq->page_pool)) {
488 err = PTR_ERR(rxq->page_pool);
489 rxq->page_pool = NULL;
490 return err;
491 }
492
493 return 0;
494 }
495
fec_txq_trigger_xmit(struct fec_enet_private * fep,struct fec_enet_priv_tx_q * txq)496 static void fec_txq_trigger_xmit(struct fec_enet_private *fep,
497 struct fec_enet_priv_tx_q *txq)
498 {
499 if (!(fep->quirks & FEC_QUIRK_ERR007885) ||
500 !readl(txq->bd.reg_desc_active) ||
501 !readl(txq->bd.reg_desc_active) ||
502 !readl(txq->bd.reg_desc_active) ||
503 !readl(txq->bd.reg_desc_active))
504 writel(0, txq->bd.reg_desc_active);
505 }
506
507 static struct bufdesc *
fec_enet_txq_submit_frag_skb(struct fec_enet_priv_tx_q * txq,struct sk_buff * skb,struct net_device * ndev)508 fec_enet_txq_submit_frag_skb(struct fec_enet_priv_tx_q *txq,
509 struct sk_buff *skb,
510 struct net_device *ndev)
511 {
512 struct fec_enet_private *fep = netdev_priv(ndev);
513 struct bufdesc *bdp = txq->bd.cur;
514 struct bufdesc_ex *ebdp;
515 int nr_frags = skb_shinfo(skb)->nr_frags;
516 int frag, frag_len;
517 unsigned short status;
518 unsigned int estatus = 0;
519 skb_frag_t *this_frag;
520 unsigned int index;
521 void *bufaddr;
522 dma_addr_t addr;
523 int i;
524
525 for (frag = 0; frag < nr_frags; frag++) {
526 this_frag = &skb_shinfo(skb)->frags[frag];
527 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
528 ebdp = (struct bufdesc_ex *)bdp;
529
530 status = fec16_to_cpu(bdp->cbd_sc);
531 status &= ~BD_ENET_TX_STATS;
532 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
533 frag_len = skb_frag_size(&skb_shinfo(skb)->frags[frag]);
534
535 /* Handle the last BD specially */
536 if (frag == nr_frags - 1) {
537 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
538 if (fep->bufdesc_ex) {
539 estatus |= BD_ENET_TX_INT;
540 if (unlikely(skb_shinfo(skb)->tx_flags &
541 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
542 estatus |= BD_ENET_TX_TS;
543 }
544 }
545
546 if (fep->bufdesc_ex) {
547 if (fep->quirks & FEC_QUIRK_HAS_AVB)
548 estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
549 if (skb->ip_summed == CHECKSUM_PARTIAL)
550 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
551
552 ebdp->cbd_bdu = 0;
553 ebdp->cbd_esc = cpu_to_fec32(estatus);
554 }
555
556 bufaddr = skb_frag_address(this_frag);
557
558 index = fec_enet_get_bd_index(bdp, &txq->bd);
559 if (((unsigned long) bufaddr) & fep->tx_align ||
560 fep->quirks & FEC_QUIRK_SWAP_FRAME) {
561 memcpy(txq->tx_bounce[index], bufaddr, frag_len);
562 bufaddr = txq->tx_bounce[index];
563
564 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
565 swap_buffer(bufaddr, frag_len);
566 }
567
568 addr = dma_map_single(&fep->pdev->dev, bufaddr, frag_len,
569 DMA_TO_DEVICE);
570 if (dma_mapping_error(&fep->pdev->dev, addr)) {
571 if (net_ratelimit())
572 netdev_err(ndev, "Tx DMA memory map failed\n");
573 goto dma_mapping_error;
574 }
575
576 bdp->cbd_bufaddr = cpu_to_fec32(addr);
577 bdp->cbd_datlen = cpu_to_fec16(frag_len);
578 /* Make sure the updates to rest of the descriptor are
579 * performed before transferring ownership.
580 */
581 wmb();
582 bdp->cbd_sc = cpu_to_fec16(status);
583 }
584
585 return bdp;
586 dma_mapping_error:
587 bdp = txq->bd.cur;
588 for (i = 0; i < frag; i++) {
589 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
590 dma_unmap_single(&fep->pdev->dev, fec32_to_cpu(bdp->cbd_bufaddr),
591 fec16_to_cpu(bdp->cbd_datlen), DMA_TO_DEVICE);
592 }
593 return ERR_PTR(-ENOMEM);
594 }
595
fec_enet_txq_submit_skb(struct fec_enet_priv_tx_q * txq,struct sk_buff * skb,struct net_device * ndev)596 static int fec_enet_txq_submit_skb(struct fec_enet_priv_tx_q *txq,
597 struct sk_buff *skb, struct net_device *ndev)
598 {
599 struct fec_enet_private *fep = netdev_priv(ndev);
600 int nr_frags = skb_shinfo(skb)->nr_frags;
601 struct bufdesc *bdp, *last_bdp;
602 void *bufaddr;
603 dma_addr_t addr;
604 unsigned short status;
605 unsigned short buflen;
606 unsigned int estatus = 0;
607 unsigned int index;
608 int entries_free;
609
610 entries_free = fec_enet_get_free_txdesc_num(txq);
611 if (entries_free < MAX_SKB_FRAGS + 1) {
612 dev_kfree_skb_any(skb);
613 if (net_ratelimit())
614 netdev_err(ndev, "NOT enough BD for SG!\n");
615 return NETDEV_TX_OK;
616 }
617
618 /* Protocol checksum off-load for TCP and UDP. */
619 if (fec_enet_clear_csum(skb, ndev)) {
620 dev_kfree_skb_any(skb);
621 return NETDEV_TX_OK;
622 }
623
624 /* Fill in a Tx ring entry */
625 bdp = txq->bd.cur;
626 last_bdp = bdp;
627 status = fec16_to_cpu(bdp->cbd_sc);
628 status &= ~BD_ENET_TX_STATS;
629
630 /* Set buffer length and buffer pointer */
631 bufaddr = skb->data;
632 buflen = skb_headlen(skb);
633
634 index = fec_enet_get_bd_index(bdp, &txq->bd);
635 if (((unsigned long) bufaddr) & fep->tx_align ||
636 fep->quirks & FEC_QUIRK_SWAP_FRAME) {
637 memcpy(txq->tx_bounce[index], skb->data, buflen);
638 bufaddr = txq->tx_bounce[index];
639
640 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
641 swap_buffer(bufaddr, buflen);
642 }
643
644 /* Push the data cache so the CPM does not get stale memory data. */
645 addr = dma_map_single(&fep->pdev->dev, bufaddr, buflen, DMA_TO_DEVICE);
646 if (dma_mapping_error(&fep->pdev->dev, addr)) {
647 dev_kfree_skb_any(skb);
648 if (net_ratelimit())
649 netdev_err(ndev, "Tx DMA memory map failed\n");
650 return NETDEV_TX_OK;
651 }
652
653 if (nr_frags) {
654 last_bdp = fec_enet_txq_submit_frag_skb(txq, skb, ndev);
655 if (IS_ERR(last_bdp)) {
656 dma_unmap_single(&fep->pdev->dev, addr,
657 buflen, DMA_TO_DEVICE);
658 dev_kfree_skb_any(skb);
659 return NETDEV_TX_OK;
660 }
661 } else {
662 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
663 if (fep->bufdesc_ex) {
664 estatus = BD_ENET_TX_INT;
665 if (unlikely(skb_shinfo(skb)->tx_flags &
666 SKBTX_HW_TSTAMP && fep->hwts_tx_en))
667 estatus |= BD_ENET_TX_TS;
668 }
669 }
670 bdp->cbd_bufaddr = cpu_to_fec32(addr);
671 bdp->cbd_datlen = cpu_to_fec16(buflen);
672
673 if (fep->bufdesc_ex) {
674
675 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
676
677 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
678 fep->hwts_tx_en))
679 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
680
681 if (fep->quirks & FEC_QUIRK_HAS_AVB)
682 estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
683
684 if (skb->ip_summed == CHECKSUM_PARTIAL)
685 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
686
687 ebdp->cbd_bdu = 0;
688 ebdp->cbd_esc = cpu_to_fec32(estatus);
689 }
690
691 index = fec_enet_get_bd_index(last_bdp, &txq->bd);
692 /* Save skb pointer */
693 txq->tx_buf[index].buf_p = skb;
694
695 /* Make sure the updates to rest of the descriptor are performed before
696 * transferring ownership.
697 */
698 wmb();
699
700 /* Send it on its way. Tell FEC it's ready, interrupt when done,
701 * it's the last BD of the frame, and to put the CRC on the end.
702 */
703 status |= (BD_ENET_TX_READY | BD_ENET_TX_TC);
704 bdp->cbd_sc = cpu_to_fec16(status);
705
706 /* If this was the last BD in the ring, start at the beginning again. */
707 bdp = fec_enet_get_nextdesc(last_bdp, &txq->bd);
708
709 skb_tx_timestamp(skb);
710
711 /* Make sure the update to bdp is performed before txq->bd.cur. */
712 wmb();
713 txq->bd.cur = bdp;
714
715 /* Trigger transmission start */
716 fec_txq_trigger_xmit(fep, txq);
717
718 return 0;
719 }
720
721 static int
fec_enet_txq_put_data_tso(struct fec_enet_priv_tx_q * txq,struct sk_buff * skb,struct net_device * ndev,struct bufdesc * bdp,int index,char * data,int size,bool last_tcp,bool is_last)722 fec_enet_txq_put_data_tso(struct fec_enet_priv_tx_q *txq, struct sk_buff *skb,
723 struct net_device *ndev,
724 struct bufdesc *bdp, int index, char *data,
725 int size, bool last_tcp, bool is_last)
726 {
727 struct fec_enet_private *fep = netdev_priv(ndev);
728 struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
729 unsigned short status;
730 unsigned int estatus = 0;
731 dma_addr_t addr;
732
733 status = fec16_to_cpu(bdp->cbd_sc);
734 status &= ~BD_ENET_TX_STATS;
735
736 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
737
738 if (((unsigned long) data) & fep->tx_align ||
739 fep->quirks & FEC_QUIRK_SWAP_FRAME) {
740 memcpy(txq->tx_bounce[index], data, size);
741 data = txq->tx_bounce[index];
742
743 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
744 swap_buffer(data, size);
745 }
746
747 addr = dma_map_single(&fep->pdev->dev, data, size, DMA_TO_DEVICE);
748 if (dma_mapping_error(&fep->pdev->dev, addr)) {
749 dev_kfree_skb_any(skb);
750 if (net_ratelimit())
751 netdev_err(ndev, "Tx DMA memory map failed\n");
752 return NETDEV_TX_OK;
753 }
754
755 bdp->cbd_datlen = cpu_to_fec16(size);
756 bdp->cbd_bufaddr = cpu_to_fec32(addr);
757
758 if (fep->bufdesc_ex) {
759 if (fep->quirks & FEC_QUIRK_HAS_AVB)
760 estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
761 if (skb->ip_summed == CHECKSUM_PARTIAL)
762 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
763 ebdp->cbd_bdu = 0;
764 ebdp->cbd_esc = cpu_to_fec32(estatus);
765 }
766
767 /* Handle the last BD specially */
768 if (last_tcp)
769 status |= (BD_ENET_TX_LAST | BD_ENET_TX_TC);
770 if (is_last) {
771 status |= BD_ENET_TX_INTR;
772 if (fep->bufdesc_ex)
773 ebdp->cbd_esc |= cpu_to_fec32(BD_ENET_TX_INT);
774 }
775
776 bdp->cbd_sc = cpu_to_fec16(status);
777
778 return 0;
779 }
780
781 static int
fec_enet_txq_put_hdr_tso(struct fec_enet_priv_tx_q * txq,struct sk_buff * skb,struct net_device * ndev,struct bufdesc * bdp,int index)782 fec_enet_txq_put_hdr_tso(struct fec_enet_priv_tx_q *txq,
783 struct sk_buff *skb, struct net_device *ndev,
784 struct bufdesc *bdp, int index)
785 {
786 struct fec_enet_private *fep = netdev_priv(ndev);
787 int hdr_len = skb_tcp_all_headers(skb);
788 struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
789 void *bufaddr;
790 unsigned long dmabuf;
791 unsigned short status;
792 unsigned int estatus = 0;
793
794 status = fec16_to_cpu(bdp->cbd_sc);
795 status &= ~BD_ENET_TX_STATS;
796 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
797
798 bufaddr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
799 dmabuf = txq->tso_hdrs_dma + index * TSO_HEADER_SIZE;
800 if (((unsigned long)bufaddr) & fep->tx_align ||
801 fep->quirks & FEC_QUIRK_SWAP_FRAME) {
802 memcpy(txq->tx_bounce[index], skb->data, hdr_len);
803 bufaddr = txq->tx_bounce[index];
804
805 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
806 swap_buffer(bufaddr, hdr_len);
807
808 dmabuf = dma_map_single(&fep->pdev->dev, bufaddr,
809 hdr_len, DMA_TO_DEVICE);
810 if (dma_mapping_error(&fep->pdev->dev, dmabuf)) {
811 dev_kfree_skb_any(skb);
812 if (net_ratelimit())
813 netdev_err(ndev, "Tx DMA memory map failed\n");
814 return NETDEV_TX_OK;
815 }
816 }
817
818 bdp->cbd_bufaddr = cpu_to_fec32(dmabuf);
819 bdp->cbd_datlen = cpu_to_fec16(hdr_len);
820
821 if (fep->bufdesc_ex) {
822 if (fep->quirks & FEC_QUIRK_HAS_AVB)
823 estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
824 if (skb->ip_summed == CHECKSUM_PARTIAL)
825 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
826 ebdp->cbd_bdu = 0;
827 ebdp->cbd_esc = cpu_to_fec32(estatus);
828 }
829
830 bdp->cbd_sc = cpu_to_fec16(status);
831
832 return 0;
833 }
834
fec_enet_txq_submit_tso(struct fec_enet_priv_tx_q * txq,struct sk_buff * skb,struct net_device * ndev)835 static int fec_enet_txq_submit_tso(struct fec_enet_priv_tx_q *txq,
836 struct sk_buff *skb,
837 struct net_device *ndev)
838 {
839 struct fec_enet_private *fep = netdev_priv(ndev);
840 int hdr_len, total_len, data_left;
841 struct bufdesc *bdp = txq->bd.cur;
842 struct bufdesc *tmp_bdp;
843 struct bufdesc_ex *ebdp;
844 struct tso_t tso;
845 unsigned int index = 0;
846 int ret;
847
848 if (tso_count_descs(skb) >= fec_enet_get_free_txdesc_num(txq)) {
849 dev_kfree_skb_any(skb);
850 if (net_ratelimit())
851 netdev_err(ndev, "NOT enough BD for TSO!\n");
852 return NETDEV_TX_OK;
853 }
854
855 /* Protocol checksum off-load for TCP and UDP. */
856 if (fec_enet_clear_csum(skb, ndev)) {
857 dev_kfree_skb_any(skb);
858 return NETDEV_TX_OK;
859 }
860
861 /* Initialize the TSO handler, and prepare the first payload */
862 hdr_len = tso_start(skb, &tso);
863
864 total_len = skb->len - hdr_len;
865 while (total_len > 0) {
866 char *hdr;
867
868 index = fec_enet_get_bd_index(bdp, &txq->bd);
869 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
870 total_len -= data_left;
871
872 /* prepare packet headers: MAC + IP + TCP */
873 hdr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
874 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
875 ret = fec_enet_txq_put_hdr_tso(txq, skb, ndev, bdp, index);
876 if (ret)
877 goto err_release;
878
879 while (data_left > 0) {
880 int size;
881
882 size = min_t(int, tso.size, data_left);
883 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
884 index = fec_enet_get_bd_index(bdp, &txq->bd);
885 ret = fec_enet_txq_put_data_tso(txq, skb, ndev,
886 bdp, index,
887 tso.data, size,
888 size == data_left,
889 total_len == 0);
890 if (ret)
891 goto err_release;
892
893 data_left -= size;
894 tso_build_data(skb, &tso, size);
895 }
896
897 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
898 }
899
900 /* Save skb pointer */
901 txq->tx_buf[index].buf_p = skb;
902
903 skb_tx_timestamp(skb);
904 txq->bd.cur = bdp;
905
906 /* Trigger transmission start */
907 fec_txq_trigger_xmit(fep, txq);
908
909 return 0;
910
911 err_release:
912 /* Release all used data descriptors for TSO */
913 tmp_bdp = txq->bd.cur;
914
915 while (tmp_bdp != bdp) {
916 /* Unmap data buffers */
917 if (tmp_bdp->cbd_bufaddr &&
918 !IS_TSO_HEADER(txq, fec32_to_cpu(tmp_bdp->cbd_bufaddr)))
919 dma_unmap_single(&fep->pdev->dev,
920 fec32_to_cpu(tmp_bdp->cbd_bufaddr),
921 fec16_to_cpu(tmp_bdp->cbd_datlen),
922 DMA_TO_DEVICE);
923
924 /* Clear standard buffer descriptor fields */
925 tmp_bdp->cbd_sc = 0;
926 tmp_bdp->cbd_datlen = 0;
927 tmp_bdp->cbd_bufaddr = 0;
928
929 /* Handle extended descriptor if enabled */
930 if (fep->bufdesc_ex) {
931 ebdp = (struct bufdesc_ex *)tmp_bdp;
932 ebdp->cbd_esc = 0;
933 }
934
935 tmp_bdp = fec_enet_get_nextdesc(tmp_bdp, &txq->bd);
936 }
937
938 dev_kfree_skb_any(skb);
939
940 return ret;
941 }
942
943 static netdev_tx_t
fec_enet_start_xmit(struct sk_buff * skb,struct net_device * ndev)944 fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
945 {
946 struct fec_enet_private *fep = netdev_priv(ndev);
947 int entries_free;
948 unsigned short queue;
949 struct fec_enet_priv_tx_q *txq;
950 struct netdev_queue *nq;
951 int ret;
952
953 queue = skb_get_queue_mapping(skb);
954 txq = fep->tx_queue[queue];
955 nq = netdev_get_tx_queue(ndev, queue);
956
957 if (skb_is_gso(skb))
958 ret = fec_enet_txq_submit_tso(txq, skb, ndev);
959 else
960 ret = fec_enet_txq_submit_skb(txq, skb, ndev);
961 if (ret)
962 return ret;
963
964 entries_free = fec_enet_get_free_txdesc_num(txq);
965 if (entries_free <= txq->tx_stop_threshold)
966 netif_tx_stop_queue(nq);
967
968 return NETDEV_TX_OK;
969 }
970
971 /* Init RX & TX buffer descriptors
972 */
fec_enet_bd_init(struct net_device * dev)973 static void fec_enet_bd_init(struct net_device *dev)
974 {
975 struct fec_enet_private *fep = netdev_priv(dev);
976 struct fec_enet_priv_tx_q *txq;
977 struct fec_enet_priv_rx_q *rxq;
978 struct bufdesc *bdp;
979 unsigned int i;
980 unsigned int q;
981
982 for (q = 0; q < fep->num_rx_queues; q++) {
983 /* Initialize the receive buffer descriptors. */
984 rxq = fep->rx_queue[q];
985 bdp = rxq->bd.base;
986
987 for (i = 0; i < rxq->bd.ring_size; i++) {
988
989 /* Initialize the BD for every fragment in the page. */
990 if (bdp->cbd_bufaddr)
991 bdp->cbd_sc = cpu_to_fec16(BD_ENET_RX_EMPTY);
992 else
993 bdp->cbd_sc = cpu_to_fec16(0);
994
995 if (fep->bufdesc_ex) {
996 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
997
998 ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT);
999 }
1000
1001 bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
1002 }
1003
1004 /* Set the last buffer to wrap */
1005 bdp = fec_enet_get_prevdesc(bdp, &rxq->bd);
1006 bdp->cbd_sc |= cpu_to_fec16(BD_ENET_RX_WRAP);
1007
1008 rxq->bd.cur = rxq->bd.base;
1009 }
1010
1011 for (q = 0; q < fep->num_tx_queues; q++) {
1012 /* ...and the same for transmit */
1013 txq = fep->tx_queue[q];
1014 bdp = txq->bd.base;
1015 txq->bd.cur = bdp;
1016
1017 for (i = 0; i < txq->bd.ring_size; i++) {
1018 struct page *page;
1019
1020 /* Initialize the BD for every fragment in the page. */
1021 bdp->cbd_sc = cpu_to_fec16(0);
1022
1023 switch (txq->tx_buf[i].type) {
1024 case FEC_TXBUF_T_SKB:
1025 if (bdp->cbd_bufaddr &&
1026 !IS_TSO_HEADER(txq, fec32_to_cpu(bdp->cbd_bufaddr)))
1027 dma_unmap_single(&fep->pdev->dev,
1028 fec32_to_cpu(bdp->cbd_bufaddr),
1029 fec16_to_cpu(bdp->cbd_datlen),
1030 DMA_TO_DEVICE);
1031 dev_kfree_skb_any(txq->tx_buf[i].buf_p);
1032 break;
1033 case FEC_TXBUF_T_XDP_NDO:
1034 dma_unmap_single(&fep->pdev->dev,
1035 fec32_to_cpu(bdp->cbd_bufaddr),
1036 fec16_to_cpu(bdp->cbd_datlen),
1037 DMA_TO_DEVICE);
1038 xdp_return_frame(txq->tx_buf[i].buf_p);
1039 break;
1040 case FEC_TXBUF_T_XDP_TX:
1041 page = txq->tx_buf[i].buf_p;
1042 page_pool_put_page(pp_page_to_nmdesc(page)->pp,
1043 page, 0, false);
1044 break;
1045 case FEC_TXBUF_T_XSK_TX:
1046 xsk_buff_free(txq->tx_buf[i].buf_p);
1047 break;
1048 default:
1049 break;
1050 }
1051
1052 txq->tx_buf[i].buf_p = NULL;
1053 /* restore default tx buffer type: FEC_TXBUF_T_SKB */
1054 txq->tx_buf[i].type = FEC_TXBUF_T_SKB;
1055 bdp->cbd_bufaddr = cpu_to_fec32(0);
1056 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
1057 }
1058
1059 /* Set the last buffer to wrap */
1060 bdp = fec_enet_get_prevdesc(bdp, &txq->bd);
1061 bdp->cbd_sc |= cpu_to_fec16(BD_ENET_TX_WRAP);
1062 txq->dirty_tx = bdp;
1063 }
1064 }
1065
fec_enet_active_rxring(struct net_device * ndev)1066 static void fec_enet_active_rxring(struct net_device *ndev)
1067 {
1068 struct fec_enet_private *fep = netdev_priv(ndev);
1069 int i;
1070
1071 for (i = 0; i < fep->num_rx_queues; i++)
1072 writel(0, fep->rx_queue[i]->bd.reg_desc_active);
1073 }
1074
fec_enet_enable_ring(struct net_device * ndev)1075 static void fec_enet_enable_ring(struct net_device *ndev)
1076 {
1077 struct fec_enet_private *fep = netdev_priv(ndev);
1078 struct fec_enet_priv_tx_q *txq;
1079 struct fec_enet_priv_rx_q *rxq;
1080 int i;
1081
1082 for (i = 0; i < fep->num_rx_queues; i++) {
1083 rxq = fep->rx_queue[i];
1084 writel(rxq->bd.dma, fep->hwp + FEC_R_DES_START(i));
1085 writel(fep->max_buf_size, fep->hwp + FEC_R_BUFF_SIZE(i));
1086
1087 /* enable DMA1/2 */
1088 if (i)
1089 writel(RCMR_MATCHEN | RCMR_CMP(i),
1090 fep->hwp + FEC_RCMR(i));
1091 }
1092
1093 for (i = 0; i < fep->num_tx_queues; i++) {
1094 txq = fep->tx_queue[i];
1095 writel(txq->bd.dma, fep->hwp + FEC_X_DES_START(i));
1096
1097 /* enable DMA1/2 */
1098 if (i)
1099 writel(DMA_CLASS_EN | IDLE_SLOPE(i),
1100 fep->hwp + FEC_DMA_CFG(i));
1101 }
1102 }
1103
1104 /* Whack a reset. We should wait for this.
1105 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
1106 * instead of reset MAC itself.
1107 */
fec_ctrl_reset(struct fec_enet_private * fep,bool allow_wol)1108 static void fec_ctrl_reset(struct fec_enet_private *fep, bool allow_wol)
1109 {
1110 u32 val;
1111
1112 if (!allow_wol || !(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) {
1113 if (fep->quirks & FEC_QUIRK_HAS_MULTI_QUEUES ||
1114 ((fep->quirks & FEC_QUIRK_NO_HARD_RESET) && fep->link)) {
1115 writel(0, fep->hwp + FEC_ECNTRL);
1116 } else {
1117 writel(FEC_ECR_RESET, fep->hwp + FEC_ECNTRL);
1118 udelay(10);
1119 }
1120 } else {
1121 val = readl(fep->hwp + FEC_ECNTRL);
1122 val |= (FEC_ECR_MAGICEN | FEC_ECR_SLEEP);
1123 writel(val, fep->hwp + FEC_ECNTRL);
1124 }
1125 }
1126
fec_set_hw_mac_addr(struct net_device * ndev)1127 static void fec_set_hw_mac_addr(struct net_device *ndev)
1128 {
1129 struct fec_enet_private *fep = netdev_priv(ndev);
1130
1131 writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
1132 (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
1133 fep->hwp + FEC_ADDR_LOW);
1134 writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
1135 fep->hwp + FEC_ADDR_HIGH);
1136 }
1137
1138 /*
1139 * This function is called to start or restart the FEC during a link
1140 * change, transmit timeout, or to reconfigure the FEC. The network
1141 * packet processing for this device must be stopped before this call.
1142 */
1143 static void
fec_restart(struct net_device * ndev)1144 fec_restart(struct net_device *ndev)
1145 {
1146 struct fec_enet_private *fep = netdev_priv(ndev);
1147 u32 ecntl = FEC_ECR_ETHEREN;
1148 u32 rcntl = FEC_RCR_MII;
1149
1150 if (OPT_ARCH_HAS_MAX_FL)
1151 rcntl |= (fep->netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN) << 16;
1152
1153 if (fep->bufdesc_ex)
1154 fec_ptp_save_state(fep);
1155
1156 fec_ctrl_reset(fep, false);
1157
1158 /*
1159 * enet-mac reset will reset mac address registers too,
1160 * so need to reconfigure it.
1161 */
1162 fec_set_hw_mac_addr(ndev);
1163
1164 /* Clear any outstanding interrupt, except MDIO. */
1165 writel((0xffffffff & ~FEC_ENET_MII), fep->hwp + FEC_IEVENT);
1166
1167 fec_enet_bd_init(ndev);
1168
1169 fec_enet_enable_ring(ndev);
1170
1171 /* Enable MII mode */
1172 if (fep->full_duplex == DUPLEX_FULL) {
1173 /* FD enable */
1174 writel(0x04, fep->hwp + FEC_X_CNTRL);
1175 } else {
1176 /* No Rcv on Xmit */
1177 rcntl |= FEC_RCR_DRT;
1178 writel(0x0, fep->hwp + FEC_X_CNTRL);
1179 }
1180
1181 /* Set MII speed */
1182 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1183
1184 #if !defined(CONFIG_M5272)
1185 if (fep->quirks & FEC_QUIRK_HAS_RACC) {
1186 u32 val = readl(fep->hwp + FEC_RACC);
1187
1188 /* align IP header */
1189 val |= FEC_RACC_SHIFT16;
1190 if (fep->csum_flags & FLAG_RX_CSUM_ENABLED)
1191 /* set RX checksum */
1192 val |= FEC_RACC_OPTIONS;
1193 else
1194 val &= ~FEC_RACC_OPTIONS;
1195 writel(val, fep->hwp + FEC_RACC);
1196 writel(min(fep->rx_frame_size, fep->max_buf_size), fep->hwp + FEC_FTRL);
1197 }
1198 #endif
1199
1200 /*
1201 * The phy interface and speed need to get configured
1202 * differently on enet-mac.
1203 */
1204 if (fep->quirks & FEC_QUIRK_ENET_MAC) {
1205 /* Enable flow control and length check */
1206 rcntl |= FEC_RCR_NLC | FEC_RCR_FLOWCTL;
1207
1208 /* RGMII, RMII or MII */
1209 if (phy_interface_mode_is_rgmii(fep->phy_interface))
1210 rcntl |= FEC_RCR_RGMII;
1211 else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
1212 rcntl |= FEC_RCR_RMII;
1213 else
1214 rcntl &= ~FEC_RCR_RMII;
1215
1216 /* 1G, 100M or 10M */
1217 if (ndev->phydev) {
1218 if (ndev->phydev->speed == SPEED_1000)
1219 ecntl |= FEC_ECR_SPEED;
1220 else if (ndev->phydev->speed == SPEED_100)
1221 rcntl &= ~FEC_RCR_10BASET;
1222 else
1223 rcntl |= FEC_RCR_10BASET;
1224 }
1225 } else {
1226 #ifdef FEC_MIIGSK_ENR
1227 if (fep->quirks & FEC_QUIRK_USE_GASKET) {
1228 u32 cfgr;
1229 /* disable the gasket and wait */
1230 writel(0, fep->hwp + FEC_MIIGSK_ENR);
1231 while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
1232 udelay(1);
1233
1234 /*
1235 * configure the gasket:
1236 * RMII, 50 MHz, no loopback, no echo
1237 * MII, 25 MHz, no loopback, no echo
1238 */
1239 cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
1240 ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
1241 if (ndev->phydev && ndev->phydev->speed == SPEED_10)
1242 cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
1243 writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
1244
1245 /* re-enable the gasket */
1246 writel(2, fep->hwp + FEC_MIIGSK_ENR);
1247 }
1248 #endif
1249 }
1250
1251 #if !defined(CONFIG_M5272)
1252 /* enable pause frame*/
1253 if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
1254 ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
1255 ndev->phydev && ndev->phydev->pause)) {
1256 rcntl |= FEC_RCR_FLOWCTL;
1257
1258 /* set FIFO threshold parameter to reduce overrun */
1259 writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
1260 writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
1261 writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
1262 writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
1263
1264 /* OPD */
1265 writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
1266 } else {
1267 rcntl &= ~FEC_RCR_FLOWCTL;
1268 }
1269 #endif /* !defined(CONFIG_M5272) */
1270
1271 writel(rcntl, fep->hwp + FEC_R_CNTRL);
1272
1273 /* Setup multicast filter. */
1274 set_multicast_list(ndev);
1275 #ifndef CONFIG_M5272
1276 writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
1277 writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
1278 #endif
1279
1280 if (fep->quirks & FEC_QUIRK_ENET_MAC) {
1281 /* enable ENET endian swap */
1282 ecntl |= FEC_ECR_BYTESWP;
1283
1284 /* When Jumbo Frame is enabled, the FIFO may not be large enough
1285 * to hold an entire frame. In such cases, if the MTU exceeds
1286 * (PKT_MAXBUF_SIZE - VLAN_ETH_HLEN - ETH_FCS_LEN), configure
1287 * the interface to operate in cut-through mode, triggered by
1288 * the FIFO threshold.
1289 * Otherwise, enable the ENET store-and-forward mode.
1290 */
1291 if ((fep->quirks & FEC_QUIRK_JUMBO_FRAME) &&
1292 (ndev->mtu > (PKT_MAXBUF_SIZE - VLAN_ETH_HLEN - ETH_FCS_LEN)))
1293 writel(0xF, fep->hwp + FEC_X_WMRK);
1294 else
1295 writel(FEC_TXWMRK_STRFWD, fep->hwp + FEC_X_WMRK);
1296 }
1297
1298 if (fep->bufdesc_ex)
1299 ecntl |= FEC_ECR_EN1588;
1300
1301 if (fep->quirks & FEC_QUIRK_DELAYED_CLKS_SUPPORT &&
1302 fep->rgmii_txc_dly)
1303 ecntl |= FEC_ENET_TXC_DLY;
1304 if (fep->quirks & FEC_QUIRK_DELAYED_CLKS_SUPPORT &&
1305 fep->rgmii_rxc_dly)
1306 ecntl |= FEC_ENET_RXC_DLY;
1307
1308 #ifndef CONFIG_M5272
1309 /* Enable the MIB statistic event counters */
1310 writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT);
1311 #endif
1312
1313 /* And last, enable the transmit and receive processing */
1314 writel(ecntl, fep->hwp + FEC_ECNTRL);
1315 fec_enet_active_rxring(ndev);
1316
1317 if (fep->bufdesc_ex) {
1318 fec_ptp_start_cyclecounter(ndev);
1319 fec_ptp_restore_state(fep);
1320 }
1321
1322 /* Enable interrupts we wish to service */
1323 if (fep->link)
1324 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1325 else
1326 writel(0, fep->hwp + FEC_IMASK);
1327
1328 /* Init the interrupt coalescing */
1329 if (fep->quirks & FEC_QUIRK_HAS_COALESCE)
1330 fec_enet_itr_coal_set(ndev);
1331 }
1332
fec_enet_ipc_handle_init(struct fec_enet_private * fep)1333 static int fec_enet_ipc_handle_init(struct fec_enet_private *fep)
1334 {
1335 if (!(of_machine_is_compatible("fsl,imx8qm") ||
1336 of_machine_is_compatible("fsl,imx8qp") ||
1337 of_machine_is_compatible("fsl,imx8qxp") ||
1338 of_machine_is_compatible("fsl,imx8dx") ||
1339 of_machine_is_compatible("fsl,imx8dxl")))
1340 return 0;
1341
1342 return imx_scu_get_handle(&fep->ipc_handle);
1343 }
1344
fec_enet_ipg_stop_set(struct fec_enet_private * fep,bool enabled)1345 static void fec_enet_ipg_stop_set(struct fec_enet_private *fep, bool enabled)
1346 {
1347 struct device_node *np = fep->pdev->dev.of_node;
1348 u32 rsrc_id, val;
1349 int idx;
1350
1351 if (!np || !fep->ipc_handle)
1352 return;
1353
1354 idx = of_alias_get_id(np, "ethernet");
1355 if (idx < 0)
1356 idx = 0;
1357 rsrc_id = idx ? IMX_SC_R_ENET_1 : IMX_SC_R_ENET_0;
1358
1359 val = enabled ? 1 : 0;
1360 imx_sc_misc_set_control(fep->ipc_handle, rsrc_id, IMX_SC_C_IPG_STOP, val);
1361 }
1362
fec_enet_stop_mode(struct fec_enet_private * fep,bool enabled)1363 static void fec_enet_stop_mode(struct fec_enet_private *fep, bool enabled)
1364 {
1365 struct fec_platform_data *pdata = fep->pdev->dev.platform_data;
1366 struct fec_stop_mode_gpr *stop_gpr = &fep->stop_gpr;
1367
1368 if (stop_gpr->gpr) {
1369 if (enabled)
1370 regmap_update_bits(stop_gpr->gpr, stop_gpr->reg,
1371 BIT(stop_gpr->bit),
1372 BIT(stop_gpr->bit));
1373 else
1374 regmap_update_bits(stop_gpr->gpr, stop_gpr->reg,
1375 BIT(stop_gpr->bit), 0);
1376 } else if (pdata && pdata->sleep_mode_enable) {
1377 pdata->sleep_mode_enable(enabled);
1378 } else {
1379 fec_enet_ipg_stop_set(fep, enabled);
1380 }
1381 }
1382
fec_irqs_disable(struct net_device * ndev)1383 static void fec_irqs_disable(struct net_device *ndev)
1384 {
1385 struct fec_enet_private *fep = netdev_priv(ndev);
1386
1387 writel(0, fep->hwp + FEC_IMASK);
1388 }
1389
fec_irqs_disable_except_wakeup(struct net_device * ndev)1390 static void fec_irqs_disable_except_wakeup(struct net_device *ndev)
1391 {
1392 struct fec_enet_private *fep = netdev_priv(ndev);
1393
1394 writel(0, fep->hwp + FEC_IMASK);
1395 writel(FEC_ENET_WAKEUP, fep->hwp + FEC_IMASK);
1396 }
1397
1398 static void
fec_stop(struct net_device * ndev)1399 fec_stop(struct net_device *ndev)
1400 {
1401 struct fec_enet_private *fep = netdev_priv(ndev);
1402 u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & FEC_RCR_RMII;
1403 u32 val;
1404
1405 /* We cannot expect a graceful transmit stop without link !!! */
1406 if (fep->link) {
1407 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
1408 udelay(10);
1409 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
1410 netdev_err(ndev, "Graceful transmit stop did not complete!\n");
1411 }
1412
1413 if (fep->bufdesc_ex)
1414 fec_ptp_save_state(fep);
1415
1416 fec_ctrl_reset(fep, true);
1417 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
1418 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
1419
1420 /* We have to keep ENET enabled to have MII interrupt stay working */
1421 if (fep->quirks & FEC_QUIRK_ENET_MAC &&
1422 !(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) {
1423 writel(FEC_ECR_ETHEREN, fep->hwp + FEC_ECNTRL);
1424 writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
1425 }
1426
1427 if (fep->bufdesc_ex) {
1428 val = readl(fep->hwp + FEC_ECNTRL);
1429 val |= FEC_ECR_EN1588;
1430 writel(val, fep->hwp + FEC_ECNTRL);
1431
1432 fec_ptp_start_cyclecounter(ndev);
1433 fec_ptp_restore_state(fep);
1434 }
1435 }
1436
1437 static void
fec_timeout(struct net_device * ndev,unsigned int txqueue)1438 fec_timeout(struct net_device *ndev, unsigned int txqueue)
1439 {
1440 struct fec_enet_private *fep = netdev_priv(ndev);
1441
1442 fec_dump(ndev);
1443
1444 ndev->stats.tx_errors++;
1445
1446 schedule_work(&fep->tx_timeout_work);
1447 }
1448
fec_enet_timeout_work(struct work_struct * work)1449 static void fec_enet_timeout_work(struct work_struct *work)
1450 {
1451 struct fec_enet_private *fep =
1452 container_of(work, struct fec_enet_private, tx_timeout_work);
1453 struct net_device *ndev = fep->netdev;
1454
1455 rtnl_lock();
1456 if (netif_device_present(ndev) || netif_running(ndev)) {
1457 napi_disable(&fep->napi);
1458 netif_tx_lock_bh(ndev);
1459 fec_restart(ndev);
1460 netif_tx_wake_all_queues(ndev);
1461 netif_tx_unlock_bh(ndev);
1462 napi_enable(&fep->napi);
1463 }
1464 rtnl_unlock();
1465 }
1466
1467 static void
fec_enet_hwtstamp(struct fec_enet_private * fep,unsigned ts,struct skb_shared_hwtstamps * hwtstamps)1468 fec_enet_hwtstamp(struct fec_enet_private *fep, unsigned ts,
1469 struct skb_shared_hwtstamps *hwtstamps)
1470 {
1471 unsigned long flags;
1472 u64 ns;
1473
1474 spin_lock_irqsave(&fep->tmreg_lock, flags);
1475 ns = timecounter_cyc2time(&fep->tc, ts);
1476 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
1477
1478 memset(hwtstamps, 0, sizeof(*hwtstamps));
1479 hwtstamps->hwtstamp = ns_to_ktime(ns);
1480 }
1481
fec_enet_xsk_xmit(struct fec_enet_private * fep,struct xsk_buff_pool * pool,u32 queue)1482 static bool fec_enet_xsk_xmit(struct fec_enet_private *fep,
1483 struct xsk_buff_pool *pool,
1484 u32 queue)
1485 {
1486 struct fec_enet_priv_tx_q *txq = fep->tx_queue[queue];
1487 struct xdp_desc *xsk_desc = pool->tx_descs;
1488 int cpu = smp_processor_id();
1489 int free_bds, budget, batch;
1490 struct netdev_queue *nq;
1491 struct bufdesc *bdp;
1492 dma_addr_t dma;
1493 u32 estatus;
1494 u16 status;
1495 int i, j;
1496
1497 nq = netdev_get_tx_queue(fep->netdev, queue);
1498 __netif_tx_lock(nq, cpu);
1499
1500 txq_trans_cond_update(nq);
1501 free_bds = fec_enet_get_free_txdesc_num(txq);
1502 if (!free_bds)
1503 goto tx_unlock;
1504
1505 budget = min(free_bds, FEC_XSK_TX_BUDGET_MAX);
1506 batch = xsk_tx_peek_release_desc_batch(pool, budget);
1507 if (!batch)
1508 goto tx_unlock;
1509
1510 bdp = txq->bd.cur;
1511 for (i = 0; i < batch; i++) {
1512 dma = xsk_buff_raw_get_dma(pool, xsk_desc[i].addr);
1513 xsk_buff_raw_dma_sync_for_device(pool, dma, xsk_desc[i].len);
1514
1515 j = fec_enet_get_bd_index(bdp, &txq->bd);
1516 txq->tx_buf[j].type = FEC_TXBUF_T_XSK_XMIT;
1517 txq->tx_buf[j].buf_p = NULL;
1518
1519 status = fec16_to_cpu(bdp->cbd_sc);
1520 status &= ~BD_ENET_TX_STATS;
1521 status |= BD_ENET_TX_INTR | BD_ENET_TX_LAST;
1522 bdp->cbd_datlen = cpu_to_fec16(xsk_desc[i].len);
1523 bdp->cbd_bufaddr = cpu_to_fec32(dma);
1524
1525 if (fep->bufdesc_ex) {
1526 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1527
1528 estatus = BD_ENET_TX_INT;
1529 if (fep->quirks & FEC_QUIRK_HAS_AVB)
1530 estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
1531
1532 ebdp->cbd_bdu = 0;
1533 ebdp->cbd_esc = cpu_to_fec32(estatus);
1534 }
1535
1536 /* Make sure the updates to rest of the descriptor are performed
1537 * before transferring ownership.
1538 */
1539 dma_wmb();
1540
1541 /* Send it on its way. Tell FEC it's ready, interrupt when done,
1542 * it's the last BD of the frame, and to put the CRC on the end.
1543 */
1544 status |= BD_ENET_TX_READY | BD_ENET_TX_TC;
1545 bdp->cbd_sc = cpu_to_fec16(status);
1546 dma_wmb();
1547
1548 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
1549 txq->bd.cur = bdp;
1550 }
1551
1552 /* Trigger transmission start */
1553 fec_txq_trigger_xmit(fep, txq);
1554
1555 __netif_tx_unlock(nq);
1556
1557 return batch < budget;
1558
1559 tx_unlock:
1560 __netif_tx_unlock(nq);
1561
1562 return true;
1563 }
1564
fec_enet_tx_queue(struct fec_enet_private * fep,u16 queue,int budget)1565 static int fec_enet_tx_queue(struct fec_enet_private *fep,
1566 u16 queue, int budget)
1567 {
1568 struct netdev_queue *nq = netdev_get_tx_queue(fep->netdev, queue);
1569 struct fec_enet_priv_tx_q *txq = fep->tx_queue[queue];
1570 struct net_device *ndev = fep->netdev;
1571 struct bufdesc *bdp = txq->dirty_tx;
1572 int index, frame_len, entries_free;
1573 struct fec_tx_buffer *tx_buf;
1574 unsigned short status;
1575 struct sk_buff *skb;
1576 struct page *page;
1577 int xsk_cnt = 0;
1578
1579 /* get next bdp of dirty_tx */
1580 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
1581
1582 while (bdp != READ_ONCE(txq->bd.cur)) {
1583 /* Order the load of bd.cur and cbd_sc */
1584 rmb();
1585 status = fec16_to_cpu(READ_ONCE(bdp->cbd_sc));
1586 if (status & BD_ENET_TX_READY)
1587 break;
1588
1589 index = fec_enet_get_bd_index(bdp, &txq->bd);
1590 tx_buf = &txq->tx_buf[index];
1591 frame_len = fec16_to_cpu(bdp->cbd_datlen);
1592
1593 switch (tx_buf->type) {
1594 case FEC_TXBUF_T_SKB:
1595 if (bdp->cbd_bufaddr &&
1596 !IS_TSO_HEADER(txq, fec32_to_cpu(bdp->cbd_bufaddr)))
1597 dma_unmap_single(&fep->pdev->dev,
1598 fec32_to_cpu(bdp->cbd_bufaddr),
1599 frame_len, DMA_TO_DEVICE);
1600
1601 bdp->cbd_bufaddr = cpu_to_fec32(0);
1602 skb = tx_buf->buf_p;
1603 if (!skb)
1604 goto tx_buf_done;
1605
1606 frame_len = skb->len;
1607
1608 /* NOTE: SKBTX_IN_PROGRESS being set does not imply it's we who
1609 * are to time stamp the packet, so we still need to check time
1610 * stamping enabled flag.
1611 */
1612 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS &&
1613 fep->hwts_tx_en) && fep->bufdesc_ex) {
1614 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1615 struct skb_shared_hwtstamps shhwtstamps;
1616
1617 fec_enet_hwtstamp(fep, fec32_to_cpu(ebdp->ts), &shhwtstamps);
1618 skb_tstamp_tx(skb, &shhwtstamps);
1619 }
1620
1621 /* Free the sk buffer associated with this last transmit */
1622 napi_consume_skb(skb, budget);
1623 break;
1624 case FEC_TXBUF_T_XDP_NDO:
1625 /* Tx processing cannot call any XDP (or page pool) APIs if
1626 * the "budget" is 0. Because NAPI is called with budget of
1627 * 0 (such as netpoll) indicates we may be in an IRQ context,
1628 * however, we can't use the page pool from IRQ context.
1629 */
1630 if (unlikely(!budget))
1631 goto out;
1632
1633 dma_unmap_single(&fep->pdev->dev,
1634 fec32_to_cpu(bdp->cbd_bufaddr),
1635 frame_len, DMA_TO_DEVICE);
1636 bdp->cbd_bufaddr = cpu_to_fec32(0);
1637 xdp_return_frame_rx_napi(tx_buf->buf_p);
1638 break;
1639 case FEC_TXBUF_T_XDP_TX:
1640 if (unlikely(!budget))
1641 goto out;
1642
1643 bdp->cbd_bufaddr = cpu_to_fec32(0);
1644 page = tx_buf->buf_p;
1645 /* The dma_sync_size = 0 as XDP_TX has already synced
1646 * DMA for_device
1647 */
1648 page_pool_put_page(pp_page_to_nmdesc(page)->pp, page,
1649 0, true);
1650 break;
1651 case FEC_TXBUF_T_XSK_XMIT:
1652 bdp->cbd_bufaddr = cpu_to_fec32(0);
1653 xsk_cnt++;
1654 break;
1655 case FEC_TXBUF_T_XSK_TX:
1656 bdp->cbd_bufaddr = cpu_to_fec32(0);
1657 xsk_buff_free(tx_buf->buf_p);
1658 break;
1659 default:
1660 break;
1661 }
1662
1663 /* Check for errors. */
1664 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
1665 BD_ENET_TX_RL | BD_ENET_TX_UN |
1666 BD_ENET_TX_CSL)) {
1667 ndev->stats.tx_errors++;
1668 if (status & BD_ENET_TX_HB) /* No heartbeat */
1669 ndev->stats.tx_heartbeat_errors++;
1670 if (status & BD_ENET_TX_LC) /* Late collision */
1671 ndev->stats.tx_window_errors++;
1672 if (status & BD_ENET_TX_RL) /* Retrans limit */
1673 ndev->stats.tx_aborted_errors++;
1674 if (status & BD_ENET_TX_UN) /* Underrun */
1675 ndev->stats.tx_fifo_errors++;
1676 if (status & BD_ENET_TX_CSL) /* Carrier lost */
1677 ndev->stats.tx_carrier_errors++;
1678 } else {
1679 ndev->stats.tx_packets++;
1680 ndev->stats.tx_bytes += frame_len;
1681 }
1682
1683 /* Deferred means some collisions occurred during transmit,
1684 * but we eventually sent the packet OK.
1685 */
1686 if (status & BD_ENET_TX_DEF)
1687 ndev->stats.collisions++;
1688
1689 tx_buf->buf_p = NULL;
1690 /* restore default tx buffer type: FEC_TXBUF_T_SKB */
1691 tx_buf->type = FEC_TXBUF_T_SKB;
1692
1693 tx_buf_done:
1694 /* Make sure the update to bdp and tx_buf are performed
1695 * before dirty_tx
1696 */
1697 wmb();
1698 txq->dirty_tx = bdp;
1699
1700 /* Update pointer to next buffer descriptor to be transmitted */
1701 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
1702
1703 /* Since we have freed up a buffer, the ring is no longer full
1704 */
1705 if (netif_tx_queue_stopped(nq)) {
1706 entries_free = fec_enet_get_free_txdesc_num(txq);
1707 if (entries_free >= txq->tx_wake_threshold)
1708 netif_tx_wake_queue(nq);
1709 }
1710 }
1711
1712 out:
1713
1714 /* ERR006358: Keep the transmitter going */
1715 if (bdp != txq->bd.cur &&
1716 readl(txq->bd.reg_desc_active) == 0)
1717 writel(0, txq->bd.reg_desc_active);
1718
1719 if (txq->xsk_pool) {
1720 struct xsk_buff_pool *pool = txq->xsk_pool;
1721
1722 if (xsk_cnt)
1723 xsk_tx_completed(pool, xsk_cnt);
1724
1725 if (xsk_uses_need_wakeup(pool))
1726 xsk_set_tx_need_wakeup(pool);
1727
1728 /* If the condition is true, it indicates that there are still
1729 * packets to be transmitted, so return "budget" to make the
1730 * NAPI continue polling.
1731 */
1732 if (!fec_enet_xsk_xmit(fep, pool, queue))
1733 return budget;
1734 }
1735
1736 return 0;
1737 }
1738
fec_enet_tx(struct net_device * ndev,int budget)1739 static int fec_enet_tx(struct net_device *ndev, int budget)
1740 {
1741 struct fec_enet_private *fep = netdev_priv(ndev);
1742 int i, count = 0;
1743
1744 /* Make sure that AVB queues are processed first. */
1745 for (i = fep->num_tx_queues - 1; i >= 0; i--)
1746 count += fec_enet_tx_queue(fep, i, budget);
1747
1748 return count;
1749 }
1750
fec_enet_update_cbd(struct fec_enet_priv_rx_q * rxq,struct bufdesc * bdp,int index)1751 static int fec_enet_update_cbd(struct fec_enet_priv_rx_q *rxq,
1752 struct bufdesc *bdp, int index)
1753 {
1754 struct page *new_page;
1755 dma_addr_t phys_addr;
1756
1757 new_page = page_pool_dev_alloc_pages(rxq->page_pool);
1758 if (unlikely(!new_page))
1759 return -ENOMEM;
1760
1761 rxq->rx_buf[index].page = new_page;
1762 phys_addr = page_pool_get_dma_addr(new_page) + FEC_ENET_XDP_HEADROOM;
1763 bdp->cbd_bufaddr = cpu_to_fec32(phys_addr);
1764
1765 return 0;
1766 }
1767
fec_enet_update_cbd_zc(struct fec_enet_priv_rx_q * rxq,struct bufdesc * bdp,int index)1768 static int fec_enet_update_cbd_zc(struct fec_enet_priv_rx_q *rxq,
1769 struct bufdesc *bdp, int index)
1770 {
1771 struct xdp_buff *new_xdp;
1772 dma_addr_t phys_addr;
1773
1774 new_xdp = xsk_buff_alloc(rxq->xsk_pool);
1775 if (unlikely(!new_xdp))
1776 return -ENOMEM;
1777
1778 rxq->rx_buf[index].xdp = new_xdp;
1779 phys_addr = xsk_buff_xdp_get_dma(new_xdp);
1780 bdp->cbd_bufaddr = cpu_to_fec32(phys_addr);
1781
1782 return 0;
1783 }
1784
fec_enet_rx_vlan(const struct net_device * ndev,struct sk_buff * skb)1785 static void fec_enet_rx_vlan(const struct net_device *ndev, struct sk_buff *skb)
1786 {
1787 if (ndev->features & NETIF_F_HW_VLAN_CTAG_RX) {
1788 const struct vlan_ethhdr *vlan_header = skb_vlan_eth_hdr(skb);
1789 const u16 vlan_tag = ntohs(vlan_header->h_vlan_TCI);
1790
1791 /* Push and remove the vlan tag */
1792
1793 memmove(skb->data + VLAN_HLEN, skb->data, ETH_ALEN * 2);
1794 skb_pull(skb, VLAN_HLEN);
1795 __vlan_hwaccel_put_tag(skb,
1796 htons(ETH_P_8021Q),
1797 vlan_tag);
1798 }
1799 }
1800
fec_rx_error_check(struct net_device * ndev,u16 status)1801 static int fec_rx_error_check(struct net_device *ndev, u16 status)
1802 {
1803 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
1804 BD_ENET_RX_CR | BD_ENET_RX_OV | BD_ENET_RX_LAST |
1805 BD_ENET_RX_CL)) {
1806 ndev->stats.rx_errors++;
1807
1808 if (status & BD_ENET_RX_OV) {
1809 /* FIFO overrun */
1810 ndev->stats.rx_fifo_errors++;
1811 return -EIO;
1812 }
1813
1814 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH |
1815 BD_ENET_RX_LAST)) {
1816 /* Frame too long or too short. */
1817 ndev->stats.rx_length_errors++;
1818 if ((status & BD_ENET_RX_LAST) && net_ratelimit())
1819 netdev_err(ndev, "rcv is not +last\n");
1820 }
1821
1822 /* CRC Error */
1823 if (status & BD_ENET_RX_CR)
1824 ndev->stats.rx_crc_errors++;
1825
1826 /* Report late collisions as a frame error. */
1827 if (status & (BD_ENET_RX_NO | BD_ENET_RX_CL))
1828 ndev->stats.rx_frame_errors++;
1829
1830 return -EIO;
1831 }
1832
1833 return 0;
1834 }
1835
fec_build_skb(struct fec_enet_private * fep,struct fec_enet_priv_rx_q * rxq,struct bufdesc * bdp,struct page * page,u32 len)1836 static struct sk_buff *fec_build_skb(struct fec_enet_private *fep,
1837 struct fec_enet_priv_rx_q *rxq,
1838 struct bufdesc *bdp,
1839 struct page *page, u32 len)
1840 {
1841 struct net_device *ndev = fep->netdev;
1842 struct bufdesc_ex *ebdp;
1843 struct sk_buff *skb;
1844
1845 skb = build_skb(page_address(page),
1846 PAGE_SIZE << fep->pagepool_order);
1847 if (unlikely(!skb)) {
1848 page_pool_recycle_direct(rxq->page_pool, page);
1849 ndev->stats.rx_dropped++;
1850 if (net_ratelimit())
1851 netdev_err(ndev, "build_skb failed\n");
1852
1853 return NULL;
1854 }
1855
1856 skb_reserve(skb, FEC_ENET_XDP_HEADROOM + fep->rx_shift);
1857 skb_put(skb, len);
1858 skb_mark_for_recycle(skb);
1859
1860 /* Get offloads from the enhanced buffer descriptor */
1861 if (fep->bufdesc_ex) {
1862 ebdp = (struct bufdesc_ex *)bdp;
1863
1864 /* If this is a VLAN packet remove the VLAN Tag */
1865 if (ebdp->cbd_esc & cpu_to_fec32(BD_ENET_RX_VLAN))
1866 fec_enet_rx_vlan(ndev, skb);
1867
1868 /* Get receive timestamp from the skb */
1869 if (fep->hwts_rx_en)
1870 fec_enet_hwtstamp(fep, fec32_to_cpu(ebdp->ts),
1871 skb_hwtstamps(skb));
1872
1873 if (fep->csum_flags & FLAG_RX_CSUM_ENABLED) {
1874 if (!(ebdp->cbd_esc &
1875 cpu_to_fec32(FLAG_RX_CSUM_ERROR)))
1876 /* don't check it */
1877 skb->ip_summed = CHECKSUM_UNNECESSARY;
1878 else
1879 skb_checksum_none_assert(skb);
1880 }
1881 }
1882
1883 skb->protocol = eth_type_trans(skb, ndev);
1884 skb_record_rx_queue(skb, rxq->bd.qid);
1885
1886 return skb;
1887 }
1888
1889 /* During a receive, the bd_rx.cur points to the current incoming buffer.
1890 * When we update through the ring, if the next incoming buffer has
1891 * not been given to the system, we just set the empty indicator,
1892 * effectively tossing the packet.
1893 */
fec_enet_rx_queue(struct fec_enet_private * fep,u16 queue,int budget)1894 static int fec_enet_rx_queue(struct fec_enet_private *fep,
1895 u16 queue, int budget)
1896 {
1897 struct fec_enet_priv_rx_q *rxq = fep->rx_queue[queue];
1898 bool need_swap = fep->quirks & FEC_QUIRK_SWAP_FRAME;
1899 struct net_device *ndev = fep->netdev;
1900 struct bufdesc *bdp = rxq->bd.cur;
1901 u32 sub_len = 4 + fep->rx_shift;
1902 int pkt_received = 0;
1903 u16 status, pkt_len;
1904 struct sk_buff *skb;
1905 struct page *page;
1906 dma_addr_t dma;
1907 int index;
1908
1909 #if defined(CONFIG_COLDFIRE) && !defined(CONFIG_COLDFIRE_COHERENT_DMA)
1910 /*
1911 * Hacky flush of all caches instead of using the DMA API for the TSO
1912 * headers.
1913 */
1914 flush_cache_all();
1915 #endif
1916
1917 /* First, grab all of the stats for the incoming packet.
1918 * These get messed up if we get called due to a busy condition.
1919 */
1920 while (!((status = fec16_to_cpu(bdp->cbd_sc)) & BD_ENET_RX_EMPTY)) {
1921
1922 if (pkt_received >= budget)
1923 break;
1924 pkt_received++;
1925
1926 writel(FEC_ENET_RXF_GET(queue), fep->hwp + FEC_IEVENT);
1927
1928 /* Check for errors. */
1929 status ^= BD_ENET_RX_LAST;
1930 if (unlikely(fec_rx_error_check(ndev, status)))
1931 goto rx_processing_done;
1932
1933 /* Process the incoming frame. */
1934 ndev->stats.rx_packets++;
1935 pkt_len = fec16_to_cpu(bdp->cbd_datlen);
1936 ndev->stats.rx_bytes += pkt_len - fep->rx_shift;
1937
1938 index = fec_enet_get_bd_index(bdp, &rxq->bd);
1939 page = rxq->rx_buf[index].page;
1940 dma = fec32_to_cpu(bdp->cbd_bufaddr);
1941 if (fec_enet_update_cbd(rxq, bdp, index)) {
1942 ndev->stats.rx_dropped++;
1943 goto rx_processing_done;
1944 }
1945
1946 dma_sync_single_for_cpu(&fep->pdev->dev, dma, pkt_len,
1947 DMA_FROM_DEVICE);
1948 prefetch(page_address(page));
1949
1950 if (unlikely(need_swap)) {
1951 u8 *data;
1952
1953 data = page_address(page) + FEC_ENET_XDP_HEADROOM;
1954 swap_buffer(data, pkt_len);
1955 }
1956
1957 /* The packet length includes FCS, but we don't want to
1958 * include that when passing upstream as it messes up
1959 * bridging applications.
1960 */
1961 skb = fec_build_skb(fep, rxq, bdp, page, pkt_len - sub_len);
1962 if (!skb)
1963 goto rx_processing_done;
1964
1965 napi_gro_receive(&fep->napi, skb);
1966
1967 rx_processing_done:
1968 /* Clear the status flags for this buffer */
1969 status &= ~BD_ENET_RX_STATS;
1970
1971 /* Mark the buffer empty */
1972 status |= BD_ENET_RX_EMPTY;
1973
1974 if (fep->bufdesc_ex) {
1975 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
1976
1977 ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT);
1978 ebdp->cbd_prot = 0;
1979 ebdp->cbd_bdu = 0;
1980 }
1981 /* Make sure the updates to rest of the descriptor are
1982 * performed before transferring ownership.
1983 */
1984 wmb();
1985 bdp->cbd_sc = cpu_to_fec16(status);
1986
1987 /* Update BD pointer to next entry */
1988 bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
1989
1990 /* Doing this here will keep the FEC running while we process
1991 * incoming frames. On a heavily loaded network, we should be
1992 * able to keep up at the expense of system resources.
1993 */
1994 writel(0, rxq->bd.reg_desc_active);
1995 }
1996 rxq->bd.cur = bdp;
1997
1998 return pkt_received;
1999 }
2000
fec_xdp_drop(struct fec_enet_priv_rx_q * rxq,struct xdp_buff * xdp,u32 sync)2001 static void fec_xdp_drop(struct fec_enet_priv_rx_q *rxq,
2002 struct xdp_buff *xdp, u32 sync)
2003 {
2004 struct page *page = virt_to_head_page(xdp->data);
2005
2006 page_pool_put_page(rxq->page_pool, page, sync, true);
2007 }
2008
2009 static int
fec_enet_xdp_get_tx_queue(struct fec_enet_private * fep,int index)2010 fec_enet_xdp_get_tx_queue(struct fec_enet_private *fep, int index)
2011 {
2012 if (unlikely(index < 0))
2013 return 0;
2014
2015 return (index % fep->num_tx_queues);
2016 }
2017
fec_enet_rx_queue_xdp(struct fec_enet_private * fep,int queue,int budget,struct bpf_prog * prog)2018 static int fec_enet_rx_queue_xdp(struct fec_enet_private *fep, int queue,
2019 int budget, struct bpf_prog *prog)
2020 {
2021 u32 data_start = FEC_ENET_XDP_HEADROOM + fep->rx_shift;
2022 struct fec_enet_priv_rx_q *rxq = fep->rx_queue[queue];
2023 struct net_device *ndev = fep->netdev;
2024 struct bufdesc *bdp = rxq->bd.cur;
2025 u32 sub_len = 4 + fep->rx_shift;
2026 int cpu = smp_processor_id();
2027 int pkt_received = 0;
2028 struct sk_buff *skb;
2029 u16 status, pkt_len;
2030 struct xdp_buff xdp;
2031 int tx_qid = queue;
2032 struct page *page;
2033 u32 xdp_res = 0;
2034 dma_addr_t dma;
2035 int index, err;
2036 u32 act, sync;
2037
2038 #if defined(CONFIG_COLDFIRE) && !defined(CONFIG_COLDFIRE_COHERENT_DMA)
2039 /*
2040 * Hacky flush of all caches instead of using the DMA API for the TSO
2041 * headers.
2042 */
2043 flush_cache_all();
2044 #endif
2045
2046 if (unlikely(tx_qid >= fep->num_tx_queues))
2047 tx_qid = fec_enet_xdp_get_tx_queue(fep, cpu);
2048
2049 xdp_init_buff(&xdp, PAGE_SIZE << fep->pagepool_order, &rxq->xdp_rxq);
2050
2051 while (!((status = fec16_to_cpu(bdp->cbd_sc)) & BD_ENET_RX_EMPTY)) {
2052 if (pkt_received >= budget)
2053 break;
2054 pkt_received++;
2055
2056 writel(FEC_ENET_RXF_GET(queue), fep->hwp + FEC_IEVENT);
2057
2058 /* Check for errors. */
2059 status ^= BD_ENET_RX_LAST;
2060 if (unlikely(fec_rx_error_check(ndev, status)))
2061 goto rx_processing_done;
2062
2063 /* Process the incoming frame. */
2064 ndev->stats.rx_packets++;
2065 pkt_len = fec16_to_cpu(bdp->cbd_datlen);
2066 ndev->stats.rx_bytes += pkt_len - fep->rx_shift;
2067
2068 index = fec_enet_get_bd_index(bdp, &rxq->bd);
2069 page = rxq->rx_buf[index].page;
2070 dma = fec32_to_cpu(bdp->cbd_bufaddr);
2071
2072 if (fec_enet_update_cbd(rxq, bdp, index)) {
2073 ndev->stats.rx_dropped++;
2074 goto rx_processing_done;
2075 }
2076
2077 dma_sync_single_for_cpu(&fep->pdev->dev, dma, pkt_len,
2078 DMA_FROM_DEVICE);
2079 prefetch(page_address(page));
2080
2081 xdp_buff_clear_frags_flag(&xdp);
2082 /* subtract 16bit shift and FCS */
2083 pkt_len -= sub_len;
2084 xdp_prepare_buff(&xdp, page_address(page), data_start,
2085 pkt_len, false);
2086
2087 act = bpf_prog_run_xdp(prog, &xdp);
2088 /* Due xdp_adjust_tail and xdp_adjust_head: DMA sync
2089 * for_device cover max len CPU touch.
2090 */
2091 sync = xdp.data_end - xdp.data;
2092 sync = max(sync, pkt_len);
2093
2094 switch (act) {
2095 case XDP_PASS:
2096 rxq->stats[RX_XDP_PASS]++;
2097 /* The packet length includes FCS, but we don't want to
2098 * include that when passing upstream as it messes up
2099 * bridging applications.
2100 */
2101 skb = fec_build_skb(fep, rxq, bdp, page, pkt_len);
2102 if (!skb)
2103 trace_xdp_exception(ndev, prog, XDP_PASS);
2104 else
2105 napi_gro_receive(&fep->napi, skb);
2106
2107 break;
2108 case XDP_REDIRECT:
2109 rxq->stats[RX_XDP_REDIRECT]++;
2110 err = xdp_do_redirect(ndev, &xdp, prog);
2111 if (unlikely(err)) {
2112 fec_xdp_drop(rxq, &xdp, sync);
2113 trace_xdp_exception(ndev, prog, XDP_REDIRECT);
2114 } else {
2115 xdp_res |= FEC_ENET_XDP_REDIR;
2116 }
2117 break;
2118 case XDP_TX:
2119 rxq->stats[RX_XDP_TX]++;
2120 err = fec_enet_xdp_tx_xmit(fep, cpu, &xdp, sync, tx_qid);
2121 if (unlikely(err)) {
2122 rxq->stats[RX_XDP_TX_ERRORS]++;
2123 fec_xdp_drop(rxq, &xdp, sync);
2124 trace_xdp_exception(ndev, prog, XDP_TX);
2125 } else {
2126 xdp_res |= FEC_ENET_XDP_TX;
2127 }
2128 break;
2129 default:
2130 bpf_warn_invalid_xdp_action(ndev, prog, act);
2131 fallthrough;
2132 case XDP_ABORTED:
2133 trace_xdp_exception(ndev, prog, act);
2134 /* handle aborts by dropping packet */
2135 fallthrough;
2136 case XDP_DROP:
2137 rxq->stats[RX_XDP_DROP]++;
2138 fec_xdp_drop(rxq, &xdp, sync);
2139 break;
2140 }
2141
2142 rx_processing_done:
2143 /* Clear the status flags for this buffer */
2144 status &= ~BD_ENET_RX_STATS;
2145 /* Mark the buffer empty */
2146 status |= BD_ENET_RX_EMPTY;
2147
2148 if (fep->bufdesc_ex) {
2149 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
2150
2151 ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT);
2152 ebdp->cbd_prot = 0;
2153 ebdp->cbd_bdu = 0;
2154 }
2155
2156 /* Make sure the updates to rest of the descriptor are
2157 * performed before transferring ownership.
2158 */
2159 dma_wmb();
2160 bdp->cbd_sc = cpu_to_fec16(status);
2161
2162 /* Update BD pointer to next entry */
2163 bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
2164
2165 /* Doing this here will keep the FEC running while we process
2166 * incoming frames. On a heavily loaded network, we should be
2167 * able to keep up at the expense of system resources.
2168 */
2169 writel(0, rxq->bd.reg_desc_active);
2170 }
2171
2172 rxq->bd.cur = bdp;
2173
2174 if (xdp_res & FEC_ENET_XDP_REDIR)
2175 xdp_do_flush();
2176
2177 if (xdp_res & FEC_ENET_XDP_TX)
2178 /* Trigger transmission start */
2179 fec_txq_trigger_xmit(fep, fep->tx_queue[tx_qid]);
2180
2181 return pkt_received;
2182 }
2183
fec_build_skb_zc(struct xdp_buff * xsk,struct napi_struct * napi)2184 static struct sk_buff *fec_build_skb_zc(struct xdp_buff *xsk,
2185 struct napi_struct *napi)
2186 {
2187 size_t len = xdp_get_buff_len(xsk);
2188 struct sk_buff *skb;
2189
2190 skb = napi_alloc_skb(napi, len);
2191 if (unlikely(!skb)) {
2192 xsk_buff_free(xsk);
2193 return NULL;
2194 }
2195
2196 skb_put_data(skb, xsk->data, len);
2197 xsk_buff_free(xsk);
2198
2199 return skb;
2200 }
2201
fec_enet_xsk_tx_xmit(struct fec_enet_private * fep,struct xdp_buff * xsk,int cpu,int queue)2202 static int fec_enet_xsk_tx_xmit(struct fec_enet_private *fep,
2203 struct xdp_buff *xsk, int cpu,
2204 int queue)
2205 {
2206 struct netdev_queue *nq = netdev_get_tx_queue(fep->netdev, queue);
2207 struct fec_enet_priv_tx_q *txq = fep->tx_queue[queue];
2208 u32 offset = xsk->data - xsk->data_hard_start;
2209 u32 headroom = txq->xsk_pool->headroom;
2210 u32 len = xsk->data_end - xsk->data;
2211 u32 index, status, estatus;
2212 struct bufdesc *bdp;
2213 dma_addr_t dma;
2214
2215 __netif_tx_lock(nq, cpu);
2216
2217 /* Avoid tx timeout as XDP shares the queue with kernel stack */
2218 txq_trans_cond_update(nq);
2219
2220 if (!fec_enet_get_free_txdesc_num(txq)) {
2221 __netif_tx_unlock(nq);
2222
2223 return -EBUSY;
2224 }
2225
2226 /* Fill in a Tx ring entry */
2227 bdp = txq->bd.cur;
2228 status = fec16_to_cpu(bdp->cbd_sc);
2229 status &= ~BD_ENET_TX_STATS;
2230
2231 index = fec_enet_get_bd_index(bdp, &txq->bd);
2232 dma = xsk_buff_xdp_get_frame_dma(xsk) + headroom + offset;
2233
2234 xsk_buff_raw_dma_sync_for_device(txq->xsk_pool, dma, len);
2235
2236 txq->tx_buf[index].buf_p = xsk;
2237 txq->tx_buf[index].type = FEC_TXBUF_T_XSK_TX;
2238
2239 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
2240 if (fep->bufdesc_ex)
2241 estatus = BD_ENET_TX_INT;
2242
2243 bdp->cbd_bufaddr = cpu_to_fec32(dma);
2244 bdp->cbd_datlen = cpu_to_fec16(len);
2245
2246 if (fep->bufdesc_ex) {
2247 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
2248
2249 if (fep->quirks & FEC_QUIRK_HAS_AVB)
2250 estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
2251
2252 ebdp->cbd_bdu = 0;
2253 ebdp->cbd_esc = cpu_to_fec32(estatus);
2254 }
2255
2256 dma_wmb();
2257 status |= BD_ENET_TX_READY | BD_ENET_TX_TC;
2258 bdp->cbd_sc = cpu_to_fec16(status);
2259 dma_wmb();
2260
2261 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
2262 txq->bd.cur = bdp;
2263
2264 __netif_tx_unlock(nq);
2265
2266 return 0;
2267 }
2268
fec_enet_rx_queue_xsk(struct fec_enet_private * fep,int queue,int budget,struct bpf_prog * prog)2269 static int fec_enet_rx_queue_xsk(struct fec_enet_private *fep, int queue,
2270 int budget, struct bpf_prog *prog)
2271 {
2272 u32 data_start = FEC_ENET_XDP_HEADROOM + fep->rx_shift;
2273 struct fec_enet_priv_rx_q *rxq = fep->rx_queue[queue];
2274 struct net_device *ndev = fep->netdev;
2275 struct bufdesc *bdp = rxq->bd.cur;
2276 u32 sub_len = 4 + fep->rx_shift;
2277 int cpu = smp_processor_id();
2278 bool wakeup_xsk = false;
2279 struct xdp_buff *xsk;
2280 int pkt_received = 0;
2281 struct sk_buff *skb;
2282 u16 status, pkt_len;
2283 u32 xdp_res = 0;
2284 int index, err;
2285 u32 act;
2286
2287 #if defined(CONFIG_COLDFIRE) && !defined(CONFIG_COLDFIRE_COHERENT_DMA)
2288 /*
2289 * Hacky flush of all caches instead of using the DMA API for the TSO
2290 * headers.
2291 */
2292 flush_cache_all();
2293 #endif
2294
2295 while (!((status = fec16_to_cpu(bdp->cbd_sc)) & BD_ENET_RX_EMPTY)) {
2296 if (unlikely(pkt_received >= budget))
2297 break;
2298
2299 writel(FEC_ENET_RXF_GET(queue), fep->hwp + FEC_IEVENT);
2300
2301 index = fec_enet_get_bd_index(bdp, &rxq->bd);
2302 xsk = rxq->rx_buf[index].xdp;
2303 if (unlikely(!xsk)) {
2304 if (fec_enet_update_cbd_zc(rxq, bdp, index))
2305 break;
2306
2307 if (fep->bufdesc_ex) {
2308 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
2309
2310 ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT);
2311 ebdp->cbd_prot = 0;
2312 ebdp->cbd_bdu = 0;
2313 }
2314
2315 dma_wmb();
2316 status &= ~BD_ENET_RX_STATS;
2317 status |= BD_ENET_RX_EMPTY;
2318 bdp->cbd_sc = cpu_to_fec16(status);
2319 break;
2320 }
2321
2322 pkt_received++;
2323 /* Check for errors. */
2324 status ^= BD_ENET_RX_LAST;
2325 if (unlikely(fec_rx_error_check(ndev, status)))
2326 goto rx_processing_done;
2327
2328 /* Process the incoming frame. */
2329 ndev->stats.rx_packets++;
2330 pkt_len = fec16_to_cpu(bdp->cbd_datlen);
2331 ndev->stats.rx_bytes += pkt_len - fep->rx_shift;
2332
2333 if (fec_enet_update_cbd_zc(rxq, bdp, index)) {
2334 ndev->stats.rx_dropped++;
2335 goto rx_processing_done;
2336 }
2337
2338 pkt_len -= sub_len;
2339 xsk->data = xsk->data_hard_start + data_start;
2340 /* Subtract FCS and 16bit shift */
2341 xsk->data_end = xsk->data + pkt_len;
2342 xsk->data_meta = xsk->data;
2343 xsk_buff_dma_sync_for_cpu(xsk);
2344
2345 /* If the XSK pool is enabled before the bpf program is
2346 * installed, or the bpf program is uninstalled before
2347 * the XSK pool is disabled. prog will be NULL and we
2348 * need to set a default XDP_PASS action.
2349 */
2350 if (unlikely(!prog))
2351 act = XDP_PASS;
2352 else
2353 act = bpf_prog_run_xdp(prog, xsk);
2354
2355 switch (act) {
2356 case XDP_PASS:
2357 rxq->stats[RX_XDP_PASS]++;
2358 skb = fec_build_skb_zc(xsk, &fep->napi);
2359 if (unlikely(!skb)) {
2360 ndev->stats.rx_dropped++;
2361 trace_xdp_exception(ndev, prog, XDP_PASS);
2362 } else {
2363 napi_gro_receive(&fep->napi, skb);
2364 }
2365
2366 break;
2367 case XDP_TX:
2368 rxq->stats[RX_XDP_TX]++;
2369 err = fec_enet_xsk_tx_xmit(fep, xsk, cpu, queue);
2370 if (unlikely(err)) {
2371 rxq->stats[RX_XDP_TX_ERRORS]++;
2372 xsk_buff_free(xsk);
2373 trace_xdp_exception(ndev, prog, XDP_TX);
2374 } else {
2375 xdp_res |= FEC_ENET_XDP_TX;
2376 }
2377 break;
2378 case XDP_REDIRECT:
2379 rxq->stats[RX_XDP_REDIRECT]++;
2380 err = xdp_do_redirect(ndev, xsk, prog);
2381 if (unlikely(err)) {
2382 if (err == -ENOBUFS)
2383 wakeup_xsk = true;
2384
2385 rxq->stats[RX_XDP_DROP]++;
2386 xsk_buff_free(xsk);
2387 trace_xdp_exception(ndev, prog, XDP_REDIRECT);
2388 } else {
2389 xdp_res |= FEC_ENET_XDP_REDIR;
2390 }
2391 break;
2392 default:
2393 bpf_warn_invalid_xdp_action(ndev, prog, act);
2394 fallthrough;
2395 case XDP_ABORTED:
2396 trace_xdp_exception(ndev, prog, act);
2397 fallthrough;
2398 case XDP_DROP:
2399 rxq->stats[RX_XDP_DROP]++;
2400 xsk_buff_free(xsk);
2401 break;
2402 }
2403
2404 rx_processing_done:
2405 /* Clear the status flags for this buffer */
2406 status &= ~BD_ENET_RX_STATS;
2407 /* Mark the buffer empty */
2408 status |= BD_ENET_RX_EMPTY;
2409
2410 if (fep->bufdesc_ex) {
2411 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
2412
2413 ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT);
2414 ebdp->cbd_prot = 0;
2415 ebdp->cbd_bdu = 0;
2416 }
2417
2418 /* Make sure the updates to rest of the descriptor are
2419 * performed before transferring ownership.
2420 */
2421 dma_wmb();
2422 bdp->cbd_sc = cpu_to_fec16(status);
2423
2424 /* Update BD pointer to next entry */
2425 bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
2426
2427 /* Doing this here will keep the FEC running while we process
2428 * incoming frames. On a heavily loaded network, we should be
2429 * able to keep up at the expense of system resources.
2430 */
2431 writel(0, rxq->bd.reg_desc_active);
2432 }
2433
2434 rxq->bd.cur = bdp;
2435
2436 if (xdp_res & FEC_ENET_XDP_REDIR)
2437 xdp_do_flush();
2438
2439 if (xdp_res & FEC_ENET_XDP_TX)
2440 fec_txq_trigger_xmit(fep, fep->tx_queue[queue]);
2441
2442 if (rxq->xsk_pool && xsk_uses_need_wakeup(rxq->xsk_pool)) {
2443 if (wakeup_xsk)
2444 xsk_set_rx_need_wakeup(rxq->xsk_pool);
2445 else
2446 xsk_clear_rx_need_wakeup(rxq->xsk_pool);
2447 }
2448
2449 return pkt_received;
2450 }
2451
fec_enet_rx(struct net_device * ndev,int budget)2452 static int fec_enet_rx(struct net_device *ndev, int budget)
2453 {
2454 struct fec_enet_private *fep = netdev_priv(ndev);
2455 struct bpf_prog *prog = READ_ONCE(fep->xdp_prog);
2456 int i, done = 0;
2457
2458 /* Make sure that AVB queues are processed first. */
2459 for (i = fep->num_rx_queues - 1; i >= 0; i--) {
2460 struct fec_enet_priv_rx_q *rxq = fep->rx_queue[i];
2461 int batch = budget - done;
2462
2463 if (rxq->xsk_pool)
2464 done += fec_enet_rx_queue_xsk(fep, i, batch, prog);
2465 else if (prog)
2466 done += fec_enet_rx_queue_xdp(fep, i, batch, prog);
2467 else
2468 done += fec_enet_rx_queue(fep, i, batch);
2469 }
2470
2471 return done;
2472 }
2473
fec_enet_collect_events(struct fec_enet_private * fep)2474 static bool fec_enet_collect_events(struct fec_enet_private *fep)
2475 {
2476 uint int_events;
2477
2478 int_events = readl(fep->hwp + FEC_IEVENT);
2479
2480 /* Don't clear MDIO events, we poll for those */
2481 int_events &= ~FEC_ENET_MII;
2482
2483 writel(int_events, fep->hwp + FEC_IEVENT);
2484
2485 return int_events != 0;
2486 }
2487
2488 static irqreturn_t
fec_enet_interrupt(int irq,void * dev_id)2489 fec_enet_interrupt(int irq, void *dev_id)
2490 {
2491 struct net_device *ndev = dev_id;
2492 struct fec_enet_private *fep = netdev_priv(ndev);
2493 irqreturn_t ret = IRQ_NONE;
2494
2495 if (fec_enet_collect_events(fep) && fep->link) {
2496 ret = IRQ_HANDLED;
2497
2498 if (napi_schedule_prep(&fep->napi)) {
2499 /* Disable interrupts */
2500 writel(0, fep->hwp + FEC_IMASK);
2501 __napi_schedule(&fep->napi);
2502 }
2503 }
2504
2505 return ret;
2506 }
2507
fec_enet_rx_napi(struct napi_struct * napi,int budget)2508 static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
2509 {
2510 struct net_device *ndev = napi->dev;
2511 struct fec_enet_private *fep = netdev_priv(ndev);
2512 int rx_done = 0, tx_done = 0;
2513 int max_done;
2514
2515 do {
2516 rx_done += fec_enet_rx(ndev, budget - rx_done);
2517 tx_done += fec_enet_tx(ndev, budget);
2518 max_done = max(rx_done, tx_done);
2519 } while ((max_done < budget) && fec_enet_collect_events(fep));
2520
2521 if (max_done < budget) {
2522 napi_complete_done(napi, max_done);
2523 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
2524 return max_done;
2525 }
2526
2527 return budget;
2528 }
2529
2530 /* ------------------------------------------------------------------------- */
fec_get_mac(struct net_device * ndev)2531 static int fec_get_mac(struct net_device *ndev)
2532 {
2533 struct fec_enet_private *fep = netdev_priv(ndev);
2534 unsigned char *iap, tmpaddr[ETH_ALEN];
2535 int ret;
2536
2537 /*
2538 * try to get mac address in following order:
2539 *
2540 * 1) module parameter via kernel command line in form
2541 * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
2542 */
2543 iap = macaddr;
2544
2545 /*
2546 * 2) from device tree data
2547 */
2548 if (!is_valid_ether_addr(iap)) {
2549 struct device_node *np = fep->pdev->dev.of_node;
2550 if (np) {
2551 ret = of_get_mac_address(np, tmpaddr);
2552 if (!ret)
2553 iap = tmpaddr;
2554 else if (ret == -EPROBE_DEFER)
2555 return ret;
2556 }
2557 }
2558
2559 /*
2560 * 3) from flash or fuse (via platform data)
2561 */
2562 if (!is_valid_ether_addr(iap)) {
2563 #ifdef CONFIG_M5272
2564 if (FEC_FLASHMAC)
2565 iap = (unsigned char *)FEC_FLASHMAC;
2566 #else
2567 struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev);
2568
2569 if (pdata)
2570 iap = (unsigned char *)&pdata->mac;
2571 #endif
2572 }
2573
2574 /*
2575 * 4) FEC mac registers set by bootloader
2576 */
2577 if (!is_valid_ether_addr(iap)) {
2578 *((__be32 *) &tmpaddr[0]) =
2579 cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW));
2580 *((__be16 *) &tmpaddr[4]) =
2581 cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
2582 iap = &tmpaddr[0];
2583 }
2584
2585 /*
2586 * 5) random mac address
2587 */
2588 if (!is_valid_ether_addr(iap)) {
2589 /* Report it and use a random ethernet address instead */
2590 dev_err(&fep->pdev->dev, "Invalid MAC address: %pM\n", iap);
2591 eth_hw_addr_random(ndev);
2592 dev_info(&fep->pdev->dev, "Using random MAC address: %pM\n",
2593 ndev->dev_addr);
2594 return 0;
2595 }
2596
2597 /* Adjust MAC if using macaddr */
2598 eth_hw_addr_gen(ndev, iap, iap == macaddr ? fep->dev_id : 0);
2599
2600 return 0;
2601 }
2602
2603 /* ------------------------------------------------------------------------- */
2604
2605 /*
2606 * Phy section
2607 */
2608
2609 /* LPI Sleep Ts count base on tx clk (clk_ref).
2610 * The lpi sleep cnt value = X us / (cycle_ns).
2611 */
fec_enet_us_to_tx_cycle(struct net_device * ndev,int us)2612 static int fec_enet_us_to_tx_cycle(struct net_device *ndev, int us)
2613 {
2614 struct fec_enet_private *fep = netdev_priv(ndev);
2615
2616 return us * (fep->clk_ref_rate / 1000) / 1000;
2617 }
2618
fec_enet_eee_mode_set(struct net_device * ndev,u32 lpi_timer,bool enable)2619 static int fec_enet_eee_mode_set(struct net_device *ndev, u32 lpi_timer,
2620 bool enable)
2621 {
2622 struct fec_enet_private *fep = netdev_priv(ndev);
2623 unsigned int sleep_cycle, wake_cycle;
2624
2625 if (enable) {
2626 sleep_cycle = fec_enet_us_to_tx_cycle(ndev, lpi_timer);
2627 wake_cycle = sleep_cycle;
2628 } else {
2629 sleep_cycle = 0;
2630 wake_cycle = 0;
2631 }
2632
2633 writel(sleep_cycle, fep->hwp + FEC_LPI_SLEEP);
2634 writel(wake_cycle, fep->hwp + FEC_LPI_WAKE);
2635
2636 return 0;
2637 }
2638
fec_enet_adjust_link(struct net_device * ndev)2639 static void fec_enet_adjust_link(struct net_device *ndev)
2640 {
2641 struct fec_enet_private *fep = netdev_priv(ndev);
2642 struct phy_device *phy_dev = ndev->phydev;
2643 int status_change = 0;
2644
2645 /*
2646 * If the netdev is down, or is going down, we're not interested
2647 * in link state events, so just mark our idea of the link as down
2648 * and ignore the event.
2649 */
2650 if (!netif_running(ndev) || !netif_device_present(ndev)) {
2651 fep->link = 0;
2652 } else if (phy_dev->link) {
2653 if (!fep->link) {
2654 fep->link = phy_dev->link;
2655 status_change = 1;
2656 }
2657
2658 if (fep->full_duplex != phy_dev->duplex) {
2659 fep->full_duplex = phy_dev->duplex;
2660 status_change = 1;
2661 }
2662
2663 if (phy_dev->speed != fep->speed) {
2664 fep->speed = phy_dev->speed;
2665 status_change = 1;
2666 }
2667
2668 /* if any of the above changed restart the FEC */
2669 if (status_change) {
2670 netif_stop_queue(ndev);
2671 napi_disable(&fep->napi);
2672 netif_tx_lock_bh(ndev);
2673 fec_restart(ndev);
2674 netif_tx_wake_all_queues(ndev);
2675 netif_tx_unlock_bh(ndev);
2676 napi_enable(&fep->napi);
2677 }
2678 if (fep->quirks & FEC_QUIRK_HAS_EEE)
2679 fec_enet_eee_mode_set(ndev,
2680 phy_dev->eee_cfg.tx_lpi_timer,
2681 phy_dev->enable_tx_lpi);
2682 } else {
2683 if (fep->link) {
2684 netif_stop_queue(ndev);
2685 napi_disable(&fep->napi);
2686 netif_tx_lock_bh(ndev);
2687 fec_stop(ndev);
2688 netif_tx_unlock_bh(ndev);
2689 napi_enable(&fep->napi);
2690 fep->link = phy_dev->link;
2691 status_change = 1;
2692 }
2693 }
2694
2695 if (status_change)
2696 phy_print_status(phy_dev);
2697 }
2698
fec_enet_mdio_wait(struct fec_enet_private * fep)2699 static int fec_enet_mdio_wait(struct fec_enet_private *fep)
2700 {
2701 uint ievent;
2702 int ret;
2703
2704 ret = readl_poll_timeout_atomic(fep->hwp + FEC_IEVENT, ievent,
2705 ievent & FEC_ENET_MII, 2, 30000);
2706
2707 if (!ret)
2708 writel(FEC_ENET_MII, fep->hwp + FEC_IEVENT);
2709
2710 return ret;
2711 }
2712
fec_enet_mdio_read_c22(struct mii_bus * bus,int mii_id,int regnum)2713 static int fec_enet_mdio_read_c22(struct mii_bus *bus, int mii_id, int regnum)
2714 {
2715 struct fec_enet_private *fep = bus->priv;
2716 struct device *dev = &fep->pdev->dev;
2717 int ret = 0, frame_start, frame_addr, frame_op;
2718
2719 ret = pm_runtime_resume_and_get(dev);
2720 if (ret < 0)
2721 return ret;
2722
2723 /* C22 read */
2724 frame_op = FEC_MMFR_OP_READ;
2725 frame_start = FEC_MMFR_ST;
2726 frame_addr = regnum;
2727
2728 /* start a read op */
2729 writel(frame_start | frame_op |
2730 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(frame_addr) |
2731 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
2732
2733 /* wait for end of transfer */
2734 ret = fec_enet_mdio_wait(fep);
2735 if (ret) {
2736 netdev_err(fep->netdev, "MDIO read timeout\n");
2737 goto out;
2738 }
2739
2740 ret = FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
2741
2742 out:
2743 pm_runtime_put_autosuspend(dev);
2744
2745 return ret;
2746 }
2747
fec_enet_mdio_read_c45(struct mii_bus * bus,int mii_id,int devad,int regnum)2748 static int fec_enet_mdio_read_c45(struct mii_bus *bus, int mii_id,
2749 int devad, int regnum)
2750 {
2751 struct fec_enet_private *fep = bus->priv;
2752 struct device *dev = &fep->pdev->dev;
2753 int ret = 0, frame_start, frame_op;
2754
2755 ret = pm_runtime_resume_and_get(dev);
2756 if (ret < 0)
2757 return ret;
2758
2759 frame_start = FEC_MMFR_ST_C45;
2760
2761 /* write address */
2762 writel(frame_start | FEC_MMFR_OP_ADDR_WRITE |
2763 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(devad) |
2764 FEC_MMFR_TA | (regnum & 0xFFFF),
2765 fep->hwp + FEC_MII_DATA);
2766
2767 /* wait for end of transfer */
2768 ret = fec_enet_mdio_wait(fep);
2769 if (ret) {
2770 netdev_err(fep->netdev, "MDIO address write timeout\n");
2771 goto out;
2772 }
2773
2774 frame_op = FEC_MMFR_OP_READ_C45;
2775
2776 /* start a read op */
2777 writel(frame_start | frame_op |
2778 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(devad) |
2779 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
2780
2781 /* wait for end of transfer */
2782 ret = fec_enet_mdio_wait(fep);
2783 if (ret) {
2784 netdev_err(fep->netdev, "MDIO read timeout\n");
2785 goto out;
2786 }
2787
2788 ret = FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
2789
2790 out:
2791 pm_runtime_put_autosuspend(dev);
2792
2793 return ret;
2794 }
2795
fec_enet_mdio_write_c22(struct mii_bus * bus,int mii_id,int regnum,u16 value)2796 static int fec_enet_mdio_write_c22(struct mii_bus *bus, int mii_id, int regnum,
2797 u16 value)
2798 {
2799 struct fec_enet_private *fep = bus->priv;
2800 struct device *dev = &fep->pdev->dev;
2801 int ret, frame_start, frame_addr;
2802
2803 ret = pm_runtime_resume_and_get(dev);
2804 if (ret < 0)
2805 return ret;
2806
2807 /* C22 write */
2808 frame_start = FEC_MMFR_ST;
2809 frame_addr = regnum;
2810
2811 /* start a write op */
2812 writel(frame_start | FEC_MMFR_OP_WRITE |
2813 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(frame_addr) |
2814 FEC_MMFR_TA | FEC_MMFR_DATA(value),
2815 fep->hwp + FEC_MII_DATA);
2816
2817 /* wait for end of transfer */
2818 ret = fec_enet_mdio_wait(fep);
2819 if (ret)
2820 netdev_err(fep->netdev, "MDIO write timeout\n");
2821
2822 pm_runtime_put_autosuspend(dev);
2823
2824 return ret;
2825 }
2826
fec_enet_mdio_write_c45(struct mii_bus * bus,int mii_id,int devad,int regnum,u16 value)2827 static int fec_enet_mdio_write_c45(struct mii_bus *bus, int mii_id,
2828 int devad, int regnum, u16 value)
2829 {
2830 struct fec_enet_private *fep = bus->priv;
2831 struct device *dev = &fep->pdev->dev;
2832 int ret, frame_start;
2833
2834 ret = pm_runtime_resume_and_get(dev);
2835 if (ret < 0)
2836 return ret;
2837
2838 frame_start = FEC_MMFR_ST_C45;
2839
2840 /* write address */
2841 writel(frame_start | FEC_MMFR_OP_ADDR_WRITE |
2842 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(devad) |
2843 FEC_MMFR_TA | (regnum & 0xFFFF),
2844 fep->hwp + FEC_MII_DATA);
2845
2846 /* wait for end of transfer */
2847 ret = fec_enet_mdio_wait(fep);
2848 if (ret) {
2849 netdev_err(fep->netdev, "MDIO address write timeout\n");
2850 goto out;
2851 }
2852
2853 /* start a write op */
2854 writel(frame_start | FEC_MMFR_OP_WRITE |
2855 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(devad) |
2856 FEC_MMFR_TA | FEC_MMFR_DATA(value),
2857 fep->hwp + FEC_MII_DATA);
2858
2859 /* wait for end of transfer */
2860 ret = fec_enet_mdio_wait(fep);
2861 if (ret)
2862 netdev_err(fep->netdev, "MDIO write timeout\n");
2863
2864 out:
2865 pm_runtime_put_autosuspend(dev);
2866
2867 return ret;
2868 }
2869
fec_enet_phy_reset_after_clk_enable(struct net_device * ndev)2870 static void fec_enet_phy_reset_after_clk_enable(struct net_device *ndev)
2871 {
2872 struct fec_enet_private *fep = netdev_priv(ndev);
2873 struct phy_device *phy_dev = ndev->phydev;
2874
2875 if (phy_dev) {
2876 phy_reset_after_clk_enable(phy_dev);
2877 } else if (fep->phy_node) {
2878 /*
2879 * If the PHY still is not bound to the MAC, but there is
2880 * OF PHY node and a matching PHY device instance already,
2881 * use the OF PHY node to obtain the PHY device instance,
2882 * and then use that PHY device instance when triggering
2883 * the PHY reset.
2884 */
2885 phy_dev = of_phy_find_device(fep->phy_node);
2886 phy_reset_after_clk_enable(phy_dev);
2887 if (phy_dev)
2888 put_device(&phy_dev->mdio.dev);
2889 }
2890 }
2891
fec_enet_clk_enable(struct net_device * ndev,bool enable)2892 static int fec_enet_clk_enable(struct net_device *ndev, bool enable)
2893 {
2894 struct fec_enet_private *fep = netdev_priv(ndev);
2895 int ret;
2896
2897 if (enable) {
2898 ret = clk_prepare_enable(fep->clk_enet_out);
2899 if (ret)
2900 return ret;
2901
2902 if (fep->clk_ptp) {
2903 mutex_lock(&fep->ptp_clk_mutex);
2904 ret = clk_prepare_enable(fep->clk_ptp);
2905 if (ret) {
2906 mutex_unlock(&fep->ptp_clk_mutex);
2907 goto failed_clk_ptp;
2908 } else {
2909 fep->ptp_clk_on = true;
2910 }
2911 mutex_unlock(&fep->ptp_clk_mutex);
2912 }
2913
2914 ret = clk_prepare_enable(fep->clk_ref);
2915 if (ret)
2916 goto failed_clk_ref;
2917
2918 ret = clk_prepare_enable(fep->clk_2x_txclk);
2919 if (ret)
2920 goto failed_clk_2x_txclk;
2921
2922 fec_enet_phy_reset_after_clk_enable(ndev);
2923 } else {
2924 clk_disable_unprepare(fep->clk_enet_out);
2925 if (fep->clk_ptp) {
2926 mutex_lock(&fep->ptp_clk_mutex);
2927 clk_disable_unprepare(fep->clk_ptp);
2928 fep->ptp_clk_on = false;
2929 mutex_unlock(&fep->ptp_clk_mutex);
2930 }
2931 clk_disable_unprepare(fep->clk_ref);
2932 clk_disable_unprepare(fep->clk_2x_txclk);
2933 }
2934
2935 return 0;
2936
2937 failed_clk_2x_txclk:
2938 if (fep->clk_ref)
2939 clk_disable_unprepare(fep->clk_ref);
2940 failed_clk_ref:
2941 if (fep->clk_ptp) {
2942 mutex_lock(&fep->ptp_clk_mutex);
2943 clk_disable_unprepare(fep->clk_ptp);
2944 fep->ptp_clk_on = false;
2945 mutex_unlock(&fep->ptp_clk_mutex);
2946 }
2947 failed_clk_ptp:
2948 clk_disable_unprepare(fep->clk_enet_out);
2949
2950 return ret;
2951 }
2952
fec_enet_parse_rgmii_delay(struct fec_enet_private * fep,struct device_node * np)2953 static int fec_enet_parse_rgmii_delay(struct fec_enet_private *fep,
2954 struct device_node *np)
2955 {
2956 u32 rgmii_tx_delay, rgmii_rx_delay;
2957
2958 /* For rgmii tx internal delay, valid values are 0ps and 2000ps */
2959 if (!of_property_read_u32(np, "tx-internal-delay-ps", &rgmii_tx_delay)) {
2960 if (rgmii_tx_delay != 0 && rgmii_tx_delay != 2000) {
2961 dev_err(&fep->pdev->dev, "The only allowed RGMII TX delay values are: 0ps, 2000ps");
2962 return -EINVAL;
2963 } else if (rgmii_tx_delay == 2000) {
2964 fep->rgmii_txc_dly = true;
2965 }
2966 }
2967
2968 /* For rgmii rx internal delay, valid values are 0ps and 2000ps */
2969 if (!of_property_read_u32(np, "rx-internal-delay-ps", &rgmii_rx_delay)) {
2970 if (rgmii_rx_delay != 0 && rgmii_rx_delay != 2000) {
2971 dev_err(&fep->pdev->dev, "The only allowed RGMII RX delay values are: 0ps, 2000ps");
2972 return -EINVAL;
2973 } else if (rgmii_rx_delay == 2000) {
2974 fep->rgmii_rxc_dly = true;
2975 }
2976 }
2977
2978 return 0;
2979 }
2980
fec_enet_mii_probe(struct net_device * ndev)2981 static int fec_enet_mii_probe(struct net_device *ndev)
2982 {
2983 struct fec_enet_private *fep = netdev_priv(ndev);
2984 struct phy_device *phy_dev;
2985 int ret;
2986
2987 if (fep->phy_node) {
2988 phy_dev = of_phy_connect(ndev, fep->phy_node,
2989 &fec_enet_adjust_link, 0,
2990 fep->phy_interface);
2991 if (!phy_dev) {
2992 netdev_err(ndev, "Unable to connect to phy\n");
2993 return -ENODEV;
2994 }
2995 } else {
2996 /* check for attached phy */
2997 phy_dev = phy_find_first(fep->mii_bus);
2998 if (fep->dev_id && phy_dev)
2999 phy_dev = phy_find_next(fep->mii_bus, phy_dev);
3000
3001 if (!phy_dev) {
3002 netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
3003 phy_dev = fixed_phy_register_100fd();
3004 if (IS_ERR(phy_dev)) {
3005 netdev_err(ndev, "could not register fixed PHY\n");
3006 return PTR_ERR(phy_dev);
3007 }
3008 }
3009
3010 ret = phy_connect_direct(ndev, phy_dev, &fec_enet_adjust_link,
3011 fep->phy_interface);
3012 if (ret) {
3013 if (phy_is_pseudo_fixed_link(phy_dev))
3014 fixed_phy_unregister(phy_dev);
3015 netdev_err(ndev, "could not attach to PHY\n");
3016 return ret;
3017 }
3018
3019 }
3020
3021 /* mask with MAC supported features */
3022 if (fep->quirks & FEC_QUIRK_HAS_GBIT) {
3023 phy_set_max_speed(phy_dev, 1000);
3024 phy_remove_link_mode(phy_dev,
3025 ETHTOOL_LINK_MODE_1000baseT_Half_BIT);
3026 phy_support_sym_pause(phy_dev);
3027 }
3028 else
3029 phy_set_max_speed(phy_dev, 100);
3030
3031 if (fep->quirks & FEC_QUIRK_HAS_EEE)
3032 phy_support_eee(phy_dev);
3033
3034 fep->link = 0;
3035 fep->full_duplex = 0;
3036
3037 phy_attached_info(phy_dev);
3038
3039 return 0;
3040 }
3041
fec_enet_mii_init(struct platform_device * pdev)3042 static int fec_enet_mii_init(struct platform_device *pdev)
3043 {
3044 static struct mii_bus *fec0_mii_bus;
3045 struct net_device *ndev = platform_get_drvdata(pdev);
3046 struct fec_enet_private *fep = netdev_priv(ndev);
3047 bool suppress_preamble = false;
3048 struct phy_device *phydev;
3049 struct device_node *node;
3050 int err = -ENXIO;
3051 u32 mii_speed, holdtime;
3052 u32 bus_freq;
3053
3054 /*
3055 * The i.MX28 dual fec interfaces are not equal.
3056 * Here are the differences:
3057 *
3058 * - fec0 supports MII & RMII modes while fec1 only supports RMII
3059 * - fec0 acts as the 1588 time master while fec1 is slave
3060 * - external phys can only be configured by fec0
3061 *
3062 * That is to say fec1 can not work independently. It only works
3063 * when fec0 is working. The reason behind this design is that the
3064 * second interface is added primarily for Switch mode.
3065 *
3066 * Because of the last point above, both phys are attached on fec0
3067 * mdio interface in board design, and need to be configured by
3068 * fec0 mii_bus.
3069 */
3070 if ((fep->quirks & FEC_QUIRK_SINGLE_MDIO) && fep->dev_id > 0) {
3071 /* fec1 uses fec0 mii_bus */
3072 if (mii_cnt && fec0_mii_bus) {
3073 fep->mii_bus = fec0_mii_bus;
3074 mii_cnt++;
3075 return 0;
3076 }
3077 return -ENOENT;
3078 }
3079
3080 bus_freq = 2500000; /* 2.5MHz by default */
3081 node = of_get_child_by_name(pdev->dev.of_node, "mdio");
3082 if (node) {
3083 of_property_read_u32(node, "clock-frequency", &bus_freq);
3084 suppress_preamble = of_property_read_bool(node,
3085 "suppress-preamble");
3086 }
3087
3088 /*
3089 * Set MII speed (= clk_get_rate() / 2 * phy_speed)
3090 *
3091 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
3092 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
3093 * Reference Manual has an error on this, and gets fixed on i.MX6Q
3094 * document.
3095 */
3096 mii_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), bus_freq * 2);
3097 if (fep->quirks & FEC_QUIRK_ENET_MAC)
3098 mii_speed--;
3099 if (mii_speed > 63) {
3100 dev_err(&pdev->dev,
3101 "fec clock (%lu) too fast to get right mii speed\n",
3102 clk_get_rate(fep->clk_ipg));
3103 err = -EINVAL;
3104 goto err_out;
3105 }
3106
3107 /*
3108 * The i.MX28 and i.MX6 types have another filed in the MSCR (aka
3109 * MII_SPEED) register that defines the MDIO output hold time. Earlier
3110 * versions are RAZ there, so just ignore the difference and write the
3111 * register always.
3112 * The minimal hold time according to IEE802.3 (clause 22) is 10 ns.
3113 * HOLDTIME + 1 is the number of clk cycles the fec is holding the
3114 * output.
3115 * The HOLDTIME bitfield takes values between 0 and 7 (inclusive).
3116 * Given that ceil(clkrate / 5000000) <= 64, the calculation for
3117 * holdtime cannot result in a value greater than 3.
3118 */
3119 holdtime = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 100000000) - 1;
3120
3121 fep->phy_speed = mii_speed << 1 | holdtime << 8;
3122
3123 if (suppress_preamble)
3124 fep->phy_speed |= BIT(7);
3125
3126 if (fep->quirks & FEC_QUIRK_CLEAR_SETUP_MII) {
3127 /* Clear MMFR to avoid to generate MII event by writing MSCR.
3128 * MII event generation condition:
3129 * - writing MSCR:
3130 * - mmfr[31:0]_not_zero & mscr[7:0]_is_zero &
3131 * mscr_reg_data_in[7:0] != 0
3132 * - writing MMFR:
3133 * - mscr[7:0]_not_zero
3134 */
3135 writel(0, fep->hwp + FEC_MII_DATA);
3136 }
3137
3138 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
3139
3140 /* Clear any pending transaction complete indication */
3141 writel(FEC_ENET_MII, fep->hwp + FEC_IEVENT);
3142
3143 fep->mii_bus = mdiobus_alloc();
3144 if (fep->mii_bus == NULL) {
3145 err = -ENOMEM;
3146 goto err_out;
3147 }
3148
3149 fep->mii_bus->name = "fec_enet_mii_bus";
3150 fep->mii_bus->read = fec_enet_mdio_read_c22;
3151 fep->mii_bus->write = fec_enet_mdio_write_c22;
3152 if (fep->quirks & FEC_QUIRK_HAS_MDIO_C45) {
3153 fep->mii_bus->read_c45 = fec_enet_mdio_read_c45;
3154 fep->mii_bus->write_c45 = fec_enet_mdio_write_c45;
3155 }
3156 snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
3157 pdev->name, fep->dev_id + 1);
3158 fep->mii_bus->priv = fep;
3159 fep->mii_bus->parent = &pdev->dev;
3160
3161 err = of_mdiobus_register(fep->mii_bus, node);
3162 if (err)
3163 goto err_out_free_mdiobus;
3164 of_node_put(node);
3165
3166 /* find all the PHY devices on the bus and set mac_managed_pm to true */
3167 mdiobus_for_each_phy(fep->mii_bus, phydev)
3168 phydev->mac_managed_pm = true;
3169
3170 mii_cnt++;
3171
3172 /* save fec0 mii_bus */
3173 if (fep->quirks & FEC_QUIRK_SINGLE_MDIO)
3174 fec0_mii_bus = fep->mii_bus;
3175
3176 return 0;
3177
3178 err_out_free_mdiobus:
3179 mdiobus_free(fep->mii_bus);
3180 err_out:
3181 of_node_put(node);
3182 return err;
3183 }
3184
fec_enet_mii_remove(struct fec_enet_private * fep)3185 static void fec_enet_mii_remove(struct fec_enet_private *fep)
3186 {
3187 if (--mii_cnt == 0) {
3188 mdiobus_unregister(fep->mii_bus);
3189 mdiobus_free(fep->mii_bus);
3190 }
3191 }
3192
fec_enet_get_drvinfo(struct net_device * ndev,struct ethtool_drvinfo * info)3193 static void fec_enet_get_drvinfo(struct net_device *ndev,
3194 struct ethtool_drvinfo *info)
3195 {
3196 struct fec_enet_private *fep = netdev_priv(ndev);
3197
3198 strscpy(info->driver, fep->pdev->dev.driver->name,
3199 sizeof(info->driver));
3200 strscpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
3201 }
3202
fec_enet_get_regs_len(struct net_device * ndev)3203 static int fec_enet_get_regs_len(struct net_device *ndev)
3204 {
3205 struct fec_enet_private *fep = netdev_priv(ndev);
3206 struct resource *r;
3207 int s = 0;
3208
3209 r = platform_get_resource(fep->pdev, IORESOURCE_MEM, 0);
3210 if (r)
3211 s = resource_size(r);
3212
3213 return s;
3214 }
3215
3216 /* List of registers that can be safety be read to dump them with ethtool */
3217 #if !defined(CONFIG_M5272) || defined(CONFIG_COMPILE_TEST)
3218 static __u32 fec_enet_register_version = 2;
3219 static u32 fec_enet_register_offset[] = {
3220 FEC_IEVENT, FEC_IMASK, FEC_R_DES_ACTIVE_0, FEC_X_DES_ACTIVE_0,
3221 FEC_ECNTRL, FEC_MII_DATA, FEC_MII_SPEED, FEC_MIB_CTRLSTAT, FEC_R_CNTRL,
3222 FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH, FEC_OPD, FEC_TXIC0, FEC_TXIC1,
3223 FEC_TXIC2, FEC_RXIC0, FEC_RXIC1, FEC_RXIC2, FEC_HASH_TABLE_HIGH,
3224 FEC_HASH_TABLE_LOW, FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW,
3225 FEC_X_WMRK, FEC_R_BOUND, FEC_R_FSTART, FEC_R_DES_START_1,
3226 FEC_X_DES_START_1, FEC_R_BUFF_SIZE_1, FEC_R_DES_START_2,
3227 FEC_X_DES_START_2, FEC_R_BUFF_SIZE_2, FEC_R_DES_START_0,
3228 FEC_X_DES_START_0, FEC_R_BUFF_SIZE_0, FEC_R_FIFO_RSFL, FEC_R_FIFO_RSEM,
3229 FEC_R_FIFO_RAEM, FEC_R_FIFO_RAFL, FEC_RACC, FEC_RCMR_1, FEC_RCMR_2,
3230 FEC_DMA_CFG_1, FEC_DMA_CFG_2, FEC_R_DES_ACTIVE_1, FEC_X_DES_ACTIVE_1,
3231 FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_2, FEC_QOS_SCHEME,
3232 RMON_T_DROP, RMON_T_PACKETS, RMON_T_BC_PKT, RMON_T_MC_PKT,
3233 RMON_T_CRC_ALIGN, RMON_T_UNDERSIZE, RMON_T_OVERSIZE, RMON_T_FRAG,
3234 RMON_T_JAB, RMON_T_COL, RMON_T_P64, RMON_T_P65TO127, RMON_T_P128TO255,
3235 RMON_T_P256TO511, RMON_T_P512TO1023, RMON_T_P1024TO2047,
3236 RMON_T_P_GTE2048, RMON_T_OCTETS,
3237 IEEE_T_DROP, IEEE_T_FRAME_OK, IEEE_T_1COL, IEEE_T_MCOL, IEEE_T_DEF,
3238 IEEE_T_LCOL, IEEE_T_EXCOL, IEEE_T_MACERR, IEEE_T_CSERR, IEEE_T_SQE,
3239 IEEE_T_FDXFC, IEEE_T_OCTETS_OK,
3240 RMON_R_PACKETS, RMON_R_BC_PKT, RMON_R_MC_PKT, RMON_R_CRC_ALIGN,
3241 RMON_R_UNDERSIZE, RMON_R_OVERSIZE, RMON_R_FRAG, RMON_R_JAB,
3242 RMON_R_RESVD_O, RMON_R_P64, RMON_R_P65TO127, RMON_R_P128TO255,
3243 RMON_R_P256TO511, RMON_R_P512TO1023, RMON_R_P1024TO2047,
3244 RMON_R_P_GTE2048, RMON_R_OCTETS,
3245 IEEE_R_DROP, IEEE_R_FRAME_OK, IEEE_R_CRC, IEEE_R_ALIGN, IEEE_R_MACERR,
3246 IEEE_R_FDXFC, IEEE_R_OCTETS_OK
3247 };
3248 /* for i.MX6ul */
3249 static u32 fec_enet_register_offset_6ul[] = {
3250 FEC_IEVENT, FEC_IMASK, FEC_R_DES_ACTIVE_0, FEC_X_DES_ACTIVE_0,
3251 FEC_ECNTRL, FEC_MII_DATA, FEC_MII_SPEED, FEC_MIB_CTRLSTAT, FEC_R_CNTRL,
3252 FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH, FEC_OPD, FEC_TXIC0, FEC_RXIC0,
3253 FEC_HASH_TABLE_HIGH, FEC_HASH_TABLE_LOW, FEC_GRP_HASH_TABLE_HIGH,
3254 FEC_GRP_HASH_TABLE_LOW, FEC_X_WMRK, FEC_R_DES_START_0,
3255 FEC_X_DES_START_0, FEC_R_BUFF_SIZE_0, FEC_R_FIFO_RSFL, FEC_R_FIFO_RSEM,
3256 FEC_R_FIFO_RAEM, FEC_R_FIFO_RAFL, FEC_RACC,
3257 RMON_T_DROP, RMON_T_PACKETS, RMON_T_BC_PKT, RMON_T_MC_PKT,
3258 RMON_T_CRC_ALIGN, RMON_T_UNDERSIZE, RMON_T_OVERSIZE, RMON_T_FRAG,
3259 RMON_T_JAB, RMON_T_COL, RMON_T_P64, RMON_T_P65TO127, RMON_T_P128TO255,
3260 RMON_T_P256TO511, RMON_T_P512TO1023, RMON_T_P1024TO2047,
3261 RMON_T_P_GTE2048, RMON_T_OCTETS,
3262 IEEE_T_DROP, IEEE_T_FRAME_OK, IEEE_T_1COL, IEEE_T_MCOL, IEEE_T_DEF,
3263 IEEE_T_LCOL, IEEE_T_EXCOL, IEEE_T_MACERR, IEEE_T_CSERR, IEEE_T_SQE,
3264 IEEE_T_FDXFC, IEEE_T_OCTETS_OK,
3265 RMON_R_PACKETS, RMON_R_BC_PKT, RMON_R_MC_PKT, RMON_R_CRC_ALIGN,
3266 RMON_R_UNDERSIZE, RMON_R_OVERSIZE, RMON_R_FRAG, RMON_R_JAB,
3267 RMON_R_RESVD_O, RMON_R_P64, RMON_R_P65TO127, RMON_R_P128TO255,
3268 RMON_R_P256TO511, RMON_R_P512TO1023, RMON_R_P1024TO2047,
3269 RMON_R_P_GTE2048, RMON_R_OCTETS,
3270 IEEE_R_DROP, IEEE_R_FRAME_OK, IEEE_R_CRC, IEEE_R_ALIGN, IEEE_R_MACERR,
3271 IEEE_R_FDXFC, IEEE_R_OCTETS_OK
3272 };
3273 #else
3274 static __u32 fec_enet_register_version = 1;
3275 static u32 fec_enet_register_offset[] = {
3276 FEC_ECNTRL, FEC_IEVENT, FEC_IMASK, FEC_IVEC, FEC_R_DES_ACTIVE_0,
3277 FEC_R_DES_ACTIVE_1, FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_0,
3278 FEC_X_DES_ACTIVE_1, FEC_X_DES_ACTIVE_2, FEC_MII_DATA, FEC_MII_SPEED,
3279 FEC_R_BOUND, FEC_R_FSTART, FEC_X_WMRK, FEC_X_FSTART, FEC_R_CNTRL,
3280 FEC_MAX_FRM_LEN, FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH,
3281 FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW, FEC_R_DES_START_0,
3282 FEC_R_DES_START_1, FEC_R_DES_START_2, FEC_X_DES_START_0,
3283 FEC_X_DES_START_1, FEC_X_DES_START_2, FEC_R_BUFF_SIZE_0,
3284 FEC_R_BUFF_SIZE_1, FEC_R_BUFF_SIZE_2
3285 };
3286 #endif
3287
fec_enet_get_regs(struct net_device * ndev,struct ethtool_regs * regs,void * regbuf)3288 static void fec_enet_get_regs(struct net_device *ndev,
3289 struct ethtool_regs *regs, void *regbuf)
3290 {
3291 u32 reg_cnt = ARRAY_SIZE(fec_enet_register_offset);
3292 struct fec_enet_private *fep = netdev_priv(ndev);
3293 u32 __iomem *theregs = (u32 __iomem *)fep->hwp;
3294 u32 *reg_list = fec_enet_register_offset;
3295 struct device *dev = &fep->pdev->dev;
3296 u32 *buf = (u32 *)regbuf;
3297 u32 i, off;
3298 int ret;
3299
3300 #if !defined(CONFIG_M5272) || defined(CONFIG_COMPILE_TEST)
3301 if (of_machine_is_compatible("fsl,imx6ul")) {
3302 reg_list = fec_enet_register_offset_6ul;
3303 reg_cnt = ARRAY_SIZE(fec_enet_register_offset_6ul);
3304 }
3305 #endif
3306
3307 ret = pm_runtime_resume_and_get(dev);
3308 if (ret < 0)
3309 return;
3310
3311 regs->version = fec_enet_register_version;
3312
3313 memset(buf, 0, regs->len);
3314
3315 for (i = 0; i < reg_cnt; i++) {
3316 off = reg_list[i];
3317
3318 if ((off == FEC_R_BOUND || off == FEC_R_FSTART) &&
3319 !(fep->quirks & FEC_QUIRK_HAS_FRREG))
3320 continue;
3321
3322 off >>= 2;
3323 buf[off] = readl(&theregs[off]);
3324 }
3325
3326 pm_runtime_put_autosuspend(dev);
3327 }
3328
fec_enet_get_ts_info(struct net_device * ndev,struct kernel_ethtool_ts_info * info)3329 static int fec_enet_get_ts_info(struct net_device *ndev,
3330 struct kernel_ethtool_ts_info *info)
3331 {
3332 struct fec_enet_private *fep = netdev_priv(ndev);
3333
3334 if (fep->bufdesc_ex) {
3335
3336 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
3337 SOF_TIMESTAMPING_TX_HARDWARE |
3338 SOF_TIMESTAMPING_RX_HARDWARE |
3339 SOF_TIMESTAMPING_RAW_HARDWARE;
3340 if (fep->ptp_clock)
3341 info->phc_index = ptp_clock_index(fep->ptp_clock);
3342
3343 info->tx_types = (1 << HWTSTAMP_TX_OFF) |
3344 (1 << HWTSTAMP_TX_ON);
3345
3346 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
3347 (1 << HWTSTAMP_FILTER_ALL);
3348 return 0;
3349 } else {
3350 return ethtool_op_get_ts_info(ndev, info);
3351 }
3352 }
3353
3354 #if !defined(CONFIG_M5272)
3355
fec_enet_get_pauseparam(struct net_device * ndev,struct ethtool_pauseparam * pause)3356 static void fec_enet_get_pauseparam(struct net_device *ndev,
3357 struct ethtool_pauseparam *pause)
3358 {
3359 struct fec_enet_private *fep = netdev_priv(ndev);
3360
3361 pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
3362 pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
3363 pause->rx_pause = pause->tx_pause;
3364 }
3365
fec_enet_set_pauseparam(struct net_device * ndev,struct ethtool_pauseparam * pause)3366 static int fec_enet_set_pauseparam(struct net_device *ndev,
3367 struct ethtool_pauseparam *pause)
3368 {
3369 struct fec_enet_private *fep = netdev_priv(ndev);
3370
3371 if (!ndev->phydev)
3372 return -ENODEV;
3373
3374 if (pause->tx_pause != pause->rx_pause) {
3375 netdev_info(ndev,
3376 "hardware only support enable/disable both tx and rx");
3377 return -EINVAL;
3378 }
3379
3380 fep->pause_flag = 0;
3381
3382 /* tx pause must be same as rx pause */
3383 fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
3384 fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
3385
3386 phy_set_sym_pause(ndev->phydev, pause->rx_pause, pause->tx_pause,
3387 pause->autoneg);
3388
3389 if (pause->autoneg) {
3390 if (netif_running(ndev))
3391 fec_stop(ndev);
3392 phy_start_aneg(ndev->phydev);
3393 }
3394 if (netif_running(ndev)) {
3395 napi_disable(&fep->napi);
3396 netif_tx_lock_bh(ndev);
3397 fec_restart(ndev);
3398 netif_tx_wake_all_queues(ndev);
3399 netif_tx_unlock_bh(ndev);
3400 napi_enable(&fep->napi);
3401 }
3402
3403 return 0;
3404 }
3405
3406 static const struct fec_stat {
3407 char name[ETH_GSTRING_LEN];
3408 u16 offset;
3409 } fec_stats[] = {
3410 /* RMON TX */
3411 { "tx_dropped", RMON_T_DROP },
3412 { "tx_packets", RMON_T_PACKETS },
3413 { "tx_broadcast", RMON_T_BC_PKT },
3414 { "tx_multicast", RMON_T_MC_PKT },
3415 { "tx_crc_errors", RMON_T_CRC_ALIGN },
3416 { "tx_undersize", RMON_T_UNDERSIZE },
3417 { "tx_oversize", RMON_T_OVERSIZE },
3418 { "tx_fragment", RMON_T_FRAG },
3419 { "tx_jabber", RMON_T_JAB },
3420 { "tx_collision", RMON_T_COL },
3421 { "tx_64byte", RMON_T_P64 },
3422 { "tx_65to127byte", RMON_T_P65TO127 },
3423 { "tx_128to255byte", RMON_T_P128TO255 },
3424 { "tx_256to511byte", RMON_T_P256TO511 },
3425 { "tx_512to1023byte", RMON_T_P512TO1023 },
3426 { "tx_1024to2047byte", RMON_T_P1024TO2047 },
3427 { "tx_GTE2048byte", RMON_T_P_GTE2048 },
3428 { "tx_octets", RMON_T_OCTETS },
3429
3430 /* IEEE TX */
3431 { "IEEE_tx_drop", IEEE_T_DROP },
3432 { "IEEE_tx_frame_ok", IEEE_T_FRAME_OK },
3433 { "IEEE_tx_1col", IEEE_T_1COL },
3434 { "IEEE_tx_mcol", IEEE_T_MCOL },
3435 { "IEEE_tx_def", IEEE_T_DEF },
3436 { "IEEE_tx_lcol", IEEE_T_LCOL },
3437 { "IEEE_tx_excol", IEEE_T_EXCOL },
3438 { "IEEE_tx_macerr", IEEE_T_MACERR },
3439 { "IEEE_tx_cserr", IEEE_T_CSERR },
3440 { "IEEE_tx_sqe", IEEE_T_SQE },
3441 { "IEEE_tx_fdxfc", IEEE_T_FDXFC },
3442 { "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK },
3443
3444 /* RMON RX */
3445 { "rx_packets", RMON_R_PACKETS },
3446 { "rx_broadcast", RMON_R_BC_PKT },
3447 { "rx_multicast", RMON_R_MC_PKT },
3448 { "rx_crc_errors", RMON_R_CRC_ALIGN },
3449 { "rx_undersize", RMON_R_UNDERSIZE },
3450 { "rx_oversize", RMON_R_OVERSIZE },
3451 { "rx_fragment", RMON_R_FRAG },
3452 { "rx_jabber", RMON_R_JAB },
3453 { "rx_64byte", RMON_R_P64 },
3454 { "rx_65to127byte", RMON_R_P65TO127 },
3455 { "rx_128to255byte", RMON_R_P128TO255 },
3456 { "rx_256to511byte", RMON_R_P256TO511 },
3457 { "rx_512to1023byte", RMON_R_P512TO1023 },
3458 { "rx_1024to2047byte", RMON_R_P1024TO2047 },
3459 { "rx_GTE2048byte", RMON_R_P_GTE2048 },
3460 { "rx_octets", RMON_R_OCTETS },
3461
3462 /* IEEE RX */
3463 { "IEEE_rx_drop", IEEE_R_DROP },
3464 { "IEEE_rx_frame_ok", IEEE_R_FRAME_OK },
3465 { "IEEE_rx_crc", IEEE_R_CRC },
3466 { "IEEE_rx_align", IEEE_R_ALIGN },
3467 { "IEEE_rx_macerr", IEEE_R_MACERR },
3468 { "IEEE_rx_fdxfc", IEEE_R_FDXFC },
3469 { "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK },
3470 };
3471
3472 #define FEC_STATS_SIZE (ARRAY_SIZE(fec_stats) * sizeof(u64))
3473
3474 static const char *fec_xdp_stat_strs[XDP_STATS_TOTAL] = {
3475 "rx_xdp_redirect", /* RX_XDP_REDIRECT = 0, */
3476 "rx_xdp_pass", /* RX_XDP_PASS, */
3477 "rx_xdp_drop", /* RX_XDP_DROP, */
3478 "rx_xdp_tx", /* RX_XDP_TX, */
3479 "rx_xdp_tx_errors", /* RX_XDP_TX_ERRORS, */
3480 "tx_xdp_xmit", /* TX_XDP_XMIT, */
3481 "tx_xdp_xmit_errors", /* TX_XDP_XMIT_ERRORS, */
3482 };
3483
fec_enet_update_ethtool_stats(struct net_device * dev)3484 static void fec_enet_update_ethtool_stats(struct net_device *dev)
3485 {
3486 struct fec_enet_private *fep = netdev_priv(dev);
3487 int i;
3488
3489 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
3490 fep->ethtool_stats[i] = readl(fep->hwp + fec_stats[i].offset);
3491 }
3492
fec_enet_get_xdp_stats(struct fec_enet_private * fep,u64 * data)3493 static void fec_enet_get_xdp_stats(struct fec_enet_private *fep, u64 *data)
3494 {
3495 u64 xdp_stats[XDP_STATS_TOTAL] = { 0 };
3496 struct fec_enet_priv_rx_q *rxq;
3497 int i, j;
3498
3499 for (i = fep->num_rx_queues - 1; i >= 0; i--) {
3500 rxq = fep->rx_queue[i];
3501
3502 for (j = 0; j < XDP_STATS_TOTAL; j++)
3503 xdp_stats[j] += rxq->stats[j];
3504 }
3505
3506 memcpy(data, xdp_stats, sizeof(xdp_stats));
3507 }
3508
fec_enet_page_pool_stats(struct fec_enet_private * fep,u64 * data)3509 static void fec_enet_page_pool_stats(struct fec_enet_private *fep, u64 *data)
3510 {
3511 #ifdef CONFIG_PAGE_POOL_STATS
3512 struct page_pool_stats stats = {};
3513 struct fec_enet_priv_rx_q *rxq;
3514 int i;
3515
3516 for (i = fep->num_rx_queues - 1; i >= 0; i--) {
3517 rxq = fep->rx_queue[i];
3518
3519 if (!rxq->page_pool)
3520 continue;
3521
3522 page_pool_get_stats(rxq->page_pool, &stats);
3523 }
3524
3525 page_pool_ethtool_stats_get(data, &stats);
3526 #endif
3527 }
3528
fec_enet_get_ethtool_stats(struct net_device * dev,struct ethtool_stats * stats,u64 * data)3529 static void fec_enet_get_ethtool_stats(struct net_device *dev,
3530 struct ethtool_stats *stats, u64 *data)
3531 {
3532 struct fec_enet_private *fep = netdev_priv(dev);
3533
3534 if (netif_running(dev))
3535 fec_enet_update_ethtool_stats(dev);
3536
3537 memcpy(data, fep->ethtool_stats, FEC_STATS_SIZE);
3538 data += FEC_STATS_SIZE / sizeof(u64);
3539
3540 fec_enet_get_xdp_stats(fep, data);
3541 data += XDP_STATS_TOTAL;
3542
3543 fec_enet_page_pool_stats(fep, data);
3544 }
3545
fec_enet_get_strings(struct net_device * netdev,u32 stringset,u8 * data)3546 static void fec_enet_get_strings(struct net_device *netdev,
3547 u32 stringset, u8 *data)
3548 {
3549 int i;
3550 switch (stringset) {
3551 case ETH_SS_STATS:
3552 for (i = 0; i < ARRAY_SIZE(fec_stats); i++) {
3553 ethtool_puts(&data, fec_stats[i].name);
3554 }
3555 for (i = 0; i < ARRAY_SIZE(fec_xdp_stat_strs); i++) {
3556 ethtool_puts(&data, fec_xdp_stat_strs[i]);
3557 }
3558 page_pool_ethtool_stats_get_strings(data);
3559
3560 break;
3561 case ETH_SS_TEST:
3562 net_selftest_get_strings(data);
3563 break;
3564 }
3565 }
3566
fec_enet_get_sset_count(struct net_device * dev,int sset)3567 static int fec_enet_get_sset_count(struct net_device *dev, int sset)
3568 {
3569 int count;
3570
3571 switch (sset) {
3572 case ETH_SS_STATS:
3573 count = ARRAY_SIZE(fec_stats) + XDP_STATS_TOTAL;
3574 count += page_pool_ethtool_stats_get_count();
3575 return count;
3576
3577 case ETH_SS_TEST:
3578 return net_selftest_get_count();
3579 default:
3580 return -EOPNOTSUPP;
3581 }
3582 }
3583
fec_enet_clear_ethtool_stats(struct net_device * dev)3584 static void fec_enet_clear_ethtool_stats(struct net_device *dev)
3585 {
3586 struct fec_enet_private *fep = netdev_priv(dev);
3587 struct fec_enet_priv_rx_q *rxq;
3588 int i, j;
3589
3590 /* Disable MIB statistics counters */
3591 writel(FEC_MIB_CTRLSTAT_DISABLE, fep->hwp + FEC_MIB_CTRLSTAT);
3592
3593 for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
3594 writel(0, fep->hwp + fec_stats[i].offset);
3595
3596 for (i = fep->num_rx_queues - 1; i >= 0; i--) {
3597 rxq = fep->rx_queue[i];
3598 for (j = 0; j < XDP_STATS_TOTAL; j++)
3599 rxq->stats[j] = 0;
3600 }
3601
3602 /* Don't disable MIB statistics counters */
3603 writel(0, fep->hwp + FEC_MIB_CTRLSTAT);
3604 }
3605
3606 #else /* !defined(CONFIG_M5272) */
3607 #define FEC_STATS_SIZE 0
fec_enet_update_ethtool_stats(struct net_device * dev)3608 static inline void fec_enet_update_ethtool_stats(struct net_device *dev)
3609 {
3610 }
3611
fec_enet_clear_ethtool_stats(struct net_device * dev)3612 static inline void fec_enet_clear_ethtool_stats(struct net_device *dev)
3613 {
3614 }
3615 #endif /* !defined(CONFIG_M5272) */
3616
3617 /* ITR clock source is enet system clock (clk_ahb).
3618 * TCTT unit is cycle_ns * 64 cycle
3619 * So, the ICTT value = X us / (cycle_ns * 64)
3620 */
fec_enet_us_to_itr_clock(struct net_device * ndev,int us)3621 static int fec_enet_us_to_itr_clock(struct net_device *ndev, int us)
3622 {
3623 struct fec_enet_private *fep = netdev_priv(ndev);
3624
3625 return us * (fep->itr_clk_rate / 64000) / 1000;
3626 }
3627
3628 /* Set threshold for interrupt coalescing */
fec_enet_itr_coal_set(struct net_device * ndev)3629 static void fec_enet_itr_coal_set(struct net_device *ndev)
3630 {
3631 struct fec_enet_private *fep = netdev_priv(ndev);
3632 u32 rx_itr = 0, tx_itr = 0;
3633 int rx_ictt, tx_ictt;
3634
3635 rx_ictt = fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr);
3636 tx_ictt = fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr);
3637
3638 if (rx_ictt > 0 && fep->rx_pkts_itr > 1) {
3639 /* Enable with enet system clock as Interrupt Coalescing timer Clock Source */
3640 rx_itr = FEC_ITR_EN | FEC_ITR_CLK_SEL;
3641 rx_itr |= FEC_ITR_ICFT(fep->rx_pkts_itr);
3642 rx_itr |= FEC_ITR_ICTT(rx_ictt);
3643 }
3644
3645 if (tx_ictt > 0 && fep->tx_pkts_itr > 1) {
3646 /* Enable with enet system clock as Interrupt Coalescing timer Clock Source */
3647 tx_itr = FEC_ITR_EN | FEC_ITR_CLK_SEL;
3648 tx_itr |= FEC_ITR_ICFT(fep->tx_pkts_itr);
3649 tx_itr |= FEC_ITR_ICTT(tx_ictt);
3650 }
3651
3652 writel(tx_itr, fep->hwp + FEC_TXIC0);
3653 writel(rx_itr, fep->hwp + FEC_RXIC0);
3654 if (fep->quirks & FEC_QUIRK_HAS_MULTI_QUEUES) {
3655 writel(tx_itr, fep->hwp + FEC_TXIC1);
3656 writel(rx_itr, fep->hwp + FEC_RXIC1);
3657 writel(tx_itr, fep->hwp + FEC_TXIC2);
3658 writel(rx_itr, fep->hwp + FEC_RXIC2);
3659 }
3660 }
3661
fec_enet_get_coalesce(struct net_device * ndev,struct ethtool_coalesce * ec,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)3662 static int fec_enet_get_coalesce(struct net_device *ndev,
3663 struct ethtool_coalesce *ec,
3664 struct kernel_ethtool_coalesce *kernel_coal,
3665 struct netlink_ext_ack *extack)
3666 {
3667 struct fec_enet_private *fep = netdev_priv(ndev);
3668
3669 if (!(fep->quirks & FEC_QUIRK_HAS_COALESCE))
3670 return -EOPNOTSUPP;
3671
3672 ec->rx_coalesce_usecs = fep->rx_time_itr;
3673 ec->rx_max_coalesced_frames = fep->rx_pkts_itr;
3674
3675 ec->tx_coalesce_usecs = fep->tx_time_itr;
3676 ec->tx_max_coalesced_frames = fep->tx_pkts_itr;
3677
3678 return 0;
3679 }
3680
fec_enet_set_coalesce(struct net_device * ndev,struct ethtool_coalesce * ec,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)3681 static int fec_enet_set_coalesce(struct net_device *ndev,
3682 struct ethtool_coalesce *ec,
3683 struct kernel_ethtool_coalesce *kernel_coal,
3684 struct netlink_ext_ack *extack)
3685 {
3686 struct fec_enet_private *fep = netdev_priv(ndev);
3687 struct device *dev = &fep->pdev->dev;
3688 unsigned int cycle;
3689
3690 if (!(fep->quirks & FEC_QUIRK_HAS_COALESCE))
3691 return -EOPNOTSUPP;
3692
3693 if (ec->rx_max_coalesced_frames > 255) {
3694 dev_err(dev, "Rx coalesced frames exceed hardware limitation\n");
3695 return -EINVAL;
3696 }
3697
3698 if (ec->tx_max_coalesced_frames > 255) {
3699 dev_err(dev, "Tx coalesced frame exceed hardware limitation\n");
3700 return -EINVAL;
3701 }
3702
3703 cycle = fec_enet_us_to_itr_clock(ndev, ec->rx_coalesce_usecs);
3704 if (cycle > 0xFFFF) {
3705 dev_err(dev, "Rx coalesced usec exceed hardware limitation\n");
3706 return -EINVAL;
3707 }
3708
3709 cycle = fec_enet_us_to_itr_clock(ndev, ec->tx_coalesce_usecs);
3710 if (cycle > 0xFFFF) {
3711 dev_err(dev, "Tx coalesced usec exceed hardware limitation\n");
3712 return -EINVAL;
3713 }
3714
3715 fep->rx_time_itr = ec->rx_coalesce_usecs;
3716 fep->rx_pkts_itr = ec->rx_max_coalesced_frames;
3717
3718 fep->tx_time_itr = ec->tx_coalesce_usecs;
3719 fep->tx_pkts_itr = ec->tx_max_coalesced_frames;
3720
3721 fec_enet_itr_coal_set(ndev);
3722
3723 return 0;
3724 }
3725
3726 static int
fec_enet_get_eee(struct net_device * ndev,struct ethtool_keee * edata)3727 fec_enet_get_eee(struct net_device *ndev, struct ethtool_keee *edata)
3728 {
3729 struct fec_enet_private *fep = netdev_priv(ndev);
3730
3731 if (!(fep->quirks & FEC_QUIRK_HAS_EEE))
3732 return -EOPNOTSUPP;
3733
3734 if (!netif_running(ndev))
3735 return -ENETDOWN;
3736
3737 return phy_ethtool_get_eee(ndev->phydev, edata);
3738 }
3739
3740 static int
fec_enet_set_eee(struct net_device * ndev,struct ethtool_keee * edata)3741 fec_enet_set_eee(struct net_device *ndev, struct ethtool_keee *edata)
3742 {
3743 struct fec_enet_private *fep = netdev_priv(ndev);
3744
3745 if (!(fep->quirks & FEC_QUIRK_HAS_EEE))
3746 return -EOPNOTSUPP;
3747
3748 if (!netif_running(ndev))
3749 return -ENETDOWN;
3750
3751 return phy_ethtool_set_eee(ndev->phydev, edata);
3752 }
3753
3754 static void
fec_enet_get_wol(struct net_device * ndev,struct ethtool_wolinfo * wol)3755 fec_enet_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
3756 {
3757 struct fec_enet_private *fep = netdev_priv(ndev);
3758
3759 if (fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET) {
3760 wol->supported = WAKE_MAGIC;
3761 wol->wolopts = fep->wol_flag & FEC_WOL_FLAG_ENABLE ? WAKE_MAGIC : 0;
3762 } else {
3763 wol->supported = wol->wolopts = 0;
3764 }
3765 }
3766
3767 static int
fec_enet_set_wol(struct net_device * ndev,struct ethtool_wolinfo * wol)3768 fec_enet_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
3769 {
3770 struct fec_enet_private *fep = netdev_priv(ndev);
3771
3772 if (!(fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET))
3773 return -EINVAL;
3774
3775 if (wol->wolopts & ~WAKE_MAGIC)
3776 return -EINVAL;
3777
3778 device_set_wakeup_enable(&ndev->dev, wol->wolopts & WAKE_MAGIC);
3779 if (device_may_wakeup(&ndev->dev))
3780 fep->wol_flag |= FEC_WOL_FLAG_ENABLE;
3781 else
3782 fep->wol_flag &= (~FEC_WOL_FLAG_ENABLE);
3783
3784 return 0;
3785 }
3786
3787 static const struct ethtool_ops fec_enet_ethtool_ops = {
3788 .supported_coalesce_params = ETHTOOL_COALESCE_USECS |
3789 ETHTOOL_COALESCE_MAX_FRAMES,
3790 .get_drvinfo = fec_enet_get_drvinfo,
3791 .get_regs_len = fec_enet_get_regs_len,
3792 .get_regs = fec_enet_get_regs,
3793 .nway_reset = phy_ethtool_nway_reset,
3794 .get_link = ethtool_op_get_link,
3795 .get_coalesce = fec_enet_get_coalesce,
3796 .set_coalesce = fec_enet_set_coalesce,
3797 #ifndef CONFIG_M5272
3798 .get_pauseparam = fec_enet_get_pauseparam,
3799 .set_pauseparam = fec_enet_set_pauseparam,
3800 .get_strings = fec_enet_get_strings,
3801 .get_ethtool_stats = fec_enet_get_ethtool_stats,
3802 .get_sset_count = fec_enet_get_sset_count,
3803 #endif
3804 .get_ts_info = fec_enet_get_ts_info,
3805 .get_wol = fec_enet_get_wol,
3806 .set_wol = fec_enet_set_wol,
3807 .get_eee = fec_enet_get_eee,
3808 .set_eee = fec_enet_set_eee,
3809 .get_link_ksettings = phy_ethtool_get_link_ksettings,
3810 .set_link_ksettings = phy_ethtool_set_link_ksettings,
3811 .self_test = net_selftest,
3812 };
3813
fec_xdp_rxq_info_reg(struct fec_enet_private * fep,struct fec_enet_priv_rx_q * rxq)3814 static int fec_xdp_rxq_info_reg(struct fec_enet_private *fep,
3815 struct fec_enet_priv_rx_q *rxq)
3816 {
3817 struct net_device *ndev = fep->netdev;
3818 void *allocator;
3819 int type, err;
3820
3821 err = xdp_rxq_info_reg(&rxq->xdp_rxq, ndev, rxq->id, 0);
3822 if (err) {
3823 netdev_err(ndev, "Failed to register xdp rxq info\n");
3824 return err;
3825 }
3826
3827 allocator = rxq->xsk_pool ? NULL : rxq->page_pool;
3828 type = rxq->xsk_pool ? MEM_TYPE_XSK_BUFF_POOL : MEM_TYPE_PAGE_POOL;
3829 err = xdp_rxq_info_reg_mem_model(&rxq->xdp_rxq, type, allocator);
3830 if (err) {
3831 netdev_err(ndev, "Failed to register XDP mem model\n");
3832 xdp_rxq_info_unreg(&rxq->xdp_rxq);
3833
3834 return err;
3835 }
3836
3837 if (rxq->xsk_pool)
3838 xsk_pool_set_rxq_info(rxq->xsk_pool, &rxq->xdp_rxq);
3839
3840 return 0;
3841 }
3842
fec_xdp_rxq_info_unreg(struct fec_enet_priv_rx_q * rxq)3843 static void fec_xdp_rxq_info_unreg(struct fec_enet_priv_rx_q *rxq)
3844 {
3845 if (xdp_rxq_info_is_reg(&rxq->xdp_rxq)) {
3846 xdp_rxq_info_unreg_mem_model(&rxq->xdp_rxq);
3847 xdp_rxq_info_unreg(&rxq->xdp_rxq);
3848 }
3849 }
3850
fec_free_rxq_buffers(struct fec_enet_priv_rx_q * rxq)3851 static void fec_free_rxq_buffers(struct fec_enet_priv_rx_q *rxq)
3852 {
3853 bool xsk = !!rxq->xsk_pool;
3854 int i;
3855
3856 for (i = 0; i < rxq->bd.ring_size; i++) {
3857 union fec_rx_buffer *buf = &rxq->rx_buf[i];
3858
3859 if (!buf->buf_p)
3860 continue;
3861
3862 if (xsk)
3863 xsk_buff_free(buf->xdp);
3864 else
3865 page_pool_put_full_page(rxq->page_pool,
3866 buf->page, false);
3867
3868 rxq->rx_buf[i].buf_p = NULL;
3869 }
3870
3871 if (!xsk) {
3872 page_pool_destroy(rxq->page_pool);
3873 rxq->page_pool = NULL;
3874 }
3875 }
3876
fec_enet_free_buffers(struct net_device * ndev)3877 static void fec_enet_free_buffers(struct net_device *ndev)
3878 {
3879 struct fec_enet_private *fep = netdev_priv(ndev);
3880 unsigned int i;
3881 struct fec_enet_priv_tx_q *txq;
3882 struct fec_enet_priv_rx_q *rxq;
3883 struct page *page;
3884 unsigned int q;
3885
3886 for (q = 0; q < fep->num_rx_queues; q++) {
3887 rxq = fep->rx_queue[q];
3888
3889 fec_xdp_rxq_info_unreg(rxq);
3890 fec_free_rxq_buffers(rxq);
3891
3892 for (i = 0; i < XDP_STATS_TOTAL; i++)
3893 rxq->stats[i] = 0;
3894 }
3895
3896 for (q = 0; q < fep->num_tx_queues; q++) {
3897 txq = fep->tx_queue[q];
3898 for (i = 0; i < txq->bd.ring_size; i++) {
3899 kfree(txq->tx_bounce[i]);
3900 txq->tx_bounce[i] = NULL;
3901
3902 switch (txq->tx_buf[i].type) {
3903 case FEC_TXBUF_T_SKB:
3904 dev_kfree_skb(txq->tx_buf[i].buf_p);
3905 break;
3906 case FEC_TXBUF_T_XDP_NDO:
3907 xdp_return_frame(txq->tx_buf[i].buf_p);
3908 break;
3909 case FEC_TXBUF_T_XDP_TX:
3910 page = txq->tx_buf[i].buf_p;
3911 page_pool_put_page(pp_page_to_nmdesc(page)->pp,
3912 page, 0, false);
3913 break;
3914 case FEC_TXBUF_T_XSK_TX:
3915 xsk_buff_free(txq->tx_buf[i].buf_p);
3916 break;
3917 default:
3918 break;
3919 }
3920
3921 txq->tx_buf[i].buf_p = NULL;
3922 txq->tx_buf[i].type = FEC_TXBUF_T_SKB;
3923 }
3924 }
3925 }
3926
fec_enet_free_queue(struct net_device * ndev)3927 static void fec_enet_free_queue(struct net_device *ndev)
3928 {
3929 struct fec_enet_private *fep = netdev_priv(ndev);
3930 int i;
3931 struct fec_enet_priv_tx_q *txq;
3932
3933 for (i = 0; i < fep->num_tx_queues; i++)
3934 if (fep->tx_queue[i] && fep->tx_queue[i]->tso_hdrs) {
3935 txq = fep->tx_queue[i];
3936 fec_dma_free(&fep->pdev->dev,
3937 txq->bd.ring_size * TSO_HEADER_SIZE,
3938 txq->tso_hdrs, txq->tso_hdrs_dma);
3939 }
3940
3941 for (i = 0; i < fep->num_rx_queues; i++)
3942 kfree(fep->rx_queue[i]);
3943 for (i = 0; i < fep->num_tx_queues; i++)
3944 kfree(fep->tx_queue[i]);
3945 }
3946
fec_enet_alloc_queue(struct net_device * ndev)3947 static int fec_enet_alloc_queue(struct net_device *ndev)
3948 {
3949 struct fec_enet_private *fep = netdev_priv(ndev);
3950 int i;
3951 int ret = 0;
3952 struct fec_enet_priv_tx_q *txq;
3953
3954 for (i = 0; i < fep->num_tx_queues; i++) {
3955 txq = kzalloc_obj(*txq);
3956 if (!txq) {
3957 ret = -ENOMEM;
3958 goto alloc_failed;
3959 }
3960
3961 fep->tx_queue[i] = txq;
3962 txq->bd.ring_size = TX_RING_SIZE;
3963 fep->total_tx_ring_size += fep->tx_queue[i]->bd.ring_size;
3964
3965 txq->tx_stop_threshold = FEC_MAX_SKB_DESCS;
3966 txq->tx_wake_threshold = FEC_MAX_SKB_DESCS + 2 * MAX_SKB_FRAGS;
3967
3968 txq->tso_hdrs = fec_dma_alloc(&fep->pdev->dev,
3969 txq->bd.ring_size * TSO_HEADER_SIZE,
3970 &txq->tso_hdrs_dma, GFP_KERNEL);
3971 if (!txq->tso_hdrs) {
3972 ret = -ENOMEM;
3973 goto alloc_failed;
3974 }
3975 }
3976
3977 for (i = 0; i < fep->num_rx_queues; i++) {
3978 fep->rx_queue[i] = kzalloc_obj(*fep->rx_queue[i]);
3979 if (!fep->rx_queue[i]) {
3980 ret = -ENOMEM;
3981 goto alloc_failed;
3982 }
3983
3984 fep->rx_queue[i]->bd.ring_size = RX_RING_SIZE;
3985 fep->total_rx_ring_size += fep->rx_queue[i]->bd.ring_size;
3986 }
3987 return ret;
3988
3989 alloc_failed:
3990 fec_enet_free_queue(ndev);
3991 return ret;
3992 }
3993
fec_alloc_rxq_buffers_pp(struct fec_enet_private * fep,struct fec_enet_priv_rx_q * rxq)3994 static int fec_alloc_rxq_buffers_pp(struct fec_enet_private *fep,
3995 struct fec_enet_priv_rx_q *rxq)
3996 {
3997 struct bufdesc *bdp = rxq->bd.base;
3998 dma_addr_t phys_addr;
3999 struct page *page;
4000 int i, err;
4001
4002 err = fec_enet_create_page_pool(fep, rxq);
4003 if (err < 0) {
4004 netdev_err(fep->netdev, "%s failed queue %d (%d)\n",
4005 __func__, rxq->bd.qid, err);
4006 return err;
4007 }
4008
4009 /* Some platforms require the RX buffer must be 64 bytes alignment.
4010 * Some platforms require 16 bytes alignment. And some platforms
4011 * require 4 bytes alignment. But since the page pool have been
4012 * introduced into the driver, the address of RX buffer is always
4013 * the page address plus FEC_ENET_XDP_HEADROOM, and
4014 * FEC_ENET_XDP_HEADROOM is 256 bytes. Therefore, this address can
4015 * satisfy all platforms. To prevent future modifications to
4016 * FEC_ENET_XDP_HEADROOM from ignoring this hardware limitation, a
4017 * BUILD_BUG_ON() test has been added, which ensures that
4018 * FEC_ENET_XDP_HEADROOM provides the required alignment.
4019 */
4020 BUILD_BUG_ON(FEC_ENET_XDP_HEADROOM & 0x3f);
4021
4022 for (i = 0; i < rxq->bd.ring_size; i++) {
4023 page = page_pool_dev_alloc_pages(rxq->page_pool);
4024 if (!page) {
4025 err = -ENOMEM;
4026 goto free_rx_buffers;
4027 }
4028
4029 phys_addr = page_pool_get_dma_addr(page) + FEC_ENET_XDP_HEADROOM;
4030 bdp->cbd_bufaddr = cpu_to_fec32(phys_addr);
4031 rxq->rx_buf[i].page = page;
4032 bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
4033 }
4034
4035 return 0;
4036
4037 free_rx_buffers:
4038 fec_free_rxq_buffers(rxq);
4039
4040 return err;
4041 }
4042
fec_alloc_rxq_buffers_zc(struct fec_enet_private * fep,struct fec_enet_priv_rx_q * rxq)4043 static int fec_alloc_rxq_buffers_zc(struct fec_enet_private *fep,
4044 struct fec_enet_priv_rx_q *rxq)
4045 {
4046 union fec_rx_buffer *buf = &rxq->rx_buf[0];
4047 struct bufdesc *bdp = rxq->bd.base;
4048 dma_addr_t phys_addr;
4049 int i;
4050
4051 for (i = 0; i < rxq->bd.ring_size; i++) {
4052 buf[i].xdp = xsk_buff_alloc(rxq->xsk_pool);
4053 if (!buf[i].xdp)
4054 break;
4055
4056 phys_addr = xsk_buff_xdp_get_dma(buf[i].xdp);
4057 bdp->cbd_bufaddr = cpu_to_fec32(phys_addr);
4058 bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
4059 }
4060
4061 for (; i < rxq->bd.ring_size; i++) {
4062 buf[i].xdp = NULL;
4063 bdp->cbd_bufaddr = cpu_to_fec32(0);
4064 bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
4065 }
4066
4067 return 0;
4068 }
4069
4070 static int
fec_enet_alloc_rxq_buffers(struct net_device * ndev,unsigned int queue)4071 fec_enet_alloc_rxq_buffers(struct net_device *ndev, unsigned int queue)
4072 {
4073 struct fec_enet_private *fep = netdev_priv(ndev);
4074 struct fec_enet_priv_rx_q *rxq;
4075 int err;
4076
4077 rxq = fep->rx_queue[queue];
4078 if (rxq->xsk_pool) {
4079 /* RX XDP ZC buffer pool may not be populated, e.g.
4080 * xdpsock TX-only.
4081 */
4082 fec_alloc_rxq_buffers_zc(fep, rxq);
4083 } else {
4084 err = fec_alloc_rxq_buffers_pp(fep, rxq);
4085 if (err)
4086 goto free_buffers;
4087 }
4088
4089 err = fec_xdp_rxq_info_reg(fep, rxq);
4090 if (err)
4091 goto free_buffers;
4092
4093 return 0;
4094
4095 free_buffers:
4096 fec_enet_free_buffers(ndev);
4097
4098 return err;
4099 }
4100
4101 static int
fec_enet_alloc_txq_buffers(struct net_device * ndev,unsigned int queue)4102 fec_enet_alloc_txq_buffers(struct net_device *ndev, unsigned int queue)
4103 {
4104 struct fec_enet_private *fep = netdev_priv(ndev);
4105 unsigned int i;
4106 struct bufdesc *bdp;
4107 struct fec_enet_priv_tx_q *txq;
4108
4109 txq = fep->tx_queue[queue];
4110 bdp = txq->bd.base;
4111 for (i = 0; i < txq->bd.ring_size; i++) {
4112 txq->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
4113 if (!txq->tx_bounce[i])
4114 goto err_alloc;
4115
4116 bdp->cbd_sc = cpu_to_fec16(0);
4117 bdp->cbd_bufaddr = cpu_to_fec32(0);
4118
4119 if (fep->bufdesc_ex) {
4120 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
4121 ebdp->cbd_esc = cpu_to_fec32(BD_ENET_TX_INT);
4122 }
4123
4124 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
4125 }
4126
4127 /* Set the last buffer to wrap. */
4128 bdp = fec_enet_get_prevdesc(bdp, &txq->bd);
4129 bdp->cbd_sc |= cpu_to_fec16(BD_ENET_TX_WRAP);
4130
4131 return 0;
4132
4133 err_alloc:
4134 fec_enet_free_buffers(ndev);
4135 return -ENOMEM;
4136 }
4137
fec_enet_alloc_buffers(struct net_device * ndev)4138 static int fec_enet_alloc_buffers(struct net_device *ndev)
4139 {
4140 struct fec_enet_private *fep = netdev_priv(ndev);
4141 unsigned int i;
4142
4143 for (i = 0; i < fep->num_rx_queues; i++)
4144 if (fec_enet_alloc_rxq_buffers(ndev, i))
4145 return -ENOMEM;
4146
4147 for (i = 0; i < fep->num_tx_queues; i++)
4148 if (fec_enet_alloc_txq_buffers(ndev, i))
4149 return -ENOMEM;
4150 return 0;
4151 }
4152
4153 static int
fec_enet_open(struct net_device * ndev)4154 fec_enet_open(struct net_device *ndev)
4155 {
4156 struct fec_enet_private *fep = netdev_priv(ndev);
4157 int ret;
4158 bool reset_again;
4159
4160 ret = pm_runtime_resume_and_get(&fep->pdev->dev);
4161 if (ret < 0)
4162 return ret;
4163
4164 pinctrl_pm_select_default_state(&fep->pdev->dev);
4165 ret = fec_enet_clk_enable(ndev, true);
4166 if (ret)
4167 goto clk_enable;
4168
4169 /* During the first fec_enet_open call the PHY isn't probed at this
4170 * point. Therefore the phy_reset_after_clk_enable() call within
4171 * fec_enet_clk_enable() fails. As we need this reset in order to be
4172 * sure the PHY is working correctly we check if we need to reset again
4173 * later when the PHY is probed
4174 */
4175 if (ndev->phydev && ndev->phydev->drv)
4176 reset_again = false;
4177 else
4178 reset_again = true;
4179
4180 /* I should reset the ring buffers here, but I don't yet know
4181 * a simple way to do that.
4182 */
4183
4184 ret = fec_enet_alloc_buffers(ndev);
4185 if (ret)
4186 goto err_enet_alloc;
4187
4188 /* Init MAC prior to mii bus probe */
4189 fec_restart(ndev);
4190
4191 /* Call phy_reset_after_clk_enable() again if it failed during
4192 * phy_reset_after_clk_enable() before because the PHY wasn't probed.
4193 */
4194 if (reset_again)
4195 fec_enet_phy_reset_after_clk_enable(ndev);
4196
4197 /* Probe and connect to PHY when open the interface */
4198 ret = fec_enet_mii_probe(ndev);
4199 if (ret)
4200 goto err_enet_mii_probe;
4201
4202 if (fep->quirks & FEC_QUIRK_ERR006687)
4203 imx6q_cpuidle_fec_irqs_used();
4204
4205 if (fep->quirks & FEC_QUIRK_HAS_PMQOS)
4206 cpu_latency_qos_add_request(&fep->pm_qos_req, 0);
4207
4208 napi_enable(&fep->napi);
4209 phy_start(ndev->phydev);
4210 netif_tx_start_all_queues(ndev);
4211
4212 device_set_wakeup_enable(&ndev->dev, fep->wol_flag &
4213 FEC_WOL_FLAG_ENABLE);
4214
4215 return 0;
4216
4217 err_enet_mii_probe:
4218 fec_enet_free_buffers(ndev);
4219 err_enet_alloc:
4220 fec_enet_clk_enable(ndev, false);
4221 clk_enable:
4222 pm_runtime_put_autosuspend(&fep->pdev->dev);
4223 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
4224 return ret;
4225 }
4226
4227 static int
fec_enet_close(struct net_device * ndev)4228 fec_enet_close(struct net_device *ndev)
4229 {
4230 struct fec_enet_private *fep = netdev_priv(ndev);
4231 struct phy_device *phy_dev = ndev->phydev;
4232
4233 phy_stop(phy_dev);
4234
4235 if (netif_device_present(ndev)) {
4236 napi_disable(&fep->napi);
4237 netif_tx_disable(ndev);
4238 fec_stop(ndev);
4239 }
4240
4241 phy_disconnect(phy_dev);
4242
4243 if (!fep->phy_node && phy_is_pseudo_fixed_link(phy_dev))
4244 fixed_phy_unregister(phy_dev);
4245
4246 if (fep->quirks & FEC_QUIRK_ERR006687)
4247 imx6q_cpuidle_fec_irqs_unused();
4248
4249 fec_enet_update_ethtool_stats(ndev);
4250
4251 fec_enet_clk_enable(ndev, false);
4252 if (fep->quirks & FEC_QUIRK_HAS_PMQOS)
4253 cpu_latency_qos_remove_request(&fep->pm_qos_req);
4254
4255 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
4256 pm_runtime_put_autosuspend(&fep->pdev->dev);
4257
4258 fec_enet_free_buffers(ndev);
4259
4260 return 0;
4261 }
4262
4263 /* Set or clear the multicast filter for this adaptor.
4264 * Skeleton taken from sunlance driver.
4265 * The CPM Ethernet implementation allows Multicast as well as individual
4266 * MAC address filtering. Some of the drivers check to make sure it is
4267 * a group multicast address, and discard those that are not. I guess I
4268 * will do the same for now, but just remove the test if you want
4269 * individual filtering as well (do the upper net layers want or support
4270 * this kind of feature?).
4271 */
4272
4273 #define FEC_HASH_BITS 6 /* #bits in hash */
4274
set_multicast_list(struct net_device * ndev)4275 static void set_multicast_list(struct net_device *ndev)
4276 {
4277 struct fec_enet_private *fep = netdev_priv(ndev);
4278 struct netdev_hw_addr *ha;
4279 unsigned int crc, tmp;
4280 unsigned char hash;
4281 unsigned int hash_high = 0, hash_low = 0;
4282
4283 if (ndev->flags & IFF_PROMISC) {
4284 tmp = readl(fep->hwp + FEC_R_CNTRL);
4285 tmp |= 0x8;
4286 writel(tmp, fep->hwp + FEC_R_CNTRL);
4287 return;
4288 }
4289
4290 tmp = readl(fep->hwp + FEC_R_CNTRL);
4291 tmp &= ~0x8;
4292 writel(tmp, fep->hwp + FEC_R_CNTRL);
4293
4294 if (ndev->flags & IFF_ALLMULTI) {
4295 /* Catch all multicast addresses, so set the
4296 * filter to all 1's
4297 */
4298 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
4299 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
4300
4301 return;
4302 }
4303
4304 /* Add the addresses in hash register */
4305 netdev_for_each_mc_addr(ha, ndev) {
4306 /* calculate crc32 value of mac address */
4307 crc = ether_crc_le(ndev->addr_len, ha->addr);
4308
4309 /* only upper 6 bits (FEC_HASH_BITS) are used
4310 * which point to specific bit in the hash registers
4311 */
4312 hash = (crc >> (32 - FEC_HASH_BITS)) & 0x3f;
4313
4314 if (hash > 31)
4315 hash_high |= 1 << (hash - 32);
4316 else
4317 hash_low |= 1 << hash;
4318 }
4319
4320 writel(hash_high, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
4321 writel(hash_low, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
4322 }
4323
4324 /* Set a MAC change in hardware. */
4325 static int
fec_set_mac_address(struct net_device * ndev,void * p)4326 fec_set_mac_address(struct net_device *ndev, void *p)
4327 {
4328 struct sockaddr *addr = p;
4329
4330 if (addr) {
4331 if (!is_valid_ether_addr(addr->sa_data))
4332 return -EADDRNOTAVAIL;
4333 eth_hw_addr_set(ndev, addr->sa_data);
4334 }
4335
4336 /* Add netif status check here to avoid system hang in below case:
4337 * ifconfig ethx down; ifconfig ethx hw ether xx:xx:xx:xx:xx:xx;
4338 * After ethx down, fec all clocks are gated off and then register
4339 * access causes system hang.
4340 */
4341 if (!netif_running(ndev))
4342 return 0;
4343
4344 fec_set_hw_mac_addr(ndev);
4345
4346 return 0;
4347 }
4348
fec_enet_set_netdev_features(struct net_device * netdev,netdev_features_t features)4349 static inline void fec_enet_set_netdev_features(struct net_device *netdev,
4350 netdev_features_t features)
4351 {
4352 struct fec_enet_private *fep = netdev_priv(netdev);
4353 netdev_features_t changed = features ^ netdev->features;
4354
4355 netdev->features = features;
4356
4357 /* Receive checksum has been changed */
4358 if (changed & NETIF_F_RXCSUM) {
4359 if (features & NETIF_F_RXCSUM)
4360 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
4361 else
4362 fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
4363 }
4364 }
4365
fec_set_features(struct net_device * netdev,netdev_features_t features)4366 static int fec_set_features(struct net_device *netdev,
4367 netdev_features_t features)
4368 {
4369 struct fec_enet_private *fep = netdev_priv(netdev);
4370 netdev_features_t changed = features ^ netdev->features;
4371
4372 if (netif_running(netdev) && changed & NETIF_F_RXCSUM) {
4373 napi_disable(&fep->napi);
4374 netif_tx_lock_bh(netdev);
4375 fec_stop(netdev);
4376 fec_enet_set_netdev_features(netdev, features);
4377 fec_restart(netdev);
4378 netif_tx_wake_all_queues(netdev);
4379 netif_tx_unlock_bh(netdev);
4380 napi_enable(&fep->napi);
4381 } else {
4382 fec_enet_set_netdev_features(netdev, features);
4383 }
4384
4385 return 0;
4386 }
4387
fec_enet_select_queue(struct net_device * ndev,struct sk_buff * skb,struct net_device * sb_dev)4388 static u16 fec_enet_select_queue(struct net_device *ndev, struct sk_buff *skb,
4389 struct net_device *sb_dev)
4390 {
4391 struct fec_enet_private *fep = netdev_priv(ndev);
4392 u16 vlan_tag = 0;
4393
4394 if (!(fep->quirks & FEC_QUIRK_HAS_AVB))
4395 return netdev_pick_tx(ndev, skb, NULL);
4396
4397 /* VLAN is present in the payload.*/
4398 if (eth_type_vlan(skb->protocol)) {
4399 struct vlan_ethhdr *vhdr = skb_vlan_eth_hdr(skb);
4400
4401 vlan_tag = ntohs(vhdr->h_vlan_TCI);
4402 /* VLAN is present in the skb but not yet pushed in the payload.*/
4403 } else if (skb_vlan_tag_present(skb)) {
4404 vlan_tag = skb->vlan_tci;
4405 } else {
4406 return vlan_tag;
4407 }
4408
4409 return fec_enet_vlan_pri_to_queue[vlan_tag >> 13];
4410 }
4411
fec_free_rxq(struct fec_enet_priv_rx_q * rxq)4412 static void fec_free_rxq(struct fec_enet_priv_rx_q *rxq)
4413 {
4414 fec_xdp_rxq_info_unreg(rxq);
4415 fec_free_rxq_buffers(rxq);
4416 kfree(rxq);
4417 }
4418
4419 static struct fec_enet_priv_rx_q *
fec_alloc_new_rxq_xsk(struct fec_enet_private * fep,int queue,struct xsk_buff_pool * pool)4420 fec_alloc_new_rxq_xsk(struct fec_enet_private *fep, int queue,
4421 struct xsk_buff_pool *pool)
4422 {
4423 struct fec_enet_priv_rx_q *old_rxq = fep->rx_queue[queue];
4424 struct fec_enet_priv_rx_q *rxq;
4425 union fec_rx_buffer *buf;
4426 int i;
4427
4428 rxq = kzalloc_obj(*rxq);
4429 if (!rxq)
4430 return NULL;
4431
4432 /* Copy the BD ring to the new rxq */
4433 rxq->bd = old_rxq->bd;
4434 rxq->id = queue;
4435 rxq->xsk_pool = pool;
4436 buf = &rxq->rx_buf[0];
4437
4438 for (i = 0; i < rxq->bd.ring_size; i++) {
4439 buf[i].xdp = xsk_buff_alloc(pool);
4440 /* RX XDP ZC buffer pool may not be populated, e.g.
4441 * xdpsock TX-only.
4442 */
4443 if (!buf[i].xdp)
4444 break;
4445 }
4446
4447 if (fec_xdp_rxq_info_reg(fep, rxq))
4448 goto free_buffers;
4449
4450 return rxq;
4451
4452 free_buffers:
4453 while (--i >= 0)
4454 xsk_buff_free(buf[i].xdp);
4455
4456 kfree(rxq);
4457
4458 return NULL;
4459 }
4460
4461 static struct fec_enet_priv_rx_q *
fec_alloc_new_rxq_pp(struct fec_enet_private * fep,int queue)4462 fec_alloc_new_rxq_pp(struct fec_enet_private *fep, int queue)
4463 {
4464 struct fec_enet_priv_rx_q *old_rxq = fep->rx_queue[queue];
4465 struct fec_enet_priv_rx_q *rxq;
4466 union fec_rx_buffer *buf;
4467 int i = 0;
4468
4469 rxq = kzalloc_obj(*rxq);
4470 if (!rxq)
4471 return NULL;
4472
4473 rxq->bd = old_rxq->bd;
4474 rxq->id = queue;
4475
4476 if (fec_enet_create_page_pool(fep, rxq))
4477 goto free_rxq;
4478
4479 buf = &rxq->rx_buf[0];
4480 for (; i < rxq->bd.ring_size; i++) {
4481 buf[i].page = page_pool_dev_alloc_pages(rxq->page_pool);
4482 if (!buf[i].page)
4483 goto free_buffers;
4484 }
4485
4486 if (fec_xdp_rxq_info_reg(fep, rxq))
4487 goto free_buffers;
4488
4489 return rxq;
4490
4491 free_buffers:
4492 while (--i >= 0)
4493 page_pool_put_full_page(rxq->page_pool,
4494 buf[i].page, false);
4495
4496 page_pool_destroy(rxq->page_pool);
4497 free_rxq:
4498 kfree(rxq);
4499
4500 return NULL;
4501 }
4502
fec_init_rxq_bd_buffers(struct fec_enet_priv_rx_q * rxq,bool xsk)4503 static void fec_init_rxq_bd_buffers(struct fec_enet_priv_rx_q *rxq, bool xsk)
4504 {
4505 union fec_rx_buffer *buf = &rxq->rx_buf[0];
4506 struct bufdesc *bdp = rxq->bd.base;
4507 dma_addr_t dma;
4508
4509 for (int i = 0; i < rxq->bd.ring_size; i++) {
4510 if (xsk)
4511 dma = buf[i].xdp ?
4512 xsk_buff_xdp_get_dma(buf[i].xdp) : 0;
4513 else
4514 dma = page_pool_get_dma_addr(buf[i].page) +
4515 FEC_ENET_XDP_HEADROOM;
4516
4517 bdp->cbd_bufaddr = cpu_to_fec32(dma);
4518 bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
4519 }
4520 }
4521
fec_xsk_restart_napi(struct fec_enet_private * fep,struct xsk_buff_pool * pool,u16 queue)4522 static int fec_xsk_restart_napi(struct fec_enet_private *fep,
4523 struct xsk_buff_pool *pool,
4524 u16 queue)
4525 {
4526 struct fec_enet_priv_tx_q *txq = fep->tx_queue[queue];
4527 struct net_device *ndev = fep->netdev;
4528 struct fec_enet_priv_rx_q *rxq;
4529 int err;
4530
4531 napi_disable(&fep->napi);
4532 netif_tx_disable(ndev);
4533 synchronize_rcu();
4534
4535 rxq = pool ? fec_alloc_new_rxq_xsk(fep, queue, pool) :
4536 fec_alloc_new_rxq_pp(fep, queue);
4537 if (!rxq) {
4538 err = -ENOMEM;
4539 goto err_alloc_new_rxq;
4540 }
4541
4542 /* Replace the old rxq with the new rxq */
4543 fec_free_rxq(fep->rx_queue[queue]);
4544 fep->rx_queue[queue] = rxq;
4545 fec_init_rxq_bd_buffers(rxq, !!pool);
4546 txq->xsk_pool = pool;
4547
4548 fec_restart(ndev);
4549 napi_enable(&fep->napi);
4550 netif_tx_start_all_queues(ndev);
4551
4552 return 0;
4553
4554 err_alloc_new_rxq:
4555 napi_enable(&fep->napi);
4556 netif_tx_start_all_queues(ndev);
4557
4558 return err;
4559 }
4560
fec_enable_xsk_pool(struct fec_enet_private * fep,struct xsk_buff_pool * pool,u16 queue)4561 static int fec_enable_xsk_pool(struct fec_enet_private *fep,
4562 struct xsk_buff_pool *pool,
4563 u16 queue)
4564 {
4565 int err;
4566
4567 err = xsk_pool_dma_map(pool, &fep->pdev->dev, 0);
4568 if (err) {
4569 netdev_err(fep->netdev, "Failed to map xsk pool\n");
4570 return err;
4571 }
4572
4573 if (!netif_running(fep->netdev)) {
4574 struct fec_enet_priv_rx_q *rxq = fep->rx_queue[queue];
4575 struct fec_enet_priv_tx_q *txq = fep->tx_queue[queue];
4576
4577 rxq->xsk_pool = pool;
4578 txq->xsk_pool = pool;
4579
4580 return 0;
4581 }
4582
4583 err = fec_xsk_restart_napi(fep, pool, queue);
4584 if (err) {
4585 xsk_pool_dma_unmap(pool, 0);
4586 return err;
4587 }
4588
4589 return 0;
4590 }
4591
fec_disable_xsk_pool(struct fec_enet_private * fep,u16 queue)4592 static int fec_disable_xsk_pool(struct fec_enet_private *fep,
4593 u16 queue)
4594 {
4595 struct fec_enet_priv_tx_q *txq = fep->tx_queue[queue];
4596 struct xsk_buff_pool *old_pool = txq->xsk_pool;
4597 int err;
4598
4599 if (!netif_running(fep->netdev)) {
4600 struct fec_enet_priv_rx_q *rxq = fep->rx_queue[queue];
4601
4602 xsk_pool_dma_unmap(old_pool, 0);
4603 rxq->xsk_pool = NULL;
4604 txq->xsk_pool = NULL;
4605
4606 return 0;
4607 }
4608
4609 err = fec_xsk_restart_napi(fep, NULL, queue);
4610 if (err)
4611 return err;
4612
4613 xsk_pool_dma_unmap(old_pool, 0);
4614
4615 return 0;
4616 }
4617
fec_setup_xsk_pool(struct fec_enet_private * fep,struct xsk_buff_pool * pool,u16 queue)4618 static int fec_setup_xsk_pool(struct fec_enet_private *fep,
4619 struct xsk_buff_pool *pool,
4620 u16 queue)
4621 {
4622 if (queue >= fep->num_rx_queues || queue >= fep->num_tx_queues)
4623 return -ERANGE;
4624
4625 return pool ? fec_enable_xsk_pool(fep, pool, queue) :
4626 fec_disable_xsk_pool(fep, queue);
4627 }
4628
fec_enet_bpf(struct net_device * dev,struct netdev_bpf * bpf)4629 static int fec_enet_bpf(struct net_device *dev, struct netdev_bpf *bpf)
4630 {
4631 struct fec_enet_private *fep = netdev_priv(dev);
4632 bool is_run = netif_running(dev);
4633 struct bpf_prog *old_prog;
4634
4635 /* No need to support the SoCs that require to do the frame swap
4636 * because the performance wouldn't be better than the skb mode.
4637 */
4638 if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
4639 return -EOPNOTSUPP;
4640
4641 switch (bpf->command) {
4642 case XDP_SETUP_PROG:
4643 if (!bpf->prog)
4644 xdp_features_clear_redirect_target(dev);
4645
4646 if (is_run) {
4647 napi_disable(&fep->napi);
4648 netif_tx_disable(dev);
4649 }
4650
4651 old_prog = xchg(&fep->xdp_prog, bpf->prog);
4652 if (old_prog)
4653 bpf_prog_put(old_prog);
4654
4655 fec_restart(dev);
4656
4657 if (is_run) {
4658 napi_enable(&fep->napi);
4659 netif_tx_start_all_queues(dev);
4660 }
4661
4662 if (bpf->prog)
4663 xdp_features_set_redirect_target(dev, false);
4664
4665 return 0;
4666 case XDP_SETUP_XSK_POOL:
4667 return fec_setup_xsk_pool(fep, bpf->xsk.pool,
4668 bpf->xsk.queue_id);
4669 default:
4670 return -EOPNOTSUPP;
4671 }
4672 }
4673
fec_enet_txq_xmit_frame(struct fec_enet_private * fep,struct fec_enet_priv_tx_q * txq,void * frame,u32 dma_sync_len,bool ndo_xmit)4674 static int fec_enet_txq_xmit_frame(struct fec_enet_private *fep,
4675 struct fec_enet_priv_tx_q *txq,
4676 void *frame, u32 dma_sync_len,
4677 bool ndo_xmit)
4678 {
4679 unsigned int index, status, estatus;
4680 struct bufdesc *bdp;
4681 dma_addr_t dma_addr;
4682 int entries_free;
4683 u16 frame_len;
4684
4685 entries_free = fec_enet_get_free_txdesc_num(txq);
4686 if (entries_free < MAX_SKB_FRAGS + 1) {
4687 netdev_err_once(fep->netdev, "NOT enough BD for SG!\n");
4688 return -EBUSY;
4689 }
4690
4691 /* Fill in a Tx ring entry */
4692 bdp = txq->bd.cur;
4693 status = fec16_to_cpu(bdp->cbd_sc);
4694 status &= ~BD_ENET_TX_STATS;
4695
4696 index = fec_enet_get_bd_index(bdp, &txq->bd);
4697
4698 if (ndo_xmit) {
4699 struct xdp_frame *xdpf = frame;
4700
4701 dma_addr = dma_map_single(&fep->pdev->dev, xdpf->data,
4702 xdpf->len, DMA_TO_DEVICE);
4703 if (dma_mapping_error(&fep->pdev->dev, dma_addr))
4704 return -ENOMEM;
4705
4706 frame_len = xdpf->len;
4707 txq->tx_buf[index].buf_p = xdpf;
4708 txq->tx_buf[index].type = FEC_TXBUF_T_XDP_NDO;
4709 } else {
4710 struct xdp_buff *xdpb = frame;
4711 struct page *page;
4712
4713 page = virt_to_page(xdpb->data);
4714 dma_addr = page_pool_get_dma_addr(page) +
4715 (xdpb->data - xdpb->data_hard_start);
4716 dma_sync_single_for_device(&fep->pdev->dev, dma_addr,
4717 dma_sync_len, DMA_BIDIRECTIONAL);
4718 frame_len = xdpb->data_end - xdpb->data;
4719 txq->tx_buf[index].buf_p = page;
4720 txq->tx_buf[index].type = FEC_TXBUF_T_XDP_TX;
4721 }
4722
4723 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
4724 if (fep->bufdesc_ex)
4725 estatus = BD_ENET_TX_INT;
4726
4727 bdp->cbd_bufaddr = cpu_to_fec32(dma_addr);
4728 bdp->cbd_datlen = cpu_to_fec16(frame_len);
4729
4730 if (fep->bufdesc_ex) {
4731 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
4732
4733 if (fep->quirks & FEC_QUIRK_HAS_AVB)
4734 estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
4735
4736 ebdp->cbd_bdu = 0;
4737 ebdp->cbd_esc = cpu_to_fec32(estatus);
4738 }
4739
4740 /* Make sure the updates to rest of the descriptor are performed before
4741 * transferring ownership.
4742 */
4743 dma_wmb();
4744
4745 /* Send it on its way. Tell FEC it's ready, interrupt when done,
4746 * it's the last BD of the frame, and to put the CRC on the end.
4747 */
4748 status |= (BD_ENET_TX_READY | BD_ENET_TX_TC);
4749 bdp->cbd_sc = cpu_to_fec16(status);
4750
4751 /* If this was the last BD in the ring, start at the beginning again. */
4752 bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
4753
4754 /* Make sure the update to bdp are performed before txq->bd.cur. */
4755 dma_wmb();
4756
4757 txq->bd.cur = bdp;
4758
4759 return 0;
4760 }
4761
fec_enet_xdp_tx_xmit(struct fec_enet_private * fep,int cpu,struct xdp_buff * xdp,u32 dma_sync_len,int queue)4762 static int fec_enet_xdp_tx_xmit(struct fec_enet_private *fep,
4763 int cpu, struct xdp_buff *xdp,
4764 u32 dma_sync_len, int queue)
4765 {
4766 struct netdev_queue *nq = netdev_get_tx_queue(fep->netdev, queue);
4767 struct fec_enet_priv_tx_q *txq = fep->tx_queue[queue];
4768 int ret;
4769
4770 __netif_tx_lock(nq, cpu);
4771
4772 /* Avoid tx timeout as XDP shares the queue with kernel stack */
4773 txq_trans_cond_update(nq);
4774 ret = fec_enet_txq_xmit_frame(fep, txq, xdp, dma_sync_len, false);
4775
4776 __netif_tx_unlock(nq);
4777
4778 return ret;
4779 }
4780
fec_enet_xdp_xmit(struct net_device * dev,int num_frames,struct xdp_frame ** frames,u32 flags)4781 static int fec_enet_xdp_xmit(struct net_device *dev,
4782 int num_frames,
4783 struct xdp_frame **frames,
4784 u32 flags)
4785 {
4786 struct fec_enet_private *fep = netdev_priv(dev);
4787 struct fec_enet_priv_tx_q *txq;
4788 int cpu = smp_processor_id();
4789 unsigned int sent_frames = 0;
4790 struct netdev_queue *nq;
4791 unsigned int queue;
4792 int i;
4793
4794 queue = fec_enet_xdp_get_tx_queue(fep, cpu);
4795 txq = fep->tx_queue[queue];
4796 nq = netdev_get_tx_queue(fep->netdev, queue);
4797
4798 __netif_tx_lock(nq, cpu);
4799
4800 /* Avoid tx timeout as XDP shares the queue with kernel stack */
4801 txq_trans_cond_update(nq);
4802 for (i = 0; i < num_frames; i++) {
4803 if (fec_enet_txq_xmit_frame(fep, txq, frames[i], 0, true) < 0)
4804 break;
4805 sent_frames++;
4806 }
4807
4808 if (sent_frames)
4809 fec_txq_trigger_xmit(fep, txq);
4810
4811 __netif_tx_unlock(nq);
4812
4813 return sent_frames;
4814 }
4815
fec_enet_xsk_wakeup(struct net_device * ndev,u32 queue,u32 flags)4816 static int fec_enet_xsk_wakeup(struct net_device *ndev, u32 queue, u32 flags)
4817 {
4818 struct fec_enet_private *fep = netdev_priv(ndev);
4819 struct fec_enet_priv_rx_q *rxq;
4820
4821 if (!netif_running(ndev) || !netif_carrier_ok(ndev))
4822 return -ENETDOWN;
4823
4824 if (queue >= fep->num_rx_queues || queue >= fep->num_tx_queues)
4825 return -ERANGE;
4826
4827 rxq = fep->rx_queue[queue];
4828 if (!rxq->xsk_pool)
4829 return -EINVAL;
4830
4831 if (!napi_if_scheduled_mark_missed(&fep->napi)) {
4832 if (likely(napi_schedule_prep(&fep->napi)))
4833 __napi_schedule(&fep->napi);
4834 }
4835
4836 return 0;
4837 }
4838
fec_hwtstamp_get(struct net_device * ndev,struct kernel_hwtstamp_config * config)4839 static int fec_hwtstamp_get(struct net_device *ndev,
4840 struct kernel_hwtstamp_config *config)
4841 {
4842 struct fec_enet_private *fep = netdev_priv(ndev);
4843
4844 if (!netif_running(ndev))
4845 return -EINVAL;
4846
4847 if (!fep->bufdesc_ex)
4848 return -EOPNOTSUPP;
4849
4850 fec_ptp_get(ndev, config);
4851
4852 return 0;
4853 }
4854
fec_hwtstamp_set(struct net_device * ndev,struct kernel_hwtstamp_config * config,struct netlink_ext_ack * extack)4855 static int fec_hwtstamp_set(struct net_device *ndev,
4856 struct kernel_hwtstamp_config *config,
4857 struct netlink_ext_ack *extack)
4858 {
4859 struct fec_enet_private *fep = netdev_priv(ndev);
4860
4861 if (!netif_running(ndev))
4862 return -EINVAL;
4863
4864 if (!fep->bufdesc_ex)
4865 return -EOPNOTSUPP;
4866
4867 return fec_ptp_set(ndev, config, extack);
4868 }
4869
fec_change_mtu(struct net_device * ndev,int new_mtu)4870 static int fec_change_mtu(struct net_device *ndev, int new_mtu)
4871 {
4872 struct fec_enet_private *fep = netdev_priv(ndev);
4873 int order;
4874
4875 if (netif_running(ndev))
4876 return -EBUSY;
4877
4878 order = get_order(new_mtu + VLAN_ETH_HLEN + ETH_FCS_LEN
4879 + FEC_DRV_RESERVE_SPACE);
4880 fep->rx_frame_size = (PAGE_SIZE << order) - FEC_DRV_RESERVE_SPACE;
4881 fep->pagepool_order = order;
4882 WRITE_ONCE(ndev->mtu, new_mtu);
4883
4884 return 0;
4885 }
4886
4887 static const struct net_device_ops fec_netdev_ops = {
4888 .ndo_open = fec_enet_open,
4889 .ndo_stop = fec_enet_close,
4890 .ndo_start_xmit = fec_enet_start_xmit,
4891 .ndo_select_queue = fec_enet_select_queue,
4892 .ndo_set_rx_mode = set_multicast_list,
4893 .ndo_validate_addr = eth_validate_addr,
4894 .ndo_tx_timeout = fec_timeout,
4895 .ndo_set_mac_address = fec_set_mac_address,
4896 .ndo_change_mtu = fec_change_mtu,
4897 .ndo_eth_ioctl = phy_do_ioctl_running,
4898 .ndo_set_features = fec_set_features,
4899 .ndo_bpf = fec_enet_bpf,
4900 .ndo_xdp_xmit = fec_enet_xdp_xmit,
4901 .ndo_xsk_wakeup = fec_enet_xsk_wakeup,
4902 .ndo_hwtstamp_get = fec_hwtstamp_get,
4903 .ndo_hwtstamp_set = fec_hwtstamp_set,
4904 };
4905
4906 static const unsigned short offset_des_active_rxq[] = {
4907 FEC_R_DES_ACTIVE_0, FEC_R_DES_ACTIVE_1, FEC_R_DES_ACTIVE_2
4908 };
4909
4910 static const unsigned short offset_des_active_txq[] = {
4911 FEC_X_DES_ACTIVE_0, FEC_X_DES_ACTIVE_1, FEC_X_DES_ACTIVE_2
4912 };
4913
4914 /*
4915 * XXX: We need to clean up on failure exits here.
4916 *
4917 */
fec_enet_init(struct net_device * ndev)4918 static int fec_enet_init(struct net_device *ndev)
4919 {
4920 struct fec_enet_private *fep = netdev_priv(ndev);
4921 struct bufdesc *cbd_base;
4922 dma_addr_t bd_dma;
4923 int bd_size;
4924 unsigned int i;
4925 unsigned dsize = fep->bufdesc_ex ? sizeof(struct bufdesc_ex) :
4926 sizeof(struct bufdesc);
4927 unsigned dsize_log2 = __fls(dsize);
4928 int ret;
4929
4930 WARN_ON(dsize != (1 << dsize_log2));
4931 #if defined(CONFIG_ARM) || defined(CONFIG_ARM64)
4932 fep->tx_align = 0xf;
4933 #else
4934 fep->tx_align = 0x3;
4935 #endif
4936 fep->rx_pkts_itr = FEC_ITR_ICFT_DEFAULT;
4937 fep->tx_pkts_itr = FEC_ITR_ICFT_DEFAULT;
4938 fep->rx_time_itr = FEC_ITR_ICTT_DEFAULT;
4939 fep->tx_time_itr = FEC_ITR_ICTT_DEFAULT;
4940
4941 /* Check mask of the streaming and coherent API */
4942 ret = dma_set_mask_and_coherent(&fep->pdev->dev, DMA_BIT_MASK(32));
4943 if (ret < 0) {
4944 dev_warn(&fep->pdev->dev, "No suitable DMA available\n");
4945 return ret;
4946 }
4947
4948 ret = fec_enet_alloc_queue(ndev);
4949 if (ret)
4950 return ret;
4951
4952 bd_size = (fep->total_tx_ring_size + fep->total_rx_ring_size) * dsize;
4953
4954 /* Allocate memory for buffer descriptors. */
4955 cbd_base = fec_dmam_alloc(&fep->pdev->dev, bd_size, &bd_dma,
4956 GFP_KERNEL);
4957 if (!cbd_base) {
4958 ret = -ENOMEM;
4959 goto free_queue_mem;
4960 }
4961
4962 /* Get the Ethernet address */
4963 ret = fec_get_mac(ndev);
4964 if (ret)
4965 goto free_queue_mem;
4966
4967 /* Set receive and transmit descriptor base. */
4968 for (i = 0; i < fep->num_rx_queues; i++) {
4969 struct fec_enet_priv_rx_q *rxq = fep->rx_queue[i];
4970 unsigned size = dsize * rxq->bd.ring_size;
4971
4972 rxq->bd.qid = i;
4973 rxq->bd.base = cbd_base;
4974 rxq->bd.cur = cbd_base;
4975 rxq->bd.dma = bd_dma;
4976 rxq->bd.dsize = dsize;
4977 rxq->bd.dsize_log2 = dsize_log2;
4978 rxq->bd.reg_desc_active = fep->hwp + offset_des_active_rxq[i];
4979 bd_dma += size;
4980 cbd_base = (struct bufdesc *)(((void *)cbd_base) + size);
4981 rxq->bd.last = (struct bufdesc *)(((void *)cbd_base) - dsize);
4982 }
4983
4984 for (i = 0; i < fep->num_tx_queues; i++) {
4985 struct fec_enet_priv_tx_q *txq = fep->tx_queue[i];
4986 unsigned size = dsize * txq->bd.ring_size;
4987
4988 txq->bd.qid = i;
4989 txq->bd.base = cbd_base;
4990 txq->bd.cur = cbd_base;
4991 txq->bd.dma = bd_dma;
4992 txq->bd.dsize = dsize;
4993 txq->bd.dsize_log2 = dsize_log2;
4994 txq->bd.reg_desc_active = fep->hwp + offset_des_active_txq[i];
4995 bd_dma += size;
4996 cbd_base = (struct bufdesc *)(((void *)cbd_base) + size);
4997 txq->bd.last = (struct bufdesc *)(((void *)cbd_base) - dsize);
4998 }
4999
5000
5001 /* The FEC Ethernet specific entries in the device structure */
5002 ndev->watchdog_timeo = TX_TIMEOUT;
5003 ndev->netdev_ops = &fec_netdev_ops;
5004 ndev->ethtool_ops = &fec_enet_ethtool_ops;
5005
5006 writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
5007 netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi);
5008
5009 if (fep->quirks & FEC_QUIRK_HAS_VLAN)
5010 /* enable hw VLAN support */
5011 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
5012
5013 if (fep->quirks & FEC_QUIRK_HAS_CSUM) {
5014 netif_set_tso_max_segs(ndev, FEC_MAX_TSO_SEGS);
5015
5016 /* enable hw accelerator */
5017 ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
5018 | NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO);
5019 fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
5020 }
5021
5022 if (fep->quirks & FEC_QUIRK_HAS_MULTI_QUEUES)
5023 fep->tx_align = 0;
5024
5025 ndev->hw_features = ndev->features;
5026
5027 if (!(fep->quirks & FEC_QUIRK_SWAP_FRAME))
5028 ndev->xdp_features = NETDEV_XDP_ACT_BASIC |
5029 NETDEV_XDP_ACT_REDIRECT |
5030 NETDEV_XDP_ACT_XSK_ZEROCOPY;
5031
5032 fec_restart(ndev);
5033
5034 if (fep->quirks & FEC_QUIRK_MIB_CLEAR)
5035 fec_enet_clear_ethtool_stats(ndev);
5036 else
5037 fec_enet_update_ethtool_stats(ndev);
5038
5039 return 0;
5040
5041 free_queue_mem:
5042 fec_enet_free_queue(ndev);
5043 return ret;
5044 }
5045
fec_enet_deinit(struct net_device * ndev)5046 static void fec_enet_deinit(struct net_device *ndev)
5047 {
5048 struct fec_enet_private *fep = netdev_priv(ndev);
5049
5050 netif_napi_del(&fep->napi);
5051 fec_enet_free_queue(ndev);
5052 }
5053
5054 #ifdef CONFIG_OF
fec_reset_phy(struct platform_device * pdev)5055 static int fec_reset_phy(struct platform_device *pdev)
5056 {
5057 struct gpio_desc *phy_reset;
5058 int msec = 1, phy_post_delay = 0;
5059 struct device_node *np = pdev->dev.of_node;
5060 int err;
5061
5062 if (!np)
5063 return 0;
5064
5065 err = of_property_read_u32(np, "phy-reset-duration", &msec);
5066 /* A sane reset duration should not be longer than 1s */
5067 if (!err && msec > 1000)
5068 msec = 1;
5069
5070 err = of_property_read_u32(np, "phy-reset-post-delay", &phy_post_delay);
5071 /* valid reset duration should be less than 1s */
5072 if (!err && phy_post_delay > 1000)
5073 return -EINVAL;
5074
5075 phy_reset = devm_gpiod_get_optional(&pdev->dev, "phy-reset",
5076 GPIOD_OUT_HIGH);
5077 if (IS_ERR(phy_reset))
5078 return dev_err_probe(&pdev->dev, PTR_ERR(phy_reset),
5079 "failed to get phy-reset-gpios\n");
5080
5081 if (!phy_reset)
5082 return 0;
5083
5084 if (msec > 20)
5085 msleep(msec);
5086 else
5087 usleep_range(msec * 1000, msec * 1000 + 1000);
5088
5089 gpiod_set_value_cansleep(phy_reset, 0);
5090
5091 if (!phy_post_delay)
5092 return 0;
5093
5094 if (phy_post_delay > 20)
5095 msleep(phy_post_delay);
5096 else
5097 usleep_range(phy_post_delay * 1000,
5098 phy_post_delay * 1000 + 1000);
5099
5100 return 0;
5101 }
5102 #else /* CONFIG_OF */
fec_reset_phy(struct platform_device * pdev)5103 static int fec_reset_phy(struct platform_device *pdev)
5104 {
5105 /*
5106 * In case of platform probe, the reset has been done
5107 * by machine code.
5108 */
5109 return 0;
5110 }
5111 #endif /* CONFIG_OF */
5112
5113 static void
fec_enet_get_queue_num(struct platform_device * pdev,int * num_tx,int * num_rx)5114 fec_enet_get_queue_num(struct platform_device *pdev, int *num_tx, int *num_rx)
5115 {
5116 struct device_node *np = pdev->dev.of_node;
5117
5118 *num_tx = *num_rx = 1;
5119
5120 if (!np || !of_device_is_available(np))
5121 return;
5122
5123 /* parse the num of tx and rx queues */
5124 of_property_read_u32(np, "fsl,num-tx-queues", num_tx);
5125
5126 of_property_read_u32(np, "fsl,num-rx-queues", num_rx);
5127
5128 if (*num_tx < 1 || *num_tx > FEC_ENET_MAX_TX_QS) {
5129 dev_warn(&pdev->dev, "Invalid num_tx(=%d), fall back to 1\n",
5130 *num_tx);
5131 *num_tx = 1;
5132 return;
5133 }
5134
5135 if (*num_rx < 1 || *num_rx > FEC_ENET_MAX_RX_QS) {
5136 dev_warn(&pdev->dev, "Invalid num_rx(=%d), fall back to 1\n",
5137 *num_rx);
5138 *num_rx = 1;
5139 return;
5140 }
5141
5142 }
5143
fec_enet_get_irq_cnt(struct platform_device * pdev)5144 static int fec_enet_get_irq_cnt(struct platform_device *pdev)
5145 {
5146 int irq_cnt = platform_irq_count(pdev);
5147
5148 if (irq_cnt > FEC_IRQ_NUM)
5149 irq_cnt = FEC_IRQ_NUM; /* last for pps */
5150 else if (irq_cnt == 2)
5151 irq_cnt = 1; /* last for pps */
5152 else if (irq_cnt <= 0)
5153 irq_cnt = 1; /* At least 1 irq is needed */
5154 return irq_cnt;
5155 }
5156
fec_enet_get_wakeup_irq(struct platform_device * pdev)5157 static void fec_enet_get_wakeup_irq(struct platform_device *pdev)
5158 {
5159 struct net_device *ndev = platform_get_drvdata(pdev);
5160 struct fec_enet_private *fep = netdev_priv(ndev);
5161
5162 if (fep->quirks & FEC_QUIRK_WAKEUP_FROM_INT2)
5163 fep->wake_irq = fep->irq[2];
5164 else
5165 fep->wake_irq = fep->irq[0];
5166 }
5167
fec_enet_init_stop_mode(struct fec_enet_private * fep,struct device_node * np)5168 static int fec_enet_init_stop_mode(struct fec_enet_private *fep,
5169 struct device_node *np)
5170 {
5171 struct device_node *gpr_np;
5172 u32 out_val[3];
5173 int ret = 0;
5174
5175 gpr_np = of_parse_phandle(np, "fsl,stop-mode", 0);
5176 if (!gpr_np)
5177 return 0;
5178
5179 ret = of_property_read_u32_array(np, "fsl,stop-mode", out_val,
5180 ARRAY_SIZE(out_val));
5181 if (ret) {
5182 dev_dbg(&fep->pdev->dev, "no stop mode property\n");
5183 goto out;
5184 }
5185
5186 fep->stop_gpr.gpr = syscon_node_to_regmap(gpr_np);
5187 if (IS_ERR(fep->stop_gpr.gpr)) {
5188 dev_err(&fep->pdev->dev, "could not find gpr regmap\n");
5189 ret = PTR_ERR(fep->stop_gpr.gpr);
5190 fep->stop_gpr.gpr = NULL;
5191 goto out;
5192 }
5193
5194 fep->stop_gpr.reg = out_val[1];
5195 fep->stop_gpr.bit = out_val[2];
5196
5197 out:
5198 of_node_put(gpr_np);
5199
5200 return ret;
5201 }
5202
5203 static int
fec_probe(struct platform_device * pdev)5204 fec_probe(struct platform_device *pdev)
5205 {
5206 struct fec_enet_private *fep;
5207 struct fec_platform_data *pdata;
5208 phy_interface_t interface;
5209 struct net_device *ndev;
5210 int i, irq, ret = 0;
5211 static int dev_id;
5212 struct device_node *np = pdev->dev.of_node, *phy_node;
5213 int num_tx_qs;
5214 int num_rx_qs;
5215 char irq_name[8];
5216 int irq_cnt;
5217 const struct fec_devinfo *dev_info;
5218
5219 fec_enet_get_queue_num(pdev, &num_tx_qs, &num_rx_qs);
5220
5221 /* Init network device */
5222 ndev = alloc_etherdev_mqs(sizeof(struct fec_enet_private) +
5223 FEC_STATS_SIZE, num_tx_qs, num_rx_qs);
5224 if (!ndev)
5225 return -ENOMEM;
5226
5227 SET_NETDEV_DEV(ndev, &pdev->dev);
5228
5229 /* setup board info structure */
5230 fep = netdev_priv(ndev);
5231
5232 dev_info = device_get_match_data(&pdev->dev);
5233 if (!dev_info)
5234 dev_info = (const struct fec_devinfo *)pdev->id_entry->driver_data;
5235 if (dev_info)
5236 fep->quirks = dev_info->quirks;
5237
5238 fep->netdev = ndev;
5239 fep->num_rx_queues = num_rx_qs;
5240 fep->num_tx_queues = num_tx_qs;
5241
5242 /* default enable pause frame auto negotiation */
5243 if (fep->quirks & FEC_QUIRK_HAS_GBIT)
5244 fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
5245
5246 /* Select default pin state */
5247 pinctrl_pm_select_default_state(&pdev->dev);
5248
5249 fep->hwp = devm_platform_ioremap_resource(pdev, 0);
5250 if (IS_ERR(fep->hwp)) {
5251 ret = PTR_ERR(fep->hwp);
5252 goto failed_ioremap;
5253 }
5254
5255 fep->pdev = pdev;
5256 fep->dev_id = dev_id++;
5257
5258 platform_set_drvdata(pdev, ndev);
5259
5260 if ((of_machine_is_compatible("fsl,imx6q") ||
5261 of_machine_is_compatible("fsl,imx6dl")) &&
5262 !of_property_read_bool(np, "fsl,err006687-workaround-present"))
5263 fep->quirks |= FEC_QUIRK_ERR006687;
5264
5265 ret = fec_enet_ipc_handle_init(fep);
5266 if (ret)
5267 goto failed_ipc_init;
5268
5269 if (of_property_read_bool(np, "fsl,magic-packet"))
5270 fep->wol_flag |= FEC_WOL_HAS_MAGIC_PACKET;
5271
5272 ret = fec_enet_init_stop_mode(fep, np);
5273 if (ret)
5274 goto failed_stop_mode;
5275
5276 phy_node = of_parse_phandle(np, "phy-handle", 0);
5277 if (!phy_node && of_phy_is_fixed_link(np)) {
5278 ret = of_phy_register_fixed_link(np);
5279 if (ret < 0) {
5280 dev_err(&pdev->dev,
5281 "broken fixed-link specification\n");
5282 goto failed_phy;
5283 }
5284 phy_node = of_node_get(np);
5285 }
5286 fep->phy_node = phy_node;
5287
5288 ret = of_get_phy_mode(pdev->dev.of_node, &interface);
5289 if (ret) {
5290 pdata = dev_get_platdata(&pdev->dev);
5291 if (pdata)
5292 fep->phy_interface = pdata->phy;
5293 else
5294 fep->phy_interface = PHY_INTERFACE_MODE_MII;
5295 } else {
5296 fep->phy_interface = interface;
5297 }
5298
5299 ret = fec_enet_parse_rgmii_delay(fep, np);
5300 if (ret)
5301 goto failed_rgmii_delay;
5302
5303 fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
5304 if (IS_ERR(fep->clk_ipg)) {
5305 ret = PTR_ERR(fep->clk_ipg);
5306 goto failed_clk;
5307 }
5308
5309 fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
5310 if (IS_ERR(fep->clk_ahb)) {
5311 ret = PTR_ERR(fep->clk_ahb);
5312 goto failed_clk;
5313 }
5314
5315 fep->itr_clk_rate = clk_get_rate(fep->clk_ahb);
5316
5317 /* enet_out is optional, depends on board */
5318 fep->clk_enet_out = devm_clk_get_optional(&pdev->dev, "enet_out");
5319 if (IS_ERR(fep->clk_enet_out)) {
5320 ret = PTR_ERR(fep->clk_enet_out);
5321 goto failed_clk;
5322 }
5323
5324 fep->ptp_clk_on = false;
5325 mutex_init(&fep->ptp_clk_mutex);
5326
5327 /* clk_ref is optional, depends on board */
5328 fep->clk_ref = devm_clk_get_optional(&pdev->dev, "enet_clk_ref");
5329 if (IS_ERR(fep->clk_ref)) {
5330 ret = PTR_ERR(fep->clk_ref);
5331 goto failed_clk;
5332 }
5333 fep->clk_ref_rate = clk_get_rate(fep->clk_ref);
5334
5335 /* clk_2x_txclk is optional, depends on board */
5336 if (fep->rgmii_txc_dly || fep->rgmii_rxc_dly) {
5337 fep->clk_2x_txclk = devm_clk_get(&pdev->dev, "enet_2x_txclk");
5338 if (IS_ERR(fep->clk_2x_txclk))
5339 fep->clk_2x_txclk = NULL;
5340 }
5341
5342 fep->bufdesc_ex = fep->quirks & FEC_QUIRK_HAS_BUFDESC_EX;
5343 fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
5344 if (IS_ERR(fep->clk_ptp)) {
5345 fep->clk_ptp = NULL;
5346 fep->bufdesc_ex = false;
5347 }
5348
5349 ret = fec_enet_clk_enable(ndev, true);
5350 if (ret)
5351 goto failed_clk;
5352
5353 ret = clk_prepare_enable(fep->clk_ipg);
5354 if (ret)
5355 goto failed_clk_ipg;
5356 ret = clk_prepare_enable(fep->clk_ahb);
5357 if (ret)
5358 goto failed_clk_ahb;
5359
5360 fep->reg_phy = devm_regulator_get_optional(&pdev->dev, "phy");
5361 if (!IS_ERR(fep->reg_phy)) {
5362 ret = regulator_enable(fep->reg_phy);
5363 if (ret) {
5364 dev_err(&pdev->dev,
5365 "Failed to enable phy regulator: %d\n", ret);
5366 goto failed_regulator;
5367 }
5368 } else {
5369 if (PTR_ERR(fep->reg_phy) == -EPROBE_DEFER) {
5370 ret = -EPROBE_DEFER;
5371 goto failed_regulator;
5372 }
5373 fep->reg_phy = NULL;
5374 }
5375
5376 pm_runtime_set_autosuspend_delay(&pdev->dev, FEC_MDIO_PM_TIMEOUT);
5377 pm_runtime_use_autosuspend(&pdev->dev);
5378 pm_runtime_get_noresume(&pdev->dev);
5379 pm_runtime_set_active(&pdev->dev);
5380 pm_runtime_enable(&pdev->dev);
5381
5382 ret = fec_reset_phy(pdev);
5383 if (ret)
5384 goto failed_reset;
5385
5386 irq_cnt = fec_enet_get_irq_cnt(pdev);
5387 if (fep->bufdesc_ex)
5388 fec_ptp_init(pdev, irq_cnt);
5389
5390 ret = fec_enet_init(ndev);
5391 if (ret)
5392 goto failed_init;
5393
5394 for (i = 0; i < irq_cnt; i++) {
5395 snprintf(irq_name, sizeof(irq_name), "int%d", i);
5396 irq = platform_get_irq_byname_optional(pdev, irq_name);
5397 if (irq < 0)
5398 irq = platform_get_irq(pdev, i);
5399 if (irq < 0) {
5400 ret = irq;
5401 goto failed_irq;
5402 }
5403 ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt,
5404 0, pdev->name, ndev);
5405 if (ret)
5406 goto failed_irq;
5407
5408 fep->irq[i] = irq;
5409 }
5410
5411 /* Decide which interrupt line is wakeup capable */
5412 fec_enet_get_wakeup_irq(pdev);
5413
5414 ret = fec_enet_mii_init(pdev);
5415 if (ret)
5416 goto failed_mii_init;
5417
5418 /* Carrier starts down, phylib will bring it up */
5419 netif_carrier_off(ndev);
5420 fec_enet_clk_enable(ndev, false);
5421 pinctrl_pm_select_sleep_state(&pdev->dev);
5422
5423 fep->pagepool_order = 0;
5424 fep->rx_frame_size = FEC_ENET_RX_FRSIZE;
5425
5426 if (fep->quirks & FEC_QUIRK_JUMBO_FRAME)
5427 fep->max_buf_size = MAX_JUMBO_BUF_SIZE;
5428 else
5429 fep->max_buf_size = PKT_MAXBUF_SIZE;
5430
5431 ndev->max_mtu = fep->max_buf_size - VLAN_ETH_HLEN - ETH_FCS_LEN;
5432
5433 if (fep->quirks & FEC_QUIRK_HAS_RACC)
5434 fep->rx_shift = 2;
5435 else
5436 fep->rx_shift = 0;
5437
5438 ret = register_netdev(ndev);
5439 if (ret)
5440 goto failed_register;
5441
5442 device_init_wakeup(&ndev->dev, fep->wol_flag &
5443 FEC_WOL_HAS_MAGIC_PACKET);
5444
5445 if (fep->bufdesc_ex && fep->ptp_clock)
5446 netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
5447
5448 INIT_WORK(&fep->tx_timeout_work, fec_enet_timeout_work);
5449
5450 pm_runtime_put_autosuspend(&pdev->dev);
5451
5452 return 0;
5453
5454 failed_register:
5455 fec_enet_mii_remove(fep);
5456 failed_mii_init:
5457 failed_irq:
5458 fec_enet_deinit(ndev);
5459 failed_init:
5460 fec_ptp_stop(pdev);
5461 failed_reset:
5462 pm_runtime_put_noidle(&pdev->dev);
5463 pm_runtime_disable(&pdev->dev);
5464 if (fep->reg_phy)
5465 regulator_disable(fep->reg_phy);
5466 failed_regulator:
5467 clk_disable_unprepare(fep->clk_ahb);
5468 failed_clk_ahb:
5469 clk_disable_unprepare(fep->clk_ipg);
5470 failed_clk_ipg:
5471 fec_enet_clk_enable(ndev, false);
5472 failed_clk:
5473 failed_rgmii_delay:
5474 if (of_phy_is_fixed_link(np))
5475 of_phy_deregister_fixed_link(np);
5476 of_node_put(phy_node);
5477 failed_stop_mode:
5478 failed_ipc_init:
5479 failed_phy:
5480 dev_id--;
5481 failed_ioremap:
5482 free_netdev(ndev);
5483
5484 return ret;
5485 }
5486
5487 static void
fec_drv_remove(struct platform_device * pdev)5488 fec_drv_remove(struct platform_device *pdev)
5489 {
5490 struct net_device *ndev = platform_get_drvdata(pdev);
5491 struct fec_enet_private *fep = netdev_priv(ndev);
5492 struct device_node *np = pdev->dev.of_node;
5493 int ret;
5494
5495 ret = pm_runtime_get_sync(&pdev->dev);
5496 if (ret < 0)
5497 dev_err(&pdev->dev,
5498 "Failed to resume device in remove callback (%pe)\n",
5499 ERR_PTR(ret));
5500
5501 cancel_work_sync(&fep->tx_timeout_work);
5502 fec_ptp_stop(pdev);
5503 unregister_netdev(ndev);
5504 fec_enet_mii_remove(fep);
5505 if (fep->reg_phy)
5506 regulator_disable(fep->reg_phy);
5507
5508 if (of_phy_is_fixed_link(np))
5509 of_phy_deregister_fixed_link(np);
5510 of_node_put(fep->phy_node);
5511
5512 /* After pm_runtime_get_sync() failed, the clks are still off, so skip
5513 * disabling them again.
5514 */
5515 if (ret >= 0) {
5516 clk_disable_unprepare(fep->clk_ahb);
5517 clk_disable_unprepare(fep->clk_ipg);
5518 }
5519 pm_runtime_put_noidle(&pdev->dev);
5520 pm_runtime_disable(&pdev->dev);
5521
5522 fec_enet_deinit(ndev);
5523 free_netdev(ndev);
5524 }
5525
fec_suspend(struct device * dev)5526 static int fec_suspend(struct device *dev)
5527 {
5528 struct net_device *ndev = dev_get_drvdata(dev);
5529 struct fec_enet_private *fep = netdev_priv(ndev);
5530 int ret;
5531
5532 rtnl_lock();
5533 if (netif_running(ndev)) {
5534 if (fep->wol_flag & FEC_WOL_FLAG_ENABLE)
5535 fep->wol_flag |= FEC_WOL_FLAG_SLEEP_ON;
5536 phy_stop(ndev->phydev);
5537 napi_disable(&fep->napi);
5538 netif_tx_lock_bh(ndev);
5539 netif_device_detach(ndev);
5540 netif_tx_unlock_bh(ndev);
5541 fec_stop(ndev);
5542 if (!(fep->wol_flag & FEC_WOL_FLAG_ENABLE)) {
5543 fec_irqs_disable(ndev);
5544 pinctrl_pm_select_sleep_state(&fep->pdev->dev);
5545 } else {
5546 fec_irqs_disable_except_wakeup(ndev);
5547 if (fep->wake_irq > 0) {
5548 disable_irq(fep->wake_irq);
5549 enable_irq_wake(fep->wake_irq);
5550 }
5551 fec_enet_stop_mode(fep, true);
5552 }
5553 /* It's safe to disable clocks since interrupts are masked */
5554 fec_enet_clk_enable(ndev, false);
5555
5556 fep->rpm_active = !pm_runtime_status_suspended(dev);
5557 if (fep->rpm_active) {
5558 ret = pm_runtime_force_suspend(dev);
5559 if (ret < 0) {
5560 rtnl_unlock();
5561 return ret;
5562 }
5563 }
5564 }
5565 rtnl_unlock();
5566
5567 if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE))
5568 regulator_disable(fep->reg_phy);
5569
5570 /* SOC supply clock to phy, when clock is disabled, phy link down
5571 * SOC control phy regulator, when regulator is disabled, phy link down
5572 */
5573 if (fep->clk_enet_out || fep->reg_phy)
5574 fep->link = 0;
5575
5576 return 0;
5577 }
5578
fec_resume(struct device * dev)5579 static int fec_resume(struct device *dev)
5580 {
5581 struct net_device *ndev = dev_get_drvdata(dev);
5582 struct fec_enet_private *fep = netdev_priv(ndev);
5583 int ret;
5584 int val;
5585
5586 if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE)) {
5587 ret = regulator_enable(fep->reg_phy);
5588 if (ret)
5589 return ret;
5590 }
5591
5592 rtnl_lock();
5593 if (netif_running(ndev)) {
5594 if (fep->rpm_active)
5595 pm_runtime_force_resume(dev);
5596
5597 ret = fec_enet_clk_enable(ndev, true);
5598 if (ret) {
5599 rtnl_unlock();
5600 goto failed_clk;
5601 }
5602 if (fep->wol_flag & FEC_WOL_FLAG_ENABLE) {
5603 fec_enet_stop_mode(fep, false);
5604 if (fep->wake_irq) {
5605 disable_irq_wake(fep->wake_irq);
5606 enable_irq(fep->wake_irq);
5607 }
5608
5609 val = readl(fep->hwp + FEC_ECNTRL);
5610 val &= ~(FEC_ECR_MAGICEN | FEC_ECR_SLEEP);
5611 writel(val, fep->hwp + FEC_ECNTRL);
5612 fep->wol_flag &= ~FEC_WOL_FLAG_SLEEP_ON;
5613 } else {
5614 pinctrl_pm_select_default_state(&fep->pdev->dev);
5615 }
5616 fec_restart(ndev);
5617 netif_tx_lock_bh(ndev);
5618 netif_device_attach(ndev);
5619 netif_tx_unlock_bh(ndev);
5620 napi_enable(&fep->napi);
5621 phy_init_hw(ndev->phydev);
5622 phy_start(ndev->phydev);
5623 }
5624 rtnl_unlock();
5625
5626 return 0;
5627
5628 failed_clk:
5629 if (fep->reg_phy)
5630 regulator_disable(fep->reg_phy);
5631 return ret;
5632 }
5633
fec_runtime_suspend(struct device * dev)5634 static int fec_runtime_suspend(struct device *dev)
5635 {
5636 struct net_device *ndev = dev_get_drvdata(dev);
5637 struct fec_enet_private *fep = netdev_priv(ndev);
5638
5639 clk_disable_unprepare(fep->clk_ahb);
5640 clk_disable_unprepare(fep->clk_ipg);
5641
5642 return 0;
5643 }
5644
fec_runtime_resume(struct device * dev)5645 static int fec_runtime_resume(struct device *dev)
5646 {
5647 struct net_device *ndev = dev_get_drvdata(dev);
5648 struct fec_enet_private *fep = netdev_priv(ndev);
5649 int ret;
5650
5651 ret = clk_prepare_enable(fep->clk_ahb);
5652 if (ret)
5653 return ret;
5654 ret = clk_prepare_enable(fep->clk_ipg);
5655 if (ret)
5656 goto failed_clk_ipg;
5657
5658 return 0;
5659
5660 failed_clk_ipg:
5661 clk_disable_unprepare(fep->clk_ahb);
5662 return ret;
5663 }
5664
5665 static const struct dev_pm_ops fec_pm_ops = {
5666 SYSTEM_SLEEP_PM_OPS(fec_suspend, fec_resume)
5667 RUNTIME_PM_OPS(fec_runtime_suspend, fec_runtime_resume, NULL)
5668 };
5669
5670 static struct platform_driver fec_driver = {
5671 .driver = {
5672 .name = DRIVER_NAME,
5673 .pm = pm_ptr(&fec_pm_ops),
5674 .of_match_table = fec_dt_ids,
5675 .suppress_bind_attrs = true,
5676 },
5677 .id_table = fec_devtype,
5678 .probe = fec_probe,
5679 .remove = fec_drv_remove,
5680 };
5681
5682 module_platform_driver(fec_driver);
5683
5684 MODULE_DESCRIPTION("NXP Fast Ethernet Controller (FEC) driver");
5685 MODULE_LICENSE("GPL");
5686