John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1 | /* This program is free software; you can redistribute it and/or modify |
| 2 | * it under the terms of the GNU General Public License as published by |
| 3 | * the Free Software Foundation; version 2 of the License |
| 4 | * |
| 5 | * This program is distributed in the hope that it will be useful, |
| 6 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 7 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 8 | * GNU General Public License for more details. |
| 9 | * |
| 10 | * Copyright (C) 2009-2016 John Crispin <blogic@openwrt.org> |
| 11 | * Copyright (C) 2009-2016 Felix Fietkau <nbd@openwrt.org> |
| 12 | * Copyright (C) 2013-2016 Michael Lee <igvtee@gmail.com> |
| 13 | */ |
| 14 | |
| 15 | #include <linux/of_device.h> |
| 16 | #include <linux/of_mdio.h> |
| 17 | #include <linux/of_net.h> |
| 18 | #include <linux/mfd/syscon.h> |
| 19 | #include <linux/regmap.h> |
| 20 | #include <linux/clk.h> |
Sean Wang | 26a2ad8 | 2016-09-14 23:13:18 +0800 | [diff] [blame] | 21 | #include <linux/pm_runtime.h> |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 22 | #include <linux/if_vlan.h> |
| 23 | #include <linux/reset.h> |
| 24 | #include <linux/tcp.h> |
| 25 | |
| 26 | #include "mtk_eth_soc.h" |
| 27 | |
| 28 | static int mtk_msg_level = -1; |
| 29 | module_param_named(msg_level, mtk_msg_level, int, 0); |
| 30 | MODULE_PARM_DESC(msg_level, "Message level (-1=defaults,0=none,...,16=all)"); |
| 31 | |
| 32 | #define MTK_ETHTOOL_STAT(x) { #x, \ |
| 33 | offsetof(struct mtk_hw_stats, x) / sizeof(u64) } |
| 34 | |
| 35 | /* strings used by ethtool */ |
| 36 | static const struct mtk_ethtool_stats { |
| 37 | char str[ETH_GSTRING_LEN]; |
| 38 | u32 offset; |
| 39 | } mtk_ethtool_stats[] = { |
| 40 | MTK_ETHTOOL_STAT(tx_bytes), |
| 41 | MTK_ETHTOOL_STAT(tx_packets), |
| 42 | MTK_ETHTOOL_STAT(tx_skip), |
| 43 | MTK_ETHTOOL_STAT(tx_collisions), |
| 44 | MTK_ETHTOOL_STAT(rx_bytes), |
| 45 | MTK_ETHTOOL_STAT(rx_packets), |
| 46 | MTK_ETHTOOL_STAT(rx_overflow), |
| 47 | MTK_ETHTOOL_STAT(rx_fcs_errors), |
| 48 | MTK_ETHTOOL_STAT(rx_short_errors), |
| 49 | MTK_ETHTOOL_STAT(rx_long_errors), |
| 50 | MTK_ETHTOOL_STAT(rx_checksum_errors), |
| 51 | MTK_ETHTOOL_STAT(rx_flow_control_packets), |
| 52 | }; |
| 53 | |
Sean Wang | 549e549 | 2016-09-01 10:47:28 +0800 | [diff] [blame] | 54 | static const char * const mtk_clks_source_name[] = { |
Sean Wang | f430dea | 2016-09-22 10:33:55 +0800 | [diff] [blame] | 55 | "ethif", "esw", "gp1", "gp2", "trgpll" |
Sean Wang | 549e549 | 2016-09-01 10:47:28 +0800 | [diff] [blame] | 56 | }; |
| 57 | |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 58 | void mtk_w32(struct mtk_eth *eth, u32 val, unsigned reg) |
| 59 | { |
| 60 | __raw_writel(val, eth->base + reg); |
| 61 | } |
| 62 | |
| 63 | u32 mtk_r32(struct mtk_eth *eth, unsigned reg) |
| 64 | { |
| 65 | return __raw_readl(eth->base + reg); |
| 66 | } |
| 67 | |
| 68 | static int mtk_mdio_busy_wait(struct mtk_eth *eth) |
| 69 | { |
| 70 | unsigned long t_start = jiffies; |
| 71 | |
| 72 | while (1) { |
| 73 | if (!(mtk_r32(eth, MTK_PHY_IAC) & PHY_IAC_ACCESS)) |
| 74 | return 0; |
| 75 | if (time_after(jiffies, t_start + PHY_IAC_TIMEOUT)) |
| 76 | break; |
| 77 | usleep_range(10, 20); |
| 78 | } |
| 79 | |
| 80 | dev_err(eth->dev, "mdio: MDIO timeout\n"); |
| 81 | return -1; |
| 82 | } |
| 83 | |
Wei Yongjun | 379672d | 2016-07-12 11:36:44 +0000 | [diff] [blame] | 84 | static u32 _mtk_mdio_write(struct mtk_eth *eth, u32 phy_addr, |
| 85 | u32 phy_register, u32 write_data) |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 86 | { |
| 87 | if (mtk_mdio_busy_wait(eth)) |
| 88 | return -1; |
| 89 | |
| 90 | write_data &= 0xffff; |
| 91 | |
| 92 | mtk_w32(eth, PHY_IAC_ACCESS | PHY_IAC_START | PHY_IAC_WRITE | |
| 93 | (phy_register << PHY_IAC_REG_SHIFT) | |
| 94 | (phy_addr << PHY_IAC_ADDR_SHIFT) | write_data, |
| 95 | MTK_PHY_IAC); |
| 96 | |
| 97 | if (mtk_mdio_busy_wait(eth)) |
| 98 | return -1; |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
Wei Yongjun | 379672d | 2016-07-12 11:36:44 +0000 | [diff] [blame] | 103 | static u32 _mtk_mdio_read(struct mtk_eth *eth, int phy_addr, int phy_reg) |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 104 | { |
| 105 | u32 d; |
| 106 | |
| 107 | if (mtk_mdio_busy_wait(eth)) |
| 108 | return 0xffff; |
| 109 | |
| 110 | mtk_w32(eth, PHY_IAC_ACCESS | PHY_IAC_START | PHY_IAC_READ | |
| 111 | (phy_reg << PHY_IAC_REG_SHIFT) | |
| 112 | (phy_addr << PHY_IAC_ADDR_SHIFT), |
| 113 | MTK_PHY_IAC); |
| 114 | |
| 115 | if (mtk_mdio_busy_wait(eth)) |
| 116 | return 0xffff; |
| 117 | |
| 118 | d = mtk_r32(eth, MTK_PHY_IAC) & 0xffff; |
| 119 | |
| 120 | return d; |
| 121 | } |
| 122 | |
| 123 | static int mtk_mdio_write(struct mii_bus *bus, int phy_addr, |
| 124 | int phy_reg, u16 val) |
| 125 | { |
| 126 | struct mtk_eth *eth = bus->priv; |
| 127 | |
| 128 | return _mtk_mdio_write(eth, phy_addr, phy_reg, val); |
| 129 | } |
| 130 | |
| 131 | static int mtk_mdio_read(struct mii_bus *bus, int phy_addr, int phy_reg) |
| 132 | { |
| 133 | struct mtk_eth *eth = bus->priv; |
| 134 | |
| 135 | return _mtk_mdio_read(eth, phy_addr, phy_reg); |
| 136 | } |
| 137 | |
Sean Wang | f430dea | 2016-09-22 10:33:55 +0800 | [diff] [blame] | 138 | static void mtk_gmac0_rgmii_adjust(struct mtk_eth *eth, int speed) |
| 139 | { |
| 140 | u32 val; |
| 141 | int ret; |
| 142 | |
| 143 | val = (speed == SPEED_1000) ? |
| 144 | INTF_MODE_RGMII_1000 : INTF_MODE_RGMII_10_100; |
| 145 | mtk_w32(eth, val, INTF_MODE); |
| 146 | |
| 147 | regmap_update_bits(eth->ethsys, ETHSYS_CLKCFG0, |
| 148 | ETHSYS_TRGMII_CLK_SEL362_5, |
| 149 | ETHSYS_TRGMII_CLK_SEL362_5); |
| 150 | |
| 151 | val = (speed == SPEED_1000) ? 250000000 : 500000000; |
| 152 | ret = clk_set_rate(eth->clks[MTK_CLK_TRGPLL], val); |
| 153 | if (ret) |
| 154 | dev_err(eth->dev, "Failed to set trgmii pll: %d\n", ret); |
| 155 | |
| 156 | val = (speed == SPEED_1000) ? |
| 157 | RCK_CTRL_RGMII_1000 : RCK_CTRL_RGMII_10_100; |
| 158 | mtk_w32(eth, val, TRGMII_RCK_CTRL); |
| 159 | |
| 160 | val = (speed == SPEED_1000) ? |
| 161 | TCK_CTRL_RGMII_1000 : TCK_CTRL_RGMII_10_100; |
| 162 | mtk_w32(eth, val, TRGMII_TCK_CTRL); |
| 163 | } |
| 164 | |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 165 | static void mtk_phy_link_adjust(struct net_device *dev) |
| 166 | { |
| 167 | struct mtk_mac *mac = netdev_priv(dev); |
John Crispin | 08ef55c | 2016-06-03 10:17:07 +0200 | [diff] [blame] | 168 | u16 lcl_adv = 0, rmt_adv = 0; |
| 169 | u8 flowctrl; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 170 | u32 mcr = MAC_MCR_MAX_RX_1536 | MAC_MCR_IPG_CFG | |
| 171 | MAC_MCR_FORCE_MODE | MAC_MCR_TX_EN | |
| 172 | MAC_MCR_RX_EN | MAC_MCR_BACKOFF_EN | |
| 173 | MAC_MCR_BACKPR_EN; |
| 174 | |
Sean Wang | dce6fa4 | 2016-09-14 23:13:21 +0800 | [diff] [blame] | 175 | if (unlikely(test_bit(MTK_RESETTING, &mac->hw->state))) |
| 176 | return; |
| 177 | |
Sean Wang | 2364c5c | 2016-09-22 16:33:35 +0800 | [diff] [blame] | 178 | switch (dev->phydev->speed) { |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 179 | case SPEED_1000: |
| 180 | mcr |= MAC_MCR_SPEED_1000; |
| 181 | break; |
| 182 | case SPEED_100: |
| 183 | mcr |= MAC_MCR_SPEED_100; |
| 184 | break; |
| 185 | }; |
| 186 | |
Sean Wang | f430dea | 2016-09-22 10:33:55 +0800 | [diff] [blame] | 187 | if (mac->id == 0 && !mac->trgmii) |
Sean Wang | 2364c5c | 2016-09-22 16:33:35 +0800 | [diff] [blame] | 188 | mtk_gmac0_rgmii_adjust(mac->hw, dev->phydev->speed); |
Sean Wang | f430dea | 2016-09-22 10:33:55 +0800 | [diff] [blame] | 189 | |
Sean Wang | 2364c5c | 2016-09-22 16:33:35 +0800 | [diff] [blame] | 190 | if (dev->phydev->link) |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 191 | mcr |= MAC_MCR_FORCE_LINK; |
| 192 | |
Sean Wang | 2364c5c | 2016-09-22 16:33:35 +0800 | [diff] [blame] | 193 | if (dev->phydev->duplex) { |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 194 | mcr |= MAC_MCR_FORCE_DPX; |
| 195 | |
Sean Wang | 2364c5c | 2016-09-22 16:33:35 +0800 | [diff] [blame] | 196 | if (dev->phydev->pause) |
John Crispin | 08ef55c | 2016-06-03 10:17:07 +0200 | [diff] [blame] | 197 | rmt_adv = LPA_PAUSE_CAP; |
Sean Wang | 2364c5c | 2016-09-22 16:33:35 +0800 | [diff] [blame] | 198 | if (dev->phydev->asym_pause) |
John Crispin | 08ef55c | 2016-06-03 10:17:07 +0200 | [diff] [blame] | 199 | rmt_adv |= LPA_PAUSE_ASYM; |
| 200 | |
Sean Wang | 2364c5c | 2016-09-22 16:33:35 +0800 | [diff] [blame] | 201 | if (dev->phydev->advertising & ADVERTISED_Pause) |
John Crispin | 08ef55c | 2016-06-03 10:17:07 +0200 | [diff] [blame] | 202 | lcl_adv |= ADVERTISE_PAUSE_CAP; |
Sean Wang | 2364c5c | 2016-09-22 16:33:35 +0800 | [diff] [blame] | 203 | if (dev->phydev->advertising & ADVERTISED_Asym_Pause) |
John Crispin | 08ef55c | 2016-06-03 10:17:07 +0200 | [diff] [blame] | 204 | lcl_adv |= ADVERTISE_PAUSE_ASYM; |
| 205 | |
| 206 | flowctrl = mii_resolve_flowctrl_fdx(lcl_adv, rmt_adv); |
| 207 | |
| 208 | if (flowctrl & FLOW_CTRL_TX) |
| 209 | mcr |= MAC_MCR_FORCE_TX_FC; |
| 210 | if (flowctrl & FLOW_CTRL_RX) |
| 211 | mcr |= MAC_MCR_FORCE_RX_FC; |
| 212 | |
| 213 | netif_dbg(mac->hw, link, dev, "rx pause %s, tx pause %s\n", |
| 214 | flowctrl & FLOW_CTRL_RX ? "enabled" : "disabled", |
| 215 | flowctrl & FLOW_CTRL_TX ? "enabled" : "disabled"); |
| 216 | } |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 217 | |
| 218 | mtk_w32(mac->hw, mcr, MTK_MAC_MCR(mac->id)); |
| 219 | |
Sean Wang | 2364c5c | 2016-09-22 16:33:35 +0800 | [diff] [blame] | 220 | if (dev->phydev->link) |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 221 | netif_carrier_on(dev); |
| 222 | else |
| 223 | netif_carrier_off(dev); |
John Crispin | 5969c42 | 2017-06-19 15:37:03 +0200 | [diff] [blame] | 224 | |
| 225 | if (!of_phy_is_fixed_link(mac->of_node)) |
| 226 | phy_print_status(dev->phydev); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | static int mtk_phy_connect_node(struct mtk_eth *eth, struct mtk_mac *mac, |
| 230 | struct device_node *phy_node) |
| 231 | { |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 232 | struct phy_device *phydev; |
Sean Wang | a2b2a19 | 2016-09-22 16:36:15 +0800 | [diff] [blame] | 233 | int phy_mode; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 234 | |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 235 | phy_mode = of_get_phy_mode(phy_node); |
| 236 | if (phy_mode < 0) { |
| 237 | dev_err(eth->dev, "incorrect phy-mode %d\n", phy_mode); |
| 238 | return -EINVAL; |
| 239 | } |
| 240 | |
| 241 | phydev = of_phy_connect(eth->netdev[mac->id], phy_node, |
| 242 | mtk_phy_link_adjust, 0, phy_mode); |
Dan Carpenter | 977bc20 | 2016-03-15 10:18:49 +0300 | [diff] [blame] | 243 | if (!phydev) { |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 244 | dev_err(eth->dev, "could not connect to PHY\n"); |
Dan Carpenter | 977bc20 | 2016-03-15 10:18:49 +0300 | [diff] [blame] | 245 | return -ENODEV; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | dev_info(eth->dev, |
| 249 | "connected mac %d to PHY at %s [uid=%08x, driver=%s]\n", |
| 250 | mac->id, phydev_name(phydev), phydev->phy_id, |
| 251 | phydev->drv->name); |
| 252 | |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 253 | return 0; |
| 254 | } |
| 255 | |
Sean Wang | 2364c5c | 2016-09-22 16:33:35 +0800 | [diff] [blame] | 256 | static int mtk_phy_connect(struct net_device *dev) |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 257 | { |
Sean Wang | 2364c5c | 2016-09-22 16:33:35 +0800 | [diff] [blame] | 258 | struct mtk_mac *mac = netdev_priv(dev); |
| 259 | struct mtk_eth *eth; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 260 | struct device_node *np; |
Sean Wang | 9ea4d31 | 2016-09-14 23:13:19 +0800 | [diff] [blame] | 261 | u32 val; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 262 | |
Sean Wang | 2364c5c | 2016-09-22 16:33:35 +0800 | [diff] [blame] | 263 | eth = mac->hw; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 264 | np = of_parse_phandle(mac->of_node, "phy-handle", 0); |
John Crispin | 0c72c50 | 2016-06-03 10:17:08 +0200 | [diff] [blame] | 265 | if (!np && of_phy_is_fixed_link(mac->of_node)) |
| 266 | if (!of_phy_register_fixed_link(mac->of_node)) |
| 267 | np = of_node_get(mac->of_node); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 268 | if (!np) |
| 269 | return -ENODEV; |
| 270 | |
| 271 | switch (of_get_phy_mode(np)) { |
Sean Wang | 572de60 | 2016-09-22 10:33:54 +0800 | [diff] [blame] | 272 | case PHY_INTERFACE_MODE_TRGMII: |
| 273 | mac->trgmii = true; |
John Crispin | 37920fc | 2016-06-03 10:17:09 +0200 | [diff] [blame] | 274 | case PHY_INTERFACE_MODE_RGMII_TXID: |
| 275 | case PHY_INTERFACE_MODE_RGMII_RXID: |
| 276 | case PHY_INTERFACE_MODE_RGMII_ID: |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 277 | case PHY_INTERFACE_MODE_RGMII: |
Sean Wang | 9ea4d31 | 2016-09-14 23:13:19 +0800 | [diff] [blame] | 278 | mac->ge_mode = 0; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 279 | break; |
| 280 | case PHY_INTERFACE_MODE_MII: |
Sean Wang | 9ea4d31 | 2016-09-14 23:13:19 +0800 | [diff] [blame] | 281 | mac->ge_mode = 1; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 282 | break; |
sean.wang@mediatek.com | 8ca7f4f | 2016-08-16 13:55:13 +0800 | [diff] [blame] | 283 | case PHY_INTERFACE_MODE_REVMII: |
Sean Wang | 9ea4d31 | 2016-09-14 23:13:19 +0800 | [diff] [blame] | 284 | mac->ge_mode = 2; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 285 | break; |
sean.wang@mediatek.com | 8ca7f4f | 2016-08-16 13:55:13 +0800 | [diff] [blame] | 286 | case PHY_INTERFACE_MODE_RMII: |
| 287 | if (!mac->id) |
| 288 | goto err_phy; |
Sean Wang | 9ea4d31 | 2016-09-14 23:13:19 +0800 | [diff] [blame] | 289 | mac->ge_mode = 3; |
sean.wang@mediatek.com | 8ca7f4f | 2016-08-16 13:55:13 +0800 | [diff] [blame] | 290 | break; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 291 | default: |
sean.wang@mediatek.com | 8ca7f4f | 2016-08-16 13:55:13 +0800 | [diff] [blame] | 292 | goto err_phy; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | /* put the gmac into the right mode */ |
| 296 | regmap_read(eth->ethsys, ETHSYS_SYSCFG0, &val); |
| 297 | val &= ~SYSCFG0_GE_MODE(SYSCFG0_GE_MASK, mac->id); |
Sean Wang | 9ea4d31 | 2016-09-14 23:13:19 +0800 | [diff] [blame] | 298 | val |= SYSCFG0_GE_MODE(mac->ge_mode, mac->id); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 299 | regmap_write(eth->ethsys, ETHSYS_SYSCFG0, val); |
| 300 | |
Sean Wang | 2364c5c | 2016-09-22 16:33:35 +0800 | [diff] [blame] | 301 | /* couple phydev to net_device */ |
Sean Wang | f6f7d9c | 2016-09-22 16:44:16 +0800 | [diff] [blame] | 302 | if (mtk_phy_connect_node(eth, mac, np)) |
| 303 | goto err_phy; |
| 304 | |
Sean Wang | 2364c5c | 2016-09-22 16:33:35 +0800 | [diff] [blame] | 305 | dev->phydev->autoneg = AUTONEG_ENABLE; |
| 306 | dev->phydev->speed = 0; |
| 307 | dev->phydev->duplex = 0; |
sean.wang@mediatek.com | b2025c7 | 2016-08-16 13:55:14 +0800 | [diff] [blame] | 308 | |
| 309 | if (of_phy_is_fixed_link(mac->of_node)) |
Sean Wang | 2364c5c | 2016-09-22 16:33:35 +0800 | [diff] [blame] | 310 | dev->phydev->supported |= |
sean.wang@mediatek.com | b2025c7 | 2016-08-16 13:55:14 +0800 | [diff] [blame] | 311 | SUPPORTED_Pause | SUPPORTED_Asym_Pause; |
| 312 | |
Sean Wang | 2364c5c | 2016-09-22 16:33:35 +0800 | [diff] [blame] | 313 | dev->phydev->supported &= PHY_GBIT_FEATURES | SUPPORTED_Pause | |
John Crispin | 08ef55c | 2016-06-03 10:17:07 +0200 | [diff] [blame] | 314 | SUPPORTED_Asym_Pause; |
Sean Wang | 2364c5c | 2016-09-22 16:33:35 +0800 | [diff] [blame] | 315 | dev->phydev->advertising = dev->phydev->supported | |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 316 | ADVERTISED_Autoneg; |
Sean Wang | 2364c5c | 2016-09-22 16:33:35 +0800 | [diff] [blame] | 317 | phy_start_aneg(dev->phydev); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 318 | |
sean.wang@mediatek.com | e8c2993 | 2016-08-13 19:16:19 +0800 | [diff] [blame] | 319 | of_node_put(np); |
| 320 | |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 321 | return 0; |
sean.wang@mediatek.com | 8ca7f4f | 2016-08-16 13:55:13 +0800 | [diff] [blame] | 322 | |
| 323 | err_phy: |
Johan Hovold | 16a67eb | 2016-11-28 19:25:05 +0100 | [diff] [blame] | 324 | if (of_phy_is_fixed_link(mac->of_node)) |
| 325 | of_phy_deregister_fixed_link(mac->of_node); |
sean.wang@mediatek.com | 8ca7f4f | 2016-08-16 13:55:13 +0800 | [diff] [blame] | 326 | of_node_put(np); |
Sean Wang | f6f7d9c | 2016-09-22 16:44:16 +0800 | [diff] [blame] | 327 | dev_err(eth->dev, "%s: invalid phy\n", __func__); |
sean.wang@mediatek.com | 8ca7f4f | 2016-08-16 13:55:13 +0800 | [diff] [blame] | 328 | return -EINVAL; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | static int mtk_mdio_init(struct mtk_eth *eth) |
| 332 | { |
| 333 | struct device_node *mii_np; |
Sean Wang | 1e515b7 | 2016-09-01 10:47:34 +0800 | [diff] [blame] | 334 | int ret; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 335 | |
| 336 | mii_np = of_get_child_by_name(eth->dev->of_node, "mdio-bus"); |
| 337 | if (!mii_np) { |
| 338 | dev_err(eth->dev, "no %s child node found", "mdio-bus"); |
| 339 | return -ENODEV; |
| 340 | } |
| 341 | |
| 342 | if (!of_device_is_available(mii_np)) { |
Sean Wang | aa6e8a5 | 2016-09-01 10:47:35 +0800 | [diff] [blame] | 343 | ret = -ENODEV; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 344 | goto err_put_node; |
| 345 | } |
| 346 | |
Sean Wang | 1e515b7 | 2016-09-01 10:47:34 +0800 | [diff] [blame] | 347 | eth->mii_bus = devm_mdiobus_alloc(eth->dev); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 348 | if (!eth->mii_bus) { |
Sean Wang | 1e515b7 | 2016-09-01 10:47:34 +0800 | [diff] [blame] | 349 | ret = -ENOMEM; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 350 | goto err_put_node; |
| 351 | } |
| 352 | |
| 353 | eth->mii_bus->name = "mdio"; |
| 354 | eth->mii_bus->read = mtk_mdio_read; |
| 355 | eth->mii_bus->write = mtk_mdio_write; |
| 356 | eth->mii_bus->priv = eth; |
| 357 | eth->mii_bus->parent = eth->dev; |
| 358 | |
| 359 | snprintf(eth->mii_bus->id, MII_BUS_ID_SIZE, "%s", mii_np->name); |
Sean Wang | 1e515b7 | 2016-09-01 10:47:34 +0800 | [diff] [blame] | 360 | ret = of_mdiobus_register(eth->mii_bus, mii_np); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 361 | |
| 362 | err_put_node: |
| 363 | of_node_put(mii_np); |
Sean Wang | 1e515b7 | 2016-09-01 10:47:34 +0800 | [diff] [blame] | 364 | return ret; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | static void mtk_mdio_cleanup(struct mtk_eth *eth) |
| 368 | { |
| 369 | if (!eth->mii_bus) |
| 370 | return; |
| 371 | |
| 372 | mdiobus_unregister(eth->mii_bus); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 373 | } |
| 374 | |
John Crispin | 5cce032 | 2017-06-19 15:37:05 +0200 | [diff] [blame^] | 375 | static inline void mtk_tx_irq_disable(struct mtk_eth *eth, u32 mask) |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 376 | { |
John Crispin | 7bc9cce | 2016-06-29 13:38:10 +0200 | [diff] [blame] | 377 | unsigned long flags; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 378 | u32 val; |
| 379 | |
John Crispin | 5cce032 | 2017-06-19 15:37:05 +0200 | [diff] [blame^] | 380 | spin_lock_irqsave(ð->tx_irq_lock, flags); |
| 381 | val = mtk_r32(eth, MTK_QDMA_INT_MASK); |
| 382 | mtk_w32(eth, val & ~mask, MTK_QDMA_INT_MASK); |
| 383 | spin_unlock_irqrestore(ð->tx_irq_lock, flags); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 384 | } |
| 385 | |
John Crispin | 5cce032 | 2017-06-19 15:37:05 +0200 | [diff] [blame^] | 386 | static inline void mtk_tx_irq_enable(struct mtk_eth *eth, u32 mask) |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 387 | { |
John Crispin | 7bc9cce | 2016-06-29 13:38:10 +0200 | [diff] [blame] | 388 | unsigned long flags; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 389 | u32 val; |
| 390 | |
John Crispin | 5cce032 | 2017-06-19 15:37:05 +0200 | [diff] [blame^] | 391 | spin_lock_irqsave(ð->tx_irq_lock, flags); |
| 392 | val = mtk_r32(eth, MTK_QDMA_INT_MASK); |
| 393 | mtk_w32(eth, val | mask, MTK_QDMA_INT_MASK); |
| 394 | spin_unlock_irqrestore(ð->tx_irq_lock, flags); |
| 395 | } |
| 396 | |
| 397 | static inline void mtk_rx_irq_disable(struct mtk_eth *eth, u32 mask) |
| 398 | { |
| 399 | unsigned long flags; |
| 400 | u32 val; |
| 401 | |
| 402 | spin_lock_irqsave(ð->rx_irq_lock, flags); |
| 403 | val = mtk_r32(eth, MTK_PDMA_INT_MASK); |
| 404 | mtk_w32(eth, val & ~mask, MTK_PDMA_INT_MASK); |
| 405 | spin_unlock_irqrestore(ð->rx_irq_lock, flags); |
| 406 | } |
| 407 | |
| 408 | static inline void mtk_rx_irq_enable(struct mtk_eth *eth, u32 mask) |
| 409 | { |
| 410 | unsigned long flags; |
| 411 | u32 val; |
| 412 | |
| 413 | spin_lock_irqsave(ð->rx_irq_lock, flags); |
| 414 | val = mtk_r32(eth, MTK_PDMA_INT_MASK); |
| 415 | mtk_w32(eth, val | mask, MTK_PDMA_INT_MASK); |
| 416 | spin_unlock_irqrestore(ð->rx_irq_lock, flags); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | static int mtk_set_mac_address(struct net_device *dev, void *p) |
| 420 | { |
| 421 | int ret = eth_mac_addr(dev, p); |
| 422 | struct mtk_mac *mac = netdev_priv(dev); |
| 423 | const char *macaddr = dev->dev_addr; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 424 | |
| 425 | if (ret) |
| 426 | return ret; |
| 427 | |
Sean Wang | dce6fa4 | 2016-09-14 23:13:21 +0800 | [diff] [blame] | 428 | if (unlikely(test_bit(MTK_RESETTING, &mac->hw->state))) |
| 429 | return -EBUSY; |
| 430 | |
Sean Wang | e3e9652 | 2016-08-11 17:51:00 +0800 | [diff] [blame] | 431 | spin_lock_bh(&mac->hw->page_lock); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 432 | mtk_w32(mac->hw, (macaddr[0] << 8) | macaddr[1], |
| 433 | MTK_GDMA_MAC_ADRH(mac->id)); |
| 434 | mtk_w32(mac->hw, (macaddr[2] << 24) | (macaddr[3] << 16) | |
| 435 | (macaddr[4] << 8) | macaddr[5], |
| 436 | MTK_GDMA_MAC_ADRL(mac->id)); |
Sean Wang | e3e9652 | 2016-08-11 17:51:00 +0800 | [diff] [blame] | 437 | spin_unlock_bh(&mac->hw->page_lock); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 438 | |
| 439 | return 0; |
| 440 | } |
| 441 | |
| 442 | void mtk_stats_update_mac(struct mtk_mac *mac) |
| 443 | { |
| 444 | struct mtk_hw_stats *hw_stats = mac->hw_stats; |
| 445 | unsigned int base = MTK_GDM1_TX_GBCNT; |
| 446 | u64 stats; |
| 447 | |
| 448 | base += hw_stats->reg_offset; |
| 449 | |
| 450 | u64_stats_update_begin(&hw_stats->syncp); |
| 451 | |
| 452 | hw_stats->rx_bytes += mtk_r32(mac->hw, base); |
| 453 | stats = mtk_r32(mac->hw, base + 0x04); |
| 454 | if (stats) |
| 455 | hw_stats->rx_bytes += (stats << 32); |
| 456 | hw_stats->rx_packets += mtk_r32(mac->hw, base + 0x08); |
| 457 | hw_stats->rx_overflow += mtk_r32(mac->hw, base + 0x10); |
| 458 | hw_stats->rx_fcs_errors += mtk_r32(mac->hw, base + 0x14); |
| 459 | hw_stats->rx_short_errors += mtk_r32(mac->hw, base + 0x18); |
| 460 | hw_stats->rx_long_errors += mtk_r32(mac->hw, base + 0x1c); |
| 461 | hw_stats->rx_checksum_errors += mtk_r32(mac->hw, base + 0x20); |
| 462 | hw_stats->rx_flow_control_packets += |
| 463 | mtk_r32(mac->hw, base + 0x24); |
| 464 | hw_stats->tx_skip += mtk_r32(mac->hw, base + 0x28); |
| 465 | hw_stats->tx_collisions += mtk_r32(mac->hw, base + 0x2c); |
| 466 | hw_stats->tx_bytes += mtk_r32(mac->hw, base + 0x30); |
| 467 | stats = mtk_r32(mac->hw, base + 0x34); |
| 468 | if (stats) |
| 469 | hw_stats->tx_bytes += (stats << 32); |
| 470 | hw_stats->tx_packets += mtk_r32(mac->hw, base + 0x38); |
| 471 | u64_stats_update_end(&hw_stats->syncp); |
| 472 | } |
| 473 | |
| 474 | static void mtk_stats_update(struct mtk_eth *eth) |
| 475 | { |
| 476 | int i; |
| 477 | |
| 478 | for (i = 0; i < MTK_MAC_COUNT; i++) { |
| 479 | if (!eth->mac[i] || !eth->mac[i]->hw_stats) |
| 480 | continue; |
| 481 | if (spin_trylock(ð->mac[i]->hw_stats->stats_lock)) { |
| 482 | mtk_stats_update_mac(eth->mac[i]); |
| 483 | spin_unlock(ð->mac[i]->hw_stats->stats_lock); |
| 484 | } |
| 485 | } |
| 486 | } |
| 487 | |
stephen hemminger | bc1f447 | 2017-01-06 19:12:52 -0800 | [diff] [blame] | 488 | static void mtk_get_stats64(struct net_device *dev, |
| 489 | struct rtnl_link_stats64 *storage) |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 490 | { |
| 491 | struct mtk_mac *mac = netdev_priv(dev); |
| 492 | struct mtk_hw_stats *hw_stats = mac->hw_stats; |
| 493 | unsigned int start; |
| 494 | |
| 495 | if (netif_running(dev) && netif_device_present(dev)) { |
| 496 | if (spin_trylock(&hw_stats->stats_lock)) { |
| 497 | mtk_stats_update_mac(mac); |
| 498 | spin_unlock(&hw_stats->stats_lock); |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | do { |
| 503 | start = u64_stats_fetch_begin_irq(&hw_stats->syncp); |
| 504 | storage->rx_packets = hw_stats->rx_packets; |
| 505 | storage->tx_packets = hw_stats->tx_packets; |
| 506 | storage->rx_bytes = hw_stats->rx_bytes; |
| 507 | storage->tx_bytes = hw_stats->tx_bytes; |
| 508 | storage->collisions = hw_stats->tx_collisions; |
| 509 | storage->rx_length_errors = hw_stats->rx_short_errors + |
| 510 | hw_stats->rx_long_errors; |
| 511 | storage->rx_over_errors = hw_stats->rx_overflow; |
| 512 | storage->rx_crc_errors = hw_stats->rx_fcs_errors; |
| 513 | storage->rx_errors = hw_stats->rx_checksum_errors; |
| 514 | storage->tx_aborted_errors = hw_stats->tx_skip; |
| 515 | } while (u64_stats_fetch_retry_irq(&hw_stats->syncp, start)); |
| 516 | |
| 517 | storage->tx_errors = dev->stats.tx_errors; |
| 518 | storage->rx_dropped = dev->stats.rx_dropped; |
| 519 | storage->tx_dropped = dev->stats.tx_dropped; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | static inline int mtk_max_frag_size(int mtu) |
| 523 | { |
| 524 | /* make sure buf_size will be at least MTK_MAX_RX_LENGTH */ |
| 525 | if (mtu + MTK_RX_ETH_HLEN < MTK_MAX_RX_LENGTH) |
| 526 | mtu = MTK_MAX_RX_LENGTH - MTK_RX_ETH_HLEN; |
| 527 | |
| 528 | return SKB_DATA_ALIGN(MTK_RX_HLEN + mtu) + |
| 529 | SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
| 530 | } |
| 531 | |
| 532 | static inline int mtk_max_buf_size(int frag_size) |
| 533 | { |
| 534 | int buf_size = frag_size - NET_SKB_PAD - NET_IP_ALIGN - |
| 535 | SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
| 536 | |
| 537 | WARN_ON(buf_size < MTK_MAX_RX_LENGTH); |
| 538 | |
| 539 | return buf_size; |
| 540 | } |
| 541 | |
| 542 | static inline void mtk_rx_get_desc(struct mtk_rx_dma *rxd, |
| 543 | struct mtk_rx_dma *dma_rxd) |
| 544 | { |
| 545 | rxd->rxd1 = READ_ONCE(dma_rxd->rxd1); |
| 546 | rxd->rxd2 = READ_ONCE(dma_rxd->rxd2); |
| 547 | rxd->rxd3 = READ_ONCE(dma_rxd->rxd3); |
| 548 | rxd->rxd4 = READ_ONCE(dma_rxd->rxd4); |
| 549 | } |
| 550 | |
| 551 | /* the qdma core needs scratch memory to be setup */ |
| 552 | static int mtk_init_fq_dma(struct mtk_eth *eth) |
| 553 | { |
John Crispin | 605e4fe | 2016-06-10 13:27:59 +0200 | [diff] [blame] | 554 | dma_addr_t phy_ring_tail; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 555 | int cnt = MTK_DMA_SIZE; |
| 556 | dma_addr_t dma_addr; |
| 557 | int i; |
| 558 | |
| 559 | eth->scratch_ring = dma_alloc_coherent(eth->dev, |
| 560 | cnt * sizeof(struct mtk_tx_dma), |
John Crispin | 605e4fe | 2016-06-10 13:27:59 +0200 | [diff] [blame] | 561 | ð->phy_scratch_ring, |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 562 | GFP_ATOMIC | __GFP_ZERO); |
| 563 | if (unlikely(!eth->scratch_ring)) |
| 564 | return -ENOMEM; |
| 565 | |
| 566 | eth->scratch_head = kcalloc(cnt, MTK_QDMA_PAGE_SIZE, |
| 567 | GFP_KERNEL); |
John Crispin | 562c5a7 | 2016-06-10 13:27:58 +0200 | [diff] [blame] | 568 | if (unlikely(!eth->scratch_head)) |
| 569 | return -ENOMEM; |
| 570 | |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 571 | dma_addr = dma_map_single(eth->dev, |
| 572 | eth->scratch_head, cnt * MTK_QDMA_PAGE_SIZE, |
| 573 | DMA_FROM_DEVICE); |
| 574 | if (unlikely(dma_mapping_error(eth->dev, dma_addr))) |
| 575 | return -ENOMEM; |
| 576 | |
| 577 | memset(eth->scratch_ring, 0x0, sizeof(struct mtk_tx_dma) * cnt); |
John Crispin | 605e4fe | 2016-06-10 13:27:59 +0200 | [diff] [blame] | 578 | phy_ring_tail = eth->phy_scratch_ring + |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 579 | (sizeof(struct mtk_tx_dma) * (cnt - 1)); |
| 580 | |
| 581 | for (i = 0; i < cnt; i++) { |
| 582 | eth->scratch_ring[i].txd1 = |
| 583 | (dma_addr + (i * MTK_QDMA_PAGE_SIZE)); |
| 584 | if (i < cnt - 1) |
John Crispin | 605e4fe | 2016-06-10 13:27:59 +0200 | [diff] [blame] | 585 | eth->scratch_ring[i].txd2 = (eth->phy_scratch_ring + |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 586 | ((i + 1) * sizeof(struct mtk_tx_dma))); |
| 587 | eth->scratch_ring[i].txd3 = TX_DMA_SDL(MTK_QDMA_PAGE_SIZE); |
| 588 | } |
| 589 | |
John Crispin | 605e4fe | 2016-06-10 13:27:59 +0200 | [diff] [blame] | 590 | mtk_w32(eth, eth->phy_scratch_ring, MTK_QDMA_FQ_HEAD); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 591 | mtk_w32(eth, phy_ring_tail, MTK_QDMA_FQ_TAIL); |
| 592 | mtk_w32(eth, (cnt << 16) | cnt, MTK_QDMA_FQ_CNT); |
| 593 | mtk_w32(eth, MTK_QDMA_PAGE_SIZE << 16, MTK_QDMA_FQ_BLEN); |
| 594 | |
| 595 | return 0; |
| 596 | } |
| 597 | |
| 598 | static inline void *mtk_qdma_phys_to_virt(struct mtk_tx_ring *ring, u32 desc) |
| 599 | { |
| 600 | void *ret = ring->dma; |
| 601 | |
| 602 | return ret + (desc - ring->phys); |
| 603 | } |
| 604 | |
| 605 | static inline struct mtk_tx_buf *mtk_desc_to_tx_buf(struct mtk_tx_ring *ring, |
| 606 | struct mtk_tx_dma *txd) |
| 607 | { |
| 608 | int idx = txd - ring->dma; |
| 609 | |
| 610 | return &ring->buf[idx]; |
| 611 | } |
| 612 | |
sean.wang@mediatek.com | 55a4e77 | 2016-08-16 13:55:15 +0800 | [diff] [blame] | 613 | static void mtk_tx_unmap(struct mtk_eth *eth, struct mtk_tx_buf *tx_buf) |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 614 | { |
| 615 | if (tx_buf->flags & MTK_TX_FLAGS_SINGLE0) { |
sean.wang@mediatek.com | 55a4e77 | 2016-08-16 13:55:15 +0800 | [diff] [blame] | 616 | dma_unmap_single(eth->dev, |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 617 | dma_unmap_addr(tx_buf, dma_addr0), |
| 618 | dma_unmap_len(tx_buf, dma_len0), |
| 619 | DMA_TO_DEVICE); |
| 620 | } else if (tx_buf->flags & MTK_TX_FLAGS_PAGE0) { |
sean.wang@mediatek.com | 55a4e77 | 2016-08-16 13:55:15 +0800 | [diff] [blame] | 621 | dma_unmap_page(eth->dev, |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 622 | dma_unmap_addr(tx_buf, dma_addr0), |
| 623 | dma_unmap_len(tx_buf, dma_len0), |
| 624 | DMA_TO_DEVICE); |
| 625 | } |
| 626 | tx_buf->flags = 0; |
| 627 | if (tx_buf->skb && |
| 628 | (tx_buf->skb != (struct sk_buff *)MTK_DMA_DUMMY_DESC)) |
| 629 | dev_kfree_skb_any(tx_buf->skb); |
| 630 | tx_buf->skb = NULL; |
| 631 | } |
| 632 | |
| 633 | static int mtk_tx_map(struct sk_buff *skb, struct net_device *dev, |
| 634 | int tx_num, struct mtk_tx_ring *ring, bool gso) |
| 635 | { |
| 636 | struct mtk_mac *mac = netdev_priv(dev); |
| 637 | struct mtk_eth *eth = mac->hw; |
| 638 | struct mtk_tx_dma *itxd, *txd; |
Sean Wang | 81d2dd0 | 2017-04-14 11:19:11 +0800 | [diff] [blame] | 639 | struct mtk_tx_buf *itx_buf, *tx_buf; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 640 | dma_addr_t mapped_addr; |
| 641 | unsigned int nr_frags; |
| 642 | int i, n_desc = 1; |
Sean Wang | c6f1dc4 | 2016-09-01 10:47:27 +0800 | [diff] [blame] | 643 | u32 txd4 = 0, fport; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 644 | |
| 645 | itxd = ring->next_free; |
| 646 | if (itxd == ring->last_free) |
| 647 | return -ENOMEM; |
| 648 | |
| 649 | /* set the forward port */ |
Sean Wang | c6f1dc4 | 2016-09-01 10:47:27 +0800 | [diff] [blame] | 650 | fport = (mac->id + 1) << TX_DMA_FPORT_SHIFT; |
| 651 | txd4 |= fport; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 652 | |
Sean Wang | 81d2dd0 | 2017-04-14 11:19:11 +0800 | [diff] [blame] | 653 | itx_buf = mtk_desc_to_tx_buf(ring, itxd); |
| 654 | memset(itx_buf, 0, sizeof(*itx_buf)); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 655 | |
| 656 | if (gso) |
| 657 | txd4 |= TX_DMA_TSO; |
| 658 | |
| 659 | /* TX Checksum offload */ |
| 660 | if (skb->ip_summed == CHECKSUM_PARTIAL) |
| 661 | txd4 |= TX_DMA_CHKSUM; |
| 662 | |
| 663 | /* VLAN header offload */ |
| 664 | if (skb_vlan_tag_present(skb)) |
| 665 | txd4 |= TX_DMA_INS_VLAN | skb_vlan_tag_get(skb); |
| 666 | |
sean.wang@mediatek.com | 55a4e77 | 2016-08-16 13:55:15 +0800 | [diff] [blame] | 667 | mapped_addr = dma_map_single(eth->dev, skb->data, |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 668 | skb_headlen(skb), DMA_TO_DEVICE); |
sean.wang@mediatek.com | 55a4e77 | 2016-08-16 13:55:15 +0800 | [diff] [blame] | 669 | if (unlikely(dma_mapping_error(eth->dev, mapped_addr))) |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 670 | return -ENOMEM; |
| 671 | |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 672 | WRITE_ONCE(itxd->txd1, mapped_addr); |
Sean Wang | 81d2dd0 | 2017-04-14 11:19:11 +0800 | [diff] [blame] | 673 | itx_buf->flags |= MTK_TX_FLAGS_SINGLE0; |
Sean Wang | 134d215 | 2017-04-14 11:19:12 +0800 | [diff] [blame] | 674 | itx_buf->flags |= (!mac->id) ? MTK_TX_FLAGS_FPORT0 : |
| 675 | MTK_TX_FLAGS_FPORT1; |
Sean Wang | 81d2dd0 | 2017-04-14 11:19:11 +0800 | [diff] [blame] | 676 | dma_unmap_addr_set(itx_buf, dma_addr0, mapped_addr); |
| 677 | dma_unmap_len_set(itx_buf, dma_len0, skb_headlen(skb)); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 678 | |
| 679 | /* TX SG offload */ |
| 680 | txd = itxd; |
| 681 | nr_frags = skb_shinfo(skb)->nr_frags; |
| 682 | for (i = 0; i < nr_frags; i++) { |
| 683 | struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i]; |
| 684 | unsigned int offset = 0; |
| 685 | int frag_size = skb_frag_size(frag); |
| 686 | |
| 687 | while (frag_size) { |
| 688 | bool last_frag = false; |
| 689 | unsigned int frag_map_size; |
| 690 | |
| 691 | txd = mtk_qdma_phys_to_virt(ring, txd->txd2); |
| 692 | if (txd == ring->last_free) |
| 693 | goto err_dma; |
| 694 | |
| 695 | n_desc++; |
| 696 | frag_map_size = min(frag_size, MTK_TX_DMA_BUF_LEN); |
sean.wang@mediatek.com | 55a4e77 | 2016-08-16 13:55:15 +0800 | [diff] [blame] | 697 | mapped_addr = skb_frag_dma_map(eth->dev, frag, offset, |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 698 | frag_map_size, |
| 699 | DMA_TO_DEVICE); |
sean.wang@mediatek.com | 55a4e77 | 2016-08-16 13:55:15 +0800 | [diff] [blame] | 700 | if (unlikely(dma_mapping_error(eth->dev, mapped_addr))) |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 701 | goto err_dma; |
| 702 | |
| 703 | if (i == nr_frags - 1 && |
| 704 | (frag_size - frag_map_size) == 0) |
| 705 | last_frag = true; |
| 706 | |
| 707 | WRITE_ONCE(txd->txd1, mapped_addr); |
| 708 | WRITE_ONCE(txd->txd3, (TX_DMA_SWC | |
| 709 | TX_DMA_PLEN0(frag_map_size) | |
John Crispin | 369f045 | 2016-04-08 00:54:11 +0200 | [diff] [blame] | 710 | last_frag * TX_DMA_LS0)); |
Sean Wang | c6f1dc4 | 2016-09-01 10:47:27 +0800 | [diff] [blame] | 711 | WRITE_ONCE(txd->txd4, fport); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 712 | |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 713 | tx_buf = mtk_desc_to_tx_buf(ring, txd); |
| 714 | memset(tx_buf, 0, sizeof(*tx_buf)); |
Sean Wang | 81d2dd0 | 2017-04-14 11:19:11 +0800 | [diff] [blame] | 715 | tx_buf->skb = (struct sk_buff *)MTK_DMA_DUMMY_DESC; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 716 | tx_buf->flags |= MTK_TX_FLAGS_PAGE0; |
Sean Wang | 134d215 | 2017-04-14 11:19:12 +0800 | [diff] [blame] | 717 | tx_buf->flags |= (!mac->id) ? MTK_TX_FLAGS_FPORT0 : |
| 718 | MTK_TX_FLAGS_FPORT1; |
| 719 | |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 720 | dma_unmap_addr_set(tx_buf, dma_addr0, mapped_addr); |
| 721 | dma_unmap_len_set(tx_buf, dma_len0, frag_map_size); |
| 722 | frag_size -= frag_map_size; |
| 723 | offset += frag_map_size; |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | /* store skb to cleanup */ |
Sean Wang | 81d2dd0 | 2017-04-14 11:19:11 +0800 | [diff] [blame] | 728 | itx_buf->skb = skb; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 729 | |
| 730 | WRITE_ONCE(itxd->txd4, txd4); |
| 731 | WRITE_ONCE(itxd->txd3, (TX_DMA_SWC | TX_DMA_PLEN0(skb_headlen(skb)) | |
| 732 | (!nr_frags * TX_DMA_LS0))); |
| 733 | |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 734 | netdev_sent_queue(dev, skb->len); |
| 735 | skb_tx_timestamp(skb); |
| 736 | |
| 737 | ring->next_free = mtk_qdma_phys_to_virt(ring, txd->txd2); |
| 738 | atomic_sub(n_desc, &ring->free_count); |
| 739 | |
| 740 | /* make sure that all changes to the dma ring are flushed before we |
| 741 | * continue |
| 742 | */ |
| 743 | wmb(); |
| 744 | |
| 745 | if (netif_xmit_stopped(netdev_get_tx_queue(dev, 0)) || !skb->xmit_more) |
| 746 | mtk_w32(eth, txd->txd2, MTK_QTX_CTX_PTR); |
| 747 | |
| 748 | return 0; |
| 749 | |
| 750 | err_dma: |
| 751 | do { |
John Crispin | 2fae723 | 2016-06-10 13:28:00 +0200 | [diff] [blame] | 752 | tx_buf = mtk_desc_to_tx_buf(ring, itxd); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 753 | |
| 754 | /* unmap dma */ |
sean.wang@mediatek.com | 55a4e77 | 2016-08-16 13:55:15 +0800 | [diff] [blame] | 755 | mtk_tx_unmap(eth, tx_buf); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 756 | |
| 757 | itxd->txd3 = TX_DMA_LS0 | TX_DMA_OWNER_CPU; |
| 758 | itxd = mtk_qdma_phys_to_virt(ring, itxd->txd2); |
| 759 | } while (itxd != txd); |
| 760 | |
| 761 | return -ENOMEM; |
| 762 | } |
| 763 | |
| 764 | static inline int mtk_cal_txd_req(struct sk_buff *skb) |
| 765 | { |
| 766 | int i, nfrags; |
| 767 | struct skb_frag_struct *frag; |
| 768 | |
| 769 | nfrags = 1; |
| 770 | if (skb_is_gso(skb)) { |
| 771 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 772 | frag = &skb_shinfo(skb)->frags[i]; |
| 773 | nfrags += DIV_ROUND_UP(frag->size, MTK_TX_DMA_BUF_LEN); |
| 774 | } |
| 775 | } else { |
| 776 | nfrags += skb_shinfo(skb)->nr_frags; |
| 777 | } |
| 778 | |
John Crispin | beeb4ca | 2016-04-08 00:54:05 +0200 | [diff] [blame] | 779 | return nfrags; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 780 | } |
| 781 | |
John Crispin | ad3cba9 | 2016-06-10 13:28:07 +0200 | [diff] [blame] | 782 | static int mtk_queue_stopped(struct mtk_eth *eth) |
| 783 | { |
| 784 | int i; |
| 785 | |
| 786 | for (i = 0; i < MTK_MAC_COUNT; i++) { |
| 787 | if (!eth->netdev[i]) |
| 788 | continue; |
| 789 | if (netif_queue_stopped(eth->netdev[i])) |
| 790 | return 1; |
| 791 | } |
| 792 | |
| 793 | return 0; |
| 794 | } |
| 795 | |
John Crispin | 13c822f | 2016-04-08 00:54:07 +0200 | [diff] [blame] | 796 | static void mtk_wake_queue(struct mtk_eth *eth) |
| 797 | { |
| 798 | int i; |
| 799 | |
| 800 | for (i = 0; i < MTK_MAC_COUNT; i++) { |
| 801 | if (!eth->netdev[i]) |
| 802 | continue; |
| 803 | netif_wake_queue(eth->netdev[i]); |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | static void mtk_stop_queue(struct mtk_eth *eth) |
| 808 | { |
| 809 | int i; |
| 810 | |
| 811 | for (i = 0; i < MTK_MAC_COUNT; i++) { |
| 812 | if (!eth->netdev[i]) |
| 813 | continue; |
| 814 | netif_stop_queue(eth->netdev[i]); |
| 815 | } |
| 816 | } |
| 817 | |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 818 | static int mtk_start_xmit(struct sk_buff *skb, struct net_device *dev) |
| 819 | { |
| 820 | struct mtk_mac *mac = netdev_priv(dev); |
| 821 | struct mtk_eth *eth = mac->hw; |
| 822 | struct mtk_tx_ring *ring = ð->tx_ring; |
| 823 | struct net_device_stats *stats = &dev->stats; |
| 824 | bool gso = false; |
| 825 | int tx_num; |
| 826 | |
John Crispin | 34c2e4c | 2016-04-08 00:54:08 +0200 | [diff] [blame] | 827 | /* normally we can rely on the stack not calling this more than once, |
| 828 | * however we have 2 queues running on the same ring so we need to lock |
| 829 | * the ring access |
| 830 | */ |
Sean Wang | e3e9652 | 2016-08-11 17:51:00 +0800 | [diff] [blame] | 831 | spin_lock(ð->page_lock); |
John Crispin | 34c2e4c | 2016-04-08 00:54:08 +0200 | [diff] [blame] | 832 | |
Sean Wang | dce6fa4 | 2016-09-14 23:13:21 +0800 | [diff] [blame] | 833 | if (unlikely(test_bit(MTK_RESETTING, ð->state))) |
| 834 | goto drop; |
| 835 | |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 836 | tx_num = mtk_cal_txd_req(skb); |
| 837 | if (unlikely(atomic_read(&ring->free_count) <= tx_num)) { |
John Crispin | 13c822f | 2016-04-08 00:54:07 +0200 | [diff] [blame] | 838 | mtk_stop_queue(eth); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 839 | netif_err(eth, tx_queued, dev, |
| 840 | "Tx Ring full when queue awake!\n"); |
Sean Wang | e3e9652 | 2016-08-11 17:51:00 +0800 | [diff] [blame] | 841 | spin_unlock(ð->page_lock); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 842 | return NETDEV_TX_BUSY; |
| 843 | } |
| 844 | |
| 845 | /* TSO: fill MSS info in tcp checksum field */ |
| 846 | if (skb_is_gso(skb)) { |
| 847 | if (skb_cow_head(skb, 0)) { |
| 848 | netif_warn(eth, tx_err, dev, |
| 849 | "GSO expand head fail.\n"); |
| 850 | goto drop; |
| 851 | } |
| 852 | |
| 853 | if (skb_shinfo(skb)->gso_type & |
| 854 | (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)) { |
| 855 | gso = true; |
| 856 | tcp_hdr(skb)->check = htons(skb_shinfo(skb)->gso_size); |
| 857 | } |
| 858 | } |
| 859 | |
| 860 | if (mtk_tx_map(skb, dev, tx_num, ring, gso) < 0) |
| 861 | goto drop; |
| 862 | |
John Crispin | 82c6544 | 2016-06-10 13:28:08 +0200 | [diff] [blame] | 863 | if (unlikely(atomic_read(&ring->free_count) <= ring->thresh)) |
John Crispin | 13c822f | 2016-04-08 00:54:07 +0200 | [diff] [blame] | 864 | mtk_stop_queue(eth); |
John Crispin | 82c6544 | 2016-06-10 13:28:08 +0200 | [diff] [blame] | 865 | |
Sean Wang | e3e9652 | 2016-08-11 17:51:00 +0800 | [diff] [blame] | 866 | spin_unlock(ð->page_lock); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 867 | |
| 868 | return NETDEV_TX_OK; |
| 869 | |
| 870 | drop: |
Sean Wang | e3e9652 | 2016-08-11 17:51:00 +0800 | [diff] [blame] | 871 | spin_unlock(ð->page_lock); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 872 | stats->tx_dropped++; |
Wei Yongjun | 81ad2b7 | 2016-10-20 17:00:32 +0000 | [diff] [blame] | 873 | dev_kfree_skb_any(skb); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 874 | return NETDEV_TX_OK; |
| 875 | } |
| 876 | |
Nelson Chang | ee40681 | 2016-09-17 23:50:55 +0800 | [diff] [blame] | 877 | static struct mtk_rx_ring *mtk_get_rx_ring(struct mtk_eth *eth) |
| 878 | { |
| 879 | int i; |
| 880 | struct mtk_rx_ring *ring; |
| 881 | int idx; |
| 882 | |
| 883 | if (!eth->hwlro) |
| 884 | return ð->rx_ring[0]; |
| 885 | |
| 886 | for (i = 0; i < MTK_MAX_RX_RING_NUM; i++) { |
| 887 | ring = ð->rx_ring[i]; |
| 888 | idx = NEXT_RX_DESP_IDX(ring->calc_idx, ring->dma_size); |
| 889 | if (ring->dma[idx].rxd2 & RX_DMA_DONE) { |
| 890 | ring->calc_idx_update = true; |
| 891 | return ring; |
| 892 | } |
| 893 | } |
| 894 | |
| 895 | return NULL; |
| 896 | } |
| 897 | |
| 898 | static void mtk_update_rx_cpu_idx(struct mtk_eth *eth) |
| 899 | { |
| 900 | struct mtk_rx_ring *ring; |
| 901 | int i; |
| 902 | |
| 903 | if (!eth->hwlro) { |
| 904 | ring = ð->rx_ring[0]; |
| 905 | mtk_w32(eth, ring->calc_idx, ring->crx_idx_reg); |
| 906 | } else { |
| 907 | for (i = 0; i < MTK_MAX_RX_RING_NUM; i++) { |
| 908 | ring = ð->rx_ring[i]; |
| 909 | if (ring->calc_idx_update) { |
| 910 | ring->calc_idx_update = false; |
| 911 | mtk_w32(eth, ring->calc_idx, ring->crx_idx_reg); |
| 912 | } |
| 913 | } |
| 914 | } |
| 915 | } |
| 916 | |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 917 | static int mtk_poll_rx(struct napi_struct *napi, int budget, |
John Crispin | eece71e | 2016-06-29 13:38:09 +0200 | [diff] [blame] | 918 | struct mtk_eth *eth) |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 919 | { |
Nelson Chang | ee40681 | 2016-09-17 23:50:55 +0800 | [diff] [blame] | 920 | struct mtk_rx_ring *ring; |
| 921 | int idx; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 922 | struct sk_buff *skb; |
| 923 | u8 *data, *new_data; |
| 924 | struct mtk_rx_dma *rxd, trxd; |
| 925 | int done = 0; |
| 926 | |
| 927 | while (done < budget) { |
| 928 | struct net_device *netdev; |
| 929 | unsigned int pktlen; |
| 930 | dma_addr_t dma_addr; |
| 931 | int mac = 0; |
| 932 | |
Nelson Chang | ee40681 | 2016-09-17 23:50:55 +0800 | [diff] [blame] | 933 | ring = mtk_get_rx_ring(eth); |
| 934 | if (unlikely(!ring)) |
| 935 | goto rx_done; |
| 936 | |
| 937 | idx = NEXT_RX_DESP_IDX(ring->calc_idx, ring->dma_size); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 938 | rxd = &ring->dma[idx]; |
| 939 | data = ring->data[idx]; |
| 940 | |
| 941 | mtk_rx_get_desc(&trxd, rxd); |
| 942 | if (!(trxd.rxd2 & RX_DMA_DONE)) |
| 943 | break; |
| 944 | |
| 945 | /* find out which mac the packet come from. values start at 1 */ |
| 946 | mac = (trxd.rxd4 >> RX_DMA_FPORT_SHIFT) & |
| 947 | RX_DMA_FPORT_MASK; |
| 948 | mac--; |
| 949 | |
| 950 | netdev = eth->netdev[mac]; |
| 951 | |
Sean Wang | dce6fa4 | 2016-09-14 23:13:21 +0800 | [diff] [blame] | 952 | if (unlikely(test_bit(MTK_RESETTING, ð->state))) |
| 953 | goto release_desc; |
| 954 | |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 955 | /* alloc new buffer */ |
| 956 | new_data = napi_alloc_frag(ring->frag_size); |
| 957 | if (unlikely(!new_data)) { |
| 958 | netdev->stats.rx_dropped++; |
| 959 | goto release_desc; |
| 960 | } |
sean.wang@mediatek.com | 55a4e77 | 2016-08-16 13:55:15 +0800 | [diff] [blame] | 961 | dma_addr = dma_map_single(eth->dev, |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 962 | new_data + NET_SKB_PAD, |
| 963 | ring->buf_size, |
| 964 | DMA_FROM_DEVICE); |
sean.wang@mediatek.com | 55a4e77 | 2016-08-16 13:55:15 +0800 | [diff] [blame] | 965 | if (unlikely(dma_mapping_error(eth->dev, dma_addr))) { |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 966 | skb_free_frag(new_data); |
John Crispin | 94321a9 | 2016-06-10 13:28:01 +0200 | [diff] [blame] | 967 | netdev->stats.rx_dropped++; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 968 | goto release_desc; |
| 969 | } |
| 970 | |
| 971 | /* receive data */ |
| 972 | skb = build_skb(data, ring->frag_size); |
| 973 | if (unlikely(!skb)) { |
Sean Wang | 1b43079 | 2016-09-01 10:47:29 +0800 | [diff] [blame] | 974 | skb_free_frag(new_data); |
John Crispin | 94321a9 | 2016-06-10 13:28:01 +0200 | [diff] [blame] | 975 | netdev->stats.rx_dropped++; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 976 | goto release_desc; |
| 977 | } |
| 978 | skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); |
| 979 | |
sean.wang@mediatek.com | 55a4e77 | 2016-08-16 13:55:15 +0800 | [diff] [blame] | 980 | dma_unmap_single(eth->dev, trxd.rxd1, |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 981 | ring->buf_size, DMA_FROM_DEVICE); |
| 982 | pktlen = RX_DMA_GET_PLEN0(trxd.rxd2); |
| 983 | skb->dev = netdev; |
| 984 | skb_put(skb, pktlen); |
| 985 | if (trxd.rxd4 & RX_DMA_L4_VALID) |
| 986 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 987 | else |
| 988 | skb_checksum_none_assert(skb); |
| 989 | skb->protocol = eth_type_trans(skb, netdev); |
| 990 | |
| 991 | if (netdev->features & NETIF_F_HW_VLAN_CTAG_RX && |
| 992 | RX_DMA_VID(trxd.rxd3)) |
| 993 | __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), |
| 994 | RX_DMA_VID(trxd.rxd3)); |
| 995 | napi_gro_receive(napi, skb); |
| 996 | |
| 997 | ring->data[idx] = new_data; |
| 998 | rxd->rxd1 = (unsigned int)dma_addr; |
| 999 | |
| 1000 | release_desc: |
| 1001 | rxd->rxd2 = RX_DMA_PLEN0(ring->buf_size); |
| 1002 | |
| 1003 | ring->calc_idx = idx; |
Sean Wang | 635372a | 2016-09-03 17:59:26 +0800 | [diff] [blame] | 1004 | |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1005 | done++; |
| 1006 | } |
| 1007 | |
Nelson Chang | ee40681 | 2016-09-17 23:50:55 +0800 | [diff] [blame] | 1008 | rx_done: |
Sean Wang | 41156ce | 2016-09-03 17:59:27 +0800 | [diff] [blame] | 1009 | if (done) { |
| 1010 | /* make sure that all changes to the dma ring are flushed before |
| 1011 | * we continue |
| 1012 | */ |
| 1013 | wmb(); |
Nelson Chang | ee40681 | 2016-09-17 23:50:55 +0800 | [diff] [blame] | 1014 | mtk_update_rx_cpu_idx(eth); |
Sean Wang | 41156ce | 2016-09-03 17:59:27 +0800 | [diff] [blame] | 1015 | } |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1016 | |
| 1017 | return done; |
| 1018 | } |
| 1019 | |
John Crispin | 8067302 | 2016-06-29 13:38:11 +0200 | [diff] [blame] | 1020 | static int mtk_poll_tx(struct mtk_eth *eth, int budget) |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1021 | { |
| 1022 | struct mtk_tx_ring *ring = ð->tx_ring; |
| 1023 | struct mtk_tx_dma *desc; |
| 1024 | struct sk_buff *skb; |
| 1025 | struct mtk_tx_buf *tx_buf; |
John Crispin | 8067302 | 2016-06-29 13:38:11 +0200 | [diff] [blame] | 1026 | unsigned int done[MTK_MAX_DEVS]; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1027 | unsigned int bytes[MTK_MAX_DEVS]; |
| 1028 | u32 cpu, dma; |
| 1029 | static int condition; |
John Crispin | 8067302 | 2016-06-29 13:38:11 +0200 | [diff] [blame] | 1030 | int total = 0, i; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1031 | |
| 1032 | memset(done, 0, sizeof(done)); |
| 1033 | memset(bytes, 0, sizeof(bytes)); |
| 1034 | |
| 1035 | cpu = mtk_r32(eth, MTK_QTX_CRX_PTR); |
| 1036 | dma = mtk_r32(eth, MTK_QTX_DRX_PTR); |
| 1037 | |
| 1038 | desc = mtk_qdma_phys_to_virt(ring, cpu); |
| 1039 | |
| 1040 | while ((cpu != dma) && budget) { |
| 1041 | u32 next_cpu = desc->txd2; |
Sean Wang | 134d215 | 2017-04-14 11:19:12 +0800 | [diff] [blame] | 1042 | int mac = 0; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1043 | |
| 1044 | desc = mtk_qdma_phys_to_virt(ring, desc->txd2); |
| 1045 | if ((desc->txd3 & TX_DMA_OWNER_CPU) == 0) |
| 1046 | break; |
| 1047 | |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1048 | tx_buf = mtk_desc_to_tx_buf(ring, desc); |
Sean Wang | 134d215 | 2017-04-14 11:19:12 +0800 | [diff] [blame] | 1049 | if (tx_buf->flags & MTK_TX_FLAGS_FPORT1) |
| 1050 | mac = 1; |
| 1051 | |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1052 | skb = tx_buf->skb; |
| 1053 | if (!skb) { |
| 1054 | condition = 1; |
| 1055 | break; |
| 1056 | } |
| 1057 | |
| 1058 | if (skb != (struct sk_buff *)MTK_DMA_DUMMY_DESC) { |
| 1059 | bytes[mac] += skb->len; |
| 1060 | done[mac]++; |
| 1061 | budget--; |
| 1062 | } |
sean.wang@mediatek.com | 55a4e77 | 2016-08-16 13:55:15 +0800 | [diff] [blame] | 1063 | mtk_tx_unmap(eth, tx_buf); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1064 | |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1065 | ring->last_free = desc; |
| 1066 | atomic_inc(&ring->free_count); |
| 1067 | |
| 1068 | cpu = next_cpu; |
| 1069 | } |
| 1070 | |
| 1071 | mtk_w32(eth, cpu, MTK_QTX_CRX_PTR); |
| 1072 | |
| 1073 | for (i = 0; i < MTK_MAC_COUNT; i++) { |
| 1074 | if (!eth->netdev[i] || !done[i]) |
| 1075 | continue; |
| 1076 | netdev_completed_queue(eth->netdev[i], done[i], bytes[i]); |
| 1077 | total += done[i]; |
| 1078 | } |
| 1079 | |
John Crispin | ad3cba9 | 2016-06-10 13:28:07 +0200 | [diff] [blame] | 1080 | if (mtk_queue_stopped(eth) && |
| 1081 | (atomic_read(&ring->free_count) > ring->thresh)) |
John Crispin | 13c822f | 2016-04-08 00:54:07 +0200 | [diff] [blame] | 1082 | mtk_wake_queue(eth); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1083 | |
| 1084 | return total; |
| 1085 | } |
| 1086 | |
John Crispin | 8067302 | 2016-06-29 13:38:11 +0200 | [diff] [blame] | 1087 | static void mtk_handle_status_irq(struct mtk_eth *eth) |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1088 | { |
John Crispin | 8067302 | 2016-06-29 13:38:11 +0200 | [diff] [blame] | 1089 | u32 status2 = mtk_r32(eth, MTK_INT_STATUS2); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1090 | |
John Crispin | eece71e | 2016-06-29 13:38:09 +0200 | [diff] [blame] | 1091 | if (unlikely(status2 & (MTK_GDM1_AF | MTK_GDM2_AF))) { |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1092 | mtk_stats_update(eth); |
John Crispin | eece71e | 2016-06-29 13:38:09 +0200 | [diff] [blame] | 1093 | mtk_w32(eth, (MTK_GDM1_AF | MTK_GDM2_AF), |
| 1094 | MTK_INT_STATUS2); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1095 | } |
John Crispin | 8067302 | 2016-06-29 13:38:11 +0200 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | static int mtk_napi_tx(struct napi_struct *napi, int budget) |
| 1099 | { |
| 1100 | struct mtk_eth *eth = container_of(napi, struct mtk_eth, tx_napi); |
| 1101 | u32 status, mask; |
| 1102 | int tx_done = 0; |
| 1103 | |
| 1104 | mtk_handle_status_irq(eth); |
| 1105 | mtk_w32(eth, MTK_TX_DONE_INT, MTK_QMTK_INT_STATUS); |
| 1106 | tx_done = mtk_poll_tx(eth, budget); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1107 | |
| 1108 | if (unlikely(netif_msg_intr(eth))) { |
John Crispin | 8067302 | 2016-06-29 13:38:11 +0200 | [diff] [blame] | 1109 | status = mtk_r32(eth, MTK_QMTK_INT_STATUS); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1110 | mask = mtk_r32(eth, MTK_QDMA_INT_MASK); |
John Crispin | 8067302 | 2016-06-29 13:38:11 +0200 | [diff] [blame] | 1111 | dev_info(eth->dev, |
| 1112 | "done tx %d, intr 0x%08x/0x%x\n", |
| 1113 | tx_done, status, mask); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1114 | } |
| 1115 | |
John Crispin | 8067302 | 2016-06-29 13:38:11 +0200 | [diff] [blame] | 1116 | if (tx_done == budget) |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1117 | return budget; |
| 1118 | |
| 1119 | status = mtk_r32(eth, MTK_QMTK_INT_STATUS); |
John Crispin | 8067302 | 2016-06-29 13:38:11 +0200 | [diff] [blame] | 1120 | if (status & MTK_TX_DONE_INT) |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1121 | return budget; |
| 1122 | |
| 1123 | napi_complete(napi); |
John Crispin | 5cce032 | 2017-06-19 15:37:05 +0200 | [diff] [blame^] | 1124 | mtk_tx_irq_enable(eth, MTK_TX_DONE_INT); |
John Crispin | 8067302 | 2016-06-29 13:38:11 +0200 | [diff] [blame] | 1125 | |
| 1126 | return tx_done; |
| 1127 | } |
| 1128 | |
| 1129 | static int mtk_napi_rx(struct napi_struct *napi, int budget) |
| 1130 | { |
| 1131 | struct mtk_eth *eth = container_of(napi, struct mtk_eth, rx_napi); |
| 1132 | u32 status, mask; |
| 1133 | int rx_done = 0; |
Sean Wang | 41156ce | 2016-09-03 17:59:27 +0800 | [diff] [blame] | 1134 | int remain_budget = budget; |
John Crispin | 8067302 | 2016-06-29 13:38:11 +0200 | [diff] [blame] | 1135 | |
| 1136 | mtk_handle_status_irq(eth); |
Sean Wang | 41156ce | 2016-09-03 17:59:27 +0800 | [diff] [blame] | 1137 | |
| 1138 | poll_again: |
Nelson Chang | bacfd11 | 2016-08-26 01:09:42 +0800 | [diff] [blame] | 1139 | mtk_w32(eth, MTK_RX_DONE_INT, MTK_PDMA_INT_STATUS); |
Sean Wang | 41156ce | 2016-09-03 17:59:27 +0800 | [diff] [blame] | 1140 | rx_done = mtk_poll_rx(napi, remain_budget, eth); |
John Crispin | 8067302 | 2016-06-29 13:38:11 +0200 | [diff] [blame] | 1141 | |
| 1142 | if (unlikely(netif_msg_intr(eth))) { |
Nelson Chang | bacfd11 | 2016-08-26 01:09:42 +0800 | [diff] [blame] | 1143 | status = mtk_r32(eth, MTK_PDMA_INT_STATUS); |
| 1144 | mask = mtk_r32(eth, MTK_PDMA_INT_MASK); |
John Crispin | 8067302 | 2016-06-29 13:38:11 +0200 | [diff] [blame] | 1145 | dev_info(eth->dev, |
| 1146 | "done rx %d, intr 0x%08x/0x%x\n", |
| 1147 | rx_done, status, mask); |
| 1148 | } |
Sean Wang | 41156ce | 2016-09-03 17:59:27 +0800 | [diff] [blame] | 1149 | if (rx_done == remain_budget) |
John Crispin | 8067302 | 2016-06-29 13:38:11 +0200 | [diff] [blame] | 1150 | return budget; |
| 1151 | |
Nelson Chang | bacfd11 | 2016-08-26 01:09:42 +0800 | [diff] [blame] | 1152 | status = mtk_r32(eth, MTK_PDMA_INT_STATUS); |
Sean Wang | 41156ce | 2016-09-03 17:59:27 +0800 | [diff] [blame] | 1153 | if (status & MTK_RX_DONE_INT) { |
| 1154 | remain_budget -= rx_done; |
| 1155 | goto poll_again; |
| 1156 | } |
John Crispin | 8067302 | 2016-06-29 13:38:11 +0200 | [diff] [blame] | 1157 | napi_complete(napi); |
John Crispin | 5cce032 | 2017-06-19 15:37:05 +0200 | [diff] [blame^] | 1158 | mtk_rx_irq_enable(eth, MTK_RX_DONE_INT); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1159 | |
Sean Wang | 41156ce | 2016-09-03 17:59:27 +0800 | [diff] [blame] | 1160 | return rx_done + budget - remain_budget; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | static int mtk_tx_alloc(struct mtk_eth *eth) |
| 1164 | { |
| 1165 | struct mtk_tx_ring *ring = ð->tx_ring; |
| 1166 | int i, sz = sizeof(*ring->dma); |
| 1167 | |
| 1168 | ring->buf = kcalloc(MTK_DMA_SIZE, sizeof(*ring->buf), |
| 1169 | GFP_KERNEL); |
| 1170 | if (!ring->buf) |
| 1171 | goto no_tx_mem; |
| 1172 | |
| 1173 | ring->dma = dma_alloc_coherent(eth->dev, |
| 1174 | MTK_DMA_SIZE * sz, |
| 1175 | &ring->phys, |
| 1176 | GFP_ATOMIC | __GFP_ZERO); |
| 1177 | if (!ring->dma) |
| 1178 | goto no_tx_mem; |
| 1179 | |
| 1180 | memset(ring->dma, 0, MTK_DMA_SIZE * sz); |
| 1181 | for (i = 0; i < MTK_DMA_SIZE; i++) { |
| 1182 | int next = (i + 1) % MTK_DMA_SIZE; |
| 1183 | u32 next_ptr = ring->phys + next * sz; |
| 1184 | |
| 1185 | ring->dma[i].txd2 = next_ptr; |
| 1186 | ring->dma[i].txd3 = TX_DMA_LS0 | TX_DMA_OWNER_CPU; |
| 1187 | } |
| 1188 | |
| 1189 | atomic_set(&ring->free_count, MTK_DMA_SIZE - 2); |
| 1190 | ring->next_free = &ring->dma[0]; |
John Crispin | 12c97c1 | 2016-06-10 13:28:06 +0200 | [diff] [blame] | 1191 | ring->last_free = &ring->dma[MTK_DMA_SIZE - 1]; |
John Crispin | 04698cc | 2016-06-10 13:28:04 +0200 | [diff] [blame] | 1192 | ring->thresh = MAX_SKB_FRAGS; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1193 | |
| 1194 | /* make sure that all changes to the dma ring are flushed before we |
| 1195 | * continue |
| 1196 | */ |
| 1197 | wmb(); |
| 1198 | |
| 1199 | mtk_w32(eth, ring->phys, MTK_QTX_CTX_PTR); |
| 1200 | mtk_w32(eth, ring->phys, MTK_QTX_DTX_PTR); |
| 1201 | mtk_w32(eth, |
| 1202 | ring->phys + ((MTK_DMA_SIZE - 1) * sz), |
| 1203 | MTK_QTX_CRX_PTR); |
| 1204 | mtk_w32(eth, |
| 1205 | ring->phys + ((MTK_DMA_SIZE - 1) * sz), |
| 1206 | MTK_QTX_DRX_PTR); |
Nelson Chang | bacfd11 | 2016-08-26 01:09:42 +0800 | [diff] [blame] | 1207 | mtk_w32(eth, (QDMA_RES_THRES << 8) | QDMA_RES_THRES, MTK_QTX_CFG(0)); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1208 | |
| 1209 | return 0; |
| 1210 | |
| 1211 | no_tx_mem: |
| 1212 | return -ENOMEM; |
| 1213 | } |
| 1214 | |
| 1215 | static void mtk_tx_clean(struct mtk_eth *eth) |
| 1216 | { |
| 1217 | struct mtk_tx_ring *ring = ð->tx_ring; |
| 1218 | int i; |
| 1219 | |
| 1220 | if (ring->buf) { |
| 1221 | for (i = 0; i < MTK_DMA_SIZE; i++) |
sean.wang@mediatek.com | 55a4e77 | 2016-08-16 13:55:15 +0800 | [diff] [blame] | 1222 | mtk_tx_unmap(eth, &ring->buf[i]); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1223 | kfree(ring->buf); |
| 1224 | ring->buf = NULL; |
| 1225 | } |
| 1226 | |
| 1227 | if (ring->dma) { |
| 1228 | dma_free_coherent(eth->dev, |
| 1229 | MTK_DMA_SIZE * sizeof(*ring->dma), |
| 1230 | ring->dma, |
| 1231 | ring->phys); |
| 1232 | ring->dma = NULL; |
| 1233 | } |
| 1234 | } |
| 1235 | |
Nelson Chang | ee40681 | 2016-09-17 23:50:55 +0800 | [diff] [blame] | 1236 | static int mtk_rx_alloc(struct mtk_eth *eth, int ring_no, int rx_flag) |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1237 | { |
Nelson Chang | ee40681 | 2016-09-17 23:50:55 +0800 | [diff] [blame] | 1238 | struct mtk_rx_ring *ring = ð->rx_ring[ring_no]; |
| 1239 | int rx_data_len, rx_dma_size; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1240 | int i; |
| 1241 | |
Nelson Chang | ee40681 | 2016-09-17 23:50:55 +0800 | [diff] [blame] | 1242 | if (rx_flag == MTK_RX_FLAGS_HWLRO) { |
| 1243 | rx_data_len = MTK_MAX_LRO_RX_LENGTH; |
| 1244 | rx_dma_size = MTK_HW_LRO_DMA_SIZE; |
| 1245 | } else { |
| 1246 | rx_data_len = ETH_DATA_LEN; |
| 1247 | rx_dma_size = MTK_DMA_SIZE; |
| 1248 | } |
| 1249 | |
| 1250 | ring->frag_size = mtk_max_frag_size(rx_data_len); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1251 | ring->buf_size = mtk_max_buf_size(ring->frag_size); |
Nelson Chang | ee40681 | 2016-09-17 23:50:55 +0800 | [diff] [blame] | 1252 | ring->data = kcalloc(rx_dma_size, sizeof(*ring->data), |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1253 | GFP_KERNEL); |
| 1254 | if (!ring->data) |
| 1255 | return -ENOMEM; |
| 1256 | |
Nelson Chang | ee40681 | 2016-09-17 23:50:55 +0800 | [diff] [blame] | 1257 | for (i = 0; i < rx_dma_size; i++) { |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1258 | ring->data[i] = netdev_alloc_frag(ring->frag_size); |
| 1259 | if (!ring->data[i]) |
| 1260 | return -ENOMEM; |
| 1261 | } |
| 1262 | |
| 1263 | ring->dma = dma_alloc_coherent(eth->dev, |
Nelson Chang | ee40681 | 2016-09-17 23:50:55 +0800 | [diff] [blame] | 1264 | rx_dma_size * sizeof(*ring->dma), |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1265 | &ring->phys, |
| 1266 | GFP_ATOMIC | __GFP_ZERO); |
| 1267 | if (!ring->dma) |
| 1268 | return -ENOMEM; |
| 1269 | |
Nelson Chang | ee40681 | 2016-09-17 23:50:55 +0800 | [diff] [blame] | 1270 | for (i = 0; i < rx_dma_size; i++) { |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1271 | dma_addr_t dma_addr = dma_map_single(eth->dev, |
| 1272 | ring->data[i] + NET_SKB_PAD, |
| 1273 | ring->buf_size, |
| 1274 | DMA_FROM_DEVICE); |
| 1275 | if (unlikely(dma_mapping_error(eth->dev, dma_addr))) |
| 1276 | return -ENOMEM; |
| 1277 | ring->dma[i].rxd1 = (unsigned int)dma_addr; |
| 1278 | |
| 1279 | ring->dma[i].rxd2 = RX_DMA_PLEN0(ring->buf_size); |
| 1280 | } |
Nelson Chang | ee40681 | 2016-09-17 23:50:55 +0800 | [diff] [blame] | 1281 | ring->dma_size = rx_dma_size; |
| 1282 | ring->calc_idx_update = false; |
| 1283 | ring->calc_idx = rx_dma_size - 1; |
| 1284 | ring->crx_idx_reg = MTK_PRX_CRX_IDX_CFG(ring_no); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1285 | /* make sure that all changes to the dma ring are flushed before we |
| 1286 | * continue |
| 1287 | */ |
| 1288 | wmb(); |
| 1289 | |
Nelson Chang | ee40681 | 2016-09-17 23:50:55 +0800 | [diff] [blame] | 1290 | mtk_w32(eth, ring->phys, MTK_PRX_BASE_PTR_CFG(ring_no)); |
| 1291 | mtk_w32(eth, rx_dma_size, MTK_PRX_MAX_CNT_CFG(ring_no)); |
| 1292 | mtk_w32(eth, ring->calc_idx, ring->crx_idx_reg); |
| 1293 | mtk_w32(eth, MTK_PST_DRX_IDX_CFG(ring_no), MTK_PDMA_RST_IDX); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1294 | |
| 1295 | return 0; |
| 1296 | } |
| 1297 | |
Nelson Chang | ee40681 | 2016-09-17 23:50:55 +0800 | [diff] [blame] | 1298 | static void mtk_rx_clean(struct mtk_eth *eth, int ring_no) |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1299 | { |
Nelson Chang | ee40681 | 2016-09-17 23:50:55 +0800 | [diff] [blame] | 1300 | struct mtk_rx_ring *ring = ð->rx_ring[ring_no]; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1301 | int i; |
| 1302 | |
| 1303 | if (ring->data && ring->dma) { |
Nelson Chang | ee40681 | 2016-09-17 23:50:55 +0800 | [diff] [blame] | 1304 | for (i = 0; i < ring->dma_size; i++) { |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1305 | if (!ring->data[i]) |
| 1306 | continue; |
| 1307 | if (!ring->dma[i].rxd1) |
| 1308 | continue; |
| 1309 | dma_unmap_single(eth->dev, |
| 1310 | ring->dma[i].rxd1, |
| 1311 | ring->buf_size, |
| 1312 | DMA_FROM_DEVICE); |
| 1313 | skb_free_frag(ring->data[i]); |
| 1314 | } |
| 1315 | kfree(ring->data); |
| 1316 | ring->data = NULL; |
| 1317 | } |
| 1318 | |
| 1319 | if (ring->dma) { |
| 1320 | dma_free_coherent(eth->dev, |
Nelson Chang | ee40681 | 2016-09-17 23:50:55 +0800 | [diff] [blame] | 1321 | ring->dma_size * sizeof(*ring->dma), |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1322 | ring->dma, |
| 1323 | ring->phys); |
| 1324 | ring->dma = NULL; |
| 1325 | } |
| 1326 | } |
| 1327 | |
Nelson Chang | ee40681 | 2016-09-17 23:50:55 +0800 | [diff] [blame] | 1328 | static int mtk_hwlro_rx_init(struct mtk_eth *eth) |
| 1329 | { |
| 1330 | int i; |
| 1331 | u32 ring_ctrl_dw1 = 0, ring_ctrl_dw2 = 0, ring_ctrl_dw3 = 0; |
| 1332 | u32 lro_ctrl_dw0 = 0, lro_ctrl_dw3 = 0; |
| 1333 | |
| 1334 | /* set LRO rings to auto-learn modes */ |
| 1335 | ring_ctrl_dw2 |= MTK_RING_AUTO_LERAN_MODE; |
| 1336 | |
| 1337 | /* validate LRO ring */ |
| 1338 | ring_ctrl_dw2 |= MTK_RING_VLD; |
| 1339 | |
| 1340 | /* set AGE timer (unit: 20us) */ |
| 1341 | ring_ctrl_dw2 |= MTK_RING_AGE_TIME_H; |
| 1342 | ring_ctrl_dw1 |= MTK_RING_AGE_TIME_L; |
| 1343 | |
| 1344 | /* set max AGG timer (unit: 20us) */ |
| 1345 | ring_ctrl_dw2 |= MTK_RING_MAX_AGG_TIME; |
| 1346 | |
| 1347 | /* set max LRO AGG count */ |
| 1348 | ring_ctrl_dw2 |= MTK_RING_MAX_AGG_CNT_L; |
| 1349 | ring_ctrl_dw3 |= MTK_RING_MAX_AGG_CNT_H; |
| 1350 | |
| 1351 | for (i = 1; i < MTK_MAX_RX_RING_NUM; i++) { |
| 1352 | mtk_w32(eth, ring_ctrl_dw1, MTK_LRO_CTRL_DW1_CFG(i)); |
| 1353 | mtk_w32(eth, ring_ctrl_dw2, MTK_LRO_CTRL_DW2_CFG(i)); |
| 1354 | mtk_w32(eth, ring_ctrl_dw3, MTK_LRO_CTRL_DW3_CFG(i)); |
| 1355 | } |
| 1356 | |
| 1357 | /* IPv4 checksum update enable */ |
| 1358 | lro_ctrl_dw0 |= MTK_L3_CKS_UPD_EN; |
| 1359 | |
| 1360 | /* switch priority comparison to packet count mode */ |
| 1361 | lro_ctrl_dw0 |= MTK_LRO_ALT_PKT_CNT_MODE; |
| 1362 | |
| 1363 | /* bandwidth threshold setting */ |
| 1364 | mtk_w32(eth, MTK_HW_LRO_BW_THRE, MTK_PDMA_LRO_CTRL_DW2); |
| 1365 | |
| 1366 | /* auto-learn score delta setting */ |
| 1367 | mtk_w32(eth, MTK_HW_LRO_REPLACE_DELTA, MTK_PDMA_LRO_ALT_SCORE_DELTA); |
| 1368 | |
| 1369 | /* set refresh timer for altering flows to 1 sec. (unit: 20us) */ |
| 1370 | mtk_w32(eth, (MTK_HW_LRO_TIMER_UNIT << 16) | MTK_HW_LRO_REFRESH_TIME, |
| 1371 | MTK_PDMA_LRO_ALT_REFRESH_TIMER); |
| 1372 | |
| 1373 | /* set HW LRO mode & the max aggregation count for rx packets */ |
| 1374 | lro_ctrl_dw3 |= MTK_ADMA_MODE | (MTK_HW_LRO_MAX_AGG_CNT & 0xff); |
| 1375 | |
| 1376 | /* the minimal remaining room of SDL0 in RXD for lro aggregation */ |
| 1377 | lro_ctrl_dw3 |= MTK_LRO_MIN_RXD_SDL; |
| 1378 | |
| 1379 | /* enable HW LRO */ |
| 1380 | lro_ctrl_dw0 |= MTK_LRO_EN; |
| 1381 | |
| 1382 | mtk_w32(eth, lro_ctrl_dw3, MTK_PDMA_LRO_CTRL_DW3); |
| 1383 | mtk_w32(eth, lro_ctrl_dw0, MTK_PDMA_LRO_CTRL_DW0); |
| 1384 | |
| 1385 | return 0; |
| 1386 | } |
| 1387 | |
| 1388 | static void mtk_hwlro_rx_uninit(struct mtk_eth *eth) |
| 1389 | { |
| 1390 | int i; |
| 1391 | u32 val; |
| 1392 | |
| 1393 | /* relinquish lro rings, flush aggregated packets */ |
| 1394 | mtk_w32(eth, MTK_LRO_RING_RELINQUISH_REQ, MTK_PDMA_LRO_CTRL_DW0); |
| 1395 | |
| 1396 | /* wait for relinquishments done */ |
| 1397 | for (i = 0; i < 10; i++) { |
| 1398 | val = mtk_r32(eth, MTK_PDMA_LRO_CTRL_DW0); |
| 1399 | if (val & MTK_LRO_RING_RELINQUISH_DONE) { |
| 1400 | msleep(20); |
| 1401 | continue; |
| 1402 | } |
Nelson Chang | ca3ba10 | 2016-09-26 14:33:50 +0800 | [diff] [blame] | 1403 | break; |
Nelson Chang | ee40681 | 2016-09-17 23:50:55 +0800 | [diff] [blame] | 1404 | } |
| 1405 | |
| 1406 | /* invalidate lro rings */ |
| 1407 | for (i = 1; i < MTK_MAX_RX_RING_NUM; i++) |
| 1408 | mtk_w32(eth, 0, MTK_LRO_CTRL_DW2_CFG(i)); |
| 1409 | |
| 1410 | /* disable HW LRO */ |
| 1411 | mtk_w32(eth, 0, MTK_PDMA_LRO_CTRL_DW0); |
| 1412 | } |
| 1413 | |
Nelson Chang | 7aab747 | 2016-09-17 23:50:56 +0800 | [diff] [blame] | 1414 | static void mtk_hwlro_val_ipaddr(struct mtk_eth *eth, int idx, __be32 ip) |
| 1415 | { |
| 1416 | u32 reg_val; |
| 1417 | |
| 1418 | reg_val = mtk_r32(eth, MTK_LRO_CTRL_DW2_CFG(idx)); |
| 1419 | |
| 1420 | /* invalidate the IP setting */ |
| 1421 | mtk_w32(eth, (reg_val & ~MTK_RING_MYIP_VLD), MTK_LRO_CTRL_DW2_CFG(idx)); |
| 1422 | |
| 1423 | mtk_w32(eth, ip, MTK_LRO_DIP_DW0_CFG(idx)); |
| 1424 | |
| 1425 | /* validate the IP setting */ |
| 1426 | mtk_w32(eth, (reg_val | MTK_RING_MYIP_VLD), MTK_LRO_CTRL_DW2_CFG(idx)); |
| 1427 | } |
| 1428 | |
| 1429 | static void mtk_hwlro_inval_ipaddr(struct mtk_eth *eth, int idx) |
| 1430 | { |
| 1431 | u32 reg_val; |
| 1432 | |
| 1433 | reg_val = mtk_r32(eth, MTK_LRO_CTRL_DW2_CFG(idx)); |
| 1434 | |
| 1435 | /* invalidate the IP setting */ |
| 1436 | mtk_w32(eth, (reg_val & ~MTK_RING_MYIP_VLD), MTK_LRO_CTRL_DW2_CFG(idx)); |
| 1437 | |
| 1438 | mtk_w32(eth, 0, MTK_LRO_DIP_DW0_CFG(idx)); |
| 1439 | } |
| 1440 | |
| 1441 | static int mtk_hwlro_get_ip_cnt(struct mtk_mac *mac) |
| 1442 | { |
| 1443 | int cnt = 0; |
| 1444 | int i; |
| 1445 | |
| 1446 | for (i = 0; i < MTK_MAX_LRO_IP_CNT; i++) { |
| 1447 | if (mac->hwlro_ip[i]) |
| 1448 | cnt++; |
| 1449 | } |
| 1450 | |
| 1451 | return cnt; |
| 1452 | } |
| 1453 | |
| 1454 | static int mtk_hwlro_add_ipaddr(struct net_device *dev, |
| 1455 | struct ethtool_rxnfc *cmd) |
| 1456 | { |
| 1457 | struct ethtool_rx_flow_spec *fsp = |
| 1458 | (struct ethtool_rx_flow_spec *)&cmd->fs; |
| 1459 | struct mtk_mac *mac = netdev_priv(dev); |
| 1460 | struct mtk_eth *eth = mac->hw; |
| 1461 | int hwlro_idx; |
| 1462 | |
| 1463 | if ((fsp->flow_type != TCP_V4_FLOW) || |
| 1464 | (!fsp->h_u.tcp_ip4_spec.ip4dst) || |
| 1465 | (fsp->location > 1)) |
| 1466 | return -EINVAL; |
| 1467 | |
| 1468 | mac->hwlro_ip[fsp->location] = htonl(fsp->h_u.tcp_ip4_spec.ip4dst); |
| 1469 | hwlro_idx = (mac->id * MTK_MAX_LRO_IP_CNT) + fsp->location; |
| 1470 | |
| 1471 | mac->hwlro_ip_cnt = mtk_hwlro_get_ip_cnt(mac); |
| 1472 | |
| 1473 | mtk_hwlro_val_ipaddr(eth, hwlro_idx, mac->hwlro_ip[fsp->location]); |
| 1474 | |
| 1475 | return 0; |
| 1476 | } |
| 1477 | |
| 1478 | static int mtk_hwlro_del_ipaddr(struct net_device *dev, |
| 1479 | struct ethtool_rxnfc *cmd) |
| 1480 | { |
| 1481 | struct ethtool_rx_flow_spec *fsp = |
| 1482 | (struct ethtool_rx_flow_spec *)&cmd->fs; |
| 1483 | struct mtk_mac *mac = netdev_priv(dev); |
| 1484 | struct mtk_eth *eth = mac->hw; |
| 1485 | int hwlro_idx; |
| 1486 | |
| 1487 | if (fsp->location > 1) |
| 1488 | return -EINVAL; |
| 1489 | |
| 1490 | mac->hwlro_ip[fsp->location] = 0; |
| 1491 | hwlro_idx = (mac->id * MTK_MAX_LRO_IP_CNT) + fsp->location; |
| 1492 | |
| 1493 | mac->hwlro_ip_cnt = mtk_hwlro_get_ip_cnt(mac); |
| 1494 | |
| 1495 | mtk_hwlro_inval_ipaddr(eth, hwlro_idx); |
| 1496 | |
| 1497 | return 0; |
| 1498 | } |
| 1499 | |
| 1500 | static void mtk_hwlro_netdev_disable(struct net_device *dev) |
| 1501 | { |
| 1502 | struct mtk_mac *mac = netdev_priv(dev); |
| 1503 | struct mtk_eth *eth = mac->hw; |
| 1504 | int i, hwlro_idx; |
| 1505 | |
| 1506 | for (i = 0; i < MTK_MAX_LRO_IP_CNT; i++) { |
| 1507 | mac->hwlro_ip[i] = 0; |
| 1508 | hwlro_idx = (mac->id * MTK_MAX_LRO_IP_CNT) + i; |
| 1509 | |
| 1510 | mtk_hwlro_inval_ipaddr(eth, hwlro_idx); |
| 1511 | } |
| 1512 | |
| 1513 | mac->hwlro_ip_cnt = 0; |
| 1514 | } |
| 1515 | |
| 1516 | static int mtk_hwlro_get_fdir_entry(struct net_device *dev, |
| 1517 | struct ethtool_rxnfc *cmd) |
| 1518 | { |
| 1519 | struct mtk_mac *mac = netdev_priv(dev); |
| 1520 | struct ethtool_rx_flow_spec *fsp = |
| 1521 | (struct ethtool_rx_flow_spec *)&cmd->fs; |
| 1522 | |
| 1523 | /* only tcp dst ipv4 is meaningful, others are meaningless */ |
| 1524 | fsp->flow_type = TCP_V4_FLOW; |
| 1525 | fsp->h_u.tcp_ip4_spec.ip4dst = ntohl(mac->hwlro_ip[fsp->location]); |
| 1526 | fsp->m_u.tcp_ip4_spec.ip4dst = 0; |
| 1527 | |
| 1528 | fsp->h_u.tcp_ip4_spec.ip4src = 0; |
| 1529 | fsp->m_u.tcp_ip4_spec.ip4src = 0xffffffff; |
| 1530 | fsp->h_u.tcp_ip4_spec.psrc = 0; |
| 1531 | fsp->m_u.tcp_ip4_spec.psrc = 0xffff; |
| 1532 | fsp->h_u.tcp_ip4_spec.pdst = 0; |
| 1533 | fsp->m_u.tcp_ip4_spec.pdst = 0xffff; |
| 1534 | fsp->h_u.tcp_ip4_spec.tos = 0; |
| 1535 | fsp->m_u.tcp_ip4_spec.tos = 0xff; |
| 1536 | |
| 1537 | return 0; |
| 1538 | } |
| 1539 | |
| 1540 | static int mtk_hwlro_get_fdir_all(struct net_device *dev, |
| 1541 | struct ethtool_rxnfc *cmd, |
| 1542 | u32 *rule_locs) |
| 1543 | { |
| 1544 | struct mtk_mac *mac = netdev_priv(dev); |
| 1545 | int cnt = 0; |
| 1546 | int i; |
| 1547 | |
| 1548 | for (i = 0; i < MTK_MAX_LRO_IP_CNT; i++) { |
| 1549 | if (mac->hwlro_ip[i]) { |
| 1550 | rule_locs[cnt] = i; |
| 1551 | cnt++; |
| 1552 | } |
| 1553 | } |
| 1554 | |
| 1555 | cmd->rule_cnt = cnt; |
| 1556 | |
| 1557 | return 0; |
| 1558 | } |
| 1559 | |
| 1560 | static netdev_features_t mtk_fix_features(struct net_device *dev, |
| 1561 | netdev_features_t features) |
| 1562 | { |
| 1563 | if (!(features & NETIF_F_LRO)) { |
| 1564 | struct mtk_mac *mac = netdev_priv(dev); |
| 1565 | int ip_cnt = mtk_hwlro_get_ip_cnt(mac); |
| 1566 | |
| 1567 | if (ip_cnt) { |
| 1568 | netdev_info(dev, "RX flow is programmed, LRO should keep on\n"); |
| 1569 | |
| 1570 | features |= NETIF_F_LRO; |
| 1571 | } |
| 1572 | } |
| 1573 | |
| 1574 | return features; |
| 1575 | } |
| 1576 | |
| 1577 | static int mtk_set_features(struct net_device *dev, netdev_features_t features) |
| 1578 | { |
| 1579 | int err = 0; |
| 1580 | |
| 1581 | if (!((dev->features ^ features) & NETIF_F_LRO)) |
| 1582 | return 0; |
| 1583 | |
| 1584 | if (!(features & NETIF_F_LRO)) |
| 1585 | mtk_hwlro_netdev_disable(dev); |
| 1586 | |
| 1587 | return err; |
| 1588 | } |
| 1589 | |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1590 | /* wait for DMA to finish whatever it is doing before we start using it again */ |
| 1591 | static int mtk_dma_busy_wait(struct mtk_eth *eth) |
| 1592 | { |
| 1593 | unsigned long t_start = jiffies; |
| 1594 | |
| 1595 | while (1) { |
| 1596 | if (!(mtk_r32(eth, MTK_QDMA_GLO_CFG) & |
| 1597 | (MTK_RX_DMA_BUSY | MTK_TX_DMA_BUSY))) |
| 1598 | return 0; |
| 1599 | if (time_after(jiffies, t_start + MTK_DMA_BUSY_TIMEOUT)) |
| 1600 | break; |
| 1601 | } |
| 1602 | |
| 1603 | dev_err(eth->dev, "DMA init timeout\n"); |
| 1604 | return -1; |
| 1605 | } |
| 1606 | |
| 1607 | static int mtk_dma_init(struct mtk_eth *eth) |
| 1608 | { |
| 1609 | int err; |
Nelson Chang | ee40681 | 2016-09-17 23:50:55 +0800 | [diff] [blame] | 1610 | u32 i; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1611 | |
| 1612 | if (mtk_dma_busy_wait(eth)) |
| 1613 | return -EBUSY; |
| 1614 | |
| 1615 | /* QDMA needs scratch memory for internal reordering of the |
| 1616 | * descriptors |
| 1617 | */ |
| 1618 | err = mtk_init_fq_dma(eth); |
| 1619 | if (err) |
| 1620 | return err; |
| 1621 | |
| 1622 | err = mtk_tx_alloc(eth); |
| 1623 | if (err) |
| 1624 | return err; |
| 1625 | |
Nelson Chang | ee40681 | 2016-09-17 23:50:55 +0800 | [diff] [blame] | 1626 | err = mtk_rx_alloc(eth, 0, MTK_RX_FLAGS_NORMAL); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1627 | if (err) |
| 1628 | return err; |
| 1629 | |
Nelson Chang | ee40681 | 2016-09-17 23:50:55 +0800 | [diff] [blame] | 1630 | if (eth->hwlro) { |
| 1631 | for (i = 1; i < MTK_MAX_RX_RING_NUM; i++) { |
| 1632 | err = mtk_rx_alloc(eth, i, MTK_RX_FLAGS_HWLRO); |
| 1633 | if (err) |
| 1634 | return err; |
| 1635 | } |
| 1636 | err = mtk_hwlro_rx_init(eth); |
| 1637 | if (err) |
| 1638 | return err; |
| 1639 | } |
| 1640 | |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1641 | /* Enable random early drop and set drop threshold automatically */ |
| 1642 | mtk_w32(eth, FC_THRES_DROP_MODE | FC_THRES_DROP_EN | FC_THRES_MIN, |
| 1643 | MTK_QDMA_FC_THRES); |
| 1644 | mtk_w32(eth, 0x0, MTK_QDMA_HRED2); |
| 1645 | |
| 1646 | return 0; |
| 1647 | } |
| 1648 | |
| 1649 | static void mtk_dma_free(struct mtk_eth *eth) |
| 1650 | { |
| 1651 | int i; |
| 1652 | |
| 1653 | for (i = 0; i < MTK_MAC_COUNT; i++) |
| 1654 | if (eth->netdev[i]) |
| 1655 | netdev_reset_queue(eth->netdev[i]); |
John Crispin | 605e4fe | 2016-06-10 13:27:59 +0200 | [diff] [blame] | 1656 | if (eth->scratch_ring) { |
| 1657 | dma_free_coherent(eth->dev, |
| 1658 | MTK_DMA_SIZE * sizeof(struct mtk_tx_dma), |
| 1659 | eth->scratch_ring, |
| 1660 | eth->phy_scratch_ring); |
| 1661 | eth->scratch_ring = NULL; |
| 1662 | eth->phy_scratch_ring = 0; |
| 1663 | } |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1664 | mtk_tx_clean(eth); |
Nelson Chang | ee40681 | 2016-09-17 23:50:55 +0800 | [diff] [blame] | 1665 | mtk_rx_clean(eth, 0); |
| 1666 | |
| 1667 | if (eth->hwlro) { |
| 1668 | mtk_hwlro_rx_uninit(eth); |
| 1669 | for (i = 1; i < MTK_MAX_RX_RING_NUM; i++) |
| 1670 | mtk_rx_clean(eth, i); |
| 1671 | } |
| 1672 | |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1673 | kfree(eth->scratch_head); |
| 1674 | } |
| 1675 | |
| 1676 | static void mtk_tx_timeout(struct net_device *dev) |
| 1677 | { |
| 1678 | struct mtk_mac *mac = netdev_priv(dev); |
| 1679 | struct mtk_eth *eth = mac->hw; |
| 1680 | |
| 1681 | eth->netdev[mac->id]->stats.tx_errors++; |
| 1682 | netif_err(eth, tx_err, dev, |
| 1683 | "transmit timed out\n"); |
John Crispin | 7c78b4a | 2016-04-08 00:54:10 +0200 | [diff] [blame] | 1684 | schedule_work(ð->pending_work); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1685 | } |
| 1686 | |
John Crispin | 8067302 | 2016-06-29 13:38:11 +0200 | [diff] [blame] | 1687 | static irqreturn_t mtk_handle_irq_rx(int irq, void *_eth) |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1688 | { |
| 1689 | struct mtk_eth *eth = _eth; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1690 | |
John Crispin | 8067302 | 2016-06-29 13:38:11 +0200 | [diff] [blame] | 1691 | if (likely(napi_schedule_prep(ð->rx_napi))) { |
| 1692 | __napi_schedule(ð->rx_napi); |
John Crispin | 5cce032 | 2017-06-19 15:37:05 +0200 | [diff] [blame^] | 1693 | mtk_rx_irq_disable(eth, MTK_RX_DONE_INT); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1694 | } |
John Crispin | 8067302 | 2016-06-29 13:38:11 +0200 | [diff] [blame] | 1695 | |
| 1696 | return IRQ_HANDLED; |
| 1697 | } |
| 1698 | |
| 1699 | static irqreturn_t mtk_handle_irq_tx(int irq, void *_eth) |
| 1700 | { |
| 1701 | struct mtk_eth *eth = _eth; |
| 1702 | |
| 1703 | if (likely(napi_schedule_prep(ð->tx_napi))) { |
| 1704 | __napi_schedule(ð->tx_napi); |
John Crispin | 5cce032 | 2017-06-19 15:37:05 +0200 | [diff] [blame^] | 1705 | mtk_tx_irq_disable(eth, MTK_TX_DONE_INT); |
John Crispin | 8067302 | 2016-06-29 13:38:11 +0200 | [diff] [blame] | 1706 | } |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1707 | |
| 1708 | return IRQ_HANDLED; |
| 1709 | } |
| 1710 | |
| 1711 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 1712 | static void mtk_poll_controller(struct net_device *dev) |
| 1713 | { |
| 1714 | struct mtk_mac *mac = netdev_priv(dev); |
| 1715 | struct mtk_eth *eth = mac->hw; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1716 | |
John Crispin | 5cce032 | 2017-06-19 15:37:05 +0200 | [diff] [blame^] | 1717 | mtk_tx_irq_disable(eth, MTK_TX_DONE_INT); |
| 1718 | mtk_rx_irq_disable(eth, MTK_RX_DONE_INT); |
John Crispin | 8186f6e | 2016-07-02 08:00:50 +0200 | [diff] [blame] | 1719 | mtk_handle_irq_rx(eth->irq[2], dev); |
John Crispin | 5cce032 | 2017-06-19 15:37:05 +0200 | [diff] [blame^] | 1720 | mtk_tx_irq_enable(eth, MTK_TX_DONE_INT); |
| 1721 | mtk_rx_irq_enable(eth, MTK_RX_DONE_INT); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1722 | } |
| 1723 | #endif |
| 1724 | |
| 1725 | static int mtk_start_dma(struct mtk_eth *eth) |
| 1726 | { |
| 1727 | int err; |
| 1728 | |
| 1729 | err = mtk_dma_init(eth); |
| 1730 | if (err) { |
| 1731 | mtk_dma_free(eth); |
| 1732 | return err; |
| 1733 | } |
| 1734 | |
| 1735 | mtk_w32(eth, |
Nelson Chang | bacfd11 | 2016-08-26 01:09:42 +0800 | [diff] [blame] | 1736 | MTK_TX_WB_DDONE | MTK_TX_DMA_EN | |
| 1737 | MTK_DMA_SIZE_16DWORDS | MTK_NDP_CO_PRO, |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1738 | MTK_QDMA_GLO_CFG); |
| 1739 | |
Nelson Chang | bacfd11 | 2016-08-26 01:09:42 +0800 | [diff] [blame] | 1740 | mtk_w32(eth, |
| 1741 | MTK_RX_DMA_EN | MTK_RX_2B_OFFSET | |
| 1742 | MTK_RX_BT_32DWORDS | MTK_MULTI_EN, |
| 1743 | MTK_PDMA_GLO_CFG); |
| 1744 | |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1745 | return 0; |
| 1746 | } |
| 1747 | |
| 1748 | static int mtk_open(struct net_device *dev) |
| 1749 | { |
| 1750 | struct mtk_mac *mac = netdev_priv(dev); |
| 1751 | struct mtk_eth *eth = mac->hw; |
| 1752 | |
| 1753 | /* we run 2 netdevs on the same dma ring so we only bring it up once */ |
| 1754 | if (!atomic_read(ð->dma_refcnt)) { |
| 1755 | int err = mtk_start_dma(eth); |
| 1756 | |
| 1757 | if (err) |
| 1758 | return err; |
| 1759 | |
John Crispin | 8067302 | 2016-06-29 13:38:11 +0200 | [diff] [blame] | 1760 | napi_enable(ð->tx_napi); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1761 | napi_enable(ð->rx_napi); |
John Crispin | 5cce032 | 2017-06-19 15:37:05 +0200 | [diff] [blame^] | 1762 | mtk_tx_irq_enable(eth, MTK_TX_DONE_INT); |
| 1763 | mtk_rx_irq_enable(eth, MTK_RX_DONE_INT); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1764 | } |
| 1765 | atomic_inc(ð->dma_refcnt); |
| 1766 | |
Sean Wang | 2364c5c | 2016-09-22 16:33:35 +0800 | [diff] [blame] | 1767 | phy_start(dev->phydev); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1768 | netif_start_queue(dev); |
| 1769 | |
| 1770 | return 0; |
| 1771 | } |
| 1772 | |
| 1773 | static void mtk_stop_dma(struct mtk_eth *eth, u32 glo_cfg) |
| 1774 | { |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1775 | u32 val; |
| 1776 | int i; |
| 1777 | |
| 1778 | /* stop the dma engine */ |
Sean Wang | e3e9652 | 2016-08-11 17:51:00 +0800 | [diff] [blame] | 1779 | spin_lock_bh(ð->page_lock); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1780 | val = mtk_r32(eth, glo_cfg); |
| 1781 | mtk_w32(eth, val & ~(MTK_TX_WB_DDONE | MTK_RX_DMA_EN | MTK_TX_DMA_EN), |
| 1782 | glo_cfg); |
Sean Wang | e3e9652 | 2016-08-11 17:51:00 +0800 | [diff] [blame] | 1783 | spin_unlock_bh(ð->page_lock); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1784 | |
| 1785 | /* wait for dma stop */ |
| 1786 | for (i = 0; i < 10; i++) { |
| 1787 | val = mtk_r32(eth, glo_cfg); |
| 1788 | if (val & (MTK_TX_DMA_BUSY | MTK_RX_DMA_BUSY)) { |
| 1789 | msleep(20); |
| 1790 | continue; |
| 1791 | } |
| 1792 | break; |
| 1793 | } |
| 1794 | } |
| 1795 | |
| 1796 | static int mtk_stop(struct net_device *dev) |
| 1797 | { |
| 1798 | struct mtk_mac *mac = netdev_priv(dev); |
| 1799 | struct mtk_eth *eth = mac->hw; |
| 1800 | |
| 1801 | netif_tx_disable(dev); |
Sean Wang | 2364c5c | 2016-09-22 16:33:35 +0800 | [diff] [blame] | 1802 | phy_stop(dev->phydev); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1803 | |
| 1804 | /* only shutdown DMA if this is the last user */ |
| 1805 | if (!atomic_dec_and_test(ð->dma_refcnt)) |
| 1806 | return 0; |
| 1807 | |
John Crispin | 5cce032 | 2017-06-19 15:37:05 +0200 | [diff] [blame^] | 1808 | mtk_tx_irq_disable(eth, MTK_TX_DONE_INT); |
| 1809 | mtk_rx_irq_disable(eth, MTK_RX_DONE_INT); |
John Crispin | 8067302 | 2016-06-29 13:38:11 +0200 | [diff] [blame] | 1810 | napi_disable(ð->tx_napi); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1811 | napi_disable(ð->rx_napi); |
| 1812 | |
| 1813 | mtk_stop_dma(eth, MTK_QDMA_GLO_CFG); |
Nelson Chang | 6bf563d | 2016-09-26 14:33:49 +0800 | [diff] [blame] | 1814 | mtk_stop_dma(eth, MTK_PDMA_GLO_CFG); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1815 | |
| 1816 | mtk_dma_free(eth); |
| 1817 | |
| 1818 | return 0; |
| 1819 | } |
| 1820 | |
Sean Wang | 2a8307a | 2016-09-14 23:13:20 +0800 | [diff] [blame] | 1821 | static void ethsys_reset(struct mtk_eth *eth, u32 reset_bits) |
| 1822 | { |
| 1823 | regmap_update_bits(eth->ethsys, ETHSYS_RSTCTRL, |
| 1824 | reset_bits, |
| 1825 | reset_bits); |
| 1826 | |
| 1827 | usleep_range(1000, 1100); |
| 1828 | regmap_update_bits(eth->ethsys, ETHSYS_RSTCTRL, |
| 1829 | reset_bits, |
| 1830 | ~reset_bits); |
| 1831 | mdelay(10); |
| 1832 | } |
| 1833 | |
Sean Wang | 9ea4d31 | 2016-09-14 23:13:19 +0800 | [diff] [blame] | 1834 | static int mtk_hw_init(struct mtk_eth *eth) |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1835 | { |
Sean Wang | 9ea4d31 | 2016-09-14 23:13:19 +0800 | [diff] [blame] | 1836 | int i, val; |
| 1837 | |
| 1838 | if (test_and_set_bit(MTK_HW_INIT, ð->state)) |
| 1839 | return 0; |
Sean Wang | 85574db | 2016-09-14 23:13:15 +0800 | [diff] [blame] | 1840 | |
Sean Wang | 26a2ad8 | 2016-09-14 23:13:18 +0800 | [diff] [blame] | 1841 | pm_runtime_enable(eth->dev); |
| 1842 | pm_runtime_get_sync(eth->dev); |
| 1843 | |
Sean Wang | 85574db | 2016-09-14 23:13:15 +0800 | [diff] [blame] | 1844 | clk_prepare_enable(eth->clks[MTK_CLK_ETHIF]); |
| 1845 | clk_prepare_enable(eth->clks[MTK_CLK_ESW]); |
| 1846 | clk_prepare_enable(eth->clks[MTK_CLK_GP1]); |
| 1847 | clk_prepare_enable(eth->clks[MTK_CLK_GP2]); |
Sean Wang | 2a8307a | 2016-09-14 23:13:20 +0800 | [diff] [blame] | 1848 | ethsys_reset(eth, RSTCTRL_FE); |
| 1849 | ethsys_reset(eth, RSTCTRL_PPE); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1850 | |
Sean Wang | 9ea4d31 | 2016-09-14 23:13:19 +0800 | [diff] [blame] | 1851 | regmap_read(eth->ethsys, ETHSYS_SYSCFG0, &val); |
| 1852 | for (i = 0; i < MTK_MAC_COUNT; i++) { |
| 1853 | if (!eth->mac[i]) |
| 1854 | continue; |
| 1855 | val &= ~SYSCFG0_GE_MODE(SYSCFG0_GE_MASK, eth->mac[i]->id); |
| 1856 | val |= SYSCFG0_GE_MODE(eth->mac[i]->ge_mode, eth->mac[i]->id); |
| 1857 | } |
| 1858 | regmap_write(eth->ethsys, ETHSYS_SYSCFG0, val); |
| 1859 | |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1860 | /* Set GE2 driving and slew rate */ |
| 1861 | regmap_write(eth->pctl, GPIO_DRV_SEL10, 0xa00); |
| 1862 | |
| 1863 | /* set GE2 TDSEL */ |
| 1864 | regmap_write(eth->pctl, GPIO_OD33_CTRL8, 0x5); |
| 1865 | |
| 1866 | /* set GE2 TUNE */ |
| 1867 | regmap_write(eth->pctl, GPIO_BIAS_CTRL, 0x0); |
| 1868 | |
| 1869 | /* GE1, Force 1000M/FD, FC ON */ |
| 1870 | mtk_w32(eth, MAC_MCR_FIXED_LINK, MTK_MAC_MCR(0)); |
| 1871 | |
| 1872 | /* GE2, Force 1000M/FD, FC ON */ |
| 1873 | mtk_w32(eth, MAC_MCR_FIXED_LINK, MTK_MAC_MCR(1)); |
| 1874 | |
Sean Wang | 87e3df4 | 2017-04-07 16:45:07 +0800 | [diff] [blame] | 1875 | /* Indicates CDM to parse the MTK special tag from CPU |
| 1876 | * which also is working out for untag packets. |
| 1877 | */ |
| 1878 | val = mtk_r32(eth, MTK_CDMQ_IG_CTRL); |
| 1879 | mtk_w32(eth, val | MTK_CDMQ_STAG_EN, MTK_CDMQ_IG_CTRL); |
| 1880 | |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1881 | /* Enable RX VLan Offloading */ |
| 1882 | mtk_w32(eth, 1, MTK_CDMP_EG_CTRL); |
| 1883 | |
John Crispin | 671d41e | 2017-06-19 15:37:04 +0200 | [diff] [blame] | 1884 | /* enable interrupt delay for RX */ |
| 1885 | mtk_w32(eth, MTK_PDMA_DELAY_RX_DELAY, MTK_PDMA_DELAY_INT); |
| 1886 | |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1887 | /* disable delay and normal interrupt */ |
| 1888 | mtk_w32(eth, 0, MTK_QDMA_DELAY_INT); |
John Crispin | 5cce032 | 2017-06-19 15:37:05 +0200 | [diff] [blame^] | 1889 | mtk_tx_irq_disable(eth, ~0); |
| 1890 | mtk_rx_irq_disable(eth, ~0); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1891 | mtk_w32(eth, RST_GL_PSE, MTK_RST_GL); |
| 1892 | mtk_w32(eth, 0, MTK_RST_GL); |
| 1893 | |
| 1894 | /* FE int grouping */ |
John Crispin | 8067302 | 2016-06-29 13:38:11 +0200 | [diff] [blame] | 1895 | mtk_w32(eth, MTK_TX_DONE_INT, MTK_PDMA_INT_GRP1); |
| 1896 | mtk_w32(eth, MTK_RX_DONE_INT, MTK_PDMA_INT_GRP2); |
| 1897 | mtk_w32(eth, MTK_TX_DONE_INT, MTK_QDMA_INT_GRP1); |
| 1898 | mtk_w32(eth, MTK_RX_DONE_INT, MTK_QDMA_INT_GRP2); |
| 1899 | mtk_w32(eth, 0x21021000, MTK_FE_INT_GRP); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1900 | |
| 1901 | for (i = 0; i < 2; i++) { |
| 1902 | u32 val = mtk_r32(eth, MTK_GDMA_FWD_CFG(i)); |
| 1903 | |
Nelson Chang | 9c08435 | 2016-08-26 01:09:43 +0800 | [diff] [blame] | 1904 | /* setup the forward port to send frame to PDMA */ |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1905 | val &= ~0xffff; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1906 | |
| 1907 | /* Enable RX checksum */ |
| 1908 | val |= MTK_GDMA_ICS_EN | MTK_GDMA_TCS_EN | MTK_GDMA_UCS_EN; |
| 1909 | |
| 1910 | /* setup the mac dma */ |
| 1911 | mtk_w32(eth, val, MTK_GDMA_FWD_CFG(i)); |
| 1912 | } |
| 1913 | |
| 1914 | return 0; |
| 1915 | } |
| 1916 | |
Sean Wang | bf253fb | 2016-09-14 23:13:16 +0800 | [diff] [blame] | 1917 | static int mtk_hw_deinit(struct mtk_eth *eth) |
| 1918 | { |
Sean Wang | 9ea4d31 | 2016-09-14 23:13:19 +0800 | [diff] [blame] | 1919 | if (!test_and_clear_bit(MTK_HW_INIT, ð->state)) |
| 1920 | return 0; |
| 1921 | |
Sean Wang | bf253fb | 2016-09-14 23:13:16 +0800 | [diff] [blame] | 1922 | clk_disable_unprepare(eth->clks[MTK_CLK_GP2]); |
| 1923 | clk_disable_unprepare(eth->clks[MTK_CLK_GP1]); |
| 1924 | clk_disable_unprepare(eth->clks[MTK_CLK_ESW]); |
| 1925 | clk_disable_unprepare(eth->clks[MTK_CLK_ETHIF]); |
| 1926 | |
Sean Wang | 26a2ad8 | 2016-09-14 23:13:18 +0800 | [diff] [blame] | 1927 | pm_runtime_put_sync(eth->dev); |
| 1928 | pm_runtime_disable(eth->dev); |
| 1929 | |
Sean Wang | bf253fb | 2016-09-14 23:13:16 +0800 | [diff] [blame] | 1930 | return 0; |
| 1931 | } |
| 1932 | |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1933 | static int __init mtk_init(struct net_device *dev) |
| 1934 | { |
| 1935 | struct mtk_mac *mac = netdev_priv(dev); |
| 1936 | struct mtk_eth *eth = mac->hw; |
| 1937 | const char *mac_addr; |
| 1938 | |
| 1939 | mac_addr = of_get_mac_address(mac->of_node); |
| 1940 | if (mac_addr) |
| 1941 | ether_addr_copy(dev->dev_addr, mac_addr); |
| 1942 | |
| 1943 | /* If the mac address is invalid, use random mac address */ |
| 1944 | if (!is_valid_ether_addr(dev->dev_addr)) { |
Tobias Klauser | e3c36e4 | 2017-03-07 16:27:10 +0100 | [diff] [blame] | 1945 | eth_hw_addr_random(dev); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1946 | dev_err(eth->dev, "generated random MAC address %pM\n", |
| 1947 | dev->dev_addr); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1948 | } |
| 1949 | |
Sean Wang | 2364c5c | 2016-09-22 16:33:35 +0800 | [diff] [blame] | 1950 | return mtk_phy_connect(dev); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1951 | } |
| 1952 | |
| 1953 | static void mtk_uninit(struct net_device *dev) |
| 1954 | { |
| 1955 | struct mtk_mac *mac = netdev_priv(dev); |
| 1956 | struct mtk_eth *eth = mac->hw; |
| 1957 | |
Sean Wang | 2364c5c | 2016-09-22 16:33:35 +0800 | [diff] [blame] | 1958 | phy_disconnect(dev->phydev); |
Johan Hovold | 16a67eb | 2016-11-28 19:25:05 +0100 | [diff] [blame] | 1959 | if (of_phy_is_fixed_link(mac->of_node)) |
| 1960 | of_phy_deregister_fixed_link(mac->of_node); |
John Crispin | 5cce032 | 2017-06-19 15:37:05 +0200 | [diff] [blame^] | 1961 | mtk_tx_irq_disable(eth, ~0); |
| 1962 | mtk_rx_irq_disable(eth, ~0); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1963 | } |
| 1964 | |
| 1965 | static int mtk_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) |
| 1966 | { |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1967 | switch (cmd) { |
| 1968 | case SIOCGMIIPHY: |
| 1969 | case SIOCGMIIREG: |
| 1970 | case SIOCSMIIREG: |
Sean Wang | 2364c5c | 2016-09-22 16:33:35 +0800 | [diff] [blame] | 1971 | return phy_mii_ioctl(dev->phydev, ifr, cmd); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1972 | default: |
| 1973 | break; |
| 1974 | } |
| 1975 | |
| 1976 | return -EOPNOTSUPP; |
| 1977 | } |
| 1978 | |
| 1979 | static void mtk_pending_work(struct work_struct *work) |
| 1980 | { |
John Crispin | 7c78b4a | 2016-04-08 00:54:10 +0200 | [diff] [blame] | 1981 | struct mtk_eth *eth = container_of(work, struct mtk_eth, pending_work); |
John Crispin | e7d425d | 2016-04-08 00:54:09 +0200 | [diff] [blame] | 1982 | int err, i; |
| 1983 | unsigned long restart = 0; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1984 | |
| 1985 | rtnl_lock(); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 1986 | |
Sean Wang | dce6fa4 | 2016-09-14 23:13:21 +0800 | [diff] [blame] | 1987 | dev_dbg(eth->dev, "[%s][%d] reset\n", __func__, __LINE__); |
| 1988 | |
| 1989 | while (test_and_set_bit_lock(MTK_RESETTING, ð->state)) |
| 1990 | cpu_relax(); |
| 1991 | |
| 1992 | dev_dbg(eth->dev, "[%s][%d] mtk_stop starts\n", __func__, __LINE__); |
John Crispin | e7d425d | 2016-04-08 00:54:09 +0200 | [diff] [blame] | 1993 | /* stop all devices to make sure that dma is properly shut down */ |
| 1994 | for (i = 0; i < MTK_MAC_COUNT; i++) { |
John Crispin | 7c78b4a | 2016-04-08 00:54:10 +0200 | [diff] [blame] | 1995 | if (!eth->netdev[i]) |
John Crispin | e7d425d | 2016-04-08 00:54:09 +0200 | [diff] [blame] | 1996 | continue; |
| 1997 | mtk_stop(eth->netdev[i]); |
| 1998 | __set_bit(i, &restart); |
| 1999 | } |
Sean Wang | dce6fa4 | 2016-09-14 23:13:21 +0800 | [diff] [blame] | 2000 | dev_dbg(eth->dev, "[%s][%d] mtk_stop ends\n", __func__, __LINE__); |
John Crispin | e7d425d | 2016-04-08 00:54:09 +0200 | [diff] [blame] | 2001 | |
Sean Wang | 9ea4d31 | 2016-09-14 23:13:19 +0800 | [diff] [blame] | 2002 | /* restart underlying hardware such as power, clock, pin mux |
| 2003 | * and the connected phy |
| 2004 | */ |
| 2005 | mtk_hw_deinit(eth); |
| 2006 | |
| 2007 | if (eth->dev->pins) |
| 2008 | pinctrl_select_state(eth->dev->pins->p, |
| 2009 | eth->dev->pins->default_state); |
| 2010 | mtk_hw_init(eth); |
| 2011 | |
| 2012 | for (i = 0; i < MTK_MAC_COUNT; i++) { |
| 2013 | if (!eth->mac[i] || |
| 2014 | of_phy_is_fixed_link(eth->mac[i]->of_node)) |
| 2015 | continue; |
Sean Wang | 2364c5c | 2016-09-22 16:33:35 +0800 | [diff] [blame] | 2016 | err = phy_init_hw(eth->netdev[i]->phydev); |
Sean Wang | 9ea4d31 | 2016-09-14 23:13:19 +0800 | [diff] [blame] | 2017 | if (err) |
| 2018 | dev_err(eth->dev, "%s: PHY init failed.\n", |
| 2019 | eth->netdev[i]->name); |
| 2020 | } |
| 2021 | |
John Crispin | e7d425d | 2016-04-08 00:54:09 +0200 | [diff] [blame] | 2022 | /* restart DMA and enable IRQs */ |
| 2023 | for (i = 0; i < MTK_MAC_COUNT; i++) { |
| 2024 | if (!test_bit(i, &restart)) |
| 2025 | continue; |
| 2026 | err = mtk_open(eth->netdev[i]); |
| 2027 | if (err) { |
| 2028 | netif_alert(eth, ifup, eth->netdev[i], |
| 2029 | "Driver up/down cycle failed, closing device.\n"); |
| 2030 | dev_close(eth->netdev[i]); |
| 2031 | } |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2032 | } |
Sean Wang | dce6fa4 | 2016-09-14 23:13:21 +0800 | [diff] [blame] | 2033 | |
| 2034 | dev_dbg(eth->dev, "[%s][%d] reset done\n", __func__, __LINE__); |
| 2035 | |
| 2036 | clear_bit_unlock(MTK_RESETTING, ð->state); |
| 2037 | |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2038 | rtnl_unlock(); |
| 2039 | } |
| 2040 | |
Sean Wang | 8a8a9e8 | 2016-09-14 23:13:17 +0800 | [diff] [blame] | 2041 | static int mtk_free_dev(struct mtk_eth *eth) |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2042 | { |
| 2043 | int i; |
| 2044 | |
| 2045 | for (i = 0; i < MTK_MAC_COUNT; i++) { |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2046 | if (!eth->netdev[i]) |
| 2047 | continue; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2048 | free_netdev(eth->netdev[i]); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2049 | } |
Sean Wang | 8a8a9e8 | 2016-09-14 23:13:17 +0800 | [diff] [blame] | 2050 | |
| 2051 | return 0; |
| 2052 | } |
| 2053 | |
| 2054 | static int mtk_unreg_dev(struct mtk_eth *eth) |
| 2055 | { |
| 2056 | int i; |
| 2057 | |
| 2058 | for (i = 0; i < MTK_MAC_COUNT; i++) { |
| 2059 | if (!eth->netdev[i]) |
| 2060 | continue; |
| 2061 | unregister_netdev(eth->netdev[i]); |
| 2062 | } |
| 2063 | |
| 2064 | return 0; |
| 2065 | } |
| 2066 | |
| 2067 | static int mtk_cleanup(struct mtk_eth *eth) |
| 2068 | { |
| 2069 | mtk_unreg_dev(eth); |
| 2070 | mtk_free_dev(eth); |
John Crispin | 7c78b4a | 2016-04-08 00:54:10 +0200 | [diff] [blame] | 2071 | cancel_work_sync(ð->pending_work); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2072 | |
| 2073 | return 0; |
| 2074 | } |
| 2075 | |
Baoyou Xie | 3a82e78 | 2016-09-30 15:48:50 +0800 | [diff] [blame] | 2076 | static int mtk_get_link_ksettings(struct net_device *ndev, |
| 2077 | struct ethtool_link_ksettings *cmd) |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2078 | { |
Sean Wang | 3e60b74 | 2016-09-22 16:42:03 +0800 | [diff] [blame] | 2079 | struct mtk_mac *mac = netdev_priv(ndev); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2080 | |
Sean Wang | dce6fa4 | 2016-09-14 23:13:21 +0800 | [diff] [blame] | 2081 | if (unlikely(test_bit(MTK_RESETTING, &mac->hw->state))) |
| 2082 | return -EBUSY; |
| 2083 | |
yuval.shaia@oracle.com | 5514174 | 2017-06-13 10:09:46 +0300 | [diff] [blame] | 2084 | phy_ethtool_ksettings_get(ndev->phydev, cmd); |
| 2085 | |
| 2086 | return 0; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2087 | } |
| 2088 | |
Baoyou Xie | 3a82e78 | 2016-09-30 15:48:50 +0800 | [diff] [blame] | 2089 | static int mtk_set_link_ksettings(struct net_device *ndev, |
| 2090 | const struct ethtool_link_ksettings *cmd) |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2091 | { |
Sean Wang | 3e60b74 | 2016-09-22 16:42:03 +0800 | [diff] [blame] | 2092 | struct mtk_mac *mac = netdev_priv(ndev); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2093 | |
Sean Wang | 3e60b74 | 2016-09-22 16:42:03 +0800 | [diff] [blame] | 2094 | if (unlikely(test_bit(MTK_RESETTING, &mac->hw->state))) |
| 2095 | return -EBUSY; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2096 | |
Sean Wang | 3e60b74 | 2016-09-22 16:42:03 +0800 | [diff] [blame] | 2097 | return phy_ethtool_ksettings_set(ndev->phydev, cmd); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2098 | } |
| 2099 | |
| 2100 | static void mtk_get_drvinfo(struct net_device *dev, |
| 2101 | struct ethtool_drvinfo *info) |
| 2102 | { |
| 2103 | struct mtk_mac *mac = netdev_priv(dev); |
| 2104 | |
| 2105 | strlcpy(info->driver, mac->hw->dev->driver->name, sizeof(info->driver)); |
| 2106 | strlcpy(info->bus_info, dev_name(mac->hw->dev), sizeof(info->bus_info)); |
| 2107 | info->n_stats = ARRAY_SIZE(mtk_ethtool_stats); |
| 2108 | } |
| 2109 | |
| 2110 | static u32 mtk_get_msglevel(struct net_device *dev) |
| 2111 | { |
| 2112 | struct mtk_mac *mac = netdev_priv(dev); |
| 2113 | |
| 2114 | return mac->hw->msg_enable; |
| 2115 | } |
| 2116 | |
| 2117 | static void mtk_set_msglevel(struct net_device *dev, u32 value) |
| 2118 | { |
| 2119 | struct mtk_mac *mac = netdev_priv(dev); |
| 2120 | |
| 2121 | mac->hw->msg_enable = value; |
| 2122 | } |
| 2123 | |
| 2124 | static int mtk_nway_reset(struct net_device *dev) |
| 2125 | { |
| 2126 | struct mtk_mac *mac = netdev_priv(dev); |
| 2127 | |
Sean Wang | dce6fa4 | 2016-09-14 23:13:21 +0800 | [diff] [blame] | 2128 | if (unlikely(test_bit(MTK_RESETTING, &mac->hw->state))) |
| 2129 | return -EBUSY; |
| 2130 | |
Sean Wang | 2364c5c | 2016-09-22 16:33:35 +0800 | [diff] [blame] | 2131 | return genphy_restart_aneg(dev->phydev); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2132 | } |
| 2133 | |
| 2134 | static u32 mtk_get_link(struct net_device *dev) |
| 2135 | { |
| 2136 | struct mtk_mac *mac = netdev_priv(dev); |
| 2137 | int err; |
| 2138 | |
Sean Wang | dce6fa4 | 2016-09-14 23:13:21 +0800 | [diff] [blame] | 2139 | if (unlikely(test_bit(MTK_RESETTING, &mac->hw->state))) |
| 2140 | return -EBUSY; |
| 2141 | |
Sean Wang | 2364c5c | 2016-09-22 16:33:35 +0800 | [diff] [blame] | 2142 | err = genphy_update_link(dev->phydev); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2143 | if (err) |
| 2144 | return ethtool_op_get_link(dev); |
| 2145 | |
Sean Wang | 2364c5c | 2016-09-22 16:33:35 +0800 | [diff] [blame] | 2146 | return dev->phydev->link; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2147 | } |
| 2148 | |
| 2149 | static void mtk_get_strings(struct net_device *dev, u32 stringset, u8 *data) |
| 2150 | { |
| 2151 | int i; |
| 2152 | |
| 2153 | switch (stringset) { |
| 2154 | case ETH_SS_STATS: |
| 2155 | for (i = 0; i < ARRAY_SIZE(mtk_ethtool_stats); i++) { |
| 2156 | memcpy(data, mtk_ethtool_stats[i].str, ETH_GSTRING_LEN); |
| 2157 | data += ETH_GSTRING_LEN; |
| 2158 | } |
| 2159 | break; |
| 2160 | } |
| 2161 | } |
| 2162 | |
| 2163 | static int mtk_get_sset_count(struct net_device *dev, int sset) |
| 2164 | { |
| 2165 | switch (sset) { |
| 2166 | case ETH_SS_STATS: |
| 2167 | return ARRAY_SIZE(mtk_ethtool_stats); |
| 2168 | default: |
| 2169 | return -EOPNOTSUPP; |
| 2170 | } |
| 2171 | } |
| 2172 | |
| 2173 | static void mtk_get_ethtool_stats(struct net_device *dev, |
| 2174 | struct ethtool_stats *stats, u64 *data) |
| 2175 | { |
| 2176 | struct mtk_mac *mac = netdev_priv(dev); |
| 2177 | struct mtk_hw_stats *hwstats = mac->hw_stats; |
| 2178 | u64 *data_src, *data_dst; |
| 2179 | unsigned int start; |
| 2180 | int i; |
| 2181 | |
Sean Wang | dce6fa4 | 2016-09-14 23:13:21 +0800 | [diff] [blame] | 2182 | if (unlikely(test_bit(MTK_RESETTING, &mac->hw->state))) |
| 2183 | return; |
| 2184 | |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2185 | if (netif_running(dev) && netif_device_present(dev)) { |
| 2186 | if (spin_trylock(&hwstats->stats_lock)) { |
| 2187 | mtk_stats_update_mac(mac); |
| 2188 | spin_unlock(&hwstats->stats_lock); |
| 2189 | } |
| 2190 | } |
| 2191 | |
Sean Wang | 94d308d | 2016-09-20 11:26:48 +0800 | [diff] [blame] | 2192 | data_src = (u64 *)hwstats; |
| 2193 | |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2194 | do { |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2195 | data_dst = data; |
| 2196 | start = u64_stats_fetch_begin_irq(&hwstats->syncp); |
| 2197 | |
| 2198 | for (i = 0; i < ARRAY_SIZE(mtk_ethtool_stats); i++) |
| 2199 | *data_dst++ = *(data_src + mtk_ethtool_stats[i].offset); |
| 2200 | } while (u64_stats_fetch_retry_irq(&hwstats->syncp, start)); |
| 2201 | } |
| 2202 | |
Nelson Chang | 7aab747 | 2016-09-17 23:50:56 +0800 | [diff] [blame] | 2203 | static int mtk_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd, |
| 2204 | u32 *rule_locs) |
| 2205 | { |
| 2206 | int ret = -EOPNOTSUPP; |
| 2207 | |
| 2208 | switch (cmd->cmd) { |
| 2209 | case ETHTOOL_GRXRINGS: |
| 2210 | if (dev->features & NETIF_F_LRO) { |
| 2211 | cmd->data = MTK_MAX_RX_RING_NUM; |
| 2212 | ret = 0; |
| 2213 | } |
| 2214 | break; |
| 2215 | case ETHTOOL_GRXCLSRLCNT: |
| 2216 | if (dev->features & NETIF_F_LRO) { |
| 2217 | struct mtk_mac *mac = netdev_priv(dev); |
| 2218 | |
| 2219 | cmd->rule_cnt = mac->hwlro_ip_cnt; |
| 2220 | ret = 0; |
| 2221 | } |
| 2222 | break; |
| 2223 | case ETHTOOL_GRXCLSRULE: |
| 2224 | if (dev->features & NETIF_F_LRO) |
| 2225 | ret = mtk_hwlro_get_fdir_entry(dev, cmd); |
| 2226 | break; |
| 2227 | case ETHTOOL_GRXCLSRLALL: |
| 2228 | if (dev->features & NETIF_F_LRO) |
| 2229 | ret = mtk_hwlro_get_fdir_all(dev, cmd, |
| 2230 | rule_locs); |
| 2231 | break; |
| 2232 | default: |
| 2233 | break; |
| 2234 | } |
| 2235 | |
| 2236 | return ret; |
| 2237 | } |
| 2238 | |
| 2239 | static int mtk_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd) |
| 2240 | { |
| 2241 | int ret = -EOPNOTSUPP; |
| 2242 | |
| 2243 | switch (cmd->cmd) { |
| 2244 | case ETHTOOL_SRXCLSRLINS: |
| 2245 | if (dev->features & NETIF_F_LRO) |
| 2246 | ret = mtk_hwlro_add_ipaddr(dev, cmd); |
| 2247 | break; |
| 2248 | case ETHTOOL_SRXCLSRLDEL: |
| 2249 | if (dev->features & NETIF_F_LRO) |
| 2250 | ret = mtk_hwlro_del_ipaddr(dev, cmd); |
| 2251 | break; |
| 2252 | default: |
| 2253 | break; |
| 2254 | } |
| 2255 | |
| 2256 | return ret; |
| 2257 | } |
| 2258 | |
Julia Lawall | 6a38cb1 | 2016-09-01 00:21:19 +0200 | [diff] [blame] | 2259 | static const struct ethtool_ops mtk_ethtool_ops = { |
Sean Wang | 3e60b74 | 2016-09-22 16:42:03 +0800 | [diff] [blame] | 2260 | .get_link_ksettings = mtk_get_link_ksettings, |
| 2261 | .set_link_ksettings = mtk_set_link_ksettings, |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2262 | .get_drvinfo = mtk_get_drvinfo, |
| 2263 | .get_msglevel = mtk_get_msglevel, |
| 2264 | .set_msglevel = mtk_set_msglevel, |
| 2265 | .nway_reset = mtk_nway_reset, |
| 2266 | .get_link = mtk_get_link, |
| 2267 | .get_strings = mtk_get_strings, |
| 2268 | .get_sset_count = mtk_get_sset_count, |
| 2269 | .get_ethtool_stats = mtk_get_ethtool_stats, |
Nelson Chang | 7aab747 | 2016-09-17 23:50:56 +0800 | [diff] [blame] | 2270 | .get_rxnfc = mtk_get_rxnfc, |
| 2271 | .set_rxnfc = mtk_set_rxnfc, |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2272 | }; |
| 2273 | |
| 2274 | static const struct net_device_ops mtk_netdev_ops = { |
| 2275 | .ndo_init = mtk_init, |
| 2276 | .ndo_uninit = mtk_uninit, |
| 2277 | .ndo_open = mtk_open, |
| 2278 | .ndo_stop = mtk_stop, |
| 2279 | .ndo_start_xmit = mtk_start_xmit, |
| 2280 | .ndo_set_mac_address = mtk_set_mac_address, |
| 2281 | .ndo_validate_addr = eth_validate_addr, |
| 2282 | .ndo_do_ioctl = mtk_do_ioctl, |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2283 | .ndo_tx_timeout = mtk_tx_timeout, |
| 2284 | .ndo_get_stats64 = mtk_get_stats64, |
Nelson Chang | 7aab747 | 2016-09-17 23:50:56 +0800 | [diff] [blame] | 2285 | .ndo_fix_features = mtk_fix_features, |
| 2286 | .ndo_set_features = mtk_set_features, |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2287 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 2288 | .ndo_poll_controller = mtk_poll_controller, |
| 2289 | #endif |
| 2290 | }; |
| 2291 | |
| 2292 | static int mtk_add_mac(struct mtk_eth *eth, struct device_node *np) |
| 2293 | { |
| 2294 | struct mtk_mac *mac; |
| 2295 | const __be32 *_id = of_get_property(np, "reg", NULL); |
| 2296 | int id, err; |
| 2297 | |
| 2298 | if (!_id) { |
| 2299 | dev_err(eth->dev, "missing mac id\n"); |
| 2300 | return -EINVAL; |
| 2301 | } |
| 2302 | |
| 2303 | id = be32_to_cpup(_id); |
| 2304 | if (id >= MTK_MAC_COUNT) { |
| 2305 | dev_err(eth->dev, "%d is not a valid mac id\n", id); |
| 2306 | return -EINVAL; |
| 2307 | } |
| 2308 | |
| 2309 | if (eth->netdev[id]) { |
| 2310 | dev_err(eth->dev, "duplicate mac id found: %d\n", id); |
| 2311 | return -EINVAL; |
| 2312 | } |
| 2313 | |
| 2314 | eth->netdev[id] = alloc_etherdev(sizeof(*mac)); |
| 2315 | if (!eth->netdev[id]) { |
| 2316 | dev_err(eth->dev, "alloc_etherdev failed\n"); |
| 2317 | return -ENOMEM; |
| 2318 | } |
| 2319 | mac = netdev_priv(eth->netdev[id]); |
| 2320 | eth->mac[id] = mac; |
| 2321 | mac->id = id; |
| 2322 | mac->hw = eth; |
| 2323 | mac->of_node = np; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2324 | |
Nelson Chang | ee40681 | 2016-09-17 23:50:55 +0800 | [diff] [blame] | 2325 | memset(mac->hwlro_ip, 0, sizeof(mac->hwlro_ip)); |
| 2326 | mac->hwlro_ip_cnt = 0; |
| 2327 | |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2328 | mac->hw_stats = devm_kzalloc(eth->dev, |
| 2329 | sizeof(*mac->hw_stats), |
| 2330 | GFP_KERNEL); |
| 2331 | if (!mac->hw_stats) { |
| 2332 | dev_err(eth->dev, "failed to allocate counter memory\n"); |
| 2333 | err = -ENOMEM; |
| 2334 | goto free_netdev; |
| 2335 | } |
| 2336 | spin_lock_init(&mac->hw_stats->stats_lock); |
sean.wang@mediatek.com | d7005652 | 2016-08-13 19:16:18 +0800 | [diff] [blame] | 2337 | u64_stats_init(&mac->hw_stats->syncp); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2338 | mac->hw_stats->reg_offset = id * MTK_STAT_OFFSET; |
| 2339 | |
| 2340 | SET_NETDEV_DEV(eth->netdev[id], eth->dev); |
John Crispin | eaadf9f | 2016-06-10 13:28:05 +0200 | [diff] [blame] | 2341 | eth->netdev[id]->watchdog_timeo = 5 * HZ; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2342 | eth->netdev[id]->netdev_ops = &mtk_netdev_ops; |
| 2343 | eth->netdev[id]->base_addr = (unsigned long)eth->base; |
Nelson Chang | ee40681 | 2016-09-17 23:50:55 +0800 | [diff] [blame] | 2344 | |
| 2345 | eth->netdev[id]->hw_features = MTK_HW_FEATURES; |
| 2346 | if (eth->hwlro) |
| 2347 | eth->netdev[id]->hw_features |= NETIF_F_LRO; |
| 2348 | |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2349 | eth->netdev[id]->vlan_features = MTK_HW_FEATURES & |
| 2350 | ~(NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX); |
| 2351 | eth->netdev[id]->features |= MTK_HW_FEATURES; |
| 2352 | eth->netdev[id]->ethtool_ops = &mtk_ethtool_ops; |
| 2353 | |
John Crispin | 8067302 | 2016-06-29 13:38:11 +0200 | [diff] [blame] | 2354 | eth->netdev[id]->irq = eth->irq[0]; |
Sean Wang | 3174b3b | 2017-04-07 16:45:08 +0800 | [diff] [blame] | 2355 | eth->netdev[id]->dev.of_node = np; |
| 2356 | |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2357 | return 0; |
| 2358 | |
| 2359 | free_netdev: |
| 2360 | free_netdev(eth->netdev[id]); |
| 2361 | return err; |
| 2362 | } |
| 2363 | |
Nelson Chang | b95b6d9 | 2016-10-06 19:44:01 +0800 | [diff] [blame] | 2364 | static int mtk_get_chip_id(struct mtk_eth *eth, u32 *chip_id) |
| 2365 | { |
| 2366 | u32 val[2], id[4]; |
| 2367 | |
| 2368 | regmap_read(eth->ethsys, ETHSYS_CHIPID0_3, &val[0]); |
| 2369 | regmap_read(eth->ethsys, ETHSYS_CHIPID4_7, &val[1]); |
| 2370 | |
| 2371 | id[3] = ((val[0] >> 16) & 0xff) - '0'; |
| 2372 | id[2] = ((val[0] >> 24) & 0xff) - '0'; |
| 2373 | id[1] = (val[1] & 0xff) - '0'; |
| 2374 | id[0] = ((val[1] >> 8) & 0xff) - '0'; |
| 2375 | |
| 2376 | *chip_id = (id[3] * 1000) + (id[2] * 100) + |
| 2377 | (id[1] * 10) + id[0]; |
| 2378 | |
| 2379 | if (!(*chip_id)) { |
| 2380 | dev_err(eth->dev, "failed to get chip id\n"); |
| 2381 | return -ENODEV; |
| 2382 | } |
| 2383 | |
| 2384 | dev_info(eth->dev, "chip id = %d\n", *chip_id); |
| 2385 | |
| 2386 | return 0; |
| 2387 | } |
| 2388 | |
Nelson Chang | 983e1a6 | 2016-10-06 19:44:02 +0800 | [diff] [blame] | 2389 | static bool mtk_is_hwlro_supported(struct mtk_eth *eth) |
| 2390 | { |
| 2391 | switch (eth->chip_id) { |
| 2392 | case MT7623_ETH: |
| 2393 | return true; |
| 2394 | } |
| 2395 | |
| 2396 | return false; |
| 2397 | } |
| 2398 | |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2399 | static int mtk_probe(struct platform_device *pdev) |
| 2400 | { |
| 2401 | struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 2402 | struct device_node *mac_np; |
| 2403 | const struct of_device_id *match; |
| 2404 | struct mtk_soc_data *soc; |
| 2405 | struct mtk_eth *eth; |
| 2406 | int err; |
John Crispin | 8067302 | 2016-06-29 13:38:11 +0200 | [diff] [blame] | 2407 | int i; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2408 | |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2409 | match = of_match_device(of_mtk_match, &pdev->dev); |
| 2410 | soc = (struct mtk_soc_data *)match->data; |
| 2411 | |
| 2412 | eth = devm_kzalloc(&pdev->dev, sizeof(*eth), GFP_KERNEL); |
| 2413 | if (!eth) |
| 2414 | return -ENOMEM; |
| 2415 | |
Sean Wang | 549e549 | 2016-09-01 10:47:28 +0800 | [diff] [blame] | 2416 | eth->dev = &pdev->dev; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2417 | eth->base = devm_ioremap_resource(&pdev->dev, res); |
Vladimir Zapolskiy | 621e49f | 2016-03-23 01:06:04 +0200 | [diff] [blame] | 2418 | if (IS_ERR(eth->base)) |
| 2419 | return PTR_ERR(eth->base); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2420 | |
| 2421 | spin_lock_init(ð->page_lock); |
John Crispin | 5cce032 | 2017-06-19 15:37:05 +0200 | [diff] [blame^] | 2422 | spin_lock_init(ð->tx_irq_lock); |
| 2423 | spin_lock_init(ð->rx_irq_lock); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2424 | |
| 2425 | eth->ethsys = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, |
| 2426 | "mediatek,ethsys"); |
| 2427 | if (IS_ERR(eth->ethsys)) { |
| 2428 | dev_err(&pdev->dev, "no ethsys regmap found\n"); |
| 2429 | return PTR_ERR(eth->ethsys); |
| 2430 | } |
| 2431 | |
| 2432 | eth->pctl = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, |
| 2433 | "mediatek,pctl"); |
| 2434 | if (IS_ERR(eth->pctl)) { |
| 2435 | dev_err(&pdev->dev, "no pctl regmap found\n"); |
| 2436 | return PTR_ERR(eth->pctl); |
| 2437 | } |
| 2438 | |
John Crispin | 8067302 | 2016-06-29 13:38:11 +0200 | [diff] [blame] | 2439 | for (i = 0; i < 3; i++) { |
| 2440 | eth->irq[i] = platform_get_irq(pdev, i); |
| 2441 | if (eth->irq[i] < 0) { |
| 2442 | dev_err(&pdev->dev, "no IRQ%d resource found\n", i); |
| 2443 | return -ENXIO; |
| 2444 | } |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2445 | } |
Sean Wang | 549e549 | 2016-09-01 10:47:28 +0800 | [diff] [blame] | 2446 | for (i = 0; i < ARRAY_SIZE(eth->clks); i++) { |
| 2447 | eth->clks[i] = devm_clk_get(eth->dev, |
| 2448 | mtk_clks_source_name[i]); |
| 2449 | if (IS_ERR(eth->clks[i])) { |
| 2450 | if (PTR_ERR(eth->clks[i]) == -EPROBE_DEFER) |
| 2451 | return -EPROBE_DEFER; |
| 2452 | return -ENODEV; |
| 2453 | } |
| 2454 | } |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2455 | |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2456 | eth->msg_enable = netif_msg_init(mtk_msg_level, MTK_DEFAULT_MSG_ENABLE); |
John Crispin | 7c78b4a | 2016-04-08 00:54:10 +0200 | [diff] [blame] | 2457 | INIT_WORK(ð->pending_work, mtk_pending_work); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2458 | |
| 2459 | err = mtk_hw_init(eth); |
| 2460 | if (err) |
| 2461 | return err; |
| 2462 | |
Nelson Chang | b95b6d9 | 2016-10-06 19:44:01 +0800 | [diff] [blame] | 2463 | err = mtk_get_chip_id(eth, ð->chip_id); |
| 2464 | if (err) |
| 2465 | return err; |
| 2466 | |
Nelson Chang | 983e1a6 | 2016-10-06 19:44:02 +0800 | [diff] [blame] | 2467 | eth->hwlro = mtk_is_hwlro_supported(eth); |
| 2468 | |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2469 | for_each_child_of_node(pdev->dev.of_node, mac_np) { |
| 2470 | if (!of_device_is_compatible(mac_np, |
| 2471 | "mediatek,eth-mac")) |
| 2472 | continue; |
| 2473 | |
| 2474 | if (!of_device_is_available(mac_np)) |
| 2475 | continue; |
| 2476 | |
| 2477 | err = mtk_add_mac(eth, mac_np); |
| 2478 | if (err) |
Sean Wang | 8a8a9e8 | 2016-09-14 23:13:17 +0800 | [diff] [blame] | 2479 | goto err_deinit_hw; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2480 | } |
| 2481 | |
Sean Wang | 85574db | 2016-09-14 23:13:15 +0800 | [diff] [blame] | 2482 | err = devm_request_irq(eth->dev, eth->irq[1], mtk_handle_irq_tx, 0, |
| 2483 | dev_name(eth->dev), eth); |
| 2484 | if (err) |
| 2485 | goto err_free_dev; |
| 2486 | |
| 2487 | err = devm_request_irq(eth->dev, eth->irq[2], mtk_handle_irq_rx, 0, |
| 2488 | dev_name(eth->dev), eth); |
| 2489 | if (err) |
| 2490 | goto err_free_dev; |
| 2491 | |
| 2492 | err = mtk_mdio_init(eth); |
| 2493 | if (err) |
| 2494 | goto err_free_dev; |
| 2495 | |
| 2496 | for (i = 0; i < MTK_MAX_DEVS; i++) { |
| 2497 | if (!eth->netdev[i]) |
| 2498 | continue; |
| 2499 | |
| 2500 | err = register_netdev(eth->netdev[i]); |
| 2501 | if (err) { |
| 2502 | dev_err(eth->dev, "error bringing up device\n"); |
Sean Wang | 8a8a9e8 | 2016-09-14 23:13:17 +0800 | [diff] [blame] | 2503 | goto err_deinit_mdio; |
Sean Wang | 85574db | 2016-09-14 23:13:15 +0800 | [diff] [blame] | 2504 | } else |
| 2505 | netif_info(eth, probe, eth->netdev[i], |
| 2506 | "mediatek frame engine at 0x%08lx, irq %d\n", |
| 2507 | eth->netdev[i]->base_addr, eth->irq[0]); |
| 2508 | } |
| 2509 | |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2510 | /* we run 2 devices on the same DMA ring so we need a dummy device |
| 2511 | * for NAPI to work |
| 2512 | */ |
| 2513 | init_dummy_netdev(ð->dummy_dev); |
John Crispin | 8067302 | 2016-06-29 13:38:11 +0200 | [diff] [blame] | 2514 | netif_napi_add(ð->dummy_dev, ð->tx_napi, mtk_napi_tx, |
| 2515 | MTK_NAPI_WEIGHT); |
| 2516 | netif_napi_add(ð->dummy_dev, ð->rx_napi, mtk_napi_rx, |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2517 | MTK_NAPI_WEIGHT); |
| 2518 | |
| 2519 | platform_set_drvdata(pdev, eth); |
| 2520 | |
| 2521 | return 0; |
| 2522 | |
Sean Wang | 8a8a9e8 | 2016-09-14 23:13:17 +0800 | [diff] [blame] | 2523 | err_deinit_mdio: |
| 2524 | mtk_mdio_cleanup(eth); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2525 | err_free_dev: |
Sean Wang | 8a8a9e8 | 2016-09-14 23:13:17 +0800 | [diff] [blame] | 2526 | mtk_free_dev(eth); |
| 2527 | err_deinit_hw: |
| 2528 | mtk_hw_deinit(eth); |
| 2529 | |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2530 | return err; |
| 2531 | } |
| 2532 | |
| 2533 | static int mtk_remove(struct platform_device *pdev) |
| 2534 | { |
| 2535 | struct mtk_eth *eth = platform_get_drvdata(pdev); |
Sean Wang | 79e9a41 | 2016-09-01 10:47:32 +0800 | [diff] [blame] | 2536 | int i; |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2537 | |
Sean Wang | 79e9a41 | 2016-09-01 10:47:32 +0800 | [diff] [blame] | 2538 | /* stop all devices to make sure that dma is properly shut down */ |
| 2539 | for (i = 0; i < MTK_MAC_COUNT; i++) { |
| 2540 | if (!eth->netdev[i]) |
| 2541 | continue; |
| 2542 | mtk_stop(eth->netdev[i]); |
| 2543 | } |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2544 | |
Sean Wang | bf253fb | 2016-09-14 23:13:16 +0800 | [diff] [blame] | 2545 | mtk_hw_deinit(eth); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2546 | |
John Crispin | 8067302 | 2016-06-29 13:38:11 +0200 | [diff] [blame] | 2547 | netif_napi_del(ð->tx_napi); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2548 | netif_napi_del(ð->rx_napi); |
| 2549 | mtk_cleanup(eth); |
Sean Wang | e82f714 | 2016-09-20 23:53:24 +0800 | [diff] [blame] | 2550 | mtk_mdio_cleanup(eth); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2551 | |
| 2552 | return 0; |
| 2553 | } |
| 2554 | |
| 2555 | const struct of_device_id of_mtk_match[] = { |
John Crispin | 8b901f6 | 2017-01-25 09:20:55 +0100 | [diff] [blame] | 2556 | { .compatible = "mediatek,mt2701-eth" }, |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2557 | {}, |
| 2558 | }; |
Sean Wang | 7077dc4 | 2016-09-14 21:29:34 +0800 | [diff] [blame] | 2559 | MODULE_DEVICE_TABLE(of, of_mtk_match); |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2560 | |
| 2561 | static struct platform_driver mtk_driver = { |
| 2562 | .probe = mtk_probe, |
| 2563 | .remove = mtk_remove, |
| 2564 | .driver = { |
| 2565 | .name = "mtk_soc_eth", |
John Crispin | 656e705 | 2016-03-08 11:29:55 +0100 | [diff] [blame] | 2566 | .of_match_table = of_mtk_match, |
| 2567 | }, |
| 2568 | }; |
| 2569 | |
| 2570 | module_platform_driver(mtk_driver); |
| 2571 | |
| 2572 | MODULE_LICENSE("GPL"); |
| 2573 | MODULE_AUTHOR("John Crispin <blogic@openwrt.org>"); |
| 2574 | MODULE_DESCRIPTION("Ethernet driver for MediaTek SoC"); |