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 28*4d942354SVladimir Oltean #define SJA1105_UNKNOWN_MULTICAST 0x010000000000ull 29*4d942354SVladimir Oltean 30ac02a451SVladimir Oltean static const struct dsa_switch_ops sja1105_switch_ops; 31ac02a451SVladimir Oltean 328aa9ebccSVladimir Oltean static void sja1105_hw_reset(struct gpio_desc *gpio, unsigned int pulse_len, 338aa9ebccSVladimir Oltean unsigned int startup_delay) 348aa9ebccSVladimir Oltean { 358aa9ebccSVladimir Oltean gpiod_set_value_cansleep(gpio, 1); 368aa9ebccSVladimir Oltean /* Wait for minimum reset pulse length */ 378aa9ebccSVladimir Oltean msleep(pulse_len); 388aa9ebccSVladimir Oltean gpiod_set_value_cansleep(gpio, 0); 398aa9ebccSVladimir Oltean /* Wait until chip is ready after reset */ 408aa9ebccSVladimir Oltean msleep(startup_delay); 418aa9ebccSVladimir Oltean } 428aa9ebccSVladimir Oltean 438aa9ebccSVladimir Oltean static void 448aa9ebccSVladimir Oltean sja1105_port_allow_traffic(struct sja1105_l2_forwarding_entry *l2_fwd, 458aa9ebccSVladimir Oltean int from, int to, bool allow) 468aa9ebccSVladimir Oltean { 47*4d942354SVladimir Oltean if (allow) 488aa9ebccSVladimir Oltean l2_fwd[from].reach_port |= BIT(to); 49*4d942354SVladimir Oltean else 508aa9ebccSVladimir Oltean l2_fwd[from].reach_port &= ~BIT(to); 518aa9ebccSVladimir Oltean } 528aa9ebccSVladimir Oltean 538aa9ebccSVladimir Oltean /* Structure used to temporarily transport device tree 548aa9ebccSVladimir Oltean * settings into sja1105_setup 558aa9ebccSVladimir Oltean */ 568aa9ebccSVladimir Oltean struct sja1105_dt_port { 578aa9ebccSVladimir Oltean phy_interface_t phy_mode; 588aa9ebccSVladimir Oltean sja1105_mii_role_t role; 598aa9ebccSVladimir Oltean }; 608aa9ebccSVladimir Oltean 618aa9ebccSVladimir Oltean static int sja1105_init_mac_settings(struct sja1105_private *priv) 628aa9ebccSVladimir Oltean { 638aa9ebccSVladimir Oltean struct sja1105_mac_config_entry default_mac = { 648aa9ebccSVladimir Oltean /* Enable all 8 priority queues on egress. 658aa9ebccSVladimir Oltean * Every queue i holds top[i] - base[i] frames. 668aa9ebccSVladimir Oltean * Sum of top[i] - base[i] is 511 (max hardware limit). 678aa9ebccSVladimir Oltean */ 688aa9ebccSVladimir Oltean .top = {0x3F, 0x7F, 0xBF, 0xFF, 0x13F, 0x17F, 0x1BF, 0x1FF}, 698aa9ebccSVladimir Oltean .base = {0x0, 0x40, 0x80, 0xC0, 0x100, 0x140, 0x180, 0x1C0}, 708aa9ebccSVladimir Oltean .enabled = {true, true, true, true, true, true, true, true}, 718aa9ebccSVladimir Oltean /* Keep standard IFG of 12 bytes on egress. */ 728aa9ebccSVladimir Oltean .ifg = 0, 738aa9ebccSVladimir Oltean /* Always put the MAC speed in automatic mode, where it can be 741fd4a173SVladimir Oltean * adjusted at runtime by PHYLINK. 758aa9ebccSVladimir Oltean */ 768aa9ebccSVladimir Oltean .speed = SJA1105_SPEED_AUTO, 778aa9ebccSVladimir Oltean /* No static correction for 1-step 1588 events */ 788aa9ebccSVladimir Oltean .tp_delin = 0, 798aa9ebccSVladimir Oltean .tp_delout = 0, 808aa9ebccSVladimir Oltean /* Disable aging for critical TTEthernet traffic */ 818aa9ebccSVladimir Oltean .maxage = 0xFF, 828aa9ebccSVladimir Oltean /* Internal VLAN (pvid) to apply to untagged ingress */ 838aa9ebccSVladimir Oltean .vlanprio = 0, 84e3502b82SVladimir Oltean .vlanid = 1, 858aa9ebccSVladimir Oltean .ing_mirr = false, 868aa9ebccSVladimir Oltean .egr_mirr = false, 878aa9ebccSVladimir Oltean /* Don't drop traffic with other EtherType than ETH_P_IP */ 888aa9ebccSVladimir Oltean .drpnona664 = false, 898aa9ebccSVladimir Oltean /* Don't drop double-tagged traffic */ 908aa9ebccSVladimir Oltean .drpdtag = false, 918aa9ebccSVladimir Oltean /* Don't drop untagged traffic */ 928aa9ebccSVladimir Oltean .drpuntag = false, 938aa9ebccSVladimir Oltean /* Don't retag 802.1p (VID 0) traffic with the pvid */ 948aa9ebccSVladimir Oltean .retag = false, 95640f763fSVladimir Oltean /* Disable learning and I/O on user ports by default - 96640f763fSVladimir Oltean * STP will enable it. 97640f763fSVladimir Oltean */ 98640f763fSVladimir Oltean .dyn_learn = false, 998aa9ebccSVladimir Oltean .egress = false, 1008aa9ebccSVladimir Oltean .ingress = false, 1018aa9ebccSVladimir Oltean }; 1028aa9ebccSVladimir Oltean struct sja1105_mac_config_entry *mac; 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 1148aa9ebccSVladimir Oltean table->entries = kcalloc(SJA1105_NUM_PORTS, 1158aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 1168aa9ebccSVladimir Oltean if (!table->entries) 1178aa9ebccSVladimir Oltean return -ENOMEM; 1188aa9ebccSVladimir Oltean 1198aa9ebccSVladimir Oltean table->entry_count = SJA1105_NUM_PORTS; 1208aa9ebccSVladimir Oltean 1218aa9ebccSVladimir Oltean mac = table->entries; 1228aa9ebccSVladimir Oltean 123640f763fSVladimir Oltean for (i = 0; i < SJA1105_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 138ffe10e67SVladimir Oltean static bool sja1105_supports_sgmii(struct sja1105_private *priv, int port) 139ffe10e67SVladimir Oltean { 140ffe10e67SVladimir Oltean if (priv->info->part_no != SJA1105R_PART_NO && 141ffe10e67SVladimir Oltean priv->info->part_no != SJA1105S_PART_NO) 142ffe10e67SVladimir Oltean return false; 143ffe10e67SVladimir Oltean 144ffe10e67SVladimir Oltean if (port != SJA1105_SGMII_PORT) 145ffe10e67SVladimir Oltean return false; 146ffe10e67SVladimir Oltean 147ffe10e67SVladimir Oltean if (dsa_is_unused_port(priv->ds, port)) 148ffe10e67SVladimir Oltean return false; 149ffe10e67SVladimir Oltean 150ffe10e67SVladimir Oltean return true; 151ffe10e67SVladimir Oltean } 152ffe10e67SVladimir Oltean 1538aa9ebccSVladimir Oltean static int sja1105_init_mii_settings(struct sja1105_private *priv, 1548aa9ebccSVladimir Oltean struct sja1105_dt_port *ports) 1558aa9ebccSVladimir Oltean { 1568aa9ebccSVladimir Oltean struct device *dev = &priv->spidev->dev; 1578aa9ebccSVladimir Oltean struct sja1105_xmii_params_entry *mii; 1588aa9ebccSVladimir Oltean struct sja1105_table *table; 1598aa9ebccSVladimir Oltean int i; 1608aa9ebccSVladimir Oltean 1618aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_XMII_PARAMS]; 1628aa9ebccSVladimir Oltean 1638aa9ebccSVladimir Oltean /* Discard previous xMII Mode Parameters Table */ 1648aa9ebccSVladimir Oltean if (table->entry_count) { 1658aa9ebccSVladimir Oltean kfree(table->entries); 1668aa9ebccSVladimir Oltean table->entry_count = 0; 1678aa9ebccSVladimir Oltean } 1688aa9ebccSVladimir Oltean 1698aa9ebccSVladimir Oltean table->entries = kcalloc(SJA1105_MAX_XMII_PARAMS_COUNT, 1708aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 1718aa9ebccSVladimir Oltean if (!table->entries) 1728aa9ebccSVladimir Oltean return -ENOMEM; 1738aa9ebccSVladimir Oltean 1741fd4a173SVladimir Oltean /* Override table based on PHYLINK DT bindings */ 1758aa9ebccSVladimir Oltean table->entry_count = SJA1105_MAX_XMII_PARAMS_COUNT; 1768aa9ebccSVladimir Oltean 1778aa9ebccSVladimir Oltean mii = table->entries; 1788aa9ebccSVladimir Oltean 1798aa9ebccSVladimir Oltean for (i = 0; i < SJA1105_NUM_PORTS; i++) { 180ee9d0cb6SVladimir Oltean if (dsa_is_unused_port(priv->ds, i)) 181ee9d0cb6SVladimir Oltean continue; 182ee9d0cb6SVladimir Oltean 1838aa9ebccSVladimir Oltean switch (ports[i].phy_mode) { 1848aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_MII: 1858aa9ebccSVladimir Oltean mii->xmii_mode[i] = XMII_MODE_MII; 1868aa9ebccSVladimir Oltean break; 1878aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RMII: 1888aa9ebccSVladimir Oltean mii->xmii_mode[i] = XMII_MODE_RMII; 1898aa9ebccSVladimir Oltean break; 1908aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RGMII: 1918aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RGMII_ID: 1928aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RGMII_RXID: 1938aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RGMII_TXID: 1948aa9ebccSVladimir Oltean mii->xmii_mode[i] = XMII_MODE_RGMII; 1958aa9ebccSVladimir Oltean break; 196ffe10e67SVladimir Oltean case PHY_INTERFACE_MODE_SGMII: 197ffe10e67SVladimir Oltean if (!sja1105_supports_sgmii(priv, i)) 198ffe10e67SVladimir Oltean return -EINVAL; 199ffe10e67SVladimir Oltean mii->xmii_mode[i] = XMII_MODE_SGMII; 200ffe10e67SVladimir Oltean break; 2018aa9ebccSVladimir Oltean default: 2028aa9ebccSVladimir Oltean dev_err(dev, "Unsupported PHY mode %s!\n", 2038aa9ebccSVladimir Oltean phy_modes(ports[i].phy_mode)); 2048aa9ebccSVladimir Oltean } 2058aa9ebccSVladimir Oltean 206ffe10e67SVladimir Oltean /* Even though the SerDes port is able to drive SGMII autoneg 207ffe10e67SVladimir Oltean * like a PHY would, from the perspective of the XMII tables, 208ffe10e67SVladimir Oltean * the SGMII port should always be put in MAC mode. 209ffe10e67SVladimir Oltean */ 210ffe10e67SVladimir Oltean if (ports[i].phy_mode == PHY_INTERFACE_MODE_SGMII) 211ffe10e67SVladimir Oltean mii->phy_mac[i] = XMII_MAC; 212ffe10e67SVladimir Oltean else 2138aa9ebccSVladimir Oltean mii->phy_mac[i] = ports[i].role; 2148aa9ebccSVladimir Oltean } 2158aa9ebccSVladimir Oltean return 0; 2168aa9ebccSVladimir Oltean } 2178aa9ebccSVladimir Oltean 2188aa9ebccSVladimir Oltean static int sja1105_init_static_fdb(struct sja1105_private *priv) 2198aa9ebccSVladimir Oltean { 220*4d942354SVladimir Oltean struct sja1105_l2_lookup_entry *l2_lookup; 2218aa9ebccSVladimir Oltean struct sja1105_table *table; 222*4d942354SVladimir Oltean int port; 2238aa9ebccSVladimir Oltean 2248aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; 2258aa9ebccSVladimir Oltean 226*4d942354SVladimir Oltean /* We only populate the FDB table through dynamic L2 Address Lookup 227*4d942354SVladimir Oltean * entries, except for a special entry at the end which is a catch-all 228*4d942354SVladimir Oltean * for unknown multicast and will be used to control flooding domain. 229291d1e72SVladimir Oltean */ 2308aa9ebccSVladimir Oltean if (table->entry_count) { 2318aa9ebccSVladimir Oltean kfree(table->entries); 2328aa9ebccSVladimir Oltean table->entry_count = 0; 2338aa9ebccSVladimir Oltean } 234*4d942354SVladimir Oltean 235*4d942354SVladimir Oltean if (!priv->info->can_limit_mcast_flood) 236*4d942354SVladimir Oltean return 0; 237*4d942354SVladimir Oltean 238*4d942354SVladimir Oltean table->entries = kcalloc(1, table->ops->unpacked_entry_size, 239*4d942354SVladimir Oltean GFP_KERNEL); 240*4d942354SVladimir Oltean if (!table->entries) 241*4d942354SVladimir Oltean return -ENOMEM; 242*4d942354SVladimir Oltean 243*4d942354SVladimir Oltean table->entry_count = 1; 244*4d942354SVladimir Oltean l2_lookup = table->entries; 245*4d942354SVladimir Oltean 246*4d942354SVladimir Oltean /* All L2 multicast addresses have an odd first octet */ 247*4d942354SVladimir Oltean l2_lookup[0].macaddr = SJA1105_UNKNOWN_MULTICAST; 248*4d942354SVladimir Oltean l2_lookup[0].mask_macaddr = SJA1105_UNKNOWN_MULTICAST; 249*4d942354SVladimir Oltean l2_lookup[0].lockeds = true; 250*4d942354SVladimir Oltean l2_lookup[0].index = SJA1105_MAX_L2_LOOKUP_COUNT - 1; 251*4d942354SVladimir Oltean 252*4d942354SVladimir Oltean /* Flood multicast to every port by default */ 253*4d942354SVladimir Oltean for (port = 0; port < priv->ds->num_ports; port++) 254*4d942354SVladimir Oltean if (!dsa_is_unused_port(priv->ds, port)) 255*4d942354SVladimir Oltean l2_lookup[0].destports |= BIT(port); 256*4d942354SVladimir Oltean 2578aa9ebccSVladimir Oltean return 0; 2588aa9ebccSVladimir Oltean } 2598aa9ebccSVladimir Oltean 2608aa9ebccSVladimir Oltean static int sja1105_init_l2_lookup_params(struct sja1105_private *priv) 2618aa9ebccSVladimir Oltean { 2628aa9ebccSVladimir Oltean struct sja1105_table *table; 2636c56e167SVladimir Oltean u64 max_fdb_entries = SJA1105_MAX_L2_LOOKUP_COUNT / SJA1105_NUM_PORTS; 2648aa9ebccSVladimir Oltean struct sja1105_l2_lookup_params_entry default_l2_lookup_params = { 2658456721dSVladimir Oltean /* Learned FDB entries are forgotten after 300 seconds */ 2668456721dSVladimir Oltean .maxage = SJA1105_AGEING_TIME_MS(300000), 2678aa9ebccSVladimir Oltean /* All entries within a FDB bin are available for learning */ 2688aa9ebccSVladimir Oltean .dyn_tbsz = SJA1105ET_FDB_BIN_SIZE, 2691da73821SVladimir Oltean /* And the P/Q/R/S equivalent setting: */ 2701da73821SVladimir Oltean .start_dynspc = 0, 2716c56e167SVladimir Oltean .maxaddrp = {max_fdb_entries, max_fdb_entries, max_fdb_entries, 2726c56e167SVladimir Oltean max_fdb_entries, max_fdb_entries, }, 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 }; 2968aa9ebccSVladimir Oltean 2978aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS]; 2988aa9ebccSVladimir Oltean 2998aa9ebccSVladimir Oltean if (table->entry_count) { 3008aa9ebccSVladimir Oltean kfree(table->entries); 3018aa9ebccSVladimir Oltean table->entry_count = 0; 3028aa9ebccSVladimir Oltean } 3038aa9ebccSVladimir Oltean 3048aa9ebccSVladimir Oltean table->entries = kcalloc(SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT, 3058aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 3068aa9ebccSVladimir Oltean if (!table->entries) 3078aa9ebccSVladimir Oltean return -ENOMEM; 3088aa9ebccSVladimir Oltean 3098aa9ebccSVladimir Oltean table->entry_count = SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT; 3108aa9ebccSVladimir Oltean 3118aa9ebccSVladimir Oltean /* This table only has a single entry */ 3128aa9ebccSVladimir Oltean ((struct sja1105_l2_lookup_params_entry *)table->entries)[0] = 3138aa9ebccSVladimir Oltean default_l2_lookup_params; 3148aa9ebccSVladimir Oltean 3158aa9ebccSVladimir Oltean return 0; 3168aa9ebccSVladimir Oltean } 3178aa9ebccSVladimir Oltean 3188aa9ebccSVladimir Oltean static int sja1105_init_static_vlan(struct sja1105_private *priv) 3198aa9ebccSVladimir Oltean { 3208aa9ebccSVladimir Oltean struct sja1105_table *table; 3218aa9ebccSVladimir Oltean struct sja1105_vlan_lookup_entry pvid = { 3228aa9ebccSVladimir Oltean .ving_mirr = 0, 3238aa9ebccSVladimir Oltean .vegr_mirr = 0, 3248aa9ebccSVladimir Oltean .vmemb_port = 0, 3258aa9ebccSVladimir Oltean .vlan_bc = 0, 3268aa9ebccSVladimir Oltean .tag_port = 0, 327e3502b82SVladimir Oltean .vlanid = 1, 3288aa9ebccSVladimir Oltean }; 329ec5ae610SVladimir Oltean struct dsa_switch *ds = priv->ds; 330ec5ae610SVladimir Oltean int port; 3318aa9ebccSVladimir Oltean 3328aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP]; 3338aa9ebccSVladimir Oltean 334e3502b82SVladimir Oltean /* The static VLAN table will only contain the initial pvid of 1. 3356666cebcSVladimir Oltean * All other VLANs are to be configured through dynamic entries, 3366666cebcSVladimir Oltean * and kept in the static configuration table as backing memory. 3378aa9ebccSVladimir Oltean */ 3388aa9ebccSVladimir Oltean if (table->entry_count) { 3398aa9ebccSVladimir Oltean kfree(table->entries); 3408aa9ebccSVladimir Oltean table->entry_count = 0; 3418aa9ebccSVladimir Oltean } 3428aa9ebccSVladimir Oltean 343c75857b0SZheng Yongjun table->entries = kzalloc(table->ops->unpacked_entry_size, 3448aa9ebccSVladimir Oltean GFP_KERNEL); 3458aa9ebccSVladimir Oltean if (!table->entries) 3468aa9ebccSVladimir Oltean return -ENOMEM; 3478aa9ebccSVladimir Oltean 3488aa9ebccSVladimir Oltean table->entry_count = 1; 3498aa9ebccSVladimir Oltean 350e3502b82SVladimir Oltean /* VLAN 1: all DT-defined ports are members; no restrictions on 351ec5ae610SVladimir Oltean * forwarding; always transmit as untagged. 3528aa9ebccSVladimir Oltean */ 353ec5ae610SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 354ec5ae610SVladimir Oltean struct sja1105_bridge_vlan *v; 355ec5ae610SVladimir Oltean 356ec5ae610SVladimir Oltean if (dsa_is_unused_port(ds, port)) 357ec5ae610SVladimir Oltean continue; 358ec5ae610SVladimir Oltean 359ec5ae610SVladimir Oltean pvid.vmemb_port |= BIT(port); 360ec5ae610SVladimir Oltean pvid.vlan_bc |= BIT(port); 361ec5ae610SVladimir Oltean pvid.tag_port &= ~BIT(port); 362ec5ae610SVladimir Oltean 363ec5ae610SVladimir Oltean /* Let traffic that don't need dsa_8021q (e.g. STP, PTP) be 364ec5ae610SVladimir Oltean * transmitted as untagged. 365ec5ae610SVladimir Oltean */ 366ec5ae610SVladimir Oltean v = kzalloc(sizeof(*v), GFP_KERNEL); 367ec5ae610SVladimir Oltean if (!v) 368ec5ae610SVladimir Oltean return -ENOMEM; 369ec5ae610SVladimir Oltean 370ec5ae610SVladimir Oltean v->port = port; 371ec5ae610SVladimir Oltean v->vid = 1; 372ec5ae610SVladimir Oltean v->untagged = true; 373ec5ae610SVladimir Oltean if (dsa_is_cpu_port(ds, port)) 374ec5ae610SVladimir Oltean v->pvid = true; 375ec5ae610SVladimir Oltean list_add(&v->list, &priv->dsa_8021q_vlans); 3768aa9ebccSVladimir Oltean } 3778aa9ebccSVladimir Oltean 3788aa9ebccSVladimir Oltean ((struct sja1105_vlan_lookup_entry *)table->entries)[0] = pvid; 3798aa9ebccSVladimir Oltean return 0; 3808aa9ebccSVladimir Oltean } 3818aa9ebccSVladimir Oltean 3828aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding(struct sja1105_private *priv) 3838aa9ebccSVladimir Oltean { 3848aa9ebccSVladimir Oltean struct sja1105_l2_forwarding_entry *l2fwd; 3858aa9ebccSVladimir Oltean struct sja1105_table *table; 3868aa9ebccSVladimir Oltean int i, j; 3878aa9ebccSVladimir Oltean 3888aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING]; 3898aa9ebccSVladimir Oltean 3908aa9ebccSVladimir Oltean if (table->entry_count) { 3918aa9ebccSVladimir Oltean kfree(table->entries); 3928aa9ebccSVladimir Oltean table->entry_count = 0; 3938aa9ebccSVladimir Oltean } 3948aa9ebccSVladimir Oltean 3958aa9ebccSVladimir Oltean table->entries = kcalloc(SJA1105_MAX_L2_FORWARDING_COUNT, 3968aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 3978aa9ebccSVladimir Oltean if (!table->entries) 3988aa9ebccSVladimir Oltean return -ENOMEM; 3998aa9ebccSVladimir Oltean 4008aa9ebccSVladimir Oltean table->entry_count = SJA1105_MAX_L2_FORWARDING_COUNT; 4018aa9ebccSVladimir Oltean 4028aa9ebccSVladimir Oltean l2fwd = table->entries; 4038aa9ebccSVladimir Oltean 4048aa9ebccSVladimir Oltean /* First 5 entries define the forwarding rules */ 4058aa9ebccSVladimir Oltean for (i = 0; i < SJA1105_NUM_PORTS; i++) { 4068aa9ebccSVladimir Oltean unsigned int upstream = dsa_upstream_port(priv->ds, i); 4078aa9ebccSVladimir Oltean 4088aa9ebccSVladimir Oltean for (j = 0; j < SJA1105_NUM_TC; j++) 4098aa9ebccSVladimir Oltean l2fwd[i].vlan_pmap[j] = j; 4108aa9ebccSVladimir Oltean 4118aa9ebccSVladimir Oltean if (i == upstream) 4128aa9ebccSVladimir Oltean continue; 4138aa9ebccSVladimir Oltean 4148aa9ebccSVladimir Oltean sja1105_port_allow_traffic(l2fwd, i, upstream, true); 4158aa9ebccSVladimir Oltean sja1105_port_allow_traffic(l2fwd, upstream, i, true); 416*4d942354SVladimir Oltean 417*4d942354SVladimir Oltean l2fwd[i].bc_domain = BIT(upstream); 418*4d942354SVladimir Oltean l2fwd[i].fl_domain = BIT(upstream); 419*4d942354SVladimir Oltean 420*4d942354SVladimir Oltean l2fwd[upstream].bc_domain |= BIT(i); 421*4d942354SVladimir Oltean l2fwd[upstream].fl_domain |= BIT(i); 4228aa9ebccSVladimir Oltean } 4238aa9ebccSVladimir Oltean /* Next 8 entries define VLAN PCP mapping from ingress to egress. 4248aa9ebccSVladimir Oltean * Create a one-to-one mapping. 4258aa9ebccSVladimir Oltean */ 4268aa9ebccSVladimir Oltean for (i = 0; i < SJA1105_NUM_TC; i++) 4278aa9ebccSVladimir Oltean for (j = 0; j < SJA1105_NUM_PORTS; j++) 4288aa9ebccSVladimir Oltean l2fwd[SJA1105_NUM_PORTS + i].vlan_pmap[j] = i; 4298aa9ebccSVladimir Oltean 4308aa9ebccSVladimir Oltean return 0; 4318aa9ebccSVladimir Oltean } 4328aa9ebccSVladimir Oltean 4338aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding_params(struct sja1105_private *priv) 4348aa9ebccSVladimir Oltean { 4358aa9ebccSVladimir Oltean struct sja1105_l2_forwarding_params_entry default_l2fwd_params = { 4368aa9ebccSVladimir Oltean /* Disallow dynamic reconfiguration of vlan_pmap */ 4378aa9ebccSVladimir Oltean .max_dynp = 0, 4388aa9ebccSVladimir Oltean /* Use a single memory partition for all ingress queues */ 4398aa9ebccSVladimir Oltean .part_spc = { SJA1105_MAX_FRAME_MEMORY, 0, 0, 0, 0, 0, 0, 0 }, 4408aa9ebccSVladimir Oltean }; 4418aa9ebccSVladimir Oltean struct sja1105_table *table; 4428aa9ebccSVladimir Oltean 4438aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS]; 4448aa9ebccSVladimir Oltean 4458aa9ebccSVladimir Oltean if (table->entry_count) { 4468aa9ebccSVladimir Oltean kfree(table->entries); 4478aa9ebccSVladimir Oltean table->entry_count = 0; 4488aa9ebccSVladimir Oltean } 4498aa9ebccSVladimir Oltean 4508aa9ebccSVladimir Oltean table->entries = kcalloc(SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT, 4518aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 4528aa9ebccSVladimir Oltean if (!table->entries) 4538aa9ebccSVladimir Oltean return -ENOMEM; 4548aa9ebccSVladimir Oltean 4558aa9ebccSVladimir Oltean table->entry_count = SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT; 4568aa9ebccSVladimir Oltean 4578aa9ebccSVladimir Oltean /* This table only has a single entry */ 4588aa9ebccSVladimir Oltean ((struct sja1105_l2_forwarding_params_entry *)table->entries)[0] = 4598aa9ebccSVladimir Oltean default_l2fwd_params; 4608aa9ebccSVladimir Oltean 4618aa9ebccSVladimir Oltean return 0; 4628aa9ebccSVladimir Oltean } 4638aa9ebccSVladimir Oltean 464aaa270c6SVladimir Oltean void sja1105_frame_memory_partitioning(struct sja1105_private *priv) 465aaa270c6SVladimir Oltean { 466aaa270c6SVladimir Oltean struct sja1105_l2_forwarding_params_entry *l2_fwd_params; 467aaa270c6SVladimir Oltean struct sja1105_vl_forwarding_params_entry *vl_fwd_params; 468aaa270c6SVladimir Oltean struct sja1105_table *table; 469aaa270c6SVladimir Oltean int max_mem; 470aaa270c6SVladimir Oltean 471aaa270c6SVladimir Oltean /* VLAN retagging is implemented using a loopback port that consumes 472aaa270c6SVladimir Oltean * frame buffers. That leaves less for us. 473aaa270c6SVladimir Oltean */ 474aaa270c6SVladimir Oltean if (priv->vlan_state == SJA1105_VLAN_BEST_EFFORT) 475aaa270c6SVladimir Oltean max_mem = SJA1105_MAX_FRAME_MEMORY_RETAGGING; 476aaa270c6SVladimir Oltean else 477aaa270c6SVladimir Oltean max_mem = SJA1105_MAX_FRAME_MEMORY; 478aaa270c6SVladimir Oltean 479aaa270c6SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS]; 480aaa270c6SVladimir Oltean l2_fwd_params = table->entries; 481aaa270c6SVladimir Oltean l2_fwd_params->part_spc[0] = max_mem; 482aaa270c6SVladimir Oltean 483aaa270c6SVladimir Oltean /* If we have any critical-traffic virtual links, we need to reserve 484aaa270c6SVladimir Oltean * some frame buffer memory for them. At the moment, hardcode the value 485aaa270c6SVladimir Oltean * at 100 blocks of 128 bytes of memory each. This leaves 829 blocks 486aaa270c6SVladimir Oltean * remaining for best-effort traffic. TODO: figure out a more flexible 487aaa270c6SVladimir Oltean * way to perform the frame buffer partitioning. 488aaa270c6SVladimir Oltean */ 489aaa270c6SVladimir Oltean if (!priv->static_config.tables[BLK_IDX_VL_FORWARDING].entry_count) 490aaa270c6SVladimir Oltean return; 491aaa270c6SVladimir Oltean 492aaa270c6SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_VL_FORWARDING_PARAMS]; 493aaa270c6SVladimir Oltean vl_fwd_params = table->entries; 494aaa270c6SVladimir Oltean 495aaa270c6SVladimir Oltean l2_fwd_params->part_spc[0] -= SJA1105_VL_FRAME_MEMORY; 496aaa270c6SVladimir Oltean vl_fwd_params->partspc[0] = SJA1105_VL_FRAME_MEMORY; 497aaa270c6SVladimir Oltean } 498aaa270c6SVladimir Oltean 4998aa9ebccSVladimir Oltean static int sja1105_init_general_params(struct sja1105_private *priv) 5008aa9ebccSVladimir Oltean { 5018aa9ebccSVladimir Oltean struct sja1105_general_params_entry default_general_params = { 502511e6ca0SVladimir Oltean /* Allow dynamic changing of the mirror port */ 503511e6ca0SVladimir Oltean .mirr_ptacu = true, 5048aa9ebccSVladimir Oltean .switchid = priv->ds->index, 5055f06c63bSVladimir Oltean /* Priority queue for link-local management frames 5065f06c63bSVladimir Oltean * (both ingress to and egress from CPU - PTP, STP etc) 5075f06c63bSVladimir Oltean */ 50808fde09aSVladimir Oltean .hostprio = 7, 5098aa9ebccSVladimir Oltean .mac_fltres1 = SJA1105_LINKLOCAL_FILTER_A, 5108aa9ebccSVladimir Oltean .mac_flt1 = SJA1105_LINKLOCAL_FILTER_A_MASK, 51142824463SVladimir Oltean .incl_srcpt1 = false, 5128aa9ebccSVladimir Oltean .send_meta1 = false, 5138aa9ebccSVladimir Oltean .mac_fltres0 = SJA1105_LINKLOCAL_FILTER_B, 5148aa9ebccSVladimir Oltean .mac_flt0 = SJA1105_LINKLOCAL_FILTER_B_MASK, 51542824463SVladimir Oltean .incl_srcpt0 = false, 5168aa9ebccSVladimir Oltean .send_meta0 = false, 5178aa9ebccSVladimir Oltean /* The destination for traffic matching mac_fltres1 and 5188aa9ebccSVladimir Oltean * mac_fltres0 on all ports except host_port. Such traffic 5198aa9ebccSVladimir Oltean * receieved on host_port itself would be dropped, except 5208aa9ebccSVladimir Oltean * by installing a temporary 'management route' 5218aa9ebccSVladimir Oltean */ 5228aa9ebccSVladimir Oltean .host_port = dsa_upstream_port(priv->ds, 0), 523511e6ca0SVladimir Oltean /* Default to an invalid value */ 524511e6ca0SVladimir Oltean .mirr_port = SJA1105_NUM_PORTS, 5258aa9ebccSVladimir Oltean /* Link-local traffic received on casc_port will be forwarded 5268aa9ebccSVladimir Oltean * to host_port without embedding the source port and device ID 5278aa9ebccSVladimir Oltean * info in the destination MAC address (presumably because it 5288aa9ebccSVladimir Oltean * is a cascaded port and a downstream SJA switch already did 5298aa9ebccSVladimir Oltean * that). Default to an invalid port (to disable the feature) 5308aa9ebccSVladimir Oltean * and overwrite this if we find any DSA (cascaded) ports. 5318aa9ebccSVladimir Oltean */ 5328aa9ebccSVladimir Oltean .casc_port = SJA1105_NUM_PORTS, 5338aa9ebccSVladimir Oltean /* No TTEthernet */ 534dfacc5a2SVladimir Oltean .vllupformat = SJA1105_VL_FORMAT_PSFP, 5358aa9ebccSVladimir Oltean .vlmarker = 0, 5368aa9ebccSVladimir Oltean .vlmask = 0, 5378aa9ebccSVladimir Oltean /* Only update correctionField for 1-step PTP (L2 transport) */ 5388aa9ebccSVladimir Oltean .ignore2stf = 0, 5396666cebcSVladimir Oltean /* Forcefully disable VLAN filtering by telling 5406666cebcSVladimir Oltean * the switch that VLAN has a different EtherType. 5416666cebcSVladimir Oltean */ 5426666cebcSVladimir Oltean .tpid = ETH_P_SJA1105, 5436666cebcSVladimir Oltean .tpid2 = ETH_P_SJA1105, 5448aa9ebccSVladimir Oltean }; 5458aa9ebccSVladimir Oltean struct sja1105_table *table; 5468aa9ebccSVladimir Oltean 5478aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; 5488aa9ebccSVladimir Oltean 5498aa9ebccSVladimir Oltean if (table->entry_count) { 5508aa9ebccSVladimir Oltean kfree(table->entries); 5518aa9ebccSVladimir Oltean table->entry_count = 0; 5528aa9ebccSVladimir Oltean } 5538aa9ebccSVladimir Oltean 5548aa9ebccSVladimir Oltean table->entries = kcalloc(SJA1105_MAX_GENERAL_PARAMS_COUNT, 5558aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 5568aa9ebccSVladimir Oltean if (!table->entries) 5578aa9ebccSVladimir Oltean return -ENOMEM; 5588aa9ebccSVladimir Oltean 5598aa9ebccSVladimir Oltean table->entry_count = SJA1105_MAX_GENERAL_PARAMS_COUNT; 5608aa9ebccSVladimir Oltean 5618aa9ebccSVladimir Oltean /* This table only has a single entry */ 5628aa9ebccSVladimir Oltean ((struct sja1105_general_params_entry *)table->entries)[0] = 5638aa9ebccSVladimir Oltean default_general_params; 5648aa9ebccSVladimir Oltean 5658aa9ebccSVladimir Oltean return 0; 5668aa9ebccSVladimir Oltean } 5678aa9ebccSVladimir Oltean 56879d5511cSVladimir Oltean static int sja1105_init_avb_params(struct sja1105_private *priv) 56979d5511cSVladimir Oltean { 57079d5511cSVladimir Oltean struct sja1105_avb_params_entry *avb; 57179d5511cSVladimir Oltean struct sja1105_table *table; 57279d5511cSVladimir Oltean 57379d5511cSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_AVB_PARAMS]; 57479d5511cSVladimir Oltean 57579d5511cSVladimir Oltean /* Discard previous AVB Parameters Table */ 57679d5511cSVladimir Oltean if (table->entry_count) { 57779d5511cSVladimir Oltean kfree(table->entries); 57879d5511cSVladimir Oltean table->entry_count = 0; 57979d5511cSVladimir Oltean } 58079d5511cSVladimir Oltean 58179d5511cSVladimir Oltean table->entries = kcalloc(SJA1105_MAX_AVB_PARAMS_COUNT, 58279d5511cSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 58379d5511cSVladimir Oltean if (!table->entries) 58479d5511cSVladimir Oltean return -ENOMEM; 58579d5511cSVladimir Oltean 58679d5511cSVladimir Oltean table->entry_count = SJA1105_MAX_AVB_PARAMS_COUNT; 58779d5511cSVladimir Oltean 58879d5511cSVladimir Oltean avb = table->entries; 58979d5511cSVladimir Oltean 59079d5511cSVladimir Oltean /* Configure the MAC addresses for meta frames */ 59179d5511cSVladimir Oltean avb->destmeta = SJA1105_META_DMAC; 59279d5511cSVladimir Oltean avb->srcmeta = SJA1105_META_SMAC; 593747e5eb3SVladimir Oltean /* On P/Q/R/S, configure the direction of the PTP_CLK pin as input by 594747e5eb3SVladimir Oltean * default. This is because there might be boards with a hardware 595747e5eb3SVladimir Oltean * layout where enabling the pin as output might cause an electrical 596747e5eb3SVladimir Oltean * clash. On E/T the pin is always an output, which the board designers 597747e5eb3SVladimir Oltean * probably already knew, so even if there are going to be electrical 598747e5eb3SVladimir Oltean * issues, there's nothing we can do. 599747e5eb3SVladimir Oltean */ 600747e5eb3SVladimir Oltean avb->cas_master = false; 60179d5511cSVladimir Oltean 60279d5511cSVladimir Oltean return 0; 60379d5511cSVladimir Oltean } 60479d5511cSVladimir Oltean 605a7cc081cSVladimir Oltean /* The L2 policing table is 2-stage. The table is looked up for each frame 606a7cc081cSVladimir Oltean * according to the ingress port, whether it was broadcast or not, and the 607a7cc081cSVladimir Oltean * classified traffic class (given by VLAN PCP). This portion of the lookup is 608a7cc081cSVladimir Oltean * fixed, and gives access to the SHARINDX, an indirection register pointing 609a7cc081cSVladimir Oltean * within the policing table itself, which is used to resolve the policer that 610a7cc081cSVladimir Oltean * will be used for this frame. 611a7cc081cSVladimir Oltean * 612a7cc081cSVladimir Oltean * Stage 1 Stage 2 613a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 614a7cc081cSVladimir Oltean * |Port 0 TC 0 |SHARINDX| | Policer 0: Rate, Burst, MTU | 615a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 616a7cc081cSVladimir Oltean * |Port 0 TC 1 |SHARINDX| | Policer 1: Rate, Burst, MTU | 617a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 618a7cc081cSVladimir Oltean * ... | Policer 2: Rate, Burst, MTU | 619a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 620a7cc081cSVladimir Oltean * |Port 0 TC 7 |SHARINDX| | Policer 3: Rate, Burst, MTU | 621a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 622a7cc081cSVladimir Oltean * |Port 1 TC 0 |SHARINDX| | Policer 4: Rate, Burst, MTU | 623a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 624a7cc081cSVladimir Oltean * ... | Policer 5: Rate, Burst, MTU | 625a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 626a7cc081cSVladimir Oltean * |Port 1 TC 7 |SHARINDX| | Policer 6: Rate, Burst, MTU | 627a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 628a7cc081cSVladimir Oltean * ... | Policer 7: Rate, Burst, MTU | 629a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 630a7cc081cSVladimir Oltean * |Port 4 TC 7 |SHARINDX| ... 631a7cc081cSVladimir Oltean * +------------+--------+ 632a7cc081cSVladimir Oltean * |Port 0 BCAST|SHARINDX| ... 633a7cc081cSVladimir Oltean * +------------+--------+ 634a7cc081cSVladimir Oltean * |Port 1 BCAST|SHARINDX| ... 635a7cc081cSVladimir Oltean * +------------+--------+ 636a7cc081cSVladimir Oltean * ... ... 637a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 638a7cc081cSVladimir Oltean * |Port 4 BCAST|SHARINDX| | Policer 44: Rate, Burst, MTU | 639a7cc081cSVladimir Oltean * +------------+--------+ +---------------------------------+ 640a7cc081cSVladimir Oltean * 641a7cc081cSVladimir Oltean * In this driver, we shall use policers 0-4 as statically alocated port 642a7cc081cSVladimir Oltean * (matchall) policers. So we need to make the SHARINDX for all lookups 643a7cc081cSVladimir Oltean * corresponding to this ingress port (8 VLAN PCP lookups and 1 broadcast 644a7cc081cSVladimir Oltean * lookup) equal. 645a7cc081cSVladimir Oltean * The remaining policers (40) shall be dynamically allocated for flower 646a7cc081cSVladimir Oltean * policers, where the key is either vlan_prio or dst_mac ff:ff:ff:ff:ff:ff. 647a7cc081cSVladimir Oltean */ 6488aa9ebccSVladimir Oltean #define SJA1105_RATE_MBPS(speed) (((speed) * 64000) / 1000) 6498aa9ebccSVladimir Oltean 6508aa9ebccSVladimir Oltean static int sja1105_init_l2_policing(struct sja1105_private *priv) 6518aa9ebccSVladimir Oltean { 6528aa9ebccSVladimir Oltean struct sja1105_l2_policing_entry *policing; 6538aa9ebccSVladimir Oltean struct sja1105_table *table; 654a7cc081cSVladimir Oltean int port, tc; 6558aa9ebccSVladimir Oltean 6568aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_POLICING]; 6578aa9ebccSVladimir Oltean 6588aa9ebccSVladimir Oltean /* Discard previous L2 Policing Table */ 6598aa9ebccSVladimir Oltean if (table->entry_count) { 6608aa9ebccSVladimir Oltean kfree(table->entries); 6618aa9ebccSVladimir Oltean table->entry_count = 0; 6628aa9ebccSVladimir Oltean } 6638aa9ebccSVladimir Oltean 6648aa9ebccSVladimir Oltean table->entries = kcalloc(SJA1105_MAX_L2_POLICING_COUNT, 6658aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 6668aa9ebccSVladimir Oltean if (!table->entries) 6678aa9ebccSVladimir Oltean return -ENOMEM; 6688aa9ebccSVladimir Oltean 6698aa9ebccSVladimir Oltean table->entry_count = SJA1105_MAX_L2_POLICING_COUNT; 6708aa9ebccSVladimir Oltean 6718aa9ebccSVladimir Oltean policing = table->entries; 6728aa9ebccSVladimir Oltean 673a7cc081cSVladimir Oltean /* Setup shared indices for the matchall policers */ 674a7cc081cSVladimir Oltean for (port = 0; port < SJA1105_NUM_PORTS; port++) { 675a7cc081cSVladimir Oltean int bcast = (SJA1105_NUM_PORTS * SJA1105_NUM_TC) + port; 676a7cc081cSVladimir Oltean 677a7cc081cSVladimir Oltean for (tc = 0; tc < SJA1105_NUM_TC; tc++) 678a7cc081cSVladimir Oltean policing[port * SJA1105_NUM_TC + tc].sharindx = port; 679a7cc081cSVladimir Oltean 680a7cc081cSVladimir Oltean policing[bcast].sharindx = port; 681a7cc081cSVladimir Oltean } 682a7cc081cSVladimir Oltean 683a7cc081cSVladimir Oltean /* Setup the matchall policer parameters */ 684a7cc081cSVladimir Oltean for (port = 0; port < SJA1105_NUM_PORTS; port++) { 685c279c726SVladimir Oltean int mtu = VLAN_ETH_FRAME_LEN + ETH_FCS_LEN; 686c279c726SVladimir Oltean 687a7cc081cSVladimir Oltean if (dsa_is_cpu_port(priv->ds, port)) 688c279c726SVladimir Oltean mtu += VLAN_HLEN; 6898aa9ebccSVladimir Oltean 690a7cc081cSVladimir Oltean policing[port].smax = 65535; /* Burst size in bytes */ 691a7cc081cSVladimir Oltean policing[port].rate = SJA1105_RATE_MBPS(1000); 692a7cc081cSVladimir Oltean policing[port].maxlen = mtu; 693a7cc081cSVladimir Oltean policing[port].partition = 0; 6948aa9ebccSVladimir Oltean } 695a7cc081cSVladimir Oltean 6968aa9ebccSVladimir Oltean return 0; 6978aa9ebccSVladimir Oltean } 6988aa9ebccSVladimir Oltean 6998aa9ebccSVladimir Oltean static int sja1105_static_config_load(struct sja1105_private *priv, 7008aa9ebccSVladimir Oltean struct sja1105_dt_port *ports) 7018aa9ebccSVladimir Oltean { 7028aa9ebccSVladimir Oltean int rc; 7038aa9ebccSVladimir Oltean 7048aa9ebccSVladimir Oltean sja1105_static_config_free(&priv->static_config); 7058aa9ebccSVladimir Oltean rc = sja1105_static_config_init(&priv->static_config, 7068aa9ebccSVladimir Oltean priv->info->static_ops, 7078aa9ebccSVladimir Oltean priv->info->device_id); 7088aa9ebccSVladimir Oltean if (rc) 7098aa9ebccSVladimir Oltean return rc; 7108aa9ebccSVladimir Oltean 7118aa9ebccSVladimir Oltean /* Build static configuration */ 7128aa9ebccSVladimir Oltean rc = sja1105_init_mac_settings(priv); 7138aa9ebccSVladimir Oltean if (rc < 0) 7148aa9ebccSVladimir Oltean return rc; 7158aa9ebccSVladimir Oltean rc = sja1105_init_mii_settings(priv, ports); 7168aa9ebccSVladimir Oltean if (rc < 0) 7178aa9ebccSVladimir Oltean return rc; 7188aa9ebccSVladimir Oltean rc = sja1105_init_static_fdb(priv); 7198aa9ebccSVladimir Oltean if (rc < 0) 7208aa9ebccSVladimir Oltean return rc; 7218aa9ebccSVladimir Oltean rc = sja1105_init_static_vlan(priv); 7228aa9ebccSVladimir Oltean if (rc < 0) 7238aa9ebccSVladimir Oltean return rc; 7248aa9ebccSVladimir Oltean rc = sja1105_init_l2_lookup_params(priv); 7258aa9ebccSVladimir Oltean if (rc < 0) 7268aa9ebccSVladimir Oltean return rc; 7278aa9ebccSVladimir Oltean rc = sja1105_init_l2_forwarding(priv); 7288aa9ebccSVladimir Oltean if (rc < 0) 7298aa9ebccSVladimir Oltean return rc; 7308aa9ebccSVladimir Oltean rc = sja1105_init_l2_forwarding_params(priv); 7318aa9ebccSVladimir Oltean if (rc < 0) 7328aa9ebccSVladimir Oltean return rc; 7338aa9ebccSVladimir Oltean rc = sja1105_init_l2_policing(priv); 7348aa9ebccSVladimir Oltean if (rc < 0) 7358aa9ebccSVladimir Oltean return rc; 7368aa9ebccSVladimir Oltean rc = sja1105_init_general_params(priv); 7378aa9ebccSVladimir Oltean if (rc < 0) 7388aa9ebccSVladimir Oltean return rc; 73979d5511cSVladimir Oltean rc = sja1105_init_avb_params(priv); 74079d5511cSVladimir Oltean if (rc < 0) 74179d5511cSVladimir Oltean return rc; 7428aa9ebccSVladimir Oltean 7438aa9ebccSVladimir Oltean /* Send initial configuration to hardware via SPI */ 7448aa9ebccSVladimir Oltean return sja1105_static_config_upload(priv); 7458aa9ebccSVladimir Oltean } 7468aa9ebccSVladimir Oltean 747f5b8631cSVladimir Oltean static int sja1105_parse_rgmii_delays(struct sja1105_private *priv, 748f5b8631cSVladimir Oltean const struct sja1105_dt_port *ports) 749f5b8631cSVladimir Oltean { 750f5b8631cSVladimir Oltean int i; 751f5b8631cSVladimir Oltean 752f5b8631cSVladimir Oltean for (i = 0; i < SJA1105_NUM_PORTS; i++) { 7539bca3a0aSOleksij Rempel if (ports[i].role == XMII_MAC) 754f5b8631cSVladimir Oltean continue; 755f5b8631cSVladimir Oltean 7569bca3a0aSOleksij Rempel if (ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_RXID || 7579bca3a0aSOleksij Rempel ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_ID) 758f5b8631cSVladimir Oltean priv->rgmii_rx_delay[i] = true; 759f5b8631cSVladimir Oltean 7609bca3a0aSOleksij Rempel if (ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_TXID || 7619bca3a0aSOleksij Rempel ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_ID) 762f5b8631cSVladimir Oltean priv->rgmii_tx_delay[i] = true; 763f5b8631cSVladimir Oltean 764f5b8631cSVladimir Oltean if ((priv->rgmii_rx_delay[i] || priv->rgmii_tx_delay[i]) && 765f5b8631cSVladimir Oltean !priv->info->setup_rgmii_delay) 766f5b8631cSVladimir Oltean return -EINVAL; 767f5b8631cSVladimir Oltean } 768f5b8631cSVladimir Oltean return 0; 769f5b8631cSVladimir Oltean } 770f5b8631cSVladimir Oltean 7718aa9ebccSVladimir Oltean static int sja1105_parse_ports_node(struct sja1105_private *priv, 7728aa9ebccSVladimir Oltean struct sja1105_dt_port *ports, 7738aa9ebccSVladimir Oltean struct device_node *ports_node) 7748aa9ebccSVladimir Oltean { 7758aa9ebccSVladimir Oltean struct device *dev = &priv->spidev->dev; 7768aa9ebccSVladimir Oltean struct device_node *child; 7778aa9ebccSVladimir Oltean 77827afe0d3SVladimir Oltean for_each_available_child_of_node(ports_node, child) { 7798aa9ebccSVladimir Oltean struct device_node *phy_node; 7800c65b2b9SAndrew Lunn phy_interface_t phy_mode; 7818aa9ebccSVladimir Oltean u32 index; 7820c65b2b9SAndrew Lunn int err; 7838aa9ebccSVladimir Oltean 7848aa9ebccSVladimir Oltean /* Get switch port number from DT */ 7858aa9ebccSVladimir Oltean if (of_property_read_u32(child, "reg", &index) < 0) { 7868aa9ebccSVladimir Oltean dev_err(dev, "Port number not defined in device tree " 7878aa9ebccSVladimir Oltean "(property \"reg\")\n"); 7887ba771e3SNishka Dasgupta of_node_put(child); 7898aa9ebccSVladimir Oltean return -ENODEV; 7908aa9ebccSVladimir Oltean } 7918aa9ebccSVladimir Oltean 7928aa9ebccSVladimir Oltean /* Get PHY mode from DT */ 7930c65b2b9SAndrew Lunn err = of_get_phy_mode(child, &phy_mode); 7940c65b2b9SAndrew Lunn if (err) { 7958aa9ebccSVladimir Oltean dev_err(dev, "Failed to read phy-mode or " 7968aa9ebccSVladimir Oltean "phy-interface-type property for port %d\n", 7978aa9ebccSVladimir Oltean index); 7987ba771e3SNishka Dasgupta of_node_put(child); 7998aa9ebccSVladimir Oltean return -ENODEV; 8008aa9ebccSVladimir Oltean } 8018aa9ebccSVladimir Oltean ports[index].phy_mode = phy_mode; 8028aa9ebccSVladimir Oltean 8038aa9ebccSVladimir Oltean phy_node = of_parse_phandle(child, "phy-handle", 0); 8048aa9ebccSVladimir Oltean if (!phy_node) { 8058aa9ebccSVladimir Oltean if (!of_phy_is_fixed_link(child)) { 8068aa9ebccSVladimir Oltean dev_err(dev, "phy-handle or fixed-link " 8078aa9ebccSVladimir Oltean "properties missing!\n"); 8087ba771e3SNishka Dasgupta of_node_put(child); 8098aa9ebccSVladimir Oltean return -ENODEV; 8108aa9ebccSVladimir Oltean } 8118aa9ebccSVladimir Oltean /* phy-handle is missing, but fixed-link isn't. 8128aa9ebccSVladimir Oltean * So it's a fixed link. Default to PHY role. 8138aa9ebccSVladimir Oltean */ 8148aa9ebccSVladimir Oltean ports[index].role = XMII_PHY; 8158aa9ebccSVladimir Oltean } else { 8168aa9ebccSVladimir Oltean /* phy-handle present => put port in MAC role */ 8178aa9ebccSVladimir Oltean ports[index].role = XMII_MAC; 8188aa9ebccSVladimir Oltean of_node_put(phy_node); 8198aa9ebccSVladimir Oltean } 8208aa9ebccSVladimir Oltean 8218aa9ebccSVladimir Oltean /* The MAC/PHY role can be overridden with explicit bindings */ 8228aa9ebccSVladimir Oltean if (of_property_read_bool(child, "sja1105,role-mac")) 8238aa9ebccSVladimir Oltean ports[index].role = XMII_MAC; 8248aa9ebccSVladimir Oltean else if (of_property_read_bool(child, "sja1105,role-phy")) 8258aa9ebccSVladimir Oltean ports[index].role = XMII_PHY; 8268aa9ebccSVladimir Oltean } 8278aa9ebccSVladimir Oltean 8288aa9ebccSVladimir Oltean return 0; 8298aa9ebccSVladimir Oltean } 8308aa9ebccSVladimir Oltean 8318aa9ebccSVladimir Oltean static int sja1105_parse_dt(struct sja1105_private *priv, 8328aa9ebccSVladimir Oltean struct sja1105_dt_port *ports) 8338aa9ebccSVladimir Oltean { 8348aa9ebccSVladimir Oltean struct device *dev = &priv->spidev->dev; 8358aa9ebccSVladimir Oltean struct device_node *switch_node = dev->of_node; 8368aa9ebccSVladimir Oltean struct device_node *ports_node; 8378aa9ebccSVladimir Oltean int rc; 8388aa9ebccSVladimir Oltean 8398aa9ebccSVladimir Oltean ports_node = of_get_child_by_name(switch_node, "ports"); 8408aa9ebccSVladimir Oltean if (!ports_node) { 8418aa9ebccSVladimir Oltean dev_err(dev, "Incorrect bindings: absent \"ports\" node\n"); 8428aa9ebccSVladimir Oltean return -ENODEV; 8438aa9ebccSVladimir Oltean } 8448aa9ebccSVladimir Oltean 8458aa9ebccSVladimir Oltean rc = sja1105_parse_ports_node(priv, ports, ports_node); 8468aa9ebccSVladimir Oltean of_node_put(ports_node); 8478aa9ebccSVladimir Oltean 8488aa9ebccSVladimir Oltean return rc; 8498aa9ebccSVladimir Oltean } 8508aa9ebccSVladimir Oltean 851ffe10e67SVladimir Oltean static int sja1105_sgmii_read(struct sja1105_private *priv, int pcs_reg) 852ffe10e67SVladimir Oltean { 853ffe10e67SVladimir Oltean const struct sja1105_regs *regs = priv->info->regs; 854ffe10e67SVladimir Oltean u32 val; 855ffe10e67SVladimir Oltean int rc; 856ffe10e67SVladimir Oltean 857ffe10e67SVladimir Oltean rc = sja1105_xfer_u32(priv, SPI_READ, regs->sgmii + pcs_reg, &val, 858ffe10e67SVladimir Oltean NULL); 859ffe10e67SVladimir Oltean if (rc < 0) 860ffe10e67SVladimir Oltean return rc; 861ffe10e67SVladimir Oltean 862ffe10e67SVladimir Oltean return val; 863ffe10e67SVladimir Oltean } 864ffe10e67SVladimir Oltean 865ffe10e67SVladimir Oltean static int sja1105_sgmii_write(struct sja1105_private *priv, int pcs_reg, 866ffe10e67SVladimir Oltean u16 pcs_val) 867ffe10e67SVladimir Oltean { 868ffe10e67SVladimir Oltean const struct sja1105_regs *regs = priv->info->regs; 869ffe10e67SVladimir Oltean u32 val = pcs_val; 870ffe10e67SVladimir Oltean int rc; 871ffe10e67SVladimir Oltean 872ffe10e67SVladimir Oltean rc = sja1105_xfer_u32(priv, SPI_WRITE, regs->sgmii + pcs_reg, &val, 873ffe10e67SVladimir Oltean NULL); 874ffe10e67SVladimir Oltean if (rc < 0) 875ffe10e67SVladimir Oltean return rc; 876ffe10e67SVladimir Oltean 877ffe10e67SVladimir Oltean return val; 878ffe10e67SVladimir Oltean } 879ffe10e67SVladimir Oltean 880ffe10e67SVladimir Oltean static void sja1105_sgmii_pcs_config(struct sja1105_private *priv, 881ffe10e67SVladimir Oltean bool an_enabled, bool an_master) 882ffe10e67SVladimir Oltean { 883ffe10e67SVladimir Oltean u16 ac = SJA1105_AC_AUTONEG_MODE_SGMII; 884ffe10e67SVladimir Oltean 885ffe10e67SVladimir Oltean /* DIGITAL_CONTROL_1: Enable vendor-specific MMD1, allow the PHY to 886ffe10e67SVladimir Oltean * stop the clock during LPI mode, make the MAC reconfigure 887ffe10e67SVladimir Oltean * autonomously after PCS autoneg is done, flush the internal FIFOs. 888ffe10e67SVladimir Oltean */ 889ffe10e67SVladimir Oltean sja1105_sgmii_write(priv, SJA1105_DC1, SJA1105_DC1_EN_VSMMD1 | 890ffe10e67SVladimir Oltean SJA1105_DC1_CLOCK_STOP_EN | 891ffe10e67SVladimir Oltean SJA1105_DC1_MAC_AUTO_SW | 892ffe10e67SVladimir Oltean SJA1105_DC1_INIT); 893ffe10e67SVladimir Oltean /* DIGITAL_CONTROL_2: No polarity inversion for TX and RX lanes */ 894ffe10e67SVladimir Oltean sja1105_sgmii_write(priv, SJA1105_DC2, SJA1105_DC2_TX_POL_INV_DISABLE); 895ffe10e67SVladimir Oltean /* AUTONEG_CONTROL: Use SGMII autoneg */ 896ffe10e67SVladimir Oltean if (an_master) 897ffe10e67SVladimir Oltean ac |= SJA1105_AC_PHY_MODE | SJA1105_AC_SGMII_LINK; 898ffe10e67SVladimir Oltean sja1105_sgmii_write(priv, SJA1105_AC, ac); 899ffe10e67SVladimir Oltean /* BASIC_CONTROL: enable in-band AN now, if requested. Otherwise, 900ffe10e67SVladimir Oltean * sja1105_sgmii_pcs_force_speed must be called later for the link 901ffe10e67SVladimir Oltean * to become operational. 902ffe10e67SVladimir Oltean */ 903ffe10e67SVladimir Oltean if (an_enabled) 904ffe10e67SVladimir Oltean sja1105_sgmii_write(priv, MII_BMCR, 905ffe10e67SVladimir Oltean BMCR_ANENABLE | BMCR_ANRESTART); 906ffe10e67SVladimir Oltean } 907ffe10e67SVladimir Oltean 908ffe10e67SVladimir Oltean static void sja1105_sgmii_pcs_force_speed(struct sja1105_private *priv, 909ffe10e67SVladimir Oltean int speed) 910ffe10e67SVladimir Oltean { 911ffe10e67SVladimir Oltean int pcs_speed; 912ffe10e67SVladimir Oltean 913ffe10e67SVladimir Oltean switch (speed) { 914ffe10e67SVladimir Oltean case SPEED_1000: 915ffe10e67SVladimir Oltean pcs_speed = BMCR_SPEED1000; 916ffe10e67SVladimir Oltean break; 917ffe10e67SVladimir Oltean case SPEED_100: 918ffe10e67SVladimir Oltean pcs_speed = BMCR_SPEED100; 919ffe10e67SVladimir Oltean break; 920ffe10e67SVladimir Oltean case SPEED_10: 921ffe10e67SVladimir Oltean pcs_speed = BMCR_SPEED10; 922ffe10e67SVladimir Oltean break; 923ffe10e67SVladimir Oltean default: 924ffe10e67SVladimir Oltean dev_err(priv->ds->dev, "Invalid speed %d\n", speed); 925ffe10e67SVladimir Oltean return; 926ffe10e67SVladimir Oltean } 927ffe10e67SVladimir Oltean sja1105_sgmii_write(priv, MII_BMCR, pcs_speed | BMCR_FULLDPLX); 928ffe10e67SVladimir Oltean } 929ffe10e67SVladimir Oltean 930c44d0535SVladimir Oltean /* Convert link speed from SJA1105 to ethtool encoding */ 9318aa9ebccSVladimir Oltean static int sja1105_speed[] = { 932c44d0535SVladimir Oltean [SJA1105_SPEED_AUTO] = SPEED_UNKNOWN, 933c44d0535SVladimir Oltean [SJA1105_SPEED_10MBPS] = SPEED_10, 934c44d0535SVladimir Oltean [SJA1105_SPEED_100MBPS] = SPEED_100, 935c44d0535SVladimir Oltean [SJA1105_SPEED_1000MBPS] = SPEED_1000, 9368aa9ebccSVladimir Oltean }; 9378aa9ebccSVladimir Oltean 9388400cff6SVladimir Oltean /* Set link speed in the MAC configuration for a specific port. */ 9398aa9ebccSVladimir Oltean static int sja1105_adjust_port_config(struct sja1105_private *priv, int port, 9408400cff6SVladimir Oltean int speed_mbps) 9418aa9ebccSVladimir Oltean { 9428aa9ebccSVladimir Oltean struct sja1105_xmii_params_entry *mii; 9438aa9ebccSVladimir Oltean struct sja1105_mac_config_entry *mac; 9448aa9ebccSVladimir Oltean struct device *dev = priv->ds->dev; 9458aa9ebccSVladimir Oltean sja1105_phy_interface_t phy_mode; 9468aa9ebccSVladimir Oltean sja1105_speed_t speed; 9478aa9ebccSVladimir Oltean int rc; 9488aa9ebccSVladimir Oltean 9498400cff6SVladimir Oltean /* On P/Q/R/S, one can read from the device via the MAC reconfiguration 9508400cff6SVladimir Oltean * tables. On E/T, MAC reconfig tables are not readable, only writable. 9518400cff6SVladimir Oltean * We have to *know* what the MAC looks like. For the sake of keeping 9528400cff6SVladimir Oltean * the code common, we'll use the static configuration tables as a 9538400cff6SVladimir Oltean * reasonable approximation for both E/T and P/Q/R/S. 9548400cff6SVladimir Oltean */ 9558aa9ebccSVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 9568400cff6SVladimir Oltean mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries; 9578aa9ebccSVladimir Oltean 958f4cfcfbdSVladimir Oltean switch (speed_mbps) { 959c44d0535SVladimir Oltean case SPEED_UNKNOWN: 960a979a0abSVladimir Oltean /* PHYLINK called sja1105_mac_config() to inform us about 961a979a0abSVladimir Oltean * the state->interface, but AN has not completed and the 962a979a0abSVladimir Oltean * speed is not yet valid. UM10944.pdf says that setting 963a979a0abSVladimir Oltean * SJA1105_SPEED_AUTO at runtime disables the port, so that is 964a979a0abSVladimir Oltean * ok for power consumption in case AN will never complete - 965a979a0abSVladimir Oltean * otherwise PHYLINK should come back with a new update. 966a979a0abSVladimir Oltean */ 967f4cfcfbdSVladimir Oltean speed = SJA1105_SPEED_AUTO; 968f4cfcfbdSVladimir Oltean break; 969c44d0535SVladimir Oltean case SPEED_10: 970f4cfcfbdSVladimir Oltean speed = SJA1105_SPEED_10MBPS; 971f4cfcfbdSVladimir Oltean break; 972c44d0535SVladimir Oltean case SPEED_100: 973f4cfcfbdSVladimir Oltean speed = SJA1105_SPEED_100MBPS; 974f4cfcfbdSVladimir Oltean break; 975c44d0535SVladimir Oltean case SPEED_1000: 976f4cfcfbdSVladimir Oltean speed = SJA1105_SPEED_1000MBPS; 977f4cfcfbdSVladimir Oltean break; 978f4cfcfbdSVladimir Oltean default: 9798aa9ebccSVladimir Oltean dev_err(dev, "Invalid speed %iMbps\n", speed_mbps); 9808aa9ebccSVladimir Oltean return -EINVAL; 9818aa9ebccSVladimir Oltean } 9828aa9ebccSVladimir Oltean 9838400cff6SVladimir Oltean /* Overwrite SJA1105_SPEED_AUTO from the static MAC configuration 9848400cff6SVladimir Oltean * table, since this will be used for the clocking setup, and we no 9858400cff6SVladimir Oltean * longer need to store it in the static config (already told hardware 9868400cff6SVladimir Oltean * we want auto during upload phase). 987ffe10e67SVladimir Oltean * Actually for the SGMII port, the MAC is fixed at 1 Gbps and 988ffe10e67SVladimir Oltean * we need to configure the PCS only (if even that). 9898aa9ebccSVladimir Oltean */ 990ffe10e67SVladimir Oltean if (sja1105_supports_sgmii(priv, port)) 991ffe10e67SVladimir Oltean mac[port].speed = SJA1105_SPEED_1000MBPS; 992ffe10e67SVladimir Oltean else 9938aa9ebccSVladimir Oltean mac[port].speed = speed; 9948aa9ebccSVladimir Oltean 9958aa9ebccSVladimir Oltean /* Write to the dynamic reconfiguration tables */ 9968400cff6SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, 9978400cff6SVladimir Oltean &mac[port], true); 9988aa9ebccSVladimir Oltean if (rc < 0) { 9998aa9ebccSVladimir Oltean dev_err(dev, "Failed to write MAC config: %d\n", rc); 10008aa9ebccSVladimir Oltean return rc; 10018aa9ebccSVladimir Oltean } 10028aa9ebccSVladimir Oltean 10038aa9ebccSVladimir Oltean /* Reconfigure the PLLs for the RGMII interfaces (required 125 MHz at 10048aa9ebccSVladimir Oltean * gigabit, 25 MHz at 100 Mbps and 2.5 MHz at 10 Mbps). For MII and 10058aa9ebccSVladimir Oltean * RMII no change of the clock setup is required. Actually, changing 10068aa9ebccSVladimir Oltean * the clock setup does interrupt the clock signal for a certain time 10078aa9ebccSVladimir Oltean * which causes trouble for all PHYs relying on this signal. 10088aa9ebccSVladimir Oltean */ 10098aa9ebccSVladimir Oltean phy_mode = mii->xmii_mode[port]; 10108aa9ebccSVladimir Oltean if (phy_mode != XMII_MODE_RGMII) 10118aa9ebccSVladimir Oltean return 0; 10128aa9ebccSVladimir Oltean 10138aa9ebccSVladimir Oltean return sja1105_clocking_setup_port(priv, port); 10148aa9ebccSVladimir Oltean } 10158aa9ebccSVladimir Oltean 101639710229SVladimir Oltean /* The SJA1105 MAC programming model is through the static config (the xMII 101739710229SVladimir Oltean * Mode table cannot be dynamically reconfigured), and we have to program 101839710229SVladimir Oltean * that early (earlier than PHYLINK calls us, anyway). 101939710229SVladimir Oltean * So just error out in case the connected PHY attempts to change the initial 102039710229SVladimir Oltean * system interface MII protocol from what is defined in the DT, at least for 102139710229SVladimir Oltean * now. 102239710229SVladimir Oltean */ 102339710229SVladimir Oltean static bool sja1105_phy_mode_mismatch(struct sja1105_private *priv, int port, 102439710229SVladimir Oltean phy_interface_t interface) 102539710229SVladimir Oltean { 102639710229SVladimir Oltean struct sja1105_xmii_params_entry *mii; 102739710229SVladimir Oltean sja1105_phy_interface_t phy_mode; 102839710229SVladimir Oltean 102939710229SVladimir Oltean mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries; 103039710229SVladimir Oltean phy_mode = mii->xmii_mode[port]; 103139710229SVladimir Oltean 103239710229SVladimir Oltean switch (interface) { 103339710229SVladimir Oltean case PHY_INTERFACE_MODE_MII: 103439710229SVladimir Oltean return (phy_mode != XMII_MODE_MII); 103539710229SVladimir Oltean case PHY_INTERFACE_MODE_RMII: 103639710229SVladimir Oltean return (phy_mode != XMII_MODE_RMII); 103739710229SVladimir Oltean case PHY_INTERFACE_MODE_RGMII: 103839710229SVladimir Oltean case PHY_INTERFACE_MODE_RGMII_ID: 103939710229SVladimir Oltean case PHY_INTERFACE_MODE_RGMII_RXID: 104039710229SVladimir Oltean case PHY_INTERFACE_MODE_RGMII_TXID: 104139710229SVladimir Oltean return (phy_mode != XMII_MODE_RGMII); 1042ffe10e67SVladimir Oltean case PHY_INTERFACE_MODE_SGMII: 1043ffe10e67SVladimir Oltean return (phy_mode != XMII_MODE_SGMII); 104439710229SVladimir Oltean default: 104539710229SVladimir Oltean return true; 104639710229SVladimir Oltean } 104739710229SVladimir Oltean } 104839710229SVladimir Oltean 1049af7cd036SVladimir Oltean static void sja1105_mac_config(struct dsa_switch *ds, int port, 1050ffe10e67SVladimir Oltean unsigned int mode, 1051af7cd036SVladimir Oltean const struct phylink_link_state *state) 10528aa9ebccSVladimir Oltean { 10538aa9ebccSVladimir Oltean struct sja1105_private *priv = ds->priv; 1054ffe10e67SVladimir Oltean bool is_sgmii = sja1105_supports_sgmii(priv, port); 10558aa9ebccSVladimir Oltean 1056ec8582d1SVladimir Oltean if (sja1105_phy_mode_mismatch(priv, port, state->interface)) { 1057ec8582d1SVladimir Oltean dev_err(ds->dev, "Changing PHY mode to %s not supported!\n", 1058ec8582d1SVladimir Oltean phy_modes(state->interface)); 105939710229SVladimir Oltean return; 1060ec8582d1SVladimir Oltean } 106139710229SVladimir Oltean 1062ffe10e67SVladimir Oltean if (phylink_autoneg_inband(mode) && !is_sgmii) { 10639f971573SVladimir Oltean dev_err(ds->dev, "In-band AN not supported!\n"); 10649f971573SVladimir Oltean return; 10659f971573SVladimir Oltean } 1066ffe10e67SVladimir Oltean 1067ffe10e67SVladimir Oltean if (is_sgmii) 1068ffe10e67SVladimir Oltean sja1105_sgmii_pcs_config(priv, phylink_autoneg_inband(mode), 1069ffe10e67SVladimir Oltean false); 10708400cff6SVladimir Oltean } 10718400cff6SVladimir Oltean 10728400cff6SVladimir Oltean static void sja1105_mac_link_down(struct dsa_switch *ds, int port, 10738400cff6SVladimir Oltean unsigned int mode, 10748400cff6SVladimir Oltean phy_interface_t interface) 10758400cff6SVladimir Oltean { 10768400cff6SVladimir Oltean sja1105_inhibit_tx(ds->priv, BIT(port), true); 10778400cff6SVladimir Oltean } 10788400cff6SVladimir Oltean 10798400cff6SVladimir Oltean static void sja1105_mac_link_up(struct dsa_switch *ds, int port, 10808400cff6SVladimir Oltean unsigned int mode, 10818400cff6SVladimir Oltean phy_interface_t interface, 10825b502a7bSRussell King struct phy_device *phydev, 10835b502a7bSRussell King int speed, int duplex, 10845b502a7bSRussell King bool tx_pause, bool rx_pause) 10858400cff6SVladimir Oltean { 1086ec8582d1SVladimir Oltean struct sja1105_private *priv = ds->priv; 1087ec8582d1SVladimir Oltean 1088ec8582d1SVladimir Oltean sja1105_adjust_port_config(priv, port, speed); 1089ec8582d1SVladimir Oltean 1090ffe10e67SVladimir Oltean if (sja1105_supports_sgmii(priv, port) && !phylink_autoneg_inband(mode)) 1091ffe10e67SVladimir Oltean sja1105_sgmii_pcs_force_speed(priv, speed); 1092ffe10e67SVladimir Oltean 1093ec8582d1SVladimir Oltean sja1105_inhibit_tx(priv, BIT(port), false); 10948aa9ebccSVladimir Oltean } 10958aa9ebccSVladimir Oltean 1096ad9f299aSVladimir Oltean static void sja1105_phylink_validate(struct dsa_switch *ds, int port, 1097ad9f299aSVladimir Oltean unsigned long *supported, 1098ad9f299aSVladimir Oltean struct phylink_link_state *state) 1099ad9f299aSVladimir Oltean { 1100ad9f299aSVladimir Oltean /* Construct a new mask which exhaustively contains all link features 1101ad9f299aSVladimir Oltean * supported by the MAC, and then apply that (logical AND) to what will 1102ad9f299aSVladimir Oltean * be sent to the PHY for "marketing". 1103ad9f299aSVladimir Oltean */ 1104ad9f299aSVladimir Oltean __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, }; 1105ad9f299aSVladimir Oltean struct sja1105_private *priv = ds->priv; 1106ad9f299aSVladimir Oltean struct sja1105_xmii_params_entry *mii; 1107ad9f299aSVladimir Oltean 1108ad9f299aSVladimir Oltean mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries; 1109ad9f299aSVladimir Oltean 111039710229SVladimir Oltean /* include/linux/phylink.h says: 111139710229SVladimir Oltean * When @state->interface is %PHY_INTERFACE_MODE_NA, phylink 111239710229SVladimir Oltean * expects the MAC driver to return all supported link modes. 111339710229SVladimir Oltean */ 111439710229SVladimir Oltean if (state->interface != PHY_INTERFACE_MODE_NA && 111539710229SVladimir Oltean sja1105_phy_mode_mismatch(priv, port, state->interface)) { 111639710229SVladimir Oltean bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS); 111739710229SVladimir Oltean return; 111839710229SVladimir Oltean } 111939710229SVladimir Oltean 1120ad9f299aSVladimir Oltean /* The MAC does not support pause frames, and also doesn't 1121ad9f299aSVladimir Oltean * support half-duplex traffic modes. 1122ad9f299aSVladimir Oltean */ 1123ad9f299aSVladimir Oltean phylink_set(mask, Autoneg); 1124ad9f299aSVladimir Oltean phylink_set(mask, MII); 1125ad9f299aSVladimir Oltean phylink_set(mask, 10baseT_Full); 1126ad9f299aSVladimir Oltean phylink_set(mask, 100baseT_Full); 1127ca68e138SOleksij Rempel phylink_set(mask, 100baseT1_Full); 1128ffe10e67SVladimir Oltean if (mii->xmii_mode[port] == XMII_MODE_RGMII || 1129ffe10e67SVladimir Oltean mii->xmii_mode[port] == XMII_MODE_SGMII) 1130ad9f299aSVladimir Oltean phylink_set(mask, 1000baseT_Full); 1131ad9f299aSVladimir Oltean 1132ad9f299aSVladimir Oltean bitmap_and(supported, supported, mask, __ETHTOOL_LINK_MODE_MASK_NBITS); 1133ad9f299aSVladimir Oltean bitmap_and(state->advertising, state->advertising, mask, 1134ad9f299aSVladimir Oltean __ETHTOOL_LINK_MODE_MASK_NBITS); 1135ad9f299aSVladimir Oltean } 1136ad9f299aSVladimir Oltean 1137ffe10e67SVladimir Oltean static int sja1105_mac_pcs_get_state(struct dsa_switch *ds, int port, 1138ffe10e67SVladimir Oltean struct phylink_link_state *state) 1139ffe10e67SVladimir Oltean { 1140ffe10e67SVladimir Oltean struct sja1105_private *priv = ds->priv; 1141ffe10e67SVladimir Oltean int ais; 1142ffe10e67SVladimir Oltean 1143ffe10e67SVladimir Oltean /* Read the vendor-specific AUTONEG_INTR_STATUS register */ 1144ffe10e67SVladimir Oltean ais = sja1105_sgmii_read(priv, SJA1105_AIS); 1145ffe10e67SVladimir Oltean if (ais < 0) 1146ffe10e67SVladimir Oltean return ais; 1147ffe10e67SVladimir Oltean 1148ffe10e67SVladimir Oltean switch (SJA1105_AIS_SPEED(ais)) { 1149ffe10e67SVladimir Oltean case 0: 1150ffe10e67SVladimir Oltean state->speed = SPEED_10; 1151ffe10e67SVladimir Oltean break; 1152ffe10e67SVladimir Oltean case 1: 1153ffe10e67SVladimir Oltean state->speed = SPEED_100; 1154ffe10e67SVladimir Oltean break; 1155ffe10e67SVladimir Oltean case 2: 1156ffe10e67SVladimir Oltean state->speed = SPEED_1000; 1157ffe10e67SVladimir Oltean break; 1158ffe10e67SVladimir Oltean default: 1159ffe10e67SVladimir Oltean dev_err(ds->dev, "Invalid SGMII PCS speed %lu\n", 1160ffe10e67SVladimir Oltean SJA1105_AIS_SPEED(ais)); 1161ffe10e67SVladimir Oltean } 1162ffe10e67SVladimir Oltean state->duplex = SJA1105_AIS_DUPLEX_MODE(ais); 1163ffe10e67SVladimir Oltean state->an_complete = SJA1105_AIS_COMPLETE(ais); 1164ffe10e67SVladimir Oltean state->link = SJA1105_AIS_LINK_STATUS(ais); 1165ffe10e67SVladimir Oltean 1166ffe10e67SVladimir Oltean return 0; 1167ffe10e67SVladimir Oltean } 1168ffe10e67SVladimir Oltean 116960f6053fSVladimir Oltean static int 117060f6053fSVladimir Oltean sja1105_find_static_fdb_entry(struct sja1105_private *priv, int port, 117160f6053fSVladimir Oltean const struct sja1105_l2_lookup_entry *requested) 117260f6053fSVladimir Oltean { 117360f6053fSVladimir Oltean struct sja1105_l2_lookup_entry *l2_lookup; 117460f6053fSVladimir Oltean struct sja1105_table *table; 117560f6053fSVladimir Oltean int i; 117660f6053fSVladimir Oltean 117760f6053fSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; 117860f6053fSVladimir Oltean l2_lookup = table->entries; 117960f6053fSVladimir Oltean 118060f6053fSVladimir Oltean for (i = 0; i < table->entry_count; i++) 118160f6053fSVladimir Oltean if (l2_lookup[i].macaddr == requested->macaddr && 118260f6053fSVladimir Oltean l2_lookup[i].vlanid == requested->vlanid && 118360f6053fSVladimir Oltean l2_lookup[i].destports & BIT(port)) 118460f6053fSVladimir Oltean return i; 118560f6053fSVladimir Oltean 118660f6053fSVladimir Oltean return -1; 118760f6053fSVladimir Oltean } 118860f6053fSVladimir Oltean 118960f6053fSVladimir Oltean /* We want FDB entries added statically through the bridge command to persist 119060f6053fSVladimir Oltean * across switch resets, which are a common thing during normal SJA1105 119160f6053fSVladimir Oltean * operation. So we have to back them up in the static configuration tables 119260f6053fSVladimir Oltean * and hence apply them on next static config upload... yay! 119360f6053fSVladimir Oltean */ 119460f6053fSVladimir Oltean static int 119560f6053fSVladimir Oltean sja1105_static_fdb_change(struct sja1105_private *priv, int port, 119660f6053fSVladimir Oltean const struct sja1105_l2_lookup_entry *requested, 119760f6053fSVladimir Oltean bool keep) 119860f6053fSVladimir Oltean { 119960f6053fSVladimir Oltean struct sja1105_l2_lookup_entry *l2_lookup; 120060f6053fSVladimir Oltean struct sja1105_table *table; 120160f6053fSVladimir Oltean int rc, match; 120260f6053fSVladimir Oltean 120360f6053fSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; 120460f6053fSVladimir Oltean 120560f6053fSVladimir Oltean match = sja1105_find_static_fdb_entry(priv, port, requested); 120660f6053fSVladimir Oltean if (match < 0) { 120760f6053fSVladimir Oltean /* Can't delete a missing entry. */ 120860f6053fSVladimir Oltean if (!keep) 120960f6053fSVladimir Oltean return 0; 121060f6053fSVladimir Oltean 121160f6053fSVladimir Oltean /* No match => new entry */ 121260f6053fSVladimir Oltean rc = sja1105_table_resize(table, table->entry_count + 1); 121360f6053fSVladimir Oltean if (rc) 121460f6053fSVladimir Oltean return rc; 121560f6053fSVladimir Oltean 121660f6053fSVladimir Oltean match = table->entry_count - 1; 121760f6053fSVladimir Oltean } 121860f6053fSVladimir Oltean 121960f6053fSVladimir Oltean /* Assign pointer after the resize (it may be new memory) */ 122060f6053fSVladimir Oltean l2_lookup = table->entries; 122160f6053fSVladimir Oltean 122260f6053fSVladimir Oltean /* We have a match. 122360f6053fSVladimir Oltean * If the job was to add this FDB entry, it's already done (mostly 122460f6053fSVladimir Oltean * anyway, since the port forwarding mask may have changed, case in 122560f6053fSVladimir Oltean * which we update it). 122660f6053fSVladimir Oltean * Otherwise we have to delete it. 122760f6053fSVladimir Oltean */ 122860f6053fSVladimir Oltean if (keep) { 122960f6053fSVladimir Oltean l2_lookup[match] = *requested; 123060f6053fSVladimir Oltean return 0; 123160f6053fSVladimir Oltean } 123260f6053fSVladimir Oltean 123360f6053fSVladimir Oltean /* To remove, the strategy is to overwrite the element with 123460f6053fSVladimir Oltean * the last one, and then reduce the array size by 1 123560f6053fSVladimir Oltean */ 123660f6053fSVladimir Oltean l2_lookup[match] = l2_lookup[table->entry_count - 1]; 123760f6053fSVladimir Oltean return sja1105_table_resize(table, table->entry_count - 1); 123860f6053fSVladimir Oltean } 123960f6053fSVladimir Oltean 1240291d1e72SVladimir Oltean /* First-generation switches have a 4-way set associative TCAM that 1241291d1e72SVladimir Oltean * holds the FDB entries. An FDB index spans from 0 to 1023 and is comprised of 1242291d1e72SVladimir Oltean * a "bin" (grouping of 4 entries) and a "way" (an entry within a bin). 1243291d1e72SVladimir Oltean * For the placement of a newly learnt FDB entry, the switch selects the bin 1244291d1e72SVladimir Oltean * based on a hash function, and the way within that bin incrementally. 1245291d1e72SVladimir Oltean */ 124609c1b412SVladimir Oltean static int sja1105et_fdb_index(int bin, int way) 1247291d1e72SVladimir Oltean { 1248291d1e72SVladimir Oltean return bin * SJA1105ET_FDB_BIN_SIZE + way; 1249291d1e72SVladimir Oltean } 1250291d1e72SVladimir Oltean 12519dfa6911SVladimir Oltean static int sja1105et_is_fdb_entry_in_bin(struct sja1105_private *priv, int bin, 1252291d1e72SVladimir Oltean const u8 *addr, u16 vid, 1253291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry *match, 1254291d1e72SVladimir Oltean int *last_unused) 1255291d1e72SVladimir Oltean { 1256291d1e72SVladimir Oltean int way; 1257291d1e72SVladimir Oltean 1258291d1e72SVladimir Oltean for (way = 0; way < SJA1105ET_FDB_BIN_SIZE; way++) { 1259291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 1260291d1e72SVladimir Oltean int index = sja1105et_fdb_index(bin, way); 1261291d1e72SVladimir Oltean 1262291d1e72SVladimir Oltean /* Skip unused entries, optionally marking them 1263291d1e72SVladimir Oltean * into the return value 1264291d1e72SVladimir Oltean */ 1265291d1e72SVladimir Oltean if (sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 1266291d1e72SVladimir Oltean index, &l2_lookup)) { 1267291d1e72SVladimir Oltean if (last_unused) 1268291d1e72SVladimir Oltean *last_unused = way; 1269291d1e72SVladimir Oltean continue; 1270291d1e72SVladimir Oltean } 1271291d1e72SVladimir Oltean 1272291d1e72SVladimir Oltean if (l2_lookup.macaddr == ether_addr_to_u64(addr) && 1273291d1e72SVladimir Oltean l2_lookup.vlanid == vid) { 1274291d1e72SVladimir Oltean if (match) 1275291d1e72SVladimir Oltean *match = l2_lookup; 1276291d1e72SVladimir Oltean return way; 1277291d1e72SVladimir Oltean } 1278291d1e72SVladimir Oltean } 1279291d1e72SVladimir Oltean /* Return an invalid entry index if not found */ 1280291d1e72SVladimir Oltean return -1; 1281291d1e72SVladimir Oltean } 1282291d1e72SVladimir Oltean 12839dfa6911SVladimir Oltean int sja1105et_fdb_add(struct dsa_switch *ds, int port, 1284291d1e72SVladimir Oltean const unsigned char *addr, u16 vid) 1285291d1e72SVladimir Oltean { 1286291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 1287291d1e72SVladimir Oltean struct sja1105_private *priv = ds->priv; 1288291d1e72SVladimir Oltean struct device *dev = ds->dev; 1289291d1e72SVladimir Oltean int last_unused = -1; 129060f6053fSVladimir Oltean int bin, way, rc; 1291291d1e72SVladimir Oltean 12929dfa6911SVladimir Oltean bin = sja1105et_fdb_hash(priv, addr, vid); 1293291d1e72SVladimir Oltean 12949dfa6911SVladimir Oltean way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid, 1295291d1e72SVladimir Oltean &l2_lookup, &last_unused); 1296291d1e72SVladimir Oltean if (way >= 0) { 1297291d1e72SVladimir Oltean /* We have an FDB entry. Is our port in the destination 1298291d1e72SVladimir Oltean * mask? If yes, we need to do nothing. If not, we need 1299291d1e72SVladimir Oltean * to rewrite the entry by adding this port to it. 1300291d1e72SVladimir Oltean */ 1301291d1e72SVladimir Oltean if (l2_lookup.destports & BIT(port)) 1302291d1e72SVladimir Oltean return 0; 1303291d1e72SVladimir Oltean l2_lookup.destports |= BIT(port); 1304291d1e72SVladimir Oltean } else { 1305291d1e72SVladimir Oltean int index = sja1105et_fdb_index(bin, way); 1306291d1e72SVladimir Oltean 1307291d1e72SVladimir Oltean /* We don't have an FDB entry. We construct a new one and 1308291d1e72SVladimir Oltean * try to find a place for it within the FDB table. 1309291d1e72SVladimir Oltean */ 1310291d1e72SVladimir Oltean l2_lookup.macaddr = ether_addr_to_u64(addr); 1311291d1e72SVladimir Oltean l2_lookup.destports = BIT(port); 1312291d1e72SVladimir Oltean l2_lookup.vlanid = vid; 1313291d1e72SVladimir Oltean 1314291d1e72SVladimir Oltean if (last_unused >= 0) { 1315291d1e72SVladimir Oltean way = last_unused; 1316291d1e72SVladimir Oltean } else { 1317291d1e72SVladimir Oltean /* Bin is full, need to evict somebody. 1318291d1e72SVladimir Oltean * Choose victim at random. If you get these messages 1319291d1e72SVladimir Oltean * often, you may need to consider changing the 1320291d1e72SVladimir Oltean * distribution function: 1321291d1e72SVladimir Oltean * static_config[BLK_IDX_L2_LOOKUP_PARAMS].entries->poly 1322291d1e72SVladimir Oltean */ 1323291d1e72SVladimir Oltean get_random_bytes(&way, sizeof(u8)); 1324291d1e72SVladimir Oltean way %= SJA1105ET_FDB_BIN_SIZE; 1325291d1e72SVladimir Oltean dev_warn(dev, "Warning, FDB bin %d full while adding entry for %pM. Evicting entry %u.\n", 1326291d1e72SVladimir Oltean bin, addr, way); 1327291d1e72SVladimir Oltean /* Evict entry */ 1328291d1e72SVladimir Oltean sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 1329291d1e72SVladimir Oltean index, NULL, false); 1330291d1e72SVladimir Oltean } 1331291d1e72SVladimir Oltean } 1332291d1e72SVladimir Oltean l2_lookup.index = sja1105et_fdb_index(bin, way); 1333291d1e72SVladimir Oltean 133460f6053fSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 1335291d1e72SVladimir Oltean l2_lookup.index, &l2_lookup, 1336291d1e72SVladimir Oltean true); 133760f6053fSVladimir Oltean if (rc < 0) 133860f6053fSVladimir Oltean return rc; 133960f6053fSVladimir Oltean 134060f6053fSVladimir Oltean return sja1105_static_fdb_change(priv, port, &l2_lookup, true); 1341291d1e72SVladimir Oltean } 1342291d1e72SVladimir Oltean 13439dfa6911SVladimir Oltean int sja1105et_fdb_del(struct dsa_switch *ds, int port, 1344291d1e72SVladimir Oltean const unsigned char *addr, u16 vid) 1345291d1e72SVladimir Oltean { 1346291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 1347291d1e72SVladimir Oltean struct sja1105_private *priv = ds->priv; 134860f6053fSVladimir Oltean int index, bin, way, rc; 1349291d1e72SVladimir Oltean bool keep; 1350291d1e72SVladimir Oltean 13519dfa6911SVladimir Oltean bin = sja1105et_fdb_hash(priv, addr, vid); 13529dfa6911SVladimir Oltean way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid, 1353291d1e72SVladimir Oltean &l2_lookup, NULL); 1354291d1e72SVladimir Oltean if (way < 0) 1355291d1e72SVladimir Oltean return 0; 1356291d1e72SVladimir Oltean index = sja1105et_fdb_index(bin, way); 1357291d1e72SVladimir Oltean 1358291d1e72SVladimir Oltean /* We have an FDB entry. Is our port in the destination mask? If yes, 1359291d1e72SVladimir Oltean * we need to remove it. If the resulting port mask becomes empty, we 1360291d1e72SVladimir Oltean * need to completely evict the FDB entry. 1361291d1e72SVladimir Oltean * Otherwise we just write it back. 1362291d1e72SVladimir Oltean */ 1363291d1e72SVladimir Oltean l2_lookup.destports &= ~BIT(port); 13647752e937SVladimir Oltean 1365291d1e72SVladimir Oltean if (l2_lookup.destports) 1366291d1e72SVladimir Oltean keep = true; 1367291d1e72SVladimir Oltean else 1368291d1e72SVladimir Oltean keep = false; 1369291d1e72SVladimir Oltean 137060f6053fSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 1371291d1e72SVladimir Oltean index, &l2_lookup, keep); 137260f6053fSVladimir Oltean if (rc < 0) 137360f6053fSVladimir Oltean return rc; 137460f6053fSVladimir Oltean 137560f6053fSVladimir Oltean return sja1105_static_fdb_change(priv, port, &l2_lookup, keep); 1376291d1e72SVladimir Oltean } 1377291d1e72SVladimir Oltean 13789dfa6911SVladimir Oltean int sja1105pqrs_fdb_add(struct dsa_switch *ds, int port, 13799dfa6911SVladimir Oltean const unsigned char *addr, u16 vid) 13809dfa6911SVladimir Oltean { 13811da73821SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 13821da73821SVladimir Oltean struct sja1105_private *priv = ds->priv; 13831da73821SVladimir Oltean int rc, i; 13841da73821SVladimir Oltean 13851da73821SVladimir Oltean /* Search for an existing entry in the FDB table */ 13861da73821SVladimir Oltean l2_lookup.macaddr = ether_addr_to_u64(addr); 13871da73821SVladimir Oltean l2_lookup.vlanid = vid; 13881da73821SVladimir Oltean l2_lookup.iotag = SJA1105_S_TAG; 13891da73821SVladimir Oltean l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0); 13907f14937fSVladimir Oltean if (priv->vlan_state != SJA1105_VLAN_UNAWARE) { 13911da73821SVladimir Oltean l2_lookup.mask_vlanid = VLAN_VID_MASK; 13921da73821SVladimir Oltean l2_lookup.mask_iotag = BIT(0); 13936d7c7d94SVladimir Oltean } else { 13946d7c7d94SVladimir Oltean l2_lookup.mask_vlanid = 0; 13956d7c7d94SVladimir Oltean l2_lookup.mask_iotag = 0; 13966d7c7d94SVladimir Oltean } 13971da73821SVladimir Oltean l2_lookup.destports = BIT(port); 13981da73821SVladimir Oltean 13991da73821SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 14001da73821SVladimir Oltean SJA1105_SEARCH, &l2_lookup); 14011da73821SVladimir Oltean if (rc == 0) { 14021da73821SVladimir Oltean /* Found and this port is already in the entry's 14031da73821SVladimir Oltean * port mask => job done 14041da73821SVladimir Oltean */ 14051da73821SVladimir Oltean if (l2_lookup.destports & BIT(port)) 14061da73821SVladimir Oltean return 0; 14071da73821SVladimir Oltean /* l2_lookup.index is populated by the switch in case it 14081da73821SVladimir Oltean * found something. 14091da73821SVladimir Oltean */ 14101da73821SVladimir Oltean l2_lookup.destports |= BIT(port); 14111da73821SVladimir Oltean goto skip_finding_an_index; 14121da73821SVladimir Oltean } 14131da73821SVladimir Oltean 14141da73821SVladimir Oltean /* Not found, so try to find an unused spot in the FDB. 14151da73821SVladimir Oltean * This is slightly inefficient because the strategy is knock-knock at 14161da73821SVladimir Oltean * every possible position from 0 to 1023. 14171da73821SVladimir Oltean */ 14181da73821SVladimir Oltean for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) { 14191da73821SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 14201da73821SVladimir Oltean i, NULL); 14211da73821SVladimir Oltean if (rc < 0) 14221da73821SVladimir Oltean break; 14231da73821SVladimir Oltean } 14241da73821SVladimir Oltean if (i == SJA1105_MAX_L2_LOOKUP_COUNT) { 14251da73821SVladimir Oltean dev_err(ds->dev, "FDB is full, cannot add entry.\n"); 14261da73821SVladimir Oltean return -EINVAL; 14271da73821SVladimir Oltean } 142817ae6555SVladimir Oltean l2_lookup.lockeds = true; 14291da73821SVladimir Oltean l2_lookup.index = i; 14301da73821SVladimir Oltean 14311da73821SVladimir Oltean skip_finding_an_index: 143260f6053fSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 14331da73821SVladimir Oltean l2_lookup.index, &l2_lookup, 14341da73821SVladimir Oltean true); 143560f6053fSVladimir Oltean if (rc < 0) 143660f6053fSVladimir Oltean return rc; 143760f6053fSVladimir Oltean 143860f6053fSVladimir Oltean return sja1105_static_fdb_change(priv, port, &l2_lookup, true); 14399dfa6911SVladimir Oltean } 14409dfa6911SVladimir Oltean 14419dfa6911SVladimir Oltean int sja1105pqrs_fdb_del(struct dsa_switch *ds, int port, 14429dfa6911SVladimir Oltean const unsigned char *addr, u16 vid) 14439dfa6911SVladimir Oltean { 14441da73821SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 14451da73821SVladimir Oltean struct sja1105_private *priv = ds->priv; 14461da73821SVladimir Oltean bool keep; 14471da73821SVladimir Oltean int rc; 14481da73821SVladimir Oltean 14491da73821SVladimir Oltean l2_lookup.macaddr = ether_addr_to_u64(addr); 14501da73821SVladimir Oltean l2_lookup.vlanid = vid; 14511da73821SVladimir Oltean l2_lookup.iotag = SJA1105_S_TAG; 14521da73821SVladimir Oltean l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0); 14537f14937fSVladimir Oltean if (priv->vlan_state != SJA1105_VLAN_UNAWARE) { 14541da73821SVladimir Oltean l2_lookup.mask_vlanid = VLAN_VID_MASK; 14551da73821SVladimir Oltean l2_lookup.mask_iotag = BIT(0); 14566d7c7d94SVladimir Oltean } else { 14576d7c7d94SVladimir Oltean l2_lookup.mask_vlanid = 0; 14586d7c7d94SVladimir Oltean l2_lookup.mask_iotag = 0; 14596d7c7d94SVladimir Oltean } 14601da73821SVladimir Oltean l2_lookup.destports = BIT(port); 14611da73821SVladimir Oltean 14621da73821SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 14631da73821SVladimir Oltean SJA1105_SEARCH, &l2_lookup); 14641da73821SVladimir Oltean if (rc < 0) 14651da73821SVladimir Oltean return 0; 14661da73821SVladimir Oltean 14671da73821SVladimir Oltean l2_lookup.destports &= ~BIT(port); 14681da73821SVladimir Oltean 14691da73821SVladimir Oltean /* Decide whether we remove just this port from the FDB entry, 14701da73821SVladimir Oltean * or if we remove it completely. 14711da73821SVladimir Oltean */ 14721da73821SVladimir Oltean if (l2_lookup.destports) 14731da73821SVladimir Oltean keep = true; 14741da73821SVladimir Oltean else 14751da73821SVladimir Oltean keep = false; 14761da73821SVladimir Oltean 147760f6053fSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 14781da73821SVladimir Oltean l2_lookup.index, &l2_lookup, keep); 147960f6053fSVladimir Oltean if (rc < 0) 148060f6053fSVladimir Oltean return rc; 148160f6053fSVladimir Oltean 148260f6053fSVladimir Oltean return sja1105_static_fdb_change(priv, port, &l2_lookup, keep); 14839dfa6911SVladimir Oltean } 14849dfa6911SVladimir Oltean 14859dfa6911SVladimir Oltean static int sja1105_fdb_add(struct dsa_switch *ds, int port, 14869dfa6911SVladimir Oltean const unsigned char *addr, u16 vid) 14879dfa6911SVladimir Oltean { 14889dfa6911SVladimir Oltean struct sja1105_private *priv = ds->priv; 1489b3ee526aSVladimir Oltean 14906d7c7d94SVladimir Oltean /* dsa_8021q is in effect when the bridge's vlan_filtering isn't, 14916d7c7d94SVladimir Oltean * so the switch still does some VLAN processing internally. 14926d7c7d94SVladimir Oltean * But Shared VLAN Learning (SVL) is also active, and it will take 14936d7c7d94SVladimir Oltean * care of autonomous forwarding between the unique pvid's of each 14946d7c7d94SVladimir Oltean * port. Here we just make sure that users can't add duplicate FDB 14956d7c7d94SVladimir Oltean * entries when in this mode - the actual VID doesn't matter except 14966d7c7d94SVladimir Oltean * for what gets printed in 'bridge fdb show'. In the case of zero, 14976d7c7d94SVladimir Oltean * no VID gets printed at all. 149893647594SVladimir Oltean */ 14997f14937fSVladimir Oltean if (priv->vlan_state != SJA1105_VLAN_FILTERING_FULL) 15006d7c7d94SVladimir Oltean vid = 0; 150193647594SVladimir Oltean 15026d7c7d94SVladimir Oltean return priv->info->fdb_add_cmd(ds, port, addr, vid); 15039dfa6911SVladimir Oltean } 15049dfa6911SVladimir Oltean 15059dfa6911SVladimir Oltean static int sja1105_fdb_del(struct dsa_switch *ds, int port, 15069dfa6911SVladimir Oltean const unsigned char *addr, u16 vid) 15079dfa6911SVladimir Oltean { 15089dfa6911SVladimir Oltean struct sja1105_private *priv = ds->priv; 15099dfa6911SVladimir Oltean 15107f14937fSVladimir Oltean if (priv->vlan_state != SJA1105_VLAN_FILTERING_FULL) 15116d7c7d94SVladimir Oltean vid = 0; 15126d7c7d94SVladimir Oltean 1513b3ee526aSVladimir Oltean return priv->info->fdb_del_cmd(ds, port, addr, vid); 15149dfa6911SVladimir Oltean } 15159dfa6911SVladimir Oltean 1516291d1e72SVladimir Oltean static int sja1105_fdb_dump(struct dsa_switch *ds, int port, 1517291d1e72SVladimir Oltean dsa_fdb_dump_cb_t *cb, void *data) 1518291d1e72SVladimir Oltean { 1519291d1e72SVladimir Oltean struct sja1105_private *priv = ds->priv; 1520291d1e72SVladimir Oltean struct device *dev = ds->dev; 1521291d1e72SVladimir Oltean int i; 1522291d1e72SVladimir Oltean 1523291d1e72SVladimir Oltean for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) { 1524291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 1525291d1e72SVladimir Oltean u8 macaddr[ETH_ALEN]; 1526291d1e72SVladimir Oltean int rc; 1527291d1e72SVladimir Oltean 1528291d1e72SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 1529291d1e72SVladimir Oltean i, &l2_lookup); 1530291d1e72SVladimir Oltean /* No fdb entry at i, not an issue */ 1531def84604SVladimir Oltean if (rc == -ENOENT) 1532291d1e72SVladimir Oltean continue; 1533291d1e72SVladimir Oltean if (rc) { 1534291d1e72SVladimir Oltean dev_err(dev, "Failed to dump FDB: %d\n", rc); 1535291d1e72SVladimir Oltean return rc; 1536291d1e72SVladimir Oltean } 1537291d1e72SVladimir Oltean 1538291d1e72SVladimir Oltean /* FDB dump callback is per port. This means we have to 1539291d1e72SVladimir Oltean * disregard a valid entry if it's not for this port, even if 1540291d1e72SVladimir Oltean * only to revisit it later. This is inefficient because the 1541291d1e72SVladimir Oltean * 1024-sized FDB table needs to be traversed 4 times through 1542291d1e72SVladimir Oltean * SPI during a 'bridge fdb show' command. 1543291d1e72SVladimir Oltean */ 1544291d1e72SVladimir Oltean if (!(l2_lookup.destports & BIT(port))) 1545291d1e72SVladimir Oltean continue; 1546*4d942354SVladimir Oltean 1547*4d942354SVladimir Oltean /* We need to hide the FDB entry for unknown multicast */ 1548*4d942354SVladimir Oltean if (l2_lookup.macaddr == SJA1105_UNKNOWN_MULTICAST && 1549*4d942354SVladimir Oltean l2_lookup.mask_macaddr == SJA1105_UNKNOWN_MULTICAST) 1550*4d942354SVladimir Oltean continue; 1551*4d942354SVladimir Oltean 1552291d1e72SVladimir Oltean u64_to_ether_addr(l2_lookup.macaddr, macaddr); 155393647594SVladimir Oltean 15546d7c7d94SVladimir Oltean /* We need to hide the dsa_8021q VLANs from the user. */ 15557f14937fSVladimir Oltean if (priv->vlan_state == SJA1105_VLAN_UNAWARE) 15566d7c7d94SVladimir Oltean l2_lookup.vlanid = 0; 155717ae6555SVladimir Oltean cb(macaddr, l2_lookup.vlanid, l2_lookup.lockeds, data); 1558291d1e72SVladimir Oltean } 1559291d1e72SVladimir Oltean return 0; 1560291d1e72SVladimir Oltean } 1561291d1e72SVladimir Oltean 1562a52b2da7SVladimir Oltean static int sja1105_mdb_add(struct dsa_switch *ds, int port, 1563291d1e72SVladimir Oltean const struct switchdev_obj_port_mdb *mdb) 1564291d1e72SVladimir Oltean { 1565a52b2da7SVladimir Oltean return sja1105_fdb_add(ds, port, mdb->addr, mdb->vid); 1566291d1e72SVladimir Oltean } 1567291d1e72SVladimir Oltean 1568291d1e72SVladimir Oltean static int sja1105_mdb_del(struct dsa_switch *ds, int port, 1569291d1e72SVladimir Oltean const struct switchdev_obj_port_mdb *mdb) 1570291d1e72SVladimir Oltean { 1571291d1e72SVladimir Oltean return sja1105_fdb_del(ds, port, mdb->addr, mdb->vid); 1572291d1e72SVladimir Oltean } 1573291d1e72SVladimir Oltean 15748aa9ebccSVladimir Oltean static int sja1105_bridge_member(struct dsa_switch *ds, int port, 15758aa9ebccSVladimir Oltean struct net_device *br, bool member) 15768aa9ebccSVladimir Oltean { 15778aa9ebccSVladimir Oltean struct sja1105_l2_forwarding_entry *l2_fwd; 15788aa9ebccSVladimir Oltean struct sja1105_private *priv = ds->priv; 15798aa9ebccSVladimir Oltean int i, rc; 15808aa9ebccSVladimir Oltean 15818aa9ebccSVladimir Oltean l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries; 15828aa9ebccSVladimir Oltean 15838aa9ebccSVladimir Oltean for (i = 0; i < SJA1105_NUM_PORTS; i++) { 15848aa9ebccSVladimir Oltean /* Add this port to the forwarding matrix of the 15858aa9ebccSVladimir Oltean * other ports in the same bridge, and viceversa. 15868aa9ebccSVladimir Oltean */ 15878aa9ebccSVladimir Oltean if (!dsa_is_user_port(ds, i)) 15888aa9ebccSVladimir Oltean continue; 15898aa9ebccSVladimir Oltean /* For the ports already under the bridge, only one thing needs 15908aa9ebccSVladimir Oltean * to be done, and that is to add this port to their 15918aa9ebccSVladimir Oltean * reachability domain. So we can perform the SPI write for 15928aa9ebccSVladimir Oltean * them immediately. However, for this port itself (the one 15938aa9ebccSVladimir Oltean * that is new to the bridge), we need to add all other ports 15948aa9ebccSVladimir Oltean * to its reachability domain. So we do that incrementally in 15958aa9ebccSVladimir Oltean * this loop, and perform the SPI write only at the end, once 15968aa9ebccSVladimir Oltean * the domain contains all other bridge ports. 15978aa9ebccSVladimir Oltean */ 15988aa9ebccSVladimir Oltean if (i == port) 15998aa9ebccSVladimir Oltean continue; 16008aa9ebccSVladimir Oltean if (dsa_to_port(ds, i)->bridge_dev != br) 16018aa9ebccSVladimir Oltean continue; 16028aa9ebccSVladimir Oltean sja1105_port_allow_traffic(l2_fwd, i, port, member); 16038aa9ebccSVladimir Oltean sja1105_port_allow_traffic(l2_fwd, port, i, member); 16048aa9ebccSVladimir Oltean 16058aa9ebccSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING, 16068aa9ebccSVladimir Oltean i, &l2_fwd[i], true); 16078aa9ebccSVladimir Oltean if (rc < 0) 16088aa9ebccSVladimir Oltean return rc; 16098aa9ebccSVladimir Oltean } 16108aa9ebccSVladimir Oltean 16118aa9ebccSVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING, 16128aa9ebccSVladimir Oltean port, &l2_fwd[port], true); 16138aa9ebccSVladimir Oltean } 16148aa9ebccSVladimir Oltean 1615640f763fSVladimir Oltean static void sja1105_bridge_stp_state_set(struct dsa_switch *ds, int port, 1616640f763fSVladimir Oltean u8 state) 1617640f763fSVladimir Oltean { 1618640f763fSVladimir Oltean struct sja1105_private *priv = ds->priv; 1619640f763fSVladimir Oltean struct sja1105_mac_config_entry *mac; 1620640f763fSVladimir Oltean 1621640f763fSVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 1622640f763fSVladimir Oltean 1623640f763fSVladimir Oltean switch (state) { 1624640f763fSVladimir Oltean case BR_STATE_DISABLED: 1625640f763fSVladimir Oltean case BR_STATE_BLOCKING: 1626640f763fSVladimir Oltean /* From UM10944 description of DRPDTAG (why put this there?): 1627640f763fSVladimir Oltean * "Management traffic flows to the port regardless of the state 1628640f763fSVladimir Oltean * of the INGRESS flag". So BPDUs are still be allowed to pass. 1629640f763fSVladimir Oltean * At the moment no difference between DISABLED and BLOCKING. 1630640f763fSVladimir Oltean */ 1631640f763fSVladimir Oltean mac[port].ingress = false; 1632640f763fSVladimir Oltean mac[port].egress = false; 1633640f763fSVladimir Oltean mac[port].dyn_learn = false; 1634640f763fSVladimir Oltean break; 1635640f763fSVladimir Oltean case BR_STATE_LISTENING: 1636640f763fSVladimir Oltean mac[port].ingress = true; 1637640f763fSVladimir Oltean mac[port].egress = false; 1638640f763fSVladimir Oltean mac[port].dyn_learn = false; 1639640f763fSVladimir Oltean break; 1640640f763fSVladimir Oltean case BR_STATE_LEARNING: 1641640f763fSVladimir Oltean mac[port].ingress = true; 1642640f763fSVladimir Oltean mac[port].egress = false; 1643*4d942354SVladimir Oltean mac[port].dyn_learn = !!(priv->learn_ena & BIT(port)); 1644640f763fSVladimir Oltean break; 1645640f763fSVladimir Oltean case BR_STATE_FORWARDING: 1646640f763fSVladimir Oltean mac[port].ingress = true; 1647640f763fSVladimir Oltean mac[port].egress = true; 1648*4d942354SVladimir Oltean mac[port].dyn_learn = !!(priv->learn_ena & BIT(port)); 1649640f763fSVladimir Oltean break; 1650640f763fSVladimir Oltean default: 1651640f763fSVladimir Oltean dev_err(ds->dev, "invalid STP state: %d\n", state); 1652640f763fSVladimir Oltean return; 1653640f763fSVladimir Oltean } 1654640f763fSVladimir Oltean 1655640f763fSVladimir Oltean sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, 1656640f763fSVladimir Oltean &mac[port], true); 1657640f763fSVladimir Oltean } 1658640f763fSVladimir Oltean 16598aa9ebccSVladimir Oltean static int sja1105_bridge_join(struct dsa_switch *ds, int port, 16608aa9ebccSVladimir Oltean struct net_device *br) 16618aa9ebccSVladimir Oltean { 16628aa9ebccSVladimir Oltean return sja1105_bridge_member(ds, port, br, true); 16638aa9ebccSVladimir Oltean } 16648aa9ebccSVladimir Oltean 16658aa9ebccSVladimir Oltean static void sja1105_bridge_leave(struct dsa_switch *ds, int port, 16668aa9ebccSVladimir Oltean struct net_device *br) 16678aa9ebccSVladimir Oltean { 16688aa9ebccSVladimir Oltean sja1105_bridge_member(ds, port, br, false); 16698aa9ebccSVladimir Oltean } 16708aa9ebccSVladimir Oltean 16714d752508SVladimir Oltean #define BYTES_PER_KBIT (1000LL / 8) 16724d752508SVladimir Oltean 16734d752508SVladimir Oltean static int sja1105_find_unused_cbs_shaper(struct sja1105_private *priv) 16744d752508SVladimir Oltean { 16754d752508SVladimir Oltean int i; 16764d752508SVladimir Oltean 16774d752508SVladimir Oltean for (i = 0; i < priv->info->num_cbs_shapers; i++) 16784d752508SVladimir Oltean if (!priv->cbs[i].idle_slope && !priv->cbs[i].send_slope) 16794d752508SVladimir Oltean return i; 16804d752508SVladimir Oltean 16814d752508SVladimir Oltean return -1; 16824d752508SVladimir Oltean } 16834d752508SVladimir Oltean 16844d752508SVladimir Oltean static int sja1105_delete_cbs_shaper(struct sja1105_private *priv, int port, 16854d752508SVladimir Oltean int prio) 16864d752508SVladimir Oltean { 16874d752508SVladimir Oltean int i; 16884d752508SVladimir Oltean 16894d752508SVladimir Oltean for (i = 0; i < priv->info->num_cbs_shapers; i++) { 16904d752508SVladimir Oltean struct sja1105_cbs_entry *cbs = &priv->cbs[i]; 16914d752508SVladimir Oltean 16924d752508SVladimir Oltean if (cbs->port == port && cbs->prio == prio) { 16934d752508SVladimir Oltean memset(cbs, 0, sizeof(*cbs)); 16944d752508SVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_CBS, 16954d752508SVladimir Oltean i, cbs, true); 16964d752508SVladimir Oltean } 16974d752508SVladimir Oltean } 16984d752508SVladimir Oltean 16994d752508SVladimir Oltean return 0; 17004d752508SVladimir Oltean } 17014d752508SVladimir Oltean 17024d752508SVladimir Oltean static int sja1105_setup_tc_cbs(struct dsa_switch *ds, int port, 17034d752508SVladimir Oltean struct tc_cbs_qopt_offload *offload) 17044d752508SVladimir Oltean { 17054d752508SVladimir Oltean struct sja1105_private *priv = ds->priv; 17064d752508SVladimir Oltean struct sja1105_cbs_entry *cbs; 17074d752508SVladimir Oltean int index; 17084d752508SVladimir Oltean 17094d752508SVladimir Oltean if (!offload->enable) 17104d752508SVladimir Oltean return sja1105_delete_cbs_shaper(priv, port, offload->queue); 17114d752508SVladimir Oltean 17124d752508SVladimir Oltean index = sja1105_find_unused_cbs_shaper(priv); 17134d752508SVladimir Oltean if (index < 0) 17144d752508SVladimir Oltean return -ENOSPC; 17154d752508SVladimir Oltean 17164d752508SVladimir Oltean cbs = &priv->cbs[index]; 17174d752508SVladimir Oltean cbs->port = port; 17184d752508SVladimir Oltean cbs->prio = offload->queue; 17194d752508SVladimir Oltean /* locredit and sendslope are negative by definition. In hardware, 17204d752508SVladimir Oltean * positive values must be provided, and the negative sign is implicit. 17214d752508SVladimir Oltean */ 17224d752508SVladimir Oltean cbs->credit_hi = offload->hicredit; 17234d752508SVladimir Oltean cbs->credit_lo = abs(offload->locredit); 17244d752508SVladimir Oltean /* User space is in kbits/sec, hardware in bytes/sec */ 17254d752508SVladimir Oltean cbs->idle_slope = offload->idleslope * BYTES_PER_KBIT; 17264d752508SVladimir Oltean cbs->send_slope = abs(offload->sendslope * BYTES_PER_KBIT); 17274d752508SVladimir Oltean /* Convert the negative values from 64-bit 2's complement 17284d752508SVladimir Oltean * to 32-bit 2's complement (for the case of 0x80000000 whose 17294d752508SVladimir Oltean * negative is still negative). 17304d752508SVladimir Oltean */ 17314d752508SVladimir Oltean cbs->credit_lo &= GENMASK_ULL(31, 0); 17324d752508SVladimir Oltean cbs->send_slope &= GENMASK_ULL(31, 0); 17334d752508SVladimir Oltean 17344d752508SVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_CBS, index, cbs, 17354d752508SVladimir Oltean true); 17364d752508SVladimir Oltean } 17374d752508SVladimir Oltean 17384d752508SVladimir Oltean static int sja1105_reload_cbs(struct sja1105_private *priv) 17394d752508SVladimir Oltean { 17404d752508SVladimir Oltean int rc = 0, i; 17414d752508SVladimir Oltean 17424d752508SVladimir Oltean for (i = 0; i < priv->info->num_cbs_shapers; i++) { 17434d752508SVladimir Oltean struct sja1105_cbs_entry *cbs = &priv->cbs[i]; 17444d752508SVladimir Oltean 17454d752508SVladimir Oltean if (!cbs->idle_slope && !cbs->send_slope) 17464d752508SVladimir Oltean continue; 17474d752508SVladimir Oltean 17484d752508SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_CBS, i, cbs, 17494d752508SVladimir Oltean true); 17504d752508SVladimir Oltean if (rc) 17514d752508SVladimir Oltean break; 17524d752508SVladimir Oltean } 17534d752508SVladimir Oltean 17544d752508SVladimir Oltean return rc; 17554d752508SVladimir Oltean } 17564d752508SVladimir Oltean 17572eea1fa8SVladimir Oltean static const char * const sja1105_reset_reasons[] = { 17582eea1fa8SVladimir Oltean [SJA1105_VLAN_FILTERING] = "VLAN filtering", 17592eea1fa8SVladimir Oltean [SJA1105_RX_HWTSTAMPING] = "RX timestamping", 17602eea1fa8SVladimir Oltean [SJA1105_AGEING_TIME] = "Ageing time", 17612eea1fa8SVladimir Oltean [SJA1105_SCHEDULING] = "Time-aware scheduling", 1762c279c726SVladimir Oltean [SJA1105_BEST_EFFORT_POLICING] = "Best-effort policing", 1763dfacc5a2SVladimir Oltean [SJA1105_VIRTUAL_LINKS] = "Virtual links", 17642eea1fa8SVladimir Oltean }; 17652eea1fa8SVladimir Oltean 17666666cebcSVladimir Oltean /* For situations where we need to change a setting at runtime that is only 17676666cebcSVladimir Oltean * available through the static configuration, resetting the switch in order 17686666cebcSVladimir Oltean * to upload the new static config is unavoidable. Back up the settings we 17696666cebcSVladimir Oltean * modify at runtime (currently only MAC) and restore them after uploading, 17706666cebcSVladimir Oltean * such that this operation is relatively seamless. 17716666cebcSVladimir Oltean */ 17722eea1fa8SVladimir Oltean int sja1105_static_config_reload(struct sja1105_private *priv, 17732eea1fa8SVladimir Oltean enum sja1105_reset_reason reason) 17746666cebcSVladimir Oltean { 17756cf99c13SVladimir Oltean struct ptp_system_timestamp ptp_sts_before; 17766cf99c13SVladimir Oltean struct ptp_system_timestamp ptp_sts_after; 17776666cebcSVladimir Oltean struct sja1105_mac_config_entry *mac; 17786666cebcSVladimir Oltean int speed_mbps[SJA1105_NUM_PORTS]; 17796cf99c13SVladimir Oltean struct dsa_switch *ds = priv->ds; 17806cf99c13SVladimir Oltean s64 t1, t2, t3, t4; 17816cf99c13SVladimir Oltean s64 t12, t34; 1782ffe10e67SVladimir Oltean u16 bmcr = 0; 17836666cebcSVladimir Oltean int rc, i; 17846cf99c13SVladimir Oltean s64 now; 17856666cebcSVladimir Oltean 1786af580ae2SVladimir Oltean mutex_lock(&priv->mgmt_lock); 1787af580ae2SVladimir Oltean 17886666cebcSVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 17896666cebcSVladimir Oltean 17908400cff6SVladimir Oltean /* Back up the dynamic link speed changed by sja1105_adjust_port_config 17918400cff6SVladimir Oltean * in order to temporarily restore it to SJA1105_SPEED_AUTO - which the 17928400cff6SVladimir Oltean * switch wants to see in the static config in order to allow us to 17938400cff6SVladimir Oltean * change it through the dynamic interface later. 17946666cebcSVladimir Oltean */ 17956666cebcSVladimir Oltean for (i = 0; i < SJA1105_NUM_PORTS; i++) { 17966666cebcSVladimir Oltean speed_mbps[i] = sja1105_speed[mac[i].speed]; 17976666cebcSVladimir Oltean mac[i].speed = SJA1105_SPEED_AUTO; 17986666cebcSVladimir Oltean } 17996666cebcSVladimir Oltean 1800ffe10e67SVladimir Oltean if (sja1105_supports_sgmii(priv, SJA1105_SGMII_PORT)) 1801ffe10e67SVladimir Oltean bmcr = sja1105_sgmii_read(priv, MII_BMCR); 1802ffe10e67SVladimir Oltean 18036cf99c13SVladimir Oltean /* No PTP operations can run right now */ 18046cf99c13SVladimir Oltean mutex_lock(&priv->ptp_data.lock); 18056cf99c13SVladimir Oltean 18066cf99c13SVladimir Oltean rc = __sja1105_ptp_gettimex(ds, &now, &ptp_sts_before); 18076cf99c13SVladimir Oltean if (rc < 0) 18086cf99c13SVladimir Oltean goto out_unlock_ptp; 18096cf99c13SVladimir Oltean 18106666cebcSVladimir Oltean /* Reset switch and send updated static configuration */ 18116666cebcSVladimir Oltean rc = sja1105_static_config_upload(priv); 18126666cebcSVladimir Oltean if (rc < 0) 18136cf99c13SVladimir Oltean goto out_unlock_ptp; 18146cf99c13SVladimir Oltean 18156cf99c13SVladimir Oltean rc = __sja1105_ptp_settime(ds, 0, &ptp_sts_after); 18166cf99c13SVladimir Oltean if (rc < 0) 18176cf99c13SVladimir Oltean goto out_unlock_ptp; 18186cf99c13SVladimir Oltean 18196cf99c13SVladimir Oltean t1 = timespec64_to_ns(&ptp_sts_before.pre_ts); 18206cf99c13SVladimir Oltean t2 = timespec64_to_ns(&ptp_sts_before.post_ts); 18216cf99c13SVladimir Oltean t3 = timespec64_to_ns(&ptp_sts_after.pre_ts); 18226cf99c13SVladimir Oltean t4 = timespec64_to_ns(&ptp_sts_after.post_ts); 18236cf99c13SVladimir Oltean /* Mid point, corresponds to pre-reset PTPCLKVAL */ 18246cf99c13SVladimir Oltean t12 = t1 + (t2 - t1) / 2; 18256cf99c13SVladimir Oltean /* Mid point, corresponds to post-reset PTPCLKVAL, aka 0 */ 18266cf99c13SVladimir Oltean t34 = t3 + (t4 - t3) / 2; 18276cf99c13SVladimir Oltean /* Advance PTPCLKVAL by the time it took since its readout */ 18286cf99c13SVladimir Oltean now += (t34 - t12); 18296cf99c13SVladimir Oltean 18306cf99c13SVladimir Oltean __sja1105_ptp_adjtime(ds, now); 18316cf99c13SVladimir Oltean 18326cf99c13SVladimir Oltean out_unlock_ptp: 18336cf99c13SVladimir Oltean mutex_unlock(&priv->ptp_data.lock); 18346666cebcSVladimir Oltean 18352eea1fa8SVladimir Oltean dev_info(priv->ds->dev, 18362eea1fa8SVladimir Oltean "Reset switch and programmed static config. Reason: %s\n", 18372eea1fa8SVladimir Oltean sja1105_reset_reasons[reason]); 18382eea1fa8SVladimir Oltean 18396666cebcSVladimir Oltean /* Configure the CGU (PLLs) for MII and RMII PHYs. 18406666cebcSVladimir Oltean * For these interfaces there is no dynamic configuration 18416666cebcSVladimir Oltean * needed, since PLLs have same settings at all speeds. 18426666cebcSVladimir Oltean */ 18436666cebcSVladimir Oltean rc = sja1105_clocking_setup(priv); 18446666cebcSVladimir Oltean if (rc < 0) 18456666cebcSVladimir Oltean goto out; 18466666cebcSVladimir Oltean 18476666cebcSVladimir Oltean for (i = 0; i < SJA1105_NUM_PORTS; i++) { 18488400cff6SVladimir Oltean rc = sja1105_adjust_port_config(priv, i, speed_mbps[i]); 18496666cebcSVladimir Oltean if (rc < 0) 18506666cebcSVladimir Oltean goto out; 18516666cebcSVladimir Oltean } 1852ffe10e67SVladimir Oltean 1853ffe10e67SVladimir Oltean if (sja1105_supports_sgmii(priv, SJA1105_SGMII_PORT)) { 1854ffe10e67SVladimir Oltean bool an_enabled = !!(bmcr & BMCR_ANENABLE); 1855ffe10e67SVladimir Oltean 1856ffe10e67SVladimir Oltean sja1105_sgmii_pcs_config(priv, an_enabled, false); 1857ffe10e67SVladimir Oltean 1858ffe10e67SVladimir Oltean if (!an_enabled) { 1859ffe10e67SVladimir Oltean int speed = SPEED_UNKNOWN; 1860ffe10e67SVladimir Oltean 1861ffe10e67SVladimir Oltean if (bmcr & BMCR_SPEED1000) 1862ffe10e67SVladimir Oltean speed = SPEED_1000; 1863ffe10e67SVladimir Oltean else if (bmcr & BMCR_SPEED100) 1864ffe10e67SVladimir Oltean speed = SPEED_100; 1865ffe10e67SVladimir Oltean else if (bmcr & BMCR_SPEED10) 1866ffe10e67SVladimir Oltean speed = SPEED_10; 1867ffe10e67SVladimir Oltean 1868ffe10e67SVladimir Oltean sja1105_sgmii_pcs_force_speed(priv, speed); 1869ffe10e67SVladimir Oltean } 1870ffe10e67SVladimir Oltean } 18714d752508SVladimir Oltean 18724d752508SVladimir Oltean rc = sja1105_reload_cbs(priv); 18734d752508SVladimir Oltean if (rc < 0) 18744d752508SVladimir Oltean goto out; 18756666cebcSVladimir Oltean out: 1876af580ae2SVladimir Oltean mutex_unlock(&priv->mgmt_lock); 1877af580ae2SVladimir Oltean 18786666cebcSVladimir Oltean return rc; 18796666cebcSVladimir Oltean } 18806666cebcSVladimir Oltean 18816666cebcSVladimir Oltean static int sja1105_pvid_apply(struct sja1105_private *priv, int port, u16 pvid) 18826666cebcSVladimir Oltean { 18836666cebcSVladimir Oltean struct sja1105_mac_config_entry *mac; 18846666cebcSVladimir Oltean 18856666cebcSVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 18866666cebcSVladimir Oltean 18876666cebcSVladimir Oltean mac[port].vlanid = pvid; 18886666cebcSVladimir Oltean 18896666cebcSVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, 18906666cebcSVladimir Oltean &mac[port], true); 18916666cebcSVladimir Oltean } 18926666cebcSVladimir Oltean 1893ac02a451SVladimir Oltean static int sja1105_crosschip_bridge_join(struct dsa_switch *ds, 1894ac02a451SVladimir Oltean int tree_index, int sw_index, 1895ac02a451SVladimir Oltean int other_port, struct net_device *br) 1896ac02a451SVladimir Oltean { 1897ac02a451SVladimir Oltean struct dsa_switch *other_ds = dsa_switch_find(tree_index, sw_index); 1898ac02a451SVladimir Oltean struct sja1105_private *other_priv = other_ds->priv; 1899ac02a451SVladimir Oltean struct sja1105_private *priv = ds->priv; 1900ac02a451SVladimir Oltean int port, rc; 1901ac02a451SVladimir Oltean 1902ac02a451SVladimir Oltean if (other_ds->ops != &sja1105_switch_ops) 1903ac02a451SVladimir Oltean return 0; 1904ac02a451SVladimir Oltean 1905ac02a451SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 1906ac02a451SVladimir Oltean if (!dsa_is_user_port(ds, port)) 1907ac02a451SVladimir Oltean continue; 1908ac02a451SVladimir Oltean if (dsa_to_port(ds, port)->bridge_dev != br) 1909ac02a451SVladimir Oltean continue; 1910ac02a451SVladimir Oltean 19115899ee36SVladimir Oltean rc = dsa_8021q_crosschip_bridge_join(priv->dsa_8021q_ctx, 19125899ee36SVladimir Oltean port, 19135899ee36SVladimir Oltean other_priv->dsa_8021q_ctx, 19145899ee36SVladimir Oltean other_port); 1915ac02a451SVladimir Oltean if (rc) 1916ac02a451SVladimir Oltean return rc; 1917ac02a451SVladimir Oltean 19185899ee36SVladimir Oltean rc = dsa_8021q_crosschip_bridge_join(other_priv->dsa_8021q_ctx, 19195899ee36SVladimir Oltean other_port, 19205899ee36SVladimir Oltean priv->dsa_8021q_ctx, 19215899ee36SVladimir Oltean port); 1922ac02a451SVladimir Oltean if (rc) 1923ac02a451SVladimir Oltean return rc; 1924ac02a451SVladimir Oltean } 1925ac02a451SVladimir Oltean 1926ac02a451SVladimir Oltean return 0; 1927ac02a451SVladimir Oltean } 1928ac02a451SVladimir Oltean 1929ac02a451SVladimir Oltean static void sja1105_crosschip_bridge_leave(struct dsa_switch *ds, 1930ac02a451SVladimir Oltean int tree_index, int sw_index, 1931ac02a451SVladimir Oltean int other_port, 1932ac02a451SVladimir Oltean struct net_device *br) 1933ac02a451SVladimir Oltean { 1934ac02a451SVladimir Oltean struct dsa_switch *other_ds = dsa_switch_find(tree_index, sw_index); 1935ac02a451SVladimir Oltean struct sja1105_private *other_priv = other_ds->priv; 1936ac02a451SVladimir Oltean struct sja1105_private *priv = ds->priv; 1937ac02a451SVladimir Oltean int port; 1938ac02a451SVladimir Oltean 1939ac02a451SVladimir Oltean if (other_ds->ops != &sja1105_switch_ops) 1940ac02a451SVladimir Oltean return; 1941ac02a451SVladimir Oltean 1942ac02a451SVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 1943ac02a451SVladimir Oltean if (!dsa_is_user_port(ds, port)) 1944ac02a451SVladimir Oltean continue; 1945ac02a451SVladimir Oltean if (dsa_to_port(ds, port)->bridge_dev != br) 1946ac02a451SVladimir Oltean continue; 1947ac02a451SVladimir Oltean 19485899ee36SVladimir Oltean dsa_8021q_crosschip_bridge_leave(priv->dsa_8021q_ctx, port, 19495899ee36SVladimir Oltean other_priv->dsa_8021q_ctx, 19505899ee36SVladimir Oltean other_port); 1951ac02a451SVladimir Oltean 19525899ee36SVladimir Oltean dsa_8021q_crosschip_bridge_leave(other_priv->dsa_8021q_ctx, 19535899ee36SVladimir Oltean other_port, 19545899ee36SVladimir Oltean priv->dsa_8021q_ctx, port); 1955ac02a451SVladimir Oltean } 1956ac02a451SVladimir Oltean } 1957ac02a451SVladimir Oltean 1958227d07a0SVladimir Oltean static int sja1105_setup_8021q_tagging(struct dsa_switch *ds, bool enabled) 1959227d07a0SVladimir Oltean { 196060b33aebSVladimir Oltean struct sja1105_private *priv = ds->priv; 19617e092af2SVladimir Oltean int rc; 1962227d07a0SVladimir Oltean 19635899ee36SVladimir Oltean rc = dsa_8021q_setup(priv->dsa_8021q_ctx, enabled); 19647e092af2SVladimir Oltean if (rc) 1965227d07a0SVladimir Oltean return rc; 1966ac02a451SVladimir Oltean 1967227d07a0SVladimir Oltean dev_info(ds->dev, "%s switch tagging\n", 1968227d07a0SVladimir Oltean enabled ? "Enabled" : "Disabled"); 1969227d07a0SVladimir Oltean return 0; 1970227d07a0SVladimir Oltean } 1971227d07a0SVladimir Oltean 19728aa9ebccSVladimir Oltean static enum dsa_tag_protocol 19734d776482SFlorian Fainelli sja1105_get_tag_protocol(struct dsa_switch *ds, int port, 19744d776482SFlorian Fainelli enum dsa_tag_protocol mp) 19758aa9ebccSVladimir Oltean { 1976227d07a0SVladimir Oltean return DSA_TAG_PROTO_SJA1105; 19778aa9ebccSVladimir Oltean } 19788aa9ebccSVladimir Oltean 19793f01c91aSVladimir Oltean static int sja1105_find_free_subvlan(u16 *subvlan_map, bool pvid) 19803f01c91aSVladimir Oltean { 19813f01c91aSVladimir Oltean int subvlan; 19823f01c91aSVladimir Oltean 19833f01c91aSVladimir Oltean if (pvid) 19843f01c91aSVladimir Oltean return 0; 19853f01c91aSVladimir Oltean 19863f01c91aSVladimir Oltean for (subvlan = 1; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++) 19873f01c91aSVladimir Oltean if (subvlan_map[subvlan] == VLAN_N_VID) 19883f01c91aSVladimir Oltean return subvlan; 19893f01c91aSVladimir Oltean 19903f01c91aSVladimir Oltean return -1; 19913f01c91aSVladimir Oltean } 19923f01c91aSVladimir Oltean 19933f01c91aSVladimir Oltean static int sja1105_find_subvlan(u16 *subvlan_map, u16 vid) 19943f01c91aSVladimir Oltean { 19953f01c91aSVladimir Oltean int subvlan; 19963f01c91aSVladimir Oltean 19973f01c91aSVladimir Oltean for (subvlan = 0; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++) 19983f01c91aSVladimir Oltean if (subvlan_map[subvlan] == vid) 19993f01c91aSVladimir Oltean return subvlan; 20003f01c91aSVladimir Oltean 20013f01c91aSVladimir Oltean return -1; 20023f01c91aSVladimir Oltean } 20033f01c91aSVladimir Oltean 20043f01c91aSVladimir Oltean static int sja1105_find_committed_subvlan(struct sja1105_private *priv, 20053f01c91aSVladimir Oltean int port, u16 vid) 20063f01c91aSVladimir Oltean { 20073f01c91aSVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 20083f01c91aSVladimir Oltean 20093f01c91aSVladimir Oltean return sja1105_find_subvlan(sp->subvlan_map, vid); 20103f01c91aSVladimir Oltean } 20113f01c91aSVladimir Oltean 20123f01c91aSVladimir Oltean static void sja1105_init_subvlan_map(u16 *subvlan_map) 20133f01c91aSVladimir Oltean { 20143f01c91aSVladimir Oltean int subvlan; 20153f01c91aSVladimir Oltean 20163f01c91aSVladimir Oltean for (subvlan = 0; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++) 20173f01c91aSVladimir Oltean subvlan_map[subvlan] = VLAN_N_VID; 20183f01c91aSVladimir Oltean } 20193f01c91aSVladimir Oltean 20203f01c91aSVladimir Oltean static void sja1105_commit_subvlan_map(struct sja1105_private *priv, int port, 20213f01c91aSVladimir Oltean u16 *subvlan_map) 20223f01c91aSVladimir Oltean { 20233f01c91aSVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 20243f01c91aSVladimir Oltean int subvlan; 20253f01c91aSVladimir Oltean 20263f01c91aSVladimir Oltean for (subvlan = 0; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++) 20273f01c91aSVladimir Oltean sp->subvlan_map[subvlan] = subvlan_map[subvlan]; 20283f01c91aSVladimir Oltean } 20293f01c91aSVladimir Oltean 2030ec5ae610SVladimir Oltean static int sja1105_is_vlan_configured(struct sja1105_private *priv, u16 vid) 2031ec5ae610SVladimir Oltean { 2032ec5ae610SVladimir Oltean struct sja1105_vlan_lookup_entry *vlan; 2033ec5ae610SVladimir Oltean int count, i; 2034ec5ae610SVladimir Oltean 2035ec5ae610SVladimir Oltean vlan = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entries; 2036ec5ae610SVladimir Oltean count = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entry_count; 2037ec5ae610SVladimir Oltean 2038ec5ae610SVladimir Oltean for (i = 0; i < count; i++) 2039ec5ae610SVladimir Oltean if (vlan[i].vlanid == vid) 2040ec5ae610SVladimir Oltean return i; 2041ec5ae610SVladimir Oltean 2042ec5ae610SVladimir Oltean /* Return an invalid entry index if not found */ 2043ec5ae610SVladimir Oltean return -1; 2044ec5ae610SVladimir Oltean } 2045ec5ae610SVladimir Oltean 20463f01c91aSVladimir Oltean static int 20473f01c91aSVladimir Oltean sja1105_find_retagging_entry(struct sja1105_retagging_entry *retagging, 20483f01c91aSVladimir Oltean int count, int from_port, u16 from_vid, 20493f01c91aSVladimir Oltean u16 to_vid) 2050ec5ae610SVladimir Oltean { 20513f01c91aSVladimir Oltean int i; 20523f01c91aSVladimir Oltean 20533f01c91aSVladimir Oltean for (i = 0; i < count; i++) 20543f01c91aSVladimir Oltean if (retagging[i].ing_port == BIT(from_port) && 20553f01c91aSVladimir Oltean retagging[i].vlan_ing == from_vid && 20563f01c91aSVladimir Oltean retagging[i].vlan_egr == to_vid) 20573f01c91aSVladimir Oltean return i; 20583f01c91aSVladimir Oltean 20593f01c91aSVladimir Oltean /* Return an invalid entry index if not found */ 20603f01c91aSVladimir Oltean return -1; 20613f01c91aSVladimir Oltean } 20623f01c91aSVladimir Oltean 20633f01c91aSVladimir Oltean static int sja1105_commit_vlans(struct sja1105_private *priv, 20643f01c91aSVladimir Oltean struct sja1105_vlan_lookup_entry *new_vlan, 20653f01c91aSVladimir Oltean struct sja1105_retagging_entry *new_retagging, 20663f01c91aSVladimir Oltean int num_retagging) 20673f01c91aSVladimir Oltean { 20683f01c91aSVladimir Oltean struct sja1105_retagging_entry *retagging; 2069ec5ae610SVladimir Oltean struct sja1105_vlan_lookup_entry *vlan; 2070ec5ae610SVladimir Oltean struct sja1105_table *table; 2071ec5ae610SVladimir Oltean int num_vlans = 0; 2072ec5ae610SVladimir Oltean int rc, i, k = 0; 2073ec5ae610SVladimir Oltean 2074ec5ae610SVladimir Oltean /* VLAN table */ 2075ec5ae610SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP]; 2076ec5ae610SVladimir Oltean vlan = table->entries; 2077ec5ae610SVladimir Oltean 2078ec5ae610SVladimir Oltean for (i = 0; i < VLAN_N_VID; i++) { 2079ec5ae610SVladimir Oltean int match = sja1105_is_vlan_configured(priv, i); 2080ec5ae610SVladimir Oltean 2081ec5ae610SVladimir Oltean if (new_vlan[i].vlanid != VLAN_N_VID) 2082ec5ae610SVladimir Oltean num_vlans++; 2083ec5ae610SVladimir Oltean 2084ec5ae610SVladimir Oltean if (new_vlan[i].vlanid == VLAN_N_VID && match >= 0) { 2085ec5ae610SVladimir Oltean /* Was there before, no longer is. Delete */ 2086ec5ae610SVladimir Oltean dev_dbg(priv->ds->dev, "Deleting VLAN %d\n", i); 2087ec5ae610SVladimir Oltean rc = sja1105_dynamic_config_write(priv, 2088ec5ae610SVladimir Oltean BLK_IDX_VLAN_LOOKUP, 2089ec5ae610SVladimir Oltean i, &vlan[match], false); 2090ec5ae610SVladimir Oltean if (rc < 0) 2091ec5ae610SVladimir Oltean return rc; 2092ec5ae610SVladimir Oltean } else if (new_vlan[i].vlanid != VLAN_N_VID) { 2093ec5ae610SVladimir Oltean /* Nothing changed, don't do anything */ 2094ec5ae610SVladimir Oltean if (match >= 0 && 2095ec5ae610SVladimir Oltean vlan[match].vlanid == new_vlan[i].vlanid && 2096ec5ae610SVladimir Oltean vlan[match].tag_port == new_vlan[i].tag_port && 2097ec5ae610SVladimir Oltean vlan[match].vlan_bc == new_vlan[i].vlan_bc && 2098ec5ae610SVladimir Oltean vlan[match].vmemb_port == new_vlan[i].vmemb_port) 2099ec5ae610SVladimir Oltean continue; 2100ec5ae610SVladimir Oltean /* Update entry */ 2101ec5ae610SVladimir Oltean dev_dbg(priv->ds->dev, "Updating VLAN %d\n", i); 2102ec5ae610SVladimir Oltean rc = sja1105_dynamic_config_write(priv, 2103ec5ae610SVladimir Oltean BLK_IDX_VLAN_LOOKUP, 2104ec5ae610SVladimir Oltean i, &new_vlan[i], 2105ec5ae610SVladimir Oltean true); 2106ec5ae610SVladimir Oltean if (rc < 0) 2107ec5ae610SVladimir Oltean return rc; 2108ec5ae610SVladimir Oltean } 2109ec5ae610SVladimir Oltean } 2110ec5ae610SVladimir Oltean 2111ec5ae610SVladimir Oltean if (table->entry_count) 2112ec5ae610SVladimir Oltean kfree(table->entries); 2113ec5ae610SVladimir Oltean 2114ec5ae610SVladimir Oltean table->entries = kcalloc(num_vlans, table->ops->unpacked_entry_size, 2115ec5ae610SVladimir Oltean GFP_KERNEL); 2116ec5ae610SVladimir Oltean if (!table->entries) 2117ec5ae610SVladimir Oltean return -ENOMEM; 2118ec5ae610SVladimir Oltean 2119ec5ae610SVladimir Oltean table->entry_count = num_vlans; 2120ec5ae610SVladimir Oltean vlan = table->entries; 2121ec5ae610SVladimir Oltean 2122ec5ae610SVladimir Oltean for (i = 0; i < VLAN_N_VID; i++) { 2123ec5ae610SVladimir Oltean if (new_vlan[i].vlanid == VLAN_N_VID) 2124ec5ae610SVladimir Oltean continue; 2125ec5ae610SVladimir Oltean vlan[k++] = new_vlan[i]; 2126ec5ae610SVladimir Oltean } 2127ec5ae610SVladimir Oltean 21283f01c91aSVladimir Oltean /* VLAN Retagging Table */ 21293f01c91aSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_RETAGGING]; 21303f01c91aSVladimir Oltean retagging = table->entries; 21313f01c91aSVladimir Oltean 21323f01c91aSVladimir Oltean for (i = 0; i < table->entry_count; i++) { 21333f01c91aSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_RETAGGING, 21343f01c91aSVladimir Oltean i, &retagging[i], false); 21353f01c91aSVladimir Oltean if (rc) 21363f01c91aSVladimir Oltean return rc; 21373f01c91aSVladimir Oltean } 21383f01c91aSVladimir Oltean 21393f01c91aSVladimir Oltean if (table->entry_count) 21403f01c91aSVladimir Oltean kfree(table->entries); 21413f01c91aSVladimir Oltean 21423f01c91aSVladimir Oltean table->entries = kcalloc(num_retagging, table->ops->unpacked_entry_size, 21433f01c91aSVladimir Oltean GFP_KERNEL); 21443f01c91aSVladimir Oltean if (!table->entries) 21453f01c91aSVladimir Oltean return -ENOMEM; 21463f01c91aSVladimir Oltean 21473f01c91aSVladimir Oltean table->entry_count = num_retagging; 21483f01c91aSVladimir Oltean retagging = table->entries; 21493f01c91aSVladimir Oltean 21503f01c91aSVladimir Oltean for (i = 0; i < num_retagging; i++) { 21513f01c91aSVladimir Oltean retagging[i] = new_retagging[i]; 21523f01c91aSVladimir Oltean 21533f01c91aSVladimir Oltean /* Update entry */ 21543f01c91aSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_RETAGGING, 21553f01c91aSVladimir Oltean i, &retagging[i], true); 21563f01c91aSVladimir Oltean if (rc < 0) 21573f01c91aSVladimir Oltean return rc; 21583f01c91aSVladimir Oltean } 21593f01c91aSVladimir Oltean 2160ec5ae610SVladimir Oltean return 0; 2161ec5ae610SVladimir Oltean } 2162ec5ae610SVladimir Oltean 21633f01c91aSVladimir Oltean struct sja1105_crosschip_vlan { 21643f01c91aSVladimir Oltean struct list_head list; 21653f01c91aSVladimir Oltean u16 vid; 21663f01c91aSVladimir Oltean bool untagged; 21673f01c91aSVladimir Oltean int port; 21683f01c91aSVladimir Oltean int other_port; 21695899ee36SVladimir Oltean struct dsa_8021q_context *other_ctx; 21703f01c91aSVladimir Oltean }; 21713f01c91aSVladimir Oltean 2172ec5ae610SVladimir Oltean struct sja1105_crosschip_switch { 2173ec5ae610SVladimir Oltean struct list_head list; 21745899ee36SVladimir Oltean struct dsa_8021q_context *other_ctx; 2175ec5ae610SVladimir Oltean }; 2176ec5ae610SVladimir Oltean 2177ec5ae610SVladimir Oltean static int sja1105_commit_pvid(struct sja1105_private *priv) 2178ec5ae610SVladimir Oltean { 2179ec5ae610SVladimir Oltean struct sja1105_bridge_vlan *v; 2180ec5ae610SVladimir Oltean struct list_head *vlan_list; 2181ec5ae610SVladimir Oltean int rc = 0; 2182ec5ae610SVladimir Oltean 2183ec5ae610SVladimir Oltean if (priv->vlan_state == SJA1105_VLAN_FILTERING_FULL) 2184ec5ae610SVladimir Oltean vlan_list = &priv->bridge_vlans; 2185ec5ae610SVladimir Oltean else 2186ec5ae610SVladimir Oltean vlan_list = &priv->dsa_8021q_vlans; 2187ec5ae610SVladimir Oltean 2188ec5ae610SVladimir Oltean list_for_each_entry(v, vlan_list, list) { 2189ec5ae610SVladimir Oltean if (v->pvid) { 2190ec5ae610SVladimir Oltean rc = sja1105_pvid_apply(priv, v->port, v->vid); 2191ec5ae610SVladimir Oltean if (rc) 2192ec5ae610SVladimir Oltean break; 2193ec5ae610SVladimir Oltean } 2194ec5ae610SVladimir Oltean } 2195ec5ae610SVladimir Oltean 2196ec5ae610SVladimir Oltean return rc; 2197ec5ae610SVladimir Oltean } 2198ec5ae610SVladimir Oltean 2199ec5ae610SVladimir Oltean static int 2200ec5ae610SVladimir Oltean sja1105_build_bridge_vlans(struct sja1105_private *priv, 2201ec5ae610SVladimir Oltean struct sja1105_vlan_lookup_entry *new_vlan) 2202ec5ae610SVladimir Oltean { 2203ec5ae610SVladimir Oltean struct sja1105_bridge_vlan *v; 2204ec5ae610SVladimir Oltean 2205ec5ae610SVladimir Oltean if (priv->vlan_state == SJA1105_VLAN_UNAWARE) 2206ec5ae610SVladimir Oltean return 0; 2207ec5ae610SVladimir Oltean 2208ec5ae610SVladimir Oltean list_for_each_entry(v, &priv->bridge_vlans, list) { 2209ec5ae610SVladimir Oltean int match = v->vid; 2210ec5ae610SVladimir Oltean 2211ec5ae610SVladimir Oltean new_vlan[match].vlanid = v->vid; 2212ec5ae610SVladimir Oltean new_vlan[match].vmemb_port |= BIT(v->port); 2213ec5ae610SVladimir Oltean new_vlan[match].vlan_bc |= BIT(v->port); 2214ec5ae610SVladimir Oltean if (!v->untagged) 2215ec5ae610SVladimir Oltean new_vlan[match].tag_port |= BIT(v->port); 2216ec5ae610SVladimir Oltean } 2217ec5ae610SVladimir Oltean 2218ec5ae610SVladimir Oltean return 0; 2219ec5ae610SVladimir Oltean } 2220ec5ae610SVladimir Oltean 2221ec5ae610SVladimir Oltean static int 2222ec5ae610SVladimir Oltean sja1105_build_dsa_8021q_vlans(struct sja1105_private *priv, 2223ec5ae610SVladimir Oltean struct sja1105_vlan_lookup_entry *new_vlan) 2224ec5ae610SVladimir Oltean { 2225ec5ae610SVladimir Oltean struct sja1105_bridge_vlan *v; 2226ec5ae610SVladimir Oltean 2227ec5ae610SVladimir Oltean if (priv->vlan_state == SJA1105_VLAN_FILTERING_FULL) 2228ec5ae610SVladimir Oltean return 0; 2229ec5ae610SVladimir Oltean 2230ec5ae610SVladimir Oltean list_for_each_entry(v, &priv->dsa_8021q_vlans, list) { 2231ec5ae610SVladimir Oltean int match = v->vid; 2232ec5ae610SVladimir Oltean 2233ec5ae610SVladimir Oltean new_vlan[match].vlanid = v->vid; 2234ec5ae610SVladimir Oltean new_vlan[match].vmemb_port |= BIT(v->port); 2235ec5ae610SVladimir Oltean new_vlan[match].vlan_bc |= BIT(v->port); 2236ec5ae610SVladimir Oltean if (!v->untagged) 2237ec5ae610SVladimir Oltean new_vlan[match].tag_port |= BIT(v->port); 2238ec5ae610SVladimir Oltean } 2239ec5ae610SVladimir Oltean 2240ec5ae610SVladimir Oltean return 0; 2241ec5ae610SVladimir Oltean } 2242ec5ae610SVladimir Oltean 22433f01c91aSVladimir Oltean static int sja1105_build_subvlans(struct sja1105_private *priv, 22443f01c91aSVladimir Oltean u16 subvlan_map[][DSA_8021Q_N_SUBVLAN], 22453f01c91aSVladimir Oltean struct sja1105_vlan_lookup_entry *new_vlan, 22463f01c91aSVladimir Oltean struct sja1105_retagging_entry *new_retagging, 22473f01c91aSVladimir Oltean int *num_retagging) 22483f01c91aSVladimir Oltean { 22493f01c91aSVladimir Oltean struct sja1105_bridge_vlan *v; 22503f01c91aSVladimir Oltean int k = *num_retagging; 22513f01c91aSVladimir Oltean 22523f01c91aSVladimir Oltean if (priv->vlan_state != SJA1105_VLAN_BEST_EFFORT) 22533f01c91aSVladimir Oltean return 0; 22543f01c91aSVladimir Oltean 22553f01c91aSVladimir Oltean list_for_each_entry(v, &priv->bridge_vlans, list) { 22563f01c91aSVladimir Oltean int upstream = dsa_upstream_port(priv->ds, v->port); 22573f01c91aSVladimir Oltean int match, subvlan; 22583f01c91aSVladimir Oltean u16 rx_vid; 22593f01c91aSVladimir Oltean 22603f01c91aSVladimir Oltean /* Only sub-VLANs on user ports need to be applied. 22613f01c91aSVladimir Oltean * Bridge VLANs also include VLANs added automatically 22623f01c91aSVladimir Oltean * by DSA on the CPU port. 22633f01c91aSVladimir Oltean */ 22643f01c91aSVladimir Oltean if (!dsa_is_user_port(priv->ds, v->port)) 22653f01c91aSVladimir Oltean continue; 22663f01c91aSVladimir Oltean 22673f01c91aSVladimir Oltean subvlan = sja1105_find_subvlan(subvlan_map[v->port], 22683f01c91aSVladimir Oltean v->vid); 22693f01c91aSVladimir Oltean if (subvlan < 0) { 22703f01c91aSVladimir Oltean subvlan = sja1105_find_free_subvlan(subvlan_map[v->port], 22713f01c91aSVladimir Oltean v->pvid); 22723f01c91aSVladimir Oltean if (subvlan < 0) { 22733f01c91aSVladimir Oltean dev_err(priv->ds->dev, "No more free subvlans\n"); 22743f01c91aSVladimir Oltean return -ENOSPC; 22753f01c91aSVladimir Oltean } 22763f01c91aSVladimir Oltean } 22773f01c91aSVladimir Oltean 22783f01c91aSVladimir Oltean rx_vid = dsa_8021q_rx_vid_subvlan(priv->ds, v->port, subvlan); 22793f01c91aSVladimir Oltean 22803f01c91aSVladimir Oltean /* @v->vid on @v->port needs to be retagged to @rx_vid 22813f01c91aSVladimir Oltean * on @upstream. Assume @v->vid on @v->port and on 22823f01c91aSVladimir Oltean * @upstream was already configured by the previous 22833f01c91aSVladimir Oltean * iteration over bridge_vlans. 22843f01c91aSVladimir Oltean */ 22853f01c91aSVladimir Oltean match = rx_vid; 22863f01c91aSVladimir Oltean new_vlan[match].vlanid = rx_vid; 22873f01c91aSVladimir Oltean new_vlan[match].vmemb_port |= BIT(v->port); 22883f01c91aSVladimir Oltean new_vlan[match].vmemb_port |= BIT(upstream); 22893f01c91aSVladimir Oltean new_vlan[match].vlan_bc |= BIT(v->port); 22903f01c91aSVladimir Oltean new_vlan[match].vlan_bc |= BIT(upstream); 22913f01c91aSVladimir Oltean /* The "untagged" flag is set the same as for the 22923f01c91aSVladimir Oltean * original VLAN 22933f01c91aSVladimir Oltean */ 22943f01c91aSVladimir Oltean if (!v->untagged) 22953f01c91aSVladimir Oltean new_vlan[match].tag_port |= BIT(v->port); 22963f01c91aSVladimir Oltean /* But it's always tagged towards the CPU */ 22973f01c91aSVladimir Oltean new_vlan[match].tag_port |= BIT(upstream); 22983f01c91aSVladimir Oltean 22993f01c91aSVladimir Oltean /* The Retagging Table generates packet *clones* with 23003f01c91aSVladimir Oltean * the new VLAN. This is a very odd hardware quirk 23013f01c91aSVladimir Oltean * which we need to suppress by dropping the original 23023f01c91aSVladimir Oltean * packet. 23033f01c91aSVladimir Oltean * Deny egress of the original VLAN towards the CPU 23043f01c91aSVladimir Oltean * port. This will force the switch to drop it, and 23053f01c91aSVladimir Oltean * we'll see only the retagged packets. 23063f01c91aSVladimir Oltean */ 23073f01c91aSVladimir Oltean match = v->vid; 23083f01c91aSVladimir Oltean new_vlan[match].vlan_bc &= ~BIT(upstream); 23093f01c91aSVladimir Oltean 23103f01c91aSVladimir Oltean /* And the retagging itself */ 23113f01c91aSVladimir Oltean new_retagging[k].vlan_ing = v->vid; 23123f01c91aSVladimir Oltean new_retagging[k].vlan_egr = rx_vid; 23133f01c91aSVladimir Oltean new_retagging[k].ing_port = BIT(v->port); 23143f01c91aSVladimir Oltean new_retagging[k].egr_port = BIT(upstream); 23153f01c91aSVladimir Oltean if (k++ == SJA1105_MAX_RETAGGING_COUNT) { 23163f01c91aSVladimir Oltean dev_err(priv->ds->dev, "No more retagging rules\n"); 23173f01c91aSVladimir Oltean return -ENOSPC; 23183f01c91aSVladimir Oltean } 23193f01c91aSVladimir Oltean 23203f01c91aSVladimir Oltean subvlan_map[v->port][subvlan] = v->vid; 23213f01c91aSVladimir Oltean } 23223f01c91aSVladimir Oltean 23233f01c91aSVladimir Oltean *num_retagging = k; 23243f01c91aSVladimir Oltean 23253f01c91aSVladimir Oltean return 0; 23263f01c91aSVladimir Oltean } 23273f01c91aSVladimir Oltean 23283f01c91aSVladimir Oltean /* Sadly, in crosschip scenarios where the CPU port is also the link to another 23293f01c91aSVladimir Oltean * switch, we should retag backwards (the dsa_8021q vid to the original vid) on 23303f01c91aSVladimir Oltean * the CPU port of neighbour switches. 23313f01c91aSVladimir Oltean */ 23323f01c91aSVladimir Oltean static int 23333f01c91aSVladimir Oltean sja1105_build_crosschip_subvlans(struct sja1105_private *priv, 23343f01c91aSVladimir Oltean struct sja1105_vlan_lookup_entry *new_vlan, 23353f01c91aSVladimir Oltean struct sja1105_retagging_entry *new_retagging, 23363f01c91aSVladimir Oltean int *num_retagging) 23373f01c91aSVladimir Oltean { 23383f01c91aSVladimir Oltean struct sja1105_crosschip_vlan *tmp, *pos; 23393f01c91aSVladimir Oltean struct dsa_8021q_crosschip_link *c; 23403f01c91aSVladimir Oltean struct sja1105_bridge_vlan *v, *w; 23413f01c91aSVladimir Oltean struct list_head crosschip_vlans; 23423f01c91aSVladimir Oltean int k = *num_retagging; 23433f01c91aSVladimir Oltean int rc = 0; 23443f01c91aSVladimir Oltean 23453f01c91aSVladimir Oltean if (priv->vlan_state != SJA1105_VLAN_BEST_EFFORT) 23463f01c91aSVladimir Oltean return 0; 23473f01c91aSVladimir Oltean 23483f01c91aSVladimir Oltean INIT_LIST_HEAD(&crosschip_vlans); 23493f01c91aSVladimir Oltean 23505899ee36SVladimir Oltean list_for_each_entry(c, &priv->dsa_8021q_ctx->crosschip_links, list) { 23515899ee36SVladimir Oltean struct sja1105_private *other_priv = c->other_ctx->ds->priv; 23523f01c91aSVladimir Oltean 23533f01c91aSVladimir Oltean if (other_priv->vlan_state == SJA1105_VLAN_FILTERING_FULL) 23543f01c91aSVladimir Oltean continue; 23553f01c91aSVladimir Oltean 23563f01c91aSVladimir Oltean /* Crosschip links are also added to the CPU ports. 23573f01c91aSVladimir Oltean * Ignore those. 23583f01c91aSVladimir Oltean */ 23593f01c91aSVladimir Oltean if (!dsa_is_user_port(priv->ds, c->port)) 23603f01c91aSVladimir Oltean continue; 23615899ee36SVladimir Oltean if (!dsa_is_user_port(c->other_ctx->ds, c->other_port)) 23623f01c91aSVladimir Oltean continue; 23633f01c91aSVladimir Oltean 23643f01c91aSVladimir Oltean /* Search for VLANs on the remote port */ 23653f01c91aSVladimir Oltean list_for_each_entry(v, &other_priv->bridge_vlans, list) { 23663f01c91aSVladimir Oltean bool already_added = false; 23673f01c91aSVladimir Oltean bool we_have_it = false; 23683f01c91aSVladimir Oltean 23693f01c91aSVladimir Oltean if (v->port != c->other_port) 23703f01c91aSVladimir Oltean continue; 23713f01c91aSVladimir Oltean 23723f01c91aSVladimir Oltean /* If @v is a pvid on @other_ds, it does not need 23733f01c91aSVladimir Oltean * re-retagging, because its SVL field is 0 and we 23743f01c91aSVladimir Oltean * already allow that, via the dsa_8021q crosschip 23753f01c91aSVladimir Oltean * links. 23763f01c91aSVladimir Oltean */ 23773f01c91aSVladimir Oltean if (v->pvid) 23783f01c91aSVladimir Oltean continue; 23793f01c91aSVladimir Oltean 23803f01c91aSVladimir Oltean /* Search for the VLAN on our local port */ 23813f01c91aSVladimir Oltean list_for_each_entry(w, &priv->bridge_vlans, list) { 23823f01c91aSVladimir Oltean if (w->port == c->port && w->vid == v->vid) { 23833f01c91aSVladimir Oltean we_have_it = true; 23843f01c91aSVladimir Oltean break; 23853f01c91aSVladimir Oltean } 23863f01c91aSVladimir Oltean } 23873f01c91aSVladimir Oltean 23883f01c91aSVladimir Oltean if (!we_have_it) 23893f01c91aSVladimir Oltean continue; 23903f01c91aSVladimir Oltean 23913f01c91aSVladimir Oltean list_for_each_entry(tmp, &crosschip_vlans, list) { 23923f01c91aSVladimir Oltean if (tmp->vid == v->vid && 23933f01c91aSVladimir Oltean tmp->untagged == v->untagged && 23943f01c91aSVladimir Oltean tmp->port == c->port && 23953f01c91aSVladimir Oltean tmp->other_port == v->port && 23965899ee36SVladimir Oltean tmp->other_ctx == c->other_ctx) { 23973f01c91aSVladimir Oltean already_added = true; 23983f01c91aSVladimir Oltean break; 23993f01c91aSVladimir Oltean } 24003f01c91aSVladimir Oltean } 24013f01c91aSVladimir Oltean 24023f01c91aSVladimir Oltean if (already_added) 24033f01c91aSVladimir Oltean continue; 24043f01c91aSVladimir Oltean 24053f01c91aSVladimir Oltean tmp = kzalloc(sizeof(*tmp), GFP_KERNEL); 24063f01c91aSVladimir Oltean if (!tmp) { 24073f01c91aSVladimir Oltean dev_err(priv->ds->dev, "Failed to allocate memory\n"); 24083f01c91aSVladimir Oltean rc = -ENOMEM; 24093f01c91aSVladimir Oltean goto out; 24103f01c91aSVladimir Oltean } 24113f01c91aSVladimir Oltean tmp->vid = v->vid; 24123f01c91aSVladimir Oltean tmp->port = c->port; 24133f01c91aSVladimir Oltean tmp->other_port = v->port; 24145899ee36SVladimir Oltean tmp->other_ctx = c->other_ctx; 24153f01c91aSVladimir Oltean tmp->untagged = v->untagged; 24163f01c91aSVladimir Oltean list_add(&tmp->list, &crosschip_vlans); 24173f01c91aSVladimir Oltean } 24183f01c91aSVladimir Oltean } 24193f01c91aSVladimir Oltean 24203f01c91aSVladimir Oltean list_for_each_entry(tmp, &crosschip_vlans, list) { 24215899ee36SVladimir Oltean struct sja1105_private *other_priv = tmp->other_ctx->ds->priv; 24223f01c91aSVladimir Oltean int upstream = dsa_upstream_port(priv->ds, tmp->port); 24233f01c91aSVladimir Oltean int match, subvlan; 24243f01c91aSVladimir Oltean u16 rx_vid; 24253f01c91aSVladimir Oltean 24263f01c91aSVladimir Oltean subvlan = sja1105_find_committed_subvlan(other_priv, 24273f01c91aSVladimir Oltean tmp->other_port, 24283f01c91aSVladimir Oltean tmp->vid); 24293f01c91aSVladimir Oltean /* If this happens, it's a bug. The neighbour switch does not 24303f01c91aSVladimir Oltean * have a subvlan for tmp->vid on tmp->other_port, but it 24313f01c91aSVladimir Oltean * should, since we already checked for its vlan_state. 24323f01c91aSVladimir Oltean */ 24333f01c91aSVladimir Oltean if (WARN_ON(subvlan < 0)) { 24343f01c91aSVladimir Oltean rc = -EINVAL; 24353f01c91aSVladimir Oltean goto out; 24363f01c91aSVladimir Oltean } 24373f01c91aSVladimir Oltean 24385899ee36SVladimir Oltean rx_vid = dsa_8021q_rx_vid_subvlan(tmp->other_ctx->ds, 24393f01c91aSVladimir Oltean tmp->other_port, 24403f01c91aSVladimir Oltean subvlan); 24413f01c91aSVladimir Oltean 24423f01c91aSVladimir Oltean /* The @rx_vid retagged from @tmp->vid on 24433f01c91aSVladimir Oltean * {@tmp->other_ds, @tmp->other_port} needs to be 24443f01c91aSVladimir Oltean * re-retagged to @tmp->vid on the way back to us. 24453f01c91aSVladimir Oltean * 24463f01c91aSVladimir Oltean * Assume the original @tmp->vid is already configured 24473f01c91aSVladimir Oltean * on this local switch, otherwise we wouldn't be 24483f01c91aSVladimir Oltean * retagging its subvlan on the other switch in the 24493f01c91aSVladimir Oltean * first place. We just need to add a reverse retagging 24503f01c91aSVladimir Oltean * rule for @rx_vid and install @rx_vid on our ports. 24513f01c91aSVladimir Oltean */ 24523f01c91aSVladimir Oltean match = rx_vid; 24533f01c91aSVladimir Oltean new_vlan[match].vlanid = rx_vid; 24543f01c91aSVladimir Oltean new_vlan[match].vmemb_port |= BIT(tmp->port); 24553f01c91aSVladimir Oltean new_vlan[match].vmemb_port |= BIT(upstream); 24563f01c91aSVladimir Oltean /* The "untagged" flag is set the same as for the 24573f01c91aSVladimir Oltean * original VLAN. And towards the CPU, it doesn't 24583f01c91aSVladimir Oltean * really matter, because @rx_vid will only receive 24593f01c91aSVladimir Oltean * traffic on that port. For consistency with other dsa_8021q 24603f01c91aSVladimir Oltean * VLANs, we'll keep the CPU port tagged. 24613f01c91aSVladimir Oltean */ 24623f01c91aSVladimir Oltean if (!tmp->untagged) 24633f01c91aSVladimir Oltean new_vlan[match].tag_port |= BIT(tmp->port); 24643f01c91aSVladimir Oltean new_vlan[match].tag_port |= BIT(upstream); 24653f01c91aSVladimir Oltean /* Deny egress of @rx_vid towards our front-panel port. 24663f01c91aSVladimir Oltean * This will force the switch to drop it, and we'll see 24673f01c91aSVladimir Oltean * only the re-retagged packets (having the original, 24683f01c91aSVladimir Oltean * pre-initial-retagging, VLAN @tmp->vid). 24693f01c91aSVladimir Oltean */ 24703f01c91aSVladimir Oltean new_vlan[match].vlan_bc &= ~BIT(tmp->port); 24713f01c91aSVladimir Oltean 24723f01c91aSVladimir Oltean /* On reverse retagging, the same ingress VLAN goes to multiple 24733f01c91aSVladimir Oltean * ports. So we have an opportunity to create composite rules 24743f01c91aSVladimir Oltean * to not waste the limited space in the retagging table. 24753f01c91aSVladimir Oltean */ 24763f01c91aSVladimir Oltean k = sja1105_find_retagging_entry(new_retagging, *num_retagging, 24773f01c91aSVladimir Oltean upstream, rx_vid, tmp->vid); 24783f01c91aSVladimir Oltean if (k < 0) { 24793f01c91aSVladimir Oltean if (*num_retagging == SJA1105_MAX_RETAGGING_COUNT) { 24803f01c91aSVladimir Oltean dev_err(priv->ds->dev, "No more retagging rules\n"); 24813f01c91aSVladimir Oltean rc = -ENOSPC; 24823f01c91aSVladimir Oltean goto out; 24833f01c91aSVladimir Oltean } 24843f01c91aSVladimir Oltean k = (*num_retagging)++; 24853f01c91aSVladimir Oltean } 24863f01c91aSVladimir Oltean /* And the retagging itself */ 24873f01c91aSVladimir Oltean new_retagging[k].vlan_ing = rx_vid; 24883f01c91aSVladimir Oltean new_retagging[k].vlan_egr = tmp->vid; 24893f01c91aSVladimir Oltean new_retagging[k].ing_port = BIT(upstream); 24903f01c91aSVladimir Oltean new_retagging[k].egr_port |= BIT(tmp->port); 24913f01c91aSVladimir Oltean } 24923f01c91aSVladimir Oltean 24933f01c91aSVladimir Oltean out: 24943f01c91aSVladimir Oltean list_for_each_entry_safe(tmp, pos, &crosschip_vlans, list) { 24953f01c91aSVladimir Oltean list_del(&tmp->list); 24963f01c91aSVladimir Oltean kfree(tmp); 24973f01c91aSVladimir Oltean } 24983f01c91aSVladimir Oltean 24993f01c91aSVladimir Oltean return rc; 25003f01c91aSVladimir Oltean } 25013f01c91aSVladimir Oltean 2502ec5ae610SVladimir Oltean static int sja1105_build_vlan_table(struct sja1105_private *priv, bool notify); 2503ec5ae610SVladimir Oltean 2504ec5ae610SVladimir Oltean static int sja1105_notify_crosschip_switches(struct sja1105_private *priv) 2505ec5ae610SVladimir Oltean { 2506ec5ae610SVladimir Oltean struct sja1105_crosschip_switch *s, *pos; 2507ec5ae610SVladimir Oltean struct list_head crosschip_switches; 2508ec5ae610SVladimir Oltean struct dsa_8021q_crosschip_link *c; 2509ec5ae610SVladimir Oltean int rc = 0; 2510ec5ae610SVladimir Oltean 2511ec5ae610SVladimir Oltean INIT_LIST_HEAD(&crosschip_switches); 2512ec5ae610SVladimir Oltean 25135899ee36SVladimir Oltean list_for_each_entry(c, &priv->dsa_8021q_ctx->crosschip_links, list) { 2514ec5ae610SVladimir Oltean bool already_added = false; 2515ec5ae610SVladimir Oltean 2516ec5ae610SVladimir Oltean list_for_each_entry(s, &crosschip_switches, list) { 25175899ee36SVladimir Oltean if (s->other_ctx == c->other_ctx) { 2518ec5ae610SVladimir Oltean already_added = true; 2519ec5ae610SVladimir Oltean break; 2520ec5ae610SVladimir Oltean } 2521ec5ae610SVladimir Oltean } 2522ec5ae610SVladimir Oltean 2523ec5ae610SVladimir Oltean if (already_added) 2524ec5ae610SVladimir Oltean continue; 2525ec5ae610SVladimir Oltean 2526ec5ae610SVladimir Oltean s = kzalloc(sizeof(*s), GFP_KERNEL); 2527ec5ae610SVladimir Oltean if (!s) { 2528ec5ae610SVladimir Oltean dev_err(priv->ds->dev, "Failed to allocate memory\n"); 2529ec5ae610SVladimir Oltean rc = -ENOMEM; 2530ec5ae610SVladimir Oltean goto out; 2531ec5ae610SVladimir Oltean } 25325899ee36SVladimir Oltean s->other_ctx = c->other_ctx; 2533ec5ae610SVladimir Oltean list_add(&s->list, &crosschip_switches); 2534ec5ae610SVladimir Oltean } 2535ec5ae610SVladimir Oltean 2536ec5ae610SVladimir Oltean list_for_each_entry(s, &crosschip_switches, list) { 25375899ee36SVladimir Oltean struct sja1105_private *other_priv = s->other_ctx->ds->priv; 2538ec5ae610SVladimir Oltean 2539ec5ae610SVladimir Oltean rc = sja1105_build_vlan_table(other_priv, false); 2540ec5ae610SVladimir Oltean if (rc) 2541ec5ae610SVladimir Oltean goto out; 2542ec5ae610SVladimir Oltean } 2543ec5ae610SVladimir Oltean 2544ec5ae610SVladimir Oltean out: 2545ec5ae610SVladimir Oltean list_for_each_entry_safe(s, pos, &crosschip_switches, list) { 2546ec5ae610SVladimir Oltean list_del(&s->list); 2547ec5ae610SVladimir Oltean kfree(s); 2548ec5ae610SVladimir Oltean } 2549ec5ae610SVladimir Oltean 2550ec5ae610SVladimir Oltean return rc; 2551ec5ae610SVladimir Oltean } 2552ec5ae610SVladimir Oltean 2553ec5ae610SVladimir Oltean static int sja1105_build_vlan_table(struct sja1105_private *priv, bool notify) 2554ec5ae610SVladimir Oltean { 25553f01c91aSVladimir Oltean u16 subvlan_map[SJA1105_NUM_PORTS][DSA_8021Q_N_SUBVLAN]; 25563f01c91aSVladimir Oltean struct sja1105_retagging_entry *new_retagging; 2557ec5ae610SVladimir Oltean struct sja1105_vlan_lookup_entry *new_vlan; 2558ec5ae610SVladimir Oltean struct sja1105_table *table; 25593f01c91aSVladimir Oltean int i, num_retagging = 0; 2560ec5ae610SVladimir Oltean int rc; 2561ec5ae610SVladimir Oltean 2562ec5ae610SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP]; 2563ec5ae610SVladimir Oltean new_vlan = kcalloc(VLAN_N_VID, 2564ec5ae610SVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 2565ec5ae610SVladimir Oltean if (!new_vlan) 2566ec5ae610SVladimir Oltean return -ENOMEM; 2567ec5ae610SVladimir Oltean 25683f01c91aSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP]; 25693f01c91aSVladimir Oltean new_retagging = kcalloc(SJA1105_MAX_RETAGGING_COUNT, 25703f01c91aSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 25713f01c91aSVladimir Oltean if (!new_retagging) { 25723f01c91aSVladimir Oltean kfree(new_vlan); 25733f01c91aSVladimir Oltean return -ENOMEM; 25743f01c91aSVladimir Oltean } 25753f01c91aSVladimir Oltean 2576ec5ae610SVladimir Oltean for (i = 0; i < VLAN_N_VID; i++) 2577ec5ae610SVladimir Oltean new_vlan[i].vlanid = VLAN_N_VID; 2578ec5ae610SVladimir Oltean 25793f01c91aSVladimir Oltean for (i = 0; i < SJA1105_MAX_RETAGGING_COUNT; i++) 25803f01c91aSVladimir Oltean new_retagging[i].vlan_ing = VLAN_N_VID; 25813f01c91aSVladimir Oltean 25823f01c91aSVladimir Oltean for (i = 0; i < priv->ds->num_ports; i++) 25833f01c91aSVladimir Oltean sja1105_init_subvlan_map(subvlan_map[i]); 25843f01c91aSVladimir Oltean 2585ec5ae610SVladimir Oltean /* Bridge VLANs */ 2586ec5ae610SVladimir Oltean rc = sja1105_build_bridge_vlans(priv, new_vlan); 2587ec5ae610SVladimir Oltean if (rc) 2588ec5ae610SVladimir Oltean goto out; 2589ec5ae610SVladimir Oltean 2590ec5ae610SVladimir Oltean /* VLANs necessary for dsa_8021q operation, given to us by tag_8021q.c: 2591ec5ae610SVladimir Oltean * - RX VLANs 2592ec5ae610SVladimir Oltean * - TX VLANs 2593ec5ae610SVladimir Oltean * - Crosschip links 2594ec5ae610SVladimir Oltean */ 2595ec5ae610SVladimir Oltean rc = sja1105_build_dsa_8021q_vlans(priv, new_vlan); 2596ec5ae610SVladimir Oltean if (rc) 2597ec5ae610SVladimir Oltean goto out; 2598ec5ae610SVladimir Oltean 25993f01c91aSVladimir Oltean /* Private VLANs necessary for dsa_8021q operation, which we need to 26003f01c91aSVladimir Oltean * determine on our own: 26013f01c91aSVladimir Oltean * - Sub-VLANs 26023f01c91aSVladimir Oltean * - Sub-VLANs of crosschip switches 26033f01c91aSVladimir Oltean */ 26043f01c91aSVladimir Oltean rc = sja1105_build_subvlans(priv, subvlan_map, new_vlan, new_retagging, 26053f01c91aSVladimir Oltean &num_retagging); 26063f01c91aSVladimir Oltean if (rc) 26073f01c91aSVladimir Oltean goto out; 26083f01c91aSVladimir Oltean 26093f01c91aSVladimir Oltean rc = sja1105_build_crosschip_subvlans(priv, new_vlan, new_retagging, 26103f01c91aSVladimir Oltean &num_retagging); 26113f01c91aSVladimir Oltean if (rc) 26123f01c91aSVladimir Oltean goto out; 26133f01c91aSVladimir Oltean 26143f01c91aSVladimir Oltean rc = sja1105_commit_vlans(priv, new_vlan, new_retagging, num_retagging); 2615ec5ae610SVladimir Oltean if (rc) 2616ec5ae610SVladimir Oltean goto out; 2617ec5ae610SVladimir Oltean 2618ec5ae610SVladimir Oltean rc = sja1105_commit_pvid(priv); 2619ec5ae610SVladimir Oltean if (rc) 2620ec5ae610SVladimir Oltean goto out; 2621ec5ae610SVladimir Oltean 26223f01c91aSVladimir Oltean for (i = 0; i < priv->ds->num_ports; i++) 26233f01c91aSVladimir Oltean sja1105_commit_subvlan_map(priv, i, subvlan_map[i]); 26243f01c91aSVladimir Oltean 2625ec5ae610SVladimir Oltean if (notify) { 2626ec5ae610SVladimir Oltean rc = sja1105_notify_crosschip_switches(priv); 2627ec5ae610SVladimir Oltean if (rc) 2628ec5ae610SVladimir Oltean goto out; 2629ec5ae610SVladimir Oltean } 2630ec5ae610SVladimir Oltean 2631ec5ae610SVladimir Oltean out: 2632ec5ae610SVladimir Oltean kfree(new_vlan); 26333f01c91aSVladimir Oltean kfree(new_retagging); 2634ec5ae610SVladimir Oltean 2635ec5ae610SVladimir Oltean return rc; 2636ec5ae610SVladimir Oltean } 2637ec5ae610SVladimir Oltean 2638070ca3bbSVladimir Oltean /* The TPID setting belongs to the General Parameters table, 2639070ca3bbSVladimir Oltean * which can only be partially reconfigured at runtime (and not the TPID). 2640070ca3bbSVladimir Oltean * So a switch reset is required. 2641070ca3bbSVladimir Oltean */ 2642bae33f2bSVladimir Oltean int sja1105_vlan_filtering(struct dsa_switch *ds, int port, bool enabled) 26436666cebcSVladimir Oltean { 26446d7c7d94SVladimir Oltean struct sja1105_l2_lookup_params_entry *l2_lookup_params; 2645070ca3bbSVladimir Oltean struct sja1105_general_params_entry *general_params; 26466666cebcSVladimir Oltean struct sja1105_private *priv = ds->priv; 26477f14937fSVladimir Oltean enum sja1105_vlan_state state; 2648070ca3bbSVladimir Oltean struct sja1105_table *table; 2649dfacc5a2SVladimir Oltean struct sja1105_rule *rule; 26502cafa72eSVladimir Oltean bool want_tagging; 2651070ca3bbSVladimir Oltean u16 tpid, tpid2; 26526666cebcSVladimir Oltean int rc; 26536666cebcSVladimir Oltean 2654dfacc5a2SVladimir Oltean list_for_each_entry(rule, &priv->flow_block.rules, list) { 2655dfacc5a2SVladimir Oltean if (rule->type == SJA1105_RULE_VL) { 2656dfacc5a2SVladimir Oltean dev_err(ds->dev, 26572e554a7aSVladimir Oltean "Cannot change VLAN filtering with active VL rules\n"); 2658dfacc5a2SVladimir Oltean return -EBUSY; 2659dfacc5a2SVladimir Oltean } 2660dfacc5a2SVladimir Oltean } 2661dfacc5a2SVladimir Oltean 2662070ca3bbSVladimir Oltean if (enabled) { 26636666cebcSVladimir Oltean /* Enable VLAN filtering. */ 266454fa49eeSVladimir Oltean tpid = ETH_P_8021Q; 266554fa49eeSVladimir Oltean tpid2 = ETH_P_8021AD; 2666070ca3bbSVladimir Oltean } else { 26676666cebcSVladimir Oltean /* Disable VLAN filtering. */ 2668070ca3bbSVladimir Oltean tpid = ETH_P_SJA1105; 2669070ca3bbSVladimir Oltean tpid2 = ETH_P_SJA1105; 2670070ca3bbSVladimir Oltean } 2671070ca3bbSVladimir Oltean 267238b5beeaSVladimir Oltean for (port = 0; port < ds->num_ports; port++) { 267338b5beeaSVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 267438b5beeaSVladimir Oltean 267538b5beeaSVladimir Oltean if (enabled) 267638b5beeaSVladimir Oltean sp->xmit_tpid = priv->info->qinq_tpid; 267738b5beeaSVladimir Oltean else 267838b5beeaSVladimir Oltean sp->xmit_tpid = ETH_P_SJA1105; 267938b5beeaSVladimir Oltean } 268038b5beeaSVladimir Oltean 26817f14937fSVladimir Oltean if (!enabled) 26827f14937fSVladimir Oltean state = SJA1105_VLAN_UNAWARE; 26832cafa72eSVladimir Oltean else if (priv->best_effort_vlan_filtering) 26842cafa72eSVladimir Oltean state = SJA1105_VLAN_BEST_EFFORT; 26857f14937fSVladimir Oltean else 26867f14937fSVladimir Oltean state = SJA1105_VLAN_FILTERING_FULL; 26877f14937fSVladimir Oltean 2688cfa36b1fSVladimir Oltean if (priv->vlan_state == state) 2689cfa36b1fSVladimir Oltean return 0; 2690cfa36b1fSVladimir Oltean 26917f14937fSVladimir Oltean priv->vlan_state = state; 26922cafa72eSVladimir Oltean want_tagging = (state == SJA1105_VLAN_UNAWARE || 26932cafa72eSVladimir Oltean state == SJA1105_VLAN_BEST_EFFORT); 26947f14937fSVladimir Oltean 2695070ca3bbSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; 2696070ca3bbSVladimir Oltean general_params = table->entries; 2697f9a1a764SVladimir Oltean /* EtherType used to identify inner tagged (C-tag) VLAN traffic */ 269854fa49eeSVladimir Oltean general_params->tpid = tpid; 269954fa49eeSVladimir Oltean /* EtherType used to identify outer tagged (S-tag) VLAN traffic */ 2700070ca3bbSVladimir Oltean general_params->tpid2 = tpid2; 270142824463SVladimir Oltean /* When VLAN filtering is on, we need to at least be able to 270242824463SVladimir Oltean * decode management traffic through the "backup plan". 270342824463SVladimir Oltean */ 270442824463SVladimir Oltean general_params->incl_srcpt1 = enabled; 270542824463SVladimir Oltean general_params->incl_srcpt0 = enabled; 2706070ca3bbSVladimir Oltean 27072cafa72eSVladimir Oltean want_tagging = priv->best_effort_vlan_filtering || !enabled; 27082cafa72eSVladimir Oltean 27096d7c7d94SVladimir Oltean /* VLAN filtering => independent VLAN learning. 27102cafa72eSVladimir Oltean * No VLAN filtering (or best effort) => shared VLAN learning. 27116d7c7d94SVladimir Oltean * 27126d7c7d94SVladimir Oltean * In shared VLAN learning mode, untagged traffic still gets 27136d7c7d94SVladimir Oltean * pvid-tagged, and the FDB table gets populated with entries 27146d7c7d94SVladimir Oltean * containing the "real" (pvid or from VLAN tag) VLAN ID. 27156d7c7d94SVladimir Oltean * However the switch performs a masked L2 lookup in the FDB, 27166d7c7d94SVladimir Oltean * effectively only looking up a frame's DMAC (and not VID) for the 27176d7c7d94SVladimir Oltean * forwarding decision. 27186d7c7d94SVladimir Oltean * 27196d7c7d94SVladimir Oltean * This is extremely convenient for us, because in modes with 27206d7c7d94SVladimir Oltean * vlan_filtering=0, dsa_8021q actually installs unique pvid's into 27216d7c7d94SVladimir Oltean * each front panel port. This is good for identification but breaks 27226d7c7d94SVladimir Oltean * learning badly - the VID of the learnt FDB entry is unique, aka 27236d7c7d94SVladimir Oltean * no frames coming from any other port are going to have it. So 27246d7c7d94SVladimir Oltean * for forwarding purposes, this is as though learning was broken 27256d7c7d94SVladimir Oltean * (all frames get flooded). 27266d7c7d94SVladimir Oltean */ 27276d7c7d94SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS]; 27286d7c7d94SVladimir Oltean l2_lookup_params = table->entries; 27292cafa72eSVladimir Oltean l2_lookup_params->shared_learn = want_tagging; 27306d7c7d94SVladimir Oltean 2731aaa270c6SVladimir Oltean sja1105_frame_memory_partitioning(priv); 2732aaa270c6SVladimir Oltean 2733aef31718SVladimir Oltean rc = sja1105_build_vlan_table(priv, false); 2734aef31718SVladimir Oltean if (rc) 2735aef31718SVladimir Oltean return rc; 2736aef31718SVladimir Oltean 27372eea1fa8SVladimir Oltean rc = sja1105_static_config_reload(priv, SJA1105_VLAN_FILTERING); 27386666cebcSVladimir Oltean if (rc) 27396666cebcSVladimir Oltean dev_err(ds->dev, "Failed to change VLAN Ethertype\n"); 27406666cebcSVladimir Oltean 2741227d07a0SVladimir Oltean /* Switch port identification based on 802.1Q is only passable 2742227d07a0SVladimir Oltean * if we are not under a vlan_filtering bridge. So make sure 27432cafa72eSVladimir Oltean * the two configurations are mutually exclusive (of course, the 27442cafa72eSVladimir Oltean * user may know better, i.e. best_effort_vlan_filtering). 2745227d07a0SVladimir Oltean */ 27462cafa72eSVladimir Oltean return sja1105_setup_8021q_tagging(ds, want_tagging); 27476666cebcSVladimir Oltean } 27486666cebcSVladimir Oltean 27495899ee36SVladimir Oltean /* Returns number of VLANs added (0 or 1) on success, 27505899ee36SVladimir Oltean * or a negative error code. 27515899ee36SVladimir Oltean */ 27525899ee36SVladimir Oltean static int sja1105_vlan_add_one(struct dsa_switch *ds, int port, u16 vid, 27535899ee36SVladimir Oltean u16 flags, struct list_head *vlan_list) 27545899ee36SVladimir Oltean { 27555899ee36SVladimir Oltean bool untagged = flags & BRIDGE_VLAN_INFO_UNTAGGED; 27565899ee36SVladimir Oltean bool pvid = flags & BRIDGE_VLAN_INFO_PVID; 27575899ee36SVladimir Oltean struct sja1105_bridge_vlan *v; 27585899ee36SVladimir Oltean 27595899ee36SVladimir Oltean list_for_each_entry(v, vlan_list, list) 27605899ee36SVladimir Oltean if (v->port == port && v->vid == vid && 27615899ee36SVladimir Oltean v->untagged == untagged && v->pvid == pvid) 27625899ee36SVladimir Oltean /* Already added */ 27635899ee36SVladimir Oltean return 0; 27645899ee36SVladimir Oltean 27655899ee36SVladimir Oltean v = kzalloc(sizeof(*v), GFP_KERNEL); 27665899ee36SVladimir Oltean if (!v) { 27675899ee36SVladimir Oltean dev_err(ds->dev, "Out of memory while storing VLAN\n"); 27685899ee36SVladimir Oltean return -ENOMEM; 27695899ee36SVladimir Oltean } 27705899ee36SVladimir Oltean 27715899ee36SVladimir Oltean v->port = port; 27725899ee36SVladimir Oltean v->vid = vid; 27735899ee36SVladimir Oltean v->untagged = untagged; 27745899ee36SVladimir Oltean v->pvid = pvid; 27755899ee36SVladimir Oltean list_add(&v->list, vlan_list); 27765899ee36SVladimir Oltean 27775899ee36SVladimir Oltean return 1; 27785899ee36SVladimir Oltean } 27795899ee36SVladimir Oltean 27805899ee36SVladimir Oltean /* Returns number of VLANs deleted (0 or 1) */ 27815899ee36SVladimir Oltean static int sja1105_vlan_del_one(struct dsa_switch *ds, int port, u16 vid, 27825899ee36SVladimir Oltean struct list_head *vlan_list) 27835899ee36SVladimir Oltean { 27845899ee36SVladimir Oltean struct sja1105_bridge_vlan *v, *n; 27855899ee36SVladimir Oltean 27865899ee36SVladimir Oltean list_for_each_entry_safe(v, n, vlan_list, list) { 27875899ee36SVladimir Oltean if (v->port == port && v->vid == vid) { 27885899ee36SVladimir Oltean list_del(&v->list); 27895899ee36SVladimir Oltean kfree(v); 27905899ee36SVladimir Oltean return 1; 27915899ee36SVladimir Oltean } 27925899ee36SVladimir Oltean } 27935899ee36SVladimir Oltean 27945899ee36SVladimir Oltean return 0; 27955899ee36SVladimir Oltean } 27965899ee36SVladimir Oltean 27971958d581SVladimir Oltean static int sja1105_vlan_add(struct dsa_switch *ds, int port, 27986666cebcSVladimir Oltean const struct switchdev_obj_port_vlan *vlan) 27996666cebcSVladimir Oltean { 28006666cebcSVladimir Oltean struct sja1105_private *priv = ds->priv; 2801ec5ae610SVladimir Oltean bool vlan_table_changed = false; 28026666cebcSVladimir Oltean int rc; 28036666cebcSVladimir Oltean 28041958d581SVladimir Oltean /* If the user wants best-effort VLAN filtering (aka vlan_filtering 28051958d581SVladimir Oltean * bridge plus tagging), be sure to at least deny alterations to the 28061958d581SVladimir Oltean * configuration done by dsa_8021q. 28071958d581SVladimir Oltean */ 28081958d581SVladimir Oltean if (priv->vlan_state != SJA1105_VLAN_FILTERING_FULL && 28091958d581SVladimir Oltean vid_is_dsa_8021q(vlan->vid)) { 28101958d581SVladimir Oltean dev_err(ds->dev, "Range 1024-3071 reserved for dsa_8021q operation\n"); 28111958d581SVladimir Oltean return -EBUSY; 28121958d581SVladimir Oltean } 28131958d581SVladimir Oltean 2814b7a9e0daSVladimir Oltean rc = sja1105_vlan_add_one(ds, port, vlan->vid, vlan->flags, 28155899ee36SVladimir Oltean &priv->bridge_vlans); 28165899ee36SVladimir Oltean if (rc < 0) 28171958d581SVladimir Oltean return rc; 28185899ee36SVladimir Oltean if (rc > 0) 2819ec5ae610SVladimir Oltean vlan_table_changed = true; 2820ec5ae610SVladimir Oltean 2821ec5ae610SVladimir Oltean if (!vlan_table_changed) 28221958d581SVladimir Oltean return 0; 2823ec5ae610SVladimir Oltean 28241958d581SVladimir Oltean return sja1105_build_vlan_table(priv, true); 28256666cebcSVladimir Oltean } 28266666cebcSVladimir Oltean 28276666cebcSVladimir Oltean static int sja1105_vlan_del(struct dsa_switch *ds, int port, 28286666cebcSVladimir Oltean const struct switchdev_obj_port_vlan *vlan) 28296666cebcSVladimir Oltean { 28306666cebcSVladimir Oltean struct sja1105_private *priv = ds->priv; 2831ec5ae610SVladimir Oltean bool vlan_table_changed = false; 28325899ee36SVladimir Oltean int rc; 28336666cebcSVladimir Oltean 2834b7a9e0daSVladimir Oltean rc = sja1105_vlan_del_one(ds, port, vlan->vid, &priv->bridge_vlans); 28355899ee36SVladimir Oltean if (rc > 0) 2836ec5ae610SVladimir Oltean vlan_table_changed = true; 2837ec5ae610SVladimir Oltean 2838ec5ae610SVladimir Oltean if (!vlan_table_changed) 28396666cebcSVladimir Oltean return 0; 2840ec5ae610SVladimir Oltean 2841ec5ae610SVladimir Oltean return sja1105_build_vlan_table(priv, true); 28426666cebcSVladimir Oltean } 28436666cebcSVladimir Oltean 28445899ee36SVladimir Oltean static int sja1105_dsa_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid, 28455899ee36SVladimir Oltean u16 flags) 28465899ee36SVladimir Oltean { 28475899ee36SVladimir Oltean struct sja1105_private *priv = ds->priv; 28485899ee36SVladimir Oltean int rc; 28495899ee36SVladimir Oltean 28505899ee36SVladimir Oltean rc = sja1105_vlan_add_one(ds, port, vid, flags, &priv->dsa_8021q_vlans); 28515899ee36SVladimir Oltean if (rc <= 0) 28525899ee36SVladimir Oltean return rc; 28535899ee36SVladimir Oltean 28545899ee36SVladimir Oltean return sja1105_build_vlan_table(priv, true); 28555899ee36SVladimir Oltean } 28565899ee36SVladimir Oltean 28575899ee36SVladimir Oltean static int sja1105_dsa_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid) 28585899ee36SVladimir Oltean { 28595899ee36SVladimir Oltean struct sja1105_private *priv = ds->priv; 28605899ee36SVladimir Oltean int rc; 28615899ee36SVladimir Oltean 28625899ee36SVladimir Oltean rc = sja1105_vlan_del_one(ds, port, vid, &priv->dsa_8021q_vlans); 28635899ee36SVladimir Oltean if (!rc) 28645899ee36SVladimir Oltean return 0; 28655899ee36SVladimir Oltean 28665899ee36SVladimir Oltean return sja1105_build_vlan_table(priv, true); 28675899ee36SVladimir Oltean } 28685899ee36SVladimir Oltean 28695899ee36SVladimir Oltean static const struct dsa_8021q_ops sja1105_dsa_8021q_ops = { 28705899ee36SVladimir Oltean .vlan_add = sja1105_dsa_8021q_vlan_add, 28715899ee36SVladimir Oltean .vlan_del = sja1105_dsa_8021q_vlan_del, 28725899ee36SVladimir Oltean }; 28735899ee36SVladimir Oltean 28748aa9ebccSVladimir Oltean /* The programming model for the SJA1105 switch is "all-at-once" via static 28758aa9ebccSVladimir Oltean * configuration tables. Some of these can be dynamically modified at runtime, 28768aa9ebccSVladimir Oltean * but not the xMII mode parameters table. 28778aa9ebccSVladimir Oltean * Furthermode, some PHYs may not have crystals for generating their clocks 28788aa9ebccSVladimir Oltean * (e.g. RMII). Instead, their 50MHz clock is supplied via the SJA1105 port's 28798aa9ebccSVladimir Oltean * ref_clk pin. So port clocking needs to be initialized early, before 28808aa9ebccSVladimir Oltean * connecting to PHYs is attempted, otherwise they won't respond through MDIO. 28818aa9ebccSVladimir Oltean * Setting correct PHY link speed does not matter now. 28828aa9ebccSVladimir Oltean * But dsa_slave_phy_setup is called later than sja1105_setup, so the PHY 28838aa9ebccSVladimir Oltean * bindings are not yet parsed by DSA core. We need to parse early so that we 28848aa9ebccSVladimir Oltean * can populate the xMII mode parameters table. 28858aa9ebccSVladimir Oltean */ 28868aa9ebccSVladimir Oltean static int sja1105_setup(struct dsa_switch *ds) 28878aa9ebccSVladimir Oltean { 28888aa9ebccSVladimir Oltean struct sja1105_dt_port ports[SJA1105_NUM_PORTS]; 28898aa9ebccSVladimir Oltean struct sja1105_private *priv = ds->priv; 28908aa9ebccSVladimir Oltean int rc; 28918aa9ebccSVladimir Oltean 28928aa9ebccSVladimir Oltean rc = sja1105_parse_dt(priv, ports); 28938aa9ebccSVladimir Oltean if (rc < 0) { 28948aa9ebccSVladimir Oltean dev_err(ds->dev, "Failed to parse DT: %d\n", rc); 28958aa9ebccSVladimir Oltean return rc; 28968aa9ebccSVladimir Oltean } 2897f5b8631cSVladimir Oltean 2898f5b8631cSVladimir Oltean /* Error out early if internal delays are required through DT 2899f5b8631cSVladimir Oltean * and we can't apply them. 2900f5b8631cSVladimir Oltean */ 2901f5b8631cSVladimir Oltean rc = sja1105_parse_rgmii_delays(priv, ports); 2902f5b8631cSVladimir Oltean if (rc < 0) { 2903f5b8631cSVladimir Oltean dev_err(ds->dev, "RGMII delay not supported\n"); 2904f5b8631cSVladimir Oltean return rc; 2905f5b8631cSVladimir Oltean } 2906f5b8631cSVladimir Oltean 290761c77126SVladimir Oltean rc = sja1105_ptp_clock_register(ds); 2908bb77f36aSVladimir Oltean if (rc < 0) { 2909bb77f36aSVladimir Oltean dev_err(ds->dev, "Failed to register PTP clock: %d\n", rc); 2910bb77f36aSVladimir Oltean return rc; 2911bb77f36aSVladimir Oltean } 29128aa9ebccSVladimir Oltean /* Create and send configuration down to device */ 29138aa9ebccSVladimir Oltean rc = sja1105_static_config_load(priv, ports); 29148aa9ebccSVladimir Oltean if (rc < 0) { 29158aa9ebccSVladimir Oltean dev_err(ds->dev, "Failed to load static config: %d\n", rc); 29168aa9ebccSVladimir Oltean return rc; 29178aa9ebccSVladimir Oltean } 29188aa9ebccSVladimir Oltean /* Configure the CGU (PHY link modes and speeds) */ 29198aa9ebccSVladimir Oltean rc = sja1105_clocking_setup(priv); 29208aa9ebccSVladimir Oltean if (rc < 0) { 29218aa9ebccSVladimir Oltean dev_err(ds->dev, "Failed to configure MII clocking: %d\n", rc); 29228aa9ebccSVladimir Oltean return rc; 29238aa9ebccSVladimir Oltean } 29246666cebcSVladimir Oltean /* On SJA1105, VLAN filtering per se is always enabled in hardware. 29256666cebcSVladimir Oltean * The only thing we can do to disable it is lie about what the 802.1Q 29266666cebcSVladimir Oltean * EtherType is. 29276666cebcSVladimir Oltean * So it will still try to apply VLAN filtering, but all ingress 29286666cebcSVladimir Oltean * traffic (except frames received with EtherType of ETH_P_SJA1105) 29296666cebcSVladimir Oltean * will be internally tagged with a distorted VLAN header where the 29306666cebcSVladimir Oltean * TPID is ETH_P_SJA1105, and the VLAN ID is the port pvid. 29316666cebcSVladimir Oltean */ 29326666cebcSVladimir Oltean ds->vlan_filtering_is_global = true; 29338aa9ebccSVladimir Oltean 29345f06c63bSVladimir Oltean /* Advertise the 8 egress queues */ 29355f06c63bSVladimir Oltean ds->num_tx_queues = SJA1105_NUM_TC; 29365f06c63bSVladimir Oltean 2937c279c726SVladimir Oltean ds->mtu_enforcement_ingress = true; 2938c279c726SVladimir Oltean 29390a7bdbc2SVladimir Oltean rc = sja1105_devlink_setup(ds); 29402cafa72eSVladimir Oltean if (rc < 0) 29412cafa72eSVladimir Oltean return rc; 29422cafa72eSVladimir Oltean 2943227d07a0SVladimir Oltean /* The DSA/switchdev model brings up switch ports in standalone mode by 2944227d07a0SVladimir Oltean * default, and that means vlan_filtering is 0 since they're not under 2945227d07a0SVladimir Oltean * a bridge, so it's safe to set up switch tagging at this time. 2946227d07a0SVladimir Oltean */ 2947bbed0bbdSVladimir Oltean rtnl_lock(); 2948bbed0bbdSVladimir Oltean rc = sja1105_setup_8021q_tagging(ds, true); 2949bbed0bbdSVladimir Oltean rtnl_unlock(); 2950bbed0bbdSVladimir Oltean 2951bbed0bbdSVladimir Oltean return rc; 2952227d07a0SVladimir Oltean } 2953227d07a0SVladimir Oltean 2954f3097be2SVladimir Oltean static void sja1105_teardown(struct dsa_switch *ds) 2955f3097be2SVladimir Oltean { 2956f3097be2SVladimir Oltean struct sja1105_private *priv = ds->priv; 2957ec5ae610SVladimir Oltean struct sja1105_bridge_vlan *v, *n; 2958a68578c2SVladimir Oltean int port; 2959a68578c2SVladimir Oltean 2960a68578c2SVladimir Oltean for (port = 0; port < SJA1105_NUM_PORTS; port++) { 2961a68578c2SVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 2962a68578c2SVladimir Oltean 2963a68578c2SVladimir Oltean if (!dsa_is_user_port(ds, port)) 2964a68578c2SVladimir Oltean continue; 2965a68578c2SVladimir Oltean 296652c0d4e3SVladimir Oltean if (sp->xmit_worker) 2967a68578c2SVladimir Oltean kthread_destroy_worker(sp->xmit_worker); 2968a68578c2SVladimir Oltean } 2969f3097be2SVladimir Oltean 29700a7bdbc2SVladimir Oltean sja1105_devlink_teardown(ds); 2971a6af7763SVladimir Oltean sja1105_flower_teardown(ds); 2972317ab5b8SVladimir Oltean sja1105_tas_teardown(ds); 297361c77126SVladimir Oltean sja1105_ptp_clock_unregister(ds); 29746cb0abbdSVladimir Oltean sja1105_static_config_free(&priv->static_config); 2975ec5ae610SVladimir Oltean 2976ec5ae610SVladimir Oltean list_for_each_entry_safe(v, n, &priv->dsa_8021q_vlans, list) { 2977ec5ae610SVladimir Oltean list_del(&v->list); 2978ec5ae610SVladimir Oltean kfree(v); 2979ec5ae610SVladimir Oltean } 2980ec5ae610SVladimir Oltean 2981ec5ae610SVladimir Oltean list_for_each_entry_safe(v, n, &priv->bridge_vlans, list) { 2982ec5ae610SVladimir Oltean list_del(&v->list); 2983ec5ae610SVladimir Oltean kfree(v); 2984ec5ae610SVladimir Oltean } 2985f3097be2SVladimir Oltean } 2986f3097be2SVladimir Oltean 2987e9bf9694SVladimir Oltean static int sja1105_port_enable(struct dsa_switch *ds, int port, 2988e9bf9694SVladimir Oltean struct phy_device *phy) 2989e9bf9694SVladimir Oltean { 2990e9bf9694SVladimir Oltean struct net_device *slave; 2991e9bf9694SVladimir Oltean 2992e9bf9694SVladimir Oltean if (!dsa_is_user_port(ds, port)) 2993e9bf9694SVladimir Oltean return 0; 2994e9bf9694SVladimir Oltean 299568bb8ea8SVivien Didelot slave = dsa_to_port(ds, port)->slave; 2996e9bf9694SVladimir Oltean 2997e9bf9694SVladimir Oltean slave->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER; 2998e9bf9694SVladimir Oltean 2999e9bf9694SVladimir Oltean return 0; 3000e9bf9694SVladimir Oltean } 3001e9bf9694SVladimir Oltean 3002a68578c2SVladimir Oltean static void sja1105_port_disable(struct dsa_switch *ds, int port) 3003a68578c2SVladimir Oltean { 3004a68578c2SVladimir Oltean struct sja1105_private *priv = ds->priv; 3005a68578c2SVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 3006a68578c2SVladimir Oltean 3007a68578c2SVladimir Oltean if (!dsa_is_user_port(ds, port)) 3008a68578c2SVladimir Oltean return; 3009a68578c2SVladimir Oltean 3010a68578c2SVladimir Oltean kthread_cancel_work_sync(&sp->xmit_work); 3011a68578c2SVladimir Oltean skb_queue_purge(&sp->xmit_queue); 3012a68578c2SVladimir Oltean } 3013a68578c2SVladimir Oltean 3014227d07a0SVladimir Oltean static int sja1105_mgmt_xmit(struct dsa_switch *ds, int port, int slot, 301547ed985eSVladimir Oltean struct sk_buff *skb, bool takets) 3016227d07a0SVladimir Oltean { 3017227d07a0SVladimir Oltean struct sja1105_mgmt_entry mgmt_route = {0}; 3018227d07a0SVladimir Oltean struct sja1105_private *priv = ds->priv; 3019227d07a0SVladimir Oltean struct ethhdr *hdr; 3020227d07a0SVladimir Oltean int timeout = 10; 3021227d07a0SVladimir Oltean int rc; 3022227d07a0SVladimir Oltean 3023227d07a0SVladimir Oltean hdr = eth_hdr(skb); 3024227d07a0SVladimir Oltean 3025227d07a0SVladimir Oltean mgmt_route.macaddr = ether_addr_to_u64(hdr->h_dest); 3026227d07a0SVladimir Oltean mgmt_route.destports = BIT(port); 3027227d07a0SVladimir Oltean mgmt_route.enfport = 1; 302847ed985eSVladimir Oltean mgmt_route.tsreg = 0; 302947ed985eSVladimir Oltean mgmt_route.takets = takets; 3030227d07a0SVladimir Oltean 3031227d07a0SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE, 3032227d07a0SVladimir Oltean slot, &mgmt_route, true); 3033227d07a0SVladimir Oltean if (rc < 0) { 3034227d07a0SVladimir Oltean kfree_skb(skb); 3035227d07a0SVladimir Oltean return rc; 3036227d07a0SVladimir Oltean } 3037227d07a0SVladimir Oltean 3038227d07a0SVladimir Oltean /* Transfer skb to the host port. */ 303968bb8ea8SVivien Didelot dsa_enqueue_skb(skb, dsa_to_port(ds, port)->slave); 3040227d07a0SVladimir Oltean 3041227d07a0SVladimir Oltean /* Wait until the switch has processed the frame */ 3042227d07a0SVladimir Oltean do { 3043227d07a0SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_MGMT_ROUTE, 3044227d07a0SVladimir Oltean slot, &mgmt_route); 3045227d07a0SVladimir Oltean if (rc < 0) { 3046227d07a0SVladimir Oltean dev_err_ratelimited(priv->ds->dev, 3047227d07a0SVladimir Oltean "failed to poll for mgmt route\n"); 3048227d07a0SVladimir Oltean continue; 3049227d07a0SVladimir Oltean } 3050227d07a0SVladimir Oltean 3051227d07a0SVladimir Oltean /* UM10944: The ENFPORT flag of the respective entry is 3052227d07a0SVladimir Oltean * cleared when a match is found. The host can use this 3053227d07a0SVladimir Oltean * flag as an acknowledgment. 3054227d07a0SVladimir Oltean */ 3055227d07a0SVladimir Oltean cpu_relax(); 3056227d07a0SVladimir Oltean } while (mgmt_route.enfport && --timeout); 3057227d07a0SVladimir Oltean 3058227d07a0SVladimir Oltean if (!timeout) { 3059227d07a0SVladimir Oltean /* Clean up the management route so that a follow-up 3060227d07a0SVladimir Oltean * frame may not match on it by mistake. 30612a7e7409SVladimir Oltean * This is only hardware supported on P/Q/R/S - on E/T it is 30622a7e7409SVladimir Oltean * a no-op and we are silently discarding the -EOPNOTSUPP. 3063227d07a0SVladimir Oltean */ 3064227d07a0SVladimir Oltean sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE, 3065227d07a0SVladimir Oltean slot, &mgmt_route, false); 3066227d07a0SVladimir Oltean dev_err_ratelimited(priv->ds->dev, "xmit timed out\n"); 3067227d07a0SVladimir Oltean } 3068227d07a0SVladimir Oltean 3069227d07a0SVladimir Oltean return NETDEV_TX_OK; 3070227d07a0SVladimir Oltean } 3071227d07a0SVladimir Oltean 3072a68578c2SVladimir Oltean #define work_to_port(work) \ 3073a68578c2SVladimir Oltean container_of((work), struct sja1105_port, xmit_work) 3074a68578c2SVladimir Oltean #define tagger_to_sja1105(t) \ 3075a68578c2SVladimir Oltean container_of((t), struct sja1105_private, tagger_data) 3076a68578c2SVladimir Oltean 3077227d07a0SVladimir Oltean /* Deferred work is unfortunately necessary because setting up the management 3078227d07a0SVladimir Oltean * route cannot be done from atomit context (SPI transfer takes a sleepable 3079227d07a0SVladimir Oltean * lock on the bus) 3080227d07a0SVladimir Oltean */ 3081a68578c2SVladimir Oltean static void sja1105_port_deferred_xmit(struct kthread_work *work) 3082227d07a0SVladimir Oltean { 3083a68578c2SVladimir Oltean struct sja1105_port *sp = work_to_port(work); 3084a68578c2SVladimir Oltean struct sja1105_tagger_data *tagger_data = sp->data; 3085a68578c2SVladimir Oltean struct sja1105_private *priv = tagger_to_sja1105(tagger_data); 3086a68578c2SVladimir Oltean int port = sp - priv->ports; 3087a68578c2SVladimir Oltean struct sk_buff *skb; 3088a68578c2SVladimir Oltean 3089a68578c2SVladimir Oltean while ((skb = skb_dequeue(&sp->xmit_queue)) != NULL) { 3090a68578c2SVladimir Oltean struct sk_buff *clone = DSA_SKB_CB(skb)->clone; 3091227d07a0SVladimir Oltean 3092227d07a0SVladimir Oltean mutex_lock(&priv->mgmt_lock); 3093227d07a0SVladimir Oltean 3094a68578c2SVladimir Oltean sja1105_mgmt_xmit(priv->ds, port, 0, skb, !!clone); 3095a68578c2SVladimir Oltean 309647ed985eSVladimir Oltean /* The clone, if there, was made by dsa_skb_tx_timestamp */ 3097a68578c2SVladimir Oltean if (clone) 3098a68578c2SVladimir Oltean sja1105_ptp_txtstamp_skb(priv->ds, port, clone); 3099227d07a0SVladimir Oltean 3100227d07a0SVladimir Oltean mutex_unlock(&priv->mgmt_lock); 3101a68578c2SVladimir Oltean } 31028aa9ebccSVladimir Oltean } 31038aa9ebccSVladimir Oltean 31048456721dSVladimir Oltean /* The MAXAGE setting belongs to the L2 Forwarding Parameters table, 31058456721dSVladimir Oltean * which cannot be reconfigured at runtime. So a switch reset is required. 31068456721dSVladimir Oltean */ 31078456721dSVladimir Oltean static int sja1105_set_ageing_time(struct dsa_switch *ds, 31088456721dSVladimir Oltean unsigned int ageing_time) 31098456721dSVladimir Oltean { 31108456721dSVladimir Oltean struct sja1105_l2_lookup_params_entry *l2_lookup_params; 31118456721dSVladimir Oltean struct sja1105_private *priv = ds->priv; 31128456721dSVladimir Oltean struct sja1105_table *table; 31138456721dSVladimir Oltean unsigned int maxage; 31148456721dSVladimir Oltean 31158456721dSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS]; 31168456721dSVladimir Oltean l2_lookup_params = table->entries; 31178456721dSVladimir Oltean 31188456721dSVladimir Oltean maxage = SJA1105_AGEING_TIME_MS(ageing_time); 31198456721dSVladimir Oltean 31208456721dSVladimir Oltean if (l2_lookup_params->maxage == maxage) 31218456721dSVladimir Oltean return 0; 31228456721dSVladimir Oltean 31238456721dSVladimir Oltean l2_lookup_params->maxage = maxage; 31248456721dSVladimir Oltean 31252eea1fa8SVladimir Oltean return sja1105_static_config_reload(priv, SJA1105_AGEING_TIME); 31268456721dSVladimir Oltean } 31278456721dSVladimir Oltean 3128c279c726SVladimir Oltean static int sja1105_change_mtu(struct dsa_switch *ds, int port, int new_mtu) 3129c279c726SVladimir Oltean { 3130c279c726SVladimir Oltean struct sja1105_l2_policing_entry *policing; 3131c279c726SVladimir Oltean struct sja1105_private *priv = ds->priv; 3132c279c726SVladimir Oltean 3133c279c726SVladimir Oltean new_mtu += VLAN_ETH_HLEN + ETH_FCS_LEN; 3134c279c726SVladimir Oltean 3135c279c726SVladimir Oltean if (dsa_is_cpu_port(ds, port)) 3136c279c726SVladimir Oltean new_mtu += VLAN_HLEN; 3137c279c726SVladimir Oltean 3138c279c726SVladimir Oltean policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries; 3139c279c726SVladimir Oltean 3140a7cc081cSVladimir Oltean if (policing[port].maxlen == new_mtu) 3141c279c726SVladimir Oltean return 0; 3142c279c726SVladimir Oltean 3143a7cc081cSVladimir Oltean policing[port].maxlen = new_mtu; 3144c279c726SVladimir Oltean 3145c279c726SVladimir Oltean return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING); 3146c279c726SVladimir Oltean } 3147c279c726SVladimir Oltean 3148c279c726SVladimir Oltean static int sja1105_get_max_mtu(struct dsa_switch *ds, int port) 3149c279c726SVladimir Oltean { 3150c279c726SVladimir Oltean return 2043 - VLAN_ETH_HLEN - ETH_FCS_LEN; 3151c279c726SVladimir Oltean } 3152c279c726SVladimir Oltean 3153317ab5b8SVladimir Oltean static int sja1105_port_setup_tc(struct dsa_switch *ds, int port, 3154317ab5b8SVladimir Oltean enum tc_setup_type type, 3155317ab5b8SVladimir Oltean void *type_data) 3156317ab5b8SVladimir Oltean { 3157317ab5b8SVladimir Oltean switch (type) { 3158317ab5b8SVladimir Oltean case TC_SETUP_QDISC_TAPRIO: 3159317ab5b8SVladimir Oltean return sja1105_setup_tc_taprio(ds, port, type_data); 31604d752508SVladimir Oltean case TC_SETUP_QDISC_CBS: 31614d752508SVladimir Oltean return sja1105_setup_tc_cbs(ds, port, type_data); 3162317ab5b8SVladimir Oltean default: 3163317ab5b8SVladimir Oltean return -EOPNOTSUPP; 3164317ab5b8SVladimir Oltean } 3165317ab5b8SVladimir Oltean } 3166317ab5b8SVladimir Oltean 3167511e6ca0SVladimir Oltean /* We have a single mirror (@to) port, but can configure ingress and egress 3168511e6ca0SVladimir Oltean * mirroring on all other (@from) ports. 3169511e6ca0SVladimir Oltean * We need to allow mirroring rules only as long as the @to port is always the 3170511e6ca0SVladimir Oltean * same, and we need to unset the @to port from mirr_port only when there is no 3171511e6ca0SVladimir Oltean * mirroring rule that references it. 3172511e6ca0SVladimir Oltean */ 3173511e6ca0SVladimir Oltean static int sja1105_mirror_apply(struct sja1105_private *priv, int from, int to, 3174511e6ca0SVladimir Oltean bool ingress, bool enabled) 3175511e6ca0SVladimir Oltean { 3176511e6ca0SVladimir Oltean struct sja1105_general_params_entry *general_params; 3177511e6ca0SVladimir Oltean struct sja1105_mac_config_entry *mac; 3178511e6ca0SVladimir Oltean struct sja1105_table *table; 3179511e6ca0SVladimir Oltean bool already_enabled; 3180511e6ca0SVladimir Oltean u64 new_mirr_port; 3181511e6ca0SVladimir Oltean int rc; 3182511e6ca0SVladimir Oltean 3183511e6ca0SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; 3184511e6ca0SVladimir Oltean general_params = table->entries; 3185511e6ca0SVladimir Oltean 3186511e6ca0SVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 3187511e6ca0SVladimir Oltean 3188511e6ca0SVladimir Oltean already_enabled = (general_params->mirr_port != SJA1105_NUM_PORTS); 3189511e6ca0SVladimir Oltean if (already_enabled && enabled && general_params->mirr_port != to) { 3190511e6ca0SVladimir Oltean dev_err(priv->ds->dev, 3191511e6ca0SVladimir Oltean "Delete mirroring rules towards port %llu first\n", 3192511e6ca0SVladimir Oltean general_params->mirr_port); 3193511e6ca0SVladimir Oltean return -EBUSY; 3194511e6ca0SVladimir Oltean } 3195511e6ca0SVladimir Oltean 3196511e6ca0SVladimir Oltean new_mirr_port = to; 3197511e6ca0SVladimir Oltean if (!enabled) { 3198511e6ca0SVladimir Oltean bool keep = false; 3199511e6ca0SVladimir Oltean int port; 3200511e6ca0SVladimir Oltean 3201511e6ca0SVladimir Oltean /* Anybody still referencing mirr_port? */ 3202511e6ca0SVladimir Oltean for (port = 0; port < SJA1105_NUM_PORTS; port++) { 3203511e6ca0SVladimir Oltean if (mac[port].ing_mirr || mac[port].egr_mirr) { 3204511e6ca0SVladimir Oltean keep = true; 3205511e6ca0SVladimir Oltean break; 3206511e6ca0SVladimir Oltean } 3207511e6ca0SVladimir Oltean } 3208511e6ca0SVladimir Oltean /* Unset already_enabled for next time */ 3209511e6ca0SVladimir Oltean if (!keep) 3210511e6ca0SVladimir Oltean new_mirr_port = SJA1105_NUM_PORTS; 3211511e6ca0SVladimir Oltean } 3212511e6ca0SVladimir Oltean if (new_mirr_port != general_params->mirr_port) { 3213511e6ca0SVladimir Oltean general_params->mirr_port = new_mirr_port; 3214511e6ca0SVladimir Oltean 3215511e6ca0SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_GENERAL_PARAMS, 3216511e6ca0SVladimir Oltean 0, general_params, true); 3217511e6ca0SVladimir Oltean if (rc < 0) 3218511e6ca0SVladimir Oltean return rc; 3219511e6ca0SVladimir Oltean } 3220511e6ca0SVladimir Oltean 3221511e6ca0SVladimir Oltean if (ingress) 3222511e6ca0SVladimir Oltean mac[from].ing_mirr = enabled; 3223511e6ca0SVladimir Oltean else 3224511e6ca0SVladimir Oltean mac[from].egr_mirr = enabled; 3225511e6ca0SVladimir Oltean 3226511e6ca0SVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, from, 3227511e6ca0SVladimir Oltean &mac[from], true); 3228511e6ca0SVladimir Oltean } 3229511e6ca0SVladimir Oltean 3230511e6ca0SVladimir Oltean static int sja1105_mirror_add(struct dsa_switch *ds, int port, 3231511e6ca0SVladimir Oltean struct dsa_mall_mirror_tc_entry *mirror, 3232511e6ca0SVladimir Oltean bool ingress) 3233511e6ca0SVladimir Oltean { 3234511e6ca0SVladimir Oltean return sja1105_mirror_apply(ds->priv, port, mirror->to_local_port, 3235511e6ca0SVladimir Oltean ingress, true); 3236511e6ca0SVladimir Oltean } 3237511e6ca0SVladimir Oltean 3238511e6ca0SVladimir Oltean static void sja1105_mirror_del(struct dsa_switch *ds, int port, 3239511e6ca0SVladimir Oltean struct dsa_mall_mirror_tc_entry *mirror) 3240511e6ca0SVladimir Oltean { 3241511e6ca0SVladimir Oltean sja1105_mirror_apply(ds->priv, port, mirror->to_local_port, 3242511e6ca0SVladimir Oltean mirror->ingress, false); 3243511e6ca0SVladimir Oltean } 3244511e6ca0SVladimir Oltean 3245a7cc081cSVladimir Oltean static int sja1105_port_policer_add(struct dsa_switch *ds, int port, 3246a7cc081cSVladimir Oltean struct dsa_mall_policer_tc_entry *policer) 3247a7cc081cSVladimir Oltean { 3248a7cc081cSVladimir Oltean struct sja1105_l2_policing_entry *policing; 3249a7cc081cSVladimir Oltean struct sja1105_private *priv = ds->priv; 3250a7cc081cSVladimir Oltean 3251a7cc081cSVladimir Oltean policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries; 3252a7cc081cSVladimir Oltean 3253a7cc081cSVladimir Oltean /* In hardware, every 8 microseconds the credit level is incremented by 3254a7cc081cSVladimir Oltean * the value of RATE bytes divided by 64, up to a maximum of SMAX 3255a7cc081cSVladimir Oltean * bytes. 3256a7cc081cSVladimir Oltean */ 3257a7cc081cSVladimir Oltean policing[port].rate = div_u64(512 * policer->rate_bytes_per_sec, 3258a7cc081cSVladimir Oltean 1000000); 32595f035af7SPo Liu policing[port].smax = policer->burst; 3260a7cc081cSVladimir Oltean 3261a7cc081cSVladimir Oltean return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING); 3262a7cc081cSVladimir Oltean } 3263a7cc081cSVladimir Oltean 3264a7cc081cSVladimir Oltean static void sja1105_port_policer_del(struct dsa_switch *ds, int port) 3265a7cc081cSVladimir Oltean { 3266a7cc081cSVladimir Oltean struct sja1105_l2_policing_entry *policing; 3267a7cc081cSVladimir Oltean struct sja1105_private *priv = ds->priv; 3268a7cc081cSVladimir Oltean 3269a7cc081cSVladimir Oltean policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries; 3270a7cc081cSVladimir Oltean 3271a7cc081cSVladimir Oltean policing[port].rate = SJA1105_RATE_MBPS(1000); 3272a7cc081cSVladimir Oltean policing[port].smax = 65535; 3273a7cc081cSVladimir Oltean 3274a7cc081cSVladimir Oltean sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING); 3275a7cc081cSVladimir Oltean } 3276a7cc081cSVladimir Oltean 3277*4d942354SVladimir Oltean static int sja1105_port_set_learning(struct sja1105_private *priv, int port, 3278*4d942354SVladimir Oltean bool enabled) 3279*4d942354SVladimir Oltean { 3280*4d942354SVladimir Oltean struct sja1105_mac_config_entry *mac; 3281*4d942354SVladimir Oltean int rc; 3282*4d942354SVladimir Oltean 3283*4d942354SVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 3284*4d942354SVladimir Oltean 3285*4d942354SVladimir Oltean mac[port].dyn_learn = !!(priv->learn_ena & BIT(port)); 3286*4d942354SVladimir Oltean 3287*4d942354SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, 3288*4d942354SVladimir Oltean &mac[port], true); 3289*4d942354SVladimir Oltean if (rc) 3290*4d942354SVladimir Oltean return rc; 3291*4d942354SVladimir Oltean 3292*4d942354SVladimir Oltean if (enabled) 3293*4d942354SVladimir Oltean priv->learn_ena |= BIT(port); 3294*4d942354SVladimir Oltean else 3295*4d942354SVladimir Oltean priv->learn_ena &= ~BIT(port); 3296*4d942354SVladimir Oltean 3297*4d942354SVladimir Oltean return 0; 3298*4d942354SVladimir Oltean } 3299*4d942354SVladimir Oltean 3300*4d942354SVladimir Oltean /* Common function for unicast and broadcast flood configuration. 3301*4d942354SVladimir Oltean * Flooding is configured between each {ingress, egress} port pair, and since 3302*4d942354SVladimir Oltean * the bridge's semantics are those of "egress flooding", it means we must 3303*4d942354SVladimir Oltean * enable flooding towards this port from all ingress ports that are in the 3304*4d942354SVladimir Oltean * same bridge. In practice, we just enable flooding from all possible ingress 3305*4d942354SVladimir Oltean * ports regardless of whether they're in the same bridge or not, since the 3306*4d942354SVladimir Oltean * reach_port configuration will not allow flooded frames to leak across 3307*4d942354SVladimir Oltean * bridging domains anyway. 3308*4d942354SVladimir Oltean */ 3309*4d942354SVladimir Oltean static int sja1105_port_ucast_bcast_flood(struct sja1105_private *priv, int to, 3310*4d942354SVladimir Oltean struct switchdev_brport_flags flags) 3311*4d942354SVladimir Oltean { 3312*4d942354SVladimir Oltean struct sja1105_l2_forwarding_entry *l2_fwd; 3313*4d942354SVladimir Oltean int from, rc; 3314*4d942354SVladimir Oltean 3315*4d942354SVladimir Oltean l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries; 3316*4d942354SVladimir Oltean 3317*4d942354SVladimir Oltean for (from = 0; from < priv->ds->num_ports; from++) { 3318*4d942354SVladimir Oltean if (dsa_is_unused_port(priv->ds, from)) 3319*4d942354SVladimir Oltean continue; 3320*4d942354SVladimir Oltean if (from == to) 3321*4d942354SVladimir Oltean continue; 3322*4d942354SVladimir Oltean 3323*4d942354SVladimir Oltean /* Unicast */ 3324*4d942354SVladimir Oltean if (flags.mask & BR_FLOOD) { 3325*4d942354SVladimir Oltean if (flags.val & BR_FLOOD) 3326*4d942354SVladimir Oltean l2_fwd[from].fl_domain |= BIT(to); 3327*4d942354SVladimir Oltean else 3328*4d942354SVladimir Oltean l2_fwd[from].fl_domain &= ~BIT(to); 3329*4d942354SVladimir Oltean } 3330*4d942354SVladimir Oltean /* Broadcast */ 3331*4d942354SVladimir Oltean if (flags.mask & BR_BCAST_FLOOD) { 3332*4d942354SVladimir Oltean if (flags.val & BR_BCAST_FLOOD) 3333*4d942354SVladimir Oltean l2_fwd[from].bc_domain |= BIT(to); 3334*4d942354SVladimir Oltean else 3335*4d942354SVladimir Oltean l2_fwd[from].bc_domain &= ~BIT(to); 3336*4d942354SVladimir Oltean } 3337*4d942354SVladimir Oltean 3338*4d942354SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING, 3339*4d942354SVladimir Oltean from, &l2_fwd[from], true); 3340*4d942354SVladimir Oltean if (rc < 0) 3341*4d942354SVladimir Oltean return rc; 3342*4d942354SVladimir Oltean } 3343*4d942354SVladimir Oltean 3344*4d942354SVladimir Oltean return 0; 3345*4d942354SVladimir Oltean } 3346*4d942354SVladimir Oltean 3347*4d942354SVladimir Oltean static int sja1105_port_mcast_flood(struct sja1105_private *priv, int to, 3348*4d942354SVladimir Oltean struct switchdev_brport_flags flags, 3349*4d942354SVladimir Oltean struct netlink_ext_ack *extack) 3350*4d942354SVladimir Oltean { 3351*4d942354SVladimir Oltean struct sja1105_l2_lookup_entry *l2_lookup; 3352*4d942354SVladimir Oltean struct sja1105_table *table; 3353*4d942354SVladimir Oltean int match; 3354*4d942354SVladimir Oltean 3355*4d942354SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; 3356*4d942354SVladimir Oltean l2_lookup = table->entries; 3357*4d942354SVladimir Oltean 3358*4d942354SVladimir Oltean for (match = 0; match < table->entry_count; match++) 3359*4d942354SVladimir Oltean if (l2_lookup[match].macaddr == SJA1105_UNKNOWN_MULTICAST && 3360*4d942354SVladimir Oltean l2_lookup[match].mask_macaddr == SJA1105_UNKNOWN_MULTICAST) 3361*4d942354SVladimir Oltean break; 3362*4d942354SVladimir Oltean 3363*4d942354SVladimir Oltean if (match == table->entry_count) { 3364*4d942354SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 3365*4d942354SVladimir Oltean "Could not find FDB entry for unknown multicast"); 3366*4d942354SVladimir Oltean return -ENOSPC; 3367*4d942354SVladimir Oltean } 3368*4d942354SVladimir Oltean 3369*4d942354SVladimir Oltean if (flags.val & BR_MCAST_FLOOD) 3370*4d942354SVladimir Oltean l2_lookup[match].destports |= BIT(to); 3371*4d942354SVladimir Oltean else 3372*4d942354SVladimir Oltean l2_lookup[match].destports &= ~BIT(to); 3373*4d942354SVladimir Oltean 3374*4d942354SVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 3375*4d942354SVladimir Oltean l2_lookup[match].index, 3376*4d942354SVladimir Oltean &l2_lookup[match], 3377*4d942354SVladimir Oltean true); 3378*4d942354SVladimir Oltean } 3379*4d942354SVladimir Oltean 3380*4d942354SVladimir Oltean static int sja1105_port_pre_bridge_flags(struct dsa_switch *ds, int port, 3381*4d942354SVladimir Oltean struct switchdev_brport_flags flags, 3382*4d942354SVladimir Oltean struct netlink_ext_ack *extack) 3383*4d942354SVladimir Oltean { 3384*4d942354SVladimir Oltean struct sja1105_private *priv = ds->priv; 3385*4d942354SVladimir Oltean 3386*4d942354SVladimir Oltean if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | 3387*4d942354SVladimir Oltean BR_BCAST_FLOOD)) 3388*4d942354SVladimir Oltean return -EINVAL; 3389*4d942354SVladimir Oltean 3390*4d942354SVladimir Oltean if (flags.mask & (BR_FLOOD | BR_MCAST_FLOOD) && 3391*4d942354SVladimir Oltean !priv->info->can_limit_mcast_flood) { 3392*4d942354SVladimir Oltean bool multicast = !!(flags.val & BR_MCAST_FLOOD); 3393*4d942354SVladimir Oltean bool unicast = !!(flags.val & BR_FLOOD); 3394*4d942354SVladimir Oltean 3395*4d942354SVladimir Oltean if (unicast != multicast) { 3396*4d942354SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 3397*4d942354SVladimir Oltean "This chip cannot configure multicast flooding independently of unicast"); 3398*4d942354SVladimir Oltean return -EINVAL; 3399*4d942354SVladimir Oltean } 3400*4d942354SVladimir Oltean } 3401*4d942354SVladimir Oltean 3402*4d942354SVladimir Oltean return 0; 3403*4d942354SVladimir Oltean } 3404*4d942354SVladimir Oltean 3405*4d942354SVladimir Oltean static int sja1105_port_bridge_flags(struct dsa_switch *ds, int port, 3406*4d942354SVladimir Oltean struct switchdev_brport_flags flags, 3407*4d942354SVladimir Oltean struct netlink_ext_ack *extack) 3408*4d942354SVladimir Oltean { 3409*4d942354SVladimir Oltean struct sja1105_private *priv = ds->priv; 3410*4d942354SVladimir Oltean int rc; 3411*4d942354SVladimir Oltean 3412*4d942354SVladimir Oltean if (flags.mask & BR_LEARNING) { 3413*4d942354SVladimir Oltean bool learn_ena = !!(flags.val & BR_LEARNING); 3414*4d942354SVladimir Oltean 3415*4d942354SVladimir Oltean rc = sja1105_port_set_learning(priv, port, learn_ena); 3416*4d942354SVladimir Oltean if (rc) 3417*4d942354SVladimir Oltean return rc; 3418*4d942354SVladimir Oltean } 3419*4d942354SVladimir Oltean 3420*4d942354SVladimir Oltean if (flags.mask & (BR_FLOOD | BR_BCAST_FLOOD)) { 3421*4d942354SVladimir Oltean rc = sja1105_port_ucast_bcast_flood(priv, port, flags); 3422*4d942354SVladimir Oltean if (rc) 3423*4d942354SVladimir Oltean return rc; 3424*4d942354SVladimir Oltean } 3425*4d942354SVladimir Oltean 3426*4d942354SVladimir Oltean /* For chips that can't offload BR_MCAST_FLOOD independently, there 3427*4d942354SVladimir Oltean * is nothing to do here, we ensured the configuration is in sync by 3428*4d942354SVladimir Oltean * offloading BR_FLOOD. 3429*4d942354SVladimir Oltean */ 3430*4d942354SVladimir Oltean if (flags.mask & BR_MCAST_FLOOD && priv->info->can_limit_mcast_flood) { 3431*4d942354SVladimir Oltean rc = sja1105_port_mcast_flood(priv, port, flags, 3432*4d942354SVladimir Oltean extack); 3433*4d942354SVladimir Oltean if (rc) 3434*4d942354SVladimir Oltean return rc; 3435*4d942354SVladimir Oltean } 3436*4d942354SVladimir Oltean 3437*4d942354SVladimir Oltean return 0; 3438*4d942354SVladimir Oltean } 3439*4d942354SVladimir Oltean 34408aa9ebccSVladimir Oltean static const struct dsa_switch_ops sja1105_switch_ops = { 34418aa9ebccSVladimir Oltean .get_tag_protocol = sja1105_get_tag_protocol, 34428aa9ebccSVladimir Oltean .setup = sja1105_setup, 3443f3097be2SVladimir Oltean .teardown = sja1105_teardown, 34448456721dSVladimir Oltean .set_ageing_time = sja1105_set_ageing_time, 3445c279c726SVladimir Oltean .port_change_mtu = sja1105_change_mtu, 3446c279c726SVladimir Oltean .port_max_mtu = sja1105_get_max_mtu, 3447ad9f299aSVladimir Oltean .phylink_validate = sja1105_phylink_validate, 3448ffe10e67SVladimir Oltean .phylink_mac_link_state = sja1105_mac_pcs_get_state, 3449af7cd036SVladimir Oltean .phylink_mac_config = sja1105_mac_config, 34508400cff6SVladimir Oltean .phylink_mac_link_up = sja1105_mac_link_up, 34518400cff6SVladimir Oltean .phylink_mac_link_down = sja1105_mac_link_down, 345252c34e6eSVladimir Oltean .get_strings = sja1105_get_strings, 345352c34e6eSVladimir Oltean .get_ethtool_stats = sja1105_get_ethtool_stats, 345452c34e6eSVladimir Oltean .get_sset_count = sja1105_get_sset_count, 3455bb77f36aSVladimir Oltean .get_ts_info = sja1105_get_ts_info, 3456e9bf9694SVladimir Oltean .port_enable = sja1105_port_enable, 3457a68578c2SVladimir Oltean .port_disable = sja1105_port_disable, 3458291d1e72SVladimir Oltean .port_fdb_dump = sja1105_fdb_dump, 3459291d1e72SVladimir Oltean .port_fdb_add = sja1105_fdb_add, 3460291d1e72SVladimir Oltean .port_fdb_del = sja1105_fdb_del, 34618aa9ebccSVladimir Oltean .port_bridge_join = sja1105_bridge_join, 34628aa9ebccSVladimir Oltean .port_bridge_leave = sja1105_bridge_leave, 3463*4d942354SVladimir Oltean .port_pre_bridge_flags = sja1105_port_pre_bridge_flags, 3464*4d942354SVladimir Oltean .port_bridge_flags = sja1105_port_bridge_flags, 3465640f763fSVladimir Oltean .port_stp_state_set = sja1105_bridge_stp_state_set, 34666666cebcSVladimir Oltean .port_vlan_filtering = sja1105_vlan_filtering, 34676666cebcSVladimir Oltean .port_vlan_add = sja1105_vlan_add, 34686666cebcSVladimir Oltean .port_vlan_del = sja1105_vlan_del, 3469291d1e72SVladimir Oltean .port_mdb_add = sja1105_mdb_add, 3470291d1e72SVladimir Oltean .port_mdb_del = sja1105_mdb_del, 3471a602afd2SVladimir Oltean .port_hwtstamp_get = sja1105_hwtstamp_get, 3472a602afd2SVladimir Oltean .port_hwtstamp_set = sja1105_hwtstamp_set, 3473f3097be2SVladimir Oltean .port_rxtstamp = sja1105_port_rxtstamp, 347447ed985eSVladimir Oltean .port_txtstamp = sja1105_port_txtstamp, 3475317ab5b8SVladimir Oltean .port_setup_tc = sja1105_port_setup_tc, 3476511e6ca0SVladimir Oltean .port_mirror_add = sja1105_mirror_add, 3477511e6ca0SVladimir Oltean .port_mirror_del = sja1105_mirror_del, 3478a7cc081cSVladimir Oltean .port_policer_add = sja1105_port_policer_add, 3479a7cc081cSVladimir Oltean .port_policer_del = sja1105_port_policer_del, 3480a6af7763SVladimir Oltean .cls_flower_add = sja1105_cls_flower_add, 3481a6af7763SVladimir Oltean .cls_flower_del = sja1105_cls_flower_del, 3482834f8933SVladimir Oltean .cls_flower_stats = sja1105_cls_flower_stats, 3483ac02a451SVladimir Oltean .crosschip_bridge_join = sja1105_crosschip_bridge_join, 3484ac02a451SVladimir Oltean .crosschip_bridge_leave = sja1105_crosschip_bridge_leave, 34852cafa72eSVladimir Oltean .devlink_param_get = sja1105_devlink_param_get, 34862cafa72eSVladimir Oltean .devlink_param_set = sja1105_devlink_param_set, 3487ff4cf8eaSVladimir Oltean .devlink_info_get = sja1105_devlink_info_get, 34888aa9ebccSVladimir Oltean }; 34898aa9ebccSVladimir Oltean 34900b0e2997SVladimir Oltean static const struct of_device_id sja1105_dt_ids[]; 34910b0e2997SVladimir Oltean 34928aa9ebccSVladimir Oltean static int sja1105_check_device_id(struct sja1105_private *priv) 34938aa9ebccSVladimir Oltean { 34948aa9ebccSVladimir Oltean const struct sja1105_regs *regs = priv->info->regs; 34958aa9ebccSVladimir Oltean u8 prod_id[SJA1105_SIZE_DEVICE_ID] = {0}; 34968aa9ebccSVladimir Oltean struct device *dev = &priv->spidev->dev; 34970b0e2997SVladimir Oltean const struct of_device_id *match; 3498dff79620SVladimir Oltean u32 device_id; 34998aa9ebccSVladimir Oltean u64 part_no; 35008aa9ebccSVladimir Oltean int rc; 35018aa9ebccSVladimir Oltean 350234d76e9fSVladimir Oltean rc = sja1105_xfer_u32(priv, SPI_READ, regs->device_id, &device_id, 350334d76e9fSVladimir Oltean NULL); 35048aa9ebccSVladimir Oltean if (rc < 0) 35058aa9ebccSVladimir Oltean return rc; 35068aa9ebccSVladimir Oltean 35071bd44870SVladimir Oltean rc = sja1105_xfer_buf(priv, SPI_READ, regs->prod_id, prod_id, 35081bd44870SVladimir Oltean SJA1105_SIZE_DEVICE_ID); 35098aa9ebccSVladimir Oltean if (rc < 0) 35108aa9ebccSVladimir Oltean return rc; 35118aa9ebccSVladimir Oltean 35128aa9ebccSVladimir Oltean sja1105_unpack(prod_id, &part_no, 19, 4, SJA1105_SIZE_DEVICE_ID); 35138aa9ebccSVladimir Oltean 35145978fac0SNathan Chancellor for (match = sja1105_dt_ids; match->compatible[0]; match++) { 35150b0e2997SVladimir Oltean const struct sja1105_info *info = match->data; 35160b0e2997SVladimir Oltean 35170b0e2997SVladimir Oltean /* Is what's been probed in our match table at all? */ 35180b0e2997SVladimir Oltean if (info->device_id != device_id || info->part_no != part_no) 35190b0e2997SVladimir Oltean continue; 35200b0e2997SVladimir Oltean 35210b0e2997SVladimir Oltean /* But is it what's in the device tree? */ 35220b0e2997SVladimir Oltean if (priv->info->device_id != device_id || 35230b0e2997SVladimir Oltean priv->info->part_no != part_no) { 35240b0e2997SVladimir Oltean dev_warn(dev, "Device tree specifies chip %s but found %s, please fix it!\n", 35250b0e2997SVladimir Oltean priv->info->name, info->name); 35260b0e2997SVladimir Oltean /* It isn't. No problem, pick that up. */ 35270b0e2997SVladimir Oltean priv->info = info; 35288aa9ebccSVladimir Oltean } 35298aa9ebccSVladimir Oltean 35308aa9ebccSVladimir Oltean return 0; 35318aa9ebccSVladimir Oltean } 35328aa9ebccSVladimir Oltean 35330b0e2997SVladimir Oltean dev_err(dev, "Unexpected {device ID, part number}: 0x%x 0x%llx\n", 35340b0e2997SVladimir Oltean device_id, part_no); 35350b0e2997SVladimir Oltean 35360b0e2997SVladimir Oltean return -ENODEV; 35370b0e2997SVladimir Oltean } 35380b0e2997SVladimir Oltean 35398aa9ebccSVladimir Oltean static int sja1105_probe(struct spi_device *spi) 35408aa9ebccSVladimir Oltean { 3541844d7edcSVladimir Oltean struct sja1105_tagger_data *tagger_data; 35428aa9ebccSVladimir Oltean struct device *dev = &spi->dev; 35438aa9ebccSVladimir Oltean struct sja1105_private *priv; 35448aa9ebccSVladimir Oltean struct dsa_switch *ds; 3545a68578c2SVladimir Oltean int rc, port; 35468aa9ebccSVladimir Oltean 35478aa9ebccSVladimir Oltean if (!dev->of_node) { 35488aa9ebccSVladimir Oltean dev_err(dev, "No DTS bindings for SJA1105 driver\n"); 35498aa9ebccSVladimir Oltean return -EINVAL; 35508aa9ebccSVladimir Oltean } 35518aa9ebccSVladimir Oltean 35528aa9ebccSVladimir Oltean priv = devm_kzalloc(dev, sizeof(struct sja1105_private), GFP_KERNEL); 35538aa9ebccSVladimir Oltean if (!priv) 35548aa9ebccSVladimir Oltean return -ENOMEM; 35558aa9ebccSVladimir Oltean 35568aa9ebccSVladimir Oltean /* Configure the optional reset pin and bring up switch */ 35578aa9ebccSVladimir Oltean priv->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); 35588aa9ebccSVladimir Oltean if (IS_ERR(priv->reset_gpio)) 35598aa9ebccSVladimir Oltean dev_dbg(dev, "reset-gpios not defined, ignoring\n"); 35608aa9ebccSVladimir Oltean else 35618aa9ebccSVladimir Oltean sja1105_hw_reset(priv->reset_gpio, 1, 1); 35628aa9ebccSVladimir Oltean 35638aa9ebccSVladimir Oltean /* Populate our driver private structure (priv) based on 35648aa9ebccSVladimir Oltean * the device tree node that was probed (spi) 35658aa9ebccSVladimir Oltean */ 35668aa9ebccSVladimir Oltean priv->spidev = spi; 35678aa9ebccSVladimir Oltean spi_set_drvdata(spi, priv); 35688aa9ebccSVladimir Oltean 35698aa9ebccSVladimir Oltean /* Configure the SPI bus */ 35708aa9ebccSVladimir Oltean spi->bits_per_word = 8; 35718aa9ebccSVladimir Oltean rc = spi_setup(spi); 35728aa9ebccSVladimir Oltean if (rc < 0) { 35738aa9ebccSVladimir Oltean dev_err(dev, "Could not init SPI\n"); 35748aa9ebccSVladimir Oltean return rc; 35758aa9ebccSVladimir Oltean } 35768aa9ebccSVladimir Oltean 35778aa9ebccSVladimir Oltean priv->info = of_device_get_match_data(dev); 35788aa9ebccSVladimir Oltean 35798aa9ebccSVladimir Oltean /* Detect hardware device */ 35808aa9ebccSVladimir Oltean rc = sja1105_check_device_id(priv); 35818aa9ebccSVladimir Oltean if (rc < 0) { 35828aa9ebccSVladimir Oltean dev_err(dev, "Device ID check failed: %d\n", rc); 35838aa9ebccSVladimir Oltean return rc; 35848aa9ebccSVladimir Oltean } 35858aa9ebccSVladimir Oltean 35868aa9ebccSVladimir Oltean dev_info(dev, "Probed switch chip: %s\n", priv->info->name); 35878aa9ebccSVladimir Oltean 35887e99e347SVivien Didelot ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL); 35898aa9ebccSVladimir Oltean if (!ds) 35908aa9ebccSVladimir Oltean return -ENOMEM; 35918aa9ebccSVladimir Oltean 35927e99e347SVivien Didelot ds->dev = dev; 35937e99e347SVivien Didelot ds->num_ports = SJA1105_NUM_PORTS; 35948aa9ebccSVladimir Oltean ds->ops = &sja1105_switch_ops; 35958aa9ebccSVladimir Oltean ds->priv = priv; 35968aa9ebccSVladimir Oltean priv->ds = ds; 35978aa9ebccSVladimir Oltean 3598844d7edcSVladimir Oltean tagger_data = &priv->tagger_data; 3599844d7edcSVladimir Oltean 3600d5a619bfSVivien Didelot mutex_init(&priv->ptp_data.lock); 3601d5a619bfSVivien Didelot mutex_init(&priv->mgmt_lock); 3602d5a619bfSVivien Didelot 36035899ee36SVladimir Oltean priv->dsa_8021q_ctx = devm_kzalloc(dev, sizeof(*priv->dsa_8021q_ctx), 36045899ee36SVladimir Oltean GFP_KERNEL); 36055899ee36SVladimir Oltean if (!priv->dsa_8021q_ctx) 36065899ee36SVladimir Oltean return -ENOMEM; 36075899ee36SVladimir Oltean 36085899ee36SVladimir Oltean priv->dsa_8021q_ctx->ops = &sja1105_dsa_8021q_ops; 3609bbed0bbdSVladimir Oltean priv->dsa_8021q_ctx->proto = htons(ETH_P_8021Q); 36105899ee36SVladimir Oltean priv->dsa_8021q_ctx->ds = ds; 36115899ee36SVladimir Oltean 36125899ee36SVladimir Oltean INIT_LIST_HEAD(&priv->dsa_8021q_ctx->crosschip_links); 3613ec5ae610SVladimir Oltean INIT_LIST_HEAD(&priv->bridge_vlans); 3614ec5ae610SVladimir Oltean INIT_LIST_HEAD(&priv->dsa_8021q_vlans); 3615ac02a451SVladimir Oltean 3616d5a619bfSVivien Didelot sja1105_tas_setup(ds); 3617a6af7763SVladimir Oltean sja1105_flower_setup(ds); 3618d5a619bfSVivien Didelot 3619d5a619bfSVivien Didelot rc = dsa_register_switch(priv->ds); 3620d5a619bfSVivien Didelot if (rc) 3621d5a619bfSVivien Didelot return rc; 3622d5a619bfSVivien Didelot 36234d752508SVladimir Oltean if (IS_ENABLED(CONFIG_NET_SCH_CBS)) { 36244d752508SVladimir Oltean priv->cbs = devm_kcalloc(dev, priv->info->num_cbs_shapers, 36254d752508SVladimir Oltean sizeof(struct sja1105_cbs_entry), 36264d752508SVladimir Oltean GFP_KERNEL); 36274d752508SVladimir Oltean if (!priv->cbs) 36284d752508SVladimir Oltean return -ENOMEM; 36294d752508SVladimir Oltean } 36304d752508SVladimir Oltean 3631227d07a0SVladimir Oltean /* Connections between dsa_port and sja1105_port */ 3632a68578c2SVladimir Oltean for (port = 0; port < SJA1105_NUM_PORTS; port++) { 3633a68578c2SVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 3634a68578c2SVladimir Oltean struct dsa_port *dp = dsa_to_port(ds, port); 3635a68578c2SVladimir Oltean struct net_device *slave; 363684eeb5d4SVladimir Oltean int subvlan; 3637227d07a0SVladimir Oltean 3638a68578c2SVladimir Oltean if (!dsa_is_user_port(ds, port)) 3639a68578c2SVladimir Oltean continue; 3640a68578c2SVladimir Oltean 3641a68578c2SVladimir Oltean dp->priv = sp; 3642a68578c2SVladimir Oltean sp->dp = dp; 3643844d7edcSVladimir Oltean sp->data = tagger_data; 3644a68578c2SVladimir Oltean slave = dp->slave; 3645a68578c2SVladimir Oltean kthread_init_work(&sp->xmit_work, sja1105_port_deferred_xmit); 3646a68578c2SVladimir Oltean sp->xmit_worker = kthread_create_worker(0, "%s_xmit", 3647a68578c2SVladimir Oltean slave->name); 3648a68578c2SVladimir Oltean if (IS_ERR(sp->xmit_worker)) { 3649a68578c2SVladimir Oltean rc = PTR_ERR(sp->xmit_worker); 3650a68578c2SVladimir Oltean dev_err(ds->dev, 3651a68578c2SVladimir Oltean "failed to create deferred xmit thread: %d\n", 3652a68578c2SVladimir Oltean rc); 3653a68578c2SVladimir Oltean goto out; 3654a68578c2SVladimir Oltean } 3655a68578c2SVladimir Oltean skb_queue_head_init(&sp->xmit_queue); 365638b5beeaSVladimir Oltean sp->xmit_tpid = ETH_P_SJA1105; 365784eeb5d4SVladimir Oltean 365884eeb5d4SVladimir Oltean for (subvlan = 0; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++) 365984eeb5d4SVladimir Oltean sp->subvlan_map[subvlan] = VLAN_N_VID; 3660227d07a0SVladimir Oltean } 3661227d07a0SVladimir Oltean 3662d5a619bfSVivien Didelot return 0; 3663a68578c2SVladimir Oltean out: 3664a68578c2SVladimir Oltean while (port-- > 0) { 3665a68578c2SVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 3666a68578c2SVladimir Oltean 3667a68578c2SVladimir Oltean if (!dsa_is_user_port(ds, port)) 3668a68578c2SVladimir Oltean continue; 3669a68578c2SVladimir Oltean 3670a68578c2SVladimir Oltean kthread_destroy_worker(sp->xmit_worker); 3671a68578c2SVladimir Oltean } 3672a68578c2SVladimir Oltean return rc; 36738aa9ebccSVladimir Oltean } 36748aa9ebccSVladimir Oltean 36758aa9ebccSVladimir Oltean static int sja1105_remove(struct spi_device *spi) 36768aa9ebccSVladimir Oltean { 36778aa9ebccSVladimir Oltean struct sja1105_private *priv = spi_get_drvdata(spi); 36788aa9ebccSVladimir Oltean 36798aa9ebccSVladimir Oltean dsa_unregister_switch(priv->ds); 36808aa9ebccSVladimir Oltean return 0; 36818aa9ebccSVladimir Oltean } 36828aa9ebccSVladimir Oltean 36838aa9ebccSVladimir Oltean static const struct of_device_id sja1105_dt_ids[] = { 36848aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105e", .data = &sja1105e_info }, 36858aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105t", .data = &sja1105t_info }, 36868aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105p", .data = &sja1105p_info }, 36878aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105q", .data = &sja1105q_info }, 36888aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105r", .data = &sja1105r_info }, 36898aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105s", .data = &sja1105s_info }, 36908aa9ebccSVladimir Oltean { /* sentinel */ }, 36918aa9ebccSVladimir Oltean }; 36928aa9ebccSVladimir Oltean MODULE_DEVICE_TABLE(of, sja1105_dt_ids); 36938aa9ebccSVladimir Oltean 36948aa9ebccSVladimir Oltean static struct spi_driver sja1105_driver = { 36958aa9ebccSVladimir Oltean .driver = { 36968aa9ebccSVladimir Oltean .name = "sja1105", 36978aa9ebccSVladimir Oltean .owner = THIS_MODULE, 36988aa9ebccSVladimir Oltean .of_match_table = of_match_ptr(sja1105_dt_ids), 36998aa9ebccSVladimir Oltean }, 37008aa9ebccSVladimir Oltean .probe = sja1105_probe, 37018aa9ebccSVladimir Oltean .remove = sja1105_remove, 37028aa9ebccSVladimir Oltean }; 37038aa9ebccSVladimir Oltean 37048aa9ebccSVladimir Oltean module_spi_driver(sja1105_driver); 37058aa9ebccSVladimir Oltean 37068aa9ebccSVladimir Oltean MODULE_AUTHOR("Vladimir Oltean <olteanv@gmail.com>"); 37078aa9ebccSVladimir Oltean MODULE_AUTHOR("Georg Waibel <georg.waibel@sensor-technik.de>"); 37088aa9ebccSVladimir Oltean MODULE_DESCRIPTION("SJA1105 Driver"); 37098aa9ebccSVladimir Oltean MODULE_LICENSE("GPL v2"); 3710