18aa9ebccSVladimir Oltean // SPDX-License-Identifier: GPL-2.0 28aa9ebccSVladimir Oltean /* Copyright (c) 2018, Sensor-Technik Wiedemann GmbH 38aa9ebccSVladimir Oltean * Copyright (c) 2018-2019, Vladimir Oltean <olteanv@gmail.com> 48aa9ebccSVladimir Oltean */ 58aa9ebccSVladimir Oltean 68aa9ebccSVladimir Oltean #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 78aa9ebccSVladimir Oltean 88aa9ebccSVladimir Oltean #include <linux/delay.h> 98aa9ebccSVladimir Oltean #include <linux/module.h> 108aa9ebccSVladimir Oltean #include <linux/printk.h> 118aa9ebccSVladimir Oltean #include <linux/spi/spi.h> 128aa9ebccSVladimir Oltean #include <linux/errno.h> 138aa9ebccSVladimir Oltean #include <linux/gpio/consumer.h> 14ad9f299aSVladimir Oltean #include <linux/phylink.h> 158aa9ebccSVladimir Oltean #include <linux/of.h> 168aa9ebccSVladimir Oltean #include <linux/of_net.h> 178aa9ebccSVladimir Oltean #include <linux/of_mdio.h> 188aa9ebccSVladimir Oltean #include <linux/of_device.h> 193ad1d171SVladimir Oltean #include <linux/pcs/pcs-xpcs.h> 208aa9ebccSVladimir Oltean #include <linux/netdev_features.h> 218aa9ebccSVladimir Oltean #include <linux/netdevice.h> 228aa9ebccSVladimir Oltean #include <linux/if_bridge.h> 238aa9ebccSVladimir Oltean #include <linux/if_ether.h> 24227d07a0SVladimir Oltean #include <linux/dsa/8021q.h> 258aa9ebccSVladimir Oltean #include "sja1105.h" 26317ab5b8SVladimir Oltean #include "sja1105_tas.h" 278aa9ebccSVladimir Oltean 284d942354SVladimir Oltean #define SJA1105_UNKNOWN_MULTICAST 0x010000000000ull 29ed040abcSVladimir Oltean #define SJA1105_DEFAULT_VLAN (VLAN_N_VID - 1) 304d942354SVladimir Oltean 31ac02a451SVladimir Oltean static const struct dsa_switch_ops sja1105_switch_ops; 32ac02a451SVladimir Oltean 338aa9ebccSVladimir Oltean static void sja1105_hw_reset(struct gpio_desc *gpio, unsigned int pulse_len, 348aa9ebccSVladimir Oltean unsigned int startup_delay) 358aa9ebccSVladimir Oltean { 368aa9ebccSVladimir Oltean gpiod_set_value_cansleep(gpio, 1); 378aa9ebccSVladimir Oltean /* Wait for minimum reset pulse length */ 388aa9ebccSVladimir Oltean msleep(pulse_len); 398aa9ebccSVladimir Oltean gpiod_set_value_cansleep(gpio, 0); 408aa9ebccSVladimir Oltean /* Wait until chip is ready after reset */ 418aa9ebccSVladimir Oltean msleep(startup_delay); 428aa9ebccSVladimir Oltean } 438aa9ebccSVladimir Oltean 448aa9ebccSVladimir Oltean static void 458aa9ebccSVladimir Oltean sja1105_port_allow_traffic(struct sja1105_l2_forwarding_entry *l2_fwd, 468aa9ebccSVladimir Oltean int from, int to, bool allow) 478aa9ebccSVladimir Oltean { 484d942354SVladimir Oltean if (allow) 498aa9ebccSVladimir Oltean l2_fwd[from].reach_port |= BIT(to); 504d942354SVladimir Oltean else 518aa9ebccSVladimir Oltean l2_fwd[from].reach_port &= ~BIT(to); 528aa9ebccSVladimir Oltean } 538aa9ebccSVladimir Oltean 547f7ccdeaSVladimir Oltean static bool sja1105_can_forward(struct sja1105_l2_forwarding_entry *l2_fwd, 557f7ccdeaSVladimir Oltean int from, int to) 567f7ccdeaSVladimir Oltean { 577f7ccdeaSVladimir Oltean return !!(l2_fwd[from].reach_port & BIT(to)); 587f7ccdeaSVladimir Oltean } 597f7ccdeaSVladimir Oltean 608aa9ebccSVladimir Oltean static int sja1105_init_mac_settings(struct sja1105_private *priv) 618aa9ebccSVladimir Oltean { 628aa9ebccSVladimir Oltean struct sja1105_mac_config_entry default_mac = { 638aa9ebccSVladimir Oltean /* Enable all 8 priority queues on egress. 648aa9ebccSVladimir Oltean * Every queue i holds top[i] - base[i] frames. 658aa9ebccSVladimir Oltean * Sum of top[i] - base[i] is 511 (max hardware limit). 668aa9ebccSVladimir Oltean */ 678aa9ebccSVladimir Oltean .top = {0x3F, 0x7F, 0xBF, 0xFF, 0x13F, 0x17F, 0x1BF, 0x1FF}, 688aa9ebccSVladimir Oltean .base = {0x0, 0x40, 0x80, 0xC0, 0x100, 0x140, 0x180, 0x1C0}, 698aa9ebccSVladimir Oltean .enabled = {true, true, true, true, true, true, true, true}, 708aa9ebccSVladimir Oltean /* Keep standard IFG of 12 bytes on egress. */ 718aa9ebccSVladimir Oltean .ifg = 0, 728aa9ebccSVladimir Oltean /* Always put the MAC speed in automatic mode, where it can be 731fd4a173SVladimir Oltean * adjusted at runtime by PHYLINK. 748aa9ebccSVladimir Oltean */ 7541fed17fSVladimir Oltean .speed = priv->info->port_speed[SJA1105_SPEED_AUTO], 768aa9ebccSVladimir Oltean /* No static correction for 1-step 1588 events */ 778aa9ebccSVladimir Oltean .tp_delin = 0, 788aa9ebccSVladimir Oltean .tp_delout = 0, 798aa9ebccSVladimir Oltean /* Disable aging for critical TTEthernet traffic */ 808aa9ebccSVladimir Oltean .maxage = 0xFF, 818aa9ebccSVladimir Oltean /* Internal VLAN (pvid) to apply to untagged ingress */ 828aa9ebccSVladimir Oltean .vlanprio = 0, 83e3502b82SVladimir Oltean .vlanid = 1, 848aa9ebccSVladimir Oltean .ing_mirr = false, 858aa9ebccSVladimir Oltean .egr_mirr = false, 868aa9ebccSVladimir Oltean /* Don't drop traffic with other EtherType than ETH_P_IP */ 878aa9ebccSVladimir Oltean .drpnona664 = false, 888aa9ebccSVladimir Oltean /* Don't drop double-tagged traffic */ 898aa9ebccSVladimir Oltean .drpdtag = false, 908aa9ebccSVladimir Oltean /* Don't drop untagged traffic */ 918aa9ebccSVladimir Oltean .drpuntag = false, 928aa9ebccSVladimir Oltean /* Don't retag 802.1p (VID 0) traffic with the pvid */ 938aa9ebccSVladimir Oltean .retag = false, 94640f763fSVladimir Oltean /* Disable learning and I/O on user ports by default - 95640f763fSVladimir Oltean * STP will enable it. 96640f763fSVladimir Oltean */ 97640f763fSVladimir Oltean .dyn_learn = false, 988aa9ebccSVladimir Oltean .egress = false, 998aa9ebccSVladimir Oltean .ingress = false, 1008aa9ebccSVladimir Oltean }; 1018aa9ebccSVladimir Oltean struct sja1105_mac_config_entry *mac; 102542043e9SVladimir Oltean struct dsa_switch *ds = priv->ds; 1038aa9ebccSVladimir Oltean struct sja1105_table *table; 1048aa9ebccSVladimir Oltean int i; 1058aa9ebccSVladimir Oltean 1068aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_MAC_CONFIG]; 1078aa9ebccSVladimir Oltean 1088aa9ebccSVladimir Oltean /* Discard previous MAC Configuration Table */ 1098aa9ebccSVladimir Oltean if (table->entry_count) { 1108aa9ebccSVladimir Oltean kfree(table->entries); 1118aa9ebccSVladimir Oltean table->entry_count = 0; 1128aa9ebccSVladimir Oltean } 1138aa9ebccSVladimir Oltean 114fd6f2c25SVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 1158aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 1168aa9ebccSVladimir Oltean if (!table->entries) 1178aa9ebccSVladimir Oltean return -ENOMEM; 1188aa9ebccSVladimir Oltean 119fd6f2c25SVladimir Oltean table->entry_count = table->ops->max_entry_count; 1208aa9ebccSVladimir Oltean 1218aa9ebccSVladimir Oltean mac = table->entries; 1228aa9ebccSVladimir Oltean 123542043e9SVladimir Oltean for (i = 0; i < ds->num_ports; i++) { 1248aa9ebccSVladimir Oltean mac[i] = default_mac; 125b0b33b04SVladimir Oltean 126b0b33b04SVladimir Oltean /* Let sja1105_bridge_stp_state_set() keep address learning 127b0b33b04SVladimir Oltean * enabled for the CPU port. 128640f763fSVladimir Oltean */ 129b0b33b04SVladimir Oltean if (dsa_is_cpu_port(ds, i)) 130b0b33b04SVladimir Oltean priv->learn_ena |= BIT(i); 131640f763fSVladimir Oltean } 1328aa9ebccSVladimir Oltean 1338aa9ebccSVladimir Oltean return 0; 1348aa9ebccSVladimir Oltean } 1358aa9ebccSVladimir Oltean 1365d645df9SVladimir Oltean static int sja1105_init_mii_settings(struct sja1105_private *priv) 1378aa9ebccSVladimir Oltean { 1388aa9ebccSVladimir Oltean struct device *dev = &priv->spidev->dev; 1398aa9ebccSVladimir Oltean struct sja1105_xmii_params_entry *mii; 140542043e9SVladimir Oltean struct dsa_switch *ds = priv->ds; 1418aa9ebccSVladimir Oltean struct sja1105_table *table; 1428aa9ebccSVladimir Oltean int i; 1438aa9ebccSVladimir Oltean 1448aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_XMII_PARAMS]; 1458aa9ebccSVladimir Oltean 1468aa9ebccSVladimir Oltean /* Discard previous xMII Mode Parameters Table */ 1478aa9ebccSVladimir Oltean if (table->entry_count) { 1488aa9ebccSVladimir Oltean kfree(table->entries); 1498aa9ebccSVladimir Oltean table->entry_count = 0; 1508aa9ebccSVladimir Oltean } 1518aa9ebccSVladimir Oltean 152fd6f2c25SVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 1538aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 1548aa9ebccSVladimir Oltean if (!table->entries) 1558aa9ebccSVladimir Oltean return -ENOMEM; 1568aa9ebccSVladimir Oltean 1571fd4a173SVladimir Oltean /* Override table based on PHYLINK DT bindings */ 158fd6f2c25SVladimir Oltean table->entry_count = table->ops->max_entry_count; 1598aa9ebccSVladimir Oltean 1608aa9ebccSVladimir Oltean mii = table->entries; 1618aa9ebccSVladimir Oltean 162542043e9SVladimir Oltean for (i = 0; i < ds->num_ports; i++) { 1635d645df9SVladimir Oltean sja1105_mii_role_t role = XMII_MAC; 1645d645df9SVladimir Oltean 165ee9d0cb6SVladimir Oltean if (dsa_is_unused_port(priv->ds, i)) 166ee9d0cb6SVladimir Oltean continue; 167ee9d0cb6SVladimir Oltean 1685d645df9SVladimir Oltean switch (priv->phy_mode[i]) { 1695a8f0974SVladimir Oltean case PHY_INTERFACE_MODE_INTERNAL: 1705a8f0974SVladimir Oltean if (priv->info->internal_phy[i] == SJA1105_NO_PHY) 1715a8f0974SVladimir Oltean goto unsupported; 1725a8f0974SVladimir Oltean 1735a8f0974SVladimir Oltean mii->xmii_mode[i] = XMII_MODE_MII; 1745a8f0974SVladimir Oltean if (priv->info->internal_phy[i] == SJA1105_PHY_BASE_TX) 1755a8f0974SVladimir Oltean mii->special[i] = true; 1765a8f0974SVladimir Oltean 1775a8f0974SVladimir Oltean break; 1785d645df9SVladimir Oltean case PHY_INTERFACE_MODE_REVMII: 1795d645df9SVladimir Oltean role = XMII_PHY; 1805d645df9SVladimir Oltean fallthrough; 1818aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_MII: 18291a05078SVladimir Oltean if (!priv->info->supports_mii[i]) 18391a05078SVladimir Oltean goto unsupported; 18491a05078SVladimir Oltean 1858aa9ebccSVladimir Oltean mii->xmii_mode[i] = XMII_MODE_MII; 1868aa9ebccSVladimir Oltean break; 1875d645df9SVladimir Oltean case PHY_INTERFACE_MODE_REVRMII: 1885d645df9SVladimir Oltean role = XMII_PHY; 1895d645df9SVladimir Oltean fallthrough; 1908aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RMII: 19191a05078SVladimir Oltean if (!priv->info->supports_rmii[i]) 19291a05078SVladimir Oltean goto unsupported; 19391a05078SVladimir Oltean 1948aa9ebccSVladimir Oltean mii->xmii_mode[i] = XMII_MODE_RMII; 1958aa9ebccSVladimir Oltean break; 1968aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RGMII: 1978aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RGMII_ID: 1988aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RGMII_RXID: 1998aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RGMII_TXID: 20091a05078SVladimir Oltean if (!priv->info->supports_rgmii[i]) 20191a05078SVladimir Oltean goto unsupported; 20291a05078SVladimir Oltean 2038aa9ebccSVladimir Oltean mii->xmii_mode[i] = XMII_MODE_RGMII; 2048aa9ebccSVladimir Oltean break; 205ffe10e67SVladimir Oltean case PHY_INTERFACE_MODE_SGMII: 20691a05078SVladimir Oltean if (!priv->info->supports_sgmii[i]) 20791a05078SVladimir Oltean goto unsupported; 20891a05078SVladimir Oltean 209ffe10e67SVladimir Oltean mii->xmii_mode[i] = XMII_MODE_SGMII; 210ece578bcSVladimir Oltean mii->special[i] = true; 211ffe10e67SVladimir Oltean break; 21291a05078SVladimir Oltean case PHY_INTERFACE_MODE_2500BASEX: 21391a05078SVladimir Oltean if (!priv->info->supports_2500basex[i]) 21491a05078SVladimir Oltean goto unsupported; 21591a05078SVladimir Oltean 21691a05078SVladimir Oltean mii->xmii_mode[i] = XMII_MODE_SGMII; 217ece578bcSVladimir Oltean mii->special[i] = true; 21891a05078SVladimir Oltean break; 21991a05078SVladimir Oltean unsupported: 2208aa9ebccSVladimir Oltean default: 22191a05078SVladimir Oltean dev_err(dev, "Unsupported PHY mode %s on port %d!\n", 2225d645df9SVladimir Oltean phy_modes(priv->phy_mode[i]), i); 2236729188dSVladimir Oltean return -EINVAL; 2248aa9ebccSVladimir Oltean } 2258aa9ebccSVladimir Oltean 2265d645df9SVladimir Oltean mii->phy_mac[i] = role; 2278aa9ebccSVladimir Oltean } 2288aa9ebccSVladimir Oltean return 0; 2298aa9ebccSVladimir Oltean } 2308aa9ebccSVladimir Oltean 2318aa9ebccSVladimir Oltean static int sja1105_init_static_fdb(struct sja1105_private *priv) 2328aa9ebccSVladimir Oltean { 2334d942354SVladimir Oltean struct sja1105_l2_lookup_entry *l2_lookup; 2348aa9ebccSVladimir Oltean struct sja1105_table *table; 2354d942354SVladimir Oltean int port; 2368aa9ebccSVladimir Oltean 2378aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; 2388aa9ebccSVladimir Oltean 2394d942354SVladimir Oltean /* We only populate the FDB table through dynamic L2 Address Lookup 2404d942354SVladimir Oltean * entries, except for a special entry at the end which is a catch-all 2414d942354SVladimir Oltean * for unknown multicast and will be used to control flooding domain. 242291d1e72SVladimir Oltean */ 2438aa9ebccSVladimir Oltean if (table->entry_count) { 2448aa9ebccSVladimir Oltean kfree(table->entries); 2458aa9ebccSVladimir Oltean table->entry_count = 0; 2468aa9ebccSVladimir Oltean } 2474d942354SVladimir Oltean 2484d942354SVladimir Oltean if (!priv->info->can_limit_mcast_flood) 2494d942354SVladimir Oltean return 0; 2504d942354SVladimir Oltean 2514d942354SVladimir Oltean table->entries = kcalloc(1, table->ops->unpacked_entry_size, 2524d942354SVladimir Oltean GFP_KERNEL); 2534d942354SVladimir Oltean if (!table->entries) 2544d942354SVladimir Oltean return -ENOMEM; 2554d942354SVladimir Oltean 2564d942354SVladimir Oltean table->entry_count = 1; 2574d942354SVladimir Oltean l2_lookup = table->entries; 2584d942354SVladimir Oltean 2594d942354SVladimir Oltean /* All L2 multicast addresses have an odd first octet */ 2604d942354SVladimir Oltean l2_lookup[0].macaddr = SJA1105_UNKNOWN_MULTICAST; 2614d942354SVladimir Oltean l2_lookup[0].mask_macaddr = SJA1105_UNKNOWN_MULTICAST; 2624d942354SVladimir Oltean l2_lookup[0].lockeds = true; 2634d942354SVladimir Oltean l2_lookup[0].index = SJA1105_MAX_L2_LOOKUP_COUNT - 1; 2644d942354SVladimir Oltean 2654d942354SVladimir Oltean /* Flood multicast to every port by default */ 2664d942354SVladimir Oltean for (port = 0; port < priv->ds->num_ports; port++) 2674d942354SVladimir Oltean if (!dsa_is_unused_port(priv->ds, port)) 2684d942354SVladimir Oltean l2_lookup[0].destports |= BIT(port); 2694d942354SVladimir Oltean 2708aa9ebccSVladimir Oltean return 0; 2718aa9ebccSVladimir Oltean } 2728aa9ebccSVladimir Oltean 2738aa9ebccSVladimir Oltean static int sja1105_init_l2_lookup_params(struct sja1105_private *priv) 2748aa9ebccSVladimir Oltean { 2758aa9ebccSVladimir Oltean struct sja1105_l2_lookup_params_entry default_l2_lookup_params = { 2768456721dSVladimir Oltean /* Learned FDB entries are forgotten after 300 seconds */ 2778456721dSVladimir Oltean .maxage = SJA1105_AGEING_TIME_MS(300000), 2788aa9ebccSVladimir Oltean /* All entries within a FDB bin are available for learning */ 2798aa9ebccSVladimir Oltean .dyn_tbsz = SJA1105ET_FDB_BIN_SIZE, 2801da73821SVladimir Oltean /* And the P/Q/R/S equivalent setting: */ 2811da73821SVladimir Oltean .start_dynspc = 0, 2828aa9ebccSVladimir Oltean /* 2^8 + 2^5 + 2^3 + 2^2 + 2^1 + 1 in Koopman notation */ 2838aa9ebccSVladimir Oltean .poly = 0x97, 2848aa9ebccSVladimir Oltean /* This selects between Independent VLAN Learning (IVL) and 2858aa9ebccSVladimir Oltean * Shared VLAN Learning (SVL) 2868aa9ebccSVladimir Oltean */ 2876d7c7d94SVladimir Oltean .shared_learn = true, 2888aa9ebccSVladimir Oltean /* Don't discard management traffic based on ENFPORT - 2898aa9ebccSVladimir Oltean * we don't perform SMAC port enforcement anyway, so 2908aa9ebccSVladimir Oltean * what we are setting here doesn't matter. 2918aa9ebccSVladimir Oltean */ 2928aa9ebccSVladimir Oltean .no_enf_hostprt = false, 2938aa9ebccSVladimir Oltean /* Don't learn SMAC for mac_fltres1 and mac_fltres0. 2948aa9ebccSVladimir Oltean * Maybe correlate with no_linklocal_learn from bridge driver? 2958aa9ebccSVladimir Oltean */ 2968aa9ebccSVladimir Oltean .no_mgmt_learn = true, 2971da73821SVladimir Oltean /* P/Q/R/S only */ 2981da73821SVladimir Oltean .use_static = true, 2991da73821SVladimir Oltean /* Dynamically learned FDB entries can overwrite other (older) 3001da73821SVladimir Oltean * dynamic FDB entries 3011da73821SVladimir Oltean */ 3021da73821SVladimir Oltean .owr_dyn = true, 3031da73821SVladimir Oltean .drpnolearn = true, 3048aa9ebccSVladimir Oltean }; 305542043e9SVladimir Oltean struct dsa_switch *ds = priv->ds; 306f238fef1SVladimir Oltean int port, num_used_ports = 0; 307542043e9SVladimir Oltean struct sja1105_table *table; 308542043e9SVladimir Oltean u64 max_fdb_entries; 309542043e9SVladimir Oltean 310542043e9SVladimir Oltean for (port = 0; port < ds->num_ports; port++) 311f238fef1SVladimir Oltean if (!dsa_is_unused_port(ds, port)) 312f238fef1SVladimir Oltean num_used_ports++; 313f238fef1SVladimir Oltean 314f238fef1SVladimir Oltean max_fdb_entries = SJA1105_MAX_L2_LOOKUP_COUNT / num_used_ports; 315f238fef1SVladimir Oltean 316f238fef1SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 317f238fef1SVladimir Oltean if (dsa_is_unused_port(ds, port)) 318f238fef1SVladimir Oltean continue; 319f238fef1SVladimir Oltean 320542043e9SVladimir Oltean default_l2_lookup_params.maxaddrp[port] = max_fdb_entries; 321f238fef1SVladimir Oltean } 3228aa9ebccSVladimir Oltean 3238aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS]; 3248aa9ebccSVladimir Oltean 3258aa9ebccSVladimir Oltean if (table->entry_count) { 3268aa9ebccSVladimir Oltean kfree(table->entries); 3278aa9ebccSVladimir Oltean table->entry_count = 0; 3288aa9ebccSVladimir Oltean } 3298aa9ebccSVladimir Oltean 330fd6f2c25SVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 3318aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 3328aa9ebccSVladimir Oltean if (!table->entries) 3338aa9ebccSVladimir Oltean return -ENOMEM; 3348aa9ebccSVladimir Oltean 335fd6f2c25SVladimir Oltean table->entry_count = table->ops->max_entry_count; 3368aa9ebccSVladimir Oltean 3378aa9ebccSVladimir Oltean /* This table only has a single entry */ 3388aa9ebccSVladimir Oltean ((struct sja1105_l2_lookup_params_entry *)table->entries)[0] = 3398aa9ebccSVladimir Oltean default_l2_lookup_params; 3408aa9ebccSVladimir Oltean 3418aa9ebccSVladimir Oltean return 0; 3428aa9ebccSVladimir Oltean } 3438aa9ebccSVladimir Oltean 344ed040abcSVladimir Oltean /* Set up a default VLAN for untagged traffic injected from the CPU 345ed040abcSVladimir Oltean * using management routes (e.g. STP, PTP) as opposed to tag_8021q. 346ed040abcSVladimir Oltean * All DT-defined ports are members of this VLAN, and there are no 347ed040abcSVladimir Oltean * restrictions on forwarding (since the CPU selects the destination). 348ed040abcSVladimir Oltean * Frames from this VLAN will always be transmitted as untagged, and 349ed040abcSVladimir Oltean * neither the bridge nor the 8021q module cannot create this VLAN ID. 350ed040abcSVladimir Oltean */ 3518aa9ebccSVladimir Oltean static int sja1105_init_static_vlan(struct sja1105_private *priv) 3528aa9ebccSVladimir Oltean { 3538aa9ebccSVladimir Oltean struct sja1105_table *table; 3548aa9ebccSVladimir Oltean struct sja1105_vlan_lookup_entry pvid = { 3553e77e59bSVladimir Oltean .type_entry = SJA1110_VLAN_D_TAG, 3568aa9ebccSVladimir Oltean .ving_mirr = 0, 3578aa9ebccSVladimir Oltean .vegr_mirr = 0, 3588aa9ebccSVladimir Oltean .vmemb_port = 0, 3598aa9ebccSVladimir Oltean .vlan_bc = 0, 3608aa9ebccSVladimir Oltean .tag_port = 0, 361ed040abcSVladimir Oltean .vlanid = SJA1105_DEFAULT_VLAN, 3628aa9ebccSVladimir Oltean }; 363ec5ae610SVladimir Oltean struct dsa_switch *ds = priv->ds; 364ec5ae610SVladimir Oltean int port; 3658aa9ebccSVladimir Oltean 3668aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP]; 3678aa9ebccSVladimir Oltean 3688aa9ebccSVladimir Oltean if (table->entry_count) { 3698aa9ebccSVladimir Oltean kfree(table->entries); 3708aa9ebccSVladimir Oltean table->entry_count = 0; 3718aa9ebccSVladimir Oltean } 3728aa9ebccSVladimir Oltean 373c75857b0SZheng Yongjun table->entries = kzalloc(table->ops->unpacked_entry_size, 3748aa9ebccSVladimir Oltean GFP_KERNEL); 3758aa9ebccSVladimir Oltean if (!table->entries) 3768aa9ebccSVladimir Oltean return -ENOMEM; 3778aa9ebccSVladimir Oltean 3788aa9ebccSVladimir Oltean table->entry_count = 1; 3798aa9ebccSVladimir Oltean 380ec5ae610SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 381ec5ae610SVladimir Oltean struct sja1105_bridge_vlan *v; 382ec5ae610SVladimir Oltean 383ec5ae610SVladimir Oltean if (dsa_is_unused_port(ds, port)) 384ec5ae610SVladimir Oltean continue; 385ec5ae610SVladimir Oltean 386ec5ae610SVladimir Oltean pvid.vmemb_port |= BIT(port); 387ec5ae610SVladimir Oltean pvid.vlan_bc |= BIT(port); 388ec5ae610SVladimir Oltean pvid.tag_port &= ~BIT(port); 389ec5ae610SVladimir Oltean 390ec5ae610SVladimir Oltean v = kzalloc(sizeof(*v), GFP_KERNEL); 391ec5ae610SVladimir Oltean if (!v) 392ec5ae610SVladimir Oltean return -ENOMEM; 393ec5ae610SVladimir Oltean 394ec5ae610SVladimir Oltean v->port = port; 395ed040abcSVladimir Oltean v->vid = SJA1105_DEFAULT_VLAN; 396ec5ae610SVladimir Oltean v->untagged = true; 397ec5ae610SVladimir Oltean if (dsa_is_cpu_port(ds, port)) 398ec5ae610SVladimir Oltean v->pvid = true; 399ec5ae610SVladimir Oltean list_add(&v->list, &priv->dsa_8021q_vlans); 400e40cba94SVladimir Oltean 401e40cba94SVladimir Oltean v = kmemdup(v, sizeof(*v), GFP_KERNEL); 402e40cba94SVladimir Oltean if (!v) 403e40cba94SVladimir Oltean return -ENOMEM; 404e40cba94SVladimir Oltean 405e40cba94SVladimir Oltean list_add(&v->list, &priv->bridge_vlans); 4068aa9ebccSVladimir Oltean } 4078aa9ebccSVladimir Oltean 4088aa9ebccSVladimir Oltean ((struct sja1105_vlan_lookup_entry *)table->entries)[0] = pvid; 4098aa9ebccSVladimir Oltean return 0; 4108aa9ebccSVladimir Oltean } 4118aa9ebccSVladimir Oltean 4128aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding(struct sja1105_private *priv) 4138aa9ebccSVladimir Oltean { 4148aa9ebccSVladimir Oltean struct sja1105_l2_forwarding_entry *l2fwd; 415542043e9SVladimir Oltean struct dsa_switch *ds = priv->ds; 4168aa9ebccSVladimir Oltean struct sja1105_table *table; 4178aa9ebccSVladimir Oltean int i, j; 4188aa9ebccSVladimir Oltean 4198aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING]; 4208aa9ebccSVladimir Oltean 4218aa9ebccSVladimir Oltean if (table->entry_count) { 4228aa9ebccSVladimir Oltean kfree(table->entries); 4238aa9ebccSVladimir Oltean table->entry_count = 0; 4248aa9ebccSVladimir Oltean } 4258aa9ebccSVladimir Oltean 426fd6f2c25SVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 4278aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 4288aa9ebccSVladimir Oltean if (!table->entries) 4298aa9ebccSVladimir Oltean return -ENOMEM; 4308aa9ebccSVladimir Oltean 431fd6f2c25SVladimir Oltean table->entry_count = table->ops->max_entry_count; 4328aa9ebccSVladimir Oltean 4338aa9ebccSVladimir Oltean l2fwd = table->entries; 4348aa9ebccSVladimir Oltean 4358aa9ebccSVladimir Oltean /* First 5 entries define the forwarding rules */ 436542043e9SVladimir Oltean for (i = 0; i < ds->num_ports; i++) { 4378aa9ebccSVladimir Oltean unsigned int upstream = dsa_upstream_port(priv->ds, i); 4388aa9ebccSVladimir Oltean 439f238fef1SVladimir Oltean if (dsa_is_unused_port(ds, i)) 440f238fef1SVladimir Oltean continue; 441f238fef1SVladimir Oltean 4428aa9ebccSVladimir Oltean for (j = 0; j < SJA1105_NUM_TC; j++) 4438aa9ebccSVladimir Oltean l2fwd[i].vlan_pmap[j] = j; 4448aa9ebccSVladimir Oltean 4457f7ccdeaSVladimir Oltean /* All ports start up with egress flooding enabled, 4467f7ccdeaSVladimir Oltean * including the CPU port. 4477f7ccdeaSVladimir Oltean */ 4487f7ccdeaSVladimir Oltean priv->ucast_egress_floods |= BIT(i); 4497f7ccdeaSVladimir Oltean priv->bcast_egress_floods |= BIT(i); 4507f7ccdeaSVladimir Oltean 4518aa9ebccSVladimir Oltean if (i == upstream) 4528aa9ebccSVladimir Oltean continue; 4538aa9ebccSVladimir Oltean 4548aa9ebccSVladimir Oltean sja1105_port_allow_traffic(l2fwd, i, upstream, true); 4558aa9ebccSVladimir Oltean sja1105_port_allow_traffic(l2fwd, upstream, i, true); 4564d942354SVladimir Oltean 4574d942354SVladimir Oltean l2fwd[i].bc_domain = BIT(upstream); 4584d942354SVladimir Oltean l2fwd[i].fl_domain = BIT(upstream); 4594d942354SVladimir Oltean 4604d942354SVladimir Oltean l2fwd[upstream].bc_domain |= BIT(i); 4614d942354SVladimir Oltean l2fwd[upstream].fl_domain |= BIT(i); 4628aa9ebccSVladimir Oltean } 463f238fef1SVladimir Oltean 4648aa9ebccSVladimir Oltean /* Next 8 entries define VLAN PCP mapping from ingress to egress. 4658aa9ebccSVladimir Oltean * Create a one-to-one mapping. 4668aa9ebccSVladimir Oltean */ 467f238fef1SVladimir Oltean for (i = 0; i < SJA1105_NUM_TC; i++) { 468f238fef1SVladimir Oltean for (j = 0; j < ds->num_ports; j++) { 469f238fef1SVladimir Oltean if (dsa_is_unused_port(ds, j)) 470f238fef1SVladimir Oltean continue; 471f238fef1SVladimir Oltean 472542043e9SVladimir Oltean l2fwd[ds->num_ports + i].vlan_pmap[j] = i; 473f238fef1SVladimir Oltean } 4743e77e59bSVladimir Oltean 4753e77e59bSVladimir Oltean l2fwd[ds->num_ports + i].type_egrpcp2outputq = true; 4763e77e59bSVladimir Oltean } 4773e77e59bSVladimir Oltean 4783e77e59bSVladimir Oltean return 0; 4793e77e59bSVladimir Oltean } 4803e77e59bSVladimir Oltean 4813e77e59bSVladimir Oltean static int sja1110_init_pcp_remapping(struct sja1105_private *priv) 4823e77e59bSVladimir Oltean { 4833e77e59bSVladimir Oltean struct sja1110_pcp_remapping_entry *pcp_remap; 4843e77e59bSVladimir Oltean struct dsa_switch *ds = priv->ds; 4853e77e59bSVladimir Oltean struct sja1105_table *table; 4863e77e59bSVladimir Oltean int port, tc; 4873e77e59bSVladimir Oltean 4883e77e59bSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_PCP_REMAPPING]; 4893e77e59bSVladimir Oltean 4903e77e59bSVladimir Oltean /* Nothing to do for SJA1105 */ 4913e77e59bSVladimir Oltean if (!table->ops->max_entry_count) 4923e77e59bSVladimir Oltean return 0; 4933e77e59bSVladimir Oltean 4943e77e59bSVladimir Oltean if (table->entry_count) { 4953e77e59bSVladimir Oltean kfree(table->entries); 4963e77e59bSVladimir Oltean table->entry_count = 0; 4973e77e59bSVladimir Oltean } 4983e77e59bSVladimir Oltean 4993e77e59bSVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 5003e77e59bSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 5013e77e59bSVladimir Oltean if (!table->entries) 5023e77e59bSVladimir Oltean return -ENOMEM; 5033e77e59bSVladimir Oltean 5043e77e59bSVladimir Oltean table->entry_count = table->ops->max_entry_count; 5053e77e59bSVladimir Oltean 5063e77e59bSVladimir Oltean pcp_remap = table->entries; 5073e77e59bSVladimir Oltean 5083e77e59bSVladimir Oltean /* Repeat the configuration done for vlan_pmap */ 5093e77e59bSVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 5103e77e59bSVladimir Oltean if (dsa_is_unused_port(ds, port)) 5113e77e59bSVladimir Oltean continue; 5123e77e59bSVladimir Oltean 5133e77e59bSVladimir Oltean for (tc = 0; tc < SJA1105_NUM_TC; tc++) 5143e77e59bSVladimir Oltean pcp_remap[port].egrpcp[tc] = tc; 515f238fef1SVladimir Oltean } 5168aa9ebccSVladimir Oltean 5178aa9ebccSVladimir Oltean return 0; 5188aa9ebccSVladimir Oltean } 5198aa9ebccSVladimir Oltean 5208aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding_params(struct sja1105_private *priv) 5218aa9ebccSVladimir Oltean { 5221bf658eeSVladimir Oltean struct sja1105_l2_forwarding_params_entry *l2fwd_params; 5238aa9ebccSVladimir Oltean struct sja1105_table *table; 5248aa9ebccSVladimir Oltean 5258aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS]; 5268aa9ebccSVladimir Oltean 5278aa9ebccSVladimir Oltean if (table->entry_count) { 5288aa9ebccSVladimir Oltean kfree(table->entries); 5298aa9ebccSVladimir Oltean table->entry_count = 0; 5308aa9ebccSVladimir Oltean } 5318aa9ebccSVladimir Oltean 532fd6f2c25SVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 5338aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 5348aa9ebccSVladimir Oltean if (!table->entries) 5358aa9ebccSVladimir Oltean return -ENOMEM; 5368aa9ebccSVladimir Oltean 537fd6f2c25SVladimir Oltean table->entry_count = table->ops->max_entry_count; 5388aa9ebccSVladimir Oltean 5398aa9ebccSVladimir Oltean /* This table only has a single entry */ 5401bf658eeSVladimir Oltean l2fwd_params = table->entries; 5411bf658eeSVladimir Oltean 5421bf658eeSVladimir Oltean /* Disallow dynamic reconfiguration of vlan_pmap */ 5431bf658eeSVladimir Oltean l2fwd_params->max_dynp = 0; 5441bf658eeSVladimir Oltean /* Use a single memory partition for all ingress queues */ 5451bf658eeSVladimir Oltean l2fwd_params->part_spc[0] = priv->info->max_frame_mem; 5468aa9ebccSVladimir Oltean 5478aa9ebccSVladimir Oltean return 0; 5488aa9ebccSVladimir Oltean } 5498aa9ebccSVladimir Oltean 550aaa270c6SVladimir Oltean void sja1105_frame_memory_partitioning(struct sja1105_private *priv) 551aaa270c6SVladimir Oltean { 552aaa270c6SVladimir Oltean struct sja1105_l2_forwarding_params_entry *l2_fwd_params; 553aaa270c6SVladimir Oltean struct sja1105_vl_forwarding_params_entry *vl_fwd_params; 5541bf658eeSVladimir Oltean int max_mem = priv->info->max_frame_mem; 555aaa270c6SVladimir Oltean struct sja1105_table *table; 556aaa270c6SVladimir Oltean 557aaa270c6SVladimir Oltean /* VLAN retagging is implemented using a loopback port that consumes 558aaa270c6SVladimir Oltean * frame buffers. That leaves less for us. 559aaa270c6SVladimir Oltean */ 560aaa270c6SVladimir Oltean if (priv->vlan_state == SJA1105_VLAN_BEST_EFFORT) 5611bf658eeSVladimir Oltean max_mem -= SJA1105_FRAME_MEMORY_RETAGGING_OVERHEAD; 562aaa270c6SVladimir Oltean 563aaa270c6SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS]; 564aaa270c6SVladimir Oltean l2_fwd_params = table->entries; 565aaa270c6SVladimir Oltean l2_fwd_params->part_spc[0] = max_mem; 566aaa270c6SVladimir Oltean 567aaa270c6SVladimir Oltean /* If we have any critical-traffic virtual links, we need to reserve 568aaa270c6SVladimir Oltean * some frame buffer memory for them. At the moment, hardcode the value 569aaa270c6SVladimir Oltean * at 100 blocks of 128 bytes of memory each. This leaves 829 blocks 570aaa270c6SVladimir Oltean * remaining for best-effort traffic. TODO: figure out a more flexible 571aaa270c6SVladimir Oltean * way to perform the frame buffer partitioning. 572aaa270c6SVladimir Oltean */ 573aaa270c6SVladimir Oltean if (!priv->static_config.tables[BLK_IDX_VL_FORWARDING].entry_count) 574aaa270c6SVladimir Oltean return; 575aaa270c6SVladimir Oltean 576aaa270c6SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_VL_FORWARDING_PARAMS]; 577aaa270c6SVladimir Oltean vl_fwd_params = table->entries; 578aaa270c6SVladimir Oltean 579aaa270c6SVladimir Oltean l2_fwd_params->part_spc[0] -= SJA1105_VL_FRAME_MEMORY; 580aaa270c6SVladimir Oltean vl_fwd_params->partspc[0] = SJA1105_VL_FRAME_MEMORY; 581aaa270c6SVladimir Oltean } 582aaa270c6SVladimir Oltean 583ceec8bc0SVladimir Oltean /* SJA1110 TDMACONFIGIDX values: 584ceec8bc0SVladimir Oltean * 585ceec8bc0SVladimir Oltean * | 100 Mbps ports | 1Gbps ports | 2.5Gbps ports | Disabled ports 586ceec8bc0SVladimir Oltean * -----+----------------+---------------+---------------+--------------- 587ceec8bc0SVladimir Oltean * 0 | 0, [5:10] | [1:2] | [3:4] | retag 588ceec8bc0SVladimir Oltean * 1 |0, [5:10], retag| [1:2] | [3:4] | - 589ceec8bc0SVladimir Oltean * 2 | 0, [5:10] | [1:3], retag | 4 | - 590ceec8bc0SVladimir Oltean * 3 | 0, [5:10] |[1:2], 4, retag| 3 | - 591ceec8bc0SVladimir Oltean * 4 | 0, 2, [5:10] | 1, retag | [3:4] | - 592ceec8bc0SVladimir Oltean * 5 | 0, 1, [5:10] | 2, retag | [3:4] | - 593ceec8bc0SVladimir Oltean * 14 | 0, [5:10] | [1:4], retag | - | - 594ceec8bc0SVladimir Oltean * 15 | [5:10] | [0:4], retag | - | - 595ceec8bc0SVladimir Oltean */ 596ceec8bc0SVladimir Oltean static void sja1110_select_tdmaconfigidx(struct sja1105_private *priv) 597ceec8bc0SVladimir Oltean { 598ceec8bc0SVladimir Oltean struct sja1105_general_params_entry *general_params; 599ceec8bc0SVladimir Oltean struct sja1105_table *table; 600ceec8bc0SVladimir Oltean bool port_1_is_base_tx; 601ceec8bc0SVladimir Oltean bool port_3_is_2500; 602ceec8bc0SVladimir Oltean bool port_4_is_2500; 603ceec8bc0SVladimir Oltean u64 tdmaconfigidx; 604ceec8bc0SVladimir Oltean 605ceec8bc0SVladimir Oltean if (priv->info->device_id != SJA1110_DEVICE_ID) 606ceec8bc0SVladimir Oltean return; 607ceec8bc0SVladimir Oltean 608ceec8bc0SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; 609ceec8bc0SVladimir Oltean general_params = table->entries; 610ceec8bc0SVladimir Oltean 611ceec8bc0SVladimir Oltean /* All the settings below are "as opposed to SGMII", which is the 612ceec8bc0SVladimir Oltean * other pinmuxing option. 613ceec8bc0SVladimir Oltean */ 614ceec8bc0SVladimir Oltean port_1_is_base_tx = priv->phy_mode[1] == PHY_INTERFACE_MODE_INTERNAL; 615ceec8bc0SVladimir Oltean port_3_is_2500 = priv->phy_mode[3] == PHY_INTERFACE_MODE_2500BASEX; 616ceec8bc0SVladimir Oltean port_4_is_2500 = priv->phy_mode[4] == PHY_INTERFACE_MODE_2500BASEX; 617ceec8bc0SVladimir Oltean 618ceec8bc0SVladimir Oltean if (port_1_is_base_tx) 619ceec8bc0SVladimir Oltean /* Retagging port will operate at 1 Gbps */ 620ceec8bc0SVladimir Oltean tdmaconfigidx = 5; 621ceec8bc0SVladimir Oltean else if (port_3_is_2500 && port_4_is_2500) 622ceec8bc0SVladimir Oltean /* Retagging port will operate at 100 Mbps */ 623ceec8bc0SVladimir Oltean tdmaconfigidx = 1; 624ceec8bc0SVladimir Oltean else if (port_3_is_2500) 625ceec8bc0SVladimir Oltean /* Retagging port will operate at 1 Gbps */ 626ceec8bc0SVladimir Oltean tdmaconfigidx = 3; 627ceec8bc0SVladimir Oltean else if (port_4_is_2500) 628ceec8bc0SVladimir Oltean /* Retagging port will operate at 1 Gbps */ 629ceec8bc0SVladimir Oltean tdmaconfigidx = 2; 630ceec8bc0SVladimir Oltean else 631ceec8bc0SVladimir Oltean /* Retagging port will operate at 1 Gbps */ 632ceec8bc0SVladimir Oltean tdmaconfigidx = 14; 633ceec8bc0SVladimir Oltean 634ceec8bc0SVladimir Oltean general_params->tdmaconfigidx = tdmaconfigidx; 635ceec8bc0SVladimir Oltean } 636ceec8bc0SVladimir Oltean 6378aa9ebccSVladimir Oltean static int sja1105_init_general_params(struct sja1105_private *priv) 6388aa9ebccSVladimir Oltean { 6398aa9ebccSVladimir Oltean struct sja1105_general_params_entry default_general_params = { 640511e6ca0SVladimir Oltean /* Allow dynamic changing of the mirror port */ 641511e6ca0SVladimir Oltean .mirr_ptacu = true, 6428aa9ebccSVladimir Oltean .switchid = priv->ds->index, 6435f06c63bSVladimir Oltean /* Priority queue for link-local management frames 6445f06c63bSVladimir Oltean * (both ingress to and egress from CPU - PTP, STP etc) 6455f06c63bSVladimir Oltean */ 64608fde09aSVladimir Oltean .hostprio = 7, 6478aa9ebccSVladimir Oltean .mac_fltres1 = SJA1105_LINKLOCAL_FILTER_A, 6488aa9ebccSVladimir Oltean .mac_flt1 = SJA1105_LINKLOCAL_FILTER_A_MASK, 64942824463SVladimir Oltean .incl_srcpt1 = false, 6508aa9ebccSVladimir Oltean .send_meta1 = false, 6518aa9ebccSVladimir Oltean .mac_fltres0 = SJA1105_LINKLOCAL_FILTER_B, 6528aa9ebccSVladimir Oltean .mac_flt0 = SJA1105_LINKLOCAL_FILTER_B_MASK, 65342824463SVladimir Oltean .incl_srcpt0 = false, 6548aa9ebccSVladimir Oltean .send_meta0 = false, 6558aa9ebccSVladimir Oltean /* The destination for traffic matching mac_fltres1 and 6568aa9ebccSVladimir Oltean * mac_fltres0 on all ports except host_port. Such traffic 6578aa9ebccSVladimir Oltean * receieved on host_port itself would be dropped, except 6588aa9ebccSVladimir Oltean * by installing a temporary 'management route' 6598aa9ebccSVladimir Oltean */ 660df2a81a3SVladimir Oltean .host_port = priv->ds->num_ports, 661511e6ca0SVladimir Oltean /* Default to an invalid value */ 662542043e9SVladimir Oltean .mirr_port = priv->ds->num_ports, 6638aa9ebccSVladimir Oltean /* No TTEthernet */ 664dfacc5a2SVladimir Oltean .vllupformat = SJA1105_VL_FORMAT_PSFP, 6658aa9ebccSVladimir Oltean .vlmarker = 0, 6668aa9ebccSVladimir Oltean .vlmask = 0, 6678aa9ebccSVladimir Oltean /* Only update correctionField for 1-step PTP (L2 transport) */ 6688aa9ebccSVladimir Oltean .ignore2stf = 0, 6696666cebcSVladimir Oltean /* Forcefully disable VLAN filtering by telling 6706666cebcSVladimir Oltean * the switch that VLAN has a different EtherType. 6716666cebcSVladimir Oltean */ 6726666cebcSVladimir Oltean .tpid = ETH_P_SJA1105, 6736666cebcSVladimir Oltean .tpid2 = ETH_P_SJA1105, 67429305260SVladimir Oltean /* Enable the TTEthernet engine on SJA1110 */ 67529305260SVladimir Oltean .tte_en = true, 6764913b8ebSVladimir Oltean /* Set up the EtherType for control packets on SJA1110 */ 6774913b8ebSVladimir Oltean .header_type = ETH_P_SJA1110, 6788aa9ebccSVladimir Oltean }; 6796c0de59bSVladimir Oltean struct sja1105_general_params_entry *general_params; 680df2a81a3SVladimir Oltean struct dsa_switch *ds = priv->ds; 6818aa9ebccSVladimir Oltean struct sja1105_table *table; 682df2a81a3SVladimir Oltean int port; 683df2a81a3SVladimir Oltean 684df2a81a3SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 685df2a81a3SVladimir Oltean if (dsa_is_cpu_port(ds, port)) { 686df2a81a3SVladimir Oltean default_general_params.host_port = port; 687df2a81a3SVladimir Oltean break; 688df2a81a3SVladimir Oltean } 689df2a81a3SVladimir Oltean } 6908aa9ebccSVladimir Oltean 6918aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; 6928aa9ebccSVladimir Oltean 6938aa9ebccSVladimir Oltean if (table->entry_count) { 6948aa9ebccSVladimir Oltean kfree(table->entries); 6958aa9ebccSVladimir Oltean table->entry_count = 0; 6968aa9ebccSVladimir Oltean } 6978aa9ebccSVladimir Oltean 698fd6f2c25SVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 6998aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 7008aa9ebccSVladimir Oltean if (!table->entries) 7018aa9ebccSVladimir Oltean return -ENOMEM; 7028aa9ebccSVladimir Oltean 703fd6f2c25SVladimir Oltean table->entry_count = table->ops->max_entry_count; 7048aa9ebccSVladimir Oltean 7056c0de59bSVladimir Oltean general_params = table->entries; 7066c0de59bSVladimir Oltean 7078aa9ebccSVladimir Oltean /* This table only has a single entry */ 7086c0de59bSVladimir Oltean general_params[0] = default_general_params; 7098aa9ebccSVladimir Oltean 710ceec8bc0SVladimir Oltean sja1110_select_tdmaconfigidx(priv); 711ceec8bc0SVladimir Oltean 7126c0de59bSVladimir Oltean /* Link-local traffic received on casc_port will be forwarded 7136c0de59bSVladimir Oltean * to host_port without embedding the source port and device ID 7146c0de59bSVladimir Oltean * info in the destination MAC address, and no RX timestamps will be 7156c0de59bSVladimir Oltean * taken either (presumably because it is a cascaded port and a 7166c0de59bSVladimir Oltean * downstream SJA switch already did that). 7176c0de59bSVladimir Oltean * To disable the feature, we need to do different things depending on 7186c0de59bSVladimir Oltean * switch generation. On SJA1105 we need to set an invalid port, while 7196c0de59bSVladimir Oltean * on SJA1110 which support multiple cascaded ports, this field is a 7206c0de59bSVladimir Oltean * bitmask so it must be left zero. 7216c0de59bSVladimir Oltean */ 7226c0de59bSVladimir Oltean if (!priv->info->multiple_cascade_ports) 7236c0de59bSVladimir Oltean general_params->casc_port = ds->num_ports; 7246c0de59bSVladimir Oltean 7258aa9ebccSVladimir Oltean return 0; 7268aa9ebccSVladimir Oltean } 7278aa9ebccSVladimir Oltean 72879d5511cSVladimir Oltean static int sja1105_init_avb_params(struct sja1105_private *priv) 72979d5511cSVladimir Oltean { 73079d5511cSVladimir Oltean struct sja1105_avb_params_entry *avb; 73179d5511cSVladimir Oltean struct sja1105_table *table; 73279d5511cSVladimir Oltean 73379d5511cSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_AVB_PARAMS]; 73479d5511cSVladimir Oltean 73579d5511cSVladimir Oltean /* Discard previous AVB Parameters Table */ 73679d5511cSVladimir Oltean if (table->entry_count) { 73779d5511cSVladimir Oltean kfree(table->entries); 73879d5511cSVladimir Oltean table->entry_count = 0; 73979d5511cSVladimir Oltean } 74079d5511cSVladimir Oltean 741fd6f2c25SVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 74279d5511cSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 74379d5511cSVladimir Oltean if (!table->entries) 74479d5511cSVladimir Oltean return -ENOMEM; 74579d5511cSVladimir Oltean 746fd6f2c25SVladimir Oltean table->entry_count = table->ops->max_entry_count; 74779d5511cSVladimir Oltean 74879d5511cSVladimir Oltean avb = table->entries; 74979d5511cSVladimir Oltean 75079d5511cSVladimir Oltean /* Configure the MAC addresses for meta frames */ 75179d5511cSVladimir Oltean avb->destmeta = SJA1105_META_DMAC; 75279d5511cSVladimir Oltean avb->srcmeta = SJA1105_META_SMAC; 753747e5eb3SVladimir Oltean /* On P/Q/R/S, configure the direction of the PTP_CLK pin as input by 754747e5eb3SVladimir Oltean * default. This is because there might be boards with a hardware 755747e5eb3SVladimir Oltean * layout where enabling the pin as output might cause an electrical 756747e5eb3SVladimir Oltean * clash. On E/T the pin is always an output, which the board designers 757747e5eb3SVladimir Oltean * probably already knew, so even if there are going to be electrical 758747e5eb3SVladimir Oltean * issues, there's nothing we can do. 759747e5eb3SVladimir Oltean */ 760747e5eb3SVladimir Oltean avb->cas_master = false; 76179d5511cSVladimir Oltean 76279d5511cSVladimir Oltean return 0; 76379d5511cSVladimir Oltean } 76479d5511cSVladimir Oltean 765a7cc081cSVladimir Oltean /* The L2 policing table is 2-stage. The table is looked up for each frame 766a7cc081cSVladimir Oltean * according to the ingress port, whether it was broadcast or not, and the 767a7cc081cSVladimir Oltean * classified traffic class (given by VLAN PCP). This portion of the lookup is 768a7cc081cSVladimir Oltean * fixed, and gives access to the SHARINDX, an indirection register pointing 769a7cc081cSVladimir Oltean * within the policing table itself, which is used to resolve the policer that 770a7cc081cSVladimir Oltean * will be used for this frame. 771a7cc081cSVladimir Oltean * 772a7cc081cSVladimir Oltean * Stage 1 Stage 2 773a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 774a7cc081cSVladimir Oltean * |Port 0 TC 0 |SHARINDX| | Policer 0: Rate, Burst, MTU | 775a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 776a7cc081cSVladimir Oltean * |Port 0 TC 1 |SHARINDX| | Policer 1: Rate, Burst, MTU | 777a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 778a7cc081cSVladimir Oltean * ... | Policer 2: Rate, Burst, MTU | 779a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 780a7cc081cSVladimir Oltean * |Port 0 TC 7 |SHARINDX| | Policer 3: Rate, Burst, MTU | 781a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 782a7cc081cSVladimir Oltean * |Port 1 TC 0 |SHARINDX| | Policer 4: Rate, Burst, MTU | 783a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 784a7cc081cSVladimir Oltean * ... | Policer 5: Rate, Burst, MTU | 785a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 786a7cc081cSVladimir Oltean * |Port 1 TC 7 |SHARINDX| | Policer 6: Rate, Burst, MTU | 787a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 788a7cc081cSVladimir Oltean * ... | Policer 7: Rate, Burst, MTU | 789a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 790a7cc081cSVladimir Oltean * |Port 4 TC 7 |SHARINDX| ... 791a7cc081cSVladimir Oltean * +------------+--------+ 792a7cc081cSVladimir Oltean * |Port 0 BCAST|SHARINDX| ... 793a7cc081cSVladimir Oltean * +------------+--------+ 794a7cc081cSVladimir Oltean * |Port 1 BCAST|SHARINDX| ... 795a7cc081cSVladimir Oltean * +------------+--------+ 796a7cc081cSVladimir Oltean * ... ... 797a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 798a7cc081cSVladimir Oltean * |Port 4 BCAST|SHARINDX| | Policer 44: Rate, Burst, MTU | 799a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 800a7cc081cSVladimir Oltean * 801a7cc081cSVladimir Oltean * In this driver, we shall use policers 0-4 as statically alocated port 802a7cc081cSVladimir Oltean * (matchall) policers. So we need to make the SHARINDX for all lookups 803a7cc081cSVladimir Oltean * corresponding to this ingress port (8 VLAN PCP lookups and 1 broadcast 804a7cc081cSVladimir Oltean * lookup) equal. 805a7cc081cSVladimir Oltean * The remaining policers (40) shall be dynamically allocated for flower 806a7cc081cSVladimir Oltean * policers, where the key is either vlan_prio or dst_mac ff:ff:ff:ff:ff:ff. 807a7cc081cSVladimir Oltean */ 8088aa9ebccSVladimir Oltean #define SJA1105_RATE_MBPS(speed) (((speed) * 64000) / 1000) 8098aa9ebccSVladimir Oltean 8108aa9ebccSVladimir Oltean static int sja1105_init_l2_policing(struct sja1105_private *priv) 8118aa9ebccSVladimir Oltean { 8128aa9ebccSVladimir Oltean struct sja1105_l2_policing_entry *policing; 813542043e9SVladimir Oltean struct dsa_switch *ds = priv->ds; 8148aa9ebccSVladimir Oltean struct sja1105_table *table; 815a7cc081cSVladimir Oltean int port, tc; 8168aa9ebccSVladimir Oltean 8178aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_POLICING]; 8188aa9ebccSVladimir Oltean 8198aa9ebccSVladimir Oltean /* Discard previous L2 Policing Table */ 8208aa9ebccSVladimir Oltean if (table->entry_count) { 8218aa9ebccSVladimir Oltean kfree(table->entries); 8228aa9ebccSVladimir Oltean table->entry_count = 0; 8238aa9ebccSVladimir Oltean } 8248aa9ebccSVladimir Oltean 825fd6f2c25SVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 8268aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 8278aa9ebccSVladimir Oltean if (!table->entries) 8288aa9ebccSVladimir Oltean return -ENOMEM; 8298aa9ebccSVladimir Oltean 830fd6f2c25SVladimir Oltean table->entry_count = table->ops->max_entry_count; 8318aa9ebccSVladimir Oltean 8328aa9ebccSVladimir Oltean policing = table->entries; 8338aa9ebccSVladimir Oltean 834a7cc081cSVladimir Oltean /* Setup shared indices for the matchall policers */ 835542043e9SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 83638fbe91fSVladimir Oltean int mcast = (ds->num_ports * (SJA1105_NUM_TC + 1)) + port; 837542043e9SVladimir Oltean int bcast = (ds->num_ports * SJA1105_NUM_TC) + port; 838a7cc081cSVladimir Oltean 839a7cc081cSVladimir Oltean for (tc = 0; tc < SJA1105_NUM_TC; tc++) 840a7cc081cSVladimir Oltean policing[port * SJA1105_NUM_TC + tc].sharindx = port; 841a7cc081cSVladimir Oltean 842a7cc081cSVladimir Oltean policing[bcast].sharindx = port; 84338fbe91fSVladimir Oltean /* Only SJA1110 has multicast policers */ 84438fbe91fSVladimir Oltean if (mcast <= table->ops->max_entry_count) 84538fbe91fSVladimir Oltean policing[mcast].sharindx = port; 846a7cc081cSVladimir Oltean } 847a7cc081cSVladimir Oltean 848a7cc081cSVladimir Oltean /* Setup the matchall policer parameters */ 849542043e9SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 850c279c726SVladimir Oltean int mtu = VLAN_ETH_FRAME_LEN + ETH_FCS_LEN; 851c279c726SVladimir Oltean 852a7cc081cSVladimir Oltean if (dsa_is_cpu_port(priv->ds, port)) 853c279c726SVladimir Oltean mtu += VLAN_HLEN; 8548aa9ebccSVladimir Oltean 855a7cc081cSVladimir Oltean policing[port].smax = 65535; /* Burst size in bytes */ 856a7cc081cSVladimir Oltean policing[port].rate = SJA1105_RATE_MBPS(1000); 857a7cc081cSVladimir Oltean policing[port].maxlen = mtu; 858a7cc081cSVladimir Oltean policing[port].partition = 0; 8598aa9ebccSVladimir Oltean } 860a7cc081cSVladimir Oltean 8618aa9ebccSVladimir Oltean return 0; 8628aa9ebccSVladimir Oltean } 8638aa9ebccSVladimir Oltean 8645d645df9SVladimir Oltean static int sja1105_static_config_load(struct sja1105_private *priv) 8658aa9ebccSVladimir Oltean { 8668aa9ebccSVladimir Oltean int rc; 8678aa9ebccSVladimir Oltean 8688aa9ebccSVladimir Oltean sja1105_static_config_free(&priv->static_config); 8698aa9ebccSVladimir Oltean rc = sja1105_static_config_init(&priv->static_config, 8708aa9ebccSVladimir Oltean priv->info->static_ops, 8718aa9ebccSVladimir Oltean priv->info->device_id); 8728aa9ebccSVladimir Oltean if (rc) 8738aa9ebccSVladimir Oltean return rc; 8748aa9ebccSVladimir Oltean 8758aa9ebccSVladimir Oltean /* Build static configuration */ 8768aa9ebccSVladimir Oltean rc = sja1105_init_mac_settings(priv); 8778aa9ebccSVladimir Oltean if (rc < 0) 8788aa9ebccSVladimir Oltean return rc; 8795d645df9SVladimir Oltean rc = sja1105_init_mii_settings(priv); 8808aa9ebccSVladimir Oltean if (rc < 0) 8818aa9ebccSVladimir Oltean return rc; 8828aa9ebccSVladimir Oltean rc = sja1105_init_static_fdb(priv); 8838aa9ebccSVladimir Oltean if (rc < 0) 8848aa9ebccSVladimir Oltean return rc; 8858aa9ebccSVladimir Oltean rc = sja1105_init_static_vlan(priv); 8868aa9ebccSVladimir Oltean if (rc < 0) 8878aa9ebccSVladimir Oltean return rc; 8888aa9ebccSVladimir Oltean rc = sja1105_init_l2_lookup_params(priv); 8898aa9ebccSVladimir Oltean if (rc < 0) 8908aa9ebccSVladimir Oltean return rc; 8918aa9ebccSVladimir Oltean rc = sja1105_init_l2_forwarding(priv); 8928aa9ebccSVladimir Oltean if (rc < 0) 8938aa9ebccSVladimir Oltean return rc; 8948aa9ebccSVladimir Oltean rc = sja1105_init_l2_forwarding_params(priv); 8958aa9ebccSVladimir Oltean if (rc < 0) 8968aa9ebccSVladimir Oltean return rc; 8978aa9ebccSVladimir Oltean rc = sja1105_init_l2_policing(priv); 8988aa9ebccSVladimir Oltean if (rc < 0) 8998aa9ebccSVladimir Oltean return rc; 9008aa9ebccSVladimir Oltean rc = sja1105_init_general_params(priv); 9018aa9ebccSVladimir Oltean if (rc < 0) 9028aa9ebccSVladimir Oltean return rc; 90379d5511cSVladimir Oltean rc = sja1105_init_avb_params(priv); 90479d5511cSVladimir Oltean if (rc < 0) 90579d5511cSVladimir Oltean return rc; 9063e77e59bSVladimir Oltean rc = sja1110_init_pcp_remapping(priv); 9073e77e59bSVladimir Oltean if (rc < 0) 9083e77e59bSVladimir Oltean return rc; 9098aa9ebccSVladimir Oltean 9108aa9ebccSVladimir Oltean /* Send initial configuration to hardware via SPI */ 9118aa9ebccSVladimir Oltean return sja1105_static_config_upload(priv); 9128aa9ebccSVladimir Oltean } 9138aa9ebccSVladimir Oltean 91429afb83aSVladimir Oltean static int sja1105_parse_rgmii_delays(struct sja1105_private *priv) 915f5b8631cSVladimir Oltean { 916542043e9SVladimir Oltean struct dsa_switch *ds = priv->ds; 91729afb83aSVladimir Oltean int port; 918f5b8631cSVladimir Oltean 91929afb83aSVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 92029afb83aSVladimir Oltean if (!priv->fixed_link[port]) 921f5b8631cSVladimir Oltean continue; 922f5b8631cSVladimir Oltean 92329afb83aSVladimir Oltean if (priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_RXID || 92429afb83aSVladimir Oltean priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_ID) 92529afb83aSVladimir Oltean priv->rgmii_rx_delay[port] = true; 926f5b8631cSVladimir Oltean 92729afb83aSVladimir Oltean if (priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_TXID || 92829afb83aSVladimir Oltean priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_ID) 92929afb83aSVladimir Oltean priv->rgmii_tx_delay[port] = true; 930f5b8631cSVladimir Oltean 93129afb83aSVladimir Oltean if ((priv->rgmii_rx_delay[port] || priv->rgmii_tx_delay[port]) && 932f5b8631cSVladimir Oltean !priv->info->setup_rgmii_delay) 933f5b8631cSVladimir Oltean return -EINVAL; 934f5b8631cSVladimir Oltean } 935f5b8631cSVladimir Oltean return 0; 936f5b8631cSVladimir Oltean } 937f5b8631cSVladimir Oltean 9388aa9ebccSVladimir Oltean static int sja1105_parse_ports_node(struct sja1105_private *priv, 9398aa9ebccSVladimir Oltean struct device_node *ports_node) 9408aa9ebccSVladimir Oltean { 9418aa9ebccSVladimir Oltean struct device *dev = &priv->spidev->dev; 9428aa9ebccSVladimir Oltean struct device_node *child; 9438aa9ebccSVladimir Oltean 94427afe0d3SVladimir Oltean for_each_available_child_of_node(ports_node, child) { 9458aa9ebccSVladimir Oltean struct device_node *phy_node; 9460c65b2b9SAndrew Lunn phy_interface_t phy_mode; 9478aa9ebccSVladimir Oltean u32 index; 9480c65b2b9SAndrew Lunn int err; 9498aa9ebccSVladimir Oltean 9508aa9ebccSVladimir Oltean /* Get switch port number from DT */ 9518aa9ebccSVladimir Oltean if (of_property_read_u32(child, "reg", &index) < 0) { 9528aa9ebccSVladimir Oltean dev_err(dev, "Port number not defined in device tree " 9538aa9ebccSVladimir Oltean "(property \"reg\")\n"); 9547ba771e3SNishka Dasgupta of_node_put(child); 9558aa9ebccSVladimir Oltean return -ENODEV; 9568aa9ebccSVladimir Oltean } 9578aa9ebccSVladimir Oltean 9588aa9ebccSVladimir Oltean /* Get PHY mode from DT */ 9590c65b2b9SAndrew Lunn err = of_get_phy_mode(child, &phy_mode); 9600c65b2b9SAndrew Lunn if (err) { 9618aa9ebccSVladimir Oltean dev_err(dev, "Failed to read phy-mode or " 9628aa9ebccSVladimir Oltean "phy-interface-type property for port %d\n", 9638aa9ebccSVladimir Oltean index); 9647ba771e3SNishka Dasgupta of_node_put(child); 9658aa9ebccSVladimir Oltean return -ENODEV; 9668aa9ebccSVladimir Oltean } 9678aa9ebccSVladimir Oltean 9688aa9ebccSVladimir Oltean phy_node = of_parse_phandle(child, "phy-handle", 0); 9698aa9ebccSVladimir Oltean if (!phy_node) { 9708aa9ebccSVladimir Oltean if (!of_phy_is_fixed_link(child)) { 9718aa9ebccSVladimir Oltean dev_err(dev, "phy-handle or fixed-link " 9728aa9ebccSVladimir Oltean "properties missing!\n"); 9737ba771e3SNishka Dasgupta of_node_put(child); 9748aa9ebccSVladimir Oltean return -ENODEV; 9758aa9ebccSVladimir Oltean } 9768aa9ebccSVladimir Oltean /* phy-handle is missing, but fixed-link isn't. 9778aa9ebccSVladimir Oltean * So it's a fixed link. Default to PHY role. 9788aa9ebccSVladimir Oltean */ 97929afb83aSVladimir Oltean priv->fixed_link[index] = true; 9808aa9ebccSVladimir Oltean } else { 9818aa9ebccSVladimir Oltean of_node_put(phy_node); 9828aa9ebccSVladimir Oltean } 9838aa9ebccSVladimir Oltean 984bf4edf4aSVladimir Oltean priv->phy_mode[index] = phy_mode; 9858aa9ebccSVladimir Oltean } 9868aa9ebccSVladimir Oltean 9878aa9ebccSVladimir Oltean return 0; 9888aa9ebccSVladimir Oltean } 9898aa9ebccSVladimir Oltean 9905d645df9SVladimir Oltean static int sja1105_parse_dt(struct sja1105_private *priv) 9918aa9ebccSVladimir Oltean { 9928aa9ebccSVladimir Oltean struct device *dev = &priv->spidev->dev; 9938aa9ebccSVladimir Oltean struct device_node *switch_node = dev->of_node; 9948aa9ebccSVladimir Oltean struct device_node *ports_node; 9958aa9ebccSVladimir Oltean int rc; 9968aa9ebccSVladimir Oltean 9978aa9ebccSVladimir Oltean ports_node = of_get_child_by_name(switch_node, "ports"); 99815074a36SVladimir Oltean if (!ports_node) 99915074a36SVladimir Oltean ports_node = of_get_child_by_name(switch_node, "ethernet-ports"); 10008aa9ebccSVladimir Oltean if (!ports_node) { 10018aa9ebccSVladimir Oltean dev_err(dev, "Incorrect bindings: absent \"ports\" node\n"); 10028aa9ebccSVladimir Oltean return -ENODEV; 10038aa9ebccSVladimir Oltean } 10048aa9ebccSVladimir Oltean 10055d645df9SVladimir Oltean rc = sja1105_parse_ports_node(priv, ports_node); 10068aa9ebccSVladimir Oltean of_node_put(ports_node); 10078aa9ebccSVladimir Oltean 10088aa9ebccSVladimir Oltean return rc; 10098aa9ebccSVladimir Oltean } 10108aa9ebccSVladimir Oltean 1011c44d0535SVladimir Oltean /* Convert link speed from SJA1105 to ethtool encoding */ 101241fed17fSVladimir Oltean static int sja1105_port_speed_to_ethtool(struct sja1105_private *priv, 101341fed17fSVladimir Oltean u64 speed) 101441fed17fSVladimir Oltean { 101541fed17fSVladimir Oltean if (speed == priv->info->port_speed[SJA1105_SPEED_10MBPS]) 101641fed17fSVladimir Oltean return SPEED_10; 101741fed17fSVladimir Oltean if (speed == priv->info->port_speed[SJA1105_SPEED_100MBPS]) 101841fed17fSVladimir Oltean return SPEED_100; 101941fed17fSVladimir Oltean if (speed == priv->info->port_speed[SJA1105_SPEED_1000MBPS]) 102041fed17fSVladimir Oltean return SPEED_1000; 102141fed17fSVladimir Oltean if (speed == priv->info->port_speed[SJA1105_SPEED_2500MBPS]) 102241fed17fSVladimir Oltean return SPEED_2500; 102341fed17fSVladimir Oltean return SPEED_UNKNOWN; 102441fed17fSVladimir Oltean } 10258aa9ebccSVladimir Oltean 10268400cff6SVladimir Oltean /* Set link speed in the MAC configuration for a specific port. */ 10278aa9ebccSVladimir Oltean static int sja1105_adjust_port_config(struct sja1105_private *priv, int port, 10288400cff6SVladimir Oltean int speed_mbps) 10298aa9ebccSVladimir Oltean { 10308aa9ebccSVladimir Oltean struct sja1105_mac_config_entry *mac; 10318aa9ebccSVladimir Oltean struct device *dev = priv->ds->dev; 103241fed17fSVladimir Oltean u64 speed; 10338aa9ebccSVladimir Oltean int rc; 10348aa9ebccSVladimir Oltean 10358400cff6SVladimir Oltean /* On P/Q/R/S, one can read from the device via the MAC reconfiguration 10368400cff6SVladimir Oltean * tables. On E/T, MAC reconfig tables are not readable, only writable. 10378400cff6SVladimir Oltean * We have to *know* what the MAC looks like. For the sake of keeping 10388400cff6SVladimir Oltean * the code common, we'll use the static configuration tables as a 10398400cff6SVladimir Oltean * reasonable approximation for both E/T and P/Q/R/S. 10408400cff6SVladimir Oltean */ 10418aa9ebccSVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 10428aa9ebccSVladimir Oltean 1043f4cfcfbdSVladimir Oltean switch (speed_mbps) { 1044c44d0535SVladimir Oltean case SPEED_UNKNOWN: 1045a979a0abSVladimir Oltean /* PHYLINK called sja1105_mac_config() to inform us about 1046a979a0abSVladimir Oltean * the state->interface, but AN has not completed and the 1047a979a0abSVladimir Oltean * speed is not yet valid. UM10944.pdf says that setting 1048a979a0abSVladimir Oltean * SJA1105_SPEED_AUTO at runtime disables the port, so that is 1049a979a0abSVladimir Oltean * ok for power consumption in case AN will never complete - 1050a979a0abSVladimir Oltean * otherwise PHYLINK should come back with a new update. 1051a979a0abSVladimir Oltean */ 105241fed17fSVladimir Oltean speed = priv->info->port_speed[SJA1105_SPEED_AUTO]; 1053f4cfcfbdSVladimir Oltean break; 1054c44d0535SVladimir Oltean case SPEED_10: 105541fed17fSVladimir Oltean speed = priv->info->port_speed[SJA1105_SPEED_10MBPS]; 1056f4cfcfbdSVladimir Oltean break; 1057c44d0535SVladimir Oltean case SPEED_100: 105841fed17fSVladimir Oltean speed = priv->info->port_speed[SJA1105_SPEED_100MBPS]; 1059f4cfcfbdSVladimir Oltean break; 1060c44d0535SVladimir Oltean case SPEED_1000: 106141fed17fSVladimir Oltean speed = priv->info->port_speed[SJA1105_SPEED_1000MBPS]; 1062f4cfcfbdSVladimir Oltean break; 106356b63466SVladimir Oltean case SPEED_2500: 106456b63466SVladimir Oltean speed = priv->info->port_speed[SJA1105_SPEED_2500MBPS]; 106556b63466SVladimir Oltean break; 1066f4cfcfbdSVladimir Oltean default: 10678aa9ebccSVladimir Oltean dev_err(dev, "Invalid speed %iMbps\n", speed_mbps); 10688aa9ebccSVladimir Oltean return -EINVAL; 10698aa9ebccSVladimir Oltean } 10708aa9ebccSVladimir Oltean 10718400cff6SVladimir Oltean /* Overwrite SJA1105_SPEED_AUTO from the static MAC configuration 10728400cff6SVladimir Oltean * table, since this will be used for the clocking setup, and we no 10738400cff6SVladimir Oltean * longer need to store it in the static config (already told hardware 10748400cff6SVladimir Oltean * we want auto during upload phase). 1075ffe10e67SVladimir Oltean * Actually for the SGMII port, the MAC is fixed at 1 Gbps and 1076ffe10e67SVladimir Oltean * we need to configure the PCS only (if even that). 10778aa9ebccSVladimir Oltean */ 107891a05078SVladimir Oltean if (priv->phy_mode[port] == PHY_INTERFACE_MODE_SGMII) 107941fed17fSVladimir Oltean mac[port].speed = priv->info->port_speed[SJA1105_SPEED_1000MBPS]; 108056b63466SVladimir Oltean else if (priv->phy_mode[port] == PHY_INTERFACE_MODE_2500BASEX) 108156b63466SVladimir Oltean mac[port].speed = priv->info->port_speed[SJA1105_SPEED_2500MBPS]; 1082ffe10e67SVladimir Oltean else 10838aa9ebccSVladimir Oltean mac[port].speed = speed; 10848aa9ebccSVladimir Oltean 10858aa9ebccSVladimir Oltean /* Write to the dynamic reconfiguration tables */ 10868400cff6SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, 10878400cff6SVladimir Oltean &mac[port], true); 10888aa9ebccSVladimir Oltean if (rc < 0) { 10898aa9ebccSVladimir Oltean dev_err(dev, "Failed to write MAC config: %d\n", rc); 10908aa9ebccSVladimir Oltean return rc; 10918aa9ebccSVladimir Oltean } 10928aa9ebccSVladimir Oltean 10938aa9ebccSVladimir Oltean /* Reconfigure the PLLs for the RGMII interfaces (required 125 MHz at 10948aa9ebccSVladimir Oltean * gigabit, 25 MHz at 100 Mbps and 2.5 MHz at 10 Mbps). For MII and 10958aa9ebccSVladimir Oltean * RMII no change of the clock setup is required. Actually, changing 10968aa9ebccSVladimir Oltean * the clock setup does interrupt the clock signal for a certain time 10978aa9ebccSVladimir Oltean * which causes trouble for all PHYs relying on this signal. 10988aa9ebccSVladimir Oltean */ 109991a05078SVladimir Oltean if (!phy_interface_mode_is_rgmii(priv->phy_mode[port])) 11008aa9ebccSVladimir Oltean return 0; 11018aa9ebccSVladimir Oltean 11028aa9ebccSVladimir Oltean return sja1105_clocking_setup_port(priv, port); 11038aa9ebccSVladimir Oltean } 11048aa9ebccSVladimir Oltean 110539710229SVladimir Oltean /* The SJA1105 MAC programming model is through the static config (the xMII 110639710229SVladimir Oltean * Mode table cannot be dynamically reconfigured), and we have to program 110739710229SVladimir Oltean * that early (earlier than PHYLINK calls us, anyway). 110839710229SVladimir Oltean * So just error out in case the connected PHY attempts to change the initial 110939710229SVladimir Oltean * system interface MII protocol from what is defined in the DT, at least for 111039710229SVladimir Oltean * now. 111139710229SVladimir Oltean */ 111239710229SVladimir Oltean static bool sja1105_phy_mode_mismatch(struct sja1105_private *priv, int port, 111339710229SVladimir Oltean phy_interface_t interface) 111439710229SVladimir Oltean { 1115bf4edf4aSVladimir Oltean return priv->phy_mode[port] != interface; 111639710229SVladimir Oltean } 111739710229SVladimir Oltean 1118af7cd036SVladimir Oltean static void sja1105_mac_config(struct dsa_switch *ds, int port, 1119ffe10e67SVladimir Oltean unsigned int mode, 1120af7cd036SVladimir Oltean const struct phylink_link_state *state) 11218aa9ebccSVladimir Oltean { 11223ad1d171SVladimir Oltean struct dsa_port *dp = dsa_to_port(ds, port); 11238aa9ebccSVladimir Oltean struct sja1105_private *priv = ds->priv; 11243ad1d171SVladimir Oltean struct dw_xpcs *xpcs; 11258aa9ebccSVladimir Oltean 1126ec8582d1SVladimir Oltean if (sja1105_phy_mode_mismatch(priv, port, state->interface)) { 1127ec8582d1SVladimir Oltean dev_err(ds->dev, "Changing PHY mode to %s not supported!\n", 1128ec8582d1SVladimir Oltean phy_modes(state->interface)); 112939710229SVladimir Oltean return; 1130ec8582d1SVladimir Oltean } 113139710229SVladimir Oltean 11323ad1d171SVladimir Oltean xpcs = priv->xpcs[port]; 1133ffe10e67SVladimir Oltean 11343ad1d171SVladimir Oltean if (xpcs) 11353ad1d171SVladimir Oltean phylink_set_pcs(dp->pl, &xpcs->pcs); 11368400cff6SVladimir Oltean } 11378400cff6SVladimir Oltean 11388400cff6SVladimir Oltean static void sja1105_mac_link_down(struct dsa_switch *ds, int port, 11398400cff6SVladimir Oltean unsigned int mode, 11408400cff6SVladimir Oltean phy_interface_t interface) 11418400cff6SVladimir Oltean { 11428400cff6SVladimir Oltean sja1105_inhibit_tx(ds->priv, BIT(port), true); 11438400cff6SVladimir Oltean } 11448400cff6SVladimir Oltean 11458400cff6SVladimir Oltean static void sja1105_mac_link_up(struct dsa_switch *ds, int port, 11468400cff6SVladimir Oltean unsigned int mode, 11478400cff6SVladimir Oltean phy_interface_t interface, 11485b502a7bSRussell King struct phy_device *phydev, 11495b502a7bSRussell King int speed, int duplex, 11505b502a7bSRussell King bool tx_pause, bool rx_pause) 11518400cff6SVladimir Oltean { 1152ec8582d1SVladimir Oltean struct sja1105_private *priv = ds->priv; 1153ec8582d1SVladimir Oltean 1154ec8582d1SVladimir Oltean sja1105_adjust_port_config(priv, port, speed); 1155ec8582d1SVladimir Oltean 1156ec8582d1SVladimir Oltean sja1105_inhibit_tx(priv, BIT(port), false); 11578aa9ebccSVladimir Oltean } 11588aa9ebccSVladimir Oltean 1159ad9f299aSVladimir Oltean static void sja1105_phylink_validate(struct dsa_switch *ds, int port, 1160ad9f299aSVladimir Oltean unsigned long *supported, 1161ad9f299aSVladimir Oltean struct phylink_link_state *state) 1162ad9f299aSVladimir Oltean { 1163ad9f299aSVladimir Oltean /* Construct a new mask which exhaustively contains all link features 1164ad9f299aSVladimir Oltean * supported by the MAC, and then apply that (logical AND) to what will 1165ad9f299aSVladimir Oltean * be sent to the PHY for "marketing". 1166ad9f299aSVladimir Oltean */ 1167ad9f299aSVladimir Oltean __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, }; 1168ad9f299aSVladimir Oltean struct sja1105_private *priv = ds->priv; 1169ad9f299aSVladimir Oltean struct sja1105_xmii_params_entry *mii; 1170ad9f299aSVladimir Oltean 1171ad9f299aSVladimir Oltean mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries; 1172ad9f299aSVladimir Oltean 117339710229SVladimir Oltean /* include/linux/phylink.h says: 117439710229SVladimir Oltean * When @state->interface is %PHY_INTERFACE_MODE_NA, phylink 117539710229SVladimir Oltean * expects the MAC driver to return all supported link modes. 117639710229SVladimir Oltean */ 117739710229SVladimir Oltean if (state->interface != PHY_INTERFACE_MODE_NA && 117839710229SVladimir Oltean sja1105_phy_mode_mismatch(priv, port, state->interface)) { 117939710229SVladimir Oltean bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS); 118039710229SVladimir Oltean return; 118139710229SVladimir Oltean } 118239710229SVladimir Oltean 1183ad9f299aSVladimir Oltean /* The MAC does not support pause frames, and also doesn't 1184ad9f299aSVladimir Oltean * support half-duplex traffic modes. 1185ad9f299aSVladimir Oltean */ 1186ad9f299aSVladimir Oltean phylink_set(mask, Autoneg); 1187ad9f299aSVladimir Oltean phylink_set(mask, MII); 1188ad9f299aSVladimir Oltean phylink_set(mask, 10baseT_Full); 1189ad9f299aSVladimir Oltean phylink_set(mask, 100baseT_Full); 1190ca68e138SOleksij Rempel phylink_set(mask, 100baseT1_Full); 1191ffe10e67SVladimir Oltean if (mii->xmii_mode[port] == XMII_MODE_RGMII || 1192ffe10e67SVladimir Oltean mii->xmii_mode[port] == XMII_MODE_SGMII) 1193ad9f299aSVladimir Oltean phylink_set(mask, 1000baseT_Full); 119456b63466SVladimir Oltean if (priv->info->supports_2500basex[port]) { 119556b63466SVladimir Oltean phylink_set(mask, 2500baseT_Full); 119656b63466SVladimir Oltean phylink_set(mask, 2500baseX_Full); 119756b63466SVladimir Oltean } 1198ad9f299aSVladimir Oltean 1199ad9f299aSVladimir Oltean bitmap_and(supported, supported, mask, __ETHTOOL_LINK_MODE_MASK_NBITS); 1200ad9f299aSVladimir Oltean bitmap_and(state->advertising, state->advertising, mask, 1201ad9f299aSVladimir Oltean __ETHTOOL_LINK_MODE_MASK_NBITS); 1202ad9f299aSVladimir Oltean } 1203ad9f299aSVladimir Oltean 120460f6053fSVladimir Oltean static int 120560f6053fSVladimir Oltean sja1105_find_static_fdb_entry(struct sja1105_private *priv, int port, 120660f6053fSVladimir Oltean const struct sja1105_l2_lookup_entry *requested) 120760f6053fSVladimir Oltean { 120860f6053fSVladimir Oltean struct sja1105_l2_lookup_entry *l2_lookup; 120960f6053fSVladimir Oltean struct sja1105_table *table; 121060f6053fSVladimir Oltean int i; 121160f6053fSVladimir Oltean 121260f6053fSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; 121360f6053fSVladimir Oltean l2_lookup = table->entries; 121460f6053fSVladimir Oltean 121560f6053fSVladimir Oltean for (i = 0; i < table->entry_count; i++) 121660f6053fSVladimir Oltean if (l2_lookup[i].macaddr == requested->macaddr && 121760f6053fSVladimir Oltean l2_lookup[i].vlanid == requested->vlanid && 121860f6053fSVladimir Oltean l2_lookup[i].destports & BIT(port)) 121960f6053fSVladimir Oltean return i; 122060f6053fSVladimir Oltean 122160f6053fSVladimir Oltean return -1; 122260f6053fSVladimir Oltean } 122360f6053fSVladimir Oltean 122460f6053fSVladimir Oltean /* We want FDB entries added statically through the bridge command to persist 122560f6053fSVladimir Oltean * across switch resets, which are a common thing during normal SJA1105 122660f6053fSVladimir Oltean * operation. So we have to back them up in the static configuration tables 122760f6053fSVladimir Oltean * and hence apply them on next static config upload... yay! 122860f6053fSVladimir Oltean */ 122960f6053fSVladimir Oltean static int 123060f6053fSVladimir Oltean sja1105_static_fdb_change(struct sja1105_private *priv, int port, 123160f6053fSVladimir Oltean const struct sja1105_l2_lookup_entry *requested, 123260f6053fSVladimir Oltean bool keep) 123360f6053fSVladimir Oltean { 123460f6053fSVladimir Oltean struct sja1105_l2_lookup_entry *l2_lookup; 123560f6053fSVladimir Oltean struct sja1105_table *table; 123660f6053fSVladimir Oltean int rc, match; 123760f6053fSVladimir Oltean 123860f6053fSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; 123960f6053fSVladimir Oltean 124060f6053fSVladimir Oltean match = sja1105_find_static_fdb_entry(priv, port, requested); 124160f6053fSVladimir Oltean if (match < 0) { 124260f6053fSVladimir Oltean /* Can't delete a missing entry. */ 124360f6053fSVladimir Oltean if (!keep) 124460f6053fSVladimir Oltean return 0; 124560f6053fSVladimir Oltean 124660f6053fSVladimir Oltean /* No match => new entry */ 124760f6053fSVladimir Oltean rc = sja1105_table_resize(table, table->entry_count + 1); 124860f6053fSVladimir Oltean if (rc) 124960f6053fSVladimir Oltean return rc; 125060f6053fSVladimir Oltean 125160f6053fSVladimir Oltean match = table->entry_count - 1; 125260f6053fSVladimir Oltean } 125360f6053fSVladimir Oltean 125460f6053fSVladimir Oltean /* Assign pointer after the resize (it may be new memory) */ 125560f6053fSVladimir Oltean l2_lookup = table->entries; 125660f6053fSVladimir Oltean 125760f6053fSVladimir Oltean /* We have a match. 125860f6053fSVladimir Oltean * If the job was to add this FDB entry, it's already done (mostly 125960f6053fSVladimir Oltean * anyway, since the port forwarding mask may have changed, case in 126060f6053fSVladimir Oltean * which we update it). 126160f6053fSVladimir Oltean * Otherwise we have to delete it. 126260f6053fSVladimir Oltean */ 126360f6053fSVladimir Oltean if (keep) { 126460f6053fSVladimir Oltean l2_lookup[match] = *requested; 126560f6053fSVladimir Oltean return 0; 126660f6053fSVladimir Oltean } 126760f6053fSVladimir Oltean 126860f6053fSVladimir Oltean /* To remove, the strategy is to overwrite the element with 126960f6053fSVladimir Oltean * the last one, and then reduce the array size by 1 127060f6053fSVladimir Oltean */ 127160f6053fSVladimir Oltean l2_lookup[match] = l2_lookup[table->entry_count - 1]; 127260f6053fSVladimir Oltean return sja1105_table_resize(table, table->entry_count - 1); 127360f6053fSVladimir Oltean } 127460f6053fSVladimir Oltean 1275291d1e72SVladimir Oltean /* First-generation switches have a 4-way set associative TCAM that 1276291d1e72SVladimir Oltean * holds the FDB entries. An FDB index spans from 0 to 1023 and is comprised of 1277291d1e72SVladimir Oltean * a "bin" (grouping of 4 entries) and a "way" (an entry within a bin). 1278291d1e72SVladimir Oltean * For the placement of a newly learnt FDB entry, the switch selects the bin 1279291d1e72SVladimir Oltean * based on a hash function, and the way within that bin incrementally. 1280291d1e72SVladimir Oltean */ 128109c1b412SVladimir Oltean static int sja1105et_fdb_index(int bin, int way) 1282291d1e72SVladimir Oltean { 1283291d1e72SVladimir Oltean return bin * SJA1105ET_FDB_BIN_SIZE + way; 1284291d1e72SVladimir Oltean } 1285291d1e72SVladimir Oltean 12869dfa6911SVladimir Oltean static int sja1105et_is_fdb_entry_in_bin(struct sja1105_private *priv, int bin, 1287291d1e72SVladimir Oltean const u8 *addr, u16 vid, 1288291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry *match, 1289291d1e72SVladimir Oltean int *last_unused) 1290291d1e72SVladimir Oltean { 1291291d1e72SVladimir Oltean int way; 1292291d1e72SVladimir Oltean 1293291d1e72SVladimir Oltean for (way = 0; way < SJA1105ET_FDB_BIN_SIZE; way++) { 1294291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 1295291d1e72SVladimir Oltean int index = sja1105et_fdb_index(bin, way); 1296291d1e72SVladimir Oltean 1297291d1e72SVladimir Oltean /* Skip unused entries, optionally marking them 1298291d1e72SVladimir Oltean * into the return value 1299291d1e72SVladimir Oltean */ 1300291d1e72SVladimir Oltean if (sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 1301291d1e72SVladimir Oltean index, &l2_lookup)) { 1302291d1e72SVladimir Oltean if (last_unused) 1303291d1e72SVladimir Oltean *last_unused = way; 1304291d1e72SVladimir Oltean continue; 1305291d1e72SVladimir Oltean } 1306291d1e72SVladimir Oltean 1307291d1e72SVladimir Oltean if (l2_lookup.macaddr == ether_addr_to_u64(addr) && 1308291d1e72SVladimir Oltean l2_lookup.vlanid == vid) { 1309291d1e72SVladimir Oltean if (match) 1310291d1e72SVladimir Oltean *match = l2_lookup; 1311291d1e72SVladimir Oltean return way; 1312291d1e72SVladimir Oltean } 1313291d1e72SVladimir Oltean } 1314291d1e72SVladimir Oltean /* Return an invalid entry index if not found */ 1315291d1e72SVladimir Oltean return -1; 1316291d1e72SVladimir Oltean } 1317291d1e72SVladimir Oltean 13189dfa6911SVladimir Oltean int sja1105et_fdb_add(struct dsa_switch *ds, int port, 1319291d1e72SVladimir Oltean const unsigned char *addr, u16 vid) 1320291d1e72SVladimir Oltean { 13216c5fc159SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}, tmp; 1322291d1e72SVladimir Oltean struct sja1105_private *priv = ds->priv; 1323291d1e72SVladimir Oltean struct device *dev = ds->dev; 1324291d1e72SVladimir Oltean int last_unused = -1; 13256c5fc159SVladimir Oltean int start, end, i; 132660f6053fSVladimir Oltean int bin, way, rc; 1327291d1e72SVladimir Oltean 13289dfa6911SVladimir Oltean bin = sja1105et_fdb_hash(priv, addr, vid); 1329291d1e72SVladimir Oltean 13309dfa6911SVladimir Oltean way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid, 1331291d1e72SVladimir Oltean &l2_lookup, &last_unused); 1332291d1e72SVladimir Oltean if (way >= 0) { 1333291d1e72SVladimir Oltean /* We have an FDB entry. Is our port in the destination 1334291d1e72SVladimir Oltean * mask? If yes, we need to do nothing. If not, we need 1335291d1e72SVladimir Oltean * to rewrite the entry by adding this port to it. 1336291d1e72SVladimir Oltean */ 1337e11e865bSVladimir Oltean if ((l2_lookup.destports & BIT(port)) && l2_lookup.lockeds) 1338291d1e72SVladimir Oltean return 0; 1339291d1e72SVladimir Oltean l2_lookup.destports |= BIT(port); 1340291d1e72SVladimir Oltean } else { 1341291d1e72SVladimir Oltean int index = sja1105et_fdb_index(bin, way); 1342291d1e72SVladimir Oltean 1343291d1e72SVladimir Oltean /* We don't have an FDB entry. We construct a new one and 1344291d1e72SVladimir Oltean * try to find a place for it within the FDB table. 1345291d1e72SVladimir Oltean */ 1346291d1e72SVladimir Oltean l2_lookup.macaddr = ether_addr_to_u64(addr); 1347291d1e72SVladimir Oltean l2_lookup.destports = BIT(port); 1348291d1e72SVladimir Oltean l2_lookup.vlanid = vid; 1349291d1e72SVladimir Oltean 1350291d1e72SVladimir Oltean if (last_unused >= 0) { 1351291d1e72SVladimir Oltean way = last_unused; 1352291d1e72SVladimir Oltean } else { 1353291d1e72SVladimir Oltean /* Bin is full, need to evict somebody. 1354291d1e72SVladimir Oltean * Choose victim at random. If you get these messages 1355291d1e72SVladimir Oltean * often, you may need to consider changing the 1356291d1e72SVladimir Oltean * distribution function: 1357291d1e72SVladimir Oltean * static_config[BLK_IDX_L2_LOOKUP_PARAMS].entries->poly 1358291d1e72SVladimir Oltean */ 1359291d1e72SVladimir Oltean get_random_bytes(&way, sizeof(u8)); 1360291d1e72SVladimir Oltean way %= SJA1105ET_FDB_BIN_SIZE; 1361291d1e72SVladimir Oltean dev_warn(dev, "Warning, FDB bin %d full while adding entry for %pM. Evicting entry %u.\n", 1362291d1e72SVladimir Oltean bin, addr, way); 1363291d1e72SVladimir Oltean /* Evict entry */ 1364291d1e72SVladimir Oltean sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 1365291d1e72SVladimir Oltean index, NULL, false); 1366291d1e72SVladimir Oltean } 1367291d1e72SVladimir Oltean } 1368e11e865bSVladimir Oltean l2_lookup.lockeds = true; 1369291d1e72SVladimir Oltean l2_lookup.index = sja1105et_fdb_index(bin, way); 1370291d1e72SVladimir Oltean 137160f6053fSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 1372291d1e72SVladimir Oltean l2_lookup.index, &l2_lookup, 1373291d1e72SVladimir Oltean true); 137460f6053fSVladimir Oltean if (rc < 0) 137560f6053fSVladimir Oltean return rc; 137660f6053fSVladimir Oltean 13776c5fc159SVladimir Oltean /* Invalidate a dynamically learned entry if that exists */ 13786c5fc159SVladimir Oltean start = sja1105et_fdb_index(bin, 0); 13796c5fc159SVladimir Oltean end = sja1105et_fdb_index(bin, way); 13806c5fc159SVladimir Oltean 13816c5fc159SVladimir Oltean for (i = start; i < end; i++) { 13826c5fc159SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 13836c5fc159SVladimir Oltean i, &tmp); 13846c5fc159SVladimir Oltean if (rc == -ENOENT) 13856c5fc159SVladimir Oltean continue; 13866c5fc159SVladimir Oltean if (rc) 13876c5fc159SVladimir Oltean return rc; 13886c5fc159SVladimir Oltean 13896c5fc159SVladimir Oltean if (tmp.macaddr != ether_addr_to_u64(addr) || tmp.vlanid != vid) 13906c5fc159SVladimir Oltean continue; 13916c5fc159SVladimir Oltean 13926c5fc159SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 13936c5fc159SVladimir Oltean i, NULL, false); 13946c5fc159SVladimir Oltean if (rc) 13956c5fc159SVladimir Oltean return rc; 13966c5fc159SVladimir Oltean 13976c5fc159SVladimir Oltean break; 13986c5fc159SVladimir Oltean } 13996c5fc159SVladimir Oltean 140060f6053fSVladimir Oltean return sja1105_static_fdb_change(priv, port, &l2_lookup, true); 1401291d1e72SVladimir Oltean } 1402291d1e72SVladimir Oltean 14039dfa6911SVladimir Oltean int sja1105et_fdb_del(struct dsa_switch *ds, int port, 1404291d1e72SVladimir Oltean const unsigned char *addr, u16 vid) 1405291d1e72SVladimir Oltean { 1406291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 1407291d1e72SVladimir Oltean struct sja1105_private *priv = ds->priv; 140860f6053fSVladimir Oltean int index, bin, way, rc; 1409291d1e72SVladimir Oltean bool keep; 1410291d1e72SVladimir Oltean 14119dfa6911SVladimir Oltean bin = sja1105et_fdb_hash(priv, addr, vid); 14129dfa6911SVladimir Oltean way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid, 1413291d1e72SVladimir Oltean &l2_lookup, NULL); 1414291d1e72SVladimir Oltean if (way < 0) 1415291d1e72SVladimir Oltean return 0; 1416291d1e72SVladimir Oltean index = sja1105et_fdb_index(bin, way); 1417291d1e72SVladimir Oltean 1418291d1e72SVladimir Oltean /* We have an FDB entry. Is our port in the destination mask? If yes, 1419291d1e72SVladimir Oltean * we need to remove it. If the resulting port mask becomes empty, we 1420291d1e72SVladimir Oltean * need to completely evict the FDB entry. 1421291d1e72SVladimir Oltean * Otherwise we just write it back. 1422291d1e72SVladimir Oltean */ 1423291d1e72SVladimir Oltean l2_lookup.destports &= ~BIT(port); 14247752e937SVladimir Oltean 1425291d1e72SVladimir Oltean if (l2_lookup.destports) 1426291d1e72SVladimir Oltean keep = true; 1427291d1e72SVladimir Oltean else 1428291d1e72SVladimir Oltean keep = false; 1429291d1e72SVladimir Oltean 143060f6053fSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 1431291d1e72SVladimir Oltean index, &l2_lookup, keep); 143260f6053fSVladimir Oltean if (rc < 0) 143360f6053fSVladimir Oltean return rc; 143460f6053fSVladimir Oltean 143560f6053fSVladimir Oltean return sja1105_static_fdb_change(priv, port, &l2_lookup, keep); 1436291d1e72SVladimir Oltean } 1437291d1e72SVladimir Oltean 14389dfa6911SVladimir Oltean int sja1105pqrs_fdb_add(struct dsa_switch *ds, int port, 14399dfa6911SVladimir Oltean const unsigned char *addr, u16 vid) 14409dfa6911SVladimir Oltean { 14416c5fc159SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}, tmp; 14421da73821SVladimir Oltean struct sja1105_private *priv = ds->priv; 14431da73821SVladimir Oltean int rc, i; 14441da73821SVladimir Oltean 14451da73821SVladimir Oltean /* Search for an existing entry in the FDB table */ 14461da73821SVladimir Oltean l2_lookup.macaddr = ether_addr_to_u64(addr); 14471da73821SVladimir Oltean l2_lookup.vlanid = vid; 14481da73821SVladimir Oltean l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0); 14491da73821SVladimir Oltean l2_lookup.mask_vlanid = VLAN_VID_MASK; 14501da73821SVladimir Oltean l2_lookup.destports = BIT(port); 14511da73821SVladimir Oltean 1452728db843SVladimir Oltean tmp = l2_lookup; 1453728db843SVladimir Oltean 14541da73821SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 1455728db843SVladimir Oltean SJA1105_SEARCH, &tmp); 1456728db843SVladimir Oltean if (rc == 0 && tmp.index != SJA1105_MAX_L2_LOOKUP_COUNT - 1) { 1457e11e865bSVladimir Oltean /* Found a static entry and this port is already in the entry's 14581da73821SVladimir Oltean * port mask => job done 14591da73821SVladimir Oltean */ 1460728db843SVladimir Oltean if ((tmp.destports & BIT(port)) && tmp.lockeds) 14611da73821SVladimir Oltean return 0; 1462728db843SVladimir Oltean 1463728db843SVladimir Oltean l2_lookup = tmp; 1464728db843SVladimir Oltean 14651da73821SVladimir Oltean /* l2_lookup.index is populated by the switch in case it 14661da73821SVladimir Oltean * found something. 14671da73821SVladimir Oltean */ 14681da73821SVladimir Oltean l2_lookup.destports |= BIT(port); 14691da73821SVladimir Oltean goto skip_finding_an_index; 14701da73821SVladimir Oltean } 14711da73821SVladimir Oltean 14721da73821SVladimir Oltean /* Not found, so try to find an unused spot in the FDB. 14731da73821SVladimir Oltean * This is slightly inefficient because the strategy is knock-knock at 14741da73821SVladimir Oltean * every possible position from 0 to 1023. 14751da73821SVladimir Oltean */ 14761da73821SVladimir Oltean for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) { 14771da73821SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 14781da73821SVladimir Oltean i, NULL); 14791da73821SVladimir Oltean if (rc < 0) 14801da73821SVladimir Oltean break; 14811da73821SVladimir Oltean } 14821da73821SVladimir Oltean if (i == SJA1105_MAX_L2_LOOKUP_COUNT) { 14831da73821SVladimir Oltean dev_err(ds->dev, "FDB is full, cannot add entry.\n"); 14841da73821SVladimir Oltean return -EINVAL; 14851da73821SVladimir Oltean } 14861da73821SVladimir Oltean l2_lookup.index = i; 14871da73821SVladimir Oltean 14881da73821SVladimir Oltean skip_finding_an_index: 1489e11e865bSVladimir Oltean l2_lookup.lockeds = true; 1490e11e865bSVladimir Oltean 149160f6053fSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 14921da73821SVladimir Oltean l2_lookup.index, &l2_lookup, 14931da73821SVladimir Oltean true); 149460f6053fSVladimir Oltean if (rc < 0) 149560f6053fSVladimir Oltean return rc; 149660f6053fSVladimir Oltean 14976c5fc159SVladimir Oltean /* The switch learns dynamic entries and looks up the FDB left to 14986c5fc159SVladimir Oltean * right. It is possible that our addition was concurrent with the 14996c5fc159SVladimir Oltean * dynamic learning of the same address, so now that the static entry 15006c5fc159SVladimir Oltean * has been installed, we are certain that address learning for this 15016c5fc159SVladimir Oltean * particular address has been turned off, so the dynamic entry either 15026c5fc159SVladimir Oltean * is in the FDB at an index smaller than the static one, or isn't (it 15036c5fc159SVladimir Oltean * can also be at a larger index, but in that case it is inactive 15046c5fc159SVladimir Oltean * because the static FDB entry will match first, and the dynamic one 15056c5fc159SVladimir Oltean * will eventually age out). Search for a dynamically learned address 15066c5fc159SVladimir Oltean * prior to our static one and invalidate it. 15076c5fc159SVladimir Oltean */ 15086c5fc159SVladimir Oltean tmp = l2_lookup; 15096c5fc159SVladimir Oltean 15106c5fc159SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 15116c5fc159SVladimir Oltean SJA1105_SEARCH, &tmp); 15126c5fc159SVladimir Oltean if (rc < 0) { 15136c5fc159SVladimir Oltean dev_err(ds->dev, 15146c5fc159SVladimir Oltean "port %d failed to read back entry for %pM vid %d: %pe\n", 15156c5fc159SVladimir Oltean port, addr, vid, ERR_PTR(rc)); 15166c5fc159SVladimir Oltean return rc; 15176c5fc159SVladimir Oltean } 15186c5fc159SVladimir Oltean 15196c5fc159SVladimir Oltean if (tmp.index < l2_lookup.index) { 15206c5fc159SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 15216c5fc159SVladimir Oltean tmp.index, NULL, false); 15226c5fc159SVladimir Oltean if (rc < 0) 15236c5fc159SVladimir Oltean return rc; 15246c5fc159SVladimir Oltean } 15256c5fc159SVladimir Oltean 152660f6053fSVladimir Oltean return sja1105_static_fdb_change(priv, port, &l2_lookup, true); 15279dfa6911SVladimir Oltean } 15289dfa6911SVladimir Oltean 15299dfa6911SVladimir Oltean int sja1105pqrs_fdb_del(struct dsa_switch *ds, int port, 15309dfa6911SVladimir Oltean const unsigned char *addr, u16 vid) 15319dfa6911SVladimir Oltean { 15321da73821SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 15331da73821SVladimir Oltean struct sja1105_private *priv = ds->priv; 15341da73821SVladimir Oltean bool keep; 15351da73821SVladimir Oltean int rc; 15361da73821SVladimir Oltean 15371da73821SVladimir Oltean l2_lookup.macaddr = ether_addr_to_u64(addr); 15381da73821SVladimir Oltean l2_lookup.vlanid = vid; 15391da73821SVladimir Oltean l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0); 15401da73821SVladimir Oltean l2_lookup.mask_vlanid = VLAN_VID_MASK; 15411da73821SVladimir Oltean l2_lookup.destports = BIT(port); 15421da73821SVladimir Oltean 15431da73821SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 15441da73821SVladimir Oltean SJA1105_SEARCH, &l2_lookup); 15451da73821SVladimir Oltean if (rc < 0) 15461da73821SVladimir Oltean return 0; 15471da73821SVladimir Oltean 15481da73821SVladimir Oltean l2_lookup.destports &= ~BIT(port); 15491da73821SVladimir Oltean 15501da73821SVladimir Oltean /* Decide whether we remove just this port from the FDB entry, 15511da73821SVladimir Oltean * or if we remove it completely. 15521da73821SVladimir Oltean */ 15531da73821SVladimir Oltean if (l2_lookup.destports) 15541da73821SVladimir Oltean keep = true; 15551da73821SVladimir Oltean else 15561da73821SVladimir Oltean keep = false; 15571da73821SVladimir Oltean 155860f6053fSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 15591da73821SVladimir Oltean l2_lookup.index, &l2_lookup, keep); 156060f6053fSVladimir Oltean if (rc < 0) 156160f6053fSVladimir Oltean return rc; 156260f6053fSVladimir Oltean 156360f6053fSVladimir Oltean return sja1105_static_fdb_change(priv, port, &l2_lookup, keep); 15649dfa6911SVladimir Oltean } 15659dfa6911SVladimir Oltean 15669dfa6911SVladimir Oltean static int sja1105_fdb_add(struct dsa_switch *ds, int port, 15679dfa6911SVladimir Oltean const unsigned char *addr, u16 vid) 15689dfa6911SVladimir Oltean { 15699dfa6911SVladimir Oltean struct sja1105_private *priv = ds->priv; 1570b3ee526aSVladimir Oltean 15716d7c7d94SVladimir Oltean /* dsa_8021q is in effect when the bridge's vlan_filtering isn't, 15726d7c7d94SVladimir Oltean * so the switch still does some VLAN processing internally. 15736d7c7d94SVladimir Oltean * But Shared VLAN Learning (SVL) is also active, and it will take 15746d7c7d94SVladimir Oltean * care of autonomous forwarding between the unique pvid's of each 15756d7c7d94SVladimir Oltean * port. Here we just make sure that users can't add duplicate FDB 15766d7c7d94SVladimir Oltean * entries when in this mode - the actual VID doesn't matter except 15776d7c7d94SVladimir Oltean * for what gets printed in 'bridge fdb show'. In the case of zero, 15786d7c7d94SVladimir Oltean * no VID gets printed at all. 157993647594SVladimir Oltean */ 15807f14937fSVladimir Oltean if (priv->vlan_state != SJA1105_VLAN_FILTERING_FULL) 15816d7c7d94SVladimir Oltean vid = 0; 158293647594SVladimir Oltean 15836d7c7d94SVladimir Oltean return priv->info->fdb_add_cmd(ds, port, addr, vid); 15849dfa6911SVladimir Oltean } 15859dfa6911SVladimir Oltean 15869dfa6911SVladimir Oltean static int sja1105_fdb_del(struct dsa_switch *ds, int port, 15879dfa6911SVladimir Oltean const unsigned char *addr, u16 vid) 15889dfa6911SVladimir Oltean { 15899dfa6911SVladimir Oltean struct sja1105_private *priv = ds->priv; 15909dfa6911SVladimir Oltean 15917f14937fSVladimir Oltean if (priv->vlan_state != SJA1105_VLAN_FILTERING_FULL) 15926d7c7d94SVladimir Oltean vid = 0; 15936d7c7d94SVladimir Oltean 1594b3ee526aSVladimir Oltean return priv->info->fdb_del_cmd(ds, port, addr, vid); 15959dfa6911SVladimir Oltean } 15969dfa6911SVladimir Oltean 1597291d1e72SVladimir Oltean static int sja1105_fdb_dump(struct dsa_switch *ds, int port, 1598291d1e72SVladimir Oltean dsa_fdb_dump_cb_t *cb, void *data) 1599291d1e72SVladimir Oltean { 1600291d1e72SVladimir Oltean struct sja1105_private *priv = ds->priv; 1601291d1e72SVladimir Oltean struct device *dev = ds->dev; 1602291d1e72SVladimir Oltean int i; 1603291d1e72SVladimir Oltean 1604291d1e72SVladimir Oltean for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) { 1605291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 1606291d1e72SVladimir Oltean u8 macaddr[ETH_ALEN]; 1607291d1e72SVladimir Oltean int rc; 1608291d1e72SVladimir Oltean 1609291d1e72SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 1610291d1e72SVladimir Oltean i, &l2_lookup); 1611291d1e72SVladimir Oltean /* No fdb entry at i, not an issue */ 1612def84604SVladimir Oltean if (rc == -ENOENT) 1613291d1e72SVladimir Oltean continue; 1614291d1e72SVladimir Oltean if (rc) { 1615291d1e72SVladimir Oltean dev_err(dev, "Failed to dump FDB: %d\n", rc); 1616291d1e72SVladimir Oltean return rc; 1617291d1e72SVladimir Oltean } 1618291d1e72SVladimir Oltean 1619291d1e72SVladimir Oltean /* FDB dump callback is per port. This means we have to 1620291d1e72SVladimir Oltean * disregard a valid entry if it's not for this port, even if 1621291d1e72SVladimir Oltean * only to revisit it later. This is inefficient because the 1622291d1e72SVladimir Oltean * 1024-sized FDB table needs to be traversed 4 times through 1623291d1e72SVladimir Oltean * SPI during a 'bridge fdb show' command. 1624291d1e72SVladimir Oltean */ 1625291d1e72SVladimir Oltean if (!(l2_lookup.destports & BIT(port))) 1626291d1e72SVladimir Oltean continue; 16274d942354SVladimir Oltean 16284d942354SVladimir Oltean /* We need to hide the FDB entry for unknown multicast */ 16294d942354SVladimir Oltean if (l2_lookup.macaddr == SJA1105_UNKNOWN_MULTICAST && 16304d942354SVladimir Oltean l2_lookup.mask_macaddr == SJA1105_UNKNOWN_MULTICAST) 16314d942354SVladimir Oltean continue; 16324d942354SVladimir Oltean 1633291d1e72SVladimir Oltean u64_to_ether_addr(l2_lookup.macaddr, macaddr); 163493647594SVladimir Oltean 16356d7c7d94SVladimir Oltean /* We need to hide the dsa_8021q VLANs from the user. */ 16367f14937fSVladimir Oltean if (priv->vlan_state == SJA1105_VLAN_UNAWARE) 16376d7c7d94SVladimir Oltean l2_lookup.vlanid = 0; 1638*21b52fedSVladimir Oltean rc = cb(macaddr, l2_lookup.vlanid, l2_lookup.lockeds, data); 1639*21b52fedSVladimir Oltean if (rc) 1640*21b52fedSVladimir Oltean return rc; 1641291d1e72SVladimir Oltean } 1642291d1e72SVladimir Oltean return 0; 1643291d1e72SVladimir Oltean } 1644291d1e72SVladimir Oltean 1645a52b2da7SVladimir Oltean static int sja1105_mdb_add(struct dsa_switch *ds, int port, 1646291d1e72SVladimir Oltean const struct switchdev_obj_port_mdb *mdb) 1647291d1e72SVladimir Oltean { 1648a52b2da7SVladimir Oltean return sja1105_fdb_add(ds, port, mdb->addr, mdb->vid); 1649291d1e72SVladimir Oltean } 1650291d1e72SVladimir Oltean 1651291d1e72SVladimir Oltean static int sja1105_mdb_del(struct dsa_switch *ds, int port, 1652291d1e72SVladimir Oltean const struct switchdev_obj_port_mdb *mdb) 1653291d1e72SVladimir Oltean { 1654291d1e72SVladimir Oltean return sja1105_fdb_del(ds, port, mdb->addr, mdb->vid); 1655291d1e72SVladimir Oltean } 1656291d1e72SVladimir Oltean 16577f7ccdeaSVladimir Oltean /* Common function for unicast and broadcast flood configuration. 16587f7ccdeaSVladimir Oltean * Flooding is configured between each {ingress, egress} port pair, and since 16597f7ccdeaSVladimir Oltean * the bridge's semantics are those of "egress flooding", it means we must 16607f7ccdeaSVladimir Oltean * enable flooding towards this port from all ingress ports that are in the 16617f7ccdeaSVladimir Oltean * same forwarding domain. 16627f7ccdeaSVladimir Oltean */ 16637f7ccdeaSVladimir Oltean static int sja1105_manage_flood_domains(struct sja1105_private *priv) 16647f7ccdeaSVladimir Oltean { 16657f7ccdeaSVladimir Oltean struct sja1105_l2_forwarding_entry *l2_fwd; 16667f7ccdeaSVladimir Oltean struct dsa_switch *ds = priv->ds; 16677f7ccdeaSVladimir Oltean int from, to, rc; 16687f7ccdeaSVladimir Oltean 16697f7ccdeaSVladimir Oltean l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries; 16707f7ccdeaSVladimir Oltean 16717f7ccdeaSVladimir Oltean for (from = 0; from < ds->num_ports; from++) { 16727f7ccdeaSVladimir Oltean u64 fl_domain = 0, bc_domain = 0; 16737f7ccdeaSVladimir Oltean 16747f7ccdeaSVladimir Oltean for (to = 0; to < priv->ds->num_ports; to++) { 16757f7ccdeaSVladimir Oltean if (!sja1105_can_forward(l2_fwd, from, to)) 16767f7ccdeaSVladimir Oltean continue; 16777f7ccdeaSVladimir Oltean 16787f7ccdeaSVladimir Oltean if (priv->ucast_egress_floods & BIT(to)) 16797f7ccdeaSVladimir Oltean fl_domain |= BIT(to); 16807f7ccdeaSVladimir Oltean if (priv->bcast_egress_floods & BIT(to)) 16817f7ccdeaSVladimir Oltean bc_domain |= BIT(to); 16827f7ccdeaSVladimir Oltean } 16837f7ccdeaSVladimir Oltean 16847f7ccdeaSVladimir Oltean /* Nothing changed, nothing to do */ 16857f7ccdeaSVladimir Oltean if (l2_fwd[from].fl_domain == fl_domain && 16867f7ccdeaSVladimir Oltean l2_fwd[from].bc_domain == bc_domain) 16877f7ccdeaSVladimir Oltean continue; 16887f7ccdeaSVladimir Oltean 16897f7ccdeaSVladimir Oltean l2_fwd[from].fl_domain = fl_domain; 16907f7ccdeaSVladimir Oltean l2_fwd[from].bc_domain = bc_domain; 16917f7ccdeaSVladimir Oltean 16927f7ccdeaSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING, 16937f7ccdeaSVladimir Oltean from, &l2_fwd[from], true); 16947f7ccdeaSVladimir Oltean if (rc < 0) 16957f7ccdeaSVladimir Oltean return rc; 16967f7ccdeaSVladimir Oltean } 16977f7ccdeaSVladimir Oltean 16987f7ccdeaSVladimir Oltean return 0; 16997f7ccdeaSVladimir Oltean } 17007f7ccdeaSVladimir Oltean 17018aa9ebccSVladimir Oltean static int sja1105_bridge_member(struct dsa_switch *ds, int port, 17028aa9ebccSVladimir Oltean struct net_device *br, bool member) 17038aa9ebccSVladimir Oltean { 17048aa9ebccSVladimir Oltean struct sja1105_l2_forwarding_entry *l2_fwd; 17058aa9ebccSVladimir Oltean struct sja1105_private *priv = ds->priv; 17068aa9ebccSVladimir Oltean int i, rc; 17078aa9ebccSVladimir Oltean 17088aa9ebccSVladimir Oltean l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries; 17098aa9ebccSVladimir Oltean 1710542043e9SVladimir Oltean for (i = 0; i < ds->num_ports; i++) { 17118aa9ebccSVladimir Oltean /* Add this port to the forwarding matrix of the 17128aa9ebccSVladimir Oltean * other ports in the same bridge, and viceversa. 17138aa9ebccSVladimir Oltean */ 17148aa9ebccSVladimir Oltean if (!dsa_is_user_port(ds, i)) 17158aa9ebccSVladimir Oltean continue; 17168aa9ebccSVladimir Oltean /* For the ports already under the bridge, only one thing needs 17178aa9ebccSVladimir Oltean * to be done, and that is to add this port to their 17188aa9ebccSVladimir Oltean * reachability domain. So we can perform the SPI write for 17198aa9ebccSVladimir Oltean * them immediately. However, for this port itself (the one 17208aa9ebccSVladimir Oltean * that is new to the bridge), we need to add all other ports 17218aa9ebccSVladimir Oltean * to its reachability domain. So we do that incrementally in 17228aa9ebccSVladimir Oltean * this loop, and perform the SPI write only at the end, once 17238aa9ebccSVladimir Oltean * the domain contains all other bridge ports. 17248aa9ebccSVladimir Oltean */ 17258aa9ebccSVladimir Oltean if (i == port) 17268aa9ebccSVladimir Oltean continue; 17278aa9ebccSVladimir Oltean if (dsa_to_port(ds, i)->bridge_dev != br) 17288aa9ebccSVladimir Oltean continue; 17298aa9ebccSVladimir Oltean sja1105_port_allow_traffic(l2_fwd, i, port, member); 17308aa9ebccSVladimir Oltean sja1105_port_allow_traffic(l2_fwd, port, i, member); 17318aa9ebccSVladimir Oltean 17328aa9ebccSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING, 17338aa9ebccSVladimir Oltean i, &l2_fwd[i], true); 17348aa9ebccSVladimir Oltean if (rc < 0) 17358aa9ebccSVladimir Oltean return rc; 17368aa9ebccSVladimir Oltean } 17378aa9ebccSVladimir Oltean 17387f7ccdeaSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING, 17398aa9ebccSVladimir Oltean port, &l2_fwd[port], true); 17407f7ccdeaSVladimir Oltean if (rc) 17417f7ccdeaSVladimir Oltean return rc; 17427f7ccdeaSVladimir Oltean 17437f7ccdeaSVladimir Oltean return sja1105_manage_flood_domains(priv); 17448aa9ebccSVladimir Oltean } 17458aa9ebccSVladimir Oltean 1746640f763fSVladimir Oltean static void sja1105_bridge_stp_state_set(struct dsa_switch *ds, int port, 1747640f763fSVladimir Oltean u8 state) 1748640f763fSVladimir Oltean { 1749640f763fSVladimir Oltean struct sja1105_private *priv = ds->priv; 1750640f763fSVladimir Oltean struct sja1105_mac_config_entry *mac; 1751640f763fSVladimir Oltean 1752640f763fSVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 1753640f763fSVladimir Oltean 1754640f763fSVladimir Oltean switch (state) { 1755640f763fSVladimir Oltean case BR_STATE_DISABLED: 1756640f763fSVladimir Oltean case BR_STATE_BLOCKING: 1757640f763fSVladimir Oltean /* From UM10944 description of DRPDTAG (why put this there?): 1758640f763fSVladimir Oltean * "Management traffic flows to the port regardless of the state 1759640f763fSVladimir Oltean * of the INGRESS flag". So BPDUs are still be allowed to pass. 1760640f763fSVladimir Oltean * At the moment no difference between DISABLED and BLOCKING. 1761640f763fSVladimir Oltean */ 1762640f763fSVladimir Oltean mac[port].ingress = false; 1763640f763fSVladimir Oltean mac[port].egress = false; 1764640f763fSVladimir Oltean mac[port].dyn_learn = false; 1765640f763fSVladimir Oltean break; 1766640f763fSVladimir Oltean case BR_STATE_LISTENING: 1767640f763fSVladimir Oltean mac[port].ingress = true; 1768640f763fSVladimir Oltean mac[port].egress = false; 1769640f763fSVladimir Oltean mac[port].dyn_learn = false; 1770640f763fSVladimir Oltean break; 1771640f763fSVladimir Oltean case BR_STATE_LEARNING: 1772640f763fSVladimir Oltean mac[port].ingress = true; 1773640f763fSVladimir Oltean mac[port].egress = false; 17744d942354SVladimir Oltean mac[port].dyn_learn = !!(priv->learn_ena & BIT(port)); 1775640f763fSVladimir Oltean break; 1776640f763fSVladimir Oltean case BR_STATE_FORWARDING: 1777640f763fSVladimir Oltean mac[port].ingress = true; 1778640f763fSVladimir Oltean mac[port].egress = true; 17794d942354SVladimir Oltean mac[port].dyn_learn = !!(priv->learn_ena & BIT(port)); 1780640f763fSVladimir Oltean break; 1781640f763fSVladimir Oltean default: 1782640f763fSVladimir Oltean dev_err(ds->dev, "invalid STP state: %d\n", state); 1783640f763fSVladimir Oltean return; 1784640f763fSVladimir Oltean } 1785640f763fSVladimir Oltean 1786640f763fSVladimir Oltean sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, 1787640f763fSVladimir Oltean &mac[port], true); 1788640f763fSVladimir Oltean } 1789640f763fSVladimir Oltean 17908aa9ebccSVladimir Oltean static int sja1105_bridge_join(struct dsa_switch *ds, int port, 17918aa9ebccSVladimir Oltean struct net_device *br) 17928aa9ebccSVladimir Oltean { 17938aa9ebccSVladimir Oltean return sja1105_bridge_member(ds, port, br, true); 17948aa9ebccSVladimir Oltean } 17958aa9ebccSVladimir Oltean 17968aa9ebccSVladimir Oltean static void sja1105_bridge_leave(struct dsa_switch *ds, int port, 17978aa9ebccSVladimir Oltean struct net_device *br) 17988aa9ebccSVladimir Oltean { 17998aa9ebccSVladimir Oltean sja1105_bridge_member(ds, port, br, false); 18008aa9ebccSVladimir Oltean } 18018aa9ebccSVladimir Oltean 18024d752508SVladimir Oltean #define BYTES_PER_KBIT (1000LL / 8) 18034d752508SVladimir Oltean 18044d752508SVladimir Oltean static int sja1105_find_unused_cbs_shaper(struct sja1105_private *priv) 18054d752508SVladimir Oltean { 18064d752508SVladimir Oltean int i; 18074d752508SVladimir Oltean 18084d752508SVladimir Oltean for (i = 0; i < priv->info->num_cbs_shapers; i++) 18094d752508SVladimir Oltean if (!priv->cbs[i].idle_slope && !priv->cbs[i].send_slope) 18104d752508SVladimir Oltean return i; 18114d752508SVladimir Oltean 18124d752508SVladimir Oltean return -1; 18134d752508SVladimir Oltean } 18144d752508SVladimir Oltean 18154d752508SVladimir Oltean static int sja1105_delete_cbs_shaper(struct sja1105_private *priv, int port, 18164d752508SVladimir Oltean int prio) 18174d752508SVladimir Oltean { 18184d752508SVladimir Oltean int i; 18194d752508SVladimir Oltean 18204d752508SVladimir Oltean for (i = 0; i < priv->info->num_cbs_shapers; i++) { 18214d752508SVladimir Oltean struct sja1105_cbs_entry *cbs = &priv->cbs[i]; 18224d752508SVladimir Oltean 18234d752508SVladimir Oltean if (cbs->port == port && cbs->prio == prio) { 18244d752508SVladimir Oltean memset(cbs, 0, sizeof(*cbs)); 18254d752508SVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_CBS, 18264d752508SVladimir Oltean i, cbs, true); 18274d752508SVladimir Oltean } 18284d752508SVladimir Oltean } 18294d752508SVladimir Oltean 18304d752508SVladimir Oltean return 0; 18314d752508SVladimir Oltean } 18324d752508SVladimir Oltean 18334d752508SVladimir Oltean static int sja1105_setup_tc_cbs(struct dsa_switch *ds, int port, 18344d752508SVladimir Oltean struct tc_cbs_qopt_offload *offload) 18354d752508SVladimir Oltean { 18364d752508SVladimir Oltean struct sja1105_private *priv = ds->priv; 18374d752508SVladimir Oltean struct sja1105_cbs_entry *cbs; 18384d752508SVladimir Oltean int index; 18394d752508SVladimir Oltean 18404d752508SVladimir Oltean if (!offload->enable) 18414d752508SVladimir Oltean return sja1105_delete_cbs_shaper(priv, port, offload->queue); 18424d752508SVladimir Oltean 18434d752508SVladimir Oltean index = sja1105_find_unused_cbs_shaper(priv); 18444d752508SVladimir Oltean if (index < 0) 18454d752508SVladimir Oltean return -ENOSPC; 18464d752508SVladimir Oltean 18474d752508SVladimir Oltean cbs = &priv->cbs[index]; 18484d752508SVladimir Oltean cbs->port = port; 18494d752508SVladimir Oltean cbs->prio = offload->queue; 18504d752508SVladimir Oltean /* locredit and sendslope are negative by definition. In hardware, 18514d752508SVladimir Oltean * positive values must be provided, and the negative sign is implicit. 18524d752508SVladimir Oltean */ 18534d752508SVladimir Oltean cbs->credit_hi = offload->hicredit; 18544d752508SVladimir Oltean cbs->credit_lo = abs(offload->locredit); 18554d752508SVladimir Oltean /* User space is in kbits/sec, hardware in bytes/sec */ 18564d752508SVladimir Oltean cbs->idle_slope = offload->idleslope * BYTES_PER_KBIT; 18574d752508SVladimir Oltean cbs->send_slope = abs(offload->sendslope * BYTES_PER_KBIT); 18584d752508SVladimir Oltean /* Convert the negative values from 64-bit 2's complement 18594d752508SVladimir Oltean * to 32-bit 2's complement (for the case of 0x80000000 whose 18604d752508SVladimir Oltean * negative is still negative). 18614d752508SVladimir Oltean */ 18624d752508SVladimir Oltean cbs->credit_lo &= GENMASK_ULL(31, 0); 18634d752508SVladimir Oltean cbs->send_slope &= GENMASK_ULL(31, 0); 18644d752508SVladimir Oltean 18654d752508SVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_CBS, index, cbs, 18664d752508SVladimir Oltean true); 18674d752508SVladimir Oltean } 18684d752508SVladimir Oltean 18694d752508SVladimir Oltean static int sja1105_reload_cbs(struct sja1105_private *priv) 18704d752508SVladimir Oltean { 18714d752508SVladimir Oltean int rc = 0, i; 18724d752508SVladimir Oltean 1873be7f62eeSVladimir Oltean /* The credit based shapers are only allocated if 1874be7f62eeSVladimir Oltean * CONFIG_NET_SCH_CBS is enabled. 1875be7f62eeSVladimir Oltean */ 1876be7f62eeSVladimir Oltean if (!priv->cbs) 1877be7f62eeSVladimir Oltean return 0; 1878be7f62eeSVladimir Oltean 18794d752508SVladimir Oltean for (i = 0; i < priv->info->num_cbs_shapers; i++) { 18804d752508SVladimir Oltean struct sja1105_cbs_entry *cbs = &priv->cbs[i]; 18814d752508SVladimir Oltean 18824d752508SVladimir Oltean if (!cbs->idle_slope && !cbs->send_slope) 18834d752508SVladimir Oltean continue; 18844d752508SVladimir Oltean 18854d752508SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_CBS, i, cbs, 18864d752508SVladimir Oltean true); 18874d752508SVladimir Oltean if (rc) 18884d752508SVladimir Oltean break; 18894d752508SVladimir Oltean } 18904d752508SVladimir Oltean 18914d752508SVladimir Oltean return rc; 18924d752508SVladimir Oltean } 18934d752508SVladimir Oltean 18942eea1fa8SVladimir Oltean static const char * const sja1105_reset_reasons[] = { 18952eea1fa8SVladimir Oltean [SJA1105_VLAN_FILTERING] = "VLAN filtering", 18962eea1fa8SVladimir Oltean [SJA1105_RX_HWTSTAMPING] = "RX timestamping", 18972eea1fa8SVladimir Oltean [SJA1105_AGEING_TIME] = "Ageing time", 18982eea1fa8SVladimir Oltean [SJA1105_SCHEDULING] = "Time-aware scheduling", 1899c279c726SVladimir Oltean [SJA1105_BEST_EFFORT_POLICING] = "Best-effort policing", 1900dfacc5a2SVladimir Oltean [SJA1105_VIRTUAL_LINKS] = "Virtual links", 19012eea1fa8SVladimir Oltean }; 19022eea1fa8SVladimir Oltean 19036666cebcSVladimir Oltean /* For situations where we need to change a setting at runtime that is only 19046666cebcSVladimir Oltean * available through the static configuration, resetting the switch in order 19056666cebcSVladimir Oltean * to upload the new static config is unavoidable. Back up the settings we 19066666cebcSVladimir Oltean * modify at runtime (currently only MAC) and restore them after uploading, 19076666cebcSVladimir Oltean * such that this operation is relatively seamless. 19086666cebcSVladimir Oltean */ 19092eea1fa8SVladimir Oltean int sja1105_static_config_reload(struct sja1105_private *priv, 19102eea1fa8SVladimir Oltean enum sja1105_reset_reason reason) 19116666cebcSVladimir Oltean { 19126cf99c13SVladimir Oltean struct ptp_system_timestamp ptp_sts_before; 19136cf99c13SVladimir Oltean struct ptp_system_timestamp ptp_sts_after; 191482760d7fSVladimir Oltean int speed_mbps[SJA1105_MAX_NUM_PORTS]; 191584db00f2SVladimir Oltean u16 bmcr[SJA1105_MAX_NUM_PORTS] = {0}; 19166666cebcSVladimir Oltean struct sja1105_mac_config_entry *mac; 19176cf99c13SVladimir Oltean struct dsa_switch *ds = priv->ds; 19186cf99c13SVladimir Oltean s64 t1, t2, t3, t4; 19196cf99c13SVladimir Oltean s64 t12, t34; 19206666cebcSVladimir Oltean int rc, i; 19216cf99c13SVladimir Oltean s64 now; 19226666cebcSVladimir Oltean 1923af580ae2SVladimir Oltean mutex_lock(&priv->mgmt_lock); 1924af580ae2SVladimir Oltean 19256666cebcSVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 19266666cebcSVladimir Oltean 19278400cff6SVladimir Oltean /* Back up the dynamic link speed changed by sja1105_adjust_port_config 19288400cff6SVladimir Oltean * in order to temporarily restore it to SJA1105_SPEED_AUTO - which the 19298400cff6SVladimir Oltean * switch wants to see in the static config in order to allow us to 19308400cff6SVladimir Oltean * change it through the dynamic interface later. 19316666cebcSVladimir Oltean */ 1932542043e9SVladimir Oltean for (i = 0; i < ds->num_ports; i++) { 19333ad1d171SVladimir Oltean u32 reg_addr = mdiobus_c45_addr(MDIO_MMD_VEND2, MDIO_CTRL1); 19343ad1d171SVladimir Oltean 193541fed17fSVladimir Oltean speed_mbps[i] = sja1105_port_speed_to_ethtool(priv, 193641fed17fSVladimir Oltean mac[i].speed); 193741fed17fSVladimir Oltean mac[i].speed = priv->info->port_speed[SJA1105_SPEED_AUTO]; 19386666cebcSVladimir Oltean 19393ad1d171SVladimir Oltean if (priv->xpcs[i]) 19403ad1d171SVladimir Oltean bmcr[i] = mdiobus_read(priv->mdio_pcs, i, reg_addr); 194184db00f2SVladimir Oltean } 1942ffe10e67SVladimir Oltean 19436cf99c13SVladimir Oltean /* No PTP operations can run right now */ 19446cf99c13SVladimir Oltean mutex_lock(&priv->ptp_data.lock); 19456cf99c13SVladimir Oltean 19466cf99c13SVladimir Oltean rc = __sja1105_ptp_gettimex(ds, &now, &ptp_sts_before); 194761c77533SVladimir Oltean if (rc < 0) { 194861c77533SVladimir Oltean mutex_unlock(&priv->ptp_data.lock); 194961c77533SVladimir Oltean goto out; 195061c77533SVladimir Oltean } 19516cf99c13SVladimir Oltean 19526666cebcSVladimir Oltean /* Reset switch and send updated static configuration */ 19536666cebcSVladimir Oltean rc = sja1105_static_config_upload(priv); 195461c77533SVladimir Oltean if (rc < 0) { 195561c77533SVladimir Oltean mutex_unlock(&priv->ptp_data.lock); 195661c77533SVladimir Oltean goto out; 195761c77533SVladimir Oltean } 19586cf99c13SVladimir Oltean 19596cf99c13SVladimir Oltean rc = __sja1105_ptp_settime(ds, 0, &ptp_sts_after); 196061c77533SVladimir Oltean if (rc < 0) { 196161c77533SVladimir Oltean mutex_unlock(&priv->ptp_data.lock); 196261c77533SVladimir Oltean goto out; 196361c77533SVladimir Oltean } 19646cf99c13SVladimir Oltean 19656cf99c13SVladimir Oltean t1 = timespec64_to_ns(&ptp_sts_before.pre_ts); 19666cf99c13SVladimir Oltean t2 = timespec64_to_ns(&ptp_sts_before.post_ts); 19676cf99c13SVladimir Oltean t3 = timespec64_to_ns(&ptp_sts_after.pre_ts); 19686cf99c13SVladimir Oltean t4 = timespec64_to_ns(&ptp_sts_after.post_ts); 19696cf99c13SVladimir Oltean /* Mid point, corresponds to pre-reset PTPCLKVAL */ 19706cf99c13SVladimir Oltean t12 = t1 + (t2 - t1) / 2; 19716cf99c13SVladimir Oltean /* Mid point, corresponds to post-reset PTPCLKVAL, aka 0 */ 19726cf99c13SVladimir Oltean t34 = t3 + (t4 - t3) / 2; 19736cf99c13SVladimir Oltean /* Advance PTPCLKVAL by the time it took since its readout */ 19746cf99c13SVladimir Oltean now += (t34 - t12); 19756cf99c13SVladimir Oltean 19766cf99c13SVladimir Oltean __sja1105_ptp_adjtime(ds, now); 19776cf99c13SVladimir Oltean 19786cf99c13SVladimir Oltean mutex_unlock(&priv->ptp_data.lock); 19796666cebcSVladimir Oltean 19802eea1fa8SVladimir Oltean dev_info(priv->ds->dev, 19812eea1fa8SVladimir Oltean "Reset switch and programmed static config. Reason: %s\n", 19822eea1fa8SVladimir Oltean sja1105_reset_reasons[reason]); 19832eea1fa8SVladimir Oltean 19846666cebcSVladimir Oltean /* Configure the CGU (PLLs) for MII and RMII PHYs. 19856666cebcSVladimir Oltean * For these interfaces there is no dynamic configuration 19866666cebcSVladimir Oltean * needed, since PLLs have same settings at all speeds. 19876666cebcSVladimir Oltean */ 1988cb5a82d2SVladimir Oltean if (priv->info->clocking_setup) { 1989c5037678SVladimir Oltean rc = priv->info->clocking_setup(priv); 19906666cebcSVladimir Oltean if (rc < 0) 19916666cebcSVladimir Oltean goto out; 1992cb5a82d2SVladimir Oltean } 19936666cebcSVladimir Oltean 1994542043e9SVladimir Oltean for (i = 0; i < ds->num_ports; i++) { 19953ad1d171SVladimir Oltean struct dw_xpcs *xpcs = priv->xpcs[i]; 19963ad1d171SVladimir Oltean unsigned int mode; 199784db00f2SVladimir Oltean 19988400cff6SVladimir Oltean rc = sja1105_adjust_port_config(priv, i, speed_mbps[i]); 19996666cebcSVladimir Oltean if (rc < 0) 20006666cebcSVladimir Oltean goto out; 2001ffe10e67SVladimir Oltean 20023ad1d171SVladimir Oltean if (!xpcs) 200384db00f2SVladimir Oltean continue; 2004ffe10e67SVladimir Oltean 20053ad1d171SVladimir Oltean if (bmcr[i] & BMCR_ANENABLE) 20063ad1d171SVladimir Oltean mode = MLO_AN_INBAND; 20073ad1d171SVladimir Oltean else if (priv->fixed_link[i]) 20083ad1d171SVladimir Oltean mode = MLO_AN_FIXED; 20093ad1d171SVladimir Oltean else 20103ad1d171SVladimir Oltean mode = MLO_AN_PHY; 201184db00f2SVladimir Oltean 20123ad1d171SVladimir Oltean rc = xpcs_do_config(xpcs, priv->phy_mode[i], mode); 20133ad1d171SVladimir Oltean if (rc < 0) 20143ad1d171SVladimir Oltean goto out; 2015ffe10e67SVladimir Oltean 20163ad1d171SVladimir Oltean if (!phylink_autoneg_inband(mode)) { 2017ffe10e67SVladimir Oltean int speed = SPEED_UNKNOWN; 2018ffe10e67SVladimir Oltean 201956b63466SVladimir Oltean if (priv->phy_mode[i] == PHY_INTERFACE_MODE_2500BASEX) 202056b63466SVladimir Oltean speed = SPEED_2500; 202156b63466SVladimir Oltean else if (bmcr[i] & BMCR_SPEED1000) 2022ffe10e67SVladimir Oltean speed = SPEED_1000; 202384db00f2SVladimir Oltean else if (bmcr[i] & BMCR_SPEED100) 2024ffe10e67SVladimir Oltean speed = SPEED_100; 2025053d8ad1SVladimir Oltean else 2026ffe10e67SVladimir Oltean speed = SPEED_10; 2027ffe10e67SVladimir Oltean 20283ad1d171SVladimir Oltean xpcs_link_up(&xpcs->pcs, mode, priv->phy_mode[i], 20293ad1d171SVladimir Oltean speed, DUPLEX_FULL); 2030ffe10e67SVladimir Oltean } 2031ffe10e67SVladimir Oltean } 20324d752508SVladimir Oltean 20334d752508SVladimir Oltean rc = sja1105_reload_cbs(priv); 20344d752508SVladimir Oltean if (rc < 0) 20354d752508SVladimir Oltean goto out; 20366666cebcSVladimir Oltean out: 2037af580ae2SVladimir Oltean mutex_unlock(&priv->mgmt_lock); 2038af580ae2SVladimir Oltean 20396666cebcSVladimir Oltean return rc; 20406666cebcSVladimir Oltean } 20416666cebcSVladimir Oltean 20426666cebcSVladimir Oltean static int sja1105_pvid_apply(struct sja1105_private *priv, int port, u16 pvid) 20436666cebcSVladimir Oltean { 20446666cebcSVladimir Oltean struct sja1105_mac_config_entry *mac; 20456666cebcSVladimir Oltean 20466666cebcSVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 20476666cebcSVladimir Oltean 20486666cebcSVladimir Oltean mac[port].vlanid = pvid; 20496666cebcSVladimir Oltean 20506666cebcSVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, 20516666cebcSVladimir Oltean &mac[port], true); 20526666cebcSVladimir Oltean } 20536666cebcSVladimir Oltean 2054ac02a451SVladimir Oltean static int sja1105_crosschip_bridge_join(struct dsa_switch *ds, 2055ac02a451SVladimir Oltean int tree_index, int sw_index, 2056ac02a451SVladimir Oltean int other_port, struct net_device *br) 2057ac02a451SVladimir Oltean { 2058ac02a451SVladimir Oltean struct dsa_switch *other_ds = dsa_switch_find(tree_index, sw_index); 2059ac02a451SVladimir Oltean struct sja1105_private *other_priv = other_ds->priv; 2060ac02a451SVladimir Oltean struct sja1105_private *priv = ds->priv; 2061ac02a451SVladimir Oltean int port, rc; 2062ac02a451SVladimir Oltean 2063ac02a451SVladimir Oltean if (other_ds->ops != &sja1105_switch_ops) 2064ac02a451SVladimir Oltean return 0; 2065ac02a451SVladimir Oltean 2066ac02a451SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 2067ac02a451SVladimir Oltean if (!dsa_is_user_port(ds, port)) 2068ac02a451SVladimir Oltean continue; 2069ac02a451SVladimir Oltean if (dsa_to_port(ds, port)->bridge_dev != br) 2070ac02a451SVladimir Oltean continue; 2071ac02a451SVladimir Oltean 20725899ee36SVladimir Oltean rc = dsa_8021q_crosschip_bridge_join(priv->dsa_8021q_ctx, 20735899ee36SVladimir Oltean port, 20745899ee36SVladimir Oltean other_priv->dsa_8021q_ctx, 20755899ee36SVladimir Oltean other_port); 2076ac02a451SVladimir Oltean if (rc) 2077ac02a451SVladimir Oltean return rc; 2078ac02a451SVladimir Oltean 20795899ee36SVladimir Oltean rc = dsa_8021q_crosschip_bridge_join(other_priv->dsa_8021q_ctx, 20805899ee36SVladimir Oltean other_port, 20815899ee36SVladimir Oltean priv->dsa_8021q_ctx, 20825899ee36SVladimir Oltean port); 2083ac02a451SVladimir Oltean if (rc) 2084ac02a451SVladimir Oltean return rc; 2085ac02a451SVladimir Oltean } 2086ac02a451SVladimir Oltean 2087ac02a451SVladimir Oltean return 0; 2088ac02a451SVladimir Oltean } 2089ac02a451SVladimir Oltean 2090ac02a451SVladimir Oltean static void sja1105_crosschip_bridge_leave(struct dsa_switch *ds, 2091ac02a451SVladimir Oltean int tree_index, int sw_index, 2092ac02a451SVladimir Oltean int other_port, 2093ac02a451SVladimir Oltean struct net_device *br) 2094ac02a451SVladimir Oltean { 2095ac02a451SVladimir Oltean struct dsa_switch *other_ds = dsa_switch_find(tree_index, sw_index); 2096ac02a451SVladimir Oltean struct sja1105_private *other_priv = other_ds->priv; 2097ac02a451SVladimir Oltean struct sja1105_private *priv = ds->priv; 2098ac02a451SVladimir Oltean int port; 2099ac02a451SVladimir Oltean 2100ac02a451SVladimir Oltean if (other_ds->ops != &sja1105_switch_ops) 2101ac02a451SVladimir Oltean return; 2102ac02a451SVladimir Oltean 2103ac02a451SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 2104ac02a451SVladimir Oltean if (!dsa_is_user_port(ds, port)) 2105ac02a451SVladimir Oltean continue; 2106ac02a451SVladimir Oltean if (dsa_to_port(ds, port)->bridge_dev != br) 2107ac02a451SVladimir Oltean continue; 2108ac02a451SVladimir Oltean 21095899ee36SVladimir Oltean dsa_8021q_crosschip_bridge_leave(priv->dsa_8021q_ctx, port, 21105899ee36SVladimir Oltean other_priv->dsa_8021q_ctx, 21115899ee36SVladimir Oltean other_port); 2112ac02a451SVladimir Oltean 21135899ee36SVladimir Oltean dsa_8021q_crosschip_bridge_leave(other_priv->dsa_8021q_ctx, 21145899ee36SVladimir Oltean other_port, 21155899ee36SVladimir Oltean priv->dsa_8021q_ctx, port); 2116ac02a451SVladimir Oltean } 2117ac02a451SVladimir Oltean } 2118ac02a451SVladimir Oltean 2119227d07a0SVladimir Oltean static int sja1105_setup_8021q_tagging(struct dsa_switch *ds, bool enabled) 2120227d07a0SVladimir Oltean { 212160b33aebSVladimir Oltean struct sja1105_private *priv = ds->priv; 21227e092af2SVladimir Oltean int rc; 2123227d07a0SVladimir Oltean 21245899ee36SVladimir Oltean rc = dsa_8021q_setup(priv->dsa_8021q_ctx, enabled); 21257e092af2SVladimir Oltean if (rc) 2126227d07a0SVladimir Oltean return rc; 2127ac02a451SVladimir Oltean 2128227d07a0SVladimir Oltean dev_info(ds->dev, "%s switch tagging\n", 2129227d07a0SVladimir Oltean enabled ? "Enabled" : "Disabled"); 2130227d07a0SVladimir Oltean return 0; 2131227d07a0SVladimir Oltean } 2132227d07a0SVladimir Oltean 21338aa9ebccSVladimir Oltean static enum dsa_tag_protocol 21344d776482SFlorian Fainelli sja1105_get_tag_protocol(struct dsa_switch *ds, int port, 21354d776482SFlorian Fainelli enum dsa_tag_protocol mp) 21368aa9ebccSVladimir Oltean { 21374913b8ebSVladimir Oltean struct sja1105_private *priv = ds->priv; 21384913b8ebSVladimir Oltean 21394913b8ebSVladimir Oltean return priv->info->tag_proto; 21408aa9ebccSVladimir Oltean } 21418aa9ebccSVladimir Oltean 21423f01c91aSVladimir Oltean static int sja1105_find_free_subvlan(u16 *subvlan_map, bool pvid) 21433f01c91aSVladimir Oltean { 21443f01c91aSVladimir Oltean int subvlan; 21453f01c91aSVladimir Oltean 21463f01c91aSVladimir Oltean if (pvid) 21473f01c91aSVladimir Oltean return 0; 21483f01c91aSVladimir Oltean 21493f01c91aSVladimir Oltean for (subvlan = 1; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++) 21503f01c91aSVladimir Oltean if (subvlan_map[subvlan] == VLAN_N_VID) 21513f01c91aSVladimir Oltean return subvlan; 21523f01c91aSVladimir Oltean 21533f01c91aSVladimir Oltean return -1; 21543f01c91aSVladimir Oltean } 21553f01c91aSVladimir Oltean 21563f01c91aSVladimir Oltean static int sja1105_find_subvlan(u16 *subvlan_map, u16 vid) 21573f01c91aSVladimir Oltean { 21583f01c91aSVladimir Oltean int subvlan; 21593f01c91aSVladimir Oltean 21603f01c91aSVladimir Oltean for (subvlan = 0; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++) 21613f01c91aSVladimir Oltean if (subvlan_map[subvlan] == vid) 21623f01c91aSVladimir Oltean return subvlan; 21633f01c91aSVladimir Oltean 21643f01c91aSVladimir Oltean return -1; 21653f01c91aSVladimir Oltean } 21663f01c91aSVladimir Oltean 21673f01c91aSVladimir Oltean static int sja1105_find_committed_subvlan(struct sja1105_private *priv, 21683f01c91aSVladimir Oltean int port, u16 vid) 21693f01c91aSVladimir Oltean { 21703f01c91aSVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 21713f01c91aSVladimir Oltean 21723f01c91aSVladimir Oltean return sja1105_find_subvlan(sp->subvlan_map, vid); 21733f01c91aSVladimir Oltean } 21743f01c91aSVladimir Oltean 21753f01c91aSVladimir Oltean static void sja1105_init_subvlan_map(u16 *subvlan_map) 21763f01c91aSVladimir Oltean { 21773f01c91aSVladimir Oltean int subvlan; 21783f01c91aSVladimir Oltean 21793f01c91aSVladimir Oltean for (subvlan = 0; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++) 21803f01c91aSVladimir Oltean subvlan_map[subvlan] = VLAN_N_VID; 21813f01c91aSVladimir Oltean } 21823f01c91aSVladimir Oltean 21833f01c91aSVladimir Oltean static void sja1105_commit_subvlan_map(struct sja1105_private *priv, int port, 21843f01c91aSVladimir Oltean u16 *subvlan_map) 21853f01c91aSVladimir Oltean { 21863f01c91aSVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 21873f01c91aSVladimir Oltean int subvlan; 21883f01c91aSVladimir Oltean 21893f01c91aSVladimir Oltean for (subvlan = 0; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++) 21903f01c91aSVladimir Oltean sp->subvlan_map[subvlan] = subvlan_map[subvlan]; 21913f01c91aSVladimir Oltean } 21923f01c91aSVladimir Oltean 2193ec5ae610SVladimir Oltean static int sja1105_is_vlan_configured(struct sja1105_private *priv, u16 vid) 2194ec5ae610SVladimir Oltean { 2195ec5ae610SVladimir Oltean struct sja1105_vlan_lookup_entry *vlan; 2196ec5ae610SVladimir Oltean int count, i; 2197ec5ae610SVladimir Oltean 2198ec5ae610SVladimir Oltean vlan = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entries; 2199ec5ae610SVladimir Oltean count = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entry_count; 2200ec5ae610SVladimir Oltean 2201ec5ae610SVladimir Oltean for (i = 0; i < count; i++) 2202ec5ae610SVladimir Oltean if (vlan[i].vlanid == vid) 2203ec5ae610SVladimir Oltean return i; 2204ec5ae610SVladimir Oltean 2205ec5ae610SVladimir Oltean /* Return an invalid entry index if not found */ 2206ec5ae610SVladimir Oltean return -1; 2207ec5ae610SVladimir Oltean } 2208ec5ae610SVladimir Oltean 22093f01c91aSVladimir Oltean static int 22103f01c91aSVladimir Oltean sja1105_find_retagging_entry(struct sja1105_retagging_entry *retagging, 22113f01c91aSVladimir Oltean int count, int from_port, u16 from_vid, 22123f01c91aSVladimir Oltean u16 to_vid) 2213ec5ae610SVladimir Oltean { 22143f01c91aSVladimir Oltean int i; 22153f01c91aSVladimir Oltean 22163f01c91aSVladimir Oltean for (i = 0; i < count; i++) 22173f01c91aSVladimir Oltean if (retagging[i].ing_port == BIT(from_port) && 22183f01c91aSVladimir Oltean retagging[i].vlan_ing == from_vid && 22193f01c91aSVladimir Oltean retagging[i].vlan_egr == to_vid) 22203f01c91aSVladimir Oltean return i; 22213f01c91aSVladimir Oltean 22223f01c91aSVladimir Oltean /* Return an invalid entry index if not found */ 22233f01c91aSVladimir Oltean return -1; 22243f01c91aSVladimir Oltean } 22253f01c91aSVladimir Oltean 22263f01c91aSVladimir Oltean static int sja1105_commit_vlans(struct sja1105_private *priv, 22273f01c91aSVladimir Oltean struct sja1105_vlan_lookup_entry *new_vlan, 22283f01c91aSVladimir Oltean struct sja1105_retagging_entry *new_retagging, 22293f01c91aSVladimir Oltean int num_retagging) 22303f01c91aSVladimir Oltean { 22313f01c91aSVladimir Oltean struct sja1105_retagging_entry *retagging; 2232ec5ae610SVladimir Oltean struct sja1105_vlan_lookup_entry *vlan; 2233ec5ae610SVladimir Oltean struct sja1105_table *table; 2234ec5ae610SVladimir Oltean int num_vlans = 0; 2235ec5ae610SVladimir Oltean int rc, i, k = 0; 2236ec5ae610SVladimir Oltean 2237ec5ae610SVladimir Oltean /* VLAN table */ 2238ec5ae610SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP]; 2239ec5ae610SVladimir Oltean vlan = table->entries; 2240ec5ae610SVladimir Oltean 2241ec5ae610SVladimir Oltean for (i = 0; i < VLAN_N_VID; i++) { 2242ec5ae610SVladimir Oltean int match = sja1105_is_vlan_configured(priv, i); 2243ec5ae610SVladimir Oltean 2244ec5ae610SVladimir Oltean if (new_vlan[i].vlanid != VLAN_N_VID) 2245ec5ae610SVladimir Oltean num_vlans++; 2246ec5ae610SVladimir Oltean 2247ec5ae610SVladimir Oltean if (new_vlan[i].vlanid == VLAN_N_VID && match >= 0) { 2248ec5ae610SVladimir Oltean /* Was there before, no longer is. Delete */ 2249ec5ae610SVladimir Oltean dev_dbg(priv->ds->dev, "Deleting VLAN %d\n", i); 2250ec5ae610SVladimir Oltean rc = sja1105_dynamic_config_write(priv, 2251ec5ae610SVladimir Oltean BLK_IDX_VLAN_LOOKUP, 2252ec5ae610SVladimir Oltean i, &vlan[match], false); 2253ec5ae610SVladimir Oltean if (rc < 0) 2254ec5ae610SVladimir Oltean return rc; 2255ec5ae610SVladimir Oltean } else if (new_vlan[i].vlanid != VLAN_N_VID) { 2256ec5ae610SVladimir Oltean /* Nothing changed, don't do anything */ 2257ec5ae610SVladimir Oltean if (match >= 0 && 2258ec5ae610SVladimir Oltean vlan[match].vlanid == new_vlan[i].vlanid && 2259ec5ae610SVladimir Oltean vlan[match].tag_port == new_vlan[i].tag_port && 2260ec5ae610SVladimir Oltean vlan[match].vlan_bc == new_vlan[i].vlan_bc && 2261ec5ae610SVladimir Oltean vlan[match].vmemb_port == new_vlan[i].vmemb_port) 2262ec5ae610SVladimir Oltean continue; 2263ec5ae610SVladimir Oltean /* Update entry */ 2264ec5ae610SVladimir Oltean dev_dbg(priv->ds->dev, "Updating VLAN %d\n", i); 2265ec5ae610SVladimir Oltean rc = sja1105_dynamic_config_write(priv, 2266ec5ae610SVladimir Oltean BLK_IDX_VLAN_LOOKUP, 2267ec5ae610SVladimir Oltean i, &new_vlan[i], 2268ec5ae610SVladimir Oltean true); 2269ec5ae610SVladimir Oltean if (rc < 0) 2270ec5ae610SVladimir Oltean return rc; 2271ec5ae610SVladimir Oltean } 2272ec5ae610SVladimir Oltean } 2273ec5ae610SVladimir Oltean 2274ec5ae610SVladimir Oltean if (table->entry_count) 2275ec5ae610SVladimir Oltean kfree(table->entries); 2276ec5ae610SVladimir Oltean 2277ec5ae610SVladimir Oltean table->entries = kcalloc(num_vlans, table->ops->unpacked_entry_size, 2278ec5ae610SVladimir Oltean GFP_KERNEL); 2279ec5ae610SVladimir Oltean if (!table->entries) 2280ec5ae610SVladimir Oltean return -ENOMEM; 2281ec5ae610SVladimir Oltean 2282ec5ae610SVladimir Oltean table->entry_count = num_vlans; 2283ec5ae610SVladimir Oltean vlan = table->entries; 2284ec5ae610SVladimir Oltean 2285ec5ae610SVladimir Oltean for (i = 0; i < VLAN_N_VID; i++) { 2286ec5ae610SVladimir Oltean if (new_vlan[i].vlanid == VLAN_N_VID) 2287ec5ae610SVladimir Oltean continue; 2288ec5ae610SVladimir Oltean vlan[k++] = new_vlan[i]; 2289ec5ae610SVladimir Oltean } 2290ec5ae610SVladimir Oltean 22913f01c91aSVladimir Oltean /* VLAN Retagging Table */ 22923f01c91aSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_RETAGGING]; 22933f01c91aSVladimir Oltean retagging = table->entries; 22943f01c91aSVladimir Oltean 22953f01c91aSVladimir Oltean for (i = 0; i < table->entry_count; i++) { 22963f01c91aSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_RETAGGING, 22973f01c91aSVladimir Oltean i, &retagging[i], false); 22983f01c91aSVladimir Oltean if (rc) 22993f01c91aSVladimir Oltean return rc; 23003f01c91aSVladimir Oltean } 23013f01c91aSVladimir Oltean 23023f01c91aSVladimir Oltean if (table->entry_count) 23033f01c91aSVladimir Oltean kfree(table->entries); 23043f01c91aSVladimir Oltean 23053f01c91aSVladimir Oltean table->entries = kcalloc(num_retagging, table->ops->unpacked_entry_size, 23063f01c91aSVladimir Oltean GFP_KERNEL); 23073f01c91aSVladimir Oltean if (!table->entries) 23083f01c91aSVladimir Oltean return -ENOMEM; 23093f01c91aSVladimir Oltean 23103f01c91aSVladimir Oltean table->entry_count = num_retagging; 23113f01c91aSVladimir Oltean retagging = table->entries; 23123f01c91aSVladimir Oltean 23133f01c91aSVladimir Oltean for (i = 0; i < num_retagging; i++) { 23143f01c91aSVladimir Oltean retagging[i] = new_retagging[i]; 23153f01c91aSVladimir Oltean 23163f01c91aSVladimir Oltean /* Update entry */ 23173f01c91aSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_RETAGGING, 23183f01c91aSVladimir Oltean i, &retagging[i], true); 23193f01c91aSVladimir Oltean if (rc < 0) 23203f01c91aSVladimir Oltean return rc; 23213f01c91aSVladimir Oltean } 23223f01c91aSVladimir Oltean 2323ec5ae610SVladimir Oltean return 0; 2324ec5ae610SVladimir Oltean } 2325ec5ae610SVladimir Oltean 23263f01c91aSVladimir Oltean struct sja1105_crosschip_vlan { 23273f01c91aSVladimir Oltean struct list_head list; 23283f01c91aSVladimir Oltean u16 vid; 23293f01c91aSVladimir Oltean bool untagged; 23303f01c91aSVladimir Oltean int port; 23313f01c91aSVladimir Oltean int other_port; 23325899ee36SVladimir Oltean struct dsa_8021q_context *other_ctx; 23333f01c91aSVladimir Oltean }; 23343f01c91aSVladimir Oltean 2335ec5ae610SVladimir Oltean struct sja1105_crosschip_switch { 2336ec5ae610SVladimir Oltean struct list_head list; 23375899ee36SVladimir Oltean struct dsa_8021q_context *other_ctx; 2338ec5ae610SVladimir Oltean }; 2339ec5ae610SVladimir Oltean 2340ec5ae610SVladimir Oltean static int sja1105_commit_pvid(struct sja1105_private *priv) 2341ec5ae610SVladimir Oltean { 2342ec5ae610SVladimir Oltean struct sja1105_bridge_vlan *v; 2343ec5ae610SVladimir Oltean struct list_head *vlan_list; 2344ec5ae610SVladimir Oltean int rc = 0; 2345ec5ae610SVladimir Oltean 2346ec5ae610SVladimir Oltean if (priv->vlan_state == SJA1105_VLAN_FILTERING_FULL) 2347ec5ae610SVladimir Oltean vlan_list = &priv->bridge_vlans; 2348ec5ae610SVladimir Oltean else 2349ec5ae610SVladimir Oltean vlan_list = &priv->dsa_8021q_vlans; 2350ec5ae610SVladimir Oltean 2351ec5ae610SVladimir Oltean list_for_each_entry(v, vlan_list, list) { 2352ec5ae610SVladimir Oltean if (v->pvid) { 2353ec5ae610SVladimir Oltean rc = sja1105_pvid_apply(priv, v->port, v->vid); 2354ec5ae610SVladimir Oltean if (rc) 2355ec5ae610SVladimir Oltean break; 2356ec5ae610SVladimir Oltean } 2357ec5ae610SVladimir Oltean } 2358ec5ae610SVladimir Oltean 2359ec5ae610SVladimir Oltean return rc; 2360ec5ae610SVladimir Oltean } 2361ec5ae610SVladimir Oltean 2362ec5ae610SVladimir Oltean static int 2363ec5ae610SVladimir Oltean sja1105_build_bridge_vlans(struct sja1105_private *priv, 2364ec5ae610SVladimir Oltean struct sja1105_vlan_lookup_entry *new_vlan) 2365ec5ae610SVladimir Oltean { 2366ec5ae610SVladimir Oltean struct sja1105_bridge_vlan *v; 2367ec5ae610SVladimir Oltean 2368ec5ae610SVladimir Oltean if (priv->vlan_state == SJA1105_VLAN_UNAWARE) 2369ec5ae610SVladimir Oltean return 0; 2370ec5ae610SVladimir Oltean 2371ec5ae610SVladimir Oltean list_for_each_entry(v, &priv->bridge_vlans, list) { 2372ec5ae610SVladimir Oltean int match = v->vid; 2373ec5ae610SVladimir Oltean 2374ec5ae610SVladimir Oltean new_vlan[match].vlanid = v->vid; 2375ec5ae610SVladimir Oltean new_vlan[match].vmemb_port |= BIT(v->port); 2376ec5ae610SVladimir Oltean new_vlan[match].vlan_bc |= BIT(v->port); 2377ec5ae610SVladimir Oltean if (!v->untagged) 2378ec5ae610SVladimir Oltean new_vlan[match].tag_port |= BIT(v->port); 23793e77e59bSVladimir Oltean new_vlan[match].type_entry = SJA1110_VLAN_D_TAG; 2380ec5ae610SVladimir Oltean } 2381ec5ae610SVladimir Oltean 2382ec5ae610SVladimir Oltean return 0; 2383ec5ae610SVladimir Oltean } 2384ec5ae610SVladimir Oltean 2385ec5ae610SVladimir Oltean static int 2386ec5ae610SVladimir Oltean sja1105_build_dsa_8021q_vlans(struct sja1105_private *priv, 2387ec5ae610SVladimir Oltean struct sja1105_vlan_lookup_entry *new_vlan) 2388ec5ae610SVladimir Oltean { 2389ec5ae610SVladimir Oltean struct sja1105_bridge_vlan *v; 2390ec5ae610SVladimir Oltean 2391ec5ae610SVladimir Oltean if (priv->vlan_state == SJA1105_VLAN_FILTERING_FULL) 2392ec5ae610SVladimir Oltean return 0; 2393ec5ae610SVladimir Oltean 2394ec5ae610SVladimir Oltean list_for_each_entry(v, &priv->dsa_8021q_vlans, list) { 2395ec5ae610SVladimir Oltean int match = v->vid; 2396ec5ae610SVladimir Oltean 2397ec5ae610SVladimir Oltean new_vlan[match].vlanid = v->vid; 2398ec5ae610SVladimir Oltean new_vlan[match].vmemb_port |= BIT(v->port); 2399ec5ae610SVladimir Oltean new_vlan[match].vlan_bc |= BIT(v->port); 2400ec5ae610SVladimir Oltean if (!v->untagged) 2401ec5ae610SVladimir Oltean new_vlan[match].tag_port |= BIT(v->port); 24023e77e59bSVladimir Oltean new_vlan[match].type_entry = SJA1110_VLAN_D_TAG; 2403ec5ae610SVladimir Oltean } 2404ec5ae610SVladimir Oltean 2405ec5ae610SVladimir Oltean return 0; 2406ec5ae610SVladimir Oltean } 2407ec5ae610SVladimir Oltean 24083f01c91aSVladimir Oltean static int sja1105_build_subvlans(struct sja1105_private *priv, 24093f01c91aSVladimir Oltean u16 subvlan_map[][DSA_8021Q_N_SUBVLAN], 24103f01c91aSVladimir Oltean struct sja1105_vlan_lookup_entry *new_vlan, 24113f01c91aSVladimir Oltean struct sja1105_retagging_entry *new_retagging, 24123f01c91aSVladimir Oltean int *num_retagging) 24133f01c91aSVladimir Oltean { 24143f01c91aSVladimir Oltean struct sja1105_bridge_vlan *v; 24153f01c91aSVladimir Oltean int k = *num_retagging; 24163f01c91aSVladimir Oltean 24173f01c91aSVladimir Oltean if (priv->vlan_state != SJA1105_VLAN_BEST_EFFORT) 24183f01c91aSVladimir Oltean return 0; 24193f01c91aSVladimir Oltean 24203f01c91aSVladimir Oltean list_for_each_entry(v, &priv->bridge_vlans, list) { 24213f01c91aSVladimir Oltean int upstream = dsa_upstream_port(priv->ds, v->port); 24223f01c91aSVladimir Oltean int match, subvlan; 24233f01c91aSVladimir Oltean u16 rx_vid; 24243f01c91aSVladimir Oltean 24253f01c91aSVladimir Oltean /* Only sub-VLANs on user ports need to be applied. 24263f01c91aSVladimir Oltean * Bridge VLANs also include VLANs added automatically 24273f01c91aSVladimir Oltean * by DSA on the CPU port. 24283f01c91aSVladimir Oltean */ 24293f01c91aSVladimir Oltean if (!dsa_is_user_port(priv->ds, v->port)) 24303f01c91aSVladimir Oltean continue; 24313f01c91aSVladimir Oltean 24323f01c91aSVladimir Oltean subvlan = sja1105_find_subvlan(subvlan_map[v->port], 24333f01c91aSVladimir Oltean v->vid); 24343f01c91aSVladimir Oltean if (subvlan < 0) { 24353f01c91aSVladimir Oltean subvlan = sja1105_find_free_subvlan(subvlan_map[v->port], 24363f01c91aSVladimir Oltean v->pvid); 24373f01c91aSVladimir Oltean if (subvlan < 0) { 24383f01c91aSVladimir Oltean dev_err(priv->ds->dev, "No more free subvlans\n"); 24393f01c91aSVladimir Oltean return -ENOSPC; 24403f01c91aSVladimir Oltean } 24413f01c91aSVladimir Oltean } 24423f01c91aSVladimir Oltean 24433f01c91aSVladimir Oltean rx_vid = dsa_8021q_rx_vid_subvlan(priv->ds, v->port, subvlan); 24443f01c91aSVladimir Oltean 24453f01c91aSVladimir Oltean /* @v->vid on @v->port needs to be retagged to @rx_vid 24463f01c91aSVladimir Oltean * on @upstream. Assume @v->vid on @v->port and on 24473f01c91aSVladimir Oltean * @upstream was already configured by the previous 24483f01c91aSVladimir Oltean * iteration over bridge_vlans. 24493f01c91aSVladimir Oltean */ 24503f01c91aSVladimir Oltean match = rx_vid; 24513f01c91aSVladimir Oltean new_vlan[match].vlanid = rx_vid; 24523f01c91aSVladimir Oltean new_vlan[match].vmemb_port |= BIT(v->port); 24533f01c91aSVladimir Oltean new_vlan[match].vmemb_port |= BIT(upstream); 24543f01c91aSVladimir Oltean new_vlan[match].vlan_bc |= BIT(v->port); 24553f01c91aSVladimir Oltean new_vlan[match].vlan_bc |= BIT(upstream); 24563f01c91aSVladimir Oltean /* The "untagged" flag is set the same as for the 24573f01c91aSVladimir Oltean * original VLAN 24583f01c91aSVladimir Oltean */ 24593f01c91aSVladimir Oltean if (!v->untagged) 24603f01c91aSVladimir Oltean new_vlan[match].tag_port |= BIT(v->port); 24613f01c91aSVladimir Oltean /* But it's always tagged towards the CPU */ 24623f01c91aSVladimir Oltean new_vlan[match].tag_port |= BIT(upstream); 24633e77e59bSVladimir Oltean new_vlan[match].type_entry = SJA1110_VLAN_D_TAG; 24643f01c91aSVladimir Oltean 24653f01c91aSVladimir Oltean /* The Retagging Table generates packet *clones* with 24663f01c91aSVladimir Oltean * the new VLAN. This is a very odd hardware quirk 24673f01c91aSVladimir Oltean * which we need to suppress by dropping the original 24683f01c91aSVladimir Oltean * packet. 24693f01c91aSVladimir Oltean * Deny egress of the original VLAN towards the CPU 24703f01c91aSVladimir Oltean * port. This will force the switch to drop it, and 24713f01c91aSVladimir Oltean * we'll see only the retagged packets. 24723f01c91aSVladimir Oltean */ 24733f01c91aSVladimir Oltean match = v->vid; 24743f01c91aSVladimir Oltean new_vlan[match].vlan_bc &= ~BIT(upstream); 24753f01c91aSVladimir Oltean 24763f01c91aSVladimir Oltean /* And the retagging itself */ 24773f01c91aSVladimir Oltean new_retagging[k].vlan_ing = v->vid; 24783f01c91aSVladimir Oltean new_retagging[k].vlan_egr = rx_vid; 24793f01c91aSVladimir Oltean new_retagging[k].ing_port = BIT(v->port); 24803f01c91aSVladimir Oltean new_retagging[k].egr_port = BIT(upstream); 24813f01c91aSVladimir Oltean if (k++ == SJA1105_MAX_RETAGGING_COUNT) { 24823f01c91aSVladimir Oltean dev_err(priv->ds->dev, "No more retagging rules\n"); 24833f01c91aSVladimir Oltean return -ENOSPC; 24843f01c91aSVladimir Oltean } 24853f01c91aSVladimir Oltean 24863f01c91aSVladimir Oltean subvlan_map[v->port][subvlan] = v->vid; 24873f01c91aSVladimir Oltean } 24883f01c91aSVladimir Oltean 24893f01c91aSVladimir Oltean *num_retagging = k; 24903f01c91aSVladimir Oltean 24913f01c91aSVladimir Oltean return 0; 24923f01c91aSVladimir Oltean } 24933f01c91aSVladimir Oltean 24943f01c91aSVladimir Oltean /* Sadly, in crosschip scenarios where the CPU port is also the link to another 24953f01c91aSVladimir Oltean * switch, we should retag backwards (the dsa_8021q vid to the original vid) on 24963f01c91aSVladimir Oltean * the CPU port of neighbour switches. 24973f01c91aSVladimir Oltean */ 24983f01c91aSVladimir Oltean static int 24993f01c91aSVladimir Oltean sja1105_build_crosschip_subvlans(struct sja1105_private *priv, 25003f01c91aSVladimir Oltean struct sja1105_vlan_lookup_entry *new_vlan, 25013f01c91aSVladimir Oltean struct sja1105_retagging_entry *new_retagging, 25023f01c91aSVladimir Oltean int *num_retagging) 25033f01c91aSVladimir Oltean { 25043f01c91aSVladimir Oltean struct sja1105_crosschip_vlan *tmp, *pos; 25053f01c91aSVladimir Oltean struct dsa_8021q_crosschip_link *c; 25063f01c91aSVladimir Oltean struct sja1105_bridge_vlan *v, *w; 25073f01c91aSVladimir Oltean struct list_head crosschip_vlans; 25083f01c91aSVladimir Oltean int k = *num_retagging; 25093f01c91aSVladimir Oltean int rc = 0; 25103f01c91aSVladimir Oltean 25113f01c91aSVladimir Oltean if (priv->vlan_state != SJA1105_VLAN_BEST_EFFORT) 25123f01c91aSVladimir Oltean return 0; 25133f01c91aSVladimir Oltean 25143f01c91aSVladimir Oltean INIT_LIST_HEAD(&crosschip_vlans); 25153f01c91aSVladimir Oltean 25165899ee36SVladimir Oltean list_for_each_entry(c, &priv->dsa_8021q_ctx->crosschip_links, list) { 25175899ee36SVladimir Oltean struct sja1105_private *other_priv = c->other_ctx->ds->priv; 25183f01c91aSVladimir Oltean 25193f01c91aSVladimir Oltean if (other_priv->vlan_state == SJA1105_VLAN_FILTERING_FULL) 25203f01c91aSVladimir Oltean continue; 25213f01c91aSVladimir Oltean 25223f01c91aSVladimir Oltean /* Crosschip links are also added to the CPU ports. 25233f01c91aSVladimir Oltean * Ignore those. 25243f01c91aSVladimir Oltean */ 25253f01c91aSVladimir Oltean if (!dsa_is_user_port(priv->ds, c->port)) 25263f01c91aSVladimir Oltean continue; 25275899ee36SVladimir Oltean if (!dsa_is_user_port(c->other_ctx->ds, c->other_port)) 25283f01c91aSVladimir Oltean continue; 25293f01c91aSVladimir Oltean 25303f01c91aSVladimir Oltean /* Search for VLANs on the remote port */ 25313f01c91aSVladimir Oltean list_for_each_entry(v, &other_priv->bridge_vlans, list) { 25323f01c91aSVladimir Oltean bool already_added = false; 25333f01c91aSVladimir Oltean bool we_have_it = false; 25343f01c91aSVladimir Oltean 25353f01c91aSVladimir Oltean if (v->port != c->other_port) 25363f01c91aSVladimir Oltean continue; 25373f01c91aSVladimir Oltean 25383f01c91aSVladimir Oltean /* If @v is a pvid on @other_ds, it does not need 25393f01c91aSVladimir Oltean * re-retagging, because its SVL field is 0 and we 25403f01c91aSVladimir Oltean * already allow that, via the dsa_8021q crosschip 25413f01c91aSVladimir Oltean * links. 25423f01c91aSVladimir Oltean */ 25433f01c91aSVladimir Oltean if (v->pvid) 25443f01c91aSVladimir Oltean continue; 25453f01c91aSVladimir Oltean 25463f01c91aSVladimir Oltean /* Search for the VLAN on our local port */ 25473f01c91aSVladimir Oltean list_for_each_entry(w, &priv->bridge_vlans, list) { 25483f01c91aSVladimir Oltean if (w->port == c->port && w->vid == v->vid) { 25493f01c91aSVladimir Oltean we_have_it = true; 25503f01c91aSVladimir Oltean break; 25513f01c91aSVladimir Oltean } 25523f01c91aSVladimir Oltean } 25533f01c91aSVladimir Oltean 25543f01c91aSVladimir Oltean if (!we_have_it) 25553f01c91aSVladimir Oltean continue; 25563f01c91aSVladimir Oltean 25573f01c91aSVladimir Oltean list_for_each_entry(tmp, &crosschip_vlans, list) { 25583f01c91aSVladimir Oltean if (tmp->vid == v->vid && 25593f01c91aSVladimir Oltean tmp->untagged == v->untagged && 25603f01c91aSVladimir Oltean tmp->port == c->port && 25613f01c91aSVladimir Oltean tmp->other_port == v->port && 25625899ee36SVladimir Oltean tmp->other_ctx == c->other_ctx) { 25633f01c91aSVladimir Oltean already_added = true; 25643f01c91aSVladimir Oltean break; 25653f01c91aSVladimir Oltean } 25663f01c91aSVladimir Oltean } 25673f01c91aSVladimir Oltean 25683f01c91aSVladimir Oltean if (already_added) 25693f01c91aSVladimir Oltean continue; 25703f01c91aSVladimir Oltean 25713f01c91aSVladimir Oltean tmp = kzalloc(sizeof(*tmp), GFP_KERNEL); 25723f01c91aSVladimir Oltean if (!tmp) { 25733f01c91aSVladimir Oltean dev_err(priv->ds->dev, "Failed to allocate memory\n"); 25743f01c91aSVladimir Oltean rc = -ENOMEM; 25753f01c91aSVladimir Oltean goto out; 25763f01c91aSVladimir Oltean } 25773f01c91aSVladimir Oltean tmp->vid = v->vid; 25783f01c91aSVladimir Oltean tmp->port = c->port; 25793f01c91aSVladimir Oltean tmp->other_port = v->port; 25805899ee36SVladimir Oltean tmp->other_ctx = c->other_ctx; 25813f01c91aSVladimir Oltean tmp->untagged = v->untagged; 25823f01c91aSVladimir Oltean list_add(&tmp->list, &crosschip_vlans); 25833f01c91aSVladimir Oltean } 25843f01c91aSVladimir Oltean } 25853f01c91aSVladimir Oltean 25863f01c91aSVladimir Oltean list_for_each_entry(tmp, &crosschip_vlans, list) { 25875899ee36SVladimir Oltean struct sja1105_private *other_priv = tmp->other_ctx->ds->priv; 25883f01c91aSVladimir Oltean int upstream = dsa_upstream_port(priv->ds, tmp->port); 25893f01c91aSVladimir Oltean int match, subvlan; 25903f01c91aSVladimir Oltean u16 rx_vid; 25913f01c91aSVladimir Oltean 25923f01c91aSVladimir Oltean subvlan = sja1105_find_committed_subvlan(other_priv, 25933f01c91aSVladimir Oltean tmp->other_port, 25943f01c91aSVladimir Oltean tmp->vid); 25953f01c91aSVladimir Oltean /* If this happens, it's a bug. The neighbour switch does not 25963f01c91aSVladimir Oltean * have a subvlan for tmp->vid on tmp->other_port, but it 25973f01c91aSVladimir Oltean * should, since we already checked for its vlan_state. 25983f01c91aSVladimir Oltean */ 25993f01c91aSVladimir Oltean if (WARN_ON(subvlan < 0)) { 26003f01c91aSVladimir Oltean rc = -EINVAL; 26013f01c91aSVladimir Oltean goto out; 26023f01c91aSVladimir Oltean } 26033f01c91aSVladimir Oltean 26045899ee36SVladimir Oltean rx_vid = dsa_8021q_rx_vid_subvlan(tmp->other_ctx->ds, 26053f01c91aSVladimir Oltean tmp->other_port, 26063f01c91aSVladimir Oltean subvlan); 26073f01c91aSVladimir Oltean 26083f01c91aSVladimir Oltean /* The @rx_vid retagged from @tmp->vid on 26093f01c91aSVladimir Oltean * {@tmp->other_ds, @tmp->other_port} needs to be 26103f01c91aSVladimir Oltean * re-retagged to @tmp->vid on the way back to us. 26113f01c91aSVladimir Oltean * 26123f01c91aSVladimir Oltean * Assume the original @tmp->vid is already configured 26133f01c91aSVladimir Oltean * on this local switch, otherwise we wouldn't be 26143f01c91aSVladimir Oltean * retagging its subvlan on the other switch in the 26153f01c91aSVladimir Oltean * first place. We just need to add a reverse retagging 26163f01c91aSVladimir Oltean * rule for @rx_vid and install @rx_vid on our ports. 26173f01c91aSVladimir Oltean */ 26183f01c91aSVladimir Oltean match = rx_vid; 26193f01c91aSVladimir Oltean new_vlan[match].vlanid = rx_vid; 26203f01c91aSVladimir Oltean new_vlan[match].vmemb_port |= BIT(tmp->port); 26213f01c91aSVladimir Oltean new_vlan[match].vmemb_port |= BIT(upstream); 26223f01c91aSVladimir Oltean /* The "untagged" flag is set the same as for the 26233f01c91aSVladimir Oltean * original VLAN. And towards the CPU, it doesn't 26243f01c91aSVladimir Oltean * really matter, because @rx_vid will only receive 26253f01c91aSVladimir Oltean * traffic on that port. For consistency with other dsa_8021q 26263f01c91aSVladimir Oltean * VLANs, we'll keep the CPU port tagged. 26273f01c91aSVladimir Oltean */ 26283f01c91aSVladimir Oltean if (!tmp->untagged) 26293f01c91aSVladimir Oltean new_vlan[match].tag_port |= BIT(tmp->port); 26303f01c91aSVladimir Oltean new_vlan[match].tag_port |= BIT(upstream); 26313e77e59bSVladimir Oltean new_vlan[match].type_entry = SJA1110_VLAN_D_TAG; 26323f01c91aSVladimir Oltean /* Deny egress of @rx_vid towards our front-panel port. 26333f01c91aSVladimir Oltean * This will force the switch to drop it, and we'll see 26343f01c91aSVladimir Oltean * only the re-retagged packets (having the original, 26353f01c91aSVladimir Oltean * pre-initial-retagging, VLAN @tmp->vid). 26363f01c91aSVladimir Oltean */ 26373f01c91aSVladimir Oltean new_vlan[match].vlan_bc &= ~BIT(tmp->port); 26383f01c91aSVladimir Oltean 26393f01c91aSVladimir Oltean /* On reverse retagging, the same ingress VLAN goes to multiple 26403f01c91aSVladimir Oltean * ports. So we have an opportunity to create composite rules 26413f01c91aSVladimir Oltean * to not waste the limited space in the retagging table. 26423f01c91aSVladimir Oltean */ 26433f01c91aSVladimir Oltean k = sja1105_find_retagging_entry(new_retagging, *num_retagging, 26443f01c91aSVladimir Oltean upstream, rx_vid, tmp->vid); 26453f01c91aSVladimir Oltean if (k < 0) { 26463f01c91aSVladimir Oltean if (*num_retagging == SJA1105_MAX_RETAGGING_COUNT) { 26473f01c91aSVladimir Oltean dev_err(priv->ds->dev, "No more retagging rules\n"); 26483f01c91aSVladimir Oltean rc = -ENOSPC; 26493f01c91aSVladimir Oltean goto out; 26503f01c91aSVladimir Oltean } 26513f01c91aSVladimir Oltean k = (*num_retagging)++; 26523f01c91aSVladimir Oltean } 26533f01c91aSVladimir Oltean /* And the retagging itself */ 26543f01c91aSVladimir Oltean new_retagging[k].vlan_ing = rx_vid; 26553f01c91aSVladimir Oltean new_retagging[k].vlan_egr = tmp->vid; 26563f01c91aSVladimir Oltean new_retagging[k].ing_port = BIT(upstream); 26573f01c91aSVladimir Oltean new_retagging[k].egr_port |= BIT(tmp->port); 26583f01c91aSVladimir Oltean } 26593f01c91aSVladimir Oltean 26603f01c91aSVladimir Oltean out: 26613f01c91aSVladimir Oltean list_for_each_entry_safe(tmp, pos, &crosschip_vlans, list) { 26623f01c91aSVladimir Oltean list_del(&tmp->list); 26633f01c91aSVladimir Oltean kfree(tmp); 26643f01c91aSVladimir Oltean } 26653f01c91aSVladimir Oltean 26663f01c91aSVladimir Oltean return rc; 26673f01c91aSVladimir Oltean } 26683f01c91aSVladimir Oltean 2669ec5ae610SVladimir Oltean static int sja1105_build_vlan_table(struct sja1105_private *priv, bool notify); 2670ec5ae610SVladimir Oltean 2671ec5ae610SVladimir Oltean static int sja1105_notify_crosschip_switches(struct sja1105_private *priv) 2672ec5ae610SVladimir Oltean { 2673ec5ae610SVladimir Oltean struct sja1105_crosschip_switch *s, *pos; 2674ec5ae610SVladimir Oltean struct list_head crosschip_switches; 2675ec5ae610SVladimir Oltean struct dsa_8021q_crosschip_link *c; 2676ec5ae610SVladimir Oltean int rc = 0; 2677ec5ae610SVladimir Oltean 2678ec5ae610SVladimir Oltean INIT_LIST_HEAD(&crosschip_switches); 2679ec5ae610SVladimir Oltean 26805899ee36SVladimir Oltean list_for_each_entry(c, &priv->dsa_8021q_ctx->crosschip_links, list) { 2681ec5ae610SVladimir Oltean bool already_added = false; 2682ec5ae610SVladimir Oltean 2683ec5ae610SVladimir Oltean list_for_each_entry(s, &crosschip_switches, list) { 26845899ee36SVladimir Oltean if (s->other_ctx == c->other_ctx) { 2685ec5ae610SVladimir Oltean already_added = true; 2686ec5ae610SVladimir Oltean break; 2687ec5ae610SVladimir Oltean } 2688ec5ae610SVladimir Oltean } 2689ec5ae610SVladimir Oltean 2690ec5ae610SVladimir Oltean if (already_added) 2691ec5ae610SVladimir Oltean continue; 2692ec5ae610SVladimir Oltean 2693ec5ae610SVladimir Oltean s = kzalloc(sizeof(*s), GFP_KERNEL); 2694ec5ae610SVladimir Oltean if (!s) { 2695ec5ae610SVladimir Oltean dev_err(priv->ds->dev, "Failed to allocate memory\n"); 2696ec5ae610SVladimir Oltean rc = -ENOMEM; 2697ec5ae610SVladimir Oltean goto out; 2698ec5ae610SVladimir Oltean } 26995899ee36SVladimir Oltean s->other_ctx = c->other_ctx; 2700ec5ae610SVladimir Oltean list_add(&s->list, &crosschip_switches); 2701ec5ae610SVladimir Oltean } 2702ec5ae610SVladimir Oltean 2703ec5ae610SVladimir Oltean list_for_each_entry(s, &crosschip_switches, list) { 27045899ee36SVladimir Oltean struct sja1105_private *other_priv = s->other_ctx->ds->priv; 2705ec5ae610SVladimir Oltean 2706ec5ae610SVladimir Oltean rc = sja1105_build_vlan_table(other_priv, false); 2707ec5ae610SVladimir Oltean if (rc) 2708ec5ae610SVladimir Oltean goto out; 2709ec5ae610SVladimir Oltean } 2710ec5ae610SVladimir Oltean 2711ec5ae610SVladimir Oltean out: 2712ec5ae610SVladimir Oltean list_for_each_entry_safe(s, pos, &crosschip_switches, list) { 2713ec5ae610SVladimir Oltean list_del(&s->list); 2714ec5ae610SVladimir Oltean kfree(s); 2715ec5ae610SVladimir Oltean } 2716ec5ae610SVladimir Oltean 2717ec5ae610SVladimir Oltean return rc; 2718ec5ae610SVladimir Oltean } 2719ec5ae610SVladimir Oltean 2720ec5ae610SVladimir Oltean static int sja1105_build_vlan_table(struct sja1105_private *priv, bool notify) 2721ec5ae610SVladimir Oltean { 272282760d7fSVladimir Oltean u16 subvlan_map[SJA1105_MAX_NUM_PORTS][DSA_8021Q_N_SUBVLAN]; 27233f01c91aSVladimir Oltean struct sja1105_retagging_entry *new_retagging; 2724ec5ae610SVladimir Oltean struct sja1105_vlan_lookup_entry *new_vlan; 2725ec5ae610SVladimir Oltean struct sja1105_table *table; 27263f01c91aSVladimir Oltean int i, num_retagging = 0; 2727ec5ae610SVladimir Oltean int rc; 2728ec5ae610SVladimir Oltean 2729ec5ae610SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP]; 2730ec5ae610SVladimir Oltean new_vlan = kcalloc(VLAN_N_VID, 2731ec5ae610SVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 2732ec5ae610SVladimir Oltean if (!new_vlan) 2733ec5ae610SVladimir Oltean return -ENOMEM; 2734ec5ae610SVladimir Oltean 27353f01c91aSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP]; 27363f01c91aSVladimir Oltean new_retagging = kcalloc(SJA1105_MAX_RETAGGING_COUNT, 27373f01c91aSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 27383f01c91aSVladimir Oltean if (!new_retagging) { 27393f01c91aSVladimir Oltean kfree(new_vlan); 27403f01c91aSVladimir Oltean return -ENOMEM; 27413f01c91aSVladimir Oltean } 27423f01c91aSVladimir Oltean 2743ec5ae610SVladimir Oltean for (i = 0; i < VLAN_N_VID; i++) 2744ec5ae610SVladimir Oltean new_vlan[i].vlanid = VLAN_N_VID; 2745ec5ae610SVladimir Oltean 27463f01c91aSVladimir Oltean for (i = 0; i < SJA1105_MAX_RETAGGING_COUNT; i++) 27473f01c91aSVladimir Oltean new_retagging[i].vlan_ing = VLAN_N_VID; 27483f01c91aSVladimir Oltean 27493f01c91aSVladimir Oltean for (i = 0; i < priv->ds->num_ports; i++) 27503f01c91aSVladimir Oltean sja1105_init_subvlan_map(subvlan_map[i]); 27513f01c91aSVladimir Oltean 2752ec5ae610SVladimir Oltean /* Bridge VLANs */ 2753ec5ae610SVladimir Oltean rc = sja1105_build_bridge_vlans(priv, new_vlan); 2754ec5ae610SVladimir Oltean if (rc) 2755ec5ae610SVladimir Oltean goto out; 2756ec5ae610SVladimir Oltean 2757ec5ae610SVladimir Oltean /* VLANs necessary for dsa_8021q operation, given to us by tag_8021q.c: 2758ec5ae610SVladimir Oltean * - RX VLANs 2759ec5ae610SVladimir Oltean * - TX VLANs 2760ec5ae610SVladimir Oltean * - Crosschip links 2761ec5ae610SVladimir Oltean */ 2762ec5ae610SVladimir Oltean rc = sja1105_build_dsa_8021q_vlans(priv, new_vlan); 2763ec5ae610SVladimir Oltean if (rc) 2764ec5ae610SVladimir Oltean goto out; 2765ec5ae610SVladimir Oltean 27663f01c91aSVladimir Oltean /* Private VLANs necessary for dsa_8021q operation, which we need to 27673f01c91aSVladimir Oltean * determine on our own: 27683f01c91aSVladimir Oltean * - Sub-VLANs 27693f01c91aSVladimir Oltean * - Sub-VLANs of crosschip switches 27703f01c91aSVladimir Oltean */ 27713f01c91aSVladimir Oltean rc = sja1105_build_subvlans(priv, subvlan_map, new_vlan, new_retagging, 27723f01c91aSVladimir Oltean &num_retagging); 27733f01c91aSVladimir Oltean if (rc) 27743f01c91aSVladimir Oltean goto out; 27753f01c91aSVladimir Oltean 27763f01c91aSVladimir Oltean rc = sja1105_build_crosschip_subvlans(priv, new_vlan, new_retagging, 27773f01c91aSVladimir Oltean &num_retagging); 27783f01c91aSVladimir Oltean if (rc) 27793f01c91aSVladimir Oltean goto out; 27803f01c91aSVladimir Oltean 27813f01c91aSVladimir Oltean rc = sja1105_commit_vlans(priv, new_vlan, new_retagging, num_retagging); 2782ec5ae610SVladimir Oltean if (rc) 2783ec5ae610SVladimir Oltean goto out; 2784ec5ae610SVladimir Oltean 2785ec5ae610SVladimir Oltean rc = sja1105_commit_pvid(priv); 2786ec5ae610SVladimir Oltean if (rc) 2787ec5ae610SVladimir Oltean goto out; 2788ec5ae610SVladimir Oltean 27893f01c91aSVladimir Oltean for (i = 0; i < priv->ds->num_ports; i++) 27903f01c91aSVladimir Oltean sja1105_commit_subvlan_map(priv, i, subvlan_map[i]); 27913f01c91aSVladimir Oltean 2792ec5ae610SVladimir Oltean if (notify) { 2793ec5ae610SVladimir Oltean rc = sja1105_notify_crosschip_switches(priv); 2794ec5ae610SVladimir Oltean if (rc) 2795ec5ae610SVladimir Oltean goto out; 2796ec5ae610SVladimir Oltean } 2797ec5ae610SVladimir Oltean 2798ec5ae610SVladimir Oltean out: 2799ec5ae610SVladimir Oltean kfree(new_vlan); 28003f01c91aSVladimir Oltean kfree(new_retagging); 2801ec5ae610SVladimir Oltean 2802ec5ae610SVladimir Oltean return rc; 2803ec5ae610SVladimir Oltean } 2804ec5ae610SVladimir Oltean 2805070ca3bbSVladimir Oltean /* The TPID setting belongs to the General Parameters table, 2806070ca3bbSVladimir Oltean * which can only be partially reconfigured at runtime (and not the TPID). 2807070ca3bbSVladimir Oltean * So a switch reset is required. 2808070ca3bbSVladimir Oltean */ 280989153ed6SVladimir Oltean int sja1105_vlan_filtering(struct dsa_switch *ds, int port, bool enabled, 281089153ed6SVladimir Oltean struct netlink_ext_ack *extack) 28116666cebcSVladimir Oltean { 28126d7c7d94SVladimir Oltean struct sja1105_l2_lookup_params_entry *l2_lookup_params; 2813070ca3bbSVladimir Oltean struct sja1105_general_params_entry *general_params; 28146666cebcSVladimir Oltean struct sja1105_private *priv = ds->priv; 28157f14937fSVladimir Oltean enum sja1105_vlan_state state; 2816070ca3bbSVladimir Oltean struct sja1105_table *table; 2817dfacc5a2SVladimir Oltean struct sja1105_rule *rule; 28182cafa72eSVladimir Oltean bool want_tagging; 2819070ca3bbSVladimir Oltean u16 tpid, tpid2; 28206666cebcSVladimir Oltean int rc; 28216666cebcSVladimir Oltean 2822dfacc5a2SVladimir Oltean list_for_each_entry(rule, &priv->flow_block.rules, list) { 2823dfacc5a2SVladimir Oltean if (rule->type == SJA1105_RULE_VL) { 282489153ed6SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 282589153ed6SVladimir Oltean "Cannot change VLAN filtering with active VL rules"); 2826dfacc5a2SVladimir Oltean return -EBUSY; 2827dfacc5a2SVladimir Oltean } 2828dfacc5a2SVladimir Oltean } 2829dfacc5a2SVladimir Oltean 2830070ca3bbSVladimir Oltean if (enabled) { 28316666cebcSVladimir Oltean /* Enable VLAN filtering. */ 283254fa49eeSVladimir Oltean tpid = ETH_P_8021Q; 283354fa49eeSVladimir Oltean tpid2 = ETH_P_8021AD; 2834070ca3bbSVladimir Oltean } else { 28356666cebcSVladimir Oltean /* Disable VLAN filtering. */ 2836070ca3bbSVladimir Oltean tpid = ETH_P_SJA1105; 2837070ca3bbSVladimir Oltean tpid2 = ETH_P_SJA1105; 2838070ca3bbSVladimir Oltean } 2839070ca3bbSVladimir Oltean 284038b5beeaSVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 284138b5beeaSVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 284238b5beeaSVladimir Oltean 284338b5beeaSVladimir Oltean if (enabled) 284438b5beeaSVladimir Oltean sp->xmit_tpid = priv->info->qinq_tpid; 284538b5beeaSVladimir Oltean else 284638b5beeaSVladimir Oltean sp->xmit_tpid = ETH_P_SJA1105; 284738b5beeaSVladimir Oltean } 284838b5beeaSVladimir Oltean 28497f14937fSVladimir Oltean if (!enabled) 28507f14937fSVladimir Oltean state = SJA1105_VLAN_UNAWARE; 28512cafa72eSVladimir Oltean else if (priv->best_effort_vlan_filtering) 28522cafa72eSVladimir Oltean state = SJA1105_VLAN_BEST_EFFORT; 28537f14937fSVladimir Oltean else 28547f14937fSVladimir Oltean state = SJA1105_VLAN_FILTERING_FULL; 28557f14937fSVladimir Oltean 2856cfa36b1fSVladimir Oltean if (priv->vlan_state == state) 2857cfa36b1fSVladimir Oltean return 0; 2858cfa36b1fSVladimir Oltean 28597f14937fSVladimir Oltean priv->vlan_state = state; 28602cafa72eSVladimir Oltean want_tagging = (state == SJA1105_VLAN_UNAWARE || 28612cafa72eSVladimir Oltean state == SJA1105_VLAN_BEST_EFFORT); 28627f14937fSVladimir Oltean 2863070ca3bbSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; 2864070ca3bbSVladimir Oltean general_params = table->entries; 2865f9a1a764SVladimir Oltean /* EtherType used to identify inner tagged (C-tag) VLAN traffic */ 286654fa49eeSVladimir Oltean general_params->tpid = tpid; 286754fa49eeSVladimir Oltean /* EtherType used to identify outer tagged (S-tag) VLAN traffic */ 2868070ca3bbSVladimir Oltean general_params->tpid2 = tpid2; 286942824463SVladimir Oltean /* When VLAN filtering is on, we need to at least be able to 287042824463SVladimir Oltean * decode management traffic through the "backup plan". 287142824463SVladimir Oltean */ 287242824463SVladimir Oltean general_params->incl_srcpt1 = enabled; 287342824463SVladimir Oltean general_params->incl_srcpt0 = enabled; 2874070ca3bbSVladimir Oltean 28752cafa72eSVladimir Oltean want_tagging = priv->best_effort_vlan_filtering || !enabled; 28762cafa72eSVladimir Oltean 28776d7c7d94SVladimir Oltean /* VLAN filtering => independent VLAN learning. 28782cafa72eSVladimir Oltean * No VLAN filtering (or best effort) => shared VLAN learning. 28796d7c7d94SVladimir Oltean * 28806d7c7d94SVladimir Oltean * In shared VLAN learning mode, untagged traffic still gets 28816d7c7d94SVladimir Oltean * pvid-tagged, and the FDB table gets populated with entries 28826d7c7d94SVladimir Oltean * containing the "real" (pvid or from VLAN tag) VLAN ID. 28836d7c7d94SVladimir Oltean * However the switch performs a masked L2 lookup in the FDB, 28846d7c7d94SVladimir Oltean * effectively only looking up a frame's DMAC (and not VID) for the 28856d7c7d94SVladimir Oltean * forwarding decision. 28866d7c7d94SVladimir Oltean * 28876d7c7d94SVladimir Oltean * This is extremely convenient for us, because in modes with 28886d7c7d94SVladimir Oltean * vlan_filtering=0, dsa_8021q actually installs unique pvid's into 28896d7c7d94SVladimir Oltean * each front panel port. This is good for identification but breaks 28906d7c7d94SVladimir Oltean * learning badly - the VID of the learnt FDB entry is unique, aka 28916d7c7d94SVladimir Oltean * no frames coming from any other port are going to have it. So 28926d7c7d94SVladimir Oltean * for forwarding purposes, this is as though learning was broken 28936d7c7d94SVladimir Oltean * (all frames get flooded). 28946d7c7d94SVladimir Oltean */ 28956d7c7d94SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS]; 28966d7c7d94SVladimir Oltean l2_lookup_params = table->entries; 28972cafa72eSVladimir Oltean l2_lookup_params->shared_learn = want_tagging; 28986d7c7d94SVladimir Oltean 2899aaa270c6SVladimir Oltean sja1105_frame_memory_partitioning(priv); 2900aaa270c6SVladimir Oltean 2901aef31718SVladimir Oltean rc = sja1105_build_vlan_table(priv, false); 2902aef31718SVladimir Oltean if (rc) 2903aef31718SVladimir Oltean return rc; 2904aef31718SVladimir Oltean 29052eea1fa8SVladimir Oltean rc = sja1105_static_config_reload(priv, SJA1105_VLAN_FILTERING); 29066666cebcSVladimir Oltean if (rc) 290789153ed6SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, "Failed to change VLAN Ethertype"); 29086666cebcSVladimir Oltean 2909227d07a0SVladimir Oltean /* Switch port identification based on 802.1Q is only passable 2910227d07a0SVladimir Oltean * if we are not under a vlan_filtering bridge. So make sure 29112cafa72eSVladimir Oltean * the two configurations are mutually exclusive (of course, the 29122cafa72eSVladimir Oltean * user may know better, i.e. best_effort_vlan_filtering). 2913227d07a0SVladimir Oltean */ 29142cafa72eSVladimir Oltean return sja1105_setup_8021q_tagging(ds, want_tagging); 29156666cebcSVladimir Oltean } 29166666cebcSVladimir Oltean 29175899ee36SVladimir Oltean /* Returns number of VLANs added (0 or 1) on success, 29185899ee36SVladimir Oltean * or a negative error code. 29195899ee36SVladimir Oltean */ 29205899ee36SVladimir Oltean static int sja1105_vlan_add_one(struct dsa_switch *ds, int port, u16 vid, 29215899ee36SVladimir Oltean u16 flags, struct list_head *vlan_list) 29225899ee36SVladimir Oltean { 29235899ee36SVladimir Oltean bool untagged = flags & BRIDGE_VLAN_INFO_UNTAGGED; 29245899ee36SVladimir Oltean bool pvid = flags & BRIDGE_VLAN_INFO_PVID; 29255899ee36SVladimir Oltean struct sja1105_bridge_vlan *v; 29265899ee36SVladimir Oltean 2927b38e659dSVladimir Oltean list_for_each_entry(v, vlan_list, list) { 2928b38e659dSVladimir Oltean if (v->port == port && v->vid == vid) { 29295899ee36SVladimir Oltean /* Already added */ 2930b38e659dSVladimir Oltean if (v->untagged == untagged && v->pvid == pvid) 2931b38e659dSVladimir Oltean /* Nothing changed */ 29325899ee36SVladimir Oltean return 0; 29335899ee36SVladimir Oltean 2934b38e659dSVladimir Oltean /* It's the same VLAN, but some of the flags changed 2935b38e659dSVladimir Oltean * and the user did not bother to delete it first. 2936b38e659dSVladimir Oltean * Update it and trigger sja1105_build_vlan_table. 2937b38e659dSVladimir Oltean */ 2938b38e659dSVladimir Oltean v->untagged = untagged; 2939b38e659dSVladimir Oltean v->pvid = pvid; 2940b38e659dSVladimir Oltean return 1; 2941b38e659dSVladimir Oltean } 2942b38e659dSVladimir Oltean } 2943b38e659dSVladimir Oltean 29445899ee36SVladimir Oltean v = kzalloc(sizeof(*v), GFP_KERNEL); 29455899ee36SVladimir Oltean if (!v) { 29465899ee36SVladimir Oltean dev_err(ds->dev, "Out of memory while storing VLAN\n"); 29475899ee36SVladimir Oltean return -ENOMEM; 29485899ee36SVladimir Oltean } 29495899ee36SVladimir Oltean 29505899ee36SVladimir Oltean v->port = port; 29515899ee36SVladimir Oltean v->vid = vid; 29525899ee36SVladimir Oltean v->untagged = untagged; 29535899ee36SVladimir Oltean v->pvid = pvid; 29545899ee36SVladimir Oltean list_add(&v->list, vlan_list); 29555899ee36SVladimir Oltean 29565899ee36SVladimir Oltean return 1; 29575899ee36SVladimir Oltean } 29585899ee36SVladimir Oltean 29595899ee36SVladimir Oltean /* Returns number of VLANs deleted (0 or 1) */ 29605899ee36SVladimir Oltean static int sja1105_vlan_del_one(struct dsa_switch *ds, int port, u16 vid, 29615899ee36SVladimir Oltean struct list_head *vlan_list) 29625899ee36SVladimir Oltean { 29635899ee36SVladimir Oltean struct sja1105_bridge_vlan *v, *n; 29645899ee36SVladimir Oltean 29655899ee36SVladimir Oltean list_for_each_entry_safe(v, n, vlan_list, list) { 29665899ee36SVladimir Oltean if (v->port == port && v->vid == vid) { 29675899ee36SVladimir Oltean list_del(&v->list); 29685899ee36SVladimir Oltean kfree(v); 29695899ee36SVladimir Oltean return 1; 29705899ee36SVladimir Oltean } 29715899ee36SVladimir Oltean } 29725899ee36SVladimir Oltean 29735899ee36SVladimir Oltean return 0; 29745899ee36SVladimir Oltean } 29755899ee36SVladimir Oltean 29761958d581SVladimir Oltean static int sja1105_vlan_add(struct dsa_switch *ds, int port, 297731046a5fSVladimir Oltean const struct switchdev_obj_port_vlan *vlan, 297831046a5fSVladimir Oltean struct netlink_ext_ack *extack) 29796666cebcSVladimir Oltean { 29806666cebcSVladimir Oltean struct sja1105_private *priv = ds->priv; 2981ec5ae610SVladimir Oltean bool vlan_table_changed = false; 29826666cebcSVladimir Oltean int rc; 29836666cebcSVladimir Oltean 29841958d581SVladimir Oltean /* If the user wants best-effort VLAN filtering (aka vlan_filtering 29851958d581SVladimir Oltean * bridge plus tagging), be sure to at least deny alterations to the 29861958d581SVladimir Oltean * configuration done by dsa_8021q. 29871958d581SVladimir Oltean */ 29881958d581SVladimir Oltean if (priv->vlan_state != SJA1105_VLAN_FILTERING_FULL && 29891958d581SVladimir Oltean vid_is_dsa_8021q(vlan->vid)) { 299031046a5fSVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 299131046a5fSVladimir Oltean "Range 1024-3071 reserved for dsa_8021q operation"); 29921958d581SVladimir Oltean return -EBUSY; 29931958d581SVladimir Oltean } 29941958d581SVladimir Oltean 2995b7a9e0daSVladimir Oltean rc = sja1105_vlan_add_one(ds, port, vlan->vid, vlan->flags, 29965899ee36SVladimir Oltean &priv->bridge_vlans); 29975899ee36SVladimir Oltean if (rc < 0) 29981958d581SVladimir Oltean return rc; 29995899ee36SVladimir Oltean if (rc > 0) 3000ec5ae610SVladimir Oltean vlan_table_changed = true; 3001ec5ae610SVladimir Oltean 3002ec5ae610SVladimir Oltean if (!vlan_table_changed) 30031958d581SVladimir Oltean return 0; 3004ec5ae610SVladimir Oltean 30051958d581SVladimir Oltean return sja1105_build_vlan_table(priv, true); 30066666cebcSVladimir Oltean } 30076666cebcSVladimir Oltean 30086666cebcSVladimir Oltean static int sja1105_vlan_del(struct dsa_switch *ds, int port, 30096666cebcSVladimir Oltean const struct switchdev_obj_port_vlan *vlan) 30106666cebcSVladimir Oltean { 30116666cebcSVladimir Oltean struct sja1105_private *priv = ds->priv; 3012ec5ae610SVladimir Oltean bool vlan_table_changed = false; 30135899ee36SVladimir Oltean int rc; 30146666cebcSVladimir Oltean 3015b7a9e0daSVladimir Oltean rc = sja1105_vlan_del_one(ds, port, vlan->vid, &priv->bridge_vlans); 30165899ee36SVladimir Oltean if (rc > 0) 3017ec5ae610SVladimir Oltean vlan_table_changed = true; 3018ec5ae610SVladimir Oltean 3019ec5ae610SVladimir Oltean if (!vlan_table_changed) 30206666cebcSVladimir Oltean return 0; 3021ec5ae610SVladimir Oltean 3022ec5ae610SVladimir Oltean return sja1105_build_vlan_table(priv, true); 30236666cebcSVladimir Oltean } 30246666cebcSVladimir Oltean 30255899ee36SVladimir Oltean static int sja1105_dsa_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid, 30265899ee36SVladimir Oltean u16 flags) 30275899ee36SVladimir Oltean { 30285899ee36SVladimir Oltean struct sja1105_private *priv = ds->priv; 30295899ee36SVladimir Oltean int rc; 30305899ee36SVladimir Oltean 30315899ee36SVladimir Oltean rc = sja1105_vlan_add_one(ds, port, vid, flags, &priv->dsa_8021q_vlans); 30325899ee36SVladimir Oltean if (rc <= 0) 30335899ee36SVladimir Oltean return rc; 30345899ee36SVladimir Oltean 30355899ee36SVladimir Oltean return sja1105_build_vlan_table(priv, true); 30365899ee36SVladimir Oltean } 30375899ee36SVladimir Oltean 30385899ee36SVladimir Oltean static int sja1105_dsa_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid) 30395899ee36SVladimir Oltean { 30405899ee36SVladimir Oltean struct sja1105_private *priv = ds->priv; 30415899ee36SVladimir Oltean int rc; 30425899ee36SVladimir Oltean 30435899ee36SVladimir Oltean rc = sja1105_vlan_del_one(ds, port, vid, &priv->dsa_8021q_vlans); 30445899ee36SVladimir Oltean if (!rc) 30455899ee36SVladimir Oltean return 0; 30465899ee36SVladimir Oltean 30475899ee36SVladimir Oltean return sja1105_build_vlan_table(priv, true); 30485899ee36SVladimir Oltean } 30495899ee36SVladimir Oltean 30505899ee36SVladimir Oltean static const struct dsa_8021q_ops sja1105_dsa_8021q_ops = { 30515899ee36SVladimir Oltean .vlan_add = sja1105_dsa_8021q_vlan_add, 30525899ee36SVladimir Oltean .vlan_del = sja1105_dsa_8021q_vlan_del, 30535899ee36SVladimir Oltean }; 30545899ee36SVladimir Oltean 30558aa9ebccSVladimir Oltean /* The programming model for the SJA1105 switch is "all-at-once" via static 30568aa9ebccSVladimir Oltean * configuration tables. Some of these can be dynamically modified at runtime, 30578aa9ebccSVladimir Oltean * but not the xMII mode parameters table. 30588aa9ebccSVladimir Oltean * Furthermode, some PHYs may not have crystals for generating their clocks 30598aa9ebccSVladimir Oltean * (e.g. RMII). Instead, their 50MHz clock is supplied via the SJA1105 port's 30608aa9ebccSVladimir Oltean * ref_clk pin. So port clocking needs to be initialized early, before 30618aa9ebccSVladimir Oltean * connecting to PHYs is attempted, otherwise they won't respond through MDIO. 30628aa9ebccSVladimir Oltean * Setting correct PHY link speed does not matter now. 30638aa9ebccSVladimir Oltean * But dsa_slave_phy_setup is called later than sja1105_setup, so the PHY 30648aa9ebccSVladimir Oltean * bindings are not yet parsed by DSA core. We need to parse early so that we 30658aa9ebccSVladimir Oltean * can populate the xMII mode parameters table. 30668aa9ebccSVladimir Oltean */ 30678aa9ebccSVladimir Oltean static int sja1105_setup(struct dsa_switch *ds) 30688aa9ebccSVladimir Oltean { 30698aa9ebccSVladimir Oltean struct sja1105_private *priv = ds->priv; 30708aa9ebccSVladimir Oltean int rc; 30718aa9ebccSVladimir Oltean 30725d645df9SVladimir Oltean rc = sja1105_parse_dt(priv); 30738aa9ebccSVladimir Oltean if (rc < 0) { 30748aa9ebccSVladimir Oltean dev_err(ds->dev, "Failed to parse DT: %d\n", rc); 30758aa9ebccSVladimir Oltean return rc; 30768aa9ebccSVladimir Oltean } 3077f5b8631cSVladimir Oltean 3078f5b8631cSVladimir Oltean /* Error out early if internal delays are required through DT 3079f5b8631cSVladimir Oltean * and we can't apply them. 3080f5b8631cSVladimir Oltean */ 308129afb83aSVladimir Oltean rc = sja1105_parse_rgmii_delays(priv); 3082f5b8631cSVladimir Oltean if (rc < 0) { 3083f5b8631cSVladimir Oltean dev_err(ds->dev, "RGMII delay not supported\n"); 3084f5b8631cSVladimir Oltean return rc; 3085f5b8631cSVladimir Oltean } 3086f5b8631cSVladimir Oltean 308761c77126SVladimir Oltean rc = sja1105_ptp_clock_register(ds); 3088bb77f36aSVladimir Oltean if (rc < 0) { 3089bb77f36aSVladimir Oltean dev_err(ds->dev, "Failed to register PTP clock: %d\n", rc); 3090bb77f36aSVladimir Oltean return rc; 3091bb77f36aSVladimir Oltean } 30925a8f0974SVladimir Oltean 30935a8f0974SVladimir Oltean rc = sja1105_mdiobus_register(ds); 30945a8f0974SVladimir Oltean if (rc < 0) { 30955a8f0974SVladimir Oltean dev_err(ds->dev, "Failed to register MDIO bus: %pe\n", 30965a8f0974SVladimir Oltean ERR_PTR(rc)); 30975a8f0974SVladimir Oltean goto out_ptp_clock_unregister; 30985a8f0974SVladimir Oltean } 30995a8f0974SVladimir Oltean 3100cb5a82d2SVladimir Oltean if (priv->info->disable_microcontroller) { 3101cb5a82d2SVladimir Oltean rc = priv->info->disable_microcontroller(priv); 3102cb5a82d2SVladimir Oltean if (rc < 0) { 3103cb5a82d2SVladimir Oltean dev_err(ds->dev, 3104cb5a82d2SVladimir Oltean "Failed to disable microcontroller: %pe\n", 3105cb5a82d2SVladimir Oltean ERR_PTR(rc)); 3106cb5a82d2SVladimir Oltean goto out_mdiobus_unregister; 3107cb5a82d2SVladimir Oltean } 3108cb5a82d2SVladimir Oltean } 3109cb5a82d2SVladimir Oltean 31108aa9ebccSVladimir Oltean /* Create and send configuration down to device */ 31115d645df9SVladimir Oltean rc = sja1105_static_config_load(priv); 31128aa9ebccSVladimir Oltean if (rc < 0) { 31138aa9ebccSVladimir Oltean dev_err(ds->dev, "Failed to load static config: %d\n", rc); 31145a8f0974SVladimir Oltean goto out_mdiobus_unregister; 31158aa9ebccSVladimir Oltean } 3116cb5a82d2SVladimir Oltean 31178aa9ebccSVladimir Oltean /* Configure the CGU (PHY link modes and speeds) */ 3118cb5a82d2SVladimir Oltean if (priv->info->clocking_setup) { 3119c5037678SVladimir Oltean rc = priv->info->clocking_setup(priv); 31208aa9ebccSVladimir Oltean if (rc < 0) { 3121cb5a82d2SVladimir Oltean dev_err(ds->dev, 3122cb5a82d2SVladimir Oltean "Failed to configure MII clocking: %pe\n", 3123cb5a82d2SVladimir Oltean ERR_PTR(rc)); 3124cec279a8SVladimir Oltean goto out_static_config_free; 31258aa9ebccSVladimir Oltean } 3126cb5a82d2SVladimir Oltean } 3127cb5a82d2SVladimir Oltean 31286666cebcSVladimir Oltean /* On SJA1105, VLAN filtering per se is always enabled in hardware. 31296666cebcSVladimir Oltean * The only thing we can do to disable it is lie about what the 802.1Q 31306666cebcSVladimir Oltean * EtherType is. 31316666cebcSVladimir Oltean * So it will still try to apply VLAN filtering, but all ingress 31326666cebcSVladimir Oltean * traffic (except frames received with EtherType of ETH_P_SJA1105) 31336666cebcSVladimir Oltean * will be internally tagged with a distorted VLAN header where the 31346666cebcSVladimir Oltean * TPID is ETH_P_SJA1105, and the VLAN ID is the port pvid. 31356666cebcSVladimir Oltean */ 31366666cebcSVladimir Oltean ds->vlan_filtering_is_global = true; 31378aa9ebccSVladimir Oltean 31385f06c63bSVladimir Oltean /* Advertise the 8 egress queues */ 31395f06c63bSVladimir Oltean ds->num_tx_queues = SJA1105_NUM_TC; 31405f06c63bSVladimir Oltean 3141c279c726SVladimir Oltean ds->mtu_enforcement_ingress = true; 3142c279c726SVladimir Oltean 31438841f6e6SVladimir Oltean priv->best_effort_vlan_filtering = true; 31448841f6e6SVladimir Oltean 31450a7bdbc2SVladimir Oltean rc = sja1105_devlink_setup(ds); 31462cafa72eSVladimir Oltean if (rc < 0) 3147cec279a8SVladimir Oltean goto out_static_config_free; 31482cafa72eSVladimir Oltean 3149227d07a0SVladimir Oltean /* The DSA/switchdev model brings up switch ports in standalone mode by 3150227d07a0SVladimir Oltean * default, and that means vlan_filtering is 0 since they're not under 3151227d07a0SVladimir Oltean * a bridge, so it's safe to set up switch tagging at this time. 3152227d07a0SVladimir Oltean */ 3153bbed0bbdSVladimir Oltean rtnl_lock(); 3154bbed0bbdSVladimir Oltean rc = sja1105_setup_8021q_tagging(ds, true); 3155bbed0bbdSVladimir Oltean rtnl_unlock(); 3156cec279a8SVladimir Oltean if (rc) 3157cec279a8SVladimir Oltean goto out_devlink_teardown; 3158cec279a8SVladimir Oltean 3159cec279a8SVladimir Oltean return 0; 3160cec279a8SVladimir Oltean 3161cec279a8SVladimir Oltean out_devlink_teardown: 3162cec279a8SVladimir Oltean sja1105_devlink_teardown(ds); 31635a8f0974SVladimir Oltean out_mdiobus_unregister: 31645a8f0974SVladimir Oltean sja1105_mdiobus_unregister(ds); 3165cec279a8SVladimir Oltean out_ptp_clock_unregister: 3166cec279a8SVladimir Oltean sja1105_ptp_clock_unregister(ds); 3167cec279a8SVladimir Oltean out_static_config_free: 3168cec279a8SVladimir Oltean sja1105_static_config_free(&priv->static_config); 3169bbed0bbdSVladimir Oltean 3170bbed0bbdSVladimir Oltean return rc; 3171227d07a0SVladimir Oltean } 3172227d07a0SVladimir Oltean 3173f3097be2SVladimir Oltean static void sja1105_teardown(struct dsa_switch *ds) 3174f3097be2SVladimir Oltean { 3175f3097be2SVladimir Oltean struct sja1105_private *priv = ds->priv; 3176ec5ae610SVladimir Oltean struct sja1105_bridge_vlan *v, *n; 3177a68578c2SVladimir Oltean int port; 3178a68578c2SVladimir Oltean 3179542043e9SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 3180a68578c2SVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 3181a68578c2SVladimir Oltean 3182a68578c2SVladimir Oltean if (!dsa_is_user_port(ds, port)) 3183a68578c2SVladimir Oltean continue; 3184a68578c2SVladimir Oltean 318552c0d4e3SVladimir Oltean if (sp->xmit_worker) 3186a68578c2SVladimir Oltean kthread_destroy_worker(sp->xmit_worker); 3187a68578c2SVladimir Oltean } 3188f3097be2SVladimir Oltean 31890a7bdbc2SVladimir Oltean sja1105_devlink_teardown(ds); 3190a6af7763SVladimir Oltean sja1105_flower_teardown(ds); 3191317ab5b8SVladimir Oltean sja1105_tas_teardown(ds); 319261c77126SVladimir Oltean sja1105_ptp_clock_unregister(ds); 31936cb0abbdSVladimir Oltean sja1105_static_config_free(&priv->static_config); 3194ec5ae610SVladimir Oltean 3195ec5ae610SVladimir Oltean list_for_each_entry_safe(v, n, &priv->dsa_8021q_vlans, list) { 3196ec5ae610SVladimir Oltean list_del(&v->list); 3197ec5ae610SVladimir Oltean kfree(v); 3198ec5ae610SVladimir Oltean } 3199ec5ae610SVladimir Oltean 3200ec5ae610SVladimir Oltean list_for_each_entry_safe(v, n, &priv->bridge_vlans, list) { 3201ec5ae610SVladimir Oltean list_del(&v->list); 3202ec5ae610SVladimir Oltean kfree(v); 3203ec5ae610SVladimir Oltean } 3204f3097be2SVladimir Oltean } 3205f3097be2SVladimir Oltean 3206a68578c2SVladimir Oltean static void sja1105_port_disable(struct dsa_switch *ds, int port) 3207a68578c2SVladimir Oltean { 3208a68578c2SVladimir Oltean struct sja1105_private *priv = ds->priv; 3209a68578c2SVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 3210a68578c2SVladimir Oltean 3211a68578c2SVladimir Oltean if (!dsa_is_user_port(ds, port)) 3212a68578c2SVladimir Oltean return; 3213a68578c2SVladimir Oltean 3214a68578c2SVladimir Oltean kthread_cancel_work_sync(&sp->xmit_work); 3215a68578c2SVladimir Oltean skb_queue_purge(&sp->xmit_queue); 3216a68578c2SVladimir Oltean } 3217a68578c2SVladimir Oltean 3218227d07a0SVladimir Oltean static int sja1105_mgmt_xmit(struct dsa_switch *ds, int port, int slot, 321947ed985eSVladimir Oltean struct sk_buff *skb, bool takets) 3220227d07a0SVladimir Oltean { 3221227d07a0SVladimir Oltean struct sja1105_mgmt_entry mgmt_route = {0}; 3222227d07a0SVladimir Oltean struct sja1105_private *priv = ds->priv; 3223227d07a0SVladimir Oltean struct ethhdr *hdr; 3224227d07a0SVladimir Oltean int timeout = 10; 3225227d07a0SVladimir Oltean int rc; 3226227d07a0SVladimir Oltean 3227227d07a0SVladimir Oltean hdr = eth_hdr(skb); 3228227d07a0SVladimir Oltean 3229227d07a0SVladimir Oltean mgmt_route.macaddr = ether_addr_to_u64(hdr->h_dest); 3230227d07a0SVladimir Oltean mgmt_route.destports = BIT(port); 3231227d07a0SVladimir Oltean mgmt_route.enfport = 1; 323247ed985eSVladimir Oltean mgmt_route.tsreg = 0; 323347ed985eSVladimir Oltean mgmt_route.takets = takets; 3234227d07a0SVladimir Oltean 3235227d07a0SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE, 3236227d07a0SVladimir Oltean slot, &mgmt_route, true); 3237227d07a0SVladimir Oltean if (rc < 0) { 3238227d07a0SVladimir Oltean kfree_skb(skb); 3239227d07a0SVladimir Oltean return rc; 3240227d07a0SVladimir Oltean } 3241227d07a0SVladimir Oltean 3242227d07a0SVladimir Oltean /* Transfer skb to the host port. */ 324368bb8ea8SVivien Didelot dsa_enqueue_skb(skb, dsa_to_port(ds, port)->slave); 3244227d07a0SVladimir Oltean 3245227d07a0SVladimir Oltean /* Wait until the switch has processed the frame */ 3246227d07a0SVladimir Oltean do { 3247227d07a0SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_MGMT_ROUTE, 3248227d07a0SVladimir Oltean slot, &mgmt_route); 3249227d07a0SVladimir Oltean if (rc < 0) { 3250227d07a0SVladimir Oltean dev_err_ratelimited(priv->ds->dev, 3251227d07a0SVladimir Oltean "failed to poll for mgmt route\n"); 3252227d07a0SVladimir Oltean continue; 3253227d07a0SVladimir Oltean } 3254227d07a0SVladimir Oltean 3255227d07a0SVladimir Oltean /* UM10944: The ENFPORT flag of the respective entry is 3256227d07a0SVladimir Oltean * cleared when a match is found. The host can use this 3257227d07a0SVladimir Oltean * flag as an acknowledgment. 3258227d07a0SVladimir Oltean */ 3259227d07a0SVladimir Oltean cpu_relax(); 3260227d07a0SVladimir Oltean } while (mgmt_route.enfport && --timeout); 3261227d07a0SVladimir Oltean 3262227d07a0SVladimir Oltean if (!timeout) { 3263227d07a0SVladimir Oltean /* Clean up the management route so that a follow-up 3264227d07a0SVladimir Oltean * frame may not match on it by mistake. 32652a7e7409SVladimir Oltean * This is only hardware supported on P/Q/R/S - on E/T it is 32662a7e7409SVladimir Oltean * a no-op and we are silently discarding the -EOPNOTSUPP. 3267227d07a0SVladimir Oltean */ 3268227d07a0SVladimir Oltean sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE, 3269227d07a0SVladimir Oltean slot, &mgmt_route, false); 3270227d07a0SVladimir Oltean dev_err_ratelimited(priv->ds->dev, "xmit timed out\n"); 3271227d07a0SVladimir Oltean } 3272227d07a0SVladimir Oltean 3273227d07a0SVladimir Oltean return NETDEV_TX_OK; 3274227d07a0SVladimir Oltean } 3275227d07a0SVladimir Oltean 3276a68578c2SVladimir Oltean #define work_to_port(work) \ 3277a68578c2SVladimir Oltean container_of((work), struct sja1105_port, xmit_work) 3278a68578c2SVladimir Oltean #define tagger_to_sja1105(t) \ 3279a68578c2SVladimir Oltean container_of((t), struct sja1105_private, tagger_data) 3280a68578c2SVladimir Oltean 3281227d07a0SVladimir Oltean /* Deferred work is unfortunately necessary because setting up the management 3282227d07a0SVladimir Oltean * route cannot be done from atomit context (SPI transfer takes a sleepable 3283227d07a0SVladimir Oltean * lock on the bus) 3284227d07a0SVladimir Oltean */ 3285a68578c2SVladimir Oltean static void sja1105_port_deferred_xmit(struct kthread_work *work) 3286227d07a0SVladimir Oltean { 3287a68578c2SVladimir Oltean struct sja1105_port *sp = work_to_port(work); 3288a68578c2SVladimir Oltean struct sja1105_tagger_data *tagger_data = sp->data; 3289a68578c2SVladimir Oltean struct sja1105_private *priv = tagger_to_sja1105(tagger_data); 3290a68578c2SVladimir Oltean int port = sp - priv->ports; 3291a68578c2SVladimir Oltean struct sk_buff *skb; 3292a68578c2SVladimir Oltean 3293a68578c2SVladimir Oltean while ((skb = skb_dequeue(&sp->xmit_queue)) != NULL) { 3294c4b364ceSYangbo Lu struct sk_buff *clone = SJA1105_SKB_CB(skb)->clone; 3295227d07a0SVladimir Oltean 3296227d07a0SVladimir Oltean mutex_lock(&priv->mgmt_lock); 3297227d07a0SVladimir Oltean 3298a68578c2SVladimir Oltean sja1105_mgmt_xmit(priv->ds, port, 0, skb, !!clone); 3299a68578c2SVladimir Oltean 330047ed985eSVladimir Oltean /* The clone, if there, was made by dsa_skb_tx_timestamp */ 3301a68578c2SVladimir Oltean if (clone) 3302a68578c2SVladimir Oltean sja1105_ptp_txtstamp_skb(priv->ds, port, clone); 3303227d07a0SVladimir Oltean 3304227d07a0SVladimir Oltean mutex_unlock(&priv->mgmt_lock); 3305a68578c2SVladimir Oltean } 33068aa9ebccSVladimir Oltean } 33078aa9ebccSVladimir Oltean 33088456721dSVladimir Oltean /* The MAXAGE setting belongs to the L2 Forwarding Parameters table, 33098456721dSVladimir Oltean * which cannot be reconfigured at runtime. So a switch reset is required. 33108456721dSVladimir Oltean */ 33118456721dSVladimir Oltean static int sja1105_set_ageing_time(struct dsa_switch *ds, 33128456721dSVladimir Oltean unsigned int ageing_time) 33138456721dSVladimir Oltean { 33148456721dSVladimir Oltean struct sja1105_l2_lookup_params_entry *l2_lookup_params; 33158456721dSVladimir Oltean struct sja1105_private *priv = ds->priv; 33168456721dSVladimir Oltean struct sja1105_table *table; 33178456721dSVladimir Oltean unsigned int maxage; 33188456721dSVladimir Oltean 33198456721dSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS]; 33208456721dSVladimir Oltean l2_lookup_params = table->entries; 33218456721dSVladimir Oltean 33228456721dSVladimir Oltean maxage = SJA1105_AGEING_TIME_MS(ageing_time); 33238456721dSVladimir Oltean 33248456721dSVladimir Oltean if (l2_lookup_params->maxage == maxage) 33258456721dSVladimir Oltean return 0; 33268456721dSVladimir Oltean 33278456721dSVladimir Oltean l2_lookup_params->maxage = maxage; 33288456721dSVladimir Oltean 33292eea1fa8SVladimir Oltean return sja1105_static_config_reload(priv, SJA1105_AGEING_TIME); 33308456721dSVladimir Oltean } 33318456721dSVladimir Oltean 3332c279c726SVladimir Oltean static int sja1105_change_mtu(struct dsa_switch *ds, int port, int new_mtu) 3333c279c726SVladimir Oltean { 3334c279c726SVladimir Oltean struct sja1105_l2_policing_entry *policing; 3335c279c726SVladimir Oltean struct sja1105_private *priv = ds->priv; 3336c279c726SVladimir Oltean 3337c279c726SVladimir Oltean new_mtu += VLAN_ETH_HLEN + ETH_FCS_LEN; 3338c279c726SVladimir Oltean 3339c279c726SVladimir Oltean if (dsa_is_cpu_port(ds, port)) 3340c279c726SVladimir Oltean new_mtu += VLAN_HLEN; 3341c279c726SVladimir Oltean 3342c279c726SVladimir Oltean policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries; 3343c279c726SVladimir Oltean 3344a7cc081cSVladimir Oltean if (policing[port].maxlen == new_mtu) 3345c279c726SVladimir Oltean return 0; 3346c279c726SVladimir Oltean 3347a7cc081cSVladimir Oltean policing[port].maxlen = new_mtu; 3348c279c726SVladimir Oltean 3349c279c726SVladimir Oltean return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING); 3350c279c726SVladimir Oltean } 3351c279c726SVladimir Oltean 3352c279c726SVladimir Oltean static int sja1105_get_max_mtu(struct dsa_switch *ds, int port) 3353c279c726SVladimir Oltean { 3354c279c726SVladimir Oltean return 2043 - VLAN_ETH_HLEN - ETH_FCS_LEN; 3355c279c726SVladimir Oltean } 3356c279c726SVladimir Oltean 3357317ab5b8SVladimir Oltean static int sja1105_port_setup_tc(struct dsa_switch *ds, int port, 3358317ab5b8SVladimir Oltean enum tc_setup_type type, 3359317ab5b8SVladimir Oltean void *type_data) 3360317ab5b8SVladimir Oltean { 3361317ab5b8SVladimir Oltean switch (type) { 3362317ab5b8SVladimir Oltean case TC_SETUP_QDISC_TAPRIO: 3363317ab5b8SVladimir Oltean return sja1105_setup_tc_taprio(ds, port, type_data); 33644d752508SVladimir Oltean case TC_SETUP_QDISC_CBS: 33654d752508SVladimir Oltean return sja1105_setup_tc_cbs(ds, port, type_data); 3366317ab5b8SVladimir Oltean default: 3367317ab5b8SVladimir Oltean return -EOPNOTSUPP; 3368317ab5b8SVladimir Oltean } 3369317ab5b8SVladimir Oltean } 3370317ab5b8SVladimir Oltean 3371511e6ca0SVladimir Oltean /* We have a single mirror (@to) port, but can configure ingress and egress 3372511e6ca0SVladimir Oltean * mirroring on all other (@from) ports. 3373511e6ca0SVladimir Oltean * We need to allow mirroring rules only as long as the @to port is always the 3374511e6ca0SVladimir Oltean * same, and we need to unset the @to port from mirr_port only when there is no 3375511e6ca0SVladimir Oltean * mirroring rule that references it. 3376511e6ca0SVladimir Oltean */ 3377511e6ca0SVladimir Oltean static int sja1105_mirror_apply(struct sja1105_private *priv, int from, int to, 3378511e6ca0SVladimir Oltean bool ingress, bool enabled) 3379511e6ca0SVladimir Oltean { 3380511e6ca0SVladimir Oltean struct sja1105_general_params_entry *general_params; 3381511e6ca0SVladimir Oltean struct sja1105_mac_config_entry *mac; 3382542043e9SVladimir Oltean struct dsa_switch *ds = priv->ds; 3383511e6ca0SVladimir Oltean struct sja1105_table *table; 3384511e6ca0SVladimir Oltean bool already_enabled; 3385511e6ca0SVladimir Oltean u64 new_mirr_port; 3386511e6ca0SVladimir Oltean int rc; 3387511e6ca0SVladimir Oltean 3388511e6ca0SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; 3389511e6ca0SVladimir Oltean general_params = table->entries; 3390511e6ca0SVladimir Oltean 3391511e6ca0SVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 3392511e6ca0SVladimir Oltean 3393542043e9SVladimir Oltean already_enabled = (general_params->mirr_port != ds->num_ports); 3394511e6ca0SVladimir Oltean if (already_enabled && enabled && general_params->mirr_port != to) { 3395511e6ca0SVladimir Oltean dev_err(priv->ds->dev, 3396511e6ca0SVladimir Oltean "Delete mirroring rules towards port %llu first\n", 3397511e6ca0SVladimir Oltean general_params->mirr_port); 3398511e6ca0SVladimir Oltean return -EBUSY; 3399511e6ca0SVladimir Oltean } 3400511e6ca0SVladimir Oltean 3401511e6ca0SVladimir Oltean new_mirr_port = to; 3402511e6ca0SVladimir Oltean if (!enabled) { 3403511e6ca0SVladimir Oltean bool keep = false; 3404511e6ca0SVladimir Oltean int port; 3405511e6ca0SVladimir Oltean 3406511e6ca0SVladimir Oltean /* Anybody still referencing mirr_port? */ 3407542043e9SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 3408511e6ca0SVladimir Oltean if (mac[port].ing_mirr || mac[port].egr_mirr) { 3409511e6ca0SVladimir Oltean keep = true; 3410511e6ca0SVladimir Oltean break; 3411511e6ca0SVladimir Oltean } 3412511e6ca0SVladimir Oltean } 3413511e6ca0SVladimir Oltean /* Unset already_enabled for next time */ 3414511e6ca0SVladimir Oltean if (!keep) 3415542043e9SVladimir Oltean new_mirr_port = ds->num_ports; 3416511e6ca0SVladimir Oltean } 3417511e6ca0SVladimir Oltean if (new_mirr_port != general_params->mirr_port) { 3418511e6ca0SVladimir Oltean general_params->mirr_port = new_mirr_port; 3419511e6ca0SVladimir Oltean 3420511e6ca0SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_GENERAL_PARAMS, 3421511e6ca0SVladimir Oltean 0, general_params, true); 3422511e6ca0SVladimir Oltean if (rc < 0) 3423511e6ca0SVladimir Oltean return rc; 3424511e6ca0SVladimir Oltean } 3425511e6ca0SVladimir Oltean 3426511e6ca0SVladimir Oltean if (ingress) 3427511e6ca0SVladimir Oltean mac[from].ing_mirr = enabled; 3428511e6ca0SVladimir Oltean else 3429511e6ca0SVladimir Oltean mac[from].egr_mirr = enabled; 3430511e6ca0SVladimir Oltean 3431511e6ca0SVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, from, 3432511e6ca0SVladimir Oltean &mac[from], true); 3433511e6ca0SVladimir Oltean } 3434511e6ca0SVladimir Oltean 3435511e6ca0SVladimir Oltean static int sja1105_mirror_add(struct dsa_switch *ds, int port, 3436511e6ca0SVladimir Oltean struct dsa_mall_mirror_tc_entry *mirror, 3437511e6ca0SVladimir Oltean bool ingress) 3438511e6ca0SVladimir Oltean { 3439511e6ca0SVladimir Oltean return sja1105_mirror_apply(ds->priv, port, mirror->to_local_port, 3440511e6ca0SVladimir Oltean ingress, true); 3441511e6ca0SVladimir Oltean } 3442511e6ca0SVladimir Oltean 3443511e6ca0SVladimir Oltean static void sja1105_mirror_del(struct dsa_switch *ds, int port, 3444511e6ca0SVladimir Oltean struct dsa_mall_mirror_tc_entry *mirror) 3445511e6ca0SVladimir Oltean { 3446511e6ca0SVladimir Oltean sja1105_mirror_apply(ds->priv, port, mirror->to_local_port, 3447511e6ca0SVladimir Oltean mirror->ingress, false); 3448511e6ca0SVladimir Oltean } 3449511e6ca0SVladimir Oltean 3450a7cc081cSVladimir Oltean static int sja1105_port_policer_add(struct dsa_switch *ds, int port, 3451a7cc081cSVladimir Oltean struct dsa_mall_policer_tc_entry *policer) 3452a7cc081cSVladimir Oltean { 3453a7cc081cSVladimir Oltean struct sja1105_l2_policing_entry *policing; 3454a7cc081cSVladimir Oltean struct sja1105_private *priv = ds->priv; 3455a7cc081cSVladimir Oltean 3456a7cc081cSVladimir Oltean policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries; 3457a7cc081cSVladimir Oltean 3458a7cc081cSVladimir Oltean /* In hardware, every 8 microseconds the credit level is incremented by 3459a7cc081cSVladimir Oltean * the value of RATE bytes divided by 64, up to a maximum of SMAX 3460a7cc081cSVladimir Oltean * bytes. 3461a7cc081cSVladimir Oltean */ 3462a7cc081cSVladimir Oltean policing[port].rate = div_u64(512 * policer->rate_bytes_per_sec, 3463a7cc081cSVladimir Oltean 1000000); 34645f035af7SPo Liu policing[port].smax = policer->burst; 3465a7cc081cSVladimir Oltean 3466a7cc081cSVladimir Oltean return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING); 3467a7cc081cSVladimir Oltean } 3468a7cc081cSVladimir Oltean 3469a7cc081cSVladimir Oltean static void sja1105_port_policer_del(struct dsa_switch *ds, int port) 3470a7cc081cSVladimir Oltean { 3471a7cc081cSVladimir Oltean struct sja1105_l2_policing_entry *policing; 3472a7cc081cSVladimir Oltean struct sja1105_private *priv = ds->priv; 3473a7cc081cSVladimir Oltean 3474a7cc081cSVladimir Oltean policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries; 3475a7cc081cSVladimir Oltean 3476a7cc081cSVladimir Oltean policing[port].rate = SJA1105_RATE_MBPS(1000); 3477a7cc081cSVladimir Oltean policing[port].smax = 65535; 3478a7cc081cSVladimir Oltean 3479a7cc081cSVladimir Oltean sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING); 3480a7cc081cSVladimir Oltean } 3481a7cc081cSVladimir Oltean 34824d942354SVladimir Oltean static int sja1105_port_set_learning(struct sja1105_private *priv, int port, 34834d942354SVladimir Oltean bool enabled) 34844d942354SVladimir Oltean { 34854d942354SVladimir Oltean struct sja1105_mac_config_entry *mac; 34864d942354SVladimir Oltean int rc; 34874d942354SVladimir Oltean 34884d942354SVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 34894d942354SVladimir Oltean 34904c44fc5eSVladimir Oltean mac[port].dyn_learn = enabled; 34914d942354SVladimir Oltean 34924d942354SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, 34934d942354SVladimir Oltean &mac[port], true); 34944d942354SVladimir Oltean if (rc) 34954d942354SVladimir Oltean return rc; 34964d942354SVladimir Oltean 34974d942354SVladimir Oltean if (enabled) 34984d942354SVladimir Oltean priv->learn_ena |= BIT(port); 34994d942354SVladimir Oltean else 35004d942354SVladimir Oltean priv->learn_ena &= ~BIT(port); 35014d942354SVladimir Oltean 35024d942354SVladimir Oltean return 0; 35034d942354SVladimir Oltean } 35044d942354SVladimir Oltean 35054d942354SVladimir Oltean static int sja1105_port_ucast_bcast_flood(struct sja1105_private *priv, int to, 35064d942354SVladimir Oltean struct switchdev_brport_flags flags) 35074d942354SVladimir Oltean { 35084d942354SVladimir Oltean if (flags.mask & BR_FLOOD) { 35094d942354SVladimir Oltean if (flags.val & BR_FLOOD) 35107f7ccdeaSVladimir Oltean priv->ucast_egress_floods |= BIT(to); 35114d942354SVladimir Oltean else 35126a5166e0SVladimir Oltean priv->ucast_egress_floods &= ~BIT(to); 35134d942354SVladimir Oltean } 35147f7ccdeaSVladimir Oltean 35154d942354SVladimir Oltean if (flags.mask & BR_BCAST_FLOOD) { 35164d942354SVladimir Oltean if (flags.val & BR_BCAST_FLOOD) 35177f7ccdeaSVladimir Oltean priv->bcast_egress_floods |= BIT(to); 35184d942354SVladimir Oltean else 35196a5166e0SVladimir Oltean priv->bcast_egress_floods &= ~BIT(to); 35204d942354SVladimir Oltean } 35214d942354SVladimir Oltean 35227f7ccdeaSVladimir Oltean return sja1105_manage_flood_domains(priv); 35234d942354SVladimir Oltean } 35244d942354SVladimir Oltean 35254d942354SVladimir Oltean static int sja1105_port_mcast_flood(struct sja1105_private *priv, int to, 35264d942354SVladimir Oltean struct switchdev_brport_flags flags, 35274d942354SVladimir Oltean struct netlink_ext_ack *extack) 35284d942354SVladimir Oltean { 35294d942354SVladimir Oltean struct sja1105_l2_lookup_entry *l2_lookup; 35304d942354SVladimir Oltean struct sja1105_table *table; 35314d942354SVladimir Oltean int match; 35324d942354SVladimir Oltean 35334d942354SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; 35344d942354SVladimir Oltean l2_lookup = table->entries; 35354d942354SVladimir Oltean 35364d942354SVladimir Oltean for (match = 0; match < table->entry_count; match++) 35374d942354SVladimir Oltean if (l2_lookup[match].macaddr == SJA1105_UNKNOWN_MULTICAST && 35384d942354SVladimir Oltean l2_lookup[match].mask_macaddr == SJA1105_UNKNOWN_MULTICAST) 35394d942354SVladimir Oltean break; 35404d942354SVladimir Oltean 35414d942354SVladimir Oltean if (match == table->entry_count) { 35424d942354SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 35434d942354SVladimir Oltean "Could not find FDB entry for unknown multicast"); 35444d942354SVladimir Oltean return -ENOSPC; 35454d942354SVladimir Oltean } 35464d942354SVladimir Oltean 35474d942354SVladimir Oltean if (flags.val & BR_MCAST_FLOOD) 35484d942354SVladimir Oltean l2_lookup[match].destports |= BIT(to); 35494d942354SVladimir Oltean else 35504d942354SVladimir Oltean l2_lookup[match].destports &= ~BIT(to); 35514d942354SVladimir Oltean 35524d942354SVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 35534d942354SVladimir Oltean l2_lookup[match].index, 35544d942354SVladimir Oltean &l2_lookup[match], 35554d942354SVladimir Oltean true); 35564d942354SVladimir Oltean } 35574d942354SVladimir Oltean 35584d942354SVladimir Oltean static int sja1105_port_pre_bridge_flags(struct dsa_switch *ds, int port, 35594d942354SVladimir Oltean struct switchdev_brport_flags flags, 35604d942354SVladimir Oltean struct netlink_ext_ack *extack) 35614d942354SVladimir Oltean { 35624d942354SVladimir Oltean struct sja1105_private *priv = ds->priv; 35634d942354SVladimir Oltean 35644d942354SVladimir Oltean if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | 35654d942354SVladimir Oltean BR_BCAST_FLOOD)) 35664d942354SVladimir Oltean return -EINVAL; 35674d942354SVladimir Oltean 35684d942354SVladimir Oltean if (flags.mask & (BR_FLOOD | BR_MCAST_FLOOD) && 35694d942354SVladimir Oltean !priv->info->can_limit_mcast_flood) { 35704d942354SVladimir Oltean bool multicast = !!(flags.val & BR_MCAST_FLOOD); 35714d942354SVladimir Oltean bool unicast = !!(flags.val & BR_FLOOD); 35724d942354SVladimir Oltean 35734d942354SVladimir Oltean if (unicast != multicast) { 35744d942354SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 35754d942354SVladimir Oltean "This chip cannot configure multicast flooding independently of unicast"); 35764d942354SVladimir Oltean return -EINVAL; 35774d942354SVladimir Oltean } 35784d942354SVladimir Oltean } 35794d942354SVladimir Oltean 35804d942354SVladimir Oltean return 0; 35814d942354SVladimir Oltean } 35824d942354SVladimir Oltean 35834d942354SVladimir Oltean static int sja1105_port_bridge_flags(struct dsa_switch *ds, int port, 35844d942354SVladimir Oltean struct switchdev_brport_flags flags, 35854d942354SVladimir Oltean struct netlink_ext_ack *extack) 35864d942354SVladimir Oltean { 35874d942354SVladimir Oltean struct sja1105_private *priv = ds->priv; 35884d942354SVladimir Oltean int rc; 35894d942354SVladimir Oltean 35904d942354SVladimir Oltean if (flags.mask & BR_LEARNING) { 35914d942354SVladimir Oltean bool learn_ena = !!(flags.val & BR_LEARNING); 35924d942354SVladimir Oltean 35934d942354SVladimir Oltean rc = sja1105_port_set_learning(priv, port, learn_ena); 35944d942354SVladimir Oltean if (rc) 35954d942354SVladimir Oltean return rc; 35964d942354SVladimir Oltean } 35974d942354SVladimir Oltean 35984d942354SVladimir Oltean if (flags.mask & (BR_FLOOD | BR_BCAST_FLOOD)) { 35994d942354SVladimir Oltean rc = sja1105_port_ucast_bcast_flood(priv, port, flags); 36004d942354SVladimir Oltean if (rc) 36014d942354SVladimir Oltean return rc; 36024d942354SVladimir Oltean } 36034d942354SVladimir Oltean 36044d942354SVladimir Oltean /* For chips that can't offload BR_MCAST_FLOOD independently, there 36054d942354SVladimir Oltean * is nothing to do here, we ensured the configuration is in sync by 36064d942354SVladimir Oltean * offloading BR_FLOOD. 36074d942354SVladimir Oltean */ 36084d942354SVladimir Oltean if (flags.mask & BR_MCAST_FLOOD && priv->info->can_limit_mcast_flood) { 36094d942354SVladimir Oltean rc = sja1105_port_mcast_flood(priv, port, flags, 36104d942354SVladimir Oltean extack); 36114d942354SVladimir Oltean if (rc) 36124d942354SVladimir Oltean return rc; 36134d942354SVladimir Oltean } 36144d942354SVladimir Oltean 36154d942354SVladimir Oltean return 0; 36164d942354SVladimir Oltean } 36174d942354SVladimir Oltean 36188aa9ebccSVladimir Oltean static const struct dsa_switch_ops sja1105_switch_ops = { 36198aa9ebccSVladimir Oltean .get_tag_protocol = sja1105_get_tag_protocol, 36208aa9ebccSVladimir Oltean .setup = sja1105_setup, 3621f3097be2SVladimir Oltean .teardown = sja1105_teardown, 36228456721dSVladimir Oltean .set_ageing_time = sja1105_set_ageing_time, 3623c279c726SVladimir Oltean .port_change_mtu = sja1105_change_mtu, 3624c279c726SVladimir Oltean .port_max_mtu = sja1105_get_max_mtu, 3625ad9f299aSVladimir Oltean .phylink_validate = sja1105_phylink_validate, 3626af7cd036SVladimir Oltean .phylink_mac_config = sja1105_mac_config, 36278400cff6SVladimir Oltean .phylink_mac_link_up = sja1105_mac_link_up, 36288400cff6SVladimir Oltean .phylink_mac_link_down = sja1105_mac_link_down, 362952c34e6eSVladimir Oltean .get_strings = sja1105_get_strings, 363052c34e6eSVladimir Oltean .get_ethtool_stats = sja1105_get_ethtool_stats, 363152c34e6eSVladimir Oltean .get_sset_count = sja1105_get_sset_count, 3632bb77f36aSVladimir Oltean .get_ts_info = sja1105_get_ts_info, 3633a68578c2SVladimir Oltean .port_disable = sja1105_port_disable, 3634291d1e72SVladimir Oltean .port_fdb_dump = sja1105_fdb_dump, 3635291d1e72SVladimir Oltean .port_fdb_add = sja1105_fdb_add, 3636291d1e72SVladimir Oltean .port_fdb_del = sja1105_fdb_del, 36378aa9ebccSVladimir Oltean .port_bridge_join = sja1105_bridge_join, 36388aa9ebccSVladimir Oltean .port_bridge_leave = sja1105_bridge_leave, 36394d942354SVladimir Oltean .port_pre_bridge_flags = sja1105_port_pre_bridge_flags, 36404d942354SVladimir Oltean .port_bridge_flags = sja1105_port_bridge_flags, 3641640f763fSVladimir Oltean .port_stp_state_set = sja1105_bridge_stp_state_set, 36426666cebcSVladimir Oltean .port_vlan_filtering = sja1105_vlan_filtering, 36436666cebcSVladimir Oltean .port_vlan_add = sja1105_vlan_add, 36446666cebcSVladimir Oltean .port_vlan_del = sja1105_vlan_del, 3645291d1e72SVladimir Oltean .port_mdb_add = sja1105_mdb_add, 3646291d1e72SVladimir Oltean .port_mdb_del = sja1105_mdb_del, 3647a602afd2SVladimir Oltean .port_hwtstamp_get = sja1105_hwtstamp_get, 3648a602afd2SVladimir Oltean .port_hwtstamp_set = sja1105_hwtstamp_set, 3649f3097be2SVladimir Oltean .port_rxtstamp = sja1105_port_rxtstamp, 365047ed985eSVladimir Oltean .port_txtstamp = sja1105_port_txtstamp, 3651317ab5b8SVladimir Oltean .port_setup_tc = sja1105_port_setup_tc, 3652511e6ca0SVladimir Oltean .port_mirror_add = sja1105_mirror_add, 3653511e6ca0SVladimir Oltean .port_mirror_del = sja1105_mirror_del, 3654a7cc081cSVladimir Oltean .port_policer_add = sja1105_port_policer_add, 3655a7cc081cSVladimir Oltean .port_policer_del = sja1105_port_policer_del, 3656a6af7763SVladimir Oltean .cls_flower_add = sja1105_cls_flower_add, 3657a6af7763SVladimir Oltean .cls_flower_del = sja1105_cls_flower_del, 3658834f8933SVladimir Oltean .cls_flower_stats = sja1105_cls_flower_stats, 3659ac02a451SVladimir Oltean .crosschip_bridge_join = sja1105_crosschip_bridge_join, 3660ac02a451SVladimir Oltean .crosschip_bridge_leave = sja1105_crosschip_bridge_leave, 36612cafa72eSVladimir Oltean .devlink_param_get = sja1105_devlink_param_get, 36622cafa72eSVladimir Oltean .devlink_param_set = sja1105_devlink_param_set, 3663ff4cf8eaSVladimir Oltean .devlink_info_get = sja1105_devlink_info_get, 36648aa9ebccSVladimir Oltean }; 36658aa9ebccSVladimir Oltean 36660b0e2997SVladimir Oltean static const struct of_device_id sja1105_dt_ids[]; 36670b0e2997SVladimir Oltean 36688aa9ebccSVladimir Oltean static int sja1105_check_device_id(struct sja1105_private *priv) 36698aa9ebccSVladimir Oltean { 36708aa9ebccSVladimir Oltean const struct sja1105_regs *regs = priv->info->regs; 36718aa9ebccSVladimir Oltean u8 prod_id[SJA1105_SIZE_DEVICE_ID] = {0}; 36728aa9ebccSVladimir Oltean struct device *dev = &priv->spidev->dev; 36730b0e2997SVladimir Oltean const struct of_device_id *match; 3674dff79620SVladimir Oltean u32 device_id; 36758aa9ebccSVladimir Oltean u64 part_no; 36768aa9ebccSVladimir Oltean int rc; 36778aa9ebccSVladimir Oltean 367834d76e9fSVladimir Oltean rc = sja1105_xfer_u32(priv, SPI_READ, regs->device_id, &device_id, 367934d76e9fSVladimir Oltean NULL); 36808aa9ebccSVladimir Oltean if (rc < 0) 36818aa9ebccSVladimir Oltean return rc; 36828aa9ebccSVladimir Oltean 36831bd44870SVladimir Oltean rc = sja1105_xfer_buf(priv, SPI_READ, regs->prod_id, prod_id, 36841bd44870SVladimir Oltean SJA1105_SIZE_DEVICE_ID); 36858aa9ebccSVladimir Oltean if (rc < 0) 36868aa9ebccSVladimir Oltean return rc; 36878aa9ebccSVladimir Oltean 36888aa9ebccSVladimir Oltean sja1105_unpack(prod_id, &part_no, 19, 4, SJA1105_SIZE_DEVICE_ID); 36898aa9ebccSVladimir Oltean 36905978fac0SNathan Chancellor for (match = sja1105_dt_ids; match->compatible[0]; match++) { 36910b0e2997SVladimir Oltean const struct sja1105_info *info = match->data; 36920b0e2997SVladimir Oltean 36930b0e2997SVladimir Oltean /* Is what's been probed in our match table at all? */ 36940b0e2997SVladimir Oltean if (info->device_id != device_id || info->part_no != part_no) 36950b0e2997SVladimir Oltean continue; 36960b0e2997SVladimir Oltean 36970b0e2997SVladimir Oltean /* But is it what's in the device tree? */ 36980b0e2997SVladimir Oltean if (priv->info->device_id != device_id || 36990b0e2997SVladimir Oltean priv->info->part_no != part_no) { 37000b0e2997SVladimir Oltean dev_warn(dev, "Device tree specifies chip %s but found %s, please fix it!\n", 37010b0e2997SVladimir Oltean priv->info->name, info->name); 37020b0e2997SVladimir Oltean /* It isn't. No problem, pick that up. */ 37030b0e2997SVladimir Oltean priv->info = info; 37048aa9ebccSVladimir Oltean } 37058aa9ebccSVladimir Oltean 37068aa9ebccSVladimir Oltean return 0; 37078aa9ebccSVladimir Oltean } 37088aa9ebccSVladimir Oltean 37090b0e2997SVladimir Oltean dev_err(dev, "Unexpected {device ID, part number}: 0x%x 0x%llx\n", 37100b0e2997SVladimir Oltean device_id, part_no); 37110b0e2997SVladimir Oltean 37120b0e2997SVladimir Oltean return -ENODEV; 37130b0e2997SVladimir Oltean } 37140b0e2997SVladimir Oltean 37158aa9ebccSVladimir Oltean static int sja1105_probe(struct spi_device *spi) 37168aa9ebccSVladimir Oltean { 3717844d7edcSVladimir Oltean struct sja1105_tagger_data *tagger_data; 37188aa9ebccSVladimir Oltean struct device *dev = &spi->dev; 37198aa9ebccSVladimir Oltean struct sja1105_private *priv; 3720718bad0eSVladimir Oltean size_t max_xfer, max_msg; 37218aa9ebccSVladimir Oltean struct dsa_switch *ds; 3722a68578c2SVladimir Oltean int rc, port; 37238aa9ebccSVladimir Oltean 37248aa9ebccSVladimir Oltean if (!dev->of_node) { 37258aa9ebccSVladimir Oltean dev_err(dev, "No DTS bindings for SJA1105 driver\n"); 37268aa9ebccSVladimir Oltean return -EINVAL; 37278aa9ebccSVladimir Oltean } 37288aa9ebccSVladimir Oltean 37298aa9ebccSVladimir Oltean priv = devm_kzalloc(dev, sizeof(struct sja1105_private), GFP_KERNEL); 37308aa9ebccSVladimir Oltean if (!priv) 37318aa9ebccSVladimir Oltean return -ENOMEM; 37328aa9ebccSVladimir Oltean 37338aa9ebccSVladimir Oltean /* Configure the optional reset pin and bring up switch */ 37348aa9ebccSVladimir Oltean priv->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); 37358aa9ebccSVladimir Oltean if (IS_ERR(priv->reset_gpio)) 37368aa9ebccSVladimir Oltean dev_dbg(dev, "reset-gpios not defined, ignoring\n"); 37378aa9ebccSVladimir Oltean else 37388aa9ebccSVladimir Oltean sja1105_hw_reset(priv->reset_gpio, 1, 1); 37398aa9ebccSVladimir Oltean 37408aa9ebccSVladimir Oltean /* Populate our driver private structure (priv) based on 37418aa9ebccSVladimir Oltean * the device tree node that was probed (spi) 37428aa9ebccSVladimir Oltean */ 37438aa9ebccSVladimir Oltean priv->spidev = spi; 37448aa9ebccSVladimir Oltean spi_set_drvdata(spi, priv); 37458aa9ebccSVladimir Oltean 37468aa9ebccSVladimir Oltean /* Configure the SPI bus */ 37478aa9ebccSVladimir Oltean spi->bits_per_word = 8; 37488aa9ebccSVladimir Oltean rc = spi_setup(spi); 37498aa9ebccSVladimir Oltean if (rc < 0) { 37508aa9ebccSVladimir Oltean dev_err(dev, "Could not init SPI\n"); 37518aa9ebccSVladimir Oltean return rc; 37528aa9ebccSVladimir Oltean } 37538aa9ebccSVladimir Oltean 3754718bad0eSVladimir Oltean /* In sja1105_xfer, we send spi_messages composed of two spi_transfers: 3755718bad0eSVladimir Oltean * a small one for the message header and another one for the current 3756718bad0eSVladimir Oltean * chunk of the packed buffer. 3757718bad0eSVladimir Oltean * Check that the restrictions imposed by the SPI controller are 3758718bad0eSVladimir Oltean * respected: the chunk buffer is smaller than the max transfer size, 3759718bad0eSVladimir Oltean * and the total length of the chunk plus its message header is smaller 3760718bad0eSVladimir Oltean * than the max message size. 3761718bad0eSVladimir Oltean * We do that during probe time since the maximum transfer size is a 3762718bad0eSVladimir Oltean * runtime invariant. 3763718bad0eSVladimir Oltean */ 3764718bad0eSVladimir Oltean max_xfer = spi_max_transfer_size(spi); 3765718bad0eSVladimir Oltean max_msg = spi_max_message_size(spi); 3766718bad0eSVladimir Oltean 3767718bad0eSVladimir Oltean /* We need to send at least one 64-bit word of SPI payload per message 3768718bad0eSVladimir Oltean * in order to be able to make useful progress. 3769718bad0eSVladimir Oltean */ 3770718bad0eSVladimir Oltean if (max_msg < SJA1105_SIZE_SPI_MSG_HEADER + 8) { 3771718bad0eSVladimir Oltean dev_err(dev, "SPI master cannot send large enough buffers, aborting\n"); 3772718bad0eSVladimir Oltean return -EINVAL; 3773718bad0eSVladimir Oltean } 3774718bad0eSVladimir Oltean 3775718bad0eSVladimir Oltean priv->max_xfer_len = SJA1105_SIZE_SPI_MSG_MAXLEN; 3776718bad0eSVladimir Oltean if (priv->max_xfer_len > max_xfer) 3777718bad0eSVladimir Oltean priv->max_xfer_len = max_xfer; 3778718bad0eSVladimir Oltean if (priv->max_xfer_len > max_msg - SJA1105_SIZE_SPI_MSG_HEADER) 3779718bad0eSVladimir Oltean priv->max_xfer_len = max_msg - SJA1105_SIZE_SPI_MSG_HEADER; 3780718bad0eSVladimir Oltean 37818aa9ebccSVladimir Oltean priv->info = of_device_get_match_data(dev); 37828aa9ebccSVladimir Oltean 37838aa9ebccSVladimir Oltean /* Detect hardware device */ 37848aa9ebccSVladimir Oltean rc = sja1105_check_device_id(priv); 37858aa9ebccSVladimir Oltean if (rc < 0) { 37868aa9ebccSVladimir Oltean dev_err(dev, "Device ID check failed: %d\n", rc); 37878aa9ebccSVladimir Oltean return rc; 37888aa9ebccSVladimir Oltean } 37898aa9ebccSVladimir Oltean 37908aa9ebccSVladimir Oltean dev_info(dev, "Probed switch chip: %s\n", priv->info->name); 37918aa9ebccSVladimir Oltean 37927e99e347SVivien Didelot ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL); 37938aa9ebccSVladimir Oltean if (!ds) 37948aa9ebccSVladimir Oltean return -ENOMEM; 37958aa9ebccSVladimir Oltean 37967e99e347SVivien Didelot ds->dev = dev; 37973e77e59bSVladimir Oltean ds->num_ports = priv->info->num_ports; 37988aa9ebccSVladimir Oltean ds->ops = &sja1105_switch_ops; 37998aa9ebccSVladimir Oltean ds->priv = priv; 38008aa9ebccSVladimir Oltean priv->ds = ds; 38018aa9ebccSVladimir Oltean 3802844d7edcSVladimir Oltean tagger_data = &priv->tagger_data; 3803844d7edcSVladimir Oltean 3804d5a619bfSVivien Didelot mutex_init(&priv->ptp_data.lock); 3805d5a619bfSVivien Didelot mutex_init(&priv->mgmt_lock); 3806d5a619bfSVivien Didelot 38075899ee36SVladimir Oltean priv->dsa_8021q_ctx = devm_kzalloc(dev, sizeof(*priv->dsa_8021q_ctx), 38085899ee36SVladimir Oltean GFP_KERNEL); 38095899ee36SVladimir Oltean if (!priv->dsa_8021q_ctx) 38105899ee36SVladimir Oltean return -ENOMEM; 38115899ee36SVladimir Oltean 38125899ee36SVladimir Oltean priv->dsa_8021q_ctx->ops = &sja1105_dsa_8021q_ops; 3813bbed0bbdSVladimir Oltean priv->dsa_8021q_ctx->proto = htons(ETH_P_8021Q); 38145899ee36SVladimir Oltean priv->dsa_8021q_ctx->ds = ds; 38155899ee36SVladimir Oltean 38165899ee36SVladimir Oltean INIT_LIST_HEAD(&priv->dsa_8021q_ctx->crosschip_links); 3817ec5ae610SVladimir Oltean INIT_LIST_HEAD(&priv->bridge_vlans); 3818ec5ae610SVladimir Oltean INIT_LIST_HEAD(&priv->dsa_8021q_vlans); 3819ac02a451SVladimir Oltean 3820d5a619bfSVivien Didelot sja1105_tas_setup(ds); 3821a6af7763SVladimir Oltean sja1105_flower_setup(ds); 3822d5a619bfSVivien Didelot 3823d5a619bfSVivien Didelot rc = dsa_register_switch(priv->ds); 3824d5a619bfSVivien Didelot if (rc) 3825d5a619bfSVivien Didelot return rc; 3826d5a619bfSVivien Didelot 38274d752508SVladimir Oltean if (IS_ENABLED(CONFIG_NET_SCH_CBS)) { 38284d752508SVladimir Oltean priv->cbs = devm_kcalloc(dev, priv->info->num_cbs_shapers, 38294d752508SVladimir Oltean sizeof(struct sja1105_cbs_entry), 38304d752508SVladimir Oltean GFP_KERNEL); 3831dc596e3fSVladimir Oltean if (!priv->cbs) { 3832dc596e3fSVladimir Oltean rc = -ENOMEM; 3833dc596e3fSVladimir Oltean goto out_unregister_switch; 3834dc596e3fSVladimir Oltean } 38354d752508SVladimir Oltean } 38364d752508SVladimir Oltean 3837227d07a0SVladimir Oltean /* Connections between dsa_port and sja1105_port */ 3838542043e9SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 3839a68578c2SVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 3840a68578c2SVladimir Oltean struct dsa_port *dp = dsa_to_port(ds, port); 3841a68578c2SVladimir Oltean struct net_device *slave; 384284eeb5d4SVladimir Oltean int subvlan; 3843227d07a0SVladimir Oltean 3844a68578c2SVladimir Oltean if (!dsa_is_user_port(ds, port)) 3845a68578c2SVladimir Oltean continue; 3846a68578c2SVladimir Oltean 3847a68578c2SVladimir Oltean dp->priv = sp; 3848a68578c2SVladimir Oltean sp->dp = dp; 3849844d7edcSVladimir Oltean sp->data = tagger_data; 3850a68578c2SVladimir Oltean slave = dp->slave; 3851a68578c2SVladimir Oltean kthread_init_work(&sp->xmit_work, sja1105_port_deferred_xmit); 3852a68578c2SVladimir Oltean sp->xmit_worker = kthread_create_worker(0, "%s_xmit", 3853a68578c2SVladimir Oltean slave->name); 3854a68578c2SVladimir Oltean if (IS_ERR(sp->xmit_worker)) { 3855a68578c2SVladimir Oltean rc = PTR_ERR(sp->xmit_worker); 3856a68578c2SVladimir Oltean dev_err(ds->dev, 3857a68578c2SVladimir Oltean "failed to create deferred xmit thread: %d\n", 3858a68578c2SVladimir Oltean rc); 3859dc596e3fSVladimir Oltean goto out_destroy_workers; 3860a68578c2SVladimir Oltean } 3861a68578c2SVladimir Oltean skb_queue_head_init(&sp->xmit_queue); 386238b5beeaSVladimir Oltean sp->xmit_tpid = ETH_P_SJA1105; 386384eeb5d4SVladimir Oltean 386484eeb5d4SVladimir Oltean for (subvlan = 0; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++) 386584eeb5d4SVladimir Oltean sp->subvlan_map[subvlan] = VLAN_N_VID; 3866227d07a0SVladimir Oltean } 3867227d07a0SVladimir Oltean 3868d5a619bfSVivien Didelot return 0; 3869dc596e3fSVladimir Oltean 3870dc596e3fSVladimir Oltean out_destroy_workers: 3871a68578c2SVladimir Oltean while (port-- > 0) { 3872a68578c2SVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 3873a68578c2SVladimir Oltean 3874a68578c2SVladimir Oltean if (!dsa_is_user_port(ds, port)) 3875a68578c2SVladimir Oltean continue; 3876a68578c2SVladimir Oltean 3877a68578c2SVladimir Oltean kthread_destroy_worker(sp->xmit_worker); 3878a68578c2SVladimir Oltean } 3879dc596e3fSVladimir Oltean 3880dc596e3fSVladimir Oltean out_unregister_switch: 3881dc596e3fSVladimir Oltean dsa_unregister_switch(ds); 3882dc596e3fSVladimir Oltean 3883a68578c2SVladimir Oltean return rc; 38848aa9ebccSVladimir Oltean } 38858aa9ebccSVladimir Oltean 38868aa9ebccSVladimir Oltean static int sja1105_remove(struct spi_device *spi) 38878aa9ebccSVladimir Oltean { 38888aa9ebccSVladimir Oltean struct sja1105_private *priv = spi_get_drvdata(spi); 38898aa9ebccSVladimir Oltean 38908aa9ebccSVladimir Oltean dsa_unregister_switch(priv->ds); 38918aa9ebccSVladimir Oltean return 0; 38928aa9ebccSVladimir Oltean } 38938aa9ebccSVladimir Oltean 38948aa9ebccSVladimir Oltean static const struct of_device_id sja1105_dt_ids[] = { 38958aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105e", .data = &sja1105e_info }, 38968aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105t", .data = &sja1105t_info }, 38978aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105p", .data = &sja1105p_info }, 38988aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105q", .data = &sja1105q_info }, 38998aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105r", .data = &sja1105r_info }, 39008aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105s", .data = &sja1105s_info }, 39013e77e59bSVladimir Oltean { .compatible = "nxp,sja1110a", .data = &sja1110a_info }, 39023e77e59bSVladimir Oltean { .compatible = "nxp,sja1110b", .data = &sja1110b_info }, 39033e77e59bSVladimir Oltean { .compatible = "nxp,sja1110c", .data = &sja1110c_info }, 39043e77e59bSVladimir Oltean { .compatible = "nxp,sja1110d", .data = &sja1110d_info }, 39058aa9ebccSVladimir Oltean { /* sentinel */ }, 39068aa9ebccSVladimir Oltean }; 39078aa9ebccSVladimir Oltean MODULE_DEVICE_TABLE(of, sja1105_dt_ids); 39088aa9ebccSVladimir Oltean 39098aa9ebccSVladimir Oltean static struct spi_driver sja1105_driver = { 39108aa9ebccSVladimir Oltean .driver = { 39118aa9ebccSVladimir Oltean .name = "sja1105", 39128aa9ebccSVladimir Oltean .owner = THIS_MODULE, 39138aa9ebccSVladimir Oltean .of_match_table = of_match_ptr(sja1105_dt_ids), 39148aa9ebccSVladimir Oltean }, 39158aa9ebccSVladimir Oltean .probe = sja1105_probe, 39168aa9ebccSVladimir Oltean .remove = sja1105_remove, 39178aa9ebccSVladimir Oltean }; 39188aa9ebccSVladimir Oltean 39198aa9ebccSVladimir Oltean module_spi_driver(sja1105_driver); 39208aa9ebccSVladimir Oltean 39218aa9ebccSVladimir Oltean MODULE_AUTHOR("Vladimir Oltean <olteanv@gmail.com>"); 39228aa9ebccSVladimir Oltean MODULE_AUTHOR("Georg Waibel <georg.waibel@sensor-technik.de>"); 39238aa9ebccSVladimir Oltean MODULE_DESCRIPTION("SJA1105 Driver"); 39248aa9ebccSVladimir Oltean MODULE_LICENSE("GPL v2"); 3925