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 202b0b33b04SVladimir Oltean * enabled for the CPU port. 203640f763fSVladimir Oltean */ 204b0b33b04SVladimir Oltean if (dsa_is_cpu_port(ds, i)) 205b0b33b04SVladimir Oltean priv->learn_ena |= BIT(i); 206640f763fSVladimir Oltean } 2078aa9ebccSVladimir Oltean 2088aa9ebccSVladimir Oltean return 0; 2098aa9ebccSVladimir Oltean } 2108aa9ebccSVladimir Oltean 2115d645df9SVladimir Oltean static int sja1105_init_mii_settings(struct sja1105_private *priv) 2128aa9ebccSVladimir Oltean { 2138aa9ebccSVladimir Oltean struct device *dev = &priv->spidev->dev; 2148aa9ebccSVladimir Oltean struct sja1105_xmii_params_entry *mii; 215542043e9SVladimir Oltean struct dsa_switch *ds = priv->ds; 2168aa9ebccSVladimir Oltean struct sja1105_table *table; 2178aa9ebccSVladimir Oltean int i; 2188aa9ebccSVladimir Oltean 2198aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_XMII_PARAMS]; 2208aa9ebccSVladimir Oltean 2218aa9ebccSVladimir Oltean /* Discard previous xMII Mode Parameters Table */ 2228aa9ebccSVladimir Oltean if (table->entry_count) { 2238aa9ebccSVladimir Oltean kfree(table->entries); 2248aa9ebccSVladimir Oltean table->entry_count = 0; 2258aa9ebccSVladimir Oltean } 2268aa9ebccSVladimir Oltean 227fd6f2c25SVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 2288aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 2298aa9ebccSVladimir Oltean if (!table->entries) 2308aa9ebccSVladimir Oltean return -ENOMEM; 2318aa9ebccSVladimir Oltean 2321fd4a173SVladimir Oltean /* Override table based on PHYLINK DT bindings */ 233fd6f2c25SVladimir Oltean table->entry_count = table->ops->max_entry_count; 2348aa9ebccSVladimir Oltean 2358aa9ebccSVladimir Oltean mii = table->entries; 2368aa9ebccSVladimir Oltean 237542043e9SVladimir Oltean for (i = 0; i < ds->num_ports; i++) { 2385d645df9SVladimir Oltean sja1105_mii_role_t role = XMII_MAC; 2395d645df9SVladimir Oltean 240ee9d0cb6SVladimir Oltean if (dsa_is_unused_port(priv->ds, i)) 241ee9d0cb6SVladimir Oltean continue; 242ee9d0cb6SVladimir Oltean 2435d645df9SVladimir Oltean switch (priv->phy_mode[i]) { 2445a8f0974SVladimir Oltean case PHY_INTERFACE_MODE_INTERNAL: 2455a8f0974SVladimir Oltean if (priv->info->internal_phy[i] == SJA1105_NO_PHY) 2465a8f0974SVladimir Oltean goto unsupported; 2475a8f0974SVladimir Oltean 2485a8f0974SVladimir Oltean mii->xmii_mode[i] = XMII_MODE_MII; 2495a8f0974SVladimir Oltean if (priv->info->internal_phy[i] == SJA1105_PHY_BASE_TX) 2505a8f0974SVladimir Oltean mii->special[i] = true; 2515a8f0974SVladimir Oltean 2525a8f0974SVladimir Oltean break; 2535d645df9SVladimir Oltean case PHY_INTERFACE_MODE_REVMII: 2545d645df9SVladimir Oltean role = XMII_PHY; 2555d645df9SVladimir Oltean fallthrough; 2568aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_MII: 25791a05078SVladimir Oltean if (!priv->info->supports_mii[i]) 25891a05078SVladimir Oltean goto unsupported; 25991a05078SVladimir Oltean 2608aa9ebccSVladimir Oltean mii->xmii_mode[i] = XMII_MODE_MII; 2618aa9ebccSVladimir Oltean break; 2625d645df9SVladimir Oltean case PHY_INTERFACE_MODE_REVRMII: 2635d645df9SVladimir Oltean role = XMII_PHY; 2645d645df9SVladimir Oltean fallthrough; 2658aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RMII: 26691a05078SVladimir Oltean if (!priv->info->supports_rmii[i]) 26791a05078SVladimir Oltean goto unsupported; 26891a05078SVladimir Oltean 2698aa9ebccSVladimir Oltean mii->xmii_mode[i] = XMII_MODE_RMII; 2708aa9ebccSVladimir Oltean break; 2718aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RGMII: 2728aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RGMII_ID: 2738aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RGMII_RXID: 2748aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RGMII_TXID: 27591a05078SVladimir Oltean if (!priv->info->supports_rgmii[i]) 27691a05078SVladimir Oltean goto unsupported; 27791a05078SVladimir Oltean 2788aa9ebccSVladimir Oltean mii->xmii_mode[i] = XMII_MODE_RGMII; 2798aa9ebccSVladimir Oltean break; 280ffe10e67SVladimir Oltean case PHY_INTERFACE_MODE_SGMII: 28191a05078SVladimir Oltean if (!priv->info->supports_sgmii[i]) 28291a05078SVladimir Oltean goto unsupported; 28391a05078SVladimir Oltean 284ffe10e67SVladimir Oltean mii->xmii_mode[i] = XMII_MODE_SGMII; 285ece578bcSVladimir Oltean mii->special[i] = true; 286ffe10e67SVladimir Oltean break; 28791a05078SVladimir Oltean case PHY_INTERFACE_MODE_2500BASEX: 28891a05078SVladimir Oltean if (!priv->info->supports_2500basex[i]) 28991a05078SVladimir Oltean goto unsupported; 29091a05078SVladimir Oltean 29191a05078SVladimir Oltean mii->xmii_mode[i] = XMII_MODE_SGMII; 292ece578bcSVladimir Oltean mii->special[i] = true; 29391a05078SVladimir Oltean break; 29491a05078SVladimir Oltean unsupported: 2958aa9ebccSVladimir Oltean default: 29691a05078SVladimir Oltean dev_err(dev, "Unsupported PHY mode %s on port %d!\n", 2975d645df9SVladimir Oltean phy_modes(priv->phy_mode[i]), i); 2986729188dSVladimir Oltean return -EINVAL; 2998aa9ebccSVladimir Oltean } 3008aa9ebccSVladimir Oltean 3015d645df9SVladimir Oltean mii->phy_mac[i] = role; 3028aa9ebccSVladimir Oltean } 3038aa9ebccSVladimir Oltean return 0; 3048aa9ebccSVladimir Oltean } 3058aa9ebccSVladimir Oltean 3068aa9ebccSVladimir Oltean static int sja1105_init_static_fdb(struct sja1105_private *priv) 3078aa9ebccSVladimir Oltean { 3084d942354SVladimir Oltean struct sja1105_l2_lookup_entry *l2_lookup; 3098aa9ebccSVladimir Oltean struct sja1105_table *table; 3104d942354SVladimir Oltean int port; 3118aa9ebccSVladimir Oltean 3128aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; 3138aa9ebccSVladimir Oltean 3144d942354SVladimir Oltean /* We only populate the FDB table through dynamic L2 Address Lookup 3154d942354SVladimir Oltean * entries, except for a special entry at the end which is a catch-all 3164d942354SVladimir Oltean * for unknown multicast and will be used to control flooding domain. 317291d1e72SVladimir Oltean */ 3188aa9ebccSVladimir Oltean if (table->entry_count) { 3198aa9ebccSVladimir Oltean kfree(table->entries); 3208aa9ebccSVladimir Oltean table->entry_count = 0; 3218aa9ebccSVladimir Oltean } 3224d942354SVladimir Oltean 3234d942354SVladimir Oltean if (!priv->info->can_limit_mcast_flood) 3244d942354SVladimir Oltean return 0; 3254d942354SVladimir Oltean 3264d942354SVladimir Oltean table->entries = kcalloc(1, table->ops->unpacked_entry_size, 3274d942354SVladimir Oltean GFP_KERNEL); 3284d942354SVladimir Oltean if (!table->entries) 3294d942354SVladimir Oltean return -ENOMEM; 3304d942354SVladimir Oltean 3314d942354SVladimir Oltean table->entry_count = 1; 3324d942354SVladimir Oltean l2_lookup = table->entries; 3334d942354SVladimir Oltean 3344d942354SVladimir Oltean /* All L2 multicast addresses have an odd first octet */ 3354d942354SVladimir Oltean l2_lookup[0].macaddr = SJA1105_UNKNOWN_MULTICAST; 3364d942354SVladimir Oltean l2_lookup[0].mask_macaddr = SJA1105_UNKNOWN_MULTICAST; 3374d942354SVladimir Oltean l2_lookup[0].lockeds = true; 3384d942354SVladimir Oltean l2_lookup[0].index = SJA1105_MAX_L2_LOOKUP_COUNT - 1; 3394d942354SVladimir Oltean 3404d942354SVladimir Oltean /* Flood multicast to every port by default */ 3414d942354SVladimir Oltean for (port = 0; port < priv->ds->num_ports; port++) 3424d942354SVladimir Oltean if (!dsa_is_unused_port(priv->ds, port)) 3434d942354SVladimir Oltean l2_lookup[0].destports |= BIT(port); 3444d942354SVladimir Oltean 3458aa9ebccSVladimir Oltean return 0; 3468aa9ebccSVladimir Oltean } 3478aa9ebccSVladimir Oltean 3488aa9ebccSVladimir Oltean static int sja1105_init_l2_lookup_params(struct sja1105_private *priv) 3498aa9ebccSVladimir Oltean { 3508aa9ebccSVladimir Oltean struct sja1105_l2_lookup_params_entry default_l2_lookup_params = { 3518456721dSVladimir Oltean /* Learned FDB entries are forgotten after 300 seconds */ 3528456721dSVladimir Oltean .maxage = SJA1105_AGEING_TIME_MS(300000), 3538aa9ebccSVladimir Oltean /* All entries within a FDB bin are available for learning */ 3548aa9ebccSVladimir Oltean .dyn_tbsz = SJA1105ET_FDB_BIN_SIZE, 3551da73821SVladimir Oltean /* And the P/Q/R/S equivalent setting: */ 3561da73821SVladimir Oltean .start_dynspc = 0, 3578aa9ebccSVladimir Oltean /* 2^8 + 2^5 + 2^3 + 2^2 + 2^1 + 1 in Koopman notation */ 3588aa9ebccSVladimir Oltean .poly = 0x97, 3598aa9ebccSVladimir Oltean /* This selects between Independent VLAN Learning (IVL) and 3608aa9ebccSVladimir Oltean * Shared VLAN Learning (SVL) 3618aa9ebccSVladimir Oltean */ 3626d7c7d94SVladimir Oltean .shared_learn = true, 3638aa9ebccSVladimir Oltean /* Don't discard management traffic based on ENFPORT - 3648aa9ebccSVladimir Oltean * we don't perform SMAC port enforcement anyway, so 3658aa9ebccSVladimir Oltean * what we are setting here doesn't matter. 3668aa9ebccSVladimir Oltean */ 3678aa9ebccSVladimir Oltean .no_enf_hostprt = false, 3688aa9ebccSVladimir Oltean /* Don't learn SMAC for mac_fltres1 and mac_fltres0. 3698aa9ebccSVladimir Oltean * Maybe correlate with no_linklocal_learn from bridge driver? 3708aa9ebccSVladimir Oltean */ 3718aa9ebccSVladimir Oltean .no_mgmt_learn = true, 3721da73821SVladimir Oltean /* P/Q/R/S only */ 3731da73821SVladimir Oltean .use_static = true, 3741da73821SVladimir Oltean /* Dynamically learned FDB entries can overwrite other (older) 3751da73821SVladimir Oltean * dynamic FDB entries 3761da73821SVladimir Oltean */ 3771da73821SVladimir Oltean .owr_dyn = true, 3781da73821SVladimir Oltean .drpnolearn = true, 3798aa9ebccSVladimir Oltean }; 380542043e9SVladimir Oltean struct dsa_switch *ds = priv->ds; 381f238fef1SVladimir Oltean int port, num_used_ports = 0; 382542043e9SVladimir Oltean struct sja1105_table *table; 383542043e9SVladimir Oltean u64 max_fdb_entries; 384542043e9SVladimir Oltean 385542043e9SVladimir Oltean for (port = 0; port < ds->num_ports; port++) 386f238fef1SVladimir Oltean if (!dsa_is_unused_port(ds, port)) 387f238fef1SVladimir Oltean num_used_ports++; 388f238fef1SVladimir Oltean 389f238fef1SVladimir Oltean max_fdb_entries = SJA1105_MAX_L2_LOOKUP_COUNT / num_used_ports; 390f238fef1SVladimir Oltean 391f238fef1SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 392f238fef1SVladimir Oltean if (dsa_is_unused_port(ds, port)) 393f238fef1SVladimir Oltean continue; 394f238fef1SVladimir Oltean 395542043e9SVladimir Oltean default_l2_lookup_params.maxaddrp[port] = max_fdb_entries; 396f238fef1SVladimir Oltean } 3978aa9ebccSVladimir Oltean 3988aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS]; 3998aa9ebccSVladimir Oltean 4008aa9ebccSVladimir Oltean if (table->entry_count) { 4018aa9ebccSVladimir Oltean kfree(table->entries); 4028aa9ebccSVladimir Oltean table->entry_count = 0; 4038aa9ebccSVladimir Oltean } 4048aa9ebccSVladimir Oltean 405fd6f2c25SVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 4068aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 4078aa9ebccSVladimir Oltean if (!table->entries) 4088aa9ebccSVladimir Oltean return -ENOMEM; 4098aa9ebccSVladimir Oltean 410fd6f2c25SVladimir Oltean table->entry_count = table->ops->max_entry_count; 4118aa9ebccSVladimir Oltean 4128aa9ebccSVladimir Oltean /* This table only has a single entry */ 4138aa9ebccSVladimir Oltean ((struct sja1105_l2_lookup_params_entry *)table->entries)[0] = 4148aa9ebccSVladimir Oltean default_l2_lookup_params; 4158aa9ebccSVladimir Oltean 4168aa9ebccSVladimir Oltean return 0; 4178aa9ebccSVladimir Oltean } 4188aa9ebccSVladimir Oltean 419ed040abcSVladimir Oltean /* Set up a default VLAN for untagged traffic injected from the CPU 420ed040abcSVladimir Oltean * using management routes (e.g. STP, PTP) as opposed to tag_8021q. 421ed040abcSVladimir Oltean * All DT-defined ports are members of this VLAN, and there are no 422ed040abcSVladimir Oltean * restrictions on forwarding (since the CPU selects the destination). 423ed040abcSVladimir Oltean * Frames from this VLAN will always be transmitted as untagged, and 424ed040abcSVladimir Oltean * neither the bridge nor the 8021q module cannot create this VLAN ID. 425ed040abcSVladimir Oltean */ 4268aa9ebccSVladimir Oltean static int sja1105_init_static_vlan(struct sja1105_private *priv) 4278aa9ebccSVladimir Oltean { 4288aa9ebccSVladimir Oltean struct sja1105_table *table; 4298aa9ebccSVladimir Oltean struct sja1105_vlan_lookup_entry pvid = { 4303e77e59bSVladimir Oltean .type_entry = SJA1110_VLAN_D_TAG, 4318aa9ebccSVladimir Oltean .ving_mirr = 0, 4328aa9ebccSVladimir Oltean .vegr_mirr = 0, 4338aa9ebccSVladimir Oltean .vmemb_port = 0, 4348aa9ebccSVladimir Oltean .vlan_bc = 0, 4358aa9ebccSVladimir Oltean .tag_port = 0, 436ed040abcSVladimir Oltean .vlanid = SJA1105_DEFAULT_VLAN, 4378aa9ebccSVladimir Oltean }; 438ec5ae610SVladimir Oltean struct dsa_switch *ds = priv->ds; 439ec5ae610SVladimir Oltean int port; 4408aa9ebccSVladimir Oltean 4418aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP]; 4428aa9ebccSVladimir Oltean 4438aa9ebccSVladimir Oltean if (table->entry_count) { 4448aa9ebccSVladimir Oltean kfree(table->entries); 4458aa9ebccSVladimir Oltean table->entry_count = 0; 4468aa9ebccSVladimir Oltean } 4478aa9ebccSVladimir Oltean 448c75857b0SZheng Yongjun table->entries = kzalloc(table->ops->unpacked_entry_size, 4498aa9ebccSVladimir Oltean GFP_KERNEL); 4508aa9ebccSVladimir Oltean if (!table->entries) 4518aa9ebccSVladimir Oltean return -ENOMEM; 4528aa9ebccSVladimir Oltean 4538aa9ebccSVladimir Oltean table->entry_count = 1; 4548aa9ebccSVladimir Oltean 455ec5ae610SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 456ec5ae610SVladimir Oltean if (dsa_is_unused_port(ds, port)) 457ec5ae610SVladimir Oltean continue; 458ec5ae610SVladimir Oltean 459ec5ae610SVladimir Oltean pvid.vmemb_port |= BIT(port); 460ec5ae610SVladimir Oltean pvid.vlan_bc |= BIT(port); 461ec5ae610SVladimir Oltean pvid.tag_port &= ~BIT(port); 462ec5ae610SVladimir Oltean 463c5130029SVladimir Oltean if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) { 4646dfd23d3SVladimir Oltean priv->tag_8021q_pvid[port] = SJA1105_DEFAULT_VLAN; 4656dfd23d3SVladimir Oltean priv->bridge_pvid[port] = SJA1105_DEFAULT_VLAN; 4666dfd23d3SVladimir Oltean } 4678aa9ebccSVladimir Oltean } 4688aa9ebccSVladimir Oltean 4698aa9ebccSVladimir Oltean ((struct sja1105_vlan_lookup_entry *)table->entries)[0] = pvid; 4708aa9ebccSVladimir Oltean return 0; 4718aa9ebccSVladimir Oltean } 4728aa9ebccSVladimir Oltean 4738aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding(struct sja1105_private *priv) 4748aa9ebccSVladimir Oltean { 4758aa9ebccSVladimir Oltean struct sja1105_l2_forwarding_entry *l2fwd; 476542043e9SVladimir Oltean struct dsa_switch *ds = priv->ds; 477*0f9b762cSVladimir Oltean struct dsa_switch_tree *dst; 4788aa9ebccSVladimir Oltean struct sja1105_table *table; 479*0f9b762cSVladimir Oltean struct dsa_link *dl; 4803fa21270SVladimir Oltean int port, tc; 4813fa21270SVladimir Oltean int from, to; 4828aa9ebccSVladimir Oltean 4838aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING]; 4848aa9ebccSVladimir Oltean 4858aa9ebccSVladimir Oltean if (table->entry_count) { 4868aa9ebccSVladimir Oltean kfree(table->entries); 4878aa9ebccSVladimir Oltean table->entry_count = 0; 4888aa9ebccSVladimir Oltean } 4898aa9ebccSVladimir Oltean 490fd6f2c25SVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 4918aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 4928aa9ebccSVladimir Oltean if (!table->entries) 4938aa9ebccSVladimir Oltean return -ENOMEM; 4948aa9ebccSVladimir Oltean 495fd6f2c25SVladimir Oltean table->entry_count = table->ops->max_entry_count; 4968aa9ebccSVladimir Oltean 4978aa9ebccSVladimir Oltean l2fwd = table->entries; 4988aa9ebccSVladimir Oltean 4993fa21270SVladimir Oltean /* First 5 entries in the L2 Forwarding Table define the forwarding 5003fa21270SVladimir Oltean * rules and the VLAN PCP to ingress queue mapping. 5013fa21270SVladimir Oltean * Set up the ingress queue mapping first. 5027f7ccdeaSVladimir Oltean */ 5033fa21270SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 5043fa21270SVladimir Oltean if (dsa_is_unused_port(ds, port)) 5058aa9ebccSVladimir Oltean continue; 5068aa9ebccSVladimir Oltean 5073fa21270SVladimir Oltean for (tc = 0; tc < SJA1105_NUM_TC; tc++) 5083fa21270SVladimir Oltean l2fwd[port].vlan_pmap[tc] = tc; 5093fa21270SVladimir Oltean } 5104d942354SVladimir Oltean 5113fa21270SVladimir Oltean /* Then manage the forwarding domain for user ports. These can forward 5123fa21270SVladimir Oltean * only to the always-on domain (CPU port and DSA links) 5133fa21270SVladimir Oltean */ 5143fa21270SVladimir Oltean for (from = 0; from < ds->num_ports; from++) { 5153fa21270SVladimir Oltean if (!dsa_is_user_port(ds, from)) 5163fa21270SVladimir Oltean continue; 5174d942354SVladimir Oltean 5183fa21270SVladimir Oltean for (to = 0; to < ds->num_ports; to++) { 5193fa21270SVladimir Oltean if (!dsa_is_cpu_port(ds, to) && 5203fa21270SVladimir Oltean !dsa_is_dsa_port(ds, to)) 5213fa21270SVladimir Oltean continue; 5223fa21270SVladimir Oltean 5233fa21270SVladimir Oltean l2fwd[from].bc_domain |= BIT(to); 5243fa21270SVladimir Oltean l2fwd[from].fl_domain |= BIT(to); 5253fa21270SVladimir Oltean 5263fa21270SVladimir Oltean sja1105_port_allow_traffic(l2fwd, from, to, true); 5273fa21270SVladimir Oltean } 5283fa21270SVladimir Oltean } 5293fa21270SVladimir Oltean 5303fa21270SVladimir Oltean /* Then manage the forwarding domain for DSA links and CPU ports (the 5313fa21270SVladimir Oltean * always-on domain). These can send packets to any enabled port except 5323fa21270SVladimir Oltean * themselves. 5333fa21270SVladimir Oltean */ 5343fa21270SVladimir Oltean for (from = 0; from < ds->num_ports; from++) { 5353fa21270SVladimir Oltean if (!dsa_is_cpu_port(ds, from) && !dsa_is_dsa_port(ds, from)) 5363fa21270SVladimir Oltean continue; 5373fa21270SVladimir Oltean 5383fa21270SVladimir Oltean for (to = 0; to < ds->num_ports; to++) { 5393fa21270SVladimir Oltean if (dsa_is_unused_port(ds, to)) 5403fa21270SVladimir Oltean continue; 5413fa21270SVladimir Oltean 5423fa21270SVladimir Oltean if (from == to) 5433fa21270SVladimir Oltean continue; 5443fa21270SVladimir Oltean 5453fa21270SVladimir Oltean l2fwd[from].bc_domain |= BIT(to); 5463fa21270SVladimir Oltean l2fwd[from].fl_domain |= BIT(to); 5473fa21270SVladimir Oltean 5483fa21270SVladimir Oltean sja1105_port_allow_traffic(l2fwd, from, to, true); 5493fa21270SVladimir Oltean } 5503fa21270SVladimir Oltean } 5513fa21270SVladimir Oltean 552*0f9b762cSVladimir Oltean /* In odd topologies ("H" connections where there is a DSA link to 553*0f9b762cSVladimir Oltean * another switch which also has its own CPU port), TX packets can loop 554*0f9b762cSVladimir Oltean * back into the system (they are flooded from CPU port 1 to the DSA 555*0f9b762cSVladimir Oltean * link, and from there to CPU port 2). Prevent this from happening by 556*0f9b762cSVladimir Oltean * cutting RX from DSA links towards our CPU port, if the remote switch 557*0f9b762cSVladimir Oltean * has its own CPU port and therefore doesn't need ours for network 558*0f9b762cSVladimir Oltean * stack termination. 559*0f9b762cSVladimir Oltean */ 560*0f9b762cSVladimir Oltean dst = ds->dst; 561*0f9b762cSVladimir Oltean 562*0f9b762cSVladimir Oltean list_for_each_entry(dl, &dst->rtable, list) { 563*0f9b762cSVladimir Oltean if (dl->dp->ds != ds || dl->link_dp->cpu_dp == dl->dp->cpu_dp) 564*0f9b762cSVladimir Oltean continue; 565*0f9b762cSVladimir Oltean 566*0f9b762cSVladimir Oltean from = dl->dp->index; 567*0f9b762cSVladimir Oltean to = dsa_upstream_port(ds, from); 568*0f9b762cSVladimir Oltean 569*0f9b762cSVladimir Oltean dev_warn(ds->dev, 570*0f9b762cSVladimir Oltean "H topology detected, cutting RX from DSA link %d to CPU port %d to prevent TX packet loops\n", 571*0f9b762cSVladimir Oltean from, to); 572*0f9b762cSVladimir Oltean 573*0f9b762cSVladimir Oltean sja1105_port_allow_traffic(l2fwd, from, to, false); 574*0f9b762cSVladimir Oltean 575*0f9b762cSVladimir Oltean l2fwd[from].bc_domain &= ~BIT(to); 576*0f9b762cSVladimir Oltean l2fwd[from].fl_domain &= ~BIT(to); 577*0f9b762cSVladimir Oltean } 578*0f9b762cSVladimir Oltean 5793fa21270SVladimir Oltean /* Finally, manage the egress flooding domain. All ports start up with 5803fa21270SVladimir Oltean * flooding enabled, including the CPU port and DSA links. 5813fa21270SVladimir Oltean */ 5823fa21270SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 5833fa21270SVladimir Oltean if (dsa_is_unused_port(ds, port)) 5843fa21270SVladimir Oltean continue; 5853fa21270SVladimir Oltean 5863fa21270SVladimir Oltean priv->ucast_egress_floods |= BIT(port); 5873fa21270SVladimir Oltean priv->bcast_egress_floods |= BIT(port); 5888aa9ebccSVladimir Oltean } 589f238fef1SVladimir Oltean 5908aa9ebccSVladimir Oltean /* Next 8 entries define VLAN PCP mapping from ingress to egress. 5918aa9ebccSVladimir Oltean * Create a one-to-one mapping. 5928aa9ebccSVladimir Oltean */ 5933fa21270SVladimir Oltean for (tc = 0; tc < SJA1105_NUM_TC; tc++) { 5943fa21270SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 5953fa21270SVladimir Oltean if (dsa_is_unused_port(ds, port)) 596f238fef1SVladimir Oltean continue; 597f238fef1SVladimir Oltean 5983fa21270SVladimir Oltean l2fwd[ds->num_ports + tc].vlan_pmap[port] = tc; 599f238fef1SVladimir Oltean } 6003e77e59bSVladimir Oltean 6013fa21270SVladimir Oltean l2fwd[ds->num_ports + tc].type_egrpcp2outputq = true; 6023e77e59bSVladimir Oltean } 6033e77e59bSVladimir Oltean 6043e77e59bSVladimir Oltean return 0; 6053e77e59bSVladimir Oltean } 6063e77e59bSVladimir Oltean 6073e77e59bSVladimir Oltean static int sja1110_init_pcp_remapping(struct sja1105_private *priv) 6083e77e59bSVladimir Oltean { 6093e77e59bSVladimir Oltean struct sja1110_pcp_remapping_entry *pcp_remap; 6103e77e59bSVladimir Oltean struct dsa_switch *ds = priv->ds; 6113e77e59bSVladimir Oltean struct sja1105_table *table; 6123e77e59bSVladimir Oltean int port, tc; 6133e77e59bSVladimir Oltean 6143e77e59bSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_PCP_REMAPPING]; 6153e77e59bSVladimir Oltean 6163e77e59bSVladimir Oltean /* Nothing to do for SJA1105 */ 6173e77e59bSVladimir Oltean if (!table->ops->max_entry_count) 6183e77e59bSVladimir Oltean return 0; 6193e77e59bSVladimir Oltean 6203e77e59bSVladimir Oltean if (table->entry_count) { 6213e77e59bSVladimir Oltean kfree(table->entries); 6223e77e59bSVladimir Oltean table->entry_count = 0; 6233e77e59bSVladimir Oltean } 6243e77e59bSVladimir Oltean 6253e77e59bSVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 6263e77e59bSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 6273e77e59bSVladimir Oltean if (!table->entries) 6283e77e59bSVladimir Oltean return -ENOMEM; 6293e77e59bSVladimir Oltean 6303e77e59bSVladimir Oltean table->entry_count = table->ops->max_entry_count; 6313e77e59bSVladimir Oltean 6323e77e59bSVladimir Oltean pcp_remap = table->entries; 6333e77e59bSVladimir Oltean 6343e77e59bSVladimir Oltean /* Repeat the configuration done for vlan_pmap */ 6353e77e59bSVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 6363e77e59bSVladimir Oltean if (dsa_is_unused_port(ds, port)) 6373e77e59bSVladimir Oltean continue; 6383e77e59bSVladimir Oltean 6393e77e59bSVladimir Oltean for (tc = 0; tc < SJA1105_NUM_TC; tc++) 6403e77e59bSVladimir Oltean pcp_remap[port].egrpcp[tc] = tc; 641f238fef1SVladimir Oltean } 6428aa9ebccSVladimir Oltean 6438aa9ebccSVladimir Oltean return 0; 6448aa9ebccSVladimir Oltean } 6458aa9ebccSVladimir Oltean 6468aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding_params(struct sja1105_private *priv) 6478aa9ebccSVladimir Oltean { 6481bf658eeSVladimir Oltean struct sja1105_l2_forwarding_params_entry *l2fwd_params; 6498aa9ebccSVladimir Oltean struct sja1105_table *table; 6508aa9ebccSVladimir Oltean 6518aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS]; 6528aa9ebccSVladimir Oltean 6538aa9ebccSVladimir Oltean if (table->entry_count) { 6548aa9ebccSVladimir Oltean kfree(table->entries); 6558aa9ebccSVladimir Oltean table->entry_count = 0; 6568aa9ebccSVladimir Oltean } 6578aa9ebccSVladimir Oltean 658fd6f2c25SVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 6598aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 6608aa9ebccSVladimir Oltean if (!table->entries) 6618aa9ebccSVladimir Oltean return -ENOMEM; 6628aa9ebccSVladimir Oltean 663fd6f2c25SVladimir Oltean table->entry_count = table->ops->max_entry_count; 6648aa9ebccSVladimir Oltean 6658aa9ebccSVladimir Oltean /* This table only has a single entry */ 6661bf658eeSVladimir Oltean l2fwd_params = table->entries; 6671bf658eeSVladimir Oltean 6681bf658eeSVladimir Oltean /* Disallow dynamic reconfiguration of vlan_pmap */ 6691bf658eeSVladimir Oltean l2fwd_params->max_dynp = 0; 6701bf658eeSVladimir Oltean /* Use a single memory partition for all ingress queues */ 6711bf658eeSVladimir Oltean l2fwd_params->part_spc[0] = priv->info->max_frame_mem; 6728aa9ebccSVladimir Oltean 6738aa9ebccSVladimir Oltean return 0; 6748aa9ebccSVladimir Oltean } 6758aa9ebccSVladimir Oltean 676aaa270c6SVladimir Oltean void sja1105_frame_memory_partitioning(struct sja1105_private *priv) 677aaa270c6SVladimir Oltean { 678aaa270c6SVladimir Oltean struct sja1105_l2_forwarding_params_entry *l2_fwd_params; 679aaa270c6SVladimir Oltean struct sja1105_vl_forwarding_params_entry *vl_fwd_params; 680aaa270c6SVladimir Oltean struct sja1105_table *table; 681aaa270c6SVladimir Oltean 682aaa270c6SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS]; 683aaa270c6SVladimir Oltean l2_fwd_params = table->entries; 6840fac6aa0SVladimir Oltean l2_fwd_params->part_spc[0] = SJA1105_MAX_FRAME_MEMORY; 685aaa270c6SVladimir Oltean 686aaa270c6SVladimir Oltean /* If we have any critical-traffic virtual links, we need to reserve 687aaa270c6SVladimir Oltean * some frame buffer memory for them. At the moment, hardcode the value 688aaa270c6SVladimir Oltean * at 100 blocks of 128 bytes of memory each. This leaves 829 blocks 689aaa270c6SVladimir Oltean * remaining for best-effort traffic. TODO: figure out a more flexible 690aaa270c6SVladimir Oltean * way to perform the frame buffer partitioning. 691aaa270c6SVladimir Oltean */ 692aaa270c6SVladimir Oltean if (!priv->static_config.tables[BLK_IDX_VL_FORWARDING].entry_count) 693aaa270c6SVladimir Oltean return; 694aaa270c6SVladimir Oltean 695aaa270c6SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_VL_FORWARDING_PARAMS]; 696aaa270c6SVladimir Oltean vl_fwd_params = table->entries; 697aaa270c6SVladimir Oltean 698aaa270c6SVladimir Oltean l2_fwd_params->part_spc[0] -= SJA1105_VL_FRAME_MEMORY; 699aaa270c6SVladimir Oltean vl_fwd_params->partspc[0] = SJA1105_VL_FRAME_MEMORY; 700aaa270c6SVladimir Oltean } 701aaa270c6SVladimir Oltean 702ceec8bc0SVladimir Oltean /* SJA1110 TDMACONFIGIDX values: 703ceec8bc0SVladimir Oltean * 704ceec8bc0SVladimir Oltean * | 100 Mbps ports | 1Gbps ports | 2.5Gbps ports | Disabled ports 705ceec8bc0SVladimir Oltean * -----+----------------+---------------+---------------+--------------- 706ceec8bc0SVladimir Oltean * 0 | 0, [5:10] | [1:2] | [3:4] | retag 707ceec8bc0SVladimir Oltean * 1 |0, [5:10], retag| [1:2] | [3:4] | - 708ceec8bc0SVladimir Oltean * 2 | 0, [5:10] | [1:3], retag | 4 | - 709ceec8bc0SVladimir Oltean * 3 | 0, [5:10] |[1:2], 4, retag| 3 | - 710ceec8bc0SVladimir Oltean * 4 | 0, 2, [5:10] | 1, retag | [3:4] | - 711ceec8bc0SVladimir Oltean * 5 | 0, 1, [5:10] | 2, retag | [3:4] | - 712ceec8bc0SVladimir Oltean * 14 | 0, [5:10] | [1:4], retag | - | - 713ceec8bc0SVladimir Oltean * 15 | [5:10] | [0:4], retag | - | - 714ceec8bc0SVladimir Oltean */ 715ceec8bc0SVladimir Oltean static void sja1110_select_tdmaconfigidx(struct sja1105_private *priv) 716ceec8bc0SVladimir Oltean { 717ceec8bc0SVladimir Oltean struct sja1105_general_params_entry *general_params; 718ceec8bc0SVladimir Oltean struct sja1105_table *table; 719ceec8bc0SVladimir Oltean bool port_1_is_base_tx; 720ceec8bc0SVladimir Oltean bool port_3_is_2500; 721ceec8bc0SVladimir Oltean bool port_4_is_2500; 722ceec8bc0SVladimir Oltean u64 tdmaconfigidx; 723ceec8bc0SVladimir Oltean 724ceec8bc0SVladimir Oltean if (priv->info->device_id != SJA1110_DEVICE_ID) 725ceec8bc0SVladimir Oltean return; 726ceec8bc0SVladimir Oltean 727ceec8bc0SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; 728ceec8bc0SVladimir Oltean general_params = table->entries; 729ceec8bc0SVladimir Oltean 730ceec8bc0SVladimir Oltean /* All the settings below are "as opposed to SGMII", which is the 731ceec8bc0SVladimir Oltean * other pinmuxing option. 732ceec8bc0SVladimir Oltean */ 733ceec8bc0SVladimir Oltean port_1_is_base_tx = priv->phy_mode[1] == PHY_INTERFACE_MODE_INTERNAL; 734ceec8bc0SVladimir Oltean port_3_is_2500 = priv->phy_mode[3] == PHY_INTERFACE_MODE_2500BASEX; 735ceec8bc0SVladimir Oltean port_4_is_2500 = priv->phy_mode[4] == PHY_INTERFACE_MODE_2500BASEX; 736ceec8bc0SVladimir Oltean 737ceec8bc0SVladimir Oltean if (port_1_is_base_tx) 738ceec8bc0SVladimir Oltean /* Retagging port will operate at 1 Gbps */ 739ceec8bc0SVladimir Oltean tdmaconfigidx = 5; 740ceec8bc0SVladimir Oltean else if (port_3_is_2500 && port_4_is_2500) 741ceec8bc0SVladimir Oltean /* Retagging port will operate at 100 Mbps */ 742ceec8bc0SVladimir Oltean tdmaconfigidx = 1; 743ceec8bc0SVladimir Oltean else if (port_3_is_2500) 744ceec8bc0SVladimir Oltean /* Retagging port will operate at 1 Gbps */ 745ceec8bc0SVladimir Oltean tdmaconfigidx = 3; 746ceec8bc0SVladimir Oltean else if (port_4_is_2500) 747ceec8bc0SVladimir Oltean /* Retagging port will operate at 1 Gbps */ 748ceec8bc0SVladimir Oltean tdmaconfigidx = 2; 749ceec8bc0SVladimir Oltean else 750ceec8bc0SVladimir Oltean /* Retagging port will operate at 1 Gbps */ 751ceec8bc0SVladimir Oltean tdmaconfigidx = 14; 752ceec8bc0SVladimir Oltean 753ceec8bc0SVladimir Oltean general_params->tdmaconfigidx = tdmaconfigidx; 754ceec8bc0SVladimir Oltean } 755ceec8bc0SVladimir Oltean 75630a100e6SVladimir Oltean static int sja1105_init_topology(struct sja1105_private *priv, 75730a100e6SVladimir Oltean struct sja1105_general_params_entry *general_params) 75830a100e6SVladimir Oltean { 75930a100e6SVladimir Oltean struct dsa_switch *ds = priv->ds; 76030a100e6SVladimir Oltean int port; 76130a100e6SVladimir Oltean 76230a100e6SVladimir Oltean /* The host port is the destination for traffic matching mac_fltres1 76330a100e6SVladimir Oltean * and mac_fltres0 on all ports except itself. Default to an invalid 76430a100e6SVladimir Oltean * value. 76530a100e6SVladimir Oltean */ 76630a100e6SVladimir Oltean general_params->host_port = ds->num_ports; 76730a100e6SVladimir Oltean 76830a100e6SVladimir Oltean /* Link-local traffic received on casc_port will be forwarded 76930a100e6SVladimir Oltean * to host_port without embedding the source port and device ID 77030a100e6SVladimir Oltean * info in the destination MAC address, and no RX timestamps will be 77130a100e6SVladimir Oltean * taken either (presumably because it is a cascaded port and a 77230a100e6SVladimir Oltean * downstream SJA switch already did that). 77330a100e6SVladimir Oltean * To disable the feature, we need to do different things depending on 77430a100e6SVladimir Oltean * switch generation. On SJA1105 we need to set an invalid port, while 77530a100e6SVladimir Oltean * on SJA1110 which support multiple cascaded ports, this field is a 77630a100e6SVladimir Oltean * bitmask so it must be left zero. 77730a100e6SVladimir Oltean */ 77830a100e6SVladimir Oltean if (!priv->info->multiple_cascade_ports) 77930a100e6SVladimir Oltean general_params->casc_port = ds->num_ports; 78030a100e6SVladimir Oltean 78130a100e6SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 78230a100e6SVladimir Oltean bool is_upstream = dsa_is_upstream_port(ds, port); 78330a100e6SVladimir Oltean bool is_dsa_link = dsa_is_dsa_port(ds, port); 78430a100e6SVladimir Oltean 78530a100e6SVladimir Oltean /* Upstream ports can be dedicated CPU ports or 78630a100e6SVladimir Oltean * upstream-facing DSA links 78730a100e6SVladimir Oltean */ 78830a100e6SVladimir Oltean if (is_upstream) { 78930a100e6SVladimir Oltean if (general_params->host_port == ds->num_ports) { 79030a100e6SVladimir Oltean general_params->host_port = port; 79130a100e6SVladimir Oltean } else { 79230a100e6SVladimir Oltean dev_err(ds->dev, 79330a100e6SVladimir Oltean "Port %llu is already a host port, configuring %d as one too is not supported\n", 79430a100e6SVladimir Oltean general_params->host_port, port); 79530a100e6SVladimir Oltean return -EINVAL; 79630a100e6SVladimir Oltean } 79730a100e6SVladimir Oltean } 79830a100e6SVladimir Oltean 79930a100e6SVladimir Oltean /* Cascade ports are downstream-facing DSA links */ 80030a100e6SVladimir Oltean if (is_dsa_link && !is_upstream) { 80130a100e6SVladimir Oltean if (priv->info->multiple_cascade_ports) { 80230a100e6SVladimir Oltean general_params->casc_port |= BIT(port); 80330a100e6SVladimir Oltean } else if (general_params->casc_port == ds->num_ports) { 80430a100e6SVladimir Oltean general_params->casc_port = port; 80530a100e6SVladimir Oltean } else { 80630a100e6SVladimir Oltean dev_err(ds->dev, 80730a100e6SVladimir Oltean "Port %llu is already a cascade port, configuring %d as one too is not supported\n", 80830a100e6SVladimir Oltean general_params->casc_port, port); 80930a100e6SVladimir Oltean return -EINVAL; 81030a100e6SVladimir Oltean } 81130a100e6SVladimir Oltean } 81230a100e6SVladimir Oltean } 81330a100e6SVladimir Oltean 81430a100e6SVladimir Oltean if (general_params->host_port == ds->num_ports) { 81530a100e6SVladimir Oltean dev_err(ds->dev, "No host port configured\n"); 81630a100e6SVladimir Oltean return -EINVAL; 81730a100e6SVladimir Oltean } 81830a100e6SVladimir Oltean 81930a100e6SVladimir Oltean return 0; 82030a100e6SVladimir Oltean } 82130a100e6SVladimir Oltean 8228aa9ebccSVladimir Oltean static int sja1105_init_general_params(struct sja1105_private *priv) 8238aa9ebccSVladimir Oltean { 8248aa9ebccSVladimir Oltean struct sja1105_general_params_entry default_general_params = { 825511e6ca0SVladimir Oltean /* Allow dynamic changing of the mirror port */ 826511e6ca0SVladimir Oltean .mirr_ptacu = true, 8278aa9ebccSVladimir Oltean .switchid = priv->ds->index, 8285f06c63bSVladimir Oltean /* Priority queue for link-local management frames 8295f06c63bSVladimir Oltean * (both ingress to and egress from CPU - PTP, STP etc) 8305f06c63bSVladimir Oltean */ 83108fde09aSVladimir Oltean .hostprio = 7, 8328aa9ebccSVladimir Oltean .mac_fltres1 = SJA1105_LINKLOCAL_FILTER_A, 8338aa9ebccSVladimir Oltean .mac_flt1 = SJA1105_LINKLOCAL_FILTER_A_MASK, 83442824463SVladimir Oltean .incl_srcpt1 = false, 8358aa9ebccSVladimir Oltean .send_meta1 = false, 8368aa9ebccSVladimir Oltean .mac_fltres0 = SJA1105_LINKLOCAL_FILTER_B, 8378aa9ebccSVladimir Oltean .mac_flt0 = SJA1105_LINKLOCAL_FILTER_B_MASK, 83842824463SVladimir Oltean .incl_srcpt0 = false, 8398aa9ebccSVladimir Oltean .send_meta0 = false, 840511e6ca0SVladimir Oltean /* Default to an invalid value */ 841542043e9SVladimir Oltean .mirr_port = priv->ds->num_ports, 8428aa9ebccSVladimir Oltean /* No TTEthernet */ 843dfacc5a2SVladimir Oltean .vllupformat = SJA1105_VL_FORMAT_PSFP, 8448aa9ebccSVladimir Oltean .vlmarker = 0, 8458aa9ebccSVladimir Oltean .vlmask = 0, 8468aa9ebccSVladimir Oltean /* Only update correctionField for 1-step PTP (L2 transport) */ 8478aa9ebccSVladimir Oltean .ignore2stf = 0, 8486666cebcSVladimir Oltean /* Forcefully disable VLAN filtering by telling 8496666cebcSVladimir Oltean * the switch that VLAN has a different EtherType. 8506666cebcSVladimir Oltean */ 8516666cebcSVladimir Oltean .tpid = ETH_P_SJA1105, 8526666cebcSVladimir Oltean .tpid2 = ETH_P_SJA1105, 85329305260SVladimir Oltean /* Enable the TTEthernet engine on SJA1110 */ 85429305260SVladimir Oltean .tte_en = true, 8554913b8ebSVladimir Oltean /* Set up the EtherType for control packets on SJA1110 */ 8564913b8ebSVladimir Oltean .header_type = ETH_P_SJA1110, 8578aa9ebccSVladimir Oltean }; 8586c0de59bSVladimir Oltean struct sja1105_general_params_entry *general_params; 8598aa9ebccSVladimir Oltean struct sja1105_table *table; 86030a100e6SVladimir Oltean int rc; 861df2a81a3SVladimir Oltean 86230a100e6SVladimir Oltean rc = sja1105_init_topology(priv, &default_general_params); 86330a100e6SVladimir Oltean if (rc) 86430a100e6SVladimir Oltean return rc; 8658aa9ebccSVladimir Oltean 8668aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; 8678aa9ebccSVladimir Oltean 8688aa9ebccSVladimir Oltean if (table->entry_count) { 8698aa9ebccSVladimir Oltean kfree(table->entries); 8708aa9ebccSVladimir Oltean table->entry_count = 0; 8718aa9ebccSVladimir Oltean } 8728aa9ebccSVladimir Oltean 873fd6f2c25SVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 8748aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 8758aa9ebccSVladimir Oltean if (!table->entries) 8768aa9ebccSVladimir Oltean return -ENOMEM; 8778aa9ebccSVladimir Oltean 878fd6f2c25SVladimir Oltean table->entry_count = table->ops->max_entry_count; 8798aa9ebccSVladimir Oltean 8806c0de59bSVladimir Oltean general_params = table->entries; 8816c0de59bSVladimir Oltean 8828aa9ebccSVladimir Oltean /* This table only has a single entry */ 8836c0de59bSVladimir Oltean general_params[0] = default_general_params; 8848aa9ebccSVladimir Oltean 885ceec8bc0SVladimir Oltean sja1110_select_tdmaconfigidx(priv); 886ceec8bc0SVladimir Oltean 8878aa9ebccSVladimir Oltean return 0; 8888aa9ebccSVladimir Oltean } 8898aa9ebccSVladimir Oltean 89079d5511cSVladimir Oltean static int sja1105_init_avb_params(struct sja1105_private *priv) 89179d5511cSVladimir Oltean { 89279d5511cSVladimir Oltean struct sja1105_avb_params_entry *avb; 89379d5511cSVladimir Oltean struct sja1105_table *table; 89479d5511cSVladimir Oltean 89579d5511cSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_AVB_PARAMS]; 89679d5511cSVladimir Oltean 89779d5511cSVladimir Oltean /* Discard previous AVB Parameters Table */ 89879d5511cSVladimir Oltean if (table->entry_count) { 89979d5511cSVladimir Oltean kfree(table->entries); 90079d5511cSVladimir Oltean table->entry_count = 0; 90179d5511cSVladimir Oltean } 90279d5511cSVladimir Oltean 903fd6f2c25SVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 90479d5511cSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 90579d5511cSVladimir Oltean if (!table->entries) 90679d5511cSVladimir Oltean return -ENOMEM; 90779d5511cSVladimir Oltean 908fd6f2c25SVladimir Oltean table->entry_count = table->ops->max_entry_count; 90979d5511cSVladimir Oltean 91079d5511cSVladimir Oltean avb = table->entries; 91179d5511cSVladimir Oltean 91279d5511cSVladimir Oltean /* Configure the MAC addresses for meta frames */ 91379d5511cSVladimir Oltean avb->destmeta = SJA1105_META_DMAC; 91479d5511cSVladimir Oltean avb->srcmeta = SJA1105_META_SMAC; 915747e5eb3SVladimir Oltean /* On P/Q/R/S, configure the direction of the PTP_CLK pin as input by 916747e5eb3SVladimir Oltean * default. This is because there might be boards with a hardware 917747e5eb3SVladimir Oltean * layout where enabling the pin as output might cause an electrical 918747e5eb3SVladimir Oltean * clash. On E/T the pin is always an output, which the board designers 919747e5eb3SVladimir Oltean * probably already knew, so even if there are going to be electrical 920747e5eb3SVladimir Oltean * issues, there's nothing we can do. 921747e5eb3SVladimir Oltean */ 922747e5eb3SVladimir Oltean avb->cas_master = false; 92379d5511cSVladimir Oltean 92479d5511cSVladimir Oltean return 0; 92579d5511cSVladimir Oltean } 92679d5511cSVladimir Oltean 927a7cc081cSVladimir Oltean /* The L2 policing table is 2-stage. The table is looked up for each frame 928a7cc081cSVladimir Oltean * according to the ingress port, whether it was broadcast or not, and the 929a7cc081cSVladimir Oltean * classified traffic class (given by VLAN PCP). This portion of the lookup is 930a7cc081cSVladimir Oltean * fixed, and gives access to the SHARINDX, an indirection register pointing 931a7cc081cSVladimir Oltean * within the policing table itself, which is used to resolve the policer that 932a7cc081cSVladimir Oltean * will be used for this frame. 933a7cc081cSVladimir Oltean * 934a7cc081cSVladimir Oltean * Stage 1 Stage 2 935a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 936a7cc081cSVladimir Oltean * |Port 0 TC 0 |SHARINDX| | Policer 0: Rate, Burst, MTU | 937a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 938a7cc081cSVladimir Oltean * |Port 0 TC 1 |SHARINDX| | Policer 1: Rate, Burst, MTU | 939a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 940a7cc081cSVladimir Oltean * ... | Policer 2: Rate, Burst, MTU | 941a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 942a7cc081cSVladimir Oltean * |Port 0 TC 7 |SHARINDX| | Policer 3: Rate, Burst, MTU | 943a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 944a7cc081cSVladimir Oltean * |Port 1 TC 0 |SHARINDX| | Policer 4: Rate, Burst, MTU | 945a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 946a7cc081cSVladimir Oltean * ... | Policer 5: Rate, Burst, MTU | 947a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 948a7cc081cSVladimir Oltean * |Port 1 TC 7 |SHARINDX| | Policer 6: Rate, Burst, MTU | 949a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 950a7cc081cSVladimir Oltean * ... | Policer 7: Rate, Burst, MTU | 951a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 952a7cc081cSVladimir Oltean * |Port 4 TC 7 |SHARINDX| ... 953a7cc081cSVladimir Oltean * +------------+--------+ 954a7cc081cSVladimir Oltean * |Port 0 BCAST|SHARINDX| ... 955a7cc081cSVladimir Oltean * +------------+--------+ 956a7cc081cSVladimir Oltean * |Port 1 BCAST|SHARINDX| ... 957a7cc081cSVladimir Oltean * +------------+--------+ 958a7cc081cSVladimir Oltean * ... ... 959a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 960a7cc081cSVladimir Oltean * |Port 4 BCAST|SHARINDX| | Policer 44: Rate, Burst, MTU | 961a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 962a7cc081cSVladimir Oltean * 963a7cc081cSVladimir Oltean * In this driver, we shall use policers 0-4 as statically alocated port 964a7cc081cSVladimir Oltean * (matchall) policers. So we need to make the SHARINDX for all lookups 965a7cc081cSVladimir Oltean * corresponding to this ingress port (8 VLAN PCP lookups and 1 broadcast 966a7cc081cSVladimir Oltean * lookup) equal. 967a7cc081cSVladimir Oltean * The remaining policers (40) shall be dynamically allocated for flower 968a7cc081cSVladimir Oltean * policers, where the key is either vlan_prio or dst_mac ff:ff:ff:ff:ff:ff. 969a7cc081cSVladimir Oltean */ 9708aa9ebccSVladimir Oltean #define SJA1105_RATE_MBPS(speed) (((speed) * 64000) / 1000) 9718aa9ebccSVladimir Oltean 9728aa9ebccSVladimir Oltean static int sja1105_init_l2_policing(struct sja1105_private *priv) 9738aa9ebccSVladimir Oltean { 9748aa9ebccSVladimir Oltean struct sja1105_l2_policing_entry *policing; 975542043e9SVladimir Oltean struct dsa_switch *ds = priv->ds; 9768aa9ebccSVladimir Oltean struct sja1105_table *table; 977a7cc081cSVladimir Oltean int port, tc; 9788aa9ebccSVladimir Oltean 9798aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_POLICING]; 9808aa9ebccSVladimir Oltean 9818aa9ebccSVladimir Oltean /* Discard previous L2 Policing Table */ 9828aa9ebccSVladimir Oltean if (table->entry_count) { 9838aa9ebccSVladimir Oltean kfree(table->entries); 9848aa9ebccSVladimir Oltean table->entry_count = 0; 9858aa9ebccSVladimir Oltean } 9868aa9ebccSVladimir Oltean 987fd6f2c25SVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 9888aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 9898aa9ebccSVladimir Oltean if (!table->entries) 9908aa9ebccSVladimir Oltean return -ENOMEM; 9918aa9ebccSVladimir Oltean 992fd6f2c25SVladimir Oltean table->entry_count = table->ops->max_entry_count; 9938aa9ebccSVladimir Oltean 9948aa9ebccSVladimir Oltean policing = table->entries; 9958aa9ebccSVladimir Oltean 996a7cc081cSVladimir Oltean /* Setup shared indices for the matchall policers */ 997542043e9SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 99838fbe91fSVladimir Oltean int mcast = (ds->num_ports * (SJA1105_NUM_TC + 1)) + port; 999542043e9SVladimir Oltean int bcast = (ds->num_ports * SJA1105_NUM_TC) + port; 1000a7cc081cSVladimir Oltean 1001a7cc081cSVladimir Oltean for (tc = 0; tc < SJA1105_NUM_TC; tc++) 1002a7cc081cSVladimir Oltean policing[port * SJA1105_NUM_TC + tc].sharindx = port; 1003a7cc081cSVladimir Oltean 1004a7cc081cSVladimir Oltean policing[bcast].sharindx = port; 100538fbe91fSVladimir Oltean /* Only SJA1110 has multicast policers */ 100638fbe91fSVladimir Oltean if (mcast <= table->ops->max_entry_count) 100738fbe91fSVladimir Oltean policing[mcast].sharindx = port; 1008a7cc081cSVladimir Oltean } 1009a7cc081cSVladimir Oltean 1010a7cc081cSVladimir Oltean /* Setup the matchall policer parameters */ 1011542043e9SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 1012c279c726SVladimir Oltean int mtu = VLAN_ETH_FRAME_LEN + ETH_FCS_LEN; 1013c279c726SVladimir Oltean 1014777e55e3SVladimir Oltean if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) 1015c279c726SVladimir Oltean mtu += VLAN_HLEN; 10168aa9ebccSVladimir Oltean 1017a7cc081cSVladimir Oltean policing[port].smax = 65535; /* Burst size in bytes */ 1018a7cc081cSVladimir Oltean policing[port].rate = SJA1105_RATE_MBPS(1000); 1019a7cc081cSVladimir Oltean policing[port].maxlen = mtu; 1020a7cc081cSVladimir Oltean policing[port].partition = 0; 10218aa9ebccSVladimir Oltean } 1022a7cc081cSVladimir Oltean 10238aa9ebccSVladimir Oltean return 0; 10248aa9ebccSVladimir Oltean } 10258aa9ebccSVladimir Oltean 10265d645df9SVladimir Oltean static int sja1105_static_config_load(struct sja1105_private *priv) 10278aa9ebccSVladimir Oltean { 10288aa9ebccSVladimir Oltean int rc; 10298aa9ebccSVladimir Oltean 10308aa9ebccSVladimir Oltean sja1105_static_config_free(&priv->static_config); 10318aa9ebccSVladimir Oltean rc = sja1105_static_config_init(&priv->static_config, 10328aa9ebccSVladimir Oltean priv->info->static_ops, 10338aa9ebccSVladimir Oltean priv->info->device_id); 10348aa9ebccSVladimir Oltean if (rc) 10358aa9ebccSVladimir Oltean return rc; 10368aa9ebccSVladimir Oltean 10378aa9ebccSVladimir Oltean /* Build static configuration */ 10388aa9ebccSVladimir Oltean rc = sja1105_init_mac_settings(priv); 10398aa9ebccSVladimir Oltean if (rc < 0) 10408aa9ebccSVladimir Oltean return rc; 10415d645df9SVladimir Oltean rc = sja1105_init_mii_settings(priv); 10428aa9ebccSVladimir Oltean if (rc < 0) 10438aa9ebccSVladimir Oltean return rc; 10448aa9ebccSVladimir Oltean rc = sja1105_init_static_fdb(priv); 10458aa9ebccSVladimir Oltean if (rc < 0) 10468aa9ebccSVladimir Oltean return rc; 10478aa9ebccSVladimir Oltean rc = sja1105_init_static_vlan(priv); 10488aa9ebccSVladimir Oltean if (rc < 0) 10498aa9ebccSVladimir Oltean return rc; 10508aa9ebccSVladimir Oltean rc = sja1105_init_l2_lookup_params(priv); 10518aa9ebccSVladimir Oltean if (rc < 0) 10528aa9ebccSVladimir Oltean return rc; 10538aa9ebccSVladimir Oltean rc = sja1105_init_l2_forwarding(priv); 10548aa9ebccSVladimir Oltean if (rc < 0) 10558aa9ebccSVladimir Oltean return rc; 10568aa9ebccSVladimir Oltean rc = sja1105_init_l2_forwarding_params(priv); 10578aa9ebccSVladimir Oltean if (rc < 0) 10588aa9ebccSVladimir Oltean return rc; 10598aa9ebccSVladimir Oltean rc = sja1105_init_l2_policing(priv); 10608aa9ebccSVladimir Oltean if (rc < 0) 10618aa9ebccSVladimir Oltean return rc; 10628aa9ebccSVladimir Oltean rc = sja1105_init_general_params(priv); 10638aa9ebccSVladimir Oltean if (rc < 0) 10648aa9ebccSVladimir Oltean return rc; 106579d5511cSVladimir Oltean rc = sja1105_init_avb_params(priv); 106679d5511cSVladimir Oltean if (rc < 0) 106779d5511cSVladimir Oltean return rc; 10683e77e59bSVladimir Oltean rc = sja1110_init_pcp_remapping(priv); 10693e77e59bSVladimir Oltean if (rc < 0) 10703e77e59bSVladimir Oltean return rc; 10718aa9ebccSVladimir Oltean 10728aa9ebccSVladimir Oltean /* Send initial configuration to hardware via SPI */ 10738aa9ebccSVladimir Oltean return sja1105_static_config_upload(priv); 10748aa9ebccSVladimir Oltean } 10758aa9ebccSVladimir Oltean 107629afb83aSVladimir Oltean static int sja1105_parse_rgmii_delays(struct sja1105_private *priv) 1077f5b8631cSVladimir Oltean { 1078542043e9SVladimir Oltean struct dsa_switch *ds = priv->ds; 107929afb83aSVladimir Oltean int port; 1080f5b8631cSVladimir Oltean 108129afb83aSVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 108229afb83aSVladimir Oltean if (!priv->fixed_link[port]) 1083f5b8631cSVladimir Oltean continue; 1084f5b8631cSVladimir Oltean 108529afb83aSVladimir Oltean if (priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_RXID || 108629afb83aSVladimir Oltean priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_ID) 108729afb83aSVladimir Oltean priv->rgmii_rx_delay[port] = true; 1088f5b8631cSVladimir Oltean 108929afb83aSVladimir Oltean if (priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_TXID || 109029afb83aSVladimir Oltean priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_ID) 109129afb83aSVladimir Oltean priv->rgmii_tx_delay[port] = true; 1092f5b8631cSVladimir Oltean 109329afb83aSVladimir Oltean if ((priv->rgmii_rx_delay[port] || priv->rgmii_tx_delay[port]) && 1094f5b8631cSVladimir Oltean !priv->info->setup_rgmii_delay) 1095f5b8631cSVladimir Oltean return -EINVAL; 1096f5b8631cSVladimir Oltean } 1097f5b8631cSVladimir Oltean return 0; 1098f5b8631cSVladimir Oltean } 1099f5b8631cSVladimir Oltean 11008aa9ebccSVladimir Oltean static int sja1105_parse_ports_node(struct sja1105_private *priv, 11018aa9ebccSVladimir Oltean struct device_node *ports_node) 11028aa9ebccSVladimir Oltean { 11038aa9ebccSVladimir Oltean struct device *dev = &priv->spidev->dev; 11048aa9ebccSVladimir Oltean struct device_node *child; 11058aa9ebccSVladimir Oltean 110627afe0d3SVladimir Oltean for_each_available_child_of_node(ports_node, child) { 11078aa9ebccSVladimir Oltean struct device_node *phy_node; 11080c65b2b9SAndrew Lunn phy_interface_t phy_mode; 11098aa9ebccSVladimir Oltean u32 index; 11100c65b2b9SAndrew Lunn int err; 11118aa9ebccSVladimir Oltean 11128aa9ebccSVladimir Oltean /* Get switch port number from DT */ 11138aa9ebccSVladimir Oltean if (of_property_read_u32(child, "reg", &index) < 0) { 11148aa9ebccSVladimir Oltean dev_err(dev, "Port number not defined in device tree " 11158aa9ebccSVladimir Oltean "(property \"reg\")\n"); 11167ba771e3SNishka Dasgupta of_node_put(child); 11178aa9ebccSVladimir Oltean return -ENODEV; 11188aa9ebccSVladimir Oltean } 11198aa9ebccSVladimir Oltean 11208aa9ebccSVladimir Oltean /* Get PHY mode from DT */ 11210c65b2b9SAndrew Lunn err = of_get_phy_mode(child, &phy_mode); 11220c65b2b9SAndrew Lunn if (err) { 11238aa9ebccSVladimir Oltean dev_err(dev, "Failed to read phy-mode or " 11248aa9ebccSVladimir Oltean "phy-interface-type property for port %d\n", 11258aa9ebccSVladimir Oltean index); 11267ba771e3SNishka Dasgupta of_node_put(child); 11278aa9ebccSVladimir Oltean return -ENODEV; 11288aa9ebccSVladimir Oltean } 11298aa9ebccSVladimir Oltean 11308aa9ebccSVladimir Oltean phy_node = of_parse_phandle(child, "phy-handle", 0); 11318aa9ebccSVladimir Oltean if (!phy_node) { 11328aa9ebccSVladimir Oltean if (!of_phy_is_fixed_link(child)) { 11338aa9ebccSVladimir Oltean dev_err(dev, "phy-handle or fixed-link " 11348aa9ebccSVladimir Oltean "properties missing!\n"); 11357ba771e3SNishka Dasgupta of_node_put(child); 11368aa9ebccSVladimir Oltean return -ENODEV; 11378aa9ebccSVladimir Oltean } 11388aa9ebccSVladimir Oltean /* phy-handle is missing, but fixed-link isn't. 11398aa9ebccSVladimir Oltean * So it's a fixed link. Default to PHY role. 11408aa9ebccSVladimir Oltean */ 114129afb83aSVladimir Oltean priv->fixed_link[index] = true; 11428aa9ebccSVladimir Oltean } else { 11438aa9ebccSVladimir Oltean of_node_put(phy_node); 11448aa9ebccSVladimir Oltean } 11458aa9ebccSVladimir Oltean 1146bf4edf4aSVladimir Oltean priv->phy_mode[index] = phy_mode; 11478aa9ebccSVladimir Oltean } 11488aa9ebccSVladimir Oltean 11498aa9ebccSVladimir Oltean return 0; 11508aa9ebccSVladimir Oltean } 11518aa9ebccSVladimir Oltean 11525d645df9SVladimir Oltean static int sja1105_parse_dt(struct sja1105_private *priv) 11538aa9ebccSVladimir Oltean { 11548aa9ebccSVladimir Oltean struct device *dev = &priv->spidev->dev; 11558aa9ebccSVladimir Oltean struct device_node *switch_node = dev->of_node; 11568aa9ebccSVladimir Oltean struct device_node *ports_node; 11578aa9ebccSVladimir Oltean int rc; 11588aa9ebccSVladimir Oltean 11598aa9ebccSVladimir Oltean ports_node = of_get_child_by_name(switch_node, "ports"); 116015074a36SVladimir Oltean if (!ports_node) 116115074a36SVladimir Oltean ports_node = of_get_child_by_name(switch_node, "ethernet-ports"); 11628aa9ebccSVladimir Oltean if (!ports_node) { 11638aa9ebccSVladimir Oltean dev_err(dev, "Incorrect bindings: absent \"ports\" node\n"); 11648aa9ebccSVladimir Oltean return -ENODEV; 11658aa9ebccSVladimir Oltean } 11668aa9ebccSVladimir Oltean 11675d645df9SVladimir Oltean rc = sja1105_parse_ports_node(priv, ports_node); 11688aa9ebccSVladimir Oltean of_node_put(ports_node); 11698aa9ebccSVladimir Oltean 11708aa9ebccSVladimir Oltean return rc; 11718aa9ebccSVladimir Oltean } 11728aa9ebccSVladimir Oltean 1173c44d0535SVladimir Oltean /* Convert link speed from SJA1105 to ethtool encoding */ 117441fed17fSVladimir Oltean static int sja1105_port_speed_to_ethtool(struct sja1105_private *priv, 117541fed17fSVladimir Oltean u64 speed) 117641fed17fSVladimir Oltean { 117741fed17fSVladimir Oltean if (speed == priv->info->port_speed[SJA1105_SPEED_10MBPS]) 117841fed17fSVladimir Oltean return SPEED_10; 117941fed17fSVladimir Oltean if (speed == priv->info->port_speed[SJA1105_SPEED_100MBPS]) 118041fed17fSVladimir Oltean return SPEED_100; 118141fed17fSVladimir Oltean if (speed == priv->info->port_speed[SJA1105_SPEED_1000MBPS]) 118241fed17fSVladimir Oltean return SPEED_1000; 118341fed17fSVladimir Oltean if (speed == priv->info->port_speed[SJA1105_SPEED_2500MBPS]) 118441fed17fSVladimir Oltean return SPEED_2500; 118541fed17fSVladimir Oltean return SPEED_UNKNOWN; 118641fed17fSVladimir Oltean } 11878aa9ebccSVladimir Oltean 11888400cff6SVladimir Oltean /* Set link speed in the MAC configuration for a specific port. */ 11898aa9ebccSVladimir Oltean static int sja1105_adjust_port_config(struct sja1105_private *priv, int port, 11908400cff6SVladimir Oltean int speed_mbps) 11918aa9ebccSVladimir Oltean { 11928aa9ebccSVladimir Oltean struct sja1105_mac_config_entry *mac; 11938aa9ebccSVladimir Oltean struct device *dev = priv->ds->dev; 119441fed17fSVladimir Oltean u64 speed; 11958aa9ebccSVladimir Oltean int rc; 11968aa9ebccSVladimir Oltean 11978400cff6SVladimir Oltean /* On P/Q/R/S, one can read from the device via the MAC reconfiguration 11988400cff6SVladimir Oltean * tables. On E/T, MAC reconfig tables are not readable, only writable. 11998400cff6SVladimir Oltean * We have to *know* what the MAC looks like. For the sake of keeping 12008400cff6SVladimir Oltean * the code common, we'll use the static configuration tables as a 12018400cff6SVladimir Oltean * reasonable approximation for both E/T and P/Q/R/S. 12028400cff6SVladimir Oltean */ 12038aa9ebccSVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 12048aa9ebccSVladimir Oltean 1205f4cfcfbdSVladimir Oltean switch (speed_mbps) { 1206c44d0535SVladimir Oltean case SPEED_UNKNOWN: 1207a979a0abSVladimir Oltean /* PHYLINK called sja1105_mac_config() to inform us about 1208a979a0abSVladimir Oltean * the state->interface, but AN has not completed and the 1209a979a0abSVladimir Oltean * speed is not yet valid. UM10944.pdf says that setting 1210a979a0abSVladimir Oltean * SJA1105_SPEED_AUTO at runtime disables the port, so that is 1211a979a0abSVladimir Oltean * ok for power consumption in case AN will never complete - 1212a979a0abSVladimir Oltean * otherwise PHYLINK should come back with a new update. 1213a979a0abSVladimir Oltean */ 121441fed17fSVladimir Oltean speed = priv->info->port_speed[SJA1105_SPEED_AUTO]; 1215f4cfcfbdSVladimir Oltean break; 1216c44d0535SVladimir Oltean case SPEED_10: 121741fed17fSVladimir Oltean speed = priv->info->port_speed[SJA1105_SPEED_10MBPS]; 1218f4cfcfbdSVladimir Oltean break; 1219c44d0535SVladimir Oltean case SPEED_100: 122041fed17fSVladimir Oltean speed = priv->info->port_speed[SJA1105_SPEED_100MBPS]; 1221f4cfcfbdSVladimir Oltean break; 1222c44d0535SVladimir Oltean case SPEED_1000: 122341fed17fSVladimir Oltean speed = priv->info->port_speed[SJA1105_SPEED_1000MBPS]; 1224f4cfcfbdSVladimir Oltean break; 122556b63466SVladimir Oltean case SPEED_2500: 122656b63466SVladimir Oltean speed = priv->info->port_speed[SJA1105_SPEED_2500MBPS]; 122756b63466SVladimir Oltean break; 1228f4cfcfbdSVladimir Oltean default: 12298aa9ebccSVladimir Oltean dev_err(dev, "Invalid speed %iMbps\n", speed_mbps); 12308aa9ebccSVladimir Oltean return -EINVAL; 12318aa9ebccSVladimir Oltean } 12328aa9ebccSVladimir Oltean 12338400cff6SVladimir Oltean /* Overwrite SJA1105_SPEED_AUTO from the static MAC configuration 12348400cff6SVladimir Oltean * table, since this will be used for the clocking setup, and we no 12358400cff6SVladimir Oltean * longer need to store it in the static config (already told hardware 12368400cff6SVladimir Oltean * we want auto during upload phase). 1237ffe10e67SVladimir Oltean * Actually for the SGMII port, the MAC is fixed at 1 Gbps and 1238ffe10e67SVladimir Oltean * we need to configure the PCS only (if even that). 12398aa9ebccSVladimir Oltean */ 124091a05078SVladimir Oltean if (priv->phy_mode[port] == PHY_INTERFACE_MODE_SGMII) 124141fed17fSVladimir Oltean mac[port].speed = priv->info->port_speed[SJA1105_SPEED_1000MBPS]; 124256b63466SVladimir Oltean else if (priv->phy_mode[port] == PHY_INTERFACE_MODE_2500BASEX) 124356b63466SVladimir Oltean mac[port].speed = priv->info->port_speed[SJA1105_SPEED_2500MBPS]; 1244ffe10e67SVladimir Oltean else 12458aa9ebccSVladimir Oltean mac[port].speed = speed; 12468aa9ebccSVladimir Oltean 12478aa9ebccSVladimir Oltean /* Write to the dynamic reconfiguration tables */ 12488400cff6SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, 12498400cff6SVladimir Oltean &mac[port], true); 12508aa9ebccSVladimir Oltean if (rc < 0) { 12518aa9ebccSVladimir Oltean dev_err(dev, "Failed to write MAC config: %d\n", rc); 12528aa9ebccSVladimir Oltean return rc; 12538aa9ebccSVladimir Oltean } 12548aa9ebccSVladimir Oltean 12558aa9ebccSVladimir Oltean /* Reconfigure the PLLs for the RGMII interfaces (required 125 MHz at 12568aa9ebccSVladimir Oltean * gigabit, 25 MHz at 100 Mbps and 2.5 MHz at 10 Mbps). For MII and 12578aa9ebccSVladimir Oltean * RMII no change of the clock setup is required. Actually, changing 12588aa9ebccSVladimir Oltean * the clock setup does interrupt the clock signal for a certain time 12598aa9ebccSVladimir Oltean * which causes trouble for all PHYs relying on this signal. 12608aa9ebccSVladimir Oltean */ 126191a05078SVladimir Oltean if (!phy_interface_mode_is_rgmii(priv->phy_mode[port])) 12628aa9ebccSVladimir Oltean return 0; 12638aa9ebccSVladimir Oltean 12648aa9ebccSVladimir Oltean return sja1105_clocking_setup_port(priv, port); 12658aa9ebccSVladimir Oltean } 12668aa9ebccSVladimir Oltean 126739710229SVladimir Oltean /* The SJA1105 MAC programming model is through the static config (the xMII 126839710229SVladimir Oltean * Mode table cannot be dynamically reconfigured), and we have to program 126939710229SVladimir Oltean * that early (earlier than PHYLINK calls us, anyway). 127039710229SVladimir Oltean * So just error out in case the connected PHY attempts to change the initial 127139710229SVladimir Oltean * system interface MII protocol from what is defined in the DT, at least for 127239710229SVladimir Oltean * now. 127339710229SVladimir Oltean */ 127439710229SVladimir Oltean static bool sja1105_phy_mode_mismatch(struct sja1105_private *priv, int port, 127539710229SVladimir Oltean phy_interface_t interface) 127639710229SVladimir Oltean { 1277bf4edf4aSVladimir Oltean return priv->phy_mode[port] != interface; 127839710229SVladimir Oltean } 127939710229SVladimir Oltean 1280af7cd036SVladimir Oltean static void sja1105_mac_config(struct dsa_switch *ds, int port, 1281ffe10e67SVladimir Oltean unsigned int mode, 1282af7cd036SVladimir Oltean const struct phylink_link_state *state) 12838aa9ebccSVladimir Oltean { 12843ad1d171SVladimir Oltean struct dsa_port *dp = dsa_to_port(ds, port); 12858aa9ebccSVladimir Oltean struct sja1105_private *priv = ds->priv; 12863ad1d171SVladimir Oltean struct dw_xpcs *xpcs; 12878aa9ebccSVladimir Oltean 1288ec8582d1SVladimir Oltean if (sja1105_phy_mode_mismatch(priv, port, state->interface)) { 1289ec8582d1SVladimir Oltean dev_err(ds->dev, "Changing PHY mode to %s not supported!\n", 1290ec8582d1SVladimir Oltean phy_modes(state->interface)); 129139710229SVladimir Oltean return; 1292ec8582d1SVladimir Oltean } 129339710229SVladimir Oltean 12943ad1d171SVladimir Oltean xpcs = priv->xpcs[port]; 1295ffe10e67SVladimir Oltean 12963ad1d171SVladimir Oltean if (xpcs) 12973ad1d171SVladimir Oltean phylink_set_pcs(dp->pl, &xpcs->pcs); 12988400cff6SVladimir Oltean } 12998400cff6SVladimir Oltean 13008400cff6SVladimir Oltean static void sja1105_mac_link_down(struct dsa_switch *ds, int port, 13018400cff6SVladimir Oltean unsigned int mode, 13028400cff6SVladimir Oltean phy_interface_t interface) 13038400cff6SVladimir Oltean { 13048400cff6SVladimir Oltean sja1105_inhibit_tx(ds->priv, BIT(port), true); 13058400cff6SVladimir Oltean } 13068400cff6SVladimir Oltean 13078400cff6SVladimir Oltean static void sja1105_mac_link_up(struct dsa_switch *ds, int port, 13088400cff6SVladimir Oltean unsigned int mode, 13098400cff6SVladimir Oltean phy_interface_t interface, 13105b502a7bSRussell King struct phy_device *phydev, 13115b502a7bSRussell King int speed, int duplex, 13125b502a7bSRussell King bool tx_pause, bool rx_pause) 13138400cff6SVladimir Oltean { 1314ec8582d1SVladimir Oltean struct sja1105_private *priv = ds->priv; 1315ec8582d1SVladimir Oltean 1316ec8582d1SVladimir Oltean sja1105_adjust_port_config(priv, port, speed); 1317ec8582d1SVladimir Oltean 1318ec8582d1SVladimir Oltean sja1105_inhibit_tx(priv, BIT(port), false); 13198aa9ebccSVladimir Oltean } 13208aa9ebccSVladimir Oltean 1321ad9f299aSVladimir Oltean static void sja1105_phylink_validate(struct dsa_switch *ds, int port, 1322ad9f299aSVladimir Oltean unsigned long *supported, 1323ad9f299aSVladimir Oltean struct phylink_link_state *state) 1324ad9f299aSVladimir Oltean { 1325ad9f299aSVladimir Oltean /* Construct a new mask which exhaustively contains all link features 1326ad9f299aSVladimir Oltean * supported by the MAC, and then apply that (logical AND) to what will 1327ad9f299aSVladimir Oltean * be sent to the PHY for "marketing". 1328ad9f299aSVladimir Oltean */ 1329ad9f299aSVladimir Oltean __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, }; 1330ad9f299aSVladimir Oltean struct sja1105_private *priv = ds->priv; 1331ad9f299aSVladimir Oltean struct sja1105_xmii_params_entry *mii; 1332ad9f299aSVladimir Oltean 1333ad9f299aSVladimir Oltean mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries; 1334ad9f299aSVladimir Oltean 133539710229SVladimir Oltean /* include/linux/phylink.h says: 133639710229SVladimir Oltean * When @state->interface is %PHY_INTERFACE_MODE_NA, phylink 133739710229SVladimir Oltean * expects the MAC driver to return all supported link modes. 133839710229SVladimir Oltean */ 133939710229SVladimir Oltean if (state->interface != PHY_INTERFACE_MODE_NA && 134039710229SVladimir Oltean sja1105_phy_mode_mismatch(priv, port, state->interface)) { 134139710229SVladimir Oltean bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS); 134239710229SVladimir Oltean return; 134339710229SVladimir Oltean } 134439710229SVladimir Oltean 1345ad9f299aSVladimir Oltean /* The MAC does not support pause frames, and also doesn't 1346ad9f299aSVladimir Oltean * support half-duplex traffic modes. 1347ad9f299aSVladimir Oltean */ 1348ad9f299aSVladimir Oltean phylink_set(mask, Autoneg); 1349ad9f299aSVladimir Oltean phylink_set(mask, MII); 1350ad9f299aSVladimir Oltean phylink_set(mask, 10baseT_Full); 1351ad9f299aSVladimir Oltean phylink_set(mask, 100baseT_Full); 1352ca68e138SOleksij Rempel phylink_set(mask, 100baseT1_Full); 1353ffe10e67SVladimir Oltean if (mii->xmii_mode[port] == XMII_MODE_RGMII || 1354ffe10e67SVladimir Oltean mii->xmii_mode[port] == XMII_MODE_SGMII) 1355ad9f299aSVladimir Oltean phylink_set(mask, 1000baseT_Full); 135656b63466SVladimir Oltean if (priv->info->supports_2500basex[port]) { 135756b63466SVladimir Oltean phylink_set(mask, 2500baseT_Full); 135856b63466SVladimir Oltean phylink_set(mask, 2500baseX_Full); 135956b63466SVladimir Oltean } 1360ad9f299aSVladimir Oltean 1361ad9f299aSVladimir Oltean bitmap_and(supported, supported, mask, __ETHTOOL_LINK_MODE_MASK_NBITS); 1362ad9f299aSVladimir Oltean bitmap_and(state->advertising, state->advertising, mask, 1363ad9f299aSVladimir Oltean __ETHTOOL_LINK_MODE_MASK_NBITS); 1364ad9f299aSVladimir Oltean } 1365ad9f299aSVladimir Oltean 136660f6053fSVladimir Oltean static int 136760f6053fSVladimir Oltean sja1105_find_static_fdb_entry(struct sja1105_private *priv, int port, 136860f6053fSVladimir Oltean const struct sja1105_l2_lookup_entry *requested) 136960f6053fSVladimir Oltean { 137060f6053fSVladimir Oltean struct sja1105_l2_lookup_entry *l2_lookup; 137160f6053fSVladimir Oltean struct sja1105_table *table; 137260f6053fSVladimir Oltean int i; 137360f6053fSVladimir Oltean 137460f6053fSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; 137560f6053fSVladimir Oltean l2_lookup = table->entries; 137660f6053fSVladimir Oltean 137760f6053fSVladimir Oltean for (i = 0; i < table->entry_count; i++) 137860f6053fSVladimir Oltean if (l2_lookup[i].macaddr == requested->macaddr && 137960f6053fSVladimir Oltean l2_lookup[i].vlanid == requested->vlanid && 138060f6053fSVladimir Oltean l2_lookup[i].destports & BIT(port)) 138160f6053fSVladimir Oltean return i; 138260f6053fSVladimir Oltean 138360f6053fSVladimir Oltean return -1; 138460f6053fSVladimir Oltean } 138560f6053fSVladimir Oltean 138660f6053fSVladimir Oltean /* We want FDB entries added statically through the bridge command to persist 138760f6053fSVladimir Oltean * across switch resets, which are a common thing during normal SJA1105 138860f6053fSVladimir Oltean * operation. So we have to back them up in the static configuration tables 138960f6053fSVladimir Oltean * and hence apply them on next static config upload... yay! 139060f6053fSVladimir Oltean */ 139160f6053fSVladimir Oltean static int 139260f6053fSVladimir Oltean sja1105_static_fdb_change(struct sja1105_private *priv, int port, 139360f6053fSVladimir Oltean const struct sja1105_l2_lookup_entry *requested, 139460f6053fSVladimir Oltean bool keep) 139560f6053fSVladimir Oltean { 139660f6053fSVladimir Oltean struct sja1105_l2_lookup_entry *l2_lookup; 139760f6053fSVladimir Oltean struct sja1105_table *table; 139860f6053fSVladimir Oltean int rc, match; 139960f6053fSVladimir Oltean 140060f6053fSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; 140160f6053fSVladimir Oltean 140260f6053fSVladimir Oltean match = sja1105_find_static_fdb_entry(priv, port, requested); 140360f6053fSVladimir Oltean if (match < 0) { 140460f6053fSVladimir Oltean /* Can't delete a missing entry. */ 140560f6053fSVladimir Oltean if (!keep) 140660f6053fSVladimir Oltean return 0; 140760f6053fSVladimir Oltean 140860f6053fSVladimir Oltean /* No match => new entry */ 140960f6053fSVladimir Oltean rc = sja1105_table_resize(table, table->entry_count + 1); 141060f6053fSVladimir Oltean if (rc) 141160f6053fSVladimir Oltean return rc; 141260f6053fSVladimir Oltean 141360f6053fSVladimir Oltean match = table->entry_count - 1; 141460f6053fSVladimir Oltean } 141560f6053fSVladimir Oltean 141660f6053fSVladimir Oltean /* Assign pointer after the resize (it may be new memory) */ 141760f6053fSVladimir Oltean l2_lookup = table->entries; 141860f6053fSVladimir Oltean 141960f6053fSVladimir Oltean /* We have a match. 142060f6053fSVladimir Oltean * If the job was to add this FDB entry, it's already done (mostly 142160f6053fSVladimir Oltean * anyway, since the port forwarding mask may have changed, case in 142260f6053fSVladimir Oltean * which we update it). 142360f6053fSVladimir Oltean * Otherwise we have to delete it. 142460f6053fSVladimir Oltean */ 142560f6053fSVladimir Oltean if (keep) { 142660f6053fSVladimir Oltean l2_lookup[match] = *requested; 142760f6053fSVladimir Oltean return 0; 142860f6053fSVladimir Oltean } 142960f6053fSVladimir Oltean 143060f6053fSVladimir Oltean /* To remove, the strategy is to overwrite the element with 143160f6053fSVladimir Oltean * the last one, and then reduce the array size by 1 143260f6053fSVladimir Oltean */ 143360f6053fSVladimir Oltean l2_lookup[match] = l2_lookup[table->entry_count - 1]; 143460f6053fSVladimir Oltean return sja1105_table_resize(table, table->entry_count - 1); 143560f6053fSVladimir Oltean } 143660f6053fSVladimir Oltean 1437291d1e72SVladimir Oltean /* First-generation switches have a 4-way set associative TCAM that 1438291d1e72SVladimir Oltean * holds the FDB entries. An FDB index spans from 0 to 1023 and is comprised of 1439291d1e72SVladimir Oltean * a "bin" (grouping of 4 entries) and a "way" (an entry within a bin). 1440291d1e72SVladimir Oltean * For the placement of a newly learnt FDB entry, the switch selects the bin 1441291d1e72SVladimir Oltean * based on a hash function, and the way within that bin incrementally. 1442291d1e72SVladimir Oltean */ 144309c1b412SVladimir Oltean static int sja1105et_fdb_index(int bin, int way) 1444291d1e72SVladimir Oltean { 1445291d1e72SVladimir Oltean return bin * SJA1105ET_FDB_BIN_SIZE + way; 1446291d1e72SVladimir Oltean } 1447291d1e72SVladimir Oltean 14489dfa6911SVladimir Oltean static int sja1105et_is_fdb_entry_in_bin(struct sja1105_private *priv, int bin, 1449291d1e72SVladimir Oltean const u8 *addr, u16 vid, 1450291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry *match, 1451291d1e72SVladimir Oltean int *last_unused) 1452291d1e72SVladimir Oltean { 1453291d1e72SVladimir Oltean int way; 1454291d1e72SVladimir Oltean 1455291d1e72SVladimir Oltean for (way = 0; way < SJA1105ET_FDB_BIN_SIZE; way++) { 1456291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 1457291d1e72SVladimir Oltean int index = sja1105et_fdb_index(bin, way); 1458291d1e72SVladimir Oltean 1459291d1e72SVladimir Oltean /* Skip unused entries, optionally marking them 1460291d1e72SVladimir Oltean * into the return value 1461291d1e72SVladimir Oltean */ 1462291d1e72SVladimir Oltean if (sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 1463291d1e72SVladimir Oltean index, &l2_lookup)) { 1464291d1e72SVladimir Oltean if (last_unused) 1465291d1e72SVladimir Oltean *last_unused = way; 1466291d1e72SVladimir Oltean continue; 1467291d1e72SVladimir Oltean } 1468291d1e72SVladimir Oltean 1469291d1e72SVladimir Oltean if (l2_lookup.macaddr == ether_addr_to_u64(addr) && 1470291d1e72SVladimir Oltean l2_lookup.vlanid == vid) { 1471291d1e72SVladimir Oltean if (match) 1472291d1e72SVladimir Oltean *match = l2_lookup; 1473291d1e72SVladimir Oltean return way; 1474291d1e72SVladimir Oltean } 1475291d1e72SVladimir Oltean } 1476291d1e72SVladimir Oltean /* Return an invalid entry index if not found */ 1477291d1e72SVladimir Oltean return -1; 1478291d1e72SVladimir Oltean } 1479291d1e72SVladimir Oltean 14809dfa6911SVladimir Oltean int sja1105et_fdb_add(struct dsa_switch *ds, int port, 1481291d1e72SVladimir Oltean const unsigned char *addr, u16 vid) 1482291d1e72SVladimir Oltean { 1483291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 1484291d1e72SVladimir Oltean struct sja1105_private *priv = ds->priv; 1485291d1e72SVladimir Oltean struct device *dev = ds->dev; 1486291d1e72SVladimir Oltean int last_unused = -1; 148760f6053fSVladimir Oltean int bin, way, rc; 1488291d1e72SVladimir Oltean 14899dfa6911SVladimir Oltean bin = sja1105et_fdb_hash(priv, addr, vid); 1490291d1e72SVladimir Oltean 14919dfa6911SVladimir Oltean way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid, 1492291d1e72SVladimir Oltean &l2_lookup, &last_unused); 1493291d1e72SVladimir Oltean if (way >= 0) { 1494291d1e72SVladimir Oltean /* We have an FDB entry. Is our port in the destination 1495291d1e72SVladimir Oltean * mask? If yes, we need to do nothing. If not, we need 1496291d1e72SVladimir Oltean * to rewrite the entry by adding this port to it. 1497291d1e72SVladimir Oltean */ 1498291d1e72SVladimir Oltean if (l2_lookup.destports & BIT(port)) 1499291d1e72SVladimir Oltean return 0; 1500291d1e72SVladimir Oltean l2_lookup.destports |= BIT(port); 1501291d1e72SVladimir Oltean } else { 1502291d1e72SVladimir Oltean int index = sja1105et_fdb_index(bin, way); 1503291d1e72SVladimir Oltean 1504291d1e72SVladimir Oltean /* We don't have an FDB entry. We construct a new one and 1505291d1e72SVladimir Oltean * try to find a place for it within the FDB table. 1506291d1e72SVladimir Oltean */ 1507291d1e72SVladimir Oltean l2_lookup.macaddr = ether_addr_to_u64(addr); 1508291d1e72SVladimir Oltean l2_lookup.destports = BIT(port); 1509291d1e72SVladimir Oltean l2_lookup.vlanid = vid; 1510291d1e72SVladimir Oltean 1511291d1e72SVladimir Oltean if (last_unused >= 0) { 1512291d1e72SVladimir Oltean way = last_unused; 1513291d1e72SVladimir Oltean } else { 1514291d1e72SVladimir Oltean /* Bin is full, need to evict somebody. 1515291d1e72SVladimir Oltean * Choose victim at random. If you get these messages 1516291d1e72SVladimir Oltean * often, you may need to consider changing the 1517291d1e72SVladimir Oltean * distribution function: 1518291d1e72SVladimir Oltean * static_config[BLK_IDX_L2_LOOKUP_PARAMS].entries->poly 1519291d1e72SVladimir Oltean */ 1520291d1e72SVladimir Oltean get_random_bytes(&way, sizeof(u8)); 1521291d1e72SVladimir Oltean way %= SJA1105ET_FDB_BIN_SIZE; 1522291d1e72SVladimir Oltean dev_warn(dev, "Warning, FDB bin %d full while adding entry for %pM. Evicting entry %u.\n", 1523291d1e72SVladimir Oltean bin, addr, way); 1524291d1e72SVladimir Oltean /* Evict entry */ 1525291d1e72SVladimir Oltean sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 1526291d1e72SVladimir Oltean index, NULL, false); 1527291d1e72SVladimir Oltean } 1528291d1e72SVladimir Oltean } 1529291d1e72SVladimir Oltean l2_lookup.index = sja1105et_fdb_index(bin, way); 1530291d1e72SVladimir Oltean 153160f6053fSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 1532291d1e72SVladimir Oltean l2_lookup.index, &l2_lookup, 1533291d1e72SVladimir Oltean true); 153460f6053fSVladimir Oltean if (rc < 0) 153560f6053fSVladimir Oltean return rc; 153660f6053fSVladimir Oltean 153760f6053fSVladimir Oltean return sja1105_static_fdb_change(priv, port, &l2_lookup, true); 1538291d1e72SVladimir Oltean } 1539291d1e72SVladimir Oltean 15409dfa6911SVladimir Oltean int sja1105et_fdb_del(struct dsa_switch *ds, int port, 1541291d1e72SVladimir Oltean const unsigned char *addr, u16 vid) 1542291d1e72SVladimir Oltean { 1543291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 1544291d1e72SVladimir Oltean struct sja1105_private *priv = ds->priv; 154560f6053fSVladimir Oltean int index, bin, way, rc; 1546291d1e72SVladimir Oltean bool keep; 1547291d1e72SVladimir Oltean 15489dfa6911SVladimir Oltean bin = sja1105et_fdb_hash(priv, addr, vid); 15499dfa6911SVladimir Oltean way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid, 1550291d1e72SVladimir Oltean &l2_lookup, NULL); 1551291d1e72SVladimir Oltean if (way < 0) 1552291d1e72SVladimir Oltean return 0; 1553291d1e72SVladimir Oltean index = sja1105et_fdb_index(bin, way); 1554291d1e72SVladimir Oltean 1555291d1e72SVladimir Oltean /* We have an FDB entry. Is our port in the destination mask? If yes, 1556291d1e72SVladimir Oltean * we need to remove it. If the resulting port mask becomes empty, we 1557291d1e72SVladimir Oltean * need to completely evict the FDB entry. 1558291d1e72SVladimir Oltean * Otherwise we just write it back. 1559291d1e72SVladimir Oltean */ 1560291d1e72SVladimir Oltean l2_lookup.destports &= ~BIT(port); 15617752e937SVladimir Oltean 1562291d1e72SVladimir Oltean if (l2_lookup.destports) 1563291d1e72SVladimir Oltean keep = true; 1564291d1e72SVladimir Oltean else 1565291d1e72SVladimir Oltean keep = false; 1566291d1e72SVladimir Oltean 156760f6053fSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 1568291d1e72SVladimir Oltean index, &l2_lookup, keep); 156960f6053fSVladimir Oltean if (rc < 0) 157060f6053fSVladimir Oltean return rc; 157160f6053fSVladimir Oltean 157260f6053fSVladimir Oltean return sja1105_static_fdb_change(priv, port, &l2_lookup, keep); 1573291d1e72SVladimir Oltean } 1574291d1e72SVladimir Oltean 15759dfa6911SVladimir Oltean int sja1105pqrs_fdb_add(struct dsa_switch *ds, int port, 15769dfa6911SVladimir Oltean const unsigned char *addr, u16 vid) 15779dfa6911SVladimir Oltean { 15781da73821SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 15791da73821SVladimir Oltean struct sja1105_private *priv = ds->priv; 15801da73821SVladimir Oltean int rc, i; 15811da73821SVladimir Oltean 15821da73821SVladimir Oltean /* Search for an existing entry in the FDB table */ 15831da73821SVladimir Oltean l2_lookup.macaddr = ether_addr_to_u64(addr); 15841da73821SVladimir Oltean l2_lookup.vlanid = vid; 15851da73821SVladimir Oltean l2_lookup.iotag = SJA1105_S_TAG; 15861da73821SVladimir Oltean l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0); 15870fac6aa0SVladimir Oltean if (priv->vlan_aware) { 15881da73821SVladimir Oltean l2_lookup.mask_vlanid = VLAN_VID_MASK; 15891da73821SVladimir Oltean l2_lookup.mask_iotag = BIT(0); 15906d7c7d94SVladimir Oltean } else { 15916d7c7d94SVladimir Oltean l2_lookup.mask_vlanid = 0; 15926d7c7d94SVladimir Oltean l2_lookup.mask_iotag = 0; 15936d7c7d94SVladimir Oltean } 15941da73821SVladimir Oltean l2_lookup.destports = BIT(port); 15951da73821SVladimir Oltean 15961da73821SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 15971da73821SVladimir Oltean SJA1105_SEARCH, &l2_lookup); 15981da73821SVladimir Oltean if (rc == 0) { 15991da73821SVladimir Oltean /* Found and this port is already in the entry's 16001da73821SVladimir Oltean * port mask => job done 16011da73821SVladimir Oltean */ 16021da73821SVladimir Oltean if (l2_lookup.destports & BIT(port)) 16031da73821SVladimir Oltean return 0; 16041da73821SVladimir Oltean /* l2_lookup.index is populated by the switch in case it 16051da73821SVladimir Oltean * found something. 16061da73821SVladimir Oltean */ 16071da73821SVladimir Oltean l2_lookup.destports |= BIT(port); 16081da73821SVladimir Oltean goto skip_finding_an_index; 16091da73821SVladimir Oltean } 16101da73821SVladimir Oltean 16111da73821SVladimir Oltean /* Not found, so try to find an unused spot in the FDB. 16121da73821SVladimir Oltean * This is slightly inefficient because the strategy is knock-knock at 16131da73821SVladimir Oltean * every possible position from 0 to 1023. 16141da73821SVladimir Oltean */ 16151da73821SVladimir Oltean for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) { 16161da73821SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 16171da73821SVladimir Oltean i, NULL); 16181da73821SVladimir Oltean if (rc < 0) 16191da73821SVladimir Oltean break; 16201da73821SVladimir Oltean } 16211da73821SVladimir Oltean if (i == SJA1105_MAX_L2_LOOKUP_COUNT) { 16221da73821SVladimir Oltean dev_err(ds->dev, "FDB is full, cannot add entry.\n"); 16231da73821SVladimir Oltean return -EINVAL; 16241da73821SVladimir Oltean } 162517ae6555SVladimir Oltean l2_lookup.lockeds = true; 16261da73821SVladimir Oltean l2_lookup.index = i; 16271da73821SVladimir Oltean 16281da73821SVladimir Oltean skip_finding_an_index: 162960f6053fSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 16301da73821SVladimir Oltean l2_lookup.index, &l2_lookup, 16311da73821SVladimir Oltean true); 163260f6053fSVladimir Oltean if (rc < 0) 163360f6053fSVladimir Oltean return rc; 163460f6053fSVladimir Oltean 163560f6053fSVladimir Oltean return sja1105_static_fdb_change(priv, port, &l2_lookup, true); 16369dfa6911SVladimir Oltean } 16379dfa6911SVladimir Oltean 16389dfa6911SVladimir Oltean int sja1105pqrs_fdb_del(struct dsa_switch *ds, int port, 16399dfa6911SVladimir Oltean const unsigned char *addr, u16 vid) 16409dfa6911SVladimir Oltean { 16411da73821SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 16421da73821SVladimir Oltean struct sja1105_private *priv = ds->priv; 16431da73821SVladimir Oltean bool keep; 16441da73821SVladimir Oltean int rc; 16451da73821SVladimir Oltean 16461da73821SVladimir Oltean l2_lookup.macaddr = ether_addr_to_u64(addr); 16471da73821SVladimir Oltean l2_lookup.vlanid = vid; 16481da73821SVladimir Oltean l2_lookup.iotag = SJA1105_S_TAG; 16491da73821SVladimir Oltean l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0); 16500fac6aa0SVladimir Oltean if (priv->vlan_aware) { 16511da73821SVladimir Oltean l2_lookup.mask_vlanid = VLAN_VID_MASK; 16521da73821SVladimir Oltean l2_lookup.mask_iotag = BIT(0); 16536d7c7d94SVladimir Oltean } else { 16546d7c7d94SVladimir Oltean l2_lookup.mask_vlanid = 0; 16556d7c7d94SVladimir Oltean l2_lookup.mask_iotag = 0; 16566d7c7d94SVladimir Oltean } 16571da73821SVladimir Oltean l2_lookup.destports = BIT(port); 16581da73821SVladimir Oltean 16591da73821SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 16601da73821SVladimir Oltean SJA1105_SEARCH, &l2_lookup); 16611da73821SVladimir Oltean if (rc < 0) 16621da73821SVladimir Oltean return 0; 16631da73821SVladimir Oltean 16641da73821SVladimir Oltean l2_lookup.destports &= ~BIT(port); 16651da73821SVladimir Oltean 16661da73821SVladimir Oltean /* Decide whether we remove just this port from the FDB entry, 16671da73821SVladimir Oltean * or if we remove it completely. 16681da73821SVladimir Oltean */ 16691da73821SVladimir Oltean if (l2_lookup.destports) 16701da73821SVladimir Oltean keep = true; 16711da73821SVladimir Oltean else 16721da73821SVladimir Oltean keep = false; 16731da73821SVladimir Oltean 167460f6053fSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 16751da73821SVladimir Oltean l2_lookup.index, &l2_lookup, keep); 167660f6053fSVladimir Oltean if (rc < 0) 167760f6053fSVladimir Oltean return rc; 167860f6053fSVladimir Oltean 167960f6053fSVladimir Oltean return sja1105_static_fdb_change(priv, port, &l2_lookup, keep); 16809dfa6911SVladimir Oltean } 16819dfa6911SVladimir Oltean 16829dfa6911SVladimir Oltean static int sja1105_fdb_add(struct dsa_switch *ds, int port, 16839dfa6911SVladimir Oltean const unsigned char *addr, u16 vid) 16849dfa6911SVladimir Oltean { 16859dfa6911SVladimir Oltean struct sja1105_private *priv = ds->priv; 1686b3ee526aSVladimir Oltean 16876d7c7d94SVladimir Oltean return priv->info->fdb_add_cmd(ds, port, addr, vid); 16889dfa6911SVladimir Oltean } 16899dfa6911SVladimir Oltean 16909dfa6911SVladimir Oltean static int sja1105_fdb_del(struct dsa_switch *ds, int port, 16919dfa6911SVladimir Oltean const unsigned char *addr, u16 vid) 16929dfa6911SVladimir Oltean { 16939dfa6911SVladimir Oltean struct sja1105_private *priv = ds->priv; 16949dfa6911SVladimir Oltean 1695b3ee526aSVladimir Oltean return priv->info->fdb_del_cmd(ds, port, addr, vid); 16969dfa6911SVladimir Oltean } 16979dfa6911SVladimir Oltean 1698291d1e72SVladimir Oltean static int sja1105_fdb_dump(struct dsa_switch *ds, int port, 1699291d1e72SVladimir Oltean dsa_fdb_dump_cb_t *cb, void *data) 1700291d1e72SVladimir Oltean { 1701291d1e72SVladimir Oltean struct sja1105_private *priv = ds->priv; 1702291d1e72SVladimir Oltean struct device *dev = ds->dev; 1703291d1e72SVladimir Oltean int i; 1704291d1e72SVladimir Oltean 1705291d1e72SVladimir Oltean for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) { 1706291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 1707291d1e72SVladimir Oltean u8 macaddr[ETH_ALEN]; 1708291d1e72SVladimir Oltean int rc; 1709291d1e72SVladimir Oltean 1710291d1e72SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 1711291d1e72SVladimir Oltean i, &l2_lookup); 1712291d1e72SVladimir Oltean /* No fdb entry at i, not an issue */ 1713def84604SVladimir Oltean if (rc == -ENOENT) 1714291d1e72SVladimir Oltean continue; 1715291d1e72SVladimir Oltean if (rc) { 1716291d1e72SVladimir Oltean dev_err(dev, "Failed to dump FDB: %d\n", rc); 1717291d1e72SVladimir Oltean return rc; 1718291d1e72SVladimir Oltean } 1719291d1e72SVladimir Oltean 1720291d1e72SVladimir Oltean /* FDB dump callback is per port. This means we have to 1721291d1e72SVladimir Oltean * disregard a valid entry if it's not for this port, even if 1722291d1e72SVladimir Oltean * only to revisit it later. This is inefficient because the 1723291d1e72SVladimir Oltean * 1024-sized FDB table needs to be traversed 4 times through 1724291d1e72SVladimir Oltean * SPI during a 'bridge fdb show' command. 1725291d1e72SVladimir Oltean */ 1726291d1e72SVladimir Oltean if (!(l2_lookup.destports & BIT(port))) 1727291d1e72SVladimir Oltean continue; 17284d942354SVladimir Oltean 17294d942354SVladimir Oltean /* We need to hide the FDB entry for unknown multicast */ 17304d942354SVladimir Oltean if (l2_lookup.macaddr == SJA1105_UNKNOWN_MULTICAST && 17314d942354SVladimir Oltean l2_lookup.mask_macaddr == SJA1105_UNKNOWN_MULTICAST) 17324d942354SVladimir Oltean continue; 17334d942354SVladimir Oltean 1734291d1e72SVladimir Oltean u64_to_ether_addr(l2_lookup.macaddr, macaddr); 173593647594SVladimir Oltean 17366d7c7d94SVladimir Oltean /* We need to hide the dsa_8021q VLANs from the user. */ 17370fac6aa0SVladimir Oltean if (!priv->vlan_aware) 17386d7c7d94SVladimir Oltean l2_lookup.vlanid = 0; 173917ae6555SVladimir Oltean cb(macaddr, l2_lookup.vlanid, l2_lookup.lockeds, data); 1740291d1e72SVladimir Oltean } 1741291d1e72SVladimir Oltean return 0; 1742291d1e72SVladimir Oltean } 1743291d1e72SVladimir Oltean 1744a52b2da7SVladimir Oltean static int sja1105_mdb_add(struct dsa_switch *ds, int port, 1745291d1e72SVladimir Oltean const struct switchdev_obj_port_mdb *mdb) 1746291d1e72SVladimir Oltean { 1747a52b2da7SVladimir Oltean return sja1105_fdb_add(ds, port, mdb->addr, mdb->vid); 1748291d1e72SVladimir Oltean } 1749291d1e72SVladimir Oltean 1750291d1e72SVladimir Oltean static int sja1105_mdb_del(struct dsa_switch *ds, int port, 1751291d1e72SVladimir Oltean const struct switchdev_obj_port_mdb *mdb) 1752291d1e72SVladimir Oltean { 1753291d1e72SVladimir Oltean return sja1105_fdb_del(ds, port, mdb->addr, mdb->vid); 1754291d1e72SVladimir Oltean } 1755291d1e72SVladimir Oltean 17567f7ccdeaSVladimir Oltean /* Common function for unicast and broadcast flood configuration. 17577f7ccdeaSVladimir Oltean * Flooding is configured between each {ingress, egress} port pair, and since 17587f7ccdeaSVladimir Oltean * the bridge's semantics are those of "egress flooding", it means we must 17597f7ccdeaSVladimir Oltean * enable flooding towards this port from all ingress ports that are in the 17607f7ccdeaSVladimir Oltean * same forwarding domain. 17617f7ccdeaSVladimir Oltean */ 17627f7ccdeaSVladimir Oltean static int sja1105_manage_flood_domains(struct sja1105_private *priv) 17637f7ccdeaSVladimir Oltean { 17647f7ccdeaSVladimir Oltean struct sja1105_l2_forwarding_entry *l2_fwd; 17657f7ccdeaSVladimir Oltean struct dsa_switch *ds = priv->ds; 17667f7ccdeaSVladimir Oltean int from, to, rc; 17677f7ccdeaSVladimir Oltean 17687f7ccdeaSVladimir Oltean l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries; 17697f7ccdeaSVladimir Oltean 17707f7ccdeaSVladimir Oltean for (from = 0; from < ds->num_ports; from++) { 17717f7ccdeaSVladimir Oltean u64 fl_domain = 0, bc_domain = 0; 17727f7ccdeaSVladimir Oltean 17737f7ccdeaSVladimir Oltean for (to = 0; to < priv->ds->num_ports; to++) { 17747f7ccdeaSVladimir Oltean if (!sja1105_can_forward(l2_fwd, from, to)) 17757f7ccdeaSVladimir Oltean continue; 17767f7ccdeaSVladimir Oltean 17777f7ccdeaSVladimir Oltean if (priv->ucast_egress_floods & BIT(to)) 17787f7ccdeaSVladimir Oltean fl_domain |= BIT(to); 17797f7ccdeaSVladimir Oltean if (priv->bcast_egress_floods & BIT(to)) 17807f7ccdeaSVladimir Oltean bc_domain |= BIT(to); 17817f7ccdeaSVladimir Oltean } 17827f7ccdeaSVladimir Oltean 17837f7ccdeaSVladimir Oltean /* Nothing changed, nothing to do */ 17847f7ccdeaSVladimir Oltean if (l2_fwd[from].fl_domain == fl_domain && 17857f7ccdeaSVladimir Oltean l2_fwd[from].bc_domain == bc_domain) 17867f7ccdeaSVladimir Oltean continue; 17877f7ccdeaSVladimir Oltean 17887f7ccdeaSVladimir Oltean l2_fwd[from].fl_domain = fl_domain; 17897f7ccdeaSVladimir Oltean l2_fwd[from].bc_domain = bc_domain; 17907f7ccdeaSVladimir Oltean 17917f7ccdeaSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING, 17927f7ccdeaSVladimir Oltean from, &l2_fwd[from], true); 17937f7ccdeaSVladimir Oltean if (rc < 0) 17947f7ccdeaSVladimir Oltean return rc; 17957f7ccdeaSVladimir Oltean } 17967f7ccdeaSVladimir Oltean 17977f7ccdeaSVladimir Oltean return 0; 17987f7ccdeaSVladimir Oltean } 17997f7ccdeaSVladimir Oltean 18008aa9ebccSVladimir Oltean static int sja1105_bridge_member(struct dsa_switch *ds, int port, 18018aa9ebccSVladimir Oltean struct net_device *br, bool member) 18028aa9ebccSVladimir Oltean { 18038aa9ebccSVladimir Oltean struct sja1105_l2_forwarding_entry *l2_fwd; 18048aa9ebccSVladimir Oltean struct sja1105_private *priv = ds->priv; 18058aa9ebccSVladimir Oltean int i, rc; 18068aa9ebccSVladimir Oltean 18078aa9ebccSVladimir Oltean l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries; 18088aa9ebccSVladimir Oltean 1809542043e9SVladimir Oltean for (i = 0; i < ds->num_ports; i++) { 18108aa9ebccSVladimir Oltean /* Add this port to the forwarding matrix of the 18118aa9ebccSVladimir Oltean * other ports in the same bridge, and viceversa. 18128aa9ebccSVladimir Oltean */ 18138aa9ebccSVladimir Oltean if (!dsa_is_user_port(ds, i)) 18148aa9ebccSVladimir Oltean continue; 18158aa9ebccSVladimir Oltean /* For the ports already under the bridge, only one thing needs 18168aa9ebccSVladimir Oltean * to be done, and that is to add this port to their 18178aa9ebccSVladimir Oltean * reachability domain. So we can perform the SPI write for 18188aa9ebccSVladimir Oltean * them immediately. However, for this port itself (the one 18198aa9ebccSVladimir Oltean * that is new to the bridge), we need to add all other ports 18208aa9ebccSVladimir Oltean * to its reachability domain. So we do that incrementally in 18218aa9ebccSVladimir Oltean * this loop, and perform the SPI write only at the end, once 18228aa9ebccSVladimir Oltean * the domain contains all other bridge ports. 18238aa9ebccSVladimir Oltean */ 18248aa9ebccSVladimir Oltean if (i == port) 18258aa9ebccSVladimir Oltean continue; 18268aa9ebccSVladimir Oltean if (dsa_to_port(ds, i)->bridge_dev != br) 18278aa9ebccSVladimir Oltean continue; 18288aa9ebccSVladimir Oltean sja1105_port_allow_traffic(l2_fwd, i, port, member); 18298aa9ebccSVladimir Oltean sja1105_port_allow_traffic(l2_fwd, port, i, member); 18308aa9ebccSVladimir Oltean 18318aa9ebccSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING, 18328aa9ebccSVladimir Oltean i, &l2_fwd[i], true); 18338aa9ebccSVladimir Oltean if (rc < 0) 18348aa9ebccSVladimir Oltean return rc; 18358aa9ebccSVladimir Oltean } 18368aa9ebccSVladimir Oltean 18377f7ccdeaSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING, 18388aa9ebccSVladimir Oltean port, &l2_fwd[port], true); 18397f7ccdeaSVladimir Oltean if (rc) 18407f7ccdeaSVladimir Oltean return rc; 18417f7ccdeaSVladimir Oltean 1842cde8078eSVladimir Oltean rc = sja1105_commit_pvid(ds, port); 1843cde8078eSVladimir Oltean if (rc) 1844cde8078eSVladimir Oltean return rc; 1845cde8078eSVladimir Oltean 18467f7ccdeaSVladimir Oltean return sja1105_manage_flood_domains(priv); 18478aa9ebccSVladimir Oltean } 18488aa9ebccSVladimir Oltean 1849640f763fSVladimir Oltean static void sja1105_bridge_stp_state_set(struct dsa_switch *ds, int port, 1850640f763fSVladimir Oltean u8 state) 1851640f763fSVladimir Oltean { 1852640f763fSVladimir Oltean struct sja1105_private *priv = ds->priv; 1853640f763fSVladimir Oltean struct sja1105_mac_config_entry *mac; 1854640f763fSVladimir Oltean 1855640f763fSVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 1856640f763fSVladimir Oltean 1857640f763fSVladimir Oltean switch (state) { 1858640f763fSVladimir Oltean case BR_STATE_DISABLED: 1859640f763fSVladimir Oltean case BR_STATE_BLOCKING: 1860640f763fSVladimir Oltean /* From UM10944 description of DRPDTAG (why put this there?): 1861640f763fSVladimir Oltean * "Management traffic flows to the port regardless of the state 1862640f763fSVladimir Oltean * of the INGRESS flag". So BPDUs are still be allowed to pass. 1863640f763fSVladimir Oltean * At the moment no difference between DISABLED and BLOCKING. 1864640f763fSVladimir Oltean */ 1865640f763fSVladimir Oltean mac[port].ingress = false; 1866640f763fSVladimir Oltean mac[port].egress = false; 1867640f763fSVladimir Oltean mac[port].dyn_learn = false; 1868640f763fSVladimir Oltean break; 1869640f763fSVladimir Oltean case BR_STATE_LISTENING: 1870640f763fSVladimir Oltean mac[port].ingress = true; 1871640f763fSVladimir Oltean mac[port].egress = false; 1872640f763fSVladimir Oltean mac[port].dyn_learn = false; 1873640f763fSVladimir Oltean break; 1874640f763fSVladimir Oltean case BR_STATE_LEARNING: 1875640f763fSVladimir Oltean mac[port].ingress = true; 1876640f763fSVladimir Oltean mac[port].egress = false; 18774d942354SVladimir Oltean mac[port].dyn_learn = !!(priv->learn_ena & BIT(port)); 1878640f763fSVladimir Oltean break; 1879640f763fSVladimir Oltean case BR_STATE_FORWARDING: 1880640f763fSVladimir Oltean mac[port].ingress = true; 1881640f763fSVladimir Oltean mac[port].egress = true; 18824d942354SVladimir Oltean mac[port].dyn_learn = !!(priv->learn_ena & BIT(port)); 1883640f763fSVladimir Oltean break; 1884640f763fSVladimir Oltean default: 1885640f763fSVladimir Oltean dev_err(ds->dev, "invalid STP state: %d\n", state); 1886640f763fSVladimir Oltean return; 1887640f763fSVladimir Oltean } 1888640f763fSVladimir Oltean 1889640f763fSVladimir Oltean sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, 1890640f763fSVladimir Oltean &mac[port], true); 1891640f763fSVladimir Oltean } 1892640f763fSVladimir Oltean 18938aa9ebccSVladimir Oltean static int sja1105_bridge_join(struct dsa_switch *ds, int port, 18948aa9ebccSVladimir Oltean struct net_device *br) 18958aa9ebccSVladimir Oltean { 18968aa9ebccSVladimir Oltean return sja1105_bridge_member(ds, port, br, true); 18978aa9ebccSVladimir Oltean } 18988aa9ebccSVladimir Oltean 18998aa9ebccSVladimir Oltean static void sja1105_bridge_leave(struct dsa_switch *ds, int port, 19008aa9ebccSVladimir Oltean struct net_device *br) 19018aa9ebccSVladimir Oltean { 19028aa9ebccSVladimir Oltean sja1105_bridge_member(ds, port, br, false); 19038aa9ebccSVladimir Oltean } 19048aa9ebccSVladimir Oltean 19054d752508SVladimir Oltean #define BYTES_PER_KBIT (1000LL / 8) 19064d752508SVladimir Oltean 19074d752508SVladimir Oltean static int sja1105_find_unused_cbs_shaper(struct sja1105_private *priv) 19084d752508SVladimir Oltean { 19094d752508SVladimir Oltean int i; 19104d752508SVladimir Oltean 19114d752508SVladimir Oltean for (i = 0; i < priv->info->num_cbs_shapers; i++) 19124d752508SVladimir Oltean if (!priv->cbs[i].idle_slope && !priv->cbs[i].send_slope) 19134d752508SVladimir Oltean return i; 19144d752508SVladimir Oltean 19154d752508SVladimir Oltean return -1; 19164d752508SVladimir Oltean } 19174d752508SVladimir Oltean 19184d752508SVladimir Oltean static int sja1105_delete_cbs_shaper(struct sja1105_private *priv, int port, 19194d752508SVladimir Oltean int prio) 19204d752508SVladimir Oltean { 19214d752508SVladimir Oltean int i; 19224d752508SVladimir Oltean 19234d752508SVladimir Oltean for (i = 0; i < priv->info->num_cbs_shapers; i++) { 19244d752508SVladimir Oltean struct sja1105_cbs_entry *cbs = &priv->cbs[i]; 19254d752508SVladimir Oltean 19264d752508SVladimir Oltean if (cbs->port == port && cbs->prio == prio) { 19274d752508SVladimir Oltean memset(cbs, 0, sizeof(*cbs)); 19284d752508SVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_CBS, 19294d752508SVladimir Oltean i, cbs, true); 19304d752508SVladimir Oltean } 19314d752508SVladimir Oltean } 19324d752508SVladimir Oltean 19334d752508SVladimir Oltean return 0; 19344d752508SVladimir Oltean } 19354d752508SVladimir Oltean 19364d752508SVladimir Oltean static int sja1105_setup_tc_cbs(struct dsa_switch *ds, int port, 19374d752508SVladimir Oltean struct tc_cbs_qopt_offload *offload) 19384d752508SVladimir Oltean { 19394d752508SVladimir Oltean struct sja1105_private *priv = ds->priv; 19404d752508SVladimir Oltean struct sja1105_cbs_entry *cbs; 19414d752508SVladimir Oltean int index; 19424d752508SVladimir Oltean 19434d752508SVladimir Oltean if (!offload->enable) 19444d752508SVladimir Oltean return sja1105_delete_cbs_shaper(priv, port, offload->queue); 19454d752508SVladimir Oltean 19464d752508SVladimir Oltean index = sja1105_find_unused_cbs_shaper(priv); 19474d752508SVladimir Oltean if (index < 0) 19484d752508SVladimir Oltean return -ENOSPC; 19494d752508SVladimir Oltean 19504d752508SVladimir Oltean cbs = &priv->cbs[index]; 19514d752508SVladimir Oltean cbs->port = port; 19524d752508SVladimir Oltean cbs->prio = offload->queue; 19534d752508SVladimir Oltean /* locredit and sendslope are negative by definition. In hardware, 19544d752508SVladimir Oltean * positive values must be provided, and the negative sign is implicit. 19554d752508SVladimir Oltean */ 19564d752508SVladimir Oltean cbs->credit_hi = offload->hicredit; 19574d752508SVladimir Oltean cbs->credit_lo = abs(offload->locredit); 19584d752508SVladimir Oltean /* User space is in kbits/sec, hardware in bytes/sec */ 19594d752508SVladimir Oltean cbs->idle_slope = offload->idleslope * BYTES_PER_KBIT; 19604d752508SVladimir Oltean cbs->send_slope = abs(offload->sendslope * BYTES_PER_KBIT); 19614d752508SVladimir Oltean /* Convert the negative values from 64-bit 2's complement 19624d752508SVladimir Oltean * to 32-bit 2's complement (for the case of 0x80000000 whose 19634d752508SVladimir Oltean * negative is still negative). 19644d752508SVladimir Oltean */ 19654d752508SVladimir Oltean cbs->credit_lo &= GENMASK_ULL(31, 0); 19664d752508SVladimir Oltean cbs->send_slope &= GENMASK_ULL(31, 0); 19674d752508SVladimir Oltean 19684d752508SVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_CBS, index, cbs, 19694d752508SVladimir Oltean true); 19704d752508SVladimir Oltean } 19714d752508SVladimir Oltean 19724d752508SVladimir Oltean static int sja1105_reload_cbs(struct sja1105_private *priv) 19734d752508SVladimir Oltean { 19744d752508SVladimir Oltean int rc = 0, i; 19754d752508SVladimir Oltean 1976be7f62eeSVladimir Oltean /* The credit based shapers are only allocated if 1977be7f62eeSVladimir Oltean * CONFIG_NET_SCH_CBS is enabled. 1978be7f62eeSVladimir Oltean */ 1979be7f62eeSVladimir Oltean if (!priv->cbs) 1980be7f62eeSVladimir Oltean return 0; 1981be7f62eeSVladimir Oltean 19824d752508SVladimir Oltean for (i = 0; i < priv->info->num_cbs_shapers; i++) { 19834d752508SVladimir Oltean struct sja1105_cbs_entry *cbs = &priv->cbs[i]; 19844d752508SVladimir Oltean 19854d752508SVladimir Oltean if (!cbs->idle_slope && !cbs->send_slope) 19864d752508SVladimir Oltean continue; 19874d752508SVladimir Oltean 19884d752508SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_CBS, i, cbs, 19894d752508SVladimir Oltean true); 19904d752508SVladimir Oltean if (rc) 19914d752508SVladimir Oltean break; 19924d752508SVladimir Oltean } 19934d752508SVladimir Oltean 19944d752508SVladimir Oltean return rc; 19954d752508SVladimir Oltean } 19964d752508SVladimir Oltean 19972eea1fa8SVladimir Oltean static const char * const sja1105_reset_reasons[] = { 19982eea1fa8SVladimir Oltean [SJA1105_VLAN_FILTERING] = "VLAN filtering", 19992eea1fa8SVladimir Oltean [SJA1105_RX_HWTSTAMPING] = "RX timestamping", 20002eea1fa8SVladimir Oltean [SJA1105_AGEING_TIME] = "Ageing time", 20012eea1fa8SVladimir Oltean [SJA1105_SCHEDULING] = "Time-aware scheduling", 2002c279c726SVladimir Oltean [SJA1105_BEST_EFFORT_POLICING] = "Best-effort policing", 2003dfacc5a2SVladimir Oltean [SJA1105_VIRTUAL_LINKS] = "Virtual links", 20042eea1fa8SVladimir Oltean }; 20052eea1fa8SVladimir Oltean 20066666cebcSVladimir Oltean /* For situations where we need to change a setting at runtime that is only 20076666cebcSVladimir Oltean * available through the static configuration, resetting the switch in order 20086666cebcSVladimir Oltean * to upload the new static config is unavoidable. Back up the settings we 20096666cebcSVladimir Oltean * modify at runtime (currently only MAC) and restore them after uploading, 20106666cebcSVladimir Oltean * such that this operation is relatively seamless. 20116666cebcSVladimir Oltean */ 20122eea1fa8SVladimir Oltean int sja1105_static_config_reload(struct sja1105_private *priv, 20132eea1fa8SVladimir Oltean enum sja1105_reset_reason reason) 20146666cebcSVladimir Oltean { 20156cf99c13SVladimir Oltean struct ptp_system_timestamp ptp_sts_before; 20166cf99c13SVladimir Oltean struct ptp_system_timestamp ptp_sts_after; 201782760d7fSVladimir Oltean int speed_mbps[SJA1105_MAX_NUM_PORTS]; 201884db00f2SVladimir Oltean u16 bmcr[SJA1105_MAX_NUM_PORTS] = {0}; 20196666cebcSVladimir Oltean struct sja1105_mac_config_entry *mac; 20206cf99c13SVladimir Oltean struct dsa_switch *ds = priv->ds; 20216cf99c13SVladimir Oltean s64 t1, t2, t3, t4; 20226cf99c13SVladimir Oltean s64 t12, t34; 20236666cebcSVladimir Oltean int rc, i; 20246cf99c13SVladimir Oltean s64 now; 20256666cebcSVladimir Oltean 2026af580ae2SVladimir Oltean mutex_lock(&priv->mgmt_lock); 2027af580ae2SVladimir Oltean 20286666cebcSVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 20296666cebcSVladimir Oltean 20308400cff6SVladimir Oltean /* Back up the dynamic link speed changed by sja1105_adjust_port_config 20318400cff6SVladimir Oltean * in order to temporarily restore it to SJA1105_SPEED_AUTO - which the 20328400cff6SVladimir Oltean * switch wants to see in the static config in order to allow us to 20338400cff6SVladimir Oltean * change it through the dynamic interface later. 20346666cebcSVladimir Oltean */ 2035542043e9SVladimir Oltean for (i = 0; i < ds->num_ports; i++) { 20363ad1d171SVladimir Oltean u32 reg_addr = mdiobus_c45_addr(MDIO_MMD_VEND2, MDIO_CTRL1); 20373ad1d171SVladimir Oltean 203841fed17fSVladimir Oltean speed_mbps[i] = sja1105_port_speed_to_ethtool(priv, 203941fed17fSVladimir Oltean mac[i].speed); 204041fed17fSVladimir Oltean mac[i].speed = priv->info->port_speed[SJA1105_SPEED_AUTO]; 20416666cebcSVladimir Oltean 20423ad1d171SVladimir Oltean if (priv->xpcs[i]) 20433ad1d171SVladimir Oltean bmcr[i] = mdiobus_read(priv->mdio_pcs, i, reg_addr); 204484db00f2SVladimir Oltean } 2045ffe10e67SVladimir Oltean 20466cf99c13SVladimir Oltean /* No PTP operations can run right now */ 20476cf99c13SVladimir Oltean mutex_lock(&priv->ptp_data.lock); 20486cf99c13SVladimir Oltean 20496cf99c13SVladimir Oltean rc = __sja1105_ptp_gettimex(ds, &now, &ptp_sts_before); 205061c77533SVladimir Oltean if (rc < 0) { 205161c77533SVladimir Oltean mutex_unlock(&priv->ptp_data.lock); 205261c77533SVladimir Oltean goto out; 205361c77533SVladimir Oltean } 20546cf99c13SVladimir Oltean 20556666cebcSVladimir Oltean /* Reset switch and send updated static configuration */ 20566666cebcSVladimir Oltean rc = sja1105_static_config_upload(priv); 205761c77533SVladimir Oltean if (rc < 0) { 205861c77533SVladimir Oltean mutex_unlock(&priv->ptp_data.lock); 205961c77533SVladimir Oltean goto out; 206061c77533SVladimir Oltean } 20616cf99c13SVladimir Oltean 20626cf99c13SVladimir Oltean rc = __sja1105_ptp_settime(ds, 0, &ptp_sts_after); 206361c77533SVladimir Oltean if (rc < 0) { 206461c77533SVladimir Oltean mutex_unlock(&priv->ptp_data.lock); 206561c77533SVladimir Oltean goto out; 206661c77533SVladimir Oltean } 20676cf99c13SVladimir Oltean 20686cf99c13SVladimir Oltean t1 = timespec64_to_ns(&ptp_sts_before.pre_ts); 20696cf99c13SVladimir Oltean t2 = timespec64_to_ns(&ptp_sts_before.post_ts); 20706cf99c13SVladimir Oltean t3 = timespec64_to_ns(&ptp_sts_after.pre_ts); 20716cf99c13SVladimir Oltean t4 = timespec64_to_ns(&ptp_sts_after.post_ts); 20726cf99c13SVladimir Oltean /* Mid point, corresponds to pre-reset PTPCLKVAL */ 20736cf99c13SVladimir Oltean t12 = t1 + (t2 - t1) / 2; 20746cf99c13SVladimir Oltean /* Mid point, corresponds to post-reset PTPCLKVAL, aka 0 */ 20756cf99c13SVladimir Oltean t34 = t3 + (t4 - t3) / 2; 20766cf99c13SVladimir Oltean /* Advance PTPCLKVAL by the time it took since its readout */ 20776cf99c13SVladimir Oltean now += (t34 - t12); 20786cf99c13SVladimir Oltean 20796cf99c13SVladimir Oltean __sja1105_ptp_adjtime(ds, now); 20806cf99c13SVladimir Oltean 20816cf99c13SVladimir Oltean mutex_unlock(&priv->ptp_data.lock); 20826666cebcSVladimir Oltean 20832eea1fa8SVladimir Oltean dev_info(priv->ds->dev, 20842eea1fa8SVladimir Oltean "Reset switch and programmed static config. Reason: %s\n", 20852eea1fa8SVladimir Oltean sja1105_reset_reasons[reason]); 20862eea1fa8SVladimir Oltean 20876666cebcSVladimir Oltean /* Configure the CGU (PLLs) for MII and RMII PHYs. 20886666cebcSVladimir Oltean * For these interfaces there is no dynamic configuration 20896666cebcSVladimir Oltean * needed, since PLLs have same settings at all speeds. 20906666cebcSVladimir Oltean */ 2091cb5a82d2SVladimir Oltean if (priv->info->clocking_setup) { 2092c5037678SVladimir Oltean rc = priv->info->clocking_setup(priv); 20936666cebcSVladimir Oltean if (rc < 0) 20946666cebcSVladimir Oltean goto out; 2095cb5a82d2SVladimir Oltean } 20966666cebcSVladimir Oltean 2097542043e9SVladimir Oltean for (i = 0; i < ds->num_ports; i++) { 20983ad1d171SVladimir Oltean struct dw_xpcs *xpcs = priv->xpcs[i]; 20993ad1d171SVladimir Oltean unsigned int mode; 210084db00f2SVladimir Oltean 21018400cff6SVladimir Oltean rc = sja1105_adjust_port_config(priv, i, speed_mbps[i]); 21026666cebcSVladimir Oltean if (rc < 0) 21036666cebcSVladimir Oltean goto out; 2104ffe10e67SVladimir Oltean 21053ad1d171SVladimir Oltean if (!xpcs) 210684db00f2SVladimir Oltean continue; 2107ffe10e67SVladimir Oltean 21083ad1d171SVladimir Oltean if (bmcr[i] & BMCR_ANENABLE) 21093ad1d171SVladimir Oltean mode = MLO_AN_INBAND; 21103ad1d171SVladimir Oltean else if (priv->fixed_link[i]) 21113ad1d171SVladimir Oltean mode = MLO_AN_FIXED; 21123ad1d171SVladimir Oltean else 21133ad1d171SVladimir Oltean mode = MLO_AN_PHY; 211484db00f2SVladimir Oltean 21153ad1d171SVladimir Oltean rc = xpcs_do_config(xpcs, priv->phy_mode[i], mode); 21163ad1d171SVladimir Oltean if (rc < 0) 21173ad1d171SVladimir Oltean goto out; 2118ffe10e67SVladimir Oltean 21193ad1d171SVladimir Oltean if (!phylink_autoneg_inband(mode)) { 2120ffe10e67SVladimir Oltean int speed = SPEED_UNKNOWN; 2121ffe10e67SVladimir Oltean 212256b63466SVladimir Oltean if (priv->phy_mode[i] == PHY_INTERFACE_MODE_2500BASEX) 212356b63466SVladimir Oltean speed = SPEED_2500; 212456b63466SVladimir Oltean else if (bmcr[i] & BMCR_SPEED1000) 2125ffe10e67SVladimir Oltean speed = SPEED_1000; 212684db00f2SVladimir Oltean else if (bmcr[i] & BMCR_SPEED100) 2127ffe10e67SVladimir Oltean speed = SPEED_100; 2128053d8ad1SVladimir Oltean else 2129ffe10e67SVladimir Oltean speed = SPEED_10; 2130ffe10e67SVladimir Oltean 21313ad1d171SVladimir Oltean xpcs_link_up(&xpcs->pcs, mode, priv->phy_mode[i], 21323ad1d171SVladimir Oltean speed, DUPLEX_FULL); 2133ffe10e67SVladimir Oltean } 2134ffe10e67SVladimir Oltean } 21354d752508SVladimir Oltean 21364d752508SVladimir Oltean rc = sja1105_reload_cbs(priv); 21374d752508SVladimir Oltean if (rc < 0) 21384d752508SVladimir Oltean goto out; 21396666cebcSVladimir Oltean out: 2140af580ae2SVladimir Oltean mutex_unlock(&priv->mgmt_lock); 2141af580ae2SVladimir Oltean 21426666cebcSVladimir Oltean return rc; 21436666cebcSVladimir Oltean } 21446666cebcSVladimir Oltean 21458aa9ebccSVladimir Oltean static enum dsa_tag_protocol 21464d776482SFlorian Fainelli sja1105_get_tag_protocol(struct dsa_switch *ds, int port, 21474d776482SFlorian Fainelli enum dsa_tag_protocol mp) 21488aa9ebccSVladimir Oltean { 21494913b8ebSVladimir Oltean struct sja1105_private *priv = ds->priv; 21504913b8ebSVladimir Oltean 21514913b8ebSVladimir Oltean return priv->info->tag_proto; 21528aa9ebccSVladimir Oltean } 21538aa9ebccSVladimir Oltean 2154070ca3bbSVladimir Oltean /* The TPID setting belongs to the General Parameters table, 2155070ca3bbSVladimir Oltean * which can only be partially reconfigured at runtime (and not the TPID). 2156070ca3bbSVladimir Oltean * So a switch reset is required. 2157070ca3bbSVladimir Oltean */ 215889153ed6SVladimir Oltean int sja1105_vlan_filtering(struct dsa_switch *ds, int port, bool enabled, 215989153ed6SVladimir Oltean struct netlink_ext_ack *extack) 21606666cebcSVladimir Oltean { 21616d7c7d94SVladimir Oltean struct sja1105_l2_lookup_params_entry *l2_lookup_params; 2162070ca3bbSVladimir Oltean struct sja1105_general_params_entry *general_params; 21636666cebcSVladimir Oltean struct sja1105_private *priv = ds->priv; 2164070ca3bbSVladimir Oltean struct sja1105_table *table; 2165dfacc5a2SVladimir Oltean struct sja1105_rule *rule; 2166070ca3bbSVladimir Oltean u16 tpid, tpid2; 21676666cebcSVladimir Oltean int rc; 21686666cebcSVladimir Oltean 2169dfacc5a2SVladimir Oltean list_for_each_entry(rule, &priv->flow_block.rules, list) { 2170dfacc5a2SVladimir Oltean if (rule->type == SJA1105_RULE_VL) { 217189153ed6SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 217289153ed6SVladimir Oltean "Cannot change VLAN filtering with active VL rules"); 2173dfacc5a2SVladimir Oltean return -EBUSY; 2174dfacc5a2SVladimir Oltean } 2175dfacc5a2SVladimir Oltean } 2176dfacc5a2SVladimir Oltean 2177070ca3bbSVladimir Oltean if (enabled) { 21786666cebcSVladimir Oltean /* Enable VLAN filtering. */ 217954fa49eeSVladimir Oltean tpid = ETH_P_8021Q; 218054fa49eeSVladimir Oltean tpid2 = ETH_P_8021AD; 2181070ca3bbSVladimir Oltean } else { 21826666cebcSVladimir Oltean /* Disable VLAN filtering. */ 2183070ca3bbSVladimir Oltean tpid = ETH_P_SJA1105; 2184070ca3bbSVladimir Oltean tpid2 = ETH_P_SJA1105; 2185070ca3bbSVladimir Oltean } 2186070ca3bbSVladimir Oltean 218738b5beeaSVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 218838b5beeaSVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 218938b5beeaSVladimir Oltean 219038b5beeaSVladimir Oltean if (enabled) 219138b5beeaSVladimir Oltean sp->xmit_tpid = priv->info->qinq_tpid; 219238b5beeaSVladimir Oltean else 219338b5beeaSVladimir Oltean sp->xmit_tpid = ETH_P_SJA1105; 219438b5beeaSVladimir Oltean } 219538b5beeaSVladimir Oltean 21960fac6aa0SVladimir Oltean if (priv->vlan_aware == enabled) 2197cfa36b1fSVladimir Oltean return 0; 2198cfa36b1fSVladimir Oltean 21990fac6aa0SVladimir Oltean priv->vlan_aware = enabled; 22007f14937fSVladimir Oltean 2201070ca3bbSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; 2202070ca3bbSVladimir Oltean general_params = table->entries; 2203f9a1a764SVladimir Oltean /* EtherType used to identify inner tagged (C-tag) VLAN traffic */ 220454fa49eeSVladimir Oltean general_params->tpid = tpid; 220554fa49eeSVladimir Oltean /* EtherType used to identify outer tagged (S-tag) VLAN traffic */ 2206070ca3bbSVladimir Oltean general_params->tpid2 = tpid2; 220742824463SVladimir Oltean /* When VLAN filtering is on, we need to at least be able to 220842824463SVladimir Oltean * decode management traffic through the "backup plan". 220942824463SVladimir Oltean */ 221042824463SVladimir Oltean general_params->incl_srcpt1 = enabled; 221142824463SVladimir Oltean general_params->incl_srcpt0 = enabled; 2212070ca3bbSVladimir Oltean 22136d7c7d94SVladimir Oltean /* VLAN filtering => independent VLAN learning. 22142cafa72eSVladimir Oltean * No VLAN filtering (or best effort) => shared VLAN learning. 22156d7c7d94SVladimir Oltean * 22166d7c7d94SVladimir Oltean * In shared VLAN learning mode, untagged traffic still gets 22176d7c7d94SVladimir Oltean * pvid-tagged, and the FDB table gets populated with entries 22186d7c7d94SVladimir Oltean * containing the "real" (pvid or from VLAN tag) VLAN ID. 22196d7c7d94SVladimir Oltean * However the switch performs a masked L2 lookup in the FDB, 22206d7c7d94SVladimir Oltean * effectively only looking up a frame's DMAC (and not VID) for the 22216d7c7d94SVladimir Oltean * forwarding decision. 22226d7c7d94SVladimir Oltean * 22236d7c7d94SVladimir Oltean * This is extremely convenient for us, because in modes with 22246d7c7d94SVladimir Oltean * vlan_filtering=0, dsa_8021q actually installs unique pvid's into 22256d7c7d94SVladimir Oltean * each front panel port. This is good for identification but breaks 22266d7c7d94SVladimir Oltean * learning badly - the VID of the learnt FDB entry is unique, aka 22276d7c7d94SVladimir Oltean * no frames coming from any other port are going to have it. So 22286d7c7d94SVladimir Oltean * for forwarding purposes, this is as though learning was broken 22296d7c7d94SVladimir Oltean * (all frames get flooded). 22306d7c7d94SVladimir Oltean */ 22316d7c7d94SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS]; 22326d7c7d94SVladimir Oltean l2_lookup_params = table->entries; 22330fac6aa0SVladimir Oltean l2_lookup_params->shared_learn = !priv->vlan_aware; 2234aaa270c6SVladimir Oltean 22356dfd23d3SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 22366dfd23d3SVladimir Oltean if (dsa_is_unused_port(ds, port)) 22376dfd23d3SVladimir Oltean continue; 22386dfd23d3SVladimir Oltean 22396dfd23d3SVladimir Oltean rc = sja1105_commit_pvid(ds, port); 2240aef31718SVladimir Oltean if (rc) 2241aef31718SVladimir Oltean return rc; 22426dfd23d3SVladimir Oltean } 2243aef31718SVladimir Oltean 22442eea1fa8SVladimir Oltean rc = sja1105_static_config_reload(priv, SJA1105_VLAN_FILTERING); 22456666cebcSVladimir Oltean if (rc) 224689153ed6SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, "Failed to change VLAN Ethertype"); 22476666cebcSVladimir Oltean 22480fac6aa0SVladimir Oltean return rc; 22496666cebcSVladimir Oltean } 22506666cebcSVladimir Oltean 22516dfd23d3SVladimir Oltean static int sja1105_vlan_add(struct sja1105_private *priv, int port, u16 vid, 22526dfd23d3SVladimir Oltean u16 flags) 22535899ee36SVladimir Oltean { 22546dfd23d3SVladimir Oltean struct sja1105_vlan_lookup_entry *vlan; 22556dfd23d3SVladimir Oltean struct sja1105_table *table; 22566dfd23d3SVladimir Oltean int match, rc; 22575899ee36SVladimir Oltean 22586dfd23d3SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP]; 22596dfd23d3SVladimir Oltean 22606dfd23d3SVladimir Oltean match = sja1105_is_vlan_configured(priv, vid); 22616dfd23d3SVladimir Oltean if (match < 0) { 22626dfd23d3SVladimir Oltean rc = sja1105_table_resize(table, table->entry_count + 1); 22636dfd23d3SVladimir Oltean if (rc) 22646dfd23d3SVladimir Oltean return rc; 22656dfd23d3SVladimir Oltean match = table->entry_count - 1; 22666dfd23d3SVladimir Oltean } 22676dfd23d3SVladimir Oltean 22686dfd23d3SVladimir Oltean /* Assign pointer after the resize (it's new memory) */ 22696dfd23d3SVladimir Oltean vlan = table->entries; 22706dfd23d3SVladimir Oltean 22716dfd23d3SVladimir Oltean vlan[match].type_entry = SJA1110_VLAN_D_TAG; 22726dfd23d3SVladimir Oltean vlan[match].vlanid = vid; 22736dfd23d3SVladimir Oltean vlan[match].vlan_bc |= BIT(port); 22746dfd23d3SVladimir Oltean vlan[match].vmemb_port |= BIT(port); 22756dfd23d3SVladimir Oltean if (flags & BRIDGE_VLAN_INFO_UNTAGGED) 22766dfd23d3SVladimir Oltean vlan[match].tag_port &= ~BIT(port); 22776dfd23d3SVladimir Oltean else 22786dfd23d3SVladimir Oltean vlan[match].tag_port |= BIT(port); 22796dfd23d3SVladimir Oltean 22806dfd23d3SVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_VLAN_LOOKUP, vid, 22816dfd23d3SVladimir Oltean &vlan[match], true); 22826dfd23d3SVladimir Oltean } 22836dfd23d3SVladimir Oltean 22846dfd23d3SVladimir Oltean static int sja1105_vlan_del(struct sja1105_private *priv, int port, u16 vid) 22856dfd23d3SVladimir Oltean { 22866dfd23d3SVladimir Oltean struct sja1105_vlan_lookup_entry *vlan; 22876dfd23d3SVladimir Oltean struct sja1105_table *table; 22886dfd23d3SVladimir Oltean bool keep = true; 22896dfd23d3SVladimir Oltean int match, rc; 22906dfd23d3SVladimir Oltean 22916dfd23d3SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP]; 22926dfd23d3SVladimir Oltean 22936dfd23d3SVladimir Oltean match = sja1105_is_vlan_configured(priv, vid); 22946dfd23d3SVladimir Oltean /* Can't delete a missing entry. */ 22956dfd23d3SVladimir Oltean if (match < 0) 22965899ee36SVladimir Oltean return 0; 22975899ee36SVladimir Oltean 22986dfd23d3SVladimir Oltean /* Assign pointer after the resize (it's new memory) */ 22996dfd23d3SVladimir Oltean vlan = table->entries; 23006dfd23d3SVladimir Oltean 23016dfd23d3SVladimir Oltean vlan[match].vlanid = vid; 23026dfd23d3SVladimir Oltean vlan[match].vlan_bc &= ~BIT(port); 23036dfd23d3SVladimir Oltean vlan[match].vmemb_port &= ~BIT(port); 23046dfd23d3SVladimir Oltean /* Also unset tag_port, just so we don't have a confusing bitmap 23056dfd23d3SVladimir Oltean * (no practical purpose). 2306b38e659dSVladimir Oltean */ 23076dfd23d3SVladimir Oltean vlan[match].tag_port &= ~BIT(port); 2308b38e659dSVladimir Oltean 23096dfd23d3SVladimir Oltean /* If there's no port left as member of this VLAN, 23106dfd23d3SVladimir Oltean * it's time for it to go. 23116dfd23d3SVladimir Oltean */ 23126dfd23d3SVladimir Oltean if (!vlan[match].vmemb_port) 23136dfd23d3SVladimir Oltean keep = false; 23145899ee36SVladimir Oltean 23156dfd23d3SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_VLAN_LOOKUP, vid, 23166dfd23d3SVladimir Oltean &vlan[match], keep); 23176dfd23d3SVladimir Oltean if (rc < 0) 23186dfd23d3SVladimir Oltean return rc; 23195899ee36SVladimir Oltean 23206dfd23d3SVladimir Oltean if (!keep) 23216dfd23d3SVladimir Oltean return sja1105_table_delete_entry(table, match); 23225899ee36SVladimir Oltean 23235899ee36SVladimir Oltean return 0; 23245899ee36SVladimir Oltean } 23255899ee36SVladimir Oltean 23266dfd23d3SVladimir Oltean static int sja1105_bridge_vlan_add(struct dsa_switch *ds, int port, 232731046a5fSVladimir Oltean const struct switchdev_obj_port_vlan *vlan, 232831046a5fSVladimir Oltean struct netlink_ext_ack *extack) 23296666cebcSVladimir Oltean { 23306666cebcSVladimir Oltean struct sja1105_private *priv = ds->priv; 2331884be12fSVladimir Oltean u16 flags = vlan->flags; 23326666cebcSVladimir Oltean int rc; 23336666cebcSVladimir Oltean 23340fac6aa0SVladimir Oltean /* Be sure to deny alterations to the configuration done by tag_8021q. 23351958d581SVladimir Oltean */ 23360fac6aa0SVladimir Oltean if (vid_is_dsa_8021q(vlan->vid)) { 233731046a5fSVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 233831046a5fSVladimir Oltean "Range 1024-3071 reserved for dsa_8021q operation"); 23391958d581SVladimir Oltean return -EBUSY; 23401958d581SVladimir Oltean } 23411958d581SVladimir Oltean 2342c5130029SVladimir Oltean /* Always install bridge VLANs as egress-tagged on CPU and DSA ports */ 2343c5130029SVladimir Oltean if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) 2344884be12fSVladimir Oltean flags = 0; 2345884be12fSVladimir Oltean 2346884be12fSVladimir Oltean rc = sja1105_vlan_add(priv, port, vlan->vid, flags); 23476dfd23d3SVladimir Oltean if (rc) 23481958d581SVladimir Oltean return rc; 2349ec5ae610SVladimir Oltean 23506dfd23d3SVladimir Oltean if (vlan->flags & BRIDGE_VLAN_INFO_PVID) 23516dfd23d3SVladimir Oltean priv->bridge_pvid[port] = vlan->vid; 2352ec5ae610SVladimir Oltean 23536dfd23d3SVladimir Oltean return sja1105_commit_pvid(ds, port); 23546666cebcSVladimir Oltean } 23556666cebcSVladimir Oltean 23566dfd23d3SVladimir Oltean static int sja1105_bridge_vlan_del(struct dsa_switch *ds, int port, 23576666cebcSVladimir Oltean const struct switchdev_obj_port_vlan *vlan) 23586666cebcSVladimir Oltean { 23596666cebcSVladimir Oltean struct sja1105_private *priv = ds->priv; 2360bef0746cSVladimir Oltean int rc; 23616666cebcSVladimir Oltean 2362bef0746cSVladimir Oltean rc = sja1105_vlan_del(priv, port, vlan->vid); 2363bef0746cSVladimir Oltean if (rc) 2364bef0746cSVladimir Oltean return rc; 2365bef0746cSVladimir Oltean 2366bef0746cSVladimir Oltean /* In case the pvid was deleted, make sure that untagged packets will 2367bef0746cSVladimir Oltean * be dropped. 2368bef0746cSVladimir Oltean */ 2369bef0746cSVladimir Oltean return sja1105_commit_pvid(ds, port); 23706666cebcSVladimir Oltean } 23716666cebcSVladimir Oltean 23725899ee36SVladimir Oltean static int sja1105_dsa_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid, 23735899ee36SVladimir Oltean u16 flags) 23745899ee36SVladimir Oltean { 23755899ee36SVladimir Oltean struct sja1105_private *priv = ds->priv; 23765899ee36SVladimir Oltean int rc; 23775899ee36SVladimir Oltean 23786dfd23d3SVladimir Oltean rc = sja1105_vlan_add(priv, port, vid, flags); 23796dfd23d3SVladimir Oltean if (rc) 23805899ee36SVladimir Oltean return rc; 23815899ee36SVladimir Oltean 23826dfd23d3SVladimir Oltean if (flags & BRIDGE_VLAN_INFO_PVID) 23836dfd23d3SVladimir Oltean priv->tag_8021q_pvid[port] = vid; 23846dfd23d3SVladimir Oltean 23856dfd23d3SVladimir Oltean return sja1105_commit_pvid(ds, port); 23865899ee36SVladimir Oltean } 23875899ee36SVladimir Oltean 23885899ee36SVladimir Oltean static int sja1105_dsa_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid) 23895899ee36SVladimir Oltean { 23905899ee36SVladimir Oltean struct sja1105_private *priv = ds->priv; 23915899ee36SVladimir Oltean 23926dfd23d3SVladimir Oltean return sja1105_vlan_del(priv, port, vid); 23935899ee36SVladimir Oltean } 23945899ee36SVladimir Oltean 23954fbc08bdSVladimir Oltean static int sja1105_prechangeupper(struct dsa_switch *ds, int port, 23964fbc08bdSVladimir Oltean struct netdev_notifier_changeupper_info *info) 23974fbc08bdSVladimir Oltean { 23984fbc08bdSVladimir Oltean struct netlink_ext_ack *extack = info->info.extack; 23994fbc08bdSVladimir Oltean struct net_device *upper = info->upper_dev; 240019fa937aSVladimir Oltean struct dsa_switch_tree *dst = ds->dst; 240119fa937aSVladimir Oltean struct dsa_port *dp; 24024fbc08bdSVladimir Oltean 24034fbc08bdSVladimir Oltean if (is_vlan_dev(upper)) { 24044fbc08bdSVladimir Oltean NL_SET_ERR_MSG_MOD(extack, "8021q uppers are not supported"); 24054fbc08bdSVladimir Oltean return -EBUSY; 24064fbc08bdSVladimir Oltean } 24074fbc08bdSVladimir Oltean 240819fa937aSVladimir Oltean if (netif_is_bridge_master(upper)) { 240919fa937aSVladimir Oltean list_for_each_entry(dp, &dst->ports, list) { 241019fa937aSVladimir Oltean if (dp->bridge_dev && dp->bridge_dev != upper && 241119fa937aSVladimir Oltean br_vlan_enabled(dp->bridge_dev)) { 241219fa937aSVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 241319fa937aSVladimir Oltean "Only one VLAN-aware bridge is supported"); 241419fa937aSVladimir Oltean return -EBUSY; 241519fa937aSVladimir Oltean } 241619fa937aSVladimir Oltean } 241719fa937aSVladimir Oltean } 241819fa937aSVladimir Oltean 24194fbc08bdSVladimir Oltean return 0; 24204fbc08bdSVladimir Oltean } 24214fbc08bdSVladimir Oltean 24228aa9ebccSVladimir Oltean /* The programming model for the SJA1105 switch is "all-at-once" via static 24238aa9ebccSVladimir Oltean * configuration tables. Some of these can be dynamically modified at runtime, 24248aa9ebccSVladimir Oltean * but not the xMII mode parameters table. 24258aa9ebccSVladimir Oltean * Furthermode, some PHYs may not have crystals for generating their clocks 24268aa9ebccSVladimir Oltean * (e.g. RMII). Instead, their 50MHz clock is supplied via the SJA1105 port's 24278aa9ebccSVladimir Oltean * ref_clk pin. So port clocking needs to be initialized early, before 24288aa9ebccSVladimir Oltean * connecting to PHYs is attempted, otherwise they won't respond through MDIO. 24298aa9ebccSVladimir Oltean * Setting correct PHY link speed does not matter now. 24308aa9ebccSVladimir Oltean * But dsa_slave_phy_setup is called later than sja1105_setup, so the PHY 24318aa9ebccSVladimir Oltean * bindings are not yet parsed by DSA core. We need to parse early so that we 24328aa9ebccSVladimir Oltean * can populate the xMII mode parameters table. 24338aa9ebccSVladimir Oltean */ 24348aa9ebccSVladimir Oltean static int sja1105_setup(struct dsa_switch *ds) 24358aa9ebccSVladimir Oltean { 24368aa9ebccSVladimir Oltean struct sja1105_private *priv = ds->priv; 24378aa9ebccSVladimir Oltean int rc; 24388aa9ebccSVladimir Oltean 24395d645df9SVladimir Oltean rc = sja1105_parse_dt(priv); 24408aa9ebccSVladimir Oltean if (rc < 0) { 24418aa9ebccSVladimir Oltean dev_err(ds->dev, "Failed to parse DT: %d\n", rc); 24428aa9ebccSVladimir Oltean return rc; 24438aa9ebccSVladimir Oltean } 2444f5b8631cSVladimir Oltean 2445f5b8631cSVladimir Oltean /* Error out early if internal delays are required through DT 2446f5b8631cSVladimir Oltean * and we can't apply them. 2447f5b8631cSVladimir Oltean */ 244829afb83aSVladimir Oltean rc = sja1105_parse_rgmii_delays(priv); 2449f5b8631cSVladimir Oltean if (rc < 0) { 2450f5b8631cSVladimir Oltean dev_err(ds->dev, "RGMII delay not supported\n"); 2451f5b8631cSVladimir Oltean return rc; 2452f5b8631cSVladimir Oltean } 2453f5b8631cSVladimir Oltean 245461c77126SVladimir Oltean rc = sja1105_ptp_clock_register(ds); 2455bb77f36aSVladimir Oltean if (rc < 0) { 2456bb77f36aSVladimir Oltean dev_err(ds->dev, "Failed to register PTP clock: %d\n", rc); 2457bb77f36aSVladimir Oltean return rc; 2458bb77f36aSVladimir Oltean } 24595a8f0974SVladimir Oltean 24605a8f0974SVladimir Oltean rc = sja1105_mdiobus_register(ds); 24615a8f0974SVladimir Oltean if (rc < 0) { 24625a8f0974SVladimir Oltean dev_err(ds->dev, "Failed to register MDIO bus: %pe\n", 24635a8f0974SVladimir Oltean ERR_PTR(rc)); 24645a8f0974SVladimir Oltean goto out_ptp_clock_unregister; 24655a8f0974SVladimir Oltean } 24665a8f0974SVladimir Oltean 2467cb5a82d2SVladimir Oltean if (priv->info->disable_microcontroller) { 2468cb5a82d2SVladimir Oltean rc = priv->info->disable_microcontroller(priv); 2469cb5a82d2SVladimir Oltean if (rc < 0) { 2470cb5a82d2SVladimir Oltean dev_err(ds->dev, 2471cb5a82d2SVladimir Oltean "Failed to disable microcontroller: %pe\n", 2472cb5a82d2SVladimir Oltean ERR_PTR(rc)); 2473cb5a82d2SVladimir Oltean goto out_mdiobus_unregister; 2474cb5a82d2SVladimir Oltean } 2475cb5a82d2SVladimir Oltean } 2476cb5a82d2SVladimir Oltean 24778aa9ebccSVladimir Oltean /* Create and send configuration down to device */ 24785d645df9SVladimir Oltean rc = sja1105_static_config_load(priv); 24798aa9ebccSVladimir Oltean if (rc < 0) { 24808aa9ebccSVladimir Oltean dev_err(ds->dev, "Failed to load static config: %d\n", rc); 24815a8f0974SVladimir Oltean goto out_mdiobus_unregister; 24828aa9ebccSVladimir Oltean } 2483cb5a82d2SVladimir Oltean 24848aa9ebccSVladimir Oltean /* Configure the CGU (PHY link modes and speeds) */ 2485cb5a82d2SVladimir Oltean if (priv->info->clocking_setup) { 2486c5037678SVladimir Oltean rc = priv->info->clocking_setup(priv); 24878aa9ebccSVladimir Oltean if (rc < 0) { 2488cb5a82d2SVladimir Oltean dev_err(ds->dev, 2489cb5a82d2SVladimir Oltean "Failed to configure MII clocking: %pe\n", 2490cb5a82d2SVladimir Oltean ERR_PTR(rc)); 2491cec279a8SVladimir Oltean goto out_static_config_free; 24928aa9ebccSVladimir Oltean } 2493cb5a82d2SVladimir Oltean } 2494cb5a82d2SVladimir Oltean 24956666cebcSVladimir Oltean /* On SJA1105, VLAN filtering per se is always enabled in hardware. 24966666cebcSVladimir Oltean * The only thing we can do to disable it is lie about what the 802.1Q 24976666cebcSVladimir Oltean * EtherType is. 24986666cebcSVladimir Oltean * So it will still try to apply VLAN filtering, but all ingress 24996666cebcSVladimir Oltean * traffic (except frames received with EtherType of ETH_P_SJA1105) 25006666cebcSVladimir Oltean * will be internally tagged with a distorted VLAN header where the 25016666cebcSVladimir Oltean * TPID is ETH_P_SJA1105, and the VLAN ID is the port pvid. 25026666cebcSVladimir Oltean */ 25036666cebcSVladimir Oltean ds->vlan_filtering_is_global = true; 2504884be12fSVladimir Oltean ds->untag_bridge_pvid = true; 2505b6ad86e6SVladimir Oltean /* tag_8021q has 3 bits for the VBID, and the value 0 is reserved */ 2506b6ad86e6SVladimir Oltean ds->num_fwd_offloading_bridges = 7; 25078aa9ebccSVladimir Oltean 25085f06c63bSVladimir Oltean /* Advertise the 8 egress queues */ 25095f06c63bSVladimir Oltean ds->num_tx_queues = SJA1105_NUM_TC; 25105f06c63bSVladimir Oltean 2511c279c726SVladimir Oltean ds->mtu_enforcement_ingress = true; 2512c279c726SVladimir Oltean 25130a7bdbc2SVladimir Oltean rc = sja1105_devlink_setup(ds); 25142cafa72eSVladimir Oltean if (rc < 0) 2515cec279a8SVladimir Oltean goto out_static_config_free; 25162cafa72eSVladimir Oltean 2517bbed0bbdSVladimir Oltean rtnl_lock(); 2518328621f6SVladimir Oltean rc = dsa_tag_8021q_register(ds, htons(ETH_P_8021Q)); 2519bbed0bbdSVladimir Oltean rtnl_unlock(); 2520cec279a8SVladimir Oltean if (rc) 2521cec279a8SVladimir Oltean goto out_devlink_teardown; 2522cec279a8SVladimir Oltean 2523cec279a8SVladimir Oltean return 0; 2524cec279a8SVladimir Oltean 2525cec279a8SVladimir Oltean out_devlink_teardown: 2526cec279a8SVladimir Oltean sja1105_devlink_teardown(ds); 25275a8f0974SVladimir Oltean out_mdiobus_unregister: 25285a8f0974SVladimir Oltean sja1105_mdiobus_unregister(ds); 2529cec279a8SVladimir Oltean out_ptp_clock_unregister: 2530cec279a8SVladimir Oltean sja1105_ptp_clock_unregister(ds); 2531cec279a8SVladimir Oltean out_static_config_free: 2532cec279a8SVladimir Oltean sja1105_static_config_free(&priv->static_config); 2533bbed0bbdSVladimir Oltean 2534bbed0bbdSVladimir Oltean return rc; 2535227d07a0SVladimir Oltean } 2536227d07a0SVladimir Oltean 2537f3097be2SVladimir Oltean static void sja1105_teardown(struct dsa_switch *ds) 2538f3097be2SVladimir Oltean { 2539f3097be2SVladimir Oltean struct sja1105_private *priv = ds->priv; 2540a68578c2SVladimir Oltean int port; 2541a68578c2SVladimir Oltean 2542328621f6SVladimir Oltean rtnl_lock(); 2543328621f6SVladimir Oltean dsa_tag_8021q_unregister(ds); 2544328621f6SVladimir Oltean rtnl_unlock(); 2545328621f6SVladimir Oltean 2546542043e9SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 2547a68578c2SVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 2548a68578c2SVladimir Oltean 2549a68578c2SVladimir Oltean if (!dsa_is_user_port(ds, port)) 2550a68578c2SVladimir Oltean continue; 2551a68578c2SVladimir Oltean 255252c0d4e3SVladimir Oltean if (sp->xmit_worker) 2553a68578c2SVladimir Oltean kthread_destroy_worker(sp->xmit_worker); 2554a68578c2SVladimir Oltean } 2555f3097be2SVladimir Oltean 25560a7bdbc2SVladimir Oltean sja1105_devlink_teardown(ds); 2557a6af7763SVladimir Oltean sja1105_flower_teardown(ds); 2558317ab5b8SVladimir Oltean sja1105_tas_teardown(ds); 255961c77126SVladimir Oltean sja1105_ptp_clock_unregister(ds); 25606cb0abbdSVladimir Oltean sja1105_static_config_free(&priv->static_config); 2561f3097be2SVladimir Oltean } 2562f3097be2SVladimir Oltean 2563a68578c2SVladimir Oltean static void sja1105_port_disable(struct dsa_switch *ds, int port) 2564a68578c2SVladimir Oltean { 2565a68578c2SVladimir Oltean struct sja1105_private *priv = ds->priv; 2566a68578c2SVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 2567a68578c2SVladimir Oltean 2568a68578c2SVladimir Oltean if (!dsa_is_user_port(ds, port)) 2569a68578c2SVladimir Oltean return; 2570a68578c2SVladimir Oltean 2571a68578c2SVladimir Oltean kthread_cancel_work_sync(&sp->xmit_work); 2572a68578c2SVladimir Oltean skb_queue_purge(&sp->xmit_queue); 2573a68578c2SVladimir Oltean } 2574a68578c2SVladimir Oltean 2575227d07a0SVladimir Oltean static int sja1105_mgmt_xmit(struct dsa_switch *ds, int port, int slot, 257647ed985eSVladimir Oltean struct sk_buff *skb, bool takets) 2577227d07a0SVladimir Oltean { 2578227d07a0SVladimir Oltean struct sja1105_mgmt_entry mgmt_route = {0}; 2579227d07a0SVladimir Oltean struct sja1105_private *priv = ds->priv; 2580227d07a0SVladimir Oltean struct ethhdr *hdr; 2581227d07a0SVladimir Oltean int timeout = 10; 2582227d07a0SVladimir Oltean int rc; 2583227d07a0SVladimir Oltean 2584227d07a0SVladimir Oltean hdr = eth_hdr(skb); 2585227d07a0SVladimir Oltean 2586227d07a0SVladimir Oltean mgmt_route.macaddr = ether_addr_to_u64(hdr->h_dest); 2587227d07a0SVladimir Oltean mgmt_route.destports = BIT(port); 2588227d07a0SVladimir Oltean mgmt_route.enfport = 1; 258947ed985eSVladimir Oltean mgmt_route.tsreg = 0; 259047ed985eSVladimir Oltean mgmt_route.takets = takets; 2591227d07a0SVladimir Oltean 2592227d07a0SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE, 2593227d07a0SVladimir Oltean slot, &mgmt_route, true); 2594227d07a0SVladimir Oltean if (rc < 0) { 2595227d07a0SVladimir Oltean kfree_skb(skb); 2596227d07a0SVladimir Oltean return rc; 2597227d07a0SVladimir Oltean } 2598227d07a0SVladimir Oltean 2599227d07a0SVladimir Oltean /* Transfer skb to the host port. */ 260068bb8ea8SVivien Didelot dsa_enqueue_skb(skb, dsa_to_port(ds, port)->slave); 2601227d07a0SVladimir Oltean 2602227d07a0SVladimir Oltean /* Wait until the switch has processed the frame */ 2603227d07a0SVladimir Oltean do { 2604227d07a0SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_MGMT_ROUTE, 2605227d07a0SVladimir Oltean slot, &mgmt_route); 2606227d07a0SVladimir Oltean if (rc < 0) { 2607227d07a0SVladimir Oltean dev_err_ratelimited(priv->ds->dev, 2608227d07a0SVladimir Oltean "failed to poll for mgmt route\n"); 2609227d07a0SVladimir Oltean continue; 2610227d07a0SVladimir Oltean } 2611227d07a0SVladimir Oltean 2612227d07a0SVladimir Oltean /* UM10944: The ENFPORT flag of the respective entry is 2613227d07a0SVladimir Oltean * cleared when a match is found. The host can use this 2614227d07a0SVladimir Oltean * flag as an acknowledgment. 2615227d07a0SVladimir Oltean */ 2616227d07a0SVladimir Oltean cpu_relax(); 2617227d07a0SVladimir Oltean } while (mgmt_route.enfport && --timeout); 2618227d07a0SVladimir Oltean 2619227d07a0SVladimir Oltean if (!timeout) { 2620227d07a0SVladimir Oltean /* Clean up the management route so that a follow-up 2621227d07a0SVladimir Oltean * frame may not match on it by mistake. 26222a7e7409SVladimir Oltean * This is only hardware supported on P/Q/R/S - on E/T it is 26232a7e7409SVladimir Oltean * a no-op and we are silently discarding the -EOPNOTSUPP. 2624227d07a0SVladimir Oltean */ 2625227d07a0SVladimir Oltean sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE, 2626227d07a0SVladimir Oltean slot, &mgmt_route, false); 2627227d07a0SVladimir Oltean dev_err_ratelimited(priv->ds->dev, "xmit timed out\n"); 2628227d07a0SVladimir Oltean } 2629227d07a0SVladimir Oltean 2630227d07a0SVladimir Oltean return NETDEV_TX_OK; 2631227d07a0SVladimir Oltean } 2632227d07a0SVladimir Oltean 2633a68578c2SVladimir Oltean #define work_to_port(work) \ 2634a68578c2SVladimir Oltean container_of((work), struct sja1105_port, xmit_work) 2635a68578c2SVladimir Oltean #define tagger_to_sja1105(t) \ 2636a68578c2SVladimir Oltean container_of((t), struct sja1105_private, tagger_data) 2637a68578c2SVladimir Oltean 2638227d07a0SVladimir Oltean /* Deferred work is unfortunately necessary because setting up the management 2639227d07a0SVladimir Oltean * route cannot be done from atomit context (SPI transfer takes a sleepable 2640227d07a0SVladimir Oltean * lock on the bus) 2641227d07a0SVladimir Oltean */ 2642a68578c2SVladimir Oltean static void sja1105_port_deferred_xmit(struct kthread_work *work) 2643227d07a0SVladimir Oltean { 2644a68578c2SVladimir Oltean struct sja1105_port *sp = work_to_port(work); 2645a68578c2SVladimir Oltean struct sja1105_tagger_data *tagger_data = sp->data; 2646a68578c2SVladimir Oltean struct sja1105_private *priv = tagger_to_sja1105(tagger_data); 2647a68578c2SVladimir Oltean int port = sp - priv->ports; 2648a68578c2SVladimir Oltean struct sk_buff *skb; 2649a68578c2SVladimir Oltean 2650a68578c2SVladimir Oltean while ((skb = skb_dequeue(&sp->xmit_queue)) != NULL) { 2651c4b364ceSYangbo Lu struct sk_buff *clone = SJA1105_SKB_CB(skb)->clone; 2652227d07a0SVladimir Oltean 2653227d07a0SVladimir Oltean mutex_lock(&priv->mgmt_lock); 2654227d07a0SVladimir Oltean 2655a68578c2SVladimir Oltean sja1105_mgmt_xmit(priv->ds, port, 0, skb, !!clone); 2656a68578c2SVladimir Oltean 265747ed985eSVladimir Oltean /* The clone, if there, was made by dsa_skb_tx_timestamp */ 2658a68578c2SVladimir Oltean if (clone) 2659a68578c2SVladimir Oltean sja1105_ptp_txtstamp_skb(priv->ds, port, clone); 2660227d07a0SVladimir Oltean 2661227d07a0SVladimir Oltean mutex_unlock(&priv->mgmt_lock); 2662a68578c2SVladimir Oltean } 26638aa9ebccSVladimir Oltean } 26648aa9ebccSVladimir Oltean 26658456721dSVladimir Oltean /* The MAXAGE setting belongs to the L2 Forwarding Parameters table, 26668456721dSVladimir Oltean * which cannot be reconfigured at runtime. So a switch reset is required. 26678456721dSVladimir Oltean */ 26688456721dSVladimir Oltean static int sja1105_set_ageing_time(struct dsa_switch *ds, 26698456721dSVladimir Oltean unsigned int ageing_time) 26708456721dSVladimir Oltean { 26718456721dSVladimir Oltean struct sja1105_l2_lookup_params_entry *l2_lookup_params; 26728456721dSVladimir Oltean struct sja1105_private *priv = ds->priv; 26738456721dSVladimir Oltean struct sja1105_table *table; 26748456721dSVladimir Oltean unsigned int maxage; 26758456721dSVladimir Oltean 26768456721dSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS]; 26778456721dSVladimir Oltean l2_lookup_params = table->entries; 26788456721dSVladimir Oltean 26798456721dSVladimir Oltean maxage = SJA1105_AGEING_TIME_MS(ageing_time); 26808456721dSVladimir Oltean 26818456721dSVladimir Oltean if (l2_lookup_params->maxage == maxage) 26828456721dSVladimir Oltean return 0; 26838456721dSVladimir Oltean 26848456721dSVladimir Oltean l2_lookup_params->maxage = maxage; 26858456721dSVladimir Oltean 26862eea1fa8SVladimir Oltean return sja1105_static_config_reload(priv, SJA1105_AGEING_TIME); 26878456721dSVladimir Oltean } 26888456721dSVladimir Oltean 2689c279c726SVladimir Oltean static int sja1105_change_mtu(struct dsa_switch *ds, int port, int new_mtu) 2690c279c726SVladimir Oltean { 2691c279c726SVladimir Oltean struct sja1105_l2_policing_entry *policing; 2692c279c726SVladimir Oltean struct sja1105_private *priv = ds->priv; 2693c279c726SVladimir Oltean 2694c279c726SVladimir Oltean new_mtu += VLAN_ETH_HLEN + ETH_FCS_LEN; 2695c279c726SVladimir Oltean 2696777e55e3SVladimir Oltean if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) 2697c279c726SVladimir Oltean new_mtu += VLAN_HLEN; 2698c279c726SVladimir Oltean 2699c279c726SVladimir Oltean policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries; 2700c279c726SVladimir Oltean 2701a7cc081cSVladimir Oltean if (policing[port].maxlen == new_mtu) 2702c279c726SVladimir Oltean return 0; 2703c279c726SVladimir Oltean 2704a7cc081cSVladimir Oltean policing[port].maxlen = new_mtu; 2705c279c726SVladimir Oltean 2706c279c726SVladimir Oltean return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING); 2707c279c726SVladimir Oltean } 2708c279c726SVladimir Oltean 2709c279c726SVladimir Oltean static int sja1105_get_max_mtu(struct dsa_switch *ds, int port) 2710c279c726SVladimir Oltean { 2711c279c726SVladimir Oltean return 2043 - VLAN_ETH_HLEN - ETH_FCS_LEN; 2712c279c726SVladimir Oltean } 2713c279c726SVladimir Oltean 2714317ab5b8SVladimir Oltean static int sja1105_port_setup_tc(struct dsa_switch *ds, int port, 2715317ab5b8SVladimir Oltean enum tc_setup_type type, 2716317ab5b8SVladimir Oltean void *type_data) 2717317ab5b8SVladimir Oltean { 2718317ab5b8SVladimir Oltean switch (type) { 2719317ab5b8SVladimir Oltean case TC_SETUP_QDISC_TAPRIO: 2720317ab5b8SVladimir Oltean return sja1105_setup_tc_taprio(ds, port, type_data); 27214d752508SVladimir Oltean case TC_SETUP_QDISC_CBS: 27224d752508SVladimir Oltean return sja1105_setup_tc_cbs(ds, port, type_data); 2723317ab5b8SVladimir Oltean default: 2724317ab5b8SVladimir Oltean return -EOPNOTSUPP; 2725317ab5b8SVladimir Oltean } 2726317ab5b8SVladimir Oltean } 2727317ab5b8SVladimir Oltean 2728511e6ca0SVladimir Oltean /* We have a single mirror (@to) port, but can configure ingress and egress 2729511e6ca0SVladimir Oltean * mirroring on all other (@from) ports. 2730511e6ca0SVladimir Oltean * We need to allow mirroring rules only as long as the @to port is always the 2731511e6ca0SVladimir Oltean * same, and we need to unset the @to port from mirr_port only when there is no 2732511e6ca0SVladimir Oltean * mirroring rule that references it. 2733511e6ca0SVladimir Oltean */ 2734511e6ca0SVladimir Oltean static int sja1105_mirror_apply(struct sja1105_private *priv, int from, int to, 2735511e6ca0SVladimir Oltean bool ingress, bool enabled) 2736511e6ca0SVladimir Oltean { 2737511e6ca0SVladimir Oltean struct sja1105_general_params_entry *general_params; 2738511e6ca0SVladimir Oltean struct sja1105_mac_config_entry *mac; 2739542043e9SVladimir Oltean struct dsa_switch *ds = priv->ds; 2740511e6ca0SVladimir Oltean struct sja1105_table *table; 2741511e6ca0SVladimir Oltean bool already_enabled; 2742511e6ca0SVladimir Oltean u64 new_mirr_port; 2743511e6ca0SVladimir Oltean int rc; 2744511e6ca0SVladimir Oltean 2745511e6ca0SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; 2746511e6ca0SVladimir Oltean general_params = table->entries; 2747511e6ca0SVladimir Oltean 2748511e6ca0SVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 2749511e6ca0SVladimir Oltean 2750542043e9SVladimir Oltean already_enabled = (general_params->mirr_port != ds->num_ports); 2751511e6ca0SVladimir Oltean if (already_enabled && enabled && general_params->mirr_port != to) { 2752511e6ca0SVladimir Oltean dev_err(priv->ds->dev, 2753511e6ca0SVladimir Oltean "Delete mirroring rules towards port %llu first\n", 2754511e6ca0SVladimir Oltean general_params->mirr_port); 2755511e6ca0SVladimir Oltean return -EBUSY; 2756511e6ca0SVladimir Oltean } 2757511e6ca0SVladimir Oltean 2758511e6ca0SVladimir Oltean new_mirr_port = to; 2759511e6ca0SVladimir Oltean if (!enabled) { 2760511e6ca0SVladimir Oltean bool keep = false; 2761511e6ca0SVladimir Oltean int port; 2762511e6ca0SVladimir Oltean 2763511e6ca0SVladimir Oltean /* Anybody still referencing mirr_port? */ 2764542043e9SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 2765511e6ca0SVladimir Oltean if (mac[port].ing_mirr || mac[port].egr_mirr) { 2766511e6ca0SVladimir Oltean keep = true; 2767511e6ca0SVladimir Oltean break; 2768511e6ca0SVladimir Oltean } 2769511e6ca0SVladimir Oltean } 2770511e6ca0SVladimir Oltean /* Unset already_enabled for next time */ 2771511e6ca0SVladimir Oltean if (!keep) 2772542043e9SVladimir Oltean new_mirr_port = ds->num_ports; 2773511e6ca0SVladimir Oltean } 2774511e6ca0SVladimir Oltean if (new_mirr_port != general_params->mirr_port) { 2775511e6ca0SVladimir Oltean general_params->mirr_port = new_mirr_port; 2776511e6ca0SVladimir Oltean 2777511e6ca0SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_GENERAL_PARAMS, 2778511e6ca0SVladimir Oltean 0, general_params, true); 2779511e6ca0SVladimir Oltean if (rc < 0) 2780511e6ca0SVladimir Oltean return rc; 2781511e6ca0SVladimir Oltean } 2782511e6ca0SVladimir Oltean 2783511e6ca0SVladimir Oltean if (ingress) 2784511e6ca0SVladimir Oltean mac[from].ing_mirr = enabled; 2785511e6ca0SVladimir Oltean else 2786511e6ca0SVladimir Oltean mac[from].egr_mirr = enabled; 2787511e6ca0SVladimir Oltean 2788511e6ca0SVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, from, 2789511e6ca0SVladimir Oltean &mac[from], true); 2790511e6ca0SVladimir Oltean } 2791511e6ca0SVladimir Oltean 2792511e6ca0SVladimir Oltean static int sja1105_mirror_add(struct dsa_switch *ds, int port, 2793511e6ca0SVladimir Oltean struct dsa_mall_mirror_tc_entry *mirror, 2794511e6ca0SVladimir Oltean bool ingress) 2795511e6ca0SVladimir Oltean { 2796511e6ca0SVladimir Oltean return sja1105_mirror_apply(ds->priv, port, mirror->to_local_port, 2797511e6ca0SVladimir Oltean ingress, true); 2798511e6ca0SVladimir Oltean } 2799511e6ca0SVladimir Oltean 2800511e6ca0SVladimir Oltean static void sja1105_mirror_del(struct dsa_switch *ds, int port, 2801511e6ca0SVladimir Oltean struct dsa_mall_mirror_tc_entry *mirror) 2802511e6ca0SVladimir Oltean { 2803511e6ca0SVladimir Oltean sja1105_mirror_apply(ds->priv, port, mirror->to_local_port, 2804511e6ca0SVladimir Oltean mirror->ingress, false); 2805511e6ca0SVladimir Oltean } 2806511e6ca0SVladimir Oltean 2807a7cc081cSVladimir Oltean static int sja1105_port_policer_add(struct dsa_switch *ds, int port, 2808a7cc081cSVladimir Oltean struct dsa_mall_policer_tc_entry *policer) 2809a7cc081cSVladimir Oltean { 2810a7cc081cSVladimir Oltean struct sja1105_l2_policing_entry *policing; 2811a7cc081cSVladimir Oltean struct sja1105_private *priv = ds->priv; 2812a7cc081cSVladimir Oltean 2813a7cc081cSVladimir Oltean policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries; 2814a7cc081cSVladimir Oltean 2815a7cc081cSVladimir Oltean /* In hardware, every 8 microseconds the credit level is incremented by 2816a7cc081cSVladimir Oltean * the value of RATE bytes divided by 64, up to a maximum of SMAX 2817a7cc081cSVladimir Oltean * bytes. 2818a7cc081cSVladimir Oltean */ 2819a7cc081cSVladimir Oltean policing[port].rate = div_u64(512 * policer->rate_bytes_per_sec, 2820a7cc081cSVladimir Oltean 1000000); 28215f035af7SPo Liu policing[port].smax = policer->burst; 2822a7cc081cSVladimir Oltean 2823a7cc081cSVladimir Oltean return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING); 2824a7cc081cSVladimir Oltean } 2825a7cc081cSVladimir Oltean 2826a7cc081cSVladimir Oltean static void sja1105_port_policer_del(struct dsa_switch *ds, int port) 2827a7cc081cSVladimir Oltean { 2828a7cc081cSVladimir Oltean struct sja1105_l2_policing_entry *policing; 2829a7cc081cSVladimir Oltean struct sja1105_private *priv = ds->priv; 2830a7cc081cSVladimir Oltean 2831a7cc081cSVladimir Oltean policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries; 2832a7cc081cSVladimir Oltean 2833a7cc081cSVladimir Oltean policing[port].rate = SJA1105_RATE_MBPS(1000); 2834a7cc081cSVladimir Oltean policing[port].smax = 65535; 2835a7cc081cSVladimir Oltean 2836a7cc081cSVladimir Oltean sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING); 2837a7cc081cSVladimir Oltean } 2838a7cc081cSVladimir Oltean 28394d942354SVladimir Oltean static int sja1105_port_set_learning(struct sja1105_private *priv, int port, 28404d942354SVladimir Oltean bool enabled) 28414d942354SVladimir Oltean { 28424d942354SVladimir Oltean struct sja1105_mac_config_entry *mac; 28434d942354SVladimir Oltean int rc; 28444d942354SVladimir Oltean 28454d942354SVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 28464d942354SVladimir Oltean 28474c44fc5eSVladimir Oltean mac[port].dyn_learn = enabled; 28484d942354SVladimir Oltean 28494d942354SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, 28504d942354SVladimir Oltean &mac[port], true); 28514d942354SVladimir Oltean if (rc) 28524d942354SVladimir Oltean return rc; 28534d942354SVladimir Oltean 28544d942354SVladimir Oltean if (enabled) 28554d942354SVladimir Oltean priv->learn_ena |= BIT(port); 28564d942354SVladimir Oltean else 28574d942354SVladimir Oltean priv->learn_ena &= ~BIT(port); 28584d942354SVladimir Oltean 28594d942354SVladimir Oltean return 0; 28604d942354SVladimir Oltean } 28614d942354SVladimir Oltean 28624d942354SVladimir Oltean static int sja1105_port_ucast_bcast_flood(struct sja1105_private *priv, int to, 28634d942354SVladimir Oltean struct switchdev_brport_flags flags) 28644d942354SVladimir Oltean { 28654d942354SVladimir Oltean if (flags.mask & BR_FLOOD) { 28664d942354SVladimir Oltean if (flags.val & BR_FLOOD) 28677f7ccdeaSVladimir Oltean priv->ucast_egress_floods |= BIT(to); 28684d942354SVladimir Oltean else 28696a5166e0SVladimir Oltean priv->ucast_egress_floods &= ~BIT(to); 28704d942354SVladimir Oltean } 28717f7ccdeaSVladimir Oltean 28724d942354SVladimir Oltean if (flags.mask & BR_BCAST_FLOOD) { 28734d942354SVladimir Oltean if (flags.val & BR_BCAST_FLOOD) 28747f7ccdeaSVladimir Oltean priv->bcast_egress_floods |= BIT(to); 28754d942354SVladimir Oltean else 28766a5166e0SVladimir Oltean priv->bcast_egress_floods &= ~BIT(to); 28774d942354SVladimir Oltean } 28784d942354SVladimir Oltean 28797f7ccdeaSVladimir Oltean return sja1105_manage_flood_domains(priv); 28804d942354SVladimir Oltean } 28814d942354SVladimir Oltean 28824d942354SVladimir Oltean static int sja1105_port_mcast_flood(struct sja1105_private *priv, int to, 28834d942354SVladimir Oltean struct switchdev_brport_flags flags, 28844d942354SVladimir Oltean struct netlink_ext_ack *extack) 28854d942354SVladimir Oltean { 28864d942354SVladimir Oltean struct sja1105_l2_lookup_entry *l2_lookup; 28874d942354SVladimir Oltean struct sja1105_table *table; 28884d942354SVladimir Oltean int match; 28894d942354SVladimir Oltean 28904d942354SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; 28914d942354SVladimir Oltean l2_lookup = table->entries; 28924d942354SVladimir Oltean 28934d942354SVladimir Oltean for (match = 0; match < table->entry_count; match++) 28944d942354SVladimir Oltean if (l2_lookup[match].macaddr == SJA1105_UNKNOWN_MULTICAST && 28954d942354SVladimir Oltean l2_lookup[match].mask_macaddr == SJA1105_UNKNOWN_MULTICAST) 28964d942354SVladimir Oltean break; 28974d942354SVladimir Oltean 28984d942354SVladimir Oltean if (match == table->entry_count) { 28994d942354SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 29004d942354SVladimir Oltean "Could not find FDB entry for unknown multicast"); 29014d942354SVladimir Oltean return -ENOSPC; 29024d942354SVladimir Oltean } 29034d942354SVladimir Oltean 29044d942354SVladimir Oltean if (flags.val & BR_MCAST_FLOOD) 29054d942354SVladimir Oltean l2_lookup[match].destports |= BIT(to); 29064d942354SVladimir Oltean else 29074d942354SVladimir Oltean l2_lookup[match].destports &= ~BIT(to); 29084d942354SVladimir Oltean 29094d942354SVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 29104d942354SVladimir Oltean l2_lookup[match].index, 29114d942354SVladimir Oltean &l2_lookup[match], 29124d942354SVladimir Oltean true); 29134d942354SVladimir Oltean } 29144d942354SVladimir Oltean 29154d942354SVladimir Oltean static int sja1105_port_pre_bridge_flags(struct dsa_switch *ds, int port, 29164d942354SVladimir Oltean struct switchdev_brport_flags flags, 29174d942354SVladimir Oltean struct netlink_ext_ack *extack) 29184d942354SVladimir Oltean { 29194d942354SVladimir Oltean struct sja1105_private *priv = ds->priv; 29204d942354SVladimir Oltean 29214d942354SVladimir Oltean if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | 29224d942354SVladimir Oltean BR_BCAST_FLOOD)) 29234d942354SVladimir Oltean return -EINVAL; 29244d942354SVladimir Oltean 29254d942354SVladimir Oltean if (flags.mask & (BR_FLOOD | BR_MCAST_FLOOD) && 29264d942354SVladimir Oltean !priv->info->can_limit_mcast_flood) { 29274d942354SVladimir Oltean bool multicast = !!(flags.val & BR_MCAST_FLOOD); 29284d942354SVladimir Oltean bool unicast = !!(flags.val & BR_FLOOD); 29294d942354SVladimir Oltean 29304d942354SVladimir Oltean if (unicast != multicast) { 29314d942354SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 29324d942354SVladimir Oltean "This chip cannot configure multicast flooding independently of unicast"); 29334d942354SVladimir Oltean return -EINVAL; 29344d942354SVladimir Oltean } 29354d942354SVladimir Oltean } 29364d942354SVladimir Oltean 29374d942354SVladimir Oltean return 0; 29384d942354SVladimir Oltean } 29394d942354SVladimir Oltean 29404d942354SVladimir Oltean static int sja1105_port_bridge_flags(struct dsa_switch *ds, int port, 29414d942354SVladimir Oltean struct switchdev_brport_flags flags, 29424d942354SVladimir Oltean struct netlink_ext_ack *extack) 29434d942354SVladimir Oltean { 29444d942354SVladimir Oltean struct sja1105_private *priv = ds->priv; 29454d942354SVladimir Oltean int rc; 29464d942354SVladimir Oltean 29474d942354SVladimir Oltean if (flags.mask & BR_LEARNING) { 29484d942354SVladimir Oltean bool learn_ena = !!(flags.val & BR_LEARNING); 29494d942354SVladimir Oltean 29504d942354SVladimir Oltean rc = sja1105_port_set_learning(priv, port, learn_ena); 29514d942354SVladimir Oltean if (rc) 29524d942354SVladimir Oltean return rc; 29534d942354SVladimir Oltean } 29544d942354SVladimir Oltean 29554d942354SVladimir Oltean if (flags.mask & (BR_FLOOD | BR_BCAST_FLOOD)) { 29564d942354SVladimir Oltean rc = sja1105_port_ucast_bcast_flood(priv, port, flags); 29574d942354SVladimir Oltean if (rc) 29584d942354SVladimir Oltean return rc; 29594d942354SVladimir Oltean } 29604d942354SVladimir Oltean 29614d942354SVladimir Oltean /* For chips that can't offload BR_MCAST_FLOOD independently, there 29624d942354SVladimir Oltean * is nothing to do here, we ensured the configuration is in sync by 29634d942354SVladimir Oltean * offloading BR_FLOOD. 29644d942354SVladimir Oltean */ 29654d942354SVladimir Oltean if (flags.mask & BR_MCAST_FLOOD && priv->info->can_limit_mcast_flood) { 29664d942354SVladimir Oltean rc = sja1105_port_mcast_flood(priv, port, flags, 29674d942354SVladimir Oltean extack); 29684d942354SVladimir Oltean if (rc) 29694d942354SVladimir Oltean return rc; 29704d942354SVladimir Oltean } 29714d942354SVladimir Oltean 29724d942354SVladimir Oltean return 0; 29734d942354SVladimir Oltean } 29744d942354SVladimir Oltean 29758aa9ebccSVladimir Oltean static const struct dsa_switch_ops sja1105_switch_ops = { 29768aa9ebccSVladimir Oltean .get_tag_protocol = sja1105_get_tag_protocol, 29778aa9ebccSVladimir Oltean .setup = sja1105_setup, 2978f3097be2SVladimir Oltean .teardown = sja1105_teardown, 29798456721dSVladimir Oltean .set_ageing_time = sja1105_set_ageing_time, 2980c279c726SVladimir Oltean .port_change_mtu = sja1105_change_mtu, 2981c279c726SVladimir Oltean .port_max_mtu = sja1105_get_max_mtu, 2982ad9f299aSVladimir Oltean .phylink_validate = sja1105_phylink_validate, 2983af7cd036SVladimir Oltean .phylink_mac_config = sja1105_mac_config, 29848400cff6SVladimir Oltean .phylink_mac_link_up = sja1105_mac_link_up, 29858400cff6SVladimir Oltean .phylink_mac_link_down = sja1105_mac_link_down, 298652c34e6eSVladimir Oltean .get_strings = sja1105_get_strings, 298752c34e6eSVladimir Oltean .get_ethtool_stats = sja1105_get_ethtool_stats, 298852c34e6eSVladimir Oltean .get_sset_count = sja1105_get_sset_count, 2989bb77f36aSVladimir Oltean .get_ts_info = sja1105_get_ts_info, 2990a68578c2SVladimir Oltean .port_disable = sja1105_port_disable, 2991291d1e72SVladimir Oltean .port_fdb_dump = sja1105_fdb_dump, 2992291d1e72SVladimir Oltean .port_fdb_add = sja1105_fdb_add, 2993291d1e72SVladimir Oltean .port_fdb_del = sja1105_fdb_del, 29948aa9ebccSVladimir Oltean .port_bridge_join = sja1105_bridge_join, 29958aa9ebccSVladimir Oltean .port_bridge_leave = sja1105_bridge_leave, 29964d942354SVladimir Oltean .port_pre_bridge_flags = sja1105_port_pre_bridge_flags, 29974d942354SVladimir Oltean .port_bridge_flags = sja1105_port_bridge_flags, 2998640f763fSVladimir Oltean .port_stp_state_set = sja1105_bridge_stp_state_set, 29996666cebcSVladimir Oltean .port_vlan_filtering = sja1105_vlan_filtering, 30006dfd23d3SVladimir Oltean .port_vlan_add = sja1105_bridge_vlan_add, 30016dfd23d3SVladimir Oltean .port_vlan_del = sja1105_bridge_vlan_del, 3002291d1e72SVladimir Oltean .port_mdb_add = sja1105_mdb_add, 3003291d1e72SVladimir Oltean .port_mdb_del = sja1105_mdb_del, 3004a602afd2SVladimir Oltean .port_hwtstamp_get = sja1105_hwtstamp_get, 3005a602afd2SVladimir Oltean .port_hwtstamp_set = sja1105_hwtstamp_set, 3006f3097be2SVladimir Oltean .port_rxtstamp = sja1105_port_rxtstamp, 300747ed985eSVladimir Oltean .port_txtstamp = sja1105_port_txtstamp, 3008317ab5b8SVladimir Oltean .port_setup_tc = sja1105_port_setup_tc, 3009511e6ca0SVladimir Oltean .port_mirror_add = sja1105_mirror_add, 3010511e6ca0SVladimir Oltean .port_mirror_del = sja1105_mirror_del, 3011a7cc081cSVladimir Oltean .port_policer_add = sja1105_port_policer_add, 3012a7cc081cSVladimir Oltean .port_policer_del = sja1105_port_policer_del, 3013a6af7763SVladimir Oltean .cls_flower_add = sja1105_cls_flower_add, 3014a6af7763SVladimir Oltean .cls_flower_del = sja1105_cls_flower_del, 3015834f8933SVladimir Oltean .cls_flower_stats = sja1105_cls_flower_stats, 3016ff4cf8eaSVladimir Oltean .devlink_info_get = sja1105_devlink_info_get, 30175da11eb4SVladimir Oltean .tag_8021q_vlan_add = sja1105_dsa_8021q_vlan_add, 30185da11eb4SVladimir Oltean .tag_8021q_vlan_del = sja1105_dsa_8021q_vlan_del, 30194fbc08bdSVladimir Oltean .port_prechangeupper = sja1105_prechangeupper, 3020b6ad86e6SVladimir Oltean .port_bridge_tx_fwd_offload = dsa_tag_8021q_bridge_tx_fwd_offload, 3021b6ad86e6SVladimir Oltean .port_bridge_tx_fwd_unoffload = dsa_tag_8021q_bridge_tx_fwd_unoffload, 30228aa9ebccSVladimir Oltean }; 30238aa9ebccSVladimir Oltean 30240b0e2997SVladimir Oltean static const struct of_device_id sja1105_dt_ids[]; 30250b0e2997SVladimir Oltean 30268aa9ebccSVladimir Oltean static int sja1105_check_device_id(struct sja1105_private *priv) 30278aa9ebccSVladimir Oltean { 30288aa9ebccSVladimir Oltean const struct sja1105_regs *regs = priv->info->regs; 30298aa9ebccSVladimir Oltean u8 prod_id[SJA1105_SIZE_DEVICE_ID] = {0}; 30308aa9ebccSVladimir Oltean struct device *dev = &priv->spidev->dev; 30310b0e2997SVladimir Oltean const struct of_device_id *match; 3032dff79620SVladimir Oltean u32 device_id; 30338aa9ebccSVladimir Oltean u64 part_no; 30348aa9ebccSVladimir Oltean int rc; 30358aa9ebccSVladimir Oltean 303634d76e9fSVladimir Oltean rc = sja1105_xfer_u32(priv, SPI_READ, regs->device_id, &device_id, 303734d76e9fSVladimir Oltean NULL); 30388aa9ebccSVladimir Oltean if (rc < 0) 30398aa9ebccSVladimir Oltean return rc; 30408aa9ebccSVladimir Oltean 30411bd44870SVladimir Oltean rc = sja1105_xfer_buf(priv, SPI_READ, regs->prod_id, prod_id, 30421bd44870SVladimir Oltean SJA1105_SIZE_DEVICE_ID); 30438aa9ebccSVladimir Oltean if (rc < 0) 30448aa9ebccSVladimir Oltean return rc; 30458aa9ebccSVladimir Oltean 30468aa9ebccSVladimir Oltean sja1105_unpack(prod_id, &part_no, 19, 4, SJA1105_SIZE_DEVICE_ID); 30478aa9ebccSVladimir Oltean 30485978fac0SNathan Chancellor for (match = sja1105_dt_ids; match->compatible[0]; match++) { 30490b0e2997SVladimir Oltean const struct sja1105_info *info = match->data; 30500b0e2997SVladimir Oltean 30510b0e2997SVladimir Oltean /* Is what's been probed in our match table at all? */ 30520b0e2997SVladimir Oltean if (info->device_id != device_id || info->part_no != part_no) 30530b0e2997SVladimir Oltean continue; 30540b0e2997SVladimir Oltean 30550b0e2997SVladimir Oltean /* But is it what's in the device tree? */ 30560b0e2997SVladimir Oltean if (priv->info->device_id != device_id || 30570b0e2997SVladimir Oltean priv->info->part_no != part_no) { 30580b0e2997SVladimir Oltean dev_warn(dev, "Device tree specifies chip %s but found %s, please fix it!\n", 30590b0e2997SVladimir Oltean priv->info->name, info->name); 30600b0e2997SVladimir Oltean /* It isn't. No problem, pick that up. */ 30610b0e2997SVladimir Oltean priv->info = info; 30628aa9ebccSVladimir Oltean } 30638aa9ebccSVladimir Oltean 30648aa9ebccSVladimir Oltean return 0; 30658aa9ebccSVladimir Oltean } 30668aa9ebccSVladimir Oltean 30670b0e2997SVladimir Oltean dev_err(dev, "Unexpected {device ID, part number}: 0x%x 0x%llx\n", 30680b0e2997SVladimir Oltean device_id, part_no); 30690b0e2997SVladimir Oltean 30700b0e2997SVladimir Oltean return -ENODEV; 30710b0e2997SVladimir Oltean } 30720b0e2997SVladimir Oltean 30738aa9ebccSVladimir Oltean static int sja1105_probe(struct spi_device *spi) 30748aa9ebccSVladimir Oltean { 3075844d7edcSVladimir Oltean struct sja1105_tagger_data *tagger_data; 30768aa9ebccSVladimir Oltean struct device *dev = &spi->dev; 30778aa9ebccSVladimir Oltean struct sja1105_private *priv; 3078718bad0eSVladimir Oltean size_t max_xfer, max_msg; 30798aa9ebccSVladimir Oltean struct dsa_switch *ds; 3080a68578c2SVladimir Oltean int rc, port; 30818aa9ebccSVladimir Oltean 30828aa9ebccSVladimir Oltean if (!dev->of_node) { 30838aa9ebccSVladimir Oltean dev_err(dev, "No DTS bindings for SJA1105 driver\n"); 30848aa9ebccSVladimir Oltean return -EINVAL; 30858aa9ebccSVladimir Oltean } 30868aa9ebccSVladimir Oltean 30878aa9ebccSVladimir Oltean priv = devm_kzalloc(dev, sizeof(struct sja1105_private), GFP_KERNEL); 30888aa9ebccSVladimir Oltean if (!priv) 30898aa9ebccSVladimir Oltean return -ENOMEM; 30908aa9ebccSVladimir Oltean 30918aa9ebccSVladimir Oltean /* Configure the optional reset pin and bring up switch */ 30928aa9ebccSVladimir Oltean priv->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); 30938aa9ebccSVladimir Oltean if (IS_ERR(priv->reset_gpio)) 30948aa9ebccSVladimir Oltean dev_dbg(dev, "reset-gpios not defined, ignoring\n"); 30958aa9ebccSVladimir Oltean else 30968aa9ebccSVladimir Oltean sja1105_hw_reset(priv->reset_gpio, 1, 1); 30978aa9ebccSVladimir Oltean 30988aa9ebccSVladimir Oltean /* Populate our driver private structure (priv) based on 30998aa9ebccSVladimir Oltean * the device tree node that was probed (spi) 31008aa9ebccSVladimir Oltean */ 31018aa9ebccSVladimir Oltean priv->spidev = spi; 31028aa9ebccSVladimir Oltean spi_set_drvdata(spi, priv); 31038aa9ebccSVladimir Oltean 31048aa9ebccSVladimir Oltean /* Configure the SPI bus */ 31058aa9ebccSVladimir Oltean spi->bits_per_word = 8; 31068aa9ebccSVladimir Oltean rc = spi_setup(spi); 31078aa9ebccSVladimir Oltean if (rc < 0) { 31088aa9ebccSVladimir Oltean dev_err(dev, "Could not init SPI\n"); 31098aa9ebccSVladimir Oltean return rc; 31108aa9ebccSVladimir Oltean } 31118aa9ebccSVladimir Oltean 3112718bad0eSVladimir Oltean /* In sja1105_xfer, we send spi_messages composed of two spi_transfers: 3113718bad0eSVladimir Oltean * a small one for the message header and another one for the current 3114718bad0eSVladimir Oltean * chunk of the packed buffer. 3115718bad0eSVladimir Oltean * Check that the restrictions imposed by the SPI controller are 3116718bad0eSVladimir Oltean * respected: the chunk buffer is smaller than the max transfer size, 3117718bad0eSVladimir Oltean * and the total length of the chunk plus its message header is smaller 3118718bad0eSVladimir Oltean * than the max message size. 3119718bad0eSVladimir Oltean * We do that during probe time since the maximum transfer size is a 3120718bad0eSVladimir Oltean * runtime invariant. 3121718bad0eSVladimir Oltean */ 3122718bad0eSVladimir Oltean max_xfer = spi_max_transfer_size(spi); 3123718bad0eSVladimir Oltean max_msg = spi_max_message_size(spi); 3124718bad0eSVladimir Oltean 3125718bad0eSVladimir Oltean /* We need to send at least one 64-bit word of SPI payload per message 3126718bad0eSVladimir Oltean * in order to be able to make useful progress. 3127718bad0eSVladimir Oltean */ 3128718bad0eSVladimir Oltean if (max_msg < SJA1105_SIZE_SPI_MSG_HEADER + 8) { 3129718bad0eSVladimir Oltean dev_err(dev, "SPI master cannot send large enough buffers, aborting\n"); 3130718bad0eSVladimir Oltean return -EINVAL; 3131718bad0eSVladimir Oltean } 3132718bad0eSVladimir Oltean 3133718bad0eSVladimir Oltean priv->max_xfer_len = SJA1105_SIZE_SPI_MSG_MAXLEN; 3134718bad0eSVladimir Oltean if (priv->max_xfer_len > max_xfer) 3135718bad0eSVladimir Oltean priv->max_xfer_len = max_xfer; 3136718bad0eSVladimir Oltean if (priv->max_xfer_len > max_msg - SJA1105_SIZE_SPI_MSG_HEADER) 3137718bad0eSVladimir Oltean priv->max_xfer_len = max_msg - SJA1105_SIZE_SPI_MSG_HEADER; 3138718bad0eSVladimir Oltean 31398aa9ebccSVladimir Oltean priv->info = of_device_get_match_data(dev); 31408aa9ebccSVladimir Oltean 31418aa9ebccSVladimir Oltean /* Detect hardware device */ 31428aa9ebccSVladimir Oltean rc = sja1105_check_device_id(priv); 31438aa9ebccSVladimir Oltean if (rc < 0) { 31448aa9ebccSVladimir Oltean dev_err(dev, "Device ID check failed: %d\n", rc); 31458aa9ebccSVladimir Oltean return rc; 31468aa9ebccSVladimir Oltean } 31478aa9ebccSVladimir Oltean 31488aa9ebccSVladimir Oltean dev_info(dev, "Probed switch chip: %s\n", priv->info->name); 31498aa9ebccSVladimir Oltean 31507e99e347SVivien Didelot ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL); 31518aa9ebccSVladimir Oltean if (!ds) 31528aa9ebccSVladimir Oltean return -ENOMEM; 31538aa9ebccSVladimir Oltean 31547e99e347SVivien Didelot ds->dev = dev; 31553e77e59bSVladimir Oltean ds->num_ports = priv->info->num_ports; 31568aa9ebccSVladimir Oltean ds->ops = &sja1105_switch_ops; 31578aa9ebccSVladimir Oltean ds->priv = priv; 31588aa9ebccSVladimir Oltean priv->ds = ds; 31598aa9ebccSVladimir Oltean 3160844d7edcSVladimir Oltean tagger_data = &priv->tagger_data; 3161844d7edcSVladimir Oltean 3162d5a619bfSVivien Didelot mutex_init(&priv->ptp_data.lock); 3163d5a619bfSVivien Didelot mutex_init(&priv->mgmt_lock); 3164d5a619bfSVivien Didelot 3165d5a619bfSVivien Didelot sja1105_tas_setup(ds); 3166a6af7763SVladimir Oltean sja1105_flower_setup(ds); 3167d5a619bfSVivien Didelot 3168d5a619bfSVivien Didelot rc = dsa_register_switch(priv->ds); 3169d5a619bfSVivien Didelot if (rc) 3170328621f6SVladimir Oltean return rc; 3171d5a619bfSVivien Didelot 31724d752508SVladimir Oltean if (IS_ENABLED(CONFIG_NET_SCH_CBS)) { 31734d752508SVladimir Oltean priv->cbs = devm_kcalloc(dev, priv->info->num_cbs_shapers, 31744d752508SVladimir Oltean sizeof(struct sja1105_cbs_entry), 31754d752508SVladimir Oltean GFP_KERNEL); 3176dc596e3fSVladimir Oltean if (!priv->cbs) { 3177dc596e3fSVladimir Oltean rc = -ENOMEM; 3178dc596e3fSVladimir Oltean goto out_unregister_switch; 3179dc596e3fSVladimir Oltean } 31804d752508SVladimir Oltean } 31814d752508SVladimir Oltean 3182227d07a0SVladimir Oltean /* Connections between dsa_port and sja1105_port */ 3183542043e9SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 3184a68578c2SVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 3185a68578c2SVladimir Oltean struct dsa_port *dp = dsa_to_port(ds, port); 3186a68578c2SVladimir Oltean struct net_device *slave; 3187227d07a0SVladimir Oltean 3188a68578c2SVladimir Oltean if (!dsa_is_user_port(ds, port)) 3189a68578c2SVladimir Oltean continue; 3190a68578c2SVladimir Oltean 3191a68578c2SVladimir Oltean dp->priv = sp; 3192a68578c2SVladimir Oltean sp->dp = dp; 3193844d7edcSVladimir Oltean sp->data = tagger_data; 3194a68578c2SVladimir Oltean slave = dp->slave; 3195a68578c2SVladimir Oltean kthread_init_work(&sp->xmit_work, sja1105_port_deferred_xmit); 3196a68578c2SVladimir Oltean sp->xmit_worker = kthread_create_worker(0, "%s_xmit", 3197a68578c2SVladimir Oltean slave->name); 3198a68578c2SVladimir Oltean if (IS_ERR(sp->xmit_worker)) { 3199a68578c2SVladimir Oltean rc = PTR_ERR(sp->xmit_worker); 3200a68578c2SVladimir Oltean dev_err(ds->dev, 3201a68578c2SVladimir Oltean "failed to create deferred xmit thread: %d\n", 3202a68578c2SVladimir Oltean rc); 3203dc596e3fSVladimir Oltean goto out_destroy_workers; 3204a68578c2SVladimir Oltean } 3205a68578c2SVladimir Oltean skb_queue_head_init(&sp->xmit_queue); 320638b5beeaSVladimir Oltean sp->xmit_tpid = ETH_P_SJA1105; 3207227d07a0SVladimir Oltean } 3208227d07a0SVladimir Oltean 3209d5a619bfSVivien Didelot return 0; 3210dc596e3fSVladimir Oltean 3211dc596e3fSVladimir Oltean out_destroy_workers: 3212a68578c2SVladimir Oltean while (port-- > 0) { 3213a68578c2SVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 3214a68578c2SVladimir Oltean 3215a68578c2SVladimir Oltean if (!dsa_is_user_port(ds, port)) 3216a68578c2SVladimir Oltean continue; 3217a68578c2SVladimir Oltean 3218a68578c2SVladimir Oltean kthread_destroy_worker(sp->xmit_worker); 3219a68578c2SVladimir Oltean } 3220dc596e3fSVladimir Oltean 3221dc596e3fSVladimir Oltean out_unregister_switch: 3222dc596e3fSVladimir Oltean dsa_unregister_switch(ds); 3223dc596e3fSVladimir Oltean 3224a68578c2SVladimir Oltean return rc; 32258aa9ebccSVladimir Oltean } 32268aa9ebccSVladimir Oltean 32278aa9ebccSVladimir Oltean static int sja1105_remove(struct spi_device *spi) 32288aa9ebccSVladimir Oltean { 32298aa9ebccSVladimir Oltean struct sja1105_private *priv = spi_get_drvdata(spi); 3230cedf4670SVladimir Oltean struct dsa_switch *ds = priv->ds; 32318aa9ebccSVladimir Oltean 3232cedf4670SVladimir Oltean dsa_unregister_switch(ds); 3233cedf4670SVladimir Oltean 32348aa9ebccSVladimir Oltean return 0; 32358aa9ebccSVladimir Oltean } 32368aa9ebccSVladimir Oltean 32378aa9ebccSVladimir Oltean static const struct of_device_id sja1105_dt_ids[] = { 32388aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105e", .data = &sja1105e_info }, 32398aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105t", .data = &sja1105t_info }, 32408aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105p", .data = &sja1105p_info }, 32418aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105q", .data = &sja1105q_info }, 32428aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105r", .data = &sja1105r_info }, 32438aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105s", .data = &sja1105s_info }, 32443e77e59bSVladimir Oltean { .compatible = "nxp,sja1110a", .data = &sja1110a_info }, 32453e77e59bSVladimir Oltean { .compatible = "nxp,sja1110b", .data = &sja1110b_info }, 32463e77e59bSVladimir Oltean { .compatible = "nxp,sja1110c", .data = &sja1110c_info }, 32473e77e59bSVladimir Oltean { .compatible = "nxp,sja1110d", .data = &sja1110d_info }, 32488aa9ebccSVladimir Oltean { /* sentinel */ }, 32498aa9ebccSVladimir Oltean }; 32508aa9ebccSVladimir Oltean MODULE_DEVICE_TABLE(of, sja1105_dt_ids); 32518aa9ebccSVladimir Oltean 32528aa9ebccSVladimir Oltean static struct spi_driver sja1105_driver = { 32538aa9ebccSVladimir Oltean .driver = { 32548aa9ebccSVladimir Oltean .name = "sja1105", 32558aa9ebccSVladimir Oltean .owner = THIS_MODULE, 32568aa9ebccSVladimir Oltean .of_match_table = of_match_ptr(sja1105_dt_ids), 32578aa9ebccSVladimir Oltean }, 32588aa9ebccSVladimir Oltean .probe = sja1105_probe, 32598aa9ebccSVladimir Oltean .remove = sja1105_remove, 32608aa9ebccSVladimir Oltean }; 32618aa9ebccSVladimir Oltean 32628aa9ebccSVladimir Oltean module_spi_driver(sja1105_driver); 32638aa9ebccSVladimir Oltean 32648aa9ebccSVladimir Oltean MODULE_AUTHOR("Vladimir Oltean <olteanv@gmail.com>"); 32658aa9ebccSVladimir Oltean MODULE_AUTHOR("Georg Waibel <georg.waibel@sensor-technik.de>"); 32668aa9ebccSVladimir Oltean MODULE_DESCRIPTION("SJA1105 Driver"); 32678aa9ebccSVladimir Oltean MODULE_LICENSE("GPL v2"); 3268