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