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