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 29ed040abcSVladimir Oltean #define SJA1105_DEFAULT_VLAN (VLAN_N_VID - 1) 304d942354SVladimir Oltean 31ac02a451SVladimir Oltean static const struct dsa_switch_ops sja1105_switch_ops; 32ac02a451SVladimir Oltean 338aa9ebccSVladimir Oltean static void sja1105_hw_reset(struct gpio_desc *gpio, unsigned int pulse_len, 348aa9ebccSVladimir Oltean unsigned int startup_delay) 358aa9ebccSVladimir Oltean { 368aa9ebccSVladimir Oltean gpiod_set_value_cansleep(gpio, 1); 378aa9ebccSVladimir Oltean /* Wait for minimum reset pulse length */ 388aa9ebccSVladimir Oltean msleep(pulse_len); 398aa9ebccSVladimir Oltean gpiod_set_value_cansleep(gpio, 0); 408aa9ebccSVladimir Oltean /* Wait until chip is ready after reset */ 418aa9ebccSVladimir Oltean msleep(startup_delay); 428aa9ebccSVladimir Oltean } 438aa9ebccSVladimir Oltean 448aa9ebccSVladimir Oltean static void 458aa9ebccSVladimir Oltean sja1105_port_allow_traffic(struct sja1105_l2_forwarding_entry *l2_fwd, 468aa9ebccSVladimir Oltean int from, int to, bool allow) 478aa9ebccSVladimir Oltean { 484d942354SVladimir Oltean if (allow) 498aa9ebccSVladimir Oltean l2_fwd[from].reach_port |= BIT(to); 504d942354SVladimir Oltean else 518aa9ebccSVladimir Oltean l2_fwd[from].reach_port &= ~BIT(to); 528aa9ebccSVladimir Oltean } 538aa9ebccSVladimir Oltean 547f7ccdeaSVladimir Oltean static bool sja1105_can_forward(struct sja1105_l2_forwarding_entry *l2_fwd, 557f7ccdeaSVladimir Oltean int from, int to) 567f7ccdeaSVladimir Oltean { 577f7ccdeaSVladimir Oltean return !!(l2_fwd[from].reach_port & BIT(to)); 587f7ccdeaSVladimir Oltean } 597f7ccdeaSVladimir Oltean 608aa9ebccSVladimir Oltean static int sja1105_init_mac_settings(struct sja1105_private *priv) 618aa9ebccSVladimir Oltean { 628aa9ebccSVladimir Oltean struct sja1105_mac_config_entry default_mac = { 638aa9ebccSVladimir Oltean /* Enable all 8 priority queues on egress. 648aa9ebccSVladimir Oltean * Every queue i holds top[i] - base[i] frames. 658aa9ebccSVladimir Oltean * Sum of top[i] - base[i] is 511 (max hardware limit). 668aa9ebccSVladimir Oltean */ 678aa9ebccSVladimir Oltean .top = {0x3F, 0x7F, 0xBF, 0xFF, 0x13F, 0x17F, 0x1BF, 0x1FF}, 688aa9ebccSVladimir Oltean .base = {0x0, 0x40, 0x80, 0xC0, 0x100, 0x140, 0x180, 0x1C0}, 698aa9ebccSVladimir Oltean .enabled = {true, true, true, true, true, true, true, true}, 708aa9ebccSVladimir Oltean /* Keep standard IFG of 12 bytes on egress. */ 718aa9ebccSVladimir Oltean .ifg = 0, 728aa9ebccSVladimir Oltean /* Always put the MAC speed in automatic mode, where it can be 731fd4a173SVladimir Oltean * adjusted at runtime by PHYLINK. 748aa9ebccSVladimir Oltean */ 7541fed17fSVladimir Oltean .speed = priv->info->port_speed[SJA1105_SPEED_AUTO], 768aa9ebccSVladimir Oltean /* No static correction for 1-step 1588 events */ 778aa9ebccSVladimir Oltean .tp_delin = 0, 788aa9ebccSVladimir Oltean .tp_delout = 0, 798aa9ebccSVladimir Oltean /* Disable aging for critical TTEthernet traffic */ 808aa9ebccSVladimir Oltean .maxage = 0xFF, 818aa9ebccSVladimir Oltean /* Internal VLAN (pvid) to apply to untagged ingress */ 828aa9ebccSVladimir Oltean .vlanprio = 0, 83e3502b82SVladimir Oltean .vlanid = 1, 848aa9ebccSVladimir Oltean .ing_mirr = false, 858aa9ebccSVladimir Oltean .egr_mirr = false, 868aa9ebccSVladimir Oltean /* Don't drop traffic with other EtherType than ETH_P_IP */ 878aa9ebccSVladimir Oltean .drpnona664 = false, 888aa9ebccSVladimir Oltean /* Don't drop double-tagged traffic */ 898aa9ebccSVladimir Oltean .drpdtag = false, 908aa9ebccSVladimir Oltean /* Don't drop untagged traffic */ 918aa9ebccSVladimir Oltean .drpuntag = false, 928aa9ebccSVladimir Oltean /* Don't retag 802.1p (VID 0) traffic with the pvid */ 938aa9ebccSVladimir Oltean .retag = false, 94640f763fSVladimir Oltean /* Disable learning and I/O on user ports by default - 95640f763fSVladimir Oltean * STP will enable it. 96640f763fSVladimir Oltean */ 97640f763fSVladimir Oltean .dyn_learn = false, 988aa9ebccSVladimir Oltean .egress = false, 998aa9ebccSVladimir Oltean .ingress = false, 1008aa9ebccSVladimir Oltean }; 1018aa9ebccSVladimir Oltean struct sja1105_mac_config_entry *mac; 102542043e9SVladimir Oltean struct dsa_switch *ds = priv->ds; 1038aa9ebccSVladimir Oltean struct sja1105_table *table; 1048aa9ebccSVladimir Oltean int i; 1058aa9ebccSVladimir Oltean 1068aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_MAC_CONFIG]; 1078aa9ebccSVladimir Oltean 1088aa9ebccSVladimir Oltean /* Discard previous MAC Configuration Table */ 1098aa9ebccSVladimir Oltean if (table->entry_count) { 1108aa9ebccSVladimir Oltean kfree(table->entries); 1118aa9ebccSVladimir Oltean table->entry_count = 0; 1128aa9ebccSVladimir Oltean } 1138aa9ebccSVladimir Oltean 114fd6f2c25SVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 1158aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 1168aa9ebccSVladimir Oltean if (!table->entries) 1178aa9ebccSVladimir Oltean return -ENOMEM; 1188aa9ebccSVladimir Oltean 119fd6f2c25SVladimir Oltean table->entry_count = table->ops->max_entry_count; 1208aa9ebccSVladimir Oltean 1218aa9ebccSVladimir Oltean mac = table->entries; 1228aa9ebccSVladimir Oltean 123542043e9SVladimir Oltean for (i = 0; i < ds->num_ports; i++) { 1248aa9ebccSVladimir Oltean mac[i] = default_mac; 125640f763fSVladimir Oltean if (i == dsa_upstream_port(priv->ds, i)) { 126640f763fSVladimir Oltean /* STP doesn't get called for CPU port, so we need to 127640f763fSVladimir Oltean * set the I/O parameters statically. 128640f763fSVladimir Oltean */ 129640f763fSVladimir Oltean mac[i].dyn_learn = true; 130640f763fSVladimir Oltean mac[i].ingress = true; 131640f763fSVladimir Oltean mac[i].egress = true; 132640f763fSVladimir Oltean } 133640f763fSVladimir Oltean } 1348aa9ebccSVladimir Oltean 1358aa9ebccSVladimir Oltean return 0; 1368aa9ebccSVladimir Oltean } 1378aa9ebccSVladimir Oltean 1385d645df9SVladimir Oltean static int sja1105_init_mii_settings(struct sja1105_private *priv) 1398aa9ebccSVladimir Oltean { 1408aa9ebccSVladimir Oltean struct device *dev = &priv->spidev->dev; 1418aa9ebccSVladimir Oltean struct sja1105_xmii_params_entry *mii; 142542043e9SVladimir Oltean struct dsa_switch *ds = priv->ds; 1438aa9ebccSVladimir Oltean struct sja1105_table *table; 1448aa9ebccSVladimir Oltean int i; 1458aa9ebccSVladimir Oltean 1468aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_XMII_PARAMS]; 1478aa9ebccSVladimir Oltean 1488aa9ebccSVladimir Oltean /* Discard previous xMII Mode Parameters Table */ 1498aa9ebccSVladimir Oltean if (table->entry_count) { 1508aa9ebccSVladimir Oltean kfree(table->entries); 1518aa9ebccSVladimir Oltean table->entry_count = 0; 1528aa9ebccSVladimir Oltean } 1538aa9ebccSVladimir Oltean 154fd6f2c25SVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 1558aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 1568aa9ebccSVladimir Oltean if (!table->entries) 1578aa9ebccSVladimir Oltean return -ENOMEM; 1588aa9ebccSVladimir Oltean 1591fd4a173SVladimir Oltean /* Override table based on PHYLINK DT bindings */ 160fd6f2c25SVladimir Oltean table->entry_count = table->ops->max_entry_count; 1618aa9ebccSVladimir Oltean 1628aa9ebccSVladimir Oltean mii = table->entries; 1638aa9ebccSVladimir Oltean 164542043e9SVladimir Oltean for (i = 0; i < ds->num_ports; i++) { 1655d645df9SVladimir Oltean sja1105_mii_role_t role = XMII_MAC; 1665d645df9SVladimir Oltean 167ee9d0cb6SVladimir Oltean if (dsa_is_unused_port(priv->ds, i)) 168ee9d0cb6SVladimir Oltean continue; 169ee9d0cb6SVladimir Oltean 1705d645df9SVladimir Oltean switch (priv->phy_mode[i]) { 1715d645df9SVladimir Oltean case PHY_INTERFACE_MODE_REVMII: 1725d645df9SVladimir Oltean role = XMII_PHY; 1735d645df9SVladimir Oltean fallthrough; 1748aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_MII: 17591a05078SVladimir Oltean if (!priv->info->supports_mii[i]) 17691a05078SVladimir Oltean goto unsupported; 17791a05078SVladimir Oltean 1788aa9ebccSVladimir Oltean mii->xmii_mode[i] = XMII_MODE_MII; 1798aa9ebccSVladimir Oltean break; 1805d645df9SVladimir Oltean case PHY_INTERFACE_MODE_REVRMII: 1815d645df9SVladimir Oltean role = XMII_PHY; 1825d645df9SVladimir Oltean fallthrough; 1838aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RMII: 18491a05078SVladimir Oltean if (!priv->info->supports_rmii[i]) 18591a05078SVladimir Oltean goto unsupported; 18691a05078SVladimir Oltean 1878aa9ebccSVladimir Oltean mii->xmii_mode[i] = XMII_MODE_RMII; 1888aa9ebccSVladimir Oltean break; 1898aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RGMII: 1908aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RGMII_ID: 1918aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RGMII_RXID: 1928aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RGMII_TXID: 19391a05078SVladimir Oltean if (!priv->info->supports_rgmii[i]) 19491a05078SVladimir Oltean goto unsupported; 19591a05078SVladimir Oltean 1968aa9ebccSVladimir Oltean mii->xmii_mode[i] = XMII_MODE_RGMII; 1978aa9ebccSVladimir Oltean break; 198ffe10e67SVladimir Oltean case PHY_INTERFACE_MODE_SGMII: 19991a05078SVladimir Oltean if (!priv->info->supports_sgmii[i]) 20091a05078SVladimir Oltean goto unsupported; 20191a05078SVladimir Oltean 202ffe10e67SVladimir Oltean mii->xmii_mode[i] = XMII_MODE_SGMII; 203ffe10e67SVladimir Oltean break; 20491a05078SVladimir Oltean case PHY_INTERFACE_MODE_2500BASEX: 20591a05078SVladimir Oltean if (!priv->info->supports_2500basex[i]) 20691a05078SVladimir Oltean goto unsupported; 20791a05078SVladimir Oltean 20891a05078SVladimir Oltean mii->xmii_mode[i] = XMII_MODE_SGMII; 20991a05078SVladimir Oltean break; 21091a05078SVladimir Oltean unsupported: 2118aa9ebccSVladimir Oltean default: 21291a05078SVladimir Oltean dev_err(dev, "Unsupported PHY mode %s on port %d!\n", 2135d645df9SVladimir Oltean phy_modes(priv->phy_mode[i]), i); 2146729188dSVladimir Oltean return -EINVAL; 2158aa9ebccSVladimir Oltean } 2168aa9ebccSVladimir Oltean 2175d645df9SVladimir Oltean mii->phy_mac[i] = role; 2188aa9ebccSVladimir Oltean } 2198aa9ebccSVladimir Oltean return 0; 2208aa9ebccSVladimir Oltean } 2218aa9ebccSVladimir Oltean 2228aa9ebccSVladimir Oltean static int sja1105_init_static_fdb(struct sja1105_private *priv) 2238aa9ebccSVladimir Oltean { 2244d942354SVladimir Oltean struct sja1105_l2_lookup_entry *l2_lookup; 2258aa9ebccSVladimir Oltean struct sja1105_table *table; 2264d942354SVladimir Oltean int port; 2278aa9ebccSVladimir Oltean 2288aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; 2298aa9ebccSVladimir Oltean 2304d942354SVladimir Oltean /* We only populate the FDB table through dynamic L2 Address Lookup 2314d942354SVladimir Oltean * entries, except for a special entry at the end which is a catch-all 2324d942354SVladimir Oltean * for unknown multicast and will be used to control flooding domain. 233291d1e72SVladimir Oltean */ 2348aa9ebccSVladimir Oltean if (table->entry_count) { 2358aa9ebccSVladimir Oltean kfree(table->entries); 2368aa9ebccSVladimir Oltean table->entry_count = 0; 2378aa9ebccSVladimir Oltean } 2384d942354SVladimir Oltean 2394d942354SVladimir Oltean if (!priv->info->can_limit_mcast_flood) 2404d942354SVladimir Oltean return 0; 2414d942354SVladimir Oltean 2424d942354SVladimir Oltean table->entries = kcalloc(1, table->ops->unpacked_entry_size, 2434d942354SVladimir Oltean GFP_KERNEL); 2444d942354SVladimir Oltean if (!table->entries) 2454d942354SVladimir Oltean return -ENOMEM; 2464d942354SVladimir Oltean 2474d942354SVladimir Oltean table->entry_count = 1; 2484d942354SVladimir Oltean l2_lookup = table->entries; 2494d942354SVladimir Oltean 2504d942354SVladimir Oltean /* All L2 multicast addresses have an odd first octet */ 2514d942354SVladimir Oltean l2_lookup[0].macaddr = SJA1105_UNKNOWN_MULTICAST; 2524d942354SVladimir Oltean l2_lookup[0].mask_macaddr = SJA1105_UNKNOWN_MULTICAST; 2534d942354SVladimir Oltean l2_lookup[0].lockeds = true; 2544d942354SVladimir Oltean l2_lookup[0].index = SJA1105_MAX_L2_LOOKUP_COUNT - 1; 2554d942354SVladimir Oltean 2564d942354SVladimir Oltean /* Flood multicast to every port by default */ 2574d942354SVladimir Oltean for (port = 0; port < priv->ds->num_ports; port++) 2584d942354SVladimir Oltean if (!dsa_is_unused_port(priv->ds, port)) 2594d942354SVladimir Oltean l2_lookup[0].destports |= BIT(port); 2604d942354SVladimir Oltean 2618aa9ebccSVladimir Oltean return 0; 2628aa9ebccSVladimir Oltean } 2638aa9ebccSVladimir Oltean 2648aa9ebccSVladimir Oltean static int sja1105_init_l2_lookup_params(struct sja1105_private *priv) 2658aa9ebccSVladimir Oltean { 2668aa9ebccSVladimir Oltean struct sja1105_l2_lookup_params_entry default_l2_lookup_params = { 2678456721dSVladimir Oltean /* Learned FDB entries are forgotten after 300 seconds */ 2688456721dSVladimir Oltean .maxage = SJA1105_AGEING_TIME_MS(300000), 2698aa9ebccSVladimir Oltean /* All entries within a FDB bin are available for learning */ 2708aa9ebccSVladimir Oltean .dyn_tbsz = SJA1105ET_FDB_BIN_SIZE, 2711da73821SVladimir Oltean /* And the P/Q/R/S equivalent setting: */ 2721da73821SVladimir Oltean .start_dynspc = 0, 2738aa9ebccSVladimir Oltean /* 2^8 + 2^5 + 2^3 + 2^2 + 2^1 + 1 in Koopman notation */ 2748aa9ebccSVladimir Oltean .poly = 0x97, 2758aa9ebccSVladimir Oltean /* This selects between Independent VLAN Learning (IVL) and 2768aa9ebccSVladimir Oltean * Shared VLAN Learning (SVL) 2778aa9ebccSVladimir Oltean */ 2786d7c7d94SVladimir Oltean .shared_learn = true, 2798aa9ebccSVladimir Oltean /* Don't discard management traffic based on ENFPORT - 2808aa9ebccSVladimir Oltean * we don't perform SMAC port enforcement anyway, so 2818aa9ebccSVladimir Oltean * what we are setting here doesn't matter. 2828aa9ebccSVladimir Oltean */ 2838aa9ebccSVladimir Oltean .no_enf_hostprt = false, 2848aa9ebccSVladimir Oltean /* Don't learn SMAC for mac_fltres1 and mac_fltres0. 2858aa9ebccSVladimir Oltean * Maybe correlate with no_linklocal_learn from bridge driver? 2868aa9ebccSVladimir Oltean */ 2878aa9ebccSVladimir Oltean .no_mgmt_learn = true, 2881da73821SVladimir Oltean /* P/Q/R/S only */ 2891da73821SVladimir Oltean .use_static = true, 2901da73821SVladimir Oltean /* Dynamically learned FDB entries can overwrite other (older) 2911da73821SVladimir Oltean * dynamic FDB entries 2921da73821SVladimir Oltean */ 2931da73821SVladimir Oltean .owr_dyn = true, 2941da73821SVladimir Oltean .drpnolearn = true, 2958aa9ebccSVladimir Oltean }; 296542043e9SVladimir Oltean struct dsa_switch *ds = priv->ds; 297f238fef1SVladimir Oltean int port, num_used_ports = 0; 298542043e9SVladimir Oltean struct sja1105_table *table; 299542043e9SVladimir Oltean u64 max_fdb_entries; 300542043e9SVladimir Oltean 301542043e9SVladimir Oltean for (port = 0; port < ds->num_ports; port++) 302f238fef1SVladimir Oltean if (!dsa_is_unused_port(ds, port)) 303f238fef1SVladimir Oltean num_used_ports++; 304f238fef1SVladimir Oltean 305f238fef1SVladimir Oltean max_fdb_entries = SJA1105_MAX_L2_LOOKUP_COUNT / num_used_ports; 306f238fef1SVladimir Oltean 307f238fef1SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 308f238fef1SVladimir Oltean if (dsa_is_unused_port(ds, port)) 309f238fef1SVladimir Oltean continue; 310f238fef1SVladimir Oltean 311542043e9SVladimir Oltean default_l2_lookup_params.maxaddrp[port] = max_fdb_entries; 312f238fef1SVladimir Oltean } 3138aa9ebccSVladimir Oltean 3148aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS]; 3158aa9ebccSVladimir Oltean 3168aa9ebccSVladimir Oltean if (table->entry_count) { 3178aa9ebccSVladimir Oltean kfree(table->entries); 3188aa9ebccSVladimir Oltean table->entry_count = 0; 3198aa9ebccSVladimir Oltean } 3208aa9ebccSVladimir Oltean 321fd6f2c25SVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 3228aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 3238aa9ebccSVladimir Oltean if (!table->entries) 3248aa9ebccSVladimir Oltean return -ENOMEM; 3258aa9ebccSVladimir Oltean 326fd6f2c25SVladimir Oltean table->entry_count = table->ops->max_entry_count; 3278aa9ebccSVladimir Oltean 3288aa9ebccSVladimir Oltean /* This table only has a single entry */ 3298aa9ebccSVladimir Oltean ((struct sja1105_l2_lookup_params_entry *)table->entries)[0] = 3308aa9ebccSVladimir Oltean default_l2_lookup_params; 3318aa9ebccSVladimir Oltean 3328aa9ebccSVladimir Oltean return 0; 3338aa9ebccSVladimir Oltean } 3348aa9ebccSVladimir Oltean 335ed040abcSVladimir Oltean /* Set up a default VLAN for untagged traffic injected from the CPU 336ed040abcSVladimir Oltean * using management routes (e.g. STP, PTP) as opposed to tag_8021q. 337ed040abcSVladimir Oltean * All DT-defined ports are members of this VLAN, and there are no 338ed040abcSVladimir Oltean * restrictions on forwarding (since the CPU selects the destination). 339ed040abcSVladimir Oltean * Frames from this VLAN will always be transmitted as untagged, and 340ed040abcSVladimir Oltean * neither the bridge nor the 8021q module cannot create this VLAN ID. 341ed040abcSVladimir Oltean */ 3428aa9ebccSVladimir Oltean static int sja1105_init_static_vlan(struct sja1105_private *priv) 3438aa9ebccSVladimir Oltean { 3448aa9ebccSVladimir Oltean struct sja1105_table *table; 3458aa9ebccSVladimir Oltean struct sja1105_vlan_lookup_entry pvid = { 346*3e77e59bSVladimir Oltean .type_entry = SJA1110_VLAN_D_TAG, 3478aa9ebccSVladimir Oltean .ving_mirr = 0, 3488aa9ebccSVladimir Oltean .vegr_mirr = 0, 3498aa9ebccSVladimir Oltean .vmemb_port = 0, 3508aa9ebccSVladimir Oltean .vlan_bc = 0, 3518aa9ebccSVladimir Oltean .tag_port = 0, 352ed040abcSVladimir Oltean .vlanid = SJA1105_DEFAULT_VLAN, 3538aa9ebccSVladimir Oltean }; 354ec5ae610SVladimir Oltean struct dsa_switch *ds = priv->ds; 355ec5ae610SVladimir Oltean int port; 3568aa9ebccSVladimir Oltean 3578aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP]; 3588aa9ebccSVladimir Oltean 3598aa9ebccSVladimir Oltean if (table->entry_count) { 3608aa9ebccSVladimir Oltean kfree(table->entries); 3618aa9ebccSVladimir Oltean table->entry_count = 0; 3628aa9ebccSVladimir Oltean } 3638aa9ebccSVladimir Oltean 364c75857b0SZheng Yongjun table->entries = kzalloc(table->ops->unpacked_entry_size, 3658aa9ebccSVladimir Oltean GFP_KERNEL); 3668aa9ebccSVladimir Oltean if (!table->entries) 3678aa9ebccSVladimir Oltean return -ENOMEM; 3688aa9ebccSVladimir Oltean 3698aa9ebccSVladimir Oltean table->entry_count = 1; 3708aa9ebccSVladimir Oltean 371ec5ae610SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 372ec5ae610SVladimir Oltean struct sja1105_bridge_vlan *v; 373ec5ae610SVladimir Oltean 374ec5ae610SVladimir Oltean if (dsa_is_unused_port(ds, port)) 375ec5ae610SVladimir Oltean continue; 376ec5ae610SVladimir Oltean 377ec5ae610SVladimir Oltean pvid.vmemb_port |= BIT(port); 378ec5ae610SVladimir Oltean pvid.vlan_bc |= BIT(port); 379ec5ae610SVladimir Oltean pvid.tag_port &= ~BIT(port); 380ec5ae610SVladimir Oltean 381ec5ae610SVladimir Oltean v = kzalloc(sizeof(*v), GFP_KERNEL); 382ec5ae610SVladimir Oltean if (!v) 383ec5ae610SVladimir Oltean return -ENOMEM; 384ec5ae610SVladimir Oltean 385ec5ae610SVladimir Oltean v->port = port; 386ed040abcSVladimir Oltean v->vid = SJA1105_DEFAULT_VLAN; 387ec5ae610SVladimir Oltean v->untagged = true; 388ec5ae610SVladimir Oltean if (dsa_is_cpu_port(ds, port)) 389ec5ae610SVladimir Oltean v->pvid = true; 390ec5ae610SVladimir Oltean list_add(&v->list, &priv->dsa_8021q_vlans); 3918aa9ebccSVladimir Oltean } 3928aa9ebccSVladimir Oltean 3938aa9ebccSVladimir Oltean ((struct sja1105_vlan_lookup_entry *)table->entries)[0] = pvid; 3948aa9ebccSVladimir Oltean return 0; 3958aa9ebccSVladimir Oltean } 3968aa9ebccSVladimir Oltean 3978aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding(struct sja1105_private *priv) 3988aa9ebccSVladimir Oltean { 3998aa9ebccSVladimir Oltean struct sja1105_l2_forwarding_entry *l2fwd; 400542043e9SVladimir Oltean struct dsa_switch *ds = priv->ds; 4018aa9ebccSVladimir Oltean struct sja1105_table *table; 4028aa9ebccSVladimir Oltean int i, j; 4038aa9ebccSVladimir Oltean 4048aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING]; 4058aa9ebccSVladimir Oltean 4068aa9ebccSVladimir Oltean if (table->entry_count) { 4078aa9ebccSVladimir Oltean kfree(table->entries); 4088aa9ebccSVladimir Oltean table->entry_count = 0; 4098aa9ebccSVladimir Oltean } 4108aa9ebccSVladimir Oltean 411fd6f2c25SVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 4128aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 4138aa9ebccSVladimir Oltean if (!table->entries) 4148aa9ebccSVladimir Oltean return -ENOMEM; 4158aa9ebccSVladimir Oltean 416fd6f2c25SVladimir Oltean table->entry_count = table->ops->max_entry_count; 4178aa9ebccSVladimir Oltean 4188aa9ebccSVladimir Oltean l2fwd = table->entries; 4198aa9ebccSVladimir Oltean 4208aa9ebccSVladimir Oltean /* First 5 entries define the forwarding rules */ 421542043e9SVladimir Oltean for (i = 0; i < ds->num_ports; i++) { 4228aa9ebccSVladimir Oltean unsigned int upstream = dsa_upstream_port(priv->ds, i); 4238aa9ebccSVladimir Oltean 424f238fef1SVladimir Oltean if (dsa_is_unused_port(ds, i)) 425f238fef1SVladimir Oltean continue; 426f238fef1SVladimir Oltean 4278aa9ebccSVladimir Oltean for (j = 0; j < SJA1105_NUM_TC; j++) 4288aa9ebccSVladimir Oltean l2fwd[i].vlan_pmap[j] = j; 4298aa9ebccSVladimir Oltean 4307f7ccdeaSVladimir Oltean /* All ports start up with egress flooding enabled, 4317f7ccdeaSVladimir Oltean * including the CPU port. 4327f7ccdeaSVladimir Oltean */ 4337f7ccdeaSVladimir Oltean priv->ucast_egress_floods |= BIT(i); 4347f7ccdeaSVladimir Oltean priv->bcast_egress_floods |= BIT(i); 4357f7ccdeaSVladimir Oltean 4368aa9ebccSVladimir Oltean if (i == upstream) 4378aa9ebccSVladimir Oltean continue; 4388aa9ebccSVladimir Oltean 4398aa9ebccSVladimir Oltean sja1105_port_allow_traffic(l2fwd, i, upstream, true); 4408aa9ebccSVladimir Oltean sja1105_port_allow_traffic(l2fwd, upstream, i, true); 4414d942354SVladimir Oltean 4424d942354SVladimir Oltean l2fwd[i].bc_domain = BIT(upstream); 4434d942354SVladimir Oltean l2fwd[i].fl_domain = BIT(upstream); 4444d942354SVladimir Oltean 4454d942354SVladimir Oltean l2fwd[upstream].bc_domain |= BIT(i); 4464d942354SVladimir Oltean l2fwd[upstream].fl_domain |= BIT(i); 4478aa9ebccSVladimir Oltean } 448f238fef1SVladimir Oltean 4498aa9ebccSVladimir Oltean /* Next 8 entries define VLAN PCP mapping from ingress to egress. 4508aa9ebccSVladimir Oltean * Create a one-to-one mapping. 4518aa9ebccSVladimir Oltean */ 452f238fef1SVladimir Oltean for (i = 0; i < SJA1105_NUM_TC; i++) { 453f238fef1SVladimir Oltean for (j = 0; j < ds->num_ports; j++) { 454f238fef1SVladimir Oltean if (dsa_is_unused_port(ds, j)) 455f238fef1SVladimir Oltean continue; 456f238fef1SVladimir Oltean 457542043e9SVladimir Oltean l2fwd[ds->num_ports + i].vlan_pmap[j] = i; 458f238fef1SVladimir Oltean } 459*3e77e59bSVladimir Oltean 460*3e77e59bSVladimir Oltean l2fwd[ds->num_ports + i].type_egrpcp2outputq = true; 461*3e77e59bSVladimir Oltean } 462*3e77e59bSVladimir Oltean 463*3e77e59bSVladimir Oltean return 0; 464*3e77e59bSVladimir Oltean } 465*3e77e59bSVladimir Oltean 466*3e77e59bSVladimir Oltean static int sja1110_init_pcp_remapping(struct sja1105_private *priv) 467*3e77e59bSVladimir Oltean { 468*3e77e59bSVladimir Oltean struct sja1110_pcp_remapping_entry *pcp_remap; 469*3e77e59bSVladimir Oltean struct dsa_switch *ds = priv->ds; 470*3e77e59bSVladimir Oltean struct sja1105_table *table; 471*3e77e59bSVladimir Oltean int port, tc; 472*3e77e59bSVladimir Oltean 473*3e77e59bSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_PCP_REMAPPING]; 474*3e77e59bSVladimir Oltean 475*3e77e59bSVladimir Oltean /* Nothing to do for SJA1105 */ 476*3e77e59bSVladimir Oltean if (!table->ops->max_entry_count) 477*3e77e59bSVladimir Oltean return 0; 478*3e77e59bSVladimir Oltean 479*3e77e59bSVladimir Oltean if (table->entry_count) { 480*3e77e59bSVladimir Oltean kfree(table->entries); 481*3e77e59bSVladimir Oltean table->entry_count = 0; 482*3e77e59bSVladimir Oltean } 483*3e77e59bSVladimir Oltean 484*3e77e59bSVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 485*3e77e59bSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 486*3e77e59bSVladimir Oltean if (!table->entries) 487*3e77e59bSVladimir Oltean return -ENOMEM; 488*3e77e59bSVladimir Oltean 489*3e77e59bSVladimir Oltean table->entry_count = table->ops->max_entry_count; 490*3e77e59bSVladimir Oltean 491*3e77e59bSVladimir Oltean pcp_remap = table->entries; 492*3e77e59bSVladimir Oltean 493*3e77e59bSVladimir Oltean /* Repeat the configuration done for vlan_pmap */ 494*3e77e59bSVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 495*3e77e59bSVladimir Oltean if (dsa_is_unused_port(ds, port)) 496*3e77e59bSVladimir Oltean continue; 497*3e77e59bSVladimir Oltean 498*3e77e59bSVladimir Oltean for (tc = 0; tc < SJA1105_NUM_TC; tc++) 499*3e77e59bSVladimir Oltean pcp_remap[port].egrpcp[tc] = tc; 500f238fef1SVladimir Oltean } 5018aa9ebccSVladimir Oltean 5028aa9ebccSVladimir Oltean return 0; 5038aa9ebccSVladimir Oltean } 5048aa9ebccSVladimir Oltean 5058aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding_params(struct sja1105_private *priv) 5068aa9ebccSVladimir Oltean { 5071bf658eeSVladimir Oltean struct sja1105_l2_forwarding_params_entry *l2fwd_params; 5088aa9ebccSVladimir Oltean struct sja1105_table *table; 5098aa9ebccSVladimir Oltean 5108aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS]; 5118aa9ebccSVladimir Oltean 5128aa9ebccSVladimir Oltean if (table->entry_count) { 5138aa9ebccSVladimir Oltean kfree(table->entries); 5148aa9ebccSVladimir Oltean table->entry_count = 0; 5158aa9ebccSVladimir Oltean } 5168aa9ebccSVladimir Oltean 517fd6f2c25SVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 5188aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 5198aa9ebccSVladimir Oltean if (!table->entries) 5208aa9ebccSVladimir Oltean return -ENOMEM; 5218aa9ebccSVladimir Oltean 522fd6f2c25SVladimir Oltean table->entry_count = table->ops->max_entry_count; 5238aa9ebccSVladimir Oltean 5248aa9ebccSVladimir Oltean /* This table only has a single entry */ 5251bf658eeSVladimir Oltean l2fwd_params = table->entries; 5261bf658eeSVladimir Oltean 5271bf658eeSVladimir Oltean /* Disallow dynamic reconfiguration of vlan_pmap */ 5281bf658eeSVladimir Oltean l2fwd_params->max_dynp = 0; 5291bf658eeSVladimir Oltean /* Use a single memory partition for all ingress queues */ 5301bf658eeSVladimir Oltean l2fwd_params->part_spc[0] = priv->info->max_frame_mem; 5318aa9ebccSVladimir Oltean 5328aa9ebccSVladimir Oltean return 0; 5338aa9ebccSVladimir Oltean } 5348aa9ebccSVladimir Oltean 535aaa270c6SVladimir Oltean void sja1105_frame_memory_partitioning(struct sja1105_private *priv) 536aaa270c6SVladimir Oltean { 537aaa270c6SVladimir Oltean struct sja1105_l2_forwarding_params_entry *l2_fwd_params; 538aaa270c6SVladimir Oltean struct sja1105_vl_forwarding_params_entry *vl_fwd_params; 5391bf658eeSVladimir Oltean int max_mem = priv->info->max_frame_mem; 540aaa270c6SVladimir Oltean struct sja1105_table *table; 541aaa270c6SVladimir Oltean 542aaa270c6SVladimir Oltean /* VLAN retagging is implemented using a loopback port that consumes 543aaa270c6SVladimir Oltean * frame buffers. That leaves less for us. 544aaa270c6SVladimir Oltean */ 545aaa270c6SVladimir Oltean if (priv->vlan_state == SJA1105_VLAN_BEST_EFFORT) 5461bf658eeSVladimir Oltean max_mem -= SJA1105_FRAME_MEMORY_RETAGGING_OVERHEAD; 547aaa270c6SVladimir Oltean 548aaa270c6SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS]; 549aaa270c6SVladimir Oltean l2_fwd_params = table->entries; 550aaa270c6SVladimir Oltean l2_fwd_params->part_spc[0] = max_mem; 551aaa270c6SVladimir Oltean 552aaa270c6SVladimir Oltean /* If we have any critical-traffic virtual links, we need to reserve 553aaa270c6SVladimir Oltean * some frame buffer memory for them. At the moment, hardcode the value 554aaa270c6SVladimir Oltean * at 100 blocks of 128 bytes of memory each. This leaves 829 blocks 555aaa270c6SVladimir Oltean * remaining for best-effort traffic. TODO: figure out a more flexible 556aaa270c6SVladimir Oltean * way to perform the frame buffer partitioning. 557aaa270c6SVladimir Oltean */ 558aaa270c6SVladimir Oltean if (!priv->static_config.tables[BLK_IDX_VL_FORWARDING].entry_count) 559aaa270c6SVladimir Oltean return; 560aaa270c6SVladimir Oltean 561aaa270c6SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_VL_FORWARDING_PARAMS]; 562aaa270c6SVladimir Oltean vl_fwd_params = table->entries; 563aaa270c6SVladimir Oltean 564aaa270c6SVladimir Oltean l2_fwd_params->part_spc[0] -= SJA1105_VL_FRAME_MEMORY; 565aaa270c6SVladimir Oltean vl_fwd_params->partspc[0] = SJA1105_VL_FRAME_MEMORY; 566aaa270c6SVladimir Oltean } 567aaa270c6SVladimir Oltean 5688aa9ebccSVladimir Oltean static int sja1105_init_general_params(struct sja1105_private *priv) 5698aa9ebccSVladimir Oltean { 5708aa9ebccSVladimir Oltean struct sja1105_general_params_entry default_general_params = { 571511e6ca0SVladimir Oltean /* Allow dynamic changing of the mirror port */ 572511e6ca0SVladimir Oltean .mirr_ptacu = true, 5738aa9ebccSVladimir Oltean .switchid = priv->ds->index, 5745f06c63bSVladimir Oltean /* Priority queue for link-local management frames 5755f06c63bSVladimir Oltean * (both ingress to and egress from CPU - PTP, STP etc) 5765f06c63bSVladimir Oltean */ 57708fde09aSVladimir Oltean .hostprio = 7, 5788aa9ebccSVladimir Oltean .mac_fltres1 = SJA1105_LINKLOCAL_FILTER_A, 5798aa9ebccSVladimir Oltean .mac_flt1 = SJA1105_LINKLOCAL_FILTER_A_MASK, 58042824463SVladimir Oltean .incl_srcpt1 = false, 5818aa9ebccSVladimir Oltean .send_meta1 = false, 5828aa9ebccSVladimir Oltean .mac_fltres0 = SJA1105_LINKLOCAL_FILTER_B, 5838aa9ebccSVladimir Oltean .mac_flt0 = SJA1105_LINKLOCAL_FILTER_B_MASK, 58442824463SVladimir Oltean .incl_srcpt0 = false, 5858aa9ebccSVladimir Oltean .send_meta0 = false, 5868aa9ebccSVladimir Oltean /* The destination for traffic matching mac_fltres1 and 5878aa9ebccSVladimir Oltean * mac_fltres0 on all ports except host_port. Such traffic 5888aa9ebccSVladimir Oltean * receieved on host_port itself would be dropped, except 5898aa9ebccSVladimir Oltean * by installing a temporary 'management route' 5908aa9ebccSVladimir Oltean */ 591df2a81a3SVladimir Oltean .host_port = priv->ds->num_ports, 592511e6ca0SVladimir Oltean /* Default to an invalid value */ 593542043e9SVladimir Oltean .mirr_port = priv->ds->num_ports, 5948aa9ebccSVladimir Oltean /* Link-local traffic received on casc_port will be forwarded 5958aa9ebccSVladimir Oltean * to host_port without embedding the source port and device ID 5968aa9ebccSVladimir Oltean * info in the destination MAC address (presumably because it 5978aa9ebccSVladimir Oltean * is a cascaded port and a downstream SJA switch already did 5988aa9ebccSVladimir Oltean * that). Default to an invalid port (to disable the feature) 5998aa9ebccSVladimir Oltean * and overwrite this if we find any DSA (cascaded) ports. 6008aa9ebccSVladimir Oltean */ 601542043e9SVladimir Oltean .casc_port = priv->ds->num_ports, 6028aa9ebccSVladimir Oltean /* No TTEthernet */ 603dfacc5a2SVladimir Oltean .vllupformat = SJA1105_VL_FORMAT_PSFP, 6048aa9ebccSVladimir Oltean .vlmarker = 0, 6058aa9ebccSVladimir Oltean .vlmask = 0, 6068aa9ebccSVladimir Oltean /* Only update correctionField for 1-step PTP (L2 transport) */ 6078aa9ebccSVladimir Oltean .ignore2stf = 0, 6086666cebcSVladimir Oltean /* Forcefully disable VLAN filtering by telling 6096666cebcSVladimir Oltean * the switch that VLAN has a different EtherType. 6106666cebcSVladimir Oltean */ 6116666cebcSVladimir Oltean .tpid = ETH_P_SJA1105, 6126666cebcSVladimir Oltean .tpid2 = ETH_P_SJA1105, 6138aa9ebccSVladimir Oltean }; 614df2a81a3SVladimir Oltean struct dsa_switch *ds = priv->ds; 6158aa9ebccSVladimir Oltean struct sja1105_table *table; 616df2a81a3SVladimir Oltean int port; 617df2a81a3SVladimir Oltean 618df2a81a3SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 619df2a81a3SVladimir Oltean if (dsa_is_cpu_port(ds, port)) { 620df2a81a3SVladimir Oltean default_general_params.host_port = port; 621df2a81a3SVladimir Oltean break; 622df2a81a3SVladimir Oltean } 623df2a81a3SVladimir Oltean } 6248aa9ebccSVladimir Oltean 6258aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; 6268aa9ebccSVladimir Oltean 6278aa9ebccSVladimir Oltean if (table->entry_count) { 6288aa9ebccSVladimir Oltean kfree(table->entries); 6298aa9ebccSVladimir Oltean table->entry_count = 0; 6308aa9ebccSVladimir Oltean } 6318aa9ebccSVladimir Oltean 632fd6f2c25SVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 6338aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 6348aa9ebccSVladimir Oltean if (!table->entries) 6358aa9ebccSVladimir Oltean return -ENOMEM; 6368aa9ebccSVladimir Oltean 637fd6f2c25SVladimir Oltean table->entry_count = table->ops->max_entry_count; 6388aa9ebccSVladimir Oltean 6398aa9ebccSVladimir Oltean /* This table only has a single entry */ 6408aa9ebccSVladimir Oltean ((struct sja1105_general_params_entry *)table->entries)[0] = 6418aa9ebccSVladimir Oltean default_general_params; 6428aa9ebccSVladimir Oltean 6438aa9ebccSVladimir Oltean return 0; 6448aa9ebccSVladimir Oltean } 6458aa9ebccSVladimir Oltean 64679d5511cSVladimir Oltean static int sja1105_init_avb_params(struct sja1105_private *priv) 64779d5511cSVladimir Oltean { 64879d5511cSVladimir Oltean struct sja1105_avb_params_entry *avb; 64979d5511cSVladimir Oltean struct sja1105_table *table; 65079d5511cSVladimir Oltean 65179d5511cSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_AVB_PARAMS]; 65279d5511cSVladimir Oltean 65379d5511cSVladimir Oltean /* Discard previous AVB Parameters Table */ 65479d5511cSVladimir Oltean if (table->entry_count) { 65579d5511cSVladimir Oltean kfree(table->entries); 65679d5511cSVladimir Oltean table->entry_count = 0; 65779d5511cSVladimir Oltean } 65879d5511cSVladimir Oltean 659fd6f2c25SVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 66079d5511cSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 66179d5511cSVladimir Oltean if (!table->entries) 66279d5511cSVladimir Oltean return -ENOMEM; 66379d5511cSVladimir Oltean 664fd6f2c25SVladimir Oltean table->entry_count = table->ops->max_entry_count; 66579d5511cSVladimir Oltean 66679d5511cSVladimir Oltean avb = table->entries; 66779d5511cSVladimir Oltean 66879d5511cSVladimir Oltean /* Configure the MAC addresses for meta frames */ 66979d5511cSVladimir Oltean avb->destmeta = SJA1105_META_DMAC; 67079d5511cSVladimir Oltean avb->srcmeta = SJA1105_META_SMAC; 671747e5eb3SVladimir Oltean /* On P/Q/R/S, configure the direction of the PTP_CLK pin as input by 672747e5eb3SVladimir Oltean * default. This is because there might be boards with a hardware 673747e5eb3SVladimir Oltean * layout where enabling the pin as output might cause an electrical 674747e5eb3SVladimir Oltean * clash. On E/T the pin is always an output, which the board designers 675747e5eb3SVladimir Oltean * probably already knew, so even if there are going to be electrical 676747e5eb3SVladimir Oltean * issues, there's nothing we can do. 677747e5eb3SVladimir Oltean */ 678747e5eb3SVladimir Oltean avb->cas_master = false; 67979d5511cSVladimir Oltean 68079d5511cSVladimir Oltean return 0; 68179d5511cSVladimir Oltean } 68279d5511cSVladimir Oltean 683a7cc081cSVladimir Oltean /* The L2 policing table is 2-stage. The table is looked up for each frame 684a7cc081cSVladimir Oltean * according to the ingress port, whether it was broadcast or not, and the 685a7cc081cSVladimir Oltean * classified traffic class (given by VLAN PCP). This portion of the lookup is 686a7cc081cSVladimir Oltean * fixed, and gives access to the SHARINDX, an indirection register pointing 687a7cc081cSVladimir Oltean * within the policing table itself, which is used to resolve the policer that 688a7cc081cSVladimir Oltean * will be used for this frame. 689a7cc081cSVladimir Oltean * 690a7cc081cSVladimir Oltean * Stage 1 Stage 2 691a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 692a7cc081cSVladimir Oltean * |Port 0 TC 0 |SHARINDX| | Policer 0: Rate, Burst, MTU | 693a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 694a7cc081cSVladimir Oltean * |Port 0 TC 1 |SHARINDX| | Policer 1: Rate, Burst, MTU | 695a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 696a7cc081cSVladimir Oltean * ... | Policer 2: Rate, Burst, MTU | 697a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 698a7cc081cSVladimir Oltean * |Port 0 TC 7 |SHARINDX| | Policer 3: Rate, Burst, MTU | 699a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 700a7cc081cSVladimir Oltean * |Port 1 TC 0 |SHARINDX| | Policer 4: Rate, Burst, MTU | 701a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 702a7cc081cSVladimir Oltean * ... | Policer 5: Rate, Burst, MTU | 703a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 704a7cc081cSVladimir Oltean * |Port 1 TC 7 |SHARINDX| | Policer 6: Rate, Burst, MTU | 705a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 706a7cc081cSVladimir Oltean * ... | Policer 7: Rate, Burst, MTU | 707a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 708a7cc081cSVladimir Oltean * |Port 4 TC 7 |SHARINDX| ... 709a7cc081cSVladimir Oltean * +------------+--------+ 710a7cc081cSVladimir Oltean * |Port 0 BCAST|SHARINDX| ... 711a7cc081cSVladimir Oltean * +------------+--------+ 712a7cc081cSVladimir Oltean * |Port 1 BCAST|SHARINDX| ... 713a7cc081cSVladimir Oltean * +------------+--------+ 714a7cc081cSVladimir Oltean * ... ... 715a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 716a7cc081cSVladimir Oltean * |Port 4 BCAST|SHARINDX| | Policer 44: Rate, Burst, MTU | 717a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 718a7cc081cSVladimir Oltean * 719a7cc081cSVladimir Oltean * In this driver, we shall use policers 0-4 as statically alocated port 720a7cc081cSVladimir Oltean * (matchall) policers. So we need to make the SHARINDX for all lookups 721a7cc081cSVladimir Oltean * corresponding to this ingress port (8 VLAN PCP lookups and 1 broadcast 722a7cc081cSVladimir Oltean * lookup) equal. 723a7cc081cSVladimir Oltean * The remaining policers (40) shall be dynamically allocated for flower 724a7cc081cSVladimir Oltean * policers, where the key is either vlan_prio or dst_mac ff:ff:ff:ff:ff:ff. 725a7cc081cSVladimir Oltean */ 7268aa9ebccSVladimir Oltean #define SJA1105_RATE_MBPS(speed) (((speed) * 64000) / 1000) 7278aa9ebccSVladimir Oltean 7288aa9ebccSVladimir Oltean static int sja1105_init_l2_policing(struct sja1105_private *priv) 7298aa9ebccSVladimir Oltean { 7308aa9ebccSVladimir Oltean struct sja1105_l2_policing_entry *policing; 731542043e9SVladimir Oltean struct dsa_switch *ds = priv->ds; 7328aa9ebccSVladimir Oltean struct sja1105_table *table; 733a7cc081cSVladimir Oltean int port, tc; 7348aa9ebccSVladimir Oltean 7358aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_POLICING]; 7368aa9ebccSVladimir Oltean 7378aa9ebccSVladimir Oltean /* Discard previous L2 Policing Table */ 7388aa9ebccSVladimir Oltean if (table->entry_count) { 7398aa9ebccSVladimir Oltean kfree(table->entries); 7408aa9ebccSVladimir Oltean table->entry_count = 0; 7418aa9ebccSVladimir Oltean } 7428aa9ebccSVladimir Oltean 743fd6f2c25SVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 7448aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 7458aa9ebccSVladimir Oltean if (!table->entries) 7468aa9ebccSVladimir Oltean return -ENOMEM; 7478aa9ebccSVladimir Oltean 748fd6f2c25SVladimir Oltean table->entry_count = table->ops->max_entry_count; 7498aa9ebccSVladimir Oltean 7508aa9ebccSVladimir Oltean policing = table->entries; 7518aa9ebccSVladimir Oltean 752a7cc081cSVladimir Oltean /* Setup shared indices for the matchall policers */ 753542043e9SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 75438fbe91fSVladimir Oltean int mcast = (ds->num_ports * (SJA1105_NUM_TC + 1)) + port; 755542043e9SVladimir Oltean int bcast = (ds->num_ports * SJA1105_NUM_TC) + port; 756a7cc081cSVladimir Oltean 757a7cc081cSVladimir Oltean for (tc = 0; tc < SJA1105_NUM_TC; tc++) 758a7cc081cSVladimir Oltean policing[port * SJA1105_NUM_TC + tc].sharindx = port; 759a7cc081cSVladimir Oltean 760a7cc081cSVladimir Oltean policing[bcast].sharindx = port; 76138fbe91fSVladimir Oltean /* Only SJA1110 has multicast policers */ 76238fbe91fSVladimir Oltean if (mcast <= table->ops->max_entry_count) 76338fbe91fSVladimir Oltean policing[mcast].sharindx = port; 764a7cc081cSVladimir Oltean } 765a7cc081cSVladimir Oltean 766a7cc081cSVladimir Oltean /* Setup the matchall policer parameters */ 767542043e9SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 768c279c726SVladimir Oltean int mtu = VLAN_ETH_FRAME_LEN + ETH_FCS_LEN; 769c279c726SVladimir Oltean 770a7cc081cSVladimir Oltean if (dsa_is_cpu_port(priv->ds, port)) 771c279c726SVladimir Oltean mtu += VLAN_HLEN; 7728aa9ebccSVladimir Oltean 773a7cc081cSVladimir Oltean policing[port].smax = 65535; /* Burst size in bytes */ 774a7cc081cSVladimir Oltean policing[port].rate = SJA1105_RATE_MBPS(1000); 775a7cc081cSVladimir Oltean policing[port].maxlen = mtu; 776a7cc081cSVladimir Oltean policing[port].partition = 0; 7778aa9ebccSVladimir Oltean } 778a7cc081cSVladimir Oltean 7798aa9ebccSVladimir Oltean return 0; 7808aa9ebccSVladimir Oltean } 7818aa9ebccSVladimir Oltean 7825d645df9SVladimir Oltean static int sja1105_static_config_load(struct sja1105_private *priv) 7838aa9ebccSVladimir Oltean { 7848aa9ebccSVladimir Oltean int rc; 7858aa9ebccSVladimir Oltean 7868aa9ebccSVladimir Oltean sja1105_static_config_free(&priv->static_config); 7878aa9ebccSVladimir Oltean rc = sja1105_static_config_init(&priv->static_config, 7888aa9ebccSVladimir Oltean priv->info->static_ops, 7898aa9ebccSVladimir Oltean priv->info->device_id); 7908aa9ebccSVladimir Oltean if (rc) 7918aa9ebccSVladimir Oltean return rc; 7928aa9ebccSVladimir Oltean 7938aa9ebccSVladimir Oltean /* Build static configuration */ 7948aa9ebccSVladimir Oltean rc = sja1105_init_mac_settings(priv); 7958aa9ebccSVladimir Oltean if (rc < 0) 7968aa9ebccSVladimir Oltean return rc; 7975d645df9SVladimir Oltean rc = sja1105_init_mii_settings(priv); 7988aa9ebccSVladimir Oltean if (rc < 0) 7998aa9ebccSVladimir Oltean return rc; 8008aa9ebccSVladimir Oltean rc = sja1105_init_static_fdb(priv); 8018aa9ebccSVladimir Oltean if (rc < 0) 8028aa9ebccSVladimir Oltean return rc; 8038aa9ebccSVladimir Oltean rc = sja1105_init_static_vlan(priv); 8048aa9ebccSVladimir Oltean if (rc < 0) 8058aa9ebccSVladimir Oltean return rc; 8068aa9ebccSVladimir Oltean rc = sja1105_init_l2_lookup_params(priv); 8078aa9ebccSVladimir Oltean if (rc < 0) 8088aa9ebccSVladimir Oltean return rc; 8098aa9ebccSVladimir Oltean rc = sja1105_init_l2_forwarding(priv); 8108aa9ebccSVladimir Oltean if (rc < 0) 8118aa9ebccSVladimir Oltean return rc; 8128aa9ebccSVladimir Oltean rc = sja1105_init_l2_forwarding_params(priv); 8138aa9ebccSVladimir Oltean if (rc < 0) 8148aa9ebccSVladimir Oltean return rc; 8158aa9ebccSVladimir Oltean rc = sja1105_init_l2_policing(priv); 8168aa9ebccSVladimir Oltean if (rc < 0) 8178aa9ebccSVladimir Oltean return rc; 8188aa9ebccSVladimir Oltean rc = sja1105_init_general_params(priv); 8198aa9ebccSVladimir Oltean if (rc < 0) 8208aa9ebccSVladimir Oltean return rc; 82179d5511cSVladimir Oltean rc = sja1105_init_avb_params(priv); 82279d5511cSVladimir Oltean if (rc < 0) 82379d5511cSVladimir Oltean return rc; 824*3e77e59bSVladimir Oltean rc = sja1110_init_pcp_remapping(priv); 825*3e77e59bSVladimir Oltean if (rc < 0) 826*3e77e59bSVladimir Oltean return rc; 8278aa9ebccSVladimir Oltean 8288aa9ebccSVladimir Oltean /* Send initial configuration to hardware via SPI */ 8298aa9ebccSVladimir Oltean return sja1105_static_config_upload(priv); 8308aa9ebccSVladimir Oltean } 8318aa9ebccSVladimir Oltean 83229afb83aSVladimir Oltean static int sja1105_parse_rgmii_delays(struct sja1105_private *priv) 833f5b8631cSVladimir Oltean { 834542043e9SVladimir Oltean struct dsa_switch *ds = priv->ds; 83529afb83aSVladimir Oltean int port; 836f5b8631cSVladimir Oltean 83729afb83aSVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 83829afb83aSVladimir Oltean if (!priv->fixed_link[port]) 839f5b8631cSVladimir Oltean continue; 840f5b8631cSVladimir Oltean 84129afb83aSVladimir Oltean if (priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_RXID || 84229afb83aSVladimir Oltean priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_ID) 84329afb83aSVladimir Oltean priv->rgmii_rx_delay[port] = true; 844f5b8631cSVladimir Oltean 84529afb83aSVladimir Oltean if (priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_TXID || 84629afb83aSVladimir Oltean priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_ID) 84729afb83aSVladimir Oltean priv->rgmii_tx_delay[port] = true; 848f5b8631cSVladimir Oltean 84929afb83aSVladimir Oltean if ((priv->rgmii_rx_delay[port] || priv->rgmii_tx_delay[port]) && 850f5b8631cSVladimir Oltean !priv->info->setup_rgmii_delay) 851f5b8631cSVladimir Oltean return -EINVAL; 852f5b8631cSVladimir Oltean } 853f5b8631cSVladimir Oltean return 0; 854f5b8631cSVladimir Oltean } 855f5b8631cSVladimir Oltean 8568aa9ebccSVladimir Oltean static int sja1105_parse_ports_node(struct sja1105_private *priv, 8578aa9ebccSVladimir Oltean struct device_node *ports_node) 8588aa9ebccSVladimir Oltean { 8598aa9ebccSVladimir Oltean struct device *dev = &priv->spidev->dev; 8608aa9ebccSVladimir Oltean struct device_node *child; 8618aa9ebccSVladimir Oltean 86227afe0d3SVladimir Oltean for_each_available_child_of_node(ports_node, child) { 8638aa9ebccSVladimir Oltean struct device_node *phy_node; 8640c65b2b9SAndrew Lunn phy_interface_t phy_mode; 8658aa9ebccSVladimir Oltean u32 index; 8660c65b2b9SAndrew Lunn int err; 8678aa9ebccSVladimir Oltean 8688aa9ebccSVladimir Oltean /* Get switch port number from DT */ 8698aa9ebccSVladimir Oltean if (of_property_read_u32(child, "reg", &index) < 0) { 8708aa9ebccSVladimir Oltean dev_err(dev, "Port number not defined in device tree " 8718aa9ebccSVladimir Oltean "(property \"reg\")\n"); 8727ba771e3SNishka Dasgupta of_node_put(child); 8738aa9ebccSVladimir Oltean return -ENODEV; 8748aa9ebccSVladimir Oltean } 8758aa9ebccSVladimir Oltean 8768aa9ebccSVladimir Oltean /* Get PHY mode from DT */ 8770c65b2b9SAndrew Lunn err = of_get_phy_mode(child, &phy_mode); 8780c65b2b9SAndrew Lunn if (err) { 8798aa9ebccSVladimir Oltean dev_err(dev, "Failed to read phy-mode or " 8808aa9ebccSVladimir Oltean "phy-interface-type property for port %d\n", 8818aa9ebccSVladimir Oltean index); 8827ba771e3SNishka Dasgupta of_node_put(child); 8838aa9ebccSVladimir Oltean return -ENODEV; 8848aa9ebccSVladimir Oltean } 8858aa9ebccSVladimir Oltean 8868aa9ebccSVladimir Oltean phy_node = of_parse_phandle(child, "phy-handle", 0); 8878aa9ebccSVladimir Oltean if (!phy_node) { 8888aa9ebccSVladimir Oltean if (!of_phy_is_fixed_link(child)) { 8898aa9ebccSVladimir Oltean dev_err(dev, "phy-handle or fixed-link " 8908aa9ebccSVladimir Oltean "properties missing!\n"); 8917ba771e3SNishka Dasgupta of_node_put(child); 8928aa9ebccSVladimir Oltean return -ENODEV; 8938aa9ebccSVladimir Oltean } 8948aa9ebccSVladimir Oltean /* phy-handle is missing, but fixed-link isn't. 8958aa9ebccSVladimir Oltean * So it's a fixed link. Default to PHY role. 8968aa9ebccSVladimir Oltean */ 89729afb83aSVladimir Oltean priv->fixed_link[index] = true; 8988aa9ebccSVladimir Oltean } else { 8998aa9ebccSVladimir Oltean of_node_put(phy_node); 9008aa9ebccSVladimir Oltean } 9018aa9ebccSVladimir Oltean 902bf4edf4aSVladimir Oltean priv->phy_mode[index] = phy_mode; 9038aa9ebccSVladimir Oltean } 9048aa9ebccSVladimir Oltean 9058aa9ebccSVladimir Oltean return 0; 9068aa9ebccSVladimir Oltean } 9078aa9ebccSVladimir Oltean 9085d645df9SVladimir Oltean static int sja1105_parse_dt(struct sja1105_private *priv) 9098aa9ebccSVladimir Oltean { 9108aa9ebccSVladimir Oltean struct device *dev = &priv->spidev->dev; 9118aa9ebccSVladimir Oltean struct device_node *switch_node = dev->of_node; 9128aa9ebccSVladimir Oltean struct device_node *ports_node; 9138aa9ebccSVladimir Oltean int rc; 9148aa9ebccSVladimir Oltean 9158aa9ebccSVladimir Oltean ports_node = of_get_child_by_name(switch_node, "ports"); 91615074a36SVladimir Oltean if (!ports_node) 91715074a36SVladimir Oltean ports_node = of_get_child_by_name(switch_node, "ethernet-ports"); 9188aa9ebccSVladimir Oltean if (!ports_node) { 9198aa9ebccSVladimir Oltean dev_err(dev, "Incorrect bindings: absent \"ports\" node\n"); 9208aa9ebccSVladimir Oltean return -ENODEV; 9218aa9ebccSVladimir Oltean } 9228aa9ebccSVladimir Oltean 9235d645df9SVladimir Oltean rc = sja1105_parse_ports_node(priv, ports_node); 9248aa9ebccSVladimir Oltean of_node_put(ports_node); 9258aa9ebccSVladimir Oltean 9268aa9ebccSVladimir Oltean return rc; 9278aa9ebccSVladimir Oltean } 9288aa9ebccSVladimir Oltean 9294c7ee010SVladimir Oltean static int sja1105_sgmii_read(struct sja1105_private *priv, int port, int mmd, 93084db00f2SVladimir Oltean int pcs_reg) 931ffe10e67SVladimir Oltean { 9324c7ee010SVladimir Oltean u64 addr = (mmd << 16) | pcs_reg; 933ffe10e67SVladimir Oltean u32 val; 934ffe10e67SVladimir Oltean int rc; 935ffe10e67SVladimir Oltean 93684db00f2SVladimir Oltean if (port != SJA1105_SGMII_PORT) 93784db00f2SVladimir Oltean return -ENODEV; 93884db00f2SVladimir Oltean 9394c7ee010SVladimir Oltean rc = sja1105_xfer_u32(priv, SPI_READ, addr, &val, NULL); 940ffe10e67SVladimir Oltean if (rc < 0) 941ffe10e67SVladimir Oltean return rc; 942ffe10e67SVladimir Oltean 943ffe10e67SVladimir Oltean return val; 944ffe10e67SVladimir Oltean } 945ffe10e67SVladimir Oltean 9464c7ee010SVladimir Oltean static int sja1105_sgmii_write(struct sja1105_private *priv, int port, int mmd, 94784db00f2SVladimir Oltean int pcs_reg, u16 pcs_val) 948ffe10e67SVladimir Oltean { 9494c7ee010SVladimir Oltean u64 addr = (mmd << 16) | pcs_reg; 950ffe10e67SVladimir Oltean u32 val = pcs_val; 951ffe10e67SVladimir Oltean int rc; 952ffe10e67SVladimir Oltean 95384db00f2SVladimir Oltean if (port != SJA1105_SGMII_PORT) 95484db00f2SVladimir Oltean return -ENODEV; 95584db00f2SVladimir Oltean 9564c7ee010SVladimir Oltean rc = sja1105_xfer_u32(priv, SPI_WRITE, addr, &val, NULL); 957ffe10e67SVladimir Oltean if (rc < 0) 958ffe10e67SVladimir Oltean return rc; 959ffe10e67SVladimir Oltean 960ffe10e67SVladimir Oltean return val; 961ffe10e67SVladimir Oltean } 962ffe10e67SVladimir Oltean 96384db00f2SVladimir Oltean static void sja1105_sgmii_pcs_config(struct sja1105_private *priv, int port, 964ffe10e67SVladimir Oltean bool an_enabled, bool an_master) 965ffe10e67SVladimir Oltean { 966ffe10e67SVladimir Oltean u16 ac = SJA1105_AC_AUTONEG_MODE_SGMII; 967ffe10e67SVladimir Oltean 968ffe10e67SVladimir Oltean /* DIGITAL_CONTROL_1: Enable vendor-specific MMD1, allow the PHY to 969ffe10e67SVladimir Oltean * stop the clock during LPI mode, make the MAC reconfigure 970ffe10e67SVladimir Oltean * autonomously after PCS autoneg is done, flush the internal FIFOs. 971ffe10e67SVladimir Oltean */ 9724c7ee010SVladimir Oltean sja1105_sgmii_write(priv, port, MDIO_MMD_VEND2, SJA1105_DC1, 97384db00f2SVladimir Oltean SJA1105_DC1_EN_VSMMD1 | 974ffe10e67SVladimir Oltean SJA1105_DC1_CLOCK_STOP_EN | 975ffe10e67SVladimir Oltean SJA1105_DC1_MAC_AUTO_SW | 976ffe10e67SVladimir Oltean SJA1105_DC1_INIT); 977ffe10e67SVladimir Oltean /* DIGITAL_CONTROL_2: No polarity inversion for TX and RX lanes */ 9784c7ee010SVladimir Oltean sja1105_sgmii_write(priv, port, MDIO_MMD_VEND2, SJA1105_DC2, 97984db00f2SVladimir Oltean SJA1105_DC2_TX_POL_INV_DISABLE); 980ffe10e67SVladimir Oltean /* AUTONEG_CONTROL: Use SGMII autoneg */ 981ffe10e67SVladimir Oltean if (an_master) 982ffe10e67SVladimir Oltean ac |= SJA1105_AC_PHY_MODE | SJA1105_AC_SGMII_LINK; 9834c7ee010SVladimir Oltean sja1105_sgmii_write(priv, port, MDIO_MMD_VEND2, SJA1105_AC, ac); 984ffe10e67SVladimir Oltean /* BASIC_CONTROL: enable in-band AN now, if requested. Otherwise, 985ffe10e67SVladimir Oltean * sja1105_sgmii_pcs_force_speed must be called later for the link 986ffe10e67SVladimir Oltean * to become operational. 987ffe10e67SVladimir Oltean */ 988ffe10e67SVladimir Oltean if (an_enabled) 9894c7ee010SVladimir Oltean sja1105_sgmii_write(priv, port, MDIO_MMD_VEND2, MDIO_CTRL1, 990ffe10e67SVladimir Oltean BMCR_ANENABLE | BMCR_ANRESTART); 991ffe10e67SVladimir Oltean } 992ffe10e67SVladimir Oltean 993ffe10e67SVladimir Oltean static void sja1105_sgmii_pcs_force_speed(struct sja1105_private *priv, 99484db00f2SVladimir Oltean int port, int speed) 995ffe10e67SVladimir Oltean { 996ffe10e67SVladimir Oltean int pcs_speed; 997ffe10e67SVladimir Oltean 998ffe10e67SVladimir Oltean switch (speed) { 999ffe10e67SVladimir Oltean case SPEED_1000: 1000ffe10e67SVladimir Oltean pcs_speed = BMCR_SPEED1000; 1001ffe10e67SVladimir Oltean break; 1002ffe10e67SVladimir Oltean case SPEED_100: 1003ffe10e67SVladimir Oltean pcs_speed = BMCR_SPEED100; 1004ffe10e67SVladimir Oltean break; 1005ffe10e67SVladimir Oltean case SPEED_10: 1006ffe10e67SVladimir Oltean pcs_speed = BMCR_SPEED10; 1007ffe10e67SVladimir Oltean break; 1008ffe10e67SVladimir Oltean default: 1009ffe10e67SVladimir Oltean dev_err(priv->ds->dev, "Invalid speed %d\n", speed); 1010ffe10e67SVladimir Oltean return; 1011ffe10e67SVladimir Oltean } 10124c7ee010SVladimir Oltean sja1105_sgmii_write(priv, port, MDIO_MMD_VEND2, MDIO_CTRL1, 10134c7ee010SVladimir Oltean pcs_speed | BMCR_FULLDPLX); 1014ffe10e67SVladimir Oltean } 1015ffe10e67SVladimir Oltean 1016c44d0535SVladimir Oltean /* Convert link speed from SJA1105 to ethtool encoding */ 101741fed17fSVladimir Oltean static int sja1105_port_speed_to_ethtool(struct sja1105_private *priv, 101841fed17fSVladimir Oltean u64 speed) 101941fed17fSVladimir Oltean { 102041fed17fSVladimir Oltean if (speed == priv->info->port_speed[SJA1105_SPEED_10MBPS]) 102141fed17fSVladimir Oltean return SPEED_10; 102241fed17fSVladimir Oltean if (speed == priv->info->port_speed[SJA1105_SPEED_100MBPS]) 102341fed17fSVladimir Oltean return SPEED_100; 102441fed17fSVladimir Oltean if (speed == priv->info->port_speed[SJA1105_SPEED_1000MBPS]) 102541fed17fSVladimir Oltean return SPEED_1000; 102641fed17fSVladimir Oltean if (speed == priv->info->port_speed[SJA1105_SPEED_2500MBPS]) 102741fed17fSVladimir Oltean return SPEED_2500; 102841fed17fSVladimir Oltean return SPEED_UNKNOWN; 102941fed17fSVladimir Oltean } 10308aa9ebccSVladimir Oltean 10318400cff6SVladimir Oltean /* Set link speed in the MAC configuration for a specific port. */ 10328aa9ebccSVladimir Oltean static int sja1105_adjust_port_config(struct sja1105_private *priv, int port, 10338400cff6SVladimir Oltean int speed_mbps) 10348aa9ebccSVladimir Oltean { 10358aa9ebccSVladimir Oltean struct sja1105_mac_config_entry *mac; 10368aa9ebccSVladimir Oltean struct device *dev = priv->ds->dev; 103741fed17fSVladimir Oltean u64 speed; 10388aa9ebccSVladimir Oltean int rc; 10398aa9ebccSVladimir Oltean 10408400cff6SVladimir Oltean /* On P/Q/R/S, one can read from the device via the MAC reconfiguration 10418400cff6SVladimir Oltean * tables. On E/T, MAC reconfig tables are not readable, only writable. 10428400cff6SVladimir Oltean * We have to *know* what the MAC looks like. For the sake of keeping 10438400cff6SVladimir Oltean * the code common, we'll use the static configuration tables as a 10448400cff6SVladimir Oltean * reasonable approximation for both E/T and P/Q/R/S. 10458400cff6SVladimir Oltean */ 10468aa9ebccSVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 10478aa9ebccSVladimir Oltean 1048f4cfcfbdSVladimir Oltean switch (speed_mbps) { 1049c44d0535SVladimir Oltean case SPEED_UNKNOWN: 1050a979a0abSVladimir Oltean /* PHYLINK called sja1105_mac_config() to inform us about 1051a979a0abSVladimir Oltean * the state->interface, but AN has not completed and the 1052a979a0abSVladimir Oltean * speed is not yet valid. UM10944.pdf says that setting 1053a979a0abSVladimir Oltean * SJA1105_SPEED_AUTO at runtime disables the port, so that is 1054a979a0abSVladimir Oltean * ok for power consumption in case AN will never complete - 1055a979a0abSVladimir Oltean * otherwise PHYLINK should come back with a new update. 1056a979a0abSVladimir Oltean */ 105741fed17fSVladimir Oltean speed = priv->info->port_speed[SJA1105_SPEED_AUTO]; 1058f4cfcfbdSVladimir Oltean break; 1059c44d0535SVladimir Oltean case SPEED_10: 106041fed17fSVladimir Oltean speed = priv->info->port_speed[SJA1105_SPEED_10MBPS]; 1061f4cfcfbdSVladimir Oltean break; 1062c44d0535SVladimir Oltean case SPEED_100: 106341fed17fSVladimir Oltean speed = priv->info->port_speed[SJA1105_SPEED_100MBPS]; 1064f4cfcfbdSVladimir Oltean break; 1065c44d0535SVladimir Oltean case SPEED_1000: 106641fed17fSVladimir Oltean speed = priv->info->port_speed[SJA1105_SPEED_1000MBPS]; 1067f4cfcfbdSVladimir Oltean break; 1068f4cfcfbdSVladimir Oltean default: 10698aa9ebccSVladimir Oltean dev_err(dev, "Invalid speed %iMbps\n", speed_mbps); 10708aa9ebccSVladimir Oltean return -EINVAL; 10718aa9ebccSVladimir Oltean } 10728aa9ebccSVladimir Oltean 10738400cff6SVladimir Oltean /* Overwrite SJA1105_SPEED_AUTO from the static MAC configuration 10748400cff6SVladimir Oltean * table, since this will be used for the clocking setup, and we no 10758400cff6SVladimir Oltean * longer need to store it in the static config (already told hardware 10768400cff6SVladimir Oltean * we want auto during upload phase). 1077ffe10e67SVladimir Oltean * Actually for the SGMII port, the MAC is fixed at 1 Gbps and 1078ffe10e67SVladimir Oltean * we need to configure the PCS only (if even that). 10798aa9ebccSVladimir Oltean */ 108091a05078SVladimir Oltean if (priv->phy_mode[port] == PHY_INTERFACE_MODE_SGMII) 108141fed17fSVladimir Oltean mac[port].speed = priv->info->port_speed[SJA1105_SPEED_1000MBPS]; 1082ffe10e67SVladimir Oltean else 10838aa9ebccSVladimir Oltean mac[port].speed = speed; 10848aa9ebccSVladimir Oltean 10858aa9ebccSVladimir Oltean /* Write to the dynamic reconfiguration tables */ 10868400cff6SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, 10878400cff6SVladimir Oltean &mac[port], true); 10888aa9ebccSVladimir Oltean if (rc < 0) { 10898aa9ebccSVladimir Oltean dev_err(dev, "Failed to write MAC config: %d\n", rc); 10908aa9ebccSVladimir Oltean return rc; 10918aa9ebccSVladimir Oltean } 10928aa9ebccSVladimir Oltean 10938aa9ebccSVladimir Oltean /* Reconfigure the PLLs for the RGMII interfaces (required 125 MHz at 10948aa9ebccSVladimir Oltean * gigabit, 25 MHz at 100 Mbps and 2.5 MHz at 10 Mbps). For MII and 10958aa9ebccSVladimir Oltean * RMII no change of the clock setup is required. Actually, changing 10968aa9ebccSVladimir Oltean * the clock setup does interrupt the clock signal for a certain time 10978aa9ebccSVladimir Oltean * which causes trouble for all PHYs relying on this signal. 10988aa9ebccSVladimir Oltean */ 109991a05078SVladimir Oltean if (!phy_interface_mode_is_rgmii(priv->phy_mode[port])) 11008aa9ebccSVladimir Oltean return 0; 11018aa9ebccSVladimir Oltean 11028aa9ebccSVladimir Oltean return sja1105_clocking_setup_port(priv, port); 11038aa9ebccSVladimir Oltean } 11048aa9ebccSVladimir Oltean 110539710229SVladimir Oltean /* The SJA1105 MAC programming model is through the static config (the xMII 110639710229SVladimir Oltean * Mode table cannot be dynamically reconfigured), and we have to program 110739710229SVladimir Oltean * that early (earlier than PHYLINK calls us, anyway). 110839710229SVladimir Oltean * So just error out in case the connected PHY attempts to change the initial 110939710229SVladimir Oltean * system interface MII protocol from what is defined in the DT, at least for 111039710229SVladimir Oltean * now. 111139710229SVladimir Oltean */ 111239710229SVladimir Oltean static bool sja1105_phy_mode_mismatch(struct sja1105_private *priv, int port, 111339710229SVladimir Oltean phy_interface_t interface) 111439710229SVladimir Oltean { 1115bf4edf4aSVladimir Oltean return priv->phy_mode[port] != interface; 111639710229SVladimir Oltean } 111739710229SVladimir Oltean 1118af7cd036SVladimir Oltean static void sja1105_mac_config(struct dsa_switch *ds, int port, 1119ffe10e67SVladimir Oltean unsigned int mode, 1120af7cd036SVladimir Oltean const struct phylink_link_state *state) 11218aa9ebccSVladimir Oltean { 11228aa9ebccSVladimir Oltean struct sja1105_private *priv = ds->priv; 112391a05078SVladimir Oltean bool is_sgmii; 112491a05078SVladimir Oltean 112591a05078SVladimir Oltean is_sgmii = (state->interface == PHY_INTERFACE_MODE_SGMII); 11268aa9ebccSVladimir Oltean 1127ec8582d1SVladimir Oltean if (sja1105_phy_mode_mismatch(priv, port, state->interface)) { 1128ec8582d1SVladimir Oltean dev_err(ds->dev, "Changing PHY mode to %s not supported!\n", 1129ec8582d1SVladimir Oltean phy_modes(state->interface)); 113039710229SVladimir Oltean return; 1131ec8582d1SVladimir Oltean } 113239710229SVladimir Oltean 1133ffe10e67SVladimir Oltean if (phylink_autoneg_inband(mode) && !is_sgmii) { 11349f971573SVladimir Oltean dev_err(ds->dev, "In-band AN not supported!\n"); 11359f971573SVladimir Oltean return; 11369f971573SVladimir Oltean } 1137ffe10e67SVladimir Oltean 1138ffe10e67SVladimir Oltean if (is_sgmii) 113984db00f2SVladimir Oltean sja1105_sgmii_pcs_config(priv, port, 114084db00f2SVladimir Oltean phylink_autoneg_inband(mode), 1141ffe10e67SVladimir Oltean false); 11428400cff6SVladimir Oltean } 11438400cff6SVladimir Oltean 11448400cff6SVladimir Oltean static void sja1105_mac_link_down(struct dsa_switch *ds, int port, 11458400cff6SVladimir Oltean unsigned int mode, 11468400cff6SVladimir Oltean phy_interface_t interface) 11478400cff6SVladimir Oltean { 11488400cff6SVladimir Oltean sja1105_inhibit_tx(ds->priv, BIT(port), true); 11498400cff6SVladimir Oltean } 11508400cff6SVladimir Oltean 11518400cff6SVladimir Oltean static void sja1105_mac_link_up(struct dsa_switch *ds, int port, 11528400cff6SVladimir Oltean unsigned int mode, 11538400cff6SVladimir Oltean phy_interface_t interface, 11545b502a7bSRussell King struct phy_device *phydev, 11555b502a7bSRussell King int speed, int duplex, 11565b502a7bSRussell King bool tx_pause, bool rx_pause) 11578400cff6SVladimir Oltean { 1158ec8582d1SVladimir Oltean struct sja1105_private *priv = ds->priv; 1159ec8582d1SVladimir Oltean 1160ec8582d1SVladimir Oltean sja1105_adjust_port_config(priv, port, speed); 1161ec8582d1SVladimir Oltean 116291a05078SVladimir Oltean if (priv->phy_mode[port] == PHY_INTERFACE_MODE_SGMII && 116391a05078SVladimir Oltean !phylink_autoneg_inband(mode)) 116484db00f2SVladimir Oltean sja1105_sgmii_pcs_force_speed(priv, port, speed); 1165ffe10e67SVladimir Oltean 1166ec8582d1SVladimir Oltean sja1105_inhibit_tx(priv, BIT(port), false); 11678aa9ebccSVladimir Oltean } 11688aa9ebccSVladimir Oltean 1169ad9f299aSVladimir Oltean static void sja1105_phylink_validate(struct dsa_switch *ds, int port, 1170ad9f299aSVladimir Oltean unsigned long *supported, 1171ad9f299aSVladimir Oltean struct phylink_link_state *state) 1172ad9f299aSVladimir Oltean { 1173ad9f299aSVladimir Oltean /* Construct a new mask which exhaustively contains all link features 1174ad9f299aSVladimir Oltean * supported by the MAC, and then apply that (logical AND) to what will 1175ad9f299aSVladimir Oltean * be sent to the PHY for "marketing". 1176ad9f299aSVladimir Oltean */ 1177ad9f299aSVladimir Oltean __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, }; 1178ad9f299aSVladimir Oltean struct sja1105_private *priv = ds->priv; 1179ad9f299aSVladimir Oltean struct sja1105_xmii_params_entry *mii; 1180ad9f299aSVladimir Oltean 1181ad9f299aSVladimir Oltean mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries; 1182ad9f299aSVladimir Oltean 118339710229SVladimir Oltean /* include/linux/phylink.h says: 118439710229SVladimir Oltean * When @state->interface is %PHY_INTERFACE_MODE_NA, phylink 118539710229SVladimir Oltean * expects the MAC driver to return all supported link modes. 118639710229SVladimir Oltean */ 118739710229SVladimir Oltean if (state->interface != PHY_INTERFACE_MODE_NA && 118839710229SVladimir Oltean sja1105_phy_mode_mismatch(priv, port, state->interface)) { 118939710229SVladimir Oltean bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS); 119039710229SVladimir Oltean return; 119139710229SVladimir Oltean } 119239710229SVladimir Oltean 1193ad9f299aSVladimir Oltean /* The MAC does not support pause frames, and also doesn't 1194ad9f299aSVladimir Oltean * support half-duplex traffic modes. 1195ad9f299aSVladimir Oltean */ 1196ad9f299aSVladimir Oltean phylink_set(mask, Autoneg); 1197ad9f299aSVladimir Oltean phylink_set(mask, MII); 1198ad9f299aSVladimir Oltean phylink_set(mask, 10baseT_Full); 1199ad9f299aSVladimir Oltean phylink_set(mask, 100baseT_Full); 1200ca68e138SOleksij Rempel phylink_set(mask, 100baseT1_Full); 1201ffe10e67SVladimir Oltean if (mii->xmii_mode[port] == XMII_MODE_RGMII || 1202ffe10e67SVladimir Oltean mii->xmii_mode[port] == XMII_MODE_SGMII) 1203ad9f299aSVladimir Oltean phylink_set(mask, 1000baseT_Full); 1204ad9f299aSVladimir Oltean 1205ad9f299aSVladimir Oltean bitmap_and(supported, supported, mask, __ETHTOOL_LINK_MODE_MASK_NBITS); 1206ad9f299aSVladimir Oltean bitmap_and(state->advertising, state->advertising, mask, 1207ad9f299aSVladimir Oltean __ETHTOOL_LINK_MODE_MASK_NBITS); 1208ad9f299aSVladimir Oltean } 1209ad9f299aSVladimir Oltean 1210ffe10e67SVladimir Oltean static int sja1105_mac_pcs_get_state(struct dsa_switch *ds, int port, 1211ffe10e67SVladimir Oltean struct phylink_link_state *state) 1212ffe10e67SVladimir Oltean { 1213ffe10e67SVladimir Oltean struct sja1105_private *priv = ds->priv; 1214ffe10e67SVladimir Oltean int ais; 1215ffe10e67SVladimir Oltean 1216ffe10e67SVladimir Oltean /* Read the vendor-specific AUTONEG_INTR_STATUS register */ 12174c7ee010SVladimir Oltean ais = sja1105_sgmii_read(priv, port, MDIO_MMD_VEND2, SJA1105_AIS); 1218ffe10e67SVladimir Oltean if (ais < 0) 1219ffe10e67SVladimir Oltean return ais; 1220ffe10e67SVladimir Oltean 1221ffe10e67SVladimir Oltean switch (SJA1105_AIS_SPEED(ais)) { 1222ffe10e67SVladimir Oltean case 0: 1223ffe10e67SVladimir Oltean state->speed = SPEED_10; 1224ffe10e67SVladimir Oltean break; 1225ffe10e67SVladimir Oltean case 1: 1226ffe10e67SVladimir Oltean state->speed = SPEED_100; 1227ffe10e67SVladimir Oltean break; 1228ffe10e67SVladimir Oltean case 2: 1229ffe10e67SVladimir Oltean state->speed = SPEED_1000; 1230ffe10e67SVladimir Oltean break; 1231ffe10e67SVladimir Oltean default: 1232ffe10e67SVladimir Oltean dev_err(ds->dev, "Invalid SGMII PCS speed %lu\n", 1233ffe10e67SVladimir Oltean SJA1105_AIS_SPEED(ais)); 1234ffe10e67SVladimir Oltean } 1235ffe10e67SVladimir Oltean state->duplex = SJA1105_AIS_DUPLEX_MODE(ais); 1236ffe10e67SVladimir Oltean state->an_complete = SJA1105_AIS_COMPLETE(ais); 1237ffe10e67SVladimir Oltean state->link = SJA1105_AIS_LINK_STATUS(ais); 1238ffe10e67SVladimir Oltean 1239ffe10e67SVladimir Oltean return 0; 1240ffe10e67SVladimir Oltean } 1241ffe10e67SVladimir Oltean 124260f6053fSVladimir Oltean static int 124360f6053fSVladimir Oltean sja1105_find_static_fdb_entry(struct sja1105_private *priv, int port, 124460f6053fSVladimir Oltean const struct sja1105_l2_lookup_entry *requested) 124560f6053fSVladimir Oltean { 124660f6053fSVladimir Oltean struct sja1105_l2_lookup_entry *l2_lookup; 124760f6053fSVladimir Oltean struct sja1105_table *table; 124860f6053fSVladimir Oltean int i; 124960f6053fSVladimir Oltean 125060f6053fSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; 125160f6053fSVladimir Oltean l2_lookup = table->entries; 125260f6053fSVladimir Oltean 125360f6053fSVladimir Oltean for (i = 0; i < table->entry_count; i++) 125460f6053fSVladimir Oltean if (l2_lookup[i].macaddr == requested->macaddr && 125560f6053fSVladimir Oltean l2_lookup[i].vlanid == requested->vlanid && 125660f6053fSVladimir Oltean l2_lookup[i].destports & BIT(port)) 125760f6053fSVladimir Oltean return i; 125860f6053fSVladimir Oltean 125960f6053fSVladimir Oltean return -1; 126060f6053fSVladimir Oltean } 126160f6053fSVladimir Oltean 126260f6053fSVladimir Oltean /* We want FDB entries added statically through the bridge command to persist 126360f6053fSVladimir Oltean * across switch resets, which are a common thing during normal SJA1105 126460f6053fSVladimir Oltean * operation. So we have to back them up in the static configuration tables 126560f6053fSVladimir Oltean * and hence apply them on next static config upload... yay! 126660f6053fSVladimir Oltean */ 126760f6053fSVladimir Oltean static int 126860f6053fSVladimir Oltean sja1105_static_fdb_change(struct sja1105_private *priv, int port, 126960f6053fSVladimir Oltean const struct sja1105_l2_lookup_entry *requested, 127060f6053fSVladimir Oltean bool keep) 127160f6053fSVladimir Oltean { 127260f6053fSVladimir Oltean struct sja1105_l2_lookup_entry *l2_lookup; 127360f6053fSVladimir Oltean struct sja1105_table *table; 127460f6053fSVladimir Oltean int rc, match; 127560f6053fSVladimir Oltean 127660f6053fSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; 127760f6053fSVladimir Oltean 127860f6053fSVladimir Oltean match = sja1105_find_static_fdb_entry(priv, port, requested); 127960f6053fSVladimir Oltean if (match < 0) { 128060f6053fSVladimir Oltean /* Can't delete a missing entry. */ 128160f6053fSVladimir Oltean if (!keep) 128260f6053fSVladimir Oltean return 0; 128360f6053fSVladimir Oltean 128460f6053fSVladimir Oltean /* No match => new entry */ 128560f6053fSVladimir Oltean rc = sja1105_table_resize(table, table->entry_count + 1); 128660f6053fSVladimir Oltean if (rc) 128760f6053fSVladimir Oltean return rc; 128860f6053fSVladimir Oltean 128960f6053fSVladimir Oltean match = table->entry_count - 1; 129060f6053fSVladimir Oltean } 129160f6053fSVladimir Oltean 129260f6053fSVladimir Oltean /* Assign pointer after the resize (it may be new memory) */ 129360f6053fSVladimir Oltean l2_lookup = table->entries; 129460f6053fSVladimir Oltean 129560f6053fSVladimir Oltean /* We have a match. 129660f6053fSVladimir Oltean * If the job was to add this FDB entry, it's already done (mostly 129760f6053fSVladimir Oltean * anyway, since the port forwarding mask may have changed, case in 129860f6053fSVladimir Oltean * which we update it). 129960f6053fSVladimir Oltean * Otherwise we have to delete it. 130060f6053fSVladimir Oltean */ 130160f6053fSVladimir Oltean if (keep) { 130260f6053fSVladimir Oltean l2_lookup[match] = *requested; 130360f6053fSVladimir Oltean return 0; 130460f6053fSVladimir Oltean } 130560f6053fSVladimir Oltean 130660f6053fSVladimir Oltean /* To remove, the strategy is to overwrite the element with 130760f6053fSVladimir Oltean * the last one, and then reduce the array size by 1 130860f6053fSVladimir Oltean */ 130960f6053fSVladimir Oltean l2_lookup[match] = l2_lookup[table->entry_count - 1]; 131060f6053fSVladimir Oltean return sja1105_table_resize(table, table->entry_count - 1); 131160f6053fSVladimir Oltean } 131260f6053fSVladimir Oltean 1313291d1e72SVladimir Oltean /* First-generation switches have a 4-way set associative TCAM that 1314291d1e72SVladimir Oltean * holds the FDB entries. An FDB index spans from 0 to 1023 and is comprised of 1315291d1e72SVladimir Oltean * a "bin" (grouping of 4 entries) and a "way" (an entry within a bin). 1316291d1e72SVladimir Oltean * For the placement of a newly learnt FDB entry, the switch selects the bin 1317291d1e72SVladimir Oltean * based on a hash function, and the way within that bin incrementally. 1318291d1e72SVladimir Oltean */ 131909c1b412SVladimir Oltean static int sja1105et_fdb_index(int bin, int way) 1320291d1e72SVladimir Oltean { 1321291d1e72SVladimir Oltean return bin * SJA1105ET_FDB_BIN_SIZE + way; 1322291d1e72SVladimir Oltean } 1323291d1e72SVladimir Oltean 13249dfa6911SVladimir Oltean static int sja1105et_is_fdb_entry_in_bin(struct sja1105_private *priv, int bin, 1325291d1e72SVladimir Oltean const u8 *addr, u16 vid, 1326291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry *match, 1327291d1e72SVladimir Oltean int *last_unused) 1328291d1e72SVladimir Oltean { 1329291d1e72SVladimir Oltean int way; 1330291d1e72SVladimir Oltean 1331291d1e72SVladimir Oltean for (way = 0; way < SJA1105ET_FDB_BIN_SIZE; way++) { 1332291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 1333291d1e72SVladimir Oltean int index = sja1105et_fdb_index(bin, way); 1334291d1e72SVladimir Oltean 1335291d1e72SVladimir Oltean /* Skip unused entries, optionally marking them 1336291d1e72SVladimir Oltean * into the return value 1337291d1e72SVladimir Oltean */ 1338291d1e72SVladimir Oltean if (sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 1339291d1e72SVladimir Oltean index, &l2_lookup)) { 1340291d1e72SVladimir Oltean if (last_unused) 1341291d1e72SVladimir Oltean *last_unused = way; 1342291d1e72SVladimir Oltean continue; 1343291d1e72SVladimir Oltean } 1344291d1e72SVladimir Oltean 1345291d1e72SVladimir Oltean if (l2_lookup.macaddr == ether_addr_to_u64(addr) && 1346291d1e72SVladimir Oltean l2_lookup.vlanid == vid) { 1347291d1e72SVladimir Oltean if (match) 1348291d1e72SVladimir Oltean *match = l2_lookup; 1349291d1e72SVladimir Oltean return way; 1350291d1e72SVladimir Oltean } 1351291d1e72SVladimir Oltean } 1352291d1e72SVladimir Oltean /* Return an invalid entry index if not found */ 1353291d1e72SVladimir Oltean return -1; 1354291d1e72SVladimir Oltean } 1355291d1e72SVladimir Oltean 13569dfa6911SVladimir Oltean int sja1105et_fdb_add(struct dsa_switch *ds, int port, 1357291d1e72SVladimir Oltean const unsigned char *addr, u16 vid) 1358291d1e72SVladimir Oltean { 1359291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 1360291d1e72SVladimir Oltean struct sja1105_private *priv = ds->priv; 1361291d1e72SVladimir Oltean struct device *dev = ds->dev; 1362291d1e72SVladimir Oltean int last_unused = -1; 136360f6053fSVladimir Oltean int bin, way, rc; 1364291d1e72SVladimir Oltean 13659dfa6911SVladimir Oltean bin = sja1105et_fdb_hash(priv, addr, vid); 1366291d1e72SVladimir Oltean 13679dfa6911SVladimir Oltean way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid, 1368291d1e72SVladimir Oltean &l2_lookup, &last_unused); 1369291d1e72SVladimir Oltean if (way >= 0) { 1370291d1e72SVladimir Oltean /* We have an FDB entry. Is our port in the destination 1371291d1e72SVladimir Oltean * mask? If yes, we need to do nothing. If not, we need 1372291d1e72SVladimir Oltean * to rewrite the entry by adding this port to it. 1373291d1e72SVladimir Oltean */ 1374291d1e72SVladimir Oltean if (l2_lookup.destports & BIT(port)) 1375291d1e72SVladimir Oltean return 0; 1376291d1e72SVladimir Oltean l2_lookup.destports |= BIT(port); 1377291d1e72SVladimir Oltean } else { 1378291d1e72SVladimir Oltean int index = sja1105et_fdb_index(bin, way); 1379291d1e72SVladimir Oltean 1380291d1e72SVladimir Oltean /* We don't have an FDB entry. We construct a new one and 1381291d1e72SVladimir Oltean * try to find a place for it within the FDB table. 1382291d1e72SVladimir Oltean */ 1383291d1e72SVladimir Oltean l2_lookup.macaddr = ether_addr_to_u64(addr); 1384291d1e72SVladimir Oltean l2_lookup.destports = BIT(port); 1385291d1e72SVladimir Oltean l2_lookup.vlanid = vid; 1386291d1e72SVladimir Oltean 1387291d1e72SVladimir Oltean if (last_unused >= 0) { 1388291d1e72SVladimir Oltean way = last_unused; 1389291d1e72SVladimir Oltean } else { 1390291d1e72SVladimir Oltean /* Bin is full, need to evict somebody. 1391291d1e72SVladimir Oltean * Choose victim at random. If you get these messages 1392291d1e72SVladimir Oltean * often, you may need to consider changing the 1393291d1e72SVladimir Oltean * distribution function: 1394291d1e72SVladimir Oltean * static_config[BLK_IDX_L2_LOOKUP_PARAMS].entries->poly 1395291d1e72SVladimir Oltean */ 1396291d1e72SVladimir Oltean get_random_bytes(&way, sizeof(u8)); 1397291d1e72SVladimir Oltean way %= SJA1105ET_FDB_BIN_SIZE; 1398291d1e72SVladimir Oltean dev_warn(dev, "Warning, FDB bin %d full while adding entry for %pM. Evicting entry %u.\n", 1399291d1e72SVladimir Oltean bin, addr, way); 1400291d1e72SVladimir Oltean /* Evict entry */ 1401291d1e72SVladimir Oltean sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 1402291d1e72SVladimir Oltean index, NULL, false); 1403291d1e72SVladimir Oltean } 1404291d1e72SVladimir Oltean } 1405291d1e72SVladimir Oltean l2_lookup.index = sja1105et_fdb_index(bin, way); 1406291d1e72SVladimir Oltean 140760f6053fSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 1408291d1e72SVladimir Oltean l2_lookup.index, &l2_lookup, 1409291d1e72SVladimir Oltean true); 141060f6053fSVladimir Oltean if (rc < 0) 141160f6053fSVladimir Oltean return rc; 141260f6053fSVladimir Oltean 141360f6053fSVladimir Oltean return sja1105_static_fdb_change(priv, port, &l2_lookup, true); 1414291d1e72SVladimir Oltean } 1415291d1e72SVladimir Oltean 14169dfa6911SVladimir Oltean int sja1105et_fdb_del(struct dsa_switch *ds, int port, 1417291d1e72SVladimir Oltean const unsigned char *addr, u16 vid) 1418291d1e72SVladimir Oltean { 1419291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 1420291d1e72SVladimir Oltean struct sja1105_private *priv = ds->priv; 142160f6053fSVladimir Oltean int index, bin, way, rc; 1422291d1e72SVladimir Oltean bool keep; 1423291d1e72SVladimir Oltean 14249dfa6911SVladimir Oltean bin = sja1105et_fdb_hash(priv, addr, vid); 14259dfa6911SVladimir Oltean way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid, 1426291d1e72SVladimir Oltean &l2_lookup, NULL); 1427291d1e72SVladimir Oltean if (way < 0) 1428291d1e72SVladimir Oltean return 0; 1429291d1e72SVladimir Oltean index = sja1105et_fdb_index(bin, way); 1430291d1e72SVladimir Oltean 1431291d1e72SVladimir Oltean /* We have an FDB entry. Is our port in the destination mask? If yes, 1432291d1e72SVladimir Oltean * we need to remove it. If the resulting port mask becomes empty, we 1433291d1e72SVladimir Oltean * need to completely evict the FDB entry. 1434291d1e72SVladimir Oltean * Otherwise we just write it back. 1435291d1e72SVladimir Oltean */ 1436291d1e72SVladimir Oltean l2_lookup.destports &= ~BIT(port); 14377752e937SVladimir Oltean 1438291d1e72SVladimir Oltean if (l2_lookup.destports) 1439291d1e72SVladimir Oltean keep = true; 1440291d1e72SVladimir Oltean else 1441291d1e72SVladimir Oltean keep = false; 1442291d1e72SVladimir Oltean 144360f6053fSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 1444291d1e72SVladimir Oltean index, &l2_lookup, keep); 144560f6053fSVladimir Oltean if (rc < 0) 144660f6053fSVladimir Oltean return rc; 144760f6053fSVladimir Oltean 144860f6053fSVladimir Oltean return sja1105_static_fdb_change(priv, port, &l2_lookup, keep); 1449291d1e72SVladimir Oltean } 1450291d1e72SVladimir Oltean 14519dfa6911SVladimir Oltean int sja1105pqrs_fdb_add(struct dsa_switch *ds, int port, 14529dfa6911SVladimir Oltean const unsigned char *addr, u16 vid) 14539dfa6911SVladimir Oltean { 14541da73821SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 14551da73821SVladimir Oltean struct sja1105_private *priv = ds->priv; 14561da73821SVladimir Oltean int rc, i; 14571da73821SVladimir Oltean 14581da73821SVladimir Oltean /* Search for an existing entry in the FDB table */ 14591da73821SVladimir Oltean l2_lookup.macaddr = ether_addr_to_u64(addr); 14601da73821SVladimir Oltean l2_lookup.vlanid = vid; 14611da73821SVladimir Oltean l2_lookup.iotag = SJA1105_S_TAG; 14621da73821SVladimir Oltean l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0); 14637f14937fSVladimir Oltean if (priv->vlan_state != SJA1105_VLAN_UNAWARE) { 14641da73821SVladimir Oltean l2_lookup.mask_vlanid = VLAN_VID_MASK; 14651da73821SVladimir Oltean l2_lookup.mask_iotag = BIT(0); 14666d7c7d94SVladimir Oltean } else { 14676d7c7d94SVladimir Oltean l2_lookup.mask_vlanid = 0; 14686d7c7d94SVladimir Oltean l2_lookup.mask_iotag = 0; 14696d7c7d94SVladimir Oltean } 14701da73821SVladimir Oltean l2_lookup.destports = BIT(port); 14711da73821SVladimir Oltean 14721da73821SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 14731da73821SVladimir Oltean SJA1105_SEARCH, &l2_lookup); 14741da73821SVladimir Oltean if (rc == 0) { 14751da73821SVladimir Oltean /* Found and this port is already in the entry's 14761da73821SVladimir Oltean * port mask => job done 14771da73821SVladimir Oltean */ 14781da73821SVladimir Oltean if (l2_lookup.destports & BIT(port)) 14791da73821SVladimir Oltean return 0; 14801da73821SVladimir Oltean /* l2_lookup.index is populated by the switch in case it 14811da73821SVladimir Oltean * found something. 14821da73821SVladimir Oltean */ 14831da73821SVladimir Oltean l2_lookup.destports |= BIT(port); 14841da73821SVladimir Oltean goto skip_finding_an_index; 14851da73821SVladimir Oltean } 14861da73821SVladimir Oltean 14871da73821SVladimir Oltean /* Not found, so try to find an unused spot in the FDB. 14881da73821SVladimir Oltean * This is slightly inefficient because the strategy is knock-knock at 14891da73821SVladimir Oltean * every possible position from 0 to 1023. 14901da73821SVladimir Oltean */ 14911da73821SVladimir Oltean for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) { 14921da73821SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 14931da73821SVladimir Oltean i, NULL); 14941da73821SVladimir Oltean if (rc < 0) 14951da73821SVladimir Oltean break; 14961da73821SVladimir Oltean } 14971da73821SVladimir Oltean if (i == SJA1105_MAX_L2_LOOKUP_COUNT) { 14981da73821SVladimir Oltean dev_err(ds->dev, "FDB is full, cannot add entry.\n"); 14991da73821SVladimir Oltean return -EINVAL; 15001da73821SVladimir Oltean } 150117ae6555SVladimir Oltean l2_lookup.lockeds = true; 15021da73821SVladimir Oltean l2_lookup.index = i; 15031da73821SVladimir Oltean 15041da73821SVladimir Oltean skip_finding_an_index: 150560f6053fSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 15061da73821SVladimir Oltean l2_lookup.index, &l2_lookup, 15071da73821SVladimir Oltean true); 150860f6053fSVladimir Oltean if (rc < 0) 150960f6053fSVladimir Oltean return rc; 151060f6053fSVladimir Oltean 151160f6053fSVladimir Oltean return sja1105_static_fdb_change(priv, port, &l2_lookup, true); 15129dfa6911SVladimir Oltean } 15139dfa6911SVladimir Oltean 15149dfa6911SVladimir Oltean int sja1105pqrs_fdb_del(struct dsa_switch *ds, int port, 15159dfa6911SVladimir Oltean const unsigned char *addr, u16 vid) 15169dfa6911SVladimir Oltean { 15171da73821SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 15181da73821SVladimir Oltean struct sja1105_private *priv = ds->priv; 15191da73821SVladimir Oltean bool keep; 15201da73821SVladimir Oltean int rc; 15211da73821SVladimir Oltean 15221da73821SVladimir Oltean l2_lookup.macaddr = ether_addr_to_u64(addr); 15231da73821SVladimir Oltean l2_lookup.vlanid = vid; 15241da73821SVladimir Oltean l2_lookup.iotag = SJA1105_S_TAG; 15251da73821SVladimir Oltean l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0); 15267f14937fSVladimir Oltean if (priv->vlan_state != SJA1105_VLAN_UNAWARE) { 15271da73821SVladimir Oltean l2_lookup.mask_vlanid = VLAN_VID_MASK; 15281da73821SVladimir Oltean l2_lookup.mask_iotag = BIT(0); 15296d7c7d94SVladimir Oltean } else { 15306d7c7d94SVladimir Oltean l2_lookup.mask_vlanid = 0; 15316d7c7d94SVladimir Oltean l2_lookup.mask_iotag = 0; 15326d7c7d94SVladimir Oltean } 15331da73821SVladimir Oltean l2_lookup.destports = BIT(port); 15341da73821SVladimir Oltean 15351da73821SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 15361da73821SVladimir Oltean SJA1105_SEARCH, &l2_lookup); 15371da73821SVladimir Oltean if (rc < 0) 15381da73821SVladimir Oltean return 0; 15391da73821SVladimir Oltean 15401da73821SVladimir Oltean l2_lookup.destports &= ~BIT(port); 15411da73821SVladimir Oltean 15421da73821SVladimir Oltean /* Decide whether we remove just this port from the FDB entry, 15431da73821SVladimir Oltean * or if we remove it completely. 15441da73821SVladimir Oltean */ 15451da73821SVladimir Oltean if (l2_lookup.destports) 15461da73821SVladimir Oltean keep = true; 15471da73821SVladimir Oltean else 15481da73821SVladimir Oltean keep = false; 15491da73821SVladimir Oltean 155060f6053fSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 15511da73821SVladimir Oltean l2_lookup.index, &l2_lookup, keep); 155260f6053fSVladimir Oltean if (rc < 0) 155360f6053fSVladimir Oltean return rc; 155460f6053fSVladimir Oltean 155560f6053fSVladimir Oltean return sja1105_static_fdb_change(priv, port, &l2_lookup, keep); 15569dfa6911SVladimir Oltean } 15579dfa6911SVladimir Oltean 15589dfa6911SVladimir Oltean static int sja1105_fdb_add(struct dsa_switch *ds, int port, 15599dfa6911SVladimir Oltean const unsigned char *addr, u16 vid) 15609dfa6911SVladimir Oltean { 15619dfa6911SVladimir Oltean struct sja1105_private *priv = ds->priv; 1562b3ee526aSVladimir Oltean 15636d7c7d94SVladimir Oltean /* dsa_8021q is in effect when the bridge's vlan_filtering isn't, 15646d7c7d94SVladimir Oltean * so the switch still does some VLAN processing internally. 15656d7c7d94SVladimir Oltean * But Shared VLAN Learning (SVL) is also active, and it will take 15666d7c7d94SVladimir Oltean * care of autonomous forwarding between the unique pvid's of each 15676d7c7d94SVladimir Oltean * port. Here we just make sure that users can't add duplicate FDB 15686d7c7d94SVladimir Oltean * entries when in this mode - the actual VID doesn't matter except 15696d7c7d94SVladimir Oltean * for what gets printed in 'bridge fdb show'. In the case of zero, 15706d7c7d94SVladimir Oltean * no VID gets printed at all. 157193647594SVladimir Oltean */ 15727f14937fSVladimir Oltean if (priv->vlan_state != SJA1105_VLAN_FILTERING_FULL) 15736d7c7d94SVladimir Oltean vid = 0; 157493647594SVladimir Oltean 15756d7c7d94SVladimir Oltean return priv->info->fdb_add_cmd(ds, port, addr, vid); 15769dfa6911SVladimir Oltean } 15779dfa6911SVladimir Oltean 15789dfa6911SVladimir Oltean static int sja1105_fdb_del(struct dsa_switch *ds, int port, 15799dfa6911SVladimir Oltean const unsigned char *addr, u16 vid) 15809dfa6911SVladimir Oltean { 15819dfa6911SVladimir Oltean struct sja1105_private *priv = ds->priv; 15829dfa6911SVladimir Oltean 15837f14937fSVladimir Oltean if (priv->vlan_state != SJA1105_VLAN_FILTERING_FULL) 15846d7c7d94SVladimir Oltean vid = 0; 15856d7c7d94SVladimir Oltean 1586b3ee526aSVladimir Oltean return priv->info->fdb_del_cmd(ds, port, addr, vid); 15879dfa6911SVladimir Oltean } 15889dfa6911SVladimir Oltean 1589291d1e72SVladimir Oltean static int sja1105_fdb_dump(struct dsa_switch *ds, int port, 1590291d1e72SVladimir Oltean dsa_fdb_dump_cb_t *cb, void *data) 1591291d1e72SVladimir Oltean { 1592291d1e72SVladimir Oltean struct sja1105_private *priv = ds->priv; 1593291d1e72SVladimir Oltean struct device *dev = ds->dev; 1594291d1e72SVladimir Oltean int i; 1595291d1e72SVladimir Oltean 1596291d1e72SVladimir Oltean for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) { 1597291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 1598291d1e72SVladimir Oltean u8 macaddr[ETH_ALEN]; 1599291d1e72SVladimir Oltean int rc; 1600291d1e72SVladimir Oltean 1601291d1e72SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 1602291d1e72SVladimir Oltean i, &l2_lookup); 1603291d1e72SVladimir Oltean /* No fdb entry at i, not an issue */ 1604def84604SVladimir Oltean if (rc == -ENOENT) 1605291d1e72SVladimir Oltean continue; 1606291d1e72SVladimir Oltean if (rc) { 1607291d1e72SVladimir Oltean dev_err(dev, "Failed to dump FDB: %d\n", rc); 1608291d1e72SVladimir Oltean return rc; 1609291d1e72SVladimir Oltean } 1610291d1e72SVladimir Oltean 1611291d1e72SVladimir Oltean /* FDB dump callback is per port. This means we have to 1612291d1e72SVladimir Oltean * disregard a valid entry if it's not for this port, even if 1613291d1e72SVladimir Oltean * only to revisit it later. This is inefficient because the 1614291d1e72SVladimir Oltean * 1024-sized FDB table needs to be traversed 4 times through 1615291d1e72SVladimir Oltean * SPI during a 'bridge fdb show' command. 1616291d1e72SVladimir Oltean */ 1617291d1e72SVladimir Oltean if (!(l2_lookup.destports & BIT(port))) 1618291d1e72SVladimir Oltean continue; 16194d942354SVladimir Oltean 16204d942354SVladimir Oltean /* We need to hide the FDB entry for unknown multicast */ 16214d942354SVladimir Oltean if (l2_lookup.macaddr == SJA1105_UNKNOWN_MULTICAST && 16224d942354SVladimir Oltean l2_lookup.mask_macaddr == SJA1105_UNKNOWN_MULTICAST) 16234d942354SVladimir Oltean continue; 16244d942354SVladimir Oltean 1625291d1e72SVladimir Oltean u64_to_ether_addr(l2_lookup.macaddr, macaddr); 162693647594SVladimir Oltean 16276d7c7d94SVladimir Oltean /* We need to hide the dsa_8021q VLANs from the user. */ 16287f14937fSVladimir Oltean if (priv->vlan_state == SJA1105_VLAN_UNAWARE) 16296d7c7d94SVladimir Oltean l2_lookup.vlanid = 0; 163017ae6555SVladimir Oltean cb(macaddr, l2_lookup.vlanid, l2_lookup.lockeds, data); 1631291d1e72SVladimir Oltean } 1632291d1e72SVladimir Oltean return 0; 1633291d1e72SVladimir Oltean } 1634291d1e72SVladimir Oltean 1635a52b2da7SVladimir Oltean static int sja1105_mdb_add(struct dsa_switch *ds, int port, 1636291d1e72SVladimir Oltean const struct switchdev_obj_port_mdb *mdb) 1637291d1e72SVladimir Oltean { 1638a52b2da7SVladimir Oltean return sja1105_fdb_add(ds, port, mdb->addr, mdb->vid); 1639291d1e72SVladimir Oltean } 1640291d1e72SVladimir Oltean 1641291d1e72SVladimir Oltean static int sja1105_mdb_del(struct dsa_switch *ds, int port, 1642291d1e72SVladimir Oltean const struct switchdev_obj_port_mdb *mdb) 1643291d1e72SVladimir Oltean { 1644291d1e72SVladimir Oltean return sja1105_fdb_del(ds, port, mdb->addr, mdb->vid); 1645291d1e72SVladimir Oltean } 1646291d1e72SVladimir Oltean 16477f7ccdeaSVladimir Oltean /* Common function for unicast and broadcast flood configuration. 16487f7ccdeaSVladimir Oltean * Flooding is configured between each {ingress, egress} port pair, and since 16497f7ccdeaSVladimir Oltean * the bridge's semantics are those of "egress flooding", it means we must 16507f7ccdeaSVladimir Oltean * enable flooding towards this port from all ingress ports that are in the 16517f7ccdeaSVladimir Oltean * same forwarding domain. 16527f7ccdeaSVladimir Oltean */ 16537f7ccdeaSVladimir Oltean static int sja1105_manage_flood_domains(struct sja1105_private *priv) 16547f7ccdeaSVladimir Oltean { 16557f7ccdeaSVladimir Oltean struct sja1105_l2_forwarding_entry *l2_fwd; 16567f7ccdeaSVladimir Oltean struct dsa_switch *ds = priv->ds; 16577f7ccdeaSVladimir Oltean int from, to, rc; 16587f7ccdeaSVladimir Oltean 16597f7ccdeaSVladimir Oltean l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries; 16607f7ccdeaSVladimir Oltean 16617f7ccdeaSVladimir Oltean for (from = 0; from < ds->num_ports; from++) { 16627f7ccdeaSVladimir Oltean u64 fl_domain = 0, bc_domain = 0; 16637f7ccdeaSVladimir Oltean 16647f7ccdeaSVladimir Oltean for (to = 0; to < priv->ds->num_ports; to++) { 16657f7ccdeaSVladimir Oltean if (!sja1105_can_forward(l2_fwd, from, to)) 16667f7ccdeaSVladimir Oltean continue; 16677f7ccdeaSVladimir Oltean 16687f7ccdeaSVladimir Oltean if (priv->ucast_egress_floods & BIT(to)) 16697f7ccdeaSVladimir Oltean fl_domain |= BIT(to); 16707f7ccdeaSVladimir Oltean if (priv->bcast_egress_floods & BIT(to)) 16717f7ccdeaSVladimir Oltean bc_domain |= BIT(to); 16727f7ccdeaSVladimir Oltean } 16737f7ccdeaSVladimir Oltean 16747f7ccdeaSVladimir Oltean /* Nothing changed, nothing to do */ 16757f7ccdeaSVladimir Oltean if (l2_fwd[from].fl_domain == fl_domain && 16767f7ccdeaSVladimir Oltean l2_fwd[from].bc_domain == bc_domain) 16777f7ccdeaSVladimir Oltean continue; 16787f7ccdeaSVladimir Oltean 16797f7ccdeaSVladimir Oltean l2_fwd[from].fl_domain = fl_domain; 16807f7ccdeaSVladimir Oltean l2_fwd[from].bc_domain = bc_domain; 16817f7ccdeaSVladimir Oltean 16827f7ccdeaSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING, 16837f7ccdeaSVladimir Oltean from, &l2_fwd[from], true); 16847f7ccdeaSVladimir Oltean if (rc < 0) 16857f7ccdeaSVladimir Oltean return rc; 16867f7ccdeaSVladimir Oltean } 16877f7ccdeaSVladimir Oltean 16887f7ccdeaSVladimir Oltean return 0; 16897f7ccdeaSVladimir Oltean } 16907f7ccdeaSVladimir Oltean 16918aa9ebccSVladimir Oltean static int sja1105_bridge_member(struct dsa_switch *ds, int port, 16928aa9ebccSVladimir Oltean struct net_device *br, bool member) 16938aa9ebccSVladimir Oltean { 16948aa9ebccSVladimir Oltean struct sja1105_l2_forwarding_entry *l2_fwd; 16958aa9ebccSVladimir Oltean struct sja1105_private *priv = ds->priv; 16968aa9ebccSVladimir Oltean int i, rc; 16978aa9ebccSVladimir Oltean 16988aa9ebccSVladimir Oltean l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries; 16998aa9ebccSVladimir Oltean 1700542043e9SVladimir Oltean for (i = 0; i < ds->num_ports; i++) { 17018aa9ebccSVladimir Oltean /* Add this port to the forwarding matrix of the 17028aa9ebccSVladimir Oltean * other ports in the same bridge, and viceversa. 17038aa9ebccSVladimir Oltean */ 17048aa9ebccSVladimir Oltean if (!dsa_is_user_port(ds, i)) 17058aa9ebccSVladimir Oltean continue; 17068aa9ebccSVladimir Oltean /* For the ports already under the bridge, only one thing needs 17078aa9ebccSVladimir Oltean * to be done, and that is to add this port to their 17088aa9ebccSVladimir Oltean * reachability domain. So we can perform the SPI write for 17098aa9ebccSVladimir Oltean * them immediately. However, for this port itself (the one 17108aa9ebccSVladimir Oltean * that is new to the bridge), we need to add all other ports 17118aa9ebccSVladimir Oltean * to its reachability domain. So we do that incrementally in 17128aa9ebccSVladimir Oltean * this loop, and perform the SPI write only at the end, once 17138aa9ebccSVladimir Oltean * the domain contains all other bridge ports. 17148aa9ebccSVladimir Oltean */ 17158aa9ebccSVladimir Oltean if (i == port) 17168aa9ebccSVladimir Oltean continue; 17178aa9ebccSVladimir Oltean if (dsa_to_port(ds, i)->bridge_dev != br) 17188aa9ebccSVladimir Oltean continue; 17198aa9ebccSVladimir Oltean sja1105_port_allow_traffic(l2_fwd, i, port, member); 17208aa9ebccSVladimir Oltean sja1105_port_allow_traffic(l2_fwd, port, i, member); 17218aa9ebccSVladimir Oltean 17228aa9ebccSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING, 17238aa9ebccSVladimir Oltean i, &l2_fwd[i], true); 17248aa9ebccSVladimir Oltean if (rc < 0) 17258aa9ebccSVladimir Oltean return rc; 17268aa9ebccSVladimir Oltean } 17278aa9ebccSVladimir Oltean 17287f7ccdeaSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING, 17298aa9ebccSVladimir Oltean port, &l2_fwd[port], true); 17307f7ccdeaSVladimir Oltean if (rc) 17317f7ccdeaSVladimir Oltean return rc; 17327f7ccdeaSVladimir Oltean 17337f7ccdeaSVladimir Oltean return sja1105_manage_flood_domains(priv); 17348aa9ebccSVladimir Oltean } 17358aa9ebccSVladimir Oltean 1736640f763fSVladimir Oltean static void sja1105_bridge_stp_state_set(struct dsa_switch *ds, int port, 1737640f763fSVladimir Oltean u8 state) 1738640f763fSVladimir Oltean { 1739640f763fSVladimir Oltean struct sja1105_private *priv = ds->priv; 1740640f763fSVladimir Oltean struct sja1105_mac_config_entry *mac; 1741640f763fSVladimir Oltean 1742640f763fSVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 1743640f763fSVladimir Oltean 1744640f763fSVladimir Oltean switch (state) { 1745640f763fSVladimir Oltean case BR_STATE_DISABLED: 1746640f763fSVladimir Oltean case BR_STATE_BLOCKING: 1747640f763fSVladimir Oltean /* From UM10944 description of DRPDTAG (why put this there?): 1748640f763fSVladimir Oltean * "Management traffic flows to the port regardless of the state 1749640f763fSVladimir Oltean * of the INGRESS flag". So BPDUs are still be allowed to pass. 1750640f763fSVladimir Oltean * At the moment no difference between DISABLED and BLOCKING. 1751640f763fSVladimir Oltean */ 1752640f763fSVladimir Oltean mac[port].ingress = false; 1753640f763fSVladimir Oltean mac[port].egress = false; 1754640f763fSVladimir Oltean mac[port].dyn_learn = false; 1755640f763fSVladimir Oltean break; 1756640f763fSVladimir Oltean case BR_STATE_LISTENING: 1757640f763fSVladimir Oltean mac[port].ingress = true; 1758640f763fSVladimir Oltean mac[port].egress = false; 1759640f763fSVladimir Oltean mac[port].dyn_learn = false; 1760640f763fSVladimir Oltean break; 1761640f763fSVladimir Oltean case BR_STATE_LEARNING: 1762640f763fSVladimir Oltean mac[port].ingress = true; 1763640f763fSVladimir Oltean mac[port].egress = false; 17644d942354SVladimir Oltean mac[port].dyn_learn = !!(priv->learn_ena & BIT(port)); 1765640f763fSVladimir Oltean break; 1766640f763fSVladimir Oltean case BR_STATE_FORWARDING: 1767640f763fSVladimir Oltean mac[port].ingress = true; 1768640f763fSVladimir Oltean mac[port].egress = true; 17694d942354SVladimir Oltean mac[port].dyn_learn = !!(priv->learn_ena & BIT(port)); 1770640f763fSVladimir Oltean break; 1771640f763fSVladimir Oltean default: 1772640f763fSVladimir Oltean dev_err(ds->dev, "invalid STP state: %d\n", state); 1773640f763fSVladimir Oltean return; 1774640f763fSVladimir Oltean } 1775640f763fSVladimir Oltean 1776640f763fSVladimir Oltean sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, 1777640f763fSVladimir Oltean &mac[port], true); 1778640f763fSVladimir Oltean } 1779640f763fSVladimir Oltean 17808aa9ebccSVladimir Oltean static int sja1105_bridge_join(struct dsa_switch *ds, int port, 17818aa9ebccSVladimir Oltean struct net_device *br) 17828aa9ebccSVladimir Oltean { 17838aa9ebccSVladimir Oltean return sja1105_bridge_member(ds, port, br, true); 17848aa9ebccSVladimir Oltean } 17858aa9ebccSVladimir Oltean 17868aa9ebccSVladimir Oltean static void sja1105_bridge_leave(struct dsa_switch *ds, int port, 17878aa9ebccSVladimir Oltean struct net_device *br) 17888aa9ebccSVladimir Oltean { 17898aa9ebccSVladimir Oltean sja1105_bridge_member(ds, port, br, false); 17908aa9ebccSVladimir Oltean } 17918aa9ebccSVladimir Oltean 17924d752508SVladimir Oltean #define BYTES_PER_KBIT (1000LL / 8) 17934d752508SVladimir Oltean 17944d752508SVladimir Oltean static int sja1105_find_unused_cbs_shaper(struct sja1105_private *priv) 17954d752508SVladimir Oltean { 17964d752508SVladimir Oltean int i; 17974d752508SVladimir Oltean 17984d752508SVladimir Oltean for (i = 0; i < priv->info->num_cbs_shapers; i++) 17994d752508SVladimir Oltean if (!priv->cbs[i].idle_slope && !priv->cbs[i].send_slope) 18004d752508SVladimir Oltean return i; 18014d752508SVladimir Oltean 18024d752508SVladimir Oltean return -1; 18034d752508SVladimir Oltean } 18044d752508SVladimir Oltean 18054d752508SVladimir Oltean static int sja1105_delete_cbs_shaper(struct sja1105_private *priv, int port, 18064d752508SVladimir Oltean int prio) 18074d752508SVladimir Oltean { 18084d752508SVladimir Oltean int i; 18094d752508SVladimir Oltean 18104d752508SVladimir Oltean for (i = 0; i < priv->info->num_cbs_shapers; i++) { 18114d752508SVladimir Oltean struct sja1105_cbs_entry *cbs = &priv->cbs[i]; 18124d752508SVladimir Oltean 18134d752508SVladimir Oltean if (cbs->port == port && cbs->prio == prio) { 18144d752508SVladimir Oltean memset(cbs, 0, sizeof(*cbs)); 18154d752508SVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_CBS, 18164d752508SVladimir Oltean i, cbs, true); 18174d752508SVladimir Oltean } 18184d752508SVladimir Oltean } 18194d752508SVladimir Oltean 18204d752508SVladimir Oltean return 0; 18214d752508SVladimir Oltean } 18224d752508SVladimir Oltean 18234d752508SVladimir Oltean static int sja1105_setup_tc_cbs(struct dsa_switch *ds, int port, 18244d752508SVladimir Oltean struct tc_cbs_qopt_offload *offload) 18254d752508SVladimir Oltean { 18264d752508SVladimir Oltean struct sja1105_private *priv = ds->priv; 18274d752508SVladimir Oltean struct sja1105_cbs_entry *cbs; 18284d752508SVladimir Oltean int index; 18294d752508SVladimir Oltean 18304d752508SVladimir Oltean if (!offload->enable) 18314d752508SVladimir Oltean return sja1105_delete_cbs_shaper(priv, port, offload->queue); 18324d752508SVladimir Oltean 18334d752508SVladimir Oltean index = sja1105_find_unused_cbs_shaper(priv); 18344d752508SVladimir Oltean if (index < 0) 18354d752508SVladimir Oltean return -ENOSPC; 18364d752508SVladimir Oltean 18374d752508SVladimir Oltean cbs = &priv->cbs[index]; 18384d752508SVladimir Oltean cbs->port = port; 18394d752508SVladimir Oltean cbs->prio = offload->queue; 18404d752508SVladimir Oltean /* locredit and sendslope are negative by definition. In hardware, 18414d752508SVladimir Oltean * positive values must be provided, and the negative sign is implicit. 18424d752508SVladimir Oltean */ 18434d752508SVladimir Oltean cbs->credit_hi = offload->hicredit; 18444d752508SVladimir Oltean cbs->credit_lo = abs(offload->locredit); 18454d752508SVladimir Oltean /* User space is in kbits/sec, hardware in bytes/sec */ 18464d752508SVladimir Oltean cbs->idle_slope = offload->idleslope * BYTES_PER_KBIT; 18474d752508SVladimir Oltean cbs->send_slope = abs(offload->sendslope * BYTES_PER_KBIT); 18484d752508SVladimir Oltean /* Convert the negative values from 64-bit 2's complement 18494d752508SVladimir Oltean * to 32-bit 2's complement (for the case of 0x80000000 whose 18504d752508SVladimir Oltean * negative is still negative). 18514d752508SVladimir Oltean */ 18524d752508SVladimir Oltean cbs->credit_lo &= GENMASK_ULL(31, 0); 18534d752508SVladimir Oltean cbs->send_slope &= GENMASK_ULL(31, 0); 18544d752508SVladimir Oltean 18554d752508SVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_CBS, index, cbs, 18564d752508SVladimir Oltean true); 18574d752508SVladimir Oltean } 18584d752508SVladimir Oltean 18594d752508SVladimir Oltean static int sja1105_reload_cbs(struct sja1105_private *priv) 18604d752508SVladimir Oltean { 18614d752508SVladimir Oltean int rc = 0, i; 18624d752508SVladimir Oltean 18634d752508SVladimir Oltean for (i = 0; i < priv->info->num_cbs_shapers; i++) { 18644d752508SVladimir Oltean struct sja1105_cbs_entry *cbs = &priv->cbs[i]; 18654d752508SVladimir Oltean 18664d752508SVladimir Oltean if (!cbs->idle_slope && !cbs->send_slope) 18674d752508SVladimir Oltean continue; 18684d752508SVladimir Oltean 18694d752508SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_CBS, i, cbs, 18704d752508SVladimir Oltean true); 18714d752508SVladimir Oltean if (rc) 18724d752508SVladimir Oltean break; 18734d752508SVladimir Oltean } 18744d752508SVladimir Oltean 18754d752508SVladimir Oltean return rc; 18764d752508SVladimir Oltean } 18774d752508SVladimir Oltean 18782eea1fa8SVladimir Oltean static const char * const sja1105_reset_reasons[] = { 18792eea1fa8SVladimir Oltean [SJA1105_VLAN_FILTERING] = "VLAN filtering", 18802eea1fa8SVladimir Oltean [SJA1105_RX_HWTSTAMPING] = "RX timestamping", 18812eea1fa8SVladimir Oltean [SJA1105_AGEING_TIME] = "Ageing time", 18822eea1fa8SVladimir Oltean [SJA1105_SCHEDULING] = "Time-aware scheduling", 1883c279c726SVladimir Oltean [SJA1105_BEST_EFFORT_POLICING] = "Best-effort policing", 1884dfacc5a2SVladimir Oltean [SJA1105_VIRTUAL_LINKS] = "Virtual links", 18852eea1fa8SVladimir Oltean }; 18862eea1fa8SVladimir Oltean 18876666cebcSVladimir Oltean /* For situations where we need to change a setting at runtime that is only 18886666cebcSVladimir Oltean * available through the static configuration, resetting the switch in order 18896666cebcSVladimir Oltean * to upload the new static config is unavoidable. Back up the settings we 18906666cebcSVladimir Oltean * modify at runtime (currently only MAC) and restore them after uploading, 18916666cebcSVladimir Oltean * such that this operation is relatively seamless. 18926666cebcSVladimir Oltean */ 18932eea1fa8SVladimir Oltean int sja1105_static_config_reload(struct sja1105_private *priv, 18942eea1fa8SVladimir Oltean enum sja1105_reset_reason reason) 18956666cebcSVladimir Oltean { 18966cf99c13SVladimir Oltean struct ptp_system_timestamp ptp_sts_before; 18976cf99c13SVladimir Oltean struct ptp_system_timestamp ptp_sts_after; 189882760d7fSVladimir Oltean int speed_mbps[SJA1105_MAX_NUM_PORTS]; 189984db00f2SVladimir Oltean u16 bmcr[SJA1105_MAX_NUM_PORTS] = {0}; 19006666cebcSVladimir Oltean struct sja1105_mac_config_entry *mac; 19016cf99c13SVladimir Oltean struct dsa_switch *ds = priv->ds; 19026cf99c13SVladimir Oltean s64 t1, t2, t3, t4; 19036cf99c13SVladimir Oltean s64 t12, t34; 19046666cebcSVladimir Oltean int rc, i; 19056cf99c13SVladimir Oltean s64 now; 19066666cebcSVladimir Oltean 1907af580ae2SVladimir Oltean mutex_lock(&priv->mgmt_lock); 1908af580ae2SVladimir Oltean 19096666cebcSVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 19106666cebcSVladimir Oltean 19118400cff6SVladimir Oltean /* Back up the dynamic link speed changed by sja1105_adjust_port_config 19128400cff6SVladimir Oltean * in order to temporarily restore it to SJA1105_SPEED_AUTO - which the 19138400cff6SVladimir Oltean * switch wants to see in the static config in order to allow us to 19148400cff6SVladimir Oltean * change it through the dynamic interface later. 19156666cebcSVladimir Oltean */ 1916542043e9SVladimir Oltean for (i = 0; i < ds->num_ports; i++) { 191741fed17fSVladimir Oltean speed_mbps[i] = sja1105_port_speed_to_ethtool(priv, 191841fed17fSVladimir Oltean mac[i].speed); 191941fed17fSVladimir Oltean mac[i].speed = priv->info->port_speed[SJA1105_SPEED_AUTO]; 19206666cebcSVladimir Oltean 192191a05078SVladimir Oltean if (priv->phy_mode[i] == PHY_INTERFACE_MODE_SGMII) 19224c7ee010SVladimir Oltean bmcr[i] = sja1105_sgmii_read(priv, i, 19234c7ee010SVladimir Oltean MDIO_MMD_VEND2, 19244c7ee010SVladimir Oltean MDIO_CTRL1); 192584db00f2SVladimir Oltean } 1926ffe10e67SVladimir Oltean 19276cf99c13SVladimir Oltean /* No PTP operations can run right now */ 19286cf99c13SVladimir Oltean mutex_lock(&priv->ptp_data.lock); 19296cf99c13SVladimir Oltean 19306cf99c13SVladimir Oltean rc = __sja1105_ptp_gettimex(ds, &now, &ptp_sts_before); 19316cf99c13SVladimir Oltean if (rc < 0) 19326cf99c13SVladimir Oltean goto out_unlock_ptp; 19336cf99c13SVladimir Oltean 19346666cebcSVladimir Oltean /* Reset switch and send updated static configuration */ 19356666cebcSVladimir Oltean rc = sja1105_static_config_upload(priv); 19366666cebcSVladimir Oltean if (rc < 0) 19376cf99c13SVladimir Oltean goto out_unlock_ptp; 19386cf99c13SVladimir Oltean 19396cf99c13SVladimir Oltean rc = __sja1105_ptp_settime(ds, 0, &ptp_sts_after); 19406cf99c13SVladimir Oltean if (rc < 0) 19416cf99c13SVladimir Oltean goto out_unlock_ptp; 19426cf99c13SVladimir Oltean 19436cf99c13SVladimir Oltean t1 = timespec64_to_ns(&ptp_sts_before.pre_ts); 19446cf99c13SVladimir Oltean t2 = timespec64_to_ns(&ptp_sts_before.post_ts); 19456cf99c13SVladimir Oltean t3 = timespec64_to_ns(&ptp_sts_after.pre_ts); 19466cf99c13SVladimir Oltean t4 = timespec64_to_ns(&ptp_sts_after.post_ts); 19476cf99c13SVladimir Oltean /* Mid point, corresponds to pre-reset PTPCLKVAL */ 19486cf99c13SVladimir Oltean t12 = t1 + (t2 - t1) / 2; 19496cf99c13SVladimir Oltean /* Mid point, corresponds to post-reset PTPCLKVAL, aka 0 */ 19506cf99c13SVladimir Oltean t34 = t3 + (t4 - t3) / 2; 19516cf99c13SVladimir Oltean /* Advance PTPCLKVAL by the time it took since its readout */ 19526cf99c13SVladimir Oltean now += (t34 - t12); 19536cf99c13SVladimir Oltean 19546cf99c13SVladimir Oltean __sja1105_ptp_adjtime(ds, now); 19556cf99c13SVladimir Oltean 19566cf99c13SVladimir Oltean out_unlock_ptp: 19576cf99c13SVladimir Oltean mutex_unlock(&priv->ptp_data.lock); 19586666cebcSVladimir Oltean 19592eea1fa8SVladimir Oltean dev_info(priv->ds->dev, 19602eea1fa8SVladimir Oltean "Reset switch and programmed static config. Reason: %s\n", 19612eea1fa8SVladimir Oltean sja1105_reset_reasons[reason]); 19622eea1fa8SVladimir Oltean 19636666cebcSVladimir Oltean /* Configure the CGU (PLLs) for MII and RMII PHYs. 19646666cebcSVladimir Oltean * For these interfaces there is no dynamic configuration 19656666cebcSVladimir Oltean * needed, since PLLs have same settings at all speeds. 19666666cebcSVladimir Oltean */ 1967c5037678SVladimir Oltean rc = priv->info->clocking_setup(priv); 19686666cebcSVladimir Oltean if (rc < 0) 19696666cebcSVladimir Oltean goto out; 19706666cebcSVladimir Oltean 1971542043e9SVladimir Oltean for (i = 0; i < ds->num_ports; i++) { 197284db00f2SVladimir Oltean bool an_enabled; 197384db00f2SVladimir Oltean 19748400cff6SVladimir Oltean rc = sja1105_adjust_port_config(priv, i, speed_mbps[i]); 19756666cebcSVladimir Oltean if (rc < 0) 19766666cebcSVladimir Oltean goto out; 1977ffe10e67SVladimir Oltean 197891a05078SVladimir Oltean if (priv->phy_mode[i] != PHY_INTERFACE_MODE_SGMII) 197984db00f2SVladimir Oltean continue; 1980ffe10e67SVladimir Oltean 198184db00f2SVladimir Oltean an_enabled = !!(bmcr[i] & BMCR_ANENABLE); 198284db00f2SVladimir Oltean 198384db00f2SVladimir Oltean sja1105_sgmii_pcs_config(priv, i, an_enabled, false); 1984ffe10e67SVladimir Oltean 1985ffe10e67SVladimir Oltean if (!an_enabled) { 1986ffe10e67SVladimir Oltean int speed = SPEED_UNKNOWN; 1987ffe10e67SVladimir Oltean 198884db00f2SVladimir Oltean if (bmcr[i] & BMCR_SPEED1000) 1989ffe10e67SVladimir Oltean speed = SPEED_1000; 199084db00f2SVladimir Oltean else if (bmcr[i] & BMCR_SPEED100) 1991ffe10e67SVladimir Oltean speed = SPEED_100; 1992053d8ad1SVladimir Oltean else 1993ffe10e67SVladimir Oltean speed = SPEED_10; 1994ffe10e67SVladimir Oltean 199584db00f2SVladimir Oltean sja1105_sgmii_pcs_force_speed(priv, i, speed); 1996ffe10e67SVladimir Oltean } 1997ffe10e67SVladimir Oltean } 19984d752508SVladimir Oltean 19994d752508SVladimir Oltean rc = sja1105_reload_cbs(priv); 20004d752508SVladimir Oltean if (rc < 0) 20014d752508SVladimir Oltean goto out; 20026666cebcSVladimir Oltean out: 2003af580ae2SVladimir Oltean mutex_unlock(&priv->mgmt_lock); 2004af580ae2SVladimir Oltean 20056666cebcSVladimir Oltean return rc; 20066666cebcSVladimir Oltean } 20076666cebcSVladimir Oltean 20086666cebcSVladimir Oltean static int sja1105_pvid_apply(struct sja1105_private *priv, int port, u16 pvid) 20096666cebcSVladimir Oltean { 20106666cebcSVladimir Oltean struct sja1105_mac_config_entry *mac; 20116666cebcSVladimir Oltean 20126666cebcSVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 20136666cebcSVladimir Oltean 20146666cebcSVladimir Oltean mac[port].vlanid = pvid; 20156666cebcSVladimir Oltean 20166666cebcSVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, 20176666cebcSVladimir Oltean &mac[port], true); 20186666cebcSVladimir Oltean } 20196666cebcSVladimir Oltean 2020ac02a451SVladimir Oltean static int sja1105_crosschip_bridge_join(struct dsa_switch *ds, 2021ac02a451SVladimir Oltean int tree_index, int sw_index, 2022ac02a451SVladimir Oltean int other_port, struct net_device *br) 2023ac02a451SVladimir Oltean { 2024ac02a451SVladimir Oltean struct dsa_switch *other_ds = dsa_switch_find(tree_index, sw_index); 2025ac02a451SVladimir Oltean struct sja1105_private *other_priv = other_ds->priv; 2026ac02a451SVladimir Oltean struct sja1105_private *priv = ds->priv; 2027ac02a451SVladimir Oltean int port, rc; 2028ac02a451SVladimir Oltean 2029ac02a451SVladimir Oltean if (other_ds->ops != &sja1105_switch_ops) 2030ac02a451SVladimir Oltean return 0; 2031ac02a451SVladimir Oltean 2032ac02a451SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 2033ac02a451SVladimir Oltean if (!dsa_is_user_port(ds, port)) 2034ac02a451SVladimir Oltean continue; 2035ac02a451SVladimir Oltean if (dsa_to_port(ds, port)->bridge_dev != br) 2036ac02a451SVladimir Oltean continue; 2037ac02a451SVladimir Oltean 20385899ee36SVladimir Oltean rc = dsa_8021q_crosschip_bridge_join(priv->dsa_8021q_ctx, 20395899ee36SVladimir Oltean port, 20405899ee36SVladimir Oltean other_priv->dsa_8021q_ctx, 20415899ee36SVladimir Oltean other_port); 2042ac02a451SVladimir Oltean if (rc) 2043ac02a451SVladimir Oltean return rc; 2044ac02a451SVladimir Oltean 20455899ee36SVladimir Oltean rc = dsa_8021q_crosschip_bridge_join(other_priv->dsa_8021q_ctx, 20465899ee36SVladimir Oltean other_port, 20475899ee36SVladimir Oltean priv->dsa_8021q_ctx, 20485899ee36SVladimir Oltean port); 2049ac02a451SVladimir Oltean if (rc) 2050ac02a451SVladimir Oltean return rc; 2051ac02a451SVladimir Oltean } 2052ac02a451SVladimir Oltean 2053ac02a451SVladimir Oltean return 0; 2054ac02a451SVladimir Oltean } 2055ac02a451SVladimir Oltean 2056ac02a451SVladimir Oltean static void sja1105_crosschip_bridge_leave(struct dsa_switch *ds, 2057ac02a451SVladimir Oltean int tree_index, int sw_index, 2058ac02a451SVladimir Oltean int other_port, 2059ac02a451SVladimir Oltean struct net_device *br) 2060ac02a451SVladimir Oltean { 2061ac02a451SVladimir Oltean struct dsa_switch *other_ds = dsa_switch_find(tree_index, sw_index); 2062ac02a451SVladimir Oltean struct sja1105_private *other_priv = other_ds->priv; 2063ac02a451SVladimir Oltean struct sja1105_private *priv = ds->priv; 2064ac02a451SVladimir Oltean int port; 2065ac02a451SVladimir Oltean 2066ac02a451SVladimir Oltean if (other_ds->ops != &sja1105_switch_ops) 2067ac02a451SVladimir Oltean return; 2068ac02a451SVladimir Oltean 2069ac02a451SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 2070ac02a451SVladimir Oltean if (!dsa_is_user_port(ds, port)) 2071ac02a451SVladimir Oltean continue; 2072ac02a451SVladimir Oltean if (dsa_to_port(ds, port)->bridge_dev != br) 2073ac02a451SVladimir Oltean continue; 2074ac02a451SVladimir Oltean 20755899ee36SVladimir Oltean dsa_8021q_crosschip_bridge_leave(priv->dsa_8021q_ctx, port, 20765899ee36SVladimir Oltean other_priv->dsa_8021q_ctx, 20775899ee36SVladimir Oltean other_port); 2078ac02a451SVladimir Oltean 20795899ee36SVladimir Oltean dsa_8021q_crosschip_bridge_leave(other_priv->dsa_8021q_ctx, 20805899ee36SVladimir Oltean other_port, 20815899ee36SVladimir Oltean priv->dsa_8021q_ctx, port); 2082ac02a451SVladimir Oltean } 2083ac02a451SVladimir Oltean } 2084ac02a451SVladimir Oltean 2085227d07a0SVladimir Oltean static int sja1105_setup_8021q_tagging(struct dsa_switch *ds, bool enabled) 2086227d07a0SVladimir Oltean { 208760b33aebSVladimir Oltean struct sja1105_private *priv = ds->priv; 20887e092af2SVladimir Oltean int rc; 2089227d07a0SVladimir Oltean 20905899ee36SVladimir Oltean rc = dsa_8021q_setup(priv->dsa_8021q_ctx, enabled); 20917e092af2SVladimir Oltean if (rc) 2092227d07a0SVladimir Oltean return rc; 2093ac02a451SVladimir Oltean 2094227d07a0SVladimir Oltean dev_info(ds->dev, "%s switch tagging\n", 2095227d07a0SVladimir Oltean enabled ? "Enabled" : "Disabled"); 2096227d07a0SVladimir Oltean return 0; 2097227d07a0SVladimir Oltean } 2098227d07a0SVladimir Oltean 20998aa9ebccSVladimir Oltean static enum dsa_tag_protocol 21004d776482SFlorian Fainelli sja1105_get_tag_protocol(struct dsa_switch *ds, int port, 21014d776482SFlorian Fainelli enum dsa_tag_protocol mp) 21028aa9ebccSVladimir Oltean { 2103227d07a0SVladimir Oltean return DSA_TAG_PROTO_SJA1105; 21048aa9ebccSVladimir Oltean } 21058aa9ebccSVladimir Oltean 21063f01c91aSVladimir Oltean static int sja1105_find_free_subvlan(u16 *subvlan_map, bool pvid) 21073f01c91aSVladimir Oltean { 21083f01c91aSVladimir Oltean int subvlan; 21093f01c91aSVladimir Oltean 21103f01c91aSVladimir Oltean if (pvid) 21113f01c91aSVladimir Oltean return 0; 21123f01c91aSVladimir Oltean 21133f01c91aSVladimir Oltean for (subvlan = 1; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++) 21143f01c91aSVladimir Oltean if (subvlan_map[subvlan] == VLAN_N_VID) 21153f01c91aSVladimir Oltean return subvlan; 21163f01c91aSVladimir Oltean 21173f01c91aSVladimir Oltean return -1; 21183f01c91aSVladimir Oltean } 21193f01c91aSVladimir Oltean 21203f01c91aSVladimir Oltean static int sja1105_find_subvlan(u16 *subvlan_map, u16 vid) 21213f01c91aSVladimir Oltean { 21223f01c91aSVladimir Oltean int subvlan; 21233f01c91aSVladimir Oltean 21243f01c91aSVladimir Oltean for (subvlan = 0; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++) 21253f01c91aSVladimir Oltean if (subvlan_map[subvlan] == vid) 21263f01c91aSVladimir Oltean return subvlan; 21273f01c91aSVladimir Oltean 21283f01c91aSVladimir Oltean return -1; 21293f01c91aSVladimir Oltean } 21303f01c91aSVladimir Oltean 21313f01c91aSVladimir Oltean static int sja1105_find_committed_subvlan(struct sja1105_private *priv, 21323f01c91aSVladimir Oltean int port, u16 vid) 21333f01c91aSVladimir Oltean { 21343f01c91aSVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 21353f01c91aSVladimir Oltean 21363f01c91aSVladimir Oltean return sja1105_find_subvlan(sp->subvlan_map, vid); 21373f01c91aSVladimir Oltean } 21383f01c91aSVladimir Oltean 21393f01c91aSVladimir Oltean static void sja1105_init_subvlan_map(u16 *subvlan_map) 21403f01c91aSVladimir Oltean { 21413f01c91aSVladimir Oltean int subvlan; 21423f01c91aSVladimir Oltean 21433f01c91aSVladimir Oltean for (subvlan = 0; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++) 21443f01c91aSVladimir Oltean subvlan_map[subvlan] = VLAN_N_VID; 21453f01c91aSVladimir Oltean } 21463f01c91aSVladimir Oltean 21473f01c91aSVladimir Oltean static void sja1105_commit_subvlan_map(struct sja1105_private *priv, int port, 21483f01c91aSVladimir Oltean u16 *subvlan_map) 21493f01c91aSVladimir Oltean { 21503f01c91aSVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 21513f01c91aSVladimir Oltean int subvlan; 21523f01c91aSVladimir Oltean 21533f01c91aSVladimir Oltean for (subvlan = 0; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++) 21543f01c91aSVladimir Oltean sp->subvlan_map[subvlan] = subvlan_map[subvlan]; 21553f01c91aSVladimir Oltean } 21563f01c91aSVladimir Oltean 2157ec5ae610SVladimir Oltean static int sja1105_is_vlan_configured(struct sja1105_private *priv, u16 vid) 2158ec5ae610SVladimir Oltean { 2159ec5ae610SVladimir Oltean struct sja1105_vlan_lookup_entry *vlan; 2160ec5ae610SVladimir Oltean int count, i; 2161ec5ae610SVladimir Oltean 2162ec5ae610SVladimir Oltean vlan = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entries; 2163ec5ae610SVladimir Oltean count = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entry_count; 2164ec5ae610SVladimir Oltean 2165ec5ae610SVladimir Oltean for (i = 0; i < count; i++) 2166ec5ae610SVladimir Oltean if (vlan[i].vlanid == vid) 2167ec5ae610SVladimir Oltean return i; 2168ec5ae610SVladimir Oltean 2169ec5ae610SVladimir Oltean /* Return an invalid entry index if not found */ 2170ec5ae610SVladimir Oltean return -1; 2171ec5ae610SVladimir Oltean } 2172ec5ae610SVladimir Oltean 21733f01c91aSVladimir Oltean static int 21743f01c91aSVladimir Oltean sja1105_find_retagging_entry(struct sja1105_retagging_entry *retagging, 21753f01c91aSVladimir Oltean int count, int from_port, u16 from_vid, 21763f01c91aSVladimir Oltean u16 to_vid) 2177ec5ae610SVladimir Oltean { 21783f01c91aSVladimir Oltean int i; 21793f01c91aSVladimir Oltean 21803f01c91aSVladimir Oltean for (i = 0; i < count; i++) 21813f01c91aSVladimir Oltean if (retagging[i].ing_port == BIT(from_port) && 21823f01c91aSVladimir Oltean retagging[i].vlan_ing == from_vid && 21833f01c91aSVladimir Oltean retagging[i].vlan_egr == to_vid) 21843f01c91aSVladimir Oltean return i; 21853f01c91aSVladimir Oltean 21863f01c91aSVladimir Oltean /* Return an invalid entry index if not found */ 21873f01c91aSVladimir Oltean return -1; 21883f01c91aSVladimir Oltean } 21893f01c91aSVladimir Oltean 21903f01c91aSVladimir Oltean static int sja1105_commit_vlans(struct sja1105_private *priv, 21913f01c91aSVladimir Oltean struct sja1105_vlan_lookup_entry *new_vlan, 21923f01c91aSVladimir Oltean struct sja1105_retagging_entry *new_retagging, 21933f01c91aSVladimir Oltean int num_retagging) 21943f01c91aSVladimir Oltean { 21953f01c91aSVladimir Oltean struct sja1105_retagging_entry *retagging; 2196ec5ae610SVladimir Oltean struct sja1105_vlan_lookup_entry *vlan; 2197ec5ae610SVladimir Oltean struct sja1105_table *table; 2198ec5ae610SVladimir Oltean int num_vlans = 0; 2199ec5ae610SVladimir Oltean int rc, i, k = 0; 2200ec5ae610SVladimir Oltean 2201ec5ae610SVladimir Oltean /* VLAN table */ 2202ec5ae610SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP]; 2203ec5ae610SVladimir Oltean vlan = table->entries; 2204ec5ae610SVladimir Oltean 2205ec5ae610SVladimir Oltean for (i = 0; i < VLAN_N_VID; i++) { 2206ec5ae610SVladimir Oltean int match = sja1105_is_vlan_configured(priv, i); 2207ec5ae610SVladimir Oltean 2208ec5ae610SVladimir Oltean if (new_vlan[i].vlanid != VLAN_N_VID) 2209ec5ae610SVladimir Oltean num_vlans++; 2210ec5ae610SVladimir Oltean 2211ec5ae610SVladimir Oltean if (new_vlan[i].vlanid == VLAN_N_VID && match >= 0) { 2212ec5ae610SVladimir Oltean /* Was there before, no longer is. Delete */ 2213ec5ae610SVladimir Oltean dev_dbg(priv->ds->dev, "Deleting VLAN %d\n", i); 2214ec5ae610SVladimir Oltean rc = sja1105_dynamic_config_write(priv, 2215ec5ae610SVladimir Oltean BLK_IDX_VLAN_LOOKUP, 2216ec5ae610SVladimir Oltean i, &vlan[match], false); 2217ec5ae610SVladimir Oltean if (rc < 0) 2218ec5ae610SVladimir Oltean return rc; 2219ec5ae610SVladimir Oltean } else if (new_vlan[i].vlanid != VLAN_N_VID) { 2220ec5ae610SVladimir Oltean /* Nothing changed, don't do anything */ 2221ec5ae610SVladimir Oltean if (match >= 0 && 2222ec5ae610SVladimir Oltean vlan[match].vlanid == new_vlan[i].vlanid && 2223ec5ae610SVladimir Oltean vlan[match].tag_port == new_vlan[i].tag_port && 2224ec5ae610SVladimir Oltean vlan[match].vlan_bc == new_vlan[i].vlan_bc && 2225ec5ae610SVladimir Oltean vlan[match].vmemb_port == new_vlan[i].vmemb_port) 2226ec5ae610SVladimir Oltean continue; 2227ec5ae610SVladimir Oltean /* Update entry */ 2228ec5ae610SVladimir Oltean dev_dbg(priv->ds->dev, "Updating VLAN %d\n", i); 2229ec5ae610SVladimir Oltean rc = sja1105_dynamic_config_write(priv, 2230ec5ae610SVladimir Oltean BLK_IDX_VLAN_LOOKUP, 2231ec5ae610SVladimir Oltean i, &new_vlan[i], 2232ec5ae610SVladimir Oltean true); 2233ec5ae610SVladimir Oltean if (rc < 0) 2234ec5ae610SVladimir Oltean return rc; 2235ec5ae610SVladimir Oltean } 2236ec5ae610SVladimir Oltean } 2237ec5ae610SVladimir Oltean 2238ec5ae610SVladimir Oltean if (table->entry_count) 2239ec5ae610SVladimir Oltean kfree(table->entries); 2240ec5ae610SVladimir Oltean 2241ec5ae610SVladimir Oltean table->entries = kcalloc(num_vlans, table->ops->unpacked_entry_size, 2242ec5ae610SVladimir Oltean GFP_KERNEL); 2243ec5ae610SVladimir Oltean if (!table->entries) 2244ec5ae610SVladimir Oltean return -ENOMEM; 2245ec5ae610SVladimir Oltean 2246ec5ae610SVladimir Oltean table->entry_count = num_vlans; 2247ec5ae610SVladimir Oltean vlan = table->entries; 2248ec5ae610SVladimir Oltean 2249ec5ae610SVladimir Oltean for (i = 0; i < VLAN_N_VID; i++) { 2250ec5ae610SVladimir Oltean if (new_vlan[i].vlanid == VLAN_N_VID) 2251ec5ae610SVladimir Oltean continue; 2252ec5ae610SVladimir Oltean vlan[k++] = new_vlan[i]; 2253ec5ae610SVladimir Oltean } 2254ec5ae610SVladimir Oltean 22553f01c91aSVladimir Oltean /* VLAN Retagging Table */ 22563f01c91aSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_RETAGGING]; 22573f01c91aSVladimir Oltean retagging = table->entries; 22583f01c91aSVladimir Oltean 22593f01c91aSVladimir Oltean for (i = 0; i < table->entry_count; i++) { 22603f01c91aSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_RETAGGING, 22613f01c91aSVladimir Oltean i, &retagging[i], false); 22623f01c91aSVladimir Oltean if (rc) 22633f01c91aSVladimir Oltean return rc; 22643f01c91aSVladimir Oltean } 22653f01c91aSVladimir Oltean 22663f01c91aSVladimir Oltean if (table->entry_count) 22673f01c91aSVladimir Oltean kfree(table->entries); 22683f01c91aSVladimir Oltean 22693f01c91aSVladimir Oltean table->entries = kcalloc(num_retagging, table->ops->unpacked_entry_size, 22703f01c91aSVladimir Oltean GFP_KERNEL); 22713f01c91aSVladimir Oltean if (!table->entries) 22723f01c91aSVladimir Oltean return -ENOMEM; 22733f01c91aSVladimir Oltean 22743f01c91aSVladimir Oltean table->entry_count = num_retagging; 22753f01c91aSVladimir Oltean retagging = table->entries; 22763f01c91aSVladimir Oltean 22773f01c91aSVladimir Oltean for (i = 0; i < num_retagging; i++) { 22783f01c91aSVladimir Oltean retagging[i] = new_retagging[i]; 22793f01c91aSVladimir Oltean 22803f01c91aSVladimir Oltean /* Update entry */ 22813f01c91aSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_RETAGGING, 22823f01c91aSVladimir Oltean i, &retagging[i], true); 22833f01c91aSVladimir Oltean if (rc < 0) 22843f01c91aSVladimir Oltean return rc; 22853f01c91aSVladimir Oltean } 22863f01c91aSVladimir Oltean 2287ec5ae610SVladimir Oltean return 0; 2288ec5ae610SVladimir Oltean } 2289ec5ae610SVladimir Oltean 22903f01c91aSVladimir Oltean struct sja1105_crosschip_vlan { 22913f01c91aSVladimir Oltean struct list_head list; 22923f01c91aSVladimir Oltean u16 vid; 22933f01c91aSVladimir Oltean bool untagged; 22943f01c91aSVladimir Oltean int port; 22953f01c91aSVladimir Oltean int other_port; 22965899ee36SVladimir Oltean struct dsa_8021q_context *other_ctx; 22973f01c91aSVladimir Oltean }; 22983f01c91aSVladimir Oltean 2299ec5ae610SVladimir Oltean struct sja1105_crosschip_switch { 2300ec5ae610SVladimir Oltean struct list_head list; 23015899ee36SVladimir Oltean struct dsa_8021q_context *other_ctx; 2302ec5ae610SVladimir Oltean }; 2303ec5ae610SVladimir Oltean 2304ec5ae610SVladimir Oltean static int sja1105_commit_pvid(struct sja1105_private *priv) 2305ec5ae610SVladimir Oltean { 2306ec5ae610SVladimir Oltean struct sja1105_bridge_vlan *v; 2307ec5ae610SVladimir Oltean struct list_head *vlan_list; 2308ec5ae610SVladimir Oltean int rc = 0; 2309ec5ae610SVladimir Oltean 2310ec5ae610SVladimir Oltean if (priv->vlan_state == SJA1105_VLAN_FILTERING_FULL) 2311ec5ae610SVladimir Oltean vlan_list = &priv->bridge_vlans; 2312ec5ae610SVladimir Oltean else 2313ec5ae610SVladimir Oltean vlan_list = &priv->dsa_8021q_vlans; 2314ec5ae610SVladimir Oltean 2315ec5ae610SVladimir Oltean list_for_each_entry(v, vlan_list, list) { 2316ec5ae610SVladimir Oltean if (v->pvid) { 2317ec5ae610SVladimir Oltean rc = sja1105_pvid_apply(priv, v->port, v->vid); 2318ec5ae610SVladimir Oltean if (rc) 2319ec5ae610SVladimir Oltean break; 2320ec5ae610SVladimir Oltean } 2321ec5ae610SVladimir Oltean } 2322ec5ae610SVladimir Oltean 2323ec5ae610SVladimir Oltean return rc; 2324ec5ae610SVladimir Oltean } 2325ec5ae610SVladimir Oltean 2326ec5ae610SVladimir Oltean static int 2327ec5ae610SVladimir Oltean sja1105_build_bridge_vlans(struct sja1105_private *priv, 2328ec5ae610SVladimir Oltean struct sja1105_vlan_lookup_entry *new_vlan) 2329ec5ae610SVladimir Oltean { 2330ec5ae610SVladimir Oltean struct sja1105_bridge_vlan *v; 2331ec5ae610SVladimir Oltean 2332ec5ae610SVladimir Oltean if (priv->vlan_state == SJA1105_VLAN_UNAWARE) 2333ec5ae610SVladimir Oltean return 0; 2334ec5ae610SVladimir Oltean 2335ec5ae610SVladimir Oltean list_for_each_entry(v, &priv->bridge_vlans, list) { 2336ec5ae610SVladimir Oltean int match = v->vid; 2337ec5ae610SVladimir Oltean 2338ec5ae610SVladimir Oltean new_vlan[match].vlanid = v->vid; 2339ec5ae610SVladimir Oltean new_vlan[match].vmemb_port |= BIT(v->port); 2340ec5ae610SVladimir Oltean new_vlan[match].vlan_bc |= BIT(v->port); 2341ec5ae610SVladimir Oltean if (!v->untagged) 2342ec5ae610SVladimir Oltean new_vlan[match].tag_port |= BIT(v->port); 2343*3e77e59bSVladimir Oltean new_vlan[match].type_entry = SJA1110_VLAN_D_TAG; 2344ec5ae610SVladimir Oltean } 2345ec5ae610SVladimir Oltean 2346ec5ae610SVladimir Oltean return 0; 2347ec5ae610SVladimir Oltean } 2348ec5ae610SVladimir Oltean 2349ec5ae610SVladimir Oltean static int 2350ec5ae610SVladimir Oltean sja1105_build_dsa_8021q_vlans(struct sja1105_private *priv, 2351ec5ae610SVladimir Oltean struct sja1105_vlan_lookup_entry *new_vlan) 2352ec5ae610SVladimir Oltean { 2353ec5ae610SVladimir Oltean struct sja1105_bridge_vlan *v; 2354ec5ae610SVladimir Oltean 2355ec5ae610SVladimir Oltean if (priv->vlan_state == SJA1105_VLAN_FILTERING_FULL) 2356ec5ae610SVladimir Oltean return 0; 2357ec5ae610SVladimir Oltean 2358ec5ae610SVladimir Oltean list_for_each_entry(v, &priv->dsa_8021q_vlans, list) { 2359ec5ae610SVladimir Oltean int match = v->vid; 2360ec5ae610SVladimir Oltean 2361ec5ae610SVladimir Oltean new_vlan[match].vlanid = v->vid; 2362ec5ae610SVladimir Oltean new_vlan[match].vmemb_port |= BIT(v->port); 2363ec5ae610SVladimir Oltean new_vlan[match].vlan_bc |= BIT(v->port); 2364ec5ae610SVladimir Oltean if (!v->untagged) 2365ec5ae610SVladimir Oltean new_vlan[match].tag_port |= BIT(v->port); 2366*3e77e59bSVladimir Oltean new_vlan[match].type_entry = SJA1110_VLAN_D_TAG; 2367ec5ae610SVladimir Oltean } 2368ec5ae610SVladimir Oltean 2369ec5ae610SVladimir Oltean return 0; 2370ec5ae610SVladimir Oltean } 2371ec5ae610SVladimir Oltean 23723f01c91aSVladimir Oltean static int sja1105_build_subvlans(struct sja1105_private *priv, 23733f01c91aSVladimir Oltean u16 subvlan_map[][DSA_8021Q_N_SUBVLAN], 23743f01c91aSVladimir Oltean struct sja1105_vlan_lookup_entry *new_vlan, 23753f01c91aSVladimir Oltean struct sja1105_retagging_entry *new_retagging, 23763f01c91aSVladimir Oltean int *num_retagging) 23773f01c91aSVladimir Oltean { 23783f01c91aSVladimir Oltean struct sja1105_bridge_vlan *v; 23793f01c91aSVladimir Oltean int k = *num_retagging; 23803f01c91aSVladimir Oltean 23813f01c91aSVladimir Oltean if (priv->vlan_state != SJA1105_VLAN_BEST_EFFORT) 23823f01c91aSVladimir Oltean return 0; 23833f01c91aSVladimir Oltean 23843f01c91aSVladimir Oltean list_for_each_entry(v, &priv->bridge_vlans, list) { 23853f01c91aSVladimir Oltean int upstream = dsa_upstream_port(priv->ds, v->port); 23863f01c91aSVladimir Oltean int match, subvlan; 23873f01c91aSVladimir Oltean u16 rx_vid; 23883f01c91aSVladimir Oltean 23893f01c91aSVladimir Oltean /* Only sub-VLANs on user ports need to be applied. 23903f01c91aSVladimir Oltean * Bridge VLANs also include VLANs added automatically 23913f01c91aSVladimir Oltean * by DSA on the CPU port. 23923f01c91aSVladimir Oltean */ 23933f01c91aSVladimir Oltean if (!dsa_is_user_port(priv->ds, v->port)) 23943f01c91aSVladimir Oltean continue; 23953f01c91aSVladimir Oltean 23963f01c91aSVladimir Oltean subvlan = sja1105_find_subvlan(subvlan_map[v->port], 23973f01c91aSVladimir Oltean v->vid); 23983f01c91aSVladimir Oltean if (subvlan < 0) { 23993f01c91aSVladimir Oltean subvlan = sja1105_find_free_subvlan(subvlan_map[v->port], 24003f01c91aSVladimir Oltean v->pvid); 24013f01c91aSVladimir Oltean if (subvlan < 0) { 24023f01c91aSVladimir Oltean dev_err(priv->ds->dev, "No more free subvlans\n"); 24033f01c91aSVladimir Oltean return -ENOSPC; 24043f01c91aSVladimir Oltean } 24053f01c91aSVladimir Oltean } 24063f01c91aSVladimir Oltean 24073f01c91aSVladimir Oltean rx_vid = dsa_8021q_rx_vid_subvlan(priv->ds, v->port, subvlan); 24083f01c91aSVladimir Oltean 24093f01c91aSVladimir Oltean /* @v->vid on @v->port needs to be retagged to @rx_vid 24103f01c91aSVladimir Oltean * on @upstream. Assume @v->vid on @v->port and on 24113f01c91aSVladimir Oltean * @upstream was already configured by the previous 24123f01c91aSVladimir Oltean * iteration over bridge_vlans. 24133f01c91aSVladimir Oltean */ 24143f01c91aSVladimir Oltean match = rx_vid; 24153f01c91aSVladimir Oltean new_vlan[match].vlanid = rx_vid; 24163f01c91aSVladimir Oltean new_vlan[match].vmemb_port |= BIT(v->port); 24173f01c91aSVladimir Oltean new_vlan[match].vmemb_port |= BIT(upstream); 24183f01c91aSVladimir Oltean new_vlan[match].vlan_bc |= BIT(v->port); 24193f01c91aSVladimir Oltean new_vlan[match].vlan_bc |= BIT(upstream); 24203f01c91aSVladimir Oltean /* The "untagged" flag is set the same as for the 24213f01c91aSVladimir Oltean * original VLAN 24223f01c91aSVladimir Oltean */ 24233f01c91aSVladimir Oltean if (!v->untagged) 24243f01c91aSVladimir Oltean new_vlan[match].tag_port |= BIT(v->port); 24253f01c91aSVladimir Oltean /* But it's always tagged towards the CPU */ 24263f01c91aSVladimir Oltean new_vlan[match].tag_port |= BIT(upstream); 2427*3e77e59bSVladimir Oltean new_vlan[match].type_entry = SJA1110_VLAN_D_TAG; 24283f01c91aSVladimir Oltean 24293f01c91aSVladimir Oltean /* The Retagging Table generates packet *clones* with 24303f01c91aSVladimir Oltean * the new VLAN. This is a very odd hardware quirk 24313f01c91aSVladimir Oltean * which we need to suppress by dropping the original 24323f01c91aSVladimir Oltean * packet. 24333f01c91aSVladimir Oltean * Deny egress of the original VLAN towards the CPU 24343f01c91aSVladimir Oltean * port. This will force the switch to drop it, and 24353f01c91aSVladimir Oltean * we'll see only the retagged packets. 24363f01c91aSVladimir Oltean */ 24373f01c91aSVladimir Oltean match = v->vid; 24383f01c91aSVladimir Oltean new_vlan[match].vlan_bc &= ~BIT(upstream); 24393f01c91aSVladimir Oltean 24403f01c91aSVladimir Oltean /* And the retagging itself */ 24413f01c91aSVladimir Oltean new_retagging[k].vlan_ing = v->vid; 24423f01c91aSVladimir Oltean new_retagging[k].vlan_egr = rx_vid; 24433f01c91aSVladimir Oltean new_retagging[k].ing_port = BIT(v->port); 24443f01c91aSVladimir Oltean new_retagging[k].egr_port = BIT(upstream); 24453f01c91aSVladimir Oltean if (k++ == SJA1105_MAX_RETAGGING_COUNT) { 24463f01c91aSVladimir Oltean dev_err(priv->ds->dev, "No more retagging rules\n"); 24473f01c91aSVladimir Oltean return -ENOSPC; 24483f01c91aSVladimir Oltean } 24493f01c91aSVladimir Oltean 24503f01c91aSVladimir Oltean subvlan_map[v->port][subvlan] = v->vid; 24513f01c91aSVladimir Oltean } 24523f01c91aSVladimir Oltean 24533f01c91aSVladimir Oltean *num_retagging = k; 24543f01c91aSVladimir Oltean 24553f01c91aSVladimir Oltean return 0; 24563f01c91aSVladimir Oltean } 24573f01c91aSVladimir Oltean 24583f01c91aSVladimir Oltean /* Sadly, in crosschip scenarios where the CPU port is also the link to another 24593f01c91aSVladimir Oltean * switch, we should retag backwards (the dsa_8021q vid to the original vid) on 24603f01c91aSVladimir Oltean * the CPU port of neighbour switches. 24613f01c91aSVladimir Oltean */ 24623f01c91aSVladimir Oltean static int 24633f01c91aSVladimir Oltean sja1105_build_crosschip_subvlans(struct sja1105_private *priv, 24643f01c91aSVladimir Oltean struct sja1105_vlan_lookup_entry *new_vlan, 24653f01c91aSVladimir Oltean struct sja1105_retagging_entry *new_retagging, 24663f01c91aSVladimir Oltean int *num_retagging) 24673f01c91aSVladimir Oltean { 24683f01c91aSVladimir Oltean struct sja1105_crosschip_vlan *tmp, *pos; 24693f01c91aSVladimir Oltean struct dsa_8021q_crosschip_link *c; 24703f01c91aSVladimir Oltean struct sja1105_bridge_vlan *v, *w; 24713f01c91aSVladimir Oltean struct list_head crosschip_vlans; 24723f01c91aSVladimir Oltean int k = *num_retagging; 24733f01c91aSVladimir Oltean int rc = 0; 24743f01c91aSVladimir Oltean 24753f01c91aSVladimir Oltean if (priv->vlan_state != SJA1105_VLAN_BEST_EFFORT) 24763f01c91aSVladimir Oltean return 0; 24773f01c91aSVladimir Oltean 24783f01c91aSVladimir Oltean INIT_LIST_HEAD(&crosschip_vlans); 24793f01c91aSVladimir Oltean 24805899ee36SVladimir Oltean list_for_each_entry(c, &priv->dsa_8021q_ctx->crosschip_links, list) { 24815899ee36SVladimir Oltean struct sja1105_private *other_priv = c->other_ctx->ds->priv; 24823f01c91aSVladimir Oltean 24833f01c91aSVladimir Oltean if (other_priv->vlan_state == SJA1105_VLAN_FILTERING_FULL) 24843f01c91aSVladimir Oltean continue; 24853f01c91aSVladimir Oltean 24863f01c91aSVladimir Oltean /* Crosschip links are also added to the CPU ports. 24873f01c91aSVladimir Oltean * Ignore those. 24883f01c91aSVladimir Oltean */ 24893f01c91aSVladimir Oltean if (!dsa_is_user_port(priv->ds, c->port)) 24903f01c91aSVladimir Oltean continue; 24915899ee36SVladimir Oltean if (!dsa_is_user_port(c->other_ctx->ds, c->other_port)) 24923f01c91aSVladimir Oltean continue; 24933f01c91aSVladimir Oltean 24943f01c91aSVladimir Oltean /* Search for VLANs on the remote port */ 24953f01c91aSVladimir Oltean list_for_each_entry(v, &other_priv->bridge_vlans, list) { 24963f01c91aSVladimir Oltean bool already_added = false; 24973f01c91aSVladimir Oltean bool we_have_it = false; 24983f01c91aSVladimir Oltean 24993f01c91aSVladimir Oltean if (v->port != c->other_port) 25003f01c91aSVladimir Oltean continue; 25013f01c91aSVladimir Oltean 25023f01c91aSVladimir Oltean /* If @v is a pvid on @other_ds, it does not need 25033f01c91aSVladimir Oltean * re-retagging, because its SVL field is 0 and we 25043f01c91aSVladimir Oltean * already allow that, via the dsa_8021q crosschip 25053f01c91aSVladimir Oltean * links. 25063f01c91aSVladimir Oltean */ 25073f01c91aSVladimir Oltean if (v->pvid) 25083f01c91aSVladimir Oltean continue; 25093f01c91aSVladimir Oltean 25103f01c91aSVladimir Oltean /* Search for the VLAN on our local port */ 25113f01c91aSVladimir Oltean list_for_each_entry(w, &priv->bridge_vlans, list) { 25123f01c91aSVladimir Oltean if (w->port == c->port && w->vid == v->vid) { 25133f01c91aSVladimir Oltean we_have_it = true; 25143f01c91aSVladimir Oltean break; 25153f01c91aSVladimir Oltean } 25163f01c91aSVladimir Oltean } 25173f01c91aSVladimir Oltean 25183f01c91aSVladimir Oltean if (!we_have_it) 25193f01c91aSVladimir Oltean continue; 25203f01c91aSVladimir Oltean 25213f01c91aSVladimir Oltean list_for_each_entry(tmp, &crosschip_vlans, list) { 25223f01c91aSVladimir Oltean if (tmp->vid == v->vid && 25233f01c91aSVladimir Oltean tmp->untagged == v->untagged && 25243f01c91aSVladimir Oltean tmp->port == c->port && 25253f01c91aSVladimir Oltean tmp->other_port == v->port && 25265899ee36SVladimir Oltean tmp->other_ctx == c->other_ctx) { 25273f01c91aSVladimir Oltean already_added = true; 25283f01c91aSVladimir Oltean break; 25293f01c91aSVladimir Oltean } 25303f01c91aSVladimir Oltean } 25313f01c91aSVladimir Oltean 25323f01c91aSVladimir Oltean if (already_added) 25333f01c91aSVladimir Oltean continue; 25343f01c91aSVladimir Oltean 25353f01c91aSVladimir Oltean tmp = kzalloc(sizeof(*tmp), GFP_KERNEL); 25363f01c91aSVladimir Oltean if (!tmp) { 25373f01c91aSVladimir Oltean dev_err(priv->ds->dev, "Failed to allocate memory\n"); 25383f01c91aSVladimir Oltean rc = -ENOMEM; 25393f01c91aSVladimir Oltean goto out; 25403f01c91aSVladimir Oltean } 25413f01c91aSVladimir Oltean tmp->vid = v->vid; 25423f01c91aSVladimir Oltean tmp->port = c->port; 25433f01c91aSVladimir Oltean tmp->other_port = v->port; 25445899ee36SVladimir Oltean tmp->other_ctx = c->other_ctx; 25453f01c91aSVladimir Oltean tmp->untagged = v->untagged; 25463f01c91aSVladimir Oltean list_add(&tmp->list, &crosschip_vlans); 25473f01c91aSVladimir Oltean } 25483f01c91aSVladimir Oltean } 25493f01c91aSVladimir Oltean 25503f01c91aSVladimir Oltean list_for_each_entry(tmp, &crosschip_vlans, list) { 25515899ee36SVladimir Oltean struct sja1105_private *other_priv = tmp->other_ctx->ds->priv; 25523f01c91aSVladimir Oltean int upstream = dsa_upstream_port(priv->ds, tmp->port); 25533f01c91aSVladimir Oltean int match, subvlan; 25543f01c91aSVladimir Oltean u16 rx_vid; 25553f01c91aSVladimir Oltean 25563f01c91aSVladimir Oltean subvlan = sja1105_find_committed_subvlan(other_priv, 25573f01c91aSVladimir Oltean tmp->other_port, 25583f01c91aSVladimir Oltean tmp->vid); 25593f01c91aSVladimir Oltean /* If this happens, it's a bug. The neighbour switch does not 25603f01c91aSVladimir Oltean * have a subvlan for tmp->vid on tmp->other_port, but it 25613f01c91aSVladimir Oltean * should, since we already checked for its vlan_state. 25623f01c91aSVladimir Oltean */ 25633f01c91aSVladimir Oltean if (WARN_ON(subvlan < 0)) { 25643f01c91aSVladimir Oltean rc = -EINVAL; 25653f01c91aSVladimir Oltean goto out; 25663f01c91aSVladimir Oltean } 25673f01c91aSVladimir Oltean 25685899ee36SVladimir Oltean rx_vid = dsa_8021q_rx_vid_subvlan(tmp->other_ctx->ds, 25693f01c91aSVladimir Oltean tmp->other_port, 25703f01c91aSVladimir Oltean subvlan); 25713f01c91aSVladimir Oltean 25723f01c91aSVladimir Oltean /* The @rx_vid retagged from @tmp->vid on 25733f01c91aSVladimir Oltean * {@tmp->other_ds, @tmp->other_port} needs to be 25743f01c91aSVladimir Oltean * re-retagged to @tmp->vid on the way back to us. 25753f01c91aSVladimir Oltean * 25763f01c91aSVladimir Oltean * Assume the original @tmp->vid is already configured 25773f01c91aSVladimir Oltean * on this local switch, otherwise we wouldn't be 25783f01c91aSVladimir Oltean * retagging its subvlan on the other switch in the 25793f01c91aSVladimir Oltean * first place. We just need to add a reverse retagging 25803f01c91aSVladimir Oltean * rule for @rx_vid and install @rx_vid on our ports. 25813f01c91aSVladimir Oltean */ 25823f01c91aSVladimir Oltean match = rx_vid; 25833f01c91aSVladimir Oltean new_vlan[match].vlanid = rx_vid; 25843f01c91aSVladimir Oltean new_vlan[match].vmemb_port |= BIT(tmp->port); 25853f01c91aSVladimir Oltean new_vlan[match].vmemb_port |= BIT(upstream); 25863f01c91aSVladimir Oltean /* The "untagged" flag is set the same as for the 25873f01c91aSVladimir Oltean * original VLAN. And towards the CPU, it doesn't 25883f01c91aSVladimir Oltean * really matter, because @rx_vid will only receive 25893f01c91aSVladimir Oltean * traffic on that port. For consistency with other dsa_8021q 25903f01c91aSVladimir Oltean * VLANs, we'll keep the CPU port tagged. 25913f01c91aSVladimir Oltean */ 25923f01c91aSVladimir Oltean if (!tmp->untagged) 25933f01c91aSVladimir Oltean new_vlan[match].tag_port |= BIT(tmp->port); 25943f01c91aSVladimir Oltean new_vlan[match].tag_port |= BIT(upstream); 2595*3e77e59bSVladimir Oltean new_vlan[match].type_entry = SJA1110_VLAN_D_TAG; 25963f01c91aSVladimir Oltean /* Deny egress of @rx_vid towards our front-panel port. 25973f01c91aSVladimir Oltean * This will force the switch to drop it, and we'll see 25983f01c91aSVladimir Oltean * only the re-retagged packets (having the original, 25993f01c91aSVladimir Oltean * pre-initial-retagging, VLAN @tmp->vid). 26003f01c91aSVladimir Oltean */ 26013f01c91aSVladimir Oltean new_vlan[match].vlan_bc &= ~BIT(tmp->port); 26023f01c91aSVladimir Oltean 26033f01c91aSVladimir Oltean /* On reverse retagging, the same ingress VLAN goes to multiple 26043f01c91aSVladimir Oltean * ports. So we have an opportunity to create composite rules 26053f01c91aSVladimir Oltean * to not waste the limited space in the retagging table. 26063f01c91aSVladimir Oltean */ 26073f01c91aSVladimir Oltean k = sja1105_find_retagging_entry(new_retagging, *num_retagging, 26083f01c91aSVladimir Oltean upstream, rx_vid, tmp->vid); 26093f01c91aSVladimir Oltean if (k < 0) { 26103f01c91aSVladimir Oltean if (*num_retagging == SJA1105_MAX_RETAGGING_COUNT) { 26113f01c91aSVladimir Oltean dev_err(priv->ds->dev, "No more retagging rules\n"); 26123f01c91aSVladimir Oltean rc = -ENOSPC; 26133f01c91aSVladimir Oltean goto out; 26143f01c91aSVladimir Oltean } 26153f01c91aSVladimir Oltean k = (*num_retagging)++; 26163f01c91aSVladimir Oltean } 26173f01c91aSVladimir Oltean /* And the retagging itself */ 26183f01c91aSVladimir Oltean new_retagging[k].vlan_ing = rx_vid; 26193f01c91aSVladimir Oltean new_retagging[k].vlan_egr = tmp->vid; 26203f01c91aSVladimir Oltean new_retagging[k].ing_port = BIT(upstream); 26213f01c91aSVladimir Oltean new_retagging[k].egr_port |= BIT(tmp->port); 26223f01c91aSVladimir Oltean } 26233f01c91aSVladimir Oltean 26243f01c91aSVladimir Oltean out: 26253f01c91aSVladimir Oltean list_for_each_entry_safe(tmp, pos, &crosschip_vlans, list) { 26263f01c91aSVladimir Oltean list_del(&tmp->list); 26273f01c91aSVladimir Oltean kfree(tmp); 26283f01c91aSVladimir Oltean } 26293f01c91aSVladimir Oltean 26303f01c91aSVladimir Oltean return rc; 26313f01c91aSVladimir Oltean } 26323f01c91aSVladimir Oltean 2633ec5ae610SVladimir Oltean static int sja1105_build_vlan_table(struct sja1105_private *priv, bool notify); 2634ec5ae610SVladimir Oltean 2635ec5ae610SVladimir Oltean static int sja1105_notify_crosschip_switches(struct sja1105_private *priv) 2636ec5ae610SVladimir Oltean { 2637ec5ae610SVladimir Oltean struct sja1105_crosschip_switch *s, *pos; 2638ec5ae610SVladimir Oltean struct list_head crosschip_switches; 2639ec5ae610SVladimir Oltean struct dsa_8021q_crosschip_link *c; 2640ec5ae610SVladimir Oltean int rc = 0; 2641ec5ae610SVladimir Oltean 2642ec5ae610SVladimir Oltean INIT_LIST_HEAD(&crosschip_switches); 2643ec5ae610SVladimir Oltean 26445899ee36SVladimir Oltean list_for_each_entry(c, &priv->dsa_8021q_ctx->crosschip_links, list) { 2645ec5ae610SVladimir Oltean bool already_added = false; 2646ec5ae610SVladimir Oltean 2647ec5ae610SVladimir Oltean list_for_each_entry(s, &crosschip_switches, list) { 26485899ee36SVladimir Oltean if (s->other_ctx == c->other_ctx) { 2649ec5ae610SVladimir Oltean already_added = true; 2650ec5ae610SVladimir Oltean break; 2651ec5ae610SVladimir Oltean } 2652ec5ae610SVladimir Oltean } 2653ec5ae610SVladimir Oltean 2654ec5ae610SVladimir Oltean if (already_added) 2655ec5ae610SVladimir Oltean continue; 2656ec5ae610SVladimir Oltean 2657ec5ae610SVladimir Oltean s = kzalloc(sizeof(*s), GFP_KERNEL); 2658ec5ae610SVladimir Oltean if (!s) { 2659ec5ae610SVladimir Oltean dev_err(priv->ds->dev, "Failed to allocate memory\n"); 2660ec5ae610SVladimir Oltean rc = -ENOMEM; 2661ec5ae610SVladimir Oltean goto out; 2662ec5ae610SVladimir Oltean } 26635899ee36SVladimir Oltean s->other_ctx = c->other_ctx; 2664ec5ae610SVladimir Oltean list_add(&s->list, &crosschip_switches); 2665ec5ae610SVladimir Oltean } 2666ec5ae610SVladimir Oltean 2667ec5ae610SVladimir Oltean list_for_each_entry(s, &crosschip_switches, list) { 26685899ee36SVladimir Oltean struct sja1105_private *other_priv = s->other_ctx->ds->priv; 2669ec5ae610SVladimir Oltean 2670ec5ae610SVladimir Oltean rc = sja1105_build_vlan_table(other_priv, false); 2671ec5ae610SVladimir Oltean if (rc) 2672ec5ae610SVladimir Oltean goto out; 2673ec5ae610SVladimir Oltean } 2674ec5ae610SVladimir Oltean 2675ec5ae610SVladimir Oltean out: 2676ec5ae610SVladimir Oltean list_for_each_entry_safe(s, pos, &crosschip_switches, list) { 2677ec5ae610SVladimir Oltean list_del(&s->list); 2678ec5ae610SVladimir Oltean kfree(s); 2679ec5ae610SVladimir Oltean } 2680ec5ae610SVladimir Oltean 2681ec5ae610SVladimir Oltean return rc; 2682ec5ae610SVladimir Oltean } 2683ec5ae610SVladimir Oltean 2684ec5ae610SVladimir Oltean static int sja1105_build_vlan_table(struct sja1105_private *priv, bool notify) 2685ec5ae610SVladimir Oltean { 268682760d7fSVladimir Oltean u16 subvlan_map[SJA1105_MAX_NUM_PORTS][DSA_8021Q_N_SUBVLAN]; 26873f01c91aSVladimir Oltean struct sja1105_retagging_entry *new_retagging; 2688ec5ae610SVladimir Oltean struct sja1105_vlan_lookup_entry *new_vlan; 2689ec5ae610SVladimir Oltean struct sja1105_table *table; 26903f01c91aSVladimir Oltean int i, num_retagging = 0; 2691ec5ae610SVladimir Oltean int rc; 2692ec5ae610SVladimir Oltean 2693ec5ae610SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP]; 2694ec5ae610SVladimir Oltean new_vlan = kcalloc(VLAN_N_VID, 2695ec5ae610SVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 2696ec5ae610SVladimir Oltean if (!new_vlan) 2697ec5ae610SVladimir Oltean return -ENOMEM; 2698ec5ae610SVladimir Oltean 26993f01c91aSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP]; 27003f01c91aSVladimir Oltean new_retagging = kcalloc(SJA1105_MAX_RETAGGING_COUNT, 27013f01c91aSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 27023f01c91aSVladimir Oltean if (!new_retagging) { 27033f01c91aSVladimir Oltean kfree(new_vlan); 27043f01c91aSVladimir Oltean return -ENOMEM; 27053f01c91aSVladimir Oltean } 27063f01c91aSVladimir Oltean 2707ec5ae610SVladimir Oltean for (i = 0; i < VLAN_N_VID; i++) 2708ec5ae610SVladimir Oltean new_vlan[i].vlanid = VLAN_N_VID; 2709ec5ae610SVladimir Oltean 27103f01c91aSVladimir Oltean for (i = 0; i < SJA1105_MAX_RETAGGING_COUNT; i++) 27113f01c91aSVladimir Oltean new_retagging[i].vlan_ing = VLAN_N_VID; 27123f01c91aSVladimir Oltean 27133f01c91aSVladimir Oltean for (i = 0; i < priv->ds->num_ports; i++) 27143f01c91aSVladimir Oltean sja1105_init_subvlan_map(subvlan_map[i]); 27153f01c91aSVladimir Oltean 2716ec5ae610SVladimir Oltean /* Bridge VLANs */ 2717ec5ae610SVladimir Oltean rc = sja1105_build_bridge_vlans(priv, new_vlan); 2718ec5ae610SVladimir Oltean if (rc) 2719ec5ae610SVladimir Oltean goto out; 2720ec5ae610SVladimir Oltean 2721ec5ae610SVladimir Oltean /* VLANs necessary for dsa_8021q operation, given to us by tag_8021q.c: 2722ec5ae610SVladimir Oltean * - RX VLANs 2723ec5ae610SVladimir Oltean * - TX VLANs 2724ec5ae610SVladimir Oltean * - Crosschip links 2725ec5ae610SVladimir Oltean */ 2726ec5ae610SVladimir Oltean rc = sja1105_build_dsa_8021q_vlans(priv, new_vlan); 2727ec5ae610SVladimir Oltean if (rc) 2728ec5ae610SVladimir Oltean goto out; 2729ec5ae610SVladimir Oltean 27303f01c91aSVladimir Oltean /* Private VLANs necessary for dsa_8021q operation, which we need to 27313f01c91aSVladimir Oltean * determine on our own: 27323f01c91aSVladimir Oltean * - Sub-VLANs 27333f01c91aSVladimir Oltean * - Sub-VLANs of crosschip switches 27343f01c91aSVladimir Oltean */ 27353f01c91aSVladimir Oltean rc = sja1105_build_subvlans(priv, subvlan_map, new_vlan, new_retagging, 27363f01c91aSVladimir Oltean &num_retagging); 27373f01c91aSVladimir Oltean if (rc) 27383f01c91aSVladimir Oltean goto out; 27393f01c91aSVladimir Oltean 27403f01c91aSVladimir Oltean rc = sja1105_build_crosschip_subvlans(priv, new_vlan, new_retagging, 27413f01c91aSVladimir Oltean &num_retagging); 27423f01c91aSVladimir Oltean if (rc) 27433f01c91aSVladimir Oltean goto out; 27443f01c91aSVladimir Oltean 27453f01c91aSVladimir Oltean rc = sja1105_commit_vlans(priv, new_vlan, new_retagging, num_retagging); 2746ec5ae610SVladimir Oltean if (rc) 2747ec5ae610SVladimir Oltean goto out; 2748ec5ae610SVladimir Oltean 2749ec5ae610SVladimir Oltean rc = sja1105_commit_pvid(priv); 2750ec5ae610SVladimir Oltean if (rc) 2751ec5ae610SVladimir Oltean goto out; 2752ec5ae610SVladimir Oltean 27533f01c91aSVladimir Oltean for (i = 0; i < priv->ds->num_ports; i++) 27543f01c91aSVladimir Oltean sja1105_commit_subvlan_map(priv, i, subvlan_map[i]); 27553f01c91aSVladimir Oltean 2756ec5ae610SVladimir Oltean if (notify) { 2757ec5ae610SVladimir Oltean rc = sja1105_notify_crosschip_switches(priv); 2758ec5ae610SVladimir Oltean if (rc) 2759ec5ae610SVladimir Oltean goto out; 2760ec5ae610SVladimir Oltean } 2761ec5ae610SVladimir Oltean 2762ec5ae610SVladimir Oltean out: 2763ec5ae610SVladimir Oltean kfree(new_vlan); 27643f01c91aSVladimir Oltean kfree(new_retagging); 2765ec5ae610SVladimir Oltean 2766ec5ae610SVladimir Oltean return rc; 2767ec5ae610SVladimir Oltean } 2768ec5ae610SVladimir Oltean 2769070ca3bbSVladimir Oltean /* The TPID setting belongs to the General Parameters table, 2770070ca3bbSVladimir Oltean * which can only be partially reconfigured at runtime (and not the TPID). 2771070ca3bbSVladimir Oltean * So a switch reset is required. 2772070ca3bbSVladimir Oltean */ 277389153ed6SVladimir Oltean int sja1105_vlan_filtering(struct dsa_switch *ds, int port, bool enabled, 277489153ed6SVladimir Oltean struct netlink_ext_ack *extack) 27756666cebcSVladimir Oltean { 27766d7c7d94SVladimir Oltean struct sja1105_l2_lookup_params_entry *l2_lookup_params; 2777070ca3bbSVladimir Oltean struct sja1105_general_params_entry *general_params; 27786666cebcSVladimir Oltean struct sja1105_private *priv = ds->priv; 27797f14937fSVladimir Oltean enum sja1105_vlan_state state; 2780070ca3bbSVladimir Oltean struct sja1105_table *table; 2781dfacc5a2SVladimir Oltean struct sja1105_rule *rule; 27822cafa72eSVladimir Oltean bool want_tagging; 2783070ca3bbSVladimir Oltean u16 tpid, tpid2; 27846666cebcSVladimir Oltean int rc; 27856666cebcSVladimir Oltean 2786dfacc5a2SVladimir Oltean list_for_each_entry(rule, &priv->flow_block.rules, list) { 2787dfacc5a2SVladimir Oltean if (rule->type == SJA1105_RULE_VL) { 278889153ed6SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 278989153ed6SVladimir Oltean "Cannot change VLAN filtering with active VL rules"); 2790dfacc5a2SVladimir Oltean return -EBUSY; 2791dfacc5a2SVladimir Oltean } 2792dfacc5a2SVladimir Oltean } 2793dfacc5a2SVladimir Oltean 2794070ca3bbSVladimir Oltean if (enabled) { 27956666cebcSVladimir Oltean /* Enable VLAN filtering. */ 279654fa49eeSVladimir Oltean tpid = ETH_P_8021Q; 279754fa49eeSVladimir Oltean tpid2 = ETH_P_8021AD; 2798070ca3bbSVladimir Oltean } else { 27996666cebcSVladimir Oltean /* Disable VLAN filtering. */ 2800070ca3bbSVladimir Oltean tpid = ETH_P_SJA1105; 2801070ca3bbSVladimir Oltean tpid2 = ETH_P_SJA1105; 2802070ca3bbSVladimir Oltean } 2803070ca3bbSVladimir Oltean 280438b5beeaSVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 280538b5beeaSVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 280638b5beeaSVladimir Oltean 280738b5beeaSVladimir Oltean if (enabled) 280838b5beeaSVladimir Oltean sp->xmit_tpid = priv->info->qinq_tpid; 280938b5beeaSVladimir Oltean else 281038b5beeaSVladimir Oltean sp->xmit_tpid = ETH_P_SJA1105; 281138b5beeaSVladimir Oltean } 281238b5beeaSVladimir Oltean 28137f14937fSVladimir Oltean if (!enabled) 28147f14937fSVladimir Oltean state = SJA1105_VLAN_UNAWARE; 28152cafa72eSVladimir Oltean else if (priv->best_effort_vlan_filtering) 28162cafa72eSVladimir Oltean state = SJA1105_VLAN_BEST_EFFORT; 28177f14937fSVladimir Oltean else 28187f14937fSVladimir Oltean state = SJA1105_VLAN_FILTERING_FULL; 28197f14937fSVladimir Oltean 2820cfa36b1fSVladimir Oltean if (priv->vlan_state == state) 2821cfa36b1fSVladimir Oltean return 0; 2822cfa36b1fSVladimir Oltean 28237f14937fSVladimir Oltean priv->vlan_state = state; 28242cafa72eSVladimir Oltean want_tagging = (state == SJA1105_VLAN_UNAWARE || 28252cafa72eSVladimir Oltean state == SJA1105_VLAN_BEST_EFFORT); 28267f14937fSVladimir Oltean 2827070ca3bbSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; 2828070ca3bbSVladimir Oltean general_params = table->entries; 2829f9a1a764SVladimir Oltean /* EtherType used to identify inner tagged (C-tag) VLAN traffic */ 283054fa49eeSVladimir Oltean general_params->tpid = tpid; 283154fa49eeSVladimir Oltean /* EtherType used to identify outer tagged (S-tag) VLAN traffic */ 2832070ca3bbSVladimir Oltean general_params->tpid2 = tpid2; 283342824463SVladimir Oltean /* When VLAN filtering is on, we need to at least be able to 283442824463SVladimir Oltean * decode management traffic through the "backup plan". 283542824463SVladimir Oltean */ 283642824463SVladimir Oltean general_params->incl_srcpt1 = enabled; 283742824463SVladimir Oltean general_params->incl_srcpt0 = enabled; 2838070ca3bbSVladimir Oltean 28392cafa72eSVladimir Oltean want_tagging = priv->best_effort_vlan_filtering || !enabled; 28402cafa72eSVladimir Oltean 28416d7c7d94SVladimir Oltean /* VLAN filtering => independent VLAN learning. 28422cafa72eSVladimir Oltean * No VLAN filtering (or best effort) => shared VLAN learning. 28436d7c7d94SVladimir Oltean * 28446d7c7d94SVladimir Oltean * In shared VLAN learning mode, untagged traffic still gets 28456d7c7d94SVladimir Oltean * pvid-tagged, and the FDB table gets populated with entries 28466d7c7d94SVladimir Oltean * containing the "real" (pvid or from VLAN tag) VLAN ID. 28476d7c7d94SVladimir Oltean * However the switch performs a masked L2 lookup in the FDB, 28486d7c7d94SVladimir Oltean * effectively only looking up a frame's DMAC (and not VID) for the 28496d7c7d94SVladimir Oltean * forwarding decision. 28506d7c7d94SVladimir Oltean * 28516d7c7d94SVladimir Oltean * This is extremely convenient for us, because in modes with 28526d7c7d94SVladimir Oltean * vlan_filtering=0, dsa_8021q actually installs unique pvid's into 28536d7c7d94SVladimir Oltean * each front panel port. This is good for identification but breaks 28546d7c7d94SVladimir Oltean * learning badly - the VID of the learnt FDB entry is unique, aka 28556d7c7d94SVladimir Oltean * no frames coming from any other port are going to have it. So 28566d7c7d94SVladimir Oltean * for forwarding purposes, this is as though learning was broken 28576d7c7d94SVladimir Oltean * (all frames get flooded). 28586d7c7d94SVladimir Oltean */ 28596d7c7d94SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS]; 28606d7c7d94SVladimir Oltean l2_lookup_params = table->entries; 28612cafa72eSVladimir Oltean l2_lookup_params->shared_learn = want_tagging; 28626d7c7d94SVladimir Oltean 2863aaa270c6SVladimir Oltean sja1105_frame_memory_partitioning(priv); 2864aaa270c6SVladimir Oltean 2865aef31718SVladimir Oltean rc = sja1105_build_vlan_table(priv, false); 2866aef31718SVladimir Oltean if (rc) 2867aef31718SVladimir Oltean return rc; 2868aef31718SVladimir Oltean 28692eea1fa8SVladimir Oltean rc = sja1105_static_config_reload(priv, SJA1105_VLAN_FILTERING); 28706666cebcSVladimir Oltean if (rc) 287189153ed6SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, "Failed to change VLAN Ethertype"); 28726666cebcSVladimir Oltean 2873227d07a0SVladimir Oltean /* Switch port identification based on 802.1Q is only passable 2874227d07a0SVladimir Oltean * if we are not under a vlan_filtering bridge. So make sure 28752cafa72eSVladimir Oltean * the two configurations are mutually exclusive (of course, the 28762cafa72eSVladimir Oltean * user may know better, i.e. best_effort_vlan_filtering). 2877227d07a0SVladimir Oltean */ 28782cafa72eSVladimir Oltean return sja1105_setup_8021q_tagging(ds, want_tagging); 28796666cebcSVladimir Oltean } 28806666cebcSVladimir Oltean 28815899ee36SVladimir Oltean /* Returns number of VLANs added (0 or 1) on success, 28825899ee36SVladimir Oltean * or a negative error code. 28835899ee36SVladimir Oltean */ 28845899ee36SVladimir Oltean static int sja1105_vlan_add_one(struct dsa_switch *ds, int port, u16 vid, 28855899ee36SVladimir Oltean u16 flags, struct list_head *vlan_list) 28865899ee36SVladimir Oltean { 28875899ee36SVladimir Oltean bool untagged = flags & BRIDGE_VLAN_INFO_UNTAGGED; 28885899ee36SVladimir Oltean bool pvid = flags & BRIDGE_VLAN_INFO_PVID; 28895899ee36SVladimir Oltean struct sja1105_bridge_vlan *v; 28905899ee36SVladimir Oltean 2891b38e659dSVladimir Oltean list_for_each_entry(v, vlan_list, list) { 2892b38e659dSVladimir Oltean if (v->port == port && v->vid == vid) { 28935899ee36SVladimir Oltean /* Already added */ 2894b38e659dSVladimir Oltean if (v->untagged == untagged && v->pvid == pvid) 2895b38e659dSVladimir Oltean /* Nothing changed */ 28965899ee36SVladimir Oltean return 0; 28975899ee36SVladimir Oltean 2898b38e659dSVladimir Oltean /* It's the same VLAN, but some of the flags changed 2899b38e659dSVladimir Oltean * and the user did not bother to delete it first. 2900b38e659dSVladimir Oltean * Update it and trigger sja1105_build_vlan_table. 2901b38e659dSVladimir Oltean */ 2902b38e659dSVladimir Oltean v->untagged = untagged; 2903b38e659dSVladimir Oltean v->pvid = pvid; 2904b38e659dSVladimir Oltean return 1; 2905b38e659dSVladimir Oltean } 2906b38e659dSVladimir Oltean } 2907b38e659dSVladimir Oltean 29085899ee36SVladimir Oltean v = kzalloc(sizeof(*v), GFP_KERNEL); 29095899ee36SVladimir Oltean if (!v) { 29105899ee36SVladimir Oltean dev_err(ds->dev, "Out of memory while storing VLAN\n"); 29115899ee36SVladimir Oltean return -ENOMEM; 29125899ee36SVladimir Oltean } 29135899ee36SVladimir Oltean 29145899ee36SVladimir Oltean v->port = port; 29155899ee36SVladimir Oltean v->vid = vid; 29165899ee36SVladimir Oltean v->untagged = untagged; 29175899ee36SVladimir Oltean v->pvid = pvid; 29185899ee36SVladimir Oltean list_add(&v->list, vlan_list); 29195899ee36SVladimir Oltean 29205899ee36SVladimir Oltean return 1; 29215899ee36SVladimir Oltean } 29225899ee36SVladimir Oltean 29235899ee36SVladimir Oltean /* Returns number of VLANs deleted (0 or 1) */ 29245899ee36SVladimir Oltean static int sja1105_vlan_del_one(struct dsa_switch *ds, int port, u16 vid, 29255899ee36SVladimir Oltean struct list_head *vlan_list) 29265899ee36SVladimir Oltean { 29275899ee36SVladimir Oltean struct sja1105_bridge_vlan *v, *n; 29285899ee36SVladimir Oltean 29295899ee36SVladimir Oltean list_for_each_entry_safe(v, n, vlan_list, list) { 29305899ee36SVladimir Oltean if (v->port == port && v->vid == vid) { 29315899ee36SVladimir Oltean list_del(&v->list); 29325899ee36SVladimir Oltean kfree(v); 29335899ee36SVladimir Oltean return 1; 29345899ee36SVladimir Oltean } 29355899ee36SVladimir Oltean } 29365899ee36SVladimir Oltean 29375899ee36SVladimir Oltean return 0; 29385899ee36SVladimir Oltean } 29395899ee36SVladimir Oltean 29401958d581SVladimir Oltean static int sja1105_vlan_add(struct dsa_switch *ds, int port, 294131046a5fSVladimir Oltean const struct switchdev_obj_port_vlan *vlan, 294231046a5fSVladimir Oltean struct netlink_ext_ack *extack) 29436666cebcSVladimir Oltean { 29446666cebcSVladimir Oltean struct sja1105_private *priv = ds->priv; 2945ec5ae610SVladimir Oltean bool vlan_table_changed = false; 29466666cebcSVladimir Oltean int rc; 29476666cebcSVladimir Oltean 29481958d581SVladimir Oltean /* If the user wants best-effort VLAN filtering (aka vlan_filtering 29491958d581SVladimir Oltean * bridge plus tagging), be sure to at least deny alterations to the 29501958d581SVladimir Oltean * configuration done by dsa_8021q. 29511958d581SVladimir Oltean */ 29521958d581SVladimir Oltean if (priv->vlan_state != SJA1105_VLAN_FILTERING_FULL && 29531958d581SVladimir Oltean vid_is_dsa_8021q(vlan->vid)) { 295431046a5fSVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 295531046a5fSVladimir Oltean "Range 1024-3071 reserved for dsa_8021q operation"); 29561958d581SVladimir Oltean return -EBUSY; 29571958d581SVladimir Oltean } 29581958d581SVladimir Oltean 2959b7a9e0daSVladimir Oltean rc = sja1105_vlan_add_one(ds, port, vlan->vid, vlan->flags, 29605899ee36SVladimir Oltean &priv->bridge_vlans); 29615899ee36SVladimir Oltean if (rc < 0) 29621958d581SVladimir Oltean return rc; 29635899ee36SVladimir Oltean if (rc > 0) 2964ec5ae610SVladimir Oltean vlan_table_changed = true; 2965ec5ae610SVladimir Oltean 2966ec5ae610SVladimir Oltean if (!vlan_table_changed) 29671958d581SVladimir Oltean return 0; 2968ec5ae610SVladimir Oltean 29691958d581SVladimir Oltean return sja1105_build_vlan_table(priv, true); 29706666cebcSVladimir Oltean } 29716666cebcSVladimir Oltean 29726666cebcSVladimir Oltean static int sja1105_vlan_del(struct dsa_switch *ds, int port, 29736666cebcSVladimir Oltean const struct switchdev_obj_port_vlan *vlan) 29746666cebcSVladimir Oltean { 29756666cebcSVladimir Oltean struct sja1105_private *priv = ds->priv; 2976ec5ae610SVladimir Oltean bool vlan_table_changed = false; 29775899ee36SVladimir Oltean int rc; 29786666cebcSVladimir Oltean 2979b7a9e0daSVladimir Oltean rc = sja1105_vlan_del_one(ds, port, vlan->vid, &priv->bridge_vlans); 29805899ee36SVladimir Oltean if (rc > 0) 2981ec5ae610SVladimir Oltean vlan_table_changed = true; 2982ec5ae610SVladimir Oltean 2983ec5ae610SVladimir Oltean if (!vlan_table_changed) 29846666cebcSVladimir Oltean return 0; 2985ec5ae610SVladimir Oltean 2986ec5ae610SVladimir Oltean return sja1105_build_vlan_table(priv, true); 29876666cebcSVladimir Oltean } 29886666cebcSVladimir Oltean 29895899ee36SVladimir Oltean static int sja1105_dsa_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid, 29905899ee36SVladimir Oltean u16 flags) 29915899ee36SVladimir Oltean { 29925899ee36SVladimir Oltean struct sja1105_private *priv = ds->priv; 29935899ee36SVladimir Oltean int rc; 29945899ee36SVladimir Oltean 29955899ee36SVladimir Oltean rc = sja1105_vlan_add_one(ds, port, vid, flags, &priv->dsa_8021q_vlans); 29965899ee36SVladimir Oltean if (rc <= 0) 29975899ee36SVladimir Oltean return rc; 29985899ee36SVladimir Oltean 29995899ee36SVladimir Oltean return sja1105_build_vlan_table(priv, true); 30005899ee36SVladimir Oltean } 30015899ee36SVladimir Oltean 30025899ee36SVladimir Oltean static int sja1105_dsa_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid) 30035899ee36SVladimir Oltean { 30045899ee36SVladimir Oltean struct sja1105_private *priv = ds->priv; 30055899ee36SVladimir Oltean int rc; 30065899ee36SVladimir Oltean 30075899ee36SVladimir Oltean rc = sja1105_vlan_del_one(ds, port, vid, &priv->dsa_8021q_vlans); 30085899ee36SVladimir Oltean if (!rc) 30095899ee36SVladimir Oltean return 0; 30105899ee36SVladimir Oltean 30115899ee36SVladimir Oltean return sja1105_build_vlan_table(priv, true); 30125899ee36SVladimir Oltean } 30135899ee36SVladimir Oltean 30145899ee36SVladimir Oltean static const struct dsa_8021q_ops sja1105_dsa_8021q_ops = { 30155899ee36SVladimir Oltean .vlan_add = sja1105_dsa_8021q_vlan_add, 30165899ee36SVladimir Oltean .vlan_del = sja1105_dsa_8021q_vlan_del, 30175899ee36SVladimir Oltean }; 30185899ee36SVladimir Oltean 30198aa9ebccSVladimir Oltean /* The programming model for the SJA1105 switch is "all-at-once" via static 30208aa9ebccSVladimir Oltean * configuration tables. Some of these can be dynamically modified at runtime, 30218aa9ebccSVladimir Oltean * but not the xMII mode parameters table. 30228aa9ebccSVladimir Oltean * Furthermode, some PHYs may not have crystals for generating their clocks 30238aa9ebccSVladimir Oltean * (e.g. RMII). Instead, their 50MHz clock is supplied via the SJA1105 port's 30248aa9ebccSVladimir Oltean * ref_clk pin. So port clocking needs to be initialized early, before 30258aa9ebccSVladimir Oltean * connecting to PHYs is attempted, otherwise they won't respond through MDIO. 30268aa9ebccSVladimir Oltean * Setting correct PHY link speed does not matter now. 30278aa9ebccSVladimir Oltean * But dsa_slave_phy_setup is called later than sja1105_setup, so the PHY 30288aa9ebccSVladimir Oltean * bindings are not yet parsed by DSA core. We need to parse early so that we 30298aa9ebccSVladimir Oltean * can populate the xMII mode parameters table. 30308aa9ebccSVladimir Oltean */ 30318aa9ebccSVladimir Oltean static int sja1105_setup(struct dsa_switch *ds) 30328aa9ebccSVladimir Oltean { 30338aa9ebccSVladimir Oltean struct sja1105_private *priv = ds->priv; 30348aa9ebccSVladimir Oltean int rc; 30358aa9ebccSVladimir Oltean 30365d645df9SVladimir Oltean rc = sja1105_parse_dt(priv); 30378aa9ebccSVladimir Oltean if (rc < 0) { 30388aa9ebccSVladimir Oltean dev_err(ds->dev, "Failed to parse DT: %d\n", rc); 30398aa9ebccSVladimir Oltean return rc; 30408aa9ebccSVladimir Oltean } 3041f5b8631cSVladimir Oltean 3042f5b8631cSVladimir Oltean /* Error out early if internal delays are required through DT 3043f5b8631cSVladimir Oltean * and we can't apply them. 3044f5b8631cSVladimir Oltean */ 304529afb83aSVladimir Oltean rc = sja1105_parse_rgmii_delays(priv); 3046f5b8631cSVladimir Oltean if (rc < 0) { 3047f5b8631cSVladimir Oltean dev_err(ds->dev, "RGMII delay not supported\n"); 3048f5b8631cSVladimir Oltean return rc; 3049f5b8631cSVladimir Oltean } 3050f5b8631cSVladimir Oltean 305161c77126SVladimir Oltean rc = sja1105_ptp_clock_register(ds); 3052bb77f36aSVladimir Oltean if (rc < 0) { 3053bb77f36aSVladimir Oltean dev_err(ds->dev, "Failed to register PTP clock: %d\n", rc); 3054bb77f36aSVladimir Oltean return rc; 3055bb77f36aSVladimir Oltean } 30568aa9ebccSVladimir Oltean /* Create and send configuration down to device */ 30575d645df9SVladimir Oltean rc = sja1105_static_config_load(priv); 30588aa9ebccSVladimir Oltean if (rc < 0) { 30598aa9ebccSVladimir Oltean dev_err(ds->dev, "Failed to load static config: %d\n", rc); 3060cec279a8SVladimir Oltean goto out_ptp_clock_unregister; 30618aa9ebccSVladimir Oltean } 30628aa9ebccSVladimir Oltean /* Configure the CGU (PHY link modes and speeds) */ 3063c5037678SVladimir Oltean rc = priv->info->clocking_setup(priv); 30648aa9ebccSVladimir Oltean if (rc < 0) { 30658aa9ebccSVladimir Oltean dev_err(ds->dev, "Failed to configure MII clocking: %d\n", rc); 3066cec279a8SVladimir Oltean goto out_static_config_free; 30678aa9ebccSVladimir Oltean } 30686666cebcSVladimir Oltean /* On SJA1105, VLAN filtering per se is always enabled in hardware. 30696666cebcSVladimir Oltean * The only thing we can do to disable it is lie about what the 802.1Q 30706666cebcSVladimir Oltean * EtherType is. 30716666cebcSVladimir Oltean * So it will still try to apply VLAN filtering, but all ingress 30726666cebcSVladimir Oltean * traffic (except frames received with EtherType of ETH_P_SJA1105) 30736666cebcSVladimir Oltean * will be internally tagged with a distorted VLAN header where the 30746666cebcSVladimir Oltean * TPID is ETH_P_SJA1105, and the VLAN ID is the port pvid. 30756666cebcSVladimir Oltean */ 30766666cebcSVladimir Oltean ds->vlan_filtering_is_global = true; 30778aa9ebccSVladimir Oltean 30785f06c63bSVladimir Oltean /* Advertise the 8 egress queues */ 30795f06c63bSVladimir Oltean ds->num_tx_queues = SJA1105_NUM_TC; 30805f06c63bSVladimir Oltean 3081c279c726SVladimir Oltean ds->mtu_enforcement_ingress = true; 3082c279c726SVladimir Oltean 30838841f6e6SVladimir Oltean priv->best_effort_vlan_filtering = true; 30848841f6e6SVladimir Oltean 30850a7bdbc2SVladimir Oltean rc = sja1105_devlink_setup(ds); 30862cafa72eSVladimir Oltean if (rc < 0) 3087cec279a8SVladimir Oltean goto out_static_config_free; 30882cafa72eSVladimir Oltean 3089227d07a0SVladimir Oltean /* The DSA/switchdev model brings up switch ports in standalone mode by 3090227d07a0SVladimir Oltean * default, and that means vlan_filtering is 0 since they're not under 3091227d07a0SVladimir Oltean * a bridge, so it's safe to set up switch tagging at this time. 3092227d07a0SVladimir Oltean */ 3093bbed0bbdSVladimir Oltean rtnl_lock(); 3094bbed0bbdSVladimir Oltean rc = sja1105_setup_8021q_tagging(ds, true); 3095bbed0bbdSVladimir Oltean rtnl_unlock(); 3096cec279a8SVladimir Oltean if (rc) 3097cec279a8SVladimir Oltean goto out_devlink_teardown; 3098cec279a8SVladimir Oltean 3099cec279a8SVladimir Oltean return 0; 3100cec279a8SVladimir Oltean 3101cec279a8SVladimir Oltean out_devlink_teardown: 3102cec279a8SVladimir Oltean sja1105_devlink_teardown(ds); 3103cec279a8SVladimir Oltean out_ptp_clock_unregister: 3104cec279a8SVladimir Oltean sja1105_ptp_clock_unregister(ds); 3105cec279a8SVladimir Oltean out_static_config_free: 3106cec279a8SVladimir Oltean sja1105_static_config_free(&priv->static_config); 3107bbed0bbdSVladimir Oltean 3108bbed0bbdSVladimir Oltean return rc; 3109227d07a0SVladimir Oltean } 3110227d07a0SVladimir Oltean 3111f3097be2SVladimir Oltean static void sja1105_teardown(struct dsa_switch *ds) 3112f3097be2SVladimir Oltean { 3113f3097be2SVladimir Oltean struct sja1105_private *priv = ds->priv; 3114ec5ae610SVladimir Oltean struct sja1105_bridge_vlan *v, *n; 3115a68578c2SVladimir Oltean int port; 3116a68578c2SVladimir Oltean 3117542043e9SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 3118a68578c2SVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 3119a68578c2SVladimir Oltean 3120a68578c2SVladimir Oltean if (!dsa_is_user_port(ds, port)) 3121a68578c2SVladimir Oltean continue; 3122a68578c2SVladimir Oltean 312352c0d4e3SVladimir Oltean if (sp->xmit_worker) 3124a68578c2SVladimir Oltean kthread_destroy_worker(sp->xmit_worker); 3125a68578c2SVladimir Oltean } 3126f3097be2SVladimir Oltean 31270a7bdbc2SVladimir Oltean sja1105_devlink_teardown(ds); 3128a6af7763SVladimir Oltean sja1105_flower_teardown(ds); 3129317ab5b8SVladimir Oltean sja1105_tas_teardown(ds); 313061c77126SVladimir Oltean sja1105_ptp_clock_unregister(ds); 31316cb0abbdSVladimir Oltean sja1105_static_config_free(&priv->static_config); 3132ec5ae610SVladimir Oltean 3133ec5ae610SVladimir Oltean list_for_each_entry_safe(v, n, &priv->dsa_8021q_vlans, list) { 3134ec5ae610SVladimir Oltean list_del(&v->list); 3135ec5ae610SVladimir Oltean kfree(v); 3136ec5ae610SVladimir Oltean } 3137ec5ae610SVladimir Oltean 3138ec5ae610SVladimir Oltean list_for_each_entry_safe(v, n, &priv->bridge_vlans, list) { 3139ec5ae610SVladimir Oltean list_del(&v->list); 3140ec5ae610SVladimir Oltean kfree(v); 3141ec5ae610SVladimir Oltean } 3142f3097be2SVladimir Oltean } 3143f3097be2SVladimir Oltean 3144a68578c2SVladimir Oltean static void sja1105_port_disable(struct dsa_switch *ds, int port) 3145a68578c2SVladimir Oltean { 3146a68578c2SVladimir Oltean struct sja1105_private *priv = ds->priv; 3147a68578c2SVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 3148a68578c2SVladimir Oltean 3149a68578c2SVladimir Oltean if (!dsa_is_user_port(ds, port)) 3150a68578c2SVladimir Oltean return; 3151a68578c2SVladimir Oltean 3152a68578c2SVladimir Oltean kthread_cancel_work_sync(&sp->xmit_work); 3153a68578c2SVladimir Oltean skb_queue_purge(&sp->xmit_queue); 3154a68578c2SVladimir Oltean } 3155a68578c2SVladimir Oltean 3156227d07a0SVladimir Oltean static int sja1105_mgmt_xmit(struct dsa_switch *ds, int port, int slot, 315747ed985eSVladimir Oltean struct sk_buff *skb, bool takets) 3158227d07a0SVladimir Oltean { 3159227d07a0SVladimir Oltean struct sja1105_mgmt_entry mgmt_route = {0}; 3160227d07a0SVladimir Oltean struct sja1105_private *priv = ds->priv; 3161227d07a0SVladimir Oltean struct ethhdr *hdr; 3162227d07a0SVladimir Oltean int timeout = 10; 3163227d07a0SVladimir Oltean int rc; 3164227d07a0SVladimir Oltean 3165227d07a0SVladimir Oltean hdr = eth_hdr(skb); 3166227d07a0SVladimir Oltean 3167227d07a0SVladimir Oltean mgmt_route.macaddr = ether_addr_to_u64(hdr->h_dest); 3168227d07a0SVladimir Oltean mgmt_route.destports = BIT(port); 3169227d07a0SVladimir Oltean mgmt_route.enfport = 1; 317047ed985eSVladimir Oltean mgmt_route.tsreg = 0; 317147ed985eSVladimir Oltean mgmt_route.takets = takets; 3172227d07a0SVladimir Oltean 3173227d07a0SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE, 3174227d07a0SVladimir Oltean slot, &mgmt_route, true); 3175227d07a0SVladimir Oltean if (rc < 0) { 3176227d07a0SVladimir Oltean kfree_skb(skb); 3177227d07a0SVladimir Oltean return rc; 3178227d07a0SVladimir Oltean } 3179227d07a0SVladimir Oltean 3180227d07a0SVladimir Oltean /* Transfer skb to the host port. */ 318168bb8ea8SVivien Didelot dsa_enqueue_skb(skb, dsa_to_port(ds, port)->slave); 3182227d07a0SVladimir Oltean 3183227d07a0SVladimir Oltean /* Wait until the switch has processed the frame */ 3184227d07a0SVladimir Oltean do { 3185227d07a0SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_MGMT_ROUTE, 3186227d07a0SVladimir Oltean slot, &mgmt_route); 3187227d07a0SVladimir Oltean if (rc < 0) { 3188227d07a0SVladimir Oltean dev_err_ratelimited(priv->ds->dev, 3189227d07a0SVladimir Oltean "failed to poll for mgmt route\n"); 3190227d07a0SVladimir Oltean continue; 3191227d07a0SVladimir Oltean } 3192227d07a0SVladimir Oltean 3193227d07a0SVladimir Oltean /* UM10944: The ENFPORT flag of the respective entry is 3194227d07a0SVladimir Oltean * cleared when a match is found. The host can use this 3195227d07a0SVladimir Oltean * flag as an acknowledgment. 3196227d07a0SVladimir Oltean */ 3197227d07a0SVladimir Oltean cpu_relax(); 3198227d07a0SVladimir Oltean } while (mgmt_route.enfport && --timeout); 3199227d07a0SVladimir Oltean 3200227d07a0SVladimir Oltean if (!timeout) { 3201227d07a0SVladimir Oltean /* Clean up the management route so that a follow-up 3202227d07a0SVladimir Oltean * frame may not match on it by mistake. 32032a7e7409SVladimir Oltean * This is only hardware supported on P/Q/R/S - on E/T it is 32042a7e7409SVladimir Oltean * a no-op and we are silently discarding the -EOPNOTSUPP. 3205227d07a0SVladimir Oltean */ 3206227d07a0SVladimir Oltean sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE, 3207227d07a0SVladimir Oltean slot, &mgmt_route, false); 3208227d07a0SVladimir Oltean dev_err_ratelimited(priv->ds->dev, "xmit timed out\n"); 3209227d07a0SVladimir Oltean } 3210227d07a0SVladimir Oltean 3211227d07a0SVladimir Oltean return NETDEV_TX_OK; 3212227d07a0SVladimir Oltean } 3213227d07a0SVladimir Oltean 3214a68578c2SVladimir Oltean #define work_to_port(work) \ 3215a68578c2SVladimir Oltean container_of((work), struct sja1105_port, xmit_work) 3216a68578c2SVladimir Oltean #define tagger_to_sja1105(t) \ 3217a68578c2SVladimir Oltean container_of((t), struct sja1105_private, tagger_data) 3218a68578c2SVladimir Oltean 3219227d07a0SVladimir Oltean /* Deferred work is unfortunately necessary because setting up the management 3220227d07a0SVladimir Oltean * route cannot be done from atomit context (SPI transfer takes a sleepable 3221227d07a0SVladimir Oltean * lock on the bus) 3222227d07a0SVladimir Oltean */ 3223a68578c2SVladimir Oltean static void sja1105_port_deferred_xmit(struct kthread_work *work) 3224227d07a0SVladimir Oltean { 3225a68578c2SVladimir Oltean struct sja1105_port *sp = work_to_port(work); 3226a68578c2SVladimir Oltean struct sja1105_tagger_data *tagger_data = sp->data; 3227a68578c2SVladimir Oltean struct sja1105_private *priv = tagger_to_sja1105(tagger_data); 3228a68578c2SVladimir Oltean int port = sp - priv->ports; 3229a68578c2SVladimir Oltean struct sk_buff *skb; 3230a68578c2SVladimir Oltean 3231a68578c2SVladimir Oltean while ((skb = skb_dequeue(&sp->xmit_queue)) != NULL) { 3232c4b364ceSYangbo Lu struct sk_buff *clone = SJA1105_SKB_CB(skb)->clone; 3233227d07a0SVladimir Oltean 3234227d07a0SVladimir Oltean mutex_lock(&priv->mgmt_lock); 3235227d07a0SVladimir Oltean 3236a68578c2SVladimir Oltean sja1105_mgmt_xmit(priv->ds, port, 0, skb, !!clone); 3237a68578c2SVladimir Oltean 323847ed985eSVladimir Oltean /* The clone, if there, was made by dsa_skb_tx_timestamp */ 3239a68578c2SVladimir Oltean if (clone) 3240a68578c2SVladimir Oltean sja1105_ptp_txtstamp_skb(priv->ds, port, clone); 3241227d07a0SVladimir Oltean 3242227d07a0SVladimir Oltean mutex_unlock(&priv->mgmt_lock); 3243a68578c2SVladimir Oltean } 32448aa9ebccSVladimir Oltean } 32458aa9ebccSVladimir Oltean 32468456721dSVladimir Oltean /* The MAXAGE setting belongs to the L2 Forwarding Parameters table, 32478456721dSVladimir Oltean * which cannot be reconfigured at runtime. So a switch reset is required. 32488456721dSVladimir Oltean */ 32498456721dSVladimir Oltean static int sja1105_set_ageing_time(struct dsa_switch *ds, 32508456721dSVladimir Oltean unsigned int ageing_time) 32518456721dSVladimir Oltean { 32528456721dSVladimir Oltean struct sja1105_l2_lookup_params_entry *l2_lookup_params; 32538456721dSVladimir Oltean struct sja1105_private *priv = ds->priv; 32548456721dSVladimir Oltean struct sja1105_table *table; 32558456721dSVladimir Oltean unsigned int maxage; 32568456721dSVladimir Oltean 32578456721dSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS]; 32588456721dSVladimir Oltean l2_lookup_params = table->entries; 32598456721dSVladimir Oltean 32608456721dSVladimir Oltean maxage = SJA1105_AGEING_TIME_MS(ageing_time); 32618456721dSVladimir Oltean 32628456721dSVladimir Oltean if (l2_lookup_params->maxage == maxage) 32638456721dSVladimir Oltean return 0; 32648456721dSVladimir Oltean 32658456721dSVladimir Oltean l2_lookup_params->maxage = maxage; 32668456721dSVladimir Oltean 32672eea1fa8SVladimir Oltean return sja1105_static_config_reload(priv, SJA1105_AGEING_TIME); 32688456721dSVladimir Oltean } 32698456721dSVladimir Oltean 3270c279c726SVladimir Oltean static int sja1105_change_mtu(struct dsa_switch *ds, int port, int new_mtu) 3271c279c726SVladimir Oltean { 3272c279c726SVladimir Oltean struct sja1105_l2_policing_entry *policing; 3273c279c726SVladimir Oltean struct sja1105_private *priv = ds->priv; 3274c279c726SVladimir Oltean 3275c279c726SVladimir Oltean new_mtu += VLAN_ETH_HLEN + ETH_FCS_LEN; 3276c279c726SVladimir Oltean 3277c279c726SVladimir Oltean if (dsa_is_cpu_port(ds, port)) 3278c279c726SVladimir Oltean new_mtu += VLAN_HLEN; 3279c279c726SVladimir Oltean 3280c279c726SVladimir Oltean policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries; 3281c279c726SVladimir Oltean 3282a7cc081cSVladimir Oltean if (policing[port].maxlen == new_mtu) 3283c279c726SVladimir Oltean return 0; 3284c279c726SVladimir Oltean 3285a7cc081cSVladimir Oltean policing[port].maxlen = new_mtu; 3286c279c726SVladimir Oltean 3287c279c726SVladimir Oltean return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING); 3288c279c726SVladimir Oltean } 3289c279c726SVladimir Oltean 3290c279c726SVladimir Oltean static int sja1105_get_max_mtu(struct dsa_switch *ds, int port) 3291c279c726SVladimir Oltean { 3292c279c726SVladimir Oltean return 2043 - VLAN_ETH_HLEN - ETH_FCS_LEN; 3293c279c726SVladimir Oltean } 3294c279c726SVladimir Oltean 3295317ab5b8SVladimir Oltean static int sja1105_port_setup_tc(struct dsa_switch *ds, int port, 3296317ab5b8SVladimir Oltean enum tc_setup_type type, 3297317ab5b8SVladimir Oltean void *type_data) 3298317ab5b8SVladimir Oltean { 3299317ab5b8SVladimir Oltean switch (type) { 3300317ab5b8SVladimir Oltean case TC_SETUP_QDISC_TAPRIO: 3301317ab5b8SVladimir Oltean return sja1105_setup_tc_taprio(ds, port, type_data); 33024d752508SVladimir Oltean case TC_SETUP_QDISC_CBS: 33034d752508SVladimir Oltean return sja1105_setup_tc_cbs(ds, port, type_data); 3304317ab5b8SVladimir Oltean default: 3305317ab5b8SVladimir Oltean return -EOPNOTSUPP; 3306317ab5b8SVladimir Oltean } 3307317ab5b8SVladimir Oltean } 3308317ab5b8SVladimir Oltean 3309511e6ca0SVladimir Oltean /* We have a single mirror (@to) port, but can configure ingress and egress 3310511e6ca0SVladimir Oltean * mirroring on all other (@from) ports. 3311511e6ca0SVladimir Oltean * We need to allow mirroring rules only as long as the @to port is always the 3312511e6ca0SVladimir Oltean * same, and we need to unset the @to port from mirr_port only when there is no 3313511e6ca0SVladimir Oltean * mirroring rule that references it. 3314511e6ca0SVladimir Oltean */ 3315511e6ca0SVladimir Oltean static int sja1105_mirror_apply(struct sja1105_private *priv, int from, int to, 3316511e6ca0SVladimir Oltean bool ingress, bool enabled) 3317511e6ca0SVladimir Oltean { 3318511e6ca0SVladimir Oltean struct sja1105_general_params_entry *general_params; 3319511e6ca0SVladimir Oltean struct sja1105_mac_config_entry *mac; 3320542043e9SVladimir Oltean struct dsa_switch *ds = priv->ds; 3321511e6ca0SVladimir Oltean struct sja1105_table *table; 3322511e6ca0SVladimir Oltean bool already_enabled; 3323511e6ca0SVladimir Oltean u64 new_mirr_port; 3324511e6ca0SVladimir Oltean int rc; 3325511e6ca0SVladimir Oltean 3326511e6ca0SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; 3327511e6ca0SVladimir Oltean general_params = table->entries; 3328511e6ca0SVladimir Oltean 3329511e6ca0SVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 3330511e6ca0SVladimir Oltean 3331542043e9SVladimir Oltean already_enabled = (general_params->mirr_port != ds->num_ports); 3332511e6ca0SVladimir Oltean if (already_enabled && enabled && general_params->mirr_port != to) { 3333511e6ca0SVladimir Oltean dev_err(priv->ds->dev, 3334511e6ca0SVladimir Oltean "Delete mirroring rules towards port %llu first\n", 3335511e6ca0SVladimir Oltean general_params->mirr_port); 3336511e6ca0SVladimir Oltean return -EBUSY; 3337511e6ca0SVladimir Oltean } 3338511e6ca0SVladimir Oltean 3339511e6ca0SVladimir Oltean new_mirr_port = to; 3340511e6ca0SVladimir Oltean if (!enabled) { 3341511e6ca0SVladimir Oltean bool keep = false; 3342511e6ca0SVladimir Oltean int port; 3343511e6ca0SVladimir Oltean 3344511e6ca0SVladimir Oltean /* Anybody still referencing mirr_port? */ 3345542043e9SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 3346511e6ca0SVladimir Oltean if (mac[port].ing_mirr || mac[port].egr_mirr) { 3347511e6ca0SVladimir Oltean keep = true; 3348511e6ca0SVladimir Oltean break; 3349511e6ca0SVladimir Oltean } 3350511e6ca0SVladimir Oltean } 3351511e6ca0SVladimir Oltean /* Unset already_enabled for next time */ 3352511e6ca0SVladimir Oltean if (!keep) 3353542043e9SVladimir Oltean new_mirr_port = ds->num_ports; 3354511e6ca0SVladimir Oltean } 3355511e6ca0SVladimir Oltean if (new_mirr_port != general_params->mirr_port) { 3356511e6ca0SVladimir Oltean general_params->mirr_port = new_mirr_port; 3357511e6ca0SVladimir Oltean 3358511e6ca0SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_GENERAL_PARAMS, 3359511e6ca0SVladimir Oltean 0, general_params, true); 3360511e6ca0SVladimir Oltean if (rc < 0) 3361511e6ca0SVladimir Oltean return rc; 3362511e6ca0SVladimir Oltean } 3363511e6ca0SVladimir Oltean 3364511e6ca0SVladimir Oltean if (ingress) 3365511e6ca0SVladimir Oltean mac[from].ing_mirr = enabled; 3366511e6ca0SVladimir Oltean else 3367511e6ca0SVladimir Oltean mac[from].egr_mirr = enabled; 3368511e6ca0SVladimir Oltean 3369511e6ca0SVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, from, 3370511e6ca0SVladimir Oltean &mac[from], true); 3371511e6ca0SVladimir Oltean } 3372511e6ca0SVladimir Oltean 3373511e6ca0SVladimir Oltean static int sja1105_mirror_add(struct dsa_switch *ds, int port, 3374511e6ca0SVladimir Oltean struct dsa_mall_mirror_tc_entry *mirror, 3375511e6ca0SVladimir Oltean bool ingress) 3376511e6ca0SVladimir Oltean { 3377511e6ca0SVladimir Oltean return sja1105_mirror_apply(ds->priv, port, mirror->to_local_port, 3378511e6ca0SVladimir Oltean ingress, true); 3379511e6ca0SVladimir Oltean } 3380511e6ca0SVladimir Oltean 3381511e6ca0SVladimir Oltean static void sja1105_mirror_del(struct dsa_switch *ds, int port, 3382511e6ca0SVladimir Oltean struct dsa_mall_mirror_tc_entry *mirror) 3383511e6ca0SVladimir Oltean { 3384511e6ca0SVladimir Oltean sja1105_mirror_apply(ds->priv, port, mirror->to_local_port, 3385511e6ca0SVladimir Oltean mirror->ingress, false); 3386511e6ca0SVladimir Oltean } 3387511e6ca0SVladimir Oltean 3388a7cc081cSVladimir Oltean static int sja1105_port_policer_add(struct dsa_switch *ds, int port, 3389a7cc081cSVladimir Oltean struct dsa_mall_policer_tc_entry *policer) 3390a7cc081cSVladimir Oltean { 3391a7cc081cSVladimir Oltean struct sja1105_l2_policing_entry *policing; 3392a7cc081cSVladimir Oltean struct sja1105_private *priv = ds->priv; 3393a7cc081cSVladimir Oltean 3394a7cc081cSVladimir Oltean policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries; 3395a7cc081cSVladimir Oltean 3396a7cc081cSVladimir Oltean /* In hardware, every 8 microseconds the credit level is incremented by 3397a7cc081cSVladimir Oltean * the value of RATE bytes divided by 64, up to a maximum of SMAX 3398a7cc081cSVladimir Oltean * bytes. 3399a7cc081cSVladimir Oltean */ 3400a7cc081cSVladimir Oltean policing[port].rate = div_u64(512 * policer->rate_bytes_per_sec, 3401a7cc081cSVladimir Oltean 1000000); 34025f035af7SPo Liu policing[port].smax = policer->burst; 3403a7cc081cSVladimir Oltean 3404a7cc081cSVladimir Oltean return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING); 3405a7cc081cSVladimir Oltean } 3406a7cc081cSVladimir Oltean 3407a7cc081cSVladimir Oltean static void sja1105_port_policer_del(struct dsa_switch *ds, int port) 3408a7cc081cSVladimir Oltean { 3409a7cc081cSVladimir Oltean struct sja1105_l2_policing_entry *policing; 3410a7cc081cSVladimir Oltean struct sja1105_private *priv = ds->priv; 3411a7cc081cSVladimir Oltean 3412a7cc081cSVladimir Oltean policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries; 3413a7cc081cSVladimir Oltean 3414a7cc081cSVladimir Oltean policing[port].rate = SJA1105_RATE_MBPS(1000); 3415a7cc081cSVladimir Oltean policing[port].smax = 65535; 3416a7cc081cSVladimir Oltean 3417a7cc081cSVladimir Oltean sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING); 3418a7cc081cSVladimir Oltean } 3419a7cc081cSVladimir Oltean 34204d942354SVladimir Oltean static int sja1105_port_set_learning(struct sja1105_private *priv, int port, 34214d942354SVladimir Oltean bool enabled) 34224d942354SVladimir Oltean { 34234d942354SVladimir Oltean struct sja1105_mac_config_entry *mac; 34244d942354SVladimir Oltean int rc; 34254d942354SVladimir Oltean 34264d942354SVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 34274d942354SVladimir Oltean 34284c44fc5eSVladimir Oltean mac[port].dyn_learn = enabled; 34294d942354SVladimir Oltean 34304d942354SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, 34314d942354SVladimir Oltean &mac[port], true); 34324d942354SVladimir Oltean if (rc) 34334d942354SVladimir Oltean return rc; 34344d942354SVladimir Oltean 34354d942354SVladimir Oltean if (enabled) 34364d942354SVladimir Oltean priv->learn_ena |= BIT(port); 34374d942354SVladimir Oltean else 34384d942354SVladimir Oltean priv->learn_ena &= ~BIT(port); 34394d942354SVladimir Oltean 34404d942354SVladimir Oltean return 0; 34414d942354SVladimir Oltean } 34424d942354SVladimir Oltean 34434d942354SVladimir Oltean static int sja1105_port_ucast_bcast_flood(struct sja1105_private *priv, int to, 34444d942354SVladimir Oltean struct switchdev_brport_flags flags) 34454d942354SVladimir Oltean { 34464d942354SVladimir Oltean if (flags.mask & BR_FLOOD) { 34474d942354SVladimir Oltean if (flags.val & BR_FLOOD) 34487f7ccdeaSVladimir Oltean priv->ucast_egress_floods |= BIT(to); 34494d942354SVladimir Oltean else 34506a5166e0SVladimir Oltean priv->ucast_egress_floods &= ~BIT(to); 34514d942354SVladimir Oltean } 34527f7ccdeaSVladimir Oltean 34534d942354SVladimir Oltean if (flags.mask & BR_BCAST_FLOOD) { 34544d942354SVladimir Oltean if (flags.val & BR_BCAST_FLOOD) 34557f7ccdeaSVladimir Oltean priv->bcast_egress_floods |= BIT(to); 34564d942354SVladimir Oltean else 34576a5166e0SVladimir Oltean priv->bcast_egress_floods &= ~BIT(to); 34584d942354SVladimir Oltean } 34594d942354SVladimir Oltean 34607f7ccdeaSVladimir Oltean return sja1105_manage_flood_domains(priv); 34614d942354SVladimir Oltean } 34624d942354SVladimir Oltean 34634d942354SVladimir Oltean static int sja1105_port_mcast_flood(struct sja1105_private *priv, int to, 34644d942354SVladimir Oltean struct switchdev_brport_flags flags, 34654d942354SVladimir Oltean struct netlink_ext_ack *extack) 34664d942354SVladimir Oltean { 34674d942354SVladimir Oltean struct sja1105_l2_lookup_entry *l2_lookup; 34684d942354SVladimir Oltean struct sja1105_table *table; 34694d942354SVladimir Oltean int match; 34704d942354SVladimir Oltean 34714d942354SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; 34724d942354SVladimir Oltean l2_lookup = table->entries; 34734d942354SVladimir Oltean 34744d942354SVladimir Oltean for (match = 0; match < table->entry_count; match++) 34754d942354SVladimir Oltean if (l2_lookup[match].macaddr == SJA1105_UNKNOWN_MULTICAST && 34764d942354SVladimir Oltean l2_lookup[match].mask_macaddr == SJA1105_UNKNOWN_MULTICAST) 34774d942354SVladimir Oltean break; 34784d942354SVladimir Oltean 34794d942354SVladimir Oltean if (match == table->entry_count) { 34804d942354SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 34814d942354SVladimir Oltean "Could not find FDB entry for unknown multicast"); 34824d942354SVladimir Oltean return -ENOSPC; 34834d942354SVladimir Oltean } 34844d942354SVladimir Oltean 34854d942354SVladimir Oltean if (flags.val & BR_MCAST_FLOOD) 34864d942354SVladimir Oltean l2_lookup[match].destports |= BIT(to); 34874d942354SVladimir Oltean else 34884d942354SVladimir Oltean l2_lookup[match].destports &= ~BIT(to); 34894d942354SVladimir Oltean 34904d942354SVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 34914d942354SVladimir Oltean l2_lookup[match].index, 34924d942354SVladimir Oltean &l2_lookup[match], 34934d942354SVladimir Oltean true); 34944d942354SVladimir Oltean } 34954d942354SVladimir Oltean 34964d942354SVladimir Oltean static int sja1105_port_pre_bridge_flags(struct dsa_switch *ds, int port, 34974d942354SVladimir Oltean struct switchdev_brport_flags flags, 34984d942354SVladimir Oltean struct netlink_ext_ack *extack) 34994d942354SVladimir Oltean { 35004d942354SVladimir Oltean struct sja1105_private *priv = ds->priv; 35014d942354SVladimir Oltean 35024d942354SVladimir Oltean if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | 35034d942354SVladimir Oltean BR_BCAST_FLOOD)) 35044d942354SVladimir Oltean return -EINVAL; 35054d942354SVladimir Oltean 35064d942354SVladimir Oltean if (flags.mask & (BR_FLOOD | BR_MCAST_FLOOD) && 35074d942354SVladimir Oltean !priv->info->can_limit_mcast_flood) { 35084d942354SVladimir Oltean bool multicast = !!(flags.val & BR_MCAST_FLOOD); 35094d942354SVladimir Oltean bool unicast = !!(flags.val & BR_FLOOD); 35104d942354SVladimir Oltean 35114d942354SVladimir Oltean if (unicast != multicast) { 35124d942354SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 35134d942354SVladimir Oltean "This chip cannot configure multicast flooding independently of unicast"); 35144d942354SVladimir Oltean return -EINVAL; 35154d942354SVladimir Oltean } 35164d942354SVladimir Oltean } 35174d942354SVladimir Oltean 35184d942354SVladimir Oltean return 0; 35194d942354SVladimir Oltean } 35204d942354SVladimir Oltean 35214d942354SVladimir Oltean static int sja1105_port_bridge_flags(struct dsa_switch *ds, int port, 35224d942354SVladimir Oltean struct switchdev_brport_flags flags, 35234d942354SVladimir Oltean struct netlink_ext_ack *extack) 35244d942354SVladimir Oltean { 35254d942354SVladimir Oltean struct sja1105_private *priv = ds->priv; 35264d942354SVladimir Oltean int rc; 35274d942354SVladimir Oltean 35284d942354SVladimir Oltean if (flags.mask & BR_LEARNING) { 35294d942354SVladimir Oltean bool learn_ena = !!(flags.val & BR_LEARNING); 35304d942354SVladimir Oltean 35314d942354SVladimir Oltean rc = sja1105_port_set_learning(priv, port, learn_ena); 35324d942354SVladimir Oltean if (rc) 35334d942354SVladimir Oltean return rc; 35344d942354SVladimir Oltean } 35354d942354SVladimir Oltean 35364d942354SVladimir Oltean if (flags.mask & (BR_FLOOD | BR_BCAST_FLOOD)) { 35374d942354SVladimir Oltean rc = sja1105_port_ucast_bcast_flood(priv, port, flags); 35384d942354SVladimir Oltean if (rc) 35394d942354SVladimir Oltean return rc; 35404d942354SVladimir Oltean } 35414d942354SVladimir Oltean 35424d942354SVladimir Oltean /* For chips that can't offload BR_MCAST_FLOOD independently, there 35434d942354SVladimir Oltean * is nothing to do here, we ensured the configuration is in sync by 35444d942354SVladimir Oltean * offloading BR_FLOOD. 35454d942354SVladimir Oltean */ 35464d942354SVladimir Oltean if (flags.mask & BR_MCAST_FLOOD && priv->info->can_limit_mcast_flood) { 35474d942354SVladimir Oltean rc = sja1105_port_mcast_flood(priv, port, flags, 35484d942354SVladimir Oltean extack); 35494d942354SVladimir Oltean if (rc) 35504d942354SVladimir Oltean return rc; 35514d942354SVladimir Oltean } 35524d942354SVladimir Oltean 35534d942354SVladimir Oltean return 0; 35544d942354SVladimir Oltean } 35554d942354SVladimir Oltean 35568aa9ebccSVladimir Oltean static const struct dsa_switch_ops sja1105_switch_ops = { 35578aa9ebccSVladimir Oltean .get_tag_protocol = sja1105_get_tag_protocol, 35588aa9ebccSVladimir Oltean .setup = sja1105_setup, 3559f3097be2SVladimir Oltean .teardown = sja1105_teardown, 35608456721dSVladimir Oltean .set_ageing_time = sja1105_set_ageing_time, 3561c279c726SVladimir Oltean .port_change_mtu = sja1105_change_mtu, 3562c279c726SVladimir Oltean .port_max_mtu = sja1105_get_max_mtu, 3563ad9f299aSVladimir Oltean .phylink_validate = sja1105_phylink_validate, 3564ffe10e67SVladimir Oltean .phylink_mac_link_state = sja1105_mac_pcs_get_state, 3565af7cd036SVladimir Oltean .phylink_mac_config = sja1105_mac_config, 35668400cff6SVladimir Oltean .phylink_mac_link_up = sja1105_mac_link_up, 35678400cff6SVladimir Oltean .phylink_mac_link_down = sja1105_mac_link_down, 356852c34e6eSVladimir Oltean .get_strings = sja1105_get_strings, 356952c34e6eSVladimir Oltean .get_ethtool_stats = sja1105_get_ethtool_stats, 357052c34e6eSVladimir Oltean .get_sset_count = sja1105_get_sset_count, 3571bb77f36aSVladimir Oltean .get_ts_info = sja1105_get_ts_info, 3572a68578c2SVladimir Oltean .port_disable = sja1105_port_disable, 3573291d1e72SVladimir Oltean .port_fdb_dump = sja1105_fdb_dump, 3574291d1e72SVladimir Oltean .port_fdb_add = sja1105_fdb_add, 3575291d1e72SVladimir Oltean .port_fdb_del = sja1105_fdb_del, 35768aa9ebccSVladimir Oltean .port_bridge_join = sja1105_bridge_join, 35778aa9ebccSVladimir Oltean .port_bridge_leave = sja1105_bridge_leave, 35784d942354SVladimir Oltean .port_pre_bridge_flags = sja1105_port_pre_bridge_flags, 35794d942354SVladimir Oltean .port_bridge_flags = sja1105_port_bridge_flags, 3580640f763fSVladimir Oltean .port_stp_state_set = sja1105_bridge_stp_state_set, 35816666cebcSVladimir Oltean .port_vlan_filtering = sja1105_vlan_filtering, 35826666cebcSVladimir Oltean .port_vlan_add = sja1105_vlan_add, 35836666cebcSVladimir Oltean .port_vlan_del = sja1105_vlan_del, 3584291d1e72SVladimir Oltean .port_mdb_add = sja1105_mdb_add, 3585291d1e72SVladimir Oltean .port_mdb_del = sja1105_mdb_del, 3586a602afd2SVladimir Oltean .port_hwtstamp_get = sja1105_hwtstamp_get, 3587a602afd2SVladimir Oltean .port_hwtstamp_set = sja1105_hwtstamp_set, 3588f3097be2SVladimir Oltean .port_rxtstamp = sja1105_port_rxtstamp, 358947ed985eSVladimir Oltean .port_txtstamp = sja1105_port_txtstamp, 3590317ab5b8SVladimir Oltean .port_setup_tc = sja1105_port_setup_tc, 3591511e6ca0SVladimir Oltean .port_mirror_add = sja1105_mirror_add, 3592511e6ca0SVladimir Oltean .port_mirror_del = sja1105_mirror_del, 3593a7cc081cSVladimir Oltean .port_policer_add = sja1105_port_policer_add, 3594a7cc081cSVladimir Oltean .port_policer_del = sja1105_port_policer_del, 3595a6af7763SVladimir Oltean .cls_flower_add = sja1105_cls_flower_add, 3596a6af7763SVladimir Oltean .cls_flower_del = sja1105_cls_flower_del, 3597834f8933SVladimir Oltean .cls_flower_stats = sja1105_cls_flower_stats, 3598ac02a451SVladimir Oltean .crosschip_bridge_join = sja1105_crosschip_bridge_join, 3599ac02a451SVladimir Oltean .crosschip_bridge_leave = sja1105_crosschip_bridge_leave, 36002cafa72eSVladimir Oltean .devlink_param_get = sja1105_devlink_param_get, 36012cafa72eSVladimir Oltean .devlink_param_set = sja1105_devlink_param_set, 3602ff4cf8eaSVladimir Oltean .devlink_info_get = sja1105_devlink_info_get, 36038aa9ebccSVladimir Oltean }; 36048aa9ebccSVladimir Oltean 36050b0e2997SVladimir Oltean static const struct of_device_id sja1105_dt_ids[]; 36060b0e2997SVladimir Oltean 36078aa9ebccSVladimir Oltean static int sja1105_check_device_id(struct sja1105_private *priv) 36088aa9ebccSVladimir Oltean { 36098aa9ebccSVladimir Oltean const struct sja1105_regs *regs = priv->info->regs; 36108aa9ebccSVladimir Oltean u8 prod_id[SJA1105_SIZE_DEVICE_ID] = {0}; 36118aa9ebccSVladimir Oltean struct device *dev = &priv->spidev->dev; 36120b0e2997SVladimir Oltean const struct of_device_id *match; 3613dff79620SVladimir Oltean u32 device_id; 36148aa9ebccSVladimir Oltean u64 part_no; 36158aa9ebccSVladimir Oltean int rc; 36168aa9ebccSVladimir Oltean 361734d76e9fSVladimir Oltean rc = sja1105_xfer_u32(priv, SPI_READ, regs->device_id, &device_id, 361834d76e9fSVladimir Oltean NULL); 36198aa9ebccSVladimir Oltean if (rc < 0) 36208aa9ebccSVladimir Oltean return rc; 36218aa9ebccSVladimir Oltean 36221bd44870SVladimir Oltean rc = sja1105_xfer_buf(priv, SPI_READ, regs->prod_id, prod_id, 36231bd44870SVladimir Oltean SJA1105_SIZE_DEVICE_ID); 36248aa9ebccSVladimir Oltean if (rc < 0) 36258aa9ebccSVladimir Oltean return rc; 36268aa9ebccSVladimir Oltean 36278aa9ebccSVladimir Oltean sja1105_unpack(prod_id, &part_no, 19, 4, SJA1105_SIZE_DEVICE_ID); 36288aa9ebccSVladimir Oltean 36295978fac0SNathan Chancellor for (match = sja1105_dt_ids; match->compatible[0]; match++) { 36300b0e2997SVladimir Oltean const struct sja1105_info *info = match->data; 36310b0e2997SVladimir Oltean 36320b0e2997SVladimir Oltean /* Is what's been probed in our match table at all? */ 36330b0e2997SVladimir Oltean if (info->device_id != device_id || info->part_no != part_no) 36340b0e2997SVladimir Oltean continue; 36350b0e2997SVladimir Oltean 36360b0e2997SVladimir Oltean /* But is it what's in the device tree? */ 36370b0e2997SVladimir Oltean if (priv->info->device_id != device_id || 36380b0e2997SVladimir Oltean priv->info->part_no != part_no) { 36390b0e2997SVladimir Oltean dev_warn(dev, "Device tree specifies chip %s but found %s, please fix it!\n", 36400b0e2997SVladimir Oltean priv->info->name, info->name); 36410b0e2997SVladimir Oltean /* It isn't. No problem, pick that up. */ 36420b0e2997SVladimir Oltean priv->info = info; 36438aa9ebccSVladimir Oltean } 36448aa9ebccSVladimir Oltean 36458aa9ebccSVladimir Oltean return 0; 36468aa9ebccSVladimir Oltean } 36478aa9ebccSVladimir Oltean 36480b0e2997SVladimir Oltean dev_err(dev, "Unexpected {device ID, part number}: 0x%x 0x%llx\n", 36490b0e2997SVladimir Oltean device_id, part_no); 36500b0e2997SVladimir Oltean 36510b0e2997SVladimir Oltean return -ENODEV; 36520b0e2997SVladimir Oltean } 36530b0e2997SVladimir Oltean 36548aa9ebccSVladimir Oltean static int sja1105_probe(struct spi_device *spi) 36558aa9ebccSVladimir Oltean { 3656844d7edcSVladimir Oltean struct sja1105_tagger_data *tagger_data; 36578aa9ebccSVladimir Oltean struct device *dev = &spi->dev; 36588aa9ebccSVladimir Oltean struct sja1105_private *priv; 3659718bad0eSVladimir Oltean size_t max_xfer, max_msg; 36608aa9ebccSVladimir Oltean struct dsa_switch *ds; 3661a68578c2SVladimir Oltean int rc, port; 36628aa9ebccSVladimir Oltean 36638aa9ebccSVladimir Oltean if (!dev->of_node) { 36648aa9ebccSVladimir Oltean dev_err(dev, "No DTS bindings for SJA1105 driver\n"); 36658aa9ebccSVladimir Oltean return -EINVAL; 36668aa9ebccSVladimir Oltean } 36678aa9ebccSVladimir Oltean 36688aa9ebccSVladimir Oltean priv = devm_kzalloc(dev, sizeof(struct sja1105_private), GFP_KERNEL); 36698aa9ebccSVladimir Oltean if (!priv) 36708aa9ebccSVladimir Oltean return -ENOMEM; 36718aa9ebccSVladimir Oltean 36728aa9ebccSVladimir Oltean /* Configure the optional reset pin and bring up switch */ 36738aa9ebccSVladimir Oltean priv->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); 36748aa9ebccSVladimir Oltean if (IS_ERR(priv->reset_gpio)) 36758aa9ebccSVladimir Oltean dev_dbg(dev, "reset-gpios not defined, ignoring\n"); 36768aa9ebccSVladimir Oltean else 36778aa9ebccSVladimir Oltean sja1105_hw_reset(priv->reset_gpio, 1, 1); 36788aa9ebccSVladimir Oltean 36798aa9ebccSVladimir Oltean /* Populate our driver private structure (priv) based on 36808aa9ebccSVladimir Oltean * the device tree node that was probed (spi) 36818aa9ebccSVladimir Oltean */ 36828aa9ebccSVladimir Oltean priv->spidev = spi; 36838aa9ebccSVladimir Oltean spi_set_drvdata(spi, priv); 36848aa9ebccSVladimir Oltean 36858aa9ebccSVladimir Oltean /* Configure the SPI bus */ 36868aa9ebccSVladimir Oltean spi->bits_per_word = 8; 36878aa9ebccSVladimir Oltean rc = spi_setup(spi); 36888aa9ebccSVladimir Oltean if (rc < 0) { 36898aa9ebccSVladimir Oltean dev_err(dev, "Could not init SPI\n"); 36908aa9ebccSVladimir Oltean return rc; 36918aa9ebccSVladimir Oltean } 36928aa9ebccSVladimir Oltean 3693718bad0eSVladimir Oltean /* In sja1105_xfer, we send spi_messages composed of two spi_transfers: 3694718bad0eSVladimir Oltean * a small one for the message header and another one for the current 3695718bad0eSVladimir Oltean * chunk of the packed buffer. 3696718bad0eSVladimir Oltean * Check that the restrictions imposed by the SPI controller are 3697718bad0eSVladimir Oltean * respected: the chunk buffer is smaller than the max transfer size, 3698718bad0eSVladimir Oltean * and the total length of the chunk plus its message header is smaller 3699718bad0eSVladimir Oltean * than the max message size. 3700718bad0eSVladimir Oltean * We do that during probe time since the maximum transfer size is a 3701718bad0eSVladimir Oltean * runtime invariant. 3702718bad0eSVladimir Oltean */ 3703718bad0eSVladimir Oltean max_xfer = spi_max_transfer_size(spi); 3704718bad0eSVladimir Oltean max_msg = spi_max_message_size(spi); 3705718bad0eSVladimir Oltean 3706718bad0eSVladimir Oltean /* We need to send at least one 64-bit word of SPI payload per message 3707718bad0eSVladimir Oltean * in order to be able to make useful progress. 3708718bad0eSVladimir Oltean */ 3709718bad0eSVladimir Oltean if (max_msg < SJA1105_SIZE_SPI_MSG_HEADER + 8) { 3710718bad0eSVladimir Oltean dev_err(dev, "SPI master cannot send large enough buffers, aborting\n"); 3711718bad0eSVladimir Oltean return -EINVAL; 3712718bad0eSVladimir Oltean } 3713718bad0eSVladimir Oltean 3714718bad0eSVladimir Oltean priv->max_xfer_len = SJA1105_SIZE_SPI_MSG_MAXLEN; 3715718bad0eSVladimir Oltean if (priv->max_xfer_len > max_xfer) 3716718bad0eSVladimir Oltean priv->max_xfer_len = max_xfer; 3717718bad0eSVladimir Oltean if (priv->max_xfer_len > max_msg - SJA1105_SIZE_SPI_MSG_HEADER) 3718718bad0eSVladimir Oltean priv->max_xfer_len = max_msg - SJA1105_SIZE_SPI_MSG_HEADER; 3719718bad0eSVladimir Oltean 37208aa9ebccSVladimir Oltean priv->info = of_device_get_match_data(dev); 37218aa9ebccSVladimir Oltean 37228aa9ebccSVladimir Oltean /* Detect hardware device */ 37238aa9ebccSVladimir Oltean rc = sja1105_check_device_id(priv); 37248aa9ebccSVladimir Oltean if (rc < 0) { 37258aa9ebccSVladimir Oltean dev_err(dev, "Device ID check failed: %d\n", rc); 37268aa9ebccSVladimir Oltean return rc; 37278aa9ebccSVladimir Oltean } 37288aa9ebccSVladimir Oltean 37298aa9ebccSVladimir Oltean dev_info(dev, "Probed switch chip: %s\n", priv->info->name); 37308aa9ebccSVladimir Oltean 37317e99e347SVivien Didelot ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL); 37328aa9ebccSVladimir Oltean if (!ds) 37338aa9ebccSVladimir Oltean return -ENOMEM; 37348aa9ebccSVladimir Oltean 37357e99e347SVivien Didelot ds->dev = dev; 3736*3e77e59bSVladimir Oltean ds->num_ports = priv->info->num_ports; 37378aa9ebccSVladimir Oltean ds->ops = &sja1105_switch_ops; 37388aa9ebccSVladimir Oltean ds->priv = priv; 37398aa9ebccSVladimir Oltean priv->ds = ds; 37408aa9ebccSVladimir Oltean 3741844d7edcSVladimir Oltean tagger_data = &priv->tagger_data; 3742844d7edcSVladimir Oltean 3743d5a619bfSVivien Didelot mutex_init(&priv->ptp_data.lock); 3744d5a619bfSVivien Didelot mutex_init(&priv->mgmt_lock); 3745d5a619bfSVivien Didelot 37465899ee36SVladimir Oltean priv->dsa_8021q_ctx = devm_kzalloc(dev, sizeof(*priv->dsa_8021q_ctx), 37475899ee36SVladimir Oltean GFP_KERNEL); 37485899ee36SVladimir Oltean if (!priv->dsa_8021q_ctx) 37495899ee36SVladimir Oltean return -ENOMEM; 37505899ee36SVladimir Oltean 37515899ee36SVladimir Oltean priv->dsa_8021q_ctx->ops = &sja1105_dsa_8021q_ops; 3752bbed0bbdSVladimir Oltean priv->dsa_8021q_ctx->proto = htons(ETH_P_8021Q); 37535899ee36SVladimir Oltean priv->dsa_8021q_ctx->ds = ds; 37545899ee36SVladimir Oltean 37555899ee36SVladimir Oltean INIT_LIST_HEAD(&priv->dsa_8021q_ctx->crosschip_links); 3756ec5ae610SVladimir Oltean INIT_LIST_HEAD(&priv->bridge_vlans); 3757ec5ae610SVladimir Oltean INIT_LIST_HEAD(&priv->dsa_8021q_vlans); 3758ac02a451SVladimir Oltean 3759d5a619bfSVivien Didelot sja1105_tas_setup(ds); 3760a6af7763SVladimir Oltean sja1105_flower_setup(ds); 3761d5a619bfSVivien Didelot 3762d5a619bfSVivien Didelot rc = dsa_register_switch(priv->ds); 3763d5a619bfSVivien Didelot if (rc) 3764d5a619bfSVivien Didelot return rc; 3765d5a619bfSVivien Didelot 37664d752508SVladimir Oltean if (IS_ENABLED(CONFIG_NET_SCH_CBS)) { 37674d752508SVladimir Oltean priv->cbs = devm_kcalloc(dev, priv->info->num_cbs_shapers, 37684d752508SVladimir Oltean sizeof(struct sja1105_cbs_entry), 37694d752508SVladimir Oltean GFP_KERNEL); 3770dc596e3fSVladimir Oltean if (!priv->cbs) { 3771dc596e3fSVladimir Oltean rc = -ENOMEM; 3772dc596e3fSVladimir Oltean goto out_unregister_switch; 3773dc596e3fSVladimir Oltean } 37744d752508SVladimir Oltean } 37754d752508SVladimir Oltean 3776227d07a0SVladimir Oltean /* Connections between dsa_port and sja1105_port */ 3777542043e9SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 3778a68578c2SVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 3779a68578c2SVladimir Oltean struct dsa_port *dp = dsa_to_port(ds, port); 3780a68578c2SVladimir Oltean struct net_device *slave; 378184eeb5d4SVladimir Oltean int subvlan; 3782227d07a0SVladimir Oltean 3783a68578c2SVladimir Oltean if (!dsa_is_user_port(ds, port)) 3784a68578c2SVladimir Oltean continue; 3785a68578c2SVladimir Oltean 3786a68578c2SVladimir Oltean dp->priv = sp; 3787a68578c2SVladimir Oltean sp->dp = dp; 3788844d7edcSVladimir Oltean sp->data = tagger_data; 3789a68578c2SVladimir Oltean slave = dp->slave; 3790a68578c2SVladimir Oltean kthread_init_work(&sp->xmit_work, sja1105_port_deferred_xmit); 3791a68578c2SVladimir Oltean sp->xmit_worker = kthread_create_worker(0, "%s_xmit", 3792a68578c2SVladimir Oltean slave->name); 3793a68578c2SVladimir Oltean if (IS_ERR(sp->xmit_worker)) { 3794a68578c2SVladimir Oltean rc = PTR_ERR(sp->xmit_worker); 3795a68578c2SVladimir Oltean dev_err(ds->dev, 3796a68578c2SVladimir Oltean "failed to create deferred xmit thread: %d\n", 3797a68578c2SVladimir Oltean rc); 3798dc596e3fSVladimir Oltean goto out_destroy_workers; 3799a68578c2SVladimir Oltean } 3800a68578c2SVladimir Oltean skb_queue_head_init(&sp->xmit_queue); 380138b5beeaSVladimir Oltean sp->xmit_tpid = ETH_P_SJA1105; 380284eeb5d4SVladimir Oltean 380384eeb5d4SVladimir Oltean for (subvlan = 0; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++) 380484eeb5d4SVladimir Oltean sp->subvlan_map[subvlan] = VLAN_N_VID; 3805227d07a0SVladimir Oltean } 3806227d07a0SVladimir Oltean 3807d5a619bfSVivien Didelot return 0; 3808dc596e3fSVladimir Oltean 3809dc596e3fSVladimir Oltean out_destroy_workers: 3810a68578c2SVladimir Oltean while (port-- > 0) { 3811a68578c2SVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 3812a68578c2SVladimir Oltean 3813a68578c2SVladimir Oltean if (!dsa_is_user_port(ds, port)) 3814a68578c2SVladimir Oltean continue; 3815a68578c2SVladimir Oltean 3816a68578c2SVladimir Oltean kthread_destroy_worker(sp->xmit_worker); 3817a68578c2SVladimir Oltean } 3818dc596e3fSVladimir Oltean 3819dc596e3fSVladimir Oltean out_unregister_switch: 3820dc596e3fSVladimir Oltean dsa_unregister_switch(ds); 3821dc596e3fSVladimir Oltean 3822a68578c2SVladimir Oltean return rc; 38238aa9ebccSVladimir Oltean } 38248aa9ebccSVladimir Oltean 38258aa9ebccSVladimir Oltean static int sja1105_remove(struct spi_device *spi) 38268aa9ebccSVladimir Oltean { 38278aa9ebccSVladimir Oltean struct sja1105_private *priv = spi_get_drvdata(spi); 38288aa9ebccSVladimir Oltean 38298aa9ebccSVladimir Oltean dsa_unregister_switch(priv->ds); 38308aa9ebccSVladimir Oltean return 0; 38318aa9ebccSVladimir Oltean } 38328aa9ebccSVladimir Oltean 38338aa9ebccSVladimir Oltean static const struct of_device_id sja1105_dt_ids[] = { 38348aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105e", .data = &sja1105e_info }, 38358aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105t", .data = &sja1105t_info }, 38368aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105p", .data = &sja1105p_info }, 38378aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105q", .data = &sja1105q_info }, 38388aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105r", .data = &sja1105r_info }, 38398aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105s", .data = &sja1105s_info }, 3840*3e77e59bSVladimir Oltean { .compatible = "nxp,sja1110a", .data = &sja1110a_info }, 3841*3e77e59bSVladimir Oltean { .compatible = "nxp,sja1110b", .data = &sja1110b_info }, 3842*3e77e59bSVladimir Oltean { .compatible = "nxp,sja1110c", .data = &sja1110c_info }, 3843*3e77e59bSVladimir Oltean { .compatible = "nxp,sja1110d", .data = &sja1110d_info }, 38448aa9ebccSVladimir Oltean { /* sentinel */ }, 38458aa9ebccSVladimir Oltean }; 38468aa9ebccSVladimir Oltean MODULE_DEVICE_TABLE(of, sja1105_dt_ids); 38478aa9ebccSVladimir Oltean 38488aa9ebccSVladimir Oltean static struct spi_driver sja1105_driver = { 38498aa9ebccSVladimir Oltean .driver = { 38508aa9ebccSVladimir Oltean .name = "sja1105", 38518aa9ebccSVladimir Oltean .owner = THIS_MODULE, 38528aa9ebccSVladimir Oltean .of_match_table = of_match_ptr(sja1105_dt_ids), 38538aa9ebccSVladimir Oltean }, 38548aa9ebccSVladimir Oltean .probe = sja1105_probe, 38558aa9ebccSVladimir Oltean .remove = sja1105_remove, 38568aa9ebccSVladimir Oltean }; 38578aa9ebccSVladimir Oltean 38588aa9ebccSVladimir Oltean module_spi_driver(sja1105_driver); 38598aa9ebccSVladimir Oltean 38608aa9ebccSVladimir Oltean MODULE_AUTHOR("Vladimir Oltean <olteanv@gmail.com>"); 38618aa9ebccSVladimir Oltean MODULE_AUTHOR("Georg Waibel <georg.waibel@sensor-technik.de>"); 38628aa9ebccSVladimir Oltean MODULE_DESCRIPTION("SJA1105 Driver"); 38638aa9ebccSVladimir Oltean MODULE_LICENSE("GPL v2"); 3864