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 #ifdef CONFIG_PAGE_POOL_STATS
2793 	struct page_pool_stats stats = {};
2794 	struct fec_enet_priv_rx_q *rxq;
2795 	int i;
2796 
2797 	for (i = fep->num_rx_queues - 1; i >= 0; i--) {
2798 		rxq = fep->rx_queue[i];
2799 
2800 		if (!rxq->page_pool)
2801 			continue;
2802 
2803 		page_pool_get_stats(rxq->page_pool, &stats);
2804 	}
2805 
2806 	page_pool_ethtool_stats_get(data, &stats);
2807 #endif
2808 }
2809 
2810 static void fec_enet_get_ethtool_stats(struct net_device *dev,
2811 				       struct ethtool_stats *stats, u64 *data)
2812 {
2813 	struct fec_enet_private *fep = netdev_priv(dev);
2814 
2815 	if (netif_running(dev))
2816 		fec_enet_update_ethtool_stats(dev);
2817 
2818 	memcpy(data, fep->ethtool_stats, FEC_STATS_SIZE);
2819 	data += FEC_STATS_SIZE / sizeof(u64);
2820 
2821 	fec_enet_get_xdp_stats(fep, data);
2822 	data += XDP_STATS_TOTAL;
2823 
2824 	fec_enet_page_pool_stats(fep, data);
2825 }
2826 
2827 static void fec_enet_get_strings(struct net_device *netdev,
2828 	u32 stringset, u8 *data)
2829 {
2830 	int i;
2831 	switch (stringset) {
2832 	case ETH_SS_STATS:
2833 		for (i = 0; i < ARRAY_SIZE(fec_stats); i++) {
2834 			memcpy(data, fec_stats[i].name, ETH_GSTRING_LEN);
2835 			data += ETH_GSTRING_LEN;
2836 		}
2837 		for (i = 0; i < ARRAY_SIZE(fec_xdp_stat_strs); i++) {
2838 			strncpy(data, fec_xdp_stat_strs[i], ETH_GSTRING_LEN);
2839 			data += ETH_GSTRING_LEN;
2840 		}
2841 		page_pool_ethtool_stats_get_strings(data);
2842 
2843 		break;
2844 	case ETH_SS_TEST:
2845 		net_selftest_get_strings(data);
2846 		break;
2847 	}
2848 }
2849 
2850 static int fec_enet_get_sset_count(struct net_device *dev, int sset)
2851 {
2852 	int count;
2853 
2854 	switch (sset) {
2855 	case ETH_SS_STATS:
2856 		count = ARRAY_SIZE(fec_stats) + XDP_STATS_TOTAL;
2857 		count += page_pool_ethtool_stats_get_count();
2858 		return count;
2859 
2860 	case ETH_SS_TEST:
2861 		return net_selftest_get_count();
2862 	default:
2863 		return -EOPNOTSUPP;
2864 	}
2865 }
2866 
2867 static void fec_enet_clear_ethtool_stats(struct net_device *dev)
2868 {
2869 	struct fec_enet_private *fep = netdev_priv(dev);
2870 	struct fec_enet_priv_rx_q *rxq;
2871 	int i, j;
2872 
2873 	/* Disable MIB statistics counters */
2874 	writel(FEC_MIB_CTRLSTAT_DISABLE, fep->hwp + FEC_MIB_CTRLSTAT);
2875 
2876 	for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
2877 		writel(0, fep->hwp + fec_stats[i].offset);
2878 
2879 	for (i = fep->num_rx_queues - 1; i >= 0; i--) {
2880 		rxq = fep->rx_queue[i];
2881 		for (j = 0; j < XDP_STATS_TOTAL; j++)
2882 			rxq->stats[j] = 0;
2883 	}
2884 
2885 	/* Don't disable MIB statistics counters */
2886 	writel(0, fep->hwp + FEC_MIB_CTRLSTAT);
2887 }
2888 
2889 #else	/* !defined(CONFIG_M5272) */
2890 #define FEC_STATS_SIZE	0
2891 static inline void fec_enet_update_ethtool_stats(struct net_device *dev)
2892 {
2893 }
2894 
2895 static inline void fec_enet_clear_ethtool_stats(struct net_device *dev)
2896 {
2897 }
2898 #endif /* !defined(CONFIG_M5272) */
2899 
2900 /* ITR clock source is enet system clock (clk_ahb).
2901  * TCTT unit is cycle_ns * 64 cycle
2902  * So, the ICTT value = X us / (cycle_ns * 64)
2903  */
2904 static int fec_enet_us_to_itr_clock(struct net_device *ndev, int us)
2905 {
2906 	struct fec_enet_private *fep = netdev_priv(ndev);
2907 
2908 	return us * (fep->itr_clk_rate / 64000) / 1000;
2909 }
2910 
2911 /* Set threshold for interrupt coalescing */
2912 static void fec_enet_itr_coal_set(struct net_device *ndev)
2913 {
2914 	struct fec_enet_private *fep = netdev_priv(ndev);
2915 	int rx_itr, tx_itr;
2916 
2917 	/* Must be greater than zero to avoid unpredictable behavior */
2918 	if (!fep->rx_time_itr || !fep->rx_pkts_itr ||
2919 	    !fep->tx_time_itr || !fep->tx_pkts_itr)
2920 		return;
2921 
2922 	/* Select enet system clock as Interrupt Coalescing
2923 	 * timer Clock Source
2924 	 */
2925 	rx_itr = FEC_ITR_CLK_SEL;
2926 	tx_itr = FEC_ITR_CLK_SEL;
2927 
2928 	/* set ICFT and ICTT */
2929 	rx_itr |= FEC_ITR_ICFT(fep->rx_pkts_itr);
2930 	rx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr));
2931 	tx_itr |= FEC_ITR_ICFT(fep->tx_pkts_itr);
2932 	tx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr));
2933 
2934 	rx_itr |= FEC_ITR_EN;
2935 	tx_itr |= FEC_ITR_EN;
2936 
2937 	writel(tx_itr, fep->hwp + FEC_TXIC0);
2938 	writel(rx_itr, fep->hwp + FEC_RXIC0);
2939 	if (fep->quirks & FEC_QUIRK_HAS_MULTI_QUEUES) {
2940 		writel(tx_itr, fep->hwp + FEC_TXIC1);
2941 		writel(rx_itr, fep->hwp + FEC_RXIC1);
2942 		writel(tx_itr, fep->hwp + FEC_TXIC2);
2943 		writel(rx_itr, fep->hwp + FEC_RXIC2);
2944 	}
2945 }
2946 
2947 static int fec_enet_get_coalesce(struct net_device *ndev,
2948 				 struct ethtool_coalesce *ec,
2949 				 struct kernel_ethtool_coalesce *kernel_coal,
2950 				 struct netlink_ext_ack *extack)
2951 {
2952 	struct fec_enet_private *fep = netdev_priv(ndev);
2953 
2954 	if (!(fep->quirks & FEC_QUIRK_HAS_COALESCE))
2955 		return -EOPNOTSUPP;
2956 
2957 	ec->rx_coalesce_usecs = fep->rx_time_itr;
2958 	ec->rx_max_coalesced_frames = fep->rx_pkts_itr;
2959 
2960 	ec->tx_coalesce_usecs = fep->tx_time_itr;
2961 	ec->tx_max_coalesced_frames = fep->tx_pkts_itr;
2962 
2963 	return 0;
2964 }
2965 
2966 static int fec_enet_set_coalesce(struct net_device *ndev,
2967 				 struct ethtool_coalesce *ec,
2968 				 struct kernel_ethtool_coalesce *kernel_coal,
2969 				 struct netlink_ext_ack *extack)
2970 {
2971 	struct fec_enet_private *fep = netdev_priv(ndev);
2972 	struct device *dev = &fep->pdev->dev;
2973 	unsigned int cycle;
2974 
2975 	if (!(fep->quirks & FEC_QUIRK_HAS_COALESCE))
2976 		return -EOPNOTSUPP;
2977 
2978 	if (ec->rx_max_coalesced_frames > 255) {
2979 		dev_err(dev, "Rx coalesced frames exceed hardware limitation\n");
2980 		return -EINVAL;
2981 	}
2982 
2983 	if (ec->tx_max_coalesced_frames > 255) {
2984 		dev_err(dev, "Tx coalesced frame exceed hardware limitation\n");
2985 		return -EINVAL;
2986 	}
2987 
2988 	cycle = fec_enet_us_to_itr_clock(ndev, ec->rx_coalesce_usecs);
2989 	if (cycle > 0xFFFF) {
2990 		dev_err(dev, "Rx coalesced usec exceed hardware limitation\n");
2991 		return -EINVAL;
2992 	}
2993 
2994 	cycle = fec_enet_us_to_itr_clock(ndev, ec->tx_coalesce_usecs);
2995 	if (cycle > 0xFFFF) {
2996 		dev_err(dev, "Tx coalesced usec exceed hardware limitation\n");
2997 		return -EINVAL;
2998 	}
2999 
3000 	fep->rx_time_itr = ec->rx_coalesce_usecs;
3001 	fep->rx_pkts_itr = ec->rx_max_coalesced_frames;
3002 
3003 	fep->tx_time_itr = ec->tx_coalesce_usecs;
3004 	fep->tx_pkts_itr = ec->tx_max_coalesced_frames;
3005 
3006 	fec_enet_itr_coal_set(ndev);
3007 
3008 	return 0;
3009 }
3010 
3011 static int fec_enet_get_tunable(struct net_device *netdev,
3012 				const struct ethtool_tunable *tuna,
3013 				void *data)
3014 {
3015 	struct fec_enet_private *fep = netdev_priv(netdev);
3016 	int ret = 0;
3017 
3018 	switch (tuna->id) {
3019 	case ETHTOOL_RX_COPYBREAK:
3020 		*(u32 *)data = fep->rx_copybreak;
3021 		break;
3022 	default:
3023 		ret = -EINVAL;
3024 		break;
3025 	}
3026 
3027 	return ret;
3028 }
3029 
3030 static int fec_enet_set_tunable(struct net_device *netdev,
3031 				const struct ethtool_tunable *tuna,
3032 				const void *data)
3033 {
3034 	struct fec_enet_private *fep = netdev_priv(netdev);
3035 	int ret = 0;
3036 
3037 	switch (tuna->id) {
3038 	case ETHTOOL_RX_COPYBREAK:
3039 		fep->rx_copybreak = *(u32 *)data;
3040 		break;
3041 	default:
3042 		ret = -EINVAL;
3043 		break;
3044 	}
3045 
3046 	return ret;
3047 }
3048 
3049 /* LPI Sleep Ts count base on tx clk (clk_ref).
3050  * The lpi sleep cnt value = X us / (cycle_ns).
3051  */
3052 static int fec_enet_us_to_tx_cycle(struct net_device *ndev, int us)
3053 {
3054 	struct fec_enet_private *fep = netdev_priv(ndev);
3055 
3056 	return us * (fep->clk_ref_rate / 1000) / 1000;
3057 }
3058 
3059 static int fec_enet_eee_mode_set(struct net_device *ndev, bool enable)
3060 {
3061 	struct fec_enet_private *fep = netdev_priv(ndev);
3062 	struct ethtool_eee *p = &fep->eee;
3063 	unsigned int sleep_cycle, wake_cycle;
3064 	int ret = 0;
3065 
3066 	if (enable) {
3067 		ret = phy_init_eee(ndev->phydev, false);
3068 		if (ret)
3069 			return ret;
3070 
3071 		sleep_cycle = fec_enet_us_to_tx_cycle(ndev, p->tx_lpi_timer);
3072 		wake_cycle = sleep_cycle;
3073 	} else {
3074 		sleep_cycle = 0;
3075 		wake_cycle = 0;
3076 	}
3077 
3078 	p->tx_lpi_enabled = enable;
3079 	p->eee_enabled = enable;
3080 	p->eee_active = enable;
3081 
3082 	writel(sleep_cycle, fep->hwp + FEC_LPI_SLEEP);
3083 	writel(wake_cycle, fep->hwp + FEC_LPI_WAKE);
3084 
3085 	return 0;
3086 }
3087 
3088 static int
3089 fec_enet_get_eee(struct net_device *ndev, struct ethtool_eee *edata)
3090 {
3091 	struct fec_enet_private *fep = netdev_priv(ndev);
3092 	struct ethtool_eee *p = &fep->eee;
3093 
3094 	if (!(fep->quirks & FEC_QUIRK_HAS_EEE))
3095 		return -EOPNOTSUPP;
3096 
3097 	if (!netif_running(ndev))
3098 		return -ENETDOWN;
3099 
3100 	edata->eee_enabled = p->eee_enabled;
3101 	edata->eee_active = p->eee_active;
3102 	edata->tx_lpi_timer = p->tx_lpi_timer;
3103 	edata->tx_lpi_enabled = p->tx_lpi_enabled;
3104 
3105 	return phy_ethtool_get_eee(ndev->phydev, edata);
3106 }
3107 
3108 static int
3109 fec_enet_set_eee(struct net_device *ndev, struct ethtool_eee *edata)
3110 {
3111 	struct fec_enet_private *fep = netdev_priv(ndev);
3112 	struct ethtool_eee *p = &fep->eee;
3113 	int ret = 0;
3114 
3115 	if (!(fep->quirks & FEC_QUIRK_HAS_EEE))
3116 		return -EOPNOTSUPP;
3117 
3118 	if (!netif_running(ndev))
3119 		return -ENETDOWN;
3120 
3121 	p->tx_lpi_timer = edata->tx_lpi_timer;
3122 
3123 	if (!edata->eee_enabled || !edata->tx_lpi_enabled ||
3124 	    !edata->tx_lpi_timer)
3125 		ret = fec_enet_eee_mode_set(ndev, false);
3126 	else
3127 		ret = fec_enet_eee_mode_set(ndev, true);
3128 
3129 	if (ret)
3130 		return ret;
3131 
3132 	return phy_ethtool_set_eee(ndev->phydev, edata);
3133 }
3134 
3135 static void
3136 fec_enet_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
3137 {
3138 	struct fec_enet_private *fep = netdev_priv(ndev);
3139 
3140 	if (fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET) {
3141 		wol->supported = WAKE_MAGIC;
3142 		wol->wolopts = fep->wol_flag & FEC_WOL_FLAG_ENABLE ? WAKE_MAGIC : 0;
3143 	} else {
3144 		wol->supported = wol->wolopts = 0;
3145 	}
3146 }
3147 
3148 static int
3149 fec_enet_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
3150 {
3151 	struct fec_enet_private *fep = netdev_priv(ndev);
3152 
3153 	if (!(fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET))
3154 		return -EINVAL;
3155 
3156 	if (wol->wolopts & ~WAKE_MAGIC)
3157 		return -EINVAL;
3158 
3159 	device_set_wakeup_enable(&ndev->dev, wol->wolopts & WAKE_MAGIC);
3160 	if (device_may_wakeup(&ndev->dev))
3161 		fep->wol_flag |= FEC_WOL_FLAG_ENABLE;
3162 	else
3163 		fep->wol_flag &= (~FEC_WOL_FLAG_ENABLE);
3164 
3165 	return 0;
3166 }
3167 
3168 static const struct ethtool_ops fec_enet_ethtool_ops = {
3169 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
3170 				     ETHTOOL_COALESCE_MAX_FRAMES,
3171 	.get_drvinfo		= fec_enet_get_drvinfo,
3172 	.get_regs_len		= fec_enet_get_regs_len,
3173 	.get_regs		= fec_enet_get_regs,
3174 	.nway_reset		= phy_ethtool_nway_reset,
3175 	.get_link		= ethtool_op_get_link,
3176 	.get_coalesce		= fec_enet_get_coalesce,
3177 	.set_coalesce		= fec_enet_set_coalesce,
3178 #ifndef CONFIG_M5272
3179 	.get_pauseparam		= fec_enet_get_pauseparam,
3180 	.set_pauseparam		= fec_enet_set_pauseparam,
3181 	.get_strings		= fec_enet_get_strings,
3182 	.get_ethtool_stats	= fec_enet_get_ethtool_stats,
3183 	.get_sset_count		= fec_enet_get_sset_count,
3184 #endif
3185 	.get_ts_info		= fec_enet_get_ts_info,
3186 	.get_tunable		= fec_enet_get_tunable,
3187 	.set_tunable		= fec_enet_set_tunable,
3188 	.get_wol		= fec_enet_get_wol,
3189 	.set_wol		= fec_enet_set_wol,
3190 	.get_eee		= fec_enet_get_eee,
3191 	.set_eee		= fec_enet_set_eee,
3192 	.get_link_ksettings	= phy_ethtool_get_link_ksettings,
3193 	.set_link_ksettings	= phy_ethtool_set_link_ksettings,
3194 	.self_test		= net_selftest,
3195 };
3196 
3197 static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
3198 {
3199 	struct fec_enet_private *fep = netdev_priv(ndev);
3200 	struct phy_device *phydev = ndev->phydev;
3201 
3202 	if (!netif_running(ndev))
3203 		return -EINVAL;
3204 
3205 	if (!phydev)
3206 		return -ENODEV;
3207 
3208 	if (fep->bufdesc_ex) {
3209 		bool use_fec_hwts = !phy_has_hwtstamp(phydev);
3210 
3211 		if (cmd == SIOCSHWTSTAMP) {
3212 			if (use_fec_hwts)
3213 				return fec_ptp_set(ndev, rq);
3214 			fec_ptp_disable_hwts(ndev);
3215 		} else if (cmd == SIOCGHWTSTAMP) {
3216 			if (use_fec_hwts)
3217 				return fec_ptp_get(ndev, rq);
3218 		}
3219 	}
3220 
3221 	return phy_mii_ioctl(phydev, rq, cmd);
3222 }
3223 
3224 static void fec_enet_free_buffers(struct net_device *ndev)
3225 {
3226 	struct fec_enet_private *fep = netdev_priv(ndev);
3227 	unsigned int i;
3228 	struct sk_buff *skb;
3229 	struct fec_enet_priv_tx_q *txq;
3230 	struct fec_enet_priv_rx_q *rxq;
3231 	unsigned int q;
3232 
3233 	for (q = 0; q < fep->num_rx_queues; q++) {
3234 		rxq = fep->rx_queue[q];
3235 		for (i = 0; i < rxq->bd.ring_size; i++)
3236 			page_pool_put_full_page(rxq->page_pool, rxq->rx_skb_info[i].page, false);
3237 
3238 		for (i = 0; i < XDP_STATS_TOTAL; i++)
3239 			rxq->stats[i] = 0;
3240 
3241 		if (xdp_rxq_info_is_reg(&rxq->xdp_rxq))
3242 			xdp_rxq_info_unreg(&rxq->xdp_rxq);
3243 		page_pool_destroy(rxq->page_pool);
3244 		rxq->page_pool = NULL;
3245 	}
3246 
3247 	for (q = 0; q < fep->num_tx_queues; q++) {
3248 		txq = fep->tx_queue[q];
3249 		for (i = 0; i < txq->bd.ring_size; i++) {
3250 			kfree(txq->tx_bounce[i]);
3251 			txq->tx_bounce[i] = NULL;
3252 			skb = txq->tx_skbuff[i];
3253 			txq->tx_skbuff[i] = NULL;
3254 			dev_kfree_skb(skb);
3255 		}
3256 	}
3257 }
3258 
3259 static void fec_enet_free_queue(struct net_device *ndev)
3260 {
3261 	struct fec_enet_private *fep = netdev_priv(ndev);
3262 	int i;
3263 	struct fec_enet_priv_tx_q *txq;
3264 
3265 	for (i = 0; i < fep->num_tx_queues; i++)
3266 		if (fep->tx_queue[i] && fep->tx_queue[i]->tso_hdrs) {
3267 			txq = fep->tx_queue[i];
3268 			dma_free_coherent(&fep->pdev->dev,
3269 					  txq->bd.ring_size * TSO_HEADER_SIZE,
3270 					  txq->tso_hdrs,
3271 					  txq->tso_hdrs_dma);
3272 		}
3273 
3274 	for (i = 0; i < fep->num_rx_queues; i++)
3275 		kfree(fep->rx_queue[i]);
3276 	for (i = 0; i < fep->num_tx_queues; i++)
3277 		kfree(fep->tx_queue[i]);
3278 }
3279 
3280 static int fec_enet_alloc_queue(struct net_device *ndev)
3281 {
3282 	struct fec_enet_private *fep = netdev_priv(ndev);
3283 	int i;
3284 	int ret = 0;
3285 	struct fec_enet_priv_tx_q *txq;
3286 
3287 	for (i = 0; i < fep->num_tx_queues; i++) {
3288 		txq = kzalloc(sizeof(*txq), GFP_KERNEL);
3289 		if (!txq) {
3290 			ret = -ENOMEM;
3291 			goto alloc_failed;
3292 		}
3293 
3294 		fep->tx_queue[i] = txq;
3295 		txq->bd.ring_size = TX_RING_SIZE;
3296 		fep->total_tx_ring_size += fep->tx_queue[i]->bd.ring_size;
3297 
3298 		txq->tx_stop_threshold = FEC_MAX_SKB_DESCS;
3299 		txq->tx_wake_threshold =
3300 			(txq->bd.ring_size - txq->tx_stop_threshold) / 2;
3301 
3302 		txq->tso_hdrs = dma_alloc_coherent(&fep->pdev->dev,
3303 					txq->bd.ring_size * TSO_HEADER_SIZE,
3304 					&txq->tso_hdrs_dma,
3305 					GFP_KERNEL);
3306 		if (!txq->tso_hdrs) {
3307 			ret = -ENOMEM;
3308 			goto alloc_failed;
3309 		}
3310 	}
3311 
3312 	for (i = 0; i < fep->num_rx_queues; i++) {
3313 		fep->rx_queue[i] = kzalloc(sizeof(*fep->rx_queue[i]),
3314 					   GFP_KERNEL);
3315 		if (!fep->rx_queue[i]) {
3316 			ret = -ENOMEM;
3317 			goto alloc_failed;
3318 		}
3319 
3320 		fep->rx_queue[i]->bd.ring_size = RX_RING_SIZE;
3321 		fep->total_rx_ring_size += fep->rx_queue[i]->bd.ring_size;
3322 	}
3323 	return ret;
3324 
3325 alloc_failed:
3326 	fec_enet_free_queue(ndev);
3327 	return ret;
3328 }
3329 
3330 static int
3331 fec_enet_alloc_rxq_buffers(struct net_device *ndev, unsigned int queue)
3332 {
3333 	struct fec_enet_private *fep = netdev_priv(ndev);
3334 	struct fec_enet_priv_rx_q *rxq;
3335 	dma_addr_t phys_addr;
3336 	struct bufdesc	*bdp;
3337 	struct page *page;
3338 	int i, err;
3339 
3340 	rxq = fep->rx_queue[queue];
3341 	bdp = rxq->bd.base;
3342 
3343 	err = fec_enet_create_page_pool(fep, rxq, rxq->bd.ring_size);
3344 	if (err < 0) {
3345 		netdev_err(ndev, "%s failed queue %d (%d)\n", __func__, queue, err);
3346 		return err;
3347 	}
3348 
3349 	for (i = 0; i < rxq->bd.ring_size; i++) {
3350 		page = page_pool_dev_alloc_pages(rxq->page_pool);
3351 		if (!page)
3352 			goto err_alloc;
3353 
3354 		phys_addr = page_pool_get_dma_addr(page) + FEC_ENET_XDP_HEADROOM;
3355 		bdp->cbd_bufaddr = cpu_to_fec32(phys_addr);
3356 
3357 		rxq->rx_skb_info[i].page = page;
3358 		rxq->rx_skb_info[i].offset = FEC_ENET_XDP_HEADROOM;
3359 		bdp->cbd_sc = cpu_to_fec16(BD_ENET_RX_EMPTY);
3360 
3361 		if (fep->bufdesc_ex) {
3362 			struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
3363 			ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT);
3364 		}
3365 
3366 		bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
3367 	}
3368 
3369 	/* Set the last buffer to wrap. */
3370 	bdp = fec_enet_get_prevdesc(bdp, &rxq->bd);
3371 	bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
3372 	return 0;
3373 
3374  err_alloc:
3375 	fec_enet_free_buffers(ndev);
3376 	return -ENOMEM;
3377 }
3378 
3379 static int
3380 fec_enet_alloc_txq_buffers(struct net_device *ndev, unsigned int queue)
3381 {
3382 	struct fec_enet_private *fep = netdev_priv(ndev);
3383 	unsigned int i;
3384 	struct bufdesc  *bdp;
3385 	struct fec_enet_priv_tx_q *txq;
3386 
3387 	txq = fep->tx_queue[queue];
3388 	bdp = txq->bd.base;
3389 	for (i = 0; i < txq->bd.ring_size; i++) {
3390 		txq->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
3391 		if (!txq->tx_bounce[i])
3392 			goto err_alloc;
3393 
3394 		bdp->cbd_sc = cpu_to_fec16(0);
3395 		bdp->cbd_bufaddr = cpu_to_fec32(0);
3396 
3397 		if (fep->bufdesc_ex) {
3398 			struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
3399 			ebdp->cbd_esc = cpu_to_fec32(BD_ENET_TX_INT);
3400 		}
3401 
3402 		bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
3403 	}
3404 
3405 	/* Set the last buffer to wrap. */
3406 	bdp = fec_enet_get_prevdesc(bdp, &txq->bd);
3407 	bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP);
3408 
3409 	return 0;
3410 
3411  err_alloc:
3412 	fec_enet_free_buffers(ndev);
3413 	return -ENOMEM;
3414 }
3415 
3416 static int fec_enet_alloc_buffers(struct net_device *ndev)
3417 {
3418 	struct fec_enet_private *fep = netdev_priv(ndev);
3419 	unsigned int i;
3420 
3421 	for (i = 0; i < fep->num_rx_queues; i++)
3422 		if (fec_enet_alloc_rxq_buffers(ndev, i))
3423 			return -ENOMEM;
3424 
3425 	for (i = 0; i < fep->num_tx_queues; i++)
3426 		if (fec_enet_alloc_txq_buffers(ndev, i))
3427 			return -ENOMEM;
3428 	return 0;
3429 }
3430 
3431 static int
3432 fec_enet_open(struct net_device *ndev)
3433 {
3434 	struct fec_enet_private *fep = netdev_priv(ndev);
3435 	int ret;
3436 	bool reset_again;
3437 
3438 	ret = pm_runtime_resume_and_get(&fep->pdev->dev);
3439 	if (ret < 0)
3440 		return ret;
3441 
3442 	pinctrl_pm_select_default_state(&fep->pdev->dev);
3443 	ret = fec_enet_clk_enable(ndev, true);
3444 	if (ret)
3445 		goto clk_enable;
3446 
3447 	/* During the first fec_enet_open call the PHY isn't probed at this
3448 	 * point. Therefore the phy_reset_after_clk_enable() call within
3449 	 * fec_enet_clk_enable() fails. As we need this reset in order to be
3450 	 * sure the PHY is working correctly we check if we need to reset again
3451 	 * later when the PHY is probed
3452 	 */
3453 	if (ndev->phydev && ndev->phydev->drv)
3454 		reset_again = false;
3455 	else
3456 		reset_again = true;
3457 
3458 	/* I should reset the ring buffers here, but I don't yet know
3459 	 * a simple way to do that.
3460 	 */
3461 
3462 	ret = fec_enet_alloc_buffers(ndev);
3463 	if (ret)
3464 		goto err_enet_alloc;
3465 
3466 	/* Init MAC prior to mii bus probe */
3467 	fec_restart(ndev);
3468 
3469 	/* Call phy_reset_after_clk_enable() again if it failed during
3470 	 * phy_reset_after_clk_enable() before because the PHY wasn't probed.
3471 	 */
3472 	if (reset_again)
3473 		fec_enet_phy_reset_after_clk_enable(ndev);
3474 
3475 	/* Probe and connect to PHY when open the interface */
3476 	ret = fec_enet_mii_probe(ndev);
3477 	if (ret)
3478 		goto err_enet_mii_probe;
3479 
3480 	if (fep->quirks & FEC_QUIRK_ERR006687)
3481 		imx6q_cpuidle_fec_irqs_used();
3482 
3483 	if (fep->quirks & FEC_QUIRK_HAS_PMQOS)
3484 		cpu_latency_qos_add_request(&fep->pm_qos_req, 0);
3485 
3486 	napi_enable(&fep->napi);
3487 	phy_start(ndev->phydev);
3488 	netif_tx_start_all_queues(ndev);
3489 
3490 	device_set_wakeup_enable(&ndev->dev, fep->wol_flag &
3491 				 FEC_WOL_FLAG_ENABLE);
3492 
3493 	return 0;
3494 
3495 err_enet_mii_probe:
3496 	fec_enet_free_buffers(ndev);
3497 err_enet_alloc:
3498 	fec_enet_clk_enable(ndev, false);
3499 clk_enable:
3500 	pm_runtime_mark_last_busy(&fep->pdev->dev);
3501 	pm_runtime_put_autosuspend(&fep->pdev->dev);
3502 	pinctrl_pm_select_sleep_state(&fep->pdev->dev);
3503 	return ret;
3504 }
3505 
3506 static int
3507 fec_enet_close(struct net_device *ndev)
3508 {
3509 	struct fec_enet_private *fep = netdev_priv(ndev);
3510 
3511 	phy_stop(ndev->phydev);
3512 
3513 	if (netif_device_present(ndev)) {
3514 		napi_disable(&fep->napi);
3515 		netif_tx_disable(ndev);
3516 		fec_stop(ndev);
3517 	}
3518 
3519 	phy_disconnect(ndev->phydev);
3520 
3521 	if (fep->quirks & FEC_QUIRK_ERR006687)
3522 		imx6q_cpuidle_fec_irqs_unused();
3523 
3524 	fec_enet_update_ethtool_stats(ndev);
3525 
3526 	fec_enet_clk_enable(ndev, false);
3527 	if (fep->quirks & FEC_QUIRK_HAS_PMQOS)
3528 		cpu_latency_qos_remove_request(&fep->pm_qos_req);
3529 
3530 	pinctrl_pm_select_sleep_state(&fep->pdev->dev);
3531 	pm_runtime_mark_last_busy(&fep->pdev->dev);
3532 	pm_runtime_put_autosuspend(&fep->pdev->dev);
3533 
3534 	fec_enet_free_buffers(ndev);
3535 
3536 	return 0;
3537 }
3538 
3539 /* Set or clear the multicast filter for this adaptor.
3540  * Skeleton taken from sunlance driver.
3541  * The CPM Ethernet implementation allows Multicast as well as individual
3542  * MAC address filtering.  Some of the drivers check to make sure it is
3543  * a group multicast address, and discard those that are not.  I guess I
3544  * will do the same for now, but just remove the test if you want
3545  * individual filtering as well (do the upper net layers want or support
3546  * this kind of feature?).
3547  */
3548 
3549 #define FEC_HASH_BITS	6		/* #bits in hash */
3550 
3551 static void set_multicast_list(struct net_device *ndev)
3552 {
3553 	struct fec_enet_private *fep = netdev_priv(ndev);
3554 	struct netdev_hw_addr *ha;
3555 	unsigned int crc, tmp;
3556 	unsigned char hash;
3557 	unsigned int hash_high = 0, hash_low = 0;
3558 
3559 	if (ndev->flags & IFF_PROMISC) {
3560 		tmp = readl(fep->hwp + FEC_R_CNTRL);
3561 		tmp |= 0x8;
3562 		writel(tmp, fep->hwp + FEC_R_CNTRL);
3563 		return;
3564 	}
3565 
3566 	tmp = readl(fep->hwp + FEC_R_CNTRL);
3567 	tmp &= ~0x8;
3568 	writel(tmp, fep->hwp + FEC_R_CNTRL);
3569 
3570 	if (ndev->flags & IFF_ALLMULTI) {
3571 		/* Catch all multicast addresses, so set the
3572 		 * filter to all 1's
3573 		 */
3574 		writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
3575 		writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
3576 
3577 		return;
3578 	}
3579 
3580 	/* Add the addresses in hash register */
3581 	netdev_for_each_mc_addr(ha, ndev) {
3582 		/* calculate crc32 value of mac address */
3583 		crc = ether_crc_le(ndev->addr_len, ha->addr);
3584 
3585 		/* only upper 6 bits (FEC_HASH_BITS) are used
3586 		 * which point to specific bit in the hash registers
3587 		 */
3588 		hash = (crc >> (32 - FEC_HASH_BITS)) & 0x3f;
3589 
3590 		if (hash > 31)
3591 			hash_high |= 1 << (hash - 32);
3592 		else
3593 			hash_low |= 1 << hash;
3594 	}
3595 
3596 	writel(hash_high, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
3597 	writel(hash_low, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
3598 }
3599 
3600 /* Set a MAC change in hardware. */
3601 static int
3602 fec_set_mac_address(struct net_device *ndev, void *p)
3603 {
3604 	struct fec_enet_private *fep = netdev_priv(ndev);
3605 	struct sockaddr *addr = p;
3606 
3607 	if (addr) {
3608 		if (!is_valid_ether_addr(addr->sa_data))
3609 			return -EADDRNOTAVAIL;
3610 		eth_hw_addr_set(ndev, addr->sa_data);
3611 	}
3612 
3613 	/* Add netif status check here to avoid system hang in below case:
3614 	 * ifconfig ethx down; ifconfig ethx hw ether xx:xx:xx:xx:xx:xx;
3615 	 * After ethx down, fec all clocks are gated off and then register
3616 	 * access causes system hang.
3617 	 */
3618 	if (!netif_running(ndev))
3619 		return 0;
3620 
3621 	writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
3622 		(ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
3623 		fep->hwp + FEC_ADDR_LOW);
3624 	writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
3625 		fep->hwp + FEC_ADDR_HIGH);
3626 	return 0;
3627 }
3628 
3629 #ifdef CONFIG_NET_POLL_CONTROLLER
3630 /**
3631  * fec_poll_controller - FEC Poll controller function
3632  * @dev: The FEC network adapter
3633  *
3634  * Polled functionality used by netconsole and others in non interrupt mode
3635  *
3636  */
3637 static void fec_poll_controller(struct net_device *dev)
3638 {
3639 	int i;
3640 	struct fec_enet_private *fep = netdev_priv(dev);
3641 
3642 	for (i = 0; i < FEC_IRQ_NUM; i++) {
3643 		if (fep->irq[i] > 0) {
3644 			disable_irq(fep->irq[i]);
3645 			fec_enet_interrupt(fep->irq[i], dev);
3646 			enable_irq(fep->irq[i]);
3647 		}
3648 	}
3649 }
3650 #endif
3651 
3652 static inline void fec_enet_set_netdev_features(struct net_device *netdev,
3653 	netdev_features_t features)
3654 {
3655 	struct fec_enet_private *fep = netdev_priv(netdev);
3656 	netdev_features_t changed = features ^ netdev->features;
3657 
3658 	netdev->features = features;
3659 
3660 	/* Receive checksum has been changed */
3661 	if (changed & NETIF_F_RXCSUM) {
3662 		if (features & NETIF_F_RXCSUM)
3663 			fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
3664 		else
3665 			fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
3666 	}
3667 }
3668 
3669 static int fec_set_features(struct net_device *netdev,
3670 	netdev_features_t features)
3671 {
3672 	struct fec_enet_private *fep = netdev_priv(netdev);
3673 	netdev_features_t changed = features ^ netdev->features;
3674 
3675 	if (netif_running(netdev) && changed & NETIF_F_RXCSUM) {
3676 		napi_disable(&fep->napi);
3677 		netif_tx_lock_bh(netdev);
3678 		fec_stop(netdev);
3679 		fec_enet_set_netdev_features(netdev, features);
3680 		fec_restart(netdev);
3681 		netif_tx_wake_all_queues(netdev);
3682 		netif_tx_unlock_bh(netdev);
3683 		napi_enable(&fep->napi);
3684 	} else {
3685 		fec_enet_set_netdev_features(netdev, features);
3686 	}
3687 
3688 	return 0;
3689 }
3690 
3691 static u16 fec_enet_get_raw_vlan_tci(struct sk_buff *skb)
3692 {
3693 	struct vlan_ethhdr *vhdr;
3694 	unsigned short vlan_TCI = 0;
3695 
3696 	if (skb->protocol == htons(ETH_P_ALL)) {
3697 		vhdr = (struct vlan_ethhdr *)(skb->data);
3698 		vlan_TCI = ntohs(vhdr->h_vlan_TCI);
3699 	}
3700 
3701 	return vlan_TCI;
3702 }
3703 
3704 static u16 fec_enet_select_queue(struct net_device *ndev, struct sk_buff *skb,
3705 				 struct net_device *sb_dev)
3706 {
3707 	struct fec_enet_private *fep = netdev_priv(ndev);
3708 	u16 vlan_tag;
3709 
3710 	if (!(fep->quirks & FEC_QUIRK_HAS_AVB))
3711 		return netdev_pick_tx(ndev, skb, NULL);
3712 
3713 	vlan_tag = fec_enet_get_raw_vlan_tci(skb);
3714 	if (!vlan_tag)
3715 		return vlan_tag;
3716 
3717 	return fec_enet_vlan_pri_to_queue[vlan_tag >> 13];
3718 }
3719 
3720 static int fec_enet_bpf(struct net_device *dev, struct netdev_bpf *bpf)
3721 {
3722 	struct fec_enet_private *fep = netdev_priv(dev);
3723 	bool is_run = netif_running(dev);
3724 	struct bpf_prog *old_prog;
3725 
3726 	switch (bpf->command) {
3727 	case XDP_SETUP_PROG:
3728 		/* No need to support the SoCs that require to
3729 		 * do the frame swap because the performance wouldn't be
3730 		 * better than the skb mode.
3731 		 */
3732 		if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
3733 			return -EOPNOTSUPP;
3734 
3735 		if (is_run) {
3736 			napi_disable(&fep->napi);
3737 			netif_tx_disable(dev);
3738 		}
3739 
3740 		old_prog = xchg(&fep->xdp_prog, bpf->prog);
3741 		fec_restart(dev);
3742 
3743 		if (is_run) {
3744 			napi_enable(&fep->napi);
3745 			netif_tx_start_all_queues(dev);
3746 		}
3747 
3748 		if (old_prog)
3749 			bpf_prog_put(old_prog);
3750 
3751 		return 0;
3752 
3753 	case XDP_SETUP_XSK_POOL:
3754 		return -EOPNOTSUPP;
3755 
3756 	default:
3757 		return -EOPNOTSUPP;
3758 	}
3759 }
3760 
3761 static int
3762 fec_enet_xdp_get_tx_queue(struct fec_enet_private *fep, int index)
3763 {
3764 	if (unlikely(index < 0))
3765 		return 0;
3766 
3767 	return (index % fep->num_tx_queues);
3768 }
3769 
3770 static int fec_enet_txq_xmit_frame(struct fec_enet_private *fep,
3771 				   struct fec_enet_priv_tx_q *txq,
3772 				   struct xdp_frame *frame)
3773 {
3774 	unsigned int index, status, estatus;
3775 	struct bufdesc *bdp;
3776 	dma_addr_t dma_addr;
3777 	int entries_free;
3778 
3779 	entries_free = fec_enet_get_free_txdesc_num(txq);
3780 	if (entries_free < MAX_SKB_FRAGS + 1) {
3781 		netdev_err(fep->netdev, "NOT enough BD for SG!\n");
3782 		return -EBUSY;
3783 	}
3784 
3785 	/* Fill in a Tx ring entry */
3786 	bdp = txq->bd.cur;
3787 	status = fec16_to_cpu(bdp->cbd_sc);
3788 	status &= ~BD_ENET_TX_STATS;
3789 
3790 	index = fec_enet_get_bd_index(bdp, &txq->bd);
3791 
3792 	dma_addr = dma_map_single(&fep->pdev->dev, frame->data,
3793 				  frame->len, DMA_TO_DEVICE);
3794 	if (dma_mapping_error(&fep->pdev->dev, dma_addr))
3795 		return -ENOMEM;
3796 
3797 	status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
3798 	if (fep->bufdesc_ex)
3799 		estatus = BD_ENET_TX_INT;
3800 
3801 	bdp->cbd_bufaddr = cpu_to_fec32(dma_addr);
3802 	bdp->cbd_datlen = cpu_to_fec16(frame->len);
3803 
3804 	if (fep->bufdesc_ex) {
3805 		struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
3806 
3807 		if (fep->quirks & FEC_QUIRK_HAS_AVB)
3808 			estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
3809 
3810 		ebdp->cbd_bdu = 0;
3811 		ebdp->cbd_esc = cpu_to_fec32(estatus);
3812 	}
3813 
3814 	txq->tx_skbuff[index] = NULL;
3815 
3816 	/* Make sure the updates to rest of the descriptor are performed before
3817 	 * transferring ownership.
3818 	 */
3819 	dma_wmb();
3820 
3821 	/* Send it on its way.  Tell FEC it's ready, interrupt when done,
3822 	 * it's the last BD of the frame, and to put the CRC on the end.
3823 	 */
3824 	status |= (BD_ENET_TX_READY | BD_ENET_TX_TC);
3825 	bdp->cbd_sc = cpu_to_fec16(status);
3826 
3827 	/* If this was the last BD in the ring, start at the beginning again. */
3828 	bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
3829 
3830 	/* Make sure the update to bdp are performed before txq->bd.cur. */
3831 	dma_wmb();
3832 
3833 	txq->bd.cur = bdp;
3834 
3835 	/* Trigger transmission start */
3836 	writel(0, txq->bd.reg_desc_active);
3837 
3838 	return 0;
3839 }
3840 
3841 static int fec_enet_xdp_xmit(struct net_device *dev,
3842 			     int num_frames,
3843 			     struct xdp_frame **frames,
3844 			     u32 flags)
3845 {
3846 	struct fec_enet_private *fep = netdev_priv(dev);
3847 	struct fec_enet_priv_tx_q *txq;
3848 	int cpu = smp_processor_id();
3849 	unsigned int sent_frames = 0;
3850 	struct netdev_queue *nq;
3851 	unsigned int queue;
3852 	int i;
3853 
3854 	queue = fec_enet_xdp_get_tx_queue(fep, cpu);
3855 	txq = fep->tx_queue[queue];
3856 	nq = netdev_get_tx_queue(fep->netdev, queue);
3857 
3858 	__netif_tx_lock(nq, cpu);
3859 
3860 	for (i = 0; i < num_frames; i++) {
3861 		if (fec_enet_txq_xmit_frame(fep, txq, frames[i]) < 0)
3862 			break;
3863 		sent_frames++;
3864 	}
3865 
3866 	__netif_tx_unlock(nq);
3867 
3868 	return sent_frames;
3869 }
3870 
3871 static const struct net_device_ops fec_netdev_ops = {
3872 	.ndo_open		= fec_enet_open,
3873 	.ndo_stop		= fec_enet_close,
3874 	.ndo_start_xmit		= fec_enet_start_xmit,
3875 	.ndo_select_queue       = fec_enet_select_queue,
3876 	.ndo_set_rx_mode	= set_multicast_list,
3877 	.ndo_validate_addr	= eth_validate_addr,
3878 	.ndo_tx_timeout		= fec_timeout,
3879 	.ndo_set_mac_address	= fec_set_mac_address,
3880 	.ndo_eth_ioctl		= fec_enet_ioctl,
3881 #ifdef CONFIG_NET_POLL_CONTROLLER
3882 	.ndo_poll_controller	= fec_poll_controller,
3883 #endif
3884 	.ndo_set_features	= fec_set_features,
3885 	.ndo_bpf		= fec_enet_bpf,
3886 	.ndo_xdp_xmit		= fec_enet_xdp_xmit,
3887 };
3888 
3889 static const unsigned short offset_des_active_rxq[] = {
3890 	FEC_R_DES_ACTIVE_0, FEC_R_DES_ACTIVE_1, FEC_R_DES_ACTIVE_2
3891 };
3892 
3893 static const unsigned short offset_des_active_txq[] = {
3894 	FEC_X_DES_ACTIVE_0, FEC_X_DES_ACTIVE_1, FEC_X_DES_ACTIVE_2
3895 };
3896 
3897  /*
3898   * XXX:  We need to clean up on failure exits here.
3899   *
3900   */
3901 static int fec_enet_init(struct net_device *ndev)
3902 {
3903 	struct fec_enet_private *fep = netdev_priv(ndev);
3904 	struct bufdesc *cbd_base;
3905 	dma_addr_t bd_dma;
3906 	int bd_size;
3907 	unsigned int i;
3908 	unsigned dsize = fep->bufdesc_ex ? sizeof(struct bufdesc_ex) :
3909 			sizeof(struct bufdesc);
3910 	unsigned dsize_log2 = __fls(dsize);
3911 	int ret;
3912 
3913 	WARN_ON(dsize != (1 << dsize_log2));
3914 #if defined(CONFIG_ARM) || defined(CONFIG_ARM64)
3915 	fep->rx_align = 0xf;
3916 	fep->tx_align = 0xf;
3917 #else
3918 	fep->rx_align = 0x3;
3919 	fep->tx_align = 0x3;
3920 #endif
3921 	fep->rx_pkts_itr = FEC_ITR_ICFT_DEFAULT;
3922 	fep->tx_pkts_itr = FEC_ITR_ICFT_DEFAULT;
3923 	fep->rx_time_itr = FEC_ITR_ICTT_DEFAULT;
3924 	fep->tx_time_itr = FEC_ITR_ICTT_DEFAULT;
3925 
3926 	/* Check mask of the streaming and coherent API */
3927 	ret = dma_set_mask_and_coherent(&fep->pdev->dev, DMA_BIT_MASK(32));
3928 	if (ret < 0) {
3929 		dev_warn(&fep->pdev->dev, "No suitable DMA available\n");
3930 		return ret;
3931 	}
3932 
3933 	ret = fec_enet_alloc_queue(ndev);
3934 	if (ret)
3935 		return ret;
3936 
3937 	bd_size = (fep->total_tx_ring_size + fep->total_rx_ring_size) * dsize;
3938 
3939 	/* Allocate memory for buffer descriptors. */
3940 	cbd_base = dmam_alloc_coherent(&fep->pdev->dev, bd_size, &bd_dma,
3941 				       GFP_KERNEL);
3942 	if (!cbd_base) {
3943 		ret = -ENOMEM;
3944 		goto free_queue_mem;
3945 	}
3946 
3947 	/* Get the Ethernet address */
3948 	ret = fec_get_mac(ndev);
3949 	if (ret)
3950 		goto free_queue_mem;
3951 
3952 	/* make sure MAC we just acquired is programmed into the hw */
3953 	fec_set_mac_address(ndev, NULL);
3954 
3955 	/* Set receive and transmit descriptor base. */
3956 	for (i = 0; i < fep->num_rx_queues; i++) {
3957 		struct fec_enet_priv_rx_q *rxq = fep->rx_queue[i];
3958 		unsigned size = dsize * rxq->bd.ring_size;
3959 
3960 		rxq->bd.qid = i;
3961 		rxq->bd.base = cbd_base;
3962 		rxq->bd.cur = cbd_base;
3963 		rxq->bd.dma = bd_dma;
3964 		rxq->bd.dsize = dsize;
3965 		rxq->bd.dsize_log2 = dsize_log2;
3966 		rxq->bd.reg_desc_active = fep->hwp + offset_des_active_rxq[i];
3967 		bd_dma += size;
3968 		cbd_base = (struct bufdesc *)(((void *)cbd_base) + size);
3969 		rxq->bd.last = (struct bufdesc *)(((void *)cbd_base) - dsize);
3970 	}
3971 
3972 	for (i = 0; i < fep->num_tx_queues; i++) {
3973 		struct fec_enet_priv_tx_q *txq = fep->tx_queue[i];
3974 		unsigned size = dsize * txq->bd.ring_size;
3975 
3976 		txq->bd.qid = i;
3977 		txq->bd.base = cbd_base;
3978 		txq->bd.cur = cbd_base;
3979 		txq->bd.dma = bd_dma;
3980 		txq->bd.dsize = dsize;
3981 		txq->bd.dsize_log2 = dsize_log2;
3982 		txq->bd.reg_desc_active = fep->hwp + offset_des_active_txq[i];
3983 		bd_dma += size;
3984 		cbd_base = (struct bufdesc *)(((void *)cbd_base) + size);
3985 		txq->bd.last = (struct bufdesc *)(((void *)cbd_base) - dsize);
3986 	}
3987 
3988 
3989 	/* The FEC Ethernet specific entries in the device structure */
3990 	ndev->watchdog_timeo = TX_TIMEOUT;
3991 	ndev->netdev_ops = &fec_netdev_ops;
3992 	ndev->ethtool_ops = &fec_enet_ethtool_ops;
3993 
3994 	writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
3995 	netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi);
3996 
3997 	if (fep->quirks & FEC_QUIRK_HAS_VLAN)
3998 		/* enable hw VLAN support */
3999 		ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
4000 
4001 	if (fep->quirks & FEC_QUIRK_HAS_CSUM) {
4002 		netif_set_tso_max_segs(ndev, FEC_MAX_TSO_SEGS);
4003 
4004 		/* enable hw accelerator */
4005 		ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
4006 				| NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO);
4007 		fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
4008 	}
4009 
4010 	if (fep->quirks & FEC_QUIRK_HAS_MULTI_QUEUES) {
4011 		fep->tx_align = 0;
4012 		fep->rx_align = 0x3f;
4013 	}
4014 
4015 	ndev->hw_features = ndev->features;
4016 
4017 	if (!(fep->quirks & FEC_QUIRK_SWAP_FRAME))
4018 		ndev->xdp_features = NETDEV_XDP_ACT_BASIC |
4019 				     NETDEV_XDP_ACT_REDIRECT |
4020 				     NETDEV_XDP_ACT_NDO_XMIT;
4021 
4022 	fec_restart(ndev);
4023 
4024 	if (fep->quirks & FEC_QUIRK_MIB_CLEAR)
4025 		fec_enet_clear_ethtool_stats(ndev);
4026 	else
4027 		fec_enet_update_ethtool_stats(ndev);
4028 
4029 	return 0;
4030 
4031 free_queue_mem:
4032 	fec_enet_free_queue(ndev);
4033 	return ret;
4034 }
4035 
4036 #ifdef CONFIG_OF
4037 static int fec_reset_phy(struct platform_device *pdev)
4038 {
4039 	struct gpio_desc *phy_reset;
4040 	int msec = 1, phy_post_delay = 0;
4041 	struct device_node *np = pdev->dev.of_node;
4042 	int err;
4043 
4044 	if (!np)
4045 		return 0;
4046 
4047 	err = of_property_read_u32(np, "phy-reset-duration", &msec);
4048 	/* A sane reset duration should not be longer than 1s */
4049 	if (!err && msec > 1000)
4050 		msec = 1;
4051 
4052 	err = of_property_read_u32(np, "phy-reset-post-delay", &phy_post_delay);
4053 	/* valid reset duration should be less than 1s */
4054 	if (!err && phy_post_delay > 1000)
4055 		return -EINVAL;
4056 
4057 	phy_reset = devm_gpiod_get_optional(&pdev->dev, "phy-reset",
4058 					    GPIOD_OUT_HIGH);
4059 	if (IS_ERR(phy_reset))
4060 		return dev_err_probe(&pdev->dev, PTR_ERR(phy_reset),
4061 				     "failed to get phy-reset-gpios\n");
4062 
4063 	if (!phy_reset)
4064 		return 0;
4065 
4066 	if (msec > 20)
4067 		msleep(msec);
4068 	else
4069 		usleep_range(msec * 1000, msec * 1000 + 1000);
4070 
4071 	gpiod_set_value_cansleep(phy_reset, 0);
4072 
4073 	if (!phy_post_delay)
4074 		return 0;
4075 
4076 	if (phy_post_delay > 20)
4077 		msleep(phy_post_delay);
4078 	else
4079 		usleep_range(phy_post_delay * 1000,
4080 			     phy_post_delay * 1000 + 1000);
4081 
4082 	return 0;
4083 }
4084 #else /* CONFIG_OF */
4085 static int fec_reset_phy(struct platform_device *pdev)
4086 {
4087 	/*
4088 	 * In case of platform probe, the reset has been done
4089 	 * by machine code.
4090 	 */
4091 	return 0;
4092 }
4093 #endif /* CONFIG_OF */
4094 
4095 static void
4096 fec_enet_get_queue_num(struct platform_device *pdev, int *num_tx, int *num_rx)
4097 {
4098 	struct device_node *np = pdev->dev.of_node;
4099 
4100 	*num_tx = *num_rx = 1;
4101 
4102 	if (!np || !of_device_is_available(np))
4103 		return;
4104 
4105 	/* parse the num of tx and rx queues */
4106 	of_property_read_u32(np, "fsl,num-tx-queues", num_tx);
4107 
4108 	of_property_read_u32(np, "fsl,num-rx-queues", num_rx);
4109 
4110 	if (*num_tx < 1 || *num_tx > FEC_ENET_MAX_TX_QS) {
4111 		dev_warn(&pdev->dev, "Invalid num_tx(=%d), fall back to 1\n",
4112 			 *num_tx);
4113 		*num_tx = 1;
4114 		return;
4115 	}
4116 
4117 	if (*num_rx < 1 || *num_rx > FEC_ENET_MAX_RX_QS) {
4118 		dev_warn(&pdev->dev, "Invalid num_rx(=%d), fall back to 1\n",
4119 			 *num_rx);
4120 		*num_rx = 1;
4121 		return;
4122 	}
4123 
4124 }
4125 
4126 static int fec_enet_get_irq_cnt(struct platform_device *pdev)
4127 {
4128 	int irq_cnt = platform_irq_count(pdev);
4129 
4130 	if (irq_cnt > FEC_IRQ_NUM)
4131 		irq_cnt = FEC_IRQ_NUM;	/* last for pps */
4132 	else if (irq_cnt == 2)
4133 		irq_cnt = 1;	/* last for pps */
4134 	else if (irq_cnt <= 0)
4135 		irq_cnt = 1;	/* At least 1 irq is needed */
4136 	return irq_cnt;
4137 }
4138 
4139 static void fec_enet_get_wakeup_irq(struct platform_device *pdev)
4140 {
4141 	struct net_device *ndev = platform_get_drvdata(pdev);
4142 	struct fec_enet_private *fep = netdev_priv(ndev);
4143 
4144 	if (fep->quirks & FEC_QUIRK_WAKEUP_FROM_INT2)
4145 		fep->wake_irq = fep->irq[2];
4146 	else
4147 		fep->wake_irq = fep->irq[0];
4148 }
4149 
4150 static int fec_enet_init_stop_mode(struct fec_enet_private *fep,
4151 				   struct device_node *np)
4152 {
4153 	struct device_node *gpr_np;
4154 	u32 out_val[3];
4155 	int ret = 0;
4156 
4157 	gpr_np = of_parse_phandle(np, "fsl,stop-mode", 0);
4158 	if (!gpr_np)
4159 		return 0;
4160 
4161 	ret = of_property_read_u32_array(np, "fsl,stop-mode", out_val,
4162 					 ARRAY_SIZE(out_val));
4163 	if (ret) {
4164 		dev_dbg(&fep->pdev->dev, "no stop mode property\n");
4165 		goto out;
4166 	}
4167 
4168 	fep->stop_gpr.gpr = syscon_node_to_regmap(gpr_np);
4169 	if (IS_ERR(fep->stop_gpr.gpr)) {
4170 		dev_err(&fep->pdev->dev, "could not find gpr regmap\n");
4171 		ret = PTR_ERR(fep->stop_gpr.gpr);
4172 		fep->stop_gpr.gpr = NULL;
4173 		goto out;
4174 	}
4175 
4176 	fep->stop_gpr.reg = out_val[1];
4177 	fep->stop_gpr.bit = out_val[2];
4178 
4179 out:
4180 	of_node_put(gpr_np);
4181 
4182 	return ret;
4183 }
4184 
4185 static int
4186 fec_probe(struct platform_device *pdev)
4187 {
4188 	struct fec_enet_private *fep;
4189 	struct fec_platform_data *pdata;
4190 	phy_interface_t interface;
4191 	struct net_device *ndev;
4192 	int i, irq, ret = 0;
4193 	const struct of_device_id *of_id;
4194 	static int dev_id;
4195 	struct device_node *np = pdev->dev.of_node, *phy_node;
4196 	int num_tx_qs;
4197 	int num_rx_qs;
4198 	char irq_name[8];
4199 	int irq_cnt;
4200 	struct fec_devinfo *dev_info;
4201 
4202 	fec_enet_get_queue_num(pdev, &num_tx_qs, &num_rx_qs);
4203 
4204 	/* Init network device */
4205 	ndev = alloc_etherdev_mqs(sizeof(struct fec_enet_private) +
4206 				  FEC_STATS_SIZE, num_tx_qs, num_rx_qs);
4207 	if (!ndev)
4208 		return -ENOMEM;
4209 
4210 	SET_NETDEV_DEV(ndev, &pdev->dev);
4211 
4212 	/* setup board info structure */
4213 	fep = netdev_priv(ndev);
4214 
4215 	of_id = of_match_device(fec_dt_ids, &pdev->dev);
4216 	if (of_id)
4217 		pdev->id_entry = of_id->data;
4218 	dev_info = (struct fec_devinfo *)pdev->id_entry->driver_data;
4219 	if (dev_info)
4220 		fep->quirks = dev_info->quirks;
4221 
4222 	fep->netdev = ndev;
4223 	fep->num_rx_queues = num_rx_qs;
4224 	fep->num_tx_queues = num_tx_qs;
4225 
4226 #if !defined(CONFIG_M5272)
4227 	/* default enable pause frame auto negotiation */
4228 	if (fep->quirks & FEC_QUIRK_HAS_GBIT)
4229 		fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
4230 #endif
4231 
4232 	/* Select default pin state */
4233 	pinctrl_pm_select_default_state(&pdev->dev);
4234 
4235 	fep->hwp = devm_platform_ioremap_resource(pdev, 0);
4236 	if (IS_ERR(fep->hwp)) {
4237 		ret = PTR_ERR(fep->hwp);
4238 		goto failed_ioremap;
4239 	}
4240 
4241 	fep->pdev = pdev;
4242 	fep->dev_id = dev_id++;
4243 
4244 	platform_set_drvdata(pdev, ndev);
4245 
4246 	if ((of_machine_is_compatible("fsl,imx6q") ||
4247 	     of_machine_is_compatible("fsl,imx6dl")) &&
4248 	    !of_property_read_bool(np, "fsl,err006687-workaround-present"))
4249 		fep->quirks |= FEC_QUIRK_ERR006687;
4250 
4251 	ret = fec_enet_ipc_handle_init(fep);
4252 	if (ret)
4253 		goto failed_ipc_init;
4254 
4255 	if (of_property_read_bool(np, "fsl,magic-packet"))
4256 		fep->wol_flag |= FEC_WOL_HAS_MAGIC_PACKET;
4257 
4258 	ret = fec_enet_init_stop_mode(fep, np);
4259 	if (ret)
4260 		goto failed_stop_mode;
4261 
4262 	phy_node = of_parse_phandle(np, "phy-handle", 0);
4263 	if (!phy_node && of_phy_is_fixed_link(np)) {
4264 		ret = of_phy_register_fixed_link(np);
4265 		if (ret < 0) {
4266 			dev_err(&pdev->dev,
4267 				"broken fixed-link specification\n");
4268 			goto failed_phy;
4269 		}
4270 		phy_node = of_node_get(np);
4271 	}
4272 	fep->phy_node = phy_node;
4273 
4274 	ret = of_get_phy_mode(pdev->dev.of_node, &interface);
4275 	if (ret) {
4276 		pdata = dev_get_platdata(&pdev->dev);
4277 		if (pdata)
4278 			fep->phy_interface = pdata->phy;
4279 		else
4280 			fep->phy_interface = PHY_INTERFACE_MODE_MII;
4281 	} else {
4282 		fep->phy_interface = interface;
4283 	}
4284 
4285 	ret = fec_enet_parse_rgmii_delay(fep, np);
4286 	if (ret)
4287 		goto failed_rgmii_delay;
4288 
4289 	fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
4290 	if (IS_ERR(fep->clk_ipg)) {
4291 		ret = PTR_ERR(fep->clk_ipg);
4292 		goto failed_clk;
4293 	}
4294 
4295 	fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
4296 	if (IS_ERR(fep->clk_ahb)) {
4297 		ret = PTR_ERR(fep->clk_ahb);
4298 		goto failed_clk;
4299 	}
4300 
4301 	fep->itr_clk_rate = clk_get_rate(fep->clk_ahb);
4302 
4303 	/* enet_out is optional, depends on board */
4304 	fep->clk_enet_out = devm_clk_get_optional(&pdev->dev, "enet_out");
4305 	if (IS_ERR(fep->clk_enet_out)) {
4306 		ret = PTR_ERR(fep->clk_enet_out);
4307 		goto failed_clk;
4308 	}
4309 
4310 	fep->ptp_clk_on = false;
4311 	mutex_init(&fep->ptp_clk_mutex);
4312 
4313 	/* clk_ref is optional, depends on board */
4314 	fep->clk_ref = devm_clk_get_optional(&pdev->dev, "enet_clk_ref");
4315 	if (IS_ERR(fep->clk_ref)) {
4316 		ret = PTR_ERR(fep->clk_ref);
4317 		goto failed_clk;
4318 	}
4319 	fep->clk_ref_rate = clk_get_rate(fep->clk_ref);
4320 
4321 	/* clk_2x_txclk is optional, depends on board */
4322 	if (fep->rgmii_txc_dly || fep->rgmii_rxc_dly) {
4323 		fep->clk_2x_txclk = devm_clk_get(&pdev->dev, "enet_2x_txclk");
4324 		if (IS_ERR(fep->clk_2x_txclk))
4325 			fep->clk_2x_txclk = NULL;
4326 	}
4327 
4328 	fep->bufdesc_ex = fep->quirks & FEC_QUIRK_HAS_BUFDESC_EX;
4329 	fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
4330 	if (IS_ERR(fep->clk_ptp)) {
4331 		fep->clk_ptp = NULL;
4332 		fep->bufdesc_ex = false;
4333 	}
4334 
4335 	ret = fec_enet_clk_enable(ndev, true);
4336 	if (ret)
4337 		goto failed_clk;
4338 
4339 	ret = clk_prepare_enable(fep->clk_ipg);
4340 	if (ret)
4341 		goto failed_clk_ipg;
4342 	ret = clk_prepare_enable(fep->clk_ahb);
4343 	if (ret)
4344 		goto failed_clk_ahb;
4345 
4346 	fep->reg_phy = devm_regulator_get_optional(&pdev->dev, "phy");
4347 	if (!IS_ERR(fep->reg_phy)) {
4348 		ret = regulator_enable(fep->reg_phy);
4349 		if (ret) {
4350 			dev_err(&pdev->dev,
4351 				"Failed to enable phy regulator: %d\n", ret);
4352 			goto failed_regulator;
4353 		}
4354 	} else {
4355 		if (PTR_ERR(fep->reg_phy) == -EPROBE_DEFER) {
4356 			ret = -EPROBE_DEFER;
4357 			goto failed_regulator;
4358 		}
4359 		fep->reg_phy = NULL;
4360 	}
4361 
4362 	pm_runtime_set_autosuspend_delay(&pdev->dev, FEC_MDIO_PM_TIMEOUT);
4363 	pm_runtime_use_autosuspend(&pdev->dev);
4364 	pm_runtime_get_noresume(&pdev->dev);
4365 	pm_runtime_set_active(&pdev->dev);
4366 	pm_runtime_enable(&pdev->dev);
4367 
4368 	ret = fec_reset_phy(pdev);
4369 	if (ret)
4370 		goto failed_reset;
4371 
4372 	irq_cnt = fec_enet_get_irq_cnt(pdev);
4373 	if (fep->bufdesc_ex)
4374 		fec_ptp_init(pdev, irq_cnt);
4375 
4376 	ret = fec_enet_init(ndev);
4377 	if (ret)
4378 		goto failed_init;
4379 
4380 	for (i = 0; i < irq_cnt; i++) {
4381 		snprintf(irq_name, sizeof(irq_name), "int%d", i);
4382 		irq = platform_get_irq_byname_optional(pdev, irq_name);
4383 		if (irq < 0)
4384 			irq = platform_get_irq(pdev, i);
4385 		if (irq < 0) {
4386 			ret = irq;
4387 			goto failed_irq;
4388 		}
4389 		ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt,
4390 				       0, pdev->name, ndev);
4391 		if (ret)
4392 			goto failed_irq;
4393 
4394 		fep->irq[i] = irq;
4395 	}
4396 
4397 	/* Decide which interrupt line is wakeup capable */
4398 	fec_enet_get_wakeup_irq(pdev);
4399 
4400 	ret = fec_enet_mii_init(pdev);
4401 	if (ret)
4402 		goto failed_mii_init;
4403 
4404 	/* Carrier starts down, phylib will bring it up */
4405 	netif_carrier_off(ndev);
4406 	fec_enet_clk_enable(ndev, false);
4407 	pinctrl_pm_select_sleep_state(&pdev->dev);
4408 
4409 	ndev->max_mtu = PKT_MAXBUF_SIZE - ETH_HLEN - ETH_FCS_LEN;
4410 
4411 	ret = register_netdev(ndev);
4412 	if (ret)
4413 		goto failed_register;
4414 
4415 	device_init_wakeup(&ndev->dev, fep->wol_flag &
4416 			   FEC_WOL_HAS_MAGIC_PACKET);
4417 
4418 	if (fep->bufdesc_ex && fep->ptp_clock)
4419 		netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
4420 
4421 	fep->rx_copybreak = COPYBREAK_DEFAULT;
4422 	INIT_WORK(&fep->tx_timeout_work, fec_enet_timeout_work);
4423 
4424 	pm_runtime_mark_last_busy(&pdev->dev);
4425 	pm_runtime_put_autosuspend(&pdev->dev);
4426 
4427 	return 0;
4428 
4429 failed_register:
4430 	fec_enet_mii_remove(fep);
4431 failed_mii_init:
4432 failed_irq:
4433 failed_init:
4434 	fec_ptp_stop(pdev);
4435 failed_reset:
4436 	pm_runtime_put_noidle(&pdev->dev);
4437 	pm_runtime_disable(&pdev->dev);
4438 	if (fep->reg_phy)
4439 		regulator_disable(fep->reg_phy);
4440 failed_regulator:
4441 	clk_disable_unprepare(fep->clk_ahb);
4442 failed_clk_ahb:
4443 	clk_disable_unprepare(fep->clk_ipg);
4444 failed_clk_ipg:
4445 	fec_enet_clk_enable(ndev, false);
4446 failed_clk:
4447 failed_rgmii_delay:
4448 	if (of_phy_is_fixed_link(np))
4449 		of_phy_deregister_fixed_link(np);
4450 	of_node_put(phy_node);
4451 failed_stop_mode:
4452 failed_ipc_init:
4453 failed_phy:
4454 	dev_id--;
4455 failed_ioremap:
4456 	free_netdev(ndev);
4457 
4458 	return ret;
4459 }
4460 
4461 static int
4462 fec_drv_remove(struct platform_device *pdev)
4463 {
4464 	struct net_device *ndev = platform_get_drvdata(pdev);
4465 	struct fec_enet_private *fep = netdev_priv(ndev);
4466 	struct device_node *np = pdev->dev.of_node;
4467 	int ret;
4468 
4469 	ret = pm_runtime_get_sync(&pdev->dev);
4470 	if (ret < 0)
4471 		dev_err(&pdev->dev,
4472 			"Failed to resume device in remove callback (%pe)\n",
4473 			ERR_PTR(ret));
4474 
4475 	cancel_work_sync(&fep->tx_timeout_work);
4476 	fec_ptp_stop(pdev);
4477 	unregister_netdev(ndev);
4478 	fec_enet_mii_remove(fep);
4479 	if (fep->reg_phy)
4480 		regulator_disable(fep->reg_phy);
4481 
4482 	if (of_phy_is_fixed_link(np))
4483 		of_phy_deregister_fixed_link(np);
4484 	of_node_put(fep->phy_node);
4485 
4486 	/* After pm_runtime_get_sync() failed, the clks are still off, so skip
4487 	 * disabling them again.
4488 	 */
4489 	if (ret >= 0) {
4490 		clk_disable_unprepare(fep->clk_ahb);
4491 		clk_disable_unprepare(fep->clk_ipg);
4492 	}
4493 	pm_runtime_put_noidle(&pdev->dev);
4494 	pm_runtime_disable(&pdev->dev);
4495 
4496 	free_netdev(ndev);
4497 	return 0;
4498 }
4499 
4500 static int __maybe_unused fec_suspend(struct device *dev)
4501 {
4502 	struct net_device *ndev = dev_get_drvdata(dev);
4503 	struct fec_enet_private *fep = netdev_priv(ndev);
4504 	int ret;
4505 
4506 	rtnl_lock();
4507 	if (netif_running(ndev)) {
4508 		if (fep->wol_flag & FEC_WOL_FLAG_ENABLE)
4509 			fep->wol_flag |= FEC_WOL_FLAG_SLEEP_ON;
4510 		phy_stop(ndev->phydev);
4511 		napi_disable(&fep->napi);
4512 		netif_tx_lock_bh(ndev);
4513 		netif_device_detach(ndev);
4514 		netif_tx_unlock_bh(ndev);
4515 		fec_stop(ndev);
4516 		if (!(fep->wol_flag & FEC_WOL_FLAG_ENABLE)) {
4517 			fec_irqs_disable(ndev);
4518 			pinctrl_pm_select_sleep_state(&fep->pdev->dev);
4519 		} else {
4520 			fec_irqs_disable_except_wakeup(ndev);
4521 			if (fep->wake_irq > 0) {
4522 				disable_irq(fep->wake_irq);
4523 				enable_irq_wake(fep->wake_irq);
4524 			}
4525 			fec_enet_stop_mode(fep, true);
4526 		}
4527 		/* It's safe to disable clocks since interrupts are masked */
4528 		fec_enet_clk_enable(ndev, false);
4529 
4530 		fep->rpm_active = !pm_runtime_status_suspended(dev);
4531 		if (fep->rpm_active) {
4532 			ret = pm_runtime_force_suspend(dev);
4533 			if (ret < 0) {
4534 				rtnl_unlock();
4535 				return ret;
4536 			}
4537 		}
4538 	}
4539 	rtnl_unlock();
4540 
4541 	if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE))
4542 		regulator_disable(fep->reg_phy);
4543 
4544 	/* SOC supply clock to phy, when clock is disabled, phy link down
4545 	 * SOC control phy regulator, when regulator is disabled, phy link down
4546 	 */
4547 	if (fep->clk_enet_out || fep->reg_phy)
4548 		fep->link = 0;
4549 
4550 	return 0;
4551 }
4552 
4553 static int __maybe_unused fec_resume(struct device *dev)
4554 {
4555 	struct net_device *ndev = dev_get_drvdata(dev);
4556 	struct fec_enet_private *fep = netdev_priv(ndev);
4557 	int ret;
4558 	int val;
4559 
4560 	if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE)) {
4561 		ret = regulator_enable(fep->reg_phy);
4562 		if (ret)
4563 			return ret;
4564 	}
4565 
4566 	rtnl_lock();
4567 	if (netif_running(ndev)) {
4568 		if (fep->rpm_active)
4569 			pm_runtime_force_resume(dev);
4570 
4571 		ret = fec_enet_clk_enable(ndev, true);
4572 		if (ret) {
4573 			rtnl_unlock();
4574 			goto failed_clk;
4575 		}
4576 		if (fep->wol_flag & FEC_WOL_FLAG_ENABLE) {
4577 			fec_enet_stop_mode(fep, false);
4578 			if (fep->wake_irq) {
4579 				disable_irq_wake(fep->wake_irq);
4580 				enable_irq(fep->wake_irq);
4581 			}
4582 
4583 			val = readl(fep->hwp + FEC_ECNTRL);
4584 			val &= ~(FEC_ECR_MAGICEN | FEC_ECR_SLEEP);
4585 			writel(val, fep->hwp + FEC_ECNTRL);
4586 			fep->wol_flag &= ~FEC_WOL_FLAG_SLEEP_ON;
4587 		} else {
4588 			pinctrl_pm_select_default_state(&fep->pdev->dev);
4589 		}
4590 		fec_restart(ndev);
4591 		netif_tx_lock_bh(ndev);
4592 		netif_device_attach(ndev);
4593 		netif_tx_unlock_bh(ndev);
4594 		napi_enable(&fep->napi);
4595 		phy_init_hw(ndev->phydev);
4596 		phy_start(ndev->phydev);
4597 	}
4598 	rtnl_unlock();
4599 
4600 	return 0;
4601 
4602 failed_clk:
4603 	if (fep->reg_phy)
4604 		regulator_disable(fep->reg_phy);
4605 	return ret;
4606 }
4607 
4608 static int __maybe_unused fec_runtime_suspend(struct device *dev)
4609 {
4610 	struct net_device *ndev = dev_get_drvdata(dev);
4611 	struct fec_enet_private *fep = netdev_priv(ndev);
4612 
4613 	clk_disable_unprepare(fep->clk_ahb);
4614 	clk_disable_unprepare(fep->clk_ipg);
4615 
4616 	return 0;
4617 }
4618 
4619 static int __maybe_unused fec_runtime_resume(struct device *dev)
4620 {
4621 	struct net_device *ndev = dev_get_drvdata(dev);
4622 	struct fec_enet_private *fep = netdev_priv(ndev);
4623 	int ret;
4624 
4625 	ret = clk_prepare_enable(fep->clk_ahb);
4626 	if (ret)
4627 		return ret;
4628 	ret = clk_prepare_enable(fep->clk_ipg);
4629 	if (ret)
4630 		goto failed_clk_ipg;
4631 
4632 	return 0;
4633 
4634 failed_clk_ipg:
4635 	clk_disable_unprepare(fep->clk_ahb);
4636 	return ret;
4637 }
4638 
4639 static const struct dev_pm_ops fec_pm_ops = {
4640 	SET_SYSTEM_SLEEP_PM_OPS(fec_suspend, fec_resume)
4641 	SET_RUNTIME_PM_OPS(fec_runtime_suspend, fec_runtime_resume, NULL)
4642 };
4643 
4644 static struct platform_driver fec_driver = {
4645 	.driver	= {
4646 		.name	= DRIVER_NAME,
4647 		.pm	= &fec_pm_ops,
4648 		.of_match_table = fec_dt_ids,
4649 		.suppress_bind_attrs = true,
4650 	},
4651 	.id_table = fec_devtype,
4652 	.probe	= fec_probe,
4653 	.remove	= fec_drv_remove,
4654 };
4655 
4656 module_platform_driver(fec_driver);
4657 
4658 MODULE_LICENSE("GPL");
4659