1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *
4  *   Copyright (C) 2009-2016 John Crispin <blogic@openwrt.org>
5  *   Copyright (C) 2009-2016 Felix Fietkau <nbd@openwrt.org>
6  *   Copyright (C) 2013-2016 Michael Lee <igvtee@gmail.com>
7  */
8 
9 #include <linux/of_device.h>
10 #include <linux/of_mdio.h>
11 #include <linux/of_net.h>
12 #include <linux/of_address.h>
13 #include <linux/mfd/syscon.h>
14 #include <linux/regmap.h>
15 #include <linux/clk.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/if_vlan.h>
18 #include <linux/reset.h>
19 #include <linux/tcp.h>
20 #include <linux/interrupt.h>
21 #include <linux/pinctrl/devinfo.h>
22 #include <linux/phylink.h>
23 #include <linux/pcs/pcs-mtk-lynxi.h>
24 #include <linux/jhash.h>
25 #include <linux/bitfield.h>
26 #include <net/dsa.h>
27 #include <net/dst_metadata.h>
28 
29 #include "mtk_eth_soc.h"
30 #include "mtk_wed.h"
31 
32 static int mtk_msg_level = -1;
33 module_param_named(msg_level, mtk_msg_level, int, 0);
34 MODULE_PARM_DESC(msg_level, "Message level (-1=defaults,0=none,...,16=all)");
35 
36 #define MTK_ETHTOOL_STAT(x) { #x, \
37 			      offsetof(struct mtk_hw_stats, x) / sizeof(u64) }
38 
39 #define MTK_ETHTOOL_XDP_STAT(x) { #x, \
40 				  offsetof(struct mtk_hw_stats, xdp_stats.x) / \
41 				  sizeof(u64) }
42 
43 static const struct mtk_reg_map mtk_reg_map = {
44 	.tx_irq_mask		= 0x1a1c,
45 	.tx_irq_status		= 0x1a18,
46 	.pdma = {
47 		.rx_ptr		= 0x0900,
48 		.rx_cnt_cfg	= 0x0904,
49 		.pcrx_ptr	= 0x0908,
50 		.glo_cfg	= 0x0a04,
51 		.rst_idx	= 0x0a08,
52 		.delay_irq	= 0x0a0c,
53 		.irq_status	= 0x0a20,
54 		.irq_mask	= 0x0a28,
55 		.adma_rx_dbg0	= 0x0a38,
56 		.int_grp	= 0x0a50,
57 	},
58 	.qdma = {
59 		.qtx_cfg	= 0x1800,
60 		.qtx_sch	= 0x1804,
61 		.rx_ptr		= 0x1900,
62 		.rx_cnt_cfg	= 0x1904,
63 		.qcrx_ptr	= 0x1908,
64 		.glo_cfg	= 0x1a04,
65 		.rst_idx	= 0x1a08,
66 		.delay_irq	= 0x1a0c,
67 		.fc_th		= 0x1a10,
68 		.tx_sch_rate	= 0x1a14,
69 		.int_grp	= 0x1a20,
70 		.hred		= 0x1a44,
71 		.ctx_ptr	= 0x1b00,
72 		.dtx_ptr	= 0x1b04,
73 		.crx_ptr	= 0x1b10,
74 		.drx_ptr	= 0x1b14,
75 		.fq_head	= 0x1b20,
76 		.fq_tail	= 0x1b24,
77 		.fq_count	= 0x1b28,
78 		.fq_blen	= 0x1b2c,
79 	},
80 	.gdm1_cnt		= 0x2400,
81 	.gdma_to_ppe		= 0x4444,
82 	.ppe_base		= 0x0c00,
83 	.wdma_base = {
84 		[0]		= 0x2800,
85 		[1]		= 0x2c00,
86 	},
87 	.pse_iq_sta		= 0x0110,
88 	.pse_oq_sta		= 0x0118,
89 };
90 
91 static const struct mtk_reg_map mt7628_reg_map = {
92 	.tx_irq_mask		= 0x0a28,
93 	.tx_irq_status		= 0x0a20,
94 	.pdma = {
95 		.rx_ptr		= 0x0900,
96 		.rx_cnt_cfg	= 0x0904,
97 		.pcrx_ptr	= 0x0908,
98 		.glo_cfg	= 0x0a04,
99 		.rst_idx	= 0x0a08,
100 		.delay_irq	= 0x0a0c,
101 		.irq_status	= 0x0a20,
102 		.irq_mask	= 0x0a28,
103 		.int_grp	= 0x0a50,
104 	},
105 };
106 
107 static const struct mtk_reg_map mt7986_reg_map = {
108 	.tx_irq_mask		= 0x461c,
109 	.tx_irq_status		= 0x4618,
110 	.pdma = {
111 		.rx_ptr		= 0x6100,
112 		.rx_cnt_cfg	= 0x6104,
113 		.pcrx_ptr	= 0x6108,
114 		.glo_cfg	= 0x6204,
115 		.rst_idx	= 0x6208,
116 		.delay_irq	= 0x620c,
117 		.irq_status	= 0x6220,
118 		.irq_mask	= 0x6228,
119 		.adma_rx_dbg0	= 0x6238,
120 		.int_grp	= 0x6250,
121 	},
122 	.qdma = {
123 		.qtx_cfg	= 0x4400,
124 		.qtx_sch	= 0x4404,
125 		.rx_ptr		= 0x4500,
126 		.rx_cnt_cfg	= 0x4504,
127 		.qcrx_ptr	= 0x4508,
128 		.glo_cfg	= 0x4604,
129 		.rst_idx	= 0x4608,
130 		.delay_irq	= 0x460c,
131 		.fc_th		= 0x4610,
132 		.int_grp	= 0x4620,
133 		.hred		= 0x4644,
134 		.ctx_ptr	= 0x4700,
135 		.dtx_ptr	= 0x4704,
136 		.crx_ptr	= 0x4710,
137 		.drx_ptr	= 0x4714,
138 		.fq_head	= 0x4720,
139 		.fq_tail	= 0x4724,
140 		.fq_count	= 0x4728,
141 		.fq_blen	= 0x472c,
142 		.tx_sch_rate	= 0x4798,
143 	},
144 	.gdm1_cnt		= 0x1c00,
145 	.gdma_to_ppe		= 0x3333,
146 	.ppe_base		= 0x2000,
147 	.wdma_base = {
148 		[0]		= 0x4800,
149 		[1]		= 0x4c00,
150 	},
151 	.pse_iq_sta		= 0x0180,
152 	.pse_oq_sta		= 0x01a0,
153 };
154 
155 /* strings used by ethtool */
156 static const struct mtk_ethtool_stats {
157 	char str[ETH_GSTRING_LEN];
158 	u32 offset;
159 } mtk_ethtool_stats[] = {
160 	MTK_ETHTOOL_STAT(tx_bytes),
161 	MTK_ETHTOOL_STAT(tx_packets),
162 	MTK_ETHTOOL_STAT(tx_skip),
163 	MTK_ETHTOOL_STAT(tx_collisions),
164 	MTK_ETHTOOL_STAT(rx_bytes),
165 	MTK_ETHTOOL_STAT(rx_packets),
166 	MTK_ETHTOOL_STAT(rx_overflow),
167 	MTK_ETHTOOL_STAT(rx_fcs_errors),
168 	MTK_ETHTOOL_STAT(rx_short_errors),
169 	MTK_ETHTOOL_STAT(rx_long_errors),
170 	MTK_ETHTOOL_STAT(rx_checksum_errors),
171 	MTK_ETHTOOL_STAT(rx_flow_control_packets),
172 	MTK_ETHTOOL_XDP_STAT(rx_xdp_redirect),
173 	MTK_ETHTOOL_XDP_STAT(rx_xdp_pass),
174 	MTK_ETHTOOL_XDP_STAT(rx_xdp_drop),
175 	MTK_ETHTOOL_XDP_STAT(rx_xdp_tx),
176 	MTK_ETHTOOL_XDP_STAT(rx_xdp_tx_errors),
177 	MTK_ETHTOOL_XDP_STAT(tx_xdp_xmit),
178 	MTK_ETHTOOL_XDP_STAT(tx_xdp_xmit_errors),
179 };
180 
181 static const char * const mtk_clks_source_name[] = {
182 	"ethif", "sgmiitop", "esw", "gp0", "gp1", "gp2", "fe", "trgpll",
183 	"sgmii_tx250m", "sgmii_rx250m", "sgmii_cdr_ref", "sgmii_cdr_fb",
184 	"sgmii2_tx250m", "sgmii2_rx250m", "sgmii2_cdr_ref", "sgmii2_cdr_fb",
185 	"sgmii_ck", "eth2pll", "wocpu0", "wocpu1", "netsys0", "netsys1"
186 };
187 
188 void mtk_w32(struct mtk_eth *eth, u32 val, unsigned reg)
189 {
190 	__raw_writel(val, eth->base + reg);
191 }
192 
193 u32 mtk_r32(struct mtk_eth *eth, unsigned reg)
194 {
195 	return __raw_readl(eth->base + reg);
196 }
197 
198 static u32 mtk_m32(struct mtk_eth *eth, u32 mask, u32 set, unsigned reg)
199 {
200 	u32 val;
201 
202 	val = mtk_r32(eth, reg);
203 	val &= ~mask;
204 	val |= set;
205 	mtk_w32(eth, val, reg);
206 	return reg;
207 }
208 
209 static int mtk_mdio_busy_wait(struct mtk_eth *eth)
210 {
211 	unsigned long t_start = jiffies;
212 
213 	while (1) {
214 		if (!(mtk_r32(eth, MTK_PHY_IAC) & PHY_IAC_ACCESS))
215 			return 0;
216 		if (time_after(jiffies, t_start + PHY_IAC_TIMEOUT))
217 			break;
218 		cond_resched();
219 	}
220 
221 	dev_err(eth->dev, "mdio: MDIO timeout\n");
222 	return -ETIMEDOUT;
223 }
224 
225 static int _mtk_mdio_write_c22(struct mtk_eth *eth, u32 phy_addr, u32 phy_reg,
226 			       u32 write_data)
227 {
228 	int ret;
229 
230 	ret = mtk_mdio_busy_wait(eth);
231 	if (ret < 0)
232 		return ret;
233 
234 	mtk_w32(eth, PHY_IAC_ACCESS |
235 		PHY_IAC_START_C22 |
236 		PHY_IAC_CMD_WRITE |
237 		PHY_IAC_REG(phy_reg) |
238 		PHY_IAC_ADDR(phy_addr) |
239 		PHY_IAC_DATA(write_data),
240 		MTK_PHY_IAC);
241 
242 	ret = mtk_mdio_busy_wait(eth);
243 	if (ret < 0)
244 		return ret;
245 
246 	return 0;
247 }
248 
249 static int _mtk_mdio_write_c45(struct mtk_eth *eth, u32 phy_addr,
250 			       u32 devad, u32 phy_reg, u32 write_data)
251 {
252 	int ret;
253 
254 	ret = mtk_mdio_busy_wait(eth);
255 	if (ret < 0)
256 		return ret;
257 
258 	mtk_w32(eth, PHY_IAC_ACCESS |
259 		PHY_IAC_START_C45 |
260 		PHY_IAC_CMD_C45_ADDR |
261 		PHY_IAC_REG(devad) |
262 		PHY_IAC_ADDR(phy_addr) |
263 		PHY_IAC_DATA(phy_reg),
264 		MTK_PHY_IAC);
265 
266 	ret = mtk_mdio_busy_wait(eth);
267 	if (ret < 0)
268 		return ret;
269 
270 	mtk_w32(eth, PHY_IAC_ACCESS |
271 		PHY_IAC_START_C45 |
272 		PHY_IAC_CMD_WRITE |
273 		PHY_IAC_REG(devad) |
274 		PHY_IAC_ADDR(phy_addr) |
275 		PHY_IAC_DATA(write_data),
276 		MTK_PHY_IAC);
277 
278 	ret = mtk_mdio_busy_wait(eth);
279 	if (ret < 0)
280 		return ret;
281 
282 	return 0;
283 }
284 
285 static int _mtk_mdio_read_c22(struct mtk_eth *eth, u32 phy_addr, u32 phy_reg)
286 {
287 	int ret;
288 
289 	ret = mtk_mdio_busy_wait(eth);
290 	if (ret < 0)
291 		return ret;
292 
293 	mtk_w32(eth, PHY_IAC_ACCESS |
294 		PHY_IAC_START_C22 |
295 		PHY_IAC_CMD_C22_READ |
296 		PHY_IAC_REG(phy_reg) |
297 		PHY_IAC_ADDR(phy_addr),
298 		MTK_PHY_IAC);
299 
300 	ret = mtk_mdio_busy_wait(eth);
301 	if (ret < 0)
302 		return ret;
303 
304 	return mtk_r32(eth, MTK_PHY_IAC) & PHY_IAC_DATA_MASK;
305 }
306 
307 static int _mtk_mdio_read_c45(struct mtk_eth *eth, u32 phy_addr,
308 			      u32 devad, u32 phy_reg)
309 {
310 	int ret;
311 
312 	ret = mtk_mdio_busy_wait(eth);
313 	if (ret < 0)
314 		return ret;
315 
316 	mtk_w32(eth, PHY_IAC_ACCESS |
317 		PHY_IAC_START_C45 |
318 		PHY_IAC_CMD_C45_ADDR |
319 		PHY_IAC_REG(devad) |
320 		PHY_IAC_ADDR(phy_addr) |
321 		PHY_IAC_DATA(phy_reg),
322 		MTK_PHY_IAC);
323 
324 	ret = mtk_mdio_busy_wait(eth);
325 	if (ret < 0)
326 		return ret;
327 
328 	mtk_w32(eth, PHY_IAC_ACCESS |
329 		PHY_IAC_START_C45 |
330 		PHY_IAC_CMD_C45_READ |
331 		PHY_IAC_REG(devad) |
332 		PHY_IAC_ADDR(phy_addr),
333 		MTK_PHY_IAC);
334 
335 	ret = mtk_mdio_busy_wait(eth);
336 	if (ret < 0)
337 		return ret;
338 
339 	return mtk_r32(eth, MTK_PHY_IAC) & PHY_IAC_DATA_MASK;
340 }
341 
342 static int mtk_mdio_write_c22(struct mii_bus *bus, int phy_addr,
343 			      int phy_reg, u16 val)
344 {
345 	struct mtk_eth *eth = bus->priv;
346 
347 	return _mtk_mdio_write_c22(eth, phy_addr, phy_reg, val);
348 }
349 
350 static int mtk_mdio_write_c45(struct mii_bus *bus, int phy_addr,
351 			      int devad, int phy_reg, u16 val)
352 {
353 	struct mtk_eth *eth = bus->priv;
354 
355 	return _mtk_mdio_write_c45(eth, phy_addr, devad, phy_reg, val);
356 }
357 
358 static int mtk_mdio_read_c22(struct mii_bus *bus, int phy_addr, int phy_reg)
359 {
360 	struct mtk_eth *eth = bus->priv;
361 
362 	return _mtk_mdio_read_c22(eth, phy_addr, phy_reg);
363 }
364 
365 static int mtk_mdio_read_c45(struct mii_bus *bus, int phy_addr, int devad,
366 			     int phy_reg)
367 {
368 	struct mtk_eth *eth = bus->priv;
369 
370 	return _mtk_mdio_read_c45(eth, phy_addr, devad, phy_reg);
371 }
372 
373 static int mt7621_gmac0_rgmii_adjust(struct mtk_eth *eth,
374 				     phy_interface_t interface)
375 {
376 	u32 val;
377 
378 	val = (interface == PHY_INTERFACE_MODE_TRGMII) ?
379 		ETHSYS_TRGMII_MT7621_DDR_PLL : 0;
380 
381 	regmap_update_bits(eth->ethsys, ETHSYS_CLKCFG0,
382 			   ETHSYS_TRGMII_MT7621_MASK, val);
383 
384 	return 0;
385 }
386 
387 static void mtk_gmac0_rgmii_adjust(struct mtk_eth *eth,
388 				   phy_interface_t interface, int speed)
389 {
390 	unsigned long rate;
391 	u32 tck, rck, intf;
392 	int ret;
393 
394 	if (interface == PHY_INTERFACE_MODE_TRGMII) {
395 		mtk_w32(eth, TRGMII_MODE, INTF_MODE);
396 		ret = clk_set_rate(eth->clks[MTK_CLK_TRGPLL], 500000000);
397 		if (ret)
398 			dev_err(eth->dev, "Failed to set trgmii pll: %d\n", ret);
399 		return;
400 	}
401 
402 	if (speed == SPEED_1000) {
403 		intf = INTF_MODE_RGMII_1000;
404 		rate = 250000000;
405 		rck = RCK_CTRL_RGMII_1000;
406 		tck = TCK_CTRL_RGMII_1000;
407 	} else {
408 		intf = INTF_MODE_RGMII_10_100;
409 		rate = 500000000;
410 		rck = RCK_CTRL_RGMII_10_100;
411 		tck = TCK_CTRL_RGMII_10_100;
412 	}
413 
414 	mtk_w32(eth, intf, INTF_MODE);
415 
416 	regmap_update_bits(eth->ethsys, ETHSYS_CLKCFG0,
417 			   ETHSYS_TRGMII_CLK_SEL362_5,
418 			   ETHSYS_TRGMII_CLK_SEL362_5);
419 
420 	ret = clk_set_rate(eth->clks[MTK_CLK_TRGPLL], rate);
421 	if (ret)
422 		dev_err(eth->dev, "Failed to set trgmii pll: %d\n", ret);
423 
424 	mtk_w32(eth, rck, TRGMII_RCK_CTRL);
425 	mtk_w32(eth, tck, TRGMII_TCK_CTRL);
426 }
427 
428 static struct phylink_pcs *mtk_mac_select_pcs(struct phylink_config *config,
429 					      phy_interface_t interface)
430 {
431 	struct mtk_mac *mac = container_of(config, struct mtk_mac,
432 					   phylink_config);
433 	struct mtk_eth *eth = mac->hw;
434 	unsigned int sid;
435 
436 	if (interface == PHY_INTERFACE_MODE_SGMII ||
437 	    phy_interface_mode_is_8023z(interface)) {
438 		sid = (MTK_HAS_CAPS(eth->soc->caps, MTK_SHARED_SGMII)) ?
439 		       0 : mac->id;
440 
441 		return eth->sgmii_pcs[sid];
442 	}
443 
444 	return NULL;
445 }
446 
447 static void mtk_mac_config(struct phylink_config *config, unsigned int mode,
448 			   const struct phylink_link_state *state)
449 {
450 	struct mtk_mac *mac = container_of(config, struct mtk_mac,
451 					   phylink_config);
452 	struct mtk_eth *eth = mac->hw;
453 	int val, ge_mode, err = 0;
454 	u32 i;
455 
456 	/* MT76x8 has no hardware settings between for the MAC */
457 	if (!MTK_HAS_CAPS(eth->soc->caps, MTK_SOC_MT7628) &&
458 	    mac->interface != state->interface) {
459 		/* Setup soc pin functions */
460 		switch (state->interface) {
461 		case PHY_INTERFACE_MODE_TRGMII:
462 		case PHY_INTERFACE_MODE_RGMII_TXID:
463 		case PHY_INTERFACE_MODE_RGMII_RXID:
464 		case PHY_INTERFACE_MODE_RGMII_ID:
465 		case PHY_INTERFACE_MODE_RGMII:
466 		case PHY_INTERFACE_MODE_MII:
467 			if (MTK_HAS_CAPS(eth->soc->caps, MTK_RGMII)) {
468 				err = mtk_gmac_rgmii_path_setup(eth, mac->id);
469 				if (err)
470 					goto init_err;
471 			}
472 			break;
473 		case PHY_INTERFACE_MODE_1000BASEX:
474 		case PHY_INTERFACE_MODE_2500BASEX:
475 		case PHY_INTERFACE_MODE_SGMII:
476 			err = mtk_gmac_sgmii_path_setup(eth, mac->id);
477 			if (err)
478 				goto init_err;
479 			break;
480 		case PHY_INTERFACE_MODE_GMII:
481 			if (MTK_HAS_CAPS(eth->soc->caps, MTK_GEPHY)) {
482 				err = mtk_gmac_gephy_path_setup(eth, mac->id);
483 				if (err)
484 					goto init_err;
485 			}
486 			break;
487 		default:
488 			goto err_phy;
489 		}
490 
491 		/* Setup clock for 1st gmac */
492 		if (!mac->id && state->interface != PHY_INTERFACE_MODE_SGMII &&
493 		    !phy_interface_mode_is_8023z(state->interface) &&
494 		    MTK_HAS_CAPS(mac->hw->soc->caps, MTK_GMAC1_TRGMII)) {
495 			if (MTK_HAS_CAPS(mac->hw->soc->caps,
496 					 MTK_TRGMII_MT7621_CLK)) {
497 				if (mt7621_gmac0_rgmii_adjust(mac->hw,
498 							      state->interface))
499 					goto err_phy;
500 			} else {
501 				/* FIXME: this is incorrect. Not only does it
502 				 * use state->speed (which is not guaranteed
503 				 * to be correct) but it also makes use of it
504 				 * in a code path that will only be reachable
505 				 * when the PHY interface mode changes, not
506 				 * when the speed changes. Consequently, RGMII
507 				 * is probably broken.
508 				 */
509 				mtk_gmac0_rgmii_adjust(mac->hw,
510 						       state->interface,
511 						       state->speed);
512 
513 				/* mt7623_pad_clk_setup */
514 				for (i = 0 ; i < NUM_TRGMII_CTRL; i++)
515 					mtk_w32(mac->hw,
516 						TD_DM_DRVP(8) | TD_DM_DRVN(8),
517 						TRGMII_TD_ODT(i));
518 
519 				/* Assert/release MT7623 RXC reset */
520 				mtk_m32(mac->hw, 0, RXC_RST | RXC_DQSISEL,
521 					TRGMII_RCK_CTRL);
522 				mtk_m32(mac->hw, RXC_RST, 0, TRGMII_RCK_CTRL);
523 			}
524 		}
525 
526 		switch (state->interface) {
527 		case PHY_INTERFACE_MODE_MII:
528 		case PHY_INTERFACE_MODE_GMII:
529 			ge_mode = 1;
530 			break;
531 		default:
532 			ge_mode = 0;
533 			break;
534 		}
535 
536 		/* put the gmac into the right mode */
537 		regmap_read(eth->ethsys, ETHSYS_SYSCFG0, &val);
538 		val &= ~SYSCFG0_GE_MODE(SYSCFG0_GE_MASK, mac->id);
539 		val |= SYSCFG0_GE_MODE(ge_mode, mac->id);
540 		regmap_write(eth->ethsys, ETHSYS_SYSCFG0, val);
541 
542 		mac->interface = state->interface;
543 	}
544 
545 	/* SGMII */
546 	if (state->interface == PHY_INTERFACE_MODE_SGMII ||
547 	    phy_interface_mode_is_8023z(state->interface)) {
548 		/* The path GMAC to SGMII will be enabled once the SGMIISYS is
549 		 * being setup done.
550 		 */
551 		regmap_read(eth->ethsys, ETHSYS_SYSCFG0, &val);
552 
553 		regmap_update_bits(eth->ethsys, ETHSYS_SYSCFG0,
554 				   SYSCFG0_SGMII_MASK,
555 				   ~(u32)SYSCFG0_SGMII_MASK);
556 
557 		/* Save the syscfg0 value for mac_finish */
558 		mac->syscfg0 = val;
559 	} else if (phylink_autoneg_inband(mode)) {
560 		dev_err(eth->dev,
561 			"In-band mode not supported in non SGMII mode!\n");
562 		return;
563 	}
564 
565 	return;
566 
567 err_phy:
568 	dev_err(eth->dev, "%s: GMAC%d mode %s not supported!\n", __func__,
569 		mac->id, phy_modes(state->interface));
570 	return;
571 
572 init_err:
573 	dev_err(eth->dev, "%s: GMAC%d mode %s err: %d!\n", __func__,
574 		mac->id, phy_modes(state->interface), err);
575 }
576 
577 static int mtk_mac_finish(struct phylink_config *config, unsigned int mode,
578 			  phy_interface_t interface)
579 {
580 	struct mtk_mac *mac = container_of(config, struct mtk_mac,
581 					   phylink_config);
582 	struct mtk_eth *eth = mac->hw;
583 	u32 mcr_cur, mcr_new;
584 
585 	/* Enable SGMII */
586 	if (interface == PHY_INTERFACE_MODE_SGMII ||
587 	    phy_interface_mode_is_8023z(interface))
588 		regmap_update_bits(eth->ethsys, ETHSYS_SYSCFG0,
589 				   SYSCFG0_SGMII_MASK, mac->syscfg0);
590 
591 	/* Setup gmac */
592 	mcr_cur = mtk_r32(mac->hw, MTK_MAC_MCR(mac->id));
593 	mcr_new = mcr_cur;
594 	mcr_new |= MAC_MCR_IPG_CFG | MAC_MCR_FORCE_MODE |
595 		   MAC_MCR_BACKOFF_EN | MAC_MCR_BACKPR_EN | MAC_MCR_FORCE_LINK |
596 		   MAC_MCR_RX_FIFO_CLR_DIS;
597 
598 	/* Only update control register when needed! */
599 	if (mcr_new != mcr_cur)
600 		mtk_w32(mac->hw, mcr_new, MTK_MAC_MCR(mac->id));
601 
602 	return 0;
603 }
604 
605 static void mtk_mac_pcs_get_state(struct phylink_config *config,
606 				  struct phylink_link_state *state)
607 {
608 	struct mtk_mac *mac = container_of(config, struct mtk_mac,
609 					   phylink_config);
610 	u32 pmsr = mtk_r32(mac->hw, MTK_MAC_MSR(mac->id));
611 
612 	state->link = (pmsr & MAC_MSR_LINK);
613 	state->duplex = (pmsr & MAC_MSR_DPX) >> 1;
614 
615 	switch (pmsr & (MAC_MSR_SPEED_1000 | MAC_MSR_SPEED_100)) {
616 	case 0:
617 		state->speed = SPEED_10;
618 		break;
619 	case MAC_MSR_SPEED_100:
620 		state->speed = SPEED_100;
621 		break;
622 	case MAC_MSR_SPEED_1000:
623 		state->speed = SPEED_1000;
624 		break;
625 	default:
626 		state->speed = SPEED_UNKNOWN;
627 		break;
628 	}
629 
630 	state->pause &= (MLO_PAUSE_RX | MLO_PAUSE_TX);
631 	if (pmsr & MAC_MSR_RX_FC)
632 		state->pause |= MLO_PAUSE_RX;
633 	if (pmsr & MAC_MSR_TX_FC)
634 		state->pause |= MLO_PAUSE_TX;
635 }
636 
637 static void mtk_mac_link_down(struct phylink_config *config, unsigned int mode,
638 			      phy_interface_t interface)
639 {
640 	struct mtk_mac *mac = container_of(config, struct mtk_mac,
641 					   phylink_config);
642 	u32 mcr = mtk_r32(mac->hw, MTK_MAC_MCR(mac->id));
643 
644 	mcr &= ~(MAC_MCR_TX_EN | MAC_MCR_RX_EN);
645 	mtk_w32(mac->hw, mcr, MTK_MAC_MCR(mac->id));
646 }
647 
648 static void mtk_set_queue_speed(struct mtk_eth *eth, unsigned int idx,
649 				int speed)
650 {
651 	const struct mtk_soc_data *soc = eth->soc;
652 	u32 ofs, val;
653 
654 	if (!MTK_HAS_CAPS(soc->caps, MTK_QDMA))
655 		return;
656 
657 	val = MTK_QTX_SCH_MIN_RATE_EN |
658 	      /* minimum: 10 Mbps */
659 	      FIELD_PREP(MTK_QTX_SCH_MIN_RATE_MAN, 1) |
660 	      FIELD_PREP(MTK_QTX_SCH_MIN_RATE_EXP, 4) |
661 	      MTK_QTX_SCH_LEAKY_BUCKET_SIZE;
662 	if (!MTK_HAS_CAPS(eth->soc->caps, MTK_NETSYS_V2))
663 		val |= MTK_QTX_SCH_LEAKY_BUCKET_EN;
664 
665 	if (IS_ENABLED(CONFIG_SOC_MT7621)) {
666 		switch (speed) {
667 		case SPEED_10:
668 			val |= MTK_QTX_SCH_MAX_RATE_EN |
669 			       FIELD_PREP(MTK_QTX_SCH_MAX_RATE_MAN, 103) |
670 			       FIELD_PREP(MTK_QTX_SCH_MAX_RATE_EXP, 2) |
671 			       FIELD_PREP(MTK_QTX_SCH_MAX_RATE_WEIGHT, 1);
672 			break;
673 		case SPEED_100:
674 			val |= MTK_QTX_SCH_MAX_RATE_EN |
675 			       FIELD_PREP(MTK_QTX_SCH_MAX_RATE_MAN, 103) |
676 			       FIELD_PREP(MTK_QTX_SCH_MAX_RATE_EXP, 3);
677 			       FIELD_PREP(MTK_QTX_SCH_MAX_RATE_WEIGHT, 1);
678 			break;
679 		case SPEED_1000:
680 			val |= MTK_QTX_SCH_MAX_RATE_EN |
681 			       FIELD_PREP(MTK_QTX_SCH_MAX_RATE_MAN, 105) |
682 			       FIELD_PREP(MTK_QTX_SCH_MAX_RATE_EXP, 4) |
683 			       FIELD_PREP(MTK_QTX_SCH_MAX_RATE_WEIGHT, 10);
684 			break;
685 		default:
686 			break;
687 		}
688 	} else {
689 		switch (speed) {
690 		case SPEED_10:
691 			val |= MTK_QTX_SCH_MAX_RATE_EN |
692 			       FIELD_PREP(MTK_QTX_SCH_MAX_RATE_MAN, 1) |
693 			       FIELD_PREP(MTK_QTX_SCH_MAX_RATE_EXP, 4) |
694 			       FIELD_PREP(MTK_QTX_SCH_MAX_RATE_WEIGHT, 1);
695 			break;
696 		case SPEED_100:
697 			val |= MTK_QTX_SCH_MAX_RATE_EN |
698 			       FIELD_PREP(MTK_QTX_SCH_MAX_RATE_MAN, 1) |
699 			       FIELD_PREP(MTK_QTX_SCH_MAX_RATE_EXP, 5);
700 			       FIELD_PREP(MTK_QTX_SCH_MAX_RATE_WEIGHT, 1);
701 			break;
702 		case SPEED_1000:
703 			val |= MTK_QTX_SCH_MAX_RATE_EN |
704 			       FIELD_PREP(MTK_QTX_SCH_MAX_RATE_MAN, 10) |
705 			       FIELD_PREP(MTK_QTX_SCH_MAX_RATE_EXP, 5) |
706 			       FIELD_PREP(MTK_QTX_SCH_MAX_RATE_WEIGHT, 10);
707 			break;
708 		default:
709 			break;
710 		}
711 	}
712 
713 	ofs = MTK_QTX_OFFSET * idx;
714 	mtk_w32(eth, val, soc->reg_map->qdma.qtx_sch + ofs);
715 }
716 
717 static void mtk_mac_link_up(struct phylink_config *config,
718 			    struct phy_device *phy,
719 			    unsigned int mode, phy_interface_t interface,
720 			    int speed, int duplex, bool tx_pause, bool rx_pause)
721 {
722 	struct mtk_mac *mac = container_of(config, struct mtk_mac,
723 					   phylink_config);
724 	u32 mcr;
725 
726 	mcr = mtk_r32(mac->hw, MTK_MAC_MCR(mac->id));
727 	mcr &= ~(MAC_MCR_SPEED_100 | MAC_MCR_SPEED_1000 |
728 		 MAC_MCR_FORCE_DPX | MAC_MCR_FORCE_TX_FC |
729 		 MAC_MCR_FORCE_RX_FC);
730 
731 	/* Configure speed */
732 	mac->speed = speed;
733 	switch (speed) {
734 	case SPEED_2500:
735 	case SPEED_1000:
736 		mcr |= MAC_MCR_SPEED_1000;
737 		break;
738 	case SPEED_100:
739 		mcr |= MAC_MCR_SPEED_100;
740 		break;
741 	}
742 
743 	/* Configure duplex */
744 	if (duplex == DUPLEX_FULL)
745 		mcr |= MAC_MCR_FORCE_DPX;
746 
747 	/* Configure pause modes - phylink will avoid these for half duplex */
748 	if (tx_pause)
749 		mcr |= MAC_MCR_FORCE_TX_FC;
750 	if (rx_pause)
751 		mcr |= MAC_MCR_FORCE_RX_FC;
752 
753 	mcr |= MAC_MCR_TX_EN | MAC_MCR_RX_EN;
754 	mtk_w32(mac->hw, mcr, MTK_MAC_MCR(mac->id));
755 }
756 
757 static const struct phylink_mac_ops mtk_phylink_ops = {
758 	.mac_select_pcs = mtk_mac_select_pcs,
759 	.mac_pcs_get_state = mtk_mac_pcs_get_state,
760 	.mac_config = mtk_mac_config,
761 	.mac_finish = mtk_mac_finish,
762 	.mac_link_down = mtk_mac_link_down,
763 	.mac_link_up = mtk_mac_link_up,
764 };
765 
766 static int mtk_mdio_init(struct mtk_eth *eth)
767 {
768 	unsigned int max_clk = 2500000, divider;
769 	struct device_node *mii_np;
770 	int ret;
771 	u32 val;
772 
773 	mii_np = of_get_child_by_name(eth->dev->of_node, "mdio-bus");
774 	if (!mii_np) {
775 		dev_err(eth->dev, "no %s child node found", "mdio-bus");
776 		return -ENODEV;
777 	}
778 
779 	if (!of_device_is_available(mii_np)) {
780 		ret = -ENODEV;
781 		goto err_put_node;
782 	}
783 
784 	eth->mii_bus = devm_mdiobus_alloc(eth->dev);
785 	if (!eth->mii_bus) {
786 		ret = -ENOMEM;
787 		goto err_put_node;
788 	}
789 
790 	eth->mii_bus->name = "mdio";
791 	eth->mii_bus->read = mtk_mdio_read_c22;
792 	eth->mii_bus->write = mtk_mdio_write_c22;
793 	eth->mii_bus->read_c45 = mtk_mdio_read_c45;
794 	eth->mii_bus->write_c45 = mtk_mdio_write_c45;
795 	eth->mii_bus->priv = eth;
796 	eth->mii_bus->parent = eth->dev;
797 
798 	snprintf(eth->mii_bus->id, MII_BUS_ID_SIZE, "%pOFn", mii_np);
799 
800 	if (!of_property_read_u32(mii_np, "clock-frequency", &val)) {
801 		if (val > MDC_MAX_FREQ || val < MDC_MAX_FREQ / MDC_MAX_DIVIDER) {
802 			dev_err(eth->dev, "MDIO clock frequency out of range");
803 			ret = -EINVAL;
804 			goto err_put_node;
805 		}
806 		max_clk = val;
807 	}
808 	divider = min_t(unsigned int, DIV_ROUND_UP(MDC_MAX_FREQ, max_clk), 63);
809 
810 	/* Configure MDC Divider */
811 	val = mtk_r32(eth, MTK_PPSC);
812 	val &= ~PPSC_MDC_CFG;
813 	val |= FIELD_PREP(PPSC_MDC_CFG, divider) | PPSC_MDC_TURBO;
814 	mtk_w32(eth, val, MTK_PPSC);
815 
816 	dev_dbg(eth->dev, "MDC is running on %d Hz\n", MDC_MAX_FREQ / divider);
817 
818 	ret = of_mdiobus_register(eth->mii_bus, mii_np);
819 
820 err_put_node:
821 	of_node_put(mii_np);
822 	return ret;
823 }
824 
825 static void mtk_mdio_cleanup(struct mtk_eth *eth)
826 {
827 	if (!eth->mii_bus)
828 		return;
829 
830 	mdiobus_unregister(eth->mii_bus);
831 }
832 
833 static inline void mtk_tx_irq_disable(struct mtk_eth *eth, u32 mask)
834 {
835 	unsigned long flags;
836 	u32 val;
837 
838 	spin_lock_irqsave(&eth->tx_irq_lock, flags);
839 	val = mtk_r32(eth, eth->soc->reg_map->tx_irq_mask);
840 	mtk_w32(eth, val & ~mask, eth->soc->reg_map->tx_irq_mask);
841 	spin_unlock_irqrestore(&eth->tx_irq_lock, flags);
842 }
843 
844 static inline void mtk_tx_irq_enable(struct mtk_eth *eth, u32 mask)
845 {
846 	unsigned long flags;
847 	u32 val;
848 
849 	spin_lock_irqsave(&eth->tx_irq_lock, flags);
850 	val = mtk_r32(eth, eth->soc->reg_map->tx_irq_mask);
851 	mtk_w32(eth, val | mask, eth->soc->reg_map->tx_irq_mask);
852 	spin_unlock_irqrestore(&eth->tx_irq_lock, flags);
853 }
854 
855 static inline void mtk_rx_irq_disable(struct mtk_eth *eth, u32 mask)
856 {
857 	unsigned long flags;
858 	u32 val;
859 
860 	spin_lock_irqsave(&eth->rx_irq_lock, flags);
861 	val = mtk_r32(eth, eth->soc->reg_map->pdma.irq_mask);
862 	mtk_w32(eth, val & ~mask, eth->soc->reg_map->pdma.irq_mask);
863 	spin_unlock_irqrestore(&eth->rx_irq_lock, flags);
864 }
865 
866 static inline void mtk_rx_irq_enable(struct mtk_eth *eth, u32 mask)
867 {
868 	unsigned long flags;
869 	u32 val;
870 
871 	spin_lock_irqsave(&eth->rx_irq_lock, flags);
872 	val = mtk_r32(eth, eth->soc->reg_map->pdma.irq_mask);
873 	mtk_w32(eth, val | mask, eth->soc->reg_map->pdma.irq_mask);
874 	spin_unlock_irqrestore(&eth->rx_irq_lock, flags);
875 }
876 
877 static int mtk_set_mac_address(struct net_device *dev, void *p)
878 {
879 	int ret = eth_mac_addr(dev, p);
880 	struct mtk_mac *mac = netdev_priv(dev);
881 	struct mtk_eth *eth = mac->hw;
882 	const char *macaddr = dev->dev_addr;
883 
884 	if (ret)
885 		return ret;
886 
887 	if (unlikely(test_bit(MTK_RESETTING, &mac->hw->state)))
888 		return -EBUSY;
889 
890 	spin_lock_bh(&mac->hw->page_lock);
891 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_SOC_MT7628)) {
892 		mtk_w32(mac->hw, (macaddr[0] << 8) | macaddr[1],
893 			MT7628_SDM_MAC_ADRH);
894 		mtk_w32(mac->hw, (macaddr[2] << 24) | (macaddr[3] << 16) |
895 			(macaddr[4] << 8) | macaddr[5],
896 			MT7628_SDM_MAC_ADRL);
897 	} else {
898 		mtk_w32(mac->hw, (macaddr[0] << 8) | macaddr[1],
899 			MTK_GDMA_MAC_ADRH(mac->id));
900 		mtk_w32(mac->hw, (macaddr[2] << 24) | (macaddr[3] << 16) |
901 			(macaddr[4] << 8) | macaddr[5],
902 			MTK_GDMA_MAC_ADRL(mac->id));
903 	}
904 	spin_unlock_bh(&mac->hw->page_lock);
905 
906 	return 0;
907 }
908 
909 void mtk_stats_update_mac(struct mtk_mac *mac)
910 {
911 	struct mtk_hw_stats *hw_stats = mac->hw_stats;
912 	struct mtk_eth *eth = mac->hw;
913 
914 	u64_stats_update_begin(&hw_stats->syncp);
915 
916 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_SOC_MT7628)) {
917 		hw_stats->tx_packets += mtk_r32(mac->hw, MT7628_SDM_TPCNT);
918 		hw_stats->tx_bytes += mtk_r32(mac->hw, MT7628_SDM_TBCNT);
919 		hw_stats->rx_packets += mtk_r32(mac->hw, MT7628_SDM_RPCNT);
920 		hw_stats->rx_bytes += mtk_r32(mac->hw, MT7628_SDM_RBCNT);
921 		hw_stats->rx_checksum_errors +=
922 			mtk_r32(mac->hw, MT7628_SDM_CS_ERR);
923 	} else {
924 		const struct mtk_reg_map *reg_map = eth->soc->reg_map;
925 		unsigned int offs = hw_stats->reg_offset;
926 		u64 stats;
927 
928 		hw_stats->rx_bytes += mtk_r32(mac->hw, reg_map->gdm1_cnt + offs);
929 		stats = mtk_r32(mac->hw, reg_map->gdm1_cnt + 0x4 + offs);
930 		if (stats)
931 			hw_stats->rx_bytes += (stats << 32);
932 		hw_stats->rx_packets +=
933 			mtk_r32(mac->hw, reg_map->gdm1_cnt + 0x8 + offs);
934 		hw_stats->rx_overflow +=
935 			mtk_r32(mac->hw, reg_map->gdm1_cnt + 0x10 + offs);
936 		hw_stats->rx_fcs_errors +=
937 			mtk_r32(mac->hw, reg_map->gdm1_cnt + 0x14 + offs);
938 		hw_stats->rx_short_errors +=
939 			mtk_r32(mac->hw, reg_map->gdm1_cnt + 0x18 + offs);
940 		hw_stats->rx_long_errors +=
941 			mtk_r32(mac->hw, reg_map->gdm1_cnt + 0x1c + offs);
942 		hw_stats->rx_checksum_errors +=
943 			mtk_r32(mac->hw, reg_map->gdm1_cnt + 0x20 + offs);
944 		hw_stats->rx_flow_control_packets +=
945 			mtk_r32(mac->hw, reg_map->gdm1_cnt + 0x24 + offs);
946 		hw_stats->tx_skip +=
947 			mtk_r32(mac->hw, reg_map->gdm1_cnt + 0x28 + offs);
948 		hw_stats->tx_collisions +=
949 			mtk_r32(mac->hw, reg_map->gdm1_cnt + 0x2c + offs);
950 		hw_stats->tx_bytes +=
951 			mtk_r32(mac->hw, reg_map->gdm1_cnt + 0x30 + offs);
952 		stats =  mtk_r32(mac->hw, reg_map->gdm1_cnt + 0x34 + offs);
953 		if (stats)
954 			hw_stats->tx_bytes += (stats << 32);
955 		hw_stats->tx_packets +=
956 			mtk_r32(mac->hw, reg_map->gdm1_cnt + 0x38 + offs);
957 	}
958 
959 	u64_stats_update_end(&hw_stats->syncp);
960 }
961 
962 static void mtk_stats_update(struct mtk_eth *eth)
963 {
964 	int i;
965 
966 	for (i = 0; i < MTK_MAC_COUNT; i++) {
967 		if (!eth->mac[i] || !eth->mac[i]->hw_stats)
968 			continue;
969 		if (spin_trylock(&eth->mac[i]->hw_stats->stats_lock)) {
970 			mtk_stats_update_mac(eth->mac[i]);
971 			spin_unlock(&eth->mac[i]->hw_stats->stats_lock);
972 		}
973 	}
974 }
975 
976 static void mtk_get_stats64(struct net_device *dev,
977 			    struct rtnl_link_stats64 *storage)
978 {
979 	struct mtk_mac *mac = netdev_priv(dev);
980 	struct mtk_hw_stats *hw_stats = mac->hw_stats;
981 	unsigned int start;
982 
983 	if (netif_running(dev) && netif_device_present(dev)) {
984 		if (spin_trylock_bh(&hw_stats->stats_lock)) {
985 			mtk_stats_update_mac(mac);
986 			spin_unlock_bh(&hw_stats->stats_lock);
987 		}
988 	}
989 
990 	do {
991 		start = u64_stats_fetch_begin(&hw_stats->syncp);
992 		storage->rx_packets = hw_stats->rx_packets;
993 		storage->tx_packets = hw_stats->tx_packets;
994 		storage->rx_bytes = hw_stats->rx_bytes;
995 		storage->tx_bytes = hw_stats->tx_bytes;
996 		storage->collisions = hw_stats->tx_collisions;
997 		storage->rx_length_errors = hw_stats->rx_short_errors +
998 			hw_stats->rx_long_errors;
999 		storage->rx_over_errors = hw_stats->rx_overflow;
1000 		storage->rx_crc_errors = hw_stats->rx_fcs_errors;
1001 		storage->rx_errors = hw_stats->rx_checksum_errors;
1002 		storage->tx_aborted_errors = hw_stats->tx_skip;
1003 	} while (u64_stats_fetch_retry(&hw_stats->syncp, start));
1004 
1005 	storage->tx_errors = dev->stats.tx_errors;
1006 	storage->rx_dropped = dev->stats.rx_dropped;
1007 	storage->tx_dropped = dev->stats.tx_dropped;
1008 }
1009 
1010 static inline int mtk_max_frag_size(int mtu)
1011 {
1012 	/* make sure buf_size will be at least MTK_MAX_RX_LENGTH */
1013 	if (mtu + MTK_RX_ETH_HLEN < MTK_MAX_RX_LENGTH_2K)
1014 		mtu = MTK_MAX_RX_LENGTH_2K - MTK_RX_ETH_HLEN;
1015 
1016 	return SKB_DATA_ALIGN(MTK_RX_HLEN + mtu) +
1017 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1018 }
1019 
1020 static inline int mtk_max_buf_size(int frag_size)
1021 {
1022 	int buf_size = frag_size - NET_SKB_PAD - NET_IP_ALIGN -
1023 		       SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1024 
1025 	WARN_ON(buf_size < MTK_MAX_RX_LENGTH_2K);
1026 
1027 	return buf_size;
1028 }
1029 
1030 static bool mtk_rx_get_desc(struct mtk_eth *eth, struct mtk_rx_dma_v2 *rxd,
1031 			    struct mtk_rx_dma_v2 *dma_rxd)
1032 {
1033 	rxd->rxd2 = READ_ONCE(dma_rxd->rxd2);
1034 	if (!(rxd->rxd2 & RX_DMA_DONE))
1035 		return false;
1036 
1037 	rxd->rxd1 = READ_ONCE(dma_rxd->rxd1);
1038 	rxd->rxd3 = READ_ONCE(dma_rxd->rxd3);
1039 	rxd->rxd4 = READ_ONCE(dma_rxd->rxd4);
1040 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_NETSYS_V2)) {
1041 		rxd->rxd5 = READ_ONCE(dma_rxd->rxd5);
1042 		rxd->rxd6 = READ_ONCE(dma_rxd->rxd6);
1043 	}
1044 
1045 	return true;
1046 }
1047 
1048 static void *mtk_max_lro_buf_alloc(gfp_t gfp_mask)
1049 {
1050 	unsigned int size = mtk_max_frag_size(MTK_MAX_LRO_RX_LENGTH);
1051 	unsigned long data;
1052 
1053 	data = __get_free_pages(gfp_mask | __GFP_COMP | __GFP_NOWARN,
1054 				get_order(size));
1055 
1056 	return (void *)data;
1057 }
1058 
1059 /* the qdma core needs scratch memory to be setup */
1060 static int mtk_init_fq_dma(struct mtk_eth *eth)
1061 {
1062 	const struct mtk_soc_data *soc = eth->soc;
1063 	dma_addr_t phy_ring_tail;
1064 	int cnt = MTK_QDMA_RING_SIZE;
1065 	dma_addr_t dma_addr;
1066 	int i;
1067 
1068 	eth->scratch_ring = dma_alloc_coherent(eth->dma_dev,
1069 					       cnt * soc->txrx.txd_size,
1070 					       &eth->phy_scratch_ring,
1071 					       GFP_KERNEL);
1072 	if (unlikely(!eth->scratch_ring))
1073 		return -ENOMEM;
1074 
1075 	eth->scratch_head = kcalloc(cnt, MTK_QDMA_PAGE_SIZE, GFP_KERNEL);
1076 	if (unlikely(!eth->scratch_head))
1077 		return -ENOMEM;
1078 
1079 	dma_addr = dma_map_single(eth->dma_dev,
1080 				  eth->scratch_head, cnt * MTK_QDMA_PAGE_SIZE,
1081 				  DMA_FROM_DEVICE);
1082 	if (unlikely(dma_mapping_error(eth->dma_dev, dma_addr)))
1083 		return -ENOMEM;
1084 
1085 	phy_ring_tail = eth->phy_scratch_ring + soc->txrx.txd_size * (cnt - 1);
1086 
1087 	for (i = 0; i < cnt; i++) {
1088 		struct mtk_tx_dma_v2 *txd;
1089 
1090 		txd = eth->scratch_ring + i * soc->txrx.txd_size;
1091 		txd->txd1 = dma_addr + i * MTK_QDMA_PAGE_SIZE;
1092 		if (i < cnt - 1)
1093 			txd->txd2 = eth->phy_scratch_ring +
1094 				    (i + 1) * soc->txrx.txd_size;
1095 
1096 		txd->txd3 = TX_DMA_PLEN0(MTK_QDMA_PAGE_SIZE);
1097 		txd->txd4 = 0;
1098 		if (MTK_HAS_CAPS(soc->caps, MTK_NETSYS_V2)) {
1099 			txd->txd5 = 0;
1100 			txd->txd6 = 0;
1101 			txd->txd7 = 0;
1102 			txd->txd8 = 0;
1103 		}
1104 	}
1105 
1106 	mtk_w32(eth, eth->phy_scratch_ring, soc->reg_map->qdma.fq_head);
1107 	mtk_w32(eth, phy_ring_tail, soc->reg_map->qdma.fq_tail);
1108 	mtk_w32(eth, (cnt << 16) | cnt, soc->reg_map->qdma.fq_count);
1109 	mtk_w32(eth, MTK_QDMA_PAGE_SIZE << 16, soc->reg_map->qdma.fq_blen);
1110 
1111 	return 0;
1112 }
1113 
1114 static void *mtk_qdma_phys_to_virt(struct mtk_tx_ring *ring, u32 desc)
1115 {
1116 	return ring->dma + (desc - ring->phys);
1117 }
1118 
1119 static struct mtk_tx_buf *mtk_desc_to_tx_buf(struct mtk_tx_ring *ring,
1120 					     void *txd, u32 txd_size)
1121 {
1122 	int idx = (txd - ring->dma) / txd_size;
1123 
1124 	return &ring->buf[idx];
1125 }
1126 
1127 static struct mtk_tx_dma *qdma_to_pdma(struct mtk_tx_ring *ring,
1128 				       struct mtk_tx_dma *dma)
1129 {
1130 	return ring->dma_pdma - (struct mtk_tx_dma *)ring->dma + dma;
1131 }
1132 
1133 static int txd_to_idx(struct mtk_tx_ring *ring, void *dma, u32 txd_size)
1134 {
1135 	return (dma - ring->dma) / txd_size;
1136 }
1137 
1138 static void mtk_tx_unmap(struct mtk_eth *eth, struct mtk_tx_buf *tx_buf,
1139 			 struct xdp_frame_bulk *bq, bool napi)
1140 {
1141 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA)) {
1142 		if (tx_buf->flags & MTK_TX_FLAGS_SINGLE0) {
1143 			dma_unmap_single(eth->dma_dev,
1144 					 dma_unmap_addr(tx_buf, dma_addr0),
1145 					 dma_unmap_len(tx_buf, dma_len0),
1146 					 DMA_TO_DEVICE);
1147 		} else if (tx_buf->flags & MTK_TX_FLAGS_PAGE0) {
1148 			dma_unmap_page(eth->dma_dev,
1149 				       dma_unmap_addr(tx_buf, dma_addr0),
1150 				       dma_unmap_len(tx_buf, dma_len0),
1151 				       DMA_TO_DEVICE);
1152 		}
1153 	} else {
1154 		if (dma_unmap_len(tx_buf, dma_len0)) {
1155 			dma_unmap_page(eth->dma_dev,
1156 				       dma_unmap_addr(tx_buf, dma_addr0),
1157 				       dma_unmap_len(tx_buf, dma_len0),
1158 				       DMA_TO_DEVICE);
1159 		}
1160 
1161 		if (dma_unmap_len(tx_buf, dma_len1)) {
1162 			dma_unmap_page(eth->dma_dev,
1163 				       dma_unmap_addr(tx_buf, dma_addr1),
1164 				       dma_unmap_len(tx_buf, dma_len1),
1165 				       DMA_TO_DEVICE);
1166 		}
1167 	}
1168 
1169 	if (tx_buf->data && tx_buf->data != (void *)MTK_DMA_DUMMY_DESC) {
1170 		if (tx_buf->type == MTK_TYPE_SKB) {
1171 			struct sk_buff *skb = tx_buf->data;
1172 
1173 			if (napi)
1174 				napi_consume_skb(skb, napi);
1175 			else
1176 				dev_kfree_skb_any(skb);
1177 		} else {
1178 			struct xdp_frame *xdpf = tx_buf->data;
1179 
1180 			if (napi && tx_buf->type == MTK_TYPE_XDP_TX)
1181 				xdp_return_frame_rx_napi(xdpf);
1182 			else if (bq)
1183 				xdp_return_frame_bulk(xdpf, bq);
1184 			else
1185 				xdp_return_frame(xdpf);
1186 		}
1187 	}
1188 	tx_buf->flags = 0;
1189 	tx_buf->data = NULL;
1190 }
1191 
1192 static void setup_tx_buf(struct mtk_eth *eth, struct mtk_tx_buf *tx_buf,
1193 			 struct mtk_tx_dma *txd, dma_addr_t mapped_addr,
1194 			 size_t size, int idx)
1195 {
1196 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA)) {
1197 		dma_unmap_addr_set(tx_buf, dma_addr0, mapped_addr);
1198 		dma_unmap_len_set(tx_buf, dma_len0, size);
1199 	} else {
1200 		if (idx & 1) {
1201 			txd->txd3 = mapped_addr;
1202 			txd->txd2 |= TX_DMA_PLEN1(size);
1203 			dma_unmap_addr_set(tx_buf, dma_addr1, mapped_addr);
1204 			dma_unmap_len_set(tx_buf, dma_len1, size);
1205 		} else {
1206 			tx_buf->data = (void *)MTK_DMA_DUMMY_DESC;
1207 			txd->txd1 = mapped_addr;
1208 			txd->txd2 = TX_DMA_PLEN0(size);
1209 			dma_unmap_addr_set(tx_buf, dma_addr0, mapped_addr);
1210 			dma_unmap_len_set(tx_buf, dma_len0, size);
1211 		}
1212 	}
1213 }
1214 
1215 static void mtk_tx_set_dma_desc_v1(struct net_device *dev, void *txd,
1216 				   struct mtk_tx_dma_desc_info *info)
1217 {
1218 	struct mtk_mac *mac = netdev_priv(dev);
1219 	struct mtk_eth *eth = mac->hw;
1220 	struct mtk_tx_dma *desc = txd;
1221 	u32 data;
1222 
1223 	WRITE_ONCE(desc->txd1, info->addr);
1224 
1225 	data = TX_DMA_SWC | TX_DMA_PLEN0(info->size) |
1226 	       FIELD_PREP(TX_DMA_PQID, info->qid);
1227 	if (info->last)
1228 		data |= TX_DMA_LS0;
1229 	WRITE_ONCE(desc->txd3, data);
1230 
1231 	data = (mac->id + 1) << TX_DMA_FPORT_SHIFT; /* forward port */
1232 	if (info->first) {
1233 		if (info->gso)
1234 			data |= TX_DMA_TSO;
1235 		/* tx checksum offload */
1236 		if (info->csum)
1237 			data |= TX_DMA_CHKSUM;
1238 		/* vlan header offload */
1239 		if (info->vlan)
1240 			data |= TX_DMA_INS_VLAN | info->vlan_tci;
1241 	}
1242 	WRITE_ONCE(desc->txd4, data);
1243 }
1244 
1245 static void mtk_tx_set_dma_desc_v2(struct net_device *dev, void *txd,
1246 				   struct mtk_tx_dma_desc_info *info)
1247 {
1248 	struct mtk_mac *mac = netdev_priv(dev);
1249 	struct mtk_tx_dma_v2 *desc = txd;
1250 	struct mtk_eth *eth = mac->hw;
1251 	u32 data;
1252 
1253 	WRITE_ONCE(desc->txd1, info->addr);
1254 
1255 	data = TX_DMA_PLEN0(info->size);
1256 	if (info->last)
1257 		data |= TX_DMA_LS0;
1258 	WRITE_ONCE(desc->txd3, data);
1259 
1260 	data = (mac->id + 1) << TX_DMA_FPORT_SHIFT_V2; /* forward port */
1261 	data |= TX_DMA_SWC_V2 | QID_BITS_V2(info->qid);
1262 	WRITE_ONCE(desc->txd4, data);
1263 
1264 	data = 0;
1265 	if (info->first) {
1266 		if (info->gso)
1267 			data |= TX_DMA_TSO_V2;
1268 		/* tx checksum offload */
1269 		if (info->csum)
1270 			data |= TX_DMA_CHKSUM_V2;
1271 	}
1272 	WRITE_ONCE(desc->txd5, data);
1273 
1274 	data = 0;
1275 	if (info->first && info->vlan)
1276 		data |= TX_DMA_INS_VLAN_V2 | info->vlan_tci;
1277 	WRITE_ONCE(desc->txd6, data);
1278 
1279 	WRITE_ONCE(desc->txd7, 0);
1280 	WRITE_ONCE(desc->txd8, 0);
1281 }
1282 
1283 static void mtk_tx_set_dma_desc(struct net_device *dev, void *txd,
1284 				struct mtk_tx_dma_desc_info *info)
1285 {
1286 	struct mtk_mac *mac = netdev_priv(dev);
1287 	struct mtk_eth *eth = mac->hw;
1288 
1289 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_NETSYS_V2))
1290 		mtk_tx_set_dma_desc_v2(dev, txd, info);
1291 	else
1292 		mtk_tx_set_dma_desc_v1(dev, txd, info);
1293 }
1294 
1295 static int mtk_tx_map(struct sk_buff *skb, struct net_device *dev,
1296 		      int tx_num, struct mtk_tx_ring *ring, bool gso)
1297 {
1298 	struct mtk_tx_dma_desc_info txd_info = {
1299 		.size = skb_headlen(skb),
1300 		.gso = gso,
1301 		.csum = skb->ip_summed == CHECKSUM_PARTIAL,
1302 		.vlan = skb_vlan_tag_present(skb),
1303 		.qid = skb_get_queue_mapping(skb),
1304 		.vlan_tci = skb_vlan_tag_get(skb),
1305 		.first = true,
1306 		.last = !skb_is_nonlinear(skb),
1307 	};
1308 	struct netdev_queue *txq;
1309 	struct mtk_mac *mac = netdev_priv(dev);
1310 	struct mtk_eth *eth = mac->hw;
1311 	const struct mtk_soc_data *soc = eth->soc;
1312 	struct mtk_tx_dma *itxd, *txd;
1313 	struct mtk_tx_dma *itxd_pdma, *txd_pdma;
1314 	struct mtk_tx_buf *itx_buf, *tx_buf;
1315 	int i, n_desc = 1;
1316 	int queue = skb_get_queue_mapping(skb);
1317 	int k = 0;
1318 
1319 	txq = netdev_get_tx_queue(dev, queue);
1320 	itxd = ring->next_free;
1321 	itxd_pdma = qdma_to_pdma(ring, itxd);
1322 	if (itxd == ring->last_free)
1323 		return -ENOMEM;
1324 
1325 	itx_buf = mtk_desc_to_tx_buf(ring, itxd, soc->txrx.txd_size);
1326 	memset(itx_buf, 0, sizeof(*itx_buf));
1327 
1328 	txd_info.addr = dma_map_single(eth->dma_dev, skb->data, txd_info.size,
1329 				       DMA_TO_DEVICE);
1330 	if (unlikely(dma_mapping_error(eth->dma_dev, txd_info.addr)))
1331 		return -ENOMEM;
1332 
1333 	mtk_tx_set_dma_desc(dev, itxd, &txd_info);
1334 
1335 	itx_buf->flags |= MTK_TX_FLAGS_SINGLE0;
1336 	itx_buf->flags |= (!mac->id) ? MTK_TX_FLAGS_FPORT0 :
1337 			  MTK_TX_FLAGS_FPORT1;
1338 	setup_tx_buf(eth, itx_buf, itxd_pdma, txd_info.addr, txd_info.size,
1339 		     k++);
1340 
1341 	/* TX SG offload */
1342 	txd = itxd;
1343 	txd_pdma = qdma_to_pdma(ring, txd);
1344 
1345 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1346 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1347 		unsigned int offset = 0;
1348 		int frag_size = skb_frag_size(frag);
1349 
1350 		while (frag_size) {
1351 			bool new_desc = true;
1352 
1353 			if (MTK_HAS_CAPS(soc->caps, MTK_QDMA) ||
1354 			    (i & 0x1)) {
1355 				txd = mtk_qdma_phys_to_virt(ring, txd->txd2);
1356 				txd_pdma = qdma_to_pdma(ring, txd);
1357 				if (txd == ring->last_free)
1358 					goto err_dma;
1359 
1360 				n_desc++;
1361 			} else {
1362 				new_desc = false;
1363 			}
1364 
1365 			memset(&txd_info, 0, sizeof(struct mtk_tx_dma_desc_info));
1366 			txd_info.size = min_t(unsigned int, frag_size,
1367 					      soc->txrx.dma_max_len);
1368 			txd_info.qid = queue;
1369 			txd_info.last = i == skb_shinfo(skb)->nr_frags - 1 &&
1370 					!(frag_size - txd_info.size);
1371 			txd_info.addr = skb_frag_dma_map(eth->dma_dev, frag,
1372 							 offset, txd_info.size,
1373 							 DMA_TO_DEVICE);
1374 			if (unlikely(dma_mapping_error(eth->dma_dev, txd_info.addr)))
1375 				goto err_dma;
1376 
1377 			mtk_tx_set_dma_desc(dev, txd, &txd_info);
1378 
1379 			tx_buf = mtk_desc_to_tx_buf(ring, txd,
1380 						    soc->txrx.txd_size);
1381 			if (new_desc)
1382 				memset(tx_buf, 0, sizeof(*tx_buf));
1383 			tx_buf->data = (void *)MTK_DMA_DUMMY_DESC;
1384 			tx_buf->flags |= MTK_TX_FLAGS_PAGE0;
1385 			tx_buf->flags |= (!mac->id) ? MTK_TX_FLAGS_FPORT0 :
1386 					 MTK_TX_FLAGS_FPORT1;
1387 
1388 			setup_tx_buf(eth, tx_buf, txd_pdma, txd_info.addr,
1389 				     txd_info.size, k++);
1390 
1391 			frag_size -= txd_info.size;
1392 			offset += txd_info.size;
1393 		}
1394 	}
1395 
1396 	/* store skb to cleanup */
1397 	itx_buf->type = MTK_TYPE_SKB;
1398 	itx_buf->data = skb;
1399 
1400 	if (!MTK_HAS_CAPS(soc->caps, MTK_QDMA)) {
1401 		if (k & 0x1)
1402 			txd_pdma->txd2 |= TX_DMA_LS0;
1403 		else
1404 			txd_pdma->txd2 |= TX_DMA_LS1;
1405 	}
1406 
1407 	netdev_tx_sent_queue(txq, skb->len);
1408 	skb_tx_timestamp(skb);
1409 
1410 	ring->next_free = mtk_qdma_phys_to_virt(ring, txd->txd2);
1411 	atomic_sub(n_desc, &ring->free_count);
1412 
1413 	/* make sure that all changes to the dma ring are flushed before we
1414 	 * continue
1415 	 */
1416 	wmb();
1417 
1418 	if (MTK_HAS_CAPS(soc->caps, MTK_QDMA)) {
1419 		if (netif_xmit_stopped(txq) || !netdev_xmit_more())
1420 			mtk_w32(eth, txd->txd2, soc->reg_map->qdma.ctx_ptr);
1421 	} else {
1422 		int next_idx;
1423 
1424 		next_idx = NEXT_DESP_IDX(txd_to_idx(ring, txd, soc->txrx.txd_size),
1425 					 ring->dma_size);
1426 		mtk_w32(eth, next_idx, MT7628_TX_CTX_IDX0);
1427 	}
1428 
1429 	return 0;
1430 
1431 err_dma:
1432 	do {
1433 		tx_buf = mtk_desc_to_tx_buf(ring, itxd, soc->txrx.txd_size);
1434 
1435 		/* unmap dma */
1436 		mtk_tx_unmap(eth, tx_buf, NULL, false);
1437 
1438 		itxd->txd3 = TX_DMA_LS0 | TX_DMA_OWNER_CPU;
1439 		if (!MTK_HAS_CAPS(soc->caps, MTK_QDMA))
1440 			itxd_pdma->txd2 = TX_DMA_DESP2_DEF;
1441 
1442 		itxd = mtk_qdma_phys_to_virt(ring, itxd->txd2);
1443 		itxd_pdma = qdma_to_pdma(ring, itxd);
1444 	} while (itxd != txd);
1445 
1446 	return -ENOMEM;
1447 }
1448 
1449 static int mtk_cal_txd_req(struct mtk_eth *eth, struct sk_buff *skb)
1450 {
1451 	int i, nfrags = 1;
1452 	skb_frag_t *frag;
1453 
1454 	if (skb_is_gso(skb)) {
1455 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1456 			frag = &skb_shinfo(skb)->frags[i];
1457 			nfrags += DIV_ROUND_UP(skb_frag_size(frag),
1458 					       eth->soc->txrx.dma_max_len);
1459 		}
1460 	} else {
1461 		nfrags += skb_shinfo(skb)->nr_frags;
1462 	}
1463 
1464 	return nfrags;
1465 }
1466 
1467 static int mtk_queue_stopped(struct mtk_eth *eth)
1468 {
1469 	int i;
1470 
1471 	for (i = 0; i < MTK_MAC_COUNT; i++) {
1472 		if (!eth->netdev[i])
1473 			continue;
1474 		if (netif_queue_stopped(eth->netdev[i]))
1475 			return 1;
1476 	}
1477 
1478 	return 0;
1479 }
1480 
1481 static void mtk_wake_queue(struct mtk_eth *eth)
1482 {
1483 	int i;
1484 
1485 	for (i = 0; i < MTK_MAC_COUNT; i++) {
1486 		if (!eth->netdev[i])
1487 			continue;
1488 		netif_tx_wake_all_queues(eth->netdev[i]);
1489 	}
1490 }
1491 
1492 static netdev_tx_t mtk_start_xmit(struct sk_buff *skb, struct net_device *dev)
1493 {
1494 	struct mtk_mac *mac = netdev_priv(dev);
1495 	struct mtk_eth *eth = mac->hw;
1496 	struct mtk_tx_ring *ring = &eth->tx_ring;
1497 	struct net_device_stats *stats = &dev->stats;
1498 	bool gso = false;
1499 	int tx_num;
1500 
1501 	/* normally we can rely on the stack not calling this more than once,
1502 	 * however we have 2 queues running on the same ring so we need to lock
1503 	 * the ring access
1504 	 */
1505 	spin_lock(&eth->page_lock);
1506 
1507 	if (unlikely(test_bit(MTK_RESETTING, &eth->state)))
1508 		goto drop;
1509 
1510 	tx_num = mtk_cal_txd_req(eth, skb);
1511 	if (unlikely(atomic_read(&ring->free_count) <= tx_num)) {
1512 		netif_tx_stop_all_queues(dev);
1513 		netif_err(eth, tx_queued, dev,
1514 			  "Tx Ring full when queue awake!\n");
1515 		spin_unlock(&eth->page_lock);
1516 		return NETDEV_TX_BUSY;
1517 	}
1518 
1519 	/* TSO: fill MSS info in tcp checksum field */
1520 	if (skb_is_gso(skb)) {
1521 		if (skb_cow_head(skb, 0)) {
1522 			netif_warn(eth, tx_err, dev,
1523 				   "GSO expand head fail.\n");
1524 			goto drop;
1525 		}
1526 
1527 		if (skb_shinfo(skb)->gso_type &
1528 				(SKB_GSO_TCPV4 | SKB_GSO_TCPV6)) {
1529 			gso = true;
1530 			tcp_hdr(skb)->check = htons(skb_shinfo(skb)->gso_size);
1531 		}
1532 	}
1533 
1534 	if (mtk_tx_map(skb, dev, tx_num, ring, gso) < 0)
1535 		goto drop;
1536 
1537 	if (unlikely(atomic_read(&ring->free_count) <= ring->thresh))
1538 		netif_tx_stop_all_queues(dev);
1539 
1540 	spin_unlock(&eth->page_lock);
1541 
1542 	return NETDEV_TX_OK;
1543 
1544 drop:
1545 	spin_unlock(&eth->page_lock);
1546 	stats->tx_dropped++;
1547 	dev_kfree_skb_any(skb);
1548 	return NETDEV_TX_OK;
1549 }
1550 
1551 static struct mtk_rx_ring *mtk_get_rx_ring(struct mtk_eth *eth)
1552 {
1553 	int i;
1554 	struct mtk_rx_ring *ring;
1555 	int idx;
1556 
1557 	if (!eth->hwlro)
1558 		return &eth->rx_ring[0];
1559 
1560 	for (i = 0; i < MTK_MAX_RX_RING_NUM; i++) {
1561 		struct mtk_rx_dma *rxd;
1562 
1563 		ring = &eth->rx_ring[i];
1564 		idx = NEXT_DESP_IDX(ring->calc_idx, ring->dma_size);
1565 		rxd = ring->dma + idx * eth->soc->txrx.rxd_size;
1566 		if (rxd->rxd2 & RX_DMA_DONE) {
1567 			ring->calc_idx_update = true;
1568 			return ring;
1569 		}
1570 	}
1571 
1572 	return NULL;
1573 }
1574 
1575 static void mtk_update_rx_cpu_idx(struct mtk_eth *eth)
1576 {
1577 	struct mtk_rx_ring *ring;
1578 	int i;
1579 
1580 	if (!eth->hwlro) {
1581 		ring = &eth->rx_ring[0];
1582 		mtk_w32(eth, ring->calc_idx, ring->crx_idx_reg);
1583 	} else {
1584 		for (i = 0; i < MTK_MAX_RX_RING_NUM; i++) {
1585 			ring = &eth->rx_ring[i];
1586 			if (ring->calc_idx_update) {
1587 				ring->calc_idx_update = false;
1588 				mtk_w32(eth, ring->calc_idx, ring->crx_idx_reg);
1589 			}
1590 		}
1591 	}
1592 }
1593 
1594 static bool mtk_page_pool_enabled(struct mtk_eth *eth)
1595 {
1596 	return MTK_HAS_CAPS(eth->soc->caps, MTK_NETSYS_V2);
1597 }
1598 
1599 static struct page_pool *mtk_create_page_pool(struct mtk_eth *eth,
1600 					      struct xdp_rxq_info *xdp_q,
1601 					      int id, int size)
1602 {
1603 	struct page_pool_params pp_params = {
1604 		.order = 0,
1605 		.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV,
1606 		.pool_size = size,
1607 		.nid = NUMA_NO_NODE,
1608 		.dev = eth->dma_dev,
1609 		.offset = MTK_PP_HEADROOM,
1610 		.max_len = MTK_PP_MAX_BUF_SIZE,
1611 	};
1612 	struct page_pool *pp;
1613 	int err;
1614 
1615 	pp_params.dma_dir = rcu_access_pointer(eth->prog) ? DMA_BIDIRECTIONAL
1616 							  : DMA_FROM_DEVICE;
1617 	pp = page_pool_create(&pp_params);
1618 	if (IS_ERR(pp))
1619 		return pp;
1620 
1621 	err = __xdp_rxq_info_reg(xdp_q, &eth->dummy_dev, id,
1622 				 eth->rx_napi.napi_id, PAGE_SIZE);
1623 	if (err < 0)
1624 		goto err_free_pp;
1625 
1626 	err = xdp_rxq_info_reg_mem_model(xdp_q, MEM_TYPE_PAGE_POOL, pp);
1627 	if (err)
1628 		goto err_unregister_rxq;
1629 
1630 	return pp;
1631 
1632 err_unregister_rxq:
1633 	xdp_rxq_info_unreg(xdp_q);
1634 err_free_pp:
1635 	page_pool_destroy(pp);
1636 
1637 	return ERR_PTR(err);
1638 }
1639 
1640 static void *mtk_page_pool_get_buff(struct page_pool *pp, dma_addr_t *dma_addr,
1641 				    gfp_t gfp_mask)
1642 {
1643 	struct page *page;
1644 
1645 	page = page_pool_alloc_pages(pp, gfp_mask | __GFP_NOWARN);
1646 	if (!page)
1647 		return NULL;
1648 
1649 	*dma_addr = page_pool_get_dma_addr(page) + MTK_PP_HEADROOM;
1650 	return page_address(page);
1651 }
1652 
1653 static void mtk_rx_put_buff(struct mtk_rx_ring *ring, void *data, bool napi)
1654 {
1655 	if (ring->page_pool)
1656 		page_pool_put_full_page(ring->page_pool,
1657 					virt_to_head_page(data), napi);
1658 	else
1659 		skb_free_frag(data);
1660 }
1661 
1662 static int mtk_xdp_frame_map(struct mtk_eth *eth, struct net_device *dev,
1663 			     struct mtk_tx_dma_desc_info *txd_info,
1664 			     struct mtk_tx_dma *txd, struct mtk_tx_buf *tx_buf,
1665 			     void *data, u16 headroom, int index, bool dma_map)
1666 {
1667 	struct mtk_tx_ring *ring = &eth->tx_ring;
1668 	struct mtk_mac *mac = netdev_priv(dev);
1669 	struct mtk_tx_dma *txd_pdma;
1670 
1671 	if (dma_map) {  /* ndo_xdp_xmit */
1672 		txd_info->addr = dma_map_single(eth->dma_dev, data,
1673 						txd_info->size, DMA_TO_DEVICE);
1674 		if (unlikely(dma_mapping_error(eth->dma_dev, txd_info->addr)))
1675 			return -ENOMEM;
1676 
1677 		tx_buf->flags |= MTK_TX_FLAGS_SINGLE0;
1678 	} else {
1679 		struct page *page = virt_to_head_page(data);
1680 
1681 		txd_info->addr = page_pool_get_dma_addr(page) +
1682 				 sizeof(struct xdp_frame) + headroom;
1683 		dma_sync_single_for_device(eth->dma_dev, txd_info->addr,
1684 					   txd_info->size, DMA_BIDIRECTIONAL);
1685 	}
1686 	mtk_tx_set_dma_desc(dev, txd, txd_info);
1687 
1688 	tx_buf->flags |= !mac->id ? MTK_TX_FLAGS_FPORT0 : MTK_TX_FLAGS_FPORT1;
1689 	tx_buf->type = dma_map ? MTK_TYPE_XDP_NDO : MTK_TYPE_XDP_TX;
1690 	tx_buf->data = (void *)MTK_DMA_DUMMY_DESC;
1691 
1692 	txd_pdma = qdma_to_pdma(ring, txd);
1693 	setup_tx_buf(eth, tx_buf, txd_pdma, txd_info->addr, txd_info->size,
1694 		     index);
1695 
1696 	return 0;
1697 }
1698 
1699 static int mtk_xdp_submit_frame(struct mtk_eth *eth, struct xdp_frame *xdpf,
1700 				struct net_device *dev, bool dma_map)
1701 {
1702 	struct skb_shared_info *sinfo = xdp_get_shared_info_from_frame(xdpf);
1703 	const struct mtk_soc_data *soc = eth->soc;
1704 	struct mtk_tx_ring *ring = &eth->tx_ring;
1705 	struct mtk_mac *mac = netdev_priv(dev);
1706 	struct mtk_tx_dma_desc_info txd_info = {
1707 		.size	= xdpf->len,
1708 		.first	= true,
1709 		.last	= !xdp_frame_has_frags(xdpf),
1710 		.qid	= mac->id,
1711 	};
1712 	int err, index = 0, n_desc = 1, nr_frags;
1713 	struct mtk_tx_buf *htx_buf, *tx_buf;
1714 	struct mtk_tx_dma *htxd, *txd;
1715 	void *data = xdpf->data;
1716 
1717 	if (unlikely(test_bit(MTK_RESETTING, &eth->state)))
1718 		return -EBUSY;
1719 
1720 	nr_frags = unlikely(xdp_frame_has_frags(xdpf)) ? sinfo->nr_frags : 0;
1721 	if (unlikely(atomic_read(&ring->free_count) <= 1 + nr_frags))
1722 		return -EBUSY;
1723 
1724 	spin_lock(&eth->page_lock);
1725 
1726 	txd = ring->next_free;
1727 	if (txd == ring->last_free) {
1728 		spin_unlock(&eth->page_lock);
1729 		return -ENOMEM;
1730 	}
1731 	htxd = txd;
1732 
1733 	tx_buf = mtk_desc_to_tx_buf(ring, txd, soc->txrx.txd_size);
1734 	memset(tx_buf, 0, sizeof(*tx_buf));
1735 	htx_buf = tx_buf;
1736 
1737 	for (;;) {
1738 		err = mtk_xdp_frame_map(eth, dev, &txd_info, txd, tx_buf,
1739 					data, xdpf->headroom, index, dma_map);
1740 		if (err < 0)
1741 			goto unmap;
1742 
1743 		if (txd_info.last)
1744 			break;
1745 
1746 		if (MTK_HAS_CAPS(soc->caps, MTK_QDMA) || (index & 0x1)) {
1747 			txd = mtk_qdma_phys_to_virt(ring, txd->txd2);
1748 			if (txd == ring->last_free)
1749 				goto unmap;
1750 
1751 			tx_buf = mtk_desc_to_tx_buf(ring, txd,
1752 						    soc->txrx.txd_size);
1753 			memset(tx_buf, 0, sizeof(*tx_buf));
1754 			n_desc++;
1755 		}
1756 
1757 		memset(&txd_info, 0, sizeof(struct mtk_tx_dma_desc_info));
1758 		txd_info.size = skb_frag_size(&sinfo->frags[index]);
1759 		txd_info.last = index + 1 == nr_frags;
1760 		txd_info.qid = mac->id;
1761 		data = skb_frag_address(&sinfo->frags[index]);
1762 
1763 		index++;
1764 	}
1765 	/* store xdpf for cleanup */
1766 	htx_buf->data = xdpf;
1767 
1768 	if (!MTK_HAS_CAPS(soc->caps, MTK_QDMA)) {
1769 		struct mtk_tx_dma *txd_pdma = qdma_to_pdma(ring, txd);
1770 
1771 		if (index & 1)
1772 			txd_pdma->txd2 |= TX_DMA_LS0;
1773 		else
1774 			txd_pdma->txd2 |= TX_DMA_LS1;
1775 	}
1776 
1777 	ring->next_free = mtk_qdma_phys_to_virt(ring, txd->txd2);
1778 	atomic_sub(n_desc, &ring->free_count);
1779 
1780 	/* make sure that all changes to the dma ring are flushed before we
1781 	 * continue
1782 	 */
1783 	wmb();
1784 
1785 	if (MTK_HAS_CAPS(soc->caps, MTK_QDMA)) {
1786 		mtk_w32(eth, txd->txd2, soc->reg_map->qdma.ctx_ptr);
1787 	} else {
1788 		int idx;
1789 
1790 		idx = txd_to_idx(ring, txd, soc->txrx.txd_size);
1791 		mtk_w32(eth, NEXT_DESP_IDX(idx, ring->dma_size),
1792 			MT7628_TX_CTX_IDX0);
1793 	}
1794 
1795 	spin_unlock(&eth->page_lock);
1796 
1797 	return 0;
1798 
1799 unmap:
1800 	while (htxd != txd) {
1801 		tx_buf = mtk_desc_to_tx_buf(ring, htxd, soc->txrx.txd_size);
1802 		mtk_tx_unmap(eth, tx_buf, NULL, false);
1803 
1804 		htxd->txd3 = TX_DMA_LS0 | TX_DMA_OWNER_CPU;
1805 		if (!MTK_HAS_CAPS(soc->caps, MTK_QDMA)) {
1806 			struct mtk_tx_dma *txd_pdma = qdma_to_pdma(ring, htxd);
1807 
1808 			txd_pdma->txd2 = TX_DMA_DESP2_DEF;
1809 		}
1810 
1811 		htxd = mtk_qdma_phys_to_virt(ring, htxd->txd2);
1812 	}
1813 
1814 	spin_unlock(&eth->page_lock);
1815 
1816 	return err;
1817 }
1818 
1819 static int mtk_xdp_xmit(struct net_device *dev, int num_frame,
1820 			struct xdp_frame **frames, u32 flags)
1821 {
1822 	struct mtk_mac *mac = netdev_priv(dev);
1823 	struct mtk_hw_stats *hw_stats = mac->hw_stats;
1824 	struct mtk_eth *eth = mac->hw;
1825 	int i, nxmit = 0;
1826 
1827 	if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
1828 		return -EINVAL;
1829 
1830 	for (i = 0; i < num_frame; i++) {
1831 		if (mtk_xdp_submit_frame(eth, frames[i], dev, true))
1832 			break;
1833 		nxmit++;
1834 	}
1835 
1836 	u64_stats_update_begin(&hw_stats->syncp);
1837 	hw_stats->xdp_stats.tx_xdp_xmit += nxmit;
1838 	hw_stats->xdp_stats.tx_xdp_xmit_errors += num_frame - nxmit;
1839 	u64_stats_update_end(&hw_stats->syncp);
1840 
1841 	return nxmit;
1842 }
1843 
1844 static u32 mtk_xdp_run(struct mtk_eth *eth, struct mtk_rx_ring *ring,
1845 		       struct xdp_buff *xdp, struct net_device *dev)
1846 {
1847 	struct mtk_mac *mac = netdev_priv(dev);
1848 	struct mtk_hw_stats *hw_stats = mac->hw_stats;
1849 	u64 *count = &hw_stats->xdp_stats.rx_xdp_drop;
1850 	struct bpf_prog *prog;
1851 	u32 act = XDP_PASS;
1852 
1853 	rcu_read_lock();
1854 
1855 	prog = rcu_dereference(eth->prog);
1856 	if (!prog)
1857 		goto out;
1858 
1859 	act = bpf_prog_run_xdp(prog, xdp);
1860 	switch (act) {
1861 	case XDP_PASS:
1862 		count = &hw_stats->xdp_stats.rx_xdp_pass;
1863 		goto update_stats;
1864 	case XDP_REDIRECT:
1865 		if (unlikely(xdp_do_redirect(dev, xdp, prog))) {
1866 			act = XDP_DROP;
1867 			break;
1868 		}
1869 
1870 		count = &hw_stats->xdp_stats.rx_xdp_redirect;
1871 		goto update_stats;
1872 	case XDP_TX: {
1873 		struct xdp_frame *xdpf = xdp_convert_buff_to_frame(xdp);
1874 
1875 		if (!xdpf || mtk_xdp_submit_frame(eth, xdpf, dev, false)) {
1876 			count = &hw_stats->xdp_stats.rx_xdp_tx_errors;
1877 			act = XDP_DROP;
1878 			break;
1879 		}
1880 
1881 		count = &hw_stats->xdp_stats.rx_xdp_tx;
1882 		goto update_stats;
1883 	}
1884 	default:
1885 		bpf_warn_invalid_xdp_action(dev, prog, act);
1886 		fallthrough;
1887 	case XDP_ABORTED:
1888 		trace_xdp_exception(dev, prog, act);
1889 		fallthrough;
1890 	case XDP_DROP:
1891 		break;
1892 	}
1893 
1894 	page_pool_put_full_page(ring->page_pool,
1895 				virt_to_head_page(xdp->data), true);
1896 
1897 update_stats:
1898 	u64_stats_update_begin(&hw_stats->syncp);
1899 	*count = *count + 1;
1900 	u64_stats_update_end(&hw_stats->syncp);
1901 out:
1902 	rcu_read_unlock();
1903 
1904 	return act;
1905 }
1906 
1907 static int mtk_poll_rx(struct napi_struct *napi, int budget,
1908 		       struct mtk_eth *eth)
1909 {
1910 	struct dim_sample dim_sample = {};
1911 	struct mtk_rx_ring *ring;
1912 	bool xdp_flush = false;
1913 	int idx;
1914 	struct sk_buff *skb;
1915 	u8 *data, *new_data;
1916 	struct mtk_rx_dma_v2 *rxd, trxd;
1917 	int done = 0, bytes = 0;
1918 
1919 	while (done < budget) {
1920 		unsigned int pktlen, *rxdcsum;
1921 		struct net_device *netdev;
1922 		dma_addr_t dma_addr;
1923 		u32 hash, reason;
1924 		int mac = 0;
1925 
1926 		ring = mtk_get_rx_ring(eth);
1927 		if (unlikely(!ring))
1928 			goto rx_done;
1929 
1930 		idx = NEXT_DESP_IDX(ring->calc_idx, ring->dma_size);
1931 		rxd = ring->dma + idx * eth->soc->txrx.rxd_size;
1932 		data = ring->data[idx];
1933 
1934 		if (!mtk_rx_get_desc(eth, &trxd, rxd))
1935 			break;
1936 
1937 		/* find out which mac the packet come from. values start at 1 */
1938 		if (MTK_HAS_CAPS(eth->soc->caps, MTK_NETSYS_V2))
1939 			mac = RX_DMA_GET_SPORT_V2(trxd.rxd5) - 1;
1940 		else if (!MTK_HAS_CAPS(eth->soc->caps, MTK_SOC_MT7628) &&
1941 			 !(trxd.rxd4 & RX_DMA_SPECIAL_TAG))
1942 			mac = RX_DMA_GET_SPORT(trxd.rxd4) - 1;
1943 
1944 		if (unlikely(mac < 0 || mac >= MTK_MAC_COUNT ||
1945 			     !eth->netdev[mac]))
1946 			goto release_desc;
1947 
1948 		netdev = eth->netdev[mac];
1949 
1950 		if (unlikely(test_bit(MTK_RESETTING, &eth->state)))
1951 			goto release_desc;
1952 
1953 		pktlen = RX_DMA_GET_PLEN0(trxd.rxd2);
1954 
1955 		/* alloc new buffer */
1956 		if (ring->page_pool) {
1957 			struct page *page = virt_to_head_page(data);
1958 			struct xdp_buff xdp;
1959 			u32 ret;
1960 
1961 			new_data = mtk_page_pool_get_buff(ring->page_pool,
1962 							  &dma_addr,
1963 							  GFP_ATOMIC);
1964 			if (unlikely(!new_data)) {
1965 				netdev->stats.rx_dropped++;
1966 				goto release_desc;
1967 			}
1968 
1969 			dma_sync_single_for_cpu(eth->dma_dev,
1970 				page_pool_get_dma_addr(page) + MTK_PP_HEADROOM,
1971 				pktlen, page_pool_get_dma_dir(ring->page_pool));
1972 
1973 			xdp_init_buff(&xdp, PAGE_SIZE, &ring->xdp_q);
1974 			xdp_prepare_buff(&xdp, data, MTK_PP_HEADROOM, pktlen,
1975 					 false);
1976 			xdp_buff_clear_frags_flag(&xdp);
1977 
1978 			ret = mtk_xdp_run(eth, ring, &xdp, netdev);
1979 			if (ret == XDP_REDIRECT)
1980 				xdp_flush = true;
1981 
1982 			if (ret != XDP_PASS)
1983 				goto skip_rx;
1984 
1985 			skb = build_skb(data, PAGE_SIZE);
1986 			if (unlikely(!skb)) {
1987 				page_pool_put_full_page(ring->page_pool,
1988 							page, true);
1989 				netdev->stats.rx_dropped++;
1990 				goto skip_rx;
1991 			}
1992 
1993 			skb_reserve(skb, xdp.data - xdp.data_hard_start);
1994 			skb_put(skb, xdp.data_end - xdp.data);
1995 			skb_mark_for_recycle(skb);
1996 		} else {
1997 			if (ring->frag_size <= PAGE_SIZE)
1998 				new_data = napi_alloc_frag(ring->frag_size);
1999 			else
2000 				new_data = mtk_max_lro_buf_alloc(GFP_ATOMIC);
2001 
2002 			if (unlikely(!new_data)) {
2003 				netdev->stats.rx_dropped++;
2004 				goto release_desc;
2005 			}
2006 
2007 			dma_addr = dma_map_single(eth->dma_dev,
2008 				new_data + NET_SKB_PAD + eth->ip_align,
2009 				ring->buf_size, DMA_FROM_DEVICE);
2010 			if (unlikely(dma_mapping_error(eth->dma_dev,
2011 						       dma_addr))) {
2012 				skb_free_frag(new_data);
2013 				netdev->stats.rx_dropped++;
2014 				goto release_desc;
2015 			}
2016 
2017 			dma_unmap_single(eth->dma_dev, trxd.rxd1,
2018 					 ring->buf_size, DMA_FROM_DEVICE);
2019 
2020 			skb = build_skb(data, ring->frag_size);
2021 			if (unlikely(!skb)) {
2022 				netdev->stats.rx_dropped++;
2023 				skb_free_frag(data);
2024 				goto skip_rx;
2025 			}
2026 
2027 			skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
2028 			skb_put(skb, pktlen);
2029 		}
2030 
2031 		skb->dev = netdev;
2032 		bytes += skb->len;
2033 
2034 		if (MTK_HAS_CAPS(eth->soc->caps, MTK_NETSYS_V2)) {
2035 			reason = FIELD_GET(MTK_RXD5_PPE_CPU_REASON, trxd.rxd5);
2036 			hash = trxd.rxd5 & MTK_RXD5_FOE_ENTRY;
2037 			if (hash != MTK_RXD5_FOE_ENTRY)
2038 				skb_set_hash(skb, jhash_1word(hash, 0),
2039 					     PKT_HASH_TYPE_L4);
2040 			rxdcsum = &trxd.rxd3;
2041 		} else {
2042 			reason = FIELD_GET(MTK_RXD4_PPE_CPU_REASON, trxd.rxd4);
2043 			hash = trxd.rxd4 & MTK_RXD4_FOE_ENTRY;
2044 			if (hash != MTK_RXD4_FOE_ENTRY)
2045 				skb_set_hash(skb, jhash_1word(hash, 0),
2046 					     PKT_HASH_TYPE_L4);
2047 			rxdcsum = &trxd.rxd4;
2048 		}
2049 
2050 		if (*rxdcsum & eth->soc->txrx.rx_dma_l4_valid)
2051 			skb->ip_summed = CHECKSUM_UNNECESSARY;
2052 		else
2053 			skb_checksum_none_assert(skb);
2054 		skb->protocol = eth_type_trans(skb, netdev);
2055 
2056 		/* When using VLAN untagging in combination with DSA, the
2057 		 * hardware treats the MTK special tag as a VLAN and untags it.
2058 		 */
2059 		if (!MTK_HAS_CAPS(eth->soc->caps, MTK_NETSYS_V2) &&
2060 		    (trxd.rxd2 & RX_DMA_VTAG) && netdev_uses_dsa(netdev)) {
2061 			unsigned int port = RX_DMA_VPID(trxd.rxd3) & GENMASK(2, 0);
2062 
2063 			if (port < ARRAY_SIZE(eth->dsa_meta) &&
2064 			    eth->dsa_meta[port])
2065 				skb_dst_set_noref(skb, &eth->dsa_meta[port]->dst);
2066 		}
2067 
2068 		if (reason == MTK_PPE_CPU_REASON_HIT_UNBIND_RATE_REACHED)
2069 			mtk_ppe_check_skb(eth->ppe[0], skb, hash);
2070 
2071 		skb_record_rx_queue(skb, 0);
2072 		napi_gro_receive(napi, skb);
2073 
2074 skip_rx:
2075 		ring->data[idx] = new_data;
2076 		rxd->rxd1 = (unsigned int)dma_addr;
2077 release_desc:
2078 		if (MTK_HAS_CAPS(eth->soc->caps, MTK_SOC_MT7628))
2079 			rxd->rxd2 = RX_DMA_LSO;
2080 		else
2081 			rxd->rxd2 = RX_DMA_PREP_PLEN0(ring->buf_size);
2082 
2083 		ring->calc_idx = idx;
2084 		done++;
2085 	}
2086 
2087 rx_done:
2088 	if (done) {
2089 		/* make sure that all changes to the dma ring are flushed before
2090 		 * we continue
2091 		 */
2092 		wmb();
2093 		mtk_update_rx_cpu_idx(eth);
2094 	}
2095 
2096 	eth->rx_packets += done;
2097 	eth->rx_bytes += bytes;
2098 	dim_update_sample(eth->rx_events, eth->rx_packets, eth->rx_bytes,
2099 			  &dim_sample);
2100 	net_dim(&eth->rx_dim, dim_sample);
2101 
2102 	if (xdp_flush)
2103 		xdp_do_flush_map();
2104 
2105 	return done;
2106 }
2107 
2108 struct mtk_poll_state {
2109     struct netdev_queue *txq;
2110     unsigned int total;
2111     unsigned int done;
2112     unsigned int bytes;
2113 };
2114 
2115 static void
2116 mtk_poll_tx_done(struct mtk_eth *eth, struct mtk_poll_state *state, u8 mac,
2117 		 struct sk_buff *skb)
2118 {
2119 	struct netdev_queue *txq;
2120 	struct net_device *dev;
2121 	unsigned int bytes = skb->len;
2122 
2123 	state->total++;
2124 	eth->tx_packets++;
2125 	eth->tx_bytes += bytes;
2126 
2127 	dev = eth->netdev[mac];
2128 	if (!dev)
2129 		return;
2130 
2131 	txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
2132 	if (state->txq == txq) {
2133 		state->done++;
2134 		state->bytes += bytes;
2135 		return;
2136 	}
2137 
2138 	if (state->txq)
2139 		netdev_tx_completed_queue(state->txq, state->done, state->bytes);
2140 
2141 	state->txq = txq;
2142 	state->done = 1;
2143 	state->bytes = bytes;
2144 }
2145 
2146 static int mtk_poll_tx_qdma(struct mtk_eth *eth, int budget,
2147 			    struct mtk_poll_state *state)
2148 {
2149 	const struct mtk_reg_map *reg_map = eth->soc->reg_map;
2150 	struct mtk_tx_ring *ring = &eth->tx_ring;
2151 	struct mtk_tx_buf *tx_buf;
2152 	struct xdp_frame_bulk bq;
2153 	struct mtk_tx_dma *desc;
2154 	u32 cpu, dma;
2155 
2156 	cpu = ring->last_free_ptr;
2157 	dma = mtk_r32(eth, reg_map->qdma.drx_ptr);
2158 
2159 	desc = mtk_qdma_phys_to_virt(ring, cpu);
2160 	xdp_frame_bulk_init(&bq);
2161 
2162 	while ((cpu != dma) && budget) {
2163 		u32 next_cpu = desc->txd2;
2164 		int mac = 0;
2165 
2166 		desc = mtk_qdma_phys_to_virt(ring, desc->txd2);
2167 		if ((desc->txd3 & TX_DMA_OWNER_CPU) == 0)
2168 			break;
2169 
2170 		tx_buf = mtk_desc_to_tx_buf(ring, desc,
2171 					    eth->soc->txrx.txd_size);
2172 		if (tx_buf->flags & MTK_TX_FLAGS_FPORT1)
2173 			mac = 1;
2174 
2175 		if (!tx_buf->data)
2176 			break;
2177 
2178 		if (tx_buf->data != (void *)MTK_DMA_DUMMY_DESC) {
2179 			if (tx_buf->type == MTK_TYPE_SKB)
2180 				mtk_poll_tx_done(eth, state, mac, tx_buf->data);
2181 
2182 			budget--;
2183 		}
2184 		mtk_tx_unmap(eth, tx_buf, &bq, true);
2185 
2186 		ring->last_free = desc;
2187 		atomic_inc(&ring->free_count);
2188 
2189 		cpu = next_cpu;
2190 	}
2191 	xdp_flush_frame_bulk(&bq);
2192 
2193 	ring->last_free_ptr = cpu;
2194 	mtk_w32(eth, cpu, reg_map->qdma.crx_ptr);
2195 
2196 	return budget;
2197 }
2198 
2199 static int mtk_poll_tx_pdma(struct mtk_eth *eth, int budget,
2200 			    struct mtk_poll_state *state)
2201 {
2202 	struct mtk_tx_ring *ring = &eth->tx_ring;
2203 	struct mtk_tx_buf *tx_buf;
2204 	struct xdp_frame_bulk bq;
2205 	struct mtk_tx_dma *desc;
2206 	u32 cpu, dma;
2207 
2208 	cpu = ring->cpu_idx;
2209 	dma = mtk_r32(eth, MT7628_TX_DTX_IDX0);
2210 	xdp_frame_bulk_init(&bq);
2211 
2212 	while ((cpu != dma) && budget) {
2213 		tx_buf = &ring->buf[cpu];
2214 		if (!tx_buf->data)
2215 			break;
2216 
2217 		if (tx_buf->data != (void *)MTK_DMA_DUMMY_DESC) {
2218 			if (tx_buf->type == MTK_TYPE_SKB)
2219 				mtk_poll_tx_done(eth, state, 0, tx_buf->data);
2220 			budget--;
2221 		}
2222 		mtk_tx_unmap(eth, tx_buf, &bq, true);
2223 
2224 		desc = ring->dma + cpu * eth->soc->txrx.txd_size;
2225 		ring->last_free = desc;
2226 		atomic_inc(&ring->free_count);
2227 
2228 		cpu = NEXT_DESP_IDX(cpu, ring->dma_size);
2229 	}
2230 	xdp_flush_frame_bulk(&bq);
2231 
2232 	ring->cpu_idx = cpu;
2233 
2234 	return budget;
2235 }
2236 
2237 static int mtk_poll_tx(struct mtk_eth *eth, int budget)
2238 {
2239 	struct mtk_tx_ring *ring = &eth->tx_ring;
2240 	struct dim_sample dim_sample = {};
2241 	struct mtk_poll_state state = {};
2242 
2243 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA))
2244 		budget = mtk_poll_tx_qdma(eth, budget, &state);
2245 	else
2246 		budget = mtk_poll_tx_pdma(eth, budget, &state);
2247 
2248 	if (state.txq)
2249 		netdev_tx_completed_queue(state.txq, state.done, state.bytes);
2250 
2251 	dim_update_sample(eth->tx_events, eth->tx_packets, eth->tx_bytes,
2252 			  &dim_sample);
2253 	net_dim(&eth->tx_dim, dim_sample);
2254 
2255 	if (mtk_queue_stopped(eth) &&
2256 	    (atomic_read(&ring->free_count) > ring->thresh))
2257 		mtk_wake_queue(eth);
2258 
2259 	return state.total;
2260 }
2261 
2262 static void mtk_handle_status_irq(struct mtk_eth *eth)
2263 {
2264 	u32 status2 = mtk_r32(eth, MTK_INT_STATUS2);
2265 
2266 	if (unlikely(status2 & (MTK_GDM1_AF | MTK_GDM2_AF))) {
2267 		mtk_stats_update(eth);
2268 		mtk_w32(eth, (MTK_GDM1_AF | MTK_GDM2_AF),
2269 			MTK_INT_STATUS2);
2270 	}
2271 }
2272 
2273 static int mtk_napi_tx(struct napi_struct *napi, int budget)
2274 {
2275 	struct mtk_eth *eth = container_of(napi, struct mtk_eth, tx_napi);
2276 	const struct mtk_reg_map *reg_map = eth->soc->reg_map;
2277 	int tx_done = 0;
2278 
2279 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA))
2280 		mtk_handle_status_irq(eth);
2281 	mtk_w32(eth, MTK_TX_DONE_INT, reg_map->tx_irq_status);
2282 	tx_done = mtk_poll_tx(eth, budget);
2283 
2284 	if (unlikely(netif_msg_intr(eth))) {
2285 		dev_info(eth->dev,
2286 			 "done tx %d, intr 0x%08x/0x%x\n", tx_done,
2287 			 mtk_r32(eth, reg_map->tx_irq_status),
2288 			 mtk_r32(eth, reg_map->tx_irq_mask));
2289 	}
2290 
2291 	if (tx_done == budget)
2292 		return budget;
2293 
2294 	if (mtk_r32(eth, reg_map->tx_irq_status) & MTK_TX_DONE_INT)
2295 		return budget;
2296 
2297 	if (napi_complete_done(napi, tx_done))
2298 		mtk_tx_irq_enable(eth, MTK_TX_DONE_INT);
2299 
2300 	return tx_done;
2301 }
2302 
2303 static int mtk_napi_rx(struct napi_struct *napi, int budget)
2304 {
2305 	struct mtk_eth *eth = container_of(napi, struct mtk_eth, rx_napi);
2306 	const struct mtk_reg_map *reg_map = eth->soc->reg_map;
2307 	int rx_done_total = 0;
2308 
2309 	mtk_handle_status_irq(eth);
2310 
2311 	do {
2312 		int rx_done;
2313 
2314 		mtk_w32(eth, eth->soc->txrx.rx_irq_done_mask,
2315 			reg_map->pdma.irq_status);
2316 		rx_done = mtk_poll_rx(napi, budget - rx_done_total, eth);
2317 		rx_done_total += rx_done;
2318 
2319 		if (unlikely(netif_msg_intr(eth))) {
2320 			dev_info(eth->dev,
2321 				 "done rx %d, intr 0x%08x/0x%x\n", rx_done,
2322 				 mtk_r32(eth, reg_map->pdma.irq_status),
2323 				 mtk_r32(eth, reg_map->pdma.irq_mask));
2324 		}
2325 
2326 		if (rx_done_total == budget)
2327 			return budget;
2328 
2329 	} while (mtk_r32(eth, reg_map->pdma.irq_status) &
2330 		 eth->soc->txrx.rx_irq_done_mask);
2331 
2332 	if (napi_complete_done(napi, rx_done_total))
2333 		mtk_rx_irq_enable(eth, eth->soc->txrx.rx_irq_done_mask);
2334 
2335 	return rx_done_total;
2336 }
2337 
2338 static int mtk_tx_alloc(struct mtk_eth *eth)
2339 {
2340 	const struct mtk_soc_data *soc = eth->soc;
2341 	struct mtk_tx_ring *ring = &eth->tx_ring;
2342 	int i, sz = soc->txrx.txd_size;
2343 	struct mtk_tx_dma_v2 *txd;
2344 	int ring_size;
2345 	u32 ofs, val;
2346 
2347 	if (MTK_HAS_CAPS(soc->caps, MTK_QDMA))
2348 		ring_size = MTK_QDMA_RING_SIZE;
2349 	else
2350 		ring_size = MTK_DMA_SIZE;
2351 
2352 	ring->buf = kcalloc(ring_size, sizeof(*ring->buf),
2353 			       GFP_KERNEL);
2354 	if (!ring->buf)
2355 		goto no_tx_mem;
2356 
2357 	ring->dma = dma_alloc_coherent(eth->dma_dev, ring_size * sz,
2358 				       &ring->phys, GFP_KERNEL);
2359 	if (!ring->dma)
2360 		goto no_tx_mem;
2361 
2362 	for (i = 0; i < ring_size; i++) {
2363 		int next = (i + 1) % ring_size;
2364 		u32 next_ptr = ring->phys + next * sz;
2365 
2366 		txd = ring->dma + i * sz;
2367 		txd->txd2 = next_ptr;
2368 		txd->txd3 = TX_DMA_LS0 | TX_DMA_OWNER_CPU;
2369 		txd->txd4 = 0;
2370 		if (MTK_HAS_CAPS(soc->caps, MTK_NETSYS_V2)) {
2371 			txd->txd5 = 0;
2372 			txd->txd6 = 0;
2373 			txd->txd7 = 0;
2374 			txd->txd8 = 0;
2375 		}
2376 	}
2377 
2378 	/* On MT7688 (PDMA only) this driver uses the ring->dma structs
2379 	 * only as the framework. The real HW descriptors are the PDMA
2380 	 * descriptors in ring->dma_pdma.
2381 	 */
2382 	if (!MTK_HAS_CAPS(soc->caps, MTK_QDMA)) {
2383 		ring->dma_pdma = dma_alloc_coherent(eth->dma_dev, ring_size * sz,
2384 						    &ring->phys_pdma, GFP_KERNEL);
2385 		if (!ring->dma_pdma)
2386 			goto no_tx_mem;
2387 
2388 		for (i = 0; i < ring_size; i++) {
2389 			ring->dma_pdma[i].txd2 = TX_DMA_DESP2_DEF;
2390 			ring->dma_pdma[i].txd4 = 0;
2391 		}
2392 	}
2393 
2394 	ring->dma_size = ring_size;
2395 	atomic_set(&ring->free_count, ring_size - 2);
2396 	ring->next_free = ring->dma;
2397 	ring->last_free = (void *)txd;
2398 	ring->last_free_ptr = (u32)(ring->phys + ((ring_size - 1) * sz));
2399 	ring->thresh = MAX_SKB_FRAGS;
2400 
2401 	/* make sure that all changes to the dma ring are flushed before we
2402 	 * continue
2403 	 */
2404 	wmb();
2405 
2406 	if (MTK_HAS_CAPS(soc->caps, MTK_QDMA)) {
2407 		mtk_w32(eth, ring->phys, soc->reg_map->qdma.ctx_ptr);
2408 		mtk_w32(eth, ring->phys, soc->reg_map->qdma.dtx_ptr);
2409 		mtk_w32(eth,
2410 			ring->phys + ((ring_size - 1) * sz),
2411 			soc->reg_map->qdma.crx_ptr);
2412 		mtk_w32(eth, ring->last_free_ptr, soc->reg_map->qdma.drx_ptr);
2413 
2414 		for (i = 0, ofs = 0; i < MTK_QDMA_NUM_QUEUES; i++) {
2415 			val = (QDMA_RES_THRES << 8) | QDMA_RES_THRES;
2416 			mtk_w32(eth, val, soc->reg_map->qdma.qtx_cfg + ofs);
2417 
2418 			val = MTK_QTX_SCH_MIN_RATE_EN |
2419 			      /* minimum: 10 Mbps */
2420 			      FIELD_PREP(MTK_QTX_SCH_MIN_RATE_MAN, 1) |
2421 			      FIELD_PREP(MTK_QTX_SCH_MIN_RATE_EXP, 4) |
2422 			      MTK_QTX_SCH_LEAKY_BUCKET_SIZE;
2423 			if (!MTK_HAS_CAPS(eth->soc->caps, MTK_NETSYS_V2))
2424 				val |= MTK_QTX_SCH_LEAKY_BUCKET_EN;
2425 			mtk_w32(eth, val, soc->reg_map->qdma.qtx_sch + ofs);
2426 			ofs += MTK_QTX_OFFSET;
2427 		}
2428 		val = MTK_QDMA_TX_SCH_MAX_WFQ | (MTK_QDMA_TX_SCH_MAX_WFQ << 16);
2429 		mtk_w32(eth, val, soc->reg_map->qdma.tx_sch_rate);
2430 		if (MTK_HAS_CAPS(eth->soc->caps, MTK_NETSYS_V2))
2431 			mtk_w32(eth, val, soc->reg_map->qdma.tx_sch_rate + 4);
2432 	} else {
2433 		mtk_w32(eth, ring->phys_pdma, MT7628_TX_BASE_PTR0);
2434 		mtk_w32(eth, ring_size, MT7628_TX_MAX_CNT0);
2435 		mtk_w32(eth, 0, MT7628_TX_CTX_IDX0);
2436 		mtk_w32(eth, MT7628_PST_DTX_IDX0, soc->reg_map->pdma.rst_idx);
2437 	}
2438 
2439 	return 0;
2440 
2441 no_tx_mem:
2442 	return -ENOMEM;
2443 }
2444 
2445 static void mtk_tx_clean(struct mtk_eth *eth)
2446 {
2447 	const struct mtk_soc_data *soc = eth->soc;
2448 	struct mtk_tx_ring *ring = &eth->tx_ring;
2449 	int i;
2450 
2451 	if (ring->buf) {
2452 		for (i = 0; i < ring->dma_size; i++)
2453 			mtk_tx_unmap(eth, &ring->buf[i], NULL, false);
2454 		kfree(ring->buf);
2455 		ring->buf = NULL;
2456 	}
2457 
2458 	if (ring->dma) {
2459 		dma_free_coherent(eth->dma_dev,
2460 				  ring->dma_size * soc->txrx.txd_size,
2461 				  ring->dma, ring->phys);
2462 		ring->dma = NULL;
2463 	}
2464 
2465 	if (ring->dma_pdma) {
2466 		dma_free_coherent(eth->dma_dev,
2467 				  ring->dma_size * soc->txrx.txd_size,
2468 				  ring->dma_pdma, ring->phys_pdma);
2469 		ring->dma_pdma = NULL;
2470 	}
2471 }
2472 
2473 static int mtk_rx_alloc(struct mtk_eth *eth, int ring_no, int rx_flag)
2474 {
2475 	const struct mtk_reg_map *reg_map = eth->soc->reg_map;
2476 	struct mtk_rx_ring *ring;
2477 	int rx_data_len, rx_dma_size;
2478 	int i;
2479 
2480 	if (rx_flag == MTK_RX_FLAGS_QDMA) {
2481 		if (ring_no)
2482 			return -EINVAL;
2483 		ring = &eth->rx_ring_qdma;
2484 	} else {
2485 		ring = &eth->rx_ring[ring_no];
2486 	}
2487 
2488 	if (rx_flag == MTK_RX_FLAGS_HWLRO) {
2489 		rx_data_len = MTK_MAX_LRO_RX_LENGTH;
2490 		rx_dma_size = MTK_HW_LRO_DMA_SIZE;
2491 	} else {
2492 		rx_data_len = ETH_DATA_LEN;
2493 		rx_dma_size = MTK_DMA_SIZE;
2494 	}
2495 
2496 	ring->frag_size = mtk_max_frag_size(rx_data_len);
2497 	ring->buf_size = mtk_max_buf_size(ring->frag_size);
2498 	ring->data = kcalloc(rx_dma_size, sizeof(*ring->data),
2499 			     GFP_KERNEL);
2500 	if (!ring->data)
2501 		return -ENOMEM;
2502 
2503 	if (mtk_page_pool_enabled(eth)) {
2504 		struct page_pool *pp;
2505 
2506 		pp = mtk_create_page_pool(eth, &ring->xdp_q, ring_no,
2507 					  rx_dma_size);
2508 		if (IS_ERR(pp))
2509 			return PTR_ERR(pp);
2510 
2511 		ring->page_pool = pp;
2512 	}
2513 
2514 	ring->dma = dma_alloc_coherent(eth->dma_dev,
2515 				       rx_dma_size * eth->soc->txrx.rxd_size,
2516 				       &ring->phys, GFP_KERNEL);
2517 	if (!ring->dma)
2518 		return -ENOMEM;
2519 
2520 	for (i = 0; i < rx_dma_size; i++) {
2521 		struct mtk_rx_dma_v2 *rxd;
2522 		dma_addr_t dma_addr;
2523 		void *data;
2524 
2525 		rxd = ring->dma + i * eth->soc->txrx.rxd_size;
2526 		if (ring->page_pool) {
2527 			data = mtk_page_pool_get_buff(ring->page_pool,
2528 						      &dma_addr, GFP_KERNEL);
2529 			if (!data)
2530 				return -ENOMEM;
2531 		} else {
2532 			if (ring->frag_size <= PAGE_SIZE)
2533 				data = netdev_alloc_frag(ring->frag_size);
2534 			else
2535 				data = mtk_max_lro_buf_alloc(GFP_KERNEL);
2536 
2537 			if (!data)
2538 				return -ENOMEM;
2539 
2540 			dma_addr = dma_map_single(eth->dma_dev,
2541 				data + NET_SKB_PAD + eth->ip_align,
2542 				ring->buf_size, DMA_FROM_DEVICE);
2543 			if (unlikely(dma_mapping_error(eth->dma_dev,
2544 						       dma_addr))) {
2545 				skb_free_frag(data);
2546 				return -ENOMEM;
2547 			}
2548 		}
2549 		rxd->rxd1 = (unsigned int)dma_addr;
2550 		ring->data[i] = data;
2551 
2552 		if (MTK_HAS_CAPS(eth->soc->caps, MTK_SOC_MT7628))
2553 			rxd->rxd2 = RX_DMA_LSO;
2554 		else
2555 			rxd->rxd2 = RX_DMA_PREP_PLEN0(ring->buf_size);
2556 
2557 		rxd->rxd3 = 0;
2558 		rxd->rxd4 = 0;
2559 		if (MTK_HAS_CAPS(eth->soc->caps, MTK_NETSYS_V2)) {
2560 			rxd->rxd5 = 0;
2561 			rxd->rxd6 = 0;
2562 			rxd->rxd7 = 0;
2563 			rxd->rxd8 = 0;
2564 		}
2565 	}
2566 
2567 	ring->dma_size = rx_dma_size;
2568 	ring->calc_idx_update = false;
2569 	ring->calc_idx = rx_dma_size - 1;
2570 	if (rx_flag == MTK_RX_FLAGS_QDMA)
2571 		ring->crx_idx_reg = reg_map->qdma.qcrx_ptr +
2572 				    ring_no * MTK_QRX_OFFSET;
2573 	else
2574 		ring->crx_idx_reg = reg_map->pdma.pcrx_ptr +
2575 				    ring_no * MTK_QRX_OFFSET;
2576 	/* make sure that all changes to the dma ring are flushed before we
2577 	 * continue
2578 	 */
2579 	wmb();
2580 
2581 	if (rx_flag == MTK_RX_FLAGS_QDMA) {
2582 		mtk_w32(eth, ring->phys,
2583 			reg_map->qdma.rx_ptr + ring_no * MTK_QRX_OFFSET);
2584 		mtk_w32(eth, rx_dma_size,
2585 			reg_map->qdma.rx_cnt_cfg + ring_no * MTK_QRX_OFFSET);
2586 		mtk_w32(eth, MTK_PST_DRX_IDX_CFG(ring_no),
2587 			reg_map->qdma.rst_idx);
2588 	} else {
2589 		mtk_w32(eth, ring->phys,
2590 			reg_map->pdma.rx_ptr + ring_no * MTK_QRX_OFFSET);
2591 		mtk_w32(eth, rx_dma_size,
2592 			reg_map->pdma.rx_cnt_cfg + ring_no * MTK_QRX_OFFSET);
2593 		mtk_w32(eth, MTK_PST_DRX_IDX_CFG(ring_no),
2594 			reg_map->pdma.rst_idx);
2595 	}
2596 	mtk_w32(eth, ring->calc_idx, ring->crx_idx_reg);
2597 
2598 	return 0;
2599 }
2600 
2601 static void mtk_rx_clean(struct mtk_eth *eth, struct mtk_rx_ring *ring)
2602 {
2603 	int i;
2604 
2605 	if (ring->data && ring->dma) {
2606 		for (i = 0; i < ring->dma_size; i++) {
2607 			struct mtk_rx_dma *rxd;
2608 
2609 			if (!ring->data[i])
2610 				continue;
2611 
2612 			rxd = ring->dma + i * eth->soc->txrx.rxd_size;
2613 			if (!rxd->rxd1)
2614 				continue;
2615 
2616 			dma_unmap_single(eth->dma_dev, rxd->rxd1,
2617 					 ring->buf_size, DMA_FROM_DEVICE);
2618 			mtk_rx_put_buff(ring, ring->data[i], false);
2619 		}
2620 		kfree(ring->data);
2621 		ring->data = NULL;
2622 	}
2623 
2624 	if (ring->dma) {
2625 		dma_free_coherent(eth->dma_dev,
2626 				  ring->dma_size * eth->soc->txrx.rxd_size,
2627 				  ring->dma, ring->phys);
2628 		ring->dma = NULL;
2629 	}
2630 
2631 	if (ring->page_pool) {
2632 		if (xdp_rxq_info_is_reg(&ring->xdp_q))
2633 			xdp_rxq_info_unreg(&ring->xdp_q);
2634 		page_pool_destroy(ring->page_pool);
2635 		ring->page_pool = NULL;
2636 	}
2637 }
2638 
2639 static int mtk_hwlro_rx_init(struct mtk_eth *eth)
2640 {
2641 	int i;
2642 	u32 ring_ctrl_dw1 = 0, ring_ctrl_dw2 = 0, ring_ctrl_dw3 = 0;
2643 	u32 lro_ctrl_dw0 = 0, lro_ctrl_dw3 = 0;
2644 
2645 	/* set LRO rings to auto-learn modes */
2646 	ring_ctrl_dw2 |= MTK_RING_AUTO_LERAN_MODE;
2647 
2648 	/* validate LRO ring */
2649 	ring_ctrl_dw2 |= MTK_RING_VLD;
2650 
2651 	/* set AGE timer (unit: 20us) */
2652 	ring_ctrl_dw2 |= MTK_RING_AGE_TIME_H;
2653 	ring_ctrl_dw1 |= MTK_RING_AGE_TIME_L;
2654 
2655 	/* set max AGG timer (unit: 20us) */
2656 	ring_ctrl_dw2 |= MTK_RING_MAX_AGG_TIME;
2657 
2658 	/* set max LRO AGG count */
2659 	ring_ctrl_dw2 |= MTK_RING_MAX_AGG_CNT_L;
2660 	ring_ctrl_dw3 |= MTK_RING_MAX_AGG_CNT_H;
2661 
2662 	for (i = 1; i < MTK_MAX_RX_RING_NUM; i++) {
2663 		mtk_w32(eth, ring_ctrl_dw1, MTK_LRO_CTRL_DW1_CFG(i));
2664 		mtk_w32(eth, ring_ctrl_dw2, MTK_LRO_CTRL_DW2_CFG(i));
2665 		mtk_w32(eth, ring_ctrl_dw3, MTK_LRO_CTRL_DW3_CFG(i));
2666 	}
2667 
2668 	/* IPv4 checksum update enable */
2669 	lro_ctrl_dw0 |= MTK_L3_CKS_UPD_EN;
2670 
2671 	/* switch priority comparison to packet count mode */
2672 	lro_ctrl_dw0 |= MTK_LRO_ALT_PKT_CNT_MODE;
2673 
2674 	/* bandwidth threshold setting */
2675 	mtk_w32(eth, MTK_HW_LRO_BW_THRE, MTK_PDMA_LRO_CTRL_DW2);
2676 
2677 	/* auto-learn score delta setting */
2678 	mtk_w32(eth, MTK_HW_LRO_REPLACE_DELTA, MTK_PDMA_LRO_ALT_SCORE_DELTA);
2679 
2680 	/* set refresh timer for altering flows to 1 sec. (unit: 20us) */
2681 	mtk_w32(eth, (MTK_HW_LRO_TIMER_UNIT << 16) | MTK_HW_LRO_REFRESH_TIME,
2682 		MTK_PDMA_LRO_ALT_REFRESH_TIMER);
2683 
2684 	/* set HW LRO mode & the max aggregation count for rx packets */
2685 	lro_ctrl_dw3 |= MTK_ADMA_MODE | (MTK_HW_LRO_MAX_AGG_CNT & 0xff);
2686 
2687 	/* the minimal remaining room of SDL0 in RXD for lro aggregation */
2688 	lro_ctrl_dw3 |= MTK_LRO_MIN_RXD_SDL;
2689 
2690 	/* enable HW LRO */
2691 	lro_ctrl_dw0 |= MTK_LRO_EN;
2692 
2693 	mtk_w32(eth, lro_ctrl_dw3, MTK_PDMA_LRO_CTRL_DW3);
2694 	mtk_w32(eth, lro_ctrl_dw0, MTK_PDMA_LRO_CTRL_DW0);
2695 
2696 	return 0;
2697 }
2698 
2699 static void mtk_hwlro_rx_uninit(struct mtk_eth *eth)
2700 {
2701 	int i;
2702 	u32 val;
2703 
2704 	/* relinquish lro rings, flush aggregated packets */
2705 	mtk_w32(eth, MTK_LRO_RING_RELINQUISH_REQ, MTK_PDMA_LRO_CTRL_DW0);
2706 
2707 	/* wait for relinquishments done */
2708 	for (i = 0; i < 10; i++) {
2709 		val = mtk_r32(eth, MTK_PDMA_LRO_CTRL_DW0);
2710 		if (val & MTK_LRO_RING_RELINQUISH_DONE) {
2711 			msleep(20);
2712 			continue;
2713 		}
2714 		break;
2715 	}
2716 
2717 	/* invalidate lro rings */
2718 	for (i = 1; i < MTK_MAX_RX_RING_NUM; i++)
2719 		mtk_w32(eth, 0, MTK_LRO_CTRL_DW2_CFG(i));
2720 
2721 	/* disable HW LRO */
2722 	mtk_w32(eth, 0, MTK_PDMA_LRO_CTRL_DW0);
2723 }
2724 
2725 static void mtk_hwlro_val_ipaddr(struct mtk_eth *eth, int idx, __be32 ip)
2726 {
2727 	u32 reg_val;
2728 
2729 	reg_val = mtk_r32(eth, MTK_LRO_CTRL_DW2_CFG(idx));
2730 
2731 	/* invalidate the IP setting */
2732 	mtk_w32(eth, (reg_val & ~MTK_RING_MYIP_VLD), MTK_LRO_CTRL_DW2_CFG(idx));
2733 
2734 	mtk_w32(eth, ip, MTK_LRO_DIP_DW0_CFG(idx));
2735 
2736 	/* validate the IP setting */
2737 	mtk_w32(eth, (reg_val | MTK_RING_MYIP_VLD), MTK_LRO_CTRL_DW2_CFG(idx));
2738 }
2739 
2740 static void mtk_hwlro_inval_ipaddr(struct mtk_eth *eth, int idx)
2741 {
2742 	u32 reg_val;
2743 
2744 	reg_val = mtk_r32(eth, MTK_LRO_CTRL_DW2_CFG(idx));
2745 
2746 	/* invalidate the IP setting */
2747 	mtk_w32(eth, (reg_val & ~MTK_RING_MYIP_VLD), MTK_LRO_CTRL_DW2_CFG(idx));
2748 
2749 	mtk_w32(eth, 0, MTK_LRO_DIP_DW0_CFG(idx));
2750 }
2751 
2752 static int mtk_hwlro_get_ip_cnt(struct mtk_mac *mac)
2753 {
2754 	int cnt = 0;
2755 	int i;
2756 
2757 	for (i = 0; i < MTK_MAX_LRO_IP_CNT; i++) {
2758 		if (mac->hwlro_ip[i])
2759 			cnt++;
2760 	}
2761 
2762 	return cnt;
2763 }
2764 
2765 static int mtk_hwlro_add_ipaddr(struct net_device *dev,
2766 				struct ethtool_rxnfc *cmd)
2767 {
2768 	struct ethtool_rx_flow_spec *fsp =
2769 		(struct ethtool_rx_flow_spec *)&cmd->fs;
2770 	struct mtk_mac *mac = netdev_priv(dev);
2771 	struct mtk_eth *eth = mac->hw;
2772 	int hwlro_idx;
2773 
2774 	if ((fsp->flow_type != TCP_V4_FLOW) ||
2775 	    (!fsp->h_u.tcp_ip4_spec.ip4dst) ||
2776 	    (fsp->location > 1))
2777 		return -EINVAL;
2778 
2779 	mac->hwlro_ip[fsp->location] = htonl(fsp->h_u.tcp_ip4_spec.ip4dst);
2780 	hwlro_idx = (mac->id * MTK_MAX_LRO_IP_CNT) + fsp->location;
2781 
2782 	mac->hwlro_ip_cnt = mtk_hwlro_get_ip_cnt(mac);
2783 
2784 	mtk_hwlro_val_ipaddr(eth, hwlro_idx, mac->hwlro_ip[fsp->location]);
2785 
2786 	return 0;
2787 }
2788 
2789 static int mtk_hwlro_del_ipaddr(struct net_device *dev,
2790 				struct ethtool_rxnfc *cmd)
2791 {
2792 	struct ethtool_rx_flow_spec *fsp =
2793 		(struct ethtool_rx_flow_spec *)&cmd->fs;
2794 	struct mtk_mac *mac = netdev_priv(dev);
2795 	struct mtk_eth *eth = mac->hw;
2796 	int hwlro_idx;
2797 
2798 	if (fsp->location > 1)
2799 		return -EINVAL;
2800 
2801 	mac->hwlro_ip[fsp->location] = 0;
2802 	hwlro_idx = (mac->id * MTK_MAX_LRO_IP_CNT) + fsp->location;
2803 
2804 	mac->hwlro_ip_cnt = mtk_hwlro_get_ip_cnt(mac);
2805 
2806 	mtk_hwlro_inval_ipaddr(eth, hwlro_idx);
2807 
2808 	return 0;
2809 }
2810 
2811 static void mtk_hwlro_netdev_disable(struct net_device *dev)
2812 {
2813 	struct mtk_mac *mac = netdev_priv(dev);
2814 	struct mtk_eth *eth = mac->hw;
2815 	int i, hwlro_idx;
2816 
2817 	for (i = 0; i < MTK_MAX_LRO_IP_CNT; i++) {
2818 		mac->hwlro_ip[i] = 0;
2819 		hwlro_idx = (mac->id * MTK_MAX_LRO_IP_CNT) + i;
2820 
2821 		mtk_hwlro_inval_ipaddr(eth, hwlro_idx);
2822 	}
2823 
2824 	mac->hwlro_ip_cnt = 0;
2825 }
2826 
2827 static int mtk_hwlro_get_fdir_entry(struct net_device *dev,
2828 				    struct ethtool_rxnfc *cmd)
2829 {
2830 	struct mtk_mac *mac = netdev_priv(dev);
2831 	struct ethtool_rx_flow_spec *fsp =
2832 		(struct ethtool_rx_flow_spec *)&cmd->fs;
2833 
2834 	if (fsp->location >= ARRAY_SIZE(mac->hwlro_ip))
2835 		return -EINVAL;
2836 
2837 	/* only tcp dst ipv4 is meaningful, others are meaningless */
2838 	fsp->flow_type = TCP_V4_FLOW;
2839 	fsp->h_u.tcp_ip4_spec.ip4dst = ntohl(mac->hwlro_ip[fsp->location]);
2840 	fsp->m_u.tcp_ip4_spec.ip4dst = 0;
2841 
2842 	fsp->h_u.tcp_ip4_spec.ip4src = 0;
2843 	fsp->m_u.tcp_ip4_spec.ip4src = 0xffffffff;
2844 	fsp->h_u.tcp_ip4_spec.psrc = 0;
2845 	fsp->m_u.tcp_ip4_spec.psrc = 0xffff;
2846 	fsp->h_u.tcp_ip4_spec.pdst = 0;
2847 	fsp->m_u.tcp_ip4_spec.pdst = 0xffff;
2848 	fsp->h_u.tcp_ip4_spec.tos = 0;
2849 	fsp->m_u.tcp_ip4_spec.tos = 0xff;
2850 
2851 	return 0;
2852 }
2853 
2854 static int mtk_hwlro_get_fdir_all(struct net_device *dev,
2855 				  struct ethtool_rxnfc *cmd,
2856 				  u32 *rule_locs)
2857 {
2858 	struct mtk_mac *mac = netdev_priv(dev);
2859 	int cnt = 0;
2860 	int i;
2861 
2862 	for (i = 0; i < MTK_MAX_LRO_IP_CNT; i++) {
2863 		if (mac->hwlro_ip[i]) {
2864 			rule_locs[cnt] = i;
2865 			cnt++;
2866 		}
2867 	}
2868 
2869 	cmd->rule_cnt = cnt;
2870 
2871 	return 0;
2872 }
2873 
2874 static netdev_features_t mtk_fix_features(struct net_device *dev,
2875 					  netdev_features_t features)
2876 {
2877 	if (!(features & NETIF_F_LRO)) {
2878 		struct mtk_mac *mac = netdev_priv(dev);
2879 		int ip_cnt = mtk_hwlro_get_ip_cnt(mac);
2880 
2881 		if (ip_cnt) {
2882 			netdev_info(dev, "RX flow is programmed, LRO should keep on\n");
2883 
2884 			features |= NETIF_F_LRO;
2885 		}
2886 	}
2887 
2888 	return features;
2889 }
2890 
2891 static int mtk_set_features(struct net_device *dev, netdev_features_t features)
2892 {
2893 	netdev_features_t diff = dev->features ^ features;
2894 
2895 	if ((diff & NETIF_F_LRO) && !(features & NETIF_F_LRO))
2896 		mtk_hwlro_netdev_disable(dev);
2897 
2898 	return 0;
2899 }
2900 
2901 /* wait for DMA to finish whatever it is doing before we start using it again */
2902 static int mtk_dma_busy_wait(struct mtk_eth *eth)
2903 {
2904 	unsigned int reg;
2905 	int ret;
2906 	u32 val;
2907 
2908 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA))
2909 		reg = eth->soc->reg_map->qdma.glo_cfg;
2910 	else
2911 		reg = eth->soc->reg_map->pdma.glo_cfg;
2912 
2913 	ret = readx_poll_timeout_atomic(__raw_readl, eth->base + reg, val,
2914 					!(val & (MTK_RX_DMA_BUSY | MTK_TX_DMA_BUSY)),
2915 					5, MTK_DMA_BUSY_TIMEOUT_US);
2916 	if (ret)
2917 		dev_err(eth->dev, "DMA init timeout\n");
2918 
2919 	return ret;
2920 }
2921 
2922 static int mtk_dma_init(struct mtk_eth *eth)
2923 {
2924 	int err;
2925 	u32 i;
2926 
2927 	if (mtk_dma_busy_wait(eth))
2928 		return -EBUSY;
2929 
2930 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA)) {
2931 		/* QDMA needs scratch memory for internal reordering of the
2932 		 * descriptors
2933 		 */
2934 		err = mtk_init_fq_dma(eth);
2935 		if (err)
2936 			return err;
2937 	}
2938 
2939 	err = mtk_tx_alloc(eth);
2940 	if (err)
2941 		return err;
2942 
2943 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA)) {
2944 		err = mtk_rx_alloc(eth, 0, MTK_RX_FLAGS_QDMA);
2945 		if (err)
2946 			return err;
2947 	}
2948 
2949 	err = mtk_rx_alloc(eth, 0, MTK_RX_FLAGS_NORMAL);
2950 	if (err)
2951 		return err;
2952 
2953 	if (eth->hwlro) {
2954 		for (i = 1; i < MTK_MAX_RX_RING_NUM; i++) {
2955 			err = mtk_rx_alloc(eth, i, MTK_RX_FLAGS_HWLRO);
2956 			if (err)
2957 				return err;
2958 		}
2959 		err = mtk_hwlro_rx_init(eth);
2960 		if (err)
2961 			return err;
2962 	}
2963 
2964 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA)) {
2965 		/* Enable random early drop and set drop threshold
2966 		 * automatically
2967 		 */
2968 		mtk_w32(eth, FC_THRES_DROP_MODE | FC_THRES_DROP_EN |
2969 			FC_THRES_MIN, eth->soc->reg_map->qdma.fc_th);
2970 		mtk_w32(eth, 0x0, eth->soc->reg_map->qdma.hred);
2971 	}
2972 
2973 	return 0;
2974 }
2975 
2976 static void mtk_dma_free(struct mtk_eth *eth)
2977 {
2978 	const struct mtk_soc_data *soc = eth->soc;
2979 	int i;
2980 
2981 	for (i = 0; i < MTK_MAC_COUNT; i++)
2982 		if (eth->netdev[i])
2983 			netdev_reset_queue(eth->netdev[i]);
2984 	if (eth->scratch_ring) {
2985 		dma_free_coherent(eth->dma_dev,
2986 				  MTK_QDMA_RING_SIZE * soc->txrx.txd_size,
2987 				  eth->scratch_ring, eth->phy_scratch_ring);
2988 		eth->scratch_ring = NULL;
2989 		eth->phy_scratch_ring = 0;
2990 	}
2991 	mtk_tx_clean(eth);
2992 	mtk_rx_clean(eth, &eth->rx_ring[0]);
2993 	mtk_rx_clean(eth, &eth->rx_ring_qdma);
2994 
2995 	if (eth->hwlro) {
2996 		mtk_hwlro_rx_uninit(eth);
2997 		for (i = 1; i < MTK_MAX_RX_RING_NUM; i++)
2998 			mtk_rx_clean(eth, &eth->rx_ring[i]);
2999 	}
3000 
3001 	kfree(eth->scratch_head);
3002 }
3003 
3004 static bool mtk_hw_reset_check(struct mtk_eth *eth)
3005 {
3006 	u32 val = mtk_r32(eth, MTK_INT_STATUS2);
3007 
3008 	return (val & MTK_FE_INT_FQ_EMPTY) || (val & MTK_FE_INT_RFIFO_UF) ||
3009 	       (val & MTK_FE_INT_RFIFO_OV) || (val & MTK_FE_INT_TSO_FAIL) ||
3010 	       (val & MTK_FE_INT_TSO_ALIGN) || (val & MTK_FE_INT_TSO_ILLEGAL);
3011 }
3012 
3013 static void mtk_tx_timeout(struct net_device *dev, unsigned int txqueue)
3014 {
3015 	struct mtk_mac *mac = netdev_priv(dev);
3016 	struct mtk_eth *eth = mac->hw;
3017 
3018 	if (test_bit(MTK_RESETTING, &eth->state))
3019 		return;
3020 
3021 	if (!mtk_hw_reset_check(eth))
3022 		return;
3023 
3024 	eth->netdev[mac->id]->stats.tx_errors++;
3025 	netif_err(eth, tx_err, dev, "transmit timed out\n");
3026 
3027 	schedule_work(&eth->pending_work);
3028 }
3029 
3030 static irqreturn_t mtk_handle_irq_rx(int irq, void *_eth)
3031 {
3032 	struct mtk_eth *eth = _eth;
3033 
3034 	eth->rx_events++;
3035 	if (likely(napi_schedule_prep(&eth->rx_napi))) {
3036 		__napi_schedule(&eth->rx_napi);
3037 		mtk_rx_irq_disable(eth, eth->soc->txrx.rx_irq_done_mask);
3038 	}
3039 
3040 	return IRQ_HANDLED;
3041 }
3042 
3043 static irqreturn_t mtk_handle_irq_tx(int irq, void *_eth)
3044 {
3045 	struct mtk_eth *eth = _eth;
3046 
3047 	eth->tx_events++;
3048 	if (likely(napi_schedule_prep(&eth->tx_napi))) {
3049 		__napi_schedule(&eth->tx_napi);
3050 		mtk_tx_irq_disable(eth, MTK_TX_DONE_INT);
3051 	}
3052 
3053 	return IRQ_HANDLED;
3054 }
3055 
3056 static irqreturn_t mtk_handle_irq(int irq, void *_eth)
3057 {
3058 	struct mtk_eth *eth = _eth;
3059 	const struct mtk_reg_map *reg_map = eth->soc->reg_map;
3060 
3061 	if (mtk_r32(eth, reg_map->pdma.irq_mask) &
3062 	    eth->soc->txrx.rx_irq_done_mask) {
3063 		if (mtk_r32(eth, reg_map->pdma.irq_status) &
3064 		    eth->soc->txrx.rx_irq_done_mask)
3065 			mtk_handle_irq_rx(irq, _eth);
3066 	}
3067 	if (mtk_r32(eth, reg_map->tx_irq_mask) & MTK_TX_DONE_INT) {
3068 		if (mtk_r32(eth, reg_map->tx_irq_status) & MTK_TX_DONE_INT)
3069 			mtk_handle_irq_tx(irq, _eth);
3070 	}
3071 
3072 	return IRQ_HANDLED;
3073 }
3074 
3075 #ifdef CONFIG_NET_POLL_CONTROLLER
3076 static void mtk_poll_controller(struct net_device *dev)
3077 {
3078 	struct mtk_mac *mac = netdev_priv(dev);
3079 	struct mtk_eth *eth = mac->hw;
3080 
3081 	mtk_tx_irq_disable(eth, MTK_TX_DONE_INT);
3082 	mtk_rx_irq_disable(eth, eth->soc->txrx.rx_irq_done_mask);
3083 	mtk_handle_irq_rx(eth->irq[2], dev);
3084 	mtk_tx_irq_enable(eth, MTK_TX_DONE_INT);
3085 	mtk_rx_irq_enable(eth, eth->soc->txrx.rx_irq_done_mask);
3086 }
3087 #endif
3088 
3089 static int mtk_start_dma(struct mtk_eth *eth)
3090 {
3091 	u32 val, rx_2b_offset = (NET_IP_ALIGN == 2) ? MTK_RX_2B_OFFSET : 0;
3092 	const struct mtk_reg_map *reg_map = eth->soc->reg_map;
3093 	int err;
3094 
3095 	err = mtk_dma_init(eth);
3096 	if (err) {
3097 		mtk_dma_free(eth);
3098 		return err;
3099 	}
3100 
3101 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA)) {
3102 		val = mtk_r32(eth, reg_map->qdma.glo_cfg);
3103 		val |= MTK_TX_DMA_EN | MTK_RX_DMA_EN |
3104 		       MTK_TX_BT_32DWORDS | MTK_NDP_CO_PRO |
3105 		       MTK_RX_2B_OFFSET | MTK_TX_WB_DDONE;
3106 
3107 		if (MTK_HAS_CAPS(eth->soc->caps, MTK_NETSYS_V2))
3108 			val |= MTK_MUTLI_CNT | MTK_RESV_BUF |
3109 			       MTK_WCOMP_EN | MTK_DMAD_WR_WDONE |
3110 			       MTK_CHK_DDONE_EN | MTK_LEAKY_BUCKET_EN;
3111 		else
3112 			val |= MTK_RX_BT_32DWORDS;
3113 		mtk_w32(eth, val, reg_map->qdma.glo_cfg);
3114 
3115 		mtk_w32(eth,
3116 			MTK_RX_DMA_EN | rx_2b_offset |
3117 			MTK_RX_BT_32DWORDS | MTK_MULTI_EN,
3118 			reg_map->pdma.glo_cfg);
3119 	} else {
3120 		mtk_w32(eth, MTK_TX_WB_DDONE | MTK_TX_DMA_EN | MTK_RX_DMA_EN |
3121 			MTK_MULTI_EN | MTK_PDMA_SIZE_8DWORDS,
3122 			reg_map->pdma.glo_cfg);
3123 	}
3124 
3125 	return 0;
3126 }
3127 
3128 static void mtk_gdm_config(struct mtk_eth *eth, u32 config)
3129 {
3130 	int i;
3131 
3132 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_SOC_MT7628))
3133 		return;
3134 
3135 	for (i = 0; i < MTK_MAC_COUNT; i++) {
3136 		u32 val = mtk_r32(eth, MTK_GDMA_FWD_CFG(i));
3137 
3138 		/* default setup the forward port to send frame to PDMA */
3139 		val &= ~0xffff;
3140 
3141 		/* Enable RX checksum */
3142 		val |= MTK_GDMA_ICS_EN | MTK_GDMA_TCS_EN | MTK_GDMA_UCS_EN;
3143 
3144 		val |= config;
3145 
3146 		if (eth->netdev[i] && netdev_uses_dsa(eth->netdev[i]))
3147 			val |= MTK_GDMA_SPECIAL_TAG;
3148 
3149 		mtk_w32(eth, val, MTK_GDMA_FWD_CFG(i));
3150 	}
3151 	/* Reset and enable PSE */
3152 	mtk_w32(eth, RST_GL_PSE, MTK_RST_GL);
3153 	mtk_w32(eth, 0, MTK_RST_GL);
3154 }
3155 
3156 
3157 static bool mtk_uses_dsa(struct net_device *dev)
3158 {
3159 #if IS_ENABLED(CONFIG_NET_DSA)
3160 	return netdev_uses_dsa(dev) &&
3161 	       dev->dsa_ptr->tag_ops->proto == DSA_TAG_PROTO_MTK;
3162 #else
3163 	return false;
3164 #endif
3165 }
3166 
3167 static int mtk_device_event(struct notifier_block *n, unsigned long event, void *ptr)
3168 {
3169 	struct mtk_mac *mac = container_of(n, struct mtk_mac, device_notifier);
3170 	struct mtk_eth *eth = mac->hw;
3171 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3172 	struct ethtool_link_ksettings s;
3173 	struct net_device *ldev;
3174 	struct list_head *iter;
3175 	struct dsa_port *dp;
3176 
3177 	if (event != NETDEV_CHANGE)
3178 		return NOTIFY_DONE;
3179 
3180 	netdev_for_each_lower_dev(dev, ldev, iter) {
3181 		if (netdev_priv(ldev) == mac)
3182 			goto found;
3183 	}
3184 
3185 	return NOTIFY_DONE;
3186 
3187 found:
3188 	if (!dsa_slave_dev_check(dev))
3189 		return NOTIFY_DONE;
3190 
3191 	if (__ethtool_get_link_ksettings(dev, &s))
3192 		return NOTIFY_DONE;
3193 
3194 	if (s.base.speed == 0 || s.base.speed == ((__u32)-1))
3195 		return NOTIFY_DONE;
3196 
3197 	dp = dsa_port_from_netdev(dev);
3198 	if (dp->index >= MTK_QDMA_NUM_QUEUES)
3199 		return NOTIFY_DONE;
3200 
3201 	if (mac->speed > 0 && mac->speed <= s.base.speed)
3202 		s.base.speed = 0;
3203 
3204 	mtk_set_queue_speed(eth, dp->index + 3, s.base.speed);
3205 
3206 	return NOTIFY_DONE;
3207 }
3208 
3209 static int mtk_open(struct net_device *dev)
3210 {
3211 	struct mtk_mac *mac = netdev_priv(dev);
3212 	struct mtk_eth *eth = mac->hw;
3213 	int i, err;
3214 
3215 	err = phylink_of_phy_connect(mac->phylink, mac->of_node, 0);
3216 	if (err) {
3217 		netdev_err(dev, "%s: could not attach PHY: %d\n", __func__,
3218 			   err);
3219 		return err;
3220 	}
3221 
3222 	/* we run 2 netdevs on the same dma ring so we only bring it up once */
3223 	if (!refcount_read(&eth->dma_refcnt)) {
3224 		const struct mtk_soc_data *soc = eth->soc;
3225 		u32 gdm_config;
3226 		int i;
3227 
3228 		err = mtk_start_dma(eth);
3229 		if (err) {
3230 			phylink_disconnect_phy(mac->phylink);
3231 			return err;
3232 		}
3233 
3234 		for (i = 0; i < ARRAY_SIZE(eth->ppe); i++)
3235 			mtk_ppe_start(eth->ppe[i]);
3236 
3237 		gdm_config = soc->offload_version ? soc->reg_map->gdma_to_ppe
3238 						  : MTK_GDMA_TO_PDMA;
3239 		mtk_gdm_config(eth, gdm_config);
3240 
3241 		napi_enable(&eth->tx_napi);
3242 		napi_enable(&eth->rx_napi);
3243 		mtk_tx_irq_enable(eth, MTK_TX_DONE_INT);
3244 		mtk_rx_irq_enable(eth, soc->txrx.rx_irq_done_mask);
3245 		refcount_set(&eth->dma_refcnt, 1);
3246 	}
3247 	else
3248 		refcount_inc(&eth->dma_refcnt);
3249 
3250 	phylink_start(mac->phylink);
3251 	netif_tx_start_all_queues(dev);
3252 
3253 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_NETSYS_V2))
3254 		return 0;
3255 
3256 	if (mtk_uses_dsa(dev) && !eth->prog) {
3257 		for (i = 0; i < ARRAY_SIZE(eth->dsa_meta); i++) {
3258 			struct metadata_dst *md_dst = eth->dsa_meta[i];
3259 
3260 			if (md_dst)
3261 				continue;
3262 
3263 			md_dst = metadata_dst_alloc(0, METADATA_HW_PORT_MUX,
3264 						    GFP_KERNEL);
3265 			if (!md_dst)
3266 				return -ENOMEM;
3267 
3268 			md_dst->u.port_info.port_id = i;
3269 			eth->dsa_meta[i] = md_dst;
3270 		}
3271 	} else {
3272 		/* Hardware DSA untagging and VLAN RX offloading need to be
3273 		 * disabled if at least one MAC does not use DSA.
3274 		 */
3275 		u32 val = mtk_r32(eth, MTK_CDMP_IG_CTRL);
3276 
3277 		val &= ~MTK_CDMP_STAG_EN;
3278 		mtk_w32(eth, val, MTK_CDMP_IG_CTRL);
3279 
3280 		mtk_w32(eth, 0, MTK_CDMP_EG_CTRL);
3281 	}
3282 
3283 	return 0;
3284 }
3285 
3286 static void mtk_stop_dma(struct mtk_eth *eth, u32 glo_cfg)
3287 {
3288 	u32 val;
3289 	int i;
3290 
3291 	/* stop the dma engine */
3292 	spin_lock_bh(&eth->page_lock);
3293 	val = mtk_r32(eth, glo_cfg);
3294 	mtk_w32(eth, val & ~(MTK_TX_WB_DDONE | MTK_RX_DMA_EN | MTK_TX_DMA_EN),
3295 		glo_cfg);
3296 	spin_unlock_bh(&eth->page_lock);
3297 
3298 	/* wait for dma stop */
3299 	for (i = 0; i < 10; i++) {
3300 		val = mtk_r32(eth, glo_cfg);
3301 		if (val & (MTK_TX_DMA_BUSY | MTK_RX_DMA_BUSY)) {
3302 			msleep(20);
3303 			continue;
3304 		}
3305 		break;
3306 	}
3307 }
3308 
3309 static int mtk_stop(struct net_device *dev)
3310 {
3311 	struct mtk_mac *mac = netdev_priv(dev);
3312 	struct mtk_eth *eth = mac->hw;
3313 	int i;
3314 
3315 	phylink_stop(mac->phylink);
3316 
3317 	netif_tx_disable(dev);
3318 
3319 	phylink_disconnect_phy(mac->phylink);
3320 
3321 	/* only shutdown DMA if this is the last user */
3322 	if (!refcount_dec_and_test(&eth->dma_refcnt))
3323 		return 0;
3324 
3325 	mtk_gdm_config(eth, MTK_GDMA_DROP_ALL);
3326 
3327 	mtk_tx_irq_disable(eth, MTK_TX_DONE_INT);
3328 	mtk_rx_irq_disable(eth, eth->soc->txrx.rx_irq_done_mask);
3329 	napi_disable(&eth->tx_napi);
3330 	napi_disable(&eth->rx_napi);
3331 
3332 	cancel_work_sync(&eth->rx_dim.work);
3333 	cancel_work_sync(&eth->tx_dim.work);
3334 
3335 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA))
3336 		mtk_stop_dma(eth, eth->soc->reg_map->qdma.glo_cfg);
3337 	mtk_stop_dma(eth, eth->soc->reg_map->pdma.glo_cfg);
3338 
3339 	mtk_dma_free(eth);
3340 
3341 	for (i = 0; i < ARRAY_SIZE(eth->ppe); i++)
3342 		mtk_ppe_stop(eth->ppe[i]);
3343 
3344 	return 0;
3345 }
3346 
3347 static int mtk_xdp_setup(struct net_device *dev, struct bpf_prog *prog,
3348 			 struct netlink_ext_ack *extack)
3349 {
3350 	struct mtk_mac *mac = netdev_priv(dev);
3351 	struct mtk_eth *eth = mac->hw;
3352 	struct bpf_prog *old_prog;
3353 	bool need_update;
3354 
3355 	if (eth->hwlro) {
3356 		NL_SET_ERR_MSG_MOD(extack, "XDP not supported with HWLRO");
3357 		return -EOPNOTSUPP;
3358 	}
3359 
3360 	if (dev->mtu > MTK_PP_MAX_BUF_SIZE) {
3361 		NL_SET_ERR_MSG_MOD(extack, "MTU too large for XDP");
3362 		return -EOPNOTSUPP;
3363 	}
3364 
3365 	need_update = !!eth->prog != !!prog;
3366 	if (netif_running(dev) && need_update)
3367 		mtk_stop(dev);
3368 
3369 	old_prog = rcu_replace_pointer(eth->prog, prog, lockdep_rtnl_is_held());
3370 	if (old_prog)
3371 		bpf_prog_put(old_prog);
3372 
3373 	if (netif_running(dev) && need_update)
3374 		return mtk_open(dev);
3375 
3376 	return 0;
3377 }
3378 
3379 static int mtk_xdp(struct net_device *dev, struct netdev_bpf *xdp)
3380 {
3381 	switch (xdp->command) {
3382 	case XDP_SETUP_PROG:
3383 		return mtk_xdp_setup(dev, xdp->prog, xdp->extack);
3384 	default:
3385 		return -EINVAL;
3386 	}
3387 }
3388 
3389 static void ethsys_reset(struct mtk_eth *eth, u32 reset_bits)
3390 {
3391 	regmap_update_bits(eth->ethsys, ETHSYS_RSTCTRL,
3392 			   reset_bits,
3393 			   reset_bits);
3394 
3395 	usleep_range(1000, 1100);
3396 	regmap_update_bits(eth->ethsys, ETHSYS_RSTCTRL,
3397 			   reset_bits,
3398 			   ~reset_bits);
3399 	mdelay(10);
3400 }
3401 
3402 static void mtk_clk_disable(struct mtk_eth *eth)
3403 {
3404 	int clk;
3405 
3406 	for (clk = MTK_CLK_MAX - 1; clk >= 0; clk--)
3407 		clk_disable_unprepare(eth->clks[clk]);
3408 }
3409 
3410 static int mtk_clk_enable(struct mtk_eth *eth)
3411 {
3412 	int clk, ret;
3413 
3414 	for (clk = 0; clk < MTK_CLK_MAX ; clk++) {
3415 		ret = clk_prepare_enable(eth->clks[clk]);
3416 		if (ret)
3417 			goto err_disable_clks;
3418 	}
3419 
3420 	return 0;
3421 
3422 err_disable_clks:
3423 	while (--clk >= 0)
3424 		clk_disable_unprepare(eth->clks[clk]);
3425 
3426 	return ret;
3427 }
3428 
3429 static void mtk_dim_rx(struct work_struct *work)
3430 {
3431 	struct dim *dim = container_of(work, struct dim, work);
3432 	struct mtk_eth *eth = container_of(dim, struct mtk_eth, rx_dim);
3433 	const struct mtk_reg_map *reg_map = eth->soc->reg_map;
3434 	struct dim_cq_moder cur_profile;
3435 	u32 val, cur;
3436 
3437 	cur_profile = net_dim_get_rx_moderation(eth->rx_dim.mode,
3438 						dim->profile_ix);
3439 	spin_lock_bh(&eth->dim_lock);
3440 
3441 	val = mtk_r32(eth, reg_map->pdma.delay_irq);
3442 	val &= MTK_PDMA_DELAY_TX_MASK;
3443 	val |= MTK_PDMA_DELAY_RX_EN;
3444 
3445 	cur = min_t(u32, DIV_ROUND_UP(cur_profile.usec, 20), MTK_PDMA_DELAY_PTIME_MASK);
3446 	val |= cur << MTK_PDMA_DELAY_RX_PTIME_SHIFT;
3447 
3448 	cur = min_t(u32, cur_profile.pkts, MTK_PDMA_DELAY_PINT_MASK);
3449 	val |= cur << MTK_PDMA_DELAY_RX_PINT_SHIFT;
3450 
3451 	mtk_w32(eth, val, reg_map->pdma.delay_irq);
3452 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA))
3453 		mtk_w32(eth, val, reg_map->qdma.delay_irq);
3454 
3455 	spin_unlock_bh(&eth->dim_lock);
3456 
3457 	dim->state = DIM_START_MEASURE;
3458 }
3459 
3460 static void mtk_dim_tx(struct work_struct *work)
3461 {
3462 	struct dim *dim = container_of(work, struct dim, work);
3463 	struct mtk_eth *eth = container_of(dim, struct mtk_eth, tx_dim);
3464 	const struct mtk_reg_map *reg_map = eth->soc->reg_map;
3465 	struct dim_cq_moder cur_profile;
3466 	u32 val, cur;
3467 
3468 	cur_profile = net_dim_get_tx_moderation(eth->tx_dim.mode,
3469 						dim->profile_ix);
3470 	spin_lock_bh(&eth->dim_lock);
3471 
3472 	val = mtk_r32(eth, reg_map->pdma.delay_irq);
3473 	val &= MTK_PDMA_DELAY_RX_MASK;
3474 	val |= MTK_PDMA_DELAY_TX_EN;
3475 
3476 	cur = min_t(u32, DIV_ROUND_UP(cur_profile.usec, 20), MTK_PDMA_DELAY_PTIME_MASK);
3477 	val |= cur << MTK_PDMA_DELAY_TX_PTIME_SHIFT;
3478 
3479 	cur = min_t(u32, cur_profile.pkts, MTK_PDMA_DELAY_PINT_MASK);
3480 	val |= cur << MTK_PDMA_DELAY_TX_PINT_SHIFT;
3481 
3482 	mtk_w32(eth, val, reg_map->pdma.delay_irq);
3483 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA))
3484 		mtk_w32(eth, val, reg_map->qdma.delay_irq);
3485 
3486 	spin_unlock_bh(&eth->dim_lock);
3487 
3488 	dim->state = DIM_START_MEASURE;
3489 }
3490 
3491 static void mtk_set_mcr_max_rx(struct mtk_mac *mac, u32 val)
3492 {
3493 	struct mtk_eth *eth = mac->hw;
3494 	u32 mcr_cur, mcr_new;
3495 
3496 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_SOC_MT7628))
3497 		return;
3498 
3499 	mcr_cur = mtk_r32(mac->hw, MTK_MAC_MCR(mac->id));
3500 	mcr_new = mcr_cur & ~MAC_MCR_MAX_RX_MASK;
3501 
3502 	if (val <= 1518)
3503 		mcr_new |= MAC_MCR_MAX_RX(MAC_MCR_MAX_RX_1518);
3504 	else if (val <= 1536)
3505 		mcr_new |= MAC_MCR_MAX_RX(MAC_MCR_MAX_RX_1536);
3506 	else if (val <= 1552)
3507 		mcr_new |= MAC_MCR_MAX_RX(MAC_MCR_MAX_RX_1552);
3508 	else
3509 		mcr_new |= MAC_MCR_MAX_RX(MAC_MCR_MAX_RX_2048);
3510 
3511 	if (mcr_new != mcr_cur)
3512 		mtk_w32(mac->hw, mcr_new, MTK_MAC_MCR(mac->id));
3513 }
3514 
3515 static void mtk_hw_reset(struct mtk_eth *eth)
3516 {
3517 	u32 val;
3518 
3519 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_NETSYS_V2)) {
3520 		regmap_write(eth->ethsys, ETHSYS_FE_RST_CHK_IDLE_EN, 0);
3521 		val = RSTCTRL_PPE0_V2;
3522 	} else {
3523 		val = RSTCTRL_PPE0;
3524 	}
3525 
3526 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_RSTCTRL_PPE1))
3527 		val |= RSTCTRL_PPE1;
3528 
3529 	ethsys_reset(eth, RSTCTRL_ETH | RSTCTRL_FE | val);
3530 
3531 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_NETSYS_V2))
3532 		regmap_write(eth->ethsys, ETHSYS_FE_RST_CHK_IDLE_EN,
3533 			     0x3ffffff);
3534 }
3535 
3536 static u32 mtk_hw_reset_read(struct mtk_eth *eth)
3537 {
3538 	u32 val;
3539 
3540 	regmap_read(eth->ethsys, ETHSYS_RSTCTRL, &val);
3541 	return val;
3542 }
3543 
3544 static void mtk_hw_warm_reset(struct mtk_eth *eth)
3545 {
3546 	u32 rst_mask, val;
3547 
3548 	regmap_update_bits(eth->ethsys, ETHSYS_RSTCTRL, RSTCTRL_FE,
3549 			   RSTCTRL_FE);
3550 	if (readx_poll_timeout_atomic(mtk_hw_reset_read, eth, val,
3551 				      val & RSTCTRL_FE, 1, 1000)) {
3552 		dev_err(eth->dev, "warm reset failed\n");
3553 		mtk_hw_reset(eth);
3554 		return;
3555 	}
3556 
3557 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_NETSYS_V2))
3558 		rst_mask = RSTCTRL_ETH | RSTCTRL_PPE0_V2;
3559 	else
3560 		rst_mask = RSTCTRL_ETH | RSTCTRL_PPE0;
3561 
3562 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_RSTCTRL_PPE1))
3563 		rst_mask |= RSTCTRL_PPE1;
3564 
3565 	regmap_update_bits(eth->ethsys, ETHSYS_RSTCTRL, rst_mask, rst_mask);
3566 
3567 	udelay(1);
3568 	val = mtk_hw_reset_read(eth);
3569 	if (!(val & rst_mask))
3570 		dev_err(eth->dev, "warm reset stage0 failed %08x (%08x)\n",
3571 			val, rst_mask);
3572 
3573 	rst_mask |= RSTCTRL_FE;
3574 	regmap_update_bits(eth->ethsys, ETHSYS_RSTCTRL, rst_mask, ~rst_mask);
3575 
3576 	udelay(1);
3577 	val = mtk_hw_reset_read(eth);
3578 	if (val & rst_mask)
3579 		dev_err(eth->dev, "warm reset stage1 failed %08x (%08x)\n",
3580 			val, rst_mask);
3581 }
3582 
3583 static bool mtk_hw_check_dma_hang(struct mtk_eth *eth)
3584 {
3585 	const struct mtk_reg_map *reg_map = eth->soc->reg_map;
3586 	bool gmac1_tx, gmac2_tx, gdm1_tx, gdm2_tx;
3587 	bool oq_hang, cdm1_busy, adma_busy;
3588 	bool wtx_busy, cdm_full, oq_free;
3589 	u32 wdidx, val, gdm1_fc, gdm2_fc;
3590 	bool qfsm_hang, qfwd_hang;
3591 	bool ret = false;
3592 
3593 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_SOC_MT7628))
3594 		return false;
3595 
3596 	/* WDMA sanity checks */
3597 	wdidx = mtk_r32(eth, reg_map->wdma_base[0] + 0xc);
3598 
3599 	val = mtk_r32(eth, reg_map->wdma_base[0] + 0x204);
3600 	wtx_busy = FIELD_GET(MTK_TX_DMA_BUSY, val);
3601 
3602 	val = mtk_r32(eth, reg_map->wdma_base[0] + 0x230);
3603 	cdm_full = !FIELD_GET(MTK_CDM_TXFIFO_RDY, val);
3604 
3605 	oq_free  = (!(mtk_r32(eth, reg_map->pse_oq_sta) & GENMASK(24, 16)) &&
3606 		    !(mtk_r32(eth, reg_map->pse_oq_sta + 0x4) & GENMASK(8, 0)) &&
3607 		    !(mtk_r32(eth, reg_map->pse_oq_sta + 0x10) & GENMASK(24, 16)));
3608 
3609 	if (wdidx == eth->reset.wdidx && wtx_busy && cdm_full && oq_free) {
3610 		if (++eth->reset.wdma_hang_count > 2) {
3611 			eth->reset.wdma_hang_count = 0;
3612 			ret = true;
3613 		}
3614 		goto out;
3615 	}
3616 
3617 	/* QDMA sanity checks */
3618 	qfsm_hang = !!mtk_r32(eth, reg_map->qdma.qtx_cfg + 0x234);
3619 	qfwd_hang = !mtk_r32(eth, reg_map->qdma.qtx_cfg + 0x308);
3620 
3621 	gdm1_tx = FIELD_GET(GENMASK(31, 16), mtk_r32(eth, MTK_FE_GDM1_FSM)) > 0;
3622 	gdm2_tx = FIELD_GET(GENMASK(31, 16), mtk_r32(eth, MTK_FE_GDM2_FSM)) > 0;
3623 	gmac1_tx = FIELD_GET(GENMASK(31, 24), mtk_r32(eth, MTK_MAC_FSM(0))) != 1;
3624 	gmac2_tx = FIELD_GET(GENMASK(31, 24), mtk_r32(eth, MTK_MAC_FSM(1))) != 1;
3625 	gdm1_fc = mtk_r32(eth, reg_map->gdm1_cnt + 0x24);
3626 	gdm2_fc = mtk_r32(eth, reg_map->gdm1_cnt + 0x64);
3627 
3628 	if (qfsm_hang && qfwd_hang &&
3629 	    ((gdm1_tx && gmac1_tx && gdm1_fc < 1) ||
3630 	     (gdm2_tx && gmac2_tx && gdm2_fc < 1))) {
3631 		if (++eth->reset.qdma_hang_count > 2) {
3632 			eth->reset.qdma_hang_count = 0;
3633 			ret = true;
3634 		}
3635 		goto out;
3636 	}
3637 
3638 	/* ADMA sanity checks */
3639 	oq_hang = !!(mtk_r32(eth, reg_map->pse_oq_sta) & GENMASK(8, 0));
3640 	cdm1_busy = !!(mtk_r32(eth, MTK_FE_CDM1_FSM) & GENMASK(31, 16));
3641 	adma_busy = !(mtk_r32(eth, reg_map->pdma.adma_rx_dbg0) & GENMASK(4, 0)) &&
3642 		    !(mtk_r32(eth, reg_map->pdma.adma_rx_dbg0) & BIT(6));
3643 
3644 	if (oq_hang && cdm1_busy && adma_busy) {
3645 		if (++eth->reset.adma_hang_count > 2) {
3646 			eth->reset.adma_hang_count = 0;
3647 			ret = true;
3648 		}
3649 		goto out;
3650 	}
3651 
3652 	eth->reset.wdma_hang_count = 0;
3653 	eth->reset.qdma_hang_count = 0;
3654 	eth->reset.adma_hang_count = 0;
3655 out:
3656 	eth->reset.wdidx = wdidx;
3657 
3658 	return ret;
3659 }
3660 
3661 static void mtk_hw_reset_monitor_work(struct work_struct *work)
3662 {
3663 	struct delayed_work *del_work = to_delayed_work(work);
3664 	struct mtk_eth *eth = container_of(del_work, struct mtk_eth,
3665 					   reset.monitor_work);
3666 
3667 	if (test_bit(MTK_RESETTING, &eth->state))
3668 		goto out;
3669 
3670 	/* DMA stuck checks */
3671 	if (mtk_hw_check_dma_hang(eth))
3672 		schedule_work(&eth->pending_work);
3673 
3674 out:
3675 	schedule_delayed_work(&eth->reset.monitor_work,
3676 			      MTK_DMA_MONITOR_TIMEOUT);
3677 }
3678 
3679 static int mtk_hw_init(struct mtk_eth *eth, bool reset)
3680 {
3681 	u32 dma_mask = ETHSYS_DMA_AG_MAP_PDMA | ETHSYS_DMA_AG_MAP_QDMA |
3682 		       ETHSYS_DMA_AG_MAP_PPE;
3683 	const struct mtk_reg_map *reg_map = eth->soc->reg_map;
3684 	int i, val, ret;
3685 
3686 	if (!reset && test_and_set_bit(MTK_HW_INIT, &eth->state))
3687 		return 0;
3688 
3689 	if (!reset) {
3690 		pm_runtime_enable(eth->dev);
3691 		pm_runtime_get_sync(eth->dev);
3692 
3693 		ret = mtk_clk_enable(eth);
3694 		if (ret)
3695 			goto err_disable_pm;
3696 	}
3697 
3698 	if (eth->ethsys)
3699 		regmap_update_bits(eth->ethsys, ETHSYS_DMA_AG_MAP, dma_mask,
3700 				   of_dma_is_coherent(eth->dma_dev->of_node) * dma_mask);
3701 
3702 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_SOC_MT7628)) {
3703 		ret = device_reset(eth->dev);
3704 		if (ret) {
3705 			dev_err(eth->dev, "MAC reset failed!\n");
3706 			goto err_disable_pm;
3707 		}
3708 
3709 		/* set interrupt delays based on current Net DIM sample */
3710 		mtk_dim_rx(&eth->rx_dim.work);
3711 		mtk_dim_tx(&eth->tx_dim.work);
3712 
3713 		/* disable delay and normal interrupt */
3714 		mtk_tx_irq_disable(eth, ~0);
3715 		mtk_rx_irq_disable(eth, ~0);
3716 
3717 		return 0;
3718 	}
3719 
3720 	msleep(100);
3721 
3722 	if (reset)
3723 		mtk_hw_warm_reset(eth);
3724 	else
3725 		mtk_hw_reset(eth);
3726 
3727 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_NETSYS_V2)) {
3728 		/* Set FE to PDMAv2 if necessary */
3729 		val = mtk_r32(eth, MTK_FE_GLO_MISC);
3730 		mtk_w32(eth,  val | BIT(4), MTK_FE_GLO_MISC);
3731 	}
3732 
3733 	if (eth->pctl) {
3734 		/* Set GE2 driving and slew rate */
3735 		regmap_write(eth->pctl, GPIO_DRV_SEL10, 0xa00);
3736 
3737 		/* set GE2 TDSEL */
3738 		regmap_write(eth->pctl, GPIO_OD33_CTRL8, 0x5);
3739 
3740 		/* set GE2 TUNE */
3741 		regmap_write(eth->pctl, GPIO_BIAS_CTRL, 0x0);
3742 	}
3743 
3744 	/* Set linkdown as the default for each GMAC. Its own MCR would be set
3745 	 * up with the more appropriate value when mtk_mac_config call is being
3746 	 * invoked.
3747 	 */
3748 	for (i = 0; i < MTK_MAC_COUNT; i++) {
3749 		struct net_device *dev = eth->netdev[i];
3750 
3751 		mtk_w32(eth, MAC_MCR_FORCE_LINK_DOWN, MTK_MAC_MCR(i));
3752 		if (dev) {
3753 			struct mtk_mac *mac = netdev_priv(dev);
3754 
3755 			mtk_set_mcr_max_rx(mac, dev->mtu + MTK_RX_ETH_HLEN);
3756 		}
3757 	}
3758 
3759 	/* Indicates CDM to parse the MTK special tag from CPU
3760 	 * which also is working out for untag packets.
3761 	 */
3762 	val = mtk_r32(eth, MTK_CDMQ_IG_CTRL);
3763 	mtk_w32(eth, val | MTK_CDMQ_STAG_EN, MTK_CDMQ_IG_CTRL);
3764 	if (!MTK_HAS_CAPS(eth->soc->caps, MTK_NETSYS_V2)) {
3765 		val = mtk_r32(eth, MTK_CDMP_IG_CTRL);
3766 		mtk_w32(eth, val | MTK_CDMP_STAG_EN, MTK_CDMP_IG_CTRL);
3767 
3768 		mtk_w32(eth, 1, MTK_CDMP_EG_CTRL);
3769 	}
3770 
3771 	/* set interrupt delays based on current Net DIM sample */
3772 	mtk_dim_rx(&eth->rx_dim.work);
3773 	mtk_dim_tx(&eth->tx_dim.work);
3774 
3775 	/* disable delay and normal interrupt */
3776 	mtk_tx_irq_disable(eth, ~0);
3777 	mtk_rx_irq_disable(eth, ~0);
3778 
3779 	/* FE int grouping */
3780 	mtk_w32(eth, MTK_TX_DONE_INT, reg_map->pdma.int_grp);
3781 	mtk_w32(eth, eth->soc->txrx.rx_irq_done_mask, reg_map->pdma.int_grp + 4);
3782 	mtk_w32(eth, MTK_TX_DONE_INT, reg_map->qdma.int_grp);
3783 	mtk_w32(eth, eth->soc->txrx.rx_irq_done_mask, reg_map->qdma.int_grp + 4);
3784 	mtk_w32(eth, 0x21021000, MTK_FE_INT_GRP);
3785 
3786 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_NETSYS_V2)) {
3787 		/* PSE should not drop port8 and port9 packets from WDMA Tx */
3788 		mtk_w32(eth, 0x00000300, PSE_DROP_CFG);
3789 
3790 		/* PSE should drop packets to port 8/9 on WDMA Rx ring full */
3791 		mtk_w32(eth, 0x00000300, PSE_PPE0_DROP);
3792 
3793 		/* PSE Free Queue Flow Control  */
3794 		mtk_w32(eth, 0x01fa01f4, PSE_FQFC_CFG2);
3795 
3796 		/* PSE config input queue threshold */
3797 		mtk_w32(eth, 0x001a000e, PSE_IQ_REV(1));
3798 		mtk_w32(eth, 0x01ff001a, PSE_IQ_REV(2));
3799 		mtk_w32(eth, 0x000e01ff, PSE_IQ_REV(3));
3800 		mtk_w32(eth, 0x000e000e, PSE_IQ_REV(4));
3801 		mtk_w32(eth, 0x000e000e, PSE_IQ_REV(5));
3802 		mtk_w32(eth, 0x000e000e, PSE_IQ_REV(6));
3803 		mtk_w32(eth, 0x000e000e, PSE_IQ_REV(7));
3804 		mtk_w32(eth, 0x000e000e, PSE_IQ_REV(8));
3805 
3806 		/* PSE config output queue threshold */
3807 		mtk_w32(eth, 0x000f000a, PSE_OQ_TH(1));
3808 		mtk_w32(eth, 0x001a000f, PSE_OQ_TH(2));
3809 		mtk_w32(eth, 0x000f001a, PSE_OQ_TH(3));
3810 		mtk_w32(eth, 0x01ff000f, PSE_OQ_TH(4));
3811 		mtk_w32(eth, 0x000f000f, PSE_OQ_TH(5));
3812 		mtk_w32(eth, 0x0006000f, PSE_OQ_TH(6));
3813 		mtk_w32(eth, 0x00060006, PSE_OQ_TH(7));
3814 		mtk_w32(eth, 0x00060006, PSE_OQ_TH(8));
3815 
3816 		/* GDM and CDM Threshold */
3817 		mtk_w32(eth, 0x00000004, MTK_GDM2_THRES);
3818 		mtk_w32(eth, 0x00000004, MTK_CDMW0_THRES);
3819 		mtk_w32(eth, 0x00000004, MTK_CDMW1_THRES);
3820 		mtk_w32(eth, 0x00000004, MTK_CDME0_THRES);
3821 		mtk_w32(eth, 0x00000004, MTK_CDME1_THRES);
3822 		mtk_w32(eth, 0x00000004, MTK_CDMM_THRES);
3823 	}
3824 
3825 	return 0;
3826 
3827 err_disable_pm:
3828 	if (!reset) {
3829 		pm_runtime_put_sync(eth->dev);
3830 		pm_runtime_disable(eth->dev);
3831 	}
3832 
3833 	return ret;
3834 }
3835 
3836 static int mtk_hw_deinit(struct mtk_eth *eth)
3837 {
3838 	if (!test_and_clear_bit(MTK_HW_INIT, &eth->state))
3839 		return 0;
3840 
3841 	mtk_clk_disable(eth);
3842 
3843 	pm_runtime_put_sync(eth->dev);
3844 	pm_runtime_disable(eth->dev);
3845 
3846 	return 0;
3847 }
3848 
3849 static void mtk_uninit(struct net_device *dev)
3850 {
3851 	struct mtk_mac *mac = netdev_priv(dev);
3852 	struct mtk_eth *eth = mac->hw;
3853 
3854 	phylink_disconnect_phy(mac->phylink);
3855 	mtk_tx_irq_disable(eth, ~0);
3856 	mtk_rx_irq_disable(eth, ~0);
3857 }
3858 
3859 static int mtk_change_mtu(struct net_device *dev, int new_mtu)
3860 {
3861 	int length = new_mtu + MTK_RX_ETH_HLEN;
3862 	struct mtk_mac *mac = netdev_priv(dev);
3863 	struct mtk_eth *eth = mac->hw;
3864 
3865 	if (rcu_access_pointer(eth->prog) &&
3866 	    length > MTK_PP_MAX_BUF_SIZE) {
3867 		netdev_err(dev, "Invalid MTU for XDP mode\n");
3868 		return -EINVAL;
3869 	}
3870 
3871 	mtk_set_mcr_max_rx(mac, length);
3872 	dev->mtu = new_mtu;
3873 
3874 	return 0;
3875 }
3876 
3877 static int mtk_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3878 {
3879 	struct mtk_mac *mac = netdev_priv(dev);
3880 
3881 	switch (cmd) {
3882 	case SIOCGMIIPHY:
3883 	case SIOCGMIIREG:
3884 	case SIOCSMIIREG:
3885 		return phylink_mii_ioctl(mac->phylink, ifr, cmd);
3886 	default:
3887 		break;
3888 	}
3889 
3890 	return -EOPNOTSUPP;
3891 }
3892 
3893 static void mtk_prepare_for_reset(struct mtk_eth *eth)
3894 {
3895 	u32 val;
3896 	int i;
3897 
3898 	/* disabe FE P3 and P4 */
3899 	val = mtk_r32(eth, MTK_FE_GLO_CFG) | MTK_FE_LINK_DOWN_P3;
3900 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_RSTCTRL_PPE1))
3901 		val |= MTK_FE_LINK_DOWN_P4;
3902 	mtk_w32(eth, val, MTK_FE_GLO_CFG);
3903 
3904 	/* adjust PPE configurations to prepare for reset */
3905 	for (i = 0; i < ARRAY_SIZE(eth->ppe); i++)
3906 		mtk_ppe_prepare_reset(eth->ppe[i]);
3907 
3908 	/* disable NETSYS interrupts */
3909 	mtk_w32(eth, 0, MTK_FE_INT_ENABLE);
3910 
3911 	/* force link down GMAC */
3912 	for (i = 0; i < 2; i++) {
3913 		val = mtk_r32(eth, MTK_MAC_MCR(i)) & ~MAC_MCR_FORCE_LINK;
3914 		mtk_w32(eth, val, MTK_MAC_MCR(i));
3915 	}
3916 }
3917 
3918 static void mtk_pending_work(struct work_struct *work)
3919 {
3920 	struct mtk_eth *eth = container_of(work, struct mtk_eth, pending_work);
3921 	unsigned long restart = 0;
3922 	u32 val;
3923 	int i;
3924 
3925 	rtnl_lock();
3926 	set_bit(MTK_RESETTING, &eth->state);
3927 
3928 	mtk_prepare_for_reset(eth);
3929 	mtk_wed_fe_reset();
3930 	/* Run again reset preliminary configuration in order to avoid any
3931 	 * possible race during FE reset since it can run releasing RTNL lock.
3932 	 */
3933 	mtk_prepare_for_reset(eth);
3934 
3935 	/* stop all devices to make sure that dma is properly shut down */
3936 	for (i = 0; i < MTK_MAC_COUNT; i++) {
3937 		if (!eth->netdev[i] || !netif_running(eth->netdev[i]))
3938 			continue;
3939 
3940 		mtk_stop(eth->netdev[i]);
3941 		__set_bit(i, &restart);
3942 	}
3943 
3944 	usleep_range(15000, 16000);
3945 
3946 	if (eth->dev->pins)
3947 		pinctrl_select_state(eth->dev->pins->p,
3948 				     eth->dev->pins->default_state);
3949 	mtk_hw_init(eth, true);
3950 
3951 	/* restart DMA and enable IRQs */
3952 	for (i = 0; i < MTK_MAC_COUNT; i++) {
3953 		if (!test_bit(i, &restart))
3954 			continue;
3955 
3956 		if (mtk_open(eth->netdev[i])) {
3957 			netif_alert(eth, ifup, eth->netdev[i],
3958 				    "Driver up/down cycle failed\n");
3959 			dev_close(eth->netdev[i]);
3960 		}
3961 	}
3962 
3963 	/* enabe FE P3 and P4 */
3964 	val = mtk_r32(eth, MTK_FE_GLO_CFG) & ~MTK_FE_LINK_DOWN_P3;
3965 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_RSTCTRL_PPE1))
3966 		val &= ~MTK_FE_LINK_DOWN_P4;
3967 	mtk_w32(eth, val, MTK_FE_GLO_CFG);
3968 
3969 	clear_bit(MTK_RESETTING, &eth->state);
3970 
3971 	mtk_wed_fe_reset_complete();
3972 
3973 	rtnl_unlock();
3974 }
3975 
3976 static int mtk_free_dev(struct mtk_eth *eth)
3977 {
3978 	int i;
3979 
3980 	for (i = 0; i < MTK_MAC_COUNT; i++) {
3981 		if (!eth->netdev[i])
3982 			continue;
3983 		free_netdev(eth->netdev[i]);
3984 	}
3985 
3986 	for (i = 0; i < ARRAY_SIZE(eth->dsa_meta); i++) {
3987 		if (!eth->dsa_meta[i])
3988 			break;
3989 		metadata_dst_free(eth->dsa_meta[i]);
3990 	}
3991 
3992 	return 0;
3993 }
3994 
3995 static int mtk_unreg_dev(struct mtk_eth *eth)
3996 {
3997 	int i;
3998 
3999 	for (i = 0; i < MTK_MAC_COUNT; i++) {
4000 		struct mtk_mac *mac;
4001 		if (!eth->netdev[i])
4002 			continue;
4003 		mac = netdev_priv(eth->netdev[i]);
4004 		if (MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA))
4005 			unregister_netdevice_notifier(&mac->device_notifier);
4006 		unregister_netdev(eth->netdev[i]);
4007 	}
4008 
4009 	return 0;
4010 }
4011 
4012 static void mtk_sgmii_destroy(struct mtk_eth *eth)
4013 {
4014 	int i;
4015 
4016 	for (i = 0; i < MTK_MAX_DEVS; i++)
4017 		mtk_pcs_lynxi_destroy(eth->sgmii_pcs[i]);
4018 }
4019 
4020 static int mtk_cleanup(struct mtk_eth *eth)
4021 {
4022 	mtk_sgmii_destroy(eth);
4023 	mtk_unreg_dev(eth);
4024 	mtk_free_dev(eth);
4025 	cancel_work_sync(&eth->pending_work);
4026 	cancel_delayed_work_sync(&eth->reset.monitor_work);
4027 
4028 	return 0;
4029 }
4030 
4031 static int mtk_get_link_ksettings(struct net_device *ndev,
4032 				  struct ethtool_link_ksettings *cmd)
4033 {
4034 	struct mtk_mac *mac = netdev_priv(ndev);
4035 
4036 	if (unlikely(test_bit(MTK_RESETTING, &mac->hw->state)))
4037 		return -EBUSY;
4038 
4039 	return phylink_ethtool_ksettings_get(mac->phylink, cmd);
4040 }
4041 
4042 static int mtk_set_link_ksettings(struct net_device *ndev,
4043 				  const struct ethtool_link_ksettings *cmd)
4044 {
4045 	struct mtk_mac *mac = netdev_priv(ndev);
4046 
4047 	if (unlikely(test_bit(MTK_RESETTING, &mac->hw->state)))
4048 		return -EBUSY;
4049 
4050 	return phylink_ethtool_ksettings_set(mac->phylink, cmd);
4051 }
4052 
4053 static void mtk_get_drvinfo(struct net_device *dev,
4054 			    struct ethtool_drvinfo *info)
4055 {
4056 	struct mtk_mac *mac = netdev_priv(dev);
4057 
4058 	strscpy(info->driver, mac->hw->dev->driver->name, sizeof(info->driver));
4059 	strscpy(info->bus_info, dev_name(mac->hw->dev), sizeof(info->bus_info));
4060 	info->n_stats = ARRAY_SIZE(mtk_ethtool_stats);
4061 }
4062 
4063 static u32 mtk_get_msglevel(struct net_device *dev)
4064 {
4065 	struct mtk_mac *mac = netdev_priv(dev);
4066 
4067 	return mac->hw->msg_enable;
4068 }
4069 
4070 static void mtk_set_msglevel(struct net_device *dev, u32 value)
4071 {
4072 	struct mtk_mac *mac = netdev_priv(dev);
4073 
4074 	mac->hw->msg_enable = value;
4075 }
4076 
4077 static int mtk_nway_reset(struct net_device *dev)
4078 {
4079 	struct mtk_mac *mac = netdev_priv(dev);
4080 
4081 	if (unlikely(test_bit(MTK_RESETTING, &mac->hw->state)))
4082 		return -EBUSY;
4083 
4084 	if (!mac->phylink)
4085 		return -ENOTSUPP;
4086 
4087 	return phylink_ethtool_nway_reset(mac->phylink);
4088 }
4089 
4090 static void mtk_get_strings(struct net_device *dev, u32 stringset, u8 *data)
4091 {
4092 	int i;
4093 
4094 	switch (stringset) {
4095 	case ETH_SS_STATS: {
4096 		struct mtk_mac *mac = netdev_priv(dev);
4097 
4098 		for (i = 0; i < ARRAY_SIZE(mtk_ethtool_stats); i++) {
4099 			memcpy(data, mtk_ethtool_stats[i].str, ETH_GSTRING_LEN);
4100 			data += ETH_GSTRING_LEN;
4101 		}
4102 		if (mtk_page_pool_enabled(mac->hw))
4103 			page_pool_ethtool_stats_get_strings(data);
4104 		break;
4105 	}
4106 	default:
4107 		break;
4108 	}
4109 }
4110 
4111 static int mtk_get_sset_count(struct net_device *dev, int sset)
4112 {
4113 	switch (sset) {
4114 	case ETH_SS_STATS: {
4115 		int count = ARRAY_SIZE(mtk_ethtool_stats);
4116 		struct mtk_mac *mac = netdev_priv(dev);
4117 
4118 		if (mtk_page_pool_enabled(mac->hw))
4119 			count += page_pool_ethtool_stats_get_count();
4120 		return count;
4121 	}
4122 	default:
4123 		return -EOPNOTSUPP;
4124 	}
4125 }
4126 
4127 static void mtk_ethtool_pp_stats(struct mtk_eth *eth, u64 *data)
4128 {
4129 	struct page_pool_stats stats = {};
4130 	int i;
4131 
4132 	for (i = 0; i < ARRAY_SIZE(eth->rx_ring); i++) {
4133 		struct mtk_rx_ring *ring = &eth->rx_ring[i];
4134 
4135 		if (!ring->page_pool)
4136 			continue;
4137 
4138 		page_pool_get_stats(ring->page_pool, &stats);
4139 	}
4140 	page_pool_ethtool_stats_get(data, &stats);
4141 }
4142 
4143 static void mtk_get_ethtool_stats(struct net_device *dev,
4144 				  struct ethtool_stats *stats, u64 *data)
4145 {
4146 	struct mtk_mac *mac = netdev_priv(dev);
4147 	struct mtk_hw_stats *hwstats = mac->hw_stats;
4148 	u64 *data_src, *data_dst;
4149 	unsigned int start;
4150 	int i;
4151 
4152 	if (unlikely(test_bit(MTK_RESETTING, &mac->hw->state)))
4153 		return;
4154 
4155 	if (netif_running(dev) && netif_device_present(dev)) {
4156 		if (spin_trylock_bh(&hwstats->stats_lock)) {
4157 			mtk_stats_update_mac(mac);
4158 			spin_unlock_bh(&hwstats->stats_lock);
4159 		}
4160 	}
4161 
4162 	data_src = (u64 *)hwstats;
4163 
4164 	do {
4165 		data_dst = data;
4166 		start = u64_stats_fetch_begin(&hwstats->syncp);
4167 
4168 		for (i = 0; i < ARRAY_SIZE(mtk_ethtool_stats); i++)
4169 			*data_dst++ = *(data_src + mtk_ethtool_stats[i].offset);
4170 		if (mtk_page_pool_enabled(mac->hw))
4171 			mtk_ethtool_pp_stats(mac->hw, data_dst);
4172 	} while (u64_stats_fetch_retry(&hwstats->syncp, start));
4173 }
4174 
4175 static int mtk_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
4176 			 u32 *rule_locs)
4177 {
4178 	int ret = -EOPNOTSUPP;
4179 
4180 	switch (cmd->cmd) {
4181 	case ETHTOOL_GRXRINGS:
4182 		if (dev->hw_features & NETIF_F_LRO) {
4183 			cmd->data = MTK_MAX_RX_RING_NUM;
4184 			ret = 0;
4185 		}
4186 		break;
4187 	case ETHTOOL_GRXCLSRLCNT:
4188 		if (dev->hw_features & NETIF_F_LRO) {
4189 			struct mtk_mac *mac = netdev_priv(dev);
4190 
4191 			cmd->rule_cnt = mac->hwlro_ip_cnt;
4192 			ret = 0;
4193 		}
4194 		break;
4195 	case ETHTOOL_GRXCLSRULE:
4196 		if (dev->hw_features & NETIF_F_LRO)
4197 			ret = mtk_hwlro_get_fdir_entry(dev, cmd);
4198 		break;
4199 	case ETHTOOL_GRXCLSRLALL:
4200 		if (dev->hw_features & NETIF_F_LRO)
4201 			ret = mtk_hwlro_get_fdir_all(dev, cmd,
4202 						     rule_locs);
4203 		break;
4204 	default:
4205 		break;
4206 	}
4207 
4208 	return ret;
4209 }
4210 
4211 static int mtk_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
4212 {
4213 	int ret = -EOPNOTSUPP;
4214 
4215 	switch (cmd->cmd) {
4216 	case ETHTOOL_SRXCLSRLINS:
4217 		if (dev->hw_features & NETIF_F_LRO)
4218 			ret = mtk_hwlro_add_ipaddr(dev, cmd);
4219 		break;
4220 	case ETHTOOL_SRXCLSRLDEL:
4221 		if (dev->hw_features & NETIF_F_LRO)
4222 			ret = mtk_hwlro_del_ipaddr(dev, cmd);
4223 		break;
4224 	default:
4225 		break;
4226 	}
4227 
4228 	return ret;
4229 }
4230 
4231 static u16 mtk_select_queue(struct net_device *dev, struct sk_buff *skb,
4232 			    struct net_device *sb_dev)
4233 {
4234 	struct mtk_mac *mac = netdev_priv(dev);
4235 	unsigned int queue = 0;
4236 
4237 	if (netdev_uses_dsa(dev))
4238 		queue = skb_get_queue_mapping(skb) + 3;
4239 	else
4240 		queue = mac->id;
4241 
4242 	if (queue >= dev->num_tx_queues)
4243 		queue = 0;
4244 
4245 	return queue;
4246 }
4247 
4248 static const struct ethtool_ops mtk_ethtool_ops = {
4249 	.get_link_ksettings	= mtk_get_link_ksettings,
4250 	.set_link_ksettings	= mtk_set_link_ksettings,
4251 	.get_drvinfo		= mtk_get_drvinfo,
4252 	.get_msglevel		= mtk_get_msglevel,
4253 	.set_msglevel		= mtk_set_msglevel,
4254 	.nway_reset		= mtk_nway_reset,
4255 	.get_link		= ethtool_op_get_link,
4256 	.get_strings		= mtk_get_strings,
4257 	.get_sset_count		= mtk_get_sset_count,
4258 	.get_ethtool_stats	= mtk_get_ethtool_stats,
4259 	.get_rxnfc		= mtk_get_rxnfc,
4260 	.set_rxnfc              = mtk_set_rxnfc,
4261 };
4262 
4263 static const struct net_device_ops mtk_netdev_ops = {
4264 	.ndo_uninit		= mtk_uninit,
4265 	.ndo_open		= mtk_open,
4266 	.ndo_stop		= mtk_stop,
4267 	.ndo_start_xmit		= mtk_start_xmit,
4268 	.ndo_set_mac_address	= mtk_set_mac_address,
4269 	.ndo_validate_addr	= eth_validate_addr,
4270 	.ndo_eth_ioctl		= mtk_do_ioctl,
4271 	.ndo_change_mtu		= mtk_change_mtu,
4272 	.ndo_tx_timeout		= mtk_tx_timeout,
4273 	.ndo_get_stats64        = mtk_get_stats64,
4274 	.ndo_fix_features	= mtk_fix_features,
4275 	.ndo_set_features	= mtk_set_features,
4276 #ifdef CONFIG_NET_POLL_CONTROLLER
4277 	.ndo_poll_controller	= mtk_poll_controller,
4278 #endif
4279 	.ndo_setup_tc		= mtk_eth_setup_tc,
4280 	.ndo_bpf		= mtk_xdp,
4281 	.ndo_xdp_xmit		= mtk_xdp_xmit,
4282 	.ndo_select_queue	= mtk_select_queue,
4283 };
4284 
4285 static int mtk_add_mac(struct mtk_eth *eth, struct device_node *np)
4286 {
4287 	const __be32 *_id = of_get_property(np, "reg", NULL);
4288 	phy_interface_t phy_mode;
4289 	struct phylink *phylink;
4290 	struct mtk_mac *mac;
4291 	int id, err;
4292 	int txqs = 1;
4293 	u32 val;
4294 
4295 	if (!_id) {
4296 		dev_err(eth->dev, "missing mac id\n");
4297 		return -EINVAL;
4298 	}
4299 
4300 	id = be32_to_cpup(_id);
4301 	if (id >= MTK_MAC_COUNT) {
4302 		dev_err(eth->dev, "%d is not a valid mac id\n", id);
4303 		return -EINVAL;
4304 	}
4305 
4306 	if (eth->netdev[id]) {
4307 		dev_err(eth->dev, "duplicate mac id found: %d\n", id);
4308 		return -EINVAL;
4309 	}
4310 
4311 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA))
4312 		txqs = MTK_QDMA_NUM_QUEUES;
4313 
4314 	eth->netdev[id] = alloc_etherdev_mqs(sizeof(*mac), txqs, 1);
4315 	if (!eth->netdev[id]) {
4316 		dev_err(eth->dev, "alloc_etherdev failed\n");
4317 		return -ENOMEM;
4318 	}
4319 	mac = netdev_priv(eth->netdev[id]);
4320 	eth->mac[id] = mac;
4321 	mac->id = id;
4322 	mac->hw = eth;
4323 	mac->of_node = np;
4324 
4325 	err = of_get_ethdev_address(mac->of_node, eth->netdev[id]);
4326 	if (err == -EPROBE_DEFER)
4327 		return err;
4328 
4329 	if (err) {
4330 		/* If the mac address is invalid, use random mac address */
4331 		eth_hw_addr_random(eth->netdev[id]);
4332 		dev_err(eth->dev, "generated random MAC address %pM\n",
4333 			eth->netdev[id]->dev_addr);
4334 	}
4335 
4336 	memset(mac->hwlro_ip, 0, sizeof(mac->hwlro_ip));
4337 	mac->hwlro_ip_cnt = 0;
4338 
4339 	mac->hw_stats = devm_kzalloc(eth->dev,
4340 				     sizeof(*mac->hw_stats),
4341 				     GFP_KERNEL);
4342 	if (!mac->hw_stats) {
4343 		dev_err(eth->dev, "failed to allocate counter memory\n");
4344 		err = -ENOMEM;
4345 		goto free_netdev;
4346 	}
4347 	spin_lock_init(&mac->hw_stats->stats_lock);
4348 	u64_stats_init(&mac->hw_stats->syncp);
4349 	mac->hw_stats->reg_offset = id * MTK_STAT_OFFSET;
4350 
4351 	/* phylink create */
4352 	err = of_get_phy_mode(np, &phy_mode);
4353 	if (err) {
4354 		dev_err(eth->dev, "incorrect phy-mode\n");
4355 		goto free_netdev;
4356 	}
4357 
4358 	/* mac config is not set */
4359 	mac->interface = PHY_INTERFACE_MODE_NA;
4360 	mac->speed = SPEED_UNKNOWN;
4361 
4362 	mac->phylink_config.dev = &eth->netdev[id]->dev;
4363 	mac->phylink_config.type = PHYLINK_NETDEV;
4364 	/* This driver makes use of state->speed in mac_config */
4365 	mac->phylink_config.legacy_pre_march2020 = true;
4366 	mac->phylink_config.mac_capabilities = MAC_ASYM_PAUSE | MAC_SYM_PAUSE |
4367 		MAC_10 | MAC_100 | MAC_1000 | MAC_2500FD;
4368 
4369 	__set_bit(PHY_INTERFACE_MODE_MII,
4370 		  mac->phylink_config.supported_interfaces);
4371 	__set_bit(PHY_INTERFACE_MODE_GMII,
4372 		  mac->phylink_config.supported_interfaces);
4373 
4374 	if (MTK_HAS_CAPS(mac->hw->soc->caps, MTK_RGMII))
4375 		phy_interface_set_rgmii(mac->phylink_config.supported_interfaces);
4376 
4377 	if (MTK_HAS_CAPS(mac->hw->soc->caps, MTK_TRGMII) && !mac->id)
4378 		__set_bit(PHY_INTERFACE_MODE_TRGMII,
4379 			  mac->phylink_config.supported_interfaces);
4380 
4381 	/* TRGMII is not permitted on MT7621 if using DDR2 */
4382 	if (MTK_HAS_CAPS(mac->hw->soc->caps, MTK_GMAC1_TRGMII) &&
4383 	    MTK_HAS_CAPS(mac->hw->soc->caps, MTK_TRGMII_MT7621_CLK)) {
4384 		regmap_read(eth->ethsys, ETHSYS_SYSCFG, &val);
4385 		if (val & SYSCFG_DRAM_TYPE_DDR2)
4386 			__clear_bit(PHY_INTERFACE_MODE_TRGMII,
4387 				    mac->phylink_config.supported_interfaces);
4388 	}
4389 
4390 	if (MTK_HAS_CAPS(mac->hw->soc->caps, MTK_SGMII)) {
4391 		__set_bit(PHY_INTERFACE_MODE_SGMII,
4392 			  mac->phylink_config.supported_interfaces);
4393 		__set_bit(PHY_INTERFACE_MODE_1000BASEX,
4394 			  mac->phylink_config.supported_interfaces);
4395 		__set_bit(PHY_INTERFACE_MODE_2500BASEX,
4396 			  mac->phylink_config.supported_interfaces);
4397 	}
4398 
4399 	phylink = phylink_create(&mac->phylink_config,
4400 				 of_fwnode_handle(mac->of_node),
4401 				 phy_mode, &mtk_phylink_ops);
4402 	if (IS_ERR(phylink)) {
4403 		err = PTR_ERR(phylink);
4404 		goto free_netdev;
4405 	}
4406 
4407 	mac->phylink = phylink;
4408 
4409 	SET_NETDEV_DEV(eth->netdev[id], eth->dev);
4410 	eth->netdev[id]->watchdog_timeo = 5 * HZ;
4411 	eth->netdev[id]->netdev_ops = &mtk_netdev_ops;
4412 	eth->netdev[id]->base_addr = (unsigned long)eth->base;
4413 
4414 	eth->netdev[id]->hw_features = eth->soc->hw_features;
4415 	if (eth->hwlro)
4416 		eth->netdev[id]->hw_features |= NETIF_F_LRO;
4417 
4418 	eth->netdev[id]->vlan_features = eth->soc->hw_features &
4419 		~NETIF_F_HW_VLAN_CTAG_TX;
4420 	eth->netdev[id]->features |= eth->soc->hw_features;
4421 	eth->netdev[id]->ethtool_ops = &mtk_ethtool_ops;
4422 
4423 	eth->netdev[id]->irq = eth->irq[0];
4424 	eth->netdev[id]->dev.of_node = np;
4425 
4426 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_SOC_MT7628))
4427 		eth->netdev[id]->max_mtu = MTK_MAX_RX_LENGTH - MTK_RX_ETH_HLEN;
4428 	else
4429 		eth->netdev[id]->max_mtu = MTK_MAX_RX_LENGTH_2K - MTK_RX_ETH_HLEN;
4430 
4431 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA)) {
4432 		mac->device_notifier.notifier_call = mtk_device_event;
4433 		register_netdevice_notifier(&mac->device_notifier);
4434 	}
4435 
4436 	if (mtk_page_pool_enabled(eth))
4437 		eth->netdev[id]->xdp_features = NETDEV_XDP_ACT_BASIC |
4438 						NETDEV_XDP_ACT_REDIRECT |
4439 						NETDEV_XDP_ACT_NDO_XMIT |
4440 						NETDEV_XDP_ACT_NDO_XMIT_SG;
4441 
4442 	return 0;
4443 
4444 free_netdev:
4445 	free_netdev(eth->netdev[id]);
4446 	return err;
4447 }
4448 
4449 void mtk_eth_set_dma_device(struct mtk_eth *eth, struct device *dma_dev)
4450 {
4451 	struct net_device *dev, *tmp;
4452 	LIST_HEAD(dev_list);
4453 	int i;
4454 
4455 	rtnl_lock();
4456 
4457 	for (i = 0; i < MTK_MAC_COUNT; i++) {
4458 		dev = eth->netdev[i];
4459 
4460 		if (!dev || !(dev->flags & IFF_UP))
4461 			continue;
4462 
4463 		list_add_tail(&dev->close_list, &dev_list);
4464 	}
4465 
4466 	dev_close_many(&dev_list, false);
4467 
4468 	eth->dma_dev = dma_dev;
4469 
4470 	list_for_each_entry_safe(dev, tmp, &dev_list, close_list) {
4471 		list_del_init(&dev->close_list);
4472 		dev_open(dev, NULL);
4473 	}
4474 
4475 	rtnl_unlock();
4476 }
4477 
4478 static int mtk_sgmii_init(struct mtk_eth *eth)
4479 {
4480 	struct device_node *np;
4481 	struct regmap *regmap;
4482 	u32 flags;
4483 	int i;
4484 
4485 	for (i = 0; i < MTK_MAX_DEVS; i++) {
4486 		np = of_parse_phandle(eth->dev->of_node, "mediatek,sgmiisys", i);
4487 		if (!np)
4488 			break;
4489 
4490 		regmap = syscon_node_to_regmap(np);
4491 		flags = 0;
4492 		if (of_property_read_bool(np, "mediatek,pnswap"))
4493 			flags |= MTK_SGMII_FLAG_PN_SWAP;
4494 
4495 		of_node_put(np);
4496 
4497 		if (IS_ERR(regmap))
4498 			return PTR_ERR(regmap);
4499 
4500 		eth->sgmii_pcs[i] = mtk_pcs_lynxi_create(eth->dev, regmap,
4501 							 eth->soc->ana_rgc3,
4502 							 flags);
4503 	}
4504 
4505 	return 0;
4506 }
4507 
4508 static int mtk_probe(struct platform_device *pdev)
4509 {
4510 	struct resource *res = NULL;
4511 	struct device_node *mac_np;
4512 	struct mtk_eth *eth;
4513 	int err, i;
4514 
4515 	eth = devm_kzalloc(&pdev->dev, sizeof(*eth), GFP_KERNEL);
4516 	if (!eth)
4517 		return -ENOMEM;
4518 
4519 	eth->soc = of_device_get_match_data(&pdev->dev);
4520 
4521 	eth->dev = &pdev->dev;
4522 	eth->dma_dev = &pdev->dev;
4523 	eth->base = devm_platform_ioremap_resource(pdev, 0);
4524 	if (IS_ERR(eth->base))
4525 		return PTR_ERR(eth->base);
4526 
4527 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_SOC_MT7628))
4528 		eth->ip_align = NET_IP_ALIGN;
4529 
4530 	spin_lock_init(&eth->page_lock);
4531 	spin_lock_init(&eth->tx_irq_lock);
4532 	spin_lock_init(&eth->rx_irq_lock);
4533 	spin_lock_init(&eth->dim_lock);
4534 
4535 	eth->rx_dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
4536 	INIT_WORK(&eth->rx_dim.work, mtk_dim_rx);
4537 	INIT_DELAYED_WORK(&eth->reset.monitor_work, mtk_hw_reset_monitor_work);
4538 
4539 	eth->tx_dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
4540 	INIT_WORK(&eth->tx_dim.work, mtk_dim_tx);
4541 
4542 	if (!MTK_HAS_CAPS(eth->soc->caps, MTK_SOC_MT7628)) {
4543 		eth->ethsys = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
4544 							      "mediatek,ethsys");
4545 		if (IS_ERR(eth->ethsys)) {
4546 			dev_err(&pdev->dev, "no ethsys regmap found\n");
4547 			return PTR_ERR(eth->ethsys);
4548 		}
4549 	}
4550 
4551 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_INFRA)) {
4552 		eth->infra = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
4553 							     "mediatek,infracfg");
4554 		if (IS_ERR(eth->infra)) {
4555 			dev_err(&pdev->dev, "no infracfg regmap found\n");
4556 			return PTR_ERR(eth->infra);
4557 		}
4558 	}
4559 
4560 	if (of_dma_is_coherent(pdev->dev.of_node)) {
4561 		struct regmap *cci;
4562 
4563 		cci = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
4564 						      "cci-control-port");
4565 		/* enable CPU/bus coherency */
4566 		if (!IS_ERR(cci))
4567 			regmap_write(cci, 0, 3);
4568 	}
4569 
4570 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_SGMII)) {
4571 		err = mtk_sgmii_init(eth);
4572 
4573 		if (err)
4574 			return err;
4575 	}
4576 
4577 	if (eth->soc->required_pctl) {
4578 		eth->pctl = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
4579 							    "mediatek,pctl");
4580 		if (IS_ERR(eth->pctl)) {
4581 			dev_err(&pdev->dev, "no pctl regmap found\n");
4582 			err = PTR_ERR(eth->pctl);
4583 			goto err_destroy_sgmii;
4584 		}
4585 	}
4586 
4587 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_NETSYS_V2)) {
4588 		res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4589 		if (!res) {
4590 			err = -EINVAL;
4591 			goto err_destroy_sgmii;
4592 		}
4593 	}
4594 
4595 	if (eth->soc->offload_version) {
4596 		for (i = 0;; i++) {
4597 			struct device_node *np;
4598 			phys_addr_t wdma_phy;
4599 			u32 wdma_base;
4600 
4601 			if (i >= ARRAY_SIZE(eth->soc->reg_map->wdma_base))
4602 				break;
4603 
4604 			np = of_parse_phandle(pdev->dev.of_node,
4605 					      "mediatek,wed", i);
4606 			if (!np)
4607 				break;
4608 
4609 			wdma_base = eth->soc->reg_map->wdma_base[i];
4610 			wdma_phy = res ? res->start + wdma_base : 0;
4611 			mtk_wed_add_hw(np, eth, eth->base + wdma_base,
4612 				       wdma_phy, i);
4613 		}
4614 	}
4615 
4616 	for (i = 0; i < 3; i++) {
4617 		if (MTK_HAS_CAPS(eth->soc->caps, MTK_SHARED_INT) && i > 0)
4618 			eth->irq[i] = eth->irq[0];
4619 		else
4620 			eth->irq[i] = platform_get_irq(pdev, i);
4621 		if (eth->irq[i] < 0) {
4622 			dev_err(&pdev->dev, "no IRQ%d resource found\n", i);
4623 			err = -ENXIO;
4624 			goto err_wed_exit;
4625 		}
4626 	}
4627 	for (i = 0; i < ARRAY_SIZE(eth->clks); i++) {
4628 		eth->clks[i] = devm_clk_get(eth->dev,
4629 					    mtk_clks_source_name[i]);
4630 		if (IS_ERR(eth->clks[i])) {
4631 			if (PTR_ERR(eth->clks[i]) == -EPROBE_DEFER) {
4632 				err = -EPROBE_DEFER;
4633 				goto err_wed_exit;
4634 			}
4635 			if (eth->soc->required_clks & BIT(i)) {
4636 				dev_err(&pdev->dev, "clock %s not found\n",
4637 					mtk_clks_source_name[i]);
4638 				err = -EINVAL;
4639 				goto err_wed_exit;
4640 			}
4641 			eth->clks[i] = NULL;
4642 		}
4643 	}
4644 
4645 	eth->msg_enable = netif_msg_init(mtk_msg_level, MTK_DEFAULT_MSG_ENABLE);
4646 	INIT_WORK(&eth->pending_work, mtk_pending_work);
4647 
4648 	err = mtk_hw_init(eth, false);
4649 	if (err)
4650 		goto err_wed_exit;
4651 
4652 	eth->hwlro = MTK_HAS_CAPS(eth->soc->caps, MTK_HWLRO);
4653 
4654 	for_each_child_of_node(pdev->dev.of_node, mac_np) {
4655 		if (!of_device_is_compatible(mac_np,
4656 					     "mediatek,eth-mac"))
4657 			continue;
4658 
4659 		if (!of_device_is_available(mac_np))
4660 			continue;
4661 
4662 		err = mtk_add_mac(eth, mac_np);
4663 		if (err) {
4664 			of_node_put(mac_np);
4665 			goto err_deinit_hw;
4666 		}
4667 	}
4668 
4669 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_SHARED_INT)) {
4670 		err = devm_request_irq(eth->dev, eth->irq[0],
4671 				       mtk_handle_irq, 0,
4672 				       dev_name(eth->dev), eth);
4673 	} else {
4674 		err = devm_request_irq(eth->dev, eth->irq[1],
4675 				       mtk_handle_irq_tx, 0,
4676 				       dev_name(eth->dev), eth);
4677 		if (err)
4678 			goto err_free_dev;
4679 
4680 		err = devm_request_irq(eth->dev, eth->irq[2],
4681 				       mtk_handle_irq_rx, 0,
4682 				       dev_name(eth->dev), eth);
4683 	}
4684 	if (err)
4685 		goto err_free_dev;
4686 
4687 	/* No MT7628/88 support yet */
4688 	if (!MTK_HAS_CAPS(eth->soc->caps, MTK_SOC_MT7628)) {
4689 		err = mtk_mdio_init(eth);
4690 		if (err)
4691 			goto err_free_dev;
4692 	}
4693 
4694 	if (eth->soc->offload_version) {
4695 		u32 num_ppe;
4696 
4697 		num_ppe = MTK_HAS_CAPS(eth->soc->caps, MTK_NETSYS_V2) ? 2 : 1;
4698 		num_ppe = min_t(u32, ARRAY_SIZE(eth->ppe), num_ppe);
4699 		for (i = 0; i < num_ppe; i++) {
4700 			u32 ppe_addr = eth->soc->reg_map->ppe_base + i * 0x400;
4701 
4702 			eth->ppe[i] = mtk_ppe_init(eth, eth->base + ppe_addr, i);
4703 
4704 			if (!eth->ppe[i]) {
4705 				err = -ENOMEM;
4706 				goto err_deinit_ppe;
4707 			}
4708 		}
4709 
4710 		err = mtk_eth_offload_init(eth);
4711 		if (err)
4712 			goto err_deinit_ppe;
4713 	}
4714 
4715 	for (i = 0; i < MTK_MAX_DEVS; i++) {
4716 		if (!eth->netdev[i])
4717 			continue;
4718 
4719 		err = register_netdev(eth->netdev[i]);
4720 		if (err) {
4721 			dev_err(eth->dev, "error bringing up device\n");
4722 			goto err_deinit_ppe;
4723 		} else
4724 			netif_info(eth, probe, eth->netdev[i],
4725 				   "mediatek frame engine at 0x%08lx, irq %d\n",
4726 				   eth->netdev[i]->base_addr, eth->irq[0]);
4727 	}
4728 
4729 	/* we run 2 devices on the same DMA ring so we need a dummy device
4730 	 * for NAPI to work
4731 	 */
4732 	init_dummy_netdev(&eth->dummy_dev);
4733 	netif_napi_add(&eth->dummy_dev, &eth->tx_napi, mtk_napi_tx);
4734 	netif_napi_add(&eth->dummy_dev, &eth->rx_napi, mtk_napi_rx);
4735 
4736 	platform_set_drvdata(pdev, eth);
4737 	schedule_delayed_work(&eth->reset.monitor_work,
4738 			      MTK_DMA_MONITOR_TIMEOUT);
4739 
4740 	return 0;
4741 
4742 err_deinit_ppe:
4743 	mtk_ppe_deinit(eth);
4744 	mtk_mdio_cleanup(eth);
4745 err_free_dev:
4746 	mtk_free_dev(eth);
4747 err_deinit_hw:
4748 	mtk_hw_deinit(eth);
4749 err_wed_exit:
4750 	mtk_wed_exit();
4751 err_destroy_sgmii:
4752 	mtk_sgmii_destroy(eth);
4753 
4754 	return err;
4755 }
4756 
4757 static int mtk_remove(struct platform_device *pdev)
4758 {
4759 	struct mtk_eth *eth = platform_get_drvdata(pdev);
4760 	struct mtk_mac *mac;
4761 	int i;
4762 
4763 	/* stop all devices to make sure that dma is properly shut down */
4764 	for (i = 0; i < MTK_MAC_COUNT; i++) {
4765 		if (!eth->netdev[i])
4766 			continue;
4767 		mtk_stop(eth->netdev[i]);
4768 		mac = netdev_priv(eth->netdev[i]);
4769 		phylink_disconnect_phy(mac->phylink);
4770 	}
4771 
4772 	mtk_wed_exit();
4773 	mtk_hw_deinit(eth);
4774 
4775 	netif_napi_del(&eth->tx_napi);
4776 	netif_napi_del(&eth->rx_napi);
4777 	mtk_cleanup(eth);
4778 	mtk_mdio_cleanup(eth);
4779 
4780 	return 0;
4781 }
4782 
4783 static const struct mtk_soc_data mt2701_data = {
4784 	.reg_map = &mtk_reg_map,
4785 	.caps = MT7623_CAPS | MTK_HWLRO,
4786 	.hw_features = MTK_HW_FEATURES,
4787 	.required_clks = MT7623_CLKS_BITMAP,
4788 	.required_pctl = true,
4789 	.txrx = {
4790 		.txd_size = sizeof(struct mtk_tx_dma),
4791 		.rxd_size = sizeof(struct mtk_rx_dma),
4792 		.rx_irq_done_mask = MTK_RX_DONE_INT,
4793 		.rx_dma_l4_valid = RX_DMA_L4_VALID,
4794 		.dma_max_len = MTK_TX_DMA_BUF_LEN,
4795 		.dma_len_offset = 16,
4796 	},
4797 };
4798 
4799 static const struct mtk_soc_data mt7621_data = {
4800 	.reg_map = &mtk_reg_map,
4801 	.caps = MT7621_CAPS,
4802 	.hw_features = MTK_HW_FEATURES,
4803 	.required_clks = MT7621_CLKS_BITMAP,
4804 	.required_pctl = false,
4805 	.offload_version = 1,
4806 	.hash_offset = 2,
4807 	.foe_entry_size = sizeof(struct mtk_foe_entry) - 16,
4808 	.txrx = {
4809 		.txd_size = sizeof(struct mtk_tx_dma),
4810 		.rxd_size = sizeof(struct mtk_rx_dma),
4811 		.rx_irq_done_mask = MTK_RX_DONE_INT,
4812 		.rx_dma_l4_valid = RX_DMA_L4_VALID,
4813 		.dma_max_len = MTK_TX_DMA_BUF_LEN,
4814 		.dma_len_offset = 16,
4815 	},
4816 };
4817 
4818 static const struct mtk_soc_data mt7622_data = {
4819 	.reg_map = &mtk_reg_map,
4820 	.ana_rgc3 = 0x2028,
4821 	.caps = MT7622_CAPS | MTK_HWLRO,
4822 	.hw_features = MTK_HW_FEATURES,
4823 	.required_clks = MT7622_CLKS_BITMAP,
4824 	.required_pctl = false,
4825 	.offload_version = 2,
4826 	.hash_offset = 2,
4827 	.has_accounting = true,
4828 	.foe_entry_size = sizeof(struct mtk_foe_entry) - 16,
4829 	.txrx = {
4830 		.txd_size = sizeof(struct mtk_tx_dma),
4831 		.rxd_size = sizeof(struct mtk_rx_dma),
4832 		.rx_irq_done_mask = MTK_RX_DONE_INT,
4833 		.rx_dma_l4_valid = RX_DMA_L4_VALID,
4834 		.dma_max_len = MTK_TX_DMA_BUF_LEN,
4835 		.dma_len_offset = 16,
4836 	},
4837 };
4838 
4839 static const struct mtk_soc_data mt7623_data = {
4840 	.reg_map = &mtk_reg_map,
4841 	.caps = MT7623_CAPS | MTK_HWLRO,
4842 	.hw_features = MTK_HW_FEATURES,
4843 	.required_clks = MT7623_CLKS_BITMAP,
4844 	.required_pctl = true,
4845 	.offload_version = 1,
4846 	.hash_offset = 2,
4847 	.foe_entry_size = sizeof(struct mtk_foe_entry) - 16,
4848 	.txrx = {
4849 		.txd_size = sizeof(struct mtk_tx_dma),
4850 		.rxd_size = sizeof(struct mtk_rx_dma),
4851 		.rx_irq_done_mask = MTK_RX_DONE_INT,
4852 		.rx_dma_l4_valid = RX_DMA_L4_VALID,
4853 		.dma_max_len = MTK_TX_DMA_BUF_LEN,
4854 		.dma_len_offset = 16,
4855 	},
4856 };
4857 
4858 static const struct mtk_soc_data mt7629_data = {
4859 	.reg_map = &mtk_reg_map,
4860 	.ana_rgc3 = 0x128,
4861 	.caps = MT7629_CAPS | MTK_HWLRO,
4862 	.hw_features = MTK_HW_FEATURES,
4863 	.required_clks = MT7629_CLKS_BITMAP,
4864 	.required_pctl = false,
4865 	.has_accounting = true,
4866 	.txrx = {
4867 		.txd_size = sizeof(struct mtk_tx_dma),
4868 		.rxd_size = sizeof(struct mtk_rx_dma),
4869 		.rx_irq_done_mask = MTK_RX_DONE_INT,
4870 		.rx_dma_l4_valid = RX_DMA_L4_VALID,
4871 		.dma_max_len = MTK_TX_DMA_BUF_LEN,
4872 		.dma_len_offset = 16,
4873 	},
4874 };
4875 
4876 static const struct mtk_soc_data mt7981_data = {
4877 	.reg_map = &mt7986_reg_map,
4878 	.ana_rgc3 = 0x128,
4879 	.caps = MT7981_CAPS,
4880 	.hw_features = MTK_HW_FEATURES,
4881 	.required_clks = MT7981_CLKS_BITMAP,
4882 	.required_pctl = false,
4883 	.offload_version = 2,
4884 	.hash_offset = 4,
4885 	.foe_entry_size = sizeof(struct mtk_foe_entry),
4886 	.has_accounting = true,
4887 	.txrx = {
4888 		.txd_size = sizeof(struct mtk_tx_dma_v2),
4889 		.rxd_size = sizeof(struct mtk_rx_dma_v2),
4890 		.rx_irq_done_mask = MTK_RX_DONE_INT_V2,
4891 		.rx_dma_l4_valid = RX_DMA_L4_VALID_V2,
4892 		.dma_max_len = MTK_TX_DMA_BUF_LEN_V2,
4893 		.dma_len_offset = 8,
4894 	},
4895 };
4896 
4897 static const struct mtk_soc_data mt7986_data = {
4898 	.reg_map = &mt7986_reg_map,
4899 	.ana_rgc3 = 0x128,
4900 	.caps = MT7986_CAPS,
4901 	.hw_features = MTK_HW_FEATURES,
4902 	.required_clks = MT7986_CLKS_BITMAP,
4903 	.required_pctl = false,
4904 	.offload_version = 2,
4905 	.hash_offset = 4,
4906 	.foe_entry_size = sizeof(struct mtk_foe_entry),
4907 	.has_accounting = true,
4908 	.txrx = {
4909 		.txd_size = sizeof(struct mtk_tx_dma_v2),
4910 		.rxd_size = sizeof(struct mtk_rx_dma_v2),
4911 		.rx_irq_done_mask = MTK_RX_DONE_INT_V2,
4912 		.rx_dma_l4_valid = RX_DMA_L4_VALID_V2,
4913 		.dma_max_len = MTK_TX_DMA_BUF_LEN_V2,
4914 		.dma_len_offset = 8,
4915 	},
4916 };
4917 
4918 static const struct mtk_soc_data rt5350_data = {
4919 	.reg_map = &mt7628_reg_map,
4920 	.caps = MT7628_CAPS,
4921 	.hw_features = MTK_HW_FEATURES_MT7628,
4922 	.required_clks = MT7628_CLKS_BITMAP,
4923 	.required_pctl = false,
4924 	.txrx = {
4925 		.txd_size = sizeof(struct mtk_tx_dma),
4926 		.rxd_size = sizeof(struct mtk_rx_dma),
4927 		.rx_irq_done_mask = MTK_RX_DONE_INT,
4928 		.rx_dma_l4_valid = RX_DMA_L4_VALID_PDMA,
4929 		.dma_max_len = MTK_TX_DMA_BUF_LEN,
4930 		.dma_len_offset = 16,
4931 	},
4932 };
4933 
4934 const struct of_device_id of_mtk_match[] = {
4935 	{ .compatible = "mediatek,mt2701-eth", .data = &mt2701_data},
4936 	{ .compatible = "mediatek,mt7621-eth", .data = &mt7621_data},
4937 	{ .compatible = "mediatek,mt7622-eth", .data = &mt7622_data},
4938 	{ .compatible = "mediatek,mt7623-eth", .data = &mt7623_data},
4939 	{ .compatible = "mediatek,mt7629-eth", .data = &mt7629_data},
4940 	{ .compatible = "mediatek,mt7981-eth", .data = &mt7981_data},
4941 	{ .compatible = "mediatek,mt7986-eth", .data = &mt7986_data},
4942 	{ .compatible = "ralink,rt5350-eth", .data = &rt5350_data},
4943 	{},
4944 };
4945 MODULE_DEVICE_TABLE(of, of_mtk_match);
4946 
4947 static struct platform_driver mtk_driver = {
4948 	.probe = mtk_probe,
4949 	.remove = mtk_remove,
4950 	.driver = {
4951 		.name = "mtk_soc_eth",
4952 		.of_match_table = of_mtk_match,
4953 	},
4954 };
4955 
4956 module_platform_driver(mtk_driver);
4957 
4958 MODULE_LICENSE("GPL");
4959 MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
4960 MODULE_DESCRIPTION("Ethernet driver for MediaTek SoC");
4961