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