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" 25317ab5b8SVladimir Oltean #include "sja1105_tas.h" 268aa9ebccSVladimir Oltean 278aa9ebccSVladimir Oltean static void sja1105_hw_reset(struct gpio_desc *gpio, unsigned int pulse_len, 288aa9ebccSVladimir Oltean unsigned int startup_delay) 298aa9ebccSVladimir Oltean { 308aa9ebccSVladimir Oltean gpiod_set_value_cansleep(gpio, 1); 318aa9ebccSVladimir Oltean /* Wait for minimum reset pulse length */ 328aa9ebccSVladimir Oltean msleep(pulse_len); 338aa9ebccSVladimir Oltean gpiod_set_value_cansleep(gpio, 0); 348aa9ebccSVladimir Oltean /* Wait until chip is ready after reset */ 358aa9ebccSVladimir Oltean msleep(startup_delay); 368aa9ebccSVladimir Oltean } 378aa9ebccSVladimir Oltean 388aa9ebccSVladimir Oltean static void 398aa9ebccSVladimir Oltean sja1105_port_allow_traffic(struct sja1105_l2_forwarding_entry *l2_fwd, 408aa9ebccSVladimir Oltean int from, int to, bool allow) 418aa9ebccSVladimir Oltean { 428aa9ebccSVladimir Oltean if (allow) { 438aa9ebccSVladimir Oltean l2_fwd[from].bc_domain |= BIT(to); 448aa9ebccSVladimir Oltean l2_fwd[from].reach_port |= BIT(to); 458aa9ebccSVladimir Oltean l2_fwd[from].fl_domain |= BIT(to); 468aa9ebccSVladimir Oltean } else { 478aa9ebccSVladimir Oltean l2_fwd[from].bc_domain &= ~BIT(to); 488aa9ebccSVladimir Oltean l2_fwd[from].reach_port &= ~BIT(to); 498aa9ebccSVladimir Oltean l2_fwd[from].fl_domain &= ~BIT(to); 508aa9ebccSVladimir Oltean } 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 1388aa9ebccSVladimir Oltean static int sja1105_init_mii_settings(struct sja1105_private *priv, 1398aa9ebccSVladimir Oltean struct sja1105_dt_port *ports) 1408aa9ebccSVladimir Oltean { 1418aa9ebccSVladimir Oltean struct device *dev = &priv->spidev->dev; 1428aa9ebccSVladimir Oltean struct sja1105_xmii_params_entry *mii; 1438aa9ebccSVladimir Oltean struct sja1105_table *table; 1448aa9ebccSVladimir Oltean int i; 1458aa9ebccSVladimir Oltean 1468aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_XMII_PARAMS]; 1478aa9ebccSVladimir Oltean 1488aa9ebccSVladimir Oltean /* Discard previous xMII Mode Parameters Table */ 1498aa9ebccSVladimir Oltean if (table->entry_count) { 1508aa9ebccSVladimir Oltean kfree(table->entries); 1518aa9ebccSVladimir Oltean table->entry_count = 0; 1528aa9ebccSVladimir Oltean } 1538aa9ebccSVladimir Oltean 1548aa9ebccSVladimir Oltean table->entries = kcalloc(SJA1105_MAX_XMII_PARAMS_COUNT, 1558aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 1568aa9ebccSVladimir Oltean if (!table->entries) 1578aa9ebccSVladimir Oltean return -ENOMEM; 1588aa9ebccSVladimir Oltean 1591fd4a173SVladimir Oltean /* Override table based on PHYLINK DT bindings */ 1608aa9ebccSVladimir Oltean table->entry_count = SJA1105_MAX_XMII_PARAMS_COUNT; 1618aa9ebccSVladimir Oltean 1628aa9ebccSVladimir Oltean mii = table->entries; 1638aa9ebccSVladimir Oltean 1648aa9ebccSVladimir Oltean for (i = 0; i < SJA1105_NUM_PORTS; i++) { 165*ee9d0cb6SVladimir Oltean if (dsa_is_unused_port(priv->ds, i)) 166*ee9d0cb6SVladimir Oltean continue; 167*ee9d0cb6SVladimir Oltean 1688aa9ebccSVladimir Oltean switch (ports[i].phy_mode) { 1698aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_MII: 1708aa9ebccSVladimir Oltean mii->xmii_mode[i] = XMII_MODE_MII; 1718aa9ebccSVladimir Oltean break; 1728aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RMII: 1738aa9ebccSVladimir Oltean mii->xmii_mode[i] = XMII_MODE_RMII; 1748aa9ebccSVladimir Oltean break; 1758aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RGMII: 1768aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RGMII_ID: 1778aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RGMII_RXID: 1788aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RGMII_TXID: 1798aa9ebccSVladimir Oltean mii->xmii_mode[i] = XMII_MODE_RGMII; 1808aa9ebccSVladimir Oltean break; 1818aa9ebccSVladimir Oltean default: 1828aa9ebccSVladimir Oltean dev_err(dev, "Unsupported PHY mode %s!\n", 1838aa9ebccSVladimir Oltean phy_modes(ports[i].phy_mode)); 1848aa9ebccSVladimir Oltean } 1858aa9ebccSVladimir Oltean 1868aa9ebccSVladimir Oltean mii->phy_mac[i] = ports[i].role; 1878aa9ebccSVladimir Oltean } 1888aa9ebccSVladimir Oltean return 0; 1898aa9ebccSVladimir Oltean } 1908aa9ebccSVladimir Oltean 1918aa9ebccSVladimir Oltean static int sja1105_init_static_fdb(struct sja1105_private *priv) 1928aa9ebccSVladimir Oltean { 1938aa9ebccSVladimir Oltean struct sja1105_table *table; 1948aa9ebccSVladimir Oltean 1958aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; 1968aa9ebccSVladimir Oltean 197291d1e72SVladimir Oltean /* We only populate the FDB table through dynamic 198291d1e72SVladimir Oltean * L2 Address Lookup entries 199291d1e72SVladimir Oltean */ 2008aa9ebccSVladimir Oltean if (table->entry_count) { 2018aa9ebccSVladimir Oltean kfree(table->entries); 2028aa9ebccSVladimir Oltean table->entry_count = 0; 2038aa9ebccSVladimir Oltean } 2048aa9ebccSVladimir Oltean return 0; 2058aa9ebccSVladimir Oltean } 2068aa9ebccSVladimir Oltean 2078aa9ebccSVladimir Oltean static int sja1105_init_l2_lookup_params(struct sja1105_private *priv) 2088aa9ebccSVladimir Oltean { 2098aa9ebccSVladimir Oltean struct sja1105_table *table; 2106c56e167SVladimir Oltean u64 max_fdb_entries = SJA1105_MAX_L2_LOOKUP_COUNT / SJA1105_NUM_PORTS; 2118aa9ebccSVladimir Oltean struct sja1105_l2_lookup_params_entry default_l2_lookup_params = { 2128456721dSVladimir Oltean /* Learned FDB entries are forgotten after 300 seconds */ 2138456721dSVladimir Oltean .maxage = SJA1105_AGEING_TIME_MS(300000), 2148aa9ebccSVladimir Oltean /* All entries within a FDB bin are available for learning */ 2158aa9ebccSVladimir Oltean .dyn_tbsz = SJA1105ET_FDB_BIN_SIZE, 2161da73821SVladimir Oltean /* And the P/Q/R/S equivalent setting: */ 2171da73821SVladimir Oltean .start_dynspc = 0, 2186c56e167SVladimir Oltean .maxaddrp = {max_fdb_entries, max_fdb_entries, max_fdb_entries, 2196c56e167SVladimir Oltean max_fdb_entries, max_fdb_entries, }, 2208aa9ebccSVladimir Oltean /* 2^8 + 2^5 + 2^3 + 2^2 + 2^1 + 1 in Koopman notation */ 2218aa9ebccSVladimir Oltean .poly = 0x97, 2228aa9ebccSVladimir Oltean /* This selects between Independent VLAN Learning (IVL) and 2238aa9ebccSVladimir Oltean * Shared VLAN Learning (SVL) 2248aa9ebccSVladimir Oltean */ 2256d7c7d94SVladimir Oltean .shared_learn = true, 2268aa9ebccSVladimir Oltean /* Don't discard management traffic based on ENFPORT - 2278aa9ebccSVladimir Oltean * we don't perform SMAC port enforcement anyway, so 2288aa9ebccSVladimir Oltean * what we are setting here doesn't matter. 2298aa9ebccSVladimir Oltean */ 2308aa9ebccSVladimir Oltean .no_enf_hostprt = false, 2318aa9ebccSVladimir Oltean /* Don't learn SMAC for mac_fltres1 and mac_fltres0. 2328aa9ebccSVladimir Oltean * Maybe correlate with no_linklocal_learn from bridge driver? 2338aa9ebccSVladimir Oltean */ 2348aa9ebccSVladimir Oltean .no_mgmt_learn = true, 2351da73821SVladimir Oltean /* P/Q/R/S only */ 2361da73821SVladimir Oltean .use_static = true, 2371da73821SVladimir Oltean /* Dynamically learned FDB entries can overwrite other (older) 2381da73821SVladimir Oltean * dynamic FDB entries 2391da73821SVladimir Oltean */ 2401da73821SVladimir Oltean .owr_dyn = true, 2411da73821SVladimir Oltean .drpnolearn = true, 2428aa9ebccSVladimir Oltean }; 2438aa9ebccSVladimir Oltean 2448aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS]; 2458aa9ebccSVladimir Oltean 2468aa9ebccSVladimir Oltean if (table->entry_count) { 2478aa9ebccSVladimir Oltean kfree(table->entries); 2488aa9ebccSVladimir Oltean table->entry_count = 0; 2498aa9ebccSVladimir Oltean } 2508aa9ebccSVladimir Oltean 2518aa9ebccSVladimir Oltean table->entries = kcalloc(SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT, 2528aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 2538aa9ebccSVladimir Oltean if (!table->entries) 2548aa9ebccSVladimir Oltean return -ENOMEM; 2558aa9ebccSVladimir Oltean 2568aa9ebccSVladimir Oltean table->entry_count = SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT; 2578aa9ebccSVladimir Oltean 2588aa9ebccSVladimir Oltean /* This table only has a single entry */ 2598aa9ebccSVladimir Oltean ((struct sja1105_l2_lookup_params_entry *)table->entries)[0] = 2608aa9ebccSVladimir Oltean default_l2_lookup_params; 2618aa9ebccSVladimir Oltean 2628aa9ebccSVladimir Oltean return 0; 2638aa9ebccSVladimir Oltean } 2648aa9ebccSVladimir Oltean 2658aa9ebccSVladimir Oltean static int sja1105_init_static_vlan(struct sja1105_private *priv) 2668aa9ebccSVladimir Oltean { 2678aa9ebccSVladimir Oltean struct sja1105_table *table; 2688aa9ebccSVladimir Oltean struct sja1105_vlan_lookup_entry pvid = { 2698aa9ebccSVladimir Oltean .ving_mirr = 0, 2708aa9ebccSVladimir Oltean .vegr_mirr = 0, 2718aa9ebccSVladimir Oltean .vmemb_port = 0, 2728aa9ebccSVladimir Oltean .vlan_bc = 0, 2738aa9ebccSVladimir Oltean .tag_port = 0, 274e3502b82SVladimir Oltean .vlanid = 1, 2758aa9ebccSVladimir Oltean }; 2768aa9ebccSVladimir Oltean int i; 2778aa9ebccSVladimir Oltean 2788aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP]; 2798aa9ebccSVladimir Oltean 280e3502b82SVladimir Oltean /* The static VLAN table will only contain the initial pvid of 1. 2816666cebcSVladimir Oltean * All other VLANs are to be configured through dynamic entries, 2826666cebcSVladimir Oltean * and kept in the static configuration table as backing memory. 2838aa9ebccSVladimir Oltean */ 2848aa9ebccSVladimir Oltean if (table->entry_count) { 2858aa9ebccSVladimir Oltean kfree(table->entries); 2868aa9ebccSVladimir Oltean table->entry_count = 0; 2878aa9ebccSVladimir Oltean } 2888aa9ebccSVladimir Oltean 2898aa9ebccSVladimir Oltean table->entries = kcalloc(1, table->ops->unpacked_entry_size, 2908aa9ebccSVladimir Oltean GFP_KERNEL); 2918aa9ebccSVladimir Oltean if (!table->entries) 2928aa9ebccSVladimir Oltean return -ENOMEM; 2938aa9ebccSVladimir Oltean 2948aa9ebccSVladimir Oltean table->entry_count = 1; 2958aa9ebccSVladimir Oltean 296e3502b82SVladimir Oltean /* VLAN 1: all DT-defined ports are members; no restrictions on 2978aa9ebccSVladimir Oltean * forwarding; always transmit priority-tagged frames as untagged. 2988aa9ebccSVladimir Oltean */ 2998aa9ebccSVladimir Oltean for (i = 0; i < SJA1105_NUM_PORTS; i++) { 3008aa9ebccSVladimir Oltean pvid.vmemb_port |= BIT(i); 3018aa9ebccSVladimir Oltean pvid.vlan_bc |= BIT(i); 3028aa9ebccSVladimir Oltean pvid.tag_port &= ~BIT(i); 3038aa9ebccSVladimir Oltean } 3048aa9ebccSVladimir Oltean 3058aa9ebccSVladimir Oltean ((struct sja1105_vlan_lookup_entry *)table->entries)[0] = pvid; 3068aa9ebccSVladimir Oltean return 0; 3078aa9ebccSVladimir Oltean } 3088aa9ebccSVladimir Oltean 3098aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding(struct sja1105_private *priv) 3108aa9ebccSVladimir Oltean { 3118aa9ebccSVladimir Oltean struct sja1105_l2_forwarding_entry *l2fwd; 3128aa9ebccSVladimir Oltean struct sja1105_table *table; 3138aa9ebccSVladimir Oltean int i, j; 3148aa9ebccSVladimir Oltean 3158aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING]; 3168aa9ebccSVladimir Oltean 3178aa9ebccSVladimir Oltean if (table->entry_count) { 3188aa9ebccSVladimir Oltean kfree(table->entries); 3198aa9ebccSVladimir Oltean table->entry_count = 0; 3208aa9ebccSVladimir Oltean } 3218aa9ebccSVladimir Oltean 3228aa9ebccSVladimir Oltean table->entries = kcalloc(SJA1105_MAX_L2_FORWARDING_COUNT, 3238aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 3248aa9ebccSVladimir Oltean if (!table->entries) 3258aa9ebccSVladimir Oltean return -ENOMEM; 3268aa9ebccSVladimir Oltean 3278aa9ebccSVladimir Oltean table->entry_count = SJA1105_MAX_L2_FORWARDING_COUNT; 3288aa9ebccSVladimir Oltean 3298aa9ebccSVladimir Oltean l2fwd = table->entries; 3308aa9ebccSVladimir Oltean 3318aa9ebccSVladimir Oltean /* First 5 entries define the forwarding rules */ 3328aa9ebccSVladimir Oltean for (i = 0; i < SJA1105_NUM_PORTS; i++) { 3338aa9ebccSVladimir Oltean unsigned int upstream = dsa_upstream_port(priv->ds, i); 3348aa9ebccSVladimir Oltean 3358aa9ebccSVladimir Oltean for (j = 0; j < SJA1105_NUM_TC; j++) 3368aa9ebccSVladimir Oltean l2fwd[i].vlan_pmap[j] = j; 3378aa9ebccSVladimir Oltean 3388aa9ebccSVladimir Oltean if (i == upstream) 3398aa9ebccSVladimir Oltean continue; 3408aa9ebccSVladimir Oltean 3418aa9ebccSVladimir Oltean sja1105_port_allow_traffic(l2fwd, i, upstream, true); 3428aa9ebccSVladimir Oltean sja1105_port_allow_traffic(l2fwd, upstream, i, true); 3438aa9ebccSVladimir Oltean } 3448aa9ebccSVladimir Oltean /* Next 8 entries define VLAN PCP mapping from ingress to egress. 3458aa9ebccSVladimir Oltean * Create a one-to-one mapping. 3468aa9ebccSVladimir Oltean */ 3478aa9ebccSVladimir Oltean for (i = 0; i < SJA1105_NUM_TC; i++) 3488aa9ebccSVladimir Oltean for (j = 0; j < SJA1105_NUM_PORTS; j++) 3498aa9ebccSVladimir Oltean l2fwd[SJA1105_NUM_PORTS + i].vlan_pmap[j] = i; 3508aa9ebccSVladimir Oltean 3518aa9ebccSVladimir Oltean return 0; 3528aa9ebccSVladimir Oltean } 3538aa9ebccSVladimir Oltean 3548aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding_params(struct sja1105_private *priv) 3558aa9ebccSVladimir Oltean { 3568aa9ebccSVladimir Oltean struct sja1105_l2_forwarding_params_entry default_l2fwd_params = { 3578aa9ebccSVladimir Oltean /* Disallow dynamic reconfiguration of vlan_pmap */ 3588aa9ebccSVladimir Oltean .max_dynp = 0, 3598aa9ebccSVladimir Oltean /* Use a single memory partition for all ingress queues */ 3608aa9ebccSVladimir Oltean .part_spc = { SJA1105_MAX_FRAME_MEMORY, 0, 0, 0, 0, 0, 0, 0 }, 3618aa9ebccSVladimir Oltean }; 3628aa9ebccSVladimir Oltean struct sja1105_table *table; 3638aa9ebccSVladimir Oltean 3648aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS]; 3658aa9ebccSVladimir Oltean 3668aa9ebccSVladimir Oltean if (table->entry_count) { 3678aa9ebccSVladimir Oltean kfree(table->entries); 3688aa9ebccSVladimir Oltean table->entry_count = 0; 3698aa9ebccSVladimir Oltean } 3708aa9ebccSVladimir Oltean 3718aa9ebccSVladimir Oltean table->entries = kcalloc(SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT, 3728aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 3738aa9ebccSVladimir Oltean if (!table->entries) 3748aa9ebccSVladimir Oltean return -ENOMEM; 3758aa9ebccSVladimir Oltean 3768aa9ebccSVladimir Oltean table->entry_count = SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT; 3778aa9ebccSVladimir Oltean 3788aa9ebccSVladimir Oltean /* This table only has a single entry */ 3798aa9ebccSVladimir Oltean ((struct sja1105_l2_forwarding_params_entry *)table->entries)[0] = 3808aa9ebccSVladimir Oltean default_l2fwd_params; 3818aa9ebccSVladimir Oltean 3828aa9ebccSVladimir Oltean return 0; 3838aa9ebccSVladimir Oltean } 3848aa9ebccSVladimir Oltean 3858aa9ebccSVladimir Oltean static int sja1105_init_general_params(struct sja1105_private *priv) 3868aa9ebccSVladimir Oltean { 3878aa9ebccSVladimir Oltean struct sja1105_general_params_entry default_general_params = { 388511e6ca0SVladimir Oltean /* Allow dynamic changing of the mirror port */ 389511e6ca0SVladimir Oltean .mirr_ptacu = true, 3908aa9ebccSVladimir Oltean .switchid = priv->ds->index, 3915f06c63bSVladimir Oltean /* Priority queue for link-local management frames 3925f06c63bSVladimir Oltean * (both ingress to and egress from CPU - PTP, STP etc) 3935f06c63bSVladimir Oltean */ 39408fde09aSVladimir Oltean .hostprio = 7, 3958aa9ebccSVladimir Oltean .mac_fltres1 = SJA1105_LINKLOCAL_FILTER_A, 3968aa9ebccSVladimir Oltean .mac_flt1 = SJA1105_LINKLOCAL_FILTER_A_MASK, 39742824463SVladimir Oltean .incl_srcpt1 = false, 3988aa9ebccSVladimir Oltean .send_meta1 = false, 3998aa9ebccSVladimir Oltean .mac_fltres0 = SJA1105_LINKLOCAL_FILTER_B, 4008aa9ebccSVladimir Oltean .mac_flt0 = SJA1105_LINKLOCAL_FILTER_B_MASK, 40142824463SVladimir Oltean .incl_srcpt0 = false, 4028aa9ebccSVladimir Oltean .send_meta0 = false, 4038aa9ebccSVladimir Oltean /* The destination for traffic matching mac_fltres1 and 4048aa9ebccSVladimir Oltean * mac_fltres0 on all ports except host_port. Such traffic 4058aa9ebccSVladimir Oltean * receieved on host_port itself would be dropped, except 4068aa9ebccSVladimir Oltean * by installing a temporary 'management route' 4078aa9ebccSVladimir Oltean */ 4088aa9ebccSVladimir Oltean .host_port = dsa_upstream_port(priv->ds, 0), 409511e6ca0SVladimir Oltean /* Default to an invalid value */ 410511e6ca0SVladimir Oltean .mirr_port = SJA1105_NUM_PORTS, 4118aa9ebccSVladimir Oltean /* Link-local traffic received on casc_port will be forwarded 4128aa9ebccSVladimir Oltean * to host_port without embedding the source port and device ID 4138aa9ebccSVladimir Oltean * info in the destination MAC address (presumably because it 4148aa9ebccSVladimir Oltean * is a cascaded port and a downstream SJA switch already did 4158aa9ebccSVladimir Oltean * that). Default to an invalid port (to disable the feature) 4168aa9ebccSVladimir Oltean * and overwrite this if we find any DSA (cascaded) ports. 4178aa9ebccSVladimir Oltean */ 4188aa9ebccSVladimir Oltean .casc_port = SJA1105_NUM_PORTS, 4198aa9ebccSVladimir Oltean /* No TTEthernet */ 4208aa9ebccSVladimir Oltean .vllupformat = 0, 4218aa9ebccSVladimir Oltean .vlmarker = 0, 4228aa9ebccSVladimir Oltean .vlmask = 0, 4238aa9ebccSVladimir Oltean /* Only update correctionField for 1-step PTP (L2 transport) */ 4248aa9ebccSVladimir Oltean .ignore2stf = 0, 4256666cebcSVladimir Oltean /* Forcefully disable VLAN filtering by telling 4266666cebcSVladimir Oltean * the switch that VLAN has a different EtherType. 4276666cebcSVladimir Oltean */ 4286666cebcSVladimir Oltean .tpid = ETH_P_SJA1105, 4296666cebcSVladimir Oltean .tpid2 = ETH_P_SJA1105, 4308aa9ebccSVladimir Oltean }; 4318aa9ebccSVladimir Oltean struct sja1105_table *table; 4328aa9ebccSVladimir Oltean 4338aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; 4348aa9ebccSVladimir Oltean 4358aa9ebccSVladimir Oltean if (table->entry_count) { 4368aa9ebccSVladimir Oltean kfree(table->entries); 4378aa9ebccSVladimir Oltean table->entry_count = 0; 4388aa9ebccSVladimir Oltean } 4398aa9ebccSVladimir Oltean 4408aa9ebccSVladimir Oltean table->entries = kcalloc(SJA1105_MAX_GENERAL_PARAMS_COUNT, 4418aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 4428aa9ebccSVladimir Oltean if (!table->entries) 4438aa9ebccSVladimir Oltean return -ENOMEM; 4448aa9ebccSVladimir Oltean 4458aa9ebccSVladimir Oltean table->entry_count = SJA1105_MAX_GENERAL_PARAMS_COUNT; 4468aa9ebccSVladimir Oltean 4478aa9ebccSVladimir Oltean /* This table only has a single entry */ 4488aa9ebccSVladimir Oltean ((struct sja1105_general_params_entry *)table->entries)[0] = 4498aa9ebccSVladimir Oltean default_general_params; 4508aa9ebccSVladimir Oltean 4518aa9ebccSVladimir Oltean return 0; 4528aa9ebccSVladimir Oltean } 4538aa9ebccSVladimir Oltean 4548aa9ebccSVladimir Oltean #define SJA1105_RATE_MBPS(speed) (((speed) * 64000) / 1000) 4558aa9ebccSVladimir Oltean 45609c1b412SVladimir Oltean static void sja1105_setup_policer(struct sja1105_l2_policing_entry *policing, 4578aa9ebccSVladimir Oltean int index) 4588aa9ebccSVladimir Oltean { 4598aa9ebccSVladimir Oltean policing[index].sharindx = index; 4608aa9ebccSVladimir Oltean policing[index].smax = 65535; /* Burst size in bytes */ 4618aa9ebccSVladimir Oltean policing[index].rate = SJA1105_RATE_MBPS(1000); 4628aa9ebccSVladimir Oltean policing[index].maxlen = ETH_FRAME_LEN + VLAN_HLEN + ETH_FCS_LEN; 4638aa9ebccSVladimir Oltean policing[index].partition = 0; 4648aa9ebccSVladimir Oltean } 4658aa9ebccSVladimir Oltean 4668aa9ebccSVladimir Oltean static int sja1105_init_l2_policing(struct sja1105_private *priv) 4678aa9ebccSVladimir Oltean { 4688aa9ebccSVladimir Oltean struct sja1105_l2_policing_entry *policing; 4698aa9ebccSVladimir Oltean struct sja1105_table *table; 4708aa9ebccSVladimir Oltean int i, j, k; 4718aa9ebccSVladimir Oltean 4728aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_POLICING]; 4738aa9ebccSVladimir Oltean 4748aa9ebccSVladimir Oltean /* Discard previous L2 Policing Table */ 4758aa9ebccSVladimir Oltean if (table->entry_count) { 4768aa9ebccSVladimir Oltean kfree(table->entries); 4778aa9ebccSVladimir Oltean table->entry_count = 0; 4788aa9ebccSVladimir Oltean } 4798aa9ebccSVladimir Oltean 4808aa9ebccSVladimir Oltean table->entries = kcalloc(SJA1105_MAX_L2_POLICING_COUNT, 4818aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 4828aa9ebccSVladimir Oltean if (!table->entries) 4838aa9ebccSVladimir Oltean return -ENOMEM; 4848aa9ebccSVladimir Oltean 4858aa9ebccSVladimir Oltean table->entry_count = SJA1105_MAX_L2_POLICING_COUNT; 4868aa9ebccSVladimir Oltean 4878aa9ebccSVladimir Oltean policing = table->entries; 4888aa9ebccSVladimir Oltean 4898aa9ebccSVladimir Oltean /* k sweeps through all unicast policers (0-39). 4908aa9ebccSVladimir Oltean * bcast sweeps through policers 40-44. 4918aa9ebccSVladimir Oltean */ 4928aa9ebccSVladimir Oltean for (i = 0, k = 0; i < SJA1105_NUM_PORTS; i++) { 4938aa9ebccSVladimir Oltean int bcast = (SJA1105_NUM_PORTS * SJA1105_NUM_TC) + i; 4948aa9ebccSVladimir Oltean 4958aa9ebccSVladimir Oltean for (j = 0; j < SJA1105_NUM_TC; j++, k++) 4968aa9ebccSVladimir Oltean sja1105_setup_policer(policing, k); 4978aa9ebccSVladimir Oltean 4988aa9ebccSVladimir Oltean /* Set up this port's policer for broadcast traffic */ 4998aa9ebccSVladimir Oltean sja1105_setup_policer(policing, bcast); 5008aa9ebccSVladimir Oltean } 5018aa9ebccSVladimir Oltean return 0; 5028aa9ebccSVladimir Oltean } 5038aa9ebccSVladimir Oltean 5048aa9ebccSVladimir Oltean static int sja1105_static_config_load(struct sja1105_private *priv, 5058aa9ebccSVladimir Oltean struct sja1105_dt_port *ports) 5068aa9ebccSVladimir Oltean { 5078aa9ebccSVladimir Oltean int rc; 5088aa9ebccSVladimir Oltean 5098aa9ebccSVladimir Oltean sja1105_static_config_free(&priv->static_config); 5108aa9ebccSVladimir Oltean rc = sja1105_static_config_init(&priv->static_config, 5118aa9ebccSVladimir Oltean priv->info->static_ops, 5128aa9ebccSVladimir Oltean priv->info->device_id); 5138aa9ebccSVladimir Oltean if (rc) 5148aa9ebccSVladimir Oltean return rc; 5158aa9ebccSVladimir Oltean 5168aa9ebccSVladimir Oltean /* Build static configuration */ 5178aa9ebccSVladimir Oltean rc = sja1105_init_mac_settings(priv); 5188aa9ebccSVladimir Oltean if (rc < 0) 5198aa9ebccSVladimir Oltean return rc; 5208aa9ebccSVladimir Oltean rc = sja1105_init_mii_settings(priv, ports); 5218aa9ebccSVladimir Oltean if (rc < 0) 5228aa9ebccSVladimir Oltean return rc; 5238aa9ebccSVladimir Oltean rc = sja1105_init_static_fdb(priv); 5248aa9ebccSVladimir Oltean if (rc < 0) 5258aa9ebccSVladimir Oltean return rc; 5268aa9ebccSVladimir Oltean rc = sja1105_init_static_vlan(priv); 5278aa9ebccSVladimir Oltean if (rc < 0) 5288aa9ebccSVladimir Oltean return rc; 5298aa9ebccSVladimir Oltean rc = sja1105_init_l2_lookup_params(priv); 5308aa9ebccSVladimir Oltean if (rc < 0) 5318aa9ebccSVladimir Oltean return rc; 5328aa9ebccSVladimir Oltean rc = sja1105_init_l2_forwarding(priv); 5338aa9ebccSVladimir Oltean if (rc < 0) 5348aa9ebccSVladimir Oltean return rc; 5358aa9ebccSVladimir Oltean rc = sja1105_init_l2_forwarding_params(priv); 5368aa9ebccSVladimir Oltean if (rc < 0) 5378aa9ebccSVladimir Oltean return rc; 5388aa9ebccSVladimir Oltean rc = sja1105_init_l2_policing(priv); 5398aa9ebccSVladimir Oltean if (rc < 0) 5408aa9ebccSVladimir Oltean return rc; 5418aa9ebccSVladimir Oltean rc = sja1105_init_general_params(priv); 5428aa9ebccSVladimir Oltean if (rc < 0) 5438aa9ebccSVladimir Oltean return rc; 5448aa9ebccSVladimir Oltean 5458aa9ebccSVladimir Oltean /* Send initial configuration to hardware via SPI */ 5468aa9ebccSVladimir Oltean return sja1105_static_config_upload(priv); 5478aa9ebccSVladimir Oltean } 5488aa9ebccSVladimir Oltean 549f5b8631cSVladimir Oltean static int sja1105_parse_rgmii_delays(struct sja1105_private *priv, 550f5b8631cSVladimir Oltean const struct sja1105_dt_port *ports) 551f5b8631cSVladimir Oltean { 552f5b8631cSVladimir Oltean int i; 553f5b8631cSVladimir Oltean 554f5b8631cSVladimir Oltean for (i = 0; i < SJA1105_NUM_PORTS; i++) { 5559bca3a0aSOleksij Rempel if (ports[i].role == XMII_MAC) 556f5b8631cSVladimir Oltean continue; 557f5b8631cSVladimir Oltean 5589bca3a0aSOleksij Rempel if (ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_RXID || 5599bca3a0aSOleksij Rempel ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_ID) 560f5b8631cSVladimir Oltean priv->rgmii_rx_delay[i] = true; 561f5b8631cSVladimir Oltean 5629bca3a0aSOleksij Rempel if (ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_TXID || 5639bca3a0aSOleksij Rempel ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_ID) 564f5b8631cSVladimir Oltean priv->rgmii_tx_delay[i] = true; 565f5b8631cSVladimir Oltean 566f5b8631cSVladimir Oltean if ((priv->rgmii_rx_delay[i] || priv->rgmii_tx_delay[i]) && 567f5b8631cSVladimir Oltean !priv->info->setup_rgmii_delay) 568f5b8631cSVladimir Oltean return -EINVAL; 569f5b8631cSVladimir Oltean } 570f5b8631cSVladimir Oltean return 0; 571f5b8631cSVladimir Oltean } 572f5b8631cSVladimir Oltean 5738aa9ebccSVladimir Oltean static int sja1105_parse_ports_node(struct sja1105_private *priv, 5748aa9ebccSVladimir Oltean struct sja1105_dt_port *ports, 5758aa9ebccSVladimir Oltean struct device_node *ports_node) 5768aa9ebccSVladimir Oltean { 5778aa9ebccSVladimir Oltean struct device *dev = &priv->spidev->dev; 5788aa9ebccSVladimir Oltean struct device_node *child; 5798aa9ebccSVladimir Oltean 58027afe0d3SVladimir Oltean for_each_available_child_of_node(ports_node, child) { 5818aa9ebccSVladimir Oltean struct device_node *phy_node; 5820c65b2b9SAndrew Lunn phy_interface_t phy_mode; 5838aa9ebccSVladimir Oltean u32 index; 5840c65b2b9SAndrew Lunn int err; 5858aa9ebccSVladimir Oltean 5868aa9ebccSVladimir Oltean /* Get switch port number from DT */ 5878aa9ebccSVladimir Oltean if (of_property_read_u32(child, "reg", &index) < 0) { 5888aa9ebccSVladimir Oltean dev_err(dev, "Port number not defined in device tree " 5898aa9ebccSVladimir Oltean "(property \"reg\")\n"); 5907ba771e3SNishka Dasgupta of_node_put(child); 5918aa9ebccSVladimir Oltean return -ENODEV; 5928aa9ebccSVladimir Oltean } 5938aa9ebccSVladimir Oltean 5948aa9ebccSVladimir Oltean /* Get PHY mode from DT */ 5950c65b2b9SAndrew Lunn err = of_get_phy_mode(child, &phy_mode); 5960c65b2b9SAndrew Lunn if (err) { 5978aa9ebccSVladimir Oltean dev_err(dev, "Failed to read phy-mode or " 5988aa9ebccSVladimir Oltean "phy-interface-type property for port %d\n", 5998aa9ebccSVladimir Oltean index); 6007ba771e3SNishka Dasgupta of_node_put(child); 6018aa9ebccSVladimir Oltean return -ENODEV; 6028aa9ebccSVladimir Oltean } 6038aa9ebccSVladimir Oltean ports[index].phy_mode = phy_mode; 6048aa9ebccSVladimir Oltean 6058aa9ebccSVladimir Oltean phy_node = of_parse_phandle(child, "phy-handle", 0); 6068aa9ebccSVladimir Oltean if (!phy_node) { 6078aa9ebccSVladimir Oltean if (!of_phy_is_fixed_link(child)) { 6088aa9ebccSVladimir Oltean dev_err(dev, "phy-handle or fixed-link " 6098aa9ebccSVladimir Oltean "properties missing!\n"); 6107ba771e3SNishka Dasgupta of_node_put(child); 6118aa9ebccSVladimir Oltean return -ENODEV; 6128aa9ebccSVladimir Oltean } 6138aa9ebccSVladimir Oltean /* phy-handle is missing, but fixed-link isn't. 6148aa9ebccSVladimir Oltean * So it's a fixed link. Default to PHY role. 6158aa9ebccSVladimir Oltean */ 6168aa9ebccSVladimir Oltean ports[index].role = XMII_PHY; 6178aa9ebccSVladimir Oltean } else { 6188aa9ebccSVladimir Oltean /* phy-handle present => put port in MAC role */ 6198aa9ebccSVladimir Oltean ports[index].role = XMII_MAC; 6208aa9ebccSVladimir Oltean of_node_put(phy_node); 6218aa9ebccSVladimir Oltean } 6228aa9ebccSVladimir Oltean 6238aa9ebccSVladimir Oltean /* The MAC/PHY role can be overridden with explicit bindings */ 6248aa9ebccSVladimir Oltean if (of_property_read_bool(child, "sja1105,role-mac")) 6258aa9ebccSVladimir Oltean ports[index].role = XMII_MAC; 6268aa9ebccSVladimir Oltean else if (of_property_read_bool(child, "sja1105,role-phy")) 6278aa9ebccSVladimir Oltean ports[index].role = XMII_PHY; 6288aa9ebccSVladimir Oltean } 6298aa9ebccSVladimir Oltean 6308aa9ebccSVladimir Oltean return 0; 6318aa9ebccSVladimir Oltean } 6328aa9ebccSVladimir Oltean 6338aa9ebccSVladimir Oltean static int sja1105_parse_dt(struct sja1105_private *priv, 6348aa9ebccSVladimir Oltean struct sja1105_dt_port *ports) 6358aa9ebccSVladimir Oltean { 6368aa9ebccSVladimir Oltean struct device *dev = &priv->spidev->dev; 6378aa9ebccSVladimir Oltean struct device_node *switch_node = dev->of_node; 6388aa9ebccSVladimir Oltean struct device_node *ports_node; 6398aa9ebccSVladimir Oltean int rc; 6408aa9ebccSVladimir Oltean 6418aa9ebccSVladimir Oltean ports_node = of_get_child_by_name(switch_node, "ports"); 6428aa9ebccSVladimir Oltean if (!ports_node) { 6438aa9ebccSVladimir Oltean dev_err(dev, "Incorrect bindings: absent \"ports\" node\n"); 6448aa9ebccSVladimir Oltean return -ENODEV; 6458aa9ebccSVladimir Oltean } 6468aa9ebccSVladimir Oltean 6478aa9ebccSVladimir Oltean rc = sja1105_parse_ports_node(priv, ports, ports_node); 6488aa9ebccSVladimir Oltean of_node_put(ports_node); 6498aa9ebccSVladimir Oltean 6508aa9ebccSVladimir Oltean return rc; 6518aa9ebccSVladimir Oltean } 6528aa9ebccSVladimir Oltean 653c44d0535SVladimir Oltean /* Convert link speed from SJA1105 to ethtool encoding */ 6548aa9ebccSVladimir Oltean static int sja1105_speed[] = { 655c44d0535SVladimir Oltean [SJA1105_SPEED_AUTO] = SPEED_UNKNOWN, 656c44d0535SVladimir Oltean [SJA1105_SPEED_10MBPS] = SPEED_10, 657c44d0535SVladimir Oltean [SJA1105_SPEED_100MBPS] = SPEED_100, 658c44d0535SVladimir Oltean [SJA1105_SPEED_1000MBPS] = SPEED_1000, 6598aa9ebccSVladimir Oltean }; 6608aa9ebccSVladimir Oltean 6618400cff6SVladimir Oltean /* Set link speed in the MAC configuration for a specific port. */ 6628aa9ebccSVladimir Oltean static int sja1105_adjust_port_config(struct sja1105_private *priv, int port, 6638400cff6SVladimir Oltean int speed_mbps) 6648aa9ebccSVladimir Oltean { 6658aa9ebccSVladimir Oltean struct sja1105_xmii_params_entry *mii; 6668aa9ebccSVladimir Oltean struct sja1105_mac_config_entry *mac; 6678aa9ebccSVladimir Oltean struct device *dev = priv->ds->dev; 6688aa9ebccSVladimir Oltean sja1105_phy_interface_t phy_mode; 6698aa9ebccSVladimir Oltean sja1105_speed_t speed; 6708aa9ebccSVladimir Oltean int rc; 6718aa9ebccSVladimir Oltean 6728400cff6SVladimir Oltean /* On P/Q/R/S, one can read from the device via the MAC reconfiguration 6738400cff6SVladimir Oltean * tables. On E/T, MAC reconfig tables are not readable, only writable. 6748400cff6SVladimir Oltean * We have to *know* what the MAC looks like. For the sake of keeping 6758400cff6SVladimir Oltean * the code common, we'll use the static configuration tables as a 6768400cff6SVladimir Oltean * reasonable approximation for both E/T and P/Q/R/S. 6778400cff6SVladimir Oltean */ 6788aa9ebccSVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 6798400cff6SVladimir Oltean mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries; 6808aa9ebccSVladimir Oltean 681f4cfcfbdSVladimir Oltean switch (speed_mbps) { 682c44d0535SVladimir Oltean case SPEED_UNKNOWN: 683a979a0abSVladimir Oltean /* PHYLINK called sja1105_mac_config() to inform us about 684a979a0abSVladimir Oltean * the state->interface, but AN has not completed and the 685a979a0abSVladimir Oltean * speed is not yet valid. UM10944.pdf says that setting 686a979a0abSVladimir Oltean * SJA1105_SPEED_AUTO at runtime disables the port, so that is 687a979a0abSVladimir Oltean * ok for power consumption in case AN will never complete - 688a979a0abSVladimir Oltean * otherwise PHYLINK should come back with a new update. 689a979a0abSVladimir Oltean */ 690f4cfcfbdSVladimir Oltean speed = SJA1105_SPEED_AUTO; 691f4cfcfbdSVladimir Oltean break; 692c44d0535SVladimir Oltean case SPEED_10: 693f4cfcfbdSVladimir Oltean speed = SJA1105_SPEED_10MBPS; 694f4cfcfbdSVladimir Oltean break; 695c44d0535SVladimir Oltean case SPEED_100: 696f4cfcfbdSVladimir Oltean speed = SJA1105_SPEED_100MBPS; 697f4cfcfbdSVladimir Oltean break; 698c44d0535SVladimir Oltean case SPEED_1000: 699f4cfcfbdSVladimir Oltean speed = SJA1105_SPEED_1000MBPS; 700f4cfcfbdSVladimir Oltean break; 701f4cfcfbdSVladimir Oltean default: 7028aa9ebccSVladimir Oltean dev_err(dev, "Invalid speed %iMbps\n", speed_mbps); 7038aa9ebccSVladimir Oltean return -EINVAL; 7048aa9ebccSVladimir Oltean } 7058aa9ebccSVladimir Oltean 7068400cff6SVladimir Oltean /* Overwrite SJA1105_SPEED_AUTO from the static MAC configuration 7078400cff6SVladimir Oltean * table, since this will be used for the clocking setup, and we no 7088400cff6SVladimir Oltean * longer need to store it in the static config (already told hardware 7098400cff6SVladimir Oltean * we want auto during upload phase). 7108aa9ebccSVladimir Oltean */ 7118aa9ebccSVladimir Oltean mac[port].speed = speed; 7128aa9ebccSVladimir Oltean 7138aa9ebccSVladimir Oltean /* Write to the dynamic reconfiguration tables */ 7148400cff6SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, 7158400cff6SVladimir Oltean &mac[port], true); 7168aa9ebccSVladimir Oltean if (rc < 0) { 7178aa9ebccSVladimir Oltean dev_err(dev, "Failed to write MAC config: %d\n", rc); 7188aa9ebccSVladimir Oltean return rc; 7198aa9ebccSVladimir Oltean } 7208aa9ebccSVladimir Oltean 7218aa9ebccSVladimir Oltean /* Reconfigure the PLLs for the RGMII interfaces (required 125 MHz at 7228aa9ebccSVladimir Oltean * gigabit, 25 MHz at 100 Mbps and 2.5 MHz at 10 Mbps). For MII and 7238aa9ebccSVladimir Oltean * RMII no change of the clock setup is required. Actually, changing 7248aa9ebccSVladimir Oltean * the clock setup does interrupt the clock signal for a certain time 7258aa9ebccSVladimir Oltean * which causes trouble for all PHYs relying on this signal. 7268aa9ebccSVladimir Oltean */ 7278aa9ebccSVladimir Oltean phy_mode = mii->xmii_mode[port]; 7288aa9ebccSVladimir Oltean if (phy_mode != XMII_MODE_RGMII) 7298aa9ebccSVladimir Oltean return 0; 7308aa9ebccSVladimir Oltean 7318aa9ebccSVladimir Oltean return sja1105_clocking_setup_port(priv, port); 7328aa9ebccSVladimir Oltean } 7338aa9ebccSVladimir Oltean 73439710229SVladimir Oltean /* The SJA1105 MAC programming model is through the static config (the xMII 73539710229SVladimir Oltean * Mode table cannot be dynamically reconfigured), and we have to program 73639710229SVladimir Oltean * that early (earlier than PHYLINK calls us, anyway). 73739710229SVladimir Oltean * So just error out in case the connected PHY attempts to change the initial 73839710229SVladimir Oltean * system interface MII protocol from what is defined in the DT, at least for 73939710229SVladimir Oltean * now. 74039710229SVladimir Oltean */ 74139710229SVladimir Oltean static bool sja1105_phy_mode_mismatch(struct sja1105_private *priv, int port, 74239710229SVladimir Oltean phy_interface_t interface) 74339710229SVladimir Oltean { 74439710229SVladimir Oltean struct sja1105_xmii_params_entry *mii; 74539710229SVladimir Oltean sja1105_phy_interface_t phy_mode; 74639710229SVladimir Oltean 74739710229SVladimir Oltean mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries; 74839710229SVladimir Oltean phy_mode = mii->xmii_mode[port]; 74939710229SVladimir Oltean 75039710229SVladimir Oltean switch (interface) { 75139710229SVladimir Oltean case PHY_INTERFACE_MODE_MII: 75239710229SVladimir Oltean return (phy_mode != XMII_MODE_MII); 75339710229SVladimir Oltean case PHY_INTERFACE_MODE_RMII: 75439710229SVladimir Oltean return (phy_mode != XMII_MODE_RMII); 75539710229SVladimir Oltean case PHY_INTERFACE_MODE_RGMII: 75639710229SVladimir Oltean case PHY_INTERFACE_MODE_RGMII_ID: 75739710229SVladimir Oltean case PHY_INTERFACE_MODE_RGMII_RXID: 75839710229SVladimir Oltean case PHY_INTERFACE_MODE_RGMII_TXID: 75939710229SVladimir Oltean return (phy_mode != XMII_MODE_RGMII); 76039710229SVladimir Oltean default: 76139710229SVladimir Oltean return true; 76239710229SVladimir Oltean } 76339710229SVladimir Oltean } 76439710229SVladimir Oltean 765af7cd036SVladimir Oltean static void sja1105_mac_config(struct dsa_switch *ds, int port, 766af7cd036SVladimir Oltean unsigned int link_an_mode, 767af7cd036SVladimir Oltean const struct phylink_link_state *state) 7688aa9ebccSVladimir Oltean { 7698aa9ebccSVladimir Oltean struct sja1105_private *priv = ds->priv; 7708aa9ebccSVladimir Oltean 771ec8582d1SVladimir Oltean if (sja1105_phy_mode_mismatch(priv, port, state->interface)) { 772ec8582d1SVladimir Oltean dev_err(ds->dev, "Changing PHY mode to %s not supported!\n", 773ec8582d1SVladimir Oltean phy_modes(state->interface)); 77439710229SVladimir Oltean return; 775ec8582d1SVladimir Oltean } 77639710229SVladimir Oltean 7779f971573SVladimir Oltean if (link_an_mode == MLO_AN_INBAND) { 7789f971573SVladimir Oltean dev_err(ds->dev, "In-band AN not supported!\n"); 7799f971573SVladimir Oltean return; 7809f971573SVladimir Oltean } 7818400cff6SVladimir Oltean } 7828400cff6SVladimir Oltean 7838400cff6SVladimir Oltean static void sja1105_mac_link_down(struct dsa_switch *ds, int port, 7848400cff6SVladimir Oltean unsigned int mode, 7858400cff6SVladimir Oltean phy_interface_t interface) 7868400cff6SVladimir Oltean { 7878400cff6SVladimir Oltean sja1105_inhibit_tx(ds->priv, BIT(port), true); 7888400cff6SVladimir Oltean } 7898400cff6SVladimir Oltean 7908400cff6SVladimir Oltean static void sja1105_mac_link_up(struct dsa_switch *ds, int port, 7918400cff6SVladimir Oltean unsigned int mode, 7928400cff6SVladimir Oltean phy_interface_t interface, 7935b502a7bSRussell King struct phy_device *phydev, 7945b502a7bSRussell King int speed, int duplex, 7955b502a7bSRussell King bool tx_pause, bool rx_pause) 7968400cff6SVladimir Oltean { 797ec8582d1SVladimir Oltean struct sja1105_private *priv = ds->priv; 798ec8582d1SVladimir Oltean 799ec8582d1SVladimir Oltean sja1105_adjust_port_config(priv, port, speed); 800ec8582d1SVladimir Oltean 801ec8582d1SVladimir Oltean sja1105_inhibit_tx(priv, BIT(port), false); 8028aa9ebccSVladimir Oltean } 8038aa9ebccSVladimir Oltean 804ad9f299aSVladimir Oltean static void sja1105_phylink_validate(struct dsa_switch *ds, int port, 805ad9f299aSVladimir Oltean unsigned long *supported, 806ad9f299aSVladimir Oltean struct phylink_link_state *state) 807ad9f299aSVladimir Oltean { 808ad9f299aSVladimir Oltean /* Construct a new mask which exhaustively contains all link features 809ad9f299aSVladimir Oltean * supported by the MAC, and then apply that (logical AND) to what will 810ad9f299aSVladimir Oltean * be sent to the PHY for "marketing". 811ad9f299aSVladimir Oltean */ 812ad9f299aSVladimir Oltean __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, }; 813ad9f299aSVladimir Oltean struct sja1105_private *priv = ds->priv; 814ad9f299aSVladimir Oltean struct sja1105_xmii_params_entry *mii; 815ad9f299aSVladimir Oltean 816ad9f299aSVladimir Oltean mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries; 817ad9f299aSVladimir Oltean 81839710229SVladimir Oltean /* include/linux/phylink.h says: 81939710229SVladimir Oltean * When @state->interface is %PHY_INTERFACE_MODE_NA, phylink 82039710229SVladimir Oltean * expects the MAC driver to return all supported link modes. 82139710229SVladimir Oltean */ 82239710229SVladimir Oltean if (state->interface != PHY_INTERFACE_MODE_NA && 82339710229SVladimir Oltean sja1105_phy_mode_mismatch(priv, port, state->interface)) { 82439710229SVladimir Oltean bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS); 82539710229SVladimir Oltean return; 82639710229SVladimir Oltean } 82739710229SVladimir Oltean 828ad9f299aSVladimir Oltean /* The MAC does not support pause frames, and also doesn't 829ad9f299aSVladimir Oltean * support half-duplex traffic modes. 830ad9f299aSVladimir Oltean */ 831ad9f299aSVladimir Oltean phylink_set(mask, Autoneg); 832ad9f299aSVladimir Oltean phylink_set(mask, MII); 833ad9f299aSVladimir Oltean phylink_set(mask, 10baseT_Full); 834ad9f299aSVladimir Oltean phylink_set(mask, 100baseT_Full); 835ca68e138SOleksij Rempel phylink_set(mask, 100baseT1_Full); 836ad9f299aSVladimir Oltean if (mii->xmii_mode[port] == XMII_MODE_RGMII) 837ad9f299aSVladimir Oltean phylink_set(mask, 1000baseT_Full); 838ad9f299aSVladimir Oltean 839ad9f299aSVladimir Oltean bitmap_and(supported, supported, mask, __ETHTOOL_LINK_MODE_MASK_NBITS); 840ad9f299aSVladimir Oltean bitmap_and(state->advertising, state->advertising, mask, 841ad9f299aSVladimir Oltean __ETHTOOL_LINK_MODE_MASK_NBITS); 842ad9f299aSVladimir Oltean } 843ad9f299aSVladimir Oltean 84460f6053fSVladimir Oltean static int 84560f6053fSVladimir Oltean sja1105_find_static_fdb_entry(struct sja1105_private *priv, int port, 84660f6053fSVladimir Oltean const struct sja1105_l2_lookup_entry *requested) 84760f6053fSVladimir Oltean { 84860f6053fSVladimir Oltean struct sja1105_l2_lookup_entry *l2_lookup; 84960f6053fSVladimir Oltean struct sja1105_table *table; 85060f6053fSVladimir Oltean int i; 85160f6053fSVladimir Oltean 85260f6053fSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; 85360f6053fSVladimir Oltean l2_lookup = table->entries; 85460f6053fSVladimir Oltean 85560f6053fSVladimir Oltean for (i = 0; i < table->entry_count; i++) 85660f6053fSVladimir Oltean if (l2_lookup[i].macaddr == requested->macaddr && 85760f6053fSVladimir Oltean l2_lookup[i].vlanid == requested->vlanid && 85860f6053fSVladimir Oltean l2_lookup[i].destports & BIT(port)) 85960f6053fSVladimir Oltean return i; 86060f6053fSVladimir Oltean 86160f6053fSVladimir Oltean return -1; 86260f6053fSVladimir Oltean } 86360f6053fSVladimir Oltean 86460f6053fSVladimir Oltean /* We want FDB entries added statically through the bridge command to persist 86560f6053fSVladimir Oltean * across switch resets, which are a common thing during normal SJA1105 86660f6053fSVladimir Oltean * operation. So we have to back them up in the static configuration tables 86760f6053fSVladimir Oltean * and hence apply them on next static config upload... yay! 86860f6053fSVladimir Oltean */ 86960f6053fSVladimir Oltean static int 87060f6053fSVladimir Oltean sja1105_static_fdb_change(struct sja1105_private *priv, int port, 87160f6053fSVladimir Oltean const struct sja1105_l2_lookup_entry *requested, 87260f6053fSVladimir Oltean bool keep) 87360f6053fSVladimir Oltean { 87460f6053fSVladimir Oltean struct sja1105_l2_lookup_entry *l2_lookup; 87560f6053fSVladimir Oltean struct sja1105_table *table; 87660f6053fSVladimir Oltean int rc, match; 87760f6053fSVladimir Oltean 87860f6053fSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; 87960f6053fSVladimir Oltean 88060f6053fSVladimir Oltean match = sja1105_find_static_fdb_entry(priv, port, requested); 88160f6053fSVladimir Oltean if (match < 0) { 88260f6053fSVladimir Oltean /* Can't delete a missing entry. */ 88360f6053fSVladimir Oltean if (!keep) 88460f6053fSVladimir Oltean return 0; 88560f6053fSVladimir Oltean 88660f6053fSVladimir Oltean /* No match => new entry */ 88760f6053fSVladimir Oltean rc = sja1105_table_resize(table, table->entry_count + 1); 88860f6053fSVladimir Oltean if (rc) 88960f6053fSVladimir Oltean return rc; 89060f6053fSVladimir Oltean 89160f6053fSVladimir Oltean match = table->entry_count - 1; 89260f6053fSVladimir Oltean } 89360f6053fSVladimir Oltean 89460f6053fSVladimir Oltean /* Assign pointer after the resize (it may be new memory) */ 89560f6053fSVladimir Oltean l2_lookup = table->entries; 89660f6053fSVladimir Oltean 89760f6053fSVladimir Oltean /* We have a match. 89860f6053fSVladimir Oltean * If the job was to add this FDB entry, it's already done (mostly 89960f6053fSVladimir Oltean * anyway, since the port forwarding mask may have changed, case in 90060f6053fSVladimir Oltean * which we update it). 90160f6053fSVladimir Oltean * Otherwise we have to delete it. 90260f6053fSVladimir Oltean */ 90360f6053fSVladimir Oltean if (keep) { 90460f6053fSVladimir Oltean l2_lookup[match] = *requested; 90560f6053fSVladimir Oltean return 0; 90660f6053fSVladimir Oltean } 90760f6053fSVladimir Oltean 90860f6053fSVladimir Oltean /* To remove, the strategy is to overwrite the element with 90960f6053fSVladimir Oltean * the last one, and then reduce the array size by 1 91060f6053fSVladimir Oltean */ 91160f6053fSVladimir Oltean l2_lookup[match] = l2_lookup[table->entry_count - 1]; 91260f6053fSVladimir Oltean return sja1105_table_resize(table, table->entry_count - 1); 91360f6053fSVladimir Oltean } 91460f6053fSVladimir Oltean 915291d1e72SVladimir Oltean /* First-generation switches have a 4-way set associative TCAM that 916291d1e72SVladimir Oltean * holds the FDB entries. An FDB index spans from 0 to 1023 and is comprised of 917291d1e72SVladimir Oltean * a "bin" (grouping of 4 entries) and a "way" (an entry within a bin). 918291d1e72SVladimir Oltean * For the placement of a newly learnt FDB entry, the switch selects the bin 919291d1e72SVladimir Oltean * based on a hash function, and the way within that bin incrementally. 920291d1e72SVladimir Oltean */ 92109c1b412SVladimir Oltean static int sja1105et_fdb_index(int bin, int way) 922291d1e72SVladimir Oltean { 923291d1e72SVladimir Oltean return bin * SJA1105ET_FDB_BIN_SIZE + way; 924291d1e72SVladimir Oltean } 925291d1e72SVladimir Oltean 9269dfa6911SVladimir Oltean static int sja1105et_is_fdb_entry_in_bin(struct sja1105_private *priv, int bin, 927291d1e72SVladimir Oltean const u8 *addr, u16 vid, 928291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry *match, 929291d1e72SVladimir Oltean int *last_unused) 930291d1e72SVladimir Oltean { 931291d1e72SVladimir Oltean int way; 932291d1e72SVladimir Oltean 933291d1e72SVladimir Oltean for (way = 0; way < SJA1105ET_FDB_BIN_SIZE; way++) { 934291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 935291d1e72SVladimir Oltean int index = sja1105et_fdb_index(bin, way); 936291d1e72SVladimir Oltean 937291d1e72SVladimir Oltean /* Skip unused entries, optionally marking them 938291d1e72SVladimir Oltean * into the return value 939291d1e72SVladimir Oltean */ 940291d1e72SVladimir Oltean if (sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 941291d1e72SVladimir Oltean index, &l2_lookup)) { 942291d1e72SVladimir Oltean if (last_unused) 943291d1e72SVladimir Oltean *last_unused = way; 944291d1e72SVladimir Oltean continue; 945291d1e72SVladimir Oltean } 946291d1e72SVladimir Oltean 947291d1e72SVladimir Oltean if (l2_lookup.macaddr == ether_addr_to_u64(addr) && 948291d1e72SVladimir Oltean l2_lookup.vlanid == vid) { 949291d1e72SVladimir Oltean if (match) 950291d1e72SVladimir Oltean *match = l2_lookup; 951291d1e72SVladimir Oltean return way; 952291d1e72SVladimir Oltean } 953291d1e72SVladimir Oltean } 954291d1e72SVladimir Oltean /* Return an invalid entry index if not found */ 955291d1e72SVladimir Oltean return -1; 956291d1e72SVladimir Oltean } 957291d1e72SVladimir Oltean 9589dfa6911SVladimir Oltean int sja1105et_fdb_add(struct dsa_switch *ds, int port, 959291d1e72SVladimir Oltean const unsigned char *addr, u16 vid) 960291d1e72SVladimir Oltean { 961291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 962291d1e72SVladimir Oltean struct sja1105_private *priv = ds->priv; 963291d1e72SVladimir Oltean struct device *dev = ds->dev; 964291d1e72SVladimir Oltean int last_unused = -1; 96560f6053fSVladimir Oltean int bin, way, rc; 966291d1e72SVladimir Oltean 9679dfa6911SVladimir Oltean bin = sja1105et_fdb_hash(priv, addr, vid); 968291d1e72SVladimir Oltean 9699dfa6911SVladimir Oltean way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid, 970291d1e72SVladimir Oltean &l2_lookup, &last_unused); 971291d1e72SVladimir Oltean if (way >= 0) { 972291d1e72SVladimir Oltean /* We have an FDB entry. Is our port in the destination 973291d1e72SVladimir Oltean * mask? If yes, we need to do nothing. If not, we need 974291d1e72SVladimir Oltean * to rewrite the entry by adding this port to it. 975291d1e72SVladimir Oltean */ 976291d1e72SVladimir Oltean if (l2_lookup.destports & BIT(port)) 977291d1e72SVladimir Oltean return 0; 978291d1e72SVladimir Oltean l2_lookup.destports |= BIT(port); 979291d1e72SVladimir Oltean } else { 980291d1e72SVladimir Oltean int index = sja1105et_fdb_index(bin, way); 981291d1e72SVladimir Oltean 982291d1e72SVladimir Oltean /* We don't have an FDB entry. We construct a new one and 983291d1e72SVladimir Oltean * try to find a place for it within the FDB table. 984291d1e72SVladimir Oltean */ 985291d1e72SVladimir Oltean l2_lookup.macaddr = ether_addr_to_u64(addr); 986291d1e72SVladimir Oltean l2_lookup.destports = BIT(port); 987291d1e72SVladimir Oltean l2_lookup.vlanid = vid; 988291d1e72SVladimir Oltean 989291d1e72SVladimir Oltean if (last_unused >= 0) { 990291d1e72SVladimir Oltean way = last_unused; 991291d1e72SVladimir Oltean } else { 992291d1e72SVladimir Oltean /* Bin is full, need to evict somebody. 993291d1e72SVladimir Oltean * Choose victim at random. If you get these messages 994291d1e72SVladimir Oltean * often, you may need to consider changing the 995291d1e72SVladimir Oltean * distribution function: 996291d1e72SVladimir Oltean * static_config[BLK_IDX_L2_LOOKUP_PARAMS].entries->poly 997291d1e72SVladimir Oltean */ 998291d1e72SVladimir Oltean get_random_bytes(&way, sizeof(u8)); 999291d1e72SVladimir Oltean way %= SJA1105ET_FDB_BIN_SIZE; 1000291d1e72SVladimir Oltean dev_warn(dev, "Warning, FDB bin %d full while adding entry for %pM. Evicting entry %u.\n", 1001291d1e72SVladimir Oltean bin, addr, way); 1002291d1e72SVladimir Oltean /* Evict entry */ 1003291d1e72SVladimir Oltean sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 1004291d1e72SVladimir Oltean index, NULL, false); 1005291d1e72SVladimir Oltean } 1006291d1e72SVladimir Oltean } 1007291d1e72SVladimir Oltean l2_lookup.index = sja1105et_fdb_index(bin, way); 1008291d1e72SVladimir Oltean 100960f6053fSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 1010291d1e72SVladimir Oltean l2_lookup.index, &l2_lookup, 1011291d1e72SVladimir Oltean true); 101260f6053fSVladimir Oltean if (rc < 0) 101360f6053fSVladimir Oltean return rc; 101460f6053fSVladimir Oltean 101560f6053fSVladimir Oltean return sja1105_static_fdb_change(priv, port, &l2_lookup, true); 1016291d1e72SVladimir Oltean } 1017291d1e72SVladimir Oltean 10189dfa6911SVladimir Oltean int sja1105et_fdb_del(struct dsa_switch *ds, int port, 1019291d1e72SVladimir Oltean const unsigned char *addr, u16 vid) 1020291d1e72SVladimir Oltean { 1021291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 1022291d1e72SVladimir Oltean struct sja1105_private *priv = ds->priv; 102360f6053fSVladimir Oltean int index, bin, way, rc; 1024291d1e72SVladimir Oltean bool keep; 1025291d1e72SVladimir Oltean 10269dfa6911SVladimir Oltean bin = sja1105et_fdb_hash(priv, addr, vid); 10279dfa6911SVladimir Oltean way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid, 1028291d1e72SVladimir Oltean &l2_lookup, NULL); 1029291d1e72SVladimir Oltean if (way < 0) 1030291d1e72SVladimir Oltean return 0; 1031291d1e72SVladimir Oltean index = sja1105et_fdb_index(bin, way); 1032291d1e72SVladimir Oltean 1033291d1e72SVladimir Oltean /* We have an FDB entry. Is our port in the destination mask? If yes, 1034291d1e72SVladimir Oltean * we need to remove it. If the resulting port mask becomes empty, we 1035291d1e72SVladimir Oltean * need to completely evict the FDB entry. 1036291d1e72SVladimir Oltean * Otherwise we just write it back. 1037291d1e72SVladimir Oltean */ 1038291d1e72SVladimir Oltean l2_lookup.destports &= ~BIT(port); 10397752e937SVladimir Oltean 1040291d1e72SVladimir Oltean if (l2_lookup.destports) 1041291d1e72SVladimir Oltean keep = true; 1042291d1e72SVladimir Oltean else 1043291d1e72SVladimir Oltean keep = false; 1044291d1e72SVladimir Oltean 104560f6053fSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 1046291d1e72SVladimir Oltean index, &l2_lookup, keep); 104760f6053fSVladimir Oltean if (rc < 0) 104860f6053fSVladimir Oltean return rc; 104960f6053fSVladimir Oltean 105060f6053fSVladimir Oltean return sja1105_static_fdb_change(priv, port, &l2_lookup, keep); 1051291d1e72SVladimir Oltean } 1052291d1e72SVladimir Oltean 10539dfa6911SVladimir Oltean int sja1105pqrs_fdb_add(struct dsa_switch *ds, int port, 10549dfa6911SVladimir Oltean const unsigned char *addr, u16 vid) 10559dfa6911SVladimir Oltean { 10561da73821SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 10571da73821SVladimir Oltean struct sja1105_private *priv = ds->priv; 10581da73821SVladimir Oltean int rc, i; 10591da73821SVladimir Oltean 10601da73821SVladimir Oltean /* Search for an existing entry in the FDB table */ 10611da73821SVladimir Oltean l2_lookup.macaddr = ether_addr_to_u64(addr); 10621da73821SVladimir Oltean l2_lookup.vlanid = vid; 10631da73821SVladimir Oltean l2_lookup.iotag = SJA1105_S_TAG; 10641da73821SVladimir Oltean l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0); 106568bb8ea8SVivien Didelot if (dsa_port_is_vlan_filtering(dsa_to_port(ds, port))) { 10661da73821SVladimir Oltean l2_lookup.mask_vlanid = VLAN_VID_MASK; 10671da73821SVladimir Oltean l2_lookup.mask_iotag = BIT(0); 10686d7c7d94SVladimir Oltean } else { 10696d7c7d94SVladimir Oltean l2_lookup.mask_vlanid = 0; 10706d7c7d94SVladimir Oltean l2_lookup.mask_iotag = 0; 10716d7c7d94SVladimir Oltean } 10721da73821SVladimir Oltean l2_lookup.destports = BIT(port); 10731da73821SVladimir Oltean 10741da73821SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 10751da73821SVladimir Oltean SJA1105_SEARCH, &l2_lookup); 10761da73821SVladimir Oltean if (rc == 0) { 10771da73821SVladimir Oltean /* Found and this port is already in the entry's 10781da73821SVladimir Oltean * port mask => job done 10791da73821SVladimir Oltean */ 10801da73821SVladimir Oltean if (l2_lookup.destports & BIT(port)) 10811da73821SVladimir Oltean return 0; 10821da73821SVladimir Oltean /* l2_lookup.index is populated by the switch in case it 10831da73821SVladimir Oltean * found something. 10841da73821SVladimir Oltean */ 10851da73821SVladimir Oltean l2_lookup.destports |= BIT(port); 10861da73821SVladimir Oltean goto skip_finding_an_index; 10871da73821SVladimir Oltean } 10881da73821SVladimir Oltean 10891da73821SVladimir Oltean /* Not found, so try to find an unused spot in the FDB. 10901da73821SVladimir Oltean * This is slightly inefficient because the strategy is knock-knock at 10911da73821SVladimir Oltean * every possible position from 0 to 1023. 10921da73821SVladimir Oltean */ 10931da73821SVladimir Oltean for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) { 10941da73821SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 10951da73821SVladimir Oltean i, NULL); 10961da73821SVladimir Oltean if (rc < 0) 10971da73821SVladimir Oltean break; 10981da73821SVladimir Oltean } 10991da73821SVladimir Oltean if (i == SJA1105_MAX_L2_LOOKUP_COUNT) { 11001da73821SVladimir Oltean dev_err(ds->dev, "FDB is full, cannot add entry.\n"); 11011da73821SVladimir Oltean return -EINVAL; 11021da73821SVladimir Oltean } 110317ae6555SVladimir Oltean l2_lookup.lockeds = true; 11041da73821SVladimir Oltean l2_lookup.index = i; 11051da73821SVladimir Oltean 11061da73821SVladimir Oltean skip_finding_an_index: 110760f6053fSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 11081da73821SVladimir Oltean l2_lookup.index, &l2_lookup, 11091da73821SVladimir Oltean true); 111060f6053fSVladimir Oltean if (rc < 0) 111160f6053fSVladimir Oltean return rc; 111260f6053fSVladimir Oltean 111360f6053fSVladimir Oltean return sja1105_static_fdb_change(priv, port, &l2_lookup, true); 11149dfa6911SVladimir Oltean } 11159dfa6911SVladimir Oltean 11169dfa6911SVladimir Oltean int sja1105pqrs_fdb_del(struct dsa_switch *ds, int port, 11179dfa6911SVladimir Oltean const unsigned char *addr, u16 vid) 11189dfa6911SVladimir Oltean { 11191da73821SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 11201da73821SVladimir Oltean struct sja1105_private *priv = ds->priv; 11211da73821SVladimir Oltean bool keep; 11221da73821SVladimir Oltean int rc; 11231da73821SVladimir Oltean 11241da73821SVladimir Oltean l2_lookup.macaddr = ether_addr_to_u64(addr); 11251da73821SVladimir Oltean l2_lookup.vlanid = vid; 11261da73821SVladimir Oltean l2_lookup.iotag = SJA1105_S_TAG; 11271da73821SVladimir Oltean l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0); 112868bb8ea8SVivien Didelot if (dsa_port_is_vlan_filtering(dsa_to_port(ds, port))) { 11291da73821SVladimir Oltean l2_lookup.mask_vlanid = VLAN_VID_MASK; 11301da73821SVladimir Oltean l2_lookup.mask_iotag = BIT(0); 11316d7c7d94SVladimir Oltean } else { 11326d7c7d94SVladimir Oltean l2_lookup.mask_vlanid = 0; 11336d7c7d94SVladimir Oltean l2_lookup.mask_iotag = 0; 11346d7c7d94SVladimir Oltean } 11351da73821SVladimir Oltean l2_lookup.destports = BIT(port); 11361da73821SVladimir Oltean 11371da73821SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 11381da73821SVladimir Oltean SJA1105_SEARCH, &l2_lookup); 11391da73821SVladimir Oltean if (rc < 0) 11401da73821SVladimir Oltean return 0; 11411da73821SVladimir Oltean 11421da73821SVladimir Oltean l2_lookup.destports &= ~BIT(port); 11431da73821SVladimir Oltean 11441da73821SVladimir Oltean /* Decide whether we remove just this port from the FDB entry, 11451da73821SVladimir Oltean * or if we remove it completely. 11461da73821SVladimir Oltean */ 11471da73821SVladimir Oltean if (l2_lookup.destports) 11481da73821SVladimir Oltean keep = true; 11491da73821SVladimir Oltean else 11501da73821SVladimir Oltean keep = false; 11511da73821SVladimir Oltean 115260f6053fSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 11531da73821SVladimir Oltean l2_lookup.index, &l2_lookup, keep); 115460f6053fSVladimir Oltean if (rc < 0) 115560f6053fSVladimir Oltean return rc; 115660f6053fSVladimir Oltean 115760f6053fSVladimir Oltean return sja1105_static_fdb_change(priv, port, &l2_lookup, keep); 11589dfa6911SVladimir Oltean } 11599dfa6911SVladimir Oltean 11609dfa6911SVladimir Oltean static int sja1105_fdb_add(struct dsa_switch *ds, int port, 11619dfa6911SVladimir Oltean const unsigned char *addr, u16 vid) 11629dfa6911SVladimir Oltean { 11639dfa6911SVladimir Oltean struct sja1105_private *priv = ds->priv; 1164b3ee526aSVladimir Oltean 11656d7c7d94SVladimir Oltean /* dsa_8021q is in effect when the bridge's vlan_filtering isn't, 11666d7c7d94SVladimir Oltean * so the switch still does some VLAN processing internally. 11676d7c7d94SVladimir Oltean * But Shared VLAN Learning (SVL) is also active, and it will take 11686d7c7d94SVladimir Oltean * care of autonomous forwarding between the unique pvid's of each 11696d7c7d94SVladimir Oltean * port. Here we just make sure that users can't add duplicate FDB 11706d7c7d94SVladimir Oltean * entries when in this mode - the actual VID doesn't matter except 11716d7c7d94SVladimir Oltean * for what gets printed in 'bridge fdb show'. In the case of zero, 11726d7c7d94SVladimir Oltean * no VID gets printed at all. 117393647594SVladimir Oltean */ 117468bb8ea8SVivien Didelot if (!dsa_port_is_vlan_filtering(dsa_to_port(ds, port))) 11756d7c7d94SVladimir Oltean vid = 0; 117693647594SVladimir Oltean 11776d7c7d94SVladimir Oltean return priv->info->fdb_add_cmd(ds, port, addr, vid); 11789dfa6911SVladimir Oltean } 11799dfa6911SVladimir Oltean 11809dfa6911SVladimir Oltean static int sja1105_fdb_del(struct dsa_switch *ds, int port, 11819dfa6911SVladimir Oltean const unsigned char *addr, u16 vid) 11829dfa6911SVladimir Oltean { 11839dfa6911SVladimir Oltean struct sja1105_private *priv = ds->priv; 11849dfa6911SVladimir Oltean 118568bb8ea8SVivien Didelot if (!dsa_port_is_vlan_filtering(dsa_to_port(ds, port))) 11866d7c7d94SVladimir Oltean vid = 0; 11876d7c7d94SVladimir Oltean 1188b3ee526aSVladimir Oltean return priv->info->fdb_del_cmd(ds, port, addr, vid); 11899dfa6911SVladimir Oltean } 11909dfa6911SVladimir Oltean 1191291d1e72SVladimir Oltean static int sja1105_fdb_dump(struct dsa_switch *ds, int port, 1192291d1e72SVladimir Oltean dsa_fdb_dump_cb_t *cb, void *data) 1193291d1e72SVladimir Oltean { 1194291d1e72SVladimir Oltean struct sja1105_private *priv = ds->priv; 1195291d1e72SVladimir Oltean struct device *dev = ds->dev; 1196291d1e72SVladimir Oltean int i; 1197291d1e72SVladimir Oltean 1198291d1e72SVladimir Oltean for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) { 1199291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 1200291d1e72SVladimir Oltean u8 macaddr[ETH_ALEN]; 1201291d1e72SVladimir Oltean int rc; 1202291d1e72SVladimir Oltean 1203291d1e72SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 1204291d1e72SVladimir Oltean i, &l2_lookup); 1205291d1e72SVladimir Oltean /* No fdb entry at i, not an issue */ 1206def84604SVladimir Oltean if (rc == -ENOENT) 1207291d1e72SVladimir Oltean continue; 1208291d1e72SVladimir Oltean if (rc) { 1209291d1e72SVladimir Oltean dev_err(dev, "Failed to dump FDB: %d\n", rc); 1210291d1e72SVladimir Oltean return rc; 1211291d1e72SVladimir Oltean } 1212291d1e72SVladimir Oltean 1213291d1e72SVladimir Oltean /* FDB dump callback is per port. This means we have to 1214291d1e72SVladimir Oltean * disregard a valid entry if it's not for this port, even if 1215291d1e72SVladimir Oltean * only to revisit it later. This is inefficient because the 1216291d1e72SVladimir Oltean * 1024-sized FDB table needs to be traversed 4 times through 1217291d1e72SVladimir Oltean * SPI during a 'bridge fdb show' command. 1218291d1e72SVladimir Oltean */ 1219291d1e72SVladimir Oltean if (!(l2_lookup.destports & BIT(port))) 1220291d1e72SVladimir Oltean continue; 1221291d1e72SVladimir Oltean u64_to_ether_addr(l2_lookup.macaddr, macaddr); 122293647594SVladimir Oltean 12236d7c7d94SVladimir Oltean /* We need to hide the dsa_8021q VLANs from the user. */ 122468bb8ea8SVivien Didelot if (!dsa_port_is_vlan_filtering(dsa_to_port(ds, port))) 12256d7c7d94SVladimir Oltean l2_lookup.vlanid = 0; 122617ae6555SVladimir Oltean cb(macaddr, l2_lookup.vlanid, l2_lookup.lockeds, data); 1227291d1e72SVladimir Oltean } 1228291d1e72SVladimir Oltean return 0; 1229291d1e72SVladimir Oltean } 1230291d1e72SVladimir Oltean 1231291d1e72SVladimir Oltean /* This callback needs to be present */ 1232291d1e72SVladimir Oltean static int sja1105_mdb_prepare(struct dsa_switch *ds, int port, 1233291d1e72SVladimir Oltean const struct switchdev_obj_port_mdb *mdb) 1234291d1e72SVladimir Oltean { 1235291d1e72SVladimir Oltean return 0; 1236291d1e72SVladimir Oltean } 1237291d1e72SVladimir Oltean 1238291d1e72SVladimir Oltean static void sja1105_mdb_add(struct dsa_switch *ds, int port, 1239291d1e72SVladimir Oltean const struct switchdev_obj_port_mdb *mdb) 1240291d1e72SVladimir Oltean { 1241291d1e72SVladimir Oltean sja1105_fdb_add(ds, port, mdb->addr, mdb->vid); 1242291d1e72SVladimir Oltean } 1243291d1e72SVladimir Oltean 1244291d1e72SVladimir Oltean static int sja1105_mdb_del(struct dsa_switch *ds, int port, 1245291d1e72SVladimir Oltean const struct switchdev_obj_port_mdb *mdb) 1246291d1e72SVladimir Oltean { 1247291d1e72SVladimir Oltean return sja1105_fdb_del(ds, port, mdb->addr, mdb->vid); 1248291d1e72SVladimir Oltean } 1249291d1e72SVladimir Oltean 12508aa9ebccSVladimir Oltean static int sja1105_bridge_member(struct dsa_switch *ds, int port, 12518aa9ebccSVladimir Oltean struct net_device *br, bool member) 12528aa9ebccSVladimir Oltean { 12538aa9ebccSVladimir Oltean struct sja1105_l2_forwarding_entry *l2_fwd; 12548aa9ebccSVladimir Oltean struct sja1105_private *priv = ds->priv; 12558aa9ebccSVladimir Oltean int i, rc; 12568aa9ebccSVladimir Oltean 12578aa9ebccSVladimir Oltean l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries; 12588aa9ebccSVladimir Oltean 12598aa9ebccSVladimir Oltean for (i = 0; i < SJA1105_NUM_PORTS; i++) { 12608aa9ebccSVladimir Oltean /* Add this port to the forwarding matrix of the 12618aa9ebccSVladimir Oltean * other ports in the same bridge, and viceversa. 12628aa9ebccSVladimir Oltean */ 12638aa9ebccSVladimir Oltean if (!dsa_is_user_port(ds, i)) 12648aa9ebccSVladimir Oltean continue; 12658aa9ebccSVladimir Oltean /* For the ports already under the bridge, only one thing needs 12668aa9ebccSVladimir Oltean * to be done, and that is to add this port to their 12678aa9ebccSVladimir Oltean * reachability domain. So we can perform the SPI write for 12688aa9ebccSVladimir Oltean * them immediately. However, for this port itself (the one 12698aa9ebccSVladimir Oltean * that is new to the bridge), we need to add all other ports 12708aa9ebccSVladimir Oltean * to its reachability domain. So we do that incrementally in 12718aa9ebccSVladimir Oltean * this loop, and perform the SPI write only at the end, once 12728aa9ebccSVladimir Oltean * the domain contains all other bridge ports. 12738aa9ebccSVladimir Oltean */ 12748aa9ebccSVladimir Oltean if (i == port) 12758aa9ebccSVladimir Oltean continue; 12768aa9ebccSVladimir Oltean if (dsa_to_port(ds, i)->bridge_dev != br) 12778aa9ebccSVladimir Oltean continue; 12788aa9ebccSVladimir Oltean sja1105_port_allow_traffic(l2_fwd, i, port, member); 12798aa9ebccSVladimir Oltean sja1105_port_allow_traffic(l2_fwd, port, i, member); 12808aa9ebccSVladimir Oltean 12818aa9ebccSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING, 12828aa9ebccSVladimir Oltean i, &l2_fwd[i], true); 12838aa9ebccSVladimir Oltean if (rc < 0) 12848aa9ebccSVladimir Oltean return rc; 12858aa9ebccSVladimir Oltean } 12868aa9ebccSVladimir Oltean 12878aa9ebccSVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING, 12888aa9ebccSVladimir Oltean port, &l2_fwd[port], true); 12898aa9ebccSVladimir Oltean } 12908aa9ebccSVladimir Oltean 1291640f763fSVladimir Oltean static void sja1105_bridge_stp_state_set(struct dsa_switch *ds, int port, 1292640f763fSVladimir Oltean u8 state) 1293640f763fSVladimir Oltean { 1294640f763fSVladimir Oltean struct sja1105_private *priv = ds->priv; 1295640f763fSVladimir Oltean struct sja1105_mac_config_entry *mac; 1296640f763fSVladimir Oltean 1297640f763fSVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 1298640f763fSVladimir Oltean 1299640f763fSVladimir Oltean switch (state) { 1300640f763fSVladimir Oltean case BR_STATE_DISABLED: 1301640f763fSVladimir Oltean case BR_STATE_BLOCKING: 1302640f763fSVladimir Oltean /* From UM10944 description of DRPDTAG (why put this there?): 1303640f763fSVladimir Oltean * "Management traffic flows to the port regardless of the state 1304640f763fSVladimir Oltean * of the INGRESS flag". So BPDUs are still be allowed to pass. 1305640f763fSVladimir Oltean * At the moment no difference between DISABLED and BLOCKING. 1306640f763fSVladimir Oltean */ 1307640f763fSVladimir Oltean mac[port].ingress = false; 1308640f763fSVladimir Oltean mac[port].egress = false; 1309640f763fSVladimir Oltean mac[port].dyn_learn = false; 1310640f763fSVladimir Oltean break; 1311640f763fSVladimir Oltean case BR_STATE_LISTENING: 1312640f763fSVladimir Oltean mac[port].ingress = true; 1313640f763fSVladimir Oltean mac[port].egress = false; 1314640f763fSVladimir Oltean mac[port].dyn_learn = false; 1315640f763fSVladimir Oltean break; 1316640f763fSVladimir Oltean case BR_STATE_LEARNING: 1317640f763fSVladimir Oltean mac[port].ingress = true; 1318640f763fSVladimir Oltean mac[port].egress = false; 1319640f763fSVladimir Oltean mac[port].dyn_learn = true; 1320640f763fSVladimir Oltean break; 1321640f763fSVladimir Oltean case BR_STATE_FORWARDING: 1322640f763fSVladimir Oltean mac[port].ingress = true; 1323640f763fSVladimir Oltean mac[port].egress = true; 1324640f763fSVladimir Oltean mac[port].dyn_learn = true; 1325640f763fSVladimir Oltean break; 1326640f763fSVladimir Oltean default: 1327640f763fSVladimir Oltean dev_err(ds->dev, "invalid STP state: %d\n", state); 1328640f763fSVladimir Oltean return; 1329640f763fSVladimir Oltean } 1330640f763fSVladimir Oltean 1331640f763fSVladimir Oltean sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, 1332640f763fSVladimir Oltean &mac[port], true); 1333640f763fSVladimir Oltean } 1334640f763fSVladimir Oltean 13358aa9ebccSVladimir Oltean static int sja1105_bridge_join(struct dsa_switch *ds, int port, 13368aa9ebccSVladimir Oltean struct net_device *br) 13378aa9ebccSVladimir Oltean { 13388aa9ebccSVladimir Oltean return sja1105_bridge_member(ds, port, br, true); 13398aa9ebccSVladimir Oltean } 13408aa9ebccSVladimir Oltean 13418aa9ebccSVladimir Oltean static void sja1105_bridge_leave(struct dsa_switch *ds, int port, 13428aa9ebccSVladimir Oltean struct net_device *br) 13438aa9ebccSVladimir Oltean { 13448aa9ebccSVladimir Oltean sja1105_bridge_member(ds, port, br, false); 13458aa9ebccSVladimir Oltean } 13468aa9ebccSVladimir Oltean 13472eea1fa8SVladimir Oltean static const char * const sja1105_reset_reasons[] = { 13482eea1fa8SVladimir Oltean [SJA1105_VLAN_FILTERING] = "VLAN filtering", 13492eea1fa8SVladimir Oltean [SJA1105_RX_HWTSTAMPING] = "RX timestamping", 13502eea1fa8SVladimir Oltean [SJA1105_AGEING_TIME] = "Ageing time", 13512eea1fa8SVladimir Oltean [SJA1105_SCHEDULING] = "Time-aware scheduling", 13522eea1fa8SVladimir Oltean }; 13532eea1fa8SVladimir Oltean 13546666cebcSVladimir Oltean /* For situations where we need to change a setting at runtime that is only 13556666cebcSVladimir Oltean * available through the static configuration, resetting the switch in order 13566666cebcSVladimir Oltean * to upload the new static config is unavoidable. Back up the settings we 13576666cebcSVladimir Oltean * modify at runtime (currently only MAC) and restore them after uploading, 13586666cebcSVladimir Oltean * such that this operation is relatively seamless. 13596666cebcSVladimir Oltean */ 13602eea1fa8SVladimir Oltean int sja1105_static_config_reload(struct sja1105_private *priv, 13612eea1fa8SVladimir Oltean enum sja1105_reset_reason reason) 13626666cebcSVladimir Oltean { 13636cf99c13SVladimir Oltean struct ptp_system_timestamp ptp_sts_before; 13646cf99c13SVladimir Oltean struct ptp_system_timestamp ptp_sts_after; 13656666cebcSVladimir Oltean struct sja1105_mac_config_entry *mac; 13666666cebcSVladimir Oltean int speed_mbps[SJA1105_NUM_PORTS]; 13676cf99c13SVladimir Oltean struct dsa_switch *ds = priv->ds; 13686cf99c13SVladimir Oltean s64 t1, t2, t3, t4; 13696cf99c13SVladimir Oltean s64 t12, t34; 13706666cebcSVladimir Oltean int rc, i; 13716cf99c13SVladimir Oltean s64 now; 13726666cebcSVladimir Oltean 1373af580ae2SVladimir Oltean mutex_lock(&priv->mgmt_lock); 1374af580ae2SVladimir Oltean 13756666cebcSVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 13766666cebcSVladimir Oltean 13778400cff6SVladimir Oltean /* Back up the dynamic link speed changed by sja1105_adjust_port_config 13788400cff6SVladimir Oltean * in order to temporarily restore it to SJA1105_SPEED_AUTO - which the 13798400cff6SVladimir Oltean * switch wants to see in the static config in order to allow us to 13808400cff6SVladimir Oltean * change it through the dynamic interface later. 13816666cebcSVladimir Oltean */ 13826666cebcSVladimir Oltean for (i = 0; i < SJA1105_NUM_PORTS; i++) { 13836666cebcSVladimir Oltean speed_mbps[i] = sja1105_speed[mac[i].speed]; 13846666cebcSVladimir Oltean mac[i].speed = SJA1105_SPEED_AUTO; 13856666cebcSVladimir Oltean } 13866666cebcSVladimir Oltean 13876cf99c13SVladimir Oltean /* No PTP operations can run right now */ 13886cf99c13SVladimir Oltean mutex_lock(&priv->ptp_data.lock); 13896cf99c13SVladimir Oltean 13906cf99c13SVladimir Oltean rc = __sja1105_ptp_gettimex(ds, &now, &ptp_sts_before); 13916cf99c13SVladimir Oltean if (rc < 0) 13926cf99c13SVladimir Oltean goto out_unlock_ptp; 13936cf99c13SVladimir Oltean 13946666cebcSVladimir Oltean /* Reset switch and send updated static configuration */ 13956666cebcSVladimir Oltean rc = sja1105_static_config_upload(priv); 13966666cebcSVladimir Oltean if (rc < 0) 13976cf99c13SVladimir Oltean goto out_unlock_ptp; 13986cf99c13SVladimir Oltean 13996cf99c13SVladimir Oltean rc = __sja1105_ptp_settime(ds, 0, &ptp_sts_after); 14006cf99c13SVladimir Oltean if (rc < 0) 14016cf99c13SVladimir Oltean goto out_unlock_ptp; 14026cf99c13SVladimir Oltean 14036cf99c13SVladimir Oltean t1 = timespec64_to_ns(&ptp_sts_before.pre_ts); 14046cf99c13SVladimir Oltean t2 = timespec64_to_ns(&ptp_sts_before.post_ts); 14056cf99c13SVladimir Oltean t3 = timespec64_to_ns(&ptp_sts_after.pre_ts); 14066cf99c13SVladimir Oltean t4 = timespec64_to_ns(&ptp_sts_after.post_ts); 14076cf99c13SVladimir Oltean /* Mid point, corresponds to pre-reset PTPCLKVAL */ 14086cf99c13SVladimir Oltean t12 = t1 + (t2 - t1) / 2; 14096cf99c13SVladimir Oltean /* Mid point, corresponds to post-reset PTPCLKVAL, aka 0 */ 14106cf99c13SVladimir Oltean t34 = t3 + (t4 - t3) / 2; 14116cf99c13SVladimir Oltean /* Advance PTPCLKVAL by the time it took since its readout */ 14126cf99c13SVladimir Oltean now += (t34 - t12); 14136cf99c13SVladimir Oltean 14146cf99c13SVladimir Oltean __sja1105_ptp_adjtime(ds, now); 14156cf99c13SVladimir Oltean 14166cf99c13SVladimir Oltean out_unlock_ptp: 14176cf99c13SVladimir Oltean mutex_unlock(&priv->ptp_data.lock); 14186666cebcSVladimir Oltean 14192eea1fa8SVladimir Oltean dev_info(priv->ds->dev, 14202eea1fa8SVladimir Oltean "Reset switch and programmed static config. Reason: %s\n", 14212eea1fa8SVladimir Oltean sja1105_reset_reasons[reason]); 14222eea1fa8SVladimir Oltean 14236666cebcSVladimir Oltean /* Configure the CGU (PLLs) for MII and RMII PHYs. 14246666cebcSVladimir Oltean * For these interfaces there is no dynamic configuration 14256666cebcSVladimir Oltean * needed, since PLLs have same settings at all speeds. 14266666cebcSVladimir Oltean */ 14276666cebcSVladimir Oltean rc = sja1105_clocking_setup(priv); 14286666cebcSVladimir Oltean if (rc < 0) 14296666cebcSVladimir Oltean goto out; 14306666cebcSVladimir Oltean 14316666cebcSVladimir Oltean for (i = 0; i < SJA1105_NUM_PORTS; i++) { 14328400cff6SVladimir Oltean rc = sja1105_adjust_port_config(priv, i, speed_mbps[i]); 14336666cebcSVladimir Oltean if (rc < 0) 14346666cebcSVladimir Oltean goto out; 14356666cebcSVladimir Oltean } 14366666cebcSVladimir Oltean out: 1437af580ae2SVladimir Oltean mutex_unlock(&priv->mgmt_lock); 1438af580ae2SVladimir Oltean 14396666cebcSVladimir Oltean return rc; 14406666cebcSVladimir Oltean } 14416666cebcSVladimir Oltean 14426666cebcSVladimir Oltean static int sja1105_pvid_apply(struct sja1105_private *priv, int port, u16 pvid) 14436666cebcSVladimir Oltean { 14446666cebcSVladimir Oltean struct sja1105_mac_config_entry *mac; 14456666cebcSVladimir Oltean 14466666cebcSVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 14476666cebcSVladimir Oltean 14486666cebcSVladimir Oltean mac[port].vlanid = pvid; 14496666cebcSVladimir Oltean 14506666cebcSVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, 14516666cebcSVladimir Oltean &mac[port], true); 14526666cebcSVladimir Oltean } 14536666cebcSVladimir Oltean 14546666cebcSVladimir Oltean static int sja1105_is_vlan_configured(struct sja1105_private *priv, u16 vid) 14556666cebcSVladimir Oltean { 14566666cebcSVladimir Oltean struct sja1105_vlan_lookup_entry *vlan; 14576666cebcSVladimir Oltean int count, i; 14586666cebcSVladimir Oltean 14596666cebcSVladimir Oltean vlan = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entries; 14606666cebcSVladimir Oltean count = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entry_count; 14616666cebcSVladimir Oltean 14626666cebcSVladimir Oltean for (i = 0; i < count; i++) 14636666cebcSVladimir Oltean if (vlan[i].vlanid == vid) 14646666cebcSVladimir Oltean return i; 14656666cebcSVladimir Oltean 14666666cebcSVladimir Oltean /* Return an invalid entry index if not found */ 14676666cebcSVladimir Oltean return -1; 14686666cebcSVladimir Oltean } 14696666cebcSVladimir Oltean 14706666cebcSVladimir Oltean static int sja1105_vlan_apply(struct sja1105_private *priv, int port, u16 vid, 14716666cebcSVladimir Oltean bool enabled, bool untagged) 14726666cebcSVladimir Oltean { 14736666cebcSVladimir Oltean struct sja1105_vlan_lookup_entry *vlan; 14746666cebcSVladimir Oltean struct sja1105_table *table; 14756666cebcSVladimir Oltean bool keep = true; 14766666cebcSVladimir Oltean int match, rc; 14776666cebcSVladimir Oltean 14786666cebcSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP]; 14796666cebcSVladimir Oltean 14806666cebcSVladimir Oltean match = sja1105_is_vlan_configured(priv, vid); 14816666cebcSVladimir Oltean if (match < 0) { 14826666cebcSVladimir Oltean /* Can't delete a missing entry. */ 14836666cebcSVladimir Oltean if (!enabled) 14846666cebcSVladimir Oltean return 0; 14856666cebcSVladimir Oltean rc = sja1105_table_resize(table, table->entry_count + 1); 14866666cebcSVladimir Oltean if (rc) 14876666cebcSVladimir Oltean return rc; 14886666cebcSVladimir Oltean match = table->entry_count - 1; 14896666cebcSVladimir Oltean } 14906666cebcSVladimir Oltean /* Assign pointer after the resize (it's new memory) */ 14916666cebcSVladimir Oltean vlan = table->entries; 14926666cebcSVladimir Oltean vlan[match].vlanid = vid; 14936666cebcSVladimir Oltean if (enabled) { 14946666cebcSVladimir Oltean vlan[match].vlan_bc |= BIT(port); 14956666cebcSVladimir Oltean vlan[match].vmemb_port |= BIT(port); 14966666cebcSVladimir Oltean } else { 14976666cebcSVladimir Oltean vlan[match].vlan_bc &= ~BIT(port); 14986666cebcSVladimir Oltean vlan[match].vmemb_port &= ~BIT(port); 14996666cebcSVladimir Oltean } 15006666cebcSVladimir Oltean /* Also unset tag_port if removing this VLAN was requested, 15016666cebcSVladimir Oltean * just so we don't have a confusing bitmap (no practical purpose). 15026666cebcSVladimir Oltean */ 15036666cebcSVladimir Oltean if (untagged || !enabled) 15046666cebcSVladimir Oltean vlan[match].tag_port &= ~BIT(port); 15056666cebcSVladimir Oltean else 15066666cebcSVladimir Oltean vlan[match].tag_port |= BIT(port); 15076666cebcSVladimir Oltean /* If there's no port left as member of this VLAN, 15086666cebcSVladimir Oltean * it's time for it to go. 15096666cebcSVladimir Oltean */ 15106666cebcSVladimir Oltean if (!vlan[match].vmemb_port) 15116666cebcSVladimir Oltean keep = false; 15126666cebcSVladimir Oltean 15136666cebcSVladimir Oltean dev_dbg(priv->ds->dev, 15146666cebcSVladimir Oltean "%s: port %d, vid %llu, broadcast domain 0x%llx, " 15156666cebcSVladimir Oltean "port members 0x%llx, tagged ports 0x%llx, keep %d\n", 15166666cebcSVladimir Oltean __func__, port, vlan[match].vlanid, vlan[match].vlan_bc, 15176666cebcSVladimir Oltean vlan[match].vmemb_port, vlan[match].tag_port, keep); 15186666cebcSVladimir Oltean 15196666cebcSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_VLAN_LOOKUP, vid, 15206666cebcSVladimir Oltean &vlan[match], keep); 15216666cebcSVladimir Oltean if (rc < 0) 15226666cebcSVladimir Oltean return rc; 15236666cebcSVladimir Oltean 15246666cebcSVladimir Oltean if (!keep) 15256666cebcSVladimir Oltean return sja1105_table_delete_entry(table, match); 15266666cebcSVladimir Oltean 15276666cebcSVladimir Oltean return 0; 15286666cebcSVladimir Oltean } 15296666cebcSVladimir Oltean 1530227d07a0SVladimir Oltean static int sja1105_setup_8021q_tagging(struct dsa_switch *ds, bool enabled) 1531227d07a0SVladimir Oltean { 1532227d07a0SVladimir Oltean int rc, i; 1533227d07a0SVladimir Oltean 1534227d07a0SVladimir Oltean for (i = 0; i < SJA1105_NUM_PORTS; i++) { 1535227d07a0SVladimir Oltean rc = dsa_port_setup_8021q_tagging(ds, i, enabled); 1536227d07a0SVladimir Oltean if (rc < 0) { 1537227d07a0SVladimir Oltean dev_err(ds->dev, "Failed to setup VLAN tagging for port %d: %d\n", 1538227d07a0SVladimir Oltean i, rc); 1539227d07a0SVladimir Oltean return rc; 1540227d07a0SVladimir Oltean } 1541227d07a0SVladimir Oltean } 1542227d07a0SVladimir Oltean dev_info(ds->dev, "%s switch tagging\n", 1543227d07a0SVladimir Oltean enabled ? "Enabled" : "Disabled"); 1544227d07a0SVladimir Oltean return 0; 1545227d07a0SVladimir Oltean } 1546227d07a0SVladimir Oltean 15478aa9ebccSVladimir Oltean static enum dsa_tag_protocol 15484d776482SFlorian Fainelli sja1105_get_tag_protocol(struct dsa_switch *ds, int port, 15494d776482SFlorian Fainelli enum dsa_tag_protocol mp) 15508aa9ebccSVladimir Oltean { 1551227d07a0SVladimir Oltean return DSA_TAG_PROTO_SJA1105; 15528aa9ebccSVladimir Oltean } 15538aa9ebccSVladimir Oltean 15546666cebcSVladimir Oltean /* This callback needs to be present */ 15556666cebcSVladimir Oltean static int sja1105_vlan_prepare(struct dsa_switch *ds, int port, 15566666cebcSVladimir Oltean const struct switchdev_obj_port_vlan *vlan) 15576666cebcSVladimir Oltean { 15586666cebcSVladimir Oltean return 0; 15596666cebcSVladimir Oltean } 15606666cebcSVladimir Oltean 1561070ca3bbSVladimir Oltean /* The TPID setting belongs to the General Parameters table, 1562070ca3bbSVladimir Oltean * which can only be partially reconfigured at runtime (and not the TPID). 1563070ca3bbSVladimir Oltean * So a switch reset is required. 1564070ca3bbSVladimir Oltean */ 15656666cebcSVladimir Oltean static int sja1105_vlan_filtering(struct dsa_switch *ds, int port, bool enabled) 15666666cebcSVladimir Oltean { 15676d7c7d94SVladimir Oltean struct sja1105_l2_lookup_params_entry *l2_lookup_params; 1568070ca3bbSVladimir Oltean struct sja1105_general_params_entry *general_params; 15696666cebcSVladimir Oltean struct sja1105_private *priv = ds->priv; 1570070ca3bbSVladimir Oltean struct sja1105_table *table; 1571070ca3bbSVladimir Oltean u16 tpid, tpid2; 15726666cebcSVladimir Oltean int rc; 15736666cebcSVladimir Oltean 1574070ca3bbSVladimir Oltean if (enabled) { 15756666cebcSVladimir Oltean /* Enable VLAN filtering. */ 157654fa49eeSVladimir Oltean tpid = ETH_P_8021Q; 157754fa49eeSVladimir Oltean tpid2 = ETH_P_8021AD; 1578070ca3bbSVladimir Oltean } else { 15796666cebcSVladimir Oltean /* Disable VLAN filtering. */ 1580070ca3bbSVladimir Oltean tpid = ETH_P_SJA1105; 1581070ca3bbSVladimir Oltean tpid2 = ETH_P_SJA1105; 1582070ca3bbSVladimir Oltean } 1583070ca3bbSVladimir Oltean 1584070ca3bbSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; 1585070ca3bbSVladimir Oltean general_params = table->entries; 1586f9a1a764SVladimir Oltean /* EtherType used to identify inner tagged (C-tag) VLAN traffic */ 158754fa49eeSVladimir Oltean general_params->tpid = tpid; 158854fa49eeSVladimir Oltean /* EtherType used to identify outer tagged (S-tag) VLAN traffic */ 1589070ca3bbSVladimir Oltean general_params->tpid2 = tpid2; 159042824463SVladimir Oltean /* When VLAN filtering is on, we need to at least be able to 159142824463SVladimir Oltean * decode management traffic through the "backup plan". 159242824463SVladimir Oltean */ 159342824463SVladimir Oltean general_params->incl_srcpt1 = enabled; 159442824463SVladimir Oltean general_params->incl_srcpt0 = enabled; 1595070ca3bbSVladimir Oltean 15966d7c7d94SVladimir Oltean /* VLAN filtering => independent VLAN learning. 15976d7c7d94SVladimir Oltean * No VLAN filtering => shared VLAN learning. 15986d7c7d94SVladimir Oltean * 15996d7c7d94SVladimir Oltean * In shared VLAN learning mode, untagged traffic still gets 16006d7c7d94SVladimir Oltean * pvid-tagged, and the FDB table gets populated with entries 16016d7c7d94SVladimir Oltean * containing the "real" (pvid or from VLAN tag) VLAN ID. 16026d7c7d94SVladimir Oltean * However the switch performs a masked L2 lookup in the FDB, 16036d7c7d94SVladimir Oltean * effectively only looking up a frame's DMAC (and not VID) for the 16046d7c7d94SVladimir Oltean * forwarding decision. 16056d7c7d94SVladimir Oltean * 16066d7c7d94SVladimir Oltean * This is extremely convenient for us, because in modes with 16076d7c7d94SVladimir Oltean * vlan_filtering=0, dsa_8021q actually installs unique pvid's into 16086d7c7d94SVladimir Oltean * each front panel port. This is good for identification but breaks 16096d7c7d94SVladimir Oltean * learning badly - the VID of the learnt FDB entry is unique, aka 16106d7c7d94SVladimir Oltean * no frames coming from any other port are going to have it. So 16116d7c7d94SVladimir Oltean * for forwarding purposes, this is as though learning was broken 16126d7c7d94SVladimir Oltean * (all frames get flooded). 16136d7c7d94SVladimir Oltean */ 16146d7c7d94SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS]; 16156d7c7d94SVladimir Oltean l2_lookup_params = table->entries; 16166d7c7d94SVladimir Oltean l2_lookup_params->shared_learn = !enabled; 16176d7c7d94SVladimir Oltean 16182eea1fa8SVladimir Oltean rc = sja1105_static_config_reload(priv, SJA1105_VLAN_FILTERING); 16196666cebcSVladimir Oltean if (rc) 16206666cebcSVladimir Oltean dev_err(ds->dev, "Failed to change VLAN Ethertype\n"); 16216666cebcSVladimir Oltean 1622227d07a0SVladimir Oltean /* Switch port identification based on 802.1Q is only passable 1623227d07a0SVladimir Oltean * if we are not under a vlan_filtering bridge. So make sure 1624227d07a0SVladimir Oltean * the two configurations are mutually exclusive. 1625227d07a0SVladimir Oltean */ 1626227d07a0SVladimir Oltean return sja1105_setup_8021q_tagging(ds, !enabled); 16276666cebcSVladimir Oltean } 16286666cebcSVladimir Oltean 16296666cebcSVladimir Oltean static void sja1105_vlan_add(struct dsa_switch *ds, int port, 16306666cebcSVladimir Oltean const struct switchdev_obj_port_vlan *vlan) 16316666cebcSVladimir Oltean { 16326666cebcSVladimir Oltean struct sja1105_private *priv = ds->priv; 16336666cebcSVladimir Oltean u16 vid; 16346666cebcSVladimir Oltean int rc; 16356666cebcSVladimir Oltean 16366666cebcSVladimir Oltean for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) { 16376666cebcSVladimir Oltean rc = sja1105_vlan_apply(priv, port, vid, true, vlan->flags & 16386666cebcSVladimir Oltean BRIDGE_VLAN_INFO_UNTAGGED); 16396666cebcSVladimir Oltean if (rc < 0) { 16406666cebcSVladimir Oltean dev_err(ds->dev, "Failed to add VLAN %d to port %d: %d\n", 16416666cebcSVladimir Oltean vid, port, rc); 16426666cebcSVladimir Oltean return; 16436666cebcSVladimir Oltean } 16446666cebcSVladimir Oltean if (vlan->flags & BRIDGE_VLAN_INFO_PVID) { 16456666cebcSVladimir Oltean rc = sja1105_pvid_apply(ds->priv, port, vid); 16466666cebcSVladimir Oltean if (rc < 0) { 16476666cebcSVladimir Oltean dev_err(ds->dev, "Failed to set pvid %d on port %d: %d\n", 16486666cebcSVladimir Oltean vid, port, rc); 16496666cebcSVladimir Oltean return; 16506666cebcSVladimir Oltean } 16516666cebcSVladimir Oltean } 16526666cebcSVladimir Oltean } 16536666cebcSVladimir Oltean } 16546666cebcSVladimir Oltean 16556666cebcSVladimir Oltean static int sja1105_vlan_del(struct dsa_switch *ds, int port, 16566666cebcSVladimir Oltean const struct switchdev_obj_port_vlan *vlan) 16576666cebcSVladimir Oltean { 16586666cebcSVladimir Oltean struct sja1105_private *priv = ds->priv; 16596666cebcSVladimir Oltean u16 vid; 16606666cebcSVladimir Oltean int rc; 16616666cebcSVladimir Oltean 16626666cebcSVladimir Oltean for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) { 16636666cebcSVladimir Oltean rc = sja1105_vlan_apply(priv, port, vid, false, vlan->flags & 16646666cebcSVladimir Oltean BRIDGE_VLAN_INFO_UNTAGGED); 16656666cebcSVladimir Oltean if (rc < 0) { 16666666cebcSVladimir Oltean dev_err(ds->dev, "Failed to remove VLAN %d from port %d: %d\n", 16676666cebcSVladimir Oltean vid, port, rc); 16686666cebcSVladimir Oltean return rc; 16696666cebcSVladimir Oltean } 16706666cebcSVladimir Oltean } 16716666cebcSVladimir Oltean return 0; 16726666cebcSVladimir Oltean } 16736666cebcSVladimir Oltean 16748aa9ebccSVladimir Oltean /* The programming model for the SJA1105 switch is "all-at-once" via static 16758aa9ebccSVladimir Oltean * configuration tables. Some of these can be dynamically modified at runtime, 16768aa9ebccSVladimir Oltean * but not the xMII mode parameters table. 16778aa9ebccSVladimir Oltean * Furthermode, some PHYs may not have crystals for generating their clocks 16788aa9ebccSVladimir Oltean * (e.g. RMII). Instead, their 50MHz clock is supplied via the SJA1105 port's 16798aa9ebccSVladimir Oltean * ref_clk pin. So port clocking needs to be initialized early, before 16808aa9ebccSVladimir Oltean * connecting to PHYs is attempted, otherwise they won't respond through MDIO. 16818aa9ebccSVladimir Oltean * Setting correct PHY link speed does not matter now. 16828aa9ebccSVladimir Oltean * But dsa_slave_phy_setup is called later than sja1105_setup, so the PHY 16838aa9ebccSVladimir Oltean * bindings are not yet parsed by DSA core. We need to parse early so that we 16848aa9ebccSVladimir Oltean * can populate the xMII mode parameters table. 16858aa9ebccSVladimir Oltean */ 16868aa9ebccSVladimir Oltean static int sja1105_setup(struct dsa_switch *ds) 16878aa9ebccSVladimir Oltean { 16888aa9ebccSVladimir Oltean struct sja1105_dt_port ports[SJA1105_NUM_PORTS]; 16898aa9ebccSVladimir Oltean struct sja1105_private *priv = ds->priv; 16908aa9ebccSVladimir Oltean int rc; 16918aa9ebccSVladimir Oltean 16928aa9ebccSVladimir Oltean rc = sja1105_parse_dt(priv, ports); 16938aa9ebccSVladimir Oltean if (rc < 0) { 16948aa9ebccSVladimir Oltean dev_err(ds->dev, "Failed to parse DT: %d\n", rc); 16958aa9ebccSVladimir Oltean return rc; 16968aa9ebccSVladimir Oltean } 1697f5b8631cSVladimir Oltean 1698f5b8631cSVladimir Oltean /* Error out early if internal delays are required through DT 1699f5b8631cSVladimir Oltean * and we can't apply them. 1700f5b8631cSVladimir Oltean */ 1701f5b8631cSVladimir Oltean rc = sja1105_parse_rgmii_delays(priv, ports); 1702f5b8631cSVladimir Oltean if (rc < 0) { 1703f5b8631cSVladimir Oltean dev_err(ds->dev, "RGMII delay not supported\n"); 1704f5b8631cSVladimir Oltean return rc; 1705f5b8631cSVladimir Oltean } 1706f5b8631cSVladimir Oltean 170761c77126SVladimir Oltean rc = sja1105_ptp_clock_register(ds); 1708bb77f36aSVladimir Oltean if (rc < 0) { 1709bb77f36aSVladimir Oltean dev_err(ds->dev, "Failed to register PTP clock: %d\n", rc); 1710bb77f36aSVladimir Oltean return rc; 1711bb77f36aSVladimir Oltean } 17128aa9ebccSVladimir Oltean /* Create and send configuration down to device */ 17138aa9ebccSVladimir Oltean rc = sja1105_static_config_load(priv, ports); 17148aa9ebccSVladimir Oltean if (rc < 0) { 17158aa9ebccSVladimir Oltean dev_err(ds->dev, "Failed to load static config: %d\n", rc); 17168aa9ebccSVladimir Oltean return rc; 17178aa9ebccSVladimir Oltean } 17188aa9ebccSVladimir Oltean /* Configure the CGU (PHY link modes and speeds) */ 17198aa9ebccSVladimir Oltean rc = sja1105_clocking_setup(priv); 17208aa9ebccSVladimir Oltean if (rc < 0) { 17218aa9ebccSVladimir Oltean dev_err(ds->dev, "Failed to configure MII clocking: %d\n", rc); 17228aa9ebccSVladimir Oltean return rc; 17238aa9ebccSVladimir Oltean } 17246666cebcSVladimir Oltean /* On SJA1105, VLAN filtering per se is always enabled in hardware. 17256666cebcSVladimir Oltean * The only thing we can do to disable it is lie about what the 802.1Q 17266666cebcSVladimir Oltean * EtherType is. 17276666cebcSVladimir Oltean * So it will still try to apply VLAN filtering, but all ingress 17286666cebcSVladimir Oltean * traffic (except frames received with EtherType of ETH_P_SJA1105) 17296666cebcSVladimir Oltean * will be internally tagged with a distorted VLAN header where the 17306666cebcSVladimir Oltean * TPID is ETH_P_SJA1105, and the VLAN ID is the port pvid. 17316666cebcSVladimir Oltean */ 17326666cebcSVladimir Oltean ds->vlan_filtering_is_global = true; 17338aa9ebccSVladimir Oltean 17345f06c63bSVladimir Oltean /* Advertise the 8 egress queues */ 17355f06c63bSVladimir Oltean ds->num_tx_queues = SJA1105_NUM_TC; 17365f06c63bSVladimir Oltean 1737227d07a0SVladimir Oltean /* The DSA/switchdev model brings up switch ports in standalone mode by 1738227d07a0SVladimir Oltean * default, and that means vlan_filtering is 0 since they're not under 1739227d07a0SVladimir Oltean * a bridge, so it's safe to set up switch tagging at this time. 1740227d07a0SVladimir Oltean */ 1741227d07a0SVladimir Oltean return sja1105_setup_8021q_tagging(ds, true); 1742227d07a0SVladimir Oltean } 1743227d07a0SVladimir Oltean 1744f3097be2SVladimir Oltean static void sja1105_teardown(struct dsa_switch *ds) 1745f3097be2SVladimir Oltean { 1746f3097be2SVladimir Oltean struct sja1105_private *priv = ds->priv; 1747a68578c2SVladimir Oltean int port; 1748a68578c2SVladimir Oltean 1749a68578c2SVladimir Oltean for (port = 0; port < SJA1105_NUM_PORTS; port++) { 1750a68578c2SVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 1751a68578c2SVladimir Oltean 1752a68578c2SVladimir Oltean if (!dsa_is_user_port(ds, port)) 1753a68578c2SVladimir Oltean continue; 1754a68578c2SVladimir Oltean 175552c0d4e3SVladimir Oltean if (sp->xmit_worker) 1756a68578c2SVladimir Oltean kthread_destroy_worker(sp->xmit_worker); 1757a68578c2SVladimir Oltean } 1758f3097be2SVladimir Oltean 1759317ab5b8SVladimir Oltean sja1105_tas_teardown(ds); 176061c77126SVladimir Oltean sja1105_ptp_clock_unregister(ds); 17616cb0abbdSVladimir Oltean sja1105_static_config_free(&priv->static_config); 1762f3097be2SVladimir Oltean } 1763f3097be2SVladimir Oltean 1764e9bf9694SVladimir Oltean static int sja1105_port_enable(struct dsa_switch *ds, int port, 1765e9bf9694SVladimir Oltean struct phy_device *phy) 1766e9bf9694SVladimir Oltean { 1767e9bf9694SVladimir Oltean struct net_device *slave; 1768e9bf9694SVladimir Oltean 1769e9bf9694SVladimir Oltean if (!dsa_is_user_port(ds, port)) 1770e9bf9694SVladimir Oltean return 0; 1771e9bf9694SVladimir Oltean 177268bb8ea8SVivien Didelot slave = dsa_to_port(ds, port)->slave; 1773e9bf9694SVladimir Oltean 1774e9bf9694SVladimir Oltean slave->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER; 1775e9bf9694SVladimir Oltean 1776e9bf9694SVladimir Oltean return 0; 1777e9bf9694SVladimir Oltean } 1778e9bf9694SVladimir Oltean 1779a68578c2SVladimir Oltean static void sja1105_port_disable(struct dsa_switch *ds, int port) 1780a68578c2SVladimir Oltean { 1781a68578c2SVladimir Oltean struct sja1105_private *priv = ds->priv; 1782a68578c2SVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 1783a68578c2SVladimir Oltean 1784a68578c2SVladimir Oltean if (!dsa_is_user_port(ds, port)) 1785a68578c2SVladimir Oltean return; 1786a68578c2SVladimir Oltean 1787a68578c2SVladimir Oltean kthread_cancel_work_sync(&sp->xmit_work); 1788a68578c2SVladimir Oltean skb_queue_purge(&sp->xmit_queue); 1789a68578c2SVladimir Oltean } 1790a68578c2SVladimir Oltean 1791227d07a0SVladimir Oltean static int sja1105_mgmt_xmit(struct dsa_switch *ds, int port, int slot, 179247ed985eSVladimir Oltean struct sk_buff *skb, bool takets) 1793227d07a0SVladimir Oltean { 1794227d07a0SVladimir Oltean struct sja1105_mgmt_entry mgmt_route = {0}; 1795227d07a0SVladimir Oltean struct sja1105_private *priv = ds->priv; 1796227d07a0SVladimir Oltean struct ethhdr *hdr; 1797227d07a0SVladimir Oltean int timeout = 10; 1798227d07a0SVladimir Oltean int rc; 1799227d07a0SVladimir Oltean 1800227d07a0SVladimir Oltean hdr = eth_hdr(skb); 1801227d07a0SVladimir Oltean 1802227d07a0SVladimir Oltean mgmt_route.macaddr = ether_addr_to_u64(hdr->h_dest); 1803227d07a0SVladimir Oltean mgmt_route.destports = BIT(port); 1804227d07a0SVladimir Oltean mgmt_route.enfport = 1; 180547ed985eSVladimir Oltean mgmt_route.tsreg = 0; 180647ed985eSVladimir Oltean mgmt_route.takets = takets; 1807227d07a0SVladimir Oltean 1808227d07a0SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE, 1809227d07a0SVladimir Oltean slot, &mgmt_route, true); 1810227d07a0SVladimir Oltean if (rc < 0) { 1811227d07a0SVladimir Oltean kfree_skb(skb); 1812227d07a0SVladimir Oltean return rc; 1813227d07a0SVladimir Oltean } 1814227d07a0SVladimir Oltean 1815227d07a0SVladimir Oltean /* Transfer skb to the host port. */ 181668bb8ea8SVivien Didelot dsa_enqueue_skb(skb, dsa_to_port(ds, port)->slave); 1817227d07a0SVladimir Oltean 1818227d07a0SVladimir Oltean /* Wait until the switch has processed the frame */ 1819227d07a0SVladimir Oltean do { 1820227d07a0SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_MGMT_ROUTE, 1821227d07a0SVladimir Oltean slot, &mgmt_route); 1822227d07a0SVladimir Oltean if (rc < 0) { 1823227d07a0SVladimir Oltean dev_err_ratelimited(priv->ds->dev, 1824227d07a0SVladimir Oltean "failed to poll for mgmt route\n"); 1825227d07a0SVladimir Oltean continue; 1826227d07a0SVladimir Oltean } 1827227d07a0SVladimir Oltean 1828227d07a0SVladimir Oltean /* UM10944: The ENFPORT flag of the respective entry is 1829227d07a0SVladimir Oltean * cleared when a match is found. The host can use this 1830227d07a0SVladimir Oltean * flag as an acknowledgment. 1831227d07a0SVladimir Oltean */ 1832227d07a0SVladimir Oltean cpu_relax(); 1833227d07a0SVladimir Oltean } while (mgmt_route.enfport && --timeout); 1834227d07a0SVladimir Oltean 1835227d07a0SVladimir Oltean if (!timeout) { 1836227d07a0SVladimir Oltean /* Clean up the management route so that a follow-up 1837227d07a0SVladimir Oltean * frame may not match on it by mistake. 18382a7e7409SVladimir Oltean * This is only hardware supported on P/Q/R/S - on E/T it is 18392a7e7409SVladimir Oltean * a no-op and we are silently discarding the -EOPNOTSUPP. 1840227d07a0SVladimir Oltean */ 1841227d07a0SVladimir Oltean sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE, 1842227d07a0SVladimir Oltean slot, &mgmt_route, false); 1843227d07a0SVladimir Oltean dev_err_ratelimited(priv->ds->dev, "xmit timed out\n"); 1844227d07a0SVladimir Oltean } 1845227d07a0SVladimir Oltean 1846227d07a0SVladimir Oltean return NETDEV_TX_OK; 1847227d07a0SVladimir Oltean } 1848227d07a0SVladimir Oltean 1849a68578c2SVladimir Oltean #define work_to_port(work) \ 1850a68578c2SVladimir Oltean container_of((work), struct sja1105_port, xmit_work) 1851a68578c2SVladimir Oltean #define tagger_to_sja1105(t) \ 1852a68578c2SVladimir Oltean container_of((t), struct sja1105_private, tagger_data) 1853a68578c2SVladimir Oltean 1854227d07a0SVladimir Oltean /* Deferred work is unfortunately necessary because setting up the management 1855227d07a0SVladimir Oltean * route cannot be done from atomit context (SPI transfer takes a sleepable 1856227d07a0SVladimir Oltean * lock on the bus) 1857227d07a0SVladimir Oltean */ 1858a68578c2SVladimir Oltean static void sja1105_port_deferred_xmit(struct kthread_work *work) 1859227d07a0SVladimir Oltean { 1860a68578c2SVladimir Oltean struct sja1105_port *sp = work_to_port(work); 1861a68578c2SVladimir Oltean struct sja1105_tagger_data *tagger_data = sp->data; 1862a68578c2SVladimir Oltean struct sja1105_private *priv = tagger_to_sja1105(tagger_data); 1863a68578c2SVladimir Oltean int port = sp - priv->ports; 1864a68578c2SVladimir Oltean struct sk_buff *skb; 1865a68578c2SVladimir Oltean 1866a68578c2SVladimir Oltean while ((skb = skb_dequeue(&sp->xmit_queue)) != NULL) { 1867a68578c2SVladimir Oltean struct sk_buff *clone = DSA_SKB_CB(skb)->clone; 1868227d07a0SVladimir Oltean 1869227d07a0SVladimir Oltean mutex_lock(&priv->mgmt_lock); 1870227d07a0SVladimir Oltean 1871a68578c2SVladimir Oltean sja1105_mgmt_xmit(priv->ds, port, 0, skb, !!clone); 1872a68578c2SVladimir Oltean 187347ed985eSVladimir Oltean /* The clone, if there, was made by dsa_skb_tx_timestamp */ 1874a68578c2SVladimir Oltean if (clone) 1875a68578c2SVladimir Oltean sja1105_ptp_txtstamp_skb(priv->ds, port, clone); 1876227d07a0SVladimir Oltean 1877227d07a0SVladimir Oltean mutex_unlock(&priv->mgmt_lock); 1878a68578c2SVladimir Oltean } 18798aa9ebccSVladimir Oltean } 18808aa9ebccSVladimir Oltean 18818456721dSVladimir Oltean /* The MAXAGE setting belongs to the L2 Forwarding Parameters table, 18828456721dSVladimir Oltean * which cannot be reconfigured at runtime. So a switch reset is required. 18838456721dSVladimir Oltean */ 18848456721dSVladimir Oltean static int sja1105_set_ageing_time(struct dsa_switch *ds, 18858456721dSVladimir Oltean unsigned int ageing_time) 18868456721dSVladimir Oltean { 18878456721dSVladimir Oltean struct sja1105_l2_lookup_params_entry *l2_lookup_params; 18888456721dSVladimir Oltean struct sja1105_private *priv = ds->priv; 18898456721dSVladimir Oltean struct sja1105_table *table; 18908456721dSVladimir Oltean unsigned int maxage; 18918456721dSVladimir Oltean 18928456721dSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS]; 18938456721dSVladimir Oltean l2_lookup_params = table->entries; 18948456721dSVladimir Oltean 18958456721dSVladimir Oltean maxage = SJA1105_AGEING_TIME_MS(ageing_time); 18968456721dSVladimir Oltean 18978456721dSVladimir Oltean if (l2_lookup_params->maxage == maxage) 18988456721dSVladimir Oltean return 0; 18998456721dSVladimir Oltean 19008456721dSVladimir Oltean l2_lookup_params->maxage = maxage; 19018456721dSVladimir Oltean 19022eea1fa8SVladimir Oltean return sja1105_static_config_reload(priv, SJA1105_AGEING_TIME); 19038456721dSVladimir Oltean } 19048456721dSVladimir Oltean 1905317ab5b8SVladimir Oltean static int sja1105_port_setup_tc(struct dsa_switch *ds, int port, 1906317ab5b8SVladimir Oltean enum tc_setup_type type, 1907317ab5b8SVladimir Oltean void *type_data) 1908317ab5b8SVladimir Oltean { 1909317ab5b8SVladimir Oltean switch (type) { 1910317ab5b8SVladimir Oltean case TC_SETUP_QDISC_TAPRIO: 1911317ab5b8SVladimir Oltean return sja1105_setup_tc_taprio(ds, port, type_data); 1912317ab5b8SVladimir Oltean default: 1913317ab5b8SVladimir Oltean return -EOPNOTSUPP; 1914317ab5b8SVladimir Oltean } 1915317ab5b8SVladimir Oltean } 1916317ab5b8SVladimir Oltean 1917511e6ca0SVladimir Oltean /* We have a single mirror (@to) port, but can configure ingress and egress 1918511e6ca0SVladimir Oltean * mirroring on all other (@from) ports. 1919511e6ca0SVladimir Oltean * We need to allow mirroring rules only as long as the @to port is always the 1920511e6ca0SVladimir Oltean * same, and we need to unset the @to port from mirr_port only when there is no 1921511e6ca0SVladimir Oltean * mirroring rule that references it. 1922511e6ca0SVladimir Oltean */ 1923511e6ca0SVladimir Oltean static int sja1105_mirror_apply(struct sja1105_private *priv, int from, int to, 1924511e6ca0SVladimir Oltean bool ingress, bool enabled) 1925511e6ca0SVladimir Oltean { 1926511e6ca0SVladimir Oltean struct sja1105_general_params_entry *general_params; 1927511e6ca0SVladimir Oltean struct sja1105_mac_config_entry *mac; 1928511e6ca0SVladimir Oltean struct sja1105_table *table; 1929511e6ca0SVladimir Oltean bool already_enabled; 1930511e6ca0SVladimir Oltean u64 new_mirr_port; 1931511e6ca0SVladimir Oltean int rc; 1932511e6ca0SVladimir Oltean 1933511e6ca0SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; 1934511e6ca0SVladimir Oltean general_params = table->entries; 1935511e6ca0SVladimir Oltean 1936511e6ca0SVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 1937511e6ca0SVladimir Oltean 1938511e6ca0SVladimir Oltean already_enabled = (general_params->mirr_port != SJA1105_NUM_PORTS); 1939511e6ca0SVladimir Oltean if (already_enabled && enabled && general_params->mirr_port != to) { 1940511e6ca0SVladimir Oltean dev_err(priv->ds->dev, 1941511e6ca0SVladimir Oltean "Delete mirroring rules towards port %llu first\n", 1942511e6ca0SVladimir Oltean general_params->mirr_port); 1943511e6ca0SVladimir Oltean return -EBUSY; 1944511e6ca0SVladimir Oltean } 1945511e6ca0SVladimir Oltean 1946511e6ca0SVladimir Oltean new_mirr_port = to; 1947511e6ca0SVladimir Oltean if (!enabled) { 1948511e6ca0SVladimir Oltean bool keep = false; 1949511e6ca0SVladimir Oltean int port; 1950511e6ca0SVladimir Oltean 1951511e6ca0SVladimir Oltean /* Anybody still referencing mirr_port? */ 1952511e6ca0SVladimir Oltean for (port = 0; port < SJA1105_NUM_PORTS; port++) { 1953511e6ca0SVladimir Oltean if (mac[port].ing_mirr || mac[port].egr_mirr) { 1954511e6ca0SVladimir Oltean keep = true; 1955511e6ca0SVladimir Oltean break; 1956511e6ca0SVladimir Oltean } 1957511e6ca0SVladimir Oltean } 1958511e6ca0SVladimir Oltean /* Unset already_enabled for next time */ 1959511e6ca0SVladimir Oltean if (!keep) 1960511e6ca0SVladimir Oltean new_mirr_port = SJA1105_NUM_PORTS; 1961511e6ca0SVladimir Oltean } 1962511e6ca0SVladimir Oltean if (new_mirr_port != general_params->mirr_port) { 1963511e6ca0SVladimir Oltean general_params->mirr_port = new_mirr_port; 1964511e6ca0SVladimir Oltean 1965511e6ca0SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_GENERAL_PARAMS, 1966511e6ca0SVladimir Oltean 0, general_params, true); 1967511e6ca0SVladimir Oltean if (rc < 0) 1968511e6ca0SVladimir Oltean return rc; 1969511e6ca0SVladimir Oltean } 1970511e6ca0SVladimir Oltean 1971511e6ca0SVladimir Oltean if (ingress) 1972511e6ca0SVladimir Oltean mac[from].ing_mirr = enabled; 1973511e6ca0SVladimir Oltean else 1974511e6ca0SVladimir Oltean mac[from].egr_mirr = enabled; 1975511e6ca0SVladimir Oltean 1976511e6ca0SVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, from, 1977511e6ca0SVladimir Oltean &mac[from], true); 1978511e6ca0SVladimir Oltean } 1979511e6ca0SVladimir Oltean 1980511e6ca0SVladimir Oltean static int sja1105_mirror_add(struct dsa_switch *ds, int port, 1981511e6ca0SVladimir Oltean struct dsa_mall_mirror_tc_entry *mirror, 1982511e6ca0SVladimir Oltean bool ingress) 1983511e6ca0SVladimir Oltean { 1984511e6ca0SVladimir Oltean return sja1105_mirror_apply(ds->priv, port, mirror->to_local_port, 1985511e6ca0SVladimir Oltean ingress, true); 1986511e6ca0SVladimir Oltean } 1987511e6ca0SVladimir Oltean 1988511e6ca0SVladimir Oltean static void sja1105_mirror_del(struct dsa_switch *ds, int port, 1989511e6ca0SVladimir Oltean struct dsa_mall_mirror_tc_entry *mirror) 1990511e6ca0SVladimir Oltean { 1991511e6ca0SVladimir Oltean sja1105_mirror_apply(ds->priv, port, mirror->to_local_port, 1992511e6ca0SVladimir Oltean mirror->ingress, false); 1993511e6ca0SVladimir Oltean } 1994511e6ca0SVladimir Oltean 19958aa9ebccSVladimir Oltean static const struct dsa_switch_ops sja1105_switch_ops = { 19968aa9ebccSVladimir Oltean .get_tag_protocol = sja1105_get_tag_protocol, 19978aa9ebccSVladimir Oltean .setup = sja1105_setup, 1998f3097be2SVladimir Oltean .teardown = sja1105_teardown, 19998456721dSVladimir Oltean .set_ageing_time = sja1105_set_ageing_time, 2000ad9f299aSVladimir Oltean .phylink_validate = sja1105_phylink_validate, 2001af7cd036SVladimir Oltean .phylink_mac_config = sja1105_mac_config, 20028400cff6SVladimir Oltean .phylink_mac_link_up = sja1105_mac_link_up, 20038400cff6SVladimir Oltean .phylink_mac_link_down = sja1105_mac_link_down, 200452c34e6eSVladimir Oltean .get_strings = sja1105_get_strings, 200552c34e6eSVladimir Oltean .get_ethtool_stats = sja1105_get_ethtool_stats, 200652c34e6eSVladimir Oltean .get_sset_count = sja1105_get_sset_count, 2007bb77f36aSVladimir Oltean .get_ts_info = sja1105_get_ts_info, 2008e9bf9694SVladimir Oltean .port_enable = sja1105_port_enable, 2009a68578c2SVladimir Oltean .port_disable = sja1105_port_disable, 2010291d1e72SVladimir Oltean .port_fdb_dump = sja1105_fdb_dump, 2011291d1e72SVladimir Oltean .port_fdb_add = sja1105_fdb_add, 2012291d1e72SVladimir Oltean .port_fdb_del = sja1105_fdb_del, 20138aa9ebccSVladimir Oltean .port_bridge_join = sja1105_bridge_join, 20148aa9ebccSVladimir Oltean .port_bridge_leave = sja1105_bridge_leave, 2015640f763fSVladimir Oltean .port_stp_state_set = sja1105_bridge_stp_state_set, 20166666cebcSVladimir Oltean .port_vlan_prepare = sja1105_vlan_prepare, 20176666cebcSVladimir Oltean .port_vlan_filtering = sja1105_vlan_filtering, 20186666cebcSVladimir Oltean .port_vlan_add = sja1105_vlan_add, 20196666cebcSVladimir Oltean .port_vlan_del = sja1105_vlan_del, 2020291d1e72SVladimir Oltean .port_mdb_prepare = sja1105_mdb_prepare, 2021291d1e72SVladimir Oltean .port_mdb_add = sja1105_mdb_add, 2022291d1e72SVladimir Oltean .port_mdb_del = sja1105_mdb_del, 2023a602afd2SVladimir Oltean .port_hwtstamp_get = sja1105_hwtstamp_get, 2024a602afd2SVladimir Oltean .port_hwtstamp_set = sja1105_hwtstamp_set, 2025f3097be2SVladimir Oltean .port_rxtstamp = sja1105_port_rxtstamp, 202647ed985eSVladimir Oltean .port_txtstamp = sja1105_port_txtstamp, 2027317ab5b8SVladimir Oltean .port_setup_tc = sja1105_port_setup_tc, 2028511e6ca0SVladimir Oltean .port_mirror_add = sja1105_mirror_add, 2029511e6ca0SVladimir Oltean .port_mirror_del = sja1105_mirror_del, 20308aa9ebccSVladimir Oltean }; 20318aa9ebccSVladimir Oltean 20328aa9ebccSVladimir Oltean static int sja1105_check_device_id(struct sja1105_private *priv) 20338aa9ebccSVladimir Oltean { 20348aa9ebccSVladimir Oltean const struct sja1105_regs *regs = priv->info->regs; 20358aa9ebccSVladimir Oltean u8 prod_id[SJA1105_SIZE_DEVICE_ID] = {0}; 20368aa9ebccSVladimir Oltean struct device *dev = &priv->spidev->dev; 2037dff79620SVladimir Oltean u32 device_id; 20388aa9ebccSVladimir Oltean u64 part_no; 20398aa9ebccSVladimir Oltean int rc; 20408aa9ebccSVladimir Oltean 204134d76e9fSVladimir Oltean rc = sja1105_xfer_u32(priv, SPI_READ, regs->device_id, &device_id, 204234d76e9fSVladimir Oltean NULL); 20438aa9ebccSVladimir Oltean if (rc < 0) 20448aa9ebccSVladimir Oltean return rc; 20458aa9ebccSVladimir Oltean 20468aa9ebccSVladimir Oltean if (device_id != priv->info->device_id) { 2047dff79620SVladimir Oltean dev_err(dev, "Expected device ID 0x%llx but read 0x%x\n", 20488aa9ebccSVladimir Oltean priv->info->device_id, device_id); 20498aa9ebccSVladimir Oltean return -ENODEV; 20508aa9ebccSVladimir Oltean } 20518aa9ebccSVladimir Oltean 20521bd44870SVladimir Oltean rc = sja1105_xfer_buf(priv, SPI_READ, regs->prod_id, prod_id, 20531bd44870SVladimir Oltean SJA1105_SIZE_DEVICE_ID); 20548aa9ebccSVladimir Oltean if (rc < 0) 20558aa9ebccSVladimir Oltean return rc; 20568aa9ebccSVladimir Oltean 20578aa9ebccSVladimir Oltean sja1105_unpack(prod_id, &part_no, 19, 4, SJA1105_SIZE_DEVICE_ID); 20588aa9ebccSVladimir Oltean 20598aa9ebccSVladimir Oltean if (part_no != priv->info->part_no) { 20608aa9ebccSVladimir Oltean dev_err(dev, "Expected part number 0x%llx but read 0x%llx\n", 20618aa9ebccSVladimir Oltean priv->info->part_no, part_no); 20628aa9ebccSVladimir Oltean return -ENODEV; 20638aa9ebccSVladimir Oltean } 20648aa9ebccSVladimir Oltean 20658aa9ebccSVladimir Oltean return 0; 20668aa9ebccSVladimir Oltean } 20678aa9ebccSVladimir Oltean 20688aa9ebccSVladimir Oltean static int sja1105_probe(struct spi_device *spi) 20698aa9ebccSVladimir Oltean { 2070844d7edcSVladimir Oltean struct sja1105_tagger_data *tagger_data; 20718aa9ebccSVladimir Oltean struct device *dev = &spi->dev; 20728aa9ebccSVladimir Oltean struct sja1105_private *priv; 20738aa9ebccSVladimir Oltean struct dsa_switch *ds; 2074a68578c2SVladimir Oltean int rc, port; 20758aa9ebccSVladimir Oltean 20768aa9ebccSVladimir Oltean if (!dev->of_node) { 20778aa9ebccSVladimir Oltean dev_err(dev, "No DTS bindings for SJA1105 driver\n"); 20788aa9ebccSVladimir Oltean return -EINVAL; 20798aa9ebccSVladimir Oltean } 20808aa9ebccSVladimir Oltean 20818aa9ebccSVladimir Oltean priv = devm_kzalloc(dev, sizeof(struct sja1105_private), GFP_KERNEL); 20828aa9ebccSVladimir Oltean if (!priv) 20838aa9ebccSVladimir Oltean return -ENOMEM; 20848aa9ebccSVladimir Oltean 20858aa9ebccSVladimir Oltean /* Configure the optional reset pin and bring up switch */ 20868aa9ebccSVladimir Oltean priv->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); 20878aa9ebccSVladimir Oltean if (IS_ERR(priv->reset_gpio)) 20888aa9ebccSVladimir Oltean dev_dbg(dev, "reset-gpios not defined, ignoring\n"); 20898aa9ebccSVladimir Oltean else 20908aa9ebccSVladimir Oltean sja1105_hw_reset(priv->reset_gpio, 1, 1); 20918aa9ebccSVladimir Oltean 20928aa9ebccSVladimir Oltean /* Populate our driver private structure (priv) based on 20938aa9ebccSVladimir Oltean * the device tree node that was probed (spi) 20948aa9ebccSVladimir Oltean */ 20958aa9ebccSVladimir Oltean priv->spidev = spi; 20968aa9ebccSVladimir Oltean spi_set_drvdata(spi, priv); 20978aa9ebccSVladimir Oltean 20988aa9ebccSVladimir Oltean /* Configure the SPI bus */ 20998aa9ebccSVladimir Oltean spi->bits_per_word = 8; 21008aa9ebccSVladimir Oltean rc = spi_setup(spi); 21018aa9ebccSVladimir Oltean if (rc < 0) { 21028aa9ebccSVladimir Oltean dev_err(dev, "Could not init SPI\n"); 21038aa9ebccSVladimir Oltean return rc; 21048aa9ebccSVladimir Oltean } 21058aa9ebccSVladimir Oltean 21068aa9ebccSVladimir Oltean priv->info = of_device_get_match_data(dev); 21078aa9ebccSVladimir Oltean 21088aa9ebccSVladimir Oltean /* Detect hardware device */ 21098aa9ebccSVladimir Oltean rc = sja1105_check_device_id(priv); 21108aa9ebccSVladimir Oltean if (rc < 0) { 21118aa9ebccSVladimir Oltean dev_err(dev, "Device ID check failed: %d\n", rc); 21128aa9ebccSVladimir Oltean return rc; 21138aa9ebccSVladimir Oltean } 21148aa9ebccSVladimir Oltean 21158aa9ebccSVladimir Oltean dev_info(dev, "Probed switch chip: %s\n", priv->info->name); 21168aa9ebccSVladimir Oltean 21177e99e347SVivien Didelot ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL); 21188aa9ebccSVladimir Oltean if (!ds) 21198aa9ebccSVladimir Oltean return -ENOMEM; 21208aa9ebccSVladimir Oltean 21217e99e347SVivien Didelot ds->dev = dev; 21227e99e347SVivien Didelot ds->num_ports = SJA1105_NUM_PORTS; 21238aa9ebccSVladimir Oltean ds->ops = &sja1105_switch_ops; 21248aa9ebccSVladimir Oltean ds->priv = priv; 21258aa9ebccSVladimir Oltean priv->ds = ds; 21268aa9ebccSVladimir Oltean 2127844d7edcSVladimir Oltean tagger_data = &priv->tagger_data; 2128844d7edcSVladimir Oltean 2129d5a619bfSVivien Didelot mutex_init(&priv->ptp_data.lock); 2130d5a619bfSVivien Didelot mutex_init(&priv->mgmt_lock); 2131d5a619bfSVivien Didelot 2132d5a619bfSVivien Didelot sja1105_tas_setup(ds); 2133d5a619bfSVivien Didelot 2134d5a619bfSVivien Didelot rc = dsa_register_switch(priv->ds); 2135d5a619bfSVivien Didelot if (rc) 2136d5a619bfSVivien Didelot return rc; 2137d5a619bfSVivien Didelot 2138227d07a0SVladimir Oltean /* Connections between dsa_port and sja1105_port */ 2139a68578c2SVladimir Oltean for (port = 0; port < SJA1105_NUM_PORTS; port++) { 2140a68578c2SVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 2141a68578c2SVladimir Oltean struct dsa_port *dp = dsa_to_port(ds, port); 2142a68578c2SVladimir Oltean struct net_device *slave; 2143227d07a0SVladimir Oltean 2144a68578c2SVladimir Oltean if (!dsa_is_user_port(ds, port)) 2145a68578c2SVladimir Oltean continue; 2146a68578c2SVladimir Oltean 2147a68578c2SVladimir Oltean dp->priv = sp; 2148a68578c2SVladimir Oltean sp->dp = dp; 2149844d7edcSVladimir Oltean sp->data = tagger_data; 2150a68578c2SVladimir Oltean slave = dp->slave; 2151a68578c2SVladimir Oltean kthread_init_work(&sp->xmit_work, sja1105_port_deferred_xmit); 2152a68578c2SVladimir Oltean sp->xmit_worker = kthread_create_worker(0, "%s_xmit", 2153a68578c2SVladimir Oltean slave->name); 2154a68578c2SVladimir Oltean if (IS_ERR(sp->xmit_worker)) { 2155a68578c2SVladimir Oltean rc = PTR_ERR(sp->xmit_worker); 2156a68578c2SVladimir Oltean dev_err(ds->dev, 2157a68578c2SVladimir Oltean "failed to create deferred xmit thread: %d\n", 2158a68578c2SVladimir Oltean rc); 2159a68578c2SVladimir Oltean goto out; 2160a68578c2SVladimir Oltean } 2161a68578c2SVladimir Oltean skb_queue_head_init(&sp->xmit_queue); 2162227d07a0SVladimir Oltean } 2163227d07a0SVladimir Oltean 2164d5a619bfSVivien Didelot return 0; 2165a68578c2SVladimir Oltean out: 2166a68578c2SVladimir Oltean while (port-- > 0) { 2167a68578c2SVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 2168a68578c2SVladimir Oltean 2169a68578c2SVladimir Oltean if (!dsa_is_user_port(ds, port)) 2170a68578c2SVladimir Oltean continue; 2171a68578c2SVladimir Oltean 2172a68578c2SVladimir Oltean kthread_destroy_worker(sp->xmit_worker); 2173a68578c2SVladimir Oltean } 2174a68578c2SVladimir Oltean return rc; 21758aa9ebccSVladimir Oltean } 21768aa9ebccSVladimir Oltean 21778aa9ebccSVladimir Oltean static int sja1105_remove(struct spi_device *spi) 21788aa9ebccSVladimir Oltean { 21798aa9ebccSVladimir Oltean struct sja1105_private *priv = spi_get_drvdata(spi); 21808aa9ebccSVladimir Oltean 21818aa9ebccSVladimir Oltean dsa_unregister_switch(priv->ds); 21828aa9ebccSVladimir Oltean return 0; 21838aa9ebccSVladimir Oltean } 21848aa9ebccSVladimir Oltean 21858aa9ebccSVladimir Oltean static const struct of_device_id sja1105_dt_ids[] = { 21868aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105e", .data = &sja1105e_info }, 21878aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105t", .data = &sja1105t_info }, 21888aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105p", .data = &sja1105p_info }, 21898aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105q", .data = &sja1105q_info }, 21908aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105r", .data = &sja1105r_info }, 21918aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105s", .data = &sja1105s_info }, 21928aa9ebccSVladimir Oltean { /* sentinel */ }, 21938aa9ebccSVladimir Oltean }; 21948aa9ebccSVladimir Oltean MODULE_DEVICE_TABLE(of, sja1105_dt_ids); 21958aa9ebccSVladimir Oltean 21968aa9ebccSVladimir Oltean static struct spi_driver sja1105_driver = { 21978aa9ebccSVladimir Oltean .driver = { 21988aa9ebccSVladimir Oltean .name = "sja1105", 21998aa9ebccSVladimir Oltean .owner = THIS_MODULE, 22008aa9ebccSVladimir Oltean .of_match_table = of_match_ptr(sja1105_dt_ids), 22018aa9ebccSVladimir Oltean }, 22028aa9ebccSVladimir Oltean .probe = sja1105_probe, 22038aa9ebccSVladimir Oltean .remove = sja1105_remove, 22048aa9ebccSVladimir Oltean }; 22058aa9ebccSVladimir Oltean 22068aa9ebccSVladimir Oltean module_spi_driver(sja1105_driver); 22078aa9ebccSVladimir Oltean 22088aa9ebccSVladimir Oltean MODULE_AUTHOR("Vladimir Oltean <olteanv@gmail.com>"); 22098aa9ebccSVladimir Oltean MODULE_AUTHOR("Georg Waibel <georg.waibel@sensor-technik.de>"); 22108aa9ebccSVladimir Oltean MODULE_DESCRIPTION("SJA1105 Driver"); 22118aa9ebccSVladimir Oltean MODULE_LICENSE("GPL v2"); 2212