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 60bef0746cSVladimir Oltean static int sja1105_is_vlan_configured(struct sja1105_private *priv, u16 vid) 61bef0746cSVladimir Oltean { 62bef0746cSVladimir Oltean struct sja1105_vlan_lookup_entry *vlan; 63bef0746cSVladimir Oltean int count, i; 64bef0746cSVladimir Oltean 65bef0746cSVladimir Oltean vlan = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entries; 66bef0746cSVladimir Oltean count = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entry_count; 67bef0746cSVladimir Oltean 68bef0746cSVladimir Oltean for (i = 0; i < count; i++) 69bef0746cSVladimir Oltean if (vlan[i].vlanid == vid) 70bef0746cSVladimir Oltean return i; 71bef0746cSVladimir Oltean 72bef0746cSVladimir Oltean /* Return an invalid entry index if not found */ 73bef0746cSVladimir Oltean return -1; 74bef0746cSVladimir Oltean } 75bef0746cSVladimir Oltean 76bef0746cSVladimir Oltean static int sja1105_drop_untagged(struct dsa_switch *ds, int port, bool drop) 77bef0746cSVladimir Oltean { 78bef0746cSVladimir Oltean struct sja1105_private *priv = ds->priv; 79bef0746cSVladimir Oltean struct sja1105_mac_config_entry *mac; 80bef0746cSVladimir Oltean 81bef0746cSVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 82bef0746cSVladimir Oltean 83bef0746cSVladimir Oltean if (mac[port].drpuntag == drop) 84bef0746cSVladimir Oltean return 0; 85bef0746cSVladimir Oltean 86bef0746cSVladimir Oltean mac[port].drpuntag = drop; 87bef0746cSVladimir Oltean 88bef0746cSVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, 89bef0746cSVladimir Oltean &mac[port], true); 90bef0746cSVladimir Oltean } 91bef0746cSVladimir Oltean 92cde8078eSVladimir Oltean static int sja1105_pvid_apply(struct sja1105_private *priv, int port, u16 pvid) 93cde8078eSVladimir Oltean { 94cde8078eSVladimir Oltean struct sja1105_mac_config_entry *mac; 95cde8078eSVladimir Oltean 96cde8078eSVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 97cde8078eSVladimir Oltean 98cde8078eSVladimir Oltean if (mac[port].vlanid == pvid) 99cde8078eSVladimir Oltean return 0; 100cde8078eSVladimir Oltean 101cde8078eSVladimir Oltean mac[port].vlanid = pvid; 102cde8078eSVladimir Oltean 103cde8078eSVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, 104cde8078eSVladimir Oltean &mac[port], true); 105cde8078eSVladimir Oltean } 106cde8078eSVladimir Oltean 107cde8078eSVladimir Oltean static int sja1105_commit_pvid(struct dsa_switch *ds, int port) 108cde8078eSVladimir Oltean { 109cde8078eSVladimir Oltean struct dsa_port *dp = dsa_to_port(ds, port); 110cde8078eSVladimir Oltean struct sja1105_private *priv = ds->priv; 111bef0746cSVladimir Oltean struct sja1105_vlan_lookup_entry *vlan; 112bef0746cSVladimir Oltean bool drop_untagged = false; 113bef0746cSVladimir Oltean int match, rc; 114cde8078eSVladimir Oltean u16 pvid; 115cde8078eSVladimir Oltean 116cde8078eSVladimir Oltean if (dp->bridge_dev && br_vlan_enabled(dp->bridge_dev)) 117cde8078eSVladimir Oltean pvid = priv->bridge_pvid[port]; 118cde8078eSVladimir Oltean else 119cde8078eSVladimir Oltean pvid = priv->tag_8021q_pvid[port]; 120cde8078eSVladimir Oltean 121bef0746cSVladimir Oltean rc = sja1105_pvid_apply(priv, port, pvid); 122bef0746cSVladimir Oltean if (rc) 123bef0746cSVladimir Oltean return rc; 124bef0746cSVladimir Oltean 125bef0746cSVladimir Oltean vlan = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entries; 126bef0746cSVladimir Oltean 127bef0746cSVladimir Oltean match = sja1105_is_vlan_configured(priv, pvid); 128bef0746cSVladimir Oltean 129bef0746cSVladimir Oltean if (match < 0 || !(vlan[match].vmemb_port & BIT(port))) 130bef0746cSVladimir Oltean drop_untagged = true; 131bef0746cSVladimir Oltean 132bef0746cSVladimir Oltean return sja1105_drop_untagged(ds, port, drop_untagged); 133cde8078eSVladimir Oltean } 134cde8078eSVladimir Oltean 1358aa9ebccSVladimir Oltean static int sja1105_init_mac_settings(struct sja1105_private *priv) 1368aa9ebccSVladimir Oltean { 1378aa9ebccSVladimir Oltean struct sja1105_mac_config_entry default_mac = { 1388aa9ebccSVladimir Oltean /* Enable all 8 priority queues on egress. 1398aa9ebccSVladimir Oltean * Every queue i holds top[i] - base[i] frames. 1408aa9ebccSVladimir Oltean * Sum of top[i] - base[i] is 511 (max hardware limit). 1418aa9ebccSVladimir Oltean */ 1428aa9ebccSVladimir Oltean .top = {0x3F, 0x7F, 0xBF, 0xFF, 0x13F, 0x17F, 0x1BF, 0x1FF}, 1438aa9ebccSVladimir Oltean .base = {0x0, 0x40, 0x80, 0xC0, 0x100, 0x140, 0x180, 0x1C0}, 1448aa9ebccSVladimir Oltean .enabled = {true, true, true, true, true, true, true, true}, 1458aa9ebccSVladimir Oltean /* Keep standard IFG of 12 bytes on egress. */ 1468aa9ebccSVladimir Oltean .ifg = 0, 1478aa9ebccSVladimir Oltean /* Always put the MAC speed in automatic mode, where it can be 1481fd4a173SVladimir Oltean * adjusted at runtime by PHYLINK. 1498aa9ebccSVladimir Oltean */ 15041fed17fSVladimir Oltean .speed = priv->info->port_speed[SJA1105_SPEED_AUTO], 1518aa9ebccSVladimir Oltean /* No static correction for 1-step 1588 events */ 1528aa9ebccSVladimir Oltean .tp_delin = 0, 1538aa9ebccSVladimir Oltean .tp_delout = 0, 1548aa9ebccSVladimir Oltean /* Disable aging for critical TTEthernet traffic */ 1558aa9ebccSVladimir Oltean .maxage = 0xFF, 1568aa9ebccSVladimir Oltean /* Internal VLAN (pvid) to apply to untagged ingress */ 1578aa9ebccSVladimir Oltean .vlanprio = 0, 158e3502b82SVladimir Oltean .vlanid = 1, 1598aa9ebccSVladimir Oltean .ing_mirr = false, 1608aa9ebccSVladimir Oltean .egr_mirr = false, 1618aa9ebccSVladimir Oltean /* Don't drop traffic with other EtherType than ETH_P_IP */ 1628aa9ebccSVladimir Oltean .drpnona664 = false, 1638aa9ebccSVladimir Oltean /* Don't drop double-tagged traffic */ 1648aa9ebccSVladimir Oltean .drpdtag = false, 1658aa9ebccSVladimir Oltean /* Don't drop untagged traffic */ 1668aa9ebccSVladimir Oltean .drpuntag = false, 1678aa9ebccSVladimir Oltean /* Don't retag 802.1p (VID 0) traffic with the pvid */ 1688aa9ebccSVladimir Oltean .retag = false, 169640f763fSVladimir Oltean /* Disable learning and I/O on user ports by default - 170640f763fSVladimir Oltean * STP will enable it. 171640f763fSVladimir Oltean */ 172640f763fSVladimir Oltean .dyn_learn = false, 1738aa9ebccSVladimir Oltean .egress = false, 1748aa9ebccSVladimir Oltean .ingress = false, 1758aa9ebccSVladimir Oltean }; 1768aa9ebccSVladimir Oltean struct sja1105_mac_config_entry *mac; 177542043e9SVladimir Oltean struct dsa_switch *ds = priv->ds; 1788aa9ebccSVladimir Oltean struct sja1105_table *table; 1798aa9ebccSVladimir Oltean int i; 1808aa9ebccSVladimir Oltean 1818aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_MAC_CONFIG]; 1828aa9ebccSVladimir Oltean 1838aa9ebccSVladimir Oltean /* Discard previous MAC Configuration Table */ 1848aa9ebccSVladimir Oltean if (table->entry_count) { 1858aa9ebccSVladimir Oltean kfree(table->entries); 1868aa9ebccSVladimir Oltean table->entry_count = 0; 1878aa9ebccSVladimir Oltean } 1888aa9ebccSVladimir Oltean 189fd6f2c25SVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 1908aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 1918aa9ebccSVladimir Oltean if (!table->entries) 1928aa9ebccSVladimir Oltean return -ENOMEM; 1938aa9ebccSVladimir Oltean 194fd6f2c25SVladimir Oltean table->entry_count = table->ops->max_entry_count; 1958aa9ebccSVladimir Oltean 1968aa9ebccSVladimir Oltean mac = table->entries; 1978aa9ebccSVladimir Oltean 198542043e9SVladimir Oltean for (i = 0; i < ds->num_ports; i++) { 1998aa9ebccSVladimir Oltean mac[i] = default_mac; 200b0b33b04SVladimir Oltean 201b0b33b04SVladimir Oltean /* Let sja1105_bridge_stp_state_set() keep address learning 202*81d45898SVladimir Oltean * enabled for the DSA ports. CPU ports use software-assisted 203*81d45898SVladimir Oltean * learning to ensure that only FDB entries belonging to the 204*81d45898SVladimir Oltean * bridge are learned, and that they are learned towards all 205*81d45898SVladimir Oltean * CPU ports in a cross-chip topology if multiple CPU ports 206*81d45898SVladimir Oltean * exist. 207640f763fSVladimir Oltean */ 208*81d45898SVladimir Oltean if (dsa_is_dsa_port(ds, i)) 209b0b33b04SVladimir Oltean priv->learn_ena |= BIT(i); 210640f763fSVladimir Oltean } 2118aa9ebccSVladimir Oltean 2128aa9ebccSVladimir Oltean return 0; 2138aa9ebccSVladimir Oltean } 2148aa9ebccSVladimir Oltean 2155d645df9SVladimir Oltean static int sja1105_init_mii_settings(struct sja1105_private *priv) 2168aa9ebccSVladimir Oltean { 2178aa9ebccSVladimir Oltean struct device *dev = &priv->spidev->dev; 2188aa9ebccSVladimir Oltean struct sja1105_xmii_params_entry *mii; 219542043e9SVladimir Oltean struct dsa_switch *ds = priv->ds; 2208aa9ebccSVladimir Oltean struct sja1105_table *table; 2218aa9ebccSVladimir Oltean int i; 2228aa9ebccSVladimir Oltean 2238aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_XMII_PARAMS]; 2248aa9ebccSVladimir Oltean 2258aa9ebccSVladimir Oltean /* Discard previous xMII Mode Parameters Table */ 2268aa9ebccSVladimir Oltean if (table->entry_count) { 2278aa9ebccSVladimir Oltean kfree(table->entries); 2288aa9ebccSVladimir Oltean table->entry_count = 0; 2298aa9ebccSVladimir Oltean } 2308aa9ebccSVladimir Oltean 231fd6f2c25SVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 2328aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 2338aa9ebccSVladimir Oltean if (!table->entries) 2348aa9ebccSVladimir Oltean return -ENOMEM; 2358aa9ebccSVladimir Oltean 2361fd4a173SVladimir Oltean /* Override table based on PHYLINK DT bindings */ 237fd6f2c25SVladimir Oltean table->entry_count = table->ops->max_entry_count; 2388aa9ebccSVladimir Oltean 2398aa9ebccSVladimir Oltean mii = table->entries; 2408aa9ebccSVladimir Oltean 241542043e9SVladimir Oltean for (i = 0; i < ds->num_ports; i++) { 2425d645df9SVladimir Oltean sja1105_mii_role_t role = XMII_MAC; 2435d645df9SVladimir Oltean 244ee9d0cb6SVladimir Oltean if (dsa_is_unused_port(priv->ds, i)) 245ee9d0cb6SVladimir Oltean continue; 246ee9d0cb6SVladimir Oltean 2475d645df9SVladimir Oltean switch (priv->phy_mode[i]) { 2485a8f0974SVladimir Oltean case PHY_INTERFACE_MODE_INTERNAL: 2495a8f0974SVladimir Oltean if (priv->info->internal_phy[i] == SJA1105_NO_PHY) 2505a8f0974SVladimir Oltean goto unsupported; 2515a8f0974SVladimir Oltean 2525a8f0974SVladimir Oltean mii->xmii_mode[i] = XMII_MODE_MII; 2535a8f0974SVladimir Oltean if (priv->info->internal_phy[i] == SJA1105_PHY_BASE_TX) 2545a8f0974SVladimir Oltean mii->special[i] = true; 2555a8f0974SVladimir Oltean 2565a8f0974SVladimir Oltean break; 2575d645df9SVladimir Oltean case PHY_INTERFACE_MODE_REVMII: 2585d645df9SVladimir Oltean role = XMII_PHY; 2595d645df9SVladimir Oltean fallthrough; 2608aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_MII: 26191a05078SVladimir Oltean if (!priv->info->supports_mii[i]) 26291a05078SVladimir Oltean goto unsupported; 26391a05078SVladimir Oltean 2648aa9ebccSVladimir Oltean mii->xmii_mode[i] = XMII_MODE_MII; 2658aa9ebccSVladimir Oltean break; 2665d645df9SVladimir Oltean case PHY_INTERFACE_MODE_REVRMII: 2675d645df9SVladimir Oltean role = XMII_PHY; 2685d645df9SVladimir Oltean fallthrough; 2698aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RMII: 27091a05078SVladimir Oltean if (!priv->info->supports_rmii[i]) 27191a05078SVladimir Oltean goto unsupported; 27291a05078SVladimir Oltean 2738aa9ebccSVladimir Oltean mii->xmii_mode[i] = XMII_MODE_RMII; 2748aa9ebccSVladimir Oltean break; 2758aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RGMII: 2768aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RGMII_ID: 2778aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RGMII_RXID: 2788aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RGMII_TXID: 27991a05078SVladimir Oltean if (!priv->info->supports_rgmii[i]) 28091a05078SVladimir Oltean goto unsupported; 28191a05078SVladimir Oltean 2828aa9ebccSVladimir Oltean mii->xmii_mode[i] = XMII_MODE_RGMII; 2838aa9ebccSVladimir Oltean break; 284ffe10e67SVladimir Oltean case PHY_INTERFACE_MODE_SGMII: 28591a05078SVladimir Oltean if (!priv->info->supports_sgmii[i]) 28691a05078SVladimir Oltean goto unsupported; 28791a05078SVladimir Oltean 288ffe10e67SVladimir Oltean mii->xmii_mode[i] = XMII_MODE_SGMII; 289ece578bcSVladimir Oltean mii->special[i] = true; 290ffe10e67SVladimir Oltean break; 29191a05078SVladimir Oltean case PHY_INTERFACE_MODE_2500BASEX: 29291a05078SVladimir Oltean if (!priv->info->supports_2500basex[i]) 29391a05078SVladimir Oltean goto unsupported; 29491a05078SVladimir Oltean 29591a05078SVladimir Oltean mii->xmii_mode[i] = XMII_MODE_SGMII; 296ece578bcSVladimir Oltean mii->special[i] = true; 29791a05078SVladimir Oltean break; 29891a05078SVladimir Oltean unsupported: 2998aa9ebccSVladimir Oltean default: 30091a05078SVladimir Oltean dev_err(dev, "Unsupported PHY mode %s on port %d!\n", 3015d645df9SVladimir Oltean phy_modes(priv->phy_mode[i]), i); 3026729188dSVladimir Oltean return -EINVAL; 3038aa9ebccSVladimir Oltean } 3048aa9ebccSVladimir Oltean 3055d645df9SVladimir Oltean mii->phy_mac[i] = role; 3068aa9ebccSVladimir Oltean } 3078aa9ebccSVladimir Oltean return 0; 3088aa9ebccSVladimir Oltean } 3098aa9ebccSVladimir Oltean 3108aa9ebccSVladimir Oltean static int sja1105_init_static_fdb(struct sja1105_private *priv) 3118aa9ebccSVladimir Oltean { 3124d942354SVladimir Oltean struct sja1105_l2_lookup_entry *l2_lookup; 3138aa9ebccSVladimir Oltean struct sja1105_table *table; 3144d942354SVladimir Oltean int port; 3158aa9ebccSVladimir Oltean 3168aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; 3178aa9ebccSVladimir Oltean 3184d942354SVladimir Oltean /* We only populate the FDB table through dynamic L2 Address Lookup 3194d942354SVladimir Oltean * entries, except for a special entry at the end which is a catch-all 3204d942354SVladimir Oltean * for unknown multicast and will be used to control flooding domain. 321291d1e72SVladimir Oltean */ 3228aa9ebccSVladimir Oltean if (table->entry_count) { 3238aa9ebccSVladimir Oltean kfree(table->entries); 3248aa9ebccSVladimir Oltean table->entry_count = 0; 3258aa9ebccSVladimir Oltean } 3264d942354SVladimir Oltean 3274d942354SVladimir Oltean if (!priv->info->can_limit_mcast_flood) 3284d942354SVladimir Oltean return 0; 3294d942354SVladimir Oltean 3304d942354SVladimir Oltean table->entries = kcalloc(1, table->ops->unpacked_entry_size, 3314d942354SVladimir Oltean GFP_KERNEL); 3324d942354SVladimir Oltean if (!table->entries) 3334d942354SVladimir Oltean return -ENOMEM; 3344d942354SVladimir Oltean 3354d942354SVladimir Oltean table->entry_count = 1; 3364d942354SVladimir Oltean l2_lookup = table->entries; 3374d942354SVladimir Oltean 3384d942354SVladimir Oltean /* All L2 multicast addresses have an odd first octet */ 3394d942354SVladimir Oltean l2_lookup[0].macaddr = SJA1105_UNKNOWN_MULTICAST; 3404d942354SVladimir Oltean l2_lookup[0].mask_macaddr = SJA1105_UNKNOWN_MULTICAST; 3414d942354SVladimir Oltean l2_lookup[0].lockeds = true; 3424d942354SVladimir Oltean l2_lookup[0].index = SJA1105_MAX_L2_LOOKUP_COUNT - 1; 3434d942354SVladimir Oltean 3444d942354SVladimir Oltean /* Flood multicast to every port by default */ 3454d942354SVladimir Oltean for (port = 0; port < priv->ds->num_ports; port++) 3464d942354SVladimir Oltean if (!dsa_is_unused_port(priv->ds, port)) 3474d942354SVladimir Oltean l2_lookup[0].destports |= BIT(port); 3484d942354SVladimir Oltean 3498aa9ebccSVladimir Oltean return 0; 3508aa9ebccSVladimir Oltean } 3518aa9ebccSVladimir Oltean 3528aa9ebccSVladimir Oltean static int sja1105_init_l2_lookup_params(struct sja1105_private *priv) 3538aa9ebccSVladimir Oltean { 3548aa9ebccSVladimir Oltean struct sja1105_l2_lookup_params_entry default_l2_lookup_params = { 3558456721dSVladimir Oltean /* Learned FDB entries are forgotten after 300 seconds */ 3568456721dSVladimir Oltean .maxage = SJA1105_AGEING_TIME_MS(300000), 3578aa9ebccSVladimir Oltean /* All entries within a FDB bin are available for learning */ 3588aa9ebccSVladimir Oltean .dyn_tbsz = SJA1105ET_FDB_BIN_SIZE, 3591da73821SVladimir Oltean /* And the P/Q/R/S equivalent setting: */ 3601da73821SVladimir Oltean .start_dynspc = 0, 3618aa9ebccSVladimir Oltean /* 2^8 + 2^5 + 2^3 + 2^2 + 2^1 + 1 in Koopman notation */ 3628aa9ebccSVladimir Oltean .poly = 0x97, 3638aa9ebccSVladimir Oltean /* This selects between Independent VLAN Learning (IVL) and 3648aa9ebccSVladimir Oltean * Shared VLAN Learning (SVL) 3658aa9ebccSVladimir Oltean */ 3666d7c7d94SVladimir Oltean .shared_learn = true, 3678aa9ebccSVladimir Oltean /* Don't discard management traffic based on ENFPORT - 3688aa9ebccSVladimir Oltean * we don't perform SMAC port enforcement anyway, so 3698aa9ebccSVladimir Oltean * what we are setting here doesn't matter. 3708aa9ebccSVladimir Oltean */ 3718aa9ebccSVladimir Oltean .no_enf_hostprt = false, 3728aa9ebccSVladimir Oltean /* Don't learn SMAC for mac_fltres1 and mac_fltres0. 3738aa9ebccSVladimir Oltean * Maybe correlate with no_linklocal_learn from bridge driver? 3748aa9ebccSVladimir Oltean */ 3758aa9ebccSVladimir Oltean .no_mgmt_learn = true, 3761da73821SVladimir Oltean /* P/Q/R/S only */ 3771da73821SVladimir Oltean .use_static = true, 3781da73821SVladimir Oltean /* Dynamically learned FDB entries can overwrite other (older) 3791da73821SVladimir Oltean * dynamic FDB entries 3801da73821SVladimir Oltean */ 3811da73821SVladimir Oltean .owr_dyn = true, 3821da73821SVladimir Oltean .drpnolearn = true, 3838aa9ebccSVladimir Oltean }; 384542043e9SVladimir Oltean struct dsa_switch *ds = priv->ds; 385f238fef1SVladimir Oltean int port, num_used_ports = 0; 386542043e9SVladimir Oltean struct sja1105_table *table; 387542043e9SVladimir Oltean u64 max_fdb_entries; 388542043e9SVladimir Oltean 389542043e9SVladimir Oltean for (port = 0; port < ds->num_ports; port++) 390f238fef1SVladimir Oltean if (!dsa_is_unused_port(ds, port)) 391f238fef1SVladimir Oltean num_used_ports++; 392f238fef1SVladimir Oltean 393f238fef1SVladimir Oltean max_fdb_entries = SJA1105_MAX_L2_LOOKUP_COUNT / num_used_ports; 394f238fef1SVladimir Oltean 395f238fef1SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 396f238fef1SVladimir Oltean if (dsa_is_unused_port(ds, port)) 397f238fef1SVladimir Oltean continue; 398f238fef1SVladimir Oltean 399542043e9SVladimir Oltean default_l2_lookup_params.maxaddrp[port] = max_fdb_entries; 400f238fef1SVladimir Oltean } 4018aa9ebccSVladimir Oltean 4028aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS]; 4038aa9ebccSVladimir Oltean 4048aa9ebccSVladimir Oltean if (table->entry_count) { 4058aa9ebccSVladimir Oltean kfree(table->entries); 4068aa9ebccSVladimir Oltean table->entry_count = 0; 4078aa9ebccSVladimir Oltean } 4088aa9ebccSVladimir Oltean 409fd6f2c25SVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 4108aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 4118aa9ebccSVladimir Oltean if (!table->entries) 4128aa9ebccSVladimir Oltean return -ENOMEM; 4138aa9ebccSVladimir Oltean 414fd6f2c25SVladimir Oltean table->entry_count = table->ops->max_entry_count; 4158aa9ebccSVladimir Oltean 4168aa9ebccSVladimir Oltean /* This table only has a single entry */ 4178aa9ebccSVladimir Oltean ((struct sja1105_l2_lookup_params_entry *)table->entries)[0] = 4188aa9ebccSVladimir Oltean default_l2_lookup_params; 4198aa9ebccSVladimir Oltean 4208aa9ebccSVladimir Oltean return 0; 4218aa9ebccSVladimir Oltean } 4228aa9ebccSVladimir Oltean 423ed040abcSVladimir Oltean /* Set up a default VLAN for untagged traffic injected from the CPU 424ed040abcSVladimir Oltean * using management routes (e.g. STP, PTP) as opposed to tag_8021q. 425ed040abcSVladimir Oltean * All DT-defined ports are members of this VLAN, and there are no 426ed040abcSVladimir Oltean * restrictions on forwarding (since the CPU selects the destination). 427ed040abcSVladimir Oltean * Frames from this VLAN will always be transmitted as untagged, and 428ed040abcSVladimir Oltean * neither the bridge nor the 8021q module cannot create this VLAN ID. 429ed040abcSVladimir Oltean */ 4308aa9ebccSVladimir Oltean static int sja1105_init_static_vlan(struct sja1105_private *priv) 4318aa9ebccSVladimir Oltean { 4328aa9ebccSVladimir Oltean struct sja1105_table *table; 4338aa9ebccSVladimir Oltean struct sja1105_vlan_lookup_entry pvid = { 4343e77e59bSVladimir Oltean .type_entry = SJA1110_VLAN_D_TAG, 4358aa9ebccSVladimir Oltean .ving_mirr = 0, 4368aa9ebccSVladimir Oltean .vegr_mirr = 0, 4378aa9ebccSVladimir Oltean .vmemb_port = 0, 4388aa9ebccSVladimir Oltean .vlan_bc = 0, 4398aa9ebccSVladimir Oltean .tag_port = 0, 440ed040abcSVladimir Oltean .vlanid = SJA1105_DEFAULT_VLAN, 4418aa9ebccSVladimir Oltean }; 442ec5ae610SVladimir Oltean struct dsa_switch *ds = priv->ds; 443ec5ae610SVladimir Oltean int port; 4448aa9ebccSVladimir Oltean 4458aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP]; 4468aa9ebccSVladimir Oltean 4478aa9ebccSVladimir Oltean if (table->entry_count) { 4488aa9ebccSVladimir Oltean kfree(table->entries); 4498aa9ebccSVladimir Oltean table->entry_count = 0; 4508aa9ebccSVladimir Oltean } 4518aa9ebccSVladimir Oltean 452c75857b0SZheng Yongjun table->entries = kzalloc(table->ops->unpacked_entry_size, 4538aa9ebccSVladimir Oltean GFP_KERNEL); 4548aa9ebccSVladimir Oltean if (!table->entries) 4558aa9ebccSVladimir Oltean return -ENOMEM; 4568aa9ebccSVladimir Oltean 4578aa9ebccSVladimir Oltean table->entry_count = 1; 4588aa9ebccSVladimir Oltean 459ec5ae610SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 460ec5ae610SVladimir Oltean if (dsa_is_unused_port(ds, port)) 461ec5ae610SVladimir Oltean continue; 462ec5ae610SVladimir Oltean 463ec5ae610SVladimir Oltean pvid.vmemb_port |= BIT(port); 464ec5ae610SVladimir Oltean pvid.vlan_bc |= BIT(port); 465ec5ae610SVladimir Oltean pvid.tag_port &= ~BIT(port); 466ec5ae610SVladimir Oltean 467c5130029SVladimir Oltean if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) { 4686dfd23d3SVladimir Oltean priv->tag_8021q_pvid[port] = SJA1105_DEFAULT_VLAN; 4696dfd23d3SVladimir Oltean priv->bridge_pvid[port] = SJA1105_DEFAULT_VLAN; 4706dfd23d3SVladimir Oltean } 4718aa9ebccSVladimir Oltean } 4728aa9ebccSVladimir Oltean 4738aa9ebccSVladimir Oltean ((struct sja1105_vlan_lookup_entry *)table->entries)[0] = pvid; 4748aa9ebccSVladimir Oltean return 0; 4758aa9ebccSVladimir Oltean } 4768aa9ebccSVladimir Oltean 4778aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding(struct sja1105_private *priv) 4788aa9ebccSVladimir Oltean { 4798aa9ebccSVladimir Oltean struct sja1105_l2_forwarding_entry *l2fwd; 480542043e9SVladimir Oltean struct dsa_switch *ds = priv->ds; 4810f9b762cSVladimir Oltean struct dsa_switch_tree *dst; 4828aa9ebccSVladimir Oltean struct sja1105_table *table; 4830f9b762cSVladimir Oltean struct dsa_link *dl; 4843fa21270SVladimir Oltean int port, tc; 4853fa21270SVladimir Oltean int from, to; 4868aa9ebccSVladimir Oltean 4878aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING]; 4888aa9ebccSVladimir Oltean 4898aa9ebccSVladimir Oltean if (table->entry_count) { 4908aa9ebccSVladimir Oltean kfree(table->entries); 4918aa9ebccSVladimir Oltean table->entry_count = 0; 4928aa9ebccSVladimir Oltean } 4938aa9ebccSVladimir Oltean 494fd6f2c25SVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 4958aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 4968aa9ebccSVladimir Oltean if (!table->entries) 4978aa9ebccSVladimir Oltean return -ENOMEM; 4988aa9ebccSVladimir Oltean 499fd6f2c25SVladimir Oltean table->entry_count = table->ops->max_entry_count; 5008aa9ebccSVladimir Oltean 5018aa9ebccSVladimir Oltean l2fwd = table->entries; 5028aa9ebccSVladimir Oltean 5033fa21270SVladimir Oltean /* First 5 entries in the L2 Forwarding Table define the forwarding 5043fa21270SVladimir Oltean * rules and the VLAN PCP to ingress queue mapping. 5053fa21270SVladimir Oltean * Set up the ingress queue mapping first. 5067f7ccdeaSVladimir Oltean */ 5073fa21270SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 5083fa21270SVladimir Oltean if (dsa_is_unused_port(ds, port)) 5098aa9ebccSVladimir Oltean continue; 5108aa9ebccSVladimir Oltean 5113fa21270SVladimir Oltean for (tc = 0; tc < SJA1105_NUM_TC; tc++) 5123fa21270SVladimir Oltean l2fwd[port].vlan_pmap[tc] = tc; 5133fa21270SVladimir Oltean } 5144d942354SVladimir Oltean 5153fa21270SVladimir Oltean /* Then manage the forwarding domain for user ports. These can forward 5163fa21270SVladimir Oltean * only to the always-on domain (CPU port and DSA links) 5173fa21270SVladimir Oltean */ 5183fa21270SVladimir Oltean for (from = 0; from < ds->num_ports; from++) { 5193fa21270SVladimir Oltean if (!dsa_is_user_port(ds, from)) 5203fa21270SVladimir Oltean continue; 5214d942354SVladimir Oltean 5223fa21270SVladimir Oltean for (to = 0; to < ds->num_ports; to++) { 5233fa21270SVladimir Oltean if (!dsa_is_cpu_port(ds, to) && 5243fa21270SVladimir Oltean !dsa_is_dsa_port(ds, to)) 5253fa21270SVladimir Oltean continue; 5263fa21270SVladimir Oltean 5273fa21270SVladimir Oltean l2fwd[from].bc_domain |= BIT(to); 5283fa21270SVladimir Oltean l2fwd[from].fl_domain |= BIT(to); 5293fa21270SVladimir Oltean 5303fa21270SVladimir Oltean sja1105_port_allow_traffic(l2fwd, from, to, true); 5313fa21270SVladimir Oltean } 5323fa21270SVladimir Oltean } 5333fa21270SVladimir Oltean 5343fa21270SVladimir Oltean /* Then manage the forwarding domain for DSA links and CPU ports (the 5353fa21270SVladimir Oltean * always-on domain). These can send packets to any enabled port except 5363fa21270SVladimir Oltean * themselves. 5373fa21270SVladimir Oltean */ 5383fa21270SVladimir Oltean for (from = 0; from < ds->num_ports; from++) { 5393fa21270SVladimir Oltean if (!dsa_is_cpu_port(ds, from) && !dsa_is_dsa_port(ds, from)) 5403fa21270SVladimir Oltean continue; 5413fa21270SVladimir Oltean 5423fa21270SVladimir Oltean for (to = 0; to < ds->num_ports; to++) { 5433fa21270SVladimir Oltean if (dsa_is_unused_port(ds, to)) 5443fa21270SVladimir Oltean continue; 5453fa21270SVladimir Oltean 5463fa21270SVladimir Oltean if (from == to) 5473fa21270SVladimir Oltean continue; 5483fa21270SVladimir Oltean 5493fa21270SVladimir Oltean l2fwd[from].bc_domain |= BIT(to); 5503fa21270SVladimir Oltean l2fwd[from].fl_domain |= BIT(to); 5513fa21270SVladimir Oltean 5523fa21270SVladimir Oltean sja1105_port_allow_traffic(l2fwd, from, to, true); 5533fa21270SVladimir Oltean } 5543fa21270SVladimir Oltean } 5553fa21270SVladimir Oltean 5560f9b762cSVladimir Oltean /* In odd topologies ("H" connections where there is a DSA link to 5570f9b762cSVladimir Oltean * another switch which also has its own CPU port), TX packets can loop 5580f9b762cSVladimir Oltean * back into the system (they are flooded from CPU port 1 to the DSA 5590f9b762cSVladimir Oltean * link, and from there to CPU port 2). Prevent this from happening by 5600f9b762cSVladimir Oltean * cutting RX from DSA links towards our CPU port, if the remote switch 5610f9b762cSVladimir Oltean * has its own CPU port and therefore doesn't need ours for network 5620f9b762cSVladimir Oltean * stack termination. 5630f9b762cSVladimir Oltean */ 5640f9b762cSVladimir Oltean dst = ds->dst; 5650f9b762cSVladimir Oltean 5660f9b762cSVladimir Oltean list_for_each_entry(dl, &dst->rtable, list) { 5670f9b762cSVladimir Oltean if (dl->dp->ds != ds || dl->link_dp->cpu_dp == dl->dp->cpu_dp) 5680f9b762cSVladimir Oltean continue; 5690f9b762cSVladimir Oltean 5700f9b762cSVladimir Oltean from = dl->dp->index; 5710f9b762cSVladimir Oltean to = dsa_upstream_port(ds, from); 5720f9b762cSVladimir Oltean 5730f9b762cSVladimir Oltean dev_warn(ds->dev, 5740f9b762cSVladimir Oltean "H topology detected, cutting RX from DSA link %d to CPU port %d to prevent TX packet loops\n", 5750f9b762cSVladimir Oltean from, to); 5760f9b762cSVladimir Oltean 5770f9b762cSVladimir Oltean sja1105_port_allow_traffic(l2fwd, from, to, false); 5780f9b762cSVladimir Oltean 5790f9b762cSVladimir Oltean l2fwd[from].bc_domain &= ~BIT(to); 5800f9b762cSVladimir Oltean l2fwd[from].fl_domain &= ~BIT(to); 5810f9b762cSVladimir Oltean } 5820f9b762cSVladimir Oltean 5833fa21270SVladimir Oltean /* Finally, manage the egress flooding domain. All ports start up with 5843fa21270SVladimir Oltean * flooding enabled, including the CPU port and DSA links. 5853fa21270SVladimir Oltean */ 5863fa21270SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 5873fa21270SVladimir Oltean if (dsa_is_unused_port(ds, port)) 5883fa21270SVladimir Oltean continue; 5893fa21270SVladimir Oltean 5903fa21270SVladimir Oltean priv->ucast_egress_floods |= BIT(port); 5913fa21270SVladimir Oltean priv->bcast_egress_floods |= BIT(port); 5928aa9ebccSVladimir Oltean } 593f238fef1SVladimir Oltean 5948aa9ebccSVladimir Oltean /* Next 8 entries define VLAN PCP mapping from ingress to egress. 5958aa9ebccSVladimir Oltean * Create a one-to-one mapping. 5968aa9ebccSVladimir Oltean */ 5973fa21270SVladimir Oltean for (tc = 0; tc < SJA1105_NUM_TC; tc++) { 5983fa21270SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 5993fa21270SVladimir Oltean if (dsa_is_unused_port(ds, port)) 600f238fef1SVladimir Oltean continue; 601f238fef1SVladimir Oltean 6023fa21270SVladimir Oltean l2fwd[ds->num_ports + tc].vlan_pmap[port] = tc; 603f238fef1SVladimir Oltean } 6043e77e59bSVladimir Oltean 6053fa21270SVladimir Oltean l2fwd[ds->num_ports + tc].type_egrpcp2outputq = true; 6063e77e59bSVladimir Oltean } 6073e77e59bSVladimir Oltean 6083e77e59bSVladimir Oltean return 0; 6093e77e59bSVladimir Oltean } 6103e77e59bSVladimir Oltean 6113e77e59bSVladimir Oltean static int sja1110_init_pcp_remapping(struct sja1105_private *priv) 6123e77e59bSVladimir Oltean { 6133e77e59bSVladimir Oltean struct sja1110_pcp_remapping_entry *pcp_remap; 6143e77e59bSVladimir Oltean struct dsa_switch *ds = priv->ds; 6153e77e59bSVladimir Oltean struct sja1105_table *table; 6163e77e59bSVladimir Oltean int port, tc; 6173e77e59bSVladimir Oltean 6183e77e59bSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_PCP_REMAPPING]; 6193e77e59bSVladimir Oltean 6203e77e59bSVladimir Oltean /* Nothing to do for SJA1105 */ 6213e77e59bSVladimir Oltean if (!table->ops->max_entry_count) 6223e77e59bSVladimir Oltean return 0; 6233e77e59bSVladimir Oltean 6243e77e59bSVladimir Oltean if (table->entry_count) { 6253e77e59bSVladimir Oltean kfree(table->entries); 6263e77e59bSVladimir Oltean table->entry_count = 0; 6273e77e59bSVladimir Oltean } 6283e77e59bSVladimir Oltean 6293e77e59bSVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 6303e77e59bSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 6313e77e59bSVladimir Oltean if (!table->entries) 6323e77e59bSVladimir Oltean return -ENOMEM; 6333e77e59bSVladimir Oltean 6343e77e59bSVladimir Oltean table->entry_count = table->ops->max_entry_count; 6353e77e59bSVladimir Oltean 6363e77e59bSVladimir Oltean pcp_remap = table->entries; 6373e77e59bSVladimir Oltean 6383e77e59bSVladimir Oltean /* Repeat the configuration done for vlan_pmap */ 6393e77e59bSVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 6403e77e59bSVladimir Oltean if (dsa_is_unused_port(ds, port)) 6413e77e59bSVladimir Oltean continue; 6423e77e59bSVladimir Oltean 6433e77e59bSVladimir Oltean for (tc = 0; tc < SJA1105_NUM_TC; tc++) 6443e77e59bSVladimir Oltean pcp_remap[port].egrpcp[tc] = tc; 645f238fef1SVladimir Oltean } 6468aa9ebccSVladimir Oltean 6478aa9ebccSVladimir Oltean return 0; 6488aa9ebccSVladimir Oltean } 6498aa9ebccSVladimir Oltean 6508aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding_params(struct sja1105_private *priv) 6518aa9ebccSVladimir Oltean { 6521bf658eeSVladimir Oltean struct sja1105_l2_forwarding_params_entry *l2fwd_params; 6538aa9ebccSVladimir Oltean struct sja1105_table *table; 6548aa9ebccSVladimir Oltean 6558aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS]; 6568aa9ebccSVladimir Oltean 6578aa9ebccSVladimir Oltean if (table->entry_count) { 6588aa9ebccSVladimir Oltean kfree(table->entries); 6598aa9ebccSVladimir Oltean table->entry_count = 0; 6608aa9ebccSVladimir Oltean } 6618aa9ebccSVladimir Oltean 662fd6f2c25SVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 6638aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 6648aa9ebccSVladimir Oltean if (!table->entries) 6658aa9ebccSVladimir Oltean return -ENOMEM; 6668aa9ebccSVladimir Oltean 667fd6f2c25SVladimir Oltean table->entry_count = table->ops->max_entry_count; 6688aa9ebccSVladimir Oltean 6698aa9ebccSVladimir Oltean /* This table only has a single entry */ 6701bf658eeSVladimir Oltean l2fwd_params = table->entries; 6711bf658eeSVladimir Oltean 6721bf658eeSVladimir Oltean /* Disallow dynamic reconfiguration of vlan_pmap */ 6731bf658eeSVladimir Oltean l2fwd_params->max_dynp = 0; 6741bf658eeSVladimir Oltean /* Use a single memory partition for all ingress queues */ 6751bf658eeSVladimir Oltean l2fwd_params->part_spc[0] = priv->info->max_frame_mem; 6768aa9ebccSVladimir Oltean 6778aa9ebccSVladimir Oltean return 0; 6788aa9ebccSVladimir Oltean } 6798aa9ebccSVladimir Oltean 680aaa270c6SVladimir Oltean void sja1105_frame_memory_partitioning(struct sja1105_private *priv) 681aaa270c6SVladimir Oltean { 682aaa270c6SVladimir Oltean struct sja1105_l2_forwarding_params_entry *l2_fwd_params; 683aaa270c6SVladimir Oltean struct sja1105_vl_forwarding_params_entry *vl_fwd_params; 684aaa270c6SVladimir Oltean struct sja1105_table *table; 685aaa270c6SVladimir Oltean 686aaa270c6SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS]; 687aaa270c6SVladimir Oltean l2_fwd_params = table->entries; 6880fac6aa0SVladimir Oltean l2_fwd_params->part_spc[0] = SJA1105_MAX_FRAME_MEMORY; 689aaa270c6SVladimir Oltean 690aaa270c6SVladimir Oltean /* If we have any critical-traffic virtual links, we need to reserve 691aaa270c6SVladimir Oltean * some frame buffer memory for them. At the moment, hardcode the value 692aaa270c6SVladimir Oltean * at 100 blocks of 128 bytes of memory each. This leaves 829 blocks 693aaa270c6SVladimir Oltean * remaining for best-effort traffic. TODO: figure out a more flexible 694aaa270c6SVladimir Oltean * way to perform the frame buffer partitioning. 695aaa270c6SVladimir Oltean */ 696aaa270c6SVladimir Oltean if (!priv->static_config.tables[BLK_IDX_VL_FORWARDING].entry_count) 697aaa270c6SVladimir Oltean return; 698aaa270c6SVladimir Oltean 699aaa270c6SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_VL_FORWARDING_PARAMS]; 700aaa270c6SVladimir Oltean vl_fwd_params = table->entries; 701aaa270c6SVladimir Oltean 702aaa270c6SVladimir Oltean l2_fwd_params->part_spc[0] -= SJA1105_VL_FRAME_MEMORY; 703aaa270c6SVladimir Oltean vl_fwd_params->partspc[0] = SJA1105_VL_FRAME_MEMORY; 704aaa270c6SVladimir Oltean } 705aaa270c6SVladimir Oltean 706ceec8bc0SVladimir Oltean /* SJA1110 TDMACONFIGIDX values: 707ceec8bc0SVladimir Oltean * 708ceec8bc0SVladimir Oltean * | 100 Mbps ports | 1Gbps ports | 2.5Gbps ports | Disabled ports 709ceec8bc0SVladimir Oltean * -----+----------------+---------------+---------------+--------------- 710ceec8bc0SVladimir Oltean * 0 | 0, [5:10] | [1:2] | [3:4] | retag 711ceec8bc0SVladimir Oltean * 1 |0, [5:10], retag| [1:2] | [3:4] | - 712ceec8bc0SVladimir Oltean * 2 | 0, [5:10] | [1:3], retag | 4 | - 713ceec8bc0SVladimir Oltean * 3 | 0, [5:10] |[1:2], 4, retag| 3 | - 714ceec8bc0SVladimir Oltean * 4 | 0, 2, [5:10] | 1, retag | [3:4] | - 715ceec8bc0SVladimir Oltean * 5 | 0, 1, [5:10] | 2, retag | [3:4] | - 716ceec8bc0SVladimir Oltean * 14 | 0, [5:10] | [1:4], retag | - | - 717ceec8bc0SVladimir Oltean * 15 | [5:10] | [0:4], retag | - | - 718ceec8bc0SVladimir Oltean */ 719ceec8bc0SVladimir Oltean static void sja1110_select_tdmaconfigidx(struct sja1105_private *priv) 720ceec8bc0SVladimir Oltean { 721ceec8bc0SVladimir Oltean struct sja1105_general_params_entry *general_params; 722ceec8bc0SVladimir Oltean struct sja1105_table *table; 723ceec8bc0SVladimir Oltean bool port_1_is_base_tx; 724ceec8bc0SVladimir Oltean bool port_3_is_2500; 725ceec8bc0SVladimir Oltean bool port_4_is_2500; 726ceec8bc0SVladimir Oltean u64 tdmaconfigidx; 727ceec8bc0SVladimir Oltean 728ceec8bc0SVladimir Oltean if (priv->info->device_id != SJA1110_DEVICE_ID) 729ceec8bc0SVladimir Oltean return; 730ceec8bc0SVladimir Oltean 731ceec8bc0SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; 732ceec8bc0SVladimir Oltean general_params = table->entries; 733ceec8bc0SVladimir Oltean 734ceec8bc0SVladimir Oltean /* All the settings below are "as opposed to SGMII", which is the 735ceec8bc0SVladimir Oltean * other pinmuxing option. 736ceec8bc0SVladimir Oltean */ 737ceec8bc0SVladimir Oltean port_1_is_base_tx = priv->phy_mode[1] == PHY_INTERFACE_MODE_INTERNAL; 738ceec8bc0SVladimir Oltean port_3_is_2500 = priv->phy_mode[3] == PHY_INTERFACE_MODE_2500BASEX; 739ceec8bc0SVladimir Oltean port_4_is_2500 = priv->phy_mode[4] == PHY_INTERFACE_MODE_2500BASEX; 740ceec8bc0SVladimir Oltean 741ceec8bc0SVladimir Oltean if (port_1_is_base_tx) 742ceec8bc0SVladimir Oltean /* Retagging port will operate at 1 Gbps */ 743ceec8bc0SVladimir Oltean tdmaconfigidx = 5; 744ceec8bc0SVladimir Oltean else if (port_3_is_2500 && port_4_is_2500) 745ceec8bc0SVladimir Oltean /* Retagging port will operate at 100 Mbps */ 746ceec8bc0SVladimir Oltean tdmaconfigidx = 1; 747ceec8bc0SVladimir Oltean else if (port_3_is_2500) 748ceec8bc0SVladimir Oltean /* Retagging port will operate at 1 Gbps */ 749ceec8bc0SVladimir Oltean tdmaconfigidx = 3; 750ceec8bc0SVladimir Oltean else if (port_4_is_2500) 751ceec8bc0SVladimir Oltean /* Retagging port will operate at 1 Gbps */ 752ceec8bc0SVladimir Oltean tdmaconfigidx = 2; 753ceec8bc0SVladimir Oltean else 754ceec8bc0SVladimir Oltean /* Retagging port will operate at 1 Gbps */ 755ceec8bc0SVladimir Oltean tdmaconfigidx = 14; 756ceec8bc0SVladimir Oltean 757ceec8bc0SVladimir Oltean general_params->tdmaconfigidx = tdmaconfigidx; 758ceec8bc0SVladimir Oltean } 759ceec8bc0SVladimir Oltean 76030a100e6SVladimir Oltean static int sja1105_init_topology(struct sja1105_private *priv, 76130a100e6SVladimir Oltean struct sja1105_general_params_entry *general_params) 76230a100e6SVladimir Oltean { 76330a100e6SVladimir Oltean struct dsa_switch *ds = priv->ds; 76430a100e6SVladimir Oltean int port; 76530a100e6SVladimir Oltean 76630a100e6SVladimir Oltean /* The host port is the destination for traffic matching mac_fltres1 76730a100e6SVladimir Oltean * and mac_fltres0 on all ports except itself. Default to an invalid 76830a100e6SVladimir Oltean * value. 76930a100e6SVladimir Oltean */ 77030a100e6SVladimir Oltean general_params->host_port = ds->num_ports; 77130a100e6SVladimir Oltean 77230a100e6SVladimir Oltean /* Link-local traffic received on casc_port will be forwarded 77330a100e6SVladimir Oltean * to host_port without embedding the source port and device ID 77430a100e6SVladimir Oltean * info in the destination MAC address, and no RX timestamps will be 77530a100e6SVladimir Oltean * taken either (presumably because it is a cascaded port and a 77630a100e6SVladimir Oltean * downstream SJA switch already did that). 77730a100e6SVladimir Oltean * To disable the feature, we need to do different things depending on 77830a100e6SVladimir Oltean * switch generation. On SJA1105 we need to set an invalid port, while 77930a100e6SVladimir Oltean * on SJA1110 which support multiple cascaded ports, this field is a 78030a100e6SVladimir Oltean * bitmask so it must be left zero. 78130a100e6SVladimir Oltean */ 78230a100e6SVladimir Oltean if (!priv->info->multiple_cascade_ports) 78330a100e6SVladimir Oltean general_params->casc_port = ds->num_ports; 78430a100e6SVladimir Oltean 78530a100e6SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 78630a100e6SVladimir Oltean bool is_upstream = dsa_is_upstream_port(ds, port); 78730a100e6SVladimir Oltean bool is_dsa_link = dsa_is_dsa_port(ds, port); 78830a100e6SVladimir Oltean 78930a100e6SVladimir Oltean /* Upstream ports can be dedicated CPU ports or 79030a100e6SVladimir Oltean * upstream-facing DSA links 79130a100e6SVladimir Oltean */ 79230a100e6SVladimir Oltean if (is_upstream) { 79330a100e6SVladimir Oltean if (general_params->host_port == ds->num_ports) { 79430a100e6SVladimir Oltean general_params->host_port = port; 79530a100e6SVladimir Oltean } else { 79630a100e6SVladimir Oltean dev_err(ds->dev, 79730a100e6SVladimir Oltean "Port %llu is already a host port, configuring %d as one too is not supported\n", 79830a100e6SVladimir Oltean general_params->host_port, port); 79930a100e6SVladimir Oltean return -EINVAL; 80030a100e6SVladimir Oltean } 80130a100e6SVladimir Oltean } 80230a100e6SVladimir Oltean 80330a100e6SVladimir Oltean /* Cascade ports are downstream-facing DSA links */ 80430a100e6SVladimir Oltean if (is_dsa_link && !is_upstream) { 80530a100e6SVladimir Oltean if (priv->info->multiple_cascade_ports) { 80630a100e6SVladimir Oltean general_params->casc_port |= BIT(port); 80730a100e6SVladimir Oltean } else if (general_params->casc_port == ds->num_ports) { 80830a100e6SVladimir Oltean general_params->casc_port = port; 80930a100e6SVladimir Oltean } else { 81030a100e6SVladimir Oltean dev_err(ds->dev, 81130a100e6SVladimir Oltean "Port %llu is already a cascade port, configuring %d as one too is not supported\n", 81230a100e6SVladimir Oltean general_params->casc_port, port); 81330a100e6SVladimir Oltean return -EINVAL; 81430a100e6SVladimir Oltean } 81530a100e6SVladimir Oltean } 81630a100e6SVladimir Oltean } 81730a100e6SVladimir Oltean 81830a100e6SVladimir Oltean if (general_params->host_port == ds->num_ports) { 81930a100e6SVladimir Oltean dev_err(ds->dev, "No host port configured\n"); 82030a100e6SVladimir Oltean return -EINVAL; 82130a100e6SVladimir Oltean } 82230a100e6SVladimir Oltean 82330a100e6SVladimir Oltean return 0; 82430a100e6SVladimir Oltean } 82530a100e6SVladimir Oltean 8268aa9ebccSVladimir Oltean static int sja1105_init_general_params(struct sja1105_private *priv) 8278aa9ebccSVladimir Oltean { 8288aa9ebccSVladimir Oltean struct sja1105_general_params_entry default_general_params = { 829511e6ca0SVladimir Oltean /* Allow dynamic changing of the mirror port */ 830511e6ca0SVladimir Oltean .mirr_ptacu = true, 8318aa9ebccSVladimir Oltean .switchid = priv->ds->index, 8325f06c63bSVladimir Oltean /* Priority queue for link-local management frames 8335f06c63bSVladimir Oltean * (both ingress to and egress from CPU - PTP, STP etc) 8345f06c63bSVladimir Oltean */ 83508fde09aSVladimir Oltean .hostprio = 7, 8368aa9ebccSVladimir Oltean .mac_fltres1 = SJA1105_LINKLOCAL_FILTER_A, 8378aa9ebccSVladimir Oltean .mac_flt1 = SJA1105_LINKLOCAL_FILTER_A_MASK, 83842824463SVladimir Oltean .incl_srcpt1 = false, 8398aa9ebccSVladimir Oltean .send_meta1 = false, 8408aa9ebccSVladimir Oltean .mac_fltres0 = SJA1105_LINKLOCAL_FILTER_B, 8418aa9ebccSVladimir Oltean .mac_flt0 = SJA1105_LINKLOCAL_FILTER_B_MASK, 84242824463SVladimir Oltean .incl_srcpt0 = false, 8438aa9ebccSVladimir Oltean .send_meta0 = false, 844511e6ca0SVladimir Oltean /* Default to an invalid value */ 845542043e9SVladimir Oltean .mirr_port = priv->ds->num_ports, 8468aa9ebccSVladimir Oltean /* No TTEthernet */ 847dfacc5a2SVladimir Oltean .vllupformat = SJA1105_VL_FORMAT_PSFP, 8488aa9ebccSVladimir Oltean .vlmarker = 0, 8498aa9ebccSVladimir Oltean .vlmask = 0, 8508aa9ebccSVladimir Oltean /* Only update correctionField for 1-step PTP (L2 transport) */ 8518aa9ebccSVladimir Oltean .ignore2stf = 0, 8526666cebcSVladimir Oltean /* Forcefully disable VLAN filtering by telling 8536666cebcSVladimir Oltean * the switch that VLAN has a different EtherType. 8546666cebcSVladimir Oltean */ 8556666cebcSVladimir Oltean .tpid = ETH_P_SJA1105, 8566666cebcSVladimir Oltean .tpid2 = ETH_P_SJA1105, 85729305260SVladimir Oltean /* Enable the TTEthernet engine on SJA1110 */ 85829305260SVladimir Oltean .tte_en = true, 8594913b8ebSVladimir Oltean /* Set up the EtherType for control packets on SJA1110 */ 8604913b8ebSVladimir Oltean .header_type = ETH_P_SJA1110, 8618aa9ebccSVladimir Oltean }; 8626c0de59bSVladimir Oltean struct sja1105_general_params_entry *general_params; 8638aa9ebccSVladimir Oltean struct sja1105_table *table; 86430a100e6SVladimir Oltean int rc; 865df2a81a3SVladimir Oltean 86630a100e6SVladimir Oltean rc = sja1105_init_topology(priv, &default_general_params); 86730a100e6SVladimir Oltean if (rc) 86830a100e6SVladimir Oltean return rc; 8698aa9ebccSVladimir Oltean 8708aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; 8718aa9ebccSVladimir Oltean 8728aa9ebccSVladimir Oltean if (table->entry_count) { 8738aa9ebccSVladimir Oltean kfree(table->entries); 8748aa9ebccSVladimir Oltean table->entry_count = 0; 8758aa9ebccSVladimir Oltean } 8768aa9ebccSVladimir Oltean 877fd6f2c25SVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 8788aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 8798aa9ebccSVladimir Oltean if (!table->entries) 8808aa9ebccSVladimir Oltean return -ENOMEM; 8818aa9ebccSVladimir Oltean 882fd6f2c25SVladimir Oltean table->entry_count = table->ops->max_entry_count; 8838aa9ebccSVladimir Oltean 8846c0de59bSVladimir Oltean general_params = table->entries; 8856c0de59bSVladimir Oltean 8868aa9ebccSVladimir Oltean /* This table only has a single entry */ 8876c0de59bSVladimir Oltean general_params[0] = default_general_params; 8888aa9ebccSVladimir Oltean 889ceec8bc0SVladimir Oltean sja1110_select_tdmaconfigidx(priv); 890ceec8bc0SVladimir Oltean 8918aa9ebccSVladimir Oltean return 0; 8928aa9ebccSVladimir Oltean } 8938aa9ebccSVladimir Oltean 89479d5511cSVladimir Oltean static int sja1105_init_avb_params(struct sja1105_private *priv) 89579d5511cSVladimir Oltean { 89679d5511cSVladimir Oltean struct sja1105_avb_params_entry *avb; 89779d5511cSVladimir Oltean struct sja1105_table *table; 89879d5511cSVladimir Oltean 89979d5511cSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_AVB_PARAMS]; 90079d5511cSVladimir Oltean 90179d5511cSVladimir Oltean /* Discard previous AVB Parameters Table */ 90279d5511cSVladimir Oltean if (table->entry_count) { 90379d5511cSVladimir Oltean kfree(table->entries); 90479d5511cSVladimir Oltean table->entry_count = 0; 90579d5511cSVladimir Oltean } 90679d5511cSVladimir Oltean 907fd6f2c25SVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 90879d5511cSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 90979d5511cSVladimir Oltean if (!table->entries) 91079d5511cSVladimir Oltean return -ENOMEM; 91179d5511cSVladimir Oltean 912fd6f2c25SVladimir Oltean table->entry_count = table->ops->max_entry_count; 91379d5511cSVladimir Oltean 91479d5511cSVladimir Oltean avb = table->entries; 91579d5511cSVladimir Oltean 91679d5511cSVladimir Oltean /* Configure the MAC addresses for meta frames */ 91779d5511cSVladimir Oltean avb->destmeta = SJA1105_META_DMAC; 91879d5511cSVladimir Oltean avb->srcmeta = SJA1105_META_SMAC; 919747e5eb3SVladimir Oltean /* On P/Q/R/S, configure the direction of the PTP_CLK pin as input by 920747e5eb3SVladimir Oltean * default. This is because there might be boards with a hardware 921747e5eb3SVladimir Oltean * layout where enabling the pin as output might cause an electrical 922747e5eb3SVladimir Oltean * clash. On E/T the pin is always an output, which the board designers 923747e5eb3SVladimir Oltean * probably already knew, so even if there are going to be electrical 924747e5eb3SVladimir Oltean * issues, there's nothing we can do. 925747e5eb3SVladimir Oltean */ 926747e5eb3SVladimir Oltean avb->cas_master = false; 92779d5511cSVladimir Oltean 92879d5511cSVladimir Oltean return 0; 92979d5511cSVladimir Oltean } 93079d5511cSVladimir Oltean 931a7cc081cSVladimir Oltean /* The L2 policing table is 2-stage. The table is looked up for each frame 932a7cc081cSVladimir Oltean * according to the ingress port, whether it was broadcast or not, and the 933a7cc081cSVladimir Oltean * classified traffic class (given by VLAN PCP). This portion of the lookup is 934a7cc081cSVladimir Oltean * fixed, and gives access to the SHARINDX, an indirection register pointing 935a7cc081cSVladimir Oltean * within the policing table itself, which is used to resolve the policer that 936a7cc081cSVladimir Oltean * will be used for this frame. 937a7cc081cSVladimir Oltean * 938a7cc081cSVladimir Oltean * Stage 1 Stage 2 939a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 940a7cc081cSVladimir Oltean * |Port 0 TC 0 |SHARINDX| | Policer 0: Rate, Burst, MTU | 941a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 942a7cc081cSVladimir Oltean * |Port 0 TC 1 |SHARINDX| | Policer 1: Rate, Burst, MTU | 943a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 944a7cc081cSVladimir Oltean * ... | Policer 2: Rate, Burst, MTU | 945a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 946a7cc081cSVladimir Oltean * |Port 0 TC 7 |SHARINDX| | Policer 3: Rate, Burst, MTU | 947a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 948a7cc081cSVladimir Oltean * |Port 1 TC 0 |SHARINDX| | Policer 4: Rate, Burst, MTU | 949a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 950a7cc081cSVladimir Oltean * ... | Policer 5: Rate, Burst, MTU | 951a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 952a7cc081cSVladimir Oltean * |Port 1 TC 7 |SHARINDX| | Policer 6: Rate, Burst, MTU | 953a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 954a7cc081cSVladimir Oltean * ... | Policer 7: Rate, Burst, MTU | 955a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 956a7cc081cSVladimir Oltean * |Port 4 TC 7 |SHARINDX| ... 957a7cc081cSVladimir Oltean * +------------+--------+ 958a7cc081cSVladimir Oltean * |Port 0 BCAST|SHARINDX| ... 959a7cc081cSVladimir Oltean * +------------+--------+ 960a7cc081cSVladimir Oltean * |Port 1 BCAST|SHARINDX| ... 961a7cc081cSVladimir Oltean * +------------+--------+ 962a7cc081cSVladimir Oltean * ... ... 963a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 964a7cc081cSVladimir Oltean * |Port 4 BCAST|SHARINDX| | Policer 44: Rate, Burst, MTU | 965a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 966a7cc081cSVladimir Oltean * 967a7cc081cSVladimir Oltean * In this driver, we shall use policers 0-4 as statically alocated port 968a7cc081cSVladimir Oltean * (matchall) policers. So we need to make the SHARINDX for all lookups 969a7cc081cSVladimir Oltean * corresponding to this ingress port (8 VLAN PCP lookups and 1 broadcast 970a7cc081cSVladimir Oltean * lookup) equal. 971a7cc081cSVladimir Oltean * The remaining policers (40) shall be dynamically allocated for flower 972a7cc081cSVladimir Oltean * policers, where the key is either vlan_prio or dst_mac ff:ff:ff:ff:ff:ff. 973a7cc081cSVladimir Oltean */ 9748aa9ebccSVladimir Oltean #define SJA1105_RATE_MBPS(speed) (((speed) * 64000) / 1000) 9758aa9ebccSVladimir Oltean 9768aa9ebccSVladimir Oltean static int sja1105_init_l2_policing(struct sja1105_private *priv) 9778aa9ebccSVladimir Oltean { 9788aa9ebccSVladimir Oltean struct sja1105_l2_policing_entry *policing; 979542043e9SVladimir Oltean struct dsa_switch *ds = priv->ds; 9808aa9ebccSVladimir Oltean struct sja1105_table *table; 981a7cc081cSVladimir Oltean int port, tc; 9828aa9ebccSVladimir Oltean 9838aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_POLICING]; 9848aa9ebccSVladimir Oltean 9858aa9ebccSVladimir Oltean /* Discard previous L2 Policing Table */ 9868aa9ebccSVladimir Oltean if (table->entry_count) { 9878aa9ebccSVladimir Oltean kfree(table->entries); 9888aa9ebccSVladimir Oltean table->entry_count = 0; 9898aa9ebccSVladimir Oltean } 9908aa9ebccSVladimir Oltean 991fd6f2c25SVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 9928aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 9938aa9ebccSVladimir Oltean if (!table->entries) 9948aa9ebccSVladimir Oltean return -ENOMEM; 9958aa9ebccSVladimir Oltean 996fd6f2c25SVladimir Oltean table->entry_count = table->ops->max_entry_count; 9978aa9ebccSVladimir Oltean 9988aa9ebccSVladimir Oltean policing = table->entries; 9998aa9ebccSVladimir Oltean 1000a7cc081cSVladimir Oltean /* Setup shared indices for the matchall policers */ 1001542043e9SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 100238fbe91fSVladimir Oltean int mcast = (ds->num_ports * (SJA1105_NUM_TC + 1)) + port; 1003542043e9SVladimir Oltean int bcast = (ds->num_ports * SJA1105_NUM_TC) + port; 1004a7cc081cSVladimir Oltean 1005a7cc081cSVladimir Oltean for (tc = 0; tc < SJA1105_NUM_TC; tc++) 1006a7cc081cSVladimir Oltean policing[port * SJA1105_NUM_TC + tc].sharindx = port; 1007a7cc081cSVladimir Oltean 1008a7cc081cSVladimir Oltean policing[bcast].sharindx = port; 100938fbe91fSVladimir Oltean /* Only SJA1110 has multicast policers */ 101038fbe91fSVladimir Oltean if (mcast <= table->ops->max_entry_count) 101138fbe91fSVladimir Oltean policing[mcast].sharindx = port; 1012a7cc081cSVladimir Oltean } 1013a7cc081cSVladimir Oltean 1014a7cc081cSVladimir Oltean /* Setup the matchall policer parameters */ 1015542043e9SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 1016c279c726SVladimir Oltean int mtu = VLAN_ETH_FRAME_LEN + ETH_FCS_LEN; 1017c279c726SVladimir Oltean 1018777e55e3SVladimir Oltean if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) 1019c279c726SVladimir Oltean mtu += VLAN_HLEN; 10208aa9ebccSVladimir Oltean 1021a7cc081cSVladimir Oltean policing[port].smax = 65535; /* Burst size in bytes */ 1022a7cc081cSVladimir Oltean policing[port].rate = SJA1105_RATE_MBPS(1000); 1023a7cc081cSVladimir Oltean policing[port].maxlen = mtu; 1024a7cc081cSVladimir Oltean policing[port].partition = 0; 10258aa9ebccSVladimir Oltean } 1026a7cc081cSVladimir Oltean 10278aa9ebccSVladimir Oltean return 0; 10288aa9ebccSVladimir Oltean } 10298aa9ebccSVladimir Oltean 10305d645df9SVladimir Oltean static int sja1105_static_config_load(struct sja1105_private *priv) 10318aa9ebccSVladimir Oltean { 10328aa9ebccSVladimir Oltean int rc; 10338aa9ebccSVladimir Oltean 10348aa9ebccSVladimir Oltean sja1105_static_config_free(&priv->static_config); 10358aa9ebccSVladimir Oltean rc = sja1105_static_config_init(&priv->static_config, 10368aa9ebccSVladimir Oltean priv->info->static_ops, 10378aa9ebccSVladimir Oltean priv->info->device_id); 10388aa9ebccSVladimir Oltean if (rc) 10398aa9ebccSVladimir Oltean return rc; 10408aa9ebccSVladimir Oltean 10418aa9ebccSVladimir Oltean /* Build static configuration */ 10428aa9ebccSVladimir Oltean rc = sja1105_init_mac_settings(priv); 10438aa9ebccSVladimir Oltean if (rc < 0) 10448aa9ebccSVladimir Oltean return rc; 10455d645df9SVladimir Oltean rc = sja1105_init_mii_settings(priv); 10468aa9ebccSVladimir Oltean if (rc < 0) 10478aa9ebccSVladimir Oltean return rc; 10488aa9ebccSVladimir Oltean rc = sja1105_init_static_fdb(priv); 10498aa9ebccSVladimir Oltean if (rc < 0) 10508aa9ebccSVladimir Oltean return rc; 10518aa9ebccSVladimir Oltean rc = sja1105_init_static_vlan(priv); 10528aa9ebccSVladimir Oltean if (rc < 0) 10538aa9ebccSVladimir Oltean return rc; 10548aa9ebccSVladimir Oltean rc = sja1105_init_l2_lookup_params(priv); 10558aa9ebccSVladimir Oltean if (rc < 0) 10568aa9ebccSVladimir Oltean return rc; 10578aa9ebccSVladimir Oltean rc = sja1105_init_l2_forwarding(priv); 10588aa9ebccSVladimir Oltean if (rc < 0) 10598aa9ebccSVladimir Oltean return rc; 10608aa9ebccSVladimir Oltean rc = sja1105_init_l2_forwarding_params(priv); 10618aa9ebccSVladimir Oltean if (rc < 0) 10628aa9ebccSVladimir Oltean return rc; 10638aa9ebccSVladimir Oltean rc = sja1105_init_l2_policing(priv); 10648aa9ebccSVladimir Oltean if (rc < 0) 10658aa9ebccSVladimir Oltean return rc; 10668aa9ebccSVladimir Oltean rc = sja1105_init_general_params(priv); 10678aa9ebccSVladimir Oltean if (rc < 0) 10688aa9ebccSVladimir Oltean return rc; 106979d5511cSVladimir Oltean rc = sja1105_init_avb_params(priv); 107079d5511cSVladimir Oltean if (rc < 0) 107179d5511cSVladimir Oltean return rc; 10723e77e59bSVladimir Oltean rc = sja1110_init_pcp_remapping(priv); 10733e77e59bSVladimir Oltean if (rc < 0) 10743e77e59bSVladimir Oltean return rc; 10758aa9ebccSVladimir Oltean 10768aa9ebccSVladimir Oltean /* Send initial configuration to hardware via SPI */ 10778aa9ebccSVladimir Oltean return sja1105_static_config_upload(priv); 10788aa9ebccSVladimir Oltean } 10798aa9ebccSVladimir Oltean 108029afb83aSVladimir Oltean static int sja1105_parse_rgmii_delays(struct sja1105_private *priv) 1081f5b8631cSVladimir Oltean { 1082542043e9SVladimir Oltean struct dsa_switch *ds = priv->ds; 108329afb83aSVladimir Oltean int port; 1084f5b8631cSVladimir Oltean 108529afb83aSVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 108629afb83aSVladimir Oltean if (!priv->fixed_link[port]) 1087f5b8631cSVladimir Oltean continue; 1088f5b8631cSVladimir Oltean 108929afb83aSVladimir Oltean if (priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_RXID || 109029afb83aSVladimir Oltean priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_ID) 109129afb83aSVladimir Oltean priv->rgmii_rx_delay[port] = true; 1092f5b8631cSVladimir Oltean 109329afb83aSVladimir Oltean if (priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_TXID || 109429afb83aSVladimir Oltean priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_ID) 109529afb83aSVladimir Oltean priv->rgmii_tx_delay[port] = true; 1096f5b8631cSVladimir Oltean 109729afb83aSVladimir Oltean if ((priv->rgmii_rx_delay[port] || priv->rgmii_tx_delay[port]) && 1098f5b8631cSVladimir Oltean !priv->info->setup_rgmii_delay) 1099f5b8631cSVladimir Oltean return -EINVAL; 1100f5b8631cSVladimir Oltean } 1101f5b8631cSVladimir Oltean return 0; 1102f5b8631cSVladimir Oltean } 1103f5b8631cSVladimir Oltean 11048aa9ebccSVladimir Oltean static int sja1105_parse_ports_node(struct sja1105_private *priv, 11058aa9ebccSVladimir Oltean struct device_node *ports_node) 11068aa9ebccSVladimir Oltean { 11078aa9ebccSVladimir Oltean struct device *dev = &priv->spidev->dev; 11088aa9ebccSVladimir Oltean struct device_node *child; 11098aa9ebccSVladimir Oltean 111027afe0d3SVladimir Oltean for_each_available_child_of_node(ports_node, child) { 11118aa9ebccSVladimir Oltean struct device_node *phy_node; 11120c65b2b9SAndrew Lunn phy_interface_t phy_mode; 11138aa9ebccSVladimir Oltean u32 index; 11140c65b2b9SAndrew Lunn int err; 11158aa9ebccSVladimir Oltean 11168aa9ebccSVladimir Oltean /* Get switch port number from DT */ 11178aa9ebccSVladimir Oltean if (of_property_read_u32(child, "reg", &index) < 0) { 11188aa9ebccSVladimir Oltean dev_err(dev, "Port number not defined in device tree " 11198aa9ebccSVladimir Oltean "(property \"reg\")\n"); 11207ba771e3SNishka Dasgupta of_node_put(child); 11218aa9ebccSVladimir Oltean return -ENODEV; 11228aa9ebccSVladimir Oltean } 11238aa9ebccSVladimir Oltean 11248aa9ebccSVladimir Oltean /* Get PHY mode from DT */ 11250c65b2b9SAndrew Lunn err = of_get_phy_mode(child, &phy_mode); 11260c65b2b9SAndrew Lunn if (err) { 11278aa9ebccSVladimir Oltean dev_err(dev, "Failed to read phy-mode or " 11288aa9ebccSVladimir Oltean "phy-interface-type property for port %d\n", 11298aa9ebccSVladimir Oltean index); 11307ba771e3SNishka Dasgupta of_node_put(child); 11318aa9ebccSVladimir Oltean return -ENODEV; 11328aa9ebccSVladimir Oltean } 11338aa9ebccSVladimir Oltean 11348aa9ebccSVladimir Oltean phy_node = of_parse_phandle(child, "phy-handle", 0); 11358aa9ebccSVladimir Oltean if (!phy_node) { 11368aa9ebccSVladimir Oltean if (!of_phy_is_fixed_link(child)) { 11378aa9ebccSVladimir Oltean dev_err(dev, "phy-handle or fixed-link " 11388aa9ebccSVladimir Oltean "properties missing!\n"); 11397ba771e3SNishka Dasgupta of_node_put(child); 11408aa9ebccSVladimir Oltean return -ENODEV; 11418aa9ebccSVladimir Oltean } 11428aa9ebccSVladimir Oltean /* phy-handle is missing, but fixed-link isn't. 11438aa9ebccSVladimir Oltean * So it's a fixed link. Default to PHY role. 11448aa9ebccSVladimir Oltean */ 114529afb83aSVladimir Oltean priv->fixed_link[index] = true; 11468aa9ebccSVladimir Oltean } else { 11478aa9ebccSVladimir Oltean of_node_put(phy_node); 11488aa9ebccSVladimir Oltean } 11498aa9ebccSVladimir Oltean 1150bf4edf4aSVladimir Oltean priv->phy_mode[index] = phy_mode; 11518aa9ebccSVladimir Oltean } 11528aa9ebccSVladimir Oltean 11538aa9ebccSVladimir Oltean return 0; 11548aa9ebccSVladimir Oltean } 11558aa9ebccSVladimir Oltean 11565d645df9SVladimir Oltean static int sja1105_parse_dt(struct sja1105_private *priv) 11578aa9ebccSVladimir Oltean { 11588aa9ebccSVladimir Oltean struct device *dev = &priv->spidev->dev; 11598aa9ebccSVladimir Oltean struct device_node *switch_node = dev->of_node; 11608aa9ebccSVladimir Oltean struct device_node *ports_node; 11618aa9ebccSVladimir Oltean int rc; 11628aa9ebccSVladimir Oltean 11638aa9ebccSVladimir Oltean ports_node = of_get_child_by_name(switch_node, "ports"); 116415074a36SVladimir Oltean if (!ports_node) 116515074a36SVladimir Oltean ports_node = of_get_child_by_name(switch_node, "ethernet-ports"); 11668aa9ebccSVladimir Oltean if (!ports_node) { 11678aa9ebccSVladimir Oltean dev_err(dev, "Incorrect bindings: absent \"ports\" node\n"); 11688aa9ebccSVladimir Oltean return -ENODEV; 11698aa9ebccSVladimir Oltean } 11708aa9ebccSVladimir Oltean 11715d645df9SVladimir Oltean rc = sja1105_parse_ports_node(priv, ports_node); 11728aa9ebccSVladimir Oltean of_node_put(ports_node); 11738aa9ebccSVladimir Oltean 11748aa9ebccSVladimir Oltean return rc; 11758aa9ebccSVladimir Oltean } 11768aa9ebccSVladimir Oltean 1177c44d0535SVladimir Oltean /* Convert link speed from SJA1105 to ethtool encoding */ 117841fed17fSVladimir Oltean static int sja1105_port_speed_to_ethtool(struct sja1105_private *priv, 117941fed17fSVladimir Oltean u64 speed) 118041fed17fSVladimir Oltean { 118141fed17fSVladimir Oltean if (speed == priv->info->port_speed[SJA1105_SPEED_10MBPS]) 118241fed17fSVladimir Oltean return SPEED_10; 118341fed17fSVladimir Oltean if (speed == priv->info->port_speed[SJA1105_SPEED_100MBPS]) 118441fed17fSVladimir Oltean return SPEED_100; 118541fed17fSVladimir Oltean if (speed == priv->info->port_speed[SJA1105_SPEED_1000MBPS]) 118641fed17fSVladimir Oltean return SPEED_1000; 118741fed17fSVladimir Oltean if (speed == priv->info->port_speed[SJA1105_SPEED_2500MBPS]) 118841fed17fSVladimir Oltean return SPEED_2500; 118941fed17fSVladimir Oltean return SPEED_UNKNOWN; 119041fed17fSVladimir Oltean } 11918aa9ebccSVladimir Oltean 11928400cff6SVladimir Oltean /* Set link speed in the MAC configuration for a specific port. */ 11938aa9ebccSVladimir Oltean static int sja1105_adjust_port_config(struct sja1105_private *priv, int port, 11948400cff6SVladimir Oltean int speed_mbps) 11958aa9ebccSVladimir Oltean { 11968aa9ebccSVladimir Oltean struct sja1105_mac_config_entry *mac; 11978aa9ebccSVladimir Oltean struct device *dev = priv->ds->dev; 119841fed17fSVladimir Oltean u64 speed; 11998aa9ebccSVladimir Oltean int rc; 12008aa9ebccSVladimir Oltean 12018400cff6SVladimir Oltean /* On P/Q/R/S, one can read from the device via the MAC reconfiguration 12028400cff6SVladimir Oltean * tables. On E/T, MAC reconfig tables are not readable, only writable. 12038400cff6SVladimir Oltean * We have to *know* what the MAC looks like. For the sake of keeping 12048400cff6SVladimir Oltean * the code common, we'll use the static configuration tables as a 12058400cff6SVladimir Oltean * reasonable approximation for both E/T and P/Q/R/S. 12068400cff6SVladimir Oltean */ 12078aa9ebccSVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 12088aa9ebccSVladimir Oltean 1209f4cfcfbdSVladimir Oltean switch (speed_mbps) { 1210c44d0535SVladimir Oltean case SPEED_UNKNOWN: 1211a979a0abSVladimir Oltean /* PHYLINK called sja1105_mac_config() to inform us about 1212a979a0abSVladimir Oltean * the state->interface, but AN has not completed and the 1213a979a0abSVladimir Oltean * speed is not yet valid. UM10944.pdf says that setting 1214a979a0abSVladimir Oltean * SJA1105_SPEED_AUTO at runtime disables the port, so that is 1215a979a0abSVladimir Oltean * ok for power consumption in case AN will never complete - 1216a979a0abSVladimir Oltean * otherwise PHYLINK should come back with a new update. 1217a979a0abSVladimir Oltean */ 121841fed17fSVladimir Oltean speed = priv->info->port_speed[SJA1105_SPEED_AUTO]; 1219f4cfcfbdSVladimir Oltean break; 1220c44d0535SVladimir Oltean case SPEED_10: 122141fed17fSVladimir Oltean speed = priv->info->port_speed[SJA1105_SPEED_10MBPS]; 1222f4cfcfbdSVladimir Oltean break; 1223c44d0535SVladimir Oltean case SPEED_100: 122441fed17fSVladimir Oltean speed = priv->info->port_speed[SJA1105_SPEED_100MBPS]; 1225f4cfcfbdSVladimir Oltean break; 1226c44d0535SVladimir Oltean case SPEED_1000: 122741fed17fSVladimir Oltean speed = priv->info->port_speed[SJA1105_SPEED_1000MBPS]; 1228f4cfcfbdSVladimir Oltean break; 122956b63466SVladimir Oltean case SPEED_2500: 123056b63466SVladimir Oltean speed = priv->info->port_speed[SJA1105_SPEED_2500MBPS]; 123156b63466SVladimir Oltean break; 1232f4cfcfbdSVladimir Oltean default: 12338aa9ebccSVladimir Oltean dev_err(dev, "Invalid speed %iMbps\n", speed_mbps); 12348aa9ebccSVladimir Oltean return -EINVAL; 12358aa9ebccSVladimir Oltean } 12368aa9ebccSVladimir Oltean 12378400cff6SVladimir Oltean /* Overwrite SJA1105_SPEED_AUTO from the static MAC configuration 12388400cff6SVladimir Oltean * table, since this will be used for the clocking setup, and we no 12398400cff6SVladimir Oltean * longer need to store it in the static config (already told hardware 12408400cff6SVladimir Oltean * we want auto during upload phase). 1241ffe10e67SVladimir Oltean * Actually for the SGMII port, the MAC is fixed at 1 Gbps and 1242ffe10e67SVladimir Oltean * we need to configure the PCS only (if even that). 12438aa9ebccSVladimir Oltean */ 124491a05078SVladimir Oltean if (priv->phy_mode[port] == PHY_INTERFACE_MODE_SGMII) 124541fed17fSVladimir Oltean mac[port].speed = priv->info->port_speed[SJA1105_SPEED_1000MBPS]; 124656b63466SVladimir Oltean else if (priv->phy_mode[port] == PHY_INTERFACE_MODE_2500BASEX) 124756b63466SVladimir Oltean mac[port].speed = priv->info->port_speed[SJA1105_SPEED_2500MBPS]; 1248ffe10e67SVladimir Oltean else 12498aa9ebccSVladimir Oltean mac[port].speed = speed; 12508aa9ebccSVladimir Oltean 12518aa9ebccSVladimir Oltean /* Write to the dynamic reconfiguration tables */ 12528400cff6SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, 12538400cff6SVladimir Oltean &mac[port], true); 12548aa9ebccSVladimir Oltean if (rc < 0) { 12558aa9ebccSVladimir Oltean dev_err(dev, "Failed to write MAC config: %d\n", rc); 12568aa9ebccSVladimir Oltean return rc; 12578aa9ebccSVladimir Oltean } 12588aa9ebccSVladimir Oltean 12598aa9ebccSVladimir Oltean /* Reconfigure the PLLs for the RGMII interfaces (required 125 MHz at 12608aa9ebccSVladimir Oltean * gigabit, 25 MHz at 100 Mbps and 2.5 MHz at 10 Mbps). For MII and 12618aa9ebccSVladimir Oltean * RMII no change of the clock setup is required. Actually, changing 12628aa9ebccSVladimir Oltean * the clock setup does interrupt the clock signal for a certain time 12638aa9ebccSVladimir Oltean * which causes trouble for all PHYs relying on this signal. 12648aa9ebccSVladimir Oltean */ 126591a05078SVladimir Oltean if (!phy_interface_mode_is_rgmii(priv->phy_mode[port])) 12668aa9ebccSVladimir Oltean return 0; 12678aa9ebccSVladimir Oltean 12688aa9ebccSVladimir Oltean return sja1105_clocking_setup_port(priv, port); 12698aa9ebccSVladimir Oltean } 12708aa9ebccSVladimir Oltean 127139710229SVladimir Oltean /* The SJA1105 MAC programming model is through the static config (the xMII 127239710229SVladimir Oltean * Mode table cannot be dynamically reconfigured), and we have to program 127339710229SVladimir Oltean * that early (earlier than PHYLINK calls us, anyway). 127439710229SVladimir Oltean * So just error out in case the connected PHY attempts to change the initial 127539710229SVladimir Oltean * system interface MII protocol from what is defined in the DT, at least for 127639710229SVladimir Oltean * now. 127739710229SVladimir Oltean */ 127839710229SVladimir Oltean static bool sja1105_phy_mode_mismatch(struct sja1105_private *priv, int port, 127939710229SVladimir Oltean phy_interface_t interface) 128039710229SVladimir Oltean { 1281bf4edf4aSVladimir Oltean return priv->phy_mode[port] != interface; 128239710229SVladimir Oltean } 128339710229SVladimir Oltean 1284af7cd036SVladimir Oltean static void sja1105_mac_config(struct dsa_switch *ds, int port, 1285ffe10e67SVladimir Oltean unsigned int mode, 1286af7cd036SVladimir Oltean const struct phylink_link_state *state) 12878aa9ebccSVladimir Oltean { 12883ad1d171SVladimir Oltean struct dsa_port *dp = dsa_to_port(ds, port); 12898aa9ebccSVladimir Oltean struct sja1105_private *priv = ds->priv; 12903ad1d171SVladimir Oltean struct dw_xpcs *xpcs; 12918aa9ebccSVladimir Oltean 1292ec8582d1SVladimir Oltean if (sja1105_phy_mode_mismatch(priv, port, state->interface)) { 1293ec8582d1SVladimir Oltean dev_err(ds->dev, "Changing PHY mode to %s not supported!\n", 1294ec8582d1SVladimir Oltean phy_modes(state->interface)); 129539710229SVladimir Oltean return; 1296ec8582d1SVladimir Oltean } 129739710229SVladimir Oltean 12983ad1d171SVladimir Oltean xpcs = priv->xpcs[port]; 1299ffe10e67SVladimir Oltean 13003ad1d171SVladimir Oltean if (xpcs) 13013ad1d171SVladimir Oltean phylink_set_pcs(dp->pl, &xpcs->pcs); 13028400cff6SVladimir Oltean } 13038400cff6SVladimir Oltean 13048400cff6SVladimir Oltean static void sja1105_mac_link_down(struct dsa_switch *ds, int port, 13058400cff6SVladimir Oltean unsigned int mode, 13068400cff6SVladimir Oltean phy_interface_t interface) 13078400cff6SVladimir Oltean { 13088400cff6SVladimir Oltean sja1105_inhibit_tx(ds->priv, BIT(port), true); 13098400cff6SVladimir Oltean } 13108400cff6SVladimir Oltean 13118400cff6SVladimir Oltean static void sja1105_mac_link_up(struct dsa_switch *ds, int port, 13128400cff6SVladimir Oltean unsigned int mode, 13138400cff6SVladimir Oltean phy_interface_t interface, 13145b502a7bSRussell King struct phy_device *phydev, 13155b502a7bSRussell King int speed, int duplex, 13165b502a7bSRussell King bool tx_pause, bool rx_pause) 13178400cff6SVladimir Oltean { 1318ec8582d1SVladimir Oltean struct sja1105_private *priv = ds->priv; 1319ec8582d1SVladimir Oltean 1320ec8582d1SVladimir Oltean sja1105_adjust_port_config(priv, port, speed); 1321ec8582d1SVladimir Oltean 1322ec8582d1SVladimir Oltean sja1105_inhibit_tx(priv, BIT(port), false); 13238aa9ebccSVladimir Oltean } 13248aa9ebccSVladimir Oltean 1325ad9f299aSVladimir Oltean static void sja1105_phylink_validate(struct dsa_switch *ds, int port, 1326ad9f299aSVladimir Oltean unsigned long *supported, 1327ad9f299aSVladimir Oltean struct phylink_link_state *state) 1328ad9f299aSVladimir Oltean { 1329ad9f299aSVladimir Oltean /* Construct a new mask which exhaustively contains all link features 1330ad9f299aSVladimir Oltean * supported by the MAC, and then apply that (logical AND) to what will 1331ad9f299aSVladimir Oltean * be sent to the PHY for "marketing". 1332ad9f299aSVladimir Oltean */ 1333ad9f299aSVladimir Oltean __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, }; 1334ad9f299aSVladimir Oltean struct sja1105_private *priv = ds->priv; 1335ad9f299aSVladimir Oltean struct sja1105_xmii_params_entry *mii; 1336ad9f299aSVladimir Oltean 1337ad9f299aSVladimir Oltean mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries; 1338ad9f299aSVladimir Oltean 133939710229SVladimir Oltean /* include/linux/phylink.h says: 134039710229SVladimir Oltean * When @state->interface is %PHY_INTERFACE_MODE_NA, phylink 134139710229SVladimir Oltean * expects the MAC driver to return all supported link modes. 134239710229SVladimir Oltean */ 134339710229SVladimir Oltean if (state->interface != PHY_INTERFACE_MODE_NA && 134439710229SVladimir Oltean sja1105_phy_mode_mismatch(priv, port, state->interface)) { 134539710229SVladimir Oltean bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS); 134639710229SVladimir Oltean return; 134739710229SVladimir Oltean } 134839710229SVladimir Oltean 1349ad9f299aSVladimir Oltean /* The MAC does not support pause frames, and also doesn't 1350ad9f299aSVladimir Oltean * support half-duplex traffic modes. 1351ad9f299aSVladimir Oltean */ 1352ad9f299aSVladimir Oltean phylink_set(mask, Autoneg); 1353ad9f299aSVladimir Oltean phylink_set(mask, MII); 1354ad9f299aSVladimir Oltean phylink_set(mask, 10baseT_Full); 1355ad9f299aSVladimir Oltean phylink_set(mask, 100baseT_Full); 1356ca68e138SOleksij Rempel phylink_set(mask, 100baseT1_Full); 1357ffe10e67SVladimir Oltean if (mii->xmii_mode[port] == XMII_MODE_RGMII || 1358ffe10e67SVladimir Oltean mii->xmii_mode[port] == XMII_MODE_SGMII) 1359ad9f299aSVladimir Oltean phylink_set(mask, 1000baseT_Full); 136056b63466SVladimir Oltean if (priv->info->supports_2500basex[port]) { 136156b63466SVladimir Oltean phylink_set(mask, 2500baseT_Full); 136256b63466SVladimir Oltean phylink_set(mask, 2500baseX_Full); 136356b63466SVladimir Oltean } 1364ad9f299aSVladimir Oltean 1365ad9f299aSVladimir Oltean bitmap_and(supported, supported, mask, __ETHTOOL_LINK_MODE_MASK_NBITS); 1366ad9f299aSVladimir Oltean bitmap_and(state->advertising, state->advertising, mask, 1367ad9f299aSVladimir Oltean __ETHTOOL_LINK_MODE_MASK_NBITS); 1368ad9f299aSVladimir Oltean } 1369ad9f299aSVladimir Oltean 137060f6053fSVladimir Oltean static int 137160f6053fSVladimir Oltean sja1105_find_static_fdb_entry(struct sja1105_private *priv, int port, 137260f6053fSVladimir Oltean const struct sja1105_l2_lookup_entry *requested) 137360f6053fSVladimir Oltean { 137460f6053fSVladimir Oltean struct sja1105_l2_lookup_entry *l2_lookup; 137560f6053fSVladimir Oltean struct sja1105_table *table; 137660f6053fSVladimir Oltean int i; 137760f6053fSVladimir Oltean 137860f6053fSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; 137960f6053fSVladimir Oltean l2_lookup = table->entries; 138060f6053fSVladimir Oltean 138160f6053fSVladimir Oltean for (i = 0; i < table->entry_count; i++) 138260f6053fSVladimir Oltean if (l2_lookup[i].macaddr == requested->macaddr && 138360f6053fSVladimir Oltean l2_lookup[i].vlanid == requested->vlanid && 138460f6053fSVladimir Oltean l2_lookup[i].destports & BIT(port)) 138560f6053fSVladimir Oltean return i; 138660f6053fSVladimir Oltean 138760f6053fSVladimir Oltean return -1; 138860f6053fSVladimir Oltean } 138960f6053fSVladimir Oltean 139060f6053fSVladimir Oltean /* We want FDB entries added statically through the bridge command to persist 139160f6053fSVladimir Oltean * across switch resets, which are a common thing during normal SJA1105 139260f6053fSVladimir Oltean * operation. So we have to back them up in the static configuration tables 139360f6053fSVladimir Oltean * and hence apply them on next static config upload... yay! 139460f6053fSVladimir Oltean */ 139560f6053fSVladimir Oltean static int 139660f6053fSVladimir Oltean sja1105_static_fdb_change(struct sja1105_private *priv, int port, 139760f6053fSVladimir Oltean const struct sja1105_l2_lookup_entry *requested, 139860f6053fSVladimir Oltean bool keep) 139960f6053fSVladimir Oltean { 140060f6053fSVladimir Oltean struct sja1105_l2_lookup_entry *l2_lookup; 140160f6053fSVladimir Oltean struct sja1105_table *table; 140260f6053fSVladimir Oltean int rc, match; 140360f6053fSVladimir Oltean 140460f6053fSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; 140560f6053fSVladimir Oltean 140660f6053fSVladimir Oltean match = sja1105_find_static_fdb_entry(priv, port, requested); 140760f6053fSVladimir Oltean if (match < 0) { 140860f6053fSVladimir Oltean /* Can't delete a missing entry. */ 140960f6053fSVladimir Oltean if (!keep) 141060f6053fSVladimir Oltean return 0; 141160f6053fSVladimir Oltean 141260f6053fSVladimir Oltean /* No match => new entry */ 141360f6053fSVladimir Oltean rc = sja1105_table_resize(table, table->entry_count + 1); 141460f6053fSVladimir Oltean if (rc) 141560f6053fSVladimir Oltean return rc; 141660f6053fSVladimir Oltean 141760f6053fSVladimir Oltean match = table->entry_count - 1; 141860f6053fSVladimir Oltean } 141960f6053fSVladimir Oltean 142060f6053fSVladimir Oltean /* Assign pointer after the resize (it may be new memory) */ 142160f6053fSVladimir Oltean l2_lookup = table->entries; 142260f6053fSVladimir Oltean 142360f6053fSVladimir Oltean /* We have a match. 142460f6053fSVladimir Oltean * If the job was to add this FDB entry, it's already done (mostly 142560f6053fSVladimir Oltean * anyway, since the port forwarding mask may have changed, case in 142660f6053fSVladimir Oltean * which we update it). 142760f6053fSVladimir Oltean * Otherwise we have to delete it. 142860f6053fSVladimir Oltean */ 142960f6053fSVladimir Oltean if (keep) { 143060f6053fSVladimir Oltean l2_lookup[match] = *requested; 143160f6053fSVladimir Oltean return 0; 143260f6053fSVladimir Oltean } 143360f6053fSVladimir Oltean 143460f6053fSVladimir Oltean /* To remove, the strategy is to overwrite the element with 143560f6053fSVladimir Oltean * the last one, and then reduce the array size by 1 143660f6053fSVladimir Oltean */ 143760f6053fSVladimir Oltean l2_lookup[match] = l2_lookup[table->entry_count - 1]; 143860f6053fSVladimir Oltean return sja1105_table_resize(table, table->entry_count - 1); 143960f6053fSVladimir Oltean } 144060f6053fSVladimir Oltean 1441291d1e72SVladimir Oltean /* First-generation switches have a 4-way set associative TCAM that 1442291d1e72SVladimir Oltean * holds the FDB entries. An FDB index spans from 0 to 1023 and is comprised of 1443291d1e72SVladimir Oltean * a "bin" (grouping of 4 entries) and a "way" (an entry within a bin). 1444291d1e72SVladimir Oltean * For the placement of a newly learnt FDB entry, the switch selects the bin 1445291d1e72SVladimir Oltean * based on a hash function, and the way within that bin incrementally. 1446291d1e72SVladimir Oltean */ 144709c1b412SVladimir Oltean static int sja1105et_fdb_index(int bin, int way) 1448291d1e72SVladimir Oltean { 1449291d1e72SVladimir Oltean return bin * SJA1105ET_FDB_BIN_SIZE + way; 1450291d1e72SVladimir Oltean } 1451291d1e72SVladimir Oltean 14529dfa6911SVladimir Oltean static int sja1105et_is_fdb_entry_in_bin(struct sja1105_private *priv, int bin, 1453291d1e72SVladimir Oltean const u8 *addr, u16 vid, 1454291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry *match, 1455291d1e72SVladimir Oltean int *last_unused) 1456291d1e72SVladimir Oltean { 1457291d1e72SVladimir Oltean int way; 1458291d1e72SVladimir Oltean 1459291d1e72SVladimir Oltean for (way = 0; way < SJA1105ET_FDB_BIN_SIZE; way++) { 1460291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 1461291d1e72SVladimir Oltean int index = sja1105et_fdb_index(bin, way); 1462291d1e72SVladimir Oltean 1463291d1e72SVladimir Oltean /* Skip unused entries, optionally marking them 1464291d1e72SVladimir Oltean * into the return value 1465291d1e72SVladimir Oltean */ 1466291d1e72SVladimir Oltean if (sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 1467291d1e72SVladimir Oltean index, &l2_lookup)) { 1468291d1e72SVladimir Oltean if (last_unused) 1469291d1e72SVladimir Oltean *last_unused = way; 1470291d1e72SVladimir Oltean continue; 1471291d1e72SVladimir Oltean } 1472291d1e72SVladimir Oltean 1473291d1e72SVladimir Oltean if (l2_lookup.macaddr == ether_addr_to_u64(addr) && 1474291d1e72SVladimir Oltean l2_lookup.vlanid == vid) { 1475291d1e72SVladimir Oltean if (match) 1476291d1e72SVladimir Oltean *match = l2_lookup; 1477291d1e72SVladimir Oltean return way; 1478291d1e72SVladimir Oltean } 1479291d1e72SVladimir Oltean } 1480291d1e72SVladimir Oltean /* Return an invalid entry index if not found */ 1481291d1e72SVladimir Oltean return -1; 1482291d1e72SVladimir Oltean } 1483291d1e72SVladimir Oltean 14849dfa6911SVladimir Oltean int sja1105et_fdb_add(struct dsa_switch *ds, int port, 1485291d1e72SVladimir Oltean const unsigned char *addr, u16 vid) 1486291d1e72SVladimir Oltean { 1487291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 1488291d1e72SVladimir Oltean struct sja1105_private *priv = ds->priv; 1489291d1e72SVladimir Oltean struct device *dev = ds->dev; 1490291d1e72SVladimir Oltean int last_unused = -1; 149160f6053fSVladimir Oltean int bin, way, rc; 1492291d1e72SVladimir Oltean 14939dfa6911SVladimir Oltean bin = sja1105et_fdb_hash(priv, addr, vid); 1494291d1e72SVladimir Oltean 14959dfa6911SVladimir Oltean way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid, 1496291d1e72SVladimir Oltean &l2_lookup, &last_unused); 1497291d1e72SVladimir Oltean if (way >= 0) { 1498291d1e72SVladimir Oltean /* We have an FDB entry. Is our port in the destination 1499291d1e72SVladimir Oltean * mask? If yes, we need to do nothing. If not, we need 1500291d1e72SVladimir Oltean * to rewrite the entry by adding this port to it. 1501291d1e72SVladimir Oltean */ 1502291d1e72SVladimir Oltean if (l2_lookup.destports & BIT(port)) 1503291d1e72SVladimir Oltean return 0; 1504291d1e72SVladimir Oltean l2_lookup.destports |= BIT(port); 1505291d1e72SVladimir Oltean } else { 1506291d1e72SVladimir Oltean int index = sja1105et_fdb_index(bin, way); 1507291d1e72SVladimir Oltean 1508291d1e72SVladimir Oltean /* We don't have an FDB entry. We construct a new one and 1509291d1e72SVladimir Oltean * try to find a place for it within the FDB table. 1510291d1e72SVladimir Oltean */ 1511291d1e72SVladimir Oltean l2_lookup.macaddr = ether_addr_to_u64(addr); 1512291d1e72SVladimir Oltean l2_lookup.destports = BIT(port); 1513291d1e72SVladimir Oltean l2_lookup.vlanid = vid; 1514291d1e72SVladimir Oltean 1515291d1e72SVladimir Oltean if (last_unused >= 0) { 1516291d1e72SVladimir Oltean way = last_unused; 1517291d1e72SVladimir Oltean } else { 1518291d1e72SVladimir Oltean /* Bin is full, need to evict somebody. 1519291d1e72SVladimir Oltean * Choose victim at random. If you get these messages 1520291d1e72SVladimir Oltean * often, you may need to consider changing the 1521291d1e72SVladimir Oltean * distribution function: 1522291d1e72SVladimir Oltean * static_config[BLK_IDX_L2_LOOKUP_PARAMS].entries->poly 1523291d1e72SVladimir Oltean */ 1524291d1e72SVladimir Oltean get_random_bytes(&way, sizeof(u8)); 1525291d1e72SVladimir Oltean way %= SJA1105ET_FDB_BIN_SIZE; 1526291d1e72SVladimir Oltean dev_warn(dev, "Warning, FDB bin %d full while adding entry for %pM. Evicting entry %u.\n", 1527291d1e72SVladimir Oltean bin, addr, way); 1528291d1e72SVladimir Oltean /* Evict entry */ 1529291d1e72SVladimir Oltean sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 1530291d1e72SVladimir Oltean index, NULL, false); 1531291d1e72SVladimir Oltean } 1532291d1e72SVladimir Oltean } 1533291d1e72SVladimir Oltean l2_lookup.index = sja1105et_fdb_index(bin, way); 1534291d1e72SVladimir Oltean 153560f6053fSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 1536291d1e72SVladimir Oltean l2_lookup.index, &l2_lookup, 1537291d1e72SVladimir Oltean true); 153860f6053fSVladimir Oltean if (rc < 0) 153960f6053fSVladimir Oltean return rc; 154060f6053fSVladimir Oltean 154160f6053fSVladimir Oltean return sja1105_static_fdb_change(priv, port, &l2_lookup, true); 1542291d1e72SVladimir Oltean } 1543291d1e72SVladimir Oltean 15449dfa6911SVladimir Oltean int sja1105et_fdb_del(struct dsa_switch *ds, int port, 1545291d1e72SVladimir Oltean const unsigned char *addr, u16 vid) 1546291d1e72SVladimir Oltean { 1547291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 1548291d1e72SVladimir Oltean struct sja1105_private *priv = ds->priv; 154960f6053fSVladimir Oltean int index, bin, way, rc; 1550291d1e72SVladimir Oltean bool keep; 1551291d1e72SVladimir Oltean 15529dfa6911SVladimir Oltean bin = sja1105et_fdb_hash(priv, addr, vid); 15539dfa6911SVladimir Oltean way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid, 1554291d1e72SVladimir Oltean &l2_lookup, NULL); 1555291d1e72SVladimir Oltean if (way < 0) 1556291d1e72SVladimir Oltean return 0; 1557291d1e72SVladimir Oltean index = sja1105et_fdb_index(bin, way); 1558291d1e72SVladimir Oltean 1559291d1e72SVladimir Oltean /* We have an FDB entry. Is our port in the destination mask? If yes, 1560291d1e72SVladimir Oltean * we need to remove it. If the resulting port mask becomes empty, we 1561291d1e72SVladimir Oltean * need to completely evict the FDB entry. 1562291d1e72SVladimir Oltean * Otherwise we just write it back. 1563291d1e72SVladimir Oltean */ 1564291d1e72SVladimir Oltean l2_lookup.destports &= ~BIT(port); 15657752e937SVladimir Oltean 1566291d1e72SVladimir Oltean if (l2_lookup.destports) 1567291d1e72SVladimir Oltean keep = true; 1568291d1e72SVladimir Oltean else 1569291d1e72SVladimir Oltean keep = false; 1570291d1e72SVladimir Oltean 157160f6053fSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 1572291d1e72SVladimir Oltean index, &l2_lookup, keep); 157360f6053fSVladimir Oltean if (rc < 0) 157460f6053fSVladimir Oltean return rc; 157560f6053fSVladimir Oltean 157660f6053fSVladimir Oltean return sja1105_static_fdb_change(priv, port, &l2_lookup, keep); 1577291d1e72SVladimir Oltean } 1578291d1e72SVladimir Oltean 15799dfa6911SVladimir Oltean int sja1105pqrs_fdb_add(struct dsa_switch *ds, int port, 15809dfa6911SVladimir Oltean const unsigned char *addr, u16 vid) 15819dfa6911SVladimir Oltean { 15821da73821SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 15831da73821SVladimir Oltean struct sja1105_private *priv = ds->priv; 15841da73821SVladimir Oltean int rc, i; 15851da73821SVladimir Oltean 15861da73821SVladimir Oltean /* Search for an existing entry in the FDB table */ 15871da73821SVladimir Oltean l2_lookup.macaddr = ether_addr_to_u64(addr); 15881da73821SVladimir Oltean l2_lookup.vlanid = vid; 15891da73821SVladimir Oltean l2_lookup.iotag = SJA1105_S_TAG; 15901da73821SVladimir Oltean l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0); 15910fac6aa0SVladimir Oltean if (priv->vlan_aware) { 15921da73821SVladimir Oltean l2_lookup.mask_vlanid = VLAN_VID_MASK; 15931da73821SVladimir Oltean l2_lookup.mask_iotag = BIT(0); 15946d7c7d94SVladimir Oltean } else { 15956d7c7d94SVladimir Oltean l2_lookup.mask_vlanid = 0; 15966d7c7d94SVladimir Oltean l2_lookup.mask_iotag = 0; 15976d7c7d94SVladimir Oltean } 15981da73821SVladimir Oltean l2_lookup.destports = BIT(port); 15991da73821SVladimir Oltean 16001da73821SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 16011da73821SVladimir Oltean SJA1105_SEARCH, &l2_lookup); 16021da73821SVladimir Oltean if (rc == 0) { 16031da73821SVladimir Oltean /* Found and this port is already in the entry's 16041da73821SVladimir Oltean * port mask => job done 16051da73821SVladimir Oltean */ 16061da73821SVladimir Oltean if (l2_lookup.destports & BIT(port)) 16071da73821SVladimir Oltean return 0; 16081da73821SVladimir Oltean /* l2_lookup.index is populated by the switch in case it 16091da73821SVladimir Oltean * found something. 16101da73821SVladimir Oltean */ 16111da73821SVladimir Oltean l2_lookup.destports |= BIT(port); 16121da73821SVladimir Oltean goto skip_finding_an_index; 16131da73821SVladimir Oltean } 16141da73821SVladimir Oltean 16151da73821SVladimir Oltean /* Not found, so try to find an unused spot in the FDB. 16161da73821SVladimir Oltean * This is slightly inefficient because the strategy is knock-knock at 16171da73821SVladimir Oltean * every possible position from 0 to 1023. 16181da73821SVladimir Oltean */ 16191da73821SVladimir Oltean for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) { 16201da73821SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 16211da73821SVladimir Oltean i, NULL); 16221da73821SVladimir Oltean if (rc < 0) 16231da73821SVladimir Oltean break; 16241da73821SVladimir Oltean } 16251da73821SVladimir Oltean if (i == SJA1105_MAX_L2_LOOKUP_COUNT) { 16261da73821SVladimir Oltean dev_err(ds->dev, "FDB is full, cannot add entry.\n"); 16271da73821SVladimir Oltean return -EINVAL; 16281da73821SVladimir Oltean } 162917ae6555SVladimir Oltean l2_lookup.lockeds = true; 16301da73821SVladimir Oltean l2_lookup.index = i; 16311da73821SVladimir Oltean 16321da73821SVladimir Oltean skip_finding_an_index: 163360f6053fSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 16341da73821SVladimir Oltean l2_lookup.index, &l2_lookup, 16351da73821SVladimir Oltean true); 163660f6053fSVladimir Oltean if (rc < 0) 163760f6053fSVladimir Oltean return rc; 163860f6053fSVladimir Oltean 163960f6053fSVladimir Oltean return sja1105_static_fdb_change(priv, port, &l2_lookup, true); 16409dfa6911SVladimir Oltean } 16419dfa6911SVladimir Oltean 16429dfa6911SVladimir Oltean int sja1105pqrs_fdb_del(struct dsa_switch *ds, int port, 16439dfa6911SVladimir Oltean const unsigned char *addr, u16 vid) 16449dfa6911SVladimir Oltean { 16451da73821SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 16461da73821SVladimir Oltean struct sja1105_private *priv = ds->priv; 16471da73821SVladimir Oltean bool keep; 16481da73821SVladimir Oltean int rc; 16491da73821SVladimir Oltean 16501da73821SVladimir Oltean l2_lookup.macaddr = ether_addr_to_u64(addr); 16511da73821SVladimir Oltean l2_lookup.vlanid = vid; 16521da73821SVladimir Oltean l2_lookup.iotag = SJA1105_S_TAG; 16531da73821SVladimir Oltean l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0); 16540fac6aa0SVladimir Oltean if (priv->vlan_aware) { 16551da73821SVladimir Oltean l2_lookup.mask_vlanid = VLAN_VID_MASK; 16561da73821SVladimir Oltean l2_lookup.mask_iotag = BIT(0); 16576d7c7d94SVladimir Oltean } else { 16586d7c7d94SVladimir Oltean l2_lookup.mask_vlanid = 0; 16596d7c7d94SVladimir Oltean l2_lookup.mask_iotag = 0; 16606d7c7d94SVladimir Oltean } 16611da73821SVladimir Oltean l2_lookup.destports = BIT(port); 16621da73821SVladimir Oltean 16631da73821SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 16641da73821SVladimir Oltean SJA1105_SEARCH, &l2_lookup); 16651da73821SVladimir Oltean if (rc < 0) 16661da73821SVladimir Oltean return 0; 16671da73821SVladimir Oltean 16681da73821SVladimir Oltean l2_lookup.destports &= ~BIT(port); 16691da73821SVladimir Oltean 16701da73821SVladimir Oltean /* Decide whether we remove just this port from the FDB entry, 16711da73821SVladimir Oltean * or if we remove it completely. 16721da73821SVladimir Oltean */ 16731da73821SVladimir Oltean if (l2_lookup.destports) 16741da73821SVladimir Oltean keep = true; 16751da73821SVladimir Oltean else 16761da73821SVladimir Oltean keep = false; 16771da73821SVladimir Oltean 167860f6053fSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 16791da73821SVladimir Oltean l2_lookup.index, &l2_lookup, keep); 168060f6053fSVladimir Oltean if (rc < 0) 168160f6053fSVladimir Oltean return rc; 168260f6053fSVladimir Oltean 168360f6053fSVladimir Oltean return sja1105_static_fdb_change(priv, port, &l2_lookup, keep); 16849dfa6911SVladimir Oltean } 16859dfa6911SVladimir Oltean 16869dfa6911SVladimir Oltean static int sja1105_fdb_add(struct dsa_switch *ds, int port, 16879dfa6911SVladimir Oltean const unsigned char *addr, u16 vid) 16889dfa6911SVladimir Oltean { 16899dfa6911SVladimir Oltean struct sja1105_private *priv = ds->priv; 1690b3ee526aSVladimir Oltean 16916d7c7d94SVladimir Oltean return priv->info->fdb_add_cmd(ds, port, addr, vid); 16929dfa6911SVladimir Oltean } 16939dfa6911SVladimir Oltean 16949dfa6911SVladimir Oltean static int sja1105_fdb_del(struct dsa_switch *ds, int port, 16959dfa6911SVladimir Oltean const unsigned char *addr, u16 vid) 16969dfa6911SVladimir Oltean { 16979dfa6911SVladimir Oltean struct sja1105_private *priv = ds->priv; 16989dfa6911SVladimir Oltean 1699b3ee526aSVladimir Oltean return priv->info->fdb_del_cmd(ds, port, addr, vid); 17009dfa6911SVladimir Oltean } 17019dfa6911SVladimir Oltean 1702291d1e72SVladimir Oltean static int sja1105_fdb_dump(struct dsa_switch *ds, int port, 1703291d1e72SVladimir Oltean dsa_fdb_dump_cb_t *cb, void *data) 1704291d1e72SVladimir Oltean { 1705291d1e72SVladimir Oltean struct sja1105_private *priv = ds->priv; 1706291d1e72SVladimir Oltean struct device *dev = ds->dev; 1707291d1e72SVladimir Oltean int i; 1708291d1e72SVladimir Oltean 1709291d1e72SVladimir Oltean for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) { 1710291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 1711291d1e72SVladimir Oltean u8 macaddr[ETH_ALEN]; 1712291d1e72SVladimir Oltean int rc; 1713291d1e72SVladimir Oltean 1714291d1e72SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 1715291d1e72SVladimir Oltean i, &l2_lookup); 1716291d1e72SVladimir Oltean /* No fdb entry at i, not an issue */ 1717def84604SVladimir Oltean if (rc == -ENOENT) 1718291d1e72SVladimir Oltean continue; 1719291d1e72SVladimir Oltean if (rc) { 1720291d1e72SVladimir Oltean dev_err(dev, "Failed to dump FDB: %d\n", rc); 1721291d1e72SVladimir Oltean return rc; 1722291d1e72SVladimir Oltean } 1723291d1e72SVladimir Oltean 1724291d1e72SVladimir Oltean /* FDB dump callback is per port. This means we have to 1725291d1e72SVladimir Oltean * disregard a valid entry if it's not for this port, even if 1726291d1e72SVladimir Oltean * only to revisit it later. This is inefficient because the 1727291d1e72SVladimir Oltean * 1024-sized FDB table needs to be traversed 4 times through 1728291d1e72SVladimir Oltean * SPI during a 'bridge fdb show' command. 1729291d1e72SVladimir Oltean */ 1730291d1e72SVladimir Oltean if (!(l2_lookup.destports & BIT(port))) 1731291d1e72SVladimir Oltean continue; 17324d942354SVladimir Oltean 17334d942354SVladimir Oltean /* We need to hide the FDB entry for unknown multicast */ 17344d942354SVladimir Oltean if (l2_lookup.macaddr == SJA1105_UNKNOWN_MULTICAST && 17354d942354SVladimir Oltean l2_lookup.mask_macaddr == SJA1105_UNKNOWN_MULTICAST) 17364d942354SVladimir Oltean continue; 17374d942354SVladimir Oltean 1738291d1e72SVladimir Oltean u64_to_ether_addr(l2_lookup.macaddr, macaddr); 173993647594SVladimir Oltean 17406d7c7d94SVladimir Oltean /* We need to hide the dsa_8021q VLANs from the user. */ 17410fac6aa0SVladimir Oltean if (!priv->vlan_aware) 17426d7c7d94SVladimir Oltean l2_lookup.vlanid = 0; 174317ae6555SVladimir Oltean cb(macaddr, l2_lookup.vlanid, l2_lookup.lockeds, data); 1744291d1e72SVladimir Oltean } 1745291d1e72SVladimir Oltean return 0; 1746291d1e72SVladimir Oltean } 1747291d1e72SVladimir Oltean 1748a52b2da7SVladimir Oltean static int sja1105_mdb_add(struct dsa_switch *ds, int port, 1749291d1e72SVladimir Oltean const struct switchdev_obj_port_mdb *mdb) 1750291d1e72SVladimir Oltean { 1751a52b2da7SVladimir Oltean return sja1105_fdb_add(ds, port, mdb->addr, mdb->vid); 1752291d1e72SVladimir Oltean } 1753291d1e72SVladimir Oltean 1754291d1e72SVladimir Oltean static int sja1105_mdb_del(struct dsa_switch *ds, int port, 1755291d1e72SVladimir Oltean const struct switchdev_obj_port_mdb *mdb) 1756291d1e72SVladimir Oltean { 1757291d1e72SVladimir Oltean return sja1105_fdb_del(ds, port, mdb->addr, mdb->vid); 1758291d1e72SVladimir Oltean } 1759291d1e72SVladimir Oltean 17607f7ccdeaSVladimir Oltean /* Common function for unicast and broadcast flood configuration. 17617f7ccdeaSVladimir Oltean * Flooding is configured between each {ingress, egress} port pair, and since 17627f7ccdeaSVladimir Oltean * the bridge's semantics are those of "egress flooding", it means we must 17637f7ccdeaSVladimir Oltean * enable flooding towards this port from all ingress ports that are in the 17647f7ccdeaSVladimir Oltean * same forwarding domain. 17657f7ccdeaSVladimir Oltean */ 17667f7ccdeaSVladimir Oltean static int sja1105_manage_flood_domains(struct sja1105_private *priv) 17677f7ccdeaSVladimir Oltean { 17687f7ccdeaSVladimir Oltean struct sja1105_l2_forwarding_entry *l2_fwd; 17697f7ccdeaSVladimir Oltean struct dsa_switch *ds = priv->ds; 17707f7ccdeaSVladimir Oltean int from, to, rc; 17717f7ccdeaSVladimir Oltean 17727f7ccdeaSVladimir Oltean l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries; 17737f7ccdeaSVladimir Oltean 17747f7ccdeaSVladimir Oltean for (from = 0; from < ds->num_ports; from++) { 17757f7ccdeaSVladimir Oltean u64 fl_domain = 0, bc_domain = 0; 17767f7ccdeaSVladimir Oltean 17777f7ccdeaSVladimir Oltean for (to = 0; to < priv->ds->num_ports; to++) { 17787f7ccdeaSVladimir Oltean if (!sja1105_can_forward(l2_fwd, from, to)) 17797f7ccdeaSVladimir Oltean continue; 17807f7ccdeaSVladimir Oltean 17817f7ccdeaSVladimir Oltean if (priv->ucast_egress_floods & BIT(to)) 17827f7ccdeaSVladimir Oltean fl_domain |= BIT(to); 17837f7ccdeaSVladimir Oltean if (priv->bcast_egress_floods & BIT(to)) 17847f7ccdeaSVladimir Oltean bc_domain |= BIT(to); 17857f7ccdeaSVladimir Oltean } 17867f7ccdeaSVladimir Oltean 17877f7ccdeaSVladimir Oltean /* Nothing changed, nothing to do */ 17887f7ccdeaSVladimir Oltean if (l2_fwd[from].fl_domain == fl_domain && 17897f7ccdeaSVladimir Oltean l2_fwd[from].bc_domain == bc_domain) 17907f7ccdeaSVladimir Oltean continue; 17917f7ccdeaSVladimir Oltean 17927f7ccdeaSVladimir Oltean l2_fwd[from].fl_domain = fl_domain; 17937f7ccdeaSVladimir Oltean l2_fwd[from].bc_domain = bc_domain; 17947f7ccdeaSVladimir Oltean 17957f7ccdeaSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING, 17967f7ccdeaSVladimir Oltean from, &l2_fwd[from], true); 17977f7ccdeaSVladimir Oltean if (rc < 0) 17987f7ccdeaSVladimir Oltean return rc; 17997f7ccdeaSVladimir Oltean } 18007f7ccdeaSVladimir Oltean 18017f7ccdeaSVladimir Oltean return 0; 18027f7ccdeaSVladimir Oltean } 18037f7ccdeaSVladimir Oltean 18048aa9ebccSVladimir Oltean static int sja1105_bridge_member(struct dsa_switch *ds, int port, 18058aa9ebccSVladimir Oltean struct net_device *br, bool member) 18068aa9ebccSVladimir Oltean { 18078aa9ebccSVladimir Oltean struct sja1105_l2_forwarding_entry *l2_fwd; 18088aa9ebccSVladimir Oltean struct sja1105_private *priv = ds->priv; 18098aa9ebccSVladimir Oltean int i, rc; 18108aa9ebccSVladimir Oltean 18118aa9ebccSVladimir Oltean l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries; 18128aa9ebccSVladimir Oltean 1813542043e9SVladimir Oltean for (i = 0; i < ds->num_ports; i++) { 18148aa9ebccSVladimir Oltean /* Add this port to the forwarding matrix of the 18158aa9ebccSVladimir Oltean * other ports in the same bridge, and viceversa. 18168aa9ebccSVladimir Oltean */ 18178aa9ebccSVladimir Oltean if (!dsa_is_user_port(ds, i)) 18188aa9ebccSVladimir Oltean continue; 18198aa9ebccSVladimir Oltean /* For the ports already under the bridge, only one thing needs 18208aa9ebccSVladimir Oltean * to be done, and that is to add this port to their 18218aa9ebccSVladimir Oltean * reachability domain. So we can perform the SPI write for 18228aa9ebccSVladimir Oltean * them immediately. However, for this port itself (the one 18238aa9ebccSVladimir Oltean * that is new to the bridge), we need to add all other ports 18248aa9ebccSVladimir Oltean * to its reachability domain. So we do that incrementally in 18258aa9ebccSVladimir Oltean * this loop, and perform the SPI write only at the end, once 18268aa9ebccSVladimir Oltean * the domain contains all other bridge ports. 18278aa9ebccSVladimir Oltean */ 18288aa9ebccSVladimir Oltean if (i == port) 18298aa9ebccSVladimir Oltean continue; 18308aa9ebccSVladimir Oltean if (dsa_to_port(ds, i)->bridge_dev != br) 18318aa9ebccSVladimir Oltean continue; 18328aa9ebccSVladimir Oltean sja1105_port_allow_traffic(l2_fwd, i, port, member); 18338aa9ebccSVladimir Oltean sja1105_port_allow_traffic(l2_fwd, port, i, member); 18348aa9ebccSVladimir Oltean 18358aa9ebccSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING, 18368aa9ebccSVladimir Oltean i, &l2_fwd[i], true); 18378aa9ebccSVladimir Oltean if (rc < 0) 18388aa9ebccSVladimir Oltean return rc; 18398aa9ebccSVladimir Oltean } 18408aa9ebccSVladimir Oltean 18417f7ccdeaSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING, 18428aa9ebccSVladimir Oltean port, &l2_fwd[port], true); 18437f7ccdeaSVladimir Oltean if (rc) 18447f7ccdeaSVladimir Oltean return rc; 18457f7ccdeaSVladimir Oltean 1846cde8078eSVladimir Oltean rc = sja1105_commit_pvid(ds, port); 1847cde8078eSVladimir Oltean if (rc) 1848cde8078eSVladimir Oltean return rc; 1849cde8078eSVladimir Oltean 18507f7ccdeaSVladimir Oltean return sja1105_manage_flood_domains(priv); 18518aa9ebccSVladimir Oltean } 18528aa9ebccSVladimir Oltean 1853640f763fSVladimir Oltean static void sja1105_bridge_stp_state_set(struct dsa_switch *ds, int port, 1854640f763fSVladimir Oltean u8 state) 1855640f763fSVladimir Oltean { 1856640f763fSVladimir Oltean struct sja1105_private *priv = ds->priv; 1857640f763fSVladimir Oltean struct sja1105_mac_config_entry *mac; 1858640f763fSVladimir Oltean 1859640f763fSVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 1860640f763fSVladimir Oltean 1861640f763fSVladimir Oltean switch (state) { 1862640f763fSVladimir Oltean case BR_STATE_DISABLED: 1863640f763fSVladimir Oltean case BR_STATE_BLOCKING: 1864640f763fSVladimir Oltean /* From UM10944 description of DRPDTAG (why put this there?): 1865640f763fSVladimir Oltean * "Management traffic flows to the port regardless of the state 1866640f763fSVladimir Oltean * of the INGRESS flag". So BPDUs are still be allowed to pass. 1867640f763fSVladimir Oltean * At the moment no difference between DISABLED and BLOCKING. 1868640f763fSVladimir Oltean */ 1869640f763fSVladimir Oltean mac[port].ingress = false; 1870640f763fSVladimir Oltean mac[port].egress = false; 1871640f763fSVladimir Oltean mac[port].dyn_learn = false; 1872640f763fSVladimir Oltean break; 1873640f763fSVladimir Oltean case BR_STATE_LISTENING: 1874640f763fSVladimir Oltean mac[port].ingress = true; 1875640f763fSVladimir Oltean mac[port].egress = false; 1876640f763fSVladimir Oltean mac[port].dyn_learn = false; 1877640f763fSVladimir Oltean break; 1878640f763fSVladimir Oltean case BR_STATE_LEARNING: 1879640f763fSVladimir Oltean mac[port].ingress = true; 1880640f763fSVladimir Oltean mac[port].egress = false; 18814d942354SVladimir Oltean mac[port].dyn_learn = !!(priv->learn_ena & BIT(port)); 1882640f763fSVladimir Oltean break; 1883640f763fSVladimir Oltean case BR_STATE_FORWARDING: 1884640f763fSVladimir Oltean mac[port].ingress = true; 1885640f763fSVladimir Oltean mac[port].egress = true; 18864d942354SVladimir Oltean mac[port].dyn_learn = !!(priv->learn_ena & BIT(port)); 1887640f763fSVladimir Oltean break; 1888640f763fSVladimir Oltean default: 1889640f763fSVladimir Oltean dev_err(ds->dev, "invalid STP state: %d\n", state); 1890640f763fSVladimir Oltean return; 1891640f763fSVladimir Oltean } 1892640f763fSVladimir Oltean 1893640f763fSVladimir Oltean sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, 1894640f763fSVladimir Oltean &mac[port], true); 1895640f763fSVladimir Oltean } 1896640f763fSVladimir Oltean 18978aa9ebccSVladimir Oltean static int sja1105_bridge_join(struct dsa_switch *ds, int port, 18988aa9ebccSVladimir Oltean struct net_device *br) 18998aa9ebccSVladimir Oltean { 19008aa9ebccSVladimir Oltean return sja1105_bridge_member(ds, port, br, true); 19018aa9ebccSVladimir Oltean } 19028aa9ebccSVladimir Oltean 19038aa9ebccSVladimir Oltean static void sja1105_bridge_leave(struct dsa_switch *ds, int port, 19048aa9ebccSVladimir Oltean struct net_device *br) 19058aa9ebccSVladimir Oltean { 19068aa9ebccSVladimir Oltean sja1105_bridge_member(ds, port, br, false); 19078aa9ebccSVladimir Oltean } 19088aa9ebccSVladimir Oltean 19094d752508SVladimir Oltean #define BYTES_PER_KBIT (1000LL / 8) 19104d752508SVladimir Oltean 19114d752508SVladimir Oltean static int sja1105_find_unused_cbs_shaper(struct sja1105_private *priv) 19124d752508SVladimir Oltean { 19134d752508SVladimir Oltean int i; 19144d752508SVladimir Oltean 19154d752508SVladimir Oltean for (i = 0; i < priv->info->num_cbs_shapers; i++) 19164d752508SVladimir Oltean if (!priv->cbs[i].idle_slope && !priv->cbs[i].send_slope) 19174d752508SVladimir Oltean return i; 19184d752508SVladimir Oltean 19194d752508SVladimir Oltean return -1; 19204d752508SVladimir Oltean } 19214d752508SVladimir Oltean 19224d752508SVladimir Oltean static int sja1105_delete_cbs_shaper(struct sja1105_private *priv, int port, 19234d752508SVladimir Oltean int prio) 19244d752508SVladimir Oltean { 19254d752508SVladimir Oltean int i; 19264d752508SVladimir Oltean 19274d752508SVladimir Oltean for (i = 0; i < priv->info->num_cbs_shapers; i++) { 19284d752508SVladimir Oltean struct sja1105_cbs_entry *cbs = &priv->cbs[i]; 19294d752508SVladimir Oltean 19304d752508SVladimir Oltean if (cbs->port == port && cbs->prio == prio) { 19314d752508SVladimir Oltean memset(cbs, 0, sizeof(*cbs)); 19324d752508SVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_CBS, 19334d752508SVladimir Oltean i, cbs, true); 19344d752508SVladimir Oltean } 19354d752508SVladimir Oltean } 19364d752508SVladimir Oltean 19374d752508SVladimir Oltean return 0; 19384d752508SVladimir Oltean } 19394d752508SVladimir Oltean 19404d752508SVladimir Oltean static int sja1105_setup_tc_cbs(struct dsa_switch *ds, int port, 19414d752508SVladimir Oltean struct tc_cbs_qopt_offload *offload) 19424d752508SVladimir Oltean { 19434d752508SVladimir Oltean struct sja1105_private *priv = ds->priv; 19444d752508SVladimir Oltean struct sja1105_cbs_entry *cbs; 19454d752508SVladimir Oltean int index; 19464d752508SVladimir Oltean 19474d752508SVladimir Oltean if (!offload->enable) 19484d752508SVladimir Oltean return sja1105_delete_cbs_shaper(priv, port, offload->queue); 19494d752508SVladimir Oltean 19504d752508SVladimir Oltean index = sja1105_find_unused_cbs_shaper(priv); 19514d752508SVladimir Oltean if (index < 0) 19524d752508SVladimir Oltean return -ENOSPC; 19534d752508SVladimir Oltean 19544d752508SVladimir Oltean cbs = &priv->cbs[index]; 19554d752508SVladimir Oltean cbs->port = port; 19564d752508SVladimir Oltean cbs->prio = offload->queue; 19574d752508SVladimir Oltean /* locredit and sendslope are negative by definition. In hardware, 19584d752508SVladimir Oltean * positive values must be provided, and the negative sign is implicit. 19594d752508SVladimir Oltean */ 19604d752508SVladimir Oltean cbs->credit_hi = offload->hicredit; 19614d752508SVladimir Oltean cbs->credit_lo = abs(offload->locredit); 19624d752508SVladimir Oltean /* User space is in kbits/sec, hardware in bytes/sec */ 19634d752508SVladimir Oltean cbs->idle_slope = offload->idleslope * BYTES_PER_KBIT; 19644d752508SVladimir Oltean cbs->send_slope = abs(offload->sendslope * BYTES_PER_KBIT); 19654d752508SVladimir Oltean /* Convert the negative values from 64-bit 2's complement 19664d752508SVladimir Oltean * to 32-bit 2's complement (for the case of 0x80000000 whose 19674d752508SVladimir Oltean * negative is still negative). 19684d752508SVladimir Oltean */ 19694d752508SVladimir Oltean cbs->credit_lo &= GENMASK_ULL(31, 0); 19704d752508SVladimir Oltean cbs->send_slope &= GENMASK_ULL(31, 0); 19714d752508SVladimir Oltean 19724d752508SVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_CBS, index, cbs, 19734d752508SVladimir Oltean true); 19744d752508SVladimir Oltean } 19754d752508SVladimir Oltean 19764d752508SVladimir Oltean static int sja1105_reload_cbs(struct sja1105_private *priv) 19774d752508SVladimir Oltean { 19784d752508SVladimir Oltean int rc = 0, i; 19794d752508SVladimir Oltean 1980be7f62eeSVladimir Oltean /* The credit based shapers are only allocated if 1981be7f62eeSVladimir Oltean * CONFIG_NET_SCH_CBS is enabled. 1982be7f62eeSVladimir Oltean */ 1983be7f62eeSVladimir Oltean if (!priv->cbs) 1984be7f62eeSVladimir Oltean return 0; 1985be7f62eeSVladimir Oltean 19864d752508SVladimir Oltean for (i = 0; i < priv->info->num_cbs_shapers; i++) { 19874d752508SVladimir Oltean struct sja1105_cbs_entry *cbs = &priv->cbs[i]; 19884d752508SVladimir Oltean 19894d752508SVladimir Oltean if (!cbs->idle_slope && !cbs->send_slope) 19904d752508SVladimir Oltean continue; 19914d752508SVladimir Oltean 19924d752508SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_CBS, i, cbs, 19934d752508SVladimir Oltean true); 19944d752508SVladimir Oltean if (rc) 19954d752508SVladimir Oltean break; 19964d752508SVladimir Oltean } 19974d752508SVladimir Oltean 19984d752508SVladimir Oltean return rc; 19994d752508SVladimir Oltean } 20004d752508SVladimir Oltean 20012eea1fa8SVladimir Oltean static const char * const sja1105_reset_reasons[] = { 20022eea1fa8SVladimir Oltean [SJA1105_VLAN_FILTERING] = "VLAN filtering", 20032eea1fa8SVladimir Oltean [SJA1105_RX_HWTSTAMPING] = "RX timestamping", 20042eea1fa8SVladimir Oltean [SJA1105_AGEING_TIME] = "Ageing time", 20052eea1fa8SVladimir Oltean [SJA1105_SCHEDULING] = "Time-aware scheduling", 2006c279c726SVladimir Oltean [SJA1105_BEST_EFFORT_POLICING] = "Best-effort policing", 2007dfacc5a2SVladimir Oltean [SJA1105_VIRTUAL_LINKS] = "Virtual links", 20082eea1fa8SVladimir Oltean }; 20092eea1fa8SVladimir Oltean 20106666cebcSVladimir Oltean /* For situations where we need to change a setting at runtime that is only 20116666cebcSVladimir Oltean * available through the static configuration, resetting the switch in order 20126666cebcSVladimir Oltean * to upload the new static config is unavoidable. Back up the settings we 20136666cebcSVladimir Oltean * modify at runtime (currently only MAC) and restore them after uploading, 20146666cebcSVladimir Oltean * such that this operation is relatively seamless. 20156666cebcSVladimir Oltean */ 20162eea1fa8SVladimir Oltean int sja1105_static_config_reload(struct sja1105_private *priv, 20172eea1fa8SVladimir Oltean enum sja1105_reset_reason reason) 20186666cebcSVladimir Oltean { 20196cf99c13SVladimir Oltean struct ptp_system_timestamp ptp_sts_before; 20206cf99c13SVladimir Oltean struct ptp_system_timestamp ptp_sts_after; 202182760d7fSVladimir Oltean int speed_mbps[SJA1105_MAX_NUM_PORTS]; 202284db00f2SVladimir Oltean u16 bmcr[SJA1105_MAX_NUM_PORTS] = {0}; 20236666cebcSVladimir Oltean struct sja1105_mac_config_entry *mac; 20246cf99c13SVladimir Oltean struct dsa_switch *ds = priv->ds; 20256cf99c13SVladimir Oltean s64 t1, t2, t3, t4; 20266cf99c13SVladimir Oltean s64 t12, t34; 20276666cebcSVladimir Oltean int rc, i; 20286cf99c13SVladimir Oltean s64 now; 20296666cebcSVladimir Oltean 2030af580ae2SVladimir Oltean mutex_lock(&priv->mgmt_lock); 2031af580ae2SVladimir Oltean 20326666cebcSVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 20336666cebcSVladimir Oltean 20348400cff6SVladimir Oltean /* Back up the dynamic link speed changed by sja1105_adjust_port_config 20358400cff6SVladimir Oltean * in order to temporarily restore it to SJA1105_SPEED_AUTO - which the 20368400cff6SVladimir Oltean * switch wants to see in the static config in order to allow us to 20378400cff6SVladimir Oltean * change it through the dynamic interface later. 20386666cebcSVladimir Oltean */ 2039542043e9SVladimir Oltean for (i = 0; i < ds->num_ports; i++) { 20403ad1d171SVladimir Oltean u32 reg_addr = mdiobus_c45_addr(MDIO_MMD_VEND2, MDIO_CTRL1); 20413ad1d171SVladimir Oltean 204241fed17fSVladimir Oltean speed_mbps[i] = sja1105_port_speed_to_ethtool(priv, 204341fed17fSVladimir Oltean mac[i].speed); 204441fed17fSVladimir Oltean mac[i].speed = priv->info->port_speed[SJA1105_SPEED_AUTO]; 20456666cebcSVladimir Oltean 20463ad1d171SVladimir Oltean if (priv->xpcs[i]) 20473ad1d171SVladimir Oltean bmcr[i] = mdiobus_read(priv->mdio_pcs, i, reg_addr); 204884db00f2SVladimir Oltean } 2049ffe10e67SVladimir Oltean 20506cf99c13SVladimir Oltean /* No PTP operations can run right now */ 20516cf99c13SVladimir Oltean mutex_lock(&priv->ptp_data.lock); 20526cf99c13SVladimir Oltean 20536cf99c13SVladimir Oltean rc = __sja1105_ptp_gettimex(ds, &now, &ptp_sts_before); 205461c77533SVladimir Oltean if (rc < 0) { 205561c77533SVladimir Oltean mutex_unlock(&priv->ptp_data.lock); 205661c77533SVladimir Oltean goto out; 205761c77533SVladimir Oltean } 20586cf99c13SVladimir Oltean 20596666cebcSVladimir Oltean /* Reset switch and send updated static configuration */ 20606666cebcSVladimir Oltean rc = sja1105_static_config_upload(priv); 206161c77533SVladimir Oltean if (rc < 0) { 206261c77533SVladimir Oltean mutex_unlock(&priv->ptp_data.lock); 206361c77533SVladimir Oltean goto out; 206461c77533SVladimir Oltean } 20656cf99c13SVladimir Oltean 20666cf99c13SVladimir Oltean rc = __sja1105_ptp_settime(ds, 0, &ptp_sts_after); 206761c77533SVladimir Oltean if (rc < 0) { 206861c77533SVladimir Oltean mutex_unlock(&priv->ptp_data.lock); 206961c77533SVladimir Oltean goto out; 207061c77533SVladimir Oltean } 20716cf99c13SVladimir Oltean 20726cf99c13SVladimir Oltean t1 = timespec64_to_ns(&ptp_sts_before.pre_ts); 20736cf99c13SVladimir Oltean t2 = timespec64_to_ns(&ptp_sts_before.post_ts); 20746cf99c13SVladimir Oltean t3 = timespec64_to_ns(&ptp_sts_after.pre_ts); 20756cf99c13SVladimir Oltean t4 = timespec64_to_ns(&ptp_sts_after.post_ts); 20766cf99c13SVladimir Oltean /* Mid point, corresponds to pre-reset PTPCLKVAL */ 20776cf99c13SVladimir Oltean t12 = t1 + (t2 - t1) / 2; 20786cf99c13SVladimir Oltean /* Mid point, corresponds to post-reset PTPCLKVAL, aka 0 */ 20796cf99c13SVladimir Oltean t34 = t3 + (t4 - t3) / 2; 20806cf99c13SVladimir Oltean /* Advance PTPCLKVAL by the time it took since its readout */ 20816cf99c13SVladimir Oltean now += (t34 - t12); 20826cf99c13SVladimir Oltean 20836cf99c13SVladimir Oltean __sja1105_ptp_adjtime(ds, now); 20846cf99c13SVladimir Oltean 20856cf99c13SVladimir Oltean mutex_unlock(&priv->ptp_data.lock); 20866666cebcSVladimir Oltean 20872eea1fa8SVladimir Oltean dev_info(priv->ds->dev, 20882eea1fa8SVladimir Oltean "Reset switch and programmed static config. Reason: %s\n", 20892eea1fa8SVladimir Oltean sja1105_reset_reasons[reason]); 20902eea1fa8SVladimir Oltean 20916666cebcSVladimir Oltean /* Configure the CGU (PLLs) for MII and RMII PHYs. 20926666cebcSVladimir Oltean * For these interfaces there is no dynamic configuration 20936666cebcSVladimir Oltean * needed, since PLLs have same settings at all speeds. 20946666cebcSVladimir Oltean */ 2095cb5a82d2SVladimir Oltean if (priv->info->clocking_setup) { 2096c5037678SVladimir Oltean rc = priv->info->clocking_setup(priv); 20976666cebcSVladimir Oltean if (rc < 0) 20986666cebcSVladimir Oltean goto out; 2099cb5a82d2SVladimir Oltean } 21006666cebcSVladimir Oltean 2101542043e9SVladimir Oltean for (i = 0; i < ds->num_ports; i++) { 21023ad1d171SVladimir Oltean struct dw_xpcs *xpcs = priv->xpcs[i]; 21033ad1d171SVladimir Oltean unsigned int mode; 210484db00f2SVladimir Oltean 21058400cff6SVladimir Oltean rc = sja1105_adjust_port_config(priv, i, speed_mbps[i]); 21066666cebcSVladimir Oltean if (rc < 0) 21076666cebcSVladimir Oltean goto out; 2108ffe10e67SVladimir Oltean 21093ad1d171SVladimir Oltean if (!xpcs) 211084db00f2SVladimir Oltean continue; 2111ffe10e67SVladimir Oltean 21123ad1d171SVladimir Oltean if (bmcr[i] & BMCR_ANENABLE) 21133ad1d171SVladimir Oltean mode = MLO_AN_INBAND; 21143ad1d171SVladimir Oltean else if (priv->fixed_link[i]) 21153ad1d171SVladimir Oltean mode = MLO_AN_FIXED; 21163ad1d171SVladimir Oltean else 21173ad1d171SVladimir Oltean mode = MLO_AN_PHY; 211884db00f2SVladimir Oltean 21193ad1d171SVladimir Oltean rc = xpcs_do_config(xpcs, priv->phy_mode[i], mode); 21203ad1d171SVladimir Oltean if (rc < 0) 21213ad1d171SVladimir Oltean goto out; 2122ffe10e67SVladimir Oltean 21233ad1d171SVladimir Oltean if (!phylink_autoneg_inband(mode)) { 2124ffe10e67SVladimir Oltean int speed = SPEED_UNKNOWN; 2125ffe10e67SVladimir Oltean 212656b63466SVladimir Oltean if (priv->phy_mode[i] == PHY_INTERFACE_MODE_2500BASEX) 212756b63466SVladimir Oltean speed = SPEED_2500; 212856b63466SVladimir Oltean else if (bmcr[i] & BMCR_SPEED1000) 2129ffe10e67SVladimir Oltean speed = SPEED_1000; 213084db00f2SVladimir Oltean else if (bmcr[i] & BMCR_SPEED100) 2131ffe10e67SVladimir Oltean speed = SPEED_100; 2132053d8ad1SVladimir Oltean else 2133ffe10e67SVladimir Oltean speed = SPEED_10; 2134ffe10e67SVladimir Oltean 21353ad1d171SVladimir Oltean xpcs_link_up(&xpcs->pcs, mode, priv->phy_mode[i], 21363ad1d171SVladimir Oltean speed, DUPLEX_FULL); 2137ffe10e67SVladimir Oltean } 2138ffe10e67SVladimir Oltean } 21394d752508SVladimir Oltean 21404d752508SVladimir Oltean rc = sja1105_reload_cbs(priv); 21414d752508SVladimir Oltean if (rc < 0) 21424d752508SVladimir Oltean goto out; 21436666cebcSVladimir Oltean out: 2144af580ae2SVladimir Oltean mutex_unlock(&priv->mgmt_lock); 2145af580ae2SVladimir Oltean 21466666cebcSVladimir Oltean return rc; 21476666cebcSVladimir Oltean } 21486666cebcSVladimir Oltean 21498aa9ebccSVladimir Oltean static enum dsa_tag_protocol 21504d776482SFlorian Fainelli sja1105_get_tag_protocol(struct dsa_switch *ds, int port, 21514d776482SFlorian Fainelli enum dsa_tag_protocol mp) 21528aa9ebccSVladimir Oltean { 21534913b8ebSVladimir Oltean struct sja1105_private *priv = ds->priv; 21544913b8ebSVladimir Oltean 21554913b8ebSVladimir Oltean return priv->info->tag_proto; 21568aa9ebccSVladimir Oltean } 21578aa9ebccSVladimir Oltean 2158070ca3bbSVladimir Oltean /* The TPID setting belongs to the General Parameters table, 2159070ca3bbSVladimir Oltean * which can only be partially reconfigured at runtime (and not the TPID). 2160070ca3bbSVladimir Oltean * So a switch reset is required. 2161070ca3bbSVladimir Oltean */ 216289153ed6SVladimir Oltean int sja1105_vlan_filtering(struct dsa_switch *ds, int port, bool enabled, 216389153ed6SVladimir Oltean struct netlink_ext_ack *extack) 21646666cebcSVladimir Oltean { 21656d7c7d94SVladimir Oltean struct sja1105_l2_lookup_params_entry *l2_lookup_params; 2166070ca3bbSVladimir Oltean struct sja1105_general_params_entry *general_params; 21676666cebcSVladimir Oltean struct sja1105_private *priv = ds->priv; 2168070ca3bbSVladimir Oltean struct sja1105_table *table; 2169dfacc5a2SVladimir Oltean struct sja1105_rule *rule; 2170070ca3bbSVladimir Oltean u16 tpid, tpid2; 21716666cebcSVladimir Oltean int rc; 21726666cebcSVladimir Oltean 2173dfacc5a2SVladimir Oltean list_for_each_entry(rule, &priv->flow_block.rules, list) { 2174dfacc5a2SVladimir Oltean if (rule->type == SJA1105_RULE_VL) { 217589153ed6SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 217689153ed6SVladimir Oltean "Cannot change VLAN filtering with active VL rules"); 2177dfacc5a2SVladimir Oltean return -EBUSY; 2178dfacc5a2SVladimir Oltean } 2179dfacc5a2SVladimir Oltean } 2180dfacc5a2SVladimir Oltean 2181070ca3bbSVladimir Oltean if (enabled) { 21826666cebcSVladimir Oltean /* Enable VLAN filtering. */ 218354fa49eeSVladimir Oltean tpid = ETH_P_8021Q; 218454fa49eeSVladimir Oltean tpid2 = ETH_P_8021AD; 2185070ca3bbSVladimir Oltean } else { 21866666cebcSVladimir Oltean /* Disable VLAN filtering. */ 2187070ca3bbSVladimir Oltean tpid = ETH_P_SJA1105; 2188070ca3bbSVladimir Oltean tpid2 = ETH_P_SJA1105; 2189070ca3bbSVladimir Oltean } 2190070ca3bbSVladimir Oltean 219138b5beeaSVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 219238b5beeaSVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 219338b5beeaSVladimir Oltean 219438b5beeaSVladimir Oltean if (enabled) 219538b5beeaSVladimir Oltean sp->xmit_tpid = priv->info->qinq_tpid; 219638b5beeaSVladimir Oltean else 219738b5beeaSVladimir Oltean sp->xmit_tpid = ETH_P_SJA1105; 219838b5beeaSVladimir Oltean } 219938b5beeaSVladimir Oltean 22000fac6aa0SVladimir Oltean if (priv->vlan_aware == enabled) 2201cfa36b1fSVladimir Oltean return 0; 2202cfa36b1fSVladimir Oltean 22030fac6aa0SVladimir Oltean priv->vlan_aware = enabled; 22047f14937fSVladimir Oltean 2205070ca3bbSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; 2206070ca3bbSVladimir Oltean general_params = table->entries; 2207f9a1a764SVladimir Oltean /* EtherType used to identify inner tagged (C-tag) VLAN traffic */ 220854fa49eeSVladimir Oltean general_params->tpid = tpid; 220954fa49eeSVladimir Oltean /* EtherType used to identify outer tagged (S-tag) VLAN traffic */ 2210070ca3bbSVladimir Oltean general_params->tpid2 = tpid2; 221142824463SVladimir Oltean /* When VLAN filtering is on, we need to at least be able to 221242824463SVladimir Oltean * decode management traffic through the "backup plan". 221342824463SVladimir Oltean */ 221442824463SVladimir Oltean general_params->incl_srcpt1 = enabled; 221542824463SVladimir Oltean general_params->incl_srcpt0 = enabled; 2216070ca3bbSVladimir Oltean 22176d7c7d94SVladimir Oltean /* VLAN filtering => independent VLAN learning. 22182cafa72eSVladimir Oltean * No VLAN filtering (or best effort) => shared VLAN learning. 22196d7c7d94SVladimir Oltean * 22206d7c7d94SVladimir Oltean * In shared VLAN learning mode, untagged traffic still gets 22216d7c7d94SVladimir Oltean * pvid-tagged, and the FDB table gets populated with entries 22226d7c7d94SVladimir Oltean * containing the "real" (pvid or from VLAN tag) VLAN ID. 22236d7c7d94SVladimir Oltean * However the switch performs a masked L2 lookup in the FDB, 22246d7c7d94SVladimir Oltean * effectively only looking up a frame's DMAC (and not VID) for the 22256d7c7d94SVladimir Oltean * forwarding decision. 22266d7c7d94SVladimir Oltean * 22276d7c7d94SVladimir Oltean * This is extremely convenient for us, because in modes with 22286d7c7d94SVladimir Oltean * vlan_filtering=0, dsa_8021q actually installs unique pvid's into 22296d7c7d94SVladimir Oltean * each front panel port. This is good for identification but breaks 22306d7c7d94SVladimir Oltean * learning badly - the VID of the learnt FDB entry is unique, aka 22316d7c7d94SVladimir Oltean * no frames coming from any other port are going to have it. So 22326d7c7d94SVladimir Oltean * for forwarding purposes, this is as though learning was broken 22336d7c7d94SVladimir Oltean * (all frames get flooded). 22346d7c7d94SVladimir Oltean */ 22356d7c7d94SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS]; 22366d7c7d94SVladimir Oltean l2_lookup_params = table->entries; 22370fac6aa0SVladimir Oltean l2_lookup_params->shared_learn = !priv->vlan_aware; 2238aaa270c6SVladimir Oltean 22396dfd23d3SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 22406dfd23d3SVladimir Oltean if (dsa_is_unused_port(ds, port)) 22416dfd23d3SVladimir Oltean continue; 22426dfd23d3SVladimir Oltean 22436dfd23d3SVladimir Oltean rc = sja1105_commit_pvid(ds, port); 2244aef31718SVladimir Oltean if (rc) 2245aef31718SVladimir Oltean return rc; 22466dfd23d3SVladimir Oltean } 2247aef31718SVladimir Oltean 22482eea1fa8SVladimir Oltean rc = sja1105_static_config_reload(priv, SJA1105_VLAN_FILTERING); 22496666cebcSVladimir Oltean if (rc) 225089153ed6SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, "Failed to change VLAN Ethertype"); 22516666cebcSVladimir Oltean 22520fac6aa0SVladimir Oltean return rc; 22536666cebcSVladimir Oltean } 22546666cebcSVladimir Oltean 22556dfd23d3SVladimir Oltean static int sja1105_vlan_add(struct sja1105_private *priv, int port, u16 vid, 22566dfd23d3SVladimir Oltean u16 flags) 22575899ee36SVladimir Oltean { 22586dfd23d3SVladimir Oltean struct sja1105_vlan_lookup_entry *vlan; 22596dfd23d3SVladimir Oltean struct sja1105_table *table; 22606dfd23d3SVladimir Oltean int match, rc; 22615899ee36SVladimir Oltean 22626dfd23d3SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP]; 22636dfd23d3SVladimir Oltean 22646dfd23d3SVladimir Oltean match = sja1105_is_vlan_configured(priv, vid); 22656dfd23d3SVladimir Oltean if (match < 0) { 22666dfd23d3SVladimir Oltean rc = sja1105_table_resize(table, table->entry_count + 1); 22676dfd23d3SVladimir Oltean if (rc) 22686dfd23d3SVladimir Oltean return rc; 22696dfd23d3SVladimir Oltean match = table->entry_count - 1; 22706dfd23d3SVladimir Oltean } 22716dfd23d3SVladimir Oltean 22726dfd23d3SVladimir Oltean /* Assign pointer after the resize (it's new memory) */ 22736dfd23d3SVladimir Oltean vlan = table->entries; 22746dfd23d3SVladimir Oltean 22756dfd23d3SVladimir Oltean vlan[match].type_entry = SJA1110_VLAN_D_TAG; 22766dfd23d3SVladimir Oltean vlan[match].vlanid = vid; 22776dfd23d3SVladimir Oltean vlan[match].vlan_bc |= BIT(port); 22786dfd23d3SVladimir Oltean vlan[match].vmemb_port |= BIT(port); 22796dfd23d3SVladimir Oltean if (flags & BRIDGE_VLAN_INFO_UNTAGGED) 22806dfd23d3SVladimir Oltean vlan[match].tag_port &= ~BIT(port); 22816dfd23d3SVladimir Oltean else 22826dfd23d3SVladimir Oltean vlan[match].tag_port |= BIT(port); 22836dfd23d3SVladimir Oltean 22846dfd23d3SVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_VLAN_LOOKUP, vid, 22856dfd23d3SVladimir Oltean &vlan[match], true); 22866dfd23d3SVladimir Oltean } 22876dfd23d3SVladimir Oltean 22886dfd23d3SVladimir Oltean static int sja1105_vlan_del(struct sja1105_private *priv, int port, u16 vid) 22896dfd23d3SVladimir Oltean { 22906dfd23d3SVladimir Oltean struct sja1105_vlan_lookup_entry *vlan; 22916dfd23d3SVladimir Oltean struct sja1105_table *table; 22926dfd23d3SVladimir Oltean bool keep = true; 22936dfd23d3SVladimir Oltean int match, rc; 22946dfd23d3SVladimir Oltean 22956dfd23d3SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP]; 22966dfd23d3SVladimir Oltean 22976dfd23d3SVladimir Oltean match = sja1105_is_vlan_configured(priv, vid); 22986dfd23d3SVladimir Oltean /* Can't delete a missing entry. */ 22996dfd23d3SVladimir Oltean if (match < 0) 23005899ee36SVladimir Oltean return 0; 23015899ee36SVladimir Oltean 23026dfd23d3SVladimir Oltean /* Assign pointer after the resize (it's new memory) */ 23036dfd23d3SVladimir Oltean vlan = table->entries; 23046dfd23d3SVladimir Oltean 23056dfd23d3SVladimir Oltean vlan[match].vlanid = vid; 23066dfd23d3SVladimir Oltean vlan[match].vlan_bc &= ~BIT(port); 23076dfd23d3SVladimir Oltean vlan[match].vmemb_port &= ~BIT(port); 23086dfd23d3SVladimir Oltean /* Also unset tag_port, just so we don't have a confusing bitmap 23096dfd23d3SVladimir Oltean * (no practical purpose). 2310b38e659dSVladimir Oltean */ 23116dfd23d3SVladimir Oltean vlan[match].tag_port &= ~BIT(port); 2312b38e659dSVladimir Oltean 23136dfd23d3SVladimir Oltean /* If there's no port left as member of this VLAN, 23146dfd23d3SVladimir Oltean * it's time for it to go. 23156dfd23d3SVladimir Oltean */ 23166dfd23d3SVladimir Oltean if (!vlan[match].vmemb_port) 23176dfd23d3SVladimir Oltean keep = false; 23185899ee36SVladimir Oltean 23196dfd23d3SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_VLAN_LOOKUP, vid, 23206dfd23d3SVladimir Oltean &vlan[match], keep); 23216dfd23d3SVladimir Oltean if (rc < 0) 23226dfd23d3SVladimir Oltean return rc; 23235899ee36SVladimir Oltean 23246dfd23d3SVladimir Oltean if (!keep) 23256dfd23d3SVladimir Oltean return sja1105_table_delete_entry(table, match); 23265899ee36SVladimir Oltean 23275899ee36SVladimir Oltean return 0; 23285899ee36SVladimir Oltean } 23295899ee36SVladimir Oltean 23306dfd23d3SVladimir Oltean static int sja1105_bridge_vlan_add(struct dsa_switch *ds, int port, 233131046a5fSVladimir Oltean const struct switchdev_obj_port_vlan *vlan, 233231046a5fSVladimir Oltean struct netlink_ext_ack *extack) 23336666cebcSVladimir Oltean { 23346666cebcSVladimir Oltean struct sja1105_private *priv = ds->priv; 2335884be12fSVladimir Oltean u16 flags = vlan->flags; 23366666cebcSVladimir Oltean int rc; 23376666cebcSVladimir Oltean 23380fac6aa0SVladimir Oltean /* Be sure to deny alterations to the configuration done by tag_8021q. 23391958d581SVladimir Oltean */ 23400fac6aa0SVladimir Oltean if (vid_is_dsa_8021q(vlan->vid)) { 234131046a5fSVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 234231046a5fSVladimir Oltean "Range 1024-3071 reserved for dsa_8021q operation"); 23431958d581SVladimir Oltean return -EBUSY; 23441958d581SVladimir Oltean } 23451958d581SVladimir Oltean 2346c5130029SVladimir Oltean /* Always install bridge VLANs as egress-tagged on CPU and DSA ports */ 2347c5130029SVladimir Oltean if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) 2348884be12fSVladimir Oltean flags = 0; 2349884be12fSVladimir Oltean 2350884be12fSVladimir Oltean rc = sja1105_vlan_add(priv, port, vlan->vid, flags); 23516dfd23d3SVladimir Oltean if (rc) 23521958d581SVladimir Oltean return rc; 2353ec5ae610SVladimir Oltean 23546dfd23d3SVladimir Oltean if (vlan->flags & BRIDGE_VLAN_INFO_PVID) 23556dfd23d3SVladimir Oltean priv->bridge_pvid[port] = vlan->vid; 2356ec5ae610SVladimir Oltean 23576dfd23d3SVladimir Oltean return sja1105_commit_pvid(ds, port); 23586666cebcSVladimir Oltean } 23596666cebcSVladimir Oltean 23606dfd23d3SVladimir Oltean static int sja1105_bridge_vlan_del(struct dsa_switch *ds, int port, 23616666cebcSVladimir Oltean const struct switchdev_obj_port_vlan *vlan) 23626666cebcSVladimir Oltean { 23636666cebcSVladimir Oltean struct sja1105_private *priv = ds->priv; 2364bef0746cSVladimir Oltean int rc; 23656666cebcSVladimir Oltean 2366bef0746cSVladimir Oltean rc = sja1105_vlan_del(priv, port, vlan->vid); 2367bef0746cSVladimir Oltean if (rc) 2368bef0746cSVladimir Oltean return rc; 2369bef0746cSVladimir Oltean 2370bef0746cSVladimir Oltean /* In case the pvid was deleted, make sure that untagged packets will 2371bef0746cSVladimir Oltean * be dropped. 2372bef0746cSVladimir Oltean */ 2373bef0746cSVladimir Oltean return sja1105_commit_pvid(ds, port); 23746666cebcSVladimir Oltean } 23756666cebcSVladimir Oltean 23765899ee36SVladimir Oltean static int sja1105_dsa_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid, 23775899ee36SVladimir Oltean u16 flags) 23785899ee36SVladimir Oltean { 23795899ee36SVladimir Oltean struct sja1105_private *priv = ds->priv; 23805899ee36SVladimir Oltean int rc; 23815899ee36SVladimir Oltean 23826dfd23d3SVladimir Oltean rc = sja1105_vlan_add(priv, port, vid, flags); 23836dfd23d3SVladimir Oltean if (rc) 23845899ee36SVladimir Oltean return rc; 23855899ee36SVladimir Oltean 23866dfd23d3SVladimir Oltean if (flags & BRIDGE_VLAN_INFO_PVID) 23876dfd23d3SVladimir Oltean priv->tag_8021q_pvid[port] = vid; 23886dfd23d3SVladimir Oltean 23896dfd23d3SVladimir Oltean return sja1105_commit_pvid(ds, port); 23905899ee36SVladimir Oltean } 23915899ee36SVladimir Oltean 23925899ee36SVladimir Oltean static int sja1105_dsa_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid) 23935899ee36SVladimir Oltean { 23945899ee36SVladimir Oltean struct sja1105_private *priv = ds->priv; 23955899ee36SVladimir Oltean 23966dfd23d3SVladimir Oltean return sja1105_vlan_del(priv, port, vid); 23975899ee36SVladimir Oltean } 23985899ee36SVladimir Oltean 23994fbc08bdSVladimir Oltean static int sja1105_prechangeupper(struct dsa_switch *ds, int port, 24004fbc08bdSVladimir Oltean struct netdev_notifier_changeupper_info *info) 24014fbc08bdSVladimir Oltean { 24024fbc08bdSVladimir Oltean struct netlink_ext_ack *extack = info->info.extack; 24034fbc08bdSVladimir Oltean struct net_device *upper = info->upper_dev; 240419fa937aSVladimir Oltean struct dsa_switch_tree *dst = ds->dst; 240519fa937aSVladimir Oltean struct dsa_port *dp; 24064fbc08bdSVladimir Oltean 24074fbc08bdSVladimir Oltean if (is_vlan_dev(upper)) { 24084fbc08bdSVladimir Oltean NL_SET_ERR_MSG_MOD(extack, "8021q uppers are not supported"); 24094fbc08bdSVladimir Oltean return -EBUSY; 24104fbc08bdSVladimir Oltean } 24114fbc08bdSVladimir Oltean 241219fa937aSVladimir Oltean if (netif_is_bridge_master(upper)) { 241319fa937aSVladimir Oltean list_for_each_entry(dp, &dst->ports, list) { 241419fa937aSVladimir Oltean if (dp->bridge_dev && dp->bridge_dev != upper && 241519fa937aSVladimir Oltean br_vlan_enabled(dp->bridge_dev)) { 241619fa937aSVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 241719fa937aSVladimir Oltean "Only one VLAN-aware bridge is supported"); 241819fa937aSVladimir Oltean return -EBUSY; 241919fa937aSVladimir Oltean } 242019fa937aSVladimir Oltean } 242119fa937aSVladimir Oltean } 242219fa937aSVladimir Oltean 24234fbc08bdSVladimir Oltean return 0; 24244fbc08bdSVladimir Oltean } 24254fbc08bdSVladimir Oltean 24268aa9ebccSVladimir Oltean /* The programming model for the SJA1105 switch is "all-at-once" via static 24278aa9ebccSVladimir Oltean * configuration tables. Some of these can be dynamically modified at runtime, 24288aa9ebccSVladimir Oltean * but not the xMII mode parameters table. 24298aa9ebccSVladimir Oltean * Furthermode, some PHYs may not have crystals for generating their clocks 24308aa9ebccSVladimir Oltean * (e.g. RMII). Instead, their 50MHz clock is supplied via the SJA1105 port's 24318aa9ebccSVladimir Oltean * ref_clk pin. So port clocking needs to be initialized early, before 24328aa9ebccSVladimir Oltean * connecting to PHYs is attempted, otherwise they won't respond through MDIO. 24338aa9ebccSVladimir Oltean * Setting correct PHY link speed does not matter now. 24348aa9ebccSVladimir Oltean * But dsa_slave_phy_setup is called later than sja1105_setup, so the PHY 24358aa9ebccSVladimir Oltean * bindings are not yet parsed by DSA core. We need to parse early so that we 24368aa9ebccSVladimir Oltean * can populate the xMII mode parameters table. 24378aa9ebccSVladimir Oltean */ 24388aa9ebccSVladimir Oltean static int sja1105_setup(struct dsa_switch *ds) 24398aa9ebccSVladimir Oltean { 24408aa9ebccSVladimir Oltean struct sja1105_private *priv = ds->priv; 24418aa9ebccSVladimir Oltean int rc; 24428aa9ebccSVladimir Oltean 24435d645df9SVladimir Oltean rc = sja1105_parse_dt(priv); 24448aa9ebccSVladimir Oltean if (rc < 0) { 24458aa9ebccSVladimir Oltean dev_err(ds->dev, "Failed to parse DT: %d\n", rc); 24468aa9ebccSVladimir Oltean return rc; 24478aa9ebccSVladimir Oltean } 2448f5b8631cSVladimir Oltean 2449f5b8631cSVladimir Oltean /* Error out early if internal delays are required through DT 2450f5b8631cSVladimir Oltean * and we can't apply them. 2451f5b8631cSVladimir Oltean */ 245229afb83aSVladimir Oltean rc = sja1105_parse_rgmii_delays(priv); 2453f5b8631cSVladimir Oltean if (rc < 0) { 2454f5b8631cSVladimir Oltean dev_err(ds->dev, "RGMII delay not supported\n"); 2455f5b8631cSVladimir Oltean return rc; 2456f5b8631cSVladimir Oltean } 2457f5b8631cSVladimir Oltean 245861c77126SVladimir Oltean rc = sja1105_ptp_clock_register(ds); 2459bb77f36aSVladimir Oltean if (rc < 0) { 2460bb77f36aSVladimir Oltean dev_err(ds->dev, "Failed to register PTP clock: %d\n", rc); 2461bb77f36aSVladimir Oltean return rc; 2462bb77f36aSVladimir Oltean } 24635a8f0974SVladimir Oltean 24645a8f0974SVladimir Oltean rc = sja1105_mdiobus_register(ds); 24655a8f0974SVladimir Oltean if (rc < 0) { 24665a8f0974SVladimir Oltean dev_err(ds->dev, "Failed to register MDIO bus: %pe\n", 24675a8f0974SVladimir Oltean ERR_PTR(rc)); 24685a8f0974SVladimir Oltean goto out_ptp_clock_unregister; 24695a8f0974SVladimir Oltean } 24705a8f0974SVladimir Oltean 2471cb5a82d2SVladimir Oltean if (priv->info->disable_microcontroller) { 2472cb5a82d2SVladimir Oltean rc = priv->info->disable_microcontroller(priv); 2473cb5a82d2SVladimir Oltean if (rc < 0) { 2474cb5a82d2SVladimir Oltean dev_err(ds->dev, 2475cb5a82d2SVladimir Oltean "Failed to disable microcontroller: %pe\n", 2476cb5a82d2SVladimir Oltean ERR_PTR(rc)); 2477cb5a82d2SVladimir Oltean goto out_mdiobus_unregister; 2478cb5a82d2SVladimir Oltean } 2479cb5a82d2SVladimir Oltean } 2480cb5a82d2SVladimir Oltean 24818aa9ebccSVladimir Oltean /* Create and send configuration down to device */ 24825d645df9SVladimir Oltean rc = sja1105_static_config_load(priv); 24838aa9ebccSVladimir Oltean if (rc < 0) { 24848aa9ebccSVladimir Oltean dev_err(ds->dev, "Failed to load static config: %d\n", rc); 24855a8f0974SVladimir Oltean goto out_mdiobus_unregister; 24868aa9ebccSVladimir Oltean } 2487cb5a82d2SVladimir Oltean 24888aa9ebccSVladimir Oltean /* Configure the CGU (PHY link modes and speeds) */ 2489cb5a82d2SVladimir Oltean if (priv->info->clocking_setup) { 2490c5037678SVladimir Oltean rc = priv->info->clocking_setup(priv); 24918aa9ebccSVladimir Oltean if (rc < 0) { 2492cb5a82d2SVladimir Oltean dev_err(ds->dev, 2493cb5a82d2SVladimir Oltean "Failed to configure MII clocking: %pe\n", 2494cb5a82d2SVladimir Oltean ERR_PTR(rc)); 2495cec279a8SVladimir Oltean goto out_static_config_free; 24968aa9ebccSVladimir Oltean } 2497cb5a82d2SVladimir Oltean } 2498cb5a82d2SVladimir Oltean 24996666cebcSVladimir Oltean /* On SJA1105, VLAN filtering per se is always enabled in hardware. 25006666cebcSVladimir Oltean * The only thing we can do to disable it is lie about what the 802.1Q 25016666cebcSVladimir Oltean * EtherType is. 25026666cebcSVladimir Oltean * So it will still try to apply VLAN filtering, but all ingress 25036666cebcSVladimir Oltean * traffic (except frames received with EtherType of ETH_P_SJA1105) 25046666cebcSVladimir Oltean * will be internally tagged with a distorted VLAN header where the 25056666cebcSVladimir Oltean * TPID is ETH_P_SJA1105, and the VLAN ID is the port pvid. 25066666cebcSVladimir Oltean */ 25076666cebcSVladimir Oltean ds->vlan_filtering_is_global = true; 2508884be12fSVladimir Oltean ds->untag_bridge_pvid = true; 2509b6ad86e6SVladimir Oltean /* tag_8021q has 3 bits for the VBID, and the value 0 is reserved */ 2510b6ad86e6SVladimir Oltean ds->num_fwd_offloading_bridges = 7; 25118aa9ebccSVladimir Oltean 25125f06c63bSVladimir Oltean /* Advertise the 8 egress queues */ 25135f06c63bSVladimir Oltean ds->num_tx_queues = SJA1105_NUM_TC; 25145f06c63bSVladimir Oltean 2515c279c726SVladimir Oltean ds->mtu_enforcement_ingress = true; 2516*81d45898SVladimir Oltean ds->assisted_learning_on_cpu_port = true; 2517c279c726SVladimir Oltean 25180a7bdbc2SVladimir Oltean rc = sja1105_devlink_setup(ds); 25192cafa72eSVladimir Oltean if (rc < 0) 2520cec279a8SVladimir Oltean goto out_static_config_free; 25212cafa72eSVladimir Oltean 2522bbed0bbdSVladimir Oltean rtnl_lock(); 2523328621f6SVladimir Oltean rc = dsa_tag_8021q_register(ds, htons(ETH_P_8021Q)); 2524bbed0bbdSVladimir Oltean rtnl_unlock(); 2525cec279a8SVladimir Oltean if (rc) 2526cec279a8SVladimir Oltean goto out_devlink_teardown; 2527cec279a8SVladimir Oltean 2528cec279a8SVladimir Oltean return 0; 2529cec279a8SVladimir Oltean 2530cec279a8SVladimir Oltean out_devlink_teardown: 2531cec279a8SVladimir Oltean sja1105_devlink_teardown(ds); 25325a8f0974SVladimir Oltean out_mdiobus_unregister: 25335a8f0974SVladimir Oltean sja1105_mdiobus_unregister(ds); 2534cec279a8SVladimir Oltean out_ptp_clock_unregister: 2535cec279a8SVladimir Oltean sja1105_ptp_clock_unregister(ds); 2536cec279a8SVladimir Oltean out_static_config_free: 2537cec279a8SVladimir Oltean sja1105_static_config_free(&priv->static_config); 2538bbed0bbdSVladimir Oltean 2539bbed0bbdSVladimir Oltean return rc; 2540227d07a0SVladimir Oltean } 2541227d07a0SVladimir Oltean 2542f3097be2SVladimir Oltean static void sja1105_teardown(struct dsa_switch *ds) 2543f3097be2SVladimir Oltean { 2544f3097be2SVladimir Oltean struct sja1105_private *priv = ds->priv; 2545a68578c2SVladimir Oltean int port; 2546a68578c2SVladimir Oltean 2547328621f6SVladimir Oltean rtnl_lock(); 2548328621f6SVladimir Oltean dsa_tag_8021q_unregister(ds); 2549328621f6SVladimir Oltean rtnl_unlock(); 2550328621f6SVladimir Oltean 2551542043e9SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 2552a68578c2SVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 2553a68578c2SVladimir Oltean 2554a68578c2SVladimir Oltean if (!dsa_is_user_port(ds, port)) 2555a68578c2SVladimir Oltean continue; 2556a68578c2SVladimir Oltean 255752c0d4e3SVladimir Oltean if (sp->xmit_worker) 2558a68578c2SVladimir Oltean kthread_destroy_worker(sp->xmit_worker); 2559a68578c2SVladimir Oltean } 2560f3097be2SVladimir Oltean 25610a7bdbc2SVladimir Oltean sja1105_devlink_teardown(ds); 2562a6af7763SVladimir Oltean sja1105_flower_teardown(ds); 2563317ab5b8SVladimir Oltean sja1105_tas_teardown(ds); 256461c77126SVladimir Oltean sja1105_ptp_clock_unregister(ds); 25656cb0abbdSVladimir Oltean sja1105_static_config_free(&priv->static_config); 2566f3097be2SVladimir Oltean } 2567f3097be2SVladimir Oltean 2568a68578c2SVladimir Oltean static void sja1105_port_disable(struct dsa_switch *ds, int port) 2569a68578c2SVladimir Oltean { 2570a68578c2SVladimir Oltean struct sja1105_private *priv = ds->priv; 2571a68578c2SVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 2572a68578c2SVladimir Oltean 2573a68578c2SVladimir Oltean if (!dsa_is_user_port(ds, port)) 2574a68578c2SVladimir Oltean return; 2575a68578c2SVladimir Oltean 2576a68578c2SVladimir Oltean kthread_cancel_work_sync(&sp->xmit_work); 2577a68578c2SVladimir Oltean skb_queue_purge(&sp->xmit_queue); 2578a68578c2SVladimir Oltean } 2579a68578c2SVladimir Oltean 2580227d07a0SVladimir Oltean static int sja1105_mgmt_xmit(struct dsa_switch *ds, int port, int slot, 258147ed985eSVladimir Oltean struct sk_buff *skb, bool takets) 2582227d07a0SVladimir Oltean { 2583227d07a0SVladimir Oltean struct sja1105_mgmt_entry mgmt_route = {0}; 2584227d07a0SVladimir Oltean struct sja1105_private *priv = ds->priv; 2585227d07a0SVladimir Oltean struct ethhdr *hdr; 2586227d07a0SVladimir Oltean int timeout = 10; 2587227d07a0SVladimir Oltean int rc; 2588227d07a0SVladimir Oltean 2589227d07a0SVladimir Oltean hdr = eth_hdr(skb); 2590227d07a0SVladimir Oltean 2591227d07a0SVladimir Oltean mgmt_route.macaddr = ether_addr_to_u64(hdr->h_dest); 2592227d07a0SVladimir Oltean mgmt_route.destports = BIT(port); 2593227d07a0SVladimir Oltean mgmt_route.enfport = 1; 259447ed985eSVladimir Oltean mgmt_route.tsreg = 0; 259547ed985eSVladimir Oltean mgmt_route.takets = takets; 2596227d07a0SVladimir Oltean 2597227d07a0SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE, 2598227d07a0SVladimir Oltean slot, &mgmt_route, true); 2599227d07a0SVladimir Oltean if (rc < 0) { 2600227d07a0SVladimir Oltean kfree_skb(skb); 2601227d07a0SVladimir Oltean return rc; 2602227d07a0SVladimir Oltean } 2603227d07a0SVladimir Oltean 2604227d07a0SVladimir Oltean /* Transfer skb to the host port. */ 260568bb8ea8SVivien Didelot dsa_enqueue_skb(skb, dsa_to_port(ds, port)->slave); 2606227d07a0SVladimir Oltean 2607227d07a0SVladimir Oltean /* Wait until the switch has processed the frame */ 2608227d07a0SVladimir Oltean do { 2609227d07a0SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_MGMT_ROUTE, 2610227d07a0SVladimir Oltean slot, &mgmt_route); 2611227d07a0SVladimir Oltean if (rc < 0) { 2612227d07a0SVladimir Oltean dev_err_ratelimited(priv->ds->dev, 2613227d07a0SVladimir Oltean "failed to poll for mgmt route\n"); 2614227d07a0SVladimir Oltean continue; 2615227d07a0SVladimir Oltean } 2616227d07a0SVladimir Oltean 2617227d07a0SVladimir Oltean /* UM10944: The ENFPORT flag of the respective entry is 2618227d07a0SVladimir Oltean * cleared when a match is found. The host can use this 2619227d07a0SVladimir Oltean * flag as an acknowledgment. 2620227d07a0SVladimir Oltean */ 2621227d07a0SVladimir Oltean cpu_relax(); 2622227d07a0SVladimir Oltean } while (mgmt_route.enfport && --timeout); 2623227d07a0SVladimir Oltean 2624227d07a0SVladimir Oltean if (!timeout) { 2625227d07a0SVladimir Oltean /* Clean up the management route so that a follow-up 2626227d07a0SVladimir Oltean * frame may not match on it by mistake. 26272a7e7409SVladimir Oltean * This is only hardware supported on P/Q/R/S - on E/T it is 26282a7e7409SVladimir Oltean * a no-op and we are silently discarding the -EOPNOTSUPP. 2629227d07a0SVladimir Oltean */ 2630227d07a0SVladimir Oltean sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE, 2631227d07a0SVladimir Oltean slot, &mgmt_route, false); 2632227d07a0SVladimir Oltean dev_err_ratelimited(priv->ds->dev, "xmit timed out\n"); 2633227d07a0SVladimir Oltean } 2634227d07a0SVladimir Oltean 2635227d07a0SVladimir Oltean return NETDEV_TX_OK; 2636227d07a0SVladimir Oltean } 2637227d07a0SVladimir Oltean 2638a68578c2SVladimir Oltean #define work_to_port(work) \ 2639a68578c2SVladimir Oltean container_of((work), struct sja1105_port, xmit_work) 2640a68578c2SVladimir Oltean #define tagger_to_sja1105(t) \ 2641a68578c2SVladimir Oltean container_of((t), struct sja1105_private, tagger_data) 2642a68578c2SVladimir Oltean 2643227d07a0SVladimir Oltean /* Deferred work is unfortunately necessary because setting up the management 2644227d07a0SVladimir Oltean * route cannot be done from atomit context (SPI transfer takes a sleepable 2645227d07a0SVladimir Oltean * lock on the bus) 2646227d07a0SVladimir Oltean */ 2647a68578c2SVladimir Oltean static void sja1105_port_deferred_xmit(struct kthread_work *work) 2648227d07a0SVladimir Oltean { 2649a68578c2SVladimir Oltean struct sja1105_port *sp = work_to_port(work); 2650a68578c2SVladimir Oltean struct sja1105_tagger_data *tagger_data = sp->data; 2651a68578c2SVladimir Oltean struct sja1105_private *priv = tagger_to_sja1105(tagger_data); 2652a68578c2SVladimir Oltean int port = sp - priv->ports; 2653a68578c2SVladimir Oltean struct sk_buff *skb; 2654a68578c2SVladimir Oltean 2655a68578c2SVladimir Oltean while ((skb = skb_dequeue(&sp->xmit_queue)) != NULL) { 2656c4b364ceSYangbo Lu struct sk_buff *clone = SJA1105_SKB_CB(skb)->clone; 2657227d07a0SVladimir Oltean 2658227d07a0SVladimir Oltean mutex_lock(&priv->mgmt_lock); 2659227d07a0SVladimir Oltean 2660a68578c2SVladimir Oltean sja1105_mgmt_xmit(priv->ds, port, 0, skb, !!clone); 2661a68578c2SVladimir Oltean 266247ed985eSVladimir Oltean /* The clone, if there, was made by dsa_skb_tx_timestamp */ 2663a68578c2SVladimir Oltean if (clone) 2664a68578c2SVladimir Oltean sja1105_ptp_txtstamp_skb(priv->ds, port, clone); 2665227d07a0SVladimir Oltean 2666227d07a0SVladimir Oltean mutex_unlock(&priv->mgmt_lock); 2667a68578c2SVladimir Oltean } 26688aa9ebccSVladimir Oltean } 26698aa9ebccSVladimir Oltean 26708456721dSVladimir Oltean /* The MAXAGE setting belongs to the L2 Forwarding Parameters table, 26718456721dSVladimir Oltean * which cannot be reconfigured at runtime. So a switch reset is required. 26728456721dSVladimir Oltean */ 26738456721dSVladimir Oltean static int sja1105_set_ageing_time(struct dsa_switch *ds, 26748456721dSVladimir Oltean unsigned int ageing_time) 26758456721dSVladimir Oltean { 26768456721dSVladimir Oltean struct sja1105_l2_lookup_params_entry *l2_lookup_params; 26778456721dSVladimir Oltean struct sja1105_private *priv = ds->priv; 26788456721dSVladimir Oltean struct sja1105_table *table; 26798456721dSVladimir Oltean unsigned int maxage; 26808456721dSVladimir Oltean 26818456721dSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS]; 26828456721dSVladimir Oltean l2_lookup_params = table->entries; 26838456721dSVladimir Oltean 26848456721dSVladimir Oltean maxage = SJA1105_AGEING_TIME_MS(ageing_time); 26858456721dSVladimir Oltean 26868456721dSVladimir Oltean if (l2_lookup_params->maxage == maxage) 26878456721dSVladimir Oltean return 0; 26888456721dSVladimir Oltean 26898456721dSVladimir Oltean l2_lookup_params->maxage = maxage; 26908456721dSVladimir Oltean 26912eea1fa8SVladimir Oltean return sja1105_static_config_reload(priv, SJA1105_AGEING_TIME); 26928456721dSVladimir Oltean } 26938456721dSVladimir Oltean 2694c279c726SVladimir Oltean static int sja1105_change_mtu(struct dsa_switch *ds, int port, int new_mtu) 2695c279c726SVladimir Oltean { 2696c279c726SVladimir Oltean struct sja1105_l2_policing_entry *policing; 2697c279c726SVladimir Oltean struct sja1105_private *priv = ds->priv; 2698c279c726SVladimir Oltean 2699c279c726SVladimir Oltean new_mtu += VLAN_ETH_HLEN + ETH_FCS_LEN; 2700c279c726SVladimir Oltean 2701777e55e3SVladimir Oltean if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) 2702c279c726SVladimir Oltean new_mtu += VLAN_HLEN; 2703c279c726SVladimir Oltean 2704c279c726SVladimir Oltean policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries; 2705c279c726SVladimir Oltean 2706a7cc081cSVladimir Oltean if (policing[port].maxlen == new_mtu) 2707c279c726SVladimir Oltean return 0; 2708c279c726SVladimir Oltean 2709a7cc081cSVladimir Oltean policing[port].maxlen = new_mtu; 2710c279c726SVladimir Oltean 2711c279c726SVladimir Oltean return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING); 2712c279c726SVladimir Oltean } 2713c279c726SVladimir Oltean 2714c279c726SVladimir Oltean static int sja1105_get_max_mtu(struct dsa_switch *ds, int port) 2715c279c726SVladimir Oltean { 2716c279c726SVladimir Oltean return 2043 - VLAN_ETH_HLEN - ETH_FCS_LEN; 2717c279c726SVladimir Oltean } 2718c279c726SVladimir Oltean 2719317ab5b8SVladimir Oltean static int sja1105_port_setup_tc(struct dsa_switch *ds, int port, 2720317ab5b8SVladimir Oltean enum tc_setup_type type, 2721317ab5b8SVladimir Oltean void *type_data) 2722317ab5b8SVladimir Oltean { 2723317ab5b8SVladimir Oltean switch (type) { 2724317ab5b8SVladimir Oltean case TC_SETUP_QDISC_TAPRIO: 2725317ab5b8SVladimir Oltean return sja1105_setup_tc_taprio(ds, port, type_data); 27264d752508SVladimir Oltean case TC_SETUP_QDISC_CBS: 27274d752508SVladimir Oltean return sja1105_setup_tc_cbs(ds, port, type_data); 2728317ab5b8SVladimir Oltean default: 2729317ab5b8SVladimir Oltean return -EOPNOTSUPP; 2730317ab5b8SVladimir Oltean } 2731317ab5b8SVladimir Oltean } 2732317ab5b8SVladimir Oltean 2733511e6ca0SVladimir Oltean /* We have a single mirror (@to) port, but can configure ingress and egress 2734511e6ca0SVladimir Oltean * mirroring on all other (@from) ports. 2735511e6ca0SVladimir Oltean * We need to allow mirroring rules only as long as the @to port is always the 2736511e6ca0SVladimir Oltean * same, and we need to unset the @to port from mirr_port only when there is no 2737511e6ca0SVladimir Oltean * mirroring rule that references it. 2738511e6ca0SVladimir Oltean */ 2739511e6ca0SVladimir Oltean static int sja1105_mirror_apply(struct sja1105_private *priv, int from, int to, 2740511e6ca0SVladimir Oltean bool ingress, bool enabled) 2741511e6ca0SVladimir Oltean { 2742511e6ca0SVladimir Oltean struct sja1105_general_params_entry *general_params; 2743511e6ca0SVladimir Oltean struct sja1105_mac_config_entry *mac; 2744542043e9SVladimir Oltean struct dsa_switch *ds = priv->ds; 2745511e6ca0SVladimir Oltean struct sja1105_table *table; 2746511e6ca0SVladimir Oltean bool already_enabled; 2747511e6ca0SVladimir Oltean u64 new_mirr_port; 2748511e6ca0SVladimir Oltean int rc; 2749511e6ca0SVladimir Oltean 2750511e6ca0SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; 2751511e6ca0SVladimir Oltean general_params = table->entries; 2752511e6ca0SVladimir Oltean 2753511e6ca0SVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 2754511e6ca0SVladimir Oltean 2755542043e9SVladimir Oltean already_enabled = (general_params->mirr_port != ds->num_ports); 2756511e6ca0SVladimir Oltean if (already_enabled && enabled && general_params->mirr_port != to) { 2757511e6ca0SVladimir Oltean dev_err(priv->ds->dev, 2758511e6ca0SVladimir Oltean "Delete mirroring rules towards port %llu first\n", 2759511e6ca0SVladimir Oltean general_params->mirr_port); 2760511e6ca0SVladimir Oltean return -EBUSY; 2761511e6ca0SVladimir Oltean } 2762511e6ca0SVladimir Oltean 2763511e6ca0SVladimir Oltean new_mirr_port = to; 2764511e6ca0SVladimir Oltean if (!enabled) { 2765511e6ca0SVladimir Oltean bool keep = false; 2766511e6ca0SVladimir Oltean int port; 2767511e6ca0SVladimir Oltean 2768511e6ca0SVladimir Oltean /* Anybody still referencing mirr_port? */ 2769542043e9SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 2770511e6ca0SVladimir Oltean if (mac[port].ing_mirr || mac[port].egr_mirr) { 2771511e6ca0SVladimir Oltean keep = true; 2772511e6ca0SVladimir Oltean break; 2773511e6ca0SVladimir Oltean } 2774511e6ca0SVladimir Oltean } 2775511e6ca0SVladimir Oltean /* Unset already_enabled for next time */ 2776511e6ca0SVladimir Oltean if (!keep) 2777542043e9SVladimir Oltean new_mirr_port = ds->num_ports; 2778511e6ca0SVladimir Oltean } 2779511e6ca0SVladimir Oltean if (new_mirr_port != general_params->mirr_port) { 2780511e6ca0SVladimir Oltean general_params->mirr_port = new_mirr_port; 2781511e6ca0SVladimir Oltean 2782511e6ca0SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_GENERAL_PARAMS, 2783511e6ca0SVladimir Oltean 0, general_params, true); 2784511e6ca0SVladimir Oltean if (rc < 0) 2785511e6ca0SVladimir Oltean return rc; 2786511e6ca0SVladimir Oltean } 2787511e6ca0SVladimir Oltean 2788511e6ca0SVladimir Oltean if (ingress) 2789511e6ca0SVladimir Oltean mac[from].ing_mirr = enabled; 2790511e6ca0SVladimir Oltean else 2791511e6ca0SVladimir Oltean mac[from].egr_mirr = enabled; 2792511e6ca0SVladimir Oltean 2793511e6ca0SVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, from, 2794511e6ca0SVladimir Oltean &mac[from], true); 2795511e6ca0SVladimir Oltean } 2796511e6ca0SVladimir Oltean 2797511e6ca0SVladimir Oltean static int sja1105_mirror_add(struct dsa_switch *ds, int port, 2798511e6ca0SVladimir Oltean struct dsa_mall_mirror_tc_entry *mirror, 2799511e6ca0SVladimir Oltean bool ingress) 2800511e6ca0SVladimir Oltean { 2801511e6ca0SVladimir Oltean return sja1105_mirror_apply(ds->priv, port, mirror->to_local_port, 2802511e6ca0SVladimir Oltean ingress, true); 2803511e6ca0SVladimir Oltean } 2804511e6ca0SVladimir Oltean 2805511e6ca0SVladimir Oltean static void sja1105_mirror_del(struct dsa_switch *ds, int port, 2806511e6ca0SVladimir Oltean struct dsa_mall_mirror_tc_entry *mirror) 2807511e6ca0SVladimir Oltean { 2808511e6ca0SVladimir Oltean sja1105_mirror_apply(ds->priv, port, mirror->to_local_port, 2809511e6ca0SVladimir Oltean mirror->ingress, false); 2810511e6ca0SVladimir Oltean } 2811511e6ca0SVladimir Oltean 2812a7cc081cSVladimir Oltean static int sja1105_port_policer_add(struct dsa_switch *ds, int port, 2813a7cc081cSVladimir Oltean struct dsa_mall_policer_tc_entry *policer) 2814a7cc081cSVladimir Oltean { 2815a7cc081cSVladimir Oltean struct sja1105_l2_policing_entry *policing; 2816a7cc081cSVladimir Oltean struct sja1105_private *priv = ds->priv; 2817a7cc081cSVladimir Oltean 2818a7cc081cSVladimir Oltean policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries; 2819a7cc081cSVladimir Oltean 2820a7cc081cSVladimir Oltean /* In hardware, every 8 microseconds the credit level is incremented by 2821a7cc081cSVladimir Oltean * the value of RATE bytes divided by 64, up to a maximum of SMAX 2822a7cc081cSVladimir Oltean * bytes. 2823a7cc081cSVladimir Oltean */ 2824a7cc081cSVladimir Oltean policing[port].rate = div_u64(512 * policer->rate_bytes_per_sec, 2825a7cc081cSVladimir Oltean 1000000); 28265f035af7SPo Liu policing[port].smax = policer->burst; 2827a7cc081cSVladimir Oltean 2828a7cc081cSVladimir Oltean return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING); 2829a7cc081cSVladimir Oltean } 2830a7cc081cSVladimir Oltean 2831a7cc081cSVladimir Oltean static void sja1105_port_policer_del(struct dsa_switch *ds, int port) 2832a7cc081cSVladimir Oltean { 2833a7cc081cSVladimir Oltean struct sja1105_l2_policing_entry *policing; 2834a7cc081cSVladimir Oltean struct sja1105_private *priv = ds->priv; 2835a7cc081cSVladimir Oltean 2836a7cc081cSVladimir Oltean policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries; 2837a7cc081cSVladimir Oltean 2838a7cc081cSVladimir Oltean policing[port].rate = SJA1105_RATE_MBPS(1000); 2839a7cc081cSVladimir Oltean policing[port].smax = 65535; 2840a7cc081cSVladimir Oltean 2841a7cc081cSVladimir Oltean sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING); 2842a7cc081cSVladimir Oltean } 2843a7cc081cSVladimir Oltean 28444d942354SVladimir Oltean static int sja1105_port_set_learning(struct sja1105_private *priv, int port, 28454d942354SVladimir Oltean bool enabled) 28464d942354SVladimir Oltean { 28474d942354SVladimir Oltean struct sja1105_mac_config_entry *mac; 28484d942354SVladimir Oltean int rc; 28494d942354SVladimir Oltean 28504d942354SVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 28514d942354SVladimir Oltean 28524c44fc5eSVladimir Oltean mac[port].dyn_learn = enabled; 28534d942354SVladimir Oltean 28544d942354SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, 28554d942354SVladimir Oltean &mac[port], true); 28564d942354SVladimir Oltean if (rc) 28574d942354SVladimir Oltean return rc; 28584d942354SVladimir Oltean 28594d942354SVladimir Oltean if (enabled) 28604d942354SVladimir Oltean priv->learn_ena |= BIT(port); 28614d942354SVladimir Oltean else 28624d942354SVladimir Oltean priv->learn_ena &= ~BIT(port); 28634d942354SVladimir Oltean 28644d942354SVladimir Oltean return 0; 28654d942354SVladimir Oltean } 28664d942354SVladimir Oltean 28674d942354SVladimir Oltean static int sja1105_port_ucast_bcast_flood(struct sja1105_private *priv, int to, 28684d942354SVladimir Oltean struct switchdev_brport_flags flags) 28694d942354SVladimir Oltean { 28704d942354SVladimir Oltean if (flags.mask & BR_FLOOD) { 28714d942354SVladimir Oltean if (flags.val & BR_FLOOD) 28727f7ccdeaSVladimir Oltean priv->ucast_egress_floods |= BIT(to); 28734d942354SVladimir Oltean else 28746a5166e0SVladimir Oltean priv->ucast_egress_floods &= ~BIT(to); 28754d942354SVladimir Oltean } 28767f7ccdeaSVladimir Oltean 28774d942354SVladimir Oltean if (flags.mask & BR_BCAST_FLOOD) { 28784d942354SVladimir Oltean if (flags.val & BR_BCAST_FLOOD) 28797f7ccdeaSVladimir Oltean priv->bcast_egress_floods |= BIT(to); 28804d942354SVladimir Oltean else 28816a5166e0SVladimir Oltean priv->bcast_egress_floods &= ~BIT(to); 28824d942354SVladimir Oltean } 28834d942354SVladimir Oltean 28847f7ccdeaSVladimir Oltean return sja1105_manage_flood_domains(priv); 28854d942354SVladimir Oltean } 28864d942354SVladimir Oltean 28874d942354SVladimir Oltean static int sja1105_port_mcast_flood(struct sja1105_private *priv, int to, 28884d942354SVladimir Oltean struct switchdev_brport_flags flags, 28894d942354SVladimir Oltean struct netlink_ext_ack *extack) 28904d942354SVladimir Oltean { 28914d942354SVladimir Oltean struct sja1105_l2_lookup_entry *l2_lookup; 28924d942354SVladimir Oltean struct sja1105_table *table; 28934d942354SVladimir Oltean int match; 28944d942354SVladimir Oltean 28954d942354SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; 28964d942354SVladimir Oltean l2_lookup = table->entries; 28974d942354SVladimir Oltean 28984d942354SVladimir Oltean for (match = 0; match < table->entry_count; match++) 28994d942354SVladimir Oltean if (l2_lookup[match].macaddr == SJA1105_UNKNOWN_MULTICAST && 29004d942354SVladimir Oltean l2_lookup[match].mask_macaddr == SJA1105_UNKNOWN_MULTICAST) 29014d942354SVladimir Oltean break; 29024d942354SVladimir Oltean 29034d942354SVladimir Oltean if (match == table->entry_count) { 29044d942354SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 29054d942354SVladimir Oltean "Could not find FDB entry for unknown multicast"); 29064d942354SVladimir Oltean return -ENOSPC; 29074d942354SVladimir Oltean } 29084d942354SVladimir Oltean 29094d942354SVladimir Oltean if (flags.val & BR_MCAST_FLOOD) 29104d942354SVladimir Oltean l2_lookup[match].destports |= BIT(to); 29114d942354SVladimir Oltean else 29124d942354SVladimir Oltean l2_lookup[match].destports &= ~BIT(to); 29134d942354SVladimir Oltean 29144d942354SVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 29154d942354SVladimir Oltean l2_lookup[match].index, 29164d942354SVladimir Oltean &l2_lookup[match], 29174d942354SVladimir Oltean true); 29184d942354SVladimir Oltean } 29194d942354SVladimir Oltean 29204d942354SVladimir Oltean static int sja1105_port_pre_bridge_flags(struct dsa_switch *ds, int port, 29214d942354SVladimir Oltean struct switchdev_brport_flags flags, 29224d942354SVladimir Oltean struct netlink_ext_ack *extack) 29234d942354SVladimir Oltean { 29244d942354SVladimir Oltean struct sja1105_private *priv = ds->priv; 29254d942354SVladimir Oltean 29264d942354SVladimir Oltean if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | 29274d942354SVladimir Oltean BR_BCAST_FLOOD)) 29284d942354SVladimir Oltean return -EINVAL; 29294d942354SVladimir Oltean 29304d942354SVladimir Oltean if (flags.mask & (BR_FLOOD | BR_MCAST_FLOOD) && 29314d942354SVladimir Oltean !priv->info->can_limit_mcast_flood) { 29324d942354SVladimir Oltean bool multicast = !!(flags.val & BR_MCAST_FLOOD); 29334d942354SVladimir Oltean bool unicast = !!(flags.val & BR_FLOOD); 29344d942354SVladimir Oltean 29354d942354SVladimir Oltean if (unicast != multicast) { 29364d942354SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 29374d942354SVladimir Oltean "This chip cannot configure multicast flooding independently of unicast"); 29384d942354SVladimir Oltean return -EINVAL; 29394d942354SVladimir Oltean } 29404d942354SVladimir Oltean } 29414d942354SVladimir Oltean 29424d942354SVladimir Oltean return 0; 29434d942354SVladimir Oltean } 29444d942354SVladimir Oltean 29454d942354SVladimir Oltean static int sja1105_port_bridge_flags(struct dsa_switch *ds, int port, 29464d942354SVladimir Oltean struct switchdev_brport_flags flags, 29474d942354SVladimir Oltean struct netlink_ext_ack *extack) 29484d942354SVladimir Oltean { 29494d942354SVladimir Oltean struct sja1105_private *priv = ds->priv; 29504d942354SVladimir Oltean int rc; 29514d942354SVladimir Oltean 29524d942354SVladimir Oltean if (flags.mask & BR_LEARNING) { 29534d942354SVladimir Oltean bool learn_ena = !!(flags.val & BR_LEARNING); 29544d942354SVladimir Oltean 29554d942354SVladimir Oltean rc = sja1105_port_set_learning(priv, port, learn_ena); 29564d942354SVladimir Oltean if (rc) 29574d942354SVladimir Oltean return rc; 29584d942354SVladimir Oltean } 29594d942354SVladimir Oltean 29604d942354SVladimir Oltean if (flags.mask & (BR_FLOOD | BR_BCAST_FLOOD)) { 29614d942354SVladimir Oltean rc = sja1105_port_ucast_bcast_flood(priv, port, flags); 29624d942354SVladimir Oltean if (rc) 29634d942354SVladimir Oltean return rc; 29644d942354SVladimir Oltean } 29654d942354SVladimir Oltean 29664d942354SVladimir Oltean /* For chips that can't offload BR_MCAST_FLOOD independently, there 29674d942354SVladimir Oltean * is nothing to do here, we ensured the configuration is in sync by 29684d942354SVladimir Oltean * offloading BR_FLOOD. 29694d942354SVladimir Oltean */ 29704d942354SVladimir Oltean if (flags.mask & BR_MCAST_FLOOD && priv->info->can_limit_mcast_flood) { 29714d942354SVladimir Oltean rc = sja1105_port_mcast_flood(priv, port, flags, 29724d942354SVladimir Oltean extack); 29734d942354SVladimir Oltean if (rc) 29744d942354SVladimir Oltean return rc; 29754d942354SVladimir Oltean } 29764d942354SVladimir Oltean 29774d942354SVladimir Oltean return 0; 29784d942354SVladimir Oltean } 29794d942354SVladimir Oltean 29808aa9ebccSVladimir Oltean static const struct dsa_switch_ops sja1105_switch_ops = { 29818aa9ebccSVladimir Oltean .get_tag_protocol = sja1105_get_tag_protocol, 29828aa9ebccSVladimir Oltean .setup = sja1105_setup, 2983f3097be2SVladimir Oltean .teardown = sja1105_teardown, 29848456721dSVladimir Oltean .set_ageing_time = sja1105_set_ageing_time, 2985c279c726SVladimir Oltean .port_change_mtu = sja1105_change_mtu, 2986c279c726SVladimir Oltean .port_max_mtu = sja1105_get_max_mtu, 2987ad9f299aSVladimir Oltean .phylink_validate = sja1105_phylink_validate, 2988af7cd036SVladimir Oltean .phylink_mac_config = sja1105_mac_config, 29898400cff6SVladimir Oltean .phylink_mac_link_up = sja1105_mac_link_up, 29908400cff6SVladimir Oltean .phylink_mac_link_down = sja1105_mac_link_down, 299152c34e6eSVladimir Oltean .get_strings = sja1105_get_strings, 299252c34e6eSVladimir Oltean .get_ethtool_stats = sja1105_get_ethtool_stats, 299352c34e6eSVladimir Oltean .get_sset_count = sja1105_get_sset_count, 2994bb77f36aSVladimir Oltean .get_ts_info = sja1105_get_ts_info, 2995a68578c2SVladimir Oltean .port_disable = sja1105_port_disable, 2996291d1e72SVladimir Oltean .port_fdb_dump = sja1105_fdb_dump, 2997291d1e72SVladimir Oltean .port_fdb_add = sja1105_fdb_add, 2998291d1e72SVladimir Oltean .port_fdb_del = sja1105_fdb_del, 29998aa9ebccSVladimir Oltean .port_bridge_join = sja1105_bridge_join, 30008aa9ebccSVladimir Oltean .port_bridge_leave = sja1105_bridge_leave, 30014d942354SVladimir Oltean .port_pre_bridge_flags = sja1105_port_pre_bridge_flags, 30024d942354SVladimir Oltean .port_bridge_flags = sja1105_port_bridge_flags, 3003640f763fSVladimir Oltean .port_stp_state_set = sja1105_bridge_stp_state_set, 30046666cebcSVladimir Oltean .port_vlan_filtering = sja1105_vlan_filtering, 30056dfd23d3SVladimir Oltean .port_vlan_add = sja1105_bridge_vlan_add, 30066dfd23d3SVladimir Oltean .port_vlan_del = sja1105_bridge_vlan_del, 3007291d1e72SVladimir Oltean .port_mdb_add = sja1105_mdb_add, 3008291d1e72SVladimir Oltean .port_mdb_del = sja1105_mdb_del, 3009a602afd2SVladimir Oltean .port_hwtstamp_get = sja1105_hwtstamp_get, 3010a602afd2SVladimir Oltean .port_hwtstamp_set = sja1105_hwtstamp_set, 3011f3097be2SVladimir Oltean .port_rxtstamp = sja1105_port_rxtstamp, 301247ed985eSVladimir Oltean .port_txtstamp = sja1105_port_txtstamp, 3013317ab5b8SVladimir Oltean .port_setup_tc = sja1105_port_setup_tc, 3014511e6ca0SVladimir Oltean .port_mirror_add = sja1105_mirror_add, 3015511e6ca0SVladimir Oltean .port_mirror_del = sja1105_mirror_del, 3016a7cc081cSVladimir Oltean .port_policer_add = sja1105_port_policer_add, 3017a7cc081cSVladimir Oltean .port_policer_del = sja1105_port_policer_del, 3018a6af7763SVladimir Oltean .cls_flower_add = sja1105_cls_flower_add, 3019a6af7763SVladimir Oltean .cls_flower_del = sja1105_cls_flower_del, 3020834f8933SVladimir Oltean .cls_flower_stats = sja1105_cls_flower_stats, 3021ff4cf8eaSVladimir Oltean .devlink_info_get = sja1105_devlink_info_get, 30225da11eb4SVladimir Oltean .tag_8021q_vlan_add = sja1105_dsa_8021q_vlan_add, 30235da11eb4SVladimir Oltean .tag_8021q_vlan_del = sja1105_dsa_8021q_vlan_del, 30244fbc08bdSVladimir Oltean .port_prechangeupper = sja1105_prechangeupper, 3025b6ad86e6SVladimir Oltean .port_bridge_tx_fwd_offload = dsa_tag_8021q_bridge_tx_fwd_offload, 3026b6ad86e6SVladimir Oltean .port_bridge_tx_fwd_unoffload = dsa_tag_8021q_bridge_tx_fwd_unoffload, 30278aa9ebccSVladimir Oltean }; 30288aa9ebccSVladimir Oltean 30290b0e2997SVladimir Oltean static const struct of_device_id sja1105_dt_ids[]; 30300b0e2997SVladimir Oltean 30318aa9ebccSVladimir Oltean static int sja1105_check_device_id(struct sja1105_private *priv) 30328aa9ebccSVladimir Oltean { 30338aa9ebccSVladimir Oltean const struct sja1105_regs *regs = priv->info->regs; 30348aa9ebccSVladimir Oltean u8 prod_id[SJA1105_SIZE_DEVICE_ID] = {0}; 30358aa9ebccSVladimir Oltean struct device *dev = &priv->spidev->dev; 30360b0e2997SVladimir Oltean const struct of_device_id *match; 3037dff79620SVladimir Oltean u32 device_id; 30388aa9ebccSVladimir Oltean u64 part_no; 30398aa9ebccSVladimir Oltean int rc; 30408aa9ebccSVladimir Oltean 304134d76e9fSVladimir Oltean rc = sja1105_xfer_u32(priv, SPI_READ, regs->device_id, &device_id, 304234d76e9fSVladimir Oltean NULL); 30438aa9ebccSVladimir Oltean if (rc < 0) 30448aa9ebccSVladimir Oltean return rc; 30458aa9ebccSVladimir Oltean 30461bd44870SVladimir Oltean rc = sja1105_xfer_buf(priv, SPI_READ, regs->prod_id, prod_id, 30471bd44870SVladimir Oltean SJA1105_SIZE_DEVICE_ID); 30488aa9ebccSVladimir Oltean if (rc < 0) 30498aa9ebccSVladimir Oltean return rc; 30508aa9ebccSVladimir Oltean 30518aa9ebccSVladimir Oltean sja1105_unpack(prod_id, &part_no, 19, 4, SJA1105_SIZE_DEVICE_ID); 30528aa9ebccSVladimir Oltean 30535978fac0SNathan Chancellor for (match = sja1105_dt_ids; match->compatible[0]; match++) { 30540b0e2997SVladimir Oltean const struct sja1105_info *info = match->data; 30550b0e2997SVladimir Oltean 30560b0e2997SVladimir Oltean /* Is what's been probed in our match table at all? */ 30570b0e2997SVladimir Oltean if (info->device_id != device_id || info->part_no != part_no) 30580b0e2997SVladimir Oltean continue; 30590b0e2997SVladimir Oltean 30600b0e2997SVladimir Oltean /* But is it what's in the device tree? */ 30610b0e2997SVladimir Oltean if (priv->info->device_id != device_id || 30620b0e2997SVladimir Oltean priv->info->part_no != part_no) { 30630b0e2997SVladimir Oltean dev_warn(dev, "Device tree specifies chip %s but found %s, please fix it!\n", 30640b0e2997SVladimir Oltean priv->info->name, info->name); 30650b0e2997SVladimir Oltean /* It isn't. No problem, pick that up. */ 30660b0e2997SVladimir Oltean priv->info = info; 30678aa9ebccSVladimir Oltean } 30688aa9ebccSVladimir Oltean 30698aa9ebccSVladimir Oltean return 0; 30708aa9ebccSVladimir Oltean } 30718aa9ebccSVladimir Oltean 30720b0e2997SVladimir Oltean dev_err(dev, "Unexpected {device ID, part number}: 0x%x 0x%llx\n", 30730b0e2997SVladimir Oltean device_id, part_no); 30740b0e2997SVladimir Oltean 30750b0e2997SVladimir Oltean return -ENODEV; 30760b0e2997SVladimir Oltean } 30770b0e2997SVladimir Oltean 30788aa9ebccSVladimir Oltean static int sja1105_probe(struct spi_device *spi) 30798aa9ebccSVladimir Oltean { 3080844d7edcSVladimir Oltean struct sja1105_tagger_data *tagger_data; 30818aa9ebccSVladimir Oltean struct device *dev = &spi->dev; 30828aa9ebccSVladimir Oltean struct sja1105_private *priv; 3083718bad0eSVladimir Oltean size_t max_xfer, max_msg; 30848aa9ebccSVladimir Oltean struct dsa_switch *ds; 3085a68578c2SVladimir Oltean int rc, port; 30868aa9ebccSVladimir Oltean 30878aa9ebccSVladimir Oltean if (!dev->of_node) { 30888aa9ebccSVladimir Oltean dev_err(dev, "No DTS bindings for SJA1105 driver\n"); 30898aa9ebccSVladimir Oltean return -EINVAL; 30908aa9ebccSVladimir Oltean } 30918aa9ebccSVladimir Oltean 30928aa9ebccSVladimir Oltean priv = devm_kzalloc(dev, sizeof(struct sja1105_private), GFP_KERNEL); 30938aa9ebccSVladimir Oltean if (!priv) 30948aa9ebccSVladimir Oltean return -ENOMEM; 30958aa9ebccSVladimir Oltean 30968aa9ebccSVladimir Oltean /* Configure the optional reset pin and bring up switch */ 30978aa9ebccSVladimir Oltean priv->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); 30988aa9ebccSVladimir Oltean if (IS_ERR(priv->reset_gpio)) 30998aa9ebccSVladimir Oltean dev_dbg(dev, "reset-gpios not defined, ignoring\n"); 31008aa9ebccSVladimir Oltean else 31018aa9ebccSVladimir Oltean sja1105_hw_reset(priv->reset_gpio, 1, 1); 31028aa9ebccSVladimir Oltean 31038aa9ebccSVladimir Oltean /* Populate our driver private structure (priv) based on 31048aa9ebccSVladimir Oltean * the device tree node that was probed (spi) 31058aa9ebccSVladimir Oltean */ 31068aa9ebccSVladimir Oltean priv->spidev = spi; 31078aa9ebccSVladimir Oltean spi_set_drvdata(spi, priv); 31088aa9ebccSVladimir Oltean 31098aa9ebccSVladimir Oltean /* Configure the SPI bus */ 31108aa9ebccSVladimir Oltean spi->bits_per_word = 8; 31118aa9ebccSVladimir Oltean rc = spi_setup(spi); 31128aa9ebccSVladimir Oltean if (rc < 0) { 31138aa9ebccSVladimir Oltean dev_err(dev, "Could not init SPI\n"); 31148aa9ebccSVladimir Oltean return rc; 31158aa9ebccSVladimir Oltean } 31168aa9ebccSVladimir Oltean 3117718bad0eSVladimir Oltean /* In sja1105_xfer, we send spi_messages composed of two spi_transfers: 3118718bad0eSVladimir Oltean * a small one for the message header and another one for the current 3119718bad0eSVladimir Oltean * chunk of the packed buffer. 3120718bad0eSVladimir Oltean * Check that the restrictions imposed by the SPI controller are 3121718bad0eSVladimir Oltean * respected: the chunk buffer is smaller than the max transfer size, 3122718bad0eSVladimir Oltean * and the total length of the chunk plus its message header is smaller 3123718bad0eSVladimir Oltean * than the max message size. 3124718bad0eSVladimir Oltean * We do that during probe time since the maximum transfer size is a 3125718bad0eSVladimir Oltean * runtime invariant. 3126718bad0eSVladimir Oltean */ 3127718bad0eSVladimir Oltean max_xfer = spi_max_transfer_size(spi); 3128718bad0eSVladimir Oltean max_msg = spi_max_message_size(spi); 3129718bad0eSVladimir Oltean 3130718bad0eSVladimir Oltean /* We need to send at least one 64-bit word of SPI payload per message 3131718bad0eSVladimir Oltean * in order to be able to make useful progress. 3132718bad0eSVladimir Oltean */ 3133718bad0eSVladimir Oltean if (max_msg < SJA1105_SIZE_SPI_MSG_HEADER + 8) { 3134718bad0eSVladimir Oltean dev_err(dev, "SPI master cannot send large enough buffers, aborting\n"); 3135718bad0eSVladimir Oltean return -EINVAL; 3136718bad0eSVladimir Oltean } 3137718bad0eSVladimir Oltean 3138718bad0eSVladimir Oltean priv->max_xfer_len = SJA1105_SIZE_SPI_MSG_MAXLEN; 3139718bad0eSVladimir Oltean if (priv->max_xfer_len > max_xfer) 3140718bad0eSVladimir Oltean priv->max_xfer_len = max_xfer; 3141718bad0eSVladimir Oltean if (priv->max_xfer_len > max_msg - SJA1105_SIZE_SPI_MSG_HEADER) 3142718bad0eSVladimir Oltean priv->max_xfer_len = max_msg - SJA1105_SIZE_SPI_MSG_HEADER; 3143718bad0eSVladimir Oltean 31448aa9ebccSVladimir Oltean priv->info = of_device_get_match_data(dev); 31458aa9ebccSVladimir Oltean 31468aa9ebccSVladimir Oltean /* Detect hardware device */ 31478aa9ebccSVladimir Oltean rc = sja1105_check_device_id(priv); 31488aa9ebccSVladimir Oltean if (rc < 0) { 31498aa9ebccSVladimir Oltean dev_err(dev, "Device ID check failed: %d\n", rc); 31508aa9ebccSVladimir Oltean return rc; 31518aa9ebccSVladimir Oltean } 31528aa9ebccSVladimir Oltean 31538aa9ebccSVladimir Oltean dev_info(dev, "Probed switch chip: %s\n", priv->info->name); 31548aa9ebccSVladimir Oltean 31557e99e347SVivien Didelot ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL); 31568aa9ebccSVladimir Oltean if (!ds) 31578aa9ebccSVladimir Oltean return -ENOMEM; 31588aa9ebccSVladimir Oltean 31597e99e347SVivien Didelot ds->dev = dev; 31603e77e59bSVladimir Oltean ds->num_ports = priv->info->num_ports; 31618aa9ebccSVladimir Oltean ds->ops = &sja1105_switch_ops; 31628aa9ebccSVladimir Oltean ds->priv = priv; 31638aa9ebccSVladimir Oltean priv->ds = ds; 31648aa9ebccSVladimir Oltean 3165844d7edcSVladimir Oltean tagger_data = &priv->tagger_data; 3166844d7edcSVladimir Oltean 3167d5a619bfSVivien Didelot mutex_init(&priv->ptp_data.lock); 3168d5a619bfSVivien Didelot mutex_init(&priv->mgmt_lock); 3169d5a619bfSVivien Didelot 3170d5a619bfSVivien Didelot sja1105_tas_setup(ds); 3171a6af7763SVladimir Oltean sja1105_flower_setup(ds); 3172d5a619bfSVivien Didelot 3173d5a619bfSVivien Didelot rc = dsa_register_switch(priv->ds); 3174d5a619bfSVivien Didelot if (rc) 3175328621f6SVladimir Oltean return rc; 3176d5a619bfSVivien Didelot 31774d752508SVladimir Oltean if (IS_ENABLED(CONFIG_NET_SCH_CBS)) { 31784d752508SVladimir Oltean priv->cbs = devm_kcalloc(dev, priv->info->num_cbs_shapers, 31794d752508SVladimir Oltean sizeof(struct sja1105_cbs_entry), 31804d752508SVladimir Oltean GFP_KERNEL); 3181dc596e3fSVladimir Oltean if (!priv->cbs) { 3182dc596e3fSVladimir Oltean rc = -ENOMEM; 3183dc596e3fSVladimir Oltean goto out_unregister_switch; 3184dc596e3fSVladimir Oltean } 31854d752508SVladimir Oltean } 31864d752508SVladimir Oltean 3187227d07a0SVladimir Oltean /* Connections between dsa_port and sja1105_port */ 3188542043e9SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 3189a68578c2SVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 3190a68578c2SVladimir Oltean struct dsa_port *dp = dsa_to_port(ds, port); 3191a68578c2SVladimir Oltean struct net_device *slave; 3192227d07a0SVladimir Oltean 3193a68578c2SVladimir Oltean if (!dsa_is_user_port(ds, port)) 3194a68578c2SVladimir Oltean continue; 3195a68578c2SVladimir Oltean 3196a68578c2SVladimir Oltean dp->priv = sp; 3197a68578c2SVladimir Oltean sp->dp = dp; 3198844d7edcSVladimir Oltean sp->data = tagger_data; 3199a68578c2SVladimir Oltean slave = dp->slave; 3200a68578c2SVladimir Oltean kthread_init_work(&sp->xmit_work, sja1105_port_deferred_xmit); 3201a68578c2SVladimir Oltean sp->xmit_worker = kthread_create_worker(0, "%s_xmit", 3202a68578c2SVladimir Oltean slave->name); 3203a68578c2SVladimir Oltean if (IS_ERR(sp->xmit_worker)) { 3204a68578c2SVladimir Oltean rc = PTR_ERR(sp->xmit_worker); 3205a68578c2SVladimir Oltean dev_err(ds->dev, 3206a68578c2SVladimir Oltean "failed to create deferred xmit thread: %d\n", 3207a68578c2SVladimir Oltean rc); 3208dc596e3fSVladimir Oltean goto out_destroy_workers; 3209a68578c2SVladimir Oltean } 3210a68578c2SVladimir Oltean skb_queue_head_init(&sp->xmit_queue); 321138b5beeaSVladimir Oltean sp->xmit_tpid = ETH_P_SJA1105; 3212227d07a0SVladimir Oltean } 3213227d07a0SVladimir Oltean 3214d5a619bfSVivien Didelot return 0; 3215dc596e3fSVladimir Oltean 3216dc596e3fSVladimir Oltean out_destroy_workers: 3217a68578c2SVladimir Oltean while (port-- > 0) { 3218a68578c2SVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 3219a68578c2SVladimir Oltean 3220a68578c2SVladimir Oltean if (!dsa_is_user_port(ds, port)) 3221a68578c2SVladimir Oltean continue; 3222a68578c2SVladimir Oltean 3223a68578c2SVladimir Oltean kthread_destroy_worker(sp->xmit_worker); 3224a68578c2SVladimir Oltean } 3225dc596e3fSVladimir Oltean 3226dc596e3fSVladimir Oltean out_unregister_switch: 3227dc596e3fSVladimir Oltean dsa_unregister_switch(ds); 3228dc596e3fSVladimir Oltean 3229a68578c2SVladimir Oltean return rc; 32308aa9ebccSVladimir Oltean } 32318aa9ebccSVladimir Oltean 32328aa9ebccSVladimir Oltean static int sja1105_remove(struct spi_device *spi) 32338aa9ebccSVladimir Oltean { 32348aa9ebccSVladimir Oltean struct sja1105_private *priv = spi_get_drvdata(spi); 3235cedf4670SVladimir Oltean struct dsa_switch *ds = priv->ds; 32368aa9ebccSVladimir Oltean 3237cedf4670SVladimir Oltean dsa_unregister_switch(ds); 3238cedf4670SVladimir Oltean 32398aa9ebccSVladimir Oltean return 0; 32408aa9ebccSVladimir Oltean } 32418aa9ebccSVladimir Oltean 32428aa9ebccSVladimir Oltean static const struct of_device_id sja1105_dt_ids[] = { 32438aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105e", .data = &sja1105e_info }, 32448aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105t", .data = &sja1105t_info }, 32458aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105p", .data = &sja1105p_info }, 32468aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105q", .data = &sja1105q_info }, 32478aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105r", .data = &sja1105r_info }, 32488aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105s", .data = &sja1105s_info }, 32493e77e59bSVladimir Oltean { .compatible = "nxp,sja1110a", .data = &sja1110a_info }, 32503e77e59bSVladimir Oltean { .compatible = "nxp,sja1110b", .data = &sja1110b_info }, 32513e77e59bSVladimir Oltean { .compatible = "nxp,sja1110c", .data = &sja1110c_info }, 32523e77e59bSVladimir Oltean { .compatible = "nxp,sja1110d", .data = &sja1110d_info }, 32538aa9ebccSVladimir Oltean { /* sentinel */ }, 32548aa9ebccSVladimir Oltean }; 32558aa9ebccSVladimir Oltean MODULE_DEVICE_TABLE(of, sja1105_dt_ids); 32568aa9ebccSVladimir Oltean 32578aa9ebccSVladimir Oltean static struct spi_driver sja1105_driver = { 32588aa9ebccSVladimir Oltean .driver = { 32598aa9ebccSVladimir Oltean .name = "sja1105", 32608aa9ebccSVladimir Oltean .owner = THIS_MODULE, 32618aa9ebccSVladimir Oltean .of_match_table = of_match_ptr(sja1105_dt_ids), 32628aa9ebccSVladimir Oltean }, 32638aa9ebccSVladimir Oltean .probe = sja1105_probe, 32648aa9ebccSVladimir Oltean .remove = sja1105_remove, 32658aa9ebccSVladimir Oltean }; 32668aa9ebccSVladimir Oltean 32678aa9ebccSVladimir Oltean module_spi_driver(sja1105_driver); 32688aa9ebccSVladimir Oltean 32698aa9ebccSVladimir Oltean MODULE_AUTHOR("Vladimir Oltean <olteanv@gmail.com>"); 32708aa9ebccSVladimir Oltean MODULE_AUTHOR("Georg Waibel <georg.waibel@sensor-technik.de>"); 32718aa9ebccSVladimir Oltean MODULE_DESCRIPTION("SJA1105 Driver"); 32728aa9ebccSVladimir Oltean MODULE_LICENSE("GPL v2"); 3273