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> 198aa9ebccSVladimir Oltean #include <linux/netdev_features.h> 208aa9ebccSVladimir Oltean #include <linux/netdevice.h> 218aa9ebccSVladimir Oltean #include <linux/if_bridge.h> 228aa9ebccSVladimir Oltean #include <linux/if_ether.h> 23227d07a0SVladimir Oltean #include <linux/dsa/8021q.h> 248aa9ebccSVladimir Oltean #include "sja1105.h" 25ffe10e67SVladimir Oltean #include "sja1105_sgmii.h" 26317ab5b8SVladimir Oltean #include "sja1105_tas.h" 278aa9ebccSVladimir Oltean 284d942354SVladimir Oltean #define SJA1105_UNKNOWN_MULTICAST 0x010000000000ull 29*ed040abcSVladimir Oltean #define SJA1105_DEFAULT_VLAN (VLAN_N_VID - 1) 304d942354SVladimir Oltean 31ac02a451SVladimir Oltean static const struct dsa_switch_ops sja1105_switch_ops; 32ac02a451SVladimir Oltean 338aa9ebccSVladimir Oltean static void sja1105_hw_reset(struct gpio_desc *gpio, unsigned int pulse_len, 348aa9ebccSVladimir Oltean unsigned int startup_delay) 358aa9ebccSVladimir Oltean { 368aa9ebccSVladimir Oltean gpiod_set_value_cansleep(gpio, 1); 378aa9ebccSVladimir Oltean /* Wait for minimum reset pulse length */ 388aa9ebccSVladimir Oltean msleep(pulse_len); 398aa9ebccSVladimir Oltean gpiod_set_value_cansleep(gpio, 0); 408aa9ebccSVladimir Oltean /* Wait until chip is ready after reset */ 418aa9ebccSVladimir Oltean msleep(startup_delay); 428aa9ebccSVladimir Oltean } 438aa9ebccSVladimir Oltean 448aa9ebccSVladimir Oltean static void 458aa9ebccSVladimir Oltean sja1105_port_allow_traffic(struct sja1105_l2_forwarding_entry *l2_fwd, 468aa9ebccSVladimir Oltean int from, int to, bool allow) 478aa9ebccSVladimir Oltean { 484d942354SVladimir Oltean if (allow) 498aa9ebccSVladimir Oltean l2_fwd[from].reach_port |= BIT(to); 504d942354SVladimir Oltean else 518aa9ebccSVladimir Oltean l2_fwd[from].reach_port &= ~BIT(to); 528aa9ebccSVladimir Oltean } 538aa9ebccSVladimir Oltean 547f7ccdeaSVladimir Oltean static bool sja1105_can_forward(struct sja1105_l2_forwarding_entry *l2_fwd, 557f7ccdeaSVladimir Oltean int from, int to) 567f7ccdeaSVladimir Oltean { 577f7ccdeaSVladimir Oltean return !!(l2_fwd[from].reach_port & BIT(to)); 587f7ccdeaSVladimir Oltean } 597f7ccdeaSVladimir Oltean 608aa9ebccSVladimir Oltean /* Structure used to temporarily transport device tree 618aa9ebccSVladimir Oltean * settings into sja1105_setup 628aa9ebccSVladimir Oltean */ 638aa9ebccSVladimir Oltean struct sja1105_dt_port { 648aa9ebccSVladimir Oltean phy_interface_t phy_mode; 658aa9ebccSVladimir Oltean sja1105_mii_role_t role; 668aa9ebccSVladimir Oltean }; 678aa9ebccSVladimir Oltean 688aa9ebccSVladimir Oltean static int sja1105_init_mac_settings(struct sja1105_private *priv) 698aa9ebccSVladimir Oltean { 708aa9ebccSVladimir Oltean struct sja1105_mac_config_entry default_mac = { 718aa9ebccSVladimir Oltean /* Enable all 8 priority queues on egress. 728aa9ebccSVladimir Oltean * Every queue i holds top[i] - base[i] frames. 738aa9ebccSVladimir Oltean * Sum of top[i] - base[i] is 511 (max hardware limit). 748aa9ebccSVladimir Oltean */ 758aa9ebccSVladimir Oltean .top = {0x3F, 0x7F, 0xBF, 0xFF, 0x13F, 0x17F, 0x1BF, 0x1FF}, 768aa9ebccSVladimir Oltean .base = {0x0, 0x40, 0x80, 0xC0, 0x100, 0x140, 0x180, 0x1C0}, 778aa9ebccSVladimir Oltean .enabled = {true, true, true, true, true, true, true, true}, 788aa9ebccSVladimir Oltean /* Keep standard IFG of 12 bytes on egress. */ 798aa9ebccSVladimir Oltean .ifg = 0, 808aa9ebccSVladimir Oltean /* Always put the MAC speed in automatic mode, where it can be 811fd4a173SVladimir Oltean * adjusted at runtime by PHYLINK. 828aa9ebccSVladimir Oltean */ 838aa9ebccSVladimir Oltean .speed = SJA1105_SPEED_AUTO, 848aa9ebccSVladimir Oltean /* No static correction for 1-step 1588 events */ 858aa9ebccSVladimir Oltean .tp_delin = 0, 868aa9ebccSVladimir Oltean .tp_delout = 0, 878aa9ebccSVladimir Oltean /* Disable aging for critical TTEthernet traffic */ 888aa9ebccSVladimir Oltean .maxage = 0xFF, 898aa9ebccSVladimir Oltean /* Internal VLAN (pvid) to apply to untagged ingress */ 908aa9ebccSVladimir Oltean .vlanprio = 0, 91e3502b82SVladimir Oltean .vlanid = 1, 928aa9ebccSVladimir Oltean .ing_mirr = false, 938aa9ebccSVladimir Oltean .egr_mirr = false, 948aa9ebccSVladimir Oltean /* Don't drop traffic with other EtherType than ETH_P_IP */ 958aa9ebccSVladimir Oltean .drpnona664 = false, 968aa9ebccSVladimir Oltean /* Don't drop double-tagged traffic */ 978aa9ebccSVladimir Oltean .drpdtag = false, 988aa9ebccSVladimir Oltean /* Don't drop untagged traffic */ 998aa9ebccSVladimir Oltean .drpuntag = false, 1008aa9ebccSVladimir Oltean /* Don't retag 802.1p (VID 0) traffic with the pvid */ 1018aa9ebccSVladimir Oltean .retag = false, 102640f763fSVladimir Oltean /* Disable learning and I/O on user ports by default - 103640f763fSVladimir Oltean * STP will enable it. 104640f763fSVladimir Oltean */ 105640f763fSVladimir Oltean .dyn_learn = false, 1068aa9ebccSVladimir Oltean .egress = false, 1078aa9ebccSVladimir Oltean .ingress = false, 1088aa9ebccSVladimir Oltean }; 1098aa9ebccSVladimir Oltean struct sja1105_mac_config_entry *mac; 1108aa9ebccSVladimir Oltean struct sja1105_table *table; 1118aa9ebccSVladimir Oltean int i; 1128aa9ebccSVladimir Oltean 1138aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_MAC_CONFIG]; 1148aa9ebccSVladimir Oltean 1158aa9ebccSVladimir Oltean /* Discard previous MAC Configuration Table */ 1168aa9ebccSVladimir Oltean if (table->entry_count) { 1178aa9ebccSVladimir Oltean kfree(table->entries); 1188aa9ebccSVladimir Oltean table->entry_count = 0; 1198aa9ebccSVladimir Oltean } 1208aa9ebccSVladimir Oltean 1218aa9ebccSVladimir Oltean table->entries = kcalloc(SJA1105_NUM_PORTS, 1228aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 1238aa9ebccSVladimir Oltean if (!table->entries) 1248aa9ebccSVladimir Oltean return -ENOMEM; 1258aa9ebccSVladimir Oltean 1268aa9ebccSVladimir Oltean table->entry_count = SJA1105_NUM_PORTS; 1278aa9ebccSVladimir Oltean 1288aa9ebccSVladimir Oltean mac = table->entries; 1298aa9ebccSVladimir Oltean 130640f763fSVladimir Oltean for (i = 0; i < SJA1105_NUM_PORTS; i++) { 1318aa9ebccSVladimir Oltean mac[i] = default_mac; 132640f763fSVladimir Oltean if (i == dsa_upstream_port(priv->ds, i)) { 133640f763fSVladimir Oltean /* STP doesn't get called for CPU port, so we need to 134640f763fSVladimir Oltean * set the I/O parameters statically. 135640f763fSVladimir Oltean */ 136640f763fSVladimir Oltean mac[i].dyn_learn = true; 137640f763fSVladimir Oltean mac[i].ingress = true; 138640f763fSVladimir Oltean mac[i].egress = true; 139640f763fSVladimir Oltean } 140640f763fSVladimir Oltean } 1418aa9ebccSVladimir Oltean 1428aa9ebccSVladimir Oltean return 0; 1438aa9ebccSVladimir Oltean } 1448aa9ebccSVladimir Oltean 145ffe10e67SVladimir Oltean static bool sja1105_supports_sgmii(struct sja1105_private *priv, int port) 146ffe10e67SVladimir Oltean { 147ffe10e67SVladimir Oltean if (priv->info->part_no != SJA1105R_PART_NO && 148ffe10e67SVladimir Oltean priv->info->part_no != SJA1105S_PART_NO) 149ffe10e67SVladimir Oltean return false; 150ffe10e67SVladimir Oltean 151ffe10e67SVladimir Oltean if (port != SJA1105_SGMII_PORT) 152ffe10e67SVladimir Oltean return false; 153ffe10e67SVladimir Oltean 154ffe10e67SVladimir Oltean if (dsa_is_unused_port(priv->ds, port)) 155ffe10e67SVladimir Oltean return false; 156ffe10e67SVladimir Oltean 157ffe10e67SVladimir Oltean return true; 158ffe10e67SVladimir Oltean } 159ffe10e67SVladimir Oltean 1608aa9ebccSVladimir Oltean static int sja1105_init_mii_settings(struct sja1105_private *priv, 1618aa9ebccSVladimir Oltean struct sja1105_dt_port *ports) 1628aa9ebccSVladimir Oltean { 1638aa9ebccSVladimir Oltean struct device *dev = &priv->spidev->dev; 1648aa9ebccSVladimir Oltean struct sja1105_xmii_params_entry *mii; 1658aa9ebccSVladimir Oltean struct sja1105_table *table; 1668aa9ebccSVladimir Oltean int i; 1678aa9ebccSVladimir Oltean 1688aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_XMII_PARAMS]; 1698aa9ebccSVladimir Oltean 1708aa9ebccSVladimir Oltean /* Discard previous xMII Mode Parameters Table */ 1718aa9ebccSVladimir Oltean if (table->entry_count) { 1728aa9ebccSVladimir Oltean kfree(table->entries); 1738aa9ebccSVladimir Oltean table->entry_count = 0; 1748aa9ebccSVladimir Oltean } 1758aa9ebccSVladimir Oltean 1768aa9ebccSVladimir Oltean table->entries = kcalloc(SJA1105_MAX_XMII_PARAMS_COUNT, 1778aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 1788aa9ebccSVladimir Oltean if (!table->entries) 1798aa9ebccSVladimir Oltean return -ENOMEM; 1808aa9ebccSVladimir Oltean 1811fd4a173SVladimir Oltean /* Override table based on PHYLINK DT bindings */ 1828aa9ebccSVladimir Oltean table->entry_count = SJA1105_MAX_XMII_PARAMS_COUNT; 1838aa9ebccSVladimir Oltean 1848aa9ebccSVladimir Oltean mii = table->entries; 1858aa9ebccSVladimir Oltean 1868aa9ebccSVladimir Oltean for (i = 0; i < SJA1105_NUM_PORTS; i++) { 187ee9d0cb6SVladimir Oltean if (dsa_is_unused_port(priv->ds, i)) 188ee9d0cb6SVladimir Oltean continue; 189ee9d0cb6SVladimir Oltean 1908aa9ebccSVladimir Oltean switch (ports[i].phy_mode) { 1918aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_MII: 1928aa9ebccSVladimir Oltean mii->xmii_mode[i] = XMII_MODE_MII; 1938aa9ebccSVladimir Oltean break; 1948aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RMII: 1958aa9ebccSVladimir Oltean mii->xmii_mode[i] = XMII_MODE_RMII; 1968aa9ebccSVladimir Oltean break; 1978aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RGMII: 1988aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RGMII_ID: 1998aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RGMII_RXID: 2008aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RGMII_TXID: 2018aa9ebccSVladimir Oltean mii->xmii_mode[i] = XMII_MODE_RGMII; 2028aa9ebccSVladimir Oltean break; 203ffe10e67SVladimir Oltean case PHY_INTERFACE_MODE_SGMII: 204ffe10e67SVladimir Oltean if (!sja1105_supports_sgmii(priv, i)) 205ffe10e67SVladimir Oltean return -EINVAL; 206ffe10e67SVladimir Oltean mii->xmii_mode[i] = XMII_MODE_SGMII; 207ffe10e67SVladimir Oltean break; 2088aa9ebccSVladimir Oltean default: 2098aa9ebccSVladimir Oltean dev_err(dev, "Unsupported PHY mode %s!\n", 2108aa9ebccSVladimir Oltean phy_modes(ports[i].phy_mode)); 2116729188dSVladimir Oltean return -EINVAL; 2128aa9ebccSVladimir Oltean } 2138aa9ebccSVladimir Oltean 214ffe10e67SVladimir Oltean /* Even though the SerDes port is able to drive SGMII autoneg 215ffe10e67SVladimir Oltean * like a PHY would, from the perspective of the XMII tables, 216ffe10e67SVladimir Oltean * the SGMII port should always be put in MAC mode. 217ffe10e67SVladimir Oltean */ 218ffe10e67SVladimir Oltean if (ports[i].phy_mode == PHY_INTERFACE_MODE_SGMII) 219ffe10e67SVladimir Oltean mii->phy_mac[i] = XMII_MAC; 220ffe10e67SVladimir Oltean else 2218aa9ebccSVladimir Oltean mii->phy_mac[i] = ports[i].role; 2228aa9ebccSVladimir Oltean } 2238aa9ebccSVladimir Oltean return 0; 2248aa9ebccSVladimir Oltean } 2258aa9ebccSVladimir Oltean 2268aa9ebccSVladimir Oltean static int sja1105_init_static_fdb(struct sja1105_private *priv) 2278aa9ebccSVladimir Oltean { 2284d942354SVladimir Oltean struct sja1105_l2_lookup_entry *l2_lookup; 2298aa9ebccSVladimir Oltean struct sja1105_table *table; 2304d942354SVladimir Oltean int port; 2318aa9ebccSVladimir Oltean 2328aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; 2338aa9ebccSVladimir Oltean 2344d942354SVladimir Oltean /* We only populate the FDB table through dynamic L2 Address Lookup 2354d942354SVladimir Oltean * entries, except for a special entry at the end which is a catch-all 2364d942354SVladimir Oltean * for unknown multicast and will be used to control flooding domain. 237291d1e72SVladimir Oltean */ 2388aa9ebccSVladimir Oltean if (table->entry_count) { 2398aa9ebccSVladimir Oltean kfree(table->entries); 2408aa9ebccSVladimir Oltean table->entry_count = 0; 2418aa9ebccSVladimir Oltean } 2424d942354SVladimir Oltean 2434d942354SVladimir Oltean if (!priv->info->can_limit_mcast_flood) 2444d942354SVladimir Oltean return 0; 2454d942354SVladimir Oltean 2464d942354SVladimir Oltean table->entries = kcalloc(1, table->ops->unpacked_entry_size, 2474d942354SVladimir Oltean GFP_KERNEL); 2484d942354SVladimir Oltean if (!table->entries) 2494d942354SVladimir Oltean return -ENOMEM; 2504d942354SVladimir Oltean 2514d942354SVladimir Oltean table->entry_count = 1; 2524d942354SVladimir Oltean l2_lookup = table->entries; 2534d942354SVladimir Oltean 2544d942354SVladimir Oltean /* All L2 multicast addresses have an odd first octet */ 2554d942354SVladimir Oltean l2_lookup[0].macaddr = SJA1105_UNKNOWN_MULTICAST; 2564d942354SVladimir Oltean l2_lookup[0].mask_macaddr = SJA1105_UNKNOWN_MULTICAST; 2574d942354SVladimir Oltean l2_lookup[0].lockeds = true; 2584d942354SVladimir Oltean l2_lookup[0].index = SJA1105_MAX_L2_LOOKUP_COUNT - 1; 2594d942354SVladimir Oltean 2604d942354SVladimir Oltean /* Flood multicast to every port by default */ 2614d942354SVladimir Oltean for (port = 0; port < priv->ds->num_ports; port++) 2624d942354SVladimir Oltean if (!dsa_is_unused_port(priv->ds, port)) 2634d942354SVladimir Oltean l2_lookup[0].destports |= BIT(port); 2644d942354SVladimir Oltean 2658aa9ebccSVladimir Oltean return 0; 2668aa9ebccSVladimir Oltean } 2678aa9ebccSVladimir Oltean 2688aa9ebccSVladimir Oltean static int sja1105_init_l2_lookup_params(struct sja1105_private *priv) 2698aa9ebccSVladimir Oltean { 2708aa9ebccSVladimir Oltean struct sja1105_table *table; 2716c56e167SVladimir Oltean u64 max_fdb_entries = SJA1105_MAX_L2_LOOKUP_COUNT / SJA1105_NUM_PORTS; 2728aa9ebccSVladimir Oltean struct sja1105_l2_lookup_params_entry default_l2_lookup_params = { 2738456721dSVladimir Oltean /* Learned FDB entries are forgotten after 300 seconds */ 2748456721dSVladimir Oltean .maxage = SJA1105_AGEING_TIME_MS(300000), 2758aa9ebccSVladimir Oltean /* All entries within a FDB bin are available for learning */ 2768aa9ebccSVladimir Oltean .dyn_tbsz = SJA1105ET_FDB_BIN_SIZE, 2771da73821SVladimir Oltean /* And the P/Q/R/S equivalent setting: */ 2781da73821SVladimir Oltean .start_dynspc = 0, 2796c56e167SVladimir Oltean .maxaddrp = {max_fdb_entries, max_fdb_entries, max_fdb_entries, 2806c56e167SVladimir Oltean max_fdb_entries, max_fdb_entries, }, 2818aa9ebccSVladimir Oltean /* 2^8 + 2^5 + 2^3 + 2^2 + 2^1 + 1 in Koopman notation */ 2828aa9ebccSVladimir Oltean .poly = 0x97, 2838aa9ebccSVladimir Oltean /* This selects between Independent VLAN Learning (IVL) and 2848aa9ebccSVladimir Oltean * Shared VLAN Learning (SVL) 2858aa9ebccSVladimir Oltean */ 2866d7c7d94SVladimir Oltean .shared_learn = true, 2878aa9ebccSVladimir Oltean /* Don't discard management traffic based on ENFPORT - 2888aa9ebccSVladimir Oltean * we don't perform SMAC port enforcement anyway, so 2898aa9ebccSVladimir Oltean * what we are setting here doesn't matter. 2908aa9ebccSVladimir Oltean */ 2918aa9ebccSVladimir Oltean .no_enf_hostprt = false, 2928aa9ebccSVladimir Oltean /* Don't learn SMAC for mac_fltres1 and mac_fltres0. 2938aa9ebccSVladimir Oltean * Maybe correlate with no_linklocal_learn from bridge driver? 2948aa9ebccSVladimir Oltean */ 2958aa9ebccSVladimir Oltean .no_mgmt_learn = true, 2961da73821SVladimir Oltean /* P/Q/R/S only */ 2971da73821SVladimir Oltean .use_static = true, 2981da73821SVladimir Oltean /* Dynamically learned FDB entries can overwrite other (older) 2991da73821SVladimir Oltean * dynamic FDB entries 3001da73821SVladimir Oltean */ 3011da73821SVladimir Oltean .owr_dyn = true, 3021da73821SVladimir Oltean .drpnolearn = true, 3038aa9ebccSVladimir Oltean }; 3048aa9ebccSVladimir Oltean 3058aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS]; 3068aa9ebccSVladimir Oltean 3078aa9ebccSVladimir Oltean if (table->entry_count) { 3088aa9ebccSVladimir Oltean kfree(table->entries); 3098aa9ebccSVladimir Oltean table->entry_count = 0; 3108aa9ebccSVladimir Oltean } 3118aa9ebccSVladimir Oltean 3128aa9ebccSVladimir Oltean table->entries = kcalloc(SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT, 3138aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 3148aa9ebccSVladimir Oltean if (!table->entries) 3158aa9ebccSVladimir Oltean return -ENOMEM; 3168aa9ebccSVladimir Oltean 3178aa9ebccSVladimir Oltean table->entry_count = SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT; 3188aa9ebccSVladimir Oltean 3198aa9ebccSVladimir Oltean /* This table only has a single entry */ 3208aa9ebccSVladimir Oltean ((struct sja1105_l2_lookup_params_entry *)table->entries)[0] = 3218aa9ebccSVladimir Oltean default_l2_lookup_params; 3228aa9ebccSVladimir Oltean 3238aa9ebccSVladimir Oltean return 0; 3248aa9ebccSVladimir Oltean } 3258aa9ebccSVladimir Oltean 326*ed040abcSVladimir Oltean /* Set up a default VLAN for untagged traffic injected from the CPU 327*ed040abcSVladimir Oltean * using management routes (e.g. STP, PTP) as opposed to tag_8021q. 328*ed040abcSVladimir Oltean * All DT-defined ports are members of this VLAN, and there are no 329*ed040abcSVladimir Oltean * restrictions on forwarding (since the CPU selects the destination). 330*ed040abcSVladimir Oltean * Frames from this VLAN will always be transmitted as untagged, and 331*ed040abcSVladimir Oltean * neither the bridge nor the 8021q module cannot create this VLAN ID. 332*ed040abcSVladimir Oltean */ 3338aa9ebccSVladimir Oltean static int sja1105_init_static_vlan(struct sja1105_private *priv) 3348aa9ebccSVladimir Oltean { 3358aa9ebccSVladimir Oltean struct sja1105_table *table; 3368aa9ebccSVladimir Oltean struct sja1105_vlan_lookup_entry pvid = { 3378aa9ebccSVladimir Oltean .ving_mirr = 0, 3388aa9ebccSVladimir Oltean .vegr_mirr = 0, 3398aa9ebccSVladimir Oltean .vmemb_port = 0, 3408aa9ebccSVladimir Oltean .vlan_bc = 0, 3418aa9ebccSVladimir Oltean .tag_port = 0, 342*ed040abcSVladimir Oltean .vlanid = SJA1105_DEFAULT_VLAN, 3438aa9ebccSVladimir Oltean }; 344ec5ae610SVladimir Oltean struct dsa_switch *ds = priv->ds; 345ec5ae610SVladimir Oltean int port; 3468aa9ebccSVladimir Oltean 3478aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP]; 3488aa9ebccSVladimir Oltean 3498aa9ebccSVladimir Oltean if (table->entry_count) { 3508aa9ebccSVladimir Oltean kfree(table->entries); 3518aa9ebccSVladimir Oltean table->entry_count = 0; 3528aa9ebccSVladimir Oltean } 3538aa9ebccSVladimir Oltean 354c75857b0SZheng Yongjun table->entries = kzalloc(table->ops->unpacked_entry_size, 3558aa9ebccSVladimir Oltean GFP_KERNEL); 3568aa9ebccSVladimir Oltean if (!table->entries) 3578aa9ebccSVladimir Oltean return -ENOMEM; 3588aa9ebccSVladimir Oltean 3598aa9ebccSVladimir Oltean table->entry_count = 1; 3608aa9ebccSVladimir Oltean 361ec5ae610SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 362ec5ae610SVladimir Oltean struct sja1105_bridge_vlan *v; 363ec5ae610SVladimir Oltean 364ec5ae610SVladimir Oltean if (dsa_is_unused_port(ds, port)) 365ec5ae610SVladimir Oltean continue; 366ec5ae610SVladimir Oltean 367ec5ae610SVladimir Oltean pvid.vmemb_port |= BIT(port); 368ec5ae610SVladimir Oltean pvid.vlan_bc |= BIT(port); 369ec5ae610SVladimir Oltean pvid.tag_port &= ~BIT(port); 370ec5ae610SVladimir Oltean 371ec5ae610SVladimir Oltean v = kzalloc(sizeof(*v), GFP_KERNEL); 372ec5ae610SVladimir Oltean if (!v) 373ec5ae610SVladimir Oltean return -ENOMEM; 374ec5ae610SVladimir Oltean 375ec5ae610SVladimir Oltean v->port = port; 376*ed040abcSVladimir Oltean v->vid = SJA1105_DEFAULT_VLAN; 377ec5ae610SVladimir Oltean v->untagged = true; 378ec5ae610SVladimir Oltean if (dsa_is_cpu_port(ds, port)) 379ec5ae610SVladimir Oltean v->pvid = true; 380ec5ae610SVladimir Oltean list_add(&v->list, &priv->dsa_8021q_vlans); 3818aa9ebccSVladimir Oltean } 3828aa9ebccSVladimir Oltean 3838aa9ebccSVladimir Oltean ((struct sja1105_vlan_lookup_entry *)table->entries)[0] = pvid; 3848aa9ebccSVladimir Oltean return 0; 3858aa9ebccSVladimir Oltean } 3868aa9ebccSVladimir Oltean 3878aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding(struct sja1105_private *priv) 3888aa9ebccSVladimir Oltean { 3898aa9ebccSVladimir Oltean struct sja1105_l2_forwarding_entry *l2fwd; 3908aa9ebccSVladimir Oltean struct sja1105_table *table; 3918aa9ebccSVladimir Oltean int i, j; 3928aa9ebccSVladimir Oltean 3938aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING]; 3948aa9ebccSVladimir Oltean 3958aa9ebccSVladimir Oltean if (table->entry_count) { 3968aa9ebccSVladimir Oltean kfree(table->entries); 3978aa9ebccSVladimir Oltean table->entry_count = 0; 3988aa9ebccSVladimir Oltean } 3998aa9ebccSVladimir Oltean 4008aa9ebccSVladimir Oltean table->entries = kcalloc(SJA1105_MAX_L2_FORWARDING_COUNT, 4018aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 4028aa9ebccSVladimir Oltean if (!table->entries) 4038aa9ebccSVladimir Oltean return -ENOMEM; 4048aa9ebccSVladimir Oltean 4058aa9ebccSVladimir Oltean table->entry_count = SJA1105_MAX_L2_FORWARDING_COUNT; 4068aa9ebccSVladimir Oltean 4078aa9ebccSVladimir Oltean l2fwd = table->entries; 4088aa9ebccSVladimir Oltean 4098aa9ebccSVladimir Oltean /* First 5 entries define the forwarding rules */ 4108aa9ebccSVladimir Oltean for (i = 0; i < SJA1105_NUM_PORTS; i++) { 4118aa9ebccSVladimir Oltean unsigned int upstream = dsa_upstream_port(priv->ds, i); 4128aa9ebccSVladimir Oltean 4138aa9ebccSVladimir Oltean for (j = 0; j < SJA1105_NUM_TC; j++) 4148aa9ebccSVladimir Oltean l2fwd[i].vlan_pmap[j] = j; 4158aa9ebccSVladimir Oltean 4167f7ccdeaSVladimir Oltean /* All ports start up with egress flooding enabled, 4177f7ccdeaSVladimir Oltean * including the CPU port. 4187f7ccdeaSVladimir Oltean */ 4197f7ccdeaSVladimir Oltean priv->ucast_egress_floods |= BIT(i); 4207f7ccdeaSVladimir Oltean priv->bcast_egress_floods |= BIT(i); 4217f7ccdeaSVladimir Oltean 4228aa9ebccSVladimir Oltean if (i == upstream) 4238aa9ebccSVladimir Oltean continue; 4248aa9ebccSVladimir Oltean 4258aa9ebccSVladimir Oltean sja1105_port_allow_traffic(l2fwd, i, upstream, true); 4268aa9ebccSVladimir Oltean sja1105_port_allow_traffic(l2fwd, upstream, i, true); 4274d942354SVladimir Oltean 4284d942354SVladimir Oltean l2fwd[i].bc_domain = BIT(upstream); 4294d942354SVladimir Oltean l2fwd[i].fl_domain = BIT(upstream); 4304d942354SVladimir Oltean 4314d942354SVladimir Oltean l2fwd[upstream].bc_domain |= BIT(i); 4324d942354SVladimir Oltean l2fwd[upstream].fl_domain |= BIT(i); 4338aa9ebccSVladimir Oltean } 4348aa9ebccSVladimir Oltean /* Next 8 entries define VLAN PCP mapping from ingress to egress. 4358aa9ebccSVladimir Oltean * Create a one-to-one mapping. 4368aa9ebccSVladimir Oltean */ 4378aa9ebccSVladimir Oltean for (i = 0; i < SJA1105_NUM_TC; i++) 4388aa9ebccSVladimir Oltean for (j = 0; j < SJA1105_NUM_PORTS; j++) 4398aa9ebccSVladimir Oltean l2fwd[SJA1105_NUM_PORTS + i].vlan_pmap[j] = i; 4408aa9ebccSVladimir Oltean 4418aa9ebccSVladimir Oltean return 0; 4428aa9ebccSVladimir Oltean } 4438aa9ebccSVladimir Oltean 4448aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding_params(struct sja1105_private *priv) 4458aa9ebccSVladimir Oltean { 4468aa9ebccSVladimir Oltean struct sja1105_l2_forwarding_params_entry default_l2fwd_params = { 4478aa9ebccSVladimir Oltean /* Disallow dynamic reconfiguration of vlan_pmap */ 4488aa9ebccSVladimir Oltean .max_dynp = 0, 4498aa9ebccSVladimir Oltean /* Use a single memory partition for all ingress queues */ 4508aa9ebccSVladimir Oltean .part_spc = { SJA1105_MAX_FRAME_MEMORY, 0, 0, 0, 0, 0, 0, 0 }, 4518aa9ebccSVladimir Oltean }; 4528aa9ebccSVladimir Oltean struct sja1105_table *table; 4538aa9ebccSVladimir Oltean 4548aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS]; 4558aa9ebccSVladimir Oltean 4568aa9ebccSVladimir Oltean if (table->entry_count) { 4578aa9ebccSVladimir Oltean kfree(table->entries); 4588aa9ebccSVladimir Oltean table->entry_count = 0; 4598aa9ebccSVladimir Oltean } 4608aa9ebccSVladimir Oltean 4618aa9ebccSVladimir Oltean table->entries = kcalloc(SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT, 4628aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 4638aa9ebccSVladimir Oltean if (!table->entries) 4648aa9ebccSVladimir Oltean return -ENOMEM; 4658aa9ebccSVladimir Oltean 4668aa9ebccSVladimir Oltean table->entry_count = SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT; 4678aa9ebccSVladimir Oltean 4688aa9ebccSVladimir Oltean /* This table only has a single entry */ 4698aa9ebccSVladimir Oltean ((struct sja1105_l2_forwarding_params_entry *)table->entries)[0] = 4708aa9ebccSVladimir Oltean default_l2fwd_params; 4718aa9ebccSVladimir Oltean 4728aa9ebccSVladimir Oltean return 0; 4738aa9ebccSVladimir Oltean } 4748aa9ebccSVladimir Oltean 475aaa270c6SVladimir Oltean void sja1105_frame_memory_partitioning(struct sja1105_private *priv) 476aaa270c6SVladimir Oltean { 477aaa270c6SVladimir Oltean struct sja1105_l2_forwarding_params_entry *l2_fwd_params; 478aaa270c6SVladimir Oltean struct sja1105_vl_forwarding_params_entry *vl_fwd_params; 479aaa270c6SVladimir Oltean struct sja1105_table *table; 480aaa270c6SVladimir Oltean int max_mem; 481aaa270c6SVladimir Oltean 482aaa270c6SVladimir Oltean /* VLAN retagging is implemented using a loopback port that consumes 483aaa270c6SVladimir Oltean * frame buffers. That leaves less for us. 484aaa270c6SVladimir Oltean */ 485aaa270c6SVladimir Oltean if (priv->vlan_state == SJA1105_VLAN_BEST_EFFORT) 486aaa270c6SVladimir Oltean max_mem = SJA1105_MAX_FRAME_MEMORY_RETAGGING; 487aaa270c6SVladimir Oltean else 488aaa270c6SVladimir Oltean max_mem = SJA1105_MAX_FRAME_MEMORY; 489aaa270c6SVladimir Oltean 490aaa270c6SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS]; 491aaa270c6SVladimir Oltean l2_fwd_params = table->entries; 492aaa270c6SVladimir Oltean l2_fwd_params->part_spc[0] = max_mem; 493aaa270c6SVladimir Oltean 494aaa270c6SVladimir Oltean /* If we have any critical-traffic virtual links, we need to reserve 495aaa270c6SVladimir Oltean * some frame buffer memory for them. At the moment, hardcode the value 496aaa270c6SVladimir Oltean * at 100 blocks of 128 bytes of memory each. This leaves 829 blocks 497aaa270c6SVladimir Oltean * remaining for best-effort traffic. TODO: figure out a more flexible 498aaa270c6SVladimir Oltean * way to perform the frame buffer partitioning. 499aaa270c6SVladimir Oltean */ 500aaa270c6SVladimir Oltean if (!priv->static_config.tables[BLK_IDX_VL_FORWARDING].entry_count) 501aaa270c6SVladimir Oltean return; 502aaa270c6SVladimir Oltean 503aaa270c6SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_VL_FORWARDING_PARAMS]; 504aaa270c6SVladimir Oltean vl_fwd_params = table->entries; 505aaa270c6SVladimir Oltean 506aaa270c6SVladimir Oltean l2_fwd_params->part_spc[0] -= SJA1105_VL_FRAME_MEMORY; 507aaa270c6SVladimir Oltean vl_fwd_params->partspc[0] = SJA1105_VL_FRAME_MEMORY; 508aaa270c6SVladimir Oltean } 509aaa270c6SVladimir Oltean 5108aa9ebccSVladimir Oltean static int sja1105_init_general_params(struct sja1105_private *priv) 5118aa9ebccSVladimir Oltean { 5128aa9ebccSVladimir Oltean struct sja1105_general_params_entry default_general_params = { 513511e6ca0SVladimir Oltean /* Allow dynamic changing of the mirror port */ 514511e6ca0SVladimir Oltean .mirr_ptacu = true, 5158aa9ebccSVladimir Oltean .switchid = priv->ds->index, 5165f06c63bSVladimir Oltean /* Priority queue for link-local management frames 5175f06c63bSVladimir Oltean * (both ingress to and egress from CPU - PTP, STP etc) 5185f06c63bSVladimir Oltean */ 51908fde09aSVladimir Oltean .hostprio = 7, 5208aa9ebccSVladimir Oltean .mac_fltres1 = SJA1105_LINKLOCAL_FILTER_A, 5218aa9ebccSVladimir Oltean .mac_flt1 = SJA1105_LINKLOCAL_FILTER_A_MASK, 52242824463SVladimir Oltean .incl_srcpt1 = false, 5238aa9ebccSVladimir Oltean .send_meta1 = false, 5248aa9ebccSVladimir Oltean .mac_fltres0 = SJA1105_LINKLOCAL_FILTER_B, 5258aa9ebccSVladimir Oltean .mac_flt0 = SJA1105_LINKLOCAL_FILTER_B_MASK, 52642824463SVladimir Oltean .incl_srcpt0 = false, 5278aa9ebccSVladimir Oltean .send_meta0 = false, 5288aa9ebccSVladimir Oltean /* The destination for traffic matching mac_fltres1 and 5298aa9ebccSVladimir Oltean * mac_fltres0 on all ports except host_port. Such traffic 5308aa9ebccSVladimir Oltean * receieved on host_port itself would be dropped, except 5318aa9ebccSVladimir Oltean * by installing a temporary 'management route' 5328aa9ebccSVladimir Oltean */ 5338aa9ebccSVladimir Oltean .host_port = dsa_upstream_port(priv->ds, 0), 534511e6ca0SVladimir Oltean /* Default to an invalid value */ 535511e6ca0SVladimir Oltean .mirr_port = SJA1105_NUM_PORTS, 5368aa9ebccSVladimir Oltean /* Link-local traffic received on casc_port will be forwarded 5378aa9ebccSVladimir Oltean * to host_port without embedding the source port and device ID 5388aa9ebccSVladimir Oltean * info in the destination MAC address (presumably because it 5398aa9ebccSVladimir Oltean * is a cascaded port and a downstream SJA switch already did 5408aa9ebccSVladimir Oltean * that). Default to an invalid port (to disable the feature) 5418aa9ebccSVladimir Oltean * and overwrite this if we find any DSA (cascaded) ports. 5428aa9ebccSVladimir Oltean */ 5438aa9ebccSVladimir Oltean .casc_port = SJA1105_NUM_PORTS, 5448aa9ebccSVladimir Oltean /* No TTEthernet */ 545dfacc5a2SVladimir Oltean .vllupformat = SJA1105_VL_FORMAT_PSFP, 5468aa9ebccSVladimir Oltean .vlmarker = 0, 5478aa9ebccSVladimir Oltean .vlmask = 0, 5488aa9ebccSVladimir Oltean /* Only update correctionField for 1-step PTP (L2 transport) */ 5498aa9ebccSVladimir Oltean .ignore2stf = 0, 5506666cebcSVladimir Oltean /* Forcefully disable VLAN filtering by telling 5516666cebcSVladimir Oltean * the switch that VLAN has a different EtherType. 5526666cebcSVladimir Oltean */ 5536666cebcSVladimir Oltean .tpid = ETH_P_SJA1105, 5546666cebcSVladimir Oltean .tpid2 = ETH_P_SJA1105, 5558aa9ebccSVladimir Oltean }; 5568aa9ebccSVladimir Oltean struct sja1105_table *table; 5578aa9ebccSVladimir Oltean 5588aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; 5598aa9ebccSVladimir Oltean 5608aa9ebccSVladimir Oltean if (table->entry_count) { 5618aa9ebccSVladimir Oltean kfree(table->entries); 5628aa9ebccSVladimir Oltean table->entry_count = 0; 5638aa9ebccSVladimir Oltean } 5648aa9ebccSVladimir Oltean 5658aa9ebccSVladimir Oltean table->entries = kcalloc(SJA1105_MAX_GENERAL_PARAMS_COUNT, 5668aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 5678aa9ebccSVladimir Oltean if (!table->entries) 5688aa9ebccSVladimir Oltean return -ENOMEM; 5698aa9ebccSVladimir Oltean 5708aa9ebccSVladimir Oltean table->entry_count = SJA1105_MAX_GENERAL_PARAMS_COUNT; 5718aa9ebccSVladimir Oltean 5728aa9ebccSVladimir Oltean /* This table only has a single entry */ 5738aa9ebccSVladimir Oltean ((struct sja1105_general_params_entry *)table->entries)[0] = 5748aa9ebccSVladimir Oltean default_general_params; 5758aa9ebccSVladimir Oltean 5768aa9ebccSVladimir Oltean return 0; 5778aa9ebccSVladimir Oltean } 5788aa9ebccSVladimir Oltean 57979d5511cSVladimir Oltean static int sja1105_init_avb_params(struct sja1105_private *priv) 58079d5511cSVladimir Oltean { 58179d5511cSVladimir Oltean struct sja1105_avb_params_entry *avb; 58279d5511cSVladimir Oltean struct sja1105_table *table; 58379d5511cSVladimir Oltean 58479d5511cSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_AVB_PARAMS]; 58579d5511cSVladimir Oltean 58679d5511cSVladimir Oltean /* Discard previous AVB Parameters Table */ 58779d5511cSVladimir Oltean if (table->entry_count) { 58879d5511cSVladimir Oltean kfree(table->entries); 58979d5511cSVladimir Oltean table->entry_count = 0; 59079d5511cSVladimir Oltean } 59179d5511cSVladimir Oltean 59279d5511cSVladimir Oltean table->entries = kcalloc(SJA1105_MAX_AVB_PARAMS_COUNT, 59379d5511cSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 59479d5511cSVladimir Oltean if (!table->entries) 59579d5511cSVladimir Oltean return -ENOMEM; 59679d5511cSVladimir Oltean 59779d5511cSVladimir Oltean table->entry_count = SJA1105_MAX_AVB_PARAMS_COUNT; 59879d5511cSVladimir Oltean 59979d5511cSVladimir Oltean avb = table->entries; 60079d5511cSVladimir Oltean 60179d5511cSVladimir Oltean /* Configure the MAC addresses for meta frames */ 60279d5511cSVladimir Oltean avb->destmeta = SJA1105_META_DMAC; 60379d5511cSVladimir Oltean avb->srcmeta = SJA1105_META_SMAC; 604747e5eb3SVladimir Oltean /* On P/Q/R/S, configure the direction of the PTP_CLK pin as input by 605747e5eb3SVladimir Oltean * default. This is because there might be boards with a hardware 606747e5eb3SVladimir Oltean * layout where enabling the pin as output might cause an electrical 607747e5eb3SVladimir Oltean * clash. On E/T the pin is always an output, which the board designers 608747e5eb3SVladimir Oltean * probably already knew, so even if there are going to be electrical 609747e5eb3SVladimir Oltean * issues, there's nothing we can do. 610747e5eb3SVladimir Oltean */ 611747e5eb3SVladimir Oltean avb->cas_master = false; 61279d5511cSVladimir Oltean 61379d5511cSVladimir Oltean return 0; 61479d5511cSVladimir Oltean } 61579d5511cSVladimir Oltean 616a7cc081cSVladimir Oltean /* The L2 policing table is 2-stage. The table is looked up for each frame 617a7cc081cSVladimir Oltean * according to the ingress port, whether it was broadcast or not, and the 618a7cc081cSVladimir Oltean * classified traffic class (given by VLAN PCP). This portion of the lookup is 619a7cc081cSVladimir Oltean * fixed, and gives access to the SHARINDX, an indirection register pointing 620a7cc081cSVladimir Oltean * within the policing table itself, which is used to resolve the policer that 621a7cc081cSVladimir Oltean * will be used for this frame. 622a7cc081cSVladimir Oltean * 623a7cc081cSVladimir Oltean * Stage 1 Stage 2 624a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 625a7cc081cSVladimir Oltean * |Port 0 TC 0 |SHARINDX| | Policer 0: Rate, Burst, MTU | 626a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 627a7cc081cSVladimir Oltean * |Port 0 TC 1 |SHARINDX| | Policer 1: Rate, Burst, MTU | 628a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 629a7cc081cSVladimir Oltean * ... | Policer 2: Rate, Burst, MTU | 630a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 631a7cc081cSVladimir Oltean * |Port 0 TC 7 |SHARINDX| | Policer 3: Rate, Burst, MTU | 632a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 633a7cc081cSVladimir Oltean * |Port 1 TC 0 |SHARINDX| | Policer 4: Rate, Burst, MTU | 634a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 635a7cc081cSVladimir Oltean * ... | Policer 5: Rate, Burst, MTU | 636a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 637a7cc081cSVladimir Oltean * |Port 1 TC 7 |SHARINDX| | Policer 6: Rate, Burst, MTU | 638a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 639a7cc081cSVladimir Oltean * ... | Policer 7: Rate, Burst, MTU | 640a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 641a7cc081cSVladimir Oltean * |Port 4 TC 7 |SHARINDX| ... 642a7cc081cSVladimir Oltean * +------------+--------+ 643a7cc081cSVladimir Oltean * |Port 0 BCAST|SHARINDX| ... 644a7cc081cSVladimir Oltean * +------------+--------+ 645a7cc081cSVladimir Oltean * |Port 1 BCAST|SHARINDX| ... 646a7cc081cSVladimir Oltean * +------------+--------+ 647a7cc081cSVladimir Oltean * ... ... 648a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 649a7cc081cSVladimir Oltean * |Port 4 BCAST|SHARINDX| | Policer 44: Rate, Burst, MTU | 650a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 651a7cc081cSVladimir Oltean * 652a7cc081cSVladimir Oltean * In this driver, we shall use policers 0-4 as statically alocated port 653a7cc081cSVladimir Oltean * (matchall) policers. So we need to make the SHARINDX for all lookups 654a7cc081cSVladimir Oltean * corresponding to this ingress port (8 VLAN PCP lookups and 1 broadcast 655a7cc081cSVladimir Oltean * lookup) equal. 656a7cc081cSVladimir Oltean * The remaining policers (40) shall be dynamically allocated for flower 657a7cc081cSVladimir Oltean * policers, where the key is either vlan_prio or dst_mac ff:ff:ff:ff:ff:ff. 658a7cc081cSVladimir Oltean */ 6598aa9ebccSVladimir Oltean #define SJA1105_RATE_MBPS(speed) (((speed) * 64000) / 1000) 6608aa9ebccSVladimir Oltean 6618aa9ebccSVladimir Oltean static int sja1105_init_l2_policing(struct sja1105_private *priv) 6628aa9ebccSVladimir Oltean { 6638aa9ebccSVladimir Oltean struct sja1105_l2_policing_entry *policing; 6648aa9ebccSVladimir Oltean struct sja1105_table *table; 665a7cc081cSVladimir Oltean int port, tc; 6668aa9ebccSVladimir Oltean 6678aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_POLICING]; 6688aa9ebccSVladimir Oltean 6698aa9ebccSVladimir Oltean /* Discard previous L2 Policing Table */ 6708aa9ebccSVladimir Oltean if (table->entry_count) { 6718aa9ebccSVladimir Oltean kfree(table->entries); 6728aa9ebccSVladimir Oltean table->entry_count = 0; 6738aa9ebccSVladimir Oltean } 6748aa9ebccSVladimir Oltean 6758aa9ebccSVladimir Oltean table->entries = kcalloc(SJA1105_MAX_L2_POLICING_COUNT, 6768aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 6778aa9ebccSVladimir Oltean if (!table->entries) 6788aa9ebccSVladimir Oltean return -ENOMEM; 6798aa9ebccSVladimir Oltean 6808aa9ebccSVladimir Oltean table->entry_count = SJA1105_MAX_L2_POLICING_COUNT; 6818aa9ebccSVladimir Oltean 6828aa9ebccSVladimir Oltean policing = table->entries; 6838aa9ebccSVladimir Oltean 684a7cc081cSVladimir Oltean /* Setup shared indices for the matchall policers */ 685a7cc081cSVladimir Oltean for (port = 0; port < SJA1105_NUM_PORTS; port++) { 686a7cc081cSVladimir Oltean int bcast = (SJA1105_NUM_PORTS * SJA1105_NUM_TC) + port; 687a7cc081cSVladimir Oltean 688a7cc081cSVladimir Oltean for (tc = 0; tc < SJA1105_NUM_TC; tc++) 689a7cc081cSVladimir Oltean policing[port * SJA1105_NUM_TC + tc].sharindx = port; 690a7cc081cSVladimir Oltean 691a7cc081cSVladimir Oltean policing[bcast].sharindx = port; 692a7cc081cSVladimir Oltean } 693a7cc081cSVladimir Oltean 694a7cc081cSVladimir Oltean /* Setup the matchall policer parameters */ 695a7cc081cSVladimir Oltean for (port = 0; port < SJA1105_NUM_PORTS; port++) { 696c279c726SVladimir Oltean int mtu = VLAN_ETH_FRAME_LEN + ETH_FCS_LEN; 697c279c726SVladimir Oltean 698a7cc081cSVladimir Oltean if (dsa_is_cpu_port(priv->ds, port)) 699c279c726SVladimir Oltean mtu += VLAN_HLEN; 7008aa9ebccSVladimir Oltean 701a7cc081cSVladimir Oltean policing[port].smax = 65535; /* Burst size in bytes */ 702a7cc081cSVladimir Oltean policing[port].rate = SJA1105_RATE_MBPS(1000); 703a7cc081cSVladimir Oltean policing[port].maxlen = mtu; 704a7cc081cSVladimir Oltean policing[port].partition = 0; 7058aa9ebccSVladimir Oltean } 706a7cc081cSVladimir Oltean 7078aa9ebccSVladimir Oltean return 0; 7088aa9ebccSVladimir Oltean } 7098aa9ebccSVladimir Oltean 7108aa9ebccSVladimir Oltean static int sja1105_static_config_load(struct sja1105_private *priv, 7118aa9ebccSVladimir Oltean struct sja1105_dt_port *ports) 7128aa9ebccSVladimir Oltean { 7138aa9ebccSVladimir Oltean int rc; 7148aa9ebccSVladimir Oltean 7158aa9ebccSVladimir Oltean sja1105_static_config_free(&priv->static_config); 7168aa9ebccSVladimir Oltean rc = sja1105_static_config_init(&priv->static_config, 7178aa9ebccSVladimir Oltean priv->info->static_ops, 7188aa9ebccSVladimir Oltean priv->info->device_id); 7198aa9ebccSVladimir Oltean if (rc) 7208aa9ebccSVladimir Oltean return rc; 7218aa9ebccSVladimir Oltean 7228aa9ebccSVladimir Oltean /* Build static configuration */ 7238aa9ebccSVladimir Oltean rc = sja1105_init_mac_settings(priv); 7248aa9ebccSVladimir Oltean if (rc < 0) 7258aa9ebccSVladimir Oltean return rc; 7268aa9ebccSVladimir Oltean rc = sja1105_init_mii_settings(priv, ports); 7278aa9ebccSVladimir Oltean if (rc < 0) 7288aa9ebccSVladimir Oltean return rc; 7298aa9ebccSVladimir Oltean rc = sja1105_init_static_fdb(priv); 7308aa9ebccSVladimir Oltean if (rc < 0) 7318aa9ebccSVladimir Oltean return rc; 7328aa9ebccSVladimir Oltean rc = sja1105_init_static_vlan(priv); 7338aa9ebccSVladimir Oltean if (rc < 0) 7348aa9ebccSVladimir Oltean return rc; 7358aa9ebccSVladimir Oltean rc = sja1105_init_l2_lookup_params(priv); 7368aa9ebccSVladimir Oltean if (rc < 0) 7378aa9ebccSVladimir Oltean return rc; 7388aa9ebccSVladimir Oltean rc = sja1105_init_l2_forwarding(priv); 7398aa9ebccSVladimir Oltean if (rc < 0) 7408aa9ebccSVladimir Oltean return rc; 7418aa9ebccSVladimir Oltean rc = sja1105_init_l2_forwarding_params(priv); 7428aa9ebccSVladimir Oltean if (rc < 0) 7438aa9ebccSVladimir Oltean return rc; 7448aa9ebccSVladimir Oltean rc = sja1105_init_l2_policing(priv); 7458aa9ebccSVladimir Oltean if (rc < 0) 7468aa9ebccSVladimir Oltean return rc; 7478aa9ebccSVladimir Oltean rc = sja1105_init_general_params(priv); 7488aa9ebccSVladimir Oltean if (rc < 0) 7498aa9ebccSVladimir Oltean return rc; 75079d5511cSVladimir Oltean rc = sja1105_init_avb_params(priv); 75179d5511cSVladimir Oltean if (rc < 0) 75279d5511cSVladimir Oltean return rc; 7538aa9ebccSVladimir Oltean 7548aa9ebccSVladimir Oltean /* Send initial configuration to hardware via SPI */ 7558aa9ebccSVladimir Oltean return sja1105_static_config_upload(priv); 7568aa9ebccSVladimir Oltean } 7578aa9ebccSVladimir Oltean 758f5b8631cSVladimir Oltean static int sja1105_parse_rgmii_delays(struct sja1105_private *priv, 759f5b8631cSVladimir Oltean const struct sja1105_dt_port *ports) 760f5b8631cSVladimir Oltean { 761f5b8631cSVladimir Oltean int i; 762f5b8631cSVladimir Oltean 763f5b8631cSVladimir Oltean for (i = 0; i < SJA1105_NUM_PORTS; i++) { 7649bca3a0aSOleksij Rempel if (ports[i].role == XMII_MAC) 765f5b8631cSVladimir Oltean continue; 766f5b8631cSVladimir Oltean 7679bca3a0aSOleksij Rempel if (ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_RXID || 7689bca3a0aSOleksij Rempel ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_ID) 769f5b8631cSVladimir Oltean priv->rgmii_rx_delay[i] = true; 770f5b8631cSVladimir Oltean 7719bca3a0aSOleksij Rempel if (ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_TXID || 7729bca3a0aSOleksij Rempel ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_ID) 773f5b8631cSVladimir Oltean priv->rgmii_tx_delay[i] = true; 774f5b8631cSVladimir Oltean 775f5b8631cSVladimir Oltean if ((priv->rgmii_rx_delay[i] || priv->rgmii_tx_delay[i]) && 776f5b8631cSVladimir Oltean !priv->info->setup_rgmii_delay) 777f5b8631cSVladimir Oltean return -EINVAL; 778f5b8631cSVladimir Oltean } 779f5b8631cSVladimir Oltean return 0; 780f5b8631cSVladimir Oltean } 781f5b8631cSVladimir Oltean 7828aa9ebccSVladimir Oltean static int sja1105_parse_ports_node(struct sja1105_private *priv, 7838aa9ebccSVladimir Oltean struct sja1105_dt_port *ports, 7848aa9ebccSVladimir Oltean struct device_node *ports_node) 7858aa9ebccSVladimir Oltean { 7868aa9ebccSVladimir Oltean struct device *dev = &priv->spidev->dev; 7878aa9ebccSVladimir Oltean struct device_node *child; 7888aa9ebccSVladimir Oltean 78927afe0d3SVladimir Oltean for_each_available_child_of_node(ports_node, child) { 7908aa9ebccSVladimir Oltean struct device_node *phy_node; 7910c65b2b9SAndrew Lunn phy_interface_t phy_mode; 7928aa9ebccSVladimir Oltean u32 index; 7930c65b2b9SAndrew Lunn int err; 7948aa9ebccSVladimir Oltean 7958aa9ebccSVladimir Oltean /* Get switch port number from DT */ 7968aa9ebccSVladimir Oltean if (of_property_read_u32(child, "reg", &index) < 0) { 7978aa9ebccSVladimir Oltean dev_err(dev, "Port number not defined in device tree " 7988aa9ebccSVladimir Oltean "(property \"reg\")\n"); 7997ba771e3SNishka Dasgupta of_node_put(child); 8008aa9ebccSVladimir Oltean return -ENODEV; 8018aa9ebccSVladimir Oltean } 8028aa9ebccSVladimir Oltean 8038aa9ebccSVladimir Oltean /* Get PHY mode from DT */ 8040c65b2b9SAndrew Lunn err = of_get_phy_mode(child, &phy_mode); 8050c65b2b9SAndrew Lunn if (err) { 8068aa9ebccSVladimir Oltean dev_err(dev, "Failed to read phy-mode or " 8078aa9ebccSVladimir Oltean "phy-interface-type property for port %d\n", 8088aa9ebccSVladimir Oltean index); 8097ba771e3SNishka Dasgupta of_node_put(child); 8108aa9ebccSVladimir Oltean return -ENODEV; 8118aa9ebccSVladimir Oltean } 8128aa9ebccSVladimir Oltean ports[index].phy_mode = phy_mode; 8138aa9ebccSVladimir Oltean 8148aa9ebccSVladimir Oltean phy_node = of_parse_phandle(child, "phy-handle", 0); 8158aa9ebccSVladimir Oltean if (!phy_node) { 8168aa9ebccSVladimir Oltean if (!of_phy_is_fixed_link(child)) { 8178aa9ebccSVladimir Oltean dev_err(dev, "phy-handle or fixed-link " 8188aa9ebccSVladimir Oltean "properties missing!\n"); 8197ba771e3SNishka Dasgupta of_node_put(child); 8208aa9ebccSVladimir Oltean return -ENODEV; 8218aa9ebccSVladimir Oltean } 8228aa9ebccSVladimir Oltean /* phy-handle is missing, but fixed-link isn't. 8238aa9ebccSVladimir Oltean * So it's a fixed link. Default to PHY role. 8248aa9ebccSVladimir Oltean */ 8258aa9ebccSVladimir Oltean ports[index].role = XMII_PHY; 8268aa9ebccSVladimir Oltean } else { 8278aa9ebccSVladimir Oltean /* phy-handle present => put port in MAC role */ 8288aa9ebccSVladimir Oltean ports[index].role = XMII_MAC; 8298aa9ebccSVladimir Oltean of_node_put(phy_node); 8308aa9ebccSVladimir Oltean } 8318aa9ebccSVladimir Oltean 8328aa9ebccSVladimir Oltean /* The MAC/PHY role can be overridden with explicit bindings */ 8338aa9ebccSVladimir Oltean if (of_property_read_bool(child, "sja1105,role-mac")) 8348aa9ebccSVladimir Oltean ports[index].role = XMII_MAC; 8358aa9ebccSVladimir Oltean else if (of_property_read_bool(child, "sja1105,role-phy")) 8368aa9ebccSVladimir Oltean ports[index].role = XMII_PHY; 8378aa9ebccSVladimir Oltean } 8388aa9ebccSVladimir Oltean 8398aa9ebccSVladimir Oltean return 0; 8408aa9ebccSVladimir Oltean } 8418aa9ebccSVladimir Oltean 8428aa9ebccSVladimir Oltean static int sja1105_parse_dt(struct sja1105_private *priv, 8438aa9ebccSVladimir Oltean struct sja1105_dt_port *ports) 8448aa9ebccSVladimir Oltean { 8458aa9ebccSVladimir Oltean struct device *dev = &priv->spidev->dev; 8468aa9ebccSVladimir Oltean struct device_node *switch_node = dev->of_node; 8478aa9ebccSVladimir Oltean struct device_node *ports_node; 8488aa9ebccSVladimir Oltean int rc; 8498aa9ebccSVladimir Oltean 8508aa9ebccSVladimir Oltean ports_node = of_get_child_by_name(switch_node, "ports"); 8518aa9ebccSVladimir Oltean if (!ports_node) { 8528aa9ebccSVladimir Oltean dev_err(dev, "Incorrect bindings: absent \"ports\" node\n"); 8538aa9ebccSVladimir Oltean return -ENODEV; 8548aa9ebccSVladimir Oltean } 8558aa9ebccSVladimir Oltean 8568aa9ebccSVladimir Oltean rc = sja1105_parse_ports_node(priv, ports, ports_node); 8578aa9ebccSVladimir Oltean of_node_put(ports_node); 8588aa9ebccSVladimir Oltean 8598aa9ebccSVladimir Oltean return rc; 8608aa9ebccSVladimir Oltean } 8618aa9ebccSVladimir Oltean 862ffe10e67SVladimir Oltean static int sja1105_sgmii_read(struct sja1105_private *priv, int pcs_reg) 863ffe10e67SVladimir Oltean { 864ffe10e67SVladimir Oltean const struct sja1105_regs *regs = priv->info->regs; 865ffe10e67SVladimir Oltean u32 val; 866ffe10e67SVladimir Oltean int rc; 867ffe10e67SVladimir Oltean 868ffe10e67SVladimir Oltean rc = sja1105_xfer_u32(priv, SPI_READ, regs->sgmii + pcs_reg, &val, 869ffe10e67SVladimir Oltean NULL); 870ffe10e67SVladimir Oltean if (rc < 0) 871ffe10e67SVladimir Oltean return rc; 872ffe10e67SVladimir Oltean 873ffe10e67SVladimir Oltean return val; 874ffe10e67SVladimir Oltean } 875ffe10e67SVladimir Oltean 876ffe10e67SVladimir Oltean static int sja1105_sgmii_write(struct sja1105_private *priv, int pcs_reg, 877ffe10e67SVladimir Oltean u16 pcs_val) 878ffe10e67SVladimir Oltean { 879ffe10e67SVladimir Oltean const struct sja1105_regs *regs = priv->info->regs; 880ffe10e67SVladimir Oltean u32 val = pcs_val; 881ffe10e67SVladimir Oltean int rc; 882ffe10e67SVladimir Oltean 883ffe10e67SVladimir Oltean rc = sja1105_xfer_u32(priv, SPI_WRITE, regs->sgmii + pcs_reg, &val, 884ffe10e67SVladimir Oltean NULL); 885ffe10e67SVladimir Oltean if (rc < 0) 886ffe10e67SVladimir Oltean return rc; 887ffe10e67SVladimir Oltean 888ffe10e67SVladimir Oltean return val; 889ffe10e67SVladimir Oltean } 890ffe10e67SVladimir Oltean 891ffe10e67SVladimir Oltean static void sja1105_sgmii_pcs_config(struct sja1105_private *priv, 892ffe10e67SVladimir Oltean bool an_enabled, bool an_master) 893ffe10e67SVladimir Oltean { 894ffe10e67SVladimir Oltean u16 ac = SJA1105_AC_AUTONEG_MODE_SGMII; 895ffe10e67SVladimir Oltean 896ffe10e67SVladimir Oltean /* DIGITAL_CONTROL_1: Enable vendor-specific MMD1, allow the PHY to 897ffe10e67SVladimir Oltean * stop the clock during LPI mode, make the MAC reconfigure 898ffe10e67SVladimir Oltean * autonomously after PCS autoneg is done, flush the internal FIFOs. 899ffe10e67SVladimir Oltean */ 900ffe10e67SVladimir Oltean sja1105_sgmii_write(priv, SJA1105_DC1, SJA1105_DC1_EN_VSMMD1 | 901ffe10e67SVladimir Oltean SJA1105_DC1_CLOCK_STOP_EN | 902ffe10e67SVladimir Oltean SJA1105_DC1_MAC_AUTO_SW | 903ffe10e67SVladimir Oltean SJA1105_DC1_INIT); 904ffe10e67SVladimir Oltean /* DIGITAL_CONTROL_2: No polarity inversion for TX and RX lanes */ 905ffe10e67SVladimir Oltean sja1105_sgmii_write(priv, SJA1105_DC2, SJA1105_DC2_TX_POL_INV_DISABLE); 906ffe10e67SVladimir Oltean /* AUTONEG_CONTROL: Use SGMII autoneg */ 907ffe10e67SVladimir Oltean if (an_master) 908ffe10e67SVladimir Oltean ac |= SJA1105_AC_PHY_MODE | SJA1105_AC_SGMII_LINK; 909ffe10e67SVladimir Oltean sja1105_sgmii_write(priv, SJA1105_AC, ac); 910ffe10e67SVladimir Oltean /* BASIC_CONTROL: enable in-band AN now, if requested. Otherwise, 911ffe10e67SVladimir Oltean * sja1105_sgmii_pcs_force_speed must be called later for the link 912ffe10e67SVladimir Oltean * to become operational. 913ffe10e67SVladimir Oltean */ 914ffe10e67SVladimir Oltean if (an_enabled) 915ffe10e67SVladimir Oltean sja1105_sgmii_write(priv, MII_BMCR, 916ffe10e67SVladimir Oltean BMCR_ANENABLE | BMCR_ANRESTART); 917ffe10e67SVladimir Oltean } 918ffe10e67SVladimir Oltean 919ffe10e67SVladimir Oltean static void sja1105_sgmii_pcs_force_speed(struct sja1105_private *priv, 920ffe10e67SVladimir Oltean int speed) 921ffe10e67SVladimir Oltean { 922ffe10e67SVladimir Oltean int pcs_speed; 923ffe10e67SVladimir Oltean 924ffe10e67SVladimir Oltean switch (speed) { 925ffe10e67SVladimir Oltean case SPEED_1000: 926ffe10e67SVladimir Oltean pcs_speed = BMCR_SPEED1000; 927ffe10e67SVladimir Oltean break; 928ffe10e67SVladimir Oltean case SPEED_100: 929ffe10e67SVladimir Oltean pcs_speed = BMCR_SPEED100; 930ffe10e67SVladimir Oltean break; 931ffe10e67SVladimir Oltean case SPEED_10: 932ffe10e67SVladimir Oltean pcs_speed = BMCR_SPEED10; 933ffe10e67SVladimir Oltean break; 934ffe10e67SVladimir Oltean default: 935ffe10e67SVladimir Oltean dev_err(priv->ds->dev, "Invalid speed %d\n", speed); 936ffe10e67SVladimir Oltean return; 937ffe10e67SVladimir Oltean } 938ffe10e67SVladimir Oltean sja1105_sgmii_write(priv, MII_BMCR, pcs_speed | BMCR_FULLDPLX); 939ffe10e67SVladimir Oltean } 940ffe10e67SVladimir Oltean 941c44d0535SVladimir Oltean /* Convert link speed from SJA1105 to ethtool encoding */ 9428aa9ebccSVladimir Oltean static int sja1105_speed[] = { 943c44d0535SVladimir Oltean [SJA1105_SPEED_AUTO] = SPEED_UNKNOWN, 944c44d0535SVladimir Oltean [SJA1105_SPEED_10MBPS] = SPEED_10, 945c44d0535SVladimir Oltean [SJA1105_SPEED_100MBPS] = SPEED_100, 946c44d0535SVladimir Oltean [SJA1105_SPEED_1000MBPS] = SPEED_1000, 9478aa9ebccSVladimir Oltean }; 9488aa9ebccSVladimir Oltean 9498400cff6SVladimir Oltean /* Set link speed in the MAC configuration for a specific port. */ 9508aa9ebccSVladimir Oltean static int sja1105_adjust_port_config(struct sja1105_private *priv, int port, 9518400cff6SVladimir Oltean int speed_mbps) 9528aa9ebccSVladimir Oltean { 9538aa9ebccSVladimir Oltean struct sja1105_xmii_params_entry *mii; 9548aa9ebccSVladimir Oltean struct sja1105_mac_config_entry *mac; 9558aa9ebccSVladimir Oltean struct device *dev = priv->ds->dev; 9568aa9ebccSVladimir Oltean sja1105_phy_interface_t phy_mode; 9578aa9ebccSVladimir Oltean sja1105_speed_t speed; 9588aa9ebccSVladimir Oltean int rc; 9598aa9ebccSVladimir Oltean 9608400cff6SVladimir Oltean /* On P/Q/R/S, one can read from the device via the MAC reconfiguration 9618400cff6SVladimir Oltean * tables. On E/T, MAC reconfig tables are not readable, only writable. 9628400cff6SVladimir Oltean * We have to *know* what the MAC looks like. For the sake of keeping 9638400cff6SVladimir Oltean * the code common, we'll use the static configuration tables as a 9648400cff6SVladimir Oltean * reasonable approximation for both E/T and P/Q/R/S. 9658400cff6SVladimir Oltean */ 9668aa9ebccSVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 9678400cff6SVladimir Oltean mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries; 9688aa9ebccSVladimir Oltean 969f4cfcfbdSVladimir Oltean switch (speed_mbps) { 970c44d0535SVladimir Oltean case SPEED_UNKNOWN: 971a979a0abSVladimir Oltean /* PHYLINK called sja1105_mac_config() to inform us about 972a979a0abSVladimir Oltean * the state->interface, but AN has not completed and the 973a979a0abSVladimir Oltean * speed is not yet valid. UM10944.pdf says that setting 974a979a0abSVladimir Oltean * SJA1105_SPEED_AUTO at runtime disables the port, so that is 975a979a0abSVladimir Oltean * ok for power consumption in case AN will never complete - 976a979a0abSVladimir Oltean * otherwise PHYLINK should come back with a new update. 977a979a0abSVladimir Oltean */ 978f4cfcfbdSVladimir Oltean speed = SJA1105_SPEED_AUTO; 979f4cfcfbdSVladimir Oltean break; 980c44d0535SVladimir Oltean case SPEED_10: 981f4cfcfbdSVladimir Oltean speed = SJA1105_SPEED_10MBPS; 982f4cfcfbdSVladimir Oltean break; 983c44d0535SVladimir Oltean case SPEED_100: 984f4cfcfbdSVladimir Oltean speed = SJA1105_SPEED_100MBPS; 985f4cfcfbdSVladimir Oltean break; 986c44d0535SVladimir Oltean case SPEED_1000: 987f4cfcfbdSVladimir Oltean speed = SJA1105_SPEED_1000MBPS; 988f4cfcfbdSVladimir Oltean break; 989f4cfcfbdSVladimir Oltean default: 9908aa9ebccSVladimir Oltean dev_err(dev, "Invalid speed %iMbps\n", speed_mbps); 9918aa9ebccSVladimir Oltean return -EINVAL; 9928aa9ebccSVladimir Oltean } 9938aa9ebccSVladimir Oltean 9948400cff6SVladimir Oltean /* Overwrite SJA1105_SPEED_AUTO from the static MAC configuration 9958400cff6SVladimir Oltean * table, since this will be used for the clocking setup, and we no 9968400cff6SVladimir Oltean * longer need to store it in the static config (already told hardware 9978400cff6SVladimir Oltean * we want auto during upload phase). 998ffe10e67SVladimir Oltean * Actually for the SGMII port, the MAC is fixed at 1 Gbps and 999ffe10e67SVladimir Oltean * we need to configure the PCS only (if even that). 10008aa9ebccSVladimir Oltean */ 1001ffe10e67SVladimir Oltean if (sja1105_supports_sgmii(priv, port)) 1002ffe10e67SVladimir Oltean mac[port].speed = SJA1105_SPEED_1000MBPS; 1003ffe10e67SVladimir Oltean else 10048aa9ebccSVladimir Oltean mac[port].speed = speed; 10058aa9ebccSVladimir Oltean 10068aa9ebccSVladimir Oltean /* Write to the dynamic reconfiguration tables */ 10078400cff6SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, 10088400cff6SVladimir Oltean &mac[port], true); 10098aa9ebccSVladimir Oltean if (rc < 0) { 10108aa9ebccSVladimir Oltean dev_err(dev, "Failed to write MAC config: %d\n", rc); 10118aa9ebccSVladimir Oltean return rc; 10128aa9ebccSVladimir Oltean } 10138aa9ebccSVladimir Oltean 10148aa9ebccSVladimir Oltean /* Reconfigure the PLLs for the RGMII interfaces (required 125 MHz at 10158aa9ebccSVladimir Oltean * gigabit, 25 MHz at 100 Mbps and 2.5 MHz at 10 Mbps). For MII and 10168aa9ebccSVladimir Oltean * RMII no change of the clock setup is required. Actually, changing 10178aa9ebccSVladimir Oltean * the clock setup does interrupt the clock signal for a certain time 10188aa9ebccSVladimir Oltean * which causes trouble for all PHYs relying on this signal. 10198aa9ebccSVladimir Oltean */ 10208aa9ebccSVladimir Oltean phy_mode = mii->xmii_mode[port]; 10218aa9ebccSVladimir Oltean if (phy_mode != XMII_MODE_RGMII) 10228aa9ebccSVladimir Oltean return 0; 10238aa9ebccSVladimir Oltean 10248aa9ebccSVladimir Oltean return sja1105_clocking_setup_port(priv, port); 10258aa9ebccSVladimir Oltean } 10268aa9ebccSVladimir Oltean 102739710229SVladimir Oltean /* The SJA1105 MAC programming model is through the static config (the xMII 102839710229SVladimir Oltean * Mode table cannot be dynamically reconfigured), and we have to program 102939710229SVladimir Oltean * that early (earlier than PHYLINK calls us, anyway). 103039710229SVladimir Oltean * So just error out in case the connected PHY attempts to change the initial 103139710229SVladimir Oltean * system interface MII protocol from what is defined in the DT, at least for 103239710229SVladimir Oltean * now. 103339710229SVladimir Oltean */ 103439710229SVladimir Oltean static bool sja1105_phy_mode_mismatch(struct sja1105_private *priv, int port, 103539710229SVladimir Oltean phy_interface_t interface) 103639710229SVladimir Oltean { 103739710229SVladimir Oltean struct sja1105_xmii_params_entry *mii; 103839710229SVladimir Oltean sja1105_phy_interface_t phy_mode; 103939710229SVladimir Oltean 104039710229SVladimir Oltean mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries; 104139710229SVladimir Oltean phy_mode = mii->xmii_mode[port]; 104239710229SVladimir Oltean 104339710229SVladimir Oltean switch (interface) { 104439710229SVladimir Oltean case PHY_INTERFACE_MODE_MII: 104539710229SVladimir Oltean return (phy_mode != XMII_MODE_MII); 104639710229SVladimir Oltean case PHY_INTERFACE_MODE_RMII: 104739710229SVladimir Oltean return (phy_mode != XMII_MODE_RMII); 104839710229SVladimir Oltean case PHY_INTERFACE_MODE_RGMII: 104939710229SVladimir Oltean case PHY_INTERFACE_MODE_RGMII_ID: 105039710229SVladimir Oltean case PHY_INTERFACE_MODE_RGMII_RXID: 105139710229SVladimir Oltean case PHY_INTERFACE_MODE_RGMII_TXID: 105239710229SVladimir Oltean return (phy_mode != XMII_MODE_RGMII); 1053ffe10e67SVladimir Oltean case PHY_INTERFACE_MODE_SGMII: 1054ffe10e67SVladimir Oltean return (phy_mode != XMII_MODE_SGMII); 105539710229SVladimir Oltean default: 105639710229SVladimir Oltean return true; 105739710229SVladimir Oltean } 105839710229SVladimir Oltean } 105939710229SVladimir Oltean 1060af7cd036SVladimir Oltean static void sja1105_mac_config(struct dsa_switch *ds, int port, 1061ffe10e67SVladimir Oltean unsigned int mode, 1062af7cd036SVladimir Oltean const struct phylink_link_state *state) 10638aa9ebccSVladimir Oltean { 10648aa9ebccSVladimir Oltean struct sja1105_private *priv = ds->priv; 1065ffe10e67SVladimir Oltean bool is_sgmii = sja1105_supports_sgmii(priv, port); 10668aa9ebccSVladimir Oltean 1067ec8582d1SVladimir Oltean if (sja1105_phy_mode_mismatch(priv, port, state->interface)) { 1068ec8582d1SVladimir Oltean dev_err(ds->dev, "Changing PHY mode to %s not supported!\n", 1069ec8582d1SVladimir Oltean phy_modes(state->interface)); 107039710229SVladimir Oltean return; 1071ec8582d1SVladimir Oltean } 107239710229SVladimir Oltean 1073ffe10e67SVladimir Oltean if (phylink_autoneg_inband(mode) && !is_sgmii) { 10749f971573SVladimir Oltean dev_err(ds->dev, "In-band AN not supported!\n"); 10759f971573SVladimir Oltean return; 10769f971573SVladimir Oltean } 1077ffe10e67SVladimir Oltean 1078ffe10e67SVladimir Oltean if (is_sgmii) 1079ffe10e67SVladimir Oltean sja1105_sgmii_pcs_config(priv, phylink_autoneg_inband(mode), 1080ffe10e67SVladimir Oltean false); 10818400cff6SVladimir Oltean } 10828400cff6SVladimir Oltean 10838400cff6SVladimir Oltean static void sja1105_mac_link_down(struct dsa_switch *ds, int port, 10848400cff6SVladimir Oltean unsigned int mode, 10858400cff6SVladimir Oltean phy_interface_t interface) 10868400cff6SVladimir Oltean { 10878400cff6SVladimir Oltean sja1105_inhibit_tx(ds->priv, BIT(port), true); 10888400cff6SVladimir Oltean } 10898400cff6SVladimir Oltean 10908400cff6SVladimir Oltean static void sja1105_mac_link_up(struct dsa_switch *ds, int port, 10918400cff6SVladimir Oltean unsigned int mode, 10928400cff6SVladimir Oltean phy_interface_t interface, 10935b502a7bSRussell King struct phy_device *phydev, 10945b502a7bSRussell King int speed, int duplex, 10955b502a7bSRussell King bool tx_pause, bool rx_pause) 10968400cff6SVladimir Oltean { 1097ec8582d1SVladimir Oltean struct sja1105_private *priv = ds->priv; 1098ec8582d1SVladimir Oltean 1099ec8582d1SVladimir Oltean sja1105_adjust_port_config(priv, port, speed); 1100ec8582d1SVladimir Oltean 1101ffe10e67SVladimir Oltean if (sja1105_supports_sgmii(priv, port) && !phylink_autoneg_inband(mode)) 1102ffe10e67SVladimir Oltean sja1105_sgmii_pcs_force_speed(priv, speed); 1103ffe10e67SVladimir Oltean 1104ec8582d1SVladimir Oltean sja1105_inhibit_tx(priv, BIT(port), false); 11058aa9ebccSVladimir Oltean } 11068aa9ebccSVladimir Oltean 1107ad9f299aSVladimir Oltean static void sja1105_phylink_validate(struct dsa_switch *ds, int port, 1108ad9f299aSVladimir Oltean unsigned long *supported, 1109ad9f299aSVladimir Oltean struct phylink_link_state *state) 1110ad9f299aSVladimir Oltean { 1111ad9f299aSVladimir Oltean /* Construct a new mask which exhaustively contains all link features 1112ad9f299aSVladimir Oltean * supported by the MAC, and then apply that (logical AND) to what will 1113ad9f299aSVladimir Oltean * be sent to the PHY for "marketing". 1114ad9f299aSVladimir Oltean */ 1115ad9f299aSVladimir Oltean __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, }; 1116ad9f299aSVladimir Oltean struct sja1105_private *priv = ds->priv; 1117ad9f299aSVladimir Oltean struct sja1105_xmii_params_entry *mii; 1118ad9f299aSVladimir Oltean 1119ad9f299aSVladimir Oltean mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries; 1120ad9f299aSVladimir Oltean 112139710229SVladimir Oltean /* include/linux/phylink.h says: 112239710229SVladimir Oltean * When @state->interface is %PHY_INTERFACE_MODE_NA, phylink 112339710229SVladimir Oltean * expects the MAC driver to return all supported link modes. 112439710229SVladimir Oltean */ 112539710229SVladimir Oltean if (state->interface != PHY_INTERFACE_MODE_NA && 112639710229SVladimir Oltean sja1105_phy_mode_mismatch(priv, port, state->interface)) { 112739710229SVladimir Oltean bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS); 112839710229SVladimir Oltean return; 112939710229SVladimir Oltean } 113039710229SVladimir Oltean 1131ad9f299aSVladimir Oltean /* The MAC does not support pause frames, and also doesn't 1132ad9f299aSVladimir Oltean * support half-duplex traffic modes. 1133ad9f299aSVladimir Oltean */ 1134ad9f299aSVladimir Oltean phylink_set(mask, Autoneg); 1135ad9f299aSVladimir Oltean phylink_set(mask, MII); 1136ad9f299aSVladimir Oltean phylink_set(mask, 10baseT_Full); 1137ad9f299aSVladimir Oltean phylink_set(mask, 100baseT_Full); 1138ca68e138SOleksij Rempel phylink_set(mask, 100baseT1_Full); 1139ffe10e67SVladimir Oltean if (mii->xmii_mode[port] == XMII_MODE_RGMII || 1140ffe10e67SVladimir Oltean mii->xmii_mode[port] == XMII_MODE_SGMII) 1141ad9f299aSVladimir Oltean phylink_set(mask, 1000baseT_Full); 1142ad9f299aSVladimir Oltean 1143ad9f299aSVladimir Oltean bitmap_and(supported, supported, mask, __ETHTOOL_LINK_MODE_MASK_NBITS); 1144ad9f299aSVladimir Oltean bitmap_and(state->advertising, state->advertising, mask, 1145ad9f299aSVladimir Oltean __ETHTOOL_LINK_MODE_MASK_NBITS); 1146ad9f299aSVladimir Oltean } 1147ad9f299aSVladimir Oltean 1148ffe10e67SVladimir Oltean static int sja1105_mac_pcs_get_state(struct dsa_switch *ds, int port, 1149ffe10e67SVladimir Oltean struct phylink_link_state *state) 1150ffe10e67SVladimir Oltean { 1151ffe10e67SVladimir Oltean struct sja1105_private *priv = ds->priv; 1152ffe10e67SVladimir Oltean int ais; 1153ffe10e67SVladimir Oltean 1154ffe10e67SVladimir Oltean /* Read the vendor-specific AUTONEG_INTR_STATUS register */ 1155ffe10e67SVladimir Oltean ais = sja1105_sgmii_read(priv, SJA1105_AIS); 1156ffe10e67SVladimir Oltean if (ais < 0) 1157ffe10e67SVladimir Oltean return ais; 1158ffe10e67SVladimir Oltean 1159ffe10e67SVladimir Oltean switch (SJA1105_AIS_SPEED(ais)) { 1160ffe10e67SVladimir Oltean case 0: 1161ffe10e67SVladimir Oltean state->speed = SPEED_10; 1162ffe10e67SVladimir Oltean break; 1163ffe10e67SVladimir Oltean case 1: 1164ffe10e67SVladimir Oltean state->speed = SPEED_100; 1165ffe10e67SVladimir Oltean break; 1166ffe10e67SVladimir Oltean case 2: 1167ffe10e67SVladimir Oltean state->speed = SPEED_1000; 1168ffe10e67SVladimir Oltean break; 1169ffe10e67SVladimir Oltean default: 1170ffe10e67SVladimir Oltean dev_err(ds->dev, "Invalid SGMII PCS speed %lu\n", 1171ffe10e67SVladimir Oltean SJA1105_AIS_SPEED(ais)); 1172ffe10e67SVladimir Oltean } 1173ffe10e67SVladimir Oltean state->duplex = SJA1105_AIS_DUPLEX_MODE(ais); 1174ffe10e67SVladimir Oltean state->an_complete = SJA1105_AIS_COMPLETE(ais); 1175ffe10e67SVladimir Oltean state->link = SJA1105_AIS_LINK_STATUS(ais); 1176ffe10e67SVladimir Oltean 1177ffe10e67SVladimir Oltean return 0; 1178ffe10e67SVladimir Oltean } 1179ffe10e67SVladimir Oltean 118060f6053fSVladimir Oltean static int 118160f6053fSVladimir Oltean sja1105_find_static_fdb_entry(struct sja1105_private *priv, int port, 118260f6053fSVladimir Oltean const struct sja1105_l2_lookup_entry *requested) 118360f6053fSVladimir Oltean { 118460f6053fSVladimir Oltean struct sja1105_l2_lookup_entry *l2_lookup; 118560f6053fSVladimir Oltean struct sja1105_table *table; 118660f6053fSVladimir Oltean int i; 118760f6053fSVladimir Oltean 118860f6053fSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; 118960f6053fSVladimir Oltean l2_lookup = table->entries; 119060f6053fSVladimir Oltean 119160f6053fSVladimir Oltean for (i = 0; i < table->entry_count; i++) 119260f6053fSVladimir Oltean if (l2_lookup[i].macaddr == requested->macaddr && 119360f6053fSVladimir Oltean l2_lookup[i].vlanid == requested->vlanid && 119460f6053fSVladimir Oltean l2_lookup[i].destports & BIT(port)) 119560f6053fSVladimir Oltean return i; 119660f6053fSVladimir Oltean 119760f6053fSVladimir Oltean return -1; 119860f6053fSVladimir Oltean } 119960f6053fSVladimir Oltean 120060f6053fSVladimir Oltean /* We want FDB entries added statically through the bridge command to persist 120160f6053fSVladimir Oltean * across switch resets, which are a common thing during normal SJA1105 120260f6053fSVladimir Oltean * operation. So we have to back them up in the static configuration tables 120360f6053fSVladimir Oltean * and hence apply them on next static config upload... yay! 120460f6053fSVladimir Oltean */ 120560f6053fSVladimir Oltean static int 120660f6053fSVladimir Oltean sja1105_static_fdb_change(struct sja1105_private *priv, int port, 120760f6053fSVladimir Oltean const struct sja1105_l2_lookup_entry *requested, 120860f6053fSVladimir Oltean bool keep) 120960f6053fSVladimir Oltean { 121060f6053fSVladimir Oltean struct sja1105_l2_lookup_entry *l2_lookup; 121160f6053fSVladimir Oltean struct sja1105_table *table; 121260f6053fSVladimir Oltean int rc, match; 121360f6053fSVladimir Oltean 121460f6053fSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; 121560f6053fSVladimir Oltean 121660f6053fSVladimir Oltean match = sja1105_find_static_fdb_entry(priv, port, requested); 121760f6053fSVladimir Oltean if (match < 0) { 121860f6053fSVladimir Oltean /* Can't delete a missing entry. */ 121960f6053fSVladimir Oltean if (!keep) 122060f6053fSVladimir Oltean return 0; 122160f6053fSVladimir Oltean 122260f6053fSVladimir Oltean /* No match => new entry */ 122360f6053fSVladimir Oltean rc = sja1105_table_resize(table, table->entry_count + 1); 122460f6053fSVladimir Oltean if (rc) 122560f6053fSVladimir Oltean return rc; 122660f6053fSVladimir Oltean 122760f6053fSVladimir Oltean match = table->entry_count - 1; 122860f6053fSVladimir Oltean } 122960f6053fSVladimir Oltean 123060f6053fSVladimir Oltean /* Assign pointer after the resize (it may be new memory) */ 123160f6053fSVladimir Oltean l2_lookup = table->entries; 123260f6053fSVladimir Oltean 123360f6053fSVladimir Oltean /* We have a match. 123460f6053fSVladimir Oltean * If the job was to add this FDB entry, it's already done (mostly 123560f6053fSVladimir Oltean * anyway, since the port forwarding mask may have changed, case in 123660f6053fSVladimir Oltean * which we update it). 123760f6053fSVladimir Oltean * Otherwise we have to delete it. 123860f6053fSVladimir Oltean */ 123960f6053fSVladimir Oltean if (keep) { 124060f6053fSVladimir Oltean l2_lookup[match] = *requested; 124160f6053fSVladimir Oltean return 0; 124260f6053fSVladimir Oltean } 124360f6053fSVladimir Oltean 124460f6053fSVladimir Oltean /* To remove, the strategy is to overwrite the element with 124560f6053fSVladimir Oltean * the last one, and then reduce the array size by 1 124660f6053fSVladimir Oltean */ 124760f6053fSVladimir Oltean l2_lookup[match] = l2_lookup[table->entry_count - 1]; 124860f6053fSVladimir Oltean return sja1105_table_resize(table, table->entry_count - 1); 124960f6053fSVladimir Oltean } 125060f6053fSVladimir Oltean 1251291d1e72SVladimir Oltean /* First-generation switches have a 4-way set associative TCAM that 1252291d1e72SVladimir Oltean * holds the FDB entries. An FDB index spans from 0 to 1023 and is comprised of 1253291d1e72SVladimir Oltean * a "bin" (grouping of 4 entries) and a "way" (an entry within a bin). 1254291d1e72SVladimir Oltean * For the placement of a newly learnt FDB entry, the switch selects the bin 1255291d1e72SVladimir Oltean * based on a hash function, and the way within that bin incrementally. 1256291d1e72SVladimir Oltean */ 125709c1b412SVladimir Oltean static int sja1105et_fdb_index(int bin, int way) 1258291d1e72SVladimir Oltean { 1259291d1e72SVladimir Oltean return bin * SJA1105ET_FDB_BIN_SIZE + way; 1260291d1e72SVladimir Oltean } 1261291d1e72SVladimir Oltean 12629dfa6911SVladimir Oltean static int sja1105et_is_fdb_entry_in_bin(struct sja1105_private *priv, int bin, 1263291d1e72SVladimir Oltean const u8 *addr, u16 vid, 1264291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry *match, 1265291d1e72SVladimir Oltean int *last_unused) 1266291d1e72SVladimir Oltean { 1267291d1e72SVladimir Oltean int way; 1268291d1e72SVladimir Oltean 1269291d1e72SVladimir Oltean for (way = 0; way < SJA1105ET_FDB_BIN_SIZE; way++) { 1270291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 1271291d1e72SVladimir Oltean int index = sja1105et_fdb_index(bin, way); 1272291d1e72SVladimir Oltean 1273291d1e72SVladimir Oltean /* Skip unused entries, optionally marking them 1274291d1e72SVladimir Oltean * into the return value 1275291d1e72SVladimir Oltean */ 1276291d1e72SVladimir Oltean if (sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 1277291d1e72SVladimir Oltean index, &l2_lookup)) { 1278291d1e72SVladimir Oltean if (last_unused) 1279291d1e72SVladimir Oltean *last_unused = way; 1280291d1e72SVladimir Oltean continue; 1281291d1e72SVladimir Oltean } 1282291d1e72SVladimir Oltean 1283291d1e72SVladimir Oltean if (l2_lookup.macaddr == ether_addr_to_u64(addr) && 1284291d1e72SVladimir Oltean l2_lookup.vlanid == vid) { 1285291d1e72SVladimir Oltean if (match) 1286291d1e72SVladimir Oltean *match = l2_lookup; 1287291d1e72SVladimir Oltean return way; 1288291d1e72SVladimir Oltean } 1289291d1e72SVladimir Oltean } 1290291d1e72SVladimir Oltean /* Return an invalid entry index if not found */ 1291291d1e72SVladimir Oltean return -1; 1292291d1e72SVladimir Oltean } 1293291d1e72SVladimir Oltean 12949dfa6911SVladimir Oltean int sja1105et_fdb_add(struct dsa_switch *ds, int port, 1295291d1e72SVladimir Oltean const unsigned char *addr, u16 vid) 1296291d1e72SVladimir Oltean { 1297291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 1298291d1e72SVladimir Oltean struct sja1105_private *priv = ds->priv; 1299291d1e72SVladimir Oltean struct device *dev = ds->dev; 1300291d1e72SVladimir Oltean int last_unused = -1; 130160f6053fSVladimir Oltean int bin, way, rc; 1302291d1e72SVladimir Oltean 13039dfa6911SVladimir Oltean bin = sja1105et_fdb_hash(priv, addr, vid); 1304291d1e72SVladimir Oltean 13059dfa6911SVladimir Oltean way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid, 1306291d1e72SVladimir Oltean &l2_lookup, &last_unused); 1307291d1e72SVladimir Oltean if (way >= 0) { 1308291d1e72SVladimir Oltean /* We have an FDB entry. Is our port in the destination 1309291d1e72SVladimir Oltean * mask? If yes, we need to do nothing. If not, we need 1310291d1e72SVladimir Oltean * to rewrite the entry by adding this port to it. 1311291d1e72SVladimir Oltean */ 1312291d1e72SVladimir Oltean if (l2_lookup.destports & BIT(port)) 1313291d1e72SVladimir Oltean return 0; 1314291d1e72SVladimir Oltean l2_lookup.destports |= BIT(port); 1315291d1e72SVladimir Oltean } else { 1316291d1e72SVladimir Oltean int index = sja1105et_fdb_index(bin, way); 1317291d1e72SVladimir Oltean 1318291d1e72SVladimir Oltean /* We don't have an FDB entry. We construct a new one and 1319291d1e72SVladimir Oltean * try to find a place for it within the FDB table. 1320291d1e72SVladimir Oltean */ 1321291d1e72SVladimir Oltean l2_lookup.macaddr = ether_addr_to_u64(addr); 1322291d1e72SVladimir Oltean l2_lookup.destports = BIT(port); 1323291d1e72SVladimir Oltean l2_lookup.vlanid = vid; 1324291d1e72SVladimir Oltean 1325291d1e72SVladimir Oltean if (last_unused >= 0) { 1326291d1e72SVladimir Oltean way = last_unused; 1327291d1e72SVladimir Oltean } else { 1328291d1e72SVladimir Oltean /* Bin is full, need to evict somebody. 1329291d1e72SVladimir Oltean * Choose victim at random. If you get these messages 1330291d1e72SVladimir Oltean * often, you may need to consider changing the 1331291d1e72SVladimir Oltean * distribution function: 1332291d1e72SVladimir Oltean * static_config[BLK_IDX_L2_LOOKUP_PARAMS].entries->poly 1333291d1e72SVladimir Oltean */ 1334291d1e72SVladimir Oltean get_random_bytes(&way, sizeof(u8)); 1335291d1e72SVladimir Oltean way %= SJA1105ET_FDB_BIN_SIZE; 1336291d1e72SVladimir Oltean dev_warn(dev, "Warning, FDB bin %d full while adding entry for %pM. Evicting entry %u.\n", 1337291d1e72SVladimir Oltean bin, addr, way); 1338291d1e72SVladimir Oltean /* Evict entry */ 1339291d1e72SVladimir Oltean sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 1340291d1e72SVladimir Oltean index, NULL, false); 1341291d1e72SVladimir Oltean } 1342291d1e72SVladimir Oltean } 1343291d1e72SVladimir Oltean l2_lookup.index = sja1105et_fdb_index(bin, way); 1344291d1e72SVladimir Oltean 134560f6053fSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 1346291d1e72SVladimir Oltean l2_lookup.index, &l2_lookup, 1347291d1e72SVladimir Oltean true); 134860f6053fSVladimir Oltean if (rc < 0) 134960f6053fSVladimir Oltean return rc; 135060f6053fSVladimir Oltean 135160f6053fSVladimir Oltean return sja1105_static_fdb_change(priv, port, &l2_lookup, true); 1352291d1e72SVladimir Oltean } 1353291d1e72SVladimir Oltean 13549dfa6911SVladimir Oltean int sja1105et_fdb_del(struct dsa_switch *ds, int port, 1355291d1e72SVladimir Oltean const unsigned char *addr, u16 vid) 1356291d1e72SVladimir Oltean { 1357291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 1358291d1e72SVladimir Oltean struct sja1105_private *priv = ds->priv; 135960f6053fSVladimir Oltean int index, bin, way, rc; 1360291d1e72SVladimir Oltean bool keep; 1361291d1e72SVladimir Oltean 13629dfa6911SVladimir Oltean bin = sja1105et_fdb_hash(priv, addr, vid); 13639dfa6911SVladimir Oltean way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid, 1364291d1e72SVladimir Oltean &l2_lookup, NULL); 1365291d1e72SVladimir Oltean if (way < 0) 1366291d1e72SVladimir Oltean return 0; 1367291d1e72SVladimir Oltean index = sja1105et_fdb_index(bin, way); 1368291d1e72SVladimir Oltean 1369291d1e72SVladimir Oltean /* We have an FDB entry. Is our port in the destination mask? If yes, 1370291d1e72SVladimir Oltean * we need to remove it. If the resulting port mask becomes empty, we 1371291d1e72SVladimir Oltean * need to completely evict the FDB entry. 1372291d1e72SVladimir Oltean * Otherwise we just write it back. 1373291d1e72SVladimir Oltean */ 1374291d1e72SVladimir Oltean l2_lookup.destports &= ~BIT(port); 13757752e937SVladimir Oltean 1376291d1e72SVladimir Oltean if (l2_lookup.destports) 1377291d1e72SVladimir Oltean keep = true; 1378291d1e72SVladimir Oltean else 1379291d1e72SVladimir Oltean keep = false; 1380291d1e72SVladimir Oltean 138160f6053fSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 1382291d1e72SVladimir Oltean index, &l2_lookup, keep); 138360f6053fSVladimir Oltean if (rc < 0) 138460f6053fSVladimir Oltean return rc; 138560f6053fSVladimir Oltean 138660f6053fSVladimir Oltean return sja1105_static_fdb_change(priv, port, &l2_lookup, keep); 1387291d1e72SVladimir Oltean } 1388291d1e72SVladimir Oltean 13899dfa6911SVladimir Oltean int sja1105pqrs_fdb_add(struct dsa_switch *ds, int port, 13909dfa6911SVladimir Oltean const unsigned char *addr, u16 vid) 13919dfa6911SVladimir Oltean { 13921da73821SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 13931da73821SVladimir Oltean struct sja1105_private *priv = ds->priv; 13941da73821SVladimir Oltean int rc, i; 13951da73821SVladimir Oltean 13961da73821SVladimir Oltean /* Search for an existing entry in the FDB table */ 13971da73821SVladimir Oltean l2_lookup.macaddr = ether_addr_to_u64(addr); 13981da73821SVladimir Oltean l2_lookup.vlanid = vid; 13991da73821SVladimir Oltean l2_lookup.iotag = SJA1105_S_TAG; 14001da73821SVladimir Oltean l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0); 14017f14937fSVladimir Oltean if (priv->vlan_state != SJA1105_VLAN_UNAWARE) { 14021da73821SVladimir Oltean l2_lookup.mask_vlanid = VLAN_VID_MASK; 14031da73821SVladimir Oltean l2_lookup.mask_iotag = BIT(0); 14046d7c7d94SVladimir Oltean } else { 14056d7c7d94SVladimir Oltean l2_lookup.mask_vlanid = 0; 14066d7c7d94SVladimir Oltean l2_lookup.mask_iotag = 0; 14076d7c7d94SVladimir Oltean } 14081da73821SVladimir Oltean l2_lookup.destports = BIT(port); 14091da73821SVladimir Oltean 14101da73821SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 14111da73821SVladimir Oltean SJA1105_SEARCH, &l2_lookup); 14121da73821SVladimir Oltean if (rc == 0) { 14131da73821SVladimir Oltean /* Found and this port is already in the entry's 14141da73821SVladimir Oltean * port mask => job done 14151da73821SVladimir Oltean */ 14161da73821SVladimir Oltean if (l2_lookup.destports & BIT(port)) 14171da73821SVladimir Oltean return 0; 14181da73821SVladimir Oltean /* l2_lookup.index is populated by the switch in case it 14191da73821SVladimir Oltean * found something. 14201da73821SVladimir Oltean */ 14211da73821SVladimir Oltean l2_lookup.destports |= BIT(port); 14221da73821SVladimir Oltean goto skip_finding_an_index; 14231da73821SVladimir Oltean } 14241da73821SVladimir Oltean 14251da73821SVladimir Oltean /* Not found, so try to find an unused spot in the FDB. 14261da73821SVladimir Oltean * This is slightly inefficient because the strategy is knock-knock at 14271da73821SVladimir Oltean * every possible position from 0 to 1023. 14281da73821SVladimir Oltean */ 14291da73821SVladimir Oltean for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) { 14301da73821SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 14311da73821SVladimir Oltean i, NULL); 14321da73821SVladimir Oltean if (rc < 0) 14331da73821SVladimir Oltean break; 14341da73821SVladimir Oltean } 14351da73821SVladimir Oltean if (i == SJA1105_MAX_L2_LOOKUP_COUNT) { 14361da73821SVladimir Oltean dev_err(ds->dev, "FDB is full, cannot add entry.\n"); 14371da73821SVladimir Oltean return -EINVAL; 14381da73821SVladimir Oltean } 143917ae6555SVladimir Oltean l2_lookup.lockeds = true; 14401da73821SVladimir Oltean l2_lookup.index = i; 14411da73821SVladimir Oltean 14421da73821SVladimir Oltean skip_finding_an_index: 144360f6053fSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 14441da73821SVladimir Oltean l2_lookup.index, &l2_lookup, 14451da73821SVladimir Oltean true); 144660f6053fSVladimir Oltean if (rc < 0) 144760f6053fSVladimir Oltean return rc; 144860f6053fSVladimir Oltean 144960f6053fSVladimir Oltean return sja1105_static_fdb_change(priv, port, &l2_lookup, true); 14509dfa6911SVladimir Oltean } 14519dfa6911SVladimir Oltean 14529dfa6911SVladimir Oltean int sja1105pqrs_fdb_del(struct dsa_switch *ds, int port, 14539dfa6911SVladimir Oltean const unsigned char *addr, u16 vid) 14549dfa6911SVladimir Oltean { 14551da73821SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 14561da73821SVladimir Oltean struct sja1105_private *priv = ds->priv; 14571da73821SVladimir Oltean bool keep; 14581da73821SVladimir Oltean int rc; 14591da73821SVladimir Oltean 14601da73821SVladimir Oltean l2_lookup.macaddr = ether_addr_to_u64(addr); 14611da73821SVladimir Oltean l2_lookup.vlanid = vid; 14621da73821SVladimir Oltean l2_lookup.iotag = SJA1105_S_TAG; 14631da73821SVladimir Oltean l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0); 14647f14937fSVladimir Oltean if (priv->vlan_state != SJA1105_VLAN_UNAWARE) { 14651da73821SVladimir Oltean l2_lookup.mask_vlanid = VLAN_VID_MASK; 14661da73821SVladimir Oltean l2_lookup.mask_iotag = BIT(0); 14676d7c7d94SVladimir Oltean } else { 14686d7c7d94SVladimir Oltean l2_lookup.mask_vlanid = 0; 14696d7c7d94SVladimir Oltean l2_lookup.mask_iotag = 0; 14706d7c7d94SVladimir Oltean } 14711da73821SVladimir Oltean l2_lookup.destports = BIT(port); 14721da73821SVladimir Oltean 14731da73821SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 14741da73821SVladimir Oltean SJA1105_SEARCH, &l2_lookup); 14751da73821SVladimir Oltean if (rc < 0) 14761da73821SVladimir Oltean return 0; 14771da73821SVladimir Oltean 14781da73821SVladimir Oltean l2_lookup.destports &= ~BIT(port); 14791da73821SVladimir Oltean 14801da73821SVladimir Oltean /* Decide whether we remove just this port from the FDB entry, 14811da73821SVladimir Oltean * or if we remove it completely. 14821da73821SVladimir Oltean */ 14831da73821SVladimir Oltean if (l2_lookup.destports) 14841da73821SVladimir Oltean keep = true; 14851da73821SVladimir Oltean else 14861da73821SVladimir Oltean keep = false; 14871da73821SVladimir Oltean 148860f6053fSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 14891da73821SVladimir Oltean l2_lookup.index, &l2_lookup, keep); 149060f6053fSVladimir Oltean if (rc < 0) 149160f6053fSVladimir Oltean return rc; 149260f6053fSVladimir Oltean 149360f6053fSVladimir Oltean return sja1105_static_fdb_change(priv, port, &l2_lookup, keep); 14949dfa6911SVladimir Oltean } 14959dfa6911SVladimir Oltean 14969dfa6911SVladimir Oltean static int sja1105_fdb_add(struct dsa_switch *ds, int port, 14979dfa6911SVladimir Oltean const unsigned char *addr, u16 vid) 14989dfa6911SVladimir Oltean { 14999dfa6911SVladimir Oltean struct sja1105_private *priv = ds->priv; 1500b3ee526aSVladimir Oltean 15016d7c7d94SVladimir Oltean /* dsa_8021q is in effect when the bridge's vlan_filtering isn't, 15026d7c7d94SVladimir Oltean * so the switch still does some VLAN processing internally. 15036d7c7d94SVladimir Oltean * But Shared VLAN Learning (SVL) is also active, and it will take 15046d7c7d94SVladimir Oltean * care of autonomous forwarding between the unique pvid's of each 15056d7c7d94SVladimir Oltean * port. Here we just make sure that users can't add duplicate FDB 15066d7c7d94SVladimir Oltean * entries when in this mode - the actual VID doesn't matter except 15076d7c7d94SVladimir Oltean * for what gets printed in 'bridge fdb show'. In the case of zero, 15086d7c7d94SVladimir Oltean * no VID gets printed at all. 150993647594SVladimir Oltean */ 15107f14937fSVladimir Oltean if (priv->vlan_state != SJA1105_VLAN_FILTERING_FULL) 15116d7c7d94SVladimir Oltean vid = 0; 151293647594SVladimir Oltean 15136d7c7d94SVladimir Oltean return priv->info->fdb_add_cmd(ds, port, addr, vid); 15149dfa6911SVladimir Oltean } 15159dfa6911SVladimir Oltean 15169dfa6911SVladimir Oltean static int sja1105_fdb_del(struct dsa_switch *ds, int port, 15179dfa6911SVladimir Oltean const unsigned char *addr, u16 vid) 15189dfa6911SVladimir Oltean { 15199dfa6911SVladimir Oltean struct sja1105_private *priv = ds->priv; 15209dfa6911SVladimir Oltean 15217f14937fSVladimir Oltean if (priv->vlan_state != SJA1105_VLAN_FILTERING_FULL) 15226d7c7d94SVladimir Oltean vid = 0; 15236d7c7d94SVladimir Oltean 1524b3ee526aSVladimir Oltean return priv->info->fdb_del_cmd(ds, port, addr, vid); 15259dfa6911SVladimir Oltean } 15269dfa6911SVladimir Oltean 1527291d1e72SVladimir Oltean static int sja1105_fdb_dump(struct dsa_switch *ds, int port, 1528291d1e72SVladimir Oltean dsa_fdb_dump_cb_t *cb, void *data) 1529291d1e72SVladimir Oltean { 1530291d1e72SVladimir Oltean struct sja1105_private *priv = ds->priv; 1531291d1e72SVladimir Oltean struct device *dev = ds->dev; 1532291d1e72SVladimir Oltean int i; 1533291d1e72SVladimir Oltean 1534291d1e72SVladimir Oltean for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) { 1535291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 1536291d1e72SVladimir Oltean u8 macaddr[ETH_ALEN]; 1537291d1e72SVladimir Oltean int rc; 1538291d1e72SVladimir Oltean 1539291d1e72SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 1540291d1e72SVladimir Oltean i, &l2_lookup); 1541291d1e72SVladimir Oltean /* No fdb entry at i, not an issue */ 1542def84604SVladimir Oltean if (rc == -ENOENT) 1543291d1e72SVladimir Oltean continue; 1544291d1e72SVladimir Oltean if (rc) { 1545291d1e72SVladimir Oltean dev_err(dev, "Failed to dump FDB: %d\n", rc); 1546291d1e72SVladimir Oltean return rc; 1547291d1e72SVladimir Oltean } 1548291d1e72SVladimir Oltean 1549291d1e72SVladimir Oltean /* FDB dump callback is per port. This means we have to 1550291d1e72SVladimir Oltean * disregard a valid entry if it's not for this port, even if 1551291d1e72SVladimir Oltean * only to revisit it later. This is inefficient because the 1552291d1e72SVladimir Oltean * 1024-sized FDB table needs to be traversed 4 times through 1553291d1e72SVladimir Oltean * SPI during a 'bridge fdb show' command. 1554291d1e72SVladimir Oltean */ 1555291d1e72SVladimir Oltean if (!(l2_lookup.destports & BIT(port))) 1556291d1e72SVladimir Oltean continue; 15574d942354SVladimir Oltean 15584d942354SVladimir Oltean /* We need to hide the FDB entry for unknown multicast */ 15594d942354SVladimir Oltean if (l2_lookup.macaddr == SJA1105_UNKNOWN_MULTICAST && 15604d942354SVladimir Oltean l2_lookup.mask_macaddr == SJA1105_UNKNOWN_MULTICAST) 15614d942354SVladimir Oltean continue; 15624d942354SVladimir Oltean 1563291d1e72SVladimir Oltean u64_to_ether_addr(l2_lookup.macaddr, macaddr); 156493647594SVladimir Oltean 15656d7c7d94SVladimir Oltean /* We need to hide the dsa_8021q VLANs from the user. */ 15667f14937fSVladimir Oltean if (priv->vlan_state == SJA1105_VLAN_UNAWARE) 15676d7c7d94SVladimir Oltean l2_lookup.vlanid = 0; 156817ae6555SVladimir Oltean cb(macaddr, l2_lookup.vlanid, l2_lookup.lockeds, data); 1569291d1e72SVladimir Oltean } 1570291d1e72SVladimir Oltean return 0; 1571291d1e72SVladimir Oltean } 1572291d1e72SVladimir Oltean 1573a52b2da7SVladimir Oltean static int sja1105_mdb_add(struct dsa_switch *ds, int port, 1574291d1e72SVladimir Oltean const struct switchdev_obj_port_mdb *mdb) 1575291d1e72SVladimir Oltean { 1576a52b2da7SVladimir Oltean return sja1105_fdb_add(ds, port, mdb->addr, mdb->vid); 1577291d1e72SVladimir Oltean } 1578291d1e72SVladimir Oltean 1579291d1e72SVladimir Oltean static int sja1105_mdb_del(struct dsa_switch *ds, int port, 1580291d1e72SVladimir Oltean const struct switchdev_obj_port_mdb *mdb) 1581291d1e72SVladimir Oltean { 1582291d1e72SVladimir Oltean return sja1105_fdb_del(ds, port, mdb->addr, mdb->vid); 1583291d1e72SVladimir Oltean } 1584291d1e72SVladimir Oltean 15857f7ccdeaSVladimir Oltean /* Common function for unicast and broadcast flood configuration. 15867f7ccdeaSVladimir Oltean * Flooding is configured between each {ingress, egress} port pair, and since 15877f7ccdeaSVladimir Oltean * the bridge's semantics are those of "egress flooding", it means we must 15887f7ccdeaSVladimir Oltean * enable flooding towards this port from all ingress ports that are in the 15897f7ccdeaSVladimir Oltean * same forwarding domain. 15907f7ccdeaSVladimir Oltean */ 15917f7ccdeaSVladimir Oltean static int sja1105_manage_flood_domains(struct sja1105_private *priv) 15927f7ccdeaSVladimir Oltean { 15937f7ccdeaSVladimir Oltean struct sja1105_l2_forwarding_entry *l2_fwd; 15947f7ccdeaSVladimir Oltean struct dsa_switch *ds = priv->ds; 15957f7ccdeaSVladimir Oltean int from, to, rc; 15967f7ccdeaSVladimir Oltean 15977f7ccdeaSVladimir Oltean l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries; 15987f7ccdeaSVladimir Oltean 15997f7ccdeaSVladimir Oltean for (from = 0; from < ds->num_ports; from++) { 16007f7ccdeaSVladimir Oltean u64 fl_domain = 0, bc_domain = 0; 16017f7ccdeaSVladimir Oltean 16027f7ccdeaSVladimir Oltean for (to = 0; to < priv->ds->num_ports; to++) { 16037f7ccdeaSVladimir Oltean if (!sja1105_can_forward(l2_fwd, from, to)) 16047f7ccdeaSVladimir Oltean continue; 16057f7ccdeaSVladimir Oltean 16067f7ccdeaSVladimir Oltean if (priv->ucast_egress_floods & BIT(to)) 16077f7ccdeaSVladimir Oltean fl_domain |= BIT(to); 16087f7ccdeaSVladimir Oltean if (priv->bcast_egress_floods & BIT(to)) 16097f7ccdeaSVladimir Oltean bc_domain |= BIT(to); 16107f7ccdeaSVladimir Oltean } 16117f7ccdeaSVladimir Oltean 16127f7ccdeaSVladimir Oltean /* Nothing changed, nothing to do */ 16137f7ccdeaSVladimir Oltean if (l2_fwd[from].fl_domain == fl_domain && 16147f7ccdeaSVladimir Oltean l2_fwd[from].bc_domain == bc_domain) 16157f7ccdeaSVladimir Oltean continue; 16167f7ccdeaSVladimir Oltean 16177f7ccdeaSVladimir Oltean l2_fwd[from].fl_domain = fl_domain; 16187f7ccdeaSVladimir Oltean l2_fwd[from].bc_domain = bc_domain; 16197f7ccdeaSVladimir Oltean 16207f7ccdeaSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING, 16217f7ccdeaSVladimir Oltean from, &l2_fwd[from], true); 16227f7ccdeaSVladimir Oltean if (rc < 0) 16237f7ccdeaSVladimir Oltean return rc; 16247f7ccdeaSVladimir Oltean } 16257f7ccdeaSVladimir Oltean 16267f7ccdeaSVladimir Oltean return 0; 16277f7ccdeaSVladimir Oltean } 16287f7ccdeaSVladimir Oltean 16298aa9ebccSVladimir Oltean static int sja1105_bridge_member(struct dsa_switch *ds, int port, 16308aa9ebccSVladimir Oltean struct net_device *br, bool member) 16318aa9ebccSVladimir Oltean { 16328aa9ebccSVladimir Oltean struct sja1105_l2_forwarding_entry *l2_fwd; 16338aa9ebccSVladimir Oltean struct sja1105_private *priv = ds->priv; 16348aa9ebccSVladimir Oltean int i, rc; 16358aa9ebccSVladimir Oltean 16368aa9ebccSVladimir Oltean l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries; 16378aa9ebccSVladimir Oltean 16388aa9ebccSVladimir Oltean for (i = 0; i < SJA1105_NUM_PORTS; i++) { 16398aa9ebccSVladimir Oltean /* Add this port to the forwarding matrix of the 16408aa9ebccSVladimir Oltean * other ports in the same bridge, and viceversa. 16418aa9ebccSVladimir Oltean */ 16428aa9ebccSVladimir Oltean if (!dsa_is_user_port(ds, i)) 16438aa9ebccSVladimir Oltean continue; 16448aa9ebccSVladimir Oltean /* For the ports already under the bridge, only one thing needs 16458aa9ebccSVladimir Oltean * to be done, and that is to add this port to their 16468aa9ebccSVladimir Oltean * reachability domain. So we can perform the SPI write for 16478aa9ebccSVladimir Oltean * them immediately. However, for this port itself (the one 16488aa9ebccSVladimir Oltean * that is new to the bridge), we need to add all other ports 16498aa9ebccSVladimir Oltean * to its reachability domain. So we do that incrementally in 16508aa9ebccSVladimir Oltean * this loop, and perform the SPI write only at the end, once 16518aa9ebccSVladimir Oltean * the domain contains all other bridge ports. 16528aa9ebccSVladimir Oltean */ 16538aa9ebccSVladimir Oltean if (i == port) 16548aa9ebccSVladimir Oltean continue; 16558aa9ebccSVladimir Oltean if (dsa_to_port(ds, i)->bridge_dev != br) 16568aa9ebccSVladimir Oltean continue; 16578aa9ebccSVladimir Oltean sja1105_port_allow_traffic(l2_fwd, i, port, member); 16588aa9ebccSVladimir Oltean sja1105_port_allow_traffic(l2_fwd, port, i, member); 16598aa9ebccSVladimir Oltean 16608aa9ebccSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING, 16618aa9ebccSVladimir Oltean i, &l2_fwd[i], true); 16628aa9ebccSVladimir Oltean if (rc < 0) 16638aa9ebccSVladimir Oltean return rc; 16648aa9ebccSVladimir Oltean } 16658aa9ebccSVladimir Oltean 16667f7ccdeaSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING, 16678aa9ebccSVladimir Oltean port, &l2_fwd[port], true); 16687f7ccdeaSVladimir Oltean if (rc) 16697f7ccdeaSVladimir Oltean return rc; 16707f7ccdeaSVladimir Oltean 16717f7ccdeaSVladimir Oltean return sja1105_manage_flood_domains(priv); 16728aa9ebccSVladimir Oltean } 16738aa9ebccSVladimir Oltean 1674640f763fSVladimir Oltean static void sja1105_bridge_stp_state_set(struct dsa_switch *ds, int port, 1675640f763fSVladimir Oltean u8 state) 1676640f763fSVladimir Oltean { 1677640f763fSVladimir Oltean struct sja1105_private *priv = ds->priv; 1678640f763fSVladimir Oltean struct sja1105_mac_config_entry *mac; 1679640f763fSVladimir Oltean 1680640f763fSVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 1681640f763fSVladimir Oltean 1682640f763fSVladimir Oltean switch (state) { 1683640f763fSVladimir Oltean case BR_STATE_DISABLED: 1684640f763fSVladimir Oltean case BR_STATE_BLOCKING: 1685640f763fSVladimir Oltean /* From UM10944 description of DRPDTAG (why put this there?): 1686640f763fSVladimir Oltean * "Management traffic flows to the port regardless of the state 1687640f763fSVladimir Oltean * of the INGRESS flag". So BPDUs are still be allowed to pass. 1688640f763fSVladimir Oltean * At the moment no difference between DISABLED and BLOCKING. 1689640f763fSVladimir Oltean */ 1690640f763fSVladimir Oltean mac[port].ingress = false; 1691640f763fSVladimir Oltean mac[port].egress = false; 1692640f763fSVladimir Oltean mac[port].dyn_learn = false; 1693640f763fSVladimir Oltean break; 1694640f763fSVladimir Oltean case BR_STATE_LISTENING: 1695640f763fSVladimir Oltean mac[port].ingress = true; 1696640f763fSVladimir Oltean mac[port].egress = false; 1697640f763fSVladimir Oltean mac[port].dyn_learn = false; 1698640f763fSVladimir Oltean break; 1699640f763fSVladimir Oltean case BR_STATE_LEARNING: 1700640f763fSVladimir Oltean mac[port].ingress = true; 1701640f763fSVladimir Oltean mac[port].egress = false; 17024d942354SVladimir Oltean mac[port].dyn_learn = !!(priv->learn_ena & BIT(port)); 1703640f763fSVladimir Oltean break; 1704640f763fSVladimir Oltean case BR_STATE_FORWARDING: 1705640f763fSVladimir Oltean mac[port].ingress = true; 1706640f763fSVladimir Oltean mac[port].egress = true; 17074d942354SVladimir Oltean mac[port].dyn_learn = !!(priv->learn_ena & BIT(port)); 1708640f763fSVladimir Oltean break; 1709640f763fSVladimir Oltean default: 1710640f763fSVladimir Oltean dev_err(ds->dev, "invalid STP state: %d\n", state); 1711640f763fSVladimir Oltean return; 1712640f763fSVladimir Oltean } 1713640f763fSVladimir Oltean 1714640f763fSVladimir Oltean sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, 1715640f763fSVladimir Oltean &mac[port], true); 1716640f763fSVladimir Oltean } 1717640f763fSVladimir Oltean 17188aa9ebccSVladimir Oltean static int sja1105_bridge_join(struct dsa_switch *ds, int port, 17198aa9ebccSVladimir Oltean struct net_device *br) 17208aa9ebccSVladimir Oltean { 17218aa9ebccSVladimir Oltean return sja1105_bridge_member(ds, port, br, true); 17228aa9ebccSVladimir Oltean } 17238aa9ebccSVladimir Oltean 17248aa9ebccSVladimir Oltean static void sja1105_bridge_leave(struct dsa_switch *ds, int port, 17258aa9ebccSVladimir Oltean struct net_device *br) 17268aa9ebccSVladimir Oltean { 17278aa9ebccSVladimir Oltean sja1105_bridge_member(ds, port, br, false); 17288aa9ebccSVladimir Oltean } 17298aa9ebccSVladimir Oltean 17304d752508SVladimir Oltean #define BYTES_PER_KBIT (1000LL / 8) 17314d752508SVladimir Oltean 17324d752508SVladimir Oltean static int sja1105_find_unused_cbs_shaper(struct sja1105_private *priv) 17334d752508SVladimir Oltean { 17344d752508SVladimir Oltean int i; 17354d752508SVladimir Oltean 17364d752508SVladimir Oltean for (i = 0; i < priv->info->num_cbs_shapers; i++) 17374d752508SVladimir Oltean if (!priv->cbs[i].idle_slope && !priv->cbs[i].send_slope) 17384d752508SVladimir Oltean return i; 17394d752508SVladimir Oltean 17404d752508SVladimir Oltean return -1; 17414d752508SVladimir Oltean } 17424d752508SVladimir Oltean 17434d752508SVladimir Oltean static int sja1105_delete_cbs_shaper(struct sja1105_private *priv, int port, 17444d752508SVladimir Oltean int prio) 17454d752508SVladimir Oltean { 17464d752508SVladimir Oltean int i; 17474d752508SVladimir Oltean 17484d752508SVladimir Oltean for (i = 0; i < priv->info->num_cbs_shapers; i++) { 17494d752508SVladimir Oltean struct sja1105_cbs_entry *cbs = &priv->cbs[i]; 17504d752508SVladimir Oltean 17514d752508SVladimir Oltean if (cbs->port == port && cbs->prio == prio) { 17524d752508SVladimir Oltean memset(cbs, 0, sizeof(*cbs)); 17534d752508SVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_CBS, 17544d752508SVladimir Oltean i, cbs, true); 17554d752508SVladimir Oltean } 17564d752508SVladimir Oltean } 17574d752508SVladimir Oltean 17584d752508SVladimir Oltean return 0; 17594d752508SVladimir Oltean } 17604d752508SVladimir Oltean 17614d752508SVladimir Oltean static int sja1105_setup_tc_cbs(struct dsa_switch *ds, int port, 17624d752508SVladimir Oltean struct tc_cbs_qopt_offload *offload) 17634d752508SVladimir Oltean { 17644d752508SVladimir Oltean struct sja1105_private *priv = ds->priv; 17654d752508SVladimir Oltean struct sja1105_cbs_entry *cbs; 17664d752508SVladimir Oltean int index; 17674d752508SVladimir Oltean 17684d752508SVladimir Oltean if (!offload->enable) 17694d752508SVladimir Oltean return sja1105_delete_cbs_shaper(priv, port, offload->queue); 17704d752508SVladimir Oltean 17714d752508SVladimir Oltean index = sja1105_find_unused_cbs_shaper(priv); 17724d752508SVladimir Oltean if (index < 0) 17734d752508SVladimir Oltean return -ENOSPC; 17744d752508SVladimir Oltean 17754d752508SVladimir Oltean cbs = &priv->cbs[index]; 17764d752508SVladimir Oltean cbs->port = port; 17774d752508SVladimir Oltean cbs->prio = offload->queue; 17784d752508SVladimir Oltean /* locredit and sendslope are negative by definition. In hardware, 17794d752508SVladimir Oltean * positive values must be provided, and the negative sign is implicit. 17804d752508SVladimir Oltean */ 17814d752508SVladimir Oltean cbs->credit_hi = offload->hicredit; 17824d752508SVladimir Oltean cbs->credit_lo = abs(offload->locredit); 17834d752508SVladimir Oltean /* User space is in kbits/sec, hardware in bytes/sec */ 17844d752508SVladimir Oltean cbs->idle_slope = offload->idleslope * BYTES_PER_KBIT; 17854d752508SVladimir Oltean cbs->send_slope = abs(offload->sendslope * BYTES_PER_KBIT); 17864d752508SVladimir Oltean /* Convert the negative values from 64-bit 2's complement 17874d752508SVladimir Oltean * to 32-bit 2's complement (for the case of 0x80000000 whose 17884d752508SVladimir Oltean * negative is still negative). 17894d752508SVladimir Oltean */ 17904d752508SVladimir Oltean cbs->credit_lo &= GENMASK_ULL(31, 0); 17914d752508SVladimir Oltean cbs->send_slope &= GENMASK_ULL(31, 0); 17924d752508SVladimir Oltean 17934d752508SVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_CBS, index, cbs, 17944d752508SVladimir Oltean true); 17954d752508SVladimir Oltean } 17964d752508SVladimir Oltean 17974d752508SVladimir Oltean static int sja1105_reload_cbs(struct sja1105_private *priv) 17984d752508SVladimir Oltean { 17994d752508SVladimir Oltean int rc = 0, i; 18004d752508SVladimir Oltean 18014d752508SVladimir Oltean for (i = 0; i < priv->info->num_cbs_shapers; i++) { 18024d752508SVladimir Oltean struct sja1105_cbs_entry *cbs = &priv->cbs[i]; 18034d752508SVladimir Oltean 18044d752508SVladimir Oltean if (!cbs->idle_slope && !cbs->send_slope) 18054d752508SVladimir Oltean continue; 18064d752508SVladimir Oltean 18074d752508SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_CBS, i, cbs, 18084d752508SVladimir Oltean true); 18094d752508SVladimir Oltean if (rc) 18104d752508SVladimir Oltean break; 18114d752508SVladimir Oltean } 18124d752508SVladimir Oltean 18134d752508SVladimir Oltean return rc; 18144d752508SVladimir Oltean } 18154d752508SVladimir Oltean 18162eea1fa8SVladimir Oltean static const char * const sja1105_reset_reasons[] = { 18172eea1fa8SVladimir Oltean [SJA1105_VLAN_FILTERING] = "VLAN filtering", 18182eea1fa8SVladimir Oltean [SJA1105_RX_HWTSTAMPING] = "RX timestamping", 18192eea1fa8SVladimir Oltean [SJA1105_AGEING_TIME] = "Ageing time", 18202eea1fa8SVladimir Oltean [SJA1105_SCHEDULING] = "Time-aware scheduling", 1821c279c726SVladimir Oltean [SJA1105_BEST_EFFORT_POLICING] = "Best-effort policing", 1822dfacc5a2SVladimir Oltean [SJA1105_VIRTUAL_LINKS] = "Virtual links", 18232eea1fa8SVladimir Oltean }; 18242eea1fa8SVladimir Oltean 18256666cebcSVladimir Oltean /* For situations where we need to change a setting at runtime that is only 18266666cebcSVladimir Oltean * available through the static configuration, resetting the switch in order 18276666cebcSVladimir Oltean * to upload the new static config is unavoidable. Back up the settings we 18286666cebcSVladimir Oltean * modify at runtime (currently only MAC) and restore them after uploading, 18296666cebcSVladimir Oltean * such that this operation is relatively seamless. 18306666cebcSVladimir Oltean */ 18312eea1fa8SVladimir Oltean int sja1105_static_config_reload(struct sja1105_private *priv, 18322eea1fa8SVladimir Oltean enum sja1105_reset_reason reason) 18336666cebcSVladimir Oltean { 18346cf99c13SVladimir Oltean struct ptp_system_timestamp ptp_sts_before; 18356cf99c13SVladimir Oltean struct ptp_system_timestamp ptp_sts_after; 18366666cebcSVladimir Oltean struct sja1105_mac_config_entry *mac; 18376666cebcSVladimir Oltean int speed_mbps[SJA1105_NUM_PORTS]; 18386cf99c13SVladimir Oltean struct dsa_switch *ds = priv->ds; 18396cf99c13SVladimir Oltean s64 t1, t2, t3, t4; 18406cf99c13SVladimir Oltean s64 t12, t34; 1841ffe10e67SVladimir Oltean u16 bmcr = 0; 18426666cebcSVladimir Oltean int rc, i; 18436cf99c13SVladimir Oltean s64 now; 18446666cebcSVladimir Oltean 1845af580ae2SVladimir Oltean mutex_lock(&priv->mgmt_lock); 1846af580ae2SVladimir Oltean 18476666cebcSVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 18486666cebcSVladimir Oltean 18498400cff6SVladimir Oltean /* Back up the dynamic link speed changed by sja1105_adjust_port_config 18508400cff6SVladimir Oltean * in order to temporarily restore it to SJA1105_SPEED_AUTO - which the 18518400cff6SVladimir Oltean * switch wants to see in the static config in order to allow us to 18528400cff6SVladimir Oltean * change it through the dynamic interface later. 18536666cebcSVladimir Oltean */ 18546666cebcSVladimir Oltean for (i = 0; i < SJA1105_NUM_PORTS; i++) { 18556666cebcSVladimir Oltean speed_mbps[i] = sja1105_speed[mac[i].speed]; 18566666cebcSVladimir Oltean mac[i].speed = SJA1105_SPEED_AUTO; 18576666cebcSVladimir Oltean } 18586666cebcSVladimir Oltean 1859ffe10e67SVladimir Oltean if (sja1105_supports_sgmii(priv, SJA1105_SGMII_PORT)) 1860ffe10e67SVladimir Oltean bmcr = sja1105_sgmii_read(priv, MII_BMCR); 1861ffe10e67SVladimir Oltean 18626cf99c13SVladimir Oltean /* No PTP operations can run right now */ 18636cf99c13SVladimir Oltean mutex_lock(&priv->ptp_data.lock); 18646cf99c13SVladimir Oltean 18656cf99c13SVladimir Oltean rc = __sja1105_ptp_gettimex(ds, &now, &ptp_sts_before); 18666cf99c13SVladimir Oltean if (rc < 0) 18676cf99c13SVladimir Oltean goto out_unlock_ptp; 18686cf99c13SVladimir Oltean 18696666cebcSVladimir Oltean /* Reset switch and send updated static configuration */ 18706666cebcSVladimir Oltean rc = sja1105_static_config_upload(priv); 18716666cebcSVladimir Oltean if (rc < 0) 18726cf99c13SVladimir Oltean goto out_unlock_ptp; 18736cf99c13SVladimir Oltean 18746cf99c13SVladimir Oltean rc = __sja1105_ptp_settime(ds, 0, &ptp_sts_after); 18756cf99c13SVladimir Oltean if (rc < 0) 18766cf99c13SVladimir Oltean goto out_unlock_ptp; 18776cf99c13SVladimir Oltean 18786cf99c13SVladimir Oltean t1 = timespec64_to_ns(&ptp_sts_before.pre_ts); 18796cf99c13SVladimir Oltean t2 = timespec64_to_ns(&ptp_sts_before.post_ts); 18806cf99c13SVladimir Oltean t3 = timespec64_to_ns(&ptp_sts_after.pre_ts); 18816cf99c13SVladimir Oltean t4 = timespec64_to_ns(&ptp_sts_after.post_ts); 18826cf99c13SVladimir Oltean /* Mid point, corresponds to pre-reset PTPCLKVAL */ 18836cf99c13SVladimir Oltean t12 = t1 + (t2 - t1) / 2; 18846cf99c13SVladimir Oltean /* Mid point, corresponds to post-reset PTPCLKVAL, aka 0 */ 18856cf99c13SVladimir Oltean t34 = t3 + (t4 - t3) / 2; 18866cf99c13SVladimir Oltean /* Advance PTPCLKVAL by the time it took since its readout */ 18876cf99c13SVladimir Oltean now += (t34 - t12); 18886cf99c13SVladimir Oltean 18896cf99c13SVladimir Oltean __sja1105_ptp_adjtime(ds, now); 18906cf99c13SVladimir Oltean 18916cf99c13SVladimir Oltean out_unlock_ptp: 18926cf99c13SVladimir Oltean mutex_unlock(&priv->ptp_data.lock); 18936666cebcSVladimir Oltean 18942eea1fa8SVladimir Oltean dev_info(priv->ds->dev, 18952eea1fa8SVladimir Oltean "Reset switch and programmed static config. Reason: %s\n", 18962eea1fa8SVladimir Oltean sja1105_reset_reasons[reason]); 18972eea1fa8SVladimir Oltean 18986666cebcSVladimir Oltean /* Configure the CGU (PLLs) for MII and RMII PHYs. 18996666cebcSVladimir Oltean * For these interfaces there is no dynamic configuration 19006666cebcSVladimir Oltean * needed, since PLLs have same settings at all speeds. 19016666cebcSVladimir Oltean */ 19026666cebcSVladimir Oltean rc = sja1105_clocking_setup(priv); 19036666cebcSVladimir Oltean if (rc < 0) 19046666cebcSVladimir Oltean goto out; 19056666cebcSVladimir Oltean 19066666cebcSVladimir Oltean for (i = 0; i < SJA1105_NUM_PORTS; i++) { 19078400cff6SVladimir Oltean rc = sja1105_adjust_port_config(priv, i, speed_mbps[i]); 19086666cebcSVladimir Oltean if (rc < 0) 19096666cebcSVladimir Oltean goto out; 19106666cebcSVladimir Oltean } 1911ffe10e67SVladimir Oltean 1912ffe10e67SVladimir Oltean if (sja1105_supports_sgmii(priv, SJA1105_SGMII_PORT)) { 1913ffe10e67SVladimir Oltean bool an_enabled = !!(bmcr & BMCR_ANENABLE); 1914ffe10e67SVladimir Oltean 1915ffe10e67SVladimir Oltean sja1105_sgmii_pcs_config(priv, an_enabled, false); 1916ffe10e67SVladimir Oltean 1917ffe10e67SVladimir Oltean if (!an_enabled) { 1918ffe10e67SVladimir Oltean int speed = SPEED_UNKNOWN; 1919ffe10e67SVladimir Oltean 1920ffe10e67SVladimir Oltean if (bmcr & BMCR_SPEED1000) 1921ffe10e67SVladimir Oltean speed = SPEED_1000; 1922ffe10e67SVladimir Oltean else if (bmcr & BMCR_SPEED100) 1923ffe10e67SVladimir Oltean speed = SPEED_100; 1924053d8ad1SVladimir Oltean else 1925ffe10e67SVladimir Oltean speed = SPEED_10; 1926ffe10e67SVladimir Oltean 1927ffe10e67SVladimir Oltean sja1105_sgmii_pcs_force_speed(priv, speed); 1928ffe10e67SVladimir Oltean } 1929ffe10e67SVladimir Oltean } 19304d752508SVladimir Oltean 19314d752508SVladimir Oltean rc = sja1105_reload_cbs(priv); 19324d752508SVladimir Oltean if (rc < 0) 19334d752508SVladimir Oltean goto out; 19346666cebcSVladimir Oltean out: 1935af580ae2SVladimir Oltean mutex_unlock(&priv->mgmt_lock); 1936af580ae2SVladimir Oltean 19376666cebcSVladimir Oltean return rc; 19386666cebcSVladimir Oltean } 19396666cebcSVladimir Oltean 19406666cebcSVladimir Oltean static int sja1105_pvid_apply(struct sja1105_private *priv, int port, u16 pvid) 19416666cebcSVladimir Oltean { 19426666cebcSVladimir Oltean struct sja1105_mac_config_entry *mac; 19436666cebcSVladimir Oltean 19446666cebcSVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 19456666cebcSVladimir Oltean 19466666cebcSVladimir Oltean mac[port].vlanid = pvid; 19476666cebcSVladimir Oltean 19486666cebcSVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, 19496666cebcSVladimir Oltean &mac[port], true); 19506666cebcSVladimir Oltean } 19516666cebcSVladimir Oltean 1952ac02a451SVladimir Oltean static int sja1105_crosschip_bridge_join(struct dsa_switch *ds, 1953ac02a451SVladimir Oltean int tree_index, int sw_index, 1954ac02a451SVladimir Oltean int other_port, struct net_device *br) 1955ac02a451SVladimir Oltean { 1956ac02a451SVladimir Oltean struct dsa_switch *other_ds = dsa_switch_find(tree_index, sw_index); 1957ac02a451SVladimir Oltean struct sja1105_private *other_priv = other_ds->priv; 1958ac02a451SVladimir Oltean struct sja1105_private *priv = ds->priv; 1959ac02a451SVladimir Oltean int port, rc; 1960ac02a451SVladimir Oltean 1961ac02a451SVladimir Oltean if (other_ds->ops != &sja1105_switch_ops) 1962ac02a451SVladimir Oltean return 0; 1963ac02a451SVladimir Oltean 1964ac02a451SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 1965ac02a451SVladimir Oltean if (!dsa_is_user_port(ds, port)) 1966ac02a451SVladimir Oltean continue; 1967ac02a451SVladimir Oltean if (dsa_to_port(ds, port)->bridge_dev != br) 1968ac02a451SVladimir Oltean continue; 1969ac02a451SVladimir Oltean 19705899ee36SVladimir Oltean rc = dsa_8021q_crosschip_bridge_join(priv->dsa_8021q_ctx, 19715899ee36SVladimir Oltean port, 19725899ee36SVladimir Oltean other_priv->dsa_8021q_ctx, 19735899ee36SVladimir Oltean other_port); 1974ac02a451SVladimir Oltean if (rc) 1975ac02a451SVladimir Oltean return rc; 1976ac02a451SVladimir Oltean 19775899ee36SVladimir Oltean rc = dsa_8021q_crosschip_bridge_join(other_priv->dsa_8021q_ctx, 19785899ee36SVladimir Oltean other_port, 19795899ee36SVladimir Oltean priv->dsa_8021q_ctx, 19805899ee36SVladimir Oltean port); 1981ac02a451SVladimir Oltean if (rc) 1982ac02a451SVladimir Oltean return rc; 1983ac02a451SVladimir Oltean } 1984ac02a451SVladimir Oltean 1985ac02a451SVladimir Oltean return 0; 1986ac02a451SVladimir Oltean } 1987ac02a451SVladimir Oltean 1988ac02a451SVladimir Oltean static void sja1105_crosschip_bridge_leave(struct dsa_switch *ds, 1989ac02a451SVladimir Oltean int tree_index, int sw_index, 1990ac02a451SVladimir Oltean int other_port, 1991ac02a451SVladimir Oltean struct net_device *br) 1992ac02a451SVladimir Oltean { 1993ac02a451SVladimir Oltean struct dsa_switch *other_ds = dsa_switch_find(tree_index, sw_index); 1994ac02a451SVladimir Oltean struct sja1105_private *other_priv = other_ds->priv; 1995ac02a451SVladimir Oltean struct sja1105_private *priv = ds->priv; 1996ac02a451SVladimir Oltean int port; 1997ac02a451SVladimir Oltean 1998ac02a451SVladimir Oltean if (other_ds->ops != &sja1105_switch_ops) 1999ac02a451SVladimir Oltean return; 2000ac02a451SVladimir Oltean 2001ac02a451SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 2002ac02a451SVladimir Oltean if (!dsa_is_user_port(ds, port)) 2003ac02a451SVladimir Oltean continue; 2004ac02a451SVladimir Oltean if (dsa_to_port(ds, port)->bridge_dev != br) 2005ac02a451SVladimir Oltean continue; 2006ac02a451SVladimir Oltean 20075899ee36SVladimir Oltean dsa_8021q_crosschip_bridge_leave(priv->dsa_8021q_ctx, port, 20085899ee36SVladimir Oltean other_priv->dsa_8021q_ctx, 20095899ee36SVladimir Oltean other_port); 2010ac02a451SVladimir Oltean 20115899ee36SVladimir Oltean dsa_8021q_crosschip_bridge_leave(other_priv->dsa_8021q_ctx, 20125899ee36SVladimir Oltean other_port, 20135899ee36SVladimir Oltean priv->dsa_8021q_ctx, port); 2014ac02a451SVladimir Oltean } 2015ac02a451SVladimir Oltean } 2016ac02a451SVladimir Oltean 2017227d07a0SVladimir Oltean static int sja1105_setup_8021q_tagging(struct dsa_switch *ds, bool enabled) 2018227d07a0SVladimir Oltean { 201960b33aebSVladimir Oltean struct sja1105_private *priv = ds->priv; 20207e092af2SVladimir Oltean int rc; 2021227d07a0SVladimir Oltean 20225899ee36SVladimir Oltean rc = dsa_8021q_setup(priv->dsa_8021q_ctx, enabled); 20237e092af2SVladimir Oltean if (rc) 2024227d07a0SVladimir Oltean return rc; 2025ac02a451SVladimir Oltean 2026227d07a0SVladimir Oltean dev_info(ds->dev, "%s switch tagging\n", 2027227d07a0SVladimir Oltean enabled ? "Enabled" : "Disabled"); 2028227d07a0SVladimir Oltean return 0; 2029227d07a0SVladimir Oltean } 2030227d07a0SVladimir Oltean 20318aa9ebccSVladimir Oltean static enum dsa_tag_protocol 20324d776482SFlorian Fainelli sja1105_get_tag_protocol(struct dsa_switch *ds, int port, 20334d776482SFlorian Fainelli enum dsa_tag_protocol mp) 20348aa9ebccSVladimir Oltean { 2035227d07a0SVladimir Oltean return DSA_TAG_PROTO_SJA1105; 20368aa9ebccSVladimir Oltean } 20378aa9ebccSVladimir Oltean 20383f01c91aSVladimir Oltean static int sja1105_find_free_subvlan(u16 *subvlan_map, bool pvid) 20393f01c91aSVladimir Oltean { 20403f01c91aSVladimir Oltean int subvlan; 20413f01c91aSVladimir Oltean 20423f01c91aSVladimir Oltean if (pvid) 20433f01c91aSVladimir Oltean return 0; 20443f01c91aSVladimir Oltean 20453f01c91aSVladimir Oltean for (subvlan = 1; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++) 20463f01c91aSVladimir Oltean if (subvlan_map[subvlan] == VLAN_N_VID) 20473f01c91aSVladimir Oltean return subvlan; 20483f01c91aSVladimir Oltean 20493f01c91aSVladimir Oltean return -1; 20503f01c91aSVladimir Oltean } 20513f01c91aSVladimir Oltean 20523f01c91aSVladimir Oltean static int sja1105_find_subvlan(u16 *subvlan_map, u16 vid) 20533f01c91aSVladimir Oltean { 20543f01c91aSVladimir Oltean int subvlan; 20553f01c91aSVladimir Oltean 20563f01c91aSVladimir Oltean for (subvlan = 0; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++) 20573f01c91aSVladimir Oltean if (subvlan_map[subvlan] == vid) 20583f01c91aSVladimir Oltean return subvlan; 20593f01c91aSVladimir Oltean 20603f01c91aSVladimir Oltean return -1; 20613f01c91aSVladimir Oltean } 20623f01c91aSVladimir Oltean 20633f01c91aSVladimir Oltean static int sja1105_find_committed_subvlan(struct sja1105_private *priv, 20643f01c91aSVladimir Oltean int port, u16 vid) 20653f01c91aSVladimir Oltean { 20663f01c91aSVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 20673f01c91aSVladimir Oltean 20683f01c91aSVladimir Oltean return sja1105_find_subvlan(sp->subvlan_map, vid); 20693f01c91aSVladimir Oltean } 20703f01c91aSVladimir Oltean 20713f01c91aSVladimir Oltean static void sja1105_init_subvlan_map(u16 *subvlan_map) 20723f01c91aSVladimir Oltean { 20733f01c91aSVladimir Oltean int subvlan; 20743f01c91aSVladimir Oltean 20753f01c91aSVladimir Oltean for (subvlan = 0; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++) 20763f01c91aSVladimir Oltean subvlan_map[subvlan] = VLAN_N_VID; 20773f01c91aSVladimir Oltean } 20783f01c91aSVladimir Oltean 20793f01c91aSVladimir Oltean static void sja1105_commit_subvlan_map(struct sja1105_private *priv, int port, 20803f01c91aSVladimir Oltean u16 *subvlan_map) 20813f01c91aSVladimir Oltean { 20823f01c91aSVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 20833f01c91aSVladimir Oltean int subvlan; 20843f01c91aSVladimir Oltean 20853f01c91aSVladimir Oltean for (subvlan = 0; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++) 20863f01c91aSVladimir Oltean sp->subvlan_map[subvlan] = subvlan_map[subvlan]; 20873f01c91aSVladimir Oltean } 20883f01c91aSVladimir Oltean 2089ec5ae610SVladimir Oltean static int sja1105_is_vlan_configured(struct sja1105_private *priv, u16 vid) 2090ec5ae610SVladimir Oltean { 2091ec5ae610SVladimir Oltean struct sja1105_vlan_lookup_entry *vlan; 2092ec5ae610SVladimir Oltean int count, i; 2093ec5ae610SVladimir Oltean 2094ec5ae610SVladimir Oltean vlan = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entries; 2095ec5ae610SVladimir Oltean count = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entry_count; 2096ec5ae610SVladimir Oltean 2097ec5ae610SVladimir Oltean for (i = 0; i < count; i++) 2098ec5ae610SVladimir Oltean if (vlan[i].vlanid == vid) 2099ec5ae610SVladimir Oltean return i; 2100ec5ae610SVladimir Oltean 2101ec5ae610SVladimir Oltean /* Return an invalid entry index if not found */ 2102ec5ae610SVladimir Oltean return -1; 2103ec5ae610SVladimir Oltean } 2104ec5ae610SVladimir Oltean 21053f01c91aSVladimir Oltean static int 21063f01c91aSVladimir Oltean sja1105_find_retagging_entry(struct sja1105_retagging_entry *retagging, 21073f01c91aSVladimir Oltean int count, int from_port, u16 from_vid, 21083f01c91aSVladimir Oltean u16 to_vid) 2109ec5ae610SVladimir Oltean { 21103f01c91aSVladimir Oltean int i; 21113f01c91aSVladimir Oltean 21123f01c91aSVladimir Oltean for (i = 0; i < count; i++) 21133f01c91aSVladimir Oltean if (retagging[i].ing_port == BIT(from_port) && 21143f01c91aSVladimir Oltean retagging[i].vlan_ing == from_vid && 21153f01c91aSVladimir Oltean retagging[i].vlan_egr == to_vid) 21163f01c91aSVladimir Oltean return i; 21173f01c91aSVladimir Oltean 21183f01c91aSVladimir Oltean /* Return an invalid entry index if not found */ 21193f01c91aSVladimir Oltean return -1; 21203f01c91aSVladimir Oltean } 21213f01c91aSVladimir Oltean 21223f01c91aSVladimir Oltean static int sja1105_commit_vlans(struct sja1105_private *priv, 21233f01c91aSVladimir Oltean struct sja1105_vlan_lookup_entry *new_vlan, 21243f01c91aSVladimir Oltean struct sja1105_retagging_entry *new_retagging, 21253f01c91aSVladimir Oltean int num_retagging) 21263f01c91aSVladimir Oltean { 21273f01c91aSVladimir Oltean struct sja1105_retagging_entry *retagging; 2128ec5ae610SVladimir Oltean struct sja1105_vlan_lookup_entry *vlan; 2129ec5ae610SVladimir Oltean struct sja1105_table *table; 2130ec5ae610SVladimir Oltean int num_vlans = 0; 2131ec5ae610SVladimir Oltean int rc, i, k = 0; 2132ec5ae610SVladimir Oltean 2133ec5ae610SVladimir Oltean /* VLAN table */ 2134ec5ae610SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP]; 2135ec5ae610SVladimir Oltean vlan = table->entries; 2136ec5ae610SVladimir Oltean 2137ec5ae610SVladimir Oltean for (i = 0; i < VLAN_N_VID; i++) { 2138ec5ae610SVladimir Oltean int match = sja1105_is_vlan_configured(priv, i); 2139ec5ae610SVladimir Oltean 2140ec5ae610SVladimir Oltean if (new_vlan[i].vlanid != VLAN_N_VID) 2141ec5ae610SVladimir Oltean num_vlans++; 2142ec5ae610SVladimir Oltean 2143ec5ae610SVladimir Oltean if (new_vlan[i].vlanid == VLAN_N_VID && match >= 0) { 2144ec5ae610SVladimir Oltean /* Was there before, no longer is. Delete */ 2145ec5ae610SVladimir Oltean dev_dbg(priv->ds->dev, "Deleting VLAN %d\n", i); 2146ec5ae610SVladimir Oltean rc = sja1105_dynamic_config_write(priv, 2147ec5ae610SVladimir Oltean BLK_IDX_VLAN_LOOKUP, 2148ec5ae610SVladimir Oltean i, &vlan[match], false); 2149ec5ae610SVladimir Oltean if (rc < 0) 2150ec5ae610SVladimir Oltean return rc; 2151ec5ae610SVladimir Oltean } else if (new_vlan[i].vlanid != VLAN_N_VID) { 2152ec5ae610SVladimir Oltean /* Nothing changed, don't do anything */ 2153ec5ae610SVladimir Oltean if (match >= 0 && 2154ec5ae610SVladimir Oltean vlan[match].vlanid == new_vlan[i].vlanid && 2155ec5ae610SVladimir Oltean vlan[match].tag_port == new_vlan[i].tag_port && 2156ec5ae610SVladimir Oltean vlan[match].vlan_bc == new_vlan[i].vlan_bc && 2157ec5ae610SVladimir Oltean vlan[match].vmemb_port == new_vlan[i].vmemb_port) 2158ec5ae610SVladimir Oltean continue; 2159ec5ae610SVladimir Oltean /* Update entry */ 2160ec5ae610SVladimir Oltean dev_dbg(priv->ds->dev, "Updating VLAN %d\n", i); 2161ec5ae610SVladimir Oltean rc = sja1105_dynamic_config_write(priv, 2162ec5ae610SVladimir Oltean BLK_IDX_VLAN_LOOKUP, 2163ec5ae610SVladimir Oltean i, &new_vlan[i], 2164ec5ae610SVladimir Oltean true); 2165ec5ae610SVladimir Oltean if (rc < 0) 2166ec5ae610SVladimir Oltean return rc; 2167ec5ae610SVladimir Oltean } 2168ec5ae610SVladimir Oltean } 2169ec5ae610SVladimir Oltean 2170ec5ae610SVladimir Oltean if (table->entry_count) 2171ec5ae610SVladimir Oltean kfree(table->entries); 2172ec5ae610SVladimir Oltean 2173ec5ae610SVladimir Oltean table->entries = kcalloc(num_vlans, table->ops->unpacked_entry_size, 2174ec5ae610SVladimir Oltean GFP_KERNEL); 2175ec5ae610SVladimir Oltean if (!table->entries) 2176ec5ae610SVladimir Oltean return -ENOMEM; 2177ec5ae610SVladimir Oltean 2178ec5ae610SVladimir Oltean table->entry_count = num_vlans; 2179ec5ae610SVladimir Oltean vlan = table->entries; 2180ec5ae610SVladimir Oltean 2181ec5ae610SVladimir Oltean for (i = 0; i < VLAN_N_VID; i++) { 2182ec5ae610SVladimir Oltean if (new_vlan[i].vlanid == VLAN_N_VID) 2183ec5ae610SVladimir Oltean continue; 2184ec5ae610SVladimir Oltean vlan[k++] = new_vlan[i]; 2185ec5ae610SVladimir Oltean } 2186ec5ae610SVladimir Oltean 21873f01c91aSVladimir Oltean /* VLAN Retagging Table */ 21883f01c91aSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_RETAGGING]; 21893f01c91aSVladimir Oltean retagging = table->entries; 21903f01c91aSVladimir Oltean 21913f01c91aSVladimir Oltean for (i = 0; i < table->entry_count; i++) { 21923f01c91aSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_RETAGGING, 21933f01c91aSVladimir Oltean i, &retagging[i], false); 21943f01c91aSVladimir Oltean if (rc) 21953f01c91aSVladimir Oltean return rc; 21963f01c91aSVladimir Oltean } 21973f01c91aSVladimir Oltean 21983f01c91aSVladimir Oltean if (table->entry_count) 21993f01c91aSVladimir Oltean kfree(table->entries); 22003f01c91aSVladimir Oltean 22013f01c91aSVladimir Oltean table->entries = kcalloc(num_retagging, table->ops->unpacked_entry_size, 22023f01c91aSVladimir Oltean GFP_KERNEL); 22033f01c91aSVladimir Oltean if (!table->entries) 22043f01c91aSVladimir Oltean return -ENOMEM; 22053f01c91aSVladimir Oltean 22063f01c91aSVladimir Oltean table->entry_count = num_retagging; 22073f01c91aSVladimir Oltean retagging = table->entries; 22083f01c91aSVladimir Oltean 22093f01c91aSVladimir Oltean for (i = 0; i < num_retagging; i++) { 22103f01c91aSVladimir Oltean retagging[i] = new_retagging[i]; 22113f01c91aSVladimir Oltean 22123f01c91aSVladimir Oltean /* Update entry */ 22133f01c91aSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_RETAGGING, 22143f01c91aSVladimir Oltean i, &retagging[i], true); 22153f01c91aSVladimir Oltean if (rc < 0) 22163f01c91aSVladimir Oltean return rc; 22173f01c91aSVladimir Oltean } 22183f01c91aSVladimir Oltean 2219ec5ae610SVladimir Oltean return 0; 2220ec5ae610SVladimir Oltean } 2221ec5ae610SVladimir Oltean 22223f01c91aSVladimir Oltean struct sja1105_crosschip_vlan { 22233f01c91aSVladimir Oltean struct list_head list; 22243f01c91aSVladimir Oltean u16 vid; 22253f01c91aSVladimir Oltean bool untagged; 22263f01c91aSVladimir Oltean int port; 22273f01c91aSVladimir Oltean int other_port; 22285899ee36SVladimir Oltean struct dsa_8021q_context *other_ctx; 22293f01c91aSVladimir Oltean }; 22303f01c91aSVladimir Oltean 2231ec5ae610SVladimir Oltean struct sja1105_crosschip_switch { 2232ec5ae610SVladimir Oltean struct list_head list; 22335899ee36SVladimir Oltean struct dsa_8021q_context *other_ctx; 2234ec5ae610SVladimir Oltean }; 2235ec5ae610SVladimir Oltean 2236ec5ae610SVladimir Oltean static int sja1105_commit_pvid(struct sja1105_private *priv) 2237ec5ae610SVladimir Oltean { 2238ec5ae610SVladimir Oltean struct sja1105_bridge_vlan *v; 2239ec5ae610SVladimir Oltean struct list_head *vlan_list; 2240ec5ae610SVladimir Oltean int rc = 0; 2241ec5ae610SVladimir Oltean 2242ec5ae610SVladimir Oltean if (priv->vlan_state == SJA1105_VLAN_FILTERING_FULL) 2243ec5ae610SVladimir Oltean vlan_list = &priv->bridge_vlans; 2244ec5ae610SVladimir Oltean else 2245ec5ae610SVladimir Oltean vlan_list = &priv->dsa_8021q_vlans; 2246ec5ae610SVladimir Oltean 2247ec5ae610SVladimir Oltean list_for_each_entry(v, vlan_list, list) { 2248ec5ae610SVladimir Oltean if (v->pvid) { 2249ec5ae610SVladimir Oltean rc = sja1105_pvid_apply(priv, v->port, v->vid); 2250ec5ae610SVladimir Oltean if (rc) 2251ec5ae610SVladimir Oltean break; 2252ec5ae610SVladimir Oltean } 2253ec5ae610SVladimir Oltean } 2254ec5ae610SVladimir Oltean 2255ec5ae610SVladimir Oltean return rc; 2256ec5ae610SVladimir Oltean } 2257ec5ae610SVladimir Oltean 2258ec5ae610SVladimir Oltean static int 2259ec5ae610SVladimir Oltean sja1105_build_bridge_vlans(struct sja1105_private *priv, 2260ec5ae610SVladimir Oltean struct sja1105_vlan_lookup_entry *new_vlan) 2261ec5ae610SVladimir Oltean { 2262ec5ae610SVladimir Oltean struct sja1105_bridge_vlan *v; 2263ec5ae610SVladimir Oltean 2264ec5ae610SVladimir Oltean if (priv->vlan_state == SJA1105_VLAN_UNAWARE) 2265ec5ae610SVladimir Oltean return 0; 2266ec5ae610SVladimir Oltean 2267ec5ae610SVladimir Oltean list_for_each_entry(v, &priv->bridge_vlans, list) { 2268ec5ae610SVladimir Oltean int match = v->vid; 2269ec5ae610SVladimir Oltean 2270ec5ae610SVladimir Oltean new_vlan[match].vlanid = v->vid; 2271ec5ae610SVladimir Oltean new_vlan[match].vmemb_port |= BIT(v->port); 2272ec5ae610SVladimir Oltean new_vlan[match].vlan_bc |= BIT(v->port); 2273ec5ae610SVladimir Oltean if (!v->untagged) 2274ec5ae610SVladimir Oltean new_vlan[match].tag_port |= BIT(v->port); 2275ec5ae610SVladimir Oltean } 2276ec5ae610SVladimir Oltean 2277ec5ae610SVladimir Oltean return 0; 2278ec5ae610SVladimir Oltean } 2279ec5ae610SVladimir Oltean 2280ec5ae610SVladimir Oltean static int 2281ec5ae610SVladimir Oltean sja1105_build_dsa_8021q_vlans(struct sja1105_private *priv, 2282ec5ae610SVladimir Oltean struct sja1105_vlan_lookup_entry *new_vlan) 2283ec5ae610SVladimir Oltean { 2284ec5ae610SVladimir Oltean struct sja1105_bridge_vlan *v; 2285ec5ae610SVladimir Oltean 2286ec5ae610SVladimir Oltean if (priv->vlan_state == SJA1105_VLAN_FILTERING_FULL) 2287ec5ae610SVladimir Oltean return 0; 2288ec5ae610SVladimir Oltean 2289ec5ae610SVladimir Oltean list_for_each_entry(v, &priv->dsa_8021q_vlans, list) { 2290ec5ae610SVladimir Oltean int match = v->vid; 2291ec5ae610SVladimir Oltean 2292ec5ae610SVladimir Oltean new_vlan[match].vlanid = v->vid; 2293ec5ae610SVladimir Oltean new_vlan[match].vmemb_port |= BIT(v->port); 2294ec5ae610SVladimir Oltean new_vlan[match].vlan_bc |= BIT(v->port); 2295ec5ae610SVladimir Oltean if (!v->untagged) 2296ec5ae610SVladimir Oltean new_vlan[match].tag_port |= BIT(v->port); 2297ec5ae610SVladimir Oltean } 2298ec5ae610SVladimir Oltean 2299ec5ae610SVladimir Oltean return 0; 2300ec5ae610SVladimir Oltean } 2301ec5ae610SVladimir Oltean 23023f01c91aSVladimir Oltean static int sja1105_build_subvlans(struct sja1105_private *priv, 23033f01c91aSVladimir Oltean u16 subvlan_map[][DSA_8021Q_N_SUBVLAN], 23043f01c91aSVladimir Oltean struct sja1105_vlan_lookup_entry *new_vlan, 23053f01c91aSVladimir Oltean struct sja1105_retagging_entry *new_retagging, 23063f01c91aSVladimir Oltean int *num_retagging) 23073f01c91aSVladimir Oltean { 23083f01c91aSVladimir Oltean struct sja1105_bridge_vlan *v; 23093f01c91aSVladimir Oltean int k = *num_retagging; 23103f01c91aSVladimir Oltean 23113f01c91aSVladimir Oltean if (priv->vlan_state != SJA1105_VLAN_BEST_EFFORT) 23123f01c91aSVladimir Oltean return 0; 23133f01c91aSVladimir Oltean 23143f01c91aSVladimir Oltean list_for_each_entry(v, &priv->bridge_vlans, list) { 23153f01c91aSVladimir Oltean int upstream = dsa_upstream_port(priv->ds, v->port); 23163f01c91aSVladimir Oltean int match, subvlan; 23173f01c91aSVladimir Oltean u16 rx_vid; 23183f01c91aSVladimir Oltean 23193f01c91aSVladimir Oltean /* Only sub-VLANs on user ports need to be applied. 23203f01c91aSVladimir Oltean * Bridge VLANs also include VLANs added automatically 23213f01c91aSVladimir Oltean * by DSA on the CPU port. 23223f01c91aSVladimir Oltean */ 23233f01c91aSVladimir Oltean if (!dsa_is_user_port(priv->ds, v->port)) 23243f01c91aSVladimir Oltean continue; 23253f01c91aSVladimir Oltean 23263f01c91aSVladimir Oltean subvlan = sja1105_find_subvlan(subvlan_map[v->port], 23273f01c91aSVladimir Oltean v->vid); 23283f01c91aSVladimir Oltean if (subvlan < 0) { 23293f01c91aSVladimir Oltean subvlan = sja1105_find_free_subvlan(subvlan_map[v->port], 23303f01c91aSVladimir Oltean v->pvid); 23313f01c91aSVladimir Oltean if (subvlan < 0) { 23323f01c91aSVladimir Oltean dev_err(priv->ds->dev, "No more free subvlans\n"); 23333f01c91aSVladimir Oltean return -ENOSPC; 23343f01c91aSVladimir Oltean } 23353f01c91aSVladimir Oltean } 23363f01c91aSVladimir Oltean 23373f01c91aSVladimir Oltean rx_vid = dsa_8021q_rx_vid_subvlan(priv->ds, v->port, subvlan); 23383f01c91aSVladimir Oltean 23393f01c91aSVladimir Oltean /* @v->vid on @v->port needs to be retagged to @rx_vid 23403f01c91aSVladimir Oltean * on @upstream. Assume @v->vid on @v->port and on 23413f01c91aSVladimir Oltean * @upstream was already configured by the previous 23423f01c91aSVladimir Oltean * iteration over bridge_vlans. 23433f01c91aSVladimir Oltean */ 23443f01c91aSVladimir Oltean match = rx_vid; 23453f01c91aSVladimir Oltean new_vlan[match].vlanid = rx_vid; 23463f01c91aSVladimir Oltean new_vlan[match].vmemb_port |= BIT(v->port); 23473f01c91aSVladimir Oltean new_vlan[match].vmemb_port |= BIT(upstream); 23483f01c91aSVladimir Oltean new_vlan[match].vlan_bc |= BIT(v->port); 23493f01c91aSVladimir Oltean new_vlan[match].vlan_bc |= BIT(upstream); 23503f01c91aSVladimir Oltean /* The "untagged" flag is set the same as for the 23513f01c91aSVladimir Oltean * original VLAN 23523f01c91aSVladimir Oltean */ 23533f01c91aSVladimir Oltean if (!v->untagged) 23543f01c91aSVladimir Oltean new_vlan[match].tag_port |= BIT(v->port); 23553f01c91aSVladimir Oltean /* But it's always tagged towards the CPU */ 23563f01c91aSVladimir Oltean new_vlan[match].tag_port |= BIT(upstream); 23573f01c91aSVladimir Oltean 23583f01c91aSVladimir Oltean /* The Retagging Table generates packet *clones* with 23593f01c91aSVladimir Oltean * the new VLAN. This is a very odd hardware quirk 23603f01c91aSVladimir Oltean * which we need to suppress by dropping the original 23613f01c91aSVladimir Oltean * packet. 23623f01c91aSVladimir Oltean * Deny egress of the original VLAN towards the CPU 23633f01c91aSVladimir Oltean * port. This will force the switch to drop it, and 23643f01c91aSVladimir Oltean * we'll see only the retagged packets. 23653f01c91aSVladimir Oltean */ 23663f01c91aSVladimir Oltean match = v->vid; 23673f01c91aSVladimir Oltean new_vlan[match].vlan_bc &= ~BIT(upstream); 23683f01c91aSVladimir Oltean 23693f01c91aSVladimir Oltean /* And the retagging itself */ 23703f01c91aSVladimir Oltean new_retagging[k].vlan_ing = v->vid; 23713f01c91aSVladimir Oltean new_retagging[k].vlan_egr = rx_vid; 23723f01c91aSVladimir Oltean new_retagging[k].ing_port = BIT(v->port); 23733f01c91aSVladimir Oltean new_retagging[k].egr_port = BIT(upstream); 23743f01c91aSVladimir Oltean if (k++ == SJA1105_MAX_RETAGGING_COUNT) { 23753f01c91aSVladimir Oltean dev_err(priv->ds->dev, "No more retagging rules\n"); 23763f01c91aSVladimir Oltean return -ENOSPC; 23773f01c91aSVladimir Oltean } 23783f01c91aSVladimir Oltean 23793f01c91aSVladimir Oltean subvlan_map[v->port][subvlan] = v->vid; 23803f01c91aSVladimir Oltean } 23813f01c91aSVladimir Oltean 23823f01c91aSVladimir Oltean *num_retagging = k; 23833f01c91aSVladimir Oltean 23843f01c91aSVladimir Oltean return 0; 23853f01c91aSVladimir Oltean } 23863f01c91aSVladimir Oltean 23873f01c91aSVladimir Oltean /* Sadly, in crosschip scenarios where the CPU port is also the link to another 23883f01c91aSVladimir Oltean * switch, we should retag backwards (the dsa_8021q vid to the original vid) on 23893f01c91aSVladimir Oltean * the CPU port of neighbour switches. 23903f01c91aSVladimir Oltean */ 23913f01c91aSVladimir Oltean static int 23923f01c91aSVladimir Oltean sja1105_build_crosschip_subvlans(struct sja1105_private *priv, 23933f01c91aSVladimir Oltean struct sja1105_vlan_lookup_entry *new_vlan, 23943f01c91aSVladimir Oltean struct sja1105_retagging_entry *new_retagging, 23953f01c91aSVladimir Oltean int *num_retagging) 23963f01c91aSVladimir Oltean { 23973f01c91aSVladimir Oltean struct sja1105_crosschip_vlan *tmp, *pos; 23983f01c91aSVladimir Oltean struct dsa_8021q_crosschip_link *c; 23993f01c91aSVladimir Oltean struct sja1105_bridge_vlan *v, *w; 24003f01c91aSVladimir Oltean struct list_head crosschip_vlans; 24013f01c91aSVladimir Oltean int k = *num_retagging; 24023f01c91aSVladimir Oltean int rc = 0; 24033f01c91aSVladimir Oltean 24043f01c91aSVladimir Oltean if (priv->vlan_state != SJA1105_VLAN_BEST_EFFORT) 24053f01c91aSVladimir Oltean return 0; 24063f01c91aSVladimir Oltean 24073f01c91aSVladimir Oltean INIT_LIST_HEAD(&crosschip_vlans); 24083f01c91aSVladimir Oltean 24095899ee36SVladimir Oltean list_for_each_entry(c, &priv->dsa_8021q_ctx->crosschip_links, list) { 24105899ee36SVladimir Oltean struct sja1105_private *other_priv = c->other_ctx->ds->priv; 24113f01c91aSVladimir Oltean 24123f01c91aSVladimir Oltean if (other_priv->vlan_state == SJA1105_VLAN_FILTERING_FULL) 24133f01c91aSVladimir Oltean continue; 24143f01c91aSVladimir Oltean 24153f01c91aSVladimir Oltean /* Crosschip links are also added to the CPU ports. 24163f01c91aSVladimir Oltean * Ignore those. 24173f01c91aSVladimir Oltean */ 24183f01c91aSVladimir Oltean if (!dsa_is_user_port(priv->ds, c->port)) 24193f01c91aSVladimir Oltean continue; 24205899ee36SVladimir Oltean if (!dsa_is_user_port(c->other_ctx->ds, c->other_port)) 24213f01c91aSVladimir Oltean continue; 24223f01c91aSVladimir Oltean 24233f01c91aSVladimir Oltean /* Search for VLANs on the remote port */ 24243f01c91aSVladimir Oltean list_for_each_entry(v, &other_priv->bridge_vlans, list) { 24253f01c91aSVladimir Oltean bool already_added = false; 24263f01c91aSVladimir Oltean bool we_have_it = false; 24273f01c91aSVladimir Oltean 24283f01c91aSVladimir Oltean if (v->port != c->other_port) 24293f01c91aSVladimir Oltean continue; 24303f01c91aSVladimir Oltean 24313f01c91aSVladimir Oltean /* If @v is a pvid on @other_ds, it does not need 24323f01c91aSVladimir Oltean * re-retagging, because its SVL field is 0 and we 24333f01c91aSVladimir Oltean * already allow that, via the dsa_8021q crosschip 24343f01c91aSVladimir Oltean * links. 24353f01c91aSVladimir Oltean */ 24363f01c91aSVladimir Oltean if (v->pvid) 24373f01c91aSVladimir Oltean continue; 24383f01c91aSVladimir Oltean 24393f01c91aSVladimir Oltean /* Search for the VLAN on our local port */ 24403f01c91aSVladimir Oltean list_for_each_entry(w, &priv->bridge_vlans, list) { 24413f01c91aSVladimir Oltean if (w->port == c->port && w->vid == v->vid) { 24423f01c91aSVladimir Oltean we_have_it = true; 24433f01c91aSVladimir Oltean break; 24443f01c91aSVladimir Oltean } 24453f01c91aSVladimir Oltean } 24463f01c91aSVladimir Oltean 24473f01c91aSVladimir Oltean if (!we_have_it) 24483f01c91aSVladimir Oltean continue; 24493f01c91aSVladimir Oltean 24503f01c91aSVladimir Oltean list_for_each_entry(tmp, &crosschip_vlans, list) { 24513f01c91aSVladimir Oltean if (tmp->vid == v->vid && 24523f01c91aSVladimir Oltean tmp->untagged == v->untagged && 24533f01c91aSVladimir Oltean tmp->port == c->port && 24543f01c91aSVladimir Oltean tmp->other_port == v->port && 24555899ee36SVladimir Oltean tmp->other_ctx == c->other_ctx) { 24563f01c91aSVladimir Oltean already_added = true; 24573f01c91aSVladimir Oltean break; 24583f01c91aSVladimir Oltean } 24593f01c91aSVladimir Oltean } 24603f01c91aSVladimir Oltean 24613f01c91aSVladimir Oltean if (already_added) 24623f01c91aSVladimir Oltean continue; 24633f01c91aSVladimir Oltean 24643f01c91aSVladimir Oltean tmp = kzalloc(sizeof(*tmp), GFP_KERNEL); 24653f01c91aSVladimir Oltean if (!tmp) { 24663f01c91aSVladimir Oltean dev_err(priv->ds->dev, "Failed to allocate memory\n"); 24673f01c91aSVladimir Oltean rc = -ENOMEM; 24683f01c91aSVladimir Oltean goto out; 24693f01c91aSVladimir Oltean } 24703f01c91aSVladimir Oltean tmp->vid = v->vid; 24713f01c91aSVladimir Oltean tmp->port = c->port; 24723f01c91aSVladimir Oltean tmp->other_port = v->port; 24735899ee36SVladimir Oltean tmp->other_ctx = c->other_ctx; 24743f01c91aSVladimir Oltean tmp->untagged = v->untagged; 24753f01c91aSVladimir Oltean list_add(&tmp->list, &crosschip_vlans); 24763f01c91aSVladimir Oltean } 24773f01c91aSVladimir Oltean } 24783f01c91aSVladimir Oltean 24793f01c91aSVladimir Oltean list_for_each_entry(tmp, &crosschip_vlans, list) { 24805899ee36SVladimir Oltean struct sja1105_private *other_priv = tmp->other_ctx->ds->priv; 24813f01c91aSVladimir Oltean int upstream = dsa_upstream_port(priv->ds, tmp->port); 24823f01c91aSVladimir Oltean int match, subvlan; 24833f01c91aSVladimir Oltean u16 rx_vid; 24843f01c91aSVladimir Oltean 24853f01c91aSVladimir Oltean subvlan = sja1105_find_committed_subvlan(other_priv, 24863f01c91aSVladimir Oltean tmp->other_port, 24873f01c91aSVladimir Oltean tmp->vid); 24883f01c91aSVladimir Oltean /* If this happens, it's a bug. The neighbour switch does not 24893f01c91aSVladimir Oltean * have a subvlan for tmp->vid on tmp->other_port, but it 24903f01c91aSVladimir Oltean * should, since we already checked for its vlan_state. 24913f01c91aSVladimir Oltean */ 24923f01c91aSVladimir Oltean if (WARN_ON(subvlan < 0)) { 24933f01c91aSVladimir Oltean rc = -EINVAL; 24943f01c91aSVladimir Oltean goto out; 24953f01c91aSVladimir Oltean } 24963f01c91aSVladimir Oltean 24975899ee36SVladimir Oltean rx_vid = dsa_8021q_rx_vid_subvlan(tmp->other_ctx->ds, 24983f01c91aSVladimir Oltean tmp->other_port, 24993f01c91aSVladimir Oltean subvlan); 25003f01c91aSVladimir Oltean 25013f01c91aSVladimir Oltean /* The @rx_vid retagged from @tmp->vid on 25023f01c91aSVladimir Oltean * {@tmp->other_ds, @tmp->other_port} needs to be 25033f01c91aSVladimir Oltean * re-retagged to @tmp->vid on the way back to us. 25043f01c91aSVladimir Oltean * 25053f01c91aSVladimir Oltean * Assume the original @tmp->vid is already configured 25063f01c91aSVladimir Oltean * on this local switch, otherwise we wouldn't be 25073f01c91aSVladimir Oltean * retagging its subvlan on the other switch in the 25083f01c91aSVladimir Oltean * first place. We just need to add a reverse retagging 25093f01c91aSVladimir Oltean * rule for @rx_vid and install @rx_vid on our ports. 25103f01c91aSVladimir Oltean */ 25113f01c91aSVladimir Oltean match = rx_vid; 25123f01c91aSVladimir Oltean new_vlan[match].vlanid = rx_vid; 25133f01c91aSVladimir Oltean new_vlan[match].vmemb_port |= BIT(tmp->port); 25143f01c91aSVladimir Oltean new_vlan[match].vmemb_port |= BIT(upstream); 25153f01c91aSVladimir Oltean /* The "untagged" flag is set the same as for the 25163f01c91aSVladimir Oltean * original VLAN. And towards the CPU, it doesn't 25173f01c91aSVladimir Oltean * really matter, because @rx_vid will only receive 25183f01c91aSVladimir Oltean * traffic on that port. For consistency with other dsa_8021q 25193f01c91aSVladimir Oltean * VLANs, we'll keep the CPU port tagged. 25203f01c91aSVladimir Oltean */ 25213f01c91aSVladimir Oltean if (!tmp->untagged) 25223f01c91aSVladimir Oltean new_vlan[match].tag_port |= BIT(tmp->port); 25233f01c91aSVladimir Oltean new_vlan[match].tag_port |= BIT(upstream); 25243f01c91aSVladimir Oltean /* Deny egress of @rx_vid towards our front-panel port. 25253f01c91aSVladimir Oltean * This will force the switch to drop it, and we'll see 25263f01c91aSVladimir Oltean * only the re-retagged packets (having the original, 25273f01c91aSVladimir Oltean * pre-initial-retagging, VLAN @tmp->vid). 25283f01c91aSVladimir Oltean */ 25293f01c91aSVladimir Oltean new_vlan[match].vlan_bc &= ~BIT(tmp->port); 25303f01c91aSVladimir Oltean 25313f01c91aSVladimir Oltean /* On reverse retagging, the same ingress VLAN goes to multiple 25323f01c91aSVladimir Oltean * ports. So we have an opportunity to create composite rules 25333f01c91aSVladimir Oltean * to not waste the limited space in the retagging table. 25343f01c91aSVladimir Oltean */ 25353f01c91aSVladimir Oltean k = sja1105_find_retagging_entry(new_retagging, *num_retagging, 25363f01c91aSVladimir Oltean upstream, rx_vid, tmp->vid); 25373f01c91aSVladimir Oltean if (k < 0) { 25383f01c91aSVladimir Oltean if (*num_retagging == SJA1105_MAX_RETAGGING_COUNT) { 25393f01c91aSVladimir Oltean dev_err(priv->ds->dev, "No more retagging rules\n"); 25403f01c91aSVladimir Oltean rc = -ENOSPC; 25413f01c91aSVladimir Oltean goto out; 25423f01c91aSVladimir Oltean } 25433f01c91aSVladimir Oltean k = (*num_retagging)++; 25443f01c91aSVladimir Oltean } 25453f01c91aSVladimir Oltean /* And the retagging itself */ 25463f01c91aSVladimir Oltean new_retagging[k].vlan_ing = rx_vid; 25473f01c91aSVladimir Oltean new_retagging[k].vlan_egr = tmp->vid; 25483f01c91aSVladimir Oltean new_retagging[k].ing_port = BIT(upstream); 25493f01c91aSVladimir Oltean new_retagging[k].egr_port |= BIT(tmp->port); 25503f01c91aSVladimir Oltean } 25513f01c91aSVladimir Oltean 25523f01c91aSVladimir Oltean out: 25533f01c91aSVladimir Oltean list_for_each_entry_safe(tmp, pos, &crosschip_vlans, list) { 25543f01c91aSVladimir Oltean list_del(&tmp->list); 25553f01c91aSVladimir Oltean kfree(tmp); 25563f01c91aSVladimir Oltean } 25573f01c91aSVladimir Oltean 25583f01c91aSVladimir Oltean return rc; 25593f01c91aSVladimir Oltean } 25603f01c91aSVladimir Oltean 2561ec5ae610SVladimir Oltean static int sja1105_build_vlan_table(struct sja1105_private *priv, bool notify); 2562ec5ae610SVladimir Oltean 2563ec5ae610SVladimir Oltean static int sja1105_notify_crosschip_switches(struct sja1105_private *priv) 2564ec5ae610SVladimir Oltean { 2565ec5ae610SVladimir Oltean struct sja1105_crosschip_switch *s, *pos; 2566ec5ae610SVladimir Oltean struct list_head crosschip_switches; 2567ec5ae610SVladimir Oltean struct dsa_8021q_crosschip_link *c; 2568ec5ae610SVladimir Oltean int rc = 0; 2569ec5ae610SVladimir Oltean 2570ec5ae610SVladimir Oltean INIT_LIST_HEAD(&crosschip_switches); 2571ec5ae610SVladimir Oltean 25725899ee36SVladimir Oltean list_for_each_entry(c, &priv->dsa_8021q_ctx->crosschip_links, list) { 2573ec5ae610SVladimir Oltean bool already_added = false; 2574ec5ae610SVladimir Oltean 2575ec5ae610SVladimir Oltean list_for_each_entry(s, &crosschip_switches, list) { 25765899ee36SVladimir Oltean if (s->other_ctx == c->other_ctx) { 2577ec5ae610SVladimir Oltean already_added = true; 2578ec5ae610SVladimir Oltean break; 2579ec5ae610SVladimir Oltean } 2580ec5ae610SVladimir Oltean } 2581ec5ae610SVladimir Oltean 2582ec5ae610SVladimir Oltean if (already_added) 2583ec5ae610SVladimir Oltean continue; 2584ec5ae610SVladimir Oltean 2585ec5ae610SVladimir Oltean s = kzalloc(sizeof(*s), GFP_KERNEL); 2586ec5ae610SVladimir Oltean if (!s) { 2587ec5ae610SVladimir Oltean dev_err(priv->ds->dev, "Failed to allocate memory\n"); 2588ec5ae610SVladimir Oltean rc = -ENOMEM; 2589ec5ae610SVladimir Oltean goto out; 2590ec5ae610SVladimir Oltean } 25915899ee36SVladimir Oltean s->other_ctx = c->other_ctx; 2592ec5ae610SVladimir Oltean list_add(&s->list, &crosschip_switches); 2593ec5ae610SVladimir Oltean } 2594ec5ae610SVladimir Oltean 2595ec5ae610SVladimir Oltean list_for_each_entry(s, &crosschip_switches, list) { 25965899ee36SVladimir Oltean struct sja1105_private *other_priv = s->other_ctx->ds->priv; 2597ec5ae610SVladimir Oltean 2598ec5ae610SVladimir Oltean rc = sja1105_build_vlan_table(other_priv, false); 2599ec5ae610SVladimir Oltean if (rc) 2600ec5ae610SVladimir Oltean goto out; 2601ec5ae610SVladimir Oltean } 2602ec5ae610SVladimir Oltean 2603ec5ae610SVladimir Oltean out: 2604ec5ae610SVladimir Oltean list_for_each_entry_safe(s, pos, &crosschip_switches, list) { 2605ec5ae610SVladimir Oltean list_del(&s->list); 2606ec5ae610SVladimir Oltean kfree(s); 2607ec5ae610SVladimir Oltean } 2608ec5ae610SVladimir Oltean 2609ec5ae610SVladimir Oltean return rc; 2610ec5ae610SVladimir Oltean } 2611ec5ae610SVladimir Oltean 2612ec5ae610SVladimir Oltean static int sja1105_build_vlan_table(struct sja1105_private *priv, bool notify) 2613ec5ae610SVladimir Oltean { 26143f01c91aSVladimir Oltean u16 subvlan_map[SJA1105_NUM_PORTS][DSA_8021Q_N_SUBVLAN]; 26153f01c91aSVladimir Oltean struct sja1105_retagging_entry *new_retagging; 2616ec5ae610SVladimir Oltean struct sja1105_vlan_lookup_entry *new_vlan; 2617ec5ae610SVladimir Oltean struct sja1105_table *table; 26183f01c91aSVladimir Oltean int i, num_retagging = 0; 2619ec5ae610SVladimir Oltean int rc; 2620ec5ae610SVladimir Oltean 2621ec5ae610SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP]; 2622ec5ae610SVladimir Oltean new_vlan = kcalloc(VLAN_N_VID, 2623ec5ae610SVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 2624ec5ae610SVladimir Oltean if (!new_vlan) 2625ec5ae610SVladimir Oltean return -ENOMEM; 2626ec5ae610SVladimir Oltean 26273f01c91aSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP]; 26283f01c91aSVladimir Oltean new_retagging = kcalloc(SJA1105_MAX_RETAGGING_COUNT, 26293f01c91aSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 26303f01c91aSVladimir Oltean if (!new_retagging) { 26313f01c91aSVladimir Oltean kfree(new_vlan); 26323f01c91aSVladimir Oltean return -ENOMEM; 26333f01c91aSVladimir Oltean } 26343f01c91aSVladimir Oltean 2635ec5ae610SVladimir Oltean for (i = 0; i < VLAN_N_VID; i++) 2636ec5ae610SVladimir Oltean new_vlan[i].vlanid = VLAN_N_VID; 2637ec5ae610SVladimir Oltean 26383f01c91aSVladimir Oltean for (i = 0; i < SJA1105_MAX_RETAGGING_COUNT; i++) 26393f01c91aSVladimir Oltean new_retagging[i].vlan_ing = VLAN_N_VID; 26403f01c91aSVladimir Oltean 26413f01c91aSVladimir Oltean for (i = 0; i < priv->ds->num_ports; i++) 26423f01c91aSVladimir Oltean sja1105_init_subvlan_map(subvlan_map[i]); 26433f01c91aSVladimir Oltean 2644ec5ae610SVladimir Oltean /* Bridge VLANs */ 2645ec5ae610SVladimir Oltean rc = sja1105_build_bridge_vlans(priv, new_vlan); 2646ec5ae610SVladimir Oltean if (rc) 2647ec5ae610SVladimir Oltean goto out; 2648ec5ae610SVladimir Oltean 2649ec5ae610SVladimir Oltean /* VLANs necessary for dsa_8021q operation, given to us by tag_8021q.c: 2650ec5ae610SVladimir Oltean * - RX VLANs 2651ec5ae610SVladimir Oltean * - TX VLANs 2652ec5ae610SVladimir Oltean * - Crosschip links 2653ec5ae610SVladimir Oltean */ 2654ec5ae610SVladimir Oltean rc = sja1105_build_dsa_8021q_vlans(priv, new_vlan); 2655ec5ae610SVladimir Oltean if (rc) 2656ec5ae610SVladimir Oltean goto out; 2657ec5ae610SVladimir Oltean 26583f01c91aSVladimir Oltean /* Private VLANs necessary for dsa_8021q operation, which we need to 26593f01c91aSVladimir Oltean * determine on our own: 26603f01c91aSVladimir Oltean * - Sub-VLANs 26613f01c91aSVladimir Oltean * - Sub-VLANs of crosschip switches 26623f01c91aSVladimir Oltean */ 26633f01c91aSVladimir Oltean rc = sja1105_build_subvlans(priv, subvlan_map, new_vlan, new_retagging, 26643f01c91aSVladimir Oltean &num_retagging); 26653f01c91aSVladimir Oltean if (rc) 26663f01c91aSVladimir Oltean goto out; 26673f01c91aSVladimir Oltean 26683f01c91aSVladimir Oltean rc = sja1105_build_crosschip_subvlans(priv, new_vlan, new_retagging, 26693f01c91aSVladimir Oltean &num_retagging); 26703f01c91aSVladimir Oltean if (rc) 26713f01c91aSVladimir Oltean goto out; 26723f01c91aSVladimir Oltean 26733f01c91aSVladimir Oltean rc = sja1105_commit_vlans(priv, new_vlan, new_retagging, num_retagging); 2674ec5ae610SVladimir Oltean if (rc) 2675ec5ae610SVladimir Oltean goto out; 2676ec5ae610SVladimir Oltean 2677ec5ae610SVladimir Oltean rc = sja1105_commit_pvid(priv); 2678ec5ae610SVladimir Oltean if (rc) 2679ec5ae610SVladimir Oltean goto out; 2680ec5ae610SVladimir Oltean 26813f01c91aSVladimir Oltean for (i = 0; i < priv->ds->num_ports; i++) 26823f01c91aSVladimir Oltean sja1105_commit_subvlan_map(priv, i, subvlan_map[i]); 26833f01c91aSVladimir Oltean 2684ec5ae610SVladimir Oltean if (notify) { 2685ec5ae610SVladimir Oltean rc = sja1105_notify_crosschip_switches(priv); 2686ec5ae610SVladimir Oltean if (rc) 2687ec5ae610SVladimir Oltean goto out; 2688ec5ae610SVladimir Oltean } 2689ec5ae610SVladimir Oltean 2690ec5ae610SVladimir Oltean out: 2691ec5ae610SVladimir Oltean kfree(new_vlan); 26923f01c91aSVladimir Oltean kfree(new_retagging); 2693ec5ae610SVladimir Oltean 2694ec5ae610SVladimir Oltean return rc; 2695ec5ae610SVladimir Oltean } 2696ec5ae610SVladimir Oltean 2697070ca3bbSVladimir Oltean /* The TPID setting belongs to the General Parameters table, 2698070ca3bbSVladimir Oltean * which can only be partially reconfigured at runtime (and not the TPID). 2699070ca3bbSVladimir Oltean * So a switch reset is required. 2700070ca3bbSVladimir Oltean */ 270189153ed6SVladimir Oltean int sja1105_vlan_filtering(struct dsa_switch *ds, int port, bool enabled, 270289153ed6SVladimir Oltean struct netlink_ext_ack *extack) 27036666cebcSVladimir Oltean { 27046d7c7d94SVladimir Oltean struct sja1105_l2_lookup_params_entry *l2_lookup_params; 2705070ca3bbSVladimir Oltean struct sja1105_general_params_entry *general_params; 27066666cebcSVladimir Oltean struct sja1105_private *priv = ds->priv; 27077f14937fSVladimir Oltean enum sja1105_vlan_state state; 2708070ca3bbSVladimir Oltean struct sja1105_table *table; 2709dfacc5a2SVladimir Oltean struct sja1105_rule *rule; 27102cafa72eSVladimir Oltean bool want_tagging; 2711070ca3bbSVladimir Oltean u16 tpid, tpid2; 27126666cebcSVladimir Oltean int rc; 27136666cebcSVladimir Oltean 2714dfacc5a2SVladimir Oltean list_for_each_entry(rule, &priv->flow_block.rules, list) { 2715dfacc5a2SVladimir Oltean if (rule->type == SJA1105_RULE_VL) { 271689153ed6SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 271789153ed6SVladimir Oltean "Cannot change VLAN filtering with active VL rules"); 2718dfacc5a2SVladimir Oltean return -EBUSY; 2719dfacc5a2SVladimir Oltean } 2720dfacc5a2SVladimir Oltean } 2721dfacc5a2SVladimir Oltean 2722070ca3bbSVladimir Oltean if (enabled) { 27236666cebcSVladimir Oltean /* Enable VLAN filtering. */ 272454fa49eeSVladimir Oltean tpid = ETH_P_8021Q; 272554fa49eeSVladimir Oltean tpid2 = ETH_P_8021AD; 2726070ca3bbSVladimir Oltean } else { 27276666cebcSVladimir Oltean /* Disable VLAN filtering. */ 2728070ca3bbSVladimir Oltean tpid = ETH_P_SJA1105; 2729070ca3bbSVladimir Oltean tpid2 = ETH_P_SJA1105; 2730070ca3bbSVladimir Oltean } 2731070ca3bbSVladimir Oltean 273238b5beeaSVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 273338b5beeaSVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 273438b5beeaSVladimir Oltean 273538b5beeaSVladimir Oltean if (enabled) 273638b5beeaSVladimir Oltean sp->xmit_tpid = priv->info->qinq_tpid; 273738b5beeaSVladimir Oltean else 273838b5beeaSVladimir Oltean sp->xmit_tpid = ETH_P_SJA1105; 273938b5beeaSVladimir Oltean } 274038b5beeaSVladimir Oltean 27417f14937fSVladimir Oltean if (!enabled) 27427f14937fSVladimir Oltean state = SJA1105_VLAN_UNAWARE; 27432cafa72eSVladimir Oltean else if (priv->best_effort_vlan_filtering) 27442cafa72eSVladimir Oltean state = SJA1105_VLAN_BEST_EFFORT; 27457f14937fSVladimir Oltean else 27467f14937fSVladimir Oltean state = SJA1105_VLAN_FILTERING_FULL; 27477f14937fSVladimir Oltean 2748cfa36b1fSVladimir Oltean if (priv->vlan_state == state) 2749cfa36b1fSVladimir Oltean return 0; 2750cfa36b1fSVladimir Oltean 27517f14937fSVladimir Oltean priv->vlan_state = state; 27522cafa72eSVladimir Oltean want_tagging = (state == SJA1105_VLAN_UNAWARE || 27532cafa72eSVladimir Oltean state == SJA1105_VLAN_BEST_EFFORT); 27547f14937fSVladimir Oltean 2755070ca3bbSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; 2756070ca3bbSVladimir Oltean general_params = table->entries; 2757f9a1a764SVladimir Oltean /* EtherType used to identify inner tagged (C-tag) VLAN traffic */ 275854fa49eeSVladimir Oltean general_params->tpid = tpid; 275954fa49eeSVladimir Oltean /* EtherType used to identify outer tagged (S-tag) VLAN traffic */ 2760070ca3bbSVladimir Oltean general_params->tpid2 = tpid2; 276142824463SVladimir Oltean /* When VLAN filtering is on, we need to at least be able to 276242824463SVladimir Oltean * decode management traffic through the "backup plan". 276342824463SVladimir Oltean */ 276442824463SVladimir Oltean general_params->incl_srcpt1 = enabled; 276542824463SVladimir Oltean general_params->incl_srcpt0 = enabled; 2766070ca3bbSVladimir Oltean 27672cafa72eSVladimir Oltean want_tagging = priv->best_effort_vlan_filtering || !enabled; 27682cafa72eSVladimir Oltean 27696d7c7d94SVladimir Oltean /* VLAN filtering => independent VLAN learning. 27702cafa72eSVladimir Oltean * No VLAN filtering (or best effort) => shared VLAN learning. 27716d7c7d94SVladimir Oltean * 27726d7c7d94SVladimir Oltean * In shared VLAN learning mode, untagged traffic still gets 27736d7c7d94SVladimir Oltean * pvid-tagged, and the FDB table gets populated with entries 27746d7c7d94SVladimir Oltean * containing the "real" (pvid or from VLAN tag) VLAN ID. 27756d7c7d94SVladimir Oltean * However the switch performs a masked L2 lookup in the FDB, 27766d7c7d94SVladimir Oltean * effectively only looking up a frame's DMAC (and not VID) for the 27776d7c7d94SVladimir Oltean * forwarding decision. 27786d7c7d94SVladimir Oltean * 27796d7c7d94SVladimir Oltean * This is extremely convenient for us, because in modes with 27806d7c7d94SVladimir Oltean * vlan_filtering=0, dsa_8021q actually installs unique pvid's into 27816d7c7d94SVladimir Oltean * each front panel port. This is good for identification but breaks 27826d7c7d94SVladimir Oltean * learning badly - the VID of the learnt FDB entry is unique, aka 27836d7c7d94SVladimir Oltean * no frames coming from any other port are going to have it. So 27846d7c7d94SVladimir Oltean * for forwarding purposes, this is as though learning was broken 27856d7c7d94SVladimir Oltean * (all frames get flooded). 27866d7c7d94SVladimir Oltean */ 27876d7c7d94SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS]; 27886d7c7d94SVladimir Oltean l2_lookup_params = table->entries; 27892cafa72eSVladimir Oltean l2_lookup_params->shared_learn = want_tagging; 27906d7c7d94SVladimir Oltean 2791aaa270c6SVladimir Oltean sja1105_frame_memory_partitioning(priv); 2792aaa270c6SVladimir Oltean 2793aef31718SVladimir Oltean rc = sja1105_build_vlan_table(priv, false); 2794aef31718SVladimir Oltean if (rc) 2795aef31718SVladimir Oltean return rc; 2796aef31718SVladimir Oltean 27972eea1fa8SVladimir Oltean rc = sja1105_static_config_reload(priv, SJA1105_VLAN_FILTERING); 27986666cebcSVladimir Oltean if (rc) 279989153ed6SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, "Failed to change VLAN Ethertype"); 28006666cebcSVladimir Oltean 2801227d07a0SVladimir Oltean /* Switch port identification based on 802.1Q is only passable 2802227d07a0SVladimir Oltean * if we are not under a vlan_filtering bridge. So make sure 28032cafa72eSVladimir Oltean * the two configurations are mutually exclusive (of course, the 28042cafa72eSVladimir Oltean * user may know better, i.e. best_effort_vlan_filtering). 2805227d07a0SVladimir Oltean */ 28062cafa72eSVladimir Oltean return sja1105_setup_8021q_tagging(ds, want_tagging); 28076666cebcSVladimir Oltean } 28086666cebcSVladimir Oltean 28095899ee36SVladimir Oltean /* Returns number of VLANs added (0 or 1) on success, 28105899ee36SVladimir Oltean * or a negative error code. 28115899ee36SVladimir Oltean */ 28125899ee36SVladimir Oltean static int sja1105_vlan_add_one(struct dsa_switch *ds, int port, u16 vid, 28135899ee36SVladimir Oltean u16 flags, struct list_head *vlan_list) 28145899ee36SVladimir Oltean { 28155899ee36SVladimir Oltean bool untagged = flags & BRIDGE_VLAN_INFO_UNTAGGED; 28165899ee36SVladimir Oltean bool pvid = flags & BRIDGE_VLAN_INFO_PVID; 28175899ee36SVladimir Oltean struct sja1105_bridge_vlan *v; 28185899ee36SVladimir Oltean 28195899ee36SVladimir Oltean list_for_each_entry(v, vlan_list, list) 28205899ee36SVladimir Oltean if (v->port == port && v->vid == vid && 28215899ee36SVladimir Oltean v->untagged == untagged && v->pvid == pvid) 28225899ee36SVladimir Oltean /* Already added */ 28235899ee36SVladimir Oltean return 0; 28245899ee36SVladimir Oltean 28255899ee36SVladimir Oltean v = kzalloc(sizeof(*v), GFP_KERNEL); 28265899ee36SVladimir Oltean if (!v) { 28275899ee36SVladimir Oltean dev_err(ds->dev, "Out of memory while storing VLAN\n"); 28285899ee36SVladimir Oltean return -ENOMEM; 28295899ee36SVladimir Oltean } 28305899ee36SVladimir Oltean 28315899ee36SVladimir Oltean v->port = port; 28325899ee36SVladimir Oltean v->vid = vid; 28335899ee36SVladimir Oltean v->untagged = untagged; 28345899ee36SVladimir Oltean v->pvid = pvid; 28355899ee36SVladimir Oltean list_add(&v->list, vlan_list); 28365899ee36SVladimir Oltean 28375899ee36SVladimir Oltean return 1; 28385899ee36SVladimir Oltean } 28395899ee36SVladimir Oltean 28405899ee36SVladimir Oltean /* Returns number of VLANs deleted (0 or 1) */ 28415899ee36SVladimir Oltean static int sja1105_vlan_del_one(struct dsa_switch *ds, int port, u16 vid, 28425899ee36SVladimir Oltean struct list_head *vlan_list) 28435899ee36SVladimir Oltean { 28445899ee36SVladimir Oltean struct sja1105_bridge_vlan *v, *n; 28455899ee36SVladimir Oltean 28465899ee36SVladimir Oltean list_for_each_entry_safe(v, n, vlan_list, list) { 28475899ee36SVladimir Oltean if (v->port == port && v->vid == vid) { 28485899ee36SVladimir Oltean list_del(&v->list); 28495899ee36SVladimir Oltean kfree(v); 28505899ee36SVladimir Oltean return 1; 28515899ee36SVladimir Oltean } 28525899ee36SVladimir Oltean } 28535899ee36SVladimir Oltean 28545899ee36SVladimir Oltean return 0; 28555899ee36SVladimir Oltean } 28565899ee36SVladimir Oltean 28571958d581SVladimir Oltean static int sja1105_vlan_add(struct dsa_switch *ds, int port, 285831046a5fSVladimir Oltean const struct switchdev_obj_port_vlan *vlan, 285931046a5fSVladimir Oltean struct netlink_ext_ack *extack) 28606666cebcSVladimir Oltean { 28616666cebcSVladimir Oltean struct sja1105_private *priv = ds->priv; 2862ec5ae610SVladimir Oltean bool vlan_table_changed = false; 28636666cebcSVladimir Oltean int rc; 28646666cebcSVladimir Oltean 28651958d581SVladimir Oltean /* If the user wants best-effort VLAN filtering (aka vlan_filtering 28661958d581SVladimir Oltean * bridge plus tagging), be sure to at least deny alterations to the 28671958d581SVladimir Oltean * configuration done by dsa_8021q. 28681958d581SVladimir Oltean */ 28691958d581SVladimir Oltean if (priv->vlan_state != SJA1105_VLAN_FILTERING_FULL && 28701958d581SVladimir Oltean vid_is_dsa_8021q(vlan->vid)) { 287131046a5fSVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 287231046a5fSVladimir Oltean "Range 1024-3071 reserved for dsa_8021q operation"); 28731958d581SVladimir Oltean return -EBUSY; 28741958d581SVladimir Oltean } 28751958d581SVladimir Oltean 2876b7a9e0daSVladimir Oltean rc = sja1105_vlan_add_one(ds, port, vlan->vid, vlan->flags, 28775899ee36SVladimir Oltean &priv->bridge_vlans); 28785899ee36SVladimir Oltean if (rc < 0) 28791958d581SVladimir Oltean return rc; 28805899ee36SVladimir Oltean if (rc > 0) 2881ec5ae610SVladimir Oltean vlan_table_changed = true; 2882ec5ae610SVladimir Oltean 2883ec5ae610SVladimir Oltean if (!vlan_table_changed) 28841958d581SVladimir Oltean return 0; 2885ec5ae610SVladimir Oltean 28861958d581SVladimir Oltean return sja1105_build_vlan_table(priv, true); 28876666cebcSVladimir Oltean } 28886666cebcSVladimir Oltean 28896666cebcSVladimir Oltean static int sja1105_vlan_del(struct dsa_switch *ds, int port, 28906666cebcSVladimir Oltean const struct switchdev_obj_port_vlan *vlan) 28916666cebcSVladimir Oltean { 28926666cebcSVladimir Oltean struct sja1105_private *priv = ds->priv; 2893ec5ae610SVladimir Oltean bool vlan_table_changed = false; 28945899ee36SVladimir Oltean int rc; 28956666cebcSVladimir Oltean 2896b7a9e0daSVladimir Oltean rc = sja1105_vlan_del_one(ds, port, vlan->vid, &priv->bridge_vlans); 28975899ee36SVladimir Oltean if (rc > 0) 2898ec5ae610SVladimir Oltean vlan_table_changed = true; 2899ec5ae610SVladimir Oltean 2900ec5ae610SVladimir Oltean if (!vlan_table_changed) 29016666cebcSVladimir Oltean return 0; 2902ec5ae610SVladimir Oltean 2903ec5ae610SVladimir Oltean return sja1105_build_vlan_table(priv, true); 29046666cebcSVladimir Oltean } 29056666cebcSVladimir Oltean 29065899ee36SVladimir Oltean static int sja1105_dsa_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid, 29075899ee36SVladimir Oltean u16 flags) 29085899ee36SVladimir Oltean { 29095899ee36SVladimir Oltean struct sja1105_private *priv = ds->priv; 29105899ee36SVladimir Oltean int rc; 29115899ee36SVladimir Oltean 29125899ee36SVladimir Oltean rc = sja1105_vlan_add_one(ds, port, vid, flags, &priv->dsa_8021q_vlans); 29135899ee36SVladimir Oltean if (rc <= 0) 29145899ee36SVladimir Oltean return rc; 29155899ee36SVladimir Oltean 29165899ee36SVladimir Oltean return sja1105_build_vlan_table(priv, true); 29175899ee36SVladimir Oltean } 29185899ee36SVladimir Oltean 29195899ee36SVladimir Oltean static int sja1105_dsa_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid) 29205899ee36SVladimir Oltean { 29215899ee36SVladimir Oltean struct sja1105_private *priv = ds->priv; 29225899ee36SVladimir Oltean int rc; 29235899ee36SVladimir Oltean 29245899ee36SVladimir Oltean rc = sja1105_vlan_del_one(ds, port, vid, &priv->dsa_8021q_vlans); 29255899ee36SVladimir Oltean if (!rc) 29265899ee36SVladimir Oltean return 0; 29275899ee36SVladimir Oltean 29285899ee36SVladimir Oltean return sja1105_build_vlan_table(priv, true); 29295899ee36SVladimir Oltean } 29305899ee36SVladimir Oltean 29315899ee36SVladimir Oltean static const struct dsa_8021q_ops sja1105_dsa_8021q_ops = { 29325899ee36SVladimir Oltean .vlan_add = sja1105_dsa_8021q_vlan_add, 29335899ee36SVladimir Oltean .vlan_del = sja1105_dsa_8021q_vlan_del, 29345899ee36SVladimir Oltean }; 29355899ee36SVladimir Oltean 29368aa9ebccSVladimir Oltean /* The programming model for the SJA1105 switch is "all-at-once" via static 29378aa9ebccSVladimir Oltean * configuration tables. Some of these can be dynamically modified at runtime, 29388aa9ebccSVladimir Oltean * but not the xMII mode parameters table. 29398aa9ebccSVladimir Oltean * Furthermode, some PHYs may not have crystals for generating their clocks 29408aa9ebccSVladimir Oltean * (e.g. RMII). Instead, their 50MHz clock is supplied via the SJA1105 port's 29418aa9ebccSVladimir Oltean * ref_clk pin. So port clocking needs to be initialized early, before 29428aa9ebccSVladimir Oltean * connecting to PHYs is attempted, otherwise they won't respond through MDIO. 29438aa9ebccSVladimir Oltean * Setting correct PHY link speed does not matter now. 29448aa9ebccSVladimir Oltean * But dsa_slave_phy_setup is called later than sja1105_setup, so the PHY 29458aa9ebccSVladimir Oltean * bindings are not yet parsed by DSA core. We need to parse early so that we 29468aa9ebccSVladimir Oltean * can populate the xMII mode parameters table. 29478aa9ebccSVladimir Oltean */ 29488aa9ebccSVladimir Oltean static int sja1105_setup(struct dsa_switch *ds) 29498aa9ebccSVladimir Oltean { 29508aa9ebccSVladimir Oltean struct sja1105_dt_port ports[SJA1105_NUM_PORTS]; 29518aa9ebccSVladimir Oltean struct sja1105_private *priv = ds->priv; 29528aa9ebccSVladimir Oltean int rc; 29538aa9ebccSVladimir Oltean 29548aa9ebccSVladimir Oltean rc = sja1105_parse_dt(priv, ports); 29558aa9ebccSVladimir Oltean if (rc < 0) { 29568aa9ebccSVladimir Oltean dev_err(ds->dev, "Failed to parse DT: %d\n", rc); 29578aa9ebccSVladimir Oltean return rc; 29588aa9ebccSVladimir Oltean } 2959f5b8631cSVladimir Oltean 2960f5b8631cSVladimir Oltean /* Error out early if internal delays are required through DT 2961f5b8631cSVladimir Oltean * and we can't apply them. 2962f5b8631cSVladimir Oltean */ 2963f5b8631cSVladimir Oltean rc = sja1105_parse_rgmii_delays(priv, ports); 2964f5b8631cSVladimir Oltean if (rc < 0) { 2965f5b8631cSVladimir Oltean dev_err(ds->dev, "RGMII delay not supported\n"); 2966f5b8631cSVladimir Oltean return rc; 2967f5b8631cSVladimir Oltean } 2968f5b8631cSVladimir Oltean 296961c77126SVladimir Oltean rc = sja1105_ptp_clock_register(ds); 2970bb77f36aSVladimir Oltean if (rc < 0) { 2971bb77f36aSVladimir Oltean dev_err(ds->dev, "Failed to register PTP clock: %d\n", rc); 2972bb77f36aSVladimir Oltean return rc; 2973bb77f36aSVladimir Oltean } 29748aa9ebccSVladimir Oltean /* Create and send configuration down to device */ 29758aa9ebccSVladimir Oltean rc = sja1105_static_config_load(priv, ports); 29768aa9ebccSVladimir Oltean if (rc < 0) { 29778aa9ebccSVladimir Oltean dev_err(ds->dev, "Failed to load static config: %d\n", rc); 2978cec279a8SVladimir Oltean goto out_ptp_clock_unregister; 29798aa9ebccSVladimir Oltean } 29808aa9ebccSVladimir Oltean /* Configure the CGU (PHY link modes and speeds) */ 29818aa9ebccSVladimir Oltean rc = sja1105_clocking_setup(priv); 29828aa9ebccSVladimir Oltean if (rc < 0) { 29838aa9ebccSVladimir Oltean dev_err(ds->dev, "Failed to configure MII clocking: %d\n", rc); 2984cec279a8SVladimir Oltean goto out_static_config_free; 29858aa9ebccSVladimir Oltean } 29866666cebcSVladimir Oltean /* On SJA1105, VLAN filtering per se is always enabled in hardware. 29876666cebcSVladimir Oltean * The only thing we can do to disable it is lie about what the 802.1Q 29886666cebcSVladimir Oltean * EtherType is. 29896666cebcSVladimir Oltean * So it will still try to apply VLAN filtering, but all ingress 29906666cebcSVladimir Oltean * traffic (except frames received with EtherType of ETH_P_SJA1105) 29916666cebcSVladimir Oltean * will be internally tagged with a distorted VLAN header where the 29926666cebcSVladimir Oltean * TPID is ETH_P_SJA1105, and the VLAN ID is the port pvid. 29936666cebcSVladimir Oltean */ 29946666cebcSVladimir Oltean ds->vlan_filtering_is_global = true; 29958aa9ebccSVladimir Oltean 29965f06c63bSVladimir Oltean /* Advertise the 8 egress queues */ 29975f06c63bSVladimir Oltean ds->num_tx_queues = SJA1105_NUM_TC; 29985f06c63bSVladimir Oltean 2999c279c726SVladimir Oltean ds->mtu_enforcement_ingress = true; 3000c279c726SVladimir Oltean 30018841f6e6SVladimir Oltean priv->best_effort_vlan_filtering = true; 30028841f6e6SVladimir Oltean 30030a7bdbc2SVladimir Oltean rc = sja1105_devlink_setup(ds); 30042cafa72eSVladimir Oltean if (rc < 0) 3005cec279a8SVladimir Oltean goto out_static_config_free; 30062cafa72eSVladimir Oltean 3007227d07a0SVladimir Oltean /* The DSA/switchdev model brings up switch ports in standalone mode by 3008227d07a0SVladimir Oltean * default, and that means vlan_filtering is 0 since they're not under 3009227d07a0SVladimir Oltean * a bridge, so it's safe to set up switch tagging at this time. 3010227d07a0SVladimir Oltean */ 3011bbed0bbdSVladimir Oltean rtnl_lock(); 3012bbed0bbdSVladimir Oltean rc = sja1105_setup_8021q_tagging(ds, true); 3013bbed0bbdSVladimir Oltean rtnl_unlock(); 3014cec279a8SVladimir Oltean if (rc) 3015cec279a8SVladimir Oltean goto out_devlink_teardown; 3016cec279a8SVladimir Oltean 3017cec279a8SVladimir Oltean return 0; 3018cec279a8SVladimir Oltean 3019cec279a8SVladimir Oltean out_devlink_teardown: 3020cec279a8SVladimir Oltean sja1105_devlink_teardown(ds); 3021cec279a8SVladimir Oltean out_ptp_clock_unregister: 3022cec279a8SVladimir Oltean sja1105_ptp_clock_unregister(ds); 3023cec279a8SVladimir Oltean out_static_config_free: 3024cec279a8SVladimir Oltean sja1105_static_config_free(&priv->static_config); 3025bbed0bbdSVladimir Oltean 3026bbed0bbdSVladimir Oltean return rc; 3027227d07a0SVladimir Oltean } 3028227d07a0SVladimir Oltean 3029f3097be2SVladimir Oltean static void sja1105_teardown(struct dsa_switch *ds) 3030f3097be2SVladimir Oltean { 3031f3097be2SVladimir Oltean struct sja1105_private *priv = ds->priv; 3032ec5ae610SVladimir Oltean struct sja1105_bridge_vlan *v, *n; 3033a68578c2SVladimir Oltean int port; 3034a68578c2SVladimir Oltean 3035a68578c2SVladimir Oltean for (port = 0; port < SJA1105_NUM_PORTS; port++) { 3036a68578c2SVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 3037a68578c2SVladimir Oltean 3038a68578c2SVladimir Oltean if (!dsa_is_user_port(ds, port)) 3039a68578c2SVladimir Oltean continue; 3040a68578c2SVladimir Oltean 304152c0d4e3SVladimir Oltean if (sp->xmit_worker) 3042a68578c2SVladimir Oltean kthread_destroy_worker(sp->xmit_worker); 3043a68578c2SVladimir Oltean } 3044f3097be2SVladimir Oltean 30450a7bdbc2SVladimir Oltean sja1105_devlink_teardown(ds); 3046a6af7763SVladimir Oltean sja1105_flower_teardown(ds); 3047317ab5b8SVladimir Oltean sja1105_tas_teardown(ds); 304861c77126SVladimir Oltean sja1105_ptp_clock_unregister(ds); 30496cb0abbdSVladimir Oltean sja1105_static_config_free(&priv->static_config); 3050ec5ae610SVladimir Oltean 3051ec5ae610SVladimir Oltean list_for_each_entry_safe(v, n, &priv->dsa_8021q_vlans, list) { 3052ec5ae610SVladimir Oltean list_del(&v->list); 3053ec5ae610SVladimir Oltean kfree(v); 3054ec5ae610SVladimir Oltean } 3055ec5ae610SVladimir Oltean 3056ec5ae610SVladimir Oltean list_for_each_entry_safe(v, n, &priv->bridge_vlans, list) { 3057ec5ae610SVladimir Oltean list_del(&v->list); 3058ec5ae610SVladimir Oltean kfree(v); 3059ec5ae610SVladimir Oltean } 3060f3097be2SVladimir Oltean } 3061f3097be2SVladimir Oltean 3062a68578c2SVladimir Oltean static void sja1105_port_disable(struct dsa_switch *ds, int port) 3063a68578c2SVladimir Oltean { 3064a68578c2SVladimir Oltean struct sja1105_private *priv = ds->priv; 3065a68578c2SVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 3066a68578c2SVladimir Oltean 3067a68578c2SVladimir Oltean if (!dsa_is_user_port(ds, port)) 3068a68578c2SVladimir Oltean return; 3069a68578c2SVladimir Oltean 3070a68578c2SVladimir Oltean kthread_cancel_work_sync(&sp->xmit_work); 3071a68578c2SVladimir Oltean skb_queue_purge(&sp->xmit_queue); 3072a68578c2SVladimir Oltean } 3073a68578c2SVladimir Oltean 3074227d07a0SVladimir Oltean static int sja1105_mgmt_xmit(struct dsa_switch *ds, int port, int slot, 307547ed985eSVladimir Oltean struct sk_buff *skb, bool takets) 3076227d07a0SVladimir Oltean { 3077227d07a0SVladimir Oltean struct sja1105_mgmt_entry mgmt_route = {0}; 3078227d07a0SVladimir Oltean struct sja1105_private *priv = ds->priv; 3079227d07a0SVladimir Oltean struct ethhdr *hdr; 3080227d07a0SVladimir Oltean int timeout = 10; 3081227d07a0SVladimir Oltean int rc; 3082227d07a0SVladimir Oltean 3083227d07a0SVladimir Oltean hdr = eth_hdr(skb); 3084227d07a0SVladimir Oltean 3085227d07a0SVladimir Oltean mgmt_route.macaddr = ether_addr_to_u64(hdr->h_dest); 3086227d07a0SVladimir Oltean mgmt_route.destports = BIT(port); 3087227d07a0SVladimir Oltean mgmt_route.enfport = 1; 308847ed985eSVladimir Oltean mgmt_route.tsreg = 0; 308947ed985eSVladimir Oltean mgmt_route.takets = takets; 3090227d07a0SVladimir Oltean 3091227d07a0SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE, 3092227d07a0SVladimir Oltean slot, &mgmt_route, true); 3093227d07a0SVladimir Oltean if (rc < 0) { 3094227d07a0SVladimir Oltean kfree_skb(skb); 3095227d07a0SVladimir Oltean return rc; 3096227d07a0SVladimir Oltean } 3097227d07a0SVladimir Oltean 3098227d07a0SVladimir Oltean /* Transfer skb to the host port. */ 309968bb8ea8SVivien Didelot dsa_enqueue_skb(skb, dsa_to_port(ds, port)->slave); 3100227d07a0SVladimir Oltean 3101227d07a0SVladimir Oltean /* Wait until the switch has processed the frame */ 3102227d07a0SVladimir Oltean do { 3103227d07a0SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_MGMT_ROUTE, 3104227d07a0SVladimir Oltean slot, &mgmt_route); 3105227d07a0SVladimir Oltean if (rc < 0) { 3106227d07a0SVladimir Oltean dev_err_ratelimited(priv->ds->dev, 3107227d07a0SVladimir Oltean "failed to poll for mgmt route\n"); 3108227d07a0SVladimir Oltean continue; 3109227d07a0SVladimir Oltean } 3110227d07a0SVladimir Oltean 3111227d07a0SVladimir Oltean /* UM10944: The ENFPORT flag of the respective entry is 3112227d07a0SVladimir Oltean * cleared when a match is found. The host can use this 3113227d07a0SVladimir Oltean * flag as an acknowledgment. 3114227d07a0SVladimir Oltean */ 3115227d07a0SVladimir Oltean cpu_relax(); 3116227d07a0SVladimir Oltean } while (mgmt_route.enfport && --timeout); 3117227d07a0SVladimir Oltean 3118227d07a0SVladimir Oltean if (!timeout) { 3119227d07a0SVladimir Oltean /* Clean up the management route so that a follow-up 3120227d07a0SVladimir Oltean * frame may not match on it by mistake. 31212a7e7409SVladimir Oltean * This is only hardware supported on P/Q/R/S - on E/T it is 31222a7e7409SVladimir Oltean * a no-op and we are silently discarding the -EOPNOTSUPP. 3123227d07a0SVladimir Oltean */ 3124227d07a0SVladimir Oltean sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE, 3125227d07a0SVladimir Oltean slot, &mgmt_route, false); 3126227d07a0SVladimir Oltean dev_err_ratelimited(priv->ds->dev, "xmit timed out\n"); 3127227d07a0SVladimir Oltean } 3128227d07a0SVladimir Oltean 3129227d07a0SVladimir Oltean return NETDEV_TX_OK; 3130227d07a0SVladimir Oltean } 3131227d07a0SVladimir Oltean 3132a68578c2SVladimir Oltean #define work_to_port(work) \ 3133a68578c2SVladimir Oltean container_of((work), struct sja1105_port, xmit_work) 3134a68578c2SVladimir Oltean #define tagger_to_sja1105(t) \ 3135a68578c2SVladimir Oltean container_of((t), struct sja1105_private, tagger_data) 3136a68578c2SVladimir Oltean 3137227d07a0SVladimir Oltean /* Deferred work is unfortunately necessary because setting up the management 3138227d07a0SVladimir Oltean * route cannot be done from atomit context (SPI transfer takes a sleepable 3139227d07a0SVladimir Oltean * lock on the bus) 3140227d07a0SVladimir Oltean */ 3141a68578c2SVladimir Oltean static void sja1105_port_deferred_xmit(struct kthread_work *work) 3142227d07a0SVladimir Oltean { 3143a68578c2SVladimir Oltean struct sja1105_port *sp = work_to_port(work); 3144a68578c2SVladimir Oltean struct sja1105_tagger_data *tagger_data = sp->data; 3145a68578c2SVladimir Oltean struct sja1105_private *priv = tagger_to_sja1105(tagger_data); 3146a68578c2SVladimir Oltean int port = sp - priv->ports; 3147a68578c2SVladimir Oltean struct sk_buff *skb; 3148a68578c2SVladimir Oltean 3149a68578c2SVladimir Oltean while ((skb = skb_dequeue(&sp->xmit_queue)) != NULL) { 3150c4b364ceSYangbo Lu struct sk_buff *clone = SJA1105_SKB_CB(skb)->clone; 3151227d07a0SVladimir Oltean 3152227d07a0SVladimir Oltean mutex_lock(&priv->mgmt_lock); 3153227d07a0SVladimir Oltean 3154a68578c2SVladimir Oltean sja1105_mgmt_xmit(priv->ds, port, 0, skb, !!clone); 3155a68578c2SVladimir Oltean 315647ed985eSVladimir Oltean /* The clone, if there, was made by dsa_skb_tx_timestamp */ 3157a68578c2SVladimir Oltean if (clone) 3158a68578c2SVladimir Oltean sja1105_ptp_txtstamp_skb(priv->ds, port, clone); 3159227d07a0SVladimir Oltean 3160227d07a0SVladimir Oltean mutex_unlock(&priv->mgmt_lock); 3161a68578c2SVladimir Oltean } 31628aa9ebccSVladimir Oltean } 31638aa9ebccSVladimir Oltean 31648456721dSVladimir Oltean /* The MAXAGE setting belongs to the L2 Forwarding Parameters table, 31658456721dSVladimir Oltean * which cannot be reconfigured at runtime. So a switch reset is required. 31668456721dSVladimir Oltean */ 31678456721dSVladimir Oltean static int sja1105_set_ageing_time(struct dsa_switch *ds, 31688456721dSVladimir Oltean unsigned int ageing_time) 31698456721dSVladimir Oltean { 31708456721dSVladimir Oltean struct sja1105_l2_lookup_params_entry *l2_lookup_params; 31718456721dSVladimir Oltean struct sja1105_private *priv = ds->priv; 31728456721dSVladimir Oltean struct sja1105_table *table; 31738456721dSVladimir Oltean unsigned int maxage; 31748456721dSVladimir Oltean 31758456721dSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS]; 31768456721dSVladimir Oltean l2_lookup_params = table->entries; 31778456721dSVladimir Oltean 31788456721dSVladimir Oltean maxage = SJA1105_AGEING_TIME_MS(ageing_time); 31798456721dSVladimir Oltean 31808456721dSVladimir Oltean if (l2_lookup_params->maxage == maxage) 31818456721dSVladimir Oltean return 0; 31828456721dSVladimir Oltean 31838456721dSVladimir Oltean l2_lookup_params->maxage = maxage; 31848456721dSVladimir Oltean 31852eea1fa8SVladimir Oltean return sja1105_static_config_reload(priv, SJA1105_AGEING_TIME); 31868456721dSVladimir Oltean } 31878456721dSVladimir Oltean 3188c279c726SVladimir Oltean static int sja1105_change_mtu(struct dsa_switch *ds, int port, int new_mtu) 3189c279c726SVladimir Oltean { 3190c279c726SVladimir Oltean struct sja1105_l2_policing_entry *policing; 3191c279c726SVladimir Oltean struct sja1105_private *priv = ds->priv; 3192c279c726SVladimir Oltean 3193c279c726SVladimir Oltean new_mtu += VLAN_ETH_HLEN + ETH_FCS_LEN; 3194c279c726SVladimir Oltean 3195c279c726SVladimir Oltean if (dsa_is_cpu_port(ds, port)) 3196c279c726SVladimir Oltean new_mtu += VLAN_HLEN; 3197c279c726SVladimir Oltean 3198c279c726SVladimir Oltean policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries; 3199c279c726SVladimir Oltean 3200a7cc081cSVladimir Oltean if (policing[port].maxlen == new_mtu) 3201c279c726SVladimir Oltean return 0; 3202c279c726SVladimir Oltean 3203a7cc081cSVladimir Oltean policing[port].maxlen = new_mtu; 3204c279c726SVladimir Oltean 3205c279c726SVladimir Oltean return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING); 3206c279c726SVladimir Oltean } 3207c279c726SVladimir Oltean 3208c279c726SVladimir Oltean static int sja1105_get_max_mtu(struct dsa_switch *ds, int port) 3209c279c726SVladimir Oltean { 3210c279c726SVladimir Oltean return 2043 - VLAN_ETH_HLEN - ETH_FCS_LEN; 3211c279c726SVladimir Oltean } 3212c279c726SVladimir Oltean 3213317ab5b8SVladimir Oltean static int sja1105_port_setup_tc(struct dsa_switch *ds, int port, 3214317ab5b8SVladimir Oltean enum tc_setup_type type, 3215317ab5b8SVladimir Oltean void *type_data) 3216317ab5b8SVladimir Oltean { 3217317ab5b8SVladimir Oltean switch (type) { 3218317ab5b8SVladimir Oltean case TC_SETUP_QDISC_TAPRIO: 3219317ab5b8SVladimir Oltean return sja1105_setup_tc_taprio(ds, port, type_data); 32204d752508SVladimir Oltean case TC_SETUP_QDISC_CBS: 32214d752508SVladimir Oltean return sja1105_setup_tc_cbs(ds, port, type_data); 3222317ab5b8SVladimir Oltean default: 3223317ab5b8SVladimir Oltean return -EOPNOTSUPP; 3224317ab5b8SVladimir Oltean } 3225317ab5b8SVladimir Oltean } 3226317ab5b8SVladimir Oltean 3227511e6ca0SVladimir Oltean /* We have a single mirror (@to) port, but can configure ingress and egress 3228511e6ca0SVladimir Oltean * mirroring on all other (@from) ports. 3229511e6ca0SVladimir Oltean * We need to allow mirroring rules only as long as the @to port is always the 3230511e6ca0SVladimir Oltean * same, and we need to unset the @to port from mirr_port only when there is no 3231511e6ca0SVladimir Oltean * mirroring rule that references it. 3232511e6ca0SVladimir Oltean */ 3233511e6ca0SVladimir Oltean static int sja1105_mirror_apply(struct sja1105_private *priv, int from, int to, 3234511e6ca0SVladimir Oltean bool ingress, bool enabled) 3235511e6ca0SVladimir Oltean { 3236511e6ca0SVladimir Oltean struct sja1105_general_params_entry *general_params; 3237511e6ca0SVladimir Oltean struct sja1105_mac_config_entry *mac; 3238511e6ca0SVladimir Oltean struct sja1105_table *table; 3239511e6ca0SVladimir Oltean bool already_enabled; 3240511e6ca0SVladimir Oltean u64 new_mirr_port; 3241511e6ca0SVladimir Oltean int rc; 3242511e6ca0SVladimir Oltean 3243511e6ca0SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; 3244511e6ca0SVladimir Oltean general_params = table->entries; 3245511e6ca0SVladimir Oltean 3246511e6ca0SVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 3247511e6ca0SVladimir Oltean 3248511e6ca0SVladimir Oltean already_enabled = (general_params->mirr_port != SJA1105_NUM_PORTS); 3249511e6ca0SVladimir Oltean if (already_enabled && enabled && general_params->mirr_port != to) { 3250511e6ca0SVladimir Oltean dev_err(priv->ds->dev, 3251511e6ca0SVladimir Oltean "Delete mirroring rules towards port %llu first\n", 3252511e6ca0SVladimir Oltean general_params->mirr_port); 3253511e6ca0SVladimir Oltean return -EBUSY; 3254511e6ca0SVladimir Oltean } 3255511e6ca0SVladimir Oltean 3256511e6ca0SVladimir Oltean new_mirr_port = to; 3257511e6ca0SVladimir Oltean if (!enabled) { 3258511e6ca0SVladimir Oltean bool keep = false; 3259511e6ca0SVladimir Oltean int port; 3260511e6ca0SVladimir Oltean 3261511e6ca0SVladimir Oltean /* Anybody still referencing mirr_port? */ 3262511e6ca0SVladimir Oltean for (port = 0; port < SJA1105_NUM_PORTS; port++) { 3263511e6ca0SVladimir Oltean if (mac[port].ing_mirr || mac[port].egr_mirr) { 3264511e6ca0SVladimir Oltean keep = true; 3265511e6ca0SVladimir Oltean break; 3266511e6ca0SVladimir Oltean } 3267511e6ca0SVladimir Oltean } 3268511e6ca0SVladimir Oltean /* Unset already_enabled for next time */ 3269511e6ca0SVladimir Oltean if (!keep) 3270511e6ca0SVladimir Oltean new_mirr_port = SJA1105_NUM_PORTS; 3271511e6ca0SVladimir Oltean } 3272511e6ca0SVladimir Oltean if (new_mirr_port != general_params->mirr_port) { 3273511e6ca0SVladimir Oltean general_params->mirr_port = new_mirr_port; 3274511e6ca0SVladimir Oltean 3275511e6ca0SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_GENERAL_PARAMS, 3276511e6ca0SVladimir Oltean 0, general_params, true); 3277511e6ca0SVladimir Oltean if (rc < 0) 3278511e6ca0SVladimir Oltean return rc; 3279511e6ca0SVladimir Oltean } 3280511e6ca0SVladimir Oltean 3281511e6ca0SVladimir Oltean if (ingress) 3282511e6ca0SVladimir Oltean mac[from].ing_mirr = enabled; 3283511e6ca0SVladimir Oltean else 3284511e6ca0SVladimir Oltean mac[from].egr_mirr = enabled; 3285511e6ca0SVladimir Oltean 3286511e6ca0SVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, from, 3287511e6ca0SVladimir Oltean &mac[from], true); 3288511e6ca0SVladimir Oltean } 3289511e6ca0SVladimir Oltean 3290511e6ca0SVladimir Oltean static int sja1105_mirror_add(struct dsa_switch *ds, int port, 3291511e6ca0SVladimir Oltean struct dsa_mall_mirror_tc_entry *mirror, 3292511e6ca0SVladimir Oltean bool ingress) 3293511e6ca0SVladimir Oltean { 3294511e6ca0SVladimir Oltean return sja1105_mirror_apply(ds->priv, port, mirror->to_local_port, 3295511e6ca0SVladimir Oltean ingress, true); 3296511e6ca0SVladimir Oltean } 3297511e6ca0SVladimir Oltean 3298511e6ca0SVladimir Oltean static void sja1105_mirror_del(struct dsa_switch *ds, int port, 3299511e6ca0SVladimir Oltean struct dsa_mall_mirror_tc_entry *mirror) 3300511e6ca0SVladimir Oltean { 3301511e6ca0SVladimir Oltean sja1105_mirror_apply(ds->priv, port, mirror->to_local_port, 3302511e6ca0SVladimir Oltean mirror->ingress, false); 3303511e6ca0SVladimir Oltean } 3304511e6ca0SVladimir Oltean 3305a7cc081cSVladimir Oltean static int sja1105_port_policer_add(struct dsa_switch *ds, int port, 3306a7cc081cSVladimir Oltean struct dsa_mall_policer_tc_entry *policer) 3307a7cc081cSVladimir Oltean { 3308a7cc081cSVladimir Oltean struct sja1105_l2_policing_entry *policing; 3309a7cc081cSVladimir Oltean struct sja1105_private *priv = ds->priv; 3310a7cc081cSVladimir Oltean 3311a7cc081cSVladimir Oltean policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries; 3312a7cc081cSVladimir Oltean 3313a7cc081cSVladimir Oltean /* In hardware, every 8 microseconds the credit level is incremented by 3314a7cc081cSVladimir Oltean * the value of RATE bytes divided by 64, up to a maximum of SMAX 3315a7cc081cSVladimir Oltean * bytes. 3316a7cc081cSVladimir Oltean */ 3317a7cc081cSVladimir Oltean policing[port].rate = div_u64(512 * policer->rate_bytes_per_sec, 3318a7cc081cSVladimir Oltean 1000000); 33195f035af7SPo Liu policing[port].smax = policer->burst; 3320a7cc081cSVladimir Oltean 3321a7cc081cSVladimir Oltean return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING); 3322a7cc081cSVladimir Oltean } 3323a7cc081cSVladimir Oltean 3324a7cc081cSVladimir Oltean static void sja1105_port_policer_del(struct dsa_switch *ds, int port) 3325a7cc081cSVladimir Oltean { 3326a7cc081cSVladimir Oltean struct sja1105_l2_policing_entry *policing; 3327a7cc081cSVladimir Oltean struct sja1105_private *priv = ds->priv; 3328a7cc081cSVladimir Oltean 3329a7cc081cSVladimir Oltean policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries; 3330a7cc081cSVladimir Oltean 3331a7cc081cSVladimir Oltean policing[port].rate = SJA1105_RATE_MBPS(1000); 3332a7cc081cSVladimir Oltean policing[port].smax = 65535; 3333a7cc081cSVladimir Oltean 3334a7cc081cSVladimir Oltean sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING); 3335a7cc081cSVladimir Oltean } 3336a7cc081cSVladimir Oltean 33374d942354SVladimir Oltean static int sja1105_port_set_learning(struct sja1105_private *priv, int port, 33384d942354SVladimir Oltean bool enabled) 33394d942354SVladimir Oltean { 33404d942354SVladimir Oltean struct sja1105_mac_config_entry *mac; 33414d942354SVladimir Oltean int rc; 33424d942354SVladimir Oltean 33434d942354SVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 33444d942354SVladimir Oltean 33454c44fc5eSVladimir Oltean mac[port].dyn_learn = enabled; 33464d942354SVladimir Oltean 33474d942354SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, 33484d942354SVladimir Oltean &mac[port], true); 33494d942354SVladimir Oltean if (rc) 33504d942354SVladimir Oltean return rc; 33514d942354SVladimir Oltean 33524d942354SVladimir Oltean if (enabled) 33534d942354SVladimir Oltean priv->learn_ena |= BIT(port); 33544d942354SVladimir Oltean else 33554d942354SVladimir Oltean priv->learn_ena &= ~BIT(port); 33564d942354SVladimir Oltean 33574d942354SVladimir Oltean return 0; 33584d942354SVladimir Oltean } 33594d942354SVladimir Oltean 33604d942354SVladimir Oltean static int sja1105_port_ucast_bcast_flood(struct sja1105_private *priv, int to, 33614d942354SVladimir Oltean struct switchdev_brport_flags flags) 33624d942354SVladimir Oltean { 33634d942354SVladimir Oltean if (flags.mask & BR_FLOOD) { 33644d942354SVladimir Oltean if (flags.val & BR_FLOOD) 33657f7ccdeaSVladimir Oltean priv->ucast_egress_floods |= BIT(to); 33664d942354SVladimir Oltean else 33676a5166e0SVladimir Oltean priv->ucast_egress_floods &= ~BIT(to); 33684d942354SVladimir Oltean } 33697f7ccdeaSVladimir Oltean 33704d942354SVladimir Oltean if (flags.mask & BR_BCAST_FLOOD) { 33714d942354SVladimir Oltean if (flags.val & BR_BCAST_FLOOD) 33727f7ccdeaSVladimir Oltean priv->bcast_egress_floods |= BIT(to); 33734d942354SVladimir Oltean else 33746a5166e0SVladimir Oltean priv->bcast_egress_floods &= ~BIT(to); 33754d942354SVladimir Oltean } 33764d942354SVladimir Oltean 33777f7ccdeaSVladimir Oltean return sja1105_manage_flood_domains(priv); 33784d942354SVladimir Oltean } 33794d942354SVladimir Oltean 33804d942354SVladimir Oltean static int sja1105_port_mcast_flood(struct sja1105_private *priv, int to, 33814d942354SVladimir Oltean struct switchdev_brport_flags flags, 33824d942354SVladimir Oltean struct netlink_ext_ack *extack) 33834d942354SVladimir Oltean { 33844d942354SVladimir Oltean struct sja1105_l2_lookup_entry *l2_lookup; 33854d942354SVladimir Oltean struct sja1105_table *table; 33864d942354SVladimir Oltean int match; 33874d942354SVladimir Oltean 33884d942354SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; 33894d942354SVladimir Oltean l2_lookup = table->entries; 33904d942354SVladimir Oltean 33914d942354SVladimir Oltean for (match = 0; match < table->entry_count; match++) 33924d942354SVladimir Oltean if (l2_lookup[match].macaddr == SJA1105_UNKNOWN_MULTICAST && 33934d942354SVladimir Oltean l2_lookup[match].mask_macaddr == SJA1105_UNKNOWN_MULTICAST) 33944d942354SVladimir Oltean break; 33954d942354SVladimir Oltean 33964d942354SVladimir Oltean if (match == table->entry_count) { 33974d942354SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 33984d942354SVladimir Oltean "Could not find FDB entry for unknown multicast"); 33994d942354SVladimir Oltean return -ENOSPC; 34004d942354SVladimir Oltean } 34014d942354SVladimir Oltean 34024d942354SVladimir Oltean if (flags.val & BR_MCAST_FLOOD) 34034d942354SVladimir Oltean l2_lookup[match].destports |= BIT(to); 34044d942354SVladimir Oltean else 34054d942354SVladimir Oltean l2_lookup[match].destports &= ~BIT(to); 34064d942354SVladimir Oltean 34074d942354SVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 34084d942354SVladimir Oltean l2_lookup[match].index, 34094d942354SVladimir Oltean &l2_lookup[match], 34104d942354SVladimir Oltean true); 34114d942354SVladimir Oltean } 34124d942354SVladimir Oltean 34134d942354SVladimir Oltean static int sja1105_port_pre_bridge_flags(struct dsa_switch *ds, int port, 34144d942354SVladimir Oltean struct switchdev_brport_flags flags, 34154d942354SVladimir Oltean struct netlink_ext_ack *extack) 34164d942354SVladimir Oltean { 34174d942354SVladimir Oltean struct sja1105_private *priv = ds->priv; 34184d942354SVladimir Oltean 34194d942354SVladimir Oltean if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | 34204d942354SVladimir Oltean BR_BCAST_FLOOD)) 34214d942354SVladimir Oltean return -EINVAL; 34224d942354SVladimir Oltean 34234d942354SVladimir Oltean if (flags.mask & (BR_FLOOD | BR_MCAST_FLOOD) && 34244d942354SVladimir Oltean !priv->info->can_limit_mcast_flood) { 34254d942354SVladimir Oltean bool multicast = !!(flags.val & BR_MCAST_FLOOD); 34264d942354SVladimir Oltean bool unicast = !!(flags.val & BR_FLOOD); 34274d942354SVladimir Oltean 34284d942354SVladimir Oltean if (unicast != multicast) { 34294d942354SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 34304d942354SVladimir Oltean "This chip cannot configure multicast flooding independently of unicast"); 34314d942354SVladimir Oltean return -EINVAL; 34324d942354SVladimir Oltean } 34334d942354SVladimir Oltean } 34344d942354SVladimir Oltean 34354d942354SVladimir Oltean return 0; 34364d942354SVladimir Oltean } 34374d942354SVladimir Oltean 34384d942354SVladimir Oltean static int sja1105_port_bridge_flags(struct dsa_switch *ds, int port, 34394d942354SVladimir Oltean struct switchdev_brport_flags flags, 34404d942354SVladimir Oltean struct netlink_ext_ack *extack) 34414d942354SVladimir Oltean { 34424d942354SVladimir Oltean struct sja1105_private *priv = ds->priv; 34434d942354SVladimir Oltean int rc; 34444d942354SVladimir Oltean 34454d942354SVladimir Oltean if (flags.mask & BR_LEARNING) { 34464d942354SVladimir Oltean bool learn_ena = !!(flags.val & BR_LEARNING); 34474d942354SVladimir Oltean 34484d942354SVladimir Oltean rc = sja1105_port_set_learning(priv, port, learn_ena); 34494d942354SVladimir Oltean if (rc) 34504d942354SVladimir Oltean return rc; 34514d942354SVladimir Oltean } 34524d942354SVladimir Oltean 34534d942354SVladimir Oltean if (flags.mask & (BR_FLOOD | BR_BCAST_FLOOD)) { 34544d942354SVladimir Oltean rc = sja1105_port_ucast_bcast_flood(priv, port, flags); 34554d942354SVladimir Oltean if (rc) 34564d942354SVladimir Oltean return rc; 34574d942354SVladimir Oltean } 34584d942354SVladimir Oltean 34594d942354SVladimir Oltean /* For chips that can't offload BR_MCAST_FLOOD independently, there 34604d942354SVladimir Oltean * is nothing to do here, we ensured the configuration is in sync by 34614d942354SVladimir Oltean * offloading BR_FLOOD. 34624d942354SVladimir Oltean */ 34634d942354SVladimir Oltean if (flags.mask & BR_MCAST_FLOOD && priv->info->can_limit_mcast_flood) { 34644d942354SVladimir Oltean rc = sja1105_port_mcast_flood(priv, port, flags, 34654d942354SVladimir Oltean extack); 34664d942354SVladimir Oltean if (rc) 34674d942354SVladimir Oltean return rc; 34684d942354SVladimir Oltean } 34694d942354SVladimir Oltean 34704d942354SVladimir Oltean return 0; 34714d942354SVladimir Oltean } 34724d942354SVladimir Oltean 34738aa9ebccSVladimir Oltean static const struct dsa_switch_ops sja1105_switch_ops = { 34748aa9ebccSVladimir Oltean .get_tag_protocol = sja1105_get_tag_protocol, 34758aa9ebccSVladimir Oltean .setup = sja1105_setup, 3476f3097be2SVladimir Oltean .teardown = sja1105_teardown, 34778456721dSVladimir Oltean .set_ageing_time = sja1105_set_ageing_time, 3478c279c726SVladimir Oltean .port_change_mtu = sja1105_change_mtu, 3479c279c726SVladimir Oltean .port_max_mtu = sja1105_get_max_mtu, 3480ad9f299aSVladimir Oltean .phylink_validate = sja1105_phylink_validate, 3481ffe10e67SVladimir Oltean .phylink_mac_link_state = sja1105_mac_pcs_get_state, 3482af7cd036SVladimir Oltean .phylink_mac_config = sja1105_mac_config, 34838400cff6SVladimir Oltean .phylink_mac_link_up = sja1105_mac_link_up, 34848400cff6SVladimir Oltean .phylink_mac_link_down = sja1105_mac_link_down, 348552c34e6eSVladimir Oltean .get_strings = sja1105_get_strings, 348652c34e6eSVladimir Oltean .get_ethtool_stats = sja1105_get_ethtool_stats, 348752c34e6eSVladimir Oltean .get_sset_count = sja1105_get_sset_count, 3488bb77f36aSVladimir Oltean .get_ts_info = sja1105_get_ts_info, 3489a68578c2SVladimir Oltean .port_disable = sja1105_port_disable, 3490291d1e72SVladimir Oltean .port_fdb_dump = sja1105_fdb_dump, 3491291d1e72SVladimir Oltean .port_fdb_add = sja1105_fdb_add, 3492291d1e72SVladimir Oltean .port_fdb_del = sja1105_fdb_del, 34938aa9ebccSVladimir Oltean .port_bridge_join = sja1105_bridge_join, 34948aa9ebccSVladimir Oltean .port_bridge_leave = sja1105_bridge_leave, 34954d942354SVladimir Oltean .port_pre_bridge_flags = sja1105_port_pre_bridge_flags, 34964d942354SVladimir Oltean .port_bridge_flags = sja1105_port_bridge_flags, 3497640f763fSVladimir Oltean .port_stp_state_set = sja1105_bridge_stp_state_set, 34986666cebcSVladimir Oltean .port_vlan_filtering = sja1105_vlan_filtering, 34996666cebcSVladimir Oltean .port_vlan_add = sja1105_vlan_add, 35006666cebcSVladimir Oltean .port_vlan_del = sja1105_vlan_del, 3501291d1e72SVladimir Oltean .port_mdb_add = sja1105_mdb_add, 3502291d1e72SVladimir Oltean .port_mdb_del = sja1105_mdb_del, 3503a602afd2SVladimir Oltean .port_hwtstamp_get = sja1105_hwtstamp_get, 3504a602afd2SVladimir Oltean .port_hwtstamp_set = sja1105_hwtstamp_set, 3505f3097be2SVladimir Oltean .port_rxtstamp = sja1105_port_rxtstamp, 350647ed985eSVladimir Oltean .port_txtstamp = sja1105_port_txtstamp, 3507317ab5b8SVladimir Oltean .port_setup_tc = sja1105_port_setup_tc, 3508511e6ca0SVladimir Oltean .port_mirror_add = sja1105_mirror_add, 3509511e6ca0SVladimir Oltean .port_mirror_del = sja1105_mirror_del, 3510a7cc081cSVladimir Oltean .port_policer_add = sja1105_port_policer_add, 3511a7cc081cSVladimir Oltean .port_policer_del = sja1105_port_policer_del, 3512a6af7763SVladimir Oltean .cls_flower_add = sja1105_cls_flower_add, 3513a6af7763SVladimir Oltean .cls_flower_del = sja1105_cls_flower_del, 3514834f8933SVladimir Oltean .cls_flower_stats = sja1105_cls_flower_stats, 3515ac02a451SVladimir Oltean .crosschip_bridge_join = sja1105_crosschip_bridge_join, 3516ac02a451SVladimir Oltean .crosschip_bridge_leave = sja1105_crosschip_bridge_leave, 35172cafa72eSVladimir Oltean .devlink_param_get = sja1105_devlink_param_get, 35182cafa72eSVladimir Oltean .devlink_param_set = sja1105_devlink_param_set, 3519ff4cf8eaSVladimir Oltean .devlink_info_get = sja1105_devlink_info_get, 35208aa9ebccSVladimir Oltean }; 35218aa9ebccSVladimir Oltean 35220b0e2997SVladimir Oltean static const struct of_device_id sja1105_dt_ids[]; 35230b0e2997SVladimir Oltean 35248aa9ebccSVladimir Oltean static int sja1105_check_device_id(struct sja1105_private *priv) 35258aa9ebccSVladimir Oltean { 35268aa9ebccSVladimir Oltean const struct sja1105_regs *regs = priv->info->regs; 35278aa9ebccSVladimir Oltean u8 prod_id[SJA1105_SIZE_DEVICE_ID] = {0}; 35288aa9ebccSVladimir Oltean struct device *dev = &priv->spidev->dev; 35290b0e2997SVladimir Oltean const struct of_device_id *match; 3530dff79620SVladimir Oltean u32 device_id; 35318aa9ebccSVladimir Oltean u64 part_no; 35328aa9ebccSVladimir Oltean int rc; 35338aa9ebccSVladimir Oltean 353434d76e9fSVladimir Oltean rc = sja1105_xfer_u32(priv, SPI_READ, regs->device_id, &device_id, 353534d76e9fSVladimir Oltean NULL); 35368aa9ebccSVladimir Oltean if (rc < 0) 35378aa9ebccSVladimir Oltean return rc; 35388aa9ebccSVladimir Oltean 35391bd44870SVladimir Oltean rc = sja1105_xfer_buf(priv, SPI_READ, regs->prod_id, prod_id, 35401bd44870SVladimir Oltean SJA1105_SIZE_DEVICE_ID); 35418aa9ebccSVladimir Oltean if (rc < 0) 35428aa9ebccSVladimir Oltean return rc; 35438aa9ebccSVladimir Oltean 35448aa9ebccSVladimir Oltean sja1105_unpack(prod_id, &part_no, 19, 4, SJA1105_SIZE_DEVICE_ID); 35458aa9ebccSVladimir Oltean 35465978fac0SNathan Chancellor for (match = sja1105_dt_ids; match->compatible[0]; match++) { 35470b0e2997SVladimir Oltean const struct sja1105_info *info = match->data; 35480b0e2997SVladimir Oltean 35490b0e2997SVladimir Oltean /* Is what's been probed in our match table at all? */ 35500b0e2997SVladimir Oltean if (info->device_id != device_id || info->part_no != part_no) 35510b0e2997SVladimir Oltean continue; 35520b0e2997SVladimir Oltean 35530b0e2997SVladimir Oltean /* But is it what's in the device tree? */ 35540b0e2997SVladimir Oltean if (priv->info->device_id != device_id || 35550b0e2997SVladimir Oltean priv->info->part_no != part_no) { 35560b0e2997SVladimir Oltean dev_warn(dev, "Device tree specifies chip %s but found %s, please fix it!\n", 35570b0e2997SVladimir Oltean priv->info->name, info->name); 35580b0e2997SVladimir Oltean /* It isn't. No problem, pick that up. */ 35590b0e2997SVladimir Oltean priv->info = info; 35608aa9ebccSVladimir Oltean } 35618aa9ebccSVladimir Oltean 35628aa9ebccSVladimir Oltean return 0; 35638aa9ebccSVladimir Oltean } 35648aa9ebccSVladimir Oltean 35650b0e2997SVladimir Oltean dev_err(dev, "Unexpected {device ID, part number}: 0x%x 0x%llx\n", 35660b0e2997SVladimir Oltean device_id, part_no); 35670b0e2997SVladimir Oltean 35680b0e2997SVladimir Oltean return -ENODEV; 35690b0e2997SVladimir Oltean } 35700b0e2997SVladimir Oltean 35718aa9ebccSVladimir Oltean static int sja1105_probe(struct spi_device *spi) 35728aa9ebccSVladimir Oltean { 3573844d7edcSVladimir Oltean struct sja1105_tagger_data *tagger_data; 35748aa9ebccSVladimir Oltean struct device *dev = &spi->dev; 35758aa9ebccSVladimir Oltean struct sja1105_private *priv; 35768aa9ebccSVladimir Oltean struct dsa_switch *ds; 3577a68578c2SVladimir Oltean int rc, port; 35788aa9ebccSVladimir Oltean 35798aa9ebccSVladimir Oltean if (!dev->of_node) { 35808aa9ebccSVladimir Oltean dev_err(dev, "No DTS bindings for SJA1105 driver\n"); 35818aa9ebccSVladimir Oltean return -EINVAL; 35828aa9ebccSVladimir Oltean } 35838aa9ebccSVladimir Oltean 35848aa9ebccSVladimir Oltean priv = devm_kzalloc(dev, sizeof(struct sja1105_private), GFP_KERNEL); 35858aa9ebccSVladimir Oltean if (!priv) 35868aa9ebccSVladimir Oltean return -ENOMEM; 35878aa9ebccSVladimir Oltean 35888aa9ebccSVladimir Oltean /* Configure the optional reset pin and bring up switch */ 35898aa9ebccSVladimir Oltean priv->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); 35908aa9ebccSVladimir Oltean if (IS_ERR(priv->reset_gpio)) 35918aa9ebccSVladimir Oltean dev_dbg(dev, "reset-gpios not defined, ignoring\n"); 35928aa9ebccSVladimir Oltean else 35938aa9ebccSVladimir Oltean sja1105_hw_reset(priv->reset_gpio, 1, 1); 35948aa9ebccSVladimir Oltean 35958aa9ebccSVladimir Oltean /* Populate our driver private structure (priv) based on 35968aa9ebccSVladimir Oltean * the device tree node that was probed (spi) 35978aa9ebccSVladimir Oltean */ 35988aa9ebccSVladimir Oltean priv->spidev = spi; 35998aa9ebccSVladimir Oltean spi_set_drvdata(spi, priv); 36008aa9ebccSVladimir Oltean 36018aa9ebccSVladimir Oltean /* Configure the SPI bus */ 36028aa9ebccSVladimir Oltean spi->bits_per_word = 8; 36038aa9ebccSVladimir Oltean rc = spi_setup(spi); 36048aa9ebccSVladimir Oltean if (rc < 0) { 36058aa9ebccSVladimir Oltean dev_err(dev, "Could not init SPI\n"); 36068aa9ebccSVladimir Oltean return rc; 36078aa9ebccSVladimir Oltean } 36088aa9ebccSVladimir Oltean 36098aa9ebccSVladimir Oltean priv->info = of_device_get_match_data(dev); 36108aa9ebccSVladimir Oltean 36118aa9ebccSVladimir Oltean /* Detect hardware device */ 36128aa9ebccSVladimir Oltean rc = sja1105_check_device_id(priv); 36138aa9ebccSVladimir Oltean if (rc < 0) { 36148aa9ebccSVladimir Oltean dev_err(dev, "Device ID check failed: %d\n", rc); 36158aa9ebccSVladimir Oltean return rc; 36168aa9ebccSVladimir Oltean } 36178aa9ebccSVladimir Oltean 36188aa9ebccSVladimir Oltean dev_info(dev, "Probed switch chip: %s\n", priv->info->name); 36198aa9ebccSVladimir Oltean 36207e99e347SVivien Didelot ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL); 36218aa9ebccSVladimir Oltean if (!ds) 36228aa9ebccSVladimir Oltean return -ENOMEM; 36238aa9ebccSVladimir Oltean 36247e99e347SVivien Didelot ds->dev = dev; 36257e99e347SVivien Didelot ds->num_ports = SJA1105_NUM_PORTS; 36268aa9ebccSVladimir Oltean ds->ops = &sja1105_switch_ops; 36278aa9ebccSVladimir Oltean ds->priv = priv; 36288aa9ebccSVladimir Oltean priv->ds = ds; 36298aa9ebccSVladimir Oltean 3630844d7edcSVladimir Oltean tagger_data = &priv->tagger_data; 3631844d7edcSVladimir Oltean 3632d5a619bfSVivien Didelot mutex_init(&priv->ptp_data.lock); 3633d5a619bfSVivien Didelot mutex_init(&priv->mgmt_lock); 3634d5a619bfSVivien Didelot 36355899ee36SVladimir Oltean priv->dsa_8021q_ctx = devm_kzalloc(dev, sizeof(*priv->dsa_8021q_ctx), 36365899ee36SVladimir Oltean GFP_KERNEL); 36375899ee36SVladimir Oltean if (!priv->dsa_8021q_ctx) 36385899ee36SVladimir Oltean return -ENOMEM; 36395899ee36SVladimir Oltean 36405899ee36SVladimir Oltean priv->dsa_8021q_ctx->ops = &sja1105_dsa_8021q_ops; 3641bbed0bbdSVladimir Oltean priv->dsa_8021q_ctx->proto = htons(ETH_P_8021Q); 36425899ee36SVladimir Oltean priv->dsa_8021q_ctx->ds = ds; 36435899ee36SVladimir Oltean 36445899ee36SVladimir Oltean INIT_LIST_HEAD(&priv->dsa_8021q_ctx->crosschip_links); 3645ec5ae610SVladimir Oltean INIT_LIST_HEAD(&priv->bridge_vlans); 3646ec5ae610SVladimir Oltean INIT_LIST_HEAD(&priv->dsa_8021q_vlans); 3647ac02a451SVladimir Oltean 3648d5a619bfSVivien Didelot sja1105_tas_setup(ds); 3649a6af7763SVladimir Oltean sja1105_flower_setup(ds); 3650d5a619bfSVivien Didelot 3651d5a619bfSVivien Didelot rc = dsa_register_switch(priv->ds); 3652d5a619bfSVivien Didelot if (rc) 3653d5a619bfSVivien Didelot return rc; 3654d5a619bfSVivien Didelot 36554d752508SVladimir Oltean if (IS_ENABLED(CONFIG_NET_SCH_CBS)) { 36564d752508SVladimir Oltean priv->cbs = devm_kcalloc(dev, priv->info->num_cbs_shapers, 36574d752508SVladimir Oltean sizeof(struct sja1105_cbs_entry), 36584d752508SVladimir Oltean GFP_KERNEL); 3659dc596e3fSVladimir Oltean if (!priv->cbs) { 3660dc596e3fSVladimir Oltean rc = -ENOMEM; 3661dc596e3fSVladimir Oltean goto out_unregister_switch; 3662dc596e3fSVladimir Oltean } 36634d752508SVladimir Oltean } 36644d752508SVladimir Oltean 3665227d07a0SVladimir Oltean /* Connections between dsa_port and sja1105_port */ 3666a68578c2SVladimir Oltean for (port = 0; port < SJA1105_NUM_PORTS; port++) { 3667a68578c2SVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 3668a68578c2SVladimir Oltean struct dsa_port *dp = dsa_to_port(ds, port); 3669a68578c2SVladimir Oltean struct net_device *slave; 367084eeb5d4SVladimir Oltean int subvlan; 3671227d07a0SVladimir Oltean 3672a68578c2SVladimir Oltean if (!dsa_is_user_port(ds, port)) 3673a68578c2SVladimir Oltean continue; 3674a68578c2SVladimir Oltean 3675a68578c2SVladimir Oltean dp->priv = sp; 3676a68578c2SVladimir Oltean sp->dp = dp; 3677844d7edcSVladimir Oltean sp->data = tagger_data; 3678a68578c2SVladimir Oltean slave = dp->slave; 3679a68578c2SVladimir Oltean kthread_init_work(&sp->xmit_work, sja1105_port_deferred_xmit); 3680a68578c2SVladimir Oltean sp->xmit_worker = kthread_create_worker(0, "%s_xmit", 3681a68578c2SVladimir Oltean slave->name); 3682a68578c2SVladimir Oltean if (IS_ERR(sp->xmit_worker)) { 3683a68578c2SVladimir Oltean rc = PTR_ERR(sp->xmit_worker); 3684a68578c2SVladimir Oltean dev_err(ds->dev, 3685a68578c2SVladimir Oltean "failed to create deferred xmit thread: %d\n", 3686a68578c2SVladimir Oltean rc); 3687dc596e3fSVladimir Oltean goto out_destroy_workers; 3688a68578c2SVladimir Oltean } 3689a68578c2SVladimir Oltean skb_queue_head_init(&sp->xmit_queue); 369038b5beeaSVladimir Oltean sp->xmit_tpid = ETH_P_SJA1105; 369184eeb5d4SVladimir Oltean 369284eeb5d4SVladimir Oltean for (subvlan = 0; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++) 369384eeb5d4SVladimir Oltean sp->subvlan_map[subvlan] = VLAN_N_VID; 3694227d07a0SVladimir Oltean } 3695227d07a0SVladimir Oltean 3696d5a619bfSVivien Didelot return 0; 3697dc596e3fSVladimir Oltean 3698dc596e3fSVladimir Oltean out_destroy_workers: 3699a68578c2SVladimir Oltean while (port-- > 0) { 3700a68578c2SVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 3701a68578c2SVladimir Oltean 3702a68578c2SVladimir Oltean if (!dsa_is_user_port(ds, port)) 3703a68578c2SVladimir Oltean continue; 3704a68578c2SVladimir Oltean 3705a68578c2SVladimir Oltean kthread_destroy_worker(sp->xmit_worker); 3706a68578c2SVladimir Oltean } 3707dc596e3fSVladimir Oltean 3708dc596e3fSVladimir Oltean out_unregister_switch: 3709dc596e3fSVladimir Oltean dsa_unregister_switch(ds); 3710dc596e3fSVladimir Oltean 3711a68578c2SVladimir Oltean return rc; 37128aa9ebccSVladimir Oltean } 37138aa9ebccSVladimir Oltean 37148aa9ebccSVladimir Oltean static int sja1105_remove(struct spi_device *spi) 37158aa9ebccSVladimir Oltean { 37168aa9ebccSVladimir Oltean struct sja1105_private *priv = spi_get_drvdata(spi); 37178aa9ebccSVladimir Oltean 37188aa9ebccSVladimir Oltean dsa_unregister_switch(priv->ds); 37198aa9ebccSVladimir Oltean return 0; 37208aa9ebccSVladimir Oltean } 37218aa9ebccSVladimir Oltean 37228aa9ebccSVladimir Oltean static const struct of_device_id sja1105_dt_ids[] = { 37238aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105e", .data = &sja1105e_info }, 37248aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105t", .data = &sja1105t_info }, 37258aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105p", .data = &sja1105p_info }, 37268aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105q", .data = &sja1105q_info }, 37278aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105r", .data = &sja1105r_info }, 37288aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105s", .data = &sja1105s_info }, 37298aa9ebccSVladimir Oltean { /* sentinel */ }, 37308aa9ebccSVladimir Oltean }; 37318aa9ebccSVladimir Oltean MODULE_DEVICE_TABLE(of, sja1105_dt_ids); 37328aa9ebccSVladimir Oltean 37338aa9ebccSVladimir Oltean static struct spi_driver sja1105_driver = { 37348aa9ebccSVladimir Oltean .driver = { 37358aa9ebccSVladimir Oltean .name = "sja1105", 37368aa9ebccSVladimir Oltean .owner = THIS_MODULE, 37378aa9ebccSVladimir Oltean .of_match_table = of_match_ptr(sja1105_dt_ids), 37388aa9ebccSVladimir Oltean }, 37398aa9ebccSVladimir Oltean .probe = sja1105_probe, 37408aa9ebccSVladimir Oltean .remove = sja1105_remove, 37418aa9ebccSVladimir Oltean }; 37428aa9ebccSVladimir Oltean 37438aa9ebccSVladimir Oltean module_spi_driver(sja1105_driver); 37448aa9ebccSVladimir Oltean 37458aa9ebccSVladimir Oltean MODULE_AUTHOR("Vladimir Oltean <olteanv@gmail.com>"); 37468aa9ebccSVladimir Oltean MODULE_AUTHOR("Georg Waibel <georg.waibel@sensor-technik.de>"); 37478aa9ebccSVladimir Oltean MODULE_DESCRIPTION("SJA1105 Driver"); 37488aa9ebccSVladimir Oltean MODULE_LICENSE("GPL v2"); 3749