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