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 294d942354SVladimir Oltean 308aa9ebccSVladimir Oltean static void sja1105_hw_reset(struct gpio_desc *gpio, unsigned int pulse_len, 318aa9ebccSVladimir Oltean unsigned int startup_delay) 328aa9ebccSVladimir Oltean { 338aa9ebccSVladimir Oltean gpiod_set_value_cansleep(gpio, 1); 348aa9ebccSVladimir Oltean /* Wait for minimum reset pulse length */ 358aa9ebccSVladimir Oltean msleep(pulse_len); 368aa9ebccSVladimir Oltean gpiod_set_value_cansleep(gpio, 0); 378aa9ebccSVladimir Oltean /* Wait until chip is ready after reset */ 388aa9ebccSVladimir Oltean msleep(startup_delay); 398aa9ebccSVladimir Oltean } 408aa9ebccSVladimir Oltean 418aa9ebccSVladimir Oltean static void 428aa9ebccSVladimir Oltean sja1105_port_allow_traffic(struct sja1105_l2_forwarding_entry *l2_fwd, 438aa9ebccSVladimir Oltean int from, int to, bool allow) 448aa9ebccSVladimir Oltean { 454d942354SVladimir Oltean if (allow) 468aa9ebccSVladimir Oltean l2_fwd[from].reach_port |= BIT(to); 474d942354SVladimir Oltean else 488aa9ebccSVladimir Oltean l2_fwd[from].reach_port &= ~BIT(to); 498aa9ebccSVladimir Oltean } 508aa9ebccSVladimir Oltean 517f7ccdeaSVladimir Oltean static bool sja1105_can_forward(struct sja1105_l2_forwarding_entry *l2_fwd, 527f7ccdeaSVladimir Oltean int from, int to) 537f7ccdeaSVladimir Oltean { 547f7ccdeaSVladimir Oltean return !!(l2_fwd[from].reach_port & BIT(to)); 557f7ccdeaSVladimir Oltean } 567f7ccdeaSVladimir Oltean 57bef0746cSVladimir Oltean static int sja1105_is_vlan_configured(struct sja1105_private *priv, u16 vid) 58bef0746cSVladimir Oltean { 59bef0746cSVladimir Oltean struct sja1105_vlan_lookup_entry *vlan; 60bef0746cSVladimir Oltean int count, i; 61bef0746cSVladimir Oltean 62bef0746cSVladimir Oltean vlan = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entries; 63bef0746cSVladimir Oltean count = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entry_count; 64bef0746cSVladimir Oltean 65bef0746cSVladimir Oltean for (i = 0; i < count; i++) 66bef0746cSVladimir Oltean if (vlan[i].vlanid == vid) 67bef0746cSVladimir Oltean return i; 68bef0746cSVladimir Oltean 69bef0746cSVladimir Oltean /* Return an invalid entry index if not found */ 70bef0746cSVladimir Oltean return -1; 71bef0746cSVladimir Oltean } 72bef0746cSVladimir Oltean 73bef0746cSVladimir Oltean static int sja1105_drop_untagged(struct dsa_switch *ds, int port, bool drop) 74bef0746cSVladimir Oltean { 75bef0746cSVladimir Oltean struct sja1105_private *priv = ds->priv; 76bef0746cSVladimir Oltean struct sja1105_mac_config_entry *mac; 77bef0746cSVladimir Oltean 78bef0746cSVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 79bef0746cSVladimir Oltean 80bef0746cSVladimir Oltean if (mac[port].drpuntag == drop) 81bef0746cSVladimir Oltean return 0; 82bef0746cSVladimir Oltean 83bef0746cSVladimir Oltean mac[port].drpuntag = drop; 84bef0746cSVladimir Oltean 85bef0746cSVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, 86bef0746cSVladimir Oltean &mac[port], true); 87bef0746cSVladimir Oltean } 88bef0746cSVladimir Oltean 89cde8078eSVladimir Oltean static int sja1105_pvid_apply(struct sja1105_private *priv, int port, u16 pvid) 90cde8078eSVladimir Oltean { 91cde8078eSVladimir Oltean struct sja1105_mac_config_entry *mac; 92cde8078eSVladimir Oltean 93cde8078eSVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 94cde8078eSVladimir Oltean 95cde8078eSVladimir Oltean if (mac[port].vlanid == pvid) 96cde8078eSVladimir Oltean return 0; 97cde8078eSVladimir Oltean 98cde8078eSVladimir Oltean mac[port].vlanid = pvid; 99cde8078eSVladimir Oltean 100cde8078eSVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, 101cde8078eSVladimir Oltean &mac[port], true); 102cde8078eSVladimir Oltean } 103cde8078eSVladimir Oltean 104cde8078eSVladimir Oltean static int sja1105_commit_pvid(struct dsa_switch *ds, int port) 105cde8078eSVladimir Oltean { 106cde8078eSVladimir Oltean struct dsa_port *dp = dsa_to_port(ds, port); 107cde8078eSVladimir Oltean struct sja1105_private *priv = ds->priv; 108bef0746cSVladimir Oltean struct sja1105_vlan_lookup_entry *vlan; 109bef0746cSVladimir Oltean bool drop_untagged = false; 110bef0746cSVladimir Oltean int match, rc; 111cde8078eSVladimir Oltean u16 pvid; 112cde8078eSVladimir Oltean 113cde8078eSVladimir Oltean if (dp->bridge_dev && br_vlan_enabled(dp->bridge_dev)) 114cde8078eSVladimir Oltean pvid = priv->bridge_pvid[port]; 115cde8078eSVladimir Oltean else 116cde8078eSVladimir Oltean pvid = priv->tag_8021q_pvid[port]; 117cde8078eSVladimir Oltean 118bef0746cSVladimir Oltean rc = sja1105_pvid_apply(priv, port, pvid); 119bef0746cSVladimir Oltean if (rc) 120bef0746cSVladimir Oltean return rc; 121bef0746cSVladimir Oltean 12273ceab83SVladimir Oltean /* Only force dropping of untagged packets when the port is under a 12373ceab83SVladimir Oltean * VLAN-aware bridge. When the tag_8021q pvid is used, we are 12473ceab83SVladimir Oltean * deliberately removing the RX VLAN from the port's VMEMB_PORT list, 12573ceab83SVladimir Oltean * to prevent DSA tag spoofing from the link partner. Untagged packets 12673ceab83SVladimir Oltean * are the only ones that should be received with tag_8021q, so 12773ceab83SVladimir Oltean * definitely don't drop them. 12873ceab83SVladimir Oltean */ 12973ceab83SVladimir Oltean if (pvid == priv->bridge_pvid[port]) { 130bef0746cSVladimir Oltean vlan = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entries; 131bef0746cSVladimir Oltean 132bef0746cSVladimir Oltean match = sja1105_is_vlan_configured(priv, pvid); 133bef0746cSVladimir Oltean 134bef0746cSVladimir Oltean if (match < 0 || !(vlan[match].vmemb_port & BIT(port))) 135bef0746cSVladimir Oltean drop_untagged = true; 13673ceab83SVladimir Oltean } 137bef0746cSVladimir Oltean 138*b0b8c67eSVladimir Oltean if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) 139*b0b8c67eSVladimir Oltean drop_untagged = true; 140*b0b8c67eSVladimir Oltean 141bef0746cSVladimir Oltean return sja1105_drop_untagged(ds, port, drop_untagged); 142cde8078eSVladimir Oltean } 143cde8078eSVladimir Oltean 1448aa9ebccSVladimir Oltean static int sja1105_init_mac_settings(struct sja1105_private *priv) 1458aa9ebccSVladimir Oltean { 1468aa9ebccSVladimir Oltean struct sja1105_mac_config_entry default_mac = { 1478aa9ebccSVladimir Oltean /* Enable all 8 priority queues on egress. 1488aa9ebccSVladimir Oltean * Every queue i holds top[i] - base[i] frames. 1498aa9ebccSVladimir Oltean * Sum of top[i] - base[i] is 511 (max hardware limit). 1508aa9ebccSVladimir Oltean */ 1518aa9ebccSVladimir Oltean .top = {0x3F, 0x7F, 0xBF, 0xFF, 0x13F, 0x17F, 0x1BF, 0x1FF}, 1528aa9ebccSVladimir Oltean .base = {0x0, 0x40, 0x80, 0xC0, 0x100, 0x140, 0x180, 0x1C0}, 1538aa9ebccSVladimir Oltean .enabled = {true, true, true, true, true, true, true, true}, 1548aa9ebccSVladimir Oltean /* Keep standard IFG of 12 bytes on egress. */ 1558aa9ebccSVladimir Oltean .ifg = 0, 1568aa9ebccSVladimir Oltean /* Always put the MAC speed in automatic mode, where it can be 1571fd4a173SVladimir Oltean * adjusted at runtime by PHYLINK. 1588aa9ebccSVladimir Oltean */ 15941fed17fSVladimir Oltean .speed = priv->info->port_speed[SJA1105_SPEED_AUTO], 1608aa9ebccSVladimir Oltean /* No static correction for 1-step 1588 events */ 1618aa9ebccSVladimir Oltean .tp_delin = 0, 1628aa9ebccSVladimir Oltean .tp_delout = 0, 1638aa9ebccSVladimir Oltean /* Disable aging for critical TTEthernet traffic */ 1648aa9ebccSVladimir Oltean .maxage = 0xFF, 1658aa9ebccSVladimir Oltean /* Internal VLAN (pvid) to apply to untagged ingress */ 1668aa9ebccSVladimir Oltean .vlanprio = 0, 167e3502b82SVladimir Oltean .vlanid = 1, 1688aa9ebccSVladimir Oltean .ing_mirr = false, 1698aa9ebccSVladimir Oltean .egr_mirr = false, 1708aa9ebccSVladimir Oltean /* Don't drop traffic with other EtherType than ETH_P_IP */ 1718aa9ebccSVladimir Oltean .drpnona664 = false, 1728aa9ebccSVladimir Oltean /* Don't drop double-tagged traffic */ 1738aa9ebccSVladimir Oltean .drpdtag = false, 1748aa9ebccSVladimir Oltean /* Don't drop untagged traffic */ 1758aa9ebccSVladimir Oltean .drpuntag = false, 1768aa9ebccSVladimir Oltean /* Don't retag 802.1p (VID 0) traffic with the pvid */ 1778aa9ebccSVladimir Oltean .retag = false, 178640f763fSVladimir Oltean /* Disable learning and I/O on user ports by default - 179640f763fSVladimir Oltean * STP will enable it. 180640f763fSVladimir Oltean */ 181640f763fSVladimir Oltean .dyn_learn = false, 1828aa9ebccSVladimir Oltean .egress = false, 1838aa9ebccSVladimir Oltean .ingress = false, 1848aa9ebccSVladimir Oltean }; 1858aa9ebccSVladimir Oltean struct sja1105_mac_config_entry *mac; 186542043e9SVladimir Oltean struct dsa_switch *ds = priv->ds; 1878aa9ebccSVladimir Oltean struct sja1105_table *table; 1885313a37bSVladimir Oltean struct dsa_port *dp; 1898aa9ebccSVladimir Oltean 1908aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_MAC_CONFIG]; 1918aa9ebccSVladimir Oltean 1928aa9ebccSVladimir Oltean /* Discard previous MAC Configuration Table */ 1938aa9ebccSVladimir Oltean if (table->entry_count) { 1948aa9ebccSVladimir Oltean kfree(table->entries); 1958aa9ebccSVladimir Oltean table->entry_count = 0; 1968aa9ebccSVladimir Oltean } 1978aa9ebccSVladimir Oltean 198fd6f2c25SVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 1998aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 2008aa9ebccSVladimir Oltean if (!table->entries) 2018aa9ebccSVladimir Oltean return -ENOMEM; 2028aa9ebccSVladimir Oltean 203fd6f2c25SVladimir Oltean table->entry_count = table->ops->max_entry_count; 2048aa9ebccSVladimir Oltean 2058aa9ebccSVladimir Oltean mac = table->entries; 2068aa9ebccSVladimir Oltean 2075313a37bSVladimir Oltean list_for_each_entry(dp, &ds->dst->ports, list) { 2085313a37bSVladimir Oltean if (dp->ds != ds) 2095313a37bSVladimir Oltean continue; 2105313a37bSVladimir Oltean 2115313a37bSVladimir Oltean mac[dp->index] = default_mac; 212b0b33b04SVladimir Oltean 213b0b33b04SVladimir Oltean /* Let sja1105_bridge_stp_state_set() keep address learning 21481d45898SVladimir Oltean * enabled for the DSA ports. CPU ports use software-assisted 21581d45898SVladimir Oltean * learning to ensure that only FDB entries belonging to the 21681d45898SVladimir Oltean * bridge are learned, and that they are learned towards all 21781d45898SVladimir Oltean * CPU ports in a cross-chip topology if multiple CPU ports 21881d45898SVladimir Oltean * exist. 219640f763fSVladimir Oltean */ 2205313a37bSVladimir Oltean if (dsa_port_is_dsa(dp)) 2215313a37bSVladimir Oltean dp->learning = true; 222*b0b8c67eSVladimir Oltean 223*b0b8c67eSVladimir Oltean /* Disallow untagged packets from being received on the 224*b0b8c67eSVladimir Oltean * CPU and DSA ports. 225*b0b8c67eSVladimir Oltean */ 226*b0b8c67eSVladimir Oltean if (dsa_port_is_cpu(dp) || dsa_port_is_dsa(dp)) 227*b0b8c67eSVladimir Oltean mac[dp->index].drpuntag = true; 228640f763fSVladimir Oltean } 2298aa9ebccSVladimir Oltean 2308aa9ebccSVladimir Oltean return 0; 2318aa9ebccSVladimir Oltean } 2328aa9ebccSVladimir Oltean 2335d645df9SVladimir Oltean static int sja1105_init_mii_settings(struct sja1105_private *priv) 2348aa9ebccSVladimir Oltean { 2358aa9ebccSVladimir Oltean struct device *dev = &priv->spidev->dev; 2368aa9ebccSVladimir Oltean struct sja1105_xmii_params_entry *mii; 237542043e9SVladimir Oltean struct dsa_switch *ds = priv->ds; 2388aa9ebccSVladimir Oltean struct sja1105_table *table; 2398aa9ebccSVladimir Oltean int i; 2408aa9ebccSVladimir Oltean 2418aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_XMII_PARAMS]; 2428aa9ebccSVladimir Oltean 2438aa9ebccSVladimir Oltean /* Discard previous xMII Mode Parameters Table */ 2448aa9ebccSVladimir Oltean if (table->entry_count) { 2458aa9ebccSVladimir Oltean kfree(table->entries); 2468aa9ebccSVladimir Oltean table->entry_count = 0; 2478aa9ebccSVladimir Oltean } 2488aa9ebccSVladimir Oltean 249fd6f2c25SVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 2508aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 2518aa9ebccSVladimir Oltean if (!table->entries) 2528aa9ebccSVladimir Oltean return -ENOMEM; 2538aa9ebccSVladimir Oltean 2541fd4a173SVladimir Oltean /* Override table based on PHYLINK DT bindings */ 255fd6f2c25SVladimir Oltean table->entry_count = table->ops->max_entry_count; 2568aa9ebccSVladimir Oltean 2578aa9ebccSVladimir Oltean mii = table->entries; 2588aa9ebccSVladimir Oltean 259542043e9SVladimir Oltean for (i = 0; i < ds->num_ports; i++) { 2605d645df9SVladimir Oltean sja1105_mii_role_t role = XMII_MAC; 2615d645df9SVladimir Oltean 262ee9d0cb6SVladimir Oltean if (dsa_is_unused_port(priv->ds, i)) 263ee9d0cb6SVladimir Oltean continue; 264ee9d0cb6SVladimir Oltean 2655d645df9SVladimir Oltean switch (priv->phy_mode[i]) { 2665a8f0974SVladimir Oltean case PHY_INTERFACE_MODE_INTERNAL: 2675a8f0974SVladimir Oltean if (priv->info->internal_phy[i] == SJA1105_NO_PHY) 2685a8f0974SVladimir Oltean goto unsupported; 2695a8f0974SVladimir Oltean 2705a8f0974SVladimir Oltean mii->xmii_mode[i] = XMII_MODE_MII; 2715a8f0974SVladimir Oltean if (priv->info->internal_phy[i] == SJA1105_PHY_BASE_TX) 2725a8f0974SVladimir Oltean mii->special[i] = true; 2735a8f0974SVladimir Oltean 2745a8f0974SVladimir Oltean break; 2755d645df9SVladimir Oltean case PHY_INTERFACE_MODE_REVMII: 2765d645df9SVladimir Oltean role = XMII_PHY; 2775d645df9SVladimir Oltean fallthrough; 2788aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_MII: 27991a05078SVladimir Oltean if (!priv->info->supports_mii[i]) 28091a05078SVladimir Oltean goto unsupported; 28191a05078SVladimir Oltean 2828aa9ebccSVladimir Oltean mii->xmii_mode[i] = XMII_MODE_MII; 2838aa9ebccSVladimir Oltean break; 2845d645df9SVladimir Oltean case PHY_INTERFACE_MODE_REVRMII: 2855d645df9SVladimir Oltean role = XMII_PHY; 2865d645df9SVladimir Oltean fallthrough; 2878aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RMII: 28891a05078SVladimir Oltean if (!priv->info->supports_rmii[i]) 28991a05078SVladimir Oltean goto unsupported; 29091a05078SVladimir Oltean 2918aa9ebccSVladimir Oltean mii->xmii_mode[i] = XMII_MODE_RMII; 2928aa9ebccSVladimir Oltean break; 2938aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RGMII: 2948aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RGMII_ID: 2958aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RGMII_RXID: 2968aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RGMII_TXID: 29791a05078SVladimir Oltean if (!priv->info->supports_rgmii[i]) 29891a05078SVladimir Oltean goto unsupported; 29991a05078SVladimir Oltean 3008aa9ebccSVladimir Oltean mii->xmii_mode[i] = XMII_MODE_RGMII; 3018aa9ebccSVladimir Oltean break; 302ffe10e67SVladimir Oltean case PHY_INTERFACE_MODE_SGMII: 30391a05078SVladimir Oltean if (!priv->info->supports_sgmii[i]) 30491a05078SVladimir Oltean goto unsupported; 30591a05078SVladimir Oltean 306ffe10e67SVladimir Oltean mii->xmii_mode[i] = XMII_MODE_SGMII; 307ece578bcSVladimir Oltean mii->special[i] = true; 308ffe10e67SVladimir Oltean break; 30991a05078SVladimir Oltean case PHY_INTERFACE_MODE_2500BASEX: 31091a05078SVladimir Oltean if (!priv->info->supports_2500basex[i]) 31191a05078SVladimir Oltean goto unsupported; 31291a05078SVladimir Oltean 31391a05078SVladimir Oltean mii->xmii_mode[i] = XMII_MODE_SGMII; 314ece578bcSVladimir Oltean mii->special[i] = true; 31591a05078SVladimir Oltean break; 31691a05078SVladimir Oltean unsupported: 3178aa9ebccSVladimir Oltean default: 31891a05078SVladimir Oltean dev_err(dev, "Unsupported PHY mode %s on port %d!\n", 3195d645df9SVladimir Oltean phy_modes(priv->phy_mode[i]), i); 3206729188dSVladimir Oltean return -EINVAL; 3218aa9ebccSVladimir Oltean } 3228aa9ebccSVladimir Oltean 3235d645df9SVladimir Oltean mii->phy_mac[i] = role; 3248aa9ebccSVladimir Oltean } 3258aa9ebccSVladimir Oltean return 0; 3268aa9ebccSVladimir Oltean } 3278aa9ebccSVladimir Oltean 3288aa9ebccSVladimir Oltean static int sja1105_init_static_fdb(struct sja1105_private *priv) 3298aa9ebccSVladimir Oltean { 3304d942354SVladimir Oltean struct sja1105_l2_lookup_entry *l2_lookup; 3318aa9ebccSVladimir Oltean struct sja1105_table *table; 3324d942354SVladimir Oltean int port; 3338aa9ebccSVladimir Oltean 3348aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; 3358aa9ebccSVladimir Oltean 3364d942354SVladimir Oltean /* We only populate the FDB table through dynamic L2 Address Lookup 3374d942354SVladimir Oltean * entries, except for a special entry at the end which is a catch-all 3384d942354SVladimir Oltean * for unknown multicast and will be used to control flooding domain. 339291d1e72SVladimir Oltean */ 3408aa9ebccSVladimir Oltean if (table->entry_count) { 3418aa9ebccSVladimir Oltean kfree(table->entries); 3428aa9ebccSVladimir Oltean table->entry_count = 0; 3438aa9ebccSVladimir Oltean } 3444d942354SVladimir Oltean 3454d942354SVladimir Oltean if (!priv->info->can_limit_mcast_flood) 3464d942354SVladimir Oltean return 0; 3474d942354SVladimir Oltean 3484d942354SVladimir Oltean table->entries = kcalloc(1, table->ops->unpacked_entry_size, 3494d942354SVladimir Oltean GFP_KERNEL); 3504d942354SVladimir Oltean if (!table->entries) 3514d942354SVladimir Oltean return -ENOMEM; 3524d942354SVladimir Oltean 3534d942354SVladimir Oltean table->entry_count = 1; 3544d942354SVladimir Oltean l2_lookup = table->entries; 3554d942354SVladimir Oltean 3564d942354SVladimir Oltean /* All L2 multicast addresses have an odd first octet */ 3574d942354SVladimir Oltean l2_lookup[0].macaddr = SJA1105_UNKNOWN_MULTICAST; 3584d942354SVladimir Oltean l2_lookup[0].mask_macaddr = SJA1105_UNKNOWN_MULTICAST; 3594d942354SVladimir Oltean l2_lookup[0].lockeds = true; 3604d942354SVladimir Oltean l2_lookup[0].index = SJA1105_MAX_L2_LOOKUP_COUNT - 1; 3614d942354SVladimir Oltean 3624d942354SVladimir Oltean /* Flood multicast to every port by default */ 3634d942354SVladimir Oltean for (port = 0; port < priv->ds->num_ports; port++) 3644d942354SVladimir Oltean if (!dsa_is_unused_port(priv->ds, port)) 3654d942354SVladimir Oltean l2_lookup[0].destports |= BIT(port); 3664d942354SVladimir Oltean 3678aa9ebccSVladimir Oltean return 0; 3688aa9ebccSVladimir Oltean } 3698aa9ebccSVladimir Oltean 3708aa9ebccSVladimir Oltean static int sja1105_init_l2_lookup_params(struct sja1105_private *priv) 3718aa9ebccSVladimir Oltean { 3728aa9ebccSVladimir Oltean struct sja1105_l2_lookup_params_entry default_l2_lookup_params = { 3738456721dSVladimir Oltean /* Learned FDB entries are forgotten after 300 seconds */ 3748456721dSVladimir Oltean .maxage = SJA1105_AGEING_TIME_MS(300000), 3758aa9ebccSVladimir Oltean /* All entries within a FDB bin are available for learning */ 3768aa9ebccSVladimir Oltean .dyn_tbsz = SJA1105ET_FDB_BIN_SIZE, 3771da73821SVladimir Oltean /* And the P/Q/R/S equivalent setting: */ 3781da73821SVladimir Oltean .start_dynspc = 0, 3798aa9ebccSVladimir Oltean /* 2^8 + 2^5 + 2^3 + 2^2 + 2^1 + 1 in Koopman notation */ 3808aa9ebccSVladimir Oltean .poly = 0x97, 3818aa9ebccSVladimir Oltean /* This selects between Independent VLAN Learning (IVL) and 3828aa9ebccSVladimir Oltean * Shared VLAN Learning (SVL) 3838aa9ebccSVladimir Oltean */ 3846d7c7d94SVladimir Oltean .shared_learn = true, 3858aa9ebccSVladimir Oltean /* Don't discard management traffic based on ENFPORT - 3868aa9ebccSVladimir Oltean * we don't perform SMAC port enforcement anyway, so 3878aa9ebccSVladimir Oltean * what we are setting here doesn't matter. 3888aa9ebccSVladimir Oltean */ 3898aa9ebccSVladimir Oltean .no_enf_hostprt = false, 3908aa9ebccSVladimir Oltean /* Don't learn SMAC for mac_fltres1 and mac_fltres0. 3918aa9ebccSVladimir Oltean * Maybe correlate with no_linklocal_learn from bridge driver? 3928aa9ebccSVladimir Oltean */ 3938aa9ebccSVladimir Oltean .no_mgmt_learn = true, 3941da73821SVladimir Oltean /* P/Q/R/S only */ 3951da73821SVladimir Oltean .use_static = true, 3961da73821SVladimir Oltean /* Dynamically learned FDB entries can overwrite other (older) 3971da73821SVladimir Oltean * dynamic FDB entries 3981da73821SVladimir Oltean */ 3991da73821SVladimir Oltean .owr_dyn = true, 4001da73821SVladimir Oltean .drpnolearn = true, 4018aa9ebccSVladimir Oltean }; 402542043e9SVladimir Oltean struct dsa_switch *ds = priv->ds; 403f238fef1SVladimir Oltean int port, num_used_ports = 0; 404542043e9SVladimir Oltean struct sja1105_table *table; 405542043e9SVladimir Oltean u64 max_fdb_entries; 406542043e9SVladimir Oltean 407542043e9SVladimir Oltean for (port = 0; port < ds->num_ports; port++) 408f238fef1SVladimir Oltean if (!dsa_is_unused_port(ds, port)) 409f238fef1SVladimir Oltean num_used_ports++; 410f238fef1SVladimir Oltean 411f238fef1SVladimir Oltean max_fdb_entries = SJA1105_MAX_L2_LOOKUP_COUNT / num_used_ports; 412f238fef1SVladimir Oltean 413f238fef1SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 414f238fef1SVladimir Oltean if (dsa_is_unused_port(ds, port)) 415f238fef1SVladimir Oltean continue; 416f238fef1SVladimir Oltean 417542043e9SVladimir Oltean default_l2_lookup_params.maxaddrp[port] = max_fdb_entries; 418f238fef1SVladimir Oltean } 4198aa9ebccSVladimir Oltean 4208aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS]; 4218aa9ebccSVladimir Oltean 4228aa9ebccSVladimir Oltean if (table->entry_count) { 4238aa9ebccSVladimir Oltean kfree(table->entries); 4248aa9ebccSVladimir Oltean table->entry_count = 0; 4258aa9ebccSVladimir Oltean } 4268aa9ebccSVladimir Oltean 427fd6f2c25SVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 4288aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 4298aa9ebccSVladimir Oltean if (!table->entries) 4308aa9ebccSVladimir Oltean return -ENOMEM; 4318aa9ebccSVladimir Oltean 432fd6f2c25SVladimir Oltean table->entry_count = table->ops->max_entry_count; 4338aa9ebccSVladimir Oltean 4348aa9ebccSVladimir Oltean /* This table only has a single entry */ 4358aa9ebccSVladimir Oltean ((struct sja1105_l2_lookup_params_entry *)table->entries)[0] = 4368aa9ebccSVladimir Oltean default_l2_lookup_params; 4378aa9ebccSVladimir Oltean 4388aa9ebccSVladimir Oltean return 0; 4398aa9ebccSVladimir Oltean } 4408aa9ebccSVladimir Oltean 441ed040abcSVladimir Oltean /* Set up a default VLAN for untagged traffic injected from the CPU 442ed040abcSVladimir Oltean * using management routes (e.g. STP, PTP) as opposed to tag_8021q. 443ed040abcSVladimir Oltean * All DT-defined ports are members of this VLAN, and there are no 444ed040abcSVladimir Oltean * restrictions on forwarding (since the CPU selects the destination). 445ed040abcSVladimir Oltean * Frames from this VLAN will always be transmitted as untagged, and 446ed040abcSVladimir Oltean * neither the bridge nor the 8021q module cannot create this VLAN ID. 447ed040abcSVladimir Oltean */ 4488aa9ebccSVladimir Oltean static int sja1105_init_static_vlan(struct sja1105_private *priv) 4498aa9ebccSVladimir Oltean { 4508aa9ebccSVladimir Oltean struct sja1105_table *table; 4518aa9ebccSVladimir Oltean struct sja1105_vlan_lookup_entry pvid = { 4523e77e59bSVladimir Oltean .type_entry = SJA1110_VLAN_D_TAG, 4538aa9ebccSVladimir Oltean .ving_mirr = 0, 4548aa9ebccSVladimir Oltean .vegr_mirr = 0, 4558aa9ebccSVladimir Oltean .vmemb_port = 0, 4568aa9ebccSVladimir Oltean .vlan_bc = 0, 4578aa9ebccSVladimir Oltean .tag_port = 0, 458ed040abcSVladimir Oltean .vlanid = SJA1105_DEFAULT_VLAN, 4598aa9ebccSVladimir Oltean }; 460ec5ae610SVladimir Oltean struct dsa_switch *ds = priv->ds; 461ec5ae610SVladimir Oltean int port; 4628aa9ebccSVladimir Oltean 4638aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP]; 4648aa9ebccSVladimir Oltean 4658aa9ebccSVladimir Oltean if (table->entry_count) { 4668aa9ebccSVladimir Oltean kfree(table->entries); 4678aa9ebccSVladimir Oltean table->entry_count = 0; 4688aa9ebccSVladimir Oltean } 4698aa9ebccSVladimir Oltean 470c75857b0SZheng Yongjun table->entries = kzalloc(table->ops->unpacked_entry_size, 4718aa9ebccSVladimir Oltean GFP_KERNEL); 4728aa9ebccSVladimir Oltean if (!table->entries) 4738aa9ebccSVladimir Oltean return -ENOMEM; 4748aa9ebccSVladimir Oltean 4758aa9ebccSVladimir Oltean table->entry_count = 1; 4768aa9ebccSVladimir Oltean 477ec5ae610SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 478ec5ae610SVladimir Oltean if (dsa_is_unused_port(ds, port)) 479ec5ae610SVladimir Oltean continue; 480ec5ae610SVladimir Oltean 481ec5ae610SVladimir Oltean pvid.vmemb_port |= BIT(port); 482ec5ae610SVladimir Oltean pvid.vlan_bc |= BIT(port); 483ec5ae610SVladimir Oltean pvid.tag_port &= ~BIT(port); 484ec5ae610SVladimir Oltean 485c5130029SVladimir Oltean if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) { 4866dfd23d3SVladimir Oltean priv->tag_8021q_pvid[port] = SJA1105_DEFAULT_VLAN; 4876dfd23d3SVladimir Oltean priv->bridge_pvid[port] = SJA1105_DEFAULT_VLAN; 4886dfd23d3SVladimir Oltean } 4898aa9ebccSVladimir Oltean } 4908aa9ebccSVladimir Oltean 4918aa9ebccSVladimir Oltean ((struct sja1105_vlan_lookup_entry *)table->entries)[0] = pvid; 4928aa9ebccSVladimir Oltean return 0; 4938aa9ebccSVladimir Oltean } 4948aa9ebccSVladimir Oltean 4958aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding(struct sja1105_private *priv) 4968aa9ebccSVladimir Oltean { 4978aa9ebccSVladimir Oltean struct sja1105_l2_forwarding_entry *l2fwd; 498542043e9SVladimir Oltean struct dsa_switch *ds = priv->ds; 4990f9b762cSVladimir Oltean struct dsa_switch_tree *dst; 5008aa9ebccSVladimir Oltean struct sja1105_table *table; 5010f9b762cSVladimir Oltean struct dsa_link *dl; 5023fa21270SVladimir Oltean int port, tc; 5033fa21270SVladimir Oltean int from, to; 5048aa9ebccSVladimir Oltean 5058aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING]; 5068aa9ebccSVladimir Oltean 5078aa9ebccSVladimir Oltean if (table->entry_count) { 5088aa9ebccSVladimir Oltean kfree(table->entries); 5098aa9ebccSVladimir Oltean table->entry_count = 0; 5108aa9ebccSVladimir Oltean } 5118aa9ebccSVladimir Oltean 512fd6f2c25SVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 5138aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 5148aa9ebccSVladimir Oltean if (!table->entries) 5158aa9ebccSVladimir Oltean return -ENOMEM; 5168aa9ebccSVladimir Oltean 517fd6f2c25SVladimir Oltean table->entry_count = table->ops->max_entry_count; 5188aa9ebccSVladimir Oltean 5198aa9ebccSVladimir Oltean l2fwd = table->entries; 5208aa9ebccSVladimir Oltean 5213fa21270SVladimir Oltean /* First 5 entries in the L2 Forwarding Table define the forwarding 5223fa21270SVladimir Oltean * rules and the VLAN PCP to ingress queue mapping. 5233fa21270SVladimir Oltean * Set up the ingress queue mapping first. 5247f7ccdeaSVladimir Oltean */ 5253fa21270SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 5263fa21270SVladimir Oltean if (dsa_is_unused_port(ds, port)) 5278aa9ebccSVladimir Oltean continue; 5288aa9ebccSVladimir Oltean 5293fa21270SVladimir Oltean for (tc = 0; tc < SJA1105_NUM_TC; tc++) 5303fa21270SVladimir Oltean l2fwd[port].vlan_pmap[tc] = tc; 5313fa21270SVladimir Oltean } 5324d942354SVladimir Oltean 5333fa21270SVladimir Oltean /* Then manage the forwarding domain for user ports. These can forward 5343fa21270SVladimir Oltean * only to the always-on domain (CPU port and DSA links) 5353fa21270SVladimir Oltean */ 5363fa21270SVladimir Oltean for (from = 0; from < ds->num_ports; from++) { 5373fa21270SVladimir Oltean if (!dsa_is_user_port(ds, from)) 5383fa21270SVladimir Oltean continue; 5394d942354SVladimir Oltean 5403fa21270SVladimir Oltean for (to = 0; to < ds->num_ports; to++) { 5413fa21270SVladimir Oltean if (!dsa_is_cpu_port(ds, to) && 5423fa21270SVladimir Oltean !dsa_is_dsa_port(ds, 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 5523fa21270SVladimir Oltean /* Then manage the forwarding domain for DSA links and CPU ports (the 5533fa21270SVladimir Oltean * always-on domain). These can send packets to any enabled port except 5543fa21270SVladimir Oltean * themselves. 5553fa21270SVladimir Oltean */ 5563fa21270SVladimir Oltean for (from = 0; from < ds->num_ports; from++) { 5573fa21270SVladimir Oltean if (!dsa_is_cpu_port(ds, from) && !dsa_is_dsa_port(ds, from)) 5583fa21270SVladimir Oltean continue; 5593fa21270SVladimir Oltean 5603fa21270SVladimir Oltean for (to = 0; to < ds->num_ports; to++) { 5613fa21270SVladimir Oltean if (dsa_is_unused_port(ds, to)) 5623fa21270SVladimir Oltean continue; 5633fa21270SVladimir Oltean 5643fa21270SVladimir Oltean if (from == to) 5653fa21270SVladimir Oltean continue; 5663fa21270SVladimir Oltean 5673fa21270SVladimir Oltean l2fwd[from].bc_domain |= BIT(to); 5683fa21270SVladimir Oltean l2fwd[from].fl_domain |= BIT(to); 5693fa21270SVladimir Oltean 5703fa21270SVladimir Oltean sja1105_port_allow_traffic(l2fwd, from, to, true); 5713fa21270SVladimir Oltean } 5723fa21270SVladimir Oltean } 5733fa21270SVladimir Oltean 5740f9b762cSVladimir Oltean /* In odd topologies ("H" connections where there is a DSA link to 5750f9b762cSVladimir Oltean * another switch which also has its own CPU port), TX packets can loop 5760f9b762cSVladimir Oltean * back into the system (they are flooded from CPU port 1 to the DSA 5770f9b762cSVladimir Oltean * link, and from there to CPU port 2). Prevent this from happening by 5780f9b762cSVladimir Oltean * cutting RX from DSA links towards our CPU port, if the remote switch 5790f9b762cSVladimir Oltean * has its own CPU port and therefore doesn't need ours for network 5800f9b762cSVladimir Oltean * stack termination. 5810f9b762cSVladimir Oltean */ 5820f9b762cSVladimir Oltean dst = ds->dst; 5830f9b762cSVladimir Oltean 5840f9b762cSVladimir Oltean list_for_each_entry(dl, &dst->rtable, list) { 5850f9b762cSVladimir Oltean if (dl->dp->ds != ds || dl->link_dp->cpu_dp == dl->dp->cpu_dp) 5860f9b762cSVladimir Oltean continue; 5870f9b762cSVladimir Oltean 5880f9b762cSVladimir Oltean from = dl->dp->index; 5890f9b762cSVladimir Oltean to = dsa_upstream_port(ds, from); 5900f9b762cSVladimir Oltean 5910f9b762cSVladimir Oltean dev_warn(ds->dev, 5920f9b762cSVladimir Oltean "H topology detected, cutting RX from DSA link %d to CPU port %d to prevent TX packet loops\n", 5930f9b762cSVladimir Oltean from, to); 5940f9b762cSVladimir Oltean 5950f9b762cSVladimir Oltean sja1105_port_allow_traffic(l2fwd, from, to, false); 5960f9b762cSVladimir Oltean 5970f9b762cSVladimir Oltean l2fwd[from].bc_domain &= ~BIT(to); 5980f9b762cSVladimir Oltean l2fwd[from].fl_domain &= ~BIT(to); 5990f9b762cSVladimir Oltean } 6000f9b762cSVladimir Oltean 6013fa21270SVladimir Oltean /* Finally, manage the egress flooding domain. All ports start up with 6023fa21270SVladimir Oltean * flooding enabled, including the CPU port and DSA links. 6033fa21270SVladimir Oltean */ 6043fa21270SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 6053fa21270SVladimir Oltean if (dsa_is_unused_port(ds, port)) 6063fa21270SVladimir Oltean continue; 6073fa21270SVladimir Oltean 6083fa21270SVladimir Oltean priv->ucast_egress_floods |= BIT(port); 6093fa21270SVladimir Oltean priv->bcast_egress_floods |= BIT(port); 6108aa9ebccSVladimir Oltean } 611f238fef1SVladimir Oltean 6128aa9ebccSVladimir Oltean /* Next 8 entries define VLAN PCP mapping from ingress to egress. 6138aa9ebccSVladimir Oltean * Create a one-to-one mapping. 6148aa9ebccSVladimir Oltean */ 6153fa21270SVladimir Oltean for (tc = 0; tc < SJA1105_NUM_TC; tc++) { 6163fa21270SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 6173fa21270SVladimir Oltean if (dsa_is_unused_port(ds, port)) 618f238fef1SVladimir Oltean continue; 619f238fef1SVladimir Oltean 6203fa21270SVladimir Oltean l2fwd[ds->num_ports + tc].vlan_pmap[port] = tc; 621f238fef1SVladimir Oltean } 6223e77e59bSVladimir Oltean 6233fa21270SVladimir Oltean l2fwd[ds->num_ports + tc].type_egrpcp2outputq = true; 6243e77e59bSVladimir Oltean } 6253e77e59bSVladimir Oltean 6263e77e59bSVladimir Oltean return 0; 6273e77e59bSVladimir Oltean } 6283e77e59bSVladimir Oltean 6293e77e59bSVladimir Oltean static int sja1110_init_pcp_remapping(struct sja1105_private *priv) 6303e77e59bSVladimir Oltean { 6313e77e59bSVladimir Oltean struct sja1110_pcp_remapping_entry *pcp_remap; 6323e77e59bSVladimir Oltean struct dsa_switch *ds = priv->ds; 6333e77e59bSVladimir Oltean struct sja1105_table *table; 6343e77e59bSVladimir Oltean int port, tc; 6353e77e59bSVladimir Oltean 6363e77e59bSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_PCP_REMAPPING]; 6373e77e59bSVladimir Oltean 6383e77e59bSVladimir Oltean /* Nothing to do for SJA1105 */ 6393e77e59bSVladimir Oltean if (!table->ops->max_entry_count) 6403e77e59bSVladimir Oltean return 0; 6413e77e59bSVladimir Oltean 6423e77e59bSVladimir Oltean if (table->entry_count) { 6433e77e59bSVladimir Oltean kfree(table->entries); 6443e77e59bSVladimir Oltean table->entry_count = 0; 6453e77e59bSVladimir Oltean } 6463e77e59bSVladimir Oltean 6473e77e59bSVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 6483e77e59bSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 6493e77e59bSVladimir Oltean if (!table->entries) 6503e77e59bSVladimir Oltean return -ENOMEM; 6513e77e59bSVladimir Oltean 6523e77e59bSVladimir Oltean table->entry_count = table->ops->max_entry_count; 6533e77e59bSVladimir Oltean 6543e77e59bSVladimir Oltean pcp_remap = table->entries; 6553e77e59bSVladimir Oltean 6563e77e59bSVladimir Oltean /* Repeat the configuration done for vlan_pmap */ 6573e77e59bSVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 6583e77e59bSVladimir Oltean if (dsa_is_unused_port(ds, port)) 6593e77e59bSVladimir Oltean continue; 6603e77e59bSVladimir Oltean 6613e77e59bSVladimir Oltean for (tc = 0; tc < SJA1105_NUM_TC; tc++) 6623e77e59bSVladimir Oltean pcp_remap[port].egrpcp[tc] = tc; 663f238fef1SVladimir Oltean } 6648aa9ebccSVladimir Oltean 6658aa9ebccSVladimir Oltean return 0; 6668aa9ebccSVladimir Oltean } 6678aa9ebccSVladimir Oltean 6688aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding_params(struct sja1105_private *priv) 6698aa9ebccSVladimir Oltean { 6701bf658eeSVladimir Oltean struct sja1105_l2_forwarding_params_entry *l2fwd_params; 6718aa9ebccSVladimir Oltean struct sja1105_table *table; 6728aa9ebccSVladimir Oltean 6738aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS]; 6748aa9ebccSVladimir Oltean 6758aa9ebccSVladimir Oltean if (table->entry_count) { 6768aa9ebccSVladimir Oltean kfree(table->entries); 6778aa9ebccSVladimir Oltean table->entry_count = 0; 6788aa9ebccSVladimir Oltean } 6798aa9ebccSVladimir Oltean 680fd6f2c25SVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 6818aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 6828aa9ebccSVladimir Oltean if (!table->entries) 6838aa9ebccSVladimir Oltean return -ENOMEM; 6848aa9ebccSVladimir Oltean 685fd6f2c25SVladimir Oltean table->entry_count = table->ops->max_entry_count; 6868aa9ebccSVladimir Oltean 6878aa9ebccSVladimir Oltean /* This table only has a single entry */ 6881bf658eeSVladimir Oltean l2fwd_params = table->entries; 6891bf658eeSVladimir Oltean 6901bf658eeSVladimir Oltean /* Disallow dynamic reconfiguration of vlan_pmap */ 6911bf658eeSVladimir Oltean l2fwd_params->max_dynp = 0; 6921bf658eeSVladimir Oltean /* Use a single memory partition for all ingress queues */ 6931bf658eeSVladimir Oltean l2fwd_params->part_spc[0] = priv->info->max_frame_mem; 6948aa9ebccSVladimir Oltean 6958aa9ebccSVladimir Oltean return 0; 6968aa9ebccSVladimir Oltean } 6978aa9ebccSVladimir Oltean 698aaa270c6SVladimir Oltean void sja1105_frame_memory_partitioning(struct sja1105_private *priv) 699aaa270c6SVladimir Oltean { 700aaa270c6SVladimir Oltean struct sja1105_l2_forwarding_params_entry *l2_fwd_params; 701aaa270c6SVladimir Oltean struct sja1105_vl_forwarding_params_entry *vl_fwd_params; 702aaa270c6SVladimir Oltean struct sja1105_table *table; 703aaa270c6SVladimir Oltean 704aaa270c6SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS]; 705aaa270c6SVladimir Oltean l2_fwd_params = table->entries; 7060fac6aa0SVladimir Oltean l2_fwd_params->part_spc[0] = SJA1105_MAX_FRAME_MEMORY; 707aaa270c6SVladimir Oltean 708aaa270c6SVladimir Oltean /* If we have any critical-traffic virtual links, we need to reserve 709aaa270c6SVladimir Oltean * some frame buffer memory for them. At the moment, hardcode the value 710aaa270c6SVladimir Oltean * at 100 blocks of 128 bytes of memory each. This leaves 829 blocks 711aaa270c6SVladimir Oltean * remaining for best-effort traffic. TODO: figure out a more flexible 712aaa270c6SVladimir Oltean * way to perform the frame buffer partitioning. 713aaa270c6SVladimir Oltean */ 714aaa270c6SVladimir Oltean if (!priv->static_config.tables[BLK_IDX_VL_FORWARDING].entry_count) 715aaa270c6SVladimir Oltean return; 716aaa270c6SVladimir Oltean 717aaa270c6SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_VL_FORWARDING_PARAMS]; 718aaa270c6SVladimir Oltean vl_fwd_params = table->entries; 719aaa270c6SVladimir Oltean 720aaa270c6SVladimir Oltean l2_fwd_params->part_spc[0] -= SJA1105_VL_FRAME_MEMORY; 721aaa270c6SVladimir Oltean vl_fwd_params->partspc[0] = SJA1105_VL_FRAME_MEMORY; 722aaa270c6SVladimir Oltean } 723aaa270c6SVladimir Oltean 724ceec8bc0SVladimir Oltean /* SJA1110 TDMACONFIGIDX values: 725ceec8bc0SVladimir Oltean * 726ceec8bc0SVladimir Oltean * | 100 Mbps ports | 1Gbps ports | 2.5Gbps ports | Disabled ports 727ceec8bc0SVladimir Oltean * -----+----------------+---------------+---------------+--------------- 728ceec8bc0SVladimir Oltean * 0 | 0, [5:10] | [1:2] | [3:4] | retag 729ceec8bc0SVladimir Oltean * 1 |0, [5:10], retag| [1:2] | [3:4] | - 730ceec8bc0SVladimir Oltean * 2 | 0, [5:10] | [1:3], retag | 4 | - 731ceec8bc0SVladimir Oltean * 3 | 0, [5:10] |[1:2], 4, retag| 3 | - 732ceec8bc0SVladimir Oltean * 4 | 0, 2, [5:10] | 1, retag | [3:4] | - 733ceec8bc0SVladimir Oltean * 5 | 0, 1, [5:10] | 2, retag | [3:4] | - 734ceec8bc0SVladimir Oltean * 14 | 0, [5:10] | [1:4], retag | - | - 735ceec8bc0SVladimir Oltean * 15 | [5:10] | [0:4], retag | - | - 736ceec8bc0SVladimir Oltean */ 737ceec8bc0SVladimir Oltean static void sja1110_select_tdmaconfigidx(struct sja1105_private *priv) 738ceec8bc0SVladimir Oltean { 739ceec8bc0SVladimir Oltean struct sja1105_general_params_entry *general_params; 740ceec8bc0SVladimir Oltean struct sja1105_table *table; 741ceec8bc0SVladimir Oltean bool port_1_is_base_tx; 742ceec8bc0SVladimir Oltean bool port_3_is_2500; 743ceec8bc0SVladimir Oltean bool port_4_is_2500; 744ceec8bc0SVladimir Oltean u64 tdmaconfigidx; 745ceec8bc0SVladimir Oltean 746ceec8bc0SVladimir Oltean if (priv->info->device_id != SJA1110_DEVICE_ID) 747ceec8bc0SVladimir Oltean return; 748ceec8bc0SVladimir Oltean 749ceec8bc0SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; 750ceec8bc0SVladimir Oltean general_params = table->entries; 751ceec8bc0SVladimir Oltean 752ceec8bc0SVladimir Oltean /* All the settings below are "as opposed to SGMII", which is the 753ceec8bc0SVladimir Oltean * other pinmuxing option. 754ceec8bc0SVladimir Oltean */ 755ceec8bc0SVladimir Oltean port_1_is_base_tx = priv->phy_mode[1] == PHY_INTERFACE_MODE_INTERNAL; 756ceec8bc0SVladimir Oltean port_3_is_2500 = priv->phy_mode[3] == PHY_INTERFACE_MODE_2500BASEX; 757ceec8bc0SVladimir Oltean port_4_is_2500 = priv->phy_mode[4] == PHY_INTERFACE_MODE_2500BASEX; 758ceec8bc0SVladimir Oltean 759ceec8bc0SVladimir Oltean if (port_1_is_base_tx) 760ceec8bc0SVladimir Oltean /* Retagging port will operate at 1 Gbps */ 761ceec8bc0SVladimir Oltean tdmaconfigidx = 5; 762ceec8bc0SVladimir Oltean else if (port_3_is_2500 && port_4_is_2500) 763ceec8bc0SVladimir Oltean /* Retagging port will operate at 100 Mbps */ 764ceec8bc0SVladimir Oltean tdmaconfigidx = 1; 765ceec8bc0SVladimir Oltean else if (port_3_is_2500) 766ceec8bc0SVladimir Oltean /* Retagging port will operate at 1 Gbps */ 767ceec8bc0SVladimir Oltean tdmaconfigidx = 3; 768ceec8bc0SVladimir Oltean else if (port_4_is_2500) 769ceec8bc0SVladimir Oltean /* Retagging port will operate at 1 Gbps */ 770ceec8bc0SVladimir Oltean tdmaconfigidx = 2; 771ceec8bc0SVladimir Oltean else 772ceec8bc0SVladimir Oltean /* Retagging port will operate at 1 Gbps */ 773ceec8bc0SVladimir Oltean tdmaconfigidx = 14; 774ceec8bc0SVladimir Oltean 775ceec8bc0SVladimir Oltean general_params->tdmaconfigidx = tdmaconfigidx; 776ceec8bc0SVladimir Oltean } 777ceec8bc0SVladimir Oltean 77830a100e6SVladimir Oltean static int sja1105_init_topology(struct sja1105_private *priv, 77930a100e6SVladimir Oltean struct sja1105_general_params_entry *general_params) 78030a100e6SVladimir Oltean { 78130a100e6SVladimir Oltean struct dsa_switch *ds = priv->ds; 78230a100e6SVladimir Oltean int port; 78330a100e6SVladimir Oltean 78430a100e6SVladimir Oltean /* The host port is the destination for traffic matching mac_fltres1 78530a100e6SVladimir Oltean * and mac_fltres0 on all ports except itself. Default to an invalid 78630a100e6SVladimir Oltean * value. 78730a100e6SVladimir Oltean */ 78830a100e6SVladimir Oltean general_params->host_port = ds->num_ports; 78930a100e6SVladimir Oltean 79030a100e6SVladimir Oltean /* Link-local traffic received on casc_port will be forwarded 79130a100e6SVladimir Oltean * to host_port without embedding the source port and device ID 79230a100e6SVladimir Oltean * info in the destination MAC address, and no RX timestamps will be 79330a100e6SVladimir Oltean * taken either (presumably because it is a cascaded port and a 79430a100e6SVladimir Oltean * downstream SJA switch already did that). 79530a100e6SVladimir Oltean * To disable the feature, we need to do different things depending on 79630a100e6SVladimir Oltean * switch generation. On SJA1105 we need to set an invalid port, while 79730a100e6SVladimir Oltean * on SJA1110 which support multiple cascaded ports, this field is a 79830a100e6SVladimir Oltean * bitmask so it must be left zero. 79930a100e6SVladimir Oltean */ 80030a100e6SVladimir Oltean if (!priv->info->multiple_cascade_ports) 80130a100e6SVladimir Oltean general_params->casc_port = ds->num_ports; 80230a100e6SVladimir Oltean 80330a100e6SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 80430a100e6SVladimir Oltean bool is_upstream = dsa_is_upstream_port(ds, port); 80530a100e6SVladimir Oltean bool is_dsa_link = dsa_is_dsa_port(ds, port); 80630a100e6SVladimir Oltean 80730a100e6SVladimir Oltean /* Upstream ports can be dedicated CPU ports or 80830a100e6SVladimir Oltean * upstream-facing DSA links 80930a100e6SVladimir Oltean */ 81030a100e6SVladimir Oltean if (is_upstream) { 81130a100e6SVladimir Oltean if (general_params->host_port == ds->num_ports) { 81230a100e6SVladimir Oltean general_params->host_port = port; 81330a100e6SVladimir Oltean } else { 81430a100e6SVladimir Oltean dev_err(ds->dev, 81530a100e6SVladimir Oltean "Port %llu is already a host port, configuring %d as one too is not supported\n", 81630a100e6SVladimir Oltean general_params->host_port, port); 81730a100e6SVladimir Oltean return -EINVAL; 81830a100e6SVladimir Oltean } 81930a100e6SVladimir Oltean } 82030a100e6SVladimir Oltean 82130a100e6SVladimir Oltean /* Cascade ports are downstream-facing DSA links */ 82230a100e6SVladimir Oltean if (is_dsa_link && !is_upstream) { 82330a100e6SVladimir Oltean if (priv->info->multiple_cascade_ports) { 82430a100e6SVladimir Oltean general_params->casc_port |= BIT(port); 82530a100e6SVladimir Oltean } else if (general_params->casc_port == ds->num_ports) { 82630a100e6SVladimir Oltean general_params->casc_port = port; 82730a100e6SVladimir Oltean } else { 82830a100e6SVladimir Oltean dev_err(ds->dev, 82930a100e6SVladimir Oltean "Port %llu is already a cascade port, configuring %d as one too is not supported\n", 83030a100e6SVladimir Oltean general_params->casc_port, port); 83130a100e6SVladimir Oltean return -EINVAL; 83230a100e6SVladimir Oltean } 83330a100e6SVladimir Oltean } 83430a100e6SVladimir Oltean } 83530a100e6SVladimir Oltean 83630a100e6SVladimir Oltean if (general_params->host_port == ds->num_ports) { 83730a100e6SVladimir Oltean dev_err(ds->dev, "No host port configured\n"); 83830a100e6SVladimir Oltean return -EINVAL; 83930a100e6SVladimir Oltean } 84030a100e6SVladimir Oltean 84130a100e6SVladimir Oltean return 0; 84230a100e6SVladimir Oltean } 84330a100e6SVladimir Oltean 8448aa9ebccSVladimir Oltean static int sja1105_init_general_params(struct sja1105_private *priv) 8458aa9ebccSVladimir Oltean { 8468aa9ebccSVladimir Oltean struct sja1105_general_params_entry default_general_params = { 847511e6ca0SVladimir Oltean /* Allow dynamic changing of the mirror port */ 848511e6ca0SVladimir Oltean .mirr_ptacu = true, 8498aa9ebccSVladimir Oltean .switchid = priv->ds->index, 8505f06c63bSVladimir Oltean /* Priority queue for link-local management frames 8515f06c63bSVladimir Oltean * (both ingress to and egress from CPU - PTP, STP etc) 8525f06c63bSVladimir Oltean */ 85308fde09aSVladimir Oltean .hostprio = 7, 8548aa9ebccSVladimir Oltean .mac_fltres1 = SJA1105_LINKLOCAL_FILTER_A, 8558aa9ebccSVladimir Oltean .mac_flt1 = SJA1105_LINKLOCAL_FILTER_A_MASK, 85642824463SVladimir Oltean .incl_srcpt1 = false, 8578aa9ebccSVladimir Oltean .send_meta1 = false, 8588aa9ebccSVladimir Oltean .mac_fltres0 = SJA1105_LINKLOCAL_FILTER_B, 8598aa9ebccSVladimir Oltean .mac_flt0 = SJA1105_LINKLOCAL_FILTER_B_MASK, 86042824463SVladimir Oltean .incl_srcpt0 = false, 8618aa9ebccSVladimir Oltean .send_meta0 = false, 862511e6ca0SVladimir Oltean /* Default to an invalid value */ 863542043e9SVladimir Oltean .mirr_port = priv->ds->num_ports, 8648aa9ebccSVladimir Oltean /* No TTEthernet */ 865dfacc5a2SVladimir Oltean .vllupformat = SJA1105_VL_FORMAT_PSFP, 8668aa9ebccSVladimir Oltean .vlmarker = 0, 8678aa9ebccSVladimir Oltean .vlmask = 0, 8688aa9ebccSVladimir Oltean /* Only update correctionField for 1-step PTP (L2 transport) */ 8698aa9ebccSVladimir Oltean .ignore2stf = 0, 8706666cebcSVladimir Oltean /* Forcefully disable VLAN filtering by telling 8716666cebcSVladimir Oltean * the switch that VLAN has a different EtherType. 8726666cebcSVladimir Oltean */ 8736666cebcSVladimir Oltean .tpid = ETH_P_SJA1105, 8746666cebcSVladimir Oltean .tpid2 = ETH_P_SJA1105, 87529305260SVladimir Oltean /* Enable the TTEthernet engine on SJA1110 */ 87629305260SVladimir Oltean .tte_en = true, 8774913b8ebSVladimir Oltean /* Set up the EtherType for control packets on SJA1110 */ 8784913b8ebSVladimir Oltean .header_type = ETH_P_SJA1110, 8798aa9ebccSVladimir Oltean }; 8806c0de59bSVladimir Oltean struct sja1105_general_params_entry *general_params; 8818aa9ebccSVladimir Oltean struct sja1105_table *table; 88230a100e6SVladimir Oltean int rc; 883df2a81a3SVladimir Oltean 88430a100e6SVladimir Oltean rc = sja1105_init_topology(priv, &default_general_params); 88530a100e6SVladimir Oltean if (rc) 88630a100e6SVladimir Oltean return rc; 8878aa9ebccSVladimir Oltean 8888aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; 8898aa9ebccSVladimir Oltean 8908aa9ebccSVladimir Oltean if (table->entry_count) { 8918aa9ebccSVladimir Oltean kfree(table->entries); 8928aa9ebccSVladimir Oltean table->entry_count = 0; 8938aa9ebccSVladimir Oltean } 8948aa9ebccSVladimir Oltean 895fd6f2c25SVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 8968aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 8978aa9ebccSVladimir Oltean if (!table->entries) 8988aa9ebccSVladimir Oltean return -ENOMEM; 8998aa9ebccSVladimir Oltean 900fd6f2c25SVladimir Oltean table->entry_count = table->ops->max_entry_count; 9018aa9ebccSVladimir Oltean 9026c0de59bSVladimir Oltean general_params = table->entries; 9036c0de59bSVladimir Oltean 9048aa9ebccSVladimir Oltean /* This table only has a single entry */ 9056c0de59bSVladimir Oltean general_params[0] = default_general_params; 9068aa9ebccSVladimir Oltean 907ceec8bc0SVladimir Oltean sja1110_select_tdmaconfigidx(priv); 908ceec8bc0SVladimir Oltean 9098aa9ebccSVladimir Oltean return 0; 9108aa9ebccSVladimir Oltean } 9118aa9ebccSVladimir Oltean 91279d5511cSVladimir Oltean static int sja1105_init_avb_params(struct sja1105_private *priv) 91379d5511cSVladimir Oltean { 91479d5511cSVladimir Oltean struct sja1105_avb_params_entry *avb; 91579d5511cSVladimir Oltean struct sja1105_table *table; 91679d5511cSVladimir Oltean 91779d5511cSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_AVB_PARAMS]; 91879d5511cSVladimir Oltean 91979d5511cSVladimir Oltean /* Discard previous AVB Parameters Table */ 92079d5511cSVladimir Oltean if (table->entry_count) { 92179d5511cSVladimir Oltean kfree(table->entries); 92279d5511cSVladimir Oltean table->entry_count = 0; 92379d5511cSVladimir Oltean } 92479d5511cSVladimir Oltean 925fd6f2c25SVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 92679d5511cSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 92779d5511cSVladimir Oltean if (!table->entries) 92879d5511cSVladimir Oltean return -ENOMEM; 92979d5511cSVladimir Oltean 930fd6f2c25SVladimir Oltean table->entry_count = table->ops->max_entry_count; 93179d5511cSVladimir Oltean 93279d5511cSVladimir Oltean avb = table->entries; 93379d5511cSVladimir Oltean 93479d5511cSVladimir Oltean /* Configure the MAC addresses for meta frames */ 93579d5511cSVladimir Oltean avb->destmeta = SJA1105_META_DMAC; 93679d5511cSVladimir Oltean avb->srcmeta = SJA1105_META_SMAC; 937747e5eb3SVladimir Oltean /* On P/Q/R/S, configure the direction of the PTP_CLK pin as input by 938747e5eb3SVladimir Oltean * default. This is because there might be boards with a hardware 939747e5eb3SVladimir Oltean * layout where enabling the pin as output might cause an electrical 940747e5eb3SVladimir Oltean * clash. On E/T the pin is always an output, which the board designers 941747e5eb3SVladimir Oltean * probably already knew, so even if there are going to be electrical 942747e5eb3SVladimir Oltean * issues, there's nothing we can do. 943747e5eb3SVladimir Oltean */ 944747e5eb3SVladimir Oltean avb->cas_master = false; 94579d5511cSVladimir Oltean 94679d5511cSVladimir Oltean return 0; 94779d5511cSVladimir Oltean } 94879d5511cSVladimir Oltean 949a7cc081cSVladimir Oltean /* The L2 policing table is 2-stage. The table is looked up for each frame 950a7cc081cSVladimir Oltean * according to the ingress port, whether it was broadcast or not, and the 951a7cc081cSVladimir Oltean * classified traffic class (given by VLAN PCP). This portion of the lookup is 952a7cc081cSVladimir Oltean * fixed, and gives access to the SHARINDX, an indirection register pointing 953a7cc081cSVladimir Oltean * within the policing table itself, which is used to resolve the policer that 954a7cc081cSVladimir Oltean * will be used for this frame. 955a7cc081cSVladimir Oltean * 956a7cc081cSVladimir Oltean * Stage 1 Stage 2 957a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 958a7cc081cSVladimir Oltean * |Port 0 TC 0 |SHARINDX| | Policer 0: Rate, Burst, MTU | 959a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 960a7cc081cSVladimir Oltean * |Port 0 TC 1 |SHARINDX| | Policer 1: Rate, Burst, MTU | 961a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 962a7cc081cSVladimir Oltean * ... | Policer 2: Rate, Burst, MTU | 963a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 964a7cc081cSVladimir Oltean * |Port 0 TC 7 |SHARINDX| | Policer 3: Rate, Burst, MTU | 965a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 966a7cc081cSVladimir Oltean * |Port 1 TC 0 |SHARINDX| | Policer 4: Rate, Burst, MTU | 967a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 968a7cc081cSVladimir Oltean * ... | Policer 5: Rate, Burst, MTU | 969a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 970a7cc081cSVladimir Oltean * |Port 1 TC 7 |SHARINDX| | Policer 6: Rate, Burst, MTU | 971a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 972a7cc081cSVladimir Oltean * ... | Policer 7: Rate, Burst, MTU | 973a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 974a7cc081cSVladimir Oltean * |Port 4 TC 7 |SHARINDX| ... 975a7cc081cSVladimir Oltean * +------------+--------+ 976a7cc081cSVladimir Oltean * |Port 0 BCAST|SHARINDX| ... 977a7cc081cSVladimir Oltean * +------------+--------+ 978a7cc081cSVladimir Oltean * |Port 1 BCAST|SHARINDX| ... 979a7cc081cSVladimir Oltean * +------------+--------+ 980a7cc081cSVladimir Oltean * ... ... 981a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 982a7cc081cSVladimir Oltean * |Port 4 BCAST|SHARINDX| | Policer 44: Rate, Burst, MTU | 983a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 984a7cc081cSVladimir Oltean * 985a7cc081cSVladimir Oltean * In this driver, we shall use policers 0-4 as statically alocated port 986a7cc081cSVladimir Oltean * (matchall) policers. So we need to make the SHARINDX for all lookups 987a7cc081cSVladimir Oltean * corresponding to this ingress port (8 VLAN PCP lookups and 1 broadcast 988a7cc081cSVladimir Oltean * lookup) equal. 989a7cc081cSVladimir Oltean * The remaining policers (40) shall be dynamically allocated for flower 990a7cc081cSVladimir Oltean * policers, where the key is either vlan_prio or dst_mac ff:ff:ff:ff:ff:ff. 991a7cc081cSVladimir Oltean */ 9928aa9ebccSVladimir Oltean #define SJA1105_RATE_MBPS(speed) (((speed) * 64000) / 1000) 9938aa9ebccSVladimir Oltean 9948aa9ebccSVladimir Oltean static int sja1105_init_l2_policing(struct sja1105_private *priv) 9958aa9ebccSVladimir Oltean { 9968aa9ebccSVladimir Oltean struct sja1105_l2_policing_entry *policing; 997542043e9SVladimir Oltean struct dsa_switch *ds = priv->ds; 9988aa9ebccSVladimir Oltean struct sja1105_table *table; 999a7cc081cSVladimir Oltean int port, tc; 10008aa9ebccSVladimir Oltean 10018aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_POLICING]; 10028aa9ebccSVladimir Oltean 10038aa9ebccSVladimir Oltean /* Discard previous L2 Policing Table */ 10048aa9ebccSVladimir Oltean if (table->entry_count) { 10058aa9ebccSVladimir Oltean kfree(table->entries); 10068aa9ebccSVladimir Oltean table->entry_count = 0; 10078aa9ebccSVladimir Oltean } 10088aa9ebccSVladimir Oltean 1009fd6f2c25SVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 10108aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 10118aa9ebccSVladimir Oltean if (!table->entries) 10128aa9ebccSVladimir Oltean return -ENOMEM; 10138aa9ebccSVladimir Oltean 1014fd6f2c25SVladimir Oltean table->entry_count = table->ops->max_entry_count; 10158aa9ebccSVladimir Oltean 10168aa9ebccSVladimir Oltean policing = table->entries; 10178aa9ebccSVladimir Oltean 1018a7cc081cSVladimir Oltean /* Setup shared indices for the matchall policers */ 1019542043e9SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 102038fbe91fSVladimir Oltean int mcast = (ds->num_ports * (SJA1105_NUM_TC + 1)) + port; 1021542043e9SVladimir Oltean int bcast = (ds->num_ports * SJA1105_NUM_TC) + port; 1022a7cc081cSVladimir Oltean 1023a7cc081cSVladimir Oltean for (tc = 0; tc < SJA1105_NUM_TC; tc++) 1024a7cc081cSVladimir Oltean policing[port * SJA1105_NUM_TC + tc].sharindx = port; 1025a7cc081cSVladimir Oltean 1026a7cc081cSVladimir Oltean policing[bcast].sharindx = port; 102738fbe91fSVladimir Oltean /* Only SJA1110 has multicast policers */ 102838fbe91fSVladimir Oltean if (mcast <= table->ops->max_entry_count) 102938fbe91fSVladimir Oltean policing[mcast].sharindx = port; 1030a7cc081cSVladimir Oltean } 1031a7cc081cSVladimir Oltean 1032a7cc081cSVladimir Oltean /* Setup the matchall policer parameters */ 1033542043e9SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 1034c279c726SVladimir Oltean int mtu = VLAN_ETH_FRAME_LEN + ETH_FCS_LEN; 1035c279c726SVladimir Oltean 1036777e55e3SVladimir Oltean if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) 1037c279c726SVladimir Oltean mtu += VLAN_HLEN; 10388aa9ebccSVladimir Oltean 1039a7cc081cSVladimir Oltean policing[port].smax = 65535; /* Burst size in bytes */ 1040a7cc081cSVladimir Oltean policing[port].rate = SJA1105_RATE_MBPS(1000); 1041a7cc081cSVladimir Oltean policing[port].maxlen = mtu; 1042a7cc081cSVladimir Oltean policing[port].partition = 0; 10438aa9ebccSVladimir Oltean } 1044a7cc081cSVladimir Oltean 10458aa9ebccSVladimir Oltean return 0; 10468aa9ebccSVladimir Oltean } 10478aa9ebccSVladimir Oltean 10485d645df9SVladimir Oltean static int sja1105_static_config_load(struct sja1105_private *priv) 10498aa9ebccSVladimir Oltean { 10508aa9ebccSVladimir Oltean int rc; 10518aa9ebccSVladimir Oltean 10528aa9ebccSVladimir Oltean sja1105_static_config_free(&priv->static_config); 10538aa9ebccSVladimir Oltean rc = sja1105_static_config_init(&priv->static_config, 10548aa9ebccSVladimir Oltean priv->info->static_ops, 10558aa9ebccSVladimir Oltean priv->info->device_id); 10568aa9ebccSVladimir Oltean if (rc) 10578aa9ebccSVladimir Oltean return rc; 10588aa9ebccSVladimir Oltean 10598aa9ebccSVladimir Oltean /* Build static configuration */ 10608aa9ebccSVladimir Oltean rc = sja1105_init_mac_settings(priv); 10618aa9ebccSVladimir Oltean if (rc < 0) 10628aa9ebccSVladimir Oltean return rc; 10635d645df9SVladimir Oltean rc = sja1105_init_mii_settings(priv); 10648aa9ebccSVladimir Oltean if (rc < 0) 10658aa9ebccSVladimir Oltean return rc; 10668aa9ebccSVladimir Oltean rc = sja1105_init_static_fdb(priv); 10678aa9ebccSVladimir Oltean if (rc < 0) 10688aa9ebccSVladimir Oltean return rc; 10698aa9ebccSVladimir Oltean rc = sja1105_init_static_vlan(priv); 10708aa9ebccSVladimir Oltean if (rc < 0) 10718aa9ebccSVladimir Oltean return rc; 10728aa9ebccSVladimir Oltean rc = sja1105_init_l2_lookup_params(priv); 10738aa9ebccSVladimir Oltean if (rc < 0) 10748aa9ebccSVladimir Oltean return rc; 10758aa9ebccSVladimir Oltean rc = sja1105_init_l2_forwarding(priv); 10768aa9ebccSVladimir Oltean if (rc < 0) 10778aa9ebccSVladimir Oltean return rc; 10788aa9ebccSVladimir Oltean rc = sja1105_init_l2_forwarding_params(priv); 10798aa9ebccSVladimir Oltean if (rc < 0) 10808aa9ebccSVladimir Oltean return rc; 10818aa9ebccSVladimir Oltean rc = sja1105_init_l2_policing(priv); 10828aa9ebccSVladimir Oltean if (rc < 0) 10838aa9ebccSVladimir Oltean return rc; 10848aa9ebccSVladimir Oltean rc = sja1105_init_general_params(priv); 10858aa9ebccSVladimir Oltean if (rc < 0) 10868aa9ebccSVladimir Oltean return rc; 108779d5511cSVladimir Oltean rc = sja1105_init_avb_params(priv); 108879d5511cSVladimir Oltean if (rc < 0) 108979d5511cSVladimir Oltean return rc; 10903e77e59bSVladimir Oltean rc = sja1110_init_pcp_remapping(priv); 10913e77e59bSVladimir Oltean if (rc < 0) 10923e77e59bSVladimir Oltean return rc; 10938aa9ebccSVladimir Oltean 10948aa9ebccSVladimir Oltean /* Send initial configuration to hardware via SPI */ 10958aa9ebccSVladimir Oltean return sja1105_static_config_upload(priv); 10968aa9ebccSVladimir Oltean } 10978aa9ebccSVladimir Oltean 109829afb83aSVladimir Oltean static int sja1105_parse_rgmii_delays(struct sja1105_private *priv) 1099f5b8631cSVladimir Oltean { 1100542043e9SVladimir Oltean struct dsa_switch *ds = priv->ds; 110129afb83aSVladimir Oltean int port; 1102f5b8631cSVladimir Oltean 110329afb83aSVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 110429afb83aSVladimir Oltean if (!priv->fixed_link[port]) 1105f5b8631cSVladimir Oltean continue; 1106f5b8631cSVladimir Oltean 110729afb83aSVladimir Oltean if (priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_RXID || 110829afb83aSVladimir Oltean priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_ID) 110929afb83aSVladimir Oltean priv->rgmii_rx_delay[port] = true; 1110f5b8631cSVladimir Oltean 111129afb83aSVladimir Oltean if (priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_TXID || 111229afb83aSVladimir Oltean priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_ID) 111329afb83aSVladimir Oltean priv->rgmii_tx_delay[port] = true; 1114f5b8631cSVladimir Oltean 111529afb83aSVladimir Oltean if ((priv->rgmii_rx_delay[port] || priv->rgmii_tx_delay[port]) && 1116f5b8631cSVladimir Oltean !priv->info->setup_rgmii_delay) 1117f5b8631cSVladimir Oltean return -EINVAL; 1118f5b8631cSVladimir Oltean } 1119f5b8631cSVladimir Oltean return 0; 1120f5b8631cSVladimir Oltean } 1121f5b8631cSVladimir Oltean 11228aa9ebccSVladimir Oltean static int sja1105_parse_ports_node(struct sja1105_private *priv, 11238aa9ebccSVladimir Oltean struct device_node *ports_node) 11248aa9ebccSVladimir Oltean { 11258aa9ebccSVladimir Oltean struct device *dev = &priv->spidev->dev; 11268aa9ebccSVladimir Oltean struct device_node *child; 11278aa9ebccSVladimir Oltean 112827afe0d3SVladimir Oltean for_each_available_child_of_node(ports_node, child) { 11298aa9ebccSVladimir Oltean struct device_node *phy_node; 11300c65b2b9SAndrew Lunn phy_interface_t phy_mode; 11318aa9ebccSVladimir Oltean u32 index; 11320c65b2b9SAndrew Lunn int err; 11338aa9ebccSVladimir Oltean 11348aa9ebccSVladimir Oltean /* Get switch port number from DT */ 11358aa9ebccSVladimir Oltean if (of_property_read_u32(child, "reg", &index) < 0) { 11368aa9ebccSVladimir Oltean dev_err(dev, "Port number not defined in device tree " 11378aa9ebccSVladimir Oltean "(property \"reg\")\n"); 11387ba771e3SNishka Dasgupta of_node_put(child); 11398aa9ebccSVladimir Oltean return -ENODEV; 11408aa9ebccSVladimir Oltean } 11418aa9ebccSVladimir Oltean 11428aa9ebccSVladimir Oltean /* Get PHY mode from DT */ 11430c65b2b9SAndrew Lunn err = of_get_phy_mode(child, &phy_mode); 11440c65b2b9SAndrew Lunn if (err) { 11458aa9ebccSVladimir Oltean dev_err(dev, "Failed to read phy-mode or " 11468aa9ebccSVladimir Oltean "phy-interface-type property for port %d\n", 11478aa9ebccSVladimir Oltean index); 11487ba771e3SNishka Dasgupta of_node_put(child); 11498aa9ebccSVladimir Oltean return -ENODEV; 11508aa9ebccSVladimir Oltean } 11518aa9ebccSVladimir Oltean 11528aa9ebccSVladimir Oltean phy_node = of_parse_phandle(child, "phy-handle", 0); 11538aa9ebccSVladimir Oltean if (!phy_node) { 11548aa9ebccSVladimir Oltean if (!of_phy_is_fixed_link(child)) { 11558aa9ebccSVladimir Oltean dev_err(dev, "phy-handle or fixed-link " 11568aa9ebccSVladimir Oltean "properties missing!\n"); 11577ba771e3SNishka Dasgupta of_node_put(child); 11588aa9ebccSVladimir Oltean return -ENODEV; 11598aa9ebccSVladimir Oltean } 11608aa9ebccSVladimir Oltean /* phy-handle is missing, but fixed-link isn't. 11618aa9ebccSVladimir Oltean * So it's a fixed link. Default to PHY role. 11628aa9ebccSVladimir Oltean */ 116329afb83aSVladimir Oltean priv->fixed_link[index] = true; 11648aa9ebccSVladimir Oltean } else { 11658aa9ebccSVladimir Oltean of_node_put(phy_node); 11668aa9ebccSVladimir Oltean } 11678aa9ebccSVladimir Oltean 1168bf4edf4aSVladimir Oltean priv->phy_mode[index] = phy_mode; 11698aa9ebccSVladimir Oltean } 11708aa9ebccSVladimir Oltean 11718aa9ebccSVladimir Oltean return 0; 11728aa9ebccSVladimir Oltean } 11738aa9ebccSVladimir Oltean 11745d645df9SVladimir Oltean static int sja1105_parse_dt(struct sja1105_private *priv) 11758aa9ebccSVladimir Oltean { 11768aa9ebccSVladimir Oltean struct device *dev = &priv->spidev->dev; 11778aa9ebccSVladimir Oltean struct device_node *switch_node = dev->of_node; 11788aa9ebccSVladimir Oltean struct device_node *ports_node; 11798aa9ebccSVladimir Oltean int rc; 11808aa9ebccSVladimir Oltean 11818aa9ebccSVladimir Oltean ports_node = of_get_child_by_name(switch_node, "ports"); 118215074a36SVladimir Oltean if (!ports_node) 118315074a36SVladimir Oltean ports_node = of_get_child_by_name(switch_node, "ethernet-ports"); 11848aa9ebccSVladimir Oltean if (!ports_node) { 11858aa9ebccSVladimir Oltean dev_err(dev, "Incorrect bindings: absent \"ports\" node\n"); 11868aa9ebccSVladimir Oltean return -ENODEV; 11878aa9ebccSVladimir Oltean } 11888aa9ebccSVladimir Oltean 11895d645df9SVladimir Oltean rc = sja1105_parse_ports_node(priv, ports_node); 11908aa9ebccSVladimir Oltean of_node_put(ports_node); 11918aa9ebccSVladimir Oltean 11928aa9ebccSVladimir Oltean return rc; 11938aa9ebccSVladimir Oltean } 11948aa9ebccSVladimir Oltean 1195c44d0535SVladimir Oltean /* Convert link speed from SJA1105 to ethtool encoding */ 119641fed17fSVladimir Oltean static int sja1105_port_speed_to_ethtool(struct sja1105_private *priv, 119741fed17fSVladimir Oltean u64 speed) 119841fed17fSVladimir Oltean { 119941fed17fSVladimir Oltean if (speed == priv->info->port_speed[SJA1105_SPEED_10MBPS]) 120041fed17fSVladimir Oltean return SPEED_10; 120141fed17fSVladimir Oltean if (speed == priv->info->port_speed[SJA1105_SPEED_100MBPS]) 120241fed17fSVladimir Oltean return SPEED_100; 120341fed17fSVladimir Oltean if (speed == priv->info->port_speed[SJA1105_SPEED_1000MBPS]) 120441fed17fSVladimir Oltean return SPEED_1000; 120541fed17fSVladimir Oltean if (speed == priv->info->port_speed[SJA1105_SPEED_2500MBPS]) 120641fed17fSVladimir Oltean return SPEED_2500; 120741fed17fSVladimir Oltean return SPEED_UNKNOWN; 120841fed17fSVladimir Oltean } 12098aa9ebccSVladimir Oltean 12108400cff6SVladimir Oltean /* Set link speed in the MAC configuration for a specific port. */ 12118aa9ebccSVladimir Oltean static int sja1105_adjust_port_config(struct sja1105_private *priv, int port, 12128400cff6SVladimir Oltean int speed_mbps) 12138aa9ebccSVladimir Oltean { 12148aa9ebccSVladimir Oltean struct sja1105_mac_config_entry *mac; 12158aa9ebccSVladimir Oltean struct device *dev = priv->ds->dev; 121641fed17fSVladimir Oltean u64 speed; 12178aa9ebccSVladimir Oltean int rc; 12188aa9ebccSVladimir Oltean 12198400cff6SVladimir Oltean /* On P/Q/R/S, one can read from the device via the MAC reconfiguration 12208400cff6SVladimir Oltean * tables. On E/T, MAC reconfig tables are not readable, only writable. 12218400cff6SVladimir Oltean * We have to *know* what the MAC looks like. For the sake of keeping 12228400cff6SVladimir Oltean * the code common, we'll use the static configuration tables as a 12238400cff6SVladimir Oltean * reasonable approximation for both E/T and P/Q/R/S. 12248400cff6SVladimir Oltean */ 12258aa9ebccSVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 12268aa9ebccSVladimir Oltean 1227f4cfcfbdSVladimir Oltean switch (speed_mbps) { 1228c44d0535SVladimir Oltean case SPEED_UNKNOWN: 1229a979a0abSVladimir Oltean /* PHYLINK called sja1105_mac_config() to inform us about 1230a979a0abSVladimir Oltean * the state->interface, but AN has not completed and the 1231a979a0abSVladimir Oltean * speed is not yet valid. UM10944.pdf says that setting 1232a979a0abSVladimir Oltean * SJA1105_SPEED_AUTO at runtime disables the port, so that is 1233a979a0abSVladimir Oltean * ok for power consumption in case AN will never complete - 1234a979a0abSVladimir Oltean * otherwise PHYLINK should come back with a new update. 1235a979a0abSVladimir Oltean */ 123641fed17fSVladimir Oltean speed = priv->info->port_speed[SJA1105_SPEED_AUTO]; 1237f4cfcfbdSVladimir Oltean break; 1238c44d0535SVladimir Oltean case SPEED_10: 123941fed17fSVladimir Oltean speed = priv->info->port_speed[SJA1105_SPEED_10MBPS]; 1240f4cfcfbdSVladimir Oltean break; 1241c44d0535SVladimir Oltean case SPEED_100: 124241fed17fSVladimir Oltean speed = priv->info->port_speed[SJA1105_SPEED_100MBPS]; 1243f4cfcfbdSVladimir Oltean break; 1244c44d0535SVladimir Oltean case SPEED_1000: 124541fed17fSVladimir Oltean speed = priv->info->port_speed[SJA1105_SPEED_1000MBPS]; 1246f4cfcfbdSVladimir Oltean break; 124756b63466SVladimir Oltean case SPEED_2500: 124856b63466SVladimir Oltean speed = priv->info->port_speed[SJA1105_SPEED_2500MBPS]; 124956b63466SVladimir Oltean break; 1250f4cfcfbdSVladimir Oltean default: 12518aa9ebccSVladimir Oltean dev_err(dev, "Invalid speed %iMbps\n", speed_mbps); 12528aa9ebccSVladimir Oltean return -EINVAL; 12538aa9ebccSVladimir Oltean } 12548aa9ebccSVladimir Oltean 12558400cff6SVladimir Oltean /* Overwrite SJA1105_SPEED_AUTO from the static MAC configuration 12568400cff6SVladimir Oltean * table, since this will be used for the clocking setup, and we no 12578400cff6SVladimir Oltean * longer need to store it in the static config (already told hardware 12588400cff6SVladimir Oltean * we want auto during upload phase). 1259ffe10e67SVladimir Oltean * Actually for the SGMII port, the MAC is fixed at 1 Gbps and 1260ffe10e67SVladimir Oltean * we need to configure the PCS only (if even that). 12618aa9ebccSVladimir Oltean */ 126291a05078SVladimir Oltean if (priv->phy_mode[port] == PHY_INTERFACE_MODE_SGMII) 126341fed17fSVladimir Oltean mac[port].speed = priv->info->port_speed[SJA1105_SPEED_1000MBPS]; 126456b63466SVladimir Oltean else if (priv->phy_mode[port] == PHY_INTERFACE_MODE_2500BASEX) 126556b63466SVladimir Oltean mac[port].speed = priv->info->port_speed[SJA1105_SPEED_2500MBPS]; 1266ffe10e67SVladimir Oltean else 12678aa9ebccSVladimir Oltean mac[port].speed = speed; 12688aa9ebccSVladimir Oltean 12698aa9ebccSVladimir Oltean /* Write to the dynamic reconfiguration tables */ 12708400cff6SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, 12718400cff6SVladimir Oltean &mac[port], true); 12728aa9ebccSVladimir Oltean if (rc < 0) { 12738aa9ebccSVladimir Oltean dev_err(dev, "Failed to write MAC config: %d\n", rc); 12748aa9ebccSVladimir Oltean return rc; 12758aa9ebccSVladimir Oltean } 12768aa9ebccSVladimir Oltean 12778aa9ebccSVladimir Oltean /* Reconfigure the PLLs for the RGMII interfaces (required 125 MHz at 12788aa9ebccSVladimir Oltean * gigabit, 25 MHz at 100 Mbps and 2.5 MHz at 10 Mbps). For MII and 12798aa9ebccSVladimir Oltean * RMII no change of the clock setup is required. Actually, changing 12808aa9ebccSVladimir Oltean * the clock setup does interrupt the clock signal for a certain time 12818aa9ebccSVladimir Oltean * which causes trouble for all PHYs relying on this signal. 12828aa9ebccSVladimir Oltean */ 128391a05078SVladimir Oltean if (!phy_interface_mode_is_rgmii(priv->phy_mode[port])) 12848aa9ebccSVladimir Oltean return 0; 12858aa9ebccSVladimir Oltean 12868aa9ebccSVladimir Oltean return sja1105_clocking_setup_port(priv, port); 12878aa9ebccSVladimir Oltean } 12888aa9ebccSVladimir Oltean 128939710229SVladimir Oltean /* The SJA1105 MAC programming model is through the static config (the xMII 129039710229SVladimir Oltean * Mode table cannot be dynamically reconfigured), and we have to program 129139710229SVladimir Oltean * that early (earlier than PHYLINK calls us, anyway). 129239710229SVladimir Oltean * So just error out in case the connected PHY attempts to change the initial 129339710229SVladimir Oltean * system interface MII protocol from what is defined in the DT, at least for 129439710229SVladimir Oltean * now. 129539710229SVladimir Oltean */ 129639710229SVladimir Oltean static bool sja1105_phy_mode_mismatch(struct sja1105_private *priv, int port, 129739710229SVladimir Oltean phy_interface_t interface) 129839710229SVladimir Oltean { 1299bf4edf4aSVladimir Oltean return priv->phy_mode[port] != interface; 130039710229SVladimir Oltean } 130139710229SVladimir Oltean 1302af7cd036SVladimir Oltean static void sja1105_mac_config(struct dsa_switch *ds, int port, 1303ffe10e67SVladimir Oltean unsigned int mode, 1304af7cd036SVladimir Oltean const struct phylink_link_state *state) 13058aa9ebccSVladimir Oltean { 13063ad1d171SVladimir Oltean struct dsa_port *dp = dsa_to_port(ds, port); 13078aa9ebccSVladimir Oltean struct sja1105_private *priv = ds->priv; 13083ad1d171SVladimir Oltean struct dw_xpcs *xpcs; 13098aa9ebccSVladimir Oltean 1310ec8582d1SVladimir Oltean if (sja1105_phy_mode_mismatch(priv, port, state->interface)) { 1311ec8582d1SVladimir Oltean dev_err(ds->dev, "Changing PHY mode to %s not supported!\n", 1312ec8582d1SVladimir Oltean phy_modes(state->interface)); 131339710229SVladimir Oltean return; 1314ec8582d1SVladimir Oltean } 131539710229SVladimir Oltean 13163ad1d171SVladimir Oltean xpcs = priv->xpcs[port]; 1317ffe10e67SVladimir Oltean 13183ad1d171SVladimir Oltean if (xpcs) 13193ad1d171SVladimir Oltean phylink_set_pcs(dp->pl, &xpcs->pcs); 13208400cff6SVladimir Oltean } 13218400cff6SVladimir Oltean 13228400cff6SVladimir Oltean static void sja1105_mac_link_down(struct dsa_switch *ds, int port, 13238400cff6SVladimir Oltean unsigned int mode, 13248400cff6SVladimir Oltean phy_interface_t interface) 13258400cff6SVladimir Oltean { 13268400cff6SVladimir Oltean sja1105_inhibit_tx(ds->priv, BIT(port), true); 13278400cff6SVladimir Oltean } 13288400cff6SVladimir Oltean 13298400cff6SVladimir Oltean static void sja1105_mac_link_up(struct dsa_switch *ds, int port, 13308400cff6SVladimir Oltean unsigned int mode, 13318400cff6SVladimir Oltean phy_interface_t interface, 13325b502a7bSRussell King struct phy_device *phydev, 13335b502a7bSRussell King int speed, int duplex, 13345b502a7bSRussell King bool tx_pause, bool rx_pause) 13358400cff6SVladimir Oltean { 1336ec8582d1SVladimir Oltean struct sja1105_private *priv = ds->priv; 1337ec8582d1SVladimir Oltean 1338ec8582d1SVladimir Oltean sja1105_adjust_port_config(priv, port, speed); 1339ec8582d1SVladimir Oltean 1340ec8582d1SVladimir Oltean sja1105_inhibit_tx(priv, BIT(port), false); 13418aa9ebccSVladimir Oltean } 13428aa9ebccSVladimir Oltean 1343ad9f299aSVladimir Oltean static void sja1105_phylink_validate(struct dsa_switch *ds, int port, 1344ad9f299aSVladimir Oltean unsigned long *supported, 1345ad9f299aSVladimir Oltean struct phylink_link_state *state) 1346ad9f299aSVladimir Oltean { 1347ad9f299aSVladimir Oltean /* Construct a new mask which exhaustively contains all link features 1348ad9f299aSVladimir Oltean * supported by the MAC, and then apply that (logical AND) to what will 1349ad9f299aSVladimir Oltean * be sent to the PHY for "marketing". 1350ad9f299aSVladimir Oltean */ 1351ad9f299aSVladimir Oltean __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, }; 1352ad9f299aSVladimir Oltean struct sja1105_private *priv = ds->priv; 1353ad9f299aSVladimir Oltean struct sja1105_xmii_params_entry *mii; 1354ad9f299aSVladimir Oltean 1355ad9f299aSVladimir Oltean mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries; 1356ad9f299aSVladimir Oltean 135739710229SVladimir Oltean /* include/linux/phylink.h says: 135839710229SVladimir Oltean * When @state->interface is %PHY_INTERFACE_MODE_NA, phylink 135939710229SVladimir Oltean * expects the MAC driver to return all supported link modes. 136039710229SVladimir Oltean */ 136139710229SVladimir Oltean if (state->interface != PHY_INTERFACE_MODE_NA && 136239710229SVladimir Oltean sja1105_phy_mode_mismatch(priv, port, state->interface)) { 136339710229SVladimir Oltean bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS); 136439710229SVladimir Oltean return; 136539710229SVladimir Oltean } 136639710229SVladimir Oltean 1367ad9f299aSVladimir Oltean /* The MAC does not support pause frames, and also doesn't 1368ad9f299aSVladimir Oltean * support half-duplex traffic modes. 1369ad9f299aSVladimir Oltean */ 1370ad9f299aSVladimir Oltean phylink_set(mask, Autoneg); 1371ad9f299aSVladimir Oltean phylink_set(mask, MII); 1372ad9f299aSVladimir Oltean phylink_set(mask, 10baseT_Full); 1373ad9f299aSVladimir Oltean phylink_set(mask, 100baseT_Full); 1374ca68e138SOleksij Rempel phylink_set(mask, 100baseT1_Full); 1375ffe10e67SVladimir Oltean if (mii->xmii_mode[port] == XMII_MODE_RGMII || 1376ffe10e67SVladimir Oltean mii->xmii_mode[port] == XMII_MODE_SGMII) 1377ad9f299aSVladimir Oltean phylink_set(mask, 1000baseT_Full); 137856b63466SVladimir Oltean if (priv->info->supports_2500basex[port]) { 137956b63466SVladimir Oltean phylink_set(mask, 2500baseT_Full); 138056b63466SVladimir Oltean phylink_set(mask, 2500baseX_Full); 138156b63466SVladimir Oltean } 1382ad9f299aSVladimir Oltean 1383ad9f299aSVladimir Oltean bitmap_and(supported, supported, mask, __ETHTOOL_LINK_MODE_MASK_NBITS); 1384ad9f299aSVladimir Oltean bitmap_and(state->advertising, state->advertising, mask, 1385ad9f299aSVladimir Oltean __ETHTOOL_LINK_MODE_MASK_NBITS); 1386ad9f299aSVladimir Oltean } 1387ad9f299aSVladimir Oltean 138860f6053fSVladimir Oltean static int 138960f6053fSVladimir Oltean sja1105_find_static_fdb_entry(struct sja1105_private *priv, int port, 139060f6053fSVladimir Oltean const struct sja1105_l2_lookup_entry *requested) 139160f6053fSVladimir Oltean { 139260f6053fSVladimir Oltean struct sja1105_l2_lookup_entry *l2_lookup; 139360f6053fSVladimir Oltean struct sja1105_table *table; 139460f6053fSVladimir Oltean int i; 139560f6053fSVladimir Oltean 139660f6053fSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; 139760f6053fSVladimir Oltean l2_lookup = table->entries; 139860f6053fSVladimir Oltean 139960f6053fSVladimir Oltean for (i = 0; i < table->entry_count; i++) 140060f6053fSVladimir Oltean if (l2_lookup[i].macaddr == requested->macaddr && 140160f6053fSVladimir Oltean l2_lookup[i].vlanid == requested->vlanid && 140260f6053fSVladimir Oltean l2_lookup[i].destports & BIT(port)) 140360f6053fSVladimir Oltean return i; 140460f6053fSVladimir Oltean 140560f6053fSVladimir Oltean return -1; 140660f6053fSVladimir Oltean } 140760f6053fSVladimir Oltean 140860f6053fSVladimir Oltean /* We want FDB entries added statically through the bridge command to persist 140960f6053fSVladimir Oltean * across switch resets, which are a common thing during normal SJA1105 141060f6053fSVladimir Oltean * operation. So we have to back them up in the static configuration tables 141160f6053fSVladimir Oltean * and hence apply them on next static config upload... yay! 141260f6053fSVladimir Oltean */ 141360f6053fSVladimir Oltean static int 141460f6053fSVladimir Oltean sja1105_static_fdb_change(struct sja1105_private *priv, int port, 141560f6053fSVladimir Oltean const struct sja1105_l2_lookup_entry *requested, 141660f6053fSVladimir Oltean bool keep) 141760f6053fSVladimir Oltean { 141860f6053fSVladimir Oltean struct sja1105_l2_lookup_entry *l2_lookup; 141960f6053fSVladimir Oltean struct sja1105_table *table; 142060f6053fSVladimir Oltean int rc, match; 142160f6053fSVladimir Oltean 142260f6053fSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; 142360f6053fSVladimir Oltean 142460f6053fSVladimir Oltean match = sja1105_find_static_fdb_entry(priv, port, requested); 142560f6053fSVladimir Oltean if (match < 0) { 142660f6053fSVladimir Oltean /* Can't delete a missing entry. */ 142760f6053fSVladimir Oltean if (!keep) 142860f6053fSVladimir Oltean return 0; 142960f6053fSVladimir Oltean 143060f6053fSVladimir Oltean /* No match => new entry */ 143160f6053fSVladimir Oltean rc = sja1105_table_resize(table, table->entry_count + 1); 143260f6053fSVladimir Oltean if (rc) 143360f6053fSVladimir Oltean return rc; 143460f6053fSVladimir Oltean 143560f6053fSVladimir Oltean match = table->entry_count - 1; 143660f6053fSVladimir Oltean } 143760f6053fSVladimir Oltean 143860f6053fSVladimir Oltean /* Assign pointer after the resize (it may be new memory) */ 143960f6053fSVladimir Oltean l2_lookup = table->entries; 144060f6053fSVladimir Oltean 144160f6053fSVladimir Oltean /* We have a match. 144260f6053fSVladimir Oltean * If the job was to add this FDB entry, it's already done (mostly 144360f6053fSVladimir Oltean * anyway, since the port forwarding mask may have changed, case in 144460f6053fSVladimir Oltean * which we update it). 144560f6053fSVladimir Oltean * Otherwise we have to delete it. 144660f6053fSVladimir Oltean */ 144760f6053fSVladimir Oltean if (keep) { 144860f6053fSVladimir Oltean l2_lookup[match] = *requested; 144960f6053fSVladimir Oltean return 0; 145060f6053fSVladimir Oltean } 145160f6053fSVladimir Oltean 145260f6053fSVladimir Oltean /* To remove, the strategy is to overwrite the element with 145360f6053fSVladimir Oltean * the last one, and then reduce the array size by 1 145460f6053fSVladimir Oltean */ 145560f6053fSVladimir Oltean l2_lookup[match] = l2_lookup[table->entry_count - 1]; 145660f6053fSVladimir Oltean return sja1105_table_resize(table, table->entry_count - 1); 145760f6053fSVladimir Oltean } 145860f6053fSVladimir Oltean 1459291d1e72SVladimir Oltean /* First-generation switches have a 4-way set associative TCAM that 1460291d1e72SVladimir Oltean * holds the FDB entries. An FDB index spans from 0 to 1023 and is comprised of 1461291d1e72SVladimir Oltean * a "bin" (grouping of 4 entries) and a "way" (an entry within a bin). 1462291d1e72SVladimir Oltean * For the placement of a newly learnt FDB entry, the switch selects the bin 1463291d1e72SVladimir Oltean * based on a hash function, and the way within that bin incrementally. 1464291d1e72SVladimir Oltean */ 146509c1b412SVladimir Oltean static int sja1105et_fdb_index(int bin, int way) 1466291d1e72SVladimir Oltean { 1467291d1e72SVladimir Oltean return bin * SJA1105ET_FDB_BIN_SIZE + way; 1468291d1e72SVladimir Oltean } 1469291d1e72SVladimir Oltean 14709dfa6911SVladimir Oltean static int sja1105et_is_fdb_entry_in_bin(struct sja1105_private *priv, int bin, 1471291d1e72SVladimir Oltean const u8 *addr, u16 vid, 1472291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry *match, 1473291d1e72SVladimir Oltean int *last_unused) 1474291d1e72SVladimir Oltean { 1475291d1e72SVladimir Oltean int way; 1476291d1e72SVladimir Oltean 1477291d1e72SVladimir Oltean for (way = 0; way < SJA1105ET_FDB_BIN_SIZE; way++) { 1478291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 1479291d1e72SVladimir Oltean int index = sja1105et_fdb_index(bin, way); 1480291d1e72SVladimir Oltean 1481291d1e72SVladimir Oltean /* Skip unused entries, optionally marking them 1482291d1e72SVladimir Oltean * into the return value 1483291d1e72SVladimir Oltean */ 1484291d1e72SVladimir Oltean if (sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 1485291d1e72SVladimir Oltean index, &l2_lookup)) { 1486291d1e72SVladimir Oltean if (last_unused) 1487291d1e72SVladimir Oltean *last_unused = way; 1488291d1e72SVladimir Oltean continue; 1489291d1e72SVladimir Oltean } 1490291d1e72SVladimir Oltean 1491291d1e72SVladimir Oltean if (l2_lookup.macaddr == ether_addr_to_u64(addr) && 1492291d1e72SVladimir Oltean l2_lookup.vlanid == vid) { 1493291d1e72SVladimir Oltean if (match) 1494291d1e72SVladimir Oltean *match = l2_lookup; 1495291d1e72SVladimir Oltean return way; 1496291d1e72SVladimir Oltean } 1497291d1e72SVladimir Oltean } 1498291d1e72SVladimir Oltean /* Return an invalid entry index if not found */ 1499291d1e72SVladimir Oltean return -1; 1500291d1e72SVladimir Oltean } 1501291d1e72SVladimir Oltean 15029dfa6911SVladimir Oltean int sja1105et_fdb_add(struct dsa_switch *ds, int port, 1503291d1e72SVladimir Oltean const unsigned char *addr, u16 vid) 1504291d1e72SVladimir Oltean { 15056c5fc159SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}, tmp; 1506291d1e72SVladimir Oltean struct sja1105_private *priv = ds->priv; 1507291d1e72SVladimir Oltean struct device *dev = ds->dev; 1508291d1e72SVladimir Oltean int last_unused = -1; 15096c5fc159SVladimir Oltean int start, end, i; 151060f6053fSVladimir Oltean int bin, way, rc; 1511291d1e72SVladimir Oltean 15129dfa6911SVladimir Oltean bin = sja1105et_fdb_hash(priv, addr, vid); 1513291d1e72SVladimir Oltean 15149dfa6911SVladimir Oltean way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid, 1515291d1e72SVladimir Oltean &l2_lookup, &last_unused); 1516291d1e72SVladimir Oltean if (way >= 0) { 1517291d1e72SVladimir Oltean /* We have an FDB entry. Is our port in the destination 1518291d1e72SVladimir Oltean * mask? If yes, we need to do nothing. If not, we need 1519291d1e72SVladimir Oltean * to rewrite the entry by adding this port to it. 1520291d1e72SVladimir Oltean */ 1521e11e865bSVladimir Oltean if ((l2_lookup.destports & BIT(port)) && l2_lookup.lockeds) 1522291d1e72SVladimir Oltean return 0; 1523291d1e72SVladimir Oltean l2_lookup.destports |= BIT(port); 1524291d1e72SVladimir Oltean } else { 1525291d1e72SVladimir Oltean int index = sja1105et_fdb_index(bin, way); 1526291d1e72SVladimir Oltean 1527291d1e72SVladimir Oltean /* We don't have an FDB entry. We construct a new one and 1528291d1e72SVladimir Oltean * try to find a place for it within the FDB table. 1529291d1e72SVladimir Oltean */ 1530291d1e72SVladimir Oltean l2_lookup.macaddr = ether_addr_to_u64(addr); 1531291d1e72SVladimir Oltean l2_lookup.destports = BIT(port); 1532291d1e72SVladimir Oltean l2_lookup.vlanid = vid; 1533291d1e72SVladimir Oltean 1534291d1e72SVladimir Oltean if (last_unused >= 0) { 1535291d1e72SVladimir Oltean way = last_unused; 1536291d1e72SVladimir Oltean } else { 1537291d1e72SVladimir Oltean /* Bin is full, need to evict somebody. 1538291d1e72SVladimir Oltean * Choose victim at random. If you get these messages 1539291d1e72SVladimir Oltean * often, you may need to consider changing the 1540291d1e72SVladimir Oltean * distribution function: 1541291d1e72SVladimir Oltean * static_config[BLK_IDX_L2_LOOKUP_PARAMS].entries->poly 1542291d1e72SVladimir Oltean */ 1543291d1e72SVladimir Oltean get_random_bytes(&way, sizeof(u8)); 1544291d1e72SVladimir Oltean way %= SJA1105ET_FDB_BIN_SIZE; 1545291d1e72SVladimir Oltean dev_warn(dev, "Warning, FDB bin %d full while adding entry for %pM. Evicting entry %u.\n", 1546291d1e72SVladimir Oltean bin, addr, way); 1547291d1e72SVladimir Oltean /* Evict entry */ 1548291d1e72SVladimir Oltean sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 1549291d1e72SVladimir Oltean index, NULL, false); 1550291d1e72SVladimir Oltean } 1551291d1e72SVladimir Oltean } 1552e11e865bSVladimir Oltean l2_lookup.lockeds = true; 1553291d1e72SVladimir Oltean l2_lookup.index = sja1105et_fdb_index(bin, way); 1554291d1e72SVladimir Oltean 155560f6053fSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 1556291d1e72SVladimir Oltean l2_lookup.index, &l2_lookup, 1557291d1e72SVladimir Oltean true); 155860f6053fSVladimir Oltean if (rc < 0) 155960f6053fSVladimir Oltean return rc; 156060f6053fSVladimir Oltean 15616c5fc159SVladimir Oltean /* Invalidate a dynamically learned entry if that exists */ 15626c5fc159SVladimir Oltean start = sja1105et_fdb_index(bin, 0); 15636c5fc159SVladimir Oltean end = sja1105et_fdb_index(bin, way); 15646c5fc159SVladimir Oltean 15656c5fc159SVladimir Oltean for (i = start; i < end; i++) { 15666c5fc159SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 15676c5fc159SVladimir Oltean i, &tmp); 15686c5fc159SVladimir Oltean if (rc == -ENOENT) 15696c5fc159SVladimir Oltean continue; 15706c5fc159SVladimir Oltean if (rc) 15716c5fc159SVladimir Oltean return rc; 15726c5fc159SVladimir Oltean 15736c5fc159SVladimir Oltean if (tmp.macaddr != ether_addr_to_u64(addr) || tmp.vlanid != vid) 15746c5fc159SVladimir Oltean continue; 15756c5fc159SVladimir Oltean 15766c5fc159SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 15776c5fc159SVladimir Oltean i, NULL, false); 15786c5fc159SVladimir Oltean if (rc) 15796c5fc159SVladimir Oltean return rc; 15806c5fc159SVladimir Oltean 15816c5fc159SVladimir Oltean break; 15826c5fc159SVladimir Oltean } 15836c5fc159SVladimir Oltean 158460f6053fSVladimir Oltean return sja1105_static_fdb_change(priv, port, &l2_lookup, true); 1585291d1e72SVladimir Oltean } 1586291d1e72SVladimir Oltean 15879dfa6911SVladimir Oltean int sja1105et_fdb_del(struct dsa_switch *ds, int port, 1588291d1e72SVladimir Oltean const unsigned char *addr, u16 vid) 1589291d1e72SVladimir Oltean { 1590291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 1591291d1e72SVladimir Oltean struct sja1105_private *priv = ds->priv; 159260f6053fSVladimir Oltean int index, bin, way, rc; 1593291d1e72SVladimir Oltean bool keep; 1594291d1e72SVladimir Oltean 15959dfa6911SVladimir Oltean bin = sja1105et_fdb_hash(priv, addr, vid); 15969dfa6911SVladimir Oltean way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid, 1597291d1e72SVladimir Oltean &l2_lookup, NULL); 1598291d1e72SVladimir Oltean if (way < 0) 1599291d1e72SVladimir Oltean return 0; 1600291d1e72SVladimir Oltean index = sja1105et_fdb_index(bin, way); 1601291d1e72SVladimir Oltean 1602291d1e72SVladimir Oltean /* We have an FDB entry. Is our port in the destination mask? If yes, 1603291d1e72SVladimir Oltean * we need to remove it. If the resulting port mask becomes empty, we 1604291d1e72SVladimir Oltean * need to completely evict the FDB entry. 1605291d1e72SVladimir Oltean * Otherwise we just write it back. 1606291d1e72SVladimir Oltean */ 1607291d1e72SVladimir Oltean l2_lookup.destports &= ~BIT(port); 16087752e937SVladimir Oltean 1609291d1e72SVladimir Oltean if (l2_lookup.destports) 1610291d1e72SVladimir Oltean keep = true; 1611291d1e72SVladimir Oltean else 1612291d1e72SVladimir Oltean keep = false; 1613291d1e72SVladimir Oltean 161460f6053fSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 1615291d1e72SVladimir Oltean index, &l2_lookup, keep); 161660f6053fSVladimir Oltean if (rc < 0) 161760f6053fSVladimir Oltean return rc; 161860f6053fSVladimir Oltean 161960f6053fSVladimir Oltean return sja1105_static_fdb_change(priv, port, &l2_lookup, keep); 1620291d1e72SVladimir Oltean } 1621291d1e72SVladimir Oltean 16229dfa6911SVladimir Oltean int sja1105pqrs_fdb_add(struct dsa_switch *ds, int port, 16239dfa6911SVladimir Oltean const unsigned char *addr, u16 vid) 16249dfa6911SVladimir Oltean { 16256c5fc159SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}, tmp; 16261da73821SVladimir Oltean struct sja1105_private *priv = ds->priv; 16271da73821SVladimir Oltean int rc, i; 16281da73821SVladimir Oltean 16291da73821SVladimir Oltean /* Search for an existing entry in the FDB table */ 16301da73821SVladimir Oltean l2_lookup.macaddr = ether_addr_to_u64(addr); 16311da73821SVladimir Oltean l2_lookup.vlanid = vid; 16321da73821SVladimir Oltean l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0); 16331da73821SVladimir Oltean l2_lookup.mask_vlanid = VLAN_VID_MASK; 16341da73821SVladimir Oltean l2_lookup.destports = BIT(port); 16351da73821SVladimir Oltean 1636728db843SVladimir Oltean tmp = l2_lookup; 1637728db843SVladimir Oltean 16381da73821SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 1639728db843SVladimir Oltean SJA1105_SEARCH, &tmp); 1640728db843SVladimir Oltean if (rc == 0 && tmp.index != SJA1105_MAX_L2_LOOKUP_COUNT - 1) { 1641e11e865bSVladimir Oltean /* Found a static entry and this port is already in the entry's 16421da73821SVladimir Oltean * port mask => job done 16431da73821SVladimir Oltean */ 1644728db843SVladimir Oltean if ((tmp.destports & BIT(port)) && tmp.lockeds) 16451da73821SVladimir Oltean return 0; 1646728db843SVladimir Oltean 1647728db843SVladimir Oltean l2_lookup = tmp; 1648728db843SVladimir Oltean 16491da73821SVladimir Oltean /* l2_lookup.index is populated by the switch in case it 16501da73821SVladimir Oltean * found something. 16511da73821SVladimir Oltean */ 16521da73821SVladimir Oltean l2_lookup.destports |= BIT(port); 16531da73821SVladimir Oltean goto skip_finding_an_index; 16541da73821SVladimir Oltean } 16551da73821SVladimir Oltean 16561da73821SVladimir Oltean /* Not found, so try to find an unused spot in the FDB. 16571da73821SVladimir Oltean * This is slightly inefficient because the strategy is knock-knock at 16581da73821SVladimir Oltean * every possible position from 0 to 1023. 16591da73821SVladimir Oltean */ 16601da73821SVladimir Oltean for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) { 16611da73821SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 16621da73821SVladimir Oltean i, NULL); 16631da73821SVladimir Oltean if (rc < 0) 16641da73821SVladimir Oltean break; 16651da73821SVladimir Oltean } 16661da73821SVladimir Oltean if (i == SJA1105_MAX_L2_LOOKUP_COUNT) { 16671da73821SVladimir Oltean dev_err(ds->dev, "FDB is full, cannot add entry.\n"); 16681da73821SVladimir Oltean return -EINVAL; 16691da73821SVladimir Oltean } 16701da73821SVladimir Oltean l2_lookup.index = i; 16711da73821SVladimir Oltean 16721da73821SVladimir Oltean skip_finding_an_index: 1673e11e865bSVladimir Oltean l2_lookup.lockeds = true; 1674e11e865bSVladimir Oltean 167560f6053fSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 16761da73821SVladimir Oltean l2_lookup.index, &l2_lookup, 16771da73821SVladimir Oltean true); 167860f6053fSVladimir Oltean if (rc < 0) 167960f6053fSVladimir Oltean return rc; 168060f6053fSVladimir Oltean 16816c5fc159SVladimir Oltean /* The switch learns dynamic entries and looks up the FDB left to 16826c5fc159SVladimir Oltean * right. It is possible that our addition was concurrent with the 16836c5fc159SVladimir Oltean * dynamic learning of the same address, so now that the static entry 16846c5fc159SVladimir Oltean * has been installed, we are certain that address learning for this 16856c5fc159SVladimir Oltean * particular address has been turned off, so the dynamic entry either 16866c5fc159SVladimir Oltean * is in the FDB at an index smaller than the static one, or isn't (it 16876c5fc159SVladimir Oltean * can also be at a larger index, but in that case it is inactive 16886c5fc159SVladimir Oltean * because the static FDB entry will match first, and the dynamic one 16896c5fc159SVladimir Oltean * will eventually age out). Search for a dynamically learned address 16906c5fc159SVladimir Oltean * prior to our static one and invalidate it. 16916c5fc159SVladimir Oltean */ 16926c5fc159SVladimir Oltean tmp = l2_lookup; 16936c5fc159SVladimir Oltean 16946c5fc159SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 16956c5fc159SVladimir Oltean SJA1105_SEARCH, &tmp); 16966c5fc159SVladimir Oltean if (rc < 0) { 16976c5fc159SVladimir Oltean dev_err(ds->dev, 16986c5fc159SVladimir Oltean "port %d failed to read back entry for %pM vid %d: %pe\n", 16996c5fc159SVladimir Oltean port, addr, vid, ERR_PTR(rc)); 17006c5fc159SVladimir Oltean return rc; 17016c5fc159SVladimir Oltean } 17026c5fc159SVladimir Oltean 17036c5fc159SVladimir Oltean if (tmp.index < l2_lookup.index) { 17046c5fc159SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 17056c5fc159SVladimir Oltean tmp.index, NULL, false); 17066c5fc159SVladimir Oltean if (rc < 0) 17076c5fc159SVladimir Oltean return rc; 17086c5fc159SVladimir Oltean } 17096c5fc159SVladimir Oltean 171060f6053fSVladimir Oltean return sja1105_static_fdb_change(priv, port, &l2_lookup, true); 17119dfa6911SVladimir Oltean } 17129dfa6911SVladimir Oltean 17139dfa6911SVladimir Oltean int sja1105pqrs_fdb_del(struct dsa_switch *ds, int port, 17149dfa6911SVladimir Oltean const unsigned char *addr, u16 vid) 17159dfa6911SVladimir Oltean { 17161da73821SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 17171da73821SVladimir Oltean struct sja1105_private *priv = ds->priv; 17181da73821SVladimir Oltean bool keep; 17191da73821SVladimir Oltean int rc; 17201da73821SVladimir Oltean 17211da73821SVladimir Oltean l2_lookup.macaddr = ether_addr_to_u64(addr); 17221da73821SVladimir Oltean l2_lookup.vlanid = vid; 17231da73821SVladimir Oltean l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0); 17241da73821SVladimir Oltean l2_lookup.mask_vlanid = VLAN_VID_MASK; 17251da73821SVladimir Oltean l2_lookup.destports = BIT(port); 17261da73821SVladimir Oltean 17271da73821SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 17281da73821SVladimir Oltean SJA1105_SEARCH, &l2_lookup); 17291da73821SVladimir Oltean if (rc < 0) 17301da73821SVladimir Oltean return 0; 17311da73821SVladimir Oltean 17321da73821SVladimir Oltean l2_lookup.destports &= ~BIT(port); 17331da73821SVladimir Oltean 17341da73821SVladimir Oltean /* Decide whether we remove just this port from the FDB entry, 17351da73821SVladimir Oltean * or if we remove it completely. 17361da73821SVladimir Oltean */ 17371da73821SVladimir Oltean if (l2_lookup.destports) 17381da73821SVladimir Oltean keep = true; 17391da73821SVladimir Oltean else 17401da73821SVladimir Oltean keep = false; 17411da73821SVladimir Oltean 174260f6053fSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 17431da73821SVladimir Oltean l2_lookup.index, &l2_lookup, keep); 174460f6053fSVladimir Oltean if (rc < 0) 174560f6053fSVladimir Oltean return rc; 174660f6053fSVladimir Oltean 174760f6053fSVladimir Oltean return sja1105_static_fdb_change(priv, port, &l2_lookup, keep); 17489dfa6911SVladimir Oltean } 17499dfa6911SVladimir Oltean 17509dfa6911SVladimir Oltean static int sja1105_fdb_add(struct dsa_switch *ds, int port, 17519dfa6911SVladimir Oltean const unsigned char *addr, u16 vid) 17529dfa6911SVladimir Oltean { 17539dfa6911SVladimir Oltean struct sja1105_private *priv = ds->priv; 1754b3ee526aSVladimir Oltean 17556d7c7d94SVladimir Oltean return priv->info->fdb_add_cmd(ds, port, addr, vid); 17569dfa6911SVladimir Oltean } 17579dfa6911SVladimir Oltean 17589dfa6911SVladimir Oltean static int sja1105_fdb_del(struct dsa_switch *ds, int port, 17599dfa6911SVladimir Oltean const unsigned char *addr, u16 vid) 17609dfa6911SVladimir Oltean { 17619dfa6911SVladimir Oltean struct sja1105_private *priv = ds->priv; 17629dfa6911SVladimir Oltean 1763b3ee526aSVladimir Oltean return priv->info->fdb_del_cmd(ds, port, addr, vid); 17649dfa6911SVladimir Oltean } 17659dfa6911SVladimir Oltean 1766291d1e72SVladimir Oltean static int sja1105_fdb_dump(struct dsa_switch *ds, int port, 1767291d1e72SVladimir Oltean dsa_fdb_dump_cb_t *cb, void *data) 1768291d1e72SVladimir Oltean { 1769291d1e72SVladimir Oltean struct sja1105_private *priv = ds->priv; 1770291d1e72SVladimir Oltean struct device *dev = ds->dev; 1771291d1e72SVladimir Oltean int i; 1772291d1e72SVladimir Oltean 1773291d1e72SVladimir Oltean for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) { 1774291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 1775291d1e72SVladimir Oltean u8 macaddr[ETH_ALEN]; 1776291d1e72SVladimir Oltean int rc; 1777291d1e72SVladimir Oltean 1778291d1e72SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 1779291d1e72SVladimir Oltean i, &l2_lookup); 1780291d1e72SVladimir Oltean /* No fdb entry at i, not an issue */ 1781def84604SVladimir Oltean if (rc == -ENOENT) 1782291d1e72SVladimir Oltean continue; 1783291d1e72SVladimir Oltean if (rc) { 1784291d1e72SVladimir Oltean dev_err(dev, "Failed to dump FDB: %d\n", rc); 1785291d1e72SVladimir Oltean return rc; 1786291d1e72SVladimir Oltean } 1787291d1e72SVladimir Oltean 1788291d1e72SVladimir Oltean /* FDB dump callback is per port. This means we have to 1789291d1e72SVladimir Oltean * disregard a valid entry if it's not for this port, even if 1790291d1e72SVladimir Oltean * only to revisit it later. This is inefficient because the 1791291d1e72SVladimir Oltean * 1024-sized FDB table needs to be traversed 4 times through 1792291d1e72SVladimir Oltean * SPI during a 'bridge fdb show' command. 1793291d1e72SVladimir Oltean */ 1794291d1e72SVladimir Oltean if (!(l2_lookup.destports & BIT(port))) 1795291d1e72SVladimir Oltean continue; 17964d942354SVladimir Oltean 17974d942354SVladimir Oltean /* We need to hide the FDB entry for unknown multicast */ 17984d942354SVladimir Oltean if (l2_lookup.macaddr == SJA1105_UNKNOWN_MULTICAST && 17994d942354SVladimir Oltean l2_lookup.mask_macaddr == SJA1105_UNKNOWN_MULTICAST) 18004d942354SVladimir Oltean continue; 18014d942354SVladimir Oltean 1802291d1e72SVladimir Oltean u64_to_ether_addr(l2_lookup.macaddr, macaddr); 180393647594SVladimir Oltean 18046d7c7d94SVladimir Oltean /* We need to hide the dsa_8021q VLANs from the user. */ 18050fac6aa0SVladimir Oltean if (!priv->vlan_aware) 18066d7c7d94SVladimir Oltean l2_lookup.vlanid = 0; 180721b52fedSVladimir Oltean rc = cb(macaddr, l2_lookup.vlanid, l2_lookup.lockeds, data); 180821b52fedSVladimir Oltean if (rc) 180921b52fedSVladimir Oltean return rc; 1810291d1e72SVladimir Oltean } 1811291d1e72SVladimir Oltean return 0; 1812291d1e72SVladimir Oltean } 1813291d1e72SVladimir Oltean 18145126ec72SVladimir Oltean static void sja1105_fast_age(struct dsa_switch *ds, int port) 18155126ec72SVladimir Oltean { 18165126ec72SVladimir Oltean struct sja1105_private *priv = ds->priv; 18175126ec72SVladimir Oltean int i; 18185126ec72SVladimir Oltean 18195126ec72SVladimir Oltean for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) { 18205126ec72SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 18215126ec72SVladimir Oltean u8 macaddr[ETH_ALEN]; 18225126ec72SVladimir Oltean int rc; 18235126ec72SVladimir Oltean 18245126ec72SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 18255126ec72SVladimir Oltean i, &l2_lookup); 18265126ec72SVladimir Oltean /* No fdb entry at i, not an issue */ 18275126ec72SVladimir Oltean if (rc == -ENOENT) 18285126ec72SVladimir Oltean continue; 18295126ec72SVladimir Oltean if (rc) { 18305126ec72SVladimir Oltean dev_err(ds->dev, "Failed to read FDB: %pe\n", 18315126ec72SVladimir Oltean ERR_PTR(rc)); 18325126ec72SVladimir Oltean return; 18335126ec72SVladimir Oltean } 18345126ec72SVladimir Oltean 18355126ec72SVladimir Oltean if (!(l2_lookup.destports & BIT(port))) 18365126ec72SVladimir Oltean continue; 18375126ec72SVladimir Oltean 18385126ec72SVladimir Oltean /* Don't delete static FDB entries */ 18395126ec72SVladimir Oltean if (l2_lookup.lockeds) 18405126ec72SVladimir Oltean continue; 18415126ec72SVladimir Oltean 18425126ec72SVladimir Oltean u64_to_ether_addr(l2_lookup.macaddr, macaddr); 18435126ec72SVladimir Oltean 18445126ec72SVladimir Oltean rc = sja1105_fdb_del(ds, port, macaddr, l2_lookup.vlanid); 18455126ec72SVladimir Oltean if (rc) { 18465126ec72SVladimir Oltean dev_err(ds->dev, 18475126ec72SVladimir Oltean "Failed to delete FDB entry %pM vid %lld: %pe\n", 18485126ec72SVladimir Oltean macaddr, l2_lookup.vlanid, ERR_PTR(rc)); 18495126ec72SVladimir Oltean return; 18505126ec72SVladimir Oltean } 18515126ec72SVladimir Oltean } 18525126ec72SVladimir Oltean } 18535126ec72SVladimir Oltean 1854a52b2da7SVladimir Oltean static int sja1105_mdb_add(struct dsa_switch *ds, int port, 1855291d1e72SVladimir Oltean const struct switchdev_obj_port_mdb *mdb) 1856291d1e72SVladimir Oltean { 1857a52b2da7SVladimir Oltean return sja1105_fdb_add(ds, port, mdb->addr, mdb->vid); 1858291d1e72SVladimir Oltean } 1859291d1e72SVladimir Oltean 1860291d1e72SVladimir Oltean static int sja1105_mdb_del(struct dsa_switch *ds, int port, 1861291d1e72SVladimir Oltean const struct switchdev_obj_port_mdb *mdb) 1862291d1e72SVladimir Oltean { 1863291d1e72SVladimir Oltean return sja1105_fdb_del(ds, port, mdb->addr, mdb->vid); 1864291d1e72SVladimir Oltean } 1865291d1e72SVladimir Oltean 18667f7ccdeaSVladimir Oltean /* Common function for unicast and broadcast flood configuration. 18677f7ccdeaSVladimir Oltean * Flooding is configured between each {ingress, egress} port pair, and since 18687f7ccdeaSVladimir Oltean * the bridge's semantics are those of "egress flooding", it means we must 18697f7ccdeaSVladimir Oltean * enable flooding towards this port from all ingress ports that are in the 18707f7ccdeaSVladimir Oltean * same forwarding domain. 18717f7ccdeaSVladimir Oltean */ 18727f7ccdeaSVladimir Oltean static int sja1105_manage_flood_domains(struct sja1105_private *priv) 18737f7ccdeaSVladimir Oltean { 18747f7ccdeaSVladimir Oltean struct sja1105_l2_forwarding_entry *l2_fwd; 18757f7ccdeaSVladimir Oltean struct dsa_switch *ds = priv->ds; 18767f7ccdeaSVladimir Oltean int from, to, rc; 18777f7ccdeaSVladimir Oltean 18787f7ccdeaSVladimir Oltean l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries; 18797f7ccdeaSVladimir Oltean 18807f7ccdeaSVladimir Oltean for (from = 0; from < ds->num_ports; from++) { 18817f7ccdeaSVladimir Oltean u64 fl_domain = 0, bc_domain = 0; 18827f7ccdeaSVladimir Oltean 18837f7ccdeaSVladimir Oltean for (to = 0; to < priv->ds->num_ports; to++) { 18847f7ccdeaSVladimir Oltean if (!sja1105_can_forward(l2_fwd, from, to)) 18857f7ccdeaSVladimir Oltean continue; 18867f7ccdeaSVladimir Oltean 18877f7ccdeaSVladimir Oltean if (priv->ucast_egress_floods & BIT(to)) 18887f7ccdeaSVladimir Oltean fl_domain |= BIT(to); 18897f7ccdeaSVladimir Oltean if (priv->bcast_egress_floods & BIT(to)) 18907f7ccdeaSVladimir Oltean bc_domain |= BIT(to); 18917f7ccdeaSVladimir Oltean } 18927f7ccdeaSVladimir Oltean 18937f7ccdeaSVladimir Oltean /* Nothing changed, nothing to do */ 18947f7ccdeaSVladimir Oltean if (l2_fwd[from].fl_domain == fl_domain && 18957f7ccdeaSVladimir Oltean l2_fwd[from].bc_domain == bc_domain) 18967f7ccdeaSVladimir Oltean continue; 18977f7ccdeaSVladimir Oltean 18987f7ccdeaSVladimir Oltean l2_fwd[from].fl_domain = fl_domain; 18997f7ccdeaSVladimir Oltean l2_fwd[from].bc_domain = bc_domain; 19007f7ccdeaSVladimir Oltean 19017f7ccdeaSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING, 19027f7ccdeaSVladimir Oltean from, &l2_fwd[from], true); 19037f7ccdeaSVladimir Oltean if (rc < 0) 19047f7ccdeaSVladimir Oltean return rc; 19057f7ccdeaSVladimir Oltean } 19067f7ccdeaSVladimir Oltean 19077f7ccdeaSVladimir Oltean return 0; 19087f7ccdeaSVladimir Oltean } 19097f7ccdeaSVladimir Oltean 19108aa9ebccSVladimir Oltean static int sja1105_bridge_member(struct dsa_switch *ds, int port, 19118aa9ebccSVladimir Oltean struct net_device *br, bool member) 19128aa9ebccSVladimir Oltean { 19138aa9ebccSVladimir Oltean struct sja1105_l2_forwarding_entry *l2_fwd; 19148aa9ebccSVladimir Oltean struct sja1105_private *priv = ds->priv; 19158aa9ebccSVladimir Oltean int i, rc; 19168aa9ebccSVladimir Oltean 19178aa9ebccSVladimir Oltean l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries; 19188aa9ebccSVladimir Oltean 1919542043e9SVladimir Oltean for (i = 0; i < ds->num_ports; i++) { 19208aa9ebccSVladimir Oltean /* Add this port to the forwarding matrix of the 19218aa9ebccSVladimir Oltean * other ports in the same bridge, and viceversa. 19228aa9ebccSVladimir Oltean */ 19238aa9ebccSVladimir Oltean if (!dsa_is_user_port(ds, i)) 19248aa9ebccSVladimir Oltean continue; 19258aa9ebccSVladimir Oltean /* For the ports already under the bridge, only one thing needs 19268aa9ebccSVladimir Oltean * to be done, and that is to add this port to their 19278aa9ebccSVladimir Oltean * reachability domain. So we can perform the SPI write for 19288aa9ebccSVladimir Oltean * them immediately. However, for this port itself (the one 19298aa9ebccSVladimir Oltean * that is new to the bridge), we need to add all other ports 19308aa9ebccSVladimir Oltean * to its reachability domain. So we do that incrementally in 19318aa9ebccSVladimir Oltean * this loop, and perform the SPI write only at the end, once 19328aa9ebccSVladimir Oltean * the domain contains all other bridge ports. 19338aa9ebccSVladimir Oltean */ 19348aa9ebccSVladimir Oltean if (i == port) 19358aa9ebccSVladimir Oltean continue; 19368aa9ebccSVladimir Oltean if (dsa_to_port(ds, i)->bridge_dev != br) 19378aa9ebccSVladimir Oltean continue; 19388aa9ebccSVladimir Oltean sja1105_port_allow_traffic(l2_fwd, i, port, member); 19398aa9ebccSVladimir Oltean sja1105_port_allow_traffic(l2_fwd, port, i, member); 19408aa9ebccSVladimir Oltean 19418aa9ebccSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING, 19428aa9ebccSVladimir Oltean i, &l2_fwd[i], true); 19438aa9ebccSVladimir Oltean if (rc < 0) 19448aa9ebccSVladimir Oltean return rc; 19458aa9ebccSVladimir Oltean } 19468aa9ebccSVladimir Oltean 19477f7ccdeaSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING, 19488aa9ebccSVladimir Oltean port, &l2_fwd[port], true); 19497f7ccdeaSVladimir Oltean if (rc) 19507f7ccdeaSVladimir Oltean return rc; 19517f7ccdeaSVladimir Oltean 1952cde8078eSVladimir Oltean rc = sja1105_commit_pvid(ds, port); 1953cde8078eSVladimir Oltean if (rc) 1954cde8078eSVladimir Oltean return rc; 1955cde8078eSVladimir Oltean 19567f7ccdeaSVladimir Oltean return sja1105_manage_flood_domains(priv); 19578aa9ebccSVladimir Oltean } 19588aa9ebccSVladimir Oltean 1959640f763fSVladimir Oltean static void sja1105_bridge_stp_state_set(struct dsa_switch *ds, int port, 1960640f763fSVladimir Oltean u8 state) 1961640f763fSVladimir Oltean { 19625313a37bSVladimir Oltean struct dsa_port *dp = dsa_to_port(ds, port); 1963640f763fSVladimir Oltean struct sja1105_private *priv = ds->priv; 1964640f763fSVladimir Oltean struct sja1105_mac_config_entry *mac; 1965640f763fSVladimir Oltean 1966640f763fSVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 1967640f763fSVladimir Oltean 1968640f763fSVladimir Oltean switch (state) { 1969640f763fSVladimir Oltean case BR_STATE_DISABLED: 1970640f763fSVladimir Oltean case BR_STATE_BLOCKING: 1971640f763fSVladimir Oltean /* From UM10944 description of DRPDTAG (why put this there?): 1972640f763fSVladimir Oltean * "Management traffic flows to the port regardless of the state 1973640f763fSVladimir Oltean * of the INGRESS flag". So BPDUs are still be allowed to pass. 1974640f763fSVladimir Oltean * At the moment no difference between DISABLED and BLOCKING. 1975640f763fSVladimir Oltean */ 1976640f763fSVladimir Oltean mac[port].ingress = false; 1977640f763fSVladimir Oltean mac[port].egress = false; 1978640f763fSVladimir Oltean mac[port].dyn_learn = false; 1979640f763fSVladimir Oltean break; 1980640f763fSVladimir Oltean case BR_STATE_LISTENING: 1981640f763fSVladimir Oltean mac[port].ingress = true; 1982640f763fSVladimir Oltean mac[port].egress = false; 1983640f763fSVladimir Oltean mac[port].dyn_learn = false; 1984640f763fSVladimir Oltean break; 1985640f763fSVladimir Oltean case BR_STATE_LEARNING: 1986640f763fSVladimir Oltean mac[port].ingress = true; 1987640f763fSVladimir Oltean mac[port].egress = false; 19885313a37bSVladimir Oltean mac[port].dyn_learn = dp->learning; 1989640f763fSVladimir Oltean break; 1990640f763fSVladimir Oltean case BR_STATE_FORWARDING: 1991640f763fSVladimir Oltean mac[port].ingress = true; 1992640f763fSVladimir Oltean mac[port].egress = true; 19935313a37bSVladimir Oltean mac[port].dyn_learn = dp->learning; 1994640f763fSVladimir Oltean break; 1995640f763fSVladimir Oltean default: 1996640f763fSVladimir Oltean dev_err(ds->dev, "invalid STP state: %d\n", state); 1997640f763fSVladimir Oltean return; 1998640f763fSVladimir Oltean } 1999640f763fSVladimir Oltean 2000640f763fSVladimir Oltean sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, 2001640f763fSVladimir Oltean &mac[port], true); 2002640f763fSVladimir Oltean } 2003640f763fSVladimir Oltean 20048aa9ebccSVladimir Oltean static int sja1105_bridge_join(struct dsa_switch *ds, int port, 20058aa9ebccSVladimir Oltean struct net_device *br) 20068aa9ebccSVladimir Oltean { 20078aa9ebccSVladimir Oltean return sja1105_bridge_member(ds, port, br, true); 20088aa9ebccSVladimir Oltean } 20098aa9ebccSVladimir Oltean 20108aa9ebccSVladimir Oltean static void sja1105_bridge_leave(struct dsa_switch *ds, int port, 20118aa9ebccSVladimir Oltean struct net_device *br) 20128aa9ebccSVladimir Oltean { 20138aa9ebccSVladimir Oltean sja1105_bridge_member(ds, port, br, false); 20148aa9ebccSVladimir Oltean } 20158aa9ebccSVladimir Oltean 20164d752508SVladimir Oltean #define BYTES_PER_KBIT (1000LL / 8) 20174d752508SVladimir Oltean 20184d752508SVladimir Oltean static int sja1105_find_unused_cbs_shaper(struct sja1105_private *priv) 20194d752508SVladimir Oltean { 20204d752508SVladimir Oltean int i; 20214d752508SVladimir Oltean 20224d752508SVladimir Oltean for (i = 0; i < priv->info->num_cbs_shapers; i++) 20234d752508SVladimir Oltean if (!priv->cbs[i].idle_slope && !priv->cbs[i].send_slope) 20244d752508SVladimir Oltean return i; 20254d752508SVladimir Oltean 20264d752508SVladimir Oltean return -1; 20274d752508SVladimir Oltean } 20284d752508SVladimir Oltean 20294d752508SVladimir Oltean static int sja1105_delete_cbs_shaper(struct sja1105_private *priv, int port, 20304d752508SVladimir Oltean int prio) 20314d752508SVladimir Oltean { 20324d752508SVladimir Oltean int i; 20334d752508SVladimir Oltean 20344d752508SVladimir Oltean for (i = 0; i < priv->info->num_cbs_shapers; i++) { 20354d752508SVladimir Oltean struct sja1105_cbs_entry *cbs = &priv->cbs[i]; 20364d752508SVladimir Oltean 20374d752508SVladimir Oltean if (cbs->port == port && cbs->prio == prio) { 20384d752508SVladimir Oltean memset(cbs, 0, sizeof(*cbs)); 20394d752508SVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_CBS, 20404d752508SVladimir Oltean i, cbs, true); 20414d752508SVladimir Oltean } 20424d752508SVladimir Oltean } 20434d752508SVladimir Oltean 20444d752508SVladimir Oltean return 0; 20454d752508SVladimir Oltean } 20464d752508SVladimir Oltean 20474d752508SVladimir Oltean static int sja1105_setup_tc_cbs(struct dsa_switch *ds, int port, 20484d752508SVladimir Oltean struct tc_cbs_qopt_offload *offload) 20494d752508SVladimir Oltean { 20504d752508SVladimir Oltean struct sja1105_private *priv = ds->priv; 20514d752508SVladimir Oltean struct sja1105_cbs_entry *cbs; 20524d752508SVladimir Oltean int index; 20534d752508SVladimir Oltean 20544d752508SVladimir Oltean if (!offload->enable) 20554d752508SVladimir Oltean return sja1105_delete_cbs_shaper(priv, port, offload->queue); 20564d752508SVladimir Oltean 20574d752508SVladimir Oltean index = sja1105_find_unused_cbs_shaper(priv); 20584d752508SVladimir Oltean if (index < 0) 20594d752508SVladimir Oltean return -ENOSPC; 20604d752508SVladimir Oltean 20614d752508SVladimir Oltean cbs = &priv->cbs[index]; 20624d752508SVladimir Oltean cbs->port = port; 20634d752508SVladimir Oltean cbs->prio = offload->queue; 20644d752508SVladimir Oltean /* locredit and sendslope are negative by definition. In hardware, 20654d752508SVladimir Oltean * positive values must be provided, and the negative sign is implicit. 20664d752508SVladimir Oltean */ 20674d752508SVladimir Oltean cbs->credit_hi = offload->hicredit; 20684d752508SVladimir Oltean cbs->credit_lo = abs(offload->locredit); 20694d752508SVladimir Oltean /* User space is in kbits/sec, hardware in bytes/sec */ 20704d752508SVladimir Oltean cbs->idle_slope = offload->idleslope * BYTES_PER_KBIT; 20714d752508SVladimir Oltean cbs->send_slope = abs(offload->sendslope * BYTES_PER_KBIT); 20724d752508SVladimir Oltean /* Convert the negative values from 64-bit 2's complement 20734d752508SVladimir Oltean * to 32-bit 2's complement (for the case of 0x80000000 whose 20744d752508SVladimir Oltean * negative is still negative). 20754d752508SVladimir Oltean */ 20764d752508SVladimir Oltean cbs->credit_lo &= GENMASK_ULL(31, 0); 20774d752508SVladimir Oltean cbs->send_slope &= GENMASK_ULL(31, 0); 20784d752508SVladimir Oltean 20794d752508SVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_CBS, index, cbs, 20804d752508SVladimir Oltean true); 20814d752508SVladimir Oltean } 20824d752508SVladimir Oltean 20834d752508SVladimir Oltean static int sja1105_reload_cbs(struct sja1105_private *priv) 20844d752508SVladimir Oltean { 20854d752508SVladimir Oltean int rc = 0, i; 20864d752508SVladimir Oltean 2087be7f62eeSVladimir Oltean /* The credit based shapers are only allocated if 2088be7f62eeSVladimir Oltean * CONFIG_NET_SCH_CBS is enabled. 2089be7f62eeSVladimir Oltean */ 2090be7f62eeSVladimir Oltean if (!priv->cbs) 2091be7f62eeSVladimir Oltean return 0; 2092be7f62eeSVladimir Oltean 20934d752508SVladimir Oltean for (i = 0; i < priv->info->num_cbs_shapers; i++) { 20944d752508SVladimir Oltean struct sja1105_cbs_entry *cbs = &priv->cbs[i]; 20954d752508SVladimir Oltean 20964d752508SVladimir Oltean if (!cbs->idle_slope && !cbs->send_slope) 20974d752508SVladimir Oltean continue; 20984d752508SVladimir Oltean 20994d752508SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_CBS, i, cbs, 21004d752508SVladimir Oltean true); 21014d752508SVladimir Oltean if (rc) 21024d752508SVladimir Oltean break; 21034d752508SVladimir Oltean } 21044d752508SVladimir Oltean 21054d752508SVladimir Oltean return rc; 21064d752508SVladimir Oltean } 21074d752508SVladimir Oltean 21082eea1fa8SVladimir Oltean static const char * const sja1105_reset_reasons[] = { 21092eea1fa8SVladimir Oltean [SJA1105_VLAN_FILTERING] = "VLAN filtering", 21102eea1fa8SVladimir Oltean [SJA1105_RX_HWTSTAMPING] = "RX timestamping", 21112eea1fa8SVladimir Oltean [SJA1105_AGEING_TIME] = "Ageing time", 21122eea1fa8SVladimir Oltean [SJA1105_SCHEDULING] = "Time-aware scheduling", 2113c279c726SVladimir Oltean [SJA1105_BEST_EFFORT_POLICING] = "Best-effort policing", 2114dfacc5a2SVladimir Oltean [SJA1105_VIRTUAL_LINKS] = "Virtual links", 21152eea1fa8SVladimir Oltean }; 21162eea1fa8SVladimir Oltean 21176666cebcSVladimir Oltean /* For situations where we need to change a setting at runtime that is only 21186666cebcSVladimir Oltean * available through the static configuration, resetting the switch in order 21196666cebcSVladimir Oltean * to upload the new static config is unavoidable. Back up the settings we 21206666cebcSVladimir Oltean * modify at runtime (currently only MAC) and restore them after uploading, 21216666cebcSVladimir Oltean * such that this operation is relatively seamless. 21226666cebcSVladimir Oltean */ 21232eea1fa8SVladimir Oltean int sja1105_static_config_reload(struct sja1105_private *priv, 21242eea1fa8SVladimir Oltean enum sja1105_reset_reason reason) 21256666cebcSVladimir Oltean { 21266cf99c13SVladimir Oltean struct ptp_system_timestamp ptp_sts_before; 21276cf99c13SVladimir Oltean struct ptp_system_timestamp ptp_sts_after; 212882760d7fSVladimir Oltean int speed_mbps[SJA1105_MAX_NUM_PORTS]; 212984db00f2SVladimir Oltean u16 bmcr[SJA1105_MAX_NUM_PORTS] = {0}; 21306666cebcSVladimir Oltean struct sja1105_mac_config_entry *mac; 21316cf99c13SVladimir Oltean struct dsa_switch *ds = priv->ds; 21326cf99c13SVladimir Oltean s64 t1, t2, t3, t4; 21336cf99c13SVladimir Oltean s64 t12, t34; 21346666cebcSVladimir Oltean int rc, i; 21356cf99c13SVladimir Oltean s64 now; 21366666cebcSVladimir Oltean 2137af580ae2SVladimir Oltean mutex_lock(&priv->mgmt_lock); 2138af580ae2SVladimir Oltean 21396666cebcSVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 21406666cebcSVladimir Oltean 21418400cff6SVladimir Oltean /* Back up the dynamic link speed changed by sja1105_adjust_port_config 21428400cff6SVladimir Oltean * in order to temporarily restore it to SJA1105_SPEED_AUTO - which the 21438400cff6SVladimir Oltean * switch wants to see in the static config in order to allow us to 21448400cff6SVladimir Oltean * change it through the dynamic interface later. 21456666cebcSVladimir Oltean */ 2146542043e9SVladimir Oltean for (i = 0; i < ds->num_ports; i++) { 21473ad1d171SVladimir Oltean u32 reg_addr = mdiobus_c45_addr(MDIO_MMD_VEND2, MDIO_CTRL1); 21483ad1d171SVladimir Oltean 214941fed17fSVladimir Oltean speed_mbps[i] = sja1105_port_speed_to_ethtool(priv, 215041fed17fSVladimir Oltean mac[i].speed); 215141fed17fSVladimir Oltean mac[i].speed = priv->info->port_speed[SJA1105_SPEED_AUTO]; 21526666cebcSVladimir Oltean 21533ad1d171SVladimir Oltean if (priv->xpcs[i]) 21543ad1d171SVladimir Oltean bmcr[i] = mdiobus_read(priv->mdio_pcs, i, reg_addr); 215584db00f2SVladimir Oltean } 2156ffe10e67SVladimir Oltean 21576cf99c13SVladimir Oltean /* No PTP operations can run right now */ 21586cf99c13SVladimir Oltean mutex_lock(&priv->ptp_data.lock); 21596cf99c13SVladimir Oltean 21606cf99c13SVladimir Oltean rc = __sja1105_ptp_gettimex(ds, &now, &ptp_sts_before); 216161c77533SVladimir Oltean if (rc < 0) { 216261c77533SVladimir Oltean mutex_unlock(&priv->ptp_data.lock); 216361c77533SVladimir Oltean goto out; 216461c77533SVladimir Oltean } 21656cf99c13SVladimir Oltean 21666666cebcSVladimir Oltean /* Reset switch and send updated static configuration */ 21676666cebcSVladimir Oltean rc = sja1105_static_config_upload(priv); 216861c77533SVladimir Oltean if (rc < 0) { 216961c77533SVladimir Oltean mutex_unlock(&priv->ptp_data.lock); 217061c77533SVladimir Oltean goto out; 217161c77533SVladimir Oltean } 21726cf99c13SVladimir Oltean 21736cf99c13SVladimir Oltean rc = __sja1105_ptp_settime(ds, 0, &ptp_sts_after); 217461c77533SVladimir Oltean if (rc < 0) { 217561c77533SVladimir Oltean mutex_unlock(&priv->ptp_data.lock); 217661c77533SVladimir Oltean goto out; 217761c77533SVladimir Oltean } 21786cf99c13SVladimir Oltean 21796cf99c13SVladimir Oltean t1 = timespec64_to_ns(&ptp_sts_before.pre_ts); 21806cf99c13SVladimir Oltean t2 = timespec64_to_ns(&ptp_sts_before.post_ts); 21816cf99c13SVladimir Oltean t3 = timespec64_to_ns(&ptp_sts_after.pre_ts); 21826cf99c13SVladimir Oltean t4 = timespec64_to_ns(&ptp_sts_after.post_ts); 21836cf99c13SVladimir Oltean /* Mid point, corresponds to pre-reset PTPCLKVAL */ 21846cf99c13SVladimir Oltean t12 = t1 + (t2 - t1) / 2; 21856cf99c13SVladimir Oltean /* Mid point, corresponds to post-reset PTPCLKVAL, aka 0 */ 21866cf99c13SVladimir Oltean t34 = t3 + (t4 - t3) / 2; 21876cf99c13SVladimir Oltean /* Advance PTPCLKVAL by the time it took since its readout */ 21886cf99c13SVladimir Oltean now += (t34 - t12); 21896cf99c13SVladimir Oltean 21906cf99c13SVladimir Oltean __sja1105_ptp_adjtime(ds, now); 21916cf99c13SVladimir Oltean 21926cf99c13SVladimir Oltean mutex_unlock(&priv->ptp_data.lock); 21936666cebcSVladimir Oltean 21942eea1fa8SVladimir Oltean dev_info(priv->ds->dev, 21952eea1fa8SVladimir Oltean "Reset switch and programmed static config. Reason: %s\n", 21962eea1fa8SVladimir Oltean sja1105_reset_reasons[reason]); 21972eea1fa8SVladimir Oltean 21986666cebcSVladimir Oltean /* Configure the CGU (PLLs) for MII and RMII PHYs. 21996666cebcSVladimir Oltean * For these interfaces there is no dynamic configuration 22006666cebcSVladimir Oltean * needed, since PLLs have same settings at all speeds. 22016666cebcSVladimir Oltean */ 2202cb5a82d2SVladimir Oltean if (priv->info->clocking_setup) { 2203c5037678SVladimir Oltean rc = priv->info->clocking_setup(priv); 22046666cebcSVladimir Oltean if (rc < 0) 22056666cebcSVladimir Oltean goto out; 2206cb5a82d2SVladimir Oltean } 22076666cebcSVladimir Oltean 2208542043e9SVladimir Oltean for (i = 0; i < ds->num_ports; i++) { 22093ad1d171SVladimir Oltean struct dw_xpcs *xpcs = priv->xpcs[i]; 22103ad1d171SVladimir Oltean unsigned int mode; 221184db00f2SVladimir Oltean 22128400cff6SVladimir Oltean rc = sja1105_adjust_port_config(priv, i, speed_mbps[i]); 22136666cebcSVladimir Oltean if (rc < 0) 22146666cebcSVladimir Oltean goto out; 2215ffe10e67SVladimir Oltean 22163ad1d171SVladimir Oltean if (!xpcs) 221784db00f2SVladimir Oltean continue; 2218ffe10e67SVladimir Oltean 22193ad1d171SVladimir Oltean if (bmcr[i] & BMCR_ANENABLE) 22203ad1d171SVladimir Oltean mode = MLO_AN_INBAND; 22213ad1d171SVladimir Oltean else if (priv->fixed_link[i]) 22223ad1d171SVladimir Oltean mode = MLO_AN_FIXED; 22233ad1d171SVladimir Oltean else 22243ad1d171SVladimir Oltean mode = MLO_AN_PHY; 222584db00f2SVladimir Oltean 22263ad1d171SVladimir Oltean rc = xpcs_do_config(xpcs, priv->phy_mode[i], mode); 22273ad1d171SVladimir Oltean if (rc < 0) 22283ad1d171SVladimir Oltean goto out; 2229ffe10e67SVladimir Oltean 22303ad1d171SVladimir Oltean if (!phylink_autoneg_inband(mode)) { 2231ffe10e67SVladimir Oltean int speed = SPEED_UNKNOWN; 2232ffe10e67SVladimir Oltean 223356b63466SVladimir Oltean if (priv->phy_mode[i] == PHY_INTERFACE_MODE_2500BASEX) 223456b63466SVladimir Oltean speed = SPEED_2500; 223556b63466SVladimir Oltean else if (bmcr[i] & BMCR_SPEED1000) 2236ffe10e67SVladimir Oltean speed = SPEED_1000; 223784db00f2SVladimir Oltean else if (bmcr[i] & BMCR_SPEED100) 2238ffe10e67SVladimir Oltean speed = SPEED_100; 2239053d8ad1SVladimir Oltean else 2240ffe10e67SVladimir Oltean speed = SPEED_10; 2241ffe10e67SVladimir Oltean 22423ad1d171SVladimir Oltean xpcs_link_up(&xpcs->pcs, mode, priv->phy_mode[i], 22433ad1d171SVladimir Oltean speed, DUPLEX_FULL); 2244ffe10e67SVladimir Oltean } 2245ffe10e67SVladimir Oltean } 22464d752508SVladimir Oltean 22474d752508SVladimir Oltean rc = sja1105_reload_cbs(priv); 22484d752508SVladimir Oltean if (rc < 0) 22494d752508SVladimir Oltean goto out; 22506666cebcSVladimir Oltean out: 2251af580ae2SVladimir Oltean mutex_unlock(&priv->mgmt_lock); 2252af580ae2SVladimir Oltean 22536666cebcSVladimir Oltean return rc; 22546666cebcSVladimir Oltean } 22556666cebcSVladimir Oltean 22568aa9ebccSVladimir Oltean static enum dsa_tag_protocol 22574d776482SFlorian Fainelli sja1105_get_tag_protocol(struct dsa_switch *ds, int port, 22584d776482SFlorian Fainelli enum dsa_tag_protocol mp) 22598aa9ebccSVladimir Oltean { 22604913b8ebSVladimir Oltean struct sja1105_private *priv = ds->priv; 22614913b8ebSVladimir Oltean 22624913b8ebSVladimir Oltean return priv->info->tag_proto; 22638aa9ebccSVladimir Oltean } 22648aa9ebccSVladimir Oltean 2265070ca3bbSVladimir Oltean /* The TPID setting belongs to the General Parameters table, 2266070ca3bbSVladimir Oltean * which can only be partially reconfigured at runtime (and not the TPID). 2267070ca3bbSVladimir Oltean * So a switch reset is required. 2268070ca3bbSVladimir Oltean */ 226989153ed6SVladimir Oltean int sja1105_vlan_filtering(struct dsa_switch *ds, int port, bool enabled, 227089153ed6SVladimir Oltean struct netlink_ext_ack *extack) 22716666cebcSVladimir Oltean { 22726d7c7d94SVladimir Oltean struct sja1105_l2_lookup_params_entry *l2_lookup_params; 2273070ca3bbSVladimir Oltean struct sja1105_general_params_entry *general_params; 22746666cebcSVladimir Oltean struct sja1105_private *priv = ds->priv; 2275070ca3bbSVladimir Oltean struct sja1105_table *table; 2276dfacc5a2SVladimir Oltean struct sja1105_rule *rule; 2277070ca3bbSVladimir Oltean u16 tpid, tpid2; 22786666cebcSVladimir Oltean int rc; 22796666cebcSVladimir Oltean 2280dfacc5a2SVladimir Oltean list_for_each_entry(rule, &priv->flow_block.rules, list) { 2281dfacc5a2SVladimir Oltean if (rule->type == SJA1105_RULE_VL) { 228289153ed6SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 228389153ed6SVladimir Oltean "Cannot change VLAN filtering with active VL rules"); 2284dfacc5a2SVladimir Oltean return -EBUSY; 2285dfacc5a2SVladimir Oltean } 2286dfacc5a2SVladimir Oltean } 2287dfacc5a2SVladimir Oltean 2288070ca3bbSVladimir Oltean if (enabled) { 22896666cebcSVladimir Oltean /* Enable VLAN filtering. */ 229054fa49eeSVladimir Oltean tpid = ETH_P_8021Q; 229154fa49eeSVladimir Oltean tpid2 = ETH_P_8021AD; 2292070ca3bbSVladimir Oltean } else { 22936666cebcSVladimir Oltean /* Disable VLAN filtering. */ 2294070ca3bbSVladimir Oltean tpid = ETH_P_SJA1105; 2295070ca3bbSVladimir Oltean tpid2 = ETH_P_SJA1105; 2296070ca3bbSVladimir Oltean } 2297070ca3bbSVladimir Oltean 229838b5beeaSVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 229938b5beeaSVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 230038b5beeaSVladimir Oltean 230138b5beeaSVladimir Oltean if (enabled) 230238b5beeaSVladimir Oltean sp->xmit_tpid = priv->info->qinq_tpid; 230338b5beeaSVladimir Oltean else 230438b5beeaSVladimir Oltean sp->xmit_tpid = ETH_P_SJA1105; 230538b5beeaSVladimir Oltean } 230638b5beeaSVladimir Oltean 23070fac6aa0SVladimir Oltean if (priv->vlan_aware == enabled) 2308cfa36b1fSVladimir Oltean return 0; 2309cfa36b1fSVladimir Oltean 23100fac6aa0SVladimir Oltean priv->vlan_aware = enabled; 23117f14937fSVladimir Oltean 2312070ca3bbSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; 2313070ca3bbSVladimir Oltean general_params = table->entries; 2314f9a1a764SVladimir Oltean /* EtherType used to identify inner tagged (C-tag) VLAN traffic */ 231554fa49eeSVladimir Oltean general_params->tpid = tpid; 231654fa49eeSVladimir Oltean /* EtherType used to identify outer tagged (S-tag) VLAN traffic */ 2317070ca3bbSVladimir Oltean general_params->tpid2 = tpid2; 231842824463SVladimir Oltean /* When VLAN filtering is on, we need to at least be able to 231942824463SVladimir Oltean * decode management traffic through the "backup plan". 232042824463SVladimir Oltean */ 232142824463SVladimir Oltean general_params->incl_srcpt1 = enabled; 232242824463SVladimir Oltean general_params->incl_srcpt0 = enabled; 2323070ca3bbSVladimir Oltean 23246d7c7d94SVladimir Oltean /* VLAN filtering => independent VLAN learning. 23252cafa72eSVladimir Oltean * No VLAN filtering (or best effort) => shared VLAN learning. 23266d7c7d94SVladimir Oltean * 23276d7c7d94SVladimir Oltean * In shared VLAN learning mode, untagged traffic still gets 23286d7c7d94SVladimir Oltean * pvid-tagged, and the FDB table gets populated with entries 23296d7c7d94SVladimir Oltean * containing the "real" (pvid or from VLAN tag) VLAN ID. 23306d7c7d94SVladimir Oltean * However the switch performs a masked L2 lookup in the FDB, 23316d7c7d94SVladimir Oltean * effectively only looking up a frame's DMAC (and not VID) for the 23326d7c7d94SVladimir Oltean * forwarding decision. 23336d7c7d94SVladimir Oltean * 23346d7c7d94SVladimir Oltean * This is extremely convenient for us, because in modes with 23356d7c7d94SVladimir Oltean * vlan_filtering=0, dsa_8021q actually installs unique pvid's into 23366d7c7d94SVladimir Oltean * each front panel port. This is good for identification but breaks 23376d7c7d94SVladimir Oltean * learning badly - the VID of the learnt FDB entry is unique, aka 23386d7c7d94SVladimir Oltean * no frames coming from any other port are going to have it. So 23396d7c7d94SVladimir Oltean * for forwarding purposes, this is as though learning was broken 23406d7c7d94SVladimir Oltean * (all frames get flooded). 23416d7c7d94SVladimir Oltean */ 23426d7c7d94SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS]; 23436d7c7d94SVladimir Oltean l2_lookup_params = table->entries; 23440fac6aa0SVladimir Oltean l2_lookup_params->shared_learn = !priv->vlan_aware; 2345aaa270c6SVladimir Oltean 23466dfd23d3SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 23476dfd23d3SVladimir Oltean if (dsa_is_unused_port(ds, port)) 23486dfd23d3SVladimir Oltean continue; 23496dfd23d3SVladimir Oltean 23506dfd23d3SVladimir Oltean rc = sja1105_commit_pvid(ds, port); 2351aef31718SVladimir Oltean if (rc) 2352aef31718SVladimir Oltean return rc; 23536dfd23d3SVladimir Oltean } 2354aef31718SVladimir Oltean 23552eea1fa8SVladimir Oltean rc = sja1105_static_config_reload(priv, SJA1105_VLAN_FILTERING); 23566666cebcSVladimir Oltean if (rc) 235789153ed6SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, "Failed to change VLAN Ethertype"); 23586666cebcSVladimir Oltean 23590fac6aa0SVladimir Oltean return rc; 23606666cebcSVladimir Oltean } 23616666cebcSVladimir Oltean 23626dfd23d3SVladimir Oltean static int sja1105_vlan_add(struct sja1105_private *priv, int port, u16 vid, 236373ceab83SVladimir Oltean u16 flags, bool allowed_ingress) 23645899ee36SVladimir Oltean { 23656dfd23d3SVladimir Oltean struct sja1105_vlan_lookup_entry *vlan; 23666dfd23d3SVladimir Oltean struct sja1105_table *table; 23676dfd23d3SVladimir Oltean int match, rc; 23685899ee36SVladimir Oltean 23696dfd23d3SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP]; 23706dfd23d3SVladimir Oltean 23716dfd23d3SVladimir Oltean match = sja1105_is_vlan_configured(priv, vid); 23726dfd23d3SVladimir Oltean if (match < 0) { 23736dfd23d3SVladimir Oltean rc = sja1105_table_resize(table, table->entry_count + 1); 23746dfd23d3SVladimir Oltean if (rc) 23756dfd23d3SVladimir Oltean return rc; 23766dfd23d3SVladimir Oltean match = table->entry_count - 1; 23776dfd23d3SVladimir Oltean } 23786dfd23d3SVladimir Oltean 23796dfd23d3SVladimir Oltean /* Assign pointer after the resize (it's new memory) */ 23806dfd23d3SVladimir Oltean vlan = table->entries; 23816dfd23d3SVladimir Oltean 23826dfd23d3SVladimir Oltean vlan[match].type_entry = SJA1110_VLAN_D_TAG; 23836dfd23d3SVladimir Oltean vlan[match].vlanid = vid; 23846dfd23d3SVladimir Oltean vlan[match].vlan_bc |= BIT(port); 238573ceab83SVladimir Oltean 238673ceab83SVladimir Oltean if (allowed_ingress) 23876dfd23d3SVladimir Oltean vlan[match].vmemb_port |= BIT(port); 238873ceab83SVladimir Oltean else 238973ceab83SVladimir Oltean vlan[match].vmemb_port &= ~BIT(port); 239073ceab83SVladimir Oltean 23916dfd23d3SVladimir Oltean if (flags & BRIDGE_VLAN_INFO_UNTAGGED) 23926dfd23d3SVladimir Oltean vlan[match].tag_port &= ~BIT(port); 23936dfd23d3SVladimir Oltean else 23946dfd23d3SVladimir Oltean vlan[match].tag_port |= BIT(port); 23956dfd23d3SVladimir Oltean 23966dfd23d3SVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_VLAN_LOOKUP, vid, 23976dfd23d3SVladimir Oltean &vlan[match], true); 23986dfd23d3SVladimir Oltean } 23996dfd23d3SVladimir Oltean 24006dfd23d3SVladimir Oltean static int sja1105_vlan_del(struct sja1105_private *priv, int port, u16 vid) 24016dfd23d3SVladimir Oltean { 24026dfd23d3SVladimir Oltean struct sja1105_vlan_lookup_entry *vlan; 24036dfd23d3SVladimir Oltean struct sja1105_table *table; 24046dfd23d3SVladimir Oltean bool keep = true; 24056dfd23d3SVladimir Oltean int match, rc; 24066dfd23d3SVladimir Oltean 24076dfd23d3SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP]; 24086dfd23d3SVladimir Oltean 24096dfd23d3SVladimir Oltean match = sja1105_is_vlan_configured(priv, vid); 24106dfd23d3SVladimir Oltean /* Can't delete a missing entry. */ 24116dfd23d3SVladimir Oltean if (match < 0) 24125899ee36SVladimir Oltean return 0; 24135899ee36SVladimir Oltean 24146dfd23d3SVladimir Oltean /* Assign pointer after the resize (it's new memory) */ 24156dfd23d3SVladimir Oltean vlan = table->entries; 24166dfd23d3SVladimir Oltean 24176dfd23d3SVladimir Oltean vlan[match].vlanid = vid; 24186dfd23d3SVladimir Oltean vlan[match].vlan_bc &= ~BIT(port); 24196dfd23d3SVladimir Oltean vlan[match].vmemb_port &= ~BIT(port); 24206dfd23d3SVladimir Oltean /* Also unset tag_port, just so we don't have a confusing bitmap 24216dfd23d3SVladimir Oltean * (no practical purpose). 2422b38e659dSVladimir Oltean */ 24236dfd23d3SVladimir Oltean vlan[match].tag_port &= ~BIT(port); 2424b38e659dSVladimir Oltean 24256dfd23d3SVladimir Oltean /* If there's no port left as member of this VLAN, 24266dfd23d3SVladimir Oltean * it's time for it to go. 24276dfd23d3SVladimir Oltean */ 24286dfd23d3SVladimir Oltean if (!vlan[match].vmemb_port) 24296dfd23d3SVladimir Oltean keep = false; 24305899ee36SVladimir Oltean 24316dfd23d3SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_VLAN_LOOKUP, vid, 24326dfd23d3SVladimir Oltean &vlan[match], keep); 24336dfd23d3SVladimir Oltean if (rc < 0) 24346dfd23d3SVladimir Oltean return rc; 24355899ee36SVladimir Oltean 24366dfd23d3SVladimir Oltean if (!keep) 24376dfd23d3SVladimir Oltean return sja1105_table_delete_entry(table, match); 24385899ee36SVladimir Oltean 24395899ee36SVladimir Oltean return 0; 24405899ee36SVladimir Oltean } 24415899ee36SVladimir Oltean 24426dfd23d3SVladimir Oltean static int sja1105_bridge_vlan_add(struct dsa_switch *ds, int port, 244331046a5fSVladimir Oltean const struct switchdev_obj_port_vlan *vlan, 244431046a5fSVladimir Oltean struct netlink_ext_ack *extack) 24456666cebcSVladimir Oltean { 24466666cebcSVladimir Oltean struct sja1105_private *priv = ds->priv; 2447884be12fSVladimir Oltean u16 flags = vlan->flags; 24486666cebcSVladimir Oltean int rc; 24496666cebcSVladimir Oltean 24500fac6aa0SVladimir Oltean /* Be sure to deny alterations to the configuration done by tag_8021q. 24511958d581SVladimir Oltean */ 24520fac6aa0SVladimir Oltean if (vid_is_dsa_8021q(vlan->vid)) { 245331046a5fSVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 245431046a5fSVladimir Oltean "Range 1024-3071 reserved for dsa_8021q operation"); 24551958d581SVladimir Oltean return -EBUSY; 24561958d581SVladimir Oltean } 24571958d581SVladimir Oltean 2458c5130029SVladimir Oltean /* Always install bridge VLANs as egress-tagged on CPU and DSA ports */ 2459c5130029SVladimir Oltean if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) 2460884be12fSVladimir Oltean flags = 0; 2461884be12fSVladimir Oltean 246273ceab83SVladimir Oltean rc = sja1105_vlan_add(priv, port, vlan->vid, flags, true); 24636dfd23d3SVladimir Oltean if (rc) 24641958d581SVladimir Oltean return rc; 2465ec5ae610SVladimir Oltean 24666dfd23d3SVladimir Oltean if (vlan->flags & BRIDGE_VLAN_INFO_PVID) 24676dfd23d3SVladimir Oltean priv->bridge_pvid[port] = vlan->vid; 2468ec5ae610SVladimir Oltean 24696dfd23d3SVladimir Oltean return sja1105_commit_pvid(ds, port); 24706666cebcSVladimir Oltean } 24716666cebcSVladimir Oltean 24726dfd23d3SVladimir Oltean static int sja1105_bridge_vlan_del(struct dsa_switch *ds, int port, 24736666cebcSVladimir Oltean const struct switchdev_obj_port_vlan *vlan) 24746666cebcSVladimir Oltean { 24756666cebcSVladimir Oltean struct sja1105_private *priv = ds->priv; 2476bef0746cSVladimir Oltean int rc; 24776666cebcSVladimir Oltean 2478bef0746cSVladimir Oltean rc = sja1105_vlan_del(priv, port, vlan->vid); 2479bef0746cSVladimir Oltean if (rc) 2480bef0746cSVladimir Oltean return rc; 2481bef0746cSVladimir Oltean 2482bef0746cSVladimir Oltean /* In case the pvid was deleted, make sure that untagged packets will 2483bef0746cSVladimir Oltean * be dropped. 2484bef0746cSVladimir Oltean */ 2485bef0746cSVladimir Oltean return sja1105_commit_pvid(ds, port); 24866666cebcSVladimir Oltean } 24876666cebcSVladimir Oltean 24885899ee36SVladimir Oltean static int sja1105_dsa_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid, 24895899ee36SVladimir Oltean u16 flags) 24905899ee36SVladimir Oltean { 24915899ee36SVladimir Oltean struct sja1105_private *priv = ds->priv; 249273ceab83SVladimir Oltean bool allowed_ingress = true; 24935899ee36SVladimir Oltean int rc; 24945899ee36SVladimir Oltean 249573ceab83SVladimir Oltean /* Prevent attackers from trying to inject a DSA tag from 249673ceab83SVladimir Oltean * the outside world. 249773ceab83SVladimir Oltean */ 249873ceab83SVladimir Oltean if (dsa_is_user_port(ds, port)) 249973ceab83SVladimir Oltean allowed_ingress = false; 250073ceab83SVladimir Oltean 250173ceab83SVladimir Oltean rc = sja1105_vlan_add(priv, port, vid, flags, allowed_ingress); 25026dfd23d3SVladimir Oltean if (rc) 25035899ee36SVladimir Oltean return rc; 25045899ee36SVladimir Oltean 25056dfd23d3SVladimir Oltean if (flags & BRIDGE_VLAN_INFO_PVID) 25066dfd23d3SVladimir Oltean priv->tag_8021q_pvid[port] = vid; 25076dfd23d3SVladimir Oltean 25086dfd23d3SVladimir Oltean return sja1105_commit_pvid(ds, port); 25095899ee36SVladimir Oltean } 25105899ee36SVladimir Oltean 25115899ee36SVladimir Oltean static int sja1105_dsa_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid) 25125899ee36SVladimir Oltean { 25135899ee36SVladimir Oltean struct sja1105_private *priv = ds->priv; 25145899ee36SVladimir Oltean 25156dfd23d3SVladimir Oltean return sja1105_vlan_del(priv, port, vid); 25165899ee36SVladimir Oltean } 25175899ee36SVladimir Oltean 25184fbc08bdSVladimir Oltean static int sja1105_prechangeupper(struct dsa_switch *ds, int port, 25194fbc08bdSVladimir Oltean struct netdev_notifier_changeupper_info *info) 25204fbc08bdSVladimir Oltean { 25214fbc08bdSVladimir Oltean struct netlink_ext_ack *extack = info->info.extack; 25224fbc08bdSVladimir Oltean struct net_device *upper = info->upper_dev; 252319fa937aSVladimir Oltean struct dsa_switch_tree *dst = ds->dst; 252419fa937aSVladimir Oltean struct dsa_port *dp; 25254fbc08bdSVladimir Oltean 25264fbc08bdSVladimir Oltean if (is_vlan_dev(upper)) { 25274fbc08bdSVladimir Oltean NL_SET_ERR_MSG_MOD(extack, "8021q uppers are not supported"); 25284fbc08bdSVladimir Oltean return -EBUSY; 25294fbc08bdSVladimir Oltean } 25304fbc08bdSVladimir Oltean 253119fa937aSVladimir Oltean if (netif_is_bridge_master(upper)) { 253219fa937aSVladimir Oltean list_for_each_entry(dp, &dst->ports, list) { 253319fa937aSVladimir Oltean if (dp->bridge_dev && dp->bridge_dev != upper && 253419fa937aSVladimir Oltean br_vlan_enabled(dp->bridge_dev)) { 253519fa937aSVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 253619fa937aSVladimir Oltean "Only one VLAN-aware bridge is supported"); 253719fa937aSVladimir Oltean return -EBUSY; 253819fa937aSVladimir Oltean } 253919fa937aSVladimir Oltean } 254019fa937aSVladimir Oltean } 254119fa937aSVladimir Oltean 25424fbc08bdSVladimir Oltean return 0; 25434fbc08bdSVladimir Oltean } 25444fbc08bdSVladimir Oltean 2545a68578c2SVladimir Oltean static void sja1105_port_disable(struct dsa_switch *ds, int port) 2546a68578c2SVladimir Oltean { 2547a68578c2SVladimir Oltean struct sja1105_private *priv = ds->priv; 2548a68578c2SVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 2549a68578c2SVladimir Oltean 2550a68578c2SVladimir Oltean if (!dsa_is_user_port(ds, port)) 2551a68578c2SVladimir Oltean return; 2552a68578c2SVladimir Oltean 2553a68578c2SVladimir Oltean kthread_cancel_work_sync(&sp->xmit_work); 2554a68578c2SVladimir Oltean skb_queue_purge(&sp->xmit_queue); 2555a68578c2SVladimir Oltean } 2556a68578c2SVladimir Oltean 2557227d07a0SVladimir Oltean static int sja1105_mgmt_xmit(struct dsa_switch *ds, int port, int slot, 255847ed985eSVladimir Oltean struct sk_buff *skb, bool takets) 2559227d07a0SVladimir Oltean { 2560227d07a0SVladimir Oltean struct sja1105_mgmt_entry mgmt_route = {0}; 2561227d07a0SVladimir Oltean struct sja1105_private *priv = ds->priv; 2562227d07a0SVladimir Oltean struct ethhdr *hdr; 2563227d07a0SVladimir Oltean int timeout = 10; 2564227d07a0SVladimir Oltean int rc; 2565227d07a0SVladimir Oltean 2566227d07a0SVladimir Oltean hdr = eth_hdr(skb); 2567227d07a0SVladimir Oltean 2568227d07a0SVladimir Oltean mgmt_route.macaddr = ether_addr_to_u64(hdr->h_dest); 2569227d07a0SVladimir Oltean mgmt_route.destports = BIT(port); 2570227d07a0SVladimir Oltean mgmt_route.enfport = 1; 257147ed985eSVladimir Oltean mgmt_route.tsreg = 0; 257247ed985eSVladimir Oltean mgmt_route.takets = takets; 2573227d07a0SVladimir Oltean 2574227d07a0SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE, 2575227d07a0SVladimir Oltean slot, &mgmt_route, true); 2576227d07a0SVladimir Oltean if (rc < 0) { 2577227d07a0SVladimir Oltean kfree_skb(skb); 2578227d07a0SVladimir Oltean return rc; 2579227d07a0SVladimir Oltean } 2580227d07a0SVladimir Oltean 2581227d07a0SVladimir Oltean /* Transfer skb to the host port. */ 258268bb8ea8SVivien Didelot dsa_enqueue_skb(skb, dsa_to_port(ds, port)->slave); 2583227d07a0SVladimir Oltean 2584227d07a0SVladimir Oltean /* Wait until the switch has processed the frame */ 2585227d07a0SVladimir Oltean do { 2586227d07a0SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_MGMT_ROUTE, 2587227d07a0SVladimir Oltean slot, &mgmt_route); 2588227d07a0SVladimir Oltean if (rc < 0) { 2589227d07a0SVladimir Oltean dev_err_ratelimited(priv->ds->dev, 2590227d07a0SVladimir Oltean "failed to poll for mgmt route\n"); 2591227d07a0SVladimir Oltean continue; 2592227d07a0SVladimir Oltean } 2593227d07a0SVladimir Oltean 2594227d07a0SVladimir Oltean /* UM10944: The ENFPORT flag of the respective entry is 2595227d07a0SVladimir Oltean * cleared when a match is found. The host can use this 2596227d07a0SVladimir Oltean * flag as an acknowledgment. 2597227d07a0SVladimir Oltean */ 2598227d07a0SVladimir Oltean cpu_relax(); 2599227d07a0SVladimir Oltean } while (mgmt_route.enfport && --timeout); 2600227d07a0SVladimir Oltean 2601227d07a0SVladimir Oltean if (!timeout) { 2602227d07a0SVladimir Oltean /* Clean up the management route so that a follow-up 2603227d07a0SVladimir Oltean * frame may not match on it by mistake. 26042a7e7409SVladimir Oltean * This is only hardware supported on P/Q/R/S - on E/T it is 26052a7e7409SVladimir Oltean * a no-op and we are silently discarding the -EOPNOTSUPP. 2606227d07a0SVladimir Oltean */ 2607227d07a0SVladimir Oltean sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE, 2608227d07a0SVladimir Oltean slot, &mgmt_route, false); 2609227d07a0SVladimir Oltean dev_err_ratelimited(priv->ds->dev, "xmit timed out\n"); 2610227d07a0SVladimir Oltean } 2611227d07a0SVladimir Oltean 2612227d07a0SVladimir Oltean return NETDEV_TX_OK; 2613227d07a0SVladimir Oltean } 2614227d07a0SVladimir Oltean 2615a68578c2SVladimir Oltean #define work_to_port(work) \ 2616a68578c2SVladimir Oltean container_of((work), struct sja1105_port, xmit_work) 2617a68578c2SVladimir Oltean #define tagger_to_sja1105(t) \ 2618a68578c2SVladimir Oltean container_of((t), struct sja1105_private, tagger_data) 2619a68578c2SVladimir Oltean 2620227d07a0SVladimir Oltean /* Deferred work is unfortunately necessary because setting up the management 2621227d07a0SVladimir Oltean * route cannot be done from atomit context (SPI transfer takes a sleepable 2622227d07a0SVladimir Oltean * lock on the bus) 2623227d07a0SVladimir Oltean */ 2624a68578c2SVladimir Oltean static void sja1105_port_deferred_xmit(struct kthread_work *work) 2625227d07a0SVladimir Oltean { 2626a68578c2SVladimir Oltean struct sja1105_port *sp = work_to_port(work); 2627a68578c2SVladimir Oltean struct sja1105_tagger_data *tagger_data = sp->data; 2628a68578c2SVladimir Oltean struct sja1105_private *priv = tagger_to_sja1105(tagger_data); 2629a68578c2SVladimir Oltean int port = sp - priv->ports; 2630a68578c2SVladimir Oltean struct sk_buff *skb; 2631a68578c2SVladimir Oltean 2632a68578c2SVladimir Oltean while ((skb = skb_dequeue(&sp->xmit_queue)) != NULL) { 2633c4b364ceSYangbo Lu struct sk_buff *clone = SJA1105_SKB_CB(skb)->clone; 2634227d07a0SVladimir Oltean 2635227d07a0SVladimir Oltean mutex_lock(&priv->mgmt_lock); 2636227d07a0SVladimir Oltean 2637a68578c2SVladimir Oltean sja1105_mgmt_xmit(priv->ds, port, 0, skb, !!clone); 2638a68578c2SVladimir Oltean 263947ed985eSVladimir Oltean /* The clone, if there, was made by dsa_skb_tx_timestamp */ 2640a68578c2SVladimir Oltean if (clone) 2641a68578c2SVladimir Oltean sja1105_ptp_txtstamp_skb(priv->ds, port, clone); 2642227d07a0SVladimir Oltean 2643227d07a0SVladimir Oltean mutex_unlock(&priv->mgmt_lock); 2644a68578c2SVladimir Oltean } 26458aa9ebccSVladimir Oltean } 26468aa9ebccSVladimir Oltean 26478456721dSVladimir Oltean /* The MAXAGE setting belongs to the L2 Forwarding Parameters table, 26488456721dSVladimir Oltean * which cannot be reconfigured at runtime. So a switch reset is required. 26498456721dSVladimir Oltean */ 26508456721dSVladimir Oltean static int sja1105_set_ageing_time(struct dsa_switch *ds, 26518456721dSVladimir Oltean unsigned int ageing_time) 26528456721dSVladimir Oltean { 26538456721dSVladimir Oltean struct sja1105_l2_lookup_params_entry *l2_lookup_params; 26548456721dSVladimir Oltean struct sja1105_private *priv = ds->priv; 26558456721dSVladimir Oltean struct sja1105_table *table; 26568456721dSVladimir Oltean unsigned int maxage; 26578456721dSVladimir Oltean 26588456721dSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS]; 26598456721dSVladimir Oltean l2_lookup_params = table->entries; 26608456721dSVladimir Oltean 26618456721dSVladimir Oltean maxage = SJA1105_AGEING_TIME_MS(ageing_time); 26628456721dSVladimir Oltean 26638456721dSVladimir Oltean if (l2_lookup_params->maxage == maxage) 26648456721dSVladimir Oltean return 0; 26658456721dSVladimir Oltean 26668456721dSVladimir Oltean l2_lookup_params->maxage = maxage; 26678456721dSVladimir Oltean 26682eea1fa8SVladimir Oltean return sja1105_static_config_reload(priv, SJA1105_AGEING_TIME); 26698456721dSVladimir Oltean } 26708456721dSVladimir Oltean 2671c279c726SVladimir Oltean static int sja1105_change_mtu(struct dsa_switch *ds, int port, int new_mtu) 2672c279c726SVladimir Oltean { 2673c279c726SVladimir Oltean struct sja1105_l2_policing_entry *policing; 2674c279c726SVladimir Oltean struct sja1105_private *priv = ds->priv; 2675c279c726SVladimir Oltean 2676c279c726SVladimir Oltean new_mtu += VLAN_ETH_HLEN + ETH_FCS_LEN; 2677c279c726SVladimir Oltean 2678777e55e3SVladimir Oltean if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) 2679c279c726SVladimir Oltean new_mtu += VLAN_HLEN; 2680c279c726SVladimir Oltean 2681c279c726SVladimir Oltean policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries; 2682c279c726SVladimir Oltean 2683a7cc081cSVladimir Oltean if (policing[port].maxlen == new_mtu) 2684c279c726SVladimir Oltean return 0; 2685c279c726SVladimir Oltean 2686a7cc081cSVladimir Oltean policing[port].maxlen = new_mtu; 2687c279c726SVladimir Oltean 2688c279c726SVladimir Oltean return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING); 2689c279c726SVladimir Oltean } 2690c279c726SVladimir Oltean 2691c279c726SVladimir Oltean static int sja1105_get_max_mtu(struct dsa_switch *ds, int port) 2692c279c726SVladimir Oltean { 2693c279c726SVladimir Oltean return 2043 - VLAN_ETH_HLEN - ETH_FCS_LEN; 2694c279c726SVladimir Oltean } 2695c279c726SVladimir Oltean 2696317ab5b8SVladimir Oltean static int sja1105_port_setup_tc(struct dsa_switch *ds, int port, 2697317ab5b8SVladimir Oltean enum tc_setup_type type, 2698317ab5b8SVladimir Oltean void *type_data) 2699317ab5b8SVladimir Oltean { 2700317ab5b8SVladimir Oltean switch (type) { 2701317ab5b8SVladimir Oltean case TC_SETUP_QDISC_TAPRIO: 2702317ab5b8SVladimir Oltean return sja1105_setup_tc_taprio(ds, port, type_data); 27034d752508SVladimir Oltean case TC_SETUP_QDISC_CBS: 27044d752508SVladimir Oltean return sja1105_setup_tc_cbs(ds, port, type_data); 2705317ab5b8SVladimir Oltean default: 2706317ab5b8SVladimir Oltean return -EOPNOTSUPP; 2707317ab5b8SVladimir Oltean } 2708317ab5b8SVladimir Oltean } 2709317ab5b8SVladimir Oltean 2710511e6ca0SVladimir Oltean /* We have a single mirror (@to) port, but can configure ingress and egress 2711511e6ca0SVladimir Oltean * mirroring on all other (@from) ports. 2712511e6ca0SVladimir Oltean * We need to allow mirroring rules only as long as the @to port is always the 2713511e6ca0SVladimir Oltean * same, and we need to unset the @to port from mirr_port only when there is no 2714511e6ca0SVladimir Oltean * mirroring rule that references it. 2715511e6ca0SVladimir Oltean */ 2716511e6ca0SVladimir Oltean static int sja1105_mirror_apply(struct sja1105_private *priv, int from, int to, 2717511e6ca0SVladimir Oltean bool ingress, bool enabled) 2718511e6ca0SVladimir Oltean { 2719511e6ca0SVladimir Oltean struct sja1105_general_params_entry *general_params; 2720511e6ca0SVladimir Oltean struct sja1105_mac_config_entry *mac; 2721542043e9SVladimir Oltean struct dsa_switch *ds = priv->ds; 2722511e6ca0SVladimir Oltean struct sja1105_table *table; 2723511e6ca0SVladimir Oltean bool already_enabled; 2724511e6ca0SVladimir Oltean u64 new_mirr_port; 2725511e6ca0SVladimir Oltean int rc; 2726511e6ca0SVladimir Oltean 2727511e6ca0SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; 2728511e6ca0SVladimir Oltean general_params = table->entries; 2729511e6ca0SVladimir Oltean 2730511e6ca0SVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 2731511e6ca0SVladimir Oltean 2732542043e9SVladimir Oltean already_enabled = (general_params->mirr_port != ds->num_ports); 2733511e6ca0SVladimir Oltean if (already_enabled && enabled && general_params->mirr_port != to) { 2734511e6ca0SVladimir Oltean dev_err(priv->ds->dev, 2735511e6ca0SVladimir Oltean "Delete mirroring rules towards port %llu first\n", 2736511e6ca0SVladimir Oltean general_params->mirr_port); 2737511e6ca0SVladimir Oltean return -EBUSY; 2738511e6ca0SVladimir Oltean } 2739511e6ca0SVladimir Oltean 2740511e6ca0SVladimir Oltean new_mirr_port = to; 2741511e6ca0SVladimir Oltean if (!enabled) { 2742511e6ca0SVladimir Oltean bool keep = false; 2743511e6ca0SVladimir Oltean int port; 2744511e6ca0SVladimir Oltean 2745511e6ca0SVladimir Oltean /* Anybody still referencing mirr_port? */ 2746542043e9SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 2747511e6ca0SVladimir Oltean if (mac[port].ing_mirr || mac[port].egr_mirr) { 2748511e6ca0SVladimir Oltean keep = true; 2749511e6ca0SVladimir Oltean break; 2750511e6ca0SVladimir Oltean } 2751511e6ca0SVladimir Oltean } 2752511e6ca0SVladimir Oltean /* Unset already_enabled for next time */ 2753511e6ca0SVladimir Oltean if (!keep) 2754542043e9SVladimir Oltean new_mirr_port = ds->num_ports; 2755511e6ca0SVladimir Oltean } 2756511e6ca0SVladimir Oltean if (new_mirr_port != general_params->mirr_port) { 2757511e6ca0SVladimir Oltean general_params->mirr_port = new_mirr_port; 2758511e6ca0SVladimir Oltean 2759511e6ca0SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_GENERAL_PARAMS, 2760511e6ca0SVladimir Oltean 0, general_params, true); 2761511e6ca0SVladimir Oltean if (rc < 0) 2762511e6ca0SVladimir Oltean return rc; 2763511e6ca0SVladimir Oltean } 2764511e6ca0SVladimir Oltean 2765511e6ca0SVladimir Oltean if (ingress) 2766511e6ca0SVladimir Oltean mac[from].ing_mirr = enabled; 2767511e6ca0SVladimir Oltean else 2768511e6ca0SVladimir Oltean mac[from].egr_mirr = enabled; 2769511e6ca0SVladimir Oltean 2770511e6ca0SVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, from, 2771511e6ca0SVladimir Oltean &mac[from], true); 2772511e6ca0SVladimir Oltean } 2773511e6ca0SVladimir Oltean 2774511e6ca0SVladimir Oltean static int sja1105_mirror_add(struct dsa_switch *ds, int port, 2775511e6ca0SVladimir Oltean struct dsa_mall_mirror_tc_entry *mirror, 2776511e6ca0SVladimir Oltean bool ingress) 2777511e6ca0SVladimir Oltean { 2778511e6ca0SVladimir Oltean return sja1105_mirror_apply(ds->priv, port, mirror->to_local_port, 2779511e6ca0SVladimir Oltean ingress, true); 2780511e6ca0SVladimir Oltean } 2781511e6ca0SVladimir Oltean 2782511e6ca0SVladimir Oltean static void sja1105_mirror_del(struct dsa_switch *ds, int port, 2783511e6ca0SVladimir Oltean struct dsa_mall_mirror_tc_entry *mirror) 2784511e6ca0SVladimir Oltean { 2785511e6ca0SVladimir Oltean sja1105_mirror_apply(ds->priv, port, mirror->to_local_port, 2786511e6ca0SVladimir Oltean mirror->ingress, false); 2787511e6ca0SVladimir Oltean } 2788511e6ca0SVladimir Oltean 2789a7cc081cSVladimir Oltean static int sja1105_port_policer_add(struct dsa_switch *ds, int port, 2790a7cc081cSVladimir Oltean struct dsa_mall_policer_tc_entry *policer) 2791a7cc081cSVladimir Oltean { 2792a7cc081cSVladimir Oltean struct sja1105_l2_policing_entry *policing; 2793a7cc081cSVladimir Oltean struct sja1105_private *priv = ds->priv; 2794a7cc081cSVladimir Oltean 2795a7cc081cSVladimir Oltean policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries; 2796a7cc081cSVladimir Oltean 2797a7cc081cSVladimir Oltean /* In hardware, every 8 microseconds the credit level is incremented by 2798a7cc081cSVladimir Oltean * the value of RATE bytes divided by 64, up to a maximum of SMAX 2799a7cc081cSVladimir Oltean * bytes. 2800a7cc081cSVladimir Oltean */ 2801a7cc081cSVladimir Oltean policing[port].rate = div_u64(512 * policer->rate_bytes_per_sec, 2802a7cc081cSVladimir Oltean 1000000); 28035f035af7SPo Liu policing[port].smax = policer->burst; 2804a7cc081cSVladimir Oltean 2805a7cc081cSVladimir Oltean return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING); 2806a7cc081cSVladimir Oltean } 2807a7cc081cSVladimir Oltean 2808a7cc081cSVladimir Oltean static void sja1105_port_policer_del(struct dsa_switch *ds, int port) 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 policing[port].rate = SJA1105_RATE_MBPS(1000); 2816a7cc081cSVladimir Oltean policing[port].smax = 65535; 2817a7cc081cSVladimir Oltean 2818a7cc081cSVladimir Oltean sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING); 2819a7cc081cSVladimir Oltean } 2820a7cc081cSVladimir Oltean 28214d942354SVladimir Oltean static int sja1105_port_set_learning(struct sja1105_private *priv, int port, 28224d942354SVladimir Oltean bool enabled) 28234d942354SVladimir Oltean { 28244d942354SVladimir Oltean struct sja1105_mac_config_entry *mac; 28254d942354SVladimir Oltean 28264d942354SVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 28274d942354SVladimir Oltean 28284c44fc5eSVladimir Oltean mac[port].dyn_learn = enabled; 28294d942354SVladimir Oltean 28305313a37bSVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, 28314d942354SVladimir Oltean &mac[port], true); 28324d942354SVladimir Oltean } 28334d942354SVladimir Oltean 28344d942354SVladimir Oltean static int sja1105_port_ucast_bcast_flood(struct sja1105_private *priv, int to, 28354d942354SVladimir Oltean struct switchdev_brport_flags flags) 28364d942354SVladimir Oltean { 28374d942354SVladimir Oltean if (flags.mask & BR_FLOOD) { 28384d942354SVladimir Oltean if (flags.val & BR_FLOOD) 28397f7ccdeaSVladimir Oltean priv->ucast_egress_floods |= BIT(to); 28404d942354SVladimir Oltean else 28416a5166e0SVladimir Oltean priv->ucast_egress_floods &= ~BIT(to); 28424d942354SVladimir Oltean } 28437f7ccdeaSVladimir Oltean 28444d942354SVladimir Oltean if (flags.mask & BR_BCAST_FLOOD) { 28454d942354SVladimir Oltean if (flags.val & BR_BCAST_FLOOD) 28467f7ccdeaSVladimir Oltean priv->bcast_egress_floods |= BIT(to); 28474d942354SVladimir Oltean else 28486a5166e0SVladimir Oltean priv->bcast_egress_floods &= ~BIT(to); 28494d942354SVladimir Oltean } 28504d942354SVladimir Oltean 28517f7ccdeaSVladimir Oltean return sja1105_manage_flood_domains(priv); 28524d942354SVladimir Oltean } 28534d942354SVladimir Oltean 28544d942354SVladimir Oltean static int sja1105_port_mcast_flood(struct sja1105_private *priv, int to, 28554d942354SVladimir Oltean struct switchdev_brport_flags flags, 28564d942354SVladimir Oltean struct netlink_ext_ack *extack) 28574d942354SVladimir Oltean { 28584d942354SVladimir Oltean struct sja1105_l2_lookup_entry *l2_lookup; 28594d942354SVladimir Oltean struct sja1105_table *table; 28604d942354SVladimir Oltean int match; 28614d942354SVladimir Oltean 28624d942354SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; 28634d942354SVladimir Oltean l2_lookup = table->entries; 28644d942354SVladimir Oltean 28654d942354SVladimir Oltean for (match = 0; match < table->entry_count; match++) 28664d942354SVladimir Oltean if (l2_lookup[match].macaddr == SJA1105_UNKNOWN_MULTICAST && 28674d942354SVladimir Oltean l2_lookup[match].mask_macaddr == SJA1105_UNKNOWN_MULTICAST) 28684d942354SVladimir Oltean break; 28694d942354SVladimir Oltean 28704d942354SVladimir Oltean if (match == table->entry_count) { 28714d942354SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 28724d942354SVladimir Oltean "Could not find FDB entry for unknown multicast"); 28734d942354SVladimir Oltean return -ENOSPC; 28744d942354SVladimir Oltean } 28754d942354SVladimir Oltean 28764d942354SVladimir Oltean if (flags.val & BR_MCAST_FLOOD) 28774d942354SVladimir Oltean l2_lookup[match].destports |= BIT(to); 28784d942354SVladimir Oltean else 28794d942354SVladimir Oltean l2_lookup[match].destports &= ~BIT(to); 28804d942354SVladimir Oltean 28814d942354SVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 28824d942354SVladimir Oltean l2_lookup[match].index, 28834d942354SVladimir Oltean &l2_lookup[match], 28844d942354SVladimir Oltean true); 28854d942354SVladimir Oltean } 28864d942354SVladimir Oltean 28874d942354SVladimir Oltean static int sja1105_port_pre_bridge_flags(struct dsa_switch *ds, int port, 28884d942354SVladimir Oltean struct switchdev_brport_flags flags, 28894d942354SVladimir Oltean struct netlink_ext_ack *extack) 28904d942354SVladimir Oltean { 28914d942354SVladimir Oltean struct sja1105_private *priv = ds->priv; 28924d942354SVladimir Oltean 28934d942354SVladimir Oltean if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | 28944d942354SVladimir Oltean BR_BCAST_FLOOD)) 28954d942354SVladimir Oltean return -EINVAL; 28964d942354SVladimir Oltean 28974d942354SVladimir Oltean if (flags.mask & (BR_FLOOD | BR_MCAST_FLOOD) && 28984d942354SVladimir Oltean !priv->info->can_limit_mcast_flood) { 28994d942354SVladimir Oltean bool multicast = !!(flags.val & BR_MCAST_FLOOD); 29004d942354SVladimir Oltean bool unicast = !!(flags.val & BR_FLOOD); 29014d942354SVladimir Oltean 29024d942354SVladimir Oltean if (unicast != multicast) { 29034d942354SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 29044d942354SVladimir Oltean "This chip cannot configure multicast flooding independently of unicast"); 29054d942354SVladimir Oltean return -EINVAL; 29064d942354SVladimir Oltean } 29074d942354SVladimir Oltean } 29084d942354SVladimir Oltean 29094d942354SVladimir Oltean return 0; 29104d942354SVladimir Oltean } 29114d942354SVladimir Oltean 29124d942354SVladimir Oltean static int sja1105_port_bridge_flags(struct dsa_switch *ds, int port, 29134d942354SVladimir Oltean struct switchdev_brport_flags flags, 29144d942354SVladimir Oltean struct netlink_ext_ack *extack) 29154d942354SVladimir Oltean { 29164d942354SVladimir Oltean struct sja1105_private *priv = ds->priv; 29174d942354SVladimir Oltean int rc; 29184d942354SVladimir Oltean 29194d942354SVladimir Oltean if (flags.mask & BR_LEARNING) { 29204d942354SVladimir Oltean bool learn_ena = !!(flags.val & BR_LEARNING); 29214d942354SVladimir Oltean 29224d942354SVladimir Oltean rc = sja1105_port_set_learning(priv, port, learn_ena); 29234d942354SVladimir Oltean if (rc) 29244d942354SVladimir Oltean return rc; 29254d942354SVladimir Oltean } 29264d942354SVladimir Oltean 29274d942354SVladimir Oltean if (flags.mask & (BR_FLOOD | BR_BCAST_FLOOD)) { 29284d942354SVladimir Oltean rc = sja1105_port_ucast_bcast_flood(priv, port, flags); 29294d942354SVladimir Oltean if (rc) 29304d942354SVladimir Oltean return rc; 29314d942354SVladimir Oltean } 29324d942354SVladimir Oltean 29334d942354SVladimir Oltean /* For chips that can't offload BR_MCAST_FLOOD independently, there 29344d942354SVladimir Oltean * is nothing to do here, we ensured the configuration is in sync by 29354d942354SVladimir Oltean * offloading BR_FLOOD. 29364d942354SVladimir Oltean */ 29374d942354SVladimir Oltean if (flags.mask & BR_MCAST_FLOOD && priv->info->can_limit_mcast_flood) { 29384d942354SVladimir Oltean rc = sja1105_port_mcast_flood(priv, port, flags, 29394d942354SVladimir Oltean extack); 29404d942354SVladimir Oltean if (rc) 29414d942354SVladimir Oltean return rc; 29424d942354SVladimir Oltean } 29434d942354SVladimir Oltean 29444d942354SVladimir Oltean return 0; 29454d942354SVladimir Oltean } 29464d942354SVladimir Oltean 2947022522acSVladimir Oltean static void sja1105_teardown_ports(struct sja1105_private *priv) 2948022522acSVladimir Oltean { 2949022522acSVladimir Oltean struct dsa_switch *ds = priv->ds; 2950022522acSVladimir Oltean int port; 2951022522acSVladimir Oltean 2952022522acSVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 2953022522acSVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 2954022522acSVladimir Oltean 2955022522acSVladimir Oltean if (sp->xmit_worker) 2956022522acSVladimir Oltean kthread_destroy_worker(sp->xmit_worker); 2957022522acSVladimir Oltean } 2958022522acSVladimir Oltean } 2959022522acSVladimir Oltean 2960022522acSVladimir Oltean static int sja1105_setup_ports(struct sja1105_private *priv) 2961022522acSVladimir Oltean { 2962022522acSVladimir Oltean struct sja1105_tagger_data *tagger_data = &priv->tagger_data; 2963022522acSVladimir Oltean struct dsa_switch *ds = priv->ds; 2964022522acSVladimir Oltean int port, rc; 2965022522acSVladimir Oltean 2966022522acSVladimir Oltean /* Connections between dsa_port and sja1105_port */ 2967022522acSVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 2968022522acSVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 2969022522acSVladimir Oltean struct dsa_port *dp = dsa_to_port(ds, port); 2970022522acSVladimir Oltean struct kthread_worker *worker; 2971022522acSVladimir Oltean struct net_device *slave; 2972022522acSVladimir Oltean 2973022522acSVladimir Oltean if (!dsa_port_is_user(dp)) 2974022522acSVladimir Oltean continue; 2975022522acSVladimir Oltean 2976022522acSVladimir Oltean dp->priv = sp; 2977022522acSVladimir Oltean sp->dp = dp; 2978022522acSVladimir Oltean sp->data = tagger_data; 2979022522acSVladimir Oltean slave = dp->slave; 2980022522acSVladimir Oltean kthread_init_work(&sp->xmit_work, sja1105_port_deferred_xmit); 2981022522acSVladimir Oltean worker = kthread_create_worker(0, "%s_xmit", slave->name); 2982022522acSVladimir Oltean if (IS_ERR(worker)) { 2983022522acSVladimir Oltean rc = PTR_ERR(worker); 2984022522acSVladimir Oltean dev_err(ds->dev, 2985022522acSVladimir Oltean "failed to create deferred xmit thread: %d\n", 2986022522acSVladimir Oltean rc); 2987022522acSVladimir Oltean goto out_destroy_workers; 2988022522acSVladimir Oltean } 2989022522acSVladimir Oltean sp->xmit_worker = worker; 2990022522acSVladimir Oltean skb_queue_head_init(&sp->xmit_queue); 2991022522acSVladimir Oltean sp->xmit_tpid = ETH_P_SJA1105; 2992022522acSVladimir Oltean } 2993022522acSVladimir Oltean 2994022522acSVladimir Oltean return 0; 2995022522acSVladimir Oltean 2996022522acSVladimir Oltean out_destroy_workers: 2997022522acSVladimir Oltean sja1105_teardown_ports(priv); 2998022522acSVladimir Oltean return rc; 2999022522acSVladimir Oltean } 3000022522acSVladimir Oltean 3001022522acSVladimir Oltean /* The programming model for the SJA1105 switch is "all-at-once" via static 3002022522acSVladimir Oltean * configuration tables. Some of these can be dynamically modified at runtime, 3003022522acSVladimir Oltean * but not the xMII mode parameters table. 3004022522acSVladimir Oltean * Furthermode, some PHYs may not have crystals for generating their clocks 3005022522acSVladimir Oltean * (e.g. RMII). Instead, their 50MHz clock is supplied via the SJA1105 port's 3006022522acSVladimir Oltean * ref_clk pin. So port clocking needs to be initialized early, before 3007022522acSVladimir Oltean * connecting to PHYs is attempted, otherwise they won't respond through MDIO. 3008022522acSVladimir Oltean * Setting correct PHY link speed does not matter now. 3009022522acSVladimir Oltean * But dsa_slave_phy_setup is called later than sja1105_setup, so the PHY 3010022522acSVladimir Oltean * bindings are not yet parsed by DSA core. We need to parse early so that we 3011022522acSVladimir Oltean * can populate the xMII mode parameters table. 3012022522acSVladimir Oltean */ 3013022522acSVladimir Oltean static int sja1105_setup(struct dsa_switch *ds) 3014022522acSVladimir Oltean { 3015022522acSVladimir Oltean struct sja1105_private *priv = ds->priv; 3016022522acSVladimir Oltean int rc; 3017022522acSVladimir Oltean 3018022522acSVladimir Oltean if (priv->info->disable_microcontroller) { 3019022522acSVladimir Oltean rc = priv->info->disable_microcontroller(priv); 3020022522acSVladimir Oltean if (rc < 0) { 3021022522acSVladimir Oltean dev_err(ds->dev, 3022022522acSVladimir Oltean "Failed to disable microcontroller: %pe\n", 3023022522acSVladimir Oltean ERR_PTR(rc)); 3024022522acSVladimir Oltean return rc; 3025022522acSVladimir Oltean } 3026022522acSVladimir Oltean } 3027022522acSVladimir Oltean 3028022522acSVladimir Oltean /* Create and send configuration down to device */ 3029022522acSVladimir Oltean rc = sja1105_static_config_load(priv); 3030022522acSVladimir Oltean if (rc < 0) { 3031022522acSVladimir Oltean dev_err(ds->dev, "Failed to load static config: %d\n", rc); 3032022522acSVladimir Oltean return rc; 3033022522acSVladimir Oltean } 3034022522acSVladimir Oltean 3035022522acSVladimir Oltean /* Configure the CGU (PHY link modes and speeds) */ 3036022522acSVladimir Oltean if (priv->info->clocking_setup) { 3037022522acSVladimir Oltean rc = priv->info->clocking_setup(priv); 3038022522acSVladimir Oltean if (rc < 0) { 3039022522acSVladimir Oltean dev_err(ds->dev, 3040022522acSVladimir Oltean "Failed to configure MII clocking: %pe\n", 3041022522acSVladimir Oltean ERR_PTR(rc)); 3042022522acSVladimir Oltean goto out_static_config_free; 3043022522acSVladimir Oltean } 3044022522acSVladimir Oltean } 3045022522acSVladimir Oltean 3046022522acSVladimir Oltean rc = sja1105_setup_ports(priv); 3047022522acSVladimir Oltean if (rc) 3048022522acSVladimir Oltean goto out_static_config_free; 3049022522acSVladimir Oltean 3050022522acSVladimir Oltean sja1105_tas_setup(ds); 3051022522acSVladimir Oltean sja1105_flower_setup(ds); 3052022522acSVladimir Oltean 3053022522acSVladimir Oltean rc = sja1105_ptp_clock_register(ds); 3054022522acSVladimir Oltean if (rc < 0) { 3055022522acSVladimir Oltean dev_err(ds->dev, "Failed to register PTP clock: %d\n", rc); 3056022522acSVladimir Oltean goto out_flower_teardown; 3057022522acSVladimir Oltean } 3058022522acSVladimir Oltean 3059022522acSVladimir Oltean rc = sja1105_mdiobus_register(ds); 3060022522acSVladimir Oltean if (rc < 0) { 3061022522acSVladimir Oltean dev_err(ds->dev, "Failed to register MDIO bus: %pe\n", 3062022522acSVladimir Oltean ERR_PTR(rc)); 3063022522acSVladimir Oltean goto out_ptp_clock_unregister; 3064022522acSVladimir Oltean } 3065022522acSVladimir Oltean 3066022522acSVladimir Oltean rc = sja1105_devlink_setup(ds); 3067022522acSVladimir Oltean if (rc < 0) 3068022522acSVladimir Oltean goto out_mdiobus_unregister; 3069022522acSVladimir Oltean 3070022522acSVladimir Oltean rtnl_lock(); 3071022522acSVladimir Oltean rc = dsa_tag_8021q_register(ds, htons(ETH_P_8021Q)); 3072022522acSVladimir Oltean rtnl_unlock(); 3073022522acSVladimir Oltean if (rc) 3074022522acSVladimir Oltean goto out_devlink_teardown; 3075022522acSVladimir Oltean 3076022522acSVladimir Oltean /* On SJA1105, VLAN filtering per se is always enabled in hardware. 3077022522acSVladimir Oltean * The only thing we can do to disable it is lie about what the 802.1Q 3078022522acSVladimir Oltean * EtherType is. 3079022522acSVladimir Oltean * So it will still try to apply VLAN filtering, but all ingress 3080022522acSVladimir Oltean * traffic (except frames received with EtherType of ETH_P_SJA1105) 3081022522acSVladimir Oltean * will be internally tagged with a distorted VLAN header where the 3082022522acSVladimir Oltean * TPID is ETH_P_SJA1105, and the VLAN ID is the port pvid. 3083022522acSVladimir Oltean */ 3084022522acSVladimir Oltean ds->vlan_filtering_is_global = true; 3085022522acSVladimir Oltean ds->untag_bridge_pvid = true; 3086022522acSVladimir Oltean /* tag_8021q has 3 bits for the VBID, and the value 0 is reserved */ 3087022522acSVladimir Oltean ds->num_fwd_offloading_bridges = 7; 3088022522acSVladimir Oltean 3089022522acSVladimir Oltean /* Advertise the 8 egress queues */ 3090022522acSVladimir Oltean ds->num_tx_queues = SJA1105_NUM_TC; 3091022522acSVladimir Oltean 3092022522acSVladimir Oltean ds->mtu_enforcement_ingress = true; 3093022522acSVladimir Oltean ds->assisted_learning_on_cpu_port = true; 3094022522acSVladimir Oltean 3095022522acSVladimir Oltean return 0; 3096022522acSVladimir Oltean 3097022522acSVladimir Oltean out_devlink_teardown: 3098022522acSVladimir Oltean sja1105_devlink_teardown(ds); 3099022522acSVladimir Oltean out_mdiobus_unregister: 3100022522acSVladimir Oltean sja1105_mdiobus_unregister(ds); 3101022522acSVladimir Oltean out_ptp_clock_unregister: 3102022522acSVladimir Oltean sja1105_ptp_clock_unregister(ds); 3103022522acSVladimir Oltean out_flower_teardown: 3104022522acSVladimir Oltean sja1105_flower_teardown(ds); 3105022522acSVladimir Oltean sja1105_tas_teardown(ds); 3106022522acSVladimir Oltean sja1105_teardown_ports(priv); 3107022522acSVladimir Oltean out_static_config_free: 3108022522acSVladimir Oltean sja1105_static_config_free(&priv->static_config); 3109022522acSVladimir Oltean 3110022522acSVladimir Oltean return rc; 3111022522acSVladimir Oltean } 3112022522acSVladimir Oltean 3113022522acSVladimir Oltean static void sja1105_teardown(struct dsa_switch *ds) 3114022522acSVladimir Oltean { 3115022522acSVladimir Oltean struct sja1105_private *priv = ds->priv; 3116022522acSVladimir Oltean 3117022522acSVladimir Oltean rtnl_lock(); 3118022522acSVladimir Oltean dsa_tag_8021q_unregister(ds); 3119022522acSVladimir Oltean rtnl_unlock(); 3120022522acSVladimir Oltean 3121022522acSVladimir Oltean sja1105_devlink_teardown(ds); 3122022522acSVladimir Oltean sja1105_mdiobus_unregister(ds); 3123022522acSVladimir Oltean sja1105_ptp_clock_unregister(ds); 3124022522acSVladimir Oltean sja1105_flower_teardown(ds); 3125022522acSVladimir Oltean sja1105_tas_teardown(ds); 3126022522acSVladimir Oltean sja1105_teardown_ports(priv); 3127022522acSVladimir Oltean sja1105_static_config_free(&priv->static_config); 3128022522acSVladimir Oltean } 3129022522acSVladimir Oltean 3130994d2cbbSVladimir Oltean const struct dsa_switch_ops sja1105_switch_ops = { 31318aa9ebccSVladimir Oltean .get_tag_protocol = sja1105_get_tag_protocol, 31328aa9ebccSVladimir Oltean .setup = sja1105_setup, 3133f3097be2SVladimir Oltean .teardown = sja1105_teardown, 31348456721dSVladimir Oltean .set_ageing_time = sja1105_set_ageing_time, 3135c279c726SVladimir Oltean .port_change_mtu = sja1105_change_mtu, 3136c279c726SVladimir Oltean .port_max_mtu = sja1105_get_max_mtu, 3137ad9f299aSVladimir Oltean .phylink_validate = sja1105_phylink_validate, 3138af7cd036SVladimir Oltean .phylink_mac_config = sja1105_mac_config, 31398400cff6SVladimir Oltean .phylink_mac_link_up = sja1105_mac_link_up, 31408400cff6SVladimir Oltean .phylink_mac_link_down = sja1105_mac_link_down, 314152c34e6eSVladimir Oltean .get_strings = sja1105_get_strings, 314252c34e6eSVladimir Oltean .get_ethtool_stats = sja1105_get_ethtool_stats, 314352c34e6eSVladimir Oltean .get_sset_count = sja1105_get_sset_count, 3144bb77f36aSVladimir Oltean .get_ts_info = sja1105_get_ts_info, 3145a68578c2SVladimir Oltean .port_disable = sja1105_port_disable, 3146291d1e72SVladimir Oltean .port_fdb_dump = sja1105_fdb_dump, 3147291d1e72SVladimir Oltean .port_fdb_add = sja1105_fdb_add, 3148291d1e72SVladimir Oltean .port_fdb_del = sja1105_fdb_del, 31495126ec72SVladimir Oltean .port_fast_age = sja1105_fast_age, 31508aa9ebccSVladimir Oltean .port_bridge_join = sja1105_bridge_join, 31518aa9ebccSVladimir Oltean .port_bridge_leave = sja1105_bridge_leave, 31524d942354SVladimir Oltean .port_pre_bridge_flags = sja1105_port_pre_bridge_flags, 31534d942354SVladimir Oltean .port_bridge_flags = sja1105_port_bridge_flags, 3154640f763fSVladimir Oltean .port_stp_state_set = sja1105_bridge_stp_state_set, 31556666cebcSVladimir Oltean .port_vlan_filtering = sja1105_vlan_filtering, 31566dfd23d3SVladimir Oltean .port_vlan_add = sja1105_bridge_vlan_add, 31576dfd23d3SVladimir Oltean .port_vlan_del = sja1105_bridge_vlan_del, 3158291d1e72SVladimir Oltean .port_mdb_add = sja1105_mdb_add, 3159291d1e72SVladimir Oltean .port_mdb_del = sja1105_mdb_del, 3160a602afd2SVladimir Oltean .port_hwtstamp_get = sja1105_hwtstamp_get, 3161a602afd2SVladimir Oltean .port_hwtstamp_set = sja1105_hwtstamp_set, 3162f3097be2SVladimir Oltean .port_rxtstamp = sja1105_port_rxtstamp, 316347ed985eSVladimir Oltean .port_txtstamp = sja1105_port_txtstamp, 3164317ab5b8SVladimir Oltean .port_setup_tc = sja1105_port_setup_tc, 3165511e6ca0SVladimir Oltean .port_mirror_add = sja1105_mirror_add, 3166511e6ca0SVladimir Oltean .port_mirror_del = sja1105_mirror_del, 3167a7cc081cSVladimir Oltean .port_policer_add = sja1105_port_policer_add, 3168a7cc081cSVladimir Oltean .port_policer_del = sja1105_port_policer_del, 3169a6af7763SVladimir Oltean .cls_flower_add = sja1105_cls_flower_add, 3170a6af7763SVladimir Oltean .cls_flower_del = sja1105_cls_flower_del, 3171834f8933SVladimir Oltean .cls_flower_stats = sja1105_cls_flower_stats, 3172ff4cf8eaSVladimir Oltean .devlink_info_get = sja1105_devlink_info_get, 31735da11eb4SVladimir Oltean .tag_8021q_vlan_add = sja1105_dsa_8021q_vlan_add, 31745da11eb4SVladimir Oltean .tag_8021q_vlan_del = sja1105_dsa_8021q_vlan_del, 31754fbc08bdSVladimir Oltean .port_prechangeupper = sja1105_prechangeupper, 3176b6ad86e6SVladimir Oltean .port_bridge_tx_fwd_offload = dsa_tag_8021q_bridge_tx_fwd_offload, 3177b6ad86e6SVladimir Oltean .port_bridge_tx_fwd_unoffload = dsa_tag_8021q_bridge_tx_fwd_unoffload, 31788aa9ebccSVladimir Oltean }; 3179994d2cbbSVladimir Oltean EXPORT_SYMBOL_GPL(sja1105_switch_ops); 31808aa9ebccSVladimir Oltean 31810b0e2997SVladimir Oltean static const struct of_device_id sja1105_dt_ids[]; 31820b0e2997SVladimir Oltean 31838aa9ebccSVladimir Oltean static int sja1105_check_device_id(struct sja1105_private *priv) 31848aa9ebccSVladimir Oltean { 31858aa9ebccSVladimir Oltean const struct sja1105_regs *regs = priv->info->regs; 31868aa9ebccSVladimir Oltean u8 prod_id[SJA1105_SIZE_DEVICE_ID] = {0}; 31878aa9ebccSVladimir Oltean struct device *dev = &priv->spidev->dev; 31880b0e2997SVladimir Oltean const struct of_device_id *match; 3189dff79620SVladimir Oltean u32 device_id; 31908aa9ebccSVladimir Oltean u64 part_no; 31918aa9ebccSVladimir Oltean int rc; 31928aa9ebccSVladimir Oltean 319334d76e9fSVladimir Oltean rc = sja1105_xfer_u32(priv, SPI_READ, regs->device_id, &device_id, 319434d76e9fSVladimir Oltean NULL); 31958aa9ebccSVladimir Oltean if (rc < 0) 31968aa9ebccSVladimir Oltean return rc; 31978aa9ebccSVladimir Oltean 31981bd44870SVladimir Oltean rc = sja1105_xfer_buf(priv, SPI_READ, regs->prod_id, prod_id, 31991bd44870SVladimir Oltean SJA1105_SIZE_DEVICE_ID); 32008aa9ebccSVladimir Oltean if (rc < 0) 32018aa9ebccSVladimir Oltean return rc; 32028aa9ebccSVladimir Oltean 32038aa9ebccSVladimir Oltean sja1105_unpack(prod_id, &part_no, 19, 4, SJA1105_SIZE_DEVICE_ID); 32048aa9ebccSVladimir Oltean 32055978fac0SNathan Chancellor for (match = sja1105_dt_ids; match->compatible[0]; match++) { 32060b0e2997SVladimir Oltean const struct sja1105_info *info = match->data; 32070b0e2997SVladimir Oltean 32080b0e2997SVladimir Oltean /* Is what's been probed in our match table at all? */ 32090b0e2997SVladimir Oltean if (info->device_id != device_id || info->part_no != part_no) 32100b0e2997SVladimir Oltean continue; 32110b0e2997SVladimir Oltean 32120b0e2997SVladimir Oltean /* But is it what's in the device tree? */ 32130b0e2997SVladimir Oltean if (priv->info->device_id != device_id || 32140b0e2997SVladimir Oltean priv->info->part_no != part_no) { 32150b0e2997SVladimir Oltean dev_warn(dev, "Device tree specifies chip %s but found %s, please fix it!\n", 32160b0e2997SVladimir Oltean priv->info->name, info->name); 32170b0e2997SVladimir Oltean /* It isn't. No problem, pick that up. */ 32180b0e2997SVladimir Oltean priv->info = info; 32198aa9ebccSVladimir Oltean } 32208aa9ebccSVladimir Oltean 32218aa9ebccSVladimir Oltean return 0; 32228aa9ebccSVladimir Oltean } 32238aa9ebccSVladimir Oltean 32240b0e2997SVladimir Oltean dev_err(dev, "Unexpected {device ID, part number}: 0x%x 0x%llx\n", 32250b0e2997SVladimir Oltean device_id, part_no); 32260b0e2997SVladimir Oltean 32270b0e2997SVladimir Oltean return -ENODEV; 32280b0e2997SVladimir Oltean } 32290b0e2997SVladimir Oltean 32308aa9ebccSVladimir Oltean static int sja1105_probe(struct spi_device *spi) 32318aa9ebccSVladimir Oltean { 32328aa9ebccSVladimir Oltean struct device *dev = &spi->dev; 32338aa9ebccSVladimir Oltean struct sja1105_private *priv; 3234718bad0eSVladimir Oltean size_t max_xfer, max_msg; 32358aa9ebccSVladimir Oltean struct dsa_switch *ds; 3236022522acSVladimir Oltean int rc; 32378aa9ebccSVladimir Oltean 32388aa9ebccSVladimir Oltean if (!dev->of_node) { 32398aa9ebccSVladimir Oltean dev_err(dev, "No DTS bindings for SJA1105 driver\n"); 32408aa9ebccSVladimir Oltean return -EINVAL; 32418aa9ebccSVladimir Oltean } 32428aa9ebccSVladimir Oltean 32438aa9ebccSVladimir Oltean priv = devm_kzalloc(dev, sizeof(struct sja1105_private), GFP_KERNEL); 32448aa9ebccSVladimir Oltean if (!priv) 32458aa9ebccSVladimir Oltean return -ENOMEM; 32468aa9ebccSVladimir Oltean 32478aa9ebccSVladimir Oltean /* Configure the optional reset pin and bring up switch */ 32488aa9ebccSVladimir Oltean priv->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); 32498aa9ebccSVladimir Oltean if (IS_ERR(priv->reset_gpio)) 32508aa9ebccSVladimir Oltean dev_dbg(dev, "reset-gpios not defined, ignoring\n"); 32518aa9ebccSVladimir Oltean else 32528aa9ebccSVladimir Oltean sja1105_hw_reset(priv->reset_gpio, 1, 1); 32538aa9ebccSVladimir Oltean 32548aa9ebccSVladimir Oltean /* Populate our driver private structure (priv) based on 32558aa9ebccSVladimir Oltean * the device tree node that was probed (spi) 32568aa9ebccSVladimir Oltean */ 32578aa9ebccSVladimir Oltean priv->spidev = spi; 32588aa9ebccSVladimir Oltean spi_set_drvdata(spi, priv); 32598aa9ebccSVladimir Oltean 32608aa9ebccSVladimir Oltean /* Configure the SPI bus */ 32618aa9ebccSVladimir Oltean spi->bits_per_word = 8; 32628aa9ebccSVladimir Oltean rc = spi_setup(spi); 32638aa9ebccSVladimir Oltean if (rc < 0) { 32648aa9ebccSVladimir Oltean dev_err(dev, "Could not init SPI\n"); 32658aa9ebccSVladimir Oltean return rc; 32668aa9ebccSVladimir Oltean } 32678aa9ebccSVladimir Oltean 3268718bad0eSVladimir Oltean /* In sja1105_xfer, we send spi_messages composed of two spi_transfers: 3269718bad0eSVladimir Oltean * a small one for the message header and another one for the current 3270718bad0eSVladimir Oltean * chunk of the packed buffer. 3271718bad0eSVladimir Oltean * Check that the restrictions imposed by the SPI controller are 3272718bad0eSVladimir Oltean * respected: the chunk buffer is smaller than the max transfer size, 3273718bad0eSVladimir Oltean * and the total length of the chunk plus its message header is smaller 3274718bad0eSVladimir Oltean * than the max message size. 3275718bad0eSVladimir Oltean * We do that during probe time since the maximum transfer size is a 3276718bad0eSVladimir Oltean * runtime invariant. 3277718bad0eSVladimir Oltean */ 3278718bad0eSVladimir Oltean max_xfer = spi_max_transfer_size(spi); 3279718bad0eSVladimir Oltean max_msg = spi_max_message_size(spi); 3280718bad0eSVladimir Oltean 3281718bad0eSVladimir Oltean /* We need to send at least one 64-bit word of SPI payload per message 3282718bad0eSVladimir Oltean * in order to be able to make useful progress. 3283718bad0eSVladimir Oltean */ 3284718bad0eSVladimir Oltean if (max_msg < SJA1105_SIZE_SPI_MSG_HEADER + 8) { 3285718bad0eSVladimir Oltean dev_err(dev, "SPI master cannot send large enough buffers, aborting\n"); 3286718bad0eSVladimir Oltean return -EINVAL; 3287718bad0eSVladimir Oltean } 3288718bad0eSVladimir Oltean 3289718bad0eSVladimir Oltean priv->max_xfer_len = SJA1105_SIZE_SPI_MSG_MAXLEN; 3290718bad0eSVladimir Oltean if (priv->max_xfer_len > max_xfer) 3291718bad0eSVladimir Oltean priv->max_xfer_len = max_xfer; 3292718bad0eSVladimir Oltean if (priv->max_xfer_len > max_msg - SJA1105_SIZE_SPI_MSG_HEADER) 3293718bad0eSVladimir Oltean priv->max_xfer_len = max_msg - SJA1105_SIZE_SPI_MSG_HEADER; 3294718bad0eSVladimir Oltean 32958aa9ebccSVladimir Oltean priv->info = of_device_get_match_data(dev); 32968aa9ebccSVladimir Oltean 32978aa9ebccSVladimir Oltean /* Detect hardware device */ 32988aa9ebccSVladimir Oltean rc = sja1105_check_device_id(priv); 32998aa9ebccSVladimir Oltean if (rc < 0) { 33008aa9ebccSVladimir Oltean dev_err(dev, "Device ID check failed: %d\n", rc); 33018aa9ebccSVladimir Oltean return rc; 33028aa9ebccSVladimir Oltean } 33038aa9ebccSVladimir Oltean 33048aa9ebccSVladimir Oltean dev_info(dev, "Probed switch chip: %s\n", priv->info->name); 33058aa9ebccSVladimir Oltean 33067e99e347SVivien Didelot ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL); 33078aa9ebccSVladimir Oltean if (!ds) 33088aa9ebccSVladimir Oltean return -ENOMEM; 33098aa9ebccSVladimir Oltean 33107e99e347SVivien Didelot ds->dev = dev; 33113e77e59bSVladimir Oltean ds->num_ports = priv->info->num_ports; 33128aa9ebccSVladimir Oltean ds->ops = &sja1105_switch_ops; 33138aa9ebccSVladimir Oltean ds->priv = priv; 33148aa9ebccSVladimir Oltean priv->ds = ds; 33158aa9ebccSVladimir Oltean 3316d5a619bfSVivien Didelot mutex_init(&priv->ptp_data.lock); 3317d5a619bfSVivien Didelot mutex_init(&priv->mgmt_lock); 3318d5a619bfSVivien Didelot 3319022522acSVladimir Oltean rc = sja1105_parse_dt(priv); 3320022522acSVladimir Oltean if (rc < 0) { 3321022522acSVladimir Oltean dev_err(ds->dev, "Failed to parse DT: %d\n", rc); 3322328621f6SVladimir Oltean return rc; 3323022522acSVladimir Oltean } 3324022522acSVladimir Oltean 3325022522acSVladimir Oltean /* Error out early if internal delays are required through DT 3326022522acSVladimir Oltean * and we can't apply them. 3327022522acSVladimir Oltean */ 3328022522acSVladimir Oltean rc = sja1105_parse_rgmii_delays(priv); 3329022522acSVladimir Oltean if (rc < 0) { 3330022522acSVladimir Oltean dev_err(ds->dev, "RGMII delay not supported\n"); 3331022522acSVladimir Oltean return rc; 3332022522acSVladimir Oltean } 3333d5a619bfSVivien Didelot 33344d752508SVladimir Oltean if (IS_ENABLED(CONFIG_NET_SCH_CBS)) { 33354d752508SVladimir Oltean priv->cbs = devm_kcalloc(dev, priv->info->num_cbs_shapers, 33364d752508SVladimir Oltean sizeof(struct sja1105_cbs_entry), 33374d752508SVladimir Oltean GFP_KERNEL); 3338022522acSVladimir Oltean if (!priv->cbs) 3339022522acSVladimir Oltean return -ENOMEM; 33404d752508SVladimir Oltean } 33414d752508SVladimir Oltean 3342022522acSVladimir Oltean return dsa_register_switch(priv->ds); 33438aa9ebccSVladimir Oltean } 33448aa9ebccSVladimir Oltean 33458aa9ebccSVladimir Oltean static int sja1105_remove(struct spi_device *spi) 33468aa9ebccSVladimir Oltean { 33478aa9ebccSVladimir Oltean struct sja1105_private *priv = spi_get_drvdata(spi); 3348cedf4670SVladimir Oltean struct dsa_switch *ds = priv->ds; 33498aa9ebccSVladimir Oltean 3350cedf4670SVladimir Oltean dsa_unregister_switch(ds); 3351cedf4670SVladimir Oltean 33528aa9ebccSVladimir Oltean return 0; 33538aa9ebccSVladimir Oltean } 33548aa9ebccSVladimir Oltean 33558aa9ebccSVladimir Oltean static const struct of_device_id sja1105_dt_ids[] = { 33568aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105e", .data = &sja1105e_info }, 33578aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105t", .data = &sja1105t_info }, 33588aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105p", .data = &sja1105p_info }, 33598aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105q", .data = &sja1105q_info }, 33608aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105r", .data = &sja1105r_info }, 33618aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105s", .data = &sja1105s_info }, 33623e77e59bSVladimir Oltean { .compatible = "nxp,sja1110a", .data = &sja1110a_info }, 33633e77e59bSVladimir Oltean { .compatible = "nxp,sja1110b", .data = &sja1110b_info }, 33643e77e59bSVladimir Oltean { .compatible = "nxp,sja1110c", .data = &sja1110c_info }, 33653e77e59bSVladimir Oltean { .compatible = "nxp,sja1110d", .data = &sja1110d_info }, 33668aa9ebccSVladimir Oltean { /* sentinel */ }, 33678aa9ebccSVladimir Oltean }; 33688aa9ebccSVladimir Oltean MODULE_DEVICE_TABLE(of, sja1105_dt_ids); 33698aa9ebccSVladimir Oltean 33708aa9ebccSVladimir Oltean static struct spi_driver sja1105_driver = { 33718aa9ebccSVladimir Oltean .driver = { 33728aa9ebccSVladimir Oltean .name = "sja1105", 33738aa9ebccSVladimir Oltean .owner = THIS_MODULE, 33748aa9ebccSVladimir Oltean .of_match_table = of_match_ptr(sja1105_dt_ids), 33758aa9ebccSVladimir Oltean }, 33768aa9ebccSVladimir Oltean .probe = sja1105_probe, 33778aa9ebccSVladimir Oltean .remove = sja1105_remove, 33788aa9ebccSVladimir Oltean }; 33798aa9ebccSVladimir Oltean 33808aa9ebccSVladimir Oltean module_spi_driver(sja1105_driver); 33818aa9ebccSVladimir Oltean 33828aa9ebccSVladimir Oltean MODULE_AUTHOR("Vladimir Oltean <olteanv@gmail.com>"); 33838aa9ebccSVladimir Oltean MODULE_AUTHOR("Georg Waibel <georg.waibel@sensor-technik.de>"); 33848aa9ebccSVladimir Oltean MODULE_DESCRIPTION("SJA1105 Driver"); 33858aa9ebccSVladimir Oltean MODULE_LICENSE("GPL v2"); 3386