• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/mfd/syscon.h>
13 #include <linux/regmap.h>
14 #include <linux/clk.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/if_vlan.h>
17 #include <linux/reset.h>
18 #include <linux/tcp.h>
19 #include <linux/interrupt.h>
20 #include <linux/pinctrl/devinfo.h>
21 #include <linux/phylink.h>
22 #include <linux/jhash.h>
23 #include <net/dsa.h>
24 
25 #include "mtk_eth_soc.h"
26 
27 static int mtk_msg_level = -1;
28 module_param_named(msg_level, mtk_msg_level, int, 0);
29 MODULE_PARM_DESC(msg_level, "Message level (-1=defaults,0=none,...,16=all)");
30 
31 #define MTK_ETHTOOL_STAT(x) { #x, \
32 			      offsetof(struct mtk_hw_stats, x) / sizeof(u64) }
33 
34 /* strings used by ethtool */
35 static const struct mtk_ethtool_stats {
36 	char str[ETH_GSTRING_LEN];
37 	u32 offset;
38 } mtk_ethtool_stats[] = {
39 	MTK_ETHTOOL_STAT(tx_bytes),
40 	MTK_ETHTOOL_STAT(tx_packets),
41 	MTK_ETHTOOL_STAT(tx_skip),
42 	MTK_ETHTOOL_STAT(tx_collisions),
43 	MTK_ETHTOOL_STAT(rx_bytes),
44 	MTK_ETHTOOL_STAT(rx_packets),
45 	MTK_ETHTOOL_STAT(rx_overflow),
46 	MTK_ETHTOOL_STAT(rx_fcs_errors),
47 	MTK_ETHTOOL_STAT(rx_short_errors),
48 	MTK_ETHTOOL_STAT(rx_long_errors),
49 	MTK_ETHTOOL_STAT(rx_checksum_errors),
50 	MTK_ETHTOOL_STAT(rx_flow_control_packets),
51 };
52 
53 static const char * const mtk_clks_source_name[] = {
54 	"ethif", "sgmiitop", "esw", "gp0", "gp1", "gp2", "fe", "trgpll",
55 	"sgmii_tx250m", "sgmii_rx250m", "sgmii_cdr_ref", "sgmii_cdr_fb",
56 	"sgmii2_tx250m", "sgmii2_rx250m", "sgmii2_cdr_ref", "sgmii2_cdr_fb",
57 	"sgmii_ck", "eth2pll",
58 };
59 
mtk_w32(struct mtk_eth * eth,u32 val,unsigned reg)60 void mtk_w32(struct mtk_eth *eth, u32 val, unsigned reg)
61 {
62 	__raw_writel(val, eth->base + reg);
63 }
64 
mtk_r32(struct mtk_eth * eth,unsigned reg)65 u32 mtk_r32(struct mtk_eth *eth, unsigned reg)
66 {
67 	return __raw_readl(eth->base + reg);
68 }
69 
mtk_m32(struct mtk_eth * eth,u32 mask,u32 set,unsigned reg)70 static u32 mtk_m32(struct mtk_eth *eth, u32 mask, u32 set, unsigned reg)
71 {
72 	u32 val;
73 
74 	val = mtk_r32(eth, reg);
75 	val &= ~mask;
76 	val |= set;
77 	mtk_w32(eth, val, reg);
78 	return reg;
79 }
80 
mtk_mdio_busy_wait(struct mtk_eth * eth)81 static int mtk_mdio_busy_wait(struct mtk_eth *eth)
82 {
83 	unsigned long t_start = jiffies;
84 
85 	while (1) {
86 		if (!(mtk_r32(eth, MTK_PHY_IAC) & PHY_IAC_ACCESS))
87 			return 0;
88 		if (time_after(jiffies, t_start + PHY_IAC_TIMEOUT))
89 			break;
90 		cond_resched();
91 	}
92 
93 	dev_err(eth->dev, "mdio: MDIO timeout\n");
94 	return -ETIMEDOUT;
95 }
96 
_mtk_mdio_write(struct mtk_eth * eth,u32 phy_addr,u32 phy_reg,u32 write_data)97 static int _mtk_mdio_write(struct mtk_eth *eth, u32 phy_addr, u32 phy_reg,
98 			   u32 write_data)
99 {
100 	int ret;
101 
102 	ret = mtk_mdio_busy_wait(eth);
103 	if (ret < 0)
104 		return ret;
105 
106 	mtk_w32(eth, PHY_IAC_ACCESS |
107 		     PHY_IAC_START_C22 |
108 		     PHY_IAC_CMD_WRITE |
109 		     PHY_IAC_REG(phy_reg) |
110 		     PHY_IAC_ADDR(phy_addr) |
111 		     PHY_IAC_DATA(write_data),
112 		MTK_PHY_IAC);
113 
114 	ret = mtk_mdio_busy_wait(eth);
115 	if (ret < 0)
116 		return ret;
117 
118 	return 0;
119 }
120 
_mtk_mdio_read(struct mtk_eth * eth,u32 phy_addr,u32 phy_reg)121 static int _mtk_mdio_read(struct mtk_eth *eth, u32 phy_addr, u32 phy_reg)
122 {
123 	int ret;
124 
125 	ret = mtk_mdio_busy_wait(eth);
126 	if (ret < 0)
127 		return ret;
128 
129 	mtk_w32(eth, PHY_IAC_ACCESS |
130 		     PHY_IAC_START_C22 |
131 		     PHY_IAC_CMD_C22_READ |
132 		     PHY_IAC_REG(phy_reg) |
133 		     PHY_IAC_ADDR(phy_addr),
134 		MTK_PHY_IAC);
135 
136 	ret = mtk_mdio_busy_wait(eth);
137 	if (ret < 0)
138 		return ret;
139 
140 	return mtk_r32(eth, MTK_PHY_IAC) & PHY_IAC_DATA_MASK;
141 }
142 
mtk_mdio_write(struct mii_bus * bus,int phy_addr,int phy_reg,u16 val)143 static int mtk_mdio_write(struct mii_bus *bus, int phy_addr,
144 			  int phy_reg, u16 val)
145 {
146 	struct mtk_eth *eth = bus->priv;
147 
148 	return _mtk_mdio_write(eth, phy_addr, phy_reg, val);
149 }
150 
mtk_mdio_read(struct mii_bus * bus,int phy_addr,int phy_reg)151 static int mtk_mdio_read(struct mii_bus *bus, int phy_addr, int phy_reg)
152 {
153 	struct mtk_eth *eth = bus->priv;
154 
155 	return _mtk_mdio_read(eth, phy_addr, phy_reg);
156 }
157 
mt7621_gmac0_rgmii_adjust(struct mtk_eth * eth,phy_interface_t interface)158 static int mt7621_gmac0_rgmii_adjust(struct mtk_eth *eth,
159 				     phy_interface_t interface)
160 {
161 	u32 val;
162 
163 	/* Check DDR memory type.
164 	 * Currently TRGMII mode with DDR2 memory is not supported.
165 	 */
166 	regmap_read(eth->ethsys, ETHSYS_SYSCFG, &val);
167 	if (interface == PHY_INTERFACE_MODE_TRGMII &&
168 	    val & SYSCFG_DRAM_TYPE_DDR2) {
169 		dev_err(eth->dev,
170 			"TRGMII mode with DDR2 memory is not supported!\n");
171 		return -EOPNOTSUPP;
172 	}
173 
174 	val = (interface == PHY_INTERFACE_MODE_TRGMII) ?
175 		ETHSYS_TRGMII_MT7621_DDR_PLL : 0;
176 
177 	regmap_update_bits(eth->ethsys, ETHSYS_CLKCFG0,
178 			   ETHSYS_TRGMII_MT7621_MASK, val);
179 
180 	return 0;
181 }
182 
mtk_gmac0_rgmii_adjust(struct mtk_eth * eth,phy_interface_t interface,int speed)183 static void mtk_gmac0_rgmii_adjust(struct mtk_eth *eth,
184 				   phy_interface_t interface, int speed)
185 {
186 	u32 val;
187 	int ret;
188 
189 	if (interface == PHY_INTERFACE_MODE_TRGMII) {
190 		mtk_w32(eth, TRGMII_MODE, INTF_MODE);
191 		val = 500000000;
192 		ret = clk_set_rate(eth->clks[MTK_CLK_TRGPLL], val);
193 		if (ret)
194 			dev_err(eth->dev, "Failed to set trgmii pll: %d\n", ret);
195 		return;
196 	}
197 
198 	val = (speed == SPEED_1000) ?
199 		INTF_MODE_RGMII_1000 : INTF_MODE_RGMII_10_100;
200 	mtk_w32(eth, val, INTF_MODE);
201 
202 	regmap_update_bits(eth->ethsys, ETHSYS_CLKCFG0,
203 			   ETHSYS_TRGMII_CLK_SEL362_5,
204 			   ETHSYS_TRGMII_CLK_SEL362_5);
205 
206 	val = (speed == SPEED_1000) ? 250000000 : 500000000;
207 	ret = clk_set_rate(eth->clks[MTK_CLK_TRGPLL], val);
208 	if (ret)
209 		dev_err(eth->dev, "Failed to set trgmii pll: %d\n", ret);
210 
211 	val = (speed == SPEED_1000) ?
212 		RCK_CTRL_RGMII_1000 : RCK_CTRL_RGMII_10_100;
213 	mtk_w32(eth, val, TRGMII_RCK_CTRL);
214 
215 	val = (speed == SPEED_1000) ?
216 		TCK_CTRL_RGMII_1000 : TCK_CTRL_RGMII_10_100;
217 	mtk_w32(eth, val, TRGMII_TCK_CTRL);
218 }
219 
mtk_mac_config(struct phylink_config * config,unsigned int mode,const struct phylink_link_state * state)220 static void mtk_mac_config(struct phylink_config *config, unsigned int mode,
221 			   const struct phylink_link_state *state)
222 {
223 	struct mtk_mac *mac = container_of(config, struct mtk_mac,
224 					   phylink_config);
225 	struct mtk_eth *eth = mac->hw;
226 	u32 mcr_cur, mcr_new, sid, i;
227 	int val, ge_mode, err = 0;
228 
229 	/* MT76x8 has no hardware settings between for the MAC */
230 	if (!MTK_HAS_CAPS(eth->soc->caps, MTK_SOC_MT7628) &&
231 	    mac->interface != state->interface) {
232 		/* Setup soc pin functions */
233 		switch (state->interface) {
234 		case PHY_INTERFACE_MODE_TRGMII:
235 			if (mac->id)
236 				goto err_phy;
237 			if (!MTK_HAS_CAPS(mac->hw->soc->caps,
238 					  MTK_GMAC1_TRGMII))
239 				goto err_phy;
240 			fallthrough;
241 		case PHY_INTERFACE_MODE_RGMII_TXID:
242 		case PHY_INTERFACE_MODE_RGMII_RXID:
243 		case PHY_INTERFACE_MODE_RGMII_ID:
244 		case PHY_INTERFACE_MODE_RGMII:
245 		case PHY_INTERFACE_MODE_MII:
246 		case PHY_INTERFACE_MODE_REVMII:
247 		case PHY_INTERFACE_MODE_RMII:
248 			if (MTK_HAS_CAPS(eth->soc->caps, MTK_RGMII)) {
249 				err = mtk_gmac_rgmii_path_setup(eth, mac->id);
250 				if (err)
251 					goto init_err;
252 			}
253 			break;
254 		case PHY_INTERFACE_MODE_1000BASEX:
255 		case PHY_INTERFACE_MODE_2500BASEX:
256 		case PHY_INTERFACE_MODE_SGMII:
257 			if (MTK_HAS_CAPS(eth->soc->caps, MTK_SGMII)) {
258 				err = mtk_gmac_sgmii_path_setup(eth, mac->id);
259 				if (err)
260 					goto init_err;
261 			}
262 			break;
263 		case PHY_INTERFACE_MODE_GMII:
264 			if (MTK_HAS_CAPS(eth->soc->caps, MTK_GEPHY)) {
265 				err = mtk_gmac_gephy_path_setup(eth, mac->id);
266 				if (err)
267 					goto init_err;
268 			}
269 			break;
270 		default:
271 			goto err_phy;
272 		}
273 
274 		/* Setup clock for 1st gmac */
275 		if (!mac->id && state->interface != PHY_INTERFACE_MODE_SGMII &&
276 		    !phy_interface_mode_is_8023z(state->interface) &&
277 		    MTK_HAS_CAPS(mac->hw->soc->caps, MTK_GMAC1_TRGMII)) {
278 			if (MTK_HAS_CAPS(mac->hw->soc->caps,
279 					 MTK_TRGMII_MT7621_CLK)) {
280 				if (mt7621_gmac0_rgmii_adjust(mac->hw,
281 							      state->interface))
282 					goto err_phy;
283 			} else {
284 				mtk_gmac0_rgmii_adjust(mac->hw,
285 						       state->interface,
286 						       state->speed);
287 
288 				/* mt7623_pad_clk_setup */
289 				for (i = 0 ; i < NUM_TRGMII_CTRL; i++)
290 					mtk_w32(mac->hw,
291 						TD_DM_DRVP(8) | TD_DM_DRVN(8),
292 						TRGMII_TD_ODT(i));
293 
294 				/* Assert/release MT7623 RXC reset */
295 				mtk_m32(mac->hw, 0, RXC_RST | RXC_DQSISEL,
296 					TRGMII_RCK_CTRL);
297 				mtk_m32(mac->hw, RXC_RST, 0, TRGMII_RCK_CTRL);
298 			}
299 		}
300 
301 		ge_mode = 0;
302 		switch (state->interface) {
303 		case PHY_INTERFACE_MODE_MII:
304 		case PHY_INTERFACE_MODE_GMII:
305 			ge_mode = 1;
306 			break;
307 		case PHY_INTERFACE_MODE_REVMII:
308 			ge_mode = 2;
309 			break;
310 		case PHY_INTERFACE_MODE_RMII:
311 			if (mac->id)
312 				goto err_phy;
313 			ge_mode = 3;
314 			break;
315 		default:
316 			break;
317 		}
318 
319 		/* put the gmac into the right mode */
320 		regmap_read(eth->ethsys, ETHSYS_SYSCFG0, &val);
321 		val &= ~SYSCFG0_GE_MODE(SYSCFG0_GE_MASK, mac->id);
322 		val |= SYSCFG0_GE_MODE(ge_mode, mac->id);
323 		regmap_write(eth->ethsys, ETHSYS_SYSCFG0, val);
324 
325 		mac->interface = state->interface;
326 	}
327 
328 	/* SGMII */
329 	if (state->interface == PHY_INTERFACE_MODE_SGMII ||
330 	    phy_interface_mode_is_8023z(state->interface)) {
331 		/* The path GMAC to SGMII will be enabled once the SGMIISYS is
332 		 * being setup done.
333 		 */
334 		regmap_read(eth->ethsys, ETHSYS_SYSCFG0, &val);
335 
336 		regmap_update_bits(eth->ethsys, ETHSYS_SYSCFG0,
337 				   SYSCFG0_SGMII_MASK,
338 				   ~(u32)SYSCFG0_SGMII_MASK);
339 
340 		/* Decide how GMAC and SGMIISYS be mapped */
341 		sid = (MTK_HAS_CAPS(eth->soc->caps, MTK_SHARED_SGMII)) ?
342 		       0 : mac->id;
343 
344 		/* Setup SGMIISYS with the determined property */
345 		if (state->interface != PHY_INTERFACE_MODE_SGMII)
346 			err = mtk_sgmii_setup_mode_force(eth->sgmii, sid,
347 							 state);
348 		else if (phylink_autoneg_inband(mode))
349 			err = mtk_sgmii_setup_mode_an(eth->sgmii, sid);
350 
351 		if (err)
352 			goto init_err;
353 
354 		regmap_update_bits(eth->ethsys, ETHSYS_SYSCFG0,
355 				   SYSCFG0_SGMII_MASK, val);
356 	} else if (phylink_autoneg_inband(mode)) {
357 		dev_err(eth->dev,
358 			"In-band mode not supported in non SGMII mode!\n");
359 		return;
360 	}
361 
362 	/* Setup gmac */
363 	mcr_cur = mtk_r32(mac->hw, MTK_MAC_MCR(mac->id));
364 	mcr_new = mcr_cur;
365 	mcr_new |= MAC_MCR_IPG_CFG | MAC_MCR_FORCE_MODE |
366 		   MAC_MCR_BACKOFF_EN | MAC_MCR_BACKPR_EN | MAC_MCR_FORCE_LINK |
367 		   MAC_MCR_RX_FIFO_CLR_DIS;
368 
369 	/* Only update control register when needed! */
370 	if (mcr_new != mcr_cur)
371 		mtk_w32(mac->hw, mcr_new, MTK_MAC_MCR(mac->id));
372 
373 	return;
374 
375 err_phy:
376 	dev_err(eth->dev, "%s: GMAC%d mode %s not supported!\n", __func__,
377 		mac->id, phy_modes(state->interface));
378 	return;
379 
380 init_err:
381 	dev_err(eth->dev, "%s: GMAC%d mode %s err: %d!\n", __func__,
382 		mac->id, phy_modes(state->interface), err);
383 }
384 
mtk_mac_pcs_get_state(struct phylink_config * config,struct phylink_link_state * state)385 static void mtk_mac_pcs_get_state(struct phylink_config *config,
386 				  struct phylink_link_state *state)
387 {
388 	struct mtk_mac *mac = container_of(config, struct mtk_mac,
389 					   phylink_config);
390 	u32 pmsr = mtk_r32(mac->hw, MTK_MAC_MSR(mac->id));
391 
392 	state->link = (pmsr & MAC_MSR_LINK);
393 	state->duplex = (pmsr & MAC_MSR_DPX) >> 1;
394 
395 	switch (pmsr & (MAC_MSR_SPEED_1000 | MAC_MSR_SPEED_100)) {
396 	case 0:
397 		state->speed = SPEED_10;
398 		break;
399 	case MAC_MSR_SPEED_100:
400 		state->speed = SPEED_100;
401 		break;
402 	case MAC_MSR_SPEED_1000:
403 		state->speed = SPEED_1000;
404 		break;
405 	default:
406 		state->speed = SPEED_UNKNOWN;
407 		break;
408 	}
409 
410 	state->pause &= (MLO_PAUSE_RX | MLO_PAUSE_TX);
411 	if (pmsr & MAC_MSR_RX_FC)
412 		state->pause |= MLO_PAUSE_RX;
413 	if (pmsr & MAC_MSR_TX_FC)
414 		state->pause |= MLO_PAUSE_TX;
415 }
416 
mtk_mac_an_restart(struct phylink_config * config)417 static void mtk_mac_an_restart(struct phylink_config *config)
418 {
419 	struct mtk_mac *mac = container_of(config, struct mtk_mac,
420 					   phylink_config);
421 
422 	mtk_sgmii_restart_an(mac->hw, mac->id);
423 }
424 
mtk_mac_link_down(struct phylink_config * config,unsigned int mode,phy_interface_t interface)425 static void mtk_mac_link_down(struct phylink_config *config, unsigned int mode,
426 			      phy_interface_t interface)
427 {
428 	struct mtk_mac *mac = container_of(config, struct mtk_mac,
429 					   phylink_config);
430 	u32 mcr = mtk_r32(mac->hw, MTK_MAC_MCR(mac->id));
431 
432 	mcr &= ~(MAC_MCR_TX_EN | MAC_MCR_RX_EN);
433 	mtk_w32(mac->hw, mcr, MTK_MAC_MCR(mac->id));
434 }
435 
mtk_mac_link_up(struct phylink_config * config,struct phy_device * phy,unsigned int mode,phy_interface_t interface,int speed,int duplex,bool tx_pause,bool rx_pause)436 static void mtk_mac_link_up(struct phylink_config *config,
437 			    struct phy_device *phy,
438 			    unsigned int mode, phy_interface_t interface,
439 			    int speed, int duplex, bool tx_pause, bool rx_pause)
440 {
441 	struct mtk_mac *mac = container_of(config, struct mtk_mac,
442 					   phylink_config);
443 	u32 mcr = mtk_r32(mac->hw, MTK_MAC_MCR(mac->id));
444 
445 	mcr &= ~(MAC_MCR_SPEED_100 | MAC_MCR_SPEED_1000 |
446 		 MAC_MCR_FORCE_DPX | MAC_MCR_FORCE_TX_FC |
447 		 MAC_MCR_FORCE_RX_FC);
448 
449 	/* Configure speed */
450 	switch (speed) {
451 	case SPEED_2500:
452 	case SPEED_1000:
453 		mcr |= MAC_MCR_SPEED_1000;
454 		break;
455 	case SPEED_100:
456 		mcr |= MAC_MCR_SPEED_100;
457 		break;
458 	}
459 
460 	/* Configure duplex */
461 	if (duplex == DUPLEX_FULL)
462 		mcr |= MAC_MCR_FORCE_DPX;
463 
464 	/* Configure pause modes - phylink will avoid these for half duplex */
465 	if (tx_pause)
466 		mcr |= MAC_MCR_FORCE_TX_FC;
467 	if (rx_pause)
468 		mcr |= MAC_MCR_FORCE_RX_FC;
469 
470 	mcr |= MAC_MCR_TX_EN | MAC_MCR_RX_EN;
471 	mtk_w32(mac->hw, mcr, MTK_MAC_MCR(mac->id));
472 }
473 
mtk_validate(struct phylink_config * config,unsigned long * supported,struct phylink_link_state * state)474 static void mtk_validate(struct phylink_config *config,
475 			 unsigned long *supported,
476 			 struct phylink_link_state *state)
477 {
478 	struct mtk_mac *mac = container_of(config, struct mtk_mac,
479 					   phylink_config);
480 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
481 
482 	if (state->interface != PHY_INTERFACE_MODE_NA &&
483 	    state->interface != PHY_INTERFACE_MODE_MII &&
484 	    state->interface != PHY_INTERFACE_MODE_GMII &&
485 	    !(MTK_HAS_CAPS(mac->hw->soc->caps, MTK_RGMII) &&
486 	      phy_interface_mode_is_rgmii(state->interface)) &&
487 	    !(MTK_HAS_CAPS(mac->hw->soc->caps, MTK_TRGMII) &&
488 	      !mac->id && state->interface == PHY_INTERFACE_MODE_TRGMII) &&
489 	    !(MTK_HAS_CAPS(mac->hw->soc->caps, MTK_SGMII) &&
490 	      (state->interface == PHY_INTERFACE_MODE_SGMII ||
491 	       phy_interface_mode_is_8023z(state->interface)))) {
492 		linkmode_zero(supported);
493 		return;
494 	}
495 
496 	phylink_set_port_modes(mask);
497 	phylink_set(mask, Autoneg);
498 
499 	switch (state->interface) {
500 	case PHY_INTERFACE_MODE_TRGMII:
501 		phylink_set(mask, 1000baseT_Full);
502 		break;
503 	case PHY_INTERFACE_MODE_1000BASEX:
504 	case PHY_INTERFACE_MODE_2500BASEX:
505 		phylink_set(mask, 1000baseX_Full);
506 		phylink_set(mask, 2500baseX_Full);
507 		break;
508 	case PHY_INTERFACE_MODE_GMII:
509 	case PHY_INTERFACE_MODE_RGMII:
510 	case PHY_INTERFACE_MODE_RGMII_ID:
511 	case PHY_INTERFACE_MODE_RGMII_RXID:
512 	case PHY_INTERFACE_MODE_RGMII_TXID:
513 		phylink_set(mask, 1000baseT_Half);
514 		fallthrough;
515 	case PHY_INTERFACE_MODE_SGMII:
516 		phylink_set(mask, 1000baseT_Full);
517 		phylink_set(mask, 1000baseX_Full);
518 		fallthrough;
519 	case PHY_INTERFACE_MODE_MII:
520 	case PHY_INTERFACE_MODE_RMII:
521 	case PHY_INTERFACE_MODE_REVMII:
522 	case PHY_INTERFACE_MODE_NA:
523 	default:
524 		phylink_set(mask, 10baseT_Half);
525 		phylink_set(mask, 10baseT_Full);
526 		phylink_set(mask, 100baseT_Half);
527 		phylink_set(mask, 100baseT_Full);
528 		break;
529 	}
530 
531 	if (state->interface == PHY_INTERFACE_MODE_NA) {
532 		if (MTK_HAS_CAPS(mac->hw->soc->caps, MTK_SGMII)) {
533 			phylink_set(mask, 1000baseT_Full);
534 			phylink_set(mask, 1000baseX_Full);
535 			phylink_set(mask, 2500baseX_Full);
536 		}
537 		if (MTK_HAS_CAPS(mac->hw->soc->caps, MTK_RGMII)) {
538 			phylink_set(mask, 1000baseT_Full);
539 			phylink_set(mask, 1000baseT_Half);
540 			phylink_set(mask, 1000baseX_Full);
541 		}
542 		if (MTK_HAS_CAPS(mac->hw->soc->caps, MTK_GEPHY)) {
543 			phylink_set(mask, 1000baseT_Full);
544 			phylink_set(mask, 1000baseT_Half);
545 		}
546 	}
547 
548 	phylink_set(mask, Pause);
549 	phylink_set(mask, Asym_Pause);
550 
551 	linkmode_and(supported, supported, mask);
552 	linkmode_and(state->advertising, state->advertising, mask);
553 
554 	/* We can only operate at 2500BaseX or 1000BaseX. If requested
555 	 * to advertise both, only report advertising at 2500BaseX.
556 	 */
557 	phylink_helper_basex_speed(state);
558 }
559 
560 static const struct phylink_mac_ops mtk_phylink_ops = {
561 	.validate = mtk_validate,
562 	.mac_pcs_get_state = mtk_mac_pcs_get_state,
563 	.mac_an_restart = mtk_mac_an_restart,
564 	.mac_config = mtk_mac_config,
565 	.mac_link_down = mtk_mac_link_down,
566 	.mac_link_up = mtk_mac_link_up,
567 };
568 
mtk_mdio_init(struct mtk_eth * eth)569 static int mtk_mdio_init(struct mtk_eth *eth)
570 {
571 	struct device_node *mii_np;
572 	int ret;
573 
574 	mii_np = of_get_child_by_name(eth->dev->of_node, "mdio-bus");
575 	if (!mii_np) {
576 		dev_err(eth->dev, "no %s child node found", "mdio-bus");
577 		return -ENODEV;
578 	}
579 
580 	if (!of_device_is_available(mii_np)) {
581 		ret = -ENODEV;
582 		goto err_put_node;
583 	}
584 
585 	eth->mii_bus = devm_mdiobus_alloc(eth->dev);
586 	if (!eth->mii_bus) {
587 		ret = -ENOMEM;
588 		goto err_put_node;
589 	}
590 
591 	eth->mii_bus->name = "mdio";
592 	eth->mii_bus->read = mtk_mdio_read;
593 	eth->mii_bus->write = mtk_mdio_write;
594 	eth->mii_bus->priv = eth;
595 	eth->mii_bus->parent = eth->dev;
596 
597 	snprintf(eth->mii_bus->id, MII_BUS_ID_SIZE, "%pOFn", mii_np);
598 	ret = of_mdiobus_register(eth->mii_bus, mii_np);
599 
600 err_put_node:
601 	of_node_put(mii_np);
602 	return ret;
603 }
604 
mtk_mdio_cleanup(struct mtk_eth * eth)605 static void mtk_mdio_cleanup(struct mtk_eth *eth)
606 {
607 	if (!eth->mii_bus)
608 		return;
609 
610 	mdiobus_unregister(eth->mii_bus);
611 }
612 
mtk_tx_irq_disable(struct mtk_eth * eth,u32 mask)613 static inline void mtk_tx_irq_disable(struct mtk_eth *eth, u32 mask)
614 {
615 	unsigned long flags;
616 	u32 val;
617 
618 	spin_lock_irqsave(&eth->tx_irq_lock, flags);
619 	val = mtk_r32(eth, eth->tx_int_mask_reg);
620 	mtk_w32(eth, val & ~mask, eth->tx_int_mask_reg);
621 	spin_unlock_irqrestore(&eth->tx_irq_lock, flags);
622 }
623 
mtk_tx_irq_enable(struct mtk_eth * eth,u32 mask)624 static inline void mtk_tx_irq_enable(struct mtk_eth *eth, u32 mask)
625 {
626 	unsigned long flags;
627 	u32 val;
628 
629 	spin_lock_irqsave(&eth->tx_irq_lock, flags);
630 	val = mtk_r32(eth, eth->tx_int_mask_reg);
631 	mtk_w32(eth, val | mask, eth->tx_int_mask_reg);
632 	spin_unlock_irqrestore(&eth->tx_irq_lock, flags);
633 }
634 
mtk_rx_irq_disable(struct mtk_eth * eth,u32 mask)635 static inline void mtk_rx_irq_disable(struct mtk_eth *eth, u32 mask)
636 {
637 	unsigned long flags;
638 	u32 val;
639 
640 	spin_lock_irqsave(&eth->rx_irq_lock, flags);
641 	val = mtk_r32(eth, MTK_PDMA_INT_MASK);
642 	mtk_w32(eth, val & ~mask, MTK_PDMA_INT_MASK);
643 	spin_unlock_irqrestore(&eth->rx_irq_lock, flags);
644 }
645 
mtk_rx_irq_enable(struct mtk_eth * eth,u32 mask)646 static inline void mtk_rx_irq_enable(struct mtk_eth *eth, u32 mask)
647 {
648 	unsigned long flags;
649 	u32 val;
650 
651 	spin_lock_irqsave(&eth->rx_irq_lock, flags);
652 	val = mtk_r32(eth, MTK_PDMA_INT_MASK);
653 	mtk_w32(eth, val | mask, MTK_PDMA_INT_MASK);
654 	spin_unlock_irqrestore(&eth->rx_irq_lock, flags);
655 }
656 
mtk_set_mac_address(struct net_device * dev,void * p)657 static int mtk_set_mac_address(struct net_device *dev, void *p)
658 {
659 	int ret = eth_mac_addr(dev, p);
660 	struct mtk_mac *mac = netdev_priv(dev);
661 	struct mtk_eth *eth = mac->hw;
662 	const char *macaddr = dev->dev_addr;
663 
664 	if (ret)
665 		return ret;
666 
667 	if (unlikely(test_bit(MTK_RESETTING, &mac->hw->state)))
668 		return -EBUSY;
669 
670 	spin_lock_bh(&mac->hw->page_lock);
671 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_SOC_MT7628)) {
672 		mtk_w32(mac->hw, (macaddr[0] << 8) | macaddr[1],
673 			MT7628_SDM_MAC_ADRH);
674 		mtk_w32(mac->hw, (macaddr[2] << 24) | (macaddr[3] << 16) |
675 			(macaddr[4] << 8) | macaddr[5],
676 			MT7628_SDM_MAC_ADRL);
677 	} else {
678 		mtk_w32(mac->hw, (macaddr[0] << 8) | macaddr[1],
679 			MTK_GDMA_MAC_ADRH(mac->id));
680 		mtk_w32(mac->hw, (macaddr[2] << 24) | (macaddr[3] << 16) |
681 			(macaddr[4] << 8) | macaddr[5],
682 			MTK_GDMA_MAC_ADRL(mac->id));
683 	}
684 	spin_unlock_bh(&mac->hw->page_lock);
685 
686 	return 0;
687 }
688 
mtk_stats_update_mac(struct mtk_mac * mac)689 void mtk_stats_update_mac(struct mtk_mac *mac)
690 {
691 	struct mtk_hw_stats *hw_stats = mac->hw_stats;
692 	struct mtk_eth *eth = mac->hw;
693 
694 	u64_stats_update_begin(&hw_stats->syncp);
695 
696 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_SOC_MT7628)) {
697 		hw_stats->tx_packets += mtk_r32(mac->hw, MT7628_SDM_TPCNT);
698 		hw_stats->tx_bytes += mtk_r32(mac->hw, MT7628_SDM_TBCNT);
699 		hw_stats->rx_packets += mtk_r32(mac->hw, MT7628_SDM_RPCNT);
700 		hw_stats->rx_bytes += mtk_r32(mac->hw, MT7628_SDM_RBCNT);
701 		hw_stats->rx_checksum_errors +=
702 			mtk_r32(mac->hw, MT7628_SDM_CS_ERR);
703 	} else {
704 		unsigned int offs = hw_stats->reg_offset;
705 		u64 stats;
706 
707 		hw_stats->rx_bytes += mtk_r32(mac->hw,
708 					      MTK_GDM1_RX_GBCNT_L + offs);
709 		stats = mtk_r32(mac->hw, MTK_GDM1_RX_GBCNT_H + offs);
710 		if (stats)
711 			hw_stats->rx_bytes += (stats << 32);
712 		hw_stats->rx_packets +=
713 			mtk_r32(mac->hw, MTK_GDM1_RX_GPCNT + offs);
714 		hw_stats->rx_overflow +=
715 			mtk_r32(mac->hw, MTK_GDM1_RX_OERCNT + offs);
716 		hw_stats->rx_fcs_errors +=
717 			mtk_r32(mac->hw, MTK_GDM1_RX_FERCNT + offs);
718 		hw_stats->rx_short_errors +=
719 			mtk_r32(mac->hw, MTK_GDM1_RX_SERCNT + offs);
720 		hw_stats->rx_long_errors +=
721 			mtk_r32(mac->hw, MTK_GDM1_RX_LENCNT + offs);
722 		hw_stats->rx_checksum_errors +=
723 			mtk_r32(mac->hw, MTK_GDM1_RX_CERCNT + offs);
724 		hw_stats->rx_flow_control_packets +=
725 			mtk_r32(mac->hw, MTK_GDM1_RX_FCCNT + offs);
726 		hw_stats->tx_skip +=
727 			mtk_r32(mac->hw, MTK_GDM1_TX_SKIPCNT + offs);
728 		hw_stats->tx_collisions +=
729 			mtk_r32(mac->hw, MTK_GDM1_TX_COLCNT + offs);
730 		hw_stats->tx_bytes +=
731 			mtk_r32(mac->hw, MTK_GDM1_TX_GBCNT_L + offs);
732 		stats =  mtk_r32(mac->hw, MTK_GDM1_TX_GBCNT_H + offs);
733 		if (stats)
734 			hw_stats->tx_bytes += (stats << 32);
735 		hw_stats->tx_packets +=
736 			mtk_r32(mac->hw, MTK_GDM1_TX_GPCNT + offs);
737 	}
738 
739 	u64_stats_update_end(&hw_stats->syncp);
740 }
741 
mtk_stats_update(struct mtk_eth * eth)742 static void mtk_stats_update(struct mtk_eth *eth)
743 {
744 	int i;
745 
746 	for (i = 0; i < MTK_MAC_COUNT; i++) {
747 		if (!eth->mac[i] || !eth->mac[i]->hw_stats)
748 			continue;
749 		if (spin_trylock(&eth->mac[i]->hw_stats->stats_lock)) {
750 			mtk_stats_update_mac(eth->mac[i]);
751 			spin_unlock(&eth->mac[i]->hw_stats->stats_lock);
752 		}
753 	}
754 }
755 
mtk_get_stats64(struct net_device * dev,struct rtnl_link_stats64 * storage)756 static void mtk_get_stats64(struct net_device *dev,
757 			    struct rtnl_link_stats64 *storage)
758 {
759 	struct mtk_mac *mac = netdev_priv(dev);
760 	struct mtk_hw_stats *hw_stats = mac->hw_stats;
761 	unsigned int start;
762 
763 	if (netif_running(dev) && netif_device_present(dev)) {
764 		if (spin_trylock_bh(&hw_stats->stats_lock)) {
765 			mtk_stats_update_mac(mac);
766 			spin_unlock_bh(&hw_stats->stats_lock);
767 		}
768 	}
769 
770 	do {
771 		start = u64_stats_fetch_begin_irq(&hw_stats->syncp);
772 		storage->rx_packets = hw_stats->rx_packets;
773 		storage->tx_packets = hw_stats->tx_packets;
774 		storage->rx_bytes = hw_stats->rx_bytes;
775 		storage->tx_bytes = hw_stats->tx_bytes;
776 		storage->collisions = hw_stats->tx_collisions;
777 		storage->rx_length_errors = hw_stats->rx_short_errors +
778 			hw_stats->rx_long_errors;
779 		storage->rx_over_errors = hw_stats->rx_overflow;
780 		storage->rx_crc_errors = hw_stats->rx_fcs_errors;
781 		storage->rx_errors = hw_stats->rx_checksum_errors;
782 		storage->tx_aborted_errors = hw_stats->tx_skip;
783 	} while (u64_stats_fetch_retry_irq(&hw_stats->syncp, start));
784 
785 	storage->tx_errors = dev->stats.tx_errors;
786 	storage->rx_dropped = dev->stats.rx_dropped;
787 	storage->tx_dropped = dev->stats.tx_dropped;
788 }
789 
mtk_max_frag_size(int mtu)790 static inline int mtk_max_frag_size(int mtu)
791 {
792 	/* make sure buf_size will be at least MTK_MAX_RX_LENGTH */
793 	if (mtu + MTK_RX_ETH_HLEN < MTK_MAX_RX_LENGTH_2K)
794 		mtu = MTK_MAX_RX_LENGTH_2K - MTK_RX_ETH_HLEN;
795 
796 	return SKB_DATA_ALIGN(MTK_RX_HLEN + mtu) +
797 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
798 }
799 
mtk_max_buf_size(int frag_size)800 static inline int mtk_max_buf_size(int frag_size)
801 {
802 	int buf_size = frag_size - NET_SKB_PAD - NET_IP_ALIGN -
803 		       SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
804 
805 	WARN_ON(buf_size < MTK_MAX_RX_LENGTH_2K);
806 
807 	return buf_size;
808 }
809 
mtk_rx_get_desc(struct mtk_rx_dma * rxd,struct mtk_rx_dma * dma_rxd)810 static inline bool mtk_rx_get_desc(struct mtk_rx_dma *rxd,
811 				   struct mtk_rx_dma *dma_rxd)
812 {
813 	rxd->rxd2 = READ_ONCE(dma_rxd->rxd2);
814 	if (!(rxd->rxd2 & RX_DMA_DONE))
815 		return false;
816 
817 	rxd->rxd1 = READ_ONCE(dma_rxd->rxd1);
818 	rxd->rxd3 = READ_ONCE(dma_rxd->rxd3);
819 	rxd->rxd4 = READ_ONCE(dma_rxd->rxd4);
820 
821 	return true;
822 }
823 
mtk_max_lro_buf_alloc(gfp_t gfp_mask)824 static void *mtk_max_lro_buf_alloc(gfp_t gfp_mask)
825 {
826 	unsigned int size = mtk_max_frag_size(MTK_MAX_LRO_RX_LENGTH);
827 	unsigned long data;
828 
829 	data = __get_free_pages(gfp_mask | __GFP_COMP | __GFP_NOWARN,
830 				get_order(size));
831 
832 	return (void *)data;
833 }
834 
835 /* the qdma core needs scratch memory to be setup */
mtk_init_fq_dma(struct mtk_eth * eth)836 static int mtk_init_fq_dma(struct mtk_eth *eth)
837 {
838 	dma_addr_t phy_ring_tail;
839 	int cnt = MTK_DMA_SIZE;
840 	dma_addr_t dma_addr;
841 	int i;
842 
843 	eth->scratch_ring = dma_alloc_coherent(eth->dev,
844 					       cnt * sizeof(struct mtk_tx_dma),
845 					       &eth->phy_scratch_ring,
846 					       GFP_ATOMIC);
847 	if (unlikely(!eth->scratch_ring))
848 		return -ENOMEM;
849 
850 	eth->scratch_head = kcalloc(cnt, MTK_QDMA_PAGE_SIZE,
851 				    GFP_KERNEL);
852 	if (unlikely(!eth->scratch_head))
853 		return -ENOMEM;
854 
855 	dma_addr = dma_map_single(eth->dev,
856 				  eth->scratch_head, cnt * MTK_QDMA_PAGE_SIZE,
857 				  DMA_FROM_DEVICE);
858 	if (unlikely(dma_mapping_error(eth->dev, dma_addr)))
859 		return -ENOMEM;
860 
861 	phy_ring_tail = eth->phy_scratch_ring +
862 			(sizeof(struct mtk_tx_dma) * (cnt - 1));
863 
864 	for (i = 0; i < cnt; i++) {
865 		eth->scratch_ring[i].txd1 =
866 					(dma_addr + (i * MTK_QDMA_PAGE_SIZE));
867 		if (i < cnt - 1)
868 			eth->scratch_ring[i].txd2 = (eth->phy_scratch_ring +
869 				((i + 1) * sizeof(struct mtk_tx_dma)));
870 		eth->scratch_ring[i].txd3 = TX_DMA_SDL(MTK_QDMA_PAGE_SIZE);
871 	}
872 
873 	mtk_w32(eth, eth->phy_scratch_ring, MTK_QDMA_FQ_HEAD);
874 	mtk_w32(eth, phy_ring_tail, MTK_QDMA_FQ_TAIL);
875 	mtk_w32(eth, (cnt << 16) | cnt, MTK_QDMA_FQ_CNT);
876 	mtk_w32(eth, MTK_QDMA_PAGE_SIZE << 16, MTK_QDMA_FQ_BLEN);
877 
878 	return 0;
879 }
880 
mtk_qdma_phys_to_virt(struct mtk_tx_ring * ring,u32 desc)881 static inline void *mtk_qdma_phys_to_virt(struct mtk_tx_ring *ring, u32 desc)
882 {
883 	void *ret = ring->dma;
884 
885 	return ret + (desc - ring->phys);
886 }
887 
mtk_desc_to_tx_buf(struct mtk_tx_ring * ring,struct mtk_tx_dma * txd)888 static inline struct mtk_tx_buf *mtk_desc_to_tx_buf(struct mtk_tx_ring *ring,
889 						    struct mtk_tx_dma *txd)
890 {
891 	int idx = txd - ring->dma;
892 
893 	return &ring->buf[idx];
894 }
895 
qdma_to_pdma(struct mtk_tx_ring * ring,struct mtk_tx_dma * dma)896 static struct mtk_tx_dma *qdma_to_pdma(struct mtk_tx_ring *ring,
897 				       struct mtk_tx_dma *dma)
898 {
899 	return ring->dma_pdma - ring->dma + dma;
900 }
901 
txd_to_idx(struct mtk_tx_ring * ring,struct mtk_tx_dma * dma)902 static int txd_to_idx(struct mtk_tx_ring *ring, struct mtk_tx_dma *dma)
903 {
904 	return ((void *)dma - (void *)ring->dma) / sizeof(*dma);
905 }
906 
mtk_tx_unmap(struct mtk_eth * eth,struct mtk_tx_buf * tx_buf,bool napi)907 static void mtk_tx_unmap(struct mtk_eth *eth, struct mtk_tx_buf *tx_buf,
908 			 bool napi)
909 {
910 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA)) {
911 		if (tx_buf->flags & MTK_TX_FLAGS_SINGLE0) {
912 			dma_unmap_single(eth->dev,
913 					 dma_unmap_addr(tx_buf, dma_addr0),
914 					 dma_unmap_len(tx_buf, dma_len0),
915 					 DMA_TO_DEVICE);
916 		} else if (tx_buf->flags & MTK_TX_FLAGS_PAGE0) {
917 			dma_unmap_page(eth->dev,
918 				       dma_unmap_addr(tx_buf, dma_addr0),
919 				       dma_unmap_len(tx_buf, dma_len0),
920 				       DMA_TO_DEVICE);
921 		}
922 	} else {
923 		if (dma_unmap_len(tx_buf, dma_len0)) {
924 			dma_unmap_page(eth->dev,
925 				       dma_unmap_addr(tx_buf, dma_addr0),
926 				       dma_unmap_len(tx_buf, dma_len0),
927 				       DMA_TO_DEVICE);
928 		}
929 
930 		if (dma_unmap_len(tx_buf, dma_len1)) {
931 			dma_unmap_page(eth->dev,
932 				       dma_unmap_addr(tx_buf, dma_addr1),
933 				       dma_unmap_len(tx_buf, dma_len1),
934 				       DMA_TO_DEVICE);
935 		}
936 	}
937 
938 	tx_buf->flags = 0;
939 	if (tx_buf->skb &&
940 	    (tx_buf->skb != (struct sk_buff *)MTK_DMA_DUMMY_DESC)) {
941 		if (napi)
942 			napi_consume_skb(tx_buf->skb, napi);
943 		else
944 			dev_kfree_skb_any(tx_buf->skb);
945 	}
946 	tx_buf->skb = NULL;
947 }
948 
setup_tx_buf(struct mtk_eth * eth,struct mtk_tx_buf * tx_buf,struct mtk_tx_dma * txd,dma_addr_t mapped_addr,size_t size,int idx)949 static void setup_tx_buf(struct mtk_eth *eth, struct mtk_tx_buf *tx_buf,
950 			 struct mtk_tx_dma *txd, dma_addr_t mapped_addr,
951 			 size_t size, int idx)
952 {
953 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA)) {
954 		dma_unmap_addr_set(tx_buf, dma_addr0, mapped_addr);
955 		dma_unmap_len_set(tx_buf, dma_len0, size);
956 	} else {
957 		if (idx & 1) {
958 			txd->txd3 = mapped_addr;
959 			txd->txd2 |= TX_DMA_PLEN1(size);
960 			dma_unmap_addr_set(tx_buf, dma_addr1, mapped_addr);
961 			dma_unmap_len_set(tx_buf, dma_len1, size);
962 		} else {
963 			tx_buf->skb = (struct sk_buff *)MTK_DMA_DUMMY_DESC;
964 			txd->txd1 = mapped_addr;
965 			txd->txd2 = TX_DMA_PLEN0(size);
966 			dma_unmap_addr_set(tx_buf, dma_addr0, mapped_addr);
967 			dma_unmap_len_set(tx_buf, dma_len0, size);
968 		}
969 	}
970 }
971 
mtk_tx_map(struct sk_buff * skb,struct net_device * dev,int tx_num,struct mtk_tx_ring * ring,bool gso)972 static int mtk_tx_map(struct sk_buff *skb, struct net_device *dev,
973 		      int tx_num, struct mtk_tx_ring *ring, bool gso)
974 {
975 	struct mtk_mac *mac = netdev_priv(dev);
976 	struct mtk_eth *eth = mac->hw;
977 	struct mtk_tx_dma *itxd, *txd;
978 	struct mtk_tx_dma *itxd_pdma, *txd_pdma;
979 	struct mtk_tx_buf *itx_buf, *tx_buf;
980 	dma_addr_t mapped_addr;
981 	unsigned int nr_frags;
982 	int i, n_desc = 1;
983 	u32 txd4 = 0, fport;
984 	int k = 0;
985 
986 	itxd = ring->next_free;
987 	itxd_pdma = qdma_to_pdma(ring, itxd);
988 	if (itxd == ring->last_free)
989 		return -ENOMEM;
990 
991 	/* set the forward port */
992 	fport = (mac->id + 1) << TX_DMA_FPORT_SHIFT;
993 	txd4 |= fport;
994 
995 	itx_buf = mtk_desc_to_tx_buf(ring, itxd);
996 	memset(itx_buf, 0, sizeof(*itx_buf));
997 
998 	if (gso)
999 		txd4 |= TX_DMA_TSO;
1000 
1001 	/* TX Checksum offload */
1002 	if (skb->ip_summed == CHECKSUM_PARTIAL)
1003 		txd4 |= TX_DMA_CHKSUM;
1004 
1005 	/* VLAN header offload */
1006 	if (skb_vlan_tag_present(skb))
1007 		txd4 |= TX_DMA_INS_VLAN | skb_vlan_tag_get(skb);
1008 
1009 	mapped_addr = dma_map_single(eth->dev, skb->data,
1010 				     skb_headlen(skb), DMA_TO_DEVICE);
1011 	if (unlikely(dma_mapping_error(eth->dev, mapped_addr)))
1012 		return -ENOMEM;
1013 
1014 	WRITE_ONCE(itxd->txd1, mapped_addr);
1015 	itx_buf->flags |= MTK_TX_FLAGS_SINGLE0;
1016 	itx_buf->flags |= (!mac->id) ? MTK_TX_FLAGS_FPORT0 :
1017 			  MTK_TX_FLAGS_FPORT1;
1018 	setup_tx_buf(eth, itx_buf, itxd_pdma, mapped_addr, skb_headlen(skb),
1019 		     k++);
1020 
1021 	/* TX SG offload */
1022 	txd = itxd;
1023 	txd_pdma = qdma_to_pdma(ring, txd);
1024 	nr_frags = skb_shinfo(skb)->nr_frags;
1025 
1026 	for (i = 0; i < nr_frags; i++) {
1027 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1028 		unsigned int offset = 0;
1029 		int frag_size = skb_frag_size(frag);
1030 
1031 		while (frag_size) {
1032 			bool last_frag = false;
1033 			unsigned int frag_map_size;
1034 			bool new_desc = true;
1035 
1036 			if (MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA) ||
1037 			    (i & 0x1)) {
1038 				txd = mtk_qdma_phys_to_virt(ring, txd->txd2);
1039 				txd_pdma = qdma_to_pdma(ring, txd);
1040 				if (txd == ring->last_free)
1041 					goto err_dma;
1042 
1043 				n_desc++;
1044 			} else {
1045 				new_desc = false;
1046 			}
1047 
1048 
1049 			frag_map_size = min(frag_size, MTK_TX_DMA_BUF_LEN);
1050 			mapped_addr = skb_frag_dma_map(eth->dev, frag, offset,
1051 						       frag_map_size,
1052 						       DMA_TO_DEVICE);
1053 			if (unlikely(dma_mapping_error(eth->dev, mapped_addr)))
1054 				goto err_dma;
1055 
1056 			if (i == nr_frags - 1 &&
1057 			    (frag_size - frag_map_size) == 0)
1058 				last_frag = true;
1059 
1060 			WRITE_ONCE(txd->txd1, mapped_addr);
1061 			WRITE_ONCE(txd->txd3, (TX_DMA_SWC |
1062 					       TX_DMA_PLEN0(frag_map_size) |
1063 					       last_frag * TX_DMA_LS0));
1064 			WRITE_ONCE(txd->txd4, fport);
1065 
1066 			tx_buf = mtk_desc_to_tx_buf(ring, txd);
1067 			if (new_desc)
1068 				memset(tx_buf, 0, sizeof(*tx_buf));
1069 			tx_buf->skb = (struct sk_buff *)MTK_DMA_DUMMY_DESC;
1070 			tx_buf->flags |= MTK_TX_FLAGS_PAGE0;
1071 			tx_buf->flags |= (!mac->id) ? MTK_TX_FLAGS_FPORT0 :
1072 					 MTK_TX_FLAGS_FPORT1;
1073 
1074 			setup_tx_buf(eth, tx_buf, txd_pdma, mapped_addr,
1075 				     frag_map_size, k++);
1076 
1077 			frag_size -= frag_map_size;
1078 			offset += frag_map_size;
1079 		}
1080 	}
1081 
1082 	/* store skb to cleanup */
1083 	itx_buf->skb = skb;
1084 
1085 	WRITE_ONCE(itxd->txd4, txd4);
1086 	WRITE_ONCE(itxd->txd3, (TX_DMA_SWC | TX_DMA_PLEN0(skb_headlen(skb)) |
1087 				(!nr_frags * TX_DMA_LS0)));
1088 	if (!MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA)) {
1089 		if (k & 0x1)
1090 			txd_pdma->txd2 |= TX_DMA_LS0;
1091 		else
1092 			txd_pdma->txd2 |= TX_DMA_LS1;
1093 	}
1094 
1095 	netdev_sent_queue(dev, skb->len);
1096 	skb_tx_timestamp(skb);
1097 
1098 	ring->next_free = mtk_qdma_phys_to_virt(ring, txd->txd2);
1099 	atomic_sub(n_desc, &ring->free_count);
1100 
1101 	/* make sure that all changes to the dma ring are flushed before we
1102 	 * continue
1103 	 */
1104 	wmb();
1105 
1106 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA)) {
1107 		if (netif_xmit_stopped(netdev_get_tx_queue(dev, 0)) ||
1108 		    !netdev_xmit_more())
1109 			mtk_w32(eth, txd->txd2, MTK_QTX_CTX_PTR);
1110 	} else {
1111 		int next_idx = NEXT_DESP_IDX(txd_to_idx(ring, txd),
1112 					     ring->dma_size);
1113 		mtk_w32(eth, next_idx, MT7628_TX_CTX_IDX0);
1114 	}
1115 
1116 	return 0;
1117 
1118 err_dma:
1119 	do {
1120 		tx_buf = mtk_desc_to_tx_buf(ring, itxd);
1121 
1122 		/* unmap dma */
1123 		mtk_tx_unmap(eth, tx_buf, false);
1124 
1125 		itxd->txd3 = TX_DMA_LS0 | TX_DMA_OWNER_CPU;
1126 		if (!MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA))
1127 			itxd_pdma->txd2 = TX_DMA_DESP2_DEF;
1128 
1129 		itxd = mtk_qdma_phys_to_virt(ring, itxd->txd2);
1130 		itxd_pdma = qdma_to_pdma(ring, itxd);
1131 	} while (itxd != txd);
1132 
1133 	return -ENOMEM;
1134 }
1135 
mtk_cal_txd_req(struct sk_buff * skb)1136 static inline int mtk_cal_txd_req(struct sk_buff *skb)
1137 {
1138 	int i, nfrags;
1139 	skb_frag_t *frag;
1140 
1141 	nfrags = 1;
1142 	if (skb_is_gso(skb)) {
1143 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1144 			frag = &skb_shinfo(skb)->frags[i];
1145 			nfrags += DIV_ROUND_UP(skb_frag_size(frag),
1146 						MTK_TX_DMA_BUF_LEN);
1147 		}
1148 	} else {
1149 		nfrags += skb_shinfo(skb)->nr_frags;
1150 	}
1151 
1152 	return nfrags;
1153 }
1154 
mtk_queue_stopped(struct mtk_eth * eth)1155 static int mtk_queue_stopped(struct mtk_eth *eth)
1156 {
1157 	int i;
1158 
1159 	for (i = 0; i < MTK_MAC_COUNT; i++) {
1160 		if (!eth->netdev[i])
1161 			continue;
1162 		if (netif_queue_stopped(eth->netdev[i]))
1163 			return 1;
1164 	}
1165 
1166 	return 0;
1167 }
1168 
mtk_wake_queue(struct mtk_eth * eth)1169 static void mtk_wake_queue(struct mtk_eth *eth)
1170 {
1171 	int i;
1172 
1173 	for (i = 0; i < MTK_MAC_COUNT; i++) {
1174 		if (!eth->netdev[i])
1175 			continue;
1176 		netif_wake_queue(eth->netdev[i]);
1177 	}
1178 }
1179 
mtk_start_xmit(struct sk_buff * skb,struct net_device * dev)1180 static netdev_tx_t mtk_start_xmit(struct sk_buff *skb, struct net_device *dev)
1181 {
1182 	struct mtk_mac *mac = netdev_priv(dev);
1183 	struct mtk_eth *eth = mac->hw;
1184 	struct mtk_tx_ring *ring = &eth->tx_ring;
1185 	struct net_device_stats *stats = &dev->stats;
1186 	bool gso = false;
1187 	int tx_num;
1188 
1189 	/* normally we can rely on the stack not calling this more than once,
1190 	 * however we have 2 queues running on the same ring so we need to lock
1191 	 * the ring access
1192 	 */
1193 	spin_lock(&eth->page_lock);
1194 
1195 	if (unlikely(test_bit(MTK_RESETTING, &eth->state)))
1196 		goto drop;
1197 
1198 	tx_num = mtk_cal_txd_req(skb);
1199 	if (unlikely(atomic_read(&ring->free_count) <= tx_num)) {
1200 		netif_stop_queue(dev);
1201 		netif_err(eth, tx_queued, dev,
1202 			  "Tx Ring full when queue awake!\n");
1203 		spin_unlock(&eth->page_lock);
1204 		return NETDEV_TX_BUSY;
1205 	}
1206 
1207 	/* TSO: fill MSS info in tcp checksum field */
1208 	if (skb_is_gso(skb)) {
1209 		if (skb_cow_head(skb, 0)) {
1210 			netif_warn(eth, tx_err, dev,
1211 				   "GSO expand head fail.\n");
1212 			goto drop;
1213 		}
1214 
1215 		if (skb_shinfo(skb)->gso_type &
1216 				(SKB_GSO_TCPV4 | SKB_GSO_TCPV6)) {
1217 			gso = true;
1218 			tcp_hdr(skb)->check = htons(skb_shinfo(skb)->gso_size);
1219 		}
1220 	}
1221 
1222 	if (mtk_tx_map(skb, dev, tx_num, ring, gso) < 0)
1223 		goto drop;
1224 
1225 	if (unlikely(atomic_read(&ring->free_count) <= ring->thresh))
1226 		netif_stop_queue(dev);
1227 
1228 	spin_unlock(&eth->page_lock);
1229 
1230 	return NETDEV_TX_OK;
1231 
1232 drop:
1233 	spin_unlock(&eth->page_lock);
1234 	stats->tx_dropped++;
1235 	dev_kfree_skb_any(skb);
1236 	return NETDEV_TX_OK;
1237 }
1238 
mtk_get_rx_ring(struct mtk_eth * eth)1239 static struct mtk_rx_ring *mtk_get_rx_ring(struct mtk_eth *eth)
1240 {
1241 	int i;
1242 	struct mtk_rx_ring *ring;
1243 	int idx;
1244 
1245 	if (!eth->hwlro)
1246 		return &eth->rx_ring[0];
1247 
1248 	for (i = 0; i < MTK_MAX_RX_RING_NUM; i++) {
1249 		ring = &eth->rx_ring[i];
1250 		idx = NEXT_DESP_IDX(ring->calc_idx, ring->dma_size);
1251 		if (ring->dma[idx].rxd2 & RX_DMA_DONE) {
1252 			ring->calc_idx_update = true;
1253 			return ring;
1254 		}
1255 	}
1256 
1257 	return NULL;
1258 }
1259 
mtk_update_rx_cpu_idx(struct mtk_eth * eth)1260 static void mtk_update_rx_cpu_idx(struct mtk_eth *eth)
1261 {
1262 	struct mtk_rx_ring *ring;
1263 	int i;
1264 
1265 	if (!eth->hwlro) {
1266 		ring = &eth->rx_ring[0];
1267 		mtk_w32(eth, ring->calc_idx, ring->crx_idx_reg);
1268 	} else {
1269 		for (i = 0; i < MTK_MAX_RX_RING_NUM; i++) {
1270 			ring = &eth->rx_ring[i];
1271 			if (ring->calc_idx_update) {
1272 				ring->calc_idx_update = false;
1273 				mtk_w32(eth, ring->calc_idx, ring->crx_idx_reg);
1274 			}
1275 		}
1276 	}
1277 }
1278 
mtk_poll_rx(struct napi_struct * napi,int budget,struct mtk_eth * eth)1279 static int mtk_poll_rx(struct napi_struct *napi, int budget,
1280 		       struct mtk_eth *eth)
1281 {
1282 	struct dim_sample dim_sample = {};
1283 	struct mtk_rx_ring *ring;
1284 	int idx;
1285 	struct sk_buff *skb;
1286 	u8 *data, *new_data;
1287 	struct mtk_rx_dma *rxd, trxd;
1288 	int done = 0, bytes = 0;
1289 
1290 	while (done < budget) {
1291 		struct net_device *netdev;
1292 		unsigned int pktlen;
1293 		dma_addr_t dma_addr;
1294 		u32 hash;
1295 		int mac;
1296 
1297 		ring = mtk_get_rx_ring(eth);
1298 		if (unlikely(!ring))
1299 			goto rx_done;
1300 
1301 		idx = NEXT_DESP_IDX(ring->calc_idx, ring->dma_size);
1302 		rxd = &ring->dma[idx];
1303 		data = ring->data[idx];
1304 
1305 		if (!mtk_rx_get_desc(&trxd, rxd))
1306 			break;
1307 
1308 		/* find out which mac the packet come from. values start at 1 */
1309 		if (MTK_HAS_CAPS(eth->soc->caps, MTK_SOC_MT7628) ||
1310 		    (trxd.rxd4 & RX_DMA_SPECIAL_TAG))
1311 			mac = 0;
1312 		else
1313 			mac = ((trxd.rxd4 >> RX_DMA_FPORT_SHIFT) &
1314 			       RX_DMA_FPORT_MASK) - 1;
1315 
1316 		if (unlikely(mac < 0 || mac >= MTK_MAC_COUNT ||
1317 			     !eth->netdev[mac]))
1318 			goto release_desc;
1319 
1320 		netdev = eth->netdev[mac];
1321 
1322 		if (unlikely(test_bit(MTK_RESETTING, &eth->state)))
1323 			goto release_desc;
1324 
1325 		/* alloc new buffer */
1326 		if (ring->frag_size <= PAGE_SIZE)
1327 			new_data = napi_alloc_frag(ring->frag_size);
1328 		else
1329 			new_data = mtk_max_lro_buf_alloc(GFP_ATOMIC);
1330 		if (unlikely(!new_data)) {
1331 			netdev->stats.rx_dropped++;
1332 			goto release_desc;
1333 		}
1334 		dma_addr = dma_map_single(eth->dev,
1335 					  new_data + NET_SKB_PAD +
1336 					  eth->ip_align,
1337 					  ring->buf_size,
1338 					  DMA_FROM_DEVICE);
1339 		if (unlikely(dma_mapping_error(eth->dev, dma_addr))) {
1340 			skb_free_frag(new_data);
1341 			netdev->stats.rx_dropped++;
1342 			goto release_desc;
1343 		}
1344 
1345 		dma_unmap_single(eth->dev, trxd.rxd1,
1346 				 ring->buf_size, DMA_FROM_DEVICE);
1347 
1348 		/* receive data */
1349 		skb = build_skb(data, ring->frag_size);
1350 		if (unlikely(!skb)) {
1351 			skb_free_frag(data);
1352 			netdev->stats.rx_dropped++;
1353 			goto skip_rx;
1354 		}
1355 		skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
1356 
1357 		pktlen = RX_DMA_GET_PLEN0(trxd.rxd2);
1358 		skb->dev = netdev;
1359 		skb_put(skb, pktlen);
1360 		if (trxd.rxd4 & eth->rx_dma_l4_valid)
1361 			skb->ip_summed = CHECKSUM_UNNECESSARY;
1362 		else
1363 			skb_checksum_none_assert(skb);
1364 		skb->protocol = eth_type_trans(skb, netdev);
1365 		bytes += pktlen;
1366 
1367 		hash = trxd.rxd4 & MTK_RXD4_FOE_ENTRY;
1368 		if (hash != MTK_RXD4_FOE_ENTRY) {
1369 			hash = jhash_1word(hash, 0);
1370 			skb_set_hash(skb, hash, PKT_HASH_TYPE_L4);
1371 		}
1372 
1373 		if (netdev->features & NETIF_F_HW_VLAN_CTAG_RX &&
1374 		    (trxd.rxd2 & RX_DMA_VTAG))
1375 			__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1376 					       RX_DMA_VID(trxd.rxd3));
1377 		skb_record_rx_queue(skb, 0);
1378 		napi_gro_receive(napi, skb);
1379 
1380 skip_rx:
1381 		ring->data[idx] = new_data;
1382 		rxd->rxd1 = (unsigned int)dma_addr;
1383 
1384 release_desc:
1385 		if (MTK_HAS_CAPS(eth->soc->caps, MTK_SOC_MT7628))
1386 			rxd->rxd2 = RX_DMA_LSO;
1387 		else
1388 			rxd->rxd2 = RX_DMA_PLEN0(ring->buf_size);
1389 
1390 		ring->calc_idx = idx;
1391 
1392 		done++;
1393 	}
1394 
1395 rx_done:
1396 	if (done) {
1397 		/* make sure that all changes to the dma ring are flushed before
1398 		 * we continue
1399 		 */
1400 		wmb();
1401 		mtk_update_rx_cpu_idx(eth);
1402 	}
1403 
1404 	eth->rx_packets += done;
1405 	eth->rx_bytes += bytes;
1406 	dim_update_sample(eth->rx_events, eth->rx_packets, eth->rx_bytes,
1407 			  &dim_sample);
1408 	net_dim(&eth->rx_dim, dim_sample);
1409 
1410 	return done;
1411 }
1412 
mtk_poll_tx_qdma(struct mtk_eth * eth,int budget,unsigned int * done,unsigned int * bytes)1413 static int mtk_poll_tx_qdma(struct mtk_eth *eth, int budget,
1414 			    unsigned int *done, unsigned int *bytes)
1415 {
1416 	struct mtk_tx_ring *ring = &eth->tx_ring;
1417 	struct mtk_tx_dma *desc;
1418 	struct sk_buff *skb;
1419 	struct mtk_tx_buf *tx_buf;
1420 	u32 cpu, dma;
1421 
1422 	cpu = ring->last_free_ptr;
1423 	dma = mtk_r32(eth, MTK_QTX_DRX_PTR);
1424 
1425 	desc = mtk_qdma_phys_to_virt(ring, cpu);
1426 
1427 	while ((cpu != dma) && budget) {
1428 		u32 next_cpu = desc->txd2;
1429 		int mac = 0;
1430 
1431 		desc = mtk_qdma_phys_to_virt(ring, desc->txd2);
1432 		if ((desc->txd3 & TX_DMA_OWNER_CPU) == 0)
1433 			break;
1434 
1435 		tx_buf = mtk_desc_to_tx_buf(ring, desc);
1436 		if (tx_buf->flags & MTK_TX_FLAGS_FPORT1)
1437 			mac = 1;
1438 
1439 		skb = tx_buf->skb;
1440 		if (!skb)
1441 			break;
1442 
1443 		if (skb != (struct sk_buff *)MTK_DMA_DUMMY_DESC) {
1444 			bytes[mac] += skb->len;
1445 			done[mac]++;
1446 			budget--;
1447 		}
1448 		mtk_tx_unmap(eth, tx_buf, true);
1449 
1450 		ring->last_free = desc;
1451 		atomic_inc(&ring->free_count);
1452 
1453 		cpu = next_cpu;
1454 	}
1455 
1456 	ring->last_free_ptr = cpu;
1457 	mtk_w32(eth, cpu, MTK_QTX_CRX_PTR);
1458 
1459 	return budget;
1460 }
1461 
mtk_poll_tx_pdma(struct mtk_eth * eth,int budget,unsigned int * done,unsigned int * bytes)1462 static int mtk_poll_tx_pdma(struct mtk_eth *eth, int budget,
1463 			    unsigned int *done, unsigned int *bytes)
1464 {
1465 	struct mtk_tx_ring *ring = &eth->tx_ring;
1466 	struct mtk_tx_dma *desc;
1467 	struct sk_buff *skb;
1468 	struct mtk_tx_buf *tx_buf;
1469 	u32 cpu, dma;
1470 
1471 	cpu = ring->cpu_idx;
1472 	dma = mtk_r32(eth, MT7628_TX_DTX_IDX0);
1473 
1474 	while ((cpu != dma) && budget) {
1475 		tx_buf = &ring->buf[cpu];
1476 		skb = tx_buf->skb;
1477 		if (!skb)
1478 			break;
1479 
1480 		if (skb != (struct sk_buff *)MTK_DMA_DUMMY_DESC) {
1481 			bytes[0] += skb->len;
1482 			done[0]++;
1483 			budget--;
1484 		}
1485 
1486 		mtk_tx_unmap(eth, tx_buf, true);
1487 
1488 		desc = &ring->dma[cpu];
1489 		ring->last_free = desc;
1490 		atomic_inc(&ring->free_count);
1491 
1492 		cpu = NEXT_DESP_IDX(cpu, ring->dma_size);
1493 	}
1494 
1495 	ring->cpu_idx = cpu;
1496 
1497 	return budget;
1498 }
1499 
mtk_poll_tx(struct mtk_eth * eth,int budget)1500 static int mtk_poll_tx(struct mtk_eth *eth, int budget)
1501 {
1502 	struct mtk_tx_ring *ring = &eth->tx_ring;
1503 	struct dim_sample dim_sample = {};
1504 	unsigned int done[MTK_MAX_DEVS];
1505 	unsigned int bytes[MTK_MAX_DEVS];
1506 	int total = 0, i;
1507 
1508 	memset(done, 0, sizeof(done));
1509 	memset(bytes, 0, sizeof(bytes));
1510 
1511 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA))
1512 		budget = mtk_poll_tx_qdma(eth, budget, done, bytes);
1513 	else
1514 		budget = mtk_poll_tx_pdma(eth, budget, done, bytes);
1515 
1516 	for (i = 0; i < MTK_MAC_COUNT; i++) {
1517 		if (!eth->netdev[i] || !done[i])
1518 			continue;
1519 		netdev_completed_queue(eth->netdev[i], done[i], bytes[i]);
1520 		total += done[i];
1521 		eth->tx_packets += done[i];
1522 		eth->tx_bytes += bytes[i];
1523 	}
1524 
1525 	dim_update_sample(eth->tx_events, eth->tx_packets, eth->tx_bytes,
1526 			  &dim_sample);
1527 	net_dim(&eth->tx_dim, dim_sample);
1528 
1529 	if (mtk_queue_stopped(eth) &&
1530 	    (atomic_read(&ring->free_count) > ring->thresh))
1531 		mtk_wake_queue(eth);
1532 
1533 	return total;
1534 }
1535 
mtk_handle_status_irq(struct mtk_eth * eth)1536 static void mtk_handle_status_irq(struct mtk_eth *eth)
1537 {
1538 	u32 status2 = mtk_r32(eth, MTK_INT_STATUS2);
1539 
1540 	if (unlikely(status2 & (MTK_GDM1_AF | MTK_GDM2_AF))) {
1541 		mtk_stats_update(eth);
1542 		mtk_w32(eth, (MTK_GDM1_AF | MTK_GDM2_AF),
1543 			MTK_INT_STATUS2);
1544 	}
1545 }
1546 
mtk_napi_tx(struct napi_struct * napi,int budget)1547 static int mtk_napi_tx(struct napi_struct *napi, int budget)
1548 {
1549 	struct mtk_eth *eth = container_of(napi, struct mtk_eth, tx_napi);
1550 	int tx_done = 0;
1551 
1552 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA))
1553 		mtk_handle_status_irq(eth);
1554 	mtk_w32(eth, MTK_TX_DONE_INT, eth->tx_int_status_reg);
1555 	tx_done = mtk_poll_tx(eth, budget);
1556 
1557 	if (unlikely(netif_msg_intr(eth))) {
1558 		dev_info(eth->dev,
1559 			 "done tx %d, intr 0x%08x/0x%x\n", tx_done,
1560 			 mtk_r32(eth, eth->tx_int_status_reg),
1561 			 mtk_r32(eth, eth->tx_int_mask_reg));
1562 	}
1563 
1564 	if (tx_done == budget)
1565 		return budget;
1566 
1567 	if (mtk_r32(eth, eth->tx_int_status_reg) & MTK_TX_DONE_INT)
1568 		return budget;
1569 
1570 	if (napi_complete_done(napi, tx_done))
1571 		mtk_tx_irq_enable(eth, MTK_TX_DONE_INT);
1572 
1573 	return tx_done;
1574 }
1575 
mtk_napi_rx(struct napi_struct * napi,int budget)1576 static int mtk_napi_rx(struct napi_struct *napi, int budget)
1577 {
1578 	struct mtk_eth *eth = container_of(napi, struct mtk_eth, rx_napi);
1579 	int rx_done_total = 0;
1580 
1581 	mtk_handle_status_irq(eth);
1582 
1583 	do {
1584 		int rx_done;
1585 
1586 		mtk_w32(eth, MTK_RX_DONE_INT, MTK_PDMA_INT_STATUS);
1587 		rx_done = mtk_poll_rx(napi, budget - rx_done_total, eth);
1588 		rx_done_total += rx_done;
1589 
1590 		if (unlikely(netif_msg_intr(eth))) {
1591 			dev_info(eth->dev,
1592 				 "done rx %d, intr 0x%08x/0x%x\n", rx_done,
1593 				 mtk_r32(eth, MTK_PDMA_INT_STATUS),
1594 				 mtk_r32(eth, MTK_PDMA_INT_MASK));
1595 		}
1596 
1597 		if (rx_done_total == budget)
1598 			return budget;
1599 
1600 	} while (mtk_r32(eth, MTK_PDMA_INT_STATUS) & MTK_RX_DONE_INT);
1601 
1602 	if (napi_complete_done(napi, rx_done_total))
1603 		mtk_rx_irq_enable(eth, MTK_RX_DONE_INT);
1604 
1605 	return rx_done_total;
1606 }
1607 
mtk_tx_alloc(struct mtk_eth * eth)1608 static int mtk_tx_alloc(struct mtk_eth *eth)
1609 {
1610 	struct mtk_tx_ring *ring = &eth->tx_ring;
1611 	int i, sz = sizeof(*ring->dma);
1612 
1613 	ring->buf = kcalloc(MTK_DMA_SIZE, sizeof(*ring->buf),
1614 			       GFP_KERNEL);
1615 	if (!ring->buf)
1616 		goto no_tx_mem;
1617 
1618 	ring->dma = dma_alloc_coherent(eth->dev, MTK_DMA_SIZE * sz,
1619 				       &ring->phys, GFP_ATOMIC);
1620 	if (!ring->dma)
1621 		goto no_tx_mem;
1622 
1623 	for (i = 0; i < MTK_DMA_SIZE; i++) {
1624 		int next = (i + 1) % MTK_DMA_SIZE;
1625 		u32 next_ptr = ring->phys + next * sz;
1626 
1627 		ring->dma[i].txd2 = next_ptr;
1628 		ring->dma[i].txd3 = TX_DMA_LS0 | TX_DMA_OWNER_CPU;
1629 	}
1630 
1631 	/* On MT7688 (PDMA only) this driver uses the ring->dma structs
1632 	 * only as the framework. The real HW descriptors are the PDMA
1633 	 * descriptors in ring->dma_pdma.
1634 	 */
1635 	if (!MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA)) {
1636 		ring->dma_pdma = dma_alloc_coherent(eth->dev, MTK_DMA_SIZE * sz,
1637 						    &ring->phys_pdma,
1638 						    GFP_ATOMIC);
1639 		if (!ring->dma_pdma)
1640 			goto no_tx_mem;
1641 
1642 		for (i = 0; i < MTK_DMA_SIZE; i++) {
1643 			ring->dma_pdma[i].txd2 = TX_DMA_DESP2_DEF;
1644 			ring->dma_pdma[i].txd4 = 0;
1645 		}
1646 	}
1647 
1648 	ring->dma_size = MTK_DMA_SIZE;
1649 	atomic_set(&ring->free_count, MTK_DMA_SIZE - 2);
1650 	ring->next_free = &ring->dma[0];
1651 	ring->last_free = &ring->dma[MTK_DMA_SIZE - 1];
1652 	ring->last_free_ptr = (u32)(ring->phys + ((MTK_DMA_SIZE - 1) * sz));
1653 	ring->thresh = MAX_SKB_FRAGS;
1654 
1655 	/* make sure that all changes to the dma ring are flushed before we
1656 	 * continue
1657 	 */
1658 	wmb();
1659 
1660 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA)) {
1661 		mtk_w32(eth, ring->phys, MTK_QTX_CTX_PTR);
1662 		mtk_w32(eth, ring->phys, MTK_QTX_DTX_PTR);
1663 		mtk_w32(eth,
1664 			ring->phys + ((MTK_DMA_SIZE - 1) * sz),
1665 			MTK_QTX_CRX_PTR);
1666 		mtk_w32(eth, ring->last_free_ptr, MTK_QTX_DRX_PTR);
1667 		mtk_w32(eth, (QDMA_RES_THRES << 8) | QDMA_RES_THRES,
1668 			MTK_QTX_CFG(0));
1669 	} else {
1670 		mtk_w32(eth, ring->phys_pdma, MT7628_TX_BASE_PTR0);
1671 		mtk_w32(eth, MTK_DMA_SIZE, MT7628_TX_MAX_CNT0);
1672 		mtk_w32(eth, 0, MT7628_TX_CTX_IDX0);
1673 		mtk_w32(eth, MT7628_PST_DTX_IDX0, MTK_PDMA_RST_IDX);
1674 	}
1675 
1676 	return 0;
1677 
1678 no_tx_mem:
1679 	return -ENOMEM;
1680 }
1681 
mtk_tx_clean(struct mtk_eth * eth)1682 static void mtk_tx_clean(struct mtk_eth *eth)
1683 {
1684 	struct mtk_tx_ring *ring = &eth->tx_ring;
1685 	int i;
1686 
1687 	if (ring->buf) {
1688 		for (i = 0; i < MTK_DMA_SIZE; i++)
1689 			mtk_tx_unmap(eth, &ring->buf[i], false);
1690 		kfree(ring->buf);
1691 		ring->buf = NULL;
1692 	}
1693 
1694 	if (ring->dma) {
1695 		dma_free_coherent(eth->dev,
1696 				  MTK_DMA_SIZE * sizeof(*ring->dma),
1697 				  ring->dma,
1698 				  ring->phys);
1699 		ring->dma = NULL;
1700 	}
1701 
1702 	if (ring->dma_pdma) {
1703 		dma_free_coherent(eth->dev,
1704 				  MTK_DMA_SIZE * sizeof(*ring->dma_pdma),
1705 				  ring->dma_pdma,
1706 				  ring->phys_pdma);
1707 		ring->dma_pdma = NULL;
1708 	}
1709 }
1710 
mtk_rx_alloc(struct mtk_eth * eth,int ring_no,int rx_flag)1711 static int mtk_rx_alloc(struct mtk_eth *eth, int ring_no, int rx_flag)
1712 {
1713 	struct mtk_rx_ring *ring;
1714 	int rx_data_len, rx_dma_size;
1715 	int i;
1716 	u32 offset = 0;
1717 
1718 	if (rx_flag == MTK_RX_FLAGS_QDMA) {
1719 		if (ring_no)
1720 			return -EINVAL;
1721 		ring = &eth->rx_ring_qdma;
1722 		offset = 0x1000;
1723 	} else {
1724 		ring = &eth->rx_ring[ring_no];
1725 	}
1726 
1727 	if (rx_flag == MTK_RX_FLAGS_HWLRO) {
1728 		rx_data_len = MTK_MAX_LRO_RX_LENGTH;
1729 		rx_dma_size = MTK_HW_LRO_DMA_SIZE;
1730 	} else {
1731 		rx_data_len = ETH_DATA_LEN;
1732 		rx_dma_size = MTK_DMA_SIZE;
1733 	}
1734 
1735 	ring->frag_size = mtk_max_frag_size(rx_data_len);
1736 	ring->buf_size = mtk_max_buf_size(ring->frag_size);
1737 	ring->data = kcalloc(rx_dma_size, sizeof(*ring->data),
1738 			     GFP_KERNEL);
1739 	if (!ring->data)
1740 		return -ENOMEM;
1741 
1742 	for (i = 0; i < rx_dma_size; i++) {
1743 		if (ring->frag_size <= PAGE_SIZE)
1744 			ring->data[i] = netdev_alloc_frag(ring->frag_size);
1745 		else
1746 			ring->data[i] = mtk_max_lro_buf_alloc(GFP_KERNEL);
1747 		if (!ring->data[i])
1748 			return -ENOMEM;
1749 	}
1750 
1751 	ring->dma = dma_alloc_coherent(eth->dev,
1752 				       rx_dma_size * sizeof(*ring->dma),
1753 				       &ring->phys, GFP_ATOMIC);
1754 	if (!ring->dma)
1755 		return -ENOMEM;
1756 
1757 	for (i = 0; i < rx_dma_size; i++) {
1758 		dma_addr_t dma_addr = dma_map_single(eth->dev,
1759 				ring->data[i] + NET_SKB_PAD + eth->ip_align,
1760 				ring->buf_size,
1761 				DMA_FROM_DEVICE);
1762 		if (unlikely(dma_mapping_error(eth->dev, dma_addr)))
1763 			return -ENOMEM;
1764 		ring->dma[i].rxd1 = (unsigned int)dma_addr;
1765 
1766 		if (MTK_HAS_CAPS(eth->soc->caps, MTK_SOC_MT7628))
1767 			ring->dma[i].rxd2 = RX_DMA_LSO;
1768 		else
1769 			ring->dma[i].rxd2 = RX_DMA_PLEN0(ring->buf_size);
1770 	}
1771 	ring->dma_size = rx_dma_size;
1772 	ring->calc_idx_update = false;
1773 	ring->calc_idx = rx_dma_size - 1;
1774 	ring->crx_idx_reg = MTK_PRX_CRX_IDX_CFG(ring_no);
1775 	/* make sure that all changes to the dma ring are flushed before we
1776 	 * continue
1777 	 */
1778 	wmb();
1779 
1780 	mtk_w32(eth, ring->phys, MTK_PRX_BASE_PTR_CFG(ring_no) + offset);
1781 	mtk_w32(eth, rx_dma_size, MTK_PRX_MAX_CNT_CFG(ring_no) + offset);
1782 	mtk_w32(eth, ring->calc_idx, ring->crx_idx_reg + offset);
1783 	mtk_w32(eth, MTK_PST_DRX_IDX_CFG(ring_no), MTK_PDMA_RST_IDX + offset);
1784 
1785 	return 0;
1786 }
1787 
mtk_rx_clean(struct mtk_eth * eth,struct mtk_rx_ring * ring)1788 static void mtk_rx_clean(struct mtk_eth *eth, struct mtk_rx_ring *ring)
1789 {
1790 	int i;
1791 
1792 	if (ring->data && ring->dma) {
1793 		for (i = 0; i < ring->dma_size; i++) {
1794 			if (!ring->data[i])
1795 				continue;
1796 			if (!ring->dma[i].rxd1)
1797 				continue;
1798 			dma_unmap_single(eth->dev,
1799 					 ring->dma[i].rxd1,
1800 					 ring->buf_size,
1801 					 DMA_FROM_DEVICE);
1802 			skb_free_frag(ring->data[i]);
1803 		}
1804 		kfree(ring->data);
1805 		ring->data = NULL;
1806 	}
1807 
1808 	if (ring->dma) {
1809 		dma_free_coherent(eth->dev,
1810 				  ring->dma_size * sizeof(*ring->dma),
1811 				  ring->dma,
1812 				  ring->phys);
1813 		ring->dma = NULL;
1814 	}
1815 }
1816 
mtk_hwlro_rx_init(struct mtk_eth * eth)1817 static int mtk_hwlro_rx_init(struct mtk_eth *eth)
1818 {
1819 	int i;
1820 	u32 ring_ctrl_dw1 = 0, ring_ctrl_dw2 = 0, ring_ctrl_dw3 = 0;
1821 	u32 lro_ctrl_dw0 = 0, lro_ctrl_dw3 = 0;
1822 
1823 	/* set LRO rings to auto-learn modes */
1824 	ring_ctrl_dw2 |= MTK_RING_AUTO_LERAN_MODE;
1825 
1826 	/* validate LRO ring */
1827 	ring_ctrl_dw2 |= MTK_RING_VLD;
1828 
1829 	/* set AGE timer (unit: 20us) */
1830 	ring_ctrl_dw2 |= MTK_RING_AGE_TIME_H;
1831 	ring_ctrl_dw1 |= MTK_RING_AGE_TIME_L;
1832 
1833 	/* set max AGG timer (unit: 20us) */
1834 	ring_ctrl_dw2 |= MTK_RING_MAX_AGG_TIME;
1835 
1836 	/* set max LRO AGG count */
1837 	ring_ctrl_dw2 |= MTK_RING_MAX_AGG_CNT_L;
1838 	ring_ctrl_dw3 |= MTK_RING_MAX_AGG_CNT_H;
1839 
1840 	for (i = 1; i < MTK_MAX_RX_RING_NUM; i++) {
1841 		mtk_w32(eth, ring_ctrl_dw1, MTK_LRO_CTRL_DW1_CFG(i));
1842 		mtk_w32(eth, ring_ctrl_dw2, MTK_LRO_CTRL_DW2_CFG(i));
1843 		mtk_w32(eth, ring_ctrl_dw3, MTK_LRO_CTRL_DW3_CFG(i));
1844 	}
1845 
1846 	/* IPv4 checksum update enable */
1847 	lro_ctrl_dw0 |= MTK_L3_CKS_UPD_EN;
1848 
1849 	/* switch priority comparison to packet count mode */
1850 	lro_ctrl_dw0 |= MTK_LRO_ALT_PKT_CNT_MODE;
1851 
1852 	/* bandwidth threshold setting */
1853 	mtk_w32(eth, MTK_HW_LRO_BW_THRE, MTK_PDMA_LRO_CTRL_DW2);
1854 
1855 	/* auto-learn score delta setting */
1856 	mtk_w32(eth, MTK_HW_LRO_REPLACE_DELTA, MTK_PDMA_LRO_ALT_SCORE_DELTA);
1857 
1858 	/* set refresh timer for altering flows to 1 sec. (unit: 20us) */
1859 	mtk_w32(eth, (MTK_HW_LRO_TIMER_UNIT << 16) | MTK_HW_LRO_REFRESH_TIME,
1860 		MTK_PDMA_LRO_ALT_REFRESH_TIMER);
1861 
1862 	/* set HW LRO mode & the max aggregation count for rx packets */
1863 	lro_ctrl_dw3 |= MTK_ADMA_MODE | (MTK_HW_LRO_MAX_AGG_CNT & 0xff);
1864 
1865 	/* the minimal remaining room of SDL0 in RXD for lro aggregation */
1866 	lro_ctrl_dw3 |= MTK_LRO_MIN_RXD_SDL;
1867 
1868 	/* enable HW LRO */
1869 	lro_ctrl_dw0 |= MTK_LRO_EN;
1870 
1871 	mtk_w32(eth, lro_ctrl_dw3, MTK_PDMA_LRO_CTRL_DW3);
1872 	mtk_w32(eth, lro_ctrl_dw0, MTK_PDMA_LRO_CTRL_DW0);
1873 
1874 	return 0;
1875 }
1876 
mtk_hwlro_rx_uninit(struct mtk_eth * eth)1877 static void mtk_hwlro_rx_uninit(struct mtk_eth *eth)
1878 {
1879 	int i;
1880 	u32 val;
1881 
1882 	/* relinquish lro rings, flush aggregated packets */
1883 	mtk_w32(eth, MTK_LRO_RING_RELINQUISH_REQ, MTK_PDMA_LRO_CTRL_DW0);
1884 
1885 	/* wait for relinquishments done */
1886 	for (i = 0; i < 10; i++) {
1887 		val = mtk_r32(eth, MTK_PDMA_LRO_CTRL_DW0);
1888 		if (val & MTK_LRO_RING_RELINQUISH_DONE) {
1889 			msleep(20);
1890 			continue;
1891 		}
1892 		break;
1893 	}
1894 
1895 	/* invalidate lro rings */
1896 	for (i = 1; i < MTK_MAX_RX_RING_NUM; i++)
1897 		mtk_w32(eth, 0, MTK_LRO_CTRL_DW2_CFG(i));
1898 
1899 	/* disable HW LRO */
1900 	mtk_w32(eth, 0, MTK_PDMA_LRO_CTRL_DW0);
1901 }
1902 
mtk_hwlro_val_ipaddr(struct mtk_eth * eth,int idx,__be32 ip)1903 static void mtk_hwlro_val_ipaddr(struct mtk_eth *eth, int idx, __be32 ip)
1904 {
1905 	u32 reg_val;
1906 
1907 	reg_val = mtk_r32(eth, MTK_LRO_CTRL_DW2_CFG(idx));
1908 
1909 	/* invalidate the IP setting */
1910 	mtk_w32(eth, (reg_val & ~MTK_RING_MYIP_VLD), MTK_LRO_CTRL_DW2_CFG(idx));
1911 
1912 	mtk_w32(eth, ip, MTK_LRO_DIP_DW0_CFG(idx));
1913 
1914 	/* validate the IP setting */
1915 	mtk_w32(eth, (reg_val | MTK_RING_MYIP_VLD), MTK_LRO_CTRL_DW2_CFG(idx));
1916 }
1917 
mtk_hwlro_inval_ipaddr(struct mtk_eth * eth,int idx)1918 static void mtk_hwlro_inval_ipaddr(struct mtk_eth *eth, int idx)
1919 {
1920 	u32 reg_val;
1921 
1922 	reg_val = mtk_r32(eth, MTK_LRO_CTRL_DW2_CFG(idx));
1923 
1924 	/* invalidate the IP setting */
1925 	mtk_w32(eth, (reg_val & ~MTK_RING_MYIP_VLD), MTK_LRO_CTRL_DW2_CFG(idx));
1926 
1927 	mtk_w32(eth, 0, MTK_LRO_DIP_DW0_CFG(idx));
1928 }
1929 
mtk_hwlro_get_ip_cnt(struct mtk_mac * mac)1930 static int mtk_hwlro_get_ip_cnt(struct mtk_mac *mac)
1931 {
1932 	int cnt = 0;
1933 	int i;
1934 
1935 	for (i = 0; i < MTK_MAX_LRO_IP_CNT; i++) {
1936 		if (mac->hwlro_ip[i])
1937 			cnt++;
1938 	}
1939 
1940 	return cnt;
1941 }
1942 
mtk_hwlro_add_ipaddr(struct net_device * dev,struct ethtool_rxnfc * cmd)1943 static int mtk_hwlro_add_ipaddr(struct net_device *dev,
1944 				struct ethtool_rxnfc *cmd)
1945 {
1946 	struct ethtool_rx_flow_spec *fsp =
1947 		(struct ethtool_rx_flow_spec *)&cmd->fs;
1948 	struct mtk_mac *mac = netdev_priv(dev);
1949 	struct mtk_eth *eth = mac->hw;
1950 	int hwlro_idx;
1951 
1952 	if ((fsp->flow_type != TCP_V4_FLOW) ||
1953 	    (!fsp->h_u.tcp_ip4_spec.ip4dst) ||
1954 	    (fsp->location > 1))
1955 		return -EINVAL;
1956 
1957 	mac->hwlro_ip[fsp->location] = htonl(fsp->h_u.tcp_ip4_spec.ip4dst);
1958 	hwlro_idx = (mac->id * MTK_MAX_LRO_IP_CNT) + fsp->location;
1959 
1960 	mac->hwlro_ip_cnt = mtk_hwlro_get_ip_cnt(mac);
1961 
1962 	mtk_hwlro_val_ipaddr(eth, hwlro_idx, mac->hwlro_ip[fsp->location]);
1963 
1964 	return 0;
1965 }
1966 
mtk_hwlro_del_ipaddr(struct net_device * dev,struct ethtool_rxnfc * cmd)1967 static int mtk_hwlro_del_ipaddr(struct net_device *dev,
1968 				struct ethtool_rxnfc *cmd)
1969 {
1970 	struct ethtool_rx_flow_spec *fsp =
1971 		(struct ethtool_rx_flow_spec *)&cmd->fs;
1972 	struct mtk_mac *mac = netdev_priv(dev);
1973 	struct mtk_eth *eth = mac->hw;
1974 	int hwlro_idx;
1975 
1976 	if (fsp->location > 1)
1977 		return -EINVAL;
1978 
1979 	mac->hwlro_ip[fsp->location] = 0;
1980 	hwlro_idx = (mac->id * MTK_MAX_LRO_IP_CNT) + fsp->location;
1981 
1982 	mac->hwlro_ip_cnt = mtk_hwlro_get_ip_cnt(mac);
1983 
1984 	mtk_hwlro_inval_ipaddr(eth, hwlro_idx);
1985 
1986 	return 0;
1987 }
1988 
mtk_hwlro_netdev_disable(struct net_device * dev)1989 static void mtk_hwlro_netdev_disable(struct net_device *dev)
1990 {
1991 	struct mtk_mac *mac = netdev_priv(dev);
1992 	struct mtk_eth *eth = mac->hw;
1993 	int i, hwlro_idx;
1994 
1995 	for (i = 0; i < MTK_MAX_LRO_IP_CNT; i++) {
1996 		mac->hwlro_ip[i] = 0;
1997 		hwlro_idx = (mac->id * MTK_MAX_LRO_IP_CNT) + i;
1998 
1999 		mtk_hwlro_inval_ipaddr(eth, hwlro_idx);
2000 	}
2001 
2002 	mac->hwlro_ip_cnt = 0;
2003 }
2004 
mtk_hwlro_get_fdir_entry(struct net_device * dev,struct ethtool_rxnfc * cmd)2005 static int mtk_hwlro_get_fdir_entry(struct net_device *dev,
2006 				    struct ethtool_rxnfc *cmd)
2007 {
2008 	struct mtk_mac *mac = netdev_priv(dev);
2009 	struct ethtool_rx_flow_spec *fsp =
2010 		(struct ethtool_rx_flow_spec *)&cmd->fs;
2011 
2012 	if (fsp->location >= ARRAY_SIZE(mac->hwlro_ip))
2013 		return -EINVAL;
2014 
2015 	/* only tcp dst ipv4 is meaningful, others are meaningless */
2016 	fsp->flow_type = TCP_V4_FLOW;
2017 	fsp->h_u.tcp_ip4_spec.ip4dst = ntohl(mac->hwlro_ip[fsp->location]);
2018 	fsp->m_u.tcp_ip4_spec.ip4dst = 0;
2019 
2020 	fsp->h_u.tcp_ip4_spec.ip4src = 0;
2021 	fsp->m_u.tcp_ip4_spec.ip4src = 0xffffffff;
2022 	fsp->h_u.tcp_ip4_spec.psrc = 0;
2023 	fsp->m_u.tcp_ip4_spec.psrc = 0xffff;
2024 	fsp->h_u.tcp_ip4_spec.pdst = 0;
2025 	fsp->m_u.tcp_ip4_spec.pdst = 0xffff;
2026 	fsp->h_u.tcp_ip4_spec.tos = 0;
2027 	fsp->m_u.tcp_ip4_spec.tos = 0xff;
2028 
2029 	return 0;
2030 }
2031 
mtk_hwlro_get_fdir_all(struct net_device * dev,struct ethtool_rxnfc * cmd,u32 * rule_locs)2032 static int mtk_hwlro_get_fdir_all(struct net_device *dev,
2033 				  struct ethtool_rxnfc *cmd,
2034 				  u32 *rule_locs)
2035 {
2036 	struct mtk_mac *mac = netdev_priv(dev);
2037 	int cnt = 0;
2038 	int i;
2039 
2040 	for (i = 0; i < MTK_MAX_LRO_IP_CNT; i++) {
2041 		if (cnt == cmd->rule_cnt)
2042 			return -EMSGSIZE;
2043 
2044 		if (mac->hwlro_ip[i]) {
2045 			rule_locs[cnt] = i;
2046 			cnt++;
2047 		}
2048 	}
2049 
2050 	cmd->rule_cnt = cnt;
2051 
2052 	return 0;
2053 }
2054 
mtk_fix_features(struct net_device * dev,netdev_features_t features)2055 static netdev_features_t mtk_fix_features(struct net_device *dev,
2056 					  netdev_features_t features)
2057 {
2058 	if (!(features & NETIF_F_LRO)) {
2059 		struct mtk_mac *mac = netdev_priv(dev);
2060 		int ip_cnt = mtk_hwlro_get_ip_cnt(mac);
2061 
2062 		if (ip_cnt) {
2063 			netdev_info(dev, "RX flow is programmed, LRO should keep on\n");
2064 
2065 			features |= NETIF_F_LRO;
2066 		}
2067 	}
2068 
2069 	return features;
2070 }
2071 
mtk_set_features(struct net_device * dev,netdev_features_t features)2072 static int mtk_set_features(struct net_device *dev, netdev_features_t features)
2073 {
2074 	int err = 0;
2075 
2076 	if (!((dev->features ^ features) & NETIF_F_LRO))
2077 		return 0;
2078 
2079 	if (!(features & NETIF_F_LRO))
2080 		mtk_hwlro_netdev_disable(dev);
2081 
2082 	return err;
2083 }
2084 
2085 /* wait for DMA to finish whatever it is doing before we start using it again */
mtk_dma_busy_wait(struct mtk_eth * eth)2086 static int mtk_dma_busy_wait(struct mtk_eth *eth)
2087 {
2088 	unsigned int reg;
2089 	int ret;
2090 	u32 val;
2091 
2092 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA))
2093 		reg = MTK_QDMA_GLO_CFG;
2094 	else
2095 		reg = MTK_PDMA_GLO_CFG;
2096 
2097 	ret = readx_poll_timeout_atomic(__raw_readl, eth->base + reg, val,
2098 					!(val & (MTK_RX_DMA_BUSY | MTK_TX_DMA_BUSY)),
2099 					5, MTK_DMA_BUSY_TIMEOUT_US);
2100 	if (ret)
2101 		dev_err(eth->dev, "DMA init timeout\n");
2102 
2103 	return ret;
2104 }
2105 
mtk_dma_init(struct mtk_eth * eth)2106 static int mtk_dma_init(struct mtk_eth *eth)
2107 {
2108 	int err;
2109 	u32 i;
2110 
2111 	if (mtk_dma_busy_wait(eth))
2112 		return -EBUSY;
2113 
2114 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA)) {
2115 		/* QDMA needs scratch memory for internal reordering of the
2116 		 * descriptors
2117 		 */
2118 		err = mtk_init_fq_dma(eth);
2119 		if (err)
2120 			return err;
2121 	}
2122 
2123 	err = mtk_tx_alloc(eth);
2124 	if (err)
2125 		return err;
2126 
2127 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA)) {
2128 		err = mtk_rx_alloc(eth, 0, MTK_RX_FLAGS_QDMA);
2129 		if (err)
2130 			return err;
2131 	}
2132 
2133 	err = mtk_rx_alloc(eth, 0, MTK_RX_FLAGS_NORMAL);
2134 	if (err)
2135 		return err;
2136 
2137 	if (eth->hwlro) {
2138 		for (i = 1; i < MTK_MAX_RX_RING_NUM; i++) {
2139 			err = mtk_rx_alloc(eth, i, MTK_RX_FLAGS_HWLRO);
2140 			if (err)
2141 				return err;
2142 		}
2143 		err = mtk_hwlro_rx_init(eth);
2144 		if (err)
2145 			return err;
2146 	}
2147 
2148 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA)) {
2149 		/* Enable random early drop and set drop threshold
2150 		 * automatically
2151 		 */
2152 		mtk_w32(eth, FC_THRES_DROP_MODE | FC_THRES_DROP_EN |
2153 			FC_THRES_MIN, MTK_QDMA_FC_THRES);
2154 		mtk_w32(eth, 0x0, MTK_QDMA_HRED2);
2155 	}
2156 
2157 	return 0;
2158 }
2159 
mtk_dma_free(struct mtk_eth * eth)2160 static void mtk_dma_free(struct mtk_eth *eth)
2161 {
2162 	int i;
2163 
2164 	for (i = 0; i < MTK_MAC_COUNT; i++)
2165 		if (eth->netdev[i])
2166 			netdev_reset_queue(eth->netdev[i]);
2167 	if (eth->scratch_ring) {
2168 		dma_free_coherent(eth->dev,
2169 				  MTK_DMA_SIZE * sizeof(struct mtk_tx_dma),
2170 				  eth->scratch_ring,
2171 				  eth->phy_scratch_ring);
2172 		eth->scratch_ring = NULL;
2173 		eth->phy_scratch_ring = 0;
2174 	}
2175 	mtk_tx_clean(eth);
2176 	mtk_rx_clean(eth, &eth->rx_ring[0]);
2177 	mtk_rx_clean(eth, &eth->rx_ring_qdma);
2178 
2179 	if (eth->hwlro) {
2180 		mtk_hwlro_rx_uninit(eth);
2181 		for (i = 1; i < MTK_MAX_RX_RING_NUM; i++)
2182 			mtk_rx_clean(eth, &eth->rx_ring[i]);
2183 	}
2184 
2185 	kfree(eth->scratch_head);
2186 }
2187 
mtk_tx_timeout(struct net_device * dev,unsigned int txqueue)2188 static void mtk_tx_timeout(struct net_device *dev, unsigned int txqueue)
2189 {
2190 	struct mtk_mac *mac = netdev_priv(dev);
2191 	struct mtk_eth *eth = mac->hw;
2192 
2193 	eth->netdev[mac->id]->stats.tx_errors++;
2194 	netif_err(eth, tx_err, dev,
2195 		  "transmit timed out\n");
2196 	schedule_work(&eth->pending_work);
2197 }
2198 
mtk_handle_irq_rx(int irq,void * _eth)2199 static irqreturn_t mtk_handle_irq_rx(int irq, void *_eth)
2200 {
2201 	struct mtk_eth *eth = _eth;
2202 
2203 	eth->rx_events++;
2204 	if (likely(napi_schedule_prep(&eth->rx_napi))) {
2205 		__napi_schedule(&eth->rx_napi);
2206 		mtk_rx_irq_disable(eth, MTK_RX_DONE_INT);
2207 	}
2208 
2209 	return IRQ_HANDLED;
2210 }
2211 
mtk_handle_irq_tx(int irq,void * _eth)2212 static irqreturn_t mtk_handle_irq_tx(int irq, void *_eth)
2213 {
2214 	struct mtk_eth *eth = _eth;
2215 
2216 	eth->tx_events++;
2217 	if (likely(napi_schedule_prep(&eth->tx_napi))) {
2218 		__napi_schedule(&eth->tx_napi);
2219 		mtk_tx_irq_disable(eth, MTK_TX_DONE_INT);
2220 	}
2221 
2222 	return IRQ_HANDLED;
2223 }
2224 
mtk_handle_irq(int irq,void * _eth)2225 static irqreturn_t mtk_handle_irq(int irq, void *_eth)
2226 {
2227 	struct mtk_eth *eth = _eth;
2228 
2229 	if (mtk_r32(eth, MTK_PDMA_INT_MASK) & MTK_RX_DONE_INT) {
2230 		if (mtk_r32(eth, MTK_PDMA_INT_STATUS) & MTK_RX_DONE_INT)
2231 			mtk_handle_irq_rx(irq, _eth);
2232 	}
2233 	if (mtk_r32(eth, eth->tx_int_mask_reg) & MTK_TX_DONE_INT) {
2234 		if (mtk_r32(eth, eth->tx_int_status_reg) & MTK_TX_DONE_INT)
2235 			mtk_handle_irq_tx(irq, _eth);
2236 	}
2237 
2238 	return IRQ_HANDLED;
2239 }
2240 
2241 #ifdef CONFIG_NET_POLL_CONTROLLER
mtk_poll_controller(struct net_device * dev)2242 static void mtk_poll_controller(struct net_device *dev)
2243 {
2244 	struct mtk_mac *mac = netdev_priv(dev);
2245 	struct mtk_eth *eth = mac->hw;
2246 
2247 	mtk_tx_irq_disable(eth, MTK_TX_DONE_INT);
2248 	mtk_rx_irq_disable(eth, MTK_RX_DONE_INT);
2249 	mtk_handle_irq_rx(eth->irq[2], dev);
2250 	mtk_tx_irq_enable(eth, MTK_TX_DONE_INT);
2251 	mtk_rx_irq_enable(eth, MTK_RX_DONE_INT);
2252 }
2253 #endif
2254 
mtk_start_dma(struct mtk_eth * eth)2255 static int mtk_start_dma(struct mtk_eth *eth)
2256 {
2257 	u32 rx_2b_offset = (NET_IP_ALIGN == 2) ? MTK_RX_2B_OFFSET : 0;
2258 	int err;
2259 
2260 	err = mtk_dma_init(eth);
2261 	if (err) {
2262 		mtk_dma_free(eth);
2263 		return err;
2264 	}
2265 
2266 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA)) {
2267 		mtk_w32(eth,
2268 			MTK_TX_WB_DDONE | MTK_TX_DMA_EN |
2269 			MTK_TX_BT_32DWORDS | MTK_NDP_CO_PRO |
2270 			MTK_RX_DMA_EN | MTK_RX_2B_OFFSET |
2271 			MTK_RX_BT_32DWORDS,
2272 			MTK_QDMA_GLO_CFG);
2273 
2274 		mtk_w32(eth,
2275 			MTK_RX_DMA_EN | rx_2b_offset |
2276 			MTK_RX_BT_32DWORDS | MTK_MULTI_EN,
2277 			MTK_PDMA_GLO_CFG);
2278 	} else {
2279 		mtk_w32(eth, MTK_TX_WB_DDONE | MTK_TX_DMA_EN | MTK_RX_DMA_EN |
2280 			MTK_MULTI_EN | MTK_PDMA_SIZE_8DWORDS,
2281 			MTK_PDMA_GLO_CFG);
2282 	}
2283 
2284 	return 0;
2285 }
2286 
mtk_gdm_config(struct mtk_eth * eth,u32 config)2287 static void mtk_gdm_config(struct mtk_eth *eth, u32 config)
2288 {
2289 	int i;
2290 
2291 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_SOC_MT7628))
2292 		return;
2293 
2294 	for (i = 0; i < MTK_MAC_COUNT; i++) {
2295 		u32 val = mtk_r32(eth, MTK_GDMA_FWD_CFG(i));
2296 
2297 		/* default setup the forward port to send frame to PDMA */
2298 		val &= ~0xffff;
2299 
2300 		/* Enable RX checksum */
2301 		val |= MTK_GDMA_ICS_EN | MTK_GDMA_TCS_EN | MTK_GDMA_UCS_EN;
2302 
2303 		val |= config;
2304 
2305 		if (!i && eth->netdev[0] && netdev_uses_dsa(eth->netdev[0]))
2306 			val |= MTK_GDMA_SPECIAL_TAG;
2307 
2308 		mtk_w32(eth, val, MTK_GDMA_FWD_CFG(i));
2309 	}
2310 	/* Reset and enable PSE */
2311 	mtk_w32(eth, RST_GL_PSE, MTK_RST_GL);
2312 	mtk_w32(eth, 0, MTK_RST_GL);
2313 }
2314 
mtk_open(struct net_device * dev)2315 static int mtk_open(struct net_device *dev)
2316 {
2317 	struct mtk_mac *mac = netdev_priv(dev);
2318 	struct mtk_eth *eth = mac->hw;
2319 	int err;
2320 
2321 	err = phylink_of_phy_connect(mac->phylink, mac->of_node, 0);
2322 	if (err) {
2323 		netdev_err(dev, "%s: could not attach PHY: %d\n", __func__,
2324 			   err);
2325 		return err;
2326 	}
2327 
2328 	/* we run 2 netdevs on the same dma ring so we only bring it up once */
2329 	if (!refcount_read(&eth->dma_refcnt)) {
2330 		u32 gdm_config = MTK_GDMA_TO_PDMA;
2331 		int err;
2332 
2333 		err = mtk_start_dma(eth);
2334 		if (err) {
2335 			phylink_disconnect_phy(mac->phylink);
2336 			return err;
2337 		}
2338 
2339 		if (eth->soc->offload_version && mtk_ppe_start(&eth->ppe) == 0)
2340 			gdm_config = MTK_GDMA_TO_PPE;
2341 
2342 		mtk_gdm_config(eth, gdm_config);
2343 
2344 		napi_enable(&eth->tx_napi);
2345 		napi_enable(&eth->rx_napi);
2346 		mtk_tx_irq_enable(eth, MTK_TX_DONE_INT);
2347 		mtk_rx_irq_enable(eth, MTK_RX_DONE_INT);
2348 		refcount_set(&eth->dma_refcnt, 1);
2349 	}
2350 	else
2351 		refcount_inc(&eth->dma_refcnt);
2352 
2353 	phylink_start(mac->phylink);
2354 	netif_start_queue(dev);
2355 	return 0;
2356 }
2357 
mtk_stop_dma(struct mtk_eth * eth,u32 glo_cfg)2358 static void mtk_stop_dma(struct mtk_eth *eth, u32 glo_cfg)
2359 {
2360 	u32 val;
2361 	int i;
2362 
2363 	/* stop the dma engine */
2364 	spin_lock_bh(&eth->page_lock);
2365 	val = mtk_r32(eth, glo_cfg);
2366 	mtk_w32(eth, val & ~(MTK_TX_WB_DDONE | MTK_RX_DMA_EN | MTK_TX_DMA_EN),
2367 		glo_cfg);
2368 	spin_unlock_bh(&eth->page_lock);
2369 
2370 	/* wait for dma stop */
2371 	for (i = 0; i < 10; i++) {
2372 		val = mtk_r32(eth, glo_cfg);
2373 		if (val & (MTK_TX_DMA_BUSY | MTK_RX_DMA_BUSY)) {
2374 			msleep(20);
2375 			continue;
2376 		}
2377 		break;
2378 	}
2379 }
2380 
mtk_stop(struct net_device * dev)2381 static int mtk_stop(struct net_device *dev)
2382 {
2383 	struct mtk_mac *mac = netdev_priv(dev);
2384 	struct mtk_eth *eth = mac->hw;
2385 
2386 	phylink_stop(mac->phylink);
2387 
2388 	netif_tx_disable(dev);
2389 
2390 	phylink_disconnect_phy(mac->phylink);
2391 
2392 	/* only shutdown DMA if this is the last user */
2393 	if (!refcount_dec_and_test(&eth->dma_refcnt))
2394 		return 0;
2395 
2396 	mtk_gdm_config(eth, MTK_GDMA_DROP_ALL);
2397 
2398 	mtk_tx_irq_disable(eth, MTK_TX_DONE_INT);
2399 	mtk_rx_irq_disable(eth, MTK_RX_DONE_INT);
2400 	napi_disable(&eth->tx_napi);
2401 	napi_disable(&eth->rx_napi);
2402 
2403 	cancel_work_sync(&eth->rx_dim.work);
2404 	cancel_work_sync(&eth->tx_dim.work);
2405 
2406 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA))
2407 		mtk_stop_dma(eth, MTK_QDMA_GLO_CFG);
2408 	mtk_stop_dma(eth, MTK_PDMA_GLO_CFG);
2409 
2410 	mtk_dma_free(eth);
2411 
2412 	if (eth->soc->offload_version)
2413 		mtk_ppe_stop(&eth->ppe);
2414 
2415 	return 0;
2416 }
2417 
ethsys_reset(struct mtk_eth * eth,u32 reset_bits)2418 static void ethsys_reset(struct mtk_eth *eth, u32 reset_bits)
2419 {
2420 	regmap_update_bits(eth->ethsys, ETHSYS_RSTCTRL,
2421 			   reset_bits,
2422 			   reset_bits);
2423 
2424 	usleep_range(1000, 1100);
2425 	regmap_update_bits(eth->ethsys, ETHSYS_RSTCTRL,
2426 			   reset_bits,
2427 			   ~reset_bits);
2428 	mdelay(10);
2429 }
2430 
mtk_clk_disable(struct mtk_eth * eth)2431 static void mtk_clk_disable(struct mtk_eth *eth)
2432 {
2433 	int clk;
2434 
2435 	for (clk = MTK_CLK_MAX - 1; clk >= 0; clk--)
2436 		clk_disable_unprepare(eth->clks[clk]);
2437 }
2438 
mtk_clk_enable(struct mtk_eth * eth)2439 static int mtk_clk_enable(struct mtk_eth *eth)
2440 {
2441 	int clk, ret;
2442 
2443 	for (clk = 0; clk < MTK_CLK_MAX ; clk++) {
2444 		ret = clk_prepare_enable(eth->clks[clk]);
2445 		if (ret)
2446 			goto err_disable_clks;
2447 	}
2448 
2449 	return 0;
2450 
2451 err_disable_clks:
2452 	while (--clk >= 0)
2453 		clk_disable_unprepare(eth->clks[clk]);
2454 
2455 	return ret;
2456 }
2457 
mtk_dim_rx(struct work_struct * work)2458 static void mtk_dim_rx(struct work_struct *work)
2459 {
2460 	struct dim *dim = container_of(work, struct dim, work);
2461 	struct mtk_eth *eth = container_of(dim, struct mtk_eth, rx_dim);
2462 	struct dim_cq_moder cur_profile;
2463 	u32 val, cur;
2464 
2465 	cur_profile = net_dim_get_rx_moderation(eth->rx_dim.mode,
2466 						dim->profile_ix);
2467 	spin_lock_bh(&eth->dim_lock);
2468 
2469 	val = mtk_r32(eth, MTK_PDMA_DELAY_INT);
2470 	val &= MTK_PDMA_DELAY_TX_MASK;
2471 	val |= MTK_PDMA_DELAY_RX_EN;
2472 
2473 	cur = min_t(u32, DIV_ROUND_UP(cur_profile.usec, 20), MTK_PDMA_DELAY_PTIME_MASK);
2474 	val |= cur << MTK_PDMA_DELAY_RX_PTIME_SHIFT;
2475 
2476 	cur = min_t(u32, cur_profile.pkts, MTK_PDMA_DELAY_PINT_MASK);
2477 	val |= cur << MTK_PDMA_DELAY_RX_PINT_SHIFT;
2478 
2479 	mtk_w32(eth, val, MTK_PDMA_DELAY_INT);
2480 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA))
2481 		mtk_w32(eth, val, MTK_QDMA_DELAY_INT);
2482 
2483 	spin_unlock_bh(&eth->dim_lock);
2484 
2485 	dim->state = DIM_START_MEASURE;
2486 }
2487 
mtk_dim_tx(struct work_struct * work)2488 static void mtk_dim_tx(struct work_struct *work)
2489 {
2490 	struct dim *dim = container_of(work, struct dim, work);
2491 	struct mtk_eth *eth = container_of(dim, struct mtk_eth, tx_dim);
2492 	struct dim_cq_moder cur_profile;
2493 	u32 val, cur;
2494 
2495 	cur_profile = net_dim_get_tx_moderation(eth->tx_dim.mode,
2496 						dim->profile_ix);
2497 	spin_lock_bh(&eth->dim_lock);
2498 
2499 	val = mtk_r32(eth, MTK_PDMA_DELAY_INT);
2500 	val &= MTK_PDMA_DELAY_RX_MASK;
2501 	val |= MTK_PDMA_DELAY_TX_EN;
2502 
2503 	cur = min_t(u32, DIV_ROUND_UP(cur_profile.usec, 20), MTK_PDMA_DELAY_PTIME_MASK);
2504 	val |= cur << MTK_PDMA_DELAY_TX_PTIME_SHIFT;
2505 
2506 	cur = min_t(u32, cur_profile.pkts, MTK_PDMA_DELAY_PINT_MASK);
2507 	val |= cur << MTK_PDMA_DELAY_TX_PINT_SHIFT;
2508 
2509 	mtk_w32(eth, val, MTK_PDMA_DELAY_INT);
2510 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA))
2511 		mtk_w32(eth, val, MTK_QDMA_DELAY_INT);
2512 
2513 	spin_unlock_bh(&eth->dim_lock);
2514 
2515 	dim->state = DIM_START_MEASURE;
2516 }
2517 
mtk_hw_init(struct mtk_eth * eth)2518 static int mtk_hw_init(struct mtk_eth *eth)
2519 {
2520 	int i, val, ret;
2521 
2522 	if (test_and_set_bit(MTK_HW_INIT, &eth->state))
2523 		return 0;
2524 
2525 	pm_runtime_enable(eth->dev);
2526 	pm_runtime_get_sync(eth->dev);
2527 
2528 	ret = mtk_clk_enable(eth);
2529 	if (ret)
2530 		goto err_disable_pm;
2531 
2532 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_SOC_MT7628)) {
2533 		ret = device_reset(eth->dev);
2534 		if (ret) {
2535 			dev_err(eth->dev, "MAC reset failed!\n");
2536 			goto err_disable_pm;
2537 		}
2538 
2539 		/* set interrupt delays based on current Net DIM sample */
2540 		mtk_dim_rx(&eth->rx_dim.work);
2541 		mtk_dim_tx(&eth->tx_dim.work);
2542 
2543 		/* disable delay and normal interrupt */
2544 		mtk_tx_irq_disable(eth, ~0);
2545 		mtk_rx_irq_disable(eth, ~0);
2546 
2547 		return 0;
2548 	}
2549 
2550 	/* Non-MT7628 handling... */
2551 	ethsys_reset(eth, RSTCTRL_FE);
2552 	ethsys_reset(eth, RSTCTRL_PPE);
2553 
2554 	if (eth->pctl) {
2555 		/* Set GE2 driving and slew rate */
2556 		regmap_write(eth->pctl, GPIO_DRV_SEL10, 0xa00);
2557 
2558 		/* set GE2 TDSEL */
2559 		regmap_write(eth->pctl, GPIO_OD33_CTRL8, 0x5);
2560 
2561 		/* set GE2 TUNE */
2562 		regmap_write(eth->pctl, GPIO_BIAS_CTRL, 0x0);
2563 	}
2564 
2565 	/* Set linkdown as the default for each GMAC. Its own MCR would be set
2566 	 * up with the more appropriate value when mtk_mac_config call is being
2567 	 * invoked.
2568 	 */
2569 	for (i = 0; i < MTK_MAC_COUNT; i++)
2570 		mtk_w32(eth, MAC_MCR_FORCE_LINK_DOWN, MTK_MAC_MCR(i));
2571 
2572 	/* Indicates CDM to parse the MTK special tag from CPU
2573 	 * which also is working out for untag packets.
2574 	 */
2575 	val = mtk_r32(eth, MTK_CDMQ_IG_CTRL);
2576 	mtk_w32(eth, val | MTK_CDMQ_STAG_EN, MTK_CDMQ_IG_CTRL);
2577 
2578 	/* Enable RX VLan Offloading */
2579 	mtk_w32(eth, 1, MTK_CDMP_EG_CTRL);
2580 
2581 	/* set interrupt delays based on current Net DIM sample */
2582 	mtk_dim_rx(&eth->rx_dim.work);
2583 	mtk_dim_tx(&eth->tx_dim.work);
2584 
2585 	/* disable delay and normal interrupt */
2586 	mtk_tx_irq_disable(eth, ~0);
2587 	mtk_rx_irq_disable(eth, ~0);
2588 
2589 	/* FE int grouping */
2590 	mtk_w32(eth, MTK_TX_DONE_INT, MTK_PDMA_INT_GRP1);
2591 	mtk_w32(eth, MTK_RX_DONE_INT, MTK_PDMA_INT_GRP2);
2592 	mtk_w32(eth, MTK_TX_DONE_INT, MTK_QDMA_INT_GRP1);
2593 	mtk_w32(eth, MTK_RX_DONE_INT, MTK_QDMA_INT_GRP2);
2594 	mtk_w32(eth, 0x21021000, MTK_FE_INT_GRP);
2595 
2596 	return 0;
2597 
2598 err_disable_pm:
2599 	pm_runtime_put_sync(eth->dev);
2600 	pm_runtime_disable(eth->dev);
2601 
2602 	return ret;
2603 }
2604 
mtk_hw_deinit(struct mtk_eth * eth)2605 static int mtk_hw_deinit(struct mtk_eth *eth)
2606 {
2607 	if (!test_and_clear_bit(MTK_HW_INIT, &eth->state))
2608 		return 0;
2609 
2610 	mtk_clk_disable(eth);
2611 
2612 	pm_runtime_put_sync(eth->dev);
2613 	pm_runtime_disable(eth->dev);
2614 
2615 	return 0;
2616 }
2617 
mtk_uninit(struct net_device * dev)2618 static void mtk_uninit(struct net_device *dev)
2619 {
2620 	struct mtk_mac *mac = netdev_priv(dev);
2621 	struct mtk_eth *eth = mac->hw;
2622 
2623 	phylink_disconnect_phy(mac->phylink);
2624 	mtk_tx_irq_disable(eth, ~0);
2625 	mtk_rx_irq_disable(eth, ~0);
2626 }
2627 
mtk_change_mtu(struct net_device * dev,int new_mtu)2628 static int mtk_change_mtu(struct net_device *dev, int new_mtu)
2629 {
2630 	int length = new_mtu + MTK_RX_ETH_HLEN;
2631 	struct mtk_mac *mac = netdev_priv(dev);
2632 	struct mtk_eth *eth = mac->hw;
2633 	u32 mcr_cur, mcr_new;
2634 
2635 	if (!MTK_HAS_CAPS(eth->soc->caps, MTK_SOC_MT7628)) {
2636 		mcr_cur = mtk_r32(mac->hw, MTK_MAC_MCR(mac->id));
2637 		mcr_new = mcr_cur & ~MAC_MCR_MAX_RX_MASK;
2638 
2639 		if (length <= 1518)
2640 			mcr_new |= MAC_MCR_MAX_RX(MAC_MCR_MAX_RX_1518);
2641 		else if (length <= 1536)
2642 			mcr_new |= MAC_MCR_MAX_RX(MAC_MCR_MAX_RX_1536);
2643 		else if (length <= 1552)
2644 			mcr_new |= MAC_MCR_MAX_RX(MAC_MCR_MAX_RX_1552);
2645 		else
2646 			mcr_new |= MAC_MCR_MAX_RX(MAC_MCR_MAX_RX_2048);
2647 
2648 		if (mcr_new != mcr_cur)
2649 			mtk_w32(mac->hw, mcr_new, MTK_MAC_MCR(mac->id));
2650 	}
2651 
2652 	dev->mtu = new_mtu;
2653 
2654 	return 0;
2655 }
2656 
mtk_do_ioctl(struct net_device * dev,struct ifreq * ifr,int cmd)2657 static int mtk_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2658 {
2659 	struct mtk_mac *mac = netdev_priv(dev);
2660 
2661 	switch (cmd) {
2662 	case SIOCGMIIPHY:
2663 	case SIOCGMIIREG:
2664 	case SIOCSMIIREG:
2665 		return phylink_mii_ioctl(mac->phylink, ifr, cmd);
2666 	default:
2667 		break;
2668 	}
2669 
2670 	return -EOPNOTSUPP;
2671 }
2672 
mtk_pending_work(struct work_struct * work)2673 static void mtk_pending_work(struct work_struct *work)
2674 {
2675 	struct mtk_eth *eth = container_of(work, struct mtk_eth, pending_work);
2676 	int err, i;
2677 	unsigned long restart = 0;
2678 
2679 	rtnl_lock();
2680 
2681 	dev_dbg(eth->dev, "[%s][%d] reset\n", __func__, __LINE__);
2682 
2683 	while (test_and_set_bit_lock(MTK_RESETTING, &eth->state))
2684 		cpu_relax();
2685 
2686 	dev_dbg(eth->dev, "[%s][%d] mtk_stop starts\n", __func__, __LINE__);
2687 	/* stop all devices to make sure that dma is properly shut down */
2688 	for (i = 0; i < MTK_MAC_COUNT; i++) {
2689 		if (!eth->netdev[i])
2690 			continue;
2691 		mtk_stop(eth->netdev[i]);
2692 		__set_bit(i, &restart);
2693 	}
2694 	dev_dbg(eth->dev, "[%s][%d] mtk_stop ends\n", __func__, __LINE__);
2695 
2696 	/* restart underlying hardware such as power, clock, pin mux
2697 	 * and the connected phy
2698 	 */
2699 	mtk_hw_deinit(eth);
2700 
2701 	if (eth->dev->pins)
2702 		pinctrl_select_state(eth->dev->pins->p,
2703 				     eth->dev->pins->default_state);
2704 	mtk_hw_init(eth);
2705 
2706 	/* restart DMA and enable IRQs */
2707 	for (i = 0; i < MTK_MAC_COUNT; i++) {
2708 		if (!test_bit(i, &restart))
2709 			continue;
2710 		err = mtk_open(eth->netdev[i]);
2711 		if (err) {
2712 			netif_alert(eth, ifup, eth->netdev[i],
2713 			      "Driver up/down cycle failed, closing device.\n");
2714 			dev_close(eth->netdev[i]);
2715 		}
2716 	}
2717 
2718 	dev_dbg(eth->dev, "[%s][%d] reset done\n", __func__, __LINE__);
2719 
2720 	clear_bit_unlock(MTK_RESETTING, &eth->state);
2721 
2722 	rtnl_unlock();
2723 }
2724 
mtk_free_dev(struct mtk_eth * eth)2725 static int mtk_free_dev(struct mtk_eth *eth)
2726 {
2727 	int i;
2728 
2729 	for (i = 0; i < MTK_MAC_COUNT; i++) {
2730 		if (!eth->netdev[i])
2731 			continue;
2732 		free_netdev(eth->netdev[i]);
2733 	}
2734 
2735 	return 0;
2736 }
2737 
mtk_unreg_dev(struct mtk_eth * eth)2738 static int mtk_unreg_dev(struct mtk_eth *eth)
2739 {
2740 	int i;
2741 
2742 	for (i = 0; i < MTK_MAC_COUNT; i++) {
2743 		if (!eth->netdev[i])
2744 			continue;
2745 		unregister_netdev(eth->netdev[i]);
2746 	}
2747 
2748 	return 0;
2749 }
2750 
mtk_cleanup(struct mtk_eth * eth)2751 static int mtk_cleanup(struct mtk_eth *eth)
2752 {
2753 	mtk_unreg_dev(eth);
2754 	mtk_free_dev(eth);
2755 	cancel_work_sync(&eth->pending_work);
2756 
2757 	return 0;
2758 }
2759 
mtk_get_link_ksettings(struct net_device * ndev,struct ethtool_link_ksettings * cmd)2760 static int mtk_get_link_ksettings(struct net_device *ndev,
2761 				  struct ethtool_link_ksettings *cmd)
2762 {
2763 	struct mtk_mac *mac = netdev_priv(ndev);
2764 
2765 	if (unlikely(test_bit(MTK_RESETTING, &mac->hw->state)))
2766 		return -EBUSY;
2767 
2768 	return phylink_ethtool_ksettings_get(mac->phylink, cmd);
2769 }
2770 
mtk_set_link_ksettings(struct net_device * ndev,const struct ethtool_link_ksettings * cmd)2771 static int mtk_set_link_ksettings(struct net_device *ndev,
2772 				  const struct ethtool_link_ksettings *cmd)
2773 {
2774 	struct mtk_mac *mac = netdev_priv(ndev);
2775 
2776 	if (unlikely(test_bit(MTK_RESETTING, &mac->hw->state)))
2777 		return -EBUSY;
2778 
2779 	return phylink_ethtool_ksettings_set(mac->phylink, cmd);
2780 }
2781 
mtk_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * info)2782 static void mtk_get_drvinfo(struct net_device *dev,
2783 			    struct ethtool_drvinfo *info)
2784 {
2785 	struct mtk_mac *mac = netdev_priv(dev);
2786 
2787 	strlcpy(info->driver, mac->hw->dev->driver->name, sizeof(info->driver));
2788 	strlcpy(info->bus_info, dev_name(mac->hw->dev), sizeof(info->bus_info));
2789 	info->n_stats = ARRAY_SIZE(mtk_ethtool_stats);
2790 }
2791 
mtk_get_msglevel(struct net_device * dev)2792 static u32 mtk_get_msglevel(struct net_device *dev)
2793 {
2794 	struct mtk_mac *mac = netdev_priv(dev);
2795 
2796 	return mac->hw->msg_enable;
2797 }
2798 
mtk_set_msglevel(struct net_device * dev,u32 value)2799 static void mtk_set_msglevel(struct net_device *dev, u32 value)
2800 {
2801 	struct mtk_mac *mac = netdev_priv(dev);
2802 
2803 	mac->hw->msg_enable = value;
2804 }
2805 
mtk_nway_reset(struct net_device * dev)2806 static int mtk_nway_reset(struct net_device *dev)
2807 {
2808 	struct mtk_mac *mac = netdev_priv(dev);
2809 
2810 	if (unlikely(test_bit(MTK_RESETTING, &mac->hw->state)))
2811 		return -EBUSY;
2812 
2813 	if (!mac->phylink)
2814 		return -ENOTSUPP;
2815 
2816 	return phylink_ethtool_nway_reset(mac->phylink);
2817 }
2818 
mtk_get_strings(struct net_device * dev,u32 stringset,u8 * data)2819 static void mtk_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2820 {
2821 	int i;
2822 
2823 	switch (stringset) {
2824 	case ETH_SS_STATS:
2825 		for (i = 0; i < ARRAY_SIZE(mtk_ethtool_stats); i++) {
2826 			memcpy(data, mtk_ethtool_stats[i].str, ETH_GSTRING_LEN);
2827 			data += ETH_GSTRING_LEN;
2828 		}
2829 		break;
2830 	}
2831 }
2832 
mtk_get_sset_count(struct net_device * dev,int sset)2833 static int mtk_get_sset_count(struct net_device *dev, int sset)
2834 {
2835 	switch (sset) {
2836 	case ETH_SS_STATS:
2837 		return ARRAY_SIZE(mtk_ethtool_stats);
2838 	default:
2839 		return -EOPNOTSUPP;
2840 	}
2841 }
2842 
mtk_get_ethtool_stats(struct net_device * dev,struct ethtool_stats * stats,u64 * data)2843 static void mtk_get_ethtool_stats(struct net_device *dev,
2844 				  struct ethtool_stats *stats, u64 *data)
2845 {
2846 	struct mtk_mac *mac = netdev_priv(dev);
2847 	struct mtk_hw_stats *hwstats = mac->hw_stats;
2848 	u64 *data_src, *data_dst;
2849 	unsigned int start;
2850 	int i;
2851 
2852 	if (unlikely(test_bit(MTK_RESETTING, &mac->hw->state)))
2853 		return;
2854 
2855 	if (netif_running(dev) && netif_device_present(dev)) {
2856 		if (spin_trylock_bh(&hwstats->stats_lock)) {
2857 			mtk_stats_update_mac(mac);
2858 			spin_unlock_bh(&hwstats->stats_lock);
2859 		}
2860 	}
2861 
2862 	data_src = (u64 *)hwstats;
2863 
2864 	do {
2865 		data_dst = data;
2866 		start = u64_stats_fetch_begin_irq(&hwstats->syncp);
2867 
2868 		for (i = 0; i < ARRAY_SIZE(mtk_ethtool_stats); i++)
2869 			*data_dst++ = *(data_src + mtk_ethtool_stats[i].offset);
2870 	} while (u64_stats_fetch_retry_irq(&hwstats->syncp, start));
2871 }
2872 
mtk_get_rxnfc(struct net_device * dev,struct ethtool_rxnfc * cmd,u32 * rule_locs)2873 static int mtk_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
2874 			 u32 *rule_locs)
2875 {
2876 	int ret = -EOPNOTSUPP;
2877 
2878 	switch (cmd->cmd) {
2879 	case ETHTOOL_GRXRINGS:
2880 		if (dev->hw_features & NETIF_F_LRO) {
2881 			cmd->data = MTK_MAX_RX_RING_NUM;
2882 			ret = 0;
2883 		}
2884 		break;
2885 	case ETHTOOL_GRXCLSRLCNT:
2886 		if (dev->hw_features & NETIF_F_LRO) {
2887 			struct mtk_mac *mac = netdev_priv(dev);
2888 
2889 			cmd->rule_cnt = mac->hwlro_ip_cnt;
2890 			ret = 0;
2891 		}
2892 		break;
2893 	case ETHTOOL_GRXCLSRULE:
2894 		if (dev->hw_features & NETIF_F_LRO)
2895 			ret = mtk_hwlro_get_fdir_entry(dev, cmd);
2896 		break;
2897 	case ETHTOOL_GRXCLSRLALL:
2898 		if (dev->hw_features & NETIF_F_LRO)
2899 			ret = mtk_hwlro_get_fdir_all(dev, cmd,
2900 						     rule_locs);
2901 		break;
2902 	default:
2903 		break;
2904 	}
2905 
2906 	return ret;
2907 }
2908 
mtk_set_rxnfc(struct net_device * dev,struct ethtool_rxnfc * cmd)2909 static int mtk_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
2910 {
2911 	int ret = -EOPNOTSUPP;
2912 
2913 	switch (cmd->cmd) {
2914 	case ETHTOOL_SRXCLSRLINS:
2915 		if (dev->hw_features & NETIF_F_LRO)
2916 			ret = mtk_hwlro_add_ipaddr(dev, cmd);
2917 		break;
2918 	case ETHTOOL_SRXCLSRLDEL:
2919 		if (dev->hw_features & NETIF_F_LRO)
2920 			ret = mtk_hwlro_del_ipaddr(dev, cmd);
2921 		break;
2922 	default:
2923 		break;
2924 	}
2925 
2926 	return ret;
2927 }
2928 
2929 static const struct ethtool_ops mtk_ethtool_ops = {
2930 	.get_link_ksettings	= mtk_get_link_ksettings,
2931 	.set_link_ksettings	= mtk_set_link_ksettings,
2932 	.get_drvinfo		= mtk_get_drvinfo,
2933 	.get_msglevel		= mtk_get_msglevel,
2934 	.set_msglevel		= mtk_set_msglevel,
2935 	.nway_reset		= mtk_nway_reset,
2936 	.get_link		= ethtool_op_get_link,
2937 	.get_strings		= mtk_get_strings,
2938 	.get_sset_count		= mtk_get_sset_count,
2939 	.get_ethtool_stats	= mtk_get_ethtool_stats,
2940 	.get_rxnfc		= mtk_get_rxnfc,
2941 	.set_rxnfc              = mtk_set_rxnfc,
2942 };
2943 
2944 static const struct net_device_ops mtk_netdev_ops = {
2945 	.ndo_uninit		= mtk_uninit,
2946 	.ndo_open		= mtk_open,
2947 	.ndo_stop		= mtk_stop,
2948 	.ndo_start_xmit		= mtk_start_xmit,
2949 	.ndo_set_mac_address	= mtk_set_mac_address,
2950 	.ndo_validate_addr	= eth_validate_addr,
2951 	.ndo_eth_ioctl		= mtk_do_ioctl,
2952 	.ndo_change_mtu		= mtk_change_mtu,
2953 	.ndo_tx_timeout		= mtk_tx_timeout,
2954 	.ndo_get_stats64        = mtk_get_stats64,
2955 	.ndo_fix_features	= mtk_fix_features,
2956 	.ndo_set_features	= mtk_set_features,
2957 #ifdef CONFIG_NET_POLL_CONTROLLER
2958 	.ndo_poll_controller	= mtk_poll_controller,
2959 #endif
2960 	.ndo_setup_tc		= mtk_eth_setup_tc,
2961 };
2962 
mtk_add_mac(struct mtk_eth * eth,struct device_node * np)2963 static int mtk_add_mac(struct mtk_eth *eth, struct device_node *np)
2964 {
2965 	const __be32 *_id = of_get_property(np, "reg", NULL);
2966 	phy_interface_t phy_mode;
2967 	struct phylink *phylink;
2968 	struct mtk_mac *mac;
2969 	int id, err;
2970 
2971 	if (!_id) {
2972 		dev_err(eth->dev, "missing mac id\n");
2973 		return -EINVAL;
2974 	}
2975 
2976 	id = be32_to_cpup(_id);
2977 	if (id >= MTK_MAC_COUNT) {
2978 		dev_err(eth->dev, "%d is not a valid mac id\n", id);
2979 		return -EINVAL;
2980 	}
2981 
2982 	if (eth->netdev[id]) {
2983 		dev_err(eth->dev, "duplicate mac id found: %d\n", id);
2984 		return -EINVAL;
2985 	}
2986 
2987 	eth->netdev[id] = alloc_etherdev(sizeof(*mac));
2988 	if (!eth->netdev[id]) {
2989 		dev_err(eth->dev, "alloc_etherdev failed\n");
2990 		return -ENOMEM;
2991 	}
2992 	mac = netdev_priv(eth->netdev[id]);
2993 	eth->mac[id] = mac;
2994 	mac->id = id;
2995 	mac->hw = eth;
2996 	mac->of_node = np;
2997 
2998 	err = of_get_ethdev_address(mac->of_node, eth->netdev[id]);
2999 	if (err == -EPROBE_DEFER)
3000 		return err;
3001 
3002 	if (err) {
3003 		/* If the mac address is invalid, use random mac address */
3004 		eth_hw_addr_random(eth->netdev[id]);
3005 		dev_err(eth->dev, "generated random MAC address %pM\n",
3006 			eth->netdev[id]->dev_addr);
3007 	}
3008 
3009 	memset(mac->hwlro_ip, 0, sizeof(mac->hwlro_ip));
3010 	mac->hwlro_ip_cnt = 0;
3011 
3012 	mac->hw_stats = devm_kzalloc(eth->dev,
3013 				     sizeof(*mac->hw_stats),
3014 				     GFP_KERNEL);
3015 	if (!mac->hw_stats) {
3016 		dev_err(eth->dev, "failed to allocate counter memory\n");
3017 		err = -ENOMEM;
3018 		goto free_netdev;
3019 	}
3020 	spin_lock_init(&mac->hw_stats->stats_lock);
3021 	u64_stats_init(&mac->hw_stats->syncp);
3022 	mac->hw_stats->reg_offset = id * MTK_STAT_OFFSET;
3023 
3024 	/* phylink create */
3025 	err = of_get_phy_mode(np, &phy_mode);
3026 	if (err) {
3027 		dev_err(eth->dev, "incorrect phy-mode\n");
3028 		goto free_netdev;
3029 	}
3030 
3031 	/* mac config is not set */
3032 	mac->interface = PHY_INTERFACE_MODE_NA;
3033 	mac->mode = MLO_AN_PHY;
3034 	mac->speed = SPEED_UNKNOWN;
3035 
3036 	mac->phylink_config.dev = &eth->netdev[id]->dev;
3037 	mac->phylink_config.type = PHYLINK_NETDEV;
3038 
3039 	phylink = phylink_create(&mac->phylink_config,
3040 				 of_fwnode_handle(mac->of_node),
3041 				 phy_mode, &mtk_phylink_ops);
3042 	if (IS_ERR(phylink)) {
3043 		err = PTR_ERR(phylink);
3044 		goto free_netdev;
3045 	}
3046 
3047 	mac->phylink = phylink;
3048 
3049 	SET_NETDEV_DEV(eth->netdev[id], eth->dev);
3050 	eth->netdev[id]->watchdog_timeo = 5 * HZ;
3051 	eth->netdev[id]->netdev_ops = &mtk_netdev_ops;
3052 	eth->netdev[id]->base_addr = (unsigned long)eth->base;
3053 
3054 	eth->netdev[id]->hw_features = eth->soc->hw_features;
3055 	if (eth->hwlro)
3056 		eth->netdev[id]->hw_features |= NETIF_F_LRO;
3057 
3058 	eth->netdev[id]->vlan_features = eth->soc->hw_features &
3059 		~(NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX);
3060 	eth->netdev[id]->features |= eth->soc->hw_features;
3061 	eth->netdev[id]->ethtool_ops = &mtk_ethtool_ops;
3062 
3063 	eth->netdev[id]->irq = eth->irq[0];
3064 	eth->netdev[id]->dev.of_node = np;
3065 
3066 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_SOC_MT7628))
3067 		eth->netdev[id]->max_mtu = MTK_MAX_RX_LENGTH - MTK_RX_ETH_HLEN;
3068 	else
3069 		eth->netdev[id]->max_mtu = MTK_MAX_RX_LENGTH_2K - MTK_RX_ETH_HLEN;
3070 
3071 	return 0;
3072 
3073 free_netdev:
3074 	free_netdev(eth->netdev[id]);
3075 	return err;
3076 }
3077 
mtk_probe(struct platform_device * pdev)3078 static int mtk_probe(struct platform_device *pdev)
3079 {
3080 	struct device_node *mac_np;
3081 	struct mtk_eth *eth;
3082 	int err, i;
3083 
3084 	eth = devm_kzalloc(&pdev->dev, sizeof(*eth), GFP_KERNEL);
3085 	if (!eth)
3086 		return -ENOMEM;
3087 
3088 	eth->soc = of_device_get_match_data(&pdev->dev);
3089 
3090 	eth->dev = &pdev->dev;
3091 	eth->base = devm_platform_ioremap_resource(pdev, 0);
3092 	if (IS_ERR(eth->base))
3093 		return PTR_ERR(eth->base);
3094 
3095 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA)) {
3096 		eth->tx_int_mask_reg = MTK_QDMA_INT_MASK;
3097 		eth->tx_int_status_reg = MTK_QDMA_INT_STATUS;
3098 	} else {
3099 		eth->tx_int_mask_reg = MTK_PDMA_INT_MASK;
3100 		eth->tx_int_status_reg = MTK_PDMA_INT_STATUS;
3101 	}
3102 
3103 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_SOC_MT7628)) {
3104 		eth->rx_dma_l4_valid = RX_DMA_L4_VALID_PDMA;
3105 		eth->ip_align = NET_IP_ALIGN;
3106 	} else {
3107 		eth->rx_dma_l4_valid = RX_DMA_L4_VALID;
3108 	}
3109 
3110 	spin_lock_init(&eth->page_lock);
3111 	spin_lock_init(&eth->tx_irq_lock);
3112 	spin_lock_init(&eth->rx_irq_lock);
3113 	spin_lock_init(&eth->dim_lock);
3114 
3115 	eth->rx_dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
3116 	INIT_WORK(&eth->rx_dim.work, mtk_dim_rx);
3117 
3118 	eth->tx_dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
3119 	INIT_WORK(&eth->tx_dim.work, mtk_dim_tx);
3120 
3121 	if (!MTK_HAS_CAPS(eth->soc->caps, MTK_SOC_MT7628)) {
3122 		eth->ethsys = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
3123 							      "mediatek,ethsys");
3124 		if (IS_ERR(eth->ethsys)) {
3125 			dev_err(&pdev->dev, "no ethsys regmap found\n");
3126 			return PTR_ERR(eth->ethsys);
3127 		}
3128 	}
3129 
3130 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_INFRA)) {
3131 		eth->infra = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
3132 							     "mediatek,infracfg");
3133 		if (IS_ERR(eth->infra)) {
3134 			dev_err(&pdev->dev, "no infracfg regmap found\n");
3135 			return PTR_ERR(eth->infra);
3136 		}
3137 	}
3138 
3139 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_SGMII)) {
3140 		eth->sgmii = devm_kzalloc(eth->dev, sizeof(*eth->sgmii),
3141 					  GFP_KERNEL);
3142 		if (!eth->sgmii)
3143 			return -ENOMEM;
3144 
3145 		err = mtk_sgmii_init(eth->sgmii, pdev->dev.of_node,
3146 				     eth->soc->ana_rgc3);
3147 
3148 		if (err)
3149 			return err;
3150 	}
3151 
3152 	if (eth->soc->required_pctl) {
3153 		eth->pctl = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
3154 							    "mediatek,pctl");
3155 		if (IS_ERR(eth->pctl)) {
3156 			dev_err(&pdev->dev, "no pctl regmap found\n");
3157 			return PTR_ERR(eth->pctl);
3158 		}
3159 	}
3160 
3161 	for (i = 0; i < 3; i++) {
3162 		if (MTK_HAS_CAPS(eth->soc->caps, MTK_SHARED_INT) && i > 0)
3163 			eth->irq[i] = eth->irq[0];
3164 		else
3165 			eth->irq[i] = platform_get_irq(pdev, i);
3166 		if (eth->irq[i] < 0) {
3167 			dev_err(&pdev->dev, "no IRQ%d resource found\n", i);
3168 			return -ENXIO;
3169 		}
3170 	}
3171 	for (i = 0; i < ARRAY_SIZE(eth->clks); i++) {
3172 		eth->clks[i] = devm_clk_get(eth->dev,
3173 					    mtk_clks_source_name[i]);
3174 		if (IS_ERR(eth->clks[i])) {
3175 			if (PTR_ERR(eth->clks[i]) == -EPROBE_DEFER)
3176 				return -EPROBE_DEFER;
3177 			if (eth->soc->required_clks & BIT(i)) {
3178 				dev_err(&pdev->dev, "clock %s not found\n",
3179 					mtk_clks_source_name[i]);
3180 				return -EINVAL;
3181 			}
3182 			eth->clks[i] = NULL;
3183 		}
3184 	}
3185 
3186 	eth->msg_enable = netif_msg_init(mtk_msg_level, MTK_DEFAULT_MSG_ENABLE);
3187 	INIT_WORK(&eth->pending_work, mtk_pending_work);
3188 
3189 	err = mtk_hw_init(eth);
3190 	if (err)
3191 		return err;
3192 
3193 	eth->hwlro = MTK_HAS_CAPS(eth->soc->caps, MTK_HWLRO);
3194 
3195 	for_each_child_of_node(pdev->dev.of_node, mac_np) {
3196 		if (!of_device_is_compatible(mac_np,
3197 					     "mediatek,eth-mac"))
3198 			continue;
3199 
3200 		if (!of_device_is_available(mac_np))
3201 			continue;
3202 
3203 		err = mtk_add_mac(eth, mac_np);
3204 		if (err) {
3205 			of_node_put(mac_np);
3206 			goto err_deinit_hw;
3207 		}
3208 	}
3209 
3210 	if (MTK_HAS_CAPS(eth->soc->caps, MTK_SHARED_INT)) {
3211 		err = devm_request_irq(eth->dev, eth->irq[0],
3212 				       mtk_handle_irq, 0,
3213 				       dev_name(eth->dev), eth);
3214 	} else {
3215 		err = devm_request_irq(eth->dev, eth->irq[1],
3216 				       mtk_handle_irq_tx, 0,
3217 				       dev_name(eth->dev), eth);
3218 		if (err)
3219 			goto err_free_dev;
3220 
3221 		err = devm_request_irq(eth->dev, eth->irq[2],
3222 				       mtk_handle_irq_rx, 0,
3223 				       dev_name(eth->dev), eth);
3224 	}
3225 	if (err)
3226 		goto err_free_dev;
3227 
3228 	/* No MT7628/88 support yet */
3229 	if (!MTK_HAS_CAPS(eth->soc->caps, MTK_SOC_MT7628)) {
3230 		err = mtk_mdio_init(eth);
3231 		if (err)
3232 			goto err_free_dev;
3233 	}
3234 
3235 	if (eth->soc->offload_version) {
3236 		err = mtk_ppe_init(&eth->ppe, eth->dev,
3237 				   eth->base + MTK_ETH_PPE_BASE, 2);
3238 		if (err)
3239 			goto err_free_dev;
3240 
3241 		err = mtk_eth_offload_init(eth);
3242 		if (err)
3243 			goto err_free_dev;
3244 	}
3245 
3246 	for (i = 0; i < MTK_MAX_DEVS; i++) {
3247 		if (!eth->netdev[i])
3248 			continue;
3249 
3250 		err = register_netdev(eth->netdev[i]);
3251 		if (err) {
3252 			dev_err(eth->dev, "error bringing up device\n");
3253 			goto err_deinit_mdio;
3254 		} else
3255 			netif_info(eth, probe, eth->netdev[i],
3256 				   "mediatek frame engine at 0x%08lx, irq %d\n",
3257 				   eth->netdev[i]->base_addr, eth->irq[0]);
3258 	}
3259 
3260 	/* we run 2 devices on the same DMA ring so we need a dummy device
3261 	 * for NAPI to work
3262 	 */
3263 	init_dummy_netdev(&eth->dummy_dev);
3264 	netif_napi_add(&eth->dummy_dev, &eth->tx_napi, mtk_napi_tx,
3265 		       MTK_NAPI_WEIGHT);
3266 	netif_napi_add(&eth->dummy_dev, &eth->rx_napi, mtk_napi_rx,
3267 		       MTK_NAPI_WEIGHT);
3268 
3269 	platform_set_drvdata(pdev, eth);
3270 
3271 	return 0;
3272 
3273 err_deinit_mdio:
3274 	mtk_mdio_cleanup(eth);
3275 err_free_dev:
3276 	mtk_free_dev(eth);
3277 err_deinit_hw:
3278 	mtk_hw_deinit(eth);
3279 
3280 	return err;
3281 }
3282 
mtk_remove(struct platform_device * pdev)3283 static int mtk_remove(struct platform_device *pdev)
3284 {
3285 	struct mtk_eth *eth = platform_get_drvdata(pdev);
3286 	struct mtk_mac *mac;
3287 	int i;
3288 
3289 	/* stop all devices to make sure that dma is properly shut down */
3290 	for (i = 0; i < MTK_MAC_COUNT; i++) {
3291 		if (!eth->netdev[i])
3292 			continue;
3293 		mtk_stop(eth->netdev[i]);
3294 		mac = netdev_priv(eth->netdev[i]);
3295 		phylink_disconnect_phy(mac->phylink);
3296 	}
3297 
3298 	mtk_hw_deinit(eth);
3299 
3300 	netif_napi_del(&eth->tx_napi);
3301 	netif_napi_del(&eth->rx_napi);
3302 	mtk_cleanup(eth);
3303 	mtk_mdio_cleanup(eth);
3304 
3305 	return 0;
3306 }
3307 
3308 static const struct mtk_soc_data mt2701_data = {
3309 	.caps = MT7623_CAPS | MTK_HWLRO,
3310 	.hw_features = MTK_HW_FEATURES,
3311 	.required_clks = MT7623_CLKS_BITMAP,
3312 	.required_pctl = true,
3313 };
3314 
3315 static const struct mtk_soc_data mt7621_data = {
3316 	.caps = MT7621_CAPS,
3317 	.hw_features = MTK_HW_FEATURES,
3318 	.required_clks = MT7621_CLKS_BITMAP,
3319 	.required_pctl = false,
3320 	.offload_version = 2,
3321 };
3322 
3323 static const struct mtk_soc_data mt7622_data = {
3324 	.ana_rgc3 = 0x2028,
3325 	.caps = MT7622_CAPS | MTK_HWLRO,
3326 	.hw_features = MTK_HW_FEATURES,
3327 	.required_clks = MT7622_CLKS_BITMAP,
3328 	.required_pctl = false,
3329 	.offload_version = 2,
3330 };
3331 
3332 static const struct mtk_soc_data mt7623_data = {
3333 	.caps = MT7623_CAPS | MTK_HWLRO,
3334 	.hw_features = MTK_HW_FEATURES,
3335 	.required_clks = MT7623_CLKS_BITMAP,
3336 	.required_pctl = true,
3337 	.offload_version = 2,
3338 };
3339 
3340 static const struct mtk_soc_data mt7629_data = {
3341 	.ana_rgc3 = 0x128,
3342 	.caps = MT7629_CAPS | MTK_HWLRO,
3343 	.hw_features = MTK_HW_FEATURES,
3344 	.required_clks = MT7629_CLKS_BITMAP,
3345 	.required_pctl = false,
3346 };
3347 
3348 static const struct mtk_soc_data rt5350_data = {
3349 	.caps = MT7628_CAPS,
3350 	.hw_features = MTK_HW_FEATURES_MT7628,
3351 	.required_clks = MT7628_CLKS_BITMAP,
3352 	.required_pctl = false,
3353 };
3354 
3355 const struct of_device_id of_mtk_match[] = {
3356 	{ .compatible = "mediatek,mt2701-eth", .data = &mt2701_data},
3357 	{ .compatible = "mediatek,mt7621-eth", .data = &mt7621_data},
3358 	{ .compatible = "mediatek,mt7622-eth", .data = &mt7622_data},
3359 	{ .compatible = "mediatek,mt7623-eth", .data = &mt7623_data},
3360 	{ .compatible = "mediatek,mt7629-eth", .data = &mt7629_data},
3361 	{ .compatible = "ralink,rt5350-eth", .data = &rt5350_data},
3362 	{},
3363 };
3364 MODULE_DEVICE_TABLE(of, of_mtk_match);
3365 
3366 static struct platform_driver mtk_driver = {
3367 	.probe = mtk_probe,
3368 	.remove = mtk_remove,
3369 	.driver = {
3370 		.name = "mtk_soc_eth",
3371 		.of_match_table = of_mtk_match,
3372 	},
3373 };
3374 
3375 module_platform_driver(mtk_driver);
3376 
3377 MODULE_LICENSE("GPL");
3378 MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
3379 MODULE_DESCRIPTION("Ethernet driver for MediaTek SoC");
3380