Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* Copyright (c) 2018, Sensor-Technik Wiedemann GmbH |
| 3 | * Copyright (c) 2018-2019, Vladimir Oltean <olteanv@gmail.com> |
| 4 | */ |
| 5 | |
| 6 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 7 | |
| 8 | #include <linux/delay.h> |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/printk.h> |
| 11 | #include <linux/spi/spi.h> |
| 12 | #include <linux/errno.h> |
| 13 | #include <linux/gpio/consumer.h> |
Vladimir Oltean | ad9f299 | 2019-05-02 23:23:38 +0300 | [diff] [blame] | 14 | #include <linux/phylink.h> |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 15 | #include <linux/of.h> |
| 16 | #include <linux/of_net.h> |
| 17 | #include <linux/of_mdio.h> |
| 18 | #include <linux/of_device.h> |
| 19 | #include <linux/netdev_features.h> |
| 20 | #include <linux/netdevice.h> |
| 21 | #include <linux/if_bridge.h> |
| 22 | #include <linux/if_ether.h> |
Vladimir Oltean | 227d07a | 2019-05-05 13:19:27 +0300 | [diff] [blame] | 23 | #include <linux/dsa/8021q.h> |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 24 | #include "sja1105.h" |
Vladimir Oltean | 317ab5b | 2019-09-15 05:00:02 +0300 | [diff] [blame] | 25 | #include "sja1105_tas.h" |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 26 | |
| 27 | static void sja1105_hw_reset(struct gpio_desc *gpio, unsigned int pulse_len, |
| 28 | unsigned int startup_delay) |
| 29 | { |
| 30 | gpiod_set_value_cansleep(gpio, 1); |
| 31 | /* Wait for minimum reset pulse length */ |
| 32 | msleep(pulse_len); |
| 33 | gpiod_set_value_cansleep(gpio, 0); |
| 34 | /* Wait until chip is ready after reset */ |
| 35 | msleep(startup_delay); |
| 36 | } |
| 37 | |
| 38 | static void |
| 39 | sja1105_port_allow_traffic(struct sja1105_l2_forwarding_entry *l2_fwd, |
| 40 | int from, int to, bool allow) |
| 41 | { |
| 42 | if (allow) { |
| 43 | l2_fwd[from].bc_domain |= BIT(to); |
| 44 | l2_fwd[from].reach_port |= BIT(to); |
| 45 | l2_fwd[from].fl_domain |= BIT(to); |
| 46 | } else { |
| 47 | l2_fwd[from].bc_domain &= ~BIT(to); |
| 48 | l2_fwd[from].reach_port &= ~BIT(to); |
| 49 | l2_fwd[from].fl_domain &= ~BIT(to); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /* Structure used to temporarily transport device tree |
| 54 | * settings into sja1105_setup |
| 55 | */ |
| 56 | struct sja1105_dt_port { |
| 57 | phy_interface_t phy_mode; |
| 58 | sja1105_mii_role_t role; |
| 59 | }; |
| 60 | |
| 61 | static int sja1105_init_mac_settings(struct sja1105_private *priv) |
| 62 | { |
| 63 | struct sja1105_mac_config_entry default_mac = { |
| 64 | /* Enable all 8 priority queues on egress. |
| 65 | * Every queue i holds top[i] - base[i] frames. |
| 66 | * Sum of top[i] - base[i] is 511 (max hardware limit). |
| 67 | */ |
| 68 | .top = {0x3F, 0x7F, 0xBF, 0xFF, 0x13F, 0x17F, 0x1BF, 0x1FF}, |
| 69 | .base = {0x0, 0x40, 0x80, 0xC0, 0x100, 0x140, 0x180, 0x1C0}, |
| 70 | .enabled = {true, true, true, true, true, true, true, true}, |
| 71 | /* Keep standard IFG of 12 bytes on egress. */ |
| 72 | .ifg = 0, |
| 73 | /* Always put the MAC speed in automatic mode, where it can be |
Vladimir Oltean | 1fd4a17 | 2019-06-08 16:03:42 +0300 | [diff] [blame] | 74 | * adjusted at runtime by PHYLINK. |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 75 | */ |
| 76 | .speed = SJA1105_SPEED_AUTO, |
| 77 | /* No static correction for 1-step 1588 events */ |
| 78 | .tp_delin = 0, |
| 79 | .tp_delout = 0, |
| 80 | /* Disable aging for critical TTEthernet traffic */ |
| 81 | .maxage = 0xFF, |
| 82 | /* Internal VLAN (pvid) to apply to untagged ingress */ |
| 83 | .vlanprio = 0, |
Vladimir Oltean | e3502b8 | 2019-06-26 02:39:35 +0300 | [diff] [blame] | 84 | .vlanid = 1, |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 85 | .ing_mirr = false, |
| 86 | .egr_mirr = false, |
| 87 | /* Don't drop traffic with other EtherType than ETH_P_IP */ |
| 88 | .drpnona664 = false, |
| 89 | /* Don't drop double-tagged traffic */ |
| 90 | .drpdtag = false, |
| 91 | /* Don't drop untagged traffic */ |
| 92 | .drpuntag = false, |
| 93 | /* Don't retag 802.1p (VID 0) traffic with the pvid */ |
| 94 | .retag = false, |
Vladimir Oltean | 640f763 | 2019-05-05 13:19:28 +0300 | [diff] [blame] | 95 | /* Disable learning and I/O on user ports by default - |
| 96 | * STP will enable it. |
| 97 | */ |
| 98 | .dyn_learn = false, |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 99 | .egress = false, |
| 100 | .ingress = false, |
| 101 | }; |
| 102 | struct sja1105_mac_config_entry *mac; |
| 103 | struct sja1105_table *table; |
| 104 | int i; |
| 105 | |
| 106 | table = &priv->static_config.tables[BLK_IDX_MAC_CONFIG]; |
| 107 | |
| 108 | /* Discard previous MAC Configuration Table */ |
| 109 | if (table->entry_count) { |
| 110 | kfree(table->entries); |
| 111 | table->entry_count = 0; |
| 112 | } |
| 113 | |
| 114 | table->entries = kcalloc(SJA1105_NUM_PORTS, |
| 115 | table->ops->unpacked_entry_size, GFP_KERNEL); |
| 116 | if (!table->entries) |
| 117 | return -ENOMEM; |
| 118 | |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 119 | table->entry_count = SJA1105_NUM_PORTS; |
| 120 | |
| 121 | mac = table->entries; |
| 122 | |
Vladimir Oltean | 640f763 | 2019-05-05 13:19:28 +0300 | [diff] [blame] | 123 | for (i = 0; i < SJA1105_NUM_PORTS; i++) { |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 124 | mac[i] = default_mac; |
Vladimir Oltean | 640f763 | 2019-05-05 13:19:28 +0300 | [diff] [blame] | 125 | if (i == dsa_upstream_port(priv->ds, i)) { |
| 126 | /* STP doesn't get called for CPU port, so we need to |
| 127 | * set the I/O parameters statically. |
| 128 | */ |
| 129 | mac[i].dyn_learn = true; |
| 130 | mac[i].ingress = true; |
| 131 | mac[i].egress = true; |
| 132 | } |
| 133 | } |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | static int sja1105_init_mii_settings(struct sja1105_private *priv, |
| 139 | struct sja1105_dt_port *ports) |
| 140 | { |
| 141 | struct device *dev = &priv->spidev->dev; |
| 142 | struct sja1105_xmii_params_entry *mii; |
| 143 | struct sja1105_table *table; |
| 144 | int i; |
| 145 | |
| 146 | table = &priv->static_config.tables[BLK_IDX_XMII_PARAMS]; |
| 147 | |
| 148 | /* Discard previous xMII Mode Parameters Table */ |
| 149 | if (table->entry_count) { |
| 150 | kfree(table->entries); |
| 151 | table->entry_count = 0; |
| 152 | } |
| 153 | |
| 154 | table->entries = kcalloc(SJA1105_MAX_XMII_PARAMS_COUNT, |
| 155 | table->ops->unpacked_entry_size, GFP_KERNEL); |
| 156 | if (!table->entries) |
| 157 | return -ENOMEM; |
| 158 | |
Vladimir Oltean | 1fd4a17 | 2019-06-08 16:03:42 +0300 | [diff] [blame] | 159 | /* Override table based on PHYLINK DT bindings */ |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 160 | table->entry_count = SJA1105_MAX_XMII_PARAMS_COUNT; |
| 161 | |
| 162 | mii = table->entries; |
| 163 | |
| 164 | for (i = 0; i < SJA1105_NUM_PORTS; i++) { |
| 165 | switch (ports[i].phy_mode) { |
| 166 | case PHY_INTERFACE_MODE_MII: |
| 167 | mii->xmii_mode[i] = XMII_MODE_MII; |
| 168 | break; |
| 169 | case PHY_INTERFACE_MODE_RMII: |
| 170 | mii->xmii_mode[i] = XMII_MODE_RMII; |
| 171 | break; |
| 172 | case PHY_INTERFACE_MODE_RGMII: |
| 173 | case PHY_INTERFACE_MODE_RGMII_ID: |
| 174 | case PHY_INTERFACE_MODE_RGMII_RXID: |
| 175 | case PHY_INTERFACE_MODE_RGMII_TXID: |
| 176 | mii->xmii_mode[i] = XMII_MODE_RGMII; |
| 177 | break; |
| 178 | default: |
| 179 | dev_err(dev, "Unsupported PHY mode %s!\n", |
| 180 | phy_modes(ports[i].phy_mode)); |
| 181 | } |
| 182 | |
| 183 | mii->phy_mac[i] = ports[i].role; |
| 184 | } |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | static int sja1105_init_static_fdb(struct sja1105_private *priv) |
| 189 | { |
| 190 | struct sja1105_table *table; |
| 191 | |
| 192 | table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; |
| 193 | |
Vladimir Oltean | 291d1e7 | 2019-05-02 23:23:31 +0300 | [diff] [blame] | 194 | /* We only populate the FDB table through dynamic |
| 195 | * L2 Address Lookup entries |
| 196 | */ |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 197 | if (table->entry_count) { |
| 198 | kfree(table->entries); |
| 199 | table->entry_count = 0; |
| 200 | } |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | static int sja1105_init_l2_lookup_params(struct sja1105_private *priv) |
| 205 | { |
| 206 | struct sja1105_table *table; |
Vladimir Oltean | 6c56e16 | 2019-06-26 02:39:37 +0300 | [diff] [blame] | 207 | u64 max_fdb_entries = SJA1105_MAX_L2_LOOKUP_COUNT / SJA1105_NUM_PORTS; |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 208 | struct sja1105_l2_lookup_params_entry default_l2_lookup_params = { |
Vladimir Oltean | 8456721 | 2019-05-02 23:23:36 +0300 | [diff] [blame] | 209 | /* Learned FDB entries are forgotten after 300 seconds */ |
| 210 | .maxage = SJA1105_AGEING_TIME_MS(300000), |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 211 | /* All entries within a FDB bin are available for learning */ |
| 212 | .dyn_tbsz = SJA1105ET_FDB_BIN_SIZE, |
Vladimir Oltean | 1da7382 | 2019-06-03 00:15:45 +0300 | [diff] [blame] | 213 | /* And the P/Q/R/S equivalent setting: */ |
| 214 | .start_dynspc = 0, |
Vladimir Oltean | 6c56e16 | 2019-06-26 02:39:37 +0300 | [diff] [blame] | 215 | .maxaddrp = {max_fdb_entries, max_fdb_entries, max_fdb_entries, |
| 216 | max_fdb_entries, max_fdb_entries, }, |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 217 | /* 2^8 + 2^5 + 2^3 + 2^2 + 2^1 + 1 in Koopman notation */ |
| 218 | .poly = 0x97, |
| 219 | /* This selects between Independent VLAN Learning (IVL) and |
| 220 | * Shared VLAN Learning (SVL) |
| 221 | */ |
Vladimir Oltean | 6d7c7d9 | 2019-08-05 01:38:44 +0300 | [diff] [blame] | 222 | .shared_learn = true, |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 223 | /* Don't discard management traffic based on ENFPORT - |
| 224 | * we don't perform SMAC port enforcement anyway, so |
| 225 | * what we are setting here doesn't matter. |
| 226 | */ |
| 227 | .no_enf_hostprt = false, |
| 228 | /* Don't learn SMAC for mac_fltres1 and mac_fltres0. |
| 229 | * Maybe correlate with no_linklocal_learn from bridge driver? |
| 230 | */ |
| 231 | .no_mgmt_learn = true, |
Vladimir Oltean | 1da7382 | 2019-06-03 00:15:45 +0300 | [diff] [blame] | 232 | /* P/Q/R/S only */ |
| 233 | .use_static = true, |
| 234 | /* Dynamically learned FDB entries can overwrite other (older) |
| 235 | * dynamic FDB entries |
| 236 | */ |
| 237 | .owr_dyn = true, |
| 238 | .drpnolearn = true, |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 239 | }; |
| 240 | |
| 241 | table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS]; |
| 242 | |
| 243 | if (table->entry_count) { |
| 244 | kfree(table->entries); |
| 245 | table->entry_count = 0; |
| 246 | } |
| 247 | |
| 248 | table->entries = kcalloc(SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT, |
| 249 | table->ops->unpacked_entry_size, GFP_KERNEL); |
| 250 | if (!table->entries) |
| 251 | return -ENOMEM; |
| 252 | |
| 253 | table->entry_count = SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT; |
| 254 | |
| 255 | /* This table only has a single entry */ |
| 256 | ((struct sja1105_l2_lookup_params_entry *)table->entries)[0] = |
| 257 | default_l2_lookup_params; |
| 258 | |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | static int sja1105_init_static_vlan(struct sja1105_private *priv) |
| 263 | { |
| 264 | struct sja1105_table *table; |
| 265 | struct sja1105_vlan_lookup_entry pvid = { |
| 266 | .ving_mirr = 0, |
| 267 | .vegr_mirr = 0, |
| 268 | .vmemb_port = 0, |
| 269 | .vlan_bc = 0, |
| 270 | .tag_port = 0, |
Vladimir Oltean | e3502b8 | 2019-06-26 02:39:35 +0300 | [diff] [blame] | 271 | .vlanid = 1, |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 272 | }; |
| 273 | int i; |
| 274 | |
| 275 | table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP]; |
| 276 | |
Vladimir Oltean | e3502b8 | 2019-06-26 02:39:35 +0300 | [diff] [blame] | 277 | /* The static VLAN table will only contain the initial pvid of 1. |
Vladimir Oltean | 6666ceb | 2019-05-02 23:23:34 +0300 | [diff] [blame] | 278 | * All other VLANs are to be configured through dynamic entries, |
| 279 | * and kept in the static configuration table as backing memory. |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 280 | */ |
| 281 | if (table->entry_count) { |
| 282 | kfree(table->entries); |
| 283 | table->entry_count = 0; |
| 284 | } |
| 285 | |
| 286 | table->entries = kcalloc(1, table->ops->unpacked_entry_size, |
| 287 | GFP_KERNEL); |
| 288 | if (!table->entries) |
| 289 | return -ENOMEM; |
| 290 | |
| 291 | table->entry_count = 1; |
| 292 | |
Vladimir Oltean | e3502b8 | 2019-06-26 02:39:35 +0300 | [diff] [blame] | 293 | /* VLAN 1: all DT-defined ports are members; no restrictions on |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 294 | * forwarding; always transmit priority-tagged frames as untagged. |
| 295 | */ |
| 296 | for (i = 0; i < SJA1105_NUM_PORTS; i++) { |
| 297 | pvid.vmemb_port |= BIT(i); |
| 298 | pvid.vlan_bc |= BIT(i); |
| 299 | pvid.tag_port &= ~BIT(i); |
| 300 | } |
| 301 | |
| 302 | ((struct sja1105_vlan_lookup_entry *)table->entries)[0] = pvid; |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | static int sja1105_init_l2_forwarding(struct sja1105_private *priv) |
| 307 | { |
| 308 | struct sja1105_l2_forwarding_entry *l2fwd; |
| 309 | struct sja1105_table *table; |
| 310 | int i, j; |
| 311 | |
| 312 | table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING]; |
| 313 | |
| 314 | if (table->entry_count) { |
| 315 | kfree(table->entries); |
| 316 | table->entry_count = 0; |
| 317 | } |
| 318 | |
| 319 | table->entries = kcalloc(SJA1105_MAX_L2_FORWARDING_COUNT, |
| 320 | table->ops->unpacked_entry_size, GFP_KERNEL); |
| 321 | if (!table->entries) |
| 322 | return -ENOMEM; |
| 323 | |
| 324 | table->entry_count = SJA1105_MAX_L2_FORWARDING_COUNT; |
| 325 | |
| 326 | l2fwd = table->entries; |
| 327 | |
| 328 | /* First 5 entries define the forwarding rules */ |
| 329 | for (i = 0; i < SJA1105_NUM_PORTS; i++) { |
| 330 | unsigned int upstream = dsa_upstream_port(priv->ds, i); |
| 331 | |
| 332 | for (j = 0; j < SJA1105_NUM_TC; j++) |
| 333 | l2fwd[i].vlan_pmap[j] = j; |
| 334 | |
| 335 | if (i == upstream) |
| 336 | continue; |
| 337 | |
| 338 | sja1105_port_allow_traffic(l2fwd, i, upstream, true); |
| 339 | sja1105_port_allow_traffic(l2fwd, upstream, i, true); |
| 340 | } |
| 341 | /* Next 8 entries define VLAN PCP mapping from ingress to egress. |
| 342 | * Create a one-to-one mapping. |
| 343 | */ |
| 344 | for (i = 0; i < SJA1105_NUM_TC; i++) |
| 345 | for (j = 0; j < SJA1105_NUM_PORTS; j++) |
| 346 | l2fwd[SJA1105_NUM_PORTS + i].vlan_pmap[j] = i; |
| 347 | |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | static int sja1105_init_l2_forwarding_params(struct sja1105_private *priv) |
| 352 | { |
| 353 | struct sja1105_l2_forwarding_params_entry default_l2fwd_params = { |
| 354 | /* Disallow dynamic reconfiguration of vlan_pmap */ |
| 355 | .max_dynp = 0, |
| 356 | /* Use a single memory partition for all ingress queues */ |
| 357 | .part_spc = { SJA1105_MAX_FRAME_MEMORY, 0, 0, 0, 0, 0, 0, 0 }, |
| 358 | }; |
| 359 | struct sja1105_table *table; |
| 360 | |
| 361 | table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS]; |
| 362 | |
| 363 | if (table->entry_count) { |
| 364 | kfree(table->entries); |
| 365 | table->entry_count = 0; |
| 366 | } |
| 367 | |
| 368 | table->entries = kcalloc(SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT, |
| 369 | table->ops->unpacked_entry_size, GFP_KERNEL); |
| 370 | if (!table->entries) |
| 371 | return -ENOMEM; |
| 372 | |
| 373 | table->entry_count = SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT; |
| 374 | |
| 375 | /* This table only has a single entry */ |
| 376 | ((struct sja1105_l2_forwarding_params_entry *)table->entries)[0] = |
| 377 | default_l2fwd_params; |
| 378 | |
| 379 | return 0; |
| 380 | } |
| 381 | |
| 382 | static int sja1105_init_general_params(struct sja1105_private *priv) |
| 383 | { |
| 384 | struct sja1105_general_params_entry default_general_params = { |
Vladimir Oltean | 511e6ca | 2019-10-04 03:33:47 +0300 | [diff] [blame] | 385 | /* Allow dynamic changing of the mirror port */ |
| 386 | .mirr_ptacu = true, |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 387 | .switchid = priv->ds->index, |
Vladimir Oltean | 5f06c63 | 2019-09-15 05:00:01 +0300 | [diff] [blame] | 388 | /* Priority queue for link-local management frames |
| 389 | * (both ingress to and egress from CPU - PTP, STP etc) |
| 390 | */ |
Vladimir Oltean | 08fde09 | 2019-06-08 15:04:41 +0300 | [diff] [blame] | 391 | .hostprio = 7, |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 392 | .mac_fltres1 = SJA1105_LINKLOCAL_FILTER_A, |
| 393 | .mac_flt1 = SJA1105_LINKLOCAL_FILTER_A_MASK, |
Vladimir Oltean | 4282446 | 2019-06-08 15:04:32 +0300 | [diff] [blame] | 394 | .incl_srcpt1 = false, |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 395 | .send_meta1 = false, |
| 396 | .mac_fltres0 = SJA1105_LINKLOCAL_FILTER_B, |
| 397 | .mac_flt0 = SJA1105_LINKLOCAL_FILTER_B_MASK, |
Vladimir Oltean | 4282446 | 2019-06-08 15:04:32 +0300 | [diff] [blame] | 398 | .incl_srcpt0 = false, |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 399 | .send_meta0 = false, |
| 400 | /* The destination for traffic matching mac_fltres1 and |
| 401 | * mac_fltres0 on all ports except host_port. Such traffic |
| 402 | * receieved on host_port itself would be dropped, except |
| 403 | * by installing a temporary 'management route' |
| 404 | */ |
| 405 | .host_port = dsa_upstream_port(priv->ds, 0), |
Vladimir Oltean | 511e6ca | 2019-10-04 03:33:47 +0300 | [diff] [blame] | 406 | /* Default to an invalid value */ |
| 407 | .mirr_port = SJA1105_NUM_PORTS, |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 408 | /* Link-local traffic received on casc_port will be forwarded |
| 409 | * to host_port without embedding the source port and device ID |
| 410 | * info in the destination MAC address (presumably because it |
| 411 | * is a cascaded port and a downstream SJA switch already did |
| 412 | * that). Default to an invalid port (to disable the feature) |
| 413 | * and overwrite this if we find any DSA (cascaded) ports. |
| 414 | */ |
| 415 | .casc_port = SJA1105_NUM_PORTS, |
| 416 | /* No TTEthernet */ |
| 417 | .vllupformat = 0, |
| 418 | .vlmarker = 0, |
| 419 | .vlmask = 0, |
| 420 | /* Only update correctionField for 1-step PTP (L2 transport) */ |
| 421 | .ignore2stf = 0, |
Vladimir Oltean | 6666ceb | 2019-05-02 23:23:34 +0300 | [diff] [blame] | 422 | /* Forcefully disable VLAN filtering by telling |
| 423 | * the switch that VLAN has a different EtherType. |
| 424 | */ |
| 425 | .tpid = ETH_P_SJA1105, |
| 426 | .tpid2 = ETH_P_SJA1105, |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 427 | }; |
| 428 | struct sja1105_table *table; |
Vladimir Oltean | 227d07a | 2019-05-05 13:19:27 +0300 | [diff] [blame] | 429 | int i, k = 0; |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 430 | |
Vladimir Oltean | 227d07a | 2019-05-05 13:19:27 +0300 | [diff] [blame] | 431 | for (i = 0; i < SJA1105_NUM_PORTS; i++) { |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 432 | if (dsa_is_dsa_port(priv->ds, i)) |
| 433 | default_general_params.casc_port = i; |
Vladimir Oltean | 227d07a | 2019-05-05 13:19:27 +0300 | [diff] [blame] | 434 | else if (dsa_is_user_port(priv->ds, i)) |
| 435 | priv->ports[i].mgmt_slot = k++; |
| 436 | } |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 437 | |
| 438 | table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; |
| 439 | |
| 440 | if (table->entry_count) { |
| 441 | kfree(table->entries); |
| 442 | table->entry_count = 0; |
| 443 | } |
| 444 | |
| 445 | table->entries = kcalloc(SJA1105_MAX_GENERAL_PARAMS_COUNT, |
| 446 | table->ops->unpacked_entry_size, GFP_KERNEL); |
| 447 | if (!table->entries) |
| 448 | return -ENOMEM; |
| 449 | |
| 450 | table->entry_count = SJA1105_MAX_GENERAL_PARAMS_COUNT; |
| 451 | |
| 452 | /* This table only has a single entry */ |
| 453 | ((struct sja1105_general_params_entry *)table->entries)[0] = |
| 454 | default_general_params; |
| 455 | |
| 456 | return 0; |
| 457 | } |
| 458 | |
| 459 | #define SJA1105_RATE_MBPS(speed) (((speed) * 64000) / 1000) |
| 460 | |
Vladimir Oltean | 09c1b41 | 2019-10-01 22:17:59 +0300 | [diff] [blame] | 461 | static void sja1105_setup_policer(struct sja1105_l2_policing_entry *policing, |
| 462 | int index) |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 463 | { |
| 464 | policing[index].sharindx = index; |
| 465 | policing[index].smax = 65535; /* Burst size in bytes */ |
| 466 | policing[index].rate = SJA1105_RATE_MBPS(1000); |
| 467 | policing[index].maxlen = ETH_FRAME_LEN + VLAN_HLEN + ETH_FCS_LEN; |
| 468 | policing[index].partition = 0; |
| 469 | } |
| 470 | |
| 471 | static int sja1105_init_l2_policing(struct sja1105_private *priv) |
| 472 | { |
| 473 | struct sja1105_l2_policing_entry *policing; |
| 474 | struct sja1105_table *table; |
| 475 | int i, j, k; |
| 476 | |
| 477 | table = &priv->static_config.tables[BLK_IDX_L2_POLICING]; |
| 478 | |
| 479 | /* Discard previous L2 Policing Table */ |
| 480 | if (table->entry_count) { |
| 481 | kfree(table->entries); |
| 482 | table->entry_count = 0; |
| 483 | } |
| 484 | |
| 485 | table->entries = kcalloc(SJA1105_MAX_L2_POLICING_COUNT, |
| 486 | table->ops->unpacked_entry_size, GFP_KERNEL); |
| 487 | if (!table->entries) |
| 488 | return -ENOMEM; |
| 489 | |
| 490 | table->entry_count = SJA1105_MAX_L2_POLICING_COUNT; |
| 491 | |
| 492 | policing = table->entries; |
| 493 | |
| 494 | /* k sweeps through all unicast policers (0-39). |
| 495 | * bcast sweeps through policers 40-44. |
| 496 | */ |
| 497 | for (i = 0, k = 0; i < SJA1105_NUM_PORTS; i++) { |
| 498 | int bcast = (SJA1105_NUM_PORTS * SJA1105_NUM_TC) + i; |
| 499 | |
| 500 | for (j = 0; j < SJA1105_NUM_TC; j++, k++) |
| 501 | sja1105_setup_policer(policing, k); |
| 502 | |
| 503 | /* Set up this port's policer for broadcast traffic */ |
| 504 | sja1105_setup_policer(policing, bcast); |
| 505 | } |
| 506 | return 0; |
| 507 | } |
| 508 | |
| 509 | static int sja1105_static_config_load(struct sja1105_private *priv, |
| 510 | struct sja1105_dt_port *ports) |
| 511 | { |
| 512 | int rc; |
| 513 | |
| 514 | sja1105_static_config_free(&priv->static_config); |
| 515 | rc = sja1105_static_config_init(&priv->static_config, |
| 516 | priv->info->static_ops, |
| 517 | priv->info->device_id); |
| 518 | if (rc) |
| 519 | return rc; |
| 520 | |
| 521 | /* Build static configuration */ |
| 522 | rc = sja1105_init_mac_settings(priv); |
| 523 | if (rc < 0) |
| 524 | return rc; |
| 525 | rc = sja1105_init_mii_settings(priv, ports); |
| 526 | if (rc < 0) |
| 527 | return rc; |
| 528 | rc = sja1105_init_static_fdb(priv); |
| 529 | if (rc < 0) |
| 530 | return rc; |
| 531 | rc = sja1105_init_static_vlan(priv); |
| 532 | if (rc < 0) |
| 533 | return rc; |
| 534 | rc = sja1105_init_l2_lookup_params(priv); |
| 535 | if (rc < 0) |
| 536 | return rc; |
| 537 | rc = sja1105_init_l2_forwarding(priv); |
| 538 | if (rc < 0) |
| 539 | return rc; |
| 540 | rc = sja1105_init_l2_forwarding_params(priv); |
| 541 | if (rc < 0) |
| 542 | return rc; |
| 543 | rc = sja1105_init_l2_policing(priv); |
| 544 | if (rc < 0) |
| 545 | return rc; |
| 546 | rc = sja1105_init_general_params(priv); |
| 547 | if (rc < 0) |
| 548 | return rc; |
| 549 | |
| 550 | /* Send initial configuration to hardware via SPI */ |
| 551 | return sja1105_static_config_upload(priv); |
| 552 | } |
| 553 | |
Vladimir Oltean | f5b8631 | 2019-05-02 23:23:32 +0300 | [diff] [blame] | 554 | static int sja1105_parse_rgmii_delays(struct sja1105_private *priv, |
| 555 | const struct sja1105_dt_port *ports) |
| 556 | { |
| 557 | int i; |
| 558 | |
| 559 | for (i = 0; i < SJA1105_NUM_PORTS; i++) { |
| 560 | if (ports->role == XMII_MAC) |
| 561 | continue; |
| 562 | |
| 563 | if (ports->phy_mode == PHY_INTERFACE_MODE_RGMII_RXID || |
| 564 | ports->phy_mode == PHY_INTERFACE_MODE_RGMII_ID) |
| 565 | priv->rgmii_rx_delay[i] = true; |
| 566 | |
| 567 | if (ports->phy_mode == PHY_INTERFACE_MODE_RGMII_TXID || |
| 568 | ports->phy_mode == PHY_INTERFACE_MODE_RGMII_ID) |
| 569 | priv->rgmii_tx_delay[i] = true; |
| 570 | |
| 571 | if ((priv->rgmii_rx_delay[i] || priv->rgmii_tx_delay[i]) && |
| 572 | !priv->info->setup_rgmii_delay) |
| 573 | return -EINVAL; |
| 574 | } |
| 575 | return 0; |
| 576 | } |
| 577 | |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 578 | static int sja1105_parse_ports_node(struct sja1105_private *priv, |
| 579 | struct sja1105_dt_port *ports, |
| 580 | struct device_node *ports_node) |
| 581 | { |
| 582 | struct device *dev = &priv->spidev->dev; |
| 583 | struct device_node *child; |
| 584 | |
| 585 | for_each_child_of_node(ports_node, child) { |
| 586 | struct device_node *phy_node; |
| 587 | int phy_mode; |
| 588 | u32 index; |
| 589 | |
| 590 | /* Get switch port number from DT */ |
| 591 | if (of_property_read_u32(child, "reg", &index) < 0) { |
| 592 | dev_err(dev, "Port number not defined in device tree " |
| 593 | "(property \"reg\")\n"); |
Nishka Dasgupta | 7ba771e | 2019-07-23 16:14:48 +0530 | [diff] [blame] | 594 | of_node_put(child); |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 595 | return -ENODEV; |
| 596 | } |
| 597 | |
| 598 | /* Get PHY mode from DT */ |
| 599 | phy_mode = of_get_phy_mode(child); |
| 600 | if (phy_mode < 0) { |
| 601 | dev_err(dev, "Failed to read phy-mode or " |
| 602 | "phy-interface-type property for port %d\n", |
| 603 | index); |
Nishka Dasgupta | 7ba771e | 2019-07-23 16:14:48 +0530 | [diff] [blame] | 604 | of_node_put(child); |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 605 | return -ENODEV; |
| 606 | } |
| 607 | ports[index].phy_mode = phy_mode; |
| 608 | |
| 609 | phy_node = of_parse_phandle(child, "phy-handle", 0); |
| 610 | if (!phy_node) { |
| 611 | if (!of_phy_is_fixed_link(child)) { |
| 612 | dev_err(dev, "phy-handle or fixed-link " |
| 613 | "properties missing!\n"); |
Nishka Dasgupta | 7ba771e | 2019-07-23 16:14:48 +0530 | [diff] [blame] | 614 | of_node_put(child); |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 615 | return -ENODEV; |
| 616 | } |
| 617 | /* phy-handle is missing, but fixed-link isn't. |
| 618 | * So it's a fixed link. Default to PHY role. |
| 619 | */ |
| 620 | ports[index].role = XMII_PHY; |
| 621 | } else { |
| 622 | /* phy-handle present => put port in MAC role */ |
| 623 | ports[index].role = XMII_MAC; |
| 624 | of_node_put(phy_node); |
| 625 | } |
| 626 | |
| 627 | /* The MAC/PHY role can be overridden with explicit bindings */ |
| 628 | if (of_property_read_bool(child, "sja1105,role-mac")) |
| 629 | ports[index].role = XMII_MAC; |
| 630 | else if (of_property_read_bool(child, "sja1105,role-phy")) |
| 631 | ports[index].role = XMII_PHY; |
| 632 | } |
| 633 | |
| 634 | return 0; |
| 635 | } |
| 636 | |
| 637 | static int sja1105_parse_dt(struct sja1105_private *priv, |
| 638 | struct sja1105_dt_port *ports) |
| 639 | { |
| 640 | struct device *dev = &priv->spidev->dev; |
| 641 | struct device_node *switch_node = dev->of_node; |
| 642 | struct device_node *ports_node; |
| 643 | int rc; |
| 644 | |
| 645 | ports_node = of_get_child_by_name(switch_node, "ports"); |
| 646 | if (!ports_node) { |
| 647 | dev_err(dev, "Incorrect bindings: absent \"ports\" node\n"); |
| 648 | return -ENODEV; |
| 649 | } |
| 650 | |
| 651 | rc = sja1105_parse_ports_node(priv, ports, ports_node); |
| 652 | of_node_put(ports_node); |
| 653 | |
| 654 | return rc; |
| 655 | } |
| 656 | |
Vladimir Oltean | c44d053 | 2019-06-08 16:03:41 +0300 | [diff] [blame] | 657 | /* Convert link speed from SJA1105 to ethtool encoding */ |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 658 | static int sja1105_speed[] = { |
Vladimir Oltean | c44d053 | 2019-06-08 16:03:41 +0300 | [diff] [blame] | 659 | [SJA1105_SPEED_AUTO] = SPEED_UNKNOWN, |
| 660 | [SJA1105_SPEED_10MBPS] = SPEED_10, |
| 661 | [SJA1105_SPEED_100MBPS] = SPEED_100, |
| 662 | [SJA1105_SPEED_1000MBPS] = SPEED_1000, |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 663 | }; |
| 664 | |
Vladimir Oltean | 8400cff | 2019-06-08 16:03:44 +0300 | [diff] [blame] | 665 | /* Set link speed in the MAC configuration for a specific port. */ |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 666 | static int sja1105_adjust_port_config(struct sja1105_private *priv, int port, |
Vladimir Oltean | 8400cff | 2019-06-08 16:03:44 +0300 | [diff] [blame] | 667 | int speed_mbps) |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 668 | { |
| 669 | struct sja1105_xmii_params_entry *mii; |
| 670 | struct sja1105_mac_config_entry *mac; |
| 671 | struct device *dev = priv->ds->dev; |
| 672 | sja1105_phy_interface_t phy_mode; |
| 673 | sja1105_speed_t speed; |
| 674 | int rc; |
| 675 | |
Vladimir Oltean | 8400cff | 2019-06-08 16:03:44 +0300 | [diff] [blame] | 676 | /* On P/Q/R/S, one can read from the device via the MAC reconfiguration |
| 677 | * tables. On E/T, MAC reconfig tables are not readable, only writable. |
| 678 | * We have to *know* what the MAC looks like. For the sake of keeping |
| 679 | * the code common, we'll use the static configuration tables as a |
| 680 | * reasonable approximation for both E/T and P/Q/R/S. |
| 681 | */ |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 682 | mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; |
Vladimir Oltean | 8400cff | 2019-06-08 16:03:44 +0300 | [diff] [blame] | 683 | mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries; |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 684 | |
Vladimir Oltean | f4cfcfb | 2019-06-03 02:31:37 +0300 | [diff] [blame] | 685 | switch (speed_mbps) { |
Vladimir Oltean | c44d053 | 2019-06-08 16:03:41 +0300 | [diff] [blame] | 686 | case SPEED_UNKNOWN: |
Vladimir Oltean | a979a0a | 2019-06-28 00:46:35 +0300 | [diff] [blame] | 687 | /* PHYLINK called sja1105_mac_config() to inform us about |
| 688 | * the state->interface, but AN has not completed and the |
| 689 | * speed is not yet valid. UM10944.pdf says that setting |
| 690 | * SJA1105_SPEED_AUTO at runtime disables the port, so that is |
| 691 | * ok for power consumption in case AN will never complete - |
| 692 | * otherwise PHYLINK should come back with a new update. |
| 693 | */ |
Vladimir Oltean | f4cfcfb | 2019-06-03 02:31:37 +0300 | [diff] [blame] | 694 | speed = SJA1105_SPEED_AUTO; |
| 695 | break; |
Vladimir Oltean | c44d053 | 2019-06-08 16:03:41 +0300 | [diff] [blame] | 696 | case SPEED_10: |
Vladimir Oltean | f4cfcfb | 2019-06-03 02:31:37 +0300 | [diff] [blame] | 697 | speed = SJA1105_SPEED_10MBPS; |
| 698 | break; |
Vladimir Oltean | c44d053 | 2019-06-08 16:03:41 +0300 | [diff] [blame] | 699 | case SPEED_100: |
Vladimir Oltean | f4cfcfb | 2019-06-03 02:31:37 +0300 | [diff] [blame] | 700 | speed = SJA1105_SPEED_100MBPS; |
| 701 | break; |
Vladimir Oltean | c44d053 | 2019-06-08 16:03:41 +0300 | [diff] [blame] | 702 | case SPEED_1000: |
Vladimir Oltean | f4cfcfb | 2019-06-03 02:31:37 +0300 | [diff] [blame] | 703 | speed = SJA1105_SPEED_1000MBPS; |
| 704 | break; |
| 705 | default: |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 706 | dev_err(dev, "Invalid speed %iMbps\n", speed_mbps); |
| 707 | return -EINVAL; |
| 708 | } |
| 709 | |
Vladimir Oltean | 8400cff | 2019-06-08 16:03:44 +0300 | [diff] [blame] | 710 | /* Overwrite SJA1105_SPEED_AUTO from the static MAC configuration |
| 711 | * table, since this will be used for the clocking setup, and we no |
| 712 | * longer need to store it in the static config (already told hardware |
| 713 | * we want auto during upload phase). |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 714 | */ |
Vladimir Oltean | f4cfcfb | 2019-06-03 02:31:37 +0300 | [diff] [blame] | 715 | mac[port].speed = speed; |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 716 | |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 717 | /* Write to the dynamic reconfiguration tables */ |
Vladimir Oltean | 8400cff | 2019-06-08 16:03:44 +0300 | [diff] [blame] | 718 | rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, |
| 719 | &mac[port], true); |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 720 | if (rc < 0) { |
| 721 | dev_err(dev, "Failed to write MAC config: %d\n", rc); |
| 722 | return rc; |
| 723 | } |
| 724 | |
| 725 | /* Reconfigure the PLLs for the RGMII interfaces (required 125 MHz at |
| 726 | * gigabit, 25 MHz at 100 Mbps and 2.5 MHz at 10 Mbps). For MII and |
| 727 | * RMII no change of the clock setup is required. Actually, changing |
| 728 | * the clock setup does interrupt the clock signal for a certain time |
| 729 | * which causes trouble for all PHYs relying on this signal. |
| 730 | */ |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 731 | phy_mode = mii->xmii_mode[port]; |
| 732 | if (phy_mode != XMII_MODE_RGMII) |
| 733 | return 0; |
| 734 | |
| 735 | return sja1105_clocking_setup_port(priv, port); |
| 736 | } |
| 737 | |
Vladimir Oltean | 3971022 | 2019-06-28 00:46:36 +0300 | [diff] [blame] | 738 | /* The SJA1105 MAC programming model is through the static config (the xMII |
| 739 | * Mode table cannot be dynamically reconfigured), and we have to program |
| 740 | * that early (earlier than PHYLINK calls us, anyway). |
| 741 | * So just error out in case the connected PHY attempts to change the initial |
| 742 | * system interface MII protocol from what is defined in the DT, at least for |
| 743 | * now. |
| 744 | */ |
| 745 | static bool sja1105_phy_mode_mismatch(struct sja1105_private *priv, int port, |
| 746 | phy_interface_t interface) |
| 747 | { |
| 748 | struct sja1105_xmii_params_entry *mii; |
| 749 | sja1105_phy_interface_t phy_mode; |
| 750 | |
| 751 | mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries; |
| 752 | phy_mode = mii->xmii_mode[port]; |
| 753 | |
| 754 | switch (interface) { |
| 755 | case PHY_INTERFACE_MODE_MII: |
| 756 | return (phy_mode != XMII_MODE_MII); |
| 757 | case PHY_INTERFACE_MODE_RMII: |
| 758 | return (phy_mode != XMII_MODE_RMII); |
| 759 | case PHY_INTERFACE_MODE_RGMII: |
| 760 | case PHY_INTERFACE_MODE_RGMII_ID: |
| 761 | case PHY_INTERFACE_MODE_RGMII_RXID: |
| 762 | case PHY_INTERFACE_MODE_RGMII_TXID: |
| 763 | return (phy_mode != XMII_MODE_RGMII); |
| 764 | default: |
| 765 | return true; |
| 766 | } |
| 767 | } |
| 768 | |
Vladimir Oltean | af7cd03 | 2019-05-28 20:38:17 +0300 | [diff] [blame] | 769 | static void sja1105_mac_config(struct dsa_switch *ds, int port, |
| 770 | unsigned int link_an_mode, |
| 771 | const struct phylink_link_state *state) |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 772 | { |
| 773 | struct sja1105_private *priv = ds->priv; |
| 774 | |
Vladimir Oltean | 3971022 | 2019-06-28 00:46:36 +0300 | [diff] [blame] | 775 | if (sja1105_phy_mode_mismatch(priv, port, state->interface)) |
| 776 | return; |
| 777 | |
Vladimir Oltean | 9f97157 | 2019-06-28 00:46:37 +0300 | [diff] [blame] | 778 | if (link_an_mode == MLO_AN_INBAND) { |
| 779 | dev_err(ds->dev, "In-band AN not supported!\n"); |
| 780 | return; |
| 781 | } |
| 782 | |
Vladimir Oltean | 8400cff | 2019-06-08 16:03:44 +0300 | [diff] [blame] | 783 | sja1105_adjust_port_config(priv, port, state->speed); |
| 784 | } |
| 785 | |
| 786 | static void sja1105_mac_link_down(struct dsa_switch *ds, int port, |
| 787 | unsigned int mode, |
| 788 | phy_interface_t interface) |
| 789 | { |
| 790 | sja1105_inhibit_tx(ds->priv, BIT(port), true); |
| 791 | } |
| 792 | |
| 793 | static void sja1105_mac_link_up(struct dsa_switch *ds, int port, |
| 794 | unsigned int mode, |
| 795 | phy_interface_t interface, |
| 796 | struct phy_device *phydev) |
| 797 | { |
| 798 | sja1105_inhibit_tx(ds->priv, BIT(port), false); |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 799 | } |
| 800 | |
Vladimir Oltean | ad9f299 | 2019-05-02 23:23:38 +0300 | [diff] [blame] | 801 | static void sja1105_phylink_validate(struct dsa_switch *ds, int port, |
| 802 | unsigned long *supported, |
| 803 | struct phylink_link_state *state) |
| 804 | { |
| 805 | /* Construct a new mask which exhaustively contains all link features |
| 806 | * supported by the MAC, and then apply that (logical AND) to what will |
| 807 | * be sent to the PHY for "marketing". |
| 808 | */ |
| 809 | __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, }; |
| 810 | struct sja1105_private *priv = ds->priv; |
| 811 | struct sja1105_xmii_params_entry *mii; |
| 812 | |
| 813 | mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries; |
| 814 | |
Vladimir Oltean | 3971022 | 2019-06-28 00:46:36 +0300 | [diff] [blame] | 815 | /* include/linux/phylink.h says: |
| 816 | * When @state->interface is %PHY_INTERFACE_MODE_NA, phylink |
| 817 | * expects the MAC driver to return all supported link modes. |
| 818 | */ |
| 819 | if (state->interface != PHY_INTERFACE_MODE_NA && |
| 820 | sja1105_phy_mode_mismatch(priv, port, state->interface)) { |
| 821 | bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS); |
| 822 | return; |
| 823 | } |
| 824 | |
Vladimir Oltean | ad9f299 | 2019-05-02 23:23:38 +0300 | [diff] [blame] | 825 | /* The MAC does not support pause frames, and also doesn't |
| 826 | * support half-duplex traffic modes. |
| 827 | */ |
| 828 | phylink_set(mask, Autoneg); |
| 829 | phylink_set(mask, MII); |
| 830 | phylink_set(mask, 10baseT_Full); |
| 831 | phylink_set(mask, 100baseT_Full); |
| 832 | if (mii->xmii_mode[port] == XMII_MODE_RGMII) |
| 833 | phylink_set(mask, 1000baseT_Full); |
| 834 | |
| 835 | bitmap_and(supported, supported, mask, __ETHTOOL_LINK_MODE_MASK_NBITS); |
| 836 | bitmap_and(state->advertising, state->advertising, mask, |
| 837 | __ETHTOOL_LINK_MODE_MASK_NBITS); |
| 838 | } |
| 839 | |
Vladimir Oltean | 60f6053 | 2019-06-26 02:39:38 +0300 | [diff] [blame] | 840 | static int |
| 841 | sja1105_find_static_fdb_entry(struct sja1105_private *priv, int port, |
| 842 | const struct sja1105_l2_lookup_entry *requested) |
| 843 | { |
| 844 | struct sja1105_l2_lookup_entry *l2_lookup; |
| 845 | struct sja1105_table *table; |
| 846 | int i; |
| 847 | |
| 848 | table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; |
| 849 | l2_lookup = table->entries; |
| 850 | |
| 851 | for (i = 0; i < table->entry_count; i++) |
| 852 | if (l2_lookup[i].macaddr == requested->macaddr && |
| 853 | l2_lookup[i].vlanid == requested->vlanid && |
| 854 | l2_lookup[i].destports & BIT(port)) |
| 855 | return i; |
| 856 | |
| 857 | return -1; |
| 858 | } |
| 859 | |
| 860 | /* We want FDB entries added statically through the bridge command to persist |
| 861 | * across switch resets, which are a common thing during normal SJA1105 |
| 862 | * operation. So we have to back them up in the static configuration tables |
| 863 | * and hence apply them on next static config upload... yay! |
| 864 | */ |
| 865 | static int |
| 866 | sja1105_static_fdb_change(struct sja1105_private *priv, int port, |
| 867 | const struct sja1105_l2_lookup_entry *requested, |
| 868 | bool keep) |
| 869 | { |
| 870 | struct sja1105_l2_lookup_entry *l2_lookup; |
| 871 | struct sja1105_table *table; |
| 872 | int rc, match; |
| 873 | |
| 874 | table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; |
| 875 | |
| 876 | match = sja1105_find_static_fdb_entry(priv, port, requested); |
| 877 | if (match < 0) { |
| 878 | /* Can't delete a missing entry. */ |
| 879 | if (!keep) |
| 880 | return 0; |
| 881 | |
| 882 | /* No match => new entry */ |
| 883 | rc = sja1105_table_resize(table, table->entry_count + 1); |
| 884 | if (rc) |
| 885 | return rc; |
| 886 | |
| 887 | match = table->entry_count - 1; |
| 888 | } |
| 889 | |
| 890 | /* Assign pointer after the resize (it may be new memory) */ |
| 891 | l2_lookup = table->entries; |
| 892 | |
| 893 | /* We have a match. |
| 894 | * If the job was to add this FDB entry, it's already done (mostly |
| 895 | * anyway, since the port forwarding mask may have changed, case in |
| 896 | * which we update it). |
| 897 | * Otherwise we have to delete it. |
| 898 | */ |
| 899 | if (keep) { |
| 900 | l2_lookup[match] = *requested; |
| 901 | return 0; |
| 902 | } |
| 903 | |
| 904 | /* To remove, the strategy is to overwrite the element with |
| 905 | * the last one, and then reduce the array size by 1 |
| 906 | */ |
| 907 | l2_lookup[match] = l2_lookup[table->entry_count - 1]; |
| 908 | return sja1105_table_resize(table, table->entry_count - 1); |
| 909 | } |
| 910 | |
Vladimir Oltean | 291d1e7 | 2019-05-02 23:23:31 +0300 | [diff] [blame] | 911 | /* First-generation switches have a 4-way set associative TCAM that |
| 912 | * holds the FDB entries. An FDB index spans from 0 to 1023 and is comprised of |
| 913 | * a "bin" (grouping of 4 entries) and a "way" (an entry within a bin). |
| 914 | * For the placement of a newly learnt FDB entry, the switch selects the bin |
| 915 | * based on a hash function, and the way within that bin incrementally. |
| 916 | */ |
Vladimir Oltean | 09c1b41 | 2019-10-01 22:17:59 +0300 | [diff] [blame] | 917 | static int sja1105et_fdb_index(int bin, int way) |
Vladimir Oltean | 291d1e7 | 2019-05-02 23:23:31 +0300 | [diff] [blame] | 918 | { |
| 919 | return bin * SJA1105ET_FDB_BIN_SIZE + way; |
| 920 | } |
| 921 | |
Vladimir Oltean | 9dfa691 | 2019-06-03 00:11:57 +0300 | [diff] [blame] | 922 | static int sja1105et_is_fdb_entry_in_bin(struct sja1105_private *priv, int bin, |
| 923 | const u8 *addr, u16 vid, |
| 924 | struct sja1105_l2_lookup_entry *match, |
| 925 | int *last_unused) |
Vladimir Oltean | 291d1e7 | 2019-05-02 23:23:31 +0300 | [diff] [blame] | 926 | { |
| 927 | int way; |
| 928 | |
| 929 | for (way = 0; way < SJA1105ET_FDB_BIN_SIZE; way++) { |
| 930 | struct sja1105_l2_lookup_entry l2_lookup = {0}; |
| 931 | int index = sja1105et_fdb_index(bin, way); |
| 932 | |
| 933 | /* Skip unused entries, optionally marking them |
| 934 | * into the return value |
| 935 | */ |
| 936 | if (sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, |
| 937 | index, &l2_lookup)) { |
| 938 | if (last_unused) |
| 939 | *last_unused = way; |
| 940 | continue; |
| 941 | } |
| 942 | |
| 943 | if (l2_lookup.macaddr == ether_addr_to_u64(addr) && |
| 944 | l2_lookup.vlanid == vid) { |
| 945 | if (match) |
| 946 | *match = l2_lookup; |
| 947 | return way; |
| 948 | } |
| 949 | } |
| 950 | /* Return an invalid entry index if not found */ |
| 951 | return -1; |
| 952 | } |
| 953 | |
Vladimir Oltean | 9dfa691 | 2019-06-03 00:11:57 +0300 | [diff] [blame] | 954 | int sja1105et_fdb_add(struct dsa_switch *ds, int port, |
| 955 | const unsigned char *addr, u16 vid) |
Vladimir Oltean | 291d1e7 | 2019-05-02 23:23:31 +0300 | [diff] [blame] | 956 | { |
| 957 | struct sja1105_l2_lookup_entry l2_lookup = {0}; |
| 958 | struct sja1105_private *priv = ds->priv; |
| 959 | struct device *dev = ds->dev; |
| 960 | int last_unused = -1; |
Vladimir Oltean | 60f6053 | 2019-06-26 02:39:38 +0300 | [diff] [blame] | 961 | int bin, way, rc; |
Vladimir Oltean | 291d1e7 | 2019-05-02 23:23:31 +0300 | [diff] [blame] | 962 | |
Vladimir Oltean | 9dfa691 | 2019-06-03 00:11:57 +0300 | [diff] [blame] | 963 | bin = sja1105et_fdb_hash(priv, addr, vid); |
Vladimir Oltean | 291d1e7 | 2019-05-02 23:23:31 +0300 | [diff] [blame] | 964 | |
Vladimir Oltean | 9dfa691 | 2019-06-03 00:11:57 +0300 | [diff] [blame] | 965 | way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid, |
| 966 | &l2_lookup, &last_unused); |
Vladimir Oltean | 291d1e7 | 2019-05-02 23:23:31 +0300 | [diff] [blame] | 967 | if (way >= 0) { |
| 968 | /* We have an FDB entry. Is our port in the destination |
| 969 | * mask? If yes, we need to do nothing. If not, we need |
| 970 | * to rewrite the entry by adding this port to it. |
| 971 | */ |
| 972 | if (l2_lookup.destports & BIT(port)) |
| 973 | return 0; |
| 974 | l2_lookup.destports |= BIT(port); |
| 975 | } else { |
| 976 | int index = sja1105et_fdb_index(bin, way); |
| 977 | |
| 978 | /* We don't have an FDB entry. We construct a new one and |
| 979 | * try to find a place for it within the FDB table. |
| 980 | */ |
| 981 | l2_lookup.macaddr = ether_addr_to_u64(addr); |
| 982 | l2_lookup.destports = BIT(port); |
| 983 | l2_lookup.vlanid = vid; |
| 984 | |
| 985 | if (last_unused >= 0) { |
| 986 | way = last_unused; |
| 987 | } else { |
| 988 | /* Bin is full, need to evict somebody. |
| 989 | * Choose victim at random. If you get these messages |
| 990 | * often, you may need to consider changing the |
| 991 | * distribution function: |
| 992 | * static_config[BLK_IDX_L2_LOOKUP_PARAMS].entries->poly |
| 993 | */ |
| 994 | get_random_bytes(&way, sizeof(u8)); |
| 995 | way %= SJA1105ET_FDB_BIN_SIZE; |
| 996 | dev_warn(dev, "Warning, FDB bin %d full while adding entry for %pM. Evicting entry %u.\n", |
| 997 | bin, addr, way); |
| 998 | /* Evict entry */ |
| 999 | sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, |
| 1000 | index, NULL, false); |
| 1001 | } |
| 1002 | } |
| 1003 | l2_lookup.index = sja1105et_fdb_index(bin, way); |
| 1004 | |
Vladimir Oltean | 60f6053 | 2019-06-26 02:39:38 +0300 | [diff] [blame] | 1005 | rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, |
| 1006 | l2_lookup.index, &l2_lookup, |
| 1007 | true); |
| 1008 | if (rc < 0) |
| 1009 | return rc; |
| 1010 | |
| 1011 | return sja1105_static_fdb_change(priv, port, &l2_lookup, true); |
Vladimir Oltean | 291d1e7 | 2019-05-02 23:23:31 +0300 | [diff] [blame] | 1012 | } |
| 1013 | |
Vladimir Oltean | 9dfa691 | 2019-06-03 00:11:57 +0300 | [diff] [blame] | 1014 | int sja1105et_fdb_del(struct dsa_switch *ds, int port, |
| 1015 | const unsigned char *addr, u16 vid) |
Vladimir Oltean | 291d1e7 | 2019-05-02 23:23:31 +0300 | [diff] [blame] | 1016 | { |
| 1017 | struct sja1105_l2_lookup_entry l2_lookup = {0}; |
| 1018 | struct sja1105_private *priv = ds->priv; |
Vladimir Oltean | 60f6053 | 2019-06-26 02:39:38 +0300 | [diff] [blame] | 1019 | int index, bin, way, rc; |
Vladimir Oltean | 291d1e7 | 2019-05-02 23:23:31 +0300 | [diff] [blame] | 1020 | bool keep; |
| 1021 | |
Vladimir Oltean | 9dfa691 | 2019-06-03 00:11:57 +0300 | [diff] [blame] | 1022 | bin = sja1105et_fdb_hash(priv, addr, vid); |
| 1023 | way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid, |
| 1024 | &l2_lookup, NULL); |
Vladimir Oltean | 291d1e7 | 2019-05-02 23:23:31 +0300 | [diff] [blame] | 1025 | if (way < 0) |
| 1026 | return 0; |
| 1027 | index = sja1105et_fdb_index(bin, way); |
| 1028 | |
| 1029 | /* We have an FDB entry. Is our port in the destination mask? If yes, |
| 1030 | * we need to remove it. If the resulting port mask becomes empty, we |
| 1031 | * need to completely evict the FDB entry. |
| 1032 | * Otherwise we just write it back. |
| 1033 | */ |
Vladimir Oltean | 7752e93 | 2019-06-03 00:15:54 +0300 | [diff] [blame] | 1034 | l2_lookup.destports &= ~BIT(port); |
| 1035 | |
Vladimir Oltean | 291d1e7 | 2019-05-02 23:23:31 +0300 | [diff] [blame] | 1036 | if (l2_lookup.destports) |
| 1037 | keep = true; |
| 1038 | else |
| 1039 | keep = false; |
| 1040 | |
Vladimir Oltean | 60f6053 | 2019-06-26 02:39:38 +0300 | [diff] [blame] | 1041 | rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, |
| 1042 | index, &l2_lookup, keep); |
| 1043 | if (rc < 0) |
| 1044 | return rc; |
| 1045 | |
| 1046 | return sja1105_static_fdb_change(priv, port, &l2_lookup, keep); |
Vladimir Oltean | 291d1e7 | 2019-05-02 23:23:31 +0300 | [diff] [blame] | 1047 | } |
| 1048 | |
Vladimir Oltean | 9dfa691 | 2019-06-03 00:11:57 +0300 | [diff] [blame] | 1049 | int sja1105pqrs_fdb_add(struct dsa_switch *ds, int port, |
| 1050 | const unsigned char *addr, u16 vid) |
| 1051 | { |
Vladimir Oltean | 1da7382 | 2019-06-03 00:15:45 +0300 | [diff] [blame] | 1052 | struct sja1105_l2_lookup_entry l2_lookup = {0}; |
| 1053 | struct sja1105_private *priv = ds->priv; |
| 1054 | int rc, i; |
| 1055 | |
| 1056 | /* Search for an existing entry in the FDB table */ |
| 1057 | l2_lookup.macaddr = ether_addr_to_u64(addr); |
| 1058 | l2_lookup.vlanid = vid; |
| 1059 | l2_lookup.iotag = SJA1105_S_TAG; |
| 1060 | l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0); |
Vivien Didelot | 68bb8ea | 2019-10-21 16:51:15 -0400 | [diff] [blame] | 1061 | if (dsa_port_is_vlan_filtering(dsa_to_port(ds, port))) { |
Vladimir Oltean | 6d7c7d9 | 2019-08-05 01:38:44 +0300 | [diff] [blame] | 1062 | l2_lookup.mask_vlanid = VLAN_VID_MASK; |
| 1063 | l2_lookup.mask_iotag = BIT(0); |
| 1064 | } else { |
| 1065 | l2_lookup.mask_vlanid = 0; |
| 1066 | l2_lookup.mask_iotag = 0; |
| 1067 | } |
Vladimir Oltean | 1da7382 | 2019-06-03 00:15:45 +0300 | [diff] [blame] | 1068 | l2_lookup.destports = BIT(port); |
| 1069 | |
| 1070 | rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, |
| 1071 | SJA1105_SEARCH, &l2_lookup); |
| 1072 | if (rc == 0) { |
| 1073 | /* Found and this port is already in the entry's |
| 1074 | * port mask => job done |
| 1075 | */ |
| 1076 | if (l2_lookup.destports & BIT(port)) |
| 1077 | return 0; |
| 1078 | /* l2_lookup.index is populated by the switch in case it |
| 1079 | * found something. |
| 1080 | */ |
| 1081 | l2_lookup.destports |= BIT(port); |
| 1082 | goto skip_finding_an_index; |
| 1083 | } |
| 1084 | |
| 1085 | /* Not found, so try to find an unused spot in the FDB. |
| 1086 | * This is slightly inefficient because the strategy is knock-knock at |
| 1087 | * every possible position from 0 to 1023. |
| 1088 | */ |
| 1089 | for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) { |
| 1090 | rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, |
| 1091 | i, NULL); |
| 1092 | if (rc < 0) |
| 1093 | break; |
| 1094 | } |
| 1095 | if (i == SJA1105_MAX_L2_LOOKUP_COUNT) { |
| 1096 | dev_err(ds->dev, "FDB is full, cannot add entry.\n"); |
| 1097 | return -EINVAL; |
| 1098 | } |
Vladimir Oltean | 17ae655 | 2019-06-26 02:39:40 +0300 | [diff] [blame] | 1099 | l2_lookup.lockeds = true; |
Vladimir Oltean | 1da7382 | 2019-06-03 00:15:45 +0300 | [diff] [blame] | 1100 | l2_lookup.index = i; |
| 1101 | |
| 1102 | skip_finding_an_index: |
Vladimir Oltean | 60f6053 | 2019-06-26 02:39:38 +0300 | [diff] [blame] | 1103 | rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, |
| 1104 | l2_lookup.index, &l2_lookup, |
| 1105 | true); |
| 1106 | if (rc < 0) |
| 1107 | return rc; |
| 1108 | |
| 1109 | return sja1105_static_fdb_change(priv, port, &l2_lookup, true); |
Vladimir Oltean | 9dfa691 | 2019-06-03 00:11:57 +0300 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | int sja1105pqrs_fdb_del(struct dsa_switch *ds, int port, |
| 1113 | const unsigned char *addr, u16 vid) |
| 1114 | { |
Vladimir Oltean | 1da7382 | 2019-06-03 00:15:45 +0300 | [diff] [blame] | 1115 | struct sja1105_l2_lookup_entry l2_lookup = {0}; |
| 1116 | struct sja1105_private *priv = ds->priv; |
| 1117 | bool keep; |
| 1118 | int rc; |
| 1119 | |
| 1120 | l2_lookup.macaddr = ether_addr_to_u64(addr); |
| 1121 | l2_lookup.vlanid = vid; |
| 1122 | l2_lookup.iotag = SJA1105_S_TAG; |
| 1123 | l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0); |
Vivien Didelot | 68bb8ea | 2019-10-21 16:51:15 -0400 | [diff] [blame] | 1124 | if (dsa_port_is_vlan_filtering(dsa_to_port(ds, port))) { |
Vladimir Oltean | 6d7c7d9 | 2019-08-05 01:38:44 +0300 | [diff] [blame] | 1125 | l2_lookup.mask_vlanid = VLAN_VID_MASK; |
| 1126 | l2_lookup.mask_iotag = BIT(0); |
| 1127 | } else { |
| 1128 | l2_lookup.mask_vlanid = 0; |
| 1129 | l2_lookup.mask_iotag = 0; |
| 1130 | } |
Vladimir Oltean | 1da7382 | 2019-06-03 00:15:45 +0300 | [diff] [blame] | 1131 | l2_lookup.destports = BIT(port); |
| 1132 | |
| 1133 | rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, |
| 1134 | SJA1105_SEARCH, &l2_lookup); |
| 1135 | if (rc < 0) |
| 1136 | return 0; |
| 1137 | |
| 1138 | l2_lookup.destports &= ~BIT(port); |
| 1139 | |
| 1140 | /* Decide whether we remove just this port from the FDB entry, |
| 1141 | * or if we remove it completely. |
| 1142 | */ |
| 1143 | if (l2_lookup.destports) |
| 1144 | keep = true; |
| 1145 | else |
| 1146 | keep = false; |
| 1147 | |
Vladimir Oltean | 60f6053 | 2019-06-26 02:39:38 +0300 | [diff] [blame] | 1148 | rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, |
| 1149 | l2_lookup.index, &l2_lookup, keep); |
| 1150 | if (rc < 0) |
| 1151 | return rc; |
| 1152 | |
| 1153 | return sja1105_static_fdb_change(priv, port, &l2_lookup, keep); |
Vladimir Oltean | 9dfa691 | 2019-06-03 00:11:57 +0300 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | static int sja1105_fdb_add(struct dsa_switch *ds, int port, |
| 1157 | const unsigned char *addr, u16 vid) |
| 1158 | { |
| 1159 | struct sja1105_private *priv = ds->priv; |
Vladimir Oltean | b3ee526 | 2019-06-26 02:39:41 +0300 | [diff] [blame] | 1160 | |
Vladimir Oltean | 6d7c7d9 | 2019-08-05 01:38:44 +0300 | [diff] [blame] | 1161 | /* dsa_8021q is in effect when the bridge's vlan_filtering isn't, |
| 1162 | * so the switch still does some VLAN processing internally. |
| 1163 | * But Shared VLAN Learning (SVL) is also active, and it will take |
| 1164 | * care of autonomous forwarding between the unique pvid's of each |
| 1165 | * port. Here we just make sure that users can't add duplicate FDB |
| 1166 | * entries when in this mode - the actual VID doesn't matter except |
| 1167 | * for what gets printed in 'bridge fdb show'. In the case of zero, |
| 1168 | * no VID gets printed at all. |
Vladimir Oltean | 9364759 | 2019-06-03 00:16:01 +0300 | [diff] [blame] | 1169 | */ |
Vivien Didelot | 68bb8ea | 2019-10-21 16:51:15 -0400 | [diff] [blame] | 1170 | if (!dsa_port_is_vlan_filtering(dsa_to_port(ds, port))) |
Vladimir Oltean | 6d7c7d9 | 2019-08-05 01:38:44 +0300 | [diff] [blame] | 1171 | vid = 0; |
Vladimir Oltean | 9364759 | 2019-06-03 00:16:01 +0300 | [diff] [blame] | 1172 | |
Vladimir Oltean | 6d7c7d9 | 2019-08-05 01:38:44 +0300 | [diff] [blame] | 1173 | return priv->info->fdb_add_cmd(ds, port, addr, vid); |
Vladimir Oltean | 9dfa691 | 2019-06-03 00:11:57 +0300 | [diff] [blame] | 1174 | } |
| 1175 | |
| 1176 | static int sja1105_fdb_del(struct dsa_switch *ds, int port, |
| 1177 | const unsigned char *addr, u16 vid) |
| 1178 | { |
| 1179 | struct sja1105_private *priv = ds->priv; |
| 1180 | |
Vivien Didelot | 68bb8ea | 2019-10-21 16:51:15 -0400 | [diff] [blame] | 1181 | if (!dsa_port_is_vlan_filtering(dsa_to_port(ds, port))) |
Vladimir Oltean | 6d7c7d9 | 2019-08-05 01:38:44 +0300 | [diff] [blame] | 1182 | vid = 0; |
Vladimir Oltean | 9364759 | 2019-06-03 00:16:01 +0300 | [diff] [blame] | 1183 | |
Vladimir Oltean | 6d7c7d9 | 2019-08-05 01:38:44 +0300 | [diff] [blame] | 1184 | return priv->info->fdb_del_cmd(ds, port, addr, vid); |
Vladimir Oltean | 9dfa691 | 2019-06-03 00:11:57 +0300 | [diff] [blame] | 1185 | } |
| 1186 | |
Vladimir Oltean | 291d1e7 | 2019-05-02 23:23:31 +0300 | [diff] [blame] | 1187 | static int sja1105_fdb_dump(struct dsa_switch *ds, int port, |
| 1188 | dsa_fdb_dump_cb_t *cb, void *data) |
| 1189 | { |
| 1190 | struct sja1105_private *priv = ds->priv; |
| 1191 | struct device *dev = ds->dev; |
| 1192 | int i; |
| 1193 | |
| 1194 | for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) { |
| 1195 | struct sja1105_l2_lookup_entry l2_lookup = {0}; |
| 1196 | u8 macaddr[ETH_ALEN]; |
| 1197 | int rc; |
| 1198 | |
| 1199 | rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, |
| 1200 | i, &l2_lookup); |
| 1201 | /* No fdb entry at i, not an issue */ |
Vladimir Oltean | def8460 | 2019-06-03 00:11:59 +0300 | [diff] [blame] | 1202 | if (rc == -ENOENT) |
Vladimir Oltean | 291d1e7 | 2019-05-02 23:23:31 +0300 | [diff] [blame] | 1203 | continue; |
| 1204 | if (rc) { |
| 1205 | dev_err(dev, "Failed to dump FDB: %d\n", rc); |
| 1206 | return rc; |
| 1207 | } |
| 1208 | |
| 1209 | /* FDB dump callback is per port. This means we have to |
| 1210 | * disregard a valid entry if it's not for this port, even if |
| 1211 | * only to revisit it later. This is inefficient because the |
| 1212 | * 1024-sized FDB table needs to be traversed 4 times through |
| 1213 | * SPI during a 'bridge fdb show' command. |
| 1214 | */ |
| 1215 | if (!(l2_lookup.destports & BIT(port))) |
| 1216 | continue; |
| 1217 | u64_to_ether_addr(l2_lookup.macaddr, macaddr); |
Vladimir Oltean | 9364759 | 2019-06-03 00:16:01 +0300 | [diff] [blame] | 1218 | |
Vladimir Oltean | 6d7c7d9 | 2019-08-05 01:38:44 +0300 | [diff] [blame] | 1219 | /* We need to hide the dsa_8021q VLANs from the user. */ |
Vivien Didelot | 68bb8ea | 2019-10-21 16:51:15 -0400 | [diff] [blame] | 1220 | if (!dsa_port_is_vlan_filtering(dsa_to_port(ds, port))) |
Vladimir Oltean | 6d7c7d9 | 2019-08-05 01:38:44 +0300 | [diff] [blame] | 1221 | l2_lookup.vlanid = 0; |
Vladimir Oltean | 17ae655 | 2019-06-26 02:39:40 +0300 | [diff] [blame] | 1222 | cb(macaddr, l2_lookup.vlanid, l2_lookup.lockeds, data); |
Vladimir Oltean | 291d1e7 | 2019-05-02 23:23:31 +0300 | [diff] [blame] | 1223 | } |
| 1224 | return 0; |
| 1225 | } |
| 1226 | |
| 1227 | /* This callback needs to be present */ |
| 1228 | static int sja1105_mdb_prepare(struct dsa_switch *ds, int port, |
| 1229 | const struct switchdev_obj_port_mdb *mdb) |
| 1230 | { |
| 1231 | return 0; |
| 1232 | } |
| 1233 | |
| 1234 | static void sja1105_mdb_add(struct dsa_switch *ds, int port, |
| 1235 | const struct switchdev_obj_port_mdb *mdb) |
| 1236 | { |
| 1237 | sja1105_fdb_add(ds, port, mdb->addr, mdb->vid); |
| 1238 | } |
| 1239 | |
| 1240 | static int sja1105_mdb_del(struct dsa_switch *ds, int port, |
| 1241 | const struct switchdev_obj_port_mdb *mdb) |
| 1242 | { |
| 1243 | return sja1105_fdb_del(ds, port, mdb->addr, mdb->vid); |
| 1244 | } |
| 1245 | |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 1246 | static int sja1105_bridge_member(struct dsa_switch *ds, int port, |
| 1247 | struct net_device *br, bool member) |
| 1248 | { |
| 1249 | struct sja1105_l2_forwarding_entry *l2_fwd; |
| 1250 | struct sja1105_private *priv = ds->priv; |
| 1251 | int i, rc; |
| 1252 | |
| 1253 | l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries; |
| 1254 | |
| 1255 | for (i = 0; i < SJA1105_NUM_PORTS; i++) { |
| 1256 | /* Add this port to the forwarding matrix of the |
| 1257 | * other ports in the same bridge, and viceversa. |
| 1258 | */ |
| 1259 | if (!dsa_is_user_port(ds, i)) |
| 1260 | continue; |
| 1261 | /* For the ports already under the bridge, only one thing needs |
| 1262 | * to be done, and that is to add this port to their |
| 1263 | * reachability domain. So we can perform the SPI write for |
| 1264 | * them immediately. However, for this port itself (the one |
| 1265 | * that is new to the bridge), we need to add all other ports |
| 1266 | * to its reachability domain. So we do that incrementally in |
| 1267 | * this loop, and perform the SPI write only at the end, once |
| 1268 | * the domain contains all other bridge ports. |
| 1269 | */ |
| 1270 | if (i == port) |
| 1271 | continue; |
| 1272 | if (dsa_to_port(ds, i)->bridge_dev != br) |
| 1273 | continue; |
| 1274 | sja1105_port_allow_traffic(l2_fwd, i, port, member); |
| 1275 | sja1105_port_allow_traffic(l2_fwd, port, i, member); |
| 1276 | |
| 1277 | rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING, |
| 1278 | i, &l2_fwd[i], true); |
| 1279 | if (rc < 0) |
| 1280 | return rc; |
| 1281 | } |
| 1282 | |
| 1283 | return sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING, |
| 1284 | port, &l2_fwd[port], true); |
| 1285 | } |
| 1286 | |
Vladimir Oltean | 640f763 | 2019-05-05 13:19:28 +0300 | [diff] [blame] | 1287 | static void sja1105_bridge_stp_state_set(struct dsa_switch *ds, int port, |
| 1288 | u8 state) |
| 1289 | { |
| 1290 | struct sja1105_private *priv = ds->priv; |
| 1291 | struct sja1105_mac_config_entry *mac; |
| 1292 | |
| 1293 | mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; |
| 1294 | |
| 1295 | switch (state) { |
| 1296 | case BR_STATE_DISABLED: |
| 1297 | case BR_STATE_BLOCKING: |
| 1298 | /* From UM10944 description of DRPDTAG (why put this there?): |
| 1299 | * "Management traffic flows to the port regardless of the state |
| 1300 | * of the INGRESS flag". So BPDUs are still be allowed to pass. |
| 1301 | * At the moment no difference between DISABLED and BLOCKING. |
| 1302 | */ |
| 1303 | mac[port].ingress = false; |
| 1304 | mac[port].egress = false; |
| 1305 | mac[port].dyn_learn = false; |
| 1306 | break; |
| 1307 | case BR_STATE_LISTENING: |
| 1308 | mac[port].ingress = true; |
| 1309 | mac[port].egress = false; |
| 1310 | mac[port].dyn_learn = false; |
| 1311 | break; |
| 1312 | case BR_STATE_LEARNING: |
| 1313 | mac[port].ingress = true; |
| 1314 | mac[port].egress = false; |
| 1315 | mac[port].dyn_learn = true; |
| 1316 | break; |
| 1317 | case BR_STATE_FORWARDING: |
| 1318 | mac[port].ingress = true; |
| 1319 | mac[port].egress = true; |
| 1320 | mac[port].dyn_learn = true; |
| 1321 | break; |
| 1322 | default: |
| 1323 | dev_err(ds->dev, "invalid STP state: %d\n", state); |
| 1324 | return; |
| 1325 | } |
| 1326 | |
| 1327 | sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, |
| 1328 | &mac[port], true); |
| 1329 | } |
| 1330 | |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 1331 | static int sja1105_bridge_join(struct dsa_switch *ds, int port, |
| 1332 | struct net_device *br) |
| 1333 | { |
| 1334 | return sja1105_bridge_member(ds, port, br, true); |
| 1335 | } |
| 1336 | |
| 1337 | static void sja1105_bridge_leave(struct dsa_switch *ds, int port, |
| 1338 | struct net_device *br) |
| 1339 | { |
| 1340 | sja1105_bridge_member(ds, port, br, false); |
| 1341 | } |
| 1342 | |
Vladimir Oltean | 6666ceb | 2019-05-02 23:23:34 +0300 | [diff] [blame] | 1343 | /* For situations where we need to change a setting at runtime that is only |
| 1344 | * available through the static configuration, resetting the switch in order |
| 1345 | * to upload the new static config is unavoidable. Back up the settings we |
| 1346 | * modify at runtime (currently only MAC) and restore them after uploading, |
| 1347 | * such that this operation is relatively seamless. |
| 1348 | */ |
Vladimir Oltean | 317ab5b | 2019-09-15 05:00:02 +0300 | [diff] [blame] | 1349 | int sja1105_static_config_reload(struct sja1105_private *priv) |
Vladimir Oltean | 6666ceb | 2019-05-02 23:23:34 +0300 | [diff] [blame] | 1350 | { |
| 1351 | struct sja1105_mac_config_entry *mac; |
| 1352 | int speed_mbps[SJA1105_NUM_PORTS]; |
| 1353 | int rc, i; |
| 1354 | |
| 1355 | mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; |
| 1356 | |
Vladimir Oltean | 8400cff | 2019-06-08 16:03:44 +0300 | [diff] [blame] | 1357 | /* Back up the dynamic link speed changed by sja1105_adjust_port_config |
| 1358 | * in order to temporarily restore it to SJA1105_SPEED_AUTO - which the |
| 1359 | * switch wants to see in the static config in order to allow us to |
| 1360 | * change it through the dynamic interface later. |
Vladimir Oltean | 6666ceb | 2019-05-02 23:23:34 +0300 | [diff] [blame] | 1361 | */ |
| 1362 | for (i = 0; i < SJA1105_NUM_PORTS; i++) { |
| 1363 | speed_mbps[i] = sja1105_speed[mac[i].speed]; |
| 1364 | mac[i].speed = SJA1105_SPEED_AUTO; |
| 1365 | } |
| 1366 | |
| 1367 | /* Reset switch and send updated static configuration */ |
| 1368 | rc = sja1105_static_config_upload(priv); |
| 1369 | if (rc < 0) |
| 1370 | goto out; |
| 1371 | |
| 1372 | /* Configure the CGU (PLLs) for MII and RMII PHYs. |
| 1373 | * For these interfaces there is no dynamic configuration |
| 1374 | * needed, since PLLs have same settings at all speeds. |
| 1375 | */ |
| 1376 | rc = sja1105_clocking_setup(priv); |
| 1377 | if (rc < 0) |
| 1378 | goto out; |
| 1379 | |
| 1380 | for (i = 0; i < SJA1105_NUM_PORTS; i++) { |
Vladimir Oltean | 8400cff | 2019-06-08 16:03:44 +0300 | [diff] [blame] | 1381 | rc = sja1105_adjust_port_config(priv, i, speed_mbps[i]); |
Vladimir Oltean | 6666ceb | 2019-05-02 23:23:34 +0300 | [diff] [blame] | 1382 | if (rc < 0) |
| 1383 | goto out; |
| 1384 | } |
| 1385 | out: |
| 1386 | return rc; |
| 1387 | } |
| 1388 | |
Vladimir Oltean | 6666ceb | 2019-05-02 23:23:34 +0300 | [diff] [blame] | 1389 | static int sja1105_pvid_apply(struct sja1105_private *priv, int port, u16 pvid) |
| 1390 | { |
| 1391 | struct sja1105_mac_config_entry *mac; |
| 1392 | |
| 1393 | mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; |
| 1394 | |
| 1395 | mac[port].vlanid = pvid; |
| 1396 | |
| 1397 | return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, |
| 1398 | &mac[port], true); |
| 1399 | } |
| 1400 | |
| 1401 | static int sja1105_is_vlan_configured(struct sja1105_private *priv, u16 vid) |
| 1402 | { |
| 1403 | struct sja1105_vlan_lookup_entry *vlan; |
| 1404 | int count, i; |
| 1405 | |
| 1406 | vlan = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entries; |
| 1407 | count = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entry_count; |
| 1408 | |
| 1409 | for (i = 0; i < count; i++) |
| 1410 | if (vlan[i].vlanid == vid) |
| 1411 | return i; |
| 1412 | |
| 1413 | /* Return an invalid entry index if not found */ |
| 1414 | return -1; |
| 1415 | } |
| 1416 | |
| 1417 | static int sja1105_vlan_apply(struct sja1105_private *priv, int port, u16 vid, |
| 1418 | bool enabled, bool untagged) |
| 1419 | { |
| 1420 | struct sja1105_vlan_lookup_entry *vlan; |
| 1421 | struct sja1105_table *table; |
| 1422 | bool keep = true; |
| 1423 | int match, rc; |
| 1424 | |
| 1425 | table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP]; |
| 1426 | |
| 1427 | match = sja1105_is_vlan_configured(priv, vid); |
| 1428 | if (match < 0) { |
| 1429 | /* Can't delete a missing entry. */ |
| 1430 | if (!enabled) |
| 1431 | return 0; |
| 1432 | rc = sja1105_table_resize(table, table->entry_count + 1); |
| 1433 | if (rc) |
| 1434 | return rc; |
| 1435 | match = table->entry_count - 1; |
| 1436 | } |
| 1437 | /* Assign pointer after the resize (it's new memory) */ |
| 1438 | vlan = table->entries; |
| 1439 | vlan[match].vlanid = vid; |
| 1440 | if (enabled) { |
| 1441 | vlan[match].vlan_bc |= BIT(port); |
| 1442 | vlan[match].vmemb_port |= BIT(port); |
| 1443 | } else { |
| 1444 | vlan[match].vlan_bc &= ~BIT(port); |
| 1445 | vlan[match].vmemb_port &= ~BIT(port); |
| 1446 | } |
| 1447 | /* Also unset tag_port if removing this VLAN was requested, |
| 1448 | * just so we don't have a confusing bitmap (no practical purpose). |
| 1449 | */ |
| 1450 | if (untagged || !enabled) |
| 1451 | vlan[match].tag_port &= ~BIT(port); |
| 1452 | else |
| 1453 | vlan[match].tag_port |= BIT(port); |
| 1454 | /* If there's no port left as member of this VLAN, |
| 1455 | * it's time for it to go. |
| 1456 | */ |
| 1457 | if (!vlan[match].vmemb_port) |
| 1458 | keep = false; |
| 1459 | |
| 1460 | dev_dbg(priv->ds->dev, |
| 1461 | "%s: port %d, vid %llu, broadcast domain 0x%llx, " |
| 1462 | "port members 0x%llx, tagged ports 0x%llx, keep %d\n", |
| 1463 | __func__, port, vlan[match].vlanid, vlan[match].vlan_bc, |
| 1464 | vlan[match].vmemb_port, vlan[match].tag_port, keep); |
| 1465 | |
| 1466 | rc = sja1105_dynamic_config_write(priv, BLK_IDX_VLAN_LOOKUP, vid, |
| 1467 | &vlan[match], keep); |
| 1468 | if (rc < 0) |
| 1469 | return rc; |
| 1470 | |
| 1471 | if (!keep) |
| 1472 | return sja1105_table_delete_entry(table, match); |
| 1473 | |
| 1474 | return 0; |
| 1475 | } |
| 1476 | |
Vladimir Oltean | 227d07a | 2019-05-05 13:19:27 +0300 | [diff] [blame] | 1477 | static int sja1105_setup_8021q_tagging(struct dsa_switch *ds, bool enabled) |
| 1478 | { |
| 1479 | int rc, i; |
| 1480 | |
| 1481 | for (i = 0; i < SJA1105_NUM_PORTS; i++) { |
| 1482 | rc = dsa_port_setup_8021q_tagging(ds, i, enabled); |
| 1483 | if (rc < 0) { |
| 1484 | dev_err(ds->dev, "Failed to setup VLAN tagging for port %d: %d\n", |
| 1485 | i, rc); |
| 1486 | return rc; |
| 1487 | } |
| 1488 | } |
| 1489 | dev_info(ds->dev, "%s switch tagging\n", |
| 1490 | enabled ? "Enabled" : "Disabled"); |
| 1491 | return 0; |
| 1492 | } |
| 1493 | |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 1494 | static enum dsa_tag_protocol |
| 1495 | sja1105_get_tag_protocol(struct dsa_switch *ds, int port) |
| 1496 | { |
Vladimir Oltean | 227d07a | 2019-05-05 13:19:27 +0300 | [diff] [blame] | 1497 | return DSA_TAG_PROTO_SJA1105; |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 1498 | } |
| 1499 | |
Vladimir Oltean | 6666ceb | 2019-05-02 23:23:34 +0300 | [diff] [blame] | 1500 | /* This callback needs to be present */ |
| 1501 | static int sja1105_vlan_prepare(struct dsa_switch *ds, int port, |
| 1502 | const struct switchdev_obj_port_vlan *vlan) |
| 1503 | { |
| 1504 | return 0; |
| 1505 | } |
| 1506 | |
Vladimir Oltean | 070ca3b | 2019-06-08 15:04:30 +0300 | [diff] [blame] | 1507 | /* The TPID setting belongs to the General Parameters table, |
| 1508 | * which can only be partially reconfigured at runtime (and not the TPID). |
| 1509 | * So a switch reset is required. |
| 1510 | */ |
Vladimir Oltean | 6666ceb | 2019-05-02 23:23:34 +0300 | [diff] [blame] | 1511 | static int sja1105_vlan_filtering(struct dsa_switch *ds, int port, bool enabled) |
| 1512 | { |
Vladimir Oltean | 6d7c7d9 | 2019-08-05 01:38:44 +0300 | [diff] [blame] | 1513 | struct sja1105_l2_lookup_params_entry *l2_lookup_params; |
Vladimir Oltean | 070ca3b | 2019-06-08 15:04:30 +0300 | [diff] [blame] | 1514 | struct sja1105_general_params_entry *general_params; |
Vladimir Oltean | 6666ceb | 2019-05-02 23:23:34 +0300 | [diff] [blame] | 1515 | struct sja1105_private *priv = ds->priv; |
Vladimir Oltean | 070ca3b | 2019-06-08 15:04:30 +0300 | [diff] [blame] | 1516 | struct sja1105_table *table; |
| 1517 | u16 tpid, tpid2; |
Vladimir Oltean | 6666ceb | 2019-05-02 23:23:34 +0300 | [diff] [blame] | 1518 | int rc; |
| 1519 | |
Vladimir Oltean | 070ca3b | 2019-06-08 15:04:30 +0300 | [diff] [blame] | 1520 | if (enabled) { |
Vladimir Oltean | 6666ceb | 2019-05-02 23:23:34 +0300 | [diff] [blame] | 1521 | /* Enable VLAN filtering. */ |
Vladimir Oltean | f9a1a76 | 2019-06-08 15:04:31 +0300 | [diff] [blame] | 1522 | tpid = ETH_P_8021AD; |
| 1523 | tpid2 = ETH_P_8021Q; |
Vladimir Oltean | 070ca3b | 2019-06-08 15:04:30 +0300 | [diff] [blame] | 1524 | } else { |
Vladimir Oltean | 6666ceb | 2019-05-02 23:23:34 +0300 | [diff] [blame] | 1525 | /* Disable VLAN filtering. */ |
Vladimir Oltean | 070ca3b | 2019-06-08 15:04:30 +0300 | [diff] [blame] | 1526 | tpid = ETH_P_SJA1105; |
| 1527 | tpid2 = ETH_P_SJA1105; |
| 1528 | } |
| 1529 | |
| 1530 | table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; |
| 1531 | general_params = table->entries; |
Vladimir Oltean | f9a1a76 | 2019-06-08 15:04:31 +0300 | [diff] [blame] | 1532 | /* EtherType used to identify outer tagged (S-tag) VLAN traffic */ |
Vladimir Oltean | 070ca3b | 2019-06-08 15:04:30 +0300 | [diff] [blame] | 1533 | general_params->tpid = tpid; |
Vladimir Oltean | f9a1a76 | 2019-06-08 15:04:31 +0300 | [diff] [blame] | 1534 | /* EtherType used to identify inner tagged (C-tag) VLAN traffic */ |
Vladimir Oltean | 070ca3b | 2019-06-08 15:04:30 +0300 | [diff] [blame] | 1535 | general_params->tpid2 = tpid2; |
Vladimir Oltean | 4282446 | 2019-06-08 15:04:32 +0300 | [diff] [blame] | 1536 | /* When VLAN filtering is on, we need to at least be able to |
| 1537 | * decode management traffic through the "backup plan". |
| 1538 | */ |
| 1539 | general_params->incl_srcpt1 = enabled; |
| 1540 | general_params->incl_srcpt0 = enabled; |
Vladimir Oltean | 070ca3b | 2019-06-08 15:04:30 +0300 | [diff] [blame] | 1541 | |
Vladimir Oltean | 6d7c7d9 | 2019-08-05 01:38:44 +0300 | [diff] [blame] | 1542 | /* VLAN filtering => independent VLAN learning. |
| 1543 | * No VLAN filtering => shared VLAN learning. |
| 1544 | * |
| 1545 | * In shared VLAN learning mode, untagged traffic still gets |
| 1546 | * pvid-tagged, and the FDB table gets populated with entries |
| 1547 | * containing the "real" (pvid or from VLAN tag) VLAN ID. |
| 1548 | * However the switch performs a masked L2 lookup in the FDB, |
| 1549 | * effectively only looking up a frame's DMAC (and not VID) for the |
| 1550 | * forwarding decision. |
| 1551 | * |
| 1552 | * This is extremely convenient for us, because in modes with |
| 1553 | * vlan_filtering=0, dsa_8021q actually installs unique pvid's into |
| 1554 | * each front panel port. This is good for identification but breaks |
| 1555 | * learning badly - the VID of the learnt FDB entry is unique, aka |
| 1556 | * no frames coming from any other port are going to have it. So |
| 1557 | * for forwarding purposes, this is as though learning was broken |
| 1558 | * (all frames get flooded). |
| 1559 | */ |
| 1560 | table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS]; |
| 1561 | l2_lookup_params = table->entries; |
| 1562 | l2_lookup_params->shared_learn = !enabled; |
| 1563 | |
Vladimir Oltean | 070ca3b | 2019-06-08 15:04:30 +0300 | [diff] [blame] | 1564 | rc = sja1105_static_config_reload(priv); |
Vladimir Oltean | 6666ceb | 2019-05-02 23:23:34 +0300 | [diff] [blame] | 1565 | if (rc) |
| 1566 | dev_err(ds->dev, "Failed to change VLAN Ethertype\n"); |
| 1567 | |
Vladimir Oltean | 227d07a | 2019-05-05 13:19:27 +0300 | [diff] [blame] | 1568 | /* Switch port identification based on 802.1Q is only passable |
| 1569 | * if we are not under a vlan_filtering bridge. So make sure |
| 1570 | * the two configurations are mutually exclusive. |
| 1571 | */ |
| 1572 | return sja1105_setup_8021q_tagging(ds, !enabled); |
Vladimir Oltean | 6666ceb | 2019-05-02 23:23:34 +0300 | [diff] [blame] | 1573 | } |
| 1574 | |
| 1575 | static void sja1105_vlan_add(struct dsa_switch *ds, int port, |
| 1576 | const struct switchdev_obj_port_vlan *vlan) |
| 1577 | { |
| 1578 | struct sja1105_private *priv = ds->priv; |
| 1579 | u16 vid; |
| 1580 | int rc; |
| 1581 | |
| 1582 | for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) { |
| 1583 | rc = sja1105_vlan_apply(priv, port, vid, true, vlan->flags & |
| 1584 | BRIDGE_VLAN_INFO_UNTAGGED); |
| 1585 | if (rc < 0) { |
| 1586 | dev_err(ds->dev, "Failed to add VLAN %d to port %d: %d\n", |
| 1587 | vid, port, rc); |
| 1588 | return; |
| 1589 | } |
| 1590 | if (vlan->flags & BRIDGE_VLAN_INFO_PVID) { |
| 1591 | rc = sja1105_pvid_apply(ds->priv, port, vid); |
| 1592 | if (rc < 0) { |
| 1593 | dev_err(ds->dev, "Failed to set pvid %d on port %d: %d\n", |
| 1594 | vid, port, rc); |
| 1595 | return; |
| 1596 | } |
| 1597 | } |
| 1598 | } |
| 1599 | } |
| 1600 | |
| 1601 | static int sja1105_vlan_del(struct dsa_switch *ds, int port, |
| 1602 | const struct switchdev_obj_port_vlan *vlan) |
| 1603 | { |
| 1604 | struct sja1105_private *priv = ds->priv; |
| 1605 | u16 vid; |
| 1606 | int rc; |
| 1607 | |
| 1608 | for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) { |
| 1609 | rc = sja1105_vlan_apply(priv, port, vid, false, vlan->flags & |
| 1610 | BRIDGE_VLAN_INFO_UNTAGGED); |
| 1611 | if (rc < 0) { |
| 1612 | dev_err(ds->dev, "Failed to remove VLAN %d from port %d: %d\n", |
| 1613 | vid, port, rc); |
| 1614 | return rc; |
| 1615 | } |
| 1616 | } |
| 1617 | return 0; |
| 1618 | } |
| 1619 | |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 1620 | /* The programming model for the SJA1105 switch is "all-at-once" via static |
| 1621 | * configuration tables. Some of these can be dynamically modified at runtime, |
| 1622 | * but not the xMII mode parameters table. |
| 1623 | * Furthermode, some PHYs may not have crystals for generating their clocks |
| 1624 | * (e.g. RMII). Instead, their 50MHz clock is supplied via the SJA1105 port's |
| 1625 | * ref_clk pin. So port clocking needs to be initialized early, before |
| 1626 | * connecting to PHYs is attempted, otherwise they won't respond through MDIO. |
| 1627 | * Setting correct PHY link speed does not matter now. |
| 1628 | * But dsa_slave_phy_setup is called later than sja1105_setup, so the PHY |
| 1629 | * bindings are not yet parsed by DSA core. We need to parse early so that we |
| 1630 | * can populate the xMII mode parameters table. |
| 1631 | */ |
| 1632 | static int sja1105_setup(struct dsa_switch *ds) |
| 1633 | { |
| 1634 | struct sja1105_dt_port ports[SJA1105_NUM_PORTS]; |
| 1635 | struct sja1105_private *priv = ds->priv; |
| 1636 | int rc; |
| 1637 | |
| 1638 | rc = sja1105_parse_dt(priv, ports); |
| 1639 | if (rc < 0) { |
| 1640 | dev_err(ds->dev, "Failed to parse DT: %d\n", rc); |
| 1641 | return rc; |
| 1642 | } |
Vladimir Oltean | f5b8631 | 2019-05-02 23:23:32 +0300 | [diff] [blame] | 1643 | |
| 1644 | /* Error out early if internal delays are required through DT |
| 1645 | * and we can't apply them. |
| 1646 | */ |
| 1647 | rc = sja1105_parse_rgmii_delays(priv, ports); |
| 1648 | if (rc < 0) { |
| 1649 | dev_err(ds->dev, "RGMII delay not supported\n"); |
| 1650 | return rc; |
| 1651 | } |
| 1652 | |
Vladimir Oltean | 61c7712 | 2019-10-12 02:18:14 +0300 | [diff] [blame] | 1653 | rc = sja1105_ptp_clock_register(ds); |
Vladimir Oltean | bb77f36 | 2019-06-08 15:04:34 +0300 | [diff] [blame] | 1654 | if (rc < 0) { |
| 1655 | dev_err(ds->dev, "Failed to register PTP clock: %d\n", rc); |
| 1656 | return rc; |
| 1657 | } |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 1658 | /* Create and send configuration down to device */ |
| 1659 | rc = sja1105_static_config_load(priv, ports); |
| 1660 | if (rc < 0) { |
| 1661 | dev_err(ds->dev, "Failed to load static config: %d\n", rc); |
| 1662 | return rc; |
| 1663 | } |
| 1664 | /* Configure the CGU (PHY link modes and speeds) */ |
| 1665 | rc = sja1105_clocking_setup(priv); |
| 1666 | if (rc < 0) { |
| 1667 | dev_err(ds->dev, "Failed to configure MII clocking: %d\n", rc); |
| 1668 | return rc; |
| 1669 | } |
Vladimir Oltean | 6666ceb | 2019-05-02 23:23:34 +0300 | [diff] [blame] | 1670 | /* On SJA1105, VLAN filtering per se is always enabled in hardware. |
| 1671 | * The only thing we can do to disable it is lie about what the 802.1Q |
| 1672 | * EtherType is. |
| 1673 | * So it will still try to apply VLAN filtering, but all ingress |
| 1674 | * traffic (except frames received with EtherType of ETH_P_SJA1105) |
| 1675 | * will be internally tagged with a distorted VLAN header where the |
| 1676 | * TPID is ETH_P_SJA1105, and the VLAN ID is the port pvid. |
| 1677 | */ |
| 1678 | ds->vlan_filtering_is_global = true; |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 1679 | |
Vladimir Oltean | 5f06c63 | 2019-09-15 05:00:01 +0300 | [diff] [blame] | 1680 | /* Advertise the 8 egress queues */ |
| 1681 | ds->num_tx_queues = SJA1105_NUM_TC; |
| 1682 | |
Vladimir Oltean | 227d07a | 2019-05-05 13:19:27 +0300 | [diff] [blame] | 1683 | /* The DSA/switchdev model brings up switch ports in standalone mode by |
| 1684 | * default, and that means vlan_filtering is 0 since they're not under |
| 1685 | * a bridge, so it's safe to set up switch tagging at this time. |
| 1686 | */ |
| 1687 | return sja1105_setup_8021q_tagging(ds, true); |
| 1688 | } |
| 1689 | |
Vladimir Oltean | f3097be | 2019-06-08 15:04:42 +0300 | [diff] [blame] | 1690 | static void sja1105_teardown(struct dsa_switch *ds) |
| 1691 | { |
| 1692 | struct sja1105_private *priv = ds->priv; |
| 1693 | |
Vladimir Oltean | 317ab5b | 2019-09-15 05:00:02 +0300 | [diff] [blame] | 1694 | sja1105_tas_teardown(ds); |
Vladimir Oltean | 61c7712 | 2019-10-12 02:18:14 +0300 | [diff] [blame] | 1695 | sja1105_ptp_clock_unregister(ds); |
Vladimir Oltean | 6cb0abb | 2019-08-05 01:38:46 +0300 | [diff] [blame] | 1696 | sja1105_static_config_free(&priv->static_config); |
Vladimir Oltean | f3097be | 2019-06-08 15:04:42 +0300 | [diff] [blame] | 1697 | } |
| 1698 | |
Vladimir Oltean | e9bf969 | 2019-08-25 22:46:30 +0300 | [diff] [blame] | 1699 | static int sja1105_port_enable(struct dsa_switch *ds, int port, |
| 1700 | struct phy_device *phy) |
| 1701 | { |
| 1702 | struct net_device *slave; |
| 1703 | |
| 1704 | if (!dsa_is_user_port(ds, port)) |
| 1705 | return 0; |
| 1706 | |
Vivien Didelot | 68bb8ea | 2019-10-21 16:51:15 -0400 | [diff] [blame] | 1707 | slave = dsa_to_port(ds, port)->slave; |
Vladimir Oltean | e9bf969 | 2019-08-25 22:46:30 +0300 | [diff] [blame] | 1708 | |
| 1709 | slave->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER; |
| 1710 | |
| 1711 | return 0; |
| 1712 | } |
| 1713 | |
Vladimir Oltean | 227d07a | 2019-05-05 13:19:27 +0300 | [diff] [blame] | 1714 | static int sja1105_mgmt_xmit(struct dsa_switch *ds, int port, int slot, |
Vladimir Oltean | 47ed985 | 2019-06-08 15:04:35 +0300 | [diff] [blame] | 1715 | struct sk_buff *skb, bool takets) |
Vladimir Oltean | 227d07a | 2019-05-05 13:19:27 +0300 | [diff] [blame] | 1716 | { |
| 1717 | struct sja1105_mgmt_entry mgmt_route = {0}; |
| 1718 | struct sja1105_private *priv = ds->priv; |
| 1719 | struct ethhdr *hdr; |
| 1720 | int timeout = 10; |
| 1721 | int rc; |
| 1722 | |
| 1723 | hdr = eth_hdr(skb); |
| 1724 | |
| 1725 | mgmt_route.macaddr = ether_addr_to_u64(hdr->h_dest); |
| 1726 | mgmt_route.destports = BIT(port); |
| 1727 | mgmt_route.enfport = 1; |
Vladimir Oltean | 47ed985 | 2019-06-08 15:04:35 +0300 | [diff] [blame] | 1728 | mgmt_route.tsreg = 0; |
| 1729 | mgmt_route.takets = takets; |
Vladimir Oltean | 227d07a | 2019-05-05 13:19:27 +0300 | [diff] [blame] | 1730 | |
| 1731 | rc = sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE, |
| 1732 | slot, &mgmt_route, true); |
| 1733 | if (rc < 0) { |
| 1734 | kfree_skb(skb); |
| 1735 | return rc; |
| 1736 | } |
| 1737 | |
| 1738 | /* Transfer skb to the host port. */ |
Vivien Didelot | 68bb8ea | 2019-10-21 16:51:15 -0400 | [diff] [blame] | 1739 | dsa_enqueue_skb(skb, dsa_to_port(ds, port)->slave); |
Vladimir Oltean | 227d07a | 2019-05-05 13:19:27 +0300 | [diff] [blame] | 1740 | |
| 1741 | /* Wait until the switch has processed the frame */ |
| 1742 | do { |
| 1743 | rc = sja1105_dynamic_config_read(priv, BLK_IDX_MGMT_ROUTE, |
| 1744 | slot, &mgmt_route); |
| 1745 | if (rc < 0) { |
| 1746 | dev_err_ratelimited(priv->ds->dev, |
| 1747 | "failed to poll for mgmt route\n"); |
| 1748 | continue; |
| 1749 | } |
| 1750 | |
| 1751 | /* UM10944: The ENFPORT flag of the respective entry is |
| 1752 | * cleared when a match is found. The host can use this |
| 1753 | * flag as an acknowledgment. |
| 1754 | */ |
| 1755 | cpu_relax(); |
| 1756 | } while (mgmt_route.enfport && --timeout); |
| 1757 | |
| 1758 | if (!timeout) { |
| 1759 | /* Clean up the management route so that a follow-up |
| 1760 | * frame may not match on it by mistake. |
Vladimir Oltean | 2a7e740 | 2019-06-03 00:15:33 +0300 | [diff] [blame] | 1761 | * This is only hardware supported on P/Q/R/S - on E/T it is |
| 1762 | * a no-op and we are silently discarding the -EOPNOTSUPP. |
Vladimir Oltean | 227d07a | 2019-05-05 13:19:27 +0300 | [diff] [blame] | 1763 | */ |
| 1764 | sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE, |
| 1765 | slot, &mgmt_route, false); |
| 1766 | dev_err_ratelimited(priv->ds->dev, "xmit timed out\n"); |
| 1767 | } |
| 1768 | |
| 1769 | return NETDEV_TX_OK; |
| 1770 | } |
| 1771 | |
| 1772 | /* Deferred work is unfortunately necessary because setting up the management |
| 1773 | * route cannot be done from atomit context (SPI transfer takes a sleepable |
| 1774 | * lock on the bus) |
| 1775 | */ |
| 1776 | static netdev_tx_t sja1105_port_deferred_xmit(struct dsa_switch *ds, int port, |
| 1777 | struct sk_buff *skb) |
| 1778 | { |
| 1779 | struct sja1105_private *priv = ds->priv; |
| 1780 | struct sja1105_port *sp = &priv->ports[port]; |
| 1781 | int slot = sp->mgmt_slot; |
Vladimir Oltean | 47ed985 | 2019-06-08 15:04:35 +0300 | [diff] [blame] | 1782 | struct sk_buff *clone; |
Vladimir Oltean | 227d07a | 2019-05-05 13:19:27 +0300 | [diff] [blame] | 1783 | |
| 1784 | /* The tragic fact about the switch having 4x2 slots for installing |
| 1785 | * management routes is that all of them except one are actually |
| 1786 | * useless. |
| 1787 | * If 2 slots are simultaneously configured for two BPDUs sent to the |
| 1788 | * same (multicast) DMAC but on different egress ports, the switch |
| 1789 | * would confuse them and redirect first frame it receives on the CPU |
| 1790 | * port towards the port configured on the numerically first slot |
| 1791 | * (therefore wrong port), then second received frame on second slot |
| 1792 | * (also wrong port). |
| 1793 | * So for all practical purposes, there needs to be a lock that |
| 1794 | * prevents that from happening. The slot used here is utterly useless |
| 1795 | * (could have simply been 0 just as fine), but we are doing it |
| 1796 | * nonetheless, in case a smarter idea ever comes up in the future. |
| 1797 | */ |
| 1798 | mutex_lock(&priv->mgmt_lock); |
| 1799 | |
Vladimir Oltean | 47ed985 | 2019-06-08 15:04:35 +0300 | [diff] [blame] | 1800 | /* The clone, if there, was made by dsa_skb_tx_timestamp */ |
| 1801 | clone = DSA_SKB_CB(skb)->clone; |
Vladimir Oltean | 227d07a | 2019-05-05 13:19:27 +0300 | [diff] [blame] | 1802 | |
Vladimir Oltean | 47ed985 | 2019-06-08 15:04:35 +0300 | [diff] [blame] | 1803 | sja1105_mgmt_xmit(ds, port, slot, skb, !!clone); |
| 1804 | |
| 1805 | if (!clone) |
| 1806 | goto out; |
| 1807 | |
Vladimir Oltean | a9d6ed7 | 2019-10-12 02:18:15 +0300 | [diff] [blame] | 1808 | sja1105_ptp_txtstamp_skb(ds, slot, clone); |
Vladimir Oltean | 47ed985 | 2019-06-08 15:04:35 +0300 | [diff] [blame] | 1809 | |
Vladimir Oltean | 47ed985 | 2019-06-08 15:04:35 +0300 | [diff] [blame] | 1810 | out: |
Vladimir Oltean | 227d07a | 2019-05-05 13:19:27 +0300 | [diff] [blame] | 1811 | mutex_unlock(&priv->mgmt_lock); |
| 1812 | return NETDEV_TX_OK; |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 1813 | } |
| 1814 | |
Vladimir Oltean | 8456721 | 2019-05-02 23:23:36 +0300 | [diff] [blame] | 1815 | /* The MAXAGE setting belongs to the L2 Forwarding Parameters table, |
| 1816 | * which cannot be reconfigured at runtime. So a switch reset is required. |
| 1817 | */ |
| 1818 | static int sja1105_set_ageing_time(struct dsa_switch *ds, |
| 1819 | unsigned int ageing_time) |
| 1820 | { |
| 1821 | struct sja1105_l2_lookup_params_entry *l2_lookup_params; |
| 1822 | struct sja1105_private *priv = ds->priv; |
| 1823 | struct sja1105_table *table; |
| 1824 | unsigned int maxage; |
| 1825 | |
| 1826 | table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS]; |
| 1827 | l2_lookup_params = table->entries; |
| 1828 | |
| 1829 | maxage = SJA1105_AGEING_TIME_MS(ageing_time); |
| 1830 | |
| 1831 | if (l2_lookup_params->maxage == maxage) |
| 1832 | return 0; |
| 1833 | |
| 1834 | l2_lookup_params->maxage = maxage; |
| 1835 | |
| 1836 | return sja1105_static_config_reload(priv); |
| 1837 | } |
| 1838 | |
Vladimir Oltean | 317ab5b | 2019-09-15 05:00:02 +0300 | [diff] [blame] | 1839 | static int sja1105_port_setup_tc(struct dsa_switch *ds, int port, |
| 1840 | enum tc_setup_type type, |
| 1841 | void *type_data) |
| 1842 | { |
| 1843 | switch (type) { |
| 1844 | case TC_SETUP_QDISC_TAPRIO: |
| 1845 | return sja1105_setup_tc_taprio(ds, port, type_data); |
| 1846 | default: |
| 1847 | return -EOPNOTSUPP; |
| 1848 | } |
| 1849 | } |
| 1850 | |
Vladimir Oltean | 511e6ca | 2019-10-04 03:33:47 +0300 | [diff] [blame] | 1851 | /* We have a single mirror (@to) port, but can configure ingress and egress |
| 1852 | * mirroring on all other (@from) ports. |
| 1853 | * We need to allow mirroring rules only as long as the @to port is always the |
| 1854 | * same, and we need to unset the @to port from mirr_port only when there is no |
| 1855 | * mirroring rule that references it. |
| 1856 | */ |
| 1857 | static int sja1105_mirror_apply(struct sja1105_private *priv, int from, int to, |
| 1858 | bool ingress, bool enabled) |
| 1859 | { |
| 1860 | struct sja1105_general_params_entry *general_params; |
| 1861 | struct sja1105_mac_config_entry *mac; |
| 1862 | struct sja1105_table *table; |
| 1863 | bool already_enabled; |
| 1864 | u64 new_mirr_port; |
| 1865 | int rc; |
| 1866 | |
| 1867 | table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; |
| 1868 | general_params = table->entries; |
| 1869 | |
| 1870 | mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; |
| 1871 | |
| 1872 | already_enabled = (general_params->mirr_port != SJA1105_NUM_PORTS); |
| 1873 | if (already_enabled && enabled && general_params->mirr_port != to) { |
| 1874 | dev_err(priv->ds->dev, |
| 1875 | "Delete mirroring rules towards port %llu first\n", |
| 1876 | general_params->mirr_port); |
| 1877 | return -EBUSY; |
| 1878 | } |
| 1879 | |
| 1880 | new_mirr_port = to; |
| 1881 | if (!enabled) { |
| 1882 | bool keep = false; |
| 1883 | int port; |
| 1884 | |
| 1885 | /* Anybody still referencing mirr_port? */ |
| 1886 | for (port = 0; port < SJA1105_NUM_PORTS; port++) { |
| 1887 | if (mac[port].ing_mirr || mac[port].egr_mirr) { |
| 1888 | keep = true; |
| 1889 | break; |
| 1890 | } |
| 1891 | } |
| 1892 | /* Unset already_enabled for next time */ |
| 1893 | if (!keep) |
| 1894 | new_mirr_port = SJA1105_NUM_PORTS; |
| 1895 | } |
| 1896 | if (new_mirr_port != general_params->mirr_port) { |
| 1897 | general_params->mirr_port = new_mirr_port; |
| 1898 | |
| 1899 | rc = sja1105_dynamic_config_write(priv, BLK_IDX_GENERAL_PARAMS, |
| 1900 | 0, general_params, true); |
| 1901 | if (rc < 0) |
| 1902 | return rc; |
| 1903 | } |
| 1904 | |
| 1905 | if (ingress) |
| 1906 | mac[from].ing_mirr = enabled; |
| 1907 | else |
| 1908 | mac[from].egr_mirr = enabled; |
| 1909 | |
| 1910 | return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, from, |
| 1911 | &mac[from], true); |
| 1912 | } |
| 1913 | |
| 1914 | static int sja1105_mirror_add(struct dsa_switch *ds, int port, |
| 1915 | struct dsa_mall_mirror_tc_entry *mirror, |
| 1916 | bool ingress) |
| 1917 | { |
| 1918 | return sja1105_mirror_apply(ds->priv, port, mirror->to_local_port, |
| 1919 | ingress, true); |
| 1920 | } |
| 1921 | |
| 1922 | static void sja1105_mirror_del(struct dsa_switch *ds, int port, |
| 1923 | struct dsa_mall_mirror_tc_entry *mirror) |
| 1924 | { |
| 1925 | sja1105_mirror_apply(ds->priv, port, mirror->to_local_port, |
| 1926 | mirror->ingress, false); |
| 1927 | } |
| 1928 | |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 1929 | static const struct dsa_switch_ops sja1105_switch_ops = { |
| 1930 | .get_tag_protocol = sja1105_get_tag_protocol, |
| 1931 | .setup = sja1105_setup, |
Vladimir Oltean | f3097be | 2019-06-08 15:04:42 +0300 | [diff] [blame] | 1932 | .teardown = sja1105_teardown, |
Vladimir Oltean | 8456721 | 2019-05-02 23:23:36 +0300 | [diff] [blame] | 1933 | .set_ageing_time = sja1105_set_ageing_time, |
Vladimir Oltean | ad9f299 | 2019-05-02 23:23:38 +0300 | [diff] [blame] | 1934 | .phylink_validate = sja1105_phylink_validate, |
Vladimir Oltean | af7cd03 | 2019-05-28 20:38:17 +0300 | [diff] [blame] | 1935 | .phylink_mac_config = sja1105_mac_config, |
Vladimir Oltean | 8400cff | 2019-06-08 16:03:44 +0300 | [diff] [blame] | 1936 | .phylink_mac_link_up = sja1105_mac_link_up, |
| 1937 | .phylink_mac_link_down = sja1105_mac_link_down, |
Vladimir Oltean | 52c34e6 | 2019-05-02 23:23:35 +0300 | [diff] [blame] | 1938 | .get_strings = sja1105_get_strings, |
| 1939 | .get_ethtool_stats = sja1105_get_ethtool_stats, |
| 1940 | .get_sset_count = sja1105_get_sset_count, |
Vladimir Oltean | bb77f36 | 2019-06-08 15:04:34 +0300 | [diff] [blame] | 1941 | .get_ts_info = sja1105_get_ts_info, |
Vladimir Oltean | e9bf969 | 2019-08-25 22:46:30 +0300 | [diff] [blame] | 1942 | .port_enable = sja1105_port_enable, |
Vladimir Oltean | 291d1e7 | 2019-05-02 23:23:31 +0300 | [diff] [blame] | 1943 | .port_fdb_dump = sja1105_fdb_dump, |
| 1944 | .port_fdb_add = sja1105_fdb_add, |
| 1945 | .port_fdb_del = sja1105_fdb_del, |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 1946 | .port_bridge_join = sja1105_bridge_join, |
| 1947 | .port_bridge_leave = sja1105_bridge_leave, |
Vladimir Oltean | 640f763 | 2019-05-05 13:19:28 +0300 | [diff] [blame] | 1948 | .port_stp_state_set = sja1105_bridge_stp_state_set, |
Vladimir Oltean | 6666ceb | 2019-05-02 23:23:34 +0300 | [diff] [blame] | 1949 | .port_vlan_prepare = sja1105_vlan_prepare, |
| 1950 | .port_vlan_filtering = sja1105_vlan_filtering, |
| 1951 | .port_vlan_add = sja1105_vlan_add, |
| 1952 | .port_vlan_del = sja1105_vlan_del, |
Vladimir Oltean | 291d1e7 | 2019-05-02 23:23:31 +0300 | [diff] [blame] | 1953 | .port_mdb_prepare = sja1105_mdb_prepare, |
| 1954 | .port_mdb_add = sja1105_mdb_add, |
| 1955 | .port_mdb_del = sja1105_mdb_del, |
Vladimir Oltean | 227d07a | 2019-05-05 13:19:27 +0300 | [diff] [blame] | 1956 | .port_deferred_xmit = sja1105_port_deferred_xmit, |
Vladimir Oltean | a602afd | 2019-06-08 15:04:43 +0300 | [diff] [blame] | 1957 | .port_hwtstamp_get = sja1105_hwtstamp_get, |
| 1958 | .port_hwtstamp_set = sja1105_hwtstamp_set, |
Vladimir Oltean | f3097be | 2019-06-08 15:04:42 +0300 | [diff] [blame] | 1959 | .port_rxtstamp = sja1105_port_rxtstamp, |
Vladimir Oltean | 47ed985 | 2019-06-08 15:04:35 +0300 | [diff] [blame] | 1960 | .port_txtstamp = sja1105_port_txtstamp, |
Vladimir Oltean | 317ab5b | 2019-09-15 05:00:02 +0300 | [diff] [blame] | 1961 | .port_setup_tc = sja1105_port_setup_tc, |
Vladimir Oltean | 511e6ca | 2019-10-04 03:33:47 +0300 | [diff] [blame] | 1962 | .port_mirror_add = sja1105_mirror_add, |
| 1963 | .port_mirror_del = sja1105_mirror_del, |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 1964 | }; |
| 1965 | |
| 1966 | static int sja1105_check_device_id(struct sja1105_private *priv) |
| 1967 | { |
| 1968 | const struct sja1105_regs *regs = priv->info->regs; |
| 1969 | u8 prod_id[SJA1105_SIZE_DEVICE_ID] = {0}; |
| 1970 | struct device *dev = &priv->spidev->dev; |
Vladimir Oltean | dff7962 | 2019-10-01 22:18:00 +0300 | [diff] [blame] | 1971 | u32 device_id; |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 1972 | u64 part_no; |
| 1973 | int rc; |
| 1974 | |
Vladimir Oltean | dff7962 | 2019-10-01 22:18:00 +0300 | [diff] [blame] | 1975 | rc = sja1105_xfer_u32(priv, SPI_READ, regs->device_id, &device_id); |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 1976 | if (rc < 0) |
| 1977 | return rc; |
| 1978 | |
| 1979 | if (device_id != priv->info->device_id) { |
Vladimir Oltean | dff7962 | 2019-10-01 22:18:00 +0300 | [diff] [blame] | 1980 | dev_err(dev, "Expected device ID 0x%llx but read 0x%x\n", |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 1981 | priv->info->device_id, device_id); |
| 1982 | return -ENODEV; |
| 1983 | } |
| 1984 | |
Vladimir Oltean | 1bd4487 | 2019-10-01 22:18:01 +0300 | [diff] [blame] | 1985 | rc = sja1105_xfer_buf(priv, SPI_READ, regs->prod_id, prod_id, |
| 1986 | SJA1105_SIZE_DEVICE_ID); |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 1987 | if (rc < 0) |
| 1988 | return rc; |
| 1989 | |
| 1990 | sja1105_unpack(prod_id, &part_no, 19, 4, SJA1105_SIZE_DEVICE_ID); |
| 1991 | |
| 1992 | if (part_no != priv->info->part_no) { |
| 1993 | dev_err(dev, "Expected part number 0x%llx but read 0x%llx\n", |
| 1994 | priv->info->part_no, part_no); |
| 1995 | return -ENODEV; |
| 1996 | } |
| 1997 | |
| 1998 | return 0; |
| 1999 | } |
| 2000 | |
| 2001 | static int sja1105_probe(struct spi_device *spi) |
| 2002 | { |
Vladimir Oltean | 844d7ed | 2019-06-08 15:04:40 +0300 | [diff] [blame] | 2003 | struct sja1105_tagger_data *tagger_data; |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 2004 | struct device *dev = &spi->dev; |
| 2005 | struct sja1105_private *priv; |
| 2006 | struct dsa_switch *ds; |
Vladimir Oltean | 227d07a | 2019-05-05 13:19:27 +0300 | [diff] [blame] | 2007 | int rc, i; |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 2008 | |
| 2009 | if (!dev->of_node) { |
| 2010 | dev_err(dev, "No DTS bindings for SJA1105 driver\n"); |
| 2011 | return -EINVAL; |
| 2012 | } |
| 2013 | |
| 2014 | priv = devm_kzalloc(dev, sizeof(struct sja1105_private), GFP_KERNEL); |
| 2015 | if (!priv) |
| 2016 | return -ENOMEM; |
| 2017 | |
| 2018 | /* Configure the optional reset pin and bring up switch */ |
| 2019 | priv->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); |
| 2020 | if (IS_ERR(priv->reset_gpio)) |
| 2021 | dev_dbg(dev, "reset-gpios not defined, ignoring\n"); |
| 2022 | else |
| 2023 | sja1105_hw_reset(priv->reset_gpio, 1, 1); |
| 2024 | |
| 2025 | /* Populate our driver private structure (priv) based on |
| 2026 | * the device tree node that was probed (spi) |
| 2027 | */ |
| 2028 | priv->spidev = spi; |
| 2029 | spi_set_drvdata(spi, priv); |
| 2030 | |
| 2031 | /* Configure the SPI bus */ |
| 2032 | spi->bits_per_word = 8; |
| 2033 | rc = spi_setup(spi); |
| 2034 | if (rc < 0) { |
| 2035 | dev_err(dev, "Could not init SPI\n"); |
| 2036 | return rc; |
| 2037 | } |
| 2038 | |
| 2039 | priv->info = of_device_get_match_data(dev); |
| 2040 | |
| 2041 | /* Detect hardware device */ |
| 2042 | rc = sja1105_check_device_id(priv); |
| 2043 | if (rc < 0) { |
| 2044 | dev_err(dev, "Device ID check failed: %d\n", rc); |
| 2045 | return rc; |
| 2046 | } |
| 2047 | |
| 2048 | dev_info(dev, "Probed switch chip: %s\n", priv->info->name); |
| 2049 | |
| 2050 | ds = dsa_switch_alloc(dev, SJA1105_NUM_PORTS); |
| 2051 | if (!ds) |
| 2052 | return -ENOMEM; |
| 2053 | |
| 2054 | ds->ops = &sja1105_switch_ops; |
| 2055 | ds->priv = priv; |
| 2056 | priv->ds = ds; |
| 2057 | |
Vladimir Oltean | 844d7ed | 2019-06-08 15:04:40 +0300 | [diff] [blame] | 2058 | tagger_data = &priv->tagger_data; |
Vladimir Oltean | 844d7ed | 2019-06-08 15:04:40 +0300 | [diff] [blame] | 2059 | |
Vivien Didelot | d5a619b | 2019-10-21 16:51:28 -0400 | [diff] [blame^] | 2060 | mutex_init(&priv->ptp_data.lock); |
| 2061 | mutex_init(&priv->mgmt_lock); |
| 2062 | |
| 2063 | sja1105_tas_setup(ds); |
| 2064 | |
| 2065 | rc = dsa_register_switch(priv->ds); |
| 2066 | if (rc) |
| 2067 | return rc; |
| 2068 | |
Vladimir Oltean | 227d07a | 2019-05-05 13:19:27 +0300 | [diff] [blame] | 2069 | /* Connections between dsa_port and sja1105_port */ |
| 2070 | for (i = 0; i < SJA1105_NUM_PORTS; i++) { |
| 2071 | struct sja1105_port *sp = &priv->ports[i]; |
| 2072 | |
Vivien Didelot | 68bb8ea | 2019-10-21 16:51:15 -0400 | [diff] [blame] | 2073 | dsa_to_port(ds, i)->priv = sp; |
| 2074 | sp->dp = dsa_to_port(ds, i); |
Vladimir Oltean | 844d7ed | 2019-06-08 15:04:40 +0300 | [diff] [blame] | 2075 | sp->data = tagger_data; |
Vladimir Oltean | 227d07a | 2019-05-05 13:19:27 +0300 | [diff] [blame] | 2076 | } |
Vladimir Oltean | 227d07a | 2019-05-05 13:19:27 +0300 | [diff] [blame] | 2077 | |
Vivien Didelot | d5a619b | 2019-10-21 16:51:28 -0400 | [diff] [blame^] | 2078 | return 0; |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 2079 | } |
| 2080 | |
| 2081 | static int sja1105_remove(struct spi_device *spi) |
| 2082 | { |
| 2083 | struct sja1105_private *priv = spi_get_drvdata(spi); |
| 2084 | |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 2085 | dsa_unregister_switch(priv->ds); |
Vladimir Oltean | 8aa9ebc | 2019-05-02 23:23:30 +0300 | [diff] [blame] | 2086 | return 0; |
| 2087 | } |
| 2088 | |
| 2089 | static const struct of_device_id sja1105_dt_ids[] = { |
| 2090 | { .compatible = "nxp,sja1105e", .data = &sja1105e_info }, |
| 2091 | { .compatible = "nxp,sja1105t", .data = &sja1105t_info }, |
| 2092 | { .compatible = "nxp,sja1105p", .data = &sja1105p_info }, |
| 2093 | { .compatible = "nxp,sja1105q", .data = &sja1105q_info }, |
| 2094 | { .compatible = "nxp,sja1105r", .data = &sja1105r_info }, |
| 2095 | { .compatible = "nxp,sja1105s", .data = &sja1105s_info }, |
| 2096 | { /* sentinel */ }, |
| 2097 | }; |
| 2098 | MODULE_DEVICE_TABLE(of, sja1105_dt_ids); |
| 2099 | |
| 2100 | static struct spi_driver sja1105_driver = { |
| 2101 | .driver = { |
| 2102 | .name = "sja1105", |
| 2103 | .owner = THIS_MODULE, |
| 2104 | .of_match_table = of_match_ptr(sja1105_dt_ids), |
| 2105 | }, |
| 2106 | .probe = sja1105_probe, |
| 2107 | .remove = sja1105_remove, |
| 2108 | }; |
| 2109 | |
| 2110 | module_spi_driver(sja1105_driver); |
| 2111 | |
| 2112 | MODULE_AUTHOR("Vladimir Oltean <olteanv@gmail.com>"); |
| 2113 | MODULE_AUTHOR("Georg Waibel <georg.waibel@sensor-technik.de>"); |
| 2114 | MODULE_DESCRIPTION("SJA1105 Driver"); |
| 2115 | MODULE_LICENSE("GPL v2"); |