xref: /openbmc/linux/drivers/net/dsa/sja1105/sja1105_main.c (revision 511e6ca047457bcf200d9b6ad75e310b0e77af19)
18aa9ebccSVladimir Oltean // SPDX-License-Identifier: GPL-2.0
28aa9ebccSVladimir Oltean /* Copyright (c) 2018, Sensor-Technik Wiedemann GmbH
38aa9ebccSVladimir Oltean  * Copyright (c) 2018-2019, Vladimir Oltean <olteanv@gmail.com>
48aa9ebccSVladimir Oltean  */
58aa9ebccSVladimir Oltean 
68aa9ebccSVladimir Oltean #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
78aa9ebccSVladimir Oltean 
88aa9ebccSVladimir Oltean #include <linux/delay.h>
98aa9ebccSVladimir Oltean #include <linux/module.h>
108aa9ebccSVladimir Oltean #include <linux/printk.h>
118aa9ebccSVladimir Oltean #include <linux/spi/spi.h>
128aa9ebccSVladimir Oltean #include <linux/errno.h>
138aa9ebccSVladimir Oltean #include <linux/gpio/consumer.h>
14ad9f299aSVladimir Oltean #include <linux/phylink.h>
158aa9ebccSVladimir Oltean #include <linux/of.h>
168aa9ebccSVladimir Oltean #include <linux/of_net.h>
178aa9ebccSVladimir Oltean #include <linux/of_mdio.h>
188aa9ebccSVladimir Oltean #include <linux/of_device.h>
198aa9ebccSVladimir Oltean #include <linux/netdev_features.h>
208aa9ebccSVladimir Oltean #include <linux/netdevice.h>
218aa9ebccSVladimir Oltean #include <linux/if_bridge.h>
228aa9ebccSVladimir Oltean #include <linux/if_ether.h>
23227d07a0SVladimir Oltean #include <linux/dsa/8021q.h>
248aa9ebccSVladimir Oltean #include "sja1105.h"
25317ab5b8SVladimir Oltean #include "sja1105_tas.h"
268aa9ebccSVladimir Oltean 
278aa9ebccSVladimir Oltean static void sja1105_hw_reset(struct gpio_desc *gpio, unsigned int pulse_len,
288aa9ebccSVladimir Oltean 			     unsigned int startup_delay)
298aa9ebccSVladimir Oltean {
308aa9ebccSVladimir Oltean 	gpiod_set_value_cansleep(gpio, 1);
318aa9ebccSVladimir Oltean 	/* Wait for minimum reset pulse length */
328aa9ebccSVladimir Oltean 	msleep(pulse_len);
338aa9ebccSVladimir Oltean 	gpiod_set_value_cansleep(gpio, 0);
348aa9ebccSVladimir Oltean 	/* Wait until chip is ready after reset */
358aa9ebccSVladimir Oltean 	msleep(startup_delay);
368aa9ebccSVladimir Oltean }
378aa9ebccSVladimir Oltean 
388aa9ebccSVladimir Oltean static void
398aa9ebccSVladimir Oltean sja1105_port_allow_traffic(struct sja1105_l2_forwarding_entry *l2_fwd,
408aa9ebccSVladimir Oltean 			   int from, int to, bool allow)
418aa9ebccSVladimir Oltean {
428aa9ebccSVladimir Oltean 	if (allow) {
438aa9ebccSVladimir Oltean 		l2_fwd[from].bc_domain  |= BIT(to);
448aa9ebccSVladimir Oltean 		l2_fwd[from].reach_port |= BIT(to);
458aa9ebccSVladimir Oltean 		l2_fwd[from].fl_domain  |= BIT(to);
468aa9ebccSVladimir Oltean 	} else {
478aa9ebccSVladimir Oltean 		l2_fwd[from].bc_domain  &= ~BIT(to);
488aa9ebccSVladimir Oltean 		l2_fwd[from].reach_port &= ~BIT(to);
498aa9ebccSVladimir Oltean 		l2_fwd[from].fl_domain  &= ~BIT(to);
508aa9ebccSVladimir Oltean 	}
518aa9ebccSVladimir Oltean }
528aa9ebccSVladimir Oltean 
538aa9ebccSVladimir Oltean /* Structure used to temporarily transport device tree
548aa9ebccSVladimir Oltean  * settings into sja1105_setup
558aa9ebccSVladimir Oltean  */
568aa9ebccSVladimir Oltean struct sja1105_dt_port {
578aa9ebccSVladimir Oltean 	phy_interface_t phy_mode;
588aa9ebccSVladimir Oltean 	sja1105_mii_role_t role;
598aa9ebccSVladimir Oltean };
608aa9ebccSVladimir Oltean 
618aa9ebccSVladimir Oltean static int sja1105_init_mac_settings(struct sja1105_private *priv)
628aa9ebccSVladimir Oltean {
638aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry default_mac = {
648aa9ebccSVladimir Oltean 		/* Enable all 8 priority queues on egress.
658aa9ebccSVladimir Oltean 		 * Every queue i holds top[i] - base[i] frames.
668aa9ebccSVladimir Oltean 		 * Sum of top[i] - base[i] is 511 (max hardware limit).
678aa9ebccSVladimir Oltean 		 */
688aa9ebccSVladimir Oltean 		.top  = {0x3F, 0x7F, 0xBF, 0xFF, 0x13F, 0x17F, 0x1BF, 0x1FF},
698aa9ebccSVladimir Oltean 		.base = {0x0, 0x40, 0x80, 0xC0, 0x100, 0x140, 0x180, 0x1C0},
708aa9ebccSVladimir Oltean 		.enabled = {true, true, true, true, true, true, true, true},
718aa9ebccSVladimir Oltean 		/* Keep standard IFG of 12 bytes on egress. */
728aa9ebccSVladimir Oltean 		.ifg = 0,
738aa9ebccSVladimir Oltean 		/* Always put the MAC speed in automatic mode, where it can be
741fd4a173SVladimir Oltean 		 * adjusted at runtime by PHYLINK.
758aa9ebccSVladimir Oltean 		 */
768aa9ebccSVladimir Oltean 		.speed = SJA1105_SPEED_AUTO,
778aa9ebccSVladimir Oltean 		/* No static correction for 1-step 1588 events */
788aa9ebccSVladimir Oltean 		.tp_delin = 0,
798aa9ebccSVladimir Oltean 		.tp_delout = 0,
808aa9ebccSVladimir Oltean 		/* Disable aging for critical TTEthernet traffic */
818aa9ebccSVladimir Oltean 		.maxage = 0xFF,
828aa9ebccSVladimir Oltean 		/* Internal VLAN (pvid) to apply to untagged ingress */
838aa9ebccSVladimir Oltean 		.vlanprio = 0,
84e3502b82SVladimir Oltean 		.vlanid = 1,
858aa9ebccSVladimir Oltean 		.ing_mirr = false,
868aa9ebccSVladimir Oltean 		.egr_mirr = false,
878aa9ebccSVladimir Oltean 		/* Don't drop traffic with other EtherType than ETH_P_IP */
888aa9ebccSVladimir Oltean 		.drpnona664 = false,
898aa9ebccSVladimir Oltean 		/* Don't drop double-tagged traffic */
908aa9ebccSVladimir Oltean 		.drpdtag = false,
918aa9ebccSVladimir Oltean 		/* Don't drop untagged traffic */
928aa9ebccSVladimir Oltean 		.drpuntag = false,
938aa9ebccSVladimir Oltean 		/* Don't retag 802.1p (VID 0) traffic with the pvid */
948aa9ebccSVladimir Oltean 		.retag = false,
95640f763fSVladimir Oltean 		/* Disable learning and I/O on user ports by default -
96640f763fSVladimir Oltean 		 * STP will enable it.
97640f763fSVladimir Oltean 		 */
98640f763fSVladimir Oltean 		.dyn_learn = false,
998aa9ebccSVladimir Oltean 		.egress = false,
1008aa9ebccSVladimir Oltean 		.ingress = false,
1018aa9ebccSVladimir Oltean 	};
1028aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
1038aa9ebccSVladimir Oltean 	struct sja1105_table *table;
1048aa9ebccSVladimir Oltean 	int i;
1058aa9ebccSVladimir Oltean 
1068aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_MAC_CONFIG];
1078aa9ebccSVladimir Oltean 
1088aa9ebccSVladimir Oltean 	/* Discard previous MAC Configuration Table */
1098aa9ebccSVladimir Oltean 	if (table->entry_count) {
1108aa9ebccSVladimir Oltean 		kfree(table->entries);
1118aa9ebccSVladimir Oltean 		table->entry_count = 0;
1128aa9ebccSVladimir Oltean 	}
1138aa9ebccSVladimir Oltean 
1148aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_NUM_PORTS,
1158aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
1168aa9ebccSVladimir Oltean 	if (!table->entries)
1178aa9ebccSVladimir Oltean 		return -ENOMEM;
1188aa9ebccSVladimir Oltean 
1198aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_NUM_PORTS;
1208aa9ebccSVladimir Oltean 
1218aa9ebccSVladimir Oltean 	mac = table->entries;
1228aa9ebccSVladimir Oltean 
123640f763fSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
1248aa9ebccSVladimir Oltean 		mac[i] = default_mac;
125640f763fSVladimir Oltean 		if (i == dsa_upstream_port(priv->ds, i)) {
126640f763fSVladimir Oltean 			/* STP doesn't get called for CPU port, so we need to
127640f763fSVladimir Oltean 			 * set the I/O parameters statically.
128640f763fSVladimir Oltean 			 */
129640f763fSVladimir Oltean 			mac[i].dyn_learn = true;
130640f763fSVladimir Oltean 			mac[i].ingress = true;
131640f763fSVladimir Oltean 			mac[i].egress = true;
132640f763fSVladimir Oltean 		}
133640f763fSVladimir Oltean 	}
1348aa9ebccSVladimir Oltean 
1358aa9ebccSVladimir Oltean 	return 0;
1368aa9ebccSVladimir Oltean }
1378aa9ebccSVladimir Oltean 
1388aa9ebccSVladimir Oltean static int sja1105_init_mii_settings(struct sja1105_private *priv,
1398aa9ebccSVladimir Oltean 				     struct sja1105_dt_port *ports)
1408aa9ebccSVladimir Oltean {
1418aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
1428aa9ebccSVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
1438aa9ebccSVladimir Oltean 	struct sja1105_table *table;
1448aa9ebccSVladimir Oltean 	int i;
1458aa9ebccSVladimir Oltean 
1468aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_XMII_PARAMS];
1478aa9ebccSVladimir Oltean 
1488aa9ebccSVladimir Oltean 	/* Discard previous xMII Mode Parameters Table */
1498aa9ebccSVladimir Oltean 	if (table->entry_count) {
1508aa9ebccSVladimir Oltean 		kfree(table->entries);
1518aa9ebccSVladimir Oltean 		table->entry_count = 0;
1528aa9ebccSVladimir Oltean 	}
1538aa9ebccSVladimir Oltean 
1548aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_XMII_PARAMS_COUNT,
1558aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
1568aa9ebccSVladimir Oltean 	if (!table->entries)
1578aa9ebccSVladimir Oltean 		return -ENOMEM;
1588aa9ebccSVladimir Oltean 
1591fd4a173SVladimir Oltean 	/* Override table based on PHYLINK DT bindings */
1608aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_XMII_PARAMS_COUNT;
1618aa9ebccSVladimir Oltean 
1628aa9ebccSVladimir Oltean 	mii = table->entries;
1638aa9ebccSVladimir Oltean 
1648aa9ebccSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
1658aa9ebccSVladimir Oltean 		switch (ports[i].phy_mode) {
1668aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_MII:
1678aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_MII;
1688aa9ebccSVladimir Oltean 			break;
1698aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RMII:
1708aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_RMII;
1718aa9ebccSVladimir Oltean 			break;
1728aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII:
1738aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_ID:
1748aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_RXID:
1758aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_TXID:
1768aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_RGMII;
1778aa9ebccSVladimir Oltean 			break;
1788aa9ebccSVladimir Oltean 		default:
1798aa9ebccSVladimir Oltean 			dev_err(dev, "Unsupported PHY mode %s!\n",
1808aa9ebccSVladimir Oltean 				phy_modes(ports[i].phy_mode));
1818aa9ebccSVladimir Oltean 		}
1828aa9ebccSVladimir Oltean 
1838aa9ebccSVladimir Oltean 		mii->phy_mac[i] = ports[i].role;
1848aa9ebccSVladimir Oltean 	}
1858aa9ebccSVladimir Oltean 	return 0;
1868aa9ebccSVladimir Oltean }
1878aa9ebccSVladimir Oltean 
1888aa9ebccSVladimir Oltean static int sja1105_init_static_fdb(struct sja1105_private *priv)
1898aa9ebccSVladimir Oltean {
1908aa9ebccSVladimir Oltean 	struct sja1105_table *table;
1918aa9ebccSVladimir Oltean 
1928aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
1938aa9ebccSVladimir Oltean 
194291d1e72SVladimir Oltean 	/* We only populate the FDB table through dynamic
195291d1e72SVladimir Oltean 	 * L2 Address Lookup entries
196291d1e72SVladimir Oltean 	 */
1978aa9ebccSVladimir Oltean 	if (table->entry_count) {
1988aa9ebccSVladimir Oltean 		kfree(table->entries);
1998aa9ebccSVladimir Oltean 		table->entry_count = 0;
2008aa9ebccSVladimir Oltean 	}
2018aa9ebccSVladimir Oltean 	return 0;
2028aa9ebccSVladimir Oltean }
2038aa9ebccSVladimir Oltean 
2048aa9ebccSVladimir Oltean static int sja1105_init_l2_lookup_params(struct sja1105_private *priv)
2058aa9ebccSVladimir Oltean {
2068aa9ebccSVladimir Oltean 	struct sja1105_table *table;
2076c56e167SVladimir Oltean 	u64 max_fdb_entries = SJA1105_MAX_L2_LOOKUP_COUNT / SJA1105_NUM_PORTS;
2088aa9ebccSVladimir Oltean 	struct sja1105_l2_lookup_params_entry default_l2_lookup_params = {
2098456721dSVladimir Oltean 		/* Learned FDB entries are forgotten after 300 seconds */
2108456721dSVladimir Oltean 		.maxage = SJA1105_AGEING_TIME_MS(300000),
2118aa9ebccSVladimir Oltean 		/* All entries within a FDB bin are available for learning */
2128aa9ebccSVladimir Oltean 		.dyn_tbsz = SJA1105ET_FDB_BIN_SIZE,
2131da73821SVladimir Oltean 		/* And the P/Q/R/S equivalent setting: */
2141da73821SVladimir Oltean 		.start_dynspc = 0,
2156c56e167SVladimir Oltean 		.maxaddrp = {max_fdb_entries, max_fdb_entries, max_fdb_entries,
2166c56e167SVladimir Oltean 			     max_fdb_entries, max_fdb_entries, },
2178aa9ebccSVladimir Oltean 		/* 2^8 + 2^5 + 2^3 + 2^2 + 2^1 + 1 in Koopman notation */
2188aa9ebccSVladimir Oltean 		.poly = 0x97,
2198aa9ebccSVladimir Oltean 		/* This selects between Independent VLAN Learning (IVL) and
2208aa9ebccSVladimir Oltean 		 * Shared VLAN Learning (SVL)
2218aa9ebccSVladimir Oltean 		 */
2226d7c7d94SVladimir Oltean 		.shared_learn = true,
2238aa9ebccSVladimir Oltean 		/* Don't discard management traffic based on ENFPORT -
2248aa9ebccSVladimir Oltean 		 * we don't perform SMAC port enforcement anyway, so
2258aa9ebccSVladimir Oltean 		 * what we are setting here doesn't matter.
2268aa9ebccSVladimir Oltean 		 */
2278aa9ebccSVladimir Oltean 		.no_enf_hostprt = false,
2288aa9ebccSVladimir Oltean 		/* Don't learn SMAC for mac_fltres1 and mac_fltres0.
2298aa9ebccSVladimir Oltean 		 * Maybe correlate with no_linklocal_learn from bridge driver?
2308aa9ebccSVladimir Oltean 		 */
2318aa9ebccSVladimir Oltean 		.no_mgmt_learn = true,
2321da73821SVladimir Oltean 		/* P/Q/R/S only */
2331da73821SVladimir Oltean 		.use_static = true,
2341da73821SVladimir Oltean 		/* Dynamically learned FDB entries can overwrite other (older)
2351da73821SVladimir Oltean 		 * dynamic FDB entries
2361da73821SVladimir Oltean 		 */
2371da73821SVladimir Oltean 		.owr_dyn = true,
2381da73821SVladimir Oltean 		.drpnolearn = true,
2398aa9ebccSVladimir Oltean 	};
2408aa9ebccSVladimir Oltean 
2418aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
2428aa9ebccSVladimir Oltean 
2438aa9ebccSVladimir Oltean 	if (table->entry_count) {
2448aa9ebccSVladimir Oltean 		kfree(table->entries);
2458aa9ebccSVladimir Oltean 		table->entry_count = 0;
2468aa9ebccSVladimir Oltean 	}
2478aa9ebccSVladimir Oltean 
2488aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT,
2498aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
2508aa9ebccSVladimir Oltean 	if (!table->entries)
2518aa9ebccSVladimir Oltean 		return -ENOMEM;
2528aa9ebccSVladimir Oltean 
2538aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT;
2548aa9ebccSVladimir Oltean 
2558aa9ebccSVladimir Oltean 	/* This table only has a single entry */
2568aa9ebccSVladimir Oltean 	((struct sja1105_l2_lookup_params_entry *)table->entries)[0] =
2578aa9ebccSVladimir Oltean 				default_l2_lookup_params;
2588aa9ebccSVladimir Oltean 
2598aa9ebccSVladimir Oltean 	return 0;
2608aa9ebccSVladimir Oltean }
2618aa9ebccSVladimir Oltean 
2628aa9ebccSVladimir Oltean static int sja1105_init_static_vlan(struct sja1105_private *priv)
2638aa9ebccSVladimir Oltean {
2648aa9ebccSVladimir Oltean 	struct sja1105_table *table;
2658aa9ebccSVladimir Oltean 	struct sja1105_vlan_lookup_entry pvid = {
2668aa9ebccSVladimir Oltean 		.ving_mirr = 0,
2678aa9ebccSVladimir Oltean 		.vegr_mirr = 0,
2688aa9ebccSVladimir Oltean 		.vmemb_port = 0,
2698aa9ebccSVladimir Oltean 		.vlan_bc = 0,
2708aa9ebccSVladimir Oltean 		.tag_port = 0,
271e3502b82SVladimir Oltean 		.vlanid = 1,
2728aa9ebccSVladimir Oltean 	};
2738aa9ebccSVladimir Oltean 	int i;
2748aa9ebccSVladimir Oltean 
2758aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
2768aa9ebccSVladimir Oltean 
277e3502b82SVladimir Oltean 	/* The static VLAN table will only contain the initial pvid of 1.
2786666cebcSVladimir Oltean 	 * All other VLANs are to be configured through dynamic entries,
2796666cebcSVladimir Oltean 	 * and kept in the static configuration table as backing memory.
2808aa9ebccSVladimir Oltean 	 */
2818aa9ebccSVladimir Oltean 	if (table->entry_count) {
2828aa9ebccSVladimir Oltean 		kfree(table->entries);
2838aa9ebccSVladimir Oltean 		table->entry_count = 0;
2848aa9ebccSVladimir Oltean 	}
2858aa9ebccSVladimir Oltean 
2868aa9ebccSVladimir Oltean 	table->entries = kcalloc(1, table->ops->unpacked_entry_size,
2878aa9ebccSVladimir Oltean 				 GFP_KERNEL);
2888aa9ebccSVladimir Oltean 	if (!table->entries)
2898aa9ebccSVladimir Oltean 		return -ENOMEM;
2908aa9ebccSVladimir Oltean 
2918aa9ebccSVladimir Oltean 	table->entry_count = 1;
2928aa9ebccSVladimir Oltean 
293e3502b82SVladimir Oltean 	/* VLAN 1: all DT-defined ports are members; no restrictions on
2948aa9ebccSVladimir Oltean 	 * forwarding; always transmit priority-tagged frames as untagged.
2958aa9ebccSVladimir Oltean 	 */
2968aa9ebccSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
2978aa9ebccSVladimir Oltean 		pvid.vmemb_port |= BIT(i);
2988aa9ebccSVladimir Oltean 		pvid.vlan_bc |= BIT(i);
2998aa9ebccSVladimir Oltean 		pvid.tag_port &= ~BIT(i);
3008aa9ebccSVladimir Oltean 	}
3018aa9ebccSVladimir Oltean 
3028aa9ebccSVladimir Oltean 	((struct sja1105_vlan_lookup_entry *)table->entries)[0] = pvid;
3038aa9ebccSVladimir Oltean 	return 0;
3048aa9ebccSVladimir Oltean }
3058aa9ebccSVladimir Oltean 
3068aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding(struct sja1105_private *priv)
3078aa9ebccSVladimir Oltean {
3088aa9ebccSVladimir Oltean 	struct sja1105_l2_forwarding_entry *l2fwd;
3098aa9ebccSVladimir Oltean 	struct sja1105_table *table;
3108aa9ebccSVladimir Oltean 	int i, j;
3118aa9ebccSVladimir Oltean 
3128aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING];
3138aa9ebccSVladimir Oltean 
3148aa9ebccSVladimir Oltean 	if (table->entry_count) {
3158aa9ebccSVladimir Oltean 		kfree(table->entries);
3168aa9ebccSVladimir Oltean 		table->entry_count = 0;
3178aa9ebccSVladimir Oltean 	}
3188aa9ebccSVladimir Oltean 
3198aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_L2_FORWARDING_COUNT,
3208aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
3218aa9ebccSVladimir Oltean 	if (!table->entries)
3228aa9ebccSVladimir Oltean 		return -ENOMEM;
3238aa9ebccSVladimir Oltean 
3248aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_L2_FORWARDING_COUNT;
3258aa9ebccSVladimir Oltean 
3268aa9ebccSVladimir Oltean 	l2fwd = table->entries;
3278aa9ebccSVladimir Oltean 
3288aa9ebccSVladimir Oltean 	/* First 5 entries define the forwarding rules */
3298aa9ebccSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
3308aa9ebccSVladimir Oltean 		unsigned int upstream = dsa_upstream_port(priv->ds, i);
3318aa9ebccSVladimir Oltean 
3328aa9ebccSVladimir Oltean 		for (j = 0; j < SJA1105_NUM_TC; j++)
3338aa9ebccSVladimir Oltean 			l2fwd[i].vlan_pmap[j] = j;
3348aa9ebccSVladimir Oltean 
3358aa9ebccSVladimir Oltean 		if (i == upstream)
3368aa9ebccSVladimir Oltean 			continue;
3378aa9ebccSVladimir Oltean 
3388aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2fwd, i, upstream, true);
3398aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2fwd, upstream, i, true);
3408aa9ebccSVladimir Oltean 	}
3418aa9ebccSVladimir Oltean 	/* Next 8 entries define VLAN PCP mapping from ingress to egress.
3428aa9ebccSVladimir Oltean 	 * Create a one-to-one mapping.
3438aa9ebccSVladimir Oltean 	 */
3448aa9ebccSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_TC; i++)
3458aa9ebccSVladimir Oltean 		for (j = 0; j < SJA1105_NUM_PORTS; j++)
3468aa9ebccSVladimir Oltean 			l2fwd[SJA1105_NUM_PORTS + i].vlan_pmap[j] = i;
3478aa9ebccSVladimir Oltean 
3488aa9ebccSVladimir Oltean 	return 0;
3498aa9ebccSVladimir Oltean }
3508aa9ebccSVladimir Oltean 
3518aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding_params(struct sja1105_private *priv)
3528aa9ebccSVladimir Oltean {
3538aa9ebccSVladimir Oltean 	struct sja1105_l2_forwarding_params_entry default_l2fwd_params = {
3548aa9ebccSVladimir Oltean 		/* Disallow dynamic reconfiguration of vlan_pmap */
3558aa9ebccSVladimir Oltean 		.max_dynp = 0,
3568aa9ebccSVladimir Oltean 		/* Use a single memory partition for all ingress queues */
3578aa9ebccSVladimir Oltean 		.part_spc = { SJA1105_MAX_FRAME_MEMORY, 0, 0, 0, 0, 0, 0, 0 },
3588aa9ebccSVladimir Oltean 	};
3598aa9ebccSVladimir Oltean 	struct sja1105_table *table;
3608aa9ebccSVladimir Oltean 
3618aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS];
3628aa9ebccSVladimir Oltean 
3638aa9ebccSVladimir Oltean 	if (table->entry_count) {
3648aa9ebccSVladimir Oltean 		kfree(table->entries);
3658aa9ebccSVladimir Oltean 		table->entry_count = 0;
3668aa9ebccSVladimir Oltean 	}
3678aa9ebccSVladimir Oltean 
3688aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT,
3698aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
3708aa9ebccSVladimir Oltean 	if (!table->entries)
3718aa9ebccSVladimir Oltean 		return -ENOMEM;
3728aa9ebccSVladimir Oltean 
3738aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT;
3748aa9ebccSVladimir Oltean 
3758aa9ebccSVladimir Oltean 	/* This table only has a single entry */
3768aa9ebccSVladimir Oltean 	((struct sja1105_l2_forwarding_params_entry *)table->entries)[0] =
3778aa9ebccSVladimir Oltean 				default_l2fwd_params;
3788aa9ebccSVladimir Oltean 
3798aa9ebccSVladimir Oltean 	return 0;
3808aa9ebccSVladimir Oltean }
3818aa9ebccSVladimir Oltean 
3828aa9ebccSVladimir Oltean static int sja1105_init_general_params(struct sja1105_private *priv)
3838aa9ebccSVladimir Oltean {
3848aa9ebccSVladimir Oltean 	struct sja1105_general_params_entry default_general_params = {
385*511e6ca0SVladimir Oltean 		/* Allow dynamic changing of the mirror port */
386*511e6ca0SVladimir Oltean 		.mirr_ptacu = true,
3878aa9ebccSVladimir Oltean 		.switchid = priv->ds->index,
3885f06c63bSVladimir Oltean 		/* Priority queue for link-local management frames
3895f06c63bSVladimir Oltean 		 * (both ingress to and egress from CPU - PTP, STP etc)
3905f06c63bSVladimir Oltean 		 */
39108fde09aSVladimir Oltean 		.hostprio = 7,
3928aa9ebccSVladimir Oltean 		.mac_fltres1 = SJA1105_LINKLOCAL_FILTER_A,
3938aa9ebccSVladimir Oltean 		.mac_flt1    = SJA1105_LINKLOCAL_FILTER_A_MASK,
39442824463SVladimir Oltean 		.incl_srcpt1 = false,
3958aa9ebccSVladimir Oltean 		.send_meta1  = false,
3968aa9ebccSVladimir Oltean 		.mac_fltres0 = SJA1105_LINKLOCAL_FILTER_B,
3978aa9ebccSVladimir Oltean 		.mac_flt0    = SJA1105_LINKLOCAL_FILTER_B_MASK,
39842824463SVladimir Oltean 		.incl_srcpt0 = false,
3998aa9ebccSVladimir Oltean 		.send_meta0  = false,
4008aa9ebccSVladimir Oltean 		/* The destination for traffic matching mac_fltres1 and
4018aa9ebccSVladimir Oltean 		 * mac_fltres0 on all ports except host_port. Such traffic
4028aa9ebccSVladimir Oltean 		 * receieved on host_port itself would be dropped, except
4038aa9ebccSVladimir Oltean 		 * by installing a temporary 'management route'
4048aa9ebccSVladimir Oltean 		 */
4058aa9ebccSVladimir Oltean 		.host_port = dsa_upstream_port(priv->ds, 0),
406*511e6ca0SVladimir Oltean 		/* Default to an invalid value */
407*511e6ca0SVladimir Oltean 		.mirr_port = SJA1105_NUM_PORTS,
4088aa9ebccSVladimir Oltean 		/* Link-local traffic received on casc_port will be forwarded
4098aa9ebccSVladimir Oltean 		 * to host_port without embedding the source port and device ID
4108aa9ebccSVladimir Oltean 		 * info in the destination MAC address (presumably because it
4118aa9ebccSVladimir Oltean 		 * is a cascaded port and a downstream SJA switch already did
4128aa9ebccSVladimir Oltean 		 * that). Default to an invalid port (to disable the feature)
4138aa9ebccSVladimir Oltean 		 * and overwrite this if we find any DSA (cascaded) ports.
4148aa9ebccSVladimir Oltean 		 */
4158aa9ebccSVladimir Oltean 		.casc_port = SJA1105_NUM_PORTS,
4168aa9ebccSVladimir Oltean 		/* No TTEthernet */
4178aa9ebccSVladimir Oltean 		.vllupformat = 0,
4188aa9ebccSVladimir Oltean 		.vlmarker = 0,
4198aa9ebccSVladimir Oltean 		.vlmask = 0,
4208aa9ebccSVladimir Oltean 		/* Only update correctionField for 1-step PTP (L2 transport) */
4218aa9ebccSVladimir Oltean 		.ignore2stf = 0,
4226666cebcSVladimir Oltean 		/* Forcefully disable VLAN filtering by telling
4236666cebcSVladimir Oltean 		 * the switch that VLAN has a different EtherType.
4246666cebcSVladimir Oltean 		 */
4256666cebcSVladimir Oltean 		.tpid = ETH_P_SJA1105,
4266666cebcSVladimir Oltean 		.tpid2 = ETH_P_SJA1105,
4278aa9ebccSVladimir Oltean 	};
4288aa9ebccSVladimir Oltean 	struct sja1105_table *table;
429227d07a0SVladimir Oltean 	int i, k = 0;
4308aa9ebccSVladimir Oltean 
431227d07a0SVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
4328aa9ebccSVladimir Oltean 		if (dsa_is_dsa_port(priv->ds, i))
4338aa9ebccSVladimir Oltean 			default_general_params.casc_port = i;
434227d07a0SVladimir Oltean 		else if (dsa_is_user_port(priv->ds, i))
435227d07a0SVladimir Oltean 			priv->ports[i].mgmt_slot = k++;
436227d07a0SVladimir Oltean 	}
4378aa9ebccSVladimir Oltean 
4388aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
4398aa9ebccSVladimir Oltean 
4408aa9ebccSVladimir Oltean 	if (table->entry_count) {
4418aa9ebccSVladimir Oltean 		kfree(table->entries);
4428aa9ebccSVladimir Oltean 		table->entry_count = 0;
4438aa9ebccSVladimir Oltean 	}
4448aa9ebccSVladimir Oltean 
4458aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_GENERAL_PARAMS_COUNT,
4468aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
4478aa9ebccSVladimir Oltean 	if (!table->entries)
4488aa9ebccSVladimir Oltean 		return -ENOMEM;
4498aa9ebccSVladimir Oltean 
4508aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_GENERAL_PARAMS_COUNT;
4518aa9ebccSVladimir Oltean 
4528aa9ebccSVladimir Oltean 	/* This table only has a single entry */
4538aa9ebccSVladimir Oltean 	((struct sja1105_general_params_entry *)table->entries)[0] =
4548aa9ebccSVladimir Oltean 				default_general_params;
4558aa9ebccSVladimir Oltean 
4568aa9ebccSVladimir Oltean 	return 0;
4578aa9ebccSVladimir Oltean }
4588aa9ebccSVladimir Oltean 
4598aa9ebccSVladimir Oltean #define SJA1105_RATE_MBPS(speed) (((speed) * 64000) / 1000)
4608aa9ebccSVladimir Oltean 
46109c1b412SVladimir Oltean static void sja1105_setup_policer(struct sja1105_l2_policing_entry *policing,
4628aa9ebccSVladimir Oltean 				  int index)
4638aa9ebccSVladimir Oltean {
4648aa9ebccSVladimir Oltean 	policing[index].sharindx = index;
4658aa9ebccSVladimir Oltean 	policing[index].smax = 65535; /* Burst size in bytes */
4668aa9ebccSVladimir Oltean 	policing[index].rate = SJA1105_RATE_MBPS(1000);
4678aa9ebccSVladimir Oltean 	policing[index].maxlen = ETH_FRAME_LEN + VLAN_HLEN + ETH_FCS_LEN;
4688aa9ebccSVladimir Oltean 	policing[index].partition = 0;
4698aa9ebccSVladimir Oltean }
4708aa9ebccSVladimir Oltean 
4718aa9ebccSVladimir Oltean static int sja1105_init_l2_policing(struct sja1105_private *priv)
4728aa9ebccSVladimir Oltean {
4738aa9ebccSVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
4748aa9ebccSVladimir Oltean 	struct sja1105_table *table;
4758aa9ebccSVladimir Oltean 	int i, j, k;
4768aa9ebccSVladimir Oltean 
4778aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_POLICING];
4788aa9ebccSVladimir Oltean 
4798aa9ebccSVladimir Oltean 	/* Discard previous L2 Policing Table */
4808aa9ebccSVladimir Oltean 	if (table->entry_count) {
4818aa9ebccSVladimir Oltean 		kfree(table->entries);
4828aa9ebccSVladimir Oltean 		table->entry_count = 0;
4838aa9ebccSVladimir Oltean 	}
4848aa9ebccSVladimir Oltean 
4858aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_L2_POLICING_COUNT,
4868aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
4878aa9ebccSVladimir Oltean 	if (!table->entries)
4888aa9ebccSVladimir Oltean 		return -ENOMEM;
4898aa9ebccSVladimir Oltean 
4908aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_L2_POLICING_COUNT;
4918aa9ebccSVladimir Oltean 
4928aa9ebccSVladimir Oltean 	policing = table->entries;
4938aa9ebccSVladimir Oltean 
4948aa9ebccSVladimir Oltean 	/* k sweeps through all unicast policers (0-39).
4958aa9ebccSVladimir Oltean 	 * bcast sweeps through policers 40-44.
4968aa9ebccSVladimir Oltean 	 */
4978aa9ebccSVladimir Oltean 	for (i = 0, k = 0; i < SJA1105_NUM_PORTS; i++) {
4988aa9ebccSVladimir Oltean 		int bcast = (SJA1105_NUM_PORTS * SJA1105_NUM_TC) + i;
4998aa9ebccSVladimir Oltean 
5008aa9ebccSVladimir Oltean 		for (j = 0; j < SJA1105_NUM_TC; j++, k++)
5018aa9ebccSVladimir Oltean 			sja1105_setup_policer(policing, k);
5028aa9ebccSVladimir Oltean 
5038aa9ebccSVladimir Oltean 		/* Set up this port's policer for broadcast traffic */
5048aa9ebccSVladimir Oltean 		sja1105_setup_policer(policing, bcast);
5058aa9ebccSVladimir Oltean 	}
5068aa9ebccSVladimir Oltean 	return 0;
5078aa9ebccSVladimir Oltean }
5088aa9ebccSVladimir Oltean 
50924c01949SVladimir Oltean static int sja1105_init_avb_params(struct sja1105_private *priv,
51024c01949SVladimir Oltean 				   bool on)
51124c01949SVladimir Oltean {
51224c01949SVladimir Oltean 	struct sja1105_avb_params_entry *avb;
51324c01949SVladimir Oltean 	struct sja1105_table *table;
51424c01949SVladimir Oltean 
51524c01949SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_AVB_PARAMS];
51624c01949SVladimir Oltean 
51724c01949SVladimir Oltean 	/* Discard previous AVB Parameters Table */
51824c01949SVladimir Oltean 	if (table->entry_count) {
51924c01949SVladimir Oltean 		kfree(table->entries);
52024c01949SVladimir Oltean 		table->entry_count = 0;
52124c01949SVladimir Oltean 	}
52224c01949SVladimir Oltean 
52324c01949SVladimir Oltean 	/* Configure the reception of meta frames only if requested */
52424c01949SVladimir Oltean 	if (!on)
52524c01949SVladimir Oltean 		return 0;
52624c01949SVladimir Oltean 
52724c01949SVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_AVB_PARAMS_COUNT,
52824c01949SVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
52924c01949SVladimir Oltean 	if (!table->entries)
53024c01949SVladimir Oltean 		return -ENOMEM;
53124c01949SVladimir Oltean 
53224c01949SVladimir Oltean 	table->entry_count = SJA1105_MAX_AVB_PARAMS_COUNT;
53324c01949SVladimir Oltean 
53424c01949SVladimir Oltean 	avb = table->entries;
53524c01949SVladimir Oltean 
53624c01949SVladimir Oltean 	avb->destmeta = SJA1105_META_DMAC;
53724c01949SVladimir Oltean 	avb->srcmeta  = SJA1105_META_SMAC;
53824c01949SVladimir Oltean 
53924c01949SVladimir Oltean 	return 0;
54024c01949SVladimir Oltean }
54124c01949SVladimir Oltean 
5428aa9ebccSVladimir Oltean static int sja1105_static_config_load(struct sja1105_private *priv,
5438aa9ebccSVladimir Oltean 				      struct sja1105_dt_port *ports)
5448aa9ebccSVladimir Oltean {
5458aa9ebccSVladimir Oltean 	int rc;
5468aa9ebccSVladimir Oltean 
5478aa9ebccSVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
5488aa9ebccSVladimir Oltean 	rc = sja1105_static_config_init(&priv->static_config,
5498aa9ebccSVladimir Oltean 					priv->info->static_ops,
5508aa9ebccSVladimir Oltean 					priv->info->device_id);
5518aa9ebccSVladimir Oltean 	if (rc)
5528aa9ebccSVladimir Oltean 		return rc;
5538aa9ebccSVladimir Oltean 
5548aa9ebccSVladimir Oltean 	/* Build static configuration */
5558aa9ebccSVladimir Oltean 	rc = sja1105_init_mac_settings(priv);
5568aa9ebccSVladimir Oltean 	if (rc < 0)
5578aa9ebccSVladimir Oltean 		return rc;
5588aa9ebccSVladimir Oltean 	rc = sja1105_init_mii_settings(priv, ports);
5598aa9ebccSVladimir Oltean 	if (rc < 0)
5608aa9ebccSVladimir Oltean 		return rc;
5618aa9ebccSVladimir Oltean 	rc = sja1105_init_static_fdb(priv);
5628aa9ebccSVladimir Oltean 	if (rc < 0)
5638aa9ebccSVladimir Oltean 		return rc;
5648aa9ebccSVladimir Oltean 	rc = sja1105_init_static_vlan(priv);
5658aa9ebccSVladimir Oltean 	if (rc < 0)
5668aa9ebccSVladimir Oltean 		return rc;
5678aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_lookup_params(priv);
5688aa9ebccSVladimir Oltean 	if (rc < 0)
5698aa9ebccSVladimir Oltean 		return rc;
5708aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_forwarding(priv);
5718aa9ebccSVladimir Oltean 	if (rc < 0)
5728aa9ebccSVladimir Oltean 		return rc;
5738aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_forwarding_params(priv);
5748aa9ebccSVladimir Oltean 	if (rc < 0)
5758aa9ebccSVladimir Oltean 		return rc;
5768aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_policing(priv);
5778aa9ebccSVladimir Oltean 	if (rc < 0)
5788aa9ebccSVladimir Oltean 		return rc;
5798aa9ebccSVladimir Oltean 	rc = sja1105_init_general_params(priv);
5808aa9ebccSVladimir Oltean 	if (rc < 0)
5818aa9ebccSVladimir Oltean 		return rc;
58224c01949SVladimir Oltean 	rc = sja1105_init_avb_params(priv, false);
58324c01949SVladimir Oltean 	if (rc < 0)
58424c01949SVladimir Oltean 		return rc;
5858aa9ebccSVladimir Oltean 
5868aa9ebccSVladimir Oltean 	/* Send initial configuration to hardware via SPI */
5878aa9ebccSVladimir Oltean 	return sja1105_static_config_upload(priv);
5888aa9ebccSVladimir Oltean }
5898aa9ebccSVladimir Oltean 
590f5b8631cSVladimir Oltean static int sja1105_parse_rgmii_delays(struct sja1105_private *priv,
591f5b8631cSVladimir Oltean 				      const struct sja1105_dt_port *ports)
592f5b8631cSVladimir Oltean {
593f5b8631cSVladimir Oltean 	int i;
594f5b8631cSVladimir Oltean 
595f5b8631cSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
596f5b8631cSVladimir Oltean 		if (ports->role == XMII_MAC)
597f5b8631cSVladimir Oltean 			continue;
598f5b8631cSVladimir Oltean 
599f5b8631cSVladimir Oltean 		if (ports->phy_mode == PHY_INTERFACE_MODE_RGMII_RXID ||
600f5b8631cSVladimir Oltean 		    ports->phy_mode == PHY_INTERFACE_MODE_RGMII_ID)
601f5b8631cSVladimir Oltean 			priv->rgmii_rx_delay[i] = true;
602f5b8631cSVladimir Oltean 
603f5b8631cSVladimir Oltean 		if (ports->phy_mode == PHY_INTERFACE_MODE_RGMII_TXID ||
604f5b8631cSVladimir Oltean 		    ports->phy_mode == PHY_INTERFACE_MODE_RGMII_ID)
605f5b8631cSVladimir Oltean 			priv->rgmii_tx_delay[i] = true;
606f5b8631cSVladimir Oltean 
607f5b8631cSVladimir Oltean 		if ((priv->rgmii_rx_delay[i] || priv->rgmii_tx_delay[i]) &&
608f5b8631cSVladimir Oltean 		     !priv->info->setup_rgmii_delay)
609f5b8631cSVladimir Oltean 			return -EINVAL;
610f5b8631cSVladimir Oltean 	}
611f5b8631cSVladimir Oltean 	return 0;
612f5b8631cSVladimir Oltean }
613f5b8631cSVladimir Oltean 
6148aa9ebccSVladimir Oltean static int sja1105_parse_ports_node(struct sja1105_private *priv,
6158aa9ebccSVladimir Oltean 				    struct sja1105_dt_port *ports,
6168aa9ebccSVladimir Oltean 				    struct device_node *ports_node)
6178aa9ebccSVladimir Oltean {
6188aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
6198aa9ebccSVladimir Oltean 	struct device_node *child;
6208aa9ebccSVladimir Oltean 
6218aa9ebccSVladimir Oltean 	for_each_child_of_node(ports_node, child) {
6228aa9ebccSVladimir Oltean 		struct device_node *phy_node;
6238aa9ebccSVladimir Oltean 		int phy_mode;
6248aa9ebccSVladimir Oltean 		u32 index;
6258aa9ebccSVladimir Oltean 
6268aa9ebccSVladimir Oltean 		/* Get switch port number from DT */
6278aa9ebccSVladimir Oltean 		if (of_property_read_u32(child, "reg", &index) < 0) {
6288aa9ebccSVladimir Oltean 			dev_err(dev, "Port number not defined in device tree "
6298aa9ebccSVladimir Oltean 				"(property \"reg\")\n");
6307ba771e3SNishka Dasgupta 			of_node_put(child);
6318aa9ebccSVladimir Oltean 			return -ENODEV;
6328aa9ebccSVladimir Oltean 		}
6338aa9ebccSVladimir Oltean 
6348aa9ebccSVladimir Oltean 		/* Get PHY mode from DT */
6358aa9ebccSVladimir Oltean 		phy_mode = of_get_phy_mode(child);
6368aa9ebccSVladimir Oltean 		if (phy_mode < 0) {
6378aa9ebccSVladimir Oltean 			dev_err(dev, "Failed to read phy-mode or "
6388aa9ebccSVladimir Oltean 				"phy-interface-type property for port %d\n",
6398aa9ebccSVladimir Oltean 				index);
6407ba771e3SNishka Dasgupta 			of_node_put(child);
6418aa9ebccSVladimir Oltean 			return -ENODEV;
6428aa9ebccSVladimir Oltean 		}
6438aa9ebccSVladimir Oltean 		ports[index].phy_mode = phy_mode;
6448aa9ebccSVladimir Oltean 
6458aa9ebccSVladimir Oltean 		phy_node = of_parse_phandle(child, "phy-handle", 0);
6468aa9ebccSVladimir Oltean 		if (!phy_node) {
6478aa9ebccSVladimir Oltean 			if (!of_phy_is_fixed_link(child)) {
6488aa9ebccSVladimir Oltean 				dev_err(dev, "phy-handle or fixed-link "
6498aa9ebccSVladimir Oltean 					"properties missing!\n");
6507ba771e3SNishka Dasgupta 				of_node_put(child);
6518aa9ebccSVladimir Oltean 				return -ENODEV;
6528aa9ebccSVladimir Oltean 			}
6538aa9ebccSVladimir Oltean 			/* phy-handle is missing, but fixed-link isn't.
6548aa9ebccSVladimir Oltean 			 * So it's a fixed link. Default to PHY role.
6558aa9ebccSVladimir Oltean 			 */
6568aa9ebccSVladimir Oltean 			ports[index].role = XMII_PHY;
6578aa9ebccSVladimir Oltean 		} else {
6588aa9ebccSVladimir Oltean 			/* phy-handle present => put port in MAC role */
6598aa9ebccSVladimir Oltean 			ports[index].role = XMII_MAC;
6608aa9ebccSVladimir Oltean 			of_node_put(phy_node);
6618aa9ebccSVladimir Oltean 		}
6628aa9ebccSVladimir Oltean 
6638aa9ebccSVladimir Oltean 		/* The MAC/PHY role can be overridden with explicit bindings */
6648aa9ebccSVladimir Oltean 		if (of_property_read_bool(child, "sja1105,role-mac"))
6658aa9ebccSVladimir Oltean 			ports[index].role = XMII_MAC;
6668aa9ebccSVladimir Oltean 		else if (of_property_read_bool(child, "sja1105,role-phy"))
6678aa9ebccSVladimir Oltean 			ports[index].role = XMII_PHY;
6688aa9ebccSVladimir Oltean 	}
6698aa9ebccSVladimir Oltean 
6708aa9ebccSVladimir Oltean 	return 0;
6718aa9ebccSVladimir Oltean }
6728aa9ebccSVladimir Oltean 
6738aa9ebccSVladimir Oltean static int sja1105_parse_dt(struct sja1105_private *priv,
6748aa9ebccSVladimir Oltean 			    struct sja1105_dt_port *ports)
6758aa9ebccSVladimir Oltean {
6768aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
6778aa9ebccSVladimir Oltean 	struct device_node *switch_node = dev->of_node;
6788aa9ebccSVladimir Oltean 	struct device_node *ports_node;
6798aa9ebccSVladimir Oltean 	int rc;
6808aa9ebccSVladimir Oltean 
6818aa9ebccSVladimir Oltean 	ports_node = of_get_child_by_name(switch_node, "ports");
6828aa9ebccSVladimir Oltean 	if (!ports_node) {
6838aa9ebccSVladimir Oltean 		dev_err(dev, "Incorrect bindings: absent \"ports\" node\n");
6848aa9ebccSVladimir Oltean 		return -ENODEV;
6858aa9ebccSVladimir Oltean 	}
6868aa9ebccSVladimir Oltean 
6878aa9ebccSVladimir Oltean 	rc = sja1105_parse_ports_node(priv, ports, ports_node);
6888aa9ebccSVladimir Oltean 	of_node_put(ports_node);
6898aa9ebccSVladimir Oltean 
6908aa9ebccSVladimir Oltean 	return rc;
6918aa9ebccSVladimir Oltean }
6928aa9ebccSVladimir Oltean 
693c44d0535SVladimir Oltean /* Convert link speed from SJA1105 to ethtool encoding */
6948aa9ebccSVladimir Oltean static int sja1105_speed[] = {
695c44d0535SVladimir Oltean 	[SJA1105_SPEED_AUTO]		= SPEED_UNKNOWN,
696c44d0535SVladimir Oltean 	[SJA1105_SPEED_10MBPS]		= SPEED_10,
697c44d0535SVladimir Oltean 	[SJA1105_SPEED_100MBPS]		= SPEED_100,
698c44d0535SVladimir Oltean 	[SJA1105_SPEED_1000MBPS]	= SPEED_1000,
6998aa9ebccSVladimir Oltean };
7008aa9ebccSVladimir Oltean 
7018400cff6SVladimir Oltean /* Set link speed in the MAC configuration for a specific port. */
7028aa9ebccSVladimir Oltean static int sja1105_adjust_port_config(struct sja1105_private *priv, int port,
7038400cff6SVladimir Oltean 				      int speed_mbps)
7048aa9ebccSVladimir Oltean {
7058aa9ebccSVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
7068aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
7078aa9ebccSVladimir Oltean 	struct device *dev = priv->ds->dev;
7088aa9ebccSVladimir Oltean 	sja1105_phy_interface_t phy_mode;
7098aa9ebccSVladimir Oltean 	sja1105_speed_t speed;
7108aa9ebccSVladimir Oltean 	int rc;
7118aa9ebccSVladimir Oltean 
7128400cff6SVladimir Oltean 	/* On P/Q/R/S, one can read from the device via the MAC reconfiguration
7138400cff6SVladimir Oltean 	 * tables. On E/T, MAC reconfig tables are not readable, only writable.
7148400cff6SVladimir Oltean 	 * We have to *know* what the MAC looks like.  For the sake of keeping
7158400cff6SVladimir Oltean 	 * the code common, we'll use the static configuration tables as a
7168400cff6SVladimir Oltean 	 * reasonable approximation for both E/T and P/Q/R/S.
7178400cff6SVladimir Oltean 	 */
7188aa9ebccSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
7198400cff6SVladimir Oltean 	mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
7208aa9ebccSVladimir Oltean 
721f4cfcfbdSVladimir Oltean 	switch (speed_mbps) {
722c44d0535SVladimir Oltean 	case SPEED_UNKNOWN:
723a979a0abSVladimir Oltean 		/* PHYLINK called sja1105_mac_config() to inform us about
724a979a0abSVladimir Oltean 		 * the state->interface, but AN has not completed and the
725a979a0abSVladimir Oltean 		 * speed is not yet valid. UM10944.pdf says that setting
726a979a0abSVladimir Oltean 		 * SJA1105_SPEED_AUTO at runtime disables the port, so that is
727a979a0abSVladimir Oltean 		 * ok for power consumption in case AN will never complete -
728a979a0abSVladimir Oltean 		 * otherwise PHYLINK should come back with a new update.
729a979a0abSVladimir Oltean 		 */
730f4cfcfbdSVladimir Oltean 		speed = SJA1105_SPEED_AUTO;
731f4cfcfbdSVladimir Oltean 		break;
732c44d0535SVladimir Oltean 	case SPEED_10:
733f4cfcfbdSVladimir Oltean 		speed = SJA1105_SPEED_10MBPS;
734f4cfcfbdSVladimir Oltean 		break;
735c44d0535SVladimir Oltean 	case SPEED_100:
736f4cfcfbdSVladimir Oltean 		speed = SJA1105_SPEED_100MBPS;
737f4cfcfbdSVladimir Oltean 		break;
738c44d0535SVladimir Oltean 	case SPEED_1000:
739f4cfcfbdSVladimir Oltean 		speed = SJA1105_SPEED_1000MBPS;
740f4cfcfbdSVladimir Oltean 		break;
741f4cfcfbdSVladimir Oltean 	default:
7428aa9ebccSVladimir Oltean 		dev_err(dev, "Invalid speed %iMbps\n", speed_mbps);
7438aa9ebccSVladimir Oltean 		return -EINVAL;
7448aa9ebccSVladimir Oltean 	}
7458aa9ebccSVladimir Oltean 
7468400cff6SVladimir Oltean 	/* Overwrite SJA1105_SPEED_AUTO from the static MAC configuration
7478400cff6SVladimir Oltean 	 * table, since this will be used for the clocking setup, and we no
7488400cff6SVladimir Oltean 	 * longer need to store it in the static config (already told hardware
7498400cff6SVladimir Oltean 	 * we want auto during upload phase).
7508aa9ebccSVladimir Oltean 	 */
7518aa9ebccSVladimir Oltean 	mac[port].speed = speed;
7528aa9ebccSVladimir Oltean 
7538aa9ebccSVladimir Oltean 	/* Write to the dynamic reconfiguration tables */
7548400cff6SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
7558400cff6SVladimir Oltean 					  &mac[port], true);
7568aa9ebccSVladimir Oltean 	if (rc < 0) {
7578aa9ebccSVladimir Oltean 		dev_err(dev, "Failed to write MAC config: %d\n", rc);
7588aa9ebccSVladimir Oltean 		return rc;
7598aa9ebccSVladimir Oltean 	}
7608aa9ebccSVladimir Oltean 
7618aa9ebccSVladimir Oltean 	/* Reconfigure the PLLs for the RGMII interfaces (required 125 MHz at
7628aa9ebccSVladimir Oltean 	 * gigabit, 25 MHz at 100 Mbps and 2.5 MHz at 10 Mbps). For MII and
7638aa9ebccSVladimir Oltean 	 * RMII no change of the clock setup is required. Actually, changing
7648aa9ebccSVladimir Oltean 	 * the clock setup does interrupt the clock signal for a certain time
7658aa9ebccSVladimir Oltean 	 * which causes trouble for all PHYs relying on this signal.
7668aa9ebccSVladimir Oltean 	 */
7678aa9ebccSVladimir Oltean 	phy_mode = mii->xmii_mode[port];
7688aa9ebccSVladimir Oltean 	if (phy_mode != XMII_MODE_RGMII)
7698aa9ebccSVladimir Oltean 		return 0;
7708aa9ebccSVladimir Oltean 
7718aa9ebccSVladimir Oltean 	return sja1105_clocking_setup_port(priv, port);
7728aa9ebccSVladimir Oltean }
7738aa9ebccSVladimir Oltean 
77439710229SVladimir Oltean /* The SJA1105 MAC programming model is through the static config (the xMII
77539710229SVladimir Oltean  * Mode table cannot be dynamically reconfigured), and we have to program
77639710229SVladimir Oltean  * that early (earlier than PHYLINK calls us, anyway).
77739710229SVladimir Oltean  * So just error out in case the connected PHY attempts to change the initial
77839710229SVladimir Oltean  * system interface MII protocol from what is defined in the DT, at least for
77939710229SVladimir Oltean  * now.
78039710229SVladimir Oltean  */
78139710229SVladimir Oltean static bool sja1105_phy_mode_mismatch(struct sja1105_private *priv, int port,
78239710229SVladimir Oltean 				      phy_interface_t interface)
78339710229SVladimir Oltean {
78439710229SVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
78539710229SVladimir Oltean 	sja1105_phy_interface_t phy_mode;
78639710229SVladimir Oltean 
78739710229SVladimir Oltean 	mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
78839710229SVladimir Oltean 	phy_mode = mii->xmii_mode[port];
78939710229SVladimir Oltean 
79039710229SVladimir Oltean 	switch (interface) {
79139710229SVladimir Oltean 	case PHY_INTERFACE_MODE_MII:
79239710229SVladimir Oltean 		return (phy_mode != XMII_MODE_MII);
79339710229SVladimir Oltean 	case PHY_INTERFACE_MODE_RMII:
79439710229SVladimir Oltean 		return (phy_mode != XMII_MODE_RMII);
79539710229SVladimir Oltean 	case PHY_INTERFACE_MODE_RGMII:
79639710229SVladimir Oltean 	case PHY_INTERFACE_MODE_RGMII_ID:
79739710229SVladimir Oltean 	case PHY_INTERFACE_MODE_RGMII_RXID:
79839710229SVladimir Oltean 	case PHY_INTERFACE_MODE_RGMII_TXID:
79939710229SVladimir Oltean 		return (phy_mode != XMII_MODE_RGMII);
80039710229SVladimir Oltean 	default:
80139710229SVladimir Oltean 		return true;
80239710229SVladimir Oltean 	}
80339710229SVladimir Oltean }
80439710229SVladimir Oltean 
805af7cd036SVladimir Oltean static void sja1105_mac_config(struct dsa_switch *ds, int port,
806af7cd036SVladimir Oltean 			       unsigned int link_an_mode,
807af7cd036SVladimir Oltean 			       const struct phylink_link_state *state)
8088aa9ebccSVladimir Oltean {
8098aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
8108aa9ebccSVladimir Oltean 
81139710229SVladimir Oltean 	if (sja1105_phy_mode_mismatch(priv, port, state->interface))
81239710229SVladimir Oltean 		return;
81339710229SVladimir Oltean 
8149f971573SVladimir Oltean 	if (link_an_mode == MLO_AN_INBAND) {
8159f971573SVladimir Oltean 		dev_err(ds->dev, "In-band AN not supported!\n");
8169f971573SVladimir Oltean 		return;
8179f971573SVladimir Oltean 	}
8189f971573SVladimir Oltean 
8198400cff6SVladimir Oltean 	sja1105_adjust_port_config(priv, port, state->speed);
8208400cff6SVladimir Oltean }
8218400cff6SVladimir Oltean 
8228400cff6SVladimir Oltean static void sja1105_mac_link_down(struct dsa_switch *ds, int port,
8238400cff6SVladimir Oltean 				  unsigned int mode,
8248400cff6SVladimir Oltean 				  phy_interface_t interface)
8258400cff6SVladimir Oltean {
8268400cff6SVladimir Oltean 	sja1105_inhibit_tx(ds->priv, BIT(port), true);
8278400cff6SVladimir Oltean }
8288400cff6SVladimir Oltean 
8298400cff6SVladimir Oltean static void sja1105_mac_link_up(struct dsa_switch *ds, int port,
8308400cff6SVladimir Oltean 				unsigned int mode,
8318400cff6SVladimir Oltean 				phy_interface_t interface,
8328400cff6SVladimir Oltean 				struct phy_device *phydev)
8338400cff6SVladimir Oltean {
8348400cff6SVladimir Oltean 	sja1105_inhibit_tx(ds->priv, BIT(port), false);
8358aa9ebccSVladimir Oltean }
8368aa9ebccSVladimir Oltean 
837ad9f299aSVladimir Oltean static void sja1105_phylink_validate(struct dsa_switch *ds, int port,
838ad9f299aSVladimir Oltean 				     unsigned long *supported,
839ad9f299aSVladimir Oltean 				     struct phylink_link_state *state)
840ad9f299aSVladimir Oltean {
841ad9f299aSVladimir Oltean 	/* Construct a new mask which exhaustively contains all link features
842ad9f299aSVladimir Oltean 	 * supported by the MAC, and then apply that (logical AND) to what will
843ad9f299aSVladimir Oltean 	 * be sent to the PHY for "marketing".
844ad9f299aSVladimir Oltean 	 */
845ad9f299aSVladimir Oltean 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
846ad9f299aSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
847ad9f299aSVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
848ad9f299aSVladimir Oltean 
849ad9f299aSVladimir Oltean 	mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
850ad9f299aSVladimir Oltean 
85139710229SVladimir Oltean 	/* include/linux/phylink.h says:
85239710229SVladimir Oltean 	 *     When @state->interface is %PHY_INTERFACE_MODE_NA, phylink
85339710229SVladimir Oltean 	 *     expects the MAC driver to return all supported link modes.
85439710229SVladimir Oltean 	 */
85539710229SVladimir Oltean 	if (state->interface != PHY_INTERFACE_MODE_NA &&
85639710229SVladimir Oltean 	    sja1105_phy_mode_mismatch(priv, port, state->interface)) {
85739710229SVladimir Oltean 		bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
85839710229SVladimir Oltean 		return;
85939710229SVladimir Oltean 	}
86039710229SVladimir Oltean 
861ad9f299aSVladimir Oltean 	/* The MAC does not support pause frames, and also doesn't
862ad9f299aSVladimir Oltean 	 * support half-duplex traffic modes.
863ad9f299aSVladimir Oltean 	 */
864ad9f299aSVladimir Oltean 	phylink_set(mask, Autoneg);
865ad9f299aSVladimir Oltean 	phylink_set(mask, MII);
866ad9f299aSVladimir Oltean 	phylink_set(mask, 10baseT_Full);
867ad9f299aSVladimir Oltean 	phylink_set(mask, 100baseT_Full);
868ad9f299aSVladimir Oltean 	if (mii->xmii_mode[port] == XMII_MODE_RGMII)
869ad9f299aSVladimir Oltean 		phylink_set(mask, 1000baseT_Full);
870ad9f299aSVladimir Oltean 
871ad9f299aSVladimir Oltean 	bitmap_and(supported, supported, mask, __ETHTOOL_LINK_MODE_MASK_NBITS);
872ad9f299aSVladimir Oltean 	bitmap_and(state->advertising, state->advertising, mask,
873ad9f299aSVladimir Oltean 		   __ETHTOOL_LINK_MODE_MASK_NBITS);
874ad9f299aSVladimir Oltean }
875ad9f299aSVladimir Oltean 
87660f6053fSVladimir Oltean static int
87760f6053fSVladimir Oltean sja1105_find_static_fdb_entry(struct sja1105_private *priv, int port,
87860f6053fSVladimir Oltean 			      const struct sja1105_l2_lookup_entry *requested)
87960f6053fSVladimir Oltean {
88060f6053fSVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
88160f6053fSVladimir Oltean 	struct sja1105_table *table;
88260f6053fSVladimir Oltean 	int i;
88360f6053fSVladimir Oltean 
88460f6053fSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
88560f6053fSVladimir Oltean 	l2_lookup = table->entries;
88660f6053fSVladimir Oltean 
88760f6053fSVladimir Oltean 	for (i = 0; i < table->entry_count; i++)
88860f6053fSVladimir Oltean 		if (l2_lookup[i].macaddr == requested->macaddr &&
88960f6053fSVladimir Oltean 		    l2_lookup[i].vlanid == requested->vlanid &&
89060f6053fSVladimir Oltean 		    l2_lookup[i].destports & BIT(port))
89160f6053fSVladimir Oltean 			return i;
89260f6053fSVladimir Oltean 
89360f6053fSVladimir Oltean 	return -1;
89460f6053fSVladimir Oltean }
89560f6053fSVladimir Oltean 
89660f6053fSVladimir Oltean /* We want FDB entries added statically through the bridge command to persist
89760f6053fSVladimir Oltean  * across switch resets, which are a common thing during normal SJA1105
89860f6053fSVladimir Oltean  * operation. So we have to back them up in the static configuration tables
89960f6053fSVladimir Oltean  * and hence apply them on next static config upload... yay!
90060f6053fSVladimir Oltean  */
90160f6053fSVladimir Oltean static int
90260f6053fSVladimir Oltean sja1105_static_fdb_change(struct sja1105_private *priv, int port,
90360f6053fSVladimir Oltean 			  const struct sja1105_l2_lookup_entry *requested,
90460f6053fSVladimir Oltean 			  bool keep)
90560f6053fSVladimir Oltean {
90660f6053fSVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
90760f6053fSVladimir Oltean 	struct sja1105_table *table;
90860f6053fSVladimir Oltean 	int rc, match;
90960f6053fSVladimir Oltean 
91060f6053fSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
91160f6053fSVladimir Oltean 
91260f6053fSVladimir Oltean 	match = sja1105_find_static_fdb_entry(priv, port, requested);
91360f6053fSVladimir Oltean 	if (match < 0) {
91460f6053fSVladimir Oltean 		/* Can't delete a missing entry. */
91560f6053fSVladimir Oltean 		if (!keep)
91660f6053fSVladimir Oltean 			return 0;
91760f6053fSVladimir Oltean 
91860f6053fSVladimir Oltean 		/* No match => new entry */
91960f6053fSVladimir Oltean 		rc = sja1105_table_resize(table, table->entry_count + 1);
92060f6053fSVladimir Oltean 		if (rc)
92160f6053fSVladimir Oltean 			return rc;
92260f6053fSVladimir Oltean 
92360f6053fSVladimir Oltean 		match = table->entry_count - 1;
92460f6053fSVladimir Oltean 	}
92560f6053fSVladimir Oltean 
92660f6053fSVladimir Oltean 	/* Assign pointer after the resize (it may be new memory) */
92760f6053fSVladimir Oltean 	l2_lookup = table->entries;
92860f6053fSVladimir Oltean 
92960f6053fSVladimir Oltean 	/* We have a match.
93060f6053fSVladimir Oltean 	 * If the job was to add this FDB entry, it's already done (mostly
93160f6053fSVladimir Oltean 	 * anyway, since the port forwarding mask may have changed, case in
93260f6053fSVladimir Oltean 	 * which we update it).
93360f6053fSVladimir Oltean 	 * Otherwise we have to delete it.
93460f6053fSVladimir Oltean 	 */
93560f6053fSVladimir Oltean 	if (keep) {
93660f6053fSVladimir Oltean 		l2_lookup[match] = *requested;
93760f6053fSVladimir Oltean 		return 0;
93860f6053fSVladimir Oltean 	}
93960f6053fSVladimir Oltean 
94060f6053fSVladimir Oltean 	/* To remove, the strategy is to overwrite the element with
94160f6053fSVladimir Oltean 	 * the last one, and then reduce the array size by 1
94260f6053fSVladimir Oltean 	 */
94360f6053fSVladimir Oltean 	l2_lookup[match] = l2_lookup[table->entry_count - 1];
94460f6053fSVladimir Oltean 	return sja1105_table_resize(table, table->entry_count - 1);
94560f6053fSVladimir Oltean }
94660f6053fSVladimir Oltean 
947291d1e72SVladimir Oltean /* First-generation switches have a 4-way set associative TCAM that
948291d1e72SVladimir Oltean  * holds the FDB entries. An FDB index spans from 0 to 1023 and is comprised of
949291d1e72SVladimir Oltean  * a "bin" (grouping of 4 entries) and a "way" (an entry within a bin).
950291d1e72SVladimir Oltean  * For the placement of a newly learnt FDB entry, the switch selects the bin
951291d1e72SVladimir Oltean  * based on a hash function, and the way within that bin incrementally.
952291d1e72SVladimir Oltean  */
95309c1b412SVladimir Oltean static int sja1105et_fdb_index(int bin, int way)
954291d1e72SVladimir Oltean {
955291d1e72SVladimir Oltean 	return bin * SJA1105ET_FDB_BIN_SIZE + way;
956291d1e72SVladimir Oltean }
957291d1e72SVladimir Oltean 
9589dfa6911SVladimir Oltean static int sja1105et_is_fdb_entry_in_bin(struct sja1105_private *priv, int bin,
959291d1e72SVladimir Oltean 					 const u8 *addr, u16 vid,
960291d1e72SVladimir Oltean 					 struct sja1105_l2_lookup_entry *match,
961291d1e72SVladimir Oltean 					 int *last_unused)
962291d1e72SVladimir Oltean {
963291d1e72SVladimir Oltean 	int way;
964291d1e72SVladimir Oltean 
965291d1e72SVladimir Oltean 	for (way = 0; way < SJA1105ET_FDB_BIN_SIZE; way++) {
966291d1e72SVladimir Oltean 		struct sja1105_l2_lookup_entry l2_lookup = {0};
967291d1e72SVladimir Oltean 		int index = sja1105et_fdb_index(bin, way);
968291d1e72SVladimir Oltean 
969291d1e72SVladimir Oltean 		/* Skip unused entries, optionally marking them
970291d1e72SVladimir Oltean 		 * into the return value
971291d1e72SVladimir Oltean 		 */
972291d1e72SVladimir Oltean 		if (sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
973291d1e72SVladimir Oltean 						index, &l2_lookup)) {
974291d1e72SVladimir Oltean 			if (last_unused)
975291d1e72SVladimir Oltean 				*last_unused = way;
976291d1e72SVladimir Oltean 			continue;
977291d1e72SVladimir Oltean 		}
978291d1e72SVladimir Oltean 
979291d1e72SVladimir Oltean 		if (l2_lookup.macaddr == ether_addr_to_u64(addr) &&
980291d1e72SVladimir Oltean 		    l2_lookup.vlanid == vid) {
981291d1e72SVladimir Oltean 			if (match)
982291d1e72SVladimir Oltean 				*match = l2_lookup;
983291d1e72SVladimir Oltean 			return way;
984291d1e72SVladimir Oltean 		}
985291d1e72SVladimir Oltean 	}
986291d1e72SVladimir Oltean 	/* Return an invalid entry index if not found */
987291d1e72SVladimir Oltean 	return -1;
988291d1e72SVladimir Oltean }
989291d1e72SVladimir Oltean 
9909dfa6911SVladimir Oltean int sja1105et_fdb_add(struct dsa_switch *ds, int port,
991291d1e72SVladimir Oltean 		      const unsigned char *addr, u16 vid)
992291d1e72SVladimir Oltean {
993291d1e72SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
994291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
995291d1e72SVladimir Oltean 	struct device *dev = ds->dev;
996291d1e72SVladimir Oltean 	int last_unused = -1;
99760f6053fSVladimir Oltean 	int bin, way, rc;
998291d1e72SVladimir Oltean 
9999dfa6911SVladimir Oltean 	bin = sja1105et_fdb_hash(priv, addr, vid);
1000291d1e72SVladimir Oltean 
10019dfa6911SVladimir Oltean 	way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid,
1002291d1e72SVladimir Oltean 					    &l2_lookup, &last_unused);
1003291d1e72SVladimir Oltean 	if (way >= 0) {
1004291d1e72SVladimir Oltean 		/* We have an FDB entry. Is our port in the destination
1005291d1e72SVladimir Oltean 		 * mask? If yes, we need to do nothing. If not, we need
1006291d1e72SVladimir Oltean 		 * to rewrite the entry by adding this port to it.
1007291d1e72SVladimir Oltean 		 */
1008291d1e72SVladimir Oltean 		if (l2_lookup.destports & BIT(port))
1009291d1e72SVladimir Oltean 			return 0;
1010291d1e72SVladimir Oltean 		l2_lookup.destports |= BIT(port);
1011291d1e72SVladimir Oltean 	} else {
1012291d1e72SVladimir Oltean 		int index = sja1105et_fdb_index(bin, way);
1013291d1e72SVladimir Oltean 
1014291d1e72SVladimir Oltean 		/* We don't have an FDB entry. We construct a new one and
1015291d1e72SVladimir Oltean 		 * try to find a place for it within the FDB table.
1016291d1e72SVladimir Oltean 		 */
1017291d1e72SVladimir Oltean 		l2_lookup.macaddr = ether_addr_to_u64(addr);
1018291d1e72SVladimir Oltean 		l2_lookup.destports = BIT(port);
1019291d1e72SVladimir Oltean 		l2_lookup.vlanid = vid;
1020291d1e72SVladimir Oltean 
1021291d1e72SVladimir Oltean 		if (last_unused >= 0) {
1022291d1e72SVladimir Oltean 			way = last_unused;
1023291d1e72SVladimir Oltean 		} else {
1024291d1e72SVladimir Oltean 			/* Bin is full, need to evict somebody.
1025291d1e72SVladimir Oltean 			 * Choose victim at random. If you get these messages
1026291d1e72SVladimir Oltean 			 * often, you may need to consider changing the
1027291d1e72SVladimir Oltean 			 * distribution function:
1028291d1e72SVladimir Oltean 			 * static_config[BLK_IDX_L2_LOOKUP_PARAMS].entries->poly
1029291d1e72SVladimir Oltean 			 */
1030291d1e72SVladimir Oltean 			get_random_bytes(&way, sizeof(u8));
1031291d1e72SVladimir Oltean 			way %= SJA1105ET_FDB_BIN_SIZE;
1032291d1e72SVladimir Oltean 			dev_warn(dev, "Warning, FDB bin %d full while adding entry for %pM. Evicting entry %u.\n",
1033291d1e72SVladimir Oltean 				 bin, addr, way);
1034291d1e72SVladimir Oltean 			/* Evict entry */
1035291d1e72SVladimir Oltean 			sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1036291d1e72SVladimir Oltean 						     index, NULL, false);
1037291d1e72SVladimir Oltean 		}
1038291d1e72SVladimir Oltean 	}
1039291d1e72SVladimir Oltean 	l2_lookup.index = sja1105et_fdb_index(bin, way);
1040291d1e72SVladimir Oltean 
104160f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1042291d1e72SVladimir Oltean 					  l2_lookup.index, &l2_lookup,
1043291d1e72SVladimir Oltean 					  true);
104460f6053fSVladimir Oltean 	if (rc < 0)
104560f6053fSVladimir Oltean 		return rc;
104660f6053fSVladimir Oltean 
104760f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, true);
1048291d1e72SVladimir Oltean }
1049291d1e72SVladimir Oltean 
10509dfa6911SVladimir Oltean int sja1105et_fdb_del(struct dsa_switch *ds, int port,
1051291d1e72SVladimir Oltean 		      const unsigned char *addr, u16 vid)
1052291d1e72SVladimir Oltean {
1053291d1e72SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
1054291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
105560f6053fSVladimir Oltean 	int index, bin, way, rc;
1056291d1e72SVladimir Oltean 	bool keep;
1057291d1e72SVladimir Oltean 
10589dfa6911SVladimir Oltean 	bin = sja1105et_fdb_hash(priv, addr, vid);
10599dfa6911SVladimir Oltean 	way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid,
1060291d1e72SVladimir Oltean 					    &l2_lookup, NULL);
1061291d1e72SVladimir Oltean 	if (way < 0)
1062291d1e72SVladimir Oltean 		return 0;
1063291d1e72SVladimir Oltean 	index = sja1105et_fdb_index(bin, way);
1064291d1e72SVladimir Oltean 
1065291d1e72SVladimir Oltean 	/* We have an FDB entry. Is our port in the destination mask? If yes,
1066291d1e72SVladimir Oltean 	 * we need to remove it. If the resulting port mask becomes empty, we
1067291d1e72SVladimir Oltean 	 * need to completely evict the FDB entry.
1068291d1e72SVladimir Oltean 	 * Otherwise we just write it back.
1069291d1e72SVladimir Oltean 	 */
1070291d1e72SVladimir Oltean 	l2_lookup.destports &= ~BIT(port);
10717752e937SVladimir Oltean 
1072291d1e72SVladimir Oltean 	if (l2_lookup.destports)
1073291d1e72SVladimir Oltean 		keep = true;
1074291d1e72SVladimir Oltean 	else
1075291d1e72SVladimir Oltean 		keep = false;
1076291d1e72SVladimir Oltean 
107760f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1078291d1e72SVladimir Oltean 					  index, &l2_lookup, keep);
107960f6053fSVladimir Oltean 	if (rc < 0)
108060f6053fSVladimir Oltean 		return rc;
108160f6053fSVladimir Oltean 
108260f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, keep);
1083291d1e72SVladimir Oltean }
1084291d1e72SVladimir Oltean 
10859dfa6911SVladimir Oltean int sja1105pqrs_fdb_add(struct dsa_switch *ds, int port,
10869dfa6911SVladimir Oltean 			const unsigned char *addr, u16 vid)
10879dfa6911SVladimir Oltean {
10881da73821SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
10891da73821SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
10901da73821SVladimir Oltean 	int rc, i;
10911da73821SVladimir Oltean 
10921da73821SVladimir Oltean 	/* Search for an existing entry in the FDB table */
10931da73821SVladimir Oltean 	l2_lookup.macaddr = ether_addr_to_u64(addr);
10941da73821SVladimir Oltean 	l2_lookup.vlanid = vid;
10951da73821SVladimir Oltean 	l2_lookup.iotag = SJA1105_S_TAG;
10961da73821SVladimir Oltean 	l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0);
10976d7c7d94SVladimir Oltean 	if (dsa_port_is_vlan_filtering(&ds->ports[port])) {
10981da73821SVladimir Oltean 		l2_lookup.mask_vlanid = VLAN_VID_MASK;
10991da73821SVladimir Oltean 		l2_lookup.mask_iotag = BIT(0);
11006d7c7d94SVladimir Oltean 	} else {
11016d7c7d94SVladimir Oltean 		l2_lookup.mask_vlanid = 0;
11026d7c7d94SVladimir Oltean 		l2_lookup.mask_iotag = 0;
11036d7c7d94SVladimir Oltean 	}
11041da73821SVladimir Oltean 	l2_lookup.destports = BIT(port);
11051da73821SVladimir Oltean 
11061da73821SVladimir Oltean 	rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
11071da73821SVladimir Oltean 					 SJA1105_SEARCH, &l2_lookup);
11081da73821SVladimir Oltean 	if (rc == 0) {
11091da73821SVladimir Oltean 		/* Found and this port is already in the entry's
11101da73821SVladimir Oltean 		 * port mask => job done
11111da73821SVladimir Oltean 		 */
11121da73821SVladimir Oltean 		if (l2_lookup.destports & BIT(port))
11131da73821SVladimir Oltean 			return 0;
11141da73821SVladimir Oltean 		/* l2_lookup.index is populated by the switch in case it
11151da73821SVladimir Oltean 		 * found something.
11161da73821SVladimir Oltean 		 */
11171da73821SVladimir Oltean 		l2_lookup.destports |= BIT(port);
11181da73821SVladimir Oltean 		goto skip_finding_an_index;
11191da73821SVladimir Oltean 	}
11201da73821SVladimir Oltean 
11211da73821SVladimir Oltean 	/* Not found, so try to find an unused spot in the FDB.
11221da73821SVladimir Oltean 	 * This is slightly inefficient because the strategy is knock-knock at
11231da73821SVladimir Oltean 	 * every possible position from 0 to 1023.
11241da73821SVladimir Oltean 	 */
11251da73821SVladimir Oltean 	for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
11261da73821SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
11271da73821SVladimir Oltean 						 i, NULL);
11281da73821SVladimir Oltean 		if (rc < 0)
11291da73821SVladimir Oltean 			break;
11301da73821SVladimir Oltean 	}
11311da73821SVladimir Oltean 	if (i == SJA1105_MAX_L2_LOOKUP_COUNT) {
11321da73821SVladimir Oltean 		dev_err(ds->dev, "FDB is full, cannot add entry.\n");
11331da73821SVladimir Oltean 		return -EINVAL;
11341da73821SVladimir Oltean 	}
113517ae6555SVladimir Oltean 	l2_lookup.lockeds = true;
11361da73821SVladimir Oltean 	l2_lookup.index = i;
11371da73821SVladimir Oltean 
11381da73821SVladimir Oltean skip_finding_an_index:
113960f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
11401da73821SVladimir Oltean 					  l2_lookup.index, &l2_lookup,
11411da73821SVladimir Oltean 					  true);
114260f6053fSVladimir Oltean 	if (rc < 0)
114360f6053fSVladimir Oltean 		return rc;
114460f6053fSVladimir Oltean 
114560f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, true);
11469dfa6911SVladimir Oltean }
11479dfa6911SVladimir Oltean 
11489dfa6911SVladimir Oltean int sja1105pqrs_fdb_del(struct dsa_switch *ds, int port,
11499dfa6911SVladimir Oltean 			const unsigned char *addr, u16 vid)
11509dfa6911SVladimir Oltean {
11511da73821SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
11521da73821SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
11531da73821SVladimir Oltean 	bool keep;
11541da73821SVladimir Oltean 	int rc;
11551da73821SVladimir Oltean 
11561da73821SVladimir Oltean 	l2_lookup.macaddr = ether_addr_to_u64(addr);
11571da73821SVladimir Oltean 	l2_lookup.vlanid = vid;
11581da73821SVladimir Oltean 	l2_lookup.iotag = SJA1105_S_TAG;
11591da73821SVladimir Oltean 	l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0);
11606d7c7d94SVladimir Oltean 	if (dsa_port_is_vlan_filtering(&ds->ports[port])) {
11611da73821SVladimir Oltean 		l2_lookup.mask_vlanid = VLAN_VID_MASK;
11621da73821SVladimir Oltean 		l2_lookup.mask_iotag = BIT(0);
11636d7c7d94SVladimir Oltean 	} else {
11646d7c7d94SVladimir Oltean 		l2_lookup.mask_vlanid = 0;
11656d7c7d94SVladimir Oltean 		l2_lookup.mask_iotag = 0;
11666d7c7d94SVladimir Oltean 	}
11671da73821SVladimir Oltean 	l2_lookup.destports = BIT(port);
11681da73821SVladimir Oltean 
11691da73821SVladimir Oltean 	rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
11701da73821SVladimir Oltean 					 SJA1105_SEARCH, &l2_lookup);
11711da73821SVladimir Oltean 	if (rc < 0)
11721da73821SVladimir Oltean 		return 0;
11731da73821SVladimir Oltean 
11741da73821SVladimir Oltean 	l2_lookup.destports &= ~BIT(port);
11751da73821SVladimir Oltean 
11761da73821SVladimir Oltean 	/* Decide whether we remove just this port from the FDB entry,
11771da73821SVladimir Oltean 	 * or if we remove it completely.
11781da73821SVladimir Oltean 	 */
11791da73821SVladimir Oltean 	if (l2_lookup.destports)
11801da73821SVladimir Oltean 		keep = true;
11811da73821SVladimir Oltean 	else
11821da73821SVladimir Oltean 		keep = false;
11831da73821SVladimir Oltean 
118460f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
11851da73821SVladimir Oltean 					  l2_lookup.index, &l2_lookup, keep);
118660f6053fSVladimir Oltean 	if (rc < 0)
118760f6053fSVladimir Oltean 		return rc;
118860f6053fSVladimir Oltean 
118960f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, keep);
11909dfa6911SVladimir Oltean }
11919dfa6911SVladimir Oltean 
11929dfa6911SVladimir Oltean static int sja1105_fdb_add(struct dsa_switch *ds, int port,
11939dfa6911SVladimir Oltean 			   const unsigned char *addr, u16 vid)
11949dfa6911SVladimir Oltean {
11959dfa6911SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1196b3ee526aSVladimir Oltean 
11976d7c7d94SVladimir Oltean 	/* dsa_8021q is in effect when the bridge's vlan_filtering isn't,
11986d7c7d94SVladimir Oltean 	 * so the switch still does some VLAN processing internally.
11996d7c7d94SVladimir Oltean 	 * But Shared VLAN Learning (SVL) is also active, and it will take
12006d7c7d94SVladimir Oltean 	 * care of autonomous forwarding between the unique pvid's of each
12016d7c7d94SVladimir Oltean 	 * port.  Here we just make sure that users can't add duplicate FDB
12026d7c7d94SVladimir Oltean 	 * entries when in this mode - the actual VID doesn't matter except
12036d7c7d94SVladimir Oltean 	 * for what gets printed in 'bridge fdb show'.  In the case of zero,
12046d7c7d94SVladimir Oltean 	 * no VID gets printed at all.
120593647594SVladimir Oltean 	 */
12066d7c7d94SVladimir Oltean 	if (!dsa_port_is_vlan_filtering(&ds->ports[port]))
12076d7c7d94SVladimir Oltean 		vid = 0;
120893647594SVladimir Oltean 
12096d7c7d94SVladimir Oltean 	return priv->info->fdb_add_cmd(ds, port, addr, vid);
12109dfa6911SVladimir Oltean }
12119dfa6911SVladimir Oltean 
12129dfa6911SVladimir Oltean static int sja1105_fdb_del(struct dsa_switch *ds, int port,
12139dfa6911SVladimir Oltean 			   const unsigned char *addr, u16 vid)
12149dfa6911SVladimir Oltean {
12159dfa6911SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
12169dfa6911SVladimir Oltean 
12176d7c7d94SVladimir Oltean 	if (!dsa_port_is_vlan_filtering(&ds->ports[port]))
12186d7c7d94SVladimir Oltean 		vid = 0;
12196d7c7d94SVladimir Oltean 
1220b3ee526aSVladimir Oltean 	return priv->info->fdb_del_cmd(ds, port, addr, vid);
12219dfa6911SVladimir Oltean }
12229dfa6911SVladimir Oltean 
1223291d1e72SVladimir Oltean static int sja1105_fdb_dump(struct dsa_switch *ds, int port,
1224291d1e72SVladimir Oltean 			    dsa_fdb_dump_cb_t *cb, void *data)
1225291d1e72SVladimir Oltean {
1226291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1227291d1e72SVladimir Oltean 	struct device *dev = ds->dev;
1228291d1e72SVladimir Oltean 	int i;
1229291d1e72SVladimir Oltean 
1230291d1e72SVladimir Oltean 	for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
1231291d1e72SVladimir Oltean 		struct sja1105_l2_lookup_entry l2_lookup = {0};
1232291d1e72SVladimir Oltean 		u8 macaddr[ETH_ALEN];
1233291d1e72SVladimir Oltean 		int rc;
1234291d1e72SVladimir Oltean 
1235291d1e72SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1236291d1e72SVladimir Oltean 						 i, &l2_lookup);
1237291d1e72SVladimir Oltean 		/* No fdb entry at i, not an issue */
1238def84604SVladimir Oltean 		if (rc == -ENOENT)
1239291d1e72SVladimir Oltean 			continue;
1240291d1e72SVladimir Oltean 		if (rc) {
1241291d1e72SVladimir Oltean 			dev_err(dev, "Failed to dump FDB: %d\n", rc);
1242291d1e72SVladimir Oltean 			return rc;
1243291d1e72SVladimir Oltean 		}
1244291d1e72SVladimir Oltean 
1245291d1e72SVladimir Oltean 		/* FDB dump callback is per port. This means we have to
1246291d1e72SVladimir Oltean 		 * disregard a valid entry if it's not for this port, even if
1247291d1e72SVladimir Oltean 		 * only to revisit it later. This is inefficient because the
1248291d1e72SVladimir Oltean 		 * 1024-sized FDB table needs to be traversed 4 times through
1249291d1e72SVladimir Oltean 		 * SPI during a 'bridge fdb show' command.
1250291d1e72SVladimir Oltean 		 */
1251291d1e72SVladimir Oltean 		if (!(l2_lookup.destports & BIT(port)))
1252291d1e72SVladimir Oltean 			continue;
1253291d1e72SVladimir Oltean 		u64_to_ether_addr(l2_lookup.macaddr, macaddr);
125493647594SVladimir Oltean 
12556d7c7d94SVladimir Oltean 		/* We need to hide the dsa_8021q VLANs from the user. */
12566d7c7d94SVladimir Oltean 		if (!dsa_port_is_vlan_filtering(&ds->ports[port]))
12576d7c7d94SVladimir Oltean 			l2_lookup.vlanid = 0;
125817ae6555SVladimir Oltean 		cb(macaddr, l2_lookup.vlanid, l2_lookup.lockeds, data);
1259291d1e72SVladimir Oltean 	}
1260291d1e72SVladimir Oltean 	return 0;
1261291d1e72SVladimir Oltean }
1262291d1e72SVladimir Oltean 
1263291d1e72SVladimir Oltean /* This callback needs to be present */
1264291d1e72SVladimir Oltean static int sja1105_mdb_prepare(struct dsa_switch *ds, int port,
1265291d1e72SVladimir Oltean 			       const struct switchdev_obj_port_mdb *mdb)
1266291d1e72SVladimir Oltean {
1267291d1e72SVladimir Oltean 	return 0;
1268291d1e72SVladimir Oltean }
1269291d1e72SVladimir Oltean 
1270291d1e72SVladimir Oltean static void sja1105_mdb_add(struct dsa_switch *ds, int port,
1271291d1e72SVladimir Oltean 			    const struct switchdev_obj_port_mdb *mdb)
1272291d1e72SVladimir Oltean {
1273291d1e72SVladimir Oltean 	sja1105_fdb_add(ds, port, mdb->addr, mdb->vid);
1274291d1e72SVladimir Oltean }
1275291d1e72SVladimir Oltean 
1276291d1e72SVladimir Oltean static int sja1105_mdb_del(struct dsa_switch *ds, int port,
1277291d1e72SVladimir Oltean 			   const struct switchdev_obj_port_mdb *mdb)
1278291d1e72SVladimir Oltean {
1279291d1e72SVladimir Oltean 	return sja1105_fdb_del(ds, port, mdb->addr, mdb->vid);
1280291d1e72SVladimir Oltean }
1281291d1e72SVladimir Oltean 
12828aa9ebccSVladimir Oltean static int sja1105_bridge_member(struct dsa_switch *ds, int port,
12838aa9ebccSVladimir Oltean 				 struct net_device *br, bool member)
12848aa9ebccSVladimir Oltean {
12858aa9ebccSVladimir Oltean 	struct sja1105_l2_forwarding_entry *l2_fwd;
12868aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
12878aa9ebccSVladimir Oltean 	int i, rc;
12888aa9ebccSVladimir Oltean 
12898aa9ebccSVladimir Oltean 	l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries;
12908aa9ebccSVladimir Oltean 
12918aa9ebccSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
12928aa9ebccSVladimir Oltean 		/* Add this port to the forwarding matrix of the
12938aa9ebccSVladimir Oltean 		 * other ports in the same bridge, and viceversa.
12948aa9ebccSVladimir Oltean 		 */
12958aa9ebccSVladimir Oltean 		if (!dsa_is_user_port(ds, i))
12968aa9ebccSVladimir Oltean 			continue;
12978aa9ebccSVladimir Oltean 		/* For the ports already under the bridge, only one thing needs
12988aa9ebccSVladimir Oltean 		 * to be done, and that is to add this port to their
12998aa9ebccSVladimir Oltean 		 * reachability domain. So we can perform the SPI write for
13008aa9ebccSVladimir Oltean 		 * them immediately. However, for this port itself (the one
13018aa9ebccSVladimir Oltean 		 * that is new to the bridge), we need to add all other ports
13028aa9ebccSVladimir Oltean 		 * to its reachability domain. So we do that incrementally in
13038aa9ebccSVladimir Oltean 		 * this loop, and perform the SPI write only at the end, once
13048aa9ebccSVladimir Oltean 		 * the domain contains all other bridge ports.
13058aa9ebccSVladimir Oltean 		 */
13068aa9ebccSVladimir Oltean 		if (i == port)
13078aa9ebccSVladimir Oltean 			continue;
13088aa9ebccSVladimir Oltean 		if (dsa_to_port(ds, i)->bridge_dev != br)
13098aa9ebccSVladimir Oltean 			continue;
13108aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2_fwd, i, port, member);
13118aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2_fwd, port, i, member);
13128aa9ebccSVladimir Oltean 
13138aa9ebccSVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
13148aa9ebccSVladimir Oltean 						  i, &l2_fwd[i], true);
13158aa9ebccSVladimir Oltean 		if (rc < 0)
13168aa9ebccSVladimir Oltean 			return rc;
13178aa9ebccSVladimir Oltean 	}
13188aa9ebccSVladimir Oltean 
13198aa9ebccSVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
13208aa9ebccSVladimir Oltean 					    port, &l2_fwd[port], true);
13218aa9ebccSVladimir Oltean }
13228aa9ebccSVladimir Oltean 
1323640f763fSVladimir Oltean static void sja1105_bridge_stp_state_set(struct dsa_switch *ds, int port,
1324640f763fSVladimir Oltean 					 u8 state)
1325640f763fSVladimir Oltean {
1326640f763fSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1327640f763fSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
1328640f763fSVladimir Oltean 
1329640f763fSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
1330640f763fSVladimir Oltean 
1331640f763fSVladimir Oltean 	switch (state) {
1332640f763fSVladimir Oltean 	case BR_STATE_DISABLED:
1333640f763fSVladimir Oltean 	case BR_STATE_BLOCKING:
1334640f763fSVladimir Oltean 		/* From UM10944 description of DRPDTAG (why put this there?):
1335640f763fSVladimir Oltean 		 * "Management traffic flows to the port regardless of the state
1336640f763fSVladimir Oltean 		 * of the INGRESS flag". So BPDUs are still be allowed to pass.
1337640f763fSVladimir Oltean 		 * At the moment no difference between DISABLED and BLOCKING.
1338640f763fSVladimir Oltean 		 */
1339640f763fSVladimir Oltean 		mac[port].ingress   = false;
1340640f763fSVladimir Oltean 		mac[port].egress    = false;
1341640f763fSVladimir Oltean 		mac[port].dyn_learn = false;
1342640f763fSVladimir Oltean 		break;
1343640f763fSVladimir Oltean 	case BR_STATE_LISTENING:
1344640f763fSVladimir Oltean 		mac[port].ingress   = true;
1345640f763fSVladimir Oltean 		mac[port].egress    = false;
1346640f763fSVladimir Oltean 		mac[port].dyn_learn = false;
1347640f763fSVladimir Oltean 		break;
1348640f763fSVladimir Oltean 	case BR_STATE_LEARNING:
1349640f763fSVladimir Oltean 		mac[port].ingress   = true;
1350640f763fSVladimir Oltean 		mac[port].egress    = false;
1351640f763fSVladimir Oltean 		mac[port].dyn_learn = true;
1352640f763fSVladimir Oltean 		break;
1353640f763fSVladimir Oltean 	case BR_STATE_FORWARDING:
1354640f763fSVladimir Oltean 		mac[port].ingress   = true;
1355640f763fSVladimir Oltean 		mac[port].egress    = true;
1356640f763fSVladimir Oltean 		mac[port].dyn_learn = true;
1357640f763fSVladimir Oltean 		break;
1358640f763fSVladimir Oltean 	default:
1359640f763fSVladimir Oltean 		dev_err(ds->dev, "invalid STP state: %d\n", state);
1360640f763fSVladimir Oltean 		return;
1361640f763fSVladimir Oltean 	}
1362640f763fSVladimir Oltean 
1363640f763fSVladimir Oltean 	sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
1364640f763fSVladimir Oltean 				     &mac[port], true);
1365640f763fSVladimir Oltean }
1366640f763fSVladimir Oltean 
13678aa9ebccSVladimir Oltean static int sja1105_bridge_join(struct dsa_switch *ds, int port,
13688aa9ebccSVladimir Oltean 			       struct net_device *br)
13698aa9ebccSVladimir Oltean {
13708aa9ebccSVladimir Oltean 	return sja1105_bridge_member(ds, port, br, true);
13718aa9ebccSVladimir Oltean }
13728aa9ebccSVladimir Oltean 
13738aa9ebccSVladimir Oltean static void sja1105_bridge_leave(struct dsa_switch *ds, int port,
13748aa9ebccSVladimir Oltean 				 struct net_device *br)
13758aa9ebccSVladimir Oltean {
13768aa9ebccSVladimir Oltean 	sja1105_bridge_member(ds, port, br, false);
13778aa9ebccSVladimir Oltean }
13788aa9ebccSVladimir Oltean 
13796666cebcSVladimir Oltean /* For situations where we need to change a setting at runtime that is only
13806666cebcSVladimir Oltean  * available through the static configuration, resetting the switch in order
13816666cebcSVladimir Oltean  * to upload the new static config is unavoidable. Back up the settings we
13826666cebcSVladimir Oltean  * modify at runtime (currently only MAC) and restore them after uploading,
13836666cebcSVladimir Oltean  * such that this operation is relatively seamless.
13846666cebcSVladimir Oltean  */
1385317ab5b8SVladimir Oltean int sja1105_static_config_reload(struct sja1105_private *priv)
13866666cebcSVladimir Oltean {
13876666cebcSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
13886666cebcSVladimir Oltean 	int speed_mbps[SJA1105_NUM_PORTS];
13896666cebcSVladimir Oltean 	int rc, i;
13906666cebcSVladimir Oltean 
13916666cebcSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
13926666cebcSVladimir Oltean 
13938400cff6SVladimir Oltean 	/* Back up the dynamic link speed changed by sja1105_adjust_port_config
13948400cff6SVladimir Oltean 	 * in order to temporarily restore it to SJA1105_SPEED_AUTO - which the
13958400cff6SVladimir Oltean 	 * switch wants to see in the static config in order to allow us to
13968400cff6SVladimir Oltean 	 * change it through the dynamic interface later.
13976666cebcSVladimir Oltean 	 */
13986666cebcSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
13996666cebcSVladimir Oltean 		speed_mbps[i] = sja1105_speed[mac[i].speed];
14006666cebcSVladimir Oltean 		mac[i].speed = SJA1105_SPEED_AUTO;
14016666cebcSVladimir Oltean 	}
14026666cebcSVladimir Oltean 
14036666cebcSVladimir Oltean 	/* Reset switch and send updated static configuration */
14046666cebcSVladimir Oltean 	rc = sja1105_static_config_upload(priv);
14056666cebcSVladimir Oltean 	if (rc < 0)
14066666cebcSVladimir Oltean 		goto out;
14076666cebcSVladimir Oltean 
14086666cebcSVladimir Oltean 	/* Configure the CGU (PLLs) for MII and RMII PHYs.
14096666cebcSVladimir Oltean 	 * For these interfaces there is no dynamic configuration
14106666cebcSVladimir Oltean 	 * needed, since PLLs have same settings at all speeds.
14116666cebcSVladimir Oltean 	 */
14126666cebcSVladimir Oltean 	rc = sja1105_clocking_setup(priv);
14136666cebcSVladimir Oltean 	if (rc < 0)
14146666cebcSVladimir Oltean 		goto out;
14156666cebcSVladimir Oltean 
14166666cebcSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
14178400cff6SVladimir Oltean 		rc = sja1105_adjust_port_config(priv, i, speed_mbps[i]);
14186666cebcSVladimir Oltean 		if (rc < 0)
14196666cebcSVladimir Oltean 			goto out;
14206666cebcSVladimir Oltean 	}
14216666cebcSVladimir Oltean out:
14226666cebcSVladimir Oltean 	return rc;
14236666cebcSVladimir Oltean }
14246666cebcSVladimir Oltean 
14256666cebcSVladimir Oltean static int sja1105_pvid_apply(struct sja1105_private *priv, int port, u16 pvid)
14266666cebcSVladimir Oltean {
14276666cebcSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
14286666cebcSVladimir Oltean 
14296666cebcSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
14306666cebcSVladimir Oltean 
14316666cebcSVladimir Oltean 	mac[port].vlanid = pvid;
14326666cebcSVladimir Oltean 
14336666cebcSVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
14346666cebcSVladimir Oltean 					   &mac[port], true);
14356666cebcSVladimir Oltean }
14366666cebcSVladimir Oltean 
14376666cebcSVladimir Oltean static int sja1105_is_vlan_configured(struct sja1105_private *priv, u16 vid)
14386666cebcSVladimir Oltean {
14396666cebcSVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
14406666cebcSVladimir Oltean 	int count, i;
14416666cebcSVladimir Oltean 
14426666cebcSVladimir Oltean 	vlan = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entries;
14436666cebcSVladimir Oltean 	count = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entry_count;
14446666cebcSVladimir Oltean 
14456666cebcSVladimir Oltean 	for (i = 0; i < count; i++)
14466666cebcSVladimir Oltean 		if (vlan[i].vlanid == vid)
14476666cebcSVladimir Oltean 			return i;
14486666cebcSVladimir Oltean 
14496666cebcSVladimir Oltean 	/* Return an invalid entry index if not found */
14506666cebcSVladimir Oltean 	return -1;
14516666cebcSVladimir Oltean }
14526666cebcSVladimir Oltean 
14536666cebcSVladimir Oltean static int sja1105_vlan_apply(struct sja1105_private *priv, int port, u16 vid,
14546666cebcSVladimir Oltean 			      bool enabled, bool untagged)
14556666cebcSVladimir Oltean {
14566666cebcSVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
14576666cebcSVladimir Oltean 	struct sja1105_table *table;
14586666cebcSVladimir Oltean 	bool keep = true;
14596666cebcSVladimir Oltean 	int match, rc;
14606666cebcSVladimir Oltean 
14616666cebcSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
14626666cebcSVladimir Oltean 
14636666cebcSVladimir Oltean 	match = sja1105_is_vlan_configured(priv, vid);
14646666cebcSVladimir Oltean 	if (match < 0) {
14656666cebcSVladimir Oltean 		/* Can't delete a missing entry. */
14666666cebcSVladimir Oltean 		if (!enabled)
14676666cebcSVladimir Oltean 			return 0;
14686666cebcSVladimir Oltean 		rc = sja1105_table_resize(table, table->entry_count + 1);
14696666cebcSVladimir Oltean 		if (rc)
14706666cebcSVladimir Oltean 			return rc;
14716666cebcSVladimir Oltean 		match = table->entry_count - 1;
14726666cebcSVladimir Oltean 	}
14736666cebcSVladimir Oltean 	/* Assign pointer after the resize (it's new memory) */
14746666cebcSVladimir Oltean 	vlan = table->entries;
14756666cebcSVladimir Oltean 	vlan[match].vlanid = vid;
14766666cebcSVladimir Oltean 	if (enabled) {
14776666cebcSVladimir Oltean 		vlan[match].vlan_bc |= BIT(port);
14786666cebcSVladimir Oltean 		vlan[match].vmemb_port |= BIT(port);
14796666cebcSVladimir Oltean 	} else {
14806666cebcSVladimir Oltean 		vlan[match].vlan_bc &= ~BIT(port);
14816666cebcSVladimir Oltean 		vlan[match].vmemb_port &= ~BIT(port);
14826666cebcSVladimir Oltean 	}
14836666cebcSVladimir Oltean 	/* Also unset tag_port if removing this VLAN was requested,
14846666cebcSVladimir Oltean 	 * just so we don't have a confusing bitmap (no practical purpose).
14856666cebcSVladimir Oltean 	 */
14866666cebcSVladimir Oltean 	if (untagged || !enabled)
14876666cebcSVladimir Oltean 		vlan[match].tag_port &= ~BIT(port);
14886666cebcSVladimir Oltean 	else
14896666cebcSVladimir Oltean 		vlan[match].tag_port |= BIT(port);
14906666cebcSVladimir Oltean 	/* If there's no port left as member of this VLAN,
14916666cebcSVladimir Oltean 	 * it's time for it to go.
14926666cebcSVladimir Oltean 	 */
14936666cebcSVladimir Oltean 	if (!vlan[match].vmemb_port)
14946666cebcSVladimir Oltean 		keep = false;
14956666cebcSVladimir Oltean 
14966666cebcSVladimir Oltean 	dev_dbg(priv->ds->dev,
14976666cebcSVladimir Oltean 		"%s: port %d, vid %llu, broadcast domain 0x%llx, "
14986666cebcSVladimir Oltean 		"port members 0x%llx, tagged ports 0x%llx, keep %d\n",
14996666cebcSVladimir Oltean 		__func__, port, vlan[match].vlanid, vlan[match].vlan_bc,
15006666cebcSVladimir Oltean 		vlan[match].vmemb_port, vlan[match].tag_port, keep);
15016666cebcSVladimir Oltean 
15026666cebcSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_VLAN_LOOKUP, vid,
15036666cebcSVladimir Oltean 					  &vlan[match], keep);
15046666cebcSVladimir Oltean 	if (rc < 0)
15056666cebcSVladimir Oltean 		return rc;
15066666cebcSVladimir Oltean 
15076666cebcSVladimir Oltean 	if (!keep)
15086666cebcSVladimir Oltean 		return sja1105_table_delete_entry(table, match);
15096666cebcSVladimir Oltean 
15106666cebcSVladimir Oltean 	return 0;
15116666cebcSVladimir Oltean }
15126666cebcSVladimir Oltean 
1513227d07a0SVladimir Oltean static int sja1105_setup_8021q_tagging(struct dsa_switch *ds, bool enabled)
1514227d07a0SVladimir Oltean {
1515227d07a0SVladimir Oltean 	int rc, i;
1516227d07a0SVladimir Oltean 
1517227d07a0SVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
1518227d07a0SVladimir Oltean 		rc = dsa_port_setup_8021q_tagging(ds, i, enabled);
1519227d07a0SVladimir Oltean 		if (rc < 0) {
1520227d07a0SVladimir Oltean 			dev_err(ds->dev, "Failed to setup VLAN tagging for port %d: %d\n",
1521227d07a0SVladimir Oltean 				i, rc);
1522227d07a0SVladimir Oltean 			return rc;
1523227d07a0SVladimir Oltean 		}
1524227d07a0SVladimir Oltean 	}
1525227d07a0SVladimir Oltean 	dev_info(ds->dev, "%s switch tagging\n",
1526227d07a0SVladimir Oltean 		 enabled ? "Enabled" : "Disabled");
1527227d07a0SVladimir Oltean 	return 0;
1528227d07a0SVladimir Oltean }
1529227d07a0SVladimir Oltean 
15308aa9ebccSVladimir Oltean static enum dsa_tag_protocol
15318aa9ebccSVladimir Oltean sja1105_get_tag_protocol(struct dsa_switch *ds, int port)
15328aa9ebccSVladimir Oltean {
1533227d07a0SVladimir Oltean 	return DSA_TAG_PROTO_SJA1105;
15348aa9ebccSVladimir Oltean }
15358aa9ebccSVladimir Oltean 
15366666cebcSVladimir Oltean /* This callback needs to be present */
15376666cebcSVladimir Oltean static int sja1105_vlan_prepare(struct dsa_switch *ds, int port,
15386666cebcSVladimir Oltean 				const struct switchdev_obj_port_vlan *vlan)
15396666cebcSVladimir Oltean {
15406666cebcSVladimir Oltean 	return 0;
15416666cebcSVladimir Oltean }
15426666cebcSVladimir Oltean 
1543070ca3bbSVladimir Oltean /* The TPID setting belongs to the General Parameters table,
1544070ca3bbSVladimir Oltean  * which can only be partially reconfigured at runtime (and not the TPID).
1545070ca3bbSVladimir Oltean  * So a switch reset is required.
1546070ca3bbSVladimir Oltean  */
15476666cebcSVladimir Oltean static int sja1105_vlan_filtering(struct dsa_switch *ds, int port, bool enabled)
15486666cebcSVladimir Oltean {
15496d7c7d94SVladimir Oltean 	struct sja1105_l2_lookup_params_entry *l2_lookup_params;
1550070ca3bbSVladimir Oltean 	struct sja1105_general_params_entry *general_params;
15516666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1552070ca3bbSVladimir Oltean 	struct sja1105_table *table;
1553070ca3bbSVladimir Oltean 	u16 tpid, tpid2;
15546666cebcSVladimir Oltean 	int rc;
15556666cebcSVladimir Oltean 
1556070ca3bbSVladimir Oltean 	if (enabled) {
15576666cebcSVladimir Oltean 		/* Enable VLAN filtering. */
1558f9a1a764SVladimir Oltean 		tpid  = ETH_P_8021AD;
1559f9a1a764SVladimir Oltean 		tpid2 = ETH_P_8021Q;
1560070ca3bbSVladimir Oltean 	} else {
15616666cebcSVladimir Oltean 		/* Disable VLAN filtering. */
1562070ca3bbSVladimir Oltean 		tpid  = ETH_P_SJA1105;
1563070ca3bbSVladimir Oltean 		tpid2 = ETH_P_SJA1105;
1564070ca3bbSVladimir Oltean 	}
1565070ca3bbSVladimir Oltean 
1566070ca3bbSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
1567070ca3bbSVladimir Oltean 	general_params = table->entries;
1568f9a1a764SVladimir Oltean 	/* EtherType used to identify outer tagged (S-tag) VLAN traffic */
1569070ca3bbSVladimir Oltean 	general_params->tpid = tpid;
1570f9a1a764SVladimir Oltean 	/* EtherType used to identify inner tagged (C-tag) VLAN traffic */
1571070ca3bbSVladimir Oltean 	general_params->tpid2 = tpid2;
157242824463SVladimir Oltean 	/* When VLAN filtering is on, we need to at least be able to
157342824463SVladimir Oltean 	 * decode management traffic through the "backup plan".
157442824463SVladimir Oltean 	 */
157542824463SVladimir Oltean 	general_params->incl_srcpt1 = enabled;
157642824463SVladimir Oltean 	general_params->incl_srcpt0 = enabled;
1577070ca3bbSVladimir Oltean 
15786d7c7d94SVladimir Oltean 	/* VLAN filtering => independent VLAN learning.
15796d7c7d94SVladimir Oltean 	 * No VLAN filtering => shared VLAN learning.
15806d7c7d94SVladimir Oltean 	 *
15816d7c7d94SVladimir Oltean 	 * In shared VLAN learning mode, untagged traffic still gets
15826d7c7d94SVladimir Oltean 	 * pvid-tagged, and the FDB table gets populated with entries
15836d7c7d94SVladimir Oltean 	 * containing the "real" (pvid or from VLAN tag) VLAN ID.
15846d7c7d94SVladimir Oltean 	 * However the switch performs a masked L2 lookup in the FDB,
15856d7c7d94SVladimir Oltean 	 * effectively only looking up a frame's DMAC (and not VID) for the
15866d7c7d94SVladimir Oltean 	 * forwarding decision.
15876d7c7d94SVladimir Oltean 	 *
15886d7c7d94SVladimir Oltean 	 * This is extremely convenient for us, because in modes with
15896d7c7d94SVladimir Oltean 	 * vlan_filtering=0, dsa_8021q actually installs unique pvid's into
15906d7c7d94SVladimir Oltean 	 * each front panel port. This is good for identification but breaks
15916d7c7d94SVladimir Oltean 	 * learning badly - the VID of the learnt FDB entry is unique, aka
15926d7c7d94SVladimir Oltean 	 * no frames coming from any other port are going to have it. So
15936d7c7d94SVladimir Oltean 	 * for forwarding purposes, this is as though learning was broken
15946d7c7d94SVladimir Oltean 	 * (all frames get flooded).
15956d7c7d94SVladimir Oltean 	 */
15966d7c7d94SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
15976d7c7d94SVladimir Oltean 	l2_lookup_params = table->entries;
15986d7c7d94SVladimir Oltean 	l2_lookup_params->shared_learn = !enabled;
15996d7c7d94SVladimir Oltean 
1600070ca3bbSVladimir Oltean 	rc = sja1105_static_config_reload(priv);
16016666cebcSVladimir Oltean 	if (rc)
16026666cebcSVladimir Oltean 		dev_err(ds->dev, "Failed to change VLAN Ethertype\n");
16036666cebcSVladimir Oltean 
1604227d07a0SVladimir Oltean 	/* Switch port identification based on 802.1Q is only passable
1605227d07a0SVladimir Oltean 	 * if we are not under a vlan_filtering bridge. So make sure
1606227d07a0SVladimir Oltean 	 * the two configurations are mutually exclusive.
1607227d07a0SVladimir Oltean 	 */
1608227d07a0SVladimir Oltean 	return sja1105_setup_8021q_tagging(ds, !enabled);
16096666cebcSVladimir Oltean }
16106666cebcSVladimir Oltean 
16116666cebcSVladimir Oltean static void sja1105_vlan_add(struct dsa_switch *ds, int port,
16126666cebcSVladimir Oltean 			     const struct switchdev_obj_port_vlan *vlan)
16136666cebcSVladimir Oltean {
16146666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
16156666cebcSVladimir Oltean 	u16 vid;
16166666cebcSVladimir Oltean 	int rc;
16176666cebcSVladimir Oltean 
16186666cebcSVladimir Oltean 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
16196666cebcSVladimir Oltean 		rc = sja1105_vlan_apply(priv, port, vid, true, vlan->flags &
16206666cebcSVladimir Oltean 					BRIDGE_VLAN_INFO_UNTAGGED);
16216666cebcSVladimir Oltean 		if (rc < 0) {
16226666cebcSVladimir Oltean 			dev_err(ds->dev, "Failed to add VLAN %d to port %d: %d\n",
16236666cebcSVladimir Oltean 				vid, port, rc);
16246666cebcSVladimir Oltean 			return;
16256666cebcSVladimir Oltean 		}
16266666cebcSVladimir Oltean 		if (vlan->flags & BRIDGE_VLAN_INFO_PVID) {
16276666cebcSVladimir Oltean 			rc = sja1105_pvid_apply(ds->priv, port, vid);
16286666cebcSVladimir Oltean 			if (rc < 0) {
16296666cebcSVladimir Oltean 				dev_err(ds->dev, "Failed to set pvid %d on port %d: %d\n",
16306666cebcSVladimir Oltean 					vid, port, rc);
16316666cebcSVladimir Oltean 				return;
16326666cebcSVladimir Oltean 			}
16336666cebcSVladimir Oltean 		}
16346666cebcSVladimir Oltean 	}
16356666cebcSVladimir Oltean }
16366666cebcSVladimir Oltean 
16376666cebcSVladimir Oltean static int sja1105_vlan_del(struct dsa_switch *ds, int port,
16386666cebcSVladimir Oltean 			    const struct switchdev_obj_port_vlan *vlan)
16396666cebcSVladimir Oltean {
16406666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
16416666cebcSVladimir Oltean 	u16 vid;
16426666cebcSVladimir Oltean 	int rc;
16436666cebcSVladimir Oltean 
16446666cebcSVladimir Oltean 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
16456666cebcSVladimir Oltean 		rc = sja1105_vlan_apply(priv, port, vid, false, vlan->flags &
16466666cebcSVladimir Oltean 					BRIDGE_VLAN_INFO_UNTAGGED);
16476666cebcSVladimir Oltean 		if (rc < 0) {
16486666cebcSVladimir Oltean 			dev_err(ds->dev, "Failed to remove VLAN %d from port %d: %d\n",
16496666cebcSVladimir Oltean 				vid, port, rc);
16506666cebcSVladimir Oltean 			return rc;
16516666cebcSVladimir Oltean 		}
16526666cebcSVladimir Oltean 	}
16536666cebcSVladimir Oltean 	return 0;
16546666cebcSVladimir Oltean }
16556666cebcSVladimir Oltean 
16568aa9ebccSVladimir Oltean /* The programming model for the SJA1105 switch is "all-at-once" via static
16578aa9ebccSVladimir Oltean  * configuration tables. Some of these can be dynamically modified at runtime,
16588aa9ebccSVladimir Oltean  * but not the xMII mode parameters table.
16598aa9ebccSVladimir Oltean  * Furthermode, some PHYs may not have crystals for generating their clocks
16608aa9ebccSVladimir Oltean  * (e.g. RMII). Instead, their 50MHz clock is supplied via the SJA1105 port's
16618aa9ebccSVladimir Oltean  * ref_clk pin. So port clocking needs to be initialized early, before
16628aa9ebccSVladimir Oltean  * connecting to PHYs is attempted, otherwise they won't respond through MDIO.
16638aa9ebccSVladimir Oltean  * Setting correct PHY link speed does not matter now.
16648aa9ebccSVladimir Oltean  * But dsa_slave_phy_setup is called later than sja1105_setup, so the PHY
16658aa9ebccSVladimir Oltean  * bindings are not yet parsed by DSA core. We need to parse early so that we
16668aa9ebccSVladimir Oltean  * can populate the xMII mode parameters table.
16678aa9ebccSVladimir Oltean  */
16688aa9ebccSVladimir Oltean static int sja1105_setup(struct dsa_switch *ds)
16698aa9ebccSVladimir Oltean {
16708aa9ebccSVladimir Oltean 	struct sja1105_dt_port ports[SJA1105_NUM_PORTS];
16718aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
16728aa9ebccSVladimir Oltean 	int rc;
16738aa9ebccSVladimir Oltean 
16748aa9ebccSVladimir Oltean 	rc = sja1105_parse_dt(priv, ports);
16758aa9ebccSVladimir Oltean 	if (rc < 0) {
16768aa9ebccSVladimir Oltean 		dev_err(ds->dev, "Failed to parse DT: %d\n", rc);
16778aa9ebccSVladimir Oltean 		return rc;
16788aa9ebccSVladimir Oltean 	}
1679f5b8631cSVladimir Oltean 
1680f5b8631cSVladimir Oltean 	/* Error out early if internal delays are required through DT
1681f5b8631cSVladimir Oltean 	 * and we can't apply them.
1682f5b8631cSVladimir Oltean 	 */
1683f5b8631cSVladimir Oltean 	rc = sja1105_parse_rgmii_delays(priv, ports);
1684f5b8631cSVladimir Oltean 	if (rc < 0) {
1685f5b8631cSVladimir Oltean 		dev_err(ds->dev, "RGMII delay not supported\n");
1686f5b8631cSVladimir Oltean 		return rc;
1687f5b8631cSVladimir Oltean 	}
1688f5b8631cSVladimir Oltean 
1689bb77f36aSVladimir Oltean 	rc = sja1105_ptp_clock_register(priv);
1690bb77f36aSVladimir Oltean 	if (rc < 0) {
1691bb77f36aSVladimir Oltean 		dev_err(ds->dev, "Failed to register PTP clock: %d\n", rc);
1692bb77f36aSVladimir Oltean 		return rc;
1693bb77f36aSVladimir Oltean 	}
16948aa9ebccSVladimir Oltean 	/* Create and send configuration down to device */
16958aa9ebccSVladimir Oltean 	rc = sja1105_static_config_load(priv, ports);
16968aa9ebccSVladimir Oltean 	if (rc < 0) {
16978aa9ebccSVladimir Oltean 		dev_err(ds->dev, "Failed to load static config: %d\n", rc);
16988aa9ebccSVladimir Oltean 		return rc;
16998aa9ebccSVladimir Oltean 	}
17008aa9ebccSVladimir Oltean 	/* Configure the CGU (PHY link modes and speeds) */
17018aa9ebccSVladimir Oltean 	rc = sja1105_clocking_setup(priv);
17028aa9ebccSVladimir Oltean 	if (rc < 0) {
17038aa9ebccSVladimir Oltean 		dev_err(ds->dev, "Failed to configure MII clocking: %d\n", rc);
17048aa9ebccSVladimir Oltean 		return rc;
17058aa9ebccSVladimir Oltean 	}
17066666cebcSVladimir Oltean 	/* On SJA1105, VLAN filtering per se is always enabled in hardware.
17076666cebcSVladimir Oltean 	 * The only thing we can do to disable it is lie about what the 802.1Q
17086666cebcSVladimir Oltean 	 * EtherType is.
17096666cebcSVladimir Oltean 	 * So it will still try to apply VLAN filtering, but all ingress
17106666cebcSVladimir Oltean 	 * traffic (except frames received with EtherType of ETH_P_SJA1105)
17116666cebcSVladimir Oltean 	 * will be internally tagged with a distorted VLAN header where the
17126666cebcSVladimir Oltean 	 * TPID is ETH_P_SJA1105, and the VLAN ID is the port pvid.
17136666cebcSVladimir Oltean 	 */
17146666cebcSVladimir Oltean 	ds->vlan_filtering_is_global = true;
17158aa9ebccSVladimir Oltean 
17165f06c63bSVladimir Oltean 	/* Advertise the 8 egress queues */
17175f06c63bSVladimir Oltean 	ds->num_tx_queues = SJA1105_NUM_TC;
17185f06c63bSVladimir Oltean 
1719227d07a0SVladimir Oltean 	/* The DSA/switchdev model brings up switch ports in standalone mode by
1720227d07a0SVladimir Oltean 	 * default, and that means vlan_filtering is 0 since they're not under
1721227d07a0SVladimir Oltean 	 * a bridge, so it's safe to set up switch tagging at this time.
1722227d07a0SVladimir Oltean 	 */
1723227d07a0SVladimir Oltean 	return sja1105_setup_8021q_tagging(ds, true);
1724227d07a0SVladimir Oltean }
1725227d07a0SVladimir Oltean 
1726f3097be2SVladimir Oltean static void sja1105_teardown(struct dsa_switch *ds)
1727f3097be2SVladimir Oltean {
1728f3097be2SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1729f3097be2SVladimir Oltean 
1730317ab5b8SVladimir Oltean 	sja1105_tas_teardown(ds);
1731f3097be2SVladimir Oltean 	cancel_work_sync(&priv->tagger_data.rxtstamp_work);
1732f3097be2SVladimir Oltean 	skb_queue_purge(&priv->tagger_data.skb_rxtstamp_queue);
17336cb0abbdSVladimir Oltean 	sja1105_ptp_clock_unregister(priv);
17346cb0abbdSVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
1735f3097be2SVladimir Oltean }
1736f3097be2SVladimir Oltean 
1737e9bf9694SVladimir Oltean static int sja1105_port_enable(struct dsa_switch *ds, int port,
1738e9bf9694SVladimir Oltean 			       struct phy_device *phy)
1739e9bf9694SVladimir Oltean {
1740e9bf9694SVladimir Oltean 	struct net_device *slave;
1741e9bf9694SVladimir Oltean 
1742e9bf9694SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
1743e9bf9694SVladimir Oltean 		return 0;
1744e9bf9694SVladimir Oltean 
1745e9bf9694SVladimir Oltean 	slave = ds->ports[port].slave;
1746e9bf9694SVladimir Oltean 
1747e9bf9694SVladimir Oltean 	slave->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
1748e9bf9694SVladimir Oltean 
1749e9bf9694SVladimir Oltean 	return 0;
1750e9bf9694SVladimir Oltean }
1751e9bf9694SVladimir Oltean 
1752227d07a0SVladimir Oltean static int sja1105_mgmt_xmit(struct dsa_switch *ds, int port, int slot,
175347ed985eSVladimir Oltean 			     struct sk_buff *skb, bool takets)
1754227d07a0SVladimir Oltean {
1755227d07a0SVladimir Oltean 	struct sja1105_mgmt_entry mgmt_route = {0};
1756227d07a0SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1757227d07a0SVladimir Oltean 	struct ethhdr *hdr;
1758227d07a0SVladimir Oltean 	int timeout = 10;
1759227d07a0SVladimir Oltean 	int rc;
1760227d07a0SVladimir Oltean 
1761227d07a0SVladimir Oltean 	hdr = eth_hdr(skb);
1762227d07a0SVladimir Oltean 
1763227d07a0SVladimir Oltean 	mgmt_route.macaddr = ether_addr_to_u64(hdr->h_dest);
1764227d07a0SVladimir Oltean 	mgmt_route.destports = BIT(port);
1765227d07a0SVladimir Oltean 	mgmt_route.enfport = 1;
176647ed985eSVladimir Oltean 	mgmt_route.tsreg = 0;
176747ed985eSVladimir Oltean 	mgmt_route.takets = takets;
1768227d07a0SVladimir Oltean 
1769227d07a0SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE,
1770227d07a0SVladimir Oltean 					  slot, &mgmt_route, true);
1771227d07a0SVladimir Oltean 	if (rc < 0) {
1772227d07a0SVladimir Oltean 		kfree_skb(skb);
1773227d07a0SVladimir Oltean 		return rc;
1774227d07a0SVladimir Oltean 	}
1775227d07a0SVladimir Oltean 
1776227d07a0SVladimir Oltean 	/* Transfer skb to the host port. */
1777227d07a0SVladimir Oltean 	dsa_enqueue_skb(skb, ds->ports[port].slave);
1778227d07a0SVladimir Oltean 
1779227d07a0SVladimir Oltean 	/* Wait until the switch has processed the frame */
1780227d07a0SVladimir Oltean 	do {
1781227d07a0SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_MGMT_ROUTE,
1782227d07a0SVladimir Oltean 						 slot, &mgmt_route);
1783227d07a0SVladimir Oltean 		if (rc < 0) {
1784227d07a0SVladimir Oltean 			dev_err_ratelimited(priv->ds->dev,
1785227d07a0SVladimir Oltean 					    "failed to poll for mgmt route\n");
1786227d07a0SVladimir Oltean 			continue;
1787227d07a0SVladimir Oltean 		}
1788227d07a0SVladimir Oltean 
1789227d07a0SVladimir Oltean 		/* UM10944: The ENFPORT flag of the respective entry is
1790227d07a0SVladimir Oltean 		 * cleared when a match is found. The host can use this
1791227d07a0SVladimir Oltean 		 * flag as an acknowledgment.
1792227d07a0SVladimir Oltean 		 */
1793227d07a0SVladimir Oltean 		cpu_relax();
1794227d07a0SVladimir Oltean 	} while (mgmt_route.enfport && --timeout);
1795227d07a0SVladimir Oltean 
1796227d07a0SVladimir Oltean 	if (!timeout) {
1797227d07a0SVladimir Oltean 		/* Clean up the management route so that a follow-up
1798227d07a0SVladimir Oltean 		 * frame may not match on it by mistake.
17992a7e7409SVladimir Oltean 		 * This is only hardware supported on P/Q/R/S - on E/T it is
18002a7e7409SVladimir Oltean 		 * a no-op and we are silently discarding the -EOPNOTSUPP.
1801227d07a0SVladimir Oltean 		 */
1802227d07a0SVladimir Oltean 		sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE,
1803227d07a0SVladimir Oltean 					     slot, &mgmt_route, false);
1804227d07a0SVladimir Oltean 		dev_err_ratelimited(priv->ds->dev, "xmit timed out\n");
1805227d07a0SVladimir Oltean 	}
1806227d07a0SVladimir Oltean 
1807227d07a0SVladimir Oltean 	return NETDEV_TX_OK;
1808227d07a0SVladimir Oltean }
1809227d07a0SVladimir Oltean 
1810227d07a0SVladimir Oltean /* Deferred work is unfortunately necessary because setting up the management
1811227d07a0SVladimir Oltean  * route cannot be done from atomit context (SPI transfer takes a sleepable
1812227d07a0SVladimir Oltean  * lock on the bus)
1813227d07a0SVladimir Oltean  */
1814227d07a0SVladimir Oltean static netdev_tx_t sja1105_port_deferred_xmit(struct dsa_switch *ds, int port,
1815227d07a0SVladimir Oltean 					      struct sk_buff *skb)
1816227d07a0SVladimir Oltean {
1817227d07a0SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1818227d07a0SVladimir Oltean 	struct sja1105_port *sp = &priv->ports[port];
181947ed985eSVladimir Oltean 	struct skb_shared_hwtstamps shwt = {0};
1820227d07a0SVladimir Oltean 	int slot = sp->mgmt_slot;
182147ed985eSVladimir Oltean 	struct sk_buff *clone;
182247ed985eSVladimir Oltean 	u64 now, ts;
182347ed985eSVladimir Oltean 	int rc;
1824227d07a0SVladimir Oltean 
1825227d07a0SVladimir Oltean 	/* The tragic fact about the switch having 4x2 slots for installing
1826227d07a0SVladimir Oltean 	 * management routes is that all of them except one are actually
1827227d07a0SVladimir Oltean 	 * useless.
1828227d07a0SVladimir Oltean 	 * If 2 slots are simultaneously configured for two BPDUs sent to the
1829227d07a0SVladimir Oltean 	 * same (multicast) DMAC but on different egress ports, the switch
1830227d07a0SVladimir Oltean 	 * would confuse them and redirect first frame it receives on the CPU
1831227d07a0SVladimir Oltean 	 * port towards the port configured on the numerically first slot
1832227d07a0SVladimir Oltean 	 * (therefore wrong port), then second received frame on second slot
1833227d07a0SVladimir Oltean 	 * (also wrong port).
1834227d07a0SVladimir Oltean 	 * So for all practical purposes, there needs to be a lock that
1835227d07a0SVladimir Oltean 	 * prevents that from happening. The slot used here is utterly useless
1836227d07a0SVladimir Oltean 	 * (could have simply been 0 just as fine), but we are doing it
1837227d07a0SVladimir Oltean 	 * nonetheless, in case a smarter idea ever comes up in the future.
1838227d07a0SVladimir Oltean 	 */
1839227d07a0SVladimir Oltean 	mutex_lock(&priv->mgmt_lock);
1840227d07a0SVladimir Oltean 
184147ed985eSVladimir Oltean 	/* The clone, if there, was made by dsa_skb_tx_timestamp */
184247ed985eSVladimir Oltean 	clone = DSA_SKB_CB(skb)->clone;
1843227d07a0SVladimir Oltean 
184447ed985eSVladimir Oltean 	sja1105_mgmt_xmit(ds, port, slot, skb, !!clone);
184547ed985eSVladimir Oltean 
184647ed985eSVladimir Oltean 	if (!clone)
184747ed985eSVladimir Oltean 		goto out;
184847ed985eSVladimir Oltean 
184947ed985eSVladimir Oltean 	skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS;
185047ed985eSVladimir Oltean 
185147ed985eSVladimir Oltean 	mutex_lock(&priv->ptp_lock);
185247ed985eSVladimir Oltean 
185347ed985eSVladimir Oltean 	now = priv->tstamp_cc.read(&priv->tstamp_cc);
185447ed985eSVladimir Oltean 
185547ed985eSVladimir Oltean 	rc = sja1105_ptpegr_ts_poll(priv, slot, &ts);
185647ed985eSVladimir Oltean 	if (rc < 0) {
185747ed985eSVladimir Oltean 		dev_err(ds->dev, "xmit: timed out polling for tstamp\n");
185847ed985eSVladimir Oltean 		kfree_skb(clone);
185947ed985eSVladimir Oltean 		goto out_unlock_ptp;
186047ed985eSVladimir Oltean 	}
186147ed985eSVladimir Oltean 
186247ed985eSVladimir Oltean 	ts = sja1105_tstamp_reconstruct(priv, now, ts);
186347ed985eSVladimir Oltean 	ts = timecounter_cyc2time(&priv->tstamp_tc, ts);
186447ed985eSVladimir Oltean 
186547ed985eSVladimir Oltean 	shwt.hwtstamp = ns_to_ktime(ts);
186647ed985eSVladimir Oltean 	skb_complete_tx_timestamp(clone, &shwt);
186747ed985eSVladimir Oltean 
186847ed985eSVladimir Oltean out_unlock_ptp:
186947ed985eSVladimir Oltean 	mutex_unlock(&priv->ptp_lock);
187047ed985eSVladimir Oltean out:
1871227d07a0SVladimir Oltean 	mutex_unlock(&priv->mgmt_lock);
1872227d07a0SVladimir Oltean 	return NETDEV_TX_OK;
18738aa9ebccSVladimir Oltean }
18748aa9ebccSVladimir Oltean 
18758456721dSVladimir Oltean /* The MAXAGE setting belongs to the L2 Forwarding Parameters table,
18768456721dSVladimir Oltean  * which cannot be reconfigured at runtime. So a switch reset is required.
18778456721dSVladimir Oltean  */
18788456721dSVladimir Oltean static int sja1105_set_ageing_time(struct dsa_switch *ds,
18798456721dSVladimir Oltean 				   unsigned int ageing_time)
18808456721dSVladimir Oltean {
18818456721dSVladimir Oltean 	struct sja1105_l2_lookup_params_entry *l2_lookup_params;
18828456721dSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
18838456721dSVladimir Oltean 	struct sja1105_table *table;
18848456721dSVladimir Oltean 	unsigned int maxage;
18858456721dSVladimir Oltean 
18868456721dSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
18878456721dSVladimir Oltean 	l2_lookup_params = table->entries;
18888456721dSVladimir Oltean 
18898456721dSVladimir Oltean 	maxage = SJA1105_AGEING_TIME_MS(ageing_time);
18908456721dSVladimir Oltean 
18918456721dSVladimir Oltean 	if (l2_lookup_params->maxage == maxage)
18928456721dSVladimir Oltean 		return 0;
18938456721dSVladimir Oltean 
18948456721dSVladimir Oltean 	l2_lookup_params->maxage = maxage;
18958456721dSVladimir Oltean 
18968456721dSVladimir Oltean 	return sja1105_static_config_reload(priv);
18978456721dSVladimir Oltean }
18988456721dSVladimir Oltean 
1899a602afd2SVladimir Oltean /* Caller must hold priv->tagger_data.meta_lock */
1900a602afd2SVladimir Oltean static int sja1105_change_rxtstamping(struct sja1105_private *priv,
1901a602afd2SVladimir Oltean 				      bool on)
1902a602afd2SVladimir Oltean {
1903a602afd2SVladimir Oltean 	struct sja1105_general_params_entry *general_params;
1904a602afd2SVladimir Oltean 	struct sja1105_table *table;
1905a602afd2SVladimir Oltean 	int rc;
1906a602afd2SVladimir Oltean 
1907a602afd2SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
1908a602afd2SVladimir Oltean 	general_params = table->entries;
1909a602afd2SVladimir Oltean 	general_params->send_meta1 = on;
1910a602afd2SVladimir Oltean 	general_params->send_meta0 = on;
1911a602afd2SVladimir Oltean 
1912a602afd2SVladimir Oltean 	rc = sja1105_init_avb_params(priv, on);
1913a602afd2SVladimir Oltean 	if (rc < 0)
1914a602afd2SVladimir Oltean 		return rc;
1915a602afd2SVladimir Oltean 
1916a602afd2SVladimir Oltean 	/* Initialize the meta state machine to a known state */
1917a602afd2SVladimir Oltean 	if (priv->tagger_data.stampable_skb) {
1918a602afd2SVladimir Oltean 		kfree_skb(priv->tagger_data.stampable_skb);
1919a602afd2SVladimir Oltean 		priv->tagger_data.stampable_skb = NULL;
1920a602afd2SVladimir Oltean 	}
1921a602afd2SVladimir Oltean 
1922a602afd2SVladimir Oltean 	return sja1105_static_config_reload(priv);
1923a602afd2SVladimir Oltean }
1924a602afd2SVladimir Oltean 
1925a602afd2SVladimir Oltean static int sja1105_hwtstamp_set(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 	bool rx_on;
1931a602afd2SVladimir Oltean 	int rc;
1932a602afd2SVladimir Oltean 
1933a602afd2SVladimir Oltean 	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
1934a602afd2SVladimir Oltean 		return -EFAULT;
1935a602afd2SVladimir Oltean 
1936a602afd2SVladimir Oltean 	switch (config.tx_type) {
1937a602afd2SVladimir Oltean 	case HWTSTAMP_TX_OFF:
1938a602afd2SVladimir Oltean 		priv->ports[port].hwts_tx_en = false;
1939a602afd2SVladimir Oltean 		break;
1940a602afd2SVladimir Oltean 	case HWTSTAMP_TX_ON:
1941a602afd2SVladimir Oltean 		priv->ports[port].hwts_tx_en = true;
1942a602afd2SVladimir Oltean 		break;
1943a602afd2SVladimir Oltean 	default:
1944a602afd2SVladimir Oltean 		return -ERANGE;
1945a602afd2SVladimir Oltean 	}
1946a602afd2SVladimir Oltean 
1947a602afd2SVladimir Oltean 	switch (config.rx_filter) {
1948a602afd2SVladimir Oltean 	case HWTSTAMP_FILTER_NONE:
1949a602afd2SVladimir Oltean 		rx_on = false;
1950a602afd2SVladimir Oltean 		break;
1951a602afd2SVladimir Oltean 	default:
1952a602afd2SVladimir Oltean 		rx_on = true;
1953a602afd2SVladimir Oltean 		break;
1954a602afd2SVladimir Oltean 	}
1955a602afd2SVladimir Oltean 
1956a602afd2SVladimir Oltean 	if (rx_on != priv->tagger_data.hwts_rx_en) {
1957a602afd2SVladimir Oltean 		spin_lock(&priv->tagger_data.meta_lock);
1958a602afd2SVladimir Oltean 		rc = sja1105_change_rxtstamping(priv, rx_on);
1959a602afd2SVladimir Oltean 		spin_unlock(&priv->tagger_data.meta_lock);
1960a602afd2SVladimir Oltean 		if (rc < 0) {
1961a602afd2SVladimir Oltean 			dev_err(ds->dev,
1962a602afd2SVladimir Oltean 				"Failed to change RX timestamping: %d\n", rc);
1963a602afd2SVladimir Oltean 			return -EFAULT;
1964a602afd2SVladimir Oltean 		}
1965a602afd2SVladimir Oltean 		priv->tagger_data.hwts_rx_en = rx_on;
1966a602afd2SVladimir Oltean 	}
1967a602afd2SVladimir Oltean 
1968a602afd2SVladimir Oltean 	if (copy_to_user(ifr->ifr_data, &config, sizeof(config)))
1969a602afd2SVladimir Oltean 		return -EFAULT;
1970a602afd2SVladimir Oltean 	return 0;
1971a602afd2SVladimir Oltean }
1972a602afd2SVladimir Oltean 
1973a602afd2SVladimir Oltean static int sja1105_hwtstamp_get(struct dsa_switch *ds, int port,
1974a602afd2SVladimir Oltean 				struct ifreq *ifr)
1975a602afd2SVladimir Oltean {
1976a602afd2SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1977a602afd2SVladimir Oltean 	struct hwtstamp_config config;
1978a602afd2SVladimir Oltean 
1979a602afd2SVladimir Oltean 	config.flags = 0;
1980a602afd2SVladimir Oltean 	if (priv->ports[port].hwts_tx_en)
1981a602afd2SVladimir Oltean 		config.tx_type = HWTSTAMP_TX_ON;
1982a602afd2SVladimir Oltean 	else
1983a602afd2SVladimir Oltean 		config.tx_type = HWTSTAMP_TX_OFF;
1984a602afd2SVladimir Oltean 	if (priv->tagger_data.hwts_rx_en)
1985a602afd2SVladimir Oltean 		config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
1986a602afd2SVladimir Oltean 	else
1987a602afd2SVladimir Oltean 		config.rx_filter = HWTSTAMP_FILTER_NONE;
1988a602afd2SVladimir Oltean 
1989a602afd2SVladimir Oltean 	return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
1990a602afd2SVladimir Oltean 		-EFAULT : 0;
1991a602afd2SVladimir Oltean }
1992a602afd2SVladimir Oltean 
1993f3097be2SVladimir Oltean #define to_tagger(d) \
1994f3097be2SVladimir Oltean 	container_of((d), struct sja1105_tagger_data, rxtstamp_work)
1995f3097be2SVladimir Oltean #define to_sja1105(d) \
1996f3097be2SVladimir Oltean 	container_of((d), struct sja1105_private, tagger_data)
1997f3097be2SVladimir Oltean 
1998f3097be2SVladimir Oltean static void sja1105_rxtstamp_work(struct work_struct *work)
1999f3097be2SVladimir Oltean {
2000f3097be2SVladimir Oltean 	struct sja1105_tagger_data *data = to_tagger(work);
2001f3097be2SVladimir Oltean 	struct sja1105_private *priv = to_sja1105(data);
2002f3097be2SVladimir Oltean 	struct sk_buff *skb;
2003f3097be2SVladimir Oltean 	u64 now;
2004f3097be2SVladimir Oltean 
2005f3097be2SVladimir Oltean 	mutex_lock(&priv->ptp_lock);
2006f3097be2SVladimir Oltean 
2007f3097be2SVladimir Oltean 	now = priv->tstamp_cc.read(&priv->tstamp_cc);
2008f3097be2SVladimir Oltean 
2009f3097be2SVladimir Oltean 	while ((skb = skb_dequeue(&data->skb_rxtstamp_queue)) != NULL) {
2010f3097be2SVladimir Oltean 		struct skb_shared_hwtstamps *shwt = skb_hwtstamps(skb);
2011f3097be2SVladimir Oltean 		u64 ts;
2012f3097be2SVladimir Oltean 
2013f3097be2SVladimir Oltean 		*shwt = (struct skb_shared_hwtstamps) {0};
2014f3097be2SVladimir Oltean 
2015f3097be2SVladimir Oltean 		ts = SJA1105_SKB_CB(skb)->meta_tstamp;
2016f3097be2SVladimir Oltean 		ts = sja1105_tstamp_reconstruct(priv, now, ts);
2017f3097be2SVladimir Oltean 		ts = timecounter_cyc2time(&priv->tstamp_tc, ts);
2018f3097be2SVladimir Oltean 
2019f3097be2SVladimir Oltean 		shwt->hwtstamp = ns_to_ktime(ts);
2020f3097be2SVladimir Oltean 		netif_rx_ni(skb);
2021f3097be2SVladimir Oltean 	}
2022f3097be2SVladimir Oltean 
2023f3097be2SVladimir Oltean 	mutex_unlock(&priv->ptp_lock);
2024f3097be2SVladimir Oltean }
2025f3097be2SVladimir Oltean 
2026f3097be2SVladimir Oltean /* Called from dsa_skb_defer_rx_timestamp */
20271dbb9869SYueHaibing static bool sja1105_port_rxtstamp(struct dsa_switch *ds, int port,
2028f3097be2SVladimir Oltean 				  struct sk_buff *skb, unsigned int type)
2029f3097be2SVladimir Oltean {
2030f3097be2SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2031f3097be2SVladimir Oltean 	struct sja1105_tagger_data *data = &priv->tagger_data;
2032f3097be2SVladimir Oltean 
2033f3097be2SVladimir Oltean 	if (!data->hwts_rx_en)
2034f3097be2SVladimir Oltean 		return false;
2035f3097be2SVladimir Oltean 
2036f3097be2SVladimir Oltean 	/* We need to read the full PTP clock to reconstruct the Rx
2037f3097be2SVladimir Oltean 	 * timestamp. For that we need a sleepable context.
2038f3097be2SVladimir Oltean 	 */
2039f3097be2SVladimir Oltean 	skb_queue_tail(&data->skb_rxtstamp_queue, skb);
2040f3097be2SVladimir Oltean 	schedule_work(&data->rxtstamp_work);
2041f3097be2SVladimir Oltean 	return true;
2042f3097be2SVladimir Oltean }
2043f3097be2SVladimir Oltean 
204447ed985eSVladimir Oltean /* Called from dsa_skb_tx_timestamp. This callback is just to make DSA clone
204547ed985eSVladimir Oltean  * the skb and have it available in DSA_SKB_CB in the .port_deferred_xmit
204647ed985eSVladimir Oltean  * callback, where we will timestamp it synchronously.
204747ed985eSVladimir Oltean  */
20481dbb9869SYueHaibing static bool sja1105_port_txtstamp(struct dsa_switch *ds, int port,
204947ed985eSVladimir Oltean 				  struct sk_buff *skb, unsigned int type)
205047ed985eSVladimir Oltean {
205147ed985eSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
205247ed985eSVladimir Oltean 	struct sja1105_port *sp = &priv->ports[port];
205347ed985eSVladimir Oltean 
205447ed985eSVladimir Oltean 	if (!sp->hwts_tx_en)
205547ed985eSVladimir Oltean 		return false;
205647ed985eSVladimir Oltean 
205747ed985eSVladimir Oltean 	return true;
205847ed985eSVladimir Oltean }
205947ed985eSVladimir Oltean 
2060317ab5b8SVladimir Oltean static int sja1105_port_setup_tc(struct dsa_switch *ds, int port,
2061317ab5b8SVladimir Oltean 				 enum tc_setup_type type,
2062317ab5b8SVladimir Oltean 				 void *type_data)
2063317ab5b8SVladimir Oltean {
2064317ab5b8SVladimir Oltean 	switch (type) {
2065317ab5b8SVladimir Oltean 	case TC_SETUP_QDISC_TAPRIO:
2066317ab5b8SVladimir Oltean 		return sja1105_setup_tc_taprio(ds, port, type_data);
2067317ab5b8SVladimir Oltean 	default:
2068317ab5b8SVladimir Oltean 		return -EOPNOTSUPP;
2069317ab5b8SVladimir Oltean 	}
2070317ab5b8SVladimir Oltean }
2071317ab5b8SVladimir Oltean 
2072*511e6ca0SVladimir Oltean /* We have a single mirror (@to) port, but can configure ingress and egress
2073*511e6ca0SVladimir Oltean  * mirroring on all other (@from) ports.
2074*511e6ca0SVladimir Oltean  * We need to allow mirroring rules only as long as the @to port is always the
2075*511e6ca0SVladimir Oltean  * same, and we need to unset the @to port from mirr_port only when there is no
2076*511e6ca0SVladimir Oltean  * mirroring rule that references it.
2077*511e6ca0SVladimir Oltean  */
2078*511e6ca0SVladimir Oltean static int sja1105_mirror_apply(struct sja1105_private *priv, int from, int to,
2079*511e6ca0SVladimir Oltean 				bool ingress, bool enabled)
2080*511e6ca0SVladimir Oltean {
2081*511e6ca0SVladimir Oltean 	struct sja1105_general_params_entry *general_params;
2082*511e6ca0SVladimir Oltean 	struct sja1105_mac_config_entry *mac;
2083*511e6ca0SVladimir Oltean 	struct sja1105_table *table;
2084*511e6ca0SVladimir Oltean 	bool already_enabled;
2085*511e6ca0SVladimir Oltean 	u64 new_mirr_port;
2086*511e6ca0SVladimir Oltean 	int rc;
2087*511e6ca0SVladimir Oltean 
2088*511e6ca0SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
2089*511e6ca0SVladimir Oltean 	general_params = table->entries;
2090*511e6ca0SVladimir Oltean 
2091*511e6ca0SVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
2092*511e6ca0SVladimir Oltean 
2093*511e6ca0SVladimir Oltean 	already_enabled = (general_params->mirr_port != SJA1105_NUM_PORTS);
2094*511e6ca0SVladimir Oltean 	if (already_enabled && enabled && general_params->mirr_port != to) {
2095*511e6ca0SVladimir Oltean 		dev_err(priv->ds->dev,
2096*511e6ca0SVladimir Oltean 			"Delete mirroring rules towards port %llu first\n",
2097*511e6ca0SVladimir Oltean 			general_params->mirr_port);
2098*511e6ca0SVladimir Oltean 		return -EBUSY;
2099*511e6ca0SVladimir Oltean 	}
2100*511e6ca0SVladimir Oltean 
2101*511e6ca0SVladimir Oltean 	new_mirr_port = to;
2102*511e6ca0SVladimir Oltean 	if (!enabled) {
2103*511e6ca0SVladimir Oltean 		bool keep = false;
2104*511e6ca0SVladimir Oltean 		int port;
2105*511e6ca0SVladimir Oltean 
2106*511e6ca0SVladimir Oltean 		/* Anybody still referencing mirr_port? */
2107*511e6ca0SVladimir Oltean 		for (port = 0; port < SJA1105_NUM_PORTS; port++) {
2108*511e6ca0SVladimir Oltean 			if (mac[port].ing_mirr || mac[port].egr_mirr) {
2109*511e6ca0SVladimir Oltean 				keep = true;
2110*511e6ca0SVladimir Oltean 				break;
2111*511e6ca0SVladimir Oltean 			}
2112*511e6ca0SVladimir Oltean 		}
2113*511e6ca0SVladimir Oltean 		/* Unset already_enabled for next time */
2114*511e6ca0SVladimir Oltean 		if (!keep)
2115*511e6ca0SVladimir Oltean 			new_mirr_port = SJA1105_NUM_PORTS;
2116*511e6ca0SVladimir Oltean 	}
2117*511e6ca0SVladimir Oltean 	if (new_mirr_port != general_params->mirr_port) {
2118*511e6ca0SVladimir Oltean 		general_params->mirr_port = new_mirr_port;
2119*511e6ca0SVladimir Oltean 
2120*511e6ca0SVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_GENERAL_PARAMS,
2121*511e6ca0SVladimir Oltean 						  0, general_params, true);
2122*511e6ca0SVladimir Oltean 		if (rc < 0)
2123*511e6ca0SVladimir Oltean 			return rc;
2124*511e6ca0SVladimir Oltean 	}
2125*511e6ca0SVladimir Oltean 
2126*511e6ca0SVladimir Oltean 	if (ingress)
2127*511e6ca0SVladimir Oltean 		mac[from].ing_mirr = enabled;
2128*511e6ca0SVladimir Oltean 	else
2129*511e6ca0SVladimir Oltean 		mac[from].egr_mirr = enabled;
2130*511e6ca0SVladimir Oltean 
2131*511e6ca0SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, from,
2132*511e6ca0SVladimir Oltean 					    &mac[from], true);
2133*511e6ca0SVladimir Oltean }
2134*511e6ca0SVladimir Oltean 
2135*511e6ca0SVladimir Oltean static int sja1105_mirror_add(struct dsa_switch *ds, int port,
2136*511e6ca0SVladimir Oltean 			      struct dsa_mall_mirror_tc_entry *mirror,
2137*511e6ca0SVladimir Oltean 			      bool ingress)
2138*511e6ca0SVladimir Oltean {
2139*511e6ca0SVladimir Oltean 	return sja1105_mirror_apply(ds->priv, port, mirror->to_local_port,
2140*511e6ca0SVladimir Oltean 				    ingress, true);
2141*511e6ca0SVladimir Oltean }
2142*511e6ca0SVladimir Oltean 
2143*511e6ca0SVladimir Oltean static void sja1105_mirror_del(struct dsa_switch *ds, int port,
2144*511e6ca0SVladimir Oltean 			       struct dsa_mall_mirror_tc_entry *mirror)
2145*511e6ca0SVladimir Oltean {
2146*511e6ca0SVladimir Oltean 	sja1105_mirror_apply(ds->priv, port, mirror->to_local_port,
2147*511e6ca0SVladimir Oltean 			     mirror->ingress, false);
2148*511e6ca0SVladimir Oltean }
2149*511e6ca0SVladimir Oltean 
21508aa9ebccSVladimir Oltean static const struct dsa_switch_ops sja1105_switch_ops = {
21518aa9ebccSVladimir Oltean 	.get_tag_protocol	= sja1105_get_tag_protocol,
21528aa9ebccSVladimir Oltean 	.setup			= sja1105_setup,
2153f3097be2SVladimir Oltean 	.teardown		= sja1105_teardown,
21548456721dSVladimir Oltean 	.set_ageing_time	= sja1105_set_ageing_time,
2155ad9f299aSVladimir Oltean 	.phylink_validate	= sja1105_phylink_validate,
2156af7cd036SVladimir Oltean 	.phylink_mac_config	= sja1105_mac_config,
21578400cff6SVladimir Oltean 	.phylink_mac_link_up	= sja1105_mac_link_up,
21588400cff6SVladimir Oltean 	.phylink_mac_link_down	= sja1105_mac_link_down,
215952c34e6eSVladimir Oltean 	.get_strings		= sja1105_get_strings,
216052c34e6eSVladimir Oltean 	.get_ethtool_stats	= sja1105_get_ethtool_stats,
216152c34e6eSVladimir Oltean 	.get_sset_count		= sja1105_get_sset_count,
2162bb77f36aSVladimir Oltean 	.get_ts_info		= sja1105_get_ts_info,
2163e9bf9694SVladimir Oltean 	.port_enable		= sja1105_port_enable,
2164291d1e72SVladimir Oltean 	.port_fdb_dump		= sja1105_fdb_dump,
2165291d1e72SVladimir Oltean 	.port_fdb_add		= sja1105_fdb_add,
2166291d1e72SVladimir Oltean 	.port_fdb_del		= sja1105_fdb_del,
21678aa9ebccSVladimir Oltean 	.port_bridge_join	= sja1105_bridge_join,
21688aa9ebccSVladimir Oltean 	.port_bridge_leave	= sja1105_bridge_leave,
2169640f763fSVladimir Oltean 	.port_stp_state_set	= sja1105_bridge_stp_state_set,
21706666cebcSVladimir Oltean 	.port_vlan_prepare	= sja1105_vlan_prepare,
21716666cebcSVladimir Oltean 	.port_vlan_filtering	= sja1105_vlan_filtering,
21726666cebcSVladimir Oltean 	.port_vlan_add		= sja1105_vlan_add,
21736666cebcSVladimir Oltean 	.port_vlan_del		= sja1105_vlan_del,
2174291d1e72SVladimir Oltean 	.port_mdb_prepare	= sja1105_mdb_prepare,
2175291d1e72SVladimir Oltean 	.port_mdb_add		= sja1105_mdb_add,
2176291d1e72SVladimir Oltean 	.port_mdb_del		= sja1105_mdb_del,
2177227d07a0SVladimir Oltean 	.port_deferred_xmit	= sja1105_port_deferred_xmit,
2178a602afd2SVladimir Oltean 	.port_hwtstamp_get	= sja1105_hwtstamp_get,
2179a602afd2SVladimir Oltean 	.port_hwtstamp_set	= sja1105_hwtstamp_set,
2180f3097be2SVladimir Oltean 	.port_rxtstamp		= sja1105_port_rxtstamp,
218147ed985eSVladimir Oltean 	.port_txtstamp		= sja1105_port_txtstamp,
2182317ab5b8SVladimir Oltean 	.port_setup_tc		= sja1105_port_setup_tc,
2183*511e6ca0SVladimir Oltean 	.port_mirror_add	= sja1105_mirror_add,
2184*511e6ca0SVladimir Oltean 	.port_mirror_del	= sja1105_mirror_del,
21858aa9ebccSVladimir Oltean };
21868aa9ebccSVladimir Oltean 
21878aa9ebccSVladimir Oltean static int sja1105_check_device_id(struct sja1105_private *priv)
21888aa9ebccSVladimir Oltean {
21898aa9ebccSVladimir Oltean 	const struct sja1105_regs *regs = priv->info->regs;
21908aa9ebccSVladimir Oltean 	u8 prod_id[SJA1105_SIZE_DEVICE_ID] = {0};
21918aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
2192dff79620SVladimir Oltean 	u32 device_id;
21938aa9ebccSVladimir Oltean 	u64 part_no;
21948aa9ebccSVladimir Oltean 	int rc;
21958aa9ebccSVladimir Oltean 
2196dff79620SVladimir Oltean 	rc = sja1105_xfer_u32(priv, SPI_READ, regs->device_id, &device_id);
21978aa9ebccSVladimir Oltean 	if (rc < 0)
21988aa9ebccSVladimir Oltean 		return rc;
21998aa9ebccSVladimir Oltean 
22008aa9ebccSVladimir Oltean 	if (device_id != priv->info->device_id) {
2201dff79620SVladimir Oltean 		dev_err(dev, "Expected device ID 0x%llx but read 0x%x\n",
22028aa9ebccSVladimir Oltean 			priv->info->device_id, device_id);
22038aa9ebccSVladimir Oltean 		return -ENODEV;
22048aa9ebccSVladimir Oltean 	}
22058aa9ebccSVladimir Oltean 
22061bd44870SVladimir Oltean 	rc = sja1105_xfer_buf(priv, SPI_READ, regs->prod_id, prod_id,
22071bd44870SVladimir Oltean 			      SJA1105_SIZE_DEVICE_ID);
22088aa9ebccSVladimir Oltean 	if (rc < 0)
22098aa9ebccSVladimir Oltean 		return rc;
22108aa9ebccSVladimir Oltean 
22118aa9ebccSVladimir Oltean 	sja1105_unpack(prod_id, &part_no, 19, 4, SJA1105_SIZE_DEVICE_ID);
22128aa9ebccSVladimir Oltean 
22138aa9ebccSVladimir Oltean 	if (part_no != priv->info->part_no) {
22148aa9ebccSVladimir Oltean 		dev_err(dev, "Expected part number 0x%llx but read 0x%llx\n",
22158aa9ebccSVladimir Oltean 			priv->info->part_no, part_no);
22168aa9ebccSVladimir Oltean 		return -ENODEV;
22178aa9ebccSVladimir Oltean 	}
22188aa9ebccSVladimir Oltean 
22198aa9ebccSVladimir Oltean 	return 0;
22208aa9ebccSVladimir Oltean }
22218aa9ebccSVladimir Oltean 
22228aa9ebccSVladimir Oltean static int sja1105_probe(struct spi_device *spi)
22238aa9ebccSVladimir Oltean {
2224844d7edcSVladimir Oltean 	struct sja1105_tagger_data *tagger_data;
22258aa9ebccSVladimir Oltean 	struct device *dev = &spi->dev;
22268aa9ebccSVladimir Oltean 	struct sja1105_private *priv;
22278aa9ebccSVladimir Oltean 	struct dsa_switch *ds;
2228227d07a0SVladimir Oltean 	int rc, i;
22298aa9ebccSVladimir Oltean 
22308aa9ebccSVladimir Oltean 	if (!dev->of_node) {
22318aa9ebccSVladimir Oltean 		dev_err(dev, "No DTS bindings for SJA1105 driver\n");
22328aa9ebccSVladimir Oltean 		return -EINVAL;
22338aa9ebccSVladimir Oltean 	}
22348aa9ebccSVladimir Oltean 
22358aa9ebccSVladimir Oltean 	priv = devm_kzalloc(dev, sizeof(struct sja1105_private), GFP_KERNEL);
22368aa9ebccSVladimir Oltean 	if (!priv)
22378aa9ebccSVladimir Oltean 		return -ENOMEM;
22388aa9ebccSVladimir Oltean 
22398aa9ebccSVladimir Oltean 	/* Configure the optional reset pin and bring up switch */
22408aa9ebccSVladimir Oltean 	priv->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
22418aa9ebccSVladimir Oltean 	if (IS_ERR(priv->reset_gpio))
22428aa9ebccSVladimir Oltean 		dev_dbg(dev, "reset-gpios not defined, ignoring\n");
22438aa9ebccSVladimir Oltean 	else
22448aa9ebccSVladimir Oltean 		sja1105_hw_reset(priv->reset_gpio, 1, 1);
22458aa9ebccSVladimir Oltean 
22468aa9ebccSVladimir Oltean 	/* Populate our driver private structure (priv) based on
22478aa9ebccSVladimir Oltean 	 * the device tree node that was probed (spi)
22488aa9ebccSVladimir Oltean 	 */
22498aa9ebccSVladimir Oltean 	priv->spidev = spi;
22508aa9ebccSVladimir Oltean 	spi_set_drvdata(spi, priv);
22518aa9ebccSVladimir Oltean 
22528aa9ebccSVladimir Oltean 	/* Configure the SPI bus */
22538aa9ebccSVladimir Oltean 	spi->bits_per_word = 8;
22548aa9ebccSVladimir Oltean 	rc = spi_setup(spi);
22558aa9ebccSVladimir Oltean 	if (rc < 0) {
22568aa9ebccSVladimir Oltean 		dev_err(dev, "Could not init SPI\n");
22578aa9ebccSVladimir Oltean 		return rc;
22588aa9ebccSVladimir Oltean 	}
22598aa9ebccSVladimir Oltean 
22608aa9ebccSVladimir Oltean 	priv->info = of_device_get_match_data(dev);
22618aa9ebccSVladimir Oltean 
22628aa9ebccSVladimir Oltean 	/* Detect hardware device */
22638aa9ebccSVladimir Oltean 	rc = sja1105_check_device_id(priv);
22648aa9ebccSVladimir Oltean 	if (rc < 0) {
22658aa9ebccSVladimir Oltean 		dev_err(dev, "Device ID check failed: %d\n", rc);
22668aa9ebccSVladimir Oltean 		return rc;
22678aa9ebccSVladimir Oltean 	}
22688aa9ebccSVladimir Oltean 
22698aa9ebccSVladimir Oltean 	dev_info(dev, "Probed switch chip: %s\n", priv->info->name);
22708aa9ebccSVladimir Oltean 
22718aa9ebccSVladimir Oltean 	ds = dsa_switch_alloc(dev, SJA1105_NUM_PORTS);
22728aa9ebccSVladimir Oltean 	if (!ds)
22738aa9ebccSVladimir Oltean 		return -ENOMEM;
22748aa9ebccSVladimir Oltean 
22758aa9ebccSVladimir Oltean 	ds->ops = &sja1105_switch_ops;
22768aa9ebccSVladimir Oltean 	ds->priv = priv;
22778aa9ebccSVladimir Oltean 	priv->ds = ds;
22788aa9ebccSVladimir Oltean 
2279844d7edcSVladimir Oltean 	tagger_data = &priv->tagger_data;
2280844d7edcSVladimir Oltean 	skb_queue_head_init(&tagger_data->skb_rxtstamp_queue);
2281f3097be2SVladimir Oltean 	INIT_WORK(&tagger_data->rxtstamp_work, sja1105_rxtstamp_work);
2282844d7edcSVladimir Oltean 
2283227d07a0SVladimir Oltean 	/* Connections between dsa_port and sja1105_port */
2284227d07a0SVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
2285227d07a0SVladimir Oltean 		struct sja1105_port *sp = &priv->ports[i];
2286227d07a0SVladimir Oltean 
2287227d07a0SVladimir Oltean 		ds->ports[i].priv = sp;
2288227d07a0SVladimir Oltean 		sp->dp = &ds->ports[i];
2289844d7edcSVladimir Oltean 		sp->data = tagger_data;
2290227d07a0SVladimir Oltean 	}
2291227d07a0SVladimir Oltean 	mutex_init(&priv->mgmt_lock);
2292227d07a0SVladimir Oltean 
2293317ab5b8SVladimir Oltean 	sja1105_tas_setup(ds);
2294317ab5b8SVladimir Oltean 
22958aa9ebccSVladimir Oltean 	return dsa_register_switch(priv->ds);
22968aa9ebccSVladimir Oltean }
22978aa9ebccSVladimir Oltean 
22988aa9ebccSVladimir Oltean static int sja1105_remove(struct spi_device *spi)
22998aa9ebccSVladimir Oltean {
23008aa9ebccSVladimir Oltean 	struct sja1105_private *priv = spi_get_drvdata(spi);
23018aa9ebccSVladimir Oltean 
23028aa9ebccSVladimir Oltean 	dsa_unregister_switch(priv->ds);
23038aa9ebccSVladimir Oltean 	return 0;
23048aa9ebccSVladimir Oltean }
23058aa9ebccSVladimir Oltean 
23068aa9ebccSVladimir Oltean static const struct of_device_id sja1105_dt_ids[] = {
23078aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105e", .data = &sja1105e_info },
23088aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105t", .data = &sja1105t_info },
23098aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105p", .data = &sja1105p_info },
23108aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105q", .data = &sja1105q_info },
23118aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105r", .data = &sja1105r_info },
23128aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105s", .data = &sja1105s_info },
23138aa9ebccSVladimir Oltean 	{ /* sentinel */ },
23148aa9ebccSVladimir Oltean };
23158aa9ebccSVladimir Oltean MODULE_DEVICE_TABLE(of, sja1105_dt_ids);
23168aa9ebccSVladimir Oltean 
23178aa9ebccSVladimir Oltean static struct spi_driver sja1105_driver = {
23188aa9ebccSVladimir Oltean 	.driver = {
23198aa9ebccSVladimir Oltean 		.name  = "sja1105",
23208aa9ebccSVladimir Oltean 		.owner = THIS_MODULE,
23218aa9ebccSVladimir Oltean 		.of_match_table = of_match_ptr(sja1105_dt_ids),
23228aa9ebccSVladimir Oltean 	},
23238aa9ebccSVladimir Oltean 	.probe  = sja1105_probe,
23248aa9ebccSVladimir Oltean 	.remove = sja1105_remove,
23258aa9ebccSVladimir Oltean };
23268aa9ebccSVladimir Oltean 
23278aa9ebccSVladimir Oltean module_spi_driver(sja1105_driver);
23288aa9ebccSVladimir Oltean 
23298aa9ebccSVladimir Oltean MODULE_AUTHOR("Vladimir Oltean <olteanv@gmail.com>");
23308aa9ebccSVladimir Oltean MODULE_AUTHOR("Georg Waibel <georg.waibel@sensor-technik.de>");
23318aa9ebccSVladimir Oltean MODULE_DESCRIPTION("SJA1105 Driver");
23328aa9ebccSVladimir Oltean MODULE_LICENSE("GPL v2");
2333