developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * |
| 4 | * Copyright (C) 2009-2016 John Crispin <blogic@openwrt.org> |
| 5 | * Copyright (C) 2009-2016 Felix Fietkau <nbd@openwrt.org> |
| 6 | * Copyright (C) 2013-2016 Michael Lee <igvtee@gmail.com> |
| 7 | */ |
| 8 | |
| 9 | #include <linux/of_device.h> |
| 10 | #include <linux/of_mdio.h> |
| 11 | #include <linux/of_net.h> |
| 12 | #include <linux/mfd/syscon.h> |
| 13 | #include <linux/regmap.h> |
| 14 | #include <linux/clk.h> |
| 15 | #include <linux/pm_runtime.h> |
| 16 | #include <linux/if_vlan.h> |
| 17 | #include <linux/reset.h> |
| 18 | #include <linux/tcp.h> |
| 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/pinctrl/devinfo.h> |
| 21 | #include <linux/phylink.h> |
| 22 | #include <net/dsa.h> |
| 23 | |
| 24 | #include "mtk_eth_soc.h" |
| 25 | #include "mtk_eth_dbg.h" |
| 26 | |
| 27 | #if defined(CONFIG_NET_MEDIATEK_HNAT) || defined(CONFIG_NET_MEDIATEK_HNAT_MODULE) |
| 28 | #include "mtk_hnat/nf_hnat_mtk.h" |
| 29 | #endif |
| 30 | |
| 31 | static int mtk_msg_level = -1; |
| 32 | module_param_named(msg_level, mtk_msg_level, int, 0); |
| 33 | MODULE_PARM_DESC(msg_level, "Message level (-1=defaults,0=none,...,16=all)"); |
| 34 | |
| 35 | #define MTK_ETHTOOL_STAT(x) { #x, \ |
| 36 | offsetof(struct mtk_hw_stats, x) / sizeof(u64) } |
| 37 | |
| 38 | /* strings used by ethtool */ |
| 39 | static const struct mtk_ethtool_stats { |
| 40 | char str[ETH_GSTRING_LEN]; |
| 41 | u32 offset; |
| 42 | } mtk_ethtool_stats[] = { |
| 43 | MTK_ETHTOOL_STAT(tx_bytes), |
| 44 | MTK_ETHTOOL_STAT(tx_packets), |
| 45 | MTK_ETHTOOL_STAT(tx_skip), |
| 46 | MTK_ETHTOOL_STAT(tx_collisions), |
| 47 | MTK_ETHTOOL_STAT(rx_bytes), |
| 48 | MTK_ETHTOOL_STAT(rx_packets), |
| 49 | MTK_ETHTOOL_STAT(rx_overflow), |
| 50 | MTK_ETHTOOL_STAT(rx_fcs_errors), |
| 51 | MTK_ETHTOOL_STAT(rx_short_errors), |
| 52 | MTK_ETHTOOL_STAT(rx_long_errors), |
| 53 | MTK_ETHTOOL_STAT(rx_checksum_errors), |
| 54 | MTK_ETHTOOL_STAT(rx_flow_control_packets), |
| 55 | }; |
| 56 | |
| 57 | static const char * const mtk_clks_source_name[] = { |
| 58 | "ethif", "sgmiitop", "esw", "gp0", "gp1", "gp2", "fe", "trgpll", |
| 59 | "sgmii_tx250m", "sgmii_rx250m", "sgmii_cdr_ref", "sgmii_cdr_fb", |
| 60 | "sgmii2_tx250m", "sgmii2_rx250m", "sgmii2_cdr_ref", "sgmii2_cdr_fb", |
| 61 | "sgmii_ck", "eth2pll", "wocpu0","wocpu1", |
| 62 | }; |
| 63 | |
| 64 | void mtk_w32(struct mtk_eth *eth, u32 val, unsigned reg) |
| 65 | { |
| 66 | __raw_writel(val, eth->base + reg); |
| 67 | } |
| 68 | |
| 69 | u32 mtk_r32(struct mtk_eth *eth, unsigned reg) |
| 70 | { |
| 71 | return __raw_readl(eth->base + reg); |
| 72 | } |
| 73 | |
| 74 | u32 mtk_m32(struct mtk_eth *eth, u32 mask, u32 set, unsigned reg) |
| 75 | { |
| 76 | u32 val; |
| 77 | |
| 78 | val = mtk_r32(eth, reg); |
| 79 | val &= ~mask; |
| 80 | val |= set; |
| 81 | mtk_w32(eth, val, reg); |
| 82 | return reg; |
| 83 | } |
| 84 | |
| 85 | static int mtk_mdio_busy_wait(struct mtk_eth *eth) |
| 86 | { |
| 87 | unsigned long t_start = jiffies; |
| 88 | |
| 89 | while (1) { |
| 90 | if (!(mtk_r32(eth, MTK_PHY_IAC) & PHY_IAC_ACCESS)) |
| 91 | return 0; |
| 92 | if (time_after(jiffies, t_start + PHY_IAC_TIMEOUT)) |
| 93 | break; |
developer | c4671b2 | 2021-05-28 13:16:42 +0800 | [diff] [blame] | 94 | cond_resched(); |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | dev_err(eth->dev, "mdio: MDIO timeout\n"); |
| 98 | return -1; |
| 99 | } |
| 100 | |
developer | 3957a91 | 2021-05-13 16:44:31 +0800 | [diff] [blame] | 101 | u32 _mtk_mdio_write(struct mtk_eth *eth, u16 phy_addr, |
| 102 | u16 phy_register, u16 write_data) |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 103 | { |
| 104 | if (mtk_mdio_busy_wait(eth)) |
| 105 | return -1; |
| 106 | |
| 107 | write_data &= 0xffff; |
| 108 | |
| 109 | mtk_w32(eth, PHY_IAC_ACCESS | PHY_IAC_START | PHY_IAC_WRITE | |
developer | fb556ca | 2021-10-13 10:52:09 +0800 | [diff] [blame^] | 110 | ((phy_register & 0x1f) << PHY_IAC_REG_SHIFT) | |
| 111 | ((phy_addr & 0x1f) << PHY_IAC_ADDR_SHIFT) | write_data, |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 112 | MTK_PHY_IAC); |
| 113 | |
| 114 | if (mtk_mdio_busy_wait(eth)) |
| 115 | return -1; |
| 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
developer | 3957a91 | 2021-05-13 16:44:31 +0800 | [diff] [blame] | 120 | u32 _mtk_mdio_read(struct mtk_eth *eth, u16 phy_addr, u16 phy_reg) |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 121 | { |
| 122 | u32 d; |
| 123 | |
| 124 | if (mtk_mdio_busy_wait(eth)) |
| 125 | return 0xffff; |
| 126 | |
| 127 | mtk_w32(eth, PHY_IAC_ACCESS | PHY_IAC_START | PHY_IAC_READ | |
developer | fb556ca | 2021-10-13 10:52:09 +0800 | [diff] [blame^] | 128 | ((phy_reg & 0x1f) << PHY_IAC_REG_SHIFT) | |
| 129 | ((phy_addr & 0x1f) << PHY_IAC_ADDR_SHIFT), |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 130 | MTK_PHY_IAC); |
| 131 | |
| 132 | if (mtk_mdio_busy_wait(eth)) |
| 133 | return 0xffff; |
| 134 | |
| 135 | d = mtk_r32(eth, MTK_PHY_IAC) & 0xffff; |
| 136 | |
| 137 | return d; |
| 138 | } |
| 139 | |
| 140 | static int mtk_mdio_write(struct mii_bus *bus, int phy_addr, |
| 141 | int phy_reg, u16 val) |
| 142 | { |
| 143 | struct mtk_eth *eth = bus->priv; |
| 144 | |
| 145 | return _mtk_mdio_write(eth, phy_addr, phy_reg, val); |
| 146 | } |
| 147 | |
| 148 | static int mtk_mdio_read(struct mii_bus *bus, int phy_addr, int phy_reg) |
| 149 | { |
| 150 | struct mtk_eth *eth = bus->priv; |
| 151 | |
| 152 | return _mtk_mdio_read(eth, phy_addr, phy_reg); |
| 153 | } |
| 154 | |
developer | 3957a91 | 2021-05-13 16:44:31 +0800 | [diff] [blame] | 155 | u32 mtk_cl45_ind_read(struct mtk_eth *eth, u16 port, u16 devad, u16 reg, u16 *data) |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 156 | { |
| 157 | mutex_lock(ð->mii_bus->mdio_lock); |
| 158 | |
| 159 | _mtk_mdio_write(eth, port, MII_MMD_ACC_CTL_REG, devad); |
| 160 | _mtk_mdio_write(eth, port, MII_MMD_ADDR_DATA_REG, reg); |
| 161 | _mtk_mdio_write(eth, port, MII_MMD_ACC_CTL_REG, MMD_OP_MODE_DATA | devad); |
| 162 | *data = _mtk_mdio_read(eth, port, MII_MMD_ADDR_DATA_REG); |
| 163 | |
| 164 | mutex_unlock(ð->mii_bus->mdio_lock); |
| 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |
developer | 3957a91 | 2021-05-13 16:44:31 +0800 | [diff] [blame] | 169 | u32 mtk_cl45_ind_write(struct mtk_eth *eth, u16 port, u16 devad, u16 reg, u16 data) |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 170 | { |
| 171 | mutex_lock(ð->mii_bus->mdio_lock); |
| 172 | |
| 173 | _mtk_mdio_write(eth, port, MII_MMD_ACC_CTL_REG, devad); |
| 174 | _mtk_mdio_write(eth, port, MII_MMD_ADDR_DATA_REG, reg); |
| 175 | _mtk_mdio_write(eth, port, MII_MMD_ACC_CTL_REG, MMD_OP_MODE_DATA | devad); |
| 176 | _mtk_mdio_write(eth, port, MII_MMD_ADDR_DATA_REG, data); |
| 177 | |
| 178 | mutex_unlock(ð->mii_bus->mdio_lock); |
| 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | static int mt7621_gmac0_rgmii_adjust(struct mtk_eth *eth, |
| 184 | phy_interface_t interface) |
| 185 | { |
| 186 | u32 val; |
| 187 | |
| 188 | /* Check DDR memory type. |
| 189 | * Currently TRGMII mode with DDR2 memory is not supported. |
| 190 | */ |
| 191 | regmap_read(eth->ethsys, ETHSYS_SYSCFG, &val); |
| 192 | if (interface == PHY_INTERFACE_MODE_TRGMII && |
| 193 | val & SYSCFG_DRAM_TYPE_DDR2) { |
| 194 | dev_err(eth->dev, |
| 195 | "TRGMII mode with DDR2 memory is not supported!\n"); |
| 196 | return -EOPNOTSUPP; |
| 197 | } |
| 198 | |
| 199 | val = (interface == PHY_INTERFACE_MODE_TRGMII) ? |
| 200 | ETHSYS_TRGMII_MT7621_DDR_PLL : 0; |
| 201 | |
| 202 | regmap_update_bits(eth->ethsys, ETHSYS_CLKCFG0, |
| 203 | ETHSYS_TRGMII_MT7621_MASK, val); |
| 204 | |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | static void mtk_gmac0_rgmii_adjust(struct mtk_eth *eth, |
| 209 | phy_interface_t interface, int speed) |
| 210 | { |
| 211 | u32 val; |
| 212 | int ret; |
| 213 | |
| 214 | if (interface == PHY_INTERFACE_MODE_TRGMII) { |
| 215 | mtk_w32(eth, TRGMII_MODE, INTF_MODE); |
| 216 | val = 500000000; |
| 217 | ret = clk_set_rate(eth->clks[MTK_CLK_TRGPLL], val); |
| 218 | if (ret) |
| 219 | dev_err(eth->dev, "Failed to set trgmii pll: %d\n", ret); |
| 220 | return; |
| 221 | } |
| 222 | |
| 223 | val = (speed == SPEED_1000) ? |
| 224 | INTF_MODE_RGMII_1000 : INTF_MODE_RGMII_10_100; |
| 225 | mtk_w32(eth, val, INTF_MODE); |
| 226 | |
| 227 | regmap_update_bits(eth->ethsys, ETHSYS_CLKCFG0, |
| 228 | ETHSYS_TRGMII_CLK_SEL362_5, |
| 229 | ETHSYS_TRGMII_CLK_SEL362_5); |
| 230 | |
| 231 | val = (speed == SPEED_1000) ? 250000000 : 500000000; |
| 232 | ret = clk_set_rate(eth->clks[MTK_CLK_TRGPLL], val); |
| 233 | if (ret) |
| 234 | dev_err(eth->dev, "Failed to set trgmii pll: %d\n", ret); |
| 235 | |
| 236 | val = (speed == SPEED_1000) ? |
| 237 | RCK_CTRL_RGMII_1000 : RCK_CTRL_RGMII_10_100; |
| 238 | mtk_w32(eth, val, TRGMII_RCK_CTRL); |
| 239 | |
| 240 | val = (speed == SPEED_1000) ? |
| 241 | TCK_CTRL_RGMII_1000 : TCK_CTRL_RGMII_10_100; |
| 242 | mtk_w32(eth, val, TRGMII_TCK_CTRL); |
| 243 | } |
| 244 | |
| 245 | static void mtk_mac_config(struct phylink_config *config, unsigned int mode, |
| 246 | const struct phylink_link_state *state) |
| 247 | { |
| 248 | struct mtk_mac *mac = container_of(config, struct mtk_mac, |
| 249 | phylink_config); |
| 250 | struct mtk_eth *eth = mac->hw; |
| 251 | u32 mcr_cur, mcr_new, sid, i; |
developer | fb556ca | 2021-10-13 10:52:09 +0800 | [diff] [blame^] | 252 | int val, ge_mode, err=0; |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 253 | |
| 254 | /* MT76x8 has no hardware settings between for the MAC */ |
| 255 | if (!MTK_HAS_CAPS(eth->soc->caps, MTK_SOC_MT7628) && |
| 256 | mac->interface != state->interface) { |
| 257 | /* Setup soc pin functions */ |
| 258 | switch (state->interface) { |
| 259 | case PHY_INTERFACE_MODE_TRGMII: |
| 260 | if (mac->id) |
| 261 | goto err_phy; |
| 262 | if (!MTK_HAS_CAPS(mac->hw->soc->caps, |
| 263 | MTK_GMAC1_TRGMII)) |
| 264 | goto err_phy; |
| 265 | /* fall through */ |
| 266 | case PHY_INTERFACE_MODE_RGMII_TXID: |
| 267 | case PHY_INTERFACE_MODE_RGMII_RXID: |
| 268 | case PHY_INTERFACE_MODE_RGMII_ID: |
| 269 | case PHY_INTERFACE_MODE_RGMII: |
| 270 | case PHY_INTERFACE_MODE_MII: |
| 271 | case PHY_INTERFACE_MODE_REVMII: |
| 272 | case PHY_INTERFACE_MODE_RMII: |
| 273 | if (MTK_HAS_CAPS(eth->soc->caps, MTK_RGMII)) { |
| 274 | err = mtk_gmac_rgmii_path_setup(eth, mac->id); |
| 275 | if (err) |
| 276 | goto init_err; |
| 277 | } |
| 278 | break; |
| 279 | case PHY_INTERFACE_MODE_1000BASEX: |
| 280 | case PHY_INTERFACE_MODE_2500BASEX: |
| 281 | case PHY_INTERFACE_MODE_SGMII: |
| 282 | if (MTK_HAS_CAPS(eth->soc->caps, MTK_SGMII)) { |
| 283 | err = mtk_gmac_sgmii_path_setup(eth, mac->id); |
| 284 | if (err) |
| 285 | goto init_err; |
| 286 | } |
| 287 | break; |
| 288 | case PHY_INTERFACE_MODE_GMII: |
| 289 | if (MTK_HAS_CAPS(eth->soc->caps, MTK_GEPHY)) { |
| 290 | err = mtk_gmac_gephy_path_setup(eth, mac->id); |
| 291 | if (err) |
| 292 | goto init_err; |
| 293 | } |
| 294 | break; |
| 295 | default: |
| 296 | goto err_phy; |
| 297 | } |
| 298 | |
| 299 | /* Setup clock for 1st gmac */ |
| 300 | if (!mac->id && state->interface != PHY_INTERFACE_MODE_SGMII && |
| 301 | !phy_interface_mode_is_8023z(state->interface) && |
| 302 | MTK_HAS_CAPS(mac->hw->soc->caps, MTK_GMAC1_TRGMII)) { |
| 303 | if (MTK_HAS_CAPS(mac->hw->soc->caps, |
| 304 | MTK_TRGMII_MT7621_CLK)) { |
| 305 | if (mt7621_gmac0_rgmii_adjust(mac->hw, |
| 306 | state->interface)) |
| 307 | goto err_phy; |
| 308 | } else { |
| 309 | mtk_gmac0_rgmii_adjust(mac->hw, |
| 310 | state->interface, |
| 311 | state->speed); |
| 312 | |
| 313 | /* mt7623_pad_clk_setup */ |
| 314 | for (i = 0 ; i < NUM_TRGMII_CTRL; i++) |
| 315 | mtk_w32(mac->hw, |
| 316 | TD_DM_DRVP(8) | TD_DM_DRVN(8), |
| 317 | TRGMII_TD_ODT(i)); |
| 318 | |
| 319 | /* Assert/release MT7623 RXC reset */ |
| 320 | mtk_m32(mac->hw, 0, RXC_RST | RXC_DQSISEL, |
| 321 | TRGMII_RCK_CTRL); |
| 322 | mtk_m32(mac->hw, RXC_RST, 0, TRGMII_RCK_CTRL); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | ge_mode = 0; |
| 327 | switch (state->interface) { |
| 328 | case PHY_INTERFACE_MODE_MII: |
| 329 | case PHY_INTERFACE_MODE_GMII: |
| 330 | ge_mode = 1; |
| 331 | break; |
| 332 | case PHY_INTERFACE_MODE_REVMII: |
| 333 | ge_mode = 2; |
| 334 | break; |
| 335 | case PHY_INTERFACE_MODE_RMII: |
| 336 | if (mac->id) |
| 337 | goto err_phy; |
| 338 | ge_mode = 3; |
| 339 | break; |
| 340 | default: |
| 341 | break; |
| 342 | } |
| 343 | |
| 344 | /* put the gmac into the right mode */ |
| 345 | regmap_read(eth->ethsys, ETHSYS_SYSCFG0, &val); |
| 346 | val &= ~SYSCFG0_GE_MODE(SYSCFG0_GE_MASK, mac->id); |
| 347 | val |= SYSCFG0_GE_MODE(ge_mode, mac->id); |
| 348 | regmap_write(eth->ethsys, ETHSYS_SYSCFG0, val); |
| 349 | |
| 350 | mac->interface = state->interface; |
| 351 | } |
| 352 | |
| 353 | /* SGMII */ |
| 354 | if (state->interface == PHY_INTERFACE_MODE_SGMII || |
| 355 | phy_interface_mode_is_8023z(state->interface)) { |
| 356 | /* The path GMAC to SGMII will be enabled once the SGMIISYS is |
| 357 | * being setup done. |
| 358 | */ |
| 359 | regmap_read(eth->ethsys, ETHSYS_SYSCFG0, &val); |
| 360 | |
| 361 | regmap_update_bits(eth->ethsys, ETHSYS_SYSCFG0, |
| 362 | SYSCFG0_SGMII_MASK, |
| 363 | ~(u32)SYSCFG0_SGMII_MASK); |
| 364 | |
| 365 | /* Decide how GMAC and SGMIISYS be mapped */ |
| 366 | sid = (MTK_HAS_CAPS(eth->soc->caps, MTK_SHARED_SGMII)) ? |
| 367 | 0 : mac->id; |
| 368 | |
| 369 | /* Setup SGMIISYS with the determined property */ |
| 370 | if (state->interface != PHY_INTERFACE_MODE_SGMII) |
| 371 | err = mtk_sgmii_setup_mode_force(eth->sgmii, sid, |
| 372 | state); |
| 373 | else if (phylink_autoneg_inband(mode)) |
| 374 | err = mtk_sgmii_setup_mode_an(eth->sgmii, sid); |
| 375 | |
| 376 | if (err) |
| 377 | goto init_err; |
| 378 | |
| 379 | regmap_update_bits(eth->ethsys, ETHSYS_SYSCFG0, |
| 380 | SYSCFG0_SGMII_MASK, val); |
| 381 | } else if (phylink_autoneg_inband(mode)) { |
| 382 | dev_err(eth->dev, |
| 383 | "In-band mode not supported in non SGMII mode!\n"); |
| 384 | return; |
| 385 | } |
| 386 | |
| 387 | /* Setup gmac */ |
| 388 | mcr_cur = mtk_r32(mac->hw, MTK_MAC_MCR(mac->id)); |
| 389 | mcr_new = mcr_cur; |
| 390 | mcr_new &= ~(MAC_MCR_SPEED_100 | MAC_MCR_SPEED_1000 | |
| 391 | MAC_MCR_FORCE_DPX | MAC_MCR_FORCE_TX_FC | |
| 392 | MAC_MCR_FORCE_RX_FC); |
| 393 | mcr_new |= MAC_MCR_MAX_RX_1536 | MAC_MCR_IPG_CFG | MAC_MCR_FORCE_MODE | |
| 394 | MAC_MCR_BACKOFF_EN | MAC_MCR_BACKPR_EN | MAC_MCR_FORCE_LINK; |
| 395 | |
| 396 | switch (state->speed) { |
| 397 | case SPEED_2500: |
| 398 | case SPEED_1000: |
| 399 | mcr_new |= MAC_MCR_SPEED_1000; |
| 400 | break; |
| 401 | case SPEED_100: |
| 402 | mcr_new |= MAC_MCR_SPEED_100; |
| 403 | break; |
| 404 | } |
| 405 | if (state->duplex == DUPLEX_FULL) { |
| 406 | mcr_new |= MAC_MCR_FORCE_DPX; |
| 407 | if (state->pause & MLO_PAUSE_TX) |
| 408 | mcr_new |= MAC_MCR_FORCE_TX_FC; |
| 409 | if (state->pause & MLO_PAUSE_RX) |
| 410 | mcr_new |= MAC_MCR_FORCE_RX_FC; |
| 411 | } |
| 412 | |
| 413 | /* Only update control register when needed! */ |
| 414 | if (mcr_new != mcr_cur) |
| 415 | mtk_w32(mac->hw, mcr_new, MTK_MAC_MCR(mac->id)); |
| 416 | |
| 417 | return; |
| 418 | |
| 419 | err_phy: |
| 420 | dev_err(eth->dev, "%s: GMAC%d mode %s not supported!\n", __func__, |
| 421 | mac->id, phy_modes(state->interface)); |
| 422 | return; |
| 423 | |
| 424 | init_err: |
| 425 | dev_err(eth->dev, "%s: GMAC%d mode %s err: %d!\n", __func__, |
| 426 | mac->id, phy_modes(state->interface), err); |
| 427 | } |
| 428 | |
| 429 | static int mtk_mac_link_state(struct phylink_config *config, |
| 430 | struct phylink_link_state *state) |
| 431 | { |
| 432 | struct mtk_mac *mac = container_of(config, struct mtk_mac, |
| 433 | phylink_config); |
| 434 | u32 pmsr = mtk_r32(mac->hw, MTK_MAC_MSR(mac->id)); |
| 435 | |
| 436 | state->link = (pmsr & MAC_MSR_LINK); |
| 437 | state->duplex = (pmsr & MAC_MSR_DPX) >> 1; |
| 438 | |
| 439 | switch (pmsr & (MAC_MSR_SPEED_1000 | MAC_MSR_SPEED_100)) { |
| 440 | case 0: |
| 441 | state->speed = SPEED_10; |
| 442 | break; |
| 443 | case MAC_MSR_SPEED_100: |
| 444 | state->speed = SPEED_100; |
| 445 | break; |
| 446 | case MAC_MSR_SPEED_1000: |
| 447 | state->speed = SPEED_1000; |
| 448 | break; |
| 449 | default: |
| 450 | state->speed = SPEED_UNKNOWN; |
| 451 | break; |
| 452 | } |
| 453 | |
| 454 | state->pause &= (MLO_PAUSE_RX | MLO_PAUSE_TX); |
| 455 | if (pmsr & MAC_MSR_RX_FC) |
| 456 | state->pause |= MLO_PAUSE_RX; |
| 457 | if (pmsr & MAC_MSR_TX_FC) |
| 458 | state->pause |= MLO_PAUSE_TX; |
| 459 | |
| 460 | return 1; |
| 461 | } |
| 462 | |
| 463 | static void mtk_mac_an_restart(struct phylink_config *config) |
| 464 | { |
| 465 | struct mtk_mac *mac = container_of(config, struct mtk_mac, |
| 466 | phylink_config); |
| 467 | |
| 468 | mtk_sgmii_restart_an(mac->hw, mac->id); |
| 469 | } |
| 470 | |
| 471 | static void mtk_mac_link_down(struct phylink_config *config, unsigned int mode, |
| 472 | phy_interface_t interface) |
| 473 | { |
| 474 | struct mtk_mac *mac = container_of(config, struct mtk_mac, |
| 475 | phylink_config); |
| 476 | u32 mcr = mtk_r32(mac->hw, MTK_MAC_MCR(mac->id)); |
| 477 | |
| 478 | mcr &= ~(MAC_MCR_TX_EN | MAC_MCR_RX_EN); |
| 479 | mtk_w32(mac->hw, mcr, MTK_MAC_MCR(mac->id)); |
| 480 | } |
| 481 | |
| 482 | static void mtk_mac_link_up(struct phylink_config *config, unsigned int mode, |
| 483 | phy_interface_t interface, |
| 484 | struct phy_device *phy) |
| 485 | { |
| 486 | struct mtk_mac *mac = container_of(config, struct mtk_mac, |
| 487 | phylink_config); |
| 488 | u32 mcr = mtk_r32(mac->hw, MTK_MAC_MCR(mac->id)); |
| 489 | |
| 490 | mcr |= MAC_MCR_TX_EN | MAC_MCR_RX_EN; |
| 491 | mtk_w32(mac->hw, mcr, MTK_MAC_MCR(mac->id)); |
| 492 | } |
| 493 | |
| 494 | static void mtk_validate(struct phylink_config *config, |
| 495 | unsigned long *supported, |
| 496 | struct phylink_link_state *state) |
| 497 | { |
| 498 | struct mtk_mac *mac = container_of(config, struct mtk_mac, |
| 499 | phylink_config); |
| 500 | __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, }; |
| 501 | |
| 502 | if (state->interface != PHY_INTERFACE_MODE_NA && |
| 503 | state->interface != PHY_INTERFACE_MODE_MII && |
| 504 | state->interface != PHY_INTERFACE_MODE_GMII && |
| 505 | !(MTK_HAS_CAPS(mac->hw->soc->caps, MTK_RGMII) && |
| 506 | phy_interface_mode_is_rgmii(state->interface)) && |
| 507 | !(MTK_HAS_CAPS(mac->hw->soc->caps, MTK_TRGMII) && |
| 508 | !mac->id && state->interface == PHY_INTERFACE_MODE_TRGMII) && |
| 509 | !(MTK_HAS_CAPS(mac->hw->soc->caps, MTK_SGMII) && |
| 510 | (state->interface == PHY_INTERFACE_MODE_SGMII || |
| 511 | phy_interface_mode_is_8023z(state->interface)))) { |
| 512 | linkmode_zero(supported); |
| 513 | return; |
| 514 | } |
| 515 | |
| 516 | phylink_set_port_modes(mask); |
| 517 | phylink_set(mask, Autoneg); |
| 518 | |
| 519 | switch (state->interface) { |
| 520 | case PHY_INTERFACE_MODE_TRGMII: |
| 521 | phylink_set(mask, 1000baseT_Full); |
| 522 | break; |
| 523 | case PHY_INTERFACE_MODE_1000BASEX: |
| 524 | case PHY_INTERFACE_MODE_2500BASEX: |
| 525 | phylink_set(mask, 1000baseX_Full); |
| 526 | phylink_set(mask, 2500baseX_Full); |
| 527 | break; |
| 528 | case PHY_INTERFACE_MODE_GMII: |
| 529 | case PHY_INTERFACE_MODE_RGMII: |
| 530 | case PHY_INTERFACE_MODE_RGMII_ID: |
| 531 | case PHY_INTERFACE_MODE_RGMII_RXID: |
| 532 | case PHY_INTERFACE_MODE_RGMII_TXID: |
| 533 | phylink_set(mask, 1000baseT_Half); |
| 534 | /* fall through */ |
| 535 | case PHY_INTERFACE_MODE_SGMII: |
| 536 | phylink_set(mask, 1000baseT_Full); |
| 537 | phylink_set(mask, 1000baseX_Full); |
| 538 | /* fall through */ |
| 539 | case PHY_INTERFACE_MODE_MII: |
| 540 | case PHY_INTERFACE_MODE_RMII: |
| 541 | case PHY_INTERFACE_MODE_REVMII: |
| 542 | case PHY_INTERFACE_MODE_NA: |
| 543 | default: |
| 544 | phylink_set(mask, 10baseT_Half); |
| 545 | phylink_set(mask, 10baseT_Full); |
| 546 | phylink_set(mask, 100baseT_Half); |
| 547 | phylink_set(mask, 100baseT_Full); |
| 548 | break; |
| 549 | } |
| 550 | |
| 551 | if (state->interface == PHY_INTERFACE_MODE_NA) { |
| 552 | if (MTK_HAS_CAPS(mac->hw->soc->caps, MTK_SGMII)) { |
| 553 | phylink_set(mask, 1000baseT_Full); |
| 554 | phylink_set(mask, 1000baseX_Full); |
| 555 | phylink_set(mask, 2500baseX_Full); |
| 556 | } |
| 557 | if (MTK_HAS_CAPS(mac->hw->soc->caps, MTK_RGMII)) { |
| 558 | phylink_set(mask, 1000baseT_Full); |
| 559 | phylink_set(mask, 1000baseT_Half); |
| 560 | phylink_set(mask, 1000baseX_Full); |
| 561 | } |
| 562 | if (MTK_HAS_CAPS(mac->hw->soc->caps, MTK_GEPHY)) { |
| 563 | phylink_set(mask, 1000baseT_Full); |
| 564 | phylink_set(mask, 1000baseT_Half); |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | phylink_set(mask, Pause); |
| 569 | phylink_set(mask, Asym_Pause); |
| 570 | |
| 571 | linkmode_and(supported, supported, mask); |
| 572 | linkmode_and(state->advertising, state->advertising, mask); |
| 573 | |
| 574 | /* We can only operate at 2500BaseX or 1000BaseX. If requested |
| 575 | * to advertise both, only report advertising at 2500BaseX. |
| 576 | */ |
| 577 | phylink_helper_basex_speed(state); |
| 578 | } |
| 579 | |
| 580 | static const struct phylink_mac_ops mtk_phylink_ops = { |
| 581 | .validate = mtk_validate, |
| 582 | .mac_link_state = mtk_mac_link_state, |
| 583 | .mac_an_restart = mtk_mac_an_restart, |
| 584 | .mac_config = mtk_mac_config, |
| 585 | .mac_link_down = mtk_mac_link_down, |
| 586 | .mac_link_up = mtk_mac_link_up, |
| 587 | }; |
| 588 | |
| 589 | static int mtk_mdio_init(struct mtk_eth *eth) |
| 590 | { |
| 591 | struct device_node *mii_np; |
| 592 | int ret; |
| 593 | |
| 594 | mii_np = of_get_child_by_name(eth->dev->of_node, "mdio-bus"); |
| 595 | if (!mii_np) { |
| 596 | dev_err(eth->dev, "no %s child node found", "mdio-bus"); |
| 597 | return -ENODEV; |
| 598 | } |
| 599 | |
| 600 | if (!of_device_is_available(mii_np)) { |
| 601 | ret = -ENODEV; |
| 602 | goto err_put_node; |
| 603 | } |
| 604 | |
| 605 | eth->mii_bus = devm_mdiobus_alloc(eth->dev); |
| 606 | if (!eth->mii_bus) { |
| 607 | ret = -ENOMEM; |
| 608 | goto err_put_node; |
| 609 | } |
| 610 | |
| 611 | eth->mii_bus->name = "mdio"; |
| 612 | eth->mii_bus->read = mtk_mdio_read; |
| 613 | eth->mii_bus->write = mtk_mdio_write; |
| 614 | eth->mii_bus->priv = eth; |
| 615 | eth->mii_bus->parent = eth->dev; |
| 616 | |
developer | fb556ca | 2021-10-13 10:52:09 +0800 | [diff] [blame^] | 617 | if(snprintf(eth->mii_bus->id, MII_BUS_ID_SIZE, "%pOFn", mii_np)) { |
| 618 | ret = -ENOMEM; |
| 619 | goto err_put_node; |
| 620 | } |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 621 | ret = of_mdiobus_register(eth->mii_bus, mii_np); |
| 622 | |
| 623 | err_put_node: |
| 624 | of_node_put(mii_np); |
| 625 | return ret; |
| 626 | } |
| 627 | |
| 628 | static void mtk_mdio_cleanup(struct mtk_eth *eth) |
| 629 | { |
| 630 | if (!eth->mii_bus) |
| 631 | return; |
| 632 | |
| 633 | mdiobus_unregister(eth->mii_bus); |
| 634 | } |
| 635 | |
| 636 | static inline void mtk_tx_irq_disable(struct mtk_eth *eth, u32 mask) |
| 637 | { |
| 638 | unsigned long flags; |
| 639 | u32 val; |
| 640 | |
| 641 | spin_lock_irqsave(ð->tx_irq_lock, flags); |
| 642 | val = mtk_r32(eth, eth->tx_int_mask_reg); |
| 643 | mtk_w32(eth, val & ~mask, eth->tx_int_mask_reg); |
| 644 | spin_unlock_irqrestore(ð->tx_irq_lock, flags); |
| 645 | } |
| 646 | |
| 647 | static inline void mtk_tx_irq_enable(struct mtk_eth *eth, u32 mask) |
| 648 | { |
| 649 | unsigned long flags; |
| 650 | u32 val; |
| 651 | |
| 652 | spin_lock_irqsave(ð->tx_irq_lock, flags); |
| 653 | val = mtk_r32(eth, eth->tx_int_mask_reg); |
| 654 | mtk_w32(eth, val | mask, eth->tx_int_mask_reg); |
| 655 | spin_unlock_irqrestore(ð->tx_irq_lock, flags); |
| 656 | } |
| 657 | |
| 658 | static inline void mtk_rx_irq_disable(struct mtk_eth *eth, u32 mask) |
| 659 | { |
| 660 | unsigned long flags; |
| 661 | u32 val; |
| 662 | |
| 663 | spin_lock_irqsave(ð->rx_irq_lock, flags); |
| 664 | val = mtk_r32(eth, MTK_PDMA_INT_MASK); |
| 665 | mtk_w32(eth, val & ~mask, MTK_PDMA_INT_MASK); |
| 666 | spin_unlock_irqrestore(ð->rx_irq_lock, flags); |
| 667 | } |
| 668 | |
| 669 | static inline void mtk_rx_irq_enable(struct mtk_eth *eth, u32 mask) |
| 670 | { |
| 671 | unsigned long flags; |
| 672 | u32 val; |
| 673 | |
| 674 | spin_lock_irqsave(ð->rx_irq_lock, flags); |
| 675 | val = mtk_r32(eth, MTK_PDMA_INT_MASK); |
| 676 | mtk_w32(eth, val | mask, MTK_PDMA_INT_MASK); |
| 677 | spin_unlock_irqrestore(ð->rx_irq_lock, flags); |
| 678 | } |
| 679 | |
| 680 | static int mtk_set_mac_address(struct net_device *dev, void *p) |
| 681 | { |
| 682 | int ret = eth_mac_addr(dev, p); |
| 683 | struct mtk_mac *mac = netdev_priv(dev); |
| 684 | struct mtk_eth *eth = mac->hw; |
| 685 | const char *macaddr = dev->dev_addr; |
| 686 | |
| 687 | if (ret) |
| 688 | return ret; |
| 689 | |
| 690 | if (unlikely(test_bit(MTK_RESETTING, &mac->hw->state))) |
| 691 | return -EBUSY; |
| 692 | |
| 693 | spin_lock_bh(&mac->hw->page_lock); |
| 694 | if (MTK_HAS_CAPS(eth->soc->caps, MTK_SOC_MT7628)) { |
| 695 | mtk_w32(mac->hw, (macaddr[0] << 8) | macaddr[1], |
| 696 | MT7628_SDM_MAC_ADRH); |
| 697 | mtk_w32(mac->hw, (macaddr[2] << 24) | (macaddr[3] << 16) | |
| 698 | (macaddr[4] << 8) | macaddr[5], |
| 699 | MT7628_SDM_MAC_ADRL); |
| 700 | } else { |
| 701 | mtk_w32(mac->hw, (macaddr[0] << 8) | macaddr[1], |
| 702 | MTK_GDMA_MAC_ADRH(mac->id)); |
| 703 | mtk_w32(mac->hw, (macaddr[2] << 24) | (macaddr[3] << 16) | |
| 704 | (macaddr[4] << 8) | macaddr[5], |
| 705 | MTK_GDMA_MAC_ADRL(mac->id)); |
| 706 | } |
| 707 | spin_unlock_bh(&mac->hw->page_lock); |
| 708 | |
| 709 | return 0; |
| 710 | } |
| 711 | |
| 712 | void mtk_stats_update_mac(struct mtk_mac *mac) |
| 713 | { |
| 714 | struct mtk_hw_stats *hw_stats = mac->hw_stats; |
| 715 | unsigned int base = MTK_GDM1_TX_GBCNT; |
| 716 | u64 stats; |
| 717 | |
| 718 | base += hw_stats->reg_offset; |
| 719 | |
| 720 | u64_stats_update_begin(&hw_stats->syncp); |
| 721 | |
| 722 | hw_stats->rx_bytes += mtk_r32(mac->hw, base); |
| 723 | stats = mtk_r32(mac->hw, base + 0x04); |
| 724 | if (stats) |
| 725 | hw_stats->rx_bytes += (stats << 32); |
| 726 | hw_stats->rx_packets += mtk_r32(mac->hw, base + 0x08); |
| 727 | hw_stats->rx_overflow += mtk_r32(mac->hw, base + 0x10); |
| 728 | hw_stats->rx_fcs_errors += mtk_r32(mac->hw, base + 0x14); |
| 729 | hw_stats->rx_short_errors += mtk_r32(mac->hw, base + 0x18); |
| 730 | hw_stats->rx_long_errors += mtk_r32(mac->hw, base + 0x1c); |
| 731 | hw_stats->rx_checksum_errors += mtk_r32(mac->hw, base + 0x20); |
| 732 | hw_stats->rx_flow_control_packets += |
| 733 | mtk_r32(mac->hw, base + 0x24); |
| 734 | hw_stats->tx_skip += mtk_r32(mac->hw, base + 0x28); |
| 735 | hw_stats->tx_collisions += mtk_r32(mac->hw, base + 0x2c); |
| 736 | hw_stats->tx_bytes += mtk_r32(mac->hw, base + 0x30); |
| 737 | stats = mtk_r32(mac->hw, base + 0x34); |
| 738 | if (stats) |
| 739 | hw_stats->tx_bytes += (stats << 32); |
| 740 | hw_stats->tx_packets += mtk_r32(mac->hw, base + 0x38); |
| 741 | u64_stats_update_end(&hw_stats->syncp); |
| 742 | } |
| 743 | |
| 744 | static void mtk_stats_update(struct mtk_eth *eth) |
| 745 | { |
| 746 | int i; |
| 747 | |
| 748 | for (i = 0; i < MTK_MAC_COUNT; i++) { |
| 749 | if (!eth->mac[i] || !eth->mac[i]->hw_stats) |
| 750 | continue; |
| 751 | if (spin_trylock(ð->mac[i]->hw_stats->stats_lock)) { |
| 752 | mtk_stats_update_mac(eth->mac[i]); |
| 753 | spin_unlock(ð->mac[i]->hw_stats->stats_lock); |
| 754 | } |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | static void mtk_get_stats64(struct net_device *dev, |
| 759 | struct rtnl_link_stats64 *storage) |
| 760 | { |
| 761 | struct mtk_mac *mac = netdev_priv(dev); |
| 762 | struct mtk_hw_stats *hw_stats = mac->hw_stats; |
| 763 | unsigned int start; |
| 764 | |
| 765 | if (netif_running(dev) && netif_device_present(dev)) { |
| 766 | if (spin_trylock_bh(&hw_stats->stats_lock)) { |
| 767 | mtk_stats_update_mac(mac); |
| 768 | spin_unlock_bh(&hw_stats->stats_lock); |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | do { |
| 773 | start = u64_stats_fetch_begin_irq(&hw_stats->syncp); |
| 774 | storage->rx_packets = hw_stats->rx_packets; |
| 775 | storage->tx_packets = hw_stats->tx_packets; |
| 776 | storage->rx_bytes = hw_stats->rx_bytes; |
| 777 | storage->tx_bytes = hw_stats->tx_bytes; |
| 778 | storage->collisions = hw_stats->tx_collisions; |
| 779 | storage->rx_length_errors = hw_stats->rx_short_errors + |
| 780 | hw_stats->rx_long_errors; |
| 781 | storage->rx_over_errors = hw_stats->rx_overflow; |
| 782 | storage->rx_crc_errors = hw_stats->rx_fcs_errors; |
| 783 | storage->rx_errors = hw_stats->rx_checksum_errors; |
| 784 | storage->tx_aborted_errors = hw_stats->tx_skip; |
| 785 | } while (u64_stats_fetch_retry_irq(&hw_stats->syncp, start)); |
| 786 | |
| 787 | storage->tx_errors = dev->stats.tx_errors; |
| 788 | storage->rx_dropped = dev->stats.rx_dropped; |
| 789 | storage->tx_dropped = dev->stats.tx_dropped; |
| 790 | } |
| 791 | |
| 792 | static inline int mtk_max_frag_size(int mtu) |
| 793 | { |
| 794 | /* make sure buf_size will be at least MTK_MAX_RX_LENGTH */ |
| 795 | if (mtu + MTK_RX_ETH_HLEN < MTK_MAX_RX_LENGTH) |
| 796 | mtu = MTK_MAX_RX_LENGTH - MTK_RX_ETH_HLEN; |
| 797 | |
| 798 | return SKB_DATA_ALIGN(MTK_RX_HLEN + mtu) + |
| 799 | SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
| 800 | } |
| 801 | |
| 802 | static inline int mtk_max_buf_size(int frag_size) |
| 803 | { |
| 804 | int buf_size = frag_size - NET_SKB_PAD - NET_IP_ALIGN - |
| 805 | SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
| 806 | |
| 807 | WARN_ON(buf_size < MTK_MAX_RX_LENGTH); |
| 808 | |
| 809 | return buf_size; |
| 810 | } |
| 811 | |
developer | c4671b2 | 2021-05-28 13:16:42 +0800 | [diff] [blame] | 812 | static inline bool mtk_rx_get_desc(struct mtk_rx_dma *rxd, |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 813 | struct mtk_rx_dma *dma_rxd) |
| 814 | { |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 815 | rxd->rxd2 = READ_ONCE(dma_rxd->rxd2); |
developer | c4671b2 | 2021-05-28 13:16:42 +0800 | [diff] [blame] | 816 | if (!(rxd->rxd2 & RX_DMA_DONE)) |
| 817 | return false; |
| 818 | |
| 819 | rxd->rxd1 = READ_ONCE(dma_rxd->rxd1); |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 820 | rxd->rxd3 = READ_ONCE(dma_rxd->rxd3); |
| 821 | rxd->rxd4 = READ_ONCE(dma_rxd->rxd4); |
developer | a2bdbd5 | 2021-05-31 19:10:17 +0800 | [diff] [blame] | 822 | #if defined(CONFIG_MEDIATEK_NETSYS_V2) |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 823 | rxd->rxd5 = READ_ONCE(dma_rxd->rxd5); |
| 824 | rxd->rxd6 = READ_ONCE(dma_rxd->rxd6); |
| 825 | #endif |
developer | c4671b2 | 2021-05-28 13:16:42 +0800 | [diff] [blame] | 826 | return true; |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | /* the qdma core needs scratch memory to be setup */ |
| 830 | static int mtk_init_fq_dma(struct mtk_eth *eth) |
| 831 | { |
| 832 | dma_addr_t phy_ring_tail; |
| 833 | int cnt = MTK_DMA_SIZE; |
| 834 | dma_addr_t dma_addr; |
| 835 | int i; |
| 836 | |
| 837 | if (!eth->soc->has_sram) { |
| 838 | eth->scratch_ring = dma_alloc_coherent(eth->dev, |
| 839 | cnt * sizeof(struct mtk_tx_dma), |
| 840 | ð->phy_scratch_ring, |
| 841 | GFP_ATOMIC); |
| 842 | } else { |
| 843 | eth->scratch_ring = eth->base + MTK_ETH_SRAM_OFFSET; |
| 844 | } |
| 845 | |
| 846 | if (unlikely(!eth->scratch_ring)) |
| 847 | return -ENOMEM; |
| 848 | |
| 849 | eth->scratch_head = kcalloc(cnt, MTK_QDMA_PAGE_SIZE, |
| 850 | GFP_KERNEL); |
| 851 | if (unlikely(!eth->scratch_head)) |
| 852 | return -ENOMEM; |
| 853 | |
| 854 | dma_addr = dma_map_single(eth->dev, |
| 855 | eth->scratch_head, cnt * MTK_QDMA_PAGE_SIZE, |
| 856 | DMA_FROM_DEVICE); |
| 857 | if (unlikely(dma_mapping_error(eth->dev, dma_addr))) |
| 858 | return -ENOMEM; |
| 859 | |
| 860 | phy_ring_tail = eth->phy_scratch_ring + |
| 861 | (sizeof(struct mtk_tx_dma) * (cnt - 1)); |
| 862 | |
| 863 | for (i = 0; i < cnt; i++) { |
| 864 | eth->scratch_ring[i].txd1 = |
| 865 | (dma_addr + (i * MTK_QDMA_PAGE_SIZE)); |
| 866 | if (i < cnt - 1) |
| 867 | eth->scratch_ring[i].txd2 = (eth->phy_scratch_ring + |
| 868 | ((i + 1) * sizeof(struct mtk_tx_dma))); |
| 869 | eth->scratch_ring[i].txd3 = TX_DMA_SDL(MTK_QDMA_PAGE_SIZE); |
| 870 | |
| 871 | eth->scratch_ring[i].txd4 = 0; |
| 872 | #if defined(CONFIG_MEDIATEK_NETSYS_V2) |
| 873 | if (eth->soc->has_sram && ((sizeof(struct mtk_tx_dma)) > 16)) { |
| 874 | eth->scratch_ring[i].txd5 = 0; |
| 875 | eth->scratch_ring[i].txd6 = 0; |
| 876 | eth->scratch_ring[i].txd7 = 0; |
| 877 | eth->scratch_ring[i].txd8 = 0; |
| 878 | } |
| 879 | #endif |
| 880 | } |
| 881 | |
| 882 | mtk_w32(eth, eth->phy_scratch_ring, MTK_QDMA_FQ_HEAD); |
| 883 | mtk_w32(eth, phy_ring_tail, MTK_QDMA_FQ_TAIL); |
| 884 | mtk_w32(eth, (cnt << 16) | cnt, MTK_QDMA_FQ_CNT); |
| 885 | mtk_w32(eth, MTK_QDMA_PAGE_SIZE << 16, MTK_QDMA_FQ_BLEN); |
| 886 | |
| 887 | return 0; |
| 888 | } |
| 889 | |
| 890 | static inline void *mtk_qdma_phys_to_virt(struct mtk_tx_ring *ring, u32 desc) |
| 891 | { |
| 892 | void *ret = ring->dma; |
| 893 | |
| 894 | return ret + (desc - ring->phys); |
| 895 | } |
| 896 | |
| 897 | static inline struct mtk_tx_buf *mtk_desc_to_tx_buf(struct mtk_tx_ring *ring, |
| 898 | struct mtk_tx_dma *txd) |
| 899 | { |
| 900 | int idx = txd - ring->dma; |
| 901 | |
| 902 | return &ring->buf[idx]; |
| 903 | } |
| 904 | |
| 905 | static struct mtk_tx_dma *qdma_to_pdma(struct mtk_tx_ring *ring, |
| 906 | struct mtk_tx_dma *dma) |
| 907 | { |
| 908 | return ring->dma_pdma - ring->dma + dma; |
| 909 | } |
| 910 | |
| 911 | static int txd_to_idx(struct mtk_tx_ring *ring, struct mtk_tx_dma *dma) |
| 912 | { |
| 913 | return ((void *)dma - (void *)ring->dma) / sizeof(*dma); |
| 914 | } |
| 915 | |
developer | c4671b2 | 2021-05-28 13:16:42 +0800 | [diff] [blame] | 916 | static void mtk_tx_unmap(struct mtk_eth *eth, struct mtk_tx_buf *tx_buf, |
| 917 | bool napi) |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 918 | { |
| 919 | if (MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA)) { |
| 920 | if (tx_buf->flags & MTK_TX_FLAGS_SINGLE0) { |
| 921 | dma_unmap_single(eth->dev, |
| 922 | dma_unmap_addr(tx_buf, dma_addr0), |
| 923 | dma_unmap_len(tx_buf, dma_len0), |
| 924 | DMA_TO_DEVICE); |
| 925 | } else if (tx_buf->flags & MTK_TX_FLAGS_PAGE0) { |
| 926 | dma_unmap_page(eth->dev, |
| 927 | dma_unmap_addr(tx_buf, dma_addr0), |
| 928 | dma_unmap_len(tx_buf, dma_len0), |
| 929 | DMA_TO_DEVICE); |
| 930 | } |
| 931 | } else { |
| 932 | if (dma_unmap_len(tx_buf, dma_len0)) { |
| 933 | dma_unmap_page(eth->dev, |
| 934 | dma_unmap_addr(tx_buf, dma_addr0), |
| 935 | dma_unmap_len(tx_buf, dma_len0), |
| 936 | DMA_TO_DEVICE); |
| 937 | } |
| 938 | |
| 939 | if (dma_unmap_len(tx_buf, dma_len1)) { |
| 940 | dma_unmap_page(eth->dev, |
| 941 | dma_unmap_addr(tx_buf, dma_addr1), |
| 942 | dma_unmap_len(tx_buf, dma_len1), |
| 943 | DMA_TO_DEVICE); |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | tx_buf->flags = 0; |
| 948 | if (tx_buf->skb && |
developer | c4671b2 | 2021-05-28 13:16:42 +0800 | [diff] [blame] | 949 | (tx_buf->skb != (struct sk_buff *)MTK_DMA_DUMMY_DESC)) { |
| 950 | if (napi) |
| 951 | napi_consume_skb(tx_buf->skb, napi); |
| 952 | else |
| 953 | dev_kfree_skb_any(tx_buf->skb); |
| 954 | } |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 955 | tx_buf->skb = NULL; |
| 956 | } |
| 957 | |
| 958 | static void setup_tx_buf(struct mtk_eth *eth, struct mtk_tx_buf *tx_buf, |
| 959 | struct mtk_tx_dma *txd, dma_addr_t mapped_addr, |
| 960 | size_t size, int idx) |
| 961 | { |
| 962 | if (MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA)) { |
| 963 | dma_unmap_addr_set(tx_buf, dma_addr0, mapped_addr); |
| 964 | dma_unmap_len_set(tx_buf, dma_len0, size); |
| 965 | } else { |
| 966 | if (idx & 1) { |
| 967 | txd->txd3 = mapped_addr; |
| 968 | txd->txd2 |= TX_DMA_PLEN1(size); |
| 969 | dma_unmap_addr_set(tx_buf, dma_addr1, mapped_addr); |
| 970 | dma_unmap_len_set(tx_buf, dma_len1, size); |
| 971 | } else { |
| 972 | tx_buf->skb = (struct sk_buff *)MTK_DMA_DUMMY_DESC; |
| 973 | txd->txd1 = mapped_addr; |
| 974 | txd->txd2 = TX_DMA_PLEN0(size); |
| 975 | dma_unmap_addr_set(tx_buf, dma_addr0, mapped_addr); |
| 976 | dma_unmap_len_set(tx_buf, dma_len0, size); |
| 977 | } |
| 978 | } |
| 979 | } |
| 980 | |
| 981 | static int mtk_tx_map(struct sk_buff *skb, struct net_device *dev, |
| 982 | int tx_num, struct mtk_tx_ring *ring, bool gso) |
| 983 | { |
| 984 | struct mtk_mac *mac = netdev_priv(dev); |
| 985 | struct mtk_eth *eth = mac->hw; |
| 986 | struct mtk_tx_dma *itxd, *txd; |
| 987 | struct mtk_tx_dma *itxd_pdma, *txd_pdma; |
| 988 | struct mtk_tx_buf *itx_buf, *tx_buf; |
| 989 | dma_addr_t mapped_addr; |
| 990 | unsigned int nr_frags; |
| 991 | int i, n_desc = 1; |
| 992 | u32 txd4 = 0, fport; |
| 993 | u32 qid = 0; |
| 994 | int k = 0; |
| 995 | |
| 996 | itxd = ring->next_free; |
| 997 | itxd_pdma = qdma_to_pdma(ring, itxd); |
| 998 | if (itxd == ring->last_free) |
| 999 | return -ENOMEM; |
| 1000 | |
| 1001 | itx_buf = mtk_desc_to_tx_buf(ring, itxd); |
| 1002 | memset(itx_buf, 0, sizeof(*itx_buf)); |
| 1003 | |
| 1004 | mapped_addr = dma_map_single(eth->dev, skb->data, |
| 1005 | skb_headlen(skb), DMA_TO_DEVICE); |
| 1006 | if (unlikely(dma_mapping_error(eth->dev, mapped_addr))) |
| 1007 | return -ENOMEM; |
| 1008 | |
| 1009 | WRITE_ONCE(itxd->txd1, mapped_addr); |
| 1010 | itx_buf->flags |= MTK_TX_FLAGS_SINGLE0; |
| 1011 | itx_buf->flags |= (!mac->id) ? MTK_TX_FLAGS_FPORT0 : |
| 1012 | MTK_TX_FLAGS_FPORT1; |
| 1013 | setup_tx_buf(eth, itx_buf, itxd_pdma, mapped_addr, skb_headlen(skb), |
| 1014 | k++); |
| 1015 | |
| 1016 | nr_frags = skb_shinfo(skb)->nr_frags; |
| 1017 | |
| 1018 | #if defined(CONFIG_NET_MEDIATEK_HW_QOS) |
| 1019 | qid = skb->mark & (MTK_QDMA_TX_MASK); |
| 1020 | #endif |
| 1021 | |
developer | a2bdbd5 | 2021-05-31 19:10:17 +0800 | [diff] [blame] | 1022 | if (MTK_HAS_CAPS(eth->soc->caps, MTK_NETSYS_V2)) { |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1023 | u32 txd5 = 0, txd6 = 0; |
| 1024 | /* set the forward port */ |
| 1025 | fport = (mac->id + 1) << TX_DMA_FPORT_SHIFT_V2; |
| 1026 | txd4 |= fport; |
| 1027 | |
| 1028 | if (gso) |
| 1029 | txd5 |= TX_DMA_TSO_V2; |
| 1030 | |
| 1031 | /* TX Checksum offload */ |
| 1032 | if (skb->ip_summed == CHECKSUM_PARTIAL) |
| 1033 | txd5 |= TX_DMA_CHKSUM_V2; |
| 1034 | |
| 1035 | /* VLAN header offload */ |
| 1036 | if (skb_vlan_tag_present(skb)) |
| 1037 | txd6 |= TX_DMA_INS_VLAN_V2 | skb_vlan_tag_get(skb); |
| 1038 | |
| 1039 | txd4 = txd4 | TX_DMA_SWC_V2; |
| 1040 | |
| 1041 | WRITE_ONCE(itxd->txd3, (TX_DMA_PLEN0(skb_headlen(skb)) | |
| 1042 | (!nr_frags * TX_DMA_LS0))); |
| 1043 | |
| 1044 | #if defined(CONFIG_MEDIATEK_NETSYS_V2) |
| 1045 | WRITE_ONCE(itxd->txd5, txd5); |
| 1046 | WRITE_ONCE(itxd->txd6, txd6); |
| 1047 | #endif |
| 1048 | } else { |
| 1049 | /* set the forward port */ |
| 1050 | fport = (mac->id + 1) << TX_DMA_FPORT_SHIFT; |
| 1051 | txd4 |= fport; |
| 1052 | |
| 1053 | if (gso) |
| 1054 | txd4 |= TX_DMA_TSO; |
| 1055 | |
| 1056 | /* TX Checksum offload */ |
| 1057 | if (skb->ip_summed == CHECKSUM_PARTIAL) |
| 1058 | txd4 |= TX_DMA_CHKSUM; |
| 1059 | |
| 1060 | /* VLAN header offload */ |
| 1061 | if (skb_vlan_tag_present(skb)) |
| 1062 | txd4 |= TX_DMA_INS_VLAN | skb_vlan_tag_get(skb); |
| 1063 | |
| 1064 | WRITE_ONCE(itxd->txd3, |
| 1065 | TX_DMA_SWC | TX_DMA_PLEN0(skb_headlen(skb)) | |
| 1066 | (!nr_frags * TX_DMA_LS0) | QID_LOW_BITS(qid)); |
| 1067 | } |
| 1068 | /* TX SG offload */ |
| 1069 | txd = itxd; |
| 1070 | txd_pdma = qdma_to_pdma(ring, txd); |
| 1071 | |
| 1072 | #if defined(CONFIG_NET_MEDIATEK_HNAT) || defined(CONFIG_NET_MEDIATEK_HNAT_MODULE) |
| 1073 | if (HNAT_SKB_CB2(skb)->magic == 0x78681415) { |
developer | a2bdbd5 | 2021-05-31 19:10:17 +0800 | [diff] [blame] | 1074 | if (MTK_HAS_CAPS(eth->soc->caps, MTK_NETSYS_V2)) { |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1075 | txd4 &= ~(0xf << TX_DMA_FPORT_SHIFT_V2); |
| 1076 | txd4 |= 0x4 << TX_DMA_FPORT_SHIFT_V2; |
| 1077 | } else { |
| 1078 | txd4 &= ~(0x7 << TX_DMA_FPORT_SHIFT); |
| 1079 | txd4 |= 0x4 << TX_DMA_FPORT_SHIFT; |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | trace_printk("[%s] nr_frags=%x HNAT_SKB_CB2(skb)->magic=%x txd4=%x<-----\n", |
| 1084 | __func__, nr_frags, HNAT_SKB_CB2(skb)->magic, txd4); |
| 1085 | #endif |
| 1086 | |
| 1087 | for (i = 0; i < nr_frags; i++) { |
| 1088 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 1089 | unsigned int offset = 0; |
| 1090 | int frag_size = skb_frag_size(frag); |
| 1091 | |
| 1092 | while (frag_size) { |
| 1093 | bool last_frag = false; |
| 1094 | unsigned int frag_map_size; |
| 1095 | bool new_desc = true; |
| 1096 | |
| 1097 | if (MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA) || |
| 1098 | (i & 0x1)) { |
| 1099 | txd = mtk_qdma_phys_to_virt(ring, txd->txd2); |
| 1100 | txd_pdma = qdma_to_pdma(ring, txd); |
| 1101 | if (txd == ring->last_free) |
| 1102 | goto err_dma; |
| 1103 | |
| 1104 | n_desc++; |
| 1105 | } else { |
| 1106 | new_desc = false; |
| 1107 | } |
| 1108 | |
| 1109 | |
| 1110 | frag_map_size = min(frag_size, MTK_TX_DMA_BUF_LEN); |
| 1111 | mapped_addr = skb_frag_dma_map(eth->dev, frag, offset, |
| 1112 | frag_map_size, |
| 1113 | DMA_TO_DEVICE); |
| 1114 | if (unlikely(dma_mapping_error(eth->dev, mapped_addr))) |
| 1115 | goto err_dma; |
| 1116 | |
| 1117 | if (i == nr_frags - 1 && |
| 1118 | (frag_size - frag_map_size) == 0) |
| 1119 | last_frag = true; |
| 1120 | |
| 1121 | WRITE_ONCE(txd->txd1, mapped_addr); |
| 1122 | |
developer | a2bdbd5 | 2021-05-31 19:10:17 +0800 | [diff] [blame] | 1123 | if (MTK_HAS_CAPS(eth->soc->caps, MTK_NETSYS_V2)) { |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1124 | WRITE_ONCE(txd->txd3, (TX_DMA_PLEN0(frag_map_size) | |
| 1125 | last_frag * TX_DMA_LS0)); |
| 1126 | WRITE_ONCE(txd->txd4, fport | TX_DMA_SWC_V2 | |
| 1127 | QID_BITS_V2(qid)); |
| 1128 | } else { |
| 1129 | WRITE_ONCE(txd->txd3, |
| 1130 | (TX_DMA_SWC | QID_LOW_BITS(qid) | |
| 1131 | TX_DMA_PLEN0(frag_map_size) | |
| 1132 | last_frag * TX_DMA_LS0)); |
| 1133 | WRITE_ONCE(txd->txd4, |
| 1134 | fport | QID_HIGH_BITS(qid)); |
| 1135 | } |
| 1136 | |
| 1137 | tx_buf = mtk_desc_to_tx_buf(ring, txd); |
| 1138 | if (new_desc) |
| 1139 | memset(tx_buf, 0, sizeof(*tx_buf)); |
| 1140 | tx_buf->skb = (struct sk_buff *)MTK_DMA_DUMMY_DESC; |
| 1141 | tx_buf->flags |= MTK_TX_FLAGS_PAGE0; |
| 1142 | tx_buf->flags |= (!mac->id) ? MTK_TX_FLAGS_FPORT0 : |
| 1143 | MTK_TX_FLAGS_FPORT1; |
| 1144 | |
| 1145 | setup_tx_buf(eth, tx_buf, txd_pdma, mapped_addr, |
| 1146 | frag_map_size, k++); |
| 1147 | |
| 1148 | frag_size -= frag_map_size; |
| 1149 | offset += frag_map_size; |
| 1150 | } |
| 1151 | } |
| 1152 | |
| 1153 | /* store skb to cleanup */ |
| 1154 | itx_buf->skb = skb; |
| 1155 | |
developer | a2bdbd5 | 2021-05-31 19:10:17 +0800 | [diff] [blame] | 1156 | if (MTK_HAS_CAPS(eth->soc->caps, MTK_NETSYS_V2)) |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1157 | WRITE_ONCE(itxd->txd4, txd4 | QID_BITS_V2(qid)); |
| 1158 | else |
| 1159 | WRITE_ONCE(itxd->txd4, txd4 | QID_HIGH_BITS(qid)); |
| 1160 | |
| 1161 | if (!MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA)) { |
| 1162 | if (k & 0x1) |
| 1163 | txd_pdma->txd2 |= TX_DMA_LS0; |
| 1164 | else |
| 1165 | txd_pdma->txd2 |= TX_DMA_LS1; |
| 1166 | } |
| 1167 | |
| 1168 | netdev_sent_queue(dev, skb->len); |
| 1169 | skb_tx_timestamp(skb); |
| 1170 | |
| 1171 | ring->next_free = mtk_qdma_phys_to_virt(ring, txd->txd2); |
| 1172 | atomic_sub(n_desc, &ring->free_count); |
| 1173 | |
| 1174 | /* make sure that all changes to the dma ring are flushed before we |
| 1175 | * continue |
| 1176 | */ |
| 1177 | wmb(); |
| 1178 | |
| 1179 | if (MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA)) { |
| 1180 | if (netif_xmit_stopped(netdev_get_tx_queue(dev, 0)) || |
| 1181 | !netdev_xmit_more()) |
| 1182 | mtk_w32(eth, txd->txd2, MTK_QTX_CTX_PTR); |
| 1183 | } else { |
| 1184 | int next_idx = NEXT_DESP_IDX(txd_to_idx(ring, txd), |
| 1185 | ring->dma_size); |
| 1186 | mtk_w32(eth, next_idx, MT7628_TX_CTX_IDX0); |
| 1187 | } |
| 1188 | |
| 1189 | return 0; |
| 1190 | |
| 1191 | err_dma: |
| 1192 | do { |
| 1193 | tx_buf = mtk_desc_to_tx_buf(ring, itxd); |
| 1194 | |
| 1195 | /* unmap dma */ |
developer | c4671b2 | 2021-05-28 13:16:42 +0800 | [diff] [blame] | 1196 | mtk_tx_unmap(eth, tx_buf, false); |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1197 | |
| 1198 | itxd->txd3 = TX_DMA_LS0 | TX_DMA_OWNER_CPU; |
| 1199 | if (!MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA)) |
| 1200 | itxd_pdma->txd2 = TX_DMA_DESP2_DEF; |
| 1201 | |
| 1202 | itxd = mtk_qdma_phys_to_virt(ring, itxd->txd2); |
| 1203 | itxd_pdma = qdma_to_pdma(ring, itxd); |
| 1204 | } while (itxd != txd); |
| 1205 | |
| 1206 | return -ENOMEM; |
| 1207 | } |
| 1208 | |
| 1209 | static inline int mtk_cal_txd_req(struct sk_buff *skb) |
| 1210 | { |
| 1211 | int i, nfrags; |
| 1212 | skb_frag_t *frag; |
| 1213 | |
| 1214 | nfrags = 1; |
| 1215 | if (skb_is_gso(skb)) { |
| 1216 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 1217 | frag = &skb_shinfo(skb)->frags[i]; |
| 1218 | nfrags += DIV_ROUND_UP(skb_frag_size(frag), |
| 1219 | MTK_TX_DMA_BUF_LEN); |
| 1220 | } |
| 1221 | } else { |
| 1222 | nfrags += skb_shinfo(skb)->nr_frags; |
| 1223 | } |
| 1224 | |
| 1225 | return nfrags; |
| 1226 | } |
| 1227 | |
| 1228 | static int mtk_queue_stopped(struct mtk_eth *eth) |
| 1229 | { |
| 1230 | int i; |
| 1231 | |
| 1232 | for (i = 0; i < MTK_MAC_COUNT; i++) { |
| 1233 | if (!eth->netdev[i]) |
| 1234 | continue; |
| 1235 | if (netif_queue_stopped(eth->netdev[i])) |
| 1236 | return 1; |
| 1237 | } |
| 1238 | |
| 1239 | return 0; |
| 1240 | } |
| 1241 | |
| 1242 | static void mtk_wake_queue(struct mtk_eth *eth) |
| 1243 | { |
| 1244 | int i; |
| 1245 | |
| 1246 | for (i = 0; i < MTK_MAC_COUNT; i++) { |
| 1247 | if (!eth->netdev[i]) |
| 1248 | continue; |
| 1249 | netif_wake_queue(eth->netdev[i]); |
| 1250 | } |
| 1251 | } |
| 1252 | |
| 1253 | static int mtk_start_xmit(struct sk_buff *skb, struct net_device *dev) |
| 1254 | { |
| 1255 | struct mtk_mac *mac = netdev_priv(dev); |
| 1256 | struct mtk_eth *eth = mac->hw; |
| 1257 | struct mtk_tx_ring *ring = ð->tx_ring; |
| 1258 | struct net_device_stats *stats = &dev->stats; |
| 1259 | bool gso = false; |
| 1260 | int tx_num; |
| 1261 | |
| 1262 | /* normally we can rely on the stack not calling this more than once, |
| 1263 | * however we have 2 queues running on the same ring so we need to lock |
| 1264 | * the ring access |
| 1265 | */ |
| 1266 | spin_lock(ð->page_lock); |
| 1267 | |
| 1268 | if (unlikely(test_bit(MTK_RESETTING, ð->state))) |
| 1269 | goto drop; |
| 1270 | |
| 1271 | tx_num = mtk_cal_txd_req(skb); |
| 1272 | if (unlikely(atomic_read(&ring->free_count) <= tx_num)) { |
| 1273 | netif_stop_queue(dev); |
| 1274 | netif_err(eth, tx_queued, dev, |
| 1275 | "Tx Ring full when queue awake!\n"); |
| 1276 | spin_unlock(ð->page_lock); |
| 1277 | return NETDEV_TX_BUSY; |
| 1278 | } |
| 1279 | |
| 1280 | /* TSO: fill MSS info in tcp checksum field */ |
| 1281 | if (skb_is_gso(skb)) { |
| 1282 | if (skb_cow_head(skb, 0)) { |
| 1283 | netif_warn(eth, tx_err, dev, |
| 1284 | "GSO expand head fail.\n"); |
| 1285 | goto drop; |
| 1286 | } |
| 1287 | |
| 1288 | if (skb_shinfo(skb)->gso_type & |
| 1289 | (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)) { |
| 1290 | gso = true; |
| 1291 | tcp_hdr(skb)->check = htons(skb_shinfo(skb)->gso_size); |
| 1292 | } |
| 1293 | } |
| 1294 | |
| 1295 | if (mtk_tx_map(skb, dev, tx_num, ring, gso) < 0) |
| 1296 | goto drop; |
| 1297 | |
| 1298 | if (unlikely(atomic_read(&ring->free_count) <= ring->thresh)) |
| 1299 | netif_stop_queue(dev); |
| 1300 | |
| 1301 | spin_unlock(ð->page_lock); |
| 1302 | |
| 1303 | return NETDEV_TX_OK; |
| 1304 | |
| 1305 | drop: |
| 1306 | spin_unlock(ð->page_lock); |
| 1307 | stats->tx_dropped++; |
| 1308 | dev_kfree_skb_any(skb); |
| 1309 | return NETDEV_TX_OK; |
| 1310 | } |
| 1311 | |
| 1312 | static struct mtk_rx_ring *mtk_get_rx_ring(struct mtk_eth *eth) |
| 1313 | { |
| 1314 | int i; |
| 1315 | struct mtk_rx_ring *ring; |
| 1316 | int idx; |
| 1317 | |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1318 | for (i = 0; i < MTK_MAX_RX_RING_NUM; i++) { |
developer | 77d03a7 | 2021-06-06 00:06:00 +0800 | [diff] [blame] | 1319 | if (!IS_NORMAL_RING(i) && !IS_HW_LRO_RING(i)) |
| 1320 | continue; |
| 1321 | |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1322 | ring = ð->rx_ring[i]; |
| 1323 | idx = NEXT_DESP_IDX(ring->calc_idx, ring->dma_size); |
| 1324 | if (ring->dma[idx].rxd2 & RX_DMA_DONE) { |
| 1325 | ring->calc_idx_update = true; |
| 1326 | return ring; |
| 1327 | } |
| 1328 | } |
| 1329 | |
| 1330 | return NULL; |
| 1331 | } |
| 1332 | |
developer | 18f46a8 | 2021-07-20 21:08:21 +0800 | [diff] [blame] | 1333 | static void mtk_update_rx_cpu_idx(struct mtk_eth *eth, struct mtk_rx_ring *ring) |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1334 | { |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1335 | int i; |
| 1336 | |
developer | fb556ca | 2021-10-13 10:52:09 +0800 | [diff] [blame^] | 1337 | if (!eth->hwlro) |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1338 | mtk_w32(eth, ring->calc_idx, ring->crx_idx_reg); |
developer | fb556ca | 2021-10-13 10:52:09 +0800 | [diff] [blame^] | 1339 | else { |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1340 | for (i = 0; i < MTK_MAX_RX_RING_NUM; i++) { |
| 1341 | ring = ð->rx_ring[i]; |
| 1342 | if (ring->calc_idx_update) { |
| 1343 | ring->calc_idx_update = false; |
| 1344 | mtk_w32(eth, ring->calc_idx, ring->crx_idx_reg); |
| 1345 | } |
| 1346 | } |
| 1347 | } |
| 1348 | } |
| 1349 | |
| 1350 | static int mtk_poll_rx(struct napi_struct *napi, int budget, |
| 1351 | struct mtk_eth *eth) |
| 1352 | { |
developer | 18f46a8 | 2021-07-20 21:08:21 +0800 | [diff] [blame] | 1353 | struct mtk_napi *rx_napi = container_of(napi, struct mtk_napi, napi); |
| 1354 | struct mtk_rx_ring *ring = rx_napi->rx_ring; |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1355 | int idx; |
| 1356 | struct sk_buff *skb; |
| 1357 | u8 *data, *new_data; |
| 1358 | struct mtk_rx_dma *rxd, trxd; |
| 1359 | int done = 0; |
| 1360 | |
developer | 18f46a8 | 2021-07-20 21:08:21 +0800 | [diff] [blame] | 1361 | if (unlikely(!ring)) |
| 1362 | goto rx_done; |
| 1363 | |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1364 | while (done < budget) { |
| 1365 | struct net_device *netdev; |
| 1366 | unsigned int pktlen; |
| 1367 | dma_addr_t dma_addr; |
| 1368 | int mac; |
| 1369 | |
developer | 18f46a8 | 2021-07-20 21:08:21 +0800 | [diff] [blame] | 1370 | if (eth->hwlro) |
| 1371 | ring = mtk_get_rx_ring(eth); |
| 1372 | |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1373 | if (unlikely(!ring)) |
| 1374 | goto rx_done; |
| 1375 | |
| 1376 | idx = NEXT_DESP_IDX(ring->calc_idx, ring->dma_size); |
| 1377 | rxd = &ring->dma[idx]; |
| 1378 | data = ring->data[idx]; |
| 1379 | |
developer | c4671b2 | 2021-05-28 13:16:42 +0800 | [diff] [blame] | 1380 | if (!mtk_rx_get_desc(&trxd, rxd)) |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1381 | break; |
| 1382 | |
| 1383 | /* find out which mac the packet come from. values start at 1 */ |
| 1384 | if (MTK_HAS_CAPS(eth->soc->caps, MTK_SOC_MT7628)) { |
| 1385 | mac = 0; |
| 1386 | } else { |
developer | a2bdbd5 | 2021-05-31 19:10:17 +0800 | [diff] [blame] | 1387 | #if defined(CONFIG_MEDIATEK_NETSYS_V2) |
| 1388 | if (MTK_HAS_CAPS(eth->soc->caps, MTK_NETSYS_V2)) |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1389 | mac = RX_DMA_GET_SPORT(trxd.rxd5) - 1; |
| 1390 | else |
| 1391 | #endif |
| 1392 | mac = (trxd.rxd4 & RX_DMA_SPECIAL_TAG) ? |
| 1393 | 0 : RX_DMA_GET_SPORT(trxd.rxd4) - 1; |
| 1394 | } |
| 1395 | |
| 1396 | if (unlikely(mac < 0 || mac >= MTK_MAC_COUNT || |
| 1397 | !eth->netdev[mac])) |
| 1398 | goto release_desc; |
| 1399 | |
| 1400 | netdev = eth->netdev[mac]; |
| 1401 | |
| 1402 | if (unlikely(test_bit(MTK_RESETTING, ð->state))) |
| 1403 | goto release_desc; |
| 1404 | |
| 1405 | /* alloc new buffer */ |
| 1406 | new_data = napi_alloc_frag(ring->frag_size); |
| 1407 | if (unlikely(!new_data)) { |
| 1408 | netdev->stats.rx_dropped++; |
| 1409 | goto release_desc; |
| 1410 | } |
| 1411 | dma_addr = dma_map_single(eth->dev, |
| 1412 | new_data + NET_SKB_PAD + |
| 1413 | eth->ip_align, |
| 1414 | ring->buf_size, |
| 1415 | DMA_FROM_DEVICE); |
| 1416 | if (unlikely(dma_mapping_error(eth->dev, dma_addr))) { |
| 1417 | skb_free_frag(new_data); |
| 1418 | netdev->stats.rx_dropped++; |
| 1419 | goto release_desc; |
| 1420 | } |
| 1421 | |
developer | c4671b2 | 2021-05-28 13:16:42 +0800 | [diff] [blame] | 1422 | dma_unmap_single(eth->dev, trxd.rxd1, |
| 1423 | ring->buf_size, DMA_FROM_DEVICE); |
| 1424 | |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1425 | /* receive data */ |
| 1426 | skb = build_skb(data, ring->frag_size); |
| 1427 | if (unlikely(!skb)) { |
developer | c4671b2 | 2021-05-28 13:16:42 +0800 | [diff] [blame] | 1428 | skb_free_frag(data); |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1429 | netdev->stats.rx_dropped++; |
developer | c4671b2 | 2021-05-28 13:16:42 +0800 | [diff] [blame] | 1430 | goto skip_rx; |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1431 | } |
| 1432 | skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); |
| 1433 | |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1434 | pktlen = RX_DMA_GET_PLEN0(trxd.rxd2); |
| 1435 | skb->dev = netdev; |
| 1436 | skb_put(skb, pktlen); |
| 1437 | |
developer | a2bdbd5 | 2021-05-31 19:10:17 +0800 | [diff] [blame] | 1438 | if ((!MTK_HAS_CAPS(eth->soc->caps, MTK_NETSYS_V2) && |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1439 | (trxd.rxd4 & eth->rx_dma_l4_valid)) || |
developer | a2bdbd5 | 2021-05-31 19:10:17 +0800 | [diff] [blame] | 1440 | (MTK_HAS_CAPS(eth->soc->caps, MTK_NETSYS_V2) && |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1441 | (trxd.rxd3 & eth->rx_dma_l4_valid))) |
| 1442 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 1443 | else |
| 1444 | skb_checksum_none_assert(skb); |
| 1445 | skb->protocol = eth_type_trans(skb, netdev); |
| 1446 | |
| 1447 | if (netdev->features & NETIF_F_HW_VLAN_CTAG_RX) { |
developer | a2bdbd5 | 2021-05-31 19:10:17 +0800 | [diff] [blame] | 1448 | if (MTK_HAS_CAPS(eth->soc->caps, MTK_NETSYS_V2)) { |
developer | 255bba2 | 2021-07-27 15:16:33 +0800 | [diff] [blame] | 1449 | if (trxd.rxd3 & RX_DMA_VTAG_V2) |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1450 | __vlan_hwaccel_put_tag(skb, |
developer | 255bba2 | 2021-07-27 15:16:33 +0800 | [diff] [blame] | 1451 | htons(RX_DMA_VPID_V2(trxd.rxd4)), |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1452 | RX_DMA_VID_V2(trxd.rxd4)); |
| 1453 | } else { |
| 1454 | if (trxd.rxd2 & RX_DMA_VTAG) |
| 1455 | __vlan_hwaccel_put_tag(skb, |
| 1456 | htons(RX_DMA_VPID(trxd.rxd3)), |
| 1457 | RX_DMA_VID(trxd.rxd3)); |
| 1458 | } |
| 1459 | |
| 1460 | /* If netdev is attached to dsa switch, the special |
| 1461 | * tag inserted in VLAN field by switch hardware can |
| 1462 | * be offload by RX HW VLAN offload. Clears the VLAN |
| 1463 | * information from @skb to avoid unexpected 8021d |
| 1464 | * handler before packet enter dsa framework. |
| 1465 | */ |
| 1466 | if (netdev_uses_dsa(netdev)) |
| 1467 | __vlan_hwaccel_clear_tag(skb); |
| 1468 | } |
| 1469 | |
| 1470 | #if defined(CONFIG_NET_MEDIATEK_HNAT) || defined(CONFIG_NET_MEDIATEK_HNAT_MODULE) |
developer | a2bdbd5 | 2021-05-31 19:10:17 +0800 | [diff] [blame] | 1471 | #if defined(CONFIG_MEDIATEK_NETSYS_V2) |
| 1472 | if (MTK_HAS_CAPS(eth->soc->caps, MTK_NETSYS_V2)) |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1473 | *(u32 *)(skb->head) = trxd.rxd5; |
| 1474 | else |
| 1475 | #endif |
| 1476 | *(u32 *)(skb->head) = trxd.rxd4; |
| 1477 | |
| 1478 | skb_hnat_alg(skb) = 0; |
developer | fdfe157 | 2021-09-13 16:56:33 +0800 | [diff] [blame] | 1479 | skb_hnat_filled(skb) = 0; |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1480 | skb_hnat_magic_tag(skb) = HNAT_MAGIC_TAG; |
| 1481 | |
| 1482 | if (skb_hnat_reason(skb) == HIT_BIND_FORCE_TO_CPU) { |
| 1483 | trace_printk("[%s] reason=0x%x(force to CPU) from WAN to Ext\n", |
| 1484 | __func__, skb_hnat_reason(skb)); |
| 1485 | skb->pkt_type = PACKET_HOST; |
| 1486 | } |
| 1487 | |
| 1488 | trace_printk("[%s] rxd:(entry=%x,sport=%x,reason=%x,alg=%x\n", |
| 1489 | __func__, skb_hnat_entry(skb), skb_hnat_sport(skb), |
| 1490 | skb_hnat_reason(skb), skb_hnat_alg(skb)); |
| 1491 | #endif |
developer | 77d03a7 | 2021-06-06 00:06:00 +0800 | [diff] [blame] | 1492 | if (mtk_hwlro_stats_ebl && |
| 1493 | IS_HW_LRO_RING(ring->ring_no) && eth->hwlro) { |
| 1494 | hw_lro_stats_update(ring->ring_no, &trxd); |
| 1495 | hw_lro_flush_stats_update(ring->ring_no, &trxd); |
| 1496 | } |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1497 | |
| 1498 | skb_record_rx_queue(skb, 0); |
| 1499 | napi_gro_receive(napi, skb); |
| 1500 | |
developer | c4671b2 | 2021-05-28 13:16:42 +0800 | [diff] [blame] | 1501 | skip_rx: |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1502 | ring->data[idx] = new_data; |
| 1503 | rxd->rxd1 = (unsigned int)dma_addr; |
| 1504 | |
| 1505 | release_desc: |
| 1506 | if (MTK_HAS_CAPS(eth->soc->caps, MTK_SOC_MT7628)) |
| 1507 | rxd->rxd2 = RX_DMA_LSO; |
| 1508 | else |
| 1509 | rxd->rxd2 = RX_DMA_PLEN0(ring->buf_size); |
| 1510 | |
| 1511 | ring->calc_idx = idx; |
| 1512 | |
| 1513 | done++; |
| 1514 | } |
| 1515 | |
| 1516 | rx_done: |
| 1517 | if (done) { |
| 1518 | /* make sure that all changes to the dma ring are flushed before |
| 1519 | * we continue |
| 1520 | */ |
| 1521 | wmb(); |
developer | 18f46a8 | 2021-07-20 21:08:21 +0800 | [diff] [blame] | 1522 | mtk_update_rx_cpu_idx(eth, ring); |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1523 | } |
| 1524 | |
| 1525 | return done; |
| 1526 | } |
| 1527 | |
developer | fb556ca | 2021-10-13 10:52:09 +0800 | [diff] [blame^] | 1528 | static void mtk_poll_tx_qdma(struct mtk_eth *eth, int budget, |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1529 | unsigned int *done, unsigned int *bytes) |
| 1530 | { |
| 1531 | struct mtk_tx_ring *ring = ð->tx_ring; |
| 1532 | struct mtk_tx_dma *desc; |
| 1533 | struct sk_buff *skb; |
| 1534 | struct mtk_tx_buf *tx_buf; |
| 1535 | u32 cpu, dma; |
| 1536 | |
developer | c4671b2 | 2021-05-28 13:16:42 +0800 | [diff] [blame] | 1537 | cpu = ring->last_free_ptr; |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1538 | dma = mtk_r32(eth, MTK_QTX_DRX_PTR); |
| 1539 | |
| 1540 | desc = mtk_qdma_phys_to_virt(ring, cpu); |
| 1541 | |
| 1542 | while ((cpu != dma) && budget) { |
| 1543 | u32 next_cpu = desc->txd2; |
| 1544 | int mac = 0; |
| 1545 | |
| 1546 | if ((desc->txd3 & TX_DMA_OWNER_CPU) == 0) |
| 1547 | break; |
| 1548 | |
| 1549 | desc = mtk_qdma_phys_to_virt(ring, desc->txd2); |
| 1550 | |
| 1551 | tx_buf = mtk_desc_to_tx_buf(ring, desc); |
| 1552 | if (tx_buf->flags & MTK_TX_FLAGS_FPORT1) |
| 1553 | mac = 1; |
| 1554 | |
| 1555 | skb = tx_buf->skb; |
| 1556 | if (!skb) |
| 1557 | break; |
| 1558 | |
| 1559 | if (skb != (struct sk_buff *)MTK_DMA_DUMMY_DESC) { |
| 1560 | bytes[mac] += skb->len; |
| 1561 | done[mac]++; |
| 1562 | budget--; |
| 1563 | } |
developer | c4671b2 | 2021-05-28 13:16:42 +0800 | [diff] [blame] | 1564 | mtk_tx_unmap(eth, tx_buf, true); |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1565 | |
| 1566 | ring->last_free = desc; |
| 1567 | atomic_inc(&ring->free_count); |
| 1568 | |
| 1569 | cpu = next_cpu; |
| 1570 | } |
| 1571 | |
developer | c4671b2 | 2021-05-28 13:16:42 +0800 | [diff] [blame] | 1572 | ring->last_free_ptr = cpu; |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1573 | mtk_w32(eth, cpu, MTK_QTX_CRX_PTR); |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1574 | } |
| 1575 | |
developer | fb556ca | 2021-10-13 10:52:09 +0800 | [diff] [blame^] | 1576 | static void mtk_poll_tx_pdma(struct mtk_eth *eth, int budget, |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1577 | unsigned int *done, unsigned int *bytes) |
| 1578 | { |
| 1579 | struct mtk_tx_ring *ring = ð->tx_ring; |
| 1580 | struct mtk_tx_dma *desc; |
| 1581 | struct sk_buff *skb; |
| 1582 | struct mtk_tx_buf *tx_buf; |
| 1583 | u32 cpu, dma; |
| 1584 | |
| 1585 | cpu = ring->cpu_idx; |
| 1586 | dma = mtk_r32(eth, MT7628_TX_DTX_IDX0); |
| 1587 | |
| 1588 | while ((cpu != dma) && budget) { |
| 1589 | tx_buf = &ring->buf[cpu]; |
| 1590 | skb = tx_buf->skb; |
| 1591 | if (!skb) |
| 1592 | break; |
| 1593 | |
| 1594 | if (skb != (struct sk_buff *)MTK_DMA_DUMMY_DESC) { |
| 1595 | bytes[0] += skb->len; |
| 1596 | done[0]++; |
| 1597 | budget--; |
| 1598 | } |
| 1599 | |
developer | c4671b2 | 2021-05-28 13:16:42 +0800 | [diff] [blame] | 1600 | mtk_tx_unmap(eth, tx_buf, true); |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1601 | |
| 1602 | desc = &ring->dma[cpu]; |
| 1603 | ring->last_free = desc; |
| 1604 | atomic_inc(&ring->free_count); |
| 1605 | |
| 1606 | cpu = NEXT_DESP_IDX(cpu, ring->dma_size); |
| 1607 | } |
| 1608 | |
| 1609 | ring->cpu_idx = cpu; |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1610 | } |
| 1611 | |
| 1612 | static int mtk_poll_tx(struct mtk_eth *eth, int budget) |
| 1613 | { |
| 1614 | struct mtk_tx_ring *ring = ð->tx_ring; |
| 1615 | unsigned int done[MTK_MAX_DEVS]; |
| 1616 | unsigned int bytes[MTK_MAX_DEVS]; |
| 1617 | int total = 0, i; |
| 1618 | |
| 1619 | memset(done, 0, sizeof(done)); |
| 1620 | memset(bytes, 0, sizeof(bytes)); |
| 1621 | |
| 1622 | if (MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA)) |
developer | fb556ca | 2021-10-13 10:52:09 +0800 | [diff] [blame^] | 1623 | mtk_poll_tx_qdma(eth, budget, done, bytes); |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1624 | else |
developer | fb556ca | 2021-10-13 10:52:09 +0800 | [diff] [blame^] | 1625 | mtk_poll_tx_pdma(eth, budget, done, bytes); |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1626 | |
| 1627 | for (i = 0; i < MTK_MAC_COUNT; i++) { |
| 1628 | if (!eth->netdev[i] || !done[i]) |
| 1629 | continue; |
| 1630 | netdev_completed_queue(eth->netdev[i], done[i], bytes[i]); |
| 1631 | total += done[i]; |
| 1632 | } |
| 1633 | |
| 1634 | if (mtk_queue_stopped(eth) && |
| 1635 | (atomic_read(&ring->free_count) > ring->thresh)) |
| 1636 | mtk_wake_queue(eth); |
| 1637 | |
| 1638 | return total; |
| 1639 | } |
| 1640 | |
| 1641 | static void mtk_handle_status_irq(struct mtk_eth *eth) |
| 1642 | { |
developer | 77f3fd4 | 2021-10-05 15:16:05 +0800 | [diff] [blame] | 1643 | u32 status2 = mtk_r32(eth, MTK_INT_STATUS); |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1644 | |
| 1645 | if (unlikely(status2 & (MTK_GDM1_AF | MTK_GDM2_AF))) { |
| 1646 | mtk_stats_update(eth); |
| 1647 | mtk_w32(eth, (MTK_GDM1_AF | MTK_GDM2_AF), |
developer | 77f3fd4 | 2021-10-05 15:16:05 +0800 | [diff] [blame] | 1648 | MTK_INT_STATUS); |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1649 | } |
| 1650 | } |
| 1651 | |
| 1652 | static int mtk_napi_tx(struct napi_struct *napi, int budget) |
| 1653 | { |
| 1654 | struct mtk_eth *eth = container_of(napi, struct mtk_eth, tx_napi); |
| 1655 | u32 status, mask; |
| 1656 | int tx_done = 0; |
| 1657 | |
| 1658 | if (MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA)) |
| 1659 | mtk_handle_status_irq(eth); |
| 1660 | mtk_w32(eth, MTK_TX_DONE_INT, eth->tx_int_status_reg); |
| 1661 | tx_done = mtk_poll_tx(eth, budget); |
| 1662 | |
| 1663 | if (unlikely(netif_msg_intr(eth))) { |
| 1664 | status = mtk_r32(eth, eth->tx_int_status_reg); |
| 1665 | mask = mtk_r32(eth, eth->tx_int_mask_reg); |
| 1666 | dev_info(eth->dev, |
| 1667 | "done tx %d, intr 0x%08x/0x%x\n", |
| 1668 | tx_done, status, mask); |
| 1669 | } |
| 1670 | |
| 1671 | if (tx_done == budget) |
| 1672 | return budget; |
| 1673 | |
| 1674 | status = mtk_r32(eth, eth->tx_int_status_reg); |
| 1675 | if (status & MTK_TX_DONE_INT) |
| 1676 | return budget; |
| 1677 | |
developer | c4671b2 | 2021-05-28 13:16:42 +0800 | [diff] [blame] | 1678 | if (napi_complete(napi)) |
| 1679 | mtk_tx_irq_enable(eth, MTK_TX_DONE_INT); |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1680 | |
| 1681 | return tx_done; |
| 1682 | } |
| 1683 | |
| 1684 | static int mtk_napi_rx(struct napi_struct *napi, int budget) |
| 1685 | { |
developer | 18f46a8 | 2021-07-20 21:08:21 +0800 | [diff] [blame] | 1686 | struct mtk_napi *rx_napi = container_of(napi, struct mtk_napi, napi); |
| 1687 | struct mtk_eth *eth = rx_napi->eth; |
| 1688 | struct mtk_rx_ring *ring = rx_napi->rx_ring; |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1689 | u32 status, mask; |
| 1690 | int rx_done = 0; |
| 1691 | int remain_budget = budget; |
| 1692 | |
| 1693 | mtk_handle_status_irq(eth); |
| 1694 | |
| 1695 | poll_again: |
developer | 18f46a8 | 2021-07-20 21:08:21 +0800 | [diff] [blame] | 1696 | mtk_w32(eth, MTK_RX_DONE_INT(ring->ring_no), MTK_PDMA_INT_STATUS); |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1697 | rx_done = mtk_poll_rx(napi, remain_budget, eth); |
| 1698 | |
| 1699 | if (unlikely(netif_msg_intr(eth))) { |
| 1700 | status = mtk_r32(eth, MTK_PDMA_INT_STATUS); |
| 1701 | mask = mtk_r32(eth, MTK_PDMA_INT_MASK); |
| 1702 | dev_info(eth->dev, |
| 1703 | "done rx %d, intr 0x%08x/0x%x\n", |
| 1704 | rx_done, status, mask); |
| 1705 | } |
| 1706 | if (rx_done == remain_budget) |
| 1707 | return budget; |
| 1708 | |
| 1709 | status = mtk_r32(eth, MTK_PDMA_INT_STATUS); |
developer | 18f46a8 | 2021-07-20 21:08:21 +0800 | [diff] [blame] | 1710 | if (status & MTK_RX_DONE_INT(ring->ring_no)) { |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1711 | remain_budget -= rx_done; |
| 1712 | goto poll_again; |
| 1713 | } |
developer | c4671b2 | 2021-05-28 13:16:42 +0800 | [diff] [blame] | 1714 | |
| 1715 | if (napi_complete(napi)) |
developer | 18f46a8 | 2021-07-20 21:08:21 +0800 | [diff] [blame] | 1716 | mtk_rx_irq_enable(eth, MTK_RX_DONE_INT(ring->ring_no)); |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1717 | |
| 1718 | return rx_done + budget - remain_budget; |
| 1719 | } |
| 1720 | |
| 1721 | static int mtk_tx_alloc(struct mtk_eth *eth) |
| 1722 | { |
| 1723 | struct mtk_tx_ring *ring = ð->tx_ring; |
| 1724 | int i, sz = sizeof(*ring->dma); |
| 1725 | |
| 1726 | ring->buf = kcalloc(MTK_DMA_SIZE, sizeof(*ring->buf), |
| 1727 | GFP_KERNEL); |
| 1728 | if (!ring->buf) |
| 1729 | goto no_tx_mem; |
| 1730 | |
| 1731 | if (!eth->soc->has_sram) |
| 1732 | ring->dma = dma_alloc_coherent(eth->dev, MTK_DMA_SIZE * sz, |
| 1733 | &ring->phys, GFP_ATOMIC); |
| 1734 | else { |
| 1735 | ring->dma = eth->scratch_ring + MTK_DMA_SIZE; |
| 1736 | ring->phys = eth->phy_scratch_ring + MTK_DMA_SIZE * sz; |
| 1737 | } |
| 1738 | |
| 1739 | if (!ring->dma) |
| 1740 | goto no_tx_mem; |
| 1741 | |
| 1742 | for (i = 0; i < MTK_DMA_SIZE; i++) { |
| 1743 | int next = (i + 1) % MTK_DMA_SIZE; |
| 1744 | u32 next_ptr = ring->phys + next * sz; |
| 1745 | |
| 1746 | ring->dma[i].txd2 = next_ptr; |
| 1747 | ring->dma[i].txd3 = TX_DMA_LS0 | TX_DMA_OWNER_CPU; |
| 1748 | ring->dma[i].txd4 = 0; |
| 1749 | #if defined(CONFIG_MEDIATEK_NETSYS_V2) |
| 1750 | if (eth->soc->has_sram && ( sz > 16)) { |
| 1751 | ring->dma[i].txd5 = 0; |
| 1752 | ring->dma[i].txd6 = 0; |
| 1753 | ring->dma[i].txd7 = 0; |
| 1754 | ring->dma[i].txd8 = 0; |
| 1755 | } |
| 1756 | #endif |
| 1757 | } |
| 1758 | |
| 1759 | /* On MT7688 (PDMA only) this driver uses the ring->dma structs |
| 1760 | * only as the framework. The real HW descriptors are the PDMA |
| 1761 | * descriptors in ring->dma_pdma. |
| 1762 | */ |
| 1763 | if (!MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA)) { |
| 1764 | ring->dma_pdma = dma_alloc_coherent(eth->dev, MTK_DMA_SIZE * sz, |
| 1765 | &ring->phys_pdma, |
| 1766 | GFP_ATOMIC); |
| 1767 | if (!ring->dma_pdma) |
| 1768 | goto no_tx_mem; |
| 1769 | |
| 1770 | for (i = 0; i < MTK_DMA_SIZE; i++) { |
| 1771 | ring->dma_pdma[i].txd2 = TX_DMA_DESP2_DEF; |
| 1772 | ring->dma_pdma[i].txd4 = 0; |
| 1773 | } |
| 1774 | } |
| 1775 | |
| 1776 | ring->dma_size = MTK_DMA_SIZE; |
| 1777 | atomic_set(&ring->free_count, MTK_DMA_SIZE - 2); |
| 1778 | ring->next_free = &ring->dma[0]; |
| 1779 | ring->last_free = &ring->dma[MTK_DMA_SIZE - 1]; |
developer | c4671b2 | 2021-05-28 13:16:42 +0800 | [diff] [blame] | 1780 | ring->last_free_ptr = (u32)(ring->phys + ((MTK_DMA_SIZE - 1) * sz)); |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1781 | ring->thresh = MAX_SKB_FRAGS; |
| 1782 | |
| 1783 | /* make sure that all changes to the dma ring are flushed before we |
| 1784 | * continue |
| 1785 | */ |
| 1786 | wmb(); |
| 1787 | |
| 1788 | if (MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA)) { |
| 1789 | mtk_w32(eth, ring->phys, MTK_QTX_CTX_PTR); |
| 1790 | mtk_w32(eth, ring->phys, MTK_QTX_DTX_PTR); |
| 1791 | mtk_w32(eth, |
| 1792 | ring->phys + ((MTK_DMA_SIZE - 1) * sz), |
| 1793 | MTK_QTX_CRX_PTR); |
developer | c4671b2 | 2021-05-28 13:16:42 +0800 | [diff] [blame] | 1794 | mtk_w32(eth, ring->last_free_ptr, MTK_QTX_DRX_PTR); |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1795 | mtk_w32(eth, (QDMA_RES_THRES << 8) | QDMA_RES_THRES, |
| 1796 | MTK_QTX_CFG(0)); |
| 1797 | } else { |
| 1798 | mtk_w32(eth, ring->phys_pdma, MT7628_TX_BASE_PTR0); |
| 1799 | mtk_w32(eth, MTK_DMA_SIZE, MT7628_TX_MAX_CNT0); |
| 1800 | mtk_w32(eth, 0, MT7628_TX_CTX_IDX0); |
| 1801 | mtk_w32(eth, MT7628_PST_DTX_IDX0, MTK_PDMA_RST_IDX); |
| 1802 | } |
| 1803 | |
| 1804 | return 0; |
| 1805 | |
| 1806 | no_tx_mem: |
| 1807 | return -ENOMEM; |
| 1808 | } |
| 1809 | |
| 1810 | static void mtk_tx_clean(struct mtk_eth *eth) |
| 1811 | { |
| 1812 | struct mtk_tx_ring *ring = ð->tx_ring; |
| 1813 | int i; |
| 1814 | |
| 1815 | if (ring->buf) { |
| 1816 | for (i = 0; i < MTK_DMA_SIZE; i++) |
developer | c4671b2 | 2021-05-28 13:16:42 +0800 | [diff] [blame] | 1817 | mtk_tx_unmap(eth, &ring->buf[i], false); |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1818 | kfree(ring->buf); |
| 1819 | ring->buf = NULL; |
| 1820 | } |
| 1821 | |
| 1822 | if (!eth->soc->has_sram && ring->dma) { |
| 1823 | dma_free_coherent(eth->dev, |
| 1824 | MTK_DMA_SIZE * sizeof(*ring->dma), |
| 1825 | ring->dma, |
| 1826 | ring->phys); |
| 1827 | ring->dma = NULL; |
| 1828 | } |
| 1829 | |
| 1830 | if (ring->dma_pdma) { |
| 1831 | dma_free_coherent(eth->dev, |
| 1832 | MTK_DMA_SIZE * sizeof(*ring->dma_pdma), |
| 1833 | ring->dma_pdma, |
| 1834 | ring->phys_pdma); |
| 1835 | ring->dma_pdma = NULL; |
| 1836 | } |
| 1837 | } |
| 1838 | |
| 1839 | static int mtk_rx_alloc(struct mtk_eth *eth, int ring_no, int rx_flag) |
| 1840 | { |
| 1841 | struct mtk_rx_ring *ring; |
| 1842 | int rx_data_len, rx_dma_size; |
| 1843 | int i; |
| 1844 | |
| 1845 | if (rx_flag == MTK_RX_FLAGS_QDMA) { |
| 1846 | if (ring_no) |
| 1847 | return -EINVAL; |
| 1848 | ring = ð->rx_ring_qdma; |
| 1849 | } else { |
| 1850 | ring = ð->rx_ring[ring_no]; |
| 1851 | } |
| 1852 | |
| 1853 | if (rx_flag == MTK_RX_FLAGS_HWLRO) { |
| 1854 | rx_data_len = MTK_MAX_LRO_RX_LENGTH; |
| 1855 | rx_dma_size = MTK_HW_LRO_DMA_SIZE; |
| 1856 | } else { |
| 1857 | rx_data_len = ETH_DATA_LEN; |
| 1858 | rx_dma_size = MTK_DMA_SIZE; |
| 1859 | } |
| 1860 | |
| 1861 | ring->frag_size = mtk_max_frag_size(rx_data_len); |
| 1862 | ring->buf_size = mtk_max_buf_size(ring->frag_size); |
| 1863 | ring->data = kcalloc(rx_dma_size, sizeof(*ring->data), |
| 1864 | GFP_KERNEL); |
| 1865 | if (!ring->data) |
| 1866 | return -ENOMEM; |
| 1867 | |
| 1868 | for (i = 0; i < rx_dma_size; i++) { |
| 1869 | ring->data[i] = netdev_alloc_frag(ring->frag_size); |
| 1870 | if (!ring->data[i]) |
| 1871 | return -ENOMEM; |
| 1872 | } |
| 1873 | |
| 1874 | if ((!eth->soc->has_sram) || (eth->soc->has_sram |
| 1875 | && (rx_flag != MTK_RX_FLAGS_NORMAL))) |
| 1876 | ring->dma = dma_alloc_coherent(eth->dev, |
| 1877 | rx_dma_size * sizeof(*ring->dma), |
| 1878 | &ring->phys, GFP_ATOMIC); |
| 1879 | else { |
| 1880 | struct mtk_tx_ring *tx_ring = ð->tx_ring; |
developer | 18f46a8 | 2021-07-20 21:08:21 +0800 | [diff] [blame] | 1881 | ring->dma = (struct mtk_rx_dma *)(tx_ring->dma + |
| 1882 | MTK_DMA_SIZE * (ring_no + 1)); |
| 1883 | ring->phys = tx_ring->phys + MTK_DMA_SIZE * |
| 1884 | sizeof(*tx_ring->dma) * (ring_no + 1); |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1885 | } |
| 1886 | |
| 1887 | if (!ring->dma) |
| 1888 | return -ENOMEM; |
| 1889 | |
| 1890 | for (i = 0; i < rx_dma_size; i++) { |
| 1891 | dma_addr_t dma_addr = dma_map_single(eth->dev, |
| 1892 | ring->data[i] + NET_SKB_PAD + eth->ip_align, |
| 1893 | ring->buf_size, |
| 1894 | DMA_FROM_DEVICE); |
| 1895 | if (unlikely(dma_mapping_error(eth->dev, dma_addr))) |
| 1896 | return -ENOMEM; |
| 1897 | ring->dma[i].rxd1 = (unsigned int)dma_addr; |
| 1898 | |
| 1899 | if (MTK_HAS_CAPS(eth->soc->caps, MTK_SOC_MT7628)) |
| 1900 | ring->dma[i].rxd2 = RX_DMA_LSO; |
| 1901 | else |
| 1902 | ring->dma[i].rxd2 = RX_DMA_PLEN0(ring->buf_size); |
| 1903 | |
| 1904 | ring->dma[i].rxd3 = 0; |
| 1905 | ring->dma[i].rxd4 = 0; |
developer | a2bdbd5 | 2021-05-31 19:10:17 +0800 | [diff] [blame] | 1906 | #if defined(CONFIG_MEDIATEK_NETSYS_V2) |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1907 | if (eth->soc->has_sram && ((sizeof(struct mtk_rx_dma)) > 16)) { |
| 1908 | ring->dma[i].rxd5 = 0; |
| 1909 | ring->dma[i].rxd6 = 0; |
| 1910 | ring->dma[i].rxd7 = 0; |
| 1911 | ring->dma[i].rxd8 = 0; |
| 1912 | } |
| 1913 | #endif |
| 1914 | } |
| 1915 | ring->dma_size = rx_dma_size; |
| 1916 | ring->calc_idx_update = false; |
| 1917 | ring->calc_idx = rx_dma_size - 1; |
| 1918 | ring->crx_idx_reg = (rx_flag == MTK_RX_FLAGS_QDMA) ? |
| 1919 | MTK_QRX_CRX_IDX_CFG(ring_no) : |
| 1920 | MTK_PRX_CRX_IDX_CFG(ring_no); |
developer | 77d03a7 | 2021-06-06 00:06:00 +0800 | [diff] [blame] | 1921 | ring->ring_no = ring_no; |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1922 | /* make sure that all changes to the dma ring are flushed before we |
| 1923 | * continue |
| 1924 | */ |
| 1925 | wmb(); |
| 1926 | |
| 1927 | if (rx_flag == MTK_RX_FLAGS_QDMA) { |
| 1928 | mtk_w32(eth, ring->phys, MTK_QRX_BASE_PTR_CFG(ring_no)); |
| 1929 | mtk_w32(eth, rx_dma_size, MTK_QRX_MAX_CNT_CFG(ring_no)); |
| 1930 | mtk_w32(eth, ring->calc_idx, ring->crx_idx_reg); |
| 1931 | mtk_w32(eth, MTK_PST_DRX_IDX_CFG(ring_no), MTK_QDMA_RST_IDX); |
| 1932 | } else { |
| 1933 | mtk_w32(eth, ring->phys, MTK_PRX_BASE_PTR_CFG(ring_no)); |
| 1934 | mtk_w32(eth, rx_dma_size, MTK_PRX_MAX_CNT_CFG(ring_no)); |
| 1935 | mtk_w32(eth, ring->calc_idx, ring->crx_idx_reg); |
| 1936 | mtk_w32(eth, MTK_PST_DRX_IDX_CFG(ring_no), MTK_PDMA_RST_IDX); |
| 1937 | } |
| 1938 | |
| 1939 | return 0; |
| 1940 | } |
| 1941 | |
| 1942 | static void mtk_rx_clean(struct mtk_eth *eth, struct mtk_rx_ring *ring, int in_sram) |
| 1943 | { |
| 1944 | int i; |
| 1945 | |
| 1946 | if (ring->data && ring->dma) { |
| 1947 | for (i = 0; i < ring->dma_size; i++) { |
| 1948 | if (!ring->data[i]) |
| 1949 | continue; |
| 1950 | if (!ring->dma[i].rxd1) |
| 1951 | continue; |
| 1952 | dma_unmap_single(eth->dev, |
| 1953 | ring->dma[i].rxd1, |
| 1954 | ring->buf_size, |
| 1955 | DMA_FROM_DEVICE); |
| 1956 | skb_free_frag(ring->data[i]); |
| 1957 | } |
| 1958 | kfree(ring->data); |
| 1959 | ring->data = NULL; |
| 1960 | } |
| 1961 | |
| 1962 | if(in_sram) |
| 1963 | return; |
| 1964 | |
| 1965 | if (ring->dma) { |
| 1966 | dma_free_coherent(eth->dev, |
| 1967 | ring->dma_size * sizeof(*ring->dma), |
| 1968 | ring->dma, |
| 1969 | ring->phys); |
| 1970 | ring->dma = NULL; |
| 1971 | } |
| 1972 | } |
| 1973 | |
| 1974 | static int mtk_hwlro_rx_init(struct mtk_eth *eth) |
| 1975 | { |
| 1976 | int i; |
developer | 77d03a7 | 2021-06-06 00:06:00 +0800 | [diff] [blame] | 1977 | u32 val; |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1978 | u32 ring_ctrl_dw1 = 0, ring_ctrl_dw2 = 0, ring_ctrl_dw3 = 0; |
| 1979 | u32 lro_ctrl_dw0 = 0, lro_ctrl_dw3 = 0; |
| 1980 | |
| 1981 | /* set LRO rings to auto-learn modes */ |
| 1982 | ring_ctrl_dw2 |= MTK_RING_AUTO_LERAN_MODE; |
| 1983 | |
| 1984 | /* validate LRO ring */ |
| 1985 | ring_ctrl_dw2 |= MTK_RING_VLD; |
| 1986 | |
| 1987 | /* set AGE timer (unit: 20us) */ |
| 1988 | ring_ctrl_dw2 |= MTK_RING_AGE_TIME_H; |
| 1989 | ring_ctrl_dw1 |= MTK_RING_AGE_TIME_L; |
| 1990 | |
| 1991 | /* set max AGG timer (unit: 20us) */ |
| 1992 | ring_ctrl_dw2 |= MTK_RING_MAX_AGG_TIME; |
| 1993 | |
| 1994 | /* set max LRO AGG count */ |
| 1995 | ring_ctrl_dw2 |= MTK_RING_MAX_AGG_CNT_L; |
| 1996 | ring_ctrl_dw3 |= MTK_RING_MAX_AGG_CNT_H; |
| 1997 | |
developer | 77d03a7 | 2021-06-06 00:06:00 +0800 | [diff] [blame] | 1998 | for (i = 1; i <= MTK_HW_LRO_RING_NUM; i++) { |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 1999 | mtk_w32(eth, ring_ctrl_dw1, MTK_LRO_CTRL_DW1_CFG(i)); |
| 2000 | mtk_w32(eth, ring_ctrl_dw2, MTK_LRO_CTRL_DW2_CFG(i)); |
| 2001 | mtk_w32(eth, ring_ctrl_dw3, MTK_LRO_CTRL_DW3_CFG(i)); |
| 2002 | } |
| 2003 | |
| 2004 | /* IPv4 checksum update enable */ |
| 2005 | lro_ctrl_dw0 |= MTK_L3_CKS_UPD_EN; |
| 2006 | |
| 2007 | /* switch priority comparison to packet count mode */ |
| 2008 | lro_ctrl_dw0 |= MTK_LRO_ALT_PKT_CNT_MODE; |
| 2009 | |
| 2010 | /* bandwidth threshold setting */ |
| 2011 | mtk_w32(eth, MTK_HW_LRO_BW_THRE, MTK_PDMA_LRO_CTRL_DW2); |
| 2012 | |
| 2013 | /* auto-learn score delta setting */ |
developer | 77d03a7 | 2021-06-06 00:06:00 +0800 | [diff] [blame] | 2014 | mtk_w32(eth, MTK_HW_LRO_REPLACE_DELTA, MTK_LRO_ALT_SCORE_DELTA); |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 2015 | |
| 2016 | /* set refresh timer for altering flows to 1 sec. (unit: 20us) */ |
| 2017 | mtk_w32(eth, (MTK_HW_LRO_TIMER_UNIT << 16) | MTK_HW_LRO_REFRESH_TIME, |
| 2018 | MTK_PDMA_LRO_ALT_REFRESH_TIMER); |
| 2019 | |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 2020 | /* the minimal remaining room of SDL0 in RXD for lro aggregation */ |
| 2021 | lro_ctrl_dw3 |= MTK_LRO_MIN_RXD_SDL; |
| 2022 | |
developer | 77d03a7 | 2021-06-06 00:06:00 +0800 | [diff] [blame] | 2023 | if (MTK_HAS_CAPS(eth->soc->caps, MTK_NETSYS_V2)) { |
| 2024 | val = mtk_r32(eth, MTK_PDMA_RX_CFG); |
| 2025 | mtk_w32(eth, val | (MTK_PDMA_LRO_SDL << MTK_RX_CFG_SDL_OFFSET), |
| 2026 | MTK_PDMA_RX_CFG); |
| 2027 | |
| 2028 | lro_ctrl_dw0 |= MTK_PDMA_LRO_SDL << MTK_CTRL_DW0_SDL_OFFSET; |
| 2029 | } else { |
| 2030 | /* set HW LRO mode & the max aggregation count for rx packets */ |
| 2031 | lro_ctrl_dw3 |= MTK_ADMA_MODE | (MTK_HW_LRO_MAX_AGG_CNT & 0xff); |
| 2032 | } |
| 2033 | |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 2034 | /* enable HW LRO */ |
| 2035 | lro_ctrl_dw0 |= MTK_LRO_EN; |
| 2036 | |
developer | 77d03a7 | 2021-06-06 00:06:00 +0800 | [diff] [blame] | 2037 | /* enable cpu reason black list */ |
| 2038 | lro_ctrl_dw0 |= MTK_LRO_CRSN_BNW; |
| 2039 | |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 2040 | mtk_w32(eth, lro_ctrl_dw3, MTK_PDMA_LRO_CTRL_DW3); |
| 2041 | mtk_w32(eth, lro_ctrl_dw0, MTK_PDMA_LRO_CTRL_DW0); |
| 2042 | |
developer | 77d03a7 | 2021-06-06 00:06:00 +0800 | [diff] [blame] | 2043 | /* no use PPE cpu reason */ |
| 2044 | mtk_w32(eth, 0xffffffff, MTK_PDMA_LRO_CTRL_DW1); |
| 2045 | |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 2046 | return 0; |
| 2047 | } |
| 2048 | |
| 2049 | static void mtk_hwlro_rx_uninit(struct mtk_eth *eth) |
| 2050 | { |
| 2051 | int i; |
| 2052 | u32 val; |
| 2053 | |
| 2054 | /* relinquish lro rings, flush aggregated packets */ |
developer | 77d03a7 | 2021-06-06 00:06:00 +0800 | [diff] [blame] | 2055 | mtk_w32(eth, MTK_LRO_RING_RELINGUISH_REQ, MTK_PDMA_LRO_CTRL_DW0); |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 2056 | |
| 2057 | /* wait for relinquishments done */ |
| 2058 | for (i = 0; i < 10; i++) { |
| 2059 | val = mtk_r32(eth, MTK_PDMA_LRO_CTRL_DW0); |
developer | 77d03a7 | 2021-06-06 00:06:00 +0800 | [diff] [blame] | 2060 | if (val & MTK_LRO_RING_RELINGUISH_DONE) { |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 2061 | msleep(20); |
| 2062 | continue; |
| 2063 | } |
| 2064 | break; |
| 2065 | } |
| 2066 | |
| 2067 | /* invalidate lro rings */ |
developer | 77d03a7 | 2021-06-06 00:06:00 +0800 | [diff] [blame] | 2068 | for (i = 1; i <= MTK_HW_LRO_RING_NUM; i++) |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 2069 | mtk_w32(eth, 0, MTK_LRO_CTRL_DW2_CFG(i)); |
| 2070 | |
| 2071 | /* disable HW LRO */ |
| 2072 | mtk_w32(eth, 0, MTK_PDMA_LRO_CTRL_DW0); |
| 2073 | } |
| 2074 | |
| 2075 | static void mtk_hwlro_val_ipaddr(struct mtk_eth *eth, int idx, __be32 ip) |
| 2076 | { |
| 2077 | u32 reg_val; |
| 2078 | |
developer | 77d03a7 | 2021-06-06 00:06:00 +0800 | [diff] [blame] | 2079 | if (MTK_HAS_CAPS(eth->soc->caps, MTK_NETSYS_V2)) |
| 2080 | idx += 1; |
| 2081 | |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 2082 | reg_val = mtk_r32(eth, MTK_LRO_CTRL_DW2_CFG(idx)); |
| 2083 | |
| 2084 | /* invalidate the IP setting */ |
| 2085 | mtk_w32(eth, (reg_val & ~MTK_RING_MYIP_VLD), MTK_LRO_CTRL_DW2_CFG(idx)); |
| 2086 | |
| 2087 | mtk_w32(eth, ip, MTK_LRO_DIP_DW0_CFG(idx)); |
| 2088 | |
| 2089 | /* validate the IP setting */ |
| 2090 | mtk_w32(eth, (reg_val | MTK_RING_MYIP_VLD), MTK_LRO_CTRL_DW2_CFG(idx)); |
| 2091 | } |
| 2092 | |
| 2093 | static void mtk_hwlro_inval_ipaddr(struct mtk_eth *eth, int idx) |
| 2094 | { |
| 2095 | u32 reg_val; |
| 2096 | |
developer | 77d03a7 | 2021-06-06 00:06:00 +0800 | [diff] [blame] | 2097 | if (MTK_HAS_CAPS(eth->soc->caps, MTK_NETSYS_V2)) |
| 2098 | idx += 1; |
| 2099 | |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 2100 | reg_val = mtk_r32(eth, MTK_LRO_CTRL_DW2_CFG(idx)); |
| 2101 | |
| 2102 | /* invalidate the IP setting */ |
| 2103 | mtk_w32(eth, (reg_val & ~MTK_RING_MYIP_VLD), MTK_LRO_CTRL_DW2_CFG(idx)); |
| 2104 | |
| 2105 | mtk_w32(eth, 0, MTK_LRO_DIP_DW0_CFG(idx)); |
| 2106 | } |
| 2107 | |
| 2108 | static int mtk_hwlro_get_ip_cnt(struct mtk_mac *mac) |
| 2109 | { |
| 2110 | int cnt = 0; |
| 2111 | int i; |
| 2112 | |
| 2113 | for (i = 0; i < MTK_MAX_LRO_IP_CNT; i++) { |
| 2114 | if (mac->hwlro_ip[i]) |
| 2115 | cnt++; |
| 2116 | } |
| 2117 | |
| 2118 | return cnt; |
| 2119 | } |
| 2120 | |
| 2121 | static int mtk_hwlro_add_ipaddr(struct net_device *dev, |
| 2122 | struct ethtool_rxnfc *cmd) |
| 2123 | { |
| 2124 | struct ethtool_rx_flow_spec *fsp = |
| 2125 | (struct ethtool_rx_flow_spec *)&cmd->fs; |
| 2126 | struct mtk_mac *mac = netdev_priv(dev); |
| 2127 | struct mtk_eth *eth = mac->hw; |
| 2128 | int hwlro_idx; |
| 2129 | |
| 2130 | if ((fsp->flow_type != TCP_V4_FLOW) || |
| 2131 | (!fsp->h_u.tcp_ip4_spec.ip4dst) || |
| 2132 | (fsp->location > 1)) |
| 2133 | return -EINVAL; |
| 2134 | |
| 2135 | mac->hwlro_ip[fsp->location] = htonl(fsp->h_u.tcp_ip4_spec.ip4dst); |
| 2136 | hwlro_idx = (mac->id * MTK_MAX_LRO_IP_CNT) + fsp->location; |
| 2137 | |
| 2138 | mac->hwlro_ip_cnt = mtk_hwlro_get_ip_cnt(mac); |
| 2139 | |
| 2140 | mtk_hwlro_val_ipaddr(eth, hwlro_idx, mac->hwlro_ip[fsp->location]); |
| 2141 | |
| 2142 | return 0; |
| 2143 | } |
| 2144 | |
| 2145 | static int mtk_hwlro_del_ipaddr(struct net_device *dev, |
| 2146 | struct ethtool_rxnfc *cmd) |
| 2147 | { |
| 2148 | struct ethtool_rx_flow_spec *fsp = |
| 2149 | (struct ethtool_rx_flow_spec *)&cmd->fs; |
| 2150 | struct mtk_mac *mac = netdev_priv(dev); |
| 2151 | struct mtk_eth *eth = mac->hw; |
| 2152 | int hwlro_idx; |
| 2153 | |
| 2154 | if (fsp->location > 1) |
| 2155 | return -EINVAL; |
| 2156 | |
| 2157 | mac->hwlro_ip[fsp->location] = 0; |
| 2158 | hwlro_idx = (mac->id * MTK_MAX_LRO_IP_CNT) + fsp->location; |
| 2159 | |
| 2160 | mac->hwlro_ip_cnt = mtk_hwlro_get_ip_cnt(mac); |
| 2161 | |
| 2162 | mtk_hwlro_inval_ipaddr(eth, hwlro_idx); |
| 2163 | |
| 2164 | return 0; |
| 2165 | } |
| 2166 | |
| 2167 | static void mtk_hwlro_netdev_disable(struct net_device *dev) |
| 2168 | { |
| 2169 | struct mtk_mac *mac = netdev_priv(dev); |
| 2170 | struct mtk_eth *eth = mac->hw; |
| 2171 | int i, hwlro_idx; |
| 2172 | |
| 2173 | for (i = 0; i < MTK_MAX_LRO_IP_CNT; i++) { |
| 2174 | mac->hwlro_ip[i] = 0; |
| 2175 | hwlro_idx = (mac->id * MTK_MAX_LRO_IP_CNT) + i; |
| 2176 | |
| 2177 | mtk_hwlro_inval_ipaddr(eth, hwlro_idx); |
| 2178 | } |
| 2179 | |
| 2180 | mac->hwlro_ip_cnt = 0; |
| 2181 | } |
| 2182 | |
| 2183 | static int mtk_hwlro_get_fdir_entry(struct net_device *dev, |
| 2184 | struct ethtool_rxnfc *cmd) |
| 2185 | { |
| 2186 | struct mtk_mac *mac = netdev_priv(dev); |
| 2187 | struct ethtool_rx_flow_spec *fsp = |
| 2188 | (struct ethtool_rx_flow_spec *)&cmd->fs; |
| 2189 | |
| 2190 | /* only tcp dst ipv4 is meaningful, others are meaningless */ |
| 2191 | fsp->flow_type = TCP_V4_FLOW; |
| 2192 | fsp->h_u.tcp_ip4_spec.ip4dst = ntohl(mac->hwlro_ip[fsp->location]); |
| 2193 | fsp->m_u.tcp_ip4_spec.ip4dst = 0; |
| 2194 | |
| 2195 | fsp->h_u.tcp_ip4_spec.ip4src = 0; |
| 2196 | fsp->m_u.tcp_ip4_spec.ip4src = 0xffffffff; |
| 2197 | fsp->h_u.tcp_ip4_spec.psrc = 0; |
| 2198 | fsp->m_u.tcp_ip4_spec.psrc = 0xffff; |
| 2199 | fsp->h_u.tcp_ip4_spec.pdst = 0; |
| 2200 | fsp->m_u.tcp_ip4_spec.pdst = 0xffff; |
| 2201 | fsp->h_u.tcp_ip4_spec.tos = 0; |
| 2202 | fsp->m_u.tcp_ip4_spec.tos = 0xff; |
| 2203 | |
| 2204 | return 0; |
| 2205 | } |
| 2206 | |
| 2207 | static int mtk_hwlro_get_fdir_all(struct net_device *dev, |
| 2208 | struct ethtool_rxnfc *cmd, |
| 2209 | u32 *rule_locs) |
| 2210 | { |
| 2211 | struct mtk_mac *mac = netdev_priv(dev); |
| 2212 | int cnt = 0; |
| 2213 | int i; |
| 2214 | |
| 2215 | for (i = 0; i < MTK_MAX_LRO_IP_CNT; i++) { |
| 2216 | if (mac->hwlro_ip[i]) { |
| 2217 | rule_locs[cnt] = i; |
| 2218 | cnt++; |
| 2219 | } |
| 2220 | } |
| 2221 | |
| 2222 | cmd->rule_cnt = cnt; |
| 2223 | |
| 2224 | return 0; |
| 2225 | } |
| 2226 | |
developer | 18f46a8 | 2021-07-20 21:08:21 +0800 | [diff] [blame] | 2227 | static int mtk_rss_init(struct mtk_eth *eth) |
| 2228 | { |
| 2229 | u32 val; |
| 2230 | |
| 2231 | if (!MTK_HAS_CAPS(eth->soc->caps, MTK_NETSYS_V2)) { |
| 2232 | /* Set RSS rings to PSE modes */ |
| 2233 | val = mtk_r32(eth, MTK_LRO_CTRL_DW2_CFG(1)); |
| 2234 | val |= MTK_RING_PSE_MODE; |
| 2235 | mtk_w32(eth, val, MTK_LRO_CTRL_DW2_CFG(1)); |
| 2236 | |
| 2237 | /* Enable non-lro multiple rx */ |
| 2238 | val = mtk_r32(eth, MTK_PDMA_LRO_CTRL_DW0); |
| 2239 | val |= MTK_NON_LRO_MULTI_EN; |
| 2240 | mtk_w32(eth, val, MTK_PDMA_LRO_CTRL_DW0); |
| 2241 | |
| 2242 | /* Enable RSS dly int supoort */ |
| 2243 | val |= MTK_LRO_DLY_INT_EN; |
| 2244 | mtk_w32(eth, val, MTK_PDMA_LRO_CTRL_DW0); |
| 2245 | |
| 2246 | /* Set RSS delay config int ring1 */ |
| 2247 | mtk_w32(eth, MTK_MAX_DELAY_INT, MTK_LRO_RX1_DLY_INT); |
| 2248 | } |
| 2249 | |
| 2250 | /* Hash Type */ |
| 2251 | val = mtk_r32(eth, MTK_PDMA_RSS_GLO_CFG); |
| 2252 | val |= MTK_RSS_IPV4_STATIC_HASH; |
| 2253 | val |= MTK_RSS_IPV6_STATIC_HASH; |
| 2254 | mtk_w32(eth, val, MTK_PDMA_RSS_GLO_CFG); |
| 2255 | |
| 2256 | /* Select the size of indirection table */ |
| 2257 | mtk_w32(eth, MTK_RSS_INDR_TABLE_SIZE4, MTK_RSS_INDR_TABLE_DW0); |
| 2258 | mtk_w32(eth, MTK_RSS_INDR_TABLE_SIZE4, MTK_RSS_INDR_TABLE_DW1); |
| 2259 | mtk_w32(eth, MTK_RSS_INDR_TABLE_SIZE4, MTK_RSS_INDR_TABLE_DW2); |
| 2260 | mtk_w32(eth, MTK_RSS_INDR_TABLE_SIZE4, MTK_RSS_INDR_TABLE_DW3); |
| 2261 | mtk_w32(eth, MTK_RSS_INDR_TABLE_SIZE4, MTK_RSS_INDR_TABLE_DW4); |
| 2262 | mtk_w32(eth, MTK_RSS_INDR_TABLE_SIZE4, MTK_RSS_INDR_TABLE_DW5); |
| 2263 | mtk_w32(eth, MTK_RSS_INDR_TABLE_SIZE4, MTK_RSS_INDR_TABLE_DW6); |
| 2264 | mtk_w32(eth, MTK_RSS_INDR_TABLE_SIZE4, MTK_RSS_INDR_TABLE_DW7); |
| 2265 | |
| 2266 | /* Pause */ |
| 2267 | val |= MTK_RSS_CFG_REQ; |
| 2268 | mtk_w32(eth, val, MTK_PDMA_RSS_GLO_CFG); |
| 2269 | |
| 2270 | /* Enable RSS*/ |
| 2271 | val |= MTK_RSS_EN; |
| 2272 | mtk_w32(eth, val, MTK_PDMA_RSS_GLO_CFG); |
| 2273 | |
| 2274 | /* Release pause */ |
| 2275 | val &= ~(MTK_RSS_CFG_REQ); |
| 2276 | mtk_w32(eth, val, MTK_PDMA_RSS_GLO_CFG); |
| 2277 | |
| 2278 | /* Set perRSS GRP INT */ |
| 2279 | mtk_w32(eth, MTK_RX_DONE_INT(MTK_RSS_RING1), MTK_PDMA_INT_GRP3); |
| 2280 | |
| 2281 | /* Set GRP INT */ |
| 2282 | mtk_w32(eth, 0x21021030, MTK_FE_INT_GRP); |
| 2283 | |
| 2284 | return 0; |
| 2285 | } |
| 2286 | |
| 2287 | static void mtk_rss_uninit(struct mtk_eth *eth) |
| 2288 | { |
| 2289 | u32 val; |
| 2290 | |
| 2291 | /* Pause */ |
| 2292 | val = mtk_r32(eth, MTK_PDMA_RSS_GLO_CFG); |
| 2293 | val |= MTK_RSS_CFG_REQ; |
| 2294 | mtk_w32(eth, val, MTK_PDMA_RSS_GLO_CFG); |
| 2295 | |
| 2296 | /* Disable RSS*/ |
| 2297 | val &= ~(MTK_RSS_EN); |
| 2298 | mtk_w32(eth, val, MTK_PDMA_RSS_GLO_CFG); |
| 2299 | |
| 2300 | /* Release pause */ |
| 2301 | val &= ~(MTK_RSS_CFG_REQ); |
| 2302 | mtk_w32(eth, val, MTK_PDMA_RSS_GLO_CFG); |
| 2303 | } |
| 2304 | |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 2305 | static netdev_features_t mtk_fix_features(struct net_device *dev, |
| 2306 | netdev_features_t features) |
| 2307 | { |
| 2308 | if (!(features & NETIF_F_LRO)) { |
| 2309 | struct mtk_mac *mac = netdev_priv(dev); |
| 2310 | int ip_cnt = mtk_hwlro_get_ip_cnt(mac); |
| 2311 | |
| 2312 | if (ip_cnt) { |
| 2313 | netdev_info(dev, "RX flow is programmed, LRO should keep on\n"); |
| 2314 | |
| 2315 | features |= NETIF_F_LRO; |
| 2316 | } |
| 2317 | } |
| 2318 | |
| 2319 | if ((features & NETIF_F_HW_VLAN_CTAG_TX) && netdev_uses_dsa(dev)) { |
| 2320 | netdev_info(dev, "TX vlan offload cannot be enabled when dsa is attached.\n"); |
| 2321 | |
| 2322 | features &= ~NETIF_F_HW_VLAN_CTAG_TX; |
| 2323 | } |
| 2324 | |
| 2325 | return features; |
| 2326 | } |
| 2327 | |
| 2328 | static int mtk_set_features(struct net_device *dev, netdev_features_t features) |
| 2329 | { |
| 2330 | struct mtk_mac *mac = netdev_priv(dev); |
| 2331 | struct mtk_eth *eth = mac->hw; |
| 2332 | int err = 0; |
| 2333 | |
| 2334 | if (!((dev->features ^ features) & MTK_SET_FEATURES)) |
| 2335 | return 0; |
| 2336 | |
| 2337 | if (!(features & NETIF_F_LRO)) |
| 2338 | mtk_hwlro_netdev_disable(dev); |
| 2339 | |
| 2340 | if (!(features & NETIF_F_HW_VLAN_CTAG_RX)) |
| 2341 | mtk_w32(eth, 0, MTK_CDMP_EG_CTRL); |
| 2342 | else |
| 2343 | mtk_w32(eth, 1, MTK_CDMP_EG_CTRL); |
| 2344 | |
| 2345 | return err; |
| 2346 | } |
| 2347 | |
| 2348 | /* wait for DMA to finish whatever it is doing before we start using it again */ |
| 2349 | static int mtk_dma_busy_wait(struct mtk_eth *eth) |
| 2350 | { |
| 2351 | unsigned long t_start = jiffies; |
| 2352 | |
| 2353 | while (1) { |
| 2354 | if (MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA)) { |
| 2355 | if (!(mtk_r32(eth, MTK_QDMA_GLO_CFG) & |
| 2356 | (MTK_RX_DMA_BUSY | MTK_TX_DMA_BUSY))) |
| 2357 | return 0; |
| 2358 | } else { |
| 2359 | if (!(mtk_r32(eth, MTK_PDMA_GLO_CFG) & |
| 2360 | (MTK_RX_DMA_BUSY | MTK_TX_DMA_BUSY))) |
| 2361 | return 0; |
| 2362 | } |
| 2363 | |
| 2364 | if (time_after(jiffies, t_start + MTK_DMA_BUSY_TIMEOUT)) |
| 2365 | break; |
| 2366 | } |
| 2367 | |
| 2368 | dev_err(eth->dev, "DMA init timeout\n"); |
| 2369 | return -1; |
| 2370 | } |
| 2371 | |
| 2372 | static int mtk_dma_init(struct mtk_eth *eth) |
| 2373 | { |
| 2374 | int err; |
| 2375 | u32 i; |
| 2376 | |
| 2377 | if (mtk_dma_busy_wait(eth)) |
| 2378 | return -EBUSY; |
| 2379 | |
| 2380 | if (MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA)) { |
| 2381 | /* QDMA needs scratch memory for internal reordering of the |
| 2382 | * descriptors |
| 2383 | */ |
| 2384 | err = mtk_init_fq_dma(eth); |
| 2385 | if (err) |
| 2386 | return err; |
| 2387 | } |
| 2388 | |
| 2389 | err = mtk_tx_alloc(eth); |
| 2390 | if (err) |
| 2391 | return err; |
| 2392 | |
| 2393 | if (MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA)) { |
| 2394 | err = mtk_rx_alloc(eth, 0, MTK_RX_FLAGS_QDMA); |
| 2395 | if (err) |
| 2396 | return err; |
| 2397 | } |
| 2398 | |
| 2399 | err = mtk_rx_alloc(eth, 0, MTK_RX_FLAGS_NORMAL); |
| 2400 | if (err) |
| 2401 | return err; |
| 2402 | |
| 2403 | if (eth->hwlro) { |
developer | 77d03a7 | 2021-06-06 00:06:00 +0800 | [diff] [blame] | 2404 | i = (MTK_HAS_CAPS(eth->soc->caps, MTK_NETSYS_V2)) ? 4 : 1; |
| 2405 | for (; i < MTK_MAX_RX_RING_NUM; i++) { |
developer | fd40db2 | 2021-04-29 10:08:25 +0800 | [diff] [blame] | 2406 | err = mtk_rx_alloc(eth, i, MTK_RX_FLAGS_HWLRO); |
| 2407 | if (err) |
| 2408 | return err; |
| 2409 | } |
| 2410 | err = mtk_hwlro_rx_init(eth); |
| 2411 | if (err) |
| 2412 | return err; |
| 2413 | } |
| 2414 | |
developer | 18f46a8 | 2021-07-20 21:08:21 +0800 | [diff] [blame] | 2415 | if (MTK_HAS_CAPS(eth->soc->caps, MTK_RSS)) { |
| 2416 | for (i = 1; i < MTK_RX_NAPI_NUM; i++) { |
| 2417 | err = mtk_rx_alloc(eth, i, MTK_RX_FLAGS_NORMAL); |
| 2418 | if (err) |
| 2419 | return err; |
| 2420 | } |
| 2421 | err |