Grygorii Strashko | 68cf027 | 2019-04-26 20:12:23 +0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Texas Instruments Ethernet Switch Driver |
| 4 | * |
| 5 | * Copyright (C) 2012 Texas Instruments |
| 6 | * |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/io.h> |
| 11 | #include <linux/clk.h> |
| 12 | #include <linux/timer.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/platform_device.h> |
| 15 | #include <linux/irqreturn.h> |
| 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/if_ether.h> |
| 18 | #include <linux/etherdevice.h> |
| 19 | #include <linux/netdevice.h> |
Richard Cochran | 2e5b38a | 2012-10-29 08:45:20 +0000 | [diff] [blame] | 20 | #include <linux/net_tstamp.h> |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 21 | #include <linux/phy.h> |
Grygorii Strashko | 3ff1884 | 2018-11-25 18:15:25 -0600 | [diff] [blame] | 22 | #include <linux/phy/phy.h> |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 23 | #include <linux/workqueue.h> |
| 24 | #include <linux/delay.h> |
Mugunthan V N | f150bd7 | 2012-07-17 08:09:50 +0000 | [diff] [blame] | 25 | #include <linux/pm_runtime.h> |
Arnd Bergmann | e2b3e49 | 2018-05-30 23:51:54 +0200 | [diff] [blame] | 26 | #include <linux/gpio/consumer.h> |
Mugunthan V N | 2eb32b0 | 2012-07-30 10:17:14 +0000 | [diff] [blame] | 27 | #include <linux/of.h> |
Heiko Schocher | 9e42f71 | 2015-10-17 06:04:35 +0200 | [diff] [blame] | 28 | #include <linux/of_mdio.h> |
Mugunthan V N | 2eb32b0 | 2012-07-30 10:17:14 +0000 | [diff] [blame] | 29 | #include <linux/of_net.h> |
| 30 | #include <linux/of_device.h> |
Mugunthan V N | 3b72c2f | 2013-02-05 08:26:48 +0000 | [diff] [blame] | 31 | #include <linux/if_vlan.h> |
Randy Dunlap | 514c603 | 2018-04-05 16:25:34 -0700 | [diff] [blame] | 32 | #include <linux/kmemleak.h> |
Ivan Khoronzhuk | 9611d6d | 2018-05-17 01:21:45 +0300 | [diff] [blame] | 33 | #include <linux/sys_soc.h> |
Ivan Khoronzhuk | 9ed4050 | 2019-07-09 00:34:32 +0300 | [diff] [blame] | 34 | #include <net/page_pool.h> |
| 35 | #include <linux/bpf.h> |
| 36 | #include <linux/bpf_trace.h> |
| 37 | #include <linux/filter.h> |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 38 | |
Mugunthan V N | 739683b | 2013-06-06 23:45:14 +0530 | [diff] [blame] | 39 | #include <linux/pinctrl/consumer.h> |
Ivan Khoronzhuk | 7929a66 | 2018-07-24 00:26:31 +0300 | [diff] [blame] | 40 | #include <net/pkt_cls.h> |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 41 | |
Mugunthan V N | dbe3472 | 2013-08-19 17:47:40 +0530 | [diff] [blame] | 42 | #include "cpsw.h" |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 43 | #include "cpsw_ale.h" |
Grygorii Strashko | 814b4a6 | 2019-04-26 20:12:37 +0300 | [diff] [blame] | 44 | #include "cpsw_priv.h" |
Grygorii Strashko | cfc0834 | 2019-04-26 20:12:41 +0300 | [diff] [blame] | 45 | #include "cpsw_sl.h" |
Richard Cochran | 2e5b38a | 2012-10-29 08:45:20 +0000 | [diff] [blame] | 46 | #include "cpts.h" |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 47 | #include "davinci_cpdma.h" |
| 48 | |
Ivan Khoronzhuk | 57d9014 | 2018-07-24 00:26:32 +0300 | [diff] [blame] | 49 | #include <net/pkt_sched.h> |
| 50 | |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 51 | static int debug_level; |
| 52 | module_param(debug_level, int, 0); |
| 53 | MODULE_PARM_DESC(debug_level, "cpsw debug level (NETIF_MSG bits)"); |
| 54 | |
| 55 | static int ale_ageout = 10; |
| 56 | module_param(ale_ageout, int, 0); |
| 57 | MODULE_PARM_DESC(ale_ageout, "cpsw ale ageout interval (seconds)"); |
| 58 | |
| 59 | static int rx_packet_max = CPSW_MAX_PACKET_SIZE; |
| 60 | module_param(rx_packet_max, int, 0); |
| 61 | MODULE_PARM_DESC(rx_packet_max, "maximum receive packet size (bytes)"); |
| 62 | |
Grygorii Strashko | 90225bf | 2017-01-06 14:07:33 -0600 | [diff] [blame] | 63 | static int descs_pool_size = CPSW_CPDMA_DESCS_POOL_SIZE_DEFAULT; |
| 64 | module_param(descs_pool_size, int, 0444); |
| 65 | MODULE_PARM_DESC(descs_pool_size, "Number of CPDMA CPPI descriptors in pool"); |
| 66 | |
Ivan Khoronzhuk | 9ed4050 | 2019-07-09 00:34:32 +0300 | [diff] [blame] | 67 | /* The buf includes headroom compatible with both skb and xdpf */ |
| 68 | #define CPSW_HEADROOM_NA (max(XDP_PACKET_HEADROOM, NET_SKB_PAD) + NET_IP_ALIGN) |
| 69 | #define CPSW_HEADROOM ALIGN(CPSW_HEADROOM_NA, sizeof(long)) |
| 70 | |
Mugunthan V N | d9ba8f9 | 2013-02-11 09:52:20 +0000 | [diff] [blame] | 71 | #define for_each_slave(priv, func, arg...) \ |
| 72 | do { \ |
Sebastian Siewior | 6e6ceae | 2013-04-24 08:48:24 +0000 | [diff] [blame] | 73 | struct cpsw_slave *slave; \ |
Ivan Khoronzhuk | 606f399 | 2016-08-10 02:22:42 +0300 | [diff] [blame] | 74 | struct cpsw_common *cpsw = (priv)->cpsw; \ |
Sebastian Siewior | 6e6ceae | 2013-04-24 08:48:24 +0000 | [diff] [blame] | 75 | int n; \ |
Ivan Khoronzhuk | 606f399 | 2016-08-10 02:22:42 +0300 | [diff] [blame] | 76 | if (cpsw->data.dual_emac) \ |
| 77 | (func)((cpsw)->slaves + priv->emac_port, ##arg);\ |
Mugunthan V N | d9ba8f9 | 2013-02-11 09:52:20 +0000 | [diff] [blame] | 78 | else \ |
Ivan Khoronzhuk | 606f399 | 2016-08-10 02:22:42 +0300 | [diff] [blame] | 79 | for (n = cpsw->data.slaves, \ |
| 80 | slave = cpsw->slaves; \ |
Sebastian Siewior | 6e6ceae | 2013-04-24 08:48:24 +0000 | [diff] [blame] | 81 | n; n--) \ |
| 82 | (func)(slave++, ##arg); \ |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 83 | } while (0) |
Mugunthan V N | d9ba8f9 | 2013-02-11 09:52:20 +0000 | [diff] [blame] | 84 | |
Ivan Khoronzhuk | 9ed4050 | 2019-07-09 00:34:32 +0300 | [diff] [blame] | 85 | #define CPSW_XMETA_OFFSET ALIGN(sizeof(struct xdp_frame), sizeof(long)) |
| 86 | |
| 87 | #define CPSW_XDP_CONSUMED 1 |
| 88 | #define CPSW_XDP_PASS 0 |
| 89 | |
Ivan Khoronzhuk | 00fe471 | 2018-11-08 22:27:57 +0200 | [diff] [blame] | 90 | static int cpsw_ndo_vlan_rx_add_vid(struct net_device *ndev, |
| 91 | __be16 proto, u16 vid); |
| 92 | |
Mugunthan V N | 0cd8f9c | 2014-01-23 00:03:12 +0530 | [diff] [blame] | 93 | static void cpsw_set_promiscious(struct net_device *ndev, bool enable) |
| 94 | { |
Ivan Khoronzhuk | 2a05a62 | 2016-08-10 02:22:44 +0300 | [diff] [blame] | 95 | struct cpsw_common *cpsw = ndev_to_cpsw(ndev); |
| 96 | struct cpsw_ale *ale = cpsw->ale; |
Mugunthan V N | 0cd8f9c | 2014-01-23 00:03:12 +0530 | [diff] [blame] | 97 | int i; |
| 98 | |
Ivan Khoronzhuk | 606f399 | 2016-08-10 02:22:42 +0300 | [diff] [blame] | 99 | if (cpsw->data.dual_emac) { |
Mugunthan V N | 0cd8f9c | 2014-01-23 00:03:12 +0530 | [diff] [blame] | 100 | bool flag = false; |
| 101 | |
| 102 | /* Enabling promiscuous mode for one interface will be |
| 103 | * common for both the interface as the interface shares |
| 104 | * the same hardware resource. |
| 105 | */ |
Ivan Khoronzhuk | 606f399 | 2016-08-10 02:22:42 +0300 | [diff] [blame] | 106 | for (i = 0; i < cpsw->data.slaves; i++) |
| 107 | if (cpsw->slaves[i].ndev->flags & IFF_PROMISC) |
Mugunthan V N | 0cd8f9c | 2014-01-23 00:03:12 +0530 | [diff] [blame] | 108 | flag = true; |
| 109 | |
| 110 | if (!enable && flag) { |
| 111 | enable = true; |
| 112 | dev_err(&ndev->dev, "promiscuity not disabled as the other interface is still in promiscuity mode\n"); |
| 113 | } |
| 114 | |
| 115 | if (enable) { |
| 116 | /* Enable Bypass */ |
| 117 | cpsw_ale_control_set(ale, 0, ALE_BYPASS, 1); |
| 118 | |
| 119 | dev_dbg(&ndev->dev, "promiscuity enabled\n"); |
| 120 | } else { |
| 121 | /* Disable Bypass */ |
| 122 | cpsw_ale_control_set(ale, 0, ALE_BYPASS, 0); |
| 123 | dev_dbg(&ndev->dev, "promiscuity disabled\n"); |
| 124 | } |
| 125 | } else { |
| 126 | if (enable) { |
| 127 | unsigned long timeout = jiffies + HZ; |
| 128 | |
Lennart Sorensen | 6f979eb | 2014-10-31 13:28:54 -0400 | [diff] [blame] | 129 | /* Disable Learn for all ports (host is port 0 and slaves are port 1 and up */ |
Ivan Khoronzhuk | 606f399 | 2016-08-10 02:22:42 +0300 | [diff] [blame] | 130 | for (i = 0; i <= cpsw->data.slaves; i++) { |
Mugunthan V N | 0cd8f9c | 2014-01-23 00:03:12 +0530 | [diff] [blame] | 131 | cpsw_ale_control_set(ale, i, |
| 132 | ALE_PORT_NOLEARN, 1); |
| 133 | cpsw_ale_control_set(ale, i, |
| 134 | ALE_PORT_NO_SA_UPDATE, 1); |
| 135 | } |
| 136 | |
| 137 | /* Clear All Untouched entries */ |
| 138 | cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1); |
| 139 | do { |
| 140 | cpu_relax(); |
| 141 | if (cpsw_ale_control_get(ale, 0, ALE_AGEOUT)) |
| 142 | break; |
| 143 | } while (time_after(timeout, jiffies)); |
| 144 | cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1); |
| 145 | |
| 146 | /* Clear all mcast from ALE */ |
Grygorii Strashko | 61f1cef | 2016-04-07 15:16:43 +0300 | [diff] [blame] | 147 | cpsw_ale_flush_multicast(ale, ALE_ALL_PORTS, -1); |
Ivan Khoronzhuk | 15180ec | 2018-11-08 22:27:56 +0200 | [diff] [blame] | 148 | __hw_addr_ref_unsync_dev(&ndev->mc, ndev, NULL); |
Mugunthan V N | 0cd8f9c | 2014-01-23 00:03:12 +0530 | [diff] [blame] | 149 | |
| 150 | /* Flood All Unicast Packets to Host port */ |
| 151 | cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 1); |
| 152 | dev_dbg(&ndev->dev, "promiscuity enabled\n"); |
| 153 | } else { |
Lennart Sorensen | 6f979eb | 2014-10-31 13:28:54 -0400 | [diff] [blame] | 154 | /* Don't Flood All Unicast Packets to Host port */ |
Mugunthan V N | 0cd8f9c | 2014-01-23 00:03:12 +0530 | [diff] [blame] | 155 | cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 0); |
| 156 | |
Lennart Sorensen | 6f979eb | 2014-10-31 13:28:54 -0400 | [diff] [blame] | 157 | /* Enable Learn for all ports (host is port 0 and slaves are port 1 and up */ |
Ivan Khoronzhuk | 606f399 | 2016-08-10 02:22:42 +0300 | [diff] [blame] | 158 | for (i = 0; i <= cpsw->data.slaves; i++) { |
Mugunthan V N | 0cd8f9c | 2014-01-23 00:03:12 +0530 | [diff] [blame] | 159 | cpsw_ale_control_set(ale, i, |
| 160 | ALE_PORT_NOLEARN, 0); |
| 161 | cpsw_ale_control_set(ale, i, |
| 162 | ALE_PORT_NO_SA_UPDATE, 0); |
| 163 | } |
| 164 | dev_dbg(&ndev->dev, "promiscuity disabled\n"); |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
Ivan Khoronzhuk | 15180ec | 2018-11-08 22:27:56 +0200 | [diff] [blame] | 169 | /** |
| 170 | * cpsw_set_mc - adds multicast entry to the table if it's not added or deletes |
| 171 | * if it's not deleted |
| 172 | * @ndev: device to sync |
| 173 | * @addr: address to be added or deleted |
| 174 | * @vid: vlan id, if vid < 0 set/unset address for real device |
| 175 | * @add: add address if the flag is set or remove otherwise |
| 176 | */ |
| 177 | static int cpsw_set_mc(struct net_device *ndev, const u8 *addr, |
| 178 | int vid, int add) |
Mugunthan V N | 5c50a85 | 2012-10-29 08:45:11 +0000 | [diff] [blame] | 179 | { |
| 180 | struct cpsw_priv *priv = netdev_priv(ndev); |
Ivan Khoronzhuk | 606f399 | 2016-08-10 02:22:42 +0300 | [diff] [blame] | 181 | struct cpsw_common *cpsw = priv->cpsw; |
Ivan Khoronzhuk | 15180ec | 2018-11-08 22:27:56 +0200 | [diff] [blame] | 182 | int mask, flags, ret; |
Mugunthan V N | 2590605 | 2015-01-13 17:35:49 +0530 | [diff] [blame] | 183 | |
Ivan Khoronzhuk | 15180ec | 2018-11-08 22:27:56 +0200 | [diff] [blame] | 184 | if (vid < 0) { |
| 185 | if (cpsw->data.dual_emac) |
| 186 | vid = cpsw->slaves[priv->emac_port].port_vlan; |
| 187 | else |
| 188 | vid = 0; |
Ivan Khoronzhuk | 5da1948 | 2018-10-12 18:28:15 +0300 | [diff] [blame] | 189 | } |
| 190 | |
Ivan Khoronzhuk | 15180ec | 2018-11-08 22:27:56 +0200 | [diff] [blame] | 191 | mask = cpsw->data.dual_emac ? ALE_PORT_HOST : ALE_ALL_PORTS; |
| 192 | flags = vid ? ALE_VLAN : 0; |
| 193 | |
| 194 | if (add) |
| 195 | ret = cpsw_ale_add_mcast(cpsw->ale, addr, mask, flags, vid, 0); |
| 196 | else |
| 197 | ret = cpsw_ale_del_mcast(cpsw->ale, addr, 0, flags, vid); |
| 198 | |
| 199 | return ret; |
| 200 | } |
| 201 | |
| 202 | static int cpsw_update_vlan_mc(struct net_device *vdev, int vid, void *ctx) |
| 203 | { |
| 204 | struct addr_sync_ctx *sync_ctx = ctx; |
| 205 | struct netdev_hw_addr *ha; |
| 206 | int found = 0, ret = 0; |
| 207 | |
| 208 | if (!vdev || !(vdev->flags & IFF_UP)) |
| 209 | return 0; |
| 210 | |
| 211 | /* vlan address is relevant if its sync_cnt != 0 */ |
| 212 | netdev_for_each_mc_addr(ha, vdev) { |
| 213 | if (ether_addr_equal(ha->addr, sync_ctx->addr)) { |
| 214 | found = ha->sync_cnt; |
| 215 | break; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | if (found) |
| 220 | sync_ctx->consumed++; |
| 221 | |
| 222 | if (sync_ctx->flush) { |
| 223 | if (!found) |
| 224 | cpsw_set_mc(sync_ctx->ndev, sync_ctx->addr, vid, 0); |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | if (found) |
| 229 | ret = cpsw_set_mc(sync_ctx->ndev, sync_ctx->addr, vid, 1); |
| 230 | |
| 231 | return ret; |
| 232 | } |
| 233 | |
| 234 | static int cpsw_add_mc_addr(struct net_device *ndev, const u8 *addr, int num) |
| 235 | { |
| 236 | struct addr_sync_ctx sync_ctx; |
| 237 | int ret; |
| 238 | |
| 239 | sync_ctx.consumed = 0; |
| 240 | sync_ctx.addr = addr; |
| 241 | sync_ctx.ndev = ndev; |
| 242 | sync_ctx.flush = 0; |
| 243 | |
| 244 | ret = vlan_for_each(ndev, cpsw_update_vlan_mc, &sync_ctx); |
| 245 | if (sync_ctx.consumed < num && !ret) |
| 246 | ret = cpsw_set_mc(ndev, addr, -1, 1); |
| 247 | |
| 248 | return ret; |
| 249 | } |
| 250 | |
| 251 | static int cpsw_del_mc_addr(struct net_device *ndev, const u8 *addr, int num) |
| 252 | { |
| 253 | struct addr_sync_ctx sync_ctx; |
| 254 | |
| 255 | sync_ctx.consumed = 0; |
| 256 | sync_ctx.addr = addr; |
| 257 | sync_ctx.ndev = ndev; |
| 258 | sync_ctx.flush = 1; |
| 259 | |
| 260 | vlan_for_each(ndev, cpsw_update_vlan_mc, &sync_ctx); |
| 261 | if (sync_ctx.consumed == num) |
| 262 | cpsw_set_mc(ndev, addr, -1, 0); |
| 263 | |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | static int cpsw_purge_vlan_mc(struct net_device *vdev, int vid, void *ctx) |
| 268 | { |
| 269 | struct addr_sync_ctx *sync_ctx = ctx; |
| 270 | struct netdev_hw_addr *ha; |
| 271 | int found = 0; |
| 272 | |
| 273 | if (!vdev || !(vdev->flags & IFF_UP)) |
| 274 | return 0; |
| 275 | |
| 276 | /* vlan address is relevant if its sync_cnt != 0 */ |
| 277 | netdev_for_each_mc_addr(ha, vdev) { |
| 278 | if (ether_addr_equal(ha->addr, sync_ctx->addr)) { |
| 279 | found = ha->sync_cnt; |
| 280 | break; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | if (!found) |
| 285 | return 0; |
| 286 | |
| 287 | sync_ctx->consumed++; |
| 288 | cpsw_set_mc(sync_ctx->ndev, sync_ctx->addr, vid, 0); |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | static int cpsw_purge_all_mc(struct net_device *ndev, const u8 *addr, int num) |
| 293 | { |
| 294 | struct addr_sync_ctx sync_ctx; |
| 295 | |
| 296 | sync_ctx.addr = addr; |
| 297 | sync_ctx.ndev = ndev; |
| 298 | sync_ctx.consumed = 0; |
| 299 | |
| 300 | vlan_for_each(ndev, cpsw_purge_vlan_mc, &sync_ctx); |
| 301 | if (sync_ctx.consumed < num) |
| 302 | cpsw_set_mc(ndev, addr, -1, 0); |
| 303 | |
Ivan Khoronzhuk | 5da1948 | 2018-10-12 18:28:15 +0300 | [diff] [blame] | 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | static void cpsw_ndo_set_rx_mode(struct net_device *ndev) |
| 308 | { |
Grygorii Strashko | 06095f3 | 2019-04-26 20:12:33 +0300 | [diff] [blame] | 309 | struct cpsw_priv *priv = netdev_priv(ndev); |
| 310 | struct cpsw_common *cpsw = priv->cpsw; |
| 311 | int slave_port = -1; |
| 312 | |
| 313 | if (cpsw->data.dual_emac) |
| 314 | slave_port = priv->emac_port + 1; |
Mugunthan V N | 5c50a85 | 2012-10-29 08:45:11 +0000 | [diff] [blame] | 315 | |
| 316 | if (ndev->flags & IFF_PROMISC) { |
| 317 | /* Enable promiscuous mode */ |
Mugunthan V N | 0cd8f9c | 2014-01-23 00:03:12 +0530 | [diff] [blame] | 318 | cpsw_set_promiscious(ndev, true); |
Grygorii Strashko | 06095f3 | 2019-04-26 20:12:33 +0300 | [diff] [blame] | 319 | cpsw_ale_set_allmulti(cpsw->ale, IFF_ALLMULTI, slave_port); |
Mugunthan V N | 5c50a85 | 2012-10-29 08:45:11 +0000 | [diff] [blame] | 320 | return; |
Mugunthan V N | 0cd8f9c | 2014-01-23 00:03:12 +0530 | [diff] [blame] | 321 | } else { |
| 322 | /* Disable promiscuous mode */ |
| 323 | cpsw_set_promiscious(ndev, false); |
Mugunthan V N | 5c50a85 | 2012-10-29 08:45:11 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Lennart Sorensen | 1e5c4bc | 2014-10-31 13:38:52 -0400 | [diff] [blame] | 326 | /* Restore allmulti on vlans if necessary */ |
Grygorii Strashko | 06095f3 | 2019-04-26 20:12:33 +0300 | [diff] [blame] | 327 | cpsw_ale_set_allmulti(cpsw->ale, |
| 328 | ndev->flags & IFF_ALLMULTI, slave_port); |
Lennart Sorensen | 1e5c4bc | 2014-10-31 13:38:52 -0400 | [diff] [blame] | 329 | |
Ivan Khoronzhuk | 15180ec | 2018-11-08 22:27:56 +0200 | [diff] [blame] | 330 | /* add/remove mcast address either for real netdev or for vlan */ |
| 331 | __hw_addr_ref_sync_dev(&ndev->mc, ndev, cpsw_add_mc_addr, |
| 332 | cpsw_del_mc_addr); |
Mugunthan V N | 5c50a85 | 2012-10-29 08:45:11 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Grygorii Strashko | c24eef2 | 2019-04-26 20:12:42 +0300 | [diff] [blame] | 335 | void cpsw_intr_enable(struct cpsw_common *cpsw) |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 336 | { |
Grygorii Strashko | dda5f5fe | 2017-11-30 18:21:11 -0600 | [diff] [blame] | 337 | writel_relaxed(0xFF, &cpsw->wr_regs->tx_en); |
| 338 | writel_relaxed(0xFF, &cpsw->wr_regs->rx_en); |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 339 | |
Ivan Khoronzhuk | 2c836bd | 2016-08-10 02:22:40 +0300 | [diff] [blame] | 340 | cpdma_ctlr_int_ctrl(cpsw->dma, true); |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 341 | return; |
| 342 | } |
| 343 | |
Grygorii Strashko | c24eef2 | 2019-04-26 20:12:42 +0300 | [diff] [blame] | 344 | void cpsw_intr_disable(struct cpsw_common *cpsw) |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 345 | { |
Grygorii Strashko | dda5f5fe | 2017-11-30 18:21:11 -0600 | [diff] [blame] | 346 | writel_relaxed(0, &cpsw->wr_regs->tx_en); |
| 347 | writel_relaxed(0, &cpsw->wr_regs->rx_en); |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 348 | |
Ivan Khoronzhuk | 2c836bd | 2016-08-10 02:22:40 +0300 | [diff] [blame] | 349 | cpdma_ctlr_int_ctrl(cpsw->dma, false); |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 350 | return; |
| 351 | } |
| 352 | |
Ivan Khoronzhuk | 9ed4050 | 2019-07-09 00:34:32 +0300 | [diff] [blame] | 353 | static int cpsw_is_xdpf_handle(void *handle) |
| 354 | { |
| 355 | return (unsigned long)handle & BIT(0); |
| 356 | } |
| 357 | |
| 358 | static void *cpsw_xdpf_to_handle(struct xdp_frame *xdpf) |
| 359 | { |
| 360 | return (void *)((unsigned long)xdpf | BIT(0)); |
| 361 | } |
| 362 | |
| 363 | static struct xdp_frame *cpsw_handle_to_xdpf(void *handle) |
| 364 | { |
| 365 | return (struct xdp_frame *)((unsigned long)handle & ~BIT(0)); |
| 366 | } |
| 367 | |
| 368 | struct __aligned(sizeof(long)) cpsw_meta_xdp { |
| 369 | struct net_device *ndev; |
| 370 | int ch; |
| 371 | }; |
| 372 | |
Grygorii Strashko | c24eef2 | 2019-04-26 20:12:42 +0300 | [diff] [blame] | 373 | void cpsw_tx_handler(void *token, int len, int status) |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 374 | { |
Ivan Khoronzhuk | 9ed4050 | 2019-07-09 00:34:32 +0300 | [diff] [blame] | 375 | struct cpsw_meta_xdp *xmeta; |
| 376 | struct xdp_frame *xdpf; |
| 377 | struct net_device *ndev; |
Ivan Khoronzhuk | e05107e | 2016-08-22 21:18:26 +0300 | [diff] [blame] | 378 | struct netdev_queue *txq; |
Ivan Khoronzhuk | 9ed4050 | 2019-07-09 00:34:32 +0300 | [diff] [blame] | 379 | struct sk_buff *skb; |
| 380 | int ch; |
| 381 | |
| 382 | if (cpsw_is_xdpf_handle(token)) { |
| 383 | xdpf = cpsw_handle_to_xdpf(token); |
| 384 | xmeta = (void *)xdpf + CPSW_XMETA_OFFSET; |
| 385 | ndev = xmeta->ndev; |
| 386 | ch = xmeta->ch; |
| 387 | xdp_return_frame(xdpf); |
| 388 | } else { |
| 389 | skb = token; |
| 390 | ndev = skb->dev; |
| 391 | ch = skb_get_queue_mapping(skb); |
| 392 | cpts_tx_timestamp(ndev_to_cpsw(ndev)->cpts, skb); |
| 393 | dev_kfree_skb_any(skb); |
| 394 | } |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 395 | |
Mugunthan V N | fae5082 | 2013-01-17 06:31:34 +0000 | [diff] [blame] | 396 | /* Check whether the queue is stopped due to stalled tx dma, if the |
| 397 | * queue is stopped then start the queue as we have free desc for tx |
| 398 | */ |
Ivan Khoronzhuk | 9ed4050 | 2019-07-09 00:34:32 +0300 | [diff] [blame] | 399 | txq = netdev_get_tx_queue(ndev, ch); |
Ivan Khoronzhuk | e05107e | 2016-08-22 21:18:26 +0300 | [diff] [blame] | 400 | if (unlikely(netif_tx_queue_stopped(txq))) |
| 401 | netif_tx_wake_queue(txq); |
| 402 | |
Tobias Klauser | 8dc43dd | 2014-03-10 13:12:23 +0100 | [diff] [blame] | 403 | ndev->stats.tx_packets++; |
| 404 | ndev->stats.tx_bytes += len; |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Grygorii Strashko | a3a41d2 | 2018-03-15 15:15:50 -0500 | [diff] [blame] | 407 | static void cpsw_rx_vlan_encap(struct sk_buff *skb) |
| 408 | { |
| 409 | struct cpsw_priv *priv = netdev_priv(skb->dev); |
| 410 | struct cpsw_common *cpsw = priv->cpsw; |
| 411 | u32 rx_vlan_encap_hdr = *((u32 *)skb->data); |
| 412 | u16 vtag, vid, prio, pkt_type; |
| 413 | |
| 414 | /* Remove VLAN header encapsulation word */ |
| 415 | skb_pull(skb, CPSW_RX_VLAN_ENCAP_HDR_SIZE); |
| 416 | |
| 417 | pkt_type = (rx_vlan_encap_hdr >> |
| 418 | CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_SHIFT) & |
| 419 | CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_MSK; |
| 420 | /* Ignore unknown & Priority-tagged packets*/ |
| 421 | if (pkt_type == CPSW_RX_VLAN_ENCAP_HDR_PKT_RESERV || |
| 422 | pkt_type == CPSW_RX_VLAN_ENCAP_HDR_PKT_PRIO_TAG) |
| 423 | return; |
| 424 | |
| 425 | vid = (rx_vlan_encap_hdr >> |
| 426 | CPSW_RX_VLAN_ENCAP_HDR_VID_SHIFT) & |
| 427 | VLAN_VID_MASK; |
| 428 | /* Ignore vid 0 and pass packet as is */ |
| 429 | if (!vid) |
| 430 | return; |
| 431 | /* Ignore default vlans in dual mac mode */ |
| 432 | if (cpsw->data.dual_emac && |
| 433 | vid == cpsw->slaves[priv->emac_port].port_vlan) |
| 434 | return; |
| 435 | |
| 436 | prio = (rx_vlan_encap_hdr >> |
| 437 | CPSW_RX_VLAN_ENCAP_HDR_PRIO_SHIFT) & |
| 438 | CPSW_RX_VLAN_ENCAP_HDR_PRIO_MSK; |
| 439 | |
| 440 | vtag = (prio << VLAN_PRIO_SHIFT) | vid; |
| 441 | __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vtag); |
| 442 | |
| 443 | /* strip vlan tag for VLAN-tagged packet */ |
| 444 | if (pkt_type == CPSW_RX_VLAN_ENCAP_HDR_PKT_VLAN_TAG) { |
| 445 | memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN); |
| 446 | skb_pull(skb, VLAN_HLEN); |
| 447 | } |
| 448 | } |
| 449 | |
Ivan Khoronzhuk | 9ed4050 | 2019-07-09 00:34:32 +0300 | [diff] [blame] | 450 | static int cpsw_xdp_tx_frame(struct cpsw_priv *priv, struct xdp_frame *xdpf, |
| 451 | struct page *page) |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 452 | { |
Ivan Khoronzhuk | 9ed4050 | 2019-07-09 00:34:32 +0300 | [diff] [blame] | 453 | struct cpsw_common *cpsw = priv->cpsw; |
| 454 | struct cpsw_meta_xdp *xmeta; |
| 455 | struct cpdma_chan *txch; |
| 456 | dma_addr_t dma; |
| 457 | int ret, port; |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 458 | |
Ivan Khoronzhuk | 9ed4050 | 2019-07-09 00:34:32 +0300 | [diff] [blame] | 459 | xmeta = (void *)xdpf + CPSW_XMETA_OFFSET; |
| 460 | xmeta->ndev = priv->ndev; |
| 461 | xmeta->ch = 0; |
| 462 | txch = cpsw->txv[0].ch; |
| 463 | |
| 464 | port = priv->emac_port + cpsw->data.dual_emac; |
| 465 | if (page) { |
| 466 | dma = page_pool_get_dma_addr(page); |
| 467 | dma += xdpf->headroom + sizeof(struct xdp_frame); |
| 468 | ret = cpdma_chan_submit_mapped(txch, cpsw_xdpf_to_handle(xdpf), |
| 469 | dma, xdpf->len, port); |
| 470 | } else { |
| 471 | if (sizeof(*xmeta) > xdpf->headroom) { |
| 472 | xdp_return_frame_rx_napi(xdpf); |
| 473 | return -EINVAL; |
| 474 | } |
| 475 | |
| 476 | ret = cpdma_chan_submit(txch, cpsw_xdpf_to_handle(xdpf), |
| 477 | xdpf->data, xdpf->len, port); |
| 478 | } |
| 479 | |
| 480 | if (ret) { |
| 481 | priv->ndev->stats.tx_dropped++; |
| 482 | xdp_return_frame_rx_napi(xdpf); |
| 483 | } |
| 484 | |
| 485 | return ret; |
| 486 | } |
| 487 | |
| 488 | static int cpsw_run_xdp(struct cpsw_priv *priv, int ch, struct xdp_buff *xdp, |
| 489 | struct page *page) |
| 490 | { |
| 491 | struct cpsw_common *cpsw = priv->cpsw; |
| 492 | struct net_device *ndev = priv->ndev; |
| 493 | int ret = CPSW_XDP_CONSUMED; |
| 494 | struct xdp_frame *xdpf; |
| 495 | struct bpf_prog *prog; |
| 496 | u32 act; |
| 497 | |
| 498 | rcu_read_lock(); |
| 499 | |
| 500 | prog = READ_ONCE(priv->xdp_prog); |
| 501 | if (!prog) { |
| 502 | ret = CPSW_XDP_PASS; |
| 503 | goto out; |
| 504 | } |
| 505 | |
| 506 | act = bpf_prog_run_xdp(prog, xdp); |
| 507 | switch (act) { |
| 508 | case XDP_PASS: |
| 509 | ret = CPSW_XDP_PASS; |
| 510 | break; |
| 511 | case XDP_TX: |
| 512 | xdpf = convert_to_xdp_frame(xdp); |
| 513 | if (unlikely(!xdpf)) |
| 514 | goto drop; |
| 515 | |
| 516 | cpsw_xdp_tx_frame(priv, xdpf, page); |
| 517 | break; |
| 518 | case XDP_REDIRECT: |
| 519 | if (xdp_do_redirect(ndev, xdp, prog)) |
| 520 | goto drop; |
| 521 | |
| 522 | /* Have to flush here, per packet, instead of doing it in bulk |
| 523 | * at the end of the napi handler. The RX devices on this |
| 524 | * particular hardware is sharing a common queue, so the |
| 525 | * incoming device might change per packet. |
| 526 | */ |
| 527 | xdp_do_flush_map(); |
| 528 | break; |
| 529 | default: |
| 530 | bpf_warn_invalid_xdp_action(act); |
| 531 | /* fall through */ |
| 532 | case XDP_ABORTED: |
| 533 | trace_xdp_exception(ndev, prog, act); |
| 534 | /* fall through -- handle aborts by dropping packet */ |
| 535 | case XDP_DROP: |
| 536 | goto drop; |
| 537 | } |
| 538 | out: |
| 539 | rcu_read_unlock(); |
| 540 | return ret; |
| 541 | drop: |
| 542 | rcu_read_unlock(); |
| 543 | page_pool_recycle_direct(cpsw->page_pool[ch], page); |
| 544 | return ret; |
| 545 | } |
| 546 | |
| 547 | static unsigned int cpsw_rxbuf_total_len(unsigned int len) |
| 548 | { |
| 549 | len += CPSW_HEADROOM; |
| 550 | len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
| 551 | |
| 552 | return SKB_DATA_ALIGN(len); |
| 553 | } |
| 554 | |
| 555 | static struct page_pool *cpsw_create_page_pool(struct cpsw_common *cpsw, |
| 556 | int size) |
| 557 | { |
| 558 | struct page_pool_params pp_params; |
| 559 | struct page_pool *pool; |
| 560 | |
| 561 | pp_params.order = 0; |
| 562 | pp_params.flags = PP_FLAG_DMA_MAP; |
| 563 | pp_params.pool_size = size; |
| 564 | pp_params.nid = NUMA_NO_NODE; |
| 565 | pp_params.dma_dir = DMA_BIDIRECTIONAL; |
| 566 | pp_params.dev = cpsw->dev; |
| 567 | |
| 568 | pool = page_pool_create(&pp_params); |
| 569 | if (IS_ERR(pool)) |
| 570 | dev_err(cpsw->dev, "cannot create rx page pool\n"); |
| 571 | |
| 572 | return pool; |
| 573 | } |
| 574 | |
| 575 | static int cpsw_ndev_create_xdp_rxq(struct cpsw_priv *priv, int ch) |
| 576 | { |
| 577 | struct cpsw_common *cpsw = priv->cpsw; |
| 578 | struct xdp_rxq_info *rxq; |
| 579 | struct page_pool *pool; |
| 580 | int ret; |
| 581 | |
| 582 | pool = cpsw->page_pool[ch]; |
| 583 | rxq = &priv->xdp_rxq[ch]; |
| 584 | |
| 585 | ret = xdp_rxq_info_reg(rxq, priv->ndev, ch); |
| 586 | if (ret) |
| 587 | return ret; |
| 588 | |
| 589 | ret = xdp_rxq_info_reg_mem_model(rxq, MEM_TYPE_PAGE_POOL, pool); |
| 590 | if (ret) |
| 591 | xdp_rxq_info_unreg(rxq); |
| 592 | |
| 593 | return ret; |
| 594 | } |
| 595 | |
| 596 | static void cpsw_ndev_destroy_xdp_rxq(struct cpsw_priv *priv, int ch) |
| 597 | { |
| 598 | struct xdp_rxq_info *rxq = &priv->xdp_rxq[ch]; |
| 599 | |
| 600 | if (!xdp_rxq_info_is_reg(rxq)) |
| 601 | return; |
| 602 | |
| 603 | xdp_rxq_info_unreg(rxq); |
| 604 | } |
| 605 | |
| 606 | static int cpsw_create_rx_pool(struct cpsw_common *cpsw, int ch) |
| 607 | { |
| 608 | struct page_pool *pool; |
| 609 | int ret = 0, pool_size; |
| 610 | |
| 611 | pool_size = cpdma_chan_get_rx_buf_num(cpsw->rxv[ch].ch); |
| 612 | pool = cpsw_create_page_pool(cpsw, pool_size); |
| 613 | if (IS_ERR(pool)) |
| 614 | ret = PTR_ERR(pool); |
| 615 | else |
| 616 | cpsw->page_pool[ch] = pool; |
| 617 | |
| 618 | return ret; |
| 619 | } |
| 620 | |
| 621 | void cpsw_destroy_xdp_rxqs(struct cpsw_common *cpsw) |
| 622 | { |
| 623 | struct net_device *ndev; |
| 624 | int i, ch; |
| 625 | |
| 626 | for (ch = 0; ch < cpsw->rx_ch_num; ch++) { |
| 627 | for (i = 0; i < cpsw->data.slaves; i++) { |
| 628 | ndev = cpsw->slaves[i].ndev; |
| 629 | if (!ndev) |
| 630 | continue; |
| 631 | |
| 632 | cpsw_ndev_destroy_xdp_rxq(netdev_priv(ndev), ch); |
| 633 | } |
| 634 | |
| 635 | page_pool_destroy(cpsw->page_pool[ch]); |
| 636 | cpsw->page_pool[ch] = NULL; |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | int cpsw_create_xdp_rxqs(struct cpsw_common *cpsw) |
| 641 | { |
| 642 | struct net_device *ndev; |
| 643 | int i, ch, ret; |
| 644 | |
| 645 | for (ch = 0; ch < cpsw->rx_ch_num; ch++) { |
| 646 | ret = cpsw_create_rx_pool(cpsw, ch); |
| 647 | if (ret) |
| 648 | goto err_cleanup; |
| 649 | |
| 650 | /* using same page pool is allowed as no running rx handlers |
| 651 | * simultaneously for both ndevs |
| 652 | */ |
| 653 | for (i = 0; i < cpsw->data.slaves; i++) { |
| 654 | ndev = cpsw->slaves[i].ndev; |
| 655 | if (!ndev) |
| 656 | continue; |
| 657 | |
| 658 | ret = cpsw_ndev_create_xdp_rxq(netdev_priv(ndev), ch); |
| 659 | if (ret) |
| 660 | goto err_cleanup; |
Ivan Khoronzhuk | fea49f6 | 2018-07-31 01:05:39 +0300 | [diff] [blame] | 661 | } |
| 662 | } |
Mugunthan V N | d9ba8f9 | 2013-02-11 09:52:20 +0000 | [diff] [blame] | 663 | |
Ivan Khoronzhuk | 9ed4050 | 2019-07-09 00:34:32 +0300 | [diff] [blame] | 664 | return 0; |
| 665 | |
| 666 | err_cleanup: |
| 667 | cpsw_destroy_xdp_rxqs(cpsw); |
| 668 | |
| 669 | return ret; |
| 670 | } |
| 671 | |
| 672 | static void cpsw_rx_handler(void *token, int len, int status) |
| 673 | { |
| 674 | struct page *new_page, *page = token; |
| 675 | void *pa = page_address(page); |
| 676 | struct cpsw_meta_xdp *xmeta = pa + CPSW_XMETA_OFFSET; |
| 677 | struct cpsw_common *cpsw = ndev_to_cpsw(xmeta->ndev); |
| 678 | int pkt_size = cpsw->rx_packet_max; |
| 679 | int ret = 0, port, ch = xmeta->ch; |
| 680 | int headroom = CPSW_HEADROOM; |
| 681 | struct net_device *ndev = xmeta->ndev; |
| 682 | struct cpsw_priv *priv; |
| 683 | struct page_pool *pool; |
| 684 | struct sk_buff *skb; |
| 685 | struct xdp_buff xdp; |
| 686 | dma_addr_t dma; |
| 687 | |
| 688 | if (cpsw->data.dual_emac && status >= 0) { |
| 689 | port = CPDMA_RX_SOURCE_PORT(status); |
| 690 | if (port) |
| 691 | ndev = cpsw->slaves[--port].ndev; |
| 692 | } |
| 693 | |
| 694 | priv = netdev_priv(ndev); |
| 695 | pool = cpsw->page_pool[ch]; |
Mugunthan V N | 16e5c57 | 2014-04-10 14:23:23 +0530 | [diff] [blame] | 696 | if (unlikely(status < 0) || unlikely(!netif_running(ndev))) { |
Ivan Khoronzhuk | fe734d0 | 2017-01-19 18:58:26 +0200 | [diff] [blame] | 697 | /* In dual emac mode check for all interfaces */ |
Ivan Khoronzhuk | d5bc161 | 2017-02-14 16:02:36 +0200 | [diff] [blame] | 698 | if (cpsw->data.dual_emac && cpsw->usage_count && |
Ivan Khoronzhuk | fe734d0 | 2017-01-19 18:58:26 +0200 | [diff] [blame] | 699 | (status >= 0)) { |
Mugunthan V N | a0e2c82 | 2014-09-10 16:38:09 +0530 | [diff] [blame] | 700 | /* The packet received is for the interface which |
| 701 | * is already down and the other interface is up |
Joe Perches | dbedd44 | 2015-03-06 20:49:12 -0800 | [diff] [blame] | 702 | * and running, instead of freeing which results |
Mugunthan V N | a0e2c82 | 2014-09-10 16:38:09 +0530 | [diff] [blame] | 703 | * in reducing of the number of rx descriptor in |
Ivan Khoronzhuk | 9ed4050 | 2019-07-09 00:34:32 +0300 | [diff] [blame] | 704 | * DMA engine, requeue page back to cpdma. |
Mugunthan V N | a0e2c82 | 2014-09-10 16:38:09 +0530 | [diff] [blame] | 705 | */ |
Ivan Khoronzhuk | 9ed4050 | 2019-07-09 00:34:32 +0300 | [diff] [blame] | 706 | new_page = page; |
Mugunthan V N | a0e2c82 | 2014-09-10 16:38:09 +0530 | [diff] [blame] | 707 | goto requeue; |
| 708 | } |
| 709 | |
Ivan Khoronzhuk | 9ed4050 | 2019-07-09 00:34:32 +0300 | [diff] [blame] | 710 | /* the interface is going down, pages are purged */ |
| 711 | page_pool_recycle_direct(pool, page); |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 712 | return; |
| 713 | } |
Sebastian Siewior | b4727e6 | 2013-04-23 07:31:39 +0000 | [diff] [blame] | 714 | |
Ivan Khoronzhuk | 9ed4050 | 2019-07-09 00:34:32 +0300 | [diff] [blame] | 715 | new_page = page_pool_dev_alloc_pages(pool); |
| 716 | if (unlikely(!new_page)) { |
| 717 | new_page = page; |
Tobias Klauser | 8dc43dd | 2014-03-10 13:12:23 +0100 | [diff] [blame] | 718 | ndev->stats.rx_dropped++; |
Ivan Khoronzhuk | 9ed4050 | 2019-07-09 00:34:32 +0300 | [diff] [blame] | 719 | goto requeue; |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 720 | } |
| 721 | |
Ivan Khoronzhuk | 9ed4050 | 2019-07-09 00:34:32 +0300 | [diff] [blame] | 722 | if (priv->xdp_prog) { |
| 723 | if (status & CPDMA_RX_VLAN_ENCAP) { |
| 724 | xdp.data = pa + CPSW_HEADROOM + |
| 725 | CPSW_RX_VLAN_ENCAP_HDR_SIZE; |
| 726 | xdp.data_end = xdp.data + len - |
| 727 | CPSW_RX_VLAN_ENCAP_HDR_SIZE; |
| 728 | } else { |
| 729 | xdp.data = pa + CPSW_HEADROOM; |
| 730 | xdp.data_end = xdp.data + len; |
| 731 | } |
| 732 | |
| 733 | xdp_set_data_meta_invalid(&xdp); |
| 734 | |
| 735 | xdp.data_hard_start = pa; |
| 736 | xdp.rxq = &priv->xdp_rxq[ch]; |
| 737 | |
| 738 | ret = cpsw_run_xdp(priv, ch, &xdp, page); |
| 739 | if (ret != CPSW_XDP_PASS) |
| 740 | goto requeue; |
| 741 | |
| 742 | /* XDP prog might have changed packet data and boundaries */ |
| 743 | len = xdp.data_end - xdp.data; |
| 744 | headroom = xdp.data - xdp.data_hard_start; |
| 745 | |
| 746 | /* XDP prog can modify vlan tag, so can't use encap header */ |
| 747 | status &= ~CPDMA_RX_VLAN_ENCAP; |
| 748 | } |
| 749 | |
| 750 | /* pass skb to netstack if no XDP prog or returned XDP_PASS */ |
| 751 | skb = build_skb(pa, cpsw_rxbuf_total_len(pkt_size)); |
| 752 | if (!skb) { |
| 753 | ndev->stats.rx_dropped++; |
| 754 | page_pool_recycle_direct(pool, page); |
| 755 | goto requeue; |
| 756 | } |
| 757 | |
| 758 | skb_reserve(skb, headroom); |
| 759 | skb_put(skb, len); |
| 760 | skb->dev = ndev; |
| 761 | if (status & CPDMA_RX_VLAN_ENCAP) |
| 762 | cpsw_rx_vlan_encap(skb); |
| 763 | if (priv->rx_ts_enabled) |
| 764 | cpts_rx_timestamp(cpsw->cpts, skb); |
| 765 | skb->protocol = eth_type_trans(skb, ndev); |
| 766 | |
| 767 | /* unmap page as no netstack skb page recycling */ |
| 768 | page_pool_release_page(pool, page); |
| 769 | netif_receive_skb(skb); |
| 770 | |
| 771 | ndev->stats.rx_bytes += len; |
| 772 | ndev->stats.rx_packets++; |
| 773 | |
Mugunthan V N | a0e2c82 | 2014-09-10 16:38:09 +0530 | [diff] [blame] | 774 | requeue: |
Ivan Khoronzhuk | 9ed4050 | 2019-07-09 00:34:32 +0300 | [diff] [blame] | 775 | xmeta = page_address(new_page) + CPSW_XMETA_OFFSET; |
| 776 | xmeta->ndev = ndev; |
| 777 | xmeta->ch = ch; |
| 778 | |
| 779 | dma = page_pool_get_dma_addr(new_page) + CPSW_HEADROOM; |
| 780 | ret = cpdma_chan_submit_mapped(cpsw->rxv[ch].ch, new_page, dma, |
| 781 | pkt_size, 0); |
Ivan Khoronzhuk | 871e846 | 2019-06-15 14:01:32 +0300 | [diff] [blame] | 782 | if (ret < 0) { |
| 783 | WARN_ON(ret == -ENOMEM); |
Ivan Khoronzhuk | 9ed4050 | 2019-07-09 00:34:32 +0300 | [diff] [blame] | 784 | page_pool_recycle_direct(pool, new_page); |
Ivan Khoronzhuk | 871e846 | 2019-06-15 14:01:32 +0300 | [diff] [blame] | 785 | } |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 786 | } |
| 787 | |
Grygorii Strashko | c24eef2 | 2019-04-26 20:12:42 +0300 | [diff] [blame] | 788 | void cpsw_split_res(struct cpsw_common *cpsw) |
Ivan Khoronzhuk | 48e0a83 | 2016-12-06 03:45:00 +0200 | [diff] [blame] | 789 | { |
Ivan Khoronzhuk | 32b78d8 | 2016-12-10 14:23:48 +0200 | [diff] [blame] | 790 | u32 consumed_rate = 0, bigest_rate = 0; |
Ivan Khoronzhuk | 48e0a83 | 2016-12-06 03:45:00 +0200 | [diff] [blame] | 791 | struct cpsw_vector *txv = cpsw->txv; |
Ivan Khoronzhuk | 32b78d8 | 2016-12-10 14:23:48 +0200 | [diff] [blame] | 792 | int i, ch_weight, rlim_ch_num = 0; |
Ivan Khoronzhuk | 48e0a83 | 2016-12-06 03:45:00 +0200 | [diff] [blame] | 793 | int budget, bigest_rate_ch = 0; |
Ivan Khoronzhuk | 48e0a83 | 2016-12-06 03:45:00 +0200 | [diff] [blame] | 794 | u32 ch_rate, max_rate; |
| 795 | int ch_budget = 0; |
| 796 | |
Ivan Khoronzhuk | 48e0a83 | 2016-12-06 03:45:00 +0200 | [diff] [blame] | 797 | for (i = 0; i < cpsw->tx_ch_num; i++) { |
| 798 | ch_rate = cpdma_chan_get_rate(txv[i].ch); |
| 799 | if (!ch_rate) |
| 800 | continue; |
| 801 | |
| 802 | rlim_ch_num++; |
| 803 | consumed_rate += ch_rate; |
| 804 | } |
| 805 | |
| 806 | if (cpsw->tx_ch_num == rlim_ch_num) { |
| 807 | max_rate = consumed_rate; |
Ivan Khoronzhuk | 32b78d8 | 2016-12-10 14:23:48 +0200 | [diff] [blame] | 808 | } else if (!rlim_ch_num) { |
| 809 | ch_budget = CPSW_POLL_WEIGHT / cpsw->tx_ch_num; |
| 810 | bigest_rate = 0; |
| 811 | max_rate = consumed_rate; |
Ivan Khoronzhuk | 48e0a83 | 2016-12-06 03:45:00 +0200 | [diff] [blame] | 812 | } else { |
Ivan Khoronzhuk | 0be01b8 | 2016-12-10 14:23:49 +0200 | [diff] [blame] | 813 | max_rate = cpsw->speed * 1000; |
| 814 | |
| 815 | /* if max_rate is less then expected due to reduced link speed, |
| 816 | * split proportionally according next potential max speed |
| 817 | */ |
| 818 | if (max_rate < consumed_rate) |
| 819 | max_rate *= 10; |
| 820 | |
| 821 | if (max_rate < consumed_rate) |
| 822 | max_rate *= 10; |
Ivan Khoronzhuk | 32b78d8 | 2016-12-10 14:23:48 +0200 | [diff] [blame] | 823 | |
Ivan Khoronzhuk | 48e0a83 | 2016-12-06 03:45:00 +0200 | [diff] [blame] | 824 | ch_budget = (consumed_rate * CPSW_POLL_WEIGHT) / max_rate; |
| 825 | ch_budget = (CPSW_POLL_WEIGHT - ch_budget) / |
| 826 | (cpsw->tx_ch_num - rlim_ch_num); |
| 827 | bigest_rate = (max_rate - consumed_rate) / |
| 828 | (cpsw->tx_ch_num - rlim_ch_num); |
| 829 | } |
| 830 | |
Ivan Khoronzhuk | 32b78d8 | 2016-12-10 14:23:48 +0200 | [diff] [blame] | 831 | /* split tx weight/budget */ |
Ivan Khoronzhuk | 48e0a83 | 2016-12-06 03:45:00 +0200 | [diff] [blame] | 832 | budget = CPSW_POLL_WEIGHT; |
| 833 | for (i = 0; i < cpsw->tx_ch_num; i++) { |
| 834 | ch_rate = cpdma_chan_get_rate(txv[i].ch); |
| 835 | if (ch_rate) { |
| 836 | txv[i].budget = (ch_rate * CPSW_POLL_WEIGHT) / max_rate; |
| 837 | if (!txv[i].budget) |
Ivan Khoronzhuk | 32b78d8 | 2016-12-10 14:23:48 +0200 | [diff] [blame] | 838 | txv[i].budget++; |
Ivan Khoronzhuk | 48e0a83 | 2016-12-06 03:45:00 +0200 | [diff] [blame] | 839 | if (ch_rate > bigest_rate) { |
| 840 | bigest_rate_ch = i; |
| 841 | bigest_rate = ch_rate; |
| 842 | } |
Ivan Khoronzhuk | 32b78d8 | 2016-12-10 14:23:48 +0200 | [diff] [blame] | 843 | |
| 844 | ch_weight = (ch_rate * 100) / max_rate; |
| 845 | if (!ch_weight) |
| 846 | ch_weight++; |
| 847 | cpdma_chan_set_weight(cpsw->txv[i].ch, ch_weight); |
Ivan Khoronzhuk | 48e0a83 | 2016-12-06 03:45:00 +0200 | [diff] [blame] | 848 | } else { |
| 849 | txv[i].budget = ch_budget; |
| 850 | if (!bigest_rate_ch) |
| 851 | bigest_rate_ch = i; |
Ivan Khoronzhuk | 32b78d8 | 2016-12-10 14:23:48 +0200 | [diff] [blame] | 852 | cpdma_chan_set_weight(cpsw->txv[i].ch, 0); |
Ivan Khoronzhuk | 48e0a83 | 2016-12-06 03:45:00 +0200 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | budget -= txv[i].budget; |
| 856 | } |
| 857 | |
| 858 | if (budget) |
| 859 | txv[bigest_rate_ch].budget += budget; |
| 860 | |
| 861 | /* split rx budget */ |
| 862 | budget = CPSW_POLL_WEIGHT; |
| 863 | ch_budget = budget / cpsw->rx_ch_num; |
| 864 | for (i = 0; i < cpsw->rx_ch_num; i++) { |
| 865 | cpsw->rxv[i].budget = ch_budget; |
| 866 | budget -= ch_budget; |
| 867 | } |
| 868 | |
| 869 | if (budget) |
| 870 | cpsw->rxv[0].budget += budget; |
| 871 | } |
| 872 | |
Felipe Balbi | c03abd8 | 2015-01-16 10:11:12 -0600 | [diff] [blame] | 873 | static irqreturn_t cpsw_tx_interrupt(int irq, void *dev_id) |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 874 | { |
Ivan Khoronzhuk | dbc4ec5 | 2016-08-10 02:22:43 +0300 | [diff] [blame] | 875 | struct cpsw_common *cpsw = dev_id; |
Felipe Balbi | 7ce67a3 | 2015-01-02 16:15:59 -0600 | [diff] [blame] | 876 | |
Ivan Khoronzhuk | 5d8d0d4 | 2016-08-10 02:22:39 +0300 | [diff] [blame] | 877 | writel(0, &cpsw->wr_regs->tx_en); |
Ivan Khoronzhuk | 2c836bd | 2016-08-10 02:22:40 +0300 | [diff] [blame] | 878 | cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_TX); |
Felipe Balbi | c03abd8 | 2015-01-16 10:11:12 -0600 | [diff] [blame] | 879 | |
Ivan Khoronzhuk | e38b5a3 | 2016-08-10 02:22:41 +0300 | [diff] [blame] | 880 | if (cpsw->quirk_irq) { |
| 881 | disable_irq_nosync(cpsw->irqs_table[1]); |
| 882 | cpsw->tx_irq_disabled = true; |
Mugunthan V N | 7da1160 | 2015-08-12 15:22:53 +0530 | [diff] [blame] | 883 | } |
| 884 | |
Ivan Khoronzhuk | dbc4ec5 | 2016-08-10 02:22:43 +0300 | [diff] [blame] | 885 | napi_schedule(&cpsw->napi_tx); |
Felipe Balbi | c03abd8 | 2015-01-16 10:11:12 -0600 | [diff] [blame] | 886 | return IRQ_HANDLED; |
| 887 | } |
| 888 | |
| 889 | static irqreturn_t cpsw_rx_interrupt(int irq, void *dev_id) |
| 890 | { |
Ivan Khoronzhuk | dbc4ec5 | 2016-08-10 02:22:43 +0300 | [diff] [blame] | 891 | struct cpsw_common *cpsw = dev_id; |
Felipe Balbi | c03abd8 | 2015-01-16 10:11:12 -0600 | [diff] [blame] | 892 | |
Ivan Khoronzhuk | 2c836bd | 2016-08-10 02:22:40 +0300 | [diff] [blame] | 893 | cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_RX); |
Ivan Khoronzhuk | 5d8d0d4 | 2016-08-10 02:22:39 +0300 | [diff] [blame] | 894 | writel(0, &cpsw->wr_regs->rx_en); |
Sebastian Siewior | fd51cf1 | 2013-04-23 07:31:37 +0000 | [diff] [blame] | 895 | |
Ivan Khoronzhuk | e38b5a3 | 2016-08-10 02:22:41 +0300 | [diff] [blame] | 896 | if (cpsw->quirk_irq) { |
| 897 | disable_irq_nosync(cpsw->irqs_table[0]); |
| 898 | cpsw->rx_irq_disabled = true; |
Mugunthan V N | 7da1160 | 2015-08-12 15:22:53 +0530 | [diff] [blame] | 899 | } |
| 900 | |
Ivan Khoronzhuk | dbc4ec5 | 2016-08-10 02:22:43 +0300 | [diff] [blame] | 901 | napi_schedule(&cpsw->napi_rx); |
Mugunthan V N | d354eb8 | 2015-08-04 16:06:19 +0530 | [diff] [blame] | 902 | return IRQ_HANDLED; |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 903 | } |
| 904 | |
Ivan Khoronzhuk | 9611d6d | 2018-05-17 01:21:45 +0300 | [diff] [blame] | 905 | static int cpsw_tx_mq_poll(struct napi_struct *napi_tx, int budget) |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 906 | { |
Ivan Khoronzhuk | e05107e | 2016-08-22 21:18:26 +0300 | [diff] [blame] | 907 | u32 ch_map; |
Ivan Khoronzhuk | 8feb0a1 | 2016-11-29 17:00:51 +0200 | [diff] [blame] | 908 | int num_tx, cur_budget, ch; |
Ivan Khoronzhuk | dbc4ec5 | 2016-08-10 02:22:43 +0300 | [diff] [blame] | 909 | struct cpsw_common *cpsw = napi_to_cpsw(napi_tx); |
Ivan Khoronzhuk | 8feb0a1 | 2016-11-29 17:00:51 +0200 | [diff] [blame] | 910 | struct cpsw_vector *txv; |
Mugunthan V N | 32a7432 | 2015-08-04 16:06:20 +0530 | [diff] [blame] | 911 | |
Ivan Khoronzhuk | e05107e | 2016-08-22 21:18:26 +0300 | [diff] [blame] | 912 | /* process every unprocessed channel */ |
| 913 | ch_map = cpdma_ctrl_txchs_state(cpsw->dma); |
Ivan Khoronzhuk | 79b3325 | 2018-07-24 00:26:29 +0300 | [diff] [blame] | 914 | for (ch = 0, num_tx = 0; ch_map & 0xff; ch_map <<= 1, ch++) { |
| 915 | if (!(ch_map & 0x80)) |
Ivan Khoronzhuk | e05107e | 2016-08-22 21:18:26 +0300 | [diff] [blame] | 916 | continue; |
| 917 | |
Ivan Khoronzhuk | 8feb0a1 | 2016-11-29 17:00:51 +0200 | [diff] [blame] | 918 | txv = &cpsw->txv[ch]; |
| 919 | if (unlikely(txv->budget > budget - num_tx)) |
| 920 | cur_budget = budget - num_tx; |
| 921 | else |
| 922 | cur_budget = txv->budget; |
| 923 | |
| 924 | num_tx += cpdma_chan_process(txv->ch, cur_budget); |
Ivan Khoronzhuk | 342934a | 2016-11-29 17:00:50 +0200 | [diff] [blame] | 925 | if (num_tx >= budget) |
| 926 | break; |
Ivan Khoronzhuk | e05107e | 2016-08-22 21:18:26 +0300 | [diff] [blame] | 927 | } |
| 928 | |
Mugunthan V N | 32a7432 | 2015-08-04 16:06:20 +0530 | [diff] [blame] | 929 | if (num_tx < budget) { |
| 930 | napi_complete(napi_tx); |
Ivan Khoronzhuk | 5d8d0d4 | 2016-08-10 02:22:39 +0300 | [diff] [blame] | 931 | writel(0xff, &cpsw->wr_regs->tx_en); |
Ivan Khoronzhuk | 9611d6d | 2018-05-17 01:21:45 +0300 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | return num_tx; |
| 935 | } |
| 936 | |
| 937 | static int cpsw_tx_poll(struct napi_struct *napi_tx, int budget) |
| 938 | { |
| 939 | struct cpsw_common *cpsw = napi_to_cpsw(napi_tx); |
| 940 | int num_tx; |
| 941 | |
| 942 | num_tx = cpdma_chan_process(cpsw->txv[0].ch, budget); |
| 943 | if (num_tx < budget) { |
| 944 | napi_complete(napi_tx); |
| 945 | writel(0xff, &cpsw->wr_regs->tx_en); |
| 946 | if (cpsw->tx_irq_disabled) { |
Ivan Khoronzhuk | e38b5a3 | 2016-08-10 02:22:41 +0300 | [diff] [blame] | 947 | cpsw->tx_irq_disabled = false; |
| 948 | enable_irq(cpsw->irqs_table[1]); |
Mugunthan V N | 7da1160 | 2015-08-12 15:22:53 +0530 | [diff] [blame] | 949 | } |
Mugunthan V N | 32a7432 | 2015-08-04 16:06:20 +0530 | [diff] [blame] | 950 | } |
| 951 | |
Mugunthan V N | 32a7432 | 2015-08-04 16:06:20 +0530 | [diff] [blame] | 952 | return num_tx; |
| 953 | } |
| 954 | |
Ivan Khoronzhuk | 9611d6d | 2018-05-17 01:21:45 +0300 | [diff] [blame] | 955 | static int cpsw_rx_mq_poll(struct napi_struct *napi_rx, int budget) |
Mugunthan V N | 32a7432 | 2015-08-04 16:06:20 +0530 | [diff] [blame] | 956 | { |
Ivan Khoronzhuk | e05107e | 2016-08-22 21:18:26 +0300 | [diff] [blame] | 957 | u32 ch_map; |
Ivan Khoronzhuk | 8feb0a1 | 2016-11-29 17:00:51 +0200 | [diff] [blame] | 958 | int num_rx, cur_budget, ch; |
Ivan Khoronzhuk | dbc4ec5 | 2016-08-10 02:22:43 +0300 | [diff] [blame] | 959 | struct cpsw_common *cpsw = napi_to_cpsw(napi_rx); |
Ivan Khoronzhuk | 8feb0a1 | 2016-11-29 17:00:51 +0200 | [diff] [blame] | 960 | struct cpsw_vector *rxv; |
Mugunthan V N | 510a1e72 | 2013-02-17 22:19:20 +0000 | [diff] [blame] | 961 | |
Ivan Khoronzhuk | e05107e | 2016-08-22 21:18:26 +0300 | [diff] [blame] | 962 | /* process every unprocessed channel */ |
| 963 | ch_map = cpdma_ctrl_rxchs_state(cpsw->dma); |
Ivan Khoronzhuk | 342934a | 2016-11-29 17:00:50 +0200 | [diff] [blame] | 964 | for (ch = 0, num_rx = 0; ch_map; ch_map >>= 1, ch++) { |
Ivan Khoronzhuk | e05107e | 2016-08-22 21:18:26 +0300 | [diff] [blame] | 965 | if (!(ch_map & 0x01)) |
| 966 | continue; |
| 967 | |
Ivan Khoronzhuk | 8feb0a1 | 2016-11-29 17:00:51 +0200 | [diff] [blame] | 968 | rxv = &cpsw->rxv[ch]; |
| 969 | if (unlikely(rxv->budget > budget - num_rx)) |
| 970 | cur_budget = budget - num_rx; |
| 971 | else |
| 972 | cur_budget = rxv->budget; |
| 973 | |
| 974 | num_rx += cpdma_chan_process(rxv->ch, cur_budget); |
Ivan Khoronzhuk | 342934a | 2016-11-29 17:00:50 +0200 | [diff] [blame] | 975 | if (num_rx >= budget) |
| 976 | break; |
Ivan Khoronzhuk | e05107e | 2016-08-22 21:18:26 +0300 | [diff] [blame] | 977 | } |
| 978 | |
Mugunthan V N | 510a1e72 | 2013-02-17 22:19:20 +0000 | [diff] [blame] | 979 | if (num_rx < budget) { |
Eric Dumazet | 6ad2016 | 2017-01-30 08:22:01 -0800 | [diff] [blame] | 980 | napi_complete_done(napi_rx, num_rx); |
Ivan Khoronzhuk | 5d8d0d4 | 2016-08-10 02:22:39 +0300 | [diff] [blame] | 981 | writel(0xff, &cpsw->wr_regs->rx_en); |
Ivan Khoronzhuk | 9611d6d | 2018-05-17 01:21:45 +0300 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | return num_rx; |
| 985 | } |
| 986 | |
| 987 | static int cpsw_rx_poll(struct napi_struct *napi_rx, int budget) |
| 988 | { |
| 989 | struct cpsw_common *cpsw = napi_to_cpsw(napi_rx); |
| 990 | int num_rx; |
| 991 | |
| 992 | num_rx = cpdma_chan_process(cpsw->rxv[0].ch, budget); |
| 993 | if (num_rx < budget) { |
| 994 | napi_complete_done(napi_rx, num_rx); |
| 995 | writel(0xff, &cpsw->wr_regs->rx_en); |
| 996 | if (cpsw->rx_irq_disabled) { |
Ivan Khoronzhuk | e38b5a3 | 2016-08-10 02:22:41 +0300 | [diff] [blame] | 997 | cpsw->rx_irq_disabled = false; |
| 998 | enable_irq(cpsw->irqs_table[0]); |
Mugunthan V N | 7da1160 | 2015-08-12 15:22:53 +0530 | [diff] [blame] | 999 | } |
Mugunthan V N | 510a1e72 | 2013-02-17 22:19:20 +0000 | [diff] [blame] | 1000 | } |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1001 | |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1002 | return num_rx; |
| 1003 | } |
| 1004 | |
| 1005 | static inline void soft_reset(const char *module, void __iomem *reg) |
| 1006 | { |
| 1007 | unsigned long timeout = jiffies + HZ; |
| 1008 | |
Grygorii Strashko | dda5f5fe | 2017-11-30 18:21:11 -0600 | [diff] [blame] | 1009 | writel_relaxed(1, reg); |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1010 | do { |
| 1011 | cpu_relax(); |
Grygorii Strashko | dda5f5fe | 2017-11-30 18:21:11 -0600 | [diff] [blame] | 1012 | } while ((readl_relaxed(reg) & 1) && time_after(timeout, jiffies)); |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1013 | |
Grygorii Strashko | dda5f5fe | 2017-11-30 18:21:11 -0600 | [diff] [blame] | 1014 | WARN(readl_relaxed(reg) & 1, "failed to soft-reset %s\n", module); |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1017 | static void cpsw_set_slave_mac(struct cpsw_slave *slave, |
| 1018 | struct cpsw_priv *priv) |
| 1019 | { |
Richard Cochran | 9750a3a | 2012-10-29 08:45:15 +0000 | [diff] [blame] | 1020 | slave_write(slave, mac_hi(priv->mac_addr), SA_HI); |
| 1021 | slave_write(slave, mac_lo(priv->mac_addr), SA_LO); |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
Ivan Khoronzhuk | 57d9014 | 2018-07-24 00:26:32 +0300 | [diff] [blame] | 1024 | static bool cpsw_shp_is_off(struct cpsw_priv *priv) |
| 1025 | { |
| 1026 | struct cpsw_common *cpsw = priv->cpsw; |
| 1027 | struct cpsw_slave *slave; |
| 1028 | u32 shift, mask, val; |
| 1029 | |
| 1030 | val = readl_relaxed(&cpsw->regs->ptype); |
| 1031 | |
| 1032 | slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)]; |
| 1033 | shift = CPSW_FIFO_SHAPE_EN_SHIFT + 3 * slave->slave_num; |
| 1034 | mask = 7 << shift; |
| 1035 | val = val & mask; |
| 1036 | |
| 1037 | return !val; |
| 1038 | } |
| 1039 | |
| 1040 | static void cpsw_fifo_shp_on(struct cpsw_priv *priv, int fifo, int on) |
| 1041 | { |
| 1042 | struct cpsw_common *cpsw = priv->cpsw; |
| 1043 | struct cpsw_slave *slave; |
| 1044 | u32 shift, mask, val; |
| 1045 | |
| 1046 | val = readl_relaxed(&cpsw->regs->ptype); |
| 1047 | |
| 1048 | slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)]; |
| 1049 | shift = CPSW_FIFO_SHAPE_EN_SHIFT + 3 * slave->slave_num; |
| 1050 | mask = (1 << --fifo) << shift; |
| 1051 | val = on ? val | mask : val & ~mask; |
| 1052 | |
| 1053 | writel_relaxed(val, &cpsw->regs->ptype); |
| 1054 | } |
| 1055 | |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1056 | static void _cpsw_adjust_link(struct cpsw_slave *slave, |
| 1057 | struct cpsw_priv *priv, bool *link) |
| 1058 | { |
| 1059 | struct phy_device *phy = slave->phy; |
| 1060 | u32 mac_control = 0; |
| 1061 | u32 slave_port; |
Ivan Khoronzhuk | 606f399 | 2016-08-10 02:22:42 +0300 | [diff] [blame] | 1062 | struct cpsw_common *cpsw = priv->cpsw; |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1063 | |
| 1064 | if (!phy) |
| 1065 | return; |
| 1066 | |
Ivan Khoronzhuk | 6f1f583 | 2016-08-10 02:22:34 +0300 | [diff] [blame] | 1067 | slave_port = cpsw_get_slave_port(slave->slave_num); |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1068 | |
| 1069 | if (phy->link) { |
Grygorii Strashko | cfc0834 | 2019-04-26 20:12:41 +0300 | [diff] [blame] | 1070 | mac_control = CPSW_SL_CTL_GMII_EN; |
| 1071 | |
| 1072 | if (phy->speed == 1000) |
| 1073 | mac_control |= CPSW_SL_CTL_GIG; |
| 1074 | if (phy->duplex) |
| 1075 | mac_control |= CPSW_SL_CTL_FULLDUPLEX; |
| 1076 | |
| 1077 | /* set speed_in input in case RMII mode is used in 100Mbps */ |
| 1078 | if (phy->speed == 100) |
| 1079 | mac_control |= CPSW_SL_CTL_IFCTL_A; |
| 1080 | /* in band mode only works in 10Mbps RGMII mode */ |
| 1081 | else if ((phy->speed == 10) && phy_interface_is_rgmii(phy)) |
| 1082 | mac_control |= CPSW_SL_CTL_EXT_EN; /* In Band mode */ |
| 1083 | |
| 1084 | if (priv->rx_pause) |
| 1085 | mac_control |= CPSW_SL_CTL_RX_FLOW_EN; |
| 1086 | |
| 1087 | if (priv->tx_pause) |
| 1088 | mac_control |= CPSW_SL_CTL_TX_FLOW_EN; |
| 1089 | |
| 1090 | if (mac_control != slave->mac_control) |
| 1091 | cpsw_sl_ctl_set(slave->mac_sl, mac_control); |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1092 | |
| 1093 | /* enable forwarding */ |
Ivan Khoronzhuk | 2a05a62 | 2016-08-10 02:22:44 +0300 | [diff] [blame] | 1094 | cpsw_ale_control_set(cpsw->ale, slave_port, |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1095 | ALE_PORT_STATE, ALE_PORT_STATE_FORWARD); |
| 1096 | |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1097 | *link = true; |
Ivan Khoronzhuk | 57d9014 | 2018-07-24 00:26:32 +0300 | [diff] [blame] | 1098 | |
| 1099 | if (priv->shp_cfg_speed && |
| 1100 | priv->shp_cfg_speed != slave->phy->speed && |
| 1101 | !cpsw_shp_is_off(priv)) |
| 1102 | dev_warn(priv->dev, |
| 1103 | "Speed was changed, CBS shaper speeds are changed!"); |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1104 | } else { |
| 1105 | mac_control = 0; |
| 1106 | /* disable forwarding */ |
Ivan Khoronzhuk | 2a05a62 | 2016-08-10 02:22:44 +0300 | [diff] [blame] | 1107 | cpsw_ale_control_set(cpsw->ale, slave_port, |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1108 | ALE_PORT_STATE, ALE_PORT_STATE_DISABLE); |
Grygorii Strashko | cfc0834 | 2019-04-26 20:12:41 +0300 | [diff] [blame] | 1109 | |
| 1110 | cpsw_sl_wait_for_idle(slave->mac_sl, 100); |
| 1111 | |
| 1112 | cpsw_sl_ctl_reset(slave->mac_sl); |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
Grygorii Strashko | cfc0834 | 2019-04-26 20:12:41 +0300 | [diff] [blame] | 1115 | if (mac_control != slave->mac_control) |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1116 | phy_print_status(phy); |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1117 | |
| 1118 | slave->mac_control = mac_control; |
| 1119 | } |
| 1120 | |
Ivan Khoronzhuk | 0be01b8 | 2016-12-10 14:23:49 +0200 | [diff] [blame] | 1121 | static int cpsw_get_common_speed(struct cpsw_common *cpsw) |
| 1122 | { |
| 1123 | int i, speed; |
| 1124 | |
| 1125 | for (i = 0, speed = 0; i < cpsw->data.slaves; i++) |
| 1126 | if (cpsw->slaves[i].phy && cpsw->slaves[i].phy->link) |
| 1127 | speed += cpsw->slaves[i].phy->speed; |
| 1128 | |
| 1129 | return speed; |
| 1130 | } |
| 1131 | |
| 1132 | static int cpsw_need_resplit(struct cpsw_common *cpsw) |
| 1133 | { |
| 1134 | int i, rlim_ch_num; |
| 1135 | int speed, ch_rate; |
| 1136 | |
| 1137 | /* re-split resources only in case speed was changed */ |
| 1138 | speed = cpsw_get_common_speed(cpsw); |
| 1139 | if (speed == cpsw->speed || !speed) |
| 1140 | return 0; |
| 1141 | |
| 1142 | cpsw->speed = speed; |
| 1143 | |
| 1144 | for (i = 0, rlim_ch_num = 0; i < cpsw->tx_ch_num; i++) { |
| 1145 | ch_rate = cpdma_chan_get_rate(cpsw->txv[i].ch); |
| 1146 | if (!ch_rate) |
| 1147 | break; |
| 1148 | |
| 1149 | rlim_ch_num++; |
| 1150 | } |
| 1151 | |
| 1152 | /* cases not dependent on speed */ |
| 1153 | if (!rlim_ch_num || rlim_ch_num == cpsw->tx_ch_num) |
| 1154 | return 0; |
| 1155 | |
| 1156 | return 1; |
| 1157 | } |
| 1158 | |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1159 | static void cpsw_adjust_link(struct net_device *ndev) |
| 1160 | { |
| 1161 | struct cpsw_priv *priv = netdev_priv(ndev); |
Ivan Khoronzhuk | 0be01b8 | 2016-12-10 14:23:49 +0200 | [diff] [blame] | 1162 | struct cpsw_common *cpsw = priv->cpsw; |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1163 | bool link = false; |
| 1164 | |
| 1165 | for_each_slave(priv, _cpsw_adjust_link, priv, &link); |
| 1166 | |
| 1167 | if (link) { |
Ivan Khoronzhuk | 0be01b8 | 2016-12-10 14:23:49 +0200 | [diff] [blame] | 1168 | if (cpsw_need_resplit(cpsw)) |
Grygorii Strashko | 9763a89 | 2019-04-26 20:12:26 +0300 | [diff] [blame] | 1169 | cpsw_split_res(cpsw); |
Ivan Khoronzhuk | 0be01b8 | 2016-12-10 14:23:49 +0200 | [diff] [blame] | 1170 | |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1171 | netif_carrier_on(ndev); |
| 1172 | if (netif_running(ndev)) |
Ivan Khoronzhuk | e05107e | 2016-08-22 21:18:26 +0300 | [diff] [blame] | 1173 | netif_tx_wake_all_queues(ndev); |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1174 | } else { |
| 1175 | netif_carrier_off(ndev); |
Ivan Khoronzhuk | e05107e | 2016-08-22 21:18:26 +0300 | [diff] [blame] | 1176 | netif_tx_stop_all_queues(ndev); |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1177 | } |
| 1178 | } |
| 1179 | |
Mugunthan V N | d9ba8f9 | 2013-02-11 09:52:20 +0000 | [diff] [blame] | 1180 | static inline void cpsw_add_dual_emac_def_ale_entries( |
| 1181 | struct cpsw_priv *priv, struct cpsw_slave *slave, |
| 1182 | u32 slave_port) |
| 1183 | { |
Ivan Khoronzhuk | 2a05a62 | 2016-08-10 02:22:44 +0300 | [diff] [blame] | 1184 | struct cpsw_common *cpsw = priv->cpsw; |
Grygorii Strashko | 71a2cbb | 2016-04-07 15:16:44 +0300 | [diff] [blame] | 1185 | u32 port_mask = 1 << slave_port | ALE_PORT_HOST; |
Mugunthan V N | d9ba8f9 | 2013-02-11 09:52:20 +0000 | [diff] [blame] | 1186 | |
Ivan Khoronzhuk | 2a05a62 | 2016-08-10 02:22:44 +0300 | [diff] [blame] | 1187 | if (cpsw->version == CPSW_VERSION_1) |
Mugunthan V N | d9ba8f9 | 2013-02-11 09:52:20 +0000 | [diff] [blame] | 1188 | slave_write(slave, slave->port_vlan, CPSW1_PORT_VLAN); |
| 1189 | else |
| 1190 | slave_write(slave, slave->port_vlan, CPSW2_PORT_VLAN); |
Ivan Khoronzhuk | 2a05a62 | 2016-08-10 02:22:44 +0300 | [diff] [blame] | 1191 | cpsw_ale_add_vlan(cpsw->ale, slave->port_vlan, port_mask, |
Mugunthan V N | d9ba8f9 | 2013-02-11 09:52:20 +0000 | [diff] [blame] | 1192 | port_mask, port_mask, 0); |
Ivan Khoronzhuk | 2a05a62 | 2016-08-10 02:22:44 +0300 | [diff] [blame] | 1193 | cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast, |
Ivan Khoronzhuk | 5b3a5a1 | 2018-10-12 19:06:29 +0300 | [diff] [blame] | 1194 | ALE_PORT_HOST, ALE_VLAN, slave->port_vlan, 0); |
Ivan Khoronzhuk | 2a05a62 | 2016-08-10 02:22:44 +0300 | [diff] [blame] | 1195 | cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr, |
| 1196 | HOST_PORT_NUM, ALE_VLAN | |
| 1197 | ALE_SECURE, slave->port_vlan); |
Grygorii Strashko | 5e5add1 | 2018-05-01 12:41:22 -0500 | [diff] [blame] | 1198 | cpsw_ale_control_set(cpsw->ale, slave_port, |
| 1199 | ALE_PORT_DROP_UNKNOWN_VLAN, 1); |
Mugunthan V N | d9ba8f9 | 2013-02-11 09:52:20 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
Daniel Mack | 1e7a2e2 | 2013-11-15 08:29:16 +0100 | [diff] [blame] | 1202 | static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv) |
| 1203 | { |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1204 | u32 slave_port; |
Sekhar Nori | 30c57f0 | 2017-04-03 17:34:28 +0530 | [diff] [blame] | 1205 | struct phy_device *phy; |
Ivan Khoronzhuk | 649a168 | 2016-08-10 02:22:37 +0300 | [diff] [blame] | 1206 | struct cpsw_common *cpsw = priv->cpsw; |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1207 | |
Grygorii Strashko | cfc0834 | 2019-04-26 20:12:41 +0300 | [diff] [blame] | 1208 | cpsw_sl_reset(slave->mac_sl, 100); |
| 1209 | cpsw_sl_ctl_reset(slave->mac_sl); |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1210 | |
| 1211 | /* setup priority mapping */ |
Grygorii Strashko | cfc0834 | 2019-04-26 20:12:41 +0300 | [diff] [blame] | 1212 | cpsw_sl_reg_write(slave->mac_sl, CPSW_SL_RX_PRI_MAP, |
| 1213 | RX_PRIORITY_MAPPING); |
Richard Cochran | 9750a3a | 2012-10-29 08:45:15 +0000 | [diff] [blame] | 1214 | |
Ivan Khoronzhuk | 2a05a62 | 2016-08-10 02:22:44 +0300 | [diff] [blame] | 1215 | switch (cpsw->version) { |
Richard Cochran | 9750a3a | 2012-10-29 08:45:15 +0000 | [diff] [blame] | 1216 | case CPSW_VERSION_1: |
| 1217 | slave_write(slave, TX_PRIORITY_MAPPING, CPSW1_TX_PRI_MAP); |
Grygorii Strashko | 48f5bcc | 2017-05-08 14:21:21 -0500 | [diff] [blame] | 1218 | /* Increase RX FIFO size to 5 for supporting fullduplex |
| 1219 | * flow control mode |
| 1220 | */ |
| 1221 | slave_write(slave, |
| 1222 | (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) | |
| 1223 | CPSW_MAX_BLKS_RX, CPSW1_MAX_BLKS); |
Richard Cochran | 9750a3a | 2012-10-29 08:45:15 +0000 | [diff] [blame] | 1224 | break; |
| 1225 | case CPSW_VERSION_2: |
Mugunthan V N | c193f36 | 2013-08-05 17:30:05 +0530 | [diff] [blame] | 1226 | case CPSW_VERSION_3: |
Mugunthan V N | 926489b | 2013-08-12 17:11:15 +0530 | [diff] [blame] | 1227 | case CPSW_VERSION_4: |
Richard Cochran | 9750a3a | 2012-10-29 08:45:15 +0000 | [diff] [blame] | 1228 | slave_write(slave, TX_PRIORITY_MAPPING, CPSW2_TX_PRI_MAP); |
Grygorii Strashko | 48f5bcc | 2017-05-08 14:21:21 -0500 | [diff] [blame] | 1229 | /* Increase RX FIFO size to 5 for supporting fullduplex |
| 1230 | * flow control mode |
| 1231 | */ |
| 1232 | slave_write(slave, |
| 1233 | (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) | |
| 1234 | CPSW_MAX_BLKS_RX, CPSW2_MAX_BLKS); |
Richard Cochran | 9750a3a | 2012-10-29 08:45:15 +0000 | [diff] [blame] | 1235 | break; |
| 1236 | } |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1237 | |
| 1238 | /* setup max packet size, and mac address */ |
Grygorii Strashko | cfc0834 | 2019-04-26 20:12:41 +0300 | [diff] [blame] | 1239 | cpsw_sl_reg_write(slave->mac_sl, CPSW_SL_RX_MAXLEN, |
| 1240 | cpsw->rx_packet_max); |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1241 | cpsw_set_slave_mac(slave, priv); |
| 1242 | |
| 1243 | slave->mac_control = 0; /* no link yet */ |
| 1244 | |
Ivan Khoronzhuk | 6f1f583 | 2016-08-10 02:22:34 +0300 | [diff] [blame] | 1245 | slave_port = cpsw_get_slave_port(slave->slave_num); |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1246 | |
Ivan Khoronzhuk | 606f399 | 2016-08-10 02:22:42 +0300 | [diff] [blame] | 1247 | if (cpsw->data.dual_emac) |
Mugunthan V N | d9ba8f9 | 2013-02-11 09:52:20 +0000 | [diff] [blame] | 1248 | cpsw_add_dual_emac_def_ale_entries(priv, slave, slave_port); |
| 1249 | else |
Ivan Khoronzhuk | 2a05a62 | 2016-08-10 02:22:44 +0300 | [diff] [blame] | 1250 | cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast, |
Mugunthan V N | d9ba8f9 | 2013-02-11 09:52:20 +0000 | [diff] [blame] | 1251 | 1 << slave_port, 0, 0, ALE_MCAST_FWD_2); |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1252 | |
David Rivshin | d733f754 | 2016-04-27 21:32:31 -0400 | [diff] [blame] | 1253 | if (slave->data->phy_node) { |
Sekhar Nori | 30c57f0 | 2017-04-03 17:34:28 +0530 | [diff] [blame] | 1254 | phy = of_phy_connect(priv->ndev, slave->data->phy_node, |
Heiko Schocher | 9e42f71 | 2015-10-17 06:04:35 +0200 | [diff] [blame] | 1255 | &cpsw_adjust_link, 0, slave->data->phy_if); |
Sekhar Nori | 30c57f0 | 2017-04-03 17:34:28 +0530 | [diff] [blame] | 1256 | if (!phy) { |
Rob Herring | f7ce910 | 2017-07-18 16:43:19 -0500 | [diff] [blame] | 1257 | dev_err(priv->dev, "phy \"%pOF\" not found on slave %d\n", |
| 1258 | slave->data->phy_node, |
David Rivshin | d733f754 | 2016-04-27 21:32:31 -0400 | [diff] [blame] | 1259 | slave->slave_num); |
| 1260 | return; |
| 1261 | } |
| 1262 | } else { |
Sekhar Nori | 30c57f0 | 2017-04-03 17:34:28 +0530 | [diff] [blame] | 1263 | phy = phy_connect(priv->ndev, slave->data->phy_id, |
Florian Fainelli | f9a8f83 | 2013-01-14 00:52:52 +0000 | [diff] [blame] | 1264 | &cpsw_adjust_link, slave->data->phy_if); |
Sekhar Nori | 30c57f0 | 2017-04-03 17:34:28 +0530 | [diff] [blame] | 1265 | if (IS_ERR(phy)) { |
David Rivshin | d733f754 | 2016-04-27 21:32:31 -0400 | [diff] [blame] | 1266 | dev_err(priv->dev, |
| 1267 | "phy \"%s\" not found on slave %d, err %ld\n", |
| 1268 | slave->data->phy_id, slave->slave_num, |
Sekhar Nori | 30c57f0 | 2017-04-03 17:34:28 +0530 | [diff] [blame] | 1269 | PTR_ERR(phy)); |
David Rivshin | d733f754 | 2016-04-27 21:32:31 -0400 | [diff] [blame] | 1270 | return; |
| 1271 | } |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1272 | } |
David Rivshin | d733f754 | 2016-04-27 21:32:31 -0400 | [diff] [blame] | 1273 | |
Sekhar Nori | 30c57f0 | 2017-04-03 17:34:28 +0530 | [diff] [blame] | 1274 | slave->phy = phy; |
| 1275 | |
David Rivshin | d733f754 | 2016-04-27 21:32:31 -0400 | [diff] [blame] | 1276 | phy_attached_info(slave->phy); |
| 1277 | |
| 1278 | phy_start(slave->phy); |
| 1279 | |
| 1280 | /* Configure GMII_SEL register */ |
Grygorii Strashko | 3ff1884 | 2018-11-25 18:15:25 -0600 | [diff] [blame] | 1281 | if (!IS_ERR(slave->data->ifphy)) |
| 1282 | phy_set_mode_ext(slave->data->ifphy, PHY_MODE_ETHERNET, |
| 1283 | slave->data->phy_if); |
| 1284 | else |
| 1285 | cpsw_phy_sel(cpsw->dev, slave->phy->interface, |
| 1286 | slave->slave_num); |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
Mugunthan V N | 3b72c2f | 2013-02-05 08:26:48 +0000 | [diff] [blame] | 1289 | static inline void cpsw_add_default_vlan(struct cpsw_priv *priv) |
| 1290 | { |
Ivan Khoronzhuk | 606f399 | 2016-08-10 02:22:42 +0300 | [diff] [blame] | 1291 | struct cpsw_common *cpsw = priv->cpsw; |
| 1292 | const int vlan = cpsw->data.default_vlan; |
Mugunthan V N | 3b72c2f | 2013-02-05 08:26:48 +0000 | [diff] [blame] | 1293 | u32 reg; |
| 1294 | int i; |
Lennart Sorensen | 1e5c4bc | 2014-10-31 13:38:52 -0400 | [diff] [blame] | 1295 | int unreg_mcast_mask; |
Mugunthan V N | 3b72c2f | 2013-02-05 08:26:48 +0000 | [diff] [blame] | 1296 | |
Ivan Khoronzhuk | 2a05a62 | 2016-08-10 02:22:44 +0300 | [diff] [blame] | 1297 | reg = (cpsw->version == CPSW_VERSION_1) ? CPSW1_PORT_VLAN : |
Mugunthan V N | 3b72c2f | 2013-02-05 08:26:48 +0000 | [diff] [blame] | 1298 | CPSW2_PORT_VLAN; |
| 1299 | |
Ivan Khoronzhuk | 5d8d0d4 | 2016-08-10 02:22:39 +0300 | [diff] [blame] | 1300 | writel(vlan, &cpsw->host_port_regs->port_vlan); |
Mugunthan V N | 3b72c2f | 2013-02-05 08:26:48 +0000 | [diff] [blame] | 1301 | |
Ivan Khoronzhuk | 606f399 | 2016-08-10 02:22:42 +0300 | [diff] [blame] | 1302 | for (i = 0; i < cpsw->data.slaves; i++) |
| 1303 | slave_write(cpsw->slaves + i, vlan, reg); |
Mugunthan V N | 3b72c2f | 2013-02-05 08:26:48 +0000 | [diff] [blame] | 1304 | |
Lennart Sorensen | 1e5c4bc | 2014-10-31 13:38:52 -0400 | [diff] [blame] | 1305 | if (priv->ndev->flags & IFF_ALLMULTI) |
| 1306 | unreg_mcast_mask = ALE_ALL_PORTS; |
| 1307 | else |
| 1308 | unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2; |
| 1309 | |
Ivan Khoronzhuk | 2a05a62 | 2016-08-10 02:22:44 +0300 | [diff] [blame] | 1310 | cpsw_ale_add_vlan(cpsw->ale, vlan, ALE_ALL_PORTS, |
Grygorii Strashko | 61f1cef | 2016-04-07 15:16:43 +0300 | [diff] [blame] | 1311 | ALE_ALL_PORTS, ALE_ALL_PORTS, |
| 1312 | unreg_mcast_mask); |
Mugunthan V N | 3b72c2f | 2013-02-05 08:26:48 +0000 | [diff] [blame] | 1313 | } |
| 1314 | |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1315 | static void cpsw_init_host_port(struct cpsw_priv *priv) |
| 1316 | { |
Mugunthan V N | d9ba8f9 | 2013-02-11 09:52:20 +0000 | [diff] [blame] | 1317 | u32 fifo_mode; |
Ivan Khoronzhuk | 5d8d0d4 | 2016-08-10 02:22:39 +0300 | [diff] [blame] | 1318 | u32 control_reg; |
| 1319 | struct cpsw_common *cpsw = priv->cpsw; |
Mugunthan V N | 3b72c2f | 2013-02-05 08:26:48 +0000 | [diff] [blame] | 1320 | |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1321 | /* soft reset the controller and initialize ale */ |
Ivan Khoronzhuk | 5d8d0d4 | 2016-08-10 02:22:39 +0300 | [diff] [blame] | 1322 | soft_reset("cpsw", &cpsw->regs->soft_reset); |
Ivan Khoronzhuk | 2a05a62 | 2016-08-10 02:22:44 +0300 | [diff] [blame] | 1323 | cpsw_ale_start(cpsw->ale); |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1324 | |
| 1325 | /* switch to vlan unaware mode */ |
Ivan Khoronzhuk | 2a05a62 | 2016-08-10 02:22:44 +0300 | [diff] [blame] | 1326 | cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_VLAN_AWARE, |
Mugunthan V N | 3b72c2f | 2013-02-05 08:26:48 +0000 | [diff] [blame] | 1327 | CPSW_ALE_VLAN_AWARE); |
Ivan Khoronzhuk | 5d8d0d4 | 2016-08-10 02:22:39 +0300 | [diff] [blame] | 1328 | control_reg = readl(&cpsw->regs->control); |
Grygorii Strashko | a3a41d2 | 2018-03-15 15:15:50 -0500 | [diff] [blame] | 1329 | control_reg |= CPSW_VLAN_AWARE | CPSW_RX_VLAN_ENCAP; |
Ivan Khoronzhuk | 5d8d0d4 | 2016-08-10 02:22:39 +0300 | [diff] [blame] | 1330 | writel(control_reg, &cpsw->regs->control); |
Ivan Khoronzhuk | 606f399 | 2016-08-10 02:22:42 +0300 | [diff] [blame] | 1331 | fifo_mode = (cpsw->data.dual_emac) ? CPSW_FIFO_DUAL_MAC_MODE : |
Mugunthan V N | d9ba8f9 | 2013-02-11 09:52:20 +0000 | [diff] [blame] | 1332 | CPSW_FIFO_NORMAL_MODE; |
Ivan Khoronzhuk | 5d8d0d4 | 2016-08-10 02:22:39 +0300 | [diff] [blame] | 1333 | writel(fifo_mode, &cpsw->host_port_regs->tx_in_ctl); |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1334 | |
| 1335 | /* setup host port priority mapping */ |
Grygorii Strashko | dda5f5fe | 2017-11-30 18:21:11 -0600 | [diff] [blame] | 1336 | writel_relaxed(CPDMA_TX_PRIORITY_MAP, |
| 1337 | &cpsw->host_port_regs->cpdma_tx_pri_map); |
| 1338 | writel_relaxed(0, &cpsw->host_port_regs->cpdma_rx_chan_map); |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1339 | |
Ivan Khoronzhuk | 2a05a62 | 2016-08-10 02:22:44 +0300 | [diff] [blame] | 1340 | cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1341 | ALE_PORT_STATE, ALE_PORT_STATE_FORWARD); |
| 1342 | |
Ivan Khoronzhuk | 606f399 | 2016-08-10 02:22:42 +0300 | [diff] [blame] | 1343 | if (!cpsw->data.dual_emac) { |
Ivan Khoronzhuk | 2a05a62 | 2016-08-10 02:22:44 +0300 | [diff] [blame] | 1344 | cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM, |
Mugunthan V N | d9ba8f9 | 2013-02-11 09:52:20 +0000 | [diff] [blame] | 1345 | 0, 0); |
Ivan Khoronzhuk | 2a05a62 | 2016-08-10 02:22:44 +0300 | [diff] [blame] | 1346 | cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast, |
Grygorii Strashko | 71a2cbb | 2016-04-07 15:16:44 +0300 | [diff] [blame] | 1347 | ALE_PORT_HOST, 0, 0, ALE_MCAST_FWD_2); |
Mugunthan V N | d9ba8f9 | 2013-02-11 09:52:20 +0000 | [diff] [blame] | 1348 | } |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1349 | } |
| 1350 | |
Grygorii Strashko | c24eef2 | 2019-04-26 20:12:42 +0300 | [diff] [blame] | 1351 | int cpsw_fill_rx_channels(struct cpsw_priv *priv) |
Ivan Khoronzhuk | 3802dce1 | 2016-08-22 21:18:24 +0300 | [diff] [blame] | 1352 | { |
| 1353 | struct cpsw_common *cpsw = priv->cpsw; |
Ivan Khoronzhuk | 9ed4050 | 2019-07-09 00:34:32 +0300 | [diff] [blame] | 1354 | struct cpsw_meta_xdp *xmeta; |
| 1355 | struct page_pool *pool; |
| 1356 | struct page *page; |
Ivan Khoronzhuk | 3802dce1 | 2016-08-22 21:18:24 +0300 | [diff] [blame] | 1357 | int ch_buf_num; |
Ivan Khoronzhuk | e05107e | 2016-08-22 21:18:26 +0300 | [diff] [blame] | 1358 | int ch, i, ret; |
Ivan Khoronzhuk | 9ed4050 | 2019-07-09 00:34:32 +0300 | [diff] [blame] | 1359 | dma_addr_t dma; |
Ivan Khoronzhuk | 3802dce1 | 2016-08-22 21:18:24 +0300 | [diff] [blame] | 1360 | |
Ivan Khoronzhuk | e05107e | 2016-08-22 21:18:26 +0300 | [diff] [blame] | 1361 | for (ch = 0; ch < cpsw->rx_ch_num; ch++) { |
Ivan Khoronzhuk | 9ed4050 | 2019-07-09 00:34:32 +0300 | [diff] [blame] | 1362 | pool = cpsw->page_pool[ch]; |
Ivan Khoronzhuk | 8feb0a1 | 2016-11-29 17:00:51 +0200 | [diff] [blame] | 1363 | ch_buf_num = cpdma_chan_get_rx_buf_num(cpsw->rxv[ch].ch); |
Ivan Khoronzhuk | e05107e | 2016-08-22 21:18:26 +0300 | [diff] [blame] | 1364 | for (i = 0; i < ch_buf_num; i++) { |
Ivan Khoronzhuk | 9ed4050 | 2019-07-09 00:34:32 +0300 | [diff] [blame] | 1365 | page = page_pool_dev_alloc_pages(pool); |
| 1366 | if (!page) { |
| 1367 | cpsw_err(priv, ifup, "allocate rx page err\n"); |
Ivan Khoronzhuk | e05107e | 2016-08-22 21:18:26 +0300 | [diff] [blame] | 1368 | return -ENOMEM; |
| 1369 | } |
| 1370 | |
Ivan Khoronzhuk | 9ed4050 | 2019-07-09 00:34:32 +0300 | [diff] [blame] | 1371 | xmeta = page_address(page) + CPSW_XMETA_OFFSET; |
| 1372 | xmeta->ndev = priv->ndev; |
| 1373 | xmeta->ch = ch; |
| 1374 | |
| 1375 | dma = page_pool_get_dma_addr(page) + CPSW_HEADROOM; |
| 1376 | ret = cpdma_chan_idle_submit_mapped(cpsw->rxv[ch].ch, |
| 1377 | page, dma, |
| 1378 | cpsw->rx_packet_max, |
| 1379 | 0); |
Ivan Khoronzhuk | e05107e | 2016-08-22 21:18:26 +0300 | [diff] [blame] | 1380 | if (ret < 0) { |
| 1381 | cpsw_err(priv, ifup, |
Ivan Khoronzhuk | 9ed4050 | 2019-07-09 00:34:32 +0300 | [diff] [blame] | 1382 | "cannot submit page to channel %d rx, error %d\n", |
Ivan Khoronzhuk | e05107e | 2016-08-22 21:18:26 +0300 | [diff] [blame] | 1383 | ch, ret); |
Ivan Khoronzhuk | 9ed4050 | 2019-07-09 00:34:32 +0300 | [diff] [blame] | 1384 | page_pool_recycle_direct(pool, page); |
Ivan Khoronzhuk | e05107e | 2016-08-22 21:18:26 +0300 | [diff] [blame] | 1385 | return ret; |
| 1386 | } |
Ivan Khoronzhuk | 3802dce1 | 2016-08-22 21:18:24 +0300 | [diff] [blame] | 1387 | } |
| 1388 | |
Ivan Khoronzhuk | e05107e | 2016-08-22 21:18:26 +0300 | [diff] [blame] | 1389 | cpsw_info(priv, ifup, "ch %d rx, submitted %d descriptors\n", |
| 1390 | ch, ch_buf_num); |
Ivan Khoronzhuk | 3802dce1 | 2016-08-22 21:18:24 +0300 | [diff] [blame] | 1391 | } |
| 1392 | |
Ivan Khoronzhuk | e05107e | 2016-08-22 21:18:26 +0300 | [diff] [blame] | 1393 | return 0; |
Ivan Khoronzhuk | 3802dce1 | 2016-08-22 21:18:24 +0300 | [diff] [blame] | 1394 | } |
| 1395 | |
Ivan Khoronzhuk | 2a05a62 | 2016-08-10 02:22:44 +0300 | [diff] [blame] | 1396 | static void cpsw_slave_stop(struct cpsw_slave *slave, struct cpsw_common *cpsw) |
Sebastian Siewior | aacebbf | 2013-04-23 07:31:36 +0000 | [diff] [blame] | 1397 | { |
Schuyler Patton | 3995d26 | 2014-03-03 16:19:06 +0530 | [diff] [blame] | 1398 | u32 slave_port; |
| 1399 | |
Ivan Khoronzhuk | 6f1f583 | 2016-08-10 02:22:34 +0300 | [diff] [blame] | 1400 | slave_port = cpsw_get_slave_port(slave->slave_num); |
Schuyler Patton | 3995d26 | 2014-03-03 16:19:06 +0530 | [diff] [blame] | 1401 | |
Sebastian Siewior | aacebbf | 2013-04-23 07:31:36 +0000 | [diff] [blame] | 1402 | if (!slave->phy) |
| 1403 | return; |
| 1404 | phy_stop(slave->phy); |
| 1405 | phy_disconnect(slave->phy); |
| 1406 | slave->phy = NULL; |
Ivan Khoronzhuk | 2a05a62 | 2016-08-10 02:22:44 +0300 | [diff] [blame] | 1407 | cpsw_ale_control_set(cpsw->ale, slave_port, |
Schuyler Patton | 3995d26 | 2014-03-03 16:19:06 +0530 | [diff] [blame] | 1408 | ALE_PORT_STATE, ALE_PORT_STATE_DISABLE); |
Grygorii Strashko | cfc0834 | 2019-04-26 20:12:41 +0300 | [diff] [blame] | 1409 | cpsw_sl_reset(slave->mac_sl, 100); |
| 1410 | cpsw_sl_ctl_reset(slave->mac_sl); |
Sebastian Siewior | aacebbf | 2013-04-23 07:31:36 +0000 | [diff] [blame] | 1411 | } |
| 1412 | |
Ivan Khoronzhuk | 7929a66 | 2018-07-24 00:26:31 +0300 | [diff] [blame] | 1413 | static int cpsw_tc_to_fifo(int tc, int num_tc) |
| 1414 | { |
| 1415 | if (tc == num_tc - 1) |
| 1416 | return 0; |
| 1417 | |
| 1418 | return CPSW_FIFO_SHAPERS_NUM - tc; |
| 1419 | } |
| 1420 | |
Ivan Khoronzhuk | 57d9014 | 2018-07-24 00:26:32 +0300 | [diff] [blame] | 1421 | static int cpsw_set_fifo_bw(struct cpsw_priv *priv, int fifo, int bw) |
| 1422 | { |
| 1423 | struct cpsw_common *cpsw = priv->cpsw; |
| 1424 | u32 val = 0, send_pct, shift; |
| 1425 | struct cpsw_slave *slave; |
| 1426 | int pct = 0, i; |
| 1427 | |
| 1428 | if (bw > priv->shp_cfg_speed * 1000) |
| 1429 | goto err; |
| 1430 | |
| 1431 | /* shaping has to stay enabled for highest fifos linearly |
| 1432 | * and fifo bw no more then interface can allow |
| 1433 | */ |
| 1434 | slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)]; |
| 1435 | send_pct = slave_read(slave, SEND_PERCENT); |
| 1436 | for (i = CPSW_FIFO_SHAPERS_NUM; i > 0; i--) { |
| 1437 | if (!bw) { |
| 1438 | if (i >= fifo || !priv->fifo_bw[i]) |
| 1439 | continue; |
| 1440 | |
| 1441 | dev_warn(priv->dev, "Prev FIFO%d is shaped", i); |
| 1442 | continue; |
| 1443 | } |
| 1444 | |
| 1445 | if (!priv->fifo_bw[i] && i > fifo) { |
| 1446 | dev_err(priv->dev, "Upper FIFO%d is not shaped", i); |
| 1447 | return -EINVAL; |
| 1448 | } |
| 1449 | |
| 1450 | shift = (i - 1) * 8; |
| 1451 | if (i == fifo) { |
| 1452 | send_pct &= ~(CPSW_PCT_MASK << shift); |
| 1453 | val = DIV_ROUND_UP(bw, priv->shp_cfg_speed * 10); |
| 1454 | if (!val) |
| 1455 | val = 1; |
| 1456 | |
| 1457 | send_pct |= val << shift; |
| 1458 | pct += val; |
| 1459 | continue; |
| 1460 | } |
| 1461 | |
| 1462 | if (priv->fifo_bw[i]) |
| 1463 | pct += (send_pct >> shift) & CPSW_PCT_MASK; |
| 1464 | } |
| 1465 | |
| 1466 | if (pct >= 100) |
| 1467 | goto err; |
| 1468 | |
| 1469 | slave_write(slave, send_pct, SEND_PERCENT); |
| 1470 | priv->fifo_bw[fifo] = bw; |
| 1471 | |
| 1472 | dev_warn(priv->dev, "set FIFO%d bw = %d\n", fifo, |
| 1473 | DIV_ROUND_CLOSEST(val * priv->shp_cfg_speed, 100)); |
| 1474 | |
| 1475 | return 0; |
| 1476 | err: |
| 1477 | dev_err(priv->dev, "Bandwidth doesn't fit in tc configuration"); |
| 1478 | return -EINVAL; |
| 1479 | } |
| 1480 | |
| 1481 | static int cpsw_set_fifo_rlimit(struct cpsw_priv *priv, int fifo, int bw) |
| 1482 | { |
| 1483 | struct cpsw_common *cpsw = priv->cpsw; |
| 1484 | struct cpsw_slave *slave; |
| 1485 | u32 tx_in_ctl_rg, val; |
| 1486 | int ret; |
| 1487 | |
| 1488 | ret = cpsw_set_fifo_bw(priv, fifo, bw); |
| 1489 | if (ret) |
| 1490 | return ret; |
| 1491 | |
| 1492 | slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)]; |
| 1493 | tx_in_ctl_rg = cpsw->version == CPSW_VERSION_1 ? |
| 1494 | CPSW1_TX_IN_CTL : CPSW2_TX_IN_CTL; |
| 1495 | |
| 1496 | if (!bw) |
| 1497 | cpsw_fifo_shp_on(priv, fifo, bw); |
| 1498 | |
| 1499 | val = slave_read(slave, tx_in_ctl_rg); |
| 1500 | if (cpsw_shp_is_off(priv)) { |
| 1501 | /* disable FIFOs rate limited queues */ |
| 1502 | val &= ~(0xf << CPSW_FIFO_RATE_EN_SHIFT); |
| 1503 | |
| 1504 | /* set type of FIFO queues to normal priority mode */ |
| 1505 | val &= ~(3 << CPSW_FIFO_QUEUE_TYPE_SHIFT); |
| 1506 | |
| 1507 | /* set type of FIFO queues to be rate limited */ |
| 1508 | if (bw) |
| 1509 | val |= 2 << CPSW_FIFO_QUEUE_TYPE_SHIFT; |
| 1510 | else |
| 1511 | priv->shp_cfg_speed = 0; |
| 1512 | } |
| 1513 | |
| 1514 | /* toggle a FIFO rate limited queue */ |
| 1515 | if (bw) |
| 1516 | val |= BIT(fifo + CPSW_FIFO_RATE_EN_SHIFT); |
| 1517 | else |
| 1518 | val &= ~BIT(fifo + CPSW_FIFO_RATE_EN_SHIFT); |
| 1519 | slave_write(slave, val, tx_in_ctl_rg); |
| 1520 | |
| 1521 | /* FIFO transmit shape enable */ |
| 1522 | cpsw_fifo_shp_on(priv, fifo, bw); |
| 1523 | return 0; |
| 1524 | } |
| 1525 | |
| 1526 | /* Defaults: |
| 1527 | * class A - prio 3 |
| 1528 | * class B - prio 2 |
| 1529 | * shaping for class A should be set first |
| 1530 | */ |
| 1531 | static int cpsw_set_cbs(struct net_device *ndev, |
| 1532 | struct tc_cbs_qopt_offload *qopt) |
| 1533 | { |
| 1534 | struct cpsw_priv *priv = netdev_priv(ndev); |
| 1535 | struct cpsw_common *cpsw = priv->cpsw; |
| 1536 | struct cpsw_slave *slave; |
| 1537 | int prev_speed = 0; |
| 1538 | int tc, ret, fifo; |
| 1539 | u32 bw = 0; |
| 1540 | |
| 1541 | tc = netdev_txq_to_tc(priv->ndev, qopt->queue); |
| 1542 | |
| 1543 | /* enable channels in backward order, as highest FIFOs must be rate |
| 1544 | * limited first and for compliance with CPDMA rate limited channels |
| 1545 | * that also used in bacward order. FIFO0 cannot be rate limited. |
| 1546 | */ |
| 1547 | fifo = cpsw_tc_to_fifo(tc, ndev->num_tc); |
| 1548 | if (!fifo) { |
| 1549 | dev_err(priv->dev, "Last tc%d can't be rate limited", tc); |
| 1550 | return -EINVAL; |
| 1551 | } |
| 1552 | |
| 1553 | /* do nothing, it's disabled anyway */ |
| 1554 | if (!qopt->enable && !priv->fifo_bw[fifo]) |
| 1555 | return 0; |
| 1556 | |
| 1557 | /* shapers can be set if link speed is known */ |
| 1558 | slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)]; |
| 1559 | if (slave->phy && slave->phy->link) { |
| 1560 | if (priv->shp_cfg_speed && |
| 1561 | priv->shp_cfg_speed != slave->phy->speed) |
| 1562 | prev_speed = priv->shp_cfg_speed; |
| 1563 | |
| 1564 | priv->shp_cfg_speed = slave->phy->speed; |
| 1565 | } |
| 1566 | |
| 1567 | if (!priv->shp_cfg_speed) { |
| 1568 | dev_err(priv->dev, "Link speed is not known"); |
| 1569 | return -1; |
| 1570 | } |
| 1571 | |
| 1572 | ret = pm_runtime_get_sync(cpsw->dev); |
| 1573 | if (ret < 0) { |
| 1574 | pm_runtime_put_noidle(cpsw->dev); |
| 1575 | return ret; |
| 1576 | } |
| 1577 | |
| 1578 | bw = qopt->enable ? qopt->idleslope : 0; |
| 1579 | ret = cpsw_set_fifo_rlimit(priv, fifo, bw); |
| 1580 | if (ret) { |
| 1581 | priv->shp_cfg_speed = prev_speed; |
| 1582 | prev_speed = 0; |
| 1583 | } |
| 1584 | |
| 1585 | if (bw && prev_speed) |
| 1586 | dev_warn(priv->dev, |
| 1587 | "Speed was changed, CBS shaper speeds are changed!"); |
| 1588 | |
| 1589 | pm_runtime_put_sync(cpsw->dev); |
| 1590 | return ret; |
| 1591 | } |
| 1592 | |
Ivan Khoronzhuk | 4b4255e | 2018-07-24 00:26:33 +0300 | [diff] [blame] | 1593 | static void cpsw_cbs_resume(struct cpsw_slave *slave, struct cpsw_priv *priv) |
| 1594 | { |
| 1595 | int fifo, bw; |
| 1596 | |
| 1597 | for (fifo = CPSW_FIFO_SHAPERS_NUM; fifo > 0; fifo--) { |
| 1598 | bw = priv->fifo_bw[fifo]; |
| 1599 | if (!bw) |
| 1600 | continue; |
| 1601 | |
| 1602 | cpsw_set_fifo_rlimit(priv, fifo, bw); |
| 1603 | } |
| 1604 | } |
| 1605 | |
| 1606 | static void cpsw_mqprio_resume(struct cpsw_slave *slave, struct cpsw_priv *priv) |
| 1607 | { |
| 1608 | struct cpsw_common *cpsw = priv->cpsw; |
| 1609 | u32 tx_prio_map = 0; |
| 1610 | int i, tc, fifo; |
| 1611 | u32 tx_prio_rg; |
| 1612 | |
| 1613 | if (!priv->mqprio_hw) |
| 1614 | return; |
| 1615 | |
| 1616 | for (i = 0; i < 8; i++) { |
| 1617 | tc = netdev_get_prio_tc_map(priv->ndev, i); |
| 1618 | fifo = CPSW_FIFO_SHAPERS_NUM - tc; |
| 1619 | tx_prio_map |= fifo << (4 * i); |
| 1620 | } |
| 1621 | |
| 1622 | tx_prio_rg = cpsw->version == CPSW_VERSION_1 ? |
| 1623 | CPSW1_TX_PRI_MAP : CPSW2_TX_PRI_MAP; |
| 1624 | |
| 1625 | slave_write(slave, tx_prio_map, tx_prio_rg); |
| 1626 | } |
| 1627 | |
Ivan Khoronzhuk | 00fe471 | 2018-11-08 22:27:57 +0200 | [diff] [blame] | 1628 | static int cpsw_restore_vlans(struct net_device *vdev, int vid, void *arg) |
| 1629 | { |
| 1630 | struct cpsw_priv *priv = arg; |
| 1631 | |
| 1632 | if (!vdev) |
| 1633 | return 0; |
| 1634 | |
| 1635 | cpsw_ndo_vlan_rx_add_vid(priv->ndev, 0, vid); |
| 1636 | return 0; |
| 1637 | } |
| 1638 | |
Ivan Khoronzhuk | 4b4255e | 2018-07-24 00:26:33 +0300 | [diff] [blame] | 1639 | /* restore resources after port reset */ |
| 1640 | static void cpsw_restore(struct cpsw_priv *priv) |
| 1641 | { |
Ivan Khoronzhuk | 00fe471 | 2018-11-08 22:27:57 +0200 | [diff] [blame] | 1642 | /* restore vlan configurations */ |
| 1643 | vlan_for_each(priv->ndev, cpsw_restore_vlans, priv); |
| 1644 | |
Ivan Khoronzhuk | 4b4255e | 2018-07-24 00:26:33 +0300 | [diff] [blame] | 1645 | /* restore MQPRIO offload */ |
| 1646 | for_each_slave(priv, cpsw_mqprio_resume, priv); |
| 1647 | |
| 1648 | /* restore CBS offload */ |
| 1649 | for_each_slave(priv, cpsw_cbs_resume, priv); |
| 1650 | } |
| 1651 | |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1652 | static int cpsw_ndo_open(struct net_device *ndev) |
| 1653 | { |
| 1654 | struct cpsw_priv *priv = netdev_priv(ndev); |
Ivan Khoronzhuk | 649a168 | 2016-08-10 02:22:37 +0300 | [diff] [blame] | 1655 | struct cpsw_common *cpsw = priv->cpsw; |
Ivan Khoronzhuk | 3802dce1 | 2016-08-22 21:18:24 +0300 | [diff] [blame] | 1656 | int ret; |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1657 | u32 reg; |
| 1658 | |
Ivan Khoronzhuk | 56e31bd | 2016-08-10 02:22:38 +0300 | [diff] [blame] | 1659 | ret = pm_runtime_get_sync(cpsw->dev); |
Grygorii Strashko | 108a653 | 2016-06-24 21:23:42 +0300 | [diff] [blame] | 1660 | if (ret < 0) { |
Ivan Khoronzhuk | 56e31bd | 2016-08-10 02:22:38 +0300 | [diff] [blame] | 1661 | pm_runtime_put_noidle(cpsw->dev); |
Grygorii Strashko | 108a653 | 2016-06-24 21:23:42 +0300 | [diff] [blame] | 1662 | return ret; |
| 1663 | } |
Grygorii Strashko | 3fa88c5 | 2016-04-19 21:09:49 +0300 | [diff] [blame] | 1664 | |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1665 | netif_carrier_off(ndev); |
| 1666 | |
Ivan Khoronzhuk | e05107e | 2016-08-22 21:18:26 +0300 | [diff] [blame] | 1667 | /* Notify the stack of the actual queue counts. */ |
| 1668 | ret = netif_set_real_num_tx_queues(ndev, cpsw->tx_ch_num); |
| 1669 | if (ret) { |
| 1670 | dev_err(priv->dev, "cannot set real number of tx queues\n"); |
| 1671 | goto err_cleanup; |
| 1672 | } |
| 1673 | |
| 1674 | ret = netif_set_real_num_rx_queues(ndev, cpsw->rx_ch_num); |
| 1675 | if (ret) { |
| 1676 | dev_err(priv->dev, "cannot set real number of rx queues\n"); |
| 1677 | goto err_cleanup; |
| 1678 | } |
| 1679 | |
Ivan Khoronzhuk | 2a05a62 | 2016-08-10 02:22:44 +0300 | [diff] [blame] | 1680 | reg = cpsw->version; |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1681 | |
| 1682 | dev_info(priv->dev, "initializing cpsw version %d.%d (%d)\n", |
| 1683 | CPSW_MAJOR_VERSION(reg), CPSW_MINOR_VERSION(reg), |
| 1684 | CPSW_RTL_VERSION(reg)); |
| 1685 | |
Ivan Khoronzhuk | d5bc161 | 2017-02-14 16:02:36 +0200 | [diff] [blame] | 1686 | /* Initialize host and slave ports */ |
| 1687 | if (!cpsw->usage_count) |
Mugunthan V N | d9ba8f9 | 2013-02-11 09:52:20 +0000 | [diff] [blame] | 1688 | cpsw_init_host_port(priv); |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1689 | for_each_slave(priv, cpsw_slave_open, priv); |
| 1690 | |
Mugunthan V N | 3b72c2f | 2013-02-05 08:26:48 +0000 | [diff] [blame] | 1691 | /* Add default VLAN */ |
Ivan Khoronzhuk | 606f399 | 2016-08-10 02:22:42 +0300 | [diff] [blame] | 1692 | if (!cpsw->data.dual_emac) |
Mugunthan V N | e6afea0 | 2014-06-18 17:21:48 +0530 | [diff] [blame] | 1693 | cpsw_add_default_vlan(priv); |
| 1694 | else |
Ivan Khoronzhuk | 2a05a62 | 2016-08-10 02:22:44 +0300 | [diff] [blame] | 1695 | cpsw_ale_add_vlan(cpsw->ale, cpsw->data.default_vlan, |
Grygorii Strashko | 61f1cef | 2016-04-07 15:16:43 +0300 | [diff] [blame] | 1696 | ALE_ALL_PORTS, ALE_ALL_PORTS, 0, 0); |
Mugunthan V N | 3b72c2f | 2013-02-05 08:26:48 +0000 | [diff] [blame] | 1697 | |
Ivan Khoronzhuk | d5bc161 | 2017-02-14 16:02:36 +0200 | [diff] [blame] | 1698 | /* initialize shared resources for every ndev */ |
| 1699 | if (!cpsw->usage_count) { |
Mugunthan V N | d9ba8f9 | 2013-02-11 09:52:20 +0000 | [diff] [blame] | 1700 | /* disable priority elevation */ |
Grygorii Strashko | dda5f5fe | 2017-11-30 18:21:11 -0600 | [diff] [blame] | 1701 | writel_relaxed(0, &cpsw->regs->ptype); |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1702 | |
Mugunthan V N | d9ba8f9 | 2013-02-11 09:52:20 +0000 | [diff] [blame] | 1703 | /* enable statistics collection only on all ports */ |
Grygorii Strashko | dda5f5fe | 2017-11-30 18:21:11 -0600 | [diff] [blame] | 1704 | writel_relaxed(0x7, &cpsw->regs->stat_port_en); |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1705 | |
Mugunthan V N | 1923d6e | 2014-09-08 22:54:02 +0530 | [diff] [blame] | 1706 | /* Enable internal fifo flow control */ |
Ivan Khoronzhuk | 5d8d0d4 | 2016-08-10 02:22:39 +0300 | [diff] [blame] | 1707 | writel(0x7, &cpsw->regs->flow_control); |
Mugunthan V N | 1923d6e | 2014-09-08 22:54:02 +0530 | [diff] [blame] | 1708 | |
Ivan Khoronzhuk | dbc4ec5 | 2016-08-10 02:22:43 +0300 | [diff] [blame] | 1709 | napi_enable(&cpsw->napi_rx); |
| 1710 | napi_enable(&cpsw->napi_tx); |
Mugunthan V N | d354eb8 | 2015-08-04 16:06:19 +0530 | [diff] [blame] | 1711 | |
Ivan Khoronzhuk | e38b5a3 | 2016-08-10 02:22:41 +0300 | [diff] [blame] | 1712 | if (cpsw->tx_irq_disabled) { |
| 1713 | cpsw->tx_irq_disabled = false; |
| 1714 | enable_irq(cpsw->irqs_table[1]); |
Mugunthan V N | 7da1160 | 2015-08-12 15:22:53 +0530 | [diff] [blame] | 1715 | } |
| 1716 | |
Ivan Khoronzhuk | e38b5a3 | 2016-08-10 02:22:41 +0300 | [diff] [blame] | 1717 | if (cpsw->rx_irq_disabled) { |
| 1718 | cpsw->rx_irq_disabled = false; |
| 1719 | enable_irq(cpsw->irqs_table[0]); |
Mugunthan V N | 7da1160 | 2015-08-12 15:22:53 +0530 | [diff] [blame] | 1720 | } |
| 1721 | |
Ivan Khoronzhuk | 9ed4050 | 2019-07-09 00:34:32 +0300 | [diff] [blame] | 1722 | /* create rxqs for both infs in dual mac as they use same pool |
| 1723 | * and must be destroyed together when no users. |
| 1724 | */ |
| 1725 | ret = cpsw_create_xdp_rxqs(cpsw); |
| 1726 | if (ret < 0) |
| 1727 | goto err_cleanup; |
| 1728 | |
Ivan Khoronzhuk | 3802dce1 | 2016-08-22 21:18:24 +0300 | [diff] [blame] | 1729 | ret = cpsw_fill_rx_channels(priv); |
| 1730 | if (ret < 0) |
| 1731 | goto err_cleanup; |
Mugunthan V N | f280e89 | 2013-12-11 22:09:05 -0600 | [diff] [blame] | 1732 | |
Grygorii Strashko | 8a2c9a5 | 2016-12-06 18:00:41 -0600 | [diff] [blame] | 1733 | if (cpts_register(cpsw->cpts)) |
Mugunthan V N | f280e89 | 2013-12-11 22:09:05 -0600 | [diff] [blame] | 1734 | dev_err(priv->dev, "error registering cpts device\n"); |
| 1735 | |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1736 | } |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1737 | |
Ivan Khoronzhuk | 4b4255e | 2018-07-24 00:26:33 +0300 | [diff] [blame] | 1738 | cpsw_restore(priv); |
| 1739 | |
Mugunthan V N | ff5b8ef | 2013-03-11 23:16:37 +0000 | [diff] [blame] | 1740 | /* Enable Interrupt pacing if configured */ |
Ivan Khoronzhuk | 2a05a62 | 2016-08-10 02:22:44 +0300 | [diff] [blame] | 1741 | if (cpsw->coal_intvl != 0) { |
Mugunthan V N | ff5b8ef | 2013-03-11 23:16:37 +0000 | [diff] [blame] | 1742 | struct ethtool_coalesce coal; |
| 1743 | |
Ivan Khoronzhuk | 2a05a62 | 2016-08-10 02:22:44 +0300 | [diff] [blame] | 1744 | coal.rx_coalesce_usecs = cpsw->coal_intvl; |
Mugunthan V N | ff5b8ef | 2013-03-11 23:16:37 +0000 | [diff] [blame] | 1745 | cpsw_set_coalesce(ndev, &coal); |
| 1746 | } |
| 1747 | |
Ivan Khoronzhuk | 2c836bd | 2016-08-10 02:22:40 +0300 | [diff] [blame] | 1748 | cpdma_ctlr_start(cpsw->dma); |
| 1749 | cpsw_intr_enable(cpsw); |
Ivan Khoronzhuk | d5bc161 | 2017-02-14 16:02:36 +0200 | [diff] [blame] | 1750 | cpsw->usage_count++; |
Mugunthan V N | f63a975 | 2014-04-10 14:23:24 +0530 | [diff] [blame] | 1751 | |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1752 | return 0; |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1753 | |
Sebastian Siewior | aacebbf | 2013-04-23 07:31:36 +0000 | [diff] [blame] | 1754 | err_cleanup: |
Ivan Khoronzhuk | 02caced | 2019-05-28 20:45:19 +0300 | [diff] [blame] | 1755 | if (!cpsw->usage_count) { |
| 1756 | cpdma_ctlr_stop(cpsw->dma); |
Ivan Khoronzhuk | 9ed4050 | 2019-07-09 00:34:32 +0300 | [diff] [blame] | 1757 | cpsw_destroy_xdp_rxqs(cpsw); |
Ivan Khoronzhuk | 02caced | 2019-05-28 20:45:19 +0300 | [diff] [blame] | 1758 | } |
| 1759 | |
Ivan Khoronzhuk | 9ed4050 | 2019-07-09 00:34:32 +0300 | [diff] [blame] | 1760 | for_each_slave(priv, cpsw_slave_stop, cpsw); |
Ivan Khoronzhuk | 56e31bd | 2016-08-10 02:22:38 +0300 | [diff] [blame] | 1761 | pm_runtime_put_sync(cpsw->dev); |
Sebastian Siewior | aacebbf | 2013-04-23 07:31:36 +0000 | [diff] [blame] | 1762 | netif_carrier_off(priv->ndev); |
| 1763 | return ret; |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1764 | } |
| 1765 | |
| 1766 | static int cpsw_ndo_stop(struct net_device *ndev) |
| 1767 | { |
| 1768 | struct cpsw_priv *priv = netdev_priv(ndev); |
Ivan Khoronzhuk | 649a168 | 2016-08-10 02:22:37 +0300 | [diff] [blame] | 1769 | struct cpsw_common *cpsw = priv->cpsw; |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1770 | |
| 1771 | cpsw_info(priv, ifdown, "shutting down cpsw device\n"); |
Ivan Khoronzhuk | 15180ec | 2018-11-08 22:27:56 +0200 | [diff] [blame] | 1772 | __hw_addr_ref_unsync_dev(&ndev->mc, ndev, cpsw_purge_all_mc); |
Ivan Khoronzhuk | e05107e | 2016-08-22 21:18:26 +0300 | [diff] [blame] | 1773 | netif_tx_stop_all_queues(priv->ndev); |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1774 | netif_carrier_off(priv->ndev); |
Mugunthan V N | d9ba8f9 | 2013-02-11 09:52:20 +0000 | [diff] [blame] | 1775 | |
Ivan Khoronzhuk | d5bc161 | 2017-02-14 16:02:36 +0200 | [diff] [blame] | 1776 | if (cpsw->usage_count <= 1) { |
Ivan Khoronzhuk | dbc4ec5 | 2016-08-10 02:22:43 +0300 | [diff] [blame] | 1777 | napi_disable(&cpsw->napi_rx); |
| 1778 | napi_disable(&cpsw->napi_tx); |
Ivan Khoronzhuk | 2a05a62 | 2016-08-10 02:22:44 +0300 | [diff] [blame] | 1779 | cpts_unregister(cpsw->cpts); |
Ivan Khoronzhuk | 2c836bd | 2016-08-10 02:22:40 +0300 | [diff] [blame] | 1780 | cpsw_intr_disable(cpsw); |
| 1781 | cpdma_ctlr_stop(cpsw->dma); |
Ivan Khoronzhuk | 2a05a62 | 2016-08-10 02:22:44 +0300 | [diff] [blame] | 1782 | cpsw_ale_stop(cpsw->ale); |
Ivan Khoronzhuk | 9ed4050 | 2019-07-09 00:34:32 +0300 | [diff] [blame] | 1783 | cpsw_destroy_xdp_rxqs(cpsw); |
Mugunthan V N | d9ba8f9 | 2013-02-11 09:52:20 +0000 | [diff] [blame] | 1784 | } |
Ivan Khoronzhuk | 2a05a62 | 2016-08-10 02:22:44 +0300 | [diff] [blame] | 1785 | for_each_slave(priv, cpsw_slave_stop, cpsw); |
Ivan Khoronzhuk | 0be01b8 | 2016-12-10 14:23:49 +0200 | [diff] [blame] | 1786 | |
| 1787 | if (cpsw_need_resplit(cpsw)) |
Grygorii Strashko | 9763a89 | 2019-04-26 20:12:26 +0300 | [diff] [blame] | 1788 | cpsw_split_res(cpsw); |
Ivan Khoronzhuk | 0be01b8 | 2016-12-10 14:23:49 +0200 | [diff] [blame] | 1789 | |
Ivan Khoronzhuk | d5bc161 | 2017-02-14 16:02:36 +0200 | [diff] [blame] | 1790 | cpsw->usage_count--; |
Ivan Khoronzhuk | 56e31bd | 2016-08-10 02:22:38 +0300 | [diff] [blame] | 1791 | pm_runtime_put_sync(cpsw->dev); |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1792 | return 0; |
| 1793 | } |
| 1794 | |
| 1795 | static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb, |
| 1796 | struct net_device *ndev) |
| 1797 | { |
| 1798 | struct cpsw_priv *priv = netdev_priv(ndev); |
Ivan Khoronzhuk | 2c836bd | 2016-08-10 02:22:40 +0300 | [diff] [blame] | 1799 | struct cpsw_common *cpsw = priv->cpsw; |
Ivan Khoronzhuk | f44f841 | 2017-06-27 16:58:52 +0300 | [diff] [blame] | 1800 | struct cpts *cpts = cpsw->cpts; |
Ivan Khoronzhuk | e05107e | 2016-08-22 21:18:26 +0300 | [diff] [blame] | 1801 | struct netdev_queue *txq; |
| 1802 | struct cpdma_chan *txch; |
| 1803 | int ret, q_idx; |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1804 | |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1805 | if (skb_padto(skb, CPSW_MIN_PACKET_SIZE)) { |
| 1806 | cpsw_err(priv, tx_err, "packet pad failed\n"); |
Tobias Klauser | 8dc43dd | 2014-03-10 13:12:23 +0100 | [diff] [blame] | 1807 | ndev->stats.tx_dropped++; |
Ivan Khoronzhuk | 1bf9605 | 2017-02-11 03:49:57 +0200 | [diff] [blame] | 1808 | return NET_XMIT_DROP; |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1809 | } |
| 1810 | |
Mugunthan V N | 9232b16 | 2013-02-11 09:52:19 +0000 | [diff] [blame] | 1811 | if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP && |
Ivan Khoronzhuk | a942312 | 2018-11-12 16:00:22 +0200 | [diff] [blame] | 1812 | priv->tx_ts_enabled && cpts_can_timestamp(cpts, skb)) |
Richard Cochran | 2e5b38a | 2012-10-29 08:45:20 +0000 | [diff] [blame] | 1813 | skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; |
| 1814 | |
Ivan Khoronzhuk | e05107e | 2016-08-22 21:18:26 +0300 | [diff] [blame] | 1815 | q_idx = skb_get_queue_mapping(skb); |
| 1816 | if (q_idx >= cpsw->tx_ch_num) |
| 1817 | q_idx = q_idx % cpsw->tx_ch_num; |
| 1818 | |
Ivan Khoronzhuk | 8feb0a1 | 2016-11-29 17:00:51 +0200 | [diff] [blame] | 1819 | txch = cpsw->txv[q_idx].ch; |
Grygorii Strashko | 62f94c2 | 2018-02-06 19:17:06 -0600 | [diff] [blame] | 1820 | txq = netdev_get_tx_queue(ndev, q_idx); |
Grygorii Strashko | 10ae805 | 2019-04-26 20:12:30 +0300 | [diff] [blame] | 1821 | skb_tx_timestamp(skb); |
| 1822 | ret = cpdma_chan_submit(txch, skb, skb->data, skb->len, |
| 1823 | priv->emac_port + cpsw->data.dual_emac); |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1824 | if (unlikely(ret != 0)) { |
| 1825 | cpsw_err(priv, tx_err, "desc submit failed\n"); |
| 1826 | goto fail; |
| 1827 | } |
| 1828 | |
Mugunthan V N | fae5082 | 2013-01-17 06:31:34 +0000 | [diff] [blame] | 1829 | /* If there is no more tx desc left free then we need to |
| 1830 | * tell the kernel to stop sending us tx frames. |
| 1831 | */ |
Ivan Khoronzhuk | e05107e | 2016-08-22 21:18:26 +0300 | [diff] [blame] | 1832 | if (unlikely(!cpdma_check_free_tx_desc(txch))) { |
Ivan Khoronzhuk | e05107e | 2016-08-22 21:18:26 +0300 | [diff] [blame] | 1833 | netif_tx_stop_queue(txq); |
Grygorii Strashko | 62f94c2 | 2018-02-06 19:17:06 -0600 | [diff] [blame] | 1834 | |
| 1835 | /* Barrier, so that stop_queue visible to other cpus */ |
| 1836 | smp_mb__after_atomic(); |
| 1837 | |
| 1838 | if (cpdma_check_free_tx_desc(txch)) |
| 1839 | netif_tx_wake_queue(txq); |
Ivan Khoronzhuk | e05107e | 2016-08-22 21:18:26 +0300 | [diff] [blame] | 1840 | } |
Mugunthan V N | fae5082 | 2013-01-17 06:31:34 +0000 | [diff] [blame] | 1841 | |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1842 | return NETDEV_TX_OK; |
| 1843 | fail: |
Tobias Klauser | 8dc43dd | 2014-03-10 13:12:23 +0100 | [diff] [blame] | 1844 | ndev->stats.tx_dropped++; |
Ivan Khoronzhuk | e05107e | 2016-08-22 21:18:26 +0300 | [diff] [blame] | 1845 | netif_tx_stop_queue(txq); |
Grygorii Strashko | 62f94c2 | 2018-02-06 19:17:06 -0600 | [diff] [blame] | 1846 | |
| 1847 | /* Barrier, so that stop_queue visible to other cpus */ |
| 1848 | smp_mb__after_atomic(); |
| 1849 | |
| 1850 | if (cpdma_check_free_tx_desc(txch)) |
| 1851 | netif_tx_wake_queue(txq); |
| 1852 | |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 1853 | return NETDEV_TX_BUSY; |
| 1854 | } |
| 1855 | |
Grygorii Strashko | c8395d4 | 2016-12-06 18:00:34 -0600 | [diff] [blame] | 1856 | #if IS_ENABLED(CONFIG_TI_CPTS) |
Richard Cochran | 2e5b38a | 2012-10-29 08:45:20 +0000 | [diff] [blame] | 1857 | |
Ivan Khoronzhuk | a942312 | 2018-11-12 16:00:22 +0200 | [diff] [blame] | 1858 | static void cpsw_hwtstamp_v1(struct cpsw_priv *priv) |
Richard Cochran | 2e5b38a | 2012-10-29 08:45:20 +0000 | [diff] [blame] | 1859 | { |
Ivan Khoronzhuk | a942312 | 2018-11-12 16:00:22 +0200 | [diff] [blame] | 1860 | struct cpsw_common *cpsw = priv->cpsw; |
Ivan Khoronzhuk | 606f399 | 2016-08-10 02:22:42 +0300 | [diff] [blame] | 1861 | struct cpsw_slave *slave = &cpsw->slaves[cpsw->data.active_slave]; |
Richard Cochran | 2e5b38a | 2012-10-29 08:45:20 +0000 | [diff] [blame] | 1862 | u32 ts_en, seq_id; |
| 1863 | |
Ivan Khoronzhuk | a942312 | 2018-11-12 16:00:22 +0200 | [diff] [blame] | 1864 | if (!priv->tx_ts_enabled && !priv->rx_ts_enabled) { |
Richard Cochran | 2e5b38a | 2012-10-29 08:45:20 +0000 | [diff] [blame] | 1865 | slave_write(slave, 0, CPSW1_TS_CTL); |
| 1866 | return; |
| 1867 | } |
| 1868 | |
| 1869 | seq_id = (30 << CPSW_V1_SEQ_ID_OFS_SHIFT) | ETH_P_1588; |
| 1870 | ts_en = EVENT_MSG_BITS << CPSW_V1_MSG_TYPE_OFS; |
| 1871 | |
Ivan Khoronzhuk | a942312 | 2018-11-12 16:00:22 +0200 | [diff] [blame] | 1872 | if (priv->tx_ts_enabled) |
Richard Cochran | 2e5b38a | 2012-10-29 08:45:20 +0000 | [diff] [blame] | 1873 | ts_en |= CPSW_V1_TS_TX_EN; |
| 1874 | |
Ivan Khoronzhuk | a942312 | 2018-11-12 16:00:22 +0200 | [diff] [blame] | 1875 | if (priv->rx_ts_enabled) |
Richard Cochran | 2e5b38a | 2012-10-29 08:45:20 +0000 | [diff] [blame] | 1876 | ts_en |= CPSW_V1_TS_RX_EN; |
| 1877 | |
| 1878 | slave_write(slave, ts_en, CPSW1_TS_CTL); |
| 1879 | slave_write(slave, seq_id, CPSW1_TS_SEQ_LTYPE); |
| 1880 | } |
| 1881 | |
| 1882 | static void cpsw_hwtstamp_v2(struct cpsw_priv *priv) |
| 1883 | { |
Mugunthan V N | d9ba8f9 | 2013-02-11 09:52:20 +0000 | [diff] [blame] | 1884 | struct cpsw_slave *slave; |
Ivan Khoronzhuk | 5d8d0d4 | 2016-08-10 02:22:39 +0300 | [diff] [blame] | 1885 | struct cpsw_common *cpsw = priv->cpsw; |
Richard Cochran | 2e5b38a | 2012-10-29 08:45:20 +0000 | [diff] [blame] | 1886 | u32 ctrl, mtype; |
| 1887 | |
Ivan Khoronzhuk | cb7d78d0 | 2016-12-10 14:23:46 +0200 | [diff] [blame] | 1888 | slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)]; |
Mugunthan V N | d9ba8f9 | 2013-02-11 09:52:20 +0000 | [diff] [blame] | 1889 | |
Richard Cochran | 2e5b38a | 2012-10-29 08:45:20 +0000 | [diff] [blame] | 1890 | ctrl = slave_read(slave, CPSW2_CONTROL); |
Ivan Khoronzhuk | 2a05a62 | 2016-08-10 02:22:44 +0300 | [diff] [blame] | 1891 | switch (cpsw->version) { |
George Cherian | 09c5537 | 2014-05-02 12:02:02 +0530 | [diff] [blame] | 1892 | case CPSW_VERSION_2: |
| 1893 | ctrl &= ~CTRL_V2_ALL_TS_MASK; |
Richard Cochran | 2e5b38a | 2012-10-29 08:45:20 +0000 | [diff] [blame] | 1894 | |
Ivan Khoronzhuk | a942312 | 2018-11-12 16:00:22 +0200 | [diff] [blame] | 1895 | if (priv->tx_ts_enabled) |
George Cherian | 09c5537 | 2014-05-02 12:02:02 +0530 | [diff] [blame] | 1896 | ctrl |= CTRL_V2_TX_TS_BITS; |
Richard Cochran | 2e5b38a | 2012-10-29 08:45:20 +0000 | [diff] [blame] | 1897 | |
Ivan Khoronzhuk | a942312 | 2018-11-12 16:00:22 +0200 | [diff] [blame] | 1898 | if (priv->rx_ts_enabled) |
George Cherian | 09c5537 | 2014-05-02 12:02:02 +0530 | [diff] [blame] | 1899 | ctrl |= CTRL_V2_RX_TS_BITS; |
Richard Cochran | 26fe7eb | 2015-05-25 11:02:13 +0200 | [diff] [blame] | 1900 | break; |
George Cherian | 09c5537 | 2014-05-02 12:02:02 +0530 | [diff] [blame] | 1901 | case CPSW_VERSION_3: |
| 1902 | default: |
| 1903 | ctrl &= ~CTRL_V3_ALL_TS_MASK; |
| 1904 | |
Ivan Khoronzhuk | a942312 | 2018-11-12 16:00:22 +0200 | [diff] [blame] | 1905 | if (priv->tx_ts_enabled) |
George Cherian | 09c5537 | 2014-05-02 12:02:02 +0530 | [diff] [blame] | 1906 | ctrl |= CTRL_V3_TX_TS_BITS; |
| 1907 | |
Ivan Khoronzhuk | a942312 | 2018-11-12 16:00:22 +0200 | [diff] [blame] | 1908 | if (priv->rx_ts_enabled) |
George Cherian | 09c5537 | 2014-05-02 12:02:02 +0530 | [diff] [blame] | 1909 | ctrl |= CTRL_V3_RX_TS_BITS; |
Richard Cochran | 26fe7eb | 2015-05-25 11:02:13 +0200 | [diff] [blame] | 1910 | break; |
George Cherian | 09c5537 | 2014-05-02 12:02:02 +0530 | [diff] [blame] | 1911 | } |
Richard Cochran | 2e5b38a | 2012-10-29 08:45:20 +0000 | [diff] [blame] | 1912 | |
| 1913 | mtype = (30 << TS_SEQ_ID_OFFSET_SHIFT) | EVENT_MSG_BITS; |
| 1914 | |
| 1915 | slave_write(slave, mtype, CPSW2_TS_SEQ_MTYPE); |
| 1916 | slave_write(slave, ctrl, CPSW2_CONTROL); |
Grygorii Strashko | dda5f5fe | 2017-11-30 18:21:11 -0600 | [diff] [blame] | 1917 | writel_relaxed(ETH_P_1588, &cpsw->regs->ts_ltype); |
Ivan Khoronzhuk | 1ebb244 | 2018-11-12 16:00:23 +0200 | [diff] [blame] | 1918 | writel_relaxed(ETH_P_8021Q, &cpsw->regs->vlan_ltype); |
Richard Cochran | 2e5b38a | 2012-10-29 08:45:20 +0000 | [diff] [blame] | 1919 | } |
| 1920 | |
Ben Hutchings | a5b4145 | 2013-11-18 23:23:40 +0000 | [diff] [blame] | 1921 | static int cpsw_hwtstamp_set(struct net_device *dev, struct ifreq *ifr) |
Richard Cochran | 2e5b38a | 2012-10-29 08:45:20 +0000 | [diff] [blame] | 1922 | { |
Mugunthan V N | 3177bf6 | 2012-11-27 07:53:40 +0000 | [diff] [blame] | 1923 | struct cpsw_priv *priv = netdev_priv(dev); |
Richard Cochran | 2e5b38a | 2012-10-29 08:45:20 +0000 | [diff] [blame] | 1924 | struct hwtstamp_config cfg; |
Ivan Khoronzhuk | 2a05a62 | 2016-08-10 02:22:44 +0300 | [diff] [blame] | 1925 | struct cpsw_common *cpsw = priv->cpsw; |
Richard Cochran | 2e5b38a | 2012-10-29 08:45:20 +0000 | [diff] [blame] | 1926 | |
Ivan Khoronzhuk | 2a05a62 | 2016-08-10 02:22:44 +0300 | [diff] [blame] | 1927 | if (cpsw->version != CPSW_VERSION_1 && |
| 1928 | cpsw->version != CPSW_VERSION_2 && |
| 1929 | cpsw->version != CPSW_VERSION_3) |
Ben Hutchings | 2ee91e5 | 2013-11-14 00:47:36 +0000 | [diff] [blame] | 1930 | return -EOPNOTSUPP; |
| 1931 | |
Richard Cochran | 2e5b38a | 2012-10-29 08:45:20 +0000 | [diff] [blame] | 1932 | if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg))) |
| 1933 | return -EFAULT; |
| 1934 | |
| 1935 | /* reserved for future extensions */ |
| 1936 | if (cfg.flags) |
| 1937 | return -EINVAL; |
| 1938 | |
Ben Hutchings | 2ee91e5 | 2013-11-14 00:47:36 +0000 | [diff] [blame] | 1939 | if (cfg.tx_type != HWTSTAMP_TX_OFF && cfg.tx_type != HWTSTAMP_TX_ON) |
Richard Cochran | 2e5b38a | 2012-10-29 08:45:20 +0000 | [diff] [blame] | 1940 | return -ERANGE; |
Richard Cochran | 2e5b38a | 2012-10-29 08:45:20 +0000 | [diff] [blame] | 1941 | |
| 1942 | switch (cfg.rx_filter) { |
| 1943 | case HWTSTAMP_FILTER_NONE: |
Ivan Khoronzhuk | a942312 | 2018-11-12 16:00:22 +0200 | [diff] [blame] | 1944 | priv->rx_ts_enabled = 0; |
Richard Cochran | 2e5b38a | 2012-10-29 08:45:20 +0000 | [diff] [blame] | 1945 | break; |
| 1946 | case HWTSTAMP_FILTER_ALL: |
Grygorii Strashko | e9523a5 | 2017-06-08 13:51:31 -0500 | [diff] [blame] | 1947 | case HWTSTAMP_FILTER_NTP_ALL: |
| 1948 | return -ERANGE; |
Richard Cochran | 2e5b38a | 2012-10-29 08:45:20 +0000 | [diff] [blame] | 1949 | case HWTSTAMP_FILTER_PTP_V1_L4_EVENT: |
| 1950 | case HWTSTAMP_FILTER_PTP_V1_L4_SYNC: |
| 1951 | case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ: |
Ivan Khoronzhuk | a942312 | 2018-11-12 16:00:22 +0200 | [diff] [blame] | 1952 | priv->rx_ts_enabled = HWTSTAMP_FILTER_PTP_V1_L4_EVENT; |
Grygorii Strashko | e9523a5 | 2017-06-08 13:51:31 -0500 | [diff] [blame] | 1953 | cfg.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT; |
| 1954 | break; |
Richard Cochran | 2e5b38a | 2012-10-29 08:45:20 +0000 | [diff] [blame] | 1955 | case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: |
| 1956 | case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: |
| 1957 | case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: |
| 1958 | case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: |
| 1959 | case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: |
| 1960 | case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: |
| 1961 | case HWTSTAMP_FILTER_PTP_V2_EVENT: |
| 1962 | case HWTSTAMP_FILTER_PTP_V2_SYNC: |
| 1963 | case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: |
Ivan Khoronzhuk | a942312 | 2018-11-12 16:00:22 +0200 | [diff] [blame] | 1964 | priv->rx_ts_enabled = HWTSTAMP_FILTER_PTP_V2_EVENT; |
Richard Cochran | 2e5b38a | 2012-10-29 08:45:20 +0000 | [diff] [blame] | 1965 | cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT; |
| 1966 | break; |
| 1967 | default: |
| 1968 | return -ERANGE; |
| 1969 | } |
| 1970 | |
Ivan Khoronzhuk | a942312 | 2018-11-12 16:00:22 +0200 | [diff] [blame] | 1971 | priv->tx_ts_enabled = cfg.tx_type == HWTSTAMP_TX_ON; |
Ben Hutchings | 2ee91e5 | 2013-11-14 00:47:36 +0000 | [diff] [blame] | 1972 | |
Ivan Khoronzhuk | 2a05a62 | 2016-08-10 02:22:44 +0300 | [diff] [blame] | 1973 | switch (cpsw->version) { |
Richard Cochran | 2e5b38a | 2012-10-29 08:45:20 +0000 | [diff] [blame] | 1974 | case CPSW_VERSION_1: |
Ivan Khoronzhuk | a942312 | 2018-11-12 16:00:22 +0200 | [diff] [blame] | 1975 | cpsw_hwtstamp_v1(priv); |
Richard Cochran | 2e5b38a | 2012-10-29 08:45:20 +0000 | [diff] [blame] | 1976 | break; |
| 1977 | case CPSW_VERSION_2: |
George Cherian | f7d403c | 2014-05-02 12:02:01 +0530 | [diff] [blame] | 1978 | case CPSW_VERSION_3: |
Richard Cochran | 2e5b38a | 2012-10-29 08:45:20 +0000 | [diff] [blame] | 1979 | cpsw_hwtstamp_v2(priv); |
| 1980 | break; |
| 1981 | default: |
Ben Hutchings | 2ee91e5 | 2013-11-14 00:47:36 +0000 | [diff] [blame] | 1982 | WARN_ON(1); |
Richard Cochran | 2e5b38a | 2012-10-29 08:45:20 +0000 | [diff] [blame] | 1983 | } |
| 1984 | |
| 1985 | return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0; |
| 1986 | } |
| 1987 | |
Ben Hutchings | a5b4145 | 2013-11-18 23:23:40 +0000 | [diff] [blame] | 1988 | static int cpsw_hwtstamp_get(struct net_device *dev, struct ifreq *ifr) |
| 1989 | { |
Ivan Khoronzhuk | 2a05a62 | 2016-08-10 02:22:44 +0300 | [diff] [blame] | 1990 | struct cpsw_common *cpsw = ndev_to_cpsw(dev); |
Ivan Khoronzhuk | a942312 | 2018-11-12 16:00:22 +0200 | [diff] [blame] | 1991 | struct cpsw_priv *priv = netdev_priv(dev); |
Ben Hutchings | a5b4145 | 2013-11-18 23:23:40 +0000 | [diff] [blame] | 1992 | struct hwtstamp_config cfg; |
| 1993 | |
Ivan Khoronzhuk | 2a05a62 | 2016-08-10 02:22:44 +0300 | [diff] [blame] | 1994 | if (cpsw->version != CPSW_VERSION_1 && |
| 1995 | cpsw->version != CPSW_VERSION_2 && |
| 1996 | cpsw->version != CPSW_VERSION_3) |
Ben Hutchings | a5b4145 | 2013-11-18 23:23:40 +0000 | [diff] [blame] | 1997 | return -EOPNOTSUPP; |
| 1998 | |
| 1999 | cfg.flags = 0; |
Ivan Khoronzhuk | a942312 | 2018-11-12 16:00:22 +0200 | [diff] [blame] | 2000 | cfg.tx_type = priv->tx_ts_enabled ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF; |
| 2001 | cfg.rx_filter = priv->rx_ts_enabled; |
Ben Hutchings | a5b4145 | 2013-11-18 23:23:40 +0000 | [diff] [blame] | 2002 | |
| 2003 | return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0; |
| 2004 | } |
Grygorii Strashko | c8395d4 | 2016-12-06 18:00:34 -0600 | [diff] [blame] | 2005 | #else |
| 2006 | static int cpsw_hwtstamp_get(struct net_device *dev, struct ifreq *ifr) |
| 2007 | { |
| 2008 | return -EOPNOTSUPP; |
| 2009 | } |
Ben Hutchings | a5b4145 | 2013-11-18 23:23:40 +0000 | [diff] [blame] | 2010 | |
Grygorii Strashko | c8395d4 | 2016-12-06 18:00:34 -0600 | [diff] [blame] | 2011 | static int cpsw_hwtstamp_set(struct net_device *dev, struct ifreq *ifr) |
| 2012 | { |
| 2013 | return -EOPNOTSUPP; |
| 2014 | } |
Richard Cochran | 2e5b38a | 2012-10-29 08:45:20 +0000 | [diff] [blame] | 2015 | #endif /*CONFIG_TI_CPTS*/ |
| 2016 | |
| 2017 | static int cpsw_ndo_ioctl(struct net_device *dev, struct ifreq *req, int cmd) |
| 2018 | { |
Mugunthan V N | 11f2c98 | 2013-03-11 23:16:38 +0000 | [diff] [blame] | 2019 | struct cpsw_priv *priv = netdev_priv(dev); |
Ivan Khoronzhuk | 606f399 | 2016-08-10 02:22:42 +0300 | [diff] [blame] | 2020 | struct cpsw_common *cpsw = priv->cpsw; |
| 2021 | int slave_no = cpsw_slave_index(cpsw, priv); |
Mugunthan V N | 11f2c98 | 2013-03-11 23:16:38 +0000 | [diff] [blame] | 2022 | |
Richard Cochran | 2e5b38a | 2012-10-29 08:45:20 +0000 | [diff] [blame] | 2023 | if (!netif_running(dev)) |
| 2024 | return -EINVAL; |
| 2025 | |
Mugunthan V N | 11f2c98 | 2013-03-11 23:16:38 +0000 | [diff] [blame] | 2026 | switch (cmd) { |
Mugunthan V N | 11f2c98 | 2013-03-11 23:16:38 +0000 | [diff] [blame] | 2027 | case SIOCSHWTSTAMP: |
Ben Hutchings | a5b4145 | 2013-11-18 23:23:40 +0000 | [diff] [blame] | 2028 | return cpsw_hwtstamp_set(dev, req); |
| 2029 | case SIOCGHWTSTAMP: |
| 2030 | return cpsw_hwtstamp_get(dev, req); |
Mugunthan V N | 11f2c98 | 2013-03-11 23:16:38 +0000 | [diff] [blame] | 2031 | } |
| 2032 | |
Ivan Khoronzhuk | 606f399 | 2016-08-10 02:22:42 +0300 | [diff] [blame] | 2033 | if (!cpsw->slaves[slave_no].phy) |
Stefan Sørensen | c1b5994 | 2014-02-16 14:54:25 +0100 | [diff] [blame] | 2034 | return -EOPNOTSUPP; |
Ivan Khoronzhuk | 606f399 | 2016-08-10 02:22:42 +0300 | [diff] [blame] | 2035 | return phy_mii_ioctl(cpsw->slaves[slave_no].phy, req, cmd); |
Richard Cochran | 2e5b38a | 2012-10-29 08:45:20 +0000 | [diff] [blame] | 2036 | } |
| 2037 | |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 2038 | static void cpsw_ndo_tx_timeout(struct net_device *ndev) |
| 2039 | { |
| 2040 | struct cpsw_priv *priv = netdev_priv(ndev); |
Ivan Khoronzhuk | 2c836bd | 2016-08-10 02:22:40 +0300 | [diff] [blame] | 2041 | struct cpsw_common *cpsw = priv->cpsw; |
Ivan Khoronzhuk | e05107e | 2016-08-22 21:18:26 +0300 | [diff] [blame] | 2042 | int ch; |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 2043 | |
| 2044 | cpsw_err(priv, tx_err, "transmit timeout, restarting dma\n"); |
Tobias Klauser | 8dc43dd | 2014-03-10 13:12:23 +0100 | [diff] [blame] | 2045 | ndev->stats.tx_errors++; |
Ivan Khoronzhuk | 2c836bd | 2016-08-10 02:22:40 +0300 | [diff] [blame] | 2046 | cpsw_intr_disable(cpsw); |
Ivan Khoronzhuk | e05107e | 2016-08-22 21:18:26 +0300 | [diff] [blame] | 2047 | for (ch = 0; ch < cpsw->tx_ch_num; ch++) { |
Ivan Khoronzhuk | 8feb0a1 | 2016-11-29 17:00:51 +0200 | [diff] [blame] | 2048 | cpdma_chan_stop(cpsw->txv[ch].ch); |
| 2049 | cpdma_chan_start(cpsw->txv[ch].ch); |
Ivan Khoronzhuk | e05107e | 2016-08-22 21:18:26 +0300 | [diff] [blame] | 2050 | } |
| 2051 | |
Ivan Khoronzhuk | 2c836bd | 2016-08-10 02:22:40 +0300 | [diff] [blame] | 2052 | cpsw_intr_enable(cpsw); |
Grygorii Strashko | 75514b6 | 2017-03-31 18:41:23 -0500 | [diff] [blame] | 2053 | netif_trans_update(ndev); |
| 2054 | netif_tx_wake_all_queues(ndev); |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 2055 | } |
| 2056 | |
Mugunthan V N | dcfd8d5 | 2013-07-25 23:44:01 +0530 | [diff] [blame] | 2057 | static int cpsw_ndo_set_mac_address(struct net_device *ndev, void *p) |
| 2058 | { |
| 2059 | struct cpsw_priv *priv = netdev_priv(ndev); |
| 2060 | struct sockaddr *addr = (struct sockaddr *)p; |
Ivan Khoronzhuk | 649a168 | 2016-08-10 02:22:37 +0300 | [diff] [blame] | 2061 | struct cpsw_common *cpsw = priv->cpsw; |
Mugunthan V N | dcfd8d5 | 2013-07-25 23:44:01 +0530 | [diff] [blame] | 2062 | int flags = 0; |
| 2063 | u16 vid = 0; |
Grygorii Strashko | a6c5d14 | 2016-06-24 21:23:45 +0300 | [diff] [blame] | 2064 | int ret; |
Mugunthan V N | dcfd8d5 | 2013-07-25 23:44:01 +0530 | [diff] [blame] | 2065 | |
| 2066 | if (!is_valid_ether_addr(addr->sa_data)) |
| 2067 | return -EADDRNOTAVAIL; |
| 2068 | |
Ivan Khoronzhuk | 56e31bd | 2016-08-10 02:22:38 +0300 | [diff] [blame] | 2069 | ret = pm_runtime_get_sync(cpsw->dev); |
Grygorii Strashko | a6c5d14 | 2016-06-24 21:23:45 +0300 | [diff] [blame] | 2070 | if (ret < 0) { |
Ivan Khoronzhuk | 56e31bd | 2016-08-10 02:22:38 +0300 | [diff] [blame] | 2071 | pm_runtime_put_noidle(cpsw->dev); |
Grygorii Strashko | a6c5d14 | 2016-06-24 21:23:45 +0300 | [diff] [blame] | 2072 | return ret; |
| 2073 | } |
| 2074 | |
Ivan Khoronzhuk | 606f399 | 2016-08-10 02:22:42 +0300 | [diff] [blame] | 2075 | if (cpsw->data.dual_emac) { |
| 2076 | vid = cpsw->slaves[priv->emac_port].port_vlan; |
Mugunthan V N | dcfd8d5 | 2013-07-25 23:44:01 +0530 | [diff] [blame] | 2077 | flags = ALE_VLAN; |
| 2078 | } |
| 2079 | |
Ivan Khoronzhuk | 2a05a62 | 2016-08-10 02:22:44 +0300 | [diff] [blame] | 2080 | cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM, |
Mugunthan V N | dcfd8d5 | 2013-07-25 23:44:01 +0530 | [diff] [blame] | 2081 | flags, vid); |
Ivan Khoronzhuk | 2a05a62 | 2016-08-10 02:22:44 +0300 | [diff] [blame] | 2082 | cpsw_ale_add_ucast(cpsw->ale, addr->sa_data, HOST_PORT_NUM, |
Mugunthan V N | dcfd8d5 | 2013-07-25 23:44:01 +0530 | [diff] [blame] | 2083 | flags, vid); |
| 2084 | |
| 2085 | memcpy(priv->mac_addr, addr->sa_data, ETH_ALEN); |
| 2086 | memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN); |
| 2087 | for_each_slave(priv, cpsw_set_slave_mac, priv); |
| 2088 | |
Ivan Khoronzhuk | 56e31bd | 2016-08-10 02:22:38 +0300 | [diff] [blame] | 2089 | pm_runtime_put(cpsw->dev); |
Grygorii Strashko | a6c5d14 | 2016-06-24 21:23:45 +0300 | [diff] [blame] | 2090 | |
Mugunthan V N | dcfd8d5 | 2013-07-25 23:44:01 +0530 | [diff] [blame] | 2091 | return 0; |
| 2092 | } |
| 2093 | |
Mugunthan V N | 3b72c2f | 2013-02-05 08:26:48 +0000 | [diff] [blame] | 2094 | static inline int cpsw_add_vlan_ale_entry(struct cpsw_priv *priv, |
| 2095 | unsigned short vid) |
| 2096 | { |
| 2097 | int ret; |
Mugunthan V N | 9f6bd8f | 2015-01-15 14:59:28 +0530 | [diff] [blame] | 2098 | int unreg_mcast_mask = 0; |
Ivan Khoronzhuk | 5b3a5a1 | 2018-10-12 19:06:29 +0300 | [diff] [blame] | 2099 | int mcast_mask; |
Mugunthan V N | 9f6bd8f | 2015-01-15 14:59:28 +0530 | [diff] [blame] | 2100 | u32 port_mask; |
Ivan Khoronzhuk | 606f399 | 2016-08-10 02:22:42 +0300 | [diff] [blame] | 2101 | struct cpsw_common *cpsw = priv->cpsw; |
Lennart Sorensen | 1e5c4bc | 2014-10-31 13:38:52 -0400 | [diff] [blame] | 2102 | |
Ivan Khoronzhuk | 606f399 | 2016-08-10 02:22:42 +0300 | [diff] [blame] | 2103 | if (cpsw->data.dual_emac) { |
Mugunthan V N | 9f6bd8f | 2015-01-15 14:59:28 +0530 | [diff] [blame] | 2104 | port_mask = (1 << (priv->emac_port + 1)) | ALE_PORT_HOST; |
Mugunthan V N | 3b72c2f | 2013-02-05 08:26:48 +0000 | [diff] [blame] | 2105 | |
Ivan Khoronzhuk | 5b3a5a1 | 2018-10-12 19:06:29 +0300 | [diff] [blame] | 2106 | mcast_mask = ALE_PORT_HOST; |
Mugunthan V N | 9f6bd8f | 2015-01-15 14:59:28 +0530 | [diff] [blame] | 2107 | if (priv->ndev->flags & IFF_ALLMULTI) |
Ivan Khoronzhuk | 5b3a5a1 | 2018-10-12 19:06:29 +0300 | [diff] [blame] | 2108 | unreg_mcast_mask = mcast_mask; |
Mugunthan V N | 9f6bd8f | 2015-01-15 14:59:28 +0530 | [diff] [blame] | 2109 | } else { |
| 2110 | port_mask = ALE_ALL_PORTS; |
Ivan Khoronzhuk | 5b3a5a1 | 2018-10-12 19:06:29 +0300 | [diff] [blame] | 2111 | mcast_mask = port_mask; |
Mugunthan V N | 9f6bd8f | 2015-01-15 14:59:28 +0530 | [diff] [blame] | 2112 | |
| 2113 | if (priv->ndev->flags & IFF_ALLMULTI) |
| 2114 | unreg_mcast_mask = ALE_ALL_PORTS; |
| 2115 | else |
| 2116 | unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2; |
| 2117 | } |
| 2118 | |
Ivan Khoronzhuk | 2a05a62 | 2016-08-10 02:22:44 +0300 | [diff] [blame] | 2119 | ret = cpsw_ale_add_vlan(cpsw->ale, vid, port_mask, 0, port_mask, |
Grygorii Strashko | 61f1cef | 2016-04-07 15:16:43 +0300 | [diff] [blame] | 2120 | unreg_mcast_mask); |
Mugunthan V N | 3b72c2f | 2013-02-05 08:26:48 +0000 | [diff] [blame] | 2121 | if (ret != 0) |
| 2122 | return ret; |
| 2123 | |
Ivan Khoronzhuk | 2a05a62 | 2016-08-10 02:22:44 +0300 | [diff] [blame] | 2124 | ret = cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr, |
Grygorii Strashko | 71a2cbb | 2016-04-07 15:16:44 +0300 | [diff] [blame] | 2125 | HOST_PORT_NUM, ALE_VLAN, vid); |
Mugunthan V N | 3b72c2f | 2013-02-05 08:26:48 +0000 | [diff] [blame] | 2126 | if (ret != 0) |
| 2127 | goto clean_vid; |
| 2128 | |
Ivan Khoronzhuk | 2a05a62 | 2016-08-10 02:22:44 +0300 | [diff] [blame] | 2129 | ret = cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast, |
Ivan Khoronzhuk | 5b3a5a1 | 2018-10-12 19:06:29 +0300 | [diff] [blame] | 2130 | mcast_mask, ALE_VLAN, vid, 0); |
Mugunthan V N | 3b72c2f | 2013-02-05 08:26:48 +0000 | [diff] [blame] | 2131 | if (ret != 0) |
| 2132 | goto clean_vlan_ucast; |
| 2133 | return 0; |
| 2134 | |
| 2135 | clean_vlan_ucast: |
Ivan Khoronzhuk | 2a05a62 | 2016-08-10 02:22:44 +0300 | [diff] [blame] | 2136 | cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr, |
Grygorii Strashko | 71a2cbb | 2016-04-07 15:16:44 +0300 | [diff] [blame] | 2137 | HOST_PORT_NUM, ALE_VLAN, vid); |
Mugunthan V N | 3b72c2f | 2013-02-05 08:26:48 +0000 | [diff] [blame] | 2138 | clean_vid: |
Ivan Khoronzhuk | 2a05a62 | 2016-08-10 02:22:44 +0300 | [diff] [blame] | 2139 | cpsw_ale_del_vlan(cpsw->ale, vid, 0); |
Mugunthan V N | 3b72c2f | 2013-02-05 08:26:48 +0000 | [diff] [blame] | 2140 | return ret; |
| 2141 | } |
| 2142 | |
| 2143 | static int cpsw_ndo_vlan_rx_add_vid(struct net_device *ndev, |
Patrick McHardy | 80d5c36 | 2013-04-19 02:04:28 +0000 | [diff] [blame] | 2144 | __be16 proto, u16 vid) |
Mugunthan V N | 3b72c2f | 2013-02-05 08:26:48 +0000 | [diff] [blame] | 2145 | { |
| 2146 | struct cpsw_priv *priv = netdev_priv(ndev); |
Ivan Khoronzhuk | 649a168 | 2016-08-10 02:22:37 +0300 | [diff] [blame] | 2147 | struct cpsw_common *cpsw = priv->cpsw; |
Grygorii Strashko | a6c5d14 | 2016-06-24 21:23:45 +0300 | [diff] [blame] | 2148 | int ret; |
Mugunthan V N | 3b72c2f | 2013-02-05 08:26:48 +0000 | [diff] [blame] | 2149 | |
Ivan Khoronzhuk | 606f399 | 2016-08-10 02:22:42 +0300 | [diff] [blame] | 2150 | if (vid == cpsw->data.default_vlan) |
Mugunthan V N | 3b72c2f | 2013-02-05 08:26:48 +0000 | [diff] [blame] | 2151 | return 0; |
| 2152 | |
Ivan Khoronzhuk | 56e31bd | 2016-08-10 02:22:38 +0300 | [diff] [blame] | 2153 | ret = pm_runtime_get_sync(cpsw->dev); |
Grygorii Strashko | a6c5d14 | 2016-06-24 21:23:45 +0300 | [diff] [blame] | 2154 | if (ret < 0) { |
Ivan Khoronzhuk | 56e31bd | 2016-08-10 02:22:38 +0300 | [diff] [blame] | 2155 | pm_runtime_put_noidle(cpsw->dev); |
Grygorii Strashko | a6c5d14 | 2016-06-24 21:23:45 +0300 | [diff] [blame] | 2156 | return ret; |
| 2157 | } |
| 2158 | |
Ivan Khoronzhuk | 606f399 | 2016-08-10 02:22:42 +0300 | [diff] [blame] | 2159 | if (cpsw->data.dual_emac) { |
Mugunthan V N | 02a5416 | 2015-01-22 15:19:22 +0530 | [diff] [blame] | 2160 | /* In dual EMAC, reserved VLAN id should not be used for |
| 2161 | * creating VLAN interfaces as this can break the dual |
| 2162 | * EMAC port separation |
| 2163 | */ |
| 2164 | int i; |
| 2165 | |
Ivan Khoronzhuk | 606f399 | 2016-08-10 02:22:42 +0300 | [diff] [blame] | 2166 | for (i = 0; i < cpsw->data.slaves; i++) { |
Ivan Khoronzhuk | 803c4f6 | 2018-08-10 15:47:09 +0300 | [diff] [blame] | 2167 | if (vid == cpsw->slaves[i].port_vlan) { |
| 2168 | ret = -EINVAL; |
| 2169 | goto err; |
| 2170 | } |
Mugunthan V N | 02a5416 | 2015-01-22 15:19:22 +0530 | [diff] [blame] | 2171 | } |
| 2172 | } |
| 2173 | |
Mugunthan V N | 3b72c2f | 2013-02-05 08:26:48 +0000 | [diff] [blame] | 2174 | dev_info(priv->dev, "Adding vlanid %d to vlan filter\n", vid); |
Grygorii Strashko | a6c5d14 | 2016-06-24 21:23:45 +0300 | [diff] [blame] | 2175 | ret = cpsw_add_vlan_ale_entry(priv, vid); |
Ivan Khoronzhuk | 803c4f6 | 2018-08-10 15:47:09 +0300 | [diff] [blame] | 2176 | err: |
Ivan Khoronzhuk | 56e31bd | 2016-08-10 02:22:38 +0300 | [diff] [blame] | 2177 | pm_runtime_put(cpsw->dev); |
Grygorii Strashko | a6c5d14 | 2016-06-24 21:23:45 +0300 | [diff] [blame] | 2178 | return ret; |
Mugunthan V N | 3b72c2f | 2013-02-05 08:26:48 +0000 | [diff] [blame] | 2179 | } |
| 2180 | |
| 2181 | static int cpsw_ndo_vlan_rx_kill_vid(struct net_device *ndev, |
Patrick McHardy | 80d5c36 | 2013-04-19 02:04:28 +0000 | [diff] [blame] | 2182 | __be16 proto, u16 vid) |
Mugunthan V N | 3b72c2f | 2013-02-05 08:26:48 +0000 | [diff] [blame] | 2183 | { |
| 2184 | struct cpsw_priv *priv = netdev_priv(ndev); |
Ivan Khoronzhuk | 649a168 | 2016-08-10 02:22:37 +0300 | [diff] [blame] | 2185 | struct cpsw_common *cpsw = priv->cpsw; |
Mugunthan V N | 3b72c2f | 2013-02-05 08:26:48 +0000 | [diff] [blame] | 2186 | int ret; |
| 2187 | |
Ivan Khoronzhuk | 606f399 | 2016-08-10 02:22:42 +0300 | [diff] [blame] | 2188 | if (vid == cpsw->data.default_vlan) |
Mugunthan V N | 3b72c2f | 2013-02-05 08:26:48 +0000 | [diff] [blame] | 2189 | return 0; |
| 2190 | |
Ivan Khoronzhuk | 56e31bd | 2016-08-10 02:22:38 +0300 | [diff] [blame] | 2191 | ret = pm_runtime_get_sync(cpsw->dev); |
Grygorii Strashko | a6c5d14 | 2016-06-24 21:23:45 +0300 | [diff] [blame] | 2192 | if (ret < 0) { |
Ivan Khoronzhuk | 56e31bd | 2016-08-10 02:22:38 +0300 | [diff] [blame] | 2193 | pm_runtime_put_noidle(cpsw->dev); |
Grygorii Strashko | a6c5d14 | 2016-06-24 21:23:45 +0300 | [diff] [blame] | 2194 | return ret; |
| 2195 | } |
| 2196 | |
Ivan Khoronzhuk | 606f399 | 2016-08-10 02:22:42 +0300 | [diff] [blame] | 2197 | if (cpsw->data.dual_emac) { |
Mugunthan V N | 02a5416 | 2015-01-22 15:19:22 +0530 | [diff] [blame] | 2198 | int i; |
| 2199 | |
Ivan Khoronzhuk | 606f399 | 2016-08-10 02:22:42 +0300 | [diff] [blame] | 2200 | for (i = 0; i < cpsw->data.slaves; i++) { |
| 2201 | if (vid == cpsw->slaves[i].port_vlan) |
Ivan Khoronzhuk | 803c4f6 | 2018-08-10 15:47:09 +0300 | [diff] [blame] | 2202 | goto err; |
Mugunthan V N | 02a5416 | 2015-01-22 15:19:22 +0530 | [diff] [blame] | 2203 | } |
| 2204 | } |
| 2205 | |
Mugunthan V N | 3b72c2f | 2013-02-05 08:26:48 +0000 | [diff] [blame] | 2206 | dev_info(priv->dev, "removing vlanid %d from vlan filter\n", vid); |
Ivan Khoronzhuk | 2a05a62 | 2016-08-10 02:22:44 +0300 | [diff] [blame] | 2207 | ret = cpsw_ale_del_vlan(cpsw->ale, vid, 0); |
Ivan Khoronzhuk | be35b98 | 2018-08-10 15:47:08 +0300 | [diff] [blame] | 2208 | ret |= cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr, |
| 2209 | HOST_PORT_NUM, ALE_VLAN, vid); |
| 2210 | ret |= cpsw_ale_del_mcast(cpsw->ale, priv->ndev->broadcast, |
| 2211 | 0, ALE_VLAN, vid); |
Ivan Khoronzhuk | 15180ec | 2018-11-08 22:27:56 +0200 | [diff] [blame] | 2212 | ret |= cpsw_ale_flush_multicast(cpsw->ale, 0, vid); |
Ivan Khoronzhuk | 803c4f6 | 2018-08-10 15:47:09 +0300 | [diff] [blame] | 2213 | err: |
Ivan Khoronzhuk | 56e31bd | 2016-08-10 02:22:38 +0300 | [diff] [blame] | 2214 | pm_runtime_put(cpsw->dev); |
Grygorii Strashko | a6c5d14 | 2016-06-24 21:23:45 +0300 | [diff] [blame] | 2215 | return ret; |
Mugunthan V N | 3b72c2f | 2013-02-05 08:26:48 +0000 | [diff] [blame] | 2216 | } |
| 2217 | |
Ivan Khoronzhuk | 83fcad0 | 2016-11-29 17:00:49 +0200 | [diff] [blame] | 2218 | static int cpsw_ndo_set_tx_maxrate(struct net_device *ndev, int queue, u32 rate) |
| 2219 | { |
| 2220 | struct cpsw_priv *priv = netdev_priv(ndev); |
Ivan Khoronzhuk | 83fcad0 | 2016-11-29 17:00:49 +0200 | [diff] [blame] | 2221 | struct cpsw_common *cpsw = priv->cpsw; |
Ivan Khoronzhuk | 52986a2 | 2016-12-10 14:23:50 +0200 | [diff] [blame] | 2222 | struct cpsw_slave *slave; |
Ivan Khoronzhuk | 32b78d8 | 2016-12-10 14:23:48 +0200 | [diff] [blame] | 2223 | u32 min_rate; |
Ivan Khoronzhuk | 83fcad0 | 2016-11-29 17:00:49 +0200 | [diff] [blame] | 2224 | u32 ch_rate; |
Ivan Khoronzhuk | 52986a2 | 2016-12-10 14:23:50 +0200 | [diff] [blame] | 2225 | int i, ret; |
Ivan Khoronzhuk | 83fcad0 | 2016-11-29 17:00:49 +0200 | [diff] [blame] | 2226 | |
| 2227 | ch_rate = netdev_get_tx_queue(ndev, queue)->tx_maxrate; |
| 2228 | if (ch_rate == rate) |
| 2229 | return 0; |
| 2230 | |
Ivan Khoronzhuk | 32b78d8 | 2016-12-10 14:23:48 +0200 | [diff] [blame] | 2231 | ch_rate = rate * 1000; |
| 2232 | min_rate = cpdma_chan_get_min_rate(cpsw->dma); |
| 2233 | if ((ch_rate < min_rate && ch_rate)) { |
| 2234 | dev_err(priv->dev, "The channel rate cannot be less than %dMbps", |
| 2235 | min_rate); |
Ivan Khoronzhuk | 83fcad0 | 2016-11-29 17:00:49 +0200 | [diff] [blame] | 2236 | return -EINVAL; |
| 2237 | } |
| 2238 | |
Ivan Khoronzhuk | 0be01b8 | 2016-12-10 14:23:49 +0200 | [diff] [blame] | 2239 | if (rate > cpsw->speed) { |
Ivan Khoronzhuk | 32b78d8 | 2016-12-10 14:23:48 +0200 | [diff] [blame] | 2240 | dev_err(priv->dev, "The channel rate cannot be more than 2Gbps"); |
Ivan Khoronzhuk | 83fcad0 | 2016-11-29 17:00:49 +0200 | [diff] [blame] | 2241 | return -EINVAL; |
| 2242 | } |
| 2243 | |
| 2244 | ret = pm_runtime_get_sync(cpsw->dev); |
| 2245 | if (ret < 0) { |
| 2246 | pm_runtime_put_noidle(cpsw->dev); |
| 2247 | return ret; |
| 2248 | } |
| 2249 | |
Ivan Khoronzhuk | 32b78d8 | 2016-12-10 14:23:48 +0200 | [diff] [blame] | 2250 | ret = cpdma_chan_set_rate(cpsw->txv[queue].ch, ch_rate); |
Ivan Khoronzhuk | 83fcad0 | 2016-11-29 17:00:49 +0200 | [diff] [blame] | 2251 | pm_runtime_put(cpsw->dev); |
Ivan Khoronzhuk | 32b78d8 | 2016-12-10 14:23:48 +0200 | [diff] [blame] | 2252 | |
| 2253 | if (ret) |
| 2254 | return ret; |
| 2255 | |
Ivan Khoronzhuk | 52986a2 | 2016-12-10 14:23:50 +0200 | [diff] [blame] | 2256 | /* update rates for slaves tx queues */ |
| 2257 | for (i = 0; i < cpsw->data.slaves; i++) { |
| 2258 | slave = &cpsw->slaves[i]; |
| 2259 | if (!slave->ndev) |
| 2260 | continue; |
| 2261 | |
| 2262 | netdev_get_tx_queue(slave->ndev, queue)->tx_maxrate = rate; |
| 2263 | } |
| 2264 | |
Grygorii Strashko | 9763a89 | 2019-04-26 20:12:26 +0300 | [diff] [blame] | 2265 | cpsw_split_res(cpsw); |
Ivan Khoronzhuk | 83fcad0 | 2016-11-29 17:00:49 +0200 | [diff] [blame] | 2266 | return ret; |
| 2267 | } |
| 2268 | |
Ivan Khoronzhuk | 7929a66 | 2018-07-24 00:26:31 +0300 | [diff] [blame] | 2269 | static int cpsw_set_mqprio(struct net_device *ndev, void *type_data) |
| 2270 | { |
| 2271 | struct tc_mqprio_qopt_offload *mqprio = type_data; |
| 2272 | struct cpsw_priv *priv = netdev_priv(ndev); |
| 2273 | struct cpsw_common *cpsw = priv->cpsw; |
| 2274 | int fifo, num_tc, count, offset; |
| 2275 | struct cpsw_slave *slave; |
| 2276 | u32 tx_prio_map = 0; |
| 2277 | int i, tc, ret; |
| 2278 | |
| 2279 | num_tc = mqprio->qopt.num_tc; |
| 2280 | if (num_tc > CPSW_TC_NUM) |
| 2281 | return -EINVAL; |
| 2282 | |
| 2283 | if (mqprio->mode != TC_MQPRIO_MODE_DCB) |
| 2284 | return -EINVAL; |
| 2285 | |
| 2286 | ret = pm_runtime_get_sync(cpsw->dev); |
| 2287 | if (ret < 0) { |
| 2288 | pm_runtime_put_noidle(cpsw->dev); |
| 2289 | return ret; |
| 2290 | } |
| 2291 | |
| 2292 | if (num_tc) { |
| 2293 | for (i = 0; i < 8; i++) { |
| 2294 | tc = mqprio->qopt.prio_tc_map[i]; |
| 2295 | fifo = cpsw_tc_to_fifo(tc, num_tc); |
| 2296 | tx_prio_map |= fifo << (4 * i); |
| 2297 | } |
| 2298 | |
| 2299 | netdev_set_num_tc(ndev, num_tc); |
| 2300 | for (i = 0; i < num_tc; i++) { |
| 2301 | count = mqprio->qopt.count[i]; |
| 2302 | offset = mqprio->qopt.offset[i]; |
| 2303 | netdev_set_tc_queue(ndev, i, count, offset); |
| 2304 | } |
| 2305 | } |
| 2306 | |
| 2307 | if (!mqprio->qopt.hw) { |
| 2308 | /* restore default configuration */ |
| 2309 | netdev_reset_tc(ndev); |
| 2310 | tx_prio_map = TX_PRIORITY_MAPPING; |
| 2311 | } |
| 2312 | |
| 2313 | priv->mqprio_hw = mqprio->qopt.hw; |
| 2314 | |
| 2315 | offset = cpsw->version == CPSW_VERSION_1 ? |
| 2316 | CPSW1_TX_PRI_MAP : CPSW2_TX_PRI_MAP; |
| 2317 | |
| 2318 | slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)]; |
| 2319 | slave_write(slave, tx_prio_map, offset); |
| 2320 | |
| 2321 | pm_runtime_put_sync(cpsw->dev); |
| 2322 | |
| 2323 | return 0; |
| 2324 | } |
| 2325 | |
| 2326 | static int cpsw_ndo_setup_tc(struct net_device *ndev, enum tc_setup_type type, |
| 2327 | void *type_data) |
| 2328 | { |
| 2329 | switch (type) { |
Ivan Khoronzhuk | 57d9014 | 2018-07-24 00:26:32 +0300 | [diff] [blame] | 2330 | case TC_SETUP_QDISC_CBS: |
| 2331 | return cpsw_set_cbs(ndev, type_data); |
| 2332 | |
Ivan Khoronzhuk | 7929a66 | 2018-07-24 00:26:31 +0300 | [diff] [blame] | 2333 | case TC_SETUP_QDISC_MQPRIO: |
| 2334 | return cpsw_set_mqprio(ndev, type_data); |
| 2335 | |
| 2336 | default: |
| 2337 | return -EOPNOTSUPP; |
| 2338 | } |
| 2339 | } |
| 2340 | |
Ivan Khoronzhuk | 9ed4050 | 2019-07-09 00:34:32 +0300 | [diff] [blame] | 2341 | static int cpsw_xdp_prog_setup(struct cpsw_priv *priv, struct netdev_bpf *bpf) |
| 2342 | { |
| 2343 | struct bpf_prog *prog = bpf->prog; |
| 2344 | |
| 2345 | if (!priv->xdpi.prog && !prog) |
| 2346 | return 0; |
| 2347 | |
| 2348 | if (!xdp_attachment_flags_ok(&priv->xdpi, bpf)) |
| 2349 | return -EBUSY; |
| 2350 | |
| 2351 | WRITE_ONCE(priv->xdp_prog, prog); |
| 2352 | |
| 2353 | xdp_attachment_setup(&priv->xdpi, bpf); |
| 2354 | |
| 2355 | return 0; |
| 2356 | } |
| 2357 | |
| 2358 | static int cpsw_ndo_bpf(struct net_device *ndev, struct netdev_bpf *bpf) |
| 2359 | { |
| 2360 | struct cpsw_priv *priv = netdev_priv(ndev); |
| 2361 | |
| 2362 | switch (bpf->command) { |
| 2363 | case XDP_SETUP_PROG: |
| 2364 | return cpsw_xdp_prog_setup(priv, bpf); |
| 2365 | |
| 2366 | case XDP_QUERY_PROG: |
| 2367 | return xdp_attachment_query(&priv->xdpi, bpf); |
| 2368 | |
| 2369 | default: |
| 2370 | return -EINVAL; |
| 2371 | } |
| 2372 | } |
| 2373 | |
| 2374 | static int cpsw_ndo_xdp_xmit(struct net_device *ndev, int n, |
| 2375 | struct xdp_frame **frames, u32 flags) |
| 2376 | { |
| 2377 | struct cpsw_priv *priv = netdev_priv(ndev); |
| 2378 | struct xdp_frame *xdpf; |
| 2379 | int i, drops = 0; |
| 2380 | |
| 2381 | if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) |
| 2382 | return -EINVAL; |
| 2383 | |
| 2384 | for (i = 0; i < n; i++) { |
| 2385 | xdpf = frames[i]; |
| 2386 | if (xdpf->len < CPSW_MIN_PACKET_SIZE) { |
| 2387 | xdp_return_frame_rx_napi(xdpf); |
| 2388 | drops++; |
| 2389 | continue; |
| 2390 | } |
| 2391 | |
| 2392 | if (cpsw_xdp_tx_frame(priv, xdpf, NULL)) |
| 2393 | drops++; |
| 2394 | } |
| 2395 | |
| 2396 | return n - drops; |
| 2397 | } |
| 2398 | |
David S. Miller | 026cc9c | 2019-04-27 20:08:25 -0400 | [diff] [blame] | 2399 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 2400 | static void cpsw_ndo_poll_controller(struct net_device *ndev) |
| 2401 | { |
| 2402 | struct cpsw_common *cpsw = ndev_to_cpsw(ndev); |
| 2403 | |
| 2404 | cpsw_intr_disable(cpsw); |
| 2405 | cpsw_rx_interrupt(cpsw->irqs_table[0], cpsw); |
| 2406 | cpsw_tx_interrupt(cpsw->irqs_table[1], cpsw); |
| 2407 | cpsw_intr_enable(cpsw); |
| 2408 | } |
| 2409 | #endif |
| 2410 | |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 2411 | static const struct net_device_ops cpsw_netdev_ops = { |
| 2412 | .ndo_open = cpsw_ndo_open, |
| 2413 | .ndo_stop = cpsw_ndo_stop, |
| 2414 | .ndo_start_xmit = cpsw_ndo_start_xmit, |
Mugunthan V N | dcfd8d5 | 2013-07-25 23:44:01 +0530 | [diff] [blame] | 2415 | .ndo_set_mac_address = cpsw_ndo_set_mac_address, |
Richard Cochran | 2e5b38a | 2012-10-29 08:45:20 +0000 | [diff] [blame] | 2416 | .ndo_do_ioctl = cpsw_ndo_ioctl, |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 2417 | .ndo_validate_addr = eth_validate_addr, |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 2418 | .ndo_tx_timeout = cpsw_ndo_tx_timeout, |
Mugunthan V N | 5c50a85 | 2012-10-29 08:45:11 +0000 | [diff] [blame] | 2419 | .ndo_set_rx_mode = cpsw_ndo_set_rx_mode, |
Ivan Khoronzhuk | 83fcad0 | 2016-11-29 17:00:49 +0200 | [diff] [blame] | 2420 | .ndo_set_tx_maxrate = cpsw_ndo_set_tx_maxrate, |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 2421 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 2422 | .ndo_poll_controller = cpsw_ndo_poll_controller, |
| 2423 | #endif |
Mugunthan V N | 3b72c2f | 2013-02-05 08:26:48 +0000 | [diff] [blame] | 2424 | .ndo_vlan_rx_add_vid = cpsw_ndo_vlan_rx_add_vid, |
| 2425 | .ndo_vlan_rx_kill_vid = cpsw_ndo_vlan_rx_kill_vid, |
Ivan Khoronzhuk | 7929a66 | 2018-07-24 00:26:31 +0300 | [diff] [blame] | 2426 | .ndo_setup_tc = cpsw_ndo_setup_tc, |
Ivan Khoronzhuk | 9ed4050 | 2019-07-09 00:34:32 +0300 | [diff] [blame] | 2427 | .ndo_bpf = cpsw_ndo_bpf, |
| 2428 | .ndo_xdp_xmit = cpsw_ndo_xdp_xmit, |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 2429 | }; |
| 2430 | |
| 2431 | static void cpsw_get_drvinfo(struct net_device *ndev, |
| 2432 | struct ethtool_drvinfo *info) |
| 2433 | { |
Ivan Khoronzhuk | 649a168 | 2016-08-10 02:22:37 +0300 | [diff] [blame] | 2434 | struct cpsw_common *cpsw = ndev_to_cpsw(ndev); |
Ivan Khoronzhuk | 56e31bd | 2016-08-10 02:22:38 +0300 | [diff] [blame] | 2435 | struct platform_device *pdev = to_platform_device(cpsw->dev); |
Jiri Pirko | 7826d43 | 2013-01-06 00:44:26 +0000 | [diff] [blame] | 2436 | |
Mugunthan V N | 52c4f0e | 2014-07-22 23:25:07 +0530 | [diff] [blame] | 2437 | strlcpy(info->driver, "cpsw", sizeof(info->driver)); |
Jiri Pirko | 7826d43 | 2013-01-06 00:44:26 +0000 | [diff] [blame] | 2438 | strlcpy(info->version, "1.0", sizeof(info->version)); |
Ivan Khoronzhuk | 56e31bd | 2016-08-10 02:22:38 +0300 | [diff] [blame] | 2439 | strlcpy(info->bus_info, pdev->name, sizeof(info->bus_info)); |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 2440 | } |
| 2441 | |
Mugunthan V N | 1923d6e | 2014-09-08 22:54:02 +0530 | [diff] [blame] | 2442 | static int cpsw_set_pauseparam(struct net_device *ndev, |
| 2443 | struct ethtool_pauseparam *pause) |
| 2444 | { |
| 2445 | struct cpsw_priv *priv = netdev_priv(ndev); |
| 2446 | bool link; |
| 2447 | |
| 2448 | priv->rx_pause = pause->rx_pause ? true : false; |
| 2449 | priv->tx_pause = pause->tx_pause ? true : false; |
| 2450 | |
| 2451 | for_each_slave(priv, _cpsw_adjust_link, priv, &link); |
Mugunthan V N | 1923d6e | 2014-09-08 22:54:02 +0530 | [diff] [blame] | 2452 | return 0; |
| 2453 | } |
| 2454 | |
Ivan Khoronzhuk | 022d7ad | 2017-01-19 18:58:27 +0200 | [diff] [blame] | 2455 | static int cpsw_set_channels(struct net_device *ndev, |
| 2456 | struct ethtool_channels *chs) |
| 2457 | { |
Grygorii Strashko | c24eef2 | 2019-04-26 20:12:42 +0300 | [diff] [blame] | 2458 | return cpsw_set_channels_common(ndev, chs, cpsw_rx_handler); |
Grygorii Strashko | be034fc | 2017-01-06 14:07:34 -0600 | [diff] [blame] | 2459 | } |
| 2460 | |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 2461 | static const struct ethtool_ops cpsw_ethtool_ops = { |
| 2462 | .get_drvinfo = cpsw_get_drvinfo, |
| 2463 | .get_msglevel = cpsw_get_msglevel, |
| 2464 | .set_msglevel = cpsw_set_msglevel, |
| 2465 | .get_link = ethtool_op_get_link, |
Richard Cochran | 2e5b38a | 2012-10-29 08:45:20 +0000 | [diff] [blame] | 2466 | .get_ts_info = cpsw_get_ts_info, |
Mugunthan V N | ff5b8ef | 2013-03-11 23:16:37 +0000 | [diff] [blame] | 2467 | .get_coalesce = cpsw_get_coalesce, |
| 2468 | .set_coalesce = cpsw_set_coalesce, |
Mugunthan V N | d971854 | 2013-07-23 15:38:17 +0530 | [diff] [blame] | 2469 | .get_sset_count = cpsw_get_sset_count, |
| 2470 | .get_strings = cpsw_get_strings, |
| 2471 | .get_ethtool_stats = cpsw_get_ethtool_stats, |
Mugunthan V N | 1923d6e | 2014-09-08 22:54:02 +0530 | [diff] [blame] | 2472 | .get_pauseparam = cpsw_get_pauseparam, |
| 2473 | .set_pauseparam = cpsw_set_pauseparam, |
Matus Ujhelyi | d8a6442 | 2013-08-20 07:59:38 +0200 | [diff] [blame] | 2474 | .get_wol = cpsw_get_wol, |
| 2475 | .set_wol = cpsw_set_wol, |
Mugunthan V N | 52c4f0e | 2014-07-22 23:25:07 +0530 | [diff] [blame] | 2476 | .get_regs_len = cpsw_get_regs_len, |
| 2477 | .get_regs = cpsw_get_regs, |
Grygorii Strashko | 7898b1d | 2016-06-24 21:23:44 +0300 | [diff] [blame] | 2478 | .begin = cpsw_ethtool_op_begin, |
| 2479 | .complete = cpsw_ethtool_op_complete, |
Ivan Khoronzhuk | ce52c74 | 2016-08-22 21:18:28 +0300 | [diff] [blame] | 2480 | .get_channels = cpsw_get_channels, |
| 2481 | .set_channels = cpsw_set_channels, |
Philippe Reynes | 2479876 | 2016-10-08 17:46:15 +0200 | [diff] [blame] | 2482 | .get_link_ksettings = cpsw_get_link_ksettings, |
| 2483 | .set_link_ksettings = cpsw_set_link_ksettings, |
Yegor Yefremov | a090994 | 2016-11-28 09:41:33 +0100 | [diff] [blame] | 2484 | .get_eee = cpsw_get_eee, |
| 2485 | .set_eee = cpsw_set_eee, |
Yegor Yefremov | 6bb10c2 | 2016-11-28 10:47:52 +0100 | [diff] [blame] | 2486 | .nway_reset = cpsw_nway_reset, |
Grygorii Strashko | be034fc | 2017-01-06 14:07:34 -0600 | [diff] [blame] | 2487 | .get_ringparam = cpsw_get_ringparam, |
| 2488 | .set_ringparam = cpsw_set_ringparam, |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 2489 | }; |
| 2490 | |
David Rivshin | 552165b | 2016-04-27 21:25:25 -0400 | [diff] [blame] | 2491 | static int cpsw_probe_dt(struct cpsw_platform_data *data, |
Mugunthan V N | 2eb32b0 | 2012-07-30 10:17:14 +0000 | [diff] [blame] | 2492 | struct platform_device *pdev) |
| 2493 | { |
| 2494 | struct device_node *node = pdev->dev.of_node; |
| 2495 | struct device_node *slave_node; |
| 2496 | int i = 0, ret; |
| 2497 | u32 prop; |
| 2498 | |
| 2499 | if (!node) |
| 2500 | return -EINVAL; |
| 2501 | |
| 2502 | if (of_property_read_u32(node, "slaves", &prop)) { |
George Cherian | 88c99ff | 2014-05-12 10:21:19 +0530 | [diff] [blame] | 2503 | dev_err(&pdev->dev, "Missing slaves property in the DT.\n"); |
Mugunthan V N | 2eb32b0 | 2012-07-30 10:17:14 +0000 | [diff] [blame] | 2504 | return -EINVAL; |
| 2505 | } |
| 2506 | data->slaves = prop; |
| 2507 | |
Mugunthan V N | e86ac13 | 2013-03-11 23:16:35 +0000 | [diff] [blame] | 2508 | if (of_property_read_u32(node, "active_slave", &prop)) { |
George Cherian | 88c99ff | 2014-05-12 10:21:19 +0530 | [diff] [blame] | 2509 | dev_err(&pdev->dev, "Missing active_slave property in the DT.\n"); |
Daniel Mack | aa1a15e | 2013-09-21 00:50:38 +0530 | [diff] [blame] | 2510 | return -EINVAL; |
Richard Cochran | 78ca0b2 | 2012-10-29 08:45:18 +0000 | [diff] [blame] | 2511 | } |
Mugunthan V N | e86ac13 | 2013-03-11 23:16:35 +0000 | [diff] [blame] | 2512 | data->active_slave = prop; |
Richard Cochran | 78ca0b2 | 2012-10-29 08:45:18 +0000 | [diff] [blame] | 2513 | |
Kees Cook | a86854d | 2018-06-12 14:07:58 -0700 | [diff] [blame] | 2514 | data->slave_data = devm_kcalloc(&pdev->dev, |
| 2515 | data->slaves, |
| 2516 | sizeof(struct cpsw_slave_data), |
Daniel Mack | aa1a15e | 2013-09-21 00:50:38 +0530 | [diff] [blame] | 2517 | GFP_KERNEL); |
Joe Perches | b2adaca | 2013-02-03 17:43:58 +0000 | [diff] [blame] | 2518 | if (!data->slave_data) |
Daniel Mack | aa1a15e | 2013-09-21 00:50:38 +0530 | [diff] [blame] | 2519 | return -ENOMEM; |
Mugunthan V N | 2eb32b0 | 2012-07-30 10:17:14 +0000 | [diff] [blame] | 2520 | |
Mugunthan V N | 2eb32b0 | 2012-07-30 10:17:14 +0000 | [diff] [blame] | 2521 | if (of_property_read_u32(node, "cpdma_channels", &prop)) { |
George Cherian | 88c99ff | 2014-05-12 10:21:19 +0530 | [diff] [blame] | 2522 | dev_err(&pdev->dev, "Missing cpdma_channels property in the DT.\n"); |
Daniel Mack | aa1a15e | 2013-09-21 00:50:38 +0530 | [diff] [blame] | 2523 | return -EINVAL; |
Mugunthan V N | 2eb32b0 | 2012-07-30 10:17:14 +0000 | [diff] [blame] | 2524 | } |
| 2525 | data->channels = prop; |
| 2526 | |
Mugunthan V N | 2eb32b0 | 2012-07-30 10:17:14 +0000 | [diff] [blame] | 2527 | if (of_property_read_u32(node, "ale_entries", &prop)) { |
George Cherian | 88c99ff | 2014-05-12 10:21:19 +0530 | [diff] [blame] | 2528 | dev_err(&pdev->dev, "Missing ale_entries property in the DT.\n"); |
Daniel Mack | aa1a15e | 2013-09-21 00:50:38 +0530 | [diff] [blame] | 2529 | return -EINVAL; |
Mugunthan V N | 2eb32b0 | 2012-07-30 10:17:14 +0000 | [diff] [blame] | 2530 | } |
| 2531 | data->ale_entries = prop; |
| 2532 | |
Mugunthan V N | 2eb32b0 | 2012-07-30 10:17:14 +0000 | [diff] [blame] | 2533 | if (of_property_read_u32(node, "bd_ram_size", &prop)) { |
George Cherian | 88c99ff | 2014-05-12 10:21:19 +0530 | [diff] [blame] | 2534 | dev_err(&pdev->dev, "Missing bd_ram_size property in the DT.\n"); |
Daniel Mack | aa1a15e | 2013-09-21 00:50:38 +0530 | [diff] [blame] | 2535 | return -EINVAL; |
Mugunthan V N | 2eb32b0 | 2012-07-30 10:17:14 +0000 | [diff] [blame] | 2536 | } |
| 2537 | data->bd_ram_size = prop; |
| 2538 | |
Mugunthan V N | 2eb32b0 | 2012-07-30 10:17:14 +0000 | [diff] [blame] | 2539 | if (of_property_read_u32(node, "mac_control", &prop)) { |
George Cherian | 88c99ff | 2014-05-12 10:21:19 +0530 | [diff] [blame] | 2540 | dev_err(&pdev->dev, "Missing mac_control property in the DT.\n"); |
Daniel Mack | aa1a15e | 2013-09-21 00:50:38 +0530 | [diff] [blame] | 2541 | return -EINVAL; |
Mugunthan V N | 2eb32b0 | 2012-07-30 10:17:14 +0000 | [diff] [blame] | 2542 | } |
| 2543 | data->mac_control = prop; |
| 2544 | |
Markus Pargmann | 281abd9 | 2013-10-04 14:44:40 +0200 | [diff] [blame] | 2545 | if (of_property_read_bool(node, "dual_emac")) |
| 2546 | data->dual_emac = 1; |
Mugunthan V N | d9ba8f9 | 2013-02-11 09:52:20 +0000 | [diff] [blame] | 2547 | |
Vaibhav Hiremath | 1fb19aa | 2012-11-14 09:07:55 +0000 | [diff] [blame] | 2548 | /* |
| 2549 | * Populate all the child nodes here... |
| 2550 | */ |
| 2551 | ret = of_platform_populate(node, NULL, NULL, &pdev->dev); |
| 2552 | /* We do not want to force this, as in some cases may not have child */ |
| 2553 | if (ret) |
George Cherian | 88c99ff | 2014-05-12 10:21:19 +0530 | [diff] [blame] | 2554 | dev_warn(&pdev->dev, "Doesn't have any child node\n"); |
Vaibhav Hiremath | 1fb19aa | 2012-11-14 09:07:55 +0000 | [diff] [blame] | 2555 | |
Ben Hutchings | 8658aaf | 2016-06-21 01:16:31 +0100 | [diff] [blame] | 2556 | for_each_available_child_of_node(node, slave_node) { |
Richard Cochran | 549985e | 2012-11-14 09:07:56 +0000 | [diff] [blame] | 2557 | struct cpsw_slave_data *slave_data = data->slave_data + i; |
| 2558 | const void *mac_addr = NULL; |
Richard Cochran | 549985e | 2012-11-14 09:07:56 +0000 | [diff] [blame] | 2559 | int lenp; |
| 2560 | const __be32 *parp; |
Richard Cochran | 549985e | 2012-11-14 09:07:56 +0000 | [diff] [blame] | 2561 | |
Markus Pargmann | f468b10 | 2013-10-04 14:44:39 +0200 | [diff] [blame] | 2562 | /* This is no slave child node, continue */ |
Rob Herring | bf5849f | 2018-12-05 13:50:32 -0600 | [diff] [blame] | 2563 | if (!of_node_name_eq(slave_node, "slave")) |
Markus Pargmann | f468b10 | 2013-10-04 14:44:39 +0200 | [diff] [blame] | 2564 | continue; |
| 2565 | |
Grygorii Strashko | 3ff1884 | 2018-11-25 18:15:25 -0600 | [diff] [blame] | 2566 | slave_data->ifphy = devm_of_phy_get(&pdev->dev, slave_node, |
| 2567 | NULL); |
| 2568 | if (!IS_ENABLED(CONFIG_TI_CPSW_PHY_SEL) && |
| 2569 | IS_ERR(slave_data->ifphy)) { |
| 2570 | ret = PTR_ERR(slave_data->ifphy); |
| 2571 | dev_err(&pdev->dev, |
| 2572 | "%d: Error retrieving port phy: %d\n", i, ret); |
Nishka Dasgupta | 3cd6e20 | 2019-07-16 11:18:43 +0530 | [diff] [blame] | 2573 | goto err_node_put; |
Grygorii Strashko | 3ff1884 | 2018-11-25 18:15:25 -0600 | [diff] [blame] | 2574 | } |
| 2575 | |
Marek Vasut | 337d172 | 2019-06-23 14:11:43 +0200 | [diff] [blame] | 2576 | slave_data->slave_node = slave_node; |
David Rivshin | 552165b | 2016-04-27 21:25:25 -0400 | [diff] [blame] | 2577 | slave_data->phy_node = of_parse_phandle(slave_node, |
| 2578 | "phy-handle", 0); |
David Rivshin | f1eea5c | 2015-12-16 23:02:10 -0500 | [diff] [blame] | 2579 | parp = of_get_property(slave_node, "phy_id", &lenp); |
David Rivshin | ae092b5 | 2016-04-27 21:38:26 -0400 | [diff] [blame] | 2580 | if (slave_data->phy_node) { |
| 2581 | dev_dbg(&pdev->dev, |
Rob Herring | f7ce910 | 2017-07-18 16:43:19 -0500 | [diff] [blame] | 2582 | "slave[%d] using phy-handle=\"%pOF\"\n", |
| 2583 | i, slave_data->phy_node); |
David Rivshin | ae092b5 | 2016-04-27 21:38:26 -0400 | [diff] [blame] | 2584 | } else if (of_phy_is_fixed_link(slave_node)) { |
David Rivshin | dfc0a6d | 2015-12-16 23:02:11 -0500 | [diff] [blame] | 2585 | /* In the case of a fixed PHY, the DT node associated |
| 2586 | * to the PHY is the Ethernet MAC DT node. |
| 2587 | */ |
Markus Brunner | 1f71e8c | 2015-11-03 22:09:51 +0100 | [diff] [blame] | 2588 | ret = of_phy_register_fixed_link(slave_node); |
Johan Hovold | 23a0987 | 2016-11-17 17:40:04 +0100 | [diff] [blame] | 2589 | if (ret) { |
| 2590 | if (ret != -EPROBE_DEFER) |
| 2591 | dev_err(&pdev->dev, "failed to register fixed-link phy: %d\n", ret); |
Nishka Dasgupta | 3cd6e20 | 2019-07-16 11:18:43 +0530 | [diff] [blame] | 2592 | goto err_node_put; |
Johan Hovold | 23a0987 | 2016-11-17 17:40:04 +0100 | [diff] [blame] | 2593 | } |
David Rivshin | 06cd6d6 | 2016-04-27 21:45:45 -0400 | [diff] [blame] | 2594 | slave_data->phy_node = of_node_get(slave_node); |
David Rivshin | f1eea5c | 2015-12-16 23:02:10 -0500 | [diff] [blame] | 2595 | } else if (parp) { |
| 2596 | u32 phyid; |
| 2597 | struct device_node *mdio_node; |
| 2598 | struct platform_device *mdio; |
| 2599 | |
| 2600 | if (lenp != (sizeof(__be32) * 2)) { |
| 2601 | dev_err(&pdev->dev, "Invalid slave[%d] phy_id property\n", i); |
| 2602 | goto no_phy_slave; |
| 2603 | } |
| 2604 | mdio_node = of_find_node_by_phandle(be32_to_cpup(parp)); |
| 2605 | phyid = be32_to_cpup(parp+1); |
| 2606 | mdio = of_find_device_by_node(mdio_node); |
| 2607 | of_node_put(mdio_node); |
| 2608 | if (!mdio) { |
| 2609 | dev_err(&pdev->dev, "Missing mdio platform device\n"); |
Nishka Dasgupta | 3cd6e20 | 2019-07-16 11:18:43 +0530 | [diff] [blame] | 2610 | ret = -EINVAL; |
| 2611 | goto err_node_put; |
David Rivshin | f1eea5c | 2015-12-16 23:02:10 -0500 | [diff] [blame] | 2612 | } |
| 2613 | snprintf(slave_data->phy_id, sizeof(slave_data->phy_id), |
| 2614 | PHY_ID_FMT, mdio->name, phyid); |
Johan Hovold | 86e1d5a | 2016-11-17 17:39:59 +0100 | [diff] [blame] | 2615 | put_device(&mdio->dev); |
David Rivshin | f1eea5c | 2015-12-16 23:02:10 -0500 | [diff] [blame] | 2616 | } else { |
David Rivshin | ae092b5 | 2016-04-27 21:38:26 -0400 | [diff] [blame] | 2617 | dev_err(&pdev->dev, |
| 2618 | "No slave[%d] phy_id, phy-handle, or fixed-link property\n", |
| 2619 | i); |
Markus Brunner | 1f71e8c | 2015-11-03 22:09:51 +0100 | [diff] [blame] | 2620 | goto no_phy_slave; |
| 2621 | } |
Andrew Lunn | 0c65b2b | 2019-11-04 02:40:33 +0100 | [diff] [blame^] | 2622 | ret = of_get_phy_mode(slave_node, &slave_data->phy_if); |
| 2623 | if (ret) { |
Mugunthan V N | 47276fc | 2014-10-24 18:51:33 +0530 | [diff] [blame] | 2624 | dev_err(&pdev->dev, "Missing or malformed slave[%d] phy-mode property\n", |
| 2625 | i); |
Nishka Dasgupta | 3cd6e20 | 2019-07-16 11:18:43 +0530 | [diff] [blame] | 2626 | goto err_node_put; |
Mugunthan V N | 47276fc | 2014-10-24 18:51:33 +0530 | [diff] [blame] | 2627 | } |
| 2628 | |
| 2629 | no_phy_slave: |
Richard Cochran | 549985e | 2012-11-14 09:07:56 +0000 | [diff] [blame] | 2630 | mac_addr = of_get_mac_address(slave_node); |
Petr Å tetiar | a51645f | 2019-05-06 23:27:04 +0200 | [diff] [blame] | 2631 | if (!IS_ERR(mac_addr)) { |
Petr Å tetiar | 2d2924a | 2019-05-10 11:35:17 +0200 | [diff] [blame] | 2632 | ether_addr_copy(slave_data->mac_addr, mac_addr); |
Markus Pargmann | 0ba517b | 2014-09-29 08:53:17 +0200 | [diff] [blame] | 2633 | } else { |
Mugunthan V N | b6745f6 | 2015-09-21 15:56:50 +0530 | [diff] [blame] | 2634 | ret = ti_cm_get_macid(&pdev->dev, i, |
| 2635 | slave_data->mac_addr); |
| 2636 | if (ret) |
Nishka Dasgupta | 3cd6e20 | 2019-07-16 11:18:43 +0530 | [diff] [blame] | 2637 | goto err_node_put; |
Markus Pargmann | 0ba517b | 2014-09-29 08:53:17 +0200 | [diff] [blame] | 2638 | } |
Mugunthan V N | d9ba8f9 | 2013-02-11 09:52:20 +0000 | [diff] [blame] | 2639 | if (data->dual_emac) { |
Mugunthan V N | 91c4166 | 2013-04-15 07:31:28 +0000 | [diff] [blame] | 2640 | if (of_property_read_u32(slave_node, "dual_emac_res_vlan", |
Mugunthan V N | d9ba8f9 | 2013-02-11 09:52:20 +0000 | [diff] [blame] | 2641 | &prop)) { |
George Cherian | 88c99ff | 2014-05-12 10:21:19 +0530 | [diff] [blame] | 2642 | dev_err(&pdev->dev, "Missing dual_emac_res_vlan in DT.\n"); |
Mugunthan V N | d9ba8f9 | 2013-02-11 09:52:20 +0000 | [diff] [blame] | 2643 | slave_data->dual_emac_res_vlan = i+1; |
George Cherian | 88c99ff | 2014-05-12 10:21:19 +0530 | [diff] [blame] | 2644 | dev_err(&pdev->dev, "Using %d as Reserved VLAN for %d slave\n", |
| 2645 | slave_data->dual_emac_res_vlan, i); |
Mugunthan V N | d9ba8f9 | 2013-02-11 09:52:20 +0000 | [diff] [blame] | 2646 | } else { |
| 2647 | slave_data->dual_emac_res_vlan = prop; |
| 2648 | } |
| 2649 | } |
| 2650 | |
Richard Cochran | 549985e | 2012-11-14 09:07:56 +0000 | [diff] [blame] | 2651 | i++; |
Nishka Dasgupta | 3cd6e20 | 2019-07-16 11:18:43 +0530 | [diff] [blame] | 2652 | if (i == data->slaves) { |
| 2653 | ret = 0; |
| 2654 | goto err_node_put; |
| 2655 | } |
Richard Cochran | 549985e | 2012-11-14 09:07:56 +0000 | [diff] [blame] | 2656 | } |
| 2657 | |
Mugunthan V N | 2eb32b0 | 2012-07-30 10:17:14 +0000 | [diff] [blame] | 2658 | return 0; |
Nishka Dasgupta | 3cd6e20 | 2019-07-16 11:18:43 +0530 | [diff] [blame] | 2659 | |
| 2660 | err_node_put: |
| 2661 | of_node_put(slave_node); |
| 2662 | return ret; |
Mugunthan V N | 2eb32b0 | 2012-07-30 10:17:14 +0000 | [diff] [blame] | 2663 | } |
| 2664 | |
Johan Hovold | a4e32b0 | 2016-11-17 17:40:00 +0100 | [diff] [blame] | 2665 | static void cpsw_remove_dt(struct platform_device *pdev) |
| 2666 | { |
Ivan Khoronzhuk | bfe5903 | 2019-06-12 00:49:03 +0300 | [diff] [blame] | 2667 | struct cpsw_common *cpsw = platform_get_drvdata(pdev); |
Johan Hovold | 8cbcc46 | 2016-11-17 17:40:01 +0100 | [diff] [blame] | 2668 | struct cpsw_platform_data *data = &cpsw->data; |
| 2669 | struct device_node *node = pdev->dev.of_node; |
| 2670 | struct device_node *slave_node; |
| 2671 | int i = 0; |
| 2672 | |
| 2673 | for_each_available_child_of_node(node, slave_node) { |
| 2674 | struct cpsw_slave_data *slave_data = &data->slave_data[i]; |
| 2675 | |
Rob Herring | bf5849f | 2018-12-05 13:50:32 -0600 | [diff] [blame] | 2676 | if (!of_node_name_eq(slave_node, "slave")) |
Johan Hovold | 8cbcc46 | 2016-11-17 17:40:01 +0100 | [diff] [blame] | 2677 | continue; |
| 2678 | |
Johan Hovold | 3f65047 | 2016-11-28 19:24:55 +0100 | [diff] [blame] | 2679 | if (of_phy_is_fixed_link(slave_node)) |
| 2680 | of_phy_deregister_fixed_link(slave_node); |
Johan Hovold | 8cbcc46 | 2016-11-17 17:40:01 +0100 | [diff] [blame] | 2681 | |
| 2682 | of_node_put(slave_data->phy_node); |
| 2683 | |
| 2684 | i++; |
Nishka Dasgupta | 3cd6e20 | 2019-07-16 11:18:43 +0530 | [diff] [blame] | 2685 | if (i == data->slaves) { |
| 2686 | of_node_put(slave_node); |
Johan Hovold | 8cbcc46 | 2016-11-17 17:40:01 +0100 | [diff] [blame] | 2687 | break; |
Nishka Dasgupta | 3cd6e20 | 2019-07-16 11:18:43 +0530 | [diff] [blame] | 2688 | } |
Johan Hovold | 8cbcc46 | 2016-11-17 17:40:01 +0100 | [diff] [blame] | 2689 | } |
| 2690 | |
Johan Hovold | a4e32b0 | 2016-11-17 17:40:00 +0100 | [diff] [blame] | 2691 | of_platform_depopulate(&pdev->dev); |
| 2692 | } |
| 2693 | |
Ivan Khoronzhuk | 56e31bd | 2016-08-10 02:22:38 +0300 | [diff] [blame] | 2694 | static int cpsw_probe_dual_emac(struct cpsw_priv *priv) |
Mugunthan V N | d9ba8f9 | 2013-02-11 09:52:20 +0000 | [diff] [blame] | 2695 | { |
Ivan Khoronzhuk | 606f399 | 2016-08-10 02:22:42 +0300 | [diff] [blame] | 2696 | struct cpsw_common *cpsw = priv->cpsw; |
| 2697 | struct cpsw_platform_data *data = &cpsw->data; |
Mugunthan V N | d9ba8f9 | 2013-02-11 09:52:20 +0000 | [diff] [blame] | 2698 | struct net_device *ndev; |
| 2699 | struct cpsw_priv *priv_sl2; |
Ivan Khoronzhuk | e38b5a3 | 2016-08-10 02:22:41 +0300 | [diff] [blame] | 2700 | int ret = 0; |
Mugunthan V N | d9ba8f9 | 2013-02-11 09:52:20 +0000 | [diff] [blame] | 2701 | |
Grygorii Strashko | d183a94 | 2019-04-26 20:12:29 +0300 | [diff] [blame] | 2702 | ndev = devm_alloc_etherdev_mqs(cpsw->dev, sizeof(struct cpsw_priv), |
| 2703 | CPSW_MAX_QUEUES, CPSW_MAX_QUEUES); |
Mugunthan V N | d9ba8f9 | 2013-02-11 09:52:20 +0000 | [diff] [blame] | 2704 | if (!ndev) { |
Ivan Khoronzhuk | 56e31bd | 2016-08-10 02:22:38 +0300 | [diff] [blame] | 2705 | dev_err(cpsw->dev, "cpsw: error allocating net_device\n"); |
Mugunthan V N | d9ba8f9 | 2013-02-11 09:52:20 +0000 | [diff] [blame] | 2706 | return -ENOMEM; |
| 2707 | } |
| 2708 | |
| 2709 | priv_sl2 = netdev_priv(ndev); |
Ivan Khoronzhuk | 606f399 | 2016-08-10 02:22:42 +0300 | [diff] [blame] | 2710 | priv_sl2->cpsw = cpsw; |
Mugunthan V N | d9ba8f9 | 2013-02-11 09:52:20 +0000 | [diff] [blame] | 2711 | priv_sl2->ndev = ndev; |
| 2712 | priv_sl2->dev = &ndev->dev; |
| 2713 | priv_sl2->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG); |
Mugunthan V N | d9ba8f9 | 2013-02-11 09:52:20 +0000 | [diff] [blame] | 2714 | |
| 2715 | if (is_valid_ether_addr(data->slave_data[1].mac_addr)) { |
| 2716 | memcpy(priv_sl2->mac_addr, data->slave_data[1].mac_addr, |
| 2717 | ETH_ALEN); |
Ivan Khoronzhuk | 56e31bd | 2016-08-10 02:22:38 +0300 | [diff] [blame] | 2718 | dev_info(cpsw->dev, "cpsw: Detected MACID = %pM\n", |
| 2719 | priv_sl2->mac_addr); |
Mugunthan V N | d9ba8f9 | 2013-02-11 09:52:20 +0000 | [diff] [blame] | 2720 | } else { |
Joe Perches | 6c1f0a1 | 2018-06-22 10:51:00 -0700 | [diff] [blame] | 2721 | eth_random_addr(priv_sl2->mac_addr); |
Ivan Khoronzhuk | 56e31bd | 2016-08-10 02:22:38 +0300 | [diff] [blame] | 2722 | dev_info(cpsw->dev, "cpsw: Random MACID = %pM\n", |
| 2723 | priv_sl2->mac_addr); |
Mugunthan V N | d9ba8f9 | 2013-02-11 09:52:20 +0000 | [diff] [blame] | 2724 | } |
| 2725 | memcpy(ndev->dev_addr, priv_sl2->mac_addr, ETH_ALEN); |
| 2726 | |
Mugunthan V N | d9ba8f9 | 2013-02-11 09:52:20 +0000 | [diff] [blame] | 2727 | priv_sl2->emac_port = 1; |
Ivan Khoronzhuk | 606f399 | 2016-08-10 02:22:42 +0300 | [diff] [blame] | 2728 | cpsw->slaves[1].ndev = ndev; |
Ivan Khoronzhuk | 193736c | 2018-07-27 19:54:39 +0300 | [diff] [blame] | 2729 | ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_CTAG_RX; |
Mugunthan V N | d9ba8f9 | 2013-02-11 09:52:20 +0000 | [diff] [blame] | 2730 | |
| 2731 | ndev->netdev_ops = &cpsw_netdev_ops; |
Wilfried Klaebe | 7ad24ea | 2014-05-11 00:12:32 +0000 | [diff] [blame] | 2732 | ndev->ethtool_ops = &cpsw_ethtool_ops; |
Mugunthan V N | d9ba8f9 | 2013-02-11 09:52:20 +0000 | [diff] [blame] | 2733 | |
| 2734 | /* register the network device */ |
Ivan Khoronzhuk | 56e31bd | 2016-08-10 02:22:38 +0300 | [diff] [blame] | 2735 | SET_NETDEV_DEV(ndev, cpsw->dev); |
Marek Vasut | 337d172 | 2019-06-23 14:11:43 +0200 | [diff] [blame] | 2736 | ndev->dev.of_node = cpsw->slaves[1].data->slave_node; |
Mugunthan V N | d9ba8f9 | 2013-02-11 09:52:20 +0000 | [diff] [blame] | 2737 | ret = register_netdev(ndev); |
Grygorii Strashko | d183a94 | 2019-04-26 20:12:29 +0300 | [diff] [blame] | 2738 | if (ret) |
Ivan Khoronzhuk | 56e31bd | 2016-08-10 02:22:38 +0300 | [diff] [blame] | 2739 | dev_err(cpsw->dev, "cpsw: error registering net device\n"); |
Mugunthan V N | d9ba8f9 | 2013-02-11 09:52:20 +0000 | [diff] [blame] | 2740 | |
| 2741 | return ret; |
| 2742 | } |
| 2743 | |
Mugunthan V N | 7da1160 | 2015-08-12 15:22:53 +0530 | [diff] [blame] | 2744 | static const struct of_device_id cpsw_of_mtable[] = { |
Ivan Khoronzhuk | 9611d6d | 2018-05-17 01:21:45 +0300 | [diff] [blame] | 2745 | { .compatible = "ti,cpsw"}, |
| 2746 | { .compatible = "ti,am335x-cpsw"}, |
| 2747 | { .compatible = "ti,am4372-cpsw"}, |
| 2748 | { .compatible = "ti,dra7-cpsw"}, |
Mugunthan V N | 7da1160 | 2015-08-12 15:22:53 +0530 | [diff] [blame] | 2749 | { /* sentinel */ }, |
| 2750 | }; |
| 2751 | MODULE_DEVICE_TABLE(of, cpsw_of_mtable); |
| 2752 | |
Ivan Khoronzhuk | 9611d6d | 2018-05-17 01:21:45 +0300 | [diff] [blame] | 2753 | static const struct soc_device_attribute cpsw_soc_devices[] = { |
| 2754 | { .family = "AM33xx", .revision = "ES1.0"}, |
| 2755 | { /* sentinel */ } |
| 2756 | }; |
| 2757 | |
Bill Pemberton | 663e12e | 2012-12-03 09:23:45 -0500 | [diff] [blame] | 2758 | static int cpsw_probe(struct platform_device *pdev) |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 2759 | { |
Grygorii Strashko | c8fb566 | 2019-04-26 20:12:27 +0300 | [diff] [blame] | 2760 | struct device *dev = &pdev->dev; |
Ivan Khoronzhuk | ef4183a | 2016-08-10 02:22:35 +0300 | [diff] [blame] | 2761 | struct clk *clk; |
Sebastian Siewior | d1bd9ac | 2013-04-24 08:48:23 +0000 | [diff] [blame] | 2762 | struct cpsw_platform_data *data; |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 2763 | struct net_device *ndev; |
| 2764 | struct cpsw_priv *priv; |
Daniel Mack | aa1a15e | 2013-09-21 00:50:38 +0530 | [diff] [blame] | 2765 | void __iomem *ss_regs; |
YueHaibing | c8ace62 | 2019-08-21 20:48:50 +0800 | [diff] [blame] | 2766 | struct resource *ss_res; |
Mugunthan V N | 1d147cc | 2015-09-07 15:16:44 +0530 | [diff] [blame] | 2767 | struct gpio_descs *mode; |
Ivan Khoronzhuk | 9611d6d | 2018-05-17 01:21:45 +0300 | [diff] [blame] | 2768 | const struct soc_device_attribute *soc; |
Ivan Khoronzhuk | 649a168 | 2016-08-10 02:22:37 +0300 | [diff] [blame] | 2769 | struct cpsw_common *cpsw; |
Grygorii Strashko | e6a8462 | 2019-04-26 20:12:39 +0300 | [diff] [blame] | 2770 | int ret = 0, ch; |
Felipe Balbi | 5087b91 | 2015-01-16 10:11:11 -0600 | [diff] [blame] | 2771 | int irq; |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 2772 | |
Grygorii Strashko | c8fb566 | 2019-04-26 20:12:27 +0300 | [diff] [blame] | 2773 | cpsw = devm_kzalloc(dev, sizeof(struct cpsw_common), GFP_KERNEL); |
Johan Hovold | 3420ea8 | 2016-11-17 17:40:03 +0100 | [diff] [blame] | 2774 | if (!cpsw) |
| 2775 | return -ENOMEM; |
| 2776 | |
Antoine Tenart | 2d683ea | 2019-08-21 16:41:23 +0200 | [diff] [blame] | 2777 | platform_set_drvdata(pdev, cpsw); |
Grygorii Strashko | c8fb566 | 2019-04-26 20:12:27 +0300 | [diff] [blame] | 2778 | cpsw->dev = dev; |
Ivan Khoronzhuk | 649a168 | 2016-08-10 02:22:37 +0300 | [diff] [blame] | 2779 | |
Grygorii Strashko | c8fb566 | 2019-04-26 20:12:27 +0300 | [diff] [blame] | 2780 | mode = devm_gpiod_get_array_optional(dev, "mode", GPIOD_OUT_LOW); |
Mugunthan V N | 1d147cc | 2015-09-07 15:16:44 +0530 | [diff] [blame] | 2781 | if (IS_ERR(mode)) { |
| 2782 | ret = PTR_ERR(mode); |
Grygorii Strashko | c8fb566 | 2019-04-26 20:12:27 +0300 | [diff] [blame] | 2783 | dev_err(dev, "gpio request failed, ret %d\n", ret); |
Grygorii Strashko | d183a94 | 2019-04-26 20:12:29 +0300 | [diff] [blame] | 2784 | return ret; |
Mugunthan V N | 1d147cc | 2015-09-07 15:16:44 +0530 | [diff] [blame] | 2785 | } |
| 2786 | |
Grygorii Strashko | 83a8471 | 2019-04-26 20:12:36 +0300 | [diff] [blame] | 2787 | clk = devm_clk_get(dev, "fck"); |
| 2788 | if (IS_ERR(clk)) { |
YueHaibing | ac97a35 | 2019-04-30 01:55:24 +0000 | [diff] [blame] | 2789 | ret = PTR_ERR(clk); |
Grygorii Strashko | 83a8471 | 2019-04-26 20:12:36 +0300 | [diff] [blame] | 2790 | dev_err(dev, "fck is not found %d\n", ret); |
| 2791 | return ret; |
| 2792 | } |
| 2793 | cpsw->bus_freq_mhz = clk_get_rate(clk) / 1000000; |
| 2794 | |
| 2795 | ss_res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 2796 | ss_regs = devm_ioremap_resource(dev, ss_res); |
| 2797 | if (IS_ERR(ss_regs)) |
| 2798 | return PTR_ERR(ss_regs); |
| 2799 | cpsw->regs = ss_regs; |
| 2800 | |
YueHaibing | c8ace62 | 2019-08-21 20:48:50 +0800 | [diff] [blame] | 2801 | cpsw->wr_regs = devm_platform_ioremap_resource(pdev, 1); |
Grygorii Strashko | 83a8471 | 2019-04-26 20:12:36 +0300 | [diff] [blame] | 2802 | if (IS_ERR(cpsw->wr_regs)) |
| 2803 | return PTR_ERR(cpsw->wr_regs); |
| 2804 | |
| 2805 | /* RX IRQ */ |
| 2806 | irq = platform_get_irq(pdev, 1); |
| 2807 | if (irq < 0) |
| 2808 | return irq; |
| 2809 | cpsw->irqs_table[0] = irq; |
| 2810 | |
| 2811 | /* TX IRQ */ |
| 2812 | irq = platform_get_irq(pdev, 2); |
| 2813 | if (irq < 0) |
| 2814 | return irq; |
| 2815 | cpsw->irqs_table[1] = irq; |
| 2816 | |
Vaibhav Hiremath | 1fb19aa | 2012-11-14 09:07:55 +0000 | [diff] [blame] | 2817 | /* |
| 2818 | * This may be required here for child devices. |
| 2819 | */ |
Grygorii Strashko | c8fb566 | 2019-04-26 20:12:27 +0300 | [diff] [blame] | 2820 | pm_runtime_enable(dev); |
Vaibhav Hiremath | 1fb19aa | 2012-11-14 09:07:55 +0000 | [diff] [blame] | 2821 | |
Johan Hovold | a4e32b0 | 2016-11-17 17:40:00 +0100 | [diff] [blame] | 2822 | /* Need to enable clocks with runtime PM api to access module |
| 2823 | * registers |
| 2824 | */ |
Grygorii Strashko | c8fb566 | 2019-04-26 20:12:27 +0300 | [diff] [blame] | 2825 | ret = pm_runtime_get_sync(dev); |
Johan Hovold | a4e32b0 | 2016-11-17 17:40:00 +0100 | [diff] [blame] | 2826 | if (ret < 0) { |
Grygorii Strashko | c8fb566 | 2019-04-26 20:12:27 +0300 | [diff] [blame] | 2827 | pm_runtime_put_noidle(dev); |
Daniel Mack | aa1a15e | 2013-09-21 00:50:38 +0530 | [diff] [blame] | 2828 | goto clean_runtime_disable_ret; |
Mugunthan V N | 2eb32b0 | 2012-07-30 10:17:14 +0000 | [diff] [blame] | 2829 | } |
Johan Hovold | a4e32b0 | 2016-11-17 17:40:00 +0100 | [diff] [blame] | 2830 | |
Johan Hovold | 23a0987 | 2016-11-17 17:40:04 +0100 | [diff] [blame] | 2831 | ret = cpsw_probe_dt(&cpsw->data, pdev); |
| 2832 | if (ret) |
Johan Hovold | a4e32b0 | 2016-11-17 17:40:00 +0100 | [diff] [blame] | 2833 | goto clean_dt_ret; |
Johan Hovold | 23a0987 | 2016-11-17 17:40:04 +0100 | [diff] [blame] | 2834 | |
Grygorii Strashko | 83a8471 | 2019-04-26 20:12:36 +0300 | [diff] [blame] | 2835 | soc = soc_device_match(cpsw_soc_devices); |
| 2836 | if (soc) |
| 2837 | cpsw->quirk_irq = 1; |
| 2838 | |
Ivan Khoronzhuk | 606f399 | 2016-08-10 02:22:42 +0300 | [diff] [blame] | 2839 | data = &cpsw->data; |
Grygorii Strashko | c8fb566 | 2019-04-26 20:12:27 +0300 | [diff] [blame] | 2840 | cpsw->slaves = devm_kcalloc(dev, |
Kees Cook | a86854d | 2018-06-12 14:07:58 -0700 | [diff] [blame] | 2841 | data->slaves, sizeof(struct cpsw_slave), |
Daniel Mack | aa1a15e | 2013-09-21 00:50:38 +0530 | [diff] [blame] | 2842 | GFP_KERNEL); |
Ivan Khoronzhuk | 606f399 | 2016-08-10 02:22:42 +0300 | [diff] [blame] | 2843 | if (!cpsw->slaves) { |
Daniel Mack | aa1a15e | 2013-09-21 00:50:38 +0530 | [diff] [blame] | 2844 | ret = -ENOMEM; |
Johan Hovold | a4e32b0 | 2016-11-17 17:40:00 +0100 | [diff] [blame] | 2845 | goto clean_dt_ret; |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 2846 | } |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 2847 | |
Grygorii Strashko | 83a8471 | 2019-04-26 20:12:36 +0300 | [diff] [blame] | 2848 | cpsw->rx_packet_max = max(rx_packet_max, CPSW_MAX_PACKET_SIZE); |
Grygorii Strashko | c24eef2 | 2019-04-26 20:12:42 +0300 | [diff] [blame] | 2849 | cpsw->descs_pool_size = descs_pool_size; |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 2850 | |
Grygorii Strashko | e6a8462 | 2019-04-26 20:12:39 +0300 | [diff] [blame] | 2851 | ret = cpsw_init_common(cpsw, ss_regs, ale_ageout, |
| 2852 | ss_res->start + CPSW2_BD_OFFSET, |
| 2853 | descs_pool_size); |
| 2854 | if (ret) |
Johan Hovold | a4e32b0 | 2016-11-17 17:40:00 +0100 | [diff] [blame] | 2855 | goto clean_dt_ret; |
Grygorii Strashko | 8a2c9a5 | 2016-12-06 18:00:41 -0600 | [diff] [blame] | 2856 | |
Grygorii Strashko | 83a8471 | 2019-04-26 20:12:36 +0300 | [diff] [blame] | 2857 | ch = cpsw->quirk_irq ? 0 : 7; |
| 2858 | cpsw->txv[0].ch = cpdma_chan_create(cpsw->dma, ch, cpsw_tx_handler, 0); |
| 2859 | if (IS_ERR(cpsw->txv[0].ch)) { |
| 2860 | dev_err(dev, "error initializing tx dma channel\n"); |
| 2861 | ret = PTR_ERR(cpsw->txv[0].ch); |
| 2862 | goto clean_cpts; |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 2863 | } |
| 2864 | |
Grygorii Strashko | 83a8471 | 2019-04-26 20:12:36 +0300 | [diff] [blame] | 2865 | cpsw->rxv[0].ch = cpdma_chan_create(cpsw->dma, 0, cpsw_rx_handler, 1); |
| 2866 | if (IS_ERR(cpsw->rxv[0].ch)) { |
| 2867 | dev_err(dev, "error initializing rx dma channel\n"); |
| 2868 | ret = PTR_ERR(cpsw->rxv[0].ch); |
| 2869 | goto clean_cpts; |
| 2870 | } |
| 2871 | cpsw_split_res(cpsw); |
| 2872 | |
| 2873 | /* setup netdev */ |
| 2874 | ndev = devm_alloc_etherdev_mqs(dev, sizeof(struct cpsw_priv), |
| 2875 | CPSW_MAX_QUEUES, CPSW_MAX_QUEUES); |
| 2876 | if (!ndev) { |
| 2877 | dev_err(dev, "error allocating net_device\n"); |
| 2878 | goto clean_cpts; |
| 2879 | } |
| 2880 | |
Grygorii Strashko | 83a8471 | 2019-04-26 20:12:36 +0300 | [diff] [blame] | 2881 | priv = netdev_priv(ndev); |
| 2882 | priv->cpsw = cpsw; |
| 2883 | priv->ndev = ndev; |
| 2884 | priv->dev = dev; |
| 2885 | priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG); |
| 2886 | priv->emac_port = 0; |
| 2887 | |
| 2888 | if (is_valid_ether_addr(data->slave_data[0].mac_addr)) { |
| 2889 | memcpy(priv->mac_addr, data->slave_data[0].mac_addr, ETH_ALEN); |
| 2890 | dev_info(dev, "Detected MACID = %pM\n", priv->mac_addr); |
| 2891 | } else { |
| 2892 | eth_random_addr(priv->mac_addr); |
| 2893 | dev_info(dev, "Random MACID = %pM\n", priv->mac_addr); |
| 2894 | } |
| 2895 | |
| 2896 | memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN); |
| 2897 | |
| 2898 | cpsw->slaves[0].ndev = ndev; |
| 2899 | |
Grygorii Strashko | a3a41d2 | 2018-03-15 15:15:50 -0500 | [diff] [blame] | 2900 | ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_CTAG_RX; |
Keerthy | 070f9c6 | 2017-07-20 16:59:52 +0530 | [diff] [blame] | 2901 | |
| 2902 | ndev->netdev_ops = &cpsw_netdev_ops; |
| 2903 | ndev->ethtool_ops = &cpsw_ethtool_ops; |
Ivan Khoronzhuk | 9611d6d | 2018-05-17 01:21:45 +0300 | [diff] [blame] | 2904 | netif_napi_add(ndev, &cpsw->napi_rx, |
| 2905 | cpsw->quirk_irq ? cpsw_rx_poll : cpsw_rx_mq_poll, |
| 2906 | CPSW_POLL_WEIGHT); |
| 2907 | netif_tx_napi_add(ndev, &cpsw->napi_tx, |
| 2908 | cpsw->quirk_irq ? cpsw_tx_poll : cpsw_tx_mq_poll, |
| 2909 | CPSW_POLL_WEIGHT); |
Keerthy | 070f9c6 | 2017-07-20 16:59:52 +0530 | [diff] [blame] | 2910 | |
| 2911 | /* register the network device */ |
Grygorii Strashko | c8fb566 | 2019-04-26 20:12:27 +0300 | [diff] [blame] | 2912 | SET_NETDEV_DEV(ndev, dev); |
Marek Vasut | 337d172 | 2019-06-23 14:11:43 +0200 | [diff] [blame] | 2913 | ndev->dev.of_node = cpsw->slaves[0].data->slave_node; |
Keerthy | 070f9c6 | 2017-07-20 16:59:52 +0530 | [diff] [blame] | 2914 | ret = register_netdev(ndev); |
| 2915 | if (ret) { |
Grygorii Strashko | c8fb566 | 2019-04-26 20:12:27 +0300 | [diff] [blame] | 2916 | dev_err(dev, "error registering net device\n"); |
Keerthy | 070f9c6 | 2017-07-20 16:59:52 +0530 | [diff] [blame] | 2917 | ret = -ENODEV; |
Grygorii Strashko | 83a8471 | 2019-04-26 20:12:36 +0300 | [diff] [blame] | 2918 | goto clean_cpts; |
Keerthy | 070f9c6 | 2017-07-20 16:59:52 +0530 | [diff] [blame] | 2919 | } |
| 2920 | |
| 2921 | if (cpsw->data.dual_emac) { |
| 2922 | ret = cpsw_probe_dual_emac(priv); |
| 2923 | if (ret) { |
| 2924 | cpsw_err(priv, probe, "error probe slave 2 emac interface\n"); |
| 2925 | goto clean_unregister_netdev_ret; |
| 2926 | } |
| 2927 | } |
| 2928 | |
Felipe Balbi | c03abd8 | 2015-01-16 10:11:12 -0600 | [diff] [blame] | 2929 | /* Grab RX and TX IRQs. Note that we also have RX_THRESHOLD and |
| 2930 | * MISC IRQs which are always kept disabled with this driver so |
| 2931 | * we will not request them. |
| 2932 | * |
| 2933 | * If anyone wants to implement support for those, make sure to |
| 2934 | * first request and append them to irqs_table array. |
| 2935 | */ |
Grygorii Strashko | 83a8471 | 2019-04-26 20:12:36 +0300 | [diff] [blame] | 2936 | ret = devm_request_irq(dev, cpsw->irqs_table[0], cpsw_rx_interrupt, |
Grygorii Strashko | c8fb566 | 2019-04-26 20:12:27 +0300 | [diff] [blame] | 2937 | 0, dev_name(dev), cpsw); |
Felipe Balbi | 5087b91 | 2015-01-16 10:11:11 -0600 | [diff] [blame] | 2938 | if (ret < 0) { |
Grygorii Strashko | c8fb566 | 2019-04-26 20:12:27 +0300 | [diff] [blame] | 2939 | dev_err(dev, "error attaching irq (%d)\n", ret); |
Grygorii Strashko | 83a8471 | 2019-04-26 20:12:36 +0300 | [diff] [blame] | 2940 | goto clean_unregister_netdev_ret; |
Felipe Balbi | 5087b91 | 2015-01-16 10:11:11 -0600 | [diff] [blame] | 2941 | } |
| 2942 | |
Felipe Balbi | 5087b91 | 2015-01-16 10:11:11 -0600 | [diff] [blame] | 2943 | |
Grygorii Strashko | 83a8471 | 2019-04-26 20:12:36 +0300 | [diff] [blame] | 2944 | ret = devm_request_irq(dev, cpsw->irqs_table[1], cpsw_tx_interrupt, |
Ivan Khoronzhuk | dbc4ec5 | 2016-08-10 02:22:43 +0300 | [diff] [blame] | 2945 | 0, dev_name(&pdev->dev), cpsw); |
Felipe Balbi | 5087b91 | 2015-01-16 10:11:11 -0600 | [diff] [blame] | 2946 | if (ret < 0) { |
Grygorii Strashko | c8fb566 | 2019-04-26 20:12:27 +0300 | [diff] [blame] | 2947 | dev_err(dev, "error attaching irq (%d)\n", ret); |
Grygorii Strashko | 83a8471 | 2019-04-26 20:12:36 +0300 | [diff] [blame] | 2948 | goto clean_unregister_netdev_ret; |
Felipe Balbi | 5087b91 | 2015-01-16 10:11:11 -0600 | [diff] [blame] | 2949 | } |
Daniel Mack | c2b32e5 | 2014-09-04 09:00:23 +0200 | [diff] [blame] | 2950 | |
Grygorii Strashko | 90225bf | 2017-01-06 14:07:33 -0600 | [diff] [blame] | 2951 | cpsw_notice(priv, probe, |
| 2952 | "initialized device (regs %pa, irq %d, pool size %d)\n", |
Grygorii Strashko | 83a8471 | 2019-04-26 20:12:36 +0300 | [diff] [blame] | 2953 | &ss_res->start, cpsw->irqs_table[0], descs_pool_size); |
Mugunthan V N | d9ba8f9 | 2013-02-11 09:52:20 +0000 | [diff] [blame] | 2954 | |
Johan Hovold | c46ab7e | 2016-11-17 17:39:58 +0100 | [diff] [blame] | 2955 | pm_runtime_put(&pdev->dev); |
| 2956 | |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 2957 | return 0; |
| 2958 | |
Johan Hovold | a7fe9d4 | 2016-11-17 17:40:02 +0100 | [diff] [blame] | 2959 | clean_unregister_netdev_ret: |
| 2960 | unregister_netdev(ndev); |
Grygorii Strashko | 83a8471 | 2019-04-26 20:12:36 +0300 | [diff] [blame] | 2961 | clean_cpts: |
| 2962 | cpts_release(cpsw->cpts); |
Ivan Khoronzhuk | 2c836bd | 2016-08-10 02:22:40 +0300 | [diff] [blame] | 2963 | cpdma_ctlr_destroy(cpsw->dma); |
Johan Hovold | a4e32b0 | 2016-11-17 17:40:00 +0100 | [diff] [blame] | 2964 | clean_dt_ret: |
| 2965 | cpsw_remove_dt(pdev); |
Johan Hovold | c46ab7e | 2016-11-17 17:39:58 +0100 | [diff] [blame] | 2966 | pm_runtime_put_sync(&pdev->dev); |
Daniel Mack | aa1a15e | 2013-09-21 00:50:38 +0530 | [diff] [blame] | 2967 | clean_runtime_disable_ret: |
Mugunthan V N | f150bd7 | 2012-07-17 08:09:50 +0000 | [diff] [blame] | 2968 | pm_runtime_disable(&pdev->dev); |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 2969 | return ret; |
| 2970 | } |
| 2971 | |
Bill Pemberton | 663e12e | 2012-12-03 09:23:45 -0500 | [diff] [blame] | 2972 | static int cpsw_remove(struct platform_device *pdev) |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 2973 | { |
Ivan Khoronzhuk | bfe5903 | 2019-06-12 00:49:03 +0300 | [diff] [blame] | 2974 | struct cpsw_common *cpsw = platform_get_drvdata(pdev); |
| 2975 | int i, ret; |
Grygorii Strashko | 8a0b6dc | 2016-07-28 20:50:35 +0300 | [diff] [blame] | 2976 | |
| 2977 | ret = pm_runtime_get_sync(&pdev->dev); |
| 2978 | if (ret < 0) { |
| 2979 | pm_runtime_put_noidle(&pdev->dev); |
| 2980 | return ret; |
| 2981 | } |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 2982 | |
Ivan Khoronzhuk | bfe5903 | 2019-06-12 00:49:03 +0300 | [diff] [blame] | 2983 | for (i = 0; i < cpsw->data.slaves; i++) |
| 2984 | if (cpsw->slaves[i].ndev) |
| 2985 | unregister_netdev(cpsw->slaves[i].ndev); |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 2986 | |
Grygorii Strashko | 8a2c9a5 | 2016-12-06 18:00:41 -0600 | [diff] [blame] | 2987 | cpts_release(cpsw->cpts); |
Ivan Khoronzhuk | 2c836bd | 2016-08-10 02:22:40 +0300 | [diff] [blame] | 2988 | cpdma_ctlr_destroy(cpsw->dma); |
Johan Hovold | a4e32b0 | 2016-11-17 17:40:00 +0100 | [diff] [blame] | 2989 | cpsw_remove_dt(pdev); |
Grygorii Strashko | 8a0b6dc | 2016-07-28 20:50:35 +0300 | [diff] [blame] | 2990 | pm_runtime_put_sync(&pdev->dev); |
| 2991 | pm_runtime_disable(&pdev->dev); |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 2992 | return 0; |
| 2993 | } |
| 2994 | |
Grygorii Strashko | 8963a50 | 2015-02-27 13:19:45 +0200 | [diff] [blame] | 2995 | #ifdef CONFIG_PM_SLEEP |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 2996 | static int cpsw_suspend(struct device *dev) |
| 2997 | { |
Keerthy | 2f9b0d9 | 2019-06-24 10:46:19 +0530 | [diff] [blame] | 2998 | struct cpsw_common *cpsw = dev_get_drvdata(dev); |
| 2999 | int i; |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 3000 | |
Keerthy | 2f9b0d9 | 2019-06-24 10:46:19 +0530 | [diff] [blame] | 3001 | for (i = 0; i < cpsw->data.slaves; i++) |
| 3002 | if (cpsw->slaves[i].ndev) |
Ivan Khoronzhuk | 606f399 | 2016-08-10 02:22:42 +0300 | [diff] [blame] | 3003 | if (netif_running(cpsw->slaves[i].ndev)) |
| 3004 | cpsw_ndo_stop(cpsw->slaves[i].ndev); |
Daniel Mack | 1e7a2e2 | 2013-11-15 08:29:16 +0100 | [diff] [blame] | 3005 | |
Mugunthan V N | 739683b | 2013-06-06 23:45:14 +0530 | [diff] [blame] | 3006 | /* Select sleep pin state */ |
Ivan Khoronzhuk | 56e31bd | 2016-08-10 02:22:38 +0300 | [diff] [blame] | 3007 | pinctrl_pm_select_sleep_state(dev); |
Mugunthan V N | 739683b | 2013-06-06 23:45:14 +0530 | [diff] [blame] | 3008 | |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 3009 | return 0; |
| 3010 | } |
| 3011 | |
| 3012 | static int cpsw_resume(struct device *dev) |
| 3013 | { |
Keerthy | 2f9b0d9 | 2019-06-24 10:46:19 +0530 | [diff] [blame] | 3014 | struct cpsw_common *cpsw = dev_get_drvdata(dev); |
| 3015 | int i; |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 3016 | |
Mugunthan V N | 739683b | 2013-06-06 23:45:14 +0530 | [diff] [blame] | 3017 | /* Select default pin state */ |
Ivan Khoronzhuk | 56e31bd | 2016-08-10 02:22:38 +0300 | [diff] [blame] | 3018 | pinctrl_pm_select_default_state(dev); |
Mugunthan V N | 739683b | 2013-06-06 23:45:14 +0530 | [diff] [blame] | 3019 | |
Grygorii Strashko | 4ccfd63 | 2016-11-29 16:27:03 -0600 | [diff] [blame] | 3020 | /* shut up ASSERT_RTNL() warning in netif_set_real_num_tx/rx_queues */ |
| 3021 | rtnl_lock(); |
Mugunthan V N | 618073e | 2014-09-11 22:52:38 +0530 | [diff] [blame] | 3022 | |
Keerthy | 2f9b0d9 | 2019-06-24 10:46:19 +0530 | [diff] [blame] | 3023 | for (i = 0; i < cpsw->data.slaves; i++) |
| 3024 | if (cpsw->slaves[i].ndev) |
Ivan Khoronzhuk | 606f399 | 2016-08-10 02:22:42 +0300 | [diff] [blame] | 3025 | if (netif_running(cpsw->slaves[i].ndev)) |
| 3026 | cpsw_ndo_open(cpsw->slaves[i].ndev); |
Keerthy | 2f9b0d9 | 2019-06-24 10:46:19 +0530 | [diff] [blame] | 3027 | |
Grygorii Strashko | 4ccfd63 | 2016-11-29 16:27:03 -0600 | [diff] [blame] | 3028 | rtnl_unlock(); |
| 3029 | |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 3030 | return 0; |
| 3031 | } |
Grygorii Strashko | 8963a50 | 2015-02-27 13:19:45 +0200 | [diff] [blame] | 3032 | #endif |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 3033 | |
Grygorii Strashko | 8963a50 | 2015-02-27 13:19:45 +0200 | [diff] [blame] | 3034 | static SIMPLE_DEV_PM_OPS(cpsw_pm_ops, cpsw_suspend, cpsw_resume); |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 3035 | |
| 3036 | static struct platform_driver cpsw_driver = { |
| 3037 | .driver = { |
| 3038 | .name = "cpsw", |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 3039 | .pm = &cpsw_pm_ops, |
Sachin Kamat | 1e5c76d | 2013-09-30 09:55:12 +0530 | [diff] [blame] | 3040 | .of_match_table = cpsw_of_mtable, |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 3041 | }, |
| 3042 | .probe = cpsw_probe, |
Bill Pemberton | 663e12e | 2012-12-03 09:23:45 -0500 | [diff] [blame] | 3043 | .remove = cpsw_remove, |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 3044 | }; |
| 3045 | |
Grygorii Strashko | 6fb3b6b5 | 2015-10-23 14:41:12 +0300 | [diff] [blame] | 3046 | module_platform_driver(cpsw_driver); |
Mugunthan V N | df82859 | 2012-03-18 20:17:54 +0000 | [diff] [blame] | 3047 | |
| 3048 | MODULE_LICENSE("GPL"); |
| 3049 | MODULE_AUTHOR("Cyril Chemparathy <cyril@ti.com>"); |
| 3050 | MODULE_AUTHOR("Mugunthan V N <mugunthanvnm@ti.com>"); |
| 3051 | MODULE_DESCRIPTION("TI CPSW Ethernet driver"); |