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 = { 3463e77e59bSVladimir 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 } 4593e77e59bSVladimir Oltean 4603e77e59bSVladimir Oltean l2fwd[ds->num_ports + i].type_egrpcp2outputq = true; 4613e77e59bSVladimir Oltean } 4623e77e59bSVladimir Oltean 4633e77e59bSVladimir Oltean return 0; 4643e77e59bSVladimir Oltean } 4653e77e59bSVladimir Oltean 4663e77e59bSVladimir Oltean static int sja1110_init_pcp_remapping(struct sja1105_private *priv) 4673e77e59bSVladimir Oltean { 4683e77e59bSVladimir Oltean struct sja1110_pcp_remapping_entry *pcp_remap; 4693e77e59bSVladimir Oltean struct dsa_switch *ds = priv->ds; 4703e77e59bSVladimir Oltean struct sja1105_table *table; 4713e77e59bSVladimir Oltean int port, tc; 4723e77e59bSVladimir Oltean 4733e77e59bSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_PCP_REMAPPING]; 4743e77e59bSVladimir Oltean 4753e77e59bSVladimir Oltean /* Nothing to do for SJA1105 */ 4763e77e59bSVladimir Oltean if (!table->ops->max_entry_count) 4773e77e59bSVladimir Oltean return 0; 4783e77e59bSVladimir Oltean 4793e77e59bSVladimir Oltean if (table->entry_count) { 4803e77e59bSVladimir Oltean kfree(table->entries); 4813e77e59bSVladimir Oltean table->entry_count = 0; 4823e77e59bSVladimir Oltean } 4833e77e59bSVladimir Oltean 4843e77e59bSVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 4853e77e59bSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 4863e77e59bSVladimir Oltean if (!table->entries) 4873e77e59bSVladimir Oltean return -ENOMEM; 4883e77e59bSVladimir Oltean 4893e77e59bSVladimir Oltean table->entry_count = table->ops->max_entry_count; 4903e77e59bSVladimir Oltean 4913e77e59bSVladimir Oltean pcp_remap = table->entries; 4923e77e59bSVladimir Oltean 4933e77e59bSVladimir Oltean /* Repeat the configuration done for vlan_pmap */ 4943e77e59bSVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 4953e77e59bSVladimir Oltean if (dsa_is_unused_port(ds, port)) 4963e77e59bSVladimir Oltean continue; 4973e77e59bSVladimir Oltean 4983e77e59bSVladimir Oltean for (tc = 0; tc < SJA1105_NUM_TC; tc++) 4993e77e59bSVladimir 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 568*ceec8bc0SVladimir Oltean /* SJA1110 TDMACONFIGIDX values: 569*ceec8bc0SVladimir Oltean * 570*ceec8bc0SVladimir Oltean * | 100 Mbps ports | 1Gbps ports | 2.5Gbps ports | Disabled ports 571*ceec8bc0SVladimir Oltean * -----+----------------+---------------+---------------+--------------- 572*ceec8bc0SVladimir Oltean * 0 | 0, [5:10] | [1:2] | [3:4] | retag 573*ceec8bc0SVladimir Oltean * 1 |0, [5:10], retag| [1:2] | [3:4] | - 574*ceec8bc0SVladimir Oltean * 2 | 0, [5:10] | [1:3], retag | 4 | - 575*ceec8bc0SVladimir Oltean * 3 | 0, [5:10] |[1:2], 4, retag| 3 | - 576*ceec8bc0SVladimir Oltean * 4 | 0, 2, [5:10] | 1, retag | [3:4] | - 577*ceec8bc0SVladimir Oltean * 5 | 0, 1, [5:10] | 2, retag | [3:4] | - 578*ceec8bc0SVladimir Oltean * 14 | 0, [5:10] | [1:4], retag | - | - 579*ceec8bc0SVladimir Oltean * 15 | [5:10] | [0:4], retag | - | - 580*ceec8bc0SVladimir Oltean */ 581*ceec8bc0SVladimir Oltean static void sja1110_select_tdmaconfigidx(struct sja1105_private *priv) 582*ceec8bc0SVladimir Oltean { 583*ceec8bc0SVladimir Oltean struct sja1105_general_params_entry *general_params; 584*ceec8bc0SVladimir Oltean struct sja1105_table *table; 585*ceec8bc0SVladimir Oltean bool port_1_is_base_tx; 586*ceec8bc0SVladimir Oltean bool port_3_is_2500; 587*ceec8bc0SVladimir Oltean bool port_4_is_2500; 588*ceec8bc0SVladimir Oltean u64 tdmaconfigidx; 589*ceec8bc0SVladimir Oltean 590*ceec8bc0SVladimir Oltean if (priv->info->device_id != SJA1110_DEVICE_ID) 591*ceec8bc0SVladimir Oltean return; 592*ceec8bc0SVladimir Oltean 593*ceec8bc0SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; 594*ceec8bc0SVladimir Oltean general_params = table->entries; 595*ceec8bc0SVladimir Oltean 596*ceec8bc0SVladimir Oltean /* All the settings below are "as opposed to SGMII", which is the 597*ceec8bc0SVladimir Oltean * other pinmuxing option. 598*ceec8bc0SVladimir Oltean */ 599*ceec8bc0SVladimir Oltean port_1_is_base_tx = priv->phy_mode[1] == PHY_INTERFACE_MODE_INTERNAL; 600*ceec8bc0SVladimir Oltean port_3_is_2500 = priv->phy_mode[3] == PHY_INTERFACE_MODE_2500BASEX; 601*ceec8bc0SVladimir Oltean port_4_is_2500 = priv->phy_mode[4] == PHY_INTERFACE_MODE_2500BASEX; 602*ceec8bc0SVladimir Oltean 603*ceec8bc0SVladimir Oltean if (port_1_is_base_tx) 604*ceec8bc0SVladimir Oltean /* Retagging port will operate at 1 Gbps */ 605*ceec8bc0SVladimir Oltean tdmaconfigidx = 5; 606*ceec8bc0SVladimir Oltean else if (port_3_is_2500 && port_4_is_2500) 607*ceec8bc0SVladimir Oltean /* Retagging port will operate at 100 Mbps */ 608*ceec8bc0SVladimir Oltean tdmaconfigidx = 1; 609*ceec8bc0SVladimir Oltean else if (port_3_is_2500) 610*ceec8bc0SVladimir Oltean /* Retagging port will operate at 1 Gbps */ 611*ceec8bc0SVladimir Oltean tdmaconfigidx = 3; 612*ceec8bc0SVladimir Oltean else if (port_4_is_2500) 613*ceec8bc0SVladimir Oltean /* Retagging port will operate at 1 Gbps */ 614*ceec8bc0SVladimir Oltean tdmaconfigidx = 2; 615*ceec8bc0SVladimir Oltean else 616*ceec8bc0SVladimir Oltean /* Retagging port will operate at 1 Gbps */ 617*ceec8bc0SVladimir Oltean tdmaconfigidx = 14; 618*ceec8bc0SVladimir Oltean 619*ceec8bc0SVladimir Oltean general_params->tdmaconfigidx = tdmaconfigidx; 620*ceec8bc0SVladimir Oltean } 621*ceec8bc0SVladimir Oltean 6228aa9ebccSVladimir Oltean static int sja1105_init_general_params(struct sja1105_private *priv) 6238aa9ebccSVladimir Oltean { 6248aa9ebccSVladimir Oltean struct sja1105_general_params_entry default_general_params = { 625511e6ca0SVladimir Oltean /* Allow dynamic changing of the mirror port */ 626511e6ca0SVladimir Oltean .mirr_ptacu = true, 6278aa9ebccSVladimir Oltean .switchid = priv->ds->index, 6285f06c63bSVladimir Oltean /* Priority queue for link-local management frames 6295f06c63bSVladimir Oltean * (both ingress to and egress from CPU - PTP, STP etc) 6305f06c63bSVladimir Oltean */ 63108fde09aSVladimir Oltean .hostprio = 7, 6328aa9ebccSVladimir Oltean .mac_fltres1 = SJA1105_LINKLOCAL_FILTER_A, 6338aa9ebccSVladimir Oltean .mac_flt1 = SJA1105_LINKLOCAL_FILTER_A_MASK, 63442824463SVladimir Oltean .incl_srcpt1 = false, 6358aa9ebccSVladimir Oltean .send_meta1 = false, 6368aa9ebccSVladimir Oltean .mac_fltres0 = SJA1105_LINKLOCAL_FILTER_B, 6378aa9ebccSVladimir Oltean .mac_flt0 = SJA1105_LINKLOCAL_FILTER_B_MASK, 63842824463SVladimir Oltean .incl_srcpt0 = false, 6398aa9ebccSVladimir Oltean .send_meta0 = false, 6408aa9ebccSVladimir Oltean /* The destination for traffic matching mac_fltres1 and 6418aa9ebccSVladimir Oltean * mac_fltres0 on all ports except host_port. Such traffic 6428aa9ebccSVladimir Oltean * receieved on host_port itself would be dropped, except 6438aa9ebccSVladimir Oltean * by installing a temporary 'management route' 6448aa9ebccSVladimir Oltean */ 645df2a81a3SVladimir Oltean .host_port = priv->ds->num_ports, 646511e6ca0SVladimir Oltean /* Default to an invalid value */ 647542043e9SVladimir Oltean .mirr_port = priv->ds->num_ports, 6488aa9ebccSVladimir Oltean /* Link-local traffic received on casc_port will be forwarded 6498aa9ebccSVladimir Oltean * to host_port without embedding the source port and device ID 6508aa9ebccSVladimir Oltean * info in the destination MAC address (presumably because it 6518aa9ebccSVladimir Oltean * is a cascaded port and a downstream SJA switch already did 6528aa9ebccSVladimir Oltean * that). Default to an invalid port (to disable the feature) 6538aa9ebccSVladimir Oltean * and overwrite this if we find any DSA (cascaded) ports. 6548aa9ebccSVladimir Oltean */ 655542043e9SVladimir Oltean .casc_port = priv->ds->num_ports, 6568aa9ebccSVladimir Oltean /* No TTEthernet */ 657dfacc5a2SVladimir Oltean .vllupformat = SJA1105_VL_FORMAT_PSFP, 6588aa9ebccSVladimir Oltean .vlmarker = 0, 6598aa9ebccSVladimir Oltean .vlmask = 0, 6608aa9ebccSVladimir Oltean /* Only update correctionField for 1-step PTP (L2 transport) */ 6618aa9ebccSVladimir Oltean .ignore2stf = 0, 6626666cebcSVladimir Oltean /* Forcefully disable VLAN filtering by telling 6636666cebcSVladimir Oltean * the switch that VLAN has a different EtherType. 6646666cebcSVladimir Oltean */ 6656666cebcSVladimir Oltean .tpid = ETH_P_SJA1105, 6666666cebcSVladimir Oltean .tpid2 = ETH_P_SJA1105, 6678aa9ebccSVladimir Oltean }; 668df2a81a3SVladimir Oltean struct dsa_switch *ds = priv->ds; 6698aa9ebccSVladimir Oltean struct sja1105_table *table; 670df2a81a3SVladimir Oltean int port; 671df2a81a3SVladimir Oltean 672df2a81a3SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 673df2a81a3SVladimir Oltean if (dsa_is_cpu_port(ds, port)) { 674df2a81a3SVladimir Oltean default_general_params.host_port = port; 675df2a81a3SVladimir Oltean break; 676df2a81a3SVladimir Oltean } 677df2a81a3SVladimir Oltean } 6788aa9ebccSVladimir Oltean 6798aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; 6808aa9ebccSVladimir Oltean 6818aa9ebccSVladimir Oltean if (table->entry_count) { 6828aa9ebccSVladimir Oltean kfree(table->entries); 6838aa9ebccSVladimir Oltean table->entry_count = 0; 6848aa9ebccSVladimir Oltean } 6858aa9ebccSVladimir Oltean 686fd6f2c25SVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 6878aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 6888aa9ebccSVladimir Oltean if (!table->entries) 6898aa9ebccSVladimir Oltean return -ENOMEM; 6908aa9ebccSVladimir Oltean 691fd6f2c25SVladimir Oltean table->entry_count = table->ops->max_entry_count; 6928aa9ebccSVladimir Oltean 6938aa9ebccSVladimir Oltean /* This table only has a single entry */ 6948aa9ebccSVladimir Oltean ((struct sja1105_general_params_entry *)table->entries)[0] = 6958aa9ebccSVladimir Oltean default_general_params; 6968aa9ebccSVladimir Oltean 697*ceec8bc0SVladimir Oltean sja1110_select_tdmaconfigidx(priv); 698*ceec8bc0SVladimir Oltean 6998aa9ebccSVladimir Oltean return 0; 7008aa9ebccSVladimir Oltean } 7018aa9ebccSVladimir Oltean 70279d5511cSVladimir Oltean static int sja1105_init_avb_params(struct sja1105_private *priv) 70379d5511cSVladimir Oltean { 70479d5511cSVladimir Oltean struct sja1105_avb_params_entry *avb; 70579d5511cSVladimir Oltean struct sja1105_table *table; 70679d5511cSVladimir Oltean 70779d5511cSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_AVB_PARAMS]; 70879d5511cSVladimir Oltean 70979d5511cSVladimir Oltean /* Discard previous AVB Parameters Table */ 71079d5511cSVladimir Oltean if (table->entry_count) { 71179d5511cSVladimir Oltean kfree(table->entries); 71279d5511cSVladimir Oltean table->entry_count = 0; 71379d5511cSVladimir Oltean } 71479d5511cSVladimir Oltean 715fd6f2c25SVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 71679d5511cSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 71779d5511cSVladimir Oltean if (!table->entries) 71879d5511cSVladimir Oltean return -ENOMEM; 71979d5511cSVladimir Oltean 720fd6f2c25SVladimir Oltean table->entry_count = table->ops->max_entry_count; 72179d5511cSVladimir Oltean 72279d5511cSVladimir Oltean avb = table->entries; 72379d5511cSVladimir Oltean 72479d5511cSVladimir Oltean /* Configure the MAC addresses for meta frames */ 72579d5511cSVladimir Oltean avb->destmeta = SJA1105_META_DMAC; 72679d5511cSVladimir Oltean avb->srcmeta = SJA1105_META_SMAC; 727747e5eb3SVladimir Oltean /* On P/Q/R/S, configure the direction of the PTP_CLK pin as input by 728747e5eb3SVladimir Oltean * default. This is because there might be boards with a hardware 729747e5eb3SVladimir Oltean * layout where enabling the pin as output might cause an electrical 730747e5eb3SVladimir Oltean * clash. On E/T the pin is always an output, which the board designers 731747e5eb3SVladimir Oltean * probably already knew, so even if there are going to be electrical 732747e5eb3SVladimir Oltean * issues, there's nothing we can do. 733747e5eb3SVladimir Oltean */ 734747e5eb3SVladimir Oltean avb->cas_master = false; 73579d5511cSVladimir Oltean 73679d5511cSVladimir Oltean return 0; 73779d5511cSVladimir Oltean } 73879d5511cSVladimir Oltean 739a7cc081cSVladimir Oltean /* The L2 policing table is 2-stage. The table is looked up for each frame 740a7cc081cSVladimir Oltean * according to the ingress port, whether it was broadcast or not, and the 741a7cc081cSVladimir Oltean * classified traffic class (given by VLAN PCP). This portion of the lookup is 742a7cc081cSVladimir Oltean * fixed, and gives access to the SHARINDX, an indirection register pointing 743a7cc081cSVladimir Oltean * within the policing table itself, which is used to resolve the policer that 744a7cc081cSVladimir Oltean * will be used for this frame. 745a7cc081cSVladimir Oltean * 746a7cc081cSVladimir Oltean * Stage 1 Stage 2 747a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 748a7cc081cSVladimir Oltean * |Port 0 TC 0 |SHARINDX| | Policer 0: Rate, Burst, MTU | 749a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 750a7cc081cSVladimir Oltean * |Port 0 TC 1 |SHARINDX| | Policer 1: Rate, Burst, MTU | 751a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 752a7cc081cSVladimir Oltean * ... | Policer 2: Rate, Burst, MTU | 753a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 754a7cc081cSVladimir Oltean * |Port 0 TC 7 |SHARINDX| | Policer 3: Rate, Burst, MTU | 755a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 756a7cc081cSVladimir Oltean * |Port 1 TC 0 |SHARINDX| | Policer 4: Rate, Burst, MTU | 757a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 758a7cc081cSVladimir Oltean * ... | Policer 5: Rate, Burst, MTU | 759a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 760a7cc081cSVladimir Oltean * |Port 1 TC 7 |SHARINDX| | Policer 6: Rate, Burst, MTU | 761a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 762a7cc081cSVladimir Oltean * ... | Policer 7: Rate, Burst, MTU | 763a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 764a7cc081cSVladimir Oltean * |Port 4 TC 7 |SHARINDX| ... 765a7cc081cSVladimir Oltean * +------------+--------+ 766a7cc081cSVladimir Oltean * |Port 0 BCAST|SHARINDX| ... 767a7cc081cSVladimir Oltean * +------------+--------+ 768a7cc081cSVladimir Oltean * |Port 1 BCAST|SHARINDX| ... 769a7cc081cSVladimir Oltean * +------------+--------+ 770a7cc081cSVladimir Oltean * ... ... 771a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 772a7cc081cSVladimir Oltean * |Port 4 BCAST|SHARINDX| | Policer 44: Rate, Burst, MTU | 773a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 774a7cc081cSVladimir Oltean * 775a7cc081cSVladimir Oltean * In this driver, we shall use policers 0-4 as statically alocated port 776a7cc081cSVladimir Oltean * (matchall) policers. So we need to make the SHARINDX for all lookups 777a7cc081cSVladimir Oltean * corresponding to this ingress port (8 VLAN PCP lookups and 1 broadcast 778a7cc081cSVladimir Oltean * lookup) equal. 779a7cc081cSVladimir Oltean * The remaining policers (40) shall be dynamically allocated for flower 780a7cc081cSVladimir Oltean * policers, where the key is either vlan_prio or dst_mac ff:ff:ff:ff:ff:ff. 781a7cc081cSVladimir Oltean */ 7828aa9ebccSVladimir Oltean #define SJA1105_RATE_MBPS(speed) (((speed) * 64000) / 1000) 7838aa9ebccSVladimir Oltean 7848aa9ebccSVladimir Oltean static int sja1105_init_l2_policing(struct sja1105_private *priv) 7858aa9ebccSVladimir Oltean { 7868aa9ebccSVladimir Oltean struct sja1105_l2_policing_entry *policing; 787542043e9SVladimir Oltean struct dsa_switch *ds = priv->ds; 7888aa9ebccSVladimir Oltean struct sja1105_table *table; 789a7cc081cSVladimir Oltean int port, tc; 7908aa9ebccSVladimir Oltean 7918aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_POLICING]; 7928aa9ebccSVladimir Oltean 7938aa9ebccSVladimir Oltean /* Discard previous L2 Policing Table */ 7948aa9ebccSVladimir Oltean if (table->entry_count) { 7958aa9ebccSVladimir Oltean kfree(table->entries); 7968aa9ebccSVladimir Oltean table->entry_count = 0; 7978aa9ebccSVladimir Oltean } 7988aa9ebccSVladimir Oltean 799fd6f2c25SVladimir Oltean table->entries = kcalloc(table->ops->max_entry_count, 8008aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 8018aa9ebccSVladimir Oltean if (!table->entries) 8028aa9ebccSVladimir Oltean return -ENOMEM; 8038aa9ebccSVladimir Oltean 804fd6f2c25SVladimir Oltean table->entry_count = table->ops->max_entry_count; 8058aa9ebccSVladimir Oltean 8068aa9ebccSVladimir Oltean policing = table->entries; 8078aa9ebccSVladimir Oltean 808a7cc081cSVladimir Oltean /* Setup shared indices for the matchall policers */ 809542043e9SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 81038fbe91fSVladimir Oltean int mcast = (ds->num_ports * (SJA1105_NUM_TC + 1)) + port; 811542043e9SVladimir Oltean int bcast = (ds->num_ports * SJA1105_NUM_TC) + port; 812a7cc081cSVladimir Oltean 813a7cc081cSVladimir Oltean for (tc = 0; tc < SJA1105_NUM_TC; tc++) 814a7cc081cSVladimir Oltean policing[port * SJA1105_NUM_TC + tc].sharindx = port; 815a7cc081cSVladimir Oltean 816a7cc081cSVladimir Oltean policing[bcast].sharindx = port; 81738fbe91fSVladimir Oltean /* Only SJA1110 has multicast policers */ 81838fbe91fSVladimir Oltean if (mcast <= table->ops->max_entry_count) 81938fbe91fSVladimir Oltean policing[mcast].sharindx = port; 820a7cc081cSVladimir Oltean } 821a7cc081cSVladimir Oltean 822a7cc081cSVladimir Oltean /* Setup the matchall policer parameters */ 823542043e9SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 824c279c726SVladimir Oltean int mtu = VLAN_ETH_FRAME_LEN + ETH_FCS_LEN; 825c279c726SVladimir Oltean 826a7cc081cSVladimir Oltean if (dsa_is_cpu_port(priv->ds, port)) 827c279c726SVladimir Oltean mtu += VLAN_HLEN; 8288aa9ebccSVladimir Oltean 829a7cc081cSVladimir Oltean policing[port].smax = 65535; /* Burst size in bytes */ 830a7cc081cSVladimir Oltean policing[port].rate = SJA1105_RATE_MBPS(1000); 831a7cc081cSVladimir Oltean policing[port].maxlen = mtu; 832a7cc081cSVladimir Oltean policing[port].partition = 0; 8338aa9ebccSVladimir Oltean } 834a7cc081cSVladimir Oltean 8358aa9ebccSVladimir Oltean return 0; 8368aa9ebccSVladimir Oltean } 8378aa9ebccSVladimir Oltean 8385d645df9SVladimir Oltean static int sja1105_static_config_load(struct sja1105_private *priv) 8398aa9ebccSVladimir Oltean { 8408aa9ebccSVladimir Oltean int rc; 8418aa9ebccSVladimir Oltean 8428aa9ebccSVladimir Oltean sja1105_static_config_free(&priv->static_config); 8438aa9ebccSVladimir Oltean rc = sja1105_static_config_init(&priv->static_config, 8448aa9ebccSVladimir Oltean priv->info->static_ops, 8458aa9ebccSVladimir Oltean priv->info->device_id); 8468aa9ebccSVladimir Oltean if (rc) 8478aa9ebccSVladimir Oltean return rc; 8488aa9ebccSVladimir Oltean 8498aa9ebccSVladimir Oltean /* Build static configuration */ 8508aa9ebccSVladimir Oltean rc = sja1105_init_mac_settings(priv); 8518aa9ebccSVladimir Oltean if (rc < 0) 8528aa9ebccSVladimir Oltean return rc; 8535d645df9SVladimir Oltean rc = sja1105_init_mii_settings(priv); 8548aa9ebccSVladimir Oltean if (rc < 0) 8558aa9ebccSVladimir Oltean return rc; 8568aa9ebccSVladimir Oltean rc = sja1105_init_static_fdb(priv); 8578aa9ebccSVladimir Oltean if (rc < 0) 8588aa9ebccSVladimir Oltean return rc; 8598aa9ebccSVladimir Oltean rc = sja1105_init_static_vlan(priv); 8608aa9ebccSVladimir Oltean if (rc < 0) 8618aa9ebccSVladimir Oltean return rc; 8628aa9ebccSVladimir Oltean rc = sja1105_init_l2_lookup_params(priv); 8638aa9ebccSVladimir Oltean if (rc < 0) 8648aa9ebccSVladimir Oltean return rc; 8658aa9ebccSVladimir Oltean rc = sja1105_init_l2_forwarding(priv); 8668aa9ebccSVladimir Oltean if (rc < 0) 8678aa9ebccSVladimir Oltean return rc; 8688aa9ebccSVladimir Oltean rc = sja1105_init_l2_forwarding_params(priv); 8698aa9ebccSVladimir Oltean if (rc < 0) 8708aa9ebccSVladimir Oltean return rc; 8718aa9ebccSVladimir Oltean rc = sja1105_init_l2_policing(priv); 8728aa9ebccSVladimir Oltean if (rc < 0) 8738aa9ebccSVladimir Oltean return rc; 8748aa9ebccSVladimir Oltean rc = sja1105_init_general_params(priv); 8758aa9ebccSVladimir Oltean if (rc < 0) 8768aa9ebccSVladimir Oltean return rc; 87779d5511cSVladimir Oltean rc = sja1105_init_avb_params(priv); 87879d5511cSVladimir Oltean if (rc < 0) 87979d5511cSVladimir Oltean return rc; 8803e77e59bSVladimir Oltean rc = sja1110_init_pcp_remapping(priv); 8813e77e59bSVladimir Oltean if (rc < 0) 8823e77e59bSVladimir Oltean return rc; 8838aa9ebccSVladimir Oltean 8848aa9ebccSVladimir Oltean /* Send initial configuration to hardware via SPI */ 8858aa9ebccSVladimir Oltean return sja1105_static_config_upload(priv); 8868aa9ebccSVladimir Oltean } 8878aa9ebccSVladimir Oltean 88829afb83aSVladimir Oltean static int sja1105_parse_rgmii_delays(struct sja1105_private *priv) 889f5b8631cSVladimir Oltean { 890542043e9SVladimir Oltean struct dsa_switch *ds = priv->ds; 89129afb83aSVladimir Oltean int port; 892f5b8631cSVladimir Oltean 89329afb83aSVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 89429afb83aSVladimir Oltean if (!priv->fixed_link[port]) 895f5b8631cSVladimir Oltean continue; 896f5b8631cSVladimir Oltean 89729afb83aSVladimir Oltean if (priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_RXID || 89829afb83aSVladimir Oltean priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_ID) 89929afb83aSVladimir Oltean priv->rgmii_rx_delay[port] = true; 900f5b8631cSVladimir Oltean 90129afb83aSVladimir Oltean if (priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_TXID || 90229afb83aSVladimir Oltean priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_ID) 90329afb83aSVladimir Oltean priv->rgmii_tx_delay[port] = true; 904f5b8631cSVladimir Oltean 90529afb83aSVladimir Oltean if ((priv->rgmii_rx_delay[port] || priv->rgmii_tx_delay[port]) && 906f5b8631cSVladimir Oltean !priv->info->setup_rgmii_delay) 907f5b8631cSVladimir Oltean return -EINVAL; 908f5b8631cSVladimir Oltean } 909f5b8631cSVladimir Oltean return 0; 910f5b8631cSVladimir Oltean } 911f5b8631cSVladimir Oltean 9128aa9ebccSVladimir Oltean static int sja1105_parse_ports_node(struct sja1105_private *priv, 9138aa9ebccSVladimir Oltean struct device_node *ports_node) 9148aa9ebccSVladimir Oltean { 9158aa9ebccSVladimir Oltean struct device *dev = &priv->spidev->dev; 9168aa9ebccSVladimir Oltean struct device_node *child; 9178aa9ebccSVladimir Oltean 91827afe0d3SVladimir Oltean for_each_available_child_of_node(ports_node, child) { 9198aa9ebccSVladimir Oltean struct device_node *phy_node; 9200c65b2b9SAndrew Lunn phy_interface_t phy_mode; 9218aa9ebccSVladimir Oltean u32 index; 9220c65b2b9SAndrew Lunn int err; 9238aa9ebccSVladimir Oltean 9248aa9ebccSVladimir Oltean /* Get switch port number from DT */ 9258aa9ebccSVladimir Oltean if (of_property_read_u32(child, "reg", &index) < 0) { 9268aa9ebccSVladimir Oltean dev_err(dev, "Port number not defined in device tree " 9278aa9ebccSVladimir Oltean "(property \"reg\")\n"); 9287ba771e3SNishka Dasgupta of_node_put(child); 9298aa9ebccSVladimir Oltean return -ENODEV; 9308aa9ebccSVladimir Oltean } 9318aa9ebccSVladimir Oltean 9328aa9ebccSVladimir Oltean /* Get PHY mode from DT */ 9330c65b2b9SAndrew Lunn err = of_get_phy_mode(child, &phy_mode); 9340c65b2b9SAndrew Lunn if (err) { 9358aa9ebccSVladimir Oltean dev_err(dev, "Failed to read phy-mode or " 9368aa9ebccSVladimir Oltean "phy-interface-type property for port %d\n", 9378aa9ebccSVladimir Oltean index); 9387ba771e3SNishka Dasgupta of_node_put(child); 9398aa9ebccSVladimir Oltean return -ENODEV; 9408aa9ebccSVladimir Oltean } 9418aa9ebccSVladimir Oltean 9428aa9ebccSVladimir Oltean phy_node = of_parse_phandle(child, "phy-handle", 0); 9438aa9ebccSVladimir Oltean if (!phy_node) { 9448aa9ebccSVladimir Oltean if (!of_phy_is_fixed_link(child)) { 9458aa9ebccSVladimir Oltean dev_err(dev, "phy-handle or fixed-link " 9468aa9ebccSVladimir Oltean "properties missing!\n"); 9477ba771e3SNishka Dasgupta of_node_put(child); 9488aa9ebccSVladimir Oltean return -ENODEV; 9498aa9ebccSVladimir Oltean } 9508aa9ebccSVladimir Oltean /* phy-handle is missing, but fixed-link isn't. 9518aa9ebccSVladimir Oltean * So it's a fixed link. Default to PHY role. 9528aa9ebccSVladimir Oltean */ 95329afb83aSVladimir Oltean priv->fixed_link[index] = true; 9548aa9ebccSVladimir Oltean } else { 9558aa9ebccSVladimir Oltean of_node_put(phy_node); 9568aa9ebccSVladimir Oltean } 9578aa9ebccSVladimir Oltean 958bf4edf4aSVladimir Oltean priv->phy_mode[index] = phy_mode; 9598aa9ebccSVladimir Oltean } 9608aa9ebccSVladimir Oltean 9618aa9ebccSVladimir Oltean return 0; 9628aa9ebccSVladimir Oltean } 9638aa9ebccSVladimir Oltean 9645d645df9SVladimir Oltean static int sja1105_parse_dt(struct sja1105_private *priv) 9658aa9ebccSVladimir Oltean { 9668aa9ebccSVladimir Oltean struct device *dev = &priv->spidev->dev; 9678aa9ebccSVladimir Oltean struct device_node *switch_node = dev->of_node; 9688aa9ebccSVladimir Oltean struct device_node *ports_node; 9698aa9ebccSVladimir Oltean int rc; 9708aa9ebccSVladimir Oltean 9718aa9ebccSVladimir Oltean ports_node = of_get_child_by_name(switch_node, "ports"); 97215074a36SVladimir Oltean if (!ports_node) 97315074a36SVladimir Oltean ports_node = of_get_child_by_name(switch_node, "ethernet-ports"); 9748aa9ebccSVladimir Oltean if (!ports_node) { 9758aa9ebccSVladimir Oltean dev_err(dev, "Incorrect bindings: absent \"ports\" node\n"); 9768aa9ebccSVladimir Oltean return -ENODEV; 9778aa9ebccSVladimir Oltean } 9788aa9ebccSVladimir Oltean 9795d645df9SVladimir Oltean rc = sja1105_parse_ports_node(priv, ports_node); 9808aa9ebccSVladimir Oltean of_node_put(ports_node); 9818aa9ebccSVladimir Oltean 9828aa9ebccSVladimir Oltean return rc; 9838aa9ebccSVladimir Oltean } 9848aa9ebccSVladimir Oltean 9854c7ee010SVladimir Oltean static int sja1105_sgmii_read(struct sja1105_private *priv, int port, int mmd, 98684db00f2SVladimir Oltean int pcs_reg) 987ffe10e67SVladimir Oltean { 9884c7ee010SVladimir Oltean u64 addr = (mmd << 16) | pcs_reg; 989ffe10e67SVladimir Oltean u32 val; 990ffe10e67SVladimir Oltean int rc; 991ffe10e67SVladimir Oltean 99284db00f2SVladimir Oltean if (port != SJA1105_SGMII_PORT) 99384db00f2SVladimir Oltean return -ENODEV; 99484db00f2SVladimir Oltean 9954c7ee010SVladimir Oltean rc = sja1105_xfer_u32(priv, SPI_READ, addr, &val, NULL); 996ffe10e67SVladimir Oltean if (rc < 0) 997ffe10e67SVladimir Oltean return rc; 998ffe10e67SVladimir Oltean 999ffe10e67SVladimir Oltean return val; 1000ffe10e67SVladimir Oltean } 1001ffe10e67SVladimir Oltean 10024c7ee010SVladimir Oltean static int sja1105_sgmii_write(struct sja1105_private *priv, int port, int mmd, 100384db00f2SVladimir Oltean int pcs_reg, u16 pcs_val) 1004ffe10e67SVladimir Oltean { 10054c7ee010SVladimir Oltean u64 addr = (mmd << 16) | pcs_reg; 1006ffe10e67SVladimir Oltean u32 val = pcs_val; 1007ffe10e67SVladimir Oltean int rc; 1008ffe10e67SVladimir Oltean 100984db00f2SVladimir Oltean if (port != SJA1105_SGMII_PORT) 101084db00f2SVladimir Oltean return -ENODEV; 101184db00f2SVladimir Oltean 10124c7ee010SVladimir Oltean rc = sja1105_xfer_u32(priv, SPI_WRITE, addr, &val, NULL); 1013ffe10e67SVladimir Oltean if (rc < 0) 1014ffe10e67SVladimir Oltean return rc; 1015ffe10e67SVladimir Oltean 1016ffe10e67SVladimir Oltean return val; 1017ffe10e67SVladimir Oltean } 1018ffe10e67SVladimir Oltean 101984db00f2SVladimir Oltean static void sja1105_sgmii_pcs_config(struct sja1105_private *priv, int port, 1020ffe10e67SVladimir Oltean bool an_enabled, bool an_master) 1021ffe10e67SVladimir Oltean { 1022ffe10e67SVladimir Oltean u16 ac = SJA1105_AC_AUTONEG_MODE_SGMII; 1023ffe10e67SVladimir Oltean 1024ffe10e67SVladimir Oltean /* DIGITAL_CONTROL_1: Enable vendor-specific MMD1, allow the PHY to 1025ffe10e67SVladimir Oltean * stop the clock during LPI mode, make the MAC reconfigure 1026ffe10e67SVladimir Oltean * autonomously after PCS autoneg is done, flush the internal FIFOs. 1027ffe10e67SVladimir Oltean */ 10284c7ee010SVladimir Oltean sja1105_sgmii_write(priv, port, MDIO_MMD_VEND2, SJA1105_DC1, 102984db00f2SVladimir Oltean SJA1105_DC1_EN_VSMMD1 | 1030ffe10e67SVladimir Oltean SJA1105_DC1_CLOCK_STOP_EN | 1031ffe10e67SVladimir Oltean SJA1105_DC1_MAC_AUTO_SW | 1032ffe10e67SVladimir Oltean SJA1105_DC1_INIT); 1033ffe10e67SVladimir Oltean /* DIGITAL_CONTROL_2: No polarity inversion for TX and RX lanes */ 10344c7ee010SVladimir Oltean sja1105_sgmii_write(priv, port, MDIO_MMD_VEND2, SJA1105_DC2, 103584db00f2SVladimir Oltean SJA1105_DC2_TX_POL_INV_DISABLE); 1036ffe10e67SVladimir Oltean /* AUTONEG_CONTROL: Use SGMII autoneg */ 1037ffe10e67SVladimir Oltean if (an_master) 1038ffe10e67SVladimir Oltean ac |= SJA1105_AC_PHY_MODE | SJA1105_AC_SGMII_LINK; 10394c7ee010SVladimir Oltean sja1105_sgmii_write(priv, port, MDIO_MMD_VEND2, SJA1105_AC, ac); 1040ffe10e67SVladimir Oltean /* BASIC_CONTROL: enable in-band AN now, if requested. Otherwise, 1041ffe10e67SVladimir Oltean * sja1105_sgmii_pcs_force_speed must be called later for the link 1042ffe10e67SVladimir Oltean * to become operational. 1043ffe10e67SVladimir Oltean */ 1044ffe10e67SVladimir Oltean if (an_enabled) 10454c7ee010SVladimir Oltean sja1105_sgmii_write(priv, port, MDIO_MMD_VEND2, MDIO_CTRL1, 1046ffe10e67SVladimir Oltean BMCR_ANENABLE | BMCR_ANRESTART); 1047ffe10e67SVladimir Oltean } 1048ffe10e67SVladimir Oltean 1049ffe10e67SVladimir Oltean static void sja1105_sgmii_pcs_force_speed(struct sja1105_private *priv, 105084db00f2SVladimir Oltean int port, int speed) 1051ffe10e67SVladimir Oltean { 1052ffe10e67SVladimir Oltean int pcs_speed; 1053ffe10e67SVladimir Oltean 1054ffe10e67SVladimir Oltean switch (speed) { 1055ffe10e67SVladimir Oltean case SPEED_1000: 1056ffe10e67SVladimir Oltean pcs_speed = BMCR_SPEED1000; 1057ffe10e67SVladimir Oltean break; 1058ffe10e67SVladimir Oltean case SPEED_100: 1059ffe10e67SVladimir Oltean pcs_speed = BMCR_SPEED100; 1060ffe10e67SVladimir Oltean break; 1061ffe10e67SVladimir Oltean case SPEED_10: 1062ffe10e67SVladimir Oltean pcs_speed = BMCR_SPEED10; 1063ffe10e67SVladimir Oltean break; 1064ffe10e67SVladimir Oltean default: 1065ffe10e67SVladimir Oltean dev_err(priv->ds->dev, "Invalid speed %d\n", speed); 1066ffe10e67SVladimir Oltean return; 1067ffe10e67SVladimir Oltean } 10684c7ee010SVladimir Oltean sja1105_sgmii_write(priv, port, MDIO_MMD_VEND2, MDIO_CTRL1, 10694c7ee010SVladimir Oltean pcs_speed | BMCR_FULLDPLX); 1070ffe10e67SVladimir Oltean } 1071ffe10e67SVladimir Oltean 1072c44d0535SVladimir Oltean /* Convert link speed from SJA1105 to ethtool encoding */ 107341fed17fSVladimir Oltean static int sja1105_port_speed_to_ethtool(struct sja1105_private *priv, 107441fed17fSVladimir Oltean u64 speed) 107541fed17fSVladimir Oltean { 107641fed17fSVladimir Oltean if (speed == priv->info->port_speed[SJA1105_SPEED_10MBPS]) 107741fed17fSVladimir Oltean return SPEED_10; 107841fed17fSVladimir Oltean if (speed == priv->info->port_speed[SJA1105_SPEED_100MBPS]) 107941fed17fSVladimir Oltean return SPEED_100; 108041fed17fSVladimir Oltean if (speed == priv->info->port_speed[SJA1105_SPEED_1000MBPS]) 108141fed17fSVladimir Oltean return SPEED_1000; 108241fed17fSVladimir Oltean if (speed == priv->info->port_speed[SJA1105_SPEED_2500MBPS]) 108341fed17fSVladimir Oltean return SPEED_2500; 108441fed17fSVladimir Oltean return SPEED_UNKNOWN; 108541fed17fSVladimir Oltean } 10868aa9ebccSVladimir Oltean 10878400cff6SVladimir Oltean /* Set link speed in the MAC configuration for a specific port. */ 10888aa9ebccSVladimir Oltean static int sja1105_adjust_port_config(struct sja1105_private *priv, int port, 10898400cff6SVladimir Oltean int speed_mbps) 10908aa9ebccSVladimir Oltean { 10918aa9ebccSVladimir Oltean struct sja1105_mac_config_entry *mac; 10928aa9ebccSVladimir Oltean struct device *dev = priv->ds->dev; 109341fed17fSVladimir Oltean u64 speed; 10948aa9ebccSVladimir Oltean int rc; 10958aa9ebccSVladimir Oltean 10968400cff6SVladimir Oltean /* On P/Q/R/S, one can read from the device via the MAC reconfiguration 10978400cff6SVladimir Oltean * tables. On E/T, MAC reconfig tables are not readable, only writable. 10988400cff6SVladimir Oltean * We have to *know* what the MAC looks like. For the sake of keeping 10998400cff6SVladimir Oltean * the code common, we'll use the static configuration tables as a 11008400cff6SVladimir Oltean * reasonable approximation for both E/T and P/Q/R/S. 11018400cff6SVladimir Oltean */ 11028aa9ebccSVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 11038aa9ebccSVladimir Oltean 1104f4cfcfbdSVladimir Oltean switch (speed_mbps) { 1105c44d0535SVladimir Oltean case SPEED_UNKNOWN: 1106a979a0abSVladimir Oltean /* PHYLINK called sja1105_mac_config() to inform us about 1107a979a0abSVladimir Oltean * the state->interface, but AN has not completed and the 1108a979a0abSVladimir Oltean * speed is not yet valid. UM10944.pdf says that setting 1109a979a0abSVladimir Oltean * SJA1105_SPEED_AUTO at runtime disables the port, so that is 1110a979a0abSVladimir Oltean * ok for power consumption in case AN will never complete - 1111a979a0abSVladimir Oltean * otherwise PHYLINK should come back with a new update. 1112a979a0abSVladimir Oltean */ 111341fed17fSVladimir Oltean speed = priv->info->port_speed[SJA1105_SPEED_AUTO]; 1114f4cfcfbdSVladimir Oltean break; 1115c44d0535SVladimir Oltean case SPEED_10: 111641fed17fSVladimir Oltean speed = priv->info->port_speed[SJA1105_SPEED_10MBPS]; 1117f4cfcfbdSVladimir Oltean break; 1118c44d0535SVladimir Oltean case SPEED_100: 111941fed17fSVladimir Oltean speed = priv->info->port_speed[SJA1105_SPEED_100MBPS]; 1120f4cfcfbdSVladimir Oltean break; 1121c44d0535SVladimir Oltean case SPEED_1000: 112241fed17fSVladimir Oltean speed = priv->info->port_speed[SJA1105_SPEED_1000MBPS]; 1123f4cfcfbdSVladimir Oltean break; 1124f4cfcfbdSVladimir Oltean default: 11258aa9ebccSVladimir Oltean dev_err(dev, "Invalid speed %iMbps\n", speed_mbps); 11268aa9ebccSVladimir Oltean return -EINVAL; 11278aa9ebccSVladimir Oltean } 11288aa9ebccSVladimir Oltean 11298400cff6SVladimir Oltean /* Overwrite SJA1105_SPEED_AUTO from the static MAC configuration 11308400cff6SVladimir Oltean * table, since this will be used for the clocking setup, and we no 11318400cff6SVladimir Oltean * longer need to store it in the static config (already told hardware 11328400cff6SVladimir Oltean * we want auto during upload phase). 1133ffe10e67SVladimir Oltean * Actually for the SGMII port, the MAC is fixed at 1 Gbps and 1134ffe10e67SVladimir Oltean * we need to configure the PCS only (if even that). 11358aa9ebccSVladimir Oltean */ 113691a05078SVladimir Oltean if (priv->phy_mode[port] == PHY_INTERFACE_MODE_SGMII) 113741fed17fSVladimir Oltean mac[port].speed = priv->info->port_speed[SJA1105_SPEED_1000MBPS]; 1138ffe10e67SVladimir Oltean else 11398aa9ebccSVladimir Oltean mac[port].speed = speed; 11408aa9ebccSVladimir Oltean 11418aa9ebccSVladimir Oltean /* Write to the dynamic reconfiguration tables */ 11428400cff6SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, 11438400cff6SVladimir Oltean &mac[port], true); 11448aa9ebccSVladimir Oltean if (rc < 0) { 11458aa9ebccSVladimir Oltean dev_err(dev, "Failed to write MAC config: %d\n", rc); 11468aa9ebccSVladimir Oltean return rc; 11478aa9ebccSVladimir Oltean } 11488aa9ebccSVladimir Oltean 11498aa9ebccSVladimir Oltean /* Reconfigure the PLLs for the RGMII interfaces (required 125 MHz at 11508aa9ebccSVladimir Oltean * gigabit, 25 MHz at 100 Mbps and 2.5 MHz at 10 Mbps). For MII and 11518aa9ebccSVladimir Oltean * RMII no change of the clock setup is required. Actually, changing 11528aa9ebccSVladimir Oltean * the clock setup does interrupt the clock signal for a certain time 11538aa9ebccSVladimir Oltean * which causes trouble for all PHYs relying on this signal. 11548aa9ebccSVladimir Oltean */ 115591a05078SVladimir Oltean if (!phy_interface_mode_is_rgmii(priv->phy_mode[port])) 11568aa9ebccSVladimir Oltean return 0; 11578aa9ebccSVladimir Oltean 11588aa9ebccSVladimir Oltean return sja1105_clocking_setup_port(priv, port); 11598aa9ebccSVladimir Oltean } 11608aa9ebccSVladimir Oltean 116139710229SVladimir Oltean /* The SJA1105 MAC programming model is through the static config (the xMII 116239710229SVladimir Oltean * Mode table cannot be dynamically reconfigured), and we have to program 116339710229SVladimir Oltean * that early (earlier than PHYLINK calls us, anyway). 116439710229SVladimir Oltean * So just error out in case the connected PHY attempts to change the initial 116539710229SVladimir Oltean * system interface MII protocol from what is defined in the DT, at least for 116639710229SVladimir Oltean * now. 116739710229SVladimir Oltean */ 116839710229SVladimir Oltean static bool sja1105_phy_mode_mismatch(struct sja1105_private *priv, int port, 116939710229SVladimir Oltean phy_interface_t interface) 117039710229SVladimir Oltean { 1171bf4edf4aSVladimir Oltean return priv->phy_mode[port] != interface; 117239710229SVladimir Oltean } 117339710229SVladimir Oltean 1174af7cd036SVladimir Oltean static void sja1105_mac_config(struct dsa_switch *ds, int port, 1175ffe10e67SVladimir Oltean unsigned int mode, 1176af7cd036SVladimir Oltean const struct phylink_link_state *state) 11778aa9ebccSVladimir Oltean { 11788aa9ebccSVladimir Oltean struct sja1105_private *priv = ds->priv; 117991a05078SVladimir Oltean bool is_sgmii; 118091a05078SVladimir Oltean 118191a05078SVladimir Oltean is_sgmii = (state->interface == PHY_INTERFACE_MODE_SGMII); 11828aa9ebccSVladimir Oltean 1183ec8582d1SVladimir Oltean if (sja1105_phy_mode_mismatch(priv, port, state->interface)) { 1184ec8582d1SVladimir Oltean dev_err(ds->dev, "Changing PHY mode to %s not supported!\n", 1185ec8582d1SVladimir Oltean phy_modes(state->interface)); 118639710229SVladimir Oltean return; 1187ec8582d1SVladimir Oltean } 118839710229SVladimir Oltean 1189ffe10e67SVladimir Oltean if (phylink_autoneg_inband(mode) && !is_sgmii) { 11909f971573SVladimir Oltean dev_err(ds->dev, "In-band AN not supported!\n"); 11919f971573SVladimir Oltean return; 11929f971573SVladimir Oltean } 1193ffe10e67SVladimir Oltean 1194ffe10e67SVladimir Oltean if (is_sgmii) 119584db00f2SVladimir Oltean sja1105_sgmii_pcs_config(priv, port, 119684db00f2SVladimir Oltean phylink_autoneg_inband(mode), 1197ffe10e67SVladimir Oltean false); 11988400cff6SVladimir Oltean } 11998400cff6SVladimir Oltean 12008400cff6SVladimir Oltean static void sja1105_mac_link_down(struct dsa_switch *ds, int port, 12018400cff6SVladimir Oltean unsigned int mode, 12028400cff6SVladimir Oltean phy_interface_t interface) 12038400cff6SVladimir Oltean { 12048400cff6SVladimir Oltean sja1105_inhibit_tx(ds->priv, BIT(port), true); 12058400cff6SVladimir Oltean } 12068400cff6SVladimir Oltean 12078400cff6SVladimir Oltean static void sja1105_mac_link_up(struct dsa_switch *ds, int port, 12088400cff6SVladimir Oltean unsigned int mode, 12098400cff6SVladimir Oltean phy_interface_t interface, 12105b502a7bSRussell King struct phy_device *phydev, 12115b502a7bSRussell King int speed, int duplex, 12125b502a7bSRussell King bool tx_pause, bool rx_pause) 12138400cff6SVladimir Oltean { 1214ec8582d1SVladimir Oltean struct sja1105_private *priv = ds->priv; 1215ec8582d1SVladimir Oltean 1216ec8582d1SVladimir Oltean sja1105_adjust_port_config(priv, port, speed); 1217ec8582d1SVladimir Oltean 121891a05078SVladimir Oltean if (priv->phy_mode[port] == PHY_INTERFACE_MODE_SGMII && 121991a05078SVladimir Oltean !phylink_autoneg_inband(mode)) 122084db00f2SVladimir Oltean sja1105_sgmii_pcs_force_speed(priv, port, speed); 1221ffe10e67SVladimir Oltean 1222ec8582d1SVladimir Oltean sja1105_inhibit_tx(priv, BIT(port), false); 12238aa9ebccSVladimir Oltean } 12248aa9ebccSVladimir Oltean 1225ad9f299aSVladimir Oltean static void sja1105_phylink_validate(struct dsa_switch *ds, int port, 1226ad9f299aSVladimir Oltean unsigned long *supported, 1227ad9f299aSVladimir Oltean struct phylink_link_state *state) 1228ad9f299aSVladimir Oltean { 1229ad9f299aSVladimir Oltean /* Construct a new mask which exhaustively contains all link features 1230ad9f299aSVladimir Oltean * supported by the MAC, and then apply that (logical AND) to what will 1231ad9f299aSVladimir Oltean * be sent to the PHY for "marketing". 1232ad9f299aSVladimir Oltean */ 1233ad9f299aSVladimir Oltean __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, }; 1234ad9f299aSVladimir Oltean struct sja1105_private *priv = ds->priv; 1235ad9f299aSVladimir Oltean struct sja1105_xmii_params_entry *mii; 1236ad9f299aSVladimir Oltean 1237ad9f299aSVladimir Oltean mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries; 1238ad9f299aSVladimir Oltean 123939710229SVladimir Oltean /* include/linux/phylink.h says: 124039710229SVladimir Oltean * When @state->interface is %PHY_INTERFACE_MODE_NA, phylink 124139710229SVladimir Oltean * expects the MAC driver to return all supported link modes. 124239710229SVladimir Oltean */ 124339710229SVladimir Oltean if (state->interface != PHY_INTERFACE_MODE_NA && 124439710229SVladimir Oltean sja1105_phy_mode_mismatch(priv, port, state->interface)) { 124539710229SVladimir Oltean bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS); 124639710229SVladimir Oltean return; 124739710229SVladimir Oltean } 124839710229SVladimir Oltean 1249ad9f299aSVladimir Oltean /* The MAC does not support pause frames, and also doesn't 1250ad9f299aSVladimir Oltean * support half-duplex traffic modes. 1251ad9f299aSVladimir Oltean */ 1252ad9f299aSVladimir Oltean phylink_set(mask, Autoneg); 1253ad9f299aSVladimir Oltean phylink_set(mask, MII); 1254ad9f299aSVladimir Oltean phylink_set(mask, 10baseT_Full); 1255ad9f299aSVladimir Oltean phylink_set(mask, 100baseT_Full); 1256ca68e138SOleksij Rempel phylink_set(mask, 100baseT1_Full); 1257ffe10e67SVladimir Oltean if (mii->xmii_mode[port] == XMII_MODE_RGMII || 1258ffe10e67SVladimir Oltean mii->xmii_mode[port] == XMII_MODE_SGMII) 1259ad9f299aSVladimir Oltean phylink_set(mask, 1000baseT_Full); 1260ad9f299aSVladimir Oltean 1261ad9f299aSVladimir Oltean bitmap_and(supported, supported, mask, __ETHTOOL_LINK_MODE_MASK_NBITS); 1262ad9f299aSVladimir Oltean bitmap_and(state->advertising, state->advertising, mask, 1263ad9f299aSVladimir Oltean __ETHTOOL_LINK_MODE_MASK_NBITS); 1264ad9f299aSVladimir Oltean } 1265ad9f299aSVladimir Oltean 1266ffe10e67SVladimir Oltean static int sja1105_mac_pcs_get_state(struct dsa_switch *ds, int port, 1267ffe10e67SVladimir Oltean struct phylink_link_state *state) 1268ffe10e67SVladimir Oltean { 1269ffe10e67SVladimir Oltean struct sja1105_private *priv = ds->priv; 1270ffe10e67SVladimir Oltean int ais; 1271ffe10e67SVladimir Oltean 1272ffe10e67SVladimir Oltean /* Read the vendor-specific AUTONEG_INTR_STATUS register */ 12734c7ee010SVladimir Oltean ais = sja1105_sgmii_read(priv, port, MDIO_MMD_VEND2, SJA1105_AIS); 1274ffe10e67SVladimir Oltean if (ais < 0) 1275ffe10e67SVladimir Oltean return ais; 1276ffe10e67SVladimir Oltean 1277ffe10e67SVladimir Oltean switch (SJA1105_AIS_SPEED(ais)) { 1278ffe10e67SVladimir Oltean case 0: 1279ffe10e67SVladimir Oltean state->speed = SPEED_10; 1280ffe10e67SVladimir Oltean break; 1281ffe10e67SVladimir Oltean case 1: 1282ffe10e67SVladimir Oltean state->speed = SPEED_100; 1283ffe10e67SVladimir Oltean break; 1284ffe10e67SVladimir Oltean case 2: 1285ffe10e67SVladimir Oltean state->speed = SPEED_1000; 1286ffe10e67SVladimir Oltean break; 1287ffe10e67SVladimir Oltean default: 1288ffe10e67SVladimir Oltean dev_err(ds->dev, "Invalid SGMII PCS speed %lu\n", 1289ffe10e67SVladimir Oltean SJA1105_AIS_SPEED(ais)); 1290ffe10e67SVladimir Oltean } 1291ffe10e67SVladimir Oltean state->duplex = SJA1105_AIS_DUPLEX_MODE(ais); 1292ffe10e67SVladimir Oltean state->an_complete = SJA1105_AIS_COMPLETE(ais); 1293ffe10e67SVladimir Oltean state->link = SJA1105_AIS_LINK_STATUS(ais); 1294ffe10e67SVladimir Oltean 1295ffe10e67SVladimir Oltean return 0; 1296ffe10e67SVladimir Oltean } 1297ffe10e67SVladimir Oltean 129860f6053fSVladimir Oltean static int 129960f6053fSVladimir Oltean sja1105_find_static_fdb_entry(struct sja1105_private *priv, int port, 130060f6053fSVladimir Oltean const struct sja1105_l2_lookup_entry *requested) 130160f6053fSVladimir Oltean { 130260f6053fSVladimir Oltean struct sja1105_l2_lookup_entry *l2_lookup; 130360f6053fSVladimir Oltean struct sja1105_table *table; 130460f6053fSVladimir Oltean int i; 130560f6053fSVladimir Oltean 130660f6053fSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; 130760f6053fSVladimir Oltean l2_lookup = table->entries; 130860f6053fSVladimir Oltean 130960f6053fSVladimir Oltean for (i = 0; i < table->entry_count; i++) 131060f6053fSVladimir Oltean if (l2_lookup[i].macaddr == requested->macaddr && 131160f6053fSVladimir Oltean l2_lookup[i].vlanid == requested->vlanid && 131260f6053fSVladimir Oltean l2_lookup[i].destports & BIT(port)) 131360f6053fSVladimir Oltean return i; 131460f6053fSVladimir Oltean 131560f6053fSVladimir Oltean return -1; 131660f6053fSVladimir Oltean } 131760f6053fSVladimir Oltean 131860f6053fSVladimir Oltean /* We want FDB entries added statically through the bridge command to persist 131960f6053fSVladimir Oltean * across switch resets, which are a common thing during normal SJA1105 132060f6053fSVladimir Oltean * operation. So we have to back them up in the static configuration tables 132160f6053fSVladimir Oltean * and hence apply them on next static config upload... yay! 132260f6053fSVladimir Oltean */ 132360f6053fSVladimir Oltean static int 132460f6053fSVladimir Oltean sja1105_static_fdb_change(struct sja1105_private *priv, int port, 132560f6053fSVladimir Oltean const struct sja1105_l2_lookup_entry *requested, 132660f6053fSVladimir Oltean bool keep) 132760f6053fSVladimir Oltean { 132860f6053fSVladimir Oltean struct sja1105_l2_lookup_entry *l2_lookup; 132960f6053fSVladimir Oltean struct sja1105_table *table; 133060f6053fSVladimir Oltean int rc, match; 133160f6053fSVladimir Oltean 133260f6053fSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; 133360f6053fSVladimir Oltean 133460f6053fSVladimir Oltean match = sja1105_find_static_fdb_entry(priv, port, requested); 133560f6053fSVladimir Oltean if (match < 0) { 133660f6053fSVladimir Oltean /* Can't delete a missing entry. */ 133760f6053fSVladimir Oltean if (!keep) 133860f6053fSVladimir Oltean return 0; 133960f6053fSVladimir Oltean 134060f6053fSVladimir Oltean /* No match => new entry */ 134160f6053fSVladimir Oltean rc = sja1105_table_resize(table, table->entry_count + 1); 134260f6053fSVladimir Oltean if (rc) 134360f6053fSVladimir Oltean return rc; 134460f6053fSVladimir Oltean 134560f6053fSVladimir Oltean match = table->entry_count - 1; 134660f6053fSVladimir Oltean } 134760f6053fSVladimir Oltean 134860f6053fSVladimir Oltean /* Assign pointer after the resize (it may be new memory) */ 134960f6053fSVladimir Oltean l2_lookup = table->entries; 135060f6053fSVladimir Oltean 135160f6053fSVladimir Oltean /* We have a match. 135260f6053fSVladimir Oltean * If the job was to add this FDB entry, it's already done (mostly 135360f6053fSVladimir Oltean * anyway, since the port forwarding mask may have changed, case in 135460f6053fSVladimir Oltean * which we update it). 135560f6053fSVladimir Oltean * Otherwise we have to delete it. 135660f6053fSVladimir Oltean */ 135760f6053fSVladimir Oltean if (keep) { 135860f6053fSVladimir Oltean l2_lookup[match] = *requested; 135960f6053fSVladimir Oltean return 0; 136060f6053fSVladimir Oltean } 136160f6053fSVladimir Oltean 136260f6053fSVladimir Oltean /* To remove, the strategy is to overwrite the element with 136360f6053fSVladimir Oltean * the last one, and then reduce the array size by 1 136460f6053fSVladimir Oltean */ 136560f6053fSVladimir Oltean l2_lookup[match] = l2_lookup[table->entry_count - 1]; 136660f6053fSVladimir Oltean return sja1105_table_resize(table, table->entry_count - 1); 136760f6053fSVladimir Oltean } 136860f6053fSVladimir Oltean 1369291d1e72SVladimir Oltean /* First-generation switches have a 4-way set associative TCAM that 1370291d1e72SVladimir Oltean * holds the FDB entries. An FDB index spans from 0 to 1023 and is comprised of 1371291d1e72SVladimir Oltean * a "bin" (grouping of 4 entries) and a "way" (an entry within a bin). 1372291d1e72SVladimir Oltean * For the placement of a newly learnt FDB entry, the switch selects the bin 1373291d1e72SVladimir Oltean * based on a hash function, and the way within that bin incrementally. 1374291d1e72SVladimir Oltean */ 137509c1b412SVladimir Oltean static int sja1105et_fdb_index(int bin, int way) 1376291d1e72SVladimir Oltean { 1377291d1e72SVladimir Oltean return bin * SJA1105ET_FDB_BIN_SIZE + way; 1378291d1e72SVladimir Oltean } 1379291d1e72SVladimir Oltean 13809dfa6911SVladimir Oltean static int sja1105et_is_fdb_entry_in_bin(struct sja1105_private *priv, int bin, 1381291d1e72SVladimir Oltean const u8 *addr, u16 vid, 1382291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry *match, 1383291d1e72SVladimir Oltean int *last_unused) 1384291d1e72SVladimir Oltean { 1385291d1e72SVladimir Oltean int way; 1386291d1e72SVladimir Oltean 1387291d1e72SVladimir Oltean for (way = 0; way < SJA1105ET_FDB_BIN_SIZE; way++) { 1388291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 1389291d1e72SVladimir Oltean int index = sja1105et_fdb_index(bin, way); 1390291d1e72SVladimir Oltean 1391291d1e72SVladimir Oltean /* Skip unused entries, optionally marking them 1392291d1e72SVladimir Oltean * into the return value 1393291d1e72SVladimir Oltean */ 1394291d1e72SVladimir Oltean if (sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 1395291d1e72SVladimir Oltean index, &l2_lookup)) { 1396291d1e72SVladimir Oltean if (last_unused) 1397291d1e72SVladimir Oltean *last_unused = way; 1398291d1e72SVladimir Oltean continue; 1399291d1e72SVladimir Oltean } 1400291d1e72SVladimir Oltean 1401291d1e72SVladimir Oltean if (l2_lookup.macaddr == ether_addr_to_u64(addr) && 1402291d1e72SVladimir Oltean l2_lookup.vlanid == vid) { 1403291d1e72SVladimir Oltean if (match) 1404291d1e72SVladimir Oltean *match = l2_lookup; 1405291d1e72SVladimir Oltean return way; 1406291d1e72SVladimir Oltean } 1407291d1e72SVladimir Oltean } 1408291d1e72SVladimir Oltean /* Return an invalid entry index if not found */ 1409291d1e72SVladimir Oltean return -1; 1410291d1e72SVladimir Oltean } 1411291d1e72SVladimir Oltean 14129dfa6911SVladimir Oltean int sja1105et_fdb_add(struct dsa_switch *ds, int port, 1413291d1e72SVladimir Oltean const unsigned char *addr, u16 vid) 1414291d1e72SVladimir Oltean { 1415291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 1416291d1e72SVladimir Oltean struct sja1105_private *priv = ds->priv; 1417291d1e72SVladimir Oltean struct device *dev = ds->dev; 1418291d1e72SVladimir Oltean int last_unused = -1; 141960f6053fSVladimir Oltean int bin, way, rc; 1420291d1e72SVladimir Oltean 14219dfa6911SVladimir Oltean bin = sja1105et_fdb_hash(priv, addr, vid); 1422291d1e72SVladimir Oltean 14239dfa6911SVladimir Oltean way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid, 1424291d1e72SVladimir Oltean &l2_lookup, &last_unused); 1425291d1e72SVladimir Oltean if (way >= 0) { 1426291d1e72SVladimir Oltean /* We have an FDB entry. Is our port in the destination 1427291d1e72SVladimir Oltean * mask? If yes, we need to do nothing. If not, we need 1428291d1e72SVladimir Oltean * to rewrite the entry by adding this port to it. 1429291d1e72SVladimir Oltean */ 1430291d1e72SVladimir Oltean if (l2_lookup.destports & BIT(port)) 1431291d1e72SVladimir Oltean return 0; 1432291d1e72SVladimir Oltean l2_lookup.destports |= BIT(port); 1433291d1e72SVladimir Oltean } else { 1434291d1e72SVladimir Oltean int index = sja1105et_fdb_index(bin, way); 1435291d1e72SVladimir Oltean 1436291d1e72SVladimir Oltean /* We don't have an FDB entry. We construct a new one and 1437291d1e72SVladimir Oltean * try to find a place for it within the FDB table. 1438291d1e72SVladimir Oltean */ 1439291d1e72SVladimir Oltean l2_lookup.macaddr = ether_addr_to_u64(addr); 1440291d1e72SVladimir Oltean l2_lookup.destports = BIT(port); 1441291d1e72SVladimir Oltean l2_lookup.vlanid = vid; 1442291d1e72SVladimir Oltean 1443291d1e72SVladimir Oltean if (last_unused >= 0) { 1444291d1e72SVladimir Oltean way = last_unused; 1445291d1e72SVladimir Oltean } else { 1446291d1e72SVladimir Oltean /* Bin is full, need to evict somebody. 1447291d1e72SVladimir Oltean * Choose victim at random. If you get these messages 1448291d1e72SVladimir Oltean * often, you may need to consider changing the 1449291d1e72SVladimir Oltean * distribution function: 1450291d1e72SVladimir Oltean * static_config[BLK_IDX_L2_LOOKUP_PARAMS].entries->poly 1451291d1e72SVladimir Oltean */ 1452291d1e72SVladimir Oltean get_random_bytes(&way, sizeof(u8)); 1453291d1e72SVladimir Oltean way %= SJA1105ET_FDB_BIN_SIZE; 1454291d1e72SVladimir Oltean dev_warn(dev, "Warning, FDB bin %d full while adding entry for %pM. Evicting entry %u.\n", 1455291d1e72SVladimir Oltean bin, addr, way); 1456291d1e72SVladimir Oltean /* Evict entry */ 1457291d1e72SVladimir Oltean sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 1458291d1e72SVladimir Oltean index, NULL, false); 1459291d1e72SVladimir Oltean } 1460291d1e72SVladimir Oltean } 1461291d1e72SVladimir Oltean l2_lookup.index = sja1105et_fdb_index(bin, way); 1462291d1e72SVladimir Oltean 146360f6053fSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 1464291d1e72SVladimir Oltean l2_lookup.index, &l2_lookup, 1465291d1e72SVladimir Oltean true); 146660f6053fSVladimir Oltean if (rc < 0) 146760f6053fSVladimir Oltean return rc; 146860f6053fSVladimir Oltean 146960f6053fSVladimir Oltean return sja1105_static_fdb_change(priv, port, &l2_lookup, true); 1470291d1e72SVladimir Oltean } 1471291d1e72SVladimir Oltean 14729dfa6911SVladimir Oltean int sja1105et_fdb_del(struct dsa_switch *ds, int port, 1473291d1e72SVladimir Oltean const unsigned char *addr, u16 vid) 1474291d1e72SVladimir Oltean { 1475291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 1476291d1e72SVladimir Oltean struct sja1105_private *priv = ds->priv; 147760f6053fSVladimir Oltean int index, bin, way, rc; 1478291d1e72SVladimir Oltean bool keep; 1479291d1e72SVladimir Oltean 14809dfa6911SVladimir Oltean bin = sja1105et_fdb_hash(priv, addr, vid); 14819dfa6911SVladimir Oltean way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid, 1482291d1e72SVladimir Oltean &l2_lookup, NULL); 1483291d1e72SVladimir Oltean if (way < 0) 1484291d1e72SVladimir Oltean return 0; 1485291d1e72SVladimir Oltean index = sja1105et_fdb_index(bin, way); 1486291d1e72SVladimir Oltean 1487291d1e72SVladimir Oltean /* We have an FDB entry. Is our port in the destination mask? If yes, 1488291d1e72SVladimir Oltean * we need to remove it. If the resulting port mask becomes empty, we 1489291d1e72SVladimir Oltean * need to completely evict the FDB entry. 1490291d1e72SVladimir Oltean * Otherwise we just write it back. 1491291d1e72SVladimir Oltean */ 1492291d1e72SVladimir Oltean l2_lookup.destports &= ~BIT(port); 14937752e937SVladimir Oltean 1494291d1e72SVladimir Oltean if (l2_lookup.destports) 1495291d1e72SVladimir Oltean keep = true; 1496291d1e72SVladimir Oltean else 1497291d1e72SVladimir Oltean keep = false; 1498291d1e72SVladimir Oltean 149960f6053fSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 1500291d1e72SVladimir Oltean index, &l2_lookup, keep); 150160f6053fSVladimir Oltean if (rc < 0) 150260f6053fSVladimir Oltean return rc; 150360f6053fSVladimir Oltean 150460f6053fSVladimir Oltean return sja1105_static_fdb_change(priv, port, &l2_lookup, keep); 1505291d1e72SVladimir Oltean } 1506291d1e72SVladimir Oltean 15079dfa6911SVladimir Oltean int sja1105pqrs_fdb_add(struct dsa_switch *ds, int port, 15089dfa6911SVladimir Oltean const unsigned char *addr, u16 vid) 15099dfa6911SVladimir Oltean { 15101da73821SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 15111da73821SVladimir Oltean struct sja1105_private *priv = ds->priv; 15121da73821SVladimir Oltean int rc, i; 15131da73821SVladimir Oltean 15141da73821SVladimir Oltean /* Search for an existing entry in the FDB table */ 15151da73821SVladimir Oltean l2_lookup.macaddr = ether_addr_to_u64(addr); 15161da73821SVladimir Oltean l2_lookup.vlanid = vid; 15171da73821SVladimir Oltean l2_lookup.iotag = SJA1105_S_TAG; 15181da73821SVladimir Oltean l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0); 15197f14937fSVladimir Oltean if (priv->vlan_state != SJA1105_VLAN_UNAWARE) { 15201da73821SVladimir Oltean l2_lookup.mask_vlanid = VLAN_VID_MASK; 15211da73821SVladimir Oltean l2_lookup.mask_iotag = BIT(0); 15226d7c7d94SVladimir Oltean } else { 15236d7c7d94SVladimir Oltean l2_lookup.mask_vlanid = 0; 15246d7c7d94SVladimir Oltean l2_lookup.mask_iotag = 0; 15256d7c7d94SVladimir Oltean } 15261da73821SVladimir Oltean l2_lookup.destports = BIT(port); 15271da73821SVladimir Oltean 15281da73821SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 15291da73821SVladimir Oltean SJA1105_SEARCH, &l2_lookup); 15301da73821SVladimir Oltean if (rc == 0) { 15311da73821SVladimir Oltean /* Found and this port is already in the entry's 15321da73821SVladimir Oltean * port mask => job done 15331da73821SVladimir Oltean */ 15341da73821SVladimir Oltean if (l2_lookup.destports & BIT(port)) 15351da73821SVladimir Oltean return 0; 15361da73821SVladimir Oltean /* l2_lookup.index is populated by the switch in case it 15371da73821SVladimir Oltean * found something. 15381da73821SVladimir Oltean */ 15391da73821SVladimir Oltean l2_lookup.destports |= BIT(port); 15401da73821SVladimir Oltean goto skip_finding_an_index; 15411da73821SVladimir Oltean } 15421da73821SVladimir Oltean 15431da73821SVladimir Oltean /* Not found, so try to find an unused spot in the FDB. 15441da73821SVladimir Oltean * This is slightly inefficient because the strategy is knock-knock at 15451da73821SVladimir Oltean * every possible position from 0 to 1023. 15461da73821SVladimir Oltean */ 15471da73821SVladimir Oltean for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) { 15481da73821SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 15491da73821SVladimir Oltean i, NULL); 15501da73821SVladimir Oltean if (rc < 0) 15511da73821SVladimir Oltean break; 15521da73821SVladimir Oltean } 15531da73821SVladimir Oltean if (i == SJA1105_MAX_L2_LOOKUP_COUNT) { 15541da73821SVladimir Oltean dev_err(ds->dev, "FDB is full, cannot add entry.\n"); 15551da73821SVladimir Oltean return -EINVAL; 15561da73821SVladimir Oltean } 155717ae6555SVladimir Oltean l2_lookup.lockeds = true; 15581da73821SVladimir Oltean l2_lookup.index = i; 15591da73821SVladimir Oltean 15601da73821SVladimir Oltean skip_finding_an_index: 156160f6053fSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 15621da73821SVladimir Oltean l2_lookup.index, &l2_lookup, 15631da73821SVladimir Oltean true); 156460f6053fSVladimir Oltean if (rc < 0) 156560f6053fSVladimir Oltean return rc; 156660f6053fSVladimir Oltean 156760f6053fSVladimir Oltean return sja1105_static_fdb_change(priv, port, &l2_lookup, true); 15689dfa6911SVladimir Oltean } 15699dfa6911SVladimir Oltean 15709dfa6911SVladimir Oltean int sja1105pqrs_fdb_del(struct dsa_switch *ds, int port, 15719dfa6911SVladimir Oltean const unsigned char *addr, u16 vid) 15729dfa6911SVladimir Oltean { 15731da73821SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 15741da73821SVladimir Oltean struct sja1105_private *priv = ds->priv; 15751da73821SVladimir Oltean bool keep; 15761da73821SVladimir Oltean int rc; 15771da73821SVladimir Oltean 15781da73821SVladimir Oltean l2_lookup.macaddr = ether_addr_to_u64(addr); 15791da73821SVladimir Oltean l2_lookup.vlanid = vid; 15801da73821SVladimir Oltean l2_lookup.iotag = SJA1105_S_TAG; 15811da73821SVladimir Oltean l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0); 15827f14937fSVladimir Oltean if (priv->vlan_state != SJA1105_VLAN_UNAWARE) { 15831da73821SVladimir Oltean l2_lookup.mask_vlanid = VLAN_VID_MASK; 15841da73821SVladimir Oltean l2_lookup.mask_iotag = BIT(0); 15856d7c7d94SVladimir Oltean } else { 15866d7c7d94SVladimir Oltean l2_lookup.mask_vlanid = 0; 15876d7c7d94SVladimir Oltean l2_lookup.mask_iotag = 0; 15886d7c7d94SVladimir Oltean } 15891da73821SVladimir Oltean l2_lookup.destports = BIT(port); 15901da73821SVladimir Oltean 15911da73821SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 15921da73821SVladimir Oltean SJA1105_SEARCH, &l2_lookup); 15931da73821SVladimir Oltean if (rc < 0) 15941da73821SVladimir Oltean return 0; 15951da73821SVladimir Oltean 15961da73821SVladimir Oltean l2_lookup.destports &= ~BIT(port); 15971da73821SVladimir Oltean 15981da73821SVladimir Oltean /* Decide whether we remove just this port from the FDB entry, 15991da73821SVladimir Oltean * or if we remove it completely. 16001da73821SVladimir Oltean */ 16011da73821SVladimir Oltean if (l2_lookup.destports) 16021da73821SVladimir Oltean keep = true; 16031da73821SVladimir Oltean else 16041da73821SVladimir Oltean keep = false; 16051da73821SVladimir Oltean 160660f6053fSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 16071da73821SVladimir Oltean l2_lookup.index, &l2_lookup, keep); 160860f6053fSVladimir Oltean if (rc < 0) 160960f6053fSVladimir Oltean return rc; 161060f6053fSVladimir Oltean 161160f6053fSVladimir Oltean return sja1105_static_fdb_change(priv, port, &l2_lookup, keep); 16129dfa6911SVladimir Oltean } 16139dfa6911SVladimir Oltean 16149dfa6911SVladimir Oltean static int sja1105_fdb_add(struct dsa_switch *ds, int port, 16159dfa6911SVladimir Oltean const unsigned char *addr, u16 vid) 16169dfa6911SVladimir Oltean { 16179dfa6911SVladimir Oltean struct sja1105_private *priv = ds->priv; 1618b3ee526aSVladimir Oltean 16196d7c7d94SVladimir Oltean /* dsa_8021q is in effect when the bridge's vlan_filtering isn't, 16206d7c7d94SVladimir Oltean * so the switch still does some VLAN processing internally. 16216d7c7d94SVladimir Oltean * But Shared VLAN Learning (SVL) is also active, and it will take 16226d7c7d94SVladimir Oltean * care of autonomous forwarding between the unique pvid's of each 16236d7c7d94SVladimir Oltean * port. Here we just make sure that users can't add duplicate FDB 16246d7c7d94SVladimir Oltean * entries when in this mode - the actual VID doesn't matter except 16256d7c7d94SVladimir Oltean * for what gets printed in 'bridge fdb show'. In the case of zero, 16266d7c7d94SVladimir Oltean * no VID gets printed at all. 162793647594SVladimir Oltean */ 16287f14937fSVladimir Oltean if (priv->vlan_state != SJA1105_VLAN_FILTERING_FULL) 16296d7c7d94SVladimir Oltean vid = 0; 163093647594SVladimir Oltean 16316d7c7d94SVladimir Oltean return priv->info->fdb_add_cmd(ds, port, addr, vid); 16329dfa6911SVladimir Oltean } 16339dfa6911SVladimir Oltean 16349dfa6911SVladimir Oltean static int sja1105_fdb_del(struct dsa_switch *ds, int port, 16359dfa6911SVladimir Oltean const unsigned char *addr, u16 vid) 16369dfa6911SVladimir Oltean { 16379dfa6911SVladimir Oltean struct sja1105_private *priv = ds->priv; 16389dfa6911SVladimir Oltean 16397f14937fSVladimir Oltean if (priv->vlan_state != SJA1105_VLAN_FILTERING_FULL) 16406d7c7d94SVladimir Oltean vid = 0; 16416d7c7d94SVladimir Oltean 1642b3ee526aSVladimir Oltean return priv->info->fdb_del_cmd(ds, port, addr, vid); 16439dfa6911SVladimir Oltean } 16449dfa6911SVladimir Oltean 1645291d1e72SVladimir Oltean static int sja1105_fdb_dump(struct dsa_switch *ds, int port, 1646291d1e72SVladimir Oltean dsa_fdb_dump_cb_t *cb, void *data) 1647291d1e72SVladimir Oltean { 1648291d1e72SVladimir Oltean struct sja1105_private *priv = ds->priv; 1649291d1e72SVladimir Oltean struct device *dev = ds->dev; 1650291d1e72SVladimir Oltean int i; 1651291d1e72SVladimir Oltean 1652291d1e72SVladimir Oltean for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) { 1653291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 1654291d1e72SVladimir Oltean u8 macaddr[ETH_ALEN]; 1655291d1e72SVladimir Oltean int rc; 1656291d1e72SVladimir Oltean 1657291d1e72SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 1658291d1e72SVladimir Oltean i, &l2_lookup); 1659291d1e72SVladimir Oltean /* No fdb entry at i, not an issue */ 1660def84604SVladimir Oltean if (rc == -ENOENT) 1661291d1e72SVladimir Oltean continue; 1662291d1e72SVladimir Oltean if (rc) { 1663291d1e72SVladimir Oltean dev_err(dev, "Failed to dump FDB: %d\n", rc); 1664291d1e72SVladimir Oltean return rc; 1665291d1e72SVladimir Oltean } 1666291d1e72SVladimir Oltean 1667291d1e72SVladimir Oltean /* FDB dump callback is per port. This means we have to 1668291d1e72SVladimir Oltean * disregard a valid entry if it's not for this port, even if 1669291d1e72SVladimir Oltean * only to revisit it later. This is inefficient because the 1670291d1e72SVladimir Oltean * 1024-sized FDB table needs to be traversed 4 times through 1671291d1e72SVladimir Oltean * SPI during a 'bridge fdb show' command. 1672291d1e72SVladimir Oltean */ 1673291d1e72SVladimir Oltean if (!(l2_lookup.destports & BIT(port))) 1674291d1e72SVladimir Oltean continue; 16754d942354SVladimir Oltean 16764d942354SVladimir Oltean /* We need to hide the FDB entry for unknown multicast */ 16774d942354SVladimir Oltean if (l2_lookup.macaddr == SJA1105_UNKNOWN_MULTICAST && 16784d942354SVladimir Oltean l2_lookup.mask_macaddr == SJA1105_UNKNOWN_MULTICAST) 16794d942354SVladimir Oltean continue; 16804d942354SVladimir Oltean 1681291d1e72SVladimir Oltean u64_to_ether_addr(l2_lookup.macaddr, macaddr); 168293647594SVladimir Oltean 16836d7c7d94SVladimir Oltean /* We need to hide the dsa_8021q VLANs from the user. */ 16847f14937fSVladimir Oltean if (priv->vlan_state == SJA1105_VLAN_UNAWARE) 16856d7c7d94SVladimir Oltean l2_lookup.vlanid = 0; 168617ae6555SVladimir Oltean cb(macaddr, l2_lookup.vlanid, l2_lookup.lockeds, data); 1687291d1e72SVladimir Oltean } 1688291d1e72SVladimir Oltean return 0; 1689291d1e72SVladimir Oltean } 1690291d1e72SVladimir Oltean 1691a52b2da7SVladimir Oltean static int sja1105_mdb_add(struct dsa_switch *ds, int port, 1692291d1e72SVladimir Oltean const struct switchdev_obj_port_mdb *mdb) 1693291d1e72SVladimir Oltean { 1694a52b2da7SVladimir Oltean return sja1105_fdb_add(ds, port, mdb->addr, mdb->vid); 1695291d1e72SVladimir Oltean } 1696291d1e72SVladimir Oltean 1697291d1e72SVladimir Oltean static int sja1105_mdb_del(struct dsa_switch *ds, int port, 1698291d1e72SVladimir Oltean const struct switchdev_obj_port_mdb *mdb) 1699291d1e72SVladimir Oltean { 1700291d1e72SVladimir Oltean return sja1105_fdb_del(ds, port, mdb->addr, mdb->vid); 1701291d1e72SVladimir Oltean } 1702291d1e72SVladimir Oltean 17037f7ccdeaSVladimir Oltean /* Common function for unicast and broadcast flood configuration. 17047f7ccdeaSVladimir Oltean * Flooding is configured between each {ingress, egress} port pair, and since 17057f7ccdeaSVladimir Oltean * the bridge's semantics are those of "egress flooding", it means we must 17067f7ccdeaSVladimir Oltean * enable flooding towards this port from all ingress ports that are in the 17077f7ccdeaSVladimir Oltean * same forwarding domain. 17087f7ccdeaSVladimir Oltean */ 17097f7ccdeaSVladimir Oltean static int sja1105_manage_flood_domains(struct sja1105_private *priv) 17107f7ccdeaSVladimir Oltean { 17117f7ccdeaSVladimir Oltean struct sja1105_l2_forwarding_entry *l2_fwd; 17127f7ccdeaSVladimir Oltean struct dsa_switch *ds = priv->ds; 17137f7ccdeaSVladimir Oltean int from, to, rc; 17147f7ccdeaSVladimir Oltean 17157f7ccdeaSVladimir Oltean l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries; 17167f7ccdeaSVladimir Oltean 17177f7ccdeaSVladimir Oltean for (from = 0; from < ds->num_ports; from++) { 17187f7ccdeaSVladimir Oltean u64 fl_domain = 0, bc_domain = 0; 17197f7ccdeaSVladimir Oltean 17207f7ccdeaSVladimir Oltean for (to = 0; to < priv->ds->num_ports; to++) { 17217f7ccdeaSVladimir Oltean if (!sja1105_can_forward(l2_fwd, from, to)) 17227f7ccdeaSVladimir Oltean continue; 17237f7ccdeaSVladimir Oltean 17247f7ccdeaSVladimir Oltean if (priv->ucast_egress_floods & BIT(to)) 17257f7ccdeaSVladimir Oltean fl_domain |= BIT(to); 17267f7ccdeaSVladimir Oltean if (priv->bcast_egress_floods & BIT(to)) 17277f7ccdeaSVladimir Oltean bc_domain |= BIT(to); 17287f7ccdeaSVladimir Oltean } 17297f7ccdeaSVladimir Oltean 17307f7ccdeaSVladimir Oltean /* Nothing changed, nothing to do */ 17317f7ccdeaSVladimir Oltean if (l2_fwd[from].fl_domain == fl_domain && 17327f7ccdeaSVladimir Oltean l2_fwd[from].bc_domain == bc_domain) 17337f7ccdeaSVladimir Oltean continue; 17347f7ccdeaSVladimir Oltean 17357f7ccdeaSVladimir Oltean l2_fwd[from].fl_domain = fl_domain; 17367f7ccdeaSVladimir Oltean l2_fwd[from].bc_domain = bc_domain; 17377f7ccdeaSVladimir Oltean 17387f7ccdeaSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING, 17397f7ccdeaSVladimir Oltean from, &l2_fwd[from], true); 17407f7ccdeaSVladimir Oltean if (rc < 0) 17417f7ccdeaSVladimir Oltean return rc; 17427f7ccdeaSVladimir Oltean } 17437f7ccdeaSVladimir Oltean 17447f7ccdeaSVladimir Oltean return 0; 17457f7ccdeaSVladimir Oltean } 17467f7ccdeaSVladimir Oltean 17478aa9ebccSVladimir Oltean static int sja1105_bridge_member(struct dsa_switch *ds, int port, 17488aa9ebccSVladimir Oltean struct net_device *br, bool member) 17498aa9ebccSVladimir Oltean { 17508aa9ebccSVladimir Oltean struct sja1105_l2_forwarding_entry *l2_fwd; 17518aa9ebccSVladimir Oltean struct sja1105_private *priv = ds->priv; 17528aa9ebccSVladimir Oltean int i, rc; 17538aa9ebccSVladimir Oltean 17548aa9ebccSVladimir Oltean l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries; 17558aa9ebccSVladimir Oltean 1756542043e9SVladimir Oltean for (i = 0; i < ds->num_ports; i++) { 17578aa9ebccSVladimir Oltean /* Add this port to the forwarding matrix of the 17588aa9ebccSVladimir Oltean * other ports in the same bridge, and viceversa. 17598aa9ebccSVladimir Oltean */ 17608aa9ebccSVladimir Oltean if (!dsa_is_user_port(ds, i)) 17618aa9ebccSVladimir Oltean continue; 17628aa9ebccSVladimir Oltean /* For the ports already under the bridge, only one thing needs 17638aa9ebccSVladimir Oltean * to be done, and that is to add this port to their 17648aa9ebccSVladimir Oltean * reachability domain. So we can perform the SPI write for 17658aa9ebccSVladimir Oltean * them immediately. However, for this port itself (the one 17668aa9ebccSVladimir Oltean * that is new to the bridge), we need to add all other ports 17678aa9ebccSVladimir Oltean * to its reachability domain. So we do that incrementally in 17688aa9ebccSVladimir Oltean * this loop, and perform the SPI write only at the end, once 17698aa9ebccSVladimir Oltean * the domain contains all other bridge ports. 17708aa9ebccSVladimir Oltean */ 17718aa9ebccSVladimir Oltean if (i == port) 17728aa9ebccSVladimir Oltean continue; 17738aa9ebccSVladimir Oltean if (dsa_to_port(ds, i)->bridge_dev != br) 17748aa9ebccSVladimir Oltean continue; 17758aa9ebccSVladimir Oltean sja1105_port_allow_traffic(l2_fwd, i, port, member); 17768aa9ebccSVladimir Oltean sja1105_port_allow_traffic(l2_fwd, port, i, member); 17778aa9ebccSVladimir Oltean 17788aa9ebccSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING, 17798aa9ebccSVladimir Oltean i, &l2_fwd[i], true); 17808aa9ebccSVladimir Oltean if (rc < 0) 17818aa9ebccSVladimir Oltean return rc; 17828aa9ebccSVladimir Oltean } 17838aa9ebccSVladimir Oltean 17847f7ccdeaSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING, 17858aa9ebccSVladimir Oltean port, &l2_fwd[port], true); 17867f7ccdeaSVladimir Oltean if (rc) 17877f7ccdeaSVladimir Oltean return rc; 17887f7ccdeaSVladimir Oltean 17897f7ccdeaSVladimir Oltean return sja1105_manage_flood_domains(priv); 17908aa9ebccSVladimir Oltean } 17918aa9ebccSVladimir Oltean 1792640f763fSVladimir Oltean static void sja1105_bridge_stp_state_set(struct dsa_switch *ds, int port, 1793640f763fSVladimir Oltean u8 state) 1794640f763fSVladimir Oltean { 1795640f763fSVladimir Oltean struct sja1105_private *priv = ds->priv; 1796640f763fSVladimir Oltean struct sja1105_mac_config_entry *mac; 1797640f763fSVladimir Oltean 1798640f763fSVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 1799640f763fSVladimir Oltean 1800640f763fSVladimir Oltean switch (state) { 1801640f763fSVladimir Oltean case BR_STATE_DISABLED: 1802640f763fSVladimir Oltean case BR_STATE_BLOCKING: 1803640f763fSVladimir Oltean /* From UM10944 description of DRPDTAG (why put this there?): 1804640f763fSVladimir Oltean * "Management traffic flows to the port regardless of the state 1805640f763fSVladimir Oltean * of the INGRESS flag". So BPDUs are still be allowed to pass. 1806640f763fSVladimir Oltean * At the moment no difference between DISABLED and BLOCKING. 1807640f763fSVladimir Oltean */ 1808640f763fSVladimir Oltean mac[port].ingress = false; 1809640f763fSVladimir Oltean mac[port].egress = false; 1810640f763fSVladimir Oltean mac[port].dyn_learn = false; 1811640f763fSVladimir Oltean break; 1812640f763fSVladimir Oltean case BR_STATE_LISTENING: 1813640f763fSVladimir Oltean mac[port].ingress = true; 1814640f763fSVladimir Oltean mac[port].egress = false; 1815640f763fSVladimir Oltean mac[port].dyn_learn = false; 1816640f763fSVladimir Oltean break; 1817640f763fSVladimir Oltean case BR_STATE_LEARNING: 1818640f763fSVladimir Oltean mac[port].ingress = true; 1819640f763fSVladimir Oltean mac[port].egress = false; 18204d942354SVladimir Oltean mac[port].dyn_learn = !!(priv->learn_ena & BIT(port)); 1821640f763fSVladimir Oltean break; 1822640f763fSVladimir Oltean case BR_STATE_FORWARDING: 1823640f763fSVladimir Oltean mac[port].ingress = true; 1824640f763fSVladimir Oltean mac[port].egress = true; 18254d942354SVladimir Oltean mac[port].dyn_learn = !!(priv->learn_ena & BIT(port)); 1826640f763fSVladimir Oltean break; 1827640f763fSVladimir Oltean default: 1828640f763fSVladimir Oltean dev_err(ds->dev, "invalid STP state: %d\n", state); 1829640f763fSVladimir Oltean return; 1830640f763fSVladimir Oltean } 1831640f763fSVladimir Oltean 1832640f763fSVladimir Oltean sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, 1833640f763fSVladimir Oltean &mac[port], true); 1834640f763fSVladimir Oltean } 1835640f763fSVladimir Oltean 18368aa9ebccSVladimir Oltean static int sja1105_bridge_join(struct dsa_switch *ds, int port, 18378aa9ebccSVladimir Oltean struct net_device *br) 18388aa9ebccSVladimir Oltean { 18398aa9ebccSVladimir Oltean return sja1105_bridge_member(ds, port, br, true); 18408aa9ebccSVladimir Oltean } 18418aa9ebccSVladimir Oltean 18428aa9ebccSVladimir Oltean static void sja1105_bridge_leave(struct dsa_switch *ds, int port, 18438aa9ebccSVladimir Oltean struct net_device *br) 18448aa9ebccSVladimir Oltean { 18458aa9ebccSVladimir Oltean sja1105_bridge_member(ds, port, br, false); 18468aa9ebccSVladimir Oltean } 18478aa9ebccSVladimir Oltean 18484d752508SVladimir Oltean #define BYTES_PER_KBIT (1000LL / 8) 18494d752508SVladimir Oltean 18504d752508SVladimir Oltean static int sja1105_find_unused_cbs_shaper(struct sja1105_private *priv) 18514d752508SVladimir Oltean { 18524d752508SVladimir Oltean int i; 18534d752508SVladimir Oltean 18544d752508SVladimir Oltean for (i = 0; i < priv->info->num_cbs_shapers; i++) 18554d752508SVladimir Oltean if (!priv->cbs[i].idle_slope && !priv->cbs[i].send_slope) 18564d752508SVladimir Oltean return i; 18574d752508SVladimir Oltean 18584d752508SVladimir Oltean return -1; 18594d752508SVladimir Oltean } 18604d752508SVladimir Oltean 18614d752508SVladimir Oltean static int sja1105_delete_cbs_shaper(struct sja1105_private *priv, int port, 18624d752508SVladimir Oltean int prio) 18634d752508SVladimir Oltean { 18644d752508SVladimir Oltean int i; 18654d752508SVladimir Oltean 18664d752508SVladimir Oltean for (i = 0; i < priv->info->num_cbs_shapers; i++) { 18674d752508SVladimir Oltean struct sja1105_cbs_entry *cbs = &priv->cbs[i]; 18684d752508SVladimir Oltean 18694d752508SVladimir Oltean if (cbs->port == port && cbs->prio == prio) { 18704d752508SVladimir Oltean memset(cbs, 0, sizeof(*cbs)); 18714d752508SVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_CBS, 18724d752508SVladimir Oltean i, cbs, true); 18734d752508SVladimir Oltean } 18744d752508SVladimir Oltean } 18754d752508SVladimir Oltean 18764d752508SVladimir Oltean return 0; 18774d752508SVladimir Oltean } 18784d752508SVladimir Oltean 18794d752508SVladimir Oltean static int sja1105_setup_tc_cbs(struct dsa_switch *ds, int port, 18804d752508SVladimir Oltean struct tc_cbs_qopt_offload *offload) 18814d752508SVladimir Oltean { 18824d752508SVladimir Oltean struct sja1105_private *priv = ds->priv; 18834d752508SVladimir Oltean struct sja1105_cbs_entry *cbs; 18844d752508SVladimir Oltean int index; 18854d752508SVladimir Oltean 18864d752508SVladimir Oltean if (!offload->enable) 18874d752508SVladimir Oltean return sja1105_delete_cbs_shaper(priv, port, offload->queue); 18884d752508SVladimir Oltean 18894d752508SVladimir Oltean index = sja1105_find_unused_cbs_shaper(priv); 18904d752508SVladimir Oltean if (index < 0) 18914d752508SVladimir Oltean return -ENOSPC; 18924d752508SVladimir Oltean 18934d752508SVladimir Oltean cbs = &priv->cbs[index]; 18944d752508SVladimir Oltean cbs->port = port; 18954d752508SVladimir Oltean cbs->prio = offload->queue; 18964d752508SVladimir Oltean /* locredit and sendslope are negative by definition. In hardware, 18974d752508SVladimir Oltean * positive values must be provided, and the negative sign is implicit. 18984d752508SVladimir Oltean */ 18994d752508SVladimir Oltean cbs->credit_hi = offload->hicredit; 19004d752508SVladimir Oltean cbs->credit_lo = abs(offload->locredit); 19014d752508SVladimir Oltean /* User space is in kbits/sec, hardware in bytes/sec */ 19024d752508SVladimir Oltean cbs->idle_slope = offload->idleslope * BYTES_PER_KBIT; 19034d752508SVladimir Oltean cbs->send_slope = abs(offload->sendslope * BYTES_PER_KBIT); 19044d752508SVladimir Oltean /* Convert the negative values from 64-bit 2's complement 19054d752508SVladimir Oltean * to 32-bit 2's complement (for the case of 0x80000000 whose 19064d752508SVladimir Oltean * negative is still negative). 19074d752508SVladimir Oltean */ 19084d752508SVladimir Oltean cbs->credit_lo &= GENMASK_ULL(31, 0); 19094d752508SVladimir Oltean cbs->send_slope &= GENMASK_ULL(31, 0); 19104d752508SVladimir Oltean 19114d752508SVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_CBS, index, cbs, 19124d752508SVladimir Oltean true); 19134d752508SVladimir Oltean } 19144d752508SVladimir Oltean 19154d752508SVladimir Oltean static int sja1105_reload_cbs(struct sja1105_private *priv) 19164d752508SVladimir Oltean { 19174d752508SVladimir Oltean int rc = 0, i; 19184d752508SVladimir Oltean 19194d752508SVladimir Oltean for (i = 0; i < priv->info->num_cbs_shapers; i++) { 19204d752508SVladimir Oltean struct sja1105_cbs_entry *cbs = &priv->cbs[i]; 19214d752508SVladimir Oltean 19224d752508SVladimir Oltean if (!cbs->idle_slope && !cbs->send_slope) 19234d752508SVladimir Oltean continue; 19244d752508SVladimir Oltean 19254d752508SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_CBS, i, cbs, 19264d752508SVladimir Oltean true); 19274d752508SVladimir Oltean if (rc) 19284d752508SVladimir Oltean break; 19294d752508SVladimir Oltean } 19304d752508SVladimir Oltean 19314d752508SVladimir Oltean return rc; 19324d752508SVladimir Oltean } 19334d752508SVladimir Oltean 19342eea1fa8SVladimir Oltean static const char * const sja1105_reset_reasons[] = { 19352eea1fa8SVladimir Oltean [SJA1105_VLAN_FILTERING] = "VLAN filtering", 19362eea1fa8SVladimir Oltean [SJA1105_RX_HWTSTAMPING] = "RX timestamping", 19372eea1fa8SVladimir Oltean [SJA1105_AGEING_TIME] = "Ageing time", 19382eea1fa8SVladimir Oltean [SJA1105_SCHEDULING] = "Time-aware scheduling", 1939c279c726SVladimir Oltean [SJA1105_BEST_EFFORT_POLICING] = "Best-effort policing", 1940dfacc5a2SVladimir Oltean [SJA1105_VIRTUAL_LINKS] = "Virtual links", 19412eea1fa8SVladimir Oltean }; 19422eea1fa8SVladimir Oltean 19436666cebcSVladimir Oltean /* For situations where we need to change a setting at runtime that is only 19446666cebcSVladimir Oltean * available through the static configuration, resetting the switch in order 19456666cebcSVladimir Oltean * to upload the new static config is unavoidable. Back up the settings we 19466666cebcSVladimir Oltean * modify at runtime (currently only MAC) and restore them after uploading, 19476666cebcSVladimir Oltean * such that this operation is relatively seamless. 19486666cebcSVladimir Oltean */ 19492eea1fa8SVladimir Oltean int sja1105_static_config_reload(struct sja1105_private *priv, 19502eea1fa8SVladimir Oltean enum sja1105_reset_reason reason) 19516666cebcSVladimir Oltean { 19526cf99c13SVladimir Oltean struct ptp_system_timestamp ptp_sts_before; 19536cf99c13SVladimir Oltean struct ptp_system_timestamp ptp_sts_after; 195482760d7fSVladimir Oltean int speed_mbps[SJA1105_MAX_NUM_PORTS]; 195584db00f2SVladimir Oltean u16 bmcr[SJA1105_MAX_NUM_PORTS] = {0}; 19566666cebcSVladimir Oltean struct sja1105_mac_config_entry *mac; 19576cf99c13SVladimir Oltean struct dsa_switch *ds = priv->ds; 19586cf99c13SVladimir Oltean s64 t1, t2, t3, t4; 19596cf99c13SVladimir Oltean s64 t12, t34; 19606666cebcSVladimir Oltean int rc, i; 19616cf99c13SVladimir Oltean s64 now; 19626666cebcSVladimir Oltean 1963af580ae2SVladimir Oltean mutex_lock(&priv->mgmt_lock); 1964af580ae2SVladimir Oltean 19656666cebcSVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 19666666cebcSVladimir Oltean 19678400cff6SVladimir Oltean /* Back up the dynamic link speed changed by sja1105_adjust_port_config 19688400cff6SVladimir Oltean * in order to temporarily restore it to SJA1105_SPEED_AUTO - which the 19698400cff6SVladimir Oltean * switch wants to see in the static config in order to allow us to 19708400cff6SVladimir Oltean * change it through the dynamic interface later. 19716666cebcSVladimir Oltean */ 1972542043e9SVladimir Oltean for (i = 0; i < ds->num_ports; i++) { 197341fed17fSVladimir Oltean speed_mbps[i] = sja1105_port_speed_to_ethtool(priv, 197441fed17fSVladimir Oltean mac[i].speed); 197541fed17fSVladimir Oltean mac[i].speed = priv->info->port_speed[SJA1105_SPEED_AUTO]; 19766666cebcSVladimir Oltean 197791a05078SVladimir Oltean if (priv->phy_mode[i] == PHY_INTERFACE_MODE_SGMII) 19784c7ee010SVladimir Oltean bmcr[i] = sja1105_sgmii_read(priv, i, 19794c7ee010SVladimir Oltean MDIO_MMD_VEND2, 19804c7ee010SVladimir Oltean MDIO_CTRL1); 198184db00f2SVladimir Oltean } 1982ffe10e67SVladimir Oltean 19836cf99c13SVladimir Oltean /* No PTP operations can run right now */ 19846cf99c13SVladimir Oltean mutex_lock(&priv->ptp_data.lock); 19856cf99c13SVladimir Oltean 19866cf99c13SVladimir Oltean rc = __sja1105_ptp_gettimex(ds, &now, &ptp_sts_before); 19876cf99c13SVladimir Oltean if (rc < 0) 19886cf99c13SVladimir Oltean goto out_unlock_ptp; 19896cf99c13SVladimir Oltean 19906666cebcSVladimir Oltean /* Reset switch and send updated static configuration */ 19916666cebcSVladimir Oltean rc = sja1105_static_config_upload(priv); 19926666cebcSVladimir Oltean if (rc < 0) 19936cf99c13SVladimir Oltean goto out_unlock_ptp; 19946cf99c13SVladimir Oltean 19956cf99c13SVladimir Oltean rc = __sja1105_ptp_settime(ds, 0, &ptp_sts_after); 19966cf99c13SVladimir Oltean if (rc < 0) 19976cf99c13SVladimir Oltean goto out_unlock_ptp; 19986cf99c13SVladimir Oltean 19996cf99c13SVladimir Oltean t1 = timespec64_to_ns(&ptp_sts_before.pre_ts); 20006cf99c13SVladimir Oltean t2 = timespec64_to_ns(&ptp_sts_before.post_ts); 20016cf99c13SVladimir Oltean t3 = timespec64_to_ns(&ptp_sts_after.pre_ts); 20026cf99c13SVladimir Oltean t4 = timespec64_to_ns(&ptp_sts_after.post_ts); 20036cf99c13SVladimir Oltean /* Mid point, corresponds to pre-reset PTPCLKVAL */ 20046cf99c13SVladimir Oltean t12 = t1 + (t2 - t1) / 2; 20056cf99c13SVladimir Oltean /* Mid point, corresponds to post-reset PTPCLKVAL, aka 0 */ 20066cf99c13SVladimir Oltean t34 = t3 + (t4 - t3) / 2; 20076cf99c13SVladimir Oltean /* Advance PTPCLKVAL by the time it took since its readout */ 20086cf99c13SVladimir Oltean now += (t34 - t12); 20096cf99c13SVladimir Oltean 20106cf99c13SVladimir Oltean __sja1105_ptp_adjtime(ds, now); 20116cf99c13SVladimir Oltean 20126cf99c13SVladimir Oltean out_unlock_ptp: 20136cf99c13SVladimir Oltean mutex_unlock(&priv->ptp_data.lock); 20146666cebcSVladimir Oltean 20152eea1fa8SVladimir Oltean dev_info(priv->ds->dev, 20162eea1fa8SVladimir Oltean "Reset switch and programmed static config. Reason: %s\n", 20172eea1fa8SVladimir Oltean sja1105_reset_reasons[reason]); 20182eea1fa8SVladimir Oltean 20196666cebcSVladimir Oltean /* Configure the CGU (PLLs) for MII and RMII PHYs. 20206666cebcSVladimir Oltean * For these interfaces there is no dynamic configuration 20216666cebcSVladimir Oltean * needed, since PLLs have same settings at all speeds. 20226666cebcSVladimir Oltean */ 2023c5037678SVladimir Oltean rc = priv->info->clocking_setup(priv); 20246666cebcSVladimir Oltean if (rc < 0) 20256666cebcSVladimir Oltean goto out; 20266666cebcSVladimir Oltean 2027542043e9SVladimir Oltean for (i = 0; i < ds->num_ports; i++) { 202884db00f2SVladimir Oltean bool an_enabled; 202984db00f2SVladimir Oltean 20308400cff6SVladimir Oltean rc = sja1105_adjust_port_config(priv, i, speed_mbps[i]); 20316666cebcSVladimir Oltean if (rc < 0) 20326666cebcSVladimir Oltean goto out; 2033ffe10e67SVladimir Oltean 203491a05078SVladimir Oltean if (priv->phy_mode[i] != PHY_INTERFACE_MODE_SGMII) 203584db00f2SVladimir Oltean continue; 2036ffe10e67SVladimir Oltean 203784db00f2SVladimir Oltean an_enabled = !!(bmcr[i] & BMCR_ANENABLE); 203884db00f2SVladimir Oltean 203984db00f2SVladimir Oltean sja1105_sgmii_pcs_config(priv, i, an_enabled, false); 2040ffe10e67SVladimir Oltean 2041ffe10e67SVladimir Oltean if (!an_enabled) { 2042ffe10e67SVladimir Oltean int speed = SPEED_UNKNOWN; 2043ffe10e67SVladimir Oltean 204484db00f2SVladimir Oltean if (bmcr[i] & BMCR_SPEED1000) 2045ffe10e67SVladimir Oltean speed = SPEED_1000; 204684db00f2SVladimir Oltean else if (bmcr[i] & BMCR_SPEED100) 2047ffe10e67SVladimir Oltean speed = SPEED_100; 2048053d8ad1SVladimir Oltean else 2049ffe10e67SVladimir Oltean speed = SPEED_10; 2050ffe10e67SVladimir Oltean 205184db00f2SVladimir Oltean sja1105_sgmii_pcs_force_speed(priv, i, speed); 2052ffe10e67SVladimir Oltean } 2053ffe10e67SVladimir Oltean } 20544d752508SVladimir Oltean 20554d752508SVladimir Oltean rc = sja1105_reload_cbs(priv); 20564d752508SVladimir Oltean if (rc < 0) 20574d752508SVladimir Oltean goto out; 20586666cebcSVladimir Oltean out: 2059af580ae2SVladimir Oltean mutex_unlock(&priv->mgmt_lock); 2060af580ae2SVladimir Oltean 20616666cebcSVladimir Oltean return rc; 20626666cebcSVladimir Oltean } 20636666cebcSVladimir Oltean 20646666cebcSVladimir Oltean static int sja1105_pvid_apply(struct sja1105_private *priv, int port, u16 pvid) 20656666cebcSVladimir Oltean { 20666666cebcSVladimir Oltean struct sja1105_mac_config_entry *mac; 20676666cebcSVladimir Oltean 20686666cebcSVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 20696666cebcSVladimir Oltean 20706666cebcSVladimir Oltean mac[port].vlanid = pvid; 20716666cebcSVladimir Oltean 20726666cebcSVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, 20736666cebcSVladimir Oltean &mac[port], true); 20746666cebcSVladimir Oltean } 20756666cebcSVladimir Oltean 2076ac02a451SVladimir Oltean static int sja1105_crosschip_bridge_join(struct dsa_switch *ds, 2077ac02a451SVladimir Oltean int tree_index, int sw_index, 2078ac02a451SVladimir Oltean int other_port, struct net_device *br) 2079ac02a451SVladimir Oltean { 2080ac02a451SVladimir Oltean struct dsa_switch *other_ds = dsa_switch_find(tree_index, sw_index); 2081ac02a451SVladimir Oltean struct sja1105_private *other_priv = other_ds->priv; 2082ac02a451SVladimir Oltean struct sja1105_private *priv = ds->priv; 2083ac02a451SVladimir Oltean int port, rc; 2084ac02a451SVladimir Oltean 2085ac02a451SVladimir Oltean if (other_ds->ops != &sja1105_switch_ops) 2086ac02a451SVladimir Oltean return 0; 2087ac02a451SVladimir Oltean 2088ac02a451SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 2089ac02a451SVladimir Oltean if (!dsa_is_user_port(ds, port)) 2090ac02a451SVladimir Oltean continue; 2091ac02a451SVladimir Oltean if (dsa_to_port(ds, port)->bridge_dev != br) 2092ac02a451SVladimir Oltean continue; 2093ac02a451SVladimir Oltean 20945899ee36SVladimir Oltean rc = dsa_8021q_crosschip_bridge_join(priv->dsa_8021q_ctx, 20955899ee36SVladimir Oltean port, 20965899ee36SVladimir Oltean other_priv->dsa_8021q_ctx, 20975899ee36SVladimir Oltean other_port); 2098ac02a451SVladimir Oltean if (rc) 2099ac02a451SVladimir Oltean return rc; 2100ac02a451SVladimir Oltean 21015899ee36SVladimir Oltean rc = dsa_8021q_crosschip_bridge_join(other_priv->dsa_8021q_ctx, 21025899ee36SVladimir Oltean other_port, 21035899ee36SVladimir Oltean priv->dsa_8021q_ctx, 21045899ee36SVladimir Oltean port); 2105ac02a451SVladimir Oltean if (rc) 2106ac02a451SVladimir Oltean return rc; 2107ac02a451SVladimir Oltean } 2108ac02a451SVladimir Oltean 2109ac02a451SVladimir Oltean return 0; 2110ac02a451SVladimir Oltean } 2111ac02a451SVladimir Oltean 2112ac02a451SVladimir Oltean static void sja1105_crosschip_bridge_leave(struct dsa_switch *ds, 2113ac02a451SVladimir Oltean int tree_index, int sw_index, 2114ac02a451SVladimir Oltean int other_port, 2115ac02a451SVladimir Oltean struct net_device *br) 2116ac02a451SVladimir Oltean { 2117ac02a451SVladimir Oltean struct dsa_switch *other_ds = dsa_switch_find(tree_index, sw_index); 2118ac02a451SVladimir Oltean struct sja1105_private *other_priv = other_ds->priv; 2119ac02a451SVladimir Oltean struct sja1105_private *priv = ds->priv; 2120ac02a451SVladimir Oltean int port; 2121ac02a451SVladimir Oltean 2122ac02a451SVladimir Oltean if (other_ds->ops != &sja1105_switch_ops) 2123ac02a451SVladimir Oltean return; 2124ac02a451SVladimir Oltean 2125ac02a451SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 2126ac02a451SVladimir Oltean if (!dsa_is_user_port(ds, port)) 2127ac02a451SVladimir Oltean continue; 2128ac02a451SVladimir Oltean if (dsa_to_port(ds, port)->bridge_dev != br) 2129ac02a451SVladimir Oltean continue; 2130ac02a451SVladimir Oltean 21315899ee36SVladimir Oltean dsa_8021q_crosschip_bridge_leave(priv->dsa_8021q_ctx, port, 21325899ee36SVladimir Oltean other_priv->dsa_8021q_ctx, 21335899ee36SVladimir Oltean other_port); 2134ac02a451SVladimir Oltean 21355899ee36SVladimir Oltean dsa_8021q_crosschip_bridge_leave(other_priv->dsa_8021q_ctx, 21365899ee36SVladimir Oltean other_port, 21375899ee36SVladimir Oltean priv->dsa_8021q_ctx, port); 2138ac02a451SVladimir Oltean } 2139ac02a451SVladimir Oltean } 2140ac02a451SVladimir Oltean 2141227d07a0SVladimir Oltean static int sja1105_setup_8021q_tagging(struct dsa_switch *ds, bool enabled) 2142227d07a0SVladimir Oltean { 214360b33aebSVladimir Oltean struct sja1105_private *priv = ds->priv; 21447e092af2SVladimir Oltean int rc; 2145227d07a0SVladimir Oltean 21465899ee36SVladimir Oltean rc = dsa_8021q_setup(priv->dsa_8021q_ctx, enabled); 21477e092af2SVladimir Oltean if (rc) 2148227d07a0SVladimir Oltean return rc; 2149ac02a451SVladimir Oltean 2150227d07a0SVladimir Oltean dev_info(ds->dev, "%s switch tagging\n", 2151227d07a0SVladimir Oltean enabled ? "Enabled" : "Disabled"); 2152227d07a0SVladimir Oltean return 0; 2153227d07a0SVladimir Oltean } 2154227d07a0SVladimir Oltean 21558aa9ebccSVladimir Oltean static enum dsa_tag_protocol 21564d776482SFlorian Fainelli sja1105_get_tag_protocol(struct dsa_switch *ds, int port, 21574d776482SFlorian Fainelli enum dsa_tag_protocol mp) 21588aa9ebccSVladimir Oltean { 2159227d07a0SVladimir Oltean return DSA_TAG_PROTO_SJA1105; 21608aa9ebccSVladimir Oltean } 21618aa9ebccSVladimir Oltean 21623f01c91aSVladimir Oltean static int sja1105_find_free_subvlan(u16 *subvlan_map, bool pvid) 21633f01c91aSVladimir Oltean { 21643f01c91aSVladimir Oltean int subvlan; 21653f01c91aSVladimir Oltean 21663f01c91aSVladimir Oltean if (pvid) 21673f01c91aSVladimir Oltean return 0; 21683f01c91aSVladimir Oltean 21693f01c91aSVladimir Oltean for (subvlan = 1; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++) 21703f01c91aSVladimir Oltean if (subvlan_map[subvlan] == VLAN_N_VID) 21713f01c91aSVladimir Oltean return subvlan; 21723f01c91aSVladimir Oltean 21733f01c91aSVladimir Oltean return -1; 21743f01c91aSVladimir Oltean } 21753f01c91aSVladimir Oltean 21763f01c91aSVladimir Oltean static int sja1105_find_subvlan(u16 *subvlan_map, u16 vid) 21773f01c91aSVladimir Oltean { 21783f01c91aSVladimir Oltean int subvlan; 21793f01c91aSVladimir Oltean 21803f01c91aSVladimir Oltean for (subvlan = 0; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++) 21813f01c91aSVladimir Oltean if (subvlan_map[subvlan] == vid) 21823f01c91aSVladimir Oltean return subvlan; 21833f01c91aSVladimir Oltean 21843f01c91aSVladimir Oltean return -1; 21853f01c91aSVladimir Oltean } 21863f01c91aSVladimir Oltean 21873f01c91aSVladimir Oltean static int sja1105_find_committed_subvlan(struct sja1105_private *priv, 21883f01c91aSVladimir Oltean int port, u16 vid) 21893f01c91aSVladimir Oltean { 21903f01c91aSVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 21913f01c91aSVladimir Oltean 21923f01c91aSVladimir Oltean return sja1105_find_subvlan(sp->subvlan_map, vid); 21933f01c91aSVladimir Oltean } 21943f01c91aSVladimir Oltean 21953f01c91aSVladimir Oltean static void sja1105_init_subvlan_map(u16 *subvlan_map) 21963f01c91aSVladimir Oltean { 21973f01c91aSVladimir Oltean int subvlan; 21983f01c91aSVladimir Oltean 21993f01c91aSVladimir Oltean for (subvlan = 0; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++) 22003f01c91aSVladimir Oltean subvlan_map[subvlan] = VLAN_N_VID; 22013f01c91aSVladimir Oltean } 22023f01c91aSVladimir Oltean 22033f01c91aSVladimir Oltean static void sja1105_commit_subvlan_map(struct sja1105_private *priv, int port, 22043f01c91aSVladimir Oltean u16 *subvlan_map) 22053f01c91aSVladimir Oltean { 22063f01c91aSVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 22073f01c91aSVladimir Oltean int subvlan; 22083f01c91aSVladimir Oltean 22093f01c91aSVladimir Oltean for (subvlan = 0; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++) 22103f01c91aSVladimir Oltean sp->subvlan_map[subvlan] = subvlan_map[subvlan]; 22113f01c91aSVladimir Oltean } 22123f01c91aSVladimir Oltean 2213ec5ae610SVladimir Oltean static int sja1105_is_vlan_configured(struct sja1105_private *priv, u16 vid) 2214ec5ae610SVladimir Oltean { 2215ec5ae610SVladimir Oltean struct sja1105_vlan_lookup_entry *vlan; 2216ec5ae610SVladimir Oltean int count, i; 2217ec5ae610SVladimir Oltean 2218ec5ae610SVladimir Oltean vlan = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entries; 2219ec5ae610SVladimir Oltean count = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entry_count; 2220ec5ae610SVladimir Oltean 2221ec5ae610SVladimir Oltean for (i = 0; i < count; i++) 2222ec5ae610SVladimir Oltean if (vlan[i].vlanid == vid) 2223ec5ae610SVladimir Oltean return i; 2224ec5ae610SVladimir Oltean 2225ec5ae610SVladimir Oltean /* Return an invalid entry index if not found */ 2226ec5ae610SVladimir Oltean return -1; 2227ec5ae610SVladimir Oltean } 2228ec5ae610SVladimir Oltean 22293f01c91aSVladimir Oltean static int 22303f01c91aSVladimir Oltean sja1105_find_retagging_entry(struct sja1105_retagging_entry *retagging, 22313f01c91aSVladimir Oltean int count, int from_port, u16 from_vid, 22323f01c91aSVladimir Oltean u16 to_vid) 2233ec5ae610SVladimir Oltean { 22343f01c91aSVladimir Oltean int i; 22353f01c91aSVladimir Oltean 22363f01c91aSVladimir Oltean for (i = 0; i < count; i++) 22373f01c91aSVladimir Oltean if (retagging[i].ing_port == BIT(from_port) && 22383f01c91aSVladimir Oltean retagging[i].vlan_ing == from_vid && 22393f01c91aSVladimir Oltean retagging[i].vlan_egr == to_vid) 22403f01c91aSVladimir Oltean return i; 22413f01c91aSVladimir Oltean 22423f01c91aSVladimir Oltean /* Return an invalid entry index if not found */ 22433f01c91aSVladimir Oltean return -1; 22443f01c91aSVladimir Oltean } 22453f01c91aSVladimir Oltean 22463f01c91aSVladimir Oltean static int sja1105_commit_vlans(struct sja1105_private *priv, 22473f01c91aSVladimir Oltean struct sja1105_vlan_lookup_entry *new_vlan, 22483f01c91aSVladimir Oltean struct sja1105_retagging_entry *new_retagging, 22493f01c91aSVladimir Oltean int num_retagging) 22503f01c91aSVladimir Oltean { 22513f01c91aSVladimir Oltean struct sja1105_retagging_entry *retagging; 2252ec5ae610SVladimir Oltean struct sja1105_vlan_lookup_entry *vlan; 2253ec5ae610SVladimir Oltean struct sja1105_table *table; 2254ec5ae610SVladimir Oltean int num_vlans = 0; 2255ec5ae610SVladimir Oltean int rc, i, k = 0; 2256ec5ae610SVladimir Oltean 2257ec5ae610SVladimir Oltean /* VLAN table */ 2258ec5ae610SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP]; 2259ec5ae610SVladimir Oltean vlan = table->entries; 2260ec5ae610SVladimir Oltean 2261ec5ae610SVladimir Oltean for (i = 0; i < VLAN_N_VID; i++) { 2262ec5ae610SVladimir Oltean int match = sja1105_is_vlan_configured(priv, i); 2263ec5ae610SVladimir Oltean 2264ec5ae610SVladimir Oltean if (new_vlan[i].vlanid != VLAN_N_VID) 2265ec5ae610SVladimir Oltean num_vlans++; 2266ec5ae610SVladimir Oltean 2267ec5ae610SVladimir Oltean if (new_vlan[i].vlanid == VLAN_N_VID && match >= 0) { 2268ec5ae610SVladimir Oltean /* Was there before, no longer is. Delete */ 2269ec5ae610SVladimir Oltean dev_dbg(priv->ds->dev, "Deleting VLAN %d\n", i); 2270ec5ae610SVladimir Oltean rc = sja1105_dynamic_config_write(priv, 2271ec5ae610SVladimir Oltean BLK_IDX_VLAN_LOOKUP, 2272ec5ae610SVladimir Oltean i, &vlan[match], false); 2273ec5ae610SVladimir Oltean if (rc < 0) 2274ec5ae610SVladimir Oltean return rc; 2275ec5ae610SVladimir Oltean } else if (new_vlan[i].vlanid != VLAN_N_VID) { 2276ec5ae610SVladimir Oltean /* Nothing changed, don't do anything */ 2277ec5ae610SVladimir Oltean if (match >= 0 && 2278ec5ae610SVladimir Oltean vlan[match].vlanid == new_vlan[i].vlanid && 2279ec5ae610SVladimir Oltean vlan[match].tag_port == new_vlan[i].tag_port && 2280ec5ae610SVladimir Oltean vlan[match].vlan_bc == new_vlan[i].vlan_bc && 2281ec5ae610SVladimir Oltean vlan[match].vmemb_port == new_vlan[i].vmemb_port) 2282ec5ae610SVladimir Oltean continue; 2283ec5ae610SVladimir Oltean /* Update entry */ 2284ec5ae610SVladimir Oltean dev_dbg(priv->ds->dev, "Updating VLAN %d\n", i); 2285ec5ae610SVladimir Oltean rc = sja1105_dynamic_config_write(priv, 2286ec5ae610SVladimir Oltean BLK_IDX_VLAN_LOOKUP, 2287ec5ae610SVladimir Oltean i, &new_vlan[i], 2288ec5ae610SVladimir Oltean true); 2289ec5ae610SVladimir Oltean if (rc < 0) 2290ec5ae610SVladimir Oltean return rc; 2291ec5ae610SVladimir Oltean } 2292ec5ae610SVladimir Oltean } 2293ec5ae610SVladimir Oltean 2294ec5ae610SVladimir Oltean if (table->entry_count) 2295ec5ae610SVladimir Oltean kfree(table->entries); 2296ec5ae610SVladimir Oltean 2297ec5ae610SVladimir Oltean table->entries = kcalloc(num_vlans, table->ops->unpacked_entry_size, 2298ec5ae610SVladimir Oltean GFP_KERNEL); 2299ec5ae610SVladimir Oltean if (!table->entries) 2300ec5ae610SVladimir Oltean return -ENOMEM; 2301ec5ae610SVladimir Oltean 2302ec5ae610SVladimir Oltean table->entry_count = num_vlans; 2303ec5ae610SVladimir Oltean vlan = table->entries; 2304ec5ae610SVladimir Oltean 2305ec5ae610SVladimir Oltean for (i = 0; i < VLAN_N_VID; i++) { 2306ec5ae610SVladimir Oltean if (new_vlan[i].vlanid == VLAN_N_VID) 2307ec5ae610SVladimir Oltean continue; 2308ec5ae610SVladimir Oltean vlan[k++] = new_vlan[i]; 2309ec5ae610SVladimir Oltean } 2310ec5ae610SVladimir Oltean 23113f01c91aSVladimir Oltean /* VLAN Retagging Table */ 23123f01c91aSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_RETAGGING]; 23133f01c91aSVladimir Oltean retagging = table->entries; 23143f01c91aSVladimir Oltean 23153f01c91aSVladimir Oltean for (i = 0; i < table->entry_count; i++) { 23163f01c91aSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_RETAGGING, 23173f01c91aSVladimir Oltean i, &retagging[i], false); 23183f01c91aSVladimir Oltean if (rc) 23193f01c91aSVladimir Oltean return rc; 23203f01c91aSVladimir Oltean } 23213f01c91aSVladimir Oltean 23223f01c91aSVladimir Oltean if (table->entry_count) 23233f01c91aSVladimir Oltean kfree(table->entries); 23243f01c91aSVladimir Oltean 23253f01c91aSVladimir Oltean table->entries = kcalloc(num_retagging, table->ops->unpacked_entry_size, 23263f01c91aSVladimir Oltean GFP_KERNEL); 23273f01c91aSVladimir Oltean if (!table->entries) 23283f01c91aSVladimir Oltean return -ENOMEM; 23293f01c91aSVladimir Oltean 23303f01c91aSVladimir Oltean table->entry_count = num_retagging; 23313f01c91aSVladimir Oltean retagging = table->entries; 23323f01c91aSVladimir Oltean 23333f01c91aSVladimir Oltean for (i = 0; i < num_retagging; i++) { 23343f01c91aSVladimir Oltean retagging[i] = new_retagging[i]; 23353f01c91aSVladimir Oltean 23363f01c91aSVladimir Oltean /* Update entry */ 23373f01c91aSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_RETAGGING, 23383f01c91aSVladimir Oltean i, &retagging[i], true); 23393f01c91aSVladimir Oltean if (rc < 0) 23403f01c91aSVladimir Oltean return rc; 23413f01c91aSVladimir Oltean } 23423f01c91aSVladimir Oltean 2343ec5ae610SVladimir Oltean return 0; 2344ec5ae610SVladimir Oltean } 2345ec5ae610SVladimir Oltean 23463f01c91aSVladimir Oltean struct sja1105_crosschip_vlan { 23473f01c91aSVladimir Oltean struct list_head list; 23483f01c91aSVladimir Oltean u16 vid; 23493f01c91aSVladimir Oltean bool untagged; 23503f01c91aSVladimir Oltean int port; 23513f01c91aSVladimir Oltean int other_port; 23525899ee36SVladimir Oltean struct dsa_8021q_context *other_ctx; 23533f01c91aSVladimir Oltean }; 23543f01c91aSVladimir Oltean 2355ec5ae610SVladimir Oltean struct sja1105_crosschip_switch { 2356ec5ae610SVladimir Oltean struct list_head list; 23575899ee36SVladimir Oltean struct dsa_8021q_context *other_ctx; 2358ec5ae610SVladimir Oltean }; 2359ec5ae610SVladimir Oltean 2360ec5ae610SVladimir Oltean static int sja1105_commit_pvid(struct sja1105_private *priv) 2361ec5ae610SVladimir Oltean { 2362ec5ae610SVladimir Oltean struct sja1105_bridge_vlan *v; 2363ec5ae610SVladimir Oltean struct list_head *vlan_list; 2364ec5ae610SVladimir Oltean int rc = 0; 2365ec5ae610SVladimir Oltean 2366ec5ae610SVladimir Oltean if (priv->vlan_state == SJA1105_VLAN_FILTERING_FULL) 2367ec5ae610SVladimir Oltean vlan_list = &priv->bridge_vlans; 2368ec5ae610SVladimir Oltean else 2369ec5ae610SVladimir Oltean vlan_list = &priv->dsa_8021q_vlans; 2370ec5ae610SVladimir Oltean 2371ec5ae610SVladimir Oltean list_for_each_entry(v, vlan_list, list) { 2372ec5ae610SVladimir Oltean if (v->pvid) { 2373ec5ae610SVladimir Oltean rc = sja1105_pvid_apply(priv, v->port, v->vid); 2374ec5ae610SVladimir Oltean if (rc) 2375ec5ae610SVladimir Oltean break; 2376ec5ae610SVladimir Oltean } 2377ec5ae610SVladimir Oltean } 2378ec5ae610SVladimir Oltean 2379ec5ae610SVladimir Oltean return rc; 2380ec5ae610SVladimir Oltean } 2381ec5ae610SVladimir Oltean 2382ec5ae610SVladimir Oltean static int 2383ec5ae610SVladimir Oltean sja1105_build_bridge_vlans(struct sja1105_private *priv, 2384ec5ae610SVladimir Oltean struct sja1105_vlan_lookup_entry *new_vlan) 2385ec5ae610SVladimir Oltean { 2386ec5ae610SVladimir Oltean struct sja1105_bridge_vlan *v; 2387ec5ae610SVladimir Oltean 2388ec5ae610SVladimir Oltean if (priv->vlan_state == SJA1105_VLAN_UNAWARE) 2389ec5ae610SVladimir Oltean return 0; 2390ec5ae610SVladimir Oltean 2391ec5ae610SVladimir Oltean list_for_each_entry(v, &priv->bridge_vlans, list) { 2392ec5ae610SVladimir Oltean int match = v->vid; 2393ec5ae610SVladimir Oltean 2394ec5ae610SVladimir Oltean new_vlan[match].vlanid = v->vid; 2395ec5ae610SVladimir Oltean new_vlan[match].vmemb_port |= BIT(v->port); 2396ec5ae610SVladimir Oltean new_vlan[match].vlan_bc |= BIT(v->port); 2397ec5ae610SVladimir Oltean if (!v->untagged) 2398ec5ae610SVladimir Oltean new_vlan[match].tag_port |= BIT(v->port); 23993e77e59bSVladimir Oltean new_vlan[match].type_entry = SJA1110_VLAN_D_TAG; 2400ec5ae610SVladimir Oltean } 2401ec5ae610SVladimir Oltean 2402ec5ae610SVladimir Oltean return 0; 2403ec5ae610SVladimir Oltean } 2404ec5ae610SVladimir Oltean 2405ec5ae610SVladimir Oltean static int 2406ec5ae610SVladimir Oltean sja1105_build_dsa_8021q_vlans(struct sja1105_private *priv, 2407ec5ae610SVladimir Oltean struct sja1105_vlan_lookup_entry *new_vlan) 2408ec5ae610SVladimir Oltean { 2409ec5ae610SVladimir Oltean struct sja1105_bridge_vlan *v; 2410ec5ae610SVladimir Oltean 2411ec5ae610SVladimir Oltean if (priv->vlan_state == SJA1105_VLAN_FILTERING_FULL) 2412ec5ae610SVladimir Oltean return 0; 2413ec5ae610SVladimir Oltean 2414ec5ae610SVladimir Oltean list_for_each_entry(v, &priv->dsa_8021q_vlans, list) { 2415ec5ae610SVladimir Oltean int match = v->vid; 2416ec5ae610SVladimir Oltean 2417ec5ae610SVladimir Oltean new_vlan[match].vlanid = v->vid; 2418ec5ae610SVladimir Oltean new_vlan[match].vmemb_port |= BIT(v->port); 2419ec5ae610SVladimir Oltean new_vlan[match].vlan_bc |= BIT(v->port); 2420ec5ae610SVladimir Oltean if (!v->untagged) 2421ec5ae610SVladimir Oltean new_vlan[match].tag_port |= BIT(v->port); 24223e77e59bSVladimir Oltean new_vlan[match].type_entry = SJA1110_VLAN_D_TAG; 2423ec5ae610SVladimir Oltean } 2424ec5ae610SVladimir Oltean 2425ec5ae610SVladimir Oltean return 0; 2426ec5ae610SVladimir Oltean } 2427ec5ae610SVladimir Oltean 24283f01c91aSVladimir Oltean static int sja1105_build_subvlans(struct sja1105_private *priv, 24293f01c91aSVladimir Oltean u16 subvlan_map[][DSA_8021Q_N_SUBVLAN], 24303f01c91aSVladimir Oltean struct sja1105_vlan_lookup_entry *new_vlan, 24313f01c91aSVladimir Oltean struct sja1105_retagging_entry *new_retagging, 24323f01c91aSVladimir Oltean int *num_retagging) 24333f01c91aSVladimir Oltean { 24343f01c91aSVladimir Oltean struct sja1105_bridge_vlan *v; 24353f01c91aSVladimir Oltean int k = *num_retagging; 24363f01c91aSVladimir Oltean 24373f01c91aSVladimir Oltean if (priv->vlan_state != SJA1105_VLAN_BEST_EFFORT) 24383f01c91aSVladimir Oltean return 0; 24393f01c91aSVladimir Oltean 24403f01c91aSVladimir Oltean list_for_each_entry(v, &priv->bridge_vlans, list) { 24413f01c91aSVladimir Oltean int upstream = dsa_upstream_port(priv->ds, v->port); 24423f01c91aSVladimir Oltean int match, subvlan; 24433f01c91aSVladimir Oltean u16 rx_vid; 24443f01c91aSVladimir Oltean 24453f01c91aSVladimir Oltean /* Only sub-VLANs on user ports need to be applied. 24463f01c91aSVladimir Oltean * Bridge VLANs also include VLANs added automatically 24473f01c91aSVladimir Oltean * by DSA on the CPU port. 24483f01c91aSVladimir Oltean */ 24493f01c91aSVladimir Oltean if (!dsa_is_user_port(priv->ds, v->port)) 24503f01c91aSVladimir Oltean continue; 24513f01c91aSVladimir Oltean 24523f01c91aSVladimir Oltean subvlan = sja1105_find_subvlan(subvlan_map[v->port], 24533f01c91aSVladimir Oltean v->vid); 24543f01c91aSVladimir Oltean if (subvlan < 0) { 24553f01c91aSVladimir Oltean subvlan = sja1105_find_free_subvlan(subvlan_map[v->port], 24563f01c91aSVladimir Oltean v->pvid); 24573f01c91aSVladimir Oltean if (subvlan < 0) { 24583f01c91aSVladimir Oltean dev_err(priv->ds->dev, "No more free subvlans\n"); 24593f01c91aSVladimir Oltean return -ENOSPC; 24603f01c91aSVladimir Oltean } 24613f01c91aSVladimir Oltean } 24623f01c91aSVladimir Oltean 24633f01c91aSVladimir Oltean rx_vid = dsa_8021q_rx_vid_subvlan(priv->ds, v->port, subvlan); 24643f01c91aSVladimir Oltean 24653f01c91aSVladimir Oltean /* @v->vid on @v->port needs to be retagged to @rx_vid 24663f01c91aSVladimir Oltean * on @upstream. Assume @v->vid on @v->port and on 24673f01c91aSVladimir Oltean * @upstream was already configured by the previous 24683f01c91aSVladimir Oltean * iteration over bridge_vlans. 24693f01c91aSVladimir Oltean */ 24703f01c91aSVladimir Oltean match = rx_vid; 24713f01c91aSVladimir Oltean new_vlan[match].vlanid = rx_vid; 24723f01c91aSVladimir Oltean new_vlan[match].vmemb_port |= BIT(v->port); 24733f01c91aSVladimir Oltean new_vlan[match].vmemb_port |= BIT(upstream); 24743f01c91aSVladimir Oltean new_vlan[match].vlan_bc |= BIT(v->port); 24753f01c91aSVladimir Oltean new_vlan[match].vlan_bc |= BIT(upstream); 24763f01c91aSVladimir Oltean /* The "untagged" flag is set the same as for the 24773f01c91aSVladimir Oltean * original VLAN 24783f01c91aSVladimir Oltean */ 24793f01c91aSVladimir Oltean if (!v->untagged) 24803f01c91aSVladimir Oltean new_vlan[match].tag_port |= BIT(v->port); 24813f01c91aSVladimir Oltean /* But it's always tagged towards the CPU */ 24823f01c91aSVladimir Oltean new_vlan[match].tag_port |= BIT(upstream); 24833e77e59bSVladimir Oltean new_vlan[match].type_entry = SJA1110_VLAN_D_TAG; 24843f01c91aSVladimir Oltean 24853f01c91aSVladimir Oltean /* The Retagging Table generates packet *clones* with 24863f01c91aSVladimir Oltean * the new VLAN. This is a very odd hardware quirk 24873f01c91aSVladimir Oltean * which we need to suppress by dropping the original 24883f01c91aSVladimir Oltean * packet. 24893f01c91aSVladimir Oltean * Deny egress of the original VLAN towards the CPU 24903f01c91aSVladimir Oltean * port. This will force the switch to drop it, and 24913f01c91aSVladimir Oltean * we'll see only the retagged packets. 24923f01c91aSVladimir Oltean */ 24933f01c91aSVladimir Oltean match = v->vid; 24943f01c91aSVladimir Oltean new_vlan[match].vlan_bc &= ~BIT(upstream); 24953f01c91aSVladimir Oltean 24963f01c91aSVladimir Oltean /* And the retagging itself */ 24973f01c91aSVladimir Oltean new_retagging[k].vlan_ing = v->vid; 24983f01c91aSVladimir Oltean new_retagging[k].vlan_egr = rx_vid; 24993f01c91aSVladimir Oltean new_retagging[k].ing_port = BIT(v->port); 25003f01c91aSVladimir Oltean new_retagging[k].egr_port = BIT(upstream); 25013f01c91aSVladimir Oltean if (k++ == SJA1105_MAX_RETAGGING_COUNT) { 25023f01c91aSVladimir Oltean dev_err(priv->ds->dev, "No more retagging rules\n"); 25033f01c91aSVladimir Oltean return -ENOSPC; 25043f01c91aSVladimir Oltean } 25053f01c91aSVladimir Oltean 25063f01c91aSVladimir Oltean subvlan_map[v->port][subvlan] = v->vid; 25073f01c91aSVladimir Oltean } 25083f01c91aSVladimir Oltean 25093f01c91aSVladimir Oltean *num_retagging = k; 25103f01c91aSVladimir Oltean 25113f01c91aSVladimir Oltean return 0; 25123f01c91aSVladimir Oltean } 25133f01c91aSVladimir Oltean 25143f01c91aSVladimir Oltean /* Sadly, in crosschip scenarios where the CPU port is also the link to another 25153f01c91aSVladimir Oltean * switch, we should retag backwards (the dsa_8021q vid to the original vid) on 25163f01c91aSVladimir Oltean * the CPU port of neighbour switches. 25173f01c91aSVladimir Oltean */ 25183f01c91aSVladimir Oltean static int 25193f01c91aSVladimir Oltean sja1105_build_crosschip_subvlans(struct sja1105_private *priv, 25203f01c91aSVladimir Oltean struct sja1105_vlan_lookup_entry *new_vlan, 25213f01c91aSVladimir Oltean struct sja1105_retagging_entry *new_retagging, 25223f01c91aSVladimir Oltean int *num_retagging) 25233f01c91aSVladimir Oltean { 25243f01c91aSVladimir Oltean struct sja1105_crosschip_vlan *tmp, *pos; 25253f01c91aSVladimir Oltean struct dsa_8021q_crosschip_link *c; 25263f01c91aSVladimir Oltean struct sja1105_bridge_vlan *v, *w; 25273f01c91aSVladimir Oltean struct list_head crosschip_vlans; 25283f01c91aSVladimir Oltean int k = *num_retagging; 25293f01c91aSVladimir Oltean int rc = 0; 25303f01c91aSVladimir Oltean 25313f01c91aSVladimir Oltean if (priv->vlan_state != SJA1105_VLAN_BEST_EFFORT) 25323f01c91aSVladimir Oltean return 0; 25333f01c91aSVladimir Oltean 25343f01c91aSVladimir Oltean INIT_LIST_HEAD(&crosschip_vlans); 25353f01c91aSVladimir Oltean 25365899ee36SVladimir Oltean list_for_each_entry(c, &priv->dsa_8021q_ctx->crosschip_links, list) { 25375899ee36SVladimir Oltean struct sja1105_private *other_priv = c->other_ctx->ds->priv; 25383f01c91aSVladimir Oltean 25393f01c91aSVladimir Oltean if (other_priv->vlan_state == SJA1105_VLAN_FILTERING_FULL) 25403f01c91aSVladimir Oltean continue; 25413f01c91aSVladimir Oltean 25423f01c91aSVladimir Oltean /* Crosschip links are also added to the CPU ports. 25433f01c91aSVladimir Oltean * Ignore those. 25443f01c91aSVladimir Oltean */ 25453f01c91aSVladimir Oltean if (!dsa_is_user_port(priv->ds, c->port)) 25463f01c91aSVladimir Oltean continue; 25475899ee36SVladimir Oltean if (!dsa_is_user_port(c->other_ctx->ds, c->other_port)) 25483f01c91aSVladimir Oltean continue; 25493f01c91aSVladimir Oltean 25503f01c91aSVladimir Oltean /* Search for VLANs on the remote port */ 25513f01c91aSVladimir Oltean list_for_each_entry(v, &other_priv->bridge_vlans, list) { 25523f01c91aSVladimir Oltean bool already_added = false; 25533f01c91aSVladimir Oltean bool we_have_it = false; 25543f01c91aSVladimir Oltean 25553f01c91aSVladimir Oltean if (v->port != c->other_port) 25563f01c91aSVladimir Oltean continue; 25573f01c91aSVladimir Oltean 25583f01c91aSVladimir Oltean /* If @v is a pvid on @other_ds, it does not need 25593f01c91aSVladimir Oltean * re-retagging, because its SVL field is 0 and we 25603f01c91aSVladimir Oltean * already allow that, via the dsa_8021q crosschip 25613f01c91aSVladimir Oltean * links. 25623f01c91aSVladimir Oltean */ 25633f01c91aSVladimir Oltean if (v->pvid) 25643f01c91aSVladimir Oltean continue; 25653f01c91aSVladimir Oltean 25663f01c91aSVladimir Oltean /* Search for the VLAN on our local port */ 25673f01c91aSVladimir Oltean list_for_each_entry(w, &priv->bridge_vlans, list) { 25683f01c91aSVladimir Oltean if (w->port == c->port && w->vid == v->vid) { 25693f01c91aSVladimir Oltean we_have_it = true; 25703f01c91aSVladimir Oltean break; 25713f01c91aSVladimir Oltean } 25723f01c91aSVladimir Oltean } 25733f01c91aSVladimir Oltean 25743f01c91aSVladimir Oltean if (!we_have_it) 25753f01c91aSVladimir Oltean continue; 25763f01c91aSVladimir Oltean 25773f01c91aSVladimir Oltean list_for_each_entry(tmp, &crosschip_vlans, list) { 25783f01c91aSVladimir Oltean if (tmp->vid == v->vid && 25793f01c91aSVladimir Oltean tmp->untagged == v->untagged && 25803f01c91aSVladimir Oltean tmp->port == c->port && 25813f01c91aSVladimir Oltean tmp->other_port == v->port && 25825899ee36SVladimir Oltean tmp->other_ctx == c->other_ctx) { 25833f01c91aSVladimir Oltean already_added = true; 25843f01c91aSVladimir Oltean break; 25853f01c91aSVladimir Oltean } 25863f01c91aSVladimir Oltean } 25873f01c91aSVladimir Oltean 25883f01c91aSVladimir Oltean if (already_added) 25893f01c91aSVladimir Oltean continue; 25903f01c91aSVladimir Oltean 25913f01c91aSVladimir Oltean tmp = kzalloc(sizeof(*tmp), GFP_KERNEL); 25923f01c91aSVladimir Oltean if (!tmp) { 25933f01c91aSVladimir Oltean dev_err(priv->ds->dev, "Failed to allocate memory\n"); 25943f01c91aSVladimir Oltean rc = -ENOMEM; 25953f01c91aSVladimir Oltean goto out; 25963f01c91aSVladimir Oltean } 25973f01c91aSVladimir Oltean tmp->vid = v->vid; 25983f01c91aSVladimir Oltean tmp->port = c->port; 25993f01c91aSVladimir Oltean tmp->other_port = v->port; 26005899ee36SVladimir Oltean tmp->other_ctx = c->other_ctx; 26013f01c91aSVladimir Oltean tmp->untagged = v->untagged; 26023f01c91aSVladimir Oltean list_add(&tmp->list, &crosschip_vlans); 26033f01c91aSVladimir Oltean } 26043f01c91aSVladimir Oltean } 26053f01c91aSVladimir Oltean 26063f01c91aSVladimir Oltean list_for_each_entry(tmp, &crosschip_vlans, list) { 26075899ee36SVladimir Oltean struct sja1105_private *other_priv = tmp->other_ctx->ds->priv; 26083f01c91aSVladimir Oltean int upstream = dsa_upstream_port(priv->ds, tmp->port); 26093f01c91aSVladimir Oltean int match, subvlan; 26103f01c91aSVladimir Oltean u16 rx_vid; 26113f01c91aSVladimir Oltean 26123f01c91aSVladimir Oltean subvlan = sja1105_find_committed_subvlan(other_priv, 26133f01c91aSVladimir Oltean tmp->other_port, 26143f01c91aSVladimir Oltean tmp->vid); 26153f01c91aSVladimir Oltean /* If this happens, it's a bug. The neighbour switch does not 26163f01c91aSVladimir Oltean * have a subvlan for tmp->vid on tmp->other_port, but it 26173f01c91aSVladimir Oltean * should, since we already checked for its vlan_state. 26183f01c91aSVladimir Oltean */ 26193f01c91aSVladimir Oltean if (WARN_ON(subvlan < 0)) { 26203f01c91aSVladimir Oltean rc = -EINVAL; 26213f01c91aSVladimir Oltean goto out; 26223f01c91aSVladimir Oltean } 26233f01c91aSVladimir Oltean 26245899ee36SVladimir Oltean rx_vid = dsa_8021q_rx_vid_subvlan(tmp->other_ctx->ds, 26253f01c91aSVladimir Oltean tmp->other_port, 26263f01c91aSVladimir Oltean subvlan); 26273f01c91aSVladimir Oltean 26283f01c91aSVladimir Oltean /* The @rx_vid retagged from @tmp->vid on 26293f01c91aSVladimir Oltean * {@tmp->other_ds, @tmp->other_port} needs to be 26303f01c91aSVladimir Oltean * re-retagged to @tmp->vid on the way back to us. 26313f01c91aSVladimir Oltean * 26323f01c91aSVladimir Oltean * Assume the original @tmp->vid is already configured 26333f01c91aSVladimir Oltean * on this local switch, otherwise we wouldn't be 26343f01c91aSVladimir Oltean * retagging its subvlan on the other switch in the 26353f01c91aSVladimir Oltean * first place. We just need to add a reverse retagging 26363f01c91aSVladimir Oltean * rule for @rx_vid and install @rx_vid on our ports. 26373f01c91aSVladimir Oltean */ 26383f01c91aSVladimir Oltean match = rx_vid; 26393f01c91aSVladimir Oltean new_vlan[match].vlanid = rx_vid; 26403f01c91aSVladimir Oltean new_vlan[match].vmemb_port |= BIT(tmp->port); 26413f01c91aSVladimir Oltean new_vlan[match].vmemb_port |= BIT(upstream); 26423f01c91aSVladimir Oltean /* The "untagged" flag is set the same as for the 26433f01c91aSVladimir Oltean * original VLAN. And towards the CPU, it doesn't 26443f01c91aSVladimir Oltean * really matter, because @rx_vid will only receive 26453f01c91aSVladimir Oltean * traffic on that port. For consistency with other dsa_8021q 26463f01c91aSVladimir Oltean * VLANs, we'll keep the CPU port tagged. 26473f01c91aSVladimir Oltean */ 26483f01c91aSVladimir Oltean if (!tmp->untagged) 26493f01c91aSVladimir Oltean new_vlan[match].tag_port |= BIT(tmp->port); 26503f01c91aSVladimir Oltean new_vlan[match].tag_port |= BIT(upstream); 26513e77e59bSVladimir Oltean new_vlan[match].type_entry = SJA1110_VLAN_D_TAG; 26523f01c91aSVladimir Oltean /* Deny egress of @rx_vid towards our front-panel port. 26533f01c91aSVladimir Oltean * This will force the switch to drop it, and we'll see 26543f01c91aSVladimir Oltean * only the re-retagged packets (having the original, 26553f01c91aSVladimir Oltean * pre-initial-retagging, VLAN @tmp->vid). 26563f01c91aSVladimir Oltean */ 26573f01c91aSVladimir Oltean new_vlan[match].vlan_bc &= ~BIT(tmp->port); 26583f01c91aSVladimir Oltean 26593f01c91aSVladimir Oltean /* On reverse retagging, the same ingress VLAN goes to multiple 26603f01c91aSVladimir Oltean * ports. So we have an opportunity to create composite rules 26613f01c91aSVladimir Oltean * to not waste the limited space in the retagging table. 26623f01c91aSVladimir Oltean */ 26633f01c91aSVladimir Oltean k = sja1105_find_retagging_entry(new_retagging, *num_retagging, 26643f01c91aSVladimir Oltean upstream, rx_vid, tmp->vid); 26653f01c91aSVladimir Oltean if (k < 0) { 26663f01c91aSVladimir Oltean if (*num_retagging == SJA1105_MAX_RETAGGING_COUNT) { 26673f01c91aSVladimir Oltean dev_err(priv->ds->dev, "No more retagging rules\n"); 26683f01c91aSVladimir Oltean rc = -ENOSPC; 26693f01c91aSVladimir Oltean goto out; 26703f01c91aSVladimir Oltean } 26713f01c91aSVladimir Oltean k = (*num_retagging)++; 26723f01c91aSVladimir Oltean } 26733f01c91aSVladimir Oltean /* And the retagging itself */ 26743f01c91aSVladimir Oltean new_retagging[k].vlan_ing = rx_vid; 26753f01c91aSVladimir Oltean new_retagging[k].vlan_egr = tmp->vid; 26763f01c91aSVladimir Oltean new_retagging[k].ing_port = BIT(upstream); 26773f01c91aSVladimir Oltean new_retagging[k].egr_port |= BIT(tmp->port); 26783f01c91aSVladimir Oltean } 26793f01c91aSVladimir Oltean 26803f01c91aSVladimir Oltean out: 26813f01c91aSVladimir Oltean list_for_each_entry_safe(tmp, pos, &crosschip_vlans, list) { 26823f01c91aSVladimir Oltean list_del(&tmp->list); 26833f01c91aSVladimir Oltean kfree(tmp); 26843f01c91aSVladimir Oltean } 26853f01c91aSVladimir Oltean 26863f01c91aSVladimir Oltean return rc; 26873f01c91aSVladimir Oltean } 26883f01c91aSVladimir Oltean 2689ec5ae610SVladimir Oltean static int sja1105_build_vlan_table(struct sja1105_private *priv, bool notify); 2690ec5ae610SVladimir Oltean 2691ec5ae610SVladimir Oltean static int sja1105_notify_crosschip_switches(struct sja1105_private *priv) 2692ec5ae610SVladimir Oltean { 2693ec5ae610SVladimir Oltean struct sja1105_crosschip_switch *s, *pos; 2694ec5ae610SVladimir Oltean struct list_head crosschip_switches; 2695ec5ae610SVladimir Oltean struct dsa_8021q_crosschip_link *c; 2696ec5ae610SVladimir Oltean int rc = 0; 2697ec5ae610SVladimir Oltean 2698ec5ae610SVladimir Oltean INIT_LIST_HEAD(&crosschip_switches); 2699ec5ae610SVladimir Oltean 27005899ee36SVladimir Oltean list_for_each_entry(c, &priv->dsa_8021q_ctx->crosschip_links, list) { 2701ec5ae610SVladimir Oltean bool already_added = false; 2702ec5ae610SVladimir Oltean 2703ec5ae610SVladimir Oltean list_for_each_entry(s, &crosschip_switches, list) { 27045899ee36SVladimir Oltean if (s->other_ctx == c->other_ctx) { 2705ec5ae610SVladimir Oltean already_added = true; 2706ec5ae610SVladimir Oltean break; 2707ec5ae610SVladimir Oltean } 2708ec5ae610SVladimir Oltean } 2709ec5ae610SVladimir Oltean 2710ec5ae610SVladimir Oltean if (already_added) 2711ec5ae610SVladimir Oltean continue; 2712ec5ae610SVladimir Oltean 2713ec5ae610SVladimir Oltean s = kzalloc(sizeof(*s), GFP_KERNEL); 2714ec5ae610SVladimir Oltean if (!s) { 2715ec5ae610SVladimir Oltean dev_err(priv->ds->dev, "Failed to allocate memory\n"); 2716ec5ae610SVladimir Oltean rc = -ENOMEM; 2717ec5ae610SVladimir Oltean goto out; 2718ec5ae610SVladimir Oltean } 27195899ee36SVladimir Oltean s->other_ctx = c->other_ctx; 2720ec5ae610SVladimir Oltean list_add(&s->list, &crosschip_switches); 2721ec5ae610SVladimir Oltean } 2722ec5ae610SVladimir Oltean 2723ec5ae610SVladimir Oltean list_for_each_entry(s, &crosschip_switches, list) { 27245899ee36SVladimir Oltean struct sja1105_private *other_priv = s->other_ctx->ds->priv; 2725ec5ae610SVladimir Oltean 2726ec5ae610SVladimir Oltean rc = sja1105_build_vlan_table(other_priv, false); 2727ec5ae610SVladimir Oltean if (rc) 2728ec5ae610SVladimir Oltean goto out; 2729ec5ae610SVladimir Oltean } 2730ec5ae610SVladimir Oltean 2731ec5ae610SVladimir Oltean out: 2732ec5ae610SVladimir Oltean list_for_each_entry_safe(s, pos, &crosschip_switches, list) { 2733ec5ae610SVladimir Oltean list_del(&s->list); 2734ec5ae610SVladimir Oltean kfree(s); 2735ec5ae610SVladimir Oltean } 2736ec5ae610SVladimir Oltean 2737ec5ae610SVladimir Oltean return rc; 2738ec5ae610SVladimir Oltean } 2739ec5ae610SVladimir Oltean 2740ec5ae610SVladimir Oltean static int sja1105_build_vlan_table(struct sja1105_private *priv, bool notify) 2741ec5ae610SVladimir Oltean { 274282760d7fSVladimir Oltean u16 subvlan_map[SJA1105_MAX_NUM_PORTS][DSA_8021Q_N_SUBVLAN]; 27433f01c91aSVladimir Oltean struct sja1105_retagging_entry *new_retagging; 2744ec5ae610SVladimir Oltean struct sja1105_vlan_lookup_entry *new_vlan; 2745ec5ae610SVladimir Oltean struct sja1105_table *table; 27463f01c91aSVladimir Oltean int i, num_retagging = 0; 2747ec5ae610SVladimir Oltean int rc; 2748ec5ae610SVladimir Oltean 2749ec5ae610SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP]; 2750ec5ae610SVladimir Oltean new_vlan = kcalloc(VLAN_N_VID, 2751ec5ae610SVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 2752ec5ae610SVladimir Oltean if (!new_vlan) 2753ec5ae610SVladimir Oltean return -ENOMEM; 2754ec5ae610SVladimir Oltean 27553f01c91aSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP]; 27563f01c91aSVladimir Oltean new_retagging = kcalloc(SJA1105_MAX_RETAGGING_COUNT, 27573f01c91aSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 27583f01c91aSVladimir Oltean if (!new_retagging) { 27593f01c91aSVladimir Oltean kfree(new_vlan); 27603f01c91aSVladimir Oltean return -ENOMEM; 27613f01c91aSVladimir Oltean } 27623f01c91aSVladimir Oltean 2763ec5ae610SVladimir Oltean for (i = 0; i < VLAN_N_VID; i++) 2764ec5ae610SVladimir Oltean new_vlan[i].vlanid = VLAN_N_VID; 2765ec5ae610SVladimir Oltean 27663f01c91aSVladimir Oltean for (i = 0; i < SJA1105_MAX_RETAGGING_COUNT; i++) 27673f01c91aSVladimir Oltean new_retagging[i].vlan_ing = VLAN_N_VID; 27683f01c91aSVladimir Oltean 27693f01c91aSVladimir Oltean for (i = 0; i < priv->ds->num_ports; i++) 27703f01c91aSVladimir Oltean sja1105_init_subvlan_map(subvlan_map[i]); 27713f01c91aSVladimir Oltean 2772ec5ae610SVladimir Oltean /* Bridge VLANs */ 2773ec5ae610SVladimir Oltean rc = sja1105_build_bridge_vlans(priv, new_vlan); 2774ec5ae610SVladimir Oltean if (rc) 2775ec5ae610SVladimir Oltean goto out; 2776ec5ae610SVladimir Oltean 2777ec5ae610SVladimir Oltean /* VLANs necessary for dsa_8021q operation, given to us by tag_8021q.c: 2778ec5ae610SVladimir Oltean * - RX VLANs 2779ec5ae610SVladimir Oltean * - TX VLANs 2780ec5ae610SVladimir Oltean * - Crosschip links 2781ec5ae610SVladimir Oltean */ 2782ec5ae610SVladimir Oltean rc = sja1105_build_dsa_8021q_vlans(priv, new_vlan); 2783ec5ae610SVladimir Oltean if (rc) 2784ec5ae610SVladimir Oltean goto out; 2785ec5ae610SVladimir Oltean 27863f01c91aSVladimir Oltean /* Private VLANs necessary for dsa_8021q operation, which we need to 27873f01c91aSVladimir Oltean * determine on our own: 27883f01c91aSVladimir Oltean * - Sub-VLANs 27893f01c91aSVladimir Oltean * - Sub-VLANs of crosschip switches 27903f01c91aSVladimir Oltean */ 27913f01c91aSVladimir Oltean rc = sja1105_build_subvlans(priv, subvlan_map, new_vlan, new_retagging, 27923f01c91aSVladimir Oltean &num_retagging); 27933f01c91aSVladimir Oltean if (rc) 27943f01c91aSVladimir Oltean goto out; 27953f01c91aSVladimir Oltean 27963f01c91aSVladimir Oltean rc = sja1105_build_crosschip_subvlans(priv, new_vlan, new_retagging, 27973f01c91aSVladimir Oltean &num_retagging); 27983f01c91aSVladimir Oltean if (rc) 27993f01c91aSVladimir Oltean goto out; 28003f01c91aSVladimir Oltean 28013f01c91aSVladimir Oltean rc = sja1105_commit_vlans(priv, new_vlan, new_retagging, num_retagging); 2802ec5ae610SVladimir Oltean if (rc) 2803ec5ae610SVladimir Oltean goto out; 2804ec5ae610SVladimir Oltean 2805ec5ae610SVladimir Oltean rc = sja1105_commit_pvid(priv); 2806ec5ae610SVladimir Oltean if (rc) 2807ec5ae610SVladimir Oltean goto out; 2808ec5ae610SVladimir Oltean 28093f01c91aSVladimir Oltean for (i = 0; i < priv->ds->num_ports; i++) 28103f01c91aSVladimir Oltean sja1105_commit_subvlan_map(priv, i, subvlan_map[i]); 28113f01c91aSVladimir Oltean 2812ec5ae610SVladimir Oltean if (notify) { 2813ec5ae610SVladimir Oltean rc = sja1105_notify_crosschip_switches(priv); 2814ec5ae610SVladimir Oltean if (rc) 2815ec5ae610SVladimir Oltean goto out; 2816ec5ae610SVladimir Oltean } 2817ec5ae610SVladimir Oltean 2818ec5ae610SVladimir Oltean out: 2819ec5ae610SVladimir Oltean kfree(new_vlan); 28203f01c91aSVladimir Oltean kfree(new_retagging); 2821ec5ae610SVladimir Oltean 2822ec5ae610SVladimir Oltean return rc; 2823ec5ae610SVladimir Oltean } 2824ec5ae610SVladimir Oltean 2825070ca3bbSVladimir Oltean /* The TPID setting belongs to the General Parameters table, 2826070ca3bbSVladimir Oltean * which can only be partially reconfigured at runtime (and not the TPID). 2827070ca3bbSVladimir Oltean * So a switch reset is required. 2828070ca3bbSVladimir Oltean */ 282989153ed6SVladimir Oltean int sja1105_vlan_filtering(struct dsa_switch *ds, int port, bool enabled, 283089153ed6SVladimir Oltean struct netlink_ext_ack *extack) 28316666cebcSVladimir Oltean { 28326d7c7d94SVladimir Oltean struct sja1105_l2_lookup_params_entry *l2_lookup_params; 2833070ca3bbSVladimir Oltean struct sja1105_general_params_entry *general_params; 28346666cebcSVladimir Oltean struct sja1105_private *priv = ds->priv; 28357f14937fSVladimir Oltean enum sja1105_vlan_state state; 2836070ca3bbSVladimir Oltean struct sja1105_table *table; 2837dfacc5a2SVladimir Oltean struct sja1105_rule *rule; 28382cafa72eSVladimir Oltean bool want_tagging; 2839070ca3bbSVladimir Oltean u16 tpid, tpid2; 28406666cebcSVladimir Oltean int rc; 28416666cebcSVladimir Oltean 2842dfacc5a2SVladimir Oltean list_for_each_entry(rule, &priv->flow_block.rules, list) { 2843dfacc5a2SVladimir Oltean if (rule->type == SJA1105_RULE_VL) { 284489153ed6SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 284589153ed6SVladimir Oltean "Cannot change VLAN filtering with active VL rules"); 2846dfacc5a2SVladimir Oltean return -EBUSY; 2847dfacc5a2SVladimir Oltean } 2848dfacc5a2SVladimir Oltean } 2849dfacc5a2SVladimir Oltean 2850070ca3bbSVladimir Oltean if (enabled) { 28516666cebcSVladimir Oltean /* Enable VLAN filtering. */ 285254fa49eeSVladimir Oltean tpid = ETH_P_8021Q; 285354fa49eeSVladimir Oltean tpid2 = ETH_P_8021AD; 2854070ca3bbSVladimir Oltean } else { 28556666cebcSVladimir Oltean /* Disable VLAN filtering. */ 2856070ca3bbSVladimir Oltean tpid = ETH_P_SJA1105; 2857070ca3bbSVladimir Oltean tpid2 = ETH_P_SJA1105; 2858070ca3bbSVladimir Oltean } 2859070ca3bbSVladimir Oltean 286038b5beeaSVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 286138b5beeaSVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 286238b5beeaSVladimir Oltean 286338b5beeaSVladimir Oltean if (enabled) 286438b5beeaSVladimir Oltean sp->xmit_tpid = priv->info->qinq_tpid; 286538b5beeaSVladimir Oltean else 286638b5beeaSVladimir Oltean sp->xmit_tpid = ETH_P_SJA1105; 286738b5beeaSVladimir Oltean } 286838b5beeaSVladimir Oltean 28697f14937fSVladimir Oltean if (!enabled) 28707f14937fSVladimir Oltean state = SJA1105_VLAN_UNAWARE; 28712cafa72eSVladimir Oltean else if (priv->best_effort_vlan_filtering) 28722cafa72eSVladimir Oltean state = SJA1105_VLAN_BEST_EFFORT; 28737f14937fSVladimir Oltean else 28747f14937fSVladimir Oltean state = SJA1105_VLAN_FILTERING_FULL; 28757f14937fSVladimir Oltean 2876cfa36b1fSVladimir Oltean if (priv->vlan_state == state) 2877cfa36b1fSVladimir Oltean return 0; 2878cfa36b1fSVladimir Oltean 28797f14937fSVladimir Oltean priv->vlan_state = state; 28802cafa72eSVladimir Oltean want_tagging = (state == SJA1105_VLAN_UNAWARE || 28812cafa72eSVladimir Oltean state == SJA1105_VLAN_BEST_EFFORT); 28827f14937fSVladimir Oltean 2883070ca3bbSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; 2884070ca3bbSVladimir Oltean general_params = table->entries; 2885f9a1a764SVladimir Oltean /* EtherType used to identify inner tagged (C-tag) VLAN traffic */ 288654fa49eeSVladimir Oltean general_params->tpid = tpid; 288754fa49eeSVladimir Oltean /* EtherType used to identify outer tagged (S-tag) VLAN traffic */ 2888070ca3bbSVladimir Oltean general_params->tpid2 = tpid2; 288942824463SVladimir Oltean /* When VLAN filtering is on, we need to at least be able to 289042824463SVladimir Oltean * decode management traffic through the "backup plan". 289142824463SVladimir Oltean */ 289242824463SVladimir Oltean general_params->incl_srcpt1 = enabled; 289342824463SVladimir Oltean general_params->incl_srcpt0 = enabled; 2894070ca3bbSVladimir Oltean 28952cafa72eSVladimir Oltean want_tagging = priv->best_effort_vlan_filtering || !enabled; 28962cafa72eSVladimir Oltean 28976d7c7d94SVladimir Oltean /* VLAN filtering => independent VLAN learning. 28982cafa72eSVladimir Oltean * No VLAN filtering (or best effort) => shared VLAN learning. 28996d7c7d94SVladimir Oltean * 29006d7c7d94SVladimir Oltean * In shared VLAN learning mode, untagged traffic still gets 29016d7c7d94SVladimir Oltean * pvid-tagged, and the FDB table gets populated with entries 29026d7c7d94SVladimir Oltean * containing the "real" (pvid or from VLAN tag) VLAN ID. 29036d7c7d94SVladimir Oltean * However the switch performs a masked L2 lookup in the FDB, 29046d7c7d94SVladimir Oltean * effectively only looking up a frame's DMAC (and not VID) for the 29056d7c7d94SVladimir Oltean * forwarding decision. 29066d7c7d94SVladimir Oltean * 29076d7c7d94SVladimir Oltean * This is extremely convenient for us, because in modes with 29086d7c7d94SVladimir Oltean * vlan_filtering=0, dsa_8021q actually installs unique pvid's into 29096d7c7d94SVladimir Oltean * each front panel port. This is good for identification but breaks 29106d7c7d94SVladimir Oltean * learning badly - the VID of the learnt FDB entry is unique, aka 29116d7c7d94SVladimir Oltean * no frames coming from any other port are going to have it. So 29126d7c7d94SVladimir Oltean * for forwarding purposes, this is as though learning was broken 29136d7c7d94SVladimir Oltean * (all frames get flooded). 29146d7c7d94SVladimir Oltean */ 29156d7c7d94SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS]; 29166d7c7d94SVladimir Oltean l2_lookup_params = table->entries; 29172cafa72eSVladimir Oltean l2_lookup_params->shared_learn = want_tagging; 29186d7c7d94SVladimir Oltean 2919aaa270c6SVladimir Oltean sja1105_frame_memory_partitioning(priv); 2920aaa270c6SVladimir Oltean 2921aef31718SVladimir Oltean rc = sja1105_build_vlan_table(priv, false); 2922aef31718SVladimir Oltean if (rc) 2923aef31718SVladimir Oltean return rc; 2924aef31718SVladimir Oltean 29252eea1fa8SVladimir Oltean rc = sja1105_static_config_reload(priv, SJA1105_VLAN_FILTERING); 29266666cebcSVladimir Oltean if (rc) 292789153ed6SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, "Failed to change VLAN Ethertype"); 29286666cebcSVladimir Oltean 2929227d07a0SVladimir Oltean /* Switch port identification based on 802.1Q is only passable 2930227d07a0SVladimir Oltean * if we are not under a vlan_filtering bridge. So make sure 29312cafa72eSVladimir Oltean * the two configurations are mutually exclusive (of course, the 29322cafa72eSVladimir Oltean * user may know better, i.e. best_effort_vlan_filtering). 2933227d07a0SVladimir Oltean */ 29342cafa72eSVladimir Oltean return sja1105_setup_8021q_tagging(ds, want_tagging); 29356666cebcSVladimir Oltean } 29366666cebcSVladimir Oltean 29375899ee36SVladimir Oltean /* Returns number of VLANs added (0 or 1) on success, 29385899ee36SVladimir Oltean * or a negative error code. 29395899ee36SVladimir Oltean */ 29405899ee36SVladimir Oltean static int sja1105_vlan_add_one(struct dsa_switch *ds, int port, u16 vid, 29415899ee36SVladimir Oltean u16 flags, struct list_head *vlan_list) 29425899ee36SVladimir Oltean { 29435899ee36SVladimir Oltean bool untagged = flags & BRIDGE_VLAN_INFO_UNTAGGED; 29445899ee36SVladimir Oltean bool pvid = flags & BRIDGE_VLAN_INFO_PVID; 29455899ee36SVladimir Oltean struct sja1105_bridge_vlan *v; 29465899ee36SVladimir Oltean 2947b38e659dSVladimir Oltean list_for_each_entry(v, vlan_list, list) { 2948b38e659dSVladimir Oltean if (v->port == port && v->vid == vid) { 29495899ee36SVladimir Oltean /* Already added */ 2950b38e659dSVladimir Oltean if (v->untagged == untagged && v->pvid == pvid) 2951b38e659dSVladimir Oltean /* Nothing changed */ 29525899ee36SVladimir Oltean return 0; 29535899ee36SVladimir Oltean 2954b38e659dSVladimir Oltean /* It's the same VLAN, but some of the flags changed 2955b38e659dSVladimir Oltean * and the user did not bother to delete it first. 2956b38e659dSVladimir Oltean * Update it and trigger sja1105_build_vlan_table. 2957b38e659dSVladimir Oltean */ 2958b38e659dSVladimir Oltean v->untagged = untagged; 2959b38e659dSVladimir Oltean v->pvid = pvid; 2960b38e659dSVladimir Oltean return 1; 2961b38e659dSVladimir Oltean } 2962b38e659dSVladimir Oltean } 2963b38e659dSVladimir Oltean 29645899ee36SVladimir Oltean v = kzalloc(sizeof(*v), GFP_KERNEL); 29655899ee36SVladimir Oltean if (!v) { 29665899ee36SVladimir Oltean dev_err(ds->dev, "Out of memory while storing VLAN\n"); 29675899ee36SVladimir Oltean return -ENOMEM; 29685899ee36SVladimir Oltean } 29695899ee36SVladimir Oltean 29705899ee36SVladimir Oltean v->port = port; 29715899ee36SVladimir Oltean v->vid = vid; 29725899ee36SVladimir Oltean v->untagged = untagged; 29735899ee36SVladimir Oltean v->pvid = pvid; 29745899ee36SVladimir Oltean list_add(&v->list, vlan_list); 29755899ee36SVladimir Oltean 29765899ee36SVladimir Oltean return 1; 29775899ee36SVladimir Oltean } 29785899ee36SVladimir Oltean 29795899ee36SVladimir Oltean /* Returns number of VLANs deleted (0 or 1) */ 29805899ee36SVladimir Oltean static int sja1105_vlan_del_one(struct dsa_switch *ds, int port, u16 vid, 29815899ee36SVladimir Oltean struct list_head *vlan_list) 29825899ee36SVladimir Oltean { 29835899ee36SVladimir Oltean struct sja1105_bridge_vlan *v, *n; 29845899ee36SVladimir Oltean 29855899ee36SVladimir Oltean list_for_each_entry_safe(v, n, vlan_list, list) { 29865899ee36SVladimir Oltean if (v->port == port && v->vid == vid) { 29875899ee36SVladimir Oltean list_del(&v->list); 29885899ee36SVladimir Oltean kfree(v); 29895899ee36SVladimir Oltean return 1; 29905899ee36SVladimir Oltean } 29915899ee36SVladimir Oltean } 29925899ee36SVladimir Oltean 29935899ee36SVladimir Oltean return 0; 29945899ee36SVladimir Oltean } 29955899ee36SVladimir Oltean 29961958d581SVladimir Oltean static int sja1105_vlan_add(struct dsa_switch *ds, int port, 299731046a5fSVladimir Oltean const struct switchdev_obj_port_vlan *vlan, 299831046a5fSVladimir Oltean struct netlink_ext_ack *extack) 29996666cebcSVladimir Oltean { 30006666cebcSVladimir Oltean struct sja1105_private *priv = ds->priv; 3001ec5ae610SVladimir Oltean bool vlan_table_changed = false; 30026666cebcSVladimir Oltean int rc; 30036666cebcSVladimir Oltean 30041958d581SVladimir Oltean /* If the user wants best-effort VLAN filtering (aka vlan_filtering 30051958d581SVladimir Oltean * bridge plus tagging), be sure to at least deny alterations to the 30061958d581SVladimir Oltean * configuration done by dsa_8021q. 30071958d581SVladimir Oltean */ 30081958d581SVladimir Oltean if (priv->vlan_state != SJA1105_VLAN_FILTERING_FULL && 30091958d581SVladimir Oltean vid_is_dsa_8021q(vlan->vid)) { 301031046a5fSVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 301131046a5fSVladimir Oltean "Range 1024-3071 reserved for dsa_8021q operation"); 30121958d581SVladimir Oltean return -EBUSY; 30131958d581SVladimir Oltean } 30141958d581SVladimir Oltean 3015b7a9e0daSVladimir Oltean rc = sja1105_vlan_add_one(ds, port, vlan->vid, vlan->flags, 30165899ee36SVladimir Oltean &priv->bridge_vlans); 30175899ee36SVladimir Oltean if (rc < 0) 30181958d581SVladimir Oltean return rc; 30195899ee36SVladimir Oltean if (rc > 0) 3020ec5ae610SVladimir Oltean vlan_table_changed = true; 3021ec5ae610SVladimir Oltean 3022ec5ae610SVladimir Oltean if (!vlan_table_changed) 30231958d581SVladimir Oltean return 0; 3024ec5ae610SVladimir Oltean 30251958d581SVladimir Oltean return sja1105_build_vlan_table(priv, true); 30266666cebcSVladimir Oltean } 30276666cebcSVladimir Oltean 30286666cebcSVladimir Oltean static int sja1105_vlan_del(struct dsa_switch *ds, int port, 30296666cebcSVladimir Oltean const struct switchdev_obj_port_vlan *vlan) 30306666cebcSVladimir Oltean { 30316666cebcSVladimir Oltean struct sja1105_private *priv = ds->priv; 3032ec5ae610SVladimir Oltean bool vlan_table_changed = false; 30335899ee36SVladimir Oltean int rc; 30346666cebcSVladimir Oltean 3035b7a9e0daSVladimir Oltean rc = sja1105_vlan_del_one(ds, port, vlan->vid, &priv->bridge_vlans); 30365899ee36SVladimir Oltean if (rc > 0) 3037ec5ae610SVladimir Oltean vlan_table_changed = true; 3038ec5ae610SVladimir Oltean 3039ec5ae610SVladimir Oltean if (!vlan_table_changed) 30406666cebcSVladimir Oltean return 0; 3041ec5ae610SVladimir Oltean 3042ec5ae610SVladimir Oltean return sja1105_build_vlan_table(priv, true); 30436666cebcSVladimir Oltean } 30446666cebcSVladimir Oltean 30455899ee36SVladimir Oltean static int sja1105_dsa_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid, 30465899ee36SVladimir Oltean u16 flags) 30475899ee36SVladimir Oltean { 30485899ee36SVladimir Oltean struct sja1105_private *priv = ds->priv; 30495899ee36SVladimir Oltean int rc; 30505899ee36SVladimir Oltean 30515899ee36SVladimir Oltean rc = sja1105_vlan_add_one(ds, port, vid, flags, &priv->dsa_8021q_vlans); 30525899ee36SVladimir Oltean if (rc <= 0) 30535899ee36SVladimir Oltean return rc; 30545899ee36SVladimir Oltean 30555899ee36SVladimir Oltean return sja1105_build_vlan_table(priv, true); 30565899ee36SVladimir Oltean } 30575899ee36SVladimir Oltean 30585899ee36SVladimir Oltean static int sja1105_dsa_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid) 30595899ee36SVladimir Oltean { 30605899ee36SVladimir Oltean struct sja1105_private *priv = ds->priv; 30615899ee36SVladimir Oltean int rc; 30625899ee36SVladimir Oltean 30635899ee36SVladimir Oltean rc = sja1105_vlan_del_one(ds, port, vid, &priv->dsa_8021q_vlans); 30645899ee36SVladimir Oltean if (!rc) 30655899ee36SVladimir Oltean return 0; 30665899ee36SVladimir Oltean 30675899ee36SVladimir Oltean return sja1105_build_vlan_table(priv, true); 30685899ee36SVladimir Oltean } 30695899ee36SVladimir Oltean 30705899ee36SVladimir Oltean static const struct dsa_8021q_ops sja1105_dsa_8021q_ops = { 30715899ee36SVladimir Oltean .vlan_add = sja1105_dsa_8021q_vlan_add, 30725899ee36SVladimir Oltean .vlan_del = sja1105_dsa_8021q_vlan_del, 30735899ee36SVladimir Oltean }; 30745899ee36SVladimir Oltean 30758aa9ebccSVladimir Oltean /* The programming model for the SJA1105 switch is "all-at-once" via static 30768aa9ebccSVladimir Oltean * configuration tables. Some of these can be dynamically modified at runtime, 30778aa9ebccSVladimir Oltean * but not the xMII mode parameters table. 30788aa9ebccSVladimir Oltean * Furthermode, some PHYs may not have crystals for generating their clocks 30798aa9ebccSVladimir Oltean * (e.g. RMII). Instead, their 50MHz clock is supplied via the SJA1105 port's 30808aa9ebccSVladimir Oltean * ref_clk pin. So port clocking needs to be initialized early, before 30818aa9ebccSVladimir Oltean * connecting to PHYs is attempted, otherwise they won't respond through MDIO. 30828aa9ebccSVladimir Oltean * Setting correct PHY link speed does not matter now. 30838aa9ebccSVladimir Oltean * But dsa_slave_phy_setup is called later than sja1105_setup, so the PHY 30848aa9ebccSVladimir Oltean * bindings are not yet parsed by DSA core. We need to parse early so that we 30858aa9ebccSVladimir Oltean * can populate the xMII mode parameters table. 30868aa9ebccSVladimir Oltean */ 30878aa9ebccSVladimir Oltean static int sja1105_setup(struct dsa_switch *ds) 30888aa9ebccSVladimir Oltean { 30898aa9ebccSVladimir Oltean struct sja1105_private *priv = ds->priv; 30908aa9ebccSVladimir Oltean int rc; 30918aa9ebccSVladimir Oltean 30925d645df9SVladimir Oltean rc = sja1105_parse_dt(priv); 30938aa9ebccSVladimir Oltean if (rc < 0) { 30948aa9ebccSVladimir Oltean dev_err(ds->dev, "Failed to parse DT: %d\n", rc); 30958aa9ebccSVladimir Oltean return rc; 30968aa9ebccSVladimir Oltean } 3097f5b8631cSVladimir Oltean 3098f5b8631cSVladimir Oltean /* Error out early if internal delays are required through DT 3099f5b8631cSVladimir Oltean * and we can't apply them. 3100f5b8631cSVladimir Oltean */ 310129afb83aSVladimir Oltean rc = sja1105_parse_rgmii_delays(priv); 3102f5b8631cSVladimir Oltean if (rc < 0) { 3103f5b8631cSVladimir Oltean dev_err(ds->dev, "RGMII delay not supported\n"); 3104f5b8631cSVladimir Oltean return rc; 3105f5b8631cSVladimir Oltean } 3106f5b8631cSVladimir Oltean 310761c77126SVladimir Oltean rc = sja1105_ptp_clock_register(ds); 3108bb77f36aSVladimir Oltean if (rc < 0) { 3109bb77f36aSVladimir Oltean dev_err(ds->dev, "Failed to register PTP clock: %d\n", rc); 3110bb77f36aSVladimir Oltean return rc; 3111bb77f36aSVladimir Oltean } 31128aa9ebccSVladimir Oltean /* Create and send configuration down to device */ 31135d645df9SVladimir Oltean rc = sja1105_static_config_load(priv); 31148aa9ebccSVladimir Oltean if (rc < 0) { 31158aa9ebccSVladimir Oltean dev_err(ds->dev, "Failed to load static config: %d\n", rc); 3116cec279a8SVladimir Oltean goto out_ptp_clock_unregister; 31178aa9ebccSVladimir Oltean } 31188aa9ebccSVladimir Oltean /* Configure the CGU (PHY link modes and speeds) */ 3119c5037678SVladimir Oltean rc = priv->info->clocking_setup(priv); 31208aa9ebccSVladimir Oltean if (rc < 0) { 31218aa9ebccSVladimir Oltean dev_err(ds->dev, "Failed to configure MII clocking: %d\n", rc); 3122cec279a8SVladimir Oltean goto out_static_config_free; 31238aa9ebccSVladimir Oltean } 31246666cebcSVladimir Oltean /* On SJA1105, VLAN filtering per se is always enabled in hardware. 31256666cebcSVladimir Oltean * The only thing we can do to disable it is lie about what the 802.1Q 31266666cebcSVladimir Oltean * EtherType is. 31276666cebcSVladimir Oltean * So it will still try to apply VLAN filtering, but all ingress 31286666cebcSVladimir Oltean * traffic (except frames received with EtherType of ETH_P_SJA1105) 31296666cebcSVladimir Oltean * will be internally tagged with a distorted VLAN header where the 31306666cebcSVladimir Oltean * TPID is ETH_P_SJA1105, and the VLAN ID is the port pvid. 31316666cebcSVladimir Oltean */ 31326666cebcSVladimir Oltean ds->vlan_filtering_is_global = true; 31338aa9ebccSVladimir Oltean 31345f06c63bSVladimir Oltean /* Advertise the 8 egress queues */ 31355f06c63bSVladimir Oltean ds->num_tx_queues = SJA1105_NUM_TC; 31365f06c63bSVladimir Oltean 3137c279c726SVladimir Oltean ds->mtu_enforcement_ingress = true; 3138c279c726SVladimir Oltean 31398841f6e6SVladimir Oltean priv->best_effort_vlan_filtering = true; 31408841f6e6SVladimir Oltean 31410a7bdbc2SVladimir Oltean rc = sja1105_devlink_setup(ds); 31422cafa72eSVladimir Oltean if (rc < 0) 3143cec279a8SVladimir Oltean goto out_static_config_free; 31442cafa72eSVladimir Oltean 3145227d07a0SVladimir Oltean /* The DSA/switchdev model brings up switch ports in standalone mode by 3146227d07a0SVladimir Oltean * default, and that means vlan_filtering is 0 since they're not under 3147227d07a0SVladimir Oltean * a bridge, so it's safe to set up switch tagging at this time. 3148227d07a0SVladimir Oltean */ 3149bbed0bbdSVladimir Oltean rtnl_lock(); 3150bbed0bbdSVladimir Oltean rc = sja1105_setup_8021q_tagging(ds, true); 3151bbed0bbdSVladimir Oltean rtnl_unlock(); 3152cec279a8SVladimir Oltean if (rc) 3153cec279a8SVladimir Oltean goto out_devlink_teardown; 3154cec279a8SVladimir Oltean 3155cec279a8SVladimir Oltean return 0; 3156cec279a8SVladimir Oltean 3157cec279a8SVladimir Oltean out_devlink_teardown: 3158cec279a8SVladimir Oltean sja1105_devlink_teardown(ds); 3159cec279a8SVladimir Oltean out_ptp_clock_unregister: 3160cec279a8SVladimir Oltean sja1105_ptp_clock_unregister(ds); 3161cec279a8SVladimir Oltean out_static_config_free: 3162cec279a8SVladimir Oltean sja1105_static_config_free(&priv->static_config); 3163bbed0bbdSVladimir Oltean 3164bbed0bbdSVladimir Oltean return rc; 3165227d07a0SVladimir Oltean } 3166227d07a0SVladimir Oltean 3167f3097be2SVladimir Oltean static void sja1105_teardown(struct dsa_switch *ds) 3168f3097be2SVladimir Oltean { 3169f3097be2SVladimir Oltean struct sja1105_private *priv = ds->priv; 3170ec5ae610SVladimir Oltean struct sja1105_bridge_vlan *v, *n; 3171a68578c2SVladimir Oltean int port; 3172a68578c2SVladimir Oltean 3173542043e9SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 3174a68578c2SVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 3175a68578c2SVladimir Oltean 3176a68578c2SVladimir Oltean if (!dsa_is_user_port(ds, port)) 3177a68578c2SVladimir Oltean continue; 3178a68578c2SVladimir Oltean 317952c0d4e3SVladimir Oltean if (sp->xmit_worker) 3180a68578c2SVladimir Oltean kthread_destroy_worker(sp->xmit_worker); 3181a68578c2SVladimir Oltean } 3182f3097be2SVladimir Oltean 31830a7bdbc2SVladimir Oltean sja1105_devlink_teardown(ds); 3184a6af7763SVladimir Oltean sja1105_flower_teardown(ds); 3185317ab5b8SVladimir Oltean sja1105_tas_teardown(ds); 318661c77126SVladimir Oltean sja1105_ptp_clock_unregister(ds); 31876cb0abbdSVladimir Oltean sja1105_static_config_free(&priv->static_config); 3188ec5ae610SVladimir Oltean 3189ec5ae610SVladimir Oltean list_for_each_entry_safe(v, n, &priv->dsa_8021q_vlans, list) { 3190ec5ae610SVladimir Oltean list_del(&v->list); 3191ec5ae610SVladimir Oltean kfree(v); 3192ec5ae610SVladimir Oltean } 3193ec5ae610SVladimir Oltean 3194ec5ae610SVladimir Oltean list_for_each_entry_safe(v, n, &priv->bridge_vlans, list) { 3195ec5ae610SVladimir Oltean list_del(&v->list); 3196ec5ae610SVladimir Oltean kfree(v); 3197ec5ae610SVladimir Oltean } 3198f3097be2SVladimir Oltean } 3199f3097be2SVladimir Oltean 3200a68578c2SVladimir Oltean static void sja1105_port_disable(struct dsa_switch *ds, int port) 3201a68578c2SVladimir Oltean { 3202a68578c2SVladimir Oltean struct sja1105_private *priv = ds->priv; 3203a68578c2SVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 3204a68578c2SVladimir Oltean 3205a68578c2SVladimir Oltean if (!dsa_is_user_port(ds, port)) 3206a68578c2SVladimir Oltean return; 3207a68578c2SVladimir Oltean 3208a68578c2SVladimir Oltean kthread_cancel_work_sync(&sp->xmit_work); 3209a68578c2SVladimir Oltean skb_queue_purge(&sp->xmit_queue); 3210a68578c2SVladimir Oltean } 3211a68578c2SVladimir Oltean 3212227d07a0SVladimir Oltean static int sja1105_mgmt_xmit(struct dsa_switch *ds, int port, int slot, 321347ed985eSVladimir Oltean struct sk_buff *skb, bool takets) 3214227d07a0SVladimir Oltean { 3215227d07a0SVladimir Oltean struct sja1105_mgmt_entry mgmt_route = {0}; 3216227d07a0SVladimir Oltean struct sja1105_private *priv = ds->priv; 3217227d07a0SVladimir Oltean struct ethhdr *hdr; 3218227d07a0SVladimir Oltean int timeout = 10; 3219227d07a0SVladimir Oltean int rc; 3220227d07a0SVladimir Oltean 3221227d07a0SVladimir Oltean hdr = eth_hdr(skb); 3222227d07a0SVladimir Oltean 3223227d07a0SVladimir Oltean mgmt_route.macaddr = ether_addr_to_u64(hdr->h_dest); 3224227d07a0SVladimir Oltean mgmt_route.destports = BIT(port); 3225227d07a0SVladimir Oltean mgmt_route.enfport = 1; 322647ed985eSVladimir Oltean mgmt_route.tsreg = 0; 322747ed985eSVladimir Oltean mgmt_route.takets = takets; 3228227d07a0SVladimir Oltean 3229227d07a0SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE, 3230227d07a0SVladimir Oltean slot, &mgmt_route, true); 3231227d07a0SVladimir Oltean if (rc < 0) { 3232227d07a0SVladimir Oltean kfree_skb(skb); 3233227d07a0SVladimir Oltean return rc; 3234227d07a0SVladimir Oltean } 3235227d07a0SVladimir Oltean 3236227d07a0SVladimir Oltean /* Transfer skb to the host port. */ 323768bb8ea8SVivien Didelot dsa_enqueue_skb(skb, dsa_to_port(ds, port)->slave); 3238227d07a0SVladimir Oltean 3239227d07a0SVladimir Oltean /* Wait until the switch has processed the frame */ 3240227d07a0SVladimir Oltean do { 3241227d07a0SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_MGMT_ROUTE, 3242227d07a0SVladimir Oltean slot, &mgmt_route); 3243227d07a0SVladimir Oltean if (rc < 0) { 3244227d07a0SVladimir Oltean dev_err_ratelimited(priv->ds->dev, 3245227d07a0SVladimir Oltean "failed to poll for mgmt route\n"); 3246227d07a0SVladimir Oltean continue; 3247227d07a0SVladimir Oltean } 3248227d07a0SVladimir Oltean 3249227d07a0SVladimir Oltean /* UM10944: The ENFPORT flag of the respective entry is 3250227d07a0SVladimir Oltean * cleared when a match is found. The host can use this 3251227d07a0SVladimir Oltean * flag as an acknowledgment. 3252227d07a0SVladimir Oltean */ 3253227d07a0SVladimir Oltean cpu_relax(); 3254227d07a0SVladimir Oltean } while (mgmt_route.enfport && --timeout); 3255227d07a0SVladimir Oltean 3256227d07a0SVladimir Oltean if (!timeout) { 3257227d07a0SVladimir Oltean /* Clean up the management route so that a follow-up 3258227d07a0SVladimir Oltean * frame may not match on it by mistake. 32592a7e7409SVladimir Oltean * This is only hardware supported on P/Q/R/S - on E/T it is 32602a7e7409SVladimir Oltean * a no-op and we are silently discarding the -EOPNOTSUPP. 3261227d07a0SVladimir Oltean */ 3262227d07a0SVladimir Oltean sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE, 3263227d07a0SVladimir Oltean slot, &mgmt_route, false); 3264227d07a0SVladimir Oltean dev_err_ratelimited(priv->ds->dev, "xmit timed out\n"); 3265227d07a0SVladimir Oltean } 3266227d07a0SVladimir Oltean 3267227d07a0SVladimir Oltean return NETDEV_TX_OK; 3268227d07a0SVladimir Oltean } 3269227d07a0SVladimir Oltean 3270a68578c2SVladimir Oltean #define work_to_port(work) \ 3271a68578c2SVladimir Oltean container_of((work), struct sja1105_port, xmit_work) 3272a68578c2SVladimir Oltean #define tagger_to_sja1105(t) \ 3273a68578c2SVladimir Oltean container_of((t), struct sja1105_private, tagger_data) 3274a68578c2SVladimir Oltean 3275227d07a0SVladimir Oltean /* Deferred work is unfortunately necessary because setting up the management 3276227d07a0SVladimir Oltean * route cannot be done from atomit context (SPI transfer takes a sleepable 3277227d07a0SVladimir Oltean * lock on the bus) 3278227d07a0SVladimir Oltean */ 3279a68578c2SVladimir Oltean static void sja1105_port_deferred_xmit(struct kthread_work *work) 3280227d07a0SVladimir Oltean { 3281a68578c2SVladimir Oltean struct sja1105_port *sp = work_to_port(work); 3282a68578c2SVladimir Oltean struct sja1105_tagger_data *tagger_data = sp->data; 3283a68578c2SVladimir Oltean struct sja1105_private *priv = tagger_to_sja1105(tagger_data); 3284a68578c2SVladimir Oltean int port = sp - priv->ports; 3285a68578c2SVladimir Oltean struct sk_buff *skb; 3286a68578c2SVladimir Oltean 3287a68578c2SVladimir Oltean while ((skb = skb_dequeue(&sp->xmit_queue)) != NULL) { 3288c4b364ceSYangbo Lu struct sk_buff *clone = SJA1105_SKB_CB(skb)->clone; 3289227d07a0SVladimir Oltean 3290227d07a0SVladimir Oltean mutex_lock(&priv->mgmt_lock); 3291227d07a0SVladimir Oltean 3292a68578c2SVladimir Oltean sja1105_mgmt_xmit(priv->ds, port, 0, skb, !!clone); 3293a68578c2SVladimir Oltean 329447ed985eSVladimir Oltean /* The clone, if there, was made by dsa_skb_tx_timestamp */ 3295a68578c2SVladimir Oltean if (clone) 3296a68578c2SVladimir Oltean sja1105_ptp_txtstamp_skb(priv->ds, port, clone); 3297227d07a0SVladimir Oltean 3298227d07a0SVladimir Oltean mutex_unlock(&priv->mgmt_lock); 3299a68578c2SVladimir Oltean } 33008aa9ebccSVladimir Oltean } 33018aa9ebccSVladimir Oltean 33028456721dSVladimir Oltean /* The MAXAGE setting belongs to the L2 Forwarding Parameters table, 33038456721dSVladimir Oltean * which cannot be reconfigured at runtime. So a switch reset is required. 33048456721dSVladimir Oltean */ 33058456721dSVladimir Oltean static int sja1105_set_ageing_time(struct dsa_switch *ds, 33068456721dSVladimir Oltean unsigned int ageing_time) 33078456721dSVladimir Oltean { 33088456721dSVladimir Oltean struct sja1105_l2_lookup_params_entry *l2_lookup_params; 33098456721dSVladimir Oltean struct sja1105_private *priv = ds->priv; 33108456721dSVladimir Oltean struct sja1105_table *table; 33118456721dSVladimir Oltean unsigned int maxage; 33128456721dSVladimir Oltean 33138456721dSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS]; 33148456721dSVladimir Oltean l2_lookup_params = table->entries; 33158456721dSVladimir Oltean 33168456721dSVladimir Oltean maxage = SJA1105_AGEING_TIME_MS(ageing_time); 33178456721dSVladimir Oltean 33188456721dSVladimir Oltean if (l2_lookup_params->maxage == maxage) 33198456721dSVladimir Oltean return 0; 33208456721dSVladimir Oltean 33218456721dSVladimir Oltean l2_lookup_params->maxage = maxage; 33228456721dSVladimir Oltean 33232eea1fa8SVladimir Oltean return sja1105_static_config_reload(priv, SJA1105_AGEING_TIME); 33248456721dSVladimir Oltean } 33258456721dSVladimir Oltean 3326c279c726SVladimir Oltean static int sja1105_change_mtu(struct dsa_switch *ds, int port, int new_mtu) 3327c279c726SVladimir Oltean { 3328c279c726SVladimir Oltean struct sja1105_l2_policing_entry *policing; 3329c279c726SVladimir Oltean struct sja1105_private *priv = ds->priv; 3330c279c726SVladimir Oltean 3331c279c726SVladimir Oltean new_mtu += VLAN_ETH_HLEN + ETH_FCS_LEN; 3332c279c726SVladimir Oltean 3333c279c726SVladimir Oltean if (dsa_is_cpu_port(ds, port)) 3334c279c726SVladimir Oltean new_mtu += VLAN_HLEN; 3335c279c726SVladimir Oltean 3336c279c726SVladimir Oltean policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries; 3337c279c726SVladimir Oltean 3338a7cc081cSVladimir Oltean if (policing[port].maxlen == new_mtu) 3339c279c726SVladimir Oltean return 0; 3340c279c726SVladimir Oltean 3341a7cc081cSVladimir Oltean policing[port].maxlen = new_mtu; 3342c279c726SVladimir Oltean 3343c279c726SVladimir Oltean return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING); 3344c279c726SVladimir Oltean } 3345c279c726SVladimir Oltean 3346c279c726SVladimir Oltean static int sja1105_get_max_mtu(struct dsa_switch *ds, int port) 3347c279c726SVladimir Oltean { 3348c279c726SVladimir Oltean return 2043 - VLAN_ETH_HLEN - ETH_FCS_LEN; 3349c279c726SVladimir Oltean } 3350c279c726SVladimir Oltean 3351317ab5b8SVladimir Oltean static int sja1105_port_setup_tc(struct dsa_switch *ds, int port, 3352317ab5b8SVladimir Oltean enum tc_setup_type type, 3353317ab5b8SVladimir Oltean void *type_data) 3354317ab5b8SVladimir Oltean { 3355317ab5b8SVladimir Oltean switch (type) { 3356317ab5b8SVladimir Oltean case TC_SETUP_QDISC_TAPRIO: 3357317ab5b8SVladimir Oltean return sja1105_setup_tc_taprio(ds, port, type_data); 33584d752508SVladimir Oltean case TC_SETUP_QDISC_CBS: 33594d752508SVladimir Oltean return sja1105_setup_tc_cbs(ds, port, type_data); 3360317ab5b8SVladimir Oltean default: 3361317ab5b8SVladimir Oltean return -EOPNOTSUPP; 3362317ab5b8SVladimir Oltean } 3363317ab5b8SVladimir Oltean } 3364317ab5b8SVladimir Oltean 3365511e6ca0SVladimir Oltean /* We have a single mirror (@to) port, but can configure ingress and egress 3366511e6ca0SVladimir Oltean * mirroring on all other (@from) ports. 3367511e6ca0SVladimir Oltean * We need to allow mirroring rules only as long as the @to port is always the 3368511e6ca0SVladimir Oltean * same, and we need to unset the @to port from mirr_port only when there is no 3369511e6ca0SVladimir Oltean * mirroring rule that references it. 3370511e6ca0SVladimir Oltean */ 3371511e6ca0SVladimir Oltean static int sja1105_mirror_apply(struct sja1105_private *priv, int from, int to, 3372511e6ca0SVladimir Oltean bool ingress, bool enabled) 3373511e6ca0SVladimir Oltean { 3374511e6ca0SVladimir Oltean struct sja1105_general_params_entry *general_params; 3375511e6ca0SVladimir Oltean struct sja1105_mac_config_entry *mac; 3376542043e9SVladimir Oltean struct dsa_switch *ds = priv->ds; 3377511e6ca0SVladimir Oltean struct sja1105_table *table; 3378511e6ca0SVladimir Oltean bool already_enabled; 3379511e6ca0SVladimir Oltean u64 new_mirr_port; 3380511e6ca0SVladimir Oltean int rc; 3381511e6ca0SVladimir Oltean 3382511e6ca0SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; 3383511e6ca0SVladimir Oltean general_params = table->entries; 3384511e6ca0SVladimir Oltean 3385511e6ca0SVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 3386511e6ca0SVladimir Oltean 3387542043e9SVladimir Oltean already_enabled = (general_params->mirr_port != ds->num_ports); 3388511e6ca0SVladimir Oltean if (already_enabled && enabled && general_params->mirr_port != to) { 3389511e6ca0SVladimir Oltean dev_err(priv->ds->dev, 3390511e6ca0SVladimir Oltean "Delete mirroring rules towards port %llu first\n", 3391511e6ca0SVladimir Oltean general_params->mirr_port); 3392511e6ca0SVladimir Oltean return -EBUSY; 3393511e6ca0SVladimir Oltean } 3394511e6ca0SVladimir Oltean 3395511e6ca0SVladimir Oltean new_mirr_port = to; 3396511e6ca0SVladimir Oltean if (!enabled) { 3397511e6ca0SVladimir Oltean bool keep = false; 3398511e6ca0SVladimir Oltean int port; 3399511e6ca0SVladimir Oltean 3400511e6ca0SVladimir Oltean /* Anybody still referencing mirr_port? */ 3401542043e9SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 3402511e6ca0SVladimir Oltean if (mac[port].ing_mirr || mac[port].egr_mirr) { 3403511e6ca0SVladimir Oltean keep = true; 3404511e6ca0SVladimir Oltean break; 3405511e6ca0SVladimir Oltean } 3406511e6ca0SVladimir Oltean } 3407511e6ca0SVladimir Oltean /* Unset already_enabled for next time */ 3408511e6ca0SVladimir Oltean if (!keep) 3409542043e9SVladimir Oltean new_mirr_port = ds->num_ports; 3410511e6ca0SVladimir Oltean } 3411511e6ca0SVladimir Oltean if (new_mirr_port != general_params->mirr_port) { 3412511e6ca0SVladimir Oltean general_params->mirr_port = new_mirr_port; 3413511e6ca0SVladimir Oltean 3414511e6ca0SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_GENERAL_PARAMS, 3415511e6ca0SVladimir Oltean 0, general_params, true); 3416511e6ca0SVladimir Oltean if (rc < 0) 3417511e6ca0SVladimir Oltean return rc; 3418511e6ca0SVladimir Oltean } 3419511e6ca0SVladimir Oltean 3420511e6ca0SVladimir Oltean if (ingress) 3421511e6ca0SVladimir Oltean mac[from].ing_mirr = enabled; 3422511e6ca0SVladimir Oltean else 3423511e6ca0SVladimir Oltean mac[from].egr_mirr = enabled; 3424511e6ca0SVladimir Oltean 3425511e6ca0SVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, from, 3426511e6ca0SVladimir Oltean &mac[from], true); 3427511e6ca0SVladimir Oltean } 3428511e6ca0SVladimir Oltean 3429511e6ca0SVladimir Oltean static int sja1105_mirror_add(struct dsa_switch *ds, int port, 3430511e6ca0SVladimir Oltean struct dsa_mall_mirror_tc_entry *mirror, 3431511e6ca0SVladimir Oltean bool ingress) 3432511e6ca0SVladimir Oltean { 3433511e6ca0SVladimir Oltean return sja1105_mirror_apply(ds->priv, port, mirror->to_local_port, 3434511e6ca0SVladimir Oltean ingress, true); 3435511e6ca0SVladimir Oltean } 3436511e6ca0SVladimir Oltean 3437511e6ca0SVladimir Oltean static void sja1105_mirror_del(struct dsa_switch *ds, int port, 3438511e6ca0SVladimir Oltean struct dsa_mall_mirror_tc_entry *mirror) 3439511e6ca0SVladimir Oltean { 3440511e6ca0SVladimir Oltean sja1105_mirror_apply(ds->priv, port, mirror->to_local_port, 3441511e6ca0SVladimir Oltean mirror->ingress, false); 3442511e6ca0SVladimir Oltean } 3443511e6ca0SVladimir Oltean 3444a7cc081cSVladimir Oltean static int sja1105_port_policer_add(struct dsa_switch *ds, int port, 3445a7cc081cSVladimir Oltean struct dsa_mall_policer_tc_entry *policer) 3446a7cc081cSVladimir Oltean { 3447a7cc081cSVladimir Oltean struct sja1105_l2_policing_entry *policing; 3448a7cc081cSVladimir Oltean struct sja1105_private *priv = ds->priv; 3449a7cc081cSVladimir Oltean 3450a7cc081cSVladimir Oltean policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries; 3451a7cc081cSVladimir Oltean 3452a7cc081cSVladimir Oltean /* In hardware, every 8 microseconds the credit level is incremented by 3453a7cc081cSVladimir Oltean * the value of RATE bytes divided by 64, up to a maximum of SMAX 3454a7cc081cSVladimir Oltean * bytes. 3455a7cc081cSVladimir Oltean */ 3456a7cc081cSVladimir Oltean policing[port].rate = div_u64(512 * policer->rate_bytes_per_sec, 3457a7cc081cSVladimir Oltean 1000000); 34585f035af7SPo Liu policing[port].smax = policer->burst; 3459a7cc081cSVladimir Oltean 3460a7cc081cSVladimir Oltean return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING); 3461a7cc081cSVladimir Oltean } 3462a7cc081cSVladimir Oltean 3463a7cc081cSVladimir Oltean static void sja1105_port_policer_del(struct dsa_switch *ds, int port) 3464a7cc081cSVladimir Oltean { 3465a7cc081cSVladimir Oltean struct sja1105_l2_policing_entry *policing; 3466a7cc081cSVladimir Oltean struct sja1105_private *priv = ds->priv; 3467a7cc081cSVladimir Oltean 3468a7cc081cSVladimir Oltean policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries; 3469a7cc081cSVladimir Oltean 3470a7cc081cSVladimir Oltean policing[port].rate = SJA1105_RATE_MBPS(1000); 3471a7cc081cSVladimir Oltean policing[port].smax = 65535; 3472a7cc081cSVladimir Oltean 3473a7cc081cSVladimir Oltean sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING); 3474a7cc081cSVladimir Oltean } 3475a7cc081cSVladimir Oltean 34764d942354SVladimir Oltean static int sja1105_port_set_learning(struct sja1105_private *priv, int port, 34774d942354SVladimir Oltean bool enabled) 34784d942354SVladimir Oltean { 34794d942354SVladimir Oltean struct sja1105_mac_config_entry *mac; 34804d942354SVladimir Oltean int rc; 34814d942354SVladimir Oltean 34824d942354SVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 34834d942354SVladimir Oltean 34844c44fc5eSVladimir Oltean mac[port].dyn_learn = enabled; 34854d942354SVladimir Oltean 34864d942354SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, 34874d942354SVladimir Oltean &mac[port], true); 34884d942354SVladimir Oltean if (rc) 34894d942354SVladimir Oltean return rc; 34904d942354SVladimir Oltean 34914d942354SVladimir Oltean if (enabled) 34924d942354SVladimir Oltean priv->learn_ena |= BIT(port); 34934d942354SVladimir Oltean else 34944d942354SVladimir Oltean priv->learn_ena &= ~BIT(port); 34954d942354SVladimir Oltean 34964d942354SVladimir Oltean return 0; 34974d942354SVladimir Oltean } 34984d942354SVladimir Oltean 34994d942354SVladimir Oltean static int sja1105_port_ucast_bcast_flood(struct sja1105_private *priv, int to, 35004d942354SVladimir Oltean struct switchdev_brport_flags flags) 35014d942354SVladimir Oltean { 35024d942354SVladimir Oltean if (flags.mask & BR_FLOOD) { 35034d942354SVladimir Oltean if (flags.val & BR_FLOOD) 35047f7ccdeaSVladimir Oltean priv->ucast_egress_floods |= BIT(to); 35054d942354SVladimir Oltean else 35066a5166e0SVladimir Oltean priv->ucast_egress_floods &= ~BIT(to); 35074d942354SVladimir Oltean } 35087f7ccdeaSVladimir Oltean 35094d942354SVladimir Oltean if (flags.mask & BR_BCAST_FLOOD) { 35104d942354SVladimir Oltean if (flags.val & BR_BCAST_FLOOD) 35117f7ccdeaSVladimir Oltean priv->bcast_egress_floods |= BIT(to); 35124d942354SVladimir Oltean else 35136a5166e0SVladimir Oltean priv->bcast_egress_floods &= ~BIT(to); 35144d942354SVladimir Oltean } 35154d942354SVladimir Oltean 35167f7ccdeaSVladimir Oltean return sja1105_manage_flood_domains(priv); 35174d942354SVladimir Oltean } 35184d942354SVladimir Oltean 35194d942354SVladimir Oltean static int sja1105_port_mcast_flood(struct sja1105_private *priv, int to, 35204d942354SVladimir Oltean struct switchdev_brport_flags flags, 35214d942354SVladimir Oltean struct netlink_ext_ack *extack) 35224d942354SVladimir Oltean { 35234d942354SVladimir Oltean struct sja1105_l2_lookup_entry *l2_lookup; 35244d942354SVladimir Oltean struct sja1105_table *table; 35254d942354SVladimir Oltean int match; 35264d942354SVladimir Oltean 35274d942354SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; 35284d942354SVladimir Oltean l2_lookup = table->entries; 35294d942354SVladimir Oltean 35304d942354SVladimir Oltean for (match = 0; match < table->entry_count; match++) 35314d942354SVladimir Oltean if (l2_lookup[match].macaddr == SJA1105_UNKNOWN_MULTICAST && 35324d942354SVladimir Oltean l2_lookup[match].mask_macaddr == SJA1105_UNKNOWN_MULTICAST) 35334d942354SVladimir Oltean break; 35344d942354SVladimir Oltean 35354d942354SVladimir Oltean if (match == table->entry_count) { 35364d942354SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 35374d942354SVladimir Oltean "Could not find FDB entry for unknown multicast"); 35384d942354SVladimir Oltean return -ENOSPC; 35394d942354SVladimir Oltean } 35404d942354SVladimir Oltean 35414d942354SVladimir Oltean if (flags.val & BR_MCAST_FLOOD) 35424d942354SVladimir Oltean l2_lookup[match].destports |= BIT(to); 35434d942354SVladimir Oltean else 35444d942354SVladimir Oltean l2_lookup[match].destports &= ~BIT(to); 35454d942354SVladimir Oltean 35464d942354SVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 35474d942354SVladimir Oltean l2_lookup[match].index, 35484d942354SVladimir Oltean &l2_lookup[match], 35494d942354SVladimir Oltean true); 35504d942354SVladimir Oltean } 35514d942354SVladimir Oltean 35524d942354SVladimir Oltean static int sja1105_port_pre_bridge_flags(struct dsa_switch *ds, int port, 35534d942354SVladimir Oltean struct switchdev_brport_flags flags, 35544d942354SVladimir Oltean struct netlink_ext_ack *extack) 35554d942354SVladimir Oltean { 35564d942354SVladimir Oltean struct sja1105_private *priv = ds->priv; 35574d942354SVladimir Oltean 35584d942354SVladimir Oltean if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | 35594d942354SVladimir Oltean BR_BCAST_FLOOD)) 35604d942354SVladimir Oltean return -EINVAL; 35614d942354SVladimir Oltean 35624d942354SVladimir Oltean if (flags.mask & (BR_FLOOD | BR_MCAST_FLOOD) && 35634d942354SVladimir Oltean !priv->info->can_limit_mcast_flood) { 35644d942354SVladimir Oltean bool multicast = !!(flags.val & BR_MCAST_FLOOD); 35654d942354SVladimir Oltean bool unicast = !!(flags.val & BR_FLOOD); 35664d942354SVladimir Oltean 35674d942354SVladimir Oltean if (unicast != multicast) { 35684d942354SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 35694d942354SVladimir Oltean "This chip cannot configure multicast flooding independently of unicast"); 35704d942354SVladimir Oltean return -EINVAL; 35714d942354SVladimir Oltean } 35724d942354SVladimir Oltean } 35734d942354SVladimir Oltean 35744d942354SVladimir Oltean return 0; 35754d942354SVladimir Oltean } 35764d942354SVladimir Oltean 35774d942354SVladimir Oltean static int sja1105_port_bridge_flags(struct dsa_switch *ds, int port, 35784d942354SVladimir Oltean struct switchdev_brport_flags flags, 35794d942354SVladimir Oltean struct netlink_ext_ack *extack) 35804d942354SVladimir Oltean { 35814d942354SVladimir Oltean struct sja1105_private *priv = ds->priv; 35824d942354SVladimir Oltean int rc; 35834d942354SVladimir Oltean 35844d942354SVladimir Oltean if (flags.mask & BR_LEARNING) { 35854d942354SVladimir Oltean bool learn_ena = !!(flags.val & BR_LEARNING); 35864d942354SVladimir Oltean 35874d942354SVladimir Oltean rc = sja1105_port_set_learning(priv, port, learn_ena); 35884d942354SVladimir Oltean if (rc) 35894d942354SVladimir Oltean return rc; 35904d942354SVladimir Oltean } 35914d942354SVladimir Oltean 35924d942354SVladimir Oltean if (flags.mask & (BR_FLOOD | BR_BCAST_FLOOD)) { 35934d942354SVladimir Oltean rc = sja1105_port_ucast_bcast_flood(priv, port, flags); 35944d942354SVladimir Oltean if (rc) 35954d942354SVladimir Oltean return rc; 35964d942354SVladimir Oltean } 35974d942354SVladimir Oltean 35984d942354SVladimir Oltean /* For chips that can't offload BR_MCAST_FLOOD independently, there 35994d942354SVladimir Oltean * is nothing to do here, we ensured the configuration is in sync by 36004d942354SVladimir Oltean * offloading BR_FLOOD. 36014d942354SVladimir Oltean */ 36024d942354SVladimir Oltean if (flags.mask & BR_MCAST_FLOOD && priv->info->can_limit_mcast_flood) { 36034d942354SVladimir Oltean rc = sja1105_port_mcast_flood(priv, port, flags, 36044d942354SVladimir Oltean extack); 36054d942354SVladimir Oltean if (rc) 36064d942354SVladimir Oltean return rc; 36074d942354SVladimir Oltean } 36084d942354SVladimir Oltean 36094d942354SVladimir Oltean return 0; 36104d942354SVladimir Oltean } 36114d942354SVladimir Oltean 36128aa9ebccSVladimir Oltean static const struct dsa_switch_ops sja1105_switch_ops = { 36138aa9ebccSVladimir Oltean .get_tag_protocol = sja1105_get_tag_protocol, 36148aa9ebccSVladimir Oltean .setup = sja1105_setup, 3615f3097be2SVladimir Oltean .teardown = sja1105_teardown, 36168456721dSVladimir Oltean .set_ageing_time = sja1105_set_ageing_time, 3617c279c726SVladimir Oltean .port_change_mtu = sja1105_change_mtu, 3618c279c726SVladimir Oltean .port_max_mtu = sja1105_get_max_mtu, 3619ad9f299aSVladimir Oltean .phylink_validate = sja1105_phylink_validate, 3620ffe10e67SVladimir Oltean .phylink_mac_link_state = sja1105_mac_pcs_get_state, 3621af7cd036SVladimir Oltean .phylink_mac_config = sja1105_mac_config, 36228400cff6SVladimir Oltean .phylink_mac_link_up = sja1105_mac_link_up, 36238400cff6SVladimir Oltean .phylink_mac_link_down = sja1105_mac_link_down, 362452c34e6eSVladimir Oltean .get_strings = sja1105_get_strings, 362552c34e6eSVladimir Oltean .get_ethtool_stats = sja1105_get_ethtool_stats, 362652c34e6eSVladimir Oltean .get_sset_count = sja1105_get_sset_count, 3627bb77f36aSVladimir Oltean .get_ts_info = sja1105_get_ts_info, 3628a68578c2SVladimir Oltean .port_disable = sja1105_port_disable, 3629291d1e72SVladimir Oltean .port_fdb_dump = sja1105_fdb_dump, 3630291d1e72SVladimir Oltean .port_fdb_add = sja1105_fdb_add, 3631291d1e72SVladimir Oltean .port_fdb_del = sja1105_fdb_del, 36328aa9ebccSVladimir Oltean .port_bridge_join = sja1105_bridge_join, 36338aa9ebccSVladimir Oltean .port_bridge_leave = sja1105_bridge_leave, 36344d942354SVladimir Oltean .port_pre_bridge_flags = sja1105_port_pre_bridge_flags, 36354d942354SVladimir Oltean .port_bridge_flags = sja1105_port_bridge_flags, 3636640f763fSVladimir Oltean .port_stp_state_set = sja1105_bridge_stp_state_set, 36376666cebcSVladimir Oltean .port_vlan_filtering = sja1105_vlan_filtering, 36386666cebcSVladimir Oltean .port_vlan_add = sja1105_vlan_add, 36396666cebcSVladimir Oltean .port_vlan_del = sja1105_vlan_del, 3640291d1e72SVladimir Oltean .port_mdb_add = sja1105_mdb_add, 3641291d1e72SVladimir Oltean .port_mdb_del = sja1105_mdb_del, 3642a602afd2SVladimir Oltean .port_hwtstamp_get = sja1105_hwtstamp_get, 3643a602afd2SVladimir Oltean .port_hwtstamp_set = sja1105_hwtstamp_set, 3644f3097be2SVladimir Oltean .port_rxtstamp = sja1105_port_rxtstamp, 364547ed985eSVladimir Oltean .port_txtstamp = sja1105_port_txtstamp, 3646317ab5b8SVladimir Oltean .port_setup_tc = sja1105_port_setup_tc, 3647511e6ca0SVladimir Oltean .port_mirror_add = sja1105_mirror_add, 3648511e6ca0SVladimir Oltean .port_mirror_del = sja1105_mirror_del, 3649a7cc081cSVladimir Oltean .port_policer_add = sja1105_port_policer_add, 3650a7cc081cSVladimir Oltean .port_policer_del = sja1105_port_policer_del, 3651a6af7763SVladimir Oltean .cls_flower_add = sja1105_cls_flower_add, 3652a6af7763SVladimir Oltean .cls_flower_del = sja1105_cls_flower_del, 3653834f8933SVladimir Oltean .cls_flower_stats = sja1105_cls_flower_stats, 3654ac02a451SVladimir Oltean .crosschip_bridge_join = sja1105_crosschip_bridge_join, 3655ac02a451SVladimir Oltean .crosschip_bridge_leave = sja1105_crosschip_bridge_leave, 36562cafa72eSVladimir Oltean .devlink_param_get = sja1105_devlink_param_get, 36572cafa72eSVladimir Oltean .devlink_param_set = sja1105_devlink_param_set, 3658ff4cf8eaSVladimir Oltean .devlink_info_get = sja1105_devlink_info_get, 36598aa9ebccSVladimir Oltean }; 36608aa9ebccSVladimir Oltean 36610b0e2997SVladimir Oltean static const struct of_device_id sja1105_dt_ids[]; 36620b0e2997SVladimir Oltean 36638aa9ebccSVladimir Oltean static int sja1105_check_device_id(struct sja1105_private *priv) 36648aa9ebccSVladimir Oltean { 36658aa9ebccSVladimir Oltean const struct sja1105_regs *regs = priv->info->regs; 36668aa9ebccSVladimir Oltean u8 prod_id[SJA1105_SIZE_DEVICE_ID] = {0}; 36678aa9ebccSVladimir Oltean struct device *dev = &priv->spidev->dev; 36680b0e2997SVladimir Oltean const struct of_device_id *match; 3669dff79620SVladimir Oltean u32 device_id; 36708aa9ebccSVladimir Oltean u64 part_no; 36718aa9ebccSVladimir Oltean int rc; 36728aa9ebccSVladimir Oltean 367334d76e9fSVladimir Oltean rc = sja1105_xfer_u32(priv, SPI_READ, regs->device_id, &device_id, 367434d76e9fSVladimir Oltean NULL); 36758aa9ebccSVladimir Oltean if (rc < 0) 36768aa9ebccSVladimir Oltean return rc; 36778aa9ebccSVladimir Oltean 36781bd44870SVladimir Oltean rc = sja1105_xfer_buf(priv, SPI_READ, regs->prod_id, prod_id, 36791bd44870SVladimir Oltean SJA1105_SIZE_DEVICE_ID); 36808aa9ebccSVladimir Oltean if (rc < 0) 36818aa9ebccSVladimir Oltean return rc; 36828aa9ebccSVladimir Oltean 36838aa9ebccSVladimir Oltean sja1105_unpack(prod_id, &part_no, 19, 4, SJA1105_SIZE_DEVICE_ID); 36848aa9ebccSVladimir Oltean 36855978fac0SNathan Chancellor for (match = sja1105_dt_ids; match->compatible[0]; match++) { 36860b0e2997SVladimir Oltean const struct sja1105_info *info = match->data; 36870b0e2997SVladimir Oltean 36880b0e2997SVladimir Oltean /* Is what's been probed in our match table at all? */ 36890b0e2997SVladimir Oltean if (info->device_id != device_id || info->part_no != part_no) 36900b0e2997SVladimir Oltean continue; 36910b0e2997SVladimir Oltean 36920b0e2997SVladimir Oltean /* But is it what's in the device tree? */ 36930b0e2997SVladimir Oltean if (priv->info->device_id != device_id || 36940b0e2997SVladimir Oltean priv->info->part_no != part_no) { 36950b0e2997SVladimir Oltean dev_warn(dev, "Device tree specifies chip %s but found %s, please fix it!\n", 36960b0e2997SVladimir Oltean priv->info->name, info->name); 36970b0e2997SVladimir Oltean /* It isn't. No problem, pick that up. */ 36980b0e2997SVladimir Oltean priv->info = info; 36998aa9ebccSVladimir Oltean } 37008aa9ebccSVladimir Oltean 37018aa9ebccSVladimir Oltean return 0; 37028aa9ebccSVladimir Oltean } 37038aa9ebccSVladimir Oltean 37040b0e2997SVladimir Oltean dev_err(dev, "Unexpected {device ID, part number}: 0x%x 0x%llx\n", 37050b0e2997SVladimir Oltean device_id, part_no); 37060b0e2997SVladimir Oltean 37070b0e2997SVladimir Oltean return -ENODEV; 37080b0e2997SVladimir Oltean } 37090b0e2997SVladimir Oltean 37108aa9ebccSVladimir Oltean static int sja1105_probe(struct spi_device *spi) 37118aa9ebccSVladimir Oltean { 3712844d7edcSVladimir Oltean struct sja1105_tagger_data *tagger_data; 37138aa9ebccSVladimir Oltean struct device *dev = &spi->dev; 37148aa9ebccSVladimir Oltean struct sja1105_private *priv; 3715718bad0eSVladimir Oltean size_t max_xfer, max_msg; 37168aa9ebccSVladimir Oltean struct dsa_switch *ds; 3717a68578c2SVladimir Oltean int rc, port; 37188aa9ebccSVladimir Oltean 37198aa9ebccSVladimir Oltean if (!dev->of_node) { 37208aa9ebccSVladimir Oltean dev_err(dev, "No DTS bindings for SJA1105 driver\n"); 37218aa9ebccSVladimir Oltean return -EINVAL; 37228aa9ebccSVladimir Oltean } 37238aa9ebccSVladimir Oltean 37248aa9ebccSVladimir Oltean priv = devm_kzalloc(dev, sizeof(struct sja1105_private), GFP_KERNEL); 37258aa9ebccSVladimir Oltean if (!priv) 37268aa9ebccSVladimir Oltean return -ENOMEM; 37278aa9ebccSVladimir Oltean 37288aa9ebccSVladimir Oltean /* Configure the optional reset pin and bring up switch */ 37298aa9ebccSVladimir Oltean priv->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); 37308aa9ebccSVladimir Oltean if (IS_ERR(priv->reset_gpio)) 37318aa9ebccSVladimir Oltean dev_dbg(dev, "reset-gpios not defined, ignoring\n"); 37328aa9ebccSVladimir Oltean else 37338aa9ebccSVladimir Oltean sja1105_hw_reset(priv->reset_gpio, 1, 1); 37348aa9ebccSVladimir Oltean 37358aa9ebccSVladimir Oltean /* Populate our driver private structure (priv) based on 37368aa9ebccSVladimir Oltean * the device tree node that was probed (spi) 37378aa9ebccSVladimir Oltean */ 37388aa9ebccSVladimir Oltean priv->spidev = spi; 37398aa9ebccSVladimir Oltean spi_set_drvdata(spi, priv); 37408aa9ebccSVladimir Oltean 37418aa9ebccSVladimir Oltean /* Configure the SPI bus */ 37428aa9ebccSVladimir Oltean spi->bits_per_word = 8; 37438aa9ebccSVladimir Oltean rc = spi_setup(spi); 37448aa9ebccSVladimir Oltean if (rc < 0) { 37458aa9ebccSVladimir Oltean dev_err(dev, "Could not init SPI\n"); 37468aa9ebccSVladimir Oltean return rc; 37478aa9ebccSVladimir Oltean } 37488aa9ebccSVladimir Oltean 3749718bad0eSVladimir Oltean /* In sja1105_xfer, we send spi_messages composed of two spi_transfers: 3750718bad0eSVladimir Oltean * a small one for the message header and another one for the current 3751718bad0eSVladimir Oltean * chunk of the packed buffer. 3752718bad0eSVladimir Oltean * Check that the restrictions imposed by the SPI controller are 3753718bad0eSVladimir Oltean * respected: the chunk buffer is smaller than the max transfer size, 3754718bad0eSVladimir Oltean * and the total length of the chunk plus its message header is smaller 3755718bad0eSVladimir Oltean * than the max message size. 3756718bad0eSVladimir Oltean * We do that during probe time since the maximum transfer size is a 3757718bad0eSVladimir Oltean * runtime invariant. 3758718bad0eSVladimir Oltean */ 3759718bad0eSVladimir Oltean max_xfer = spi_max_transfer_size(spi); 3760718bad0eSVladimir Oltean max_msg = spi_max_message_size(spi); 3761718bad0eSVladimir Oltean 3762718bad0eSVladimir Oltean /* We need to send at least one 64-bit word of SPI payload per message 3763718bad0eSVladimir Oltean * in order to be able to make useful progress. 3764718bad0eSVladimir Oltean */ 3765718bad0eSVladimir Oltean if (max_msg < SJA1105_SIZE_SPI_MSG_HEADER + 8) { 3766718bad0eSVladimir Oltean dev_err(dev, "SPI master cannot send large enough buffers, aborting\n"); 3767718bad0eSVladimir Oltean return -EINVAL; 3768718bad0eSVladimir Oltean } 3769718bad0eSVladimir Oltean 3770718bad0eSVladimir Oltean priv->max_xfer_len = SJA1105_SIZE_SPI_MSG_MAXLEN; 3771718bad0eSVladimir Oltean if (priv->max_xfer_len > max_xfer) 3772718bad0eSVladimir Oltean priv->max_xfer_len = max_xfer; 3773718bad0eSVladimir Oltean if (priv->max_xfer_len > max_msg - SJA1105_SIZE_SPI_MSG_HEADER) 3774718bad0eSVladimir Oltean priv->max_xfer_len = max_msg - SJA1105_SIZE_SPI_MSG_HEADER; 3775718bad0eSVladimir Oltean 37768aa9ebccSVladimir Oltean priv->info = of_device_get_match_data(dev); 37778aa9ebccSVladimir Oltean 37788aa9ebccSVladimir Oltean /* Detect hardware device */ 37798aa9ebccSVladimir Oltean rc = sja1105_check_device_id(priv); 37808aa9ebccSVladimir Oltean if (rc < 0) { 37818aa9ebccSVladimir Oltean dev_err(dev, "Device ID check failed: %d\n", rc); 37828aa9ebccSVladimir Oltean return rc; 37838aa9ebccSVladimir Oltean } 37848aa9ebccSVladimir Oltean 37858aa9ebccSVladimir Oltean dev_info(dev, "Probed switch chip: %s\n", priv->info->name); 37868aa9ebccSVladimir Oltean 37877e99e347SVivien Didelot ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL); 37888aa9ebccSVladimir Oltean if (!ds) 37898aa9ebccSVladimir Oltean return -ENOMEM; 37908aa9ebccSVladimir Oltean 37917e99e347SVivien Didelot ds->dev = dev; 37923e77e59bSVladimir Oltean ds->num_ports = priv->info->num_ports; 37938aa9ebccSVladimir Oltean ds->ops = &sja1105_switch_ops; 37948aa9ebccSVladimir Oltean ds->priv = priv; 37958aa9ebccSVladimir Oltean priv->ds = ds; 37968aa9ebccSVladimir Oltean 3797844d7edcSVladimir Oltean tagger_data = &priv->tagger_data; 3798844d7edcSVladimir Oltean 3799d5a619bfSVivien Didelot mutex_init(&priv->ptp_data.lock); 3800d5a619bfSVivien Didelot mutex_init(&priv->mgmt_lock); 3801d5a619bfSVivien Didelot 38025899ee36SVladimir Oltean priv->dsa_8021q_ctx = devm_kzalloc(dev, sizeof(*priv->dsa_8021q_ctx), 38035899ee36SVladimir Oltean GFP_KERNEL); 38045899ee36SVladimir Oltean if (!priv->dsa_8021q_ctx) 38055899ee36SVladimir Oltean return -ENOMEM; 38065899ee36SVladimir Oltean 38075899ee36SVladimir Oltean priv->dsa_8021q_ctx->ops = &sja1105_dsa_8021q_ops; 3808bbed0bbdSVladimir Oltean priv->dsa_8021q_ctx->proto = htons(ETH_P_8021Q); 38095899ee36SVladimir Oltean priv->dsa_8021q_ctx->ds = ds; 38105899ee36SVladimir Oltean 38115899ee36SVladimir Oltean INIT_LIST_HEAD(&priv->dsa_8021q_ctx->crosschip_links); 3812ec5ae610SVladimir Oltean INIT_LIST_HEAD(&priv->bridge_vlans); 3813ec5ae610SVladimir Oltean INIT_LIST_HEAD(&priv->dsa_8021q_vlans); 3814ac02a451SVladimir Oltean 3815d5a619bfSVivien Didelot sja1105_tas_setup(ds); 3816a6af7763SVladimir Oltean sja1105_flower_setup(ds); 3817d5a619bfSVivien Didelot 3818d5a619bfSVivien Didelot rc = dsa_register_switch(priv->ds); 3819d5a619bfSVivien Didelot if (rc) 3820d5a619bfSVivien Didelot return rc; 3821d5a619bfSVivien Didelot 38224d752508SVladimir Oltean if (IS_ENABLED(CONFIG_NET_SCH_CBS)) { 38234d752508SVladimir Oltean priv->cbs = devm_kcalloc(dev, priv->info->num_cbs_shapers, 38244d752508SVladimir Oltean sizeof(struct sja1105_cbs_entry), 38254d752508SVladimir Oltean GFP_KERNEL); 3826dc596e3fSVladimir Oltean if (!priv->cbs) { 3827dc596e3fSVladimir Oltean rc = -ENOMEM; 3828dc596e3fSVladimir Oltean goto out_unregister_switch; 3829dc596e3fSVladimir Oltean } 38304d752508SVladimir Oltean } 38314d752508SVladimir Oltean 3832227d07a0SVladimir Oltean /* Connections between dsa_port and sja1105_port */ 3833542043e9SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 3834a68578c2SVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 3835a68578c2SVladimir Oltean struct dsa_port *dp = dsa_to_port(ds, port); 3836a68578c2SVladimir Oltean struct net_device *slave; 383784eeb5d4SVladimir Oltean int subvlan; 3838227d07a0SVladimir Oltean 3839a68578c2SVladimir Oltean if (!dsa_is_user_port(ds, port)) 3840a68578c2SVladimir Oltean continue; 3841a68578c2SVladimir Oltean 3842a68578c2SVladimir Oltean dp->priv = sp; 3843a68578c2SVladimir Oltean sp->dp = dp; 3844844d7edcSVladimir Oltean sp->data = tagger_data; 3845a68578c2SVladimir Oltean slave = dp->slave; 3846a68578c2SVladimir Oltean kthread_init_work(&sp->xmit_work, sja1105_port_deferred_xmit); 3847a68578c2SVladimir Oltean sp->xmit_worker = kthread_create_worker(0, "%s_xmit", 3848a68578c2SVladimir Oltean slave->name); 3849a68578c2SVladimir Oltean if (IS_ERR(sp->xmit_worker)) { 3850a68578c2SVladimir Oltean rc = PTR_ERR(sp->xmit_worker); 3851a68578c2SVladimir Oltean dev_err(ds->dev, 3852a68578c2SVladimir Oltean "failed to create deferred xmit thread: %d\n", 3853a68578c2SVladimir Oltean rc); 3854dc596e3fSVladimir Oltean goto out_destroy_workers; 3855a68578c2SVladimir Oltean } 3856a68578c2SVladimir Oltean skb_queue_head_init(&sp->xmit_queue); 385738b5beeaSVladimir Oltean sp->xmit_tpid = ETH_P_SJA1105; 385884eeb5d4SVladimir Oltean 385984eeb5d4SVladimir Oltean for (subvlan = 0; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++) 386084eeb5d4SVladimir Oltean sp->subvlan_map[subvlan] = VLAN_N_VID; 3861227d07a0SVladimir Oltean } 3862227d07a0SVladimir Oltean 3863d5a619bfSVivien Didelot return 0; 3864dc596e3fSVladimir Oltean 3865dc596e3fSVladimir Oltean out_destroy_workers: 3866a68578c2SVladimir Oltean while (port-- > 0) { 3867a68578c2SVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 3868a68578c2SVladimir Oltean 3869a68578c2SVladimir Oltean if (!dsa_is_user_port(ds, port)) 3870a68578c2SVladimir Oltean continue; 3871a68578c2SVladimir Oltean 3872a68578c2SVladimir Oltean kthread_destroy_worker(sp->xmit_worker); 3873a68578c2SVladimir Oltean } 3874dc596e3fSVladimir Oltean 3875dc596e3fSVladimir Oltean out_unregister_switch: 3876dc596e3fSVladimir Oltean dsa_unregister_switch(ds); 3877dc596e3fSVladimir Oltean 3878a68578c2SVladimir Oltean return rc; 38798aa9ebccSVladimir Oltean } 38808aa9ebccSVladimir Oltean 38818aa9ebccSVladimir Oltean static int sja1105_remove(struct spi_device *spi) 38828aa9ebccSVladimir Oltean { 38838aa9ebccSVladimir Oltean struct sja1105_private *priv = spi_get_drvdata(spi); 38848aa9ebccSVladimir Oltean 38858aa9ebccSVladimir Oltean dsa_unregister_switch(priv->ds); 38868aa9ebccSVladimir Oltean return 0; 38878aa9ebccSVladimir Oltean } 38888aa9ebccSVladimir Oltean 38898aa9ebccSVladimir Oltean static const struct of_device_id sja1105_dt_ids[] = { 38908aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105e", .data = &sja1105e_info }, 38918aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105t", .data = &sja1105t_info }, 38928aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105p", .data = &sja1105p_info }, 38938aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105q", .data = &sja1105q_info }, 38948aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105r", .data = &sja1105r_info }, 38958aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105s", .data = &sja1105s_info }, 38963e77e59bSVladimir Oltean { .compatible = "nxp,sja1110a", .data = &sja1110a_info }, 38973e77e59bSVladimir Oltean { .compatible = "nxp,sja1110b", .data = &sja1110b_info }, 38983e77e59bSVladimir Oltean { .compatible = "nxp,sja1110c", .data = &sja1110c_info }, 38993e77e59bSVladimir Oltean { .compatible = "nxp,sja1110d", .data = &sja1110d_info }, 39008aa9ebccSVladimir Oltean { /* sentinel */ }, 39018aa9ebccSVladimir Oltean }; 39028aa9ebccSVladimir Oltean MODULE_DEVICE_TABLE(of, sja1105_dt_ids); 39038aa9ebccSVladimir Oltean 39048aa9ebccSVladimir Oltean static struct spi_driver sja1105_driver = { 39058aa9ebccSVladimir Oltean .driver = { 39068aa9ebccSVladimir Oltean .name = "sja1105", 39078aa9ebccSVladimir Oltean .owner = THIS_MODULE, 39088aa9ebccSVladimir Oltean .of_match_table = of_match_ptr(sja1105_dt_ids), 39098aa9ebccSVladimir Oltean }, 39108aa9ebccSVladimir Oltean .probe = sja1105_probe, 39118aa9ebccSVladimir Oltean .remove = sja1105_remove, 39128aa9ebccSVladimir Oltean }; 39138aa9ebccSVladimir Oltean 39148aa9ebccSVladimir Oltean module_spi_driver(sja1105_driver); 39158aa9ebccSVladimir Oltean 39168aa9ebccSVladimir Oltean MODULE_AUTHOR("Vladimir Oltean <olteanv@gmail.com>"); 39178aa9ebccSVladimir Oltean MODULE_AUTHOR("Georg Waibel <georg.waibel@sensor-technik.de>"); 39188aa9ebccSVladimir Oltean MODULE_DESCRIPTION("SJA1105 Driver"); 39198aa9ebccSVladimir Oltean MODULE_LICENSE("GPL v2"); 3920