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