xref: /openbmc/linux/drivers/net/dsa/sja1105/sja1105_main.c (revision d763778224ea8005b650e12d8f4cd470265fe0b9)
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"
258aa9ebccSVladimir Oltean 
268aa9ebccSVladimir Oltean static void sja1105_hw_reset(struct gpio_desc *gpio, unsigned int pulse_len,
278aa9ebccSVladimir Oltean 			     unsigned int startup_delay)
288aa9ebccSVladimir Oltean {
298aa9ebccSVladimir Oltean 	gpiod_set_value_cansleep(gpio, 1);
308aa9ebccSVladimir Oltean 	/* Wait for minimum reset pulse length */
318aa9ebccSVladimir Oltean 	msleep(pulse_len);
328aa9ebccSVladimir Oltean 	gpiod_set_value_cansleep(gpio, 0);
338aa9ebccSVladimir Oltean 	/* Wait until chip is ready after reset */
348aa9ebccSVladimir Oltean 	msleep(startup_delay);
358aa9ebccSVladimir Oltean }
368aa9ebccSVladimir Oltean 
378aa9ebccSVladimir Oltean static void
388aa9ebccSVladimir Oltean sja1105_port_allow_traffic(struct sja1105_l2_forwarding_entry *l2_fwd,
398aa9ebccSVladimir Oltean 			   int from, int to, bool allow)
408aa9ebccSVladimir Oltean {
418aa9ebccSVladimir Oltean 	if (allow) {
428aa9ebccSVladimir Oltean 		l2_fwd[from].bc_domain  |= BIT(to);
438aa9ebccSVladimir Oltean 		l2_fwd[from].reach_port |= BIT(to);
448aa9ebccSVladimir Oltean 		l2_fwd[from].fl_domain  |= BIT(to);
458aa9ebccSVladimir Oltean 	} else {
468aa9ebccSVladimir Oltean 		l2_fwd[from].bc_domain  &= ~BIT(to);
478aa9ebccSVladimir Oltean 		l2_fwd[from].reach_port &= ~BIT(to);
488aa9ebccSVladimir Oltean 		l2_fwd[from].fl_domain  &= ~BIT(to);
498aa9ebccSVladimir Oltean 	}
508aa9ebccSVladimir Oltean }
518aa9ebccSVladimir Oltean 
528aa9ebccSVladimir Oltean /* Structure used to temporarily transport device tree
538aa9ebccSVladimir Oltean  * settings into sja1105_setup
548aa9ebccSVladimir Oltean  */
558aa9ebccSVladimir Oltean struct sja1105_dt_port {
568aa9ebccSVladimir Oltean 	phy_interface_t phy_mode;
578aa9ebccSVladimir Oltean 	sja1105_mii_role_t role;
588aa9ebccSVladimir Oltean };
598aa9ebccSVladimir Oltean 
608aa9ebccSVladimir Oltean static int sja1105_init_mac_settings(struct sja1105_private *priv)
618aa9ebccSVladimir Oltean {
628aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry default_mac = {
638aa9ebccSVladimir Oltean 		/* Enable all 8 priority queues on egress.
648aa9ebccSVladimir Oltean 		 * Every queue i holds top[i] - base[i] frames.
658aa9ebccSVladimir Oltean 		 * Sum of top[i] - base[i] is 511 (max hardware limit).
668aa9ebccSVladimir Oltean 		 */
678aa9ebccSVladimir Oltean 		.top  = {0x3F, 0x7F, 0xBF, 0xFF, 0x13F, 0x17F, 0x1BF, 0x1FF},
688aa9ebccSVladimir Oltean 		.base = {0x0, 0x40, 0x80, 0xC0, 0x100, 0x140, 0x180, 0x1C0},
698aa9ebccSVladimir Oltean 		.enabled = {true, true, true, true, true, true, true, true},
708aa9ebccSVladimir Oltean 		/* Keep standard IFG of 12 bytes on egress. */
718aa9ebccSVladimir Oltean 		.ifg = 0,
728aa9ebccSVladimir Oltean 		/* Always put the MAC speed in automatic mode, where it can be
731fd4a173SVladimir Oltean 		 * adjusted at runtime by PHYLINK.
748aa9ebccSVladimir Oltean 		 */
758aa9ebccSVladimir Oltean 		.speed = SJA1105_SPEED_AUTO,
768aa9ebccSVladimir Oltean 		/* No static correction for 1-step 1588 events */
778aa9ebccSVladimir Oltean 		.tp_delin = 0,
788aa9ebccSVladimir Oltean 		.tp_delout = 0,
798aa9ebccSVladimir Oltean 		/* Disable aging for critical TTEthernet traffic */
808aa9ebccSVladimir Oltean 		.maxage = 0xFF,
818aa9ebccSVladimir Oltean 		/* Internal VLAN (pvid) to apply to untagged ingress */
828aa9ebccSVladimir Oltean 		.vlanprio = 0,
83e3502b82SVladimir Oltean 		.vlanid = 1,
848aa9ebccSVladimir Oltean 		.ing_mirr = false,
858aa9ebccSVladimir Oltean 		.egr_mirr = false,
868aa9ebccSVladimir Oltean 		/* Don't drop traffic with other EtherType than ETH_P_IP */
878aa9ebccSVladimir Oltean 		.drpnona664 = false,
888aa9ebccSVladimir Oltean 		/* Don't drop double-tagged traffic */
898aa9ebccSVladimir Oltean 		.drpdtag = false,
908aa9ebccSVladimir Oltean 		/* Don't drop untagged traffic */
918aa9ebccSVladimir Oltean 		.drpuntag = false,
928aa9ebccSVladimir Oltean 		/* Don't retag 802.1p (VID 0) traffic with the pvid */
938aa9ebccSVladimir Oltean 		.retag = false,
94640f763fSVladimir Oltean 		/* Disable learning and I/O on user ports by default -
95640f763fSVladimir Oltean 		 * STP will enable it.
96640f763fSVladimir Oltean 		 */
97640f763fSVladimir Oltean 		.dyn_learn = false,
988aa9ebccSVladimir Oltean 		.egress = false,
998aa9ebccSVladimir Oltean 		.ingress = false,
1008aa9ebccSVladimir Oltean 	};
1018aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
1028aa9ebccSVladimir Oltean 	struct sja1105_table *table;
1038aa9ebccSVladimir Oltean 	int i;
1048aa9ebccSVladimir Oltean 
1058aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_MAC_CONFIG];
1068aa9ebccSVladimir Oltean 
1078aa9ebccSVladimir Oltean 	/* Discard previous MAC Configuration Table */
1088aa9ebccSVladimir Oltean 	if (table->entry_count) {
1098aa9ebccSVladimir Oltean 		kfree(table->entries);
1108aa9ebccSVladimir Oltean 		table->entry_count = 0;
1118aa9ebccSVladimir Oltean 	}
1128aa9ebccSVladimir Oltean 
1138aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_NUM_PORTS,
1148aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
1158aa9ebccSVladimir Oltean 	if (!table->entries)
1168aa9ebccSVladimir Oltean 		return -ENOMEM;
1178aa9ebccSVladimir Oltean 
1188aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_NUM_PORTS;
1198aa9ebccSVladimir Oltean 
1208aa9ebccSVladimir Oltean 	mac = table->entries;
1218aa9ebccSVladimir Oltean 
122640f763fSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
1238aa9ebccSVladimir Oltean 		mac[i] = default_mac;
124640f763fSVladimir Oltean 		if (i == dsa_upstream_port(priv->ds, i)) {
125640f763fSVladimir Oltean 			/* STP doesn't get called for CPU port, so we need to
126640f763fSVladimir Oltean 			 * set the I/O parameters statically.
127640f763fSVladimir Oltean 			 */
128640f763fSVladimir Oltean 			mac[i].dyn_learn = true;
129640f763fSVladimir Oltean 			mac[i].ingress = true;
130640f763fSVladimir Oltean 			mac[i].egress = true;
131640f763fSVladimir Oltean 		}
132640f763fSVladimir Oltean 	}
1338aa9ebccSVladimir Oltean 
1348aa9ebccSVladimir Oltean 	return 0;
1358aa9ebccSVladimir Oltean }
1368aa9ebccSVladimir Oltean 
1378aa9ebccSVladimir Oltean static int sja1105_init_mii_settings(struct sja1105_private *priv,
1388aa9ebccSVladimir Oltean 				     struct sja1105_dt_port *ports)
1398aa9ebccSVladimir Oltean {
1408aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
1418aa9ebccSVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
1428aa9ebccSVladimir Oltean 	struct sja1105_table *table;
1438aa9ebccSVladimir Oltean 	int i;
1448aa9ebccSVladimir Oltean 
1458aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_XMII_PARAMS];
1468aa9ebccSVladimir Oltean 
1478aa9ebccSVladimir Oltean 	/* Discard previous xMII Mode Parameters Table */
1488aa9ebccSVladimir Oltean 	if (table->entry_count) {
1498aa9ebccSVladimir Oltean 		kfree(table->entries);
1508aa9ebccSVladimir Oltean 		table->entry_count = 0;
1518aa9ebccSVladimir Oltean 	}
1528aa9ebccSVladimir Oltean 
1538aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_XMII_PARAMS_COUNT,
1548aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
1558aa9ebccSVladimir Oltean 	if (!table->entries)
1568aa9ebccSVladimir Oltean 		return -ENOMEM;
1578aa9ebccSVladimir Oltean 
1581fd4a173SVladimir Oltean 	/* Override table based on PHYLINK DT bindings */
1598aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_XMII_PARAMS_COUNT;
1608aa9ebccSVladimir Oltean 
1618aa9ebccSVladimir Oltean 	mii = table->entries;
1628aa9ebccSVladimir Oltean 
1638aa9ebccSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
1648aa9ebccSVladimir Oltean 		switch (ports[i].phy_mode) {
1658aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_MII:
1668aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_MII;
1678aa9ebccSVladimir Oltean 			break;
1688aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RMII:
1698aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_RMII;
1708aa9ebccSVladimir Oltean 			break;
1718aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII:
1728aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_ID:
1738aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_RXID:
1748aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_TXID:
1758aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_RGMII;
1768aa9ebccSVladimir Oltean 			break;
1778aa9ebccSVladimir Oltean 		default:
1788aa9ebccSVladimir Oltean 			dev_err(dev, "Unsupported PHY mode %s!\n",
1798aa9ebccSVladimir Oltean 				phy_modes(ports[i].phy_mode));
1808aa9ebccSVladimir Oltean 		}
1818aa9ebccSVladimir Oltean 
1828aa9ebccSVladimir Oltean 		mii->phy_mac[i] = ports[i].role;
1838aa9ebccSVladimir Oltean 	}
1848aa9ebccSVladimir Oltean 	return 0;
1858aa9ebccSVladimir Oltean }
1868aa9ebccSVladimir Oltean 
1878aa9ebccSVladimir Oltean static int sja1105_init_static_fdb(struct sja1105_private *priv)
1888aa9ebccSVladimir Oltean {
1898aa9ebccSVladimir Oltean 	struct sja1105_table *table;
1908aa9ebccSVladimir Oltean 
1918aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
1928aa9ebccSVladimir Oltean 
193291d1e72SVladimir Oltean 	/* We only populate the FDB table through dynamic
194291d1e72SVladimir Oltean 	 * L2 Address Lookup entries
195291d1e72SVladimir Oltean 	 */
1968aa9ebccSVladimir Oltean 	if (table->entry_count) {
1978aa9ebccSVladimir Oltean 		kfree(table->entries);
1988aa9ebccSVladimir Oltean 		table->entry_count = 0;
1998aa9ebccSVladimir Oltean 	}
2008aa9ebccSVladimir Oltean 	return 0;
2018aa9ebccSVladimir Oltean }
2028aa9ebccSVladimir Oltean 
2038aa9ebccSVladimir Oltean static int sja1105_init_l2_lookup_params(struct sja1105_private *priv)
2048aa9ebccSVladimir Oltean {
2058aa9ebccSVladimir Oltean 	struct sja1105_table *table;
2066c56e167SVladimir Oltean 	u64 max_fdb_entries = SJA1105_MAX_L2_LOOKUP_COUNT / SJA1105_NUM_PORTS;
2078aa9ebccSVladimir Oltean 	struct sja1105_l2_lookup_params_entry default_l2_lookup_params = {
2088456721dSVladimir Oltean 		/* Learned FDB entries are forgotten after 300 seconds */
2098456721dSVladimir Oltean 		.maxage = SJA1105_AGEING_TIME_MS(300000),
2108aa9ebccSVladimir Oltean 		/* All entries within a FDB bin are available for learning */
2118aa9ebccSVladimir Oltean 		.dyn_tbsz = SJA1105ET_FDB_BIN_SIZE,
2121da73821SVladimir Oltean 		/* And the P/Q/R/S equivalent setting: */
2131da73821SVladimir Oltean 		.start_dynspc = 0,
2146c56e167SVladimir Oltean 		.maxaddrp = {max_fdb_entries, max_fdb_entries, max_fdb_entries,
2156c56e167SVladimir Oltean 			     max_fdb_entries, max_fdb_entries, },
2168aa9ebccSVladimir Oltean 		/* 2^8 + 2^5 + 2^3 + 2^2 + 2^1 + 1 in Koopman notation */
2178aa9ebccSVladimir Oltean 		.poly = 0x97,
2188aa9ebccSVladimir Oltean 		/* This selects between Independent VLAN Learning (IVL) and
2198aa9ebccSVladimir Oltean 		 * Shared VLAN Learning (SVL)
2208aa9ebccSVladimir Oltean 		 */
2218aa9ebccSVladimir Oltean 		.shared_learn = false,
2228aa9ebccSVladimir Oltean 		/* Don't discard management traffic based on ENFPORT -
2238aa9ebccSVladimir Oltean 		 * we don't perform SMAC port enforcement anyway, so
2248aa9ebccSVladimir Oltean 		 * what we are setting here doesn't matter.
2258aa9ebccSVladimir Oltean 		 */
2268aa9ebccSVladimir Oltean 		.no_enf_hostprt = false,
2278aa9ebccSVladimir Oltean 		/* Don't learn SMAC for mac_fltres1 and mac_fltres0.
2288aa9ebccSVladimir Oltean 		 * Maybe correlate with no_linklocal_learn from bridge driver?
2298aa9ebccSVladimir Oltean 		 */
2308aa9ebccSVladimir Oltean 		.no_mgmt_learn = true,
2311da73821SVladimir Oltean 		/* P/Q/R/S only */
2321da73821SVladimir Oltean 		.use_static = true,
2331da73821SVladimir Oltean 		/* Dynamically learned FDB entries can overwrite other (older)
2341da73821SVladimir Oltean 		 * dynamic FDB entries
2351da73821SVladimir Oltean 		 */
2361da73821SVladimir Oltean 		.owr_dyn = true,
2371da73821SVladimir Oltean 		.drpnolearn = true,
2388aa9ebccSVladimir Oltean 	};
2398aa9ebccSVladimir Oltean 
2408aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
2418aa9ebccSVladimir Oltean 
2428aa9ebccSVladimir Oltean 	if (table->entry_count) {
2438aa9ebccSVladimir Oltean 		kfree(table->entries);
2448aa9ebccSVladimir Oltean 		table->entry_count = 0;
2458aa9ebccSVladimir Oltean 	}
2468aa9ebccSVladimir Oltean 
2478aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT,
2488aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
2498aa9ebccSVladimir Oltean 	if (!table->entries)
2508aa9ebccSVladimir Oltean 		return -ENOMEM;
2518aa9ebccSVladimir Oltean 
2528aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT;
2538aa9ebccSVladimir Oltean 
2548aa9ebccSVladimir Oltean 	/* This table only has a single entry */
2558aa9ebccSVladimir Oltean 	((struct sja1105_l2_lookup_params_entry *)table->entries)[0] =
2568aa9ebccSVladimir Oltean 				default_l2_lookup_params;
2578aa9ebccSVladimir Oltean 
2588aa9ebccSVladimir Oltean 	return 0;
2598aa9ebccSVladimir Oltean }
2608aa9ebccSVladimir Oltean 
2618aa9ebccSVladimir Oltean static int sja1105_init_static_vlan(struct sja1105_private *priv)
2628aa9ebccSVladimir Oltean {
2638aa9ebccSVladimir Oltean 	struct sja1105_table *table;
2648aa9ebccSVladimir Oltean 	struct sja1105_vlan_lookup_entry pvid = {
2658aa9ebccSVladimir Oltean 		.ving_mirr = 0,
2668aa9ebccSVladimir Oltean 		.vegr_mirr = 0,
2678aa9ebccSVladimir Oltean 		.vmemb_port = 0,
2688aa9ebccSVladimir Oltean 		.vlan_bc = 0,
2698aa9ebccSVladimir Oltean 		.tag_port = 0,
270e3502b82SVladimir Oltean 		.vlanid = 1,
2718aa9ebccSVladimir Oltean 	};
2728aa9ebccSVladimir Oltean 	int i;
2738aa9ebccSVladimir Oltean 
2748aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
2758aa9ebccSVladimir Oltean 
276e3502b82SVladimir Oltean 	/* The static VLAN table will only contain the initial pvid of 1.
2776666cebcSVladimir Oltean 	 * All other VLANs are to be configured through dynamic entries,
2786666cebcSVladimir Oltean 	 * and kept in the static configuration table as backing memory.
2798aa9ebccSVladimir Oltean 	 */
2808aa9ebccSVladimir Oltean 	if (table->entry_count) {
2818aa9ebccSVladimir Oltean 		kfree(table->entries);
2828aa9ebccSVladimir Oltean 		table->entry_count = 0;
2838aa9ebccSVladimir Oltean 	}
2848aa9ebccSVladimir Oltean 
2858aa9ebccSVladimir Oltean 	table->entries = kcalloc(1, table->ops->unpacked_entry_size,
2868aa9ebccSVladimir Oltean 				 GFP_KERNEL);
2878aa9ebccSVladimir Oltean 	if (!table->entries)
2888aa9ebccSVladimir Oltean 		return -ENOMEM;
2898aa9ebccSVladimir Oltean 
2908aa9ebccSVladimir Oltean 	table->entry_count = 1;
2918aa9ebccSVladimir Oltean 
292e3502b82SVladimir Oltean 	/* VLAN 1: all DT-defined ports are members; no restrictions on
2938aa9ebccSVladimir Oltean 	 * forwarding; always transmit priority-tagged frames as untagged.
2948aa9ebccSVladimir Oltean 	 */
2958aa9ebccSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
2968aa9ebccSVladimir Oltean 		pvid.vmemb_port |= BIT(i);
2978aa9ebccSVladimir Oltean 		pvid.vlan_bc |= BIT(i);
2988aa9ebccSVladimir Oltean 		pvid.tag_port &= ~BIT(i);
2998aa9ebccSVladimir Oltean 	}
3008aa9ebccSVladimir Oltean 
3018aa9ebccSVladimir Oltean 	((struct sja1105_vlan_lookup_entry *)table->entries)[0] = pvid;
3028aa9ebccSVladimir Oltean 	return 0;
3038aa9ebccSVladimir Oltean }
3048aa9ebccSVladimir Oltean 
3058aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding(struct sja1105_private *priv)
3068aa9ebccSVladimir Oltean {
3078aa9ebccSVladimir Oltean 	struct sja1105_l2_forwarding_entry *l2fwd;
3088aa9ebccSVladimir Oltean 	struct sja1105_table *table;
3098aa9ebccSVladimir Oltean 	int i, j;
3108aa9ebccSVladimir Oltean 
3118aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING];
3128aa9ebccSVladimir Oltean 
3138aa9ebccSVladimir Oltean 	if (table->entry_count) {
3148aa9ebccSVladimir Oltean 		kfree(table->entries);
3158aa9ebccSVladimir Oltean 		table->entry_count = 0;
3168aa9ebccSVladimir Oltean 	}
3178aa9ebccSVladimir Oltean 
3188aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_L2_FORWARDING_COUNT,
3198aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
3208aa9ebccSVladimir Oltean 	if (!table->entries)
3218aa9ebccSVladimir Oltean 		return -ENOMEM;
3228aa9ebccSVladimir Oltean 
3238aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_L2_FORWARDING_COUNT;
3248aa9ebccSVladimir Oltean 
3258aa9ebccSVladimir Oltean 	l2fwd = table->entries;
3268aa9ebccSVladimir Oltean 
3278aa9ebccSVladimir Oltean 	/* First 5 entries define the forwarding rules */
3288aa9ebccSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
3298aa9ebccSVladimir Oltean 		unsigned int upstream = dsa_upstream_port(priv->ds, i);
3308aa9ebccSVladimir Oltean 
3318aa9ebccSVladimir Oltean 		for (j = 0; j < SJA1105_NUM_TC; j++)
3328aa9ebccSVladimir Oltean 			l2fwd[i].vlan_pmap[j] = j;
3338aa9ebccSVladimir Oltean 
3348aa9ebccSVladimir Oltean 		if (i == upstream)
3358aa9ebccSVladimir Oltean 			continue;
3368aa9ebccSVladimir Oltean 
3378aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2fwd, i, upstream, true);
3388aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2fwd, upstream, i, true);
3398aa9ebccSVladimir Oltean 	}
3408aa9ebccSVladimir Oltean 	/* Next 8 entries define VLAN PCP mapping from ingress to egress.
3418aa9ebccSVladimir Oltean 	 * Create a one-to-one mapping.
3428aa9ebccSVladimir Oltean 	 */
3438aa9ebccSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_TC; i++)
3448aa9ebccSVladimir Oltean 		for (j = 0; j < SJA1105_NUM_PORTS; j++)
3458aa9ebccSVladimir Oltean 			l2fwd[SJA1105_NUM_PORTS + i].vlan_pmap[j] = i;
3468aa9ebccSVladimir Oltean 
3478aa9ebccSVladimir Oltean 	return 0;
3488aa9ebccSVladimir Oltean }
3498aa9ebccSVladimir Oltean 
3508aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding_params(struct sja1105_private *priv)
3518aa9ebccSVladimir Oltean {
3528aa9ebccSVladimir Oltean 	struct sja1105_l2_forwarding_params_entry default_l2fwd_params = {
3538aa9ebccSVladimir Oltean 		/* Disallow dynamic reconfiguration of vlan_pmap */
3548aa9ebccSVladimir Oltean 		.max_dynp = 0,
3558aa9ebccSVladimir Oltean 		/* Use a single memory partition for all ingress queues */
3568aa9ebccSVladimir Oltean 		.part_spc = { SJA1105_MAX_FRAME_MEMORY, 0, 0, 0, 0, 0, 0, 0 },
3578aa9ebccSVladimir Oltean 	};
3588aa9ebccSVladimir Oltean 	struct sja1105_table *table;
3598aa9ebccSVladimir Oltean 
3608aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS];
3618aa9ebccSVladimir Oltean 
3628aa9ebccSVladimir Oltean 	if (table->entry_count) {
3638aa9ebccSVladimir Oltean 		kfree(table->entries);
3648aa9ebccSVladimir Oltean 		table->entry_count = 0;
3658aa9ebccSVladimir Oltean 	}
3668aa9ebccSVladimir Oltean 
3678aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT,
3688aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
3698aa9ebccSVladimir Oltean 	if (!table->entries)
3708aa9ebccSVladimir Oltean 		return -ENOMEM;
3718aa9ebccSVladimir Oltean 
3728aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT;
3738aa9ebccSVladimir Oltean 
3748aa9ebccSVladimir Oltean 	/* This table only has a single entry */
3758aa9ebccSVladimir Oltean 	((struct sja1105_l2_forwarding_params_entry *)table->entries)[0] =
3768aa9ebccSVladimir Oltean 				default_l2fwd_params;
3778aa9ebccSVladimir Oltean 
3788aa9ebccSVladimir Oltean 	return 0;
3798aa9ebccSVladimir Oltean }
3808aa9ebccSVladimir Oltean 
3818aa9ebccSVladimir Oltean static int sja1105_init_general_params(struct sja1105_private *priv)
3828aa9ebccSVladimir Oltean {
3838aa9ebccSVladimir Oltean 	struct sja1105_general_params_entry default_general_params = {
3848aa9ebccSVladimir Oltean 		/* Disallow dynamic changing of the mirror port */
3858aa9ebccSVladimir Oltean 		.mirr_ptacu = 0,
3868aa9ebccSVladimir Oltean 		.switchid = priv->ds->index,
3878aa9ebccSVladimir Oltean 		/* Priority queue for link-local frames trapped to CPU */
38808fde09aSVladimir Oltean 		.hostprio = 7,
3898aa9ebccSVladimir Oltean 		.mac_fltres1 = SJA1105_LINKLOCAL_FILTER_A,
3908aa9ebccSVladimir Oltean 		.mac_flt1    = SJA1105_LINKLOCAL_FILTER_A_MASK,
39142824463SVladimir Oltean 		.incl_srcpt1 = false,
3928aa9ebccSVladimir Oltean 		.send_meta1  = false,
3938aa9ebccSVladimir Oltean 		.mac_fltres0 = SJA1105_LINKLOCAL_FILTER_B,
3948aa9ebccSVladimir Oltean 		.mac_flt0    = SJA1105_LINKLOCAL_FILTER_B_MASK,
39542824463SVladimir Oltean 		.incl_srcpt0 = false,
3968aa9ebccSVladimir Oltean 		.send_meta0  = false,
3978aa9ebccSVladimir Oltean 		/* The destination for traffic matching mac_fltres1 and
3988aa9ebccSVladimir Oltean 		 * mac_fltres0 on all ports except host_port. Such traffic
3998aa9ebccSVladimir Oltean 		 * receieved on host_port itself would be dropped, except
4008aa9ebccSVladimir Oltean 		 * by installing a temporary 'management route'
4018aa9ebccSVladimir Oltean 		 */
4028aa9ebccSVladimir Oltean 		.host_port = dsa_upstream_port(priv->ds, 0),
4038aa9ebccSVladimir Oltean 		/* Same as host port */
4048aa9ebccSVladimir Oltean 		.mirr_port = dsa_upstream_port(priv->ds, 0),
4058aa9ebccSVladimir Oltean 		/* Link-local traffic received on casc_port will be forwarded
4068aa9ebccSVladimir Oltean 		 * to host_port without embedding the source port and device ID
4078aa9ebccSVladimir Oltean 		 * info in the destination MAC address (presumably because it
4088aa9ebccSVladimir Oltean 		 * is a cascaded port and a downstream SJA switch already did
4098aa9ebccSVladimir Oltean 		 * that). Default to an invalid port (to disable the feature)
4108aa9ebccSVladimir Oltean 		 * and overwrite this if we find any DSA (cascaded) ports.
4118aa9ebccSVladimir Oltean 		 */
4128aa9ebccSVladimir Oltean 		.casc_port = SJA1105_NUM_PORTS,
4138aa9ebccSVladimir Oltean 		/* No TTEthernet */
4148aa9ebccSVladimir Oltean 		.vllupformat = 0,
4158aa9ebccSVladimir Oltean 		.vlmarker = 0,
4168aa9ebccSVladimir Oltean 		.vlmask = 0,
4178aa9ebccSVladimir Oltean 		/* Only update correctionField for 1-step PTP (L2 transport) */
4188aa9ebccSVladimir Oltean 		.ignore2stf = 0,
4196666cebcSVladimir Oltean 		/* Forcefully disable VLAN filtering by telling
4206666cebcSVladimir Oltean 		 * the switch that VLAN has a different EtherType.
4216666cebcSVladimir Oltean 		 */
4226666cebcSVladimir Oltean 		.tpid = ETH_P_SJA1105,
4236666cebcSVladimir Oltean 		.tpid2 = ETH_P_SJA1105,
4248aa9ebccSVladimir Oltean 	};
4258aa9ebccSVladimir Oltean 	struct sja1105_table *table;
426227d07a0SVladimir Oltean 	int i, k = 0;
4278aa9ebccSVladimir Oltean 
428227d07a0SVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
4298aa9ebccSVladimir Oltean 		if (dsa_is_dsa_port(priv->ds, i))
4308aa9ebccSVladimir Oltean 			default_general_params.casc_port = i;
431227d07a0SVladimir Oltean 		else if (dsa_is_user_port(priv->ds, i))
432227d07a0SVladimir Oltean 			priv->ports[i].mgmt_slot = k++;
433227d07a0SVladimir Oltean 	}
4348aa9ebccSVladimir Oltean 
4358aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
4368aa9ebccSVladimir Oltean 
4378aa9ebccSVladimir Oltean 	if (table->entry_count) {
4388aa9ebccSVladimir Oltean 		kfree(table->entries);
4398aa9ebccSVladimir Oltean 		table->entry_count = 0;
4408aa9ebccSVladimir Oltean 	}
4418aa9ebccSVladimir Oltean 
4428aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_GENERAL_PARAMS_COUNT,
4438aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
4448aa9ebccSVladimir Oltean 	if (!table->entries)
4458aa9ebccSVladimir Oltean 		return -ENOMEM;
4468aa9ebccSVladimir Oltean 
4478aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_GENERAL_PARAMS_COUNT;
4488aa9ebccSVladimir Oltean 
4498aa9ebccSVladimir Oltean 	/* This table only has a single entry */
4508aa9ebccSVladimir Oltean 	((struct sja1105_general_params_entry *)table->entries)[0] =
4518aa9ebccSVladimir Oltean 				default_general_params;
4528aa9ebccSVladimir Oltean 
4538aa9ebccSVladimir Oltean 	return 0;
4548aa9ebccSVladimir Oltean }
4558aa9ebccSVladimir Oltean 
4568aa9ebccSVladimir Oltean #define SJA1105_RATE_MBPS(speed) (((speed) * 64000) / 1000)
4578aa9ebccSVladimir Oltean 
4588aa9ebccSVladimir Oltean static inline void
4598aa9ebccSVladimir Oltean sja1105_setup_policer(struct sja1105_l2_policing_entry *policing,
4608aa9ebccSVladimir Oltean 		      int index)
4618aa9ebccSVladimir Oltean {
4628aa9ebccSVladimir Oltean 	policing[index].sharindx = index;
4638aa9ebccSVladimir Oltean 	policing[index].smax = 65535; /* Burst size in bytes */
4648aa9ebccSVladimir Oltean 	policing[index].rate = SJA1105_RATE_MBPS(1000);
4658aa9ebccSVladimir Oltean 	policing[index].maxlen = ETH_FRAME_LEN + VLAN_HLEN + ETH_FCS_LEN;
4668aa9ebccSVladimir Oltean 	policing[index].partition = 0;
4678aa9ebccSVladimir Oltean }
4688aa9ebccSVladimir Oltean 
4698aa9ebccSVladimir Oltean static int sja1105_init_l2_policing(struct sja1105_private *priv)
4708aa9ebccSVladimir Oltean {
4718aa9ebccSVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
4728aa9ebccSVladimir Oltean 	struct sja1105_table *table;
4738aa9ebccSVladimir Oltean 	int i, j, k;
4748aa9ebccSVladimir Oltean 
4758aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_POLICING];
4768aa9ebccSVladimir Oltean 
4778aa9ebccSVladimir Oltean 	/* Discard previous L2 Policing Table */
4788aa9ebccSVladimir Oltean 	if (table->entry_count) {
4798aa9ebccSVladimir Oltean 		kfree(table->entries);
4808aa9ebccSVladimir Oltean 		table->entry_count = 0;
4818aa9ebccSVladimir Oltean 	}
4828aa9ebccSVladimir Oltean 
4838aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_L2_POLICING_COUNT,
4848aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
4858aa9ebccSVladimir Oltean 	if (!table->entries)
4868aa9ebccSVladimir Oltean 		return -ENOMEM;
4878aa9ebccSVladimir Oltean 
4888aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_L2_POLICING_COUNT;
4898aa9ebccSVladimir Oltean 
4908aa9ebccSVladimir Oltean 	policing = table->entries;
4918aa9ebccSVladimir Oltean 
4928aa9ebccSVladimir Oltean 	/* k sweeps through all unicast policers (0-39).
4938aa9ebccSVladimir Oltean 	 * bcast sweeps through policers 40-44.
4948aa9ebccSVladimir Oltean 	 */
4958aa9ebccSVladimir Oltean 	for (i = 0, k = 0; i < SJA1105_NUM_PORTS; i++) {
4968aa9ebccSVladimir Oltean 		int bcast = (SJA1105_NUM_PORTS * SJA1105_NUM_TC) + i;
4978aa9ebccSVladimir Oltean 
4988aa9ebccSVladimir Oltean 		for (j = 0; j < SJA1105_NUM_TC; j++, k++)
4998aa9ebccSVladimir Oltean 			sja1105_setup_policer(policing, k);
5008aa9ebccSVladimir Oltean 
5018aa9ebccSVladimir Oltean 		/* Set up this port's policer for broadcast traffic */
5028aa9ebccSVladimir Oltean 		sja1105_setup_policer(policing, bcast);
5038aa9ebccSVladimir Oltean 	}
5048aa9ebccSVladimir Oltean 	return 0;
5058aa9ebccSVladimir Oltean }
5068aa9ebccSVladimir Oltean 
50724c01949SVladimir Oltean static int sja1105_init_avb_params(struct sja1105_private *priv,
50824c01949SVladimir Oltean 				   bool on)
50924c01949SVladimir Oltean {
51024c01949SVladimir Oltean 	struct sja1105_avb_params_entry *avb;
51124c01949SVladimir Oltean 	struct sja1105_table *table;
51224c01949SVladimir Oltean 
51324c01949SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_AVB_PARAMS];
51424c01949SVladimir Oltean 
51524c01949SVladimir Oltean 	/* Discard previous AVB Parameters Table */
51624c01949SVladimir Oltean 	if (table->entry_count) {
51724c01949SVladimir Oltean 		kfree(table->entries);
51824c01949SVladimir Oltean 		table->entry_count = 0;
51924c01949SVladimir Oltean 	}
52024c01949SVladimir Oltean 
52124c01949SVladimir Oltean 	/* Configure the reception of meta frames only if requested */
52224c01949SVladimir Oltean 	if (!on)
52324c01949SVladimir Oltean 		return 0;
52424c01949SVladimir Oltean 
52524c01949SVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_AVB_PARAMS_COUNT,
52624c01949SVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
52724c01949SVladimir Oltean 	if (!table->entries)
52824c01949SVladimir Oltean 		return -ENOMEM;
52924c01949SVladimir Oltean 
53024c01949SVladimir Oltean 	table->entry_count = SJA1105_MAX_AVB_PARAMS_COUNT;
53124c01949SVladimir Oltean 
53224c01949SVladimir Oltean 	avb = table->entries;
53324c01949SVladimir Oltean 
53424c01949SVladimir Oltean 	avb->destmeta = SJA1105_META_DMAC;
53524c01949SVladimir Oltean 	avb->srcmeta  = SJA1105_META_SMAC;
53624c01949SVladimir Oltean 
53724c01949SVladimir Oltean 	return 0;
53824c01949SVladimir Oltean }
53924c01949SVladimir Oltean 
5408aa9ebccSVladimir Oltean static int sja1105_static_config_load(struct sja1105_private *priv,
5418aa9ebccSVladimir Oltean 				      struct sja1105_dt_port *ports)
5428aa9ebccSVladimir Oltean {
5438aa9ebccSVladimir Oltean 	int rc;
5448aa9ebccSVladimir Oltean 
5458aa9ebccSVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
5468aa9ebccSVladimir Oltean 	rc = sja1105_static_config_init(&priv->static_config,
5478aa9ebccSVladimir Oltean 					priv->info->static_ops,
5488aa9ebccSVladimir Oltean 					priv->info->device_id);
5498aa9ebccSVladimir Oltean 	if (rc)
5508aa9ebccSVladimir Oltean 		return rc;
5518aa9ebccSVladimir Oltean 
5528aa9ebccSVladimir Oltean 	/* Build static configuration */
5538aa9ebccSVladimir Oltean 	rc = sja1105_init_mac_settings(priv);
5548aa9ebccSVladimir Oltean 	if (rc < 0)
5558aa9ebccSVladimir Oltean 		return rc;
5568aa9ebccSVladimir Oltean 	rc = sja1105_init_mii_settings(priv, ports);
5578aa9ebccSVladimir Oltean 	if (rc < 0)
5588aa9ebccSVladimir Oltean 		return rc;
5598aa9ebccSVladimir Oltean 	rc = sja1105_init_static_fdb(priv);
5608aa9ebccSVladimir Oltean 	if (rc < 0)
5618aa9ebccSVladimir Oltean 		return rc;
5628aa9ebccSVladimir Oltean 	rc = sja1105_init_static_vlan(priv);
5638aa9ebccSVladimir Oltean 	if (rc < 0)
5648aa9ebccSVladimir Oltean 		return rc;
5658aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_lookup_params(priv);
5668aa9ebccSVladimir Oltean 	if (rc < 0)
5678aa9ebccSVladimir Oltean 		return rc;
5688aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_forwarding(priv);
5698aa9ebccSVladimir Oltean 	if (rc < 0)
5708aa9ebccSVladimir Oltean 		return rc;
5718aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_forwarding_params(priv);
5728aa9ebccSVladimir Oltean 	if (rc < 0)
5738aa9ebccSVladimir Oltean 		return rc;
5748aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_policing(priv);
5758aa9ebccSVladimir Oltean 	if (rc < 0)
5768aa9ebccSVladimir Oltean 		return rc;
5778aa9ebccSVladimir Oltean 	rc = sja1105_init_general_params(priv);
5788aa9ebccSVladimir Oltean 	if (rc < 0)
5798aa9ebccSVladimir Oltean 		return rc;
58024c01949SVladimir Oltean 	rc = sja1105_init_avb_params(priv, false);
58124c01949SVladimir Oltean 	if (rc < 0)
58224c01949SVladimir Oltean 		return rc;
5838aa9ebccSVladimir Oltean 
5848aa9ebccSVladimir Oltean 	/* Send initial configuration to hardware via SPI */
5858aa9ebccSVladimir Oltean 	return sja1105_static_config_upload(priv);
5868aa9ebccSVladimir Oltean }
5878aa9ebccSVladimir Oltean 
588f5b8631cSVladimir Oltean static int sja1105_parse_rgmii_delays(struct sja1105_private *priv,
589f5b8631cSVladimir Oltean 				      const struct sja1105_dt_port *ports)
590f5b8631cSVladimir Oltean {
591f5b8631cSVladimir Oltean 	int i;
592f5b8631cSVladimir Oltean 
593f5b8631cSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
594f5b8631cSVladimir Oltean 		if (ports->role == XMII_MAC)
595f5b8631cSVladimir Oltean 			continue;
596f5b8631cSVladimir Oltean 
597f5b8631cSVladimir Oltean 		if (ports->phy_mode == PHY_INTERFACE_MODE_RGMII_RXID ||
598f5b8631cSVladimir Oltean 		    ports->phy_mode == PHY_INTERFACE_MODE_RGMII_ID)
599f5b8631cSVladimir Oltean 			priv->rgmii_rx_delay[i] = true;
600f5b8631cSVladimir Oltean 
601f5b8631cSVladimir Oltean 		if (ports->phy_mode == PHY_INTERFACE_MODE_RGMII_TXID ||
602f5b8631cSVladimir Oltean 		    ports->phy_mode == PHY_INTERFACE_MODE_RGMII_ID)
603f5b8631cSVladimir Oltean 			priv->rgmii_tx_delay[i] = true;
604f5b8631cSVladimir Oltean 
605f5b8631cSVladimir Oltean 		if ((priv->rgmii_rx_delay[i] || priv->rgmii_tx_delay[i]) &&
606f5b8631cSVladimir Oltean 		     !priv->info->setup_rgmii_delay)
607f5b8631cSVladimir Oltean 			return -EINVAL;
608f5b8631cSVladimir Oltean 	}
609f5b8631cSVladimir Oltean 	return 0;
610f5b8631cSVladimir Oltean }
611f5b8631cSVladimir Oltean 
6128aa9ebccSVladimir Oltean static int sja1105_parse_ports_node(struct sja1105_private *priv,
6138aa9ebccSVladimir Oltean 				    struct sja1105_dt_port *ports,
6148aa9ebccSVladimir Oltean 				    struct device_node *ports_node)
6158aa9ebccSVladimir Oltean {
6168aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
6178aa9ebccSVladimir Oltean 	struct device_node *child;
6188aa9ebccSVladimir Oltean 
6198aa9ebccSVladimir Oltean 	for_each_child_of_node(ports_node, child) {
6208aa9ebccSVladimir Oltean 		struct device_node *phy_node;
6218aa9ebccSVladimir Oltean 		int phy_mode;
6228aa9ebccSVladimir Oltean 		u32 index;
6238aa9ebccSVladimir Oltean 
6248aa9ebccSVladimir Oltean 		/* Get switch port number from DT */
6258aa9ebccSVladimir Oltean 		if (of_property_read_u32(child, "reg", &index) < 0) {
6268aa9ebccSVladimir Oltean 			dev_err(dev, "Port number not defined in device tree "
6278aa9ebccSVladimir Oltean 				"(property \"reg\")\n");
6288aa9ebccSVladimir Oltean 			return -ENODEV;
6298aa9ebccSVladimir Oltean 		}
6308aa9ebccSVladimir Oltean 
6318aa9ebccSVladimir Oltean 		/* Get PHY mode from DT */
6328aa9ebccSVladimir Oltean 		phy_mode = of_get_phy_mode(child);
6338aa9ebccSVladimir Oltean 		if (phy_mode < 0) {
6348aa9ebccSVladimir Oltean 			dev_err(dev, "Failed to read phy-mode or "
6358aa9ebccSVladimir Oltean 				"phy-interface-type property for port %d\n",
6368aa9ebccSVladimir Oltean 				index);
6378aa9ebccSVladimir Oltean 			return -ENODEV;
6388aa9ebccSVladimir Oltean 		}
6398aa9ebccSVladimir Oltean 		ports[index].phy_mode = phy_mode;
6408aa9ebccSVladimir Oltean 
6418aa9ebccSVladimir Oltean 		phy_node = of_parse_phandle(child, "phy-handle", 0);
6428aa9ebccSVladimir Oltean 		if (!phy_node) {
6438aa9ebccSVladimir Oltean 			if (!of_phy_is_fixed_link(child)) {
6448aa9ebccSVladimir Oltean 				dev_err(dev, "phy-handle or fixed-link "
6458aa9ebccSVladimir Oltean 					"properties missing!\n");
6468aa9ebccSVladimir Oltean 				return -ENODEV;
6478aa9ebccSVladimir Oltean 			}
6488aa9ebccSVladimir Oltean 			/* phy-handle is missing, but fixed-link isn't.
6498aa9ebccSVladimir Oltean 			 * So it's a fixed link. Default to PHY role.
6508aa9ebccSVladimir Oltean 			 */
6518aa9ebccSVladimir Oltean 			ports[index].role = XMII_PHY;
6528aa9ebccSVladimir Oltean 		} else {
6538aa9ebccSVladimir Oltean 			/* phy-handle present => put port in MAC role */
6548aa9ebccSVladimir Oltean 			ports[index].role = XMII_MAC;
6558aa9ebccSVladimir Oltean 			of_node_put(phy_node);
6568aa9ebccSVladimir Oltean 		}
6578aa9ebccSVladimir Oltean 
6588aa9ebccSVladimir Oltean 		/* The MAC/PHY role can be overridden with explicit bindings */
6598aa9ebccSVladimir Oltean 		if (of_property_read_bool(child, "sja1105,role-mac"))
6608aa9ebccSVladimir Oltean 			ports[index].role = XMII_MAC;
6618aa9ebccSVladimir Oltean 		else if (of_property_read_bool(child, "sja1105,role-phy"))
6628aa9ebccSVladimir Oltean 			ports[index].role = XMII_PHY;
6638aa9ebccSVladimir Oltean 	}
6648aa9ebccSVladimir Oltean 
6658aa9ebccSVladimir Oltean 	return 0;
6668aa9ebccSVladimir Oltean }
6678aa9ebccSVladimir Oltean 
6688aa9ebccSVladimir Oltean static int sja1105_parse_dt(struct sja1105_private *priv,
6698aa9ebccSVladimir Oltean 			    struct sja1105_dt_port *ports)
6708aa9ebccSVladimir Oltean {
6718aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
6728aa9ebccSVladimir Oltean 	struct device_node *switch_node = dev->of_node;
6738aa9ebccSVladimir Oltean 	struct device_node *ports_node;
6748aa9ebccSVladimir Oltean 	int rc;
6758aa9ebccSVladimir Oltean 
6768aa9ebccSVladimir Oltean 	ports_node = of_get_child_by_name(switch_node, "ports");
6778aa9ebccSVladimir Oltean 	if (!ports_node) {
6788aa9ebccSVladimir Oltean 		dev_err(dev, "Incorrect bindings: absent \"ports\" node\n");
6798aa9ebccSVladimir Oltean 		return -ENODEV;
6808aa9ebccSVladimir Oltean 	}
6818aa9ebccSVladimir Oltean 
6828aa9ebccSVladimir Oltean 	rc = sja1105_parse_ports_node(priv, ports, ports_node);
6838aa9ebccSVladimir Oltean 	of_node_put(ports_node);
6848aa9ebccSVladimir Oltean 
6858aa9ebccSVladimir Oltean 	return rc;
6868aa9ebccSVladimir Oltean }
6878aa9ebccSVladimir Oltean 
688c44d0535SVladimir Oltean /* Convert link speed from SJA1105 to ethtool encoding */
6898aa9ebccSVladimir Oltean static int sja1105_speed[] = {
690c44d0535SVladimir Oltean 	[SJA1105_SPEED_AUTO]		= SPEED_UNKNOWN,
691c44d0535SVladimir Oltean 	[SJA1105_SPEED_10MBPS]		= SPEED_10,
692c44d0535SVladimir Oltean 	[SJA1105_SPEED_100MBPS]		= SPEED_100,
693c44d0535SVladimir Oltean 	[SJA1105_SPEED_1000MBPS]	= SPEED_1000,
6948aa9ebccSVladimir Oltean };
6958aa9ebccSVladimir Oltean 
6968400cff6SVladimir Oltean /* Set link speed in the MAC configuration for a specific port. */
6978aa9ebccSVladimir Oltean static int sja1105_adjust_port_config(struct sja1105_private *priv, int port,
6988400cff6SVladimir Oltean 				      int speed_mbps)
6998aa9ebccSVladimir Oltean {
7008aa9ebccSVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
7018aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
7028aa9ebccSVladimir Oltean 	struct device *dev = priv->ds->dev;
7038aa9ebccSVladimir Oltean 	sja1105_phy_interface_t phy_mode;
7048aa9ebccSVladimir Oltean 	sja1105_speed_t speed;
7058aa9ebccSVladimir Oltean 	int rc;
7068aa9ebccSVladimir Oltean 
7078400cff6SVladimir Oltean 	/* On P/Q/R/S, one can read from the device via the MAC reconfiguration
7088400cff6SVladimir Oltean 	 * tables. On E/T, MAC reconfig tables are not readable, only writable.
7098400cff6SVladimir Oltean 	 * We have to *know* what the MAC looks like.  For the sake of keeping
7108400cff6SVladimir Oltean 	 * the code common, we'll use the static configuration tables as a
7118400cff6SVladimir Oltean 	 * reasonable approximation for both E/T and P/Q/R/S.
7128400cff6SVladimir Oltean 	 */
7138aa9ebccSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
7148400cff6SVladimir Oltean 	mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
7158aa9ebccSVladimir Oltean 
716f4cfcfbdSVladimir Oltean 	switch (speed_mbps) {
717c44d0535SVladimir Oltean 	case SPEED_UNKNOWN:
718f4cfcfbdSVladimir Oltean 		/* No speed update requested */
719f4cfcfbdSVladimir Oltean 		speed = SJA1105_SPEED_AUTO;
720f4cfcfbdSVladimir Oltean 		break;
721c44d0535SVladimir Oltean 	case SPEED_10:
722f4cfcfbdSVladimir Oltean 		speed = SJA1105_SPEED_10MBPS;
723f4cfcfbdSVladimir Oltean 		break;
724c44d0535SVladimir Oltean 	case SPEED_100:
725f4cfcfbdSVladimir Oltean 		speed = SJA1105_SPEED_100MBPS;
726f4cfcfbdSVladimir Oltean 		break;
727c44d0535SVladimir Oltean 	case SPEED_1000:
728f4cfcfbdSVladimir Oltean 		speed = SJA1105_SPEED_1000MBPS;
729f4cfcfbdSVladimir Oltean 		break;
730f4cfcfbdSVladimir Oltean 	default:
7318aa9ebccSVladimir Oltean 		dev_err(dev, "Invalid speed %iMbps\n", speed_mbps);
7328aa9ebccSVladimir Oltean 		return -EINVAL;
7338aa9ebccSVladimir Oltean 	}
7348aa9ebccSVladimir Oltean 
7358400cff6SVladimir Oltean 	/* Overwrite SJA1105_SPEED_AUTO from the static MAC configuration
7368400cff6SVladimir Oltean 	 * table, since this will be used for the clocking setup, and we no
7378400cff6SVladimir Oltean 	 * longer need to store it in the static config (already told hardware
7388400cff6SVladimir Oltean 	 * we want auto during upload phase).
7398aa9ebccSVladimir Oltean 	 */
7408aa9ebccSVladimir Oltean 	mac[port].speed = speed;
7418aa9ebccSVladimir Oltean 
7428aa9ebccSVladimir Oltean 	/* Write to the dynamic reconfiguration tables */
7438400cff6SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
7448400cff6SVladimir Oltean 					  &mac[port], true);
7458aa9ebccSVladimir Oltean 	if (rc < 0) {
7468aa9ebccSVladimir Oltean 		dev_err(dev, "Failed to write MAC config: %d\n", rc);
7478aa9ebccSVladimir Oltean 		return rc;
7488aa9ebccSVladimir Oltean 	}
7498aa9ebccSVladimir Oltean 
7508aa9ebccSVladimir Oltean 	/* Reconfigure the PLLs for the RGMII interfaces (required 125 MHz at
7518aa9ebccSVladimir Oltean 	 * gigabit, 25 MHz at 100 Mbps and 2.5 MHz at 10 Mbps). For MII and
7528aa9ebccSVladimir Oltean 	 * RMII no change of the clock setup is required. Actually, changing
7538aa9ebccSVladimir Oltean 	 * the clock setup does interrupt the clock signal for a certain time
7548aa9ebccSVladimir Oltean 	 * which causes trouble for all PHYs relying on this signal.
7558aa9ebccSVladimir Oltean 	 */
7568aa9ebccSVladimir Oltean 	phy_mode = mii->xmii_mode[port];
7578aa9ebccSVladimir Oltean 	if (phy_mode != XMII_MODE_RGMII)
7588aa9ebccSVladimir Oltean 		return 0;
7598aa9ebccSVladimir Oltean 
7608aa9ebccSVladimir Oltean 	return sja1105_clocking_setup_port(priv, port);
7618aa9ebccSVladimir Oltean }
7628aa9ebccSVladimir Oltean 
763af7cd036SVladimir Oltean static void sja1105_mac_config(struct dsa_switch *ds, int port,
764af7cd036SVladimir Oltean 			       unsigned int link_an_mode,
765af7cd036SVladimir Oltean 			       const struct phylink_link_state *state)
7668aa9ebccSVladimir Oltean {
7678aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
7688aa9ebccSVladimir Oltean 
769af7cd036SVladimir Oltean 	if (!state->link)
7708400cff6SVladimir Oltean 		return;
7718400cff6SVladimir Oltean 
7728400cff6SVladimir Oltean 	sja1105_adjust_port_config(priv, port, state->speed);
7738400cff6SVladimir Oltean }
7748400cff6SVladimir Oltean 
7758400cff6SVladimir Oltean static void sja1105_mac_link_down(struct dsa_switch *ds, int port,
7768400cff6SVladimir Oltean 				  unsigned int mode,
7778400cff6SVladimir Oltean 				  phy_interface_t interface)
7788400cff6SVladimir Oltean {
7798400cff6SVladimir Oltean 	sja1105_inhibit_tx(ds->priv, BIT(port), true);
7808400cff6SVladimir Oltean }
7818400cff6SVladimir Oltean 
7828400cff6SVladimir Oltean static void sja1105_mac_link_up(struct dsa_switch *ds, int port,
7838400cff6SVladimir Oltean 				unsigned int mode,
7848400cff6SVladimir Oltean 				phy_interface_t interface,
7858400cff6SVladimir Oltean 				struct phy_device *phydev)
7868400cff6SVladimir Oltean {
7878400cff6SVladimir Oltean 	sja1105_inhibit_tx(ds->priv, BIT(port), false);
7888aa9ebccSVladimir Oltean }
7898aa9ebccSVladimir Oltean 
790ad9f299aSVladimir Oltean static void sja1105_phylink_validate(struct dsa_switch *ds, int port,
791ad9f299aSVladimir Oltean 				     unsigned long *supported,
792ad9f299aSVladimir Oltean 				     struct phylink_link_state *state)
793ad9f299aSVladimir Oltean {
794ad9f299aSVladimir Oltean 	/* Construct a new mask which exhaustively contains all link features
795ad9f299aSVladimir Oltean 	 * supported by the MAC, and then apply that (logical AND) to what will
796ad9f299aSVladimir Oltean 	 * be sent to the PHY for "marketing".
797ad9f299aSVladimir Oltean 	 */
798ad9f299aSVladimir Oltean 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
799ad9f299aSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
800ad9f299aSVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
801ad9f299aSVladimir Oltean 
802ad9f299aSVladimir Oltean 	mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
803ad9f299aSVladimir Oltean 
804ad9f299aSVladimir Oltean 	/* The MAC does not support pause frames, and also doesn't
805ad9f299aSVladimir Oltean 	 * support half-duplex traffic modes.
806ad9f299aSVladimir Oltean 	 */
807ad9f299aSVladimir Oltean 	phylink_set(mask, Autoneg);
808ad9f299aSVladimir Oltean 	phylink_set(mask, MII);
809ad9f299aSVladimir Oltean 	phylink_set(mask, 10baseT_Full);
810ad9f299aSVladimir Oltean 	phylink_set(mask, 100baseT_Full);
811ad9f299aSVladimir Oltean 	if (mii->xmii_mode[port] == XMII_MODE_RGMII)
812ad9f299aSVladimir Oltean 		phylink_set(mask, 1000baseT_Full);
813ad9f299aSVladimir Oltean 
814ad9f299aSVladimir Oltean 	bitmap_and(supported, supported, mask, __ETHTOOL_LINK_MODE_MASK_NBITS);
815ad9f299aSVladimir Oltean 	bitmap_and(state->advertising, state->advertising, mask,
816ad9f299aSVladimir Oltean 		   __ETHTOOL_LINK_MODE_MASK_NBITS);
817ad9f299aSVladimir Oltean }
818ad9f299aSVladimir Oltean 
81960f6053fSVladimir Oltean static int
82060f6053fSVladimir Oltean sja1105_find_static_fdb_entry(struct sja1105_private *priv, int port,
82160f6053fSVladimir Oltean 			      const struct sja1105_l2_lookup_entry *requested)
82260f6053fSVladimir Oltean {
82360f6053fSVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
82460f6053fSVladimir Oltean 	struct sja1105_table *table;
82560f6053fSVladimir Oltean 	int i;
82660f6053fSVladimir Oltean 
82760f6053fSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
82860f6053fSVladimir Oltean 	l2_lookup = table->entries;
82960f6053fSVladimir Oltean 
83060f6053fSVladimir Oltean 	for (i = 0; i < table->entry_count; i++)
83160f6053fSVladimir Oltean 		if (l2_lookup[i].macaddr == requested->macaddr &&
83260f6053fSVladimir Oltean 		    l2_lookup[i].vlanid == requested->vlanid &&
83360f6053fSVladimir Oltean 		    l2_lookup[i].destports & BIT(port))
83460f6053fSVladimir Oltean 			return i;
83560f6053fSVladimir Oltean 
83660f6053fSVladimir Oltean 	return -1;
83760f6053fSVladimir Oltean }
83860f6053fSVladimir Oltean 
83960f6053fSVladimir Oltean /* We want FDB entries added statically through the bridge command to persist
84060f6053fSVladimir Oltean  * across switch resets, which are a common thing during normal SJA1105
84160f6053fSVladimir Oltean  * operation. So we have to back them up in the static configuration tables
84260f6053fSVladimir Oltean  * and hence apply them on next static config upload... yay!
84360f6053fSVladimir Oltean  */
84460f6053fSVladimir Oltean static int
84560f6053fSVladimir Oltean sja1105_static_fdb_change(struct sja1105_private *priv, int port,
84660f6053fSVladimir Oltean 			  const struct sja1105_l2_lookup_entry *requested,
84760f6053fSVladimir Oltean 			  bool keep)
84860f6053fSVladimir Oltean {
84960f6053fSVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
85060f6053fSVladimir Oltean 	struct sja1105_table *table;
85160f6053fSVladimir Oltean 	int rc, match;
85260f6053fSVladimir Oltean 
85360f6053fSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
85460f6053fSVladimir Oltean 
85560f6053fSVladimir Oltean 	match = sja1105_find_static_fdb_entry(priv, port, requested);
85660f6053fSVladimir Oltean 	if (match < 0) {
85760f6053fSVladimir Oltean 		/* Can't delete a missing entry. */
85860f6053fSVladimir Oltean 		if (!keep)
85960f6053fSVladimir Oltean 			return 0;
86060f6053fSVladimir Oltean 
86160f6053fSVladimir Oltean 		/* No match => new entry */
86260f6053fSVladimir Oltean 		rc = sja1105_table_resize(table, table->entry_count + 1);
86360f6053fSVladimir Oltean 		if (rc)
86460f6053fSVladimir Oltean 			return rc;
86560f6053fSVladimir Oltean 
86660f6053fSVladimir Oltean 		match = table->entry_count - 1;
86760f6053fSVladimir Oltean 	}
86860f6053fSVladimir Oltean 
86960f6053fSVladimir Oltean 	/* Assign pointer after the resize (it may be new memory) */
87060f6053fSVladimir Oltean 	l2_lookup = table->entries;
87160f6053fSVladimir Oltean 
87260f6053fSVladimir Oltean 	/* We have a match.
87360f6053fSVladimir Oltean 	 * If the job was to add this FDB entry, it's already done (mostly
87460f6053fSVladimir Oltean 	 * anyway, since the port forwarding mask may have changed, case in
87560f6053fSVladimir Oltean 	 * which we update it).
87660f6053fSVladimir Oltean 	 * Otherwise we have to delete it.
87760f6053fSVladimir Oltean 	 */
87860f6053fSVladimir Oltean 	if (keep) {
87960f6053fSVladimir Oltean 		l2_lookup[match] = *requested;
88060f6053fSVladimir Oltean 		return 0;
88160f6053fSVladimir Oltean 	}
88260f6053fSVladimir Oltean 
88360f6053fSVladimir Oltean 	/* To remove, the strategy is to overwrite the element with
88460f6053fSVladimir Oltean 	 * the last one, and then reduce the array size by 1
88560f6053fSVladimir Oltean 	 */
88660f6053fSVladimir Oltean 	l2_lookup[match] = l2_lookup[table->entry_count - 1];
88760f6053fSVladimir Oltean 	return sja1105_table_resize(table, table->entry_count - 1);
88860f6053fSVladimir Oltean }
88960f6053fSVladimir Oltean 
890291d1e72SVladimir Oltean /* First-generation switches have a 4-way set associative TCAM that
891291d1e72SVladimir Oltean  * holds the FDB entries. An FDB index spans from 0 to 1023 and is comprised of
892291d1e72SVladimir Oltean  * a "bin" (grouping of 4 entries) and a "way" (an entry within a bin).
893291d1e72SVladimir Oltean  * For the placement of a newly learnt FDB entry, the switch selects the bin
894291d1e72SVladimir Oltean  * based on a hash function, and the way within that bin incrementally.
895291d1e72SVladimir Oltean  */
896291d1e72SVladimir Oltean static inline int sja1105et_fdb_index(int bin, int way)
897291d1e72SVladimir Oltean {
898291d1e72SVladimir Oltean 	return bin * SJA1105ET_FDB_BIN_SIZE + way;
899291d1e72SVladimir Oltean }
900291d1e72SVladimir Oltean 
9019dfa6911SVladimir Oltean static int sja1105et_is_fdb_entry_in_bin(struct sja1105_private *priv, int bin,
902291d1e72SVladimir Oltean 					 const u8 *addr, u16 vid,
903291d1e72SVladimir Oltean 					 struct sja1105_l2_lookup_entry *match,
904291d1e72SVladimir Oltean 					 int *last_unused)
905291d1e72SVladimir Oltean {
906291d1e72SVladimir Oltean 	int way;
907291d1e72SVladimir Oltean 
908291d1e72SVladimir Oltean 	for (way = 0; way < SJA1105ET_FDB_BIN_SIZE; way++) {
909291d1e72SVladimir Oltean 		struct sja1105_l2_lookup_entry l2_lookup = {0};
910291d1e72SVladimir Oltean 		int index = sja1105et_fdb_index(bin, way);
911291d1e72SVladimir Oltean 
912291d1e72SVladimir Oltean 		/* Skip unused entries, optionally marking them
913291d1e72SVladimir Oltean 		 * into the return value
914291d1e72SVladimir Oltean 		 */
915291d1e72SVladimir Oltean 		if (sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
916291d1e72SVladimir Oltean 						index, &l2_lookup)) {
917291d1e72SVladimir Oltean 			if (last_unused)
918291d1e72SVladimir Oltean 				*last_unused = way;
919291d1e72SVladimir Oltean 			continue;
920291d1e72SVladimir Oltean 		}
921291d1e72SVladimir Oltean 
922291d1e72SVladimir Oltean 		if (l2_lookup.macaddr == ether_addr_to_u64(addr) &&
923291d1e72SVladimir Oltean 		    l2_lookup.vlanid == vid) {
924291d1e72SVladimir Oltean 			if (match)
925291d1e72SVladimir Oltean 				*match = l2_lookup;
926291d1e72SVladimir Oltean 			return way;
927291d1e72SVladimir Oltean 		}
928291d1e72SVladimir Oltean 	}
929291d1e72SVladimir Oltean 	/* Return an invalid entry index if not found */
930291d1e72SVladimir Oltean 	return -1;
931291d1e72SVladimir Oltean }
932291d1e72SVladimir Oltean 
9339dfa6911SVladimir Oltean int sja1105et_fdb_add(struct dsa_switch *ds, int port,
934291d1e72SVladimir Oltean 		      const unsigned char *addr, u16 vid)
935291d1e72SVladimir Oltean {
936291d1e72SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
937291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
938291d1e72SVladimir Oltean 	struct device *dev = ds->dev;
939291d1e72SVladimir Oltean 	int last_unused = -1;
94060f6053fSVladimir Oltean 	int bin, way, rc;
941291d1e72SVladimir Oltean 
9429dfa6911SVladimir Oltean 	bin = sja1105et_fdb_hash(priv, addr, vid);
943291d1e72SVladimir Oltean 
9449dfa6911SVladimir Oltean 	way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid,
945291d1e72SVladimir Oltean 					    &l2_lookup, &last_unused);
946291d1e72SVladimir Oltean 	if (way >= 0) {
947291d1e72SVladimir Oltean 		/* We have an FDB entry. Is our port in the destination
948291d1e72SVladimir Oltean 		 * mask? If yes, we need to do nothing. If not, we need
949291d1e72SVladimir Oltean 		 * to rewrite the entry by adding this port to it.
950291d1e72SVladimir Oltean 		 */
951291d1e72SVladimir Oltean 		if (l2_lookup.destports & BIT(port))
952291d1e72SVladimir Oltean 			return 0;
953291d1e72SVladimir Oltean 		l2_lookup.destports |= BIT(port);
954291d1e72SVladimir Oltean 	} else {
955291d1e72SVladimir Oltean 		int index = sja1105et_fdb_index(bin, way);
956291d1e72SVladimir Oltean 
957291d1e72SVladimir Oltean 		/* We don't have an FDB entry. We construct a new one and
958291d1e72SVladimir Oltean 		 * try to find a place for it within the FDB table.
959291d1e72SVladimir Oltean 		 */
960291d1e72SVladimir Oltean 		l2_lookup.macaddr = ether_addr_to_u64(addr);
961291d1e72SVladimir Oltean 		l2_lookup.destports = BIT(port);
962291d1e72SVladimir Oltean 		l2_lookup.vlanid = vid;
963291d1e72SVladimir Oltean 
964291d1e72SVladimir Oltean 		if (last_unused >= 0) {
965291d1e72SVladimir Oltean 			way = last_unused;
966291d1e72SVladimir Oltean 		} else {
967291d1e72SVladimir Oltean 			/* Bin is full, need to evict somebody.
968291d1e72SVladimir Oltean 			 * Choose victim at random. If you get these messages
969291d1e72SVladimir Oltean 			 * often, you may need to consider changing the
970291d1e72SVladimir Oltean 			 * distribution function:
971291d1e72SVladimir Oltean 			 * static_config[BLK_IDX_L2_LOOKUP_PARAMS].entries->poly
972291d1e72SVladimir Oltean 			 */
973291d1e72SVladimir Oltean 			get_random_bytes(&way, sizeof(u8));
974291d1e72SVladimir Oltean 			way %= SJA1105ET_FDB_BIN_SIZE;
975291d1e72SVladimir Oltean 			dev_warn(dev, "Warning, FDB bin %d full while adding entry for %pM. Evicting entry %u.\n",
976291d1e72SVladimir Oltean 				 bin, addr, way);
977291d1e72SVladimir Oltean 			/* Evict entry */
978291d1e72SVladimir Oltean 			sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
979291d1e72SVladimir Oltean 						     index, NULL, false);
980291d1e72SVladimir Oltean 		}
981291d1e72SVladimir Oltean 	}
982291d1e72SVladimir Oltean 	l2_lookup.index = sja1105et_fdb_index(bin, way);
983291d1e72SVladimir Oltean 
98460f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
985291d1e72SVladimir Oltean 					  l2_lookup.index, &l2_lookup,
986291d1e72SVladimir Oltean 					  true);
98760f6053fSVladimir Oltean 	if (rc < 0)
98860f6053fSVladimir Oltean 		return rc;
98960f6053fSVladimir Oltean 
99060f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, true);
991291d1e72SVladimir Oltean }
992291d1e72SVladimir Oltean 
9939dfa6911SVladimir Oltean int sja1105et_fdb_del(struct dsa_switch *ds, int port,
994291d1e72SVladimir Oltean 		      const unsigned char *addr, u16 vid)
995291d1e72SVladimir Oltean {
996291d1e72SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
997291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
99860f6053fSVladimir Oltean 	int index, bin, way, rc;
999291d1e72SVladimir Oltean 	bool keep;
1000291d1e72SVladimir Oltean 
10019dfa6911SVladimir Oltean 	bin = sja1105et_fdb_hash(priv, addr, vid);
10029dfa6911SVladimir Oltean 	way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid,
1003291d1e72SVladimir Oltean 					    &l2_lookup, NULL);
1004291d1e72SVladimir Oltean 	if (way < 0)
1005291d1e72SVladimir Oltean 		return 0;
1006291d1e72SVladimir Oltean 	index = sja1105et_fdb_index(bin, way);
1007291d1e72SVladimir Oltean 
1008291d1e72SVladimir Oltean 	/* We have an FDB entry. Is our port in the destination mask? If yes,
1009291d1e72SVladimir Oltean 	 * we need to remove it. If the resulting port mask becomes empty, we
1010291d1e72SVladimir Oltean 	 * need to completely evict the FDB entry.
1011291d1e72SVladimir Oltean 	 * Otherwise we just write it back.
1012291d1e72SVladimir Oltean 	 */
1013291d1e72SVladimir Oltean 	l2_lookup.destports &= ~BIT(port);
10147752e937SVladimir Oltean 
1015291d1e72SVladimir Oltean 	if (l2_lookup.destports)
1016291d1e72SVladimir Oltean 		keep = true;
1017291d1e72SVladimir Oltean 	else
1018291d1e72SVladimir Oltean 		keep = false;
1019291d1e72SVladimir Oltean 
102060f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1021291d1e72SVladimir Oltean 					  index, &l2_lookup, keep);
102260f6053fSVladimir Oltean 	if (rc < 0)
102360f6053fSVladimir Oltean 		return rc;
102460f6053fSVladimir Oltean 
102560f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, keep);
1026291d1e72SVladimir Oltean }
1027291d1e72SVladimir Oltean 
10289dfa6911SVladimir Oltean int sja1105pqrs_fdb_add(struct dsa_switch *ds, int port,
10299dfa6911SVladimir Oltean 			const unsigned char *addr, u16 vid)
10309dfa6911SVladimir Oltean {
10311da73821SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
10321da73821SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
10331da73821SVladimir Oltean 	int rc, i;
10341da73821SVladimir Oltean 
10351da73821SVladimir Oltean 	/* Search for an existing entry in the FDB table */
10361da73821SVladimir Oltean 	l2_lookup.macaddr = ether_addr_to_u64(addr);
10371da73821SVladimir Oltean 	l2_lookup.vlanid = vid;
10381da73821SVladimir Oltean 	l2_lookup.iotag = SJA1105_S_TAG;
10391da73821SVladimir Oltean 	l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0);
10401da73821SVladimir Oltean 	l2_lookup.mask_vlanid = VLAN_VID_MASK;
10411da73821SVladimir Oltean 	l2_lookup.mask_iotag = BIT(0);
10421da73821SVladimir Oltean 	l2_lookup.destports = BIT(port);
10431da73821SVladimir Oltean 
10441da73821SVladimir Oltean 	rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
10451da73821SVladimir Oltean 					 SJA1105_SEARCH, &l2_lookup);
10461da73821SVladimir Oltean 	if (rc == 0) {
10471da73821SVladimir Oltean 		/* Found and this port is already in the entry's
10481da73821SVladimir Oltean 		 * port mask => job done
10491da73821SVladimir Oltean 		 */
10501da73821SVladimir Oltean 		if (l2_lookup.destports & BIT(port))
10511da73821SVladimir Oltean 			return 0;
10521da73821SVladimir Oltean 		/* l2_lookup.index is populated by the switch in case it
10531da73821SVladimir Oltean 		 * found something.
10541da73821SVladimir Oltean 		 */
10551da73821SVladimir Oltean 		l2_lookup.destports |= BIT(port);
10561da73821SVladimir Oltean 		goto skip_finding_an_index;
10571da73821SVladimir Oltean 	}
10581da73821SVladimir Oltean 
10591da73821SVladimir Oltean 	/* Not found, so try to find an unused spot in the FDB.
10601da73821SVladimir Oltean 	 * This is slightly inefficient because the strategy is knock-knock at
10611da73821SVladimir Oltean 	 * every possible position from 0 to 1023.
10621da73821SVladimir Oltean 	 */
10631da73821SVladimir Oltean 	for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
10641da73821SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
10651da73821SVladimir Oltean 						 i, NULL);
10661da73821SVladimir Oltean 		if (rc < 0)
10671da73821SVladimir Oltean 			break;
10681da73821SVladimir Oltean 	}
10691da73821SVladimir Oltean 	if (i == SJA1105_MAX_L2_LOOKUP_COUNT) {
10701da73821SVladimir Oltean 		dev_err(ds->dev, "FDB is full, cannot add entry.\n");
10711da73821SVladimir Oltean 		return -EINVAL;
10721da73821SVladimir Oltean 	}
107317ae6555SVladimir Oltean 	l2_lookup.lockeds = true;
10741da73821SVladimir Oltean 	l2_lookup.index = i;
10751da73821SVladimir Oltean 
10761da73821SVladimir Oltean skip_finding_an_index:
107760f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
10781da73821SVladimir Oltean 					  l2_lookup.index, &l2_lookup,
10791da73821SVladimir Oltean 					  true);
108060f6053fSVladimir Oltean 	if (rc < 0)
108160f6053fSVladimir Oltean 		return rc;
108260f6053fSVladimir Oltean 
108360f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, true);
10849dfa6911SVladimir Oltean }
10859dfa6911SVladimir Oltean 
10869dfa6911SVladimir Oltean int sja1105pqrs_fdb_del(struct dsa_switch *ds, int port,
10879dfa6911SVladimir Oltean 			const unsigned char *addr, u16 vid)
10889dfa6911SVladimir Oltean {
10891da73821SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
10901da73821SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
10911da73821SVladimir Oltean 	bool keep;
10921da73821SVladimir Oltean 	int rc;
10931da73821SVladimir Oltean 
10941da73821SVladimir Oltean 	l2_lookup.macaddr = ether_addr_to_u64(addr);
10951da73821SVladimir Oltean 	l2_lookup.vlanid = vid;
10961da73821SVladimir Oltean 	l2_lookup.iotag = SJA1105_S_TAG;
10971da73821SVladimir Oltean 	l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0);
10981da73821SVladimir Oltean 	l2_lookup.mask_vlanid = VLAN_VID_MASK;
10991da73821SVladimir Oltean 	l2_lookup.mask_iotag = BIT(0);
11001da73821SVladimir Oltean 	l2_lookup.destports = BIT(port);
11011da73821SVladimir Oltean 
11021da73821SVladimir Oltean 	rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
11031da73821SVladimir Oltean 					 SJA1105_SEARCH, &l2_lookup);
11041da73821SVladimir Oltean 	if (rc < 0)
11051da73821SVladimir Oltean 		return 0;
11061da73821SVladimir Oltean 
11071da73821SVladimir Oltean 	l2_lookup.destports &= ~BIT(port);
11081da73821SVladimir Oltean 
11091da73821SVladimir Oltean 	/* Decide whether we remove just this port from the FDB entry,
11101da73821SVladimir Oltean 	 * or if we remove it completely.
11111da73821SVladimir Oltean 	 */
11121da73821SVladimir Oltean 	if (l2_lookup.destports)
11131da73821SVladimir Oltean 		keep = true;
11141da73821SVladimir Oltean 	else
11151da73821SVladimir Oltean 		keep = false;
11161da73821SVladimir Oltean 
111760f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
11181da73821SVladimir Oltean 					  l2_lookup.index, &l2_lookup, keep);
111960f6053fSVladimir Oltean 	if (rc < 0)
112060f6053fSVladimir Oltean 		return rc;
112160f6053fSVladimir Oltean 
112260f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, keep);
11239dfa6911SVladimir Oltean }
11249dfa6911SVladimir Oltean 
11259dfa6911SVladimir Oltean static int sja1105_fdb_add(struct dsa_switch *ds, int port,
11269dfa6911SVladimir Oltean 			   const unsigned char *addr, u16 vid)
11279dfa6911SVladimir Oltean {
11289dfa6911SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1129b3ee526aSVladimir Oltean 	u16 rx_vid, tx_vid;
1130b3ee526aSVladimir Oltean 	int rc, i;
1131b3ee526aSVladimir Oltean 
1132b3ee526aSVladimir Oltean 	if (dsa_port_is_vlan_filtering(&ds->ports[port]))
1133b3ee526aSVladimir Oltean 		return priv->info->fdb_add_cmd(ds, port, addr, vid);
11349dfa6911SVladimir Oltean 
113593647594SVladimir Oltean 	/* Since we make use of VLANs even when the bridge core doesn't tell us
113693647594SVladimir Oltean 	 * to, translate these FDB entries into the correct dsa_8021q ones.
1137b3ee526aSVladimir Oltean 	 * The basic idea (also repeats for removal below) is:
1138b3ee526aSVladimir Oltean 	 * - Each of the other front-panel ports needs to be able to forward a
1139b3ee526aSVladimir Oltean 	 *   pvid-tagged (aka tagged with their rx_vid) frame that matches this
1140b3ee526aSVladimir Oltean 	 *   DMAC.
1141b3ee526aSVladimir Oltean 	 * - The CPU port (aka the tx_vid of this port) needs to be able to
1142b3ee526aSVladimir Oltean 	 *   send a frame matching this DMAC to the specified port.
1143b3ee526aSVladimir Oltean 	 * For a better picture see net/dsa/tag_8021q.c.
114493647594SVladimir Oltean 	 */
1145b3ee526aSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
1146b3ee526aSVladimir Oltean 		if (i == port)
1147b3ee526aSVladimir Oltean 			continue;
1148b3ee526aSVladimir Oltean 		if (i == dsa_upstream_port(priv->ds, port))
1149b3ee526aSVladimir Oltean 			continue;
115093647594SVladimir Oltean 
1151b3ee526aSVladimir Oltean 		rx_vid = dsa_8021q_rx_vid(ds, i);
1152b3ee526aSVladimir Oltean 		rc = priv->info->fdb_add_cmd(ds, port, addr, rx_vid);
115393647594SVladimir Oltean 		if (rc < 0)
115493647594SVladimir Oltean 			return rc;
115593647594SVladimir Oltean 	}
1156b3ee526aSVladimir Oltean 	tx_vid = dsa_8021q_tx_vid(ds, port);
1157b3ee526aSVladimir Oltean 	return priv->info->fdb_add_cmd(ds, port, addr, tx_vid);
11589dfa6911SVladimir Oltean }
11599dfa6911SVladimir Oltean 
11609dfa6911SVladimir Oltean static int sja1105_fdb_del(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 	u16 rx_vid, tx_vid;
1165b3ee526aSVladimir Oltean 	int rc, i;
11669dfa6911SVladimir Oltean 
1167b3ee526aSVladimir Oltean 	if (dsa_port_is_vlan_filtering(&ds->ports[port]))
1168b3ee526aSVladimir Oltean 		return priv->info->fdb_del_cmd(ds, port, addr, vid);
116993647594SVladimir Oltean 
1170b3ee526aSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
1171b3ee526aSVladimir Oltean 		if (i == port)
1172b3ee526aSVladimir Oltean 			continue;
1173b3ee526aSVladimir Oltean 		if (i == dsa_upstream_port(priv->ds, port))
1174b3ee526aSVladimir Oltean 			continue;
1175b3ee526aSVladimir Oltean 
1176b3ee526aSVladimir Oltean 		rx_vid = dsa_8021q_rx_vid(ds, i);
1177b3ee526aSVladimir Oltean 		rc = priv->info->fdb_del_cmd(ds, port, addr, rx_vid);
117893647594SVladimir Oltean 		if (rc < 0)
117993647594SVladimir Oltean 			return rc;
118093647594SVladimir Oltean 	}
1181b3ee526aSVladimir Oltean 	tx_vid = dsa_8021q_tx_vid(ds, port);
1182b3ee526aSVladimir Oltean 	return priv->info->fdb_del_cmd(ds, port, addr, tx_vid);
11839dfa6911SVladimir Oltean }
11849dfa6911SVladimir Oltean 
1185291d1e72SVladimir Oltean static int sja1105_fdb_dump(struct dsa_switch *ds, int port,
1186291d1e72SVladimir Oltean 			    dsa_fdb_dump_cb_t *cb, void *data)
1187291d1e72SVladimir Oltean {
1188291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1189291d1e72SVladimir Oltean 	struct device *dev = ds->dev;
1190b3ee526aSVladimir Oltean 	u16 rx_vid, tx_vid;
1191291d1e72SVladimir Oltean 	int i;
1192291d1e72SVladimir Oltean 
1193b3ee526aSVladimir Oltean 	rx_vid = dsa_8021q_rx_vid(ds, port);
1194b3ee526aSVladimir Oltean 	tx_vid = dsa_8021q_tx_vid(ds, port);
1195b3ee526aSVladimir Oltean 
1196291d1e72SVladimir Oltean 	for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
1197291d1e72SVladimir Oltean 		struct sja1105_l2_lookup_entry l2_lookup = {0};
1198291d1e72SVladimir Oltean 		u8 macaddr[ETH_ALEN];
1199291d1e72SVladimir Oltean 		int rc;
1200291d1e72SVladimir Oltean 
1201291d1e72SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1202291d1e72SVladimir Oltean 						 i, &l2_lookup);
1203291d1e72SVladimir Oltean 		/* No fdb entry at i, not an issue */
1204def84604SVladimir Oltean 		if (rc == -ENOENT)
1205291d1e72SVladimir Oltean 			continue;
1206291d1e72SVladimir Oltean 		if (rc) {
1207291d1e72SVladimir Oltean 			dev_err(dev, "Failed to dump FDB: %d\n", rc);
1208291d1e72SVladimir Oltean 			return rc;
1209291d1e72SVladimir Oltean 		}
1210291d1e72SVladimir Oltean 
1211291d1e72SVladimir Oltean 		/* FDB dump callback is per port. This means we have to
1212291d1e72SVladimir Oltean 		 * disregard a valid entry if it's not for this port, even if
1213291d1e72SVladimir Oltean 		 * only to revisit it later. This is inefficient because the
1214291d1e72SVladimir Oltean 		 * 1024-sized FDB table needs to be traversed 4 times through
1215291d1e72SVladimir Oltean 		 * SPI during a 'bridge fdb show' command.
1216291d1e72SVladimir Oltean 		 */
1217291d1e72SVladimir Oltean 		if (!(l2_lookup.destports & BIT(port)))
1218291d1e72SVladimir Oltean 			continue;
1219291d1e72SVladimir Oltean 		u64_to_ether_addr(l2_lookup.macaddr, macaddr);
122093647594SVladimir Oltean 
1221*d7637782SVladimir Oltean 		/* On SJA1105 E/T, the switch doesn't implement the LOCKEDS
1222*d7637782SVladimir Oltean 		 * bit, so it doesn't tell us whether a FDB entry is static
1223*d7637782SVladimir Oltean 		 * or not.
1224*d7637782SVladimir Oltean 		 * But, of course, we can find out - we're the ones who added
1225*d7637782SVladimir Oltean 		 * it in the first place.
1226*d7637782SVladimir Oltean 		 */
1227*d7637782SVladimir Oltean 		if (priv->info->device_id == SJA1105E_DEVICE_ID ||
1228*d7637782SVladimir Oltean 		    priv->info->device_id == SJA1105T_DEVICE_ID) {
1229*d7637782SVladimir Oltean 			int match;
1230*d7637782SVladimir Oltean 
1231*d7637782SVladimir Oltean 			match = sja1105_find_static_fdb_entry(priv, port,
1232*d7637782SVladimir Oltean 							      &l2_lookup);
1233*d7637782SVladimir Oltean 			l2_lookup.lockeds = (match >= 0);
1234*d7637782SVladimir Oltean 		}
1235*d7637782SVladimir Oltean 
1236b3ee526aSVladimir Oltean 		/* We need to hide the dsa_8021q VLANs from the user. This
1237b3ee526aSVladimir Oltean 		 * basically means hiding the duplicates and only showing
1238b3ee526aSVladimir Oltean 		 * the pvid that is supposed to be active in standalone and
1239b3ee526aSVladimir Oltean 		 * non-vlan_filtering modes (aka 1).
1240b3ee526aSVladimir Oltean 		 * - For statically added FDB entries (bridge fdb add), we
1241b3ee526aSVladimir Oltean 		 *   can convert the TX VID (coming from the CPU port) into the
1242b3ee526aSVladimir Oltean 		 *   pvid and ignore the RX VIDs of the other ports.
1243b3ee526aSVladimir Oltean 		 * - For dynamically learned FDB entries, a single entry with
1244b3ee526aSVladimir Oltean 		 *   no duplicates is learned - that which has the real port's
1245b3ee526aSVladimir Oltean 		 *   pvid, aka RX VID.
124693647594SVladimir Oltean 		 */
1247b3ee526aSVladimir Oltean 		if (!dsa_port_is_vlan_filtering(&ds->ports[port])) {
1248b3ee526aSVladimir Oltean 			if (l2_lookup.vlanid == tx_vid ||
1249b3ee526aSVladimir Oltean 			    l2_lookup.vlanid == rx_vid)
125093647594SVladimir Oltean 				l2_lookup.vlanid = 1;
1251b3ee526aSVladimir Oltean 			else
1252b3ee526aSVladimir Oltean 				continue;
1253b3ee526aSVladimir Oltean 		}
125417ae6555SVladimir Oltean 		cb(macaddr, l2_lookup.vlanid, l2_lookup.lockeds, data);
1255291d1e72SVladimir Oltean 	}
1256291d1e72SVladimir Oltean 	return 0;
1257291d1e72SVladimir Oltean }
1258291d1e72SVladimir Oltean 
1259291d1e72SVladimir Oltean /* This callback needs to be present */
1260291d1e72SVladimir Oltean static int sja1105_mdb_prepare(struct dsa_switch *ds, int port,
1261291d1e72SVladimir Oltean 			       const struct switchdev_obj_port_mdb *mdb)
1262291d1e72SVladimir Oltean {
1263291d1e72SVladimir Oltean 	return 0;
1264291d1e72SVladimir Oltean }
1265291d1e72SVladimir Oltean 
1266291d1e72SVladimir Oltean static void sja1105_mdb_add(struct dsa_switch *ds, int port,
1267291d1e72SVladimir Oltean 			    const struct switchdev_obj_port_mdb *mdb)
1268291d1e72SVladimir Oltean {
1269291d1e72SVladimir Oltean 	sja1105_fdb_add(ds, port, mdb->addr, mdb->vid);
1270291d1e72SVladimir Oltean }
1271291d1e72SVladimir Oltean 
1272291d1e72SVladimir Oltean static int sja1105_mdb_del(struct dsa_switch *ds, int port,
1273291d1e72SVladimir Oltean 			   const struct switchdev_obj_port_mdb *mdb)
1274291d1e72SVladimir Oltean {
1275291d1e72SVladimir Oltean 	return sja1105_fdb_del(ds, port, mdb->addr, mdb->vid);
1276291d1e72SVladimir Oltean }
1277291d1e72SVladimir Oltean 
12788aa9ebccSVladimir Oltean static int sja1105_bridge_member(struct dsa_switch *ds, int port,
12798aa9ebccSVladimir Oltean 				 struct net_device *br, bool member)
12808aa9ebccSVladimir Oltean {
12818aa9ebccSVladimir Oltean 	struct sja1105_l2_forwarding_entry *l2_fwd;
12828aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
12838aa9ebccSVladimir Oltean 	int i, rc;
12848aa9ebccSVladimir Oltean 
12858aa9ebccSVladimir Oltean 	l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries;
12868aa9ebccSVladimir Oltean 
12878aa9ebccSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
12888aa9ebccSVladimir Oltean 		/* Add this port to the forwarding matrix of the
12898aa9ebccSVladimir Oltean 		 * other ports in the same bridge, and viceversa.
12908aa9ebccSVladimir Oltean 		 */
12918aa9ebccSVladimir Oltean 		if (!dsa_is_user_port(ds, i))
12928aa9ebccSVladimir Oltean 			continue;
12938aa9ebccSVladimir Oltean 		/* For the ports already under the bridge, only one thing needs
12948aa9ebccSVladimir Oltean 		 * to be done, and that is to add this port to their
12958aa9ebccSVladimir Oltean 		 * reachability domain. So we can perform the SPI write for
12968aa9ebccSVladimir Oltean 		 * them immediately. However, for this port itself (the one
12978aa9ebccSVladimir Oltean 		 * that is new to the bridge), we need to add all other ports
12988aa9ebccSVladimir Oltean 		 * to its reachability domain. So we do that incrementally in
12998aa9ebccSVladimir Oltean 		 * this loop, and perform the SPI write only at the end, once
13008aa9ebccSVladimir Oltean 		 * the domain contains all other bridge ports.
13018aa9ebccSVladimir Oltean 		 */
13028aa9ebccSVladimir Oltean 		if (i == port)
13038aa9ebccSVladimir Oltean 			continue;
13048aa9ebccSVladimir Oltean 		if (dsa_to_port(ds, i)->bridge_dev != br)
13058aa9ebccSVladimir Oltean 			continue;
13068aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2_fwd, i, port, member);
13078aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2_fwd, port, i, member);
13088aa9ebccSVladimir Oltean 
13098aa9ebccSVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
13108aa9ebccSVladimir Oltean 						  i, &l2_fwd[i], true);
13118aa9ebccSVladimir Oltean 		if (rc < 0)
13128aa9ebccSVladimir Oltean 			return rc;
13138aa9ebccSVladimir Oltean 	}
13148aa9ebccSVladimir Oltean 
13158aa9ebccSVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
13168aa9ebccSVladimir Oltean 					    port, &l2_fwd[port], true);
13178aa9ebccSVladimir Oltean }
13188aa9ebccSVladimir Oltean 
1319640f763fSVladimir Oltean static void sja1105_bridge_stp_state_set(struct dsa_switch *ds, int port,
1320640f763fSVladimir Oltean 					 u8 state)
1321640f763fSVladimir Oltean {
1322640f763fSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1323640f763fSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
1324640f763fSVladimir Oltean 
1325640f763fSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
1326640f763fSVladimir Oltean 
1327640f763fSVladimir Oltean 	switch (state) {
1328640f763fSVladimir Oltean 	case BR_STATE_DISABLED:
1329640f763fSVladimir Oltean 	case BR_STATE_BLOCKING:
1330640f763fSVladimir Oltean 		/* From UM10944 description of DRPDTAG (why put this there?):
1331640f763fSVladimir Oltean 		 * "Management traffic flows to the port regardless of the state
1332640f763fSVladimir Oltean 		 * of the INGRESS flag". So BPDUs are still be allowed to pass.
1333640f763fSVladimir Oltean 		 * At the moment no difference between DISABLED and BLOCKING.
1334640f763fSVladimir Oltean 		 */
1335640f763fSVladimir Oltean 		mac[port].ingress   = false;
1336640f763fSVladimir Oltean 		mac[port].egress    = false;
1337640f763fSVladimir Oltean 		mac[port].dyn_learn = false;
1338640f763fSVladimir Oltean 		break;
1339640f763fSVladimir Oltean 	case BR_STATE_LISTENING:
1340640f763fSVladimir Oltean 		mac[port].ingress   = true;
1341640f763fSVladimir Oltean 		mac[port].egress    = false;
1342640f763fSVladimir Oltean 		mac[port].dyn_learn = false;
1343640f763fSVladimir Oltean 		break;
1344640f763fSVladimir Oltean 	case BR_STATE_LEARNING:
1345640f763fSVladimir Oltean 		mac[port].ingress   = true;
1346640f763fSVladimir Oltean 		mac[port].egress    = false;
1347640f763fSVladimir Oltean 		mac[port].dyn_learn = true;
1348640f763fSVladimir Oltean 		break;
1349640f763fSVladimir Oltean 	case BR_STATE_FORWARDING:
1350640f763fSVladimir Oltean 		mac[port].ingress   = true;
1351640f763fSVladimir Oltean 		mac[port].egress    = true;
1352640f763fSVladimir Oltean 		mac[port].dyn_learn = true;
1353640f763fSVladimir Oltean 		break;
1354640f763fSVladimir Oltean 	default:
1355640f763fSVladimir Oltean 		dev_err(ds->dev, "invalid STP state: %d\n", state);
1356640f763fSVladimir Oltean 		return;
1357640f763fSVladimir Oltean 	}
1358640f763fSVladimir Oltean 
1359640f763fSVladimir Oltean 	sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
1360640f763fSVladimir Oltean 				     &mac[port], true);
1361640f763fSVladimir Oltean }
1362640f763fSVladimir Oltean 
13638aa9ebccSVladimir Oltean static int sja1105_bridge_join(struct dsa_switch *ds, int port,
13648aa9ebccSVladimir Oltean 			       struct net_device *br)
13658aa9ebccSVladimir Oltean {
13668aa9ebccSVladimir Oltean 	return sja1105_bridge_member(ds, port, br, true);
13678aa9ebccSVladimir Oltean }
13688aa9ebccSVladimir Oltean 
13698aa9ebccSVladimir Oltean static void sja1105_bridge_leave(struct dsa_switch *ds, int port,
13708aa9ebccSVladimir Oltean 				 struct net_device *br)
13718aa9ebccSVladimir Oltean {
13728aa9ebccSVladimir Oltean 	sja1105_bridge_member(ds, port, br, false);
13738aa9ebccSVladimir Oltean }
13748aa9ebccSVladimir Oltean 
13756666cebcSVladimir Oltean /* For situations where we need to change a setting at runtime that is only
13766666cebcSVladimir Oltean  * available through the static configuration, resetting the switch in order
13776666cebcSVladimir Oltean  * to upload the new static config is unavoidable. Back up the settings we
13786666cebcSVladimir Oltean  * modify at runtime (currently only MAC) and restore them after uploading,
13796666cebcSVladimir Oltean  * such that this operation is relatively seamless.
13806666cebcSVladimir Oltean  */
13816666cebcSVladimir Oltean static int sja1105_static_config_reload(struct sja1105_private *priv)
13826666cebcSVladimir Oltean {
13836666cebcSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
13846666cebcSVladimir Oltean 	int speed_mbps[SJA1105_NUM_PORTS];
13856666cebcSVladimir Oltean 	int rc, i;
13866666cebcSVladimir Oltean 
13876666cebcSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
13886666cebcSVladimir Oltean 
13898400cff6SVladimir Oltean 	/* Back up the dynamic link speed changed by sja1105_adjust_port_config
13908400cff6SVladimir Oltean 	 * in order to temporarily restore it to SJA1105_SPEED_AUTO - which the
13918400cff6SVladimir Oltean 	 * switch wants to see in the static config in order to allow us to
13928400cff6SVladimir Oltean 	 * change it through the dynamic interface later.
13936666cebcSVladimir Oltean 	 */
13946666cebcSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
13956666cebcSVladimir Oltean 		speed_mbps[i] = sja1105_speed[mac[i].speed];
13966666cebcSVladimir Oltean 		mac[i].speed = SJA1105_SPEED_AUTO;
13976666cebcSVladimir Oltean 	}
13986666cebcSVladimir Oltean 
13996666cebcSVladimir Oltean 	/* Reset switch and send updated static configuration */
14006666cebcSVladimir Oltean 	rc = sja1105_static_config_upload(priv);
14016666cebcSVladimir Oltean 	if (rc < 0)
14026666cebcSVladimir Oltean 		goto out;
14036666cebcSVladimir Oltean 
14046666cebcSVladimir Oltean 	/* Configure the CGU (PLLs) for MII and RMII PHYs.
14056666cebcSVladimir Oltean 	 * For these interfaces there is no dynamic configuration
14066666cebcSVladimir Oltean 	 * needed, since PLLs have same settings at all speeds.
14076666cebcSVladimir Oltean 	 */
14086666cebcSVladimir Oltean 	rc = sja1105_clocking_setup(priv);
14096666cebcSVladimir Oltean 	if (rc < 0)
14106666cebcSVladimir Oltean 		goto out;
14116666cebcSVladimir Oltean 
14126666cebcSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
14138400cff6SVladimir Oltean 		rc = sja1105_adjust_port_config(priv, i, speed_mbps[i]);
14146666cebcSVladimir Oltean 		if (rc < 0)
14156666cebcSVladimir Oltean 			goto out;
14166666cebcSVladimir Oltean 	}
14176666cebcSVladimir Oltean out:
14186666cebcSVladimir Oltean 	return rc;
14196666cebcSVladimir Oltean }
14206666cebcSVladimir Oltean 
14216666cebcSVladimir Oltean static int sja1105_pvid_apply(struct sja1105_private *priv, int port, u16 pvid)
14226666cebcSVladimir Oltean {
14236666cebcSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
14246666cebcSVladimir Oltean 
14256666cebcSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
14266666cebcSVladimir Oltean 
14276666cebcSVladimir Oltean 	mac[port].vlanid = pvid;
14286666cebcSVladimir Oltean 
14296666cebcSVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
14306666cebcSVladimir Oltean 					   &mac[port], true);
14316666cebcSVladimir Oltean }
14326666cebcSVladimir Oltean 
14336666cebcSVladimir Oltean static int sja1105_is_vlan_configured(struct sja1105_private *priv, u16 vid)
14346666cebcSVladimir Oltean {
14356666cebcSVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
14366666cebcSVladimir Oltean 	int count, i;
14376666cebcSVladimir Oltean 
14386666cebcSVladimir Oltean 	vlan = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entries;
14396666cebcSVladimir Oltean 	count = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entry_count;
14406666cebcSVladimir Oltean 
14416666cebcSVladimir Oltean 	for (i = 0; i < count; i++)
14426666cebcSVladimir Oltean 		if (vlan[i].vlanid == vid)
14436666cebcSVladimir Oltean 			return i;
14446666cebcSVladimir Oltean 
14456666cebcSVladimir Oltean 	/* Return an invalid entry index if not found */
14466666cebcSVladimir Oltean 	return -1;
14476666cebcSVladimir Oltean }
14486666cebcSVladimir Oltean 
14496666cebcSVladimir Oltean static int sja1105_vlan_apply(struct sja1105_private *priv, int port, u16 vid,
14506666cebcSVladimir Oltean 			      bool enabled, bool untagged)
14516666cebcSVladimir Oltean {
14526666cebcSVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
14536666cebcSVladimir Oltean 	struct sja1105_table *table;
14546666cebcSVladimir Oltean 	bool keep = true;
14556666cebcSVladimir Oltean 	int match, rc;
14566666cebcSVladimir Oltean 
14576666cebcSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
14586666cebcSVladimir Oltean 
14596666cebcSVladimir Oltean 	match = sja1105_is_vlan_configured(priv, vid);
14606666cebcSVladimir Oltean 	if (match < 0) {
14616666cebcSVladimir Oltean 		/* Can't delete a missing entry. */
14626666cebcSVladimir Oltean 		if (!enabled)
14636666cebcSVladimir Oltean 			return 0;
14646666cebcSVladimir Oltean 		rc = sja1105_table_resize(table, table->entry_count + 1);
14656666cebcSVladimir Oltean 		if (rc)
14666666cebcSVladimir Oltean 			return rc;
14676666cebcSVladimir Oltean 		match = table->entry_count - 1;
14686666cebcSVladimir Oltean 	}
14696666cebcSVladimir Oltean 	/* Assign pointer after the resize (it's new memory) */
14706666cebcSVladimir Oltean 	vlan = table->entries;
14716666cebcSVladimir Oltean 	vlan[match].vlanid = vid;
14726666cebcSVladimir Oltean 	if (enabled) {
14736666cebcSVladimir Oltean 		vlan[match].vlan_bc |= BIT(port);
14746666cebcSVladimir Oltean 		vlan[match].vmemb_port |= BIT(port);
14756666cebcSVladimir Oltean 	} else {
14766666cebcSVladimir Oltean 		vlan[match].vlan_bc &= ~BIT(port);
14776666cebcSVladimir Oltean 		vlan[match].vmemb_port &= ~BIT(port);
14786666cebcSVladimir Oltean 	}
14796666cebcSVladimir Oltean 	/* Also unset tag_port if removing this VLAN was requested,
14806666cebcSVladimir Oltean 	 * just so we don't have a confusing bitmap (no practical purpose).
14816666cebcSVladimir Oltean 	 */
14826666cebcSVladimir Oltean 	if (untagged || !enabled)
14836666cebcSVladimir Oltean 		vlan[match].tag_port &= ~BIT(port);
14846666cebcSVladimir Oltean 	else
14856666cebcSVladimir Oltean 		vlan[match].tag_port |= BIT(port);
14866666cebcSVladimir Oltean 	/* If there's no port left as member of this VLAN,
14876666cebcSVladimir Oltean 	 * it's time for it to go.
14886666cebcSVladimir Oltean 	 */
14896666cebcSVladimir Oltean 	if (!vlan[match].vmemb_port)
14906666cebcSVladimir Oltean 		keep = false;
14916666cebcSVladimir Oltean 
14926666cebcSVladimir Oltean 	dev_dbg(priv->ds->dev,
14936666cebcSVladimir Oltean 		"%s: port %d, vid %llu, broadcast domain 0x%llx, "
14946666cebcSVladimir Oltean 		"port members 0x%llx, tagged ports 0x%llx, keep %d\n",
14956666cebcSVladimir Oltean 		__func__, port, vlan[match].vlanid, vlan[match].vlan_bc,
14966666cebcSVladimir Oltean 		vlan[match].vmemb_port, vlan[match].tag_port, keep);
14976666cebcSVladimir Oltean 
14986666cebcSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_VLAN_LOOKUP, vid,
14996666cebcSVladimir Oltean 					  &vlan[match], keep);
15006666cebcSVladimir Oltean 	if (rc < 0)
15016666cebcSVladimir Oltean 		return rc;
15026666cebcSVladimir Oltean 
15036666cebcSVladimir Oltean 	if (!keep)
15046666cebcSVladimir Oltean 		return sja1105_table_delete_entry(table, match);
15056666cebcSVladimir Oltean 
15066666cebcSVladimir Oltean 	return 0;
15076666cebcSVladimir Oltean }
15086666cebcSVladimir Oltean 
1509227d07a0SVladimir Oltean static int sja1105_setup_8021q_tagging(struct dsa_switch *ds, bool enabled)
1510227d07a0SVladimir Oltean {
1511227d07a0SVladimir Oltean 	int rc, i;
1512227d07a0SVladimir Oltean 
1513227d07a0SVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
1514227d07a0SVladimir Oltean 		rc = dsa_port_setup_8021q_tagging(ds, i, enabled);
1515227d07a0SVladimir Oltean 		if (rc < 0) {
1516227d07a0SVladimir Oltean 			dev_err(ds->dev, "Failed to setup VLAN tagging for port %d: %d\n",
1517227d07a0SVladimir Oltean 				i, rc);
1518227d07a0SVladimir Oltean 			return rc;
1519227d07a0SVladimir Oltean 		}
1520227d07a0SVladimir Oltean 	}
1521227d07a0SVladimir Oltean 	dev_info(ds->dev, "%s switch tagging\n",
1522227d07a0SVladimir Oltean 		 enabled ? "Enabled" : "Disabled");
1523227d07a0SVladimir Oltean 	return 0;
1524227d07a0SVladimir Oltean }
1525227d07a0SVladimir Oltean 
15268aa9ebccSVladimir Oltean static enum dsa_tag_protocol
15278aa9ebccSVladimir Oltean sja1105_get_tag_protocol(struct dsa_switch *ds, int port)
15288aa9ebccSVladimir Oltean {
1529227d07a0SVladimir Oltean 	return DSA_TAG_PROTO_SJA1105;
15308aa9ebccSVladimir Oltean }
15318aa9ebccSVladimir Oltean 
15326666cebcSVladimir Oltean /* This callback needs to be present */
15336666cebcSVladimir Oltean static int sja1105_vlan_prepare(struct dsa_switch *ds, int port,
15346666cebcSVladimir Oltean 				const struct switchdev_obj_port_vlan *vlan)
15356666cebcSVladimir Oltean {
15366666cebcSVladimir Oltean 	return 0;
15376666cebcSVladimir Oltean }
15386666cebcSVladimir Oltean 
1539070ca3bbSVladimir Oltean /* The TPID setting belongs to the General Parameters table,
1540070ca3bbSVladimir Oltean  * which can only be partially reconfigured at runtime (and not the TPID).
1541070ca3bbSVladimir Oltean  * So a switch reset is required.
1542070ca3bbSVladimir Oltean  */
15436666cebcSVladimir Oltean static int sja1105_vlan_filtering(struct dsa_switch *ds, int port, bool enabled)
15446666cebcSVladimir Oltean {
1545070ca3bbSVladimir Oltean 	struct sja1105_general_params_entry *general_params;
15466666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1547070ca3bbSVladimir Oltean 	struct sja1105_table *table;
1548070ca3bbSVladimir Oltean 	u16 tpid, tpid2;
15496666cebcSVladimir Oltean 	int rc;
15506666cebcSVladimir Oltean 
1551070ca3bbSVladimir Oltean 	if (enabled) {
15526666cebcSVladimir Oltean 		/* Enable VLAN filtering. */
1553f9a1a764SVladimir Oltean 		tpid  = ETH_P_8021AD;
1554f9a1a764SVladimir Oltean 		tpid2 = ETH_P_8021Q;
1555070ca3bbSVladimir Oltean 	} else {
15566666cebcSVladimir Oltean 		/* Disable VLAN filtering. */
1557070ca3bbSVladimir Oltean 		tpid  = ETH_P_SJA1105;
1558070ca3bbSVladimir Oltean 		tpid2 = ETH_P_SJA1105;
1559070ca3bbSVladimir Oltean 	}
1560070ca3bbSVladimir Oltean 
1561070ca3bbSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
1562070ca3bbSVladimir Oltean 	general_params = table->entries;
1563f9a1a764SVladimir Oltean 	/* EtherType used to identify outer tagged (S-tag) VLAN traffic */
1564070ca3bbSVladimir Oltean 	general_params->tpid = tpid;
1565f9a1a764SVladimir Oltean 	/* EtherType used to identify inner tagged (C-tag) VLAN traffic */
1566070ca3bbSVladimir Oltean 	general_params->tpid2 = tpid2;
156742824463SVladimir Oltean 	/* When VLAN filtering is on, we need to at least be able to
156842824463SVladimir Oltean 	 * decode management traffic through the "backup plan".
156942824463SVladimir Oltean 	 */
157042824463SVladimir Oltean 	general_params->incl_srcpt1 = enabled;
157142824463SVladimir Oltean 	general_params->incl_srcpt0 = enabled;
1572070ca3bbSVladimir Oltean 
1573070ca3bbSVladimir Oltean 	rc = sja1105_static_config_reload(priv);
15746666cebcSVladimir Oltean 	if (rc)
15756666cebcSVladimir Oltean 		dev_err(ds->dev, "Failed to change VLAN Ethertype\n");
15766666cebcSVladimir Oltean 
1577227d07a0SVladimir Oltean 	/* Switch port identification based on 802.1Q is only passable
1578227d07a0SVladimir Oltean 	 * if we are not under a vlan_filtering bridge. So make sure
1579227d07a0SVladimir Oltean 	 * the two configurations are mutually exclusive.
1580227d07a0SVladimir Oltean 	 */
1581227d07a0SVladimir Oltean 	return sja1105_setup_8021q_tagging(ds, !enabled);
15826666cebcSVladimir Oltean }
15836666cebcSVladimir Oltean 
15846666cebcSVladimir Oltean static void sja1105_vlan_add(struct dsa_switch *ds, int port,
15856666cebcSVladimir Oltean 			     const struct switchdev_obj_port_vlan *vlan)
15866666cebcSVladimir Oltean {
15876666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
15886666cebcSVladimir Oltean 	u16 vid;
15896666cebcSVladimir Oltean 	int rc;
15906666cebcSVladimir Oltean 
15916666cebcSVladimir Oltean 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
15926666cebcSVladimir Oltean 		rc = sja1105_vlan_apply(priv, port, vid, true, vlan->flags &
15936666cebcSVladimir Oltean 					BRIDGE_VLAN_INFO_UNTAGGED);
15946666cebcSVladimir Oltean 		if (rc < 0) {
15956666cebcSVladimir Oltean 			dev_err(ds->dev, "Failed to add VLAN %d to port %d: %d\n",
15966666cebcSVladimir Oltean 				vid, port, rc);
15976666cebcSVladimir Oltean 			return;
15986666cebcSVladimir Oltean 		}
15996666cebcSVladimir Oltean 		if (vlan->flags & BRIDGE_VLAN_INFO_PVID) {
16006666cebcSVladimir Oltean 			rc = sja1105_pvid_apply(ds->priv, port, vid);
16016666cebcSVladimir Oltean 			if (rc < 0) {
16026666cebcSVladimir Oltean 				dev_err(ds->dev, "Failed to set pvid %d on port %d: %d\n",
16036666cebcSVladimir Oltean 					vid, port, rc);
16046666cebcSVladimir Oltean 				return;
16056666cebcSVladimir Oltean 			}
16066666cebcSVladimir Oltean 		}
16076666cebcSVladimir Oltean 	}
16086666cebcSVladimir Oltean }
16096666cebcSVladimir Oltean 
16106666cebcSVladimir Oltean static int sja1105_vlan_del(struct dsa_switch *ds, int port,
16116666cebcSVladimir Oltean 			    const struct switchdev_obj_port_vlan *vlan)
16126666cebcSVladimir Oltean {
16136666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
16146666cebcSVladimir Oltean 	u16 vid;
16156666cebcSVladimir Oltean 	int rc;
16166666cebcSVladimir Oltean 
16176666cebcSVladimir Oltean 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
16186666cebcSVladimir Oltean 		rc = sja1105_vlan_apply(priv, port, vid, false, vlan->flags &
16196666cebcSVladimir Oltean 					BRIDGE_VLAN_INFO_UNTAGGED);
16206666cebcSVladimir Oltean 		if (rc < 0) {
16216666cebcSVladimir Oltean 			dev_err(ds->dev, "Failed to remove VLAN %d from port %d: %d\n",
16226666cebcSVladimir Oltean 				vid, port, rc);
16236666cebcSVladimir Oltean 			return rc;
16246666cebcSVladimir Oltean 		}
16256666cebcSVladimir Oltean 	}
16266666cebcSVladimir Oltean 	return 0;
16276666cebcSVladimir Oltean }
16286666cebcSVladimir Oltean 
16298aa9ebccSVladimir Oltean /* The programming model for the SJA1105 switch is "all-at-once" via static
16308aa9ebccSVladimir Oltean  * configuration tables. Some of these can be dynamically modified at runtime,
16318aa9ebccSVladimir Oltean  * but not the xMII mode parameters table.
16328aa9ebccSVladimir Oltean  * Furthermode, some PHYs may not have crystals for generating their clocks
16338aa9ebccSVladimir Oltean  * (e.g. RMII). Instead, their 50MHz clock is supplied via the SJA1105 port's
16348aa9ebccSVladimir Oltean  * ref_clk pin. So port clocking needs to be initialized early, before
16358aa9ebccSVladimir Oltean  * connecting to PHYs is attempted, otherwise they won't respond through MDIO.
16368aa9ebccSVladimir Oltean  * Setting correct PHY link speed does not matter now.
16378aa9ebccSVladimir Oltean  * But dsa_slave_phy_setup is called later than sja1105_setup, so the PHY
16388aa9ebccSVladimir Oltean  * bindings are not yet parsed by DSA core. We need to parse early so that we
16398aa9ebccSVladimir Oltean  * can populate the xMII mode parameters table.
16408aa9ebccSVladimir Oltean  */
16418aa9ebccSVladimir Oltean static int sja1105_setup(struct dsa_switch *ds)
16428aa9ebccSVladimir Oltean {
16438aa9ebccSVladimir Oltean 	struct sja1105_dt_port ports[SJA1105_NUM_PORTS];
16448aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
16458aa9ebccSVladimir Oltean 	int rc;
16468aa9ebccSVladimir Oltean 
16478aa9ebccSVladimir Oltean 	rc = sja1105_parse_dt(priv, ports);
16488aa9ebccSVladimir Oltean 	if (rc < 0) {
16498aa9ebccSVladimir Oltean 		dev_err(ds->dev, "Failed to parse DT: %d\n", rc);
16508aa9ebccSVladimir Oltean 		return rc;
16518aa9ebccSVladimir Oltean 	}
1652f5b8631cSVladimir Oltean 
1653f5b8631cSVladimir Oltean 	/* Error out early if internal delays are required through DT
1654f5b8631cSVladimir Oltean 	 * and we can't apply them.
1655f5b8631cSVladimir Oltean 	 */
1656f5b8631cSVladimir Oltean 	rc = sja1105_parse_rgmii_delays(priv, ports);
1657f5b8631cSVladimir Oltean 	if (rc < 0) {
1658f5b8631cSVladimir Oltean 		dev_err(ds->dev, "RGMII delay not supported\n");
1659f5b8631cSVladimir Oltean 		return rc;
1660f5b8631cSVladimir Oltean 	}
1661f5b8631cSVladimir Oltean 
1662bb77f36aSVladimir Oltean 	rc = sja1105_ptp_clock_register(priv);
1663bb77f36aSVladimir Oltean 	if (rc < 0) {
1664bb77f36aSVladimir Oltean 		dev_err(ds->dev, "Failed to register PTP clock: %d\n", rc);
1665bb77f36aSVladimir Oltean 		return rc;
1666bb77f36aSVladimir Oltean 	}
16678aa9ebccSVladimir Oltean 	/* Create and send configuration down to device */
16688aa9ebccSVladimir Oltean 	rc = sja1105_static_config_load(priv, ports);
16698aa9ebccSVladimir Oltean 	if (rc < 0) {
16708aa9ebccSVladimir Oltean 		dev_err(ds->dev, "Failed to load static config: %d\n", rc);
16718aa9ebccSVladimir Oltean 		return rc;
16728aa9ebccSVladimir Oltean 	}
16738aa9ebccSVladimir Oltean 	/* Configure the CGU (PHY link modes and speeds) */
16748aa9ebccSVladimir Oltean 	rc = sja1105_clocking_setup(priv);
16758aa9ebccSVladimir Oltean 	if (rc < 0) {
16768aa9ebccSVladimir Oltean 		dev_err(ds->dev, "Failed to configure MII clocking: %d\n", rc);
16778aa9ebccSVladimir Oltean 		return rc;
16788aa9ebccSVladimir Oltean 	}
16796666cebcSVladimir Oltean 	/* On SJA1105, VLAN filtering per se is always enabled in hardware.
16806666cebcSVladimir Oltean 	 * The only thing we can do to disable it is lie about what the 802.1Q
16816666cebcSVladimir Oltean 	 * EtherType is.
16826666cebcSVladimir Oltean 	 * So it will still try to apply VLAN filtering, but all ingress
16836666cebcSVladimir Oltean 	 * traffic (except frames received with EtherType of ETH_P_SJA1105)
16846666cebcSVladimir Oltean 	 * will be internally tagged with a distorted VLAN header where the
16856666cebcSVladimir Oltean 	 * TPID is ETH_P_SJA1105, and the VLAN ID is the port pvid.
16866666cebcSVladimir Oltean 	 */
16876666cebcSVladimir Oltean 	ds->vlan_filtering_is_global = true;
16888aa9ebccSVladimir Oltean 
1689227d07a0SVladimir Oltean 	/* The DSA/switchdev model brings up switch ports in standalone mode by
1690227d07a0SVladimir Oltean 	 * default, and that means vlan_filtering is 0 since they're not under
1691227d07a0SVladimir Oltean 	 * a bridge, so it's safe to set up switch tagging at this time.
1692227d07a0SVladimir Oltean 	 */
1693227d07a0SVladimir Oltean 	return sja1105_setup_8021q_tagging(ds, true);
1694227d07a0SVladimir Oltean }
1695227d07a0SVladimir Oltean 
1696f3097be2SVladimir Oltean static void sja1105_teardown(struct dsa_switch *ds)
1697f3097be2SVladimir Oltean {
1698f3097be2SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1699f3097be2SVladimir Oltean 
1700f3097be2SVladimir Oltean 	cancel_work_sync(&priv->tagger_data.rxtstamp_work);
1701f3097be2SVladimir Oltean 	skb_queue_purge(&priv->tagger_data.skb_rxtstamp_queue);
1702f3097be2SVladimir Oltean }
1703f3097be2SVladimir Oltean 
1704227d07a0SVladimir Oltean static int sja1105_mgmt_xmit(struct dsa_switch *ds, int port, int slot,
170547ed985eSVladimir Oltean 			     struct sk_buff *skb, bool takets)
1706227d07a0SVladimir Oltean {
1707227d07a0SVladimir Oltean 	struct sja1105_mgmt_entry mgmt_route = {0};
1708227d07a0SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1709227d07a0SVladimir Oltean 	struct ethhdr *hdr;
1710227d07a0SVladimir Oltean 	int timeout = 10;
1711227d07a0SVladimir Oltean 	int rc;
1712227d07a0SVladimir Oltean 
1713227d07a0SVladimir Oltean 	hdr = eth_hdr(skb);
1714227d07a0SVladimir Oltean 
1715227d07a0SVladimir Oltean 	mgmt_route.macaddr = ether_addr_to_u64(hdr->h_dest);
1716227d07a0SVladimir Oltean 	mgmt_route.destports = BIT(port);
1717227d07a0SVladimir Oltean 	mgmt_route.enfport = 1;
171847ed985eSVladimir Oltean 	mgmt_route.tsreg = 0;
171947ed985eSVladimir Oltean 	mgmt_route.takets = takets;
1720227d07a0SVladimir Oltean 
1721227d07a0SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE,
1722227d07a0SVladimir Oltean 					  slot, &mgmt_route, true);
1723227d07a0SVladimir Oltean 	if (rc < 0) {
1724227d07a0SVladimir Oltean 		kfree_skb(skb);
1725227d07a0SVladimir Oltean 		return rc;
1726227d07a0SVladimir Oltean 	}
1727227d07a0SVladimir Oltean 
1728227d07a0SVladimir Oltean 	/* Transfer skb to the host port. */
1729227d07a0SVladimir Oltean 	dsa_enqueue_skb(skb, ds->ports[port].slave);
1730227d07a0SVladimir Oltean 
1731227d07a0SVladimir Oltean 	/* Wait until the switch has processed the frame */
1732227d07a0SVladimir Oltean 	do {
1733227d07a0SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_MGMT_ROUTE,
1734227d07a0SVladimir Oltean 						 slot, &mgmt_route);
1735227d07a0SVladimir Oltean 		if (rc < 0) {
1736227d07a0SVladimir Oltean 			dev_err_ratelimited(priv->ds->dev,
1737227d07a0SVladimir Oltean 					    "failed to poll for mgmt route\n");
1738227d07a0SVladimir Oltean 			continue;
1739227d07a0SVladimir Oltean 		}
1740227d07a0SVladimir Oltean 
1741227d07a0SVladimir Oltean 		/* UM10944: The ENFPORT flag of the respective entry is
1742227d07a0SVladimir Oltean 		 * cleared when a match is found. The host can use this
1743227d07a0SVladimir Oltean 		 * flag as an acknowledgment.
1744227d07a0SVladimir Oltean 		 */
1745227d07a0SVladimir Oltean 		cpu_relax();
1746227d07a0SVladimir Oltean 	} while (mgmt_route.enfport && --timeout);
1747227d07a0SVladimir Oltean 
1748227d07a0SVladimir Oltean 	if (!timeout) {
1749227d07a0SVladimir Oltean 		/* Clean up the management route so that a follow-up
1750227d07a0SVladimir Oltean 		 * frame may not match on it by mistake.
17512a7e7409SVladimir Oltean 		 * This is only hardware supported on P/Q/R/S - on E/T it is
17522a7e7409SVladimir Oltean 		 * a no-op and we are silently discarding the -EOPNOTSUPP.
1753227d07a0SVladimir Oltean 		 */
1754227d07a0SVladimir Oltean 		sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE,
1755227d07a0SVladimir Oltean 					     slot, &mgmt_route, false);
1756227d07a0SVladimir Oltean 		dev_err_ratelimited(priv->ds->dev, "xmit timed out\n");
1757227d07a0SVladimir Oltean 	}
1758227d07a0SVladimir Oltean 
1759227d07a0SVladimir Oltean 	return NETDEV_TX_OK;
1760227d07a0SVladimir Oltean }
1761227d07a0SVladimir Oltean 
1762227d07a0SVladimir Oltean /* Deferred work is unfortunately necessary because setting up the management
1763227d07a0SVladimir Oltean  * route cannot be done from atomit context (SPI transfer takes a sleepable
1764227d07a0SVladimir Oltean  * lock on the bus)
1765227d07a0SVladimir Oltean  */
1766227d07a0SVladimir Oltean static netdev_tx_t sja1105_port_deferred_xmit(struct dsa_switch *ds, int port,
1767227d07a0SVladimir Oltean 					      struct sk_buff *skb)
1768227d07a0SVladimir Oltean {
1769227d07a0SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1770227d07a0SVladimir Oltean 	struct sja1105_port *sp = &priv->ports[port];
177147ed985eSVladimir Oltean 	struct skb_shared_hwtstamps shwt = {0};
1772227d07a0SVladimir Oltean 	int slot = sp->mgmt_slot;
177347ed985eSVladimir Oltean 	struct sk_buff *clone;
177447ed985eSVladimir Oltean 	u64 now, ts;
177547ed985eSVladimir Oltean 	int rc;
1776227d07a0SVladimir Oltean 
1777227d07a0SVladimir Oltean 	/* The tragic fact about the switch having 4x2 slots for installing
1778227d07a0SVladimir Oltean 	 * management routes is that all of them except one are actually
1779227d07a0SVladimir Oltean 	 * useless.
1780227d07a0SVladimir Oltean 	 * If 2 slots are simultaneously configured for two BPDUs sent to the
1781227d07a0SVladimir Oltean 	 * same (multicast) DMAC but on different egress ports, the switch
1782227d07a0SVladimir Oltean 	 * would confuse them and redirect first frame it receives on the CPU
1783227d07a0SVladimir Oltean 	 * port towards the port configured on the numerically first slot
1784227d07a0SVladimir Oltean 	 * (therefore wrong port), then second received frame on second slot
1785227d07a0SVladimir Oltean 	 * (also wrong port).
1786227d07a0SVladimir Oltean 	 * So for all practical purposes, there needs to be a lock that
1787227d07a0SVladimir Oltean 	 * prevents that from happening. The slot used here is utterly useless
1788227d07a0SVladimir Oltean 	 * (could have simply been 0 just as fine), but we are doing it
1789227d07a0SVladimir Oltean 	 * nonetheless, in case a smarter idea ever comes up in the future.
1790227d07a0SVladimir Oltean 	 */
1791227d07a0SVladimir Oltean 	mutex_lock(&priv->mgmt_lock);
1792227d07a0SVladimir Oltean 
179347ed985eSVladimir Oltean 	/* The clone, if there, was made by dsa_skb_tx_timestamp */
179447ed985eSVladimir Oltean 	clone = DSA_SKB_CB(skb)->clone;
1795227d07a0SVladimir Oltean 
179647ed985eSVladimir Oltean 	sja1105_mgmt_xmit(ds, port, slot, skb, !!clone);
179747ed985eSVladimir Oltean 
179847ed985eSVladimir Oltean 	if (!clone)
179947ed985eSVladimir Oltean 		goto out;
180047ed985eSVladimir Oltean 
180147ed985eSVladimir Oltean 	skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS;
180247ed985eSVladimir Oltean 
180347ed985eSVladimir Oltean 	mutex_lock(&priv->ptp_lock);
180447ed985eSVladimir Oltean 
180547ed985eSVladimir Oltean 	now = priv->tstamp_cc.read(&priv->tstamp_cc);
180647ed985eSVladimir Oltean 
180747ed985eSVladimir Oltean 	rc = sja1105_ptpegr_ts_poll(priv, slot, &ts);
180847ed985eSVladimir Oltean 	if (rc < 0) {
180947ed985eSVladimir Oltean 		dev_err(ds->dev, "xmit: timed out polling for tstamp\n");
181047ed985eSVladimir Oltean 		kfree_skb(clone);
181147ed985eSVladimir Oltean 		goto out_unlock_ptp;
181247ed985eSVladimir Oltean 	}
181347ed985eSVladimir Oltean 
181447ed985eSVladimir Oltean 	ts = sja1105_tstamp_reconstruct(priv, now, ts);
181547ed985eSVladimir Oltean 	ts = timecounter_cyc2time(&priv->tstamp_tc, ts);
181647ed985eSVladimir Oltean 
181747ed985eSVladimir Oltean 	shwt.hwtstamp = ns_to_ktime(ts);
181847ed985eSVladimir Oltean 	skb_complete_tx_timestamp(clone, &shwt);
181947ed985eSVladimir Oltean 
182047ed985eSVladimir Oltean out_unlock_ptp:
182147ed985eSVladimir Oltean 	mutex_unlock(&priv->ptp_lock);
182247ed985eSVladimir Oltean out:
1823227d07a0SVladimir Oltean 	mutex_unlock(&priv->mgmt_lock);
1824227d07a0SVladimir Oltean 	return NETDEV_TX_OK;
18258aa9ebccSVladimir Oltean }
18268aa9ebccSVladimir Oltean 
18278456721dSVladimir Oltean /* The MAXAGE setting belongs to the L2 Forwarding Parameters table,
18288456721dSVladimir Oltean  * which cannot be reconfigured at runtime. So a switch reset is required.
18298456721dSVladimir Oltean  */
18308456721dSVladimir Oltean static int sja1105_set_ageing_time(struct dsa_switch *ds,
18318456721dSVladimir Oltean 				   unsigned int ageing_time)
18328456721dSVladimir Oltean {
18338456721dSVladimir Oltean 	struct sja1105_l2_lookup_params_entry *l2_lookup_params;
18348456721dSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
18358456721dSVladimir Oltean 	struct sja1105_table *table;
18368456721dSVladimir Oltean 	unsigned int maxage;
18378456721dSVladimir Oltean 
18388456721dSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
18398456721dSVladimir Oltean 	l2_lookup_params = table->entries;
18408456721dSVladimir Oltean 
18418456721dSVladimir Oltean 	maxage = SJA1105_AGEING_TIME_MS(ageing_time);
18428456721dSVladimir Oltean 
18438456721dSVladimir Oltean 	if (l2_lookup_params->maxage == maxage)
18448456721dSVladimir Oltean 		return 0;
18458456721dSVladimir Oltean 
18468456721dSVladimir Oltean 	l2_lookup_params->maxage = maxage;
18478456721dSVladimir Oltean 
18488456721dSVladimir Oltean 	return sja1105_static_config_reload(priv);
18498456721dSVladimir Oltean }
18508456721dSVladimir Oltean 
1851a602afd2SVladimir Oltean /* Caller must hold priv->tagger_data.meta_lock */
1852a602afd2SVladimir Oltean static int sja1105_change_rxtstamping(struct sja1105_private *priv,
1853a602afd2SVladimir Oltean 				      bool on)
1854a602afd2SVladimir Oltean {
1855a602afd2SVladimir Oltean 	struct sja1105_general_params_entry *general_params;
1856a602afd2SVladimir Oltean 	struct sja1105_table *table;
1857a602afd2SVladimir Oltean 	int rc;
1858a602afd2SVladimir Oltean 
1859a602afd2SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
1860a602afd2SVladimir Oltean 	general_params = table->entries;
1861a602afd2SVladimir Oltean 	general_params->send_meta1 = on;
1862a602afd2SVladimir Oltean 	general_params->send_meta0 = on;
1863a602afd2SVladimir Oltean 
1864a602afd2SVladimir Oltean 	rc = sja1105_init_avb_params(priv, on);
1865a602afd2SVladimir Oltean 	if (rc < 0)
1866a602afd2SVladimir Oltean 		return rc;
1867a602afd2SVladimir Oltean 
1868a602afd2SVladimir Oltean 	/* Initialize the meta state machine to a known state */
1869a602afd2SVladimir Oltean 	if (priv->tagger_data.stampable_skb) {
1870a602afd2SVladimir Oltean 		kfree_skb(priv->tagger_data.stampable_skb);
1871a602afd2SVladimir Oltean 		priv->tagger_data.stampable_skb = NULL;
1872a602afd2SVladimir Oltean 	}
1873a602afd2SVladimir Oltean 
1874a602afd2SVladimir Oltean 	return sja1105_static_config_reload(priv);
1875a602afd2SVladimir Oltean }
1876a602afd2SVladimir Oltean 
1877a602afd2SVladimir Oltean static int sja1105_hwtstamp_set(struct dsa_switch *ds, int port,
1878a602afd2SVladimir Oltean 				struct ifreq *ifr)
1879a602afd2SVladimir Oltean {
1880a602afd2SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1881a602afd2SVladimir Oltean 	struct hwtstamp_config config;
1882a602afd2SVladimir Oltean 	bool rx_on;
1883a602afd2SVladimir Oltean 	int rc;
1884a602afd2SVladimir Oltean 
1885a602afd2SVladimir Oltean 	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
1886a602afd2SVladimir Oltean 		return -EFAULT;
1887a602afd2SVladimir Oltean 
1888a602afd2SVladimir Oltean 	switch (config.tx_type) {
1889a602afd2SVladimir Oltean 	case HWTSTAMP_TX_OFF:
1890a602afd2SVladimir Oltean 		priv->ports[port].hwts_tx_en = false;
1891a602afd2SVladimir Oltean 		break;
1892a602afd2SVladimir Oltean 	case HWTSTAMP_TX_ON:
1893a602afd2SVladimir Oltean 		priv->ports[port].hwts_tx_en = true;
1894a602afd2SVladimir Oltean 		break;
1895a602afd2SVladimir Oltean 	default:
1896a602afd2SVladimir Oltean 		return -ERANGE;
1897a602afd2SVladimir Oltean 	}
1898a602afd2SVladimir Oltean 
1899a602afd2SVladimir Oltean 	switch (config.rx_filter) {
1900a602afd2SVladimir Oltean 	case HWTSTAMP_FILTER_NONE:
1901a602afd2SVladimir Oltean 		rx_on = false;
1902a602afd2SVladimir Oltean 		break;
1903a602afd2SVladimir Oltean 	default:
1904a602afd2SVladimir Oltean 		rx_on = true;
1905a602afd2SVladimir Oltean 		break;
1906a602afd2SVladimir Oltean 	}
1907a602afd2SVladimir Oltean 
1908a602afd2SVladimir Oltean 	if (rx_on != priv->tagger_data.hwts_rx_en) {
1909a602afd2SVladimir Oltean 		spin_lock(&priv->tagger_data.meta_lock);
1910a602afd2SVladimir Oltean 		rc = sja1105_change_rxtstamping(priv, rx_on);
1911a602afd2SVladimir Oltean 		spin_unlock(&priv->tagger_data.meta_lock);
1912a602afd2SVladimir Oltean 		if (rc < 0) {
1913a602afd2SVladimir Oltean 			dev_err(ds->dev,
1914a602afd2SVladimir Oltean 				"Failed to change RX timestamping: %d\n", rc);
1915a602afd2SVladimir Oltean 			return -EFAULT;
1916a602afd2SVladimir Oltean 		}
1917a602afd2SVladimir Oltean 		priv->tagger_data.hwts_rx_en = rx_on;
1918a602afd2SVladimir Oltean 	}
1919a602afd2SVladimir Oltean 
1920a602afd2SVladimir Oltean 	if (copy_to_user(ifr->ifr_data, &config, sizeof(config)))
1921a602afd2SVladimir Oltean 		return -EFAULT;
1922a602afd2SVladimir Oltean 	return 0;
1923a602afd2SVladimir Oltean }
1924a602afd2SVladimir Oltean 
1925a602afd2SVladimir Oltean static int sja1105_hwtstamp_get(struct dsa_switch *ds, int port,
1926a602afd2SVladimir Oltean 				struct ifreq *ifr)
1927a602afd2SVladimir Oltean {
1928a602afd2SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1929a602afd2SVladimir Oltean 	struct hwtstamp_config config;
1930a602afd2SVladimir Oltean 
1931a602afd2SVladimir Oltean 	config.flags = 0;
1932a602afd2SVladimir Oltean 	if (priv->ports[port].hwts_tx_en)
1933a602afd2SVladimir Oltean 		config.tx_type = HWTSTAMP_TX_ON;
1934a602afd2SVladimir Oltean 	else
1935a602afd2SVladimir Oltean 		config.tx_type = HWTSTAMP_TX_OFF;
1936a602afd2SVladimir Oltean 	if (priv->tagger_data.hwts_rx_en)
1937a602afd2SVladimir Oltean 		config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
1938a602afd2SVladimir Oltean 	else
1939a602afd2SVladimir Oltean 		config.rx_filter = HWTSTAMP_FILTER_NONE;
1940a602afd2SVladimir Oltean 
1941a602afd2SVladimir Oltean 	return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
1942a602afd2SVladimir Oltean 		-EFAULT : 0;
1943a602afd2SVladimir Oltean }
1944a602afd2SVladimir Oltean 
1945f3097be2SVladimir Oltean #define to_tagger(d) \
1946f3097be2SVladimir Oltean 	container_of((d), struct sja1105_tagger_data, rxtstamp_work)
1947f3097be2SVladimir Oltean #define to_sja1105(d) \
1948f3097be2SVladimir Oltean 	container_of((d), struct sja1105_private, tagger_data)
1949f3097be2SVladimir Oltean 
1950f3097be2SVladimir Oltean static void sja1105_rxtstamp_work(struct work_struct *work)
1951f3097be2SVladimir Oltean {
1952f3097be2SVladimir Oltean 	struct sja1105_tagger_data *data = to_tagger(work);
1953f3097be2SVladimir Oltean 	struct sja1105_private *priv = to_sja1105(data);
1954f3097be2SVladimir Oltean 	struct sk_buff *skb;
1955f3097be2SVladimir Oltean 	u64 now;
1956f3097be2SVladimir Oltean 
1957f3097be2SVladimir Oltean 	mutex_lock(&priv->ptp_lock);
1958f3097be2SVladimir Oltean 
1959f3097be2SVladimir Oltean 	now = priv->tstamp_cc.read(&priv->tstamp_cc);
1960f3097be2SVladimir Oltean 
1961f3097be2SVladimir Oltean 	while ((skb = skb_dequeue(&data->skb_rxtstamp_queue)) != NULL) {
1962f3097be2SVladimir Oltean 		struct skb_shared_hwtstamps *shwt = skb_hwtstamps(skb);
1963f3097be2SVladimir Oltean 		u64 ts;
1964f3097be2SVladimir Oltean 
1965f3097be2SVladimir Oltean 		*shwt = (struct skb_shared_hwtstamps) {0};
1966f3097be2SVladimir Oltean 
1967f3097be2SVladimir Oltean 		ts = SJA1105_SKB_CB(skb)->meta_tstamp;
1968f3097be2SVladimir Oltean 		ts = sja1105_tstamp_reconstruct(priv, now, ts);
1969f3097be2SVladimir Oltean 		ts = timecounter_cyc2time(&priv->tstamp_tc, ts);
1970f3097be2SVladimir Oltean 
1971f3097be2SVladimir Oltean 		shwt->hwtstamp = ns_to_ktime(ts);
1972f3097be2SVladimir Oltean 		netif_rx_ni(skb);
1973f3097be2SVladimir Oltean 	}
1974f3097be2SVladimir Oltean 
1975f3097be2SVladimir Oltean 	mutex_unlock(&priv->ptp_lock);
1976f3097be2SVladimir Oltean }
1977f3097be2SVladimir Oltean 
1978f3097be2SVladimir Oltean /* Called from dsa_skb_defer_rx_timestamp */
19791dbb9869SYueHaibing static bool sja1105_port_rxtstamp(struct dsa_switch *ds, int port,
1980f3097be2SVladimir Oltean 				  struct sk_buff *skb, unsigned int type)
1981f3097be2SVladimir Oltean {
1982f3097be2SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1983f3097be2SVladimir Oltean 	struct sja1105_tagger_data *data = &priv->tagger_data;
1984f3097be2SVladimir Oltean 
1985f3097be2SVladimir Oltean 	if (!data->hwts_rx_en)
1986f3097be2SVladimir Oltean 		return false;
1987f3097be2SVladimir Oltean 
1988f3097be2SVladimir Oltean 	/* We need to read the full PTP clock to reconstruct the Rx
1989f3097be2SVladimir Oltean 	 * timestamp. For that we need a sleepable context.
1990f3097be2SVladimir Oltean 	 */
1991f3097be2SVladimir Oltean 	skb_queue_tail(&data->skb_rxtstamp_queue, skb);
1992f3097be2SVladimir Oltean 	schedule_work(&data->rxtstamp_work);
1993f3097be2SVladimir Oltean 	return true;
1994f3097be2SVladimir Oltean }
1995f3097be2SVladimir Oltean 
199647ed985eSVladimir Oltean /* Called from dsa_skb_tx_timestamp. This callback is just to make DSA clone
199747ed985eSVladimir Oltean  * the skb and have it available in DSA_SKB_CB in the .port_deferred_xmit
199847ed985eSVladimir Oltean  * callback, where we will timestamp it synchronously.
199947ed985eSVladimir Oltean  */
20001dbb9869SYueHaibing static bool sja1105_port_txtstamp(struct dsa_switch *ds, int port,
200147ed985eSVladimir Oltean 				  struct sk_buff *skb, unsigned int type)
200247ed985eSVladimir Oltean {
200347ed985eSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
200447ed985eSVladimir Oltean 	struct sja1105_port *sp = &priv->ports[port];
200547ed985eSVladimir Oltean 
200647ed985eSVladimir Oltean 	if (!sp->hwts_tx_en)
200747ed985eSVladimir Oltean 		return false;
200847ed985eSVladimir Oltean 
200947ed985eSVladimir Oltean 	return true;
201047ed985eSVladimir Oltean }
201147ed985eSVladimir Oltean 
20128aa9ebccSVladimir Oltean static const struct dsa_switch_ops sja1105_switch_ops = {
20138aa9ebccSVladimir Oltean 	.get_tag_protocol	= sja1105_get_tag_protocol,
20148aa9ebccSVladimir Oltean 	.setup			= sja1105_setup,
2015f3097be2SVladimir Oltean 	.teardown		= sja1105_teardown,
20168456721dSVladimir Oltean 	.set_ageing_time	= sja1105_set_ageing_time,
2017ad9f299aSVladimir Oltean 	.phylink_validate	= sja1105_phylink_validate,
2018af7cd036SVladimir Oltean 	.phylink_mac_config	= sja1105_mac_config,
20198400cff6SVladimir Oltean 	.phylink_mac_link_up	= sja1105_mac_link_up,
20208400cff6SVladimir Oltean 	.phylink_mac_link_down	= sja1105_mac_link_down,
202152c34e6eSVladimir Oltean 	.get_strings		= sja1105_get_strings,
202252c34e6eSVladimir Oltean 	.get_ethtool_stats	= sja1105_get_ethtool_stats,
202352c34e6eSVladimir Oltean 	.get_sset_count		= sja1105_get_sset_count,
2024bb77f36aSVladimir Oltean 	.get_ts_info		= sja1105_get_ts_info,
2025291d1e72SVladimir Oltean 	.port_fdb_dump		= sja1105_fdb_dump,
2026291d1e72SVladimir Oltean 	.port_fdb_add		= sja1105_fdb_add,
2027291d1e72SVladimir Oltean 	.port_fdb_del		= sja1105_fdb_del,
20288aa9ebccSVladimir Oltean 	.port_bridge_join	= sja1105_bridge_join,
20298aa9ebccSVladimir Oltean 	.port_bridge_leave	= sja1105_bridge_leave,
2030640f763fSVladimir Oltean 	.port_stp_state_set	= sja1105_bridge_stp_state_set,
20316666cebcSVladimir Oltean 	.port_vlan_prepare	= sja1105_vlan_prepare,
20326666cebcSVladimir Oltean 	.port_vlan_filtering	= sja1105_vlan_filtering,
20336666cebcSVladimir Oltean 	.port_vlan_add		= sja1105_vlan_add,
20346666cebcSVladimir Oltean 	.port_vlan_del		= sja1105_vlan_del,
2035291d1e72SVladimir Oltean 	.port_mdb_prepare	= sja1105_mdb_prepare,
2036291d1e72SVladimir Oltean 	.port_mdb_add		= sja1105_mdb_add,
2037291d1e72SVladimir Oltean 	.port_mdb_del		= sja1105_mdb_del,
2038227d07a0SVladimir Oltean 	.port_deferred_xmit	= sja1105_port_deferred_xmit,
2039a602afd2SVladimir Oltean 	.port_hwtstamp_get	= sja1105_hwtstamp_get,
2040a602afd2SVladimir Oltean 	.port_hwtstamp_set	= sja1105_hwtstamp_set,
2041f3097be2SVladimir Oltean 	.port_rxtstamp		= sja1105_port_rxtstamp,
204247ed985eSVladimir Oltean 	.port_txtstamp		= sja1105_port_txtstamp,
20438aa9ebccSVladimir Oltean };
20448aa9ebccSVladimir Oltean 
20458aa9ebccSVladimir Oltean static int sja1105_check_device_id(struct sja1105_private *priv)
20468aa9ebccSVladimir Oltean {
20478aa9ebccSVladimir Oltean 	const struct sja1105_regs *regs = priv->info->regs;
20488aa9ebccSVladimir Oltean 	u8 prod_id[SJA1105_SIZE_DEVICE_ID] = {0};
20498aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
20508aa9ebccSVladimir Oltean 	u64 device_id;
20518aa9ebccSVladimir Oltean 	u64 part_no;
20528aa9ebccSVladimir Oltean 	int rc;
20538aa9ebccSVladimir Oltean 
20548aa9ebccSVladimir Oltean 	rc = sja1105_spi_send_int(priv, SPI_READ, regs->device_id,
20558aa9ebccSVladimir Oltean 				  &device_id, SJA1105_SIZE_DEVICE_ID);
20568aa9ebccSVladimir Oltean 	if (rc < 0)
20578aa9ebccSVladimir Oltean 		return rc;
20588aa9ebccSVladimir Oltean 
20598aa9ebccSVladimir Oltean 	if (device_id != priv->info->device_id) {
20608aa9ebccSVladimir Oltean 		dev_err(dev, "Expected device ID 0x%llx but read 0x%llx\n",
20618aa9ebccSVladimir Oltean 			priv->info->device_id, device_id);
20628aa9ebccSVladimir Oltean 		return -ENODEV;
20638aa9ebccSVladimir Oltean 	}
20648aa9ebccSVladimir Oltean 
20658aa9ebccSVladimir Oltean 	rc = sja1105_spi_send_packed_buf(priv, SPI_READ, regs->prod_id,
20668aa9ebccSVladimir Oltean 					 prod_id, SJA1105_SIZE_DEVICE_ID);
20678aa9ebccSVladimir Oltean 	if (rc < 0)
20688aa9ebccSVladimir Oltean 		return rc;
20698aa9ebccSVladimir Oltean 
20708aa9ebccSVladimir Oltean 	sja1105_unpack(prod_id, &part_no, 19, 4, SJA1105_SIZE_DEVICE_ID);
20718aa9ebccSVladimir Oltean 
20728aa9ebccSVladimir Oltean 	if (part_no != priv->info->part_no) {
20738aa9ebccSVladimir Oltean 		dev_err(dev, "Expected part number 0x%llx but read 0x%llx\n",
20748aa9ebccSVladimir Oltean 			priv->info->part_no, part_no);
20758aa9ebccSVladimir Oltean 		return -ENODEV;
20768aa9ebccSVladimir Oltean 	}
20778aa9ebccSVladimir Oltean 
20788aa9ebccSVladimir Oltean 	return 0;
20798aa9ebccSVladimir Oltean }
20808aa9ebccSVladimir Oltean 
20818aa9ebccSVladimir Oltean static int sja1105_probe(struct spi_device *spi)
20828aa9ebccSVladimir Oltean {
2083844d7edcSVladimir Oltean 	struct sja1105_tagger_data *tagger_data;
20848aa9ebccSVladimir Oltean 	struct device *dev = &spi->dev;
20858aa9ebccSVladimir Oltean 	struct sja1105_private *priv;
20868aa9ebccSVladimir Oltean 	struct dsa_switch *ds;
2087227d07a0SVladimir Oltean 	int rc, i;
20888aa9ebccSVladimir Oltean 
20898aa9ebccSVladimir Oltean 	if (!dev->of_node) {
20908aa9ebccSVladimir Oltean 		dev_err(dev, "No DTS bindings for SJA1105 driver\n");
20918aa9ebccSVladimir Oltean 		return -EINVAL;
20928aa9ebccSVladimir Oltean 	}
20938aa9ebccSVladimir Oltean 
20948aa9ebccSVladimir Oltean 	priv = devm_kzalloc(dev, sizeof(struct sja1105_private), GFP_KERNEL);
20958aa9ebccSVladimir Oltean 	if (!priv)
20968aa9ebccSVladimir Oltean 		return -ENOMEM;
20978aa9ebccSVladimir Oltean 
20988aa9ebccSVladimir Oltean 	/* Configure the optional reset pin and bring up switch */
20998aa9ebccSVladimir Oltean 	priv->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
21008aa9ebccSVladimir Oltean 	if (IS_ERR(priv->reset_gpio))
21018aa9ebccSVladimir Oltean 		dev_dbg(dev, "reset-gpios not defined, ignoring\n");
21028aa9ebccSVladimir Oltean 	else
21038aa9ebccSVladimir Oltean 		sja1105_hw_reset(priv->reset_gpio, 1, 1);
21048aa9ebccSVladimir Oltean 
21058aa9ebccSVladimir Oltean 	/* Populate our driver private structure (priv) based on
21068aa9ebccSVladimir Oltean 	 * the device tree node that was probed (spi)
21078aa9ebccSVladimir Oltean 	 */
21088aa9ebccSVladimir Oltean 	priv->spidev = spi;
21098aa9ebccSVladimir Oltean 	spi_set_drvdata(spi, priv);
21108aa9ebccSVladimir Oltean 
21118aa9ebccSVladimir Oltean 	/* Configure the SPI bus */
21128aa9ebccSVladimir Oltean 	spi->bits_per_word = 8;
21138aa9ebccSVladimir Oltean 	rc = spi_setup(spi);
21148aa9ebccSVladimir Oltean 	if (rc < 0) {
21158aa9ebccSVladimir Oltean 		dev_err(dev, "Could not init SPI\n");
21168aa9ebccSVladimir Oltean 		return rc;
21178aa9ebccSVladimir Oltean 	}
21188aa9ebccSVladimir Oltean 
21198aa9ebccSVladimir Oltean 	priv->info = of_device_get_match_data(dev);
21208aa9ebccSVladimir Oltean 
21218aa9ebccSVladimir Oltean 	/* Detect hardware device */
21228aa9ebccSVladimir Oltean 	rc = sja1105_check_device_id(priv);
21238aa9ebccSVladimir Oltean 	if (rc < 0) {
21248aa9ebccSVladimir Oltean 		dev_err(dev, "Device ID check failed: %d\n", rc);
21258aa9ebccSVladimir Oltean 		return rc;
21268aa9ebccSVladimir Oltean 	}
21278aa9ebccSVladimir Oltean 
21288aa9ebccSVladimir Oltean 	dev_info(dev, "Probed switch chip: %s\n", priv->info->name);
21298aa9ebccSVladimir Oltean 
21308aa9ebccSVladimir Oltean 	ds = dsa_switch_alloc(dev, SJA1105_NUM_PORTS);
21318aa9ebccSVladimir Oltean 	if (!ds)
21328aa9ebccSVladimir Oltean 		return -ENOMEM;
21338aa9ebccSVladimir Oltean 
21348aa9ebccSVladimir Oltean 	ds->ops = &sja1105_switch_ops;
21358aa9ebccSVladimir Oltean 	ds->priv = priv;
21368aa9ebccSVladimir Oltean 	priv->ds = ds;
21378aa9ebccSVladimir Oltean 
2138844d7edcSVladimir Oltean 	tagger_data = &priv->tagger_data;
2139844d7edcSVladimir Oltean 	skb_queue_head_init(&tagger_data->skb_rxtstamp_queue);
2140f3097be2SVladimir Oltean 	INIT_WORK(&tagger_data->rxtstamp_work, sja1105_rxtstamp_work);
2141844d7edcSVladimir Oltean 
2142227d07a0SVladimir Oltean 	/* Connections between dsa_port and sja1105_port */
2143227d07a0SVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
2144227d07a0SVladimir Oltean 		struct sja1105_port *sp = &priv->ports[i];
2145227d07a0SVladimir Oltean 
2146227d07a0SVladimir Oltean 		ds->ports[i].priv = sp;
2147227d07a0SVladimir Oltean 		sp->dp = &ds->ports[i];
2148844d7edcSVladimir Oltean 		sp->data = tagger_data;
2149227d07a0SVladimir Oltean 	}
2150227d07a0SVladimir Oltean 	mutex_init(&priv->mgmt_lock);
2151227d07a0SVladimir Oltean 
21528aa9ebccSVladimir Oltean 	return dsa_register_switch(priv->ds);
21538aa9ebccSVladimir Oltean }
21548aa9ebccSVladimir Oltean 
21558aa9ebccSVladimir Oltean static int sja1105_remove(struct spi_device *spi)
21568aa9ebccSVladimir Oltean {
21578aa9ebccSVladimir Oltean 	struct sja1105_private *priv = spi_get_drvdata(spi);
21588aa9ebccSVladimir Oltean 
2159bb77f36aSVladimir Oltean 	sja1105_ptp_clock_unregister(priv);
21608aa9ebccSVladimir Oltean 	dsa_unregister_switch(priv->ds);
21618aa9ebccSVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
21628aa9ebccSVladimir Oltean 	return 0;
21638aa9ebccSVladimir Oltean }
21648aa9ebccSVladimir Oltean 
21658aa9ebccSVladimir Oltean static const struct of_device_id sja1105_dt_ids[] = {
21668aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105e", .data = &sja1105e_info },
21678aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105t", .data = &sja1105t_info },
21688aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105p", .data = &sja1105p_info },
21698aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105q", .data = &sja1105q_info },
21708aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105r", .data = &sja1105r_info },
21718aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105s", .data = &sja1105s_info },
21728aa9ebccSVladimir Oltean 	{ /* sentinel */ },
21738aa9ebccSVladimir Oltean };
21748aa9ebccSVladimir Oltean MODULE_DEVICE_TABLE(of, sja1105_dt_ids);
21758aa9ebccSVladimir Oltean 
21768aa9ebccSVladimir Oltean static struct spi_driver sja1105_driver = {
21778aa9ebccSVladimir Oltean 	.driver = {
21788aa9ebccSVladimir Oltean 		.name  = "sja1105",
21798aa9ebccSVladimir Oltean 		.owner = THIS_MODULE,
21808aa9ebccSVladimir Oltean 		.of_match_table = of_match_ptr(sja1105_dt_ids),
21818aa9ebccSVladimir Oltean 	},
21828aa9ebccSVladimir Oltean 	.probe  = sja1105_probe,
21838aa9ebccSVladimir Oltean 	.remove = sja1105_remove,
21848aa9ebccSVladimir Oltean };
21858aa9ebccSVladimir Oltean 
21868aa9ebccSVladimir Oltean module_spi_driver(sja1105_driver);
21878aa9ebccSVladimir Oltean 
21888aa9ebccSVladimir Oltean MODULE_AUTHOR("Vladimir Oltean <olteanv@gmail.com>");
21898aa9ebccSVladimir Oltean MODULE_AUTHOR("Georg Waibel <georg.waibel@sensor-technik.de>");
21908aa9ebccSVladimir Oltean MODULE_DESCRIPTION("SJA1105 Driver");
21918aa9ebccSVladimir Oltean MODULE_LICENSE("GPL v2");
2192