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