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